atmel_nand.c 18 KB

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