rb750_nand.c 8.5 KB

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