blob: 9c704c60e8eec2c9888418e5bd5b8ccf6fc36291 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Dirk Behme778933f2008-12-14 09:47:16 +01002/*
3 * (C) Copyright 2004-2008 Texas Instruments, <www.ti.com>
4 * Rohit Choraria <rohitkc@ti.com>
Dirk Behme778933f2008-12-14 09:47:16 +01005 */
6
Tom Rinidec7ea02024-05-20 13:35:03 -06007#include <config.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Sean Anderson11a4c702023-11-04 16:37:41 -04009#include <system-constants.h>
Dirk Behme778933f2008-12-14 09:47:16 +010010#include <asm/io.h>
Roger Quadrose52ec9e2024-01-11 15:19:18 +020011#include <dm.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090012#include <linux/errno.h>
Roger Quadros6550c622022-10-11 14:49:59 +030013
14#ifdef CONFIG_ARCH_OMAP2PLUS
Dirk Behme778933f2008-12-14 09:47:16 +010015#include <asm/arch/mem.h>
Roger Quadros6550c622022-10-11 14:49:59 +030016#endif
17
Roger Quadrose52ec9e2024-01-11 15:19:18 +020018#include <linux/io.h>
19#include <linux/ioport.h>
pekon gupta5bbb0992013-11-22 16:53:29 +053020#include <linux/mtd/omap_gpmc.h>
Dirk Behme778933f2008-12-14 09:47:16 +010021#include <linux/mtd/nand_ecc.h>
Tom Rini3bde7e22021-09-22 14:50:35 -040022#include <linux/mtd/rawnand.h>
Andreas Bießmann82a65472013-04-05 04:55:21 +000023#include <linux/bch.h>
Stefano Babicaade5792012-03-21 23:56:17 +000024#include <linux/compiler.h>
Dirk Behme778933f2008-12-14 09:47:16 +010025#include <nand.h>
Roger Quadrosa42b7702022-12-20 12:22:03 +020026
27#include "omap_elm.h"
pekon gupta6bd91a82013-11-18 19:03:00 +053028
Roger Quadros6550c622022-10-11 14:49:59 +030029#ifndef GPMC_MAX_CS
30#define GPMC_MAX_CS 4
31#endif
32
pekon gupta6bd91a82013-11-18 19:03:00 +053033#define BADBLOCK_MARKER_LENGTH 2
34#define SECTOR_BYTES 512
Roger Quadrosaa418fb2022-12-20 12:21:56 +020035#define ECCSIZE0_SHIFT 12
36#define ECCSIZE1_SHIFT 22
37#define ECC1RESULTSIZE 0x1
pekon guptaeff10ee2013-11-19 11:02:15 +053038#define ECCCLEAR (0x1 << 8)
39#define ECCRESULTREG1 (0x1 << 0)
pekon guptab0f750a2013-11-19 11:02:17 +053040/* 4 bit padding to make byte aligned, 56 = 52 + 4 */
41#define BCH4_BIT_PAD 4
42
pekon gupta03742c92013-11-19 11:02:16 +053043#ifdef CONFIG_BCH
44static u8 bch8_polynomial[] = {0xef, 0x51, 0x2e, 0x09, 0xed, 0x93, 0x9a, 0xc2,
45 0x97, 0x79, 0xe5, 0x24, 0xb5};
46#endif
Rostislav Lisovy77cdf8a2014-09-02 16:23:58 +020047static uint8_t cs_next;
Dirk Behme778933f2008-12-14 09:47:16 +010048
Michal Sojka26160072015-02-17 17:08:37 +010049#if defined(CONFIG_NAND_OMAP_GPMC_WSCFG)
50static const int8_t wscfg[CONFIG_SYS_MAX_NAND_DEVICE] =
51 { CONFIG_NAND_OMAP_GPMC_WSCFG };
52#else
53/* wscfg is preset to zero since its a static variable */
54static const int8_t wscfg[CONFIG_SYS_MAX_NAND_DEVICE];
55#endif
56
Dirk Behme778933f2008-12-14 09:47:16 +010057/*
Rostislav Lisovy77cdf8a2014-09-02 16:23:58 +020058 * Driver configurations
59 */
60struct omap_nand_info {
61 struct bch_control *control;
62 enum omap_ecc ecc_scheme;
Michal Sojka26160072015-02-17 17:08:37 +010063 uint8_t cs;
64 uint8_t ws; /* wait status pin (0,1) */
Roger Quadros3b7aaa52022-10-11 14:50:02 +030065 void __iomem *fifo;
Rostislav Lisovy77cdf8a2014-09-02 16:23:58 +020066};
67
68/* We are wasting a bit of memory but al least we are safe */
69static struct omap_nand_info omap_nand_info[GPMC_MAX_CS];
70
71/*
Dirk Behme778933f2008-12-14 09:47:16 +010072 * omap_nand_hwcontrol - Set the address pointers corretly for the
73 * following address/data/command operation
74 */
75static void omap_nand_hwcontrol(struct mtd_info *mtd, int32_t cmd,
76 uint32_t ctrl)
77{
Scott Wood17fed142016-05-30 13:57:56 -050078 register struct nand_chip *this = mtd_to_nand(mtd);
79 struct omap_nand_info *info = nand_get_controller_data(this);
Rostislav Lisovy77cdf8a2014-09-02 16:23:58 +020080 int cs = info->cs;
Dirk Behme778933f2008-12-14 09:47:16 +010081
82 /*
83 * Point the IO_ADDR to DATA and ADDRESS registers instead
84 * of chip address
85 */
86 switch (ctrl) {
87 case NAND_CTRL_CHANGE | NAND_CTRL_CLE:
Dirk Behmea4becd62009-08-08 09:30:22 +020088 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
Dirk Behme778933f2008-12-14 09:47:16 +010089 break;
90 case NAND_CTRL_CHANGE | NAND_CTRL_ALE:
Dirk Behmea4becd62009-08-08 09:30:22 +020091 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_adr;
Dirk Behme778933f2008-12-14 09:47:16 +010092 break;
93 case NAND_CTRL_CHANGE | NAND_NCE:
Dirk Behmea4becd62009-08-08 09:30:22 +020094 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
Dirk Behme778933f2008-12-14 09:47:16 +010095 break;
96 }
97
98 if (cmd != NAND_CMD_NONE)
99 writeb(cmd, this->IO_ADDR_W);
100}
101
Simon Schwarz4f62e982011-09-14 15:30:16 -0400102/* Check wait pin as dev ready indicator */
Stefan Roesee05972f2014-11-13 03:43:39 +0100103static int omap_dev_ready(struct mtd_info *mtd)
Simon Schwarz4f62e982011-09-14 15:30:16 -0400104{
Scott Wood17fed142016-05-30 13:57:56 -0500105 register struct nand_chip *this = mtd_to_nand(mtd);
106 struct omap_nand_info *info = nand_get_controller_data(this);
Michal Sojka26160072015-02-17 17:08:37 +0100107 return gpmc_cfg->status & (1 << (8 + info->ws));
Simon Schwarz4f62e982011-09-14 15:30:16 -0400108}
Dirk Behme778933f2008-12-14 09:47:16 +0100109
110/*
111 * gen_true_ecc - This function will generate true ECC value, which
112 * can be used when correcting data read from NAND flash memory core
113 *
114 * @ecc_buf: buffer to store ecc code
115 *
116 * @return: re-formatted ECC value
117 */
118static uint32_t gen_true_ecc(uint8_t *ecc_buf)
119{
120 return ecc_buf[0] | (ecc_buf[1] << 16) | ((ecc_buf[2] & 0xF0) << 20) |
121 ((ecc_buf[2] & 0x0F) << 8);
122}
123
124/*
125 * omap_correct_data - Compares the ecc read from nand spare area with ECC
Vagrant Cascadianedfdb992016-04-30 19:18:00 -0700126 * registers values and corrects one bit error if it has occurred
Dirk Behme778933f2008-12-14 09:47:16 +0100127 * Further details can be had from OMAP TRM and the following selected links:
128 * http://en.wikipedia.org/wiki/Hamming_code
129 * http://www.cs.utexas.edu/users/plaxton/c/337/05f/slides/ErrorCorrection-4.pdf
130 *
131 * @mtd: MTD device structure
132 * @dat: page data
133 * @read_ecc: ecc read from nand flash
134 * @calc_ecc: ecc read from ECC registers
135 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100136 * Return: 0 if data is OK or corrected, else returns -1
Dirk Behme778933f2008-12-14 09:47:16 +0100137 */
Stefano Babicaade5792012-03-21 23:56:17 +0000138static int __maybe_unused omap_correct_data(struct mtd_info *mtd, uint8_t *dat,
Dirk Behme778933f2008-12-14 09:47:16 +0100139 uint8_t *read_ecc, uint8_t *calc_ecc)
140{
141 uint32_t orig_ecc, new_ecc, res, hm;
142 uint16_t parity_bits, byte;
143 uint8_t bit;
144
145 /* Regenerate the orginal ECC */
146 orig_ecc = gen_true_ecc(read_ecc);
147 new_ecc = gen_true_ecc(calc_ecc);
148 /* Get the XOR of real ecc */
149 res = orig_ecc ^ new_ecc;
150 if (res) {
151 /* Get the hamming width */
152 hm = hweight32(res);
153 /* Single bit errors can be corrected! */
154 if (hm == 12) {
155 /* Correctable data! */
156 parity_bits = res >> 16;
157 bit = (parity_bits & 0x7);
158 byte = (parity_bits >> 3) & 0x1FF;
159 /* Flip the bit to correct */
160 dat[byte] ^= (0x1 << bit);
161 } else if (hm == 1) {
162 printf("Error: Ecc is wrong\n");
163 /* ECC itself is corrupted */
164 return 2;
165 } else {
166 /*
167 * hm distance != parity pairs OR one, could mean 2 bit
168 * error OR potentially be on a blank page..
169 * orig_ecc: contains spare area data from nand flash.
170 * new_ecc: generated ecc while reading data area.
171 * Note: if the ecc = 0, all data bits from which it was
172 * generated are 0xFF.
173 * The 3 byte(24 bits) ecc is generated per 512byte
174 * chunk of a page. If orig_ecc(from spare area)
175 * is 0xFF && new_ecc(computed now from data area)=0x0,
176 * this means that data area is 0xFF and spare area is
177 * 0xFF. A sure sign of a erased page!
178 */
179 if ((orig_ecc == 0x0FFF0FFF) && (new_ecc == 0x00000000))
180 return 0;
181 printf("Error: Bad compare! failed\n");
182 /* detected 2 bit error */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500183 return -EBADMSG;
Dirk Behme778933f2008-12-14 09:47:16 +0100184 }
185 }
186 return 0;
187}
188
189/*
pekon guptaeff10ee2013-11-19 11:02:15 +0530190 * omap_enable_hwecc - configures GPMC as per ECC scheme before read/write
Andreas Bießmann82a65472013-04-05 04:55:21 +0000191 * @mtd: MTD device structure
192 * @mode: Read/Write mode
193 */
194__maybe_unused
pekon guptaeff10ee2013-11-19 11:02:15 +0530195static void omap_enable_hwecc(struct mtd_info *mtd, int32_t mode)
Andreas Bießmann82a65472013-04-05 04:55:21 +0000196{
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200197 struct nand_chip *nand = mtd_to_nand(mtd);
198 struct omap_nand_info *info = nand_get_controller_data(nand);
pekon guptaeff10ee2013-11-19 11:02:15 +0530199 unsigned int dev_width = (nand->options & NAND_BUSWIDTH_16) ? 1 : 0;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200200 u32 val;
Andreas Bießmann82a65472013-04-05 04:55:21 +0000201
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200202 /* Clear ecc and enable bits */
203 writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
204
205 /* program ecc and result sizes */
206 val = ((((nand->ecc.size >> 1) - 1) << ECCSIZE1_SHIFT) |
207 ECC1RESULTSIZE);
208 writel(val, &gpmc_cfg->ecc_size_config);
209
210 switch (mode) {
211 case NAND_ECC_READ:
212 case NAND_ECC_WRITE:
213 writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
pekon guptaeff10ee2013-11-19 11:02:15 +0530214 break;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200215 case NAND_ECC_READSYN:
216 writel(ECCCLEAR, &gpmc_cfg->ecc_control);
pekon gupta046cf862014-06-02 17:14:42 +0530217 break;
pekon guptaeff10ee2013-11-19 11:02:15 +0530218 default:
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200219 printf("%s: error: unrecognized Mode[%d]!\n", __func__, mode);
220 break;
pekon gupta6bd91a82013-11-18 19:03:00 +0530221 }
Andreas Bießmann82a65472013-04-05 04:55:21 +0000222
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200223 /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
224 val = (dev_width << 7) | (info->cs << 1) | (0x1);
225 writel(val, &gpmc_cfg->ecc_config);
Andreas Bießmann82a65472013-04-05 04:55:21 +0000226}
227
228/*
pekon gupta03742c92013-11-19 11:02:16 +0530229 * omap_calculate_ecc - Read ECC result
230 * @mtd: MTD structure
231 * @dat: unused
232 * @ecc_code: ecc_code buffer
233 * Using noninverted ECC can be considered ugly since writing a blank
234 * page ie. padding will clear the ECC bytes. This is no problem as
235 * long nobody is trying to write data on the seemingly unused page.
236 * Reading an erased page will produce an ECC mismatch between
237 * generated and read ECC bytes that has to be dealt with separately.
238 * E.g. if page is 0xFF (fresh erased), and if HW ECC engine within GPMC
239 * is used, the result of read will be 0x0 while the ECC offsets of the
240 * spare area will be 0xFF which will result in an ECC mismatch.
Mansoor Ahamede5612512012-11-06 13:06:33 +0000241 */
pekon gupta03742c92013-11-19 11:02:16 +0530242static int omap_calculate_ecc(struct mtd_info *mtd, const uint8_t *dat,
Mansoor Ahamede5612512012-11-06 13:06:33 +0000243 uint8_t *ecc_code)
244{
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200245 u32 val;
246
247 val = readl(&gpmc_cfg->ecc1_result);
248 ecc_code[0] = val & 0xFF;
249 ecc_code[1] = (val >> 16) & 0xFF;
250 ecc_code[2] = ((val >> 8) & 0x0F) | ((val >> 20) & 0xF0);
251
252 return 0;
253}
254
255/* GPMC ecc engine settings for read */
256#define BCH_WRAPMODE_1 1 /* BCH wrap mode 1 */
257#define BCH8R_ECC_SIZE0 0x1a /* ecc_size0 = 26 */
258#define BCH8R_ECC_SIZE1 0x2 /* ecc_size1 = 2 */
259#define BCH4R_ECC_SIZE0 0xd /* ecc_size0 = 13 */
260#define BCH4R_ECC_SIZE1 0x3 /* ecc_size1 = 3 */
261
262/* GPMC ecc engine settings for write */
263#define BCH_WRAPMODE_6 6 /* BCH wrap mode 6 */
264#define BCH_ECC_SIZE0 0x0 /* ecc_size0 = 0, no oob protection */
265#define BCH_ECC_SIZE1 0x20 /* ecc_size1 = 32 */
266
267/**
268 * omap_enable_hwecc_bch - Program GPMC to perform BCH ECC calculation
269 * @mtd: MTD device structure
270 * @mode: Read/Write mode
271 *
272 * When using BCH with SW correction (i.e. no ELM), sector size is set
273 * to 512 bytes and we use BCH_WRAPMODE_6 wrapping mode
274 * for both reading and writing with:
275 * eccsize0 = 0 (no additional protected byte in spare area)
276 * eccsize1 = 32 (skip 32 nibbles = 16 bytes per sector in spare area)
277 */
278static void __maybe_unused omap_enable_hwecc_bch(struct mtd_info *mtd,
279 int mode)
280{
281 unsigned int bch_type;
282 unsigned int dev_width, nsectors;
Scott Wood17fed142016-05-30 13:57:56 -0500283 struct nand_chip *chip = mtd_to_nand(mtd);
284 struct omap_nand_info *info = nand_get_controller_data(chip);
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200285 u32 val, wr_mode;
286 unsigned int ecc_size1, ecc_size0;
287
288 /* GPMC configurations for calculating ECC */
289 switch (info->ecc_scheme) {
290 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
291 bch_type = 1;
292 nsectors = 1;
293 wr_mode = BCH_WRAPMODE_6;
294 ecc_size0 = BCH_ECC_SIZE0;
295 ecc_size1 = BCH_ECC_SIZE1;
296 break;
297 case OMAP_ECC_BCH8_CODE_HW:
298 bch_type = 1;
Roger Quadrosde8100f2023-12-11 13:45:59 +0200299 nsectors = 1;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200300 if (mode == NAND_ECC_READ) {
301 wr_mode = BCH_WRAPMODE_1;
302 ecc_size0 = BCH8R_ECC_SIZE0;
303 ecc_size1 = BCH8R_ECC_SIZE1;
304 } else {
305 wr_mode = BCH_WRAPMODE_6;
306 ecc_size0 = BCH_ECC_SIZE0;
307 ecc_size1 = BCH_ECC_SIZE1;
308 }
309 break;
310 case OMAP_ECC_BCH16_CODE_HW:
311 bch_type = 0x2;
Roger Quadrosde8100f2023-12-11 13:45:59 +0200312 nsectors = 1;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200313 if (mode == NAND_ECC_READ) {
314 wr_mode = 0x01;
315 ecc_size0 = 52; /* ECC bits in nibbles per sector */
316 ecc_size1 = 0; /* non-ECC bits in nibbles per sector */
317 } else {
318 wr_mode = 0x01;
319 ecc_size0 = 0; /* extra bits in nibbles per sector */
320 ecc_size1 = 52; /* OOB bits in nibbles per sector */
321 }
322 break;
323 default:
324 return;
325 }
326
327 writel(ECCRESULTREG1, &gpmc_cfg->ecc_control);
328
329 /* Configure ecc size for BCH */
330 val = (ecc_size1 << ECCSIZE1_SHIFT) | (ecc_size0 << ECCSIZE0_SHIFT);
331 writel(val, &gpmc_cfg->ecc_size_config);
332
333 dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
334
335 /* BCH configuration */
336 val = ((1 << 16) | /* enable BCH */
337 (bch_type << 12) | /* BCH4/BCH8/BCH16 */
338 (wr_mode << 8) | /* wrap mode */
339 (dev_width << 7) | /* bus width */
340 (((nsectors - 1) & 0x7) << 4) | /* number of sectors */
341 (info->cs << 1) | /* ECC CS */
342 (0x1)); /* enable ECC */
343
344 writel(val, &gpmc_cfg->ecc_config);
345
346 /* Clear ecc and enable bits */
347 writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
348}
349
350/**
Roger Quadrosde8100f2023-12-11 13:45:59 +0200351 * omap_calculate_ecc_bch - Generate BCH ECC bytes for one sector
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200352 * @mtd: MTD device structure
353 * @dat: The pointer to data on which ecc is computed
354 * @ecc_code: The ecc_code buffer
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200355 *
356 * Support calculating of BCH4/8/16 ECC vectors for one sector
357 * within a page. Sector number is in @sector.
358 */
Roger Quadrosde8100f2023-12-11 13:45:59 +0200359static int __maybe_unused omap_calculate_ecc_bch(struct mtd_info *mtd, const u8 *dat,
360 u8 *ecc_code)
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200361{
362 struct nand_chip *chip = mtd_to_nand(mtd);
363 struct omap_nand_info *info = nand_get_controller_data(chip);
Ladislav Michld5b1c272016-07-12 20:28:16 +0200364 const uint32_t *ptr;
365 uint32_t val = 0;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000366 int8_t i = 0, j;
367
pekon guptaaa168482014-04-11 12:55:33 +0530368 switch (info->ecc_scheme) {
pekon gupta03742c92013-11-19 11:02:16 +0530369#ifdef CONFIG_BCH
370 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
371#endif
372 case OMAP_ECC_BCH8_CODE_HW:
Roger Quadrosde8100f2023-12-11 13:45:59 +0200373 ptr = &gpmc_cfg->bch_result_0_3[0].bch_result_x[3];
pekon gupta03742c92013-11-19 11:02:16 +0530374 val = readl(ptr);
375 ecc_code[i++] = (val >> 0) & 0xFF;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000376 ptr--;
377 for (j = 0; j < 3; j++) {
pekon gupta03742c92013-11-19 11:02:16 +0530378 val = readl(ptr);
379 ecc_code[i++] = (val >> 24) & 0xFF;
380 ecc_code[i++] = (val >> 16) & 0xFF;
381 ecc_code[i++] = (val >> 8) & 0xFF;
382 ecc_code[i++] = (val >> 0) & 0xFF;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000383 ptr--;
384 }
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200385
pekon gupta03742c92013-11-19 11:02:16 +0530386 break;
pekon gupta046cf862014-06-02 17:14:42 +0530387 case OMAP_ECC_BCH16_CODE_HW:
Roger Quadrosde8100f2023-12-11 13:45:59 +0200388 val = readl(&gpmc_cfg->bch_result_4_6[0].bch_result_x[2]);
pekon gupta046cf862014-06-02 17:14:42 +0530389 ecc_code[i++] = (val >> 8) & 0xFF;
390 ecc_code[i++] = (val >> 0) & 0xFF;
Roger Quadrosde8100f2023-12-11 13:45:59 +0200391 val = readl(&gpmc_cfg->bch_result_4_6[0].bch_result_x[1]);
pekon gupta046cf862014-06-02 17:14:42 +0530392 ecc_code[i++] = (val >> 24) & 0xFF;
393 ecc_code[i++] = (val >> 16) & 0xFF;
394 ecc_code[i++] = (val >> 8) & 0xFF;
395 ecc_code[i++] = (val >> 0) & 0xFF;
Roger Quadrosde8100f2023-12-11 13:45:59 +0200396 val = readl(&gpmc_cfg->bch_result_4_6[0].bch_result_x[0]);
pekon gupta046cf862014-06-02 17:14:42 +0530397 ecc_code[i++] = (val >> 24) & 0xFF;
398 ecc_code[i++] = (val >> 16) & 0xFF;
399 ecc_code[i++] = (val >> 8) & 0xFF;
400 ecc_code[i++] = (val >> 0) & 0xFF;
401 for (j = 3; j >= 0; j--) {
Roger Quadrosde8100f2023-12-11 13:45:59 +0200402 val = readl(&gpmc_cfg->bch_result_0_3[0].bch_result_x[j]
pekon gupta046cf862014-06-02 17:14:42 +0530403 );
404 ecc_code[i++] = (val >> 24) & 0xFF;
405 ecc_code[i++] = (val >> 16) & 0xFF;
406 ecc_code[i++] = (val >> 8) & 0xFF;
407 ecc_code[i++] = (val >> 0) & 0xFF;
408 }
409 break;
pekon gupta03742c92013-11-19 11:02:16 +0530410 default:
411 return -EINVAL;
412 }
413 /* ECC scheme specific syndrome customizations */
pekon guptaaa168482014-04-11 12:55:33 +0530414 switch (info->ecc_scheme) {
pekon gupta03742c92013-11-19 11:02:16 +0530415#ifdef CONFIG_BCH
416 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200417 /* Add constant polynomial to remainder, so that
418 * ECC of blank pages results in 0x0 on reading back
419 */
pekon gupta03742c92013-11-19 11:02:16 +0530420 for (i = 0; i < chip->ecc.bytes; i++)
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200421 ecc_code[i] ^= bch8_polynomial[i];
pekon gupta03742c92013-11-19 11:02:16 +0530422 break;
423#endif
424 case OMAP_ECC_BCH8_CODE_HW:
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200425 /* Set 14th ECC byte as 0x0 for ROM compatibility */
426 ecc_code[chip->ecc.bytes - 1] = 0x0;
pekon gupta03742c92013-11-19 11:02:16 +0530427 break;
pekon gupta046cf862014-06-02 17:14:42 +0530428 case OMAP_ECC_BCH16_CODE_HW:
429 break;
pekon gupta03742c92013-11-19 11:02:16 +0530430 default:
431 return -EINVAL;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000432 }
pekon gupta03742c92013-11-19 11:02:16 +0530433 return 0;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000434}
435
Roger Quadros3b7aaa52022-10-11 14:50:02 +0300436static inline void omap_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
437{
438 struct nand_chip *chip = mtd_to_nand(mtd);
439 struct omap_nand_info *info = nand_get_controller_data(chip);
440 u32 alignment = ((uintptr_t)buf | len) & 3;
441
442 if (alignment & 1)
443 readsb(info->fifo, buf, len);
444 else if (alignment & 3)
445 readsw(info->fifo, buf, len >> 1);
446 else
447 readsl(info->fifo, buf, len >> 2);
448}
449
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200450#ifdef CONFIG_NAND_OMAP_GPMC_PREFETCH
451
452#define PREFETCH_CONFIG1_CS_SHIFT 24
453#define PREFETCH_FIFOTHRESHOLD_MAX 0x40
454#define PREFETCH_FIFOTHRESHOLD(val) ((val) << 8)
455#define PREFETCH_STATUS_COUNT(val) (val & 0x00003fff)
456#define PREFETCH_STATUS_FIFO_CNT(val) ((val >> 24) & 0x7F)
457#define ENABLE_PREFETCH (1 << 7)
458
459/**
460 * omap_prefetch_enable - configures and starts prefetch transfer
461 * @fifo_th: fifo threshold to be used for read/ write
462 * @count: number of bytes to be transferred
463 * @is_write: prefetch read(0) or write post(1) mode
464 * @cs: chip select to use
465 */
466static int omap_prefetch_enable(int fifo_th, unsigned int count, int is_write, int cs)
467{
468 uint32_t val;
469
470 if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX)
471 return -EINVAL;
472
473 if (readl(&gpmc_cfg->prefetch_control))
474 return -EBUSY;
475
476 /* Set the amount of bytes to be prefetched */
477 writel(count, &gpmc_cfg->prefetch_config2);
478
479 val = (cs << PREFETCH_CONFIG1_CS_SHIFT) | (is_write & 1) |
480 PREFETCH_FIFOTHRESHOLD(fifo_th) | ENABLE_PREFETCH;
481 writel(val, &gpmc_cfg->prefetch_config1);
482
483 /* Start the prefetch engine */
484 writel(1, &gpmc_cfg->prefetch_control);
485
486 return 0;
487}
488
489/**
490 * omap_prefetch_reset - disables and stops the prefetch engine
491 */
492static void omap_prefetch_reset(void)
493{
494 writel(0, &gpmc_cfg->prefetch_control);
495 writel(0, &gpmc_cfg->prefetch_config1);
496}
497
498static int __read_prefetch_aligned(struct nand_chip *chip, uint32_t *buf, int len)
499{
500 int ret;
501 uint32_t cnt;
Scott Wood17fed142016-05-30 13:57:56 -0500502 struct omap_nand_info *info = nand_get_controller_data(chip);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200503
504 ret = omap_prefetch_enable(PREFETCH_FIFOTHRESHOLD_MAX, len, 0, info->cs);
505 if (ret < 0)
506 return ret;
507
508 do {
509 int i;
510
511 cnt = readl(&gpmc_cfg->prefetch_status);
512 cnt = PREFETCH_STATUS_FIFO_CNT(cnt);
513
514 for (i = 0; i < cnt / 4; i++) {
Roger Quadros3b7aaa52022-10-11 14:50:02 +0300515 *buf++ = readl(info->fifo);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200516 len -= 4;
517 }
518 } while (len);
519
520 omap_prefetch_reset();
521
522 return 0;
523}
524
Jeroen Hofstee5e67ac72015-05-30 10:11:24 +0200525static void omap_nand_read_prefetch(struct mtd_info *mtd, uint8_t *buf, int len)
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200526{
527 int ret;
Roger Quadros3068f0f2022-10-11 14:50:01 +0300528 uintptr_t head, tail;
Scott Wood17fed142016-05-30 13:57:56 -0500529 struct nand_chip *chip = mtd_to_nand(mtd);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200530
531 /*
532 * If the destination buffer is unaligned, start with reading
533 * the overlap byte-wise.
534 */
Roger Quadros3068f0f2022-10-11 14:50:01 +0300535 head = ((uintptr_t)buf) % 4;
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200536 if (head) {
Roger Quadros3b7aaa52022-10-11 14:50:02 +0300537 omap_nand_read_buf(mtd, buf, head);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200538 buf += head;
539 len -= head;
540 }
541
542 /*
543 * Only transfer multiples of 4 bytes in a pre-fetched fashion.
544 * If there's a residue, care for it byte-wise afterwards.
545 */
546 tail = len % 4;
547
Jeroen Hofstee5e67ac72015-05-30 10:11:24 +0200548 ret = __read_prefetch_aligned(chip, (uint32_t *)buf, len - tail);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200549 if (ret < 0) {
550 /* fallback in case the prefetch engine is busy */
Roger Quadros3b7aaa52022-10-11 14:50:02 +0300551 omap_nand_read_buf(mtd, buf, len);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200552 } else if (tail) {
553 buf += len - tail;
Roger Quadros3b7aaa52022-10-11 14:50:02 +0300554 omap_nand_read_buf(mtd, buf, tail);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200555 }
556}
557#endif /* CONFIG_NAND_OMAP_GPMC_PREFETCH */
558
pekon gupta03742c92013-11-19 11:02:16 +0530559#ifdef CONFIG_NAND_OMAP_ELM
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200560
Mansoor Ahamede5612512012-11-06 13:06:33 +0000561/*
Jeroen Hofstee79369072014-10-08 22:57:42 +0200562 * omap_reverse_list - re-orders list elements in reverse order [internal]
563 * @list: pointer to start of list
564 * @length: length of list
565*/
566static void omap_reverse_list(u8 *list, unsigned int length)
567{
568 unsigned int i, j;
569 unsigned int half_length = length / 2;
570 u8 tmp;
571 for (i = 0, j = length - 1; i < half_length; i++, j--) {
572 tmp = list[i];
573 list[i] = list[j];
574 list[j] = tmp;
575 }
576}
577
578/*
Mansoor Ahamede5612512012-11-06 13:06:33 +0000579 * omap_correct_data_bch - Compares the ecc read from nand spare area
Vagrant Cascadianedfdb992016-04-30 19:18:00 -0700580 * with ECC registers values and corrects one bit error if it has occurred
Mansoor Ahamede5612512012-11-06 13:06:33 +0000581 *
582 * @mtd: MTD device structure
583 * @dat: page data
584 * @read_ecc: ecc read from nand flash (ignored)
585 * @calc_ecc: ecc read from ECC registers
586 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100587 * Return: 0 if data is OK or corrected, else returns -1
Mansoor Ahamede5612512012-11-06 13:06:33 +0000588 */
589static int omap_correct_data_bch(struct mtd_info *mtd, uint8_t *dat,
590 uint8_t *read_ecc, uint8_t *calc_ecc)
591{
Scott Wood17fed142016-05-30 13:57:56 -0500592 struct nand_chip *chip = mtd_to_nand(mtd);
593 struct omap_nand_info *info = nand_get_controller_data(chip);
pekon gupta3c43c5b2014-04-11 12:55:34 +0530594 struct nand_ecc_ctrl *ecc = &chip->ecc;
pekon guptab0f750a2013-11-19 11:02:17 +0530595 uint32_t error_count = 0, error_max;
pekon gupta046cf862014-06-02 17:14:42 +0530596 uint32_t error_loc[ELM_MAX_ERROR_COUNT];
pekon gupta9d4b7472014-04-11 12:55:32 +0530597 enum bch_level bch_type;
pekon guptab0f750a2013-11-19 11:02:17 +0530598 uint32_t i, ecc_flag = 0;
Guido Martínez20b27be2015-01-02 14:49:10 -0300599 uint8_t count;
pekon guptab0f750a2013-11-19 11:02:17 +0530600 uint32_t byte_pos, bit_pos;
Guido Martínez20b27be2015-01-02 14:49:10 -0300601 int err = 0;
pekon guptab0f750a2013-11-19 11:02:17 +0530602
603 /* check calculated ecc */
pekon gupta3c43c5b2014-04-11 12:55:34 +0530604 for (i = 0; i < ecc->bytes && !ecc_flag; i++) {
pekon guptab0f750a2013-11-19 11:02:17 +0530605 if (calc_ecc[i] != 0x00)
David Rivshinefa78532021-11-18 13:25:24 -0500606 goto not_ecc_match;
pekon guptab0f750a2013-11-19 11:02:17 +0530607 }
David Rivshinefa78532021-11-18 13:25:24 -0500608 return 0;
609not_ecc_match:
Mansoor Ahamede5612512012-11-06 13:06:33 +0000610
David Rivshinefa78532021-11-18 13:25:24 -0500611 /* check for whether it's an erased-page */
612 for (i = 0; i < ecc->bytes; i++) {
Mansoor Ahamede5612512012-11-06 13:06:33 +0000613 if (read_ecc[i] != 0xff)
David Rivshinefa78532021-11-18 13:25:24 -0500614 goto not_erased;
pekon guptab0f750a2013-11-19 11:02:17 +0530615 }
David Rivshinefa78532021-11-18 13:25:24 -0500616 for (i = 0; i < SECTOR_BYTES; i++) {
617 if (dat[i] != 0xff)
618 goto not_erased;
619 }
620 return 0;
621not_erased:
622
623 /*
624 * Check for whether it's an erased page with a correctable
625 * number of bitflips. Erased pages have all 1's in the data,
626 * so we just compute the number of 0 bits in the data and
627 * see if it's under the correction threshold.
628 *
629 * NOTE: The check for a perfect erased page above is faster for
630 * the more common case, even though it's logically redundant.
631 */
632 for (i = 0; i < ecc->bytes; i++)
633 error_count += hweight8(~read_ecc[i]);
634
635 for (i = 0; i < SECTOR_BYTES; i++)
636 error_count += hweight8(~dat[i]);
637
638 if (error_count <= ecc->strength) {
639 memset(read_ecc, 0xFF, ecc->bytes);
640 memset(dat, 0xFF, SECTOR_BYTES);
641 debug("nand: %u bit-flip(s) corrected in erased page\n",
642 error_count);
643 return error_count;
644 }
Mansoor Ahamede5612512012-11-06 13:06:33 +0000645
Mansoor Ahamede5612512012-11-06 13:06:33 +0000646 /*
647 * while reading ECC result we read it in big endian.
648 * Hence while loading to ELM we have rotate to get the right endian.
649 */
pekon guptaaa168482014-04-11 12:55:33 +0530650 switch (info->ecc_scheme) {
pekon guptab0f750a2013-11-19 11:02:17 +0530651 case OMAP_ECC_BCH8_CODE_HW:
pekon gupta9d4b7472014-04-11 12:55:32 +0530652 bch_type = BCH_8_BIT;
pekon gupta3c43c5b2014-04-11 12:55:34 +0530653 omap_reverse_list(calc_ecc, ecc->bytes - 1);
pekon guptab0f750a2013-11-19 11:02:17 +0530654 break;
pekon gupta046cf862014-06-02 17:14:42 +0530655 case OMAP_ECC_BCH16_CODE_HW:
656 bch_type = BCH_16_BIT;
657 omap_reverse_list(calc_ecc, ecc->bytes);
658 break;
pekon guptab0f750a2013-11-19 11:02:17 +0530659 default:
660 return -EINVAL;
661 }
Mansoor Ahamede5612512012-11-06 13:06:33 +0000662 /* use elm module to check for errors */
pekon gupta9d4b7472014-04-11 12:55:32 +0530663 elm_config(bch_type);
David Rivshinefa78532021-11-18 13:25:24 -0500664 error_count = 0;
pekon guptacfe6b8a2014-04-11 12:55:35 +0530665 err = elm_check_error(calc_ecc, bch_type, &error_count, error_loc);
666 if (err)
667 return err;
668
Mansoor Ahamede5612512012-11-06 13:06:33 +0000669 /* correct bch error */
pekon guptab0f750a2013-11-19 11:02:17 +0530670 for (count = 0; count < error_count; count++) {
pekon guptaaa168482014-04-11 12:55:33 +0530671 switch (info->ecc_scheme) {
pekon gupta9d4b7472014-04-11 12:55:32 +0530672 case OMAP_ECC_BCH8_CODE_HW:
pekon guptab0f750a2013-11-19 11:02:17 +0530673 /* 14th byte in ECC is reserved to match ROM layout */
pekon gupta3c43c5b2014-04-11 12:55:34 +0530674 error_max = SECTOR_BYTES + (ecc->bytes - 1);
pekon guptab0f750a2013-11-19 11:02:17 +0530675 break;
pekon gupta046cf862014-06-02 17:14:42 +0530676 case OMAP_ECC_BCH16_CODE_HW:
677 error_max = SECTOR_BYTES + ecc->bytes;
678 break;
pekon guptab0f750a2013-11-19 11:02:17 +0530679 default:
680 return -EINVAL;
681 }
682 byte_pos = error_max - (error_loc[count] / 8) - 1;
683 bit_pos = error_loc[count] % 8;
684 if (byte_pos < SECTOR_BYTES) {
685 dat[byte_pos] ^= 1 << bit_pos;
Ezequiel García69cf8ad2015-10-04 18:34:42 -0300686 debug("nand: bit-flip corrected @data=%d\n", byte_pos);
pekon guptab0f750a2013-11-19 11:02:17 +0530687 } else if (byte_pos < error_max) {
Belisko Marek9ab54142014-04-25 12:00:07 +0200688 read_ecc[byte_pos - SECTOR_BYTES] ^= 1 << bit_pos;
Ezequiel García69cf8ad2015-10-04 18:34:42 -0300689 debug("nand: bit-flip corrected @oob=%d\n", byte_pos -
pekon guptab0f750a2013-11-19 11:02:17 +0530690 SECTOR_BYTES);
691 } else {
692 err = -EBADMSG;
693 printf("nand: error: invalid bit-flip location\n");
694 }
695 }
696 return (err) ? err : error_count;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000697}
Mansoor Ahamede5612512012-11-06 13:06:33 +0000698
699/**
700 * omap_read_page_bch - hardware ecc based page read function
701 * @mtd: mtd info structure
702 * @chip: nand chip info structure
703 * @buf: buffer to store read data
Sergey Lapin3a38a552013-01-14 03:46:50 +0000704 * @oob_required: caller expects OOB data read to chip->oob_poi
Mansoor Ahamede5612512012-11-06 13:06:33 +0000705 * @page: page number to read
706 *
707 */
708static int omap_read_page_bch(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000709 uint8_t *buf, int oob_required, int page)
Mansoor Ahamede5612512012-11-06 13:06:33 +0000710{
711 int i, eccsize = chip->ecc.size;
712 int eccbytes = chip->ecc.bytes;
713 int eccsteps = chip->ecc.steps;
714 uint8_t *p = buf;
715 uint8_t *ecc_calc = chip->buffers->ecccalc;
716 uint8_t *ecc_code = chip->buffers->ecccode;
717 uint32_t *eccpos = chip->ecc.layout->eccpos;
718 uint8_t *oob = chip->oob_poi;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000719 uint32_t oob_pos;
Roger Quadrosde8100f2023-12-11 13:45:59 +0200720 u32 data_pos = 0;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000721
Mansoor Ahamede5612512012-11-06 13:06:33 +0000722 /* oob area start */
723 oob_pos = (eccsize * eccsteps) + chip->ecc.layout->eccpos[0];
724 oob += chip->ecc.layout->eccpos[0];
725
Roger Quadrosde8100f2023-12-11 13:45:59 +0200726 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize,
727 oob += eccbytes) {
728 /* Enable ECC engine */
729 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Mansoor Ahamede5612512012-11-06 13:06:33 +0000730
Roger Quadrosde8100f2023-12-11 13:45:59 +0200731 /* read data */
732 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_pos, -1);
733 chip->read_buf(mtd, p, eccsize);
Mansoor Ahamede5612512012-11-06 13:06:33 +0000734
Roger Quadrosde8100f2023-12-11 13:45:59 +0200735 /* read respective ecc from oob area */
736 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_pos, -1);
737 chip->read_buf(mtd, oob, eccbytes);
738 /* read syndrome */
739 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200740
Roger Quadrosde8100f2023-12-11 13:45:59 +0200741 data_pos += eccsize;
742 oob_pos += eccbytes;
743 }
Mansoor Ahamede5612512012-11-06 13:06:33 +0000744
745 for (i = 0; i < chip->ecc.total; i++)
746 ecc_code[i] = chip->oob_poi[eccpos[i]];
747
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200748 /* error detect & correct */
Mansoor Ahamede5612512012-11-06 13:06:33 +0000749 eccsteps = chip->ecc.steps;
750 p = buf;
751
752 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
753 int stat;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000754 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
755 if (stat < 0)
756 mtd->ecc_stats.failed++;
757 else
758 mtd->ecc_stats.corrected += stat;
759 }
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200760
Mansoor Ahamede5612512012-11-06 13:06:33 +0000761 return 0;
762}
pekon gupta6bd91a82013-11-18 19:03:00 +0530763#endif /* CONFIG_NAND_OMAP_ELM */
Mansoor Ahamede5612512012-11-06 13:06:33 +0000764
Andreas Bießmann82a65472013-04-05 04:55:21 +0000765/*
766 * OMAP3 BCH8 support (with BCH library)
767 */
pekon gupta6bd91a82013-11-18 19:03:00 +0530768#ifdef CONFIG_BCH
Andreas Bießmann82a65472013-04-05 04:55:21 +0000769/**
pekon gupta6bd91a82013-11-18 19:03:00 +0530770 * omap_correct_data_bch_sw - Decode received data and correct errors
Andreas Bießmann82a65472013-04-05 04:55:21 +0000771 * @mtd: MTD device structure
772 * @data: page data
773 * @read_ecc: ecc read from nand flash
774 * @calc_ecc: ecc read from HW ECC registers
775 */
pekon gupta6bd91a82013-11-18 19:03:00 +0530776static int omap_correct_data_bch_sw(struct mtd_info *mtd, u_char *data,
Andreas Bießmann82a65472013-04-05 04:55:21 +0000777 u_char *read_ecc, u_char *calc_ecc)
778{
779 int i, count;
780 /* cannot correct more than 8 errors */
781 unsigned int errloc[8];
Scott Wood17fed142016-05-30 13:57:56 -0500782 struct nand_chip *chip = mtd_to_nand(mtd);
783 struct omap_nand_info *info = nand_get_controller_data(chip);
Andreas Bießmann82a65472013-04-05 04:55:21 +0000784
Ladislav Michl6cc51852017-01-09 11:15:14 +0100785 count = decode_bch(info->control, NULL, SECTOR_BYTES,
786 read_ecc, calc_ecc, NULL, errloc);
Andreas Bießmann82a65472013-04-05 04:55:21 +0000787 if (count > 0) {
788 /* correct errors */
789 for (i = 0; i < count; i++) {
790 /* correct data only, not ecc bytes */
Ladislav Michl6cc51852017-01-09 11:15:14 +0100791 if (errloc[i] < SECTOR_BYTES << 3)
792 data[errloc[i] >> 3] ^= 1 << (errloc[i] & 7);
Ezequiel García69cf8ad2015-10-04 18:34:42 -0300793 debug("corrected bitflip %u\n", errloc[i]);
Andreas Bießmann82a65472013-04-05 04:55:21 +0000794#ifdef DEBUG
795 puts("read_ecc: ");
796 /*
797 * BCH8 have 13 bytes of ECC; BCH4 needs adoption
798 * here!
799 */
800 for (i = 0; i < 13; i++)
801 printf("%02x ", read_ecc[i]);
802 puts("\n");
803 puts("calc_ecc: ");
804 for (i = 0; i < 13; i++)
805 printf("%02x ", calc_ecc[i]);
806 puts("\n");
807#endif
808 }
809 } else if (count < 0) {
810 puts("ecc unrecoverable error\n");
811 }
812 return count;
813}
814
815/**
816 * omap_free_bch - Release BCH ecc resources
817 * @mtd: MTD device structure
818 */
819static void __maybe_unused omap_free_bch(struct mtd_info *mtd)
820{
Scott Wood17fed142016-05-30 13:57:56 -0500821 struct nand_chip *chip = mtd_to_nand(mtd);
822 struct omap_nand_info *info = nand_get_controller_data(chip);
Andreas Bießmann82a65472013-04-05 04:55:21 +0000823
pekon guptaaa168482014-04-11 12:55:33 +0530824 if (info->control) {
825 free_bch(info->control);
826 info->control = NULL;
Andreas Bießmann82a65472013-04-05 04:55:21 +0000827 }
828}
pekon gupta6bd91a82013-11-18 19:03:00 +0530829#endif /* CONFIG_BCH */
830
831/**
832 * omap_select_ecc_scheme - configures driver for particular ecc-scheme
833 * @nand: NAND chip device structure
834 * @ecc_scheme: ecc scheme to configure
835 * @pagesize: number of main-area bytes per page of NAND device
836 * @oobsize: number of OOB/spare bytes per page of NAND device
837 */
838static int omap_select_ecc_scheme(struct nand_chip *nand,
839 enum omap_ecc ecc_scheme, unsigned int pagesize, unsigned int oobsize) {
Scott Wood17fed142016-05-30 13:57:56 -0500840 struct omap_nand_info *info = nand_get_controller_data(nand);
Roger Quadros224add22022-10-11 14:50:06 +0300841 struct nand_ecclayout *ecclayout = nand->ecc.layout;
pekon gupta6bd91a82013-11-18 19:03:00 +0530842 int eccsteps = pagesize / SECTOR_BYTES;
843 int i;
844
845 switch (ecc_scheme) {
846 case OMAP_ECC_HAM1_CODE_SW:
847 debug("nand: selected OMAP_ECC_HAM1_CODE_SW\n");
848 /* For this ecc-scheme, ecc.bytes, ecc.layout, ... are
849 * initialized in nand_scan_tail(), so just set ecc.mode */
pekon guptaaa168482014-04-11 12:55:33 +0530850 info->control = NULL;
pekon gupta6bd91a82013-11-18 19:03:00 +0530851 nand->ecc.mode = NAND_ECC_SOFT;
852 nand->ecc.layout = NULL;
Nikita Kiryanov4110c822013-12-12 15:19:31 +0200853 nand->ecc.size = 0;
pekon gupta6bd91a82013-11-18 19:03:00 +0530854 break;
855
856 case OMAP_ECC_HAM1_CODE_HW:
857 debug("nand: selected OMAP_ECC_HAM1_CODE_HW\n");
858 /* check ecc-scheme requirements before updating ecc info */
859 if ((3 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
860 printf("nand: error: insufficient OOB: require=%d\n", (
861 (3 * eccsteps) + BADBLOCK_MARKER_LENGTH));
862 return -EINVAL;
863 }
pekon guptaaa168482014-04-11 12:55:33 +0530864 info->control = NULL;
pekon gupta6bd91a82013-11-18 19:03:00 +0530865 /* populate ecc specific fields */
Nikita Kiryanov2e18ff22013-12-17 15:18:01 +0200866 memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
pekon gupta6bd91a82013-11-18 19:03:00 +0530867 nand->ecc.mode = NAND_ECC_HW;
868 nand->ecc.strength = 1;
869 nand->ecc.size = SECTOR_BYTES;
870 nand->ecc.bytes = 3;
871 nand->ecc.hwctl = omap_enable_hwecc;
872 nand->ecc.correct = omap_correct_data;
873 nand->ecc.calculate = omap_calculate_ecc;
874 /* define ecc-layout */
875 ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
pekon guptaf0aff022013-12-05 17:54:21 +0530876 for (i = 0; i < ecclayout->eccbytes; i++) {
877 if (nand->options & NAND_BUSWIDTH_16)
878 ecclayout->eccpos[i] = i + 2;
879 else
880 ecclayout->eccpos[i] = i + 1;
881 }
pekon gupta6bd91a82013-11-18 19:03:00 +0530882 ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
883 ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
884 BADBLOCK_MARKER_LENGTH;
pekon gupta6bd91a82013-11-18 19:03:00 +0530885 break;
886
887 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
888#ifdef CONFIG_BCH
889 debug("nand: selected OMAP_ECC_BCH8_CODE_HW_DETECTION_SW\n");
890 /* check ecc-scheme requirements before updating ecc info */
891 if ((13 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
892 printf("nand: error: insufficient OOB: require=%d\n", (
893 (13 * eccsteps) + BADBLOCK_MARKER_LENGTH));
894 return -EINVAL;
895 }
896 /* check if BCH S/W library can be used for error detection */
pekon guptaaa168482014-04-11 12:55:33 +0530897 info->control = init_bch(13, 8, 0x201b);
898 if (!info->control) {
pekon gupta6bd91a82013-11-18 19:03:00 +0530899 printf("nand: error: could not init_bch()\n");
900 return -ENODEV;
901 }
pekon gupta6bd91a82013-11-18 19:03:00 +0530902 /* populate ecc specific fields */
Nikita Kiryanov2e18ff22013-12-17 15:18:01 +0200903 memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
pekon gupta6bd91a82013-11-18 19:03:00 +0530904 nand->ecc.mode = NAND_ECC_HW;
905 nand->ecc.strength = 8;
906 nand->ecc.size = SECTOR_BYTES;
907 nand->ecc.bytes = 13;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200908 nand->ecc.hwctl = omap_enable_hwecc_bch;
pekon gupta6bd91a82013-11-18 19:03:00 +0530909 nand->ecc.correct = omap_correct_data_bch_sw;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200910 nand->ecc.calculate = omap_calculate_ecc_bch;
Roger Quadrosde8100f2023-12-11 13:45:59 +0200911 nand->ecc.steps = eccsteps;
pekon gupta6bd91a82013-11-18 19:03:00 +0530912 /* define ecc-layout */
913 ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
914 ecclayout->eccpos[0] = BADBLOCK_MARKER_LENGTH;
915 for (i = 1; i < ecclayout->eccbytes; i++) {
916 if (i % nand->ecc.bytes)
917 ecclayout->eccpos[i] =
918 ecclayout->eccpos[i - 1] + 1;
919 else
920 ecclayout->eccpos[i] =
921 ecclayout->eccpos[i - 1] + 2;
922 }
923 ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
924 ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
925 BADBLOCK_MARKER_LENGTH;
pekon gupta6bd91a82013-11-18 19:03:00 +0530926 break;
927#else
928 printf("nand: error: CONFIG_BCH required for ECC\n");
929 return -EINVAL;
930#endif
931
932 case OMAP_ECC_BCH8_CODE_HW:
933#ifdef CONFIG_NAND_OMAP_ELM
934 debug("nand: selected OMAP_ECC_BCH8_CODE_HW\n");
935 /* check ecc-scheme requirements before updating ecc info */
936 if ((14 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
937 printf("nand: error: insufficient OOB: require=%d\n", (
938 (14 * eccsteps) + BADBLOCK_MARKER_LENGTH));
939 return -EINVAL;
940 }
941 /* intialize ELM for ECC error detection */
942 elm_init();
pekon guptaaa168482014-04-11 12:55:33 +0530943 info->control = NULL;
pekon gupta6bd91a82013-11-18 19:03:00 +0530944 /* populate ecc specific fields */
Nikita Kiryanov2e18ff22013-12-17 15:18:01 +0200945 memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
pekon gupta6bd91a82013-11-18 19:03:00 +0530946 nand->ecc.mode = NAND_ECC_HW;
947 nand->ecc.strength = 8;
948 nand->ecc.size = SECTOR_BYTES;
949 nand->ecc.bytes = 14;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200950 nand->ecc.hwctl = omap_enable_hwecc_bch;
pekon gupta6bd91a82013-11-18 19:03:00 +0530951 nand->ecc.correct = omap_correct_data_bch;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200952 nand->ecc.calculate = omap_calculate_ecc_bch;
pekon gupta6bd91a82013-11-18 19:03:00 +0530953 nand->ecc.read_page = omap_read_page_bch;
Roger Quadrosde8100f2023-12-11 13:45:59 +0200954 nand->ecc.steps = eccsteps;
pekon gupta6bd91a82013-11-18 19:03:00 +0530955 /* define ecc-layout */
956 ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
957 for (i = 0; i < ecclayout->eccbytes; i++)
958 ecclayout->eccpos[i] = i + BADBLOCK_MARKER_LENGTH;
959 ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
960 ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
961 BADBLOCK_MARKER_LENGTH;
pekon gupta6bd91a82013-11-18 19:03:00 +0530962 break;
963#else
964 printf("nand: error: CONFIG_NAND_OMAP_ELM required for ECC\n");
965 return -EINVAL;
966#endif
967
pekon gupta046cf862014-06-02 17:14:42 +0530968 case OMAP_ECC_BCH16_CODE_HW:
969#ifdef CONFIG_NAND_OMAP_ELM
970 debug("nand: using OMAP_ECC_BCH16_CODE_HW\n");
971 /* check ecc-scheme requirements before updating ecc info */
972 if ((26 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
973 printf("nand: error: insufficient OOB: require=%d\n", (
974 (26 * eccsteps) + BADBLOCK_MARKER_LENGTH));
975 return -EINVAL;
976 }
977 /* intialize ELM for ECC error detection */
978 elm_init();
979 /* populate ecc specific fields */
980 nand->ecc.mode = NAND_ECC_HW;
981 nand->ecc.size = SECTOR_BYTES;
982 nand->ecc.bytes = 26;
983 nand->ecc.strength = 16;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200984 nand->ecc.hwctl = omap_enable_hwecc_bch;
pekon gupta046cf862014-06-02 17:14:42 +0530985 nand->ecc.correct = omap_correct_data_bch;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200986 nand->ecc.calculate = omap_calculate_ecc_bch;
pekon gupta046cf862014-06-02 17:14:42 +0530987 nand->ecc.read_page = omap_read_page_bch;
Roger Quadrosde8100f2023-12-11 13:45:59 +0200988 nand->ecc.steps = eccsteps;
pekon gupta046cf862014-06-02 17:14:42 +0530989 /* define ecc-layout */
990 ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
991 for (i = 0; i < ecclayout->eccbytes; i++)
992 ecclayout->eccpos[i] = i + BADBLOCK_MARKER_LENGTH;
993 ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
994 ecclayout->oobfree[0].length = oobsize - nand->ecc.bytes -
995 BADBLOCK_MARKER_LENGTH;
996 break;
997#else
998 printf("nand: error: CONFIG_NAND_OMAP_ELM required for ECC\n");
999 return -EINVAL;
1000#endif
pekon gupta6bd91a82013-11-18 19:03:00 +05301001 default:
1002 debug("nand: error: ecc scheme not enabled or supported\n");
1003 return -EINVAL;
1004 }
Nikita Kiryanove8167892013-12-16 19:19:01 +02001005
1006 /* nand_scan_tail() sets ham1 sw ecc; hw ecc layout is set by driver */
1007 if (ecc_scheme != OMAP_ECC_HAM1_CODE_SW)
1008 nand->ecc.layout = ecclayout;
1009
pekon guptaaa168482014-04-11 12:55:33 +05301010 info->ecc_scheme = ecc_scheme;
pekon gupta6bd91a82013-11-18 19:03:00 +05301011 return 0;
1012}
Andreas Bießmann82a65472013-04-05 04:55:21 +00001013
Simon Glass7ec24132024-09-29 19:49:48 -06001014#ifndef CONFIG_XPL_BUILD
Dirk Behme778933f2008-12-14 09:47:16 +01001015/*
Andreas Bießmann1e4eccf2013-04-04 23:52:50 +00001016 * omap_nand_switch_ecc - switch the ECC operation between different engines
1017 * (h/w and s/w) and different algorithms (hamming and BCHx)
Dirk Behme778933f2008-12-14 09:47:16 +01001018 *
Andreas Bießmann1e4eccf2013-04-04 23:52:50 +00001019 * @hardware - true if one of the HW engines should be used
1020 * @eccstrength - the number of bits that could be corrected
1021 * (1 - hamming, 4 - BCH4, 8 - BCH8, 16 - BCH16)
Dirk Behme778933f2008-12-14 09:47:16 +01001022 */
pekon gupta6bd91a82013-11-18 19:03:00 +05301023int __maybe_unused omap_nand_switch_ecc(uint32_t hardware, uint32_t eccstrength)
Dirk Behme778933f2008-12-14 09:47:16 +01001024{
1025 struct nand_chip *nand;
Mugunthan V N7b670cc2017-06-26 19:12:51 -05001026 struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
pekon gupta6bd91a82013-11-18 19:03:00 +05301027 int err = 0;
Dirk Behme778933f2008-12-14 09:47:16 +01001028
Mugunthan V N7b670cc2017-06-26 19:12:51 -05001029 if (!mtd) {
pekon gupta6bd91a82013-11-18 19:03:00 +05301030 printf("nand: error: no NAND devices found\n");
1031 return -ENODEV;
Dirk Behme778933f2008-12-14 09:47:16 +01001032 }
1033
Scott Wood17fed142016-05-30 13:57:56 -05001034 nand = mtd_to_nand(mtd);
Dirk Behme778933f2008-12-14 09:47:16 +01001035 nand->options |= NAND_OWN_BUFFERS;
Jeroen Hofstee96306f22014-01-15 17:58:54 +01001036 nand->options &= ~NAND_SUBPAGE_READ;
Dirk Behme778933f2008-12-14 09:47:16 +01001037 /* Setup the ecc configurations again */
Andreas Bießmann1e4eccf2013-04-04 23:52:50 +00001038 if (hardware) {
1039 if (eccstrength == 1) {
pekon gupta6bd91a82013-11-18 19:03:00 +05301040 err = omap_select_ecc_scheme(nand,
1041 OMAP_ECC_HAM1_CODE_HW,
1042 mtd->writesize, mtd->oobsize);
1043 } else if (eccstrength == 8) {
1044 err = omap_select_ecc_scheme(nand,
1045 OMAP_ECC_BCH8_CODE_HW,
1046 mtd->writesize, mtd->oobsize);
Heiko Schocher5bf904c2016-06-07 08:55:42 +02001047 } else if (eccstrength == 16) {
1048 err = omap_select_ecc_scheme(nand,
1049 OMAP_ECC_BCH16_CODE_HW,
1050 mtd->writesize, mtd->oobsize);
pekon gupta6bd91a82013-11-18 19:03:00 +05301051 } else {
1052 printf("nand: error: unsupported ECC scheme\n");
1053 return -EINVAL;
Andreas Bießmann1e4eccf2013-04-04 23:52:50 +00001054 }
Dirk Behme778933f2008-12-14 09:47:16 +01001055 } else {
Ash Charles4a5faa82015-02-18 11:25:11 -08001056 if (eccstrength == 1) {
1057 err = omap_select_ecc_scheme(nand,
1058 OMAP_ECC_HAM1_CODE_SW,
1059 mtd->writesize, mtd->oobsize);
1060 } else if (eccstrength == 8) {
1061 err = omap_select_ecc_scheme(nand,
1062 OMAP_ECC_BCH8_CODE_HW_DETECTION_SW,
pekon gupta6bd91a82013-11-18 19:03:00 +05301063 mtd->writesize, mtd->oobsize);
Ash Charles4a5faa82015-02-18 11:25:11 -08001064 } else {
1065 printf("nand: error: unsupported ECC scheme\n");
1066 return -EINVAL;
1067 }
Dirk Behme778933f2008-12-14 09:47:16 +01001068 }
1069
1070 /* Update NAND handling after ECC mode switch */
pekon gupta6bd91a82013-11-18 19:03:00 +05301071 if (!err)
1072 err = nand_scan_tail(mtd);
1073 return err;
Dirk Behme778933f2008-12-14 09:47:16 +01001074}
Simon Glass7ec24132024-09-29 19:49:48 -06001075#endif /* CONFIG_XPL_BUILD */
Dirk Behme778933f2008-12-14 09:47:16 +01001076
1077/*
1078 * Board-specific NAND initialization. The following members of the
1079 * argument are board-specific:
1080 * - IO_ADDR_R: address to read the 8 I/O lines of the flash device
1081 * - IO_ADDR_W: address to write the 8 I/O lines of the flash device
1082 * - cmd_ctrl: hardwarespecific function for accesing control-lines
1083 * - waitfunc: hardwarespecific function for accesing device ready/busy line
1084 * - ecc.hwctl: function to enable (reset) hardware ecc generator
1085 * - ecc.mode: mode of ecc, see defines
1086 * - chip_delay: chip dependent delay for transfering data from array to
1087 * read regs (tR)
1088 * - options: various chip options. They can partly be set to inform
1089 * nand_scan about special functionality. See the defines for further
1090 * explanation
1091 */
Roger Quadrose52ec9e2024-01-11 15:19:18 +02001092int gpmc_nand_init(struct nand_chip *nand, void __iomem *nand_base)
Dirk Behme778933f2008-12-14 09:47:16 +01001093{
1094 int32_t gpmc_config = 0;
Rostislav Lisovy77cdf8a2014-09-02 16:23:58 +02001095 int cs = cs_next++;
pekon gupta6bd91a82013-11-18 19:03:00 +05301096 int err = 0;
Roger Quadros3b7aaa52022-10-11 14:50:02 +03001097 struct omap_nand_info *info;
1098
Dirk Behme778933f2008-12-14 09:47:16 +01001099 /*
1100 * xloader/Uboot's gpmc configuration would have configured GPMC for
1101 * nand type of memory. The following logic scans and latches on to the
1102 * first CS with NAND type memory.
1103 * TBD: need to make this logic generic to handle multiple CS NAND
1104 * devices.
1105 */
1106 while (cs < GPMC_MAX_CS) {
Dirk Behme778933f2008-12-14 09:47:16 +01001107 /* Check if NAND type is set */
Dirk Behmea4becd62009-08-08 09:30:22 +02001108 if ((readl(&gpmc_cfg->cs[cs].config1) & 0xC00) == 0x800) {
Dirk Behme778933f2008-12-14 09:47:16 +01001109 /* Found it!! */
1110 break;
1111 }
1112 cs++;
1113 }
1114 if (cs >= GPMC_MAX_CS) {
pekon gupta6bd91a82013-11-18 19:03:00 +05301115 printf("nand: error: Unable to find NAND settings in "
Dirk Behme778933f2008-12-14 09:47:16 +01001116 "GPMC Configuration - quitting\n");
1117 return -ENODEV;
1118 }
1119
Dirk Behmea4becd62009-08-08 09:30:22 +02001120 gpmc_config = readl(&gpmc_cfg->config);
Dirk Behme778933f2008-12-14 09:47:16 +01001121 /* Disable Write protect */
1122 gpmc_config |= 0x10;
Dirk Behmea4becd62009-08-08 09:30:22 +02001123 writel(gpmc_config, &gpmc_cfg->config);
Dirk Behme778933f2008-12-14 09:47:16 +01001124
Dirk Behmea4becd62009-08-08 09:30:22 +02001125 nand->IO_ADDR_R = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
1126 nand->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
Roger Quadros3b7aaa52022-10-11 14:50:02 +03001127
1128 info = &omap_nand_info[cs];
1129 info->control = NULL;
1130 info->cs = cs;
1131 info->ws = wscfg[cs];
Roger Quadrose52ec9e2024-01-11 15:19:18 +02001132 info->fifo = nand_base;
Scott Wood17fed142016-05-30 13:57:56 -05001133 nand_set_controller_data(nand, &omap_nand_info[cs]);
pekon gupta6bd91a82013-11-18 19:03:00 +05301134 nand->cmd_ctrl = omap_nand_hwcontrol;
1135 nand->options |= NAND_NO_PADDING | NAND_CACHEPRG;
Dirk Behme778933f2008-12-14 09:47:16 +01001136 nand->chip_delay = 100;
Roger Quadros224add22022-10-11 14:50:06 +03001137 nand->ecc.layout = kzalloc(sizeof(*nand->ecc.layout), GFP_KERNEL);
1138 if (!nand->ecc.layout)
1139 return -ENOMEM;
Mansoor Ahamede5612512012-11-06 13:06:33 +00001140
pekon gupta6250faf2014-05-06 00:46:19 +05301141 /* configure driver and controller based on NAND device bus-width */
1142 gpmc_config = readl(&gpmc_cfg->cs[cs].config1);
1143#if defined(CONFIG_SYS_NAND_BUSWIDTH_16BIT)
1144 nand->options |= NAND_BUSWIDTH_16;
1145 writel(gpmc_config | (0x1 << 12), &gpmc_cfg->cs[cs].config1);
1146#else
1147 nand->options &= ~NAND_BUSWIDTH_16;
1148 writel(gpmc_config & ~(0x1 << 12), &gpmc_cfg->cs[cs].config1);
1149#endif
pekon gupta6bd91a82013-11-18 19:03:00 +05301150 /* select ECC scheme */
pekon gupta3ef49732013-11-18 19:03:01 +05301151#if defined(CONFIG_NAND_OMAP_ECCSCHEME)
1152 err = omap_select_ecc_scheme(nand, CONFIG_NAND_OMAP_ECCSCHEME,
pekon gupta6bd91a82013-11-18 19:03:00 +05301153 CONFIG_SYS_NAND_PAGE_SIZE, CONFIG_SYS_NAND_OOBSIZE);
pekon gupta3ef49732013-11-18 19:03:01 +05301154#else
1155 /* pagesize and oobsize are not required to configure sw ecc-scheme */
pekon gupta6bd91a82013-11-18 19:03:00 +05301156 err = omap_select_ecc_scheme(nand, OMAP_ECC_HAM1_CODE_SW,
1157 0, 0);
Mansoor Ahamede5612512012-11-06 13:06:33 +00001158#endif
pekon gupta6bd91a82013-11-18 19:03:00 +05301159 if (err)
1160 return err;
Simon Schwarz4f62e982011-09-14 15:30:16 -04001161
Egli, Samuel8645fd92015-02-13 15:47:10 +01001162#ifdef CONFIG_NAND_OMAP_GPMC_PREFETCH
Jeroen Hofstee5e67ac72015-05-30 10:11:24 +02001163 nand->read_buf = omap_nand_read_prefetch;
Egli, Samuel8645fd92015-02-13 15:47:10 +01001164#else
Roger Quadros3b7aaa52022-10-11 14:50:02 +03001165 nand->read_buf = omap_nand_read_buf;
Simon Schwarz4f62e982011-09-14 15:30:16 -04001166#endif
Stefan Roesee05972f2014-11-13 03:43:39 +01001167
1168 nand->dev_ready = omap_dev_ready;
1169
Dirk Behme778933f2008-12-14 09:47:16 +01001170 return 0;
1171}
Roger Quadros80cf6372022-12-20 12:21:59 +02001172
1173/* First NAND chip for SPL use only */
1174static __maybe_unused struct nand_chip *nand_chip;
1175
1176#if CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)
1177
1178static int gpmc_nand_probe(struct udevice *dev)
1179{
1180 struct nand_chip *nand = dev_get_priv(dev);
1181 struct mtd_info *mtd = nand_to_mtd(nand);
Roger Quadrose52ec9e2024-01-11 15:19:18 +02001182 struct resource res;
1183 void __iomem *base;
Roger Quadros80cf6372022-12-20 12:21:59 +02001184 int ret;
1185
Roger Quadrose52ec9e2024-01-11 15:19:18 +02001186 ret = dev_read_resource(dev, 0, &res);
1187 if (ret)
1188 return ret;
1189
1190 base = devm_ioremap(dev, res.start, resource_size(&res));
Vignesh Raghavendra9896dd62024-07-31 20:28:55 +05301191 ret = gpmc_nand_init(nand, base);
1192 if (ret)
1193 return ret;
1194
Roger Quadros2b25eac2024-01-11 15:19:19 +02001195 mtd->dev = dev;
1196 nand_set_flash_node(nand, dev_ofnode(dev));
Roger Quadros80cf6372022-12-20 12:21:59 +02001197
1198 ret = nand_scan(mtd, CONFIG_SYS_NAND_MAX_CHIPS);
1199 if (ret)
1200 return ret;
1201
1202 ret = nand_register(0, mtd);
1203 if (ret)
1204 return ret;
1205
1206 if (!nand_chip)
1207 nand_chip = nand;
1208
1209 return 0;
1210}
1211
1212static const struct udevice_id gpmc_nand_ids[] = {
1213 { .compatible = "ti,am64-nand" },
1214 { .compatible = "ti,omap2-nand" },
1215 { }
1216};
1217
1218U_BOOT_DRIVER(gpmc_nand) = {
1219 .name = "gpmc-nand",
1220 .id = UCLASS_MTD,
1221 .of_match = gpmc_nand_ids,
1222 .probe = gpmc_nand_probe,
1223 .priv_auto = sizeof(struct nand_chip),
1224};
1225
1226void board_nand_init(void)
1227{
1228 struct udevice *dev;
1229 int ret;
1230
Roger Quadrosa42b7702022-12-20 12:22:03 +02001231#ifdef CONFIG_NAND_OMAP_ELM
1232 ret = uclass_get_device_by_driver(UCLASS_MTD,
1233 DM_DRIVER_GET(gpmc_elm), &dev);
1234 if (ret && ret != -ENODEV) {
1235 pr_err("%s: Failed to get ELM device: %d\n", __func__, ret);
1236 return;
1237 }
1238#endif
1239
Roger Quadros80cf6372022-12-20 12:21:59 +02001240 ret = uclass_get_device_by_driver(UCLASS_MTD,
1241 DM_DRIVER_GET(gpmc_nand), &dev);
1242 if (ret && ret != -ENODEV)
1243 pr_err("%s: Failed to get GPMC device: %d\n", __func__, ret);
1244}
1245
1246#else
1247
1248int board_nand_init(struct nand_chip *nand)
1249{
Roger Quadrose52ec9e2024-01-11 15:19:18 +02001250 return gpmc_nand_init(nand, (void __iomem *)CFG_SYS_NAND_BASE);
Roger Quadros80cf6372022-12-20 12:21:59 +02001251}
1252
1253#endif /* CONFIG_SYS_NAND_SELF_INIT */
Roger Quadros685c4282022-12-20 12:22:00 +02001254
1255#if defined(CONFIG_SPL_NAND_INIT)
1256
1257/* nand_init() is provided by nand.c */
1258
1259/* Unselect after operation */
1260void nand_deselect(void)
1261{
1262 struct mtd_info *mtd = nand_to_mtd(nand_chip);
1263
1264 if (nand_chip->select_chip)
1265 nand_chip->select_chip(mtd, -1);
1266}
1267
1268static int nand_is_bad_block(int block)
1269{
1270 struct mtd_info *mtd = nand_to_mtd(nand_chip);
1271
1272 loff_t ofs = block * CONFIG_SYS_NAND_BLOCK_SIZE;
1273
1274 return nand_chip->block_bad(mtd, ofs);
1275}
1276
1277static int nand_read_page(int block, int page, uchar *dst)
1278{
Sean Anderson11a4c702023-11-04 16:37:41 -04001279 int page_addr = block * SYS_NAND_BLOCK_PAGES + page;
Roger Quadros685c4282022-12-20 12:22:00 +02001280 loff_t ofs = page_addr * CONFIG_SYS_NAND_PAGE_SIZE;
1281 int ret;
1282 size_t len = CONFIG_SYS_NAND_PAGE_SIZE;
1283 struct mtd_info *mtd = nand_to_mtd(nand_chip);
1284
1285 ret = nand_read(mtd, ofs, &len, dst);
1286 if (ret)
1287 printf("nand_read failed %d\n", ret);
1288
1289 return ret;
1290}
1291
1292#include "nand_spl_loaders.c"
1293#endif /* CONFIG_SPL_NAND_INIT */