ingenic_nand_drv.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Ingenic JZ47xx NAND driver
  4. *
  5. * Copyright (c) 2015 Imagination Technologies
  6. * Author: Alex Smith <alex.smith@imgtec.com>
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/init.h>
  10. #include <linux/io.h>
  11. #include <linux/list.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/of_address.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/rawnand.h>
  20. #include <linux/mtd/partitions.h>
  21. #include <linux/jz4780-nemc.h>
  22. #include "ingenic_ecc.h"
  23. #define DRV_NAME "ingenic-nand"
  24. struct jz_soc_info {
  25. unsigned long data_offset;
  26. unsigned long addr_offset;
  27. unsigned long cmd_offset;
  28. const struct mtd_ooblayout_ops *oob_layout;
  29. bool oob_first;
  30. };
  31. struct ingenic_nand_cs {
  32. unsigned int bank;
  33. void __iomem *base;
  34. };
  35. struct ingenic_nfc {
  36. struct device *dev;
  37. struct ingenic_ecc *ecc;
  38. const struct jz_soc_info *soc_info;
  39. struct nand_controller controller;
  40. unsigned int num_banks;
  41. struct list_head chips;
  42. struct ingenic_nand_cs cs[] __counted_by(num_banks);
  43. };
  44. struct ingenic_nand {
  45. struct nand_chip chip;
  46. struct list_head chip_list;
  47. struct gpio_desc *busy_gpio;
  48. struct gpio_desc *wp_gpio;
  49. unsigned int reading: 1;
  50. };
  51. static inline struct ingenic_nand *to_ingenic_nand(struct mtd_info *mtd)
  52. {
  53. return container_of(mtd_to_nand(mtd), struct ingenic_nand, chip);
  54. }
  55. static inline struct ingenic_nfc *to_ingenic_nfc(struct nand_controller *ctrl)
  56. {
  57. return container_of(ctrl, struct ingenic_nfc, controller);
  58. }
  59. static int qi_lb60_ooblayout_ecc(struct mtd_info *mtd, int section,
  60. struct mtd_oob_region *oobregion)
  61. {
  62. struct nand_chip *chip = mtd_to_nand(mtd);
  63. struct nand_ecc_ctrl *ecc = &chip->ecc;
  64. if (section || !ecc->total)
  65. return -ERANGE;
  66. oobregion->length = ecc->total;
  67. oobregion->offset = 12;
  68. return 0;
  69. }
  70. static int qi_lb60_ooblayout_free(struct mtd_info *mtd, int section,
  71. struct mtd_oob_region *oobregion)
  72. {
  73. struct nand_chip *chip = mtd_to_nand(mtd);
  74. struct nand_ecc_ctrl *ecc = &chip->ecc;
  75. if (section)
  76. return -ERANGE;
  77. oobregion->length = mtd->oobsize - ecc->total - 12;
  78. oobregion->offset = 12 + ecc->total;
  79. return 0;
  80. }
  81. static const struct mtd_ooblayout_ops qi_lb60_ooblayout_ops = {
  82. .ecc = qi_lb60_ooblayout_ecc,
  83. .free = qi_lb60_ooblayout_free,
  84. };
  85. static int jz4725b_ooblayout_ecc(struct mtd_info *mtd, int section,
  86. struct mtd_oob_region *oobregion)
  87. {
  88. struct nand_chip *chip = mtd_to_nand(mtd);
  89. struct nand_ecc_ctrl *ecc = &chip->ecc;
  90. if (section || !ecc->total)
  91. return -ERANGE;
  92. oobregion->length = ecc->total;
  93. oobregion->offset = 3;
  94. return 0;
  95. }
  96. static int jz4725b_ooblayout_free(struct mtd_info *mtd, int section,
  97. struct mtd_oob_region *oobregion)
  98. {
  99. struct nand_chip *chip = mtd_to_nand(mtd);
  100. struct nand_ecc_ctrl *ecc = &chip->ecc;
  101. if (section)
  102. return -ERANGE;
  103. oobregion->length = mtd->oobsize - ecc->total - 3;
  104. oobregion->offset = 3 + ecc->total;
  105. return 0;
  106. }
  107. static const struct mtd_ooblayout_ops jz4725b_ooblayout_ops = {
  108. .ecc = jz4725b_ooblayout_ecc,
  109. .free = jz4725b_ooblayout_free,
  110. };
  111. static void ingenic_nand_ecc_hwctl(struct nand_chip *chip, int mode)
  112. {
  113. struct ingenic_nand *nand = to_ingenic_nand(nand_to_mtd(chip));
  114. nand->reading = (mode == NAND_ECC_READ);
  115. }
  116. static int ingenic_nand_ecc_calculate(struct nand_chip *chip, const u8 *dat,
  117. u8 *ecc_code)
  118. {
  119. struct ingenic_nand *nand = to_ingenic_nand(nand_to_mtd(chip));
  120. struct ingenic_nfc *nfc = to_ingenic_nfc(nand->chip.controller);
  121. struct ingenic_ecc_params params;
  122. /*
  123. * Don't need to generate the ECC when reading, the ECC engine does it
  124. * for us as part of decoding/correction.
  125. */
  126. if (nand->reading)
  127. return 0;
  128. params.size = nand->chip.ecc.size;
  129. params.bytes = nand->chip.ecc.bytes;
  130. params.strength = nand->chip.ecc.strength;
  131. return ingenic_ecc_calculate(nfc->ecc, &params, dat, ecc_code);
  132. }
  133. static int ingenic_nand_ecc_correct(struct nand_chip *chip, u8 *dat,
  134. u8 *read_ecc, u8 *calc_ecc)
  135. {
  136. struct ingenic_nand *nand = to_ingenic_nand(nand_to_mtd(chip));
  137. struct ingenic_nfc *nfc = to_ingenic_nfc(nand->chip.controller);
  138. struct ingenic_ecc_params params;
  139. params.size = nand->chip.ecc.size;
  140. params.bytes = nand->chip.ecc.bytes;
  141. params.strength = nand->chip.ecc.strength;
  142. return ingenic_ecc_correct(nfc->ecc, &params, dat, read_ecc);
  143. }
  144. static int ingenic_nand_attach_chip(struct nand_chip *chip)
  145. {
  146. struct mtd_info *mtd = nand_to_mtd(chip);
  147. struct ingenic_nfc *nfc = to_ingenic_nfc(chip->controller);
  148. int eccbytes;
  149. if (chip->ecc.strength == 4) {
  150. /* JZ4740 uses 9 bytes of ECC to correct maximum 4 errors */
  151. chip->ecc.bytes = 9;
  152. } else {
  153. chip->ecc.bytes = fls((1 + 8) * chip->ecc.size) *
  154. (chip->ecc.strength / 8);
  155. }
  156. switch (chip->ecc.engine_type) {
  157. case NAND_ECC_ENGINE_TYPE_ON_HOST:
  158. if (!nfc->ecc) {
  159. dev_err(nfc->dev, "HW ECC selected, but ECC controller not found\n");
  160. return -ENODEV;
  161. }
  162. chip->ecc.hwctl = ingenic_nand_ecc_hwctl;
  163. chip->ecc.calculate = ingenic_nand_ecc_calculate;
  164. chip->ecc.correct = ingenic_nand_ecc_correct;
  165. fallthrough;
  166. case NAND_ECC_ENGINE_TYPE_SOFT:
  167. dev_info(nfc->dev, "using %s (strength %d, size %d, bytes %d)\n",
  168. (nfc->ecc) ? "hardware ECC" : "software ECC",
  169. chip->ecc.strength, chip->ecc.size, chip->ecc.bytes);
  170. break;
  171. case NAND_ECC_ENGINE_TYPE_NONE:
  172. dev_info(nfc->dev, "not using ECC\n");
  173. break;
  174. default:
  175. dev_err(nfc->dev, "ECC mode %d not supported\n",
  176. chip->ecc.engine_type);
  177. return -EINVAL;
  178. }
  179. /* The NAND core will generate the ECC layout for SW ECC */
  180. if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST)
  181. return 0;
  182. /* Generate ECC layout. ECC codes are right aligned in the OOB area. */
  183. eccbytes = mtd->writesize / chip->ecc.size * chip->ecc.bytes;
  184. if (eccbytes > mtd->oobsize - 2) {
  185. dev_err(nfc->dev,
  186. "invalid ECC config: required %d ECC bytes, but only %d are available",
  187. eccbytes, mtd->oobsize - 2);
  188. return -EINVAL;
  189. }
  190. /*
  191. * The generic layout for BBT markers will most likely overlap with our
  192. * ECC bytes in the OOB, so move the BBT markers outside the OOB area.
  193. */
  194. if (chip->bbt_options & NAND_BBT_USE_FLASH)
  195. chip->bbt_options |= NAND_BBT_NO_OOB;
  196. if (nfc->soc_info->oob_first)
  197. chip->ecc.read_page = nand_read_page_hwecc_oob_first;
  198. /* For legacy reasons we use a different layout on the qi,lb60 board. */
  199. if (of_machine_is_compatible("qi,lb60"))
  200. mtd_set_ooblayout(mtd, &qi_lb60_ooblayout_ops);
  201. else if (nfc->soc_info->oob_layout)
  202. mtd_set_ooblayout(mtd, nfc->soc_info->oob_layout);
  203. else
  204. mtd_set_ooblayout(mtd, nand_get_large_page_ooblayout());
  205. return 0;
  206. }
  207. static int ingenic_nand_exec_instr(struct nand_chip *chip,
  208. struct ingenic_nand_cs *cs,
  209. const struct nand_op_instr *instr)
  210. {
  211. struct ingenic_nand *nand = to_ingenic_nand(nand_to_mtd(chip));
  212. struct ingenic_nfc *nfc = to_ingenic_nfc(chip->controller);
  213. unsigned int i;
  214. switch (instr->type) {
  215. case NAND_OP_CMD_INSTR:
  216. writeb(instr->ctx.cmd.opcode,
  217. cs->base + nfc->soc_info->cmd_offset);
  218. return 0;
  219. case NAND_OP_ADDR_INSTR:
  220. for (i = 0; i < instr->ctx.addr.naddrs; i++)
  221. writeb(instr->ctx.addr.addrs[i],
  222. cs->base + nfc->soc_info->addr_offset);
  223. return 0;
  224. case NAND_OP_DATA_IN_INSTR:
  225. if (instr->ctx.data.force_8bit ||
  226. !(chip->options & NAND_BUSWIDTH_16))
  227. ioread8_rep(cs->base + nfc->soc_info->data_offset,
  228. instr->ctx.data.buf.in,
  229. instr->ctx.data.len);
  230. else
  231. ioread16_rep(cs->base + nfc->soc_info->data_offset,
  232. instr->ctx.data.buf.in,
  233. instr->ctx.data.len);
  234. return 0;
  235. case NAND_OP_DATA_OUT_INSTR:
  236. if (instr->ctx.data.force_8bit ||
  237. !(chip->options & NAND_BUSWIDTH_16))
  238. iowrite8_rep(cs->base + nfc->soc_info->data_offset,
  239. instr->ctx.data.buf.out,
  240. instr->ctx.data.len);
  241. else
  242. iowrite16_rep(cs->base + nfc->soc_info->data_offset,
  243. instr->ctx.data.buf.out,
  244. instr->ctx.data.len);
  245. return 0;
  246. case NAND_OP_WAITRDY_INSTR:
  247. if (!nand->busy_gpio)
  248. return nand_soft_waitrdy(chip,
  249. instr->ctx.waitrdy.timeout_ms);
  250. return nand_gpio_waitrdy(chip, nand->busy_gpio,
  251. instr->ctx.waitrdy.timeout_ms);
  252. default:
  253. break;
  254. }
  255. return -EINVAL;
  256. }
  257. static int ingenic_nand_exec_op(struct nand_chip *chip,
  258. const struct nand_operation *op,
  259. bool check_only)
  260. {
  261. struct ingenic_nand *nand = to_ingenic_nand(nand_to_mtd(chip));
  262. struct ingenic_nfc *nfc = to_ingenic_nfc(nand->chip.controller);
  263. struct ingenic_nand_cs *cs;
  264. unsigned int i;
  265. int ret = 0;
  266. if (check_only)
  267. return 0;
  268. cs = &nfc->cs[op->cs];
  269. jz4780_nemc_assert(nfc->dev, cs->bank, true);
  270. for (i = 0; i < op->ninstrs; i++) {
  271. ret = ingenic_nand_exec_instr(chip, cs, &op->instrs[i]);
  272. if (ret)
  273. break;
  274. if (op->instrs[i].delay_ns)
  275. ndelay(op->instrs[i].delay_ns);
  276. }
  277. jz4780_nemc_assert(nfc->dev, cs->bank, false);
  278. return ret;
  279. }
  280. static const struct nand_controller_ops ingenic_nand_controller_ops = {
  281. .attach_chip = ingenic_nand_attach_chip,
  282. .exec_op = ingenic_nand_exec_op,
  283. };
  284. static int ingenic_nand_init_chip(struct platform_device *pdev,
  285. struct ingenic_nfc *nfc,
  286. struct device_node *np,
  287. unsigned int chipnr)
  288. {
  289. struct device *dev = &pdev->dev;
  290. struct ingenic_nand *nand;
  291. struct ingenic_nand_cs *cs;
  292. struct nand_chip *chip;
  293. struct mtd_info *mtd;
  294. const __be32 *reg;
  295. int ret = 0;
  296. cs = &nfc->cs[chipnr];
  297. reg = of_get_property(np, "reg", NULL);
  298. if (!reg)
  299. return -EINVAL;
  300. cs->bank = be32_to_cpu(*reg);
  301. jz4780_nemc_set_type(nfc->dev, cs->bank, JZ4780_NEMC_BANK_NAND);
  302. cs->base = devm_platform_ioremap_resource(pdev, chipnr);
  303. if (IS_ERR(cs->base))
  304. return PTR_ERR(cs->base);
  305. nand = devm_kzalloc(dev, sizeof(*nand), GFP_KERNEL);
  306. if (!nand)
  307. return -ENOMEM;
  308. nand->busy_gpio = devm_gpiod_get_optional(dev, "rb", GPIOD_IN);
  309. if (IS_ERR(nand->busy_gpio)) {
  310. ret = PTR_ERR(nand->busy_gpio);
  311. dev_err(dev, "failed to request busy GPIO: %d\n", ret);
  312. return ret;
  313. }
  314. nand->wp_gpio = devm_gpiod_get_optional(dev, "wp", GPIOD_OUT_LOW);
  315. if (IS_ERR(nand->wp_gpio)) {
  316. ret = PTR_ERR(nand->wp_gpio);
  317. dev_err(dev, "failed to request WP GPIO: %d\n", ret);
  318. return ret;
  319. }
  320. chip = &nand->chip;
  321. mtd = nand_to_mtd(chip);
  322. mtd->name = devm_kasprintf(dev, GFP_KERNEL, "%s.%d", dev_name(dev),
  323. cs->bank);
  324. if (!mtd->name)
  325. return -ENOMEM;
  326. mtd->dev.parent = dev;
  327. chip->options = NAND_NO_SUBPAGE_WRITE;
  328. chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
  329. chip->controller = &nfc->controller;
  330. nand_set_flash_node(chip, np);
  331. chip->controller->ops = &ingenic_nand_controller_ops;
  332. ret = nand_scan(chip, 1);
  333. if (ret)
  334. return ret;
  335. ret = mtd_device_register(mtd, NULL, 0);
  336. if (ret) {
  337. nand_cleanup(chip);
  338. return ret;
  339. }
  340. list_add_tail(&nand->chip_list, &nfc->chips);
  341. return 0;
  342. }
  343. static void ingenic_nand_cleanup_chips(struct ingenic_nfc *nfc)
  344. {
  345. struct ingenic_nand *ingenic_chip;
  346. struct nand_chip *chip;
  347. int ret;
  348. while (!list_empty(&nfc->chips)) {
  349. ingenic_chip = list_first_entry(&nfc->chips,
  350. struct ingenic_nand, chip_list);
  351. chip = &ingenic_chip->chip;
  352. ret = mtd_device_unregister(nand_to_mtd(chip));
  353. WARN_ON(ret);
  354. nand_cleanup(chip);
  355. list_del(&ingenic_chip->chip_list);
  356. }
  357. }
  358. static int ingenic_nand_init_chips(struct ingenic_nfc *nfc,
  359. struct platform_device *pdev)
  360. {
  361. struct device *dev = &pdev->dev;
  362. struct device_node *np;
  363. int i = 0;
  364. int ret;
  365. int num_chips = of_get_child_count(dev->of_node);
  366. if (num_chips > nfc->num_banks) {
  367. dev_err(dev, "found %d chips but only %d banks\n",
  368. num_chips, nfc->num_banks);
  369. return -EINVAL;
  370. }
  371. for_each_child_of_node(dev->of_node, np) {
  372. ret = ingenic_nand_init_chip(pdev, nfc, np, i);
  373. if (ret) {
  374. ingenic_nand_cleanup_chips(nfc);
  375. of_node_put(np);
  376. return ret;
  377. }
  378. i++;
  379. }
  380. return 0;
  381. }
  382. static int ingenic_nand_probe(struct platform_device *pdev)
  383. {
  384. struct device *dev = &pdev->dev;
  385. unsigned int num_banks;
  386. struct ingenic_nfc *nfc;
  387. int ret;
  388. num_banks = jz4780_nemc_num_banks(dev);
  389. if (num_banks == 0) {
  390. dev_err(dev, "no banks found\n");
  391. return -ENODEV;
  392. }
  393. nfc = devm_kzalloc(dev, struct_size(nfc, cs, num_banks), GFP_KERNEL);
  394. if (!nfc)
  395. return -ENOMEM;
  396. nfc->soc_info = device_get_match_data(dev);
  397. if (!nfc->soc_info)
  398. return -EINVAL;
  399. /*
  400. * Check for ECC HW before we call nand_scan_ident, to prevent us from
  401. * having to call it again if the ECC driver returns -EPROBE_DEFER.
  402. */
  403. nfc->ecc = of_ingenic_ecc_get(dev->of_node);
  404. if (IS_ERR(nfc->ecc))
  405. return PTR_ERR(nfc->ecc);
  406. nfc->dev = dev;
  407. nfc->num_banks = num_banks;
  408. nand_controller_init(&nfc->controller);
  409. INIT_LIST_HEAD(&nfc->chips);
  410. ret = ingenic_nand_init_chips(nfc, pdev);
  411. if (ret) {
  412. if (nfc->ecc)
  413. ingenic_ecc_release(nfc->ecc);
  414. return ret;
  415. }
  416. platform_set_drvdata(pdev, nfc);
  417. return 0;
  418. }
  419. static void ingenic_nand_remove(struct platform_device *pdev)
  420. {
  421. struct ingenic_nfc *nfc = platform_get_drvdata(pdev);
  422. if (nfc->ecc)
  423. ingenic_ecc_release(nfc->ecc);
  424. ingenic_nand_cleanup_chips(nfc);
  425. }
  426. static const struct jz_soc_info jz4740_soc_info = {
  427. .data_offset = 0x00000000,
  428. .cmd_offset = 0x00008000,
  429. .addr_offset = 0x00010000,
  430. .oob_first = true,
  431. };
  432. static const struct jz_soc_info jz4725b_soc_info = {
  433. .data_offset = 0x00000000,
  434. .cmd_offset = 0x00008000,
  435. .addr_offset = 0x00010000,
  436. .oob_layout = &jz4725b_ooblayout_ops,
  437. };
  438. static const struct jz_soc_info jz4780_soc_info = {
  439. .data_offset = 0x00000000,
  440. .cmd_offset = 0x00400000,
  441. .addr_offset = 0x00800000,
  442. };
  443. static const struct of_device_id ingenic_nand_dt_match[] = {
  444. { .compatible = "ingenic,jz4740-nand", .data = &jz4740_soc_info },
  445. { .compatible = "ingenic,jz4725b-nand", .data = &jz4725b_soc_info },
  446. { .compatible = "ingenic,jz4780-nand", .data = &jz4780_soc_info },
  447. {},
  448. };
  449. MODULE_DEVICE_TABLE(of, ingenic_nand_dt_match);
  450. static struct platform_driver ingenic_nand_driver = {
  451. .probe = ingenic_nand_probe,
  452. .remove_new = ingenic_nand_remove,
  453. .driver = {
  454. .name = DRV_NAME,
  455. .of_match_table = ingenic_nand_dt_match,
  456. },
  457. };
  458. module_platform_driver(ingenic_nand_driver);
  459. MODULE_AUTHOR("Alex Smith <alex@alex-smith.me.uk>");
  460. MODULE_AUTHOR("Harvey Hunt <harveyhuntnexus@gmail.com>");
  461. MODULE_DESCRIPTION("Ingenic JZ47xx NAND driver");
  462. MODULE_LICENSE("GPL v2");