ndfc.c 7.6 KB

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