mtd: nand: remove unneeded of_mtd.h inclusions
[deliverable/linux.git] / drivers / mtd / nand / omap2.c
CommitLineData
67ce04bf
VS
1/*
2 * Copyright © 2004 Texas Instruments, Jian Zhang <jzhang@ti.com>
3 * Copyright © 2004 Micron Technology Inc.
4 * Copyright © 2004 David Brownell
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/platform_device.h>
763e7359 12#include <linux/dmaengine.h>
67ce04bf
VS
13#include <linux/dma-mapping.h>
14#include <linux/delay.h>
10f22ee3 15#include <linux/gpio/consumer.h>
a0e5cc58 16#include <linux/module.h>
4e070376 17#include <linux/interrupt.h>
c276aca4 18#include <linux/jiffies.h>
19#include <linux/sched.h>
67ce04bf
VS
20#include <linux/mtd/mtd.h>
21#include <linux/mtd/nand.h>
22#include <linux/mtd/partitions.h>
763e7359 23#include <linux/omap-dma.h>
67ce04bf 24#include <linux/io.h>
5a0e3ad6 25#include <linux/slab.h>
62116e51
PA
26#include <linux/of.h>
27#include <linux/of_device.h>
c9711ec5 28#include <linux/of_mtd.h>
67ce04bf 29
32d42a85 30#include <linux/mtd/nand_bch.h>
62116e51 31#include <linux/platform_data/elm.h>
0e618ef0 32
c509aefd 33#include <linux/omap-gpmc.h>
2203747c 34#include <linux/platform_data/mtd-nand-omap2.h>
67ce04bf 35
67ce04bf 36#define DRIVER_NAME "omap2-nand"
4e070376 37#define OMAP_NAND_TIMEOUT_MS 5000
67ce04bf 38
67ce04bf
VS
39#define NAND_Ecc_P1e (1 << 0)
40#define NAND_Ecc_P2e (1 << 1)
41#define NAND_Ecc_P4e (1 << 2)
42#define NAND_Ecc_P8e (1 << 3)
43#define NAND_Ecc_P16e (1 << 4)
44#define NAND_Ecc_P32e (1 << 5)
45#define NAND_Ecc_P64e (1 << 6)
46#define NAND_Ecc_P128e (1 << 7)
47#define NAND_Ecc_P256e (1 << 8)
48#define NAND_Ecc_P512e (1 << 9)
49#define NAND_Ecc_P1024e (1 << 10)
50#define NAND_Ecc_P2048e (1 << 11)
51
52#define NAND_Ecc_P1o (1 << 16)
53#define NAND_Ecc_P2o (1 << 17)
54#define NAND_Ecc_P4o (1 << 18)
55#define NAND_Ecc_P8o (1 << 19)
56#define NAND_Ecc_P16o (1 << 20)
57#define NAND_Ecc_P32o (1 << 21)
58#define NAND_Ecc_P64o (1 << 22)
59#define NAND_Ecc_P128o (1 << 23)
60#define NAND_Ecc_P256o (1 << 24)
61#define NAND_Ecc_P512o (1 << 25)
62#define NAND_Ecc_P1024o (1 << 26)
63#define NAND_Ecc_P2048o (1 << 27)
64
65#define TF(value) (value ? 1 : 0)
66
67#define P2048e(a) (TF(a & NAND_Ecc_P2048e) << 0)
68#define P2048o(a) (TF(a & NAND_Ecc_P2048o) << 1)
69#define P1e(a) (TF(a & NAND_Ecc_P1e) << 2)
70#define P1o(a) (TF(a & NAND_Ecc_P1o) << 3)
71#define P2e(a) (TF(a & NAND_Ecc_P2e) << 4)
72#define P2o(a) (TF(a & NAND_Ecc_P2o) << 5)
73#define P4e(a) (TF(a & NAND_Ecc_P4e) << 6)
74#define P4o(a) (TF(a & NAND_Ecc_P4o) << 7)
75
76#define P8e(a) (TF(a & NAND_Ecc_P8e) << 0)
77#define P8o(a) (TF(a & NAND_Ecc_P8o) << 1)
78#define P16e(a) (TF(a & NAND_Ecc_P16e) << 2)
79#define P16o(a) (TF(a & NAND_Ecc_P16o) << 3)
80#define P32e(a) (TF(a & NAND_Ecc_P32e) << 4)
81#define P32o(a) (TF(a & NAND_Ecc_P32o) << 5)
82#define P64e(a) (TF(a & NAND_Ecc_P64e) << 6)
83#define P64o(a) (TF(a & NAND_Ecc_P64o) << 7)
84
85#define P128e(a) (TF(a & NAND_Ecc_P128e) << 0)
86#define P128o(a) (TF(a & NAND_Ecc_P128o) << 1)
87#define P256e(a) (TF(a & NAND_Ecc_P256e) << 2)
88#define P256o(a) (TF(a & NAND_Ecc_P256o) << 3)
89#define P512e(a) (TF(a & NAND_Ecc_P512e) << 4)
90#define P512o(a) (TF(a & NAND_Ecc_P512o) << 5)
91#define P1024e(a) (TF(a & NAND_Ecc_P1024e) << 6)
92#define P1024o(a) (TF(a & NAND_Ecc_P1024o) << 7)
93
94#define P8e_s(a) (TF(a & NAND_Ecc_P8e) << 0)
95#define P8o_s(a) (TF(a & NAND_Ecc_P8o) << 1)
96#define P16e_s(a) (TF(a & NAND_Ecc_P16e) << 2)
97#define P16o_s(a) (TF(a & NAND_Ecc_P16o) << 3)
98#define P1e_s(a) (TF(a & NAND_Ecc_P1e) << 4)
99#define P1o_s(a) (TF(a & NAND_Ecc_P1o) << 5)
100#define P2e_s(a) (TF(a & NAND_Ecc_P2e) << 6)
101#define P2o_s(a) (TF(a & NAND_Ecc_P2o) << 7)
102
103#define P4e_s(a) (TF(a & NAND_Ecc_P4e) << 0)
104#define P4o_s(a) (TF(a & NAND_Ecc_P4o) << 1)
105
65b97cf6
AM
106#define PREFETCH_CONFIG1_CS_SHIFT 24
107#define ECC_CONFIG_CS_SHIFT 1
108#define CS_MASK 0x7
109#define ENABLE_PREFETCH (0x1 << 7)
110#define DMA_MPU_MODE_SHIFT 2
2ef9f3dd 111#define ECCSIZE0_SHIFT 12
65b97cf6
AM
112#define ECCSIZE1_SHIFT 22
113#define ECC1RESULTSIZE 0x1
114#define ECCCLEAR 0x100
115#define ECC1 0x1
47f88af4
AM
116#define PREFETCH_FIFOTHRESHOLD_MAX 0x40
117#define PREFETCH_FIFOTHRESHOLD(val) ((val) << 8)
118#define PREFETCH_STATUS_COUNT(val) (val & 0x00003fff)
119#define PREFETCH_STATUS_FIFO_CNT(val) ((val >> 24) & 0x7F)
120#define STATUS_BUFF_EMPTY 0x00000001
65b97cf6 121
d5e7c864
LV
122#define OMAP24XX_DMA_GPMC 4
123
62116e51
PA
124#define SECTOR_BYTES 512
125/* 4 bit padding to make byte aligned, 56 = 52 + 4 */
126#define BCH4_BIT_PAD 4
62116e51
PA
127
128/* GPMC ecc engine settings for read */
129#define BCH_WRAPMODE_1 1 /* BCH wrap mode 1 */
130#define BCH8R_ECC_SIZE0 0x1a /* ecc_size0 = 26 */
131#define BCH8R_ECC_SIZE1 0x2 /* ecc_size1 = 2 */
132#define BCH4R_ECC_SIZE0 0xd /* ecc_size0 = 13 */
133#define BCH4R_ECC_SIZE1 0x3 /* ecc_size1 = 3 */
134
135/* GPMC ecc engine settings for write */
136#define BCH_WRAPMODE_6 6 /* BCH wrap mode 6 */
137#define BCH_ECC_SIZE0 0x0 /* ecc_size0 = 0, no oob protection */
138#define BCH_ECC_SIZE1 0x20 /* ecc_size1 = 32 */
139
b491da72 140#define BADBLOCK_MARKER_LENGTH 2
a919e511 141
9748fff9 142static u_char bch16_vector[] = {0xf5, 0x24, 0x1c, 0xd0, 0x61, 0xb3, 0xf1, 0x55,
143 0x2e, 0x2c, 0x86, 0xa3, 0xed, 0x36, 0x1b, 0x78,
144 0x48, 0x76, 0xa9, 0x3b, 0x97, 0xd1, 0x7a, 0x93,
145 0x07, 0x0e};
62116e51
PA
146static u_char bch8_vector[] = {0xf3, 0xdb, 0x14, 0x16, 0x8b, 0xd2, 0xbe, 0xcc,
147 0xac, 0x6b, 0xff, 0x99, 0x7b};
148static u_char bch4_vector[] = {0x00, 0x6b, 0x31, 0xdd, 0x41, 0xbc, 0x10};
62116e51 149
1dc338e8
RL
150/* Shared among all NAND instances to synchronize access to the ECC Engine */
151static struct nand_hw_control omap_gpmc_controller = {
152 .lock = __SPIN_LOCK_UNLOCKED(omap_gpmc_controller.lock),
153 .wq = __WAIT_QUEUE_HEAD_INITIALIZER(omap_gpmc_controller.wq),
154};
59e9c5ae 155
67ce04bf 156struct omap_nand_info {
67ce04bf
VS
157 struct nand_chip nand;
158 struct platform_device *pdev;
159
160 int gpmc_cs;
01b95fc6
RQ
161 bool dev_ready;
162 enum nand_io xfer_type;
163 int devsize;
4e558072 164 enum omap_ecc ecc_opt;
01b95fc6
RQ
165 struct device_node *elm_of_node;
166
167 unsigned long phys_base;
dfe32893 168 struct completion comp;
763e7359 169 struct dma_chan *dma;
5c468455
AM
170 int gpmc_irq_fifo;
171 int gpmc_irq_count;
4e070376
SG
172 enum {
173 OMAP_NAND_IO_READ = 0, /* read */
174 OMAP_NAND_IO_WRITE, /* write */
175 } iomode;
176 u_char *buf;
177 int buf_len;
c509aefd 178 /* Interface to GPMC */
65b97cf6 179 struct gpmc_nand_regs reg;
c509aefd 180 struct gpmc_nand_ops *ops;
c9711ec5 181 bool flash_bbt;
94cb4ee0
RL
182 /* generated at runtime depending on ECC algorithm and layout selected */
183 struct nand_ecclayout oobinfo;
a919e511 184 /* fields specific for BCHx_HW ECC scheme */
62116e51 185 struct device *elm_dev;
10f22ee3
RQ
186 /* NAND ready gpio */
187 struct gpio_desc *ready_gpiod;
67ce04bf
VS
188};
189
4578ea9a
BB
190static inline struct omap_nand_info *mtd_to_omap(struct mtd_info *mtd)
191{
432420c0 192 return container_of(mtd_to_nand(mtd), struct omap_nand_info, nand);
4578ea9a 193}
432420c0 194
65b97cf6
AM
195/**
196 * omap_prefetch_enable - configures and starts prefetch transfer
197 * @cs: cs (chip select) number
198 * @fifo_th: fifo threshold to be used for read/ write
199 * @dma_mode: dma mode enable (1) or disable (0)
200 * @u32_count: number of bytes to be transferred
201 * @is_write: prefetch read(0) or write post(1) mode
202 */
203static int omap_prefetch_enable(int cs, int fifo_th, int dma_mode,
204 unsigned int u32_count, int is_write, struct omap_nand_info *info)
205{
206 u32 val;
207
208 if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX)
209 return -1;
210
211 if (readl(info->reg.gpmc_prefetch_control))
212 return -EBUSY;
213
214 /* Set the amount of bytes to be prefetched */
215 writel(u32_count, info->reg.gpmc_prefetch_config2);
216
217 /* Set dma/mpu mode, the prefetch read / post write and
218 * enable the engine. Set which cs is has requested for.
219 */
220 val = ((cs << PREFETCH_CONFIG1_CS_SHIFT) |
221 PREFETCH_FIFOTHRESHOLD(fifo_th) | ENABLE_PREFETCH |
222 (dma_mode << DMA_MPU_MODE_SHIFT) | (0x1 & is_write));
223 writel(val, info->reg.gpmc_prefetch_config1);
224
225 /* Start the prefetch engine */
226 writel(0x1, info->reg.gpmc_prefetch_control);
227
228 return 0;
229}
230
231/**
232 * omap_prefetch_reset - disables and stops the prefetch engine
233 */
234static int omap_prefetch_reset(int cs, struct omap_nand_info *info)
235{
236 u32 config1;
237
238 /* check if the same module/cs is trying to reset */
239 config1 = readl(info->reg.gpmc_prefetch_config1);
240 if (((config1 >> PREFETCH_CONFIG1_CS_SHIFT) & CS_MASK) != cs)
241 return -EINVAL;
242
243 /* Stop the PFPW engine */
244 writel(0x0, info->reg.gpmc_prefetch_control);
245
246 /* Reset/disable the PFPW engine */
247 writel(0x0, info->reg.gpmc_prefetch_config1);
248
249 return 0;
250}
251
67ce04bf
VS
252/**
253 * omap_hwcontrol - hardware specific access to control-lines
254 * @mtd: MTD device structure
255 * @cmd: command to device
256 * @ctrl:
257 * NAND_NCE: bit 0 -> don't care
258 * NAND_CLE: bit 1 -> Command Latch
259 * NAND_ALE: bit 2 -> Address Latch
260 *
261 * NOTE: boards may use different bits for these!!
262 */
263static void omap_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
264{
4578ea9a 265 struct omap_nand_info *info = mtd_to_omap(mtd);
67ce04bf 266
2c01946c
SG
267 if (cmd != NAND_CMD_NONE) {
268 if (ctrl & NAND_CLE)
65b97cf6 269 writeb(cmd, info->reg.gpmc_nand_command);
2c01946c
SG
270
271 else if (ctrl & NAND_ALE)
65b97cf6 272 writeb(cmd, info->reg.gpmc_nand_address);
2c01946c
SG
273
274 else /* NAND_NCE */
65b97cf6 275 writeb(cmd, info->reg.gpmc_nand_data);
2c01946c 276 }
67ce04bf
VS
277}
278
59e9c5ae 279/**
280 * omap_read_buf8 - read data from NAND controller into buffer
281 * @mtd: MTD device structure
282 * @buf: buffer to store date
283 * @len: number of bytes to read
284 */
285static void omap_read_buf8(struct mtd_info *mtd, u_char *buf, int len)
286{
4bd4ebcc 287 struct nand_chip *nand = mtd_to_nand(mtd);
59e9c5ae 288
289 ioread8_rep(nand->IO_ADDR_R, buf, len);
290}
291
292/**
293 * omap_write_buf8 - write buffer to NAND controller
294 * @mtd: MTD device structure
295 * @buf: data buffer
296 * @len: number of bytes to write
297 */
298static void omap_write_buf8(struct mtd_info *mtd, const u_char *buf, int len)
299{
4578ea9a 300 struct omap_nand_info *info = mtd_to_omap(mtd);
59e9c5ae 301 u_char *p = (u_char *)buf;
d6e55216 302 bool status;
59e9c5ae 303
304 while (len--) {
305 iowrite8(*p++, info->nand.IO_ADDR_W);
2c01946c
SG
306 /* wait until buffer is available for write */
307 do {
d6e55216 308 status = info->ops->nand_writebuffer_empty();
2c01946c 309 } while (!status);
59e9c5ae 310 }
311}
312
67ce04bf
VS
313/**
314 * omap_read_buf16 - read data from NAND controller into buffer
315 * @mtd: MTD device structure
316 * @buf: buffer to store date
317 * @len: number of bytes to read
318 */
319static void omap_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
320{
4bd4ebcc 321 struct nand_chip *nand = mtd_to_nand(mtd);
67ce04bf 322
59e9c5ae 323 ioread16_rep(nand->IO_ADDR_R, buf, len / 2);
67ce04bf
VS
324}
325
326/**
327 * omap_write_buf16 - write buffer to NAND controller
328 * @mtd: MTD device structure
329 * @buf: data buffer
330 * @len: number of bytes to write
331 */
332static void omap_write_buf16(struct mtd_info *mtd, const u_char * buf, int len)
333{
4578ea9a 334 struct omap_nand_info *info = mtd_to_omap(mtd);
67ce04bf 335 u16 *p = (u16 *) buf;
d6e55216 336 bool status;
67ce04bf
VS
337 /* FIXME try bursts of writesw() or DMA ... */
338 len >>= 1;
339
340 while (len--) {
59e9c5ae 341 iowrite16(*p++, info->nand.IO_ADDR_W);
2c01946c
SG
342 /* wait until buffer is available for write */
343 do {
d6e55216 344 status = info->ops->nand_writebuffer_empty();
2c01946c 345 } while (!status);
67ce04bf
VS
346 }
347}
59e9c5ae 348
349/**
350 * omap_read_buf_pref - read data from NAND controller into buffer
351 * @mtd: MTD device structure
352 * @buf: buffer to store date
353 * @len: number of bytes to read
354 */
355static void omap_read_buf_pref(struct mtd_info *mtd, u_char *buf, int len)
356{
4578ea9a 357 struct omap_nand_info *info = mtd_to_omap(mtd);
2c01946c 358 uint32_t r_count = 0;
59e9c5ae 359 int ret = 0;
360 u32 *p = (u32 *)buf;
361
362 /* take care of subpage reads */
c3341d0c
VS
363 if (len % 4) {
364 if (info->nand.options & NAND_BUSWIDTH_16)
365 omap_read_buf16(mtd, buf, len % 4);
366 else
367 omap_read_buf8(mtd, buf, len % 4);
368 p = (u32 *) (buf + len % 4);
369 len -= len % 4;
59e9c5ae 370 }
59e9c5ae 371
372 /* configure and start prefetch transfer */
65b97cf6
AM
373 ret = omap_prefetch_enable(info->gpmc_cs,
374 PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x0, info);
59e9c5ae 375 if (ret) {
376 /* PFPW engine is busy, use cpu copy method */
377 if (info->nand.options & NAND_BUSWIDTH_16)
c5d8c0ca 378 omap_read_buf16(mtd, (u_char *)p, len);
59e9c5ae 379 else
c5d8c0ca 380 omap_read_buf8(mtd, (u_char *)p, len);
59e9c5ae 381 } else {
382 do {
65b97cf6 383 r_count = readl(info->reg.gpmc_prefetch_status);
47f88af4 384 r_count = PREFETCH_STATUS_FIFO_CNT(r_count);
2c01946c
SG
385 r_count = r_count >> 2;
386 ioread32_rep(info->nand.IO_ADDR_R, p, r_count);
59e9c5ae 387 p += r_count;
388 len -= r_count << 2;
389 } while (len);
59e9c5ae 390 /* disable and stop the PFPW engine */
65b97cf6 391 omap_prefetch_reset(info->gpmc_cs, info);
59e9c5ae 392 }
393}
394
395/**
396 * omap_write_buf_pref - write buffer to NAND controller
397 * @mtd: MTD device structure
398 * @buf: data buffer
399 * @len: number of bytes to write
400 */
401static void omap_write_buf_pref(struct mtd_info *mtd,
402 const u_char *buf, int len)
403{
4578ea9a 404 struct omap_nand_info *info = mtd_to_omap(mtd);
4e070376 405 uint32_t w_count = 0;
59e9c5ae 406 int i = 0, ret = 0;
c5d8c0ca 407 u16 *p = (u16 *)buf;
4e070376 408 unsigned long tim, limit;
65b97cf6 409 u32 val;
59e9c5ae 410
411 /* take care of subpage writes */
412 if (len % 2 != 0) {
2c01946c 413 writeb(*buf, info->nand.IO_ADDR_W);
59e9c5ae 414 p = (u16 *)(buf + 1);
415 len--;
416 }
417
418 /* configure and start prefetch transfer */
65b97cf6
AM
419 ret = omap_prefetch_enable(info->gpmc_cs,
420 PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x1, info);
59e9c5ae 421 if (ret) {
422 /* PFPW engine is busy, use cpu copy method */
423 if (info->nand.options & NAND_BUSWIDTH_16)
c5d8c0ca 424 omap_write_buf16(mtd, (u_char *)p, len);
59e9c5ae 425 else
c5d8c0ca 426 omap_write_buf8(mtd, (u_char *)p, len);
59e9c5ae 427 } else {
2c01946c 428 while (len) {
65b97cf6 429 w_count = readl(info->reg.gpmc_prefetch_status);
47f88af4 430 w_count = PREFETCH_STATUS_FIFO_CNT(w_count);
2c01946c 431 w_count = w_count >> 1;
59e9c5ae 432 for (i = 0; (i < w_count) && len; i++, len -= 2)
2c01946c 433 iowrite16(*p++, info->nand.IO_ADDR_W);
59e9c5ae 434 }
2c01946c 435 /* wait for data to flushed-out before reset the prefetch */
4e070376
SG
436 tim = 0;
437 limit = (loops_per_jiffy *
438 msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
65b97cf6 439 do {
4e070376 440 cpu_relax();
65b97cf6 441 val = readl(info->reg.gpmc_prefetch_status);
47f88af4 442 val = PREFETCH_STATUS_COUNT(val);
65b97cf6 443 } while (val && (tim++ < limit));
4e070376 444
59e9c5ae 445 /* disable and stop the PFPW engine */
65b97cf6 446 omap_prefetch_reset(info->gpmc_cs, info);
59e9c5ae 447 }
448}
449
dfe32893 450/*
2df41d05 451 * omap_nand_dma_callback: callback on the completion of dma transfer
dfe32893 452 * @data: pointer to completion data structure
453 */
763e7359
RK
454static void omap_nand_dma_callback(void *data)
455{
456 complete((struct completion *) data);
457}
dfe32893 458
459/*
4cacbe22 460 * omap_nand_dma_transfer: configure and start dma transfer
dfe32893 461 * @mtd: MTD device structure
462 * @addr: virtual address in RAM of source/destination
463 * @len: number of data bytes to be transferred
464 * @is_write: flag for read/write operation
465 */
466static inline int omap_nand_dma_transfer(struct mtd_info *mtd, void *addr,
467 unsigned int len, int is_write)
468{
4578ea9a 469 struct omap_nand_info *info = mtd_to_omap(mtd);
2df41d05 470 struct dma_async_tx_descriptor *tx;
dfe32893 471 enum dma_data_direction dir = is_write ? DMA_TO_DEVICE :
472 DMA_FROM_DEVICE;
2df41d05 473 struct scatterlist sg;
4e070376 474 unsigned long tim, limit;
2df41d05
RK
475 unsigned n;
476 int ret;
65b97cf6 477 u32 val;
dfe32893 478
479 if (addr >= high_memory) {
480 struct page *p1;
481
482 if (((size_t)addr & PAGE_MASK) !=
483 ((size_t)(addr + len - 1) & PAGE_MASK))
484 goto out_copy;
485 p1 = vmalloc_to_page(addr);
486 if (!p1)
487 goto out_copy;
488 addr = page_address(p1) + ((size_t)addr & ~PAGE_MASK);
489 }
490
2df41d05
RK
491 sg_init_one(&sg, addr, len);
492 n = dma_map_sg(info->dma->device->dev, &sg, 1, dir);
493 if (n == 0) {
dfe32893 494 dev_err(&info->pdev->dev,
495 "Couldn't DMA map a %d byte buffer\n", len);
496 goto out_copy;
497 }
498
2df41d05
RK
499 tx = dmaengine_prep_slave_sg(info->dma, &sg, n,
500 is_write ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
501 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
502 if (!tx)
503 goto out_copy_unmap;
504
505 tx->callback = omap_nand_dma_callback;
506 tx->callback_param = &info->comp;
507 dmaengine_submit(tx);
508
65b97cf6
AM
509 /* configure and start prefetch transfer */
510 ret = omap_prefetch_enable(info->gpmc_cs,
511 PREFETCH_FIFOTHRESHOLD_MAX, 0x1, len, is_write, info);
dfe32893 512 if (ret)
4e070376 513 /* PFPW engine is busy, use cpu copy method */
d7efe228 514 goto out_copy_unmap;
dfe32893 515
516 init_completion(&info->comp);
2df41d05 517 dma_async_issue_pending(info->dma);
dfe32893 518
519 /* setup and start DMA using dma_addr */
520 wait_for_completion(&info->comp);
4e070376
SG
521 tim = 0;
522 limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
65b97cf6
AM
523
524 do {
4e070376 525 cpu_relax();
65b97cf6 526 val = readl(info->reg.gpmc_prefetch_status);
47f88af4 527 val = PREFETCH_STATUS_COUNT(val);
65b97cf6 528 } while (val && (tim++ < limit));
dfe32893 529
dfe32893 530 /* disable and stop the PFPW engine */
65b97cf6 531 omap_prefetch_reset(info->gpmc_cs, info);
dfe32893 532
2df41d05 533 dma_unmap_sg(info->dma->device->dev, &sg, 1, dir);
dfe32893 534 return 0;
535
d7efe228 536out_copy_unmap:
2df41d05 537 dma_unmap_sg(info->dma->device->dev, &sg, 1, dir);
dfe32893 538out_copy:
539 if (info->nand.options & NAND_BUSWIDTH_16)
540 is_write == 0 ? omap_read_buf16(mtd, (u_char *) addr, len)
541 : omap_write_buf16(mtd, (u_char *) addr, len);
542 else
543 is_write == 0 ? omap_read_buf8(mtd, (u_char *) addr, len)
544 : omap_write_buf8(mtd, (u_char *) addr, len);
545 return 0;
546}
dfe32893 547
548/**
549 * omap_read_buf_dma_pref - read data from NAND controller into buffer
550 * @mtd: MTD device structure
551 * @buf: buffer to store date
552 * @len: number of bytes to read
553 */
554static void omap_read_buf_dma_pref(struct mtd_info *mtd, u_char *buf, int len)
555{
556 if (len <= mtd->oobsize)
557 omap_read_buf_pref(mtd, buf, len);
558 else
559 /* start transfer in DMA mode */
560 omap_nand_dma_transfer(mtd, buf, len, 0x0);
561}
562
563/**
564 * omap_write_buf_dma_pref - write buffer to NAND controller
565 * @mtd: MTD device structure
566 * @buf: data buffer
567 * @len: number of bytes to write
568 */
569static void omap_write_buf_dma_pref(struct mtd_info *mtd,
570 const u_char *buf, int len)
571{
572 if (len <= mtd->oobsize)
573 omap_write_buf_pref(mtd, buf, len);
574 else
575 /* start transfer in DMA mode */
bdaefc41 576 omap_nand_dma_transfer(mtd, (u_char *) buf, len, 0x1);
dfe32893 577}
578
4e070376 579/*
4cacbe22 580 * omap_nand_irq - GPMC irq handler
4e070376
SG
581 * @this_irq: gpmc irq number
582 * @dev: omap_nand_info structure pointer is passed here
583 */
584static irqreturn_t omap_nand_irq(int this_irq, void *dev)
585{
586 struct omap_nand_info *info = (struct omap_nand_info *) dev;
587 u32 bytes;
4e070376 588
65b97cf6 589 bytes = readl(info->reg.gpmc_prefetch_status);
47f88af4 590 bytes = PREFETCH_STATUS_FIFO_CNT(bytes);
4e070376
SG
591 bytes = bytes & 0xFFFC; /* io in multiple of 4 bytes */
592 if (info->iomode == OMAP_NAND_IO_WRITE) { /* checks for write io */
5c468455 593 if (this_irq == info->gpmc_irq_count)
4e070376
SG
594 goto done;
595
596 if (info->buf_len && (info->buf_len < bytes))
597 bytes = info->buf_len;
598 else if (!info->buf_len)
599 bytes = 0;
600 iowrite32_rep(info->nand.IO_ADDR_W,
601 (u32 *)info->buf, bytes >> 2);
602 info->buf = info->buf + bytes;
603 info->buf_len -= bytes;
604
605 } else {
606 ioread32_rep(info->nand.IO_ADDR_R,
607 (u32 *)info->buf, bytes >> 2);
608 info->buf = info->buf + bytes;
609
5c468455 610 if (this_irq == info->gpmc_irq_count)
4e070376
SG
611 goto done;
612 }
4e070376
SG
613
614 return IRQ_HANDLED;
615
616done:
617 complete(&info->comp);
4e070376 618
5c468455
AM
619 disable_irq_nosync(info->gpmc_irq_fifo);
620 disable_irq_nosync(info->gpmc_irq_count);
4e070376
SG
621
622 return IRQ_HANDLED;
623}
624
625/*
626 * omap_read_buf_irq_pref - read data from NAND controller into buffer
627 * @mtd: MTD device structure
628 * @buf: buffer to store date
629 * @len: number of bytes to read
630 */
631static void omap_read_buf_irq_pref(struct mtd_info *mtd, u_char *buf, int len)
632{
4578ea9a 633 struct omap_nand_info *info = mtd_to_omap(mtd);
4e070376
SG
634 int ret = 0;
635
636 if (len <= mtd->oobsize) {
637 omap_read_buf_pref(mtd, buf, len);
638 return;
639 }
640
641 info->iomode = OMAP_NAND_IO_READ;
642 info->buf = buf;
643 init_completion(&info->comp);
644
645 /* configure and start prefetch transfer */
65b97cf6
AM
646 ret = omap_prefetch_enable(info->gpmc_cs,
647 PREFETCH_FIFOTHRESHOLD_MAX/2, 0x0, len, 0x0, info);
4e070376
SG
648 if (ret)
649 /* PFPW engine is busy, use cpu copy method */
650 goto out_copy;
651
652 info->buf_len = len;
5c468455
AM
653
654 enable_irq(info->gpmc_irq_count);
655 enable_irq(info->gpmc_irq_fifo);
4e070376
SG
656
657 /* waiting for read to complete */
658 wait_for_completion(&info->comp);
659
660 /* disable and stop the PFPW engine */
65b97cf6 661 omap_prefetch_reset(info->gpmc_cs, info);
4e070376
SG
662 return;
663
664out_copy:
665 if (info->nand.options & NAND_BUSWIDTH_16)
666 omap_read_buf16(mtd, buf, len);
667 else
668 omap_read_buf8(mtd, buf, len);
669}
670
671/*
672 * omap_write_buf_irq_pref - write buffer to NAND controller
673 * @mtd: MTD device structure
674 * @buf: data buffer
675 * @len: number of bytes to write
676 */
677static void omap_write_buf_irq_pref(struct mtd_info *mtd,
678 const u_char *buf, int len)
679{
4578ea9a 680 struct omap_nand_info *info = mtd_to_omap(mtd);
4e070376
SG
681 int ret = 0;
682 unsigned long tim, limit;
65b97cf6 683 u32 val;
4e070376
SG
684
685 if (len <= mtd->oobsize) {
686 omap_write_buf_pref(mtd, buf, len);
687 return;
688 }
689
690 info->iomode = OMAP_NAND_IO_WRITE;
691 info->buf = (u_char *) buf;
692 init_completion(&info->comp);
693
317379a9 694 /* configure and start prefetch transfer : size=24 */
65b97cf6
AM
695 ret = omap_prefetch_enable(info->gpmc_cs,
696 (PREFETCH_FIFOTHRESHOLD_MAX * 3) / 8, 0x0, len, 0x1, info);
4e070376
SG
697 if (ret)
698 /* PFPW engine is busy, use cpu copy method */
699 goto out_copy;
700
701 info->buf_len = len;
5c468455
AM
702
703 enable_irq(info->gpmc_irq_count);
704 enable_irq(info->gpmc_irq_fifo);
4e070376
SG
705
706 /* waiting for write to complete */
707 wait_for_completion(&info->comp);
5c468455 708
4e070376
SG
709 /* wait for data to flushed-out before reset the prefetch */
710 tim = 0;
711 limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
65b97cf6
AM
712 do {
713 val = readl(info->reg.gpmc_prefetch_status);
47f88af4 714 val = PREFETCH_STATUS_COUNT(val);
4e070376 715 cpu_relax();
65b97cf6 716 } while (val && (tim++ < limit));
4e070376
SG
717
718 /* disable and stop the PFPW engine */
65b97cf6 719 omap_prefetch_reset(info->gpmc_cs, info);
4e070376
SG
720 return;
721
722out_copy:
723 if (info->nand.options & NAND_BUSWIDTH_16)
724 omap_write_buf16(mtd, buf, len);
725 else
726 omap_write_buf8(mtd, buf, len);
727}
728
67ce04bf
VS
729/**
730 * gen_true_ecc - This function will generate true ECC value
731 * @ecc_buf: buffer to store ecc code
732 *
733 * This generated true ECC value can be used when correcting
734 * data read from NAND flash memory core
735 */
736static void gen_true_ecc(u8 *ecc_buf)
737{
738 u32 tmp = ecc_buf[0] | (ecc_buf[1] << 16) |
739 ((ecc_buf[2] & 0xF0) << 20) | ((ecc_buf[2] & 0x0F) << 8);
740
741 ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) |
742 P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp));
743 ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) |
744 P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
745 ecc_buf[2] = ~(P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) |
746 P1e(tmp) | P2048o(tmp) | P2048e(tmp));
747}
748
749/**
750 * omap_compare_ecc - Detect (2 bits) and correct (1 bit) error in data
751 * @ecc_data1: ecc code from nand spare area
752 * @ecc_data2: ecc code from hardware register obtained from hardware ecc
753 * @page_data: page data
754 *
755 * This function compares two ECC's and indicates if there is an error.
756 * If the error can be corrected it will be corrected to the buffer.
74f1b724
JO
757 * If there is no error, %0 is returned. If there is an error but it
758 * was corrected, %1 is returned. Otherwise, %-1 is returned.
67ce04bf
VS
759 */
760static int omap_compare_ecc(u8 *ecc_data1, /* read from NAND memory */
761 u8 *ecc_data2, /* read from register */
762 u8 *page_data)
763{
764 uint i;
765 u8 tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
766 u8 comp0_bit[8], comp1_bit[8], comp2_bit[8];
767 u8 ecc_bit[24];
768 u8 ecc_sum = 0;
769 u8 find_bit = 0;
770 uint find_byte = 0;
771 int isEccFF;
772
773 isEccFF = ((*(u32 *)ecc_data1 & 0xFFFFFF) == 0xFFFFFF);
774
775 gen_true_ecc(ecc_data1);
776 gen_true_ecc(ecc_data2);
777
778 for (i = 0; i <= 2; i++) {
779 *(ecc_data1 + i) = ~(*(ecc_data1 + i));
780 *(ecc_data2 + i) = ~(*(ecc_data2 + i));
781 }
782
783 for (i = 0; i < 8; i++) {
784 tmp0_bit[i] = *ecc_data1 % 2;
785 *ecc_data1 = *ecc_data1 / 2;
786 }
787
788 for (i = 0; i < 8; i++) {
789 tmp1_bit[i] = *(ecc_data1 + 1) % 2;
790 *(ecc_data1 + 1) = *(ecc_data1 + 1) / 2;
791 }
792
793 for (i = 0; i < 8; i++) {
794 tmp2_bit[i] = *(ecc_data1 + 2) % 2;
795 *(ecc_data1 + 2) = *(ecc_data1 + 2) / 2;
796 }
797
798 for (i = 0; i < 8; i++) {
799 comp0_bit[i] = *ecc_data2 % 2;
800 *ecc_data2 = *ecc_data2 / 2;
801 }
802
803 for (i = 0; i < 8; i++) {
804 comp1_bit[i] = *(ecc_data2 + 1) % 2;
805 *(ecc_data2 + 1) = *(ecc_data2 + 1) / 2;
806 }
807
808 for (i = 0; i < 8; i++) {
809 comp2_bit[i] = *(ecc_data2 + 2) % 2;
810 *(ecc_data2 + 2) = *(ecc_data2 + 2) / 2;
811 }
812
813 for (i = 0; i < 6; i++)
814 ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
815
816 for (i = 0; i < 8; i++)
817 ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
818
819 for (i = 0; i < 8; i++)
820 ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
821
822 ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
823 ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
824
825 for (i = 0; i < 24; i++)
826 ecc_sum += ecc_bit[i];
827
828 switch (ecc_sum) {
829 case 0:
830 /* Not reached because this function is not called if
831 * ECC values are equal
832 */
833 return 0;
834
835 case 1:
836 /* Uncorrectable error */
289c0522 837 pr_debug("ECC UNCORRECTED_ERROR 1\n");
6e941192 838 return -EBADMSG;
67ce04bf
VS
839
840 case 11:
841 /* UN-Correctable error */
289c0522 842 pr_debug("ECC UNCORRECTED_ERROR B\n");
6e941192 843 return -EBADMSG;
67ce04bf
VS
844
845 case 12:
846 /* Correctable error */
847 find_byte = (ecc_bit[23] << 8) +
848 (ecc_bit[21] << 7) +
849 (ecc_bit[19] << 6) +
850 (ecc_bit[17] << 5) +
851 (ecc_bit[15] << 4) +
852 (ecc_bit[13] << 3) +
853 (ecc_bit[11] << 2) +
854 (ecc_bit[9] << 1) +
855 ecc_bit[7];
856
857 find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
858
0a32a102
BN
859 pr_debug("Correcting single bit ECC error at offset: "
860 "%d, bit: %d\n", find_byte, find_bit);
67ce04bf
VS
861
862 page_data[find_byte] ^= (1 << find_bit);
863
74f1b724 864 return 1;
67ce04bf
VS
865 default:
866 if (isEccFF) {
867 if (ecc_data2[0] == 0 &&
868 ecc_data2[1] == 0 &&
869 ecc_data2[2] == 0)
870 return 0;
871 }
289c0522 872 pr_debug("UNCORRECTED_ERROR default\n");
6e941192 873 return -EBADMSG;
67ce04bf
VS
874 }
875}
876
877/**
878 * omap_correct_data - Compares the ECC read with HW generated ECC
879 * @mtd: MTD device structure
880 * @dat: page data
881 * @read_ecc: ecc read from nand flash
882 * @calc_ecc: ecc read from HW ECC registers
883 *
884 * Compares the ecc read from nand spare area with ECC registers values
74f1b724
JO
885 * and if ECC's mismatched, it will call 'omap_compare_ecc' for error
886 * detection and correction. If there are no errors, %0 is returned. If
887 * there were errors and all of the errors were corrected, the number of
888 * corrected errors is returned. If uncorrectable errors exist, %-1 is
889 * returned.
67ce04bf
VS
890 */
891static int omap_correct_data(struct mtd_info *mtd, u_char *dat,
892 u_char *read_ecc, u_char *calc_ecc)
893{
4578ea9a 894 struct omap_nand_info *info = mtd_to_omap(mtd);
67ce04bf 895 int blockCnt = 0, i = 0, ret = 0;
74f1b724 896 int stat = 0;
67ce04bf
VS
897
898 /* Ex NAND_ECC_HW12_2048 */
899 if ((info->nand.ecc.mode == NAND_ECC_HW) &&
900 (info->nand.ecc.size == 2048))
901 blockCnt = 4;
902 else
903 blockCnt = 1;
904
905 for (i = 0; i < blockCnt; i++) {
906 if (memcmp(read_ecc, calc_ecc, 3) != 0) {
907 ret = omap_compare_ecc(read_ecc, calc_ecc, dat);
908 if (ret < 0)
909 return ret;
74f1b724
JO
910 /* keep track of the number of corrected errors */
911 stat += ret;
67ce04bf
VS
912 }
913 read_ecc += 3;
914 calc_ecc += 3;
915 dat += 512;
916 }
74f1b724 917 return stat;
67ce04bf
VS
918}
919
920/**
921 * omap_calcuate_ecc - Generate non-inverted ECC bytes.
922 * @mtd: MTD device structure
923 * @dat: The pointer to data on which ecc is computed
924 * @ecc_code: The ecc_code buffer
925 *
926 * Using noninverted ECC can be considered ugly since writing a blank
927 * page ie. padding will clear the ECC bytes. This is no problem as long
928 * nobody is trying to write data on the seemingly unused page. Reading
929 * an erased page will produce an ECC mismatch between generated and read
930 * ECC bytes that has to be dealt with separately.
931 */
932static int omap_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
933 u_char *ecc_code)
934{
4578ea9a 935 struct omap_nand_info *info = mtd_to_omap(mtd);
65b97cf6
AM
936 u32 val;
937
938 val = readl(info->reg.gpmc_ecc_config);
40ddbf50 939 if (((val >> ECC_CONFIG_CS_SHIFT) & CS_MASK) != info->gpmc_cs)
65b97cf6
AM
940 return -EINVAL;
941
942 /* read ecc result */
943 val = readl(info->reg.gpmc_ecc1_result);
944 *ecc_code++ = val; /* P128e, ..., P1e */
945 *ecc_code++ = val >> 16; /* P128o, ..., P1o */
946 /* P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e */
947 *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0);
948
949 return 0;
67ce04bf
VS
950}
951
952/**
953 * omap_enable_hwecc - This function enables the hardware ecc functionality
954 * @mtd: MTD device structure
955 * @mode: Read/Write mode
956 */
957static void omap_enable_hwecc(struct mtd_info *mtd, int mode)
958{
4578ea9a 959 struct omap_nand_info *info = mtd_to_omap(mtd);
4bd4ebcc 960 struct nand_chip *chip = mtd_to_nand(mtd);
67ce04bf 961 unsigned int dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
65b97cf6
AM
962 u32 val;
963
964 /* clear ecc and enable bits */
965 val = ECCCLEAR | ECC1;
966 writel(val, info->reg.gpmc_ecc_control);
67ce04bf 967
65b97cf6
AM
968 /* program ecc and result sizes */
969 val = ((((info->nand.ecc.size >> 1) - 1) << ECCSIZE1_SHIFT) |
970 ECC1RESULTSIZE);
971 writel(val, info->reg.gpmc_ecc_size_config);
972
973 switch (mode) {
974 case NAND_ECC_READ:
975 case NAND_ECC_WRITE:
976 writel(ECCCLEAR | ECC1, info->reg.gpmc_ecc_control);
977 break;
978 case NAND_ECC_READSYN:
979 writel(ECCCLEAR, info->reg.gpmc_ecc_control);
980 break;
981 default:
982 dev_info(&info->pdev->dev,
983 "error: unrecognized Mode[%d]!\n", mode);
984 break;
985 }
67ce04bf 986
65b97cf6
AM
987 /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
988 val = (dev_width << 7) | (info->gpmc_cs << 1) | (0x1);
989 writel(val, info->reg.gpmc_ecc_config);
67ce04bf 990}
2c01946c 991
67ce04bf
VS
992/**
993 * omap_wait - wait until the command is done
994 * @mtd: MTD device structure
995 * @chip: NAND Chip structure
996 *
997 * Wait function is called during Program and erase operations and
998 * the way it is called from MTD layer, we should wait till the NAND
999 * chip is ready after the programming/erase operation has completed.
1000 *
1001 * Erase can take up to 400ms and program up to 20ms according to
1002 * general NAND and SmartMedia specs
1003 */
1004static int omap_wait(struct mtd_info *mtd, struct nand_chip *chip)
1005{
4bd4ebcc 1006 struct nand_chip *this = mtd_to_nand(mtd);
4578ea9a 1007 struct omap_nand_info *info = mtd_to_omap(mtd);
67ce04bf 1008 unsigned long timeo = jiffies;
a9c465f0 1009 int status, state = this->state;
67ce04bf
VS
1010
1011 if (state == FL_ERASING)
4ff6772b 1012 timeo += msecs_to_jiffies(400);
67ce04bf 1013 else
4ff6772b 1014 timeo += msecs_to_jiffies(20);
67ce04bf 1015
65b97cf6 1016 writeb(NAND_CMD_STATUS & 0xFF, info->reg.gpmc_nand_command);
67ce04bf 1017 while (time_before(jiffies, timeo)) {
65b97cf6 1018 status = readb(info->reg.gpmc_nand_data);
c276aca4 1019 if (status & NAND_STATUS_READY)
67ce04bf 1020 break;
c276aca4 1021 cond_resched();
67ce04bf 1022 }
a9c465f0 1023
4ea1e4ba 1024 status = readb(info->reg.gpmc_nand_data);
67ce04bf
VS
1025 return status;
1026}
1027
1028/**
10f22ee3 1029 * omap_dev_ready - checks the NAND Ready GPIO line
67ce04bf 1030 * @mtd: MTD device structure
10f22ee3
RQ
1031 *
1032 * Returns true if ready and false if busy.
67ce04bf
VS
1033 */
1034static int omap_dev_ready(struct mtd_info *mtd)
1035{
4578ea9a 1036 struct omap_nand_info *info = mtd_to_omap(mtd);
67ce04bf 1037
10f22ee3 1038 return gpiod_get_value(info->ready_gpiod);
67ce04bf
VS
1039}
1040
0e618ef0 1041/**
7c977c3e 1042 * omap_enable_hwecc_bch - Program GPMC to perform BCH ECC calculation
0e618ef0
ID
1043 * @mtd: MTD device structure
1044 * @mode: Read/Write mode
62116e51 1045 *
0760e818
NMG
1046 * When using BCH with SW correction (i.e. no ELM), sector size is set
1047 * to 512 bytes and we use BCH_WRAPMODE_6 wrapping mode
1048 * for both reading and writing with:
62116e51
PA
1049 * eccsize0 = 0 (no additional protected byte in spare area)
1050 * eccsize1 = 32 (skip 32 nibbles = 16 bytes per sector in spare area)
0e618ef0 1051 */
7c977c3e 1052static void __maybe_unused omap_enable_hwecc_bch(struct mtd_info *mtd, int mode)
0e618ef0 1053{
16e69322 1054 unsigned int bch_type;
2ef9f3dd 1055 unsigned int dev_width, nsectors;
4578ea9a 1056 struct omap_nand_info *info = mtd_to_omap(mtd);
c5957a32 1057 enum omap_ecc ecc_opt = info->ecc_opt;
4bd4ebcc 1058 struct nand_chip *chip = mtd_to_nand(mtd);
62116e51
PA
1059 u32 val, wr_mode;
1060 unsigned int ecc_size1, ecc_size0;
1061
c5957a32
PG
1062 /* GPMC configurations for calculating ECC */
1063 switch (ecc_opt) {
1064 case OMAP_ECC_BCH4_CODE_HW_DETECTION_SW:
16e69322
PG
1065 bch_type = 0;
1066 nsectors = 1;
0760e818
NMG
1067 wr_mode = BCH_WRAPMODE_6;
1068 ecc_size0 = BCH_ECC_SIZE0;
1069 ecc_size1 = BCH_ECC_SIZE1;
c5957a32
PG
1070 break;
1071 case OMAP_ECC_BCH4_CODE_HW:
16e69322
PG
1072 bch_type = 0;
1073 nsectors = chip->ecc.steps;
c5957a32
PG
1074 if (mode == NAND_ECC_READ) {
1075 wr_mode = BCH_WRAPMODE_1;
1076 ecc_size0 = BCH4R_ECC_SIZE0;
1077 ecc_size1 = BCH4R_ECC_SIZE1;
1078 } else {
1079 wr_mode = BCH_WRAPMODE_6;
1080 ecc_size0 = BCH_ECC_SIZE0;
1081 ecc_size1 = BCH_ECC_SIZE1;
1082 }
1083 break;
1084 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
16e69322
PG
1085 bch_type = 1;
1086 nsectors = 1;
0760e818
NMG
1087 wr_mode = BCH_WRAPMODE_6;
1088 ecc_size0 = BCH_ECC_SIZE0;
1089 ecc_size1 = BCH_ECC_SIZE1;
c5957a32
PG
1090 break;
1091 case OMAP_ECC_BCH8_CODE_HW:
16e69322
PG
1092 bch_type = 1;
1093 nsectors = chip->ecc.steps;
c5957a32
PG
1094 if (mode == NAND_ECC_READ) {
1095 wr_mode = BCH_WRAPMODE_1;
1096 ecc_size0 = BCH8R_ECC_SIZE0;
1097 ecc_size1 = BCH8R_ECC_SIZE1;
1098 } else {
1099 wr_mode = BCH_WRAPMODE_6;
1100 ecc_size0 = BCH_ECC_SIZE0;
1101 ecc_size1 = BCH_ECC_SIZE1;
1102 }
1103 break;
9748fff9 1104 case OMAP_ECC_BCH16_CODE_HW:
1105 bch_type = 0x2;
1106 nsectors = chip->ecc.steps;
1107 if (mode == NAND_ECC_READ) {
1108 wr_mode = 0x01;
1109 ecc_size0 = 52; /* ECC bits in nibbles per sector */
1110 ecc_size1 = 0; /* non-ECC bits in nibbles per sector */
1111 } else {
1112 wr_mode = 0x01;
1113 ecc_size0 = 0; /* extra bits in nibbles per sector */
1114 ecc_size1 = 52; /* OOB bits in nibbles per sector */
1115 }
1116 break;
c5957a32
PG
1117 default:
1118 return;
1119 }
2ef9f3dd
AM
1120
1121 writel(ECC1, info->reg.gpmc_ecc_control);
1122
62116e51
PA
1123 /* Configure ecc size for BCH */
1124 val = (ecc_size1 << ECCSIZE1_SHIFT) | (ecc_size0 << ECCSIZE0_SHIFT);
2ef9f3dd
AM
1125 writel(val, info->reg.gpmc_ecc_size_config);
1126
62116e51
PA
1127 dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
1128
2ef9f3dd
AM
1129 /* BCH configuration */
1130 val = ((1 << 16) | /* enable BCH */
16e69322 1131 (bch_type << 12) | /* BCH4/BCH8/BCH16 */
62116e51 1132 (wr_mode << 8) | /* wrap mode */
2ef9f3dd
AM
1133 (dev_width << 7) | /* bus width */
1134 (((nsectors-1) & 0x7) << 4) | /* number of sectors */
1135 (info->gpmc_cs << 1) | /* ECC CS */
1136 (0x1)); /* enable ECC */
1137
1138 writel(val, info->reg.gpmc_ecc_config);
1139
62116e51 1140 /* Clear ecc and enable bits */
2ef9f3dd 1141 writel(ECCCLEAR | ECC1, info->reg.gpmc_ecc_control);
0e618ef0 1142}
7c977c3e 1143
2c9f2365 1144static u8 bch4_polynomial[] = {0x28, 0x13, 0xcc, 0x39, 0x96, 0xac, 0x7f};
7bcd1dca
PG
1145static u8 bch8_polynomial[] = {0xef, 0x51, 0x2e, 0x09, 0xed, 0x93, 0x9a, 0xc2,
1146 0x97, 0x79, 0xe5, 0x24, 0xb5};
0e618ef0 1147
62116e51 1148/**
a4c7ca00 1149 * omap_calculate_ecc_bch - Generate bytes of ECC bytes
62116e51
PA
1150 * @mtd: MTD device structure
1151 * @dat: The pointer to data on which ecc is computed
1152 * @ecc_code: The ecc_code buffer
1153 *
1154 * Support calculating of BCH4/8 ecc vectors for the page
1155 */
a4c7ca00 1156static int __maybe_unused omap_calculate_ecc_bch(struct mtd_info *mtd,
f5dc06fb 1157 const u_char *dat, u_char *ecc_calc)
62116e51 1158{
4578ea9a 1159 struct omap_nand_info *info = mtd_to_omap(mtd);
f5dc06fb
PG
1160 int eccbytes = info->nand.ecc.bytes;
1161 struct gpmc_nand_regs *gpmc_regs = &info->reg;
1162 u8 *ecc_code;
62116e51 1163 unsigned long nsectors, bch_val1, bch_val2, bch_val3, bch_val4;
9748fff9 1164 u32 val;
2913aae5 1165 int i, j;
62116e51
PA
1166
1167 nsectors = ((readl(info->reg.gpmc_ecc_config) >> 4) & 0x7) + 1;
62116e51 1168 for (i = 0; i < nsectors; i++) {
f5dc06fb
PG
1169 ecc_code = ecc_calc;
1170 switch (info->ecc_opt) {
7bcd1dca 1171 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
f5dc06fb
PG
1172 case OMAP_ECC_BCH8_CODE_HW:
1173 bch_val1 = readl(gpmc_regs->gpmc_bch_result0[i]);
1174 bch_val2 = readl(gpmc_regs->gpmc_bch_result1[i]);
1175 bch_val3 = readl(gpmc_regs->gpmc_bch_result2[i]);
1176 bch_val4 = readl(gpmc_regs->gpmc_bch_result3[i]);
62116e51
PA
1177 *ecc_code++ = (bch_val4 & 0xFF);
1178 *ecc_code++ = ((bch_val3 >> 24) & 0xFF);
1179 *ecc_code++ = ((bch_val3 >> 16) & 0xFF);
1180 *ecc_code++ = ((bch_val3 >> 8) & 0xFF);
1181 *ecc_code++ = (bch_val3 & 0xFF);
1182 *ecc_code++ = ((bch_val2 >> 24) & 0xFF);
1183 *ecc_code++ = ((bch_val2 >> 16) & 0xFF);
1184 *ecc_code++ = ((bch_val2 >> 8) & 0xFF);
1185 *ecc_code++ = (bch_val2 & 0xFF);
1186 *ecc_code++ = ((bch_val1 >> 24) & 0xFF);
1187 *ecc_code++ = ((bch_val1 >> 16) & 0xFF);
1188 *ecc_code++ = ((bch_val1 >> 8) & 0xFF);
1189 *ecc_code++ = (bch_val1 & 0xFF);
f5dc06fb 1190 break;
2c9f2365 1191 case OMAP_ECC_BCH4_CODE_HW_DETECTION_SW:
f5dc06fb
PG
1192 case OMAP_ECC_BCH4_CODE_HW:
1193 bch_val1 = readl(gpmc_regs->gpmc_bch_result0[i]);
1194 bch_val2 = readl(gpmc_regs->gpmc_bch_result1[i]);
62116e51
PA
1195 *ecc_code++ = ((bch_val2 >> 12) & 0xFF);
1196 *ecc_code++ = ((bch_val2 >> 4) & 0xFF);
1197 *ecc_code++ = ((bch_val2 & 0xF) << 4) |
1198 ((bch_val1 >> 28) & 0xF);
1199 *ecc_code++ = ((bch_val1 >> 20) & 0xFF);
1200 *ecc_code++ = ((bch_val1 >> 12) & 0xFF);
1201 *ecc_code++ = ((bch_val1 >> 4) & 0xFF);
1202 *ecc_code++ = ((bch_val1 & 0xF) << 4);
f5dc06fb 1203 break;
9748fff9 1204 case OMAP_ECC_BCH16_CODE_HW:
1205 val = readl(gpmc_regs->gpmc_bch_result6[i]);
1206 ecc_code[0] = ((val >> 8) & 0xFF);
1207 ecc_code[1] = ((val >> 0) & 0xFF);
1208 val = readl(gpmc_regs->gpmc_bch_result5[i]);
1209 ecc_code[2] = ((val >> 24) & 0xFF);
1210 ecc_code[3] = ((val >> 16) & 0xFF);
1211 ecc_code[4] = ((val >> 8) & 0xFF);
1212 ecc_code[5] = ((val >> 0) & 0xFF);
1213 val = readl(gpmc_regs->gpmc_bch_result4[i]);
1214 ecc_code[6] = ((val >> 24) & 0xFF);
1215 ecc_code[7] = ((val >> 16) & 0xFF);
1216 ecc_code[8] = ((val >> 8) & 0xFF);
1217 ecc_code[9] = ((val >> 0) & 0xFF);
1218 val = readl(gpmc_regs->gpmc_bch_result3[i]);
1219 ecc_code[10] = ((val >> 24) & 0xFF);
1220 ecc_code[11] = ((val >> 16) & 0xFF);
1221 ecc_code[12] = ((val >> 8) & 0xFF);
1222 ecc_code[13] = ((val >> 0) & 0xFF);
1223 val = readl(gpmc_regs->gpmc_bch_result2[i]);
1224 ecc_code[14] = ((val >> 24) & 0xFF);
1225 ecc_code[15] = ((val >> 16) & 0xFF);
1226 ecc_code[16] = ((val >> 8) & 0xFF);
1227 ecc_code[17] = ((val >> 0) & 0xFF);
1228 val = readl(gpmc_regs->gpmc_bch_result1[i]);
1229 ecc_code[18] = ((val >> 24) & 0xFF);
1230 ecc_code[19] = ((val >> 16) & 0xFF);
1231 ecc_code[20] = ((val >> 8) & 0xFF);
1232 ecc_code[21] = ((val >> 0) & 0xFF);
1233 val = readl(gpmc_regs->gpmc_bch_result0[i]);
1234 ecc_code[22] = ((val >> 24) & 0xFF);
1235 ecc_code[23] = ((val >> 16) & 0xFF);
1236 ecc_code[24] = ((val >> 8) & 0xFF);
1237 ecc_code[25] = ((val >> 0) & 0xFF);
1238 break;
f5dc06fb
PG
1239 default:
1240 return -EINVAL;
62116e51 1241 }
f5dc06fb
PG
1242
1243 /* ECC scheme specific syndrome customizations */
1244 switch (info->ecc_opt) {
2c9f2365
PG
1245 case OMAP_ECC_BCH4_CODE_HW_DETECTION_SW:
1246 /* Add constant polynomial to remainder, so that
1247 * ECC of blank pages results in 0x0 on reading back */
2913aae5
TJ
1248 for (j = 0; j < eccbytes; j++)
1249 ecc_calc[j] ^= bch4_polynomial[j];
2c9f2365 1250 break;
f5dc06fb
PG
1251 case OMAP_ECC_BCH4_CODE_HW:
1252 /* Set 8th ECC byte as 0x0 for ROM compatibility */
1253 ecc_calc[eccbytes - 1] = 0x0;
1254 break;
7bcd1dca
PG
1255 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
1256 /* Add constant polynomial to remainder, so that
1257 * ECC of blank pages results in 0x0 on reading back */
2913aae5
TJ
1258 for (j = 0; j < eccbytes; j++)
1259 ecc_calc[j] ^= bch8_polynomial[j];
7bcd1dca 1260 break;
f5dc06fb
PG
1261 case OMAP_ECC_BCH8_CODE_HW:
1262 /* Set 14th ECC byte as 0x0 for ROM compatibility */
1263 ecc_calc[eccbytes - 1] = 0x0;
1264 break;
9748fff9 1265 case OMAP_ECC_BCH16_CODE_HW:
1266 break;
f5dc06fb
PG
1267 default:
1268 return -EINVAL;
1269 }
1270
1271 ecc_calc += eccbytes;
62116e51
PA
1272 }
1273
1274 return 0;
1275}
1276
1277/**
1278 * erased_sector_bitflips - count bit flips
1279 * @data: data sector buffer
1280 * @oob: oob buffer
1281 * @info: omap_nand_info
1282 *
1283 * Check the bit flips in erased page falls below correctable level.
1284 * If falls below, report the page as erased with correctable bit
1285 * flip, else report as uncorrectable page.
1286 */
1287static int erased_sector_bitflips(u_char *data, u_char *oob,
1288 struct omap_nand_info *info)
1289{
1290 int flip_bits = 0, i;
1291
1292 for (i = 0; i < info->nand.ecc.size; i++) {
1293 flip_bits += hweight8(~data[i]);
1294 if (flip_bits > info->nand.ecc.strength)
1295 return 0;
1296 }
1297
1298 for (i = 0; i < info->nand.ecc.bytes - 1; i++) {
1299 flip_bits += hweight8(~oob[i]);
1300 if (flip_bits > info->nand.ecc.strength)
1301 return 0;
1302 }
1303
1304 /*
1305 * Bit flips falls in correctable level.
1306 * Fill data area with 0xFF
1307 */
1308 if (flip_bits) {
1309 memset(data, 0xFF, info->nand.ecc.size);
1310 memset(oob, 0xFF, info->nand.ecc.bytes);
1311 }
1312
1313 return flip_bits;
1314}
1315
1316/**
1317 * omap_elm_correct_data - corrects page data area in case error reported
1318 * @mtd: MTD device structure
1319 * @data: page data
1320 * @read_ecc: ecc read from nand flash
1321 * @calc_ecc: ecc read from HW ECC registers
1322 *
1323 * Calculated ecc vector reported as zero in case of non-error pages.
78f43c53
PG
1324 * In case of non-zero ecc vector, first filter out erased-pages, and
1325 * then process data via ELM to detect bit-flips.
62116e51
PA
1326 */
1327static int omap_elm_correct_data(struct mtd_info *mtd, u_char *data,
1328 u_char *read_ecc, u_char *calc_ecc)
1329{
4578ea9a 1330 struct omap_nand_info *info = mtd_to_omap(mtd);
de0a4d69 1331 struct nand_ecc_ctrl *ecc = &info->nand.ecc;
62116e51
PA
1332 int eccsteps = info->nand.ecc.steps;
1333 int i , j, stat = 0;
de0a4d69 1334 int eccflag, actual_eccbytes;
62116e51
PA
1335 struct elm_errorvec err_vec[ERROR_VECTOR_MAX];
1336 u_char *ecc_vec = calc_ecc;
1337 u_char *spare_ecc = read_ecc;
1338 u_char *erased_ecc_vec;
78f43c53
PG
1339 u_char *buf;
1340 int bitflip_count;
62116e51 1341 bool is_error_reported = false;
b08e1f63 1342 u32 bit_pos, byte_pos, error_max, pos;
13fbe064 1343 int err;
62116e51 1344
de0a4d69
PG
1345 switch (info->ecc_opt) {
1346 case OMAP_ECC_BCH4_CODE_HW:
1347 /* omit 7th ECC byte reserved for ROM code compatibility */
1348 actual_eccbytes = ecc->bytes - 1;
78f43c53 1349 erased_ecc_vec = bch4_vector;
de0a4d69
PG
1350 break;
1351 case OMAP_ECC_BCH8_CODE_HW:
1352 /* omit 14th ECC byte reserved for ROM code compatibility */
1353 actual_eccbytes = ecc->bytes - 1;
78f43c53 1354 erased_ecc_vec = bch8_vector;
de0a4d69 1355 break;
9748fff9 1356 case OMAP_ECC_BCH16_CODE_HW:
1357 actual_eccbytes = ecc->bytes;
1358 erased_ecc_vec = bch16_vector;
1359 break;
de0a4d69 1360 default:
d2f08c75 1361 dev_err(&info->pdev->dev, "invalid driver configuration\n");
de0a4d69
PG
1362 return -EINVAL;
1363 }
1364
62116e51
PA
1365 /* Initialize elm error vector to zero */
1366 memset(err_vec, 0, sizeof(err_vec));
1367
62116e51
PA
1368 for (i = 0; i < eccsteps ; i++) {
1369 eccflag = 0; /* initialize eccflag */
1370
1371 /*
1372 * Check any error reported,
1373 * In case of error, non zero ecc reported.
1374 */
de0a4d69 1375 for (j = 0; j < actual_eccbytes; j++) {
62116e51
PA
1376 if (calc_ecc[j] != 0) {
1377 eccflag = 1; /* non zero ecc, error present */
1378 break;
1379 }
1380 }
1381
1382 if (eccflag == 1) {
78f43c53
PG
1383 if (memcmp(calc_ecc, erased_ecc_vec,
1384 actual_eccbytes) == 0) {
62116e51 1385 /*
78f43c53
PG
1386 * calc_ecc[] matches pattern for ECC(all 0xff)
1387 * so this is definitely an erased-page
62116e51 1388 */
62116e51 1389 } else {
78f43c53
PG
1390 buf = &data[info->nand.ecc.size * i];
1391 /*
1392 * count number of 0-bits in read_buf.
1393 * This check can be removed once a similar
1394 * check is introduced in generic NAND driver
1395 */
1396 bitflip_count = erased_sector_bitflips(
1397 buf, read_ecc, info);
1398 if (bitflip_count) {
1399 /*
1400 * number of 0-bits within ECC limits
1401 * So this may be an erased-page
1402 */
1403 stat += bitflip_count;
1404 } else {
1405 /*
1406 * Too many 0-bits. It may be a
1407 * - programmed-page, OR
1408 * - erased-page with many bit-flips
1409 * So this page requires check by ELM
1410 */
1411 err_vec[i].error_reported = true;
1412 is_error_reported = true;
62116e51
PA
1413 }
1414 }
1415 }
1416
1417 /* Update the ecc vector */
de0a4d69
PG
1418 calc_ecc += ecc->bytes;
1419 read_ecc += ecc->bytes;
62116e51
PA
1420 }
1421
1422 /* Check if any error reported */
1423 if (!is_error_reported)
f306e8c3 1424 return stat;
62116e51
PA
1425
1426 /* Decode BCH error using ELM module */
1427 elm_decode_bch_error_page(info->elm_dev, ecc_vec, err_vec);
1428
13fbe064 1429 err = 0;
62116e51 1430 for (i = 0; i < eccsteps; i++) {
13fbe064 1431 if (err_vec[i].error_uncorrectable) {
d2f08c75
EG
1432 dev_err(&info->pdev->dev,
1433 "uncorrectable bit-flips found\n");
13fbe064
PG
1434 err = -EBADMSG;
1435 } else if (err_vec[i].error_reported) {
62116e51 1436 for (j = 0; j < err_vec[i].error_count; j++) {
b08e1f63
PG
1437 switch (info->ecc_opt) {
1438 case OMAP_ECC_BCH4_CODE_HW:
1439 /* Add 4 bits to take care of padding */
62116e51
PA
1440 pos = err_vec[i].error_loc[j] +
1441 BCH4_BIT_PAD;
b08e1f63
PG
1442 break;
1443 case OMAP_ECC_BCH8_CODE_HW:
9748fff9 1444 case OMAP_ECC_BCH16_CODE_HW:
b08e1f63
PG
1445 pos = err_vec[i].error_loc[j];
1446 break;
1447 default:
1448 return -EINVAL;
1449 }
1450 error_max = (ecc->size + actual_eccbytes) * 8;
62116e51
PA
1451 /* Calculate bit position of error */
1452 bit_pos = pos % 8;
1453
1454 /* Calculate byte position of error */
1455 byte_pos = (error_max - pos - 1) / 8;
1456
1457 if (pos < error_max) {
13fbe064
PG
1458 if (byte_pos < 512) {
1459 pr_debug("bitflip@dat[%d]=%x\n",
1460 byte_pos, data[byte_pos]);
62116e51 1461 data[byte_pos] ^= 1 << bit_pos;
13fbe064
PG
1462 } else {
1463 pr_debug("bitflip@oob[%d]=%x\n",
1464 (byte_pos - 512),
1465 spare_ecc[byte_pos - 512]);
62116e51
PA
1466 spare_ecc[byte_pos - 512] ^=
1467 1 << bit_pos;
13fbe064
PG
1468 }
1469 } else {
d2f08c75
EG
1470 dev_err(&info->pdev->dev,
1471 "invalid bit-flip @ %d:%d\n",
1472 byte_pos, bit_pos);
13fbe064 1473 err = -EBADMSG;
62116e51 1474 }
62116e51
PA
1475 }
1476 }
1477
1478 /* Update number of correctable errors */
1479 stat += err_vec[i].error_count;
1480
1481 /* Update page data with sector size */
b08e1f63 1482 data += ecc->size;
de0a4d69 1483 spare_ecc += ecc->bytes;
62116e51
PA
1484 }
1485
13fbe064 1486 return (err) ? err : stat;
62116e51
PA
1487}
1488
62116e51
PA
1489/**
1490 * omap_write_page_bch - BCH ecc based write page function for entire page
1491 * @mtd: mtd info structure
1492 * @chip: nand chip info structure
1493 * @buf: data buffer
1494 * @oob_required: must write chip->oob_poi to OOB
45aaeff9 1495 * @page: page
62116e51
PA
1496 *
1497 * Custom write page method evolved to support multi sector writing in one shot
1498 */
1499static int omap_write_page_bch(struct mtd_info *mtd, struct nand_chip *chip,
45aaeff9 1500 const uint8_t *buf, int oob_required, int page)
62116e51
PA
1501{
1502 int i;
1503 uint8_t *ecc_calc = chip->buffers->ecccalc;
1504 uint32_t *eccpos = chip->ecc.layout->eccpos;
1505
1506 /* Enable GPMC ecc engine */
1507 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1508
1509 /* Write data */
1510 chip->write_buf(mtd, buf, mtd->writesize);
1511
1512 /* Update ecc vector from GPMC result registers */
1513 chip->ecc.calculate(mtd, buf, &ecc_calc[0]);
1514
1515 for (i = 0; i < chip->ecc.total; i++)
1516 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1517
1518 /* Write ecc vector to OOB area */
1519 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1520 return 0;
1521}
1522
1523/**
1524 * omap_read_page_bch - BCH ecc based page read function for entire page
1525 * @mtd: mtd info structure
1526 * @chip: nand chip info structure
1527 * @buf: buffer to store read data
1528 * @oob_required: caller requires OOB data read to chip->oob_poi
1529 * @page: page number to read
1530 *
1531 * For BCH ecc scheme, GPMC used for syndrome calculation and ELM module
1532 * used for error correction.
1533 * Custom method evolved to support ELM error correction & multi sector
1534 * reading. On reading page data area is read along with OOB data with
1535 * ecc engine enabled. ecc vector updated after read of OOB data.
1536 * For non error pages ecc vector reported as zero.
1537 */
1538static int omap_read_page_bch(struct mtd_info *mtd, struct nand_chip *chip,
1539 uint8_t *buf, int oob_required, int page)
1540{
1541 uint8_t *ecc_calc = chip->buffers->ecccalc;
1542 uint8_t *ecc_code = chip->buffers->ecccode;
1543 uint32_t *eccpos = chip->ecc.layout->eccpos;
1544 uint8_t *oob = &chip->oob_poi[eccpos[0]];
1545 uint32_t oob_pos = mtd->writesize + chip->ecc.layout->eccpos[0];
1546 int stat;
1547 unsigned int max_bitflips = 0;
1548
1549 /* Enable GPMC ecc engine */
1550 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1551
1552 /* Read data */
1553 chip->read_buf(mtd, buf, mtd->writesize);
1554
1555 /* Read oob bytes */
1556 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_pos, -1);
1557 chip->read_buf(mtd, oob, chip->ecc.total);
1558
1559 /* Calculate ecc bytes */
1560 chip->ecc.calculate(mtd, buf, ecc_calc);
1561
1562 memcpy(ecc_code, &chip->oob_poi[eccpos[0]], chip->ecc.total);
1563
1564 stat = chip->ecc.correct(mtd, buf, ecc_code, ecc_calc);
1565
1566 if (stat < 0) {
1567 mtd->ecc_stats.failed++;
1568 } else {
1569 mtd->ecc_stats.corrected += stat;
1570 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1571 }
1572
1573 return max_bitflips;
1574}
1575
0e618ef0 1576/**
a919e511
PG
1577 * is_elm_present - checks for presence of ELM module by scanning DT nodes
1578 * @omap_nand_info: NAND device structure containing platform data
0e618ef0 1579 */
93af53b8
EG
1580static bool is_elm_present(struct omap_nand_info *info,
1581 struct device_node *elm_node)
0e618ef0 1582{
a919e511 1583 struct platform_device *pdev;
93af53b8 1584
a919e511
PG
1585 /* check whether elm-id is passed via DT */
1586 if (!elm_node) {
d2f08c75 1587 dev_err(&info->pdev->dev, "ELM devicetree node not found\n");
93af53b8 1588 return false;
a919e511
PG
1589 }
1590 pdev = of_find_device_by_node(elm_node);
1591 /* check whether ELM device is registered */
1592 if (!pdev) {
d2f08c75 1593 dev_err(&info->pdev->dev, "ELM device not found\n");
93af53b8 1594 return false;
0e618ef0 1595 }
a919e511
PG
1596 /* ELM module available, now configure it */
1597 info->elm_dev = &pdev->dev;
93af53b8
EG
1598 return true;
1599}
3f4eb14b 1600
93af53b8
EG
1601static bool omap2_nand_ecc_check(struct omap_nand_info *info,
1602 struct omap_nand_platform_data *pdata)
1603{
1604 bool ecc_needs_bch, ecc_needs_omap_bch, ecc_needs_elm;
1605
1606 switch (info->ecc_opt) {
1607 case OMAP_ECC_BCH4_CODE_HW_DETECTION_SW:
1608 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
1609 ecc_needs_omap_bch = false;
1610 ecc_needs_bch = true;
1611 ecc_needs_elm = false;
1612 break;
1613 case OMAP_ECC_BCH4_CODE_HW:
1614 case OMAP_ECC_BCH8_CODE_HW:
1615 case OMAP_ECC_BCH16_CODE_HW:
1616 ecc_needs_omap_bch = true;
1617 ecc_needs_bch = false;
1618 ecc_needs_elm = true;
1619 break;
1620 default:
1621 ecc_needs_omap_bch = false;
1622 ecc_needs_bch = false;
1623 ecc_needs_elm = false;
1624 break;
1625 }
1626
1627 if (ecc_needs_bch && !IS_ENABLED(CONFIG_MTD_NAND_ECC_BCH)) {
1628 dev_err(&info->pdev->dev,
1629 "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
1630 return false;
1631 }
1632 if (ecc_needs_omap_bch && !IS_ENABLED(CONFIG_MTD_NAND_OMAP_BCH)) {
1633 dev_err(&info->pdev->dev,
1634 "CONFIG_MTD_NAND_OMAP_BCH not enabled\n");
1635 return false;
1636 }
01b95fc6 1637 if (ecc_needs_elm && !is_elm_present(info, info->elm_of_node)) {
93af53b8
EG
1638 dev_err(&info->pdev->dev, "ELM not available\n");
1639 return false;
1640 }
1641
1642 return true;
0e618ef0
ID
1643}
1644
c9711ec5
RQ
1645static const char * const nand_xfer_types[] = {
1646 [NAND_OMAP_PREFETCH_POLLED] = "prefetch-polled",
1647 [NAND_OMAP_POLLED] = "polled",
1648 [NAND_OMAP_PREFETCH_DMA] = "prefetch-dma",
1649 [NAND_OMAP_PREFETCH_IRQ] = "prefetch-irq",
1650};
1651
1652static int omap_get_dt_info(struct device *dev, struct omap_nand_info *info)
1653{
1654 struct device_node *child = dev->of_node;
1655 int i;
1656 const char *s;
1657 u32 cs;
1658
1659 if (of_property_read_u32(child, "reg", &cs) < 0) {
1660 dev_err(dev, "reg not found in DT\n");
1661 return -EINVAL;
1662 }
1663
1664 info->gpmc_cs = cs;
1665
1666 /* detect availability of ELM module. Won't be present pre-OMAP4 */
1667 info->elm_of_node = of_parse_phandle(child, "ti,elm-id", 0);
1668 if (!info->elm_of_node)
1669 dev_dbg(dev, "ti,elm-id not in DT\n");
1670
1671 /* select ecc-scheme for NAND */
1672 if (of_property_read_string(child, "ti,nand-ecc-opt", &s)) {
1673 dev_err(dev, "ti,nand-ecc-opt not found\n");
1674 return -EINVAL;
1675 }
1676
1677 if (!strcmp(s, "sw")) {
1678 info->ecc_opt = OMAP_ECC_HAM1_CODE_SW;
1679 } else if (!strcmp(s, "ham1") ||
1680 !strcmp(s, "hw") || !strcmp(s, "hw-romcode")) {
1681 info->ecc_opt = OMAP_ECC_HAM1_CODE_HW;
1682 } else if (!strcmp(s, "bch4")) {
1683 if (info->elm_of_node)
1684 info->ecc_opt = OMAP_ECC_BCH4_CODE_HW;
1685 else
1686 info->ecc_opt = OMAP_ECC_BCH4_CODE_HW_DETECTION_SW;
1687 } else if (!strcmp(s, "bch8")) {
1688 if (info->elm_of_node)
1689 info->ecc_opt = OMAP_ECC_BCH8_CODE_HW;
1690 else
1691 info->ecc_opt = OMAP_ECC_BCH8_CODE_HW_DETECTION_SW;
1692 } else if (!strcmp(s, "bch16")) {
1693 info->ecc_opt = OMAP_ECC_BCH16_CODE_HW;
1694 } else {
1695 dev_err(dev, "unrecognized value for ti,nand-ecc-opt\n");
1696 return -EINVAL;
1697 }
1698
1699 /* select data transfer mode */
1700 if (!of_property_read_string(child, "ti,nand-xfer-type", &s)) {
1701 for (i = 0; i < ARRAY_SIZE(nand_xfer_types); i++) {
1702 if (!strcasecmp(s, nand_xfer_types[i])) {
1703 info->xfer_type = i;
1704 goto next;
1705 }
1706 }
1707
1708 dev_err(dev, "unrecognized value for ti,nand-xfer-type\n");
1709 return -EINVAL;
1710 }
1711
1712next:
1713 of_get_nand_on_flash_bbt(child);
1714
1715 if (of_get_nand_bus_width(child) == 16)
1716 info->devsize = NAND_BUSWIDTH_16;
1717
1718 return 0;
1719}
1720
06f25510 1721static int omap_nand_probe(struct platform_device *pdev)
67ce04bf
VS
1722{
1723 struct omap_nand_info *info;
c9711ec5 1724 struct omap_nand_platform_data *pdata = NULL;
633deb58
PG
1725 struct mtd_info *mtd;
1726 struct nand_chip *nand_chip;
b491da72 1727 struct nand_ecclayout *ecclayout;
67ce04bf 1728 int err;
b491da72 1729 int i;
633deb58
PG
1730 dma_cap_mask_t mask;
1731 unsigned sig;
eae39cb4 1732 unsigned oob_index;
9c4c2f8b 1733 struct resource *res;
c9711ec5 1734 struct device *dev = &pdev->dev;
67ce04bf 1735
70ba6d71
PG
1736 info = devm_kzalloc(&pdev->dev, sizeof(struct omap_nand_info),
1737 GFP_KERNEL);
67ce04bf
VS
1738 if (!info)
1739 return -ENOMEM;
1740
c9711ec5 1741 info->pdev = pdev;
67ce04bf 1742
c9711ec5
RQ
1743 if (dev->of_node) {
1744 if (omap_get_dt_info(dev, info))
1745 return -EINVAL;
1746 } else {
1747 pdata = dev_get_platdata(&pdev->dev);
1748 if (!pdata) {
1749 dev_err(&pdev->dev, "platform data missing\n");
1750 return -EINVAL;
1751 }
1752
1753 info->gpmc_cs = pdata->cs;
1754 info->reg = pdata->reg;
1755 info->ecc_opt = pdata->ecc_opt;
10f22ee3
RQ
1756 if (pdata->dev_ready)
1757 dev_info(&pdev->dev, "pdata->dev_ready is deprecated\n");
1758
c9711ec5
RQ
1759 info->xfer_type = pdata->xfer_type;
1760 info->devsize = pdata->devsize;
1761 info->elm_of_node = pdata->elm_of_node;
1762 info->flash_bbt = pdata->flash_bbt;
1763 }
1764
1765 platform_set_drvdata(pdev, info);
c509aefd
RQ
1766 info->ops = gpmc_omap_get_nand_ops(&info->reg, info->gpmc_cs);
1767 if (!info->ops) {
1768 dev_err(&pdev->dev, "Failed to get GPMC->NAND interface\n");
1769 return -ENODEV;
1770 }
01b95fc6 1771
432420c0
BB
1772 nand_chip = &info->nand;
1773 mtd = nand_to_mtd(nand_chip);
853f1c58 1774 mtd->dev.parent = &pdev->dev;
32d42a85 1775 nand_chip->ecc.priv = NULL;
c9711ec5 1776 nand_set_flash_node(nand_chip, dev->of_node);
67ce04bf 1777
9c4c2f8b 1778 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
00d09891
JH
1779 nand_chip->IO_ADDR_R = devm_ioremap_resource(&pdev->dev, res);
1780 if (IS_ERR(nand_chip->IO_ADDR_R))
1781 return PTR_ERR(nand_chip->IO_ADDR_R);
67ce04bf 1782
9c4c2f8b 1783 info->phys_base = res->start;
59e9c5ae 1784
1dc338e8 1785 nand_chip->controller = &omap_gpmc_controller;
67ce04bf 1786
633deb58
PG
1787 nand_chip->IO_ADDR_W = nand_chip->IO_ADDR_R;
1788 nand_chip->cmd_ctrl = omap_hwcontrol;
67ce04bf 1789
10f22ee3
RQ
1790 info->ready_gpiod = devm_gpiod_get_optional(&pdev->dev, "rb",
1791 GPIOD_IN);
1792 if (IS_ERR(info->ready_gpiod)) {
1793 dev_err(dev, "failed to get ready gpio\n");
1794 return PTR_ERR(info->ready_gpiod);
1795 }
1796
67ce04bf
VS
1797 /*
1798 * If RDY/BSY line is connected to OMAP then use the omap ready
4cacbe22
PM
1799 * function and the generic nand_wait function which reads the status
1800 * register after monitoring the RDY/BSY line. Otherwise use a standard
67ce04bf
VS
1801 * chip delay which is slightly more than tR (AC Timing) of the NAND
1802 * device and read status register until you get a failure or success
1803 */
10f22ee3 1804 if (info->ready_gpiod) {
633deb58
PG
1805 nand_chip->dev_ready = omap_dev_ready;
1806 nand_chip->chip_delay = 0;
67ce04bf 1807 } else {
633deb58
PG
1808 nand_chip->waitfunc = omap_wait;
1809 nand_chip->chip_delay = 50;
67ce04bf
VS
1810 }
1811
c9711ec5 1812 if (info->flash_bbt)
fef775ca
EG
1813 nand_chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
1814 else
1815 nand_chip->options |= NAND_SKIP_BBTSCAN;
1816
f18befb5 1817 /* scan NAND device connected to chip controller */
01b95fc6 1818 nand_chip->options |= info->devsize & NAND_BUSWIDTH_16;
f18befb5 1819 if (nand_scan_ident(mtd, 1, NULL)) {
01b95fc6
RQ
1820 dev_err(&info->pdev->dev,
1821 "scan failed, may be bus-width mismatch\n");
f18befb5 1822 err = -ENXIO;
70ba6d71 1823 goto return_error;
f18befb5
PG
1824 }
1825
1826 /* re-populate low-level callbacks based on xfer modes */
01b95fc6 1827 switch (info->xfer_type) {
1b0b323c 1828 case NAND_OMAP_PREFETCH_POLLED:
633deb58
PG
1829 nand_chip->read_buf = omap_read_buf_pref;
1830 nand_chip->write_buf = omap_write_buf_pref;
1b0b323c
SG
1831 break;
1832
1833 case NAND_OMAP_POLLED:
cf0e4d2b 1834 /* Use nand_base defaults for {read,write}_buf */
1b0b323c
SG
1835 break;
1836
1837 case NAND_OMAP_PREFETCH_DMA:
763e7359
RK
1838 dma_cap_zero(mask);
1839 dma_cap_set(DMA_SLAVE, mask);
1840 sig = OMAP24XX_DMA_GPMC;
1841 info->dma = dma_request_channel(mask, omap_dma_filter_fn, &sig);
1842 if (!info->dma) {
2df41d05
RK
1843 dev_err(&pdev->dev, "DMA engine request failed\n");
1844 err = -ENXIO;
70ba6d71 1845 goto return_error;
763e7359
RK
1846 } else {
1847 struct dma_slave_config cfg;
763e7359
RK
1848
1849 memset(&cfg, 0, sizeof(cfg));
1850 cfg.src_addr = info->phys_base;
1851 cfg.dst_addr = info->phys_base;
1852 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1853 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1854 cfg.src_maxburst = 16;
1855 cfg.dst_maxburst = 16;
d680e2c1
AB
1856 err = dmaengine_slave_config(info->dma, &cfg);
1857 if (err) {
763e7359 1858 dev_err(&pdev->dev, "DMA engine slave config failed: %d\n",
d680e2c1 1859 err);
70ba6d71 1860 goto return_error;
763e7359 1861 }
633deb58
PG
1862 nand_chip->read_buf = omap_read_buf_dma_pref;
1863 nand_chip->write_buf = omap_write_buf_dma_pref;
1b0b323c
SG
1864 }
1865 break;
1866
4e070376 1867 case NAND_OMAP_PREFETCH_IRQ:
5c468455
AM
1868 info->gpmc_irq_fifo = platform_get_irq(pdev, 0);
1869 if (info->gpmc_irq_fifo <= 0) {
1870 dev_err(&pdev->dev, "error getting fifo irq\n");
1871 err = -ENODEV;
70ba6d71 1872 goto return_error;
5c468455 1873 }
70ba6d71
PG
1874 err = devm_request_irq(&pdev->dev, info->gpmc_irq_fifo,
1875 omap_nand_irq, IRQF_SHARED,
1876 "gpmc-nand-fifo", info);
4e070376
SG
1877 if (err) {
1878 dev_err(&pdev->dev, "requesting irq(%d) error:%d",
5c468455
AM
1879 info->gpmc_irq_fifo, err);
1880 info->gpmc_irq_fifo = 0;
70ba6d71 1881 goto return_error;
5c468455
AM
1882 }
1883
1884 info->gpmc_irq_count = platform_get_irq(pdev, 1);
1885 if (info->gpmc_irq_count <= 0) {
1886 dev_err(&pdev->dev, "error getting count irq\n");
1887 err = -ENODEV;
70ba6d71 1888 goto return_error;
5c468455 1889 }
70ba6d71
PG
1890 err = devm_request_irq(&pdev->dev, info->gpmc_irq_count,
1891 omap_nand_irq, IRQF_SHARED,
1892 "gpmc-nand-count", info);
5c468455
AM
1893 if (err) {
1894 dev_err(&pdev->dev, "requesting irq(%d) error:%d",
1895 info->gpmc_irq_count, err);
1896 info->gpmc_irq_count = 0;
70ba6d71 1897 goto return_error;
4e070376 1898 }
5c468455 1899
633deb58
PG
1900 nand_chip->read_buf = omap_read_buf_irq_pref;
1901 nand_chip->write_buf = omap_write_buf_irq_pref;
5c468455 1902
4e070376
SG
1903 break;
1904
1b0b323c
SG
1905 default:
1906 dev_err(&pdev->dev,
01b95fc6 1907 "xfer_type(%d) not supported!\n", info->xfer_type);
1b0b323c 1908 err = -EINVAL;
70ba6d71 1909 goto return_error;
59e9c5ae 1910 }
59e9c5ae 1911
93af53b8
EG
1912 if (!omap2_nand_ecc_check(info, pdata)) {
1913 err = -EINVAL;
1914 goto return_error;
1915 }
1916
a8c65d50
BB
1917 /*
1918 * Bail out earlier to let NAND_ECC_SOFT code create its own
1919 * ecclayout instead of using ours.
1920 */
1921 if (info->ecc_opt == OMAP_ECC_HAM1_CODE_SW) {
1922 nand_chip->ecc.mode = NAND_ECC_SOFT;
1923 goto scan_tail;
1924 }
1925
a919e511 1926 /* populate MTD interface based on ECC scheme */
94cb4ee0 1927 ecclayout = &info->oobinfo;
a8c65d50 1928 nand_chip->ecc.layout = ecclayout;
4e558072 1929 switch (info->ecc_opt) {
a919e511
PG
1930 case OMAP_ECC_HAM1_CODE_HW:
1931 pr_info("nand: using OMAP_ECC_HAM1_CODE_HW\n");
1932 nand_chip->ecc.mode = NAND_ECC_HW;
633deb58
PG
1933 nand_chip->ecc.bytes = 3;
1934 nand_chip->ecc.size = 512;
1935 nand_chip->ecc.strength = 1;
1936 nand_chip->ecc.calculate = omap_calculate_ecc;
1937 nand_chip->ecc.hwctl = omap_enable_hwecc;
1938 nand_chip->ecc.correct = omap_correct_data;
b491da72
PG
1939 /* define ECC layout */
1940 ecclayout->eccbytes = nand_chip->ecc.bytes *
1941 (mtd->writesize /
1942 nand_chip->ecc.size);
1943 if (nand_chip->options & NAND_BUSWIDTH_16)
eae39cb4 1944 oob_index = BADBLOCK_MARKER_LENGTH;
b491da72 1945 else
eae39cb4
PG
1946 oob_index = 1;
1947 for (i = 0; i < ecclayout->eccbytes; i++, oob_index++)
1948 ecclayout->eccpos[i] = oob_index;
aa6092f9
PG
1949 /* no reserved-marker in ecclayout for this ecc-scheme */
1950 ecclayout->oobfree->offset =
1951 ecclayout->eccpos[ecclayout->eccbytes - 1] + 1;
a919e511
PG
1952 break;
1953
1954 case OMAP_ECC_BCH4_CODE_HW_DETECTION_SW:
a919e511
PG
1955 pr_info("nand: using OMAP_ECC_BCH4_CODE_HW_DETECTION_SW\n");
1956 nand_chip->ecc.mode = NAND_ECC_HW;
1957 nand_chip->ecc.size = 512;
1958 nand_chip->ecc.bytes = 7;
1959 nand_chip->ecc.strength = 4;
7c977c3e 1960 nand_chip->ecc.hwctl = omap_enable_hwecc_bch;
32d42a85 1961 nand_chip->ecc.correct = nand_bch_correct_data;
2c9f2365 1962 nand_chip->ecc.calculate = omap_calculate_ecc_bch;
b491da72
PG
1963 /* define ECC layout */
1964 ecclayout->eccbytes = nand_chip->ecc.bytes *
1965 (mtd->writesize /
1966 nand_chip->ecc.size);
eae39cb4
PG
1967 oob_index = BADBLOCK_MARKER_LENGTH;
1968 for (i = 0; i < ecclayout->eccbytes; i++, oob_index++) {
1969 ecclayout->eccpos[i] = oob_index;
1970 if (((i + 1) % nand_chip->ecc.bytes) == 0)
1971 oob_index++;
1972 }
aa6092f9
PG
1973 /* include reserved-marker in ecclayout->oobfree calculation */
1974 ecclayout->oobfree->offset = 1 +
1975 ecclayout->eccpos[ecclayout->eccbytes - 1] + 1;
a919e511 1976 /* software bch library is used for locating errors */
a8c65d50 1977 nand_chip->ecc.priv = nand_bch_init(mtd);
32d42a85 1978 if (!nand_chip->ecc.priv) {
d2f08c75 1979 dev_err(&info->pdev->dev, "unable to use BCH library\n");
0e618ef0 1980 err = -EINVAL;
d2f08c75 1981 goto return_error;
a919e511
PG
1982 }
1983 break;
a919e511
PG
1984
1985 case OMAP_ECC_BCH4_CODE_HW:
a919e511
PG
1986 pr_info("nand: using OMAP_ECC_BCH4_CODE_HW ECC scheme\n");
1987 nand_chip->ecc.mode = NAND_ECC_HW;
1988 nand_chip->ecc.size = 512;
1989 /* 14th bit is kept reserved for ROM-code compatibility */
1990 nand_chip->ecc.bytes = 7 + 1;
1991 nand_chip->ecc.strength = 4;
7c977c3e 1992 nand_chip->ecc.hwctl = omap_enable_hwecc_bch;
a919e511 1993 nand_chip->ecc.correct = omap_elm_correct_data;
a4c7ca00 1994 nand_chip->ecc.calculate = omap_calculate_ecc_bch;
a919e511
PG
1995 nand_chip->ecc.read_page = omap_read_page_bch;
1996 nand_chip->ecc.write_page = omap_write_page_bch;
b491da72
PG
1997 /* define ECC layout */
1998 ecclayout->eccbytes = nand_chip->ecc.bytes *
1999 (mtd->writesize /
2000 nand_chip->ecc.size);
eae39cb4
PG
2001 oob_index = BADBLOCK_MARKER_LENGTH;
2002 for (i = 0; i < ecclayout->eccbytes; i++, oob_index++)
2003 ecclayout->eccpos[i] = oob_index;
aa6092f9
PG
2004 /* reserved marker already included in ecclayout->eccbytes */
2005 ecclayout->oobfree->offset =
2006 ecclayout->eccpos[ecclayout->eccbytes - 1] + 1;
93af53b8
EG
2007
2008 err = elm_config(info->elm_dev, BCH4_ECC,
432420c0 2009 mtd->writesize / nand_chip->ecc.size,
93af53b8
EG
2010 nand_chip->ecc.size, nand_chip->ecc.bytes);
2011 if (err < 0)
70ba6d71 2012 goto return_error;
a919e511 2013 break;
a919e511
PG
2014
2015 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
a919e511
PG
2016 pr_info("nand: using OMAP_ECC_BCH8_CODE_HW_DETECTION_SW\n");
2017 nand_chip->ecc.mode = NAND_ECC_HW;
2018 nand_chip->ecc.size = 512;
2019 nand_chip->ecc.bytes = 13;
2020 nand_chip->ecc.strength = 8;
7c977c3e 2021 nand_chip->ecc.hwctl = omap_enable_hwecc_bch;
32d42a85 2022 nand_chip->ecc.correct = nand_bch_correct_data;
7bcd1dca 2023 nand_chip->ecc.calculate = omap_calculate_ecc_bch;
b491da72
PG
2024 /* define ECC layout */
2025 ecclayout->eccbytes = nand_chip->ecc.bytes *
2026 (mtd->writesize /
2027 nand_chip->ecc.size);
eae39cb4
PG
2028 oob_index = BADBLOCK_MARKER_LENGTH;
2029 for (i = 0; i < ecclayout->eccbytes; i++, oob_index++) {
2030 ecclayout->eccpos[i] = oob_index;
2031 if (((i + 1) % nand_chip->ecc.bytes) == 0)
2032 oob_index++;
2033 }
aa6092f9
PG
2034 /* include reserved-marker in ecclayout->oobfree calculation */
2035 ecclayout->oobfree->offset = 1 +
2036 ecclayout->eccpos[ecclayout->eccbytes - 1] + 1;
a919e511 2037 /* software bch library is used for locating errors */
a8c65d50 2038 nand_chip->ecc.priv = nand_bch_init(mtd);
32d42a85 2039 if (!nand_chip->ecc.priv) {
d2f08c75 2040 dev_err(&info->pdev->dev, "unable to use BCH library\n");
a919e511 2041 err = -EINVAL;
70ba6d71 2042 goto return_error;
a919e511
PG
2043 }
2044 break;
a919e511
PG
2045
2046 case OMAP_ECC_BCH8_CODE_HW:
a919e511
PG
2047 pr_info("nand: using OMAP_ECC_BCH8_CODE_HW ECC scheme\n");
2048 nand_chip->ecc.mode = NAND_ECC_HW;
2049 nand_chip->ecc.size = 512;
2050 /* 14th bit is kept reserved for ROM-code compatibility */
2051 nand_chip->ecc.bytes = 13 + 1;
2052 nand_chip->ecc.strength = 8;
7c977c3e 2053 nand_chip->ecc.hwctl = omap_enable_hwecc_bch;
a919e511 2054 nand_chip->ecc.correct = omap_elm_correct_data;
a4c7ca00 2055 nand_chip->ecc.calculate = omap_calculate_ecc_bch;
a919e511
PG
2056 nand_chip->ecc.read_page = omap_read_page_bch;
2057 nand_chip->ecc.write_page = omap_write_page_bch;
93af53b8
EG
2058
2059 err = elm_config(info->elm_dev, BCH8_ECC,
432420c0 2060 mtd->writesize / nand_chip->ecc.size,
93af53b8
EG
2061 nand_chip->ecc.size, nand_chip->ecc.bytes);
2062 if (err < 0)
70ba6d71 2063 goto return_error;
93af53b8 2064
b491da72
PG
2065 /* define ECC layout */
2066 ecclayout->eccbytes = nand_chip->ecc.bytes *
2067 (mtd->writesize /
2068 nand_chip->ecc.size);
eae39cb4
PG
2069 oob_index = BADBLOCK_MARKER_LENGTH;
2070 for (i = 0; i < ecclayout->eccbytes; i++, oob_index++)
2071 ecclayout->eccpos[i] = oob_index;
aa6092f9
PG
2072 /* reserved marker already included in ecclayout->eccbytes */
2073 ecclayout->oobfree->offset =
2074 ecclayout->eccpos[ecclayout->eccbytes - 1] + 1;
a919e511 2075 break;
a919e511 2076
9748fff9 2077 case OMAP_ECC_BCH16_CODE_HW:
9748fff9 2078 pr_info("using OMAP_ECC_BCH16_CODE_HW ECC scheme\n");
2079 nand_chip->ecc.mode = NAND_ECC_HW;
2080 nand_chip->ecc.size = 512;
2081 nand_chip->ecc.bytes = 26;
2082 nand_chip->ecc.strength = 16;
2083 nand_chip->ecc.hwctl = omap_enable_hwecc_bch;
2084 nand_chip->ecc.correct = omap_elm_correct_data;
2085 nand_chip->ecc.calculate = omap_calculate_ecc_bch;
2086 nand_chip->ecc.read_page = omap_read_page_bch;
2087 nand_chip->ecc.write_page = omap_write_page_bch;
93af53b8
EG
2088
2089 err = elm_config(info->elm_dev, BCH16_ECC,
432420c0 2090 mtd->writesize / nand_chip->ecc.size,
93af53b8
EG
2091 nand_chip->ecc.size, nand_chip->ecc.bytes);
2092 if (err < 0)
9748fff9 2093 goto return_error;
93af53b8 2094
9748fff9 2095 /* define ECC layout */
2096 ecclayout->eccbytes = nand_chip->ecc.bytes *
2097 (mtd->writesize /
2098 nand_chip->ecc.size);
2099 oob_index = BADBLOCK_MARKER_LENGTH;
2100 for (i = 0; i < ecclayout->eccbytes; i++, oob_index++)
2101 ecclayout->eccpos[i] = oob_index;
2102 /* reserved marker already included in ecclayout->eccbytes */
2103 ecclayout->oobfree->offset =
2104 ecclayout->eccpos[ecclayout->eccbytes - 1] + 1;
2105 break;
a919e511 2106 default:
d2f08c75 2107 dev_err(&info->pdev->dev, "invalid or unsupported ECC scheme\n");
a919e511 2108 err = -EINVAL;
70ba6d71 2109 goto return_error;
f3d73f36 2110 }
67ce04bf 2111
bb38eefb
PG
2112 /* all OOB bytes from oobfree->offset till end off OOB are free */
2113 ecclayout->oobfree->length = mtd->oobsize - ecclayout->oobfree->offset;
b491da72
PG
2114 /* check if NAND device's OOB is enough to store ECC signatures */
2115 if (mtd->oobsize < (ecclayout->eccbytes + BADBLOCK_MARKER_LENGTH)) {
d2f08c75
EG
2116 dev_err(&info->pdev->dev,
2117 "not enough OOB bytes required = %d, available=%d\n",
2118 ecclayout->eccbytes, mtd->oobsize);
b491da72 2119 err = -EINVAL;
70ba6d71 2120 goto return_error;
f040d332 2121 }
1b0b323c 2122
7d5929c1 2123scan_tail:
a80f1c1f 2124 /* second phase scan */
633deb58 2125 if (nand_scan_tail(mtd)) {
a80f1c1f 2126 err = -ENXIO;
70ba6d71 2127 goto return_error;
a80f1c1f
JW
2128 }
2129
c9711ec5
RQ
2130 if (dev->of_node)
2131 mtd_device_register(mtd, NULL, 0);
2132 else
2133 mtd_device_register(mtd, pdata->parts, pdata->nr_parts);
67ce04bf 2134
633deb58 2135 platform_set_drvdata(pdev, mtd);
67ce04bf
VS
2136
2137 return 0;
2138
70ba6d71 2139return_error:
763e7359
RK
2140 if (info->dma)
2141 dma_release_channel(info->dma);
32d42a85
PG
2142 if (nand_chip->ecc.priv) {
2143 nand_bch_free(nand_chip->ecc.priv);
2144 nand_chip->ecc.priv = NULL;
2145 }
67ce04bf
VS
2146 return err;
2147}
2148
2149static int omap_nand_remove(struct platform_device *pdev)
2150{
2151 struct mtd_info *mtd = platform_get_drvdata(pdev);
4bd4ebcc 2152 struct nand_chip *nand_chip = mtd_to_nand(mtd);
4578ea9a 2153 struct omap_nand_info *info = mtd_to_omap(mtd);
32d42a85
PG
2154 if (nand_chip->ecc.priv) {
2155 nand_bch_free(nand_chip->ecc.priv);
2156 nand_chip->ecc.priv = NULL;
2157 }
763e7359
RK
2158 if (info->dma)
2159 dma_release_channel(info->dma);
633deb58 2160 nand_release(mtd);
67ce04bf
VS
2161 return 0;
2162}
2163
c9711ec5
RQ
2164static const struct of_device_id omap_nand_ids[] = {
2165 { .compatible = "ti,omap2-nand", },
2166 {},
2167};
2168
67ce04bf
VS
2169static struct platform_driver omap_nand_driver = {
2170 .probe = omap_nand_probe,
2171 .remove = omap_nand_remove,
2172 .driver = {
2173 .name = DRIVER_NAME,
c9711ec5 2174 .of_match_table = of_match_ptr(omap_nand_ids),
67ce04bf
VS
2175 },
2176};
2177
f99640de 2178module_platform_driver(omap_nand_driver);
67ce04bf 2179
c804c733 2180MODULE_ALIAS("platform:" DRIVER_NAME);
67ce04bf
VS
2181MODULE_LICENSE("GPL");
2182MODULE_DESCRIPTION("Glue layer for NAND flash on TI OMAP boards");
This page took 0.437669 seconds and 5 git commands to generate.