atmel_nand.c 17 KB

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