1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright © 2004 Texas Instruments, Jian Zhang <jzhang@ti.com>
4 * Copyright © 2004 Micron Technology Inc.
5 * Copyright © 2004 David Brownell
8 #include <linux/platform_device.h>
9 #include <linux/dmaengine.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/delay.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/module.h>
14 #include <linux/interrupt.h>
15 #include <linux/jiffies.h>
16 #include <linux/sched.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/nand-ecc-sw-bch.h>
19 #include <linux/mtd/rawnand.h>
20 #include <linux/mtd/partitions.h>
21 #include <linux/omap-dma.h>
22 #include <linux/iopoll.h>
23 #include <linux/slab.h>
25 #include <linux/of_device.h>
27 #include <linux/platform_data/elm.h>
29 #include <linux/omap-gpmc.h>
30 #include <linux/platform_data/mtd-nand-omap2.h>
32 #define DRIVER_NAME "omap2-nand"
33 #define OMAP_NAND_TIMEOUT_MS 5000
35 #define NAND_Ecc_P1e (1 << 0)
36 #define NAND_Ecc_P2e (1 << 1)
37 #define NAND_Ecc_P4e (1 << 2)
38 #define NAND_Ecc_P8e (1 << 3)
39 #define NAND_Ecc_P16e (1 << 4)
40 #define NAND_Ecc_P32e (1 << 5)
41 #define NAND_Ecc_P64e (1 << 6)
42 #define NAND_Ecc_P128e (1 << 7)
43 #define NAND_Ecc_P256e (1 << 8)
44 #define NAND_Ecc_P512e (1 << 9)
45 #define NAND_Ecc_P1024e (1 << 10)
46 #define NAND_Ecc_P2048e (1 << 11)
48 #define NAND_Ecc_P1o (1 << 16)
49 #define NAND_Ecc_P2o (1 << 17)
50 #define NAND_Ecc_P4o (1 << 18)
51 #define NAND_Ecc_P8o (1 << 19)
52 #define NAND_Ecc_P16o (1 << 20)
53 #define NAND_Ecc_P32o (1 << 21)
54 #define NAND_Ecc_P64o (1 << 22)
55 #define NAND_Ecc_P128o (1 << 23)
56 #define NAND_Ecc_P256o (1 << 24)
57 #define NAND_Ecc_P512o (1 << 25)
58 #define NAND_Ecc_P1024o (1 << 26)
59 #define NAND_Ecc_P2048o (1 << 27)
61 #define TF(value) (value ? 1 : 0)
63 #define P2048e(a) (TF(a & NAND_Ecc_P2048e) << 0)
64 #define P2048o(a) (TF(a & NAND_Ecc_P2048o) << 1)
65 #define P1e(a) (TF(a & NAND_Ecc_P1e) << 2)
66 #define P1o(a) (TF(a & NAND_Ecc_P1o) << 3)
67 #define P2e(a) (TF(a & NAND_Ecc_P2e) << 4)
68 #define P2o(a) (TF(a & NAND_Ecc_P2o) << 5)
69 #define P4e(a) (TF(a & NAND_Ecc_P4e) << 6)
70 #define P4o(a) (TF(a & NAND_Ecc_P4o) << 7)
72 #define P8e(a) (TF(a & NAND_Ecc_P8e) << 0)
73 #define P8o(a) (TF(a & NAND_Ecc_P8o) << 1)
74 #define P16e(a) (TF(a & NAND_Ecc_P16e) << 2)
75 #define P16o(a) (TF(a & NAND_Ecc_P16o) << 3)
76 #define P32e(a) (TF(a & NAND_Ecc_P32e) << 4)
77 #define P32o(a) (TF(a & NAND_Ecc_P32o) << 5)
78 #define P64e(a) (TF(a & NAND_Ecc_P64e) << 6)
79 #define P64o(a) (TF(a & NAND_Ecc_P64o) << 7)
81 #define P128e(a) (TF(a & NAND_Ecc_P128e) << 0)
82 #define P128o(a) (TF(a & NAND_Ecc_P128o) << 1)
83 #define P256e(a) (TF(a & NAND_Ecc_P256e) << 2)
84 #define P256o(a) (TF(a & NAND_Ecc_P256o) << 3)
85 #define P512e(a) (TF(a & NAND_Ecc_P512e) << 4)
86 #define P512o(a) (TF(a & NAND_Ecc_P512o) << 5)
87 #define P1024e(a) (TF(a & NAND_Ecc_P1024e) << 6)
88 #define P1024o(a) (TF(a & NAND_Ecc_P1024o) << 7)
90 #define P8e_s(a) (TF(a & NAND_Ecc_P8e) << 0)
91 #define P8o_s(a) (TF(a & NAND_Ecc_P8o) << 1)
92 #define P16e_s(a) (TF(a & NAND_Ecc_P16e) << 2)
93 #define P16o_s(a) (TF(a & NAND_Ecc_P16o) << 3)
94 #define P1e_s(a) (TF(a & NAND_Ecc_P1e) << 4)
95 #define P1o_s(a) (TF(a & NAND_Ecc_P1o) << 5)
96 #define P2e_s(a) (TF(a & NAND_Ecc_P2e) << 6)
97 #define P2o_s(a) (TF(a & NAND_Ecc_P2o) << 7)
99 #define P4e_s(a) (TF(a & NAND_Ecc_P4e) << 0)
100 #define P4o_s(a) (TF(a & NAND_Ecc_P4o) << 1)
102 #define PREFETCH_CONFIG1_CS_SHIFT 24
103 #define ECC_CONFIG_CS_SHIFT 1
105 #define ENABLE_PREFETCH (0x1 << 7)
106 #define DMA_MPU_MODE_SHIFT 2
107 #define ECCSIZE0_SHIFT 12
108 #define ECCSIZE1_SHIFT 22
109 #define ECC1RESULTSIZE 0x1
110 #define ECCCLEAR 0x100
112 #define PREFETCH_FIFOTHRESHOLD_MAX 0x40
113 #define PREFETCH_FIFOTHRESHOLD(val) ((val) << 8)
114 #define PREFETCH_STATUS_COUNT(val) (val & 0x00003fff)
115 #define PREFETCH_STATUS_FIFO_CNT(val) ((val >> 24) & 0x7F)
116 #define STATUS_BUFF_EMPTY 0x00000001
118 #define SECTOR_BYTES 512
119 /* 4 bit padding to make byte aligned, 56 = 52 + 4 */
120 #define BCH4_BIT_PAD 4
122 /* GPMC ecc engine settings for read */
123 #define BCH_WRAPMODE_1 1 /* BCH wrap mode 1 */
124 #define BCH8R_ECC_SIZE0 0x1a /* ecc_size0 = 26 */
125 #define BCH8R_ECC_SIZE1 0x2 /* ecc_size1 = 2 */
126 #define BCH4R_ECC_SIZE0 0xd /* ecc_size0 = 13 */
127 #define BCH4R_ECC_SIZE1 0x3 /* ecc_size1 = 3 */
129 /* GPMC ecc engine settings for write */
130 #define BCH_WRAPMODE_6 6 /* BCH wrap mode 6 */
131 #define BCH_ECC_SIZE0 0x0 /* ecc_size0 = 0, no oob protection */
132 #define BCH_ECC_SIZE1 0x20 /* ecc_size1 = 32 */
136 static u_char bch16_vector[] = {0xf5, 0x24, 0x1c, 0xd0, 0x61, 0xb3, 0xf1, 0x55,
137 0x2e, 0x2c, 0x86, 0xa3, 0xed, 0x36, 0x1b, 0x78,
138 0x48, 0x76, 0xa9, 0x3b, 0x97, 0xd1, 0x7a, 0x93,
140 static u_char bch8_vector[] = {0xf3, 0xdb, 0x14, 0x16, 0x8b, 0xd2, 0xbe, 0xcc,
141 0xac, 0x6b, 0xff, 0x99, 0x7b};
142 static u_char bch4_vector[] = {0x00, 0x6b, 0x31, 0xdd, 0x41, 0xbc, 0x10};
144 struct omap_nand_info {
145 struct nand_chip nand;
146 struct platform_device *pdev;
150 enum nand_io xfer_type;
151 enum omap_ecc ecc_opt;
152 struct device_node *elm_of_node;
154 unsigned long phys_base;
155 struct completion comp;
156 struct dma_chan *dma;
160 OMAP_NAND_IO_READ = 0, /* read */
161 OMAP_NAND_IO_WRITE, /* write */
165 /* Interface to GPMC */
167 struct gpmc_nand_regs reg;
168 struct gpmc_nand_ops *ops;
170 /* fields specific for BCHx_HW ECC scheme */
171 struct device *elm_dev;
172 /* NAND ready gpio */
173 struct gpio_desc *ready_gpiod;
175 unsigned int nsteps_per_eccpg;
176 unsigned int eccpg_size;
177 unsigned int eccpg_bytes;
178 void (*data_in)(struct nand_chip *chip, void *buf,
179 unsigned int len, bool force_8bit);
180 void (*data_out)(struct nand_chip *chip,
181 const void *buf, unsigned int len,
185 static inline struct omap_nand_info *mtd_to_omap(struct mtd_info *mtd)
187 return container_of(mtd_to_nand(mtd), struct omap_nand_info, nand);
190 static void omap_nand_data_in(struct nand_chip *chip, void *buf,
191 unsigned int len, bool force_8bit);
193 static void omap_nand_data_out(struct nand_chip *chip,
194 const void *buf, unsigned int len,
198 * omap_prefetch_enable - configures and starts prefetch transfer
199 * @cs: cs (chip select) number
200 * @fifo_th: fifo threshold to be used for read/ write
201 * @dma_mode: dma mode enable (1) or disable (0)
202 * @u32_count: number of bytes to be transferred
203 * @is_write: prefetch read(0) or write post(1) mode
204 * @info: NAND device structure containing platform data
206 static int omap_prefetch_enable(int cs, int fifo_th, int dma_mode,
207 unsigned int u32_count, int is_write, struct omap_nand_info *info)
211 if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX)
214 if (readl(info->reg.gpmc_prefetch_control))
217 /* Set the amount of bytes to be prefetched */
218 writel(u32_count, info->reg.gpmc_prefetch_config2);
220 /* Set dma/mpu mode, the prefetch read / post write and
221 * enable the engine. Set which cs is has requested for.
223 val = ((cs << PREFETCH_CONFIG1_CS_SHIFT) |
224 PREFETCH_FIFOTHRESHOLD(fifo_th) | ENABLE_PREFETCH |
225 (dma_mode << DMA_MPU_MODE_SHIFT) | (is_write & 0x1));
226 writel(val, info->reg.gpmc_prefetch_config1);
228 /* Start the prefetch engine */
229 writel(0x1, info->reg.gpmc_prefetch_control);
235 * omap_prefetch_reset - disables and stops the prefetch engine
237 static int omap_prefetch_reset(int cs, struct omap_nand_info *info)
241 /* check if the same module/cs is trying to reset */
242 config1 = readl(info->reg.gpmc_prefetch_config1);
243 if (((config1 >> PREFETCH_CONFIG1_CS_SHIFT) & CS_MASK) != cs)
246 /* Stop the PFPW engine */
247 writel(0x0, info->reg.gpmc_prefetch_control);
249 /* Reset/disable the PFPW engine */
250 writel(0x0, info->reg.gpmc_prefetch_config1);
256 * omap_nand_data_in_pref - NAND data in using prefetch engine
258 static void omap_nand_data_in_pref(struct nand_chip *chip, void *buf,
259 unsigned int len, bool force_8bit)
261 struct omap_nand_info *info = mtd_to_omap(nand_to_mtd(chip));
262 uint32_t r_count = 0;
265 unsigned int pref_len;
268 omap_nand_data_in(chip, buf, len, force_8bit);
272 /* read 32-bit words using prefetch and remaining bytes normally */
274 /* configure and start prefetch transfer */
275 pref_len = len - (len & 3);
276 ret = omap_prefetch_enable(info->gpmc_cs,
277 PREFETCH_FIFOTHRESHOLD_MAX, 0x0, pref_len, 0x0, info);
279 /* prefetch engine is busy, use CPU copy method */
280 omap_nand_data_in(chip, buf, len, false);
283 r_count = readl(info->reg.gpmc_prefetch_status);
284 r_count = PREFETCH_STATUS_FIFO_CNT(r_count);
285 r_count = r_count >> 2;
286 ioread32_rep(info->fifo, p, r_count);
288 pref_len -= r_count << 2;
290 /* disable and stop the Prefetch engine */
291 omap_prefetch_reset(info->gpmc_cs, info);
292 /* fetch any remaining bytes */
294 omap_nand_data_in(chip, p, len & 3, false);
299 * omap_nand_data_out_pref - NAND data out using Write Posting engine
301 static void omap_nand_data_out_pref(struct nand_chip *chip,
302 const void *buf, unsigned int len,
305 struct omap_nand_info *info = mtd_to_omap(nand_to_mtd(chip));
306 uint32_t w_count = 0;
309 unsigned long tim, limit;
313 omap_nand_data_out(chip, buf, len, force_8bit);
317 /* take care of subpage writes */
319 writeb(*(u8 *)buf, info->fifo);
320 p = (u16 *)(buf + 1);
324 /* configure and start prefetch transfer */
325 ret = omap_prefetch_enable(info->gpmc_cs,
326 PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x1, info);
328 /* write posting engine is busy, use CPU copy method */
329 omap_nand_data_out(chip, buf, len, false);
332 w_count = readl(info->reg.gpmc_prefetch_status);
333 w_count = PREFETCH_STATUS_FIFO_CNT(w_count);
334 w_count = w_count >> 1;
335 for (i = 0; (i < w_count) && len; i++, len -= 2)
336 iowrite16(*p++, info->fifo);
338 /* wait for data to flushed-out before reset the prefetch */
340 limit = (loops_per_jiffy *
341 msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
344 val = readl(info->reg.gpmc_prefetch_status);
345 val = PREFETCH_STATUS_COUNT(val);
346 } while (val && (tim++ < limit));
348 /* disable and stop the PFPW engine */
349 omap_prefetch_reset(info->gpmc_cs, info);
354 * omap_nand_dma_callback: callback on the completion of dma transfer
355 * @data: pointer to completion data structure
357 static void omap_nand_dma_callback(void *data)
359 complete((struct completion *) data);
363 * omap_nand_dma_transfer: configure and start dma transfer
364 * @chip: nand chip structure
365 * @addr: virtual address in RAM of source/destination
366 * @len: number of data bytes to be transferred
367 * @is_write: flag for read/write operation
369 static inline int omap_nand_dma_transfer(struct nand_chip *chip,
370 const void *addr, unsigned int len,
373 struct omap_nand_info *info = mtd_to_omap(nand_to_mtd(chip));
374 struct dma_async_tx_descriptor *tx;
375 enum dma_data_direction dir = is_write ? DMA_TO_DEVICE :
377 struct scatterlist sg;
378 unsigned long tim, limit;
383 if (!virt_addr_valid(addr))
386 sg_init_one(&sg, addr, len);
387 n = dma_map_sg(info->dma->device->dev, &sg, 1, dir);
389 dev_err(&info->pdev->dev,
390 "Couldn't DMA map a %d byte buffer\n", len);
394 tx = dmaengine_prep_slave_sg(info->dma, &sg, n,
395 is_write ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
396 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
400 tx->callback = omap_nand_dma_callback;
401 tx->callback_param = &info->comp;
402 dmaengine_submit(tx);
404 init_completion(&info->comp);
406 /* setup and start DMA using dma_addr */
407 dma_async_issue_pending(info->dma);
409 /* configure and start prefetch transfer */
410 ret = omap_prefetch_enable(info->gpmc_cs,
411 PREFETCH_FIFOTHRESHOLD_MAX, 0x1, len, is_write, info);
413 /* PFPW engine is busy, use cpu copy method */
416 wait_for_completion(&info->comp);
418 limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
422 val = readl(info->reg.gpmc_prefetch_status);
423 val = PREFETCH_STATUS_COUNT(val);
424 } while (val && (tim++ < limit));
426 /* disable and stop the PFPW engine */
427 omap_prefetch_reset(info->gpmc_cs, info);
429 dma_unmap_sg(info->dma->device->dev, &sg, 1, dir);
433 dma_unmap_sg(info->dma->device->dev, &sg, 1, dir);
435 is_write == 0 ? omap_nand_data_in(chip, (void *)addr, len, false)
436 : omap_nand_data_out(chip, addr, len, false);
442 * omap_nand_data_in_dma_pref - NAND data in using DMA and Prefetch
444 static void omap_nand_data_in_dma_pref(struct nand_chip *chip, void *buf,
445 unsigned int len, bool force_8bit)
447 struct mtd_info *mtd = nand_to_mtd(chip);
450 omap_nand_data_in(chip, buf, len, force_8bit);
454 if (len <= mtd->oobsize)
455 omap_nand_data_in_pref(chip, buf, len, false);
457 /* start transfer in DMA mode */
458 omap_nand_dma_transfer(chip, buf, len, 0x0);
462 * omap_nand_data_out_dma_pref - NAND data out using DMA and write posting
464 static void omap_nand_data_out_dma_pref(struct nand_chip *chip,
465 const void *buf, unsigned int len,
468 struct mtd_info *mtd = nand_to_mtd(chip);
471 omap_nand_data_out(chip, buf, len, force_8bit);
475 if (len <= mtd->oobsize)
476 omap_nand_data_out_pref(chip, buf, len, false);
478 /* start transfer in DMA mode */
479 omap_nand_dma_transfer(chip, buf, len, 0x1);
483 * omap_nand_irq - GPMC irq handler
484 * @this_irq: gpmc irq number
485 * @dev: omap_nand_info structure pointer is passed here
487 static irqreturn_t omap_nand_irq(int this_irq, void *dev)
489 struct omap_nand_info *info = (struct omap_nand_info *) dev;
492 bytes = readl(info->reg.gpmc_prefetch_status);
493 bytes = PREFETCH_STATUS_FIFO_CNT(bytes);
494 bytes = bytes & 0xFFFC; /* io in multiple of 4 bytes */
495 if (info->iomode == OMAP_NAND_IO_WRITE) { /* checks for write io */
496 if (this_irq == info->gpmc_irq_count)
499 if (info->buf_len && (info->buf_len < bytes))
500 bytes = info->buf_len;
501 else if (!info->buf_len)
503 iowrite32_rep(info->fifo, (u32 *)info->buf,
505 info->buf = info->buf + bytes;
506 info->buf_len -= bytes;
509 ioread32_rep(info->fifo, (u32 *)info->buf,
511 info->buf = info->buf + bytes;
513 if (this_irq == info->gpmc_irq_count)
520 complete(&info->comp);
522 disable_irq_nosync(info->gpmc_irq_fifo);
523 disable_irq_nosync(info->gpmc_irq_count);
529 * omap_nand_data_in_irq_pref - NAND data in using Prefetch and IRQ
531 static void omap_nand_data_in_irq_pref(struct nand_chip *chip, void *buf,
532 unsigned int len, bool force_8bit)
534 struct omap_nand_info *info = mtd_to_omap(nand_to_mtd(chip));
535 struct mtd_info *mtd = nand_to_mtd(&info->nand);
538 if (len <= mtd->oobsize || force_8bit) {
539 omap_nand_data_in(chip, buf, len, force_8bit);
543 info->iomode = OMAP_NAND_IO_READ;
545 init_completion(&info->comp);
547 /* configure and start prefetch transfer */
548 ret = omap_prefetch_enable(info->gpmc_cs,
549 PREFETCH_FIFOTHRESHOLD_MAX/2, 0x0, len, 0x0, info);
551 /* PFPW engine is busy, use cpu copy method */
552 omap_nand_data_in(chip, buf, len, false);
558 enable_irq(info->gpmc_irq_count);
559 enable_irq(info->gpmc_irq_fifo);
561 /* waiting for read to complete */
562 wait_for_completion(&info->comp);
564 /* disable and stop the PFPW engine */
565 omap_prefetch_reset(info->gpmc_cs, info);
570 * omap_nand_data_out_irq_pref - NAND out using write posting and IRQ
572 static void omap_nand_data_out_irq_pref(struct nand_chip *chip,
573 const void *buf, unsigned int len,
576 struct omap_nand_info *info = mtd_to_omap(nand_to_mtd(chip));
577 struct mtd_info *mtd = nand_to_mtd(&info->nand);
579 unsigned long tim, limit;
582 if (len <= mtd->oobsize || force_8bit) {
583 omap_nand_data_out(chip, buf, len, force_8bit);
587 info->iomode = OMAP_NAND_IO_WRITE;
588 info->buf = (u_char *) buf;
589 init_completion(&info->comp);
591 /* configure and start prefetch transfer : size=24 */
592 ret = omap_prefetch_enable(info->gpmc_cs,
593 (PREFETCH_FIFOTHRESHOLD_MAX * 3) / 8, 0x0, len, 0x1, info);
595 /* PFPW engine is busy, use cpu copy method */
596 omap_nand_data_out(chip, buf, len, false);
602 enable_irq(info->gpmc_irq_count);
603 enable_irq(info->gpmc_irq_fifo);
605 /* waiting for write to complete */
606 wait_for_completion(&info->comp);
608 /* wait for data to flushed-out before reset the prefetch */
610 limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
612 val = readl(info->reg.gpmc_prefetch_status);
613 val = PREFETCH_STATUS_COUNT(val);
615 } while (val && (tim++ < limit));
617 /* disable and stop the PFPW engine */
618 omap_prefetch_reset(info->gpmc_cs, info);
623 * gen_true_ecc - This function will generate true ECC value
624 * @ecc_buf: buffer to store ecc code
626 * This generated true ECC value can be used when correcting
627 * data read from NAND flash memory core
629 static void gen_true_ecc(u8 *ecc_buf)
631 u32 tmp = ecc_buf[0] | (ecc_buf[1] << 16) |
632 ((ecc_buf[2] & 0xF0) << 20) | ((ecc_buf[2] & 0x0F) << 8);
634 ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) |
635 P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp));
636 ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) |
637 P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
638 ecc_buf[2] = ~(P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) |
639 P1e(tmp) | P2048o(tmp) | P2048e(tmp));
643 * omap_compare_ecc - Detect (2 bits) and correct (1 bit) error in data
644 * @ecc_data1: ecc code from nand spare area
645 * @ecc_data2: ecc code from hardware register obtained from hardware ecc
646 * @page_data: page data
648 * This function compares two ECC's and indicates if there is an error.
649 * If the error can be corrected it will be corrected to the buffer.
650 * If there is no error, %0 is returned. If there is an error but it
651 * was corrected, %1 is returned. Otherwise, %-1 is returned.
653 static int omap_compare_ecc(u8 *ecc_data1, /* read from NAND memory */
654 u8 *ecc_data2, /* read from register */
658 u8 tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
659 u8 comp0_bit[8], comp1_bit[8], comp2_bit[8];
666 isEccFF = ((*(u32 *)ecc_data1 & 0xFFFFFF) == 0xFFFFFF);
668 gen_true_ecc(ecc_data1);
669 gen_true_ecc(ecc_data2);
671 for (i = 0; i <= 2; i++) {
672 *(ecc_data1 + i) = ~(*(ecc_data1 + i));
673 *(ecc_data2 + i) = ~(*(ecc_data2 + i));
676 for (i = 0; i < 8; i++) {
677 tmp0_bit[i] = *ecc_data1 % 2;
678 *ecc_data1 = *ecc_data1 / 2;
681 for (i = 0; i < 8; i++) {
682 tmp1_bit[i] = *(ecc_data1 + 1) % 2;
683 *(ecc_data1 + 1) = *(ecc_data1 + 1) / 2;
686 for (i = 0; i < 8; i++) {
687 tmp2_bit[i] = *(ecc_data1 + 2) % 2;
688 *(ecc_data1 + 2) = *(ecc_data1 + 2) / 2;
691 for (i = 0; i < 8; i++) {
692 comp0_bit[i] = *ecc_data2 % 2;
693 *ecc_data2 = *ecc_data2 / 2;
696 for (i = 0; i < 8; i++) {
697 comp1_bit[i] = *(ecc_data2 + 1) % 2;
698 *(ecc_data2 + 1) = *(ecc_data2 + 1) / 2;
701 for (i = 0; i < 8; i++) {
702 comp2_bit[i] = *(ecc_data2 + 2) % 2;
703 *(ecc_data2 + 2) = *(ecc_data2 + 2) / 2;
706 for (i = 0; i < 6; i++)
707 ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
709 for (i = 0; i < 8; i++)
710 ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
712 for (i = 0; i < 8; i++)
713 ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
715 ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
716 ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
718 for (i = 0; i < 24; i++)
719 ecc_sum += ecc_bit[i];
723 /* Not reached because this function is not called if
724 * ECC values are equal
729 /* Uncorrectable error */
730 pr_debug("ECC UNCORRECTED_ERROR 1\n");
734 /* UN-Correctable error */
735 pr_debug("ECC UNCORRECTED_ERROR B\n");
739 /* Correctable error */
740 find_byte = (ecc_bit[23] << 8) +
750 find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
752 pr_debug("Correcting single bit ECC error at offset: "
753 "%d, bit: %d\n", find_byte, find_bit);
755 page_data[find_byte] ^= (1 << find_bit);
760 if (ecc_data2[0] == 0 &&
765 pr_debug("UNCORRECTED_ERROR default\n");
771 * omap_correct_data - Compares the ECC read with HW generated ECC
772 * @chip: NAND chip object
774 * @read_ecc: ecc read from nand flash
775 * @calc_ecc: ecc read from HW ECC registers
777 * Compares the ecc read from nand spare area with ECC registers values
778 * and if ECC's mismatched, it will call 'omap_compare_ecc' for error
779 * detection and correction. If there are no errors, %0 is returned. If
780 * there were errors and all of the errors were corrected, the number of
781 * corrected errors is returned. If uncorrectable errors exist, %-1 is
784 static int omap_correct_data(struct nand_chip *chip, u_char *dat,
785 u_char *read_ecc, u_char *calc_ecc)
787 struct omap_nand_info *info = mtd_to_omap(nand_to_mtd(chip));
788 int blockCnt = 0, i = 0, ret = 0;
791 /* Ex NAND_ECC_HW12_2048 */
792 if (info->nand.ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST &&
793 info->nand.ecc.size == 2048)
798 for (i = 0; i < blockCnt; i++) {
799 if (memcmp(read_ecc, calc_ecc, 3) != 0) {
800 ret = omap_compare_ecc(read_ecc, calc_ecc, dat);
803 /* keep track of the number of corrected errors */
814 * omap_calculate_ecc - Generate non-inverted ECC bytes.
815 * @chip: NAND chip object
816 * @dat: The pointer to data on which ecc is computed
817 * @ecc_code: The ecc_code buffer
819 * Using noninverted ECC can be considered ugly since writing a blank
820 * page ie. padding will clear the ECC bytes. This is no problem as long
821 * nobody is trying to write data on the seemingly unused page. Reading
822 * an erased page will produce an ECC mismatch between generated and read
823 * ECC bytes that has to be dealt with separately.
825 static int omap_calculate_ecc(struct nand_chip *chip, const u_char *dat,
828 struct omap_nand_info *info = mtd_to_omap(nand_to_mtd(chip));
831 val = readl(info->reg.gpmc_ecc_config);
832 if (((val >> ECC_CONFIG_CS_SHIFT) & CS_MASK) != info->gpmc_cs)
835 /* read ecc result */
836 val = readl(info->reg.gpmc_ecc1_result);
837 *ecc_code++ = val; /* P128e, ..., P1e */
838 *ecc_code++ = val >> 16; /* P128o, ..., P1o */
839 /* P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e */
840 *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0);
846 * omap_enable_hwecc - This function enables the hardware ecc functionality
847 * @chip: NAND chip object
848 * @mode: Read/Write mode
850 static void omap_enable_hwecc(struct nand_chip *chip, int mode)
852 struct omap_nand_info *info = mtd_to_omap(nand_to_mtd(chip));
853 unsigned int dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
856 /* clear ecc and enable bits */
857 val = ECCCLEAR | ECC1;
858 writel(val, info->reg.gpmc_ecc_control);
860 /* program ecc and result sizes */
861 val = ((((info->nand.ecc.size >> 1) - 1) << ECCSIZE1_SHIFT) |
863 writel(val, info->reg.gpmc_ecc_size_config);
868 writel(ECCCLEAR | ECC1, info->reg.gpmc_ecc_control);
870 case NAND_ECC_READSYN:
871 writel(ECCCLEAR, info->reg.gpmc_ecc_control);
874 dev_info(&info->pdev->dev,
875 "error: unrecognized Mode[%d]!\n", mode);
879 /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
880 val = (dev_width << 7) | (info->gpmc_cs << 1) | (0x1);
881 writel(val, info->reg.gpmc_ecc_config);
885 * omap_enable_hwecc_bch - Program GPMC to perform BCH ECC calculation
886 * @chip: NAND chip object
887 * @mode: Read/Write mode
889 * When using BCH with SW correction (i.e. no ELM), sector size is set
890 * to 512 bytes and we use BCH_WRAPMODE_6 wrapping mode
891 * for both reading and writing with:
892 * eccsize0 = 0 (no additional protected byte in spare area)
893 * eccsize1 = 32 (skip 32 nibbles = 16 bytes per sector in spare area)
895 static void __maybe_unused omap_enable_hwecc_bch(struct nand_chip *chip,
898 unsigned int bch_type;
899 unsigned int dev_width, nsectors;
900 struct omap_nand_info *info = mtd_to_omap(nand_to_mtd(chip));
901 enum omap_ecc ecc_opt = info->ecc_opt;
903 unsigned int ecc_size1, ecc_size0;
905 /* GPMC configurations for calculating ECC */
907 case OMAP_ECC_BCH4_CODE_HW_DETECTION_SW:
910 wr_mode = BCH_WRAPMODE_6;
911 ecc_size0 = BCH_ECC_SIZE0;
912 ecc_size1 = BCH_ECC_SIZE1;
914 case OMAP_ECC_BCH4_CODE_HW:
916 nsectors = chip->ecc.steps;
917 if (mode == NAND_ECC_READ) {
918 wr_mode = BCH_WRAPMODE_1;
919 ecc_size0 = BCH4R_ECC_SIZE0;
920 ecc_size1 = BCH4R_ECC_SIZE1;
922 wr_mode = BCH_WRAPMODE_6;
923 ecc_size0 = BCH_ECC_SIZE0;
924 ecc_size1 = BCH_ECC_SIZE1;
927 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
930 wr_mode = BCH_WRAPMODE_6;
931 ecc_size0 = BCH_ECC_SIZE0;
932 ecc_size1 = BCH_ECC_SIZE1;
934 case OMAP_ECC_BCH8_CODE_HW:
936 nsectors = chip->ecc.steps;
937 if (mode == NAND_ECC_READ) {
938 wr_mode = BCH_WRAPMODE_1;
939 ecc_size0 = BCH8R_ECC_SIZE0;
940 ecc_size1 = BCH8R_ECC_SIZE1;
942 wr_mode = BCH_WRAPMODE_6;
943 ecc_size0 = BCH_ECC_SIZE0;
944 ecc_size1 = BCH_ECC_SIZE1;
947 case OMAP_ECC_BCH16_CODE_HW:
949 nsectors = chip->ecc.steps;
950 if (mode == NAND_ECC_READ) {
952 ecc_size0 = 52; /* ECC bits in nibbles per sector */
953 ecc_size1 = 0; /* non-ECC bits in nibbles per sector */
956 ecc_size0 = 0; /* extra bits in nibbles per sector */
957 ecc_size1 = 52; /* OOB bits in nibbles per sector */
964 writel(ECC1, info->reg.gpmc_ecc_control);
966 /* Configure ecc size for BCH */
967 val = (ecc_size1 << ECCSIZE1_SHIFT) | (ecc_size0 << ECCSIZE0_SHIFT);
968 writel(val, info->reg.gpmc_ecc_size_config);
970 dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
972 /* BCH configuration */
973 val = ((1 << 16) | /* enable BCH */
974 (bch_type << 12) | /* BCH4/BCH8/BCH16 */
975 (wr_mode << 8) | /* wrap mode */
976 (dev_width << 7) | /* bus width */
977 (((nsectors-1) & 0x7) << 4) | /* number of sectors */
978 (info->gpmc_cs << 1) | /* ECC CS */
979 (0x1)); /* enable ECC */
981 writel(val, info->reg.gpmc_ecc_config);
983 /* Clear ecc and enable bits */
984 writel(ECCCLEAR | ECC1, info->reg.gpmc_ecc_control);
987 static u8 bch4_polynomial[] = {0x28, 0x13, 0xcc, 0x39, 0x96, 0xac, 0x7f};
988 static u8 bch8_polynomial[] = {0xef, 0x51, 0x2e, 0x09, 0xed, 0x93, 0x9a, 0xc2,
989 0x97, 0x79, 0xe5, 0x24, 0xb5};
992 * _omap_calculate_ecc_bch - Generate ECC bytes for one sector
993 * @mtd: MTD device structure
994 * @dat: The pointer to data on which ecc is computed
995 * @ecc_calc: The ecc_code buffer
996 * @i: The sector number (for a multi sector page)
998 * Support calculating of BCH4/8/16 ECC vectors for one sector
999 * within a page. Sector number is in @i.
1001 static int _omap_calculate_ecc_bch(struct mtd_info *mtd,
1002 const u_char *dat, u_char *ecc_calc, int i)
1004 struct omap_nand_info *info = mtd_to_omap(mtd);
1005 int eccbytes = info->nand.ecc.bytes;
1006 struct gpmc_nand_regs *gpmc_regs = &info->reg;
1008 unsigned long bch_val1, bch_val2, bch_val3, bch_val4;
1012 ecc_code = ecc_calc;
1013 switch (info->ecc_opt) {
1014 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
1015 case OMAP_ECC_BCH8_CODE_HW:
1016 bch_val1 = readl(gpmc_regs->gpmc_bch_result0[i]);
1017 bch_val2 = readl(gpmc_regs->gpmc_bch_result1[i]);
1018 bch_val3 = readl(gpmc_regs->gpmc_bch_result2[i]);
1019 bch_val4 = readl(gpmc_regs->gpmc_bch_result3[i]);
1020 *ecc_code++ = (bch_val4 & 0xFF);
1021 *ecc_code++ = ((bch_val3 >> 24) & 0xFF);
1022 *ecc_code++ = ((bch_val3 >> 16) & 0xFF);
1023 *ecc_code++ = ((bch_val3 >> 8) & 0xFF);
1024 *ecc_code++ = (bch_val3 & 0xFF);
1025 *ecc_code++ = ((bch_val2 >> 24) & 0xFF);
1026 *ecc_code++ = ((bch_val2 >> 16) & 0xFF);
1027 *ecc_code++ = ((bch_val2 >> 8) & 0xFF);
1028 *ecc_code++ = (bch_val2 & 0xFF);
1029 *ecc_code++ = ((bch_val1 >> 24) & 0xFF);
1030 *ecc_code++ = ((bch_val1 >> 16) & 0xFF);
1031 *ecc_code++ = ((bch_val1 >> 8) & 0xFF);
1032 *ecc_code++ = (bch_val1 & 0xFF);
1034 case OMAP_ECC_BCH4_CODE_HW_DETECTION_SW:
1035 case OMAP_ECC_BCH4_CODE_HW:
1036 bch_val1 = readl(gpmc_regs->gpmc_bch_result0[i]);
1037 bch_val2 = readl(gpmc_regs->gpmc_bch_result1[i]);
1038 *ecc_code++ = ((bch_val2 >> 12) & 0xFF);
1039 *ecc_code++ = ((bch_val2 >> 4) & 0xFF);
1040 *ecc_code++ = ((bch_val2 & 0xF) << 4) |
1041 ((bch_val1 >> 28) & 0xF);
1042 *ecc_code++ = ((bch_val1 >> 20) & 0xFF);
1043 *ecc_code++ = ((bch_val1 >> 12) & 0xFF);
1044 *ecc_code++ = ((bch_val1 >> 4) & 0xFF);
1045 *ecc_code++ = ((bch_val1 & 0xF) << 4);
1047 case OMAP_ECC_BCH16_CODE_HW:
1048 val = readl(gpmc_regs->gpmc_bch_result6[i]);
1049 ecc_code[0] = ((val >> 8) & 0xFF);
1050 ecc_code[1] = ((val >> 0) & 0xFF);
1051 val = readl(gpmc_regs->gpmc_bch_result5[i]);
1052 ecc_code[2] = ((val >> 24) & 0xFF);
1053 ecc_code[3] = ((val >> 16) & 0xFF);
1054 ecc_code[4] = ((val >> 8) & 0xFF);
1055 ecc_code[5] = ((val >> 0) & 0xFF);
1056 val = readl(gpmc_regs->gpmc_bch_result4[i]);
1057 ecc_code[6] = ((val >> 24) & 0xFF);
1058 ecc_code[7] = ((val >> 16) & 0xFF);
1059 ecc_code[8] = ((val >> 8) & 0xFF);
1060 ecc_code[9] = ((val >> 0) & 0xFF);
1061 val = readl(gpmc_regs->gpmc_bch_result3[i]);
1062 ecc_code[10] = ((val >> 24) & 0xFF);
1063 ecc_code[11] = ((val >> 16) & 0xFF);
1064 ecc_code[12] = ((val >> 8) & 0xFF);
1065 ecc_code[13] = ((val >> 0) & 0xFF);
1066 val = readl(gpmc_regs->gpmc_bch_result2[i]);
1067 ecc_code[14] = ((val >> 24) & 0xFF);
1068 ecc_code[15] = ((val >> 16) & 0xFF);
1069 ecc_code[16] = ((val >> 8) & 0xFF);
1070 ecc_code[17] = ((val >> 0) & 0xFF);
1071 val = readl(gpmc_regs->gpmc_bch_result1[i]);
1072 ecc_code[18] = ((val >> 24) & 0xFF);
1073 ecc_code[19] = ((val >> 16) & 0xFF);
1074 ecc_code[20] = ((val >> 8) & 0xFF);
1075 ecc_code[21] = ((val >> 0) & 0xFF);
1076 val = readl(gpmc_regs->gpmc_bch_result0[i]);
1077 ecc_code[22] = ((val >> 24) & 0xFF);
1078 ecc_code[23] = ((val >> 16) & 0xFF);
1079 ecc_code[24] = ((val >> 8) & 0xFF);
1080 ecc_code[25] = ((val >> 0) & 0xFF);
1086 /* ECC scheme specific syndrome customizations */
1087 switch (info->ecc_opt) {
1088 case OMAP_ECC_BCH4_CODE_HW_DETECTION_SW:
1089 /* Add constant polynomial to remainder, so that
1090 * ECC of blank pages results in 0x0 on reading back
1092 for (j = 0; j < eccbytes; j++)
1093 ecc_calc[j] ^= bch4_polynomial[j];
1095 case OMAP_ECC_BCH4_CODE_HW:
1096 /* Set 8th ECC byte as 0x0 for ROM compatibility */
1097 ecc_calc[eccbytes - 1] = 0x0;
1099 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
1100 /* Add constant polynomial to remainder, so that
1101 * ECC of blank pages results in 0x0 on reading back
1103 for (j = 0; j < eccbytes; j++)
1104 ecc_calc[j] ^= bch8_polynomial[j];
1106 case OMAP_ECC_BCH8_CODE_HW:
1107 /* Set 14th ECC byte as 0x0 for ROM compatibility */
1108 ecc_calc[eccbytes - 1] = 0x0;
1110 case OMAP_ECC_BCH16_CODE_HW:
1120 * omap_calculate_ecc_bch_sw - ECC generator for sector for SW based correction
1121 * @chip: NAND chip object
1122 * @dat: The pointer to data on which ecc is computed
1123 * @ecc_calc: Buffer storing the calculated ECC bytes
1125 * Support calculating of BCH4/8/16 ECC vectors for one sector. This is used
1126 * when SW based correction is required as ECC is required for one sector
1129 static int omap_calculate_ecc_bch_sw(struct nand_chip *chip,
1130 const u_char *dat, u_char *ecc_calc)
1132 return _omap_calculate_ecc_bch(nand_to_mtd(chip), dat, ecc_calc, 0);
1136 * omap_calculate_ecc_bch_multi - Generate ECC for multiple sectors
1137 * @mtd: MTD device structure
1138 * @dat: The pointer to data on which ecc is computed
1139 * @ecc_calc: Buffer storing the calculated ECC bytes
1141 * Support calculating of BCH4/8/16 ecc vectors for the entire page in one go.
1143 static int omap_calculate_ecc_bch_multi(struct mtd_info *mtd,
1144 const u_char *dat, u_char *ecc_calc)
1146 struct omap_nand_info *info = mtd_to_omap(mtd);
1147 int eccbytes = info->nand.ecc.bytes;
1148 unsigned long nsectors;
1151 nsectors = ((readl(info->reg.gpmc_ecc_config) >> 4) & 0x7) + 1;
1152 for (i = 0; i < nsectors; i++) {
1153 ret = _omap_calculate_ecc_bch(mtd, dat, ecc_calc, i);
1157 ecc_calc += eccbytes;
1164 * erased_sector_bitflips - count bit flips
1165 * @data: data sector buffer
1167 * @info: omap_nand_info
1169 * Check the bit flips in erased page falls below correctable level.
1170 * If falls below, report the page as erased with correctable bit
1171 * flip, else report as uncorrectable page.
1173 static int erased_sector_bitflips(u_char *data, u_char *oob,
1174 struct omap_nand_info *info)
1176 int flip_bits = 0, i;
1178 for (i = 0; i < info->nand.ecc.size; i++) {
1179 flip_bits += hweight8(~data[i]);
1180 if (flip_bits > info->nand.ecc.strength)
1184 for (i = 0; i < info->nand.ecc.bytes - 1; i++) {
1185 flip_bits += hweight8(~oob[i]);
1186 if (flip_bits > info->nand.ecc.strength)
1191 * Bit flips falls in correctable level.
1192 * Fill data area with 0xFF
1195 memset(data, 0xFF, info->nand.ecc.size);
1196 memset(oob, 0xFF, info->nand.ecc.bytes);
1203 * omap_elm_correct_data - corrects page data area in case error reported
1204 * @chip: NAND chip object
1206 * @read_ecc: ecc read from nand flash
1207 * @calc_ecc: ecc read from HW ECC registers
1209 * Calculated ecc vector reported as zero in case of non-error pages.
1210 * In case of non-zero ecc vector, first filter out erased-pages, and
1211 * then process data via ELM to detect bit-flips.
1213 static int omap_elm_correct_data(struct nand_chip *chip, u_char *data,
1214 u_char *read_ecc, u_char *calc_ecc)
1216 struct omap_nand_info *info = mtd_to_omap(nand_to_mtd(chip));
1217 struct nand_ecc_ctrl *ecc = &info->nand.ecc;
1218 int eccsteps = info->nsteps_per_eccpg;
1219 int i , j, stat = 0;
1220 int eccflag, actual_eccbytes;
1221 struct elm_errorvec err_vec[ERROR_VECTOR_MAX];
1222 u_char *ecc_vec = calc_ecc;
1223 u_char *spare_ecc = read_ecc;
1224 u_char *erased_ecc_vec;
1227 bool is_error_reported = false;
1228 u32 bit_pos, byte_pos, error_max, pos;
1231 switch (info->ecc_opt) {
1232 case OMAP_ECC_BCH4_CODE_HW:
1233 /* omit 7th ECC byte reserved for ROM code compatibility */
1234 actual_eccbytes = ecc->bytes - 1;
1235 erased_ecc_vec = bch4_vector;
1237 case OMAP_ECC_BCH8_CODE_HW:
1238 /* omit 14th ECC byte reserved for ROM code compatibility */
1239 actual_eccbytes = ecc->bytes - 1;
1240 erased_ecc_vec = bch8_vector;
1242 case OMAP_ECC_BCH16_CODE_HW:
1243 actual_eccbytes = ecc->bytes;
1244 erased_ecc_vec = bch16_vector;
1247 dev_err(&info->pdev->dev, "invalid driver configuration\n");
1251 /* Initialize elm error vector to zero */
1252 memset(err_vec, 0, sizeof(err_vec));
1254 for (i = 0; i < eccsteps ; i++) {
1255 eccflag = 0; /* initialize eccflag */
1258 * Check any error reported,
1259 * In case of error, non zero ecc reported.
1261 for (j = 0; j < actual_eccbytes; j++) {
1262 if (calc_ecc[j] != 0) {
1263 eccflag = 1; /* non zero ecc, error present */
1269 if (memcmp(calc_ecc, erased_ecc_vec,
1270 actual_eccbytes) == 0) {
1272 * calc_ecc[] matches pattern for ECC(all 0xff)
1273 * so this is definitely an erased-page
1276 buf = &data[info->nand.ecc.size * i];
1278 * count number of 0-bits in read_buf.
1279 * This check can be removed once a similar
1280 * check is introduced in generic NAND driver
1282 bitflip_count = erased_sector_bitflips(
1283 buf, read_ecc, info);
1284 if (bitflip_count) {
1286 * number of 0-bits within ECC limits
1287 * So this may be an erased-page
1289 stat += bitflip_count;
1292 * Too many 0-bits. It may be a
1293 * - programmed-page, OR
1294 * - erased-page with many bit-flips
1295 * So this page requires check by ELM
1297 err_vec[i].error_reported = true;
1298 is_error_reported = true;
1303 /* Update the ecc vector */
1304 calc_ecc += ecc->bytes;
1305 read_ecc += ecc->bytes;
1308 /* Check if any error reported */
1309 if (!is_error_reported)
1312 /* Decode BCH error using ELM module */
1313 elm_decode_bch_error_page(info->elm_dev, ecc_vec, err_vec);
1316 for (i = 0; i < eccsteps; i++) {
1317 if (err_vec[i].error_uncorrectable) {
1318 dev_err(&info->pdev->dev,
1319 "uncorrectable bit-flips found\n");
1321 } else if (err_vec[i].error_reported) {
1322 for (j = 0; j < err_vec[i].error_count; j++) {
1323 switch (info->ecc_opt) {
1324 case OMAP_ECC_BCH4_CODE_HW:
1325 /* Add 4 bits to take care of padding */
1326 pos = err_vec[i].error_loc[j] +
1329 case OMAP_ECC_BCH8_CODE_HW:
1330 case OMAP_ECC_BCH16_CODE_HW:
1331 pos = err_vec[i].error_loc[j];
1336 error_max = (ecc->size + actual_eccbytes) * 8;
1337 /* Calculate bit position of error */
1340 /* Calculate byte position of error */
1341 byte_pos = (error_max - pos - 1) / 8;
1343 if (pos < error_max) {
1344 if (byte_pos < 512) {
1345 pr_debug("bitflip@dat[%d]=%x\n",
1346 byte_pos, data[byte_pos]);
1347 data[byte_pos] ^= 1 << bit_pos;
1349 pr_debug("bitflip@oob[%d]=%x\n",
1351 spare_ecc[byte_pos - 512]);
1352 spare_ecc[byte_pos - 512] ^=
1356 dev_err(&info->pdev->dev,
1357 "invalid bit-flip @ %d:%d\n",
1364 /* Update number of correctable errors */
1365 stat = max_t(unsigned int, stat, err_vec[i].error_count);
1367 /* Update page data with sector size */
1369 spare_ecc += ecc->bytes;
1372 return (err) ? err : stat;
1376 * omap_write_page_bch - BCH ecc based write page function for entire page
1377 * @chip: nand chip info structure
1379 * @oob_required: must write chip->oob_poi to OOB
1382 * Custom write page method evolved to support multi sector writing in one shot
1384 static int omap_write_page_bch(struct nand_chip *chip, const uint8_t *buf,
1385 int oob_required, int page)
1387 struct mtd_info *mtd = nand_to_mtd(chip);
1388 struct omap_nand_info *info = mtd_to_omap(mtd);
1389 uint8_t *ecc_calc = chip->ecc.calc_buf;
1393 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
1397 for (eccpg = 0; eccpg < info->neccpg; eccpg++) {
1398 /* Enable GPMC ecc engine */
1399 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
1402 info->data_out(chip, buf + (eccpg * info->eccpg_size),
1403 info->eccpg_size, false);
1405 /* Update ecc vector from GPMC result registers */
1406 ret = omap_calculate_ecc_bch_multi(mtd,
1407 buf + (eccpg * info->eccpg_size),
1412 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc,
1414 eccpg * info->eccpg_bytes,
1420 /* Write ecc vector to OOB area */
1421 info->data_out(chip, chip->oob_poi, mtd->oobsize, false);
1423 return nand_prog_page_end_op(chip);
1427 * omap_write_subpage_bch - BCH hardware ECC based subpage write
1428 * @chip: nand chip info structure
1429 * @offset: column address of subpage within the page
1430 * @data_len: data length
1432 * @oob_required: must write chip->oob_poi to OOB
1433 * @page: page number to write
1435 * OMAP optimized subpage write method.
1437 static int omap_write_subpage_bch(struct nand_chip *chip, u32 offset,
1438 u32 data_len, const u8 *buf,
1439 int oob_required, int page)
1441 struct mtd_info *mtd = nand_to_mtd(chip);
1442 struct omap_nand_info *info = mtd_to_omap(mtd);
1443 u8 *ecc_calc = chip->ecc.calc_buf;
1444 int ecc_size = chip->ecc.size;
1445 int ecc_bytes = chip->ecc.bytes;
1446 u32 start_step = offset / ecc_size;
1447 u32 end_step = (offset + data_len - 1) / ecc_size;
1452 * Write entire page at one go as it would be optimal
1453 * as ECC is calculated by hardware.
1454 * ECC is calculated for all subpages but we choose
1455 * only what we want.
1457 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
1461 for (eccpg = 0; eccpg < info->neccpg; eccpg++) {
1462 /* Enable GPMC ECC engine */
1463 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
1466 info->data_out(chip, buf + (eccpg * info->eccpg_size),
1467 info->eccpg_size, false);
1469 for (step = 0; step < info->nsteps_per_eccpg; step++) {
1470 unsigned int base_step = eccpg * info->nsteps_per_eccpg;
1471 const u8 *bufoffs = buf + (eccpg * info->eccpg_size);
1473 /* Mask ECC of un-touched subpages with 0xFFs */
1474 if ((step + base_step) < start_step ||
1475 (step + base_step) > end_step)
1476 memset(ecc_calc + (step * ecc_bytes), 0xff,
1479 ret = _omap_calculate_ecc_bch(mtd,
1480 bufoffs + (step * ecc_size),
1481 ecc_calc + (step * ecc_bytes),
1489 * Copy the calculated ECC for the whole page including the
1490 * masked values (0xFF) corresponding to unwritten subpages.
1492 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi,
1493 eccpg * info->eccpg_bytes,
1499 /* write OOB buffer to NAND device */
1500 info->data_out(chip, chip->oob_poi, mtd->oobsize, false);
1502 return nand_prog_page_end_op(chip);
1506 * omap_read_page_bch - BCH ecc based page read function for entire page
1507 * @chip: nand chip info structure
1508 * @buf: buffer to store read data
1509 * @oob_required: caller requires OOB data read to chip->oob_poi
1510 * @page: page number to read
1512 * For BCH ecc scheme, GPMC used for syndrome calculation and ELM module
1513 * used for error correction.
1514 * Custom method evolved to support ELM error correction & multi sector
1515 * reading. On reading page data area is read along with OOB data with
1516 * ecc engine enabled. ecc vector updated after read of OOB data.
1517 * For non error pages ecc vector reported as zero.
1519 static int omap_read_page_bch(struct nand_chip *chip, uint8_t *buf,
1520 int oob_required, int page)
1522 struct mtd_info *mtd = nand_to_mtd(chip);
1523 struct omap_nand_info *info = mtd_to_omap(mtd);
1524 uint8_t *ecc_calc = chip->ecc.calc_buf;
1525 uint8_t *ecc_code = chip->ecc.code_buf;
1526 unsigned int max_bitflips = 0, eccpg;
1529 ret = nand_read_page_op(chip, page, 0, NULL, 0);
1533 for (eccpg = 0; eccpg < info->neccpg; eccpg++) {
1534 /* Enable GPMC ecc engine */
1535 chip->ecc.hwctl(chip, NAND_ECC_READ);
1538 ret = nand_change_read_column_op(chip, eccpg * info->eccpg_size,
1539 buf + (eccpg * info->eccpg_size),
1540 info->eccpg_size, false);
1544 /* Read oob bytes */
1545 ret = nand_change_read_column_op(chip,
1546 mtd->writesize + BBM_LEN +
1547 (eccpg * info->eccpg_bytes),
1548 chip->oob_poi + BBM_LEN +
1549 (eccpg * info->eccpg_bytes),
1550 info->eccpg_bytes, false);
1554 /* Calculate ecc bytes */
1555 ret = omap_calculate_ecc_bch_multi(mtd,
1556 buf + (eccpg * info->eccpg_size),
1561 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code,
1563 eccpg * info->eccpg_bytes,
1568 stat = chip->ecc.correct(chip,
1569 buf + (eccpg * info->eccpg_size),
1570 ecc_code, ecc_calc);
1572 mtd->ecc_stats.failed++;
1574 mtd->ecc_stats.corrected += stat;
1575 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1579 return max_bitflips;
1583 * is_elm_present - checks for presence of ELM module by scanning DT nodes
1584 * @info: NAND device structure containing platform data
1585 * @elm_node: ELM's DT node
1587 static bool is_elm_present(struct omap_nand_info *info,
1588 struct device_node *elm_node)
1590 struct platform_device *pdev;
1592 /* check whether elm-id is passed via DT */
1594 dev_err(&info->pdev->dev, "ELM devicetree node not found\n");
1597 pdev = of_find_device_by_node(elm_node);
1598 /* check whether ELM device is registered */
1600 dev_err(&info->pdev->dev, "ELM device not found\n");
1603 /* ELM module available, now configure it */
1604 info->elm_dev = &pdev->dev;
1608 static bool omap2_nand_ecc_check(struct omap_nand_info *info)
1610 bool ecc_needs_bch, ecc_needs_omap_bch, ecc_needs_elm;
1612 switch (info->ecc_opt) {
1613 case OMAP_ECC_BCH4_CODE_HW_DETECTION_SW:
1614 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
1615 ecc_needs_omap_bch = false;
1616 ecc_needs_bch = true;
1617 ecc_needs_elm = false;
1619 case OMAP_ECC_BCH4_CODE_HW:
1620 case OMAP_ECC_BCH8_CODE_HW:
1621 case OMAP_ECC_BCH16_CODE_HW:
1622 ecc_needs_omap_bch = true;
1623 ecc_needs_bch = false;
1624 ecc_needs_elm = true;
1627 ecc_needs_omap_bch = false;
1628 ecc_needs_bch = false;
1629 ecc_needs_elm = false;
1633 if (ecc_needs_bch && !IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_BCH)) {
1634 dev_err(&info->pdev->dev,
1635 "CONFIG_MTD_NAND_ECC_SW_BCH not enabled\n");
1638 if (ecc_needs_omap_bch && !IS_ENABLED(CONFIG_MTD_NAND_OMAP_BCH)) {
1639 dev_err(&info->pdev->dev,
1640 "CONFIG_MTD_NAND_OMAP_BCH not enabled\n");
1643 if (ecc_needs_elm && !is_elm_present(info, info->elm_of_node)) {
1644 dev_err(&info->pdev->dev, "ELM not available\n");
1651 static const char * const nand_xfer_types[] = {
1652 [NAND_OMAP_PREFETCH_POLLED] = "prefetch-polled",
1653 [NAND_OMAP_POLLED] = "polled",
1654 [NAND_OMAP_PREFETCH_DMA] = "prefetch-dma",
1655 [NAND_OMAP_PREFETCH_IRQ] = "prefetch-irq",
1658 static int omap_get_dt_info(struct device *dev, struct omap_nand_info *info)
1660 struct device_node *child = dev->of_node;
1665 if (of_property_read_u32(child, "reg", &cs) < 0) {
1666 dev_err(dev, "reg not found in DT\n");
1672 /* detect availability of ELM module. Won't be present pre-OMAP4 */
1673 info->elm_of_node = of_parse_phandle(child, "ti,elm-id", 0);
1674 if (!info->elm_of_node) {
1675 info->elm_of_node = of_parse_phandle(child, "elm_id", 0);
1676 if (!info->elm_of_node)
1677 dev_dbg(dev, "ti,elm-id not in DT\n");
1680 /* select ecc-scheme for NAND */
1681 if (of_property_read_string(child, "ti,nand-ecc-opt", &s)) {
1682 dev_err(dev, "ti,nand-ecc-opt not found\n");
1686 if (!strcmp(s, "sw")) {
1687 info->ecc_opt = OMAP_ECC_HAM1_CODE_SW;
1688 } else if (!strcmp(s, "ham1") ||
1689 !strcmp(s, "hw") || !strcmp(s, "hw-romcode")) {
1690 info->ecc_opt = OMAP_ECC_HAM1_CODE_HW;
1691 } else if (!strcmp(s, "bch4")) {
1692 if (info->elm_of_node)
1693 info->ecc_opt = OMAP_ECC_BCH4_CODE_HW;
1695 info->ecc_opt = OMAP_ECC_BCH4_CODE_HW_DETECTION_SW;
1696 } else if (!strcmp(s, "bch8")) {
1697 if (info->elm_of_node)
1698 info->ecc_opt = OMAP_ECC_BCH8_CODE_HW;
1700 info->ecc_opt = OMAP_ECC_BCH8_CODE_HW_DETECTION_SW;
1701 } else if (!strcmp(s, "bch16")) {
1702 info->ecc_opt = OMAP_ECC_BCH16_CODE_HW;
1704 dev_err(dev, "unrecognized value for ti,nand-ecc-opt\n");
1708 /* select data transfer mode */
1709 if (!of_property_read_string(child, "ti,nand-xfer-type", &s)) {
1710 for (i = 0; i < ARRAY_SIZE(nand_xfer_types); i++) {
1711 if (!strcasecmp(s, nand_xfer_types[i])) {
1712 info->xfer_type = i;
1717 dev_err(dev, "unrecognized value for ti,nand-xfer-type\n");
1724 static int omap_ooblayout_ecc(struct mtd_info *mtd, int section,
1725 struct mtd_oob_region *oobregion)
1727 struct omap_nand_info *info = mtd_to_omap(mtd);
1728 struct nand_chip *chip = &info->nand;
1731 if (info->ecc_opt == OMAP_ECC_HAM1_CODE_HW &&
1732 !(chip->options & NAND_BUSWIDTH_16))
1738 oobregion->offset = off;
1739 oobregion->length = chip->ecc.total;
1744 static int omap_ooblayout_free(struct mtd_info *mtd, int section,
1745 struct mtd_oob_region *oobregion)
1747 struct omap_nand_info *info = mtd_to_omap(mtd);
1748 struct nand_chip *chip = &info->nand;
1751 if (info->ecc_opt == OMAP_ECC_HAM1_CODE_HW &&
1752 !(chip->options & NAND_BUSWIDTH_16))
1758 off += chip->ecc.total;
1759 if (off >= mtd->oobsize)
1762 oobregion->offset = off;
1763 oobregion->length = mtd->oobsize - off;
1768 static const struct mtd_ooblayout_ops omap_ooblayout_ops = {
1769 .ecc = omap_ooblayout_ecc,
1770 .free = omap_ooblayout_free,
1773 static int omap_sw_ooblayout_ecc(struct mtd_info *mtd, int section,
1774 struct mtd_oob_region *oobregion)
1776 struct nand_device *nand = mtd_to_nanddev(mtd);
1777 unsigned int nsteps = nanddev_get_ecc_nsteps(nand);
1778 unsigned int ecc_bytes = nanddev_get_ecc_bytes_per_step(nand);
1781 if (section >= nsteps)
1785 * When SW correction is employed, one OMAP specific marker byte is
1786 * reserved after each ECC step.
1788 oobregion->offset = off + (section * (ecc_bytes + 1));
1789 oobregion->length = ecc_bytes;
1794 static int omap_sw_ooblayout_free(struct mtd_info *mtd, int section,
1795 struct mtd_oob_region *oobregion)
1797 struct nand_device *nand = mtd_to_nanddev(mtd);
1798 unsigned int nsteps = nanddev_get_ecc_nsteps(nand);
1799 unsigned int ecc_bytes = nanddev_get_ecc_bytes_per_step(nand);
1806 * When SW correction is employed, one OMAP specific marker byte is
1807 * reserved after each ECC step.
1809 off += ((ecc_bytes + 1) * nsteps);
1810 if (off >= mtd->oobsize)
1813 oobregion->offset = off;
1814 oobregion->length = mtd->oobsize - off;
1819 static const struct mtd_ooblayout_ops omap_sw_ooblayout_ops = {
1820 .ecc = omap_sw_ooblayout_ecc,
1821 .free = omap_sw_ooblayout_free,
1824 static int omap_nand_attach_chip(struct nand_chip *chip)
1826 struct mtd_info *mtd = nand_to_mtd(chip);
1827 struct omap_nand_info *info = mtd_to_omap(mtd);
1828 struct device *dev = &info->pdev->dev;
1829 int min_oobbytes = BBM_LEN;
1830 int elm_bch_strength = -1;
1831 int oobbytes_per_step;
1832 dma_cap_mask_t mask;
1835 if (chip->bbt_options & NAND_BBT_USE_FLASH)
1836 chip->bbt_options |= NAND_BBT_NO_OOB;
1838 chip->options |= NAND_SKIP_BBTSCAN;
1840 /* Re-populate low-level callbacks based on xfer modes */
1841 switch (info->xfer_type) {
1842 case NAND_OMAP_PREFETCH_POLLED:
1843 info->data_in = omap_nand_data_in_pref;
1844 info->data_out = omap_nand_data_out_pref;
1847 case NAND_OMAP_POLLED:
1848 /* Use nand_base defaults for {read,write}_buf */
1851 case NAND_OMAP_PREFETCH_DMA:
1853 dma_cap_set(DMA_SLAVE, mask);
1854 info->dma = dma_request_chan(dev->parent, "rxtx");
1856 if (IS_ERR(info->dma)) {
1857 dev_err(dev, "DMA engine request failed\n");
1858 return PTR_ERR(info->dma);
1860 struct dma_slave_config cfg;
1862 memset(&cfg, 0, sizeof(cfg));
1863 cfg.src_addr = info->phys_base;
1864 cfg.dst_addr = info->phys_base;
1865 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1866 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1867 cfg.src_maxburst = 16;
1868 cfg.dst_maxburst = 16;
1869 err = dmaengine_slave_config(info->dma, &cfg);
1872 "DMA engine slave config failed: %d\n",
1877 info->data_in = omap_nand_data_in_dma_pref;
1878 info->data_out = omap_nand_data_out_dma_pref;
1882 case NAND_OMAP_PREFETCH_IRQ:
1883 info->gpmc_irq_fifo = platform_get_irq(info->pdev, 0);
1884 if (info->gpmc_irq_fifo <= 0)
1886 err = devm_request_irq(dev, info->gpmc_irq_fifo,
1887 omap_nand_irq, IRQF_SHARED,
1888 "gpmc-nand-fifo", info);
1890 dev_err(dev, "Requesting IRQ %d, error %d\n",
1891 info->gpmc_irq_fifo, err);
1892 info->gpmc_irq_fifo = 0;
1896 info->gpmc_irq_count = platform_get_irq(info->pdev, 1);
1897 if (info->gpmc_irq_count <= 0)
1899 err = devm_request_irq(dev, info->gpmc_irq_count,
1900 omap_nand_irq, IRQF_SHARED,
1901 "gpmc-nand-count", info);
1903 dev_err(dev, "Requesting IRQ %d, error %d\n",
1904 info->gpmc_irq_count, err);
1905 info->gpmc_irq_count = 0;
1909 info->data_in = omap_nand_data_in_irq_pref;
1910 info->data_out = omap_nand_data_out_irq_pref;
1914 dev_err(dev, "xfer_type %d not supported!\n", info->xfer_type);
1918 if (!omap2_nand_ecc_check(info))
1922 * Bail out earlier to let NAND_ECC_ENGINE_TYPE_SOFT code create its own
1923 * ooblayout instead of using ours.
1925 if (info->ecc_opt == OMAP_ECC_HAM1_CODE_SW) {
1926 chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
1927 chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
1931 /* Populate MTD interface based on ECC scheme */
1932 switch (info->ecc_opt) {
1933 case OMAP_ECC_HAM1_CODE_HW:
1934 dev_info(dev, "nand: using OMAP_ECC_HAM1_CODE_HW\n");
1935 chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
1936 chip->ecc.bytes = 3;
1937 chip->ecc.size = 512;
1938 chip->ecc.strength = 1;
1939 chip->ecc.calculate = omap_calculate_ecc;
1940 chip->ecc.hwctl = omap_enable_hwecc;
1941 chip->ecc.correct = omap_correct_data;
1942 mtd_set_ooblayout(mtd, &omap_ooblayout_ops);
1943 oobbytes_per_step = chip->ecc.bytes;
1945 if (!(chip->options & NAND_BUSWIDTH_16))
1950 case OMAP_ECC_BCH4_CODE_HW_DETECTION_SW:
1951 pr_info("nand: using OMAP_ECC_BCH4_CODE_HW_DETECTION_SW\n");
1952 chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
1953 chip->ecc.size = 512;
1954 chip->ecc.bytes = 7;
1955 chip->ecc.strength = 4;
1956 chip->ecc.hwctl = omap_enable_hwecc_bch;
1957 chip->ecc.correct = rawnand_sw_bch_correct;
1958 chip->ecc.calculate = omap_calculate_ecc_bch_sw;
1959 mtd_set_ooblayout(mtd, &omap_sw_ooblayout_ops);
1960 /* Reserve one byte for the OMAP marker */
1961 oobbytes_per_step = chip->ecc.bytes + 1;
1962 /* Software BCH library is used for locating errors */
1963 err = rawnand_sw_bch_init(chip);
1965 dev_err(dev, "Unable to use BCH library\n");
1970 case OMAP_ECC_BCH4_CODE_HW:
1971 pr_info("nand: using OMAP_ECC_BCH4_CODE_HW ECC scheme\n");
1972 chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
1973 chip->ecc.size = 512;
1974 /* 14th bit is kept reserved for ROM-code compatibility */
1975 chip->ecc.bytes = 7 + 1;
1976 chip->ecc.strength = 4;
1977 chip->ecc.hwctl = omap_enable_hwecc_bch;
1978 chip->ecc.correct = omap_elm_correct_data;
1979 chip->ecc.read_page = omap_read_page_bch;
1980 chip->ecc.write_page = omap_write_page_bch;
1981 chip->ecc.write_subpage = omap_write_subpage_bch;
1982 mtd_set_ooblayout(mtd, &omap_ooblayout_ops);
1983 oobbytes_per_step = chip->ecc.bytes;
1984 elm_bch_strength = BCH4_ECC;
1987 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
1988 pr_info("nand: using OMAP_ECC_BCH8_CODE_HW_DETECTION_SW\n");
1989 chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
1990 chip->ecc.size = 512;
1991 chip->ecc.bytes = 13;
1992 chip->ecc.strength = 8;
1993 chip->ecc.hwctl = omap_enable_hwecc_bch;
1994 chip->ecc.correct = rawnand_sw_bch_correct;
1995 chip->ecc.calculate = omap_calculate_ecc_bch_sw;
1996 mtd_set_ooblayout(mtd, &omap_sw_ooblayout_ops);
1997 /* Reserve one byte for the OMAP marker */
1998 oobbytes_per_step = chip->ecc.bytes + 1;
1999 /* Software BCH library is used for locating errors */
2000 err = rawnand_sw_bch_init(chip);
2002 dev_err(dev, "unable to use BCH library\n");
2007 case OMAP_ECC_BCH8_CODE_HW:
2008 pr_info("nand: using OMAP_ECC_BCH8_CODE_HW ECC scheme\n");
2009 chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
2010 chip->ecc.size = 512;
2011 /* 14th bit is kept reserved for ROM-code compatibility */
2012 chip->ecc.bytes = 13 + 1;
2013 chip->ecc.strength = 8;
2014 chip->ecc.hwctl = omap_enable_hwecc_bch;
2015 chip->ecc.correct = omap_elm_correct_data;
2016 chip->ecc.read_page = omap_read_page_bch;
2017 chip->ecc.write_page = omap_write_page_bch;
2018 chip->ecc.write_subpage = omap_write_subpage_bch;
2019 mtd_set_ooblayout(mtd, &omap_ooblayout_ops);
2020 oobbytes_per_step = chip->ecc.bytes;
2021 elm_bch_strength = BCH8_ECC;
2024 case OMAP_ECC_BCH16_CODE_HW:
2025 pr_info("Using OMAP_ECC_BCH16_CODE_HW ECC scheme\n");
2026 chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
2027 chip->ecc.size = 512;
2028 chip->ecc.bytes = 26;
2029 chip->ecc.strength = 16;
2030 chip->ecc.hwctl = omap_enable_hwecc_bch;
2031 chip->ecc.correct = omap_elm_correct_data;
2032 chip->ecc.read_page = omap_read_page_bch;
2033 chip->ecc.write_page = omap_write_page_bch;
2034 chip->ecc.write_subpage = omap_write_subpage_bch;
2035 mtd_set_ooblayout(mtd, &omap_ooblayout_ops);
2036 oobbytes_per_step = chip->ecc.bytes;
2037 elm_bch_strength = BCH16_ECC;
2040 dev_err(dev, "Invalid or unsupported ECC scheme\n");
2044 if (elm_bch_strength >= 0) {
2045 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2046 info->neccpg = chip->ecc.steps / ERROR_VECTOR_MAX;
2048 info->nsteps_per_eccpg = ERROR_VECTOR_MAX;
2051 info->nsteps_per_eccpg = chip->ecc.steps;
2053 info->eccpg_size = info->nsteps_per_eccpg * chip->ecc.size;
2054 info->eccpg_bytes = info->nsteps_per_eccpg * chip->ecc.bytes;
2056 err = elm_config(info->elm_dev, elm_bch_strength,
2057 info->nsteps_per_eccpg, chip->ecc.size,
2063 /* Check if NAND device's OOB is enough to store ECC signatures */
2064 min_oobbytes += (oobbytes_per_step *
2065 (mtd->writesize / chip->ecc.size));
2066 if (mtd->oobsize < min_oobbytes) {
2068 "Not enough OOB bytes: required = %d, available=%d\n",
2069 min_oobbytes, mtd->oobsize);
2076 static void omap_nand_data_in(struct nand_chip *chip, void *buf,
2077 unsigned int len, bool force_8bit)
2079 struct omap_nand_info *info = mtd_to_omap(nand_to_mtd(chip));
2080 u32 alignment = ((uintptr_t)buf | len) & 3;
2082 if (force_8bit || (alignment & 1))
2083 ioread8_rep(info->fifo, buf, len);
2084 else if (alignment & 3)
2085 ioread16_rep(info->fifo, buf, len >> 1);
2087 ioread32_rep(info->fifo, buf, len >> 2);
2090 static void omap_nand_data_out(struct nand_chip *chip,
2091 const void *buf, unsigned int len,
2094 struct omap_nand_info *info = mtd_to_omap(nand_to_mtd(chip));
2095 u32 alignment = ((uintptr_t)buf | len) & 3;
2097 if (force_8bit || (alignment & 1))
2098 iowrite8_rep(info->fifo, buf, len);
2099 else if (alignment & 3)
2100 iowrite16_rep(info->fifo, buf, len >> 1);
2102 iowrite32_rep(info->fifo, buf, len >> 2);
2105 static int omap_nand_exec_instr(struct nand_chip *chip,
2106 const struct nand_op_instr *instr)
2108 struct omap_nand_info *info = mtd_to_omap(nand_to_mtd(chip));
2112 switch (instr->type) {
2113 case NAND_OP_CMD_INSTR:
2114 iowrite8(instr->ctx.cmd.opcode,
2115 info->reg.gpmc_nand_command);
2118 case NAND_OP_ADDR_INSTR:
2119 for (i = 0; i < instr->ctx.addr.naddrs; i++) {
2120 iowrite8(instr->ctx.addr.addrs[i],
2121 info->reg.gpmc_nand_address);
2125 case NAND_OP_DATA_IN_INSTR:
2126 info->data_in(chip, instr->ctx.data.buf.in,
2127 instr->ctx.data.len,
2128 instr->ctx.data.force_8bit);
2131 case NAND_OP_DATA_OUT_INSTR:
2132 info->data_out(chip, instr->ctx.data.buf.out,
2133 instr->ctx.data.len,
2134 instr->ctx.data.force_8bit);
2137 case NAND_OP_WAITRDY_INSTR:
2138 ret = info->ready_gpiod ?
2139 nand_gpio_waitrdy(chip, info->ready_gpiod, instr->ctx.waitrdy.timeout_ms) :
2140 nand_soft_waitrdy(chip, instr->ctx.waitrdy.timeout_ms);
2146 if (instr->delay_ns)
2147 ndelay(instr->delay_ns);
2152 static int omap_nand_exec_op(struct nand_chip *chip,
2153 const struct nand_operation *op,
2161 for (i = 0; i < op->ninstrs; i++) {
2164 ret = omap_nand_exec_instr(chip, &op->instrs[i]);
2172 static const struct nand_controller_ops omap_nand_controller_ops = {
2173 .attach_chip = omap_nand_attach_chip,
2174 .exec_op = omap_nand_exec_op,
2177 /* Shared among all NAND instances to synchronize access to the ECC Engine */
2178 static struct nand_controller omap_gpmc_controller;
2179 static bool omap_gpmc_controller_initialized;
2181 static int omap_nand_probe(struct platform_device *pdev)
2183 struct omap_nand_info *info;
2184 struct mtd_info *mtd;
2185 struct nand_chip *nand_chip;
2187 struct resource *res;
2188 struct device *dev = &pdev->dev;
2189 void __iomem *vaddr;
2191 info = devm_kzalloc(&pdev->dev, sizeof(struct omap_nand_info),
2198 err = omap_get_dt_info(dev, info);
2202 info->ops = gpmc_omap_get_nand_ops(&info->reg, info->gpmc_cs);
2204 dev_err(&pdev->dev, "Failed to get GPMC->NAND interface\n");
2208 nand_chip = &info->nand;
2209 mtd = nand_to_mtd(nand_chip);
2210 mtd->dev.parent = &pdev->dev;
2211 nand_set_flash_node(nand_chip, dev->of_node);
2214 mtd->name = devm_kasprintf(&pdev->dev, GFP_KERNEL,
2215 "omap2-nand.%d", info->gpmc_cs);
2217 dev_err(&pdev->dev, "Failed to set MTD name\n");
2222 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2223 vaddr = devm_ioremap_resource(&pdev->dev, res);
2225 return PTR_ERR(vaddr);
2228 info->phys_base = res->start;
2230 if (!omap_gpmc_controller_initialized) {
2231 omap_gpmc_controller.ops = &omap_nand_controller_ops;
2232 nand_controller_init(&omap_gpmc_controller);
2233 omap_gpmc_controller_initialized = true;
2236 nand_chip->controller = &omap_gpmc_controller;
2238 info->ready_gpiod = devm_gpiod_get_optional(&pdev->dev, "rb",
2240 if (IS_ERR(info->ready_gpiod)) {
2241 dev_err(dev, "failed to get ready gpio\n");
2242 return PTR_ERR(info->ready_gpiod);
2245 if (info->flash_bbt)
2246 nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
2248 /* default operations */
2249 info->data_in = omap_nand_data_in;
2250 info->data_out = omap_nand_data_out;
2252 err = nand_scan(nand_chip, 1);
2256 err = mtd_device_register(mtd, NULL, 0);
2260 platform_set_drvdata(pdev, mtd);
2265 nand_cleanup(nand_chip);
2268 if (!IS_ERR_OR_NULL(info->dma))
2269 dma_release_channel(info->dma);
2271 rawnand_sw_bch_cleanup(nand_chip);
2276 static int omap_nand_remove(struct platform_device *pdev)
2278 struct mtd_info *mtd = platform_get_drvdata(pdev);
2279 struct nand_chip *nand_chip = mtd_to_nand(mtd);
2280 struct omap_nand_info *info = mtd_to_omap(mtd);
2283 rawnand_sw_bch_cleanup(nand_chip);
2286 dma_release_channel(info->dma);
2287 ret = mtd_device_unregister(mtd);
2289 nand_cleanup(nand_chip);
2293 /* omap_nand_ids defined in linux/platform_data/mtd-nand-omap2.h */
2294 MODULE_DEVICE_TABLE(of, omap_nand_ids);
2296 static struct platform_driver omap_nand_driver = {
2297 .probe = omap_nand_probe,
2298 .remove = omap_nand_remove,
2300 .name = DRIVER_NAME,
2301 .of_match_table = of_match_ptr(omap_nand_ids),
2305 module_platform_driver(omap_nand_driver);
2307 MODULE_ALIAS("platform:" DRIVER_NAME);
2308 MODULE_LICENSE("GPL");
2309 MODULE_DESCRIPTION("Glue layer for NAND flash on TI OMAP boards");