ndfc.c 6.9 KB

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