gpio.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Updated, and converted to generic GPIO based driver by Russell King.
  4. *
  5. * Written by Ben Dooks <[email protected]>
  6. * Based on 2.4 version by Mark Whittaker
  7. *
  8. * © 2004 Simtec Electronics
  9. *
  10. * Device driver for NAND flash that uses a memory mapped interface to
  11. * read/write the NAND commands and data, and GPIO pins for control signals
  12. * (the DT binding refers to this as "GPIO assisted NAND flash")
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/err.h>
  16. #include <linux/slab.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/gpio/consumer.h>
  20. #include <linux/io.h>
  21. #include <linux/mtd/mtd.h>
  22. #include <linux/mtd/rawnand.h>
  23. #include <linux/mtd/partitions.h>
  24. #include <linux/mtd/nand-gpio.h>
  25. #include <linux/of.h>
  26. #include <linux/of_address.h>
  27. #include <linux/delay.h>
  28. struct gpiomtd {
  29. struct nand_controller base;
  30. void __iomem *io;
  31. void __iomem *io_sync;
  32. struct nand_chip nand_chip;
  33. struct gpio_nand_platdata plat;
  34. struct gpio_desc *nce; /* Optional chip enable */
  35. struct gpio_desc *cle;
  36. struct gpio_desc *ale;
  37. struct gpio_desc *rdy;
  38. struct gpio_desc *nwp; /* Optional write protection */
  39. };
  40. static inline struct gpiomtd *gpio_nand_getpriv(struct mtd_info *mtd)
  41. {
  42. return container_of(mtd_to_nand(mtd), struct gpiomtd, nand_chip);
  43. }
  44. #ifdef CONFIG_ARM
  45. /* gpio_nand_dosync()
  46. *
  47. * Make sure the GPIO state changes occur in-order with writes to NAND
  48. * memory region.
  49. * Needed on PXA due to bus-reordering within the SoC itself (see section on
  50. * I/O ordering in PXA manual (section 2.3, p35)
  51. */
  52. static void gpio_nand_dosync(struct gpiomtd *gpiomtd)
  53. {
  54. unsigned long tmp;
  55. if (gpiomtd->io_sync) {
  56. /*
  57. * Linux memory barriers don't cater for what's required here.
  58. * What's required is what's here - a read from a separate
  59. * region with a dependency on that read.
  60. */
  61. tmp = readl(gpiomtd->io_sync);
  62. asm volatile("mov %1, %0\n" : "=r" (tmp) : "r" (tmp));
  63. }
  64. }
  65. #else
  66. static inline void gpio_nand_dosync(struct gpiomtd *gpiomtd) {}
  67. #endif
  68. static int gpio_nand_exec_instr(struct nand_chip *chip,
  69. const struct nand_op_instr *instr)
  70. {
  71. struct gpiomtd *gpiomtd = gpio_nand_getpriv(nand_to_mtd(chip));
  72. unsigned int i;
  73. switch (instr->type) {
  74. case NAND_OP_CMD_INSTR:
  75. gpio_nand_dosync(gpiomtd);
  76. gpiod_set_value(gpiomtd->cle, 1);
  77. gpio_nand_dosync(gpiomtd);
  78. writeb(instr->ctx.cmd.opcode, gpiomtd->io);
  79. gpio_nand_dosync(gpiomtd);
  80. gpiod_set_value(gpiomtd->cle, 0);
  81. return 0;
  82. case NAND_OP_ADDR_INSTR:
  83. gpio_nand_dosync(gpiomtd);
  84. gpiod_set_value(gpiomtd->ale, 1);
  85. gpio_nand_dosync(gpiomtd);
  86. for (i = 0; i < instr->ctx.addr.naddrs; i++)
  87. writeb(instr->ctx.addr.addrs[i], gpiomtd->io);
  88. gpio_nand_dosync(gpiomtd);
  89. gpiod_set_value(gpiomtd->ale, 0);
  90. return 0;
  91. case NAND_OP_DATA_IN_INSTR:
  92. gpio_nand_dosync(gpiomtd);
  93. if ((chip->options & NAND_BUSWIDTH_16) &&
  94. !instr->ctx.data.force_8bit)
  95. ioread16_rep(gpiomtd->io, instr->ctx.data.buf.in,
  96. instr->ctx.data.len / 2);
  97. else
  98. ioread8_rep(gpiomtd->io, instr->ctx.data.buf.in,
  99. instr->ctx.data.len);
  100. return 0;
  101. case NAND_OP_DATA_OUT_INSTR:
  102. gpio_nand_dosync(gpiomtd);
  103. if ((chip->options & NAND_BUSWIDTH_16) &&
  104. !instr->ctx.data.force_8bit)
  105. iowrite16_rep(gpiomtd->io, instr->ctx.data.buf.out,
  106. instr->ctx.data.len / 2);
  107. else
  108. iowrite8_rep(gpiomtd->io, instr->ctx.data.buf.out,
  109. instr->ctx.data.len);
  110. return 0;
  111. case NAND_OP_WAITRDY_INSTR:
  112. if (!gpiomtd->rdy)
  113. return nand_soft_waitrdy(chip, instr->ctx.waitrdy.timeout_ms);
  114. return nand_gpio_waitrdy(chip, gpiomtd->rdy,
  115. instr->ctx.waitrdy.timeout_ms);
  116. default:
  117. return -EINVAL;
  118. }
  119. return 0;
  120. }
  121. static int gpio_nand_exec_op(struct nand_chip *chip,
  122. const struct nand_operation *op,
  123. bool check_only)
  124. {
  125. struct gpiomtd *gpiomtd = gpio_nand_getpriv(nand_to_mtd(chip));
  126. unsigned int i;
  127. int ret = 0;
  128. if (check_only)
  129. return 0;
  130. gpio_nand_dosync(gpiomtd);
  131. gpiod_set_value(gpiomtd->nce, 0);
  132. for (i = 0; i < op->ninstrs; i++) {
  133. ret = gpio_nand_exec_instr(chip, &op->instrs[i]);
  134. if (ret)
  135. break;
  136. if (op->instrs[i].delay_ns)
  137. ndelay(op->instrs[i].delay_ns);
  138. }
  139. gpio_nand_dosync(gpiomtd);
  140. gpiod_set_value(gpiomtd->nce, 1);
  141. return ret;
  142. }
  143. static int gpio_nand_attach_chip(struct nand_chip *chip)
  144. {
  145. if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT &&
  146. chip->ecc.algo == NAND_ECC_ALGO_UNKNOWN)
  147. chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
  148. return 0;
  149. }
  150. static const struct nand_controller_ops gpio_nand_ops = {
  151. .exec_op = gpio_nand_exec_op,
  152. .attach_chip = gpio_nand_attach_chip,
  153. };
  154. #ifdef CONFIG_OF
  155. static const struct of_device_id gpio_nand_id_table[] = {
  156. { .compatible = "gpio-control-nand" },
  157. {}
  158. };
  159. MODULE_DEVICE_TABLE(of, gpio_nand_id_table);
  160. static int gpio_nand_get_config_of(const struct device *dev,
  161. struct gpio_nand_platdata *plat)
  162. {
  163. u32 val;
  164. if (!dev->of_node)
  165. return -ENODEV;
  166. if (!of_property_read_u32(dev->of_node, "bank-width", &val)) {
  167. if (val == 2) {
  168. plat->options |= NAND_BUSWIDTH_16;
  169. } else if (val != 1) {
  170. dev_err(dev, "invalid bank-width %u\n", val);
  171. return -EINVAL;
  172. }
  173. }
  174. if (!of_property_read_u32(dev->of_node, "chip-delay", &val))
  175. plat->chip_delay = val;
  176. return 0;
  177. }
  178. static struct resource *gpio_nand_get_io_sync_of(struct platform_device *pdev)
  179. {
  180. struct resource *r;
  181. u64 addr;
  182. if (of_property_read_u64(pdev->dev.of_node,
  183. "gpio-control-nand,io-sync-reg", &addr))
  184. return NULL;
  185. r = devm_kzalloc(&pdev->dev, sizeof(*r), GFP_KERNEL);
  186. if (!r)
  187. return NULL;
  188. r->start = addr;
  189. r->end = r->start + 0x3;
  190. r->flags = IORESOURCE_MEM;
  191. return r;
  192. }
  193. #else /* CONFIG_OF */
  194. static inline int gpio_nand_get_config_of(const struct device *dev,
  195. struct gpio_nand_platdata *plat)
  196. {
  197. return -ENOSYS;
  198. }
  199. static inline struct resource *
  200. gpio_nand_get_io_sync_of(struct platform_device *pdev)
  201. {
  202. return NULL;
  203. }
  204. #endif /* CONFIG_OF */
  205. static inline int gpio_nand_get_config(const struct device *dev,
  206. struct gpio_nand_platdata *plat)
  207. {
  208. int ret = gpio_nand_get_config_of(dev, plat);
  209. if (!ret)
  210. return ret;
  211. if (dev_get_platdata(dev)) {
  212. memcpy(plat, dev_get_platdata(dev), sizeof(*plat));
  213. return 0;
  214. }
  215. return -EINVAL;
  216. }
  217. static inline struct resource *
  218. gpio_nand_get_io_sync(struct platform_device *pdev)
  219. {
  220. struct resource *r = gpio_nand_get_io_sync_of(pdev);
  221. if (r)
  222. return r;
  223. return platform_get_resource(pdev, IORESOURCE_MEM, 1);
  224. }
  225. static int gpio_nand_remove(struct platform_device *pdev)
  226. {
  227. struct gpiomtd *gpiomtd = platform_get_drvdata(pdev);
  228. struct nand_chip *chip = &gpiomtd->nand_chip;
  229. int ret;
  230. ret = mtd_device_unregister(nand_to_mtd(chip));
  231. WARN_ON(ret);
  232. nand_cleanup(chip);
  233. /* Enable write protection and disable the chip */
  234. if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
  235. gpiod_set_value(gpiomtd->nwp, 0);
  236. if (gpiomtd->nce && !IS_ERR(gpiomtd->nce))
  237. gpiod_set_value(gpiomtd->nce, 0);
  238. return 0;
  239. }
  240. static int gpio_nand_probe(struct platform_device *pdev)
  241. {
  242. struct gpiomtd *gpiomtd;
  243. struct nand_chip *chip;
  244. struct mtd_info *mtd;
  245. struct resource *res;
  246. struct device *dev = &pdev->dev;
  247. int ret = 0;
  248. if (!dev->of_node && !dev_get_platdata(dev))
  249. return -EINVAL;
  250. gpiomtd = devm_kzalloc(dev, sizeof(*gpiomtd), GFP_KERNEL);
  251. if (!gpiomtd)
  252. return -ENOMEM;
  253. chip = &gpiomtd->nand_chip;
  254. gpiomtd->io = devm_platform_ioremap_resource(pdev, 0);
  255. if (IS_ERR(gpiomtd->io))
  256. return PTR_ERR(gpiomtd->io);
  257. res = gpio_nand_get_io_sync(pdev);
  258. if (res) {
  259. gpiomtd->io_sync = devm_ioremap_resource(dev, res);
  260. if (IS_ERR(gpiomtd->io_sync))
  261. return PTR_ERR(gpiomtd->io_sync);
  262. }
  263. ret = gpio_nand_get_config(dev, &gpiomtd->plat);
  264. if (ret)
  265. return ret;
  266. /* Just enable the chip */
  267. gpiomtd->nce = devm_gpiod_get_optional(dev, "nce", GPIOD_OUT_HIGH);
  268. if (IS_ERR(gpiomtd->nce))
  269. return PTR_ERR(gpiomtd->nce);
  270. /* We disable write protection once we know probe() will succeed */
  271. gpiomtd->nwp = devm_gpiod_get_optional(dev, "nwp", GPIOD_OUT_LOW);
  272. if (IS_ERR(gpiomtd->nwp)) {
  273. ret = PTR_ERR(gpiomtd->nwp);
  274. goto out_ce;
  275. }
  276. gpiomtd->ale = devm_gpiod_get(dev, "ale", GPIOD_OUT_LOW);
  277. if (IS_ERR(gpiomtd->ale)) {
  278. ret = PTR_ERR(gpiomtd->ale);
  279. goto out_ce;
  280. }
  281. gpiomtd->cle = devm_gpiod_get(dev, "cle", GPIOD_OUT_LOW);
  282. if (IS_ERR(gpiomtd->cle)) {
  283. ret = PTR_ERR(gpiomtd->cle);
  284. goto out_ce;
  285. }
  286. gpiomtd->rdy = devm_gpiod_get_optional(dev, "rdy", GPIOD_IN);
  287. if (IS_ERR(gpiomtd->rdy)) {
  288. ret = PTR_ERR(gpiomtd->rdy);
  289. goto out_ce;
  290. }
  291. nand_controller_init(&gpiomtd->base);
  292. gpiomtd->base.ops = &gpio_nand_ops;
  293. nand_set_flash_node(chip, pdev->dev.of_node);
  294. chip->options = gpiomtd->plat.options;
  295. chip->controller = &gpiomtd->base;
  296. mtd = nand_to_mtd(chip);
  297. mtd->dev.parent = dev;
  298. platform_set_drvdata(pdev, gpiomtd);
  299. /* Disable write protection, if wired up */
  300. if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
  301. gpiod_direction_output(gpiomtd->nwp, 1);
  302. /*
  303. * This driver assumes that the default ECC engine should be TYPE_SOFT.
  304. * Set ->engine_type before registering the NAND devices in order to
  305. * provide a driver specific default value.
  306. */
  307. chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
  308. ret = nand_scan(chip, 1);
  309. if (ret)
  310. goto err_wp;
  311. if (gpiomtd->plat.adjust_parts)
  312. gpiomtd->plat.adjust_parts(&gpiomtd->plat, mtd->size);
  313. ret = mtd_device_register(mtd, gpiomtd->plat.parts,
  314. gpiomtd->plat.num_parts);
  315. if (!ret)
  316. return 0;
  317. err_wp:
  318. if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
  319. gpiod_set_value(gpiomtd->nwp, 0);
  320. out_ce:
  321. if (gpiomtd->nce && !IS_ERR(gpiomtd->nce))
  322. gpiod_set_value(gpiomtd->nce, 0);
  323. return ret;
  324. }
  325. static struct platform_driver gpio_nand_driver = {
  326. .probe = gpio_nand_probe,
  327. .remove = gpio_nand_remove,
  328. .driver = {
  329. .name = "gpio-nand",
  330. .of_match_table = of_match_ptr(gpio_nand_id_table),
  331. },
  332. };
  333. module_platform_driver(gpio_nand_driver);
  334. MODULE_LICENSE("GPL");
  335. MODULE_AUTHOR("Ben Dooks <[email protected]>");
  336. MODULE_DESCRIPTION("GPIO NAND Driver");