blob: 1a5ed0de31a9c1866943f89553fd00a5128149f4 [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
7#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Dirk Behme778933f2008-12-14 09:47:16 +01009#include <asm/io.h>
Roger Quadros80cf6372022-12-20 12:21:59 +020010#include <dm/uclass.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090011#include <linux/errno.h>
Roger Quadros6550c622022-10-11 14:49:59 +030012
13#ifdef CONFIG_ARCH_OMAP2PLUS
Dirk Behme778933f2008-12-14 09:47:16 +010014#include <asm/arch/mem.h>
Roger Quadros6550c622022-10-11 14:49:59 +030015#endif
16
pekon gupta5bbb0992013-11-22 16:53:29 +053017#include <linux/mtd/omap_gpmc.h>
Dirk Behme778933f2008-12-14 09:47:16 +010018#include <linux/mtd/nand_ecc.h>
Tom Rini3bde7e22021-09-22 14:50:35 -040019#include <linux/mtd/rawnand.h>
Andreas Bießmann82a65472013-04-05 04:55:21 +000020#include <linux/bch.h>
Stefano Babicaade5792012-03-21 23:56:17 +000021#include <linux/compiler.h>
Dirk Behme778933f2008-12-14 09:47:16 +010022#include <nand.h>
Roger Quadrosa42b7702022-12-20 12:22:03 +020023
24#include "omap_elm.h"
pekon gupta6bd91a82013-11-18 19:03:00 +053025
Roger Quadros6550c622022-10-11 14:49:59 +030026#ifndef GPMC_MAX_CS
27#define GPMC_MAX_CS 4
28#endif
29
pekon gupta6bd91a82013-11-18 19:03:00 +053030#define BADBLOCK_MARKER_LENGTH 2
31#define SECTOR_BYTES 512
Roger Quadrosaa418fb2022-12-20 12:21:56 +020032#define ECCSIZE0_SHIFT 12
33#define ECCSIZE1_SHIFT 22
34#define ECC1RESULTSIZE 0x1
pekon guptaeff10ee2013-11-19 11:02:15 +053035#define ECCCLEAR (0x1 << 8)
36#define ECCRESULTREG1 (0x1 << 0)
pekon guptab0f750a2013-11-19 11:02:17 +053037/* 4 bit padding to make byte aligned, 56 = 52 + 4 */
38#define BCH4_BIT_PAD 4
39
pekon gupta03742c92013-11-19 11:02:16 +053040#ifdef CONFIG_BCH
41static u8 bch8_polynomial[] = {0xef, 0x51, 0x2e, 0x09, 0xed, 0x93, 0x9a, 0xc2,
42 0x97, 0x79, 0xe5, 0x24, 0xb5};
43#endif
Rostislav Lisovy77cdf8a2014-09-02 16:23:58 +020044static uint8_t cs_next;
Dirk Behme778933f2008-12-14 09:47:16 +010045
Michal Sojka26160072015-02-17 17:08:37 +010046#if defined(CONFIG_NAND_OMAP_GPMC_WSCFG)
47static const int8_t wscfg[CONFIG_SYS_MAX_NAND_DEVICE] =
48 { CONFIG_NAND_OMAP_GPMC_WSCFG };
49#else
50/* wscfg is preset to zero since its a static variable */
51static const int8_t wscfg[CONFIG_SYS_MAX_NAND_DEVICE];
52#endif
53
Dirk Behme778933f2008-12-14 09:47:16 +010054/*
Rostislav Lisovy77cdf8a2014-09-02 16:23:58 +020055 * Driver configurations
56 */
57struct omap_nand_info {
58 struct bch_control *control;
59 enum omap_ecc ecc_scheme;
Michal Sojka26160072015-02-17 17:08:37 +010060 uint8_t cs;
61 uint8_t ws; /* wait status pin (0,1) */
Roger Quadros3b7aaa52022-10-11 14:50:02 +030062 void __iomem *fifo;
Rostislav Lisovy77cdf8a2014-09-02 16:23:58 +020063};
64
65/* We are wasting a bit of memory but al least we are safe */
66static struct omap_nand_info omap_nand_info[GPMC_MAX_CS];
67
68/*
Dirk Behme778933f2008-12-14 09:47:16 +010069 * omap_nand_hwcontrol - Set the address pointers corretly for the
70 * following address/data/command operation
71 */
72static void omap_nand_hwcontrol(struct mtd_info *mtd, int32_t cmd,
73 uint32_t ctrl)
74{
Scott Wood17fed142016-05-30 13:57:56 -050075 register struct nand_chip *this = mtd_to_nand(mtd);
76 struct omap_nand_info *info = nand_get_controller_data(this);
Rostislav Lisovy77cdf8a2014-09-02 16:23:58 +020077 int cs = info->cs;
Dirk Behme778933f2008-12-14 09:47:16 +010078
79 /*
80 * Point the IO_ADDR to DATA and ADDRESS registers instead
81 * of chip address
82 */
83 switch (ctrl) {
84 case NAND_CTRL_CHANGE | NAND_CTRL_CLE:
Dirk Behmea4becd62009-08-08 09:30:22 +020085 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
Dirk Behme778933f2008-12-14 09:47:16 +010086 break;
87 case NAND_CTRL_CHANGE | NAND_CTRL_ALE:
Dirk Behmea4becd62009-08-08 09:30:22 +020088 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_adr;
Dirk Behme778933f2008-12-14 09:47:16 +010089 break;
90 case NAND_CTRL_CHANGE | NAND_NCE:
Dirk Behmea4becd62009-08-08 09:30:22 +020091 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
Dirk Behme778933f2008-12-14 09:47:16 +010092 break;
93 }
94
95 if (cmd != NAND_CMD_NONE)
96 writeb(cmd, this->IO_ADDR_W);
97}
98
Simon Schwarz4f62e982011-09-14 15:30:16 -040099/* Check wait pin as dev ready indicator */
Stefan Roesee05972f2014-11-13 03:43:39 +0100100static int omap_dev_ready(struct mtd_info *mtd)
Simon Schwarz4f62e982011-09-14 15:30:16 -0400101{
Scott Wood17fed142016-05-30 13:57:56 -0500102 register struct nand_chip *this = mtd_to_nand(mtd);
103 struct omap_nand_info *info = nand_get_controller_data(this);
Michal Sojka26160072015-02-17 17:08:37 +0100104 return gpmc_cfg->status & (1 << (8 + info->ws));
Simon Schwarz4f62e982011-09-14 15:30:16 -0400105}
Dirk Behme778933f2008-12-14 09:47:16 +0100106
107/*
108 * gen_true_ecc - This function will generate true ECC value, which
109 * can be used when correcting data read from NAND flash memory core
110 *
111 * @ecc_buf: buffer to store ecc code
112 *
113 * @return: re-formatted ECC value
114 */
115static uint32_t gen_true_ecc(uint8_t *ecc_buf)
116{
117 return ecc_buf[0] | (ecc_buf[1] << 16) | ((ecc_buf[2] & 0xF0) << 20) |
118 ((ecc_buf[2] & 0x0F) << 8);
119}
120
121/*
122 * omap_correct_data - Compares the ecc read from nand spare area with ECC
Vagrant Cascadianedfdb992016-04-30 19:18:00 -0700123 * registers values and corrects one bit error if it has occurred
Dirk Behme778933f2008-12-14 09:47:16 +0100124 * Further details can be had from OMAP TRM and the following selected links:
125 * http://en.wikipedia.org/wiki/Hamming_code
126 * http://www.cs.utexas.edu/users/plaxton/c/337/05f/slides/ErrorCorrection-4.pdf
127 *
128 * @mtd: MTD device structure
129 * @dat: page data
130 * @read_ecc: ecc read from nand flash
131 * @calc_ecc: ecc read from ECC registers
132 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100133 * Return: 0 if data is OK or corrected, else returns -1
Dirk Behme778933f2008-12-14 09:47:16 +0100134 */
Stefano Babicaade5792012-03-21 23:56:17 +0000135static int __maybe_unused omap_correct_data(struct mtd_info *mtd, uint8_t *dat,
Dirk Behme778933f2008-12-14 09:47:16 +0100136 uint8_t *read_ecc, uint8_t *calc_ecc)
137{
138 uint32_t orig_ecc, new_ecc, res, hm;
139 uint16_t parity_bits, byte;
140 uint8_t bit;
141
142 /* Regenerate the orginal ECC */
143 orig_ecc = gen_true_ecc(read_ecc);
144 new_ecc = gen_true_ecc(calc_ecc);
145 /* Get the XOR of real ecc */
146 res = orig_ecc ^ new_ecc;
147 if (res) {
148 /* Get the hamming width */
149 hm = hweight32(res);
150 /* Single bit errors can be corrected! */
151 if (hm == 12) {
152 /* Correctable data! */
153 parity_bits = res >> 16;
154 bit = (parity_bits & 0x7);
155 byte = (parity_bits >> 3) & 0x1FF;
156 /* Flip the bit to correct */
157 dat[byte] ^= (0x1 << bit);
158 } else if (hm == 1) {
159 printf("Error: Ecc is wrong\n");
160 /* ECC itself is corrupted */
161 return 2;
162 } else {
163 /*
164 * hm distance != parity pairs OR one, could mean 2 bit
165 * error OR potentially be on a blank page..
166 * orig_ecc: contains spare area data from nand flash.
167 * new_ecc: generated ecc while reading data area.
168 * Note: if the ecc = 0, all data bits from which it was
169 * generated are 0xFF.
170 * The 3 byte(24 bits) ecc is generated per 512byte
171 * chunk of a page. If orig_ecc(from spare area)
172 * is 0xFF && new_ecc(computed now from data area)=0x0,
173 * this means that data area is 0xFF and spare area is
174 * 0xFF. A sure sign of a erased page!
175 */
176 if ((orig_ecc == 0x0FFF0FFF) && (new_ecc == 0x00000000))
177 return 0;
178 printf("Error: Bad compare! failed\n");
179 /* detected 2 bit error */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500180 return -EBADMSG;
Dirk Behme778933f2008-12-14 09:47:16 +0100181 }
182 }
183 return 0;
184}
185
186/*
pekon guptaeff10ee2013-11-19 11:02:15 +0530187 * omap_enable_hwecc - configures GPMC as per ECC scheme before read/write
Andreas Bießmann82a65472013-04-05 04:55:21 +0000188 * @mtd: MTD device structure
189 * @mode: Read/Write mode
190 */
191__maybe_unused
pekon guptaeff10ee2013-11-19 11:02:15 +0530192static void omap_enable_hwecc(struct mtd_info *mtd, int32_t mode)
Andreas Bießmann82a65472013-04-05 04:55:21 +0000193{
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200194 struct nand_chip *nand = mtd_to_nand(mtd);
195 struct omap_nand_info *info = nand_get_controller_data(nand);
pekon guptaeff10ee2013-11-19 11:02:15 +0530196 unsigned int dev_width = (nand->options & NAND_BUSWIDTH_16) ? 1 : 0;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200197 u32 val;
Andreas Bießmann82a65472013-04-05 04:55:21 +0000198
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200199 /* Clear ecc and enable bits */
200 writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
201
202 /* program ecc and result sizes */
203 val = ((((nand->ecc.size >> 1) - 1) << ECCSIZE1_SHIFT) |
204 ECC1RESULTSIZE);
205 writel(val, &gpmc_cfg->ecc_size_config);
206
207 switch (mode) {
208 case NAND_ECC_READ:
209 case NAND_ECC_WRITE:
210 writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
pekon guptaeff10ee2013-11-19 11:02:15 +0530211 break;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200212 case NAND_ECC_READSYN:
213 writel(ECCCLEAR, &gpmc_cfg->ecc_control);
pekon gupta046cf862014-06-02 17:14:42 +0530214 break;
pekon guptaeff10ee2013-11-19 11:02:15 +0530215 default:
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200216 printf("%s: error: unrecognized Mode[%d]!\n", __func__, mode);
217 break;
pekon gupta6bd91a82013-11-18 19:03:00 +0530218 }
Andreas Bießmann82a65472013-04-05 04:55:21 +0000219
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200220 /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
221 val = (dev_width << 7) | (info->cs << 1) | (0x1);
222 writel(val, &gpmc_cfg->ecc_config);
Andreas Bießmann82a65472013-04-05 04:55:21 +0000223}
224
225/*
pekon gupta03742c92013-11-19 11:02:16 +0530226 * omap_calculate_ecc - Read ECC result
227 * @mtd: MTD structure
228 * @dat: unused
229 * @ecc_code: ecc_code buffer
230 * Using noninverted ECC can be considered ugly since writing a blank
231 * page ie. padding will clear the ECC bytes. This is no problem as
232 * long nobody is trying to write data on the seemingly unused page.
233 * Reading an erased page will produce an ECC mismatch between
234 * generated and read ECC bytes that has to be dealt with separately.
235 * E.g. if page is 0xFF (fresh erased), and if HW ECC engine within GPMC
236 * is used, the result of read will be 0x0 while the ECC offsets of the
237 * spare area will be 0xFF which will result in an ECC mismatch.
Mansoor Ahamede5612512012-11-06 13:06:33 +0000238 */
pekon gupta03742c92013-11-19 11:02:16 +0530239static int omap_calculate_ecc(struct mtd_info *mtd, const uint8_t *dat,
Mansoor Ahamede5612512012-11-06 13:06:33 +0000240 uint8_t *ecc_code)
241{
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200242 u32 val;
243
244 val = readl(&gpmc_cfg->ecc1_result);
245 ecc_code[0] = val & 0xFF;
246 ecc_code[1] = (val >> 16) & 0xFF;
247 ecc_code[2] = ((val >> 8) & 0x0F) | ((val >> 20) & 0xF0);
248
249 return 0;
250}
251
252/* GPMC ecc engine settings for read */
253#define BCH_WRAPMODE_1 1 /* BCH wrap mode 1 */
254#define BCH8R_ECC_SIZE0 0x1a /* ecc_size0 = 26 */
255#define BCH8R_ECC_SIZE1 0x2 /* ecc_size1 = 2 */
256#define BCH4R_ECC_SIZE0 0xd /* ecc_size0 = 13 */
257#define BCH4R_ECC_SIZE1 0x3 /* ecc_size1 = 3 */
258
259/* GPMC ecc engine settings for write */
260#define BCH_WRAPMODE_6 6 /* BCH wrap mode 6 */
261#define BCH_ECC_SIZE0 0x0 /* ecc_size0 = 0, no oob protection */
262#define BCH_ECC_SIZE1 0x20 /* ecc_size1 = 32 */
263
264/**
265 * omap_enable_hwecc_bch - Program GPMC to perform BCH ECC calculation
266 * @mtd: MTD device structure
267 * @mode: Read/Write mode
268 *
269 * When using BCH with SW correction (i.e. no ELM), sector size is set
270 * to 512 bytes and we use BCH_WRAPMODE_6 wrapping mode
271 * for both reading and writing with:
272 * eccsize0 = 0 (no additional protected byte in spare area)
273 * eccsize1 = 32 (skip 32 nibbles = 16 bytes per sector in spare area)
274 */
275static void __maybe_unused omap_enable_hwecc_bch(struct mtd_info *mtd,
276 int mode)
277{
278 unsigned int bch_type;
279 unsigned int dev_width, nsectors;
Scott Wood17fed142016-05-30 13:57:56 -0500280 struct nand_chip *chip = mtd_to_nand(mtd);
281 struct omap_nand_info *info = nand_get_controller_data(chip);
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200282 u32 val, wr_mode;
283 unsigned int ecc_size1, ecc_size0;
284
285 /* GPMC configurations for calculating ECC */
286 switch (info->ecc_scheme) {
287 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
288 bch_type = 1;
289 nsectors = 1;
290 wr_mode = BCH_WRAPMODE_6;
291 ecc_size0 = BCH_ECC_SIZE0;
292 ecc_size1 = BCH_ECC_SIZE1;
293 break;
294 case OMAP_ECC_BCH8_CODE_HW:
295 bch_type = 1;
296 nsectors = chip->ecc.steps;
297 if (mode == NAND_ECC_READ) {
298 wr_mode = BCH_WRAPMODE_1;
299 ecc_size0 = BCH8R_ECC_SIZE0;
300 ecc_size1 = BCH8R_ECC_SIZE1;
301 } else {
302 wr_mode = BCH_WRAPMODE_6;
303 ecc_size0 = BCH_ECC_SIZE0;
304 ecc_size1 = BCH_ECC_SIZE1;
305 }
306 break;
307 case OMAP_ECC_BCH16_CODE_HW:
308 bch_type = 0x2;
309 nsectors = chip->ecc.steps;
310 if (mode == NAND_ECC_READ) {
311 wr_mode = 0x01;
312 ecc_size0 = 52; /* ECC bits in nibbles per sector */
313 ecc_size1 = 0; /* non-ECC bits in nibbles per sector */
314 } else {
315 wr_mode = 0x01;
316 ecc_size0 = 0; /* extra bits in nibbles per sector */
317 ecc_size1 = 52; /* OOB bits in nibbles per sector */
318 }
319 break;
320 default:
321 return;
322 }
323
324 writel(ECCRESULTREG1, &gpmc_cfg->ecc_control);
325
326 /* Configure ecc size for BCH */
327 val = (ecc_size1 << ECCSIZE1_SHIFT) | (ecc_size0 << ECCSIZE0_SHIFT);
328 writel(val, &gpmc_cfg->ecc_size_config);
329
330 dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
331
332 /* BCH configuration */
333 val = ((1 << 16) | /* enable BCH */
334 (bch_type << 12) | /* BCH4/BCH8/BCH16 */
335 (wr_mode << 8) | /* wrap mode */
336 (dev_width << 7) | /* bus width */
337 (((nsectors - 1) & 0x7) << 4) | /* number of sectors */
338 (info->cs << 1) | /* ECC CS */
339 (0x1)); /* enable ECC */
340
341 writel(val, &gpmc_cfg->ecc_config);
342
343 /* Clear ecc and enable bits */
344 writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
345}
346
347/**
348 * _omap_calculate_ecc_bch - Generate BCH ECC bytes for one sector
349 * @mtd: MTD device structure
350 * @dat: The pointer to data on which ecc is computed
351 * @ecc_code: The ecc_code buffer
352 * @sector: The sector number (for a multi sector page)
353 *
354 * Support calculating of BCH4/8/16 ECC vectors for one sector
355 * within a page. Sector number is in @sector.
356 */
357static int _omap_calculate_ecc_bch(struct mtd_info *mtd, const u8 *dat,
358 u8 *ecc_code, int sector)
359{
360 struct nand_chip *chip = mtd_to_nand(mtd);
361 struct omap_nand_info *info = nand_get_controller_data(chip);
Ladislav Michld5b1c272016-07-12 20:28:16 +0200362 const uint32_t *ptr;
363 uint32_t val = 0;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000364 int8_t i = 0, j;
365
pekon guptaaa168482014-04-11 12:55:33 +0530366 switch (info->ecc_scheme) {
pekon gupta03742c92013-11-19 11:02:16 +0530367#ifdef CONFIG_BCH
368 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
369#endif
370 case OMAP_ECC_BCH8_CODE_HW:
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200371 ptr = &gpmc_cfg->bch_result_0_3[sector].bch_result_x[3];
pekon gupta03742c92013-11-19 11:02:16 +0530372 val = readl(ptr);
373 ecc_code[i++] = (val >> 0) & 0xFF;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000374 ptr--;
375 for (j = 0; j < 3; j++) {
pekon gupta03742c92013-11-19 11:02:16 +0530376 val = readl(ptr);
377 ecc_code[i++] = (val >> 24) & 0xFF;
378 ecc_code[i++] = (val >> 16) & 0xFF;
379 ecc_code[i++] = (val >> 8) & 0xFF;
380 ecc_code[i++] = (val >> 0) & 0xFF;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000381 ptr--;
382 }
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200383
pekon gupta03742c92013-11-19 11:02:16 +0530384 break;
pekon gupta046cf862014-06-02 17:14:42 +0530385 case OMAP_ECC_BCH16_CODE_HW:
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200386 val = readl(&gpmc_cfg->bch_result_4_6[sector].bch_result_x[2]);
pekon gupta046cf862014-06-02 17:14:42 +0530387 ecc_code[i++] = (val >> 8) & 0xFF;
388 ecc_code[i++] = (val >> 0) & 0xFF;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200389 val = readl(&gpmc_cfg->bch_result_4_6[sector].bch_result_x[1]);
pekon gupta046cf862014-06-02 17:14:42 +0530390 ecc_code[i++] = (val >> 24) & 0xFF;
391 ecc_code[i++] = (val >> 16) & 0xFF;
392 ecc_code[i++] = (val >> 8) & 0xFF;
393 ecc_code[i++] = (val >> 0) & 0xFF;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200394 val = readl(&gpmc_cfg->bch_result_4_6[sector].bch_result_x[0]);
pekon gupta046cf862014-06-02 17:14:42 +0530395 ecc_code[i++] = (val >> 24) & 0xFF;
396 ecc_code[i++] = (val >> 16) & 0xFF;
397 ecc_code[i++] = (val >> 8) & 0xFF;
398 ecc_code[i++] = (val >> 0) & 0xFF;
399 for (j = 3; j >= 0; j--) {
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200400 val = readl(&gpmc_cfg->bch_result_0_3[sector].bch_result_x[j]
pekon gupta046cf862014-06-02 17:14:42 +0530401 );
402 ecc_code[i++] = (val >> 24) & 0xFF;
403 ecc_code[i++] = (val >> 16) & 0xFF;
404 ecc_code[i++] = (val >> 8) & 0xFF;
405 ecc_code[i++] = (val >> 0) & 0xFF;
406 }
407 break;
pekon gupta03742c92013-11-19 11:02:16 +0530408 default:
409 return -EINVAL;
410 }
411 /* ECC scheme specific syndrome customizations */
pekon guptaaa168482014-04-11 12:55:33 +0530412 switch (info->ecc_scheme) {
pekon gupta03742c92013-11-19 11:02:16 +0530413#ifdef CONFIG_BCH
414 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200415 /* Add constant polynomial to remainder, so that
416 * ECC of blank pages results in 0x0 on reading back
417 */
pekon gupta03742c92013-11-19 11:02:16 +0530418 for (i = 0; i < chip->ecc.bytes; i++)
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200419 ecc_code[i] ^= bch8_polynomial[i];
pekon gupta03742c92013-11-19 11:02:16 +0530420 break;
421#endif
422 case OMAP_ECC_BCH8_CODE_HW:
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200423 /* Set 14th ECC byte as 0x0 for ROM compatibility */
424 ecc_code[chip->ecc.bytes - 1] = 0x0;
pekon gupta03742c92013-11-19 11:02:16 +0530425 break;
pekon gupta046cf862014-06-02 17:14:42 +0530426 case OMAP_ECC_BCH16_CODE_HW:
427 break;
pekon gupta03742c92013-11-19 11:02:16 +0530428 default:
429 return -EINVAL;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000430 }
pekon gupta03742c92013-11-19 11:02:16 +0530431 return 0;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000432}
433
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200434/**
435 * omap_calculate_ecc_bch - ECC generator for 1 sector
436 * @mtd: MTD device structure
437 * @dat: The pointer to data on which ecc is computed
438 * @ecc_code: The ecc_code buffer
439 *
440 * Support calculating of BCH4/8/16 ECC vectors for one sector. This is used
441 * when SW based correction is required as ECC is required for one sector
442 * at a time.
443 */
444static int __maybe_unused omap_calculate_ecc_bch(struct mtd_info *mtd,
445 const u_char *dat, u_char *ecc_calc)
446{
447 return _omap_calculate_ecc_bch(mtd, dat, ecc_calc, 0);
448}
449
Roger Quadros3b7aaa52022-10-11 14:50:02 +0300450static inline void omap_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
451{
452 struct nand_chip *chip = mtd_to_nand(mtd);
453 struct omap_nand_info *info = nand_get_controller_data(chip);
454 u32 alignment = ((uintptr_t)buf | len) & 3;
455
456 if (alignment & 1)
457 readsb(info->fifo, buf, len);
458 else if (alignment & 3)
459 readsw(info->fifo, buf, len >> 1);
460 else
461 readsl(info->fifo, buf, len >> 2);
462}
463
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200464#ifdef CONFIG_NAND_OMAP_GPMC_PREFETCH
465
466#define PREFETCH_CONFIG1_CS_SHIFT 24
467#define PREFETCH_FIFOTHRESHOLD_MAX 0x40
468#define PREFETCH_FIFOTHRESHOLD(val) ((val) << 8)
469#define PREFETCH_STATUS_COUNT(val) (val & 0x00003fff)
470#define PREFETCH_STATUS_FIFO_CNT(val) ((val >> 24) & 0x7F)
471#define ENABLE_PREFETCH (1 << 7)
472
473/**
474 * omap_prefetch_enable - configures and starts prefetch transfer
475 * @fifo_th: fifo threshold to be used for read/ write
476 * @count: number of bytes to be transferred
477 * @is_write: prefetch read(0) or write post(1) mode
478 * @cs: chip select to use
479 */
480static int omap_prefetch_enable(int fifo_th, unsigned int count, int is_write, int cs)
481{
482 uint32_t val;
483
484 if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX)
485 return -EINVAL;
486
487 if (readl(&gpmc_cfg->prefetch_control))
488 return -EBUSY;
489
490 /* Set the amount of bytes to be prefetched */
491 writel(count, &gpmc_cfg->prefetch_config2);
492
493 val = (cs << PREFETCH_CONFIG1_CS_SHIFT) | (is_write & 1) |
494 PREFETCH_FIFOTHRESHOLD(fifo_th) | ENABLE_PREFETCH;
495 writel(val, &gpmc_cfg->prefetch_config1);
496
497 /* Start the prefetch engine */
498 writel(1, &gpmc_cfg->prefetch_control);
499
500 return 0;
501}
502
503/**
504 * omap_prefetch_reset - disables and stops the prefetch engine
505 */
506static void omap_prefetch_reset(void)
507{
508 writel(0, &gpmc_cfg->prefetch_control);
509 writel(0, &gpmc_cfg->prefetch_config1);
510}
511
512static int __read_prefetch_aligned(struct nand_chip *chip, uint32_t *buf, int len)
513{
514 int ret;
515 uint32_t cnt;
Scott Wood17fed142016-05-30 13:57:56 -0500516 struct omap_nand_info *info = nand_get_controller_data(chip);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200517
518 ret = omap_prefetch_enable(PREFETCH_FIFOTHRESHOLD_MAX, len, 0, info->cs);
519 if (ret < 0)
520 return ret;
521
522 do {
523 int i;
524
525 cnt = readl(&gpmc_cfg->prefetch_status);
526 cnt = PREFETCH_STATUS_FIFO_CNT(cnt);
527
528 for (i = 0; i < cnt / 4; i++) {
Roger Quadros3b7aaa52022-10-11 14:50:02 +0300529 *buf++ = readl(info->fifo);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200530 len -= 4;
531 }
532 } while (len);
533
534 omap_prefetch_reset();
535
536 return 0;
537}
538
Jeroen Hofstee5e67ac72015-05-30 10:11:24 +0200539static void omap_nand_read_prefetch(struct mtd_info *mtd, uint8_t *buf, int len)
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200540{
541 int ret;
Roger Quadros3068f0f2022-10-11 14:50:01 +0300542 uintptr_t head, tail;
Scott Wood17fed142016-05-30 13:57:56 -0500543 struct nand_chip *chip = mtd_to_nand(mtd);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200544
545 /*
546 * If the destination buffer is unaligned, start with reading
547 * the overlap byte-wise.
548 */
Roger Quadros3068f0f2022-10-11 14:50:01 +0300549 head = ((uintptr_t)buf) % 4;
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200550 if (head) {
Roger Quadros3b7aaa52022-10-11 14:50:02 +0300551 omap_nand_read_buf(mtd, buf, head);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200552 buf += head;
553 len -= head;
554 }
555
556 /*
557 * Only transfer multiples of 4 bytes in a pre-fetched fashion.
558 * If there's a residue, care for it byte-wise afterwards.
559 */
560 tail = len % 4;
561
Jeroen Hofstee5e67ac72015-05-30 10:11:24 +0200562 ret = __read_prefetch_aligned(chip, (uint32_t *)buf, len - tail);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200563 if (ret < 0) {
564 /* fallback in case the prefetch engine is busy */
Roger Quadros3b7aaa52022-10-11 14:50:02 +0300565 omap_nand_read_buf(mtd, buf, len);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200566 } else if (tail) {
567 buf += len - tail;
Roger Quadros3b7aaa52022-10-11 14:50:02 +0300568 omap_nand_read_buf(mtd, buf, tail);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200569 }
570}
571#endif /* CONFIG_NAND_OMAP_GPMC_PREFETCH */
572
pekon gupta03742c92013-11-19 11:02:16 +0530573#ifdef CONFIG_NAND_OMAP_ELM
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200574
575/**
576 * omap_calculate_ecc_bch_multi - Generate ECC for multiple sectors
577 * @mtd: MTD device structure
578 * @dat: The pointer to data on which ecc is computed
579 * @ecc_code: The ecc_code buffer
580 *
581 * Support calculating of BCH4/8/16 ecc vectors for the entire page in one go.
582 */
583static int omap_calculate_ecc_bch_multi(struct mtd_info *mtd,
584 const u_char *dat, u_char *ecc_calc)
585{
586 struct nand_chip *chip = mtd_to_nand(mtd);
587 int eccbytes = chip->ecc.bytes;
588 unsigned long nsectors;
589 int i, ret;
590
591 nsectors = ((readl(&gpmc_cfg->ecc_config) >> 4) & 0x7) + 1;
592 for (i = 0; i < nsectors; i++) {
593 ret = _omap_calculate_ecc_bch(mtd, dat, ecc_calc, i);
594 if (ret)
595 return ret;
596
597 ecc_calc += eccbytes;
598 }
599
600 return 0;
601}
602
Mansoor Ahamede5612512012-11-06 13:06:33 +0000603/*
Jeroen Hofstee79369072014-10-08 22:57:42 +0200604 * omap_reverse_list - re-orders list elements in reverse order [internal]
605 * @list: pointer to start of list
606 * @length: length of list
607*/
608static void omap_reverse_list(u8 *list, unsigned int length)
609{
610 unsigned int i, j;
611 unsigned int half_length = length / 2;
612 u8 tmp;
613 for (i = 0, j = length - 1; i < half_length; i++, j--) {
614 tmp = list[i];
615 list[i] = list[j];
616 list[j] = tmp;
617 }
618}
619
620/*
Mansoor Ahamede5612512012-11-06 13:06:33 +0000621 * omap_correct_data_bch - Compares the ecc read from nand spare area
Vagrant Cascadianedfdb992016-04-30 19:18:00 -0700622 * with ECC registers values and corrects one bit error if it has occurred
Mansoor Ahamede5612512012-11-06 13:06:33 +0000623 *
624 * @mtd: MTD device structure
625 * @dat: page data
626 * @read_ecc: ecc read from nand flash (ignored)
627 * @calc_ecc: ecc read from ECC registers
628 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100629 * Return: 0 if data is OK or corrected, else returns -1
Mansoor Ahamede5612512012-11-06 13:06:33 +0000630 */
631static int omap_correct_data_bch(struct mtd_info *mtd, uint8_t *dat,
632 uint8_t *read_ecc, uint8_t *calc_ecc)
633{
Scott Wood17fed142016-05-30 13:57:56 -0500634 struct nand_chip *chip = mtd_to_nand(mtd);
635 struct omap_nand_info *info = nand_get_controller_data(chip);
pekon gupta3c43c5b2014-04-11 12:55:34 +0530636 struct nand_ecc_ctrl *ecc = &chip->ecc;
pekon guptab0f750a2013-11-19 11:02:17 +0530637 uint32_t error_count = 0, error_max;
pekon gupta046cf862014-06-02 17:14:42 +0530638 uint32_t error_loc[ELM_MAX_ERROR_COUNT];
pekon gupta9d4b7472014-04-11 12:55:32 +0530639 enum bch_level bch_type;
pekon guptab0f750a2013-11-19 11:02:17 +0530640 uint32_t i, ecc_flag = 0;
Guido Martínez20b27be2015-01-02 14:49:10 -0300641 uint8_t count;
pekon guptab0f750a2013-11-19 11:02:17 +0530642 uint32_t byte_pos, bit_pos;
Guido Martínez20b27be2015-01-02 14:49:10 -0300643 int err = 0;
pekon guptab0f750a2013-11-19 11:02:17 +0530644
645 /* check calculated ecc */
pekon gupta3c43c5b2014-04-11 12:55:34 +0530646 for (i = 0; i < ecc->bytes && !ecc_flag; i++) {
pekon guptab0f750a2013-11-19 11:02:17 +0530647 if (calc_ecc[i] != 0x00)
David Rivshinefa78532021-11-18 13:25:24 -0500648 goto not_ecc_match;
pekon guptab0f750a2013-11-19 11:02:17 +0530649 }
David Rivshinefa78532021-11-18 13:25:24 -0500650 return 0;
651not_ecc_match:
Mansoor Ahamede5612512012-11-06 13:06:33 +0000652
David Rivshinefa78532021-11-18 13:25:24 -0500653 /* check for whether it's an erased-page */
654 for (i = 0; i < ecc->bytes; i++) {
Mansoor Ahamede5612512012-11-06 13:06:33 +0000655 if (read_ecc[i] != 0xff)
David Rivshinefa78532021-11-18 13:25:24 -0500656 goto not_erased;
pekon guptab0f750a2013-11-19 11:02:17 +0530657 }
David Rivshinefa78532021-11-18 13:25:24 -0500658 for (i = 0; i < SECTOR_BYTES; i++) {
659 if (dat[i] != 0xff)
660 goto not_erased;
661 }
662 return 0;
663not_erased:
664
665 /*
666 * Check for whether it's an erased page with a correctable
667 * number of bitflips. Erased pages have all 1's in the data,
668 * so we just compute the number of 0 bits in the data and
669 * see if it's under the correction threshold.
670 *
671 * NOTE: The check for a perfect erased page above is faster for
672 * the more common case, even though it's logically redundant.
673 */
674 for (i = 0; i < ecc->bytes; i++)
675 error_count += hweight8(~read_ecc[i]);
676
677 for (i = 0; i < SECTOR_BYTES; i++)
678 error_count += hweight8(~dat[i]);
679
680 if (error_count <= ecc->strength) {
681 memset(read_ecc, 0xFF, ecc->bytes);
682 memset(dat, 0xFF, SECTOR_BYTES);
683 debug("nand: %u bit-flip(s) corrected in erased page\n",
684 error_count);
685 return error_count;
686 }
Mansoor Ahamede5612512012-11-06 13:06:33 +0000687
Mansoor Ahamede5612512012-11-06 13:06:33 +0000688 /*
689 * while reading ECC result we read it in big endian.
690 * Hence while loading to ELM we have rotate to get the right endian.
691 */
pekon guptaaa168482014-04-11 12:55:33 +0530692 switch (info->ecc_scheme) {
pekon guptab0f750a2013-11-19 11:02:17 +0530693 case OMAP_ECC_BCH8_CODE_HW:
pekon gupta9d4b7472014-04-11 12:55:32 +0530694 bch_type = BCH_8_BIT;
pekon gupta3c43c5b2014-04-11 12:55:34 +0530695 omap_reverse_list(calc_ecc, ecc->bytes - 1);
pekon guptab0f750a2013-11-19 11:02:17 +0530696 break;
pekon gupta046cf862014-06-02 17:14:42 +0530697 case OMAP_ECC_BCH16_CODE_HW:
698 bch_type = BCH_16_BIT;
699 omap_reverse_list(calc_ecc, ecc->bytes);
700 break;
pekon guptab0f750a2013-11-19 11:02:17 +0530701 default:
702 return -EINVAL;
703 }
Mansoor Ahamede5612512012-11-06 13:06:33 +0000704 /* use elm module to check for errors */
pekon gupta9d4b7472014-04-11 12:55:32 +0530705 elm_config(bch_type);
David Rivshinefa78532021-11-18 13:25:24 -0500706 error_count = 0;
pekon guptacfe6b8a2014-04-11 12:55:35 +0530707 err = elm_check_error(calc_ecc, bch_type, &error_count, error_loc);
708 if (err)
709 return err;
710
Mansoor Ahamede5612512012-11-06 13:06:33 +0000711 /* correct bch error */
pekon guptab0f750a2013-11-19 11:02:17 +0530712 for (count = 0; count < error_count; count++) {
pekon guptaaa168482014-04-11 12:55:33 +0530713 switch (info->ecc_scheme) {
pekon gupta9d4b7472014-04-11 12:55:32 +0530714 case OMAP_ECC_BCH8_CODE_HW:
pekon guptab0f750a2013-11-19 11:02:17 +0530715 /* 14th byte in ECC is reserved to match ROM layout */
pekon gupta3c43c5b2014-04-11 12:55:34 +0530716 error_max = SECTOR_BYTES + (ecc->bytes - 1);
pekon guptab0f750a2013-11-19 11:02:17 +0530717 break;
pekon gupta046cf862014-06-02 17:14:42 +0530718 case OMAP_ECC_BCH16_CODE_HW:
719 error_max = SECTOR_BYTES + ecc->bytes;
720 break;
pekon guptab0f750a2013-11-19 11:02:17 +0530721 default:
722 return -EINVAL;
723 }
724 byte_pos = error_max - (error_loc[count] / 8) - 1;
725 bit_pos = error_loc[count] % 8;
726 if (byte_pos < SECTOR_BYTES) {
727 dat[byte_pos] ^= 1 << bit_pos;
Ezequiel García69cf8ad2015-10-04 18:34:42 -0300728 debug("nand: bit-flip corrected @data=%d\n", byte_pos);
pekon guptab0f750a2013-11-19 11:02:17 +0530729 } else if (byte_pos < error_max) {
Belisko Marek9ab54142014-04-25 12:00:07 +0200730 read_ecc[byte_pos - SECTOR_BYTES] ^= 1 << bit_pos;
Ezequiel García69cf8ad2015-10-04 18:34:42 -0300731 debug("nand: bit-flip corrected @oob=%d\n", byte_pos -
pekon guptab0f750a2013-11-19 11:02:17 +0530732 SECTOR_BYTES);
733 } else {
734 err = -EBADMSG;
735 printf("nand: error: invalid bit-flip location\n");
736 }
737 }
738 return (err) ? err : error_count;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000739}
Mansoor Ahamede5612512012-11-06 13:06:33 +0000740
741/**
742 * omap_read_page_bch - hardware ecc based page read function
743 * @mtd: mtd info structure
744 * @chip: nand chip info structure
745 * @buf: buffer to store read data
Sergey Lapin3a38a552013-01-14 03:46:50 +0000746 * @oob_required: caller expects OOB data read to chip->oob_poi
Mansoor Ahamede5612512012-11-06 13:06:33 +0000747 * @page: page number to read
748 *
749 */
750static int omap_read_page_bch(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000751 uint8_t *buf, int oob_required, int page)
Mansoor Ahamede5612512012-11-06 13:06:33 +0000752{
753 int i, eccsize = chip->ecc.size;
754 int eccbytes = chip->ecc.bytes;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200755 int ecctotal = chip->ecc.total;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000756 int eccsteps = chip->ecc.steps;
757 uint8_t *p = buf;
758 uint8_t *ecc_calc = chip->buffers->ecccalc;
759 uint8_t *ecc_code = chip->buffers->ecccode;
760 uint32_t *eccpos = chip->ecc.layout->eccpos;
761 uint8_t *oob = chip->oob_poi;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000762 uint32_t oob_pos;
763
Mansoor Ahamede5612512012-11-06 13:06:33 +0000764 /* oob area start */
765 oob_pos = (eccsize * eccsteps) + chip->ecc.layout->eccpos[0];
766 oob += chip->ecc.layout->eccpos[0];
767
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200768 /* Enable ECC engine */
769 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Mansoor Ahamede5612512012-11-06 13:06:33 +0000770
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200771 /* read entire page */
772 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, 0, -1);
773 chip->read_buf(mtd, buf, mtd->writesize);
Mansoor Ahamede5612512012-11-06 13:06:33 +0000774
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200775 /* read all ecc bytes from oob area */
776 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_pos, -1);
777 chip->read_buf(mtd, oob, ecctotal);
778
779 /* Calculate ecc bytes */
780 omap_calculate_ecc_bch_multi(mtd, buf, ecc_calc);
Mansoor Ahamede5612512012-11-06 13:06:33 +0000781
782 for (i = 0; i < chip->ecc.total; i++)
783 ecc_code[i] = chip->oob_poi[eccpos[i]];
784
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200785 /* error detect & correct */
Mansoor Ahamede5612512012-11-06 13:06:33 +0000786 eccsteps = chip->ecc.steps;
787 p = buf;
788
789 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
790 int stat;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000791 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
792 if (stat < 0)
793 mtd->ecc_stats.failed++;
794 else
795 mtd->ecc_stats.corrected += stat;
796 }
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200797
Mansoor Ahamede5612512012-11-06 13:06:33 +0000798 return 0;
799}
pekon gupta6bd91a82013-11-18 19:03:00 +0530800#endif /* CONFIG_NAND_OMAP_ELM */
Mansoor Ahamede5612512012-11-06 13:06:33 +0000801
Andreas Bießmann82a65472013-04-05 04:55:21 +0000802/*
803 * OMAP3 BCH8 support (with BCH library)
804 */
pekon gupta6bd91a82013-11-18 19:03:00 +0530805#ifdef CONFIG_BCH
Andreas Bießmann82a65472013-04-05 04:55:21 +0000806/**
pekon gupta6bd91a82013-11-18 19:03:00 +0530807 * omap_correct_data_bch_sw - Decode received data and correct errors
Andreas Bießmann82a65472013-04-05 04:55:21 +0000808 * @mtd: MTD device structure
809 * @data: page data
810 * @read_ecc: ecc read from nand flash
811 * @calc_ecc: ecc read from HW ECC registers
812 */
pekon gupta6bd91a82013-11-18 19:03:00 +0530813static int omap_correct_data_bch_sw(struct mtd_info *mtd, u_char *data,
Andreas Bießmann82a65472013-04-05 04:55:21 +0000814 u_char *read_ecc, u_char *calc_ecc)
815{
816 int i, count;
817 /* cannot correct more than 8 errors */
818 unsigned int errloc[8];
Scott Wood17fed142016-05-30 13:57:56 -0500819 struct nand_chip *chip = mtd_to_nand(mtd);
820 struct omap_nand_info *info = nand_get_controller_data(chip);
Andreas Bießmann82a65472013-04-05 04:55:21 +0000821
Ladislav Michl6cc51852017-01-09 11:15:14 +0100822 count = decode_bch(info->control, NULL, SECTOR_BYTES,
823 read_ecc, calc_ecc, NULL, errloc);
Andreas Bießmann82a65472013-04-05 04:55:21 +0000824 if (count > 0) {
825 /* correct errors */
826 for (i = 0; i < count; i++) {
827 /* correct data only, not ecc bytes */
Ladislav Michl6cc51852017-01-09 11:15:14 +0100828 if (errloc[i] < SECTOR_BYTES << 3)
829 data[errloc[i] >> 3] ^= 1 << (errloc[i] & 7);
Ezequiel García69cf8ad2015-10-04 18:34:42 -0300830 debug("corrected bitflip %u\n", errloc[i]);
Andreas Bießmann82a65472013-04-05 04:55:21 +0000831#ifdef DEBUG
832 puts("read_ecc: ");
833 /*
834 * BCH8 have 13 bytes of ECC; BCH4 needs adoption
835 * here!
836 */
837 for (i = 0; i < 13; i++)
838 printf("%02x ", read_ecc[i]);
839 puts("\n");
840 puts("calc_ecc: ");
841 for (i = 0; i < 13; i++)
842 printf("%02x ", calc_ecc[i]);
843 puts("\n");
844#endif
845 }
846 } else if (count < 0) {
847 puts("ecc unrecoverable error\n");
848 }
849 return count;
850}
851
852/**
853 * omap_free_bch - Release BCH ecc resources
854 * @mtd: MTD device structure
855 */
856static void __maybe_unused omap_free_bch(struct mtd_info *mtd)
857{
Scott Wood17fed142016-05-30 13:57:56 -0500858 struct nand_chip *chip = mtd_to_nand(mtd);
859 struct omap_nand_info *info = nand_get_controller_data(chip);
Andreas Bießmann82a65472013-04-05 04:55:21 +0000860
pekon guptaaa168482014-04-11 12:55:33 +0530861 if (info->control) {
862 free_bch(info->control);
863 info->control = NULL;
Andreas Bießmann82a65472013-04-05 04:55:21 +0000864 }
865}
pekon gupta6bd91a82013-11-18 19:03:00 +0530866#endif /* CONFIG_BCH */
867
868/**
869 * omap_select_ecc_scheme - configures driver for particular ecc-scheme
870 * @nand: NAND chip device structure
871 * @ecc_scheme: ecc scheme to configure
872 * @pagesize: number of main-area bytes per page of NAND device
873 * @oobsize: number of OOB/spare bytes per page of NAND device
874 */
875static int omap_select_ecc_scheme(struct nand_chip *nand,
876 enum omap_ecc ecc_scheme, unsigned int pagesize, unsigned int oobsize) {
Scott Wood17fed142016-05-30 13:57:56 -0500877 struct omap_nand_info *info = nand_get_controller_data(nand);
Roger Quadros224add22022-10-11 14:50:06 +0300878 struct nand_ecclayout *ecclayout = nand->ecc.layout;
pekon gupta6bd91a82013-11-18 19:03:00 +0530879 int eccsteps = pagesize / SECTOR_BYTES;
880 int i;
881
882 switch (ecc_scheme) {
883 case OMAP_ECC_HAM1_CODE_SW:
884 debug("nand: selected OMAP_ECC_HAM1_CODE_SW\n");
885 /* For this ecc-scheme, ecc.bytes, ecc.layout, ... are
886 * initialized in nand_scan_tail(), so just set ecc.mode */
pekon guptaaa168482014-04-11 12:55:33 +0530887 info->control = NULL;
pekon gupta6bd91a82013-11-18 19:03:00 +0530888 nand->ecc.mode = NAND_ECC_SOFT;
889 nand->ecc.layout = NULL;
Nikita Kiryanov4110c822013-12-12 15:19:31 +0200890 nand->ecc.size = 0;
pekon gupta6bd91a82013-11-18 19:03:00 +0530891 break;
892
893 case OMAP_ECC_HAM1_CODE_HW:
894 debug("nand: selected OMAP_ECC_HAM1_CODE_HW\n");
895 /* check ecc-scheme requirements before updating ecc info */
896 if ((3 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
897 printf("nand: error: insufficient OOB: require=%d\n", (
898 (3 * eccsteps) + BADBLOCK_MARKER_LENGTH));
899 return -EINVAL;
900 }
pekon guptaaa168482014-04-11 12:55:33 +0530901 info->control = NULL;
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 = 1;
906 nand->ecc.size = SECTOR_BYTES;
907 nand->ecc.bytes = 3;
908 nand->ecc.hwctl = omap_enable_hwecc;
909 nand->ecc.correct = omap_correct_data;
910 nand->ecc.calculate = omap_calculate_ecc;
911 /* define ecc-layout */
912 ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
pekon guptaf0aff022013-12-05 17:54:21 +0530913 for (i = 0; i < ecclayout->eccbytes; i++) {
914 if (nand->options & NAND_BUSWIDTH_16)
915 ecclayout->eccpos[i] = i + 2;
916 else
917 ecclayout->eccpos[i] = i + 1;
918 }
pekon gupta6bd91a82013-11-18 19:03:00 +0530919 ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
920 ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
921 BADBLOCK_MARKER_LENGTH;
pekon gupta6bd91a82013-11-18 19:03:00 +0530922 break;
923
924 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
925#ifdef CONFIG_BCH
926 debug("nand: selected OMAP_ECC_BCH8_CODE_HW_DETECTION_SW\n");
927 /* check ecc-scheme requirements before updating ecc info */
928 if ((13 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
929 printf("nand: error: insufficient OOB: require=%d\n", (
930 (13 * eccsteps) + BADBLOCK_MARKER_LENGTH));
931 return -EINVAL;
932 }
933 /* check if BCH S/W library can be used for error detection */
pekon guptaaa168482014-04-11 12:55:33 +0530934 info->control = init_bch(13, 8, 0x201b);
935 if (!info->control) {
pekon gupta6bd91a82013-11-18 19:03:00 +0530936 printf("nand: error: could not init_bch()\n");
937 return -ENODEV;
938 }
pekon gupta6bd91a82013-11-18 19:03:00 +0530939 /* populate ecc specific fields */
Nikita Kiryanov2e18ff22013-12-17 15:18:01 +0200940 memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
pekon gupta6bd91a82013-11-18 19:03:00 +0530941 nand->ecc.mode = NAND_ECC_HW;
942 nand->ecc.strength = 8;
943 nand->ecc.size = SECTOR_BYTES;
944 nand->ecc.bytes = 13;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200945 nand->ecc.hwctl = omap_enable_hwecc_bch;
pekon gupta6bd91a82013-11-18 19:03:00 +0530946 nand->ecc.correct = omap_correct_data_bch_sw;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200947 nand->ecc.calculate = omap_calculate_ecc_bch;
pekon gupta6bd91a82013-11-18 19:03:00 +0530948 /* define ecc-layout */
949 ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
950 ecclayout->eccpos[0] = BADBLOCK_MARKER_LENGTH;
951 for (i = 1; i < ecclayout->eccbytes; i++) {
952 if (i % nand->ecc.bytes)
953 ecclayout->eccpos[i] =
954 ecclayout->eccpos[i - 1] + 1;
955 else
956 ecclayout->eccpos[i] =
957 ecclayout->eccpos[i - 1] + 2;
958 }
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_BCH required for ECC\n");
965 return -EINVAL;
966#endif
967
968 case OMAP_ECC_BCH8_CODE_HW:
969#ifdef CONFIG_NAND_OMAP_ELM
970 debug("nand: selected OMAP_ECC_BCH8_CODE_HW\n");
971 /* check ecc-scheme requirements before updating ecc info */
972 if ((14 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
973 printf("nand: error: insufficient OOB: require=%d\n", (
974 (14 * eccsteps) + BADBLOCK_MARKER_LENGTH));
975 return -EINVAL;
976 }
977 /* intialize ELM for ECC error detection */
978 elm_init();
pekon guptaaa168482014-04-11 12:55:33 +0530979 info->control = NULL;
pekon gupta6bd91a82013-11-18 19:03:00 +0530980 /* populate ecc specific fields */
Nikita Kiryanov2e18ff22013-12-17 15:18:01 +0200981 memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
pekon gupta6bd91a82013-11-18 19:03:00 +0530982 nand->ecc.mode = NAND_ECC_HW;
983 nand->ecc.strength = 8;
984 nand->ecc.size = SECTOR_BYTES;
985 nand->ecc.bytes = 14;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200986 nand->ecc.hwctl = omap_enable_hwecc_bch;
pekon gupta6bd91a82013-11-18 19:03:00 +0530987 nand->ecc.correct = omap_correct_data_bch;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200988 nand->ecc.calculate = omap_calculate_ecc_bch;
pekon gupta6bd91a82013-11-18 19:03:00 +0530989 nand->ecc.read_page = omap_read_page_bch;
990 /* define ecc-layout */
991 ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
992 for (i = 0; i < ecclayout->eccbytes; i++)
993 ecclayout->eccpos[i] = i + BADBLOCK_MARKER_LENGTH;
994 ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
995 ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
996 BADBLOCK_MARKER_LENGTH;
pekon gupta6bd91a82013-11-18 19:03:00 +0530997 break;
998#else
999 printf("nand: error: CONFIG_NAND_OMAP_ELM required for ECC\n");
1000 return -EINVAL;
1001#endif
1002
pekon gupta046cf862014-06-02 17:14:42 +05301003 case OMAP_ECC_BCH16_CODE_HW:
1004#ifdef CONFIG_NAND_OMAP_ELM
1005 debug("nand: using OMAP_ECC_BCH16_CODE_HW\n");
1006 /* check ecc-scheme requirements before updating ecc info */
1007 if ((26 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
1008 printf("nand: error: insufficient OOB: require=%d\n", (
1009 (26 * eccsteps) + BADBLOCK_MARKER_LENGTH));
1010 return -EINVAL;
1011 }
1012 /* intialize ELM for ECC error detection */
1013 elm_init();
1014 /* populate ecc specific fields */
1015 nand->ecc.mode = NAND_ECC_HW;
1016 nand->ecc.size = SECTOR_BYTES;
1017 nand->ecc.bytes = 26;
1018 nand->ecc.strength = 16;
Roger Quadrosaa418fb2022-12-20 12:21:56 +02001019 nand->ecc.hwctl = omap_enable_hwecc_bch;
pekon gupta046cf862014-06-02 17:14:42 +05301020 nand->ecc.correct = omap_correct_data_bch;
Roger Quadrosaa418fb2022-12-20 12:21:56 +02001021 nand->ecc.calculate = omap_calculate_ecc_bch;
pekon gupta046cf862014-06-02 17:14:42 +05301022 nand->ecc.read_page = omap_read_page_bch;
1023 /* define ecc-layout */
1024 ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
1025 for (i = 0; i < ecclayout->eccbytes; i++)
1026 ecclayout->eccpos[i] = i + BADBLOCK_MARKER_LENGTH;
1027 ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
1028 ecclayout->oobfree[0].length = oobsize - nand->ecc.bytes -
1029 BADBLOCK_MARKER_LENGTH;
1030 break;
1031#else
1032 printf("nand: error: CONFIG_NAND_OMAP_ELM required for ECC\n");
1033 return -EINVAL;
1034#endif
pekon gupta6bd91a82013-11-18 19:03:00 +05301035 default:
1036 debug("nand: error: ecc scheme not enabled or supported\n");
1037 return -EINVAL;
1038 }
Nikita Kiryanove8167892013-12-16 19:19:01 +02001039
1040 /* nand_scan_tail() sets ham1 sw ecc; hw ecc layout is set by driver */
1041 if (ecc_scheme != OMAP_ECC_HAM1_CODE_SW)
1042 nand->ecc.layout = ecclayout;
1043
pekon guptaaa168482014-04-11 12:55:33 +05301044 info->ecc_scheme = ecc_scheme;
pekon gupta6bd91a82013-11-18 19:03:00 +05301045 return 0;
1046}
Andreas Bießmann82a65472013-04-05 04:55:21 +00001047
Simon Schwarz4f62e982011-09-14 15:30:16 -04001048#ifndef CONFIG_SPL_BUILD
Dirk Behme778933f2008-12-14 09:47:16 +01001049/*
Andreas Bießmann1e4eccf2013-04-04 23:52:50 +00001050 * omap_nand_switch_ecc - switch the ECC operation between different engines
1051 * (h/w and s/w) and different algorithms (hamming and BCHx)
Dirk Behme778933f2008-12-14 09:47:16 +01001052 *
Andreas Bießmann1e4eccf2013-04-04 23:52:50 +00001053 * @hardware - true if one of the HW engines should be used
1054 * @eccstrength - the number of bits that could be corrected
1055 * (1 - hamming, 4 - BCH4, 8 - BCH8, 16 - BCH16)
Dirk Behme778933f2008-12-14 09:47:16 +01001056 */
pekon gupta6bd91a82013-11-18 19:03:00 +05301057int __maybe_unused omap_nand_switch_ecc(uint32_t hardware, uint32_t eccstrength)
Dirk Behme778933f2008-12-14 09:47:16 +01001058{
1059 struct nand_chip *nand;
Mugunthan V N7b670cc2017-06-26 19:12:51 -05001060 struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
pekon gupta6bd91a82013-11-18 19:03:00 +05301061 int err = 0;
Dirk Behme778933f2008-12-14 09:47:16 +01001062
Mugunthan V N7b670cc2017-06-26 19:12:51 -05001063 if (!mtd) {
pekon gupta6bd91a82013-11-18 19:03:00 +05301064 printf("nand: error: no NAND devices found\n");
1065 return -ENODEV;
Dirk Behme778933f2008-12-14 09:47:16 +01001066 }
1067
Scott Wood17fed142016-05-30 13:57:56 -05001068 nand = mtd_to_nand(mtd);
Dirk Behme778933f2008-12-14 09:47:16 +01001069 nand->options |= NAND_OWN_BUFFERS;
Jeroen Hofstee96306f22014-01-15 17:58:54 +01001070 nand->options &= ~NAND_SUBPAGE_READ;
Dirk Behme778933f2008-12-14 09:47:16 +01001071 /* Setup the ecc configurations again */
Andreas Bießmann1e4eccf2013-04-04 23:52:50 +00001072 if (hardware) {
1073 if (eccstrength == 1) {
pekon gupta6bd91a82013-11-18 19:03:00 +05301074 err = omap_select_ecc_scheme(nand,
1075 OMAP_ECC_HAM1_CODE_HW,
1076 mtd->writesize, mtd->oobsize);
1077 } else if (eccstrength == 8) {
1078 err = omap_select_ecc_scheme(nand,
1079 OMAP_ECC_BCH8_CODE_HW,
1080 mtd->writesize, mtd->oobsize);
Heiko Schocher5bf904c2016-06-07 08:55:42 +02001081 } else if (eccstrength == 16) {
1082 err = omap_select_ecc_scheme(nand,
1083 OMAP_ECC_BCH16_CODE_HW,
1084 mtd->writesize, mtd->oobsize);
pekon gupta6bd91a82013-11-18 19:03:00 +05301085 } else {
1086 printf("nand: error: unsupported ECC scheme\n");
1087 return -EINVAL;
Andreas Bießmann1e4eccf2013-04-04 23:52:50 +00001088 }
Dirk Behme778933f2008-12-14 09:47:16 +01001089 } else {
Ash Charles4a5faa82015-02-18 11:25:11 -08001090 if (eccstrength == 1) {
1091 err = omap_select_ecc_scheme(nand,
1092 OMAP_ECC_HAM1_CODE_SW,
1093 mtd->writesize, mtd->oobsize);
1094 } else if (eccstrength == 8) {
1095 err = omap_select_ecc_scheme(nand,
1096 OMAP_ECC_BCH8_CODE_HW_DETECTION_SW,
pekon gupta6bd91a82013-11-18 19:03:00 +05301097 mtd->writesize, mtd->oobsize);
Ash Charles4a5faa82015-02-18 11:25:11 -08001098 } else {
1099 printf("nand: error: unsupported ECC scheme\n");
1100 return -EINVAL;
1101 }
Dirk Behme778933f2008-12-14 09:47:16 +01001102 }
1103
1104 /* Update NAND handling after ECC mode switch */
pekon gupta6bd91a82013-11-18 19:03:00 +05301105 if (!err)
1106 err = nand_scan_tail(mtd);
1107 return err;
Dirk Behme778933f2008-12-14 09:47:16 +01001108}
Simon Schwarz4f62e982011-09-14 15:30:16 -04001109#endif /* CONFIG_SPL_BUILD */
Dirk Behme778933f2008-12-14 09:47:16 +01001110
1111/*
1112 * Board-specific NAND initialization. The following members of the
1113 * argument are board-specific:
1114 * - IO_ADDR_R: address to read the 8 I/O lines of the flash device
1115 * - IO_ADDR_W: address to write the 8 I/O lines of the flash device
1116 * - cmd_ctrl: hardwarespecific function for accesing control-lines
1117 * - waitfunc: hardwarespecific function for accesing device ready/busy line
1118 * - ecc.hwctl: function to enable (reset) hardware ecc generator
1119 * - ecc.mode: mode of ecc, see defines
1120 * - chip_delay: chip dependent delay for transfering data from array to
1121 * read regs (tR)
1122 * - options: various chip options. They can partly be set to inform
1123 * nand_scan about special functionality. See the defines for further
1124 * explanation
1125 */
Roger Quadros80cf6372022-12-20 12:21:59 +02001126int gpmc_nand_init(struct nand_chip *nand)
Dirk Behme778933f2008-12-14 09:47:16 +01001127{
1128 int32_t gpmc_config = 0;
Rostislav Lisovy77cdf8a2014-09-02 16:23:58 +02001129 int cs = cs_next++;
pekon gupta6bd91a82013-11-18 19:03:00 +05301130 int err = 0;
Roger Quadros3b7aaa52022-10-11 14:50:02 +03001131 struct omap_nand_info *info;
1132
Dirk Behme778933f2008-12-14 09:47:16 +01001133 /*
1134 * xloader/Uboot's gpmc configuration would have configured GPMC for
1135 * nand type of memory. The following logic scans and latches on to the
1136 * first CS with NAND type memory.
1137 * TBD: need to make this logic generic to handle multiple CS NAND
1138 * devices.
1139 */
1140 while (cs < GPMC_MAX_CS) {
Dirk Behme778933f2008-12-14 09:47:16 +01001141 /* Check if NAND type is set */
Dirk Behmea4becd62009-08-08 09:30:22 +02001142 if ((readl(&gpmc_cfg->cs[cs].config1) & 0xC00) == 0x800) {
Dirk Behme778933f2008-12-14 09:47:16 +01001143 /* Found it!! */
1144 break;
1145 }
1146 cs++;
1147 }
1148 if (cs >= GPMC_MAX_CS) {
pekon gupta6bd91a82013-11-18 19:03:00 +05301149 printf("nand: error: Unable to find NAND settings in "
Dirk Behme778933f2008-12-14 09:47:16 +01001150 "GPMC Configuration - quitting\n");
1151 return -ENODEV;
1152 }
1153
Dirk Behmea4becd62009-08-08 09:30:22 +02001154 gpmc_config = readl(&gpmc_cfg->config);
Dirk Behme778933f2008-12-14 09:47:16 +01001155 /* Disable Write protect */
1156 gpmc_config |= 0x10;
Dirk Behmea4becd62009-08-08 09:30:22 +02001157 writel(gpmc_config, &gpmc_cfg->config);
Dirk Behme778933f2008-12-14 09:47:16 +01001158
Dirk Behmea4becd62009-08-08 09:30:22 +02001159 nand->IO_ADDR_R = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
1160 nand->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
Roger Quadros3b7aaa52022-10-11 14:50:02 +03001161
1162 info = &omap_nand_info[cs];
1163 info->control = NULL;
1164 info->cs = cs;
1165 info->ws = wscfg[cs];
Tom Rinia63bea92022-12-19 09:29:55 -05001166 info->fifo = (void __iomem *)CFG_SYS_NAND_BASE;
Scott Wood17fed142016-05-30 13:57:56 -05001167 nand_set_controller_data(nand, &omap_nand_info[cs]);
pekon gupta6bd91a82013-11-18 19:03:00 +05301168 nand->cmd_ctrl = omap_nand_hwcontrol;
1169 nand->options |= NAND_NO_PADDING | NAND_CACHEPRG;
Dirk Behme778933f2008-12-14 09:47:16 +01001170 nand->chip_delay = 100;
Roger Quadros224add22022-10-11 14:50:06 +03001171 nand->ecc.layout = kzalloc(sizeof(*nand->ecc.layout), GFP_KERNEL);
1172 if (!nand->ecc.layout)
1173 return -ENOMEM;
Mansoor Ahamede5612512012-11-06 13:06:33 +00001174
pekon gupta6250faf2014-05-06 00:46:19 +05301175 /* configure driver and controller based on NAND device bus-width */
1176 gpmc_config = readl(&gpmc_cfg->cs[cs].config1);
1177#if defined(CONFIG_SYS_NAND_BUSWIDTH_16BIT)
1178 nand->options |= NAND_BUSWIDTH_16;
1179 writel(gpmc_config | (0x1 << 12), &gpmc_cfg->cs[cs].config1);
1180#else
1181 nand->options &= ~NAND_BUSWIDTH_16;
1182 writel(gpmc_config & ~(0x1 << 12), &gpmc_cfg->cs[cs].config1);
1183#endif
pekon gupta6bd91a82013-11-18 19:03:00 +05301184 /* select ECC scheme */
pekon gupta3ef49732013-11-18 19:03:01 +05301185#if defined(CONFIG_NAND_OMAP_ECCSCHEME)
1186 err = omap_select_ecc_scheme(nand, CONFIG_NAND_OMAP_ECCSCHEME,
pekon gupta6bd91a82013-11-18 19:03:00 +05301187 CONFIG_SYS_NAND_PAGE_SIZE, CONFIG_SYS_NAND_OOBSIZE);
pekon gupta3ef49732013-11-18 19:03:01 +05301188#else
1189 /* pagesize and oobsize are not required to configure sw ecc-scheme */
pekon gupta6bd91a82013-11-18 19:03:00 +05301190 err = omap_select_ecc_scheme(nand, OMAP_ECC_HAM1_CODE_SW,
1191 0, 0);
Mansoor Ahamede5612512012-11-06 13:06:33 +00001192#endif
pekon gupta6bd91a82013-11-18 19:03:00 +05301193 if (err)
1194 return err;
Simon Schwarz4f62e982011-09-14 15:30:16 -04001195
Egli, Samuel8645fd92015-02-13 15:47:10 +01001196#ifdef CONFIG_NAND_OMAP_GPMC_PREFETCH
Jeroen Hofstee5e67ac72015-05-30 10:11:24 +02001197 nand->read_buf = omap_nand_read_prefetch;
Egli, Samuel8645fd92015-02-13 15:47:10 +01001198#else
Roger Quadros3b7aaa52022-10-11 14:50:02 +03001199 nand->read_buf = omap_nand_read_buf;
Simon Schwarz4f62e982011-09-14 15:30:16 -04001200#endif
Stefan Roesee05972f2014-11-13 03:43:39 +01001201
1202 nand->dev_ready = omap_dev_ready;
1203
Dirk Behme778933f2008-12-14 09:47:16 +01001204 return 0;
1205}
Roger Quadros80cf6372022-12-20 12:21:59 +02001206
1207/* First NAND chip for SPL use only */
1208static __maybe_unused struct nand_chip *nand_chip;
1209
1210#if CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)
1211
1212static int gpmc_nand_probe(struct udevice *dev)
1213{
1214 struct nand_chip *nand = dev_get_priv(dev);
1215 struct mtd_info *mtd = nand_to_mtd(nand);
1216 int ret;
1217
1218 gpmc_nand_init(nand);
1219
1220 ret = nand_scan(mtd, CONFIG_SYS_NAND_MAX_CHIPS);
1221 if (ret)
1222 return ret;
1223
1224 ret = nand_register(0, mtd);
1225 if (ret)
1226 return ret;
1227
1228 if (!nand_chip)
1229 nand_chip = nand;
1230
1231 return 0;
1232}
1233
1234static const struct udevice_id gpmc_nand_ids[] = {
1235 { .compatible = "ti,am64-nand" },
1236 { .compatible = "ti,omap2-nand" },
1237 { }
1238};
1239
1240U_BOOT_DRIVER(gpmc_nand) = {
1241 .name = "gpmc-nand",
1242 .id = UCLASS_MTD,
1243 .of_match = gpmc_nand_ids,
1244 .probe = gpmc_nand_probe,
1245 .priv_auto = sizeof(struct nand_chip),
1246};
1247
1248void board_nand_init(void)
1249{
1250 struct udevice *dev;
1251 int ret;
1252
Roger Quadrosa42b7702022-12-20 12:22:03 +02001253#ifdef CONFIG_NAND_OMAP_ELM
1254 ret = uclass_get_device_by_driver(UCLASS_MTD,
1255 DM_DRIVER_GET(gpmc_elm), &dev);
1256 if (ret && ret != -ENODEV) {
1257 pr_err("%s: Failed to get ELM device: %d\n", __func__, ret);
1258 return;
1259 }
1260#endif
1261
Roger Quadros80cf6372022-12-20 12:21:59 +02001262 ret = uclass_get_device_by_driver(UCLASS_MTD,
1263 DM_DRIVER_GET(gpmc_nand), &dev);
1264 if (ret && ret != -ENODEV)
1265 pr_err("%s: Failed to get GPMC device: %d\n", __func__, ret);
1266}
1267
1268#else
1269
1270int board_nand_init(struct nand_chip *nand)
1271{
1272 return gpmc_nand_init(nand);
1273}
1274
1275#endif /* CONFIG_SYS_NAND_SELF_INIT */
Roger Quadros685c4282022-12-20 12:22:00 +02001276
1277#if defined(CONFIG_SPL_NAND_INIT)
1278
1279/* nand_init() is provided by nand.c */
1280
1281/* Unselect after operation */
1282void nand_deselect(void)
1283{
1284 struct mtd_info *mtd = nand_to_mtd(nand_chip);
1285
1286 if (nand_chip->select_chip)
1287 nand_chip->select_chip(mtd, -1);
1288}
1289
1290static int nand_is_bad_block(int block)
1291{
1292 struct mtd_info *mtd = nand_to_mtd(nand_chip);
1293
1294 loff_t ofs = block * CONFIG_SYS_NAND_BLOCK_SIZE;
1295
1296 return nand_chip->block_bad(mtd, ofs);
1297}
1298
1299static int nand_read_page(int block, int page, uchar *dst)
1300{
1301 int page_addr = block * CONFIG_SYS_NAND_PAGE_COUNT + page;
1302 loff_t ofs = page_addr * CONFIG_SYS_NAND_PAGE_SIZE;
1303 int ret;
1304 size_t len = CONFIG_SYS_NAND_PAGE_SIZE;
1305 struct mtd_info *mtd = nand_to_mtd(nand_chip);
1306
1307 ret = nand_read(mtd, ofs, &len, dst);
1308 if (ret)
1309 printf("nand_read failed %d\n", ret);
1310
1311 return ret;
1312}
1313
1314#include "nand_spl_loaders.c"
1315#endif /* CONFIG_SPL_NAND_INIT */