rb91x_nand.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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/kernel.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/module.h>
  13. #include <linux/mtd/nand.h>
  14. #include <linux/mtd/mtd.h>
  15. #include <linux/mtd/partitions.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/io.h>
  18. #include <linux/slab.h>
  19. #include <linux/gpio.h>
  20. #include <linux/platform_data/rb91x_nand.h>
  21. #include <asm/mach-ath79/ar71xx_regs.h>
  22. #include <asm/mach-ath79/ath79.h>
  23. #define DRV_DESC "NAND flash driver for the RouterBOARD 91x series"
  24. #define RB91X_NAND_NRWE BIT(12)
  25. #define RB91X_NAND_DATA_BITS (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4) |\
  26. BIT(13) | BIT(14) | BIT(15))
  27. #define RB91X_NAND_INPUT_BITS (RB91X_NAND_DATA_BITS | RB91X_NAND_RDY)
  28. #define RB91X_NAND_OUTPUT_BITS (RB91X_NAND_DATA_BITS | RB91X_NAND_NRWE)
  29. #define RB91X_NAND_LOW_DATA_MASK 0x1f
  30. #define RB91X_NAND_HIGH_DATA_MASK 0xe0
  31. #define RB91X_NAND_HIGH_DATA_SHIFT 8
  32. struct rb91x_nand_info {
  33. struct nand_chip chip;
  34. struct mtd_info mtd;
  35. struct device *dev;
  36. int gpio_nce;
  37. int gpio_ale;
  38. int gpio_cle;
  39. int gpio_rdy;
  40. int gpio_read;
  41. int gpio_nrw;
  42. int gpio_nle;
  43. };
  44. static inline struct rb91x_nand_info *mtd_to_rbinfo(struct mtd_info *mtd)
  45. {
  46. return container_of(mtd, struct rb91x_nand_info, mtd);
  47. }
  48. /*
  49. * We need to use the OLD Yaffs-1 OOB layout, otherwise the RB bootloader
  50. * will not be able to find the kernel that we load.
  51. */
  52. static struct nand_ecclayout rb91x_nand_ecclayout = {
  53. .eccbytes = 6,
  54. .eccpos = { 8, 9, 10, 13, 14, 15 },
  55. .oobavail = 9,
  56. .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
  57. };
  58. static struct mtd_partition rb91x_nand_partitions[] = {
  59. {
  60. .name = "booter",
  61. .offset = 0,
  62. .size = (256 * 1024),
  63. .mask_flags = MTD_WRITEABLE,
  64. }, {
  65. .name = "kernel",
  66. .offset = (256 * 1024),
  67. .size = (4 * 1024 * 1024) - (256 * 1024),
  68. }, {
  69. .name = "rootfs",
  70. .offset = MTDPART_OFS_NXTBLK,
  71. .size = MTDPART_SIZ_FULL,
  72. },
  73. };
  74. static void rb91x_nand_write(struct rb91x_nand_info *rbni,
  75. const u8 *buf,
  76. unsigned len)
  77. {
  78. void __iomem *base = ath79_gpio_base;
  79. u32 oe_reg;
  80. u32 out_reg;
  81. u32 out;
  82. unsigned i;
  83. /* enable the latch */
  84. gpio_set_value_cansleep(rbni->gpio_nle, 0);
  85. oe_reg = __raw_readl(base + AR71XX_GPIO_REG_OE);
  86. out_reg = __raw_readl(base + AR71XX_GPIO_REG_OUT);
  87. /* set data lines to output mode */
  88. __raw_writel(oe_reg & ~(RB91X_NAND_DATA_BITS | RB91X_NAND_NRWE),
  89. base + AR71XX_GPIO_REG_OE);
  90. out = out_reg & ~(RB91X_NAND_DATA_BITS | RB91X_NAND_NRWE);
  91. for (i = 0; i != len; i++) {
  92. u32 data;
  93. data = (buf[i] & RB91X_NAND_HIGH_DATA_MASK) <<
  94. RB91X_NAND_HIGH_DATA_SHIFT;
  95. data |= buf[i] & RB91X_NAND_LOW_DATA_MASK;
  96. data |= out;
  97. __raw_writel(data, base + AR71XX_GPIO_REG_OUT);
  98. /* deactivate WE line */
  99. data |= RB91X_NAND_NRWE;
  100. __raw_writel(data, base + AR71XX_GPIO_REG_OUT);
  101. /* flush write */
  102. __raw_readl(base + AR71XX_GPIO_REG_OUT);
  103. }
  104. /* restore registers */
  105. __raw_writel(out_reg, base + AR71XX_GPIO_REG_OUT);
  106. __raw_writel(oe_reg, base + AR71XX_GPIO_REG_OE);
  107. /* flush write */
  108. __raw_readl(base + AR71XX_GPIO_REG_OUT);
  109. /* disable the latch */
  110. gpio_set_value_cansleep(rbni->gpio_nle, 1);
  111. }
  112. static void rb91x_nand_read(struct rb91x_nand_info *rbni,
  113. u8 *read_buf,
  114. unsigned len)
  115. {
  116. void __iomem *base = ath79_gpio_base;
  117. u32 oe_reg;
  118. u32 out_reg;
  119. unsigned i;
  120. /* enable read mode */
  121. gpio_set_value_cansleep(rbni->gpio_read, 1);
  122. /* enable latch */
  123. gpio_set_value_cansleep(rbni->gpio_nle, 0);
  124. /* save registers */
  125. oe_reg = __raw_readl(base + AR71XX_GPIO_REG_OE);
  126. out_reg = __raw_readl(base + AR71XX_GPIO_REG_OUT);
  127. /* set data lines to input mode */
  128. __raw_writel(oe_reg | RB91X_NAND_DATA_BITS,
  129. base + AR71XX_GPIO_REG_OE);
  130. for (i = 0; i < len; i++) {
  131. u32 in;
  132. u8 data;
  133. /* activate RE line */
  134. __raw_writel(RB91X_NAND_NRWE, base + AR71XX_GPIO_REG_CLEAR);
  135. /* flush write */
  136. __raw_readl(base + AR71XX_GPIO_REG_CLEAR);
  137. /* read input lines */
  138. in = __raw_readl(base + AR71XX_GPIO_REG_IN);
  139. /* deactivate RE line */
  140. __raw_writel(RB91X_NAND_NRWE, base + AR71XX_GPIO_REG_SET);
  141. data = (in & RB91X_NAND_LOW_DATA_MASK);
  142. data |= (in >> RB91X_NAND_HIGH_DATA_SHIFT) &
  143. RB91X_NAND_HIGH_DATA_MASK;
  144. read_buf[i] = data;
  145. }
  146. /* restore registers */
  147. __raw_writel(out_reg, base + AR71XX_GPIO_REG_OUT);
  148. __raw_writel(oe_reg, base + AR71XX_GPIO_REG_OE);
  149. /* flush write */
  150. __raw_readl(base + AR71XX_GPIO_REG_OUT);
  151. /* disable latch */
  152. gpio_set_value_cansleep(rbni->gpio_nle, 1);
  153. /* disable read mode */
  154. gpio_set_value_cansleep(rbni->gpio_read, 0);
  155. }
  156. static int rb91x_nand_dev_ready(struct mtd_info *mtd)
  157. {
  158. struct rb91x_nand_info *rbni = mtd_to_rbinfo(mtd);
  159. return gpio_get_value_cansleep(rbni->gpio_rdy);
  160. }
  161. static void rb91x_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
  162. unsigned int ctrl)
  163. {
  164. struct rb91x_nand_info *rbni = mtd_to_rbinfo(mtd);
  165. if (ctrl & NAND_CTRL_CHANGE) {
  166. gpio_set_value_cansleep(rbni->gpio_cle,
  167. (ctrl & NAND_CLE) ? 1 : 0);
  168. gpio_set_value_cansleep(rbni->gpio_ale,
  169. (ctrl & NAND_ALE) ? 1 : 0);
  170. gpio_set_value_cansleep(rbni->gpio_nce,
  171. (ctrl & NAND_NCE) ? 0 : 1);
  172. }
  173. if (cmd != NAND_CMD_NONE) {
  174. u8 t = cmd;
  175. rb91x_nand_write(rbni, &t, 1);
  176. }
  177. }
  178. static u8 rb91x_nand_read_byte(struct mtd_info *mtd)
  179. {
  180. struct rb91x_nand_info *rbni = mtd_to_rbinfo(mtd);
  181. u8 data = 0xff;
  182. rb91x_nand_read(rbni, &data, 1);
  183. return data;
  184. }
  185. static void rb91x_nand_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  186. {
  187. struct rb91x_nand_info *rbni = mtd_to_rbinfo(mtd);
  188. rb91x_nand_read(rbni, buf, len);
  189. }
  190. static void rb91x_nand_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  191. {
  192. struct rb91x_nand_info *rbni = mtd_to_rbinfo(mtd);
  193. rb91x_nand_write(rbni, buf, len);
  194. }
  195. static int rb91x_nand_gpio_init(struct rb91x_nand_info *info)
  196. {
  197. int ret;
  198. /*
  199. * Ensure that the LATCH is disabled before initializing
  200. * control lines.
  201. */
  202. ret = devm_gpio_request_one(info->dev, info->gpio_nle,
  203. GPIOF_OUT_INIT_HIGH, "LATCH enable");
  204. if (ret)
  205. return ret;
  206. ret = devm_gpio_request_one(info->dev, info->gpio_nce,
  207. GPIOF_OUT_INIT_HIGH, "NAND nCE");
  208. if (ret)
  209. return ret;
  210. ret = devm_gpio_request_one(info->dev, info->gpio_nrw,
  211. GPIOF_OUT_INIT_HIGH, "NAND nRW");
  212. if (ret)
  213. return ret;
  214. ret = devm_gpio_request_one(info->dev, info->gpio_cle,
  215. GPIOF_OUT_INIT_LOW, "NAND CLE");
  216. if (ret)
  217. return ret;
  218. ret = devm_gpio_request_one(info->dev, info->gpio_ale,
  219. GPIOF_OUT_INIT_LOW, "NAND ALE");
  220. if (ret)
  221. return ret;
  222. ret = devm_gpio_request_one(info->dev, info->gpio_read,
  223. GPIOF_OUT_INIT_LOW, "NAND READ");
  224. if (ret)
  225. return ret;
  226. ret = devm_gpio_request_one(info->dev, info->gpio_rdy,
  227. GPIOF_IN, "NAND RDY");
  228. return ret;
  229. }
  230. static int rb91x_nand_probe(struct platform_device *pdev)
  231. {
  232. struct rb91x_nand_info *rbni;
  233. struct rb91x_nand_platform_data *pdata;
  234. int ret;
  235. pr_info(DRV_DESC "\n");
  236. pdata = dev_get_platdata(&pdev->dev);
  237. if (!pdata)
  238. return -EINVAL;
  239. rbni = devm_kzalloc(&pdev->dev, sizeof(*rbni), GFP_KERNEL);
  240. if (!rbni)
  241. return -ENOMEM;
  242. rbni->dev = &pdev->dev;
  243. rbni->gpio_nce = pdata->gpio_nce;
  244. rbni->gpio_ale = pdata->gpio_ale;
  245. rbni->gpio_cle = pdata->gpio_cle;
  246. rbni->gpio_read = pdata->gpio_read;
  247. rbni->gpio_nrw = pdata->gpio_nrw;
  248. rbni->gpio_rdy = pdata->gpio_rdy;
  249. rbni->gpio_nle = pdata->gpio_nle;
  250. rbni->chip.priv = &rbni;
  251. rbni->mtd.priv = &rbni->chip;
  252. rbni->mtd.owner = THIS_MODULE;
  253. rbni->chip.cmd_ctrl = rb91x_nand_cmd_ctrl;
  254. rbni->chip.dev_ready = rb91x_nand_dev_ready;
  255. rbni->chip.read_byte = rb91x_nand_read_byte;
  256. rbni->chip.write_buf = rb91x_nand_write_buf;
  257. rbni->chip.read_buf = rb91x_nand_read_buf;
  258. rbni->chip.chip_delay = 25;
  259. rbni->chip.ecc.mode = NAND_ECC_SOFT;
  260. platform_set_drvdata(pdev, rbni);
  261. ret = rb91x_nand_gpio_init(rbni);
  262. if (ret)
  263. return ret;
  264. ret = nand_scan_ident(&rbni->mtd, 1, NULL);
  265. if (ret)
  266. return ret;
  267. if (rbni->mtd.writesize == 512)
  268. rbni->chip.ecc.layout = &rb91x_nand_ecclayout;
  269. ret = nand_scan_tail(&rbni->mtd);
  270. if (ret)
  271. return ret;
  272. ret = mtd_device_register(&rbni->mtd, rb91x_nand_partitions,
  273. ARRAY_SIZE(rb91x_nand_partitions));
  274. if (ret)
  275. goto err_release_nand;
  276. return 0;
  277. err_release_nand:
  278. nand_release(&rbni->mtd);
  279. return ret;
  280. }
  281. static int rb91x_nand_remove(struct platform_device *pdev)
  282. {
  283. struct rb91x_nand_info *info = platform_get_drvdata(pdev);
  284. nand_release(&info->mtd);
  285. return 0;
  286. }
  287. static struct platform_driver rb91x_nand_driver = {
  288. .probe = rb91x_nand_probe,
  289. .remove = rb91x_nand_remove,
  290. .driver = {
  291. .name = RB91X_NAND_DRIVER_NAME,
  292. .owner = THIS_MODULE,
  293. },
  294. };
  295. module_platform_driver(rb91x_nand_driver);
  296. MODULE_DESCRIPTION(DRV_DESC);
  297. MODULE_VERSION(DRV_VERSION);
  298. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  299. MODULE_LICENSE("GPL v2");