1a430fa06SMiquel Raynal // SPDX-License-Identifier: GPL-2.0+
2a430fa06SMiquel Raynal /*
3a430fa06SMiquel Raynal * LPC32xx SLC NAND flash controller driver
4a430fa06SMiquel Raynal *
5*44cdfc0eSVladimir Zapolskiy * (C) Copyright 2015-2018 Vladimir Zapolskiy <vz@mleia.com>
6*44cdfc0eSVladimir Zapolskiy * Copyright (c) 2015 Tyco Fire Protection Products.
7a430fa06SMiquel Raynal *
8a430fa06SMiquel Raynal * Hardware ECC support original source code
9a430fa06SMiquel Raynal * Copyright (C) 2008 by NXP Semiconductors
10a430fa06SMiquel Raynal * Author: Kevin Wells
11a430fa06SMiquel Raynal */
12a430fa06SMiquel Raynal
13a430fa06SMiquel Raynal #include <common.h>
14a430fa06SMiquel Raynal #include <nand.h>
15a430fa06SMiquel Raynal #include <linux/mtd/nand_ecc.h>
16a430fa06SMiquel Raynal #include <linux/errno.h>
17a430fa06SMiquel Raynal #include <asm/io.h>
18a430fa06SMiquel Raynal #include <asm/arch/config.h>
19a430fa06SMiquel Raynal #include <asm/arch/clk.h>
20a430fa06SMiquel Raynal #include <asm/arch/sys_proto.h>
21a430fa06SMiquel Raynal #include <asm/arch/dma.h>
22a430fa06SMiquel Raynal #include <asm/arch/cpu.h>
23a430fa06SMiquel Raynal
24a430fa06SMiquel Raynal struct lpc32xx_nand_slc_regs {
25a430fa06SMiquel Raynal u32 data;
26a430fa06SMiquel Raynal u32 addr;
27a430fa06SMiquel Raynal u32 cmd;
28a430fa06SMiquel Raynal u32 stop;
29a430fa06SMiquel Raynal u32 ctrl;
30a430fa06SMiquel Raynal u32 cfg;
31a430fa06SMiquel Raynal u32 stat;
32a430fa06SMiquel Raynal u32 int_stat;
33a430fa06SMiquel Raynal u32 ien;
34a430fa06SMiquel Raynal u32 isr;
35a430fa06SMiquel Raynal u32 icr;
36a430fa06SMiquel Raynal u32 tac;
37a430fa06SMiquel Raynal u32 tc;
38a430fa06SMiquel Raynal u32 ecc;
39a430fa06SMiquel Raynal u32 dma_data;
40a430fa06SMiquel Raynal };
41a430fa06SMiquel Raynal
42a430fa06SMiquel Raynal /* CFG register */
43a430fa06SMiquel Raynal #define CFG_CE_LOW (1 << 5)
44a430fa06SMiquel Raynal #define CFG_DMA_ECC (1 << 4) /* Enable DMA ECC bit */
45a430fa06SMiquel Raynal #define CFG_ECC_EN (1 << 3) /* ECC enable bit */
46a430fa06SMiquel Raynal #define CFG_DMA_BURST (1 << 2) /* DMA burst bit */
47a430fa06SMiquel Raynal #define CFG_DMA_DIR (1 << 1) /* DMA write(0)/read(1) bit */
48a430fa06SMiquel Raynal
49a430fa06SMiquel Raynal /* CTRL register */
50a430fa06SMiquel Raynal #define CTRL_SW_RESET (1 << 2)
51a430fa06SMiquel Raynal #define CTRL_ECC_CLEAR (1 << 1) /* Reset ECC bit */
52a430fa06SMiquel Raynal #define CTRL_DMA_START (1 << 0) /* Start DMA channel bit */
53a430fa06SMiquel Raynal
54a430fa06SMiquel Raynal /* STAT register */
55a430fa06SMiquel Raynal #define STAT_DMA_FIFO (1 << 2) /* DMA FIFO has data bit */
56a430fa06SMiquel Raynal #define STAT_NAND_READY (1 << 0)
57a430fa06SMiquel Raynal
58a430fa06SMiquel Raynal /* INT_STAT register */
59a430fa06SMiquel Raynal #define INT_STAT_TC (1 << 1)
60a430fa06SMiquel Raynal #define INT_STAT_RDY (1 << 0)
61a430fa06SMiquel Raynal
62a430fa06SMiquel Raynal /* TAC register bits, be aware of overflows */
63a430fa06SMiquel Raynal #define TAC_W_RDY(n) (max_t(uint32_t, (n), 0xF) << 28)
64a430fa06SMiquel Raynal #define TAC_W_WIDTH(n) (max_t(uint32_t, (n), 0xF) << 24)
65a430fa06SMiquel Raynal #define TAC_W_HOLD(n) (max_t(uint32_t, (n), 0xF) << 20)
66a430fa06SMiquel Raynal #define TAC_W_SETUP(n) (max_t(uint32_t, (n), 0xF) << 16)
67a430fa06SMiquel Raynal #define TAC_R_RDY(n) (max_t(uint32_t, (n), 0xF) << 12)
68a430fa06SMiquel Raynal #define TAC_R_WIDTH(n) (max_t(uint32_t, (n), 0xF) << 8)
69a430fa06SMiquel Raynal #define TAC_R_HOLD(n) (max_t(uint32_t, (n), 0xF) << 4)
70a430fa06SMiquel Raynal #define TAC_R_SETUP(n) (max_t(uint32_t, (n), 0xF) << 0)
71a430fa06SMiquel Raynal
72a430fa06SMiquel Raynal /* NAND ECC Layout for small page NAND devices
73a430fa06SMiquel Raynal * Note: For large page devices, the default layouts are used. */
74a430fa06SMiquel Raynal static struct nand_ecclayout lpc32xx_nand_oob_16 = {
75a430fa06SMiquel Raynal .eccbytes = 6,
76*44cdfc0eSVladimir Zapolskiy .eccpos = { 10, 11, 12, 13, 14, 15, },
77a430fa06SMiquel Raynal .oobfree = {
78*44cdfc0eSVladimir Zapolskiy { .offset = 0, .length = 4, },
79*44cdfc0eSVladimir Zapolskiy { .offset = 6, .length = 4, },
80a430fa06SMiquel Raynal }
81a430fa06SMiquel Raynal };
82a430fa06SMiquel Raynal
83*44cdfc0eSVladimir Zapolskiy #if defined(CONFIG_DMA_LPC32XX) && !defined(CONFIG_SPL_BUILD)
84a430fa06SMiquel Raynal #define ECCSTEPS (CONFIG_SYS_NAND_PAGE_SIZE / CONFIG_SYS_NAND_ECCSIZE)
85a430fa06SMiquel Raynal
86a430fa06SMiquel Raynal /*
87a430fa06SMiquel Raynal * DMA Descriptors
88a430fa06SMiquel Raynal * For Large Block: 17 descriptors = ((16 Data and ECC Read) + 1 Spare Area)
89a430fa06SMiquel Raynal * For Small Block: 5 descriptors = ((4 Data and ECC Read) + 1 Spare Area)
90a430fa06SMiquel Raynal */
91a430fa06SMiquel Raynal static struct lpc32xx_dmac_ll dmalist[ECCSTEPS * 2 + 1];
92a430fa06SMiquel Raynal static u32 ecc_buffer[8]; /* MAX ECC size */
93a430fa06SMiquel Raynal static unsigned int dmachan = (unsigned int)-1; /* Invalid channel */
94a430fa06SMiquel Raynal
95a430fa06SMiquel Raynal /*
96a430fa06SMiquel Raynal * Helper macro for the DMA client (i.e. NAND SLC):
97a430fa06SMiquel Raynal * - to write the next DMA linked list item address
98a430fa06SMiquel Raynal * (see arch/include/asm/arch-lpc32xx/dma.h).
99a430fa06SMiquel Raynal * - to assign the DMA data register to DMA source or destination address.
100a430fa06SMiquel Raynal * - to assign the ECC register to DMA source or destination address.
101a430fa06SMiquel Raynal */
102a430fa06SMiquel Raynal #define lpc32xx_dmac_next_lli(x) ((u32)x)
103a430fa06SMiquel Raynal #define lpc32xx_dmac_set_dma_data() ((u32)&lpc32xx_nand_slc_regs->dma_data)
104a430fa06SMiquel Raynal #define lpc32xx_dmac_set_ecc() ((u32)&lpc32xx_nand_slc_regs->ecc)
105a430fa06SMiquel Raynal #endif
106a430fa06SMiquel Raynal
107a430fa06SMiquel Raynal static struct lpc32xx_nand_slc_regs __iomem *lpc32xx_nand_slc_regs
108a430fa06SMiquel Raynal = (struct lpc32xx_nand_slc_regs __iomem *)SLC_NAND_BASE;
109a430fa06SMiquel Raynal
lpc32xx_nand_init(void)110a430fa06SMiquel Raynal static void lpc32xx_nand_init(void)
111a430fa06SMiquel Raynal {
112a430fa06SMiquel Raynal uint32_t hclk = get_hclk_clk_rate();
113a430fa06SMiquel Raynal
114a430fa06SMiquel Raynal /* Reset SLC NAND controller */
115a430fa06SMiquel Raynal writel(CTRL_SW_RESET, &lpc32xx_nand_slc_regs->ctrl);
116a430fa06SMiquel Raynal
117a430fa06SMiquel Raynal /* 8-bit bus, no DMA, no ECC, ordinary CE signal */
118a430fa06SMiquel Raynal writel(0, &lpc32xx_nand_slc_regs->cfg);
119a430fa06SMiquel Raynal
120a430fa06SMiquel Raynal /* Interrupts disabled and cleared */
121a430fa06SMiquel Raynal writel(0, &lpc32xx_nand_slc_regs->ien);
122a430fa06SMiquel Raynal writel(INT_STAT_TC | INT_STAT_RDY,
123a430fa06SMiquel Raynal &lpc32xx_nand_slc_regs->icr);
124a430fa06SMiquel Raynal
125a430fa06SMiquel Raynal /* Configure NAND flash timings */
126a430fa06SMiquel Raynal writel(TAC_W_RDY(CONFIG_LPC32XX_NAND_SLC_WDR_CLKS) |
127a430fa06SMiquel Raynal TAC_W_WIDTH(hclk / CONFIG_LPC32XX_NAND_SLC_WWIDTH) |
128a430fa06SMiquel Raynal TAC_W_HOLD(hclk / CONFIG_LPC32XX_NAND_SLC_WHOLD) |
129a430fa06SMiquel Raynal TAC_W_SETUP(hclk / CONFIG_LPC32XX_NAND_SLC_WSETUP) |
130a430fa06SMiquel Raynal TAC_R_RDY(CONFIG_LPC32XX_NAND_SLC_RDR_CLKS) |
131a430fa06SMiquel Raynal TAC_R_WIDTH(hclk / CONFIG_LPC32XX_NAND_SLC_RWIDTH) |
132a430fa06SMiquel Raynal TAC_R_HOLD(hclk / CONFIG_LPC32XX_NAND_SLC_RHOLD) |
133a430fa06SMiquel Raynal TAC_R_SETUP(hclk / CONFIG_LPC32XX_NAND_SLC_RSETUP),
134a430fa06SMiquel Raynal &lpc32xx_nand_slc_regs->tac);
135a430fa06SMiquel Raynal }
136a430fa06SMiquel Raynal
lpc32xx_nand_cmd_ctrl(struct mtd_info * mtd,int cmd,unsigned int ctrl)137a430fa06SMiquel Raynal static void lpc32xx_nand_cmd_ctrl(struct mtd_info *mtd,
138a430fa06SMiquel Raynal int cmd, unsigned int ctrl)
139a430fa06SMiquel Raynal {
140a430fa06SMiquel Raynal debug("ctrl: 0x%08x, cmd: 0x%08x\n", ctrl, cmd);
141a430fa06SMiquel Raynal
142a430fa06SMiquel Raynal if (ctrl & NAND_NCE)
143a430fa06SMiquel Raynal setbits_le32(&lpc32xx_nand_slc_regs->cfg, CFG_CE_LOW);
144a430fa06SMiquel Raynal else
145a430fa06SMiquel Raynal clrbits_le32(&lpc32xx_nand_slc_regs->cfg, CFG_CE_LOW);
146a430fa06SMiquel Raynal
147a430fa06SMiquel Raynal if (cmd == NAND_CMD_NONE)
148a430fa06SMiquel Raynal return;
149a430fa06SMiquel Raynal
150a430fa06SMiquel Raynal if (ctrl & NAND_CLE)
151a430fa06SMiquel Raynal writel(cmd & 0xFF, &lpc32xx_nand_slc_regs->cmd);
152a430fa06SMiquel Raynal else if (ctrl & NAND_ALE)
153a430fa06SMiquel Raynal writel(cmd & 0xFF, &lpc32xx_nand_slc_regs->addr);
154a430fa06SMiquel Raynal }
155a430fa06SMiquel Raynal
lpc32xx_nand_dev_ready(struct mtd_info * mtd)156a430fa06SMiquel Raynal static int lpc32xx_nand_dev_ready(struct mtd_info *mtd)
157a430fa06SMiquel Raynal {
158a430fa06SMiquel Raynal return readl(&lpc32xx_nand_slc_regs->stat) & STAT_NAND_READY;
159a430fa06SMiquel Raynal }
160a430fa06SMiquel Raynal
161*44cdfc0eSVladimir Zapolskiy #if defined(CONFIG_DMA_LPC32XX) && !defined(CONFIG_SPL_BUILD)
162a430fa06SMiquel Raynal /*
163a430fa06SMiquel Raynal * Prepares DMA descriptors for NAND RD/WR operations
164a430fa06SMiquel Raynal * If the size is < 256 Bytes then it is assumed to be
165a430fa06SMiquel Raynal * an OOB transfer
166a430fa06SMiquel Raynal */
lpc32xx_nand_dma_configure(struct nand_chip * chip,const u8 * buffer,int size,int read)167a430fa06SMiquel Raynal static void lpc32xx_nand_dma_configure(struct nand_chip *chip,
168a430fa06SMiquel Raynal const u8 *buffer, int size,
169a430fa06SMiquel Raynal int read)
170a430fa06SMiquel Raynal {
171a430fa06SMiquel Raynal u32 i, dmasrc, ctrl, ecc_ctrl, oob_ctrl, dmadst;
172a430fa06SMiquel Raynal struct lpc32xx_dmac_ll *dmalist_cur;
173a430fa06SMiquel Raynal struct lpc32xx_dmac_ll *dmalist_cur_ecc;
174a430fa06SMiquel Raynal
175a430fa06SMiquel Raynal /*
176a430fa06SMiquel Raynal * CTRL descriptor entry for reading ECC
177a430fa06SMiquel Raynal * Copy Multiple times to sync DMA with Flash Controller
178a430fa06SMiquel Raynal */
179a430fa06SMiquel Raynal ecc_ctrl = 0x5 |
180a430fa06SMiquel Raynal DMAC_CHAN_SRC_BURST_1 |
181a430fa06SMiquel Raynal DMAC_CHAN_DEST_BURST_1 |
182a430fa06SMiquel Raynal DMAC_CHAN_SRC_WIDTH_32 |
183a430fa06SMiquel Raynal DMAC_CHAN_DEST_WIDTH_32 |
184a430fa06SMiquel Raynal DMAC_CHAN_DEST_AHB1;
185a430fa06SMiquel Raynal
186a430fa06SMiquel Raynal /* CTRL descriptor entry for reading/writing Data */
187a430fa06SMiquel Raynal ctrl = (CONFIG_SYS_NAND_ECCSIZE / 4) |
188a430fa06SMiquel Raynal DMAC_CHAN_SRC_BURST_4 |
189a430fa06SMiquel Raynal DMAC_CHAN_DEST_BURST_4 |
190a430fa06SMiquel Raynal DMAC_CHAN_SRC_WIDTH_32 |
191a430fa06SMiquel Raynal DMAC_CHAN_DEST_WIDTH_32 |
192a430fa06SMiquel Raynal DMAC_CHAN_DEST_AHB1;
193a430fa06SMiquel Raynal
194a430fa06SMiquel Raynal /* CTRL descriptor entry for reading/writing Spare Area */
195a430fa06SMiquel Raynal oob_ctrl = (CONFIG_SYS_NAND_OOBSIZE / 4) |
196a430fa06SMiquel Raynal DMAC_CHAN_SRC_BURST_4 |
197a430fa06SMiquel Raynal DMAC_CHAN_DEST_BURST_4 |
198a430fa06SMiquel Raynal DMAC_CHAN_SRC_WIDTH_32 |
199a430fa06SMiquel Raynal DMAC_CHAN_DEST_WIDTH_32 |
200a430fa06SMiquel Raynal DMAC_CHAN_DEST_AHB1;
201a430fa06SMiquel Raynal
202a430fa06SMiquel Raynal if (read) {
203a430fa06SMiquel Raynal dmasrc = lpc32xx_dmac_set_dma_data();
204a430fa06SMiquel Raynal dmadst = (u32)buffer;
205a430fa06SMiquel Raynal ctrl |= DMAC_CHAN_DEST_AUTOINC;
206a430fa06SMiquel Raynal } else {
207a430fa06SMiquel Raynal dmadst = lpc32xx_dmac_set_dma_data();
208a430fa06SMiquel Raynal dmasrc = (u32)buffer;
209a430fa06SMiquel Raynal ctrl |= DMAC_CHAN_SRC_AUTOINC;
210a430fa06SMiquel Raynal }
211a430fa06SMiquel Raynal
212a430fa06SMiquel Raynal /*
213a430fa06SMiquel Raynal * Write Operation Sequence for Small Block NAND
214a430fa06SMiquel Raynal * ----------------------------------------------------------
215a430fa06SMiquel Raynal * 1. X'fer 256 bytes of data from Memory to Flash.
216a430fa06SMiquel Raynal * 2. Copy generated ECC data from Register to Spare Area
217a430fa06SMiquel Raynal * 3. X'fer next 256 bytes of data from Memory to Flash.
218a430fa06SMiquel Raynal * 4. Copy generated ECC data from Register to Spare Area.
219a430fa06SMiquel Raynal * 5. X'fer 16 byets of Spare area from Memory to Flash.
220a430fa06SMiquel Raynal * Read Operation Sequence for Small Block NAND
221a430fa06SMiquel Raynal * ----------------------------------------------------------
222a430fa06SMiquel Raynal * 1. X'fer 256 bytes of data from Flash to Memory.
223a430fa06SMiquel Raynal * 2. Copy generated ECC data from Register to ECC calc Buffer.
224a430fa06SMiquel Raynal * 3. X'fer next 256 bytes of data from Flash to Memory.
225a430fa06SMiquel Raynal * 4. Copy generated ECC data from Register to ECC calc Buffer.
226a430fa06SMiquel Raynal * 5. X'fer 16 bytes of Spare area from Flash to Memory.
227a430fa06SMiquel Raynal * Write Operation Sequence for Large Block NAND
228a430fa06SMiquel Raynal * ----------------------------------------------------------
229a430fa06SMiquel Raynal * 1. Steps(1-4) of Write Operations repeate for four times
230a430fa06SMiquel Raynal * which generates 16 DMA descriptors to X'fer 2048 bytes of
231a430fa06SMiquel Raynal * data & 32 bytes of ECC data.
232a430fa06SMiquel Raynal * 2. X'fer 64 bytes of Spare area from Memory to Flash.
233a430fa06SMiquel Raynal * Read Operation Sequence for Large Block NAND
234a430fa06SMiquel Raynal * ----------------------------------------------------------
235a430fa06SMiquel Raynal * 1. Steps(1-4) of Read Operations repeate for four times
236a430fa06SMiquel Raynal * which generates 16 DMA descriptors to X'fer 2048 bytes of
237a430fa06SMiquel Raynal * data & 32 bytes of ECC data.
238a430fa06SMiquel Raynal * 2. X'fer 64 bytes of Spare area from Flash to Memory.
239a430fa06SMiquel Raynal */
240a430fa06SMiquel Raynal
241a430fa06SMiquel Raynal for (i = 0; i < size/CONFIG_SYS_NAND_ECCSIZE; i++) {
242a430fa06SMiquel Raynal dmalist_cur = &dmalist[i * 2];
243a430fa06SMiquel Raynal dmalist_cur_ecc = &dmalist[(i * 2) + 1];
244a430fa06SMiquel Raynal
245a430fa06SMiquel Raynal dmalist_cur->dma_src = (read ? (dmasrc) : (dmasrc + (i*256)));
246a430fa06SMiquel Raynal dmalist_cur->dma_dest = (read ? (dmadst + (i*256)) : dmadst);
247a430fa06SMiquel Raynal dmalist_cur->next_lli = lpc32xx_dmac_next_lli(dmalist_cur_ecc);
248a430fa06SMiquel Raynal dmalist_cur->next_ctrl = ctrl;
249a430fa06SMiquel Raynal
250a430fa06SMiquel Raynal dmalist_cur_ecc->dma_src = lpc32xx_dmac_set_ecc();
251a430fa06SMiquel Raynal dmalist_cur_ecc->dma_dest = (u32)&ecc_buffer[i];
252a430fa06SMiquel Raynal dmalist_cur_ecc->next_lli =
253a430fa06SMiquel Raynal lpc32xx_dmac_next_lli(&dmalist[(i * 2) + 2]);
254a430fa06SMiquel Raynal dmalist_cur_ecc->next_ctrl = ecc_ctrl;
255a430fa06SMiquel Raynal }
256a430fa06SMiquel Raynal
257a430fa06SMiquel Raynal if (i) { /* Data only transfer */
258a430fa06SMiquel Raynal dmalist_cur_ecc = &dmalist[(i * 2) - 1];
259a430fa06SMiquel Raynal dmalist_cur_ecc->next_lli = 0;
260a430fa06SMiquel Raynal dmalist_cur_ecc->next_ctrl |= DMAC_CHAN_INT_TC_EN;
261a430fa06SMiquel Raynal return;
262a430fa06SMiquel Raynal }
263a430fa06SMiquel Raynal
264a430fa06SMiquel Raynal /* OOB only transfer */
265a430fa06SMiquel Raynal if (read) {
266a430fa06SMiquel Raynal dmasrc = lpc32xx_dmac_set_dma_data();
267a430fa06SMiquel Raynal dmadst = (u32)buffer;
268a430fa06SMiquel Raynal oob_ctrl |= DMAC_CHAN_DEST_AUTOINC;
269a430fa06SMiquel Raynal } else {
270a430fa06SMiquel Raynal dmadst = lpc32xx_dmac_set_dma_data();
271a430fa06SMiquel Raynal dmasrc = (u32)buffer;
272a430fa06SMiquel Raynal oob_ctrl |= DMAC_CHAN_SRC_AUTOINC;
273a430fa06SMiquel Raynal }
274a430fa06SMiquel Raynal
275a430fa06SMiquel Raynal /* Read/ Write Spare Area Data To/From Flash */
276a430fa06SMiquel Raynal dmalist_cur = &dmalist[i * 2];
277a430fa06SMiquel Raynal dmalist_cur->dma_src = dmasrc;
278a430fa06SMiquel Raynal dmalist_cur->dma_dest = dmadst;
279a430fa06SMiquel Raynal dmalist_cur->next_lli = 0;
280a430fa06SMiquel Raynal dmalist_cur->next_ctrl = (oob_ctrl | DMAC_CHAN_INT_TC_EN);
281a430fa06SMiquel Raynal }
282a430fa06SMiquel Raynal
lpc32xx_nand_xfer(struct mtd_info * mtd,const u8 * buf,int len,int read)283a430fa06SMiquel Raynal static void lpc32xx_nand_xfer(struct mtd_info *mtd, const u8 *buf,
284a430fa06SMiquel Raynal int len, int read)
285a430fa06SMiquel Raynal {
286a430fa06SMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd);
287a430fa06SMiquel Raynal u32 config;
288a430fa06SMiquel Raynal int ret;
289a430fa06SMiquel Raynal
290a430fa06SMiquel Raynal /* DMA Channel Configuration */
291a430fa06SMiquel Raynal config = (read ? DMAC_CHAN_FLOW_D_P2M : DMAC_CHAN_FLOW_D_M2P) |
292a430fa06SMiquel Raynal (read ? DMAC_DEST_PERIP(0) : DMAC_DEST_PERIP(DMA_PERID_NAND1)) |
293a430fa06SMiquel Raynal (read ? DMAC_SRC_PERIP(DMA_PERID_NAND1) : DMAC_SRC_PERIP(0)) |
294a430fa06SMiquel Raynal DMAC_CHAN_ENABLE;
295a430fa06SMiquel Raynal
296a430fa06SMiquel Raynal /* Prepare DMA descriptors */
297a430fa06SMiquel Raynal lpc32xx_nand_dma_configure(chip, buf, len, read);
298a430fa06SMiquel Raynal
299a430fa06SMiquel Raynal /* Setup SLC controller and start transfer */
300a430fa06SMiquel Raynal if (read)
301a430fa06SMiquel Raynal setbits_le32(&lpc32xx_nand_slc_regs->cfg, CFG_DMA_DIR);
302a430fa06SMiquel Raynal else /* NAND_ECC_WRITE */
303a430fa06SMiquel Raynal clrbits_le32(&lpc32xx_nand_slc_regs->cfg, CFG_DMA_DIR);
304a430fa06SMiquel Raynal setbits_le32(&lpc32xx_nand_slc_regs->cfg, CFG_DMA_BURST);
305a430fa06SMiquel Raynal
306a430fa06SMiquel Raynal /* Write length for new transfers */
307a430fa06SMiquel Raynal if (!((readl(&lpc32xx_nand_slc_regs->stat) & STAT_DMA_FIFO) |
308a430fa06SMiquel Raynal readl(&lpc32xx_nand_slc_regs->tc))) {
309a430fa06SMiquel Raynal int tmp = (len != mtd->oobsize) ? mtd->oobsize : 0;
310a430fa06SMiquel Raynal writel(len + tmp, &lpc32xx_nand_slc_regs->tc);
311a430fa06SMiquel Raynal }
312a430fa06SMiquel Raynal
313a430fa06SMiquel Raynal setbits_le32(&lpc32xx_nand_slc_regs->ctrl, CTRL_DMA_START);
314a430fa06SMiquel Raynal
315a430fa06SMiquel Raynal /* Start DMA transfers */
316a430fa06SMiquel Raynal ret = lpc32xx_dma_start_xfer(dmachan, dmalist, config);
317a430fa06SMiquel Raynal if (unlikely(ret < 0))
318a430fa06SMiquel Raynal BUG();
319a430fa06SMiquel Raynal
320a430fa06SMiquel Raynal /* Wait for NAND to be ready */
321a430fa06SMiquel Raynal while (!lpc32xx_nand_dev_ready(mtd))
322a430fa06SMiquel Raynal ;
323a430fa06SMiquel Raynal
324a430fa06SMiquel Raynal /* Wait till DMA transfer is DONE */
325a430fa06SMiquel Raynal if (lpc32xx_dma_wait_status(dmachan))
326a430fa06SMiquel Raynal pr_err("NAND DMA transfer error!\r\n");
327a430fa06SMiquel Raynal
328a430fa06SMiquel Raynal /* Stop DMA & HW ECC */
329a430fa06SMiquel Raynal clrbits_le32(&lpc32xx_nand_slc_regs->ctrl, CTRL_DMA_START);
330a430fa06SMiquel Raynal clrbits_le32(&lpc32xx_nand_slc_regs->cfg,
331a430fa06SMiquel Raynal CFG_DMA_DIR | CFG_DMA_BURST | CFG_ECC_EN | CFG_DMA_ECC);
332a430fa06SMiquel Raynal }
333a430fa06SMiquel Raynal
slc_ecc_copy_to_buffer(u8 * spare,const u32 * ecc,int count)334a430fa06SMiquel Raynal static u32 slc_ecc_copy_to_buffer(u8 *spare, const u32 *ecc, int count)
335a430fa06SMiquel Raynal {
336a430fa06SMiquel Raynal int i;
337a430fa06SMiquel Raynal for (i = 0; i < (count * CONFIG_SYS_NAND_ECCBYTES);
338a430fa06SMiquel Raynal i += CONFIG_SYS_NAND_ECCBYTES) {
339a430fa06SMiquel Raynal u32 ce = ecc[i / CONFIG_SYS_NAND_ECCBYTES];
340a430fa06SMiquel Raynal ce = ~(ce << 2) & 0xFFFFFF;
341a430fa06SMiquel Raynal spare[i+2] = (u8)(ce & 0xFF); ce >>= 8;
342a430fa06SMiquel Raynal spare[i+1] = (u8)(ce & 0xFF); ce >>= 8;
343a430fa06SMiquel Raynal spare[i] = (u8)(ce & 0xFF);
344a430fa06SMiquel Raynal }
345a430fa06SMiquel Raynal return 0;
346a430fa06SMiquel Raynal }
347a430fa06SMiquel Raynal
lpc32xx_ecc_calculate(struct mtd_info * mtd,const uint8_t * dat,uint8_t * ecc_code)348a430fa06SMiquel Raynal static int lpc32xx_ecc_calculate(struct mtd_info *mtd, const uint8_t *dat,
349a430fa06SMiquel Raynal uint8_t *ecc_code)
350a430fa06SMiquel Raynal {
351a430fa06SMiquel Raynal return slc_ecc_copy_to_buffer(ecc_code, ecc_buffer, ECCSTEPS);
352a430fa06SMiquel Raynal }
353a430fa06SMiquel Raynal
354a430fa06SMiquel Raynal /*
355a430fa06SMiquel Raynal * Enables and prepares SLC NAND controller
356a430fa06SMiquel Raynal * for doing data transfers with H/W ECC enabled.
357a430fa06SMiquel Raynal */
lpc32xx_hwecc_enable(struct mtd_info * mtd,int mode)358a430fa06SMiquel Raynal static void lpc32xx_hwecc_enable(struct mtd_info *mtd, int mode)
359a430fa06SMiquel Raynal {
360a430fa06SMiquel Raynal /* Clear ECC */
361a430fa06SMiquel Raynal writel(CTRL_ECC_CLEAR, &lpc32xx_nand_slc_regs->ctrl);
362a430fa06SMiquel Raynal
363a430fa06SMiquel Raynal /* Setup SLC controller for H/W ECC operations */
364a430fa06SMiquel Raynal setbits_le32(&lpc32xx_nand_slc_regs->cfg, CFG_ECC_EN | CFG_DMA_ECC);
365a430fa06SMiquel Raynal }
366a430fa06SMiquel Raynal
367a430fa06SMiquel Raynal /*
368a430fa06SMiquel Raynal * lpc32xx_correct_data - [NAND Interface] Detect and correct bit error(s)
369a430fa06SMiquel Raynal * mtd: MTD block structure
370a430fa06SMiquel Raynal * dat: raw data read from the chip
371a430fa06SMiquel Raynal * read_ecc: ECC from the chip
372a430fa06SMiquel Raynal * calc_ecc: the ECC calculated from raw data
373a430fa06SMiquel Raynal *
374a430fa06SMiquel Raynal * Detect and correct a 1 bit error for 256 byte block
375a430fa06SMiquel Raynal */
lpc32xx_correct_data(struct mtd_info * mtd,u_char * dat,u_char * read_ecc,u_char * calc_ecc)376a430fa06SMiquel Raynal int lpc32xx_correct_data(struct mtd_info *mtd, u_char *dat,
377a430fa06SMiquel Raynal u_char *read_ecc, u_char *calc_ecc)
378a430fa06SMiquel Raynal {
379a430fa06SMiquel Raynal unsigned int i;
380a430fa06SMiquel Raynal int ret1, ret2 = 0;
381a430fa06SMiquel Raynal u_char *r = read_ecc;
382a430fa06SMiquel Raynal u_char *c = calc_ecc;
383a430fa06SMiquel Raynal u16 data_offset = 0;
384a430fa06SMiquel Raynal
385a430fa06SMiquel Raynal for (i = 0 ; i < ECCSTEPS ; i++) {
386a430fa06SMiquel Raynal r += CONFIG_SYS_NAND_ECCBYTES;
387a430fa06SMiquel Raynal c += CONFIG_SYS_NAND_ECCBYTES;
388a430fa06SMiquel Raynal data_offset += CONFIG_SYS_NAND_ECCSIZE;
389a430fa06SMiquel Raynal
390a430fa06SMiquel Raynal ret1 = nand_correct_data(mtd, dat + data_offset, r, c);
391a430fa06SMiquel Raynal if (ret1 < 0)
392a430fa06SMiquel Raynal return -EBADMSG;
393a430fa06SMiquel Raynal else
394a430fa06SMiquel Raynal ret2 += ret1;
395a430fa06SMiquel Raynal }
396a430fa06SMiquel Raynal
397a430fa06SMiquel Raynal return ret2;
398a430fa06SMiquel Raynal }
399a430fa06SMiquel Raynal
lpc32xx_dma_read_buf(struct mtd_info * mtd,uint8_t * buf,int len)400a430fa06SMiquel Raynal static void lpc32xx_dma_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
401a430fa06SMiquel Raynal {
402a430fa06SMiquel Raynal lpc32xx_nand_xfer(mtd, buf, len, 1);
403a430fa06SMiquel Raynal }
404a430fa06SMiquel Raynal
lpc32xx_dma_write_buf(struct mtd_info * mtd,const uint8_t * buf,int len)405a430fa06SMiquel Raynal static void lpc32xx_dma_write_buf(struct mtd_info *mtd, const uint8_t *buf,
406a430fa06SMiquel Raynal int len)
407a430fa06SMiquel Raynal {
408a430fa06SMiquel Raynal lpc32xx_nand_xfer(mtd, buf, len, 0);
409a430fa06SMiquel Raynal }
410a430fa06SMiquel Raynal
411a430fa06SMiquel Raynal /* Reuse the logic from "nand_read_page_hwecc()" */
lpc32xx_read_page_hwecc(struct mtd_info * mtd,struct nand_chip * chip,uint8_t * buf,int oob_required,int page)412a430fa06SMiquel Raynal static int lpc32xx_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
413a430fa06SMiquel Raynal uint8_t *buf, int oob_required, int page)
414a430fa06SMiquel Raynal {
415a430fa06SMiquel Raynal int i;
416a430fa06SMiquel Raynal int stat;
417a430fa06SMiquel Raynal uint8_t *p = buf;
418a430fa06SMiquel Raynal uint8_t *ecc_calc = chip->buffers->ecccalc;
419a430fa06SMiquel Raynal uint8_t *ecc_code = chip->buffers->ecccode;
420a430fa06SMiquel Raynal uint32_t *eccpos = chip->ecc.layout->eccpos;
421a430fa06SMiquel Raynal unsigned int max_bitflips = 0;
422a430fa06SMiquel Raynal
423a430fa06SMiquel Raynal /*
424a430fa06SMiquel Raynal * As per the "LPC32x0 and LPC32x0/01 User manual" table 173 notes
425a430fa06SMiquel Raynal * and section 9.7, the NAND SLC & DMA allowed single DMA transaction
426a430fa06SMiquel Raynal * of a page size using DMA controller scatter/gather mode through
427a430fa06SMiquel Raynal * linked list; the ECC read is done without any software intervention.
428a430fa06SMiquel Raynal */
429a430fa06SMiquel Raynal
430a430fa06SMiquel Raynal lpc32xx_hwecc_enable(mtd, NAND_ECC_READ);
431a430fa06SMiquel Raynal lpc32xx_dma_read_buf(mtd, p, chip->ecc.size * chip->ecc.steps);
432a430fa06SMiquel Raynal lpc32xx_ecc_calculate(mtd, p, &ecc_calc[0]);
433a430fa06SMiquel Raynal lpc32xx_dma_read_buf(mtd, chip->oob_poi, mtd->oobsize);
434a430fa06SMiquel Raynal
435a430fa06SMiquel Raynal for (i = 0; i < chip->ecc.total; i++)
436a430fa06SMiquel Raynal ecc_code[i] = chip->oob_poi[eccpos[i]];
437a430fa06SMiquel Raynal
438a430fa06SMiquel Raynal stat = chip->ecc.correct(mtd, p, &ecc_code[0], &ecc_calc[0]);
439a430fa06SMiquel Raynal if (stat < 0)
440a430fa06SMiquel Raynal mtd->ecc_stats.failed++;
441a430fa06SMiquel Raynal else {
442a430fa06SMiquel Raynal mtd->ecc_stats.corrected += stat;
443a430fa06SMiquel Raynal max_bitflips = max_t(unsigned int, max_bitflips, stat);
444a430fa06SMiquel Raynal }
445a430fa06SMiquel Raynal
446a430fa06SMiquel Raynal return max_bitflips;
447a430fa06SMiquel Raynal }
448a430fa06SMiquel Raynal
449a430fa06SMiquel Raynal /* Reuse the logic from "nand_write_page_hwecc()" */
lpc32xx_write_page_hwecc(struct mtd_info * mtd,struct nand_chip * chip,const uint8_t * buf,int oob_required,int page)450a430fa06SMiquel Raynal static int lpc32xx_write_page_hwecc(struct mtd_info *mtd,
451a430fa06SMiquel Raynal struct nand_chip *chip,
452a430fa06SMiquel Raynal const uint8_t *buf, int oob_required,
453a430fa06SMiquel Raynal int page)
454a430fa06SMiquel Raynal {
455a430fa06SMiquel Raynal int i;
456a430fa06SMiquel Raynal uint8_t *ecc_calc = chip->buffers->ecccalc;
457a430fa06SMiquel Raynal const uint8_t *p = buf;
458a430fa06SMiquel Raynal uint32_t *eccpos = chip->ecc.layout->eccpos;
459a430fa06SMiquel Raynal
460a430fa06SMiquel Raynal /*
461a430fa06SMiquel Raynal * As per the "LPC32x0 and LPC32x0/01 User manual" table 173 notes
462a430fa06SMiquel Raynal * and section 9.7, the NAND SLC & DMA allowed single DMA transaction
463a430fa06SMiquel Raynal * of a page size using DMA controller scatter/gather mode through
464a430fa06SMiquel Raynal * linked list; the ECC read is done without any software intervention.
465a430fa06SMiquel Raynal */
466a430fa06SMiquel Raynal
467a430fa06SMiquel Raynal lpc32xx_hwecc_enable(mtd, NAND_ECC_WRITE);
468a430fa06SMiquel Raynal lpc32xx_dma_write_buf(mtd, p, chip->ecc.size * chip->ecc.steps);
469a430fa06SMiquel Raynal lpc32xx_ecc_calculate(mtd, p, &ecc_calc[0]);
470a430fa06SMiquel Raynal
471a430fa06SMiquel Raynal for (i = 0; i < chip->ecc.total; i++)
472a430fa06SMiquel Raynal chip->oob_poi[eccpos[i]] = ecc_calc[i];
473a430fa06SMiquel Raynal
474a430fa06SMiquel Raynal lpc32xx_dma_write_buf(mtd, chip->oob_poi, mtd->oobsize);
475a430fa06SMiquel Raynal
476a430fa06SMiquel Raynal return 0;
477a430fa06SMiquel Raynal }
478*44cdfc0eSVladimir Zapolskiy #else
lpc32xx_read_buf(struct mtd_info * mtd,uint8_t * buf,int len)479*44cdfc0eSVladimir Zapolskiy static void lpc32xx_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
480*44cdfc0eSVladimir Zapolskiy {
481*44cdfc0eSVladimir Zapolskiy while (len-- > 0)
482*44cdfc0eSVladimir Zapolskiy *buf++ = readl(&lpc32xx_nand_slc_regs->data);
483*44cdfc0eSVladimir Zapolskiy }
484*44cdfc0eSVladimir Zapolskiy
lpc32xx_write_buf(struct mtd_info * mtd,const uint8_t * buf,int len)485*44cdfc0eSVladimir Zapolskiy static void lpc32xx_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
486*44cdfc0eSVladimir Zapolskiy {
487*44cdfc0eSVladimir Zapolskiy while (len-- > 0)
488*44cdfc0eSVladimir Zapolskiy writel(*buf++, &lpc32xx_nand_slc_regs->data);
489*44cdfc0eSVladimir Zapolskiy }
490a430fa06SMiquel Raynal #endif
491a430fa06SMiquel Raynal
lpc32xx_read_byte(struct mtd_info * mtd)492*44cdfc0eSVladimir Zapolskiy static uint8_t lpc32xx_read_byte(struct mtd_info *mtd)
493*44cdfc0eSVladimir Zapolskiy {
494*44cdfc0eSVladimir Zapolskiy return readl(&lpc32xx_nand_slc_regs->data);
495*44cdfc0eSVladimir Zapolskiy }
496*44cdfc0eSVladimir Zapolskiy
lpc32xx_write_byte(struct mtd_info * mtd,uint8_t byte)497*44cdfc0eSVladimir Zapolskiy static void lpc32xx_write_byte(struct mtd_info *mtd, uint8_t byte)
498*44cdfc0eSVladimir Zapolskiy {
499*44cdfc0eSVladimir Zapolskiy writel(byte, &lpc32xx_nand_slc_regs->data);
500*44cdfc0eSVladimir Zapolskiy }
501*44cdfc0eSVladimir Zapolskiy
502a430fa06SMiquel Raynal /*
503a430fa06SMiquel Raynal * LPC32xx has only one SLC NAND controller, don't utilize
504a430fa06SMiquel Raynal * CONFIG_SYS_NAND_SELF_INIT to be able to reuse this function
505a430fa06SMiquel Raynal * both in SPL NAND and U-Boot images.
506a430fa06SMiquel Raynal */
board_nand_init(struct nand_chip * lpc32xx_chip)507a430fa06SMiquel Raynal int board_nand_init(struct nand_chip *lpc32xx_chip)
508a430fa06SMiquel Raynal {
509*44cdfc0eSVladimir Zapolskiy #if defined(CONFIG_DMA_LPC32XX) && !defined(CONFIG_SPL_BUILD)
510a430fa06SMiquel Raynal int ret;
511a430fa06SMiquel Raynal
512a430fa06SMiquel Raynal /* Acquire a channel for our use */
513a430fa06SMiquel Raynal ret = lpc32xx_dma_get_channel();
514a430fa06SMiquel Raynal if (unlikely(ret < 0)) {
515a430fa06SMiquel Raynal pr_info("Unable to get free DMA channel for NAND transfers\n");
516a430fa06SMiquel Raynal return -1;
517a430fa06SMiquel Raynal }
518a430fa06SMiquel Raynal dmachan = (unsigned int)ret;
519a430fa06SMiquel Raynal #endif
520a430fa06SMiquel Raynal
521a430fa06SMiquel Raynal lpc32xx_chip->cmd_ctrl = lpc32xx_nand_cmd_ctrl;
522a430fa06SMiquel Raynal lpc32xx_chip->dev_ready = lpc32xx_nand_dev_ready;
523a430fa06SMiquel Raynal
524a430fa06SMiquel Raynal /*
525a430fa06SMiquel Raynal * The implementation of these functions is quite common, but
526a430fa06SMiquel Raynal * they MUST be defined, because access to data register
527a430fa06SMiquel Raynal * is strictly 32-bit aligned.
528a430fa06SMiquel Raynal */
529a430fa06SMiquel Raynal lpc32xx_chip->read_byte = lpc32xx_read_byte;
530a430fa06SMiquel Raynal lpc32xx_chip->write_byte = lpc32xx_write_byte;
531a430fa06SMiquel Raynal
532*44cdfc0eSVladimir Zapolskiy #if defined(CONFIG_DMA_LPC32XX) && !defined(CONFIG_SPL_BUILD)
533a430fa06SMiquel Raynal /* Hardware ECC calculation is supported when DMA driver is selected */
534a430fa06SMiquel Raynal lpc32xx_chip->ecc.mode = NAND_ECC_HW;
535a430fa06SMiquel Raynal
536a430fa06SMiquel Raynal lpc32xx_chip->read_buf = lpc32xx_dma_read_buf;
537a430fa06SMiquel Raynal lpc32xx_chip->write_buf = lpc32xx_dma_write_buf;
538a430fa06SMiquel Raynal
539a430fa06SMiquel Raynal lpc32xx_chip->ecc.calculate = lpc32xx_ecc_calculate;
540a430fa06SMiquel Raynal lpc32xx_chip->ecc.correct = lpc32xx_correct_data;
541a430fa06SMiquel Raynal lpc32xx_chip->ecc.hwctl = lpc32xx_hwecc_enable;
542a430fa06SMiquel Raynal lpc32xx_chip->chip_delay = 2000;
543a430fa06SMiquel Raynal
544a430fa06SMiquel Raynal lpc32xx_chip->ecc.read_page = lpc32xx_read_page_hwecc;
545a430fa06SMiquel Raynal lpc32xx_chip->ecc.write_page = lpc32xx_write_page_hwecc;
546a430fa06SMiquel Raynal lpc32xx_chip->options |= NAND_NO_SUBPAGE_WRITE;
547a430fa06SMiquel Raynal #else
548a430fa06SMiquel Raynal /*
549a430fa06SMiquel Raynal * Hardware ECC calculation is not supported by the driver,
550a430fa06SMiquel Raynal * because it requires DMA support, see LPC32x0 User Manual,
551a430fa06SMiquel Raynal * note after SLC_ECC register description (UM10326, p.198)
552a430fa06SMiquel Raynal */
553a430fa06SMiquel Raynal lpc32xx_chip->ecc.mode = NAND_ECC_SOFT;
554a430fa06SMiquel Raynal
555a430fa06SMiquel Raynal /*
556a430fa06SMiquel Raynal * The implementation of these functions is quite common, but
557a430fa06SMiquel Raynal * they MUST be defined, because access to data register
558a430fa06SMiquel Raynal * is strictly 32-bit aligned.
559a430fa06SMiquel Raynal */
560a430fa06SMiquel Raynal lpc32xx_chip->read_buf = lpc32xx_read_buf;
561a430fa06SMiquel Raynal lpc32xx_chip->write_buf = lpc32xx_write_buf;
562a430fa06SMiquel Raynal #endif
563a430fa06SMiquel Raynal
564a430fa06SMiquel Raynal /*
565a430fa06SMiquel Raynal * These values are predefined
566a430fa06SMiquel Raynal * for both small and large page NAND flash devices.
567a430fa06SMiquel Raynal */
568a430fa06SMiquel Raynal lpc32xx_chip->ecc.size = CONFIG_SYS_NAND_ECCSIZE;
569a430fa06SMiquel Raynal lpc32xx_chip->ecc.bytes = CONFIG_SYS_NAND_ECCBYTES;
570a430fa06SMiquel Raynal lpc32xx_chip->ecc.strength = 1;
571a430fa06SMiquel Raynal
572a430fa06SMiquel Raynal if (CONFIG_SYS_NAND_PAGE_SIZE != NAND_LARGE_BLOCK_PAGE_SIZE)
573a430fa06SMiquel Raynal lpc32xx_chip->ecc.layout = &lpc32xx_nand_oob_16;
574a430fa06SMiquel Raynal
575a430fa06SMiquel Raynal #if defined(CONFIG_SYS_NAND_USE_FLASH_BBT)
576a430fa06SMiquel Raynal lpc32xx_chip->bbt_options |= NAND_BBT_USE_FLASH;
577a430fa06SMiquel Raynal #endif
578a430fa06SMiquel Raynal
579a430fa06SMiquel Raynal /* Initialize NAND interface */
580a430fa06SMiquel Raynal lpc32xx_nand_init();
581a430fa06SMiquel Raynal
582a430fa06SMiquel Raynal return 0;
583a430fa06SMiquel Raynal }
584