gpio.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 nand_chip *chip, int cmd,
  70. unsigned int ctrl)
  71. {
  72. struct gpiomtd *gpiomtd = gpio_nand_getpriv(nand_to_mtd(chip));
  73. gpio_nand_dosync(gpiomtd);
  74. if (ctrl & NAND_CTRL_CHANGE) {
  75. if (gpiomtd->nce)
  76. gpiod_set_value(gpiomtd->nce, !(ctrl & NAND_NCE));
  77. gpiod_set_value(gpiomtd->cle, !!(ctrl & NAND_CLE));
  78. gpiod_set_value(gpiomtd->ale, !!(ctrl & NAND_ALE));
  79. gpio_nand_dosync(gpiomtd);
  80. }
  81. if (cmd == NAND_CMD_NONE)
  82. return;
  83. writeb(cmd, gpiomtd->nand_chip.legacy.IO_ADDR_W);
  84. gpio_nand_dosync(gpiomtd);
  85. }
  86. static int gpio_nand_devready(struct nand_chip *chip)
  87. {
  88. struct gpiomtd *gpiomtd = gpio_nand_getpriv(nand_to_mtd(chip));
  89. return gpiod_get_value(gpiomtd->rdy);
  90. }
  91. #ifdef CONFIG_OF
  92. static const struct of_device_id gpio_nand_id_table[] = {
  93. { .compatible = "gpio-control-nand" },
  94. {}
  95. };
  96. MODULE_DEVICE_TABLE(of, gpio_nand_id_table);
  97. static int gpio_nand_get_config_of(const struct device *dev,
  98. struct gpio_nand_platdata *plat)
  99. {
  100. u32 val;
  101. if (!dev->of_node)
  102. return -ENODEV;
  103. if (!of_property_read_u32(dev->of_node, "bank-width", &val)) {
  104. if (val == 2) {
  105. plat->options |= NAND_BUSWIDTH_16;
  106. } else if (val != 1) {
  107. dev_err(dev, "invalid bank-width %u\n", val);
  108. return -EINVAL;
  109. }
  110. }
  111. if (!of_property_read_u32(dev->of_node, "chip-delay", &val))
  112. plat->chip_delay = val;
  113. return 0;
  114. }
  115. static struct resource *gpio_nand_get_io_sync_of(struct platform_device *pdev)
  116. {
  117. struct resource *r;
  118. u64 addr;
  119. if (of_property_read_u64(pdev->dev.of_node,
  120. "gpio-control-nand,io-sync-reg", &addr))
  121. return NULL;
  122. r = devm_kzalloc(&pdev->dev, sizeof(*r), GFP_KERNEL);
  123. if (!r)
  124. return NULL;
  125. r->start = addr;
  126. r->end = r->start + 0x3;
  127. r->flags = IORESOURCE_MEM;
  128. return r;
  129. }
  130. #else /* CONFIG_OF */
  131. static inline int gpio_nand_get_config_of(const struct device *dev,
  132. struct gpio_nand_platdata *plat)
  133. {
  134. return -ENOSYS;
  135. }
  136. static inline struct resource *
  137. gpio_nand_get_io_sync_of(struct platform_device *pdev)
  138. {
  139. return NULL;
  140. }
  141. #endif /* CONFIG_OF */
  142. static inline int gpio_nand_get_config(const struct device *dev,
  143. struct gpio_nand_platdata *plat)
  144. {
  145. int ret = gpio_nand_get_config_of(dev, plat);
  146. if (!ret)
  147. return ret;
  148. if (dev_get_platdata(dev)) {
  149. memcpy(plat, dev_get_platdata(dev), sizeof(*plat));
  150. return 0;
  151. }
  152. return -EINVAL;
  153. }
  154. static inline struct resource *
  155. gpio_nand_get_io_sync(struct platform_device *pdev)
  156. {
  157. struct resource *r = gpio_nand_get_io_sync_of(pdev);
  158. if (r)
  159. return r;
  160. return platform_get_resource(pdev, IORESOURCE_MEM, 1);
  161. }
  162. static int gpio_nand_remove(struct platform_device *pdev)
  163. {
  164. struct gpiomtd *gpiomtd = platform_get_drvdata(pdev);
  165. nand_release(&gpiomtd->nand_chip);
  166. /* Enable write protection and disable the chip */
  167. if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
  168. gpiod_set_value(gpiomtd->nwp, 0);
  169. if (gpiomtd->nce && !IS_ERR(gpiomtd->nce))
  170. gpiod_set_value(gpiomtd->nce, 0);
  171. return 0;
  172. }
  173. static int gpio_nand_probe(struct platform_device *pdev)
  174. {
  175. struct gpiomtd *gpiomtd;
  176. struct nand_chip *chip;
  177. struct mtd_info *mtd;
  178. struct resource *res;
  179. struct device *dev = &pdev->dev;
  180. int ret = 0;
  181. if (!dev->of_node && !dev_get_platdata(dev))
  182. return -EINVAL;
  183. gpiomtd = devm_kzalloc(dev, sizeof(*gpiomtd), GFP_KERNEL);
  184. if (!gpiomtd)
  185. return -ENOMEM;
  186. chip = &gpiomtd->nand_chip;
  187. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  188. chip->legacy.IO_ADDR_R = devm_ioremap_resource(dev, res);
  189. if (IS_ERR(chip->legacy.IO_ADDR_R))
  190. return PTR_ERR(chip->legacy.IO_ADDR_R);
  191. res = gpio_nand_get_io_sync(pdev);
  192. if (res) {
  193. gpiomtd->io_sync = devm_ioremap_resource(dev, res);
  194. if (IS_ERR(gpiomtd->io_sync))
  195. return PTR_ERR(gpiomtd->io_sync);
  196. }
  197. ret = gpio_nand_get_config(dev, &gpiomtd->plat);
  198. if (ret)
  199. return ret;
  200. /* Just enable the chip */
  201. gpiomtd->nce = devm_gpiod_get_optional(dev, "nce", GPIOD_OUT_HIGH);
  202. if (IS_ERR(gpiomtd->nce))
  203. return PTR_ERR(gpiomtd->nce);
  204. /* We disable write protection once we know probe() will succeed */
  205. gpiomtd->nwp = devm_gpiod_get_optional(dev, "nwp", GPIOD_OUT_LOW);
  206. if (IS_ERR(gpiomtd->nwp)) {
  207. ret = PTR_ERR(gpiomtd->nwp);
  208. goto out_ce;
  209. }
  210. gpiomtd->ale = devm_gpiod_get(dev, "ale", GPIOD_OUT_LOW);
  211. if (IS_ERR(gpiomtd->ale)) {
  212. ret = PTR_ERR(gpiomtd->ale);
  213. goto out_ce;
  214. }
  215. gpiomtd->cle = devm_gpiod_get(dev, "cle", GPIOD_OUT_LOW);
  216. if (IS_ERR(gpiomtd->cle)) {
  217. ret = PTR_ERR(gpiomtd->cle);
  218. goto out_ce;
  219. }
  220. gpiomtd->rdy = devm_gpiod_get_optional(dev, "rdy", GPIOD_IN);
  221. if (IS_ERR(gpiomtd->rdy)) {
  222. ret = PTR_ERR(gpiomtd->rdy);
  223. goto out_ce;
  224. }
  225. /* Using RDY pin */
  226. if (gpiomtd->rdy)
  227. chip->legacy.dev_ready = gpio_nand_devready;
  228. nand_set_flash_node(chip, pdev->dev.of_node);
  229. chip->legacy.IO_ADDR_W = chip->legacy.IO_ADDR_R;
  230. chip->ecc.mode = NAND_ECC_SOFT;
  231. chip->ecc.algo = NAND_ECC_HAMMING;
  232. chip->options = gpiomtd->plat.options;
  233. chip->chip_delay = gpiomtd->plat.chip_delay;
  234. chip->legacy.cmd_ctrl = gpio_nand_cmd_ctrl;
  235. mtd = nand_to_mtd(chip);
  236. mtd->dev.parent = dev;
  237. platform_set_drvdata(pdev, gpiomtd);
  238. /* Disable write protection, if wired up */
  239. if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
  240. gpiod_direction_output(gpiomtd->nwp, 1);
  241. ret = nand_scan(chip, 1);
  242. if (ret)
  243. goto err_wp;
  244. if (gpiomtd->plat.adjust_parts)
  245. gpiomtd->plat.adjust_parts(&gpiomtd->plat, mtd->size);
  246. ret = mtd_device_register(mtd, gpiomtd->plat.parts,
  247. gpiomtd->plat.num_parts);
  248. if (!ret)
  249. return 0;
  250. err_wp:
  251. if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
  252. gpiod_set_value(gpiomtd->nwp, 0);
  253. out_ce:
  254. if (gpiomtd->nce && !IS_ERR(gpiomtd->nce))
  255. gpiod_set_value(gpiomtd->nce, 0);
  256. return ret;
  257. }
  258. static struct platform_driver gpio_nand_driver = {
  259. .probe = gpio_nand_probe,
  260. .remove = gpio_nand_remove,
  261. .driver = {
  262. .name = "gpio-nand",
  263. .of_match_table = of_match_ptr(gpio_nand_id_table),
  264. },
  265. };
  266. module_platform_driver(gpio_nand_driver);
  267. MODULE_LICENSE("GPL");
  268. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  269. MODULE_DESCRIPTION("GPIO NAND Driver");