ndfc.c 7.4 KB

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