jz4740_nand.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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/gpio.h>
  25. #include <asm/mach-jz4740/jz4740_nand.h>
  26. #define JZ_REG_NAND_CTRL 0x50
  27. #define JZ_REG_NAND_ECC_CTRL 0x100
  28. #define JZ_REG_NAND_DATA 0x104
  29. #define JZ_REG_NAND_PAR0 0x108
  30. #define JZ_REG_NAND_PAR1 0x10C
  31. #define JZ_REG_NAND_PAR2 0x110
  32. #define JZ_REG_NAND_IRQ_STAT 0x114
  33. #define JZ_REG_NAND_IRQ_CTRL 0x118
  34. #define JZ_REG_NAND_ERR(x) (0x11C + ((x) << 2))
  35. #define JZ_NAND_ECC_CTRL_PAR_READY BIT(4)
  36. #define JZ_NAND_ECC_CTRL_ENCODING BIT(3)
  37. #define JZ_NAND_ECC_CTRL_RS BIT(2)
  38. #define JZ_NAND_ECC_CTRL_RESET BIT(1)
  39. #define JZ_NAND_ECC_CTRL_ENABLE BIT(0)
  40. #define JZ_NAND_STATUS_ERR_COUNT (BIT(31) | BIT(30) | BIT(29))
  41. #define JZ_NAND_STATUS_PAD_FINISH BIT(4)
  42. #define JZ_NAND_STATUS_DEC_FINISH BIT(3)
  43. #define JZ_NAND_STATUS_ENC_FINISH BIT(2)
  44. #define JZ_NAND_STATUS_UNCOR_ERROR BIT(1)
  45. #define JZ_NAND_STATUS_ERROR BIT(0)
  46. #define JZ_NAND_CTRL_ENABLE_CHIP(x) BIT((x) << 1)
  47. #define JZ_NAND_CTRL_ASSERT_CHIP(x) BIT(((x) << 1) + 1)
  48. #define JZ_NAND_CTRL_ASSERT_CHIP_MASK 0xaa
  49. #define JZ_NAND_MEM_CMD_OFFSET 0x08000
  50. #define JZ_NAND_MEM_ADDR_OFFSET 0x10000
  51. struct jz_nand {
  52. struct nand_chip chip;
  53. void __iomem *base;
  54. struct resource *mem;
  55. unsigned char banks[JZ_NAND_NUM_BANKS];
  56. void __iomem *bank_base[JZ_NAND_NUM_BANKS];
  57. struct resource *bank_mem[JZ_NAND_NUM_BANKS];
  58. int selected_bank;
  59. struct gpio_desc *busy_gpio;
  60. bool is_reading;
  61. };
  62. static inline struct jz_nand *mtd_to_jz_nand(struct mtd_info *mtd)
  63. {
  64. return container_of(mtd_to_nand(mtd), struct jz_nand, chip);
  65. }
  66. static void jz_nand_select_chip(struct mtd_info *mtd, int chipnr)
  67. {
  68. struct jz_nand *nand = mtd_to_jz_nand(mtd);
  69. struct nand_chip *chip = mtd_to_nand(mtd);
  70. uint32_t ctrl;
  71. int banknr;
  72. ctrl = readl(nand->base + JZ_REG_NAND_CTRL);
  73. ctrl &= ~JZ_NAND_CTRL_ASSERT_CHIP_MASK;
  74. if (chipnr == -1) {
  75. banknr = -1;
  76. } else {
  77. banknr = nand->banks[chipnr] - 1;
  78. chip->IO_ADDR_R = nand->bank_base[banknr];
  79. chip->IO_ADDR_W = nand->bank_base[banknr];
  80. }
  81. writel(ctrl, nand->base + JZ_REG_NAND_CTRL);
  82. nand->selected_bank = banknr;
  83. }
  84. static void jz_nand_cmd_ctrl(struct mtd_info *mtd, int dat, unsigned int ctrl)
  85. {
  86. struct jz_nand *nand = mtd_to_jz_nand(mtd);
  87. struct nand_chip *chip = mtd_to_nand(mtd);
  88. uint32_t reg;
  89. void __iomem *bank_base = nand->bank_base[nand->selected_bank];
  90. BUG_ON(nand->selected_bank < 0);
  91. if (ctrl & NAND_CTRL_CHANGE) {
  92. BUG_ON((ctrl & NAND_ALE) && (ctrl & NAND_CLE));
  93. if (ctrl & NAND_ALE)
  94. bank_base += JZ_NAND_MEM_ADDR_OFFSET;
  95. else if (ctrl & NAND_CLE)
  96. bank_base += JZ_NAND_MEM_CMD_OFFSET;
  97. chip->IO_ADDR_W = bank_base;
  98. reg = readl(nand->base + JZ_REG_NAND_CTRL);
  99. if (ctrl & NAND_NCE)
  100. reg |= JZ_NAND_CTRL_ASSERT_CHIP(nand->selected_bank);
  101. else
  102. reg &= ~JZ_NAND_CTRL_ASSERT_CHIP(nand->selected_bank);
  103. writel(reg, nand->base + JZ_REG_NAND_CTRL);
  104. }
  105. if (dat != NAND_CMD_NONE)
  106. writeb(dat, chip->IO_ADDR_W);
  107. }
  108. static int jz_nand_dev_ready(struct mtd_info *mtd)
  109. {
  110. struct jz_nand *nand = mtd_to_jz_nand(mtd);
  111. return gpiod_get_value_cansleep(nand->busy_gpio);
  112. }
  113. static void jz_nand_hwctl(struct mtd_info *mtd, int mode)
  114. {
  115. struct jz_nand *nand = mtd_to_jz_nand(mtd);
  116. uint32_t reg;
  117. writel(0, nand->base + JZ_REG_NAND_IRQ_STAT);
  118. reg = readl(nand->base + JZ_REG_NAND_ECC_CTRL);
  119. reg |= JZ_NAND_ECC_CTRL_RESET;
  120. reg |= JZ_NAND_ECC_CTRL_ENABLE;
  121. reg |= JZ_NAND_ECC_CTRL_RS;
  122. switch (mode) {
  123. case NAND_ECC_READ:
  124. reg &= ~JZ_NAND_ECC_CTRL_ENCODING;
  125. nand->is_reading = true;
  126. break;
  127. case NAND_ECC_WRITE:
  128. reg |= JZ_NAND_ECC_CTRL_ENCODING;
  129. nand->is_reading = false;
  130. break;
  131. default:
  132. break;
  133. }
  134. writel(reg, nand->base + JZ_REG_NAND_ECC_CTRL);
  135. }
  136. static int jz_nand_calculate_ecc_rs(struct mtd_info *mtd, const uint8_t *dat,
  137. uint8_t *ecc_code)
  138. {
  139. struct jz_nand *nand = mtd_to_jz_nand(mtd);
  140. uint32_t reg, status;
  141. int i;
  142. unsigned int timeout = 1000;
  143. static uint8_t empty_block_ecc[] = {0xcd, 0x9d, 0x90, 0x58, 0xf4,
  144. 0x8b, 0xff, 0xb7, 0x6f};
  145. if (nand->is_reading)
  146. return 0;
  147. do {
  148. status = readl(nand->base + JZ_REG_NAND_IRQ_STAT);
  149. } while (!(status & JZ_NAND_STATUS_ENC_FINISH) && --timeout);
  150. if (timeout == 0)
  151. return -1;
  152. reg = readl(nand->base + JZ_REG_NAND_ECC_CTRL);
  153. reg &= ~JZ_NAND_ECC_CTRL_ENABLE;
  154. writel(reg, nand->base + JZ_REG_NAND_ECC_CTRL);
  155. for (i = 0; i < 9; ++i)
  156. ecc_code[i] = readb(nand->base + JZ_REG_NAND_PAR0 + i);
  157. /* If the written data is completly 0xff, we also want to write 0xff as
  158. * ecc, otherwise we will get in trouble when doing subpage writes. */
  159. if (memcmp(ecc_code, empty_block_ecc, 9) == 0)
  160. memset(ecc_code, 0xff, 9);
  161. return 0;
  162. }
  163. static void jz_nand_correct_data(uint8_t *dat, int index, int mask)
  164. {
  165. int offset = index & 0x7;
  166. uint16_t data;
  167. index += (index >> 3);
  168. data = dat[index];
  169. data |= dat[index+1] << 8;
  170. mask ^= (data >> offset) & 0x1ff;
  171. data &= ~(0x1ff << offset);
  172. data |= (mask << offset);
  173. dat[index] = data & 0xff;
  174. dat[index+1] = (data >> 8) & 0xff;
  175. }
  176. static int jz_nand_correct_ecc_rs(struct mtd_info *mtd, uint8_t *dat,
  177. uint8_t *read_ecc, uint8_t *calc_ecc)
  178. {
  179. struct jz_nand *nand = mtd_to_jz_nand(mtd);
  180. int i, error_count, index;
  181. uint32_t reg, status, error;
  182. unsigned int timeout = 1000;
  183. for (i = 0; i < 9; ++i)
  184. writeb(read_ecc[i], nand->base + JZ_REG_NAND_PAR0 + i);
  185. reg = readl(nand->base + JZ_REG_NAND_ECC_CTRL);
  186. reg |= JZ_NAND_ECC_CTRL_PAR_READY;
  187. writel(reg, nand->base + JZ_REG_NAND_ECC_CTRL);
  188. do {
  189. status = readl(nand->base + JZ_REG_NAND_IRQ_STAT);
  190. } while (!(status & JZ_NAND_STATUS_DEC_FINISH) && --timeout);
  191. if (timeout == 0)
  192. return -ETIMEDOUT;
  193. reg = readl(nand->base + JZ_REG_NAND_ECC_CTRL);
  194. reg &= ~JZ_NAND_ECC_CTRL_ENABLE;
  195. writel(reg, nand->base + JZ_REG_NAND_ECC_CTRL);
  196. if (status & JZ_NAND_STATUS_ERROR) {
  197. if (status & JZ_NAND_STATUS_UNCOR_ERROR)
  198. return -EBADMSG;
  199. error_count = (status & JZ_NAND_STATUS_ERR_COUNT) >> 29;
  200. for (i = 0; i < error_count; ++i) {
  201. error = readl(nand->base + JZ_REG_NAND_ERR(i));
  202. index = ((error >> 16) & 0x1ff) - 1;
  203. if (index >= 0 && index < 512)
  204. jz_nand_correct_data(dat, index, error & 0x1ff);
  205. }
  206. return error_count;
  207. }
  208. return 0;
  209. }
  210. static int jz_nand_ioremap_resource(struct platform_device *pdev,
  211. const char *name, struct resource **res, void *__iomem *base)
  212. {
  213. int ret;
  214. *res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
  215. if (!*res) {
  216. dev_err(&pdev->dev, "Failed to get platform %s memory\n", name);
  217. ret = -ENXIO;
  218. goto err;
  219. }
  220. *res = request_mem_region((*res)->start, resource_size(*res),
  221. pdev->name);
  222. if (!*res) {
  223. dev_err(&pdev->dev, "Failed to request %s memory region\n", name);
  224. ret = -EBUSY;
  225. goto err;
  226. }
  227. *base = ioremap((*res)->start, resource_size(*res));
  228. if (!*base) {
  229. dev_err(&pdev->dev, "Failed to ioremap %s memory region\n", name);
  230. ret = -EBUSY;
  231. goto err_release_mem;
  232. }
  233. return 0;
  234. err_release_mem:
  235. release_mem_region((*res)->start, resource_size(*res));
  236. err:
  237. *res = NULL;
  238. *base = NULL;
  239. return ret;
  240. }
  241. static inline void jz_nand_iounmap_resource(struct resource *res,
  242. void __iomem *base)
  243. {
  244. iounmap(base);
  245. release_mem_region(res->start, resource_size(res));
  246. }
  247. static int jz_nand_detect_bank(struct platform_device *pdev,
  248. struct jz_nand *nand, unsigned char bank,
  249. size_t chipnr, uint8_t *nand_maf_id,
  250. uint8_t *nand_dev_id)
  251. {
  252. int ret;
  253. int gpio;
  254. char gpio_name[9];
  255. char res_name[6];
  256. uint32_t ctrl;
  257. struct nand_chip *chip = &nand->chip;
  258. struct mtd_info *mtd = nand_to_mtd(chip);
  259. /* Request GPIO port. */
  260. gpio = JZ_GPIO_MEM_CS0 + bank - 1;
  261. sprintf(gpio_name, "NAND CS%d", bank);
  262. ret = gpio_request(gpio, gpio_name);
  263. if (ret) {
  264. dev_warn(&pdev->dev,
  265. "Failed to request %s gpio %d: %d\n",
  266. gpio_name, gpio, ret);
  267. goto notfound_gpio;
  268. }
  269. /* Request I/O resource. */
  270. sprintf(res_name, "bank%d", bank);
  271. ret = jz_nand_ioremap_resource(pdev, res_name,
  272. &nand->bank_mem[bank - 1],
  273. &nand->bank_base[bank - 1]);
  274. if (ret)
  275. goto notfound_resource;
  276. /* Enable chip in bank. */
  277. jz_gpio_set_function(gpio, JZ_GPIO_FUNC_MEM_CS0);
  278. ctrl = readl(nand->base + JZ_REG_NAND_CTRL);
  279. ctrl |= JZ_NAND_CTRL_ENABLE_CHIP(bank - 1);
  280. writel(ctrl, nand->base + JZ_REG_NAND_CTRL);
  281. if (chipnr == 0) {
  282. /* Detect first chip. */
  283. ret = nand_scan_ident(mtd, 1, NULL);
  284. if (ret)
  285. goto notfound_id;
  286. /* Retrieve the IDs from the first chip. */
  287. chip->select_chip(mtd, 0);
  288. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  289. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  290. *nand_maf_id = chip->read_byte(mtd);
  291. *nand_dev_id = chip->read_byte(mtd);
  292. } else {
  293. /* Detect additional chip. */
  294. chip->select_chip(mtd, chipnr);
  295. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  296. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  297. if (*nand_maf_id != chip->read_byte(mtd)
  298. || *nand_dev_id != chip->read_byte(mtd)) {
  299. ret = -ENODEV;
  300. goto notfound_id;
  301. }
  302. /* Update size of the MTD. */
  303. chip->numchips++;
  304. mtd->size += chip->chipsize;
  305. }
  306. dev_info(&pdev->dev, "Found chip %i on bank %i\n", chipnr, bank);
  307. return 0;
  308. notfound_id:
  309. dev_info(&pdev->dev, "No chip found on bank %i\n", bank);
  310. ctrl &= ~(JZ_NAND_CTRL_ENABLE_CHIP(bank - 1));
  311. writel(ctrl, nand->base + JZ_REG_NAND_CTRL);
  312. jz_gpio_set_function(gpio, JZ_GPIO_FUNC_NONE);
  313. jz_nand_iounmap_resource(nand->bank_mem[bank - 1],
  314. nand->bank_base[bank - 1]);
  315. notfound_resource:
  316. gpio_free(gpio);
  317. notfound_gpio:
  318. return ret;
  319. }
  320. static int jz_nand_probe(struct platform_device *pdev)
  321. {
  322. int ret;
  323. struct jz_nand *nand;
  324. struct nand_chip *chip;
  325. struct mtd_info *mtd;
  326. struct jz_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  327. size_t chipnr, bank_idx;
  328. uint8_t nand_maf_id = 0, nand_dev_id = 0;
  329. nand = kzalloc(sizeof(*nand), GFP_KERNEL);
  330. if (!nand)
  331. return -ENOMEM;
  332. ret = jz_nand_ioremap_resource(pdev, "mmio", &nand->mem, &nand->base);
  333. if (ret)
  334. goto err_free;
  335. nand->busy_gpio = devm_gpiod_get_optional(&pdev->dev, "busy", GPIOD_IN);
  336. if (IS_ERR(nand->busy_gpio)) {
  337. ret = PTR_ERR(nand->busy_gpio);
  338. dev_err(&pdev->dev, "Failed to request busy gpio %d\n",
  339. ret);
  340. goto err_iounmap_mmio;
  341. }
  342. chip = &nand->chip;
  343. mtd = nand_to_mtd(chip);
  344. mtd->dev.parent = &pdev->dev;
  345. mtd->name = "jz4740-nand";
  346. chip->ecc.hwctl = jz_nand_hwctl;
  347. chip->ecc.calculate = jz_nand_calculate_ecc_rs;
  348. chip->ecc.correct = jz_nand_correct_ecc_rs;
  349. chip->ecc.mode = NAND_ECC_HW_OOB_FIRST;
  350. chip->ecc.size = 512;
  351. chip->ecc.bytes = 9;
  352. chip->ecc.strength = 4;
  353. chip->ecc.options = NAND_ECC_GENERIC_ERASED_CHECK;
  354. chip->chip_delay = 50;
  355. chip->cmd_ctrl = jz_nand_cmd_ctrl;
  356. chip->select_chip = jz_nand_select_chip;
  357. if (nand->busy_gpio)
  358. chip->dev_ready = jz_nand_dev_ready;
  359. platform_set_drvdata(pdev, nand);
  360. /* We are going to autodetect NAND chips in the banks specified in the
  361. * platform data. Although nand_scan_ident() can detect multiple chips,
  362. * it requires those chips to be numbered consecuitively, which is not
  363. * always the case for external memory banks. And a fixed chip-to-bank
  364. * mapping is not practical either, since for example Dingoo units
  365. * produced at different times have NAND chips in different banks.
  366. */
  367. chipnr = 0;
  368. for (bank_idx = 0; bank_idx < JZ_NAND_NUM_BANKS; bank_idx++) {
  369. unsigned char bank;
  370. /* If there is no platform data, look for NAND in bank 1,
  371. * which is the most likely bank since it is the only one
  372. * that can be booted from.
  373. */
  374. bank = pdata ? pdata->banks[bank_idx] : bank_idx ^ 1;
  375. if (bank == 0)
  376. break;
  377. if (bank > JZ_NAND_NUM_BANKS) {
  378. dev_warn(&pdev->dev,
  379. "Skipping non-existing bank: %d\n", bank);
  380. continue;
  381. }
  382. /* The detection routine will directly or indirectly call
  383. * jz_nand_select_chip(), so nand->banks has to contain the
  384. * bank we're checking.
  385. */
  386. nand->banks[chipnr] = bank;
  387. if (jz_nand_detect_bank(pdev, nand, bank, chipnr,
  388. &nand_maf_id, &nand_dev_id) == 0)
  389. chipnr++;
  390. else
  391. nand->banks[chipnr] = 0;
  392. }
  393. if (chipnr == 0) {
  394. dev_err(&pdev->dev, "No NAND chips found\n");
  395. goto err_iounmap_mmio;
  396. }
  397. if (pdata && pdata->ident_callback) {
  398. pdata->ident_callback(pdev, mtd, &pdata->partitions,
  399. &pdata->num_partitions);
  400. }
  401. ret = nand_scan_tail(mtd);
  402. if (ret) {
  403. dev_err(&pdev->dev, "Failed to scan NAND\n");
  404. goto err_unclaim_banks;
  405. }
  406. ret = mtd_device_parse_register(mtd, NULL, NULL,
  407. pdata ? pdata->partitions : NULL,
  408. pdata ? pdata->num_partitions : 0);
  409. if (ret) {
  410. dev_err(&pdev->dev, "Failed to add mtd device\n");
  411. goto err_nand_release;
  412. }
  413. dev_info(&pdev->dev, "Successfully registered JZ4740 NAND driver\n");
  414. return 0;
  415. err_nand_release:
  416. nand_release(mtd);
  417. err_unclaim_banks:
  418. while (chipnr--) {
  419. unsigned char bank = nand->banks[chipnr];
  420. gpio_free(JZ_GPIO_MEM_CS0 + bank - 1);
  421. jz_nand_iounmap_resource(nand->bank_mem[bank - 1],
  422. nand->bank_base[bank - 1]);
  423. }
  424. writel(0, nand->base + JZ_REG_NAND_CTRL);
  425. err_iounmap_mmio:
  426. jz_nand_iounmap_resource(nand->mem, nand->base);
  427. err_free:
  428. kfree(nand);
  429. return ret;
  430. }
  431. static int jz_nand_remove(struct platform_device *pdev)
  432. {
  433. struct jz_nand *nand = platform_get_drvdata(pdev);
  434. size_t i;
  435. nand_release(nand_to_mtd(&nand->chip));
  436. /* Deassert and disable all chips */
  437. writel(0, nand->base + JZ_REG_NAND_CTRL);
  438. for (i = 0; i < JZ_NAND_NUM_BANKS; ++i) {
  439. unsigned char bank = nand->banks[i];
  440. if (bank != 0) {
  441. jz_nand_iounmap_resource(nand->bank_mem[bank - 1],
  442. nand->bank_base[bank - 1]);
  443. gpio_free(JZ_GPIO_MEM_CS0 + bank - 1);
  444. }
  445. }
  446. jz_nand_iounmap_resource(nand->mem, nand->base);
  447. kfree(nand);
  448. return 0;
  449. }
  450. static struct platform_driver jz_nand_driver = {
  451. .probe = jz_nand_probe,
  452. .remove = jz_nand_remove,
  453. .driver = {
  454. .name = "jz4740-nand",
  455. },
  456. };
  457. module_platform_driver(jz_nand_driver);
  458. MODULE_LICENSE("GPL");
  459. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  460. MODULE_DESCRIPTION("NAND controller driver for JZ4740 SoC");
  461. MODULE_ALIAS("platform:jz4740-nand");