gpio.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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/rawnand.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 nand_chip nand_chip;
  37. struct gpio_nand_platdata plat;
  38. };
  39. static inline struct gpiomtd *gpio_nand_getpriv(struct mtd_info *mtd)
  40. {
  41. return container_of(mtd_to_nand(mtd), struct gpiomtd, nand_chip);
  42. }
  43. #ifdef CONFIG_ARM
  44. /* gpio_nand_dosync()
  45. *
  46. * Make sure the GPIO state changes occur in-order with writes to NAND
  47. * memory region.
  48. * Needed on PXA due to bus-reordering within the SoC itself (see section on
  49. * I/O ordering in PXA manual (section 2.3, p35)
  50. */
  51. static void gpio_nand_dosync(struct gpiomtd *gpiomtd)
  52. {
  53. unsigned long tmp;
  54. if (gpiomtd->io_sync) {
  55. /*
  56. * Linux memory barriers don't cater for what's required here.
  57. * What's required is what's here - a read from a separate
  58. * region with a dependency on that read.
  59. */
  60. tmp = readl(gpiomtd->io_sync);
  61. asm volatile("mov %1, %0\n" : "=r" (tmp) : "r" (tmp));
  62. }
  63. }
  64. #else
  65. static inline void gpio_nand_dosync(struct gpiomtd *gpiomtd) {}
  66. #endif
  67. static void gpio_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  68. {
  69. struct gpiomtd *gpiomtd = gpio_nand_getpriv(mtd);
  70. gpio_nand_dosync(gpiomtd);
  71. if (ctrl & NAND_CTRL_CHANGE) {
  72. if (gpio_is_valid(gpiomtd->plat.gpio_nce))
  73. gpio_set_value(gpiomtd->plat.gpio_nce,
  74. !(ctrl & NAND_NCE));
  75. gpio_set_value(gpiomtd->plat.gpio_cle, !!(ctrl & NAND_CLE));
  76. gpio_set_value(gpiomtd->plat.gpio_ale, !!(ctrl & NAND_ALE));
  77. gpio_nand_dosync(gpiomtd);
  78. }
  79. if (cmd == NAND_CMD_NONE)
  80. return;
  81. writeb(cmd, gpiomtd->nand_chip.IO_ADDR_W);
  82. gpio_nand_dosync(gpiomtd);
  83. }
  84. static int gpio_nand_devready(struct mtd_info *mtd)
  85. {
  86. struct gpiomtd *gpiomtd = gpio_nand_getpriv(mtd);
  87. return gpio_get_value(gpiomtd->plat.gpio_rdy);
  88. }
  89. #ifdef CONFIG_OF
  90. static const struct of_device_id gpio_nand_id_table[] = {
  91. { .compatible = "gpio-control-nand" },
  92. {}
  93. };
  94. MODULE_DEVICE_TABLE(of, gpio_nand_id_table);
  95. static int gpio_nand_get_config_of(const struct device *dev,
  96. struct gpio_nand_platdata *plat)
  97. {
  98. u32 val;
  99. if (!dev->of_node)
  100. return -ENODEV;
  101. if (!of_property_read_u32(dev->of_node, "bank-width", &val)) {
  102. if (val == 2) {
  103. plat->options |= NAND_BUSWIDTH_16;
  104. } else if (val != 1) {
  105. dev_err(dev, "invalid bank-width %u\n", val);
  106. return -EINVAL;
  107. }
  108. }
  109. plat->gpio_rdy = of_get_gpio(dev->of_node, 0);
  110. plat->gpio_nce = of_get_gpio(dev->of_node, 1);
  111. plat->gpio_ale = of_get_gpio(dev->of_node, 2);
  112. plat->gpio_cle = of_get_gpio(dev->of_node, 3);
  113. plat->gpio_nwp = of_get_gpio(dev->of_node, 4);
  114. if (!of_property_read_u32(dev->of_node, "chip-delay", &val))
  115. plat->chip_delay = val;
  116. return 0;
  117. }
  118. static struct resource *gpio_nand_get_io_sync_of(struct platform_device *pdev)
  119. {
  120. struct resource *r;
  121. u64 addr;
  122. if (of_property_read_u64(pdev->dev.of_node,
  123. "gpio-control-nand,io-sync-reg", &addr))
  124. return NULL;
  125. r = devm_kzalloc(&pdev->dev, sizeof(*r), GFP_KERNEL);
  126. if (!r)
  127. return NULL;
  128. r->start = addr;
  129. r->end = r->start + 0x3;
  130. r->flags = IORESOURCE_MEM;
  131. return r;
  132. }
  133. #else /* CONFIG_OF */
  134. static inline int gpio_nand_get_config_of(const struct device *dev,
  135. struct gpio_nand_platdata *plat)
  136. {
  137. return -ENOSYS;
  138. }
  139. static inline struct resource *
  140. gpio_nand_get_io_sync_of(struct platform_device *pdev)
  141. {
  142. return NULL;
  143. }
  144. #endif /* CONFIG_OF */
  145. static inline int gpio_nand_get_config(const struct device *dev,
  146. struct gpio_nand_platdata *plat)
  147. {
  148. int ret = gpio_nand_get_config_of(dev, plat);
  149. if (!ret)
  150. return ret;
  151. if (dev_get_platdata(dev)) {
  152. memcpy(plat, dev_get_platdata(dev), sizeof(*plat));
  153. return 0;
  154. }
  155. return -EINVAL;
  156. }
  157. static inline struct resource *
  158. gpio_nand_get_io_sync(struct platform_device *pdev)
  159. {
  160. struct resource *r = gpio_nand_get_io_sync_of(pdev);
  161. if (r)
  162. return r;
  163. return platform_get_resource(pdev, IORESOURCE_MEM, 1);
  164. }
  165. static int gpio_nand_remove(struct platform_device *pdev)
  166. {
  167. struct gpiomtd *gpiomtd = platform_get_drvdata(pdev);
  168. nand_release(&gpiomtd->nand_chip);
  169. if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
  170. gpio_set_value(gpiomtd->plat.gpio_nwp, 0);
  171. if (gpio_is_valid(gpiomtd->plat.gpio_nce))
  172. gpio_set_value(gpiomtd->plat.gpio_nce, 1);
  173. return 0;
  174. }
  175. static int gpio_nand_probe(struct platform_device *pdev)
  176. {
  177. struct gpiomtd *gpiomtd;
  178. struct nand_chip *chip;
  179. struct mtd_info *mtd;
  180. struct resource *res;
  181. int ret = 0;
  182. if (!pdev->dev.of_node && !dev_get_platdata(&pdev->dev))
  183. return -EINVAL;
  184. gpiomtd = devm_kzalloc(&pdev->dev, sizeof(*gpiomtd), GFP_KERNEL);
  185. if (!gpiomtd)
  186. return -ENOMEM;
  187. chip = &gpiomtd->nand_chip;
  188. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  189. chip->IO_ADDR_R = devm_ioremap_resource(&pdev->dev, res);
  190. if (IS_ERR(chip->IO_ADDR_R))
  191. return PTR_ERR(chip->IO_ADDR_R);
  192. res = gpio_nand_get_io_sync(pdev);
  193. if (res) {
  194. gpiomtd->io_sync = devm_ioremap_resource(&pdev->dev, res);
  195. if (IS_ERR(gpiomtd->io_sync))
  196. return PTR_ERR(gpiomtd->io_sync);
  197. }
  198. ret = gpio_nand_get_config(&pdev->dev, &gpiomtd->plat);
  199. if (ret)
  200. return ret;
  201. if (gpio_is_valid(gpiomtd->plat.gpio_nce)) {
  202. ret = devm_gpio_request(&pdev->dev, gpiomtd->plat.gpio_nce,
  203. "NAND NCE");
  204. if (ret)
  205. return ret;
  206. gpio_direction_output(gpiomtd->plat.gpio_nce, 1);
  207. }
  208. if (gpio_is_valid(gpiomtd->plat.gpio_nwp)) {
  209. ret = devm_gpio_request(&pdev->dev, gpiomtd->plat.gpio_nwp,
  210. "NAND NWP");
  211. if (ret)
  212. return ret;
  213. }
  214. ret = devm_gpio_request(&pdev->dev, gpiomtd->plat.gpio_ale, "NAND ALE");
  215. if (ret)
  216. return ret;
  217. gpio_direction_output(gpiomtd->plat.gpio_ale, 0);
  218. ret = devm_gpio_request(&pdev->dev, gpiomtd->plat.gpio_cle, "NAND CLE");
  219. if (ret)
  220. return ret;
  221. gpio_direction_output(gpiomtd->plat.gpio_cle, 0);
  222. if (gpio_is_valid(gpiomtd->plat.gpio_rdy)) {
  223. ret = devm_gpio_request(&pdev->dev, gpiomtd->plat.gpio_rdy,
  224. "NAND RDY");
  225. if (ret)
  226. return ret;
  227. gpio_direction_input(gpiomtd->plat.gpio_rdy);
  228. chip->dev_ready = gpio_nand_devready;
  229. }
  230. nand_set_flash_node(chip, pdev->dev.of_node);
  231. chip->IO_ADDR_W = chip->IO_ADDR_R;
  232. chip->ecc.mode = NAND_ECC_SOFT;
  233. chip->ecc.algo = NAND_ECC_HAMMING;
  234. chip->options = gpiomtd->plat.options;
  235. chip->chip_delay = gpiomtd->plat.chip_delay;
  236. chip->cmd_ctrl = gpio_nand_cmd_ctrl;
  237. mtd = nand_to_mtd(chip);
  238. mtd->dev.parent = &pdev->dev;
  239. platform_set_drvdata(pdev, gpiomtd);
  240. if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
  241. gpio_direction_output(gpiomtd->plat.gpio_nwp, 1);
  242. ret = nand_scan(mtd, 1);
  243. if (ret)
  244. goto err_wp;
  245. if (gpiomtd->plat.adjust_parts)
  246. gpiomtd->plat.adjust_parts(&gpiomtd->plat, mtd->size);
  247. ret = mtd_device_register(mtd, gpiomtd->plat.parts,
  248. gpiomtd->plat.num_parts);
  249. if (!ret)
  250. return 0;
  251. err_wp:
  252. if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
  253. gpio_set_value(gpiomtd->plat.gpio_nwp, 0);
  254. return ret;
  255. }
  256. static struct platform_driver gpio_nand_driver = {
  257. .probe = gpio_nand_probe,
  258. .remove = gpio_nand_remove,
  259. .driver = {
  260. .name = "gpio-nand",
  261. .of_match_table = of_match_ptr(gpio_nand_id_table),
  262. },
  263. };
  264. module_platform_driver(gpio_nand_driver);
  265. MODULE_LICENSE("GPL");
  266. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  267. MODULE_DESCRIPTION("GPIO NAND Driver");