atmel_nand.c 15 KB

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