lpc32xx_mlc.c 25 KB

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