1
0

rb4xx_nand.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * NAND flash driver for the MikroTik RouterBoard 4xx series
  3. *
  4. * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
  5. * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
  6. *
  7. * This file was based on the driver for Linux 2.6.22 published by
  8. * MikroTik for their RouterBoard 4xx series devices.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published
  12. * by the Free Software Foundation.
  13. */
  14. #include <linux/version.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
  19. #include <linux/mtd/nand.h>
  20. #else
  21. #include <linux/mtd/rawnand.h>
  22. #endif
  23. #include <linux/mtd/mtd.h>
  24. #include <linux/mtd/partitions.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/delay.h>
  27. #include <linux/io.h>
  28. #include <linux/gpio.h>
  29. #include <linux/slab.h>
  30. #include <asm/mach-ath79/ath79.h>
  31. #include <asm/mach-ath79/rb4xx_cpld.h>
  32. #define DRV_NAME "rb4xx-nand"
  33. #define DRV_VERSION "0.2.0"
  34. #define DRV_DESC "NAND flash driver for RouterBoard 4xx series"
  35. #define RB4XX_NAND_GPIO_READY 5
  36. #define RB4XX_NAND_GPIO_ALE 37
  37. #define RB4XX_NAND_GPIO_CLE 38
  38. #define RB4XX_NAND_GPIO_NCE 39
  39. struct rb4xx_nand_info {
  40. struct nand_chip chip;
  41. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  42. struct mtd_info mtd;
  43. #endif
  44. };
  45. static inline struct rb4xx_nand_info *mtd_to_rbinfo(struct mtd_info *mtd)
  46. {
  47. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  48. return container_of(mtd, struct rb4xx_nand_info, mtd);
  49. #else
  50. struct nand_chip *chip = mtd_to_nand(mtd);
  51. return container_of(chip, struct rb4xx_nand_info, chip);
  52. #endif
  53. }
  54. static struct mtd_info *rbinfo_to_mtd(struct rb4xx_nand_info *nfc)
  55. {
  56. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  57. return &nfc->mtd;
  58. #else
  59. return nand_to_mtd(&nfc->chip);
  60. #endif
  61. }
  62. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  63. /*
  64. * We need to use the OLD Yaffs-1 OOB layout, otherwise the RB bootloader
  65. * will not be able to find the kernel that we load.
  66. */
  67. static struct nand_ecclayout rb4xx_nand_ecclayout = {
  68. .eccbytes = 6,
  69. .eccpos = { 8, 9, 10, 13, 14, 15 },
  70. .oobavail = 9,
  71. .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
  72. };
  73. #else
  74. static int rb4xx_ooblayout_ecc(struct mtd_info *mtd, int section,
  75. struct mtd_oob_region *oobregion)
  76. {
  77. switch (section) {
  78. case 0:
  79. oobregion->offset = 8;
  80. oobregion->length = 3;
  81. return 0;
  82. case 1:
  83. oobregion->offset = 13;
  84. oobregion->length = 3;
  85. return 0;
  86. default:
  87. return -ERANGE;
  88. }
  89. }
  90. static int rb4xx_ooblayout_free(struct mtd_info *mtd, int section,
  91. struct mtd_oob_region *oobregion)
  92. {
  93. switch (section) {
  94. case 0:
  95. oobregion->offset = 0;
  96. oobregion->length = 4;
  97. return 0;
  98. case 1:
  99. oobregion->offset = 4;
  100. oobregion->length = 1;
  101. return 0;
  102. case 2:
  103. oobregion->offset = 6;
  104. oobregion->length = 2;
  105. return 0;
  106. case 3:
  107. oobregion->offset = 11;
  108. oobregion->length = 2;
  109. return 0;
  110. default:
  111. return -ERANGE;
  112. }
  113. }
  114. static const struct mtd_ooblayout_ops rb4xx_nand_ecclayout_ops = {
  115. .ecc = rb4xx_ooblayout_ecc,
  116. .free = rb4xx_ooblayout_free,
  117. };
  118. #endif /* < 4.6 */
  119. static struct mtd_partition rb4xx_nand_partitions[] = {
  120. {
  121. .name = "booter",
  122. .offset = 0,
  123. .size = (256 * 1024),
  124. .mask_flags = MTD_WRITEABLE,
  125. },
  126. {
  127. .name = "kernel",
  128. .offset = (256 * 1024),
  129. .size = (4 * 1024 * 1024) - (256 * 1024),
  130. },
  131. {
  132. .name = "ubi",
  133. .offset = MTDPART_OFS_NXTBLK,
  134. .size = MTDPART_SIZ_FULL,
  135. },
  136. };
  137. static int rb4xx_nand_dev_ready(struct mtd_info *mtd)
  138. {
  139. return gpio_get_value_cansleep(RB4XX_NAND_GPIO_READY);
  140. }
  141. static void rb4xx_nand_write_cmd(unsigned char cmd)
  142. {
  143. unsigned char data = cmd;
  144. int err;
  145. err = rb4xx_cpld_write(&data, 1);
  146. if (err)
  147. pr_err("rb4xx_nand: write cmd failed, err=%d\n", err);
  148. }
  149. static void rb4xx_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
  150. unsigned int ctrl)
  151. {
  152. if (ctrl & NAND_CTRL_CHANGE) {
  153. gpio_set_value_cansleep(RB4XX_NAND_GPIO_CLE,
  154. (ctrl & NAND_CLE) ? 1 : 0);
  155. gpio_set_value_cansleep(RB4XX_NAND_GPIO_ALE,
  156. (ctrl & NAND_ALE) ? 1 : 0);
  157. gpio_set_value_cansleep(RB4XX_NAND_GPIO_NCE,
  158. (ctrl & NAND_NCE) ? 0 : 1);
  159. }
  160. if (cmd != NAND_CMD_NONE)
  161. rb4xx_nand_write_cmd(cmd);
  162. }
  163. static unsigned char rb4xx_nand_read_byte(struct mtd_info *mtd)
  164. {
  165. unsigned char data = 0;
  166. int err;
  167. err = rb4xx_cpld_read(&data, 1);
  168. if (err) {
  169. pr_err("rb4xx_nand: read data failed, err=%d\n", err);
  170. data = 0xff;
  171. }
  172. return data;
  173. }
  174. static void rb4xx_nand_write_buf(struct mtd_info *mtd, const unsigned char *buf,
  175. int len)
  176. {
  177. int err;
  178. err = rb4xx_cpld_write(buf, len);
  179. if (err)
  180. pr_err("rb4xx_nand: write buf failed, err=%d\n", err);
  181. }
  182. static void rb4xx_nand_read_buf(struct mtd_info *mtd, unsigned char *buf,
  183. int len)
  184. {
  185. int err;
  186. err = rb4xx_cpld_read(buf, len);
  187. if (err)
  188. pr_err("rb4xx_nand: read buf failed, err=%d\n", err);
  189. }
  190. static int rb4xx_nand_probe(struct platform_device *pdev)
  191. {
  192. struct rb4xx_nand_info *info;
  193. struct mtd_info *mtd;
  194. int ret;
  195. printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
  196. ret = gpio_request(RB4XX_NAND_GPIO_READY, "NAND RDY");
  197. if (ret) {
  198. dev_err(&pdev->dev, "unable to request gpio %d\n",
  199. RB4XX_NAND_GPIO_READY);
  200. goto err;
  201. }
  202. ret = gpio_direction_input(RB4XX_NAND_GPIO_READY);
  203. if (ret) {
  204. dev_err(&pdev->dev, "unable to set input mode on gpio %d\n",
  205. RB4XX_NAND_GPIO_READY);
  206. goto err_free_gpio_ready;
  207. }
  208. ret = gpio_request(RB4XX_NAND_GPIO_ALE, "NAND ALE");
  209. if (ret) {
  210. dev_err(&pdev->dev, "unable to request gpio %d\n",
  211. RB4XX_NAND_GPIO_ALE);
  212. goto err_free_gpio_ready;
  213. }
  214. ret = gpio_direction_output(RB4XX_NAND_GPIO_ALE, 0);
  215. if (ret) {
  216. dev_err(&pdev->dev, "unable to set output mode on gpio %d\n",
  217. RB4XX_NAND_GPIO_ALE);
  218. goto err_free_gpio_ale;
  219. }
  220. ret = gpio_request(RB4XX_NAND_GPIO_CLE, "NAND CLE");
  221. if (ret) {
  222. dev_err(&pdev->dev, "unable to request gpio %d\n",
  223. RB4XX_NAND_GPIO_CLE);
  224. goto err_free_gpio_ale;
  225. }
  226. ret = gpio_direction_output(RB4XX_NAND_GPIO_CLE, 0);
  227. if (ret) {
  228. dev_err(&pdev->dev, "unable to set output mode on gpio %d\n",
  229. RB4XX_NAND_GPIO_CLE);
  230. goto err_free_gpio_cle;
  231. }
  232. ret = gpio_request(RB4XX_NAND_GPIO_NCE, "NAND NCE");
  233. if (ret) {
  234. dev_err(&pdev->dev, "unable to request gpio %d\n",
  235. RB4XX_NAND_GPIO_NCE);
  236. goto err_free_gpio_cle;
  237. }
  238. ret = gpio_direction_output(RB4XX_NAND_GPIO_NCE, 1);
  239. if (ret) {
  240. dev_err(&pdev->dev, "unable to set output mode on gpio %d\n",
  241. RB4XX_NAND_GPIO_ALE);
  242. goto err_free_gpio_nce;
  243. }
  244. info = kzalloc(sizeof(*info), GFP_KERNEL);
  245. if (!info) {
  246. dev_err(&pdev->dev, "rb4xx-nand: no memory for private data\n");
  247. ret = -ENOMEM;
  248. goto err_free_gpio_nce;
  249. }
  250. info->chip.priv = &info;
  251. mtd = rbinfo_to_mtd(info);
  252. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  253. mtd->priv = &info->chip;
  254. #endif
  255. mtd->owner = THIS_MODULE;
  256. info->chip.cmd_ctrl = rb4xx_nand_cmd_ctrl;
  257. info->chip.dev_ready = rb4xx_nand_dev_ready;
  258. info->chip.read_byte = rb4xx_nand_read_byte;
  259. info->chip.write_buf = rb4xx_nand_write_buf;
  260. info->chip.read_buf = rb4xx_nand_read_buf;
  261. info->chip.chip_delay = 25;
  262. info->chip.ecc.mode = NAND_ECC_SOFT;
  263. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0)
  264. info->chip.ecc.algo = NAND_ECC_HAMMING;
  265. #endif
  266. info->chip.options = NAND_NO_SUBPAGE_WRITE;
  267. platform_set_drvdata(pdev, info);
  268. ret = nand_scan_ident(mtd, 1, NULL);
  269. if (ret) {
  270. ret = -ENXIO;
  271. goto err_free_info;
  272. }
  273. if (mtd->writesize == 512)
  274. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  275. info->chip.ecc.layout = &rb4xx_nand_ecclayout;
  276. #else
  277. mtd_set_ooblayout(mtd, &rb4xx_nand_ecclayout_ops);
  278. #endif
  279. ret = nand_scan_tail(mtd);
  280. if (ret) {
  281. return -ENXIO;
  282. goto err_set_drvdata;
  283. }
  284. mtd_device_register(mtd, rb4xx_nand_partitions,
  285. ARRAY_SIZE(rb4xx_nand_partitions));
  286. if (ret)
  287. goto err_release_nand;
  288. return 0;
  289. err_release_nand:
  290. nand_release(&info->chip);
  291. err_set_drvdata:
  292. platform_set_drvdata(pdev, NULL);
  293. err_free_info:
  294. kfree(info);
  295. err_free_gpio_nce:
  296. gpio_free(RB4XX_NAND_GPIO_NCE);
  297. err_free_gpio_cle:
  298. gpio_free(RB4XX_NAND_GPIO_CLE);
  299. err_free_gpio_ale:
  300. gpio_free(RB4XX_NAND_GPIO_ALE);
  301. err_free_gpio_ready:
  302. gpio_free(RB4XX_NAND_GPIO_READY);
  303. err:
  304. return ret;
  305. }
  306. static int rb4xx_nand_remove(struct platform_device *pdev)
  307. {
  308. struct rb4xx_nand_info *info = platform_get_drvdata(pdev);
  309. nand_release(&info->chip));
  310. platform_set_drvdata(pdev, NULL);
  311. kfree(info);
  312. gpio_free(RB4XX_NAND_GPIO_NCE);
  313. gpio_free(RB4XX_NAND_GPIO_CLE);
  314. gpio_free(RB4XX_NAND_GPIO_ALE);
  315. gpio_free(RB4XX_NAND_GPIO_READY);
  316. return 0;
  317. }
  318. static struct platform_driver rb4xx_nand_driver = {
  319. .probe = rb4xx_nand_probe,
  320. .remove = rb4xx_nand_remove,
  321. .driver = {
  322. .name = DRV_NAME,
  323. .owner = THIS_MODULE,
  324. },
  325. };
  326. static int __init rb4xx_nand_init(void)
  327. {
  328. return platform_driver_register(&rb4xx_nand_driver);
  329. }
  330. static void __exit rb4xx_nand_exit(void)
  331. {
  332. platform_driver_unregister(&rb4xx_nand_driver);
  333. }
  334. module_init(rb4xx_nand_init);
  335. module_exit(rb4xx_nand_exit);
  336. MODULE_DESCRIPTION(DRV_DESC);
  337. MODULE_VERSION(DRV_VERSION);
  338. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  339. MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org>");
  340. MODULE_LICENSE("GPL v2");