atmel_nand.c 15 KB

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