1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) STMicroelectronics 2019
4 * Author: Christophe Kerello <christophe.kerello@st.com>
13 #include <linux/bitfield.h>
14 #include <linux/bitops.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/iopoll.h>
18 #include <linux/ioport.h>
20 /* Bad block marker length */
21 #define FMC2_BBM_LEN 2
24 #define FMC2_ECC_STEP_SIZE 512
27 #define FMC2_RB_DELAY_US 30
35 #define FMC2_TSYNC 3000
36 #define FMC2_PCR_TIMING_MASK 0xf
37 #define FMC2_PMEM_PATT_TIMING_MASK 0xff
39 /* FMC2 Controller Registers */
43 #define FMC2_PMEM 0x88
44 #define FMC2_PATT 0x8c
45 #define FMC2_HECCR 0x94
46 #define FMC2_BCHISR 0x254
47 #define FMC2_BCHICR 0x258
48 #define FMC2_BCHPBR1 0x260
49 #define FMC2_BCHPBR2 0x264
50 #define FMC2_BCHPBR3 0x268
51 #define FMC2_BCHPBR4 0x26c
52 #define FMC2_BCHDSR0 0x27c
53 #define FMC2_BCHDSR1 0x280
54 #define FMC2_BCHDSR2 0x284
55 #define FMC2_BCHDSR3 0x288
56 #define FMC2_BCHDSR4 0x28c
58 /* Register: FMC2_BCR1 */
59 #define FMC2_BCR1_FMC2EN BIT(31)
61 /* Register: FMC2_PCR */
62 #define FMC2_PCR_PWAITEN BIT(1)
63 #define FMC2_PCR_PBKEN BIT(2)
64 #define FMC2_PCR_PWID GENMASK(5, 4)
65 #define FMC2_PCR_PWID_BUSWIDTH_8 0
66 #define FMC2_PCR_PWID_BUSWIDTH_16 1
67 #define FMC2_PCR_ECCEN BIT(6)
68 #define FMC2_PCR_ECCALG BIT(8)
69 #define FMC2_PCR_TCLR GENMASK(12, 9)
70 #define FMC2_PCR_TCLR_DEFAULT 0xf
71 #define FMC2_PCR_TAR GENMASK(16, 13)
72 #define FMC2_PCR_TAR_DEFAULT 0xf
73 #define FMC2_PCR_ECCSS GENMASK(19, 17)
74 #define FMC2_PCR_ECCSS_512 1
75 #define FMC2_PCR_ECCSS_2048 3
76 #define FMC2_PCR_BCHECC BIT(24)
77 #define FMC2_PCR_WEN BIT(25)
79 /* Register: FMC2_SR */
80 #define FMC2_SR_NWRF BIT(6)
82 /* Register: FMC2_PMEM */
83 #define FMC2_PMEM_MEMSET GENMASK(7, 0)
84 #define FMC2_PMEM_MEMWAIT GENMASK(15, 8)
85 #define FMC2_PMEM_MEMHOLD GENMASK(23, 16)
86 #define FMC2_PMEM_MEMHIZ GENMASK(31, 24)
87 #define FMC2_PMEM_DEFAULT 0x0a0a0a0a
89 /* Register: FMC2_PATT */
90 #define FMC2_PATT_ATTSET GENMASK(7, 0)
91 #define FMC2_PATT_ATTWAIT GENMASK(15, 8)
92 #define FMC2_PATT_ATTHOLD GENMASK(23, 16)
93 #define FMC2_PATT_ATTHIZ GENMASK(31, 24)
94 #define FMC2_PATT_DEFAULT 0x0a0a0a0a
96 /* Register: FMC2_BCHISR */
97 #define FMC2_BCHISR_DERF BIT(1)
98 #define FMC2_BCHISR_EPBRF BIT(4)
100 /* Register: FMC2_BCHICR */
101 #define FMC2_BCHICR_CLEAR_IRQ GENMASK(4, 0)
103 /* Register: FMC2_BCHDSR0 */
104 #define FMC2_BCHDSR0_DUE BIT(0)
105 #define FMC2_BCHDSR0_DEF BIT(1)
106 #define FMC2_BCHDSR0_DEN GENMASK(7, 4)
108 /* Register: FMC2_BCHDSR1 */
109 #define FMC2_BCHDSR1_EBP1 GENMASK(12, 0)
110 #define FMC2_BCHDSR1_EBP2 GENMASK(28, 16)
112 /* Register: FMC2_BCHDSR2 */
113 #define FMC2_BCHDSR2_EBP3 GENMASK(12, 0)
114 #define FMC2_BCHDSR2_EBP4 GENMASK(28, 16)
116 /* Register: FMC2_BCHDSR3 */
117 #define FMC2_BCHDSR3_EBP5 GENMASK(12, 0)
118 #define FMC2_BCHDSR3_EBP6 GENMASK(28, 16)
120 /* Register: FMC2_BCHDSR4 */
121 #define FMC2_BCHDSR4_EBP7 GENMASK(12, 0)
122 #define FMC2_BCHDSR4_EBP8 GENMASK(28, 16)
124 #define FMC2_NSEC_PER_SEC 1000000000L
126 #define FMC2_TIMEOUT_5S 5000000
128 enum stm32_fmc2_ecc {
134 struct stm32_fmc2_timings {
145 struct stm32_fmc2_nand {
146 struct nand_chip chip;
147 struct stm32_fmc2_timings timings;
149 int cs_used[FMC2_MAX_CE];
152 static inline struct stm32_fmc2_nand *to_fmc2_nand(struct nand_chip *chip)
154 return container_of(chip, struct stm32_fmc2_nand, chip);
157 struct stm32_fmc2_nfc {
158 struct nand_hw_control base;
159 struct stm32_fmc2_nand nand;
160 struct nand_ecclayout ecclayout;
161 void __iomem *io_base;
162 void __iomem *data_base[FMC2_MAX_CE];
163 void __iomem *cmd_base[FMC2_MAX_CE];
164 void __iomem *addr_base[FMC2_MAX_CE];
171 static inline struct stm32_fmc2_nfc *to_stm32_nfc(struct nand_hw_control *base)
173 return container_of(base, struct stm32_fmc2_nfc, base);
176 static void stm32_fmc2_nfc_timings_init(struct nand_chip *chip)
178 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
179 struct stm32_fmc2_nand *nand = to_fmc2_nand(chip);
180 struct stm32_fmc2_timings *timings = &nand->timings;
181 u32 pcr = readl(nfc->io_base + FMC2_PCR);
184 /* Set tclr/tar timings */
185 pcr &= ~FMC2_PCR_TCLR;
186 pcr |= FIELD_PREP(FMC2_PCR_TCLR, timings->tclr);
187 pcr &= ~FMC2_PCR_TAR;
188 pcr |= FIELD_PREP(FMC2_PCR_TAR, timings->tar);
190 /* Set tset/twait/thold/thiz timings in common bank */
191 pmem = FIELD_PREP(FMC2_PMEM_MEMSET, timings->tset_mem);
192 pmem |= FIELD_PREP(FMC2_PMEM_MEMWAIT, timings->twait);
193 pmem |= FIELD_PREP(FMC2_PMEM_MEMHOLD, timings->thold_mem);
194 pmem |= FIELD_PREP(FMC2_PMEM_MEMHIZ, timings->thiz);
196 /* Set tset/twait/thold/thiz timings in attribut bank */
197 patt = FIELD_PREP(FMC2_PATT_ATTSET, timings->tset_att);
198 patt |= FIELD_PREP(FMC2_PATT_ATTWAIT, timings->twait);
199 patt |= FIELD_PREP(FMC2_PATT_ATTHOLD, timings->thold_att);
200 patt |= FIELD_PREP(FMC2_PATT_ATTHIZ, timings->thiz);
202 writel(pcr, nfc->io_base + FMC2_PCR);
203 writel(pmem, nfc->io_base + FMC2_PMEM);
204 writel(patt, nfc->io_base + FMC2_PATT);
207 static void stm32_fmc2_nfc_setup(struct nand_chip *chip)
209 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
210 u32 pcr = readl(nfc->io_base + FMC2_PCR);
212 /* Configure ECC algorithm (default configuration is Hamming) */
213 pcr &= ~FMC2_PCR_ECCALG;
214 pcr &= ~FMC2_PCR_BCHECC;
215 if (chip->ecc.strength == FMC2_ECC_BCH8) {
216 pcr |= FMC2_PCR_ECCALG;
217 pcr |= FMC2_PCR_BCHECC;
218 } else if (chip->ecc.strength == FMC2_ECC_BCH4) {
219 pcr |= FMC2_PCR_ECCALG;
223 pcr &= ~FMC2_PCR_PWID;
224 if (chip->options & NAND_BUSWIDTH_16)
225 pcr |= FIELD_PREP(FMC2_PCR_PWID, FMC2_PCR_PWID_BUSWIDTH_16);
227 /* Set ECC sector size */
228 pcr &= ~FMC2_PCR_ECCSS;
229 pcr |= FIELD_PREP(FMC2_PCR_ECCSS, FMC2_PCR_ECCSS_512);
231 writel(pcr, nfc->io_base + FMC2_PCR);
234 static void stm32_fmc2_nfc_select_chip(struct mtd_info *mtd, int chipnr)
236 struct nand_chip *chip = mtd_to_nand(mtd);
237 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
238 struct stm32_fmc2_nand *nand = to_fmc2_nand(chip);
240 if (chipnr < 0 || chipnr >= nand->ncs)
243 if (nand->cs_used[chipnr] == nfc->cs_sel)
246 nfc->cs_sel = nand->cs_used[chipnr];
247 chip->IO_ADDR_R = nfc->data_base[nfc->cs_sel];
248 chip->IO_ADDR_W = nfc->data_base[nfc->cs_sel];
250 stm32_fmc2_nfc_setup(chip);
251 stm32_fmc2_nfc_timings_init(chip);
254 static void stm32_fmc2_nfc_set_buswidth_16(struct stm32_fmc2_nfc *nfc,
257 u32 pcr = readl(nfc->io_base + FMC2_PCR);
259 pcr &= ~FMC2_PCR_PWID;
261 pcr |= FIELD_PREP(FMC2_PCR_PWID, FMC2_PCR_PWID_BUSWIDTH_16);
262 writel(pcr, nfc->io_base + FMC2_PCR);
265 static void stm32_fmc2_nfc_set_ecc(struct stm32_fmc2_nfc *nfc, bool enable)
267 u32 pcr = readl(nfc->io_base + FMC2_PCR);
269 pcr &= ~FMC2_PCR_ECCEN;
271 pcr |= FMC2_PCR_ECCEN;
272 writel(pcr, nfc->io_base + FMC2_PCR);
275 static void stm32_fmc2_nfc_clear_bch_irq(struct stm32_fmc2_nfc *nfc)
277 writel(FMC2_BCHICR_CLEAR_IRQ, nfc->io_base + FMC2_BCHICR);
280 static void stm32_fmc2_nfc_cmd_ctrl(struct mtd_info *mtd, int cmd,
283 struct nand_chip *chip = mtd_to_nand(mtd);
284 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
286 if (cmd == NAND_CMD_NONE)
289 if (ctrl & NAND_CLE) {
290 writeb(cmd, nfc->cmd_base[nfc->cs_sel]);
294 writeb(cmd, nfc->addr_base[nfc->cs_sel]);
298 * Enable ECC logic and reset syndrome/parity bits previously calculated
299 * Syndrome/parity bits is cleared by setting the ECCEN bit to 0
301 static void stm32_fmc2_nfc_hwctl(struct mtd_info *mtd, int mode)
303 struct nand_chip *chip = mtd_to_nand(mtd);
304 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
306 stm32_fmc2_nfc_set_ecc(nfc, false);
308 if (chip->ecc.strength != FMC2_ECC_HAM) {
309 u32 pcr = readl(nfc->io_base + FMC2_PCR);
311 if (mode == NAND_ECC_WRITE)
314 pcr &= ~FMC2_PCR_WEN;
315 writel(pcr, nfc->io_base + FMC2_PCR);
317 stm32_fmc2_nfc_clear_bch_irq(nfc);
320 stm32_fmc2_nfc_set_ecc(nfc, true);
324 * ECC Hamming calculation
325 * ECC is 3 bytes for 512 bytes of data (supports error correction up to
328 static int stm32_fmc2_nfc_ham_calculate(struct mtd_info *mtd, const u8 *data,
331 struct nand_chip *chip = mtd_to_nand(mtd);
332 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
336 ret = readl_poll_timeout(nfc->io_base + FMC2_SR, sr,
337 sr & FMC2_SR_NWRF, FMC2_TIMEOUT_5S);
339 pr_err("Ham timeout\n");
343 heccr = readl(nfc->io_base + FMC2_HECCR);
347 ecc[2] = heccr >> 16;
349 stm32_fmc2_nfc_set_ecc(nfc, false);
354 static int stm32_fmc2_nfc_ham_correct(struct mtd_info *mtd, u8 *dat,
355 u8 *read_ecc, u8 *calc_ecc)
357 u8 bit_position = 0, b0, b1, b2;
358 u32 byte_addr = 0, b;
361 /* Indicate which bit and byte is faulty (if any) */
362 b0 = read_ecc[0] ^ calc_ecc[0];
363 b1 = read_ecc[1] ^ calc_ecc[1];
364 b2 = read_ecc[2] ^ calc_ecc[2];
365 b = b0 | (b1 << 8) | (b2 << 16);
371 /* Calculate bit position */
372 for (i = 0; i < 3; i++) {
375 bit_position += shifting;
385 /* Calculate byte position */
387 for (i = 0; i < 9; i++) {
390 byte_addr += shifting;
401 dat[byte_addr] ^= (1 << bit_position);
407 * ECC BCH calculation and correction
408 * ECC is 7/13 bytes for 512 bytes of data (supports error correction up to
409 * max of 4-bit/8-bit)
412 static int stm32_fmc2_nfc_bch_calculate(struct mtd_info *mtd, const u8 *data,
415 struct nand_chip *chip = mtd_to_nand(mtd);
416 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
420 /* Wait until the BCH code is ready */
421 ret = readl_poll_timeout(nfc->io_base + FMC2_BCHISR, bchisr,
422 bchisr & FMC2_BCHISR_EPBRF, FMC2_TIMEOUT_5S);
424 pr_err("Bch timeout\n");
428 /* Read parity bits */
429 bchpbr = readl(nfc->io_base + FMC2_BCHPBR1);
431 ecc[1] = bchpbr >> 8;
432 ecc[2] = bchpbr >> 16;
433 ecc[3] = bchpbr >> 24;
435 bchpbr = readl(nfc->io_base + FMC2_BCHPBR2);
437 ecc[5] = bchpbr >> 8;
438 ecc[6] = bchpbr >> 16;
440 if (chip->ecc.strength == FMC2_ECC_BCH8) {
441 ecc[7] = bchpbr >> 24;
443 bchpbr = readl(nfc->io_base + FMC2_BCHPBR3);
445 ecc[9] = bchpbr >> 8;
446 ecc[10] = bchpbr >> 16;
447 ecc[11] = bchpbr >> 24;
449 bchpbr = readl(nfc->io_base + FMC2_BCHPBR4);
453 stm32_fmc2_nfc_set_ecc(nfc, false);
458 static int stm32_fmc2_nfc_bch_correct(struct mtd_info *mtd, u8 *dat,
459 u8 *read_ecc, u8 *calc_ecc)
461 struct nand_chip *chip = mtd_to_nand(mtd);
462 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
463 u32 bchdsr0, bchdsr1, bchdsr2, bchdsr3, bchdsr4, bchisr;
465 int i, ret, den, eccsize = chip->ecc.size;
466 unsigned int nb_errs = 0;
468 /* Wait until the decoding error is ready */
469 ret = readl_poll_timeout(nfc->io_base + FMC2_BCHISR, bchisr,
470 bchisr & FMC2_BCHISR_DERF, FMC2_TIMEOUT_5S);
472 pr_err("Bch timeout\n");
476 bchdsr0 = readl(nfc->io_base + FMC2_BCHDSR0);
477 bchdsr1 = readl(nfc->io_base + FMC2_BCHDSR1);
478 bchdsr2 = readl(nfc->io_base + FMC2_BCHDSR2);
479 bchdsr3 = readl(nfc->io_base + FMC2_BCHDSR3);
480 bchdsr4 = readl(nfc->io_base + FMC2_BCHDSR4);
482 stm32_fmc2_nfc_set_ecc(nfc, false);
484 /* No errors found */
485 if (likely(!(bchdsr0 & FMC2_BCHDSR0_DEF)))
488 /* Too many errors detected */
489 if (unlikely(bchdsr0 & FMC2_BCHDSR0_DUE))
492 pos[0] = FIELD_GET(FMC2_BCHDSR1_EBP1, bchdsr1);
493 pos[1] = FIELD_GET(FMC2_BCHDSR1_EBP2, bchdsr1);
494 pos[2] = FIELD_GET(FMC2_BCHDSR2_EBP3, bchdsr2);
495 pos[3] = FIELD_GET(FMC2_BCHDSR2_EBP4, bchdsr2);
496 pos[4] = FIELD_GET(FMC2_BCHDSR3_EBP5, bchdsr3);
497 pos[5] = FIELD_GET(FMC2_BCHDSR3_EBP6, bchdsr3);
498 pos[6] = FIELD_GET(FMC2_BCHDSR4_EBP7, bchdsr4);
499 pos[7] = FIELD_GET(FMC2_BCHDSR4_EBP8, bchdsr4);
501 den = FIELD_GET(FMC2_BCHDSR0_DEN, bchdsr0);
502 for (i = 0; i < den; i++) {
503 if (pos[i] < eccsize * 8) {
504 __change_bit(pos[i], (unsigned long *)dat);
512 static int stm32_fmc2_nfc_read_page(struct mtd_info *mtd,
513 struct nand_chip *chip, u8 *buf,
514 int oob_required, int page)
516 int i, s, stat, eccsize = chip->ecc.size;
517 int eccbytes = chip->ecc.bytes;
518 int eccsteps = chip->ecc.steps;
519 int eccstrength = chip->ecc.strength;
521 u8 *ecc_calc = chip->buffers->ecccalc;
522 u8 *ecc_code = chip->buffers->ecccode;
523 unsigned int max_bitflips = 0;
525 for (i = mtd->writesize + FMC2_BBM_LEN, s = 0; s < eccsteps;
526 s++, i += eccbytes, p += eccsize) {
527 chip->ecc.hwctl(mtd, NAND_ECC_READ);
529 /* Read the nand page sector (512 bytes) */
530 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, s * eccsize, -1);
531 chip->read_buf(mtd, p, eccsize);
533 /* Read the corresponding ECC bytes */
534 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, i, -1);
535 chip->read_buf(mtd, ecc_code, eccbytes);
537 /* Correct the data */
538 stat = chip->ecc.correct(mtd, p, ecc_code, ecc_calc);
539 if (stat == -EBADMSG)
540 /* Check for empty pages with bitflips */
541 stat = nand_check_erased_ecc_chunk(p, eccsize,
547 mtd->ecc_stats.failed++;
549 mtd->ecc_stats.corrected += stat;
550 max_bitflips = max_t(unsigned int, max_bitflips, stat);
556 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
557 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
563 static void stm32_fmc2_nfc_init(struct stm32_fmc2_nfc *nfc)
565 u32 pcr = readl(nfc->io_base + FMC2_PCR);
566 u32 bcr1 = readl(nfc->io_base + FMC2_BCR1);
568 /* Set CS used to undefined */
571 /* Enable wait feature and nand flash memory bank */
572 pcr |= FMC2_PCR_PWAITEN;
573 pcr |= FMC2_PCR_PBKEN;
575 /* Set buswidth to 8 bits mode for identification */
576 pcr &= ~FMC2_PCR_PWID;
578 /* ECC logic is disabled */
579 pcr &= ~FMC2_PCR_ECCEN;
582 pcr &= ~FMC2_PCR_ECCALG;
583 pcr &= ~FMC2_PCR_BCHECC;
584 pcr &= ~FMC2_PCR_WEN;
586 /* Set default ECC sector size */
587 pcr &= ~FMC2_PCR_ECCSS;
588 pcr |= FIELD_PREP(FMC2_PCR_ECCSS, FMC2_PCR_ECCSS_2048);
590 /* Set default tclr/tar timings */
591 pcr &= ~FMC2_PCR_TCLR;
592 pcr |= FIELD_PREP(FMC2_PCR_TCLR, FMC2_PCR_TCLR_DEFAULT);
593 pcr &= ~FMC2_PCR_TAR;
594 pcr |= FIELD_PREP(FMC2_PCR_TAR, FMC2_PCR_TAR_DEFAULT);
596 /* Enable FMC2 controller */
597 bcr1 |= FMC2_BCR1_FMC2EN;
599 writel(bcr1, nfc->io_base + FMC2_BCR1);
600 writel(pcr, nfc->io_base + FMC2_PCR);
601 writel(FMC2_PMEM_DEFAULT, nfc->io_base + FMC2_PMEM);
602 writel(FMC2_PATT_DEFAULT, nfc->io_base + FMC2_PATT);
605 static void stm32_fmc2_nfc_calc_timings(struct nand_chip *chip,
606 const struct nand_sdr_timings *sdrt)
608 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
609 struct stm32_fmc2_nand *nand = to_fmc2_nand(chip);
610 struct stm32_fmc2_timings *tims = &nand->timings;
611 unsigned long hclk = clk_get_rate(&nfc->clk);
612 unsigned long hclkp = FMC2_NSEC_PER_SEC / (hclk / 1000);
613 unsigned long timing, tar, tclr, thiz, twait;
614 unsigned long tset_mem, tset_att, thold_mem, thold_att;
616 tar = max_t(unsigned long, hclkp, sdrt->tAR_min);
617 timing = DIV_ROUND_UP(tar, hclkp) - 1;
618 tims->tar = min_t(unsigned long, timing, FMC2_PCR_TIMING_MASK);
620 tclr = max_t(unsigned long, hclkp, sdrt->tCLR_min);
621 timing = DIV_ROUND_UP(tclr, hclkp) - 1;
622 tims->tclr = min_t(unsigned long, timing, FMC2_PCR_TIMING_MASK);
624 tims->thiz = FMC2_THIZ;
625 thiz = (tims->thiz + 1) * hclkp;
632 twait = max_t(unsigned long, hclkp, sdrt->tRP_min);
633 twait = max_t(unsigned long, twait, sdrt->tWP_min);
634 twait = max_t(unsigned long, twait, sdrt->tREA_max + FMC2_TIO);
635 timing = DIV_ROUND_UP(twait, hclkp);
636 tims->twait = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
639 * tSETUP_MEM > tCS - tWAIT
640 * tSETUP_MEM > tALS - tWAIT
641 * tSETUP_MEM > tDS - (tWAIT - tHIZ)
644 if (sdrt->tCS_min > twait && (tset_mem < sdrt->tCS_min - twait))
645 tset_mem = sdrt->tCS_min - twait;
646 if (sdrt->tALS_min > twait && (tset_mem < sdrt->tALS_min - twait))
647 tset_mem = sdrt->tALS_min - twait;
648 if (twait > thiz && (sdrt->tDS_min > twait - thiz) &&
649 (tset_mem < sdrt->tDS_min - (twait - thiz)))
650 tset_mem = sdrt->tDS_min - (twait - thiz);
651 timing = DIV_ROUND_UP(tset_mem, hclkp);
652 tims->tset_mem = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
656 * tHOLD_MEM > tREH - tSETUP_MEM
657 * tHOLD_MEM > max(tRC, tWC) - (tSETUP_MEM + tWAIT)
659 thold_mem = max_t(unsigned long, hclkp, sdrt->tCH_min);
660 if (sdrt->tREH_min > tset_mem &&
661 (thold_mem < sdrt->tREH_min - tset_mem))
662 thold_mem = sdrt->tREH_min - tset_mem;
663 if ((sdrt->tRC_min > tset_mem + twait) &&
664 (thold_mem < sdrt->tRC_min - (tset_mem + twait)))
665 thold_mem = sdrt->tRC_min - (tset_mem + twait);
666 if ((sdrt->tWC_min > tset_mem + twait) &&
667 (thold_mem < sdrt->tWC_min - (tset_mem + twait)))
668 thold_mem = sdrt->tWC_min - (tset_mem + twait);
669 timing = DIV_ROUND_UP(thold_mem, hclkp);
670 tims->thold_mem = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
673 * tSETUP_ATT > tCS - tWAIT
674 * tSETUP_ATT > tCLS - tWAIT
675 * tSETUP_ATT > tALS - tWAIT
676 * tSETUP_ATT > tRHW - tHOLD_MEM
677 * tSETUP_ATT > tDS - (tWAIT - tHIZ)
680 if (sdrt->tCS_min > twait && (tset_att < sdrt->tCS_min - twait))
681 tset_att = sdrt->tCS_min - twait;
682 if (sdrt->tCLS_min > twait && (tset_att < sdrt->tCLS_min - twait))
683 tset_att = sdrt->tCLS_min - twait;
684 if (sdrt->tALS_min > twait && (tset_att < sdrt->tALS_min - twait))
685 tset_att = sdrt->tALS_min - twait;
686 if (sdrt->tRHW_min > thold_mem &&
687 (tset_att < sdrt->tRHW_min - thold_mem))
688 tset_att = sdrt->tRHW_min - thold_mem;
689 if (twait > thiz && (sdrt->tDS_min > twait - thiz) &&
690 (tset_att < sdrt->tDS_min - (twait - thiz)))
691 tset_att = sdrt->tDS_min - (twait - thiz);
692 timing = DIV_ROUND_UP(tset_att, hclkp);
693 tims->tset_att = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
701 * tHOLD_ATT > tWB + tIO + tSYNC - tSETUP_MEM
702 * tHOLD_ATT > tADL - tSETUP_MEM
703 * tHOLD_ATT > tWH - tSETUP_MEM
704 * tHOLD_ATT > tWHR - tSETUP_MEM
705 * tHOLD_ATT > tRC - (tSETUP_ATT + tWAIT)
706 * tHOLD_ATT > tWC - (tSETUP_ATT + tWAIT)
708 thold_att = max_t(unsigned long, hclkp, sdrt->tALH_min);
709 thold_att = max_t(unsigned long, thold_att, sdrt->tCH_min);
710 thold_att = max_t(unsigned long, thold_att, sdrt->tCLH_min);
711 thold_att = max_t(unsigned long, thold_att, sdrt->tCOH_min);
712 thold_att = max_t(unsigned long, thold_att, sdrt->tDH_min);
713 if ((sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC > tset_mem) &&
714 (thold_att < sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC - tset_mem))
715 thold_att = sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC - tset_mem;
716 if (sdrt->tADL_min > tset_mem &&
717 (thold_att < sdrt->tADL_min - tset_mem))
718 thold_att = sdrt->tADL_min - tset_mem;
719 if (sdrt->tWH_min > tset_mem &&
720 (thold_att < sdrt->tWH_min - tset_mem))
721 thold_att = sdrt->tWH_min - tset_mem;
722 if (sdrt->tWHR_min > tset_mem &&
723 (thold_att < sdrt->tWHR_min - tset_mem))
724 thold_att = sdrt->tWHR_min - tset_mem;
725 if ((sdrt->tRC_min > tset_att + twait) &&
726 (thold_att < sdrt->tRC_min - (tset_att + twait)))
727 thold_att = sdrt->tRC_min - (tset_att + twait);
728 if ((sdrt->tWC_min > tset_att + twait) &&
729 (thold_att < sdrt->tWC_min - (tset_att + twait)))
730 thold_att = sdrt->tWC_min - (tset_att + twait);
731 timing = DIV_ROUND_UP(thold_att, hclkp);
732 tims->thold_att = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
735 static int stm32_fmc2_nfc_setup_interface(struct mtd_info *mtd, int chipnr,
736 const struct nand_data_interface *cf)
738 struct nand_chip *chip = mtd_to_nand(mtd);
739 const struct nand_sdr_timings *sdrt;
741 sdrt = nand_get_sdr_timings(cf);
743 return PTR_ERR(sdrt);
745 if (chipnr == NAND_DATA_IFACE_CHECK_ONLY)
748 stm32_fmc2_nfc_calc_timings(chip, sdrt);
749 stm32_fmc2_nfc_timings_init(chip);
754 static void stm32_fmc2_nfc_nand_callbacks_setup(struct nand_chip *chip)
756 chip->ecc.hwctl = stm32_fmc2_nfc_hwctl;
759 * Specific callbacks to read/write a page depending on
760 * the algo used (Hamming, BCH).
762 if (chip->ecc.strength == FMC2_ECC_HAM) {
763 /* Hamming is used */
764 chip->ecc.calculate = stm32_fmc2_nfc_ham_calculate;
765 chip->ecc.correct = stm32_fmc2_nfc_ham_correct;
766 chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 4 : 3;
767 chip->ecc.options |= NAND_ECC_GENERIC_ERASED_CHECK;
772 chip->ecc.read_page = stm32_fmc2_nfc_read_page;
773 chip->ecc.calculate = stm32_fmc2_nfc_bch_calculate;
774 chip->ecc.correct = stm32_fmc2_nfc_bch_correct;
776 if (chip->ecc.strength == FMC2_ECC_BCH8)
777 chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 14 : 13;
779 chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 8 : 7;
782 static int stm32_fmc2_nfc_calc_ecc_bytes(int step_size, int strength)
785 if (strength == FMC2_ECC_HAM)
789 if (strength == FMC2_ECC_BCH8)
796 NAND_ECC_CAPS_SINGLE(stm32_fmc2_nfc_ecc_caps, stm32_fmc2_nfc_calc_ecc_bytes,
798 FMC2_ECC_HAM, FMC2_ECC_BCH4, FMC2_ECC_BCH8);
800 static int stm32_fmc2_nfc_parse_child(struct stm32_fmc2_nfc *nfc, ofnode node)
802 struct stm32_fmc2_nand *nand = &nfc->nand;
806 if (!ofnode_get_property(node, "reg", &nand->ncs))
809 nand->ncs /= sizeof(u32);
811 pr_err("Invalid reg property size\n");
815 ret = ofnode_read_u32_array(node, "reg", cs, nand->ncs);
817 pr_err("Could not retrieve reg property\n");
821 for (i = 0; i < nand->ncs; i++) {
822 if (cs[i] >= FMC2_MAX_CE) {
823 pr_err("Invalid reg value: %d\n",
828 if (nfc->cs_assigned & BIT(cs[i])) {
829 pr_err("Cs already assigned: %d\n",
834 nfc->cs_assigned |= BIT(cs[i]);
835 nand->cs_used[i] = cs[i];
838 nand->chip.flash_node = ofnode_to_offset(node);
843 static int stm32_fmc2_nfc_parse_dt(struct udevice *dev,
844 struct stm32_fmc2_nfc *nfc)
849 dev_for_each_subnode(child, dev)
853 pr_err("NAND chip not defined\n");
858 pr_err("Too many NAND chips defined\n");
862 dev_for_each_subnode(child, dev) {
863 ret = stm32_fmc2_nfc_parse_child(nfc, child);
871 static int stm32_fmc2_nfc_probe(struct udevice *dev)
873 struct stm32_fmc2_nfc *nfc = dev_get_priv(dev);
874 struct stm32_fmc2_nand *nand = &nfc->nand;
875 struct nand_chip *chip = &nand->chip;
876 struct mtd_info *mtd = &chip->mtd;
877 struct nand_ecclayout *ecclayout;
878 struct resource resource;
879 struct reset_ctl reset;
880 int oob_index, chip_cs, mem_region, ret;
883 spin_lock_init(&nfc->controller.lock);
884 init_waitqueue_head(&nfc->controller.wq);
886 ret = stm32_fmc2_nfc_parse_dt(dev, nfc);
891 ret = dev_read_resource(dev, 0, &resource);
893 pr_err("Resource io_base not found");
896 nfc->io_base = (void __iomem *)resource.start;
898 for (chip_cs = 0, mem_region = 1; chip_cs < FMC2_MAX_CE;
899 chip_cs++, mem_region += 3) {
900 if (!(nfc->cs_assigned & BIT(chip_cs)))
903 ret = dev_read_resource(dev, mem_region, &resource);
905 pr_err("Resource data_base not found for cs%d",
909 nfc->data_base[chip_cs] = (void __iomem *)resource.start;
911 ret = dev_read_resource(dev, mem_region + 1, &resource);
913 pr_err("Resource cmd_base not found for cs%d",
917 nfc->cmd_base[chip_cs] = (void __iomem *)resource.start;
919 ret = dev_read_resource(dev, mem_region + 2, &resource);
921 pr_err("Resource addr_base not found for cs%d",
925 nfc->addr_base[chip_cs] = (void __iomem *)resource.start;
928 /* Enable the clock */
929 ret = clk_get_by_index(dev, 0, &nfc->clk);
933 ret = clk_enable(&nfc->clk);
938 ret = reset_get_by_index(dev, 0, &reset);
940 reset_assert(&reset);
942 reset_deassert(&reset);
945 stm32_fmc2_nfc_init(nfc);
947 chip->controller = &nfc->base;
948 chip->select_chip = stm32_fmc2_nfc_select_chip;
949 chip->setup_data_interface = stm32_fmc2_nfc_setup_interface;
950 chip->cmd_ctrl = stm32_fmc2_nfc_cmd_ctrl;
951 chip->chip_delay = FMC2_RB_DELAY_US;
952 chip->options |= NAND_BUSWIDTH_AUTO | NAND_NO_SUBPAGE_WRITE |
953 NAND_USE_BOUNCE_BUFFER;
955 /* Default ECC settings */
956 chip->ecc.mode = NAND_ECC_HW;
957 chip->ecc.size = FMC2_ECC_STEP_SIZE;
958 chip->ecc.strength = FMC2_ECC_BCH8;
960 ret = nand_scan_ident(mtd, nand->ncs, NULL);
965 * Only NAND_ECC_HW mode is actually supported
966 * Hamming => ecc.strength = 1
967 * BCH4 => ecc.strength = 4
968 * BCH8 => ecc.strength = 8
969 * ECC sector size = 512
971 if (chip->ecc.mode != NAND_ECC_HW) {
972 pr_err("Nand_ecc_mode is not well defined in the DT\n");
976 ret = nand_check_ecc_caps(chip, &stm32_fmc2_nfc_ecc_caps,
977 mtd->oobsize - FMC2_BBM_LEN);
979 pr_err("No valid ECC settings set\n");
983 if (chip->bbt_options & NAND_BBT_USE_FLASH)
984 chip->bbt_options |= NAND_BBT_NO_OOB;
986 stm32_fmc2_nfc_nand_callbacks_setup(chip);
988 /* Define ECC layout */
989 ecclayout = &nfc->ecclayout;
990 ecclayout->eccbytes = chip->ecc.bytes *
991 (mtd->writesize / chip->ecc.size);
992 oob_index = FMC2_BBM_LEN;
993 for (i = 0; i < ecclayout->eccbytes; i++, oob_index++)
994 ecclayout->eccpos[i] = oob_index;
995 ecclayout->oobfree->offset = oob_index;
996 ecclayout->oobfree->length = mtd->oobsize - ecclayout->oobfree->offset;
997 chip->ecc.layout = ecclayout;
999 if (chip->options & NAND_BUSWIDTH_16)
1000 stm32_fmc2_nfc_set_buswidth_16(nfc, true);
1002 ret = nand_scan_tail(mtd);
1006 return nand_register(0, mtd);
1009 static const struct udevice_id stm32_fmc2_nfc_match[] = {
1010 { .compatible = "st,stm32mp15-fmc2" },
1014 U_BOOT_DRIVER(stm32_fmc2_nfc) = {
1015 .name = "stm32_fmc2_nfc",
1017 .of_match = stm32_fmc2_nfc_match,
1018 .probe = stm32_fmc2_nfc_probe,
1019 .priv_auto_alloc_size = sizeof(struct stm32_fmc2_nfc),
1022 void board_nand_init(void)
1024 struct udevice *dev;
1027 ret = uclass_get_device_by_driver(UCLASS_MTD,
1028 DM_GET_DRIVER(stm32_fmc2_nfc),
1030 if (ret && ret != -ENODEV)
1031 pr_err("Failed to initialize STM32 FMC2 NFC controller. (error %d)\n",