/kern_2.6.32/drivers/mtd/nand/gpio.c

http://omnia2droid.googlecode.com/ · C · 375 lines · 283 code · 61 blank · 31 comment · 41 complexity · 69099a6d4fa3c0cc39fc22ef02e99cbf MD5 · raw file

  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. struct gpiomtd {
  30. void __iomem *io_sync;
  31. struct mtd_info mtd_info;
  32. struct nand_chip nand_chip;
  33. struct gpio_nand_platdata plat;
  34. };
  35. #define gpio_nand_getpriv(x) container_of(x, struct gpiomtd, mtd_info)
  36. #ifdef CONFIG_ARM
  37. /* gpio_nand_dosync()
  38. *
  39. * Make sure the GPIO state changes occur in-order with writes to NAND
  40. * memory region.
  41. * Needed on PXA due to bus-reordering within the SoC itself (see section on
  42. * I/O ordering in PXA manual (section 2.3, p35)
  43. */
  44. static void gpio_nand_dosync(struct gpiomtd *gpiomtd)
  45. {
  46. unsigned long tmp;
  47. if (gpiomtd->io_sync) {
  48. /*
  49. * Linux memory barriers don't cater for what's required here.
  50. * What's required is what's here - a read from a separate
  51. * region with a dependency on that read.
  52. */
  53. tmp = readl(gpiomtd->io_sync);
  54. asm volatile("mov %1, %0\n" : "=r" (tmp) : "r" (tmp));
  55. }
  56. }
  57. #else
  58. static inline void gpio_nand_dosync(struct gpiomtd *gpiomtd) {}
  59. #endif
  60. static void gpio_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  61. {
  62. struct gpiomtd *gpiomtd = gpio_nand_getpriv(mtd);
  63. gpio_nand_dosync(gpiomtd);
  64. if (ctrl & NAND_CTRL_CHANGE) {
  65. gpio_set_value(gpiomtd->plat.gpio_nce, !(ctrl & NAND_NCE));
  66. gpio_set_value(gpiomtd->plat.gpio_cle, !!(ctrl & NAND_CLE));
  67. gpio_set_value(gpiomtd->plat.gpio_ale, !!(ctrl & NAND_ALE));
  68. gpio_nand_dosync(gpiomtd);
  69. }
  70. if (cmd == NAND_CMD_NONE)
  71. return;
  72. writeb(cmd, gpiomtd->nand_chip.IO_ADDR_W);
  73. gpio_nand_dosync(gpiomtd);
  74. }
  75. static void gpio_nand_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
  76. {
  77. struct nand_chip *this = mtd->priv;
  78. writesb(this->IO_ADDR_W, buf, len);
  79. }
  80. static void gpio_nand_readbuf(struct mtd_info *mtd, u_char *buf, int len)
  81. {
  82. struct nand_chip *this = mtd->priv;
  83. readsb(this->IO_ADDR_R, buf, len);
  84. }
  85. static int gpio_nand_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
  86. {
  87. struct nand_chip *this = mtd->priv;
  88. unsigned char read, *p = (unsigned char *) buf;
  89. int i, err = 0;
  90. for (i = 0; i < len; i++) {
  91. read = readb(this->IO_ADDR_R);
  92. if (read != p[i]) {
  93. pr_debug("%s: err at %d (read %04x vs %04x)\n",
  94. __func__, i, read, p[i]);
  95. err = -EFAULT;
  96. }
  97. }
  98. return err;
  99. }
  100. static void gpio_nand_writebuf16(struct mtd_info *mtd, const u_char *buf,
  101. int len)
  102. {
  103. struct nand_chip *this = mtd->priv;
  104. if (IS_ALIGNED((unsigned long)buf, 2)) {
  105. writesw(this->IO_ADDR_W, 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. writew(*ptr, this->IO_ADDR_W);
  111. }
  112. }
  113. static void gpio_nand_readbuf16(struct mtd_info *mtd, u_char *buf, int len)
  114. {
  115. struct nand_chip *this = mtd->priv;
  116. if (IS_ALIGNED((unsigned long)buf, 2)) {
  117. readsw(this->IO_ADDR_R, buf, len>>1);
  118. } else {
  119. int i;
  120. unsigned short *ptr = (unsigned short *)buf;
  121. for (i = 0; i < len; i += 2, ptr++)
  122. *ptr = readw(this->IO_ADDR_R);
  123. }
  124. }
  125. static int gpio_nand_verifybuf16(struct mtd_info *mtd, const u_char *buf,
  126. int len)
  127. {
  128. struct nand_chip *this = mtd->priv;
  129. unsigned short read, *p = (unsigned short *) buf;
  130. int i, err = 0;
  131. len >>= 1;
  132. for (i = 0; i < len; i++) {
  133. read = readw(this->IO_ADDR_R);
  134. if (read != p[i]) {
  135. pr_debug("%s: err at %d (read %04x vs %04x)\n",
  136. __func__, i, read, p[i]);
  137. err = -EFAULT;
  138. }
  139. }
  140. return err;
  141. }
  142. static int gpio_nand_devready(struct mtd_info *mtd)
  143. {
  144. struct gpiomtd *gpiomtd = gpio_nand_getpriv(mtd);
  145. return gpio_get_value(gpiomtd->plat.gpio_rdy);
  146. }
  147. static int __devexit gpio_nand_remove(struct platform_device *dev)
  148. {
  149. struct gpiomtd *gpiomtd = platform_get_drvdata(dev);
  150. struct resource *res;
  151. nand_release(&gpiomtd->mtd_info);
  152. res = platform_get_resource(dev, IORESOURCE_MEM, 1);
  153. iounmap(gpiomtd->io_sync);
  154. if (res)
  155. release_mem_region(res->start, res->end - res->start + 1);
  156. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  157. iounmap(gpiomtd->nand_chip.IO_ADDR_R);
  158. release_mem_region(res->start, res->end - res->start + 1);
  159. if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
  160. gpio_set_value(gpiomtd->plat.gpio_nwp, 0);
  161. gpio_set_value(gpiomtd->plat.gpio_nce, 1);
  162. gpio_free(gpiomtd->plat.gpio_cle);
  163. gpio_free(gpiomtd->plat.gpio_ale);
  164. gpio_free(gpiomtd->plat.gpio_nce);
  165. if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
  166. gpio_free(gpiomtd->plat.gpio_nwp);
  167. gpio_free(gpiomtd->plat.gpio_rdy);
  168. kfree(gpiomtd);
  169. return 0;
  170. }
  171. static void __iomem *request_and_remap(struct resource *res, size_t size,
  172. const char *name, int *err)
  173. {
  174. void __iomem *ptr;
  175. if (!request_mem_region(res->start, res->end - res->start + 1, name)) {
  176. *err = -EBUSY;
  177. return NULL;
  178. }
  179. ptr = ioremap(res->start, size);
  180. if (!ptr) {
  181. release_mem_region(res->start, res->end - res->start + 1);
  182. *err = -ENOMEM;
  183. }
  184. return ptr;
  185. }
  186. static int __devinit gpio_nand_probe(struct platform_device *dev)
  187. {
  188. struct gpiomtd *gpiomtd;
  189. struct nand_chip *this;
  190. struct resource *res0, *res1;
  191. int ret;
  192. if (!dev->dev.platform_data)
  193. return -EINVAL;
  194. res0 = platform_get_resource(dev, IORESOURCE_MEM, 0);
  195. if (!res0)
  196. return -EINVAL;
  197. gpiomtd = kzalloc(sizeof(*gpiomtd), GFP_KERNEL);
  198. if (gpiomtd == NULL) {
  199. dev_err(&dev->dev, "failed to create NAND MTD\n");
  200. return -ENOMEM;
  201. }
  202. this = &gpiomtd->nand_chip;
  203. this->IO_ADDR_R = request_and_remap(res0, 2, "NAND", &ret);
  204. if (!this->IO_ADDR_R) {
  205. dev_err(&dev->dev, "unable to map NAND\n");
  206. goto err_map;
  207. }
  208. res1 = platform_get_resource(dev, IORESOURCE_MEM, 1);
  209. if (res1) {
  210. gpiomtd->io_sync = request_and_remap(res1, 4, "NAND sync", &ret);
  211. if (!gpiomtd->io_sync) {
  212. dev_err(&dev->dev, "unable to map sync NAND\n");
  213. goto err_sync;
  214. }
  215. }
  216. memcpy(&gpiomtd->plat, dev->dev.platform_data, sizeof(gpiomtd->plat));
  217. ret = gpio_request(gpiomtd->plat.gpio_nce, "NAND NCE");
  218. if (ret)
  219. goto err_nce;
  220. gpio_direction_output(gpiomtd->plat.gpio_nce, 1);
  221. if (gpio_is_valid(gpiomtd->plat.gpio_nwp)) {
  222. ret = gpio_request(gpiomtd->plat.gpio_nwp, "NAND NWP");
  223. if (ret)
  224. goto err_nwp;
  225. gpio_direction_output(gpiomtd->plat.gpio_nwp, 1);
  226. }
  227. ret = gpio_request(gpiomtd->plat.gpio_ale, "NAND ALE");
  228. if (ret)
  229. goto err_ale;
  230. gpio_direction_output(gpiomtd->plat.gpio_ale, 0);
  231. ret = gpio_request(gpiomtd->plat.gpio_cle, "NAND CLE");
  232. if (ret)
  233. goto err_cle;
  234. gpio_direction_output(gpiomtd->plat.gpio_cle, 0);
  235. ret = gpio_request(gpiomtd->plat.gpio_rdy, "NAND RDY");
  236. if (ret)
  237. goto err_rdy;
  238. gpio_direction_input(gpiomtd->plat.gpio_rdy);
  239. this->IO_ADDR_W = this->IO_ADDR_R;
  240. this->ecc.mode = NAND_ECC_SOFT;
  241. this->options = gpiomtd->plat.options;
  242. this->chip_delay = gpiomtd->plat.chip_delay;
  243. /* install our routines */
  244. this->cmd_ctrl = gpio_nand_cmd_ctrl;
  245. this->dev_ready = gpio_nand_devready;
  246. if (this->options & NAND_BUSWIDTH_16) {
  247. this->read_buf = gpio_nand_readbuf16;
  248. this->write_buf = gpio_nand_writebuf16;
  249. this->verify_buf = gpio_nand_verifybuf16;
  250. } else {
  251. this->read_buf = gpio_nand_readbuf;
  252. this->write_buf = gpio_nand_writebuf;
  253. this->verify_buf = gpio_nand_verifybuf;
  254. }
  255. /* set the mtd private data for the nand driver */
  256. gpiomtd->mtd_info.priv = this;
  257. gpiomtd->mtd_info.owner = THIS_MODULE;
  258. if (nand_scan(&gpiomtd->mtd_info, 1)) {
  259. dev_err(&dev->dev, "no nand chips found?\n");
  260. ret = -ENXIO;
  261. goto err_wp;
  262. }
  263. if (gpiomtd->plat.adjust_parts)
  264. gpiomtd->plat.adjust_parts(&gpiomtd->plat,
  265. gpiomtd->mtd_info.size);
  266. add_mtd_partitions(&gpiomtd->mtd_info, gpiomtd->plat.parts,
  267. gpiomtd->plat.num_parts);
  268. platform_set_drvdata(dev, gpiomtd);
  269. return 0;
  270. err_wp:
  271. if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
  272. gpio_set_value(gpiomtd->plat.gpio_nwp, 0);
  273. gpio_free(gpiomtd->plat.gpio_rdy);
  274. err_rdy:
  275. gpio_free(gpiomtd->plat.gpio_cle);
  276. err_cle:
  277. gpio_free(gpiomtd->plat.gpio_ale);
  278. err_ale:
  279. if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
  280. gpio_free(gpiomtd->plat.gpio_nwp);
  281. err_nwp:
  282. gpio_free(gpiomtd->plat.gpio_nce);
  283. err_nce:
  284. iounmap(gpiomtd->io_sync);
  285. if (res1)
  286. release_mem_region(res1->start, res1->end - res1->start + 1);
  287. err_sync:
  288. iounmap(gpiomtd->nand_chip.IO_ADDR_R);
  289. release_mem_region(res0->start, res0->end - res0->start + 1);
  290. err_map:
  291. kfree(gpiomtd);
  292. return ret;
  293. }
  294. static struct platform_driver gpio_nand_driver = {
  295. .probe = gpio_nand_probe,
  296. .remove = gpio_nand_remove,
  297. .driver = {
  298. .name = "gpio-nand",
  299. },
  300. };
  301. static int __init gpio_nand_init(void)
  302. {
  303. printk(KERN_INFO "GPIO NAND driver, Š 2004 Simtec Electronics\n");
  304. return platform_driver_register(&gpio_nand_driver);
  305. }
  306. static void __exit gpio_nand_exit(void)
  307. {
  308. platform_driver_unregister(&gpio_nand_driver);
  309. }
  310. module_init(gpio_nand_init);
  311. module_exit(gpio_nand_exit);
  312. MODULE_LICENSE("GPL");
  313. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  314. MODULE_DESCRIPTION("GPIO NAND Driver");