xxs1500_ss.c 7.8 KB

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