blob: 7e9ccf787894ecc5e313e8aae98641a5e9e848fd [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>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090010#include <linux/errno.h>
Roger Quadros6550c622022-10-11 14:49:59 +030011
12#ifdef CONFIG_ARCH_OMAP2PLUS
Dirk Behme778933f2008-12-14 09:47:16 +010013#include <asm/arch/mem.h>
Roger Quadros6550c622022-10-11 14:49:59 +030014#endif
15
pekon gupta5bbb0992013-11-22 16:53:29 +053016#include <linux/mtd/omap_gpmc.h>
Dirk Behme778933f2008-12-14 09:47:16 +010017#include <linux/mtd/nand_ecc.h>
Tom Rini3bde7e22021-09-22 14:50:35 -040018#include <linux/mtd/rawnand.h>
Andreas Bießmann82a65472013-04-05 04:55:21 +000019#include <linux/bch.h>
Stefano Babicaade5792012-03-21 23:56:17 +000020#include <linux/compiler.h>
Dirk Behme778933f2008-12-14 09:47:16 +010021#include <nand.h>
pekon gupta7295fe82013-11-22 16:53:30 +053022#include <linux/mtd/omap_elm.h>
pekon gupta6bd91a82013-11-18 19:03:00 +053023
Roger Quadros6550c622022-10-11 14:49:59 +030024#ifndef GPMC_MAX_CS
25#define GPMC_MAX_CS 4
26#endif
27
pekon gupta6bd91a82013-11-18 19:03:00 +053028#define BADBLOCK_MARKER_LENGTH 2
29#define SECTOR_BYTES 512
pekon guptaeff10ee2013-11-19 11:02:15 +053030#define ECCCLEAR (0x1 << 8)
31#define ECCRESULTREG1 (0x1 << 0)
pekon guptab0f750a2013-11-19 11:02:17 +053032/* 4 bit padding to make byte aligned, 56 = 52 + 4 */
33#define BCH4_BIT_PAD 4
34
pekon gupta03742c92013-11-19 11:02:16 +053035#ifdef CONFIG_BCH
36static u8 bch8_polynomial[] = {0xef, 0x51, 0x2e, 0x09, 0xed, 0x93, 0x9a, 0xc2,
37 0x97, 0x79, 0xe5, 0x24, 0xb5};
38#endif
Rostislav Lisovy77cdf8a2014-09-02 16:23:58 +020039static uint8_t cs_next;
pekon gupta6bd91a82013-11-18 19:03:00 +053040static __maybe_unused struct nand_ecclayout omap_ecclayout;
Dirk Behme778933f2008-12-14 09:47:16 +010041
Michal Sojka26160072015-02-17 17:08:37 +010042#if defined(CONFIG_NAND_OMAP_GPMC_WSCFG)
43static const int8_t wscfg[CONFIG_SYS_MAX_NAND_DEVICE] =
44 { CONFIG_NAND_OMAP_GPMC_WSCFG };
45#else
46/* wscfg is preset to zero since its a static variable */
47static const int8_t wscfg[CONFIG_SYS_MAX_NAND_DEVICE];
48#endif
49
Dirk Behme778933f2008-12-14 09:47:16 +010050/*
Rostislav Lisovy77cdf8a2014-09-02 16:23:58 +020051 * Driver configurations
52 */
53struct omap_nand_info {
54 struct bch_control *control;
55 enum omap_ecc ecc_scheme;
Michal Sojka26160072015-02-17 17:08:37 +010056 uint8_t cs;
57 uint8_t ws; /* wait status pin (0,1) */
Rostislav Lisovy77cdf8a2014-09-02 16:23:58 +020058};
59
60/* We are wasting a bit of memory but al least we are safe */
61static struct omap_nand_info omap_nand_info[GPMC_MAX_CS];
62
63/*
Dirk Behme778933f2008-12-14 09:47:16 +010064 * omap_nand_hwcontrol - Set the address pointers corretly for the
65 * following address/data/command operation
66 */
67static void omap_nand_hwcontrol(struct mtd_info *mtd, int32_t cmd,
68 uint32_t ctrl)
69{
Scott Wood17fed142016-05-30 13:57:56 -050070 register struct nand_chip *this = mtd_to_nand(mtd);
71 struct omap_nand_info *info = nand_get_controller_data(this);
Rostislav Lisovy77cdf8a2014-09-02 16:23:58 +020072 int cs = info->cs;
Dirk Behme778933f2008-12-14 09:47:16 +010073
74 /*
75 * Point the IO_ADDR to DATA and ADDRESS registers instead
76 * of chip address
77 */
78 switch (ctrl) {
79 case NAND_CTRL_CHANGE | NAND_CTRL_CLE:
Dirk Behmea4becd62009-08-08 09:30:22 +020080 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
Dirk Behme778933f2008-12-14 09:47:16 +010081 break;
82 case NAND_CTRL_CHANGE | NAND_CTRL_ALE:
Dirk Behmea4becd62009-08-08 09:30:22 +020083 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_adr;
Dirk Behme778933f2008-12-14 09:47:16 +010084 break;
85 case NAND_CTRL_CHANGE | NAND_NCE:
Dirk Behmea4becd62009-08-08 09:30:22 +020086 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
Dirk Behme778933f2008-12-14 09:47:16 +010087 break;
88 }
89
90 if (cmd != NAND_CMD_NONE)
91 writeb(cmd, this->IO_ADDR_W);
92}
93
Simon Schwarz4f62e982011-09-14 15:30:16 -040094/* Check wait pin as dev ready indicator */
Stefan Roesee05972f2014-11-13 03:43:39 +010095static int omap_dev_ready(struct mtd_info *mtd)
Simon Schwarz4f62e982011-09-14 15:30:16 -040096{
Scott Wood17fed142016-05-30 13:57:56 -050097 register struct nand_chip *this = mtd_to_nand(mtd);
98 struct omap_nand_info *info = nand_get_controller_data(this);
Michal Sojka26160072015-02-17 17:08:37 +010099 return gpmc_cfg->status & (1 << (8 + info->ws));
Simon Schwarz4f62e982011-09-14 15:30:16 -0400100}
Dirk Behme778933f2008-12-14 09:47:16 +0100101
102/*
103 * gen_true_ecc - This function will generate true ECC value, which
104 * can be used when correcting data read from NAND flash memory core
105 *
106 * @ecc_buf: buffer to store ecc code
107 *
108 * @return: re-formatted ECC value
109 */
110static uint32_t gen_true_ecc(uint8_t *ecc_buf)
111{
112 return ecc_buf[0] | (ecc_buf[1] << 16) | ((ecc_buf[2] & 0xF0) << 20) |
113 ((ecc_buf[2] & 0x0F) << 8);
114}
115
116/*
117 * omap_correct_data - Compares the ecc read from nand spare area with ECC
Vagrant Cascadianedfdb992016-04-30 19:18:00 -0700118 * registers values and corrects one bit error if it has occurred
Dirk Behme778933f2008-12-14 09:47:16 +0100119 * Further details can be had from OMAP TRM and the following selected links:
120 * http://en.wikipedia.org/wiki/Hamming_code
121 * http://www.cs.utexas.edu/users/plaxton/c/337/05f/slides/ErrorCorrection-4.pdf
122 *
123 * @mtd: MTD device structure
124 * @dat: page data
125 * @read_ecc: ecc read from nand flash
126 * @calc_ecc: ecc read from ECC registers
127 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100128 * Return: 0 if data is OK or corrected, else returns -1
Dirk Behme778933f2008-12-14 09:47:16 +0100129 */
Stefano Babicaade5792012-03-21 23:56:17 +0000130static int __maybe_unused omap_correct_data(struct mtd_info *mtd, uint8_t *dat,
Dirk Behme778933f2008-12-14 09:47:16 +0100131 uint8_t *read_ecc, uint8_t *calc_ecc)
132{
133 uint32_t orig_ecc, new_ecc, res, hm;
134 uint16_t parity_bits, byte;
135 uint8_t bit;
136
137 /* Regenerate the orginal ECC */
138 orig_ecc = gen_true_ecc(read_ecc);
139 new_ecc = gen_true_ecc(calc_ecc);
140 /* Get the XOR of real ecc */
141 res = orig_ecc ^ new_ecc;
142 if (res) {
143 /* Get the hamming width */
144 hm = hweight32(res);
145 /* Single bit errors can be corrected! */
146 if (hm == 12) {
147 /* Correctable data! */
148 parity_bits = res >> 16;
149 bit = (parity_bits & 0x7);
150 byte = (parity_bits >> 3) & 0x1FF;
151 /* Flip the bit to correct */
152 dat[byte] ^= (0x1 << bit);
153 } else if (hm == 1) {
154 printf("Error: Ecc is wrong\n");
155 /* ECC itself is corrupted */
156 return 2;
157 } else {
158 /*
159 * hm distance != parity pairs OR one, could mean 2 bit
160 * error OR potentially be on a blank page..
161 * orig_ecc: contains spare area data from nand flash.
162 * new_ecc: generated ecc while reading data area.
163 * Note: if the ecc = 0, all data bits from which it was
164 * generated are 0xFF.
165 * The 3 byte(24 bits) ecc is generated per 512byte
166 * chunk of a page. If orig_ecc(from spare area)
167 * is 0xFF && new_ecc(computed now from data area)=0x0,
168 * this means that data area is 0xFF and spare area is
169 * 0xFF. A sure sign of a erased page!
170 */
171 if ((orig_ecc == 0x0FFF0FFF) && (new_ecc == 0x00000000))
172 return 0;
173 printf("Error: Bad compare! failed\n");
174 /* detected 2 bit error */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500175 return -EBADMSG;
Dirk Behme778933f2008-12-14 09:47:16 +0100176 }
177 }
178 return 0;
179}
180
181/*
pekon guptaeff10ee2013-11-19 11:02:15 +0530182 * omap_enable_hwecc - configures GPMC as per ECC scheme before read/write
Andreas Bießmann82a65472013-04-05 04:55:21 +0000183 * @mtd: MTD device structure
184 * @mode: Read/Write mode
185 */
186__maybe_unused
pekon guptaeff10ee2013-11-19 11:02:15 +0530187static void omap_enable_hwecc(struct mtd_info *mtd, int32_t mode)
Andreas Bießmann82a65472013-04-05 04:55:21 +0000188{
Scott Wood17fed142016-05-30 13:57:56 -0500189 struct nand_chip *nand = mtd_to_nand(mtd);
190 struct omap_nand_info *info = nand_get_controller_data(nand);
pekon guptaeff10ee2013-11-19 11:02:15 +0530191 unsigned int dev_width = (nand->options & NAND_BUSWIDTH_16) ? 1 : 0;
192 unsigned int ecc_algo = 0;
193 unsigned int bch_type = 0;
194 unsigned int eccsize1 = 0x00, eccsize0 = 0x00, bch_wrapmode = 0x00;
195 u32 ecc_size_config_val = 0;
196 u32 ecc_config_val = 0;
Rostislav Lisovy77cdf8a2014-09-02 16:23:58 +0200197 int cs = info->cs;
Andreas Bießmann82a65472013-04-05 04:55:21 +0000198
pekon guptaeff10ee2013-11-19 11:02:15 +0530199 /* configure GPMC for specific ecc-scheme */
pekon guptaaa168482014-04-11 12:55:33 +0530200 switch (info->ecc_scheme) {
pekon guptaeff10ee2013-11-19 11:02:15 +0530201 case OMAP_ECC_HAM1_CODE_SW:
202 return;
203 case OMAP_ECC_HAM1_CODE_HW:
204 ecc_algo = 0x0;
205 bch_type = 0x0;
206 bch_wrapmode = 0x00;
207 eccsize0 = 0xFF;
208 eccsize1 = 0xFF;
209 break;
210 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
211 case OMAP_ECC_BCH8_CODE_HW:
212 ecc_algo = 0x1;
213 bch_type = 0x1;
214 if (mode == NAND_ECC_WRITE) {
215 bch_wrapmode = 0x01;
216 eccsize0 = 0; /* extra bits in nibbles per sector */
217 eccsize1 = 28; /* OOB bits in nibbles per sector */
218 } else {
219 bch_wrapmode = 0x01;
220 eccsize0 = 26; /* ECC bits in nibbles per sector */
221 eccsize1 = 2; /* non-ECC bits in nibbles per sector */
Stefan Roese7a12cc62013-12-05 07:58:06 +0100222 }
pekon guptaeff10ee2013-11-19 11:02:15 +0530223 break;
pekon gupta046cf862014-06-02 17:14:42 +0530224 case OMAP_ECC_BCH16_CODE_HW:
225 ecc_algo = 0x1;
226 bch_type = 0x2;
227 if (mode == NAND_ECC_WRITE) {
228 bch_wrapmode = 0x01;
229 eccsize0 = 0; /* extra bits in nibbles per sector */
230 eccsize1 = 52; /* OOB bits in nibbles per sector */
231 } else {
232 bch_wrapmode = 0x01;
233 eccsize0 = 52; /* ECC bits in nibbles per sector */
234 eccsize1 = 0; /* non-ECC bits in nibbles per sector */
235 }
236 break;
pekon guptaeff10ee2013-11-19 11:02:15 +0530237 default:
238 return;
pekon gupta6bd91a82013-11-18 19:03:00 +0530239 }
pekon guptaeff10ee2013-11-19 11:02:15 +0530240 /* Clear ecc and enable bits */
241 writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
242 /* Configure ecc size for BCH */
243 ecc_size_config_val = (eccsize1 << 22) | (eccsize0 << 12);
244 writel(ecc_size_config_val, &gpmc_cfg->ecc_size_config);
Andreas Bießmann82a65472013-04-05 04:55:21 +0000245
pekon guptaeff10ee2013-11-19 11:02:15 +0530246 /* Configure device details for BCH engine */
247 ecc_config_val = ((ecc_algo << 16) | /* HAM1 | BCHx */
248 (bch_type << 12) | /* BCH4/BCH8/BCH16 */
249 (bch_wrapmode << 8) | /* wrap mode */
250 (dev_width << 7) | /* bus width */
251 (0x0 << 4) | /* number of sectors */
252 (cs << 1) | /* ECC CS */
253 (0x1)); /* enable ECC */
254 writel(ecc_config_val, &gpmc_cfg->ecc_config);
Andreas Bießmann82a65472013-04-05 04:55:21 +0000255}
256
257/*
pekon gupta03742c92013-11-19 11:02:16 +0530258 * omap_calculate_ecc - Read ECC result
259 * @mtd: MTD structure
260 * @dat: unused
261 * @ecc_code: ecc_code buffer
262 * Using noninverted ECC can be considered ugly since writing a blank
263 * page ie. padding will clear the ECC bytes. This is no problem as
264 * long nobody is trying to write data on the seemingly unused page.
265 * Reading an erased page will produce an ECC mismatch between
266 * generated and read ECC bytes that has to be dealt with separately.
267 * E.g. if page is 0xFF (fresh erased), and if HW ECC engine within GPMC
268 * is used, the result of read will be 0x0 while the ECC offsets of the
269 * spare area will be 0xFF which will result in an ECC mismatch.
Mansoor Ahamede5612512012-11-06 13:06:33 +0000270 */
pekon gupta03742c92013-11-19 11:02:16 +0530271static int omap_calculate_ecc(struct mtd_info *mtd, const uint8_t *dat,
Mansoor Ahamede5612512012-11-06 13:06:33 +0000272 uint8_t *ecc_code)
273{
Scott Wood17fed142016-05-30 13:57:56 -0500274 struct nand_chip *chip = mtd_to_nand(mtd);
275 struct omap_nand_info *info = nand_get_controller_data(chip);
Ladislav Michld5b1c272016-07-12 20:28:16 +0200276 const uint32_t *ptr;
277 uint32_t val = 0;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000278 int8_t i = 0, j;
279
pekon guptaaa168482014-04-11 12:55:33 +0530280 switch (info->ecc_scheme) {
pekon gupta03742c92013-11-19 11:02:16 +0530281 case OMAP_ECC_HAM1_CODE_HW:
282 val = readl(&gpmc_cfg->ecc1_result);
283 ecc_code[0] = val & 0xFF;
284 ecc_code[1] = (val >> 16) & 0xFF;
285 ecc_code[2] = ((val >> 8) & 0x0F) | ((val >> 20) & 0xF0);
286 break;
287#ifdef CONFIG_BCH
288 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
289#endif
290 case OMAP_ECC_BCH8_CODE_HW:
Mansoor Ahamede5612512012-11-06 13:06:33 +0000291 ptr = &gpmc_cfg->bch_result_0_3[0].bch_result_x[3];
pekon gupta03742c92013-11-19 11:02:16 +0530292 val = readl(ptr);
293 ecc_code[i++] = (val >> 0) & 0xFF;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000294 ptr--;
295 for (j = 0; j < 3; j++) {
pekon gupta03742c92013-11-19 11:02:16 +0530296 val = readl(ptr);
297 ecc_code[i++] = (val >> 24) & 0xFF;
298 ecc_code[i++] = (val >> 16) & 0xFF;
299 ecc_code[i++] = (val >> 8) & 0xFF;
300 ecc_code[i++] = (val >> 0) & 0xFF;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000301 ptr--;
302 }
pekon gupta03742c92013-11-19 11:02:16 +0530303 break;
pekon gupta046cf862014-06-02 17:14:42 +0530304 case OMAP_ECC_BCH16_CODE_HW:
305 val = readl(&gpmc_cfg->bch_result_4_6[0].bch_result_x[2]);
306 ecc_code[i++] = (val >> 8) & 0xFF;
307 ecc_code[i++] = (val >> 0) & 0xFF;
308 val = readl(&gpmc_cfg->bch_result_4_6[0].bch_result_x[1]);
309 ecc_code[i++] = (val >> 24) & 0xFF;
310 ecc_code[i++] = (val >> 16) & 0xFF;
311 ecc_code[i++] = (val >> 8) & 0xFF;
312 ecc_code[i++] = (val >> 0) & 0xFF;
313 val = readl(&gpmc_cfg->bch_result_4_6[0].bch_result_x[0]);
314 ecc_code[i++] = (val >> 24) & 0xFF;
315 ecc_code[i++] = (val >> 16) & 0xFF;
316 ecc_code[i++] = (val >> 8) & 0xFF;
317 ecc_code[i++] = (val >> 0) & 0xFF;
318 for (j = 3; j >= 0; j--) {
319 val = readl(&gpmc_cfg->bch_result_0_3[0].bch_result_x[j]
320 );
321 ecc_code[i++] = (val >> 24) & 0xFF;
322 ecc_code[i++] = (val >> 16) & 0xFF;
323 ecc_code[i++] = (val >> 8) & 0xFF;
324 ecc_code[i++] = (val >> 0) & 0xFF;
325 }
326 break;
pekon gupta03742c92013-11-19 11:02:16 +0530327 default:
328 return -EINVAL;
329 }
330 /* ECC scheme specific syndrome customizations */
pekon guptaaa168482014-04-11 12:55:33 +0530331 switch (info->ecc_scheme) {
pekon gupta03742c92013-11-19 11:02:16 +0530332 case OMAP_ECC_HAM1_CODE_HW:
333 break;
334#ifdef CONFIG_BCH
335 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
336
337 for (i = 0; i < chip->ecc.bytes; i++)
338 *(ecc_code + i) = *(ecc_code + i) ^
339 bch8_polynomial[i];
340 break;
341#endif
342 case OMAP_ECC_BCH8_CODE_HW:
343 ecc_code[chip->ecc.bytes - 1] = 0x00;
344 break;
pekon gupta046cf862014-06-02 17:14:42 +0530345 case OMAP_ECC_BCH16_CODE_HW:
346 break;
pekon gupta03742c92013-11-19 11:02:16 +0530347 default:
348 return -EINVAL;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000349 }
pekon gupta03742c92013-11-19 11:02:16 +0530350 return 0;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000351}
352
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200353#ifdef CONFIG_NAND_OMAP_GPMC_PREFETCH
354
355#define PREFETCH_CONFIG1_CS_SHIFT 24
356#define PREFETCH_FIFOTHRESHOLD_MAX 0x40
357#define PREFETCH_FIFOTHRESHOLD(val) ((val) << 8)
358#define PREFETCH_STATUS_COUNT(val) (val & 0x00003fff)
359#define PREFETCH_STATUS_FIFO_CNT(val) ((val >> 24) & 0x7F)
360#define ENABLE_PREFETCH (1 << 7)
361
362/**
363 * omap_prefetch_enable - configures and starts prefetch transfer
364 * @fifo_th: fifo threshold to be used for read/ write
365 * @count: number of bytes to be transferred
366 * @is_write: prefetch read(0) or write post(1) mode
367 * @cs: chip select to use
368 */
369static int omap_prefetch_enable(int fifo_th, unsigned int count, int is_write, int cs)
370{
371 uint32_t val;
372
373 if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX)
374 return -EINVAL;
375
376 if (readl(&gpmc_cfg->prefetch_control))
377 return -EBUSY;
378
379 /* Set the amount of bytes to be prefetched */
380 writel(count, &gpmc_cfg->prefetch_config2);
381
382 val = (cs << PREFETCH_CONFIG1_CS_SHIFT) | (is_write & 1) |
383 PREFETCH_FIFOTHRESHOLD(fifo_th) | ENABLE_PREFETCH;
384 writel(val, &gpmc_cfg->prefetch_config1);
385
386 /* Start the prefetch engine */
387 writel(1, &gpmc_cfg->prefetch_control);
388
389 return 0;
390}
391
392/**
393 * omap_prefetch_reset - disables and stops the prefetch engine
394 */
395static void omap_prefetch_reset(void)
396{
397 writel(0, &gpmc_cfg->prefetch_control);
398 writel(0, &gpmc_cfg->prefetch_config1);
399}
400
401static int __read_prefetch_aligned(struct nand_chip *chip, uint32_t *buf, int len)
402{
403 int ret;
404 uint32_t cnt;
Scott Wood17fed142016-05-30 13:57:56 -0500405 struct omap_nand_info *info = nand_get_controller_data(chip);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200406
407 ret = omap_prefetch_enable(PREFETCH_FIFOTHRESHOLD_MAX, len, 0, info->cs);
408 if (ret < 0)
409 return ret;
410
411 do {
412 int i;
413
414 cnt = readl(&gpmc_cfg->prefetch_status);
415 cnt = PREFETCH_STATUS_FIFO_CNT(cnt);
416
417 for (i = 0; i < cnt / 4; i++) {
418 *buf++ = readl(CONFIG_SYS_NAND_BASE);
419 len -= 4;
420 }
421 } while (len);
422
423 omap_prefetch_reset();
424
425 return 0;
426}
427
Jeroen Hofstee5e67ac72015-05-30 10:11:24 +0200428static inline void omap_nand_read(struct mtd_info *mtd, uint8_t *buf, int len)
429{
Scott Wood17fed142016-05-30 13:57:56 -0500430 struct nand_chip *chip = mtd_to_nand(mtd);
Jeroen Hofstee5e67ac72015-05-30 10:11:24 +0200431
432 if (chip->options & NAND_BUSWIDTH_16)
433 nand_read_buf16(mtd, buf, len);
434 else
435 nand_read_buf(mtd, buf, len);
436}
437
438static void omap_nand_read_prefetch(struct mtd_info *mtd, uint8_t *buf, int len)
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200439{
440 int ret;
441 uint32_t head, tail;
Scott Wood17fed142016-05-30 13:57:56 -0500442 struct nand_chip *chip = mtd_to_nand(mtd);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200443
444 /*
445 * If the destination buffer is unaligned, start with reading
446 * the overlap byte-wise.
447 */
448 head = ((uint32_t) buf) % 4;
449 if (head) {
Jeroen Hofstee5e67ac72015-05-30 10:11:24 +0200450 omap_nand_read(mtd, buf, head);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200451 buf += head;
452 len -= head;
453 }
454
455 /*
456 * Only transfer multiples of 4 bytes in a pre-fetched fashion.
457 * If there's a residue, care for it byte-wise afterwards.
458 */
459 tail = len % 4;
460
Jeroen Hofstee5e67ac72015-05-30 10:11:24 +0200461 ret = __read_prefetch_aligned(chip, (uint32_t *)buf, len - tail);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200462 if (ret < 0) {
463 /* fallback in case the prefetch engine is busy */
Jeroen Hofstee5e67ac72015-05-30 10:11:24 +0200464 omap_nand_read(mtd, buf, len);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200465 } else if (tail) {
466 buf += len - tail;
Jeroen Hofstee5e67ac72015-05-30 10:11:24 +0200467 omap_nand_read(mtd, buf, tail);
Jeroen Hofsteef2bf5712015-05-30 10:11:23 +0200468 }
469}
470#endif /* CONFIG_NAND_OMAP_GPMC_PREFETCH */
471
pekon gupta03742c92013-11-19 11:02:16 +0530472#ifdef CONFIG_NAND_OMAP_ELM
Mansoor Ahamede5612512012-11-06 13:06:33 +0000473/*
Jeroen Hofstee79369072014-10-08 22:57:42 +0200474 * omap_reverse_list - re-orders list elements in reverse order [internal]
475 * @list: pointer to start of list
476 * @length: length of list
477*/
478static void omap_reverse_list(u8 *list, unsigned int length)
479{
480 unsigned int i, j;
481 unsigned int half_length = length / 2;
482 u8 tmp;
483 for (i = 0, j = length - 1; i < half_length; i++, j--) {
484 tmp = list[i];
485 list[i] = list[j];
486 list[j] = tmp;
487 }
488}
489
490/*
Mansoor Ahamede5612512012-11-06 13:06:33 +0000491 * omap_correct_data_bch - Compares the ecc read from nand spare area
Vagrant Cascadianedfdb992016-04-30 19:18:00 -0700492 * with ECC registers values and corrects one bit error if it has occurred
Mansoor Ahamede5612512012-11-06 13:06:33 +0000493 *
494 * @mtd: MTD device structure
495 * @dat: page data
496 * @read_ecc: ecc read from nand flash (ignored)
497 * @calc_ecc: ecc read from ECC registers
498 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100499 * Return: 0 if data is OK or corrected, else returns -1
Mansoor Ahamede5612512012-11-06 13:06:33 +0000500 */
501static int omap_correct_data_bch(struct mtd_info *mtd, uint8_t *dat,
502 uint8_t *read_ecc, uint8_t *calc_ecc)
503{
Scott Wood17fed142016-05-30 13:57:56 -0500504 struct nand_chip *chip = mtd_to_nand(mtd);
505 struct omap_nand_info *info = nand_get_controller_data(chip);
pekon gupta3c43c5b2014-04-11 12:55:34 +0530506 struct nand_ecc_ctrl *ecc = &chip->ecc;
pekon guptab0f750a2013-11-19 11:02:17 +0530507 uint32_t error_count = 0, error_max;
pekon gupta046cf862014-06-02 17:14:42 +0530508 uint32_t error_loc[ELM_MAX_ERROR_COUNT];
pekon gupta9d4b7472014-04-11 12:55:32 +0530509 enum bch_level bch_type;
pekon guptab0f750a2013-11-19 11:02:17 +0530510 uint32_t i, ecc_flag = 0;
Guido Martínez20b27be2015-01-02 14:49:10 -0300511 uint8_t count;
pekon guptab0f750a2013-11-19 11:02:17 +0530512 uint32_t byte_pos, bit_pos;
Guido Martínez20b27be2015-01-02 14:49:10 -0300513 int err = 0;
pekon guptab0f750a2013-11-19 11:02:17 +0530514
515 /* check calculated ecc */
pekon gupta3c43c5b2014-04-11 12:55:34 +0530516 for (i = 0; i < ecc->bytes && !ecc_flag; i++) {
pekon guptab0f750a2013-11-19 11:02:17 +0530517 if (calc_ecc[i] != 0x00)
David Rivshinefa78532021-11-18 13:25:24 -0500518 goto not_ecc_match;
pekon guptab0f750a2013-11-19 11:02:17 +0530519 }
David Rivshinefa78532021-11-18 13:25:24 -0500520 return 0;
521not_ecc_match:
Mansoor Ahamede5612512012-11-06 13:06:33 +0000522
David Rivshinefa78532021-11-18 13:25:24 -0500523 /* check for whether it's an erased-page */
524 for (i = 0; i < ecc->bytes; i++) {
Mansoor Ahamede5612512012-11-06 13:06:33 +0000525 if (read_ecc[i] != 0xff)
David Rivshinefa78532021-11-18 13:25:24 -0500526 goto not_erased;
pekon guptab0f750a2013-11-19 11:02:17 +0530527 }
David Rivshinefa78532021-11-18 13:25:24 -0500528 for (i = 0; i < SECTOR_BYTES; i++) {
529 if (dat[i] != 0xff)
530 goto not_erased;
531 }
532 return 0;
533not_erased:
534
535 /*
536 * Check for whether it's an erased page with a correctable
537 * number of bitflips. Erased pages have all 1's in the data,
538 * so we just compute the number of 0 bits in the data and
539 * see if it's under the correction threshold.
540 *
541 * NOTE: The check for a perfect erased page above is faster for
542 * the more common case, even though it's logically redundant.
543 */
544 for (i = 0; i < ecc->bytes; i++)
545 error_count += hweight8(~read_ecc[i]);
546
547 for (i = 0; i < SECTOR_BYTES; i++)
548 error_count += hweight8(~dat[i]);
549
550 if (error_count <= ecc->strength) {
551 memset(read_ecc, 0xFF, ecc->bytes);
552 memset(dat, 0xFF, SECTOR_BYTES);
553 debug("nand: %u bit-flip(s) corrected in erased page\n",
554 error_count);
555 return error_count;
556 }
Mansoor Ahamede5612512012-11-06 13:06:33 +0000557
Mansoor Ahamede5612512012-11-06 13:06:33 +0000558 /*
559 * while reading ECC result we read it in big endian.
560 * Hence while loading to ELM we have rotate to get the right endian.
561 */
pekon guptaaa168482014-04-11 12:55:33 +0530562 switch (info->ecc_scheme) {
pekon guptab0f750a2013-11-19 11:02:17 +0530563 case OMAP_ECC_BCH8_CODE_HW:
pekon gupta9d4b7472014-04-11 12:55:32 +0530564 bch_type = BCH_8_BIT;
pekon gupta3c43c5b2014-04-11 12:55:34 +0530565 omap_reverse_list(calc_ecc, ecc->bytes - 1);
pekon guptab0f750a2013-11-19 11:02:17 +0530566 break;
pekon gupta046cf862014-06-02 17:14:42 +0530567 case OMAP_ECC_BCH16_CODE_HW:
568 bch_type = BCH_16_BIT;
569 omap_reverse_list(calc_ecc, ecc->bytes);
570 break;
pekon guptab0f750a2013-11-19 11:02:17 +0530571 default:
572 return -EINVAL;
573 }
Mansoor Ahamede5612512012-11-06 13:06:33 +0000574 /* use elm module to check for errors */
pekon gupta9d4b7472014-04-11 12:55:32 +0530575 elm_config(bch_type);
David Rivshinefa78532021-11-18 13:25:24 -0500576 error_count = 0;
pekon guptacfe6b8a2014-04-11 12:55:35 +0530577 err = elm_check_error(calc_ecc, bch_type, &error_count, error_loc);
578 if (err)
579 return err;
580
Mansoor Ahamede5612512012-11-06 13:06:33 +0000581 /* correct bch error */
pekon guptab0f750a2013-11-19 11:02:17 +0530582 for (count = 0; count < error_count; count++) {
pekon guptaaa168482014-04-11 12:55:33 +0530583 switch (info->ecc_scheme) {
pekon gupta9d4b7472014-04-11 12:55:32 +0530584 case OMAP_ECC_BCH8_CODE_HW:
pekon guptab0f750a2013-11-19 11:02:17 +0530585 /* 14th byte in ECC is reserved to match ROM layout */
pekon gupta3c43c5b2014-04-11 12:55:34 +0530586 error_max = SECTOR_BYTES + (ecc->bytes - 1);
pekon guptab0f750a2013-11-19 11:02:17 +0530587 break;
pekon gupta046cf862014-06-02 17:14:42 +0530588 case OMAP_ECC_BCH16_CODE_HW:
589 error_max = SECTOR_BYTES + ecc->bytes;
590 break;
pekon guptab0f750a2013-11-19 11:02:17 +0530591 default:
592 return -EINVAL;
593 }
594 byte_pos = error_max - (error_loc[count] / 8) - 1;
595 bit_pos = error_loc[count] % 8;
596 if (byte_pos < SECTOR_BYTES) {
597 dat[byte_pos] ^= 1 << bit_pos;
Ezequiel García69cf8ad2015-10-04 18:34:42 -0300598 debug("nand: bit-flip corrected @data=%d\n", byte_pos);
pekon guptab0f750a2013-11-19 11:02:17 +0530599 } else if (byte_pos < error_max) {
Belisko Marek9ab54142014-04-25 12:00:07 +0200600 read_ecc[byte_pos - SECTOR_BYTES] ^= 1 << bit_pos;
Ezequiel García69cf8ad2015-10-04 18:34:42 -0300601 debug("nand: bit-flip corrected @oob=%d\n", byte_pos -
pekon guptab0f750a2013-11-19 11:02:17 +0530602 SECTOR_BYTES);
603 } else {
604 err = -EBADMSG;
605 printf("nand: error: invalid bit-flip location\n");
606 }
607 }
608 return (err) ? err : error_count;
Mansoor Ahamede5612512012-11-06 13:06:33 +0000609}
Mansoor Ahamede5612512012-11-06 13:06:33 +0000610
611/**
612 * omap_read_page_bch - hardware ecc based page read function
613 * @mtd: mtd info structure
614 * @chip: nand chip info structure
615 * @buf: buffer to store read data
Sergey Lapin3a38a552013-01-14 03:46:50 +0000616 * @oob_required: caller expects OOB data read to chip->oob_poi
Mansoor Ahamede5612512012-11-06 13:06:33 +0000617 * @page: page number to read
618 *
619 */
620static int omap_read_page_bch(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000621 uint8_t *buf, int oob_required, int page)
Mansoor Ahamede5612512012-11-06 13:06:33 +0000622{
623 int i, eccsize = chip->ecc.size;
624 int eccbytes = chip->ecc.bytes;
625 int eccsteps = chip->ecc.steps;
626 uint8_t *p = buf;
627 uint8_t *ecc_calc = chip->buffers->ecccalc;
628 uint8_t *ecc_code = chip->buffers->ecccode;
629 uint32_t *eccpos = chip->ecc.layout->eccpos;
630 uint8_t *oob = chip->oob_poi;
631 uint32_t data_pos;
632 uint32_t oob_pos;
633
634 data_pos = 0;
635 /* oob area start */
636 oob_pos = (eccsize * eccsteps) + chip->ecc.layout->eccpos[0];
637 oob += chip->ecc.layout->eccpos[0];
638
639 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize,
640 oob += eccbytes) {
641 chip->ecc.hwctl(mtd, NAND_ECC_READ);
642 /* read data */
Rostislav Lisovya9ee70a2014-09-02 17:00:30 +0200643 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_pos, -1);
Mansoor Ahamede5612512012-11-06 13:06:33 +0000644 chip->read_buf(mtd, p, eccsize);
645
646 /* read respective ecc from oob area */
Rostislav Lisovya9ee70a2014-09-02 17:00:30 +0200647 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_pos, -1);
Mansoor Ahamede5612512012-11-06 13:06:33 +0000648 chip->read_buf(mtd, oob, eccbytes);
649 /* read syndrome */
650 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
651
652 data_pos += eccsize;
653 oob_pos += eccbytes;
654 }
655
656 for (i = 0; i < chip->ecc.total; i++)
657 ecc_code[i] = chip->oob_poi[eccpos[i]];
658
659 eccsteps = chip->ecc.steps;
660 p = buf;
661
662 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
663 int stat;
664
665 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
666 if (stat < 0)
667 mtd->ecc_stats.failed++;
668 else
669 mtd->ecc_stats.corrected += stat;
670 }
671 return 0;
672}
pekon gupta6bd91a82013-11-18 19:03:00 +0530673#endif /* CONFIG_NAND_OMAP_ELM */
Mansoor Ahamede5612512012-11-06 13:06:33 +0000674
Andreas Bießmann82a65472013-04-05 04:55:21 +0000675/*
676 * OMAP3 BCH8 support (with BCH library)
677 */
pekon gupta6bd91a82013-11-18 19:03:00 +0530678#ifdef CONFIG_BCH
Andreas Bießmann82a65472013-04-05 04:55:21 +0000679/**
pekon gupta6bd91a82013-11-18 19:03:00 +0530680 * omap_correct_data_bch_sw - Decode received data and correct errors
Andreas Bießmann82a65472013-04-05 04:55:21 +0000681 * @mtd: MTD device structure
682 * @data: page data
683 * @read_ecc: ecc read from nand flash
684 * @calc_ecc: ecc read from HW ECC registers
685 */
pekon gupta6bd91a82013-11-18 19:03:00 +0530686static int omap_correct_data_bch_sw(struct mtd_info *mtd, u_char *data,
Andreas Bießmann82a65472013-04-05 04:55:21 +0000687 u_char *read_ecc, u_char *calc_ecc)
688{
689 int i, count;
690 /* cannot correct more than 8 errors */
691 unsigned int errloc[8];
Scott Wood17fed142016-05-30 13:57:56 -0500692 struct nand_chip *chip = mtd_to_nand(mtd);
693 struct omap_nand_info *info = nand_get_controller_data(chip);
Andreas Bießmann82a65472013-04-05 04:55:21 +0000694
Ladislav Michl6cc51852017-01-09 11:15:14 +0100695 count = decode_bch(info->control, NULL, SECTOR_BYTES,
696 read_ecc, calc_ecc, NULL, errloc);
Andreas Bießmann82a65472013-04-05 04:55:21 +0000697 if (count > 0) {
698 /* correct errors */
699 for (i = 0; i < count; i++) {
700 /* correct data only, not ecc bytes */
Ladislav Michl6cc51852017-01-09 11:15:14 +0100701 if (errloc[i] < SECTOR_BYTES << 3)
702 data[errloc[i] >> 3] ^= 1 << (errloc[i] & 7);
Ezequiel García69cf8ad2015-10-04 18:34:42 -0300703 debug("corrected bitflip %u\n", errloc[i]);
Andreas Bießmann82a65472013-04-05 04:55:21 +0000704#ifdef DEBUG
705 puts("read_ecc: ");
706 /*
707 * BCH8 have 13 bytes of ECC; BCH4 needs adoption
708 * here!
709 */
710 for (i = 0; i < 13; i++)
711 printf("%02x ", read_ecc[i]);
712 puts("\n");
713 puts("calc_ecc: ");
714 for (i = 0; i < 13; i++)
715 printf("%02x ", calc_ecc[i]);
716 puts("\n");
717#endif
718 }
719 } else if (count < 0) {
720 puts("ecc unrecoverable error\n");
721 }
722 return count;
723}
724
725/**
726 * omap_free_bch - Release BCH ecc resources
727 * @mtd: MTD device structure
728 */
729static void __maybe_unused omap_free_bch(struct mtd_info *mtd)
730{
Scott Wood17fed142016-05-30 13:57:56 -0500731 struct nand_chip *chip = mtd_to_nand(mtd);
732 struct omap_nand_info *info = nand_get_controller_data(chip);
Andreas Bießmann82a65472013-04-05 04:55:21 +0000733
pekon guptaaa168482014-04-11 12:55:33 +0530734 if (info->control) {
735 free_bch(info->control);
736 info->control = NULL;
Andreas Bießmann82a65472013-04-05 04:55:21 +0000737 }
738}
pekon gupta6bd91a82013-11-18 19:03:00 +0530739#endif /* CONFIG_BCH */
740
741/**
742 * omap_select_ecc_scheme - configures driver for particular ecc-scheme
743 * @nand: NAND chip device structure
744 * @ecc_scheme: ecc scheme to configure
745 * @pagesize: number of main-area bytes per page of NAND device
746 * @oobsize: number of OOB/spare bytes per page of NAND device
747 */
748static int omap_select_ecc_scheme(struct nand_chip *nand,
749 enum omap_ecc ecc_scheme, unsigned int pagesize, unsigned int oobsize) {
Scott Wood17fed142016-05-30 13:57:56 -0500750 struct omap_nand_info *info = nand_get_controller_data(nand);
Nikita Kiryanove8167892013-12-16 19:19:01 +0200751 struct nand_ecclayout *ecclayout = &omap_ecclayout;
pekon gupta6bd91a82013-11-18 19:03:00 +0530752 int eccsteps = pagesize / SECTOR_BYTES;
753 int i;
754
755 switch (ecc_scheme) {
756 case OMAP_ECC_HAM1_CODE_SW:
757 debug("nand: selected OMAP_ECC_HAM1_CODE_SW\n");
758 /* For this ecc-scheme, ecc.bytes, ecc.layout, ... are
759 * initialized in nand_scan_tail(), so just set ecc.mode */
pekon guptaaa168482014-04-11 12:55:33 +0530760 info->control = NULL;
pekon gupta6bd91a82013-11-18 19:03:00 +0530761 nand->ecc.mode = NAND_ECC_SOFT;
762 nand->ecc.layout = NULL;
Nikita Kiryanov4110c822013-12-12 15:19:31 +0200763 nand->ecc.size = 0;
pekon gupta6bd91a82013-11-18 19:03:00 +0530764 break;
765
766 case OMAP_ECC_HAM1_CODE_HW:
767 debug("nand: selected OMAP_ECC_HAM1_CODE_HW\n");
768 /* check ecc-scheme requirements before updating ecc info */
769 if ((3 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
770 printf("nand: error: insufficient OOB: require=%d\n", (
771 (3 * eccsteps) + BADBLOCK_MARKER_LENGTH));
772 return -EINVAL;
773 }
pekon guptaaa168482014-04-11 12:55:33 +0530774 info->control = NULL;
pekon gupta6bd91a82013-11-18 19:03:00 +0530775 /* populate ecc specific fields */
Nikita Kiryanov2e18ff22013-12-17 15:18:01 +0200776 memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
pekon gupta6bd91a82013-11-18 19:03:00 +0530777 nand->ecc.mode = NAND_ECC_HW;
778 nand->ecc.strength = 1;
779 nand->ecc.size = SECTOR_BYTES;
780 nand->ecc.bytes = 3;
781 nand->ecc.hwctl = omap_enable_hwecc;
782 nand->ecc.correct = omap_correct_data;
783 nand->ecc.calculate = omap_calculate_ecc;
784 /* define ecc-layout */
785 ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
pekon guptaf0aff022013-12-05 17:54:21 +0530786 for (i = 0; i < ecclayout->eccbytes; i++) {
787 if (nand->options & NAND_BUSWIDTH_16)
788 ecclayout->eccpos[i] = i + 2;
789 else
790 ecclayout->eccpos[i] = i + 1;
791 }
pekon gupta6bd91a82013-11-18 19:03:00 +0530792 ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
793 ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
794 BADBLOCK_MARKER_LENGTH;
pekon gupta6bd91a82013-11-18 19:03:00 +0530795 break;
796
797 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
798#ifdef CONFIG_BCH
799 debug("nand: selected OMAP_ECC_BCH8_CODE_HW_DETECTION_SW\n");
800 /* check ecc-scheme requirements before updating ecc info */
801 if ((13 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
802 printf("nand: error: insufficient OOB: require=%d\n", (
803 (13 * eccsteps) + BADBLOCK_MARKER_LENGTH));
804 return -EINVAL;
805 }
806 /* check if BCH S/W library can be used for error detection */
pekon guptaaa168482014-04-11 12:55:33 +0530807 info->control = init_bch(13, 8, 0x201b);
808 if (!info->control) {
pekon gupta6bd91a82013-11-18 19:03:00 +0530809 printf("nand: error: could not init_bch()\n");
810 return -ENODEV;
811 }
pekon gupta6bd91a82013-11-18 19:03:00 +0530812 /* populate ecc specific fields */
Nikita Kiryanov2e18ff22013-12-17 15:18:01 +0200813 memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
pekon gupta6bd91a82013-11-18 19:03:00 +0530814 nand->ecc.mode = NAND_ECC_HW;
815 nand->ecc.strength = 8;
816 nand->ecc.size = SECTOR_BYTES;
817 nand->ecc.bytes = 13;
pekon guptaeff10ee2013-11-19 11:02:15 +0530818 nand->ecc.hwctl = omap_enable_hwecc;
pekon gupta6bd91a82013-11-18 19:03:00 +0530819 nand->ecc.correct = omap_correct_data_bch_sw;
pekon gupta03742c92013-11-19 11:02:16 +0530820 nand->ecc.calculate = omap_calculate_ecc;
pekon gupta6bd91a82013-11-18 19:03:00 +0530821 /* define ecc-layout */
822 ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
823 ecclayout->eccpos[0] = BADBLOCK_MARKER_LENGTH;
824 for (i = 1; i < ecclayout->eccbytes; i++) {
825 if (i % nand->ecc.bytes)
826 ecclayout->eccpos[i] =
827 ecclayout->eccpos[i - 1] + 1;
828 else
829 ecclayout->eccpos[i] =
830 ecclayout->eccpos[i - 1] + 2;
831 }
832 ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
833 ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
834 BADBLOCK_MARKER_LENGTH;
pekon gupta6bd91a82013-11-18 19:03:00 +0530835 break;
836#else
837 printf("nand: error: CONFIG_BCH required for ECC\n");
838 return -EINVAL;
839#endif
840
841 case OMAP_ECC_BCH8_CODE_HW:
842#ifdef CONFIG_NAND_OMAP_ELM
843 debug("nand: selected OMAP_ECC_BCH8_CODE_HW\n");
844 /* check ecc-scheme requirements before updating ecc info */
845 if ((14 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
846 printf("nand: error: insufficient OOB: require=%d\n", (
847 (14 * eccsteps) + BADBLOCK_MARKER_LENGTH));
848 return -EINVAL;
849 }
850 /* intialize ELM for ECC error detection */
851 elm_init();
pekon guptaaa168482014-04-11 12:55:33 +0530852 info->control = NULL;
pekon gupta6bd91a82013-11-18 19:03:00 +0530853 /* populate ecc specific fields */
Nikita Kiryanov2e18ff22013-12-17 15:18:01 +0200854 memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
pekon gupta6bd91a82013-11-18 19:03:00 +0530855 nand->ecc.mode = NAND_ECC_HW;
856 nand->ecc.strength = 8;
857 nand->ecc.size = SECTOR_BYTES;
858 nand->ecc.bytes = 14;
pekon guptaeff10ee2013-11-19 11:02:15 +0530859 nand->ecc.hwctl = omap_enable_hwecc;
pekon gupta6bd91a82013-11-18 19:03:00 +0530860 nand->ecc.correct = omap_correct_data_bch;
pekon gupta03742c92013-11-19 11:02:16 +0530861 nand->ecc.calculate = omap_calculate_ecc;
pekon gupta6bd91a82013-11-18 19:03:00 +0530862 nand->ecc.read_page = omap_read_page_bch;
863 /* define ecc-layout */
864 ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
865 for (i = 0; i < ecclayout->eccbytes; i++)
866 ecclayout->eccpos[i] = i + BADBLOCK_MARKER_LENGTH;
867 ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
868 ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
869 BADBLOCK_MARKER_LENGTH;
pekon gupta6bd91a82013-11-18 19:03:00 +0530870 break;
871#else
872 printf("nand: error: CONFIG_NAND_OMAP_ELM required for ECC\n");
873 return -EINVAL;
874#endif
875
pekon gupta046cf862014-06-02 17:14:42 +0530876 case OMAP_ECC_BCH16_CODE_HW:
877#ifdef CONFIG_NAND_OMAP_ELM
878 debug("nand: using OMAP_ECC_BCH16_CODE_HW\n");
879 /* check ecc-scheme requirements before updating ecc info */
880 if ((26 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
881 printf("nand: error: insufficient OOB: require=%d\n", (
882 (26 * eccsteps) + BADBLOCK_MARKER_LENGTH));
883 return -EINVAL;
884 }
885 /* intialize ELM for ECC error detection */
886 elm_init();
887 /* populate ecc specific fields */
888 nand->ecc.mode = NAND_ECC_HW;
889 nand->ecc.size = SECTOR_BYTES;
890 nand->ecc.bytes = 26;
891 nand->ecc.strength = 16;
892 nand->ecc.hwctl = omap_enable_hwecc;
893 nand->ecc.correct = omap_correct_data_bch;
894 nand->ecc.calculate = omap_calculate_ecc;
895 nand->ecc.read_page = omap_read_page_bch;
896 /* define ecc-layout */
897 ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
898 for (i = 0; i < ecclayout->eccbytes; i++)
899 ecclayout->eccpos[i] = i + BADBLOCK_MARKER_LENGTH;
900 ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
901 ecclayout->oobfree[0].length = oobsize - nand->ecc.bytes -
902 BADBLOCK_MARKER_LENGTH;
903 break;
904#else
905 printf("nand: error: CONFIG_NAND_OMAP_ELM required for ECC\n");
906 return -EINVAL;
907#endif
pekon gupta6bd91a82013-11-18 19:03:00 +0530908 default:
909 debug("nand: error: ecc scheme not enabled or supported\n");
910 return -EINVAL;
911 }
Nikita Kiryanove8167892013-12-16 19:19:01 +0200912
913 /* nand_scan_tail() sets ham1 sw ecc; hw ecc layout is set by driver */
914 if (ecc_scheme != OMAP_ECC_HAM1_CODE_SW)
915 nand->ecc.layout = ecclayout;
916
pekon guptaaa168482014-04-11 12:55:33 +0530917 info->ecc_scheme = ecc_scheme;
pekon gupta6bd91a82013-11-18 19:03:00 +0530918 return 0;
919}
Andreas Bießmann82a65472013-04-05 04:55:21 +0000920
Simon Schwarz4f62e982011-09-14 15:30:16 -0400921#ifndef CONFIG_SPL_BUILD
Dirk Behme778933f2008-12-14 09:47:16 +0100922/*
Andreas Bießmann1e4eccf2013-04-04 23:52:50 +0000923 * omap_nand_switch_ecc - switch the ECC operation between different engines
924 * (h/w and s/w) and different algorithms (hamming and BCHx)
Dirk Behme778933f2008-12-14 09:47:16 +0100925 *
Andreas Bießmann1e4eccf2013-04-04 23:52:50 +0000926 * @hardware - true if one of the HW engines should be used
927 * @eccstrength - the number of bits that could be corrected
928 * (1 - hamming, 4 - BCH4, 8 - BCH8, 16 - BCH16)
Dirk Behme778933f2008-12-14 09:47:16 +0100929 */
pekon gupta6bd91a82013-11-18 19:03:00 +0530930int __maybe_unused omap_nand_switch_ecc(uint32_t hardware, uint32_t eccstrength)
Dirk Behme778933f2008-12-14 09:47:16 +0100931{
932 struct nand_chip *nand;
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500933 struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
pekon gupta6bd91a82013-11-18 19:03:00 +0530934 int err = 0;
Dirk Behme778933f2008-12-14 09:47:16 +0100935
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500936 if (!mtd) {
pekon gupta6bd91a82013-11-18 19:03:00 +0530937 printf("nand: error: no NAND devices found\n");
938 return -ENODEV;
Dirk Behme778933f2008-12-14 09:47:16 +0100939 }
940
Scott Wood17fed142016-05-30 13:57:56 -0500941 nand = mtd_to_nand(mtd);
Dirk Behme778933f2008-12-14 09:47:16 +0100942 nand->options |= NAND_OWN_BUFFERS;
Jeroen Hofstee96306f22014-01-15 17:58:54 +0100943 nand->options &= ~NAND_SUBPAGE_READ;
Dirk Behme778933f2008-12-14 09:47:16 +0100944 /* Setup the ecc configurations again */
Andreas Bießmann1e4eccf2013-04-04 23:52:50 +0000945 if (hardware) {
946 if (eccstrength == 1) {
pekon gupta6bd91a82013-11-18 19:03:00 +0530947 err = omap_select_ecc_scheme(nand,
948 OMAP_ECC_HAM1_CODE_HW,
949 mtd->writesize, mtd->oobsize);
950 } else if (eccstrength == 8) {
951 err = omap_select_ecc_scheme(nand,
952 OMAP_ECC_BCH8_CODE_HW,
953 mtd->writesize, mtd->oobsize);
Heiko Schocher5bf904c2016-06-07 08:55:42 +0200954 } else if (eccstrength == 16) {
955 err = omap_select_ecc_scheme(nand,
956 OMAP_ECC_BCH16_CODE_HW,
957 mtd->writesize, mtd->oobsize);
pekon gupta6bd91a82013-11-18 19:03:00 +0530958 } else {
959 printf("nand: error: unsupported ECC scheme\n");
960 return -EINVAL;
Andreas Bießmann1e4eccf2013-04-04 23:52:50 +0000961 }
Dirk Behme778933f2008-12-14 09:47:16 +0100962 } else {
Ash Charles4a5faa82015-02-18 11:25:11 -0800963 if (eccstrength == 1) {
964 err = omap_select_ecc_scheme(nand,
965 OMAP_ECC_HAM1_CODE_SW,
966 mtd->writesize, mtd->oobsize);
967 } else if (eccstrength == 8) {
968 err = omap_select_ecc_scheme(nand,
969 OMAP_ECC_BCH8_CODE_HW_DETECTION_SW,
pekon gupta6bd91a82013-11-18 19:03:00 +0530970 mtd->writesize, mtd->oobsize);
Ash Charles4a5faa82015-02-18 11:25:11 -0800971 } else {
972 printf("nand: error: unsupported ECC scheme\n");
973 return -EINVAL;
974 }
Dirk Behme778933f2008-12-14 09:47:16 +0100975 }
976
977 /* Update NAND handling after ECC mode switch */
pekon gupta6bd91a82013-11-18 19:03:00 +0530978 if (!err)
979 err = nand_scan_tail(mtd);
980 return err;
Dirk Behme778933f2008-12-14 09:47:16 +0100981}
Simon Schwarz4f62e982011-09-14 15:30:16 -0400982#endif /* CONFIG_SPL_BUILD */
Dirk Behme778933f2008-12-14 09:47:16 +0100983
984/*
985 * Board-specific NAND initialization. The following members of the
986 * argument are board-specific:
987 * - IO_ADDR_R: address to read the 8 I/O lines of the flash device
988 * - IO_ADDR_W: address to write the 8 I/O lines of the flash device
989 * - cmd_ctrl: hardwarespecific function for accesing control-lines
990 * - waitfunc: hardwarespecific function for accesing device ready/busy line
991 * - ecc.hwctl: function to enable (reset) hardware ecc generator
992 * - ecc.mode: mode of ecc, see defines
993 * - chip_delay: chip dependent delay for transfering data from array to
994 * read regs (tR)
995 * - options: various chip options. They can partly be set to inform
996 * nand_scan about special functionality. See the defines for further
997 * explanation
998 */
999int board_nand_init(struct nand_chip *nand)
1000{
1001 int32_t gpmc_config = 0;
Rostislav Lisovy77cdf8a2014-09-02 16:23:58 +02001002 int cs = cs_next++;
pekon gupta6bd91a82013-11-18 19:03:00 +05301003 int err = 0;
Dirk Behme778933f2008-12-14 09:47:16 +01001004 /*
1005 * xloader/Uboot's gpmc configuration would have configured GPMC for
1006 * nand type of memory. The following logic scans and latches on to the
1007 * first CS with NAND type memory.
1008 * TBD: need to make this logic generic to handle multiple CS NAND
1009 * devices.
1010 */
1011 while (cs < GPMC_MAX_CS) {
Dirk Behme778933f2008-12-14 09:47:16 +01001012 /* Check if NAND type is set */
Dirk Behmea4becd62009-08-08 09:30:22 +02001013 if ((readl(&gpmc_cfg->cs[cs].config1) & 0xC00) == 0x800) {
Dirk Behme778933f2008-12-14 09:47:16 +01001014 /* Found it!! */
1015 break;
1016 }
1017 cs++;
1018 }
1019 if (cs >= GPMC_MAX_CS) {
pekon gupta6bd91a82013-11-18 19:03:00 +05301020 printf("nand: error: Unable to find NAND settings in "
Dirk Behme778933f2008-12-14 09:47:16 +01001021 "GPMC Configuration - quitting\n");
1022 return -ENODEV;
1023 }
1024
Dirk Behmea4becd62009-08-08 09:30:22 +02001025 gpmc_config = readl(&gpmc_cfg->config);
Dirk Behme778933f2008-12-14 09:47:16 +01001026 /* Disable Write protect */
1027 gpmc_config |= 0x10;
Dirk Behmea4becd62009-08-08 09:30:22 +02001028 writel(gpmc_config, &gpmc_cfg->config);
Dirk Behme778933f2008-12-14 09:47:16 +01001029
Dirk Behmea4becd62009-08-08 09:30:22 +02001030 nand->IO_ADDR_R = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
1031 nand->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
Rostislav Lisovy77cdf8a2014-09-02 16:23:58 +02001032 omap_nand_info[cs].control = NULL;
1033 omap_nand_info[cs].cs = cs;
Michal Sojka26160072015-02-17 17:08:37 +01001034 omap_nand_info[cs].ws = wscfg[cs];
Scott Wood17fed142016-05-30 13:57:56 -05001035 nand_set_controller_data(nand, &omap_nand_info[cs]);
pekon gupta6bd91a82013-11-18 19:03:00 +05301036 nand->cmd_ctrl = omap_nand_hwcontrol;
1037 nand->options |= NAND_NO_PADDING | NAND_CACHEPRG;
Dirk Behme778933f2008-12-14 09:47:16 +01001038 nand->chip_delay = 100;
pekon gupta6bd91a82013-11-18 19:03:00 +05301039 nand->ecc.layout = &omap_ecclayout;
Mansoor Ahamede5612512012-11-06 13:06:33 +00001040
pekon gupta6250faf2014-05-06 00:46:19 +05301041 /* configure driver and controller based on NAND device bus-width */
1042 gpmc_config = readl(&gpmc_cfg->cs[cs].config1);
1043#if defined(CONFIG_SYS_NAND_BUSWIDTH_16BIT)
1044 nand->options |= NAND_BUSWIDTH_16;
1045 writel(gpmc_config | (0x1 << 12), &gpmc_cfg->cs[cs].config1);
1046#else
1047 nand->options &= ~NAND_BUSWIDTH_16;
1048 writel(gpmc_config & ~(0x1 << 12), &gpmc_cfg->cs[cs].config1);
1049#endif
pekon gupta6bd91a82013-11-18 19:03:00 +05301050 /* select ECC scheme */
pekon gupta3ef49732013-11-18 19:03:01 +05301051#if defined(CONFIG_NAND_OMAP_ECCSCHEME)
1052 err = omap_select_ecc_scheme(nand, CONFIG_NAND_OMAP_ECCSCHEME,
pekon gupta6bd91a82013-11-18 19:03:00 +05301053 CONFIG_SYS_NAND_PAGE_SIZE, CONFIG_SYS_NAND_OOBSIZE);
pekon gupta3ef49732013-11-18 19:03:01 +05301054#else
1055 /* pagesize and oobsize are not required to configure sw ecc-scheme */
pekon gupta6bd91a82013-11-18 19:03:00 +05301056 err = omap_select_ecc_scheme(nand, OMAP_ECC_HAM1_CODE_SW,
1057 0, 0);
Mansoor Ahamede5612512012-11-06 13:06:33 +00001058#endif
pekon gupta6bd91a82013-11-18 19:03:00 +05301059 if (err)
1060 return err;
Simon Schwarz4f62e982011-09-14 15:30:16 -04001061
Egli, Samuel8645fd92015-02-13 15:47:10 +01001062#ifdef CONFIG_NAND_OMAP_GPMC_PREFETCH
Jeroen Hofstee5e67ac72015-05-30 10:11:24 +02001063 nand->read_buf = omap_nand_read_prefetch;
Egli, Samuel8645fd92015-02-13 15:47:10 +01001064#else
Jeroen Hofstee5e67ac72015-05-30 10:11:24 +02001065 if (nand->options & NAND_BUSWIDTH_16)
1066 nand->read_buf = nand_read_buf16;
Egli, Samuel8645fd92015-02-13 15:47:10 +01001067 else
1068 nand->read_buf = nand_read_buf;
Simon Schwarz4f62e982011-09-14 15:30:16 -04001069#endif
Stefan Roesee05972f2014-11-13 03:43:39 +01001070
1071 nand->dev_ready = omap_dev_ready;
1072
Dirk Behme778933f2008-12-14 09:47:16 +01001073 return 0;
1074}