lpc32xx_slc.c 28 KB

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