1*cfcc706cSMiquel Raynal /*
2*cfcc706cSMiquel Raynal * (C) Copyright 2016 Xilinx, Inc.
3*cfcc706cSMiquel Raynal *
4*cfcc706cSMiquel Raynal * Xilinx Zynq NAND Flash Controller Driver
5*cfcc706cSMiquel Raynal * This driver is based on plat_nand.c and mxc_nand.c drivers
6*cfcc706cSMiquel Raynal *
7*cfcc706cSMiquel Raynal * SPDX-License-Identifier: GPL-2.0+
8*cfcc706cSMiquel Raynal */
9*cfcc706cSMiquel Raynal
10*cfcc706cSMiquel Raynal #include <common.h>
11*cfcc706cSMiquel Raynal #include <malloc.h>
12*cfcc706cSMiquel Raynal #include <asm/io.h>
13*cfcc706cSMiquel Raynal #include <linux/errno.h>
14*cfcc706cSMiquel Raynal #include <nand.h>
15*cfcc706cSMiquel Raynal #include <linux/mtd/mtd.h>
16*cfcc706cSMiquel Raynal #include <linux/mtd/rawnand.h>
17*cfcc706cSMiquel Raynal #include <linux/mtd/partitions.h>
18*cfcc706cSMiquel Raynal #include <linux/mtd/nand_ecc.h>
19*cfcc706cSMiquel Raynal #include <asm/arch/hardware.h>
20*cfcc706cSMiquel Raynal #include <asm/arch/sys_proto.h>
21*cfcc706cSMiquel Raynal
22*cfcc706cSMiquel Raynal /* The NAND flash driver defines */
23*cfcc706cSMiquel Raynal #define ZYNQ_NAND_CMD_PHASE 1
24*cfcc706cSMiquel Raynal #define ZYNQ_NAND_DATA_PHASE 2
25*cfcc706cSMiquel Raynal #define ZYNQ_NAND_ECC_SIZE 512
26*cfcc706cSMiquel Raynal #define ZYNQ_NAND_SET_OPMODE_8BIT (0 << 0)
27*cfcc706cSMiquel Raynal #define ZYNQ_NAND_SET_OPMODE_16BIT (1 << 0)
28*cfcc706cSMiquel Raynal #define ZYNQ_NAND_ECC_STATUS (1 << 6)
29*cfcc706cSMiquel Raynal #define ZYNQ_MEMC_CLRCR_INT_CLR1 (1 << 4)
30*cfcc706cSMiquel Raynal #define ZYNQ_MEMC_SR_RAW_INT_ST1 (1 << 6)
31*cfcc706cSMiquel Raynal #define ZYNQ_MEMC_SR_INT_ST1 (1 << 4)
32*cfcc706cSMiquel Raynal #define ZYNQ_MEMC_NAND_ECC_MODE_MASK 0xC
33*cfcc706cSMiquel Raynal
34*cfcc706cSMiquel Raynal /* Flash memory controller operating parameters */
35*cfcc706cSMiquel Raynal #define ZYNQ_NAND_CLR_CONFIG ((0x1 << 1) | /* Disable interrupt */ \
36*cfcc706cSMiquel Raynal (0x1 << 4) | /* Clear interrupt */ \
37*cfcc706cSMiquel Raynal (0x1 << 6)) /* Disable ECC interrupt */
38*cfcc706cSMiquel Raynal
39*cfcc706cSMiquel Raynal #ifndef CONFIG_NAND_ZYNQ_USE_BOOTLOADER1_TIMINGS
40*cfcc706cSMiquel Raynal
41*cfcc706cSMiquel Raynal /* Assuming 50MHz clock (20ns cycle time) and 3V operation */
42*cfcc706cSMiquel Raynal #define ZYNQ_NAND_SET_CYCLES ((0x2 << 20) | /* t_rr from nand_cycles */ \
43*cfcc706cSMiquel Raynal (0x2 << 17) | /* t_ar from nand_cycles */ \
44*cfcc706cSMiquel Raynal (0x1 << 14) | /* t_clr from nand_cycles */ \
45*cfcc706cSMiquel Raynal (0x3 << 11) | /* t_wp from nand_cycles */ \
46*cfcc706cSMiquel Raynal (0x2 << 8) | /* t_rea from nand_cycles */ \
47*cfcc706cSMiquel Raynal (0x5 << 4) | /* t_wc from nand_cycles */ \
48*cfcc706cSMiquel Raynal (0x5 << 0)) /* t_rc from nand_cycles */
49*cfcc706cSMiquel Raynal #endif
50*cfcc706cSMiquel Raynal
51*cfcc706cSMiquel Raynal
52*cfcc706cSMiquel Raynal #define ZYNQ_NAND_DIRECT_CMD ((0x4 << 23) | /* Chip 0 from interface 1 */ \
53*cfcc706cSMiquel Raynal (0x2 << 21)) /* UpdateRegs operation */
54*cfcc706cSMiquel Raynal
55*cfcc706cSMiquel Raynal #define ZYNQ_NAND_ECC_CONFIG ((0x1 << 2) | /* ECC available on APB */ \
56*cfcc706cSMiquel Raynal (0x1 << 4) | /* ECC read at end of page */ \
57*cfcc706cSMiquel Raynal (0x0 << 5)) /* No Jumping */
58*cfcc706cSMiquel Raynal
59*cfcc706cSMiquel Raynal #define ZYNQ_NAND_ECC_CMD1 ((0x80) | /* Write command */ \
60*cfcc706cSMiquel Raynal (0x00 << 8) | /* Read command */ \
61*cfcc706cSMiquel Raynal (0x30 << 16) | /* Read End command */ \
62*cfcc706cSMiquel Raynal (0x1 << 24)) /* Read End command calid */
63*cfcc706cSMiquel Raynal
64*cfcc706cSMiquel Raynal #define ZYNQ_NAND_ECC_CMD2 ((0x85) | /* Write col change cmd */ \
65*cfcc706cSMiquel Raynal (0x05 << 8) | /* Read col change cmd */ \
66*cfcc706cSMiquel Raynal (0xE0 << 16) | /* Read col change end cmd */ \
67*cfcc706cSMiquel Raynal (0x1 << 24)) /* Read col change
68*cfcc706cSMiquel Raynal end cmd valid */
69*cfcc706cSMiquel Raynal /* AXI Address definitions */
70*cfcc706cSMiquel Raynal #define START_CMD_SHIFT 3
71*cfcc706cSMiquel Raynal #define END_CMD_SHIFT 11
72*cfcc706cSMiquel Raynal #define END_CMD_VALID_SHIFT 20
73*cfcc706cSMiquel Raynal #define ADDR_CYCLES_SHIFT 21
74*cfcc706cSMiquel Raynal #define CLEAR_CS_SHIFT 21
75*cfcc706cSMiquel Raynal #define ECC_LAST_SHIFT 10
76*cfcc706cSMiquel Raynal #define COMMAND_PHASE (0 << 19)
77*cfcc706cSMiquel Raynal #define DATA_PHASE (1 << 19)
78*cfcc706cSMiquel Raynal #define ONDIE_ECC_FEATURE_ADDR 0x90
79*cfcc706cSMiquel Raynal #define ONDIE_ECC_FEATURE_ENABLE 0x08
80*cfcc706cSMiquel Raynal
81*cfcc706cSMiquel Raynal #define ZYNQ_NAND_ECC_LAST (1 << ECC_LAST_SHIFT) /* Set ECC_Last */
82*cfcc706cSMiquel Raynal #define ZYNQ_NAND_CLEAR_CS (1 << CLEAR_CS_SHIFT) /* Clear chip select */
83*cfcc706cSMiquel Raynal
84*cfcc706cSMiquel Raynal /* ECC block registers bit position and bit mask */
85*cfcc706cSMiquel Raynal #define ZYNQ_NAND_ECC_BUSY (1 << 6) /* ECC block is busy */
86*cfcc706cSMiquel Raynal #define ZYNQ_NAND_ECC_MASK 0x00FFFFFF /* ECC value mask */
87*cfcc706cSMiquel Raynal
88*cfcc706cSMiquel Raynal #define ZYNQ_NAND_ROW_ADDR_CYCL_MASK 0x0F
89*cfcc706cSMiquel Raynal #define ZYNQ_NAND_COL_ADDR_CYCL_MASK 0xF0
90*cfcc706cSMiquel Raynal
91*cfcc706cSMiquel Raynal #define ZYNQ_NAND_MIO_NUM_NAND_8BIT 13
92*cfcc706cSMiquel Raynal #define ZYNQ_NAND_MIO_NUM_NAND_16BIT 8
93*cfcc706cSMiquel Raynal
94*cfcc706cSMiquel Raynal enum zynq_nand_bus_width {
95*cfcc706cSMiquel Raynal NAND_BW_UNKNOWN = -1,
96*cfcc706cSMiquel Raynal NAND_BW_8BIT,
97*cfcc706cSMiquel Raynal NAND_BW_16BIT,
98*cfcc706cSMiquel Raynal };
99*cfcc706cSMiquel Raynal
100*cfcc706cSMiquel Raynal #ifndef NAND_CMD_LOCK_TIGHT
101*cfcc706cSMiquel Raynal #define NAND_CMD_LOCK_TIGHT 0x2c
102*cfcc706cSMiquel Raynal #endif
103*cfcc706cSMiquel Raynal
104*cfcc706cSMiquel Raynal #ifndef NAND_CMD_LOCK_STATUS
105*cfcc706cSMiquel Raynal #define NAND_CMD_LOCK_STATUS 0x7a
106*cfcc706cSMiquel Raynal #endif
107*cfcc706cSMiquel Raynal
108*cfcc706cSMiquel Raynal /* SMC register set */
109*cfcc706cSMiquel Raynal struct zynq_nand_smc_regs {
110*cfcc706cSMiquel Raynal u32 csr; /* 0x00 */
111*cfcc706cSMiquel Raynal u32 reserved0[2];
112*cfcc706cSMiquel Raynal u32 cfr; /* 0x0C */
113*cfcc706cSMiquel Raynal u32 dcr; /* 0x10 */
114*cfcc706cSMiquel Raynal u32 scr; /* 0x14 */
115*cfcc706cSMiquel Raynal u32 sor; /* 0x18 */
116*cfcc706cSMiquel Raynal u32 reserved1[249];
117*cfcc706cSMiquel Raynal u32 esr; /* 0x400 */
118*cfcc706cSMiquel Raynal u32 emcr; /* 0x404 */
119*cfcc706cSMiquel Raynal u32 emcmd1r; /* 0x408 */
120*cfcc706cSMiquel Raynal u32 emcmd2r; /* 0x40C */
121*cfcc706cSMiquel Raynal u32 reserved2[2];
122*cfcc706cSMiquel Raynal u32 eval0r; /* 0x418 */
123*cfcc706cSMiquel Raynal };
124*cfcc706cSMiquel Raynal #define zynq_nand_smc_base ((struct zynq_nand_smc_regs __iomem *)\
125*cfcc706cSMiquel Raynal ZYNQ_SMC_BASEADDR)
126*cfcc706cSMiquel Raynal
127*cfcc706cSMiquel Raynal /*
128*cfcc706cSMiquel Raynal * struct zynq_nand_info - Defines the NAND flash driver instance
129*cfcc706cSMiquel Raynal * @parts: Pointer to the mtd_partition structure
130*cfcc706cSMiquel Raynal * @nand_base: Virtual address of the NAND flash device
131*cfcc706cSMiquel Raynal * @end_cmd_pending: End command is pending
132*cfcc706cSMiquel Raynal * @end_cmd: End command
133*cfcc706cSMiquel Raynal */
134*cfcc706cSMiquel Raynal struct zynq_nand_info {
135*cfcc706cSMiquel Raynal void __iomem *nand_base;
136*cfcc706cSMiquel Raynal u8 end_cmd_pending;
137*cfcc706cSMiquel Raynal u8 end_cmd;
138*cfcc706cSMiquel Raynal };
139*cfcc706cSMiquel Raynal
140*cfcc706cSMiquel Raynal /*
141*cfcc706cSMiquel Raynal * struct zynq_nand_command_format - Defines NAND flash command format
142*cfcc706cSMiquel Raynal * @start_cmd: First cycle command (Start command)
143*cfcc706cSMiquel Raynal * @end_cmd: Second cycle command (Last command)
144*cfcc706cSMiquel Raynal * @addr_cycles: Number of address cycles required to send the address
145*cfcc706cSMiquel Raynal * @end_cmd_valid: The second cycle command is valid for cmd or data phase
146*cfcc706cSMiquel Raynal */
147*cfcc706cSMiquel Raynal struct zynq_nand_command_format {
148*cfcc706cSMiquel Raynal u8 start_cmd;
149*cfcc706cSMiquel Raynal u8 end_cmd;
150*cfcc706cSMiquel Raynal u8 addr_cycles;
151*cfcc706cSMiquel Raynal u8 end_cmd_valid;
152*cfcc706cSMiquel Raynal };
153*cfcc706cSMiquel Raynal
154*cfcc706cSMiquel Raynal /* The NAND flash operations command format */
155*cfcc706cSMiquel Raynal static const struct zynq_nand_command_format zynq_nand_commands[] = {
156*cfcc706cSMiquel Raynal {NAND_CMD_READ0, NAND_CMD_READSTART, 5, ZYNQ_NAND_CMD_PHASE},
157*cfcc706cSMiquel Raynal {NAND_CMD_RNDOUT, NAND_CMD_RNDOUTSTART, 2, ZYNQ_NAND_CMD_PHASE},
158*cfcc706cSMiquel Raynal {NAND_CMD_READID, NAND_CMD_NONE, 1, 0},
159*cfcc706cSMiquel Raynal {NAND_CMD_STATUS, NAND_CMD_NONE, 0, 0},
160*cfcc706cSMiquel Raynal {NAND_CMD_SEQIN, NAND_CMD_PAGEPROG, 5, ZYNQ_NAND_DATA_PHASE},
161*cfcc706cSMiquel Raynal {NAND_CMD_RNDIN, NAND_CMD_NONE, 2, 0},
162*cfcc706cSMiquel Raynal {NAND_CMD_ERASE1, NAND_CMD_ERASE2, 3, ZYNQ_NAND_CMD_PHASE},
163*cfcc706cSMiquel Raynal {NAND_CMD_RESET, NAND_CMD_NONE, 0, 0},
164*cfcc706cSMiquel Raynal {NAND_CMD_PARAM, NAND_CMD_NONE, 1, 0},
165*cfcc706cSMiquel Raynal {NAND_CMD_GET_FEATURES, NAND_CMD_NONE, 1, 0},
166*cfcc706cSMiquel Raynal {NAND_CMD_SET_FEATURES, NAND_CMD_NONE, 1, 0},
167*cfcc706cSMiquel Raynal {NAND_CMD_LOCK, NAND_CMD_NONE, 0, 0},
168*cfcc706cSMiquel Raynal {NAND_CMD_LOCK_TIGHT, NAND_CMD_NONE, 0, 0},
169*cfcc706cSMiquel Raynal {NAND_CMD_UNLOCK1, NAND_CMD_NONE, 3, 0},
170*cfcc706cSMiquel Raynal {NAND_CMD_UNLOCK2, NAND_CMD_NONE, 3, 0},
171*cfcc706cSMiquel Raynal {NAND_CMD_LOCK_STATUS, NAND_CMD_NONE, 3, 0},
172*cfcc706cSMiquel Raynal {NAND_CMD_NONE, NAND_CMD_NONE, 0, 0},
173*cfcc706cSMiquel Raynal /* Add all the flash commands supported by the flash device */
174*cfcc706cSMiquel Raynal };
175*cfcc706cSMiquel Raynal
176*cfcc706cSMiquel Raynal /* Define default oob placement schemes for large and small page devices */
177*cfcc706cSMiquel Raynal static struct nand_ecclayout nand_oob_16 = {
178*cfcc706cSMiquel Raynal .eccbytes = 3,
179*cfcc706cSMiquel Raynal .eccpos = {0, 1, 2},
180*cfcc706cSMiquel Raynal .oobfree = {
181*cfcc706cSMiquel Raynal { .offset = 8, .length = 8 }
182*cfcc706cSMiquel Raynal }
183*cfcc706cSMiquel Raynal };
184*cfcc706cSMiquel Raynal
185*cfcc706cSMiquel Raynal static struct nand_ecclayout nand_oob_64 = {
186*cfcc706cSMiquel Raynal .eccbytes = 12,
187*cfcc706cSMiquel Raynal .eccpos = {
188*cfcc706cSMiquel Raynal 52, 53, 54, 55, 56, 57,
189*cfcc706cSMiquel Raynal 58, 59, 60, 61, 62, 63},
190*cfcc706cSMiquel Raynal .oobfree = {
191*cfcc706cSMiquel Raynal { .offset = 2, .length = 50 }
192*cfcc706cSMiquel Raynal }
193*cfcc706cSMiquel Raynal };
194*cfcc706cSMiquel Raynal
195*cfcc706cSMiquel Raynal static struct nand_ecclayout ondie_nand_oob_64 = {
196*cfcc706cSMiquel Raynal .eccbytes = 32,
197*cfcc706cSMiquel Raynal
198*cfcc706cSMiquel Raynal .eccpos = {
199*cfcc706cSMiquel Raynal 8, 9, 10, 11, 12, 13, 14, 15,
200*cfcc706cSMiquel Raynal 24, 25, 26, 27, 28, 29, 30, 31,
201*cfcc706cSMiquel Raynal 40, 41, 42, 43, 44, 45, 46, 47,
202*cfcc706cSMiquel Raynal 56, 57, 58, 59, 60, 61, 62, 63
203*cfcc706cSMiquel Raynal },
204*cfcc706cSMiquel Raynal
205*cfcc706cSMiquel Raynal .oobfree = {
206*cfcc706cSMiquel Raynal { .offset = 4, .length = 4 },
207*cfcc706cSMiquel Raynal { .offset = 20, .length = 4 },
208*cfcc706cSMiquel Raynal { .offset = 36, .length = 4 },
209*cfcc706cSMiquel Raynal { .offset = 52, .length = 4 }
210*cfcc706cSMiquel Raynal }
211*cfcc706cSMiquel Raynal };
212*cfcc706cSMiquel Raynal
213*cfcc706cSMiquel Raynal /* bbt decriptors for chips with on-die ECC and
214*cfcc706cSMiquel Raynal chips with 64-byte OOB */
215*cfcc706cSMiquel Raynal static u8 bbt_pattern[] = {'B', 'b', 't', '0' };
216*cfcc706cSMiquel Raynal static u8 mirror_pattern[] = {'1', 't', 'b', 'B' };
217*cfcc706cSMiquel Raynal
218*cfcc706cSMiquel Raynal static struct nand_bbt_descr bbt_main_descr = {
219*cfcc706cSMiquel Raynal .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
220*cfcc706cSMiquel Raynal NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
221*cfcc706cSMiquel Raynal .offs = 4,
222*cfcc706cSMiquel Raynal .len = 4,
223*cfcc706cSMiquel Raynal .veroffs = 20,
224*cfcc706cSMiquel Raynal .maxblocks = 4,
225*cfcc706cSMiquel Raynal .pattern = bbt_pattern
226*cfcc706cSMiquel Raynal };
227*cfcc706cSMiquel Raynal
228*cfcc706cSMiquel Raynal static struct nand_bbt_descr bbt_mirror_descr = {
229*cfcc706cSMiquel Raynal .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
230*cfcc706cSMiquel Raynal NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
231*cfcc706cSMiquel Raynal .offs = 4,
232*cfcc706cSMiquel Raynal .len = 4,
233*cfcc706cSMiquel Raynal .veroffs = 20,
234*cfcc706cSMiquel Raynal .maxblocks = 4,
235*cfcc706cSMiquel Raynal .pattern = mirror_pattern
236*cfcc706cSMiquel Raynal };
237*cfcc706cSMiquel Raynal
238*cfcc706cSMiquel Raynal /*
239*cfcc706cSMiquel Raynal * zynq_nand_waitfor_ecc_completion - Wait for ECC completion
240*cfcc706cSMiquel Raynal *
241*cfcc706cSMiquel Raynal * returns: status for command completion, -1 for Timeout
242*cfcc706cSMiquel Raynal */
zynq_nand_waitfor_ecc_completion(void)243*cfcc706cSMiquel Raynal static int zynq_nand_waitfor_ecc_completion(void)
244*cfcc706cSMiquel Raynal {
245*cfcc706cSMiquel Raynal unsigned long timeout;
246*cfcc706cSMiquel Raynal u32 status;
247*cfcc706cSMiquel Raynal
248*cfcc706cSMiquel Raynal /* Wait max 10us */
249*cfcc706cSMiquel Raynal timeout = 10;
250*cfcc706cSMiquel Raynal status = readl(&zynq_nand_smc_base->esr);
251*cfcc706cSMiquel Raynal while (status & ZYNQ_NAND_ECC_BUSY) {
252*cfcc706cSMiquel Raynal status = readl(&zynq_nand_smc_base->esr);
253*cfcc706cSMiquel Raynal if (timeout == 0)
254*cfcc706cSMiquel Raynal return -1;
255*cfcc706cSMiquel Raynal timeout--;
256*cfcc706cSMiquel Raynal udelay(1);
257*cfcc706cSMiquel Raynal }
258*cfcc706cSMiquel Raynal
259*cfcc706cSMiquel Raynal return status;
260*cfcc706cSMiquel Raynal }
261*cfcc706cSMiquel Raynal
262*cfcc706cSMiquel Raynal /*
263*cfcc706cSMiquel Raynal * zynq_nand_init_nand_flash - Initialize NAND controller
264*cfcc706cSMiquel Raynal * @option: Device property flags
265*cfcc706cSMiquel Raynal *
266*cfcc706cSMiquel Raynal * This function initializes the NAND flash interface on the NAND controller.
267*cfcc706cSMiquel Raynal *
268*cfcc706cSMiquel Raynal * returns: 0 on success or error value on failure
269*cfcc706cSMiquel Raynal */
zynq_nand_init_nand_flash(int option)270*cfcc706cSMiquel Raynal static int zynq_nand_init_nand_flash(int option)
271*cfcc706cSMiquel Raynal {
272*cfcc706cSMiquel Raynal u32 status;
273*cfcc706cSMiquel Raynal
274*cfcc706cSMiquel Raynal /* disable interrupts */
275*cfcc706cSMiquel Raynal writel(ZYNQ_NAND_CLR_CONFIG, &zynq_nand_smc_base->cfr);
276*cfcc706cSMiquel Raynal #ifndef CONFIG_NAND_ZYNQ_USE_BOOTLOADER1_TIMINGS
277*cfcc706cSMiquel Raynal /* Initialize the NAND interface by setting cycles and operation mode */
278*cfcc706cSMiquel Raynal writel(ZYNQ_NAND_SET_CYCLES, &zynq_nand_smc_base->scr);
279*cfcc706cSMiquel Raynal #endif
280*cfcc706cSMiquel Raynal if (option & NAND_BUSWIDTH_16)
281*cfcc706cSMiquel Raynal writel(ZYNQ_NAND_SET_OPMODE_16BIT, &zynq_nand_smc_base->sor);
282*cfcc706cSMiquel Raynal else
283*cfcc706cSMiquel Raynal writel(ZYNQ_NAND_SET_OPMODE_8BIT, &zynq_nand_smc_base->sor);
284*cfcc706cSMiquel Raynal
285*cfcc706cSMiquel Raynal writel(ZYNQ_NAND_DIRECT_CMD, &zynq_nand_smc_base->dcr);
286*cfcc706cSMiquel Raynal
287*cfcc706cSMiquel Raynal /* Wait till the ECC operation is complete */
288*cfcc706cSMiquel Raynal status = zynq_nand_waitfor_ecc_completion();
289*cfcc706cSMiquel Raynal if (status < 0) {
290*cfcc706cSMiquel Raynal printf("%s: Timeout\n", __func__);
291*cfcc706cSMiquel Raynal return status;
292*cfcc706cSMiquel Raynal }
293*cfcc706cSMiquel Raynal
294*cfcc706cSMiquel Raynal /* Set the command1 and command2 register */
295*cfcc706cSMiquel Raynal writel(ZYNQ_NAND_ECC_CMD1, &zynq_nand_smc_base->emcmd1r);
296*cfcc706cSMiquel Raynal writel(ZYNQ_NAND_ECC_CMD2, &zynq_nand_smc_base->emcmd2r);
297*cfcc706cSMiquel Raynal
298*cfcc706cSMiquel Raynal return 0;
299*cfcc706cSMiquel Raynal }
300*cfcc706cSMiquel Raynal
301*cfcc706cSMiquel Raynal /*
302*cfcc706cSMiquel Raynal * zynq_nand_calculate_hwecc - Calculate Hardware ECC
303*cfcc706cSMiquel Raynal * @mtd: Pointer to the mtd_info structure
304*cfcc706cSMiquel Raynal * @data: Pointer to the page data
305*cfcc706cSMiquel Raynal * @ecc_code: Pointer to the ECC buffer where ECC data needs to be stored
306*cfcc706cSMiquel Raynal *
307*cfcc706cSMiquel Raynal * This function retrieves the Hardware ECC data from the controller and returns
308*cfcc706cSMiquel Raynal * ECC data back to the MTD subsystem.
309*cfcc706cSMiquel Raynal *
310*cfcc706cSMiquel Raynal * returns: 0 on success or error value on failure
311*cfcc706cSMiquel Raynal */
zynq_nand_calculate_hwecc(struct mtd_info * mtd,const u8 * data,u8 * ecc_code)312*cfcc706cSMiquel Raynal static int zynq_nand_calculate_hwecc(struct mtd_info *mtd, const u8 *data,
313*cfcc706cSMiquel Raynal u8 *ecc_code)
314*cfcc706cSMiquel Raynal {
315*cfcc706cSMiquel Raynal u32 ecc_value = 0;
316*cfcc706cSMiquel Raynal u8 ecc_reg, ecc_byte;
317*cfcc706cSMiquel Raynal u32 ecc_status;
318*cfcc706cSMiquel Raynal
319*cfcc706cSMiquel Raynal /* Wait till the ECC operation is complete */
320*cfcc706cSMiquel Raynal ecc_status = zynq_nand_waitfor_ecc_completion();
321*cfcc706cSMiquel Raynal if (ecc_status < 0) {
322*cfcc706cSMiquel Raynal printf("%s: Timeout\n", __func__);
323*cfcc706cSMiquel Raynal return ecc_status;
324*cfcc706cSMiquel Raynal }
325*cfcc706cSMiquel Raynal
326*cfcc706cSMiquel Raynal for (ecc_reg = 0; ecc_reg < 4; ecc_reg++) {
327*cfcc706cSMiquel Raynal /* Read ECC value for each block */
328*cfcc706cSMiquel Raynal ecc_value = readl(&zynq_nand_smc_base->eval0r + ecc_reg);
329*cfcc706cSMiquel Raynal
330*cfcc706cSMiquel Raynal /* Get the ecc status from ecc read value */
331*cfcc706cSMiquel Raynal ecc_status = (ecc_value >> 24) & 0xFF;
332*cfcc706cSMiquel Raynal
333*cfcc706cSMiquel Raynal /* ECC value valid */
334*cfcc706cSMiquel Raynal if (ecc_status & ZYNQ_NAND_ECC_STATUS) {
335*cfcc706cSMiquel Raynal for (ecc_byte = 0; ecc_byte < 3; ecc_byte++) {
336*cfcc706cSMiquel Raynal /* Copy ECC bytes to MTD buffer */
337*cfcc706cSMiquel Raynal *ecc_code = ecc_value & 0xFF;
338*cfcc706cSMiquel Raynal ecc_value = ecc_value >> 8;
339*cfcc706cSMiquel Raynal ecc_code++;
340*cfcc706cSMiquel Raynal }
341*cfcc706cSMiquel Raynal } else {
342*cfcc706cSMiquel Raynal debug("%s: ecc status failed\n", __func__);
343*cfcc706cSMiquel Raynal }
344*cfcc706cSMiquel Raynal }
345*cfcc706cSMiquel Raynal
346*cfcc706cSMiquel Raynal return 0;
347*cfcc706cSMiquel Raynal }
348*cfcc706cSMiquel Raynal
349*cfcc706cSMiquel Raynal /*
350*cfcc706cSMiquel Raynal * onehot - onehot function
351*cfcc706cSMiquel Raynal * @value: value to check for onehot
352*cfcc706cSMiquel Raynal *
353*cfcc706cSMiquel Raynal * This function checks whether a value is onehot or not.
354*cfcc706cSMiquel Raynal * onehot is if and only if one bit is set.
355*cfcc706cSMiquel Raynal *
356*cfcc706cSMiquel Raynal * FIXME: Try to move this in common.h
357*cfcc706cSMiquel Raynal */
onehot(unsigned short value)358*cfcc706cSMiquel Raynal static bool onehot(unsigned short value)
359*cfcc706cSMiquel Raynal {
360*cfcc706cSMiquel Raynal bool onehot;
361*cfcc706cSMiquel Raynal
362*cfcc706cSMiquel Raynal onehot = value && !(value & (value - 1));
363*cfcc706cSMiquel Raynal return onehot;
364*cfcc706cSMiquel Raynal }
365*cfcc706cSMiquel Raynal
366*cfcc706cSMiquel Raynal /*
367*cfcc706cSMiquel Raynal * zynq_nand_correct_data - ECC correction function
368*cfcc706cSMiquel Raynal * @mtd: Pointer to the mtd_info structure
369*cfcc706cSMiquel Raynal * @buf: Pointer to the page data
370*cfcc706cSMiquel Raynal * @read_ecc: Pointer to the ECC value read from spare data area
371*cfcc706cSMiquel Raynal * @calc_ecc: Pointer to the calculated ECC value
372*cfcc706cSMiquel Raynal *
373*cfcc706cSMiquel Raynal * This function corrects the ECC single bit errors & detects 2-bit errors.
374*cfcc706cSMiquel Raynal *
375*cfcc706cSMiquel Raynal * returns: 0 if no ECC errors found
376*cfcc706cSMiquel Raynal * 1 if single bit error found and corrected.
377*cfcc706cSMiquel Raynal * -1 if multiple ECC errors found.
378*cfcc706cSMiquel Raynal */
zynq_nand_correct_data(struct mtd_info * mtd,unsigned char * buf,unsigned char * read_ecc,unsigned char * calc_ecc)379*cfcc706cSMiquel Raynal static int zynq_nand_correct_data(struct mtd_info *mtd, unsigned char *buf,
380*cfcc706cSMiquel Raynal unsigned char *read_ecc, unsigned char *calc_ecc)
381*cfcc706cSMiquel Raynal {
382*cfcc706cSMiquel Raynal unsigned char bit_addr;
383*cfcc706cSMiquel Raynal unsigned int byte_addr;
384*cfcc706cSMiquel Raynal unsigned short ecc_odd, ecc_even;
385*cfcc706cSMiquel Raynal unsigned short read_ecc_lower, read_ecc_upper;
386*cfcc706cSMiquel Raynal unsigned short calc_ecc_lower, calc_ecc_upper;
387*cfcc706cSMiquel Raynal
388*cfcc706cSMiquel Raynal read_ecc_lower = (read_ecc[0] | (read_ecc[1] << 8)) & 0xfff;
389*cfcc706cSMiquel Raynal read_ecc_upper = ((read_ecc[1] >> 4) | (read_ecc[2] << 4)) & 0xfff;
390*cfcc706cSMiquel Raynal
391*cfcc706cSMiquel Raynal calc_ecc_lower = (calc_ecc[0] | (calc_ecc[1] << 8)) & 0xfff;
392*cfcc706cSMiquel Raynal calc_ecc_upper = ((calc_ecc[1] >> 4) | (calc_ecc[2] << 4)) & 0xfff;
393*cfcc706cSMiquel Raynal
394*cfcc706cSMiquel Raynal ecc_odd = read_ecc_lower ^ calc_ecc_lower;
395*cfcc706cSMiquel Raynal ecc_even = read_ecc_upper ^ calc_ecc_upper;
396*cfcc706cSMiquel Raynal
397*cfcc706cSMiquel Raynal if ((ecc_odd == 0) && (ecc_even == 0))
398*cfcc706cSMiquel Raynal return 0; /* no error */
399*cfcc706cSMiquel Raynal
400*cfcc706cSMiquel Raynal if (ecc_odd == (~ecc_even & 0xfff)) {
401*cfcc706cSMiquel Raynal /* bits [11:3] of error code is byte offset */
402*cfcc706cSMiquel Raynal byte_addr = (ecc_odd >> 3) & 0x1ff;
403*cfcc706cSMiquel Raynal /* bits [2:0] of error code is bit offset */
404*cfcc706cSMiquel Raynal bit_addr = ecc_odd & 0x7;
405*cfcc706cSMiquel Raynal /* Toggling error bit */
406*cfcc706cSMiquel Raynal buf[byte_addr] ^= (1 << bit_addr);
407*cfcc706cSMiquel Raynal return 1;
408*cfcc706cSMiquel Raynal }
409*cfcc706cSMiquel Raynal
410*cfcc706cSMiquel Raynal if (onehot(ecc_odd | ecc_even))
411*cfcc706cSMiquel Raynal return 1; /* one error in parity */
412*cfcc706cSMiquel Raynal
413*cfcc706cSMiquel Raynal return -1; /* Uncorrectable error */
414*cfcc706cSMiquel Raynal }
415*cfcc706cSMiquel Raynal
416*cfcc706cSMiquel Raynal /*
417*cfcc706cSMiquel Raynal * zynq_nand_read_oob - [REPLACABLE] the most common OOB data read function
418*cfcc706cSMiquel Raynal * @mtd: mtd info structure
419*cfcc706cSMiquel Raynal * @chip: nand chip info structure
420*cfcc706cSMiquel Raynal * @page: page number to read
421*cfcc706cSMiquel Raynal * @sndcmd: flag whether to issue read command or not
422*cfcc706cSMiquel Raynal */
zynq_nand_read_oob(struct mtd_info * mtd,struct nand_chip * chip,int page)423*cfcc706cSMiquel Raynal static int zynq_nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
424*cfcc706cSMiquel Raynal int page)
425*cfcc706cSMiquel Raynal {
426*cfcc706cSMiquel Raynal unsigned long data_phase_addr = 0;
427*cfcc706cSMiquel Raynal int data_width = 4;
428*cfcc706cSMiquel Raynal u8 *p;
429*cfcc706cSMiquel Raynal
430*cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
431*cfcc706cSMiquel Raynal
432*cfcc706cSMiquel Raynal p = chip->oob_poi;
433*cfcc706cSMiquel Raynal chip->read_buf(mtd, p, (mtd->oobsize - data_width));
434*cfcc706cSMiquel Raynal p += mtd->oobsize - data_width;
435*cfcc706cSMiquel Raynal
436*cfcc706cSMiquel Raynal data_phase_addr = (unsigned long)chip->IO_ADDR_R;
437*cfcc706cSMiquel Raynal data_phase_addr |= ZYNQ_NAND_CLEAR_CS;
438*cfcc706cSMiquel Raynal chip->IO_ADDR_R = (void __iomem *)data_phase_addr;
439*cfcc706cSMiquel Raynal chip->read_buf(mtd, p, data_width);
440*cfcc706cSMiquel Raynal
441*cfcc706cSMiquel Raynal return 0;
442*cfcc706cSMiquel Raynal }
443*cfcc706cSMiquel Raynal
444*cfcc706cSMiquel Raynal /*
445*cfcc706cSMiquel Raynal * zynq_nand_write_oob - [REPLACABLE] the most common OOB data write function
446*cfcc706cSMiquel Raynal * @mtd: mtd info structure
447*cfcc706cSMiquel Raynal * @chip: nand chip info structure
448*cfcc706cSMiquel Raynal * @page: page number to write
449*cfcc706cSMiquel Raynal */
zynq_nand_write_oob(struct mtd_info * mtd,struct nand_chip * chip,int page)450*cfcc706cSMiquel Raynal static int zynq_nand_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
451*cfcc706cSMiquel Raynal int page)
452*cfcc706cSMiquel Raynal {
453*cfcc706cSMiquel Raynal int status = 0, data_width = 4;
454*cfcc706cSMiquel Raynal const u8 *buf = chip->oob_poi;
455*cfcc706cSMiquel Raynal unsigned long data_phase_addr = 0;
456*cfcc706cSMiquel Raynal
457*cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
458*cfcc706cSMiquel Raynal
459*cfcc706cSMiquel Raynal chip->write_buf(mtd, buf, (mtd->oobsize - data_width));
460*cfcc706cSMiquel Raynal buf += mtd->oobsize - data_width;
461*cfcc706cSMiquel Raynal
462*cfcc706cSMiquel Raynal data_phase_addr = (unsigned long)chip->IO_ADDR_W;
463*cfcc706cSMiquel Raynal data_phase_addr |= ZYNQ_NAND_CLEAR_CS;
464*cfcc706cSMiquel Raynal data_phase_addr |= (1 << END_CMD_VALID_SHIFT);
465*cfcc706cSMiquel Raynal chip->IO_ADDR_W = (void __iomem *)data_phase_addr;
466*cfcc706cSMiquel Raynal chip->write_buf(mtd, buf, data_width);
467*cfcc706cSMiquel Raynal
468*cfcc706cSMiquel Raynal /* Send command to program the OOB data */
469*cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
470*cfcc706cSMiquel Raynal status = chip->waitfunc(mtd, chip);
471*cfcc706cSMiquel Raynal
472*cfcc706cSMiquel Raynal return status & NAND_STATUS_FAIL ? -EIO : 0;
473*cfcc706cSMiquel Raynal }
474*cfcc706cSMiquel Raynal
475*cfcc706cSMiquel Raynal /*
476*cfcc706cSMiquel Raynal * zynq_nand_read_page_raw - [Intern] read raw page data without ecc
477*cfcc706cSMiquel Raynal * @mtd: mtd info structure
478*cfcc706cSMiquel Raynal * @chip: nand chip info structure
479*cfcc706cSMiquel Raynal * @buf: buffer to store read data
480*cfcc706cSMiquel Raynal * @oob_required: must write chip->oob_poi to OOB
481*cfcc706cSMiquel Raynal * @page: page number to read
482*cfcc706cSMiquel Raynal */
zynq_nand_read_page_raw(struct mtd_info * mtd,struct nand_chip * chip,u8 * buf,int oob_required,int page)483*cfcc706cSMiquel Raynal static int zynq_nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
484*cfcc706cSMiquel Raynal u8 *buf, int oob_required, int page)
485*cfcc706cSMiquel Raynal {
486*cfcc706cSMiquel Raynal unsigned long data_width = 4;
487*cfcc706cSMiquel Raynal unsigned long data_phase_addr = 0;
488*cfcc706cSMiquel Raynal u8 *p;
489*cfcc706cSMiquel Raynal
490*cfcc706cSMiquel Raynal chip->read_buf(mtd, buf, mtd->writesize);
491*cfcc706cSMiquel Raynal
492*cfcc706cSMiquel Raynal p = chip->oob_poi;
493*cfcc706cSMiquel Raynal chip->read_buf(mtd, p, (mtd->oobsize - data_width));
494*cfcc706cSMiquel Raynal p += (mtd->oobsize - data_width);
495*cfcc706cSMiquel Raynal
496*cfcc706cSMiquel Raynal data_phase_addr = (unsigned long)chip->IO_ADDR_R;
497*cfcc706cSMiquel Raynal data_phase_addr |= ZYNQ_NAND_CLEAR_CS;
498*cfcc706cSMiquel Raynal chip->IO_ADDR_R = (void __iomem *)data_phase_addr;
499*cfcc706cSMiquel Raynal
500*cfcc706cSMiquel Raynal chip->read_buf(mtd, p, data_width);
501*cfcc706cSMiquel Raynal return 0;
502*cfcc706cSMiquel Raynal }
503*cfcc706cSMiquel Raynal
zynq_nand_read_page_raw_nooob(struct mtd_info * mtd,struct nand_chip * chip,u8 * buf,int oob_required,int page)504*cfcc706cSMiquel Raynal static int zynq_nand_read_page_raw_nooob(struct mtd_info *mtd,
505*cfcc706cSMiquel Raynal struct nand_chip *chip, u8 *buf, int oob_required, int page)
506*cfcc706cSMiquel Raynal {
507*cfcc706cSMiquel Raynal chip->read_buf(mtd, buf, mtd->writesize);
508*cfcc706cSMiquel Raynal return 0;
509*cfcc706cSMiquel Raynal }
510*cfcc706cSMiquel Raynal
zynq_nand_read_subpage_raw(struct mtd_info * mtd,struct nand_chip * chip,u32 data_offs,u32 readlen,u8 * buf,int page)511*cfcc706cSMiquel Raynal static int zynq_nand_read_subpage_raw(struct mtd_info *mtd,
512*cfcc706cSMiquel Raynal struct nand_chip *chip, u32 data_offs,
513*cfcc706cSMiquel Raynal u32 readlen, u8 *buf, int page)
514*cfcc706cSMiquel Raynal {
515*cfcc706cSMiquel Raynal if (data_offs != 0) {
516*cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_offs, -1);
517*cfcc706cSMiquel Raynal buf += data_offs;
518*cfcc706cSMiquel Raynal }
519*cfcc706cSMiquel Raynal chip->read_buf(mtd, buf, readlen);
520*cfcc706cSMiquel Raynal
521*cfcc706cSMiquel Raynal return 0;
522*cfcc706cSMiquel Raynal }
523*cfcc706cSMiquel Raynal
524*cfcc706cSMiquel Raynal /*
525*cfcc706cSMiquel Raynal * zynq_nand_write_page_raw - [Intern] raw page write function
526*cfcc706cSMiquel Raynal * @mtd: mtd info structure
527*cfcc706cSMiquel Raynal * @chip: nand chip info structure
528*cfcc706cSMiquel Raynal * @buf: data buffer
529*cfcc706cSMiquel Raynal * @oob_required: must write chip->oob_poi to OOB
530*cfcc706cSMiquel Raynal */
zynq_nand_write_page_raw(struct mtd_info * mtd,struct nand_chip * chip,const u8 * buf,int oob_required,int page)531*cfcc706cSMiquel Raynal static int zynq_nand_write_page_raw(struct mtd_info *mtd,
532*cfcc706cSMiquel Raynal struct nand_chip *chip, const u8 *buf, int oob_required, int page)
533*cfcc706cSMiquel Raynal {
534*cfcc706cSMiquel Raynal unsigned long data_width = 4;
535*cfcc706cSMiquel Raynal unsigned long data_phase_addr = 0;
536*cfcc706cSMiquel Raynal u8 *p;
537*cfcc706cSMiquel Raynal
538*cfcc706cSMiquel Raynal chip->write_buf(mtd, buf, mtd->writesize);
539*cfcc706cSMiquel Raynal
540*cfcc706cSMiquel Raynal p = chip->oob_poi;
541*cfcc706cSMiquel Raynal chip->write_buf(mtd, p, (mtd->oobsize - data_width));
542*cfcc706cSMiquel Raynal p += (mtd->oobsize - data_width);
543*cfcc706cSMiquel Raynal
544*cfcc706cSMiquel Raynal data_phase_addr = (unsigned long)chip->IO_ADDR_W;
545*cfcc706cSMiquel Raynal data_phase_addr |= ZYNQ_NAND_CLEAR_CS;
546*cfcc706cSMiquel Raynal data_phase_addr |= (1 << END_CMD_VALID_SHIFT);
547*cfcc706cSMiquel Raynal chip->IO_ADDR_W = (void __iomem *)data_phase_addr;
548*cfcc706cSMiquel Raynal
549*cfcc706cSMiquel Raynal chip->write_buf(mtd, p, data_width);
550*cfcc706cSMiquel Raynal
551*cfcc706cSMiquel Raynal return 0;
552*cfcc706cSMiquel Raynal }
553*cfcc706cSMiquel Raynal
554*cfcc706cSMiquel Raynal /*
555*cfcc706cSMiquel Raynal * nand_write_page_hwecc - Hardware ECC based page write function
556*cfcc706cSMiquel Raynal * @mtd: Pointer to the mtd info structure
557*cfcc706cSMiquel Raynal * @chip: Pointer to the NAND chip info structure
558*cfcc706cSMiquel Raynal * @buf: Pointer to the data buffer
559*cfcc706cSMiquel Raynal * @oob_required: must write chip->oob_poi to OOB
560*cfcc706cSMiquel Raynal *
561*cfcc706cSMiquel Raynal * This functions writes data and hardware generated ECC values in to the page.
562*cfcc706cSMiquel Raynal */
zynq_nand_write_page_hwecc(struct mtd_info * mtd,struct nand_chip * chip,const u8 * buf,int oob_required,int page)563*cfcc706cSMiquel Raynal static int zynq_nand_write_page_hwecc(struct mtd_info *mtd,
564*cfcc706cSMiquel Raynal struct nand_chip *chip, const u8 *buf, int oob_required, int page)
565*cfcc706cSMiquel Raynal {
566*cfcc706cSMiquel Raynal int i, eccsteps, eccsize = chip->ecc.size;
567*cfcc706cSMiquel Raynal u8 *ecc_calc = chip->buffers->ecccalc;
568*cfcc706cSMiquel Raynal const u8 *p = buf;
569*cfcc706cSMiquel Raynal u32 *eccpos = chip->ecc.layout->eccpos;
570*cfcc706cSMiquel Raynal unsigned long data_phase_addr = 0;
571*cfcc706cSMiquel Raynal unsigned long data_width = 4;
572*cfcc706cSMiquel Raynal u8 *oob_ptr;
573*cfcc706cSMiquel Raynal
574*cfcc706cSMiquel Raynal for (eccsteps = chip->ecc.steps; (eccsteps - 1); eccsteps--) {
575*cfcc706cSMiquel Raynal chip->write_buf(mtd, p, eccsize);
576*cfcc706cSMiquel Raynal p += eccsize;
577*cfcc706cSMiquel Raynal }
578*cfcc706cSMiquel Raynal chip->write_buf(mtd, p, (eccsize - data_width));
579*cfcc706cSMiquel Raynal p += eccsize - data_width;
580*cfcc706cSMiquel Raynal
581*cfcc706cSMiquel Raynal /* Set ECC Last bit to 1 */
582*cfcc706cSMiquel Raynal data_phase_addr = (unsigned long) chip->IO_ADDR_W;
583*cfcc706cSMiquel Raynal data_phase_addr |= ZYNQ_NAND_ECC_LAST;
584*cfcc706cSMiquel Raynal chip->IO_ADDR_W = (void __iomem *)data_phase_addr;
585*cfcc706cSMiquel Raynal chip->write_buf(mtd, p, data_width);
586*cfcc706cSMiquel Raynal
587*cfcc706cSMiquel Raynal /* Wait for ECC to be calculated and read the error values */
588*cfcc706cSMiquel Raynal p = buf;
589*cfcc706cSMiquel Raynal chip->ecc.calculate(mtd, p, &ecc_calc[0]);
590*cfcc706cSMiquel Raynal
591*cfcc706cSMiquel Raynal for (i = 0; i < chip->ecc.total; i++)
592*cfcc706cSMiquel Raynal chip->oob_poi[eccpos[i]] = ~(ecc_calc[i]);
593*cfcc706cSMiquel Raynal
594*cfcc706cSMiquel Raynal /* Clear ECC last bit */
595*cfcc706cSMiquel Raynal data_phase_addr = (unsigned long)chip->IO_ADDR_W;
596*cfcc706cSMiquel Raynal data_phase_addr &= ~ZYNQ_NAND_ECC_LAST;
597*cfcc706cSMiquel Raynal chip->IO_ADDR_W = (void __iomem *)data_phase_addr;
598*cfcc706cSMiquel Raynal
599*cfcc706cSMiquel Raynal /* Write the spare area with ECC bytes */
600*cfcc706cSMiquel Raynal oob_ptr = chip->oob_poi;
601*cfcc706cSMiquel Raynal chip->write_buf(mtd, oob_ptr, (mtd->oobsize - data_width));
602*cfcc706cSMiquel Raynal
603*cfcc706cSMiquel Raynal data_phase_addr = (unsigned long)chip->IO_ADDR_W;
604*cfcc706cSMiquel Raynal data_phase_addr |= ZYNQ_NAND_CLEAR_CS;
605*cfcc706cSMiquel Raynal data_phase_addr |= (1 << END_CMD_VALID_SHIFT);
606*cfcc706cSMiquel Raynal chip->IO_ADDR_W = (void __iomem *)data_phase_addr;
607*cfcc706cSMiquel Raynal oob_ptr += (mtd->oobsize - data_width);
608*cfcc706cSMiquel Raynal chip->write_buf(mtd, oob_ptr, data_width);
609*cfcc706cSMiquel Raynal
610*cfcc706cSMiquel Raynal return 0;
611*cfcc706cSMiquel Raynal }
612*cfcc706cSMiquel Raynal
613*cfcc706cSMiquel Raynal /*
614*cfcc706cSMiquel Raynal * zynq_nand_write_page_swecc - [REPLACABLE] software ecc based page
615*cfcc706cSMiquel Raynal * write function
616*cfcc706cSMiquel Raynal * @mtd: mtd info structure
617*cfcc706cSMiquel Raynal * @chip: nand chip info structure
618*cfcc706cSMiquel Raynal * @buf: data buffer
619*cfcc706cSMiquel Raynal * @oob_required: must write chip->oob_poi to OOB
620*cfcc706cSMiquel Raynal */
zynq_nand_write_page_swecc(struct mtd_info * mtd,struct nand_chip * chip,const u8 * buf,int oob_required,int page)621*cfcc706cSMiquel Raynal static int zynq_nand_write_page_swecc(struct mtd_info *mtd,
622*cfcc706cSMiquel Raynal struct nand_chip *chip, const u8 *buf, int oob_required, int page)
623*cfcc706cSMiquel Raynal {
624*cfcc706cSMiquel Raynal int i, eccsize = chip->ecc.size;
625*cfcc706cSMiquel Raynal int eccbytes = chip->ecc.bytes;
626*cfcc706cSMiquel Raynal int eccsteps = chip->ecc.steps;
627*cfcc706cSMiquel Raynal u8 *ecc_calc = chip->buffers->ecccalc;
628*cfcc706cSMiquel Raynal const u8 *p = buf;
629*cfcc706cSMiquel Raynal u32 *eccpos = chip->ecc.layout->eccpos;
630*cfcc706cSMiquel Raynal
631*cfcc706cSMiquel Raynal /* Software ecc calculation */
632*cfcc706cSMiquel Raynal for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
633*cfcc706cSMiquel Raynal chip->ecc.calculate(mtd, p, &ecc_calc[i]);
634*cfcc706cSMiquel Raynal
635*cfcc706cSMiquel Raynal for (i = 0; i < chip->ecc.total; i++)
636*cfcc706cSMiquel Raynal chip->oob_poi[eccpos[i]] = ecc_calc[i];
637*cfcc706cSMiquel Raynal
638*cfcc706cSMiquel Raynal return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
639*cfcc706cSMiquel Raynal }
640*cfcc706cSMiquel Raynal
641*cfcc706cSMiquel Raynal /*
642*cfcc706cSMiquel Raynal * nand_read_page_hwecc - Hardware ECC based page read function
643*cfcc706cSMiquel Raynal * @mtd: Pointer to the mtd info structure
644*cfcc706cSMiquel Raynal * @chip: Pointer to the NAND chip info structure
645*cfcc706cSMiquel Raynal * @buf: Pointer to the buffer to store read data
646*cfcc706cSMiquel Raynal * @oob_required: must write chip->oob_poi to OOB
647*cfcc706cSMiquel Raynal * @page: page number to read
648*cfcc706cSMiquel Raynal *
649*cfcc706cSMiquel Raynal * This functions reads data and checks the data integrity by comparing hardware
650*cfcc706cSMiquel Raynal * generated ECC values and read ECC values from spare area.
651*cfcc706cSMiquel Raynal *
652*cfcc706cSMiquel Raynal * returns: 0 always and updates ECC operation status in to MTD structure
653*cfcc706cSMiquel Raynal */
zynq_nand_read_page_hwecc(struct mtd_info * mtd,struct nand_chip * chip,u8 * buf,int oob_required,int page)654*cfcc706cSMiquel Raynal static int zynq_nand_read_page_hwecc(struct mtd_info *mtd,
655*cfcc706cSMiquel Raynal struct nand_chip *chip, u8 *buf, int oob_required, int page)
656*cfcc706cSMiquel Raynal {
657*cfcc706cSMiquel Raynal int i, stat, eccsteps, eccsize = chip->ecc.size;
658*cfcc706cSMiquel Raynal int eccbytes = chip->ecc.bytes;
659*cfcc706cSMiquel Raynal u8 *p = buf;
660*cfcc706cSMiquel Raynal u8 *ecc_calc = chip->buffers->ecccalc;
661*cfcc706cSMiquel Raynal u8 *ecc_code = chip->buffers->ecccode;
662*cfcc706cSMiquel Raynal u32 *eccpos = chip->ecc.layout->eccpos;
663*cfcc706cSMiquel Raynal unsigned long data_phase_addr = 0;
664*cfcc706cSMiquel Raynal unsigned long data_width = 4;
665*cfcc706cSMiquel Raynal u8 *oob_ptr;
666*cfcc706cSMiquel Raynal
667*cfcc706cSMiquel Raynal for (eccsteps = chip->ecc.steps; (eccsteps - 1); eccsteps--) {
668*cfcc706cSMiquel Raynal chip->read_buf(mtd, p, eccsize);
669*cfcc706cSMiquel Raynal p += eccsize;
670*cfcc706cSMiquel Raynal }
671*cfcc706cSMiquel Raynal chip->read_buf(mtd, p, (eccsize - data_width));
672*cfcc706cSMiquel Raynal p += eccsize - data_width;
673*cfcc706cSMiquel Raynal
674*cfcc706cSMiquel Raynal /* Set ECC Last bit to 1 */
675*cfcc706cSMiquel Raynal data_phase_addr = (unsigned long)chip->IO_ADDR_R;
676*cfcc706cSMiquel Raynal data_phase_addr |= ZYNQ_NAND_ECC_LAST;
677*cfcc706cSMiquel Raynal chip->IO_ADDR_R = (void __iomem *)data_phase_addr;
678*cfcc706cSMiquel Raynal chip->read_buf(mtd, p, data_width);
679*cfcc706cSMiquel Raynal
680*cfcc706cSMiquel Raynal /* Read the calculated ECC value */
681*cfcc706cSMiquel Raynal p = buf;
682*cfcc706cSMiquel Raynal chip->ecc.calculate(mtd, p, &ecc_calc[0]);
683*cfcc706cSMiquel Raynal
684*cfcc706cSMiquel Raynal /* Clear ECC last bit */
685*cfcc706cSMiquel Raynal data_phase_addr = (unsigned long)chip->IO_ADDR_R;
686*cfcc706cSMiquel Raynal data_phase_addr &= ~ZYNQ_NAND_ECC_LAST;
687*cfcc706cSMiquel Raynal chip->IO_ADDR_R = (void __iomem *)data_phase_addr;
688*cfcc706cSMiquel Raynal
689*cfcc706cSMiquel Raynal /* Read the stored ECC value */
690*cfcc706cSMiquel Raynal oob_ptr = chip->oob_poi;
691*cfcc706cSMiquel Raynal chip->read_buf(mtd, oob_ptr, (mtd->oobsize - data_width));
692*cfcc706cSMiquel Raynal
693*cfcc706cSMiquel Raynal /* de-assert chip select */
694*cfcc706cSMiquel Raynal data_phase_addr = (unsigned long)chip->IO_ADDR_R;
695*cfcc706cSMiquel Raynal data_phase_addr |= ZYNQ_NAND_CLEAR_CS;
696*cfcc706cSMiquel Raynal chip->IO_ADDR_R = (void __iomem *)data_phase_addr;
697*cfcc706cSMiquel Raynal
698*cfcc706cSMiquel Raynal oob_ptr += (mtd->oobsize - data_width);
699*cfcc706cSMiquel Raynal chip->read_buf(mtd, oob_ptr, data_width);
700*cfcc706cSMiquel Raynal
701*cfcc706cSMiquel Raynal for (i = 0; i < chip->ecc.total; i++)
702*cfcc706cSMiquel Raynal ecc_code[i] = ~(chip->oob_poi[eccpos[i]]);
703*cfcc706cSMiquel Raynal
704*cfcc706cSMiquel Raynal eccsteps = chip->ecc.steps;
705*cfcc706cSMiquel Raynal p = buf;
706*cfcc706cSMiquel Raynal
707*cfcc706cSMiquel Raynal /* Check ECC error for all blocks and correct if it is correctable */
708*cfcc706cSMiquel Raynal for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
709*cfcc706cSMiquel Raynal stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
710*cfcc706cSMiquel Raynal if (stat < 0)
711*cfcc706cSMiquel Raynal mtd->ecc_stats.failed++;
712*cfcc706cSMiquel Raynal else
713*cfcc706cSMiquel Raynal mtd->ecc_stats.corrected += stat;
714*cfcc706cSMiquel Raynal }
715*cfcc706cSMiquel Raynal return 0;
716*cfcc706cSMiquel Raynal }
717*cfcc706cSMiquel Raynal
718*cfcc706cSMiquel Raynal /*
719*cfcc706cSMiquel Raynal * zynq_nand_read_page_swecc - [REPLACABLE] software ecc based page
720*cfcc706cSMiquel Raynal * read function
721*cfcc706cSMiquel Raynal * @mtd: mtd info structure
722*cfcc706cSMiquel Raynal * @chip: nand chip info structure
723*cfcc706cSMiquel Raynal * @buf: buffer to store read data
724*cfcc706cSMiquel Raynal * @page: page number to read
725*cfcc706cSMiquel Raynal */
zynq_nand_read_page_swecc(struct mtd_info * mtd,struct nand_chip * chip,u8 * buf,int oob_required,int page)726*cfcc706cSMiquel Raynal static int zynq_nand_read_page_swecc(struct mtd_info *mtd,
727*cfcc706cSMiquel Raynal struct nand_chip *chip, u8 *buf, int oob_required, int page)
728*cfcc706cSMiquel Raynal {
729*cfcc706cSMiquel Raynal int i, eccsize = chip->ecc.size;
730*cfcc706cSMiquel Raynal int eccbytes = chip->ecc.bytes;
731*cfcc706cSMiquel Raynal int eccsteps = chip->ecc.steps;
732*cfcc706cSMiquel Raynal u8 *p = buf;
733*cfcc706cSMiquel Raynal u8 *ecc_calc = chip->buffers->ecccalc;
734*cfcc706cSMiquel Raynal u8 *ecc_code = chip->buffers->ecccode;
735*cfcc706cSMiquel Raynal u32 *eccpos = chip->ecc.layout->eccpos;
736*cfcc706cSMiquel Raynal
737*cfcc706cSMiquel Raynal chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
738*cfcc706cSMiquel Raynal
739*cfcc706cSMiquel Raynal for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
740*cfcc706cSMiquel Raynal chip->ecc.calculate(mtd, p, &ecc_calc[i]);
741*cfcc706cSMiquel Raynal
742*cfcc706cSMiquel Raynal for (i = 0; i < chip->ecc.total; i++)
743*cfcc706cSMiquel Raynal ecc_code[i] = chip->oob_poi[eccpos[i]];
744*cfcc706cSMiquel Raynal
745*cfcc706cSMiquel Raynal eccsteps = chip->ecc.steps;
746*cfcc706cSMiquel Raynal p = buf;
747*cfcc706cSMiquel Raynal
748*cfcc706cSMiquel Raynal for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
749*cfcc706cSMiquel Raynal int stat;
750*cfcc706cSMiquel Raynal
751*cfcc706cSMiquel Raynal stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
752*cfcc706cSMiquel Raynal if (stat < 0)
753*cfcc706cSMiquel Raynal mtd->ecc_stats.failed++;
754*cfcc706cSMiquel Raynal else
755*cfcc706cSMiquel Raynal mtd->ecc_stats.corrected += stat;
756*cfcc706cSMiquel Raynal }
757*cfcc706cSMiquel Raynal return 0;
758*cfcc706cSMiquel Raynal }
759*cfcc706cSMiquel Raynal
760*cfcc706cSMiquel Raynal /*
761*cfcc706cSMiquel Raynal * zynq_nand_select_chip - Select the flash device
762*cfcc706cSMiquel Raynal * @mtd: Pointer to the mtd_info structure
763*cfcc706cSMiquel Raynal * @chip: Chip number to be selected
764*cfcc706cSMiquel Raynal *
765*cfcc706cSMiquel Raynal * This function is empty as the NAND controller handles chip select line
766*cfcc706cSMiquel Raynal * internally based on the chip address passed in command and data phase.
767*cfcc706cSMiquel Raynal */
zynq_nand_select_chip(struct mtd_info * mtd,int chip)768*cfcc706cSMiquel Raynal static void zynq_nand_select_chip(struct mtd_info *mtd, int chip)
769*cfcc706cSMiquel Raynal {
770*cfcc706cSMiquel Raynal /* Not support multiple chips yet */
771*cfcc706cSMiquel Raynal }
772*cfcc706cSMiquel Raynal
773*cfcc706cSMiquel Raynal /*
774*cfcc706cSMiquel Raynal * zynq_nand_cmd_function - Send command to NAND device
775*cfcc706cSMiquel Raynal * @mtd: Pointer to the mtd_info structure
776*cfcc706cSMiquel Raynal * @command: The command to be sent to the flash device
777*cfcc706cSMiquel Raynal * @column: The column address for this command, -1 if none
778*cfcc706cSMiquel Raynal * @page_addr: The page address for this command, -1 if none
779*cfcc706cSMiquel Raynal */
zynq_nand_cmd_function(struct mtd_info * mtd,unsigned int command,int column,int page_addr)780*cfcc706cSMiquel Raynal static void zynq_nand_cmd_function(struct mtd_info *mtd, unsigned int command,
781*cfcc706cSMiquel Raynal int column, int page_addr)
782*cfcc706cSMiquel Raynal {
783*cfcc706cSMiquel Raynal struct nand_chip *chip = mtd->priv;
784*cfcc706cSMiquel Raynal const struct zynq_nand_command_format *curr_cmd = NULL;
785*cfcc706cSMiquel Raynal u8 addr_cycles = 0;
786*cfcc706cSMiquel Raynal struct zynq_nand_info *xnand = (struct zynq_nand_info *)chip->priv;
787*cfcc706cSMiquel Raynal void *cmd_addr;
788*cfcc706cSMiquel Raynal unsigned long cmd_data = 0;
789*cfcc706cSMiquel Raynal unsigned long cmd_phase_addr = 0;
790*cfcc706cSMiquel Raynal unsigned long data_phase_addr = 0;
791*cfcc706cSMiquel Raynal u8 end_cmd = 0;
792*cfcc706cSMiquel Raynal u8 end_cmd_valid = 0;
793*cfcc706cSMiquel Raynal u32 index;
794*cfcc706cSMiquel Raynal
795*cfcc706cSMiquel Raynal if (xnand->end_cmd_pending) {
796*cfcc706cSMiquel Raynal /* Check for end command if this command request is same as the
797*cfcc706cSMiquel Raynal * pending command then return
798*cfcc706cSMiquel Raynal */
799*cfcc706cSMiquel Raynal if (xnand->end_cmd == command) {
800*cfcc706cSMiquel Raynal xnand->end_cmd = 0;
801*cfcc706cSMiquel Raynal xnand->end_cmd_pending = 0;
802*cfcc706cSMiquel Raynal return;
803*cfcc706cSMiquel Raynal }
804*cfcc706cSMiquel Raynal }
805*cfcc706cSMiquel Raynal
806*cfcc706cSMiquel Raynal /* Emulate NAND_CMD_READOOB for large page device */
807*cfcc706cSMiquel Raynal if ((mtd->writesize > ZYNQ_NAND_ECC_SIZE) &&
808*cfcc706cSMiquel Raynal (command == NAND_CMD_READOOB)) {
809*cfcc706cSMiquel Raynal column += mtd->writesize;
810*cfcc706cSMiquel Raynal command = NAND_CMD_READ0;
811*cfcc706cSMiquel Raynal }
812*cfcc706cSMiquel Raynal
813*cfcc706cSMiquel Raynal /* Get the command format */
814*cfcc706cSMiquel Raynal for (index = 0; index < ARRAY_SIZE(zynq_nand_commands); index++)
815*cfcc706cSMiquel Raynal if (command == zynq_nand_commands[index].start_cmd)
816*cfcc706cSMiquel Raynal break;
817*cfcc706cSMiquel Raynal
818*cfcc706cSMiquel Raynal if (index == ARRAY_SIZE(zynq_nand_commands)) {
819*cfcc706cSMiquel Raynal printf("%s: Unsupported start cmd %02x\n", __func__, command);
820*cfcc706cSMiquel Raynal return;
821*cfcc706cSMiquel Raynal }
822*cfcc706cSMiquel Raynal curr_cmd = &zynq_nand_commands[index];
823*cfcc706cSMiquel Raynal
824*cfcc706cSMiquel Raynal /* Clear interrupt */
825*cfcc706cSMiquel Raynal writel(ZYNQ_MEMC_CLRCR_INT_CLR1, &zynq_nand_smc_base->cfr);
826*cfcc706cSMiquel Raynal
827*cfcc706cSMiquel Raynal /* Get the command phase address */
828*cfcc706cSMiquel Raynal if (curr_cmd->end_cmd_valid == ZYNQ_NAND_CMD_PHASE)
829*cfcc706cSMiquel Raynal end_cmd_valid = 1;
830*cfcc706cSMiquel Raynal
831*cfcc706cSMiquel Raynal if (curr_cmd->end_cmd == NAND_CMD_NONE)
832*cfcc706cSMiquel Raynal end_cmd = 0x0;
833*cfcc706cSMiquel Raynal else
834*cfcc706cSMiquel Raynal end_cmd = curr_cmd->end_cmd;
835*cfcc706cSMiquel Raynal
836*cfcc706cSMiquel Raynal if (command == NAND_CMD_READ0 ||
837*cfcc706cSMiquel Raynal command == NAND_CMD_SEQIN) {
838*cfcc706cSMiquel Raynal addr_cycles = chip->onfi_params.addr_cycles &
839*cfcc706cSMiquel Raynal ZYNQ_NAND_ROW_ADDR_CYCL_MASK;
840*cfcc706cSMiquel Raynal addr_cycles += ((chip->onfi_params.addr_cycles &
841*cfcc706cSMiquel Raynal ZYNQ_NAND_COL_ADDR_CYCL_MASK) >> 4);
842*cfcc706cSMiquel Raynal } else {
843*cfcc706cSMiquel Raynal addr_cycles = curr_cmd->addr_cycles;
844*cfcc706cSMiquel Raynal }
845*cfcc706cSMiquel Raynal
846*cfcc706cSMiquel Raynal cmd_phase_addr = (unsigned long)xnand->nand_base |
847*cfcc706cSMiquel Raynal (addr_cycles << ADDR_CYCLES_SHIFT) |
848*cfcc706cSMiquel Raynal (end_cmd_valid << END_CMD_VALID_SHIFT) |
849*cfcc706cSMiquel Raynal (COMMAND_PHASE) |
850*cfcc706cSMiquel Raynal (end_cmd << END_CMD_SHIFT) |
851*cfcc706cSMiquel Raynal (curr_cmd->start_cmd << START_CMD_SHIFT);
852*cfcc706cSMiquel Raynal
853*cfcc706cSMiquel Raynal cmd_addr = (void __iomem *)cmd_phase_addr;
854*cfcc706cSMiquel Raynal
855*cfcc706cSMiquel Raynal /* Get the data phase address */
856*cfcc706cSMiquel Raynal end_cmd_valid = 0;
857*cfcc706cSMiquel Raynal
858*cfcc706cSMiquel Raynal data_phase_addr = (unsigned long)xnand->nand_base |
859*cfcc706cSMiquel Raynal (0x0 << CLEAR_CS_SHIFT) |
860*cfcc706cSMiquel Raynal (end_cmd_valid << END_CMD_VALID_SHIFT) |
861*cfcc706cSMiquel Raynal (DATA_PHASE) |
862*cfcc706cSMiquel Raynal (end_cmd << END_CMD_SHIFT) |
863*cfcc706cSMiquel Raynal (0x0 << ECC_LAST_SHIFT);
864*cfcc706cSMiquel Raynal
865*cfcc706cSMiquel Raynal chip->IO_ADDR_R = (void __iomem *)data_phase_addr;
866*cfcc706cSMiquel Raynal chip->IO_ADDR_W = chip->IO_ADDR_R;
867*cfcc706cSMiquel Raynal
868*cfcc706cSMiquel Raynal /* Command phase AXI Read & Write */
869*cfcc706cSMiquel Raynal if (column != -1 && page_addr != -1) {
870*cfcc706cSMiquel Raynal /* Adjust columns for 16 bit bus width */
871*cfcc706cSMiquel Raynal if (chip->options & NAND_BUSWIDTH_16)
872*cfcc706cSMiquel Raynal column >>= 1;
873*cfcc706cSMiquel Raynal cmd_data = column;
874*cfcc706cSMiquel Raynal if (mtd->writesize > ZYNQ_NAND_ECC_SIZE) {
875*cfcc706cSMiquel Raynal cmd_data |= page_addr << 16;
876*cfcc706cSMiquel Raynal /* Another address cycle for devices > 128MiB */
877*cfcc706cSMiquel Raynal if (chip->chipsize > (128 << 20)) {
878*cfcc706cSMiquel Raynal writel(cmd_data, cmd_addr);
879*cfcc706cSMiquel Raynal cmd_data = (page_addr >> 16);
880*cfcc706cSMiquel Raynal }
881*cfcc706cSMiquel Raynal } else {
882*cfcc706cSMiquel Raynal cmd_data |= page_addr << 8;
883*cfcc706cSMiquel Raynal }
884*cfcc706cSMiquel Raynal } else if (page_addr != -1) { /* Erase */
885*cfcc706cSMiquel Raynal cmd_data = page_addr;
886*cfcc706cSMiquel Raynal } else if (column != -1) { /* Change read/write column, read id etc */
887*cfcc706cSMiquel Raynal /* Adjust columns for 16 bit bus width */
888*cfcc706cSMiquel Raynal if ((chip->options & NAND_BUSWIDTH_16) &&
889*cfcc706cSMiquel Raynal ((command == NAND_CMD_READ0) ||
890*cfcc706cSMiquel Raynal (command == NAND_CMD_SEQIN) ||
891*cfcc706cSMiquel Raynal (command == NAND_CMD_RNDOUT) ||
892*cfcc706cSMiquel Raynal (command == NAND_CMD_RNDIN)))
893*cfcc706cSMiquel Raynal column >>= 1;
894*cfcc706cSMiquel Raynal cmd_data = column;
895*cfcc706cSMiquel Raynal }
896*cfcc706cSMiquel Raynal
897*cfcc706cSMiquel Raynal writel(cmd_data, cmd_addr);
898*cfcc706cSMiquel Raynal
899*cfcc706cSMiquel Raynal if (curr_cmd->end_cmd_valid) {
900*cfcc706cSMiquel Raynal xnand->end_cmd = curr_cmd->end_cmd;
901*cfcc706cSMiquel Raynal xnand->end_cmd_pending = 1;
902*cfcc706cSMiquel Raynal }
903*cfcc706cSMiquel Raynal
904*cfcc706cSMiquel Raynal ndelay(100);
905*cfcc706cSMiquel Raynal
906*cfcc706cSMiquel Raynal if ((command == NAND_CMD_READ0) ||
907*cfcc706cSMiquel Raynal (command == NAND_CMD_RESET) ||
908*cfcc706cSMiquel Raynal (command == NAND_CMD_PARAM) ||
909*cfcc706cSMiquel Raynal (command == NAND_CMD_GET_FEATURES))
910*cfcc706cSMiquel Raynal /* wait until command is processed */
911*cfcc706cSMiquel Raynal nand_wait_ready(mtd);
912*cfcc706cSMiquel Raynal }
913*cfcc706cSMiquel Raynal
914*cfcc706cSMiquel Raynal /*
915*cfcc706cSMiquel Raynal * zynq_nand_read_buf - read chip data into buffer
916*cfcc706cSMiquel Raynal * @mtd: MTD device structure
917*cfcc706cSMiquel Raynal * @buf: buffer to store date
918*cfcc706cSMiquel Raynal * @len: number of bytes to read
919*cfcc706cSMiquel Raynal */
zynq_nand_read_buf(struct mtd_info * mtd,u8 * buf,int len)920*cfcc706cSMiquel Raynal static void zynq_nand_read_buf(struct mtd_info *mtd, u8 *buf, int len)
921*cfcc706cSMiquel Raynal {
922*cfcc706cSMiquel Raynal struct nand_chip *chip = mtd->priv;
923*cfcc706cSMiquel Raynal
924*cfcc706cSMiquel Raynal /* Make sure that buf is 32 bit aligned */
925*cfcc706cSMiquel Raynal if (((unsigned long)buf & 0x3) != 0) {
926*cfcc706cSMiquel Raynal if (((unsigned long)buf & 0x1) != 0) {
927*cfcc706cSMiquel Raynal if (len) {
928*cfcc706cSMiquel Raynal *buf = readb(chip->IO_ADDR_R);
929*cfcc706cSMiquel Raynal buf += 1;
930*cfcc706cSMiquel Raynal len--;
931*cfcc706cSMiquel Raynal }
932*cfcc706cSMiquel Raynal }
933*cfcc706cSMiquel Raynal
934*cfcc706cSMiquel Raynal if (((unsigned long)buf & 0x3) != 0) {
935*cfcc706cSMiquel Raynal if (len >= 2) {
936*cfcc706cSMiquel Raynal *(u16 *)buf = readw(chip->IO_ADDR_R);
937*cfcc706cSMiquel Raynal buf += 2;
938*cfcc706cSMiquel Raynal len -= 2;
939*cfcc706cSMiquel Raynal }
940*cfcc706cSMiquel Raynal }
941*cfcc706cSMiquel Raynal }
942*cfcc706cSMiquel Raynal
943*cfcc706cSMiquel Raynal /* copy aligned data */
944*cfcc706cSMiquel Raynal while (len >= 4) {
945*cfcc706cSMiquel Raynal *(u32 *)buf = readl(chip->IO_ADDR_R);
946*cfcc706cSMiquel Raynal buf += 4;
947*cfcc706cSMiquel Raynal len -= 4;
948*cfcc706cSMiquel Raynal }
949*cfcc706cSMiquel Raynal
950*cfcc706cSMiquel Raynal /* mop up any remaining bytes */
951*cfcc706cSMiquel Raynal if (len) {
952*cfcc706cSMiquel Raynal if (len >= 2) {
953*cfcc706cSMiquel Raynal *(u16 *)buf = readw(chip->IO_ADDR_R);
954*cfcc706cSMiquel Raynal buf += 2;
955*cfcc706cSMiquel Raynal len -= 2;
956*cfcc706cSMiquel Raynal }
957*cfcc706cSMiquel Raynal if (len)
958*cfcc706cSMiquel Raynal *buf = readb(chip->IO_ADDR_R);
959*cfcc706cSMiquel Raynal }
960*cfcc706cSMiquel Raynal }
961*cfcc706cSMiquel Raynal
962*cfcc706cSMiquel Raynal /*
963*cfcc706cSMiquel Raynal * zynq_nand_write_buf - write buffer to chip
964*cfcc706cSMiquel Raynal * @mtd: MTD device structure
965*cfcc706cSMiquel Raynal * @buf: data buffer
966*cfcc706cSMiquel Raynal * @len: number of bytes to write
967*cfcc706cSMiquel Raynal */
zynq_nand_write_buf(struct mtd_info * mtd,const u8 * buf,int len)968*cfcc706cSMiquel Raynal static void zynq_nand_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
969*cfcc706cSMiquel Raynal {
970*cfcc706cSMiquel Raynal struct nand_chip *chip = mtd->priv;
971*cfcc706cSMiquel Raynal const u32 *nand = chip->IO_ADDR_W;
972*cfcc706cSMiquel Raynal
973*cfcc706cSMiquel Raynal /* Make sure that buf is 32 bit aligned */
974*cfcc706cSMiquel Raynal if (((unsigned long)buf & 0x3) != 0) {
975*cfcc706cSMiquel Raynal if (((unsigned long)buf & 0x1) != 0) {
976*cfcc706cSMiquel Raynal if (len) {
977*cfcc706cSMiquel Raynal writeb(*buf, nand);
978*cfcc706cSMiquel Raynal buf += 1;
979*cfcc706cSMiquel Raynal len--;
980*cfcc706cSMiquel Raynal }
981*cfcc706cSMiquel Raynal }
982*cfcc706cSMiquel Raynal
983*cfcc706cSMiquel Raynal if (((unsigned long)buf & 0x3) != 0) {
984*cfcc706cSMiquel Raynal if (len >= 2) {
985*cfcc706cSMiquel Raynal writew(*(u16 *)buf, nand);
986*cfcc706cSMiquel Raynal buf += 2;
987*cfcc706cSMiquel Raynal len -= 2;
988*cfcc706cSMiquel Raynal }
989*cfcc706cSMiquel Raynal }
990*cfcc706cSMiquel Raynal }
991*cfcc706cSMiquel Raynal
992*cfcc706cSMiquel Raynal /* copy aligned data */
993*cfcc706cSMiquel Raynal while (len >= 4) {
994*cfcc706cSMiquel Raynal writel(*(u32 *)buf, nand);
995*cfcc706cSMiquel Raynal buf += 4;
996*cfcc706cSMiquel Raynal len -= 4;
997*cfcc706cSMiquel Raynal }
998*cfcc706cSMiquel Raynal
999*cfcc706cSMiquel Raynal /* mop up any remaining bytes */
1000*cfcc706cSMiquel Raynal if (len) {
1001*cfcc706cSMiquel Raynal if (len >= 2) {
1002*cfcc706cSMiquel Raynal writew(*(u16 *)buf, nand);
1003*cfcc706cSMiquel Raynal buf += 2;
1004*cfcc706cSMiquel Raynal len -= 2;
1005*cfcc706cSMiquel Raynal }
1006*cfcc706cSMiquel Raynal
1007*cfcc706cSMiquel Raynal if (len)
1008*cfcc706cSMiquel Raynal writeb(*buf, nand);
1009*cfcc706cSMiquel Raynal }
1010*cfcc706cSMiquel Raynal }
1011*cfcc706cSMiquel Raynal
1012*cfcc706cSMiquel Raynal /*
1013*cfcc706cSMiquel Raynal * zynq_nand_device_ready - Check device ready/busy line
1014*cfcc706cSMiquel Raynal * @mtd: Pointer to the mtd_info structure
1015*cfcc706cSMiquel Raynal *
1016*cfcc706cSMiquel Raynal * returns: 0 on busy or 1 on ready state
1017*cfcc706cSMiquel Raynal */
zynq_nand_device_ready(struct mtd_info * mtd)1018*cfcc706cSMiquel Raynal static int zynq_nand_device_ready(struct mtd_info *mtd)
1019*cfcc706cSMiquel Raynal {
1020*cfcc706cSMiquel Raynal u32 csr_val;
1021*cfcc706cSMiquel Raynal
1022*cfcc706cSMiquel Raynal csr_val = readl(&zynq_nand_smc_base->csr);
1023*cfcc706cSMiquel Raynal /* Check the raw_int_status1 bit */
1024*cfcc706cSMiquel Raynal if (csr_val & ZYNQ_MEMC_SR_RAW_INT_ST1) {
1025*cfcc706cSMiquel Raynal /* Clear the interrupt condition */
1026*cfcc706cSMiquel Raynal writel(ZYNQ_MEMC_SR_INT_ST1, &zynq_nand_smc_base->cfr);
1027*cfcc706cSMiquel Raynal return 1;
1028*cfcc706cSMiquel Raynal }
1029*cfcc706cSMiquel Raynal
1030*cfcc706cSMiquel Raynal return 0;
1031*cfcc706cSMiquel Raynal }
1032*cfcc706cSMiquel Raynal
zynq_nand_check_is_16bit_bw_flash(void)1033*cfcc706cSMiquel Raynal static int zynq_nand_check_is_16bit_bw_flash(void)
1034*cfcc706cSMiquel Raynal {
1035*cfcc706cSMiquel Raynal int is_16bit_bw = NAND_BW_UNKNOWN;
1036*cfcc706cSMiquel Raynal int mio_num_8bit = 0, mio_num_16bit = 0;
1037*cfcc706cSMiquel Raynal
1038*cfcc706cSMiquel Raynal mio_num_8bit = zynq_slcr_get_mio_pin_status("nand8");
1039*cfcc706cSMiquel Raynal if (mio_num_8bit == ZYNQ_NAND_MIO_NUM_NAND_8BIT)
1040*cfcc706cSMiquel Raynal is_16bit_bw = NAND_BW_8BIT;
1041*cfcc706cSMiquel Raynal
1042*cfcc706cSMiquel Raynal mio_num_16bit = zynq_slcr_get_mio_pin_status("nand16");
1043*cfcc706cSMiquel Raynal if (mio_num_8bit == ZYNQ_NAND_MIO_NUM_NAND_8BIT &&
1044*cfcc706cSMiquel Raynal mio_num_16bit == ZYNQ_NAND_MIO_NUM_NAND_16BIT)
1045*cfcc706cSMiquel Raynal is_16bit_bw = NAND_BW_16BIT;
1046*cfcc706cSMiquel Raynal
1047*cfcc706cSMiquel Raynal return is_16bit_bw;
1048*cfcc706cSMiquel Raynal }
1049*cfcc706cSMiquel Raynal
zynq_nand_init(struct nand_chip * nand_chip,int devnum)1050*cfcc706cSMiquel Raynal static int zynq_nand_init(struct nand_chip *nand_chip, int devnum)
1051*cfcc706cSMiquel Raynal {
1052*cfcc706cSMiquel Raynal struct zynq_nand_info *xnand;
1053*cfcc706cSMiquel Raynal struct mtd_info *mtd;
1054*cfcc706cSMiquel Raynal unsigned long ecc_page_size;
1055*cfcc706cSMiquel Raynal u8 maf_id, dev_id, i;
1056*cfcc706cSMiquel Raynal u8 get_feature[4];
1057*cfcc706cSMiquel Raynal u8 set_feature[4] = {ONDIE_ECC_FEATURE_ENABLE, 0x00, 0x00, 0x00};
1058*cfcc706cSMiquel Raynal unsigned long ecc_cfg;
1059*cfcc706cSMiquel Raynal int ondie_ecc_enabled = 0;
1060*cfcc706cSMiquel Raynal int err = -1;
1061*cfcc706cSMiquel Raynal int is_16bit_bw;
1062*cfcc706cSMiquel Raynal
1063*cfcc706cSMiquel Raynal xnand = calloc(1, sizeof(struct zynq_nand_info));
1064*cfcc706cSMiquel Raynal if (!xnand) {
1065*cfcc706cSMiquel Raynal printf("%s: failed to allocate\n", __func__);
1066*cfcc706cSMiquel Raynal goto fail;
1067*cfcc706cSMiquel Raynal }
1068*cfcc706cSMiquel Raynal
1069*cfcc706cSMiquel Raynal xnand->nand_base = (void __iomem *)ZYNQ_NAND_BASEADDR;
1070*cfcc706cSMiquel Raynal mtd = nand_to_mtd(nand_chip);
1071*cfcc706cSMiquel Raynal
1072*cfcc706cSMiquel Raynal nand_chip->priv = xnand;
1073*cfcc706cSMiquel Raynal mtd->priv = nand_chip;
1074*cfcc706cSMiquel Raynal
1075*cfcc706cSMiquel Raynal /* Set address of NAND IO lines */
1076*cfcc706cSMiquel Raynal nand_chip->IO_ADDR_R = xnand->nand_base;
1077*cfcc706cSMiquel Raynal nand_chip->IO_ADDR_W = xnand->nand_base;
1078*cfcc706cSMiquel Raynal
1079*cfcc706cSMiquel Raynal /* Set the driver entry points for MTD */
1080*cfcc706cSMiquel Raynal nand_chip->cmdfunc = zynq_nand_cmd_function;
1081*cfcc706cSMiquel Raynal nand_chip->dev_ready = zynq_nand_device_ready;
1082*cfcc706cSMiquel Raynal nand_chip->select_chip = zynq_nand_select_chip;
1083*cfcc706cSMiquel Raynal
1084*cfcc706cSMiquel Raynal /* If we don't set this delay driver sets 20us by default */
1085*cfcc706cSMiquel Raynal nand_chip->chip_delay = 30;
1086*cfcc706cSMiquel Raynal
1087*cfcc706cSMiquel Raynal /* Buffer read/write routines */
1088*cfcc706cSMiquel Raynal nand_chip->read_buf = zynq_nand_read_buf;
1089*cfcc706cSMiquel Raynal nand_chip->write_buf = zynq_nand_write_buf;
1090*cfcc706cSMiquel Raynal
1091*cfcc706cSMiquel Raynal is_16bit_bw = zynq_nand_check_is_16bit_bw_flash();
1092*cfcc706cSMiquel Raynal if (is_16bit_bw == NAND_BW_UNKNOWN) {
1093*cfcc706cSMiquel Raynal printf("%s: Unable detect NAND based on MIO settings\n",
1094*cfcc706cSMiquel Raynal __func__);
1095*cfcc706cSMiquel Raynal goto fail;
1096*cfcc706cSMiquel Raynal }
1097*cfcc706cSMiquel Raynal
1098*cfcc706cSMiquel Raynal if (is_16bit_bw == NAND_BW_16BIT)
1099*cfcc706cSMiquel Raynal nand_chip->options = NAND_BUSWIDTH_16;
1100*cfcc706cSMiquel Raynal
1101*cfcc706cSMiquel Raynal nand_chip->bbt_options = NAND_BBT_USE_FLASH;
1102*cfcc706cSMiquel Raynal
1103*cfcc706cSMiquel Raynal /* Initialize the NAND flash interface on NAND controller */
1104*cfcc706cSMiquel Raynal if (zynq_nand_init_nand_flash(nand_chip->options) < 0) {
1105*cfcc706cSMiquel Raynal printf("%s: nand flash init failed\n", __func__);
1106*cfcc706cSMiquel Raynal goto fail;
1107*cfcc706cSMiquel Raynal }
1108*cfcc706cSMiquel Raynal
1109*cfcc706cSMiquel Raynal /* first scan to find the device and get the page size */
1110*cfcc706cSMiquel Raynal if (nand_scan_ident(mtd, 1, NULL)) {
1111*cfcc706cSMiquel Raynal printf("%s: nand_scan_ident failed\n", __func__);
1112*cfcc706cSMiquel Raynal goto fail;
1113*cfcc706cSMiquel Raynal }
1114*cfcc706cSMiquel Raynal /* Send the command for reading device ID */
1115*cfcc706cSMiquel Raynal nand_chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1116*cfcc706cSMiquel Raynal nand_chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
1117*cfcc706cSMiquel Raynal
1118*cfcc706cSMiquel Raynal /* Read manufacturer and device IDs */
1119*cfcc706cSMiquel Raynal maf_id = nand_chip->read_byte(mtd);
1120*cfcc706cSMiquel Raynal dev_id = nand_chip->read_byte(mtd);
1121*cfcc706cSMiquel Raynal
1122*cfcc706cSMiquel Raynal if ((maf_id == 0x2c) && ((dev_id == 0xf1) ||
1123*cfcc706cSMiquel Raynal (dev_id == 0xa1) || (dev_id == 0xb1) ||
1124*cfcc706cSMiquel Raynal (dev_id == 0xaa) || (dev_id == 0xba) ||
1125*cfcc706cSMiquel Raynal (dev_id == 0xda) || (dev_id == 0xca) ||
1126*cfcc706cSMiquel Raynal (dev_id == 0xac) || (dev_id == 0xbc) ||
1127*cfcc706cSMiquel Raynal (dev_id == 0xdc) || (dev_id == 0xcc) ||
1128*cfcc706cSMiquel Raynal (dev_id == 0xa3) || (dev_id == 0xb3) ||
1129*cfcc706cSMiquel Raynal (dev_id == 0xd3) || (dev_id == 0xc3))) {
1130*cfcc706cSMiquel Raynal nand_chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES,
1131*cfcc706cSMiquel Raynal ONDIE_ECC_FEATURE_ADDR, -1);
1132*cfcc706cSMiquel Raynal for (i = 0; i < 4; i++)
1133*cfcc706cSMiquel Raynal writeb(set_feature[i], nand_chip->IO_ADDR_W);
1134*cfcc706cSMiquel Raynal
1135*cfcc706cSMiquel Raynal /* Wait for 1us after writing data with SET_FEATURES command */
1136*cfcc706cSMiquel Raynal ndelay(1000);
1137*cfcc706cSMiquel Raynal
1138*cfcc706cSMiquel Raynal nand_chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES,
1139*cfcc706cSMiquel Raynal ONDIE_ECC_FEATURE_ADDR, -1);
1140*cfcc706cSMiquel Raynal nand_chip->read_buf(mtd, get_feature, 4);
1141*cfcc706cSMiquel Raynal
1142*cfcc706cSMiquel Raynal if (get_feature[0] & ONDIE_ECC_FEATURE_ENABLE) {
1143*cfcc706cSMiquel Raynal debug("%s: OnDie ECC flash\n", __func__);
1144*cfcc706cSMiquel Raynal ondie_ecc_enabled = 1;
1145*cfcc706cSMiquel Raynal } else {
1146*cfcc706cSMiquel Raynal printf("%s: Unable to detect OnDie ECC\n", __func__);
1147*cfcc706cSMiquel Raynal }
1148*cfcc706cSMiquel Raynal }
1149*cfcc706cSMiquel Raynal
1150*cfcc706cSMiquel Raynal if (ondie_ecc_enabled) {
1151*cfcc706cSMiquel Raynal /* Bypass the controller ECC block */
1152*cfcc706cSMiquel Raynal ecc_cfg = readl(&zynq_nand_smc_base->emcr);
1153*cfcc706cSMiquel Raynal ecc_cfg &= ~ZYNQ_MEMC_NAND_ECC_MODE_MASK;
1154*cfcc706cSMiquel Raynal writel(ecc_cfg, &zynq_nand_smc_base->emcr);
1155*cfcc706cSMiquel Raynal
1156*cfcc706cSMiquel Raynal /* The software ECC routines won't work
1157*cfcc706cSMiquel Raynal * with the SMC controller
1158*cfcc706cSMiquel Raynal */
1159*cfcc706cSMiquel Raynal nand_chip->ecc.mode = NAND_ECC_HW;
1160*cfcc706cSMiquel Raynal nand_chip->ecc.strength = 1;
1161*cfcc706cSMiquel Raynal nand_chip->ecc.read_page = zynq_nand_read_page_raw_nooob;
1162*cfcc706cSMiquel Raynal nand_chip->ecc.read_subpage = zynq_nand_read_subpage_raw;
1163*cfcc706cSMiquel Raynal nand_chip->ecc.write_page = zynq_nand_write_page_raw;
1164*cfcc706cSMiquel Raynal nand_chip->ecc.read_page_raw = zynq_nand_read_page_raw;
1165*cfcc706cSMiquel Raynal nand_chip->ecc.write_page_raw = zynq_nand_write_page_raw;
1166*cfcc706cSMiquel Raynal nand_chip->ecc.read_oob = zynq_nand_read_oob;
1167*cfcc706cSMiquel Raynal nand_chip->ecc.write_oob = zynq_nand_write_oob;
1168*cfcc706cSMiquel Raynal nand_chip->ecc.size = mtd->writesize;
1169*cfcc706cSMiquel Raynal nand_chip->ecc.bytes = 0;
1170*cfcc706cSMiquel Raynal
1171*cfcc706cSMiquel Raynal /* NAND with on-die ECC supports subpage reads */
1172*cfcc706cSMiquel Raynal nand_chip->options |= NAND_SUBPAGE_READ;
1173*cfcc706cSMiquel Raynal
1174*cfcc706cSMiquel Raynal /* On-Die ECC spare bytes offset 8 is used for ECC codes */
1175*cfcc706cSMiquel Raynal if (ondie_ecc_enabled) {
1176*cfcc706cSMiquel Raynal nand_chip->ecc.layout = &ondie_nand_oob_64;
1177*cfcc706cSMiquel Raynal /* Use the BBT pattern descriptors */
1178*cfcc706cSMiquel Raynal nand_chip->bbt_td = &bbt_main_descr;
1179*cfcc706cSMiquel Raynal nand_chip->bbt_md = &bbt_mirror_descr;
1180*cfcc706cSMiquel Raynal }
1181*cfcc706cSMiquel Raynal } else {
1182*cfcc706cSMiquel Raynal /* Hardware ECC generates 3 bytes ECC code for each 512 bytes */
1183*cfcc706cSMiquel Raynal nand_chip->ecc.mode = NAND_ECC_HW;
1184*cfcc706cSMiquel Raynal nand_chip->ecc.strength = 1;
1185*cfcc706cSMiquel Raynal nand_chip->ecc.size = ZYNQ_NAND_ECC_SIZE;
1186*cfcc706cSMiquel Raynal nand_chip->ecc.bytes = 3;
1187*cfcc706cSMiquel Raynal nand_chip->ecc.calculate = zynq_nand_calculate_hwecc;
1188*cfcc706cSMiquel Raynal nand_chip->ecc.correct = zynq_nand_correct_data;
1189*cfcc706cSMiquel Raynal nand_chip->ecc.hwctl = NULL;
1190*cfcc706cSMiquel Raynal nand_chip->ecc.read_page = zynq_nand_read_page_hwecc;
1191*cfcc706cSMiquel Raynal nand_chip->ecc.write_page = zynq_nand_write_page_hwecc;
1192*cfcc706cSMiquel Raynal nand_chip->ecc.read_page_raw = zynq_nand_read_page_raw;
1193*cfcc706cSMiquel Raynal nand_chip->ecc.write_page_raw = zynq_nand_write_page_raw;
1194*cfcc706cSMiquel Raynal nand_chip->ecc.read_oob = zynq_nand_read_oob;
1195*cfcc706cSMiquel Raynal nand_chip->ecc.write_oob = zynq_nand_write_oob;
1196*cfcc706cSMiquel Raynal
1197*cfcc706cSMiquel Raynal switch (mtd->writesize) {
1198*cfcc706cSMiquel Raynal case 512:
1199*cfcc706cSMiquel Raynal ecc_page_size = 0x1;
1200*cfcc706cSMiquel Raynal /* Set the ECC memory config register */
1201*cfcc706cSMiquel Raynal writel((ZYNQ_NAND_ECC_CONFIG | ecc_page_size),
1202*cfcc706cSMiquel Raynal &zynq_nand_smc_base->emcr);
1203*cfcc706cSMiquel Raynal break;
1204*cfcc706cSMiquel Raynal case 1024:
1205*cfcc706cSMiquel Raynal ecc_page_size = 0x2;
1206*cfcc706cSMiquel Raynal /* Set the ECC memory config register */
1207*cfcc706cSMiquel Raynal writel((ZYNQ_NAND_ECC_CONFIG | ecc_page_size),
1208*cfcc706cSMiquel Raynal &zynq_nand_smc_base->emcr);
1209*cfcc706cSMiquel Raynal break;
1210*cfcc706cSMiquel Raynal case 2048:
1211*cfcc706cSMiquel Raynal ecc_page_size = 0x3;
1212*cfcc706cSMiquel Raynal /* Set the ECC memory config register */
1213*cfcc706cSMiquel Raynal writel((ZYNQ_NAND_ECC_CONFIG | ecc_page_size),
1214*cfcc706cSMiquel Raynal &zynq_nand_smc_base->emcr);
1215*cfcc706cSMiquel Raynal break;
1216*cfcc706cSMiquel Raynal default:
1217*cfcc706cSMiquel Raynal nand_chip->ecc.mode = NAND_ECC_SOFT;
1218*cfcc706cSMiquel Raynal nand_chip->ecc.calculate = nand_calculate_ecc;
1219*cfcc706cSMiquel Raynal nand_chip->ecc.correct = nand_correct_data;
1220*cfcc706cSMiquel Raynal nand_chip->ecc.read_page = zynq_nand_read_page_swecc;
1221*cfcc706cSMiquel Raynal nand_chip->ecc.write_page = zynq_nand_write_page_swecc;
1222*cfcc706cSMiquel Raynal nand_chip->ecc.size = 256;
1223*cfcc706cSMiquel Raynal break;
1224*cfcc706cSMiquel Raynal }
1225*cfcc706cSMiquel Raynal
1226*cfcc706cSMiquel Raynal if (mtd->oobsize == 16)
1227*cfcc706cSMiquel Raynal nand_chip->ecc.layout = &nand_oob_16;
1228*cfcc706cSMiquel Raynal else if (mtd->oobsize == 64)
1229*cfcc706cSMiquel Raynal nand_chip->ecc.layout = &nand_oob_64;
1230*cfcc706cSMiquel Raynal else
1231*cfcc706cSMiquel Raynal printf("%s: No oob layout found\n", __func__);
1232*cfcc706cSMiquel Raynal }
1233*cfcc706cSMiquel Raynal
1234*cfcc706cSMiquel Raynal /* Second phase scan */
1235*cfcc706cSMiquel Raynal if (nand_scan_tail(mtd)) {
1236*cfcc706cSMiquel Raynal printf("%s: nand_scan_tail failed\n", __func__);
1237*cfcc706cSMiquel Raynal goto fail;
1238*cfcc706cSMiquel Raynal }
1239*cfcc706cSMiquel Raynal if (nand_register(devnum, mtd))
1240*cfcc706cSMiquel Raynal goto fail;
1241*cfcc706cSMiquel Raynal return 0;
1242*cfcc706cSMiquel Raynal fail:
1243*cfcc706cSMiquel Raynal free(xnand);
1244*cfcc706cSMiquel Raynal return err;
1245*cfcc706cSMiquel Raynal }
1246*cfcc706cSMiquel Raynal
1247*cfcc706cSMiquel Raynal static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
1248*cfcc706cSMiquel Raynal
board_nand_init(void)1249*cfcc706cSMiquel Raynal void board_nand_init(void)
1250*cfcc706cSMiquel Raynal {
1251*cfcc706cSMiquel Raynal struct nand_chip *nand = &nand_chip[0];
1252*cfcc706cSMiquel Raynal
1253*cfcc706cSMiquel Raynal if (zynq_nand_init(nand, 0))
1254*cfcc706cSMiquel Raynal puts("ZYNQ NAND init failed\n");
1255*cfcc706cSMiquel Raynal }
1256