lpc32xx_mlc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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. {
  433. struct lpc32xx_nand_host *host = chip->priv;
  434. const uint8_t *oobbuf = chip->oob_poi;
  435. uint8_t *dma_buf = (uint8_t *)buf;
  436. int res;
  437. int i, j;
  438. if (use_dma && (void *)buf >= high_memory) {
  439. dma_buf = host->dma_buf;
  440. memcpy(dma_buf, buf, mtd->writesize);
  441. }
  442. for (i = 0; i < host->mlcsubpages; i++) {
  443. /* Start Encode */
  444. writeb(0x00, MLC_ECC_ENC_REG(host->io_base));
  445. /* Write 512 + 6 Bytes to Buffer */
  446. if (use_dma) {
  447. res = lpc32xx_xmit_dma(mtd, dma_buf + i * 512, 512,
  448. DMA_MEM_TO_DEV);
  449. if (res)
  450. return res;
  451. } else {
  452. for (j = 0; j < (512 >> 2); j++) {
  453. writel(*((uint32_t *)(buf)),
  454. MLC_BUFF(host->io_base));
  455. buf += 4;
  456. }
  457. }
  458. writel(*((uint32_t *)(oobbuf)), MLC_BUFF(host->io_base));
  459. oobbuf += 4;
  460. writew(*((uint16_t *)(oobbuf)), MLC_BUFF(host->io_base));
  461. oobbuf += 12;
  462. /* Auto Encode w/ Bit 8 = 0 (see LPC MLC Controller manual) */
  463. writeb(0x00, MLC_ECC_AUTO_ENC_REG(host->io_base));
  464. /* Wait for Controller Ready */
  465. lpc32xx_waitfunc_controller(mtd, chip);
  466. }
  467. return 0;
  468. }
  469. static int lpc32xx_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  470. const uint8_t *buf, int oob_required, int page,
  471. int cached, int raw)
  472. {
  473. int res;
  474. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  475. res = lpc32xx_write_page_lowlevel(mtd, chip, buf, oob_required);
  476. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  477. lpc32xx_waitfunc(mtd, chip);
  478. return res;
  479. }
  480. static int lpc32xx_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  481. int page)
  482. {
  483. struct lpc32xx_nand_host *host = chip->priv;
  484. /* Read whole page - necessary with MLC controller! */
  485. lpc32xx_read_page(mtd, chip, host->dummy_buf, 1, page);
  486. return 0;
  487. }
  488. static int lpc32xx_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  489. int page)
  490. {
  491. /* None, write_oob conflicts with the automatic LPC MLC ECC decoder! */
  492. return 0;
  493. }
  494. /* Prepares MLC for transfers with H/W ECC enabled: always enabled anyway */
  495. static void lpc32xx_ecc_enable(struct mtd_info *mtd, int mode)
  496. {
  497. /* Always enabled! */
  498. }
  499. static int lpc32xx_dma_setup(struct lpc32xx_nand_host *host)
  500. {
  501. struct mtd_info *mtd = &host->mtd;
  502. dma_cap_mask_t mask;
  503. if (!host->pdata || !host->pdata->dma_filter) {
  504. dev_err(mtd->dev.parent, "no DMA platform data\n");
  505. return -ENOENT;
  506. }
  507. dma_cap_zero(mask);
  508. dma_cap_set(DMA_SLAVE, mask);
  509. host->dma_chan = dma_request_channel(mask, host->pdata->dma_filter,
  510. "nand-mlc");
  511. if (!host->dma_chan) {
  512. dev_err(mtd->dev.parent, "Failed to request DMA channel\n");
  513. return -EBUSY;
  514. }
  515. /*
  516. * Set direction to a sensible value even if the dmaengine driver
  517. * should ignore it. With the default (DMA_MEM_TO_MEM), the amba-pl08x
  518. * driver criticizes it as "alien transfer direction".
  519. */
  520. host->dma_slave_config.direction = DMA_DEV_TO_MEM;
  521. host->dma_slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  522. host->dma_slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  523. host->dma_slave_config.src_maxburst = 128;
  524. host->dma_slave_config.dst_maxburst = 128;
  525. /* DMA controller does flow control: */
  526. host->dma_slave_config.device_fc = false;
  527. host->dma_slave_config.src_addr = MLC_BUFF(host->io_base_phy);
  528. host->dma_slave_config.dst_addr = MLC_BUFF(host->io_base_phy);
  529. if (dmaengine_slave_config(host->dma_chan, &host->dma_slave_config)) {
  530. dev_err(mtd->dev.parent, "Failed to setup DMA slave\n");
  531. goto out1;
  532. }
  533. return 0;
  534. out1:
  535. dma_release_channel(host->dma_chan);
  536. return -ENXIO;
  537. }
  538. static struct lpc32xx_nand_cfg_mlc *lpc32xx_parse_dt(struct device *dev)
  539. {
  540. struct lpc32xx_nand_cfg_mlc *ncfg;
  541. struct device_node *np = dev->of_node;
  542. ncfg = devm_kzalloc(dev, sizeof(*ncfg), GFP_KERNEL);
  543. if (!ncfg) {
  544. dev_err(dev, "could not allocate memory for platform data\n");
  545. return NULL;
  546. }
  547. of_property_read_u32(np, "nxp,tcea-delay", &ncfg->tcea_delay);
  548. of_property_read_u32(np, "nxp,busy-delay", &ncfg->busy_delay);
  549. of_property_read_u32(np, "nxp,nand-ta", &ncfg->nand_ta);
  550. of_property_read_u32(np, "nxp,rd-high", &ncfg->rd_high);
  551. of_property_read_u32(np, "nxp,rd-low", &ncfg->rd_low);
  552. of_property_read_u32(np, "nxp,wr-high", &ncfg->wr_high);
  553. of_property_read_u32(np, "nxp,wr-low", &ncfg->wr_low);
  554. if (!ncfg->tcea_delay || !ncfg->busy_delay || !ncfg->nand_ta ||
  555. !ncfg->rd_high || !ncfg->rd_low || !ncfg->wr_high ||
  556. !ncfg->wr_low) {
  557. dev_err(dev, "chip parameters not specified correctly\n");
  558. return NULL;
  559. }
  560. ncfg->wp_gpio = of_get_named_gpio(np, "gpios", 0);
  561. return ncfg;
  562. }
  563. /*
  564. * Probe for NAND controller
  565. */
  566. static int lpc32xx_nand_probe(struct platform_device *pdev)
  567. {
  568. struct lpc32xx_nand_host *host;
  569. struct mtd_info *mtd;
  570. struct nand_chip *nand_chip;
  571. struct resource *rc;
  572. int res;
  573. struct mtd_part_parser_data ppdata = {};
  574. /* Allocate memory for the device structure (and zero it) */
  575. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  576. if (!host) {
  577. dev_err(&pdev->dev, "failed to allocate device structure.\n");
  578. return -ENOMEM;
  579. }
  580. rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  581. if (rc == NULL) {
  582. dev_err(&pdev->dev, "No memory resource found for device!\r\n");
  583. return -ENXIO;
  584. }
  585. host->io_base = devm_ioremap_resource(&pdev->dev, rc);
  586. if (IS_ERR(host->io_base))
  587. return PTR_ERR(host->io_base);
  588. host->io_base_phy = rc->start;
  589. mtd = &host->mtd;
  590. nand_chip = &host->nand_chip;
  591. if (pdev->dev.of_node)
  592. host->ncfg = lpc32xx_parse_dt(&pdev->dev);
  593. if (!host->ncfg) {
  594. dev_err(&pdev->dev,
  595. "Missing or bad NAND config from device tree\n");
  596. return -ENOENT;
  597. }
  598. if (host->ncfg->wp_gpio == -EPROBE_DEFER)
  599. return -EPROBE_DEFER;
  600. if (gpio_is_valid(host->ncfg->wp_gpio) &&
  601. gpio_request(host->ncfg->wp_gpio, "NAND WP")) {
  602. dev_err(&pdev->dev, "GPIO not available\n");
  603. return -EBUSY;
  604. }
  605. lpc32xx_wp_disable(host);
  606. host->pdata = pdev->dev.platform_data;
  607. nand_chip->priv = host; /* link the private data structures */
  608. mtd->priv = nand_chip;
  609. mtd->owner = THIS_MODULE;
  610. mtd->dev.parent = &pdev->dev;
  611. /* Get NAND clock */
  612. host->clk = clk_get(&pdev->dev, NULL);
  613. if (IS_ERR(host->clk)) {
  614. dev_err(&pdev->dev, "Clock initialization failure\n");
  615. res = -ENOENT;
  616. goto err_exit1;
  617. }
  618. clk_enable(host->clk);
  619. nand_chip->cmd_ctrl = lpc32xx_nand_cmd_ctrl;
  620. nand_chip->dev_ready = lpc32xx_nand_device_ready;
  621. nand_chip->chip_delay = 25; /* us */
  622. nand_chip->IO_ADDR_R = MLC_DATA(host->io_base);
  623. nand_chip->IO_ADDR_W = MLC_DATA(host->io_base);
  624. /* Init NAND controller */
  625. lpc32xx_nand_setup(host);
  626. platform_set_drvdata(pdev, host);
  627. /* Initialize function pointers */
  628. nand_chip->ecc.hwctl = lpc32xx_ecc_enable;
  629. nand_chip->ecc.read_page_raw = lpc32xx_read_page;
  630. nand_chip->ecc.read_page = lpc32xx_read_page;
  631. nand_chip->ecc.write_page_raw = lpc32xx_write_page_lowlevel;
  632. nand_chip->ecc.write_page = lpc32xx_write_page_lowlevel;
  633. nand_chip->ecc.write_oob = lpc32xx_write_oob;
  634. nand_chip->ecc.read_oob = lpc32xx_read_oob;
  635. nand_chip->ecc.strength = 4;
  636. nand_chip->write_page = lpc32xx_write_page;
  637. nand_chip->waitfunc = lpc32xx_waitfunc;
  638. nand_chip->bbt_options = NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
  639. nand_chip->bbt_td = &lpc32xx_nand_bbt;
  640. nand_chip->bbt_md = &lpc32xx_nand_bbt_mirror;
  641. /* bitflip_threshold's default is defined as ecc_strength anyway.
  642. * Unfortunately, it is set only later at add_mtd_device(). Meanwhile
  643. * being 0, it causes bad block table scanning errors in
  644. * nand_scan_tail(), so preparing it here. */
  645. mtd->bitflip_threshold = nand_chip->ecc.strength;
  646. if (use_dma) {
  647. res = lpc32xx_dma_setup(host);
  648. if (res) {
  649. res = -EIO;
  650. goto err_exit2;
  651. }
  652. }
  653. /*
  654. * Scan to find existance of the device and
  655. * Get the type of NAND device SMALL block or LARGE block
  656. */
  657. if (nand_scan_ident(mtd, 1, NULL)) {
  658. res = -ENXIO;
  659. goto err_exit3;
  660. }
  661. host->dma_buf = devm_kzalloc(&pdev->dev, mtd->writesize, GFP_KERNEL);
  662. if (!host->dma_buf) {
  663. dev_err(&pdev->dev, "Error allocating dma_buf memory\n");
  664. res = -ENOMEM;
  665. goto err_exit3;
  666. }
  667. host->dummy_buf = devm_kzalloc(&pdev->dev, mtd->writesize, GFP_KERNEL);
  668. if (!host->dummy_buf) {
  669. dev_err(&pdev->dev, "Error allocating dummy_buf memory\n");
  670. res = -ENOMEM;
  671. goto err_exit3;
  672. }
  673. nand_chip->ecc.mode = NAND_ECC_HW;
  674. nand_chip->ecc.size = mtd->writesize;
  675. nand_chip->ecc.layout = &lpc32xx_nand_oob;
  676. host->mlcsubpages = mtd->writesize / 512;
  677. /* initially clear interrupt status */
  678. readb(MLC_IRQ_SR(host->io_base));
  679. init_completion(&host->comp_nand);
  680. init_completion(&host->comp_controller);
  681. host->irq = platform_get_irq(pdev, 0);
  682. if ((host->irq < 0) || (host->irq >= NR_IRQS)) {
  683. dev_err(&pdev->dev, "failed to get platform irq\n");
  684. res = -EINVAL;
  685. goto err_exit3;
  686. }
  687. if (request_irq(host->irq, (irq_handler_t)&lpc3xxx_nand_irq,
  688. IRQF_TRIGGER_HIGH, DRV_NAME, host)) {
  689. dev_err(&pdev->dev, "Error requesting NAND IRQ\n");
  690. res = -ENXIO;
  691. goto err_exit3;
  692. }
  693. /*
  694. * Fills out all the uninitialized function pointers with the defaults
  695. * And scans for a bad block table if appropriate.
  696. */
  697. if (nand_scan_tail(mtd)) {
  698. res = -ENXIO;
  699. goto err_exit4;
  700. }
  701. mtd->name = DRV_NAME;
  702. ppdata.of_node = pdev->dev.of_node;
  703. res = mtd_device_parse_register(mtd, NULL, &ppdata, host->ncfg->parts,
  704. host->ncfg->num_parts);
  705. if (!res)
  706. return res;
  707. nand_release(mtd);
  708. err_exit4:
  709. free_irq(host->irq, host);
  710. err_exit3:
  711. if (use_dma)
  712. dma_release_channel(host->dma_chan);
  713. err_exit2:
  714. clk_disable(host->clk);
  715. clk_put(host->clk);
  716. platform_set_drvdata(pdev, NULL);
  717. err_exit1:
  718. lpc32xx_wp_enable(host);
  719. gpio_free(host->ncfg->wp_gpio);
  720. return res;
  721. }
  722. /*
  723. * Remove NAND device
  724. */
  725. static int lpc32xx_nand_remove(struct platform_device *pdev)
  726. {
  727. struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
  728. struct mtd_info *mtd = &host->mtd;
  729. nand_release(mtd);
  730. free_irq(host->irq, host);
  731. if (use_dma)
  732. dma_release_channel(host->dma_chan);
  733. clk_disable(host->clk);
  734. clk_put(host->clk);
  735. platform_set_drvdata(pdev, NULL);
  736. lpc32xx_wp_enable(host);
  737. gpio_free(host->ncfg->wp_gpio);
  738. return 0;
  739. }
  740. #ifdef CONFIG_PM
  741. static int lpc32xx_nand_resume(struct platform_device *pdev)
  742. {
  743. struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
  744. /* Re-enable NAND clock */
  745. clk_enable(host->clk);
  746. /* Fresh init of NAND controller */
  747. lpc32xx_nand_setup(host);
  748. /* Disable write protect */
  749. lpc32xx_wp_disable(host);
  750. return 0;
  751. }
  752. static int lpc32xx_nand_suspend(struct platform_device *pdev, pm_message_t pm)
  753. {
  754. struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
  755. /* Enable write protect for safety */
  756. lpc32xx_wp_enable(host);
  757. /* Disable clock */
  758. clk_disable(host->clk);
  759. return 0;
  760. }
  761. #else
  762. #define lpc32xx_nand_resume NULL
  763. #define lpc32xx_nand_suspend NULL
  764. #endif
  765. static const struct of_device_id lpc32xx_nand_match[] = {
  766. { .compatible = "nxp,lpc3220-mlc" },
  767. { /* sentinel */ },
  768. };
  769. MODULE_DEVICE_TABLE(of, lpc32xx_nand_match);
  770. static struct platform_driver lpc32xx_nand_driver = {
  771. .probe = lpc32xx_nand_probe,
  772. .remove = lpc32xx_nand_remove,
  773. .resume = lpc32xx_nand_resume,
  774. .suspend = lpc32xx_nand_suspend,
  775. .driver = {
  776. .name = DRV_NAME,
  777. .owner = THIS_MODULE,
  778. .of_match_table = of_match_ptr(lpc32xx_nand_match),
  779. },
  780. };
  781. module_platform_driver(lpc32xx_nand_driver);
  782. MODULE_LICENSE("GPL");
  783. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  784. MODULE_DESCRIPTION("NAND driver for the NXP LPC32XX MLC controller");