gpio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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/init.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 void gpio_nand_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
  79. {
  80. struct nand_chip *this = mtd->priv;
  81. iowrite8_rep(this->IO_ADDR_W, buf, len);
  82. }
  83. static void gpio_nand_readbuf(struct mtd_info *mtd, u_char *buf, int len)
  84. {
  85. struct nand_chip *this = mtd->priv;
  86. ioread8_rep(this->IO_ADDR_R, buf, len);
  87. }
  88. static void gpio_nand_writebuf16(struct mtd_info *mtd, const u_char *buf,
  89. int len)
  90. {
  91. struct nand_chip *this = mtd->priv;
  92. if (IS_ALIGNED((unsigned long)buf, 2)) {
  93. iowrite16_rep(this->IO_ADDR_W, buf, len>>1);
  94. } else {
  95. int i;
  96. unsigned short *ptr = (unsigned short *)buf;
  97. for (i = 0; i < len; i += 2, ptr++)
  98. writew(*ptr, this->IO_ADDR_W);
  99. }
  100. }
  101. static void gpio_nand_readbuf16(struct mtd_info *mtd, u_char *buf, int len)
  102. {
  103. struct nand_chip *this = mtd->priv;
  104. if (IS_ALIGNED((unsigned long)buf, 2)) {
  105. ioread16_rep(this->IO_ADDR_R, buf, len>>1);
  106. } else {
  107. int i;
  108. unsigned short *ptr = (unsigned short *)buf;
  109. for (i = 0; i < len; i += 2, ptr++)
  110. *ptr = readw(this->IO_ADDR_R);
  111. }
  112. }
  113. static int gpio_nand_devready(struct mtd_info *mtd)
  114. {
  115. struct gpiomtd *gpiomtd = gpio_nand_getpriv(mtd);
  116. if (gpio_is_valid(gpiomtd->plat.gpio_rdy))
  117. return gpio_get_value(gpiomtd->plat.gpio_rdy);
  118. return 1;
  119. }
  120. #ifdef CONFIG_OF
  121. static const struct of_device_id gpio_nand_id_table[] = {
  122. { .compatible = "gpio-control-nand" },
  123. {}
  124. };
  125. MODULE_DEVICE_TABLE(of, gpio_nand_id_table);
  126. static int gpio_nand_get_config_of(const struct device *dev,
  127. struct gpio_nand_platdata *plat)
  128. {
  129. u32 val;
  130. if (!of_property_read_u32(dev->of_node, "bank-width", &val)) {
  131. if (val == 2) {
  132. plat->options |= NAND_BUSWIDTH_16;
  133. } else if (val != 1) {
  134. dev_err(dev, "invalid bank-width %u\n", val);
  135. return -EINVAL;
  136. }
  137. }
  138. plat->gpio_rdy = of_get_gpio(dev->of_node, 0);
  139. plat->gpio_nce = of_get_gpio(dev->of_node, 1);
  140. plat->gpio_ale = of_get_gpio(dev->of_node, 2);
  141. plat->gpio_cle = of_get_gpio(dev->of_node, 3);
  142. plat->gpio_nwp = of_get_gpio(dev->of_node, 4);
  143. if (!of_property_read_u32(dev->of_node, "chip-delay", &val))
  144. plat->chip_delay = val;
  145. return 0;
  146. }
  147. static struct resource *gpio_nand_get_io_sync_of(struct platform_device *pdev)
  148. {
  149. struct resource *r = devm_kzalloc(&pdev->dev, sizeof(*r), GFP_KERNEL);
  150. u64 addr;
  151. if (!r || of_property_read_u64(pdev->dev.of_node,
  152. "gpio-control-nand,io-sync-reg", &addr))
  153. return NULL;
  154. r->start = addr;
  155. r->end = r->start + 0x3;
  156. r->flags = IORESOURCE_MEM;
  157. return r;
  158. }
  159. #else /* CONFIG_OF */
  160. #define gpio_nand_id_table NULL
  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. struct resource *res;
  196. nand_release(&gpiomtd->mtd_info);
  197. res = gpio_nand_get_io_sync(dev);
  198. iounmap(gpiomtd->io_sync);
  199. if (res)
  200. release_mem_region(res->start, resource_size(res));
  201. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  202. iounmap(gpiomtd->nand_chip.IO_ADDR_R);
  203. release_mem_region(res->start, resource_size(res));
  204. if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
  205. gpio_set_value(gpiomtd->plat.gpio_nwp, 0);
  206. gpio_set_value(gpiomtd->plat.gpio_nce, 1);
  207. gpio_free(gpiomtd->plat.gpio_cle);
  208. gpio_free(gpiomtd->plat.gpio_ale);
  209. gpio_free(gpiomtd->plat.gpio_nce);
  210. if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
  211. gpio_free(gpiomtd->plat.gpio_nwp);
  212. if (gpio_is_valid(gpiomtd->plat.gpio_rdy))
  213. gpio_free(gpiomtd->plat.gpio_rdy);
  214. kfree(gpiomtd);
  215. return 0;
  216. }
  217. static void __iomem *request_and_remap(struct resource *res, size_t size,
  218. const char *name, int *err)
  219. {
  220. void __iomem *ptr;
  221. if (!request_mem_region(res->start, resource_size(res), name)) {
  222. *err = -EBUSY;
  223. return NULL;
  224. }
  225. ptr = ioremap(res->start, size);
  226. if (!ptr) {
  227. release_mem_region(res->start, resource_size(res));
  228. *err = -ENOMEM;
  229. }
  230. return ptr;
  231. }
  232. static int gpio_nand_probe(struct platform_device *dev)
  233. {
  234. struct gpiomtd *gpiomtd;
  235. struct nand_chip *this;
  236. struct resource *res0, *res1;
  237. struct mtd_part_parser_data ppdata = {};
  238. int ret = 0;
  239. if (!dev->dev.of_node && !dev->dev.platform_data)
  240. return -EINVAL;
  241. res0 = platform_get_resource(dev, IORESOURCE_MEM, 0);
  242. if (!res0)
  243. return -EINVAL;
  244. gpiomtd = kzalloc(sizeof(*gpiomtd), GFP_KERNEL);
  245. if (gpiomtd == NULL) {
  246. dev_err(&dev->dev, "failed to create NAND MTD\n");
  247. return -ENOMEM;
  248. }
  249. this = &gpiomtd->nand_chip;
  250. this->IO_ADDR_R = request_and_remap(res0, 2, "NAND", &ret);
  251. if (!this->IO_ADDR_R) {
  252. dev_err(&dev->dev, "unable to map NAND\n");
  253. goto err_map;
  254. }
  255. res1 = gpio_nand_get_io_sync(dev);
  256. if (res1) {
  257. gpiomtd->io_sync = request_and_remap(res1, 4, "NAND sync", &ret);
  258. if (!gpiomtd->io_sync) {
  259. dev_err(&dev->dev, "unable to map sync NAND\n");
  260. goto err_sync;
  261. }
  262. }
  263. ret = gpio_nand_get_config(&dev->dev, &gpiomtd->plat);
  264. if (ret)
  265. goto err_nce;
  266. ret = gpio_request(gpiomtd->plat.gpio_nce, "NAND NCE");
  267. if (ret)
  268. goto err_nce;
  269. gpio_direction_output(gpiomtd->plat.gpio_nce, 1);
  270. if (gpio_is_valid(gpiomtd->plat.gpio_nwp)) {
  271. ret = gpio_request(gpiomtd->plat.gpio_nwp, "NAND NWP");
  272. if (ret)
  273. goto err_nwp;
  274. gpio_direction_output(gpiomtd->plat.gpio_nwp, 1);
  275. }
  276. ret = gpio_request(gpiomtd->plat.gpio_ale, "NAND ALE");
  277. if (ret)
  278. goto err_ale;
  279. gpio_direction_output(gpiomtd->plat.gpio_ale, 0);
  280. ret = gpio_request(gpiomtd->plat.gpio_cle, "NAND CLE");
  281. if (ret)
  282. goto err_cle;
  283. gpio_direction_output(gpiomtd->plat.gpio_cle, 0);
  284. if (gpio_is_valid(gpiomtd->plat.gpio_rdy)) {
  285. ret = gpio_request(gpiomtd->plat.gpio_rdy, "NAND RDY");
  286. if (ret)
  287. goto err_rdy;
  288. gpio_direction_input(gpiomtd->plat.gpio_rdy);
  289. }
  290. this->IO_ADDR_W = this->IO_ADDR_R;
  291. this->ecc.mode = NAND_ECC_SOFT;
  292. this->options = gpiomtd->plat.options;
  293. this->chip_delay = gpiomtd->plat.chip_delay;
  294. /* install our routines */
  295. this->cmd_ctrl = gpio_nand_cmd_ctrl;
  296. this->dev_ready = gpio_nand_devready;
  297. if (this->options & NAND_BUSWIDTH_16) {
  298. this->read_buf = gpio_nand_readbuf16;
  299. this->write_buf = gpio_nand_writebuf16;
  300. } else {
  301. this->read_buf = gpio_nand_readbuf;
  302. this->write_buf = gpio_nand_writebuf;
  303. }
  304. /* set the mtd private data for the nand driver */
  305. gpiomtd->mtd_info.priv = this;
  306. gpiomtd->mtd_info.owner = THIS_MODULE;
  307. if (nand_scan(&gpiomtd->mtd_info, 1)) {
  308. dev_err(&dev->dev, "no nand chips found?\n");
  309. ret = -ENXIO;
  310. goto err_wp;
  311. }
  312. if (gpiomtd->plat.adjust_parts)
  313. gpiomtd->plat.adjust_parts(&gpiomtd->plat,
  314. gpiomtd->mtd_info.size);
  315. ppdata.of_node = dev->dev.of_node;
  316. ret = mtd_device_parse_register(&gpiomtd->mtd_info, NULL, &ppdata,
  317. gpiomtd->plat.parts,
  318. gpiomtd->plat.num_parts);
  319. if (ret)
  320. goto err_wp;
  321. platform_set_drvdata(dev, gpiomtd);
  322. return 0;
  323. err_wp:
  324. if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
  325. gpio_set_value(gpiomtd->plat.gpio_nwp, 0);
  326. if (gpio_is_valid(gpiomtd->plat.gpio_rdy))
  327. gpio_free(gpiomtd->plat.gpio_rdy);
  328. err_rdy:
  329. gpio_free(gpiomtd->plat.gpio_cle);
  330. err_cle:
  331. gpio_free(gpiomtd->plat.gpio_ale);
  332. err_ale:
  333. if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
  334. gpio_free(gpiomtd->plat.gpio_nwp);
  335. err_nwp:
  336. gpio_free(gpiomtd->plat.gpio_nce);
  337. err_nce:
  338. iounmap(gpiomtd->io_sync);
  339. if (res1)
  340. release_mem_region(res1->start, resource_size(res1));
  341. err_sync:
  342. iounmap(gpiomtd->nand_chip.IO_ADDR_R);
  343. release_mem_region(res0->start, resource_size(res0));
  344. err_map:
  345. kfree(gpiomtd);
  346. return ret;
  347. }
  348. static struct platform_driver gpio_nand_driver = {
  349. .probe = gpio_nand_probe,
  350. .remove = gpio_nand_remove,
  351. .driver = {
  352. .name = "gpio-nand",
  353. .of_match_table = gpio_nand_id_table,
  354. },
  355. };
  356. module_platform_driver(gpio_nand_driver);
  357. MODULE_LICENSE("GPL");
  358. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  359. MODULE_DESCRIPTION("GPIO NAND Driver");