xxs1500_ss.c 7.8 KB

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