lpc32xx_slc.c 29 KB

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