at91_cf.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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/platform_device.h>
  14. #include <linux/errno.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <pcmcia/ss.h>
  18. #include <asm/hardware.h>
  19. #include <asm/io.h>
  20. #include <asm/sizes.h>
  21. #include <asm/arch/board.h>
  22. #include <asm/arch/gpio.h>
  23. #include <asm/arch/at91rm9200_mc.h>
  24. /*
  25. * A0..A10 work in each range; A23 indicates I/O space; A25 is CFRNW;
  26. * some other bit in {A24,A22..A11} is nREG to flag memory access
  27. * (vs attributes). So more than 2KB/region would just be waste.
  28. * Note: These are offsets from the physical base address.
  29. */
  30. #define CF_ATTR_PHYS (0)
  31. #define CF_IO_PHYS (1 << 23)
  32. #define CF_MEM_PHYS (0x017ff800)
  33. /*--------------------------------------------------------------------------*/
  34. static const char driver_name[] = "at91_cf";
  35. struct at91_cf_socket {
  36. struct pcmcia_socket socket;
  37. unsigned present:1;
  38. struct platform_device *pdev;
  39. struct at91_cf_data *board;
  40. unsigned long phys_baseaddr;
  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)
  53. {
  54. struct at91_cf_socket *cf = _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,
  61. present ? "present" : "gone");
  62. pcmcia_parse_events(&cf->socket, SS_DETECT);
  63. }
  64. }
  65. return IRQ_HANDLED;
  66. }
  67. static int at91_cf_get_status(struct pcmcia_socket *s, u_int *sp)
  68. {
  69. struct at91_cf_socket *cf;
  70. if (!sp)
  71. return -EINVAL;
  72. cf = container_of(s, struct at91_cf_socket, socket);
  73. /* NOTE: CF is always 3VCARD */
  74. if (at91_cf_present(cf)) {
  75. int rdy = cf->board->irq_pin; /* RDY/nIRQ */
  76. int vcc = cf->board->vcc_pin;
  77. *sp = SS_DETECT | SS_3VCARD;
  78. if (!rdy || at91_get_gpio_value(rdy))
  79. *sp |= SS_READY;
  80. if (!vcc || at91_get_gpio_value(vcc))
  81. *sp |= SS_POWERON;
  82. } else
  83. *sp = 0;
  84. return 0;
  85. }
  86. static int
  87. at91_cf_set_socket(struct pcmcia_socket *sock, struct socket_state_t *s)
  88. {
  89. struct at91_cf_socket *cf;
  90. cf = container_of(sock, struct at91_cf_socket, socket);
  91. /* switch Vcc if needed and possible */
  92. if (cf->board->vcc_pin) {
  93. switch (s->Vcc) {
  94. case 0:
  95. at91_set_gpio_value(cf->board->vcc_pin, 0);
  96. break;
  97. case 33:
  98. at91_set_gpio_value(cf->board->vcc_pin, 1);
  99. break;
  100. default:
  101. return -EINVAL;
  102. }
  103. }
  104. /* toggle reset if needed */
  105. at91_set_gpio_value(cf->board->rst_pin, s->flags & SS_RESET);
  106. pr_debug("%s: Vcc %d, io_irq %d, flags %04x csc %04x\n",
  107. driver_name, s->Vcc, s->io_irq, s->flags, s->csc_mask);
  108. return 0;
  109. }
  110. static int at91_cf_ss_suspend(struct pcmcia_socket *s)
  111. {
  112. return at91_cf_set_socket(s, &dead_socket);
  113. }
  114. /* we already mapped the I/O region */
  115. static int at91_cf_set_io_map(struct pcmcia_socket *s, struct pccard_io_map *io)
  116. {
  117. struct at91_cf_socket *cf;
  118. u32 csr;
  119. cf = container_of(s, struct at91_cf_socket, socket);
  120. io->flags &= (MAP_ACTIVE | MAP_16BIT | MAP_AUTOSZ);
  121. /*
  122. * Use 16 bit accesses unless/until we need 8-bit i/o space.
  123. */
  124. csr = at91_sys_read(AT91_SMC_CSR(cf->board->chipselect)) & ~AT91_SMC_DBW;
  125. /*
  126. * NOTE: this CF controller ignores IOIS16, so we can't really do
  127. * MAP_AUTOSZ. The 16bit mode allows single byte access on either
  128. * D0-D7 (even addr) or D8-D15 (odd), so it's close enough for many
  129. * purposes (and handles ide-cs).
  130. *
  131. * The 8bit mode is needed for odd byte access on D0-D7. It seems
  132. * some cards only like that way to get at the odd byte, despite
  133. * CF 3.0 spec table 35 also giving the D8-D15 option.
  134. */
  135. if (!(io->flags & (MAP_16BIT | MAP_AUTOSZ))) {
  136. csr |= AT91_SMC_DBW_8;
  137. pr_debug("%s: 8bit i/o bus\n", driver_name);
  138. } else {
  139. csr |= AT91_SMC_DBW_16;
  140. pr_debug("%s: 16bit i/o bus\n", driver_name);
  141. }
  142. at91_sys_write(AT91_SMC_CSR(cf->board->chipselect), csr);
  143. io->start = cf->socket.io_offset;
  144. io->stop = io->start + SZ_2K - 1;
  145. return 0;
  146. }
  147. /* pcmcia layer maps/unmaps mem regions */
  148. static int
  149. at91_cf_set_mem_map(struct pcmcia_socket *s, struct pccard_mem_map *map)
  150. {
  151. struct at91_cf_socket *cf;
  152. if (map->card_start)
  153. return -EINVAL;
  154. cf = container_of(s, struct at91_cf_socket, socket);
  155. map->flags &= (MAP_ACTIVE | MAP_ATTRIB | MAP_16BIT);
  156. if (map->flags & MAP_ATTRIB)
  157. map->static_start = cf->phys_baseaddr + CF_ATTR_PHYS;
  158. else
  159. map->static_start = cf->phys_baseaddr + CF_MEM_PHYS;
  160. return 0;
  161. }
  162. static struct pccard_operations at91_cf_ops = {
  163. .init = at91_cf_ss_init,
  164. .suspend = at91_cf_ss_suspend,
  165. .get_status = at91_cf_get_status,
  166. .set_socket = at91_cf_set_socket,
  167. .set_io_map = at91_cf_set_io_map,
  168. .set_mem_map = at91_cf_set_mem_map,
  169. };
  170. /*--------------------------------------------------------------------------*/
  171. static int __init at91_cf_probe(struct platform_device *pdev)
  172. {
  173. struct at91_cf_socket *cf;
  174. struct at91_cf_data *board = pdev->dev.platform_data;
  175. struct resource *io;
  176. int status;
  177. if (!board || !board->det_pin || !board->rst_pin)
  178. return -ENODEV;
  179. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  180. if (!io)
  181. return -ENODEV;
  182. cf = kzalloc(sizeof *cf, GFP_KERNEL);
  183. if (!cf)
  184. return -ENOMEM;
  185. cf->board = board;
  186. cf->pdev = pdev;
  187. cf->phys_baseaddr = io->start;
  188. platform_set_drvdata(pdev, cf);
  189. /* must be a GPIO; ergo must trigger on both edges */
  190. status = request_irq(board->det_pin, at91_cf_irq, 0, driver_name, cf);
  191. if (status < 0)
  192. goto fail0;
  193. device_init_wakeup(&pdev->dev, 1);
  194. /*
  195. * The card driver will request this irq later as needed.
  196. * but it causes lots of "irqNN: nobody cared" messages
  197. * unless we report that we handle everything (sigh).
  198. * (Note: DK board doesn't wire the IRQ pin...)
  199. */
  200. if (board->irq_pin) {
  201. status = request_irq(board->irq_pin, at91_cf_irq,
  202. IRQF_SHARED, driver_name, cf);
  203. if (status < 0)
  204. goto fail0a;
  205. cf->socket.pci_irq = board->irq_pin;
  206. } else
  207. cf->socket.pci_irq = NR_IRQS + 1;
  208. /* pcmcia layer only remaps "real" memory not iospace */
  209. cf->socket.io_offset = (unsigned long) ioremap(cf->phys_baseaddr + CF_IO_PHYS, SZ_2K);
  210. if (!cf->socket.io_offset) {
  211. status = -ENXIO;
  212. goto fail1;
  213. }
  214. /* reserve chip-select regions */
  215. if (!request_mem_region(io->start, io->end + 1 - io->start,
  216. driver_name)) {
  217. status = -ENXIO;
  218. goto fail1;
  219. }
  220. pr_info("%s: irqs det #%d, io #%d\n", driver_name,
  221. board->det_pin, board->irq_pin);
  222. cf->socket.owner = THIS_MODULE;
  223. cf->socket.dev.parent = &pdev->dev;
  224. cf->socket.ops = &at91_cf_ops;
  225. cf->socket.resource_ops = &pccard_static_ops;
  226. cf->socket.features = SS_CAP_PCCARD | SS_CAP_STATIC_MAP
  227. | SS_CAP_MEM_ALIGN;
  228. cf->socket.map_size = SZ_2K;
  229. cf->socket.io[0].res = io;
  230. status = pcmcia_register_socket(&cf->socket);
  231. if (status < 0)
  232. goto fail2;
  233. return 0;
  234. fail2:
  235. release_mem_region(io->start, io->end + 1 - io->start);
  236. fail1:
  237. if (cf->socket.io_offset)
  238. iounmap((void __iomem *) cf->socket.io_offset);
  239. if (board->irq_pin)
  240. free_irq(board->irq_pin, cf);
  241. fail0a:
  242. device_init_wakeup(&pdev->dev, 0);
  243. free_irq(board->det_pin, cf);
  244. fail0:
  245. kfree(cf);
  246. return status;
  247. }
  248. static int __exit at91_cf_remove(struct platform_device *pdev)
  249. {
  250. struct at91_cf_socket *cf = platform_get_drvdata(pdev);
  251. struct at91_cf_data *board = cf->board;
  252. struct resource *io = cf->socket.io[0].res;
  253. pcmcia_unregister_socket(&cf->socket);
  254. if (board->irq_pin)
  255. free_irq(board->irq_pin, cf);
  256. device_init_wakeup(&pdev->dev, 0);
  257. free_irq(board->det_pin, cf);
  258. iounmap((void __iomem *) cf->socket.io_offset);
  259. release_mem_region(io->start, io->end + 1 - io->start);
  260. kfree(cf);
  261. return 0;
  262. }
  263. #ifdef CONFIG_PM
  264. static int at91_cf_suspend(struct platform_device *pdev, pm_message_t mesg)
  265. {
  266. struct at91_cf_socket *cf = platform_get_drvdata(pdev);
  267. struct at91_cf_data *board = cf->board;
  268. pcmcia_socket_dev_suspend(&pdev->dev, mesg);
  269. if (device_may_wakeup(&pdev->dev)) {
  270. enable_irq_wake(board->det_pin);
  271. if (board->irq_pin)
  272. enable_irq_wake(board->irq_pin);
  273. } else {
  274. disable_irq_wake(board->det_pin);
  275. if (board->irq_pin)
  276. disable_irq_wake(board->irq_pin);
  277. }
  278. return 0;
  279. }
  280. static int at91_cf_resume(struct platform_device *pdev)
  281. {
  282. pcmcia_socket_dev_resume(&pdev->dev);
  283. return 0;
  284. }
  285. #else
  286. #define at91_cf_suspend NULL
  287. #define at91_cf_resume NULL
  288. #endif
  289. static struct platform_driver at91_cf_driver = {
  290. .driver = {
  291. .name = (char *) driver_name,
  292. .owner = THIS_MODULE,
  293. },
  294. .probe = at91_cf_probe,
  295. .remove = __exit_p(at91_cf_remove),
  296. .suspend = at91_cf_suspend,
  297. .resume = at91_cf_resume,
  298. };
  299. /*--------------------------------------------------------------------------*/
  300. static int __init at91_cf_init(void)
  301. {
  302. return platform_driver_register(&at91_cf_driver);
  303. }
  304. module_init(at91_cf_init);
  305. static void __exit at91_cf_exit(void)
  306. {
  307. platform_driver_unregister(&at91_cf_driver);
  308. }
  309. module_exit(at91_cf_exit);
  310. MODULE_DESCRIPTION("AT91 Compact Flash Driver");
  311. MODULE_AUTHOR("David Brownell");
  312. MODULE_LICENSE("GPL");