lpc32xx_mlc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. /*
  2. * Driver for NAND MLC Controller in LPC32xx
  3. *
  4. * Author: Roland Stigge <stigge@antcom.de>
  5. *
  6. * Copyright © 2011 WORK Microwave GmbH
  7. * Copyright © 2011, 2012 Roland Stigge
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. *
  20. * NAND Flash Controller Operation:
  21. * - Read: Auto Decode
  22. * - Write: Auto Encode
  23. * - Tested Page Sizes: 2048, 4096
  24. */
  25. #include <linux/slab.h>
  26. #include <linux/module.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/clk.h>
  32. #include <linux/err.h>
  33. #include <linux/delay.h>
  34. #include <linux/completion.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/of.h>
  37. #include <linux/of_mtd.h>
  38. #include <linux/of_gpio.h>
  39. #include <linux/amba/pl08x.h>
  40. #include <linux/io.h>
  41. #include <linux/mm.h>
  42. #include <linux/dma-mapping.h>
  43. #include <linux/dmaengine.h>
  44. #include <linux/mtd/nand_ecc.h>
  45. #define DRV_NAME "lpc32xx_mlc"
  46. /**********************************************************************
  47. * MLC NAND controller register offsets
  48. **********************************************************************/
  49. #define MLC_BUFF(x) (x + 0x00000)
  50. #define MLC_DATA(x) (x + 0x08000)
  51. #define MLC_CMD(x) (x + 0x10000)
  52. #define MLC_ADDR(x) (x + 0x10004)
  53. #define MLC_ECC_ENC_REG(x) (x + 0x10008)
  54. #define MLC_ECC_DEC_REG(x) (x + 0x1000C)
  55. #define MLC_ECC_AUTO_ENC_REG(x) (x + 0x10010)
  56. #define MLC_ECC_AUTO_DEC_REG(x) (x + 0x10014)
  57. #define MLC_RPR(x) (x + 0x10018)
  58. #define MLC_WPR(x) (x + 0x1001C)
  59. #define MLC_RUBP(x) (x + 0x10020)
  60. #define MLC_ROBP(x) (x + 0x10024)
  61. #define MLC_SW_WP_ADD_LOW(x) (x + 0x10028)
  62. #define MLC_SW_WP_ADD_HIG(x) (x + 0x1002C)
  63. #define MLC_ICR(x) (x + 0x10030)
  64. #define MLC_TIME_REG(x) (x + 0x10034)
  65. #define MLC_IRQ_MR(x) (x + 0x10038)
  66. #define MLC_IRQ_SR(x) (x + 0x1003C)
  67. #define MLC_LOCK_PR(x) (x + 0x10044)
  68. #define MLC_ISR(x) (x + 0x10048)
  69. #define MLC_CEH(x) (x + 0x1004C)
  70. /**********************************************************************
  71. * MLC_CMD bit definitions
  72. **********************************************************************/
  73. #define MLCCMD_RESET 0xFF
  74. /**********************************************************************
  75. * MLC_ICR bit definitions
  76. **********************************************************************/
  77. #define MLCICR_WPROT (1 << 3)
  78. #define MLCICR_LARGEBLOCK (1 << 2)
  79. #define MLCICR_LONGADDR (1 << 1)
  80. #define MLCICR_16BIT (1 << 0) /* unsupported by LPC32x0! */
  81. /**********************************************************************
  82. * MLC_TIME_REG bit definitions
  83. **********************************************************************/
  84. #define MLCTIMEREG_TCEA_DELAY(n) (((n) & 0x03) << 24)
  85. #define MLCTIMEREG_BUSY_DELAY(n) (((n) & 0x1F) << 19)
  86. #define MLCTIMEREG_NAND_TA(n) (((n) & 0x07) << 16)
  87. #define MLCTIMEREG_RD_HIGH(n) (((n) & 0x0F) << 12)
  88. #define MLCTIMEREG_RD_LOW(n) (((n) & 0x0F) << 8)
  89. #define MLCTIMEREG_WR_HIGH(n) (((n) & 0x0F) << 4)
  90. #define MLCTIMEREG_WR_LOW(n) (((n) & 0x0F) << 0)
  91. /**********************************************************************
  92. * MLC_IRQ_MR and MLC_IRQ_SR bit definitions
  93. **********************************************************************/
  94. #define MLCIRQ_NAND_READY (1 << 5)
  95. #define MLCIRQ_CONTROLLER_READY (1 << 4)
  96. #define MLCIRQ_DECODE_FAILURE (1 << 3)
  97. #define MLCIRQ_DECODE_ERROR (1 << 2)
  98. #define MLCIRQ_ECC_READY (1 << 1)
  99. #define MLCIRQ_WRPROT_FAULT (1 << 0)
  100. /**********************************************************************
  101. * MLC_LOCK_PR bit definitions
  102. **********************************************************************/
  103. #define MLCLOCKPR_MAGIC 0xA25E
  104. /**********************************************************************
  105. * MLC_ISR bit definitions
  106. **********************************************************************/
  107. #define MLCISR_DECODER_FAILURE (1 << 6)
  108. #define MLCISR_ERRORS ((1 << 4) | (1 << 5))
  109. #define MLCISR_ERRORS_DETECTED (1 << 3)
  110. #define MLCISR_ECC_READY (1 << 2)
  111. #define MLCISR_CONTROLLER_READY (1 << 1)
  112. #define MLCISR_NAND_READY (1 << 0)
  113. /**********************************************************************
  114. * MLC_CEH bit definitions
  115. **********************************************************************/
  116. #define MLCCEH_NORMAL (1 << 0)
  117. struct lpc32xx_nand_cfg_mlc {
  118. uint32_t tcea_delay;
  119. uint32_t busy_delay;
  120. uint32_t nand_ta;
  121. uint32_t rd_high;
  122. uint32_t rd_low;
  123. uint32_t wr_high;
  124. uint32_t wr_low;
  125. int wp_gpio;
  126. struct mtd_partition *parts;
  127. unsigned num_parts;
  128. };
  129. static struct nand_ecclayout lpc32xx_nand_oob = {
  130. .eccbytes = 40,
  131. .eccpos = { 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  132. 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  133. 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  134. 54, 55, 56, 57, 58, 59, 60, 61, 62, 63 },
  135. .oobfree = {
  136. { .offset = 0,
  137. .length = 6, },
  138. { .offset = 16,
  139. .length = 6, },
  140. { .offset = 32,
  141. .length = 6, },
  142. { .offset = 48,
  143. .length = 6, },
  144. },
  145. };
  146. static struct nand_bbt_descr lpc32xx_nand_bbt = {
  147. .options = NAND_BBT_ABSPAGE | NAND_BBT_2BIT | NAND_BBT_NO_OOB |
  148. NAND_BBT_WRITE,
  149. .pages = { 524224, 0, 0, 0, 0, 0, 0, 0 },
  150. };
  151. static struct nand_bbt_descr lpc32xx_nand_bbt_mirror = {
  152. .options = NAND_BBT_ABSPAGE | NAND_BBT_2BIT | NAND_BBT_NO_OOB |
  153. NAND_BBT_WRITE,
  154. .pages = { 524160, 0, 0, 0, 0, 0, 0, 0 },
  155. };
  156. struct lpc32xx_nand_host {
  157. struct nand_chip nand_chip;
  158. struct clk *clk;
  159. struct mtd_info mtd;
  160. void __iomem *io_base;
  161. int irq;
  162. struct lpc32xx_nand_cfg_mlc *ncfg;
  163. struct completion comp_nand;
  164. struct completion comp_controller;
  165. uint32_t llptr;
  166. /*
  167. * Physical addresses of ECC buffer, DMA data buffers, OOB data buffer
  168. */
  169. dma_addr_t oob_buf_phy;
  170. /*
  171. * Virtual addresses of ECC buffer, DMA data buffers, OOB data buffer
  172. */
  173. uint8_t *oob_buf;
  174. /* Physical address of DMA base address */
  175. dma_addr_t io_base_phy;
  176. struct completion comp_dma;
  177. struct dma_chan *dma_chan;
  178. struct dma_slave_config dma_slave_config;
  179. struct scatterlist sgl;
  180. uint8_t *dma_buf;
  181. uint8_t *dummy_buf;
  182. int mlcsubpages; /* number of 512bytes-subpages */
  183. };
  184. /*
  185. * Activate/Deactivate DMA Operation:
  186. *
  187. * Using the PL080 DMA Controller for transferring the 512 byte subpages
  188. * instead of doing readl() / writel() in a loop slows it down significantly.
  189. * Measurements via getnstimeofday() upon 512 byte subpage reads reveal:
  190. *
  191. * - readl() of 128 x 32 bits in a loop: ~20us
  192. * - DMA read of 512 bytes (32 bit, 4...128 words bursts): ~60us
  193. * - DMA read of 512 bytes (32 bit, no bursts): ~100us
  194. *
  195. * This applies to the transfer itself. In the DMA case: only the
  196. * wait_for_completion() (DMA setup _not_ included).
  197. *
  198. * Note that the 512 bytes subpage transfer is done directly from/to a
  199. * FIFO/buffer inside the NAND controller. Most of the time (~400-800us for a
  200. * 2048 bytes page) is spent waiting for the NAND IRQ, anyway. (The NAND
  201. * controller transferring data between its internal buffer to/from the NAND
  202. * chip.)
  203. *
  204. * Therefore, using the PL080 DMA is disabled by default, for now.
  205. *
  206. */
  207. static int use_dma;
  208. static void lpc32xx_nand_setup(struct lpc32xx_nand_host *host)
  209. {
  210. uint32_t clkrate, tmp;
  211. /* Reset MLC controller */
  212. writel(MLCCMD_RESET, MLC_CMD(host->io_base));
  213. udelay(1000);
  214. /* Get base clock for MLC block */
  215. clkrate = clk_get_rate(host->clk);
  216. if (clkrate == 0)
  217. clkrate = 104000000;
  218. /* Unlock MLC_ICR
  219. * (among others, will be locked again automatically) */
  220. writew(MLCLOCKPR_MAGIC, MLC_LOCK_PR(host->io_base));
  221. /* Configure MLC Controller: Large Block, 5 Byte Address */
  222. tmp = MLCICR_LARGEBLOCK | MLCICR_LONGADDR;
  223. writel(tmp, MLC_ICR(host->io_base));
  224. /* Unlock MLC_TIME_REG
  225. * (among others, will be locked again automatically) */
  226. writew(MLCLOCKPR_MAGIC, MLC_LOCK_PR(host->io_base));
  227. /* Compute clock setup values, see LPC and NAND manual */
  228. tmp = 0;
  229. tmp |= MLCTIMEREG_TCEA_DELAY(clkrate / host->ncfg->tcea_delay + 1);
  230. tmp |= MLCTIMEREG_BUSY_DELAY(clkrate / host->ncfg->busy_delay + 1);
  231. tmp |= MLCTIMEREG_NAND_TA(clkrate / host->ncfg->nand_ta + 1);
  232. tmp |= MLCTIMEREG_RD_HIGH(clkrate / host->ncfg->rd_high + 1);
  233. tmp |= MLCTIMEREG_RD_LOW(clkrate / host->ncfg->rd_low);
  234. tmp |= MLCTIMEREG_WR_HIGH(clkrate / host->ncfg->wr_high + 1);
  235. tmp |= MLCTIMEREG_WR_LOW(clkrate / host->ncfg->wr_low);
  236. writel(tmp, MLC_TIME_REG(host->io_base));
  237. /* Enable IRQ for CONTROLLER_READY and NAND_READY */
  238. writeb(MLCIRQ_CONTROLLER_READY | MLCIRQ_NAND_READY,
  239. MLC_IRQ_MR(host->io_base));
  240. /* Normal nCE operation: nCE controlled by controller */
  241. writel(MLCCEH_NORMAL, MLC_CEH(host->io_base));
  242. }
  243. /*
  244. * Hardware specific access to control lines
  245. */
  246. static void lpc32xx_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
  247. unsigned int ctrl)
  248. {
  249. struct nand_chip *nand_chip = mtd->priv;
  250. struct lpc32xx_nand_host *host = nand_chip->priv;
  251. if (cmd != NAND_CMD_NONE) {
  252. if (ctrl & NAND_CLE)
  253. writel(cmd, MLC_CMD(host->io_base));
  254. else
  255. writel(cmd, MLC_ADDR(host->io_base));
  256. }
  257. }
  258. /*
  259. * Read Device Ready (NAND device _and_ controller ready)
  260. */
  261. static int lpc32xx_nand_device_ready(struct mtd_info *mtd)
  262. {
  263. struct nand_chip *nand_chip = mtd->priv;
  264. struct lpc32xx_nand_host *host = nand_chip->priv;
  265. if ((readb(MLC_ISR(host->io_base)) &
  266. (MLCISR_CONTROLLER_READY | MLCISR_NAND_READY)) ==
  267. (MLCISR_CONTROLLER_READY | MLCISR_NAND_READY))
  268. return 1;
  269. return 0;
  270. }
  271. static irqreturn_t lpc3xxx_nand_irq(int irq, struct lpc32xx_nand_host *host)
  272. {
  273. uint8_t sr;
  274. /* Clear interrupt flag by reading status */
  275. sr = readb(MLC_IRQ_SR(host->io_base));
  276. if (sr & MLCIRQ_NAND_READY)
  277. complete(&host->comp_nand);
  278. if (sr & MLCIRQ_CONTROLLER_READY)
  279. complete(&host->comp_controller);
  280. return IRQ_HANDLED;
  281. }
  282. static int lpc32xx_waitfunc_nand(struct mtd_info *mtd, struct nand_chip *chip)
  283. {
  284. struct lpc32xx_nand_host *host = chip->priv;
  285. if (readb(MLC_ISR(host->io_base)) & MLCISR_NAND_READY)
  286. goto exit;
  287. wait_for_completion(&host->comp_nand);
  288. while (!(readb(MLC_ISR(host->io_base)) & MLCISR_NAND_READY)) {
  289. /* Seems to be delayed sometimes by controller */
  290. dev_dbg(&mtd->dev, "Warning: NAND not ready.\n");
  291. cpu_relax();
  292. }
  293. exit:
  294. return NAND_STATUS_READY;
  295. }
  296. static int lpc32xx_waitfunc_controller(struct mtd_info *mtd,
  297. struct nand_chip *chip)
  298. {
  299. struct lpc32xx_nand_host *host = chip->priv;
  300. if (readb(MLC_ISR(host->io_base)) & MLCISR_CONTROLLER_READY)
  301. goto exit;
  302. wait_for_completion(&host->comp_controller);
  303. while (!(readb(MLC_ISR(host->io_base)) &
  304. MLCISR_CONTROLLER_READY)) {
  305. dev_dbg(&mtd->dev, "Warning: Controller not ready.\n");
  306. cpu_relax();
  307. }
  308. exit:
  309. return NAND_STATUS_READY;
  310. }
  311. static int lpc32xx_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
  312. {
  313. lpc32xx_waitfunc_nand(mtd, chip);
  314. lpc32xx_waitfunc_controller(mtd, chip);
  315. return NAND_STATUS_READY;
  316. }
  317. /*
  318. * Enable NAND write protect
  319. */
  320. static void lpc32xx_wp_enable(struct lpc32xx_nand_host *host)
  321. {
  322. if (gpio_is_valid(host->ncfg->wp_gpio))
  323. gpio_set_value(host->ncfg->wp_gpio, 0);
  324. }
  325. /*
  326. * Disable NAND write protect
  327. */
  328. static void lpc32xx_wp_disable(struct lpc32xx_nand_host *host)
  329. {
  330. if (gpio_is_valid(host->ncfg->wp_gpio))
  331. gpio_set_value(host->ncfg->wp_gpio, 1);
  332. }
  333. static void lpc32xx_dma_complete_func(void *completion)
  334. {
  335. complete(completion);
  336. }
  337. static int lpc32xx_xmit_dma(struct mtd_info *mtd, void *mem, int len,
  338. enum dma_transfer_direction dir)
  339. {
  340. struct nand_chip *chip = mtd->priv;
  341. struct lpc32xx_nand_host *host = chip->priv;
  342. struct dma_async_tx_descriptor *desc;
  343. int flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
  344. int res;
  345. sg_init_one(&host->sgl, mem, len);
  346. res = dma_map_sg(host->dma_chan->device->dev, &host->sgl, 1,
  347. DMA_BIDIRECTIONAL);
  348. if (res != 1) {
  349. dev_err(mtd->dev.parent, "Failed to map sg list\n");
  350. return -ENXIO;
  351. }
  352. desc = dmaengine_prep_slave_sg(host->dma_chan, &host->sgl, 1, dir,
  353. flags);
  354. if (!desc) {
  355. dev_err(mtd->dev.parent, "Failed to prepare slave sg\n");
  356. goto out1;
  357. }
  358. init_completion(&host->comp_dma);
  359. desc->callback = lpc32xx_dma_complete_func;
  360. desc->callback_param = &host->comp_dma;
  361. dmaengine_submit(desc);
  362. dma_async_issue_pending(host->dma_chan);
  363. wait_for_completion_timeout(&host->comp_dma, msecs_to_jiffies(1000));
  364. dma_unmap_sg(host->dma_chan->device->dev, &host->sgl, 1,
  365. DMA_BIDIRECTIONAL);
  366. return 0;
  367. out1:
  368. dma_unmap_sg(host->dma_chan->device->dev, &host->sgl, 1,
  369. DMA_BIDIRECTIONAL);
  370. return -ENXIO;
  371. }
  372. static int lpc32xx_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  373. uint8_t *buf, int oob_required, int page)
  374. {
  375. struct lpc32xx_nand_host *host = chip->priv;
  376. int i, j;
  377. uint8_t *oobbuf = chip->oob_poi;
  378. uint32_t mlc_isr;
  379. int res;
  380. uint8_t *dma_buf;
  381. bool dma_mapped;
  382. if ((void *)buf <= high_memory) {
  383. dma_buf = buf;
  384. dma_mapped = true;
  385. } else {
  386. dma_buf = host->dma_buf;
  387. dma_mapped = false;
  388. }
  389. /* Writing Command and Address */
  390. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  391. /* For all sub-pages */
  392. for (i = 0; i < host->mlcsubpages; i++) {
  393. /* Start Auto Decode Command */
  394. writeb(0x00, MLC_ECC_AUTO_DEC_REG(host->io_base));
  395. /* Wait for Controller Ready */
  396. lpc32xx_waitfunc_controller(mtd, chip);
  397. /* Check ECC Error status */
  398. mlc_isr = readl(MLC_ISR(host->io_base));
  399. if (mlc_isr & MLCISR_DECODER_FAILURE) {
  400. mtd->ecc_stats.failed++;
  401. dev_warn(&mtd->dev, "%s: DECODER_FAILURE\n", __func__);
  402. } else if (mlc_isr & MLCISR_ERRORS_DETECTED) {
  403. mtd->ecc_stats.corrected += ((mlc_isr >> 4) & 0x3) + 1;
  404. }
  405. /* Read 512 + 16 Bytes */
  406. if (use_dma) {
  407. res = lpc32xx_xmit_dma(mtd, dma_buf + i * 512, 512,
  408. DMA_DEV_TO_MEM);
  409. if (res)
  410. return res;
  411. } else {
  412. for (j = 0; j < (512 >> 2); j++) {
  413. *((uint32_t *)(buf)) =
  414. readl(MLC_BUFF(host->io_base));
  415. buf += 4;
  416. }
  417. }
  418. for (j = 0; j < (16 >> 2); j++) {
  419. *((uint32_t *)(oobbuf)) =
  420. readl(MLC_BUFF(host->io_base));
  421. oobbuf += 4;
  422. }
  423. }
  424. if (use_dma && !dma_mapped)
  425. memcpy(buf, dma_buf, mtd->writesize);
  426. return 0;
  427. }
  428. static int lpc32xx_write_page_lowlevel(struct mtd_info *mtd,
  429. struct nand_chip *chip,
  430. const uint8_t *buf, int oob_required)
  431. {
  432. struct lpc32xx_nand_host *host = chip->priv;
  433. const uint8_t *oobbuf = chip->oob_poi;
  434. uint8_t *dma_buf = (uint8_t *)buf;
  435. int res;
  436. int i, j;
  437. if (use_dma && (void *)buf >= high_memory) {
  438. dma_buf = host->dma_buf;
  439. memcpy(dma_buf, buf, mtd->writesize);
  440. }
  441. for (i = 0; i < host->mlcsubpages; i++) {
  442. /* Start Encode */
  443. writeb(0x00, MLC_ECC_ENC_REG(host->io_base));
  444. /* Write 512 + 6 Bytes to Buffer */
  445. if (use_dma) {
  446. res = lpc32xx_xmit_dma(mtd, dma_buf + i * 512, 512,
  447. DMA_MEM_TO_DEV);
  448. if (res)
  449. return res;
  450. } else {
  451. for (j = 0; j < (512 >> 2); j++) {
  452. writel(*((uint32_t *)(buf)),
  453. MLC_BUFF(host->io_base));
  454. buf += 4;
  455. }
  456. }
  457. writel(*((uint32_t *)(oobbuf)), MLC_BUFF(host->io_base));
  458. oobbuf += 4;
  459. writew(*((uint16_t *)(oobbuf)), MLC_BUFF(host->io_base));
  460. oobbuf += 12;
  461. /* Auto Encode w/ Bit 8 = 0 (see LPC MLC Controller manual) */
  462. writeb(0x00, MLC_ECC_AUTO_ENC_REG(host->io_base));
  463. /* Wait for Controller Ready */
  464. lpc32xx_waitfunc_controller(mtd, chip);
  465. }
  466. return 0;
  467. }
  468. static int lpc32xx_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  469. const uint8_t *buf, int oob_required, int page,
  470. int cached, int raw)
  471. {
  472. int res;
  473. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  474. res = lpc32xx_write_page_lowlevel(mtd, chip, buf, oob_required);
  475. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  476. lpc32xx_waitfunc(mtd, chip);
  477. return res;
  478. }
  479. static int lpc32xx_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  480. int page)
  481. {
  482. struct lpc32xx_nand_host *host = chip->priv;
  483. /* Read whole page - necessary with MLC controller! */
  484. lpc32xx_read_page(mtd, chip, host->dummy_buf, 1, page);
  485. return 0;
  486. }
  487. static int lpc32xx_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  488. int page)
  489. {
  490. /* None, write_oob conflicts with the automatic LPC MLC ECC decoder! */
  491. return 0;
  492. }
  493. /* Prepares MLC for transfers with H/W ECC enabled: always enabled anyway */
  494. static void lpc32xx_ecc_enable(struct mtd_info *mtd, int mode)
  495. {
  496. /* Always enabled! */
  497. }
  498. static bool lpc32xx_dma_filter(struct dma_chan *chan, void *param)
  499. {
  500. struct pl08x_dma_chan *ch =
  501. container_of(chan, struct pl08x_dma_chan, chan);
  502. /* In LPC32xx's PL080 DMA wiring, the MLC NAND DMA signal is #12 */
  503. if (ch->cd->min_signal == 12)
  504. return true;
  505. return false;
  506. }
  507. static int lpc32xx_dma_setup(struct lpc32xx_nand_host *host)
  508. {
  509. struct mtd_info *mtd = &host->mtd;
  510. dma_cap_mask_t mask;
  511. dma_cap_zero(mask);
  512. dma_cap_set(DMA_SLAVE, mask);
  513. host->dma_chan = dma_request_channel(mask, lpc32xx_dma_filter, NULL);
  514. if (!host->dma_chan) {
  515. dev_err(mtd->dev.parent, "Failed to request DMA channel\n");
  516. return -EBUSY;
  517. }
  518. /*
  519. * Set direction to a sensible value even if the dmaengine driver
  520. * should ignore it. With the default (DMA_MEM_TO_MEM), the amba-pl08x
  521. * driver criticizes it as "alien transfer direction".
  522. */
  523. host->dma_slave_config.direction = DMA_DEV_TO_MEM;
  524. host->dma_slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  525. host->dma_slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  526. host->dma_slave_config.src_maxburst = 128;
  527. host->dma_slave_config.dst_maxburst = 128;
  528. /* DMA controller does flow control: */
  529. host->dma_slave_config.device_fc = false;
  530. host->dma_slave_config.src_addr = MLC_BUFF(host->io_base_phy);
  531. host->dma_slave_config.dst_addr = MLC_BUFF(host->io_base_phy);
  532. if (dmaengine_slave_config(host->dma_chan, &host->dma_slave_config)) {
  533. dev_err(mtd->dev.parent, "Failed to setup DMA slave\n");
  534. goto out1;
  535. }
  536. return 0;
  537. out1:
  538. dma_release_channel(host->dma_chan);
  539. return -ENXIO;
  540. }
  541. #ifdef CONFIG_OF
  542. static struct lpc32xx_nand_cfg_mlc *lpc32xx_parse_dt(struct device *dev)
  543. {
  544. struct lpc32xx_nand_cfg_mlc *pdata;
  545. struct device_node *np = dev->of_node;
  546. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  547. if (!pdata) {
  548. dev_err(dev, "could not allocate memory for platform data\n");
  549. return NULL;
  550. }
  551. of_property_read_u32(np, "nxp,tcea-delay", &pdata->tcea_delay);
  552. of_property_read_u32(np, "nxp,busy-delay", &pdata->busy_delay);
  553. of_property_read_u32(np, "nxp,nand-ta", &pdata->nand_ta);
  554. of_property_read_u32(np, "nxp,rd-high", &pdata->rd_high);
  555. of_property_read_u32(np, "nxp,rd-low", &pdata->rd_low);
  556. of_property_read_u32(np, "nxp,wr-high", &pdata->wr_high);
  557. of_property_read_u32(np, "nxp,wr-low", &pdata->wr_low);
  558. if (!pdata->tcea_delay || !pdata->busy_delay || !pdata->nand_ta ||
  559. !pdata->rd_high || !pdata->rd_low || !pdata->wr_high ||
  560. !pdata->wr_low) {
  561. dev_err(dev, "chip parameters not specified correctly\n");
  562. return NULL;
  563. }
  564. pdata->wp_gpio = of_get_named_gpio(np, "gpios", 0);
  565. return pdata;
  566. }
  567. #else
  568. static struct lpc32xx_nand_cfg_mlc *lpc32xx_parse_dt(struct device *dev)
  569. {
  570. return NULL;
  571. }
  572. #endif
  573. /*
  574. * Probe for NAND controller
  575. */
  576. static int __devinit lpc32xx_nand_probe(struct platform_device *pdev)
  577. {
  578. struct lpc32xx_nand_host *host;
  579. struct mtd_info *mtd;
  580. struct nand_chip *nand_chip;
  581. struct resource *rc;
  582. int res;
  583. struct mtd_part_parser_data ppdata = {};
  584. /* Allocate memory for the device structure (and zero it) */
  585. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  586. if (!host) {
  587. dev_err(&pdev->dev, "failed to allocate device structure.\n");
  588. return -ENOMEM;
  589. }
  590. rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  591. if (rc == NULL) {
  592. dev_err(&pdev->dev, "No memory resource found for device!\r\n");
  593. return -ENXIO;
  594. }
  595. host->io_base = devm_request_and_ioremap(&pdev->dev, rc);
  596. if (host->io_base == NULL) {
  597. dev_err(&pdev->dev, "ioremap failed\n");
  598. return -EIO;
  599. }
  600. host->io_base_phy = rc->start;
  601. mtd = &host->mtd;
  602. nand_chip = &host->nand_chip;
  603. if (pdev->dev.of_node)
  604. host->ncfg = lpc32xx_parse_dt(&pdev->dev);
  605. else
  606. host->ncfg = pdev->dev.platform_data;
  607. if (!host->ncfg) {
  608. dev_err(&pdev->dev, "Missing platform data\n");
  609. return -ENOENT;
  610. }
  611. if (host->ncfg->wp_gpio == -EPROBE_DEFER)
  612. return -EPROBE_DEFER;
  613. if (gpio_is_valid(host->ncfg->wp_gpio) &&
  614. gpio_request(host->ncfg->wp_gpio, "NAND WP")) {
  615. dev_err(&pdev->dev, "GPIO not available\n");
  616. return -EBUSY;
  617. }
  618. lpc32xx_wp_disable(host);
  619. nand_chip->priv = host; /* link the private data structures */
  620. mtd->priv = nand_chip;
  621. mtd->owner = THIS_MODULE;
  622. mtd->dev.parent = &pdev->dev;
  623. /* Get NAND clock */
  624. host->clk = clk_get(&pdev->dev, NULL);
  625. if (IS_ERR(host->clk)) {
  626. dev_err(&pdev->dev, "Clock initialization failure\n");
  627. res = -ENOENT;
  628. goto err_exit1;
  629. }
  630. clk_enable(host->clk);
  631. nand_chip->cmd_ctrl = lpc32xx_nand_cmd_ctrl;
  632. nand_chip->dev_ready = lpc32xx_nand_device_ready;
  633. nand_chip->chip_delay = 25; /* us */
  634. nand_chip->IO_ADDR_R = MLC_DATA(host->io_base);
  635. nand_chip->IO_ADDR_W = MLC_DATA(host->io_base);
  636. /* Init NAND controller */
  637. lpc32xx_nand_setup(host);
  638. platform_set_drvdata(pdev, host);
  639. /* Initialize function pointers */
  640. nand_chip->ecc.hwctl = lpc32xx_ecc_enable;
  641. nand_chip->ecc.read_page_raw = lpc32xx_read_page;
  642. nand_chip->ecc.read_page = lpc32xx_read_page;
  643. nand_chip->ecc.write_page_raw = lpc32xx_write_page_lowlevel;
  644. nand_chip->ecc.write_page = lpc32xx_write_page_lowlevel;
  645. nand_chip->ecc.write_oob = lpc32xx_write_oob;
  646. nand_chip->ecc.read_oob = lpc32xx_read_oob;
  647. nand_chip->ecc.strength = 4;
  648. nand_chip->write_page = lpc32xx_write_page;
  649. nand_chip->waitfunc = lpc32xx_waitfunc;
  650. nand_chip->bbt_options = NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
  651. nand_chip->bbt_td = &lpc32xx_nand_bbt;
  652. nand_chip->bbt_md = &lpc32xx_nand_bbt_mirror;
  653. /* bitflip_threshold's default is defined as ecc_strength anyway.
  654. * Unfortunately, it is set only later at add_mtd_device(). Meanwhile
  655. * being 0, it causes bad block table scanning errors in
  656. * nand_scan_tail(), so preparing it here. */
  657. mtd->bitflip_threshold = nand_chip->ecc.strength;
  658. if (use_dma) {
  659. res = lpc32xx_dma_setup(host);
  660. if (res) {
  661. res = -EIO;
  662. goto err_exit2;
  663. }
  664. }
  665. /*
  666. * Scan to find existance of the device and
  667. * Get the type of NAND device SMALL block or LARGE block
  668. */
  669. if (nand_scan_ident(mtd, 1, NULL)) {
  670. res = -ENXIO;
  671. goto err_exit3;
  672. }
  673. host->dma_buf = devm_kzalloc(&pdev->dev, mtd->writesize, GFP_KERNEL);
  674. if (!host->dma_buf) {
  675. dev_err(&pdev->dev, "Error allocating dma_buf memory\n");
  676. res = -ENOMEM;
  677. goto err_exit3;
  678. }
  679. host->dummy_buf = devm_kzalloc(&pdev->dev, mtd->writesize, GFP_KERNEL);
  680. if (!host->dummy_buf) {
  681. dev_err(&pdev->dev, "Error allocating dummy_buf memory\n");
  682. res = -ENOMEM;
  683. goto err_exit3;
  684. }
  685. nand_chip->ecc.mode = NAND_ECC_HW;
  686. nand_chip->ecc.size = mtd->writesize;
  687. nand_chip->ecc.layout = &lpc32xx_nand_oob;
  688. host->mlcsubpages = mtd->writesize / 512;
  689. /* initially clear interrupt status */
  690. readb(MLC_IRQ_SR(host->io_base));
  691. init_completion(&host->comp_nand);
  692. init_completion(&host->comp_controller);
  693. host->irq = platform_get_irq(pdev, 0);
  694. if ((host->irq < 0) || (host->irq >= NR_IRQS)) {
  695. dev_err(&pdev->dev, "failed to get platform irq\n");
  696. res = -EINVAL;
  697. goto err_exit3;
  698. }
  699. if (request_irq(host->irq, (irq_handler_t)&lpc3xxx_nand_irq,
  700. IRQF_TRIGGER_HIGH, DRV_NAME, host)) {
  701. dev_err(&pdev->dev, "Error requesting NAND IRQ\n");
  702. res = -ENXIO;
  703. goto err_exit3;
  704. }
  705. /*
  706. * Fills out all the uninitialized function pointers with the defaults
  707. * And scans for a bad block table if appropriate.
  708. */
  709. if (nand_scan_tail(mtd)) {
  710. res = -ENXIO;
  711. goto err_exit4;
  712. }
  713. mtd->name = DRV_NAME;
  714. ppdata.of_node = pdev->dev.of_node;
  715. res = mtd_device_parse_register(mtd, NULL, &ppdata, host->ncfg->parts,
  716. host->ncfg->num_parts);
  717. if (!res)
  718. return res;
  719. nand_release(mtd);
  720. err_exit4:
  721. free_irq(host->irq, host);
  722. err_exit3:
  723. if (use_dma)
  724. dma_release_channel(host->dma_chan);
  725. err_exit2:
  726. clk_disable(host->clk);
  727. clk_put(host->clk);
  728. platform_set_drvdata(pdev, NULL);
  729. err_exit1:
  730. lpc32xx_wp_enable(host);
  731. gpio_free(host->ncfg->wp_gpio);
  732. return res;
  733. }
  734. /*
  735. * Remove NAND device
  736. */
  737. static int __devexit lpc32xx_nand_remove(struct platform_device *pdev)
  738. {
  739. struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
  740. struct mtd_info *mtd = &host->mtd;
  741. nand_release(mtd);
  742. free_irq(host->irq, host);
  743. if (use_dma)
  744. dma_release_channel(host->dma_chan);
  745. clk_disable(host->clk);
  746. clk_put(host->clk);
  747. platform_set_drvdata(pdev, NULL);
  748. lpc32xx_wp_enable(host);
  749. gpio_free(host->ncfg->wp_gpio);
  750. return 0;
  751. }
  752. #ifdef CONFIG_PM
  753. static int lpc32xx_nand_resume(struct platform_device *pdev)
  754. {
  755. struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
  756. /* Re-enable NAND clock */
  757. clk_enable(host->clk);
  758. /* Fresh init of NAND controller */
  759. lpc32xx_nand_setup(host);
  760. /* Disable write protect */
  761. lpc32xx_wp_disable(host);
  762. return 0;
  763. }
  764. static int lpc32xx_nand_suspend(struct platform_device *pdev, pm_message_t pm)
  765. {
  766. struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
  767. /* Enable write protect for safety */
  768. lpc32xx_wp_enable(host);
  769. /* Disable clock */
  770. clk_disable(host->clk);
  771. return 0;
  772. }
  773. #else
  774. #define lpc32xx_nand_resume NULL
  775. #define lpc32xx_nand_suspend NULL
  776. #endif
  777. #if defined(CONFIG_OF)
  778. static const struct of_device_id lpc32xx_nand_match[] = {
  779. { .compatible = "nxp,lpc3220-mlc" },
  780. { /* sentinel */ },
  781. };
  782. MODULE_DEVICE_TABLE(of, lpc32xx_nand_match);
  783. #endif
  784. static struct platform_driver lpc32xx_nand_driver = {
  785. .probe = lpc32xx_nand_probe,
  786. .remove = __devexit_p(lpc32xx_nand_remove),
  787. .resume = lpc32xx_nand_resume,
  788. .suspend = lpc32xx_nand_suspend,
  789. .driver = {
  790. .name = DRV_NAME,
  791. .owner = THIS_MODULE,
  792. .of_match_table = of_match_ptr(lpc32xx_nand_match),
  793. },
  794. };
  795. module_platform_driver(lpc32xx_nand_driver);
  796. MODULE_LICENSE("GPL");
  797. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  798. MODULE_DESCRIPTION("NAND driver for the NXP LPC32XX MLC controller");