jz4780_nand.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * JZ4780 NAND driver
  3. *
  4. * Copyright (c) 2015 Imagination Technologies
  5. * Author: Alex Smith <alex.smith@imgtec.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published
  9. * by the Free Software Foundation.
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/init.h>
  13. #include <linux/io.h>
  14. #include <linux/list.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <linux/mtd/mtd.h>
  22. #include <linux/mtd/nand.h>
  23. #include <linux/mtd/partitions.h>
  24. #include <linux/jz4780-nemc.h>
  25. #include "jz4780_bch.h"
  26. #define DRV_NAME "jz4780-nand"
  27. #define OFFSET_DATA 0x00000000
  28. #define OFFSET_CMD 0x00400000
  29. #define OFFSET_ADDR 0x00800000
  30. /* Command delay when there is no R/B pin. */
  31. #define RB_DELAY_US 100
  32. struct jz4780_nand_cs {
  33. unsigned int bank;
  34. void __iomem *base;
  35. };
  36. struct jz4780_nand_controller {
  37. struct device *dev;
  38. struct jz4780_bch *bch;
  39. struct nand_hw_control controller;
  40. unsigned int num_banks;
  41. struct list_head chips;
  42. int selected;
  43. struct jz4780_nand_cs cs[];
  44. };
  45. struct jz4780_nand_chip {
  46. struct nand_chip chip;
  47. struct list_head chip_list;
  48. struct gpio_desc *busy_gpio;
  49. struct gpio_desc *wp_gpio;
  50. unsigned int reading: 1;
  51. };
  52. static inline struct jz4780_nand_chip *to_jz4780_nand_chip(struct mtd_info *mtd)
  53. {
  54. return container_of(mtd_to_nand(mtd), struct jz4780_nand_chip, chip);
  55. }
  56. static inline struct jz4780_nand_controller *to_jz4780_nand_controller(struct nand_hw_control *ctrl)
  57. {
  58. return container_of(ctrl, struct jz4780_nand_controller, controller);
  59. }
  60. static void jz4780_nand_select_chip(struct mtd_info *mtd, int chipnr)
  61. {
  62. struct jz4780_nand_chip *nand = to_jz4780_nand_chip(mtd);
  63. struct jz4780_nand_controller *nfc = to_jz4780_nand_controller(nand->chip.controller);
  64. struct jz4780_nand_cs *cs;
  65. /* Ensure the currently selected chip is deasserted. */
  66. if (chipnr == -1 && nfc->selected >= 0) {
  67. cs = &nfc->cs[nfc->selected];
  68. jz4780_nemc_assert(nfc->dev, cs->bank, false);
  69. }
  70. nfc->selected = chipnr;
  71. }
  72. static void jz4780_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
  73. unsigned int ctrl)
  74. {
  75. struct jz4780_nand_chip *nand = to_jz4780_nand_chip(mtd);
  76. struct jz4780_nand_controller *nfc = to_jz4780_nand_controller(nand->chip.controller);
  77. struct jz4780_nand_cs *cs;
  78. if (WARN_ON(nfc->selected < 0))
  79. return;
  80. cs = &nfc->cs[nfc->selected];
  81. jz4780_nemc_assert(nfc->dev, cs->bank, ctrl & NAND_NCE);
  82. if (cmd == NAND_CMD_NONE)
  83. return;
  84. if (ctrl & NAND_ALE)
  85. writeb(cmd, cs->base + OFFSET_ADDR);
  86. else if (ctrl & NAND_CLE)
  87. writeb(cmd, cs->base + OFFSET_CMD);
  88. }
  89. static int jz4780_nand_dev_ready(struct mtd_info *mtd)
  90. {
  91. struct jz4780_nand_chip *nand = to_jz4780_nand_chip(mtd);
  92. return !gpiod_get_value_cansleep(nand->busy_gpio);
  93. }
  94. static void jz4780_nand_ecc_hwctl(struct mtd_info *mtd, int mode)
  95. {
  96. struct jz4780_nand_chip *nand = to_jz4780_nand_chip(mtd);
  97. nand->reading = (mode == NAND_ECC_READ);
  98. }
  99. static int jz4780_nand_ecc_calculate(struct mtd_info *mtd, const u8 *dat,
  100. u8 *ecc_code)
  101. {
  102. struct jz4780_nand_chip *nand = to_jz4780_nand_chip(mtd);
  103. struct jz4780_nand_controller *nfc = to_jz4780_nand_controller(nand->chip.controller);
  104. struct jz4780_bch_params params;
  105. /*
  106. * Don't need to generate the ECC when reading, BCH does it for us as
  107. * part of decoding/correction.
  108. */
  109. if (nand->reading)
  110. return 0;
  111. params.size = nand->chip.ecc.size;
  112. params.bytes = nand->chip.ecc.bytes;
  113. params.strength = nand->chip.ecc.strength;
  114. return jz4780_bch_calculate(nfc->bch, &params, dat, ecc_code);
  115. }
  116. static int jz4780_nand_ecc_correct(struct mtd_info *mtd, u8 *dat,
  117. u8 *read_ecc, u8 *calc_ecc)
  118. {
  119. struct jz4780_nand_chip *nand = to_jz4780_nand_chip(mtd);
  120. struct jz4780_nand_controller *nfc = to_jz4780_nand_controller(nand->chip.controller);
  121. struct jz4780_bch_params params;
  122. params.size = nand->chip.ecc.size;
  123. params.bytes = nand->chip.ecc.bytes;
  124. params.strength = nand->chip.ecc.strength;
  125. return jz4780_bch_correct(nfc->bch, &params, dat, read_ecc);
  126. }
  127. static int jz4780_nand_init_ecc(struct jz4780_nand_chip *nand, struct device *dev)
  128. {
  129. struct nand_chip *chip = &nand->chip;
  130. struct mtd_info *mtd = nand_to_mtd(chip);
  131. struct jz4780_nand_controller *nfc = to_jz4780_nand_controller(chip->controller);
  132. int eccbytes;
  133. chip->ecc.bytes = fls((1 + 8) * chip->ecc.size) *
  134. (chip->ecc.strength / 8);
  135. switch (chip->ecc.mode) {
  136. case NAND_ECC_HW:
  137. if (!nfc->bch) {
  138. dev_err(dev, "HW BCH selected, but BCH controller not found\n");
  139. return -ENODEV;
  140. }
  141. chip->ecc.hwctl = jz4780_nand_ecc_hwctl;
  142. chip->ecc.calculate = jz4780_nand_ecc_calculate;
  143. chip->ecc.correct = jz4780_nand_ecc_correct;
  144. /* fall through */
  145. case NAND_ECC_SOFT:
  146. dev_info(dev, "using %s (strength %d, size %d, bytes %d)\n",
  147. (nfc->bch) ? "hardware BCH" : "software ECC",
  148. chip->ecc.strength, chip->ecc.size, chip->ecc.bytes);
  149. break;
  150. case NAND_ECC_NONE:
  151. dev_info(dev, "not using ECC\n");
  152. break;
  153. default:
  154. dev_err(dev, "ECC mode %d not supported\n", chip->ecc.mode);
  155. return -EINVAL;
  156. }
  157. /* The NAND core will generate the ECC layout for SW ECC */
  158. if (chip->ecc.mode != NAND_ECC_HW)
  159. return 0;
  160. /* Generate ECC layout. ECC codes are right aligned in the OOB area. */
  161. eccbytes = mtd->writesize / chip->ecc.size * chip->ecc.bytes;
  162. if (eccbytes > mtd->oobsize - 2) {
  163. dev_err(dev,
  164. "invalid ECC config: required %d ECC bytes, but only %d are available",
  165. eccbytes, mtd->oobsize - 2);
  166. return -EINVAL;
  167. }
  168. mtd->ooblayout = &nand_ooblayout_lp_ops;
  169. return 0;
  170. }
  171. static int jz4780_nand_init_chip(struct platform_device *pdev,
  172. struct jz4780_nand_controller *nfc,
  173. struct device_node *np,
  174. unsigned int chipnr)
  175. {
  176. struct device *dev = &pdev->dev;
  177. struct jz4780_nand_chip *nand;
  178. struct jz4780_nand_cs *cs;
  179. struct resource *res;
  180. struct nand_chip *chip;
  181. struct mtd_info *mtd;
  182. const __be32 *reg;
  183. int ret = 0;
  184. cs = &nfc->cs[chipnr];
  185. reg = of_get_property(np, "reg", NULL);
  186. if (!reg)
  187. return -EINVAL;
  188. cs->bank = be32_to_cpu(*reg);
  189. jz4780_nemc_set_type(nfc->dev, cs->bank, JZ4780_NEMC_BANK_NAND);
  190. res = platform_get_resource(pdev, IORESOURCE_MEM, chipnr);
  191. cs->base = devm_ioremap_resource(dev, res);
  192. if (IS_ERR(cs->base))
  193. return PTR_ERR(cs->base);
  194. nand = devm_kzalloc(dev, sizeof(*nand), GFP_KERNEL);
  195. if (!nand)
  196. return -ENOMEM;
  197. nand->busy_gpio = devm_gpiod_get_optional(dev, "rb", GPIOD_IN);
  198. if (IS_ERR(nand->busy_gpio)) {
  199. ret = PTR_ERR(nand->busy_gpio);
  200. dev_err(dev, "failed to request busy GPIO: %d\n", ret);
  201. return ret;
  202. } else if (nand->busy_gpio) {
  203. nand->chip.dev_ready = jz4780_nand_dev_ready;
  204. }
  205. nand->wp_gpio = devm_gpiod_get_optional(dev, "wp", GPIOD_OUT_LOW);
  206. if (IS_ERR(nand->wp_gpio)) {
  207. ret = PTR_ERR(nand->wp_gpio);
  208. dev_err(dev, "failed to request WP GPIO: %d\n", ret);
  209. return ret;
  210. }
  211. chip = &nand->chip;
  212. mtd = nand_to_mtd(chip);
  213. mtd->name = devm_kasprintf(dev, GFP_KERNEL, "%s.%d", dev_name(dev),
  214. cs->bank);
  215. if (!mtd->name)
  216. return -ENOMEM;
  217. mtd->dev.parent = dev;
  218. chip->IO_ADDR_R = cs->base + OFFSET_DATA;
  219. chip->IO_ADDR_W = cs->base + OFFSET_DATA;
  220. chip->chip_delay = RB_DELAY_US;
  221. chip->options = NAND_NO_SUBPAGE_WRITE;
  222. chip->select_chip = jz4780_nand_select_chip;
  223. chip->cmd_ctrl = jz4780_nand_cmd_ctrl;
  224. chip->ecc.mode = NAND_ECC_HW;
  225. chip->controller = &nfc->controller;
  226. nand_set_flash_node(chip, np);
  227. ret = nand_scan_ident(mtd, 1, NULL);
  228. if (ret)
  229. return ret;
  230. ret = jz4780_nand_init_ecc(nand, dev);
  231. if (ret)
  232. return ret;
  233. ret = nand_scan_tail(mtd);
  234. if (ret)
  235. return ret;
  236. ret = mtd_device_register(mtd, NULL, 0);
  237. if (ret) {
  238. nand_release(mtd);
  239. return ret;
  240. }
  241. list_add_tail(&nand->chip_list, &nfc->chips);
  242. return 0;
  243. }
  244. static void jz4780_nand_cleanup_chips(struct jz4780_nand_controller *nfc)
  245. {
  246. struct jz4780_nand_chip *chip;
  247. while (!list_empty(&nfc->chips)) {
  248. chip = list_first_entry(&nfc->chips, struct jz4780_nand_chip, chip_list);
  249. nand_release(nand_to_mtd(&chip->chip));
  250. list_del(&chip->chip_list);
  251. }
  252. }
  253. static int jz4780_nand_init_chips(struct jz4780_nand_controller *nfc,
  254. struct platform_device *pdev)
  255. {
  256. struct device *dev = &pdev->dev;
  257. struct device_node *np;
  258. int i = 0;
  259. int ret;
  260. int num_chips = of_get_child_count(dev->of_node);
  261. if (num_chips > nfc->num_banks) {
  262. dev_err(dev, "found %d chips but only %d banks\n", num_chips, nfc->num_banks);
  263. return -EINVAL;
  264. }
  265. for_each_child_of_node(dev->of_node, np) {
  266. ret = jz4780_nand_init_chip(pdev, nfc, np, i);
  267. if (ret) {
  268. jz4780_nand_cleanup_chips(nfc);
  269. return ret;
  270. }
  271. i++;
  272. }
  273. return 0;
  274. }
  275. static int jz4780_nand_probe(struct platform_device *pdev)
  276. {
  277. struct device *dev = &pdev->dev;
  278. unsigned int num_banks;
  279. struct jz4780_nand_controller *nfc;
  280. int ret;
  281. num_banks = jz4780_nemc_num_banks(dev);
  282. if (num_banks == 0) {
  283. dev_err(dev, "no banks found\n");
  284. return -ENODEV;
  285. }
  286. nfc = devm_kzalloc(dev, sizeof(*nfc) + (sizeof(nfc->cs[0]) * num_banks), GFP_KERNEL);
  287. if (!nfc)
  288. return -ENOMEM;
  289. /*
  290. * Check for BCH HW before we call nand_scan_ident, to prevent us from
  291. * having to call it again if the BCH driver returns -EPROBE_DEFER.
  292. */
  293. nfc->bch = of_jz4780_bch_get(dev->of_node);
  294. if (IS_ERR(nfc->bch))
  295. return PTR_ERR(nfc->bch);
  296. nfc->dev = dev;
  297. nfc->num_banks = num_banks;
  298. nand_hw_control_init(&nfc->controller);
  299. INIT_LIST_HEAD(&nfc->chips);
  300. ret = jz4780_nand_init_chips(nfc, pdev);
  301. if (ret) {
  302. if (nfc->bch)
  303. jz4780_bch_release(nfc->bch);
  304. return ret;
  305. }
  306. platform_set_drvdata(pdev, nfc);
  307. return 0;
  308. }
  309. static int jz4780_nand_remove(struct platform_device *pdev)
  310. {
  311. struct jz4780_nand_controller *nfc = platform_get_drvdata(pdev);
  312. if (nfc->bch)
  313. jz4780_bch_release(nfc->bch);
  314. jz4780_nand_cleanup_chips(nfc);
  315. return 0;
  316. }
  317. static const struct of_device_id jz4780_nand_dt_match[] = {
  318. { .compatible = "ingenic,jz4780-nand" },
  319. {},
  320. };
  321. MODULE_DEVICE_TABLE(of, jz4780_nand_dt_match);
  322. static struct platform_driver jz4780_nand_driver = {
  323. .probe = jz4780_nand_probe,
  324. .remove = jz4780_nand_remove,
  325. .driver = {
  326. .name = DRV_NAME,
  327. .of_match_table = of_match_ptr(jz4780_nand_dt_match),
  328. },
  329. };
  330. module_platform_driver(jz4780_nand_driver);
  331. MODULE_AUTHOR("Alex Smith <alex@alex-smith.me.uk>");
  332. MODULE_AUTHOR("Harvey Hunt <harveyhuntnexus@gmail.com>");
  333. MODULE_DESCRIPTION("Ingenic JZ4780 NAND driver");
  334. MODULE_LICENSE("GPL v2");