blob: fa60c371c2da28a63c3dd767f3e2a7407ca11b80 [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>
pekon gupta7295fe82013-11-22 16:53:30 +053023#include <linux/mtd/omap_elm.h>
pekon gupta6bd91a82013-11-18 19:03:00 +053024
Roger Quadros6550c622022-10-11 14:49:59 +030025#ifndef GPMC_MAX_CS
26#define GPMC_MAX_CS 4
27#endif
28
pekon gupta6bd91a82013-11-18 19:03:00 +053029#define BADBLOCK_MARKER_LENGTH 2
30#define SECTOR_BYTES 512
Roger Quadrosaa418fb2022-12-20 12:21:56 +020031#define ECCSIZE0_SHIFT 12
32#define ECCSIZE1_SHIFT 22
33#define ECC1RESULTSIZE 0x1
pekon guptaeff10ee2013-11-19 11:02:15 +053034#define ECCCLEAR (0x1 << 8)
35#define ECCRESULTREG1 (0x1 << 0)
pekon guptab0f750a2013-11-19 11:02:17 +053036/* 4 bit padding to make byte aligned, 56 = 52 + 4 */
37#define BCH4_BIT_PAD 4
38
pekon gupta03742c92013-11-19 11:02:16 +053039#ifdef CONFIG_BCH
40static u8 bch8_polynomial[] = {0xef, 0x51, 0x2e, 0x09, 0xed, 0x93, 0x9a, 0xc2,
41 0x97, 0x79, 0xe5, 0x24, 0xb5};
42#endif
Rostislav Lisovy77cdf8a2014-09-02 16:23:58 +020043static uint8_t cs_next;
Dirk Behme778933f2008-12-14 09:47:16 +010044
Michal Sojka26160072015-02-17 17:08:37 +010045#if defined(CONFIG_NAND_OMAP_GPMC_WSCFG)
46static const int8_t wscfg[CONFIG_SYS_MAX_NAND_DEVICE] =
47 { CONFIG_NAND_OMAP_GPMC_WSCFG };
48#else
49/* wscfg is preset to zero since its a static variable */
50static const int8_t wscfg[CONFIG_SYS_MAX_NAND_DEVICE];
51#endif
52
Dirk Behme778933f2008-12-14 09:47:16 +010053/*
Rostislav Lisovy77cdf8a2014-09-02 16:23:58 +020054 * Driver configurations
55 */
56struct omap_nand_info {
57 struct bch_control *control;
58 enum omap_ecc ecc_scheme;
Michal Sojka26160072015-02-17 17:08:37 +010059 uint8_t cs;
60 uint8_t ws; /* wait status pin (0,1) */
Roger Quadros3b7aaa52022-10-11 14:50:02 +030061 void __iomem *fifo;
Rostislav Lisovy77cdf8a2014-09-02 16:23:58 +020062};
63
64/* We are wasting a bit of memory but al least we are safe */
65static struct omap_nand_info omap_nand_info[GPMC_MAX_CS];
66
67/*
Dirk Behme778933f2008-12-14 09:47:16 +010068 * omap_nand_hwcontrol - Set the address pointers corretly for the
69 * following address/data/command operation
70 */
71static void omap_nand_hwcontrol(struct mtd_info *mtd, int32_t cmd,
72 uint32_t ctrl)
73{
Scott Wood17fed142016-05-30 13:57:56 -050074 register struct nand_chip *this = mtd_to_nand(mtd);
75 struct omap_nand_info *info = nand_get_controller_data(this);
Rostislav Lisovy77cdf8a2014-09-02 16:23:58 +020076 int cs = info->cs;
Dirk Behme778933f2008-12-14 09:47:16 +010077
78 /*
79 * Point the IO_ADDR to DATA and ADDRESS registers instead
80 * of chip address
81 */
82 switch (ctrl) {
83 case NAND_CTRL_CHANGE | NAND_CTRL_CLE:
Dirk Behmea4becd62009-08-08 09:30:22 +020084 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
Dirk Behme778933f2008-12-14 09:47:16 +010085 break;
86 case NAND_CTRL_CHANGE | NAND_CTRL_ALE:
Dirk Behmea4becd62009-08-08 09:30:22 +020087 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_adr;
Dirk Behme778933f2008-12-14 09:47:16 +010088 break;
89 case NAND_CTRL_CHANGE | NAND_NCE:
Dirk Behmea4becd62009-08-08 09:30:22 +020090 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
Dirk Behme778933f2008-12-14 09:47:16 +010091 break;
92 }
93
94 if (cmd != NAND_CMD_NONE)
95 writeb(cmd, this->IO_ADDR_W);
96}
97
Simon Schwarz4f62e982011-09-14 15:30:16 -040098/* Check wait pin as dev ready indicator */
Stefan Roesee05972f2014-11-13 03:43:39 +010099static int omap_dev_ready(struct mtd_info *mtd)
Simon Schwarz4f62e982011-09-14 15:30:16 -0400100{
Scott Wood17fed142016-05-30 13:57:56 -0500101 register struct nand_chip *this = mtd_to_nand(mtd);
102 struct omap_nand_info *info = nand_get_controller_data(this);
Michal Sojka26160072015-02-17 17:08:37 +0100103 return gpmc_cfg->status & (1 << (8 + info->ws));
Simon Schwarz4f62e982011-09-14 15:30:16 -0400104}
Dirk Behme778933f2008-12-14 09:47:16 +0100105
106/*
107 * gen_true_ecc - This function will generate true ECC value, which
108 * can be used when correcting data read from NAND flash memory core
109 *
110 * @ecc_buf: buffer to store ecc code
111 *
112 * @return: re-formatted ECC value
113 */
114static uint32_t gen_true_ecc(uint8_t *ecc_buf)
115{
116 return ecc_buf[0] | (ecc_buf[1] << 16) | ((ecc_buf[2] & 0xF0) << 20) |
117 ((ecc_buf[2] & 0x0F) << 8);
118}
119
120/*
121 * omap_correct_data - Compares the ecc read from nand spare area with ECC
Vagrant Cascadianedfdb992016-04-30 19:18:00 -0700122 * registers values and corrects one bit error if it has occurred
Dirk Behme778933f2008-12-14 09:47:16 +0100123 * Further details can be had from OMAP TRM and the following selected links:
124 * http://en.wikipedia.org/wiki/Hamming_code
125 * http://www.cs.utexas.edu/users/plaxton/c/337/05f/slides/ErrorCorrection-4.pdf
126 *
127 * @mtd: MTD device structure
128 * @dat: page data
129 * @read_ecc: ecc read from nand flash
130 * @calc_ecc: ecc read from ECC registers
131 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100132 * Return: 0 if data is OK or corrected, else returns -1
Dirk Behme778933f2008-12-14 09:47:16 +0100133 */
Stefano Babicaade5792012-03-21 23:56:17 +0000134static int __maybe_unused omap_correct_data(struct mtd_info *mtd, uint8_t *dat,
Dirk Behme778933f2008-12-14 09:47:16 +0100135 uint8_t *read_ecc, uint8_t *calc_ecc)
136{
137 uint32_t orig_ecc, new_ecc, res, hm;
138 uint16_t parity_bits, byte;
139 uint8_t bit;
140
141 /* Regenerate the orginal ECC */
142 orig_ecc = gen_true_ecc(read_ecc);
143 new_ecc = gen_true_ecc(calc_ecc);
144 /* Get the XOR of real ecc */
145 res = orig_ecc ^ new_ecc;
146 if (res) {
147 /* Get the hamming width */
148 hm = hweight32(res);
149 /* Single bit errors can be corrected! */
150 if (hm == 12) {
151 /* Correctable data! */
152 parity_bits = res >> 16;
153 bit = (parity_bits & 0x7);
154 byte = (parity_bits >> 3) & 0x1FF;
155 /* Flip the bit to correct */
156 dat[byte] ^= (0x1 << bit);
157 } else if (hm == 1) {
158 printf("Error: Ecc is wrong\n");
159 /* ECC itself is corrupted */
160 return 2;
161 } else {
162 /*
163 * hm distance != parity pairs OR one, could mean 2 bit
164 * error OR potentially be on a blank page..
165 * orig_ecc: contains spare area data from nand flash.
166 * new_ecc: generated ecc while reading data area.
167 * Note: if the ecc = 0, all data bits from which it was
168 * generated are 0xFF.
169 * The 3 byte(24 bits) ecc is generated per 512byte
170 * chunk of a page. If orig_ecc(from spare area)
171 * is 0xFF && new_ecc(computed now from data area)=0x0,
172 * this means that data area is 0xFF and spare area is
173 * 0xFF. A sure sign of a erased page!
174 */
175 if ((orig_ecc == 0x0FFF0FFF) && (new_ecc == 0x00000000))
176 return 0;
177 printf("Error: Bad compare! failed\n");
178 /* detected 2 bit error */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500179 return -EBADMSG;
Dirk Behme778933f2008-12-14 09:47:16 +0100180 }
181 }
182 return 0;
183}
184
185/*
pekon guptaeff10ee2013-11-19 11:02:15 +0530186 * omap_enable_hwecc - configures GPMC as per ECC scheme before read/write
Andreas Bießmann82a65472013-04-05 04:55:21 +0000187 * @mtd: MTD device structure
188 * @mode: Read/Write mode
189 */
190__maybe_unused
pekon guptaeff10ee2013-11-19 11:02:15 +0530191static void omap_enable_hwecc(struct mtd_info *mtd, int32_t mode)
Andreas Bießmann82a65472013-04-05 04:55:21 +0000192{
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200193 struct nand_chip *nand = mtd_to_nand(mtd);
194 struct omap_nand_info *info = nand_get_controller_data(nand);
pekon guptaeff10ee2013-11-19 11:02:15 +0530195 unsigned int dev_width = (nand->options & NAND_BUSWIDTH_16) ? 1 : 0;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200196 u32 val;
Andreas Bießmann82a65472013-04-05 04:55:21 +0000197
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200198 /* Clear ecc and enable bits */
199 writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
200
201 /* program ecc and result sizes */
202 val = ((((nand->ecc.size >> 1) - 1) << ECCSIZE1_SHIFT) |
203 ECC1RESULTSIZE);
204 writel(val, &gpmc_cfg->ecc_size_config);
205
206 switch (mode) {
207 case NAND_ECC_READ:
208 case NAND_ECC_WRITE:
209 writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
pekon guptaeff10ee2013-11-19 11:02:15 +0530210 break;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200211 case NAND_ECC_READSYN:
212 writel(ECCCLEAR, &gpmc_cfg->ecc_control);
pekon gupta046cf862014-06-02 17:14:42 +0530213 break;
pekon guptaeff10ee2013-11-19 11:02:15 +0530214 default:
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200215 printf("%s: error: unrecognized Mode[%d]!\n", __func__, mode);
216 break;
pekon gupta6bd91a82013-11-18 19:03:00 +0530217 }
Andreas Bießmann82a65472013-04-05 04:55:21 +0000218
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200219 /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
220 val = (dev_width << 7) | (info->cs << 1) | (0x1);
221 writel(val, &gpmc_cfg->ecc_config);
Andreas Bießmann82a65472013-04-05 04:55:21 +0000222}
223
224/*
pekon gupta03742c92013-11-19 11:02:16 +0530225 * omap_calculate_ecc - Read ECC result
226 * @mtd: MTD structure
227 * @dat: unused
228 * @ecc_code: ecc_code buffer
229 * Using noninverted ECC can be considered ugly since writing a blank
230 * page ie. padding will clear the ECC bytes. This is no problem as
231 * long nobody is trying to write data on the seemingly unused page.
232 * Reading an erased page will produce an ECC mismatch between
233 * generated and read ECC bytes that has to be dealt with separately.
234 * E.g. if page is 0xFF (fresh erased), and if HW ECC engine within GPMC
235 * is used, the result of read will be 0x0 while the ECC offsets of the
236 * spare area will be 0xFF which will result in an ECC mismatch.
Mansoor Ahamede5612512012-11-06 13:06:33 +0000237 */
pekon gupta03742c92013-11-19 11:02:16 +0530238static int omap_calculate_ecc(struct mtd_info *mtd, const uint8_t *dat,
Mansoor Ahamede5612512012-11-06 13:06:33 +0000239 uint8_t *ecc_code)
240{
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200241 u32 val;
242
243 val = readl(&gpmc_cfg->ecc1_result);
244 ecc_code[0] = val & 0xFF;
245 ecc_code[1] = (val >> 16) & 0xFF;
246 ecc_code[2] = ((val >> 8) & 0x0F) | ((val >> 20) & 0xF0);
247
248 return 0;
249}
250
251/* GPMC ecc engine settings for read */
252#define BCH_WRAPMODE_1 1 /* BCH wrap mode 1 */
253#define BCH8R_ECC_SIZE0 0x1a /* ecc_size0 = 26 */
254#define BCH8R_ECC_SIZE1 0x2 /* ecc_size1 = 2 */
255#define BCH4R_ECC_SIZE0 0xd /* ecc_size0 = 13 */
256#define BCH4R_ECC_SIZE1 0x3 /* ecc_size1 = 3 */
257
258/* GPMC ecc engine settings for write */
259#define BCH_WRAPMODE_6 6 /* BCH wrap mode 6 */
260#define BCH_ECC_SIZE0 0x0 /* ecc_size0 = 0, no oob protection */
261#define BCH_ECC_SIZE1 0x20 /* ecc_size1 = 32 */
262
263/**
264 * omap_enable_hwecc_bch - Program GPMC to perform BCH ECC calculation
265 * @mtd: MTD device structure
266 * @mode: Read/Write mode
267 *
268 * When using BCH with SW correction (i.e. no ELM), sector size is set
269 * to 512 bytes and we use BCH_WRAPMODE_6 wrapping mode
270 * for both reading and writing with:
271 * eccsize0 = 0 (no additional protected byte in spare area)
272 * eccsize1 = 32 (skip 32 nibbles = 16 bytes per sector in spare area)
273 */
274static void __maybe_unused omap_enable_hwecc_bch(struct mtd_info *mtd,
275 int mode)
276{
277 unsigned int bch_type;
278 unsigned int dev_width, nsectors;
Scott Wood17fed142016-05-30 13:57:56 -0500279 struct nand_chip *chip = mtd_to_nand(mtd);
280 struct omap_nand_info *info = nand_get_controller_data(chip);
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200281 u32 val, wr_mode;
282 unsigned int ecc_size1, ecc_size0;
283
284 /* GPMC configurations for calculating ECC */
285 switch (info->ecc_scheme) {
286 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
287 bch_type = 1;
288 nsectors = 1;
289 wr_mode = BCH_WRAPMODE_6;
290 ecc_size0 = BCH_ECC_SIZE0;
291 ecc_size1 = BCH_ECC_SIZE1;
292 break;
293 case OMAP_ECC_BCH8_CODE_HW:
294 bch_type = 1;
295 nsectors = chip->ecc.steps;
296 if (mode == NAND_ECC_READ) {
297 wr_mode = BCH_WRAPMODE_1;
298 ecc_size0 = BCH8R_ECC_SIZE0;
299 ecc_size1 = BCH8R_ECC_SIZE1;
300 } else {
301 wr_mode = BCH_WRAPMODE_6;
302 ecc_size0 = BCH_ECC_SIZE0;
303 ecc_size1 = BCH_ECC_SIZE1;
304 }
305 break;
306 case OMAP_ECC_BCH16_CODE_HW:
307 bch_type = 0x2;
308 nsectors = chip->ecc.steps;
309 if (mode == NAND_ECC_READ) {
310 wr_mode = 0x01;
311 ecc_size0 = 52; /* ECC bits in nibbles per sector */
312 ecc_size1 = 0; /* non-ECC bits in nibbles per sector */
313 } else {
314 wr_mode = 0x01;
315 ecc_size0 = 0; /* extra bits in nibbles per sector */
316 ecc_size1 = 52; /* OOB bits in nibbles per sector */
317 }
318 break;
319 default:
320 return;
321 }
322
323 writel(ECCRESULTREG1, &gpmc_cfg->ecc_control);
324
325 /* Configure ecc size for BCH */
326 val = (ecc_size1 << ECCSIZE1_SHIFT) | (ecc_size0 << ECCSIZE0_SHIFT);
327 writel(val, &gpmc_cfg->ecc_size_config);
328
329 dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
330
331 /* BCH configuration */
332 val = ((1 << 16) | /* enable BCH */
333 (bch_type << 12) | /* BCH4/BCH8/BCH16 */
334 (wr_mode << 8) | /* wrap mode */
335 (dev_width << 7) | /* bus width */
336 (((nsectors - 1) & 0x7) << 4) | /* number of sectors */
337 (info->cs << 1) | /* ECC CS */
338 (0x1)); /* enable ECC */
339
340 writel(val, &gpmc_cfg->ecc_config);
341
342 /* Clear ecc and enable bits */
343 writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
344}
345
346/**
347 * _omap_calculate_ecc_bch - Generate BCH ECC bytes for one sector
348 * @mtd: MTD device structure
349 * @dat: The pointer to data on which ecc is computed
350 * @ecc_code: The ecc_code buffer
351 * @sector: The sector number (for a multi sector page)
352 *
353 * Support calculating of BCH4/8/16 ECC vectors for one sector
354 * within a page. Sector number is in @sector.
355 */
356static int _omap_calculate_ecc_bch(struct mtd_info *mtd, const u8 *dat,
357 u8 *ecc_code, int sector)
358{
359 struct nand_chip *chip = mtd_to_nand(mtd);
360 struct omap_nand_info *info = nand_get_controller_data(chip);
Ladislav Michld5b1c272016-07-12 20:28:16 +0200361 const uint32_t *ptr;
362 uint32_t val = 0;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000363 int8_t i = 0, j;
364
pekon guptaaa168482014-04-11 12:55:33 +0530365 switch (info->ecc_scheme) {
pekon gupta03742c92013-11-19 11:02:16 +0530366#ifdef CONFIG_BCH
367 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
368#endif
369 case OMAP_ECC_BCH8_CODE_HW:
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200370 ptr = &gpmc_cfg->bch_result_0_3[sector].bch_result_x[3];
pekon gupta03742c92013-11-19 11:02:16 +0530371 val = readl(ptr);
372 ecc_code[i++] = (val >> 0) & 0xFF;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000373 ptr--;
374 for (j = 0; j < 3; j++) {
pekon gupta03742c92013-11-19 11:02:16 +0530375 val = readl(ptr);
376 ecc_code[i++] = (val >> 24) & 0xFF;
377 ecc_code[i++] = (val >> 16) & 0xFF;
378 ecc_code[i++] = (val >> 8) & 0xFF;
379 ecc_code[i++] = (val >> 0) & 0xFF;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000380 ptr--;
381 }
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200382
pekon gupta03742c92013-11-19 11:02:16 +0530383 break;
pekon gupta046cf862014-06-02 17:14:42 +0530384 case OMAP_ECC_BCH16_CODE_HW:
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200385 val = readl(&gpmc_cfg->bch_result_4_6[sector].bch_result_x[2]);
pekon gupta046cf862014-06-02 17:14:42 +0530386 ecc_code[i++] = (val >> 8) & 0xFF;
387 ecc_code[i++] = (val >> 0) & 0xFF;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200388 val = readl(&gpmc_cfg->bch_result_4_6[sector].bch_result_x[1]);
pekon gupta046cf862014-06-02 17:14:42 +0530389 ecc_code[i++] = (val >> 24) & 0xFF;
390 ecc_code[i++] = (val >> 16) & 0xFF;
391 ecc_code[i++] = (val >> 8) & 0xFF;
392 ecc_code[i++] = (val >> 0) & 0xFF;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200393 val = readl(&gpmc_cfg->bch_result_4_6[sector].bch_result_x[0]);
pekon gupta046cf862014-06-02 17:14:42 +0530394 ecc_code[i++] = (val >> 24) & 0xFF;
395 ecc_code[i++] = (val >> 16) & 0xFF;
396 ecc_code[i++] = (val >> 8) & 0xFF;
397 ecc_code[i++] = (val >> 0) & 0xFF;
398 for (j = 3; j >= 0; j--) {
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200399 val = readl(&gpmc_cfg->bch_result_0_3[sector].bch_result_x[j]
pekon gupta046cf862014-06-02 17:14:42 +0530400 );
401 ecc_code[i++] = (val >> 24) & 0xFF;
402 ecc_code[i++] = (val >> 16) & 0xFF;
403 ecc_code[i++] = (val >> 8) & 0xFF;
404 ecc_code[i++] = (val >> 0) & 0xFF;
405 }
406 break;
pekon gupta03742c92013-11-19 11:02:16 +0530407 default:
408 return -EINVAL;
409 }
410 /* ECC scheme specific syndrome customizations */
pekon guptaaa168482014-04-11 12:55:33 +0530411 switch (info->ecc_scheme) {
pekon gupta03742c92013-11-19 11:02:16 +0530412#ifdef CONFIG_BCH
413 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200414 /* Add constant polynomial to remainder, so that
415 * ECC of blank pages results in 0x0 on reading back
416 */
pekon gupta03742c92013-11-19 11:02:16 +0530417 for (i = 0; i < chip->ecc.bytes; i++)
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200418 ecc_code[i] ^= bch8_polynomial[i];
pekon gupta03742c92013-11-19 11:02:16 +0530419 break;
420#endif
421 case OMAP_ECC_BCH8_CODE_HW:
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200422 /* Set 14th ECC byte as 0x0 for ROM compatibility */
423 ecc_code[chip->ecc.bytes - 1] = 0x0;
pekon gupta03742c92013-11-19 11:02:16 +0530424 break;
pekon gupta046cf862014-06-02 17:14:42 +0530425 case OMAP_ECC_BCH16_CODE_HW:
426 break;
pekon gupta03742c92013-11-19 11:02:16 +0530427 default:
428 return -EINVAL;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000429 }
pekon gupta03742c92013-11-19 11:02:16 +0530430 return 0;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000431}
432
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200433/**
434 * omap_calculate_ecc_bch - ECC generator for 1 sector
435 * @mtd: MTD device structure
436 * @dat: The pointer to data on which ecc is computed
437 * @ecc_code: The ecc_code buffer
438 *
439 * Support calculating of BCH4/8/16 ECC vectors for one sector. This is used
440 * when SW based correction is required as ECC is required for one sector
441 * at a time.
442 */
443static int __maybe_unused omap_calculate_ecc_bch(struct mtd_info *mtd,
444 const u_char *dat, u_char *ecc_calc)
445{
446 return _omap_calculate_ecc_bch(mtd, dat, ecc_calc, 0);
447}
448
Roger Quadros3b7aaa52022-10-11 14:50:02 +0300449static inline void omap_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
450{
451 struct nand_chip *chip = mtd_to_nand(mtd);
452 struct omap_nand_info *info = nand_get_controller_data(chip);
453 u32 alignment = ((uintptr_t)buf | len) & 3;
454
455 if (alignment & 1)
456 readsb(info->fifo, buf, len);
457 else if (alignment & 3)
458 readsw(info->fifo, buf, len >> 1);
459 else
460 readsl(info->fifo, buf, len >> 2);
461}
462
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200463#ifdef CONFIG_NAND_OMAP_GPMC_PREFETCH
464
465#define PREFETCH_CONFIG1_CS_SHIFT 24
466#define PREFETCH_FIFOTHRESHOLD_MAX 0x40
467#define PREFETCH_FIFOTHRESHOLD(val) ((val) << 8)
468#define PREFETCH_STATUS_COUNT(val) (val & 0x00003fff)
469#define PREFETCH_STATUS_FIFO_CNT(val) ((val >> 24) & 0x7F)
470#define ENABLE_PREFETCH (1 << 7)
471
472/**
473 * omap_prefetch_enable - configures and starts prefetch transfer
474 * @fifo_th: fifo threshold to be used for read/ write
475 * @count: number of bytes to be transferred
476 * @is_write: prefetch read(0) or write post(1) mode
477 * @cs: chip select to use
478 */
479static int omap_prefetch_enable(int fifo_th, unsigned int count, int is_write, int cs)
480{
481 uint32_t val;
482
483 if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX)
484 return -EINVAL;
485
486 if (readl(&gpmc_cfg->prefetch_control))
487 return -EBUSY;
488
489 /* Set the amount of bytes to be prefetched */
490 writel(count, &gpmc_cfg->prefetch_config2);
491
492 val = (cs << PREFETCH_CONFIG1_CS_SHIFT) | (is_write & 1) |
493 PREFETCH_FIFOTHRESHOLD(fifo_th) | ENABLE_PREFETCH;
494 writel(val, &gpmc_cfg->prefetch_config1);
495
496 /* Start the prefetch engine */
497 writel(1, &gpmc_cfg->prefetch_control);
498
499 return 0;
500}
501
502/**
503 * omap_prefetch_reset - disables and stops the prefetch engine
504 */
505static void omap_prefetch_reset(void)
506{
507 writel(0, &gpmc_cfg->prefetch_control);
508 writel(0, &gpmc_cfg->prefetch_config1);
509}
510
511static int __read_prefetch_aligned(struct nand_chip *chip, uint32_t *buf, int len)
512{
513 int ret;
514 uint32_t cnt;
Scott Wood17fed142016-05-30 13:57:56 -0500515 struct omap_nand_info *info = nand_get_controller_data(chip);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200516
517 ret = omap_prefetch_enable(PREFETCH_FIFOTHRESHOLD_MAX, len, 0, info->cs);
518 if (ret < 0)
519 return ret;
520
521 do {
522 int i;
523
524 cnt = readl(&gpmc_cfg->prefetch_status);
525 cnt = PREFETCH_STATUS_FIFO_CNT(cnt);
526
527 for (i = 0; i < cnt / 4; i++) {
Roger Quadros3b7aaa52022-10-11 14:50:02 +0300528 *buf++ = readl(info->fifo);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200529 len -= 4;
530 }
531 } while (len);
532
533 omap_prefetch_reset();
534
535 return 0;
536}
537
Jeroen Hofstee5e67ac72015-05-30 10:11:24 +0200538static void omap_nand_read_prefetch(struct mtd_info *mtd, uint8_t *buf, int len)
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200539{
540 int ret;
Roger Quadros3068f0f2022-10-11 14:50:01 +0300541 uintptr_t head, tail;
Scott Wood17fed142016-05-30 13:57:56 -0500542 struct nand_chip *chip = mtd_to_nand(mtd);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200543
544 /*
545 * If the destination buffer is unaligned, start with reading
546 * the overlap byte-wise.
547 */
Roger Quadros3068f0f2022-10-11 14:50:01 +0300548 head = ((uintptr_t)buf) % 4;
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200549 if (head) {
Roger Quadros3b7aaa52022-10-11 14:50:02 +0300550 omap_nand_read_buf(mtd, buf, head);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200551 buf += head;
552 len -= head;
553 }
554
555 /*
556 * Only transfer multiples of 4 bytes in a pre-fetched fashion.
557 * If there's a residue, care for it byte-wise afterwards.
558 */
559 tail = len % 4;
560
Jeroen Hofstee5e67ac72015-05-30 10:11:24 +0200561 ret = __read_prefetch_aligned(chip, (uint32_t *)buf, len - tail);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200562 if (ret < 0) {
563 /* fallback in case the prefetch engine is busy */
Roger Quadros3b7aaa52022-10-11 14:50:02 +0300564 omap_nand_read_buf(mtd, buf, len);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200565 } else if (tail) {
566 buf += len - tail;
Roger Quadros3b7aaa52022-10-11 14:50:02 +0300567 omap_nand_read_buf(mtd, buf, tail);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200568 }
569}
570#endif /* CONFIG_NAND_OMAP_GPMC_PREFETCH */
571
pekon gupta03742c92013-11-19 11:02:16 +0530572#ifdef CONFIG_NAND_OMAP_ELM
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200573
574/**
575 * omap_calculate_ecc_bch_multi - Generate ECC for multiple sectors
576 * @mtd: MTD device structure
577 * @dat: The pointer to data on which ecc is computed
578 * @ecc_code: The ecc_code buffer
579 *
580 * Support calculating of BCH4/8/16 ecc vectors for the entire page in one go.
581 */
582static int omap_calculate_ecc_bch_multi(struct mtd_info *mtd,
583 const u_char *dat, u_char *ecc_calc)
584{
585 struct nand_chip *chip = mtd_to_nand(mtd);
586 int eccbytes = chip->ecc.bytes;
587 unsigned long nsectors;
588 int i, ret;
589
590 nsectors = ((readl(&gpmc_cfg->ecc_config) >> 4) & 0x7) + 1;
591 for (i = 0; i < nsectors; i++) {
592 ret = _omap_calculate_ecc_bch(mtd, dat, ecc_calc, i);
593 if (ret)
594 return ret;
595
596 ecc_calc += eccbytes;
597 }
598
599 return 0;
600}
601
Mansoor Ahamede5612512012-11-06 13:06:33 +0000602/*
Jeroen Hofstee79369072014-10-08 22:57:42 +0200603 * omap_reverse_list - re-orders list elements in reverse order [internal]
604 * @list: pointer to start of list
605 * @length: length of list
606*/
607static void omap_reverse_list(u8 *list, unsigned int length)
608{
609 unsigned int i, j;
610 unsigned int half_length = length / 2;
611 u8 tmp;
612 for (i = 0, j = length - 1; i < half_length; i++, j--) {
613 tmp = list[i];
614 list[i] = list[j];
615 list[j] = tmp;
616 }
617}
618
619/*
Mansoor Ahamede5612512012-11-06 13:06:33 +0000620 * omap_correct_data_bch - Compares the ecc read from nand spare area
Vagrant Cascadianedfdb992016-04-30 19:18:00 -0700621 * with ECC registers values and corrects one bit error if it has occurred
Mansoor Ahamede5612512012-11-06 13:06:33 +0000622 *
623 * @mtd: MTD device structure
624 * @dat: page data
625 * @read_ecc: ecc read from nand flash (ignored)
626 * @calc_ecc: ecc read from ECC registers
627 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100628 * Return: 0 if data is OK or corrected, else returns -1
Mansoor Ahamede5612512012-11-06 13:06:33 +0000629 */
630static int omap_correct_data_bch(struct mtd_info *mtd, uint8_t *dat,
631 uint8_t *read_ecc, uint8_t *calc_ecc)
632{
Scott Wood17fed142016-05-30 13:57:56 -0500633 struct nand_chip *chip = mtd_to_nand(mtd);
634 struct omap_nand_info *info = nand_get_controller_data(chip);
pekon gupta3c43c5b2014-04-11 12:55:34 +0530635 struct nand_ecc_ctrl *ecc = &chip->ecc;
pekon guptab0f750a2013-11-19 11:02:17 +0530636 uint32_t error_count = 0, error_max;
pekon gupta046cf862014-06-02 17:14:42 +0530637 uint32_t error_loc[ELM_MAX_ERROR_COUNT];
pekon gupta9d4b7472014-04-11 12:55:32 +0530638 enum bch_level bch_type;
pekon guptab0f750a2013-11-19 11:02:17 +0530639 uint32_t i, ecc_flag = 0;
Guido Martínez20b27be2015-01-02 14:49:10 -0300640 uint8_t count;
pekon guptab0f750a2013-11-19 11:02:17 +0530641 uint32_t byte_pos, bit_pos;
Guido Martínez20b27be2015-01-02 14:49:10 -0300642 int err = 0;
pekon guptab0f750a2013-11-19 11:02:17 +0530643
644 /* check calculated ecc */
pekon gupta3c43c5b2014-04-11 12:55:34 +0530645 for (i = 0; i < ecc->bytes && !ecc_flag; i++) {
pekon guptab0f750a2013-11-19 11:02:17 +0530646 if (calc_ecc[i] != 0x00)
David Rivshinefa78532021-11-18 13:25:24 -0500647 goto not_ecc_match;
pekon guptab0f750a2013-11-19 11:02:17 +0530648 }
David Rivshinefa78532021-11-18 13:25:24 -0500649 return 0;
650not_ecc_match:
Mansoor Ahamede5612512012-11-06 13:06:33 +0000651
David Rivshinefa78532021-11-18 13:25:24 -0500652 /* check for whether it's an erased-page */
653 for (i = 0; i < ecc->bytes; i++) {
Mansoor Ahamede5612512012-11-06 13:06:33 +0000654 if (read_ecc[i] != 0xff)
David Rivshinefa78532021-11-18 13:25:24 -0500655 goto not_erased;
pekon guptab0f750a2013-11-19 11:02:17 +0530656 }
David Rivshinefa78532021-11-18 13:25:24 -0500657 for (i = 0; i < SECTOR_BYTES; i++) {
658 if (dat[i] != 0xff)
659 goto not_erased;
660 }
661 return 0;
662not_erased:
663
664 /*
665 * Check for whether it's an erased page with a correctable
666 * number of bitflips. Erased pages have all 1's in the data,
667 * so we just compute the number of 0 bits in the data and
668 * see if it's under the correction threshold.
669 *
670 * NOTE: The check for a perfect erased page above is faster for
671 * the more common case, even though it's logically redundant.
672 */
673 for (i = 0; i < ecc->bytes; i++)
674 error_count += hweight8(~read_ecc[i]);
675
676 for (i = 0; i < SECTOR_BYTES; i++)
677 error_count += hweight8(~dat[i]);
678
679 if (error_count <= ecc->strength) {
680 memset(read_ecc, 0xFF, ecc->bytes);
681 memset(dat, 0xFF, SECTOR_BYTES);
682 debug("nand: %u bit-flip(s) corrected in erased page\n",
683 error_count);
684 return error_count;
685 }
Mansoor Ahamede5612512012-11-06 13:06:33 +0000686
Mansoor Ahamede5612512012-11-06 13:06:33 +0000687 /*
688 * while reading ECC result we read it in big endian.
689 * Hence while loading to ELM we have rotate to get the right endian.
690 */
pekon guptaaa168482014-04-11 12:55:33 +0530691 switch (info->ecc_scheme) {
pekon guptab0f750a2013-11-19 11:02:17 +0530692 case OMAP_ECC_BCH8_CODE_HW:
pekon gupta9d4b7472014-04-11 12:55:32 +0530693 bch_type = BCH_8_BIT;
pekon gupta3c43c5b2014-04-11 12:55:34 +0530694 omap_reverse_list(calc_ecc, ecc->bytes - 1);
pekon guptab0f750a2013-11-19 11:02:17 +0530695 break;
pekon gupta046cf862014-06-02 17:14:42 +0530696 case OMAP_ECC_BCH16_CODE_HW:
697 bch_type = BCH_16_BIT;
698 omap_reverse_list(calc_ecc, ecc->bytes);
699 break;
pekon guptab0f750a2013-11-19 11:02:17 +0530700 default:
701 return -EINVAL;
702 }
Mansoor Ahamede5612512012-11-06 13:06:33 +0000703 /* use elm module to check for errors */
pekon gupta9d4b7472014-04-11 12:55:32 +0530704 elm_config(bch_type);
David Rivshinefa78532021-11-18 13:25:24 -0500705 error_count = 0;
pekon guptacfe6b8a2014-04-11 12:55:35 +0530706 err = elm_check_error(calc_ecc, bch_type, &error_count, error_loc);
707 if (err)
708 return err;
709
Mansoor Ahamede5612512012-11-06 13:06:33 +0000710 /* correct bch error */
pekon guptab0f750a2013-11-19 11:02:17 +0530711 for (count = 0; count < error_count; count++) {
pekon guptaaa168482014-04-11 12:55:33 +0530712 switch (info->ecc_scheme) {
pekon gupta9d4b7472014-04-11 12:55:32 +0530713 case OMAP_ECC_BCH8_CODE_HW:
pekon guptab0f750a2013-11-19 11:02:17 +0530714 /* 14th byte in ECC is reserved to match ROM layout */
pekon gupta3c43c5b2014-04-11 12:55:34 +0530715 error_max = SECTOR_BYTES + (ecc->bytes - 1);
pekon guptab0f750a2013-11-19 11:02:17 +0530716 break;
pekon gupta046cf862014-06-02 17:14:42 +0530717 case OMAP_ECC_BCH16_CODE_HW:
718 error_max = SECTOR_BYTES + ecc->bytes;
719 break;
pekon guptab0f750a2013-11-19 11:02:17 +0530720 default:
721 return -EINVAL;
722 }
723 byte_pos = error_max - (error_loc[count] / 8) - 1;
724 bit_pos = error_loc[count] % 8;
725 if (byte_pos < SECTOR_BYTES) {
726 dat[byte_pos] ^= 1 << bit_pos;
Ezequiel García69cf8ad2015-10-04 18:34:42 -0300727 debug("nand: bit-flip corrected @data=%d\n", byte_pos);
pekon guptab0f750a2013-11-19 11:02:17 +0530728 } else if (byte_pos < error_max) {
Belisko Marek9ab54142014-04-25 12:00:07 +0200729 read_ecc[byte_pos - SECTOR_BYTES] ^= 1 << bit_pos;
Ezequiel García69cf8ad2015-10-04 18:34:42 -0300730 debug("nand: bit-flip corrected @oob=%d\n", byte_pos -
pekon guptab0f750a2013-11-19 11:02:17 +0530731 SECTOR_BYTES);
732 } else {
733 err = -EBADMSG;
734 printf("nand: error: invalid bit-flip location\n");
735 }
736 }
737 return (err) ? err : error_count;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000738}
Mansoor Ahamede5612512012-11-06 13:06:33 +0000739
740/**
741 * omap_read_page_bch - hardware ecc based page read function
742 * @mtd: mtd info structure
743 * @chip: nand chip info structure
744 * @buf: buffer to store read data
Sergey Lapin3a38a552013-01-14 03:46:50 +0000745 * @oob_required: caller expects OOB data read to chip->oob_poi
Mansoor Ahamede5612512012-11-06 13:06:33 +0000746 * @page: page number to read
747 *
748 */
749static int omap_read_page_bch(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000750 uint8_t *buf, int oob_required, int page)
Mansoor Ahamede5612512012-11-06 13:06:33 +0000751{
752 int i, eccsize = chip->ecc.size;
753 int eccbytes = chip->ecc.bytes;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200754 int ecctotal = chip->ecc.total;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000755 int eccsteps = chip->ecc.steps;
756 uint8_t *p = buf;
757 uint8_t *ecc_calc = chip->buffers->ecccalc;
758 uint8_t *ecc_code = chip->buffers->ecccode;
759 uint32_t *eccpos = chip->ecc.layout->eccpos;
760 uint8_t *oob = chip->oob_poi;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000761 uint32_t oob_pos;
762
Mansoor Ahamede5612512012-11-06 13:06:33 +0000763 /* oob area start */
764 oob_pos = (eccsize * eccsteps) + chip->ecc.layout->eccpos[0];
765 oob += chip->ecc.layout->eccpos[0];
766
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200767 /* Enable ECC engine */
768 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Mansoor Ahamede5612512012-11-06 13:06:33 +0000769
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200770 /* read entire page */
771 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, 0, -1);
772 chip->read_buf(mtd, buf, mtd->writesize);
Mansoor Ahamede5612512012-11-06 13:06:33 +0000773
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200774 /* read all ecc bytes from oob area */
775 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_pos, -1);
776 chip->read_buf(mtd, oob, ecctotal);
777
778 /* Calculate ecc bytes */
779 omap_calculate_ecc_bch_multi(mtd, buf, ecc_calc);
Mansoor Ahamede5612512012-11-06 13:06:33 +0000780
781 for (i = 0; i < chip->ecc.total; i++)
782 ecc_code[i] = chip->oob_poi[eccpos[i]];
783
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200784 /* error detect & correct */
Mansoor Ahamede5612512012-11-06 13:06:33 +0000785 eccsteps = chip->ecc.steps;
786 p = buf;
787
788 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
789 int stat;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000790 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
791 if (stat < 0)
792 mtd->ecc_stats.failed++;
793 else
794 mtd->ecc_stats.corrected += stat;
795 }
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200796
Mansoor Ahamede5612512012-11-06 13:06:33 +0000797 return 0;
798}
pekon gupta6bd91a82013-11-18 19:03:00 +0530799#endif /* CONFIG_NAND_OMAP_ELM */
Mansoor Ahamede5612512012-11-06 13:06:33 +0000800
Andreas Bießmann82a65472013-04-05 04:55:21 +0000801/*
802 * OMAP3 BCH8 support (with BCH library)
803 */
pekon gupta6bd91a82013-11-18 19:03:00 +0530804#ifdef CONFIG_BCH
Andreas Bießmann82a65472013-04-05 04:55:21 +0000805/**
pekon gupta6bd91a82013-11-18 19:03:00 +0530806 * omap_correct_data_bch_sw - Decode received data and correct errors
Andreas Bießmann82a65472013-04-05 04:55:21 +0000807 * @mtd: MTD device structure
808 * @data: page data
809 * @read_ecc: ecc read from nand flash
810 * @calc_ecc: ecc read from HW ECC registers
811 */
pekon gupta6bd91a82013-11-18 19:03:00 +0530812static int omap_correct_data_bch_sw(struct mtd_info *mtd, u_char *data,
Andreas Bießmann82a65472013-04-05 04:55:21 +0000813 u_char *read_ecc, u_char *calc_ecc)
814{
815 int i, count;
816 /* cannot correct more than 8 errors */
817 unsigned int errloc[8];
Scott Wood17fed142016-05-30 13:57:56 -0500818 struct nand_chip *chip = mtd_to_nand(mtd);
819 struct omap_nand_info *info = nand_get_controller_data(chip);
Andreas Bießmann82a65472013-04-05 04:55:21 +0000820
Ladislav Michl6cc51852017-01-09 11:15:14 +0100821 count = decode_bch(info->control, NULL, SECTOR_BYTES,
822 read_ecc, calc_ecc, NULL, errloc);
Andreas Bießmann82a65472013-04-05 04:55:21 +0000823 if (count > 0) {
824 /* correct errors */
825 for (i = 0; i < count; i++) {
826 /* correct data only, not ecc bytes */
Ladislav Michl6cc51852017-01-09 11:15:14 +0100827 if (errloc[i] < SECTOR_BYTES << 3)
828 data[errloc[i] >> 3] ^= 1 << (errloc[i] & 7);
Ezequiel García69cf8ad2015-10-04 18:34:42 -0300829 debug("corrected bitflip %u\n", errloc[i]);
Andreas Bießmann82a65472013-04-05 04:55:21 +0000830#ifdef DEBUG
831 puts("read_ecc: ");
832 /*
833 * BCH8 have 13 bytes of ECC; BCH4 needs adoption
834 * here!
835 */
836 for (i = 0; i < 13; i++)
837 printf("%02x ", read_ecc[i]);
838 puts("\n");
839 puts("calc_ecc: ");
840 for (i = 0; i < 13; i++)
841 printf("%02x ", calc_ecc[i]);
842 puts("\n");
843#endif
844 }
845 } else if (count < 0) {
846 puts("ecc unrecoverable error\n");
847 }
848 return count;
849}
850
851/**
852 * omap_free_bch - Release BCH ecc resources
853 * @mtd: MTD device structure
854 */
855static void __maybe_unused omap_free_bch(struct mtd_info *mtd)
856{
Scott Wood17fed142016-05-30 13:57:56 -0500857 struct nand_chip *chip = mtd_to_nand(mtd);
858 struct omap_nand_info *info = nand_get_controller_data(chip);
Andreas Bießmann82a65472013-04-05 04:55:21 +0000859
pekon guptaaa168482014-04-11 12:55:33 +0530860 if (info->control) {
861 free_bch(info->control);
862 info->control = NULL;
Andreas Bießmann82a65472013-04-05 04:55:21 +0000863 }
864}
pekon gupta6bd91a82013-11-18 19:03:00 +0530865#endif /* CONFIG_BCH */
866
867/**
868 * omap_select_ecc_scheme - configures driver for particular ecc-scheme
869 * @nand: NAND chip device structure
870 * @ecc_scheme: ecc scheme to configure
871 * @pagesize: number of main-area bytes per page of NAND device
872 * @oobsize: number of OOB/spare bytes per page of NAND device
873 */
874static int omap_select_ecc_scheme(struct nand_chip *nand,
875 enum omap_ecc ecc_scheme, unsigned int pagesize, unsigned int oobsize) {
Scott Wood17fed142016-05-30 13:57:56 -0500876 struct omap_nand_info *info = nand_get_controller_data(nand);
Roger Quadros224add22022-10-11 14:50:06 +0300877 struct nand_ecclayout *ecclayout = nand->ecc.layout;
pekon gupta6bd91a82013-11-18 19:03:00 +0530878 int eccsteps = pagesize / SECTOR_BYTES;
879 int i;
880
881 switch (ecc_scheme) {
882 case OMAP_ECC_HAM1_CODE_SW:
883 debug("nand: selected OMAP_ECC_HAM1_CODE_SW\n");
884 /* For this ecc-scheme, ecc.bytes, ecc.layout, ... are
885 * initialized in nand_scan_tail(), so just set ecc.mode */
pekon guptaaa168482014-04-11 12:55:33 +0530886 info->control = NULL;
pekon gupta6bd91a82013-11-18 19:03:00 +0530887 nand->ecc.mode = NAND_ECC_SOFT;
888 nand->ecc.layout = NULL;
Nikita Kiryanov4110c822013-12-12 15:19:31 +0200889 nand->ecc.size = 0;
pekon gupta6bd91a82013-11-18 19:03:00 +0530890 break;
891
892 case OMAP_ECC_HAM1_CODE_HW:
893 debug("nand: selected OMAP_ECC_HAM1_CODE_HW\n");
894 /* check ecc-scheme requirements before updating ecc info */
895 if ((3 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
896 printf("nand: error: insufficient OOB: require=%d\n", (
897 (3 * eccsteps) + BADBLOCK_MARKER_LENGTH));
898 return -EINVAL;
899 }
pekon guptaaa168482014-04-11 12:55:33 +0530900 info->control = NULL;
pekon gupta6bd91a82013-11-18 19:03:00 +0530901 /* populate ecc specific fields */
Nikita Kiryanov2e18ff22013-12-17 15:18:01 +0200902 memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
pekon gupta6bd91a82013-11-18 19:03:00 +0530903 nand->ecc.mode = NAND_ECC_HW;
904 nand->ecc.strength = 1;
905 nand->ecc.size = SECTOR_BYTES;
906 nand->ecc.bytes = 3;
907 nand->ecc.hwctl = omap_enable_hwecc;
908 nand->ecc.correct = omap_correct_data;
909 nand->ecc.calculate = omap_calculate_ecc;
910 /* define ecc-layout */
911 ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
pekon guptaf0aff022013-12-05 17:54:21 +0530912 for (i = 0; i < ecclayout->eccbytes; i++) {
913 if (nand->options & NAND_BUSWIDTH_16)
914 ecclayout->eccpos[i] = i + 2;
915 else
916 ecclayout->eccpos[i] = i + 1;
917 }
pekon gupta6bd91a82013-11-18 19:03:00 +0530918 ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
919 ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
920 BADBLOCK_MARKER_LENGTH;
pekon gupta6bd91a82013-11-18 19:03:00 +0530921 break;
922
923 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
924#ifdef CONFIG_BCH
925 debug("nand: selected OMAP_ECC_BCH8_CODE_HW_DETECTION_SW\n");
926 /* check ecc-scheme requirements before updating ecc info */
927 if ((13 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
928 printf("nand: error: insufficient OOB: require=%d\n", (
929 (13 * eccsteps) + BADBLOCK_MARKER_LENGTH));
930 return -EINVAL;
931 }
932 /* check if BCH S/W library can be used for error detection */
pekon guptaaa168482014-04-11 12:55:33 +0530933 info->control = init_bch(13, 8, 0x201b);
934 if (!info->control) {
pekon gupta6bd91a82013-11-18 19:03:00 +0530935 printf("nand: error: could not init_bch()\n");
936 return -ENODEV;
937 }
pekon gupta6bd91a82013-11-18 19:03:00 +0530938 /* populate ecc specific fields */
Nikita Kiryanov2e18ff22013-12-17 15:18:01 +0200939 memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
pekon gupta6bd91a82013-11-18 19:03:00 +0530940 nand->ecc.mode = NAND_ECC_HW;
941 nand->ecc.strength = 8;
942 nand->ecc.size = SECTOR_BYTES;
943 nand->ecc.bytes = 13;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200944 nand->ecc.hwctl = omap_enable_hwecc_bch;
pekon gupta6bd91a82013-11-18 19:03:00 +0530945 nand->ecc.correct = omap_correct_data_bch_sw;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200946 nand->ecc.calculate = omap_calculate_ecc_bch;
pekon gupta6bd91a82013-11-18 19:03:00 +0530947 /* define ecc-layout */
948 ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
949 ecclayout->eccpos[0] = BADBLOCK_MARKER_LENGTH;
950 for (i = 1; i < ecclayout->eccbytes; i++) {
951 if (i % nand->ecc.bytes)
952 ecclayout->eccpos[i] =
953 ecclayout->eccpos[i - 1] + 1;
954 else
955 ecclayout->eccpos[i] =
956 ecclayout->eccpos[i - 1] + 2;
957 }
958 ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
959 ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
960 BADBLOCK_MARKER_LENGTH;
pekon gupta6bd91a82013-11-18 19:03:00 +0530961 break;
962#else
963 printf("nand: error: CONFIG_BCH required for ECC\n");
964 return -EINVAL;
965#endif
966
967 case OMAP_ECC_BCH8_CODE_HW:
968#ifdef CONFIG_NAND_OMAP_ELM
969 debug("nand: selected OMAP_ECC_BCH8_CODE_HW\n");
970 /* check ecc-scheme requirements before updating ecc info */
971 if ((14 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
972 printf("nand: error: insufficient OOB: require=%d\n", (
973 (14 * eccsteps) + BADBLOCK_MARKER_LENGTH));
974 return -EINVAL;
975 }
976 /* intialize ELM for ECC error detection */
977 elm_init();
pekon guptaaa168482014-04-11 12:55:33 +0530978 info->control = NULL;
pekon gupta6bd91a82013-11-18 19:03:00 +0530979 /* populate ecc specific fields */
Nikita Kiryanov2e18ff22013-12-17 15:18:01 +0200980 memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
pekon gupta6bd91a82013-11-18 19:03:00 +0530981 nand->ecc.mode = NAND_ECC_HW;
982 nand->ecc.strength = 8;
983 nand->ecc.size = SECTOR_BYTES;
984 nand->ecc.bytes = 14;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200985 nand->ecc.hwctl = omap_enable_hwecc_bch;
pekon gupta6bd91a82013-11-18 19:03:00 +0530986 nand->ecc.correct = omap_correct_data_bch;
Roger Quadrosaa418fb2022-12-20 12:21:56 +0200987 nand->ecc.calculate = omap_calculate_ecc_bch;
pekon gupta6bd91a82013-11-18 19:03:00 +0530988 nand->ecc.read_page = omap_read_page_bch;
989 /* 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 - ecclayout->eccbytes -
995 BADBLOCK_MARKER_LENGTH;
pekon gupta6bd91a82013-11-18 19:03:00 +0530996 break;
997#else
998 printf("nand: error: CONFIG_NAND_OMAP_ELM required for ECC\n");
999 return -EINVAL;
1000#endif
1001
pekon gupta046cf862014-06-02 17:14:42 +05301002 case OMAP_ECC_BCH16_CODE_HW:
1003#ifdef CONFIG_NAND_OMAP_ELM
1004 debug("nand: using OMAP_ECC_BCH16_CODE_HW\n");
1005 /* check ecc-scheme requirements before updating ecc info */
1006 if ((26 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
1007 printf("nand: error: insufficient OOB: require=%d\n", (
1008 (26 * eccsteps) + BADBLOCK_MARKER_LENGTH));
1009 return -EINVAL;
1010 }
1011 /* intialize ELM for ECC error detection */
1012 elm_init();
1013 /* populate ecc specific fields */
1014 nand->ecc.mode = NAND_ECC_HW;
1015 nand->ecc.size = SECTOR_BYTES;
1016 nand->ecc.bytes = 26;
1017 nand->ecc.strength = 16;
Roger Quadrosaa418fb2022-12-20 12:21:56 +02001018 nand->ecc.hwctl = omap_enable_hwecc_bch;
pekon gupta046cf862014-06-02 17:14:42 +05301019 nand->ecc.correct = omap_correct_data_bch;
Roger Quadrosaa418fb2022-12-20 12:21:56 +02001020 nand->ecc.calculate = omap_calculate_ecc_bch;
pekon gupta046cf862014-06-02 17:14:42 +05301021 nand->ecc.read_page = omap_read_page_bch;
1022 /* define ecc-layout */
1023 ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
1024 for (i = 0; i < ecclayout->eccbytes; i++)
1025 ecclayout->eccpos[i] = i + BADBLOCK_MARKER_LENGTH;
1026 ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
1027 ecclayout->oobfree[0].length = oobsize - nand->ecc.bytes -
1028 BADBLOCK_MARKER_LENGTH;
1029 break;
1030#else
1031 printf("nand: error: CONFIG_NAND_OMAP_ELM required for ECC\n");
1032 return -EINVAL;
1033#endif
pekon gupta6bd91a82013-11-18 19:03:00 +05301034 default:
1035 debug("nand: error: ecc scheme not enabled or supported\n");
1036 return -EINVAL;
1037 }
Nikita Kiryanove8167892013-12-16 19:19:01 +02001038
1039 /* nand_scan_tail() sets ham1 sw ecc; hw ecc layout is set by driver */
1040 if (ecc_scheme != OMAP_ECC_HAM1_CODE_SW)
1041 nand->ecc.layout = ecclayout;
1042
pekon guptaaa168482014-04-11 12:55:33 +05301043 info->ecc_scheme = ecc_scheme;
pekon gupta6bd91a82013-11-18 19:03:00 +05301044 return 0;
1045}
Andreas Bießmann82a65472013-04-05 04:55:21 +00001046
Simon Schwarz4f62e982011-09-14 15:30:16 -04001047#ifndef CONFIG_SPL_BUILD
Dirk Behme778933f2008-12-14 09:47:16 +01001048/*
Andreas Bießmann1e4eccf2013-04-04 23:52:50 +00001049 * omap_nand_switch_ecc - switch the ECC operation between different engines
1050 * (h/w and s/w) and different algorithms (hamming and BCHx)
Dirk Behme778933f2008-12-14 09:47:16 +01001051 *
Andreas Bießmann1e4eccf2013-04-04 23:52:50 +00001052 * @hardware - true if one of the HW engines should be used
1053 * @eccstrength - the number of bits that could be corrected
1054 * (1 - hamming, 4 - BCH4, 8 - BCH8, 16 - BCH16)
Dirk Behme778933f2008-12-14 09:47:16 +01001055 */
pekon gupta6bd91a82013-11-18 19:03:00 +05301056int __maybe_unused omap_nand_switch_ecc(uint32_t hardware, uint32_t eccstrength)
Dirk Behme778933f2008-12-14 09:47:16 +01001057{
1058 struct nand_chip *nand;
Mugunthan V N7b670cc2017-06-26 19:12:51 -05001059 struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
pekon gupta6bd91a82013-11-18 19:03:00 +05301060 int err = 0;
Dirk Behme778933f2008-12-14 09:47:16 +01001061
Mugunthan V N7b670cc2017-06-26 19:12:51 -05001062 if (!mtd) {
pekon gupta6bd91a82013-11-18 19:03:00 +05301063 printf("nand: error: no NAND devices found\n");
1064 return -ENODEV;
Dirk Behme778933f2008-12-14 09:47:16 +01001065 }
1066
Scott Wood17fed142016-05-30 13:57:56 -05001067 nand = mtd_to_nand(mtd);
Dirk Behme778933f2008-12-14 09:47:16 +01001068 nand->options |= NAND_OWN_BUFFERS;
Jeroen Hofstee96306f22014-01-15 17:58:54 +01001069 nand->options &= ~NAND_SUBPAGE_READ;
Dirk Behme778933f2008-12-14 09:47:16 +01001070 /* Setup the ecc configurations again */
Andreas Bießmann1e4eccf2013-04-04 23:52:50 +00001071 if (hardware) {
1072 if (eccstrength == 1) {
pekon gupta6bd91a82013-11-18 19:03:00 +05301073 err = omap_select_ecc_scheme(nand,
1074 OMAP_ECC_HAM1_CODE_HW,
1075 mtd->writesize, mtd->oobsize);
1076 } else if (eccstrength == 8) {
1077 err = omap_select_ecc_scheme(nand,
1078 OMAP_ECC_BCH8_CODE_HW,
1079 mtd->writesize, mtd->oobsize);
Heiko Schocher5bf904c2016-06-07 08:55:42 +02001080 } else if (eccstrength == 16) {
1081 err = omap_select_ecc_scheme(nand,
1082 OMAP_ECC_BCH16_CODE_HW,
1083 mtd->writesize, mtd->oobsize);
pekon gupta6bd91a82013-11-18 19:03:00 +05301084 } else {
1085 printf("nand: error: unsupported ECC scheme\n");
1086 return -EINVAL;
Andreas Bießmann1e4eccf2013-04-04 23:52:50 +00001087 }
Dirk Behme778933f2008-12-14 09:47:16 +01001088 } else {
Ash Charles4a5faa82015-02-18 11:25:11 -08001089 if (eccstrength == 1) {
1090 err = omap_select_ecc_scheme(nand,
1091 OMAP_ECC_HAM1_CODE_SW,
1092 mtd->writesize, mtd->oobsize);
1093 } else if (eccstrength == 8) {
1094 err = omap_select_ecc_scheme(nand,
1095 OMAP_ECC_BCH8_CODE_HW_DETECTION_SW,
pekon gupta6bd91a82013-11-18 19:03:00 +05301096 mtd->writesize, mtd->oobsize);
Ash Charles4a5faa82015-02-18 11:25:11 -08001097 } else {
1098 printf("nand: error: unsupported ECC scheme\n");
1099 return -EINVAL;
1100 }
Dirk Behme778933f2008-12-14 09:47:16 +01001101 }
1102
1103 /* Update NAND handling after ECC mode switch */
pekon gupta6bd91a82013-11-18 19:03:00 +05301104 if (!err)
1105 err = nand_scan_tail(mtd);
1106 return err;
Dirk Behme778933f2008-12-14 09:47:16 +01001107}
Simon Schwarz4f62e982011-09-14 15:30:16 -04001108#endif /* CONFIG_SPL_BUILD */
Dirk Behme778933f2008-12-14 09:47:16 +01001109
1110/*
1111 * Board-specific NAND initialization. The following members of the
1112 * argument are board-specific:
1113 * - IO_ADDR_R: address to read the 8 I/O lines of the flash device
1114 * - IO_ADDR_W: address to write the 8 I/O lines of the flash device
1115 * - cmd_ctrl: hardwarespecific function for accesing control-lines
1116 * - waitfunc: hardwarespecific function for accesing device ready/busy line
1117 * - ecc.hwctl: function to enable (reset) hardware ecc generator
1118 * - ecc.mode: mode of ecc, see defines
1119 * - chip_delay: chip dependent delay for transfering data from array to
1120 * read regs (tR)
1121 * - options: various chip options. They can partly be set to inform
1122 * nand_scan about special functionality. See the defines for further
1123 * explanation
1124 */
Roger Quadros80cf6372022-12-20 12:21:59 +02001125int gpmc_nand_init(struct nand_chip *nand)
Dirk Behme778933f2008-12-14 09:47:16 +01001126{
1127 int32_t gpmc_config = 0;
Rostislav Lisovy77cdf8a2014-09-02 16:23:58 +02001128 int cs = cs_next++;
pekon gupta6bd91a82013-11-18 19:03:00 +05301129 int err = 0;
Roger Quadros3b7aaa52022-10-11 14:50:02 +03001130 struct omap_nand_info *info;
1131
Dirk Behme778933f2008-12-14 09:47:16 +01001132 /*
1133 * xloader/Uboot's gpmc configuration would have configured GPMC for
1134 * nand type of memory. The following logic scans and latches on to the
1135 * first CS with NAND type memory.
1136 * TBD: need to make this logic generic to handle multiple CS NAND
1137 * devices.
1138 */
1139 while (cs < GPMC_MAX_CS) {
Dirk Behme778933f2008-12-14 09:47:16 +01001140 /* Check if NAND type is set */
Dirk Behmea4becd62009-08-08 09:30:22 +02001141 if ((readl(&gpmc_cfg->cs[cs].config1) & 0xC00) == 0x800) {
Dirk Behme778933f2008-12-14 09:47:16 +01001142 /* Found it!! */
1143 break;
1144 }
1145 cs++;
1146 }
1147 if (cs >= GPMC_MAX_CS) {
pekon gupta6bd91a82013-11-18 19:03:00 +05301148 printf("nand: error: Unable to find NAND settings in "
Dirk Behme778933f2008-12-14 09:47:16 +01001149 "GPMC Configuration - quitting\n");
1150 return -ENODEV;
1151 }
1152
Dirk Behmea4becd62009-08-08 09:30:22 +02001153 gpmc_config = readl(&gpmc_cfg->config);
Dirk Behme778933f2008-12-14 09:47:16 +01001154 /* Disable Write protect */
1155 gpmc_config |= 0x10;
Dirk Behmea4becd62009-08-08 09:30:22 +02001156 writel(gpmc_config, &gpmc_cfg->config);
Dirk Behme778933f2008-12-14 09:47:16 +01001157
Dirk Behmea4becd62009-08-08 09:30:22 +02001158 nand->IO_ADDR_R = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
1159 nand->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
Roger Quadros3b7aaa52022-10-11 14:50:02 +03001160
1161 info = &omap_nand_info[cs];
1162 info->control = NULL;
1163 info->cs = cs;
1164 info->ws = wscfg[cs];
Tom Rinia63bea92022-12-19 09:29:55 -05001165 info->fifo = (void __iomem *)CFG_SYS_NAND_BASE;
Scott Wood17fed142016-05-30 13:57:56 -05001166 nand_set_controller_data(nand, &omap_nand_info[cs]);
pekon gupta6bd91a82013-11-18 19:03:00 +05301167 nand->cmd_ctrl = omap_nand_hwcontrol;
1168 nand->options |= NAND_NO_PADDING | NAND_CACHEPRG;
Dirk Behme778933f2008-12-14 09:47:16 +01001169 nand->chip_delay = 100;
Roger Quadros224add22022-10-11 14:50:06 +03001170 nand->ecc.layout = kzalloc(sizeof(*nand->ecc.layout), GFP_KERNEL);
1171 if (!nand->ecc.layout)
1172 return -ENOMEM;
Mansoor Ahamede5612512012-11-06 13:06:33 +00001173
pekon gupta6250faf2014-05-06 00:46:19 +05301174 /* configure driver and controller based on NAND device bus-width */
1175 gpmc_config = readl(&gpmc_cfg->cs[cs].config1);
1176#if defined(CONFIG_SYS_NAND_BUSWIDTH_16BIT)
1177 nand->options |= NAND_BUSWIDTH_16;
1178 writel(gpmc_config | (0x1 << 12), &gpmc_cfg->cs[cs].config1);
1179#else
1180 nand->options &= ~NAND_BUSWIDTH_16;
1181 writel(gpmc_config & ~(0x1 << 12), &gpmc_cfg->cs[cs].config1);
1182#endif
pekon gupta6bd91a82013-11-18 19:03:00 +05301183 /* select ECC scheme */
pekon gupta3ef49732013-11-18 19:03:01 +05301184#if defined(CONFIG_NAND_OMAP_ECCSCHEME)
1185 err = omap_select_ecc_scheme(nand, CONFIG_NAND_OMAP_ECCSCHEME,
pekon gupta6bd91a82013-11-18 19:03:00 +05301186 CONFIG_SYS_NAND_PAGE_SIZE, CONFIG_SYS_NAND_OOBSIZE);
pekon gupta3ef49732013-11-18 19:03:01 +05301187#else
1188 /* pagesize and oobsize are not required to configure sw ecc-scheme */
pekon gupta6bd91a82013-11-18 19:03:00 +05301189 err = omap_select_ecc_scheme(nand, OMAP_ECC_HAM1_CODE_SW,
1190 0, 0);
Mansoor Ahamede5612512012-11-06 13:06:33 +00001191#endif
pekon gupta6bd91a82013-11-18 19:03:00 +05301192 if (err)
1193 return err;
Simon Schwarz4f62e982011-09-14 15:30:16 -04001194
Egli, Samuel8645fd92015-02-13 15:47:10 +01001195#ifdef CONFIG_NAND_OMAP_GPMC_PREFETCH
Jeroen Hofstee5e67ac72015-05-30 10:11:24 +02001196 nand->read_buf = omap_nand_read_prefetch;
Egli, Samuel8645fd92015-02-13 15:47:10 +01001197#else
Roger Quadros3b7aaa52022-10-11 14:50:02 +03001198 nand->read_buf = omap_nand_read_buf;
Simon Schwarz4f62e982011-09-14 15:30:16 -04001199#endif
Stefan Roesee05972f2014-11-13 03:43:39 +01001200
1201 nand->dev_ready = omap_dev_ready;
1202
Dirk Behme778933f2008-12-14 09:47:16 +01001203 return 0;
1204}
Roger Quadros80cf6372022-12-20 12:21:59 +02001205
1206/* First NAND chip for SPL use only */
1207static __maybe_unused struct nand_chip *nand_chip;
1208
1209#if CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)
1210
1211static int gpmc_nand_probe(struct udevice *dev)
1212{
1213 struct nand_chip *nand = dev_get_priv(dev);
1214 struct mtd_info *mtd = nand_to_mtd(nand);
1215 int ret;
1216
1217 gpmc_nand_init(nand);
1218
1219 ret = nand_scan(mtd, CONFIG_SYS_NAND_MAX_CHIPS);
1220 if (ret)
1221 return ret;
1222
1223 ret = nand_register(0, mtd);
1224 if (ret)
1225 return ret;
1226
1227 if (!nand_chip)
1228 nand_chip = nand;
1229
1230 return 0;
1231}
1232
1233static const struct udevice_id gpmc_nand_ids[] = {
1234 { .compatible = "ti,am64-nand" },
1235 { .compatible = "ti,omap2-nand" },
1236 { }
1237};
1238
1239U_BOOT_DRIVER(gpmc_nand) = {
1240 .name = "gpmc-nand",
1241 .id = UCLASS_MTD,
1242 .of_match = gpmc_nand_ids,
1243 .probe = gpmc_nand_probe,
1244 .priv_auto = sizeof(struct nand_chip),
1245};
1246
1247void board_nand_init(void)
1248{
1249 struct udevice *dev;
1250 int ret;
1251
1252 ret = uclass_get_device_by_driver(UCLASS_MTD,
1253 DM_DRIVER_GET(gpmc_nand), &dev);
1254 if (ret && ret != -ENODEV)
1255 pr_err("%s: Failed to get GPMC device: %d\n", __func__, ret);
1256}
1257
1258#else
1259
1260int board_nand_init(struct nand_chip *nand)
1261{
1262 return gpmc_nand_init(nand);
1263}
1264
1265#endif /* CONFIG_SYS_NAND_SELF_INIT */