gpio.c 7.6 KB

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