at91_nand.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /*
  2. * drivers/mtd/nand/at91_nand.c
  3. *
  4. * Copyright (C) 2003 Rick Bronson
  5. *
  6. * Derived from drivers/mtd/nand/autcpu12.c
  7. * Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de)
  8. *
  9. * Derived from drivers/mtd/spia.c
  10. * Copyright (C) 2000 Steven J. Hill (sjhill@cotw.com)
  11. *
  12. *
  13. * Add Hardware ECC support for AT91SAM9260 / AT91SAM9263
  14. * Richard Genoud (richard.genoud@gmail.com), Adeneo Copyright (C) 2007
  15. *
  16. * Derived from Das U-Boot source code
  17. * (u-boot-1.1.5/board/atmel/at91sam9263ek/nand.c)
  18. * (C) Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
  19. *
  20. *
  21. * This program is free software; you can redistribute it and/or modify
  22. * it under the terms of the GNU General Public License version 2 as
  23. * published by the Free Software Foundation.
  24. *
  25. */
  26. #include <linux/slab.h>
  27. #include <linux/module.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/mtd/mtd.h>
  30. #include <linux/mtd/nand.h>
  31. #include <linux/mtd/partitions.h>
  32. #include <asm/io.h>
  33. #include <asm/sizes.h>
  34. #include <asm/hardware.h>
  35. #include <asm/arch/board.h>
  36. #include <asm/arch/gpio.h>
  37. #ifdef CONFIG_MTD_NAND_AT91_ECC_HW
  38. #define hard_ecc 1
  39. #else
  40. #define hard_ecc 0
  41. #endif
  42. #ifdef CONFIG_MTD_NAND_AT91_ECC_NONE
  43. #define no_ecc 1
  44. #else
  45. #define no_ecc 0
  46. #endif
  47. /* Register access macros */
  48. #define ecc_readl(add, reg) \
  49. __raw_readl(add + AT91_ECC_##reg)
  50. #define ecc_writel(add, reg, value) \
  51. __raw_writel((value), add + AT91_ECC_##reg)
  52. #include <asm/arch/at91_ecc.h> /* AT91SAM9260/3 ECC registers */
  53. /* oob layout for large page size
  54. * bad block info is on bytes 0 and 1
  55. * the bytes have to be consecutives to avoid
  56. * several NAND_CMD_RNDOUT during read
  57. */
  58. static struct nand_ecclayout at91_oobinfo_large = {
  59. .eccbytes = 4,
  60. .eccpos = {60, 61, 62, 63},
  61. .oobfree = {
  62. {2, 58}
  63. },
  64. };
  65. /* oob layout for small page size
  66. * bad block info is on bytes 4 and 5
  67. * the bytes have to be consecutives to avoid
  68. * several NAND_CMD_RNDOUT during read
  69. */
  70. static struct nand_ecclayout at91_oobinfo_small = {
  71. .eccbytes = 4,
  72. .eccpos = {0, 1, 2, 3},
  73. .oobfree = {
  74. {6, 10}
  75. },
  76. };
  77. struct at91_nand_host {
  78. struct nand_chip nand_chip;
  79. struct mtd_info mtd;
  80. void __iomem *io_base;
  81. struct at91_nand_data *board;
  82. struct device *dev;
  83. void __iomem *ecc;
  84. };
  85. /*
  86. * Enable NAND.
  87. */
  88. static void at91_nand_enable(struct at91_nand_host *host)
  89. {
  90. if (host->board->enable_pin)
  91. at91_set_gpio_value(host->board->enable_pin, 0);
  92. }
  93. /*
  94. * Disable NAND.
  95. */
  96. static void at91_nand_disable(struct at91_nand_host *host)
  97. {
  98. if (host->board->enable_pin)
  99. at91_set_gpio_value(host->board->enable_pin, 1);
  100. }
  101. /*
  102. * Hardware specific access to control-lines
  103. */
  104. static void at91_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  105. {
  106. struct nand_chip *nand_chip = mtd->priv;
  107. struct at91_nand_host *host = nand_chip->priv;
  108. if (ctrl & NAND_CTRL_CHANGE) {
  109. if (ctrl & NAND_NCE)
  110. at91_nand_enable(host);
  111. else
  112. at91_nand_disable(host);
  113. }
  114. if (cmd == NAND_CMD_NONE)
  115. return;
  116. if (ctrl & NAND_CLE)
  117. writeb(cmd, host->io_base + (1 << host->board->cle));
  118. else
  119. writeb(cmd, host->io_base + (1 << host->board->ale));
  120. }
  121. /*
  122. * Read the Device Ready pin.
  123. */
  124. static int at91_nand_device_ready(struct mtd_info *mtd)
  125. {
  126. struct nand_chip *nand_chip = mtd->priv;
  127. struct at91_nand_host *host = nand_chip->priv;
  128. return at91_get_gpio_value(host->board->rdy_pin);
  129. }
  130. /*
  131. * write oob for small pages
  132. */
  133. static int at91_nand_write_oob_512(struct mtd_info *mtd,
  134. struct nand_chip *chip, int page)
  135. {
  136. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  137. int eccsize = chip->ecc.size, length = mtd->oobsize;
  138. int len, pos, status = 0;
  139. const uint8_t *bufpoi = chip->oob_poi;
  140. pos = eccsize + chunk;
  141. chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
  142. len = min_t(int, length, chunk);
  143. chip->write_buf(mtd, bufpoi, len);
  144. bufpoi += len;
  145. length -= len;
  146. if (length > 0)
  147. chip->write_buf(mtd, bufpoi, length);
  148. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  149. status = chip->waitfunc(mtd, chip);
  150. return status & NAND_STATUS_FAIL ? -EIO : 0;
  151. }
  152. /*
  153. * read oob for small pages
  154. */
  155. static int at91_nand_read_oob_512(struct mtd_info *mtd,
  156. struct nand_chip *chip, int page, int sndcmd)
  157. {
  158. if (sndcmd) {
  159. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  160. sndcmd = 0;
  161. }
  162. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  163. return sndcmd;
  164. }
  165. /*
  166. * Calculate HW ECC
  167. *
  168. * function called after a write
  169. *
  170. * mtd: MTD block structure
  171. * dat: raw data (unused)
  172. * ecc_code: buffer for ECC
  173. */
  174. static int at91_nand_calculate(struct mtd_info *mtd,
  175. const u_char *dat, unsigned char *ecc_code)
  176. {
  177. struct nand_chip *nand_chip = mtd->priv;
  178. struct at91_nand_host *host = nand_chip->priv;
  179. uint32_t *eccpos = nand_chip->ecc.layout->eccpos;
  180. unsigned int ecc_value;
  181. /* get the first 2 ECC bytes */
  182. ecc_value = ecc_readl(host->ecc, PR);
  183. ecc_code[eccpos[0]] = ecc_value & 0xFF;
  184. ecc_code[eccpos[1]] = (ecc_value >> 8) & 0xFF;
  185. /* get the last 2 ECC bytes */
  186. ecc_value = ecc_readl(host->ecc, NPR) & AT91_ECC_NPARITY;
  187. ecc_code[eccpos[2]] = ecc_value & 0xFF;
  188. ecc_code[eccpos[3]] = (ecc_value >> 8) & 0xFF;
  189. return 0;
  190. }
  191. /*
  192. * HW ECC read page function
  193. *
  194. * mtd: mtd info structure
  195. * chip: nand chip info structure
  196. * buf: buffer to store read data
  197. */
  198. static int at91_nand_read_page(struct mtd_info *mtd,
  199. struct nand_chip *chip, uint8_t *buf)
  200. {
  201. int eccsize = chip->ecc.size;
  202. int eccbytes = chip->ecc.bytes;
  203. uint32_t *eccpos = chip->ecc.layout->eccpos;
  204. uint8_t *p = buf;
  205. uint8_t *oob = chip->oob_poi;
  206. uint8_t *ecc_pos;
  207. int stat;
  208. /* read the page */
  209. chip->read_buf(mtd, p, eccsize);
  210. /* move to ECC position if needed */
  211. if (eccpos[0] != 0) {
  212. /* This only works on large pages
  213. * because the ECC controller waits for
  214. * NAND_CMD_RNDOUTSTART after the
  215. * NAND_CMD_RNDOUT.
  216. * anyway, for small pages, the eccpos[0] == 0
  217. */
  218. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  219. mtd->writesize + eccpos[0], -1);
  220. }
  221. /* the ECC controller needs to read the ECC just after the data */
  222. ecc_pos = oob + eccpos[0];
  223. chip->read_buf(mtd, ecc_pos, eccbytes);
  224. /* check if there's an error */
  225. stat = chip->ecc.correct(mtd, p, oob, NULL);
  226. if (stat < 0)
  227. mtd->ecc_stats.failed++;
  228. else
  229. mtd->ecc_stats.corrected += stat;
  230. /* get back to oob start (end of page) */
  231. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  232. /* read the oob */
  233. chip->read_buf(mtd, oob, mtd->oobsize);
  234. return 0;
  235. }
  236. /*
  237. * HW ECC Correction
  238. *
  239. * function called after a read
  240. *
  241. * mtd: MTD block structure
  242. * dat: raw data read from the chip
  243. * read_ecc: ECC from the chip (unused)
  244. * isnull: unused
  245. *
  246. * Detect and correct a 1 bit error for a page
  247. */
  248. static int at91_nand_correct(struct mtd_info *mtd, u_char *dat,
  249. u_char *read_ecc, u_char *isnull)
  250. {
  251. struct nand_chip *nand_chip = mtd->priv;
  252. struct at91_nand_host *host = nand_chip->priv;
  253. unsigned int ecc_status;
  254. unsigned int ecc_word, ecc_bit;
  255. /* get the status from the Status Register */
  256. ecc_status = ecc_readl(host->ecc, SR);
  257. /* if there's no error */
  258. if (likely(!(ecc_status & AT91_ECC_RECERR)))
  259. return 0;
  260. /* get error bit offset (4 bits) */
  261. ecc_bit = ecc_readl(host->ecc, PR) & AT91_ECC_BITADDR;
  262. /* get word address (12 bits) */
  263. ecc_word = ecc_readl(host->ecc, PR) & AT91_ECC_WORDADDR;
  264. ecc_word >>= 4;
  265. /* if there are multiple errors */
  266. if (ecc_status & AT91_ECC_MULERR) {
  267. /* check if it is a freshly erased block
  268. * (filled with 0xff) */
  269. if ((ecc_bit == AT91_ECC_BITADDR)
  270. && (ecc_word == (AT91_ECC_WORDADDR >> 4))) {
  271. /* the block has just been erased, return OK */
  272. return 0;
  273. }
  274. /* it doesn't seems to be a freshly
  275. * erased block.
  276. * We can't correct so many errors */
  277. dev_dbg(host->dev, "at91_nand : multiple errors detected."
  278. " Unable to correct.\n");
  279. return -EIO;
  280. }
  281. /* if there's a single bit error : we can correct it */
  282. if (ecc_status & AT91_ECC_ECCERR) {
  283. /* there's nothing much to do here.
  284. * the bit error is on the ECC itself.
  285. */
  286. dev_dbg(host->dev, "at91_nand : one bit error on ECC code."
  287. " Nothing to correct\n");
  288. return 0;
  289. }
  290. dev_dbg(host->dev, "at91_nand : one bit error on data."
  291. " (word offset in the page :"
  292. " 0x%x bit offset : 0x%x)\n",
  293. ecc_word, ecc_bit);
  294. /* correct the error */
  295. if (nand_chip->options & NAND_BUSWIDTH_16) {
  296. /* 16 bits words */
  297. ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
  298. } else {
  299. /* 8 bits words */
  300. dat[ecc_word] ^= (1 << ecc_bit);
  301. }
  302. dev_dbg(host->dev, "at91_nand : error corrected\n");
  303. return 1;
  304. }
  305. /*
  306. * Enable HW ECC : unsused
  307. */
  308. static void at91_nand_hwctl(struct mtd_info *mtd, int mode) { ; }
  309. #ifdef CONFIG_MTD_PARTITIONS
  310. static const char *part_probes[] = { "cmdlinepart", NULL };
  311. #endif
  312. /*
  313. * Probe for the NAND device.
  314. */
  315. static int __init at91_nand_probe(struct platform_device *pdev)
  316. {
  317. struct at91_nand_host *host;
  318. struct mtd_info *mtd;
  319. struct nand_chip *nand_chip;
  320. struct resource *regs;
  321. struct resource *mem;
  322. int res;
  323. #ifdef CONFIG_MTD_PARTITIONS
  324. struct mtd_partition *partitions = NULL;
  325. int num_partitions = 0;
  326. #endif
  327. /* Allocate memory for the device structure (and zero it) */
  328. host = kzalloc(sizeof(struct at91_nand_host), GFP_KERNEL);
  329. if (!host) {
  330. printk(KERN_ERR "at91_nand: failed to allocate device structure.\n");
  331. return -ENOMEM;
  332. }
  333. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  334. if (!mem) {
  335. printk(KERN_ERR "at91_nand: can't get I/O resource mem\n");
  336. return -ENXIO;
  337. }
  338. host->io_base = ioremap(mem->start, mem->end - mem->start + 1);
  339. if (host->io_base == NULL) {
  340. printk(KERN_ERR "at91_nand: ioremap failed\n");
  341. kfree(host);
  342. return -EIO;
  343. }
  344. mtd = &host->mtd;
  345. nand_chip = &host->nand_chip;
  346. host->board = pdev->dev.platform_data;
  347. host->dev = &pdev->dev;
  348. nand_chip->priv = host; /* link the private data structures */
  349. mtd->priv = nand_chip;
  350. mtd->owner = THIS_MODULE;
  351. /* Set address of NAND IO lines */
  352. nand_chip->IO_ADDR_R = host->io_base;
  353. nand_chip->IO_ADDR_W = host->io_base;
  354. nand_chip->cmd_ctrl = at91_nand_cmd_ctrl;
  355. if (host->board->rdy_pin)
  356. nand_chip->dev_ready = at91_nand_device_ready;
  357. regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  358. if (!regs && hard_ecc) {
  359. printk(KERN_ERR "at91_nand: can't get I/O resource "
  360. "regs\nFalling back on software ECC\n");
  361. }
  362. nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
  363. if (no_ecc)
  364. nand_chip->ecc.mode = NAND_ECC_NONE;
  365. if (hard_ecc && regs) {
  366. host->ecc = ioremap(regs->start, regs->end - regs->start + 1);
  367. if (host->ecc == NULL) {
  368. printk(KERN_ERR "at91_nand: ioremap failed\n");
  369. res = -EIO;
  370. goto err_ecc_ioremap;
  371. }
  372. nand_chip->ecc.mode = NAND_ECC_HW_SYNDROME;
  373. nand_chip->ecc.calculate = at91_nand_calculate;
  374. nand_chip->ecc.correct = at91_nand_correct;
  375. nand_chip->ecc.hwctl = at91_nand_hwctl;
  376. nand_chip->ecc.read_page = at91_nand_read_page;
  377. nand_chip->ecc.bytes = 4;
  378. nand_chip->ecc.prepad = 0;
  379. nand_chip->ecc.postpad = 0;
  380. }
  381. nand_chip->chip_delay = 20; /* 20us command delay time */
  382. if (host->board->bus_width_16) /* 16-bit bus width */
  383. nand_chip->options |= NAND_BUSWIDTH_16;
  384. platform_set_drvdata(pdev, host);
  385. at91_nand_enable(host);
  386. if (host->board->det_pin) {
  387. if (at91_get_gpio_value(host->board->det_pin)) {
  388. printk ("No SmartMedia card inserted.\n");
  389. res = ENXIO;
  390. goto out;
  391. }
  392. }
  393. /* first scan to find the device and get the page size */
  394. if (nand_scan_ident(mtd, 1)) {
  395. res = -ENXIO;
  396. goto out;
  397. }
  398. if (nand_chip->ecc.mode == NAND_ECC_HW_SYNDROME) {
  399. /* ECC is calculated for the whole page (1 step) */
  400. nand_chip->ecc.size = mtd->writesize;
  401. /* set ECC page size and oob layout */
  402. switch (mtd->writesize) {
  403. case 512:
  404. nand_chip->ecc.layout = &at91_oobinfo_small;
  405. nand_chip->ecc.read_oob = at91_nand_read_oob_512;
  406. nand_chip->ecc.write_oob = at91_nand_write_oob_512;
  407. ecc_writel(host->ecc, MR, AT91_ECC_PAGESIZE_528);
  408. break;
  409. case 1024:
  410. nand_chip->ecc.layout = &at91_oobinfo_large;
  411. ecc_writel(host->ecc, MR, AT91_ECC_PAGESIZE_1056);
  412. break;
  413. case 2048:
  414. nand_chip->ecc.layout = &at91_oobinfo_large;
  415. ecc_writel(host->ecc, MR, AT91_ECC_PAGESIZE_2112);
  416. break;
  417. case 4096:
  418. nand_chip->ecc.layout = &at91_oobinfo_large;
  419. ecc_writel(host->ecc, MR, AT91_ECC_PAGESIZE_4224);
  420. break;
  421. default:
  422. /* page size not handled by HW ECC */
  423. /* switching back to soft ECC */
  424. nand_chip->ecc.mode = NAND_ECC_SOFT;
  425. nand_chip->ecc.calculate = NULL;
  426. nand_chip->ecc.correct = NULL;
  427. nand_chip->ecc.hwctl = NULL;
  428. nand_chip->ecc.read_page = NULL;
  429. nand_chip->ecc.postpad = 0;
  430. nand_chip->ecc.prepad = 0;
  431. nand_chip->ecc.bytes = 0;
  432. break;
  433. }
  434. }
  435. /* second phase scan */
  436. if (nand_scan_tail(mtd)) {
  437. res = -ENXIO;
  438. goto out;
  439. }
  440. #ifdef CONFIG_MTD_PARTITIONS
  441. #ifdef CONFIG_MTD_CMDLINE_PARTS
  442. mtd->name = "at91_nand";
  443. num_partitions = parse_mtd_partitions(mtd, part_probes,
  444. &partitions, 0);
  445. #endif
  446. if (num_partitions <= 0 && host->board->partition_info)
  447. partitions = host->board->partition_info(mtd->size,
  448. &num_partitions);
  449. if ((!partitions) || (num_partitions == 0)) {
  450. printk(KERN_ERR "at91_nand: No parititions defined, or unsupported device.\n");
  451. res = ENXIO;
  452. goto release;
  453. }
  454. res = add_mtd_partitions(mtd, partitions, num_partitions);
  455. #else
  456. res = add_mtd_device(mtd);
  457. #endif
  458. if (!res)
  459. return res;
  460. #ifdef CONFIG_MTD_PARTITIONS
  461. release:
  462. #endif
  463. nand_release(mtd);
  464. out:
  465. iounmap(host->ecc);
  466. err_ecc_ioremap:
  467. at91_nand_disable(host);
  468. platform_set_drvdata(pdev, NULL);
  469. iounmap(host->io_base);
  470. kfree(host);
  471. return res;
  472. }
  473. /*
  474. * Remove a NAND device.
  475. */
  476. static int __devexit at91_nand_remove(struct platform_device *pdev)
  477. {
  478. struct at91_nand_host *host = platform_get_drvdata(pdev);
  479. struct mtd_info *mtd = &host->mtd;
  480. nand_release(mtd);
  481. at91_nand_disable(host);
  482. iounmap(host->io_base);
  483. iounmap(host->ecc);
  484. kfree(host);
  485. return 0;
  486. }
  487. static struct platform_driver at91_nand_driver = {
  488. .probe = at91_nand_probe,
  489. .remove = at91_nand_remove,
  490. .driver = {
  491. .name = "at91_nand",
  492. .owner = THIS_MODULE,
  493. },
  494. };
  495. static int __init at91_nand_init(void)
  496. {
  497. return platform_driver_register(&at91_nand_driver);
  498. }
  499. static void __exit at91_nand_exit(void)
  500. {
  501. platform_driver_unregister(&at91_nand_driver);
  502. }
  503. module_init(at91_nand_init);
  504. module_exit(at91_nand_exit);
  505. MODULE_LICENSE("GPL");
  506. MODULE_AUTHOR("Rick Bronson");
  507. MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91RM9200 / AT91SAM9");
  508. MODULE_ALIAS("platform:at91_nand");