atmel_nand.c 15 KB

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