1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2014 Panasonic Corporation
4 * Copyright (C) 2013-2014, Altera Corporation <www.altera.com>
5 * Copyright (C) 2009-2010, Intel Corporation and its suppliers.
10 #include <linux/bitfield.h>
11 #include <linux/dma-direction.h>
12 #include <linux/errno.h>
14 #include <linux/mtd/mtd.h>
15 #include <linux/mtd/rawnand.h>
19 static dma_addr_t dma_map_single(void *dev, void *ptr, size_t size,
20 enum dma_data_direction dir)
22 unsigned long addr = (unsigned long)ptr;
24 if (dir == DMA_FROM_DEVICE)
25 invalidate_dcache_range(addr, addr + size);
27 flush_dcache_range(addr, addr + size);
32 static void dma_unmap_single(void *dev, dma_addr_t addr, size_t size,
33 enum dma_data_direction dir)
35 if (dir != DMA_TO_DEVICE)
36 invalidate_dcache_range(addr, addr + size);
39 static int dma_mapping_error(void *dev, dma_addr_t addr)
44 #define DENALI_NAND_NAME "denali-nand"
46 /* for Indexed Addressing */
47 #define DENALI_INDEXED_CTRL 0x00
48 #define DENALI_INDEXED_DATA 0x10
50 #define DENALI_MAP00 (0 << 26) /* direct access to buffer */
51 #define DENALI_MAP01 (1 << 26) /* read/write pages in PIO */
52 #define DENALI_MAP10 (2 << 26) /* high-level control plane */
53 #define DENALI_MAP11 (3 << 26) /* direct controller access */
55 /* MAP11 access cycle type */
56 #define DENALI_MAP11_CMD ((DENALI_MAP11) | 0) /* command cycle */
57 #define DENALI_MAP11_ADDR ((DENALI_MAP11) | 1) /* address cycle */
58 #define DENALI_MAP11_DATA ((DENALI_MAP11) | 2) /* data cycle */
61 #define DENALI_ERASE 0x01
63 #define DENALI_BANK(denali) ((denali)->active_bank << 24)
65 #define DENALI_INVALID_BANK -1
66 #define DENALI_NR_BANKS 4
69 * The bus interface clock, clk_x, is phase aligned with the core clock. The
70 * clk_x is an integral multiple N of the core clk. The value N is configured
71 * at IP delivery time, and its available value is 4, 5, or 6. We need to align
72 * to the largest value to make it work with any possible configuration.
74 #define DENALI_CLK_X_MULT 6
76 static inline struct denali_nand_info *mtd_to_denali(struct mtd_info *mtd)
78 return container_of(mtd_to_nand(mtd), struct denali_nand_info, nand);
82 * Direct Addressing - the slave address forms the control information (command
83 * type, bank, block, and page address). The slave data is the actual data to
84 * be transferred. This mode requires 28 bits of address region allocated.
86 static u32 denali_direct_read(struct denali_nand_info *denali, u32 addr)
88 return ioread32(denali->host + addr);
91 static void denali_direct_write(struct denali_nand_info *denali, u32 addr,
94 iowrite32(data, denali->host + addr);
98 * Indexed Addressing - address translation module intervenes in passing the
99 * control information. This mode reduces the required address range. The
100 * control information and transferred data are latched by the registers in
101 * the translation module.
103 static u32 denali_indexed_read(struct denali_nand_info *denali, u32 addr)
105 iowrite32(addr, denali->host + DENALI_INDEXED_CTRL);
106 return ioread32(denali->host + DENALI_INDEXED_DATA);
109 static void denali_indexed_write(struct denali_nand_info *denali, u32 addr,
112 iowrite32(addr, denali->host + DENALI_INDEXED_CTRL);
113 iowrite32(data, denali->host + DENALI_INDEXED_DATA);
117 * Use the configuration feature register to determine the maximum number of
118 * banks that the hardware supports.
120 static void denali_detect_max_banks(struct denali_nand_info *denali)
122 uint32_t features = ioread32(denali->reg + FEATURES);
124 denali->max_banks = 1 << FIELD_GET(FEATURES__N_BANKS, features);
126 /* the encoding changed from rev 5.0 to 5.1 */
127 if (denali->revision < 0x0501)
128 denali->max_banks <<= 1;
131 static void __maybe_unused denali_enable_irq(struct denali_nand_info *denali)
135 for (i = 0; i < DENALI_NR_BANKS; i++)
136 iowrite32(U32_MAX, denali->reg + INTR_EN(i));
137 iowrite32(GLOBAL_INT_EN_FLAG, denali->reg + GLOBAL_INT_ENABLE);
140 static void __maybe_unused denali_disable_irq(struct denali_nand_info *denali)
144 for (i = 0; i < DENALI_NR_BANKS; i++)
145 iowrite32(0, denali->reg + INTR_EN(i));
146 iowrite32(0, denali->reg + GLOBAL_INT_ENABLE);
149 static void denali_clear_irq(struct denali_nand_info *denali,
150 int bank, uint32_t irq_status)
152 /* write one to clear bits */
153 iowrite32(irq_status, denali->reg + INTR_STATUS(bank));
156 static void denali_clear_irq_all(struct denali_nand_info *denali)
160 for (i = 0; i < DENALI_NR_BANKS; i++)
161 denali_clear_irq(denali, i, U32_MAX);
164 static void __denali_check_irq(struct denali_nand_info *denali)
169 for (i = 0; i < DENALI_NR_BANKS; i++) {
170 irq_status = ioread32(denali->reg + INTR_STATUS(i));
171 denali_clear_irq(denali, i, irq_status);
173 if (i != denali->active_bank)
176 denali->irq_status |= irq_status;
180 static void denali_reset_irq(struct denali_nand_info *denali)
182 denali->irq_status = 0;
183 denali->irq_mask = 0;
186 static uint32_t denali_wait_for_irq(struct denali_nand_info *denali,
189 unsigned long time_left = 1000000;
192 __denali_check_irq(denali);
194 if (irq_mask & denali->irq_status)
195 return denali->irq_status;
201 dev_err(denali->dev, "timeout while waiting for irq 0x%x\n",
206 return denali->irq_status;
209 static uint32_t denali_check_irq(struct denali_nand_info *denali)
211 __denali_check_irq(denali);
213 return denali->irq_status;
216 static void denali_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
218 struct denali_nand_info *denali = mtd_to_denali(mtd);
219 u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali);
222 for (i = 0; i < len; i++)
223 buf[i] = denali->host_read(denali, addr);
226 static void denali_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
228 struct denali_nand_info *denali = mtd_to_denali(mtd);
229 u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali);
232 for (i = 0; i < len; i++)
233 denali->host_write(denali, addr, buf[i]);
236 static void denali_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
238 struct denali_nand_info *denali = mtd_to_denali(mtd);
239 u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali);
240 uint16_t *buf16 = (uint16_t *)buf;
243 for (i = 0; i < len / 2; i++)
244 buf16[i] = denali->host_read(denali, addr);
247 static void denali_write_buf16(struct mtd_info *mtd, const uint8_t *buf,
250 struct denali_nand_info *denali = mtd_to_denali(mtd);
251 u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali);
252 const uint16_t *buf16 = (const uint16_t *)buf;
255 for (i = 0; i < len / 2; i++)
256 denali->host_write(denali, addr, buf16[i]);
259 static uint8_t denali_read_byte(struct mtd_info *mtd)
263 denali_read_buf(mtd, &byte, 1);
268 static void denali_write_byte(struct mtd_info *mtd, uint8_t byte)
270 denali_write_buf(mtd, &byte, 1);
273 static uint16_t denali_read_word(struct mtd_info *mtd)
277 denali_read_buf16(mtd, (uint8_t *)&word, 2);
282 static void denali_cmd_ctrl(struct mtd_info *mtd, int dat, unsigned int ctrl)
284 struct denali_nand_info *denali = mtd_to_denali(mtd);
288 type = DENALI_MAP11_CMD;
289 else if (ctrl & NAND_ALE)
290 type = DENALI_MAP11_ADDR;
295 * Some commands are followed by chip->dev_ready or chip->waitfunc.
296 * irq_status must be cleared here to catch the R/B# interrupt later.
298 if (ctrl & NAND_CTRL_CHANGE)
299 denali_reset_irq(denali);
301 denali->host_write(denali, DENALI_BANK(denali) | type, dat);
304 static int denali_dev_ready(struct mtd_info *mtd)
306 struct denali_nand_info *denali = mtd_to_denali(mtd);
308 return !!(denali_check_irq(denali) & INTR__INT_ACT);
311 static int denali_check_erased_page(struct mtd_info *mtd,
312 struct nand_chip *chip, uint8_t *buf,
313 unsigned long uncor_ecc_flags,
314 unsigned int max_bitflips)
316 uint8_t *ecc_code = chip->buffers->ecccode;
317 int ecc_steps = chip->ecc.steps;
318 int ecc_size = chip->ecc.size;
319 int ecc_bytes = chip->ecc.bytes;
322 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
327 for (i = 0; i < ecc_steps; i++) {
328 if (!(uncor_ecc_flags & BIT(i)))
331 stat = nand_check_erased_ecc_chunk(buf, ecc_size,
336 mtd->ecc_stats.failed++;
338 mtd->ecc_stats.corrected += stat;
339 max_bitflips = max_t(unsigned int, max_bitflips, stat);
343 ecc_code += ecc_bytes;
349 static int denali_hw_ecc_fixup(struct mtd_info *mtd,
350 struct denali_nand_info *denali,
351 unsigned long *uncor_ecc_flags)
353 struct nand_chip *chip = mtd_to_nand(mtd);
354 int bank = denali->active_bank;
356 unsigned int max_bitflips;
358 ecc_cor = ioread32(denali->reg + ECC_COR_INFO(bank));
359 ecc_cor >>= ECC_COR_INFO__SHIFT(bank);
361 if (ecc_cor & ECC_COR_INFO__UNCOR_ERR) {
363 * This flag is set when uncorrectable error occurs at least in
364 * one ECC sector. We can not know "how many sectors", or
365 * "which sector(s)". We need erase-page check for all sectors.
367 *uncor_ecc_flags = GENMASK(chip->ecc.steps - 1, 0);
371 max_bitflips = FIELD_GET(ECC_COR_INFO__MAX_ERRORS, ecc_cor);
374 * The register holds the maximum of per-sector corrected bitflips.
375 * This is suitable for the return value of the ->read_page() callback.
376 * Unfortunately, we can not know the total number of corrected bits in
377 * the page. Increase the stats by max_bitflips. (compromised solution)
379 mtd->ecc_stats.corrected += max_bitflips;
384 static int denali_sw_ecc_fixup(struct mtd_info *mtd,
385 struct denali_nand_info *denali,
386 unsigned long *uncor_ecc_flags, uint8_t *buf)
388 unsigned int ecc_size = denali->nand.ecc.size;
389 unsigned int bitflips = 0;
390 unsigned int max_bitflips = 0;
391 uint32_t err_addr, err_cor_info;
392 unsigned int err_byte, err_sector, err_device;
393 uint8_t err_cor_value;
394 unsigned int prev_sector = 0;
397 denali_reset_irq(denali);
400 err_addr = ioread32(denali->reg + ECC_ERROR_ADDRESS);
401 err_sector = FIELD_GET(ECC_ERROR_ADDRESS__SECTOR, err_addr);
402 err_byte = FIELD_GET(ECC_ERROR_ADDRESS__OFFSET, err_addr);
404 err_cor_info = ioread32(denali->reg + ERR_CORRECTION_INFO);
405 err_cor_value = FIELD_GET(ERR_CORRECTION_INFO__BYTE,
407 err_device = FIELD_GET(ERR_CORRECTION_INFO__DEVICE,
410 /* reset the bitflip counter when crossing ECC sector */
411 if (err_sector != prev_sector)
414 if (err_cor_info & ERR_CORRECTION_INFO__UNCOR) {
416 * Check later if this is a real ECC error, or
419 *uncor_ecc_flags |= BIT(err_sector);
420 } else if (err_byte < ecc_size) {
422 * If err_byte is larger than ecc_size, means error
423 * happened in OOB, so we ignore it. It's no need for
424 * us to correct it err_device is represented the NAND
425 * error bits are happened in if there are more than
426 * one NAND connected.
429 unsigned int flips_in_byte;
431 offset = (err_sector * ecc_size + err_byte) *
432 denali->devs_per_cs + err_device;
434 /* correct the ECC error */
435 flips_in_byte = hweight8(buf[offset] ^ err_cor_value);
436 buf[offset] ^= err_cor_value;
437 mtd->ecc_stats.corrected += flips_in_byte;
438 bitflips += flips_in_byte;
440 max_bitflips = max(max_bitflips, bitflips);
443 prev_sector = err_sector;
444 } while (!(err_cor_info & ERR_CORRECTION_INFO__LAST_ERR));
447 * Once handle all ECC errors, controller will trigger an
448 * ECC_TRANSACTION_DONE interrupt.
450 irq_status = denali_wait_for_irq(denali, INTR__ECC_TRANSACTION_DONE);
451 if (!(irq_status & INTR__ECC_TRANSACTION_DONE))
457 static void denali_setup_dma64(struct denali_nand_info *denali,
458 dma_addr_t dma_addr, int page, int write)
461 const int page_count = 1;
463 mode = DENALI_MAP10 | DENALI_BANK(denali) | page;
465 /* DMA is a three step process */
468 * 1. setup transfer type, interrupt when complete,
469 * burst len = 64 bytes, the number of pages
471 denali->host_write(denali, mode,
472 0x01002000 | (64 << 16) | (write << 8) | page_count);
474 /* 2. set memory low address */
475 denali->host_write(denali, mode, lower_32_bits(dma_addr));
477 /* 3. set memory high address */
478 denali->host_write(denali, mode, upper_32_bits(dma_addr));
481 static void denali_setup_dma32(struct denali_nand_info *denali,
482 dma_addr_t dma_addr, int page, int write)
485 const int page_count = 1;
487 mode = DENALI_MAP10 | DENALI_BANK(denali);
489 /* DMA is a four step process */
491 /* 1. setup transfer type and # of pages */
492 denali->host_write(denali, mode | page,
493 0x2000 | (write << 8) | page_count);
495 /* 2. set memory high address bits 23:8 */
496 denali->host_write(denali, mode | ((dma_addr >> 16) << 8), 0x2200);
498 /* 3. set memory low address bits 23:8 */
499 denali->host_write(denali, mode | ((dma_addr & 0xffff) << 8), 0x2300);
501 /* 4. interrupt when complete, burst len = 64 bytes */
502 denali->host_write(denali, mode | 0x14000, 0x2400);
505 static int denali_pio_read(struct denali_nand_info *denali, void *buf,
506 size_t size, int page, int raw)
508 u32 addr = DENALI_MAP01 | DENALI_BANK(denali) | page;
509 uint32_t *buf32 = (uint32_t *)buf;
510 uint32_t irq_status, ecc_err_mask;
513 if (denali->caps & DENALI_CAP_HW_ECC_FIXUP)
514 ecc_err_mask = INTR__ECC_UNCOR_ERR;
516 ecc_err_mask = INTR__ECC_ERR;
518 denali_reset_irq(denali);
520 for (i = 0; i < size / 4; i++)
521 *buf32++ = denali->host_read(denali, addr);
523 irq_status = denali_wait_for_irq(denali, INTR__PAGE_XFER_INC);
524 if (!(irq_status & INTR__PAGE_XFER_INC))
527 if (irq_status & INTR__ERASED_PAGE)
528 memset(buf, 0xff, size);
530 return irq_status & ecc_err_mask ? -EBADMSG : 0;
533 static int denali_pio_write(struct denali_nand_info *denali,
534 const void *buf, size_t size, int page, int raw)
536 u32 addr = DENALI_MAP01 | DENALI_BANK(denali) | page;
537 const uint32_t *buf32 = (uint32_t *)buf;
541 denali_reset_irq(denali);
543 for (i = 0; i < size / 4; i++)
544 denali->host_write(denali, addr, *buf32++);
546 irq_status = denali_wait_for_irq(denali,
547 INTR__PROGRAM_COMP | INTR__PROGRAM_FAIL);
548 if (!(irq_status & INTR__PROGRAM_COMP))
554 static int denali_pio_xfer(struct denali_nand_info *denali, void *buf,
555 size_t size, int page, int raw, int write)
558 return denali_pio_write(denali, buf, size, page, raw);
560 return denali_pio_read(denali, buf, size, page, raw);
563 static int denali_dma_xfer(struct denali_nand_info *denali, void *buf,
564 size_t size, int page, int raw, int write)
567 uint32_t irq_mask, irq_status, ecc_err_mask;
568 enum dma_data_direction dir = write ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
571 dma_addr = dma_map_single(denali->dev, buf, size, dir);
572 if (dma_mapping_error(denali->dev, dma_addr)) {
573 dev_dbg(denali->dev, "Failed to DMA-map buffer. Trying PIO.\n");
574 return denali_pio_xfer(denali, buf, size, page, raw, write);
579 * INTR__PROGRAM_COMP is never asserted for the DMA transfer.
580 * We can use INTR__DMA_CMD_COMP instead. This flag is asserted
581 * when the page program is completed.
583 irq_mask = INTR__DMA_CMD_COMP | INTR__PROGRAM_FAIL;
585 } else if (denali->caps & DENALI_CAP_HW_ECC_FIXUP) {
586 irq_mask = INTR__DMA_CMD_COMP;
587 ecc_err_mask = INTR__ECC_UNCOR_ERR;
589 irq_mask = INTR__DMA_CMD_COMP;
590 ecc_err_mask = INTR__ECC_ERR;
593 iowrite32(DMA_ENABLE__FLAG, denali->reg + DMA_ENABLE);
595 denali_reset_irq(denali);
596 denali->setup_dma(denali, dma_addr, page, write);
598 irq_status = denali_wait_for_irq(denali, irq_mask);
599 if (!(irq_status & INTR__DMA_CMD_COMP))
601 else if (irq_status & ecc_err_mask)
604 iowrite32(0, denali->reg + DMA_ENABLE);
606 dma_unmap_single(denali->dev, dma_addr, size, dir);
608 if (irq_status & INTR__ERASED_PAGE)
609 memset(buf, 0xff, size);
614 static int denali_data_xfer(struct denali_nand_info *denali, void *buf,
615 size_t size, int page, int raw, int write)
617 iowrite32(raw ? 0 : ECC_ENABLE__FLAG, denali->reg + ECC_ENABLE);
618 iowrite32(raw ? TRANSFER_SPARE_REG__FLAG : 0,
619 denali->reg + TRANSFER_SPARE_REG);
621 if (denali->dma_avail)
622 return denali_dma_xfer(denali, buf, size, page, raw, write);
624 return denali_pio_xfer(denali, buf, size, page, raw, write);
627 static void denali_oob_xfer(struct mtd_info *mtd, struct nand_chip *chip,
630 struct denali_nand_info *denali = mtd_to_denali(mtd);
631 unsigned int start_cmd = write ? NAND_CMD_SEQIN : NAND_CMD_READ0;
632 unsigned int rnd_cmd = write ? NAND_CMD_RNDIN : NAND_CMD_RNDOUT;
633 int writesize = mtd->writesize;
634 int oobsize = mtd->oobsize;
635 uint8_t *bufpoi = chip->oob_poi;
636 int ecc_steps = chip->ecc.steps;
637 int ecc_size = chip->ecc.size;
638 int ecc_bytes = chip->ecc.bytes;
639 int oob_skip = denali->oob_skip_bytes;
640 size_t size = writesize + oobsize;
643 /* BBM at the beginning of the OOB area */
644 chip->cmdfunc(mtd, start_cmd, writesize, page);
646 chip->write_buf(mtd, bufpoi, oob_skip);
648 chip->read_buf(mtd, bufpoi, oob_skip);
652 for (i = 0; i < ecc_steps; i++) {
653 pos = ecc_size + i * (ecc_size + ecc_bytes);
656 if (pos >= writesize)
658 else if (pos + len > writesize)
659 len = writesize - pos;
661 chip->cmdfunc(mtd, rnd_cmd, pos, -1);
663 chip->write_buf(mtd, bufpoi, len);
665 chip->read_buf(mtd, bufpoi, len);
667 if (len < ecc_bytes) {
668 len = ecc_bytes - len;
669 chip->cmdfunc(mtd, rnd_cmd, writesize + oob_skip, -1);
671 chip->write_buf(mtd, bufpoi, len);
673 chip->read_buf(mtd, bufpoi, len);
679 len = oobsize - (bufpoi - chip->oob_poi);
680 chip->cmdfunc(mtd, rnd_cmd, size - len, -1);
682 chip->write_buf(mtd, bufpoi, len);
684 chip->read_buf(mtd, bufpoi, len);
687 static int denali_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
688 uint8_t *buf, int oob_required, int page)
690 struct denali_nand_info *denali = mtd_to_denali(mtd);
691 int writesize = mtd->writesize;
692 int oobsize = mtd->oobsize;
693 int ecc_steps = chip->ecc.steps;
694 int ecc_size = chip->ecc.size;
695 int ecc_bytes = chip->ecc.bytes;
696 void *tmp_buf = denali->buf;
697 int oob_skip = denali->oob_skip_bytes;
698 size_t size = writesize + oobsize;
699 int ret, i, pos, len;
701 ret = denali_data_xfer(denali, tmp_buf, size, page, 1, 0);
705 /* Arrange the buffer for syndrome payload/ecc layout */
707 for (i = 0; i < ecc_steps; i++) {
708 pos = i * (ecc_size + ecc_bytes);
711 if (pos >= writesize)
713 else if (pos + len > writesize)
714 len = writesize - pos;
716 memcpy(buf, tmp_buf + pos, len);
718 if (len < ecc_size) {
719 len = ecc_size - len;
720 memcpy(buf, tmp_buf + writesize + oob_skip,
728 uint8_t *oob = chip->oob_poi;
730 /* BBM at the beginning of the OOB area */
731 memcpy(oob, tmp_buf + writesize, oob_skip);
735 for (i = 0; i < ecc_steps; i++) {
736 pos = ecc_size + i * (ecc_size + ecc_bytes);
739 if (pos >= writesize)
741 else if (pos + len > writesize)
742 len = writesize - pos;
744 memcpy(oob, tmp_buf + pos, len);
746 if (len < ecc_bytes) {
747 len = ecc_bytes - len;
748 memcpy(oob, tmp_buf + writesize + oob_skip,
755 len = oobsize - (oob - chip->oob_poi);
756 memcpy(oob, tmp_buf + size - len, len);
762 static int denali_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
765 denali_oob_xfer(mtd, chip, page, 0);
770 static int denali_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
773 struct denali_nand_info *denali = mtd_to_denali(mtd);
776 denali_reset_irq(denali);
778 denali_oob_xfer(mtd, chip, page, 1);
780 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
781 status = chip->waitfunc(mtd, chip);
783 return status & NAND_STATUS_FAIL ? -EIO : 0;
786 static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
787 uint8_t *buf, int oob_required, int page)
789 struct denali_nand_info *denali = mtd_to_denali(mtd);
790 unsigned long uncor_ecc_flags = 0;
794 ret = denali_data_xfer(denali, buf, mtd->writesize, page, 0, 0);
795 if (ret && ret != -EBADMSG)
798 if (denali->caps & DENALI_CAP_HW_ECC_FIXUP)
799 stat = denali_hw_ecc_fixup(mtd, denali, &uncor_ecc_flags);
800 else if (ret == -EBADMSG)
801 stat = denali_sw_ecc_fixup(mtd, denali, &uncor_ecc_flags, buf);
806 if (uncor_ecc_flags) {
807 ret = denali_read_oob(mtd, chip, page);
811 stat = denali_check_erased_page(mtd, chip, buf,
812 uncor_ecc_flags, stat);
818 static int denali_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
819 const uint8_t *buf, int oob_required, int page)
821 struct denali_nand_info *denali = mtd_to_denali(mtd);
822 int writesize = mtd->writesize;
823 int oobsize = mtd->oobsize;
824 int ecc_steps = chip->ecc.steps;
825 int ecc_size = chip->ecc.size;
826 int ecc_bytes = chip->ecc.bytes;
827 void *tmp_buf = denali->buf;
828 int oob_skip = denali->oob_skip_bytes;
829 size_t size = writesize + oobsize;
833 * Fill the buffer with 0xff first except the full page transfer.
834 * This simplifies the logic.
836 if (!buf || !oob_required)
837 memset(tmp_buf, 0xff, size);
839 /* Arrange the buffer for syndrome payload/ecc layout */
841 for (i = 0; i < ecc_steps; i++) {
842 pos = i * (ecc_size + ecc_bytes);
845 if (pos >= writesize)
847 else if (pos + len > writesize)
848 len = writesize - pos;
850 memcpy(tmp_buf + pos, buf, len);
852 if (len < ecc_size) {
853 len = ecc_size - len;
854 memcpy(tmp_buf + writesize + oob_skip, buf,
862 const uint8_t *oob = chip->oob_poi;
864 /* BBM at the beginning of the OOB area */
865 memcpy(tmp_buf + writesize, oob, oob_skip);
869 for (i = 0; i < ecc_steps; i++) {
870 pos = ecc_size + i * (ecc_size + ecc_bytes);
873 if (pos >= writesize)
875 else if (pos + len > writesize)
876 len = writesize - pos;
878 memcpy(tmp_buf + pos, oob, len);
880 if (len < ecc_bytes) {
881 len = ecc_bytes - len;
882 memcpy(tmp_buf + writesize + oob_skip, oob,
889 len = oobsize - (oob - chip->oob_poi);
890 memcpy(tmp_buf + size - len, oob, len);
893 return denali_data_xfer(denali, tmp_buf, size, page, 1, 1);
896 static int denali_write_page(struct mtd_info *mtd, struct nand_chip *chip,
897 const uint8_t *buf, int oob_required, int page)
899 struct denali_nand_info *denali = mtd_to_denali(mtd);
901 return denali_data_xfer(denali, (void *)buf, mtd->writesize,
905 static void denali_select_chip(struct mtd_info *mtd, int chip)
907 struct denali_nand_info *denali = mtd_to_denali(mtd);
909 denali->active_bank = chip;
912 static int denali_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
914 struct denali_nand_info *denali = mtd_to_denali(mtd);
917 /* R/B# pin transitioned from low to high? */
918 irq_status = denali_wait_for_irq(denali, INTR__INT_ACT);
920 return irq_status & INTR__INT_ACT ? 0 : NAND_STATUS_FAIL;
923 static int denali_erase(struct mtd_info *mtd, int page)
925 struct denali_nand_info *denali = mtd_to_denali(mtd);
928 denali_reset_irq(denali);
930 denali->host_write(denali, DENALI_MAP10 | DENALI_BANK(denali) | page,
933 /* wait for erase to complete or failure to occur */
934 irq_status = denali_wait_for_irq(denali,
935 INTR__ERASE_COMP | INTR__ERASE_FAIL);
937 return irq_status & INTR__ERASE_COMP ? 0 : NAND_STATUS_FAIL;
940 static int denali_setup_data_interface(struct mtd_info *mtd, int chipnr,
941 const struct nand_data_interface *conf)
943 struct denali_nand_info *denali = mtd_to_denali(mtd);
944 const struct nand_sdr_timings *timings;
946 int acc_clks, re_2_we, re_2_re, we_2_re, addr_2_data;
947 int rdwr_en_lo, rdwr_en_hi, rdwr_en_lo_hi, cs_setup;
948 int addr_2_data_mask;
951 timings = nand_get_sdr_timings(conf);
953 return PTR_ERR(timings);
955 /* clk_x period in picoseconds */
956 t_clk = DIV_ROUND_DOWN_ULL(1000000000000ULL, denali->clk_x_rate);
960 if (chipnr == NAND_DATA_IFACE_CHECK_ONLY)
963 /* tREA -> ACC_CLKS */
964 acc_clks = DIV_ROUND_UP(timings->tREA_max, t_clk);
965 acc_clks = min_t(int, acc_clks, ACC_CLKS__VALUE);
967 tmp = ioread32(denali->reg + ACC_CLKS);
968 tmp &= ~ACC_CLKS__VALUE;
969 tmp |= FIELD_PREP(ACC_CLKS__VALUE, acc_clks);
970 iowrite32(tmp, denali->reg + ACC_CLKS);
972 /* tRWH -> RE_2_WE */
973 re_2_we = DIV_ROUND_UP(timings->tRHW_min, t_clk);
974 re_2_we = min_t(int, re_2_we, RE_2_WE__VALUE);
976 tmp = ioread32(denali->reg + RE_2_WE);
977 tmp &= ~RE_2_WE__VALUE;
978 tmp |= FIELD_PREP(RE_2_WE__VALUE, re_2_we);
979 iowrite32(tmp, denali->reg + RE_2_WE);
981 /* tRHZ -> RE_2_RE */
982 re_2_re = DIV_ROUND_UP(timings->tRHZ_max, t_clk);
983 re_2_re = min_t(int, re_2_re, RE_2_RE__VALUE);
985 tmp = ioread32(denali->reg + RE_2_RE);
986 tmp &= ~RE_2_RE__VALUE;
987 tmp |= FIELD_PREP(RE_2_RE__VALUE, re_2_re);
988 iowrite32(tmp, denali->reg + RE_2_RE);
991 * tCCS, tWHR -> WE_2_RE
993 * With WE_2_RE properly set, the Denali controller automatically takes
994 * care of the delay; the driver need not set NAND_WAIT_TCCS.
996 we_2_re = DIV_ROUND_UP(max(timings->tCCS_min, timings->tWHR_min),
998 we_2_re = min_t(int, we_2_re, TWHR2_AND_WE_2_RE__WE_2_RE);
1000 tmp = ioread32(denali->reg + TWHR2_AND_WE_2_RE);
1001 tmp &= ~TWHR2_AND_WE_2_RE__WE_2_RE;
1002 tmp |= FIELD_PREP(TWHR2_AND_WE_2_RE__WE_2_RE, we_2_re);
1003 iowrite32(tmp, denali->reg + TWHR2_AND_WE_2_RE);
1005 /* tADL -> ADDR_2_DATA */
1007 /* for older versions, ADDR_2_DATA is only 6 bit wide */
1008 addr_2_data_mask = TCWAW_AND_ADDR_2_DATA__ADDR_2_DATA;
1009 if (denali->revision < 0x0501)
1010 addr_2_data_mask >>= 1;
1012 addr_2_data = DIV_ROUND_UP(timings->tADL_min, t_clk);
1013 addr_2_data = min_t(int, addr_2_data, addr_2_data_mask);
1015 tmp = ioread32(denali->reg + TCWAW_AND_ADDR_2_DATA);
1016 tmp &= ~TCWAW_AND_ADDR_2_DATA__ADDR_2_DATA;
1017 tmp |= FIELD_PREP(TCWAW_AND_ADDR_2_DATA__ADDR_2_DATA, addr_2_data);
1018 iowrite32(tmp, denali->reg + TCWAW_AND_ADDR_2_DATA);
1020 /* tREH, tWH -> RDWR_EN_HI_CNT */
1021 rdwr_en_hi = DIV_ROUND_UP(max(timings->tREH_min, timings->tWH_min),
1023 rdwr_en_hi = min_t(int, rdwr_en_hi, RDWR_EN_HI_CNT__VALUE);
1025 tmp = ioread32(denali->reg + RDWR_EN_HI_CNT);
1026 tmp &= ~RDWR_EN_HI_CNT__VALUE;
1027 tmp |= FIELD_PREP(RDWR_EN_HI_CNT__VALUE, rdwr_en_hi);
1028 iowrite32(tmp, denali->reg + RDWR_EN_HI_CNT);
1030 /* tRP, tWP -> RDWR_EN_LO_CNT */
1031 rdwr_en_lo = DIV_ROUND_UP(max(timings->tRP_min, timings->tWP_min),
1033 rdwr_en_lo_hi = DIV_ROUND_UP(max(timings->tRC_min, timings->tWC_min),
1035 rdwr_en_lo_hi = max(rdwr_en_lo_hi, DENALI_CLK_X_MULT);
1036 rdwr_en_lo = max(rdwr_en_lo, rdwr_en_lo_hi - rdwr_en_hi);
1037 rdwr_en_lo = min_t(int, rdwr_en_lo, RDWR_EN_LO_CNT__VALUE);
1039 tmp = ioread32(denali->reg + RDWR_EN_LO_CNT);
1040 tmp &= ~RDWR_EN_LO_CNT__VALUE;
1041 tmp |= FIELD_PREP(RDWR_EN_LO_CNT__VALUE, rdwr_en_lo);
1042 iowrite32(tmp, denali->reg + RDWR_EN_LO_CNT);
1044 /* tCS, tCEA -> CS_SETUP_CNT */
1045 cs_setup = max3((int)DIV_ROUND_UP(timings->tCS_min, t_clk) - rdwr_en_lo,
1046 (int)DIV_ROUND_UP(timings->tCEA_max, t_clk) - acc_clks,
1048 cs_setup = min_t(int, cs_setup, CS_SETUP_CNT__VALUE);
1050 tmp = ioread32(denali->reg + CS_SETUP_CNT);
1051 tmp &= ~CS_SETUP_CNT__VALUE;
1052 tmp |= FIELD_PREP(CS_SETUP_CNT__VALUE, cs_setup);
1053 iowrite32(tmp, denali->reg + CS_SETUP_CNT);
1058 static void denali_reset_banks(struct denali_nand_info *denali)
1063 for (i = 0; i < denali->max_banks; i++) {
1064 denali->active_bank = i;
1066 denali_reset_irq(denali);
1068 iowrite32(DEVICE_RESET__BANK(i),
1069 denali->reg + DEVICE_RESET);
1071 irq_status = denali_wait_for_irq(denali,
1072 INTR__RST_COMP | INTR__INT_ACT | INTR__TIME_OUT);
1073 if (!(irq_status & INTR__INT_ACT))
1077 dev_dbg(denali->dev, "%d chips connected\n", i);
1078 denali->max_banks = i;
1081 static void denali_hw_init(struct denali_nand_info *denali)
1084 * The REVISION register may not be reliable. Platforms are allowed to
1087 if (!denali->revision)
1088 denali->revision = swab16(ioread32(denali->reg + REVISION));
1091 * tell driver how many bit controller will skip before writing
1092 * ECC code in OOB. This is normally used for bad block marker
1094 denali->oob_skip_bytes = CONFIG_NAND_DENALI_SPARE_AREA_SKIP_BYTES;
1095 iowrite32(denali->oob_skip_bytes, denali->reg + SPARE_AREA_SKIP_BYTES);
1096 denali_detect_max_banks(denali);
1097 iowrite32(0x0F, denali->reg + RB_PIN_ENABLED);
1098 iowrite32(CHIP_EN_DONT_CARE__FLAG, denali->reg + CHIP_ENABLE_DONT_CARE);
1100 iowrite32(0xffff, denali->reg + SPARE_AREA_MARKER);
1103 int denali_calc_ecc_bytes(int step_size, int strength)
1105 /* BCH code. Denali requires ecc.bytes to be multiple of 2 */
1106 return DIV_ROUND_UP(strength * fls(step_size * 8), 16) * 2;
1108 EXPORT_SYMBOL(denali_calc_ecc_bytes);
1110 static int denali_ecc_setup(struct mtd_info *mtd, struct nand_chip *chip,
1111 struct denali_nand_info *denali)
1113 int oobavail = mtd->oobsize - denali->oob_skip_bytes;
1117 * If .size and .strength are already set (usually by DT),
1118 * check if they are supported by this controller.
1120 if (chip->ecc.size && chip->ecc.strength)
1121 return nand_check_ecc_caps(chip, denali->ecc_caps, oobavail);
1124 * We want .size and .strength closest to the chip's requirement
1125 * unless NAND_ECC_MAXIMIZE is requested.
1127 if (!(chip->ecc.options & NAND_ECC_MAXIMIZE)) {
1128 ret = nand_match_ecc_req(chip, denali->ecc_caps, oobavail);
1133 /* Max ECC strength is the last thing we can do */
1134 return nand_maximize_ecc(chip, denali->ecc_caps, oobavail);
1137 static struct nand_ecclayout nand_oob;
1139 static int denali_ooblayout_ecc(struct mtd_info *mtd, int section,
1140 struct mtd_oob_region *oobregion)
1142 struct denali_nand_info *denali = mtd_to_denali(mtd);
1143 struct nand_chip *chip = mtd_to_nand(mtd);
1148 oobregion->offset = denali->oob_skip_bytes;
1149 oobregion->length = chip->ecc.total;
1154 static int denali_ooblayout_free(struct mtd_info *mtd, int section,
1155 struct mtd_oob_region *oobregion)
1157 struct denali_nand_info *denali = mtd_to_denali(mtd);
1158 struct nand_chip *chip = mtd_to_nand(mtd);
1163 oobregion->offset = chip->ecc.total + denali->oob_skip_bytes;
1164 oobregion->length = mtd->oobsize - oobregion->offset;
1169 static const struct mtd_ooblayout_ops denali_ooblayout_ops = {
1170 .ecc = denali_ooblayout_ecc,
1171 .free = denali_ooblayout_free,
1174 static int denali_multidev_fixup(struct denali_nand_info *denali)
1176 struct nand_chip *chip = &denali->nand;
1177 struct mtd_info *mtd = nand_to_mtd(chip);
1180 * Support for multi device:
1181 * When the IP configuration is x16 capable and two x8 chips are
1182 * connected in parallel, DEVICES_CONNECTED should be set to 2.
1183 * In this case, the core framework knows nothing about this fact,
1184 * so we should tell it the _logical_ pagesize and anything necessary.
1186 denali->devs_per_cs = ioread32(denali->reg + DEVICES_CONNECTED);
1189 * On some SoCs, DEVICES_CONNECTED is not auto-detected.
1190 * For those, DEVICES_CONNECTED is left to 0. Set 1 if it is the case.
1192 if (denali->devs_per_cs == 0) {
1193 denali->devs_per_cs = 1;
1194 iowrite32(1, denali->reg + DEVICES_CONNECTED);
1197 if (denali->devs_per_cs == 1)
1200 if (denali->devs_per_cs != 2) {
1201 dev_err(denali->dev, "unsupported number of devices %d\n",
1202 denali->devs_per_cs);
1206 /* 2 chips in parallel */
1208 mtd->erasesize <<= 1;
1209 mtd->writesize <<= 1;
1211 chip->chipsize <<= 1;
1212 chip->page_shift += 1;
1213 chip->phys_erase_shift += 1;
1214 chip->bbt_erase_shift += 1;
1215 chip->chip_shift += 1;
1216 chip->pagemask <<= 1;
1217 chip->ecc.size <<= 1;
1218 chip->ecc.bytes <<= 1;
1219 chip->ecc.strength <<= 1;
1220 denali->oob_skip_bytes <<= 1;
1225 int denali_init(struct denali_nand_info *denali)
1227 struct nand_chip *chip = &denali->nand;
1228 struct mtd_info *mtd = nand_to_mtd(chip);
1229 u32 features = ioread32(denali->reg + FEATURES);
1232 denali_hw_init(denali);
1234 denali_clear_irq_all(denali);
1236 denali_reset_banks(denali);
1238 denali->active_bank = DENALI_INVALID_BANK;
1240 chip->flash_node = dev_of_offset(denali->dev);
1241 /* Fallback to the default name if DT did not give "label" property */
1243 mtd->name = "denali-nand";
1245 chip->select_chip = denali_select_chip;
1246 chip->read_byte = denali_read_byte;
1247 chip->write_byte = denali_write_byte;
1248 chip->read_word = denali_read_word;
1249 chip->cmd_ctrl = denali_cmd_ctrl;
1250 chip->dev_ready = denali_dev_ready;
1251 chip->waitfunc = denali_waitfunc;
1253 if (features & FEATURES__INDEX_ADDR) {
1254 denali->host_read = denali_indexed_read;
1255 denali->host_write = denali_indexed_write;
1257 denali->host_read = denali_direct_read;
1258 denali->host_write = denali_direct_write;
1261 /* clk rate info is needed for setup_data_interface */
1262 if (denali->clk_x_rate)
1263 chip->setup_data_interface = denali_setup_data_interface;
1265 ret = nand_scan_ident(mtd, denali->max_banks, NULL);
1269 if (ioread32(denali->reg + FEATURES) & FEATURES__DMA)
1270 denali->dma_avail = 1;
1272 if (denali->dma_avail) {
1273 chip->buf_align = 16;
1274 if (denali->caps & DENALI_CAP_DMA_64BIT)
1275 denali->setup_dma = denali_setup_dma64;
1277 denali->setup_dma = denali_setup_dma32;
1279 chip->buf_align = 4;
1282 chip->options |= NAND_USE_BOUNCE_BUFFER;
1283 chip->bbt_options |= NAND_BBT_USE_FLASH;
1284 chip->bbt_options |= NAND_BBT_NO_OOB;
1285 denali->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
1287 /* no subpage writes on denali */
1288 chip->options |= NAND_NO_SUBPAGE_WRITE;
1290 ret = denali_ecc_setup(mtd, chip, denali);
1292 dev_err(denali->dev, "Failed to setup ECC settings.\n");
1296 dev_dbg(denali->dev,
1297 "chosen ECC settings: step=%d, strength=%d, bytes=%d\n",
1298 chip->ecc.size, chip->ecc.strength, chip->ecc.bytes);
1300 iowrite32(FIELD_PREP(ECC_CORRECTION__ERASE_THRESHOLD, 1) |
1301 FIELD_PREP(ECC_CORRECTION__VALUE, chip->ecc.strength),
1302 denali->reg + ECC_CORRECTION);
1303 iowrite32(mtd->erasesize / mtd->writesize,
1304 denali->reg + PAGES_PER_BLOCK);
1305 iowrite32(chip->options & NAND_BUSWIDTH_16 ? 1 : 0,
1306 denali->reg + DEVICE_WIDTH);
1307 iowrite32(chip->options & NAND_ROW_ADDR_3 ? 0 : TWO_ROW_ADDR_CYCLES__FLAG,
1308 denali->reg + TWO_ROW_ADDR_CYCLES);
1309 iowrite32(mtd->writesize, denali->reg + DEVICE_MAIN_AREA_SIZE);
1310 iowrite32(mtd->oobsize, denali->reg + DEVICE_SPARE_AREA_SIZE);
1312 iowrite32(chip->ecc.size, denali->reg + CFG_DATA_BLOCK_SIZE);
1313 iowrite32(chip->ecc.size, denali->reg + CFG_LAST_DATA_BLOCK_SIZE);
1314 /* chip->ecc.steps is set by nand_scan_tail(); not available here */
1315 iowrite32(mtd->writesize / chip->ecc.size,
1316 denali->reg + CFG_NUM_DATA_BLOCKS);
1318 mtd_set_ooblayout(mtd, &denali_ooblayout_ops);
1320 nand_oob.eccbytes = denali->nand.ecc.bytes;
1321 denali->nand.ecc.layout = &nand_oob;
1323 if (chip->options & NAND_BUSWIDTH_16) {
1324 chip->read_buf = denali_read_buf16;
1325 chip->write_buf = denali_write_buf16;
1327 chip->read_buf = denali_read_buf;
1328 chip->write_buf = denali_write_buf;
1330 chip->ecc.options |= NAND_ECC_CUSTOM_PAGE_ACCESS;
1331 chip->ecc.read_page = denali_read_page;
1332 chip->ecc.read_page_raw = denali_read_page_raw;
1333 chip->ecc.write_page = denali_write_page;
1334 chip->ecc.write_page_raw = denali_write_page_raw;
1335 chip->ecc.read_oob = denali_read_oob;
1336 chip->ecc.write_oob = denali_write_oob;
1337 chip->erase = denali_erase;
1339 ret = denali_multidev_fixup(denali);
1344 * This buffer is DMA-mapped by denali_{read,write}_page_raw. Do not
1345 * use devm_kmalloc() because the memory allocated by devm_ does not
1346 * guarantee DMA-safe alignment.
1348 denali->buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
1352 ret = nand_scan_tail(mtd);
1356 ret = nand_register(0, mtd);
1358 dev_err(denali->dev, "Failed to register MTD: %d\n", ret);