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