ndfc.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. writel(ccr, ndfc->ndfcbase + NDFC_CCR);
  53. }
  54. static void ndfc_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  55. {
  56. struct nand_chip *chip = mtd->priv;
  57. if (cmd == NAND_CMD_NONE)
  58. return;
  59. if (ctrl & NAND_CLE)
  60. writel(cmd & 0xFF, chip->IO_ADDR_W + NDFC_CMD);
  61. else
  62. writel(cmd & 0xFF, chip->IO_ADDR_W + 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->autooob = mtd->pl_chip->oobinfo;
  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. struct mtd_info *mtd_ubi;
  175. nc->partitions[NAND_PARTS_CONTENT_IDX].mtdp = &mtd_ubi;
  176. add_mtd_device(&nandmtd->mtd); /* for testing */
  177. add_mtd_partitions(&nandmtd->mtd,
  178. nc->partitions,
  179. nc->nr_partitions);
  180. add_mtd_device(mtd_ubi);
  181. } else
  182. #else
  183. add_mtd_device(&nandmtd->mtd);
  184. #endif
  185. atomic_inc(&ndfc->childs_active);
  186. return 0;
  187. }
  188. static int ndfc_chip_remove(struct platform_device *pdev)
  189. {
  190. return 0;
  191. }
  192. static int ndfc_nand_probe(struct platform_device *pdev)
  193. {
  194. struct platform_nand_ctrl *nc = pdev->dev.platform_data;
  195. struct ndfc_controller_settings *settings = nc->priv;
  196. struct resource *res = pdev->resource;
  197. struct ndfc_controller *ndfc = &ndfc_ctrl;
  198. unsigned long long phys = setting->erpn | res->start;
  199. ndfc->ndfcbase = ioremap64(phys, res->end - res->start + 1);
  200. if (!ndfc->ndfcbase) {
  201. printk(KERN_ERR "NDFC: ioremap failed\n");
  202. return -EIO;
  203. }
  204. __raw_writel(settings->ccr_settings, ndfc->ndfcbase + NDFC_CCR);
  205. spin_lock_init(&ndfc->ndfc_control.lock);
  206. init_waitqueue_head(&ndfc->ndfc_control.wq);
  207. platform_set_drvdata(pdev, ndfc);
  208. printk("NDFC NAND Driver initialized. Chip-Rev: 0x%08x\n",
  209. __raw_readl(ndfc->ndfcbase + NDFC_REVID));
  210. return 0;
  211. }
  212. static int ndfc_nand_remove(struct platform_device *pdev)
  213. {
  214. struct ndfc_controller *ndfc = platform_get_drvdata(pdev);
  215. if (atomic_read(&ndfc->childs_active))
  216. return -EBUSY;
  217. if (ndfc) {
  218. platform_set_drvdata(pdev, NULL);
  219. iounmap(ndfc_ctrl.ndfcbase);
  220. ndfc_ctrl.ndfcbase = NULL;
  221. }
  222. return 0;
  223. }
  224. /* driver device registration */
  225. static struct platform_driver ndfc_chip_driver = {
  226. .probe = ndfc_chip_probe,
  227. .remove = ndfc_chip_remove,
  228. .driver = {
  229. .name = "ndfc-chip",
  230. .owner = THIS_MODULE,
  231. },
  232. };
  233. static struct platform_driver ndfc_nand_driver = {
  234. .probe = ndfc_nand_probe,
  235. .remove = ndfc_nand_remove,
  236. .driver = {
  237. .name = "ndfc-nand",
  238. .owner = THIS_MODULE,
  239. },
  240. };
  241. static int __init ndfc_nand_init(void)
  242. {
  243. int ret;
  244. spin_lock_init(&ndfc_ctrl.ndfc_control.lock);
  245. init_waitqueue_head(&ndfc_ctrl.ndfc_control.wq);
  246. ret = platform_driver_register(&ndfc_nand_driver);
  247. if (!ret)
  248. ret = platform_driver_register(&ndfc_chip_driver);
  249. return ret;
  250. }
  251. static void __exit ndfc_nand_exit(void)
  252. {
  253. platform_driver_unregister(&ndfc_chip_driver);
  254. platform_driver_unregister(&ndfc_nand_driver);
  255. }
  256. module_init(ndfc_nand_init);
  257. module_exit(ndfc_nand_exit);
  258. MODULE_LICENSE("GPL");
  259. MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
  260. MODULE_DESCRIPTION("Platform driver for NDFC");