ndfc.c 7.5 KB

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