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