at91_cf.c 10 KB

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