atmel_nand.c 18 KB

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