omap_cf.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * omap_cf.c -- OMAP 16xx CompactFlash controller driver
  3. *
  4. * Copyright (c) 2005 David Brownell
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/errno.h>
  15. #include <linux/init.h>
  16. #include <linux/delay.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/slab.h>
  19. #include <pcmcia/ss.h>
  20. #include <mach/hardware.h>
  21. #include <asm/io.h>
  22. #include <asm/sizes.h>
  23. #include <mach/mux.h>
  24. #include <mach/tc.h>
  25. /* NOTE: don't expect this to support many I/O cards. The 16xx chips have
  26. * hard-wired timings to support Compact Flash memory cards; they won't work
  27. * with various other devices (like WLAN adapters) without some external
  28. * logic to help out.
  29. *
  30. * NOTE: CF controller docs disagree with address space docs as to where
  31. * CF_BASE really lives; this is a doc erratum.
  32. */
  33. #define CF_BASE 0xfffe2800
  34. /* status; read after IRQ */
  35. #define CF_STATUS (CF_BASE + 0x00)
  36. # define CF_STATUS_BAD_READ (1 << 2)
  37. # define CF_STATUS_BAD_WRITE (1 << 1)
  38. # define CF_STATUS_CARD_DETECT (1 << 0)
  39. /* which chipselect (CS0..CS3) is used for CF (active low) */
  40. #define CF_CFG (CF_BASE + 0x02)
  41. /* card reset */
  42. #define CF_CONTROL (CF_BASE + 0x04)
  43. # define CF_CONTROL_RESET (1 << 0)
  44. #define omap_cf_present() (!(omap_readw(CF_STATUS) & CF_STATUS_CARD_DETECT))
  45. /*--------------------------------------------------------------------------*/
  46. static const char driver_name[] = "omap_cf";
  47. struct omap_cf_socket {
  48. struct pcmcia_socket socket;
  49. struct timer_list timer;
  50. unsigned present:1;
  51. unsigned active:1;
  52. struct platform_device *pdev;
  53. unsigned long phys_cf;
  54. u_int irq;
  55. struct resource iomem;
  56. };
  57. #define POLL_INTERVAL (2 * HZ)
  58. /*--------------------------------------------------------------------------*/
  59. static int omap_cf_ss_init(struct pcmcia_socket *s)
  60. {
  61. return 0;
  62. }
  63. /* the timer is primarily to kick this socket's pccardd */
  64. static void omap_cf_timer(struct timer_list *t)
  65. {
  66. struct omap_cf_socket *cf = from_timer(cf, t, timer);
  67. unsigned present = omap_cf_present();
  68. if (present != cf->present) {
  69. cf->present = present;
  70. pr_debug("%s: card %s\n", driver_name,
  71. present ? "present" : "gone");
  72. pcmcia_parse_events(&cf->socket, SS_DETECT);
  73. }
  74. if (cf->active)
  75. mod_timer(&cf->timer, jiffies + POLL_INTERVAL);
  76. }
  77. /* This irq handler prevents "irqNNN: nobody cared" messages as drivers
  78. * claim the card's IRQ. It may also detect some card insertions, but
  79. * not removals; it can't always eliminate timer irqs.
  80. */
  81. static irqreturn_t omap_cf_irq(int irq, void *_cf)
  82. {
  83. struct omap_cf_socket *cf = (struct omap_cf_socket *)_cf;
  84. omap_cf_timer(&cf->timer);
  85. return IRQ_HANDLED;
  86. }
  87. static int omap_cf_get_status(struct pcmcia_socket *s, u_int *sp)
  88. {
  89. if (!sp)
  90. return -EINVAL;
  91. /* NOTE CF is always 3VCARD */
  92. if (omap_cf_present()) {
  93. struct omap_cf_socket *cf;
  94. *sp = SS_READY | SS_DETECT | SS_POWERON | SS_3VCARD;
  95. cf = container_of(s, struct omap_cf_socket, socket);
  96. s->pcmcia_irq = 0;
  97. s->pci_irq = cf->irq;
  98. } else
  99. *sp = 0;
  100. return 0;
  101. }
  102. static int
  103. omap_cf_set_socket(struct pcmcia_socket *sock, struct socket_state_t *s)
  104. {
  105. u16 control;
  106. /* REVISIT some non-OSK boards may support power switching */
  107. switch (s->Vcc) {
  108. case 0:
  109. case 33:
  110. break;
  111. default:
  112. return -EINVAL;
  113. }
  114. control = omap_readw(CF_CONTROL);
  115. if (s->flags & SS_RESET)
  116. omap_writew(CF_CONTROL_RESET, CF_CONTROL);
  117. else
  118. omap_writew(0, CF_CONTROL);
  119. pr_debug("%s: Vcc %d, io_irq %d, flags %04x csc %04x\n",
  120. driver_name, s->Vcc, s->io_irq, s->flags, s->csc_mask);
  121. return 0;
  122. }
  123. static int omap_cf_ss_suspend(struct pcmcia_socket *s)
  124. {
  125. pr_debug("%s: %s\n", driver_name, __func__);
  126. return omap_cf_set_socket(s, &dead_socket);
  127. }
  128. /* regions are 2K each: mem, attrib, io (and reserved-for-ide) */
  129. static int
  130. omap_cf_set_io_map(struct pcmcia_socket *s, struct pccard_io_map *io)
  131. {
  132. struct omap_cf_socket *cf;
  133. cf = container_of(s, struct omap_cf_socket, socket);
  134. io->flags &= MAP_ACTIVE|MAP_ATTRIB|MAP_16BIT;
  135. io->start = cf->phys_cf + SZ_4K;
  136. io->stop = io->start + SZ_2K - 1;
  137. return 0;
  138. }
  139. static int
  140. omap_cf_set_mem_map(struct pcmcia_socket *s, struct pccard_mem_map *map)
  141. {
  142. struct omap_cf_socket *cf;
  143. if (map->card_start)
  144. return -EINVAL;
  145. cf = container_of(s, struct omap_cf_socket, socket);
  146. map->static_start = cf->phys_cf;
  147. map->flags &= MAP_ACTIVE|MAP_ATTRIB|MAP_16BIT;
  148. if (map->flags & MAP_ATTRIB)
  149. map->static_start += SZ_2K;
  150. return 0;
  151. }
  152. static struct pccard_operations omap_cf_ops = {
  153. .init = omap_cf_ss_init,
  154. .suspend = omap_cf_ss_suspend,
  155. .get_status = omap_cf_get_status,
  156. .set_socket = omap_cf_set_socket,
  157. .set_io_map = omap_cf_set_io_map,
  158. .set_mem_map = omap_cf_set_mem_map,
  159. };
  160. /*--------------------------------------------------------------------------*/
  161. /*
  162. * NOTE: right now the only board-specific platform_data is
  163. * "what chipselect is used". Boards could want more.
  164. */
  165. static int __init omap_cf_probe(struct platform_device *pdev)
  166. {
  167. unsigned seg;
  168. struct omap_cf_socket *cf;
  169. int irq;
  170. int status;
  171. seg = (int) pdev->dev.platform_data;
  172. if (seg == 0 || seg > 3)
  173. return -ENODEV;
  174. /* either CFLASH.IREQ (INT_1610_CF) or some GPIO */
  175. irq = platform_get_irq(pdev, 0);
  176. if (irq < 0)
  177. return -EINVAL;
  178. cf = kzalloc(sizeof *cf, GFP_KERNEL);
  179. if (!cf)
  180. return -ENOMEM;
  181. timer_setup(&cf->timer, omap_cf_timer, 0);
  182. cf->pdev = pdev;
  183. platform_set_drvdata(pdev, cf);
  184. /* this primarily just shuts up irq handling noise */
  185. status = request_irq(irq, omap_cf_irq, IRQF_SHARED,
  186. driver_name, cf);
  187. if (status < 0)
  188. goto fail0;
  189. cf->irq = irq;
  190. cf->socket.pci_irq = irq;
  191. switch (seg) {
  192. /* NOTE: CS0 could be configured too ... */
  193. case 1:
  194. cf->phys_cf = OMAP_CS1_PHYS;
  195. break;
  196. case 2:
  197. cf->phys_cf = OMAP_CS2_PHYS;
  198. break;
  199. case 3:
  200. cf->phys_cf = omap_cs3_phys();
  201. break;
  202. default:
  203. goto fail1;
  204. }
  205. cf->iomem.start = cf->phys_cf;
  206. cf->iomem.end = cf->iomem.end + SZ_8K - 1;
  207. cf->iomem.flags = IORESOURCE_MEM;
  208. /* pcmcia layer only remaps "real" memory */
  209. cf->socket.io_offset = (unsigned long)
  210. ioremap(cf->phys_cf + SZ_4K, SZ_2K);
  211. if (!cf->socket.io_offset)
  212. goto fail1;
  213. if (!request_mem_region(cf->phys_cf, SZ_8K, driver_name))
  214. goto fail1;
  215. /* NOTE: CF conflicts with MMC1 */
  216. omap_cfg_reg(W11_1610_CF_CD1);
  217. omap_cfg_reg(P11_1610_CF_CD2);
  218. omap_cfg_reg(R11_1610_CF_IOIS16);
  219. omap_cfg_reg(V10_1610_CF_IREQ);
  220. omap_cfg_reg(W10_1610_CF_RESET);
  221. omap_writew(~(1 << seg), CF_CFG);
  222. pr_info("%s: cs%d on irq %d\n", driver_name, seg, irq);
  223. /* NOTE: better EMIFS setup might support more cards; but the
  224. * TRM only shows how to affect regular flash signals, not their
  225. * CF/PCMCIA variants...
  226. */
  227. pr_debug("%s: cs%d, previous ccs %08x acs %08x\n", driver_name,
  228. seg, omap_readl(EMIFS_CCS(seg)), omap_readl(EMIFS_ACS(seg)));
  229. omap_writel(0x0004a1b3, EMIFS_CCS(seg)); /* synch mode 4 etc */
  230. omap_writel(0x00000000, EMIFS_ACS(seg)); /* OE hold/setup */
  231. /* CF uses armxor_ck, which is "always" available */
  232. pr_debug("%s: sts %04x cfg %04x control %04x %s\n", driver_name,
  233. omap_readw(CF_STATUS), omap_readw(CF_CFG),
  234. omap_readw(CF_CONTROL),
  235. omap_cf_present() ? "present" : "(not present)");
  236. cf->socket.owner = THIS_MODULE;
  237. cf->socket.dev.parent = &pdev->dev;
  238. cf->socket.ops = &omap_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 = SZ_2K;
  243. cf->socket.io[0].res = &cf->iomem;
  244. status = pcmcia_register_socket(&cf->socket);
  245. if (status < 0)
  246. goto fail2;
  247. cf->active = 1;
  248. mod_timer(&cf->timer, jiffies + POLL_INTERVAL);
  249. return 0;
  250. fail2:
  251. release_mem_region(cf->phys_cf, SZ_8K);
  252. fail1:
  253. if (cf->socket.io_offset)
  254. iounmap((void __iomem *) cf->socket.io_offset);
  255. free_irq(irq, cf);
  256. fail0:
  257. kfree(cf);
  258. return status;
  259. }
  260. static int __exit omap_cf_remove(struct platform_device *pdev)
  261. {
  262. struct omap_cf_socket *cf = platform_get_drvdata(pdev);
  263. cf->active = 0;
  264. pcmcia_unregister_socket(&cf->socket);
  265. del_timer_sync(&cf->timer);
  266. iounmap((void __iomem *) cf->socket.io_offset);
  267. release_mem_region(cf->phys_cf, SZ_8K);
  268. free_irq(cf->irq, cf);
  269. kfree(cf);
  270. return 0;
  271. }
  272. static struct platform_driver omap_cf_driver = {
  273. .driver = {
  274. .name = (char *) driver_name,
  275. },
  276. .remove = __exit_p(omap_cf_remove),
  277. };
  278. static int __init omap_cf_init(void)
  279. {
  280. if (cpu_is_omap16xx())
  281. return platform_driver_probe(&omap_cf_driver, omap_cf_probe);
  282. return -ENODEV;
  283. }
  284. static void __exit omap_cf_exit(void)
  285. {
  286. if (cpu_is_omap16xx())
  287. platform_driver_unregister(&omap_cf_driver);
  288. }
  289. module_init(omap_cf_init);
  290. module_exit(omap_cf_exit);
  291. MODULE_DESCRIPTION("OMAP CF Driver");
  292. MODULE_LICENSE("GPL");
  293. MODULE_ALIAS("platform:omap_cf");