atmel_nand.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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. bool has_pmecc;
  86. u8 pmecc_corr_cap;
  87. u16 pmecc_sector_size;
  88. u32 pmecc_lookup_table_offset;
  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 (gpio_is_valid(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 (gpio_is_valid(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. * oob_required: caller expects OOB data read to chip->oob_poi
  279. */
  280. static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  281. uint8_t *buf, int oob_required, 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. unsigned int max_bitflips = 0;
  291. /*
  292. * Errata: ALE is incorrectly wired up to the ECC controller
  293. * on the AP7000, so it will include the address cycles in the
  294. * ECC calculation.
  295. *
  296. * Workaround: Reset the parity registers before reading the
  297. * actual data.
  298. */
  299. if (cpu_is_at32ap7000()) {
  300. struct atmel_nand_host *host = chip->priv;
  301. ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
  302. }
  303. /* read the page */
  304. chip->read_buf(mtd, p, eccsize);
  305. /* move to ECC position if needed */
  306. if (eccpos[0] != 0) {
  307. /* This only works on large pages
  308. * because the ECC controller waits for
  309. * NAND_CMD_RNDOUTSTART after the
  310. * NAND_CMD_RNDOUT.
  311. * anyway, for small pages, the eccpos[0] == 0
  312. */
  313. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  314. mtd->writesize + eccpos[0], -1);
  315. }
  316. /* the ECC controller needs to read the ECC just after the data */
  317. ecc_pos = oob + eccpos[0];
  318. chip->read_buf(mtd, ecc_pos, eccbytes);
  319. /* check if there's an error */
  320. stat = chip->ecc.correct(mtd, p, oob, NULL);
  321. if (stat < 0) {
  322. mtd->ecc_stats.failed++;
  323. } else {
  324. mtd->ecc_stats.corrected += stat;
  325. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  326. }
  327. /* get back to oob start (end of page) */
  328. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  329. /* read the oob */
  330. chip->read_buf(mtd, oob, mtd->oobsize);
  331. return max_bitflips;
  332. }
  333. /*
  334. * HW ECC Correction
  335. *
  336. * function called after a read
  337. *
  338. * mtd: MTD block structure
  339. * dat: raw data read from the chip
  340. * read_ecc: ECC from the chip (unused)
  341. * isnull: unused
  342. *
  343. * Detect and correct a 1 bit error for a page
  344. */
  345. static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
  346. u_char *read_ecc, u_char *isnull)
  347. {
  348. struct nand_chip *nand_chip = mtd->priv;
  349. struct atmel_nand_host *host = nand_chip->priv;
  350. unsigned int ecc_status;
  351. unsigned int ecc_word, ecc_bit;
  352. /* get the status from the Status Register */
  353. ecc_status = ecc_readl(host->ecc, SR);
  354. /* if there's no error */
  355. if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
  356. return 0;
  357. /* get error bit offset (4 bits) */
  358. ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
  359. /* get word address (12 bits) */
  360. ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
  361. ecc_word >>= 4;
  362. /* if there are multiple errors */
  363. if (ecc_status & ATMEL_ECC_MULERR) {
  364. /* check if it is a freshly erased block
  365. * (filled with 0xff) */
  366. if ((ecc_bit == ATMEL_ECC_BITADDR)
  367. && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
  368. /* the block has just been erased, return OK */
  369. return 0;
  370. }
  371. /* it doesn't seems to be a freshly
  372. * erased block.
  373. * We can't correct so many errors */
  374. dev_dbg(host->dev, "atmel_nand : multiple errors detected."
  375. " Unable to correct.\n");
  376. return -EIO;
  377. }
  378. /* if there's a single bit error : we can correct it */
  379. if (ecc_status & ATMEL_ECC_ECCERR) {
  380. /* there's nothing much to do here.
  381. * the bit error is on the ECC itself.
  382. */
  383. dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
  384. " Nothing to correct\n");
  385. return 0;
  386. }
  387. dev_dbg(host->dev, "atmel_nand : one bit error on data."
  388. " (word offset in the page :"
  389. " 0x%x bit offset : 0x%x)\n",
  390. ecc_word, ecc_bit);
  391. /* correct the error */
  392. if (nand_chip->options & NAND_BUSWIDTH_16) {
  393. /* 16 bits words */
  394. ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
  395. } else {
  396. /* 8 bits words */
  397. dat[ecc_word] ^= (1 << ecc_bit);
  398. }
  399. dev_dbg(host->dev, "atmel_nand : error corrected\n");
  400. return 1;
  401. }
  402. /*
  403. * Enable HW ECC : unused on most chips
  404. */
  405. static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
  406. {
  407. if (cpu_is_at32ap7000()) {
  408. struct nand_chip *nand_chip = mtd->priv;
  409. struct atmel_nand_host *host = nand_chip->priv;
  410. ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
  411. }
  412. }
  413. #if defined(CONFIG_OF)
  414. static int __devinit atmel_of_init_port(struct atmel_nand_host *host,
  415. struct device_node *np)
  416. {
  417. u32 val, table_offset;
  418. u32 offset[2];
  419. int ecc_mode;
  420. struct atmel_nand_data *board = &host->board;
  421. enum of_gpio_flags flags;
  422. if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
  423. if (val >= 32) {
  424. dev_err(host->dev, "invalid addr-offset %u\n", val);
  425. return -EINVAL;
  426. }
  427. board->ale = val;
  428. }
  429. if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
  430. if (val >= 32) {
  431. dev_err(host->dev, "invalid cmd-offset %u\n", val);
  432. return -EINVAL;
  433. }
  434. board->cle = val;
  435. }
  436. ecc_mode = of_get_nand_ecc_mode(np);
  437. board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode;
  438. board->on_flash_bbt = of_get_nand_on_flash_bbt(np);
  439. if (of_get_nand_bus_width(np) == 16)
  440. board->bus_width_16 = 1;
  441. board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
  442. board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
  443. board->enable_pin = of_get_gpio(np, 1);
  444. board->det_pin = of_get_gpio(np, 2);
  445. host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
  446. if (!(board->ecc_mode == NAND_ECC_HW) || !host->has_pmecc)
  447. return 0; /* Not using PMECC */
  448. /* use PMECC, get correction capability, sector size and lookup
  449. * table offset.
  450. */
  451. if (of_property_read_u32(np, "atmel,pmecc-cap", &val) != 0) {
  452. dev_err(host->dev, "Cannot decide PMECC Capability\n");
  453. return -EINVAL;
  454. } else if ((val != 2) && (val != 4) && (val != 8) && (val != 12) &&
  455. (val != 24)) {
  456. dev_err(host->dev,
  457. "Unsupported PMECC correction capability: %d; should be 2, 4, 8, 12 or 24\n",
  458. val);
  459. return -EINVAL;
  460. }
  461. host->pmecc_corr_cap = (u8)val;
  462. if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) != 0) {
  463. dev_err(host->dev, "Cannot decide PMECC Sector Size\n");
  464. return -EINVAL;
  465. } else if ((val != 512) && (val != 1024)) {
  466. dev_err(host->dev,
  467. "Unsupported PMECC sector size: %d; should be 512 or 1024 bytes\n",
  468. val);
  469. return -EINVAL;
  470. }
  471. host->pmecc_sector_size = (u16)val;
  472. if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
  473. offset, 2) != 0) {
  474. dev_err(host->dev, "Cannot get PMECC lookup table offset\n");
  475. return -EINVAL;
  476. }
  477. table_offset = host->pmecc_sector_size == 512 ? offset[0] : offset[1];
  478. if (!table_offset) {
  479. dev_err(host->dev, "Invalid PMECC lookup table offset\n");
  480. return -EINVAL;
  481. }
  482. host->pmecc_lookup_table_offset = table_offset;
  483. return 0;
  484. }
  485. #else
  486. static int __devinit atmel_of_init_port(struct atmel_nand_host *host,
  487. struct device_node *np)
  488. {
  489. return -EINVAL;
  490. }
  491. #endif
  492. static int __init atmel_hw_nand_init_params(struct platform_device *pdev,
  493. struct atmel_nand_host *host)
  494. {
  495. struct mtd_info *mtd = &host->mtd;
  496. struct nand_chip *nand_chip = &host->nand_chip;
  497. struct resource *regs;
  498. regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  499. if (!regs) {
  500. dev_err(host->dev,
  501. "Can't get I/O resource regs, use software ECC\n");
  502. nand_chip->ecc.mode = NAND_ECC_SOFT;
  503. return 0;
  504. }
  505. host->ecc = ioremap(regs->start, resource_size(regs));
  506. if (host->ecc == NULL) {
  507. dev_err(host->dev, "ioremap failed\n");
  508. return -EIO;
  509. }
  510. /* ECC is calculated for the whole page (1 step) */
  511. nand_chip->ecc.size = mtd->writesize;
  512. /* set ECC page size and oob layout */
  513. switch (mtd->writesize) {
  514. case 512:
  515. nand_chip->ecc.layout = &atmel_oobinfo_small;
  516. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
  517. break;
  518. case 1024:
  519. nand_chip->ecc.layout = &atmel_oobinfo_large;
  520. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
  521. break;
  522. case 2048:
  523. nand_chip->ecc.layout = &atmel_oobinfo_large;
  524. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
  525. break;
  526. case 4096:
  527. nand_chip->ecc.layout = &atmel_oobinfo_large;
  528. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
  529. break;
  530. default:
  531. /* page size not handled by HW ECC */
  532. /* switching back to soft ECC */
  533. nand_chip->ecc.mode = NAND_ECC_SOFT;
  534. return 0;
  535. }
  536. /* set up for HW ECC */
  537. nand_chip->ecc.calculate = atmel_nand_calculate;
  538. nand_chip->ecc.correct = atmel_nand_correct;
  539. nand_chip->ecc.hwctl = atmel_nand_hwctl;
  540. nand_chip->ecc.read_page = atmel_nand_read_page;
  541. nand_chip->ecc.bytes = 4;
  542. nand_chip->ecc.strength = 1;
  543. return 0;
  544. }
  545. /*
  546. * Probe for the NAND device.
  547. */
  548. static int __init atmel_nand_probe(struct platform_device *pdev)
  549. {
  550. struct atmel_nand_host *host;
  551. struct mtd_info *mtd;
  552. struct nand_chip *nand_chip;
  553. struct resource *mem;
  554. struct mtd_part_parser_data ppdata = {};
  555. int res;
  556. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  557. if (!mem) {
  558. printk(KERN_ERR "atmel_nand: can't get I/O resource mem\n");
  559. return -ENXIO;
  560. }
  561. /* Allocate memory for the device structure (and zero it) */
  562. host = kzalloc(sizeof(struct atmel_nand_host), GFP_KERNEL);
  563. if (!host) {
  564. printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
  565. return -ENOMEM;
  566. }
  567. host->io_phys = (dma_addr_t)mem->start;
  568. host->io_base = ioremap(mem->start, resource_size(mem));
  569. if (host->io_base == NULL) {
  570. printk(KERN_ERR "atmel_nand: ioremap failed\n");
  571. res = -EIO;
  572. goto err_nand_ioremap;
  573. }
  574. mtd = &host->mtd;
  575. nand_chip = &host->nand_chip;
  576. host->dev = &pdev->dev;
  577. if (pdev->dev.of_node) {
  578. res = atmel_of_init_port(host, pdev->dev.of_node);
  579. if (res)
  580. goto err_nand_ioremap;
  581. } else {
  582. memcpy(&host->board, pdev->dev.platform_data,
  583. sizeof(struct atmel_nand_data));
  584. }
  585. nand_chip->priv = host; /* link the private data structures */
  586. mtd->priv = nand_chip;
  587. mtd->owner = THIS_MODULE;
  588. /* Set address of NAND IO lines */
  589. nand_chip->IO_ADDR_R = host->io_base;
  590. nand_chip->IO_ADDR_W = host->io_base;
  591. nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
  592. if (gpio_is_valid(host->board.rdy_pin))
  593. nand_chip->dev_ready = atmel_nand_device_ready;
  594. nand_chip->ecc.mode = host->board.ecc_mode;
  595. nand_chip->chip_delay = 20; /* 20us command delay time */
  596. if (host->board.bus_width_16) /* 16-bit bus width */
  597. nand_chip->options |= NAND_BUSWIDTH_16;
  598. nand_chip->read_buf = atmel_read_buf;
  599. nand_chip->write_buf = atmel_write_buf;
  600. platform_set_drvdata(pdev, host);
  601. atmel_nand_enable(host);
  602. if (gpio_is_valid(host->board.det_pin)) {
  603. if (gpio_get_value(host->board.det_pin)) {
  604. printk(KERN_INFO "No SmartMedia card inserted.\n");
  605. res = -ENXIO;
  606. goto err_no_card;
  607. }
  608. }
  609. if (host->board.on_flash_bbt || on_flash_bbt) {
  610. printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
  611. nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
  612. }
  613. if (!cpu_has_dma())
  614. use_dma = 0;
  615. if (use_dma) {
  616. dma_cap_mask_t mask;
  617. dma_cap_zero(mask);
  618. dma_cap_set(DMA_MEMCPY, mask);
  619. host->dma_chan = dma_request_channel(mask, NULL, NULL);
  620. if (!host->dma_chan) {
  621. dev_err(host->dev, "Failed to request DMA channel\n");
  622. use_dma = 0;
  623. }
  624. }
  625. if (use_dma)
  626. dev_info(host->dev, "Using %s for DMA transfers.\n",
  627. dma_chan_name(host->dma_chan));
  628. else
  629. dev_info(host->dev, "No DMA support for NAND access.\n");
  630. /* first scan to find the device and get the page size */
  631. if (nand_scan_ident(mtd, 1, NULL)) {
  632. res = -ENXIO;
  633. goto err_scan_ident;
  634. }
  635. if (nand_chip->ecc.mode == NAND_ECC_HW) {
  636. res = atmel_hw_nand_init_params(pdev, host);
  637. if (res != 0)
  638. goto err_hw_ecc;
  639. }
  640. /* second phase scan */
  641. if (nand_scan_tail(mtd)) {
  642. res = -ENXIO;
  643. goto err_scan_tail;
  644. }
  645. mtd->name = "atmel_nand";
  646. ppdata.of_node = pdev->dev.of_node;
  647. res = mtd_device_parse_register(mtd, NULL, &ppdata,
  648. host->board.parts, host->board.num_parts);
  649. if (!res)
  650. return res;
  651. err_scan_tail:
  652. if (host->ecc)
  653. iounmap(host->ecc);
  654. err_hw_ecc:
  655. err_scan_ident:
  656. err_no_card:
  657. atmel_nand_disable(host);
  658. platform_set_drvdata(pdev, NULL);
  659. if (host->dma_chan)
  660. dma_release_channel(host->dma_chan);
  661. iounmap(host->io_base);
  662. err_nand_ioremap:
  663. kfree(host);
  664. return res;
  665. }
  666. /*
  667. * Remove a NAND device.
  668. */
  669. static int __exit atmel_nand_remove(struct platform_device *pdev)
  670. {
  671. struct atmel_nand_host *host = platform_get_drvdata(pdev);
  672. struct mtd_info *mtd = &host->mtd;
  673. nand_release(mtd);
  674. atmel_nand_disable(host);
  675. if (host->ecc)
  676. iounmap(host->ecc);
  677. if (host->dma_chan)
  678. dma_release_channel(host->dma_chan);
  679. iounmap(host->io_base);
  680. kfree(host);
  681. return 0;
  682. }
  683. #if defined(CONFIG_OF)
  684. static const struct of_device_id atmel_nand_dt_ids[] = {
  685. { .compatible = "atmel,at91rm9200-nand" },
  686. { /* sentinel */ }
  687. };
  688. MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
  689. #endif
  690. static struct platform_driver atmel_nand_driver = {
  691. .remove = __exit_p(atmel_nand_remove),
  692. .driver = {
  693. .name = "atmel_nand",
  694. .owner = THIS_MODULE,
  695. .of_match_table = of_match_ptr(atmel_nand_dt_ids),
  696. },
  697. };
  698. static int __init atmel_nand_init(void)
  699. {
  700. return platform_driver_probe(&atmel_nand_driver, atmel_nand_probe);
  701. }
  702. static void __exit atmel_nand_exit(void)
  703. {
  704. platform_driver_unregister(&atmel_nand_driver);
  705. }
  706. module_init(atmel_nand_init);
  707. module_exit(atmel_nand_exit);
  708. MODULE_LICENSE("GPL");
  709. MODULE_AUTHOR("Rick Bronson");
  710. MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
  711. MODULE_ALIAS("platform:atmel_nand");