atmel_nand.c 15 KB

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