lpc32xx_slc.c 29 KB

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