electra_cf.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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_address.h>
  33. #include <linux/of_irq.h>
  34. #include <linux/of_platform.h>
  35. #include <linux/slab.h>
  36. #include <pcmcia/ss.h>
  37. static const char driver_name[] = "electra-cf";
  38. struct electra_cf_socket {
  39. struct pcmcia_socket socket;
  40. struct timer_list timer;
  41. unsigned present:1;
  42. unsigned active:1;
  43. struct platform_device *ofdev;
  44. unsigned long mem_phys;
  45. void __iomem *mem_base;
  46. unsigned long mem_size;
  47. void __iomem *io_virt;
  48. unsigned int io_base;
  49. unsigned int io_size;
  50. u_int irq;
  51. struct resource iomem;
  52. void __iomem *gpio_base;
  53. int gpio_detect;
  54. int gpio_vsense;
  55. int gpio_3v;
  56. int gpio_5v;
  57. };
  58. #define POLL_INTERVAL (2 * HZ)
  59. static int electra_cf_present(struct electra_cf_socket *cf)
  60. {
  61. unsigned int gpio;
  62. gpio = in_le32(cf->gpio_base+0x40);
  63. return !(gpio & (1 << cf->gpio_detect));
  64. }
  65. static int electra_cf_ss_init(struct pcmcia_socket *s)
  66. {
  67. return 0;
  68. }
  69. /* the timer is primarily to kick this socket's pccardd */
  70. static void electra_cf_timer(struct timer_list *t)
  71. {
  72. struct electra_cf_socket *cf = from_timer(cf, t, timer);
  73. int present = electra_cf_present(cf);
  74. if (present != cf->present) {
  75. cf->present = present;
  76. pcmcia_parse_events(&cf->socket, SS_DETECT);
  77. }
  78. if (cf->active)
  79. mod_timer(&cf->timer, jiffies + POLL_INTERVAL);
  80. }
  81. static irqreturn_t electra_cf_irq(int irq, void *_cf)
  82. {
  83. struct electra_cf_socket *cf = _cf;
  84. electra_cf_timer(&cf->timer);
  85. return IRQ_HANDLED;
  86. }
  87. static int electra_cf_get_status(struct pcmcia_socket *s, u_int *sp)
  88. {
  89. struct electra_cf_socket *cf;
  90. if (!sp)
  91. return -EINVAL;
  92. cf = container_of(s, struct electra_cf_socket, socket);
  93. /* NOTE CF is always 3VCARD */
  94. if (electra_cf_present(cf)) {
  95. *sp = SS_READY | SS_DETECT | SS_POWERON | SS_3VCARD;
  96. s->pci_irq = cf->irq;
  97. } else
  98. *sp = 0;
  99. return 0;
  100. }
  101. static int electra_cf_set_socket(struct pcmcia_socket *sock,
  102. struct socket_state_t *s)
  103. {
  104. unsigned int gpio;
  105. unsigned int vcc;
  106. struct electra_cf_socket *cf;
  107. cf = container_of(sock, struct electra_cf_socket, socket);
  108. /* "reset" means no power in our case */
  109. vcc = (s->flags & SS_RESET) ? 0 : s->Vcc;
  110. switch (vcc) {
  111. case 0:
  112. gpio = 0;
  113. break;
  114. case 33:
  115. gpio = (1 << cf->gpio_3v);
  116. break;
  117. case 5:
  118. gpio = (1 << cf->gpio_5v);
  119. break;
  120. default:
  121. return -EINVAL;
  122. }
  123. gpio |= 1 << (cf->gpio_3v + 16); /* enwr */
  124. gpio |= 1 << (cf->gpio_5v + 16); /* enwr */
  125. out_le32(cf->gpio_base+0x90, gpio);
  126. pr_debug("%s: Vcc %d, io_irq %d, flags %04x csc %04x\n",
  127. driver_name, s->Vcc, s->io_irq, s->flags, s->csc_mask);
  128. return 0;
  129. }
  130. static int electra_cf_set_io_map(struct pcmcia_socket *s,
  131. struct pccard_io_map *io)
  132. {
  133. return 0;
  134. }
  135. static int electra_cf_set_mem_map(struct pcmcia_socket *s,
  136. struct pccard_mem_map *map)
  137. {
  138. struct electra_cf_socket *cf;
  139. if (map->card_start)
  140. return -EINVAL;
  141. cf = container_of(s, struct electra_cf_socket, socket);
  142. map->static_start = cf->mem_phys;
  143. map->flags &= MAP_ACTIVE|MAP_ATTRIB;
  144. if (!(map->flags & MAP_ATTRIB))
  145. map->static_start += 0x800;
  146. return 0;
  147. }
  148. static struct pccard_operations electra_cf_ops = {
  149. .init = electra_cf_ss_init,
  150. .get_status = electra_cf_get_status,
  151. .set_socket = electra_cf_set_socket,
  152. .set_io_map = electra_cf_set_io_map,
  153. .set_mem_map = electra_cf_set_mem_map,
  154. };
  155. static int electra_cf_probe(struct platform_device *ofdev)
  156. {
  157. struct device *device = &ofdev->dev;
  158. struct device_node *np = ofdev->dev.of_node;
  159. struct electra_cf_socket *cf;
  160. struct resource mem, io;
  161. int status;
  162. const unsigned int *prop;
  163. int err;
  164. struct vm_struct *area;
  165. err = of_address_to_resource(np, 0, &mem);
  166. if (err)
  167. return -EINVAL;
  168. err = of_address_to_resource(np, 1, &io);
  169. if (err)
  170. return -EINVAL;
  171. cf = kzalloc(sizeof(*cf), GFP_KERNEL);
  172. if (!cf)
  173. return -ENOMEM;
  174. timer_setup(&cf->timer, electra_cf_timer, 0);
  175. cf->irq = 0;
  176. cf->ofdev = ofdev;
  177. cf->mem_phys = mem.start;
  178. cf->mem_size = PAGE_ALIGN(resource_size(&mem));
  179. cf->mem_base = ioremap(cf->mem_phys, cf->mem_size);
  180. cf->io_size = PAGE_ALIGN(resource_size(&io));
  181. area = __get_vm_area(cf->io_size, 0, PHB_IO_BASE, PHB_IO_END);
  182. if (area == NULL) {
  183. status = -ENOMEM;
  184. goto fail1;
  185. }
  186. cf->io_virt = (void __iomem *)(area->addr);
  187. cf->gpio_base = ioremap(0xfc103000, 0x1000);
  188. dev_set_drvdata(device, cf);
  189. if (!cf->mem_base || !cf->io_virt || !cf->gpio_base ||
  190. (__ioremap_at(io.start, cf->io_virt, cf->io_size,
  191. pgprot_val(pgprot_noncached(__pgprot(0)))) == NULL)) {
  192. dev_err(device, "can't ioremap ranges\n");
  193. status = -ENOMEM;
  194. goto fail1;
  195. }
  196. cf->io_base = (unsigned long)cf->io_virt - VMALLOC_END;
  197. cf->iomem.start = (unsigned long)cf->mem_base;
  198. cf->iomem.end = (unsigned long)cf->mem_base + (mem.end - mem.start);
  199. cf->iomem.flags = IORESOURCE_MEM;
  200. cf->irq = irq_of_parse_and_map(np, 0);
  201. status = request_irq(cf->irq, electra_cf_irq, IRQF_SHARED,
  202. driver_name, cf);
  203. if (status < 0) {
  204. dev_err(device, "request_irq failed\n");
  205. goto fail1;
  206. }
  207. cf->socket.pci_irq = cf->irq;
  208. prop = of_get_property(np, "card-detect-gpio", NULL);
  209. if (!prop)
  210. goto fail1;
  211. cf->gpio_detect = *prop;
  212. prop = of_get_property(np, "card-vsense-gpio", NULL);
  213. if (!prop)
  214. goto fail1;
  215. cf->gpio_vsense = *prop;
  216. prop = of_get_property(np, "card-3v-gpio", NULL);
  217. if (!prop)
  218. goto fail1;
  219. cf->gpio_3v = *prop;
  220. prop = of_get_property(np, "card-5v-gpio", NULL);
  221. if (!prop)
  222. goto fail1;
  223. cf->gpio_5v = *prop;
  224. cf->socket.io_offset = cf->io_base;
  225. /* reserve chip-select regions */
  226. if (!request_mem_region(cf->mem_phys, cf->mem_size, driver_name)) {
  227. status = -ENXIO;
  228. dev_err(device, "Can't claim memory region\n");
  229. goto fail1;
  230. }
  231. if (!request_region(cf->io_base, cf->io_size, driver_name)) {
  232. status = -ENXIO;
  233. dev_err(device, "Can't claim I/O region\n");
  234. goto fail2;
  235. }
  236. cf->socket.owner = THIS_MODULE;
  237. cf->socket.dev.parent = &ofdev->dev;
  238. cf->socket.ops = &electra_cf_ops;
  239. cf->socket.resource_ops = &pccard_static_ops;
  240. cf->socket.features = SS_CAP_PCCARD | SS_CAP_STATIC_MAP |
  241. SS_CAP_MEM_ALIGN;
  242. cf->socket.map_size = 0x800;
  243. status = pcmcia_register_socket(&cf->socket);
  244. if (status < 0) {
  245. dev_err(device, "pcmcia_register_socket failed\n");
  246. goto fail3;
  247. }
  248. dev_info(device, "at mem 0x%lx io 0x%llx irq %d\n",
  249. cf->mem_phys, io.start, cf->irq);
  250. cf->active = 1;
  251. electra_cf_timer(&cf->timer);
  252. return 0;
  253. fail3:
  254. release_region(cf->io_base, cf->io_size);
  255. fail2:
  256. release_mem_region(cf->mem_phys, cf->mem_size);
  257. fail1:
  258. if (cf->irq)
  259. free_irq(cf->irq, cf);
  260. if (cf->io_virt)
  261. __iounmap_at(cf->io_virt, cf->io_size);
  262. if (cf->mem_base)
  263. iounmap(cf->mem_base);
  264. if (cf->gpio_base)
  265. iounmap(cf->gpio_base);
  266. if (area)
  267. device_init_wakeup(&ofdev->dev, 0);
  268. kfree(cf);
  269. return status;
  270. }
  271. static int electra_cf_remove(struct platform_device *ofdev)
  272. {
  273. struct device *device = &ofdev->dev;
  274. struct electra_cf_socket *cf;
  275. cf = dev_get_drvdata(device);
  276. cf->active = 0;
  277. pcmcia_unregister_socket(&cf->socket);
  278. free_irq(cf->irq, cf);
  279. del_timer_sync(&cf->timer);
  280. __iounmap_at(cf->io_virt, cf->io_size);
  281. iounmap(cf->mem_base);
  282. iounmap(cf->gpio_base);
  283. release_mem_region(cf->mem_phys, cf->mem_size);
  284. release_region(cf->io_base, cf->io_size);
  285. kfree(cf);
  286. return 0;
  287. }
  288. static const struct of_device_id electra_cf_match[] = {
  289. {
  290. .compatible = "electra-cf",
  291. },
  292. {},
  293. };
  294. MODULE_DEVICE_TABLE(of, electra_cf_match);
  295. static struct platform_driver electra_cf_driver = {
  296. .driver = {
  297. .name = driver_name,
  298. .of_match_table = electra_cf_match,
  299. },
  300. .probe = electra_cf_probe,
  301. .remove = electra_cf_remove,
  302. };
  303. module_platform_driver(electra_cf_driver);
  304. MODULE_LICENSE("GPL");
  305. MODULE_AUTHOR("Olof Johansson <olof@lixom.net>");
  306. MODULE_DESCRIPTION("PA Semi Electra CF driver");