at91_cf.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * at91_cf.c -- AT91 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/sched.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/errno.h>
  16. #include <linux/init.h>
  17. #include <linux/interrupt.h>
  18. #include <pcmcia/ss.h>
  19. #include <asm/hardware.h>
  20. #include <asm/io.h>
  21. #include <asm/sizes.h>
  22. #include <asm/arch/at91rm9200.h>
  23. #include <asm/arch/board.h>
  24. #include <asm/arch/gpio.h>
  25. #define CF_SIZE 0x30000000 /* CS5+CS6: unavailable */
  26. /*
  27. * A0..A10 work in each range; A23 indicates I/O space; A25 is CFRNW;
  28. * some other bit in {A24,A22..A11} is nREG to flag memory access
  29. * (vs attributes). So more than 2KB/region would just be waste.
  30. */
  31. #define CF_ATTR_PHYS (AT91_CF_BASE)
  32. #define CF_IO_PHYS (AT91_CF_BASE + (1 << 23))
  33. #define CF_MEM_PHYS (AT91_CF_BASE + 0x017ff800)
  34. /*--------------------------------------------------------------------------*/
  35. static const char driver_name[] = "at91_cf";
  36. struct at91_cf_socket {
  37. struct pcmcia_socket socket;
  38. unsigned present:1;
  39. struct platform_device *pdev;
  40. struct at91_cf_data *board;
  41. };
  42. #define SZ_2K (2 * SZ_1K)
  43. static inline int at91_cf_present(struct at91_cf_socket *cf)
  44. {
  45. return !at91_get_gpio_value(cf->board->det_pin);
  46. }
  47. /*--------------------------------------------------------------------------*/
  48. static int at91_cf_ss_init(struct pcmcia_socket *s)
  49. {
  50. return 0;
  51. }
  52. static irqreturn_t at91_cf_irq(int irq, void *_cf, struct pt_regs *r)
  53. {
  54. struct at91_cf_socket *cf = (struct at91_cf_socket *) _cf;
  55. if (irq == cf->board->det_pin) {
  56. unsigned present = at91_cf_present(cf);
  57. /* kick pccard as needed */
  58. if (present != cf->present) {
  59. cf->present = present;
  60. pr_debug("%s: card %s\n", driver_name, present ? "present" : "gone");
  61. pcmcia_parse_events(&cf->socket, SS_DETECT);
  62. }
  63. }
  64. return IRQ_HANDLED;
  65. }
  66. static int at91_cf_get_status(struct pcmcia_socket *s, u_int *sp)
  67. {
  68. struct at91_cf_socket *cf;
  69. if (!sp)
  70. return -EINVAL;
  71. cf = container_of(s, struct at91_cf_socket, socket);
  72. /* NOTE: we assume 3VCARD, not XVCARD... */
  73. if (at91_cf_present(cf)) {
  74. int rdy = cf->board->irq_pin; /* RDY/nIRQ */
  75. int vcc = cf->board->vcc_pin;
  76. *sp = SS_DETECT | SS_3VCARD;
  77. if (!rdy || at91_get_gpio_value(rdy))
  78. *sp |= SS_READY;
  79. if (!vcc || at91_get_gpio_value(vcc))
  80. *sp |= SS_POWERON;
  81. } else
  82. *sp = 0;
  83. return 0;
  84. }
  85. static int at91_cf_set_socket(struct pcmcia_socket *sock, struct socket_state_t *s)
  86. {
  87. struct at91_cf_socket *cf;
  88. cf = container_of(sock, struct at91_cf_socket, socket);
  89. /* switch Vcc if needed and possible */
  90. if (cf->board->vcc_pin) {
  91. switch (s->Vcc) {
  92. case 0:
  93. at91_set_gpio_value(cf->board->vcc_pin, 0);
  94. break;
  95. case 33:
  96. at91_set_gpio_value(cf->board->vcc_pin, 1);
  97. break;
  98. default:
  99. return -EINVAL;
  100. }
  101. }
  102. /* toggle reset if needed */
  103. at91_set_gpio_value(cf->board->rst_pin, s->flags & SS_RESET);
  104. pr_debug("%s: Vcc %d, io_irq %d, flags %04x csc %04x\n",
  105. driver_name, s->Vcc, s->io_irq, s->flags, s->csc_mask);
  106. return 0;
  107. }
  108. static int at91_cf_ss_suspend(struct pcmcia_socket *s)
  109. {
  110. return at91_cf_set_socket(s, &dead_socket);
  111. }
  112. /* we already mapped the I/O region */
  113. static int at91_cf_set_io_map(struct pcmcia_socket *s, struct pccard_io_map *io)
  114. {
  115. struct at91_cf_socket *cf;
  116. u32 csr;
  117. cf = container_of(s, struct at91_cf_socket, socket);
  118. io->flags &= (MAP_ACTIVE | MAP_16BIT | MAP_AUTOSZ);
  119. /*
  120. * Use 16 bit accesses unless/until we need 8-bit i/o space.
  121. * Always set CSR4 ... PCMCIA won't always unmap things.
  122. */
  123. csr = at91_sys_read(AT91_SMC_CSR(4)) & ~AT91_SMC_DBW;
  124. /*
  125. * NOTE: this CF controller ignores IOIS16, so we can't really do
  126. * MAP_AUTOSZ. The 16bit mode allows single byte access on either
  127. * D0-D7 (even addr) or D8-D15 (odd), so it's close enough for many
  128. * purposes (and handles ide-cs).
  129. *
  130. * The 8bit mode is needed for odd byte access on D0-D7. It seems
  131. * some cards only like that way to get at the odd byte, despite
  132. * CF 3.0 spec table 35 also giving the D8-D15 option.
  133. */
  134. if (!(io->flags & (MAP_16BIT|MAP_AUTOSZ))) {
  135. csr |= AT91_SMC_DBW_8;
  136. pr_debug("%s: 8bit i/o bus\n", driver_name);
  137. } else {
  138. csr |= AT91_SMC_DBW_16;
  139. pr_debug("%s: 16bit i/o bus\n", driver_name);
  140. }
  141. at91_sys_write(AT91_SMC_CSR(4), csr);
  142. io->start = cf->socket.io_offset;
  143. io->stop = io->start + SZ_2K - 1;
  144. return 0;
  145. }
  146. /* pcmcia layer maps/unmaps mem regions */
  147. static int at91_cf_set_mem_map(struct pcmcia_socket *s, struct pccard_mem_map *map)
  148. {
  149. struct at91_cf_socket *cf;
  150. if (map->card_start)
  151. return -EINVAL;
  152. cf = container_of(s, struct at91_cf_socket, socket);
  153. map->flags &= MAP_ACTIVE|MAP_ATTRIB|MAP_16BIT;
  154. if (map->flags & MAP_ATTRIB)
  155. map->static_start = CF_ATTR_PHYS;
  156. else
  157. map->static_start = CF_MEM_PHYS;
  158. return 0;
  159. }
  160. static struct pccard_operations at91_cf_ops = {
  161. .init = at91_cf_ss_init,
  162. .suspend = at91_cf_ss_suspend,
  163. .get_status = at91_cf_get_status,
  164. .set_socket = at91_cf_set_socket,
  165. .set_io_map = at91_cf_set_io_map,
  166. .set_mem_map = at91_cf_set_mem_map,
  167. };
  168. /*--------------------------------------------------------------------------*/
  169. static int __init at91_cf_probe(struct device *dev)
  170. {
  171. struct at91_cf_socket *cf;
  172. struct at91_cf_data *board = dev->platform_data;
  173. struct platform_device *pdev = to_platform_device(dev);
  174. unsigned int csa;
  175. int status;
  176. if (!board || !board->det_pin || !board->rst_pin)
  177. return -ENODEV;
  178. cf = kcalloc(1, sizeof *cf, GFP_KERNEL);
  179. if (!cf)
  180. return -ENOMEM;
  181. cf->board = board;
  182. cf->pdev = pdev;
  183. dev_set_drvdata(dev, cf);
  184. /* CF takes over CS4, CS5, CS6 */
  185. csa = at91_sys_read(AT91_EBI_CSA);
  186. at91_sys_write(AT91_EBI_CSA, csa | AT91_EBI_CS4A_SMC_COMPACTFLASH);
  187. /* force poweron defaults for these pins ... */
  188. (void) at91_set_A_periph(AT91_PIN_PC9, 0); /* A25/CFRNW */
  189. (void) at91_set_A_periph(AT91_PIN_PC10, 0); /* NCS4/CFCS */
  190. (void) at91_set_A_periph(AT91_PIN_PC11, 0); /* NCS5/CFCE1 */
  191. (void) at91_set_A_periph(AT91_PIN_PC12, 0); /* NCS6/CFCE2 */
  192. /* nWAIT is _not_ a default setting */
  193. (void) at91_set_A_periph(AT91_PIN_PC6, 1); /* nWAIT */
  194. /*
  195. * Static memory controller timing adjustments.
  196. * REVISIT: these timings are in terms of MCK cycles, so
  197. * when MCK changes (cpufreq etc) so must these values...
  198. */
  199. at91_sys_write(AT91_SMC_CSR(4), AT91_SMC_ACSS_STD | AT91_SMC_DBW_16 | AT91_SMC_BAT | AT91_SMC_WSEN
  200. | AT91_SMC_NWS_(32) /* wait states */
  201. | AT91_SMC_RWSETUP_(6) /* setup time */
  202. | AT91_SMC_RWHOLD_(4) /* hold time */
  203. );
  204. /* must be a GPIO; ergo must trigger on both edges */
  205. status = request_irq(board->det_pin, at91_cf_irq,
  206. SA_SAMPLE_RANDOM, driver_name, cf);
  207. if (status < 0)
  208. goto fail0;
  209. /*
  210. * The card driver will request this irq later as needed.
  211. * but it causes lots of "irqNN: nobody cared" messages
  212. * unless we report that we handle everything (sigh).
  213. * (Note: DK board doesn't wire the IRQ pin...)
  214. */
  215. if (board->irq_pin) {
  216. status = request_irq(board->irq_pin, at91_cf_irq,
  217. SA_SHIRQ, driver_name, cf);
  218. if (status < 0)
  219. goto fail0a;
  220. cf->socket.pci_irq = board->irq_pin;
  221. }
  222. else
  223. cf->socket.pci_irq = NR_IRQS + 1;
  224. /* pcmcia layer only remaps "real" memory not iospace */
  225. cf->socket.io_offset = (unsigned long) ioremap(CF_IO_PHYS, SZ_2K);
  226. if (!cf->socket.io_offset)
  227. goto fail1;
  228. /* reserve CS4, CS5, and CS6 regions; but use just CS4 */
  229. if (!request_mem_region(AT91_CF_BASE, CF_SIZE, driver_name))
  230. goto fail1;
  231. pr_info("%s: irqs det #%d, io #%d\n", driver_name,
  232. board->det_pin, board->irq_pin);
  233. cf->socket.owner = THIS_MODULE;
  234. cf->socket.dev.dev = dev;
  235. cf->socket.ops = &at91_cf_ops;
  236. cf->socket.resource_ops = &pccard_static_ops;
  237. cf->socket.features = SS_CAP_PCCARD | SS_CAP_STATIC_MAP
  238. | SS_CAP_MEM_ALIGN;
  239. cf->socket.map_size = SZ_2K;
  240. cf->socket.io[0].NumPorts = SZ_2K;
  241. status = pcmcia_register_socket(&cf->socket);
  242. if (status < 0)
  243. goto fail2;
  244. return 0;
  245. fail2:
  246. iounmap((void __iomem *) cf->socket.io_offset);
  247. release_mem_region(AT91_CF_BASE, CF_SIZE);
  248. fail1:
  249. if (board->irq_pin)
  250. free_irq(board->irq_pin, cf);
  251. fail0a:
  252. free_irq(board->det_pin, cf);
  253. fail0:
  254. at91_sys_write(AT91_EBI_CSA, csa);
  255. kfree(cf);
  256. return status;
  257. }
  258. static int __exit at91_cf_remove(struct device *dev)
  259. {
  260. struct at91_cf_socket *cf = dev_get_drvdata(dev);
  261. unsigned int csa;
  262. pcmcia_unregister_socket(&cf->socket);
  263. free_irq(cf->board->irq_pin, cf);
  264. free_irq(cf->board->det_pin, cf);
  265. iounmap((void __iomem *) cf->socket.io_offset);
  266. release_mem_region(AT91_CF_BASE, CF_SIZE);
  267. csa = at91_sys_read(AT91_EBI_CSA);
  268. at91_sys_write(AT91_EBI_CSA, csa & ~AT91_EBI_CS4A);
  269. kfree(cf);
  270. return 0;
  271. }
  272. static struct device_driver at91_cf_driver = {
  273. .name = (char *) driver_name,
  274. .bus = &platform_bus_type,
  275. .probe = at91_cf_probe,
  276. .remove = __exit_p(at91_cf_remove),
  277. .suspend = pcmcia_socket_dev_suspend,
  278. .resume = pcmcia_socket_dev_resume,
  279. };
  280. /*--------------------------------------------------------------------------*/
  281. static int __init at91_cf_init(void)
  282. {
  283. return driver_register(&at91_cf_driver);
  284. }
  285. module_init(at91_cf_init);
  286. static void __exit at91_cf_exit(void)
  287. {
  288. driver_unregister(&at91_cf_driver);
  289. }
  290. module_exit(at91_cf_exit);
  291. MODULE_DESCRIPTION("AT91 Compact Flash Driver");
  292. MODULE_AUTHOR("David Brownell");
  293. MODULE_LICENSE("GPL");