lpc32xx_mlc.c 25 KB

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