gpio.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * drivers/mtd/nand/gpio.c
  3. *
  4. * Updated, and converted to generic GPIO based driver by Russell King.
  5. *
  6. * Written by Ben Dooks <ben@simtec.co.uk>
  7. * Based on 2.4 version by Mark Whittaker
  8. *
  9. * © 2004 Simtec Electronics
  10. *
  11. * Device driver for NAND flash that uses a memory mapped interface to
  12. * read/write the NAND commands and data, and GPIO pins for control signals
  13. * (the DT binding refers to this as "GPIO assisted NAND flash")
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License version 2 as
  17. * published by the Free Software Foundation.
  18. *
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/err.h>
  22. #include <linux/slab.h>
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/gpio.h>
  26. #include <linux/io.h>
  27. #include <linux/mtd/mtd.h>
  28. #include <linux/mtd/nand.h>
  29. #include <linux/mtd/partitions.h>
  30. #include <linux/mtd/nand-gpio.h>
  31. #include <linux/of.h>
  32. #include <linux/of_address.h>
  33. #include <linux/of_gpio.h>
  34. struct gpiomtd {
  35. void __iomem *io_sync;
  36. struct mtd_info mtd_info;
  37. struct nand_chip nand_chip;
  38. struct gpio_nand_platdata plat;
  39. };
  40. #define gpio_nand_getpriv(x) container_of(x, struct gpiomtd, mtd_info)
  41. #ifdef CONFIG_ARM
  42. /* gpio_nand_dosync()
  43. *
  44. * Make sure the GPIO state changes occur in-order with writes to NAND
  45. * memory region.
  46. * Needed on PXA due to bus-reordering within the SoC itself (see section on
  47. * I/O ordering in PXA manual (section 2.3, p35)
  48. */
  49. static void gpio_nand_dosync(struct gpiomtd *gpiomtd)
  50. {
  51. unsigned long tmp;
  52. if (gpiomtd->io_sync) {
  53. /*
  54. * Linux memory barriers don't cater for what's required here.
  55. * What's required is what's here - a read from a separate
  56. * region with a dependency on that read.
  57. */
  58. tmp = readl(gpiomtd->io_sync);
  59. asm volatile("mov %1, %0\n" : "=r" (tmp) : "r" (tmp));
  60. }
  61. }
  62. #else
  63. static inline void gpio_nand_dosync(struct gpiomtd *gpiomtd) {}
  64. #endif
  65. static void gpio_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  66. {
  67. struct gpiomtd *gpiomtd = gpio_nand_getpriv(mtd);
  68. gpio_nand_dosync(gpiomtd);
  69. if (ctrl & NAND_CTRL_CHANGE) {
  70. gpio_set_value(gpiomtd->plat.gpio_nce, !(ctrl & NAND_NCE));
  71. gpio_set_value(gpiomtd->plat.gpio_cle, !!(ctrl & NAND_CLE));
  72. gpio_set_value(gpiomtd->plat.gpio_ale, !!(ctrl & NAND_ALE));
  73. gpio_nand_dosync(gpiomtd);
  74. }
  75. if (cmd == NAND_CMD_NONE)
  76. return;
  77. writeb(cmd, gpiomtd->nand_chip.IO_ADDR_W);
  78. gpio_nand_dosync(gpiomtd);
  79. }
  80. static int gpio_nand_devready(struct mtd_info *mtd)
  81. {
  82. struct gpiomtd *gpiomtd = gpio_nand_getpriv(mtd);
  83. return gpio_get_value(gpiomtd->plat.gpio_rdy);
  84. }
  85. #ifdef CONFIG_OF
  86. static const struct of_device_id gpio_nand_id_table[] = {
  87. { .compatible = "gpio-control-nand" },
  88. {}
  89. };
  90. MODULE_DEVICE_TABLE(of, gpio_nand_id_table);
  91. static int gpio_nand_get_config_of(const struct device *dev,
  92. struct gpio_nand_platdata *plat)
  93. {
  94. u32 val;
  95. if (!dev->of_node)
  96. return -ENODEV;
  97. if (!of_property_read_u32(dev->of_node, "bank-width", &val)) {
  98. if (val == 2) {
  99. plat->options |= NAND_BUSWIDTH_16;
  100. } else if (val != 1) {
  101. dev_err(dev, "invalid bank-width %u\n", val);
  102. return -EINVAL;
  103. }
  104. }
  105. plat->gpio_rdy = of_get_gpio(dev->of_node, 0);
  106. plat->gpio_nce = of_get_gpio(dev->of_node, 1);
  107. plat->gpio_ale = of_get_gpio(dev->of_node, 2);
  108. plat->gpio_cle = of_get_gpio(dev->of_node, 3);
  109. plat->gpio_nwp = of_get_gpio(dev->of_node, 4);
  110. if (!of_property_read_u32(dev->of_node, "chip-delay", &val))
  111. plat->chip_delay = val;
  112. return 0;
  113. }
  114. static struct resource *gpio_nand_get_io_sync_of(struct platform_device *pdev)
  115. {
  116. struct resource *r;
  117. u64 addr;
  118. if (of_property_read_u64(pdev->dev.of_node,
  119. "gpio-control-nand,io-sync-reg", &addr))
  120. return NULL;
  121. r = devm_kzalloc(&pdev->dev, sizeof(*r), GFP_KERNEL);
  122. if (!r)
  123. return NULL;
  124. r->start = addr;
  125. r->end = r->start + 0x3;
  126. r->flags = IORESOURCE_MEM;
  127. return r;
  128. }
  129. #else /* CONFIG_OF */
  130. static inline int gpio_nand_get_config_of(const struct device *dev,
  131. struct gpio_nand_platdata *plat)
  132. {
  133. return -ENOSYS;
  134. }
  135. static inline struct resource *
  136. gpio_nand_get_io_sync_of(struct platform_device *pdev)
  137. {
  138. return NULL;
  139. }
  140. #endif /* CONFIG_OF */
  141. static inline int gpio_nand_get_config(const struct device *dev,
  142. struct gpio_nand_platdata *plat)
  143. {
  144. int ret = gpio_nand_get_config_of(dev, plat);
  145. if (!ret)
  146. return ret;
  147. if (dev_get_platdata(dev)) {
  148. memcpy(plat, dev_get_platdata(dev), sizeof(*plat));
  149. return 0;
  150. }
  151. return -EINVAL;
  152. }
  153. static inline struct resource *
  154. gpio_nand_get_io_sync(struct platform_device *pdev)
  155. {
  156. struct resource *r = gpio_nand_get_io_sync_of(pdev);
  157. if (r)
  158. return r;
  159. return platform_get_resource(pdev, IORESOURCE_MEM, 1);
  160. }
  161. static int gpio_nand_remove(struct platform_device *pdev)
  162. {
  163. struct gpiomtd *gpiomtd = platform_get_drvdata(pdev);
  164. nand_release(&gpiomtd->mtd_info);
  165. if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
  166. gpio_set_value(gpiomtd->plat.gpio_nwp, 0);
  167. gpio_set_value(gpiomtd->plat.gpio_nce, 1);
  168. return 0;
  169. }
  170. static int gpio_nand_probe(struct platform_device *pdev)
  171. {
  172. struct gpiomtd *gpiomtd;
  173. struct nand_chip *chip;
  174. struct resource *res;
  175. struct mtd_part_parser_data ppdata = {};
  176. int ret = 0;
  177. if (!pdev->dev.of_node && !dev_get_platdata(&pdev->dev))
  178. return -EINVAL;
  179. gpiomtd = devm_kzalloc(&pdev->dev, sizeof(*gpiomtd), GFP_KERNEL);
  180. if (!gpiomtd)
  181. return -ENOMEM;
  182. chip = &gpiomtd->nand_chip;
  183. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  184. chip->IO_ADDR_R = devm_ioremap_resource(&pdev->dev, res);
  185. if (IS_ERR(chip->IO_ADDR_R))
  186. return PTR_ERR(chip->IO_ADDR_R);
  187. res = gpio_nand_get_io_sync(pdev);
  188. if (res) {
  189. gpiomtd->io_sync = devm_ioremap_resource(&pdev->dev, res);
  190. if (IS_ERR(gpiomtd->io_sync))
  191. return PTR_ERR(gpiomtd->io_sync);
  192. }
  193. ret = gpio_nand_get_config(&pdev->dev, &gpiomtd->plat);
  194. if (ret)
  195. return ret;
  196. ret = devm_gpio_request(&pdev->dev, gpiomtd->plat.gpio_nce, "NAND NCE");
  197. if (ret)
  198. return ret;
  199. gpio_direction_output(gpiomtd->plat.gpio_nce, 1);
  200. if (gpio_is_valid(gpiomtd->plat.gpio_nwp)) {
  201. ret = devm_gpio_request(&pdev->dev, gpiomtd->plat.gpio_nwp,
  202. "NAND NWP");
  203. if (ret)
  204. return ret;
  205. }
  206. ret = devm_gpio_request(&pdev->dev, gpiomtd->plat.gpio_ale, "NAND ALE");
  207. if (ret)
  208. return ret;
  209. gpio_direction_output(gpiomtd->plat.gpio_ale, 0);
  210. ret = devm_gpio_request(&pdev->dev, gpiomtd->plat.gpio_cle, "NAND CLE");
  211. if (ret)
  212. return ret;
  213. gpio_direction_output(gpiomtd->plat.gpio_cle, 0);
  214. if (gpio_is_valid(gpiomtd->plat.gpio_rdy)) {
  215. ret = devm_gpio_request(&pdev->dev, gpiomtd->plat.gpio_rdy,
  216. "NAND RDY");
  217. if (ret)
  218. return ret;
  219. gpio_direction_input(gpiomtd->plat.gpio_rdy);
  220. chip->dev_ready = gpio_nand_devready;
  221. }
  222. chip->IO_ADDR_W = chip->IO_ADDR_R;
  223. chip->ecc.mode = NAND_ECC_SOFT;
  224. chip->options = gpiomtd->plat.options;
  225. chip->chip_delay = gpiomtd->plat.chip_delay;
  226. chip->cmd_ctrl = gpio_nand_cmd_ctrl;
  227. gpiomtd->mtd_info.priv = chip;
  228. gpiomtd->mtd_info.dev.parent = &pdev->dev;
  229. platform_set_drvdata(pdev, gpiomtd);
  230. if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
  231. gpio_direction_output(gpiomtd->plat.gpio_nwp, 1);
  232. if (nand_scan(&gpiomtd->mtd_info, 1)) {
  233. ret = -ENXIO;
  234. goto err_wp;
  235. }
  236. if (gpiomtd->plat.adjust_parts)
  237. gpiomtd->plat.adjust_parts(&gpiomtd->plat,
  238. gpiomtd->mtd_info.size);
  239. ppdata.of_node = pdev->dev.of_node;
  240. ret = mtd_device_parse_register(&gpiomtd->mtd_info, NULL, &ppdata,
  241. gpiomtd->plat.parts,
  242. gpiomtd->plat.num_parts);
  243. if (!ret)
  244. return 0;
  245. err_wp:
  246. if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
  247. gpio_set_value(gpiomtd->plat.gpio_nwp, 0);
  248. return ret;
  249. }
  250. static struct platform_driver gpio_nand_driver = {
  251. .probe = gpio_nand_probe,
  252. .remove = gpio_nand_remove,
  253. .driver = {
  254. .name = "gpio-nand",
  255. .of_match_table = of_match_ptr(gpio_nand_id_table),
  256. },
  257. };
  258. module_platform_driver(gpio_nand_driver);
  259. MODULE_LICENSE("GPL");
  260. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  261. MODULE_DESCRIPTION("GPIO NAND Driver");