lpc32xx_mlc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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/mtd/lpc32xx_mlc.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 lpc32xx_mlc_platform_data *pdata;
  159. struct clk *clk;
  160. struct mtd_info mtd;
  161. void __iomem *io_base;
  162. int irq;
  163. struct lpc32xx_nand_cfg_mlc *ncfg;
  164. struct completion comp_nand;
  165. struct completion comp_controller;
  166. uint32_t llptr;
  167. /*
  168. * Physical addresses of ECC buffer, DMA data buffers, OOB data buffer
  169. */
  170. dma_addr_t oob_buf_phy;
  171. /*
  172. * Virtual addresses of ECC buffer, DMA data buffers, OOB data buffer
  173. */
  174. uint8_t *oob_buf;
  175. /* Physical address of DMA base address */
  176. dma_addr_t io_base_phy;
  177. struct completion comp_dma;
  178. struct dma_chan *dma_chan;
  179. struct dma_slave_config dma_slave_config;
  180. struct scatterlist sgl;
  181. uint8_t *dma_buf;
  182. uint8_t *dummy_buf;
  183. int mlcsubpages; /* number of 512bytes-subpages */
  184. };
  185. /*
  186. * Activate/Deactivate DMA Operation:
  187. *
  188. * Using the PL080 DMA Controller for transferring the 512 byte subpages
  189. * instead of doing readl() / writel() in a loop slows it down significantly.
  190. * Measurements via getnstimeofday() upon 512 byte subpage reads reveal:
  191. *
  192. * - readl() of 128 x 32 bits in a loop: ~20us
  193. * - DMA read of 512 bytes (32 bit, 4...128 words bursts): ~60us
  194. * - DMA read of 512 bytes (32 bit, no bursts): ~100us
  195. *
  196. * This applies to the transfer itself. In the DMA case: only the
  197. * wait_for_completion() (DMA setup _not_ included).
  198. *
  199. * Note that the 512 bytes subpage transfer is done directly from/to a
  200. * FIFO/buffer inside the NAND controller. Most of the time (~400-800us for a
  201. * 2048 bytes page) is spent waiting for the NAND IRQ, anyway. (The NAND
  202. * controller transferring data between its internal buffer to/from the NAND
  203. * chip.)
  204. *
  205. * Therefore, using the PL080 DMA is disabled by default, for now.
  206. *
  207. */
  208. static int use_dma;
  209. static void lpc32xx_nand_setup(struct lpc32xx_nand_host *host)
  210. {
  211. uint32_t clkrate, tmp;
  212. /* Reset MLC controller */
  213. writel(MLCCMD_RESET, MLC_CMD(host->io_base));
  214. udelay(1000);
  215. /* Get base clock for MLC block */
  216. clkrate = clk_get_rate(host->clk);
  217. if (clkrate == 0)
  218. clkrate = 104000000;
  219. /* Unlock MLC_ICR
  220. * (among others, will be locked again automatically) */
  221. writew(MLCLOCKPR_MAGIC, MLC_LOCK_PR(host->io_base));
  222. /* Configure MLC Controller: Large Block, 5 Byte Address */
  223. tmp = MLCICR_LARGEBLOCK | MLCICR_LONGADDR;
  224. writel(tmp, MLC_ICR(host->io_base));
  225. /* Unlock MLC_TIME_REG
  226. * (among others, will be locked again automatically) */
  227. writew(MLCLOCKPR_MAGIC, MLC_LOCK_PR(host->io_base));
  228. /* Compute clock setup values, see LPC and NAND manual */
  229. tmp = 0;
  230. tmp |= MLCTIMEREG_TCEA_DELAY(clkrate / host->ncfg->tcea_delay + 1);
  231. tmp |= MLCTIMEREG_BUSY_DELAY(clkrate / host->ncfg->busy_delay + 1);
  232. tmp |= MLCTIMEREG_NAND_TA(clkrate / host->ncfg->nand_ta + 1);
  233. tmp |= MLCTIMEREG_RD_HIGH(clkrate / host->ncfg->rd_high + 1);
  234. tmp |= MLCTIMEREG_RD_LOW(clkrate / host->ncfg->rd_low);
  235. tmp |= MLCTIMEREG_WR_HIGH(clkrate / host->ncfg->wr_high + 1);
  236. tmp |= MLCTIMEREG_WR_LOW(clkrate / host->ncfg->wr_low);
  237. writel(tmp, MLC_TIME_REG(host->io_base));
  238. /* Enable IRQ for CONTROLLER_READY and NAND_READY */
  239. writeb(MLCIRQ_CONTROLLER_READY | MLCIRQ_NAND_READY,
  240. MLC_IRQ_MR(host->io_base));
  241. /* Normal nCE operation: nCE controlled by controller */
  242. writel(MLCCEH_NORMAL, MLC_CEH(host->io_base));
  243. }
  244. /*
  245. * Hardware specific access to control lines
  246. */
  247. static void lpc32xx_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
  248. unsigned int ctrl)
  249. {
  250. struct nand_chip *nand_chip = mtd->priv;
  251. struct lpc32xx_nand_host *host = nand_chip->priv;
  252. if (cmd != NAND_CMD_NONE) {
  253. if (ctrl & NAND_CLE)
  254. writel(cmd, MLC_CMD(host->io_base));
  255. else
  256. writel(cmd, MLC_ADDR(host->io_base));
  257. }
  258. }
  259. /*
  260. * Read Device Ready (NAND device _and_ controller ready)
  261. */
  262. static int lpc32xx_nand_device_ready(struct mtd_info *mtd)
  263. {
  264. struct nand_chip *nand_chip = mtd->priv;
  265. struct lpc32xx_nand_host *host = nand_chip->priv;
  266. if ((readb(MLC_ISR(host->io_base)) &
  267. (MLCISR_CONTROLLER_READY | MLCISR_NAND_READY)) ==
  268. (MLCISR_CONTROLLER_READY | MLCISR_NAND_READY))
  269. return 1;
  270. return 0;
  271. }
  272. static irqreturn_t lpc3xxx_nand_irq(int irq, struct lpc32xx_nand_host *host)
  273. {
  274. uint8_t sr;
  275. /* Clear interrupt flag by reading status */
  276. sr = readb(MLC_IRQ_SR(host->io_base));
  277. if (sr & MLCIRQ_NAND_READY)
  278. complete(&host->comp_nand);
  279. if (sr & MLCIRQ_CONTROLLER_READY)
  280. complete(&host->comp_controller);
  281. return IRQ_HANDLED;
  282. }
  283. static int lpc32xx_waitfunc_nand(struct mtd_info *mtd, struct nand_chip *chip)
  284. {
  285. struct lpc32xx_nand_host *host = chip->priv;
  286. if (readb(MLC_ISR(host->io_base)) & MLCISR_NAND_READY)
  287. goto exit;
  288. wait_for_completion(&host->comp_nand);
  289. while (!(readb(MLC_ISR(host->io_base)) & MLCISR_NAND_READY)) {
  290. /* Seems to be delayed sometimes by controller */
  291. dev_dbg(&mtd->dev, "Warning: NAND not ready.\n");
  292. cpu_relax();
  293. }
  294. exit:
  295. return NAND_STATUS_READY;
  296. }
  297. static int lpc32xx_waitfunc_controller(struct mtd_info *mtd,
  298. struct nand_chip *chip)
  299. {
  300. struct lpc32xx_nand_host *host = chip->priv;
  301. if (readb(MLC_ISR(host->io_base)) & MLCISR_CONTROLLER_READY)
  302. goto exit;
  303. wait_for_completion(&host->comp_controller);
  304. while (!(readb(MLC_ISR(host->io_base)) &
  305. MLCISR_CONTROLLER_READY)) {
  306. dev_dbg(&mtd->dev, "Warning: Controller not ready.\n");
  307. cpu_relax();
  308. }
  309. exit:
  310. return NAND_STATUS_READY;
  311. }
  312. static int lpc32xx_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
  313. {
  314. lpc32xx_waitfunc_nand(mtd, chip);
  315. lpc32xx_waitfunc_controller(mtd, chip);
  316. return NAND_STATUS_READY;
  317. }
  318. /*
  319. * Enable NAND write protect
  320. */
  321. static void lpc32xx_wp_enable(struct lpc32xx_nand_host *host)
  322. {
  323. if (gpio_is_valid(host->ncfg->wp_gpio))
  324. gpio_set_value(host->ncfg->wp_gpio, 0);
  325. }
  326. /*
  327. * Disable NAND write protect
  328. */
  329. static void lpc32xx_wp_disable(struct lpc32xx_nand_host *host)
  330. {
  331. if (gpio_is_valid(host->ncfg->wp_gpio))
  332. gpio_set_value(host->ncfg->wp_gpio, 1);
  333. }
  334. static void lpc32xx_dma_complete_func(void *completion)
  335. {
  336. complete(completion);
  337. }
  338. static int lpc32xx_xmit_dma(struct mtd_info *mtd, void *mem, int len,
  339. enum dma_transfer_direction dir)
  340. {
  341. struct nand_chip *chip = mtd->priv;
  342. struct lpc32xx_nand_host *host = chip->priv;
  343. struct dma_async_tx_descriptor *desc;
  344. int flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
  345. int res;
  346. sg_init_one(&host->sgl, mem, len);
  347. res = dma_map_sg(host->dma_chan->device->dev, &host->sgl, 1,
  348. DMA_BIDIRECTIONAL);
  349. if (res != 1) {
  350. dev_err(mtd->dev.parent, "Failed to map sg list\n");
  351. return -ENXIO;
  352. }
  353. desc = dmaengine_prep_slave_sg(host->dma_chan, &host->sgl, 1, dir,
  354. flags);
  355. if (!desc) {
  356. dev_err(mtd->dev.parent, "Failed to prepare slave sg\n");
  357. goto out1;
  358. }
  359. init_completion(&host->comp_dma);
  360. desc->callback = lpc32xx_dma_complete_func;
  361. desc->callback_param = &host->comp_dma;
  362. dmaengine_submit(desc);
  363. dma_async_issue_pending(host->dma_chan);
  364. wait_for_completion_timeout(&host->comp_dma, msecs_to_jiffies(1000));
  365. dma_unmap_sg(host->dma_chan->device->dev, &host->sgl, 1,
  366. DMA_BIDIRECTIONAL);
  367. return 0;
  368. out1:
  369. dma_unmap_sg(host->dma_chan->device->dev, &host->sgl, 1,
  370. DMA_BIDIRECTIONAL);
  371. return -ENXIO;
  372. }
  373. static int lpc32xx_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  374. uint8_t *buf, int oob_required, int page)
  375. {
  376. struct lpc32xx_nand_host *host = chip->priv;
  377. int i, j;
  378. uint8_t *oobbuf = chip->oob_poi;
  379. uint32_t mlc_isr;
  380. int res;
  381. uint8_t *dma_buf;
  382. bool dma_mapped;
  383. if ((void *)buf <= high_memory) {
  384. dma_buf = buf;
  385. dma_mapped = true;
  386. } else {
  387. dma_buf = host->dma_buf;
  388. dma_mapped = false;
  389. }
  390. /* Writing Command and Address */
  391. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  392. /* For all sub-pages */
  393. for (i = 0; i < host->mlcsubpages; i++) {
  394. /* Start Auto Decode Command */
  395. writeb(0x00, MLC_ECC_AUTO_DEC_REG(host->io_base));
  396. /* Wait for Controller Ready */
  397. lpc32xx_waitfunc_controller(mtd, chip);
  398. /* Check ECC Error status */
  399. mlc_isr = readl(MLC_ISR(host->io_base));
  400. if (mlc_isr & MLCISR_DECODER_FAILURE) {
  401. mtd->ecc_stats.failed++;
  402. dev_warn(&mtd->dev, "%s: DECODER_FAILURE\n", __func__);
  403. } else if (mlc_isr & MLCISR_ERRORS_DETECTED) {
  404. mtd->ecc_stats.corrected += ((mlc_isr >> 4) & 0x3) + 1;
  405. }
  406. /* Read 512 + 16 Bytes */
  407. if (use_dma) {
  408. res = lpc32xx_xmit_dma(mtd, dma_buf + i * 512, 512,
  409. DMA_DEV_TO_MEM);
  410. if (res)
  411. return res;
  412. } else {
  413. for (j = 0; j < (512 >> 2); j++) {
  414. *((uint32_t *)(buf)) =
  415. readl(MLC_BUFF(host->io_base));
  416. buf += 4;
  417. }
  418. }
  419. for (j = 0; j < (16 >> 2); j++) {
  420. *((uint32_t *)(oobbuf)) =
  421. readl(MLC_BUFF(host->io_base));
  422. oobbuf += 4;
  423. }
  424. }
  425. if (use_dma && !dma_mapped)
  426. memcpy(buf, dma_buf, mtd->writesize);
  427. return 0;
  428. }
  429. static int lpc32xx_write_page_lowlevel(struct mtd_info *mtd,
  430. struct nand_chip *chip,
  431. const uint8_t *buf, int oob_required,
  432. int page)
  433. {
  434. struct lpc32xx_nand_host *host = chip->priv;
  435. const uint8_t *oobbuf = chip->oob_poi;
  436. uint8_t *dma_buf = (uint8_t *)buf;
  437. int res;
  438. int i, j;
  439. if (use_dma && (void *)buf >= high_memory) {
  440. dma_buf = host->dma_buf;
  441. memcpy(dma_buf, buf, mtd->writesize);
  442. }
  443. for (i = 0; i < host->mlcsubpages; i++) {
  444. /* Start Encode */
  445. writeb(0x00, MLC_ECC_ENC_REG(host->io_base));
  446. /* Write 512 + 6 Bytes to Buffer */
  447. if (use_dma) {
  448. res = lpc32xx_xmit_dma(mtd, dma_buf + i * 512, 512,
  449. DMA_MEM_TO_DEV);
  450. if (res)
  451. return res;
  452. } else {
  453. for (j = 0; j < (512 >> 2); j++) {
  454. writel(*((uint32_t *)(buf)),
  455. MLC_BUFF(host->io_base));
  456. buf += 4;
  457. }
  458. }
  459. writel(*((uint32_t *)(oobbuf)), MLC_BUFF(host->io_base));
  460. oobbuf += 4;
  461. writew(*((uint16_t *)(oobbuf)), MLC_BUFF(host->io_base));
  462. oobbuf += 12;
  463. /* Auto Encode w/ Bit 8 = 0 (see LPC MLC Controller manual) */
  464. writeb(0x00, MLC_ECC_AUTO_ENC_REG(host->io_base));
  465. /* Wait for Controller Ready */
  466. lpc32xx_waitfunc_controller(mtd, chip);
  467. }
  468. return 0;
  469. }
  470. static int lpc32xx_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  471. int page)
  472. {
  473. struct lpc32xx_nand_host *host = chip->priv;
  474. /* Read whole page - necessary with MLC controller! */
  475. lpc32xx_read_page(mtd, chip, host->dummy_buf, 1, page);
  476. return 0;
  477. }
  478. static int lpc32xx_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  479. int page)
  480. {
  481. /* None, write_oob conflicts with the automatic LPC MLC ECC decoder! */
  482. return 0;
  483. }
  484. /* Prepares MLC for transfers with H/W ECC enabled: always enabled anyway */
  485. static void lpc32xx_ecc_enable(struct mtd_info *mtd, int mode)
  486. {
  487. /* Always enabled! */
  488. }
  489. static int lpc32xx_dma_setup(struct lpc32xx_nand_host *host)
  490. {
  491. struct mtd_info *mtd = &host->mtd;
  492. dma_cap_mask_t mask;
  493. if (!host->pdata || !host->pdata->dma_filter) {
  494. dev_err(mtd->dev.parent, "no DMA platform data\n");
  495. return -ENOENT;
  496. }
  497. dma_cap_zero(mask);
  498. dma_cap_set(DMA_SLAVE, mask);
  499. host->dma_chan = dma_request_channel(mask, host->pdata->dma_filter,
  500. "nand-mlc");
  501. if (!host->dma_chan) {
  502. dev_err(mtd->dev.parent, "Failed to request DMA channel\n");
  503. return -EBUSY;
  504. }
  505. /*
  506. * Set direction to a sensible value even if the dmaengine driver
  507. * should ignore it. With the default (DMA_MEM_TO_MEM), the amba-pl08x
  508. * driver criticizes it as "alien transfer direction".
  509. */
  510. host->dma_slave_config.direction = DMA_DEV_TO_MEM;
  511. host->dma_slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  512. host->dma_slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  513. host->dma_slave_config.src_maxburst = 128;
  514. host->dma_slave_config.dst_maxburst = 128;
  515. /* DMA controller does flow control: */
  516. host->dma_slave_config.device_fc = false;
  517. host->dma_slave_config.src_addr = MLC_BUFF(host->io_base_phy);
  518. host->dma_slave_config.dst_addr = MLC_BUFF(host->io_base_phy);
  519. if (dmaengine_slave_config(host->dma_chan, &host->dma_slave_config)) {
  520. dev_err(mtd->dev.parent, "Failed to setup DMA slave\n");
  521. goto out1;
  522. }
  523. return 0;
  524. out1:
  525. dma_release_channel(host->dma_chan);
  526. return -ENXIO;
  527. }
  528. static struct lpc32xx_nand_cfg_mlc *lpc32xx_parse_dt(struct device *dev)
  529. {
  530. struct lpc32xx_nand_cfg_mlc *ncfg;
  531. struct device_node *np = dev->of_node;
  532. ncfg = devm_kzalloc(dev, sizeof(*ncfg), GFP_KERNEL);
  533. if (!ncfg)
  534. return NULL;
  535. of_property_read_u32(np, "nxp,tcea-delay", &ncfg->tcea_delay);
  536. of_property_read_u32(np, "nxp,busy-delay", &ncfg->busy_delay);
  537. of_property_read_u32(np, "nxp,nand-ta", &ncfg->nand_ta);
  538. of_property_read_u32(np, "nxp,rd-high", &ncfg->rd_high);
  539. of_property_read_u32(np, "nxp,rd-low", &ncfg->rd_low);
  540. of_property_read_u32(np, "nxp,wr-high", &ncfg->wr_high);
  541. of_property_read_u32(np, "nxp,wr-low", &ncfg->wr_low);
  542. if (!ncfg->tcea_delay || !ncfg->busy_delay || !ncfg->nand_ta ||
  543. !ncfg->rd_high || !ncfg->rd_low || !ncfg->wr_high ||
  544. !ncfg->wr_low) {
  545. dev_err(dev, "chip parameters not specified correctly\n");
  546. return NULL;
  547. }
  548. ncfg->wp_gpio = of_get_named_gpio(np, "gpios", 0);
  549. return ncfg;
  550. }
  551. /*
  552. * Probe for NAND controller
  553. */
  554. static int lpc32xx_nand_probe(struct platform_device *pdev)
  555. {
  556. struct lpc32xx_nand_host *host;
  557. struct mtd_info *mtd;
  558. struct nand_chip *nand_chip;
  559. struct resource *rc;
  560. int res;
  561. struct mtd_part_parser_data ppdata = {};
  562. /* Allocate memory for the device structure (and zero it) */
  563. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  564. if (!host)
  565. return -ENOMEM;
  566. rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  567. host->io_base = devm_ioremap_resource(&pdev->dev, rc);
  568. if (IS_ERR(host->io_base))
  569. return PTR_ERR(host->io_base);
  570. host->io_base_phy = rc->start;
  571. mtd = &host->mtd;
  572. nand_chip = &host->nand_chip;
  573. if (pdev->dev.of_node)
  574. host->ncfg = lpc32xx_parse_dt(&pdev->dev);
  575. if (!host->ncfg) {
  576. dev_err(&pdev->dev,
  577. "Missing or bad NAND config from device tree\n");
  578. return -ENOENT;
  579. }
  580. if (host->ncfg->wp_gpio == -EPROBE_DEFER)
  581. return -EPROBE_DEFER;
  582. if (gpio_is_valid(host->ncfg->wp_gpio) &&
  583. gpio_request(host->ncfg->wp_gpio, "NAND WP")) {
  584. dev_err(&pdev->dev, "GPIO not available\n");
  585. return -EBUSY;
  586. }
  587. lpc32xx_wp_disable(host);
  588. host->pdata = dev_get_platdata(&pdev->dev);
  589. nand_chip->priv = host; /* link the private data structures */
  590. mtd->priv = nand_chip;
  591. mtd->dev.parent = &pdev->dev;
  592. /* Get NAND clock */
  593. host->clk = clk_get(&pdev->dev, NULL);
  594. if (IS_ERR(host->clk)) {
  595. dev_err(&pdev->dev, "Clock initialization failure\n");
  596. res = -ENOENT;
  597. goto err_exit1;
  598. }
  599. clk_prepare_enable(host->clk);
  600. nand_chip->cmd_ctrl = lpc32xx_nand_cmd_ctrl;
  601. nand_chip->dev_ready = lpc32xx_nand_device_ready;
  602. nand_chip->chip_delay = 25; /* us */
  603. nand_chip->IO_ADDR_R = MLC_DATA(host->io_base);
  604. nand_chip->IO_ADDR_W = MLC_DATA(host->io_base);
  605. /* Init NAND controller */
  606. lpc32xx_nand_setup(host);
  607. platform_set_drvdata(pdev, host);
  608. /* Initialize function pointers */
  609. nand_chip->ecc.hwctl = lpc32xx_ecc_enable;
  610. nand_chip->ecc.read_page_raw = lpc32xx_read_page;
  611. nand_chip->ecc.read_page = lpc32xx_read_page;
  612. nand_chip->ecc.write_page_raw = lpc32xx_write_page_lowlevel;
  613. nand_chip->ecc.write_page = lpc32xx_write_page_lowlevel;
  614. nand_chip->ecc.write_oob = lpc32xx_write_oob;
  615. nand_chip->ecc.read_oob = lpc32xx_read_oob;
  616. nand_chip->ecc.strength = 4;
  617. nand_chip->waitfunc = lpc32xx_waitfunc;
  618. nand_chip->options = NAND_NO_SUBPAGE_WRITE;
  619. nand_chip->bbt_options = NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
  620. nand_chip->bbt_td = &lpc32xx_nand_bbt;
  621. nand_chip->bbt_md = &lpc32xx_nand_bbt_mirror;
  622. if (use_dma) {
  623. res = lpc32xx_dma_setup(host);
  624. if (res) {
  625. res = -EIO;
  626. goto err_exit2;
  627. }
  628. }
  629. /*
  630. * Scan to find existance of the device and
  631. * Get the type of NAND device SMALL block or LARGE block
  632. */
  633. if (nand_scan_ident(mtd, 1, NULL)) {
  634. res = -ENXIO;
  635. goto err_exit3;
  636. }
  637. host->dma_buf = devm_kzalloc(&pdev->dev, mtd->writesize, GFP_KERNEL);
  638. if (!host->dma_buf) {
  639. res = -ENOMEM;
  640. goto err_exit3;
  641. }
  642. host->dummy_buf = devm_kzalloc(&pdev->dev, mtd->writesize, GFP_KERNEL);
  643. if (!host->dummy_buf) {
  644. res = -ENOMEM;
  645. goto err_exit3;
  646. }
  647. nand_chip->ecc.mode = NAND_ECC_HW;
  648. nand_chip->ecc.size = mtd->writesize;
  649. nand_chip->ecc.layout = &lpc32xx_nand_oob;
  650. host->mlcsubpages = mtd->writesize / 512;
  651. /* initially clear interrupt status */
  652. readb(MLC_IRQ_SR(host->io_base));
  653. init_completion(&host->comp_nand);
  654. init_completion(&host->comp_controller);
  655. host->irq = platform_get_irq(pdev, 0);
  656. if ((host->irq < 0) || (host->irq >= NR_IRQS)) {
  657. dev_err(&pdev->dev, "failed to get platform irq\n");
  658. res = -EINVAL;
  659. goto err_exit3;
  660. }
  661. if (request_irq(host->irq, (irq_handler_t)&lpc3xxx_nand_irq,
  662. IRQF_TRIGGER_HIGH, DRV_NAME, host)) {
  663. dev_err(&pdev->dev, "Error requesting NAND IRQ\n");
  664. res = -ENXIO;
  665. goto err_exit3;
  666. }
  667. /*
  668. * Fills out all the uninitialized function pointers with the defaults
  669. * And scans for a bad block table if appropriate.
  670. */
  671. if (nand_scan_tail(mtd)) {
  672. res = -ENXIO;
  673. goto err_exit4;
  674. }
  675. mtd->name = DRV_NAME;
  676. ppdata.of_node = pdev->dev.of_node;
  677. res = mtd_device_parse_register(mtd, NULL, &ppdata, host->ncfg->parts,
  678. host->ncfg->num_parts);
  679. if (!res)
  680. return res;
  681. nand_release(mtd);
  682. err_exit4:
  683. free_irq(host->irq, host);
  684. err_exit3:
  685. if (use_dma)
  686. dma_release_channel(host->dma_chan);
  687. err_exit2:
  688. clk_disable_unprepare(host->clk);
  689. clk_put(host->clk);
  690. err_exit1:
  691. lpc32xx_wp_enable(host);
  692. gpio_free(host->ncfg->wp_gpio);
  693. return res;
  694. }
  695. /*
  696. * Remove NAND device
  697. */
  698. static int lpc32xx_nand_remove(struct platform_device *pdev)
  699. {
  700. struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
  701. struct mtd_info *mtd = &host->mtd;
  702. nand_release(mtd);
  703. free_irq(host->irq, host);
  704. if (use_dma)
  705. dma_release_channel(host->dma_chan);
  706. clk_disable_unprepare(host->clk);
  707. clk_put(host->clk);
  708. lpc32xx_wp_enable(host);
  709. gpio_free(host->ncfg->wp_gpio);
  710. return 0;
  711. }
  712. #ifdef CONFIG_PM
  713. static int lpc32xx_nand_resume(struct platform_device *pdev)
  714. {
  715. struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
  716. /* Re-enable NAND clock */
  717. clk_prepare_enable(host->clk);
  718. /* Fresh init of NAND controller */
  719. lpc32xx_nand_setup(host);
  720. /* Disable write protect */
  721. lpc32xx_wp_disable(host);
  722. return 0;
  723. }
  724. static int lpc32xx_nand_suspend(struct platform_device *pdev, pm_message_t pm)
  725. {
  726. struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
  727. /* Enable write protect for safety */
  728. lpc32xx_wp_enable(host);
  729. /* Disable clock */
  730. clk_disable_unprepare(host->clk);
  731. return 0;
  732. }
  733. #else
  734. #define lpc32xx_nand_resume NULL
  735. #define lpc32xx_nand_suspend NULL
  736. #endif
  737. static const struct of_device_id lpc32xx_nand_match[] = {
  738. { .compatible = "nxp,lpc3220-mlc" },
  739. { /* sentinel */ },
  740. };
  741. MODULE_DEVICE_TABLE(of, lpc32xx_nand_match);
  742. static struct platform_driver lpc32xx_nand_driver = {
  743. .probe = lpc32xx_nand_probe,
  744. .remove = lpc32xx_nand_remove,
  745. .resume = lpc32xx_nand_resume,
  746. .suspend = lpc32xx_nand_suspend,
  747. .driver = {
  748. .name = DRV_NAME,
  749. .of_match_table = lpc32xx_nand_match,
  750. },
  751. };
  752. module_platform_driver(lpc32xx_nand_driver);
  753. MODULE_LICENSE("GPL");
  754. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  755. MODULE_DESCRIPTION("NAND driver for the NXP LPC32XX MLC controller");