atmel_nand.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. !!host->board->rdy_pin_active_low;
  127. }
  128. /*
  129. * Minimal-overhead PIO for data access.
  130. */
  131. static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  132. {
  133. struct nand_chip *nand_chip = mtd->priv;
  134. __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
  135. }
  136. static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
  137. {
  138. struct nand_chip *nand_chip = mtd->priv;
  139. __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
  140. }
  141. static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  142. {
  143. struct nand_chip *nand_chip = mtd->priv;
  144. __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
  145. }
  146. static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
  147. {
  148. struct nand_chip *nand_chip = mtd->priv;
  149. __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
  150. }
  151. /*
  152. * Calculate HW ECC
  153. *
  154. * function called after a write
  155. *
  156. * mtd: MTD block structure
  157. * dat: raw data (unused)
  158. * ecc_code: buffer for ECC
  159. */
  160. static int atmel_nand_calculate(struct mtd_info *mtd,
  161. const u_char *dat, unsigned char *ecc_code)
  162. {
  163. struct nand_chip *nand_chip = mtd->priv;
  164. struct atmel_nand_host *host = nand_chip->priv;
  165. uint32_t *eccpos = nand_chip->ecc.layout->eccpos;
  166. unsigned int ecc_value;
  167. /* get the first 2 ECC bytes */
  168. ecc_value = ecc_readl(host->ecc, PR);
  169. ecc_code[0] = ecc_value & 0xFF;
  170. ecc_code[1] = (ecc_value >> 8) & 0xFF;
  171. /* get the last 2 ECC bytes */
  172. ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
  173. ecc_code[2] = ecc_value & 0xFF;
  174. ecc_code[3] = (ecc_value >> 8) & 0xFF;
  175. return 0;
  176. }
  177. /*
  178. * HW ECC read page function
  179. *
  180. * mtd: mtd info structure
  181. * chip: nand chip info structure
  182. * buf: buffer to store read data
  183. */
  184. static int atmel_nand_read_page(struct mtd_info *mtd,
  185. struct nand_chip *chip, uint8_t *buf)
  186. {
  187. int eccsize = chip->ecc.size;
  188. int eccbytes = chip->ecc.bytes;
  189. uint32_t *eccpos = chip->ecc.layout->eccpos;
  190. uint8_t *p = buf;
  191. uint8_t *oob = chip->oob_poi;
  192. uint8_t *ecc_pos;
  193. int stat;
  194. /*
  195. * Errata: ALE is incorrectly wired up to the ECC controller
  196. * on the AP7000, so it will include the address cycles in the
  197. * ECC calculation.
  198. *
  199. * Workaround: Reset the parity registers before reading the
  200. * actual data.
  201. */
  202. if (cpu_is_at32ap7000()) {
  203. struct atmel_nand_host *host = chip->priv;
  204. ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
  205. }
  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 atmel_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 atmel_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 & ATMEL_ECC_RECERR)))
  257. return 0;
  258. /* get error bit offset (4 bits) */
  259. ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
  260. /* get word address (12 bits) */
  261. ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
  262. ecc_word >>= 4;
  263. /* if there are multiple errors */
  264. if (ecc_status & ATMEL_ECC_MULERR) {
  265. /* check if it is a freshly erased block
  266. * (filled with 0xff) */
  267. if ((ecc_bit == ATMEL_ECC_BITADDR)
  268. && (ecc_word == (ATMEL_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, "atmel_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 & ATMEL_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, "atmel_nand : one bit error on ECC code."
  285. " Nothing to correct\n");
  286. return 0;
  287. }
  288. dev_dbg(host->dev, "atmel_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, "atmel_nand : error corrected\n");
  301. return 1;
  302. }
  303. /*
  304. * Enable HW ECC : unused on most chips
  305. */
  306. static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
  307. {
  308. if (cpu_is_at32ap7000()) {
  309. struct nand_chip *nand_chip = mtd->priv;
  310. struct atmel_nand_host *host = nand_chip->priv;
  311. ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
  312. }
  313. }
  314. #ifdef CONFIG_MTD_PARTITIONS
  315. static const char *part_probes[] = { "cmdlinepart", NULL };
  316. #endif
  317. /*
  318. * Probe for the NAND device.
  319. */
  320. static int __init atmel_nand_probe(struct platform_device *pdev)
  321. {
  322. struct atmel_nand_host *host;
  323. struct mtd_info *mtd;
  324. struct nand_chip *nand_chip;
  325. struct resource *regs;
  326. struct resource *mem;
  327. int res;
  328. #ifdef CONFIG_MTD_PARTITIONS
  329. struct mtd_partition *partitions = NULL;
  330. int num_partitions = 0;
  331. #endif
  332. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  333. if (!mem) {
  334. printk(KERN_ERR "atmel_nand: can't get I/O resource mem\n");
  335. return -ENXIO;
  336. }
  337. /* Allocate memory for the device structure (and zero it) */
  338. host = kzalloc(sizeof(struct atmel_nand_host), GFP_KERNEL);
  339. if (!host) {
  340. printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
  341. return -ENOMEM;
  342. }
  343. host->io_base = ioremap(mem->start, mem->end - mem->start + 1);
  344. if (host->io_base == NULL) {
  345. printk(KERN_ERR "atmel_nand: ioremap failed\n");
  346. res = -EIO;
  347. goto err_nand_ioremap;
  348. }
  349. mtd = &host->mtd;
  350. nand_chip = &host->nand_chip;
  351. host->board = pdev->dev.platform_data;
  352. host->dev = &pdev->dev;
  353. nand_chip->priv = host; /* link the private data structures */
  354. mtd->priv = nand_chip;
  355. mtd->owner = THIS_MODULE;
  356. /* Set address of NAND IO lines */
  357. nand_chip->IO_ADDR_R = host->io_base;
  358. nand_chip->IO_ADDR_W = host->io_base;
  359. nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
  360. if (host->board->rdy_pin)
  361. nand_chip->dev_ready = atmel_nand_device_ready;
  362. regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  363. if (!regs && hard_ecc) {
  364. printk(KERN_ERR "atmel_nand: can't get I/O resource "
  365. "regs\nFalling back on software ECC\n");
  366. }
  367. nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
  368. if (no_ecc)
  369. nand_chip->ecc.mode = NAND_ECC_NONE;
  370. if (hard_ecc && regs) {
  371. host->ecc = ioremap(regs->start, regs->end - regs->start + 1);
  372. if (host->ecc == NULL) {
  373. printk(KERN_ERR "atmel_nand: ioremap failed\n");
  374. res = -EIO;
  375. goto err_ecc_ioremap;
  376. }
  377. nand_chip->ecc.mode = NAND_ECC_HW;
  378. nand_chip->ecc.calculate = atmel_nand_calculate;
  379. nand_chip->ecc.correct = atmel_nand_correct;
  380. nand_chip->ecc.hwctl = atmel_nand_hwctl;
  381. nand_chip->ecc.read_page = atmel_nand_read_page;
  382. nand_chip->ecc.bytes = 4;
  383. }
  384. nand_chip->chip_delay = 20; /* 20us command delay time */
  385. if (host->board->bus_width_16) { /* 16-bit bus width */
  386. nand_chip->options |= NAND_BUSWIDTH_16;
  387. nand_chip->read_buf = atmel_read_buf16;
  388. nand_chip->write_buf = atmel_write_buf16;
  389. } else {
  390. nand_chip->read_buf = atmel_read_buf;
  391. nand_chip->write_buf = atmel_write_buf;
  392. }
  393. platform_set_drvdata(pdev, host);
  394. atmel_nand_enable(host);
  395. if (host->board->det_pin) {
  396. if (gpio_get_value(host->board->det_pin)) {
  397. printk("No SmartMedia card inserted.\n");
  398. res = ENXIO;
  399. goto err_no_card;
  400. }
  401. }
  402. /* first scan to find the device and get the page size */
  403. if (nand_scan_ident(mtd, 1)) {
  404. res = -ENXIO;
  405. goto err_scan_ident;
  406. }
  407. if (nand_chip->ecc.mode == NAND_ECC_HW) {
  408. /* ECC is calculated for the whole page (1 step) */
  409. nand_chip->ecc.size = mtd->writesize;
  410. /* set ECC page size and oob layout */
  411. switch (mtd->writesize) {
  412. case 512:
  413. nand_chip->ecc.layout = &atmel_oobinfo_small;
  414. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
  415. break;
  416. case 1024:
  417. nand_chip->ecc.layout = &atmel_oobinfo_large;
  418. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
  419. break;
  420. case 2048:
  421. nand_chip->ecc.layout = &atmel_oobinfo_large;
  422. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
  423. break;
  424. case 4096:
  425. nand_chip->ecc.layout = &atmel_oobinfo_large;
  426. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
  427. break;
  428. default:
  429. /* page size not handled by HW ECC */
  430. /* switching back to soft ECC */
  431. nand_chip->ecc.mode = NAND_ECC_SOFT;
  432. nand_chip->ecc.calculate = NULL;
  433. nand_chip->ecc.correct = NULL;
  434. nand_chip->ecc.hwctl = NULL;
  435. nand_chip->ecc.read_page = NULL;
  436. nand_chip->ecc.postpad = 0;
  437. nand_chip->ecc.prepad = 0;
  438. nand_chip->ecc.bytes = 0;
  439. break;
  440. }
  441. }
  442. /* second phase scan */
  443. if (nand_scan_tail(mtd)) {
  444. res = -ENXIO;
  445. goto err_scan_tail;
  446. }
  447. #ifdef CONFIG_MTD_PARTITIONS
  448. #ifdef CONFIG_MTD_CMDLINE_PARTS
  449. mtd->name = "atmel_nand";
  450. num_partitions = parse_mtd_partitions(mtd, part_probes,
  451. &partitions, 0);
  452. #endif
  453. if (num_partitions <= 0 && host->board->partition_info)
  454. partitions = host->board->partition_info(mtd->size,
  455. &num_partitions);
  456. if ((!partitions) || (num_partitions == 0)) {
  457. printk(KERN_ERR "atmel_nand: No parititions defined, or unsupported device.\n");
  458. res = ENXIO;
  459. goto err_no_partitions;
  460. }
  461. res = add_mtd_partitions(mtd, partitions, num_partitions);
  462. #else
  463. res = add_mtd_device(mtd);
  464. #endif
  465. if (!res)
  466. return res;
  467. #ifdef CONFIG_MTD_PARTITIONS
  468. err_no_partitions:
  469. #endif
  470. nand_release(mtd);
  471. err_scan_tail:
  472. err_scan_ident:
  473. err_no_card:
  474. atmel_nand_disable(host);
  475. platform_set_drvdata(pdev, NULL);
  476. if (host->ecc)
  477. iounmap(host->ecc);
  478. err_ecc_ioremap:
  479. iounmap(host->io_base);
  480. err_nand_ioremap:
  481. kfree(host);
  482. return res;
  483. }
  484. /*
  485. * Remove a NAND device.
  486. */
  487. static int __exit atmel_nand_remove(struct platform_device *pdev)
  488. {
  489. struct atmel_nand_host *host = platform_get_drvdata(pdev);
  490. struct mtd_info *mtd = &host->mtd;
  491. nand_release(mtd);
  492. atmel_nand_disable(host);
  493. if (host->ecc)
  494. iounmap(host->ecc);
  495. iounmap(host->io_base);
  496. kfree(host);
  497. return 0;
  498. }
  499. static struct platform_driver atmel_nand_driver = {
  500. .remove = __exit_p(atmel_nand_remove),
  501. .driver = {
  502. .name = "atmel_nand",
  503. .owner = THIS_MODULE,
  504. },
  505. };
  506. static int __init atmel_nand_init(void)
  507. {
  508. return platform_driver_probe(&atmel_nand_driver, atmel_nand_probe);
  509. }
  510. static void __exit atmel_nand_exit(void)
  511. {
  512. platform_driver_unregister(&atmel_nand_driver);
  513. }
  514. module_init(atmel_nand_init);
  515. module_exit(atmel_nand_exit);
  516. MODULE_LICENSE("GPL");
  517. MODULE_AUTHOR("Rick Bronson");
  518. MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
  519. MODULE_ALIAS("platform:atmel_nand");