rb4xx_nand.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/mtd/nand.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/partitions.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/delay.h>
  22. #include <linux/io.h>
  23. #include <linux/gpio.h>
  24. #include <linux/slab.h>
  25. #include <asm/mach-ath79/ath79.h>
  26. #include <asm/mach-ath79/rb4xx_cpld.h>
  27. #define DRV_NAME "rb4xx-nand"
  28. #define DRV_VERSION "0.2.0"
  29. #define DRV_DESC "NAND flash driver for RouterBoard 4xx series"
  30. #define RB4XX_NAND_GPIO_READY 5
  31. #define RB4XX_NAND_GPIO_ALE 37
  32. #define RB4XX_NAND_GPIO_CLE 38
  33. #define RB4XX_NAND_GPIO_NCE 39
  34. struct rb4xx_nand_info {
  35. struct nand_chip chip;
  36. struct mtd_info mtd;
  37. };
  38. /*
  39. * We need to use the OLD Yaffs-1 OOB layout, otherwise the RB bootloader
  40. * will not be able to find the kernel that we load.
  41. */
  42. static struct nand_ecclayout rb4xx_nand_ecclayout = {
  43. .eccbytes = 6,
  44. .eccpos = { 8, 9, 10, 13, 14, 15 },
  45. .oobavail = 9,
  46. .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
  47. };
  48. static struct mtd_partition rb4xx_nand_partitions[] = {
  49. {
  50. .name = "booter",
  51. .offset = 0,
  52. .size = (256 * 1024),
  53. .mask_flags = MTD_WRITEABLE,
  54. },
  55. {
  56. .name = "kernel",
  57. .offset = (256 * 1024),
  58. .size = (4 * 1024 * 1024) - (256 * 1024),
  59. },
  60. {
  61. .name = "rootfs",
  62. .offset = MTDPART_OFS_NXTBLK,
  63. .size = MTDPART_SIZ_FULL,
  64. },
  65. };
  66. static int rb4xx_nand_dev_ready(struct mtd_info *mtd)
  67. {
  68. return gpio_get_value_cansleep(RB4XX_NAND_GPIO_READY);
  69. }
  70. static void rb4xx_nand_write_cmd(unsigned char cmd)
  71. {
  72. unsigned char data = cmd;
  73. int err;
  74. err = rb4xx_cpld_write(&data, 1);
  75. if (err)
  76. pr_err("rb4xx_nand: write cmd failed, err=%d\n", err);
  77. }
  78. static void rb4xx_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
  79. unsigned int ctrl)
  80. {
  81. if (ctrl & NAND_CTRL_CHANGE) {
  82. gpio_set_value_cansleep(RB4XX_NAND_GPIO_CLE,
  83. (ctrl & NAND_CLE) ? 1 : 0);
  84. gpio_set_value_cansleep(RB4XX_NAND_GPIO_ALE,
  85. (ctrl & NAND_ALE) ? 1 : 0);
  86. gpio_set_value_cansleep(RB4XX_NAND_GPIO_NCE,
  87. (ctrl & NAND_NCE) ? 0 : 1);
  88. }
  89. if (cmd != NAND_CMD_NONE)
  90. rb4xx_nand_write_cmd(cmd);
  91. }
  92. static unsigned char rb4xx_nand_read_byte(struct mtd_info *mtd)
  93. {
  94. unsigned char data = 0;
  95. int err;
  96. err = rb4xx_cpld_read(&data, NULL, 1);
  97. if (err) {
  98. pr_err("rb4xx_nand: read data failed, err=%d\n", err);
  99. data = 0xff;
  100. }
  101. return data;
  102. }
  103. static void rb4xx_nand_write_buf(struct mtd_info *mtd, const unsigned char *buf,
  104. int len)
  105. {
  106. int err;
  107. err = rb4xx_cpld_write(buf, len);
  108. if (err)
  109. pr_err("rb4xx_nand: write buf failed, err=%d\n", err);
  110. }
  111. static void rb4xx_nand_read_buf(struct mtd_info *mtd, unsigned char *buf,
  112. int len)
  113. {
  114. int err;
  115. err = rb4xx_cpld_read(buf, NULL, len);
  116. if (err)
  117. pr_err("rb4xx_nand: read buf failed, err=%d\n", err);
  118. }
  119. static int rb4xx_nand_probe(struct platform_device *pdev)
  120. {
  121. struct rb4xx_nand_info *info;
  122. int ret;
  123. printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
  124. ret = gpio_request(RB4XX_NAND_GPIO_READY, "NAND RDY");
  125. if (ret) {
  126. dev_err(&pdev->dev, "unable to request gpio %d\n",
  127. RB4XX_NAND_GPIO_READY);
  128. goto err;
  129. }
  130. ret = gpio_direction_input(RB4XX_NAND_GPIO_READY);
  131. if (ret) {
  132. dev_err(&pdev->dev, "unable to set input mode on gpio %d\n",
  133. RB4XX_NAND_GPIO_READY);
  134. goto err_free_gpio_ready;
  135. }
  136. ret = gpio_request(RB4XX_NAND_GPIO_ALE, "NAND ALE");
  137. if (ret) {
  138. dev_err(&pdev->dev, "unable to request gpio %d\n",
  139. RB4XX_NAND_GPIO_ALE);
  140. goto err_free_gpio_ready;
  141. }
  142. ret = gpio_direction_output(RB4XX_NAND_GPIO_ALE, 0);
  143. if (ret) {
  144. dev_err(&pdev->dev, "unable to set output mode on gpio %d\n",
  145. RB4XX_NAND_GPIO_ALE);
  146. goto err_free_gpio_ale;
  147. }
  148. ret = gpio_request(RB4XX_NAND_GPIO_CLE, "NAND CLE");
  149. if (ret) {
  150. dev_err(&pdev->dev, "unable to request gpio %d\n",
  151. RB4XX_NAND_GPIO_CLE);
  152. goto err_free_gpio_ale;
  153. }
  154. ret = gpio_direction_output(RB4XX_NAND_GPIO_CLE, 0);
  155. if (ret) {
  156. dev_err(&pdev->dev, "unable to set output mode on gpio %d\n",
  157. RB4XX_NAND_GPIO_CLE);
  158. goto err_free_gpio_cle;
  159. }
  160. ret = gpio_request(RB4XX_NAND_GPIO_NCE, "NAND NCE");
  161. if (ret) {
  162. dev_err(&pdev->dev, "unable to request gpio %d\n",
  163. RB4XX_NAND_GPIO_NCE);
  164. goto err_free_gpio_cle;
  165. }
  166. ret = gpio_direction_output(RB4XX_NAND_GPIO_NCE, 1);
  167. if (ret) {
  168. dev_err(&pdev->dev, "unable to set output mode on gpio %d\n",
  169. RB4XX_NAND_GPIO_ALE);
  170. goto err_free_gpio_nce;
  171. }
  172. info = kzalloc(sizeof(*info), GFP_KERNEL);
  173. if (!info) {
  174. dev_err(&pdev->dev, "rb4xx-nand: no memory for private data\n");
  175. ret = -ENOMEM;
  176. goto err_free_gpio_nce;
  177. }
  178. info->chip.priv = &info;
  179. info->mtd.priv = &info->chip;
  180. info->mtd.owner = THIS_MODULE;
  181. info->chip.cmd_ctrl = rb4xx_nand_cmd_ctrl;
  182. info->chip.dev_ready = rb4xx_nand_dev_ready;
  183. info->chip.read_byte = rb4xx_nand_read_byte;
  184. info->chip.write_buf = rb4xx_nand_write_buf;
  185. info->chip.read_buf = rb4xx_nand_read_buf;
  186. info->chip.chip_delay = 25;
  187. info->chip.ecc.mode = NAND_ECC_SOFT;
  188. platform_set_drvdata(pdev, info);
  189. ret = nand_scan_ident(&info->mtd, 1, NULL);
  190. if (ret) {
  191. ret = -ENXIO;
  192. goto err_free_info;
  193. }
  194. if (info->mtd.writesize == 512)
  195. info->chip.ecc.layout = &rb4xx_nand_ecclayout;
  196. ret = nand_scan_tail(&info->mtd);
  197. if (ret) {
  198. return -ENXIO;
  199. goto err_set_drvdata;
  200. }
  201. mtd_device_register(&info->mtd, rb4xx_nand_partitions,
  202. ARRAY_SIZE(rb4xx_nand_partitions));
  203. if (ret)
  204. goto err_release_nand;
  205. return 0;
  206. err_release_nand:
  207. nand_release(&info->mtd);
  208. err_set_drvdata:
  209. platform_set_drvdata(pdev, NULL);
  210. err_free_info:
  211. kfree(info);
  212. err_free_gpio_nce:
  213. gpio_free(RB4XX_NAND_GPIO_NCE);
  214. err_free_gpio_cle:
  215. gpio_free(RB4XX_NAND_GPIO_CLE);
  216. err_free_gpio_ale:
  217. gpio_free(RB4XX_NAND_GPIO_ALE);
  218. err_free_gpio_ready:
  219. gpio_free(RB4XX_NAND_GPIO_READY);
  220. err:
  221. return ret;
  222. }
  223. static int rb4xx_nand_remove(struct platform_device *pdev)
  224. {
  225. struct rb4xx_nand_info *info = platform_get_drvdata(pdev);
  226. nand_release(&info->mtd);
  227. platform_set_drvdata(pdev, NULL);
  228. kfree(info);
  229. gpio_free(RB4XX_NAND_GPIO_NCE);
  230. gpio_free(RB4XX_NAND_GPIO_CLE);
  231. gpio_free(RB4XX_NAND_GPIO_ALE);
  232. gpio_free(RB4XX_NAND_GPIO_READY);
  233. return 0;
  234. }
  235. static struct platform_driver rb4xx_nand_driver = {
  236. .probe = rb4xx_nand_probe,
  237. .remove = rb4xx_nand_remove,
  238. .driver = {
  239. .name = DRV_NAME,
  240. .owner = THIS_MODULE,
  241. },
  242. };
  243. static int __init rb4xx_nand_init(void)
  244. {
  245. return platform_driver_register(&rb4xx_nand_driver);
  246. }
  247. static void __exit rb4xx_nand_exit(void)
  248. {
  249. platform_driver_unregister(&rb4xx_nand_driver);
  250. }
  251. module_init(rb4xx_nand_init);
  252. module_exit(rb4xx_nand_exit);
  253. MODULE_DESCRIPTION(DRV_DESC);
  254. MODULE_VERSION(DRV_VERSION);
  255. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  256. MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org>");
  257. MODULE_LICENSE("GPL v2");