gpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. writesb(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. readsb(this->IO_ADDR_R, buf, len);
  87. }
  88. static int gpio_nand_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
  89. {
  90. struct nand_chip *this = mtd->priv;
  91. unsigned char read, *p = (unsigned char *) buf;
  92. int i, err = 0;
  93. for (i = 0; i < len; i++) {
  94. read = readb(this->IO_ADDR_R);
  95. if (read != p[i]) {
  96. pr_debug("%s: err at %d (read %04x vs %04x)\n",
  97. __func__, i, read, p[i]);
  98. err = -EFAULT;
  99. }
  100. }
  101. return err;
  102. }
  103. static void gpio_nand_writebuf16(struct mtd_info *mtd, const u_char *buf,
  104. int len)
  105. {
  106. struct nand_chip *this = mtd->priv;
  107. if (IS_ALIGNED((unsigned long)buf, 2)) {
  108. writesw(this->IO_ADDR_W, buf, len>>1);
  109. } else {
  110. int i;
  111. unsigned short *ptr = (unsigned short *)buf;
  112. for (i = 0; i < len; i += 2, ptr++)
  113. writew(*ptr, this->IO_ADDR_W);
  114. }
  115. }
  116. static void gpio_nand_readbuf16(struct mtd_info *mtd, u_char *buf, int len)
  117. {
  118. struct nand_chip *this = mtd->priv;
  119. if (IS_ALIGNED((unsigned long)buf, 2)) {
  120. readsw(this->IO_ADDR_R, buf, len>>1);
  121. } else {
  122. int i;
  123. unsigned short *ptr = (unsigned short *)buf;
  124. for (i = 0; i < len; i += 2, ptr++)
  125. *ptr = readw(this->IO_ADDR_R);
  126. }
  127. }
  128. static int gpio_nand_verifybuf16(struct mtd_info *mtd, const u_char *buf,
  129. int len)
  130. {
  131. struct nand_chip *this = mtd->priv;
  132. unsigned short read, *p = (unsigned short *) buf;
  133. int i, err = 0;
  134. len >>= 1;
  135. for (i = 0; i < len; i++) {
  136. read = readw(this->IO_ADDR_R);
  137. if (read != p[i]) {
  138. pr_debug("%s: err at %d (read %04x vs %04x)\n",
  139. __func__, i, read, p[i]);
  140. err = -EFAULT;
  141. }
  142. }
  143. return err;
  144. }
  145. static int gpio_nand_devready(struct mtd_info *mtd)
  146. {
  147. struct gpiomtd *gpiomtd = gpio_nand_getpriv(mtd);
  148. return gpio_get_value(gpiomtd->plat.gpio_rdy);
  149. }
  150. #ifdef CONFIG_OF
  151. static const struct of_device_id gpio_nand_id_table[] = {
  152. { .compatible = "gpio-control-nand" },
  153. {}
  154. };
  155. MODULE_DEVICE_TABLE(of, gpio_nand_id_table);
  156. static int gpio_nand_get_config_of(const struct device *dev,
  157. struct gpio_nand_platdata *plat)
  158. {
  159. u32 val;
  160. if (!of_property_read_u32(dev->of_node, "bank-width", &val)) {
  161. if (val == 2) {
  162. plat->options |= NAND_BUSWIDTH_16;
  163. } else if (val != 1) {
  164. dev_err(dev, "invalid bank-width %u\n", val);
  165. return -EINVAL;
  166. }
  167. }
  168. plat->gpio_rdy = of_get_gpio(dev->of_node, 0);
  169. plat->gpio_nce = of_get_gpio(dev->of_node, 1);
  170. plat->gpio_ale = of_get_gpio(dev->of_node, 2);
  171. plat->gpio_cle = of_get_gpio(dev->of_node, 3);
  172. plat->gpio_nwp = of_get_gpio(dev->of_node, 4);
  173. if (!of_property_read_u32(dev->of_node, "chip-delay", &val))
  174. plat->chip_delay = val;
  175. return 0;
  176. }
  177. static struct resource *gpio_nand_get_io_sync_of(struct platform_device *pdev)
  178. {
  179. struct resource *r = devm_kzalloc(&pdev->dev, sizeof(*r), GFP_KERNEL);
  180. u64 addr;
  181. if (!r || of_property_read_u64(pdev->dev.of_node,
  182. "gpio-control-nand,io-sync-reg", &addr))
  183. return NULL;
  184. r->start = addr;
  185. r->end = r->start + 0x3;
  186. r->flags = IORESOURCE_MEM;
  187. return r;
  188. }
  189. #else /* CONFIG_OF */
  190. #define gpio_nand_id_table NULL
  191. static inline int gpio_nand_get_config_of(const struct device *dev,
  192. struct gpio_nand_platdata *plat)
  193. {
  194. return -ENOSYS;
  195. }
  196. static inline struct resource *
  197. gpio_nand_get_io_sync_of(struct platform_device *pdev)
  198. {
  199. return NULL;
  200. }
  201. #endif /* CONFIG_OF */
  202. static inline int gpio_nand_get_config(const struct device *dev,
  203. struct gpio_nand_platdata *plat)
  204. {
  205. int ret = gpio_nand_get_config_of(dev, plat);
  206. if (!ret)
  207. return ret;
  208. if (dev->platform_data) {
  209. memcpy(plat, dev->platform_data, sizeof(*plat));
  210. return 0;
  211. }
  212. return -EINVAL;
  213. }
  214. static inline struct resource *
  215. gpio_nand_get_io_sync(struct platform_device *pdev)
  216. {
  217. struct resource *r = gpio_nand_get_io_sync_of(pdev);
  218. if (r)
  219. return r;
  220. return platform_get_resource(pdev, IORESOURCE_MEM, 1);
  221. }
  222. static int __devexit gpio_nand_remove(struct platform_device *dev)
  223. {
  224. struct gpiomtd *gpiomtd = platform_get_drvdata(dev);
  225. struct resource *res;
  226. nand_release(&gpiomtd->mtd_info);
  227. res = gpio_nand_get_io_sync(dev);
  228. iounmap(gpiomtd->io_sync);
  229. if (res)
  230. release_mem_region(res->start, resource_size(res));
  231. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  232. iounmap(gpiomtd->nand_chip.IO_ADDR_R);
  233. release_mem_region(res->start, resource_size(res));
  234. if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
  235. gpio_set_value(gpiomtd->plat.gpio_nwp, 0);
  236. gpio_set_value(gpiomtd->plat.gpio_nce, 1);
  237. gpio_free(gpiomtd->plat.gpio_cle);
  238. gpio_free(gpiomtd->plat.gpio_ale);
  239. gpio_free(gpiomtd->plat.gpio_nce);
  240. if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
  241. gpio_free(gpiomtd->plat.gpio_nwp);
  242. gpio_free(gpiomtd->plat.gpio_rdy);
  243. kfree(gpiomtd);
  244. return 0;
  245. }
  246. static void __iomem *request_and_remap(struct resource *res, size_t size,
  247. const char *name, int *err)
  248. {
  249. void __iomem *ptr;
  250. if (!request_mem_region(res->start, resource_size(res), name)) {
  251. *err = -EBUSY;
  252. return NULL;
  253. }
  254. ptr = ioremap(res->start, size);
  255. if (!ptr) {
  256. release_mem_region(res->start, resource_size(res));
  257. *err = -ENOMEM;
  258. }
  259. return ptr;
  260. }
  261. static int __devinit gpio_nand_probe(struct platform_device *dev)
  262. {
  263. struct gpiomtd *gpiomtd;
  264. struct nand_chip *this;
  265. struct resource *res0, *res1;
  266. struct mtd_part_parser_data ppdata = {};
  267. int ret = 0;
  268. if (!dev->dev.of_node && !dev->dev.platform_data)
  269. return -EINVAL;
  270. res0 = platform_get_resource(dev, IORESOURCE_MEM, 0);
  271. if (!res0)
  272. return -EINVAL;
  273. gpiomtd = kzalloc(sizeof(*gpiomtd), GFP_KERNEL);
  274. if (gpiomtd == NULL) {
  275. dev_err(&dev->dev, "failed to create NAND MTD\n");
  276. return -ENOMEM;
  277. }
  278. this = &gpiomtd->nand_chip;
  279. this->IO_ADDR_R = request_and_remap(res0, 2, "NAND", &ret);
  280. if (!this->IO_ADDR_R) {
  281. dev_err(&dev->dev, "unable to map NAND\n");
  282. goto err_map;
  283. }
  284. res1 = gpio_nand_get_io_sync(dev);
  285. if (res1) {
  286. gpiomtd->io_sync = request_and_remap(res1, 4, "NAND sync", &ret);
  287. if (!gpiomtd->io_sync) {
  288. dev_err(&dev->dev, "unable to map sync NAND\n");
  289. goto err_sync;
  290. }
  291. }
  292. ret = gpio_nand_get_config(&dev->dev, &gpiomtd->plat);
  293. if (ret)
  294. goto err_nce;
  295. ret = gpio_request(gpiomtd->plat.gpio_nce, "NAND NCE");
  296. if (ret)
  297. goto err_nce;
  298. gpio_direction_output(gpiomtd->plat.gpio_nce, 1);
  299. if (gpio_is_valid(gpiomtd->plat.gpio_nwp)) {
  300. ret = gpio_request(gpiomtd->plat.gpio_nwp, "NAND NWP");
  301. if (ret)
  302. goto err_nwp;
  303. gpio_direction_output(gpiomtd->plat.gpio_nwp, 1);
  304. }
  305. ret = gpio_request(gpiomtd->plat.gpio_ale, "NAND ALE");
  306. if (ret)
  307. goto err_ale;
  308. gpio_direction_output(gpiomtd->plat.gpio_ale, 0);
  309. ret = gpio_request(gpiomtd->plat.gpio_cle, "NAND CLE");
  310. if (ret)
  311. goto err_cle;
  312. gpio_direction_output(gpiomtd->plat.gpio_cle, 0);
  313. ret = gpio_request(gpiomtd->plat.gpio_rdy, "NAND RDY");
  314. if (ret)
  315. goto err_rdy;
  316. gpio_direction_input(gpiomtd->plat.gpio_rdy);
  317. this->IO_ADDR_W = this->IO_ADDR_R;
  318. this->ecc.mode = NAND_ECC_SOFT;
  319. this->options = gpiomtd->plat.options;
  320. this->chip_delay = gpiomtd->plat.chip_delay;
  321. /* install our routines */
  322. this->cmd_ctrl = gpio_nand_cmd_ctrl;
  323. this->dev_ready = gpio_nand_devready;
  324. if (this->options & NAND_BUSWIDTH_16) {
  325. this->read_buf = gpio_nand_readbuf16;
  326. this->write_buf = gpio_nand_writebuf16;
  327. this->verify_buf = gpio_nand_verifybuf16;
  328. } else {
  329. this->read_buf = gpio_nand_readbuf;
  330. this->write_buf = gpio_nand_writebuf;
  331. this->verify_buf = gpio_nand_verifybuf;
  332. }
  333. /* set the mtd private data for the nand driver */
  334. gpiomtd->mtd_info.priv = this;
  335. gpiomtd->mtd_info.owner = THIS_MODULE;
  336. if (nand_scan(&gpiomtd->mtd_info, 1)) {
  337. dev_err(&dev->dev, "no nand chips found?\n");
  338. ret = -ENXIO;
  339. goto err_wp;
  340. }
  341. if (gpiomtd->plat.adjust_parts)
  342. gpiomtd->plat.adjust_parts(&gpiomtd->plat,
  343. gpiomtd->mtd_info.size);
  344. ppdata.of_node = dev->dev.of_node;
  345. ret = mtd_device_parse_register(&gpiomtd->mtd_info, NULL, &ppdata,
  346. gpiomtd->plat.parts,
  347. gpiomtd->plat.num_parts);
  348. if (ret)
  349. goto err_wp;
  350. platform_set_drvdata(dev, gpiomtd);
  351. return 0;
  352. err_wp:
  353. if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
  354. gpio_set_value(gpiomtd->plat.gpio_nwp, 0);
  355. gpio_free(gpiomtd->plat.gpio_rdy);
  356. err_rdy:
  357. gpio_free(gpiomtd->plat.gpio_cle);
  358. err_cle:
  359. gpio_free(gpiomtd->plat.gpio_ale);
  360. err_ale:
  361. if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
  362. gpio_free(gpiomtd->plat.gpio_nwp);
  363. err_nwp:
  364. gpio_free(gpiomtd->plat.gpio_nce);
  365. err_nce:
  366. iounmap(gpiomtd->io_sync);
  367. if (res1)
  368. release_mem_region(res1->start, resource_size(res1));
  369. err_sync:
  370. iounmap(gpiomtd->nand_chip.IO_ADDR_R);
  371. release_mem_region(res0->start, resource_size(res0));
  372. err_map:
  373. kfree(gpiomtd);
  374. return ret;
  375. }
  376. static struct platform_driver gpio_nand_driver = {
  377. .probe = gpio_nand_probe,
  378. .remove = gpio_nand_remove,
  379. .driver = {
  380. .name = "gpio-nand",
  381. .of_match_table = gpio_nand_id_table,
  382. },
  383. };
  384. static int __init gpio_nand_init(void)
  385. {
  386. printk(KERN_INFO "GPIO NAND driver, © 2004 Simtec Electronics\n");
  387. return platform_driver_register(&gpio_nand_driver);
  388. }
  389. static void __exit gpio_nand_exit(void)
  390. {
  391. platform_driver_unregister(&gpio_nand_driver);
  392. }
  393. module_init(gpio_nand_init);
  394. module_exit(gpio_nand_exit);
  395. MODULE_LICENSE("GPL");
  396. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  397. MODULE_DESCRIPTION("GPIO NAND Driver");