lpc32xx_mlc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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/amba/pl08x.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 clk *clk;
  159. struct mtd_info mtd;
  160. void __iomem *io_base;
  161. int irq;
  162. struct lpc32xx_nand_cfg_mlc *ncfg;
  163. struct completion comp_nand;
  164. struct completion comp_controller;
  165. uint32_t llptr;
  166. /*
  167. * Physical addresses of ECC buffer, DMA data buffers, OOB data buffer
  168. */
  169. dma_addr_t oob_buf_phy;
  170. /*
  171. * Virtual addresses of ECC buffer, DMA data buffers, OOB data buffer
  172. */
  173. uint8_t *oob_buf;
  174. /* Physical address of DMA base address */
  175. dma_addr_t io_base_phy;
  176. struct completion comp_dma;
  177. struct dma_chan *dma_chan;
  178. struct dma_slave_config dma_slave_config;
  179. struct scatterlist sgl;
  180. uint8_t *dma_buf;
  181. uint8_t *dummy_buf;
  182. int mlcsubpages; /* number of 512bytes-subpages */
  183. };
  184. /*
  185. * Activate/Deactivate DMA Operation:
  186. *
  187. * Using the PL080 DMA Controller for transferring the 512 byte subpages
  188. * instead of doing readl() / writel() in a loop slows it down significantly.
  189. * Measurements via getnstimeofday() upon 512 byte subpage reads reveal:
  190. *
  191. * - readl() of 128 x 32 bits in a loop: ~20us
  192. * - DMA read of 512 bytes (32 bit, 4...128 words bursts): ~60us
  193. * - DMA read of 512 bytes (32 bit, no bursts): ~100us
  194. *
  195. * This applies to the transfer itself. In the DMA case: only the
  196. * wait_for_completion() (DMA setup _not_ included).
  197. *
  198. * Note that the 512 bytes subpage transfer is done directly from/to a
  199. * FIFO/buffer inside the NAND controller. Most of the time (~400-800us for a
  200. * 2048 bytes page) is spent waiting for the NAND IRQ, anyway. (The NAND
  201. * controller transferring data between its internal buffer to/from the NAND
  202. * chip.)
  203. *
  204. * Therefore, using the PL080 DMA is disabled by default, for now.
  205. *
  206. */
  207. static int use_dma;
  208. static void lpc32xx_nand_setup(struct lpc32xx_nand_host *host)
  209. {
  210. uint32_t clkrate, tmp;
  211. /* Reset MLC controller */
  212. writel(MLCCMD_RESET, MLC_CMD(host->io_base));
  213. udelay(1000);
  214. /* Get base clock for MLC block */
  215. clkrate = clk_get_rate(host->clk);
  216. if (clkrate == 0)
  217. clkrate = 104000000;
  218. /* Unlock MLC_ICR
  219. * (among others, will be locked again automatically) */
  220. writew(MLCLOCKPR_MAGIC, MLC_LOCK_PR(host->io_base));
  221. /* Configure MLC Controller: Large Block, 5 Byte Address */
  222. tmp = MLCICR_LARGEBLOCK | MLCICR_LONGADDR;
  223. writel(tmp, MLC_ICR(host->io_base));
  224. /* Unlock MLC_TIME_REG
  225. * (among others, will be locked again automatically) */
  226. writew(MLCLOCKPR_MAGIC, MLC_LOCK_PR(host->io_base));
  227. /* Compute clock setup values, see LPC and NAND manual */
  228. tmp = 0;
  229. tmp |= MLCTIMEREG_TCEA_DELAY(clkrate / host->ncfg->tcea_delay + 1);
  230. tmp |= MLCTIMEREG_BUSY_DELAY(clkrate / host->ncfg->busy_delay + 1);
  231. tmp |= MLCTIMEREG_NAND_TA(clkrate / host->ncfg->nand_ta + 1);
  232. tmp |= MLCTIMEREG_RD_HIGH(clkrate / host->ncfg->rd_high + 1);
  233. tmp |= MLCTIMEREG_RD_LOW(clkrate / host->ncfg->rd_low);
  234. tmp |= MLCTIMEREG_WR_HIGH(clkrate / host->ncfg->wr_high + 1);
  235. tmp |= MLCTIMEREG_WR_LOW(clkrate / host->ncfg->wr_low);
  236. writel(tmp, MLC_TIME_REG(host->io_base));
  237. /* Enable IRQ for CONTROLLER_READY and NAND_READY */
  238. writeb(MLCIRQ_CONTROLLER_READY | MLCIRQ_NAND_READY,
  239. MLC_IRQ_MR(host->io_base));
  240. /* Normal nCE operation: nCE controlled by controller */
  241. writel(MLCCEH_NORMAL, MLC_CEH(host->io_base));
  242. }
  243. /*
  244. * Hardware specific access to control lines
  245. */
  246. static void lpc32xx_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
  247. unsigned int ctrl)
  248. {
  249. struct nand_chip *nand_chip = mtd->priv;
  250. struct lpc32xx_nand_host *host = nand_chip->priv;
  251. if (cmd != NAND_CMD_NONE) {
  252. if (ctrl & NAND_CLE)
  253. writel(cmd, MLC_CMD(host->io_base));
  254. else
  255. writel(cmd, MLC_ADDR(host->io_base));
  256. }
  257. }
  258. /*
  259. * Read Device Ready (NAND device _and_ controller ready)
  260. */
  261. static int lpc32xx_nand_device_ready(struct mtd_info *mtd)
  262. {
  263. struct nand_chip *nand_chip = mtd->priv;
  264. struct lpc32xx_nand_host *host = nand_chip->priv;
  265. if ((readb(MLC_ISR(host->io_base)) &
  266. (MLCISR_CONTROLLER_READY | MLCISR_NAND_READY)) ==
  267. (MLCISR_CONTROLLER_READY | MLCISR_NAND_READY))
  268. return 1;
  269. return 0;
  270. }
  271. static irqreturn_t lpc3xxx_nand_irq(int irq, struct lpc32xx_nand_host *host)
  272. {
  273. uint8_t sr;
  274. /* Clear interrupt flag by reading status */
  275. sr = readb(MLC_IRQ_SR(host->io_base));
  276. if (sr & MLCIRQ_NAND_READY)
  277. complete(&host->comp_nand);
  278. if (sr & MLCIRQ_CONTROLLER_READY)
  279. complete(&host->comp_controller);
  280. return IRQ_HANDLED;
  281. }
  282. static int lpc32xx_waitfunc_nand(struct mtd_info *mtd, struct nand_chip *chip)
  283. {
  284. struct lpc32xx_nand_host *host = chip->priv;
  285. if (readb(MLC_ISR(host->io_base)) & MLCISR_NAND_READY)
  286. goto exit;
  287. wait_for_completion(&host->comp_nand);
  288. while (!(readb(MLC_ISR(host->io_base)) & MLCISR_NAND_READY)) {
  289. /* Seems to be delayed sometimes by controller */
  290. dev_dbg(&mtd->dev, "Warning: NAND not ready.\n");
  291. cpu_relax();
  292. }
  293. exit:
  294. return NAND_STATUS_READY;
  295. }
  296. static int lpc32xx_waitfunc_controller(struct mtd_info *mtd,
  297. struct nand_chip *chip)
  298. {
  299. struct lpc32xx_nand_host *host = chip->priv;
  300. if (readb(MLC_ISR(host->io_base)) & MLCISR_CONTROLLER_READY)
  301. goto exit;
  302. wait_for_completion(&host->comp_controller);
  303. while (!(readb(MLC_ISR(host->io_base)) &
  304. MLCISR_CONTROLLER_READY)) {
  305. dev_dbg(&mtd->dev, "Warning: Controller not ready.\n");
  306. cpu_relax();
  307. }
  308. exit:
  309. return NAND_STATUS_READY;
  310. }
  311. static int lpc32xx_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
  312. {
  313. lpc32xx_waitfunc_nand(mtd, chip);
  314. lpc32xx_waitfunc_controller(mtd, chip);
  315. return NAND_STATUS_READY;
  316. }
  317. /*
  318. * Enable NAND write protect
  319. */
  320. static void lpc32xx_wp_enable(struct lpc32xx_nand_host *host)
  321. {
  322. if (gpio_is_valid(host->ncfg->wp_gpio))
  323. gpio_set_value(host->ncfg->wp_gpio, 0);
  324. }
  325. /*
  326. * Disable NAND write protect
  327. */
  328. static void lpc32xx_wp_disable(struct lpc32xx_nand_host *host)
  329. {
  330. if (gpio_is_valid(host->ncfg->wp_gpio))
  331. gpio_set_value(host->ncfg->wp_gpio, 1);
  332. }
  333. static void lpc32xx_dma_complete_func(void *completion)
  334. {
  335. complete(completion);
  336. }
  337. static int lpc32xx_xmit_dma(struct mtd_info *mtd, void *mem, int len,
  338. enum dma_transfer_direction dir)
  339. {
  340. struct nand_chip *chip = mtd->priv;
  341. struct lpc32xx_nand_host *host = chip->priv;
  342. struct dma_async_tx_descriptor *desc;
  343. int flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
  344. int res;
  345. sg_init_one(&host->sgl, mem, len);
  346. res = dma_map_sg(host->dma_chan->device->dev, &host->sgl, 1,
  347. DMA_BIDIRECTIONAL);
  348. if (res != 1) {
  349. dev_err(mtd->dev.parent, "Failed to map sg list\n");
  350. return -ENXIO;
  351. }
  352. desc = dmaengine_prep_slave_sg(host->dma_chan, &host->sgl, 1, dir,
  353. flags);
  354. if (!desc) {
  355. dev_err(mtd->dev.parent, "Failed to prepare slave sg\n");
  356. goto out1;
  357. }
  358. init_completion(&host->comp_dma);
  359. desc->callback = lpc32xx_dma_complete_func;
  360. desc->callback_param = &host->comp_dma;
  361. dmaengine_submit(desc);
  362. dma_async_issue_pending(host->dma_chan);
  363. wait_for_completion_timeout(&host->comp_dma, msecs_to_jiffies(1000));
  364. dma_unmap_sg(host->dma_chan->device->dev, &host->sgl, 1,
  365. DMA_BIDIRECTIONAL);
  366. return 0;
  367. out1:
  368. dma_unmap_sg(host->dma_chan->device->dev, &host->sgl, 1,
  369. DMA_BIDIRECTIONAL);
  370. return -ENXIO;
  371. }
  372. static int lpc32xx_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  373. uint8_t *buf, int oob_required, int page)
  374. {
  375. struct lpc32xx_nand_host *host = chip->priv;
  376. int i, j;
  377. uint8_t *oobbuf = chip->oob_poi;
  378. uint32_t mlc_isr;
  379. int res;
  380. uint8_t *dma_buf;
  381. bool dma_mapped;
  382. if ((void *)buf <= high_memory) {
  383. dma_buf = buf;
  384. dma_mapped = true;
  385. } else {
  386. dma_buf = host->dma_buf;
  387. dma_mapped = false;
  388. }
  389. /* Writing Command and Address */
  390. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  391. /* For all sub-pages */
  392. for (i = 0; i < host->mlcsubpages; i++) {
  393. /* Start Auto Decode Command */
  394. writeb(0x00, MLC_ECC_AUTO_DEC_REG(host->io_base));
  395. /* Wait for Controller Ready */
  396. lpc32xx_waitfunc_controller(mtd, chip);
  397. /* Check ECC Error status */
  398. mlc_isr = readl(MLC_ISR(host->io_base));
  399. if (mlc_isr & MLCISR_DECODER_FAILURE) {
  400. mtd->ecc_stats.failed++;
  401. dev_warn(&mtd->dev, "%s: DECODER_FAILURE\n", __func__);
  402. } else if (mlc_isr & MLCISR_ERRORS_DETECTED) {
  403. mtd->ecc_stats.corrected += ((mlc_isr >> 4) & 0x3) + 1;
  404. }
  405. /* Read 512 + 16 Bytes */
  406. if (use_dma) {
  407. res = lpc32xx_xmit_dma(mtd, dma_buf + i * 512, 512,
  408. DMA_DEV_TO_MEM);
  409. if (res)
  410. return res;
  411. } else {
  412. for (j = 0; j < (512 >> 2); j++) {
  413. *((uint32_t *)(buf)) =
  414. readl(MLC_BUFF(host->io_base));
  415. buf += 4;
  416. }
  417. }
  418. for (j = 0; j < (16 >> 2); j++) {
  419. *((uint32_t *)(oobbuf)) =
  420. readl(MLC_BUFF(host->io_base));
  421. oobbuf += 4;
  422. }
  423. }
  424. if (use_dma && !dma_mapped)
  425. memcpy(buf, dma_buf, mtd->writesize);
  426. return 0;
  427. }
  428. static int lpc32xx_write_page_lowlevel(struct mtd_info *mtd,
  429. struct nand_chip *chip,
  430. const uint8_t *buf, int oob_required)
  431. {
  432. struct lpc32xx_nand_host *host = chip->priv;
  433. const uint8_t *oobbuf = chip->oob_poi;
  434. uint8_t *dma_buf = (uint8_t *)buf;
  435. int res;
  436. int i, j;
  437. if (use_dma && (void *)buf >= high_memory) {
  438. dma_buf = host->dma_buf;
  439. memcpy(dma_buf, buf, mtd->writesize);
  440. }
  441. for (i = 0; i < host->mlcsubpages; i++) {
  442. /* Start Encode */
  443. writeb(0x00, MLC_ECC_ENC_REG(host->io_base));
  444. /* Write 512 + 6 Bytes to Buffer */
  445. if (use_dma) {
  446. res = lpc32xx_xmit_dma(mtd, dma_buf + i * 512, 512,
  447. DMA_MEM_TO_DEV);
  448. if (res)
  449. return res;
  450. } else {
  451. for (j = 0; j < (512 >> 2); j++) {
  452. writel(*((uint32_t *)(buf)),
  453. MLC_BUFF(host->io_base));
  454. buf += 4;
  455. }
  456. }
  457. writel(*((uint32_t *)(oobbuf)), MLC_BUFF(host->io_base));
  458. oobbuf += 4;
  459. writew(*((uint16_t *)(oobbuf)), MLC_BUFF(host->io_base));
  460. oobbuf += 12;
  461. /* Auto Encode w/ Bit 8 = 0 (see LPC MLC Controller manual) */
  462. writeb(0x00, MLC_ECC_AUTO_ENC_REG(host->io_base));
  463. /* Wait for Controller Ready */
  464. lpc32xx_waitfunc_controller(mtd, chip);
  465. }
  466. return 0;
  467. }
  468. static int lpc32xx_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  469. const uint8_t *buf, int oob_required, int page,
  470. int cached, int raw)
  471. {
  472. int res;
  473. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  474. res = lpc32xx_write_page_lowlevel(mtd, chip, buf, oob_required);
  475. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  476. lpc32xx_waitfunc(mtd, chip);
  477. return res;
  478. }
  479. static int lpc32xx_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  480. int page)
  481. {
  482. struct lpc32xx_nand_host *host = chip->priv;
  483. /* Read whole page - necessary with MLC controller! */
  484. lpc32xx_read_page(mtd, chip, host->dummy_buf, 1, page);
  485. return 0;
  486. }
  487. static int lpc32xx_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  488. int page)
  489. {
  490. /* None, write_oob conflicts with the automatic LPC MLC ECC decoder! */
  491. return 0;
  492. }
  493. /* Prepares MLC for transfers with H/W ECC enabled: always enabled anyway */
  494. static void lpc32xx_ecc_enable(struct mtd_info *mtd, int mode)
  495. {
  496. /* Always enabled! */
  497. }
  498. static int lpc32xx_dma_setup(struct lpc32xx_nand_host *host)
  499. {
  500. struct mtd_info *mtd = &host->mtd;
  501. dma_cap_mask_t mask;
  502. dma_cap_zero(mask);
  503. dma_cap_set(DMA_SLAVE, mask);
  504. host->dma_chan = dma_request_channel(mask, pl08x_filter_id, "nand-mlc");
  505. if (!host->dma_chan) {
  506. dev_err(mtd->dev.parent, "Failed to request DMA channel\n");
  507. return -EBUSY;
  508. }
  509. /*
  510. * Set direction to a sensible value even if the dmaengine driver
  511. * should ignore it. With the default (DMA_MEM_TO_MEM), the amba-pl08x
  512. * driver criticizes it as "alien transfer direction".
  513. */
  514. host->dma_slave_config.direction = DMA_DEV_TO_MEM;
  515. host->dma_slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  516. host->dma_slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  517. host->dma_slave_config.src_maxburst = 128;
  518. host->dma_slave_config.dst_maxburst = 128;
  519. /* DMA controller does flow control: */
  520. host->dma_slave_config.device_fc = false;
  521. host->dma_slave_config.src_addr = MLC_BUFF(host->io_base_phy);
  522. host->dma_slave_config.dst_addr = MLC_BUFF(host->io_base_phy);
  523. if (dmaengine_slave_config(host->dma_chan, &host->dma_slave_config)) {
  524. dev_err(mtd->dev.parent, "Failed to setup DMA slave\n");
  525. goto out1;
  526. }
  527. return 0;
  528. out1:
  529. dma_release_channel(host->dma_chan);
  530. return -ENXIO;
  531. }
  532. #ifdef CONFIG_OF
  533. static struct lpc32xx_nand_cfg_mlc *lpc32xx_parse_dt(struct device *dev)
  534. {
  535. struct lpc32xx_nand_cfg_mlc *pdata;
  536. struct device_node *np = dev->of_node;
  537. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  538. if (!pdata) {
  539. dev_err(dev, "could not allocate memory for platform data\n");
  540. return NULL;
  541. }
  542. of_property_read_u32(np, "nxp,tcea-delay", &pdata->tcea_delay);
  543. of_property_read_u32(np, "nxp,busy-delay", &pdata->busy_delay);
  544. of_property_read_u32(np, "nxp,nand-ta", &pdata->nand_ta);
  545. of_property_read_u32(np, "nxp,rd-high", &pdata->rd_high);
  546. of_property_read_u32(np, "nxp,rd-low", &pdata->rd_low);
  547. of_property_read_u32(np, "nxp,wr-high", &pdata->wr_high);
  548. of_property_read_u32(np, "nxp,wr-low", &pdata->wr_low);
  549. if (!pdata->tcea_delay || !pdata->busy_delay || !pdata->nand_ta ||
  550. !pdata->rd_high || !pdata->rd_low || !pdata->wr_high ||
  551. !pdata->wr_low) {
  552. dev_err(dev, "chip parameters not specified correctly\n");
  553. return NULL;
  554. }
  555. pdata->wp_gpio = of_get_named_gpio(np, "gpios", 0);
  556. return pdata;
  557. }
  558. #else
  559. static struct lpc32xx_nand_cfg_mlc *lpc32xx_parse_dt(struct device *dev)
  560. {
  561. return NULL;
  562. }
  563. #endif
  564. /*
  565. * Probe for NAND controller
  566. */
  567. static int __devinit lpc32xx_nand_probe(struct platform_device *pdev)
  568. {
  569. struct lpc32xx_nand_host *host;
  570. struct mtd_info *mtd;
  571. struct nand_chip *nand_chip;
  572. struct resource *rc;
  573. int res;
  574. struct mtd_part_parser_data ppdata = {};
  575. /* Allocate memory for the device structure (and zero it) */
  576. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  577. if (!host) {
  578. dev_err(&pdev->dev, "failed to allocate device structure.\n");
  579. return -ENOMEM;
  580. }
  581. rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  582. if (rc == NULL) {
  583. dev_err(&pdev->dev, "No memory resource found for device!\r\n");
  584. return -ENXIO;
  585. }
  586. host->io_base = devm_request_and_ioremap(&pdev->dev, rc);
  587. if (host->io_base == NULL) {
  588. dev_err(&pdev->dev, "ioremap failed\n");
  589. return -EIO;
  590. }
  591. host->io_base_phy = rc->start;
  592. mtd = &host->mtd;
  593. nand_chip = &host->nand_chip;
  594. if (pdev->dev.of_node)
  595. host->ncfg = lpc32xx_parse_dt(&pdev->dev);
  596. else
  597. host->ncfg = pdev->dev.platform_data;
  598. if (!host->ncfg) {
  599. dev_err(&pdev->dev, "Missing platform data\n");
  600. return -ENOENT;
  601. }
  602. if (host->ncfg->wp_gpio == -EPROBE_DEFER)
  603. return -EPROBE_DEFER;
  604. if (gpio_is_valid(host->ncfg->wp_gpio) &&
  605. gpio_request(host->ncfg->wp_gpio, "NAND WP")) {
  606. dev_err(&pdev->dev, "GPIO not available\n");
  607. return -EBUSY;
  608. }
  609. lpc32xx_wp_disable(host);
  610. nand_chip->priv = host; /* link the private data structures */
  611. mtd->priv = nand_chip;
  612. mtd->owner = THIS_MODULE;
  613. mtd->dev.parent = &pdev->dev;
  614. /* Get NAND clock */
  615. host->clk = clk_get(&pdev->dev, NULL);
  616. if (IS_ERR(host->clk)) {
  617. dev_err(&pdev->dev, "Clock initialization failure\n");
  618. res = -ENOENT;
  619. goto err_exit1;
  620. }
  621. clk_enable(host->clk);
  622. nand_chip->cmd_ctrl = lpc32xx_nand_cmd_ctrl;
  623. nand_chip->dev_ready = lpc32xx_nand_device_ready;
  624. nand_chip->chip_delay = 25; /* us */
  625. nand_chip->IO_ADDR_R = MLC_DATA(host->io_base);
  626. nand_chip->IO_ADDR_W = MLC_DATA(host->io_base);
  627. /* Init NAND controller */
  628. lpc32xx_nand_setup(host);
  629. platform_set_drvdata(pdev, host);
  630. /* Initialize function pointers */
  631. nand_chip->ecc.hwctl = lpc32xx_ecc_enable;
  632. nand_chip->ecc.read_page_raw = lpc32xx_read_page;
  633. nand_chip->ecc.read_page = lpc32xx_read_page;
  634. nand_chip->ecc.write_page_raw = lpc32xx_write_page_lowlevel;
  635. nand_chip->ecc.write_page = lpc32xx_write_page_lowlevel;
  636. nand_chip->ecc.write_oob = lpc32xx_write_oob;
  637. nand_chip->ecc.read_oob = lpc32xx_read_oob;
  638. nand_chip->ecc.strength = 4;
  639. nand_chip->write_page = lpc32xx_write_page;
  640. nand_chip->waitfunc = lpc32xx_waitfunc;
  641. nand_chip->bbt_options = NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
  642. nand_chip->bbt_td = &lpc32xx_nand_bbt;
  643. nand_chip->bbt_md = &lpc32xx_nand_bbt_mirror;
  644. /* bitflip_threshold's default is defined as ecc_strength anyway.
  645. * Unfortunately, it is set only later at add_mtd_device(). Meanwhile
  646. * being 0, it causes bad block table scanning errors in
  647. * nand_scan_tail(), so preparing it here. */
  648. mtd->bitflip_threshold = nand_chip->ecc.strength;
  649. if (use_dma) {
  650. res = lpc32xx_dma_setup(host);
  651. if (res) {
  652. res = -EIO;
  653. goto err_exit2;
  654. }
  655. }
  656. /*
  657. * Scan to find existance of the device and
  658. * Get the type of NAND device SMALL block or LARGE block
  659. */
  660. if (nand_scan_ident(mtd, 1, NULL)) {
  661. res = -ENXIO;
  662. goto err_exit3;
  663. }
  664. host->dma_buf = devm_kzalloc(&pdev->dev, mtd->writesize, GFP_KERNEL);
  665. if (!host->dma_buf) {
  666. dev_err(&pdev->dev, "Error allocating dma_buf memory\n");
  667. res = -ENOMEM;
  668. goto err_exit3;
  669. }
  670. host->dummy_buf = devm_kzalloc(&pdev->dev, mtd->writesize, GFP_KERNEL);
  671. if (!host->dummy_buf) {
  672. dev_err(&pdev->dev, "Error allocating dummy_buf memory\n");
  673. res = -ENOMEM;
  674. goto err_exit3;
  675. }
  676. nand_chip->ecc.mode = NAND_ECC_HW;
  677. nand_chip->ecc.size = mtd->writesize;
  678. nand_chip->ecc.layout = &lpc32xx_nand_oob;
  679. host->mlcsubpages = mtd->writesize / 512;
  680. /* initially clear interrupt status */
  681. readb(MLC_IRQ_SR(host->io_base));
  682. init_completion(&host->comp_nand);
  683. init_completion(&host->comp_controller);
  684. host->irq = platform_get_irq(pdev, 0);
  685. if ((host->irq < 0) || (host->irq >= NR_IRQS)) {
  686. dev_err(&pdev->dev, "failed to get platform irq\n");
  687. res = -EINVAL;
  688. goto err_exit3;
  689. }
  690. if (request_irq(host->irq, (irq_handler_t)&lpc3xxx_nand_irq,
  691. IRQF_TRIGGER_HIGH, DRV_NAME, host)) {
  692. dev_err(&pdev->dev, "Error requesting NAND IRQ\n");
  693. res = -ENXIO;
  694. goto err_exit3;
  695. }
  696. /*
  697. * Fills out all the uninitialized function pointers with the defaults
  698. * And scans for a bad block table if appropriate.
  699. */
  700. if (nand_scan_tail(mtd)) {
  701. res = -ENXIO;
  702. goto err_exit4;
  703. }
  704. mtd->name = DRV_NAME;
  705. ppdata.of_node = pdev->dev.of_node;
  706. res = mtd_device_parse_register(mtd, NULL, &ppdata, host->ncfg->parts,
  707. host->ncfg->num_parts);
  708. if (!res)
  709. return res;
  710. nand_release(mtd);
  711. err_exit4:
  712. free_irq(host->irq, host);
  713. err_exit3:
  714. if (use_dma)
  715. dma_release_channel(host->dma_chan);
  716. err_exit2:
  717. clk_disable(host->clk);
  718. clk_put(host->clk);
  719. platform_set_drvdata(pdev, NULL);
  720. err_exit1:
  721. lpc32xx_wp_enable(host);
  722. gpio_free(host->ncfg->wp_gpio);
  723. return res;
  724. }
  725. /*
  726. * Remove NAND device
  727. */
  728. static int __devexit lpc32xx_nand_remove(struct platform_device *pdev)
  729. {
  730. struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
  731. struct mtd_info *mtd = &host->mtd;
  732. nand_release(mtd);
  733. free_irq(host->irq, host);
  734. if (use_dma)
  735. dma_release_channel(host->dma_chan);
  736. clk_disable(host->clk);
  737. clk_put(host->clk);
  738. platform_set_drvdata(pdev, NULL);
  739. lpc32xx_wp_enable(host);
  740. gpio_free(host->ncfg->wp_gpio);
  741. return 0;
  742. }
  743. #ifdef CONFIG_PM
  744. static int lpc32xx_nand_resume(struct platform_device *pdev)
  745. {
  746. struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
  747. /* Re-enable NAND clock */
  748. clk_enable(host->clk);
  749. /* Fresh init of NAND controller */
  750. lpc32xx_nand_setup(host);
  751. /* Disable write protect */
  752. lpc32xx_wp_disable(host);
  753. return 0;
  754. }
  755. static int lpc32xx_nand_suspend(struct platform_device *pdev, pm_message_t pm)
  756. {
  757. struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
  758. /* Enable write protect for safety */
  759. lpc32xx_wp_enable(host);
  760. /* Disable clock */
  761. clk_disable(host->clk);
  762. return 0;
  763. }
  764. #else
  765. #define lpc32xx_nand_resume NULL
  766. #define lpc32xx_nand_suspend NULL
  767. #endif
  768. #if defined(CONFIG_OF)
  769. static const struct of_device_id lpc32xx_nand_match[] = {
  770. { .compatible = "nxp,lpc3220-mlc" },
  771. { /* sentinel */ },
  772. };
  773. MODULE_DEVICE_TABLE(of, lpc32xx_nand_match);
  774. #endif
  775. static struct platform_driver lpc32xx_nand_driver = {
  776. .probe = lpc32xx_nand_probe,
  777. .remove = __devexit_p(lpc32xx_nand_remove),
  778. .resume = lpc32xx_nand_resume,
  779. .suspend = lpc32xx_nand_suspend,
  780. .driver = {
  781. .name = DRV_NAME,
  782. .owner = THIS_MODULE,
  783. .of_match_table = of_match_ptr(lpc32xx_nand_match),
  784. },
  785. };
  786. module_platform_driver(lpc32xx_nand_driver);
  787. MODULE_LICENSE("GPL");
  788. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  789. MODULE_DESCRIPTION("NAND driver for the NXP LPC32XX MLC controller");