ndfc.c 7.5 KB

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