ndfc.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Overview:
  3. * Platform independent driver for NDFC (NanD Flash Controller)
  4. * integrated into EP440 cores
  5. *
  6. * Ported to an OF platform driver by Sean MacLennan
  7. *
  8. * The NDFC supports multiple chips, but this driver only supports a
  9. * single chip since I do not have access to any boards with
  10. * multiple chips.
  11. *
  12. * Author: Thomas Gleixner
  13. *
  14. * Copyright 2006 IBM
  15. * Copyright 2008 PIKA Technologies
  16. * Sean MacLennan <smaclennan@pikatech.com>
  17. *
  18. * This program is free software; you can redistribute it and/or modify it
  19. * under the terms of the GNU General Public License as published by the
  20. * Free Software Foundation; either version 2 of the License, or (at your
  21. * option) any later version.
  22. *
  23. */
  24. #include <linux/module.h>
  25. #include <linux/mtd/rawnand.h>
  26. #include <linux/mtd/nand_ecc.h>
  27. #include <linux/mtd/partitions.h>
  28. #include <linux/mtd/ndfc.h>
  29. #include <linux/slab.h>
  30. #include <linux/mtd/mtd.h>
  31. #include <linux/of_address.h>
  32. #include <linux/of_platform.h>
  33. #include <asm/io.h>
  34. #define NDFC_MAX_CS 4
  35. struct ndfc_controller {
  36. struct platform_device *ofdev;
  37. void __iomem *ndfcbase;
  38. struct nand_chip chip;
  39. int chip_select;
  40. struct nand_hw_control ndfc_control;
  41. };
  42. static struct ndfc_controller ndfc_ctrl[NDFC_MAX_CS];
  43. static void ndfc_select_chip(struct mtd_info *mtd, int chip)
  44. {
  45. uint32_t ccr;
  46. struct nand_chip *nchip = mtd_to_nand(mtd);
  47. struct ndfc_controller *ndfc = nand_get_controller_data(nchip);
  48. ccr = in_be32(ndfc->ndfcbase + NDFC_CCR);
  49. if (chip >= 0) {
  50. ccr &= ~NDFC_CCR_BS_MASK;
  51. ccr |= NDFC_CCR_BS(chip + ndfc->chip_select);
  52. } else
  53. ccr |= NDFC_CCR_RESET_CE;
  54. out_be32(ndfc->ndfcbase + NDFC_CCR, ccr);
  55. }
  56. static void ndfc_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  57. {
  58. struct nand_chip *chip = mtd_to_nand(mtd);
  59. struct ndfc_controller *ndfc = nand_get_controller_data(chip);
  60. if (cmd == NAND_CMD_NONE)
  61. return;
  62. if (ctrl & NAND_CLE)
  63. writel(cmd & 0xFF, ndfc->ndfcbase + NDFC_CMD);
  64. else
  65. writel(cmd & 0xFF, ndfc->ndfcbase + NDFC_ALE);
  66. }
  67. static int ndfc_ready(struct mtd_info *mtd)
  68. {
  69. struct nand_chip *chip = mtd_to_nand(mtd);
  70. struct ndfc_controller *ndfc = nand_get_controller_data(chip);
  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 nand_chip *chip = mtd_to_nand(mtd);
  77. struct ndfc_controller *ndfc = nand_get_controller_data(chip);
  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 nand_chip *chip = mtd_to_nand(mtd);
  87. struct ndfc_controller *ndfc = nand_get_controller_data(chip);
  88. uint32_t ecc;
  89. uint8_t *p = (uint8_t *)&ecc;
  90. wmb();
  91. ecc = in_be32(ndfc->ndfcbase + NDFC_ECC);
  92. /* The NDFC uses Smart Media (SMC) bytes order */
  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 nand_chip *chip = mtd_to_nand(mtd);
  108. struct ndfc_controller *ndfc = nand_get_controller_data(chip);
  109. uint32_t *p = (uint32_t *) buf;
  110. for(;len > 0; len -= 4)
  111. *p++ = in_be32(ndfc->ndfcbase + NDFC_DATA);
  112. }
  113. static void ndfc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  114. {
  115. struct nand_chip *chip = mtd_to_nand(mtd);
  116. struct ndfc_controller *ndfc = nand_get_controller_data(chip);
  117. uint32_t *p = (uint32_t *) buf;
  118. for(;len > 0; len -= 4)
  119. out_be32(ndfc->ndfcbase + NDFC_DATA, *p++);
  120. }
  121. /*
  122. * Initialize chip structure
  123. */
  124. static int ndfc_chip_init(struct ndfc_controller *ndfc,
  125. struct device_node *node)
  126. {
  127. struct device_node *flash_np;
  128. struct nand_chip *chip = &ndfc->chip;
  129. struct mtd_info *mtd = nand_to_mtd(chip);
  130. int ret;
  131. chip->IO_ADDR_R = ndfc->ndfcbase + NDFC_DATA;
  132. chip->IO_ADDR_W = ndfc->ndfcbase + NDFC_DATA;
  133. chip->cmd_ctrl = ndfc_hwcontrol;
  134. chip->dev_ready = ndfc_ready;
  135. chip->select_chip = ndfc_select_chip;
  136. chip->chip_delay = 50;
  137. chip->controller = &ndfc->ndfc_control;
  138. chip->read_buf = ndfc_read_buf;
  139. chip->write_buf = ndfc_write_buf;
  140. chip->ecc.correct = nand_correct_data;
  141. chip->ecc.hwctl = ndfc_enable_hwecc;
  142. chip->ecc.calculate = ndfc_calculate_ecc;
  143. chip->ecc.mode = NAND_ECC_HW;
  144. chip->ecc.size = 256;
  145. chip->ecc.bytes = 3;
  146. chip->ecc.strength = 1;
  147. nand_set_controller_data(chip, ndfc);
  148. mtd->dev.parent = &ndfc->ofdev->dev;
  149. flash_np = of_get_next_child(node, NULL);
  150. if (!flash_np)
  151. return -ENODEV;
  152. nand_set_flash_node(chip, flash_np);
  153. mtd->name = kasprintf(GFP_KERNEL, "%s.%s", dev_name(&ndfc->ofdev->dev),
  154. flash_np->name);
  155. if (!mtd->name) {
  156. ret = -ENOMEM;
  157. goto err;
  158. }
  159. ret = nand_scan(mtd, 1);
  160. if (ret)
  161. goto err;
  162. ret = mtd_device_register(mtd, NULL, 0);
  163. err:
  164. of_node_put(flash_np);
  165. if (ret)
  166. kfree(mtd->name);
  167. return ret;
  168. }
  169. static int ndfc_probe(struct platform_device *ofdev)
  170. {
  171. struct ndfc_controller *ndfc;
  172. const __be32 *reg;
  173. u32 ccr;
  174. u32 cs;
  175. int err, len;
  176. /* Read the reg property to get the chip select */
  177. reg = of_get_property(ofdev->dev.of_node, "reg", &len);
  178. if (reg == NULL || len != 12) {
  179. dev_err(&ofdev->dev, "unable read reg property (%d)\n", len);
  180. return -ENOENT;
  181. }
  182. cs = be32_to_cpu(reg[0]);
  183. if (cs >= NDFC_MAX_CS) {
  184. dev_err(&ofdev->dev, "invalid CS number (%d)\n", cs);
  185. return -EINVAL;
  186. }
  187. ndfc = &ndfc_ctrl[cs];
  188. ndfc->chip_select = cs;
  189. nand_hw_control_init(&ndfc->ndfc_control);
  190. ndfc->ofdev = ofdev;
  191. dev_set_drvdata(&ofdev->dev, ndfc);
  192. ndfc->ndfcbase = of_iomap(ofdev->dev.of_node, 0);
  193. if (!ndfc->ndfcbase) {
  194. dev_err(&ofdev->dev, "failed to get memory\n");
  195. return -EIO;
  196. }
  197. ccr = NDFC_CCR_BS(ndfc->chip_select);
  198. /* It is ok if ccr does not exist - just default to 0 */
  199. reg = of_get_property(ofdev->dev.of_node, "ccr", NULL);
  200. if (reg)
  201. ccr |= be32_to_cpup(reg);
  202. out_be32(ndfc->ndfcbase + NDFC_CCR, ccr);
  203. /* Set the bank settings if given */
  204. reg = of_get_property(ofdev->dev.of_node, "bank-settings", NULL);
  205. if (reg) {
  206. int offset = NDFC_BCFG0 + (ndfc->chip_select << 2);
  207. out_be32(ndfc->ndfcbase + offset, be32_to_cpup(reg));
  208. }
  209. err = ndfc_chip_init(ndfc, ofdev->dev.of_node);
  210. if (err) {
  211. iounmap(ndfc->ndfcbase);
  212. return err;
  213. }
  214. return 0;
  215. }
  216. static int ndfc_remove(struct platform_device *ofdev)
  217. {
  218. struct ndfc_controller *ndfc = dev_get_drvdata(&ofdev->dev);
  219. struct mtd_info *mtd = nand_to_mtd(&ndfc->chip);
  220. nand_release(&ndfc->chip);
  221. kfree(mtd->name);
  222. return 0;
  223. }
  224. static const struct of_device_id ndfc_match[] = {
  225. { .compatible = "ibm,ndfc", },
  226. {}
  227. };
  228. MODULE_DEVICE_TABLE(of, ndfc_match);
  229. static struct platform_driver ndfc_driver = {
  230. .driver = {
  231. .name = "ndfc",
  232. .of_match_table = ndfc_match,
  233. },
  234. .probe = ndfc_probe,
  235. .remove = ndfc_remove,
  236. };
  237. module_platform_driver(ndfc_driver);
  238. MODULE_LICENSE("GPL");
  239. MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
  240. MODULE_DESCRIPTION("OF Platform driver for NDFC");