jz4740_nand.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
  3. * JZ4740 SoC NAND controller driver
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * You should have received a copy of the GNU General Public License along
  11. * with this program; if not, write to the Free Software Foundation, Inc.,
  12. * 675 Mass Ave, Cambridge, MA 02139, USA.
  13. *
  14. */
  15. #include <linux/ioport.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #include <linux/mtd/mtd.h>
  21. #include <linux/mtd/nand.h>
  22. #include <linux/mtd/partitions.h>
  23. #include <linux/gpio.h>
  24. #include <asm/mach-jz4740/jz4740_nand.h>
  25. #define JZ_REG_NAND_CTRL 0x50
  26. #define JZ_REG_NAND_ECC_CTRL 0x100
  27. #define JZ_REG_NAND_DATA 0x104
  28. #define JZ_REG_NAND_PAR0 0x108
  29. #define JZ_REG_NAND_PAR1 0x10C
  30. #define JZ_REG_NAND_PAR2 0x110
  31. #define JZ_REG_NAND_IRQ_STAT 0x114
  32. #define JZ_REG_NAND_IRQ_CTRL 0x118
  33. #define JZ_REG_NAND_ERR(x) (0x11C + ((x) << 2))
  34. #define JZ_NAND_ECC_CTRL_PAR_READY BIT(4)
  35. #define JZ_NAND_ECC_CTRL_ENCODING BIT(3)
  36. #define JZ_NAND_ECC_CTRL_RS BIT(2)
  37. #define JZ_NAND_ECC_CTRL_RESET BIT(1)
  38. #define JZ_NAND_ECC_CTRL_ENABLE BIT(0)
  39. #define JZ_NAND_STATUS_ERR_COUNT (BIT(31) | BIT(30) | BIT(29))
  40. #define JZ_NAND_STATUS_PAD_FINISH BIT(4)
  41. #define JZ_NAND_STATUS_DEC_FINISH BIT(3)
  42. #define JZ_NAND_STATUS_ENC_FINISH BIT(2)
  43. #define JZ_NAND_STATUS_UNCOR_ERROR BIT(1)
  44. #define JZ_NAND_STATUS_ERROR BIT(0)
  45. #define JZ_NAND_CTRL_ENABLE_CHIP(x) BIT((x) << 1)
  46. #define JZ_NAND_CTRL_ASSERT_CHIP(x) BIT(((x) << 1) + 1)
  47. #define JZ_NAND_MEM_ADDR_OFFSET 0x10000
  48. #define JZ_NAND_MEM_CMD_OFFSET 0x08000
  49. struct jz_nand {
  50. struct mtd_info mtd;
  51. struct nand_chip chip;
  52. void __iomem *base;
  53. struct resource *mem;
  54. void __iomem *bank_base;
  55. struct resource *bank_mem;
  56. struct jz_nand_platform_data *pdata;
  57. bool is_reading;
  58. };
  59. static inline struct jz_nand *mtd_to_jz_nand(struct mtd_info *mtd)
  60. {
  61. return container_of(mtd, struct jz_nand, mtd);
  62. }
  63. static void jz_nand_cmd_ctrl(struct mtd_info *mtd, int dat, unsigned int ctrl)
  64. {
  65. struct jz_nand *nand = mtd_to_jz_nand(mtd);
  66. struct nand_chip *chip = mtd->priv;
  67. uint32_t reg;
  68. if (ctrl & NAND_CTRL_CHANGE) {
  69. BUG_ON((ctrl & NAND_ALE) && (ctrl & NAND_CLE));
  70. if (ctrl & NAND_ALE)
  71. chip->IO_ADDR_W = nand->bank_base + JZ_NAND_MEM_ADDR_OFFSET;
  72. else if (ctrl & NAND_CLE)
  73. chip->IO_ADDR_W = nand->bank_base + JZ_NAND_MEM_CMD_OFFSET;
  74. else
  75. chip->IO_ADDR_W = nand->bank_base;
  76. reg = readl(nand->base + JZ_REG_NAND_CTRL);
  77. if (ctrl & NAND_NCE)
  78. reg |= JZ_NAND_CTRL_ASSERT_CHIP(0);
  79. else
  80. reg &= ~JZ_NAND_CTRL_ASSERT_CHIP(0);
  81. writel(reg, nand->base + JZ_REG_NAND_CTRL);
  82. }
  83. if (dat != NAND_CMD_NONE)
  84. writeb(dat, chip->IO_ADDR_W);
  85. }
  86. static int jz_nand_dev_ready(struct mtd_info *mtd)
  87. {
  88. struct jz_nand *nand = mtd_to_jz_nand(mtd);
  89. return gpio_get_value_cansleep(nand->pdata->busy_gpio);
  90. }
  91. static void jz_nand_hwctl(struct mtd_info *mtd, int mode)
  92. {
  93. struct jz_nand *nand = mtd_to_jz_nand(mtd);
  94. uint32_t reg;
  95. writel(0, nand->base + JZ_REG_NAND_IRQ_STAT);
  96. reg = readl(nand->base + JZ_REG_NAND_ECC_CTRL);
  97. reg |= JZ_NAND_ECC_CTRL_RESET;
  98. reg |= JZ_NAND_ECC_CTRL_ENABLE;
  99. reg |= JZ_NAND_ECC_CTRL_RS;
  100. switch (mode) {
  101. case NAND_ECC_READ:
  102. reg &= ~JZ_NAND_ECC_CTRL_ENCODING;
  103. nand->is_reading = true;
  104. break;
  105. case NAND_ECC_WRITE:
  106. reg |= JZ_NAND_ECC_CTRL_ENCODING;
  107. nand->is_reading = false;
  108. break;
  109. default:
  110. break;
  111. }
  112. writel(reg, nand->base + JZ_REG_NAND_ECC_CTRL);
  113. }
  114. static int jz_nand_calculate_ecc_rs(struct mtd_info *mtd, const uint8_t *dat,
  115. uint8_t *ecc_code)
  116. {
  117. struct jz_nand *nand = mtd_to_jz_nand(mtd);
  118. uint32_t reg, status;
  119. int i;
  120. unsigned int timeout = 1000;
  121. static uint8_t empty_block_ecc[] = {0xcd, 0x9d, 0x90, 0x58, 0xf4,
  122. 0x8b, 0xff, 0xb7, 0x6f};
  123. if (nand->is_reading)
  124. return 0;
  125. do {
  126. status = readl(nand->base + JZ_REG_NAND_IRQ_STAT);
  127. } while (!(status & JZ_NAND_STATUS_ENC_FINISH) && --timeout);
  128. if (timeout == 0)
  129. return -1;
  130. reg = readl(nand->base + JZ_REG_NAND_ECC_CTRL);
  131. reg &= ~JZ_NAND_ECC_CTRL_ENABLE;
  132. writel(reg, nand->base + JZ_REG_NAND_ECC_CTRL);
  133. for (i = 0; i < 9; ++i)
  134. ecc_code[i] = readb(nand->base + JZ_REG_NAND_PAR0 + i);
  135. /* If the written data is completly 0xff, we also want to write 0xff as
  136. * ecc, otherwise we will get in trouble when doing subpage writes. */
  137. if (memcmp(ecc_code, empty_block_ecc, 9) == 0)
  138. memset(ecc_code, 0xff, 9);
  139. return 0;
  140. }
  141. static void jz_nand_correct_data(uint8_t *dat, int index, int mask)
  142. {
  143. int offset = index & 0x7;
  144. uint16_t data;
  145. index += (index >> 3);
  146. data = dat[index];
  147. data |= dat[index+1] << 8;
  148. mask ^= (data >> offset) & 0x1ff;
  149. data &= ~(0x1ff << offset);
  150. data |= (mask << offset);
  151. dat[index] = data & 0xff;
  152. dat[index+1] = (data >> 8) & 0xff;
  153. }
  154. static int jz_nand_correct_ecc_rs(struct mtd_info *mtd, uint8_t *dat,
  155. uint8_t *read_ecc, uint8_t *calc_ecc)
  156. {
  157. struct jz_nand *nand = mtd_to_jz_nand(mtd);
  158. int i, error_count, index;
  159. uint32_t reg, status, error;
  160. uint32_t t;
  161. unsigned int timeout = 1000;
  162. t = read_ecc[0];
  163. if (t == 0xff) {
  164. for (i = 1; i < 9; ++i)
  165. t &= read_ecc[i];
  166. t &= dat[0];
  167. t &= dat[nand->chip.ecc.size / 2];
  168. t &= dat[nand->chip.ecc.size - 1];
  169. if (t == 0xff) {
  170. for (i = 1; i < nand->chip.ecc.size - 1; ++i)
  171. t &= dat[i];
  172. if (t == 0xff)
  173. return 0;
  174. }
  175. }
  176. for (i = 0; i < 9; ++i)
  177. writeb(read_ecc[i], nand->base + JZ_REG_NAND_PAR0 + i);
  178. reg = readl(nand->base + JZ_REG_NAND_ECC_CTRL);
  179. reg |= JZ_NAND_ECC_CTRL_PAR_READY;
  180. writel(reg, nand->base + JZ_REG_NAND_ECC_CTRL);
  181. do {
  182. status = readl(nand->base + JZ_REG_NAND_IRQ_STAT);
  183. } while (!(status & JZ_NAND_STATUS_DEC_FINISH) && --timeout);
  184. if (timeout == 0)
  185. return -1;
  186. reg = readl(nand->base + JZ_REG_NAND_ECC_CTRL);
  187. reg &= ~JZ_NAND_ECC_CTRL_ENABLE;
  188. writel(reg, nand->base + JZ_REG_NAND_ECC_CTRL);
  189. if (status & JZ_NAND_STATUS_ERROR) {
  190. if (status & JZ_NAND_STATUS_UNCOR_ERROR)
  191. return -1;
  192. error_count = (status & JZ_NAND_STATUS_ERR_COUNT) >> 29;
  193. for (i = 0; i < error_count; ++i) {
  194. error = readl(nand->base + JZ_REG_NAND_ERR(i));
  195. index = ((error >> 16) & 0x1ff) - 1;
  196. if (index >= 0 && index < 512)
  197. jz_nand_correct_data(dat, index, error & 0x1ff);
  198. }
  199. return error_count;
  200. }
  201. return 0;
  202. }
  203. static int jz_nand_ioremap_resource(struct platform_device *pdev,
  204. const char *name, struct resource **res, void __iomem **base)
  205. {
  206. int ret;
  207. *res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
  208. if (!*res) {
  209. dev_err(&pdev->dev, "Failed to get platform %s memory\n", name);
  210. ret = -ENXIO;
  211. goto err;
  212. }
  213. *res = request_mem_region((*res)->start, resource_size(*res),
  214. pdev->name);
  215. if (!*res) {
  216. dev_err(&pdev->dev, "Failed to request %s memory region\n", name);
  217. ret = -EBUSY;
  218. goto err;
  219. }
  220. *base = ioremap((*res)->start, resource_size(*res));
  221. if (!*base) {
  222. dev_err(&pdev->dev, "Failed to ioremap %s memory region\n", name);
  223. ret = -EBUSY;
  224. goto err_release_mem;
  225. }
  226. return 0;
  227. err_release_mem:
  228. release_mem_region((*res)->start, resource_size(*res));
  229. err:
  230. *res = NULL;
  231. *base = NULL;
  232. return ret;
  233. }
  234. static int __devinit jz_nand_probe(struct platform_device *pdev)
  235. {
  236. int ret;
  237. struct jz_nand *nand;
  238. struct nand_chip *chip;
  239. struct mtd_info *mtd;
  240. struct jz_nand_platform_data *pdata = pdev->dev.platform_data;
  241. nand = kzalloc(sizeof(*nand), GFP_KERNEL);
  242. if (!nand) {
  243. dev_err(&pdev->dev, "Failed to allocate device structure.\n");
  244. return -ENOMEM;
  245. }
  246. ret = jz_nand_ioremap_resource(pdev, "mmio", &nand->mem, &nand->base);
  247. if (ret)
  248. goto err_free;
  249. ret = jz_nand_ioremap_resource(pdev, "bank", &nand->bank_mem,
  250. &nand->bank_base);
  251. if (ret)
  252. goto err_iounmap_mmio;
  253. if (pdata && gpio_is_valid(pdata->busy_gpio)) {
  254. ret = gpio_request(pdata->busy_gpio, "NAND busy pin");
  255. if (ret) {
  256. dev_err(&pdev->dev,
  257. "Failed to request busy gpio %d: %d\n",
  258. pdata->busy_gpio, ret);
  259. goto err_iounmap_mem;
  260. }
  261. }
  262. mtd = &nand->mtd;
  263. chip = &nand->chip;
  264. mtd->priv = chip;
  265. mtd->owner = THIS_MODULE;
  266. mtd->name = "jz4740-nand";
  267. chip->ecc.hwctl = jz_nand_hwctl;
  268. chip->ecc.calculate = jz_nand_calculate_ecc_rs;
  269. chip->ecc.correct = jz_nand_correct_ecc_rs;
  270. chip->ecc.mode = NAND_ECC_HW_OOB_FIRST;
  271. chip->ecc.size = 512;
  272. chip->ecc.bytes = 9;
  273. chip->ecc.strength = 2;
  274. /*
  275. * FIXME: ecc_strength value of 2 bits per 512 bytes of data is a
  276. * conservative guess, given 9 ecc bytes and reed-solomon alg.
  277. */
  278. if (pdata)
  279. chip->ecc.layout = pdata->ecc_layout;
  280. chip->chip_delay = 50;
  281. chip->cmd_ctrl = jz_nand_cmd_ctrl;
  282. if (pdata && gpio_is_valid(pdata->busy_gpio))
  283. chip->dev_ready = jz_nand_dev_ready;
  284. chip->IO_ADDR_R = nand->bank_base;
  285. chip->IO_ADDR_W = nand->bank_base;
  286. nand->pdata = pdata;
  287. platform_set_drvdata(pdev, nand);
  288. writel(JZ_NAND_CTRL_ENABLE_CHIP(0), nand->base + JZ_REG_NAND_CTRL);
  289. ret = nand_scan_ident(mtd, 1, NULL);
  290. if (ret) {
  291. dev_err(&pdev->dev, "Failed to scan nand\n");
  292. goto err_gpio_free;
  293. }
  294. if (pdata && pdata->ident_callback) {
  295. pdata->ident_callback(pdev, chip, &pdata->partitions,
  296. &pdata->num_partitions);
  297. }
  298. ret = nand_scan_tail(mtd);
  299. if (ret) {
  300. dev_err(&pdev->dev, "Failed to scan nand\n");
  301. goto err_gpio_free;
  302. }
  303. ret = mtd_device_parse_register(mtd, NULL, NULL,
  304. pdata ? pdata->partitions : NULL,
  305. pdata ? pdata->num_partitions : 0);
  306. if (ret) {
  307. dev_err(&pdev->dev, "Failed to add mtd device\n");
  308. goto err_nand_release;
  309. }
  310. dev_info(&pdev->dev, "Successfully registered JZ4740 NAND driver\n");
  311. return 0;
  312. err_nand_release:
  313. nand_release(&nand->mtd);
  314. err_gpio_free:
  315. platform_set_drvdata(pdev, NULL);
  316. gpio_free(pdata->busy_gpio);
  317. err_iounmap_mem:
  318. iounmap(nand->bank_base);
  319. err_iounmap_mmio:
  320. iounmap(nand->base);
  321. err_free:
  322. kfree(nand);
  323. return ret;
  324. }
  325. static int __devexit jz_nand_remove(struct platform_device *pdev)
  326. {
  327. struct jz_nand *nand = platform_get_drvdata(pdev);
  328. nand_release(&nand->mtd);
  329. /* Deassert and disable all chips */
  330. writel(0, nand->base + JZ_REG_NAND_CTRL);
  331. iounmap(nand->bank_base);
  332. release_mem_region(nand->bank_mem->start, resource_size(nand->bank_mem));
  333. iounmap(nand->base);
  334. release_mem_region(nand->mem->start, resource_size(nand->mem));
  335. platform_set_drvdata(pdev, NULL);
  336. kfree(nand);
  337. return 0;
  338. }
  339. static struct platform_driver jz_nand_driver = {
  340. .probe = jz_nand_probe,
  341. .remove = __devexit_p(jz_nand_remove),
  342. .driver = {
  343. .name = "jz4740-nand",
  344. .owner = THIS_MODULE,
  345. },
  346. };
  347. module_platform_driver(jz_nand_driver);
  348. MODULE_LICENSE("GPL");
  349. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  350. MODULE_DESCRIPTION("NAND controller driver for JZ4740 SoC");
  351. MODULE_ALIAS("platform:jz4740-nand");