atmel_nand.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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 use_dma = 1;
  46. module_param(use_dma, int, 0);
  47. static int on_flash_bbt = 0;
  48. module_param(on_flash_bbt, int, 0);
  49. /* Register access macros */
  50. #define ecc_readl(add, reg) \
  51. __raw_readl(add + ATMEL_ECC_##reg)
  52. #define ecc_writel(add, reg, value) \
  53. __raw_writel((value), add + ATMEL_ECC_##reg)
  54. #include "atmel_nand_ecc.h" /* Hardware ECC registers */
  55. /* oob layout for large page size
  56. * bad block info is on bytes 0 and 1
  57. * the bytes have to be consecutives to avoid
  58. * several NAND_CMD_RNDOUT during read
  59. */
  60. static struct nand_ecclayout atmel_oobinfo_large = {
  61. .eccbytes = 4,
  62. .eccpos = {60, 61, 62, 63},
  63. .oobfree = {
  64. {2, 58}
  65. },
  66. };
  67. /* oob layout for small page size
  68. * bad block info is on bytes 4 and 5
  69. * the bytes have to be consecutives to avoid
  70. * several NAND_CMD_RNDOUT during read
  71. */
  72. static struct nand_ecclayout atmel_oobinfo_small = {
  73. .eccbytes = 4,
  74. .eccpos = {0, 1, 2, 3},
  75. .oobfree = {
  76. {6, 10}
  77. },
  78. };
  79. struct atmel_nand_host {
  80. struct nand_chip nand_chip;
  81. struct mtd_info mtd;
  82. void __iomem *io_base;
  83. dma_addr_t io_phys;
  84. struct atmel_nand_data *board;
  85. struct device *dev;
  86. void __iomem *ecc;
  87. struct completion comp;
  88. struct dma_chan *dma_chan;
  89. };
  90. static int cpu_has_dma(void)
  91. {
  92. return cpu_is_at91sam9rl() || cpu_is_at91sam9g45();
  93. }
  94. /*
  95. * Enable NAND.
  96. */
  97. static void atmel_nand_enable(struct atmel_nand_host *host)
  98. {
  99. if (host->board->enable_pin)
  100. gpio_set_value(host->board->enable_pin, 0);
  101. }
  102. /*
  103. * Disable NAND.
  104. */
  105. static void atmel_nand_disable(struct atmel_nand_host *host)
  106. {
  107. if (host->board->enable_pin)
  108. gpio_set_value(host->board->enable_pin, 1);
  109. }
  110. /*
  111. * Hardware specific access to control-lines
  112. */
  113. static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  114. {
  115. struct nand_chip *nand_chip = mtd->priv;
  116. struct atmel_nand_host *host = nand_chip->priv;
  117. if (ctrl & NAND_CTRL_CHANGE) {
  118. if (ctrl & NAND_NCE)
  119. atmel_nand_enable(host);
  120. else
  121. atmel_nand_disable(host);
  122. }
  123. if (cmd == NAND_CMD_NONE)
  124. return;
  125. if (ctrl & NAND_CLE)
  126. writeb(cmd, host->io_base + (1 << host->board->cle));
  127. else
  128. writeb(cmd, host->io_base + (1 << host->board->ale));
  129. }
  130. /*
  131. * Read the Device Ready pin.
  132. */
  133. static int atmel_nand_device_ready(struct mtd_info *mtd)
  134. {
  135. struct nand_chip *nand_chip = mtd->priv;
  136. struct atmel_nand_host *host = nand_chip->priv;
  137. return gpio_get_value(host->board->rdy_pin) ^
  138. !!host->board->rdy_pin_active_low;
  139. }
  140. /*
  141. * Minimal-overhead PIO for data access.
  142. */
  143. static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
  144. {
  145. struct nand_chip *nand_chip = mtd->priv;
  146. __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
  147. }
  148. static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
  149. {
  150. struct nand_chip *nand_chip = mtd->priv;
  151. __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
  152. }
  153. static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
  154. {
  155. struct nand_chip *nand_chip = mtd->priv;
  156. __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
  157. }
  158. static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
  159. {
  160. struct nand_chip *nand_chip = mtd->priv;
  161. __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
  162. }
  163. static void dma_complete_func(void *completion)
  164. {
  165. complete(completion);
  166. }
  167. static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
  168. int is_read)
  169. {
  170. struct dma_device *dma_dev;
  171. enum dma_ctrl_flags flags;
  172. dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
  173. struct dma_async_tx_descriptor *tx = NULL;
  174. dma_cookie_t cookie;
  175. struct nand_chip *chip = mtd->priv;
  176. struct atmel_nand_host *host = chip->priv;
  177. void *p = buf;
  178. int err = -EIO;
  179. enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  180. if (buf >= high_memory) {
  181. struct page *pg;
  182. if (((size_t)buf & PAGE_MASK) !=
  183. ((size_t)(buf + len - 1) & PAGE_MASK)) {
  184. dev_warn(host->dev, "Buffer not fit in one page\n");
  185. goto err_buf;
  186. }
  187. pg = vmalloc_to_page(buf);
  188. if (pg == 0) {
  189. dev_err(host->dev, "Failed to vmalloc_to_page\n");
  190. goto err_buf;
  191. }
  192. p = page_address(pg) + ((size_t)buf & ~PAGE_MASK);
  193. }
  194. dma_dev = host->dma_chan->device;
  195. flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT | DMA_COMPL_SKIP_SRC_UNMAP |
  196. DMA_COMPL_SKIP_DEST_UNMAP;
  197. phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
  198. if (dma_mapping_error(dma_dev->dev, phys_addr)) {
  199. dev_err(host->dev, "Failed to dma_map_single\n");
  200. goto err_buf;
  201. }
  202. if (is_read) {
  203. dma_src_addr = host->io_phys;
  204. dma_dst_addr = phys_addr;
  205. } else {
  206. dma_src_addr = phys_addr;
  207. dma_dst_addr = host->io_phys;
  208. }
  209. tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
  210. dma_src_addr, len, flags);
  211. if (!tx) {
  212. dev_err(host->dev, "Failed to prepare DMA memcpy\n");
  213. goto err_dma;
  214. }
  215. init_completion(&host->comp);
  216. tx->callback = dma_complete_func;
  217. tx->callback_param = &host->comp;
  218. cookie = tx->tx_submit(tx);
  219. if (dma_submit_error(cookie)) {
  220. dev_err(host->dev, "Failed to do DMA tx_submit\n");
  221. goto err_dma;
  222. }
  223. dma_async_issue_pending(host->dma_chan);
  224. wait_for_completion(&host->comp);
  225. err = 0;
  226. err_dma:
  227. dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
  228. err_buf:
  229. if (err != 0)
  230. dev_warn(host->dev, "Fall back to CPU I/O\n");
  231. return err;
  232. }
  233. static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  234. {
  235. struct nand_chip *chip = mtd->priv;
  236. struct atmel_nand_host *host = chip->priv;
  237. if (use_dma && len > mtd->oobsize)
  238. /* only use DMA for bigger than oob size: better performances */
  239. if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
  240. return;
  241. if (host->board->bus_width_16)
  242. atmel_read_buf16(mtd, buf, len);
  243. else
  244. atmel_read_buf8(mtd, buf, len);
  245. }
  246. static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  247. {
  248. struct nand_chip *chip = mtd->priv;
  249. struct atmel_nand_host *host = chip->priv;
  250. if (use_dma && len > mtd->oobsize)
  251. /* only use DMA for bigger than oob size: better performances */
  252. if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
  253. return;
  254. if (host->board->bus_width_16)
  255. atmel_write_buf16(mtd, buf, len);
  256. else
  257. atmel_write_buf8(mtd, buf, len);
  258. }
  259. /*
  260. * Calculate HW ECC
  261. *
  262. * function called after a write
  263. *
  264. * mtd: MTD block structure
  265. * dat: raw data (unused)
  266. * ecc_code: buffer for ECC
  267. */
  268. static int atmel_nand_calculate(struct mtd_info *mtd,
  269. const u_char *dat, unsigned char *ecc_code)
  270. {
  271. struct nand_chip *nand_chip = mtd->priv;
  272. struct atmel_nand_host *host = nand_chip->priv;
  273. unsigned int ecc_value;
  274. /* get the first 2 ECC bytes */
  275. ecc_value = ecc_readl(host->ecc, PR);
  276. ecc_code[0] = ecc_value & 0xFF;
  277. ecc_code[1] = (ecc_value >> 8) & 0xFF;
  278. /* get the last 2 ECC bytes */
  279. ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
  280. ecc_code[2] = ecc_value & 0xFF;
  281. ecc_code[3] = (ecc_value >> 8) & 0xFF;
  282. return 0;
  283. }
  284. /*
  285. * HW ECC read page function
  286. *
  287. * mtd: mtd info structure
  288. * chip: nand chip info structure
  289. * buf: buffer to store read data
  290. */
  291. static int atmel_nand_read_page(struct mtd_info *mtd,
  292. struct nand_chip *chip, uint8_t *buf, int page)
  293. {
  294. int eccsize = chip->ecc.size;
  295. int eccbytes = chip->ecc.bytes;
  296. uint32_t *eccpos = chip->ecc.layout->eccpos;
  297. uint8_t *p = buf;
  298. uint8_t *oob = chip->oob_poi;
  299. uint8_t *ecc_pos;
  300. int stat;
  301. /*
  302. * Errata: ALE is incorrectly wired up to the ECC controller
  303. * on the AP7000, so it will include the address cycles in the
  304. * ECC calculation.
  305. *
  306. * Workaround: Reset the parity registers before reading the
  307. * actual data.
  308. */
  309. if (cpu_is_at32ap7000()) {
  310. struct atmel_nand_host *host = chip->priv;
  311. ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
  312. }
  313. /* read the page */
  314. chip->read_buf(mtd, p, eccsize);
  315. /* move to ECC position if needed */
  316. if (eccpos[0] != 0) {
  317. /* This only works on large pages
  318. * because the ECC controller waits for
  319. * NAND_CMD_RNDOUTSTART after the
  320. * NAND_CMD_RNDOUT.
  321. * anyway, for small pages, the eccpos[0] == 0
  322. */
  323. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  324. mtd->writesize + eccpos[0], -1);
  325. }
  326. /* the ECC controller needs to read the ECC just after the data */
  327. ecc_pos = oob + eccpos[0];
  328. chip->read_buf(mtd, ecc_pos, eccbytes);
  329. /* check if there's an error */
  330. stat = chip->ecc.correct(mtd, p, oob, NULL);
  331. if (stat < 0)
  332. mtd->ecc_stats.failed++;
  333. else
  334. mtd->ecc_stats.corrected += stat;
  335. /* get back to oob start (end of page) */
  336. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  337. /* read the oob */
  338. chip->read_buf(mtd, oob, mtd->oobsize);
  339. return 0;
  340. }
  341. /*
  342. * HW ECC Correction
  343. *
  344. * function called after a read
  345. *
  346. * mtd: MTD block structure
  347. * dat: raw data read from the chip
  348. * read_ecc: ECC from the chip (unused)
  349. * isnull: unused
  350. *
  351. * Detect and correct a 1 bit error for a page
  352. */
  353. static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
  354. u_char *read_ecc, u_char *isnull)
  355. {
  356. struct nand_chip *nand_chip = mtd->priv;
  357. struct atmel_nand_host *host = nand_chip->priv;
  358. unsigned int ecc_status;
  359. unsigned int ecc_word, ecc_bit;
  360. /* get the status from the Status Register */
  361. ecc_status = ecc_readl(host->ecc, SR);
  362. /* if there's no error */
  363. if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
  364. return 0;
  365. /* get error bit offset (4 bits) */
  366. ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
  367. /* get word address (12 bits) */
  368. ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
  369. ecc_word >>= 4;
  370. /* if there are multiple errors */
  371. if (ecc_status & ATMEL_ECC_MULERR) {
  372. /* check if it is a freshly erased block
  373. * (filled with 0xff) */
  374. if ((ecc_bit == ATMEL_ECC_BITADDR)
  375. && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
  376. /* the block has just been erased, return OK */
  377. return 0;
  378. }
  379. /* it doesn't seems to be a freshly
  380. * erased block.
  381. * We can't correct so many errors */
  382. dev_dbg(host->dev, "atmel_nand : multiple errors detected."
  383. " Unable to correct.\n");
  384. return -EIO;
  385. }
  386. /* if there's a single bit error : we can correct it */
  387. if (ecc_status & ATMEL_ECC_ECCERR) {
  388. /* there's nothing much to do here.
  389. * the bit error is on the ECC itself.
  390. */
  391. dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
  392. " Nothing to correct\n");
  393. return 0;
  394. }
  395. dev_dbg(host->dev, "atmel_nand : one bit error on data."
  396. " (word offset in the page :"
  397. " 0x%x bit offset : 0x%x)\n",
  398. ecc_word, ecc_bit);
  399. /* correct the error */
  400. if (nand_chip->options & NAND_BUSWIDTH_16) {
  401. /* 16 bits words */
  402. ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
  403. } else {
  404. /* 8 bits words */
  405. dat[ecc_word] ^= (1 << ecc_bit);
  406. }
  407. dev_dbg(host->dev, "atmel_nand : error corrected\n");
  408. return 1;
  409. }
  410. /*
  411. * Enable HW ECC : unused on most chips
  412. */
  413. static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
  414. {
  415. if (cpu_is_at32ap7000()) {
  416. struct nand_chip *nand_chip = mtd->priv;
  417. struct atmel_nand_host *host = nand_chip->priv;
  418. ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
  419. }
  420. }
  421. #ifdef CONFIG_MTD_CMDLINE_PARTS
  422. static const char *part_probes[] = { "cmdlinepart", NULL };
  423. #endif
  424. /*
  425. * Probe for the NAND device.
  426. */
  427. static int __init atmel_nand_probe(struct platform_device *pdev)
  428. {
  429. struct atmel_nand_host *host;
  430. struct mtd_info *mtd;
  431. struct nand_chip *nand_chip;
  432. struct resource *regs;
  433. struct resource *mem;
  434. int res;
  435. #ifdef CONFIG_MTD_PARTITIONS
  436. struct mtd_partition *partitions = NULL;
  437. int num_partitions = 0;
  438. #endif
  439. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  440. if (!mem) {
  441. printk(KERN_ERR "atmel_nand: can't get I/O resource mem\n");
  442. return -ENXIO;
  443. }
  444. /* Allocate memory for the device structure (and zero it) */
  445. host = kzalloc(sizeof(struct atmel_nand_host), GFP_KERNEL);
  446. if (!host) {
  447. printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
  448. return -ENOMEM;
  449. }
  450. host->io_phys = (dma_addr_t)mem->start;
  451. host->io_base = ioremap(mem->start, mem->end - mem->start + 1);
  452. if (host->io_base == NULL) {
  453. printk(KERN_ERR "atmel_nand: ioremap failed\n");
  454. res = -EIO;
  455. goto err_nand_ioremap;
  456. }
  457. mtd = &host->mtd;
  458. nand_chip = &host->nand_chip;
  459. host->board = pdev->dev.platform_data;
  460. host->dev = &pdev->dev;
  461. nand_chip->priv = host; /* link the private data structures */
  462. mtd->priv = nand_chip;
  463. mtd->owner = THIS_MODULE;
  464. /* Set address of NAND IO lines */
  465. nand_chip->IO_ADDR_R = host->io_base;
  466. nand_chip->IO_ADDR_W = host->io_base;
  467. nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
  468. if (host->board->rdy_pin)
  469. nand_chip->dev_ready = atmel_nand_device_ready;
  470. regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  471. if (!regs && hard_ecc) {
  472. printk(KERN_ERR "atmel_nand: can't get I/O resource "
  473. "regs\nFalling back on software ECC\n");
  474. }
  475. nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
  476. if (no_ecc)
  477. nand_chip->ecc.mode = NAND_ECC_NONE;
  478. if (hard_ecc && regs) {
  479. host->ecc = ioremap(regs->start, regs->end - regs->start + 1);
  480. if (host->ecc == NULL) {
  481. printk(KERN_ERR "atmel_nand: ioremap failed\n");
  482. res = -EIO;
  483. goto err_ecc_ioremap;
  484. }
  485. nand_chip->ecc.mode = NAND_ECC_HW;
  486. nand_chip->ecc.calculate = atmel_nand_calculate;
  487. nand_chip->ecc.correct = atmel_nand_correct;
  488. nand_chip->ecc.hwctl = atmel_nand_hwctl;
  489. nand_chip->ecc.read_page = atmel_nand_read_page;
  490. nand_chip->ecc.bytes = 4;
  491. }
  492. nand_chip->chip_delay = 20; /* 20us command delay time */
  493. if (host->board->bus_width_16) /* 16-bit bus width */
  494. nand_chip->options |= NAND_BUSWIDTH_16;
  495. nand_chip->read_buf = atmel_read_buf;
  496. nand_chip->write_buf = atmel_write_buf;
  497. platform_set_drvdata(pdev, host);
  498. atmel_nand_enable(host);
  499. if (host->board->det_pin) {
  500. if (gpio_get_value(host->board->det_pin)) {
  501. printk(KERN_INFO "No SmartMedia card inserted.\n");
  502. res = -ENXIO;
  503. goto err_no_card;
  504. }
  505. }
  506. if (on_flash_bbt) {
  507. printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
  508. nand_chip->options |= NAND_USE_FLASH_BBT;
  509. }
  510. if (!cpu_has_dma())
  511. use_dma = 0;
  512. if (use_dma) {
  513. dma_cap_mask_t mask;
  514. dma_cap_zero(mask);
  515. dma_cap_set(DMA_MEMCPY, mask);
  516. host->dma_chan = dma_request_channel(mask, 0, NULL);
  517. if (!host->dma_chan) {
  518. dev_err(host->dev, "Failed to request DMA channel\n");
  519. use_dma = 0;
  520. }
  521. }
  522. if (use_dma)
  523. dev_info(host->dev, "Using %s for DMA transfers.\n",
  524. dma_chan_name(host->dma_chan));
  525. else
  526. dev_info(host->dev, "No DMA support for NAND access.\n");
  527. /* first scan to find the device and get the page size */
  528. if (nand_scan_ident(mtd, 1, NULL)) {
  529. res = -ENXIO;
  530. goto err_scan_ident;
  531. }
  532. if (nand_chip->ecc.mode == NAND_ECC_HW) {
  533. /* ECC is calculated for the whole page (1 step) */
  534. nand_chip->ecc.size = mtd->writesize;
  535. /* set ECC page size and oob layout */
  536. switch (mtd->writesize) {
  537. case 512:
  538. nand_chip->ecc.layout = &atmel_oobinfo_small;
  539. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
  540. break;
  541. case 1024:
  542. nand_chip->ecc.layout = &atmel_oobinfo_large;
  543. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
  544. break;
  545. case 2048:
  546. nand_chip->ecc.layout = &atmel_oobinfo_large;
  547. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
  548. break;
  549. case 4096:
  550. nand_chip->ecc.layout = &atmel_oobinfo_large;
  551. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
  552. break;
  553. default:
  554. /* page size not handled by HW ECC */
  555. /* switching back to soft ECC */
  556. nand_chip->ecc.mode = NAND_ECC_SOFT;
  557. nand_chip->ecc.calculate = NULL;
  558. nand_chip->ecc.correct = NULL;
  559. nand_chip->ecc.hwctl = NULL;
  560. nand_chip->ecc.read_page = NULL;
  561. nand_chip->ecc.postpad = 0;
  562. nand_chip->ecc.prepad = 0;
  563. nand_chip->ecc.bytes = 0;
  564. break;
  565. }
  566. }
  567. /* second phase scan */
  568. if (nand_scan_tail(mtd)) {
  569. res = -ENXIO;
  570. goto err_scan_tail;
  571. }
  572. #ifdef CONFIG_MTD_PARTITIONS
  573. #ifdef CONFIG_MTD_CMDLINE_PARTS
  574. mtd->name = "atmel_nand";
  575. num_partitions = parse_mtd_partitions(mtd, part_probes,
  576. &partitions, 0);
  577. #endif
  578. if (num_partitions <= 0 && host->board->partition_info)
  579. partitions = host->board->partition_info(mtd->size,
  580. &num_partitions);
  581. if ((!partitions) || (num_partitions == 0)) {
  582. printk(KERN_ERR "atmel_nand: No partitions defined, or unsupported device.\n");
  583. res = -ENXIO;
  584. goto err_no_partitions;
  585. }
  586. res = add_mtd_partitions(mtd, partitions, num_partitions);
  587. #else
  588. res = add_mtd_device(mtd);
  589. #endif
  590. if (!res)
  591. return res;
  592. #ifdef CONFIG_MTD_PARTITIONS
  593. err_no_partitions:
  594. #endif
  595. nand_release(mtd);
  596. err_scan_tail:
  597. err_scan_ident:
  598. err_no_card:
  599. atmel_nand_disable(host);
  600. platform_set_drvdata(pdev, NULL);
  601. if (host->dma_chan)
  602. dma_release_channel(host->dma_chan);
  603. if (host->ecc)
  604. iounmap(host->ecc);
  605. err_ecc_ioremap:
  606. iounmap(host->io_base);
  607. err_nand_ioremap:
  608. kfree(host);
  609. return res;
  610. }
  611. /*
  612. * Remove a NAND device.
  613. */
  614. static int __exit atmel_nand_remove(struct platform_device *pdev)
  615. {
  616. struct atmel_nand_host *host = platform_get_drvdata(pdev);
  617. struct mtd_info *mtd = &host->mtd;
  618. nand_release(mtd);
  619. atmel_nand_disable(host);
  620. if (host->ecc)
  621. iounmap(host->ecc);
  622. if (host->dma_chan)
  623. dma_release_channel(host->dma_chan);
  624. iounmap(host->io_base);
  625. kfree(host);
  626. return 0;
  627. }
  628. static struct platform_driver atmel_nand_driver = {
  629. .remove = __exit_p(atmel_nand_remove),
  630. .driver = {
  631. .name = "atmel_nand",
  632. .owner = THIS_MODULE,
  633. },
  634. };
  635. static int __init atmel_nand_init(void)
  636. {
  637. return platform_driver_probe(&atmel_nand_driver, atmel_nand_probe);
  638. }
  639. static void __exit atmel_nand_exit(void)
  640. {
  641. platform_driver_unregister(&atmel_nand_driver);
  642. }
  643. module_init(atmel_nand_init);
  644. module_exit(atmel_nand_exit);
  645. MODULE_LICENSE("GPL");
  646. MODULE_AUTHOR("Rick Bronson");
  647. MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
  648. MODULE_ALIAS("platform:atmel_nand");