lpc32xx_mlc.c 25 KB

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