lpc32xx_slc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. int 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. if (gpio_is_valid(host->ncfg->wp_gpio))
  265. gpio_set_value(host->ncfg->wp_gpio, 0);
  266. }
  267. /*
  268. * Disable NAND write protect
  269. */
  270. static void lpc32xx_wp_disable(struct lpc32xx_nand_host *host)
  271. {
  272. if (gpio_is_valid(host->ncfg->wp_gpio))
  273. gpio_set_value(host->ncfg->wp_gpio, 1);
  274. }
  275. /*
  276. * Prepares SLC for transfers with H/W ECC enabled
  277. */
  278. static void lpc32xx_nand_ecc_enable(struct mtd_info *mtd, int mode)
  279. {
  280. /* Hardware ECC is enabled automatically in hardware as needed */
  281. }
  282. /*
  283. * Calculates the ECC for the data
  284. */
  285. static int lpc32xx_nand_ecc_calculate(struct mtd_info *mtd,
  286. const unsigned char *buf,
  287. unsigned char *code)
  288. {
  289. /*
  290. * ECC is calculated automatically in hardware during syndrome read
  291. * and write operations, so it doesn't need to be calculated here.
  292. */
  293. return 0;
  294. }
  295. /*
  296. * Read a single byte from NAND device
  297. */
  298. static uint8_t lpc32xx_nand_read_byte(struct mtd_info *mtd)
  299. {
  300. struct nand_chip *chip = mtd->priv;
  301. struct lpc32xx_nand_host *host = chip->priv;
  302. return (uint8_t)readl(SLC_DATA(host->io_base));
  303. }
  304. /*
  305. * Simple device read without ECC
  306. */
  307. static void lpc32xx_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  308. {
  309. struct nand_chip *chip = mtd->priv;
  310. struct lpc32xx_nand_host *host = chip->priv;
  311. /* Direct device read with no ECC */
  312. while (len-- > 0)
  313. *buf++ = (uint8_t)readl(SLC_DATA(host->io_base));
  314. }
  315. /*
  316. * Simple device write without ECC
  317. */
  318. static void lpc32xx_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  319. {
  320. struct nand_chip *chip = mtd->priv;
  321. struct lpc32xx_nand_host *host = chip->priv;
  322. /* Direct device write with no ECC */
  323. while (len-- > 0)
  324. writel((uint32_t)*buf++, SLC_DATA(host->io_base));
  325. }
  326. /*
  327. * Verify data in buffer to data on device
  328. */
  329. static int lpc32xx_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  330. {
  331. struct nand_chip *chip = mtd->priv;
  332. struct lpc32xx_nand_host *host = chip->priv;
  333. int i;
  334. /* DATA register must be read as 32 bits or it will fail */
  335. for (i = 0; i < len; i++) {
  336. if (buf[i] != (uint8_t)readl(SLC_DATA(host->io_base)))
  337. return -EFAULT;
  338. }
  339. return 0;
  340. }
  341. /*
  342. * Read the OOB data from the device without ECC using FIFO method
  343. */
  344. static int lpc32xx_nand_read_oob_syndrome(struct mtd_info *mtd,
  345. struct nand_chip *chip, int page)
  346. {
  347. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  348. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  349. return 0;
  350. }
  351. /*
  352. * Write the OOB data to the device without ECC using FIFO method
  353. */
  354. static int lpc32xx_nand_write_oob_syndrome(struct mtd_info *mtd,
  355. struct nand_chip *chip, int page)
  356. {
  357. int status;
  358. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  359. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  360. /* Send command to program the OOB data */
  361. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  362. status = chip->waitfunc(mtd, chip);
  363. return status & NAND_STATUS_FAIL ? -EIO : 0;
  364. }
  365. /*
  366. * Fills in the ECC fields in the OOB buffer with the hardware generated ECC
  367. */
  368. static void lpc32xx_slc_ecc_copy(uint8_t *spare, const uint32_t *ecc, int count)
  369. {
  370. int i;
  371. for (i = 0; i < (count * 3); i += 3) {
  372. uint32_t ce = ecc[i / 3];
  373. ce = ~(ce << 2) & 0xFFFFFF;
  374. spare[i + 2] = (uint8_t)(ce & 0xFF);
  375. ce >>= 8;
  376. spare[i + 1] = (uint8_t)(ce & 0xFF);
  377. ce >>= 8;
  378. spare[i] = (uint8_t)(ce & 0xFF);
  379. }
  380. }
  381. static void lpc32xx_dma_complete_func(void *completion)
  382. {
  383. complete(completion);
  384. }
  385. static int lpc32xx_xmit_dma(struct mtd_info *mtd, dma_addr_t dma,
  386. void *mem, int len, enum dma_transfer_direction dir)
  387. {
  388. struct nand_chip *chip = mtd->priv;
  389. struct lpc32xx_nand_host *host = chip->priv;
  390. struct dma_async_tx_descriptor *desc;
  391. int flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
  392. int res;
  393. host->dma_slave_config.direction = dir;
  394. host->dma_slave_config.src_addr = dma;
  395. host->dma_slave_config.dst_addr = dma;
  396. host->dma_slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  397. host->dma_slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  398. host->dma_slave_config.src_maxburst = 4;
  399. host->dma_slave_config.dst_maxburst = 4;
  400. /* DMA controller does flow control: */
  401. host->dma_slave_config.device_fc = false;
  402. if (dmaengine_slave_config(host->dma_chan, &host->dma_slave_config)) {
  403. dev_err(mtd->dev.parent, "Failed to setup DMA slave\n");
  404. return -ENXIO;
  405. }
  406. sg_init_one(&host->sgl, mem, len);
  407. res = dma_map_sg(host->dma_chan->device->dev, &host->sgl, 1,
  408. DMA_BIDIRECTIONAL);
  409. if (res != 1) {
  410. dev_err(mtd->dev.parent, "Failed to map sg list\n");
  411. return -ENXIO;
  412. }
  413. desc = dmaengine_prep_slave_sg(host->dma_chan, &host->sgl, 1, dir,
  414. flags);
  415. if (!desc) {
  416. dev_err(mtd->dev.parent, "Failed to prepare slave sg\n");
  417. goto out1;
  418. }
  419. init_completion(&host->comp);
  420. desc->callback = lpc32xx_dma_complete_func;
  421. desc->callback_param = &host->comp;
  422. dmaengine_submit(desc);
  423. dma_async_issue_pending(host->dma_chan);
  424. wait_for_completion_timeout(&host->comp, msecs_to_jiffies(1000));
  425. dma_unmap_sg(host->dma_chan->device->dev, &host->sgl, 1,
  426. DMA_BIDIRECTIONAL);
  427. return 0;
  428. out1:
  429. dma_unmap_sg(host->dma_chan->device->dev, &host->sgl, 1,
  430. DMA_BIDIRECTIONAL);
  431. return -ENXIO;
  432. }
  433. /*
  434. * DMA read/write transfers with ECC support
  435. */
  436. static int lpc32xx_xfer(struct mtd_info *mtd, uint8_t *buf, int eccsubpages,
  437. int read)
  438. {
  439. struct nand_chip *chip = mtd->priv;
  440. struct lpc32xx_nand_host *host = chip->priv;
  441. int i, status = 0;
  442. unsigned long timeout;
  443. int res;
  444. enum dma_transfer_direction dir =
  445. read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
  446. uint8_t *dma_buf;
  447. bool dma_mapped;
  448. if ((void *)buf <= high_memory) {
  449. dma_buf = buf;
  450. dma_mapped = true;
  451. } else {
  452. dma_buf = host->data_buf;
  453. dma_mapped = false;
  454. if (!read)
  455. memcpy(host->data_buf, buf, mtd->writesize);
  456. }
  457. if (read) {
  458. writel(readl(SLC_CFG(host->io_base)) |
  459. SLCCFG_DMA_DIR | SLCCFG_ECC_EN | SLCCFG_DMA_ECC |
  460. SLCCFG_DMA_BURST, SLC_CFG(host->io_base));
  461. } else {
  462. writel((readl(SLC_CFG(host->io_base)) |
  463. SLCCFG_ECC_EN | SLCCFG_DMA_ECC | SLCCFG_DMA_BURST) &
  464. ~SLCCFG_DMA_DIR,
  465. SLC_CFG(host->io_base));
  466. }
  467. /* Clear initial ECC */
  468. writel(SLCCTRL_ECC_CLEAR, SLC_CTRL(host->io_base));
  469. /* Transfer size is data area only */
  470. writel(mtd->writesize, SLC_TC(host->io_base));
  471. /* Start transfer in the NAND controller */
  472. writel(readl(SLC_CTRL(host->io_base)) | SLCCTRL_DMA_START,
  473. SLC_CTRL(host->io_base));
  474. for (i = 0; i < chip->ecc.steps; i++) {
  475. /* Data */
  476. res = lpc32xx_xmit_dma(mtd, SLC_DMA_DATA(host->io_base_dma),
  477. dma_buf + i * chip->ecc.size,
  478. mtd->writesize / chip->ecc.steps, dir);
  479. if (res)
  480. return res;
  481. /* Always _read_ ECC */
  482. if (i == chip->ecc.steps - 1)
  483. break;
  484. if (!read) /* ECC availability delayed on write */
  485. udelay(10);
  486. res = lpc32xx_xmit_dma(mtd, SLC_ECC(host->io_base_dma),
  487. &host->ecc_buf[i], 4, DMA_DEV_TO_MEM);
  488. if (res)
  489. return res;
  490. }
  491. /*
  492. * According to NXP, the DMA can be finished here, but the NAND
  493. * controller may still have buffered data. After porting to using the
  494. * dmaengine DMA driver (amba-pl080), the condition (DMA_FIFO empty)
  495. * appears to be always true, according to tests. Keeping the check for
  496. * safety reasons for now.
  497. */
  498. if (readl(SLC_STAT(host->io_base)) & SLCSTAT_DMA_FIFO) {
  499. dev_warn(mtd->dev.parent, "FIFO not empty!\n");
  500. timeout = jiffies + msecs_to_jiffies(LPC32XX_DMA_TIMEOUT);
  501. while ((readl(SLC_STAT(host->io_base)) & SLCSTAT_DMA_FIFO) &&
  502. time_before(jiffies, timeout))
  503. cpu_relax();
  504. if (!time_before(jiffies, timeout)) {
  505. dev_err(mtd->dev.parent, "FIFO held data too long\n");
  506. status = -EIO;
  507. }
  508. }
  509. /* Read last calculated ECC value */
  510. if (!read)
  511. udelay(10);
  512. host->ecc_buf[chip->ecc.steps - 1] =
  513. readl(SLC_ECC(host->io_base));
  514. /* Flush DMA */
  515. dmaengine_terminate_all(host->dma_chan);
  516. if (readl(SLC_STAT(host->io_base)) & SLCSTAT_DMA_FIFO ||
  517. readl(SLC_TC(host->io_base))) {
  518. /* Something is left in the FIFO, something is wrong */
  519. dev_err(mtd->dev.parent, "DMA FIFO failure\n");
  520. status = -EIO;
  521. }
  522. /* Stop DMA & HW ECC */
  523. writel(readl(SLC_CTRL(host->io_base)) & ~SLCCTRL_DMA_START,
  524. SLC_CTRL(host->io_base));
  525. writel(readl(SLC_CFG(host->io_base)) &
  526. ~(SLCCFG_DMA_DIR | SLCCFG_ECC_EN | SLCCFG_DMA_ECC |
  527. SLCCFG_DMA_BURST), SLC_CFG(host->io_base));
  528. if (!dma_mapped && read)
  529. memcpy(buf, host->data_buf, mtd->writesize);
  530. return status;
  531. }
  532. /*
  533. * Read the data and OOB data from the device, use ECC correction with the
  534. * data, disable ECC for the OOB data
  535. */
  536. static int lpc32xx_nand_read_page_syndrome(struct mtd_info *mtd,
  537. struct nand_chip *chip, uint8_t *buf,
  538. int oob_required, int page)
  539. {
  540. struct lpc32xx_nand_host *host = chip->priv;
  541. int stat, i, status;
  542. uint8_t *oobecc, tmpecc[LPC32XX_ECC_SAVE_SIZE];
  543. /* Issue read command */
  544. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  545. /* Read data and oob, calculate ECC */
  546. status = lpc32xx_xfer(mtd, buf, chip->ecc.steps, 1);
  547. /* Get OOB data */
  548. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  549. /* Convert to stored ECC format */
  550. lpc32xx_slc_ecc_copy(tmpecc, (uint32_t *) host->ecc_buf, chip->ecc.steps);
  551. /* Pointer to ECC data retrieved from NAND spare area */
  552. oobecc = chip->oob_poi + chip->ecc.layout->eccpos[0];
  553. for (i = 0; i < chip->ecc.steps; i++) {
  554. stat = chip->ecc.correct(mtd, buf, oobecc,
  555. &tmpecc[i * chip->ecc.bytes]);
  556. if (stat < 0)
  557. mtd->ecc_stats.failed++;
  558. else
  559. mtd->ecc_stats.corrected += stat;
  560. buf += chip->ecc.size;
  561. oobecc += chip->ecc.bytes;
  562. }
  563. return status;
  564. }
  565. /*
  566. * Read the data and OOB data from the device, no ECC correction with the
  567. * data or OOB data
  568. */
  569. static int lpc32xx_nand_read_page_raw_syndrome(struct mtd_info *mtd,
  570. struct nand_chip *chip,
  571. uint8_t *buf, int oob_required,
  572. int page)
  573. {
  574. /* Issue read command */
  575. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  576. /* Raw reads can just use the FIFO interface */
  577. chip->read_buf(mtd, buf, chip->ecc.size * chip->ecc.steps);
  578. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  579. return 0;
  580. }
  581. /*
  582. * Write the data and OOB data to the device, use ECC with the data,
  583. * disable ECC for the OOB data
  584. */
  585. static int lpc32xx_nand_write_page_syndrome(struct mtd_info *mtd,
  586. struct nand_chip *chip,
  587. const uint8_t *buf, int oob_required)
  588. {
  589. struct lpc32xx_nand_host *host = chip->priv;
  590. uint8_t *pb = chip->oob_poi + chip->ecc.layout->eccpos[0];
  591. int error;
  592. /* Write data, calculate ECC on outbound data */
  593. error = lpc32xx_xfer(mtd, (uint8_t *)buf, chip->ecc.steps, 0);
  594. if (error)
  595. return error;
  596. /*
  597. * The calculated ECC needs some manual work done to it before
  598. * committing it to NAND. Process the calculated ECC and place
  599. * the resultant values directly into the OOB buffer. */
  600. lpc32xx_slc_ecc_copy(pb, (uint32_t *)host->ecc_buf, chip->ecc.steps);
  601. /* Write ECC data to device */
  602. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  603. return 0;
  604. }
  605. /*
  606. * Write the data and OOB data to the device, no ECC correction with the
  607. * data or OOB data
  608. */
  609. static int lpc32xx_nand_write_page_raw_syndrome(struct mtd_info *mtd,
  610. struct nand_chip *chip,
  611. const uint8_t *buf,
  612. int oob_required)
  613. {
  614. /* Raw writes can just use the FIFO interface */
  615. chip->write_buf(mtd, buf, chip->ecc.size * chip->ecc.steps);
  616. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  617. return 0;
  618. }
  619. static int lpc32xx_nand_dma_setup(struct lpc32xx_nand_host *host)
  620. {
  621. struct mtd_info *mtd = &host->mtd;
  622. dma_cap_mask_t mask;
  623. dma_cap_zero(mask);
  624. dma_cap_set(DMA_SLAVE, mask);
  625. host->dma_chan = dma_request_channel(mask, pl08x_filter_id, "nand-slc");
  626. if (!host->dma_chan) {
  627. dev_err(mtd->dev.parent, "Failed to request DMA channel\n");
  628. return -EBUSY;
  629. }
  630. return 0;
  631. }
  632. #ifdef CONFIG_OF
  633. static struct lpc32xx_nand_cfg_slc *lpc32xx_parse_dt(struct device *dev)
  634. {
  635. struct lpc32xx_nand_cfg_slc *pdata;
  636. struct device_node *np = dev->of_node;
  637. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  638. if (!pdata) {
  639. dev_err(dev, "could not allocate memory for platform data\n");
  640. return NULL;
  641. }
  642. of_property_read_u32(np, "nxp,wdr-clks", &pdata->wdr_clks);
  643. of_property_read_u32(np, "nxp,wwidth", &pdata->wwidth);
  644. of_property_read_u32(np, "nxp,whold", &pdata->whold);
  645. of_property_read_u32(np, "nxp,wsetup", &pdata->wsetup);
  646. of_property_read_u32(np, "nxp,rdr-clks", &pdata->rdr_clks);
  647. of_property_read_u32(np, "nxp,rwidth", &pdata->rwidth);
  648. of_property_read_u32(np, "nxp,rhold", &pdata->rhold);
  649. of_property_read_u32(np, "nxp,rsetup", &pdata->rsetup);
  650. if (!pdata->wdr_clks || !pdata->wwidth || !pdata->whold ||
  651. !pdata->wsetup || !pdata->rdr_clks || !pdata->rwidth ||
  652. !pdata->rhold || !pdata->rsetup) {
  653. dev_err(dev, "chip parameters not specified correctly\n");
  654. return NULL;
  655. }
  656. pdata->use_bbt = of_get_nand_on_flash_bbt(np);
  657. pdata->wp_gpio = of_get_named_gpio(np, "gpios", 0);
  658. return pdata;
  659. }
  660. #else
  661. static struct lpc32xx_nand_cfg_slc *lpc32xx_parse_dt(struct device *dev)
  662. {
  663. return NULL;
  664. }
  665. #endif
  666. /*
  667. * Probe for NAND controller
  668. */
  669. static int __devinit lpc32xx_nand_probe(struct platform_device *pdev)
  670. {
  671. struct lpc32xx_nand_host *host;
  672. struct mtd_info *mtd;
  673. struct nand_chip *chip;
  674. struct resource *rc;
  675. struct mtd_part_parser_data ppdata = {};
  676. int res;
  677. rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  678. if (rc == NULL) {
  679. dev_err(&pdev->dev, "No memory resource found for device\n");
  680. return -EBUSY;
  681. }
  682. /* Allocate memory for the device structure (and zero it) */
  683. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  684. if (!host) {
  685. dev_err(&pdev->dev, "failed to allocate device structure\n");
  686. return -ENOMEM;
  687. }
  688. host->io_base_dma = rc->start;
  689. host->io_base = devm_request_and_ioremap(&pdev->dev, rc);
  690. if (host->io_base == NULL) {
  691. dev_err(&pdev->dev, "ioremap failed\n");
  692. return -ENOMEM;
  693. }
  694. if (pdev->dev.of_node)
  695. host->ncfg = lpc32xx_parse_dt(&pdev->dev);
  696. else
  697. host->ncfg = pdev->dev.platform_data;
  698. if (!host->ncfg) {
  699. dev_err(&pdev->dev, "Missing platform data\n");
  700. return -ENOENT;
  701. }
  702. if (host->ncfg->wp_gpio == -EPROBE_DEFER)
  703. return -EPROBE_DEFER;
  704. if (gpio_is_valid(host->ncfg->wp_gpio) &&
  705. gpio_request(host->ncfg->wp_gpio, "NAND WP")) {
  706. dev_err(&pdev->dev, "GPIO not available\n");
  707. return -EBUSY;
  708. }
  709. lpc32xx_wp_disable(host);
  710. mtd = &host->mtd;
  711. chip = &host->nand_chip;
  712. chip->priv = host;
  713. mtd->priv = chip;
  714. mtd->owner = THIS_MODULE;
  715. mtd->dev.parent = &pdev->dev;
  716. /* Get NAND clock */
  717. host->clk = clk_get(&pdev->dev, NULL);
  718. if (IS_ERR(host->clk)) {
  719. dev_err(&pdev->dev, "Clock failure\n");
  720. res = -ENOENT;
  721. goto err_exit1;
  722. }
  723. clk_enable(host->clk);
  724. /* Set NAND IO addresses and command/ready functions */
  725. chip->IO_ADDR_R = SLC_DATA(host->io_base);
  726. chip->IO_ADDR_W = SLC_DATA(host->io_base);
  727. chip->cmd_ctrl = lpc32xx_nand_cmd_ctrl;
  728. chip->dev_ready = lpc32xx_nand_device_ready;
  729. chip->chip_delay = 20; /* 20us command delay time */
  730. /* Init NAND controller */
  731. lpc32xx_nand_setup(host);
  732. platform_set_drvdata(pdev, host);
  733. /* NAND callbacks for LPC32xx SLC hardware */
  734. chip->ecc.mode = NAND_ECC_HW_SYNDROME;
  735. chip->read_byte = lpc32xx_nand_read_byte;
  736. chip->read_buf = lpc32xx_nand_read_buf;
  737. chip->write_buf = lpc32xx_nand_write_buf;
  738. chip->ecc.read_page_raw = lpc32xx_nand_read_page_raw_syndrome;
  739. chip->ecc.read_page = lpc32xx_nand_read_page_syndrome;
  740. chip->ecc.write_page_raw = lpc32xx_nand_write_page_raw_syndrome;
  741. chip->ecc.write_page = lpc32xx_nand_write_page_syndrome;
  742. chip->ecc.write_oob = lpc32xx_nand_write_oob_syndrome;
  743. chip->ecc.read_oob = lpc32xx_nand_read_oob_syndrome;
  744. chip->ecc.calculate = lpc32xx_nand_ecc_calculate;
  745. chip->ecc.correct = nand_correct_data;
  746. chip->ecc.strength = 1;
  747. chip->ecc.hwctl = lpc32xx_nand_ecc_enable;
  748. chip->verify_buf = lpc32xx_verify_buf;
  749. /* bitflip_threshold's default is defined as ecc_strength anyway.
  750. * Unfortunately, it is set only later at add_mtd_device(). Meanwhile
  751. * being 0, it causes bad block table scanning errors in
  752. * nand_scan_tail(), so preparing it here already. */
  753. mtd->bitflip_threshold = chip->ecc.strength;
  754. /*
  755. * Allocate a large enough buffer for a single huge page plus
  756. * extra space for the spare area and ECC storage area
  757. */
  758. host->dma_buf_len = LPC32XX_DMA_DATA_SIZE + LPC32XX_ECC_SAVE_SIZE;
  759. host->data_buf = devm_kzalloc(&pdev->dev, host->dma_buf_len,
  760. GFP_KERNEL);
  761. if (host->data_buf == NULL) {
  762. dev_err(&pdev->dev, "Error allocating memory\n");
  763. res = -ENOMEM;
  764. goto err_exit2;
  765. }
  766. res = lpc32xx_nand_dma_setup(host);
  767. if (res) {
  768. res = -EIO;
  769. goto err_exit2;
  770. }
  771. /* Find NAND device */
  772. if (nand_scan_ident(mtd, 1, NULL)) {
  773. res = -ENXIO;
  774. goto err_exit3;
  775. }
  776. /* OOB and ECC CPU and DMA work areas */
  777. host->ecc_buf = (uint32_t *)(host->data_buf + LPC32XX_DMA_DATA_SIZE);
  778. /*
  779. * Small page FLASH has a unique OOB layout, but large and huge
  780. * page FLASH use the standard layout. Small page FLASH uses a
  781. * custom BBT marker layout.
  782. */
  783. if (mtd->writesize <= 512)
  784. chip->ecc.layout = &lpc32xx_nand_oob_16;
  785. /* These sizes remain the same regardless of page size */
  786. chip->ecc.size = 256;
  787. chip->ecc.bytes = LPC32XX_SLC_DEV_ECC_BYTES;
  788. chip->ecc.prepad = chip->ecc.postpad = 0;
  789. /* Avoid extra scan if using BBT, setup BBT support */
  790. if (host->ncfg->use_bbt) {
  791. chip->options |= NAND_SKIP_BBTSCAN;
  792. chip->bbt_options |= NAND_BBT_USE_FLASH;
  793. /*
  794. * Use a custom BBT marker setup for small page FLASH that
  795. * won't interfere with the ECC layout. Large and huge page
  796. * FLASH use the standard layout.
  797. */
  798. if (mtd->writesize <= 512) {
  799. chip->bbt_td = &bbt_smallpage_main_descr;
  800. chip->bbt_md = &bbt_smallpage_mirror_descr;
  801. }
  802. }
  803. /*
  804. * Fills out all the uninitialized function pointers with the defaults
  805. */
  806. if (nand_scan_tail(mtd)) {
  807. res = -ENXIO;
  808. goto err_exit3;
  809. }
  810. /* Standard layout in FLASH for bad block tables */
  811. if (host->ncfg->use_bbt) {
  812. if (nand_default_bbt(mtd) < 0)
  813. dev_err(&pdev->dev,
  814. "Error initializing default bad block tables\n");
  815. }
  816. mtd->name = "nxp_lpc3220_slc";
  817. ppdata.of_node = pdev->dev.of_node;
  818. res = mtd_device_parse_register(mtd, NULL, &ppdata, host->ncfg->parts,
  819. host->ncfg->num_parts);
  820. if (!res)
  821. return res;
  822. nand_release(mtd);
  823. err_exit3:
  824. dma_release_channel(host->dma_chan);
  825. err_exit2:
  826. clk_disable(host->clk);
  827. clk_put(host->clk);
  828. platform_set_drvdata(pdev, NULL);
  829. err_exit1:
  830. lpc32xx_wp_enable(host);
  831. gpio_free(host->ncfg->wp_gpio);
  832. return res;
  833. }
  834. /*
  835. * Remove NAND device.
  836. */
  837. static int __devexit lpc32xx_nand_remove(struct platform_device *pdev)
  838. {
  839. uint32_t tmp;
  840. struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
  841. struct mtd_info *mtd = &host->mtd;
  842. nand_release(mtd);
  843. dma_release_channel(host->dma_chan);
  844. /* Force CE high */
  845. tmp = readl(SLC_CTRL(host->io_base));
  846. tmp &= ~SLCCFG_CE_LOW;
  847. writel(tmp, SLC_CTRL(host->io_base));
  848. clk_disable(host->clk);
  849. clk_put(host->clk);
  850. platform_set_drvdata(pdev, NULL);
  851. lpc32xx_wp_enable(host);
  852. gpio_free(host->ncfg->wp_gpio);
  853. return 0;
  854. }
  855. #ifdef CONFIG_PM
  856. static int lpc32xx_nand_resume(struct platform_device *pdev)
  857. {
  858. struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
  859. /* Re-enable NAND clock */
  860. clk_enable(host->clk);
  861. /* Fresh init of NAND controller */
  862. lpc32xx_nand_setup(host);
  863. /* Disable write protect */
  864. lpc32xx_wp_disable(host);
  865. return 0;
  866. }
  867. static int lpc32xx_nand_suspend(struct platform_device *pdev, pm_message_t pm)
  868. {
  869. uint32_t tmp;
  870. struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
  871. /* Force CE high */
  872. tmp = readl(SLC_CTRL(host->io_base));
  873. tmp &= ~SLCCFG_CE_LOW;
  874. writel(tmp, SLC_CTRL(host->io_base));
  875. /* Enable write protect for safety */
  876. lpc32xx_wp_enable(host);
  877. /* Disable clock */
  878. clk_disable(host->clk);
  879. return 0;
  880. }
  881. #else
  882. #define lpc32xx_nand_resume NULL
  883. #define lpc32xx_nand_suspend NULL
  884. #endif
  885. #if defined(CONFIG_OF)
  886. static const struct of_device_id lpc32xx_nand_match[] = {
  887. { .compatible = "nxp,lpc3220-slc" },
  888. { /* sentinel */ },
  889. };
  890. MODULE_DEVICE_TABLE(of, lpc32xx_nand_match);
  891. #endif
  892. static struct platform_driver lpc32xx_nand_driver = {
  893. .probe = lpc32xx_nand_probe,
  894. .remove = __devexit_p(lpc32xx_nand_remove),
  895. .resume = lpc32xx_nand_resume,
  896. .suspend = lpc32xx_nand_suspend,
  897. .driver = {
  898. .name = LPC32XX_MODNAME,
  899. .owner = THIS_MODULE,
  900. .of_match_table = of_match_ptr(lpc32xx_nand_match),
  901. },
  902. };
  903. module_platform_driver(lpc32xx_nand_driver);
  904. MODULE_LICENSE("GPL");
  905. MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>");
  906. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  907. MODULE_DESCRIPTION("NAND driver for the NXP LPC32XX SLC controller");