ndfc.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. #define NDFC_MAX_CS 4
  36. struct ndfc_controller {
  37. struct platform_device *ofdev;
  38. void __iomem *ndfcbase;
  39. struct mtd_info mtd;
  40. struct nand_chip chip;
  41. int chip_select;
  42. struct nand_hw_control ndfc_control;
  43. };
  44. static struct ndfc_controller ndfc_ctrl[NDFC_MAX_CS];
  45. static void ndfc_select_chip(struct mtd_info *mtd, int chip)
  46. {
  47. uint32_t ccr;
  48. struct nand_chip *nchip = mtd->priv;
  49. struct ndfc_controller *ndfc = nchip->priv;
  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 nand_chip *chip = mtd->priv;
  61. struct ndfc_controller *ndfc = chip->priv;
  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 nand_chip *chip = mtd->priv;
  72. struct ndfc_controller *ndfc = chip->priv;
  73. return in_be32(ndfc->ndfcbase + NDFC_STAT) & NDFC_STAT_IS_READY;
  74. }
  75. static void ndfc_enable_hwecc(struct mtd_info *mtd, int mode)
  76. {
  77. uint32_t ccr;
  78. struct nand_chip *chip = mtd->priv;
  79. struct ndfc_controller *ndfc = chip->priv;
  80. ccr = in_be32(ndfc->ndfcbase + NDFC_CCR);
  81. ccr |= NDFC_CCR_RESET_ECC;
  82. out_be32(ndfc->ndfcbase + NDFC_CCR, 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 nand_chip *chip = mtd->priv;
  89. struct ndfc_controller *ndfc = chip->priv;
  90. uint32_t ecc;
  91. uint8_t *p = (uint8_t *)&ecc;
  92. wmb();
  93. ecc = in_be32(ndfc->ndfcbase + NDFC_ECC);
  94. /* The NDFC uses Smart Media (SMC) bytes order */
  95. ecc_code[0] = p[1];
  96. ecc_code[1] = p[2];
  97. ecc_code[2] = p[3];
  98. return 0;
  99. }
  100. /*
  101. * Speedups for buffer read/write/verify
  102. *
  103. * NDFC allows 32bit read/write of data. So we can speed up the buffer
  104. * functions. No further checking, as nand_base will always read/write
  105. * page aligned.
  106. */
  107. static void ndfc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  108. {
  109. struct nand_chip *chip = mtd->priv;
  110. struct ndfc_controller *ndfc = chip->priv;
  111. uint32_t *p = (uint32_t *) buf;
  112. for(;len > 0; len -= 4)
  113. *p++ = in_be32(ndfc->ndfcbase + NDFC_DATA);
  114. }
  115. static void ndfc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  116. {
  117. struct nand_chip *chip = mtd->priv;
  118. struct ndfc_controller *ndfc = chip->priv;
  119. uint32_t *p = (uint32_t *) buf;
  120. for(;len > 0; len -= 4)
  121. out_be32(ndfc->ndfcbase + NDFC_DATA, *p++);
  122. }
  123. static int ndfc_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  124. {
  125. struct nand_chip *chip = mtd->priv;
  126. struct ndfc_controller *ndfc = chip->priv;
  127. uint32_t *p = (uint32_t *) buf;
  128. for(;len > 0; len -= 4)
  129. if (*p++ != in_be32(ndfc->ndfcbase + NDFC_DATA))
  130. return -EFAULT;
  131. return 0;
  132. }
  133. /*
  134. * Initialize chip structure
  135. */
  136. static int ndfc_chip_init(struct ndfc_controller *ndfc,
  137. struct device_node *node)
  138. {
  139. struct device_node *flash_np;
  140. struct nand_chip *chip = &ndfc->chip;
  141. struct mtd_part_parser_data ppdata;
  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. chip->ecc.strength = 1;
  160. chip->priv = ndfc;
  161. ndfc->mtd.priv = chip;
  162. ndfc->mtd.owner = THIS_MODULE;
  163. flash_np = of_get_next_child(node, NULL);
  164. if (!flash_np)
  165. return -ENODEV;
  166. ppdata.of_node = flash_np;
  167. ndfc->mtd.name = kasprintf(GFP_KERNEL, "%s.%s",
  168. dev_name(&ndfc->ofdev->dev), flash_np->name);
  169. if (!ndfc->mtd.name) {
  170. ret = -ENOMEM;
  171. goto err;
  172. }
  173. ret = nand_scan(&ndfc->mtd, 1);
  174. if (ret)
  175. goto err;
  176. ret = mtd_device_parse_register(&ndfc->mtd, NULL, &ppdata, NULL, 0);
  177. err:
  178. of_node_put(flash_np);
  179. if (ret)
  180. kfree(ndfc->mtd.name);
  181. return ret;
  182. }
  183. static int __devinit ndfc_probe(struct platform_device *ofdev)
  184. {
  185. struct ndfc_controller *ndfc;
  186. const __be32 *reg;
  187. u32 ccr;
  188. int err, len, cs;
  189. /* Read the reg property to get the chip select */
  190. reg = of_get_property(ofdev->dev.of_node, "reg", &len);
  191. if (reg == NULL || len != 12) {
  192. dev_err(&ofdev->dev, "unable read reg property (%d)\n", len);
  193. return -ENOENT;
  194. }
  195. cs = be32_to_cpu(reg[0]);
  196. if (cs >= NDFC_MAX_CS) {
  197. dev_err(&ofdev->dev, "invalid CS number (%d)\n", cs);
  198. return -EINVAL;
  199. }
  200. ndfc = &ndfc_ctrl[cs];
  201. ndfc->chip_select = cs;
  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. ndfc->ndfcbase = of_iomap(ofdev->dev.of_node, 0);
  207. if (!ndfc->ndfcbase) {
  208. dev_err(&ofdev->dev, "failed to get memory\n");
  209. return -EIO;
  210. }
  211. ccr = NDFC_CCR_BS(ndfc->chip_select);
  212. /* It is ok if ccr does not exist - just default to 0 */
  213. reg = of_get_property(ofdev->dev.of_node, "ccr", NULL);
  214. if (reg)
  215. ccr |= be32_to_cpup(reg);
  216. out_be32(ndfc->ndfcbase + NDFC_CCR, ccr);
  217. /* Set the bank settings if given */
  218. reg = of_get_property(ofdev->dev.of_node, "bank-settings", NULL);
  219. if (reg) {
  220. int offset = NDFC_BCFG0 + (ndfc->chip_select << 2);
  221. out_be32(ndfc->ndfcbase + offset, be32_to_cpup(reg));
  222. }
  223. err = ndfc_chip_init(ndfc, ofdev->dev.of_node);
  224. if (err) {
  225. iounmap(ndfc->ndfcbase);
  226. return err;
  227. }
  228. return 0;
  229. }
  230. static int __devexit ndfc_remove(struct platform_device *ofdev)
  231. {
  232. struct ndfc_controller *ndfc = dev_get_drvdata(&ofdev->dev);
  233. nand_release(&ndfc->mtd);
  234. kfree(ndfc->mtd.name);
  235. return 0;
  236. }
  237. static const struct of_device_id ndfc_match[] = {
  238. { .compatible = "ibm,ndfc", },
  239. {}
  240. };
  241. MODULE_DEVICE_TABLE(of, ndfc_match);
  242. static struct platform_driver ndfc_driver = {
  243. .driver = {
  244. .name = "ndfc",
  245. .owner = THIS_MODULE,
  246. .of_match_table = ndfc_match,
  247. },
  248. .probe = ndfc_probe,
  249. .remove = __devexit_p(ndfc_remove),
  250. };
  251. module_platform_driver(ndfc_driver);
  252. MODULE_LICENSE("GPL");
  253. MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
  254. MODULE_DESCRIPTION("OF Platform driver for NDFC");