lpc32xx_mlc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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_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 nand_chip nand_chip;
  164. struct lpc32xx_mlc_platform_data *pdata;
  165. struct clk *clk;
  166. void __iomem *io_base;
  167. int irq;
  168. struct lpc32xx_nand_cfg_mlc *ncfg;
  169. struct completion comp_nand;
  170. struct completion comp_controller;
  171. uint32_t llptr;
  172. /*
  173. * Physical addresses of ECC buffer, DMA data buffers, OOB data buffer
  174. */
  175. dma_addr_t oob_buf_phy;
  176. /*
  177. * Virtual addresses of ECC buffer, DMA data buffers, OOB data buffer
  178. */
  179. uint8_t *oob_buf;
  180. /* Physical address of DMA base address */
  181. dma_addr_t io_base_phy;
  182. struct completion comp_dma;
  183. struct dma_chan *dma_chan;
  184. struct dma_slave_config dma_slave_config;
  185. struct scatterlist sgl;
  186. uint8_t *dma_buf;
  187. uint8_t *dummy_buf;
  188. int mlcsubpages; /* number of 512bytes-subpages */
  189. };
  190. /*
  191. * Activate/Deactivate DMA Operation:
  192. *
  193. * Using the PL080 DMA Controller for transferring the 512 byte subpages
  194. * instead of doing readl() / writel() in a loop slows it down significantly.
  195. * Measurements via getnstimeofday() upon 512 byte subpage reads reveal:
  196. *
  197. * - readl() of 128 x 32 bits in a loop: ~20us
  198. * - DMA read of 512 bytes (32 bit, 4...128 words bursts): ~60us
  199. * - DMA read of 512 bytes (32 bit, no bursts): ~100us
  200. *
  201. * This applies to the transfer itself. In the DMA case: only the
  202. * wait_for_completion() (DMA setup _not_ included).
  203. *
  204. * Note that the 512 bytes subpage transfer is done directly from/to a
  205. * FIFO/buffer inside the NAND controller. Most of the time (~400-800us for a
  206. * 2048 bytes page) is spent waiting for the NAND IRQ, anyway. (The NAND
  207. * controller transferring data between its internal buffer to/from the NAND
  208. * chip.)
  209. *
  210. * Therefore, using the PL080 DMA is disabled by default, for now.
  211. *
  212. */
  213. static int use_dma;
  214. static void lpc32xx_nand_setup(struct lpc32xx_nand_host *host)
  215. {
  216. uint32_t clkrate, tmp;
  217. /* Reset MLC controller */
  218. writel(MLCCMD_RESET, MLC_CMD(host->io_base));
  219. udelay(1000);
  220. /* Get base clock for MLC block */
  221. clkrate = clk_get_rate(host->clk);
  222. if (clkrate == 0)
  223. clkrate = 104000000;
  224. /* Unlock MLC_ICR
  225. * (among others, will be locked again automatically) */
  226. writew(MLCLOCKPR_MAGIC, MLC_LOCK_PR(host->io_base));
  227. /* Configure MLC Controller: Large Block, 5 Byte Address */
  228. tmp = MLCICR_LARGEBLOCK | MLCICR_LONGADDR;
  229. writel(tmp, MLC_ICR(host->io_base));
  230. /* Unlock MLC_TIME_REG
  231. * (among others, will be locked again automatically) */
  232. writew(MLCLOCKPR_MAGIC, MLC_LOCK_PR(host->io_base));
  233. /* Compute clock setup values, see LPC and NAND manual */
  234. tmp = 0;
  235. tmp |= MLCTIMEREG_TCEA_DELAY(clkrate / host->ncfg->tcea_delay + 1);
  236. tmp |= MLCTIMEREG_BUSY_DELAY(clkrate / host->ncfg->busy_delay + 1);
  237. tmp |= MLCTIMEREG_NAND_TA(clkrate / host->ncfg->nand_ta + 1);
  238. tmp |= MLCTIMEREG_RD_HIGH(clkrate / host->ncfg->rd_high + 1);
  239. tmp |= MLCTIMEREG_RD_LOW(clkrate / host->ncfg->rd_low);
  240. tmp |= MLCTIMEREG_WR_HIGH(clkrate / host->ncfg->wr_high + 1);
  241. tmp |= MLCTIMEREG_WR_LOW(clkrate / host->ncfg->wr_low);
  242. writel(tmp, MLC_TIME_REG(host->io_base));
  243. /* Enable IRQ for CONTROLLER_READY and NAND_READY */
  244. writeb(MLCIRQ_CONTROLLER_READY | MLCIRQ_NAND_READY,
  245. MLC_IRQ_MR(host->io_base));
  246. /* Normal nCE operation: nCE controlled by controller */
  247. writel(MLCCEH_NORMAL, MLC_CEH(host->io_base));
  248. }
  249. /*
  250. * Hardware specific access to control lines
  251. */
  252. static void lpc32xx_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
  253. unsigned int ctrl)
  254. {
  255. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  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 mtd_info *mtd)
  268. {
  269. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  270. struct lpc32xx_nand_host *host = nand_get_controller_data(nand_chip);
  271. if ((readb(MLC_ISR(host->io_base)) &
  272. (MLCISR_CONTROLLER_READY | MLCISR_NAND_READY)) ==
  273. (MLCISR_CONTROLLER_READY | MLCISR_NAND_READY))
  274. return 1;
  275. return 0;
  276. }
  277. static irqreturn_t lpc3xxx_nand_irq(int irq, struct lpc32xx_nand_host *host)
  278. {
  279. uint8_t sr;
  280. /* Clear interrupt flag by reading status */
  281. sr = readb(MLC_IRQ_SR(host->io_base));
  282. if (sr & MLCIRQ_NAND_READY)
  283. complete(&host->comp_nand);
  284. if (sr & MLCIRQ_CONTROLLER_READY)
  285. complete(&host->comp_controller);
  286. return IRQ_HANDLED;
  287. }
  288. static int lpc32xx_waitfunc_nand(struct mtd_info *mtd, struct nand_chip *chip)
  289. {
  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 mtd_info *mtd,
  303. struct nand_chip *chip)
  304. {
  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 mtd_info *mtd, struct nand_chip *chip)
  318. {
  319. lpc32xx_waitfunc_nand(mtd, chip);
  320. lpc32xx_waitfunc_controller(mtd, 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 mtd_info *mtd, struct nand_chip *chip,
  379. uint8_t *buf, int oob_required, int page)
  380. {
  381. struct lpc32xx_nand_host *host = nand_get_controller_data(chip);
  382. int i, j;
  383. uint8_t *oobbuf = chip->oob_poi;
  384. uint32_t mlc_isr;
  385. int res;
  386. uint8_t *dma_buf;
  387. bool dma_mapped;
  388. if ((void *)buf <= high_memory) {
  389. dma_buf = buf;
  390. dma_mapped = true;
  391. } else {
  392. dma_buf = host->dma_buf;
  393. dma_mapped = false;
  394. }
  395. /* Writing Command and Address */
  396. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  397. /* For all sub-pages */
  398. for (i = 0; i < host->mlcsubpages; i++) {
  399. /* Start Auto Decode Command */
  400. writeb(0x00, MLC_ECC_AUTO_DEC_REG(host->io_base));
  401. /* Wait for Controller Ready */
  402. lpc32xx_waitfunc_controller(mtd, chip);
  403. /* Check ECC Error status */
  404. mlc_isr = readl(MLC_ISR(host->io_base));
  405. if (mlc_isr & MLCISR_DECODER_FAILURE) {
  406. mtd->ecc_stats.failed++;
  407. dev_warn(&mtd->dev, "%s: DECODER_FAILURE\n", __func__);
  408. } else if (mlc_isr & MLCISR_ERRORS_DETECTED) {
  409. mtd->ecc_stats.corrected += ((mlc_isr >> 4) & 0x3) + 1;
  410. }
  411. /* Read 512 + 16 Bytes */
  412. if (use_dma) {
  413. res = lpc32xx_xmit_dma(mtd, dma_buf + i * 512, 512,
  414. DMA_DEV_TO_MEM);
  415. if (res)
  416. return res;
  417. } else {
  418. for (j = 0; j < (512 >> 2); j++) {
  419. *((uint32_t *)(buf)) =
  420. readl(MLC_BUFF(host->io_base));
  421. buf += 4;
  422. }
  423. }
  424. for (j = 0; j < (16 >> 2); j++) {
  425. *((uint32_t *)(oobbuf)) =
  426. readl(MLC_BUFF(host->io_base));
  427. oobbuf += 4;
  428. }
  429. }
  430. if (use_dma && !dma_mapped)
  431. memcpy(buf, dma_buf, mtd->writesize);
  432. return 0;
  433. }
  434. static int lpc32xx_write_page_lowlevel(struct mtd_info *mtd,
  435. struct nand_chip *chip,
  436. const uint8_t *buf, int oob_required,
  437. int page)
  438. {
  439. struct lpc32xx_nand_host *host = nand_get_controller_data(chip);
  440. const uint8_t *oobbuf = chip->oob_poi;
  441. uint8_t *dma_buf = (uint8_t *)buf;
  442. int res;
  443. int i, j;
  444. if (use_dma && (void *)buf >= high_memory) {
  445. dma_buf = host->dma_buf;
  446. memcpy(dma_buf, buf, mtd->writesize);
  447. }
  448. for (i = 0; i < host->mlcsubpages; i++) {
  449. /* Start Encode */
  450. writeb(0x00, MLC_ECC_ENC_REG(host->io_base));
  451. /* Write 512 + 6 Bytes to Buffer */
  452. if (use_dma) {
  453. res = lpc32xx_xmit_dma(mtd, dma_buf + i * 512, 512,
  454. DMA_MEM_TO_DEV);
  455. if (res)
  456. return res;
  457. } else {
  458. for (j = 0; j < (512 >> 2); j++) {
  459. writel(*((uint32_t *)(buf)),
  460. MLC_BUFF(host->io_base));
  461. buf += 4;
  462. }
  463. }
  464. writel(*((uint32_t *)(oobbuf)), MLC_BUFF(host->io_base));
  465. oobbuf += 4;
  466. writew(*((uint16_t *)(oobbuf)), MLC_BUFF(host->io_base));
  467. oobbuf += 12;
  468. /* Auto Encode w/ Bit 8 = 0 (see LPC MLC Controller manual) */
  469. writeb(0x00, MLC_ECC_AUTO_ENC_REG(host->io_base));
  470. /* Wait for Controller Ready */
  471. lpc32xx_waitfunc_controller(mtd, chip);
  472. }
  473. return 0;
  474. }
  475. static int lpc32xx_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  476. int page)
  477. {
  478. struct lpc32xx_nand_host *host = nand_get_controller_data(chip);
  479. /* Read whole page - necessary with MLC controller! */
  480. lpc32xx_read_page(mtd, chip, host->dummy_buf, 1, page);
  481. return 0;
  482. }
  483. static int lpc32xx_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  484. 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 mtd_info *mtd, 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. /*
  557. * Probe for NAND controller
  558. */
  559. static int lpc32xx_nand_probe(struct platform_device *pdev)
  560. {
  561. struct lpc32xx_nand_host *host;
  562. struct mtd_info *mtd;
  563. struct nand_chip *nand_chip;
  564. struct resource *rc;
  565. int res;
  566. /* Allocate memory for the device structure (and zero it) */
  567. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  568. if (!host)
  569. return -ENOMEM;
  570. rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  571. host->io_base = devm_ioremap_resource(&pdev->dev, rc);
  572. if (IS_ERR(host->io_base))
  573. return PTR_ERR(host->io_base);
  574. host->io_base_phy = rc->start;
  575. nand_chip = &host->nand_chip;
  576. mtd = nand_to_mtd(nand_chip);
  577. if (pdev->dev.of_node)
  578. host->ncfg = lpc32xx_parse_dt(&pdev->dev);
  579. if (!host->ncfg) {
  580. dev_err(&pdev->dev,
  581. "Missing or bad NAND config from device tree\n");
  582. return -ENOENT;
  583. }
  584. if (host->ncfg->wp_gpio == -EPROBE_DEFER)
  585. return -EPROBE_DEFER;
  586. if (gpio_is_valid(host->ncfg->wp_gpio) &&
  587. gpio_request(host->ncfg->wp_gpio, "NAND WP")) {
  588. dev_err(&pdev->dev, "GPIO not available\n");
  589. return -EBUSY;
  590. }
  591. lpc32xx_wp_disable(host);
  592. host->pdata = dev_get_platdata(&pdev->dev);
  593. /* link the private data structures */
  594. nand_set_controller_data(nand_chip, host);
  595. nand_set_flash_node(nand_chip, pdev->dev.of_node);
  596. mtd->dev.parent = &pdev->dev;
  597. /* Get NAND clock */
  598. host->clk = clk_get(&pdev->dev, NULL);
  599. if (IS_ERR(host->clk)) {
  600. dev_err(&pdev->dev, "Clock initialization failure\n");
  601. res = -ENOENT;
  602. goto err_exit1;
  603. }
  604. clk_prepare_enable(host->clk);
  605. nand_chip->cmd_ctrl = lpc32xx_nand_cmd_ctrl;
  606. nand_chip->dev_ready = lpc32xx_nand_device_ready;
  607. nand_chip->chip_delay = 25; /* us */
  608. nand_chip->IO_ADDR_R = MLC_DATA(host->io_base);
  609. nand_chip->IO_ADDR_W = MLC_DATA(host->io_base);
  610. /* Init NAND controller */
  611. lpc32xx_nand_setup(host);
  612. platform_set_drvdata(pdev, host);
  613. /* Initialize function pointers */
  614. nand_chip->ecc.hwctl = lpc32xx_ecc_enable;
  615. nand_chip->ecc.read_page_raw = lpc32xx_read_page;
  616. nand_chip->ecc.read_page = lpc32xx_read_page;
  617. nand_chip->ecc.write_page_raw = lpc32xx_write_page_lowlevel;
  618. nand_chip->ecc.write_page = lpc32xx_write_page_lowlevel;
  619. nand_chip->ecc.write_oob = lpc32xx_write_oob;
  620. nand_chip->ecc.read_oob = lpc32xx_read_oob;
  621. nand_chip->ecc.strength = 4;
  622. nand_chip->ecc.bytes = 10;
  623. nand_chip->waitfunc = lpc32xx_waitfunc;
  624. nand_chip->options = NAND_NO_SUBPAGE_WRITE;
  625. nand_chip->bbt_options = NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
  626. nand_chip->bbt_td = &lpc32xx_nand_bbt;
  627. nand_chip->bbt_md = &lpc32xx_nand_bbt_mirror;
  628. if (use_dma) {
  629. res = lpc32xx_dma_setup(host);
  630. if (res) {
  631. res = -EIO;
  632. goto err_exit2;
  633. }
  634. }
  635. /*
  636. * Scan to find existance of the device and
  637. * Get the type of NAND device SMALL block or LARGE block
  638. */
  639. if (nand_scan_ident(mtd, 1, NULL)) {
  640. res = -ENXIO;
  641. goto err_exit3;
  642. }
  643. host->dma_buf = devm_kzalloc(&pdev->dev, mtd->writesize, GFP_KERNEL);
  644. if (!host->dma_buf) {
  645. res = -ENOMEM;
  646. goto err_exit3;
  647. }
  648. host->dummy_buf = devm_kzalloc(&pdev->dev, mtd->writesize, GFP_KERNEL);
  649. if (!host->dummy_buf) {
  650. res = -ENOMEM;
  651. goto err_exit3;
  652. }
  653. nand_chip->ecc.mode = NAND_ECC_HW;
  654. nand_chip->ecc.size = 512;
  655. mtd_set_ooblayout(mtd, &lpc32xx_ooblayout_ops);
  656. host->mlcsubpages = mtd->writesize / 512;
  657. /* initially clear interrupt status */
  658. readb(MLC_IRQ_SR(host->io_base));
  659. init_completion(&host->comp_nand);
  660. init_completion(&host->comp_controller);
  661. host->irq = platform_get_irq(pdev, 0);
  662. if (host->irq < 0) {
  663. dev_err(&pdev->dev, "failed to get platform irq\n");
  664. res = -EINVAL;
  665. goto err_exit3;
  666. }
  667. if (request_irq(host->irq, (irq_handler_t)&lpc3xxx_nand_irq,
  668. IRQF_TRIGGER_HIGH, DRV_NAME, host)) {
  669. dev_err(&pdev->dev, "Error requesting NAND IRQ\n");
  670. res = -ENXIO;
  671. goto err_exit3;
  672. }
  673. /*
  674. * Fills out all the uninitialized function pointers with the defaults
  675. * And scans for a bad block table if appropriate.
  676. */
  677. if (nand_scan_tail(mtd)) {
  678. res = -ENXIO;
  679. goto err_exit4;
  680. }
  681. mtd->name = DRV_NAME;
  682. res = mtd_device_register(mtd, host->ncfg->parts,
  683. host->ncfg->num_parts);
  684. if (!res)
  685. return res;
  686. nand_release(mtd);
  687. err_exit4:
  688. free_irq(host->irq, host);
  689. err_exit3:
  690. if (use_dma)
  691. dma_release_channel(host->dma_chan);
  692. err_exit2:
  693. clk_disable_unprepare(host->clk);
  694. clk_put(host->clk);
  695. err_exit1:
  696. lpc32xx_wp_enable(host);
  697. gpio_free(host->ncfg->wp_gpio);
  698. return res;
  699. }
  700. /*
  701. * Remove NAND device
  702. */
  703. static int lpc32xx_nand_remove(struct platform_device *pdev)
  704. {
  705. struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
  706. struct mtd_info *mtd = nand_to_mtd(&host->nand_chip);
  707. nand_release(mtd);
  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. gpio_free(host->ncfg->wp_gpio);
  715. return 0;
  716. }
  717. #ifdef CONFIG_PM
  718. static int lpc32xx_nand_resume(struct platform_device *pdev)
  719. {
  720. struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
  721. /* Re-enable NAND clock */
  722. clk_prepare_enable(host->clk);
  723. /* Fresh init of NAND controller */
  724. lpc32xx_nand_setup(host);
  725. /* Disable write protect */
  726. lpc32xx_wp_disable(host);
  727. return 0;
  728. }
  729. static int lpc32xx_nand_suspend(struct platform_device *pdev, pm_message_t pm)
  730. {
  731. struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
  732. /* Enable write protect for safety */
  733. lpc32xx_wp_enable(host);
  734. /* Disable clock */
  735. clk_disable_unprepare(host->clk);
  736. return 0;
  737. }
  738. #else
  739. #define lpc32xx_nand_resume NULL
  740. #define lpc32xx_nand_suspend NULL
  741. #endif
  742. static const struct of_device_id lpc32xx_nand_match[] = {
  743. { .compatible = "nxp,lpc3220-mlc" },
  744. { /* sentinel */ },
  745. };
  746. MODULE_DEVICE_TABLE(of, lpc32xx_nand_match);
  747. static struct platform_driver lpc32xx_nand_driver = {
  748. .probe = lpc32xx_nand_probe,
  749. .remove = lpc32xx_nand_remove,
  750. .resume = lpc32xx_nand_resume,
  751. .suspend = lpc32xx_nand_suspend,
  752. .driver = {
  753. .name = DRV_NAME,
  754. .of_match_table = lpc32xx_nand_match,
  755. },
  756. };
  757. module_platform_driver(lpc32xx_nand_driver);
  758. MODULE_LICENSE("GPL");
  759. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  760. MODULE_DESCRIPTION("NAND driver for the NXP LPC32XX MLC controller");