1
0

rb750_nand.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * NAND flash driver for the MikroTik RouterBOARD 750
  3. *
  4. * Copyright (C) 2010-2012 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/module.h>
  13. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
  14. #include <linux/mtd/nand.h>
  15. #else
  16. #include <linux/mtd/rawnand.h>
  17. #endif
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/partitions.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/io.h>
  22. #include <linux/slab.h>
  23. #include <asm/mach-ath79/ar71xx_regs.h>
  24. #include <asm/mach-ath79/ath79.h>
  25. #include <asm/mach-ath79/mach-rb750.h>
  26. #define DRV_NAME "rb750-nand"
  27. #define DRV_VERSION "0.1.0"
  28. #define DRV_DESC "NAND flash driver for the RouterBOARD 750"
  29. #define RB750_NAND_IO0 BIT(RB750_GPIO_NAND_IO0)
  30. #define RB750_NAND_ALE BIT(RB750_GPIO_NAND_ALE)
  31. #define RB750_NAND_CLE BIT(RB750_GPIO_NAND_CLE)
  32. #define RB750_NAND_NRE BIT(RB750_GPIO_NAND_NRE)
  33. #define RB750_NAND_NWE BIT(RB750_GPIO_NAND_NWE)
  34. #define RB750_NAND_RDY BIT(RB750_GPIO_NAND_RDY)
  35. #define RB750_NAND_DATA_SHIFT 1
  36. #define RB750_NAND_DATA_BITS (0xff << RB750_NAND_DATA_SHIFT)
  37. #define RB750_NAND_INPUT_BITS (RB750_NAND_DATA_BITS | RB750_NAND_RDY)
  38. #define RB750_NAND_OUTPUT_BITS (RB750_NAND_ALE | RB750_NAND_CLE | \
  39. RB750_NAND_NRE | RB750_NAND_NWE)
  40. struct rb750_nand_info {
  41. struct nand_chip chip;
  42. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  43. struct mtd_info mtd;
  44. #endif
  45. struct rb7xx_nand_platform_data *pdata;
  46. };
  47. static inline struct rb750_nand_info *mtd_to_rbinfo(struct mtd_info *mtd)
  48. {
  49. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  50. return container_of(mtd, struct rb750_nand_info, mtd);
  51. #else
  52. struct nand_chip *chip = mtd_to_nand(mtd);
  53. return container_of(chip, struct rb750_nand_info, chip);
  54. #endif
  55. }
  56. static struct mtd_info *rbinfo_to_mtd(struct rb750_nand_info *nfc)
  57. {
  58. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  59. return &nfc->mtd;
  60. #else
  61. return nand_to_mtd(&nfc->chip);
  62. #endif
  63. }
  64. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  65. /*
  66. * We need to use the OLD Yaffs-1 OOB layout, otherwise the RB bootloader
  67. * will not be able to find the kernel that we load.
  68. */
  69. static struct nand_ecclayout rb750_nand_ecclayout = {
  70. .eccbytes = 6,
  71. .eccpos = { 8, 9, 10, 13, 14, 15 },
  72. .oobavail = 9,
  73. .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
  74. };
  75. #else
  76. static int rb750_ooblayout_ecc(struct mtd_info *mtd, int section,
  77. struct mtd_oob_region *oobregion)
  78. {
  79. switch (section) {
  80. case 0:
  81. oobregion->offset = 8;
  82. oobregion->length = 3;
  83. return 0;
  84. case 1:
  85. oobregion->offset = 13;
  86. oobregion->length = 3;
  87. return 0;
  88. default:
  89. return -ERANGE;
  90. }
  91. }
  92. static int rb750_ooblayout_free(struct mtd_info *mtd, int section,
  93. struct mtd_oob_region *oobregion)
  94. {
  95. switch (section) {
  96. case 0:
  97. oobregion->offset = 0;
  98. oobregion->length = 4;
  99. return 0;
  100. case 1:
  101. oobregion->offset = 4;
  102. oobregion->length = 1;
  103. return 0;
  104. case 2:
  105. oobregion->offset = 6;
  106. oobregion->length = 2;
  107. return 0;
  108. case 3:
  109. oobregion->offset = 11;
  110. oobregion->length = 2;
  111. return 0;
  112. default:
  113. return -ERANGE;
  114. }
  115. }
  116. static const struct mtd_ooblayout_ops rb750_nand_ecclayout_ops = {
  117. .ecc = rb750_ooblayout_ecc,
  118. .free = rb750_ooblayout_free,
  119. };
  120. #endif /* < 4.6 */
  121. static struct mtd_partition rb750_nand_partitions[] = {
  122. {
  123. .name = "booter",
  124. .offset = 0,
  125. .size = (256 * 1024),
  126. .mask_flags = MTD_WRITEABLE,
  127. }, {
  128. .name = "kernel",
  129. .offset = (256 * 1024),
  130. .size = (4 * 1024 * 1024) - (256 * 1024),
  131. }, {
  132. .name = "ubi",
  133. .offset = MTDPART_OFS_NXTBLK,
  134. .size = MTDPART_SIZ_FULL,
  135. },
  136. };
  137. static void rb750_nand_write(const u8 *buf, unsigned len)
  138. {
  139. void __iomem *base = ath79_gpio_base;
  140. u32 out;
  141. u32 t;
  142. unsigned i;
  143. /* set data lines to output mode */
  144. t = __raw_readl(base + AR71XX_GPIO_REG_OE);
  145. __raw_writel(t | RB750_NAND_DATA_BITS, base + AR71XX_GPIO_REG_OE);
  146. out = __raw_readl(base + AR71XX_GPIO_REG_OUT);
  147. out &= ~(RB750_NAND_DATA_BITS | RB750_NAND_NWE);
  148. for (i = 0; i != len; i++) {
  149. u32 data;
  150. data = buf[i];
  151. data <<= RB750_NAND_DATA_SHIFT;
  152. data |= out;
  153. __raw_writel(data, base + AR71XX_GPIO_REG_OUT);
  154. __raw_writel(data | RB750_NAND_NWE, base + AR71XX_GPIO_REG_OUT);
  155. /* flush write */
  156. __raw_readl(base + AR71XX_GPIO_REG_OUT);
  157. }
  158. /* set data lines to input mode */
  159. t = __raw_readl(base + AR71XX_GPIO_REG_OE);
  160. __raw_writel(t & ~RB750_NAND_DATA_BITS, base + AR71XX_GPIO_REG_OE);
  161. /* flush write */
  162. __raw_readl(base + AR71XX_GPIO_REG_OE);
  163. }
  164. static void rb750_nand_read(u8 *read_buf, unsigned len)
  165. {
  166. void __iomem *base = ath79_gpio_base;
  167. unsigned i;
  168. for (i = 0; i < len; i++) {
  169. u8 data;
  170. /* activate RE line */
  171. __raw_writel(RB750_NAND_NRE, base + AR71XX_GPIO_REG_CLEAR);
  172. /* flush write */
  173. __raw_readl(base + AR71XX_GPIO_REG_CLEAR);
  174. /* read input lines */
  175. data = __raw_readl(base + AR71XX_GPIO_REG_IN) >>
  176. RB750_NAND_DATA_SHIFT;
  177. /* deactivate RE line */
  178. __raw_writel(RB750_NAND_NRE, base + AR71XX_GPIO_REG_SET);
  179. read_buf[i] = data;
  180. }
  181. }
  182. static void rb750_nand_select_chip(struct mtd_info *mtd, int chip)
  183. {
  184. struct rb750_nand_info *rbinfo = mtd_to_rbinfo(mtd);
  185. void __iomem *base = ath79_gpio_base;
  186. u32 t;
  187. if (chip >= 0) {
  188. rbinfo->pdata->enable_pins();
  189. /* set input mode for data lines */
  190. t = __raw_readl(base + AR71XX_GPIO_REG_OE);
  191. __raw_writel(t & ~RB750_NAND_INPUT_BITS,
  192. base + AR71XX_GPIO_REG_OE);
  193. /* deactivate RE and WE lines */
  194. __raw_writel(RB750_NAND_NRE | RB750_NAND_NWE,
  195. base + AR71XX_GPIO_REG_SET);
  196. /* flush write */
  197. (void) __raw_readl(base + AR71XX_GPIO_REG_SET);
  198. /* activate CE line */
  199. __raw_writel(rbinfo->pdata->nce_line,
  200. base + AR71XX_GPIO_REG_CLEAR);
  201. } else {
  202. /* deactivate CE line */
  203. __raw_writel(rbinfo->pdata->nce_line,
  204. base + AR71XX_GPIO_REG_SET);
  205. /* flush write */
  206. (void) __raw_readl(base + AR71XX_GPIO_REG_SET);
  207. t = __raw_readl(base + AR71XX_GPIO_REG_OE);
  208. __raw_writel(t | RB750_NAND_IO0 | RB750_NAND_RDY,
  209. base + AR71XX_GPIO_REG_OE);
  210. rbinfo->pdata->disable_pins();
  211. }
  212. }
  213. static int rb750_nand_dev_ready(struct mtd_info *mtd)
  214. {
  215. void __iomem *base = ath79_gpio_base;
  216. return !!(__raw_readl(base + AR71XX_GPIO_REG_IN) & RB750_NAND_RDY);
  217. }
  218. static void rb750_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
  219. unsigned int ctrl)
  220. {
  221. if (ctrl & NAND_CTRL_CHANGE) {
  222. void __iomem *base = ath79_gpio_base;
  223. u32 t;
  224. t = __raw_readl(base + AR71XX_GPIO_REG_OUT);
  225. t &= ~(RB750_NAND_CLE | RB750_NAND_ALE);
  226. t |= (ctrl & NAND_CLE) ? RB750_NAND_CLE : 0;
  227. t |= (ctrl & NAND_ALE) ? RB750_NAND_ALE : 0;
  228. __raw_writel(t, base + AR71XX_GPIO_REG_OUT);
  229. /* flush write */
  230. __raw_readl(base + AR71XX_GPIO_REG_OUT);
  231. }
  232. if (cmd != NAND_CMD_NONE) {
  233. u8 t = cmd;
  234. rb750_nand_write(&t, 1);
  235. }
  236. }
  237. static u8 rb750_nand_read_byte(struct mtd_info *mtd)
  238. {
  239. u8 data = 0;
  240. rb750_nand_read(&data, 1);
  241. return data;
  242. }
  243. static void rb750_nand_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  244. {
  245. rb750_nand_read(buf, len);
  246. }
  247. static void rb750_nand_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  248. {
  249. rb750_nand_write(buf, len);
  250. }
  251. static void __init rb750_nand_gpio_init(struct rb750_nand_info *info)
  252. {
  253. void __iomem *base = ath79_gpio_base;
  254. u32 out;
  255. u32 t;
  256. out = __raw_readl(base + AR71XX_GPIO_REG_OUT);
  257. /* setup output levels */
  258. __raw_writel(RB750_NAND_NCE | RB750_NAND_NRE | RB750_NAND_NWE,
  259. base + AR71XX_GPIO_REG_SET);
  260. __raw_writel(RB750_NAND_ALE | RB750_NAND_CLE,
  261. base + AR71XX_GPIO_REG_CLEAR);
  262. /* setup input lines */
  263. t = __raw_readl(base + AR71XX_GPIO_REG_OE);
  264. __raw_writel(t & ~(RB750_NAND_INPUT_BITS), base + AR71XX_GPIO_REG_OE);
  265. /* setup output lines */
  266. t = __raw_readl(base + AR71XX_GPIO_REG_OE);
  267. t |= RB750_NAND_OUTPUT_BITS;
  268. t |= info->pdata->nce_line;
  269. __raw_writel(t, base + AR71XX_GPIO_REG_OE);
  270. info->pdata->latch_change(~out & RB750_NAND_IO0, out & RB750_NAND_IO0);
  271. }
  272. static int rb750_nand_probe(struct platform_device *pdev)
  273. {
  274. struct rb750_nand_info *info;
  275. struct rb7xx_nand_platform_data *pdata;
  276. struct mtd_info *mtd;
  277. int ret;
  278. printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
  279. pdata = pdev->dev.platform_data;
  280. if (!pdata)
  281. return -EINVAL;
  282. info = kzalloc(sizeof(*info), GFP_KERNEL);
  283. if (!info)
  284. return -ENOMEM;
  285. info->chip.priv = &info;
  286. mtd = rbinfo_to_mtd(info);
  287. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  288. mtd->priv = &info->chip;
  289. #endif
  290. mtd->owner = THIS_MODULE;
  291. info->chip.select_chip = rb750_nand_select_chip;
  292. info->chip.cmd_ctrl = rb750_nand_cmd_ctrl;
  293. info->chip.dev_ready = rb750_nand_dev_ready;
  294. info->chip.read_byte = rb750_nand_read_byte;
  295. info->chip.write_buf = rb750_nand_write_buf;
  296. info->chip.read_buf = rb750_nand_read_buf;
  297. info->chip.chip_delay = 25;
  298. info->chip.ecc.mode = NAND_ECC_SOFT;
  299. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0)
  300. info->chip.ecc.algo = NAND_ECC_HAMMING;
  301. #endif
  302. info->chip.options = NAND_NO_SUBPAGE_WRITE;
  303. info->pdata = pdata;
  304. platform_set_drvdata(pdev, info);
  305. rb750_nand_gpio_init(info);
  306. ret = nand_scan_ident(mtd, 1, NULL);
  307. if (ret) {
  308. ret = -ENXIO;
  309. goto err_free_info;
  310. }
  311. if (mtd->writesize == 512)
  312. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  313. info->chip.ecc.layout = &rb750_nand_ecclayout;
  314. #else
  315. mtd_set_ooblayout(mtd, &rb750_nand_ecclayout_ops);
  316. #endif
  317. ret = nand_scan_tail(mtd);
  318. if (ret) {
  319. return -ENXIO;
  320. goto err_set_drvdata;
  321. }
  322. ret = mtd_device_register(mtd, rb750_nand_partitions,
  323. ARRAY_SIZE(rb750_nand_partitions));
  324. if (ret)
  325. goto err_release_nand;
  326. return 0;
  327. err_release_nand:
  328. nand_release(&info->chip);
  329. err_set_drvdata:
  330. platform_set_drvdata(pdev, NULL);
  331. err_free_info:
  332. kfree(info);
  333. return ret;
  334. }
  335. static int rb750_nand_remove(struct platform_device *pdev)
  336. {
  337. struct rb750_nand_info *info = platform_get_drvdata(pdev);
  338. nand_release(&info->chip);
  339. platform_set_drvdata(pdev, NULL);
  340. kfree(info);
  341. return 0;
  342. }
  343. static struct platform_driver rb750_nand_driver = {
  344. .probe = rb750_nand_probe,
  345. .remove = rb750_nand_remove,
  346. .driver = {
  347. .name = DRV_NAME,
  348. .owner = THIS_MODULE,
  349. },
  350. };
  351. static int __init rb750_nand_init(void)
  352. {
  353. return platform_driver_register(&rb750_nand_driver);
  354. }
  355. static void __exit rb750_nand_exit(void)
  356. {
  357. platform_driver_unregister(&rb750_nand_driver);
  358. }
  359. module_init(rb750_nand_init);
  360. module_exit(rb750_nand_exit);
  361. MODULE_DESCRIPTION(DRV_DESC);
  362. MODULE_VERSION(DRV_VERSION);
  363. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  364. MODULE_LICENSE("GPL v2");