nand_rb4xx.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * NAND driver for the MikroTik RouterBoard 4xx series
  4. *
  5. * Copyright (C) 2008-2011 Gabor Juhos <[email protected]>
  6. * Copyright (C) 2008 Imre Kaloz <[email protected]>
  7. * Copyright (C) 2015 Bert Vermeulen <[email protected]>
  8. * Copyright (C) 2020 Christopher Hill <[email protected]>
  9. *
  10. * This file was based on the driver for Linux 2.6.22 published by
  11. * MikroTik for their RouterBoard 4xx series devices.
  12. *
  13. * N.B. driver probe reports "DMA mask not set" warnings which are
  14. * an artifact of using a platform_driver as an MFD device child.
  15. * See conversation here https://lkml.org/lkml/2020/4/28/675
  16. */
  17. #include <linux/platform_device.h>
  18. #include <linux/mtd/rawnand.h>
  19. #include <linux/gpio/consumer.h>
  20. #include <linux/module.h>
  21. #include <linux/of_device.h>
  22. #include <linux/version.h>
  23. #include <mfd/rb4xx-cpld.h>
  24. struct rb4xx_nand {
  25. struct rb4xx_cpld *cpld;
  26. struct device *dev;
  27. struct nand_chip chip;
  28. struct gpio_desc *ale;
  29. struct gpio_desc *cle;
  30. struct gpio_desc *nce;
  31. struct gpio_desc *rdy;
  32. };
  33. static int rb4xx_ooblayout_ecc(struct mtd_info *mtd, int section,
  34. struct mtd_oob_region *oobregion)
  35. {
  36. switch (section) {
  37. case 0:
  38. oobregion->offset = 8;
  39. oobregion->length = 3;
  40. return 0;
  41. case 1:
  42. oobregion->offset = 13;
  43. oobregion->length = 3;
  44. return 0;
  45. default:
  46. return -ERANGE;
  47. }
  48. }
  49. static int rb4xx_ooblayout_free(struct mtd_info *mtd, int section,
  50. struct mtd_oob_region *oobregion)
  51. {
  52. switch (section) {
  53. case 0:
  54. oobregion->offset = 0;
  55. oobregion->length = 4;
  56. return 0;
  57. case 1:
  58. oobregion->offset = 4;
  59. oobregion->length = 1;
  60. return 0;
  61. case 2:
  62. oobregion->offset = 6;
  63. oobregion->length = 2;
  64. return 0;
  65. case 3:
  66. oobregion->offset = 11;
  67. oobregion->length = 2;
  68. return 0;
  69. default:
  70. return -ERANGE;
  71. }
  72. }
  73. static const struct mtd_ooblayout_ops rb4xx_nand_ecclayout_ops = {
  74. .ecc = rb4xx_ooblayout_ecc,
  75. .free = rb4xx_ooblayout_free,
  76. };
  77. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0)
  78. static uint8_t rb4xx_nand_read_byte(struct mtd_info *mtd)
  79. {
  80. struct rb4xx_nand *nand = mtd->priv;
  81. #else
  82. static u8 rb4xx_nand_read_byte(struct nand_chip *chip)
  83. {
  84. struct rb4xx_nand *nand = chip->priv;
  85. #endif
  86. struct rb4xx_cpld *cpld = nand->cpld;
  87. u8 data;
  88. int ret;
  89. ret = cpld->read_nand(cpld, &data, 1);
  90. if (unlikely(ret))
  91. return 0xff;
  92. return data;
  93. }
  94. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0)
  95. static void rb4xx_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
  96. int len)
  97. {
  98. struct rb4xx_nand *nand = mtd->priv;
  99. #else
  100. static void rb4xx_nand_write_buf(struct nand_chip *chip, const u8 *buf, int len)
  101. {
  102. struct rb4xx_nand *nand = chip->priv;
  103. #endif
  104. struct rb4xx_cpld *cpld = nand->cpld;
  105. cpld->write_nand(cpld, buf, len);
  106. }
  107. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0)
  108. static void rb4xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  109. {
  110. struct rb4xx_nand *nand = mtd->priv;
  111. #else
  112. static void rb4xx_nand_read_buf(struct nand_chip *chip, u8 *buf, int len)
  113. {
  114. struct rb4xx_nand *nand = chip->priv;
  115. #endif
  116. struct rb4xx_cpld *cpld = nand->cpld;
  117. cpld->read_nand(cpld, buf, len);
  118. }
  119. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0)
  120. static void rb4xx_nand_cmd_ctrl(struct mtd_info *mtd, int dat, unsigned int ctrl)
  121. {
  122. struct rb4xx_nand *nand = mtd->priv;
  123. #else
  124. static void rb4xx_nand_cmd_ctrl(struct nand_chip *chip, int dat,
  125. unsigned int ctrl)
  126. {
  127. struct rb4xx_nand *nand = chip->priv;
  128. #endif
  129. struct rb4xx_cpld *cpld = nand->cpld;
  130. u8 data = dat;
  131. if (ctrl & NAND_CTRL_CHANGE) {
  132. gpiod_set_value_cansleep(nand->cle, !!(ctrl & NAND_CLE));
  133. gpiod_set_value_cansleep(nand->ale, !!(ctrl & NAND_ALE));
  134. gpiod_set_value_cansleep(nand->nce, !(ctrl & NAND_NCE));
  135. }
  136. if (dat != NAND_CMD_NONE)
  137. cpld->write_nand(cpld, &data, 1);
  138. }
  139. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0)
  140. static int rb4xx_nand_dev_ready(struct mtd_info *mtd)
  141. {
  142. struct rb4xx_nand *nand = mtd->priv;
  143. #else
  144. static int rb4xx_nand_dev_ready(struct nand_chip *chip)
  145. {
  146. struct rb4xx_nand *nand = chip->priv;
  147. #endif
  148. return gpiod_get_value_cansleep(nand->rdy);
  149. }
  150. static int rb4xx_nand_probe(struct platform_device *pdev)
  151. {
  152. struct device *dev = &pdev->dev;
  153. struct device *parent = dev->parent;
  154. struct rb4xx_nand *nand;
  155. struct mtd_info *mtd;
  156. int ret;
  157. if (!parent)
  158. return -ENODEV;
  159. nand = devm_kzalloc(dev, sizeof(*nand), GFP_KERNEL);
  160. if (!nand)
  161. return -ENOMEM;
  162. platform_set_drvdata(pdev, nand);
  163. nand->cpld = dev_get_drvdata(parent);
  164. nand->dev = dev;
  165. nand->ale = devm_gpiod_get_index(dev, NULL, 0, GPIOD_OUT_LOW);
  166. if (IS_ERR(nand->ale))
  167. dev_err(dev, "missing gpio ALE: %ld\n", PTR_ERR(nand->ale));
  168. nand->cle = devm_gpiod_get_index(dev, NULL, 1, GPIOD_OUT_LOW);
  169. if (IS_ERR(nand->cle))
  170. dev_err(dev, "missing gpio CLE: %ld\n", PTR_ERR(nand->cle));
  171. nand->nce = devm_gpiod_get_index(dev, NULL, 2, GPIOD_OUT_LOW);
  172. if (IS_ERR(nand->nce))
  173. dev_err(dev, "missing gpio nCE: %ld\n", PTR_ERR(nand->nce));
  174. nand->rdy = devm_gpiod_get_index(dev, NULL, 3, GPIOD_IN);
  175. if (IS_ERR(nand->rdy))
  176. dev_err(dev, "missing gpio RDY: %ld\n", PTR_ERR(nand->rdy));
  177. if (IS_ERR(nand->ale) || IS_ERR(nand->cle) ||
  178. IS_ERR(nand->nce) || IS_ERR(nand->rdy))
  179. return -ENOENT;
  180. gpiod_set_consumer_name(nand->ale, "mikrotik:nand:ALE");
  181. gpiod_set_consumer_name(nand->cle, "mikrotik:nand:CLE");
  182. gpiod_set_consumer_name(nand->nce, "mikrotik:nand:nCE");
  183. gpiod_set_consumer_name(nand->rdy, "mikrotik:nand:RDY");
  184. mtd = nand_to_mtd(&nand->chip);
  185. mtd->priv = nand;
  186. mtd->owner = THIS_MODULE;
  187. mtd->dev.parent = dev;
  188. mtd_set_of_node(mtd, dev->of_node);
  189. if (mtd->writesize == 512)
  190. mtd_set_ooblayout(mtd, &rb4xx_nand_ecclayout_ops);
  191. nand->chip.ecc.mode = NAND_ECC_SOFT;
  192. nand->chip.ecc.algo = NAND_ECC_HAMMING;
  193. nand->chip.options = NAND_NO_SUBPAGE_WRITE;
  194. nand->chip.priv = nand;
  195. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0)
  196. nand->chip.read_byte = rb4xx_nand_read_byte;
  197. nand->chip.write_buf = rb4xx_nand_write_buf;
  198. nand->chip.read_buf = rb4xx_nand_read_buf;
  199. nand->chip.cmd_ctrl = rb4xx_nand_cmd_ctrl;
  200. nand->chip.dev_ready = rb4xx_nand_dev_ready;
  201. nand->chip.chip_delay = 25;
  202. ret = nand_scan(mtd, 1);
  203. #else
  204. nand->chip.legacy.read_byte = rb4xx_nand_read_byte;
  205. nand->chip.legacy.write_buf = rb4xx_nand_write_buf;
  206. nand->chip.legacy.read_buf = rb4xx_nand_read_buf;
  207. nand->chip.legacy.cmd_ctrl = rb4xx_nand_cmd_ctrl;
  208. nand->chip.legacy.dev_ready = rb4xx_nand_dev_ready;
  209. nand->chip.legacy.chip_delay = 25;
  210. ret = nand_scan(&nand->chip, 1);
  211. #endif
  212. if (ret)
  213. return -ENXIO;
  214. ret = mtd_device_register(mtd, NULL, 0);
  215. if (ret) {
  216. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0)
  217. nand_release(mtd);
  218. #else
  219. nand_release(&nand->chip);
  220. #endif
  221. return ret;
  222. }
  223. return 0;
  224. }
  225. static int rb4xx_nand_remove(struct platform_device *pdev)
  226. {
  227. struct rb4xx_nand *nand = platform_get_drvdata(pdev);
  228. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0)
  229. nand_release(nand_to_mtd(&nand->chip));
  230. #else
  231. nand_release(&nand->chip);
  232. #endif
  233. return 0;
  234. }
  235. static const struct platform_device_id rb4xx_nand_id_table[] = {
  236. { "mikrotik,rb4xx-nand", },
  237. { },
  238. };
  239. MODULE_DEVICE_TABLE(platform, rb4xx_nand_id_table);
  240. static struct platform_driver rb4xx_nand_driver = {
  241. .probe = rb4xx_nand_probe,
  242. .remove = rb4xx_nand_remove,
  243. .id_table = rb4xx_nand_id_table,
  244. .driver = {
  245. .name = "rb4xx-nand",
  246. },
  247. };
  248. module_platform_driver(rb4xx_nand_driver);
  249. MODULE_DESCRIPTION("Mikrotik RB4xx NAND driver");
  250. MODULE_AUTHOR("Gabor Juhos <[email protected]>");
  251. MODULE_AUTHOR("Imre Kaloz <[email protected]>");
  252. MODULE_AUTHOR("Bert Vermeulen <[email protected]>");
  253. MODULE_AUTHOR("Christopher Hill <[email protected]");
  254. MODULE_LICENSE("GPL v2");