ndfc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * drivers/mtd/ndfc.c
  3. *
  4. * Overview:
  5. * Platform independend driver for NDFC (NanD Flash Controller)
  6. * integrated into EP440 cores
  7. *
  8. * Author: Thomas Gleixner
  9. *
  10. * Copyright 2006 IBM
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/mtd/nand.h>
  20. #include <linux/mtd/nand_ecc.h>
  21. #include <linux/mtd/partitions.h>
  22. #include <linux/mtd/ndfc.h>
  23. #include <linux/mtd/mtd.h>
  24. #include <linux/platform_device.h>
  25. #include <asm/io.h>
  26. #ifdef CONFIG_40x
  27. #include <asm/ibm405.h>
  28. #else
  29. #include <asm/ibm44x.h>
  30. #endif
  31. struct ndfc_nand_mtd {
  32. struct mtd_info mtd;
  33. struct nand_chip chip;
  34. struct platform_nand_chip *pl_chip;
  35. };
  36. static struct ndfc_nand_mtd ndfc_mtd[NDFC_MAX_BANKS];
  37. struct ndfc_controller {
  38. void __iomem *ndfcbase;
  39. struct nand_hw_control ndfc_control;
  40. atomic_t childs_active;
  41. };
  42. static struct ndfc_controller ndfc_ctrl;
  43. static void ndfc_select_chip(struct mtd_info *mtd, int chip)
  44. {
  45. uint32_t ccr;
  46. struct ndfc_controller *ndfc = &ndfc_ctrl;
  47. struct nand_chip *nandchip = mtd->priv;
  48. struct ndfc_nand_mtd *nandmtd = nandchip->priv;
  49. struct platform_nand_chip *pchip = nandmtd->pl_chip;
  50. ccr = __raw_readl(ndfc->ndfcbase + NDFC_CCR);
  51. if (chip >= 0) {
  52. ccr &= ~NDFC_CCR_BS_MASK;
  53. ccr |= NDFC_CCR_BS(chip + pchip->chip_offset);
  54. } else
  55. ccr |= NDFC_CCR_RESET_CE;
  56. __raw_writel(ccr, ndfc->ndfcbase + NDFC_CCR);
  57. }
  58. static void ndfc_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  59. {
  60. struct ndfc_controller *ndfc = &ndfc_ctrl;
  61. if (cmd == NAND_CMD_NONE)
  62. return;
  63. if (ctrl & NAND_CLE)
  64. writel(cmd & 0xFF, ndfc->ndfcbase + NDFC_CMD);
  65. else
  66. writel(cmd & 0xFF, ndfc->ndfcbase + NDFC_ALE);
  67. }
  68. static int ndfc_ready(struct mtd_info *mtd)
  69. {
  70. struct ndfc_controller *ndfc = &ndfc_ctrl;
  71. return __raw_readl(ndfc->ndfcbase + NDFC_STAT) & NDFC_STAT_IS_READY;
  72. }
  73. static void ndfc_enable_hwecc(struct mtd_info *mtd, int mode)
  74. {
  75. uint32_t ccr;
  76. struct ndfc_controller *ndfc = &ndfc_ctrl;
  77. ccr = __raw_readl(ndfc->ndfcbase + NDFC_CCR);
  78. ccr |= NDFC_CCR_RESET_ECC;
  79. __raw_writel(ccr, ndfc->ndfcbase + NDFC_CCR);
  80. wmb();
  81. }
  82. static int ndfc_calculate_ecc(struct mtd_info *mtd,
  83. const u_char *dat, u_char *ecc_code)
  84. {
  85. struct ndfc_controller *ndfc = &ndfc_ctrl;
  86. uint32_t ecc;
  87. uint8_t *p = (uint8_t *)&ecc;
  88. wmb();
  89. ecc = __raw_readl(ndfc->ndfcbase + NDFC_ECC);
  90. ecc_code[0] = p[1];
  91. ecc_code[1] = p[2];
  92. ecc_code[2] = p[3];
  93. return 0;
  94. }
  95. /*
  96. * Speedups for buffer read/write/verify
  97. *
  98. * NDFC allows 32bit read/write of data. So we can speed up the buffer
  99. * functions. No further checking, as nand_base will always read/write
  100. * page aligned.
  101. */
  102. static void ndfc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  103. {
  104. struct ndfc_controller *ndfc = &ndfc_ctrl;
  105. uint32_t *p = (uint32_t *) buf;
  106. for(;len > 0; len -= 4)
  107. *p++ = __raw_readl(ndfc->ndfcbase + NDFC_DATA);
  108. }
  109. static void ndfc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  110. {
  111. struct ndfc_controller *ndfc = &ndfc_ctrl;
  112. uint32_t *p = (uint32_t *) buf;
  113. for(;len > 0; len -= 4)
  114. __raw_writel(*p++, ndfc->ndfcbase + NDFC_DATA);
  115. }
  116. static int ndfc_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  117. {
  118. struct ndfc_controller *ndfc = &ndfc_ctrl;
  119. uint32_t *p = (uint32_t *) buf;
  120. for(;len > 0; len -= 4)
  121. if (*p++ != __raw_readl(ndfc->ndfcbase + NDFC_DATA))
  122. return -EFAULT;
  123. return 0;
  124. }
  125. /*
  126. * Initialize chip structure
  127. */
  128. static void ndfc_chip_init(struct ndfc_nand_mtd *mtd)
  129. {
  130. struct ndfc_controller *ndfc = &ndfc_ctrl;
  131. struct nand_chip *chip = &mtd->chip;
  132. chip->IO_ADDR_R = ndfc->ndfcbase + NDFC_DATA;
  133. chip->IO_ADDR_W = ndfc->ndfcbase + NDFC_DATA;
  134. chip->cmd_ctrl = ndfc_hwcontrol;
  135. chip->dev_ready = ndfc_ready;
  136. chip->select_chip = ndfc_select_chip;
  137. chip->chip_delay = 50;
  138. chip->priv = mtd;
  139. chip->options = mtd->pl_chip->options;
  140. chip->controller = &ndfc->ndfc_control;
  141. chip->read_buf = ndfc_read_buf;
  142. chip->write_buf = ndfc_write_buf;
  143. chip->verify_buf = ndfc_verify_buf;
  144. chip->ecc.correct = nand_correct_data;
  145. chip->ecc.hwctl = ndfc_enable_hwecc;
  146. chip->ecc.calculate = ndfc_calculate_ecc;
  147. chip->ecc.mode = NAND_ECC_HW;
  148. chip->ecc.size = 256;
  149. chip->ecc.bytes = 3;
  150. chip->ecclayout = chip->ecc.layout = mtd->pl_chip->ecclayout;
  151. mtd->mtd.priv = chip;
  152. mtd->mtd.owner = THIS_MODULE;
  153. }
  154. static int ndfc_chip_probe(struct platform_device *pdev)
  155. {
  156. struct platform_nand_chip *nc = pdev->dev.platform_data;
  157. struct ndfc_chip_settings *settings = nc->priv;
  158. struct ndfc_controller *ndfc = &ndfc_ctrl;
  159. struct ndfc_nand_mtd *nandmtd;
  160. if (nc->chip_offset >= NDFC_MAX_BANKS || nc->nr_chips > NDFC_MAX_BANKS)
  161. return -EINVAL;
  162. /* Set the bank settings */
  163. __raw_writel(settings->bank_settings,
  164. ndfc->ndfcbase + NDFC_BCFG0 + (nc->chip_offset << 2));
  165. nandmtd = &ndfc_mtd[pdev->id];
  166. if (nandmtd->pl_chip)
  167. return -EBUSY;
  168. nandmtd->pl_chip = nc;
  169. ndfc_chip_init(nandmtd);
  170. /* Scan for chips */
  171. if (nand_scan(&nandmtd->mtd, nc->nr_chips)) {
  172. nandmtd->pl_chip = NULL;
  173. return -ENODEV;
  174. }
  175. #ifdef CONFIG_MTD_PARTITIONS
  176. printk("Number of partitions %d\n", nc->nr_partitions);
  177. if (nc->nr_partitions) {
  178. /* Add the full device, so complete dumps can be made */
  179. add_mtd_device(&nandmtd->mtd);
  180. add_mtd_partitions(&nandmtd->mtd, nc->partitions,
  181. nc->nr_partitions);
  182. } else
  183. #else
  184. add_mtd_device(&nandmtd->mtd);
  185. #endif
  186. atomic_inc(&ndfc->childs_active);
  187. return 0;
  188. }
  189. static int ndfc_chip_remove(struct platform_device *pdev)
  190. {
  191. return 0;
  192. }
  193. static int ndfc_nand_probe(struct platform_device *pdev)
  194. {
  195. struct platform_nand_ctrl *nc = pdev->dev.platform_data;
  196. struct ndfc_controller_settings *settings = nc->priv;
  197. struct resource *res = pdev->resource;
  198. struct ndfc_controller *ndfc = &ndfc_ctrl;
  199. unsigned long long phys = settings->ndfc_erpn | res->start;
  200. #ifndef CONFIG_PHYS_64BIT
  201. ndfc->ndfcbase = ioremap((phys_addr_t)phys, res->end - res->start + 1);
  202. #else
  203. ndfc->ndfcbase = ioremap64(phys, res->end - res->start + 1);
  204. #endif
  205. if (!ndfc->ndfcbase) {
  206. printk(KERN_ERR "NDFC: ioremap failed\n");
  207. return -EIO;
  208. }
  209. __raw_writel(settings->ccr_settings, ndfc->ndfcbase + NDFC_CCR);
  210. spin_lock_init(&ndfc->ndfc_control.lock);
  211. init_waitqueue_head(&ndfc->ndfc_control.wq);
  212. platform_set_drvdata(pdev, ndfc);
  213. printk("NDFC NAND Driver initialized. Chip-Rev: 0x%08x\n",
  214. __raw_readl(ndfc->ndfcbase + NDFC_REVID));
  215. return 0;
  216. }
  217. static int ndfc_nand_remove(struct platform_device *pdev)
  218. {
  219. struct ndfc_controller *ndfc = platform_get_drvdata(pdev);
  220. if (atomic_read(&ndfc->childs_active))
  221. return -EBUSY;
  222. if (ndfc) {
  223. platform_set_drvdata(pdev, NULL);
  224. iounmap(ndfc_ctrl.ndfcbase);
  225. ndfc_ctrl.ndfcbase = NULL;
  226. }
  227. return 0;
  228. }
  229. /* driver device registration */
  230. static struct platform_driver ndfc_chip_driver = {
  231. .probe = ndfc_chip_probe,
  232. .remove = ndfc_chip_remove,
  233. .driver = {
  234. .name = "ndfc-chip",
  235. .owner = THIS_MODULE,
  236. },
  237. };
  238. static struct platform_driver ndfc_nand_driver = {
  239. .probe = ndfc_nand_probe,
  240. .remove = ndfc_nand_remove,
  241. .driver = {
  242. .name = "ndfc-nand",
  243. .owner = THIS_MODULE,
  244. },
  245. };
  246. static int __init ndfc_nand_init(void)
  247. {
  248. int ret;
  249. spin_lock_init(&ndfc_ctrl.ndfc_control.lock);
  250. init_waitqueue_head(&ndfc_ctrl.ndfc_control.wq);
  251. ret = platform_driver_register(&ndfc_nand_driver);
  252. if (!ret)
  253. ret = platform_driver_register(&ndfc_chip_driver);
  254. return ret;
  255. }
  256. static void __exit ndfc_nand_exit(void)
  257. {
  258. platform_driver_unregister(&ndfc_chip_driver);
  259. platform_driver_unregister(&ndfc_nand_driver);
  260. }
  261. module_init(ndfc_nand_init);
  262. module_exit(ndfc_nand_exit);
  263. MODULE_LICENSE("GPL");
  264. MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
  265. MODULE_DESCRIPTION("Platform driver for NDFC");
  266. MODULE_ALIAS("platform:ndfc-chip");
  267. MODULE_ALIAS("platform:ndfc-nand");