lpc32xx_slc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. /*
  2. * NXP LPC32XX NAND SLC driver
  3. *
  4. * Authors:
  5. * Kevin Wells <kevin.wells@nxp.com>
  6. * Roland Stigge <stigge@antcom.de>
  7. *
  8. * Copyright © 2011 NXP Semiconductors
  9. * Copyright © 2012 Roland Stigge
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. */
  21. #include <linux/slab.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/mtd/mtd.h>
  25. #include <linux/mtd/nand.h>
  26. #include <linux/mtd/partitions.h>
  27. #include <linux/clk.h>
  28. #include <linux/err.h>
  29. #include <linux/delay.h>
  30. #include <linux/io.h>
  31. #include <linux/mm.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/dmaengine.h>
  34. #include <linux/mtd/nand_ecc.h>
  35. #include <linux/gpio.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. #define LPC32XX_MODNAME "lpc32xx-nand"
  41. /**********************************************************************
  42. * SLC NAND controller register offsets
  43. **********************************************************************/
  44. #define SLC_DATA(x) (x + 0x000)
  45. #define SLC_ADDR(x) (x + 0x004)
  46. #define SLC_CMD(x) (x + 0x008)
  47. #define SLC_STOP(x) (x + 0x00C)
  48. #define SLC_CTRL(x) (x + 0x010)
  49. #define SLC_CFG(x) (x + 0x014)
  50. #define SLC_STAT(x) (x + 0x018)
  51. #define SLC_INT_STAT(x) (x + 0x01C)
  52. #define SLC_IEN(x) (x + 0x020)
  53. #define SLC_ISR(x) (x + 0x024)
  54. #define SLC_ICR(x) (x + 0x028)
  55. #define SLC_TAC(x) (x + 0x02C)
  56. #define SLC_TC(x) (x + 0x030)
  57. #define SLC_ECC(x) (x + 0x034)
  58. #define SLC_DMA_DATA(x) (x + 0x038)
  59. /**********************************************************************
  60. * slc_ctrl register definitions
  61. **********************************************************************/
  62. #define SLCCTRL_SW_RESET (1 << 2) /* Reset the NAND controller bit */
  63. #define SLCCTRL_ECC_CLEAR (1 << 1) /* Reset ECC bit */
  64. #define SLCCTRL_DMA_START (1 << 0) /* Start DMA channel bit */
  65. /**********************************************************************
  66. * slc_cfg register definitions
  67. **********************************************************************/
  68. #define SLCCFG_CE_LOW (1 << 5) /* Force CE low bit */
  69. #define SLCCFG_DMA_ECC (1 << 4) /* Enable DMA ECC bit */
  70. #define SLCCFG_ECC_EN (1 << 3) /* ECC enable bit */
  71. #define SLCCFG_DMA_BURST (1 << 2) /* DMA burst bit */
  72. #define SLCCFG_DMA_DIR (1 << 1) /* DMA write(0)/read(1) bit */
  73. #define SLCCFG_WIDTH (1 << 0) /* External device width, 0=8bit */
  74. /**********************************************************************
  75. * slc_stat register definitions
  76. **********************************************************************/
  77. #define SLCSTAT_DMA_FIFO (1 << 2) /* DMA FIFO has data bit */
  78. #define SLCSTAT_SLC_FIFO (1 << 1) /* SLC FIFO has data bit */
  79. #define SLCSTAT_NAND_READY (1 << 0) /* NAND device is ready bit */
  80. /**********************************************************************
  81. * slc_int_stat, slc_ien, slc_isr, and slc_icr register definitions
  82. **********************************************************************/
  83. #define SLCSTAT_INT_TC (1 << 1) /* Transfer count bit */
  84. #define SLCSTAT_INT_RDY_EN (1 << 0) /* Ready interrupt bit */
  85. /**********************************************************************
  86. * slc_tac register definitions
  87. **********************************************************************/
  88. /* Clock setting for RDY write sample wait time in 2*n clocks */
  89. #define SLCTAC_WDR(n) (((n) & 0xF) << 28)
  90. /* Write pulse width in clock cycles, 1 to 16 clocks */
  91. #define SLCTAC_WWIDTH(n) (((n) & 0xF) << 24)
  92. /* Write hold time of control and data signals, 1 to 16 clocks */
  93. #define SLCTAC_WHOLD(n) (((n) & 0xF) << 20)
  94. /* Write setup time of control and data signals, 1 to 16 clocks */
  95. #define SLCTAC_WSETUP(n) (((n) & 0xF) << 16)
  96. /* Clock setting for RDY read sample wait time in 2*n clocks */
  97. #define SLCTAC_RDR(n) (((n) & 0xF) << 12)
  98. /* Read pulse width in clock cycles, 1 to 16 clocks */
  99. #define SLCTAC_RWIDTH(n) (((n) & 0xF) << 8)
  100. /* Read hold time of control and data signals, 1 to 16 clocks */
  101. #define SLCTAC_RHOLD(n) (((n) & 0xF) << 4)
  102. /* Read setup time of control and data signals, 1 to 16 clocks */
  103. #define SLCTAC_RSETUP(n) (((n) & 0xF) << 0)
  104. /**********************************************************************
  105. * slc_ecc register definitions
  106. **********************************************************************/
  107. /* ECC line party fetch macro */
  108. #define SLCECC_TO_LINEPAR(n) (((n) >> 6) & 0x7FFF)
  109. #define SLCECC_TO_COLPAR(n) ((n) & 0x3F)
  110. /*
  111. * DMA requires storage space for the DMA local buffer and the hardware ECC
  112. * storage area. The DMA local buffer is only used if DMA mapping fails
  113. * during runtime.
  114. */
  115. #define LPC32XX_DMA_DATA_SIZE 4096
  116. #define LPC32XX_ECC_SAVE_SIZE ((4096 / 256) * 4)
  117. /* Number of bytes used for ECC stored in NAND per 256 bytes */
  118. #define LPC32XX_SLC_DEV_ECC_BYTES 3
  119. /*
  120. * If the NAND base clock frequency can't be fetched, this frequency will be
  121. * used instead as the base. This rate is used to setup the timing registers
  122. * used for NAND accesses.
  123. */
  124. #define LPC32XX_DEF_BUS_RATE 133250000
  125. /* Milliseconds for DMA FIFO timeout (unlikely anyway) */
  126. #define LPC32XX_DMA_TIMEOUT 100
  127. /*
  128. * NAND ECC Layout for small page NAND devices
  129. * Note: For large and huge page devices, the default layouts are used
  130. */
  131. static struct nand_ecclayout lpc32xx_nand_oob_16 = {
  132. .eccbytes = 6,
  133. .eccpos = {10, 11, 12, 13, 14, 15},
  134. .oobfree = {
  135. { .offset = 0, .length = 4 },
  136. { .offset = 6, .length = 4 },
  137. },
  138. };
  139. static u8 bbt_pattern[] = {'B', 'b', 't', '0' };
  140. static u8 mirror_pattern[] = {'1', 't', 'b', 'B' };
  141. /*
  142. * Small page FLASH BBT descriptors, marker at offset 0, version at offset 6
  143. * Note: Large page devices used the default layout
  144. */
  145. static struct nand_bbt_descr bbt_smallpage_main_descr = {
  146. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  147. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  148. .offs = 0,
  149. .len = 4,
  150. .veroffs = 6,
  151. .maxblocks = 4,
  152. .pattern = bbt_pattern
  153. };
  154. static struct nand_bbt_descr bbt_smallpage_mirror_descr = {
  155. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  156. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  157. .offs = 0,
  158. .len = 4,
  159. .veroffs = 6,
  160. .maxblocks = 4,
  161. .pattern = mirror_pattern
  162. };
  163. /*
  164. * NAND platform configuration structure
  165. */
  166. struct lpc32xx_nand_cfg_slc {
  167. uint32_t wdr_clks;
  168. uint32_t wwidth;
  169. uint32_t whold;
  170. uint32_t wsetup;
  171. uint32_t rdr_clks;
  172. uint32_t rwidth;
  173. uint32_t rhold;
  174. uint32_t rsetup;
  175. bool use_bbt;
  176. unsigned wp_gpio;
  177. struct mtd_partition *parts;
  178. unsigned num_parts;
  179. };
  180. struct lpc32xx_nand_host {
  181. struct nand_chip nand_chip;
  182. struct clk *clk;
  183. struct mtd_info mtd;
  184. void __iomem *io_base;
  185. struct lpc32xx_nand_cfg_slc *ncfg;
  186. struct completion comp;
  187. struct dma_chan *dma_chan;
  188. uint32_t dma_buf_len;
  189. struct dma_slave_config dma_slave_config;
  190. struct scatterlist sgl;
  191. /*
  192. * DMA and CPU addresses of ECC work area and data buffer
  193. */
  194. uint32_t *ecc_buf;
  195. uint8_t *data_buf;
  196. dma_addr_t io_base_dma;
  197. };
  198. static void lpc32xx_nand_setup(struct lpc32xx_nand_host *host)
  199. {
  200. uint32_t clkrate, tmp;
  201. /* Reset SLC controller */
  202. writel(SLCCTRL_SW_RESET, SLC_CTRL(host->io_base));
  203. udelay(1000);
  204. /* Basic setup */
  205. writel(0, SLC_CFG(host->io_base));
  206. writel(0, SLC_IEN(host->io_base));
  207. writel((SLCSTAT_INT_TC | SLCSTAT_INT_RDY_EN),
  208. SLC_ICR(host->io_base));
  209. /* Get base clock for SLC block */
  210. clkrate = clk_get_rate(host->clk);
  211. if (clkrate == 0)
  212. clkrate = LPC32XX_DEF_BUS_RATE;
  213. /* Compute clock setup values */
  214. tmp = SLCTAC_WDR(host->ncfg->wdr_clks) |
  215. SLCTAC_WWIDTH(1 + (clkrate / host->ncfg->wwidth)) |
  216. SLCTAC_WHOLD(1 + (clkrate / host->ncfg->whold)) |
  217. SLCTAC_WSETUP(1 + (clkrate / host->ncfg->wsetup)) |
  218. SLCTAC_RDR(host->ncfg->rdr_clks) |
  219. SLCTAC_RWIDTH(1 + (clkrate / host->ncfg->rwidth)) |
  220. SLCTAC_RHOLD(1 + (clkrate / host->ncfg->rhold)) |
  221. SLCTAC_RSETUP(1 + (clkrate / host->ncfg->rsetup));
  222. writel(tmp, SLC_TAC(host->io_base));
  223. }
  224. /*
  225. * Hardware specific access to control lines
  226. */
  227. static void lpc32xx_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
  228. unsigned int ctrl)
  229. {
  230. uint32_t tmp;
  231. struct nand_chip *chip = mtd->priv;
  232. struct lpc32xx_nand_host *host = chip->priv;
  233. /* Does CE state need to be changed? */
  234. tmp = readl(SLC_CFG(host->io_base));
  235. if (ctrl & NAND_NCE)
  236. tmp |= SLCCFG_CE_LOW;
  237. else
  238. tmp &= ~SLCCFG_CE_LOW;
  239. writel(tmp, SLC_CFG(host->io_base));
  240. if (cmd != NAND_CMD_NONE) {
  241. if (ctrl & NAND_CLE)
  242. writel(cmd, SLC_CMD(host->io_base));
  243. else
  244. writel(cmd, SLC_ADDR(host->io_base));
  245. }
  246. }
  247. /*
  248. * Read the Device Ready pin
  249. */
  250. static int lpc32xx_nand_device_ready(struct mtd_info *mtd)
  251. {
  252. struct nand_chip *chip = mtd->priv;
  253. struct lpc32xx_nand_host *host = chip->priv;
  254. int rdy = 0;
  255. if ((readl(SLC_STAT(host->io_base)) & SLCSTAT_NAND_READY) != 0)
  256. rdy = 1;
  257. return rdy;
  258. }
  259. /*
  260. * Enable NAND write protect
  261. */
  262. static void lpc32xx_wp_enable(struct lpc32xx_nand_host *host)
  263. {
  264. gpio_set_value(host->ncfg->wp_gpio, 0);
  265. }
  266. /*
  267. * Disable NAND write protect
  268. */
  269. static void lpc32xx_wp_disable(struct lpc32xx_nand_host *host)
  270. {
  271. gpio_set_value(host->ncfg->wp_gpio, 1);
  272. }
  273. /*
  274. * Prepares SLC for transfers with H/W ECC enabled
  275. */
  276. static void lpc32xx_nand_ecc_enable(struct mtd_info *mtd, int mode)
  277. {
  278. /* Hardware ECC is enabled automatically in hardware as needed */
  279. }
  280. /*
  281. * Calculates the ECC for the data
  282. */
  283. static int lpc32xx_nand_ecc_calculate(struct mtd_info *mtd,
  284. const unsigned char *buf,
  285. unsigned char *code)
  286. {
  287. /*
  288. * ECC is calculated automatically in hardware during syndrome read
  289. * and write operations, so it doesn't need to be calculated here.
  290. */
  291. return 0;
  292. }
  293. /*
  294. * Read a single byte from NAND device
  295. */
  296. static uint8_t lpc32xx_nand_read_byte(struct mtd_info *mtd)
  297. {
  298. struct nand_chip *chip = mtd->priv;
  299. struct lpc32xx_nand_host *host = chip->priv;
  300. return (uint8_t)readl(SLC_DATA(host->io_base));
  301. }
  302. /*
  303. * Simple device read without ECC
  304. */
  305. static void lpc32xx_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  306. {
  307. struct nand_chip *chip = mtd->priv;
  308. struct lpc32xx_nand_host *host = chip->priv;
  309. /* Direct device read with no ECC */
  310. while (len-- > 0)
  311. *buf++ = (uint8_t)readl(SLC_DATA(host->io_base));
  312. }
  313. /*
  314. * Simple device write without ECC
  315. */
  316. static void lpc32xx_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  317. {
  318. struct nand_chip *chip = mtd->priv;
  319. struct lpc32xx_nand_host *host = chip->priv;
  320. /* Direct device write with no ECC */
  321. while (len-- > 0)
  322. writel((uint32_t)*buf++, SLC_DATA(host->io_base));
  323. }
  324. /*
  325. * Verify data in buffer to data on device
  326. */
  327. static int lpc32xx_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  328. {
  329. struct nand_chip *chip = mtd->priv;
  330. struct lpc32xx_nand_host *host = chip->priv;
  331. int i;
  332. /* DATA register must be read as 32 bits or it will fail */
  333. for (i = 0; i < len; i++) {
  334. if (buf[i] != (uint8_t)readl(SLC_DATA(host->io_base)))
  335. return -EFAULT;
  336. }
  337. return 0;
  338. }
  339. /*
  340. * Read the OOB data from the device without ECC using FIFO method
  341. */
  342. static int lpc32xx_nand_read_oob_syndrome(struct mtd_info *mtd,
  343. struct nand_chip *chip, int page)
  344. {
  345. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  346. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  347. return 0;
  348. }
  349. /*
  350. * Write the OOB data to the device without ECC using FIFO method
  351. */
  352. static int lpc32xx_nand_write_oob_syndrome(struct mtd_info *mtd,
  353. struct nand_chip *chip, int page)
  354. {
  355. int status;
  356. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  357. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  358. /* Send command to program the OOB data */
  359. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  360. status = chip->waitfunc(mtd, chip);
  361. return status & NAND_STATUS_FAIL ? -EIO : 0;
  362. }
  363. /*
  364. * Fills in the ECC fields in the OOB buffer with the hardware generated ECC
  365. */
  366. static void lpc32xx_slc_ecc_copy(uint8_t *spare, const uint32_t *ecc, int count)
  367. {
  368. int i;
  369. for (i = 0; i < (count * 3); i += 3) {
  370. uint32_t ce = ecc[i / 3];
  371. ce = ~(ce << 2) & 0xFFFFFF;
  372. spare[i + 2] = (uint8_t)(ce & 0xFF);
  373. ce >>= 8;
  374. spare[i + 1] = (uint8_t)(ce & 0xFF);
  375. ce >>= 8;
  376. spare[i] = (uint8_t)(ce & 0xFF);
  377. }
  378. }
  379. static void lpc32xx_dma_complete_func(void *completion)
  380. {
  381. complete(completion);
  382. }
  383. static int lpc32xx_xmit_dma(struct mtd_info *mtd, dma_addr_t dma,
  384. void *mem, int len, enum dma_transfer_direction dir)
  385. {
  386. struct nand_chip *chip = mtd->priv;
  387. struct lpc32xx_nand_host *host = chip->priv;
  388. struct dma_async_tx_descriptor *desc;
  389. int flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
  390. int res;
  391. host->dma_slave_config.direction = dir;
  392. host->dma_slave_config.src_addr = dma;
  393. host->dma_slave_config.dst_addr = dma;
  394. host->dma_slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  395. host->dma_slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  396. host->dma_slave_config.src_maxburst = 4;
  397. host->dma_slave_config.dst_maxburst = 4;
  398. /* DMA controller does flow control: */
  399. host->dma_slave_config.device_fc = false;
  400. if (dmaengine_slave_config(host->dma_chan, &host->dma_slave_config)) {
  401. dev_err(mtd->dev.parent, "Failed to setup DMA slave\n");
  402. return -ENXIO;
  403. }
  404. sg_init_one(&host->sgl, mem, len);
  405. res = dma_map_sg(host->dma_chan->device->dev, &host->sgl, 1,
  406. DMA_BIDIRECTIONAL);
  407. if (res != 1) {
  408. dev_err(mtd->dev.parent, "Failed to map sg list\n");
  409. return -ENXIO;
  410. }
  411. desc = dmaengine_prep_slave_sg(host->dma_chan, &host->sgl, 1, dir,
  412. flags);
  413. if (!desc) {
  414. dev_err(mtd->dev.parent, "Failed to prepare slave sg\n");
  415. goto out1;
  416. }
  417. init_completion(&host->comp);
  418. desc->callback = lpc32xx_dma_complete_func;
  419. desc->callback_param = &host->comp;
  420. dmaengine_submit(desc);
  421. dma_async_issue_pending(host->dma_chan);
  422. wait_for_completion_timeout(&host->comp, msecs_to_jiffies(1000));
  423. dma_unmap_sg(host->dma_chan->device->dev, &host->sgl, 1,
  424. DMA_BIDIRECTIONAL);
  425. return 0;
  426. out1:
  427. dma_unmap_sg(host->dma_chan->device->dev, &host->sgl, 1,
  428. DMA_BIDIRECTIONAL);
  429. return -ENXIO;
  430. }
  431. /*
  432. * DMA read/write transfers with ECC support
  433. */
  434. static int lpc32xx_xfer(struct mtd_info *mtd, uint8_t *buf, int eccsubpages,
  435. int read)
  436. {
  437. struct nand_chip *chip = mtd->priv;
  438. struct lpc32xx_nand_host *host = chip->priv;
  439. int i, status = 0;
  440. unsigned long timeout;
  441. int res;
  442. enum dma_transfer_direction dir =
  443. read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
  444. uint8_t *dma_buf;
  445. bool dma_mapped;
  446. if ((void *)buf <= high_memory) {
  447. dma_buf = buf;
  448. dma_mapped = true;
  449. } else {
  450. dma_buf = host->data_buf;
  451. dma_mapped = false;
  452. if (!read)
  453. memcpy(host->data_buf, buf, mtd->writesize);
  454. }
  455. if (read) {
  456. writel(readl(SLC_CFG(host->io_base)) |
  457. SLCCFG_DMA_DIR | SLCCFG_ECC_EN | SLCCFG_DMA_ECC |
  458. SLCCFG_DMA_BURST, SLC_CFG(host->io_base));
  459. } else {
  460. writel((readl(SLC_CFG(host->io_base)) |
  461. SLCCFG_ECC_EN | SLCCFG_DMA_ECC | SLCCFG_DMA_BURST) &
  462. ~SLCCFG_DMA_DIR,
  463. SLC_CFG(host->io_base));
  464. }
  465. /* Clear initial ECC */
  466. writel(SLCCTRL_ECC_CLEAR, SLC_CTRL(host->io_base));
  467. /* Transfer size is data area only */
  468. writel(mtd->writesize, SLC_TC(host->io_base));
  469. /* Start transfer in the NAND controller */
  470. writel(readl(SLC_CTRL(host->io_base)) | SLCCTRL_DMA_START,
  471. SLC_CTRL(host->io_base));
  472. for (i = 0; i < chip->ecc.steps; i++) {
  473. /* Data */
  474. res = lpc32xx_xmit_dma(mtd, SLC_DMA_DATA(host->io_base_dma),
  475. dma_buf + i * chip->ecc.size,
  476. mtd->writesize / chip->ecc.steps, dir);
  477. if (res)
  478. return res;
  479. /* Always _read_ ECC */
  480. if (i == chip->ecc.steps - 1)
  481. break;
  482. if (!read) /* ECC availability delayed on write */
  483. udelay(10);
  484. res = lpc32xx_xmit_dma(mtd, SLC_ECC(host->io_base_dma),
  485. &host->ecc_buf[i], 4, DMA_DEV_TO_MEM);
  486. if (res)
  487. return res;
  488. }
  489. /*
  490. * According to NXP, the DMA can be finished here, but the NAND
  491. * controller may still have buffered data. After porting to using the
  492. * dmaengine DMA driver (amba-pl080), the condition (DMA_FIFO empty)
  493. * appears to be always true, according to tests. Keeping the check for
  494. * safety reasons for now.
  495. */
  496. if (readl(SLC_STAT(host->io_base)) & SLCSTAT_DMA_FIFO) {
  497. dev_warn(mtd->dev.parent, "FIFO not empty!\n");
  498. timeout = jiffies + msecs_to_jiffies(LPC32XX_DMA_TIMEOUT);
  499. while ((readl(SLC_STAT(host->io_base)) & SLCSTAT_DMA_FIFO) &&
  500. time_before(jiffies, timeout))
  501. cpu_relax();
  502. if (!time_before(jiffies, timeout)) {
  503. dev_err(mtd->dev.parent, "FIFO held data too long\n");
  504. status = -EIO;
  505. }
  506. }
  507. /* Read last calculated ECC value */
  508. if (!read)
  509. udelay(10);
  510. host->ecc_buf[chip->ecc.steps - 1] =
  511. readl(SLC_ECC(host->io_base));
  512. /* Flush DMA */
  513. dmaengine_terminate_all(host->dma_chan);
  514. if (readl(SLC_STAT(host->io_base)) & SLCSTAT_DMA_FIFO ||
  515. readl(SLC_TC(host->io_base))) {
  516. /* Something is left in the FIFO, something is wrong */
  517. dev_err(mtd->dev.parent, "DMA FIFO failure\n");
  518. status = -EIO;
  519. }
  520. /* Stop DMA & HW ECC */
  521. writel(readl(SLC_CTRL(host->io_base)) & ~SLCCTRL_DMA_START,
  522. SLC_CTRL(host->io_base));
  523. writel(readl(SLC_CFG(host->io_base)) &
  524. ~(SLCCFG_DMA_DIR | SLCCFG_ECC_EN | SLCCFG_DMA_ECC |
  525. SLCCFG_DMA_BURST), SLC_CFG(host->io_base));
  526. if (!dma_mapped && read)
  527. memcpy(buf, host->data_buf, mtd->writesize);
  528. return status;
  529. }
  530. /*
  531. * Read the data and OOB data from the device, use ECC correction with the
  532. * data, disable ECC for the OOB data
  533. */
  534. static int lpc32xx_nand_read_page_syndrome(struct mtd_info *mtd,
  535. struct nand_chip *chip, uint8_t *buf,
  536. int oob_required, int page)
  537. {
  538. struct lpc32xx_nand_host *host = chip->priv;
  539. int stat, i, status;
  540. uint8_t *oobecc, tmpecc[LPC32XX_ECC_SAVE_SIZE];
  541. /* Issue read command */
  542. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  543. /* Read data and oob, calculate ECC */
  544. status = lpc32xx_xfer(mtd, buf, chip->ecc.steps, 1);
  545. /* Get OOB data */
  546. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  547. /* Convert to stored ECC format */
  548. lpc32xx_slc_ecc_copy(tmpecc, (uint32_t *) host->ecc_buf, chip->ecc.steps);
  549. /* Pointer to ECC data retrieved from NAND spare area */
  550. oobecc = chip->oob_poi + chip->ecc.layout->eccpos[0];
  551. for (i = 0; i < chip->ecc.steps; i++) {
  552. stat = chip->ecc.correct(mtd, buf, oobecc,
  553. &tmpecc[i * chip->ecc.bytes]);
  554. if (stat < 0)
  555. mtd->ecc_stats.failed++;
  556. else
  557. mtd->ecc_stats.corrected += stat;
  558. buf += chip->ecc.size;
  559. oobecc += chip->ecc.bytes;
  560. }
  561. return status;
  562. }
  563. /*
  564. * Read the data and OOB data from the device, no ECC correction with the
  565. * data or OOB data
  566. */
  567. static int lpc32xx_nand_read_page_raw_syndrome(struct mtd_info *mtd,
  568. struct nand_chip *chip,
  569. uint8_t *buf, int oob_required,
  570. int page)
  571. {
  572. /* Issue read command */
  573. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  574. /* Raw reads can just use the FIFO interface */
  575. chip->read_buf(mtd, buf, chip->ecc.size * chip->ecc.steps);
  576. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  577. return 0;
  578. }
  579. /*
  580. * Write the data and OOB data to the device, use ECC with the data,
  581. * disable ECC for the OOB data
  582. */
  583. static int lpc32xx_nand_write_page_syndrome(struct mtd_info *mtd,
  584. struct nand_chip *chip,
  585. const uint8_t *buf, int oob_required)
  586. {
  587. struct lpc32xx_nand_host *host = chip->priv;
  588. uint8_t *pb = chip->oob_poi + chip->ecc.layout->eccpos[0];
  589. int error;
  590. /* Write data, calculate ECC on outbound data */
  591. error = lpc32xx_xfer(mtd, (uint8_t *)buf, chip->ecc.steps, 0);
  592. if (error)
  593. return error;
  594. /*
  595. * The calculated ECC needs some manual work done to it before
  596. * committing it to NAND. Process the calculated ECC and place
  597. * the resultant values directly into the OOB buffer. */
  598. lpc32xx_slc_ecc_copy(pb, (uint32_t *)host->ecc_buf, chip->ecc.steps);
  599. /* Write ECC data to device */
  600. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  601. return 0;
  602. }
  603. /*
  604. * Write the data and OOB data to the device, no ECC correction with the
  605. * data or OOB data
  606. */
  607. static int lpc32xx_nand_write_page_raw_syndrome(struct mtd_info *mtd,
  608. struct nand_chip *chip,
  609. const uint8_t *buf,
  610. int oob_required)
  611. {
  612. /* Raw writes can just use the FIFO interface */
  613. chip->write_buf(mtd, buf, chip->ecc.size * chip->ecc.steps);
  614. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  615. return 0;
  616. }
  617. static bool lpc32xx_dma_filter(struct dma_chan *chan, void *param)
  618. {
  619. struct pl08x_dma_chan *ch =
  620. container_of(chan, struct pl08x_dma_chan, chan);
  621. /* In LPC32xx's PL080 DMA wiring, the SLC NAND DMA signal is #1 */
  622. if (ch->cd->min_signal == 1)
  623. return true;
  624. return false;
  625. }
  626. static int lpc32xx_nand_dma_setup(struct lpc32xx_nand_host *host)
  627. {
  628. struct mtd_info *mtd = &host->mtd;
  629. dma_cap_mask_t mask;
  630. dma_cap_zero(mask);
  631. dma_cap_set(DMA_SLAVE, mask);
  632. host->dma_chan = dma_request_channel(mask, lpc32xx_dma_filter, NULL);
  633. if (!host->dma_chan) {
  634. dev_err(mtd->dev.parent, "Failed to request DMA channel\n");
  635. return -EBUSY;
  636. }
  637. return 0;
  638. }
  639. #ifdef CONFIG_OF
  640. static struct lpc32xx_nand_cfg_slc *lpc32xx_parse_dt(struct device *dev)
  641. {
  642. struct lpc32xx_nand_cfg_slc *pdata;
  643. struct device_node *np = dev->of_node;
  644. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  645. if (!pdata) {
  646. dev_err(dev, "could not allocate memory for platform data\n");
  647. return NULL;
  648. }
  649. of_property_read_u32(np, "nxp,wdr-clks", &pdata->wdr_clks);
  650. of_property_read_u32(np, "nxp,wwidth", &pdata->wwidth);
  651. of_property_read_u32(np, "nxp,whold", &pdata->whold);
  652. of_property_read_u32(np, "nxp,wsetup", &pdata->wsetup);
  653. of_property_read_u32(np, "nxp,rdr-clks", &pdata->rdr_clks);
  654. of_property_read_u32(np, "nxp,rwidth", &pdata->rwidth);
  655. of_property_read_u32(np, "nxp,rhold", &pdata->rhold);
  656. of_property_read_u32(np, "nxp,rsetup", &pdata->rsetup);
  657. if (!pdata->wdr_clks || !pdata->wwidth || !pdata->whold ||
  658. !pdata->wsetup || !pdata->rdr_clks || !pdata->rwidth ||
  659. !pdata->rhold || !pdata->rsetup) {
  660. dev_err(dev, "chip parameters not specified correctly\n");
  661. return NULL;
  662. }
  663. pdata->use_bbt = of_get_nand_on_flash_bbt(np);
  664. pdata->wp_gpio = of_get_named_gpio_flags(np, "gpios", 0, NULL);
  665. return pdata;
  666. }
  667. #else
  668. static struct lpc32xx_nand_cfg_slc *lpc32xx_parse_dt(struct device *dev)
  669. {
  670. return NULL;
  671. }
  672. #endif
  673. /*
  674. * Probe for NAND controller
  675. */
  676. static int __devinit lpc32xx_nand_probe(struct platform_device *pdev)
  677. {
  678. struct lpc32xx_nand_host *host;
  679. struct mtd_info *mtd;
  680. struct nand_chip *chip;
  681. struct resource *rc;
  682. struct mtd_part_parser_data ppdata = {};
  683. int res;
  684. rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  685. if (rc == NULL) {
  686. dev_err(&pdev->dev, "No memory resource found for device\n");
  687. return -EBUSY;
  688. }
  689. /* Allocate memory for the device structure (and zero it) */
  690. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  691. if (!host) {
  692. dev_err(&pdev->dev, "failed to allocate device structure\n");
  693. return -ENOMEM;
  694. }
  695. host->io_base_dma = rc->start;
  696. host->io_base = devm_request_and_ioremap(&pdev->dev, rc);
  697. if (host->io_base == NULL) {
  698. dev_err(&pdev->dev, "ioremap failed\n");
  699. return -ENOMEM;
  700. }
  701. if (pdev->dev.of_node)
  702. host->ncfg = lpc32xx_parse_dt(&pdev->dev);
  703. else
  704. host->ncfg = pdev->dev.platform_data;
  705. if (!host->ncfg) {
  706. dev_err(&pdev->dev, "Missing platform data\n");
  707. return -ENOENT;
  708. }
  709. if (gpio_request(host->ncfg->wp_gpio, "NAND WP")) {
  710. dev_err(&pdev->dev, "GPIO not available\n");
  711. return -EBUSY;
  712. }
  713. lpc32xx_wp_disable(host);
  714. mtd = &host->mtd;
  715. chip = &host->nand_chip;
  716. chip->priv = host;
  717. mtd->priv = chip;
  718. mtd->owner = THIS_MODULE;
  719. mtd->dev.parent = &pdev->dev;
  720. /* Get NAND clock */
  721. host->clk = clk_get(&pdev->dev, NULL);
  722. if (IS_ERR(host->clk)) {
  723. dev_err(&pdev->dev, "Clock failure\n");
  724. res = -ENOENT;
  725. goto err_exit1;
  726. }
  727. clk_enable(host->clk);
  728. /* Set NAND IO addresses and command/ready functions */
  729. chip->IO_ADDR_R = SLC_DATA(host->io_base);
  730. chip->IO_ADDR_W = SLC_DATA(host->io_base);
  731. chip->cmd_ctrl = lpc32xx_nand_cmd_ctrl;
  732. chip->dev_ready = lpc32xx_nand_device_ready;
  733. chip->chip_delay = 20; /* 20us command delay time */
  734. /* Init NAND controller */
  735. lpc32xx_nand_setup(host);
  736. platform_set_drvdata(pdev, host);
  737. /* NAND callbacks for LPC32xx SLC hardware */
  738. chip->ecc.mode = NAND_ECC_HW_SYNDROME;
  739. chip->read_byte = lpc32xx_nand_read_byte;
  740. chip->read_buf = lpc32xx_nand_read_buf;
  741. chip->write_buf = lpc32xx_nand_write_buf;
  742. chip->ecc.read_page_raw = lpc32xx_nand_read_page_raw_syndrome;
  743. chip->ecc.read_page = lpc32xx_nand_read_page_syndrome;
  744. chip->ecc.write_page_raw = lpc32xx_nand_write_page_raw_syndrome;
  745. chip->ecc.write_page = lpc32xx_nand_write_page_syndrome;
  746. chip->ecc.write_oob = lpc32xx_nand_write_oob_syndrome;
  747. chip->ecc.read_oob = lpc32xx_nand_read_oob_syndrome;
  748. chip->ecc.calculate = lpc32xx_nand_ecc_calculate;
  749. chip->ecc.correct = nand_correct_data;
  750. chip->ecc.strength = 1;
  751. chip->ecc.hwctl = lpc32xx_nand_ecc_enable;
  752. chip->verify_buf = lpc32xx_verify_buf;
  753. /* bitflip_threshold's default is defined as ecc_strength anyway.
  754. * Unfortunately, it is set only later at add_mtd_device(). Meanwhile
  755. * being 0, it causes bad block table scanning errors in
  756. * nand_scan_tail(), so preparing it here already. */
  757. mtd->bitflip_threshold = chip->ecc.strength;
  758. /*
  759. * Allocate a large enough buffer for a single huge page plus
  760. * extra space for the spare area and ECC storage area
  761. */
  762. host->dma_buf_len = LPC32XX_DMA_DATA_SIZE + LPC32XX_ECC_SAVE_SIZE;
  763. host->data_buf = devm_kzalloc(&pdev->dev, host->dma_buf_len,
  764. GFP_KERNEL);
  765. if (host->data_buf == NULL) {
  766. dev_err(&pdev->dev, "Error allocating memory\n");
  767. res = -ENOMEM;
  768. goto err_exit2;
  769. }
  770. res = lpc32xx_nand_dma_setup(host);
  771. if (res) {
  772. res = -EIO;
  773. goto err_exit2;
  774. }
  775. /* Find NAND device */
  776. if (nand_scan_ident(mtd, 1, NULL)) {
  777. res = -ENXIO;
  778. goto err_exit3;
  779. }
  780. /* OOB and ECC CPU and DMA work areas */
  781. host->ecc_buf = (uint32_t *)(host->data_buf + LPC32XX_DMA_DATA_SIZE);
  782. /*
  783. * Small page FLASH has a unique OOB layout, but large and huge
  784. * page FLASH use the standard layout. Small page FLASH uses a
  785. * custom BBT marker layout.
  786. */
  787. if (mtd->writesize <= 512)
  788. chip->ecc.layout = &lpc32xx_nand_oob_16;
  789. /* These sizes remain the same regardless of page size */
  790. chip->ecc.size = 256;
  791. chip->ecc.bytes = LPC32XX_SLC_DEV_ECC_BYTES;
  792. chip->ecc.prepad = chip->ecc.postpad = 0;
  793. /* Avoid extra scan if using BBT, setup BBT support */
  794. if (host->ncfg->use_bbt) {
  795. chip->options |= NAND_SKIP_BBTSCAN;
  796. chip->bbt_options |= NAND_BBT_USE_FLASH;
  797. /*
  798. * Use a custom BBT marker setup for small page FLASH that
  799. * won't interfere with the ECC layout. Large and huge page
  800. * FLASH use the standard layout.
  801. */
  802. if (mtd->writesize <= 512) {
  803. chip->bbt_td = &bbt_smallpage_main_descr;
  804. chip->bbt_md = &bbt_smallpage_mirror_descr;
  805. }
  806. }
  807. /*
  808. * Fills out all the uninitialized function pointers with the defaults
  809. */
  810. if (nand_scan_tail(mtd)) {
  811. res = -ENXIO;
  812. goto err_exit3;
  813. }
  814. /* Standard layout in FLASH for bad block tables */
  815. if (host->ncfg->use_bbt) {
  816. if (nand_default_bbt(mtd) < 0)
  817. dev_err(&pdev->dev,
  818. "Error initializing default bad block tables\n");
  819. }
  820. mtd->name = "nxp_lpc3220_slc";
  821. ppdata.of_node = pdev->dev.of_node;
  822. res = mtd_device_parse_register(mtd, NULL, &ppdata, host->ncfg->parts,
  823. host->ncfg->num_parts);
  824. if (!res)
  825. return res;
  826. nand_release(mtd);
  827. err_exit3:
  828. dma_release_channel(host->dma_chan);
  829. err_exit2:
  830. clk_disable(host->clk);
  831. clk_put(host->clk);
  832. platform_set_drvdata(pdev, NULL);
  833. err_exit1:
  834. lpc32xx_wp_enable(host);
  835. gpio_free(host->ncfg->wp_gpio);
  836. return res;
  837. }
  838. /*
  839. * Remove NAND device.
  840. */
  841. static int __devexit lpc32xx_nand_remove(struct platform_device *pdev)
  842. {
  843. uint32_t tmp;
  844. struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
  845. struct mtd_info *mtd = &host->mtd;
  846. nand_release(mtd);
  847. dma_release_channel(host->dma_chan);
  848. /* Force CE high */
  849. tmp = readl(SLC_CTRL(host->io_base));
  850. tmp &= ~SLCCFG_CE_LOW;
  851. writel(tmp, SLC_CTRL(host->io_base));
  852. clk_disable(host->clk);
  853. clk_put(host->clk);
  854. platform_set_drvdata(pdev, NULL);
  855. lpc32xx_wp_enable(host);
  856. gpio_free(host->ncfg->wp_gpio);
  857. return 0;
  858. }
  859. #ifdef CONFIG_PM
  860. static int lpc32xx_nand_resume(struct platform_device *pdev)
  861. {
  862. struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
  863. /* Re-enable NAND clock */
  864. clk_enable(host->clk);
  865. /* Fresh init of NAND controller */
  866. lpc32xx_nand_setup(host);
  867. /* Disable write protect */
  868. lpc32xx_wp_disable(host);
  869. return 0;
  870. }
  871. static int lpc32xx_nand_suspend(struct platform_device *pdev, pm_message_t pm)
  872. {
  873. uint32_t tmp;
  874. struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
  875. /* Force CE high */
  876. tmp = readl(SLC_CTRL(host->io_base));
  877. tmp &= ~SLCCFG_CE_LOW;
  878. writel(tmp, SLC_CTRL(host->io_base));
  879. /* Enable write protect for safety */
  880. lpc32xx_wp_enable(host);
  881. /* Disable clock */
  882. clk_disable(host->clk);
  883. return 0;
  884. }
  885. #else
  886. #define lpc32xx_nand_resume NULL
  887. #define lpc32xx_nand_suspend NULL
  888. #endif
  889. #if defined(CONFIG_OF)
  890. static const struct of_device_id lpc32xx_nand_match[] = {
  891. { .compatible = "nxp,lpc3220-slc" },
  892. { /* sentinel */ },
  893. };
  894. MODULE_DEVICE_TABLE(of, lpc32xx_nand_match);
  895. #endif
  896. static struct platform_driver lpc32xx_nand_driver = {
  897. .probe = lpc32xx_nand_probe,
  898. .remove = __devexit_p(lpc32xx_nand_remove),
  899. .resume = lpc32xx_nand_resume,
  900. .suspend = lpc32xx_nand_suspend,
  901. .driver = {
  902. .name = LPC32XX_MODNAME,
  903. .owner = THIS_MODULE,
  904. .of_match_table = of_match_ptr(lpc32xx_nand_match),
  905. },
  906. };
  907. module_platform_driver(lpc32xx_nand_driver);
  908. MODULE_LICENSE("GPL");
  909. MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>");
  910. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  911. MODULE_DESCRIPTION("NAND driver for the NXP LPC32XX SLC controller");