xxs1500_ss.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PCMCIA socket code for the MyCable XXS1500 system.
  4. *
  5. * Copyright (c) 2009 Manuel Lauss <manuel.lauss@gmail.com>
  6. *
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/gpio.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/ioport.h>
  13. #include <linux/mm.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pm.h>
  17. #include <linux/resource.h>
  18. #include <linux/slab.h>
  19. #include <linux/spinlock.h>
  20. #include <pcmcia/ss.h>
  21. #include <pcmcia/cistpl.h>
  22. #include <asm/irq.h>
  23. #include <asm/mach-au1x00/au1000.h>
  24. #define MEM_MAP_SIZE 0x400000
  25. #define IO_MAP_SIZE 0x1000
  26. /*
  27. * 3.3V cards only; all interfacing is done via gpios:
  28. *
  29. * 0/1: carddetect (00 = card present, xx = huh)
  30. * 4: card irq
  31. * 204: reset (high-act)
  32. * 205: buffer enable (low-act)
  33. * 208/209: card voltage key (00,01,10,11)
  34. * 210: battwarn
  35. * 211: batdead
  36. * 214: power (low-act)
  37. */
  38. #define GPIO_CDA 0
  39. #define GPIO_CDB 1
  40. #define GPIO_CARDIRQ 4
  41. #define GPIO_RESET 204
  42. #define GPIO_OUTEN 205
  43. #define GPIO_VSL 208
  44. #define GPIO_VSH 209
  45. #define GPIO_BATTDEAD 210
  46. #define GPIO_BATTWARN 211
  47. #define GPIO_POWER 214
  48. struct xxs1500_pcmcia_sock {
  49. struct pcmcia_socket socket;
  50. void *virt_io;
  51. phys_addr_t phys_io;
  52. phys_addr_t phys_attr;
  53. phys_addr_t phys_mem;
  54. /* previous flags for set_socket() */
  55. unsigned int old_flags;
  56. };
  57. #define to_xxs_socket(x) container_of(x, struct xxs1500_pcmcia_sock, socket)
  58. static irqreturn_t cdirq(int irq, void *data)
  59. {
  60. struct xxs1500_pcmcia_sock *sock = data;
  61. pcmcia_parse_events(&sock->socket, SS_DETECT);
  62. return IRQ_HANDLED;
  63. }
  64. static int xxs1500_pcmcia_configure(struct pcmcia_socket *skt,
  65. struct socket_state_t *state)
  66. {
  67. struct xxs1500_pcmcia_sock *sock = to_xxs_socket(skt);
  68. unsigned int changed;
  69. /* power control */
  70. switch (state->Vcc) {
  71. case 0:
  72. gpio_set_value(GPIO_POWER, 1); /* power off */
  73. break;
  74. case 33:
  75. gpio_set_value(GPIO_POWER, 0); /* power on */
  76. break;
  77. case 50:
  78. default:
  79. return -EINVAL;
  80. }
  81. changed = state->flags ^ sock->old_flags;
  82. if (changed & SS_RESET) {
  83. if (state->flags & SS_RESET) {
  84. gpio_set_value(GPIO_RESET, 1); /* assert reset */
  85. gpio_set_value(GPIO_OUTEN, 1); /* buffers off */
  86. } else {
  87. gpio_set_value(GPIO_RESET, 0); /* deassert reset */
  88. gpio_set_value(GPIO_OUTEN, 0); /* buffers on */
  89. msleep(500);
  90. }
  91. }
  92. sock->old_flags = state->flags;
  93. return 0;
  94. }
  95. static int xxs1500_pcmcia_get_status(struct pcmcia_socket *skt,
  96. unsigned int *value)
  97. {
  98. unsigned int status;
  99. int i;
  100. status = 0;
  101. /* check carddetects: GPIO[0:1] must both be low */
  102. if (!gpio_get_value(GPIO_CDA) && !gpio_get_value(GPIO_CDB))
  103. status |= SS_DETECT;
  104. /* determine card voltage: GPIO[208:209] binary value */
  105. i = (!!gpio_get_value(GPIO_VSL)) | ((!!gpio_get_value(GPIO_VSH)) << 1);
  106. switch (i) {
  107. case 0:
  108. case 1:
  109. case 2:
  110. status |= SS_3VCARD; /* 3V card */
  111. break;
  112. case 3: /* 5V card, unsupported */
  113. default:
  114. status |= SS_XVCARD; /* treated as unsupported in core */
  115. }
  116. /* GPIO214: low active power switch */
  117. status |= gpio_get_value(GPIO_POWER) ? 0 : SS_POWERON;
  118. /* GPIO204: high-active reset line */
  119. status |= gpio_get_value(GPIO_RESET) ? SS_RESET : SS_READY;
  120. /* other stuff */
  121. status |= gpio_get_value(GPIO_BATTDEAD) ? 0 : SS_BATDEAD;
  122. status |= gpio_get_value(GPIO_BATTWARN) ? 0 : SS_BATWARN;
  123. *value = status;
  124. return 0;
  125. }
  126. static int xxs1500_pcmcia_sock_init(struct pcmcia_socket *skt)
  127. {
  128. gpio_direction_input(GPIO_CDA);
  129. gpio_direction_input(GPIO_CDB);
  130. gpio_direction_input(GPIO_VSL);
  131. gpio_direction_input(GPIO_VSH);
  132. gpio_direction_input(GPIO_BATTDEAD);
  133. gpio_direction_input(GPIO_BATTWARN);
  134. gpio_direction_output(GPIO_RESET, 1); /* assert reset */
  135. gpio_direction_output(GPIO_OUTEN, 1); /* disable buffers */
  136. gpio_direction_output(GPIO_POWER, 1); /* power off */
  137. return 0;
  138. }
  139. static int xxs1500_pcmcia_sock_suspend(struct pcmcia_socket *skt)
  140. {
  141. return 0;
  142. }
  143. static int au1x00_pcmcia_set_io_map(struct pcmcia_socket *skt,
  144. struct pccard_io_map *map)
  145. {
  146. struct xxs1500_pcmcia_sock *sock = to_xxs_socket(skt);
  147. map->start = (u32)sock->virt_io;
  148. map->stop = map->start + IO_MAP_SIZE;
  149. return 0;
  150. }
  151. static int au1x00_pcmcia_set_mem_map(struct pcmcia_socket *skt,
  152. struct pccard_mem_map *map)
  153. {
  154. struct xxs1500_pcmcia_sock *sock = to_xxs_socket(skt);
  155. if (map->flags & MAP_ATTRIB)
  156. map->static_start = sock->phys_attr + map->card_start;
  157. else
  158. map->static_start = sock->phys_mem + map->card_start;
  159. return 0;
  160. }
  161. static struct pccard_operations xxs1500_pcmcia_operations = {
  162. .init = xxs1500_pcmcia_sock_init,
  163. .suspend = xxs1500_pcmcia_sock_suspend,
  164. .get_status = xxs1500_pcmcia_get_status,
  165. .set_socket = xxs1500_pcmcia_configure,
  166. .set_io_map = au1x00_pcmcia_set_io_map,
  167. .set_mem_map = au1x00_pcmcia_set_mem_map,
  168. };
  169. static int xxs1500_pcmcia_probe(struct platform_device *pdev)
  170. {
  171. struct xxs1500_pcmcia_sock *sock;
  172. struct resource *r;
  173. int ret, irq;
  174. sock = kzalloc(sizeof(struct xxs1500_pcmcia_sock), GFP_KERNEL);
  175. if (!sock)
  176. return -ENOMEM;
  177. ret = -ENODEV;
  178. /* 36bit PCMCIA Attribute area address */
  179. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-attr");
  180. if (!r) {
  181. dev_err(&pdev->dev, "missing 'pcmcia-attr' resource!\n");
  182. goto out0;
  183. }
  184. sock->phys_attr = r->start;
  185. /* 36bit PCMCIA Memory area address */
  186. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-mem");
  187. if (!r) {
  188. dev_err(&pdev->dev, "missing 'pcmcia-mem' resource!\n");
  189. goto out0;
  190. }
  191. sock->phys_mem = r->start;
  192. /* 36bit PCMCIA IO area address */
  193. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-io");
  194. if (!r) {
  195. dev_err(&pdev->dev, "missing 'pcmcia-io' resource!\n");
  196. goto out0;
  197. }
  198. sock->phys_io = r->start;
  199. /*
  200. * PCMCIA client drivers use the inb/outb macros to access
  201. * the IO registers. Since mips_io_port_base is added
  202. * to the access address of the mips implementation of
  203. * inb/outb, we need to subtract it here because we want
  204. * to access the I/O or MEM address directly, without
  205. * going through this "mips_io_port_base" mechanism.
  206. */
  207. sock->virt_io = (void *)(ioremap(sock->phys_io, IO_MAP_SIZE) -
  208. mips_io_port_base);
  209. if (!sock->virt_io) {
  210. dev_err(&pdev->dev, "cannot remap IO area\n");
  211. ret = -ENOMEM;
  212. goto out0;
  213. }
  214. sock->socket.ops = &xxs1500_pcmcia_operations;
  215. sock->socket.owner = THIS_MODULE;
  216. sock->socket.pci_irq = gpio_to_irq(GPIO_CARDIRQ);
  217. sock->socket.features = SS_CAP_STATIC_MAP | SS_CAP_PCCARD;
  218. sock->socket.map_size = MEM_MAP_SIZE;
  219. sock->socket.io_offset = (unsigned long)sock->virt_io;
  220. sock->socket.dev.parent = &pdev->dev;
  221. sock->socket.resource_ops = &pccard_static_ops;
  222. platform_set_drvdata(pdev, sock);
  223. /* setup carddetect irq: use one of the 2 GPIOs as an
  224. * edge detector.
  225. */
  226. irq = gpio_to_irq(GPIO_CDA);
  227. irq_set_irq_type(irq, IRQ_TYPE_EDGE_BOTH);
  228. ret = request_irq(irq, cdirq, 0, "pcmcia_carddetect", sock);
  229. if (ret) {
  230. dev_err(&pdev->dev, "cannot setup cd irq\n");
  231. goto out1;
  232. }
  233. ret = pcmcia_register_socket(&sock->socket);
  234. if (ret) {
  235. dev_err(&pdev->dev, "failed to register\n");
  236. goto out2;
  237. }
  238. printk(KERN_INFO "MyCable XXS1500 PCMCIA socket services\n");
  239. return 0;
  240. out2:
  241. free_irq(gpio_to_irq(GPIO_CDA), sock);
  242. out1:
  243. iounmap((void *)(sock->virt_io + (u32)mips_io_port_base));
  244. out0:
  245. kfree(sock);
  246. return ret;
  247. }
  248. static int xxs1500_pcmcia_remove(struct platform_device *pdev)
  249. {
  250. struct xxs1500_pcmcia_sock *sock = platform_get_drvdata(pdev);
  251. pcmcia_unregister_socket(&sock->socket);
  252. free_irq(gpio_to_irq(GPIO_CDA), sock);
  253. iounmap((void *)(sock->virt_io + (u32)mips_io_port_base));
  254. kfree(sock);
  255. return 0;
  256. }
  257. static struct platform_driver xxs1500_pcmcia_socket_driver = {
  258. .driver = {
  259. .name = "xxs1500_pcmcia",
  260. },
  261. .probe = xxs1500_pcmcia_probe,
  262. .remove = xxs1500_pcmcia_remove,
  263. };
  264. module_platform_driver(xxs1500_pcmcia_socket_driver);
  265. MODULE_LICENSE("GPL");
  266. MODULE_DESCRIPTION("PCMCIA Socket Services for MyCable XXS1500 systems");
  267. MODULE_AUTHOR("Manuel Lauss");