ndfc.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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_controller ndfc_control;
  41. };
  42. static struct ndfc_controller ndfc_ctrl[NDFC_MAX_CS];
  43. static void ndfc_select_chip(struct nand_chip *nchip, int chip)
  44. {
  45. uint32_t ccr;
  46. struct ndfc_controller *ndfc = nand_get_controller_data(nchip);
  47. ccr = in_be32(ndfc->ndfcbase + NDFC_CCR);
  48. if (chip >= 0) {
  49. ccr &= ~NDFC_CCR_BS_MASK;
  50. ccr |= NDFC_CCR_BS(chip + ndfc->chip_select);
  51. } else
  52. ccr |= NDFC_CCR_RESET_CE;
  53. out_be32(ndfc->ndfcbase + NDFC_CCR, ccr);
  54. }
  55. static void ndfc_hwcontrol(struct nand_chip *chip, int cmd, unsigned int ctrl)
  56. {
  57. struct ndfc_controller *ndfc = nand_get_controller_data(chip);
  58. if (cmd == NAND_CMD_NONE)
  59. return;
  60. if (ctrl & NAND_CLE)
  61. writel(cmd & 0xFF, ndfc->ndfcbase + NDFC_CMD);
  62. else
  63. writel(cmd & 0xFF, ndfc->ndfcbase + NDFC_ALE);
  64. }
  65. static int ndfc_ready(struct nand_chip *chip)
  66. {
  67. struct ndfc_controller *ndfc = nand_get_controller_data(chip);
  68. return in_be32(ndfc->ndfcbase + NDFC_STAT) & NDFC_STAT_IS_READY;
  69. }
  70. static void ndfc_enable_hwecc(struct nand_chip *chip, int mode)
  71. {
  72. uint32_t ccr;
  73. struct ndfc_controller *ndfc = nand_get_controller_data(chip);
  74. ccr = in_be32(ndfc->ndfcbase + NDFC_CCR);
  75. ccr |= NDFC_CCR_RESET_ECC;
  76. out_be32(ndfc->ndfcbase + NDFC_CCR, ccr);
  77. wmb();
  78. }
  79. static int ndfc_calculate_ecc(struct nand_chip *chip,
  80. const u_char *dat, u_char *ecc_code)
  81. {
  82. struct ndfc_controller *ndfc = nand_get_controller_data(chip);
  83. uint32_t ecc;
  84. uint8_t *p = (uint8_t *)&ecc;
  85. wmb();
  86. ecc = in_be32(ndfc->ndfcbase + NDFC_ECC);
  87. /* The NDFC uses Smart Media (SMC) bytes order */
  88. ecc_code[0] = p[1];
  89. ecc_code[1] = p[2];
  90. ecc_code[2] = p[3];
  91. return 0;
  92. }
  93. /*
  94. * Speedups for buffer read/write/verify
  95. *
  96. * NDFC allows 32bit read/write of data. So we can speed up the buffer
  97. * functions. No further checking, as nand_base will always read/write
  98. * page aligned.
  99. */
  100. static void ndfc_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
  101. {
  102. struct ndfc_controller *ndfc = nand_get_controller_data(chip);
  103. uint32_t *p = (uint32_t *) buf;
  104. for(;len > 0; len -= 4)
  105. *p++ = in_be32(ndfc->ndfcbase + NDFC_DATA);
  106. }
  107. static void ndfc_write_buf(struct nand_chip *chip, const uint8_t *buf, int len)
  108. {
  109. struct ndfc_controller *ndfc = nand_get_controller_data(chip);
  110. uint32_t *p = (uint32_t *) buf;
  111. for(;len > 0; len -= 4)
  112. out_be32(ndfc->ndfcbase + NDFC_DATA, *p++);
  113. }
  114. /*
  115. * Initialize chip structure
  116. */
  117. static int ndfc_chip_init(struct ndfc_controller *ndfc,
  118. struct device_node *node)
  119. {
  120. struct device_node *flash_np;
  121. struct nand_chip *chip = &ndfc->chip;
  122. struct mtd_info *mtd = nand_to_mtd(chip);
  123. int ret;
  124. chip->legacy.IO_ADDR_R = ndfc->ndfcbase + NDFC_DATA;
  125. chip->legacy.IO_ADDR_W = ndfc->ndfcbase + NDFC_DATA;
  126. chip->legacy.cmd_ctrl = ndfc_hwcontrol;
  127. chip->legacy.dev_ready = ndfc_ready;
  128. chip->select_chip = ndfc_select_chip;
  129. chip->legacy.chip_delay = 50;
  130. chip->controller = &ndfc->ndfc_control;
  131. chip->legacy.read_buf = ndfc_read_buf;
  132. chip->legacy.write_buf = ndfc_write_buf;
  133. chip->ecc.correct = nand_correct_data;
  134. chip->ecc.hwctl = ndfc_enable_hwecc;
  135. chip->ecc.calculate = ndfc_calculate_ecc;
  136. chip->ecc.mode = NAND_ECC_HW;
  137. chip->ecc.size = 256;
  138. chip->ecc.bytes = 3;
  139. chip->ecc.strength = 1;
  140. nand_set_controller_data(chip, ndfc);
  141. mtd->dev.parent = &ndfc->ofdev->dev;
  142. flash_np = of_get_next_child(node, NULL);
  143. if (!flash_np)
  144. return -ENODEV;
  145. nand_set_flash_node(chip, flash_np);
  146. mtd->name = kasprintf(GFP_KERNEL, "%s.%pOFn", dev_name(&ndfc->ofdev->dev),
  147. flash_np);
  148. if (!mtd->name) {
  149. ret = -ENOMEM;
  150. goto err;
  151. }
  152. ret = nand_scan(chip, 1);
  153. if (ret)
  154. goto err;
  155. ret = mtd_device_register(mtd, NULL, 0);
  156. err:
  157. of_node_put(flash_np);
  158. if (ret)
  159. kfree(mtd->name);
  160. return ret;
  161. }
  162. static int ndfc_probe(struct platform_device *ofdev)
  163. {
  164. struct ndfc_controller *ndfc;
  165. const __be32 *reg;
  166. u32 ccr;
  167. u32 cs;
  168. int err, len;
  169. /* Read the reg property to get the chip select */
  170. reg = of_get_property(ofdev->dev.of_node, "reg", &len);
  171. if (reg == NULL || len != 12) {
  172. dev_err(&ofdev->dev, "unable read reg property (%d)\n", len);
  173. return -ENOENT;
  174. }
  175. cs = be32_to_cpu(reg[0]);
  176. if (cs >= NDFC_MAX_CS) {
  177. dev_err(&ofdev->dev, "invalid CS number (%d)\n", cs);
  178. return -EINVAL;
  179. }
  180. ndfc = &ndfc_ctrl[cs];
  181. ndfc->chip_select = cs;
  182. nand_controller_init(&ndfc->ndfc_control);
  183. ndfc->ofdev = ofdev;
  184. dev_set_drvdata(&ofdev->dev, ndfc);
  185. ndfc->ndfcbase = of_iomap(ofdev->dev.of_node, 0);
  186. if (!ndfc->ndfcbase) {
  187. dev_err(&ofdev->dev, "failed to get memory\n");
  188. return -EIO;
  189. }
  190. ccr = NDFC_CCR_BS(ndfc->chip_select);
  191. /* It is ok if ccr does not exist - just default to 0 */
  192. reg = of_get_property(ofdev->dev.of_node, "ccr", NULL);
  193. if (reg)
  194. ccr |= be32_to_cpup(reg);
  195. out_be32(ndfc->ndfcbase + NDFC_CCR, ccr);
  196. /* Set the bank settings if given */
  197. reg = of_get_property(ofdev->dev.of_node, "bank-settings", NULL);
  198. if (reg) {
  199. int offset = NDFC_BCFG0 + (ndfc->chip_select << 2);
  200. out_be32(ndfc->ndfcbase + offset, be32_to_cpup(reg));
  201. }
  202. err = ndfc_chip_init(ndfc, ofdev->dev.of_node);
  203. if (err) {
  204. iounmap(ndfc->ndfcbase);
  205. return err;
  206. }
  207. return 0;
  208. }
  209. static int ndfc_remove(struct platform_device *ofdev)
  210. {
  211. struct ndfc_controller *ndfc = dev_get_drvdata(&ofdev->dev);
  212. struct mtd_info *mtd = nand_to_mtd(&ndfc->chip);
  213. nand_release(&ndfc->chip);
  214. kfree(mtd->name);
  215. return 0;
  216. }
  217. static const struct of_device_id ndfc_match[] = {
  218. { .compatible = "ibm,ndfc", },
  219. {}
  220. };
  221. MODULE_DEVICE_TABLE(of, ndfc_match);
  222. static struct platform_driver ndfc_driver = {
  223. .driver = {
  224. .name = "ndfc",
  225. .of_match_table = ndfc_match,
  226. },
  227. .probe = ndfc_probe,
  228. .remove = ndfc_remove,
  229. };
  230. module_platform_driver(ndfc_driver);
  231. MODULE_LICENSE("GPL");
  232. MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
  233. MODULE_DESCRIPTION("OF Platform driver for NDFC");