atmel_nand.c 19 KB

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