at91_cf.c 10 KB

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