gpio.c 9.0 KB

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