ndfc.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * drivers/mtd/ndfc.c
  3. *
  4. * Overview:
  5. * Platform independent driver for NDFC (NanD Flash Controller)
  6. * integrated into EP440 cores
  7. *
  8. * Ported to an OF platform driver by Sean MacLennan
  9. *
  10. * The NDFC supports multiple chips, but this driver only supports a
  11. * single chip since I do not have access to any boards with
  12. * multiple chips.
  13. *
  14. * Author: Thomas Gleixner
  15. *
  16. * Copyright 2006 IBM
  17. * Copyright 2008 PIKA Technologies
  18. * Sean MacLennan <smaclennan@pikatech.com>
  19. *
  20. * This program is free software; you can redistribute it and/or modify it
  21. * under the terms of the GNU General Public License as published by the
  22. * Free Software Foundation; either version 2 of the License, or (at your
  23. * option) any later version.
  24. *
  25. */
  26. #include <linux/module.h>
  27. #include <linux/mtd/nand.h>
  28. #include <linux/mtd/nand_ecc.h>
  29. #include <linux/mtd/partitions.h>
  30. #include <linux/mtd/ndfc.h>
  31. #include <linux/slab.h>
  32. #include <linux/mtd/mtd.h>
  33. #include <linux/of_platform.h>
  34. #include <asm/io.h>
  35. struct ndfc_controller {
  36. struct platform_device *ofdev;
  37. void __iomem *ndfcbase;
  38. struct mtd_info mtd;
  39. struct nand_chip chip;
  40. int chip_select;
  41. struct nand_hw_control ndfc_control;
  42. #ifdef CONFIG_MTD_PARTITIONS
  43. struct mtd_partition *parts;
  44. #endif
  45. };
  46. static struct ndfc_controller ndfc_ctrl;
  47. static void ndfc_select_chip(struct mtd_info *mtd, int chip)
  48. {
  49. uint32_t ccr;
  50. struct ndfc_controller *ndfc = &ndfc_ctrl;
  51. ccr = in_be32(ndfc->ndfcbase + NDFC_CCR);
  52. if (chip >= 0) {
  53. ccr &= ~NDFC_CCR_BS_MASK;
  54. ccr |= NDFC_CCR_BS(chip + ndfc->chip_select);
  55. } else
  56. ccr |= NDFC_CCR_RESET_CE;
  57. out_be32(ndfc->ndfcbase + NDFC_CCR, ccr);
  58. }
  59. static void ndfc_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  60. {
  61. struct ndfc_controller *ndfc = &ndfc_ctrl;
  62. if (cmd == NAND_CMD_NONE)
  63. return;
  64. if (ctrl & NAND_CLE)
  65. writel(cmd & 0xFF, ndfc->ndfcbase + NDFC_CMD);
  66. else
  67. writel(cmd & 0xFF, ndfc->ndfcbase + NDFC_ALE);
  68. }
  69. static int ndfc_ready(struct mtd_info *mtd)
  70. {
  71. struct ndfc_controller *ndfc = &ndfc_ctrl;
  72. return in_be32(ndfc->ndfcbase + NDFC_STAT) & NDFC_STAT_IS_READY;
  73. }
  74. static void ndfc_enable_hwecc(struct mtd_info *mtd, int mode)
  75. {
  76. uint32_t ccr;
  77. struct ndfc_controller *ndfc = &ndfc_ctrl;
  78. ccr = in_be32(ndfc->ndfcbase + NDFC_CCR);
  79. ccr |= NDFC_CCR_RESET_ECC;
  80. out_be32(ndfc->ndfcbase + NDFC_CCR, ccr);
  81. wmb();
  82. }
  83. static int ndfc_calculate_ecc(struct mtd_info *mtd,
  84. const u_char *dat, u_char *ecc_code)
  85. {
  86. struct ndfc_controller *ndfc = &ndfc_ctrl;
  87. uint32_t ecc;
  88. uint8_t *p = (uint8_t *)&ecc;
  89. wmb();
  90. ecc = in_be32(ndfc->ndfcbase + NDFC_ECC);
  91. /* The NDFC uses Smart Media (SMC) bytes order */
  92. ecc_code[0] = p[1];
  93. ecc_code[1] = p[2];
  94. ecc_code[2] = p[3];
  95. return 0;
  96. }
  97. /*
  98. * Speedups for buffer read/write/verify
  99. *
  100. * NDFC allows 32bit read/write of data. So we can speed up the buffer
  101. * functions. No further checking, as nand_base will always read/write
  102. * page aligned.
  103. */
  104. static void ndfc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  105. {
  106. struct ndfc_controller *ndfc = &ndfc_ctrl;
  107. uint32_t *p = (uint32_t *) buf;
  108. for(;len > 0; len -= 4)
  109. *p++ = in_be32(ndfc->ndfcbase + NDFC_DATA);
  110. }
  111. static void ndfc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  112. {
  113. struct ndfc_controller *ndfc = &ndfc_ctrl;
  114. uint32_t *p = (uint32_t *) buf;
  115. for(;len > 0; len -= 4)
  116. out_be32(ndfc->ndfcbase + NDFC_DATA, *p++);
  117. }
  118. static int ndfc_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  119. {
  120. struct ndfc_controller *ndfc = &ndfc_ctrl;
  121. uint32_t *p = (uint32_t *) buf;
  122. for(;len > 0; len -= 4)
  123. if (*p++ != in_be32(ndfc->ndfcbase + NDFC_DATA))
  124. return -EFAULT;
  125. return 0;
  126. }
  127. /*
  128. * Initialize chip structure
  129. */
  130. static int ndfc_chip_init(struct ndfc_controller *ndfc,
  131. struct device_node *node)
  132. {
  133. #ifdef CONFIG_MTD_PARTITIONS
  134. #ifdef CONFIG_MTD_CMDLINE_PARTS
  135. static const char *part_types[] = { "cmdlinepart", NULL };
  136. #else
  137. static const char *part_types[] = { NULL };
  138. #endif
  139. #endif
  140. struct device_node *flash_np;
  141. struct nand_chip *chip = &ndfc->chip;
  142. int ret;
  143. chip->IO_ADDR_R = ndfc->ndfcbase + NDFC_DATA;
  144. chip->IO_ADDR_W = ndfc->ndfcbase + NDFC_DATA;
  145. chip->cmd_ctrl = ndfc_hwcontrol;
  146. chip->dev_ready = ndfc_ready;
  147. chip->select_chip = ndfc_select_chip;
  148. chip->chip_delay = 50;
  149. chip->controller = &ndfc->ndfc_control;
  150. chip->read_buf = ndfc_read_buf;
  151. chip->write_buf = ndfc_write_buf;
  152. chip->verify_buf = ndfc_verify_buf;
  153. chip->ecc.correct = nand_correct_data;
  154. chip->ecc.hwctl = ndfc_enable_hwecc;
  155. chip->ecc.calculate = ndfc_calculate_ecc;
  156. chip->ecc.mode = NAND_ECC_HW;
  157. chip->ecc.size = 256;
  158. chip->ecc.bytes = 3;
  159. ndfc->mtd.priv = chip;
  160. ndfc->mtd.owner = THIS_MODULE;
  161. flash_np = of_get_next_child(node, NULL);
  162. if (!flash_np)
  163. return -ENODEV;
  164. ndfc->mtd.name = kasprintf(GFP_KERNEL, "%s.%s",
  165. dev_name(&ndfc->ofdev->dev), flash_np->name);
  166. if (!ndfc->mtd.name) {
  167. ret = -ENOMEM;
  168. goto err;
  169. }
  170. ret = nand_scan(&ndfc->mtd, 1);
  171. if (ret)
  172. goto err;
  173. #ifdef CONFIG_MTD_PARTITIONS
  174. ret = parse_mtd_partitions(&ndfc->mtd, part_types, &ndfc->parts, 0);
  175. if (ret < 0)
  176. goto err;
  177. #ifdef CONFIG_MTD_OF_PARTS
  178. if (ret == 0) {
  179. ret = of_mtd_parse_partitions(&ndfc->ofdev->dev, flash_np,
  180. &ndfc->parts);
  181. if (ret < 0)
  182. goto err;
  183. }
  184. #endif
  185. if (ret > 0)
  186. ret = add_mtd_partitions(&ndfc->mtd, ndfc->parts, ret);
  187. else
  188. #endif
  189. ret = add_mtd_device(&ndfc->mtd);
  190. err:
  191. of_node_put(flash_np);
  192. if (ret)
  193. kfree(ndfc->mtd.name);
  194. return ret;
  195. }
  196. static int __devinit ndfc_probe(struct platform_device *ofdev,
  197. const struct of_device_id *match)
  198. {
  199. struct ndfc_controller *ndfc = &ndfc_ctrl;
  200. const __be32 *reg;
  201. u32 ccr;
  202. int err, len;
  203. spin_lock_init(&ndfc->ndfc_control.lock);
  204. init_waitqueue_head(&ndfc->ndfc_control.wq);
  205. ndfc->ofdev = ofdev;
  206. dev_set_drvdata(&ofdev->dev, ndfc);
  207. /* Read the reg property to get the chip select */
  208. reg = of_get_property(ofdev->dev.of_node, "reg", &len);
  209. if (reg == NULL || len != 12) {
  210. dev_err(&ofdev->dev, "unable read reg property (%d)\n", len);
  211. return -ENOENT;
  212. }
  213. ndfc->chip_select = be32_to_cpu(reg[0]);
  214. ndfc->ndfcbase = of_iomap(ofdev->dev.of_node, 0);
  215. if (!ndfc->ndfcbase) {
  216. dev_err(&ofdev->dev, "failed to get memory\n");
  217. return -EIO;
  218. }
  219. ccr = NDFC_CCR_BS(ndfc->chip_select);
  220. /* It is ok if ccr does not exist - just default to 0 */
  221. reg = of_get_property(ofdev->dev.of_node, "ccr", NULL);
  222. if (reg)
  223. ccr |= be32_to_cpup(reg);
  224. out_be32(ndfc->ndfcbase + NDFC_CCR, ccr);
  225. /* Set the bank settings if given */
  226. reg = of_get_property(ofdev->dev.of_node, "bank-settings", NULL);
  227. if (reg) {
  228. int offset = NDFC_BCFG0 + (ndfc->chip_select << 2);
  229. out_be32(ndfc->ndfcbase + offset, be32_to_cpup(reg));
  230. }
  231. err = ndfc_chip_init(ndfc, ofdev->dev.of_node);
  232. if (err) {
  233. iounmap(ndfc->ndfcbase);
  234. return err;
  235. }
  236. return 0;
  237. }
  238. static int __devexit ndfc_remove(struct platform_device *ofdev)
  239. {
  240. struct ndfc_controller *ndfc = dev_get_drvdata(&ofdev->dev);
  241. nand_release(&ndfc->mtd);
  242. return 0;
  243. }
  244. static const struct of_device_id ndfc_match[] = {
  245. { .compatible = "ibm,ndfc", },
  246. {}
  247. };
  248. MODULE_DEVICE_TABLE(of, ndfc_match);
  249. static struct of_platform_driver ndfc_driver = {
  250. .driver = {
  251. .name = "ndfc",
  252. .owner = THIS_MODULE,
  253. .of_match_table = ndfc_match,
  254. },
  255. .probe = ndfc_probe,
  256. .remove = __devexit_p(ndfc_remove),
  257. };
  258. static int __init ndfc_nand_init(void)
  259. {
  260. return of_register_platform_driver(&ndfc_driver);
  261. }
  262. static void __exit ndfc_nand_exit(void)
  263. {
  264. of_unregister_platform_driver(&ndfc_driver);
  265. }
  266. module_init(ndfc_nand_init);
  267. module_exit(ndfc_nand_exit);
  268. MODULE_LICENSE("GPL");
  269. MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
  270. MODULE_DESCRIPTION("OF Platform driver for NDFC");