rb91x_nand.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * NAND flash driver for the MikroTik RouterBOARD 91x series
  3. *
  4. * Copyright (C) 2013-2014 Gabor Juhos <juhosg@openwrt.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. */
  10. #include <linux/version.h>
  11. #include <linux/kernel.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/module.h>
  14. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
  15. #include <linux/mtd/nand.h>
  16. #else
  17. #include <linux/mtd/rawnand.h>
  18. #endif
  19. #include <linux/mtd/mtd.h>
  20. #include <linux/mtd/partitions.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/io.h>
  23. #include <linux/slab.h>
  24. #include <linux/gpio.h>
  25. #include <linux/platform_data/rb91x_nand.h>
  26. #include <asm/mach-ath79/ar71xx_regs.h>
  27. #include <asm/mach-ath79/ath79.h>
  28. #define DRV_DESC "NAND flash driver for the RouterBOARD 91x series"
  29. #define RB91X_NAND_NRWE BIT(12)
  30. #define RB91X_NAND_DATA_BITS (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4) |\
  31. BIT(13) | BIT(14) | BIT(15))
  32. #define RB91X_NAND_INPUT_BITS (RB91X_NAND_DATA_BITS | RB91X_NAND_RDY)
  33. #define RB91X_NAND_OUTPUT_BITS (RB91X_NAND_DATA_BITS | RB91X_NAND_NRWE)
  34. #define RB91X_NAND_LOW_DATA_MASK 0x1f
  35. #define RB91X_NAND_HIGH_DATA_MASK 0xe0
  36. #define RB91X_NAND_HIGH_DATA_SHIFT 8
  37. struct rb91x_nand_info {
  38. struct nand_chip chip;
  39. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  40. struct mtd_info mtd;
  41. #endif
  42. struct device *dev;
  43. int gpio_nce;
  44. int gpio_ale;
  45. int gpio_cle;
  46. int gpio_rdy;
  47. int gpio_read;
  48. int gpio_nrw;
  49. int gpio_nle;
  50. };
  51. static inline struct rb91x_nand_info *mtd_to_rbinfo(struct mtd_info *mtd)
  52. {
  53. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  54. return container_of(mtd, struct rb91x_nand_info, mtd);
  55. #else
  56. struct nand_chip *chip = mtd_to_nand(mtd);
  57. return container_of(chip, struct rb91x_nand_info, chip);
  58. #endif
  59. }
  60. static struct mtd_info *rbinfo_to_mtd(struct rb91x_nand_info *nfc)
  61. {
  62. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  63. return &nfc->mtd;
  64. #else
  65. return nand_to_mtd(&nfc->chip);
  66. #endif
  67. }
  68. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  69. /*
  70. * We need to use the OLD Yaffs-1 OOB layout, otherwise the RB bootloader
  71. * will not be able to find the kernel that we load.
  72. */
  73. static struct nand_ecclayout rb91x_nand_ecclayout = {
  74. .eccbytes = 6,
  75. .eccpos = { 8, 9, 10, 13, 14, 15 },
  76. .oobavail = 9,
  77. .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
  78. };
  79. #else
  80. static int rb91x_ooblayout_ecc(struct mtd_info *mtd, int section,
  81. struct mtd_oob_region *oobregion)
  82. {
  83. switch (section) {
  84. case 0:
  85. oobregion->offset = 8;
  86. oobregion->length = 3;
  87. return 0;
  88. case 1:
  89. oobregion->offset = 13;
  90. oobregion->length = 3;
  91. return 0;
  92. default:
  93. return -ERANGE;
  94. }
  95. }
  96. static int rb91x_ooblayout_free(struct mtd_info *mtd, int section,
  97. struct mtd_oob_region *oobregion)
  98. {
  99. switch (section) {
  100. case 0:
  101. oobregion->offset = 0;
  102. oobregion->length = 4;
  103. return 0;
  104. case 1:
  105. oobregion->offset = 4;
  106. oobregion->length = 1;
  107. return 0;
  108. case 2:
  109. oobregion->offset = 6;
  110. oobregion->length = 2;
  111. return 0;
  112. case 3:
  113. oobregion->offset = 11;
  114. oobregion->length = 2;
  115. return 0;
  116. default:
  117. return -ERANGE;
  118. }
  119. }
  120. static const struct mtd_ooblayout_ops rb91x_nand_ecclayout_ops = {
  121. .ecc = rb91x_ooblayout_ecc,
  122. .free = rb91x_ooblayout_free,
  123. };
  124. #endif /* < 4.6 */
  125. static struct mtd_partition rb91x_nand_partitions[] = {
  126. {
  127. .name = "booter",
  128. .offset = 0,
  129. .size = (256 * 1024),
  130. .mask_flags = MTD_WRITEABLE,
  131. }, {
  132. .name = "kernel",
  133. .offset = (256 * 1024),
  134. .size = (4 * 1024 * 1024) - (256 * 1024),
  135. }, {
  136. .name = "ubi",
  137. .offset = MTDPART_OFS_NXTBLK,
  138. .size = MTDPART_SIZ_FULL,
  139. },
  140. };
  141. static void rb91x_nand_write(struct rb91x_nand_info *rbni,
  142. const u8 *buf,
  143. unsigned len)
  144. {
  145. void __iomem *base = ath79_gpio_base;
  146. u32 oe_reg;
  147. u32 out_reg;
  148. u32 out;
  149. unsigned i;
  150. /* enable the latch */
  151. gpio_set_value_cansleep(rbni->gpio_nle, 0);
  152. oe_reg = __raw_readl(base + AR71XX_GPIO_REG_OE);
  153. out_reg = __raw_readl(base + AR71XX_GPIO_REG_OUT);
  154. /* set data lines to output mode */
  155. __raw_writel(oe_reg & ~(RB91X_NAND_DATA_BITS | RB91X_NAND_NRWE),
  156. base + AR71XX_GPIO_REG_OE);
  157. out = out_reg & ~(RB91X_NAND_DATA_BITS | RB91X_NAND_NRWE);
  158. for (i = 0; i != len; i++) {
  159. u32 data;
  160. data = (buf[i] & RB91X_NAND_HIGH_DATA_MASK) <<
  161. RB91X_NAND_HIGH_DATA_SHIFT;
  162. data |= buf[i] & RB91X_NAND_LOW_DATA_MASK;
  163. data |= out;
  164. __raw_writel(data, base + AR71XX_GPIO_REG_OUT);
  165. /* deactivate WE line */
  166. data |= RB91X_NAND_NRWE;
  167. __raw_writel(data, base + AR71XX_GPIO_REG_OUT);
  168. /* flush write */
  169. __raw_readl(base + AR71XX_GPIO_REG_OUT);
  170. }
  171. /* restore registers */
  172. __raw_writel(out_reg, base + AR71XX_GPIO_REG_OUT);
  173. __raw_writel(oe_reg, base + AR71XX_GPIO_REG_OE);
  174. /* flush write */
  175. __raw_readl(base + AR71XX_GPIO_REG_OUT);
  176. /* disable the latch */
  177. gpio_set_value_cansleep(rbni->gpio_nle, 1);
  178. }
  179. static void rb91x_nand_read(struct rb91x_nand_info *rbni,
  180. u8 *read_buf,
  181. unsigned len)
  182. {
  183. void __iomem *base = ath79_gpio_base;
  184. u32 oe_reg;
  185. u32 out_reg;
  186. unsigned i;
  187. /* enable read mode */
  188. gpio_set_value_cansleep(rbni->gpio_read, 1);
  189. /* enable latch */
  190. gpio_set_value_cansleep(rbni->gpio_nle, 0);
  191. /* save registers */
  192. oe_reg = __raw_readl(base + AR71XX_GPIO_REG_OE);
  193. out_reg = __raw_readl(base + AR71XX_GPIO_REG_OUT);
  194. /* set data lines to input mode */
  195. __raw_writel(oe_reg | RB91X_NAND_DATA_BITS,
  196. base + AR71XX_GPIO_REG_OE);
  197. for (i = 0; i < len; i++) {
  198. u32 in;
  199. u8 data;
  200. /* activate RE line */
  201. __raw_writel(RB91X_NAND_NRWE, base + AR71XX_GPIO_REG_CLEAR);
  202. /* flush write */
  203. __raw_readl(base + AR71XX_GPIO_REG_CLEAR);
  204. /* read input lines */
  205. in = __raw_readl(base + AR71XX_GPIO_REG_IN);
  206. /* deactivate RE line */
  207. __raw_writel(RB91X_NAND_NRWE, base + AR71XX_GPIO_REG_SET);
  208. data = (in & RB91X_NAND_LOW_DATA_MASK);
  209. data |= (in >> RB91X_NAND_HIGH_DATA_SHIFT) &
  210. RB91X_NAND_HIGH_DATA_MASK;
  211. read_buf[i] = data;
  212. }
  213. /* restore registers */
  214. __raw_writel(out_reg, base + AR71XX_GPIO_REG_OUT);
  215. __raw_writel(oe_reg, base + AR71XX_GPIO_REG_OE);
  216. /* flush write */
  217. __raw_readl(base + AR71XX_GPIO_REG_OUT);
  218. /* disable latch */
  219. gpio_set_value_cansleep(rbni->gpio_nle, 1);
  220. /* disable read mode */
  221. gpio_set_value_cansleep(rbni->gpio_read, 0);
  222. }
  223. static int rb91x_nand_dev_ready(struct mtd_info *mtd)
  224. {
  225. struct rb91x_nand_info *rbni = mtd_to_rbinfo(mtd);
  226. return gpio_get_value_cansleep(rbni->gpio_rdy);
  227. }
  228. static void rb91x_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
  229. unsigned int ctrl)
  230. {
  231. struct rb91x_nand_info *rbni = mtd_to_rbinfo(mtd);
  232. if (ctrl & NAND_CTRL_CHANGE) {
  233. gpio_set_value_cansleep(rbni->gpio_cle,
  234. (ctrl & NAND_CLE) ? 1 : 0);
  235. gpio_set_value_cansleep(rbni->gpio_ale,
  236. (ctrl & NAND_ALE) ? 1 : 0);
  237. gpio_set_value_cansleep(rbni->gpio_nce,
  238. (ctrl & NAND_NCE) ? 0 : 1);
  239. }
  240. if (cmd != NAND_CMD_NONE) {
  241. u8 t = cmd;
  242. rb91x_nand_write(rbni, &t, 1);
  243. }
  244. }
  245. static u8 rb91x_nand_read_byte(struct mtd_info *mtd)
  246. {
  247. struct rb91x_nand_info *rbni = mtd_to_rbinfo(mtd);
  248. u8 data = 0xff;
  249. rb91x_nand_read(rbni, &data, 1);
  250. return data;
  251. }
  252. static void rb91x_nand_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  253. {
  254. struct rb91x_nand_info *rbni = mtd_to_rbinfo(mtd);
  255. rb91x_nand_read(rbni, buf, len);
  256. }
  257. static void rb91x_nand_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  258. {
  259. struct rb91x_nand_info *rbni = mtd_to_rbinfo(mtd);
  260. rb91x_nand_write(rbni, buf, len);
  261. }
  262. static int rb91x_nand_gpio_init(struct rb91x_nand_info *info)
  263. {
  264. int ret;
  265. /*
  266. * Ensure that the LATCH is disabled before initializing
  267. * control lines.
  268. */
  269. ret = devm_gpio_request_one(info->dev, info->gpio_nle,
  270. GPIOF_OUT_INIT_HIGH, "LATCH enable");
  271. if (ret)
  272. return ret;
  273. ret = devm_gpio_request_one(info->dev, info->gpio_nce,
  274. GPIOF_OUT_INIT_HIGH, "NAND nCE");
  275. if (ret)
  276. return ret;
  277. ret = devm_gpio_request_one(info->dev, info->gpio_nrw,
  278. GPIOF_OUT_INIT_HIGH, "NAND nRW");
  279. if (ret)
  280. return ret;
  281. ret = devm_gpio_request_one(info->dev, info->gpio_cle,
  282. GPIOF_OUT_INIT_LOW, "NAND CLE");
  283. if (ret)
  284. return ret;
  285. ret = devm_gpio_request_one(info->dev, info->gpio_ale,
  286. GPIOF_OUT_INIT_LOW, "NAND ALE");
  287. if (ret)
  288. return ret;
  289. ret = devm_gpio_request_one(info->dev, info->gpio_read,
  290. GPIOF_OUT_INIT_LOW, "NAND READ");
  291. if (ret)
  292. return ret;
  293. ret = devm_gpio_request_one(info->dev, info->gpio_rdy,
  294. GPIOF_IN, "NAND RDY");
  295. return ret;
  296. }
  297. static int rb91x_nand_probe(struct platform_device *pdev)
  298. {
  299. struct rb91x_nand_info *rbni;
  300. struct rb91x_nand_platform_data *pdata;
  301. struct mtd_info *mtd;
  302. int ret;
  303. pr_info(DRV_DESC "\n");
  304. pdata = dev_get_platdata(&pdev->dev);
  305. if (!pdata)
  306. return -EINVAL;
  307. rbni = devm_kzalloc(&pdev->dev, sizeof(*rbni), GFP_KERNEL);
  308. if (!rbni)
  309. return -ENOMEM;
  310. rbni->dev = &pdev->dev;
  311. rbni->gpio_nce = pdata->gpio_nce;
  312. rbni->gpio_ale = pdata->gpio_ale;
  313. rbni->gpio_cle = pdata->gpio_cle;
  314. rbni->gpio_read = pdata->gpio_read;
  315. rbni->gpio_nrw = pdata->gpio_nrw;
  316. rbni->gpio_rdy = pdata->gpio_rdy;
  317. rbni->gpio_nle = pdata->gpio_nle;
  318. rbni->chip.priv = &rbni;
  319. mtd = rbinfo_to_mtd(rbni);
  320. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  321. mtd->priv = &rbni->chip;
  322. #endif
  323. mtd->owner = THIS_MODULE;
  324. rbni->chip.cmd_ctrl = rb91x_nand_cmd_ctrl;
  325. rbni->chip.dev_ready = rb91x_nand_dev_ready;
  326. rbni->chip.read_byte = rb91x_nand_read_byte;
  327. rbni->chip.write_buf = rb91x_nand_write_buf;
  328. rbni->chip.read_buf = rb91x_nand_read_buf;
  329. rbni->chip.chip_delay = 25;
  330. rbni->chip.ecc.mode = NAND_ECC_SOFT;
  331. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0)
  332. rbni->chip.ecc.algo = NAND_ECC_HAMMING;
  333. #endif
  334. rbni->chip.options = NAND_NO_SUBPAGE_WRITE;
  335. platform_set_drvdata(pdev, rbni);
  336. ret = rb91x_nand_gpio_init(rbni);
  337. if (ret)
  338. return ret;
  339. ret = nand_scan_ident(mtd, 1, NULL);
  340. if (ret)
  341. return ret;
  342. if (mtd->writesize == 512)
  343. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  344. rbni->chip.ecc.layout = &rb91x_nand_ecclayout;
  345. #else
  346. mtd_set_ooblayout(mtd, &rb91x_nand_ecclayout_ops);
  347. #endif
  348. ret = nand_scan_tail(mtd);
  349. if (ret)
  350. return ret;
  351. ret = mtd_device_register(mtd, rb91x_nand_partitions,
  352. ARRAY_SIZE(rb91x_nand_partitions));
  353. if (ret)
  354. goto err_release_nand;
  355. return 0;
  356. err_release_nand:
  357. nand_release(&rbni->chip);
  358. return ret;
  359. }
  360. static int rb91x_nand_remove(struct platform_device *pdev)
  361. {
  362. struct rb91x_nand_info *info = platform_get_drvdata(pdev);
  363. nand_release(&rbni->chip);
  364. return 0;
  365. }
  366. static struct platform_driver rb91x_nand_driver = {
  367. .probe = rb91x_nand_probe,
  368. .remove = rb91x_nand_remove,
  369. .driver = {
  370. .name = RB91X_NAND_DRIVER_NAME,
  371. .owner = THIS_MODULE,
  372. },
  373. };
  374. module_platform_driver(rb91x_nand_driver);
  375. MODULE_DESCRIPTION(DRV_DESC);
  376. MODULE_VERSION(DRV_VERSION);
  377. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  378. MODULE_LICENSE("GPL v2");