atmel_nand.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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. if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
  239. return;
  240. if (host->board->bus_width_16)
  241. atmel_read_buf16(mtd, buf, len);
  242. else
  243. atmel_read_buf8(mtd, buf, len);
  244. }
  245. static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  246. {
  247. struct nand_chip *chip = mtd->priv;
  248. struct atmel_nand_host *host = chip->priv;
  249. if (use_dma && len >= mtd->oobsize)
  250. if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
  251. return;
  252. if (host->board->bus_width_16)
  253. atmel_write_buf16(mtd, buf, len);
  254. else
  255. atmel_write_buf8(mtd, buf, len);
  256. }
  257. /*
  258. * Calculate HW ECC
  259. *
  260. * function called after a write
  261. *
  262. * mtd: MTD block structure
  263. * dat: raw data (unused)
  264. * ecc_code: buffer for ECC
  265. */
  266. static int atmel_nand_calculate(struct mtd_info *mtd,
  267. const u_char *dat, unsigned char *ecc_code)
  268. {
  269. struct nand_chip *nand_chip = mtd->priv;
  270. struct atmel_nand_host *host = nand_chip->priv;
  271. unsigned int ecc_value;
  272. /* get the first 2 ECC bytes */
  273. ecc_value = ecc_readl(host->ecc, PR);
  274. ecc_code[0] = ecc_value & 0xFF;
  275. ecc_code[1] = (ecc_value >> 8) & 0xFF;
  276. /* get the last 2 ECC bytes */
  277. ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
  278. ecc_code[2] = ecc_value & 0xFF;
  279. ecc_code[3] = (ecc_value >> 8) & 0xFF;
  280. return 0;
  281. }
  282. /*
  283. * HW ECC read page function
  284. *
  285. * mtd: mtd info structure
  286. * chip: nand chip info structure
  287. * buf: buffer to store read data
  288. */
  289. static int atmel_nand_read_page(struct mtd_info *mtd,
  290. struct nand_chip *chip, uint8_t *buf, int page)
  291. {
  292. int eccsize = chip->ecc.size;
  293. int eccbytes = chip->ecc.bytes;
  294. uint32_t *eccpos = chip->ecc.layout->eccpos;
  295. uint8_t *p = buf;
  296. uint8_t *oob = chip->oob_poi;
  297. uint8_t *ecc_pos;
  298. int stat;
  299. /*
  300. * Errata: ALE is incorrectly wired up to the ECC controller
  301. * on the AP7000, so it will include the address cycles in the
  302. * ECC calculation.
  303. *
  304. * Workaround: Reset the parity registers before reading the
  305. * actual data.
  306. */
  307. if (cpu_is_at32ap7000()) {
  308. struct atmel_nand_host *host = chip->priv;
  309. ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
  310. }
  311. /* read the page */
  312. chip->read_buf(mtd, p, eccsize);
  313. /* move to ECC position if needed */
  314. if (eccpos[0] != 0) {
  315. /* This only works on large pages
  316. * because the ECC controller waits for
  317. * NAND_CMD_RNDOUTSTART after the
  318. * NAND_CMD_RNDOUT.
  319. * anyway, for small pages, the eccpos[0] == 0
  320. */
  321. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  322. mtd->writesize + eccpos[0], -1);
  323. }
  324. /* the ECC controller needs to read the ECC just after the data */
  325. ecc_pos = oob + eccpos[0];
  326. chip->read_buf(mtd, ecc_pos, eccbytes);
  327. /* check if there's an error */
  328. stat = chip->ecc.correct(mtd, p, oob, NULL);
  329. if (stat < 0)
  330. mtd->ecc_stats.failed++;
  331. else
  332. mtd->ecc_stats.corrected += stat;
  333. /* get back to oob start (end of page) */
  334. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  335. /* read the oob */
  336. chip->read_buf(mtd, oob, mtd->oobsize);
  337. return 0;
  338. }
  339. /*
  340. * HW ECC Correction
  341. *
  342. * function called after a read
  343. *
  344. * mtd: MTD block structure
  345. * dat: raw data read from the chip
  346. * read_ecc: ECC from the chip (unused)
  347. * isnull: unused
  348. *
  349. * Detect and correct a 1 bit error for a page
  350. */
  351. static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
  352. u_char *read_ecc, u_char *isnull)
  353. {
  354. struct nand_chip *nand_chip = mtd->priv;
  355. struct atmel_nand_host *host = nand_chip->priv;
  356. unsigned int ecc_status;
  357. unsigned int ecc_word, ecc_bit;
  358. /* get the status from the Status Register */
  359. ecc_status = ecc_readl(host->ecc, SR);
  360. /* if there's no error */
  361. if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
  362. return 0;
  363. /* get error bit offset (4 bits) */
  364. ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
  365. /* get word address (12 bits) */
  366. ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
  367. ecc_word >>= 4;
  368. /* if there are multiple errors */
  369. if (ecc_status & ATMEL_ECC_MULERR) {
  370. /* check if it is a freshly erased block
  371. * (filled with 0xff) */
  372. if ((ecc_bit == ATMEL_ECC_BITADDR)
  373. && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
  374. /* the block has just been erased, return OK */
  375. return 0;
  376. }
  377. /* it doesn't seems to be a freshly
  378. * erased block.
  379. * We can't correct so many errors */
  380. dev_dbg(host->dev, "atmel_nand : multiple errors detected."
  381. " Unable to correct.\n");
  382. return -EIO;
  383. }
  384. /* if there's a single bit error : we can correct it */
  385. if (ecc_status & ATMEL_ECC_ECCERR) {
  386. /* there's nothing much to do here.
  387. * the bit error is on the ECC itself.
  388. */
  389. dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
  390. " Nothing to correct\n");
  391. return 0;
  392. }
  393. dev_dbg(host->dev, "atmel_nand : one bit error on data."
  394. " (word offset in the page :"
  395. " 0x%x bit offset : 0x%x)\n",
  396. ecc_word, ecc_bit);
  397. /* correct the error */
  398. if (nand_chip->options & NAND_BUSWIDTH_16) {
  399. /* 16 bits words */
  400. ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
  401. } else {
  402. /* 8 bits words */
  403. dat[ecc_word] ^= (1 << ecc_bit);
  404. }
  405. dev_dbg(host->dev, "atmel_nand : error corrected\n");
  406. return 1;
  407. }
  408. /*
  409. * Enable HW ECC : unused on most chips
  410. */
  411. static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
  412. {
  413. if (cpu_is_at32ap7000()) {
  414. struct nand_chip *nand_chip = mtd->priv;
  415. struct atmel_nand_host *host = nand_chip->priv;
  416. ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
  417. }
  418. }
  419. #ifdef CONFIG_MTD_CMDLINE_PARTS
  420. static const char *part_probes[] = { "cmdlinepart", NULL };
  421. #endif
  422. /*
  423. * Probe for the NAND device.
  424. */
  425. static int __init atmel_nand_probe(struct platform_device *pdev)
  426. {
  427. struct atmel_nand_host *host;
  428. struct mtd_info *mtd;
  429. struct nand_chip *nand_chip;
  430. struct resource *regs;
  431. struct resource *mem;
  432. int res;
  433. #ifdef CONFIG_MTD_PARTITIONS
  434. struct mtd_partition *partitions = NULL;
  435. int num_partitions = 0;
  436. #endif
  437. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  438. if (!mem) {
  439. printk(KERN_ERR "atmel_nand: can't get I/O resource mem\n");
  440. return -ENXIO;
  441. }
  442. /* Allocate memory for the device structure (and zero it) */
  443. host = kzalloc(sizeof(struct atmel_nand_host), GFP_KERNEL);
  444. if (!host) {
  445. printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
  446. return -ENOMEM;
  447. }
  448. host->io_phys = (dma_addr_t)mem->start;
  449. host->io_base = ioremap(mem->start, mem->end - mem->start + 1);
  450. if (host->io_base == NULL) {
  451. printk(KERN_ERR "atmel_nand: ioremap failed\n");
  452. res = -EIO;
  453. goto err_nand_ioremap;
  454. }
  455. mtd = &host->mtd;
  456. nand_chip = &host->nand_chip;
  457. host->board = pdev->dev.platform_data;
  458. host->dev = &pdev->dev;
  459. nand_chip->priv = host; /* link the private data structures */
  460. mtd->priv = nand_chip;
  461. mtd->owner = THIS_MODULE;
  462. /* Set address of NAND IO lines */
  463. nand_chip->IO_ADDR_R = host->io_base;
  464. nand_chip->IO_ADDR_W = host->io_base;
  465. nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
  466. if (host->board->rdy_pin)
  467. nand_chip->dev_ready = atmel_nand_device_ready;
  468. regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  469. if (!regs && hard_ecc) {
  470. printk(KERN_ERR "atmel_nand: can't get I/O resource "
  471. "regs\nFalling back on software ECC\n");
  472. }
  473. nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
  474. if (no_ecc)
  475. nand_chip->ecc.mode = NAND_ECC_NONE;
  476. if (hard_ecc && regs) {
  477. host->ecc = ioremap(regs->start, regs->end - regs->start + 1);
  478. if (host->ecc == NULL) {
  479. printk(KERN_ERR "atmel_nand: ioremap failed\n");
  480. res = -EIO;
  481. goto err_ecc_ioremap;
  482. }
  483. nand_chip->ecc.mode = NAND_ECC_HW;
  484. nand_chip->ecc.calculate = atmel_nand_calculate;
  485. nand_chip->ecc.correct = atmel_nand_correct;
  486. nand_chip->ecc.hwctl = atmel_nand_hwctl;
  487. nand_chip->ecc.read_page = atmel_nand_read_page;
  488. nand_chip->ecc.bytes = 4;
  489. }
  490. nand_chip->chip_delay = 20; /* 20us command delay time */
  491. if (host->board->bus_width_16) /* 16-bit bus width */
  492. nand_chip->options |= NAND_BUSWIDTH_16;
  493. nand_chip->read_buf = atmel_read_buf;
  494. nand_chip->write_buf = atmel_write_buf;
  495. platform_set_drvdata(pdev, host);
  496. atmel_nand_enable(host);
  497. if (host->board->det_pin) {
  498. if (gpio_get_value(host->board->det_pin)) {
  499. printk(KERN_INFO "No SmartMedia card inserted.\n");
  500. res = -ENXIO;
  501. goto err_no_card;
  502. }
  503. }
  504. if (on_flash_bbt) {
  505. printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
  506. nand_chip->options |= NAND_USE_FLASH_BBT;
  507. }
  508. if (cpu_has_dma() && use_dma) {
  509. dma_cap_mask_t mask;
  510. dma_cap_zero(mask);
  511. dma_cap_set(DMA_MEMCPY, mask);
  512. host->dma_chan = dma_request_channel(mask, 0, NULL);
  513. if (!host->dma_chan) {
  514. dev_err(host->dev, "Failed to request DMA channel\n");
  515. use_dma = 0;
  516. }
  517. }
  518. if (use_dma)
  519. dev_info(host->dev, "Using DMA for NAND access.\n");
  520. else
  521. dev_info(host->dev, "No DMA support for NAND access.\n");
  522. /* first scan to find the device and get the page size */
  523. if (nand_scan_ident(mtd, 1, NULL)) {
  524. res = -ENXIO;
  525. goto err_scan_ident;
  526. }
  527. if (nand_chip->ecc.mode == NAND_ECC_HW) {
  528. /* ECC is calculated for the whole page (1 step) */
  529. nand_chip->ecc.size = mtd->writesize;
  530. /* set ECC page size and oob layout */
  531. switch (mtd->writesize) {
  532. case 512:
  533. nand_chip->ecc.layout = &atmel_oobinfo_small;
  534. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
  535. break;
  536. case 1024:
  537. nand_chip->ecc.layout = &atmel_oobinfo_large;
  538. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
  539. break;
  540. case 2048:
  541. nand_chip->ecc.layout = &atmel_oobinfo_large;
  542. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
  543. break;
  544. case 4096:
  545. nand_chip->ecc.layout = &atmel_oobinfo_large;
  546. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
  547. break;
  548. default:
  549. /* page size not handled by HW ECC */
  550. /* switching back to soft ECC */
  551. nand_chip->ecc.mode = NAND_ECC_SOFT;
  552. nand_chip->ecc.calculate = NULL;
  553. nand_chip->ecc.correct = NULL;
  554. nand_chip->ecc.hwctl = NULL;
  555. nand_chip->ecc.read_page = NULL;
  556. nand_chip->ecc.postpad = 0;
  557. nand_chip->ecc.prepad = 0;
  558. nand_chip->ecc.bytes = 0;
  559. break;
  560. }
  561. }
  562. /* second phase scan */
  563. if (nand_scan_tail(mtd)) {
  564. res = -ENXIO;
  565. goto err_scan_tail;
  566. }
  567. #ifdef CONFIG_MTD_PARTITIONS
  568. #ifdef CONFIG_MTD_CMDLINE_PARTS
  569. mtd->name = "atmel_nand";
  570. num_partitions = parse_mtd_partitions(mtd, part_probes,
  571. &partitions, 0);
  572. #endif
  573. if (num_partitions <= 0 && host->board->partition_info)
  574. partitions = host->board->partition_info(mtd->size,
  575. &num_partitions);
  576. if ((!partitions) || (num_partitions == 0)) {
  577. printk(KERN_ERR "atmel_nand: No partitions defined, or unsupported device.\n");
  578. res = -ENXIO;
  579. goto err_no_partitions;
  580. }
  581. res = add_mtd_partitions(mtd, partitions, num_partitions);
  582. #else
  583. res = add_mtd_device(mtd);
  584. #endif
  585. if (!res)
  586. return res;
  587. #ifdef CONFIG_MTD_PARTITIONS
  588. err_no_partitions:
  589. #endif
  590. nand_release(mtd);
  591. err_scan_tail:
  592. err_scan_ident:
  593. err_no_card:
  594. atmel_nand_disable(host);
  595. platform_set_drvdata(pdev, NULL);
  596. if (host->dma_chan)
  597. dma_release_channel(host->dma_chan);
  598. if (host->ecc)
  599. iounmap(host->ecc);
  600. err_ecc_ioremap:
  601. iounmap(host->io_base);
  602. err_nand_ioremap:
  603. kfree(host);
  604. return res;
  605. }
  606. /*
  607. * Remove a NAND device.
  608. */
  609. static int __exit atmel_nand_remove(struct platform_device *pdev)
  610. {
  611. struct atmel_nand_host *host = platform_get_drvdata(pdev);
  612. struct mtd_info *mtd = &host->mtd;
  613. nand_release(mtd);
  614. atmel_nand_disable(host);
  615. if (host->ecc)
  616. iounmap(host->ecc);
  617. if (host->dma_chan)
  618. dma_release_channel(host->dma_chan);
  619. iounmap(host->io_base);
  620. kfree(host);
  621. return 0;
  622. }
  623. static struct platform_driver atmel_nand_driver = {
  624. .remove = __exit_p(atmel_nand_remove),
  625. .driver = {
  626. .name = "atmel_nand",
  627. .owner = THIS_MODULE,
  628. },
  629. };
  630. static int __init atmel_nand_init(void)
  631. {
  632. return platform_driver_probe(&atmel_nand_driver, atmel_nand_probe);
  633. }
  634. static void __exit atmel_nand_exit(void)
  635. {
  636. platform_driver_unregister(&atmel_nand_driver);
  637. }
  638. module_init(atmel_nand_init);
  639. module_exit(atmel_nand_exit);
  640. MODULE_LICENSE("GPL");
  641. MODULE_AUTHOR("Rick Bronson");
  642. MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
  643. MODULE_ALIAS("platform:atmel_nand");