fsl_upm.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Freescale UPM NAND driver.
  4. *
  5. * Copyright © 2007-2008 MontaVista Software, Inc.
  6. *
  7. * Author: Anton Vorontsov <[email protected]>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/delay.h>
  12. #include <linux/mtd/rawnand.h>
  13. #include <linux/mtd/partitions.h>
  14. #include <linux/mtd/mtd.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/io.h>
  17. #include <linux/slab.h>
  18. #include <asm/fsl_lbc.h>
  19. struct fsl_upm_nand {
  20. struct nand_controller base;
  21. struct device *dev;
  22. struct nand_chip chip;
  23. struct fsl_upm upm;
  24. uint8_t upm_addr_offset;
  25. uint8_t upm_cmd_offset;
  26. void __iomem *io_base;
  27. struct gpio_desc *rnb_gpio[NAND_MAX_CHIPS];
  28. uint32_t mchip_offsets[NAND_MAX_CHIPS];
  29. uint32_t mchip_count;
  30. uint32_t mchip_number;
  31. };
  32. static inline struct fsl_upm_nand *to_fsl_upm_nand(struct mtd_info *mtdinfo)
  33. {
  34. return container_of(mtd_to_nand(mtdinfo), struct fsl_upm_nand,
  35. chip);
  36. }
  37. static int fun_chip_init(struct fsl_upm_nand *fun,
  38. const struct device_node *upm_np,
  39. const struct resource *io_res)
  40. {
  41. struct mtd_info *mtd = nand_to_mtd(&fun->chip);
  42. int ret;
  43. struct device_node *flash_np;
  44. fun->chip.ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
  45. fun->chip.ecc.algo = NAND_ECC_ALGO_HAMMING;
  46. fun->chip.controller = &fun->base;
  47. mtd->dev.parent = fun->dev;
  48. flash_np = of_get_next_child(upm_np, NULL);
  49. if (!flash_np)
  50. return -ENODEV;
  51. nand_set_flash_node(&fun->chip, flash_np);
  52. mtd->name = devm_kasprintf(fun->dev, GFP_KERNEL, "0x%llx.%pOFn",
  53. (u64)io_res->start,
  54. flash_np);
  55. if (!mtd->name) {
  56. ret = -ENOMEM;
  57. goto err;
  58. }
  59. ret = nand_scan(&fun->chip, fun->mchip_count);
  60. if (ret)
  61. goto err;
  62. ret = mtd_device_register(mtd, NULL, 0);
  63. err:
  64. of_node_put(flash_np);
  65. return ret;
  66. }
  67. static int func_exec_instr(struct nand_chip *chip,
  68. const struct nand_op_instr *instr)
  69. {
  70. struct fsl_upm_nand *fun = to_fsl_upm_nand(nand_to_mtd(chip));
  71. u32 mar, reg_offs = fun->mchip_offsets[fun->mchip_number];
  72. unsigned int i;
  73. const u8 *out;
  74. u8 *in;
  75. switch (instr->type) {
  76. case NAND_OP_CMD_INSTR:
  77. fsl_upm_start_pattern(&fun->upm, fun->upm_cmd_offset);
  78. mar = (instr->ctx.cmd.opcode << (32 - fun->upm.width)) |
  79. reg_offs;
  80. fsl_upm_run_pattern(&fun->upm, fun->io_base + reg_offs, mar);
  81. fsl_upm_end_pattern(&fun->upm);
  82. return 0;
  83. case NAND_OP_ADDR_INSTR:
  84. fsl_upm_start_pattern(&fun->upm, fun->upm_addr_offset);
  85. for (i = 0; i < instr->ctx.addr.naddrs; i++) {
  86. mar = (instr->ctx.addr.addrs[i] << (32 - fun->upm.width)) |
  87. reg_offs;
  88. fsl_upm_run_pattern(&fun->upm, fun->io_base + reg_offs, mar);
  89. }
  90. fsl_upm_end_pattern(&fun->upm);
  91. return 0;
  92. case NAND_OP_DATA_IN_INSTR:
  93. in = instr->ctx.data.buf.in;
  94. for (i = 0; i < instr->ctx.data.len; i++)
  95. in[i] = in_8(fun->io_base + reg_offs);
  96. return 0;
  97. case NAND_OP_DATA_OUT_INSTR:
  98. out = instr->ctx.data.buf.out;
  99. for (i = 0; i < instr->ctx.data.len; i++)
  100. out_8(fun->io_base + reg_offs, out[i]);
  101. return 0;
  102. case NAND_OP_WAITRDY_INSTR:
  103. if (!fun->rnb_gpio[fun->mchip_number])
  104. return nand_soft_waitrdy(chip, instr->ctx.waitrdy.timeout_ms);
  105. return nand_gpio_waitrdy(chip, fun->rnb_gpio[fun->mchip_number],
  106. instr->ctx.waitrdy.timeout_ms);
  107. default:
  108. return -EINVAL;
  109. }
  110. return 0;
  111. }
  112. static int fun_exec_op(struct nand_chip *chip, const struct nand_operation *op,
  113. bool check_only)
  114. {
  115. struct fsl_upm_nand *fun = to_fsl_upm_nand(nand_to_mtd(chip));
  116. unsigned int i;
  117. int ret;
  118. if (op->cs >= NAND_MAX_CHIPS)
  119. return -EINVAL;
  120. if (check_only)
  121. return 0;
  122. fun->mchip_number = op->cs;
  123. for (i = 0; i < op->ninstrs; i++) {
  124. ret = func_exec_instr(chip, &op->instrs[i]);
  125. if (ret)
  126. return ret;
  127. if (op->instrs[i].delay_ns)
  128. ndelay(op->instrs[i].delay_ns);
  129. }
  130. return 0;
  131. }
  132. static const struct nand_controller_ops fun_ops = {
  133. .exec_op = fun_exec_op,
  134. };
  135. static int fun_probe(struct platform_device *ofdev)
  136. {
  137. struct fsl_upm_nand *fun;
  138. struct resource *io_res;
  139. const __be32 *prop;
  140. int ret;
  141. int size;
  142. int i;
  143. fun = devm_kzalloc(&ofdev->dev, sizeof(*fun), GFP_KERNEL);
  144. if (!fun)
  145. return -ENOMEM;
  146. io_res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
  147. fun->io_base = devm_ioremap_resource(&ofdev->dev, io_res);
  148. if (IS_ERR(fun->io_base))
  149. return PTR_ERR(fun->io_base);
  150. ret = fsl_upm_find(io_res->start, &fun->upm);
  151. if (ret) {
  152. dev_err(&ofdev->dev, "can't find UPM\n");
  153. return ret;
  154. }
  155. prop = of_get_property(ofdev->dev.of_node, "fsl,upm-addr-offset",
  156. &size);
  157. if (!prop || size != sizeof(uint32_t)) {
  158. dev_err(&ofdev->dev, "can't get UPM address offset\n");
  159. return -EINVAL;
  160. }
  161. fun->upm_addr_offset = *prop;
  162. prop = of_get_property(ofdev->dev.of_node, "fsl,upm-cmd-offset", &size);
  163. if (!prop || size != sizeof(uint32_t)) {
  164. dev_err(&ofdev->dev, "can't get UPM command offset\n");
  165. return -EINVAL;
  166. }
  167. fun->upm_cmd_offset = *prop;
  168. prop = of_get_property(ofdev->dev.of_node,
  169. "fsl,upm-addr-line-cs-offsets", &size);
  170. if (prop && (size / sizeof(uint32_t)) > 0) {
  171. fun->mchip_count = size / sizeof(uint32_t);
  172. if (fun->mchip_count >= NAND_MAX_CHIPS) {
  173. dev_err(&ofdev->dev, "too much multiple chips\n");
  174. return -EINVAL;
  175. }
  176. for (i = 0; i < fun->mchip_count; i++)
  177. fun->mchip_offsets[i] = be32_to_cpu(prop[i]);
  178. } else {
  179. fun->mchip_count = 1;
  180. }
  181. for (i = 0; i < fun->mchip_count; i++) {
  182. fun->rnb_gpio[i] = devm_gpiod_get_index_optional(&ofdev->dev,
  183. NULL, i,
  184. GPIOD_IN);
  185. if (IS_ERR(fun->rnb_gpio[i])) {
  186. dev_err(&ofdev->dev, "RNB gpio #%d is invalid\n", i);
  187. return PTR_ERR(fun->rnb_gpio[i]);
  188. }
  189. }
  190. nand_controller_init(&fun->base);
  191. fun->base.ops = &fun_ops;
  192. fun->dev = &ofdev->dev;
  193. ret = fun_chip_init(fun, ofdev->dev.of_node, io_res);
  194. if (ret)
  195. return ret;
  196. dev_set_drvdata(&ofdev->dev, fun);
  197. return 0;
  198. }
  199. static int fun_remove(struct platform_device *ofdev)
  200. {
  201. struct fsl_upm_nand *fun = dev_get_drvdata(&ofdev->dev);
  202. struct nand_chip *chip = &fun->chip;
  203. struct mtd_info *mtd = nand_to_mtd(chip);
  204. int ret;
  205. ret = mtd_device_unregister(mtd);
  206. WARN_ON(ret);
  207. nand_cleanup(chip);
  208. return 0;
  209. }
  210. static const struct of_device_id of_fun_match[] = {
  211. { .compatible = "fsl,upm-nand" },
  212. {},
  213. };
  214. MODULE_DEVICE_TABLE(of, of_fun_match);
  215. static struct platform_driver of_fun_driver = {
  216. .driver = {
  217. .name = "fsl,upm-nand",
  218. .of_match_table = of_fun_match,
  219. },
  220. .probe = fun_probe,
  221. .remove = fun_remove,
  222. };
  223. module_platform_driver(of_fun_driver);
  224. MODULE_LICENSE("GPL");
  225. MODULE_AUTHOR("Anton Vorontsov <[email protected]>");
  226. MODULE_DESCRIPTION("Driver for NAND chips working through Freescale "
  227. "LocalBus User-Programmable Machine");