gpio.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Updated, and converted to generic GPIO based driver by Russell King.
  3. *
  4. * Written by Ben Dooks <ben@simtec.co.uk>
  5. * Based on 2.4 version by Mark Whittaker
  6. *
  7. * © 2004 Simtec Electronics
  8. *
  9. * Device driver for NAND flash that uses a memory mapped interface to
  10. * read/write the NAND commands and data, and GPIO pins for control signals
  11. * (the DT binding refers to this as "GPIO assisted NAND flash")
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/err.h>
  20. #include <linux/slab.h>
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/gpio/consumer.h>
  24. #include <linux/io.h>
  25. #include <linux/mtd/mtd.h>
  26. #include <linux/mtd/rawnand.h>
  27. #include <linux/mtd/partitions.h>
  28. #include <linux/mtd/nand-gpio.h>
  29. #include <linux/of.h>
  30. #include <linux/of_address.h>
  31. struct gpiomtd {
  32. void __iomem *io_sync;
  33. struct nand_chip nand_chip;
  34. struct gpio_nand_platdata plat;
  35. struct gpio_desc *nce; /* Optional chip enable */
  36. struct gpio_desc *cle;
  37. struct gpio_desc *ale;
  38. struct gpio_desc *rdy;
  39. struct gpio_desc *nwp; /* Optional write protection */
  40. };
  41. static inline struct gpiomtd *gpio_nand_getpriv(struct mtd_info *mtd)
  42. {
  43. return container_of(mtd_to_nand(mtd), struct gpiomtd, nand_chip);
  44. }
  45. #ifdef CONFIG_ARM
  46. /* gpio_nand_dosync()
  47. *
  48. * Make sure the GPIO state changes occur in-order with writes to NAND
  49. * memory region.
  50. * Needed on PXA due to bus-reordering within the SoC itself (see section on
  51. * I/O ordering in PXA manual (section 2.3, p35)
  52. */
  53. static void gpio_nand_dosync(struct gpiomtd *gpiomtd)
  54. {
  55. unsigned long tmp;
  56. if (gpiomtd->io_sync) {
  57. /*
  58. * Linux memory barriers don't cater for what's required here.
  59. * What's required is what's here - a read from a separate
  60. * region with a dependency on that read.
  61. */
  62. tmp = readl(gpiomtd->io_sync);
  63. asm volatile("mov %1, %0\n" : "=r" (tmp) : "r" (tmp));
  64. }
  65. }
  66. #else
  67. static inline void gpio_nand_dosync(struct gpiomtd *gpiomtd) {}
  68. #endif
  69. static void gpio_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  70. {
  71. struct gpiomtd *gpiomtd = gpio_nand_getpriv(mtd);
  72. gpio_nand_dosync(gpiomtd);
  73. if (ctrl & NAND_CTRL_CHANGE) {
  74. if (gpiomtd->nce)
  75. gpiod_set_value(gpiomtd->nce, !(ctrl & NAND_NCE));
  76. gpiod_set_value(gpiomtd->cle, !!(ctrl & NAND_CLE));
  77. gpiod_set_value(gpiomtd->ale, !!(ctrl & NAND_ALE));
  78. gpio_nand_dosync(gpiomtd);
  79. }
  80. if (cmd == NAND_CMD_NONE)
  81. return;
  82. writeb(cmd, gpiomtd->nand_chip.IO_ADDR_W);
  83. gpio_nand_dosync(gpiomtd);
  84. }
  85. static int gpio_nand_devready(struct mtd_info *mtd)
  86. {
  87. struct gpiomtd *gpiomtd = gpio_nand_getpriv(mtd);
  88. return gpiod_get_value(gpiomtd->rdy);
  89. }
  90. #ifdef CONFIG_OF
  91. static const struct of_device_id gpio_nand_id_table[] = {
  92. { .compatible = "gpio-control-nand" },
  93. {}
  94. };
  95. MODULE_DEVICE_TABLE(of, gpio_nand_id_table);
  96. static int gpio_nand_get_config_of(const struct device *dev,
  97. struct gpio_nand_platdata *plat)
  98. {
  99. u32 val;
  100. if (!dev->of_node)
  101. return -ENODEV;
  102. if (!of_property_read_u32(dev->of_node, "bank-width", &val)) {
  103. if (val == 2) {
  104. plat->options |= NAND_BUSWIDTH_16;
  105. } else if (val != 1) {
  106. dev_err(dev, "invalid bank-width %u\n", val);
  107. return -EINVAL;
  108. }
  109. }
  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(nand_to_mtd(&gpiomtd->nand_chip));
  165. /* Enable write protection and disable the chip */
  166. if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
  167. gpiod_set_value(gpiomtd->nwp, 0);
  168. if (gpiomtd->nce && !IS_ERR(gpiomtd->nce))
  169. gpiod_set_value(gpiomtd->nce, 0);
  170. return 0;
  171. }
  172. static int gpio_nand_probe(struct platform_device *pdev)
  173. {
  174. struct gpiomtd *gpiomtd;
  175. struct nand_chip *chip;
  176. struct mtd_info *mtd;
  177. struct resource *res;
  178. struct device *dev = &pdev->dev;
  179. int ret = 0;
  180. if (!dev->of_node && !dev_get_platdata(dev))
  181. return -EINVAL;
  182. gpiomtd = devm_kzalloc(dev, sizeof(*gpiomtd), GFP_KERNEL);
  183. if (!gpiomtd)
  184. return -ENOMEM;
  185. chip = &gpiomtd->nand_chip;
  186. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  187. chip->IO_ADDR_R = devm_ioremap_resource(dev, res);
  188. if (IS_ERR(chip->IO_ADDR_R))
  189. return PTR_ERR(chip->IO_ADDR_R);
  190. res = gpio_nand_get_io_sync(pdev);
  191. if (res) {
  192. gpiomtd->io_sync = devm_ioremap_resource(dev, res);
  193. if (IS_ERR(gpiomtd->io_sync))
  194. return PTR_ERR(gpiomtd->io_sync);
  195. }
  196. ret = gpio_nand_get_config(dev, &gpiomtd->plat);
  197. if (ret)
  198. return ret;
  199. /* Just enable the chip */
  200. gpiomtd->nce = devm_gpiod_get_optional(dev, "nce", GPIOD_OUT_HIGH);
  201. if (IS_ERR(gpiomtd->nce))
  202. return PTR_ERR(gpiomtd->nce);
  203. /* We disable write protection once we know probe() will succeed */
  204. gpiomtd->nwp = devm_gpiod_get_optional(dev, "nwp", GPIOD_OUT_LOW);
  205. if (IS_ERR(gpiomtd->nwp)) {
  206. ret = PTR_ERR(gpiomtd->nwp);
  207. goto out_ce;
  208. }
  209. gpiomtd->ale = devm_gpiod_get(dev, "ale", GPIOD_OUT_LOW);
  210. if (IS_ERR(gpiomtd->ale)) {
  211. ret = PTR_ERR(gpiomtd->ale);
  212. goto out_ce;
  213. }
  214. gpiomtd->cle = devm_gpiod_get(dev, "cle", GPIOD_OUT_LOW);
  215. if (IS_ERR(gpiomtd->cle)) {
  216. ret = PTR_ERR(gpiomtd->cle);
  217. goto out_ce;
  218. }
  219. gpiomtd->rdy = devm_gpiod_get_optional(dev, "rdy", GPIOD_IN);
  220. if (IS_ERR(gpiomtd->rdy)) {
  221. ret = PTR_ERR(gpiomtd->rdy);
  222. goto out_ce;
  223. }
  224. /* Using RDY pin */
  225. if (gpiomtd->rdy)
  226. chip->dev_ready = gpio_nand_devready;
  227. nand_set_flash_node(chip, pdev->dev.of_node);
  228. chip->IO_ADDR_W = chip->IO_ADDR_R;
  229. chip->ecc.mode = NAND_ECC_SOFT;
  230. chip->ecc.algo = NAND_ECC_HAMMING;
  231. chip->options = gpiomtd->plat.options;
  232. chip->chip_delay = gpiomtd->plat.chip_delay;
  233. chip->cmd_ctrl = gpio_nand_cmd_ctrl;
  234. mtd = nand_to_mtd(chip);
  235. mtd->dev.parent = dev;
  236. platform_set_drvdata(pdev, gpiomtd);
  237. /* Disable write protection, if wired up */
  238. if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
  239. gpiod_direction_output(gpiomtd->nwp, 1);
  240. ret = nand_scan(mtd, 1);
  241. if (ret)
  242. goto err_wp;
  243. if (gpiomtd->plat.adjust_parts)
  244. gpiomtd->plat.adjust_parts(&gpiomtd->plat, mtd->size);
  245. ret = mtd_device_register(mtd, gpiomtd->plat.parts,
  246. gpiomtd->plat.num_parts);
  247. if (!ret)
  248. return 0;
  249. err_wp:
  250. if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
  251. gpiod_set_value(gpiomtd->nwp, 0);
  252. out_ce:
  253. if (gpiomtd->nce && !IS_ERR(gpiomtd->nce))
  254. gpiod_set_value(gpiomtd->nce, 0);
  255. return ret;
  256. }
  257. static struct platform_driver gpio_nand_driver = {
  258. .probe = gpio_nand_probe,
  259. .remove = gpio_nand_remove,
  260. .driver = {
  261. .name = "gpio-nand",
  262. .of_match_table = of_match_ptr(gpio_nand_id_table),
  263. },
  264. };
  265. module_platform_driver(gpio_nand_driver);
  266. MODULE_LICENSE("GPL");
  267. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  268. MODULE_DESCRIPTION("GPIO NAND Driver");