electra_cf.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Copyright (C) 2007 PA Semi, Inc
  3. *
  4. * Maintained by: Olof Johansson <olof@lixom.net>
  5. *
  6. * Based on drivers/pcmcia/omap_cf.c
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/sched.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/errno.h>
  27. #include <linux/init.h>
  28. #include <linux/delay.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/mm.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/of_platform.h>
  33. #include <linux/slab.h>
  34. #include <pcmcia/ss.h>
  35. static const char driver_name[] = "electra-cf";
  36. struct electra_cf_socket {
  37. struct pcmcia_socket socket;
  38. struct timer_list timer;
  39. unsigned present:1;
  40. unsigned active:1;
  41. struct platform_device *ofdev;
  42. unsigned long mem_phys;
  43. void __iomem * mem_base;
  44. unsigned long mem_size;
  45. void __iomem * io_virt;
  46. unsigned int io_base;
  47. unsigned int io_size;
  48. u_int irq;
  49. struct resource iomem;
  50. void __iomem * gpio_base;
  51. int gpio_detect;
  52. int gpio_vsense;
  53. int gpio_3v;
  54. int gpio_5v;
  55. };
  56. #define POLL_INTERVAL (2 * HZ)
  57. static int electra_cf_present(struct electra_cf_socket *cf)
  58. {
  59. unsigned int gpio;
  60. gpio = in_le32(cf->gpio_base+0x40);
  61. return !(gpio & (1 << cf->gpio_detect));
  62. }
  63. static int electra_cf_ss_init(struct pcmcia_socket *s)
  64. {
  65. return 0;
  66. }
  67. /* the timer is primarily to kick this socket's pccardd */
  68. static void electra_cf_timer(unsigned long _cf)
  69. {
  70. struct electra_cf_socket *cf = (void *) _cf;
  71. int present = electra_cf_present(cf);
  72. if (present != cf->present) {
  73. cf->present = present;
  74. pcmcia_parse_events(&cf->socket, SS_DETECT);
  75. }
  76. if (cf->active)
  77. mod_timer(&cf->timer, jiffies + POLL_INTERVAL);
  78. }
  79. static irqreturn_t electra_cf_irq(int irq, void *_cf)
  80. {
  81. electra_cf_timer((unsigned long)_cf);
  82. return IRQ_HANDLED;
  83. }
  84. static int electra_cf_get_status(struct pcmcia_socket *s, u_int *sp)
  85. {
  86. struct electra_cf_socket *cf;
  87. if (!sp)
  88. return -EINVAL;
  89. cf = container_of(s, struct electra_cf_socket, socket);
  90. /* NOTE CF is always 3VCARD */
  91. if (electra_cf_present(cf)) {
  92. *sp = SS_READY | SS_DETECT | SS_POWERON | SS_3VCARD;
  93. s->pci_irq = cf->irq;
  94. } else
  95. *sp = 0;
  96. return 0;
  97. }
  98. static int electra_cf_set_socket(struct pcmcia_socket *sock,
  99. struct socket_state_t *s)
  100. {
  101. unsigned int gpio;
  102. unsigned int vcc;
  103. struct electra_cf_socket *cf;
  104. cf = container_of(sock, struct electra_cf_socket, socket);
  105. /* "reset" means no power in our case */
  106. vcc = (s->flags & SS_RESET) ? 0 : s->Vcc;
  107. switch (vcc) {
  108. case 0:
  109. gpio = 0;
  110. break;
  111. case 33:
  112. gpio = (1 << cf->gpio_3v);
  113. break;
  114. case 5:
  115. gpio = (1 << cf->gpio_5v);
  116. break;
  117. default:
  118. return -EINVAL;
  119. }
  120. gpio |= 1 << (cf->gpio_3v + 16); /* enwr */
  121. gpio |= 1 << (cf->gpio_5v + 16); /* enwr */
  122. out_le32(cf->gpio_base+0x90, gpio);
  123. pr_debug("%s: Vcc %d, io_irq %d, flags %04x csc %04x\n",
  124. driver_name, s->Vcc, s->io_irq, s->flags, s->csc_mask);
  125. return 0;
  126. }
  127. static int electra_cf_set_io_map(struct pcmcia_socket *s,
  128. struct pccard_io_map *io)
  129. {
  130. return 0;
  131. }
  132. static int electra_cf_set_mem_map(struct pcmcia_socket *s,
  133. struct pccard_mem_map *map)
  134. {
  135. struct electra_cf_socket *cf;
  136. if (map->card_start)
  137. return -EINVAL;
  138. cf = container_of(s, struct electra_cf_socket, socket);
  139. map->static_start = cf->mem_phys;
  140. map->flags &= MAP_ACTIVE|MAP_ATTRIB;
  141. if (!(map->flags & MAP_ATTRIB))
  142. map->static_start += 0x800;
  143. return 0;
  144. }
  145. static struct pccard_operations electra_cf_ops = {
  146. .init = electra_cf_ss_init,
  147. .get_status = electra_cf_get_status,
  148. .set_socket = electra_cf_set_socket,
  149. .set_io_map = electra_cf_set_io_map,
  150. .set_mem_map = electra_cf_set_mem_map,
  151. };
  152. static int __devinit electra_cf_probe(struct platform_device *ofdev)
  153. {
  154. struct device *device = &ofdev->dev;
  155. struct device_node *np = ofdev->dev.of_node;
  156. struct electra_cf_socket *cf;
  157. struct resource mem, io;
  158. int status;
  159. const unsigned int *prop;
  160. int err;
  161. struct vm_struct *area;
  162. err = of_address_to_resource(np, 0, &mem);
  163. if (err)
  164. return -EINVAL;
  165. err = of_address_to_resource(np, 1, &io);
  166. if (err)
  167. return -EINVAL;
  168. cf = kzalloc(sizeof *cf, GFP_KERNEL);
  169. if (!cf)
  170. return -ENOMEM;
  171. setup_timer(&cf->timer, electra_cf_timer, (unsigned long)cf);
  172. cf->irq = NO_IRQ;
  173. cf->ofdev = ofdev;
  174. cf->mem_phys = mem.start;
  175. cf->mem_size = PAGE_ALIGN(resource_size(&mem));
  176. cf->mem_base = ioremap(cf->mem_phys, cf->mem_size);
  177. cf->io_size = PAGE_ALIGN(resource_size(&io));
  178. area = __get_vm_area(cf->io_size, 0, PHB_IO_BASE, PHB_IO_END);
  179. if (area == NULL)
  180. return -ENOMEM;
  181. cf->io_virt = (void __iomem *)(area->addr);
  182. cf->gpio_base = ioremap(0xfc103000, 0x1000);
  183. dev_set_drvdata(device, cf);
  184. if (!cf->mem_base || !cf->io_virt || !cf->gpio_base ||
  185. (__ioremap_at(io.start, cf->io_virt, cf->io_size,
  186. _PAGE_NO_CACHE | _PAGE_GUARDED) == NULL)) {
  187. dev_err(device, "can't ioremap ranges\n");
  188. status = -ENOMEM;
  189. goto fail1;
  190. }
  191. cf->io_base = (unsigned long)cf->io_virt - VMALLOC_END;
  192. cf->iomem.start = (unsigned long)cf->mem_base;
  193. cf->iomem.end = (unsigned long)cf->mem_base + (mem.end - mem.start);
  194. cf->iomem.flags = IORESOURCE_MEM;
  195. cf->irq = irq_of_parse_and_map(np, 0);
  196. status = request_irq(cf->irq, electra_cf_irq, IRQF_SHARED,
  197. driver_name, cf);
  198. if (status < 0) {
  199. dev_err(device, "request_irq failed\n");
  200. goto fail1;
  201. }
  202. cf->socket.pci_irq = cf->irq;
  203. prop = of_get_property(np, "card-detect-gpio", NULL);
  204. if (!prop)
  205. goto fail1;
  206. cf->gpio_detect = *prop;
  207. prop = of_get_property(np, "card-vsense-gpio", NULL);
  208. if (!prop)
  209. goto fail1;
  210. cf->gpio_vsense = *prop;
  211. prop = of_get_property(np, "card-3v-gpio", NULL);
  212. if (!prop)
  213. goto fail1;
  214. cf->gpio_3v = *prop;
  215. prop = of_get_property(np, "card-5v-gpio", NULL);
  216. if (!prop)
  217. goto fail1;
  218. cf->gpio_5v = *prop;
  219. cf->socket.io_offset = cf->io_base;
  220. /* reserve chip-select regions */
  221. if (!request_mem_region(cf->mem_phys, cf->mem_size, driver_name)) {
  222. status = -ENXIO;
  223. dev_err(device, "Can't claim memory region\n");
  224. goto fail1;
  225. }
  226. if (!request_region(cf->io_base, cf->io_size, driver_name)) {
  227. status = -ENXIO;
  228. dev_err(device, "Can't claim I/O region\n");
  229. goto fail2;
  230. }
  231. cf->socket.owner = THIS_MODULE;
  232. cf->socket.dev.parent = &ofdev->dev;
  233. cf->socket.ops = &electra_cf_ops;
  234. cf->socket.resource_ops = &pccard_static_ops;
  235. cf->socket.features = SS_CAP_PCCARD | SS_CAP_STATIC_MAP |
  236. SS_CAP_MEM_ALIGN;
  237. cf->socket.map_size = 0x800;
  238. status = pcmcia_register_socket(&cf->socket);
  239. if (status < 0) {
  240. dev_err(device, "pcmcia_register_socket failed\n");
  241. goto fail3;
  242. }
  243. dev_info(device, "at mem 0x%lx io 0x%llx irq %d\n",
  244. cf->mem_phys, io.start, cf->irq);
  245. cf->active = 1;
  246. electra_cf_timer((unsigned long)cf);
  247. return 0;
  248. fail3:
  249. release_region(cf->io_base, cf->io_size);
  250. fail2:
  251. release_mem_region(cf->mem_phys, cf->mem_size);
  252. fail1:
  253. if (cf->irq != NO_IRQ)
  254. free_irq(cf->irq, cf);
  255. if (cf->io_virt)
  256. __iounmap_at(cf->io_virt, cf->io_size);
  257. if (cf->mem_base)
  258. iounmap(cf->mem_base);
  259. if (cf->gpio_base)
  260. iounmap(cf->gpio_base);
  261. device_init_wakeup(&ofdev->dev, 0);
  262. kfree(cf);
  263. return status;
  264. }
  265. static int __devexit electra_cf_remove(struct platform_device *ofdev)
  266. {
  267. struct device *device = &ofdev->dev;
  268. struct electra_cf_socket *cf;
  269. cf = dev_get_drvdata(device);
  270. cf->active = 0;
  271. pcmcia_unregister_socket(&cf->socket);
  272. free_irq(cf->irq, cf);
  273. del_timer_sync(&cf->timer);
  274. __iounmap_at(cf->io_virt, cf->io_size);
  275. iounmap(cf->mem_base);
  276. iounmap(cf->gpio_base);
  277. release_mem_region(cf->mem_phys, cf->mem_size);
  278. release_region(cf->io_base, cf->io_size);
  279. kfree(cf);
  280. return 0;
  281. }
  282. static const struct of_device_id electra_cf_match[] = {
  283. {
  284. .compatible = "electra-cf",
  285. },
  286. {},
  287. };
  288. MODULE_DEVICE_TABLE(of, electra_cf_match);
  289. static struct platform_driver electra_cf_driver = {
  290. .driver = {
  291. .name = (char *)driver_name,
  292. .owner = THIS_MODULE,
  293. .of_match_table = electra_cf_match,
  294. },
  295. .probe = electra_cf_probe,
  296. .remove = electra_cf_remove,
  297. };
  298. module_platform_driver(electra_cf_driver);
  299. MODULE_LICENSE("GPL");
  300. MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
  301. MODULE_DESCRIPTION("PA Semi Electra CF driver");