at91_nand.c 14 KB

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