technologic-nand-controller.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Technologic Systems TS72xx NAND controller driver
  4. *
  5. * Copyright (C) 2023 Nikita Shubin <nikita.shubin@maquefel.me>
  6. *
  7. * Derived from: plat_nand.c
  8. * Author: Vitaly Wool <vitalywool@gmail.com>
  9. */
  10. #include <linux/bits.h>
  11. #include <linux/err.h>
  12. #include <linux/io.h>
  13. #include <linux/iopoll.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/mtd/mtd.h>
  18. #include <linux/mtd/platnand.h>
  19. #define TS72XX_NAND_CONTROL_ADDR_LINE BIT(22) /* 0xN0400000 */
  20. #define TS72XX_NAND_BUSY_ADDR_LINE BIT(23) /* 0xN0800000 */
  21. #define TS72XX_NAND_ALE BIT(0)
  22. #define TS72XX_NAND_CLE BIT(1)
  23. #define TS72XX_NAND_NCE BIT(2)
  24. #define TS72XX_NAND_CTRL_CLE (TS72XX_NAND_NCE | TS72XX_NAND_CLE)
  25. #define TS72XX_NAND_CTRL_ALE (TS72XX_NAND_NCE | TS72XX_NAND_ALE)
  26. struct ts72xx_nand_data {
  27. struct nand_controller controller;
  28. struct nand_chip chip;
  29. void __iomem *base;
  30. void __iomem *ctrl;
  31. void __iomem *busy;
  32. };
  33. static inline struct ts72xx_nand_data *chip_to_ts72xx(struct nand_chip *chip)
  34. {
  35. return container_of(chip, struct ts72xx_nand_data, chip);
  36. }
  37. static int ts72xx_nand_attach_chip(struct nand_chip *chip)
  38. {
  39. switch (chip->ecc.engine_type) {
  40. case NAND_ECC_ENGINE_TYPE_ON_HOST:
  41. return -EINVAL;
  42. case NAND_ECC_ENGINE_TYPE_SOFT:
  43. if (chip->ecc.algo == NAND_ECC_ALGO_UNKNOWN)
  44. chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
  45. chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
  46. fallthrough;
  47. default:
  48. return 0;
  49. }
  50. }
  51. static void ts72xx_nand_ctrl(struct nand_chip *chip, u8 value)
  52. {
  53. struct ts72xx_nand_data *data = chip_to_ts72xx(chip);
  54. unsigned char bits = ioread8(data->ctrl) & ~GENMASK(2, 0);
  55. iowrite8(bits | value, data->ctrl);
  56. }
  57. static int ts72xx_nand_exec_instr(struct nand_chip *chip,
  58. const struct nand_op_instr *instr)
  59. {
  60. struct ts72xx_nand_data *data = chip_to_ts72xx(chip);
  61. unsigned int timeout_us;
  62. u32 status;
  63. int ret;
  64. switch (instr->type) {
  65. case NAND_OP_CMD_INSTR:
  66. ts72xx_nand_ctrl(chip, TS72XX_NAND_CTRL_CLE);
  67. iowrite8(instr->ctx.cmd.opcode, data->base);
  68. ts72xx_nand_ctrl(chip, TS72XX_NAND_NCE);
  69. break;
  70. case NAND_OP_ADDR_INSTR:
  71. ts72xx_nand_ctrl(chip, TS72XX_NAND_CTRL_ALE);
  72. iowrite8_rep(data->base, instr->ctx.addr.addrs, instr->ctx.addr.naddrs);
  73. ts72xx_nand_ctrl(chip, TS72XX_NAND_NCE);
  74. break;
  75. case NAND_OP_DATA_IN_INSTR:
  76. ioread8_rep(data->base, instr->ctx.data.buf.in, instr->ctx.data.len);
  77. break;
  78. case NAND_OP_DATA_OUT_INSTR:
  79. iowrite8_rep(data->base, instr->ctx.data.buf.in, instr->ctx.data.len);
  80. break;
  81. case NAND_OP_WAITRDY_INSTR:
  82. timeout_us = instr->ctx.waitrdy.timeout_ms * 1000;
  83. ret = readb_poll_timeout(data->busy, status, status & BIT(5), 0, timeout_us);
  84. if (ret)
  85. return ret;
  86. break;
  87. }
  88. if (instr->delay_ns)
  89. ndelay(instr->delay_ns);
  90. return 0;
  91. }
  92. static int ts72xx_nand_exec_op(struct nand_chip *chip,
  93. const struct nand_operation *op, bool check_only)
  94. {
  95. unsigned int i;
  96. int ret;
  97. if (check_only)
  98. return 0;
  99. for (i = 0; i < op->ninstrs; i++) {
  100. ret = ts72xx_nand_exec_instr(chip, &op->instrs[i]);
  101. if (ret)
  102. return ret;
  103. }
  104. return 0;
  105. }
  106. static const struct nand_controller_ops ts72xx_nand_ops = {
  107. .attach_chip = ts72xx_nand_attach_chip,
  108. .exec_op = ts72xx_nand_exec_op,
  109. };
  110. static int ts72xx_nand_probe(struct platform_device *pdev)
  111. {
  112. struct ts72xx_nand_data *data;
  113. struct fwnode_handle *child;
  114. struct mtd_info *mtd;
  115. int err;
  116. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  117. if (!data)
  118. return -ENOMEM;
  119. nand_controller_init(&data->controller);
  120. data->controller.ops = &ts72xx_nand_ops;
  121. data->chip.controller = &data->controller;
  122. data->base = devm_platform_ioremap_resource(pdev, 0);
  123. if (IS_ERR(data->base))
  124. return PTR_ERR(data->base);
  125. data->ctrl = data->base + TS72XX_NAND_CONTROL_ADDR_LINE;
  126. data->busy = data->base + TS72XX_NAND_BUSY_ADDR_LINE;
  127. child = fwnode_get_next_child_node(dev_fwnode(&pdev->dev), NULL);
  128. if (!child)
  129. return dev_err_probe(&pdev->dev, -ENXIO,
  130. "ts72xx controller node should have exactly one child\n");
  131. nand_set_flash_node(&data->chip, to_of_node(child));
  132. mtd = nand_to_mtd(&data->chip);
  133. mtd->dev.parent = &pdev->dev;
  134. platform_set_drvdata(pdev, data);
  135. /*
  136. * This driver assumes that the default ECC engine should be TYPE_SOFT.
  137. * Set ->engine_type before registering the NAND devices in order to
  138. * provide a driver specific default value.
  139. */
  140. data->chip.ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
  141. /* Scan to find existence of the device */
  142. err = nand_scan(&data->chip, 1);
  143. if (err)
  144. goto err_handle_put;
  145. err = mtd_device_parse_register(mtd, NULL, NULL, NULL, 0);
  146. if (err)
  147. goto err_clean_nand;
  148. return 0;
  149. err_clean_nand:
  150. nand_cleanup(&data->chip);
  151. err_handle_put:
  152. fwnode_handle_put(child);
  153. return err;
  154. }
  155. static void ts72xx_nand_remove(struct platform_device *pdev)
  156. {
  157. struct ts72xx_nand_data *data = platform_get_drvdata(pdev);
  158. struct fwnode_handle *fwnode = dev_fwnode(&pdev->dev);
  159. struct nand_chip *chip = &data->chip;
  160. int ret;
  161. ret = mtd_device_unregister(nand_to_mtd(chip));
  162. WARN_ON(ret);
  163. nand_cleanup(chip);
  164. fwnode_handle_put(fwnode);
  165. }
  166. static const struct of_device_id ts72xx_id_table[] = {
  167. { .compatible = "technologic,ts7200-nand" },
  168. { /* sentinel */ }
  169. };
  170. MODULE_DEVICE_TABLE(of, ts72xx_id_table);
  171. static struct platform_driver ts72xx_nand_driver = {
  172. .driver = {
  173. .name = "ts72xx-nand",
  174. .of_match_table = ts72xx_id_table,
  175. },
  176. .probe = ts72xx_nand_probe,
  177. .remove_new = ts72xx_nand_remove,
  178. };
  179. module_platform_driver(ts72xx_nand_driver);
  180. MODULE_AUTHOR("Nikita Shubin <nikita.shubin@maquefel.me>");
  181. MODULE_DESCRIPTION("Technologic Systems TS72xx NAND controller driver");
  182. MODULE_LICENSE("GPL");