blob: 64be6486b4e0b6ba3667f1e4ef7abf86e123d511 [file] [log] [blame]
Christophe Kerelloda141682019-04-05 11:41:50 +02001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
3 * Copyright (C) STMicroelectronics 2019
4 * Author: Christophe Kerello <christophe.kerello@st.com>
5 */
6
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +01007#define LOG_CATEGORY UCLASS_MTD
8
Christophe Kerelloda141682019-04-05 11:41:50 +02009#include <common.h>
10#include <clk.h>
11#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Christophe Kerelloda141682019-04-05 11:41:50 +020013#include <nand.h>
14#include <reset.h>
Christophe Kerelloe389a152022-02-22 17:38:49 +010015#include <asm/gpio.h>
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +010016#include <dm/device_compat.h>
Christophe Kerellof4aca872020-07-31 09:53:38 +020017#include <linux/bitfield.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060018#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060019#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070020#include <linux/err.h>
Christophe Kerelloda141682019-04-05 11:41:50 +020021#include <linux/iopoll.h>
22#include <linux/ioport.h>
Tom Rini3bde7e22021-09-22 14:50:35 -040023#include <linux/mtd/rawnand.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060024#include <linux/printk.h>
Christophe Kerelloda141682019-04-05 11:41:50 +020025
26/* Bad block marker length */
27#define FMC2_BBM_LEN 2
28
29/* ECC step size */
30#define FMC2_ECC_STEP_SIZE 512
31
32/* Command delay */
33#define FMC2_RB_DELAY_US 30
34
35/* Max chip enable */
36#define FMC2_MAX_CE 2
37
38/* Timings */
39#define FMC2_THIZ 1
40#define FMC2_TIO 8000
41#define FMC2_TSYNC 3000
42#define FMC2_PCR_TIMING_MASK 0xf
43#define FMC2_PMEM_PATT_TIMING_MASK 0xff
44
45/* FMC2 Controller Registers */
46#define FMC2_BCR1 0x0
47#define FMC2_PCR 0x80
48#define FMC2_SR 0x84
49#define FMC2_PMEM 0x88
50#define FMC2_PATT 0x8c
51#define FMC2_HECCR 0x94
52#define FMC2_BCHISR 0x254
53#define FMC2_BCHICR 0x258
54#define FMC2_BCHPBR1 0x260
55#define FMC2_BCHPBR2 0x264
56#define FMC2_BCHPBR3 0x268
57#define FMC2_BCHPBR4 0x26c
58#define FMC2_BCHDSR0 0x27c
59#define FMC2_BCHDSR1 0x280
60#define FMC2_BCHDSR2 0x284
61#define FMC2_BCHDSR3 0x288
62#define FMC2_BCHDSR4 0x28c
63
64/* Register: FMC2_BCR1 */
65#define FMC2_BCR1_FMC2EN BIT(31)
66
67/* Register: FMC2_PCR */
68#define FMC2_PCR_PWAITEN BIT(1)
69#define FMC2_PCR_PBKEN BIT(2)
Christophe Kerellof4aca872020-07-31 09:53:38 +020070#define FMC2_PCR_PWID GENMASK(5, 4)
Christophe Kerelloda141682019-04-05 11:41:50 +020071#define FMC2_PCR_PWID_BUSWIDTH_8 0
72#define FMC2_PCR_PWID_BUSWIDTH_16 1
73#define FMC2_PCR_ECCEN BIT(6)
74#define FMC2_PCR_ECCALG BIT(8)
Christophe Kerellof4aca872020-07-31 09:53:38 +020075#define FMC2_PCR_TCLR GENMASK(12, 9)
Christophe Kerelloda141682019-04-05 11:41:50 +020076#define FMC2_PCR_TCLR_DEFAULT 0xf
Christophe Kerellof4aca872020-07-31 09:53:38 +020077#define FMC2_PCR_TAR GENMASK(16, 13)
Christophe Kerelloda141682019-04-05 11:41:50 +020078#define FMC2_PCR_TAR_DEFAULT 0xf
Christophe Kerellof4aca872020-07-31 09:53:38 +020079#define FMC2_PCR_ECCSS GENMASK(19, 17)
Christophe Kerelloda141682019-04-05 11:41:50 +020080#define FMC2_PCR_ECCSS_512 1
81#define FMC2_PCR_ECCSS_2048 3
82#define FMC2_PCR_BCHECC BIT(24)
83#define FMC2_PCR_WEN BIT(25)
84
85/* Register: FMC2_SR */
86#define FMC2_SR_NWRF BIT(6)
87
88/* Register: FMC2_PMEM */
Christophe Kerellof4aca872020-07-31 09:53:38 +020089#define FMC2_PMEM_MEMSET GENMASK(7, 0)
90#define FMC2_PMEM_MEMWAIT GENMASK(15, 8)
91#define FMC2_PMEM_MEMHOLD GENMASK(23, 16)
92#define FMC2_PMEM_MEMHIZ GENMASK(31, 24)
Christophe Kerelloda141682019-04-05 11:41:50 +020093#define FMC2_PMEM_DEFAULT 0x0a0a0a0a
94
95/* Register: FMC2_PATT */
Christophe Kerellof4aca872020-07-31 09:53:38 +020096#define FMC2_PATT_ATTSET GENMASK(7, 0)
97#define FMC2_PATT_ATTWAIT GENMASK(15, 8)
98#define FMC2_PATT_ATTHOLD GENMASK(23, 16)
99#define FMC2_PATT_ATTHIZ GENMASK(31, 24)
Christophe Kerelloda141682019-04-05 11:41:50 +0200100#define FMC2_PATT_DEFAULT 0x0a0a0a0a
101
102/* Register: FMC2_BCHISR */
103#define FMC2_BCHISR_DERF BIT(1)
104#define FMC2_BCHISR_EPBRF BIT(4)
105
106/* Register: FMC2_BCHICR */
107#define FMC2_BCHICR_CLEAR_IRQ GENMASK(4, 0)
108
109/* Register: FMC2_BCHDSR0 */
110#define FMC2_BCHDSR0_DUE BIT(0)
111#define FMC2_BCHDSR0_DEF BIT(1)
Christophe Kerellof4aca872020-07-31 09:53:38 +0200112#define FMC2_BCHDSR0_DEN GENMASK(7, 4)
Christophe Kerelloda141682019-04-05 11:41:50 +0200113
114/* Register: FMC2_BCHDSR1 */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200115#define FMC2_BCHDSR1_EBP1 GENMASK(12, 0)
116#define FMC2_BCHDSR1_EBP2 GENMASK(28, 16)
Christophe Kerelloda141682019-04-05 11:41:50 +0200117
118/* Register: FMC2_BCHDSR2 */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200119#define FMC2_BCHDSR2_EBP3 GENMASK(12, 0)
120#define FMC2_BCHDSR2_EBP4 GENMASK(28, 16)
Christophe Kerelloda141682019-04-05 11:41:50 +0200121
122/* Register: FMC2_BCHDSR3 */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200123#define FMC2_BCHDSR3_EBP5 GENMASK(12, 0)
124#define FMC2_BCHDSR3_EBP6 GENMASK(28, 16)
Christophe Kerelloda141682019-04-05 11:41:50 +0200125
126/* Register: FMC2_BCHDSR4 */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200127#define FMC2_BCHDSR4_EBP7 GENMASK(12, 0)
128#define FMC2_BCHDSR4_EBP8 GENMASK(28, 16)
Christophe Kerelloda141682019-04-05 11:41:50 +0200129
130#define FMC2_NSEC_PER_SEC 1000000000L
131
Christophe Kerello92693e32020-07-31 09:53:36 +0200132#define FMC2_TIMEOUT_5S 5000000
133
Christophe Kerelloda141682019-04-05 11:41:50 +0200134enum stm32_fmc2_ecc {
135 FMC2_ECC_HAM = 1,
136 FMC2_ECC_BCH4 = 4,
137 FMC2_ECC_BCH8 = 8
138};
139
140struct stm32_fmc2_timings {
141 u8 tclr;
142 u8 tar;
143 u8 thiz;
144 u8 twait;
145 u8 thold_mem;
146 u8 tset_mem;
147 u8 thold_att;
148 u8 tset_att;
149};
150
151struct stm32_fmc2_nand {
152 struct nand_chip chip;
153 struct stm32_fmc2_timings timings;
Christophe Kerelloe389a152022-02-22 17:38:49 +0100154 struct gpio_desc wp_gpio;
Christophe Kerelloda141682019-04-05 11:41:50 +0200155 int ncs;
156 int cs_used[FMC2_MAX_CE];
157};
158
159static inline struct stm32_fmc2_nand *to_fmc2_nand(struct nand_chip *chip)
160{
161 return container_of(chip, struct stm32_fmc2_nand, chip);
162}
163
164struct stm32_fmc2_nfc {
165 struct nand_hw_control base;
166 struct stm32_fmc2_nand nand;
167 struct nand_ecclayout ecclayout;
Christophe Kerello6276f862020-07-31 09:53:41 +0200168 fdt_addr_t io_base;
169 fdt_addr_t data_base[FMC2_MAX_CE];
170 fdt_addr_t cmd_base[FMC2_MAX_CE];
171 fdt_addr_t addr_base[FMC2_MAX_CE];
Christophe Kerelloda141682019-04-05 11:41:50 +0200172 struct clk clk;
173
174 u8 cs_assigned;
175 int cs_sel;
176};
177
178static inline struct stm32_fmc2_nfc *to_stm32_nfc(struct nand_hw_control *base)
179{
180 return container_of(base, struct stm32_fmc2_nfc, base);
181}
182
Christophe Kerellod1a25872020-07-31 09:53:37 +0200183static void stm32_fmc2_nfc_timings_init(struct nand_chip *chip)
Christophe Kerelloda141682019-04-05 11:41:50 +0200184{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200185 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200186 struct stm32_fmc2_nand *nand = to_fmc2_nand(chip);
187 struct stm32_fmc2_timings *timings = &nand->timings;
Christophe Kerelloda141682019-04-05 11:41:50 +0200188 u32 pmem, patt;
189
190 /* Set tclr/tar timings */
Christophe Kerello9de081d2020-07-31 09:53:39 +0200191 clrsetbits_le32(nfc->io_base + FMC2_PCR,
192 FMC2_PCR_TCLR | FMC2_PCR_TAR,
193 FIELD_PREP(FMC2_PCR_TCLR, timings->tclr) |
194 FIELD_PREP(FMC2_PCR_TAR, timings->tar));
Christophe Kerelloda141682019-04-05 11:41:50 +0200195
196 /* Set tset/twait/thold/thiz timings in common bank */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200197 pmem = FIELD_PREP(FMC2_PMEM_MEMSET, timings->tset_mem);
198 pmem |= FIELD_PREP(FMC2_PMEM_MEMWAIT, timings->twait);
199 pmem |= FIELD_PREP(FMC2_PMEM_MEMHOLD, timings->thold_mem);
200 pmem |= FIELD_PREP(FMC2_PMEM_MEMHIZ, timings->thiz);
Christophe Kerello9de081d2020-07-31 09:53:39 +0200201 writel(pmem, nfc->io_base + FMC2_PMEM);
Christophe Kerelloda141682019-04-05 11:41:50 +0200202
203 /* Set tset/twait/thold/thiz timings in attribut bank */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200204 patt = FIELD_PREP(FMC2_PATT_ATTSET, timings->tset_att);
205 patt |= FIELD_PREP(FMC2_PATT_ATTWAIT, timings->twait);
206 patt |= FIELD_PREP(FMC2_PATT_ATTHOLD, timings->thold_att);
207 patt |= FIELD_PREP(FMC2_PATT_ATTHIZ, timings->thiz);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200208 writel(patt, nfc->io_base + FMC2_PATT);
Christophe Kerelloda141682019-04-05 11:41:50 +0200209}
210
Christophe Kerellod1a25872020-07-31 09:53:37 +0200211static void stm32_fmc2_nfc_setup(struct nand_chip *chip)
Christophe Kerelloda141682019-04-05 11:41:50 +0200212{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200213 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerello9de081d2020-07-31 09:53:39 +0200214 u32 pcr = 0, pcr_mask;
Christophe Kerelloda141682019-04-05 11:41:50 +0200215
216 /* Configure ECC algorithm (default configuration is Hamming) */
Christophe Kerello9de081d2020-07-31 09:53:39 +0200217 pcr_mask = FMC2_PCR_ECCALG;
218 pcr_mask |= FMC2_PCR_BCHECC;
Christophe Kerelloda141682019-04-05 11:41:50 +0200219 if (chip->ecc.strength == FMC2_ECC_BCH8) {
220 pcr |= FMC2_PCR_ECCALG;
221 pcr |= FMC2_PCR_BCHECC;
222 } else if (chip->ecc.strength == FMC2_ECC_BCH4) {
223 pcr |= FMC2_PCR_ECCALG;
224 }
225
226 /* Set buswidth */
Christophe Kerello9de081d2020-07-31 09:53:39 +0200227 pcr_mask |= FMC2_PCR_PWID;
Christophe Kerelloda141682019-04-05 11:41:50 +0200228 if (chip->options & NAND_BUSWIDTH_16)
Christophe Kerellof4aca872020-07-31 09:53:38 +0200229 pcr |= FIELD_PREP(FMC2_PCR_PWID, FMC2_PCR_PWID_BUSWIDTH_16);
Christophe Kerelloda141682019-04-05 11:41:50 +0200230
231 /* Set ECC sector size */
Christophe Kerello9de081d2020-07-31 09:53:39 +0200232 pcr_mask |= FMC2_PCR_ECCSS;
Christophe Kerellof4aca872020-07-31 09:53:38 +0200233 pcr |= FIELD_PREP(FMC2_PCR_ECCSS, FMC2_PCR_ECCSS_512);
Christophe Kerelloda141682019-04-05 11:41:50 +0200234
Christophe Kerello9de081d2020-07-31 09:53:39 +0200235 clrsetbits_le32(nfc->io_base + FMC2_PCR, pcr_mask, pcr);
Christophe Kerelloda141682019-04-05 11:41:50 +0200236}
237
Christophe Kerellod1a25872020-07-31 09:53:37 +0200238static void stm32_fmc2_nfc_select_chip(struct mtd_info *mtd, int chipnr)
Christophe Kerelloda141682019-04-05 11:41:50 +0200239{
240 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200241 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200242 struct stm32_fmc2_nand *nand = to_fmc2_nand(chip);
243
244 if (chipnr < 0 || chipnr >= nand->ncs)
245 return;
246
Christophe Kerellod1a25872020-07-31 09:53:37 +0200247 if (nand->cs_used[chipnr] == nfc->cs_sel)
Christophe Kerelloda141682019-04-05 11:41:50 +0200248 return;
249
Christophe Kerellod1a25872020-07-31 09:53:37 +0200250 nfc->cs_sel = nand->cs_used[chipnr];
Christophe Kerello6276f862020-07-31 09:53:41 +0200251 chip->IO_ADDR_R = (void __iomem *)nfc->data_base[nfc->cs_sel];
252 chip->IO_ADDR_W = (void __iomem *)nfc->data_base[nfc->cs_sel];
Christophe Kerelloda141682019-04-05 11:41:50 +0200253
Christophe Kerellod1a25872020-07-31 09:53:37 +0200254 stm32_fmc2_nfc_setup(chip);
255 stm32_fmc2_nfc_timings_init(chip);
Christophe Kerelloda141682019-04-05 11:41:50 +0200256}
257
Christophe Kerellod1a25872020-07-31 09:53:37 +0200258static void stm32_fmc2_nfc_set_buswidth_16(struct stm32_fmc2_nfc *nfc,
259 bool set)
Christophe Kerelloda141682019-04-05 11:41:50 +0200260{
Christophe Kerello9de081d2020-07-31 09:53:39 +0200261 u32 pcr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200262
Christophe Kerello9de081d2020-07-31 09:53:39 +0200263 pcr = set ? FIELD_PREP(FMC2_PCR_PWID, FMC2_PCR_PWID_BUSWIDTH_16) :
264 FIELD_PREP(FMC2_PCR_PWID, FMC2_PCR_PWID_BUSWIDTH_8);
265
266 clrsetbits_le32(nfc->io_base + FMC2_PCR, FMC2_PCR_PWID, pcr);
Christophe Kerelloda141682019-04-05 11:41:50 +0200267}
268
Christophe Kerellod1a25872020-07-31 09:53:37 +0200269static void stm32_fmc2_nfc_set_ecc(struct stm32_fmc2_nfc *nfc, bool enable)
Christophe Kerelloda141682019-04-05 11:41:50 +0200270{
Christophe Kerello9de081d2020-07-31 09:53:39 +0200271 clrsetbits_le32(nfc->io_base + FMC2_PCR, FMC2_PCR_ECCEN,
272 enable ? FMC2_PCR_ECCEN : 0);
Christophe Kerelloda141682019-04-05 11:41:50 +0200273}
274
Christophe Kerellod1a25872020-07-31 09:53:37 +0200275static void stm32_fmc2_nfc_clear_bch_irq(struct stm32_fmc2_nfc *nfc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200276{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200277 writel(FMC2_BCHICR_CLEAR_IRQ, nfc->io_base + FMC2_BCHICR);
Christophe Kerelloda141682019-04-05 11:41:50 +0200278}
279
Christophe Kerellod1a25872020-07-31 09:53:37 +0200280static void stm32_fmc2_nfc_cmd_ctrl(struct mtd_info *mtd, int cmd,
281 unsigned int ctrl)
Christophe Kerelloda141682019-04-05 11:41:50 +0200282{
283 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200284 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200285
286 if (cmd == NAND_CMD_NONE)
287 return;
288
289 if (ctrl & NAND_CLE) {
Christophe Kerellod1a25872020-07-31 09:53:37 +0200290 writeb(cmd, nfc->cmd_base[nfc->cs_sel]);
Christophe Kerelloda141682019-04-05 11:41:50 +0200291 return;
292 }
293
Christophe Kerellod1a25872020-07-31 09:53:37 +0200294 writeb(cmd, nfc->addr_base[nfc->cs_sel]);
Christophe Kerelloda141682019-04-05 11:41:50 +0200295}
296
297/*
298 * Enable ECC logic and reset syndrome/parity bits previously calculated
299 * Syndrome/parity bits is cleared by setting the ECCEN bit to 0
300 */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200301static void stm32_fmc2_nfc_hwctl(struct mtd_info *mtd, int mode)
Christophe Kerelloda141682019-04-05 11:41:50 +0200302{
303 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200304 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200305
Christophe Kerellod1a25872020-07-31 09:53:37 +0200306 stm32_fmc2_nfc_set_ecc(nfc, false);
Christophe Kerelloda141682019-04-05 11:41:50 +0200307
308 if (chip->ecc.strength != FMC2_ECC_HAM) {
Christophe Kerello9de081d2020-07-31 09:53:39 +0200309 clrsetbits_le32(nfc->io_base + FMC2_PCR, FMC2_PCR_WEN,
310 mode == NAND_ECC_WRITE ? FMC2_PCR_WEN : 0);
Christophe Kerelloda141682019-04-05 11:41:50 +0200311
Christophe Kerellod1a25872020-07-31 09:53:37 +0200312 stm32_fmc2_nfc_clear_bch_irq(nfc);
Christophe Kerelloda141682019-04-05 11:41:50 +0200313 }
314
Christophe Kerellod1a25872020-07-31 09:53:37 +0200315 stm32_fmc2_nfc_set_ecc(nfc, true);
Christophe Kerelloda141682019-04-05 11:41:50 +0200316}
317
318/*
319 * ECC Hamming calculation
320 * ECC is 3 bytes for 512 bytes of data (supports error correction up to
321 * max of 1-bit)
322 */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200323static int stm32_fmc2_nfc_ham_calculate(struct mtd_info *mtd, const u8 *data,
324 u8 *ecc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200325{
326 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200327 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200328 u32 heccr, sr;
329 int ret;
330
Christophe Kerellod1a25872020-07-31 09:53:37 +0200331 ret = readl_poll_timeout(nfc->io_base + FMC2_SR, sr,
Christophe Kerello92693e32020-07-31 09:53:36 +0200332 sr & FMC2_SR_NWRF, FMC2_TIMEOUT_5S);
Christophe Kerelloda141682019-04-05 11:41:50 +0200333 if (ret < 0) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100334 log_err("Ham timeout\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200335 return ret;
336 }
337
Christophe Kerellod1a25872020-07-31 09:53:37 +0200338 heccr = readl(nfc->io_base + FMC2_HECCR);
Christophe Kerelloda141682019-04-05 11:41:50 +0200339
340 ecc[0] = heccr;
341 ecc[1] = heccr >> 8;
342 ecc[2] = heccr >> 16;
343
Christophe Kerellod1a25872020-07-31 09:53:37 +0200344 stm32_fmc2_nfc_set_ecc(nfc, false);
Christophe Kerelloda141682019-04-05 11:41:50 +0200345
346 return 0;
347}
348
Christophe Kerellod1a25872020-07-31 09:53:37 +0200349static int stm32_fmc2_nfc_ham_correct(struct mtd_info *mtd, u8 *dat,
350 u8 *read_ecc, u8 *calc_ecc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200351{
352 u8 bit_position = 0, b0, b1, b2;
353 u32 byte_addr = 0, b;
354 u32 i, shifting = 1;
355
356 /* Indicate which bit and byte is faulty (if any) */
357 b0 = read_ecc[0] ^ calc_ecc[0];
358 b1 = read_ecc[1] ^ calc_ecc[1];
359 b2 = read_ecc[2] ^ calc_ecc[2];
360 b = b0 | (b1 << 8) | (b2 << 16);
361
362 /* No errors */
363 if (likely(!b))
364 return 0;
365
366 /* Calculate bit position */
367 for (i = 0; i < 3; i++) {
368 switch (b % 4) {
369 case 2:
370 bit_position += shifting;
371 case 1:
372 break;
373 default:
374 return -EBADMSG;
375 }
376 shifting <<= 1;
377 b >>= 2;
378 }
379
380 /* Calculate byte position */
381 shifting = 1;
382 for (i = 0; i < 9; i++) {
383 switch (b % 4) {
384 case 2:
385 byte_addr += shifting;
386 case 1:
387 break;
388 default:
389 return -EBADMSG;
390 }
391 shifting <<= 1;
392 b >>= 2;
393 }
394
395 /* Flip the bit */
396 dat[byte_addr] ^= (1 << bit_position);
397
398 return 1;
399}
400
401/*
402 * ECC BCH calculation and correction
403 * ECC is 7/13 bytes for 512 bytes of data (supports error correction up to
404 * max of 4-bit/8-bit)
405 */
406
Christophe Kerellod1a25872020-07-31 09:53:37 +0200407static int stm32_fmc2_nfc_bch_calculate(struct mtd_info *mtd, const u8 *data,
408 u8 *ecc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200409{
410 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200411 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200412 u32 bchpbr, bchisr;
413 int ret;
414
415 /* Wait until the BCH code is ready */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200416 ret = readl_poll_timeout(nfc->io_base + FMC2_BCHISR, bchisr,
Christophe Kerello92693e32020-07-31 09:53:36 +0200417 bchisr & FMC2_BCHISR_EPBRF, FMC2_TIMEOUT_5S);
Christophe Kerelloda141682019-04-05 11:41:50 +0200418 if (ret < 0) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100419 log_err("Bch timeout\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200420 return ret;
421 }
422
423 /* Read parity bits */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200424 bchpbr = readl(nfc->io_base + FMC2_BCHPBR1);
Christophe Kerelloda141682019-04-05 11:41:50 +0200425 ecc[0] = bchpbr;
426 ecc[1] = bchpbr >> 8;
427 ecc[2] = bchpbr >> 16;
428 ecc[3] = bchpbr >> 24;
429
Christophe Kerellod1a25872020-07-31 09:53:37 +0200430 bchpbr = readl(nfc->io_base + FMC2_BCHPBR2);
Christophe Kerelloda141682019-04-05 11:41:50 +0200431 ecc[4] = bchpbr;
432 ecc[5] = bchpbr >> 8;
433 ecc[6] = bchpbr >> 16;
434
435 if (chip->ecc.strength == FMC2_ECC_BCH8) {
436 ecc[7] = bchpbr >> 24;
437
Christophe Kerellod1a25872020-07-31 09:53:37 +0200438 bchpbr = readl(nfc->io_base + FMC2_BCHPBR3);
Christophe Kerelloda141682019-04-05 11:41:50 +0200439 ecc[8] = bchpbr;
440 ecc[9] = bchpbr >> 8;
441 ecc[10] = bchpbr >> 16;
442 ecc[11] = bchpbr >> 24;
443
Christophe Kerellod1a25872020-07-31 09:53:37 +0200444 bchpbr = readl(nfc->io_base + FMC2_BCHPBR4);
Christophe Kerelloda141682019-04-05 11:41:50 +0200445 ecc[12] = bchpbr;
446 }
447
Christophe Kerellod1a25872020-07-31 09:53:37 +0200448 stm32_fmc2_nfc_set_ecc(nfc, false);
Christophe Kerelloda141682019-04-05 11:41:50 +0200449
450 return 0;
451}
452
Christophe Kerellod1a25872020-07-31 09:53:37 +0200453static int stm32_fmc2_nfc_bch_correct(struct mtd_info *mtd, u8 *dat,
454 u8 *read_ecc, u8 *calc_ecc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200455{
456 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200457 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200458 u32 bchdsr0, bchdsr1, bchdsr2, bchdsr3, bchdsr4, bchisr;
459 u16 pos[8];
460 int i, ret, den, eccsize = chip->ecc.size;
461 unsigned int nb_errs = 0;
462
463 /* Wait until the decoding error is ready */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200464 ret = readl_poll_timeout(nfc->io_base + FMC2_BCHISR, bchisr,
Christophe Kerello92693e32020-07-31 09:53:36 +0200465 bchisr & FMC2_BCHISR_DERF, FMC2_TIMEOUT_5S);
Christophe Kerelloda141682019-04-05 11:41:50 +0200466 if (ret < 0) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100467 log_err("Bch timeout\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200468 return ret;
469 }
470
Christophe Kerellod1a25872020-07-31 09:53:37 +0200471 bchdsr0 = readl(nfc->io_base + FMC2_BCHDSR0);
472 bchdsr1 = readl(nfc->io_base + FMC2_BCHDSR1);
473 bchdsr2 = readl(nfc->io_base + FMC2_BCHDSR2);
474 bchdsr3 = readl(nfc->io_base + FMC2_BCHDSR3);
475 bchdsr4 = readl(nfc->io_base + FMC2_BCHDSR4);
Christophe Kerelloda141682019-04-05 11:41:50 +0200476
Christophe Kerellod1a25872020-07-31 09:53:37 +0200477 stm32_fmc2_nfc_set_ecc(nfc, false);
Christophe Kerelloda141682019-04-05 11:41:50 +0200478
479 /* No errors found */
480 if (likely(!(bchdsr0 & FMC2_BCHDSR0_DEF)))
481 return 0;
482
483 /* Too many errors detected */
484 if (unlikely(bchdsr0 & FMC2_BCHDSR0_DUE))
485 return -EBADMSG;
486
Christophe Kerellof4aca872020-07-31 09:53:38 +0200487 pos[0] = FIELD_GET(FMC2_BCHDSR1_EBP1, bchdsr1);
488 pos[1] = FIELD_GET(FMC2_BCHDSR1_EBP2, bchdsr1);
489 pos[2] = FIELD_GET(FMC2_BCHDSR2_EBP3, bchdsr2);
490 pos[3] = FIELD_GET(FMC2_BCHDSR2_EBP4, bchdsr2);
491 pos[4] = FIELD_GET(FMC2_BCHDSR3_EBP5, bchdsr3);
492 pos[5] = FIELD_GET(FMC2_BCHDSR3_EBP6, bchdsr3);
493 pos[6] = FIELD_GET(FMC2_BCHDSR4_EBP7, bchdsr4);
494 pos[7] = FIELD_GET(FMC2_BCHDSR4_EBP8, bchdsr4);
Christophe Kerelloda141682019-04-05 11:41:50 +0200495
Christophe Kerellof4aca872020-07-31 09:53:38 +0200496 den = FIELD_GET(FMC2_BCHDSR0_DEN, bchdsr0);
Christophe Kerelloda141682019-04-05 11:41:50 +0200497 for (i = 0; i < den; i++) {
498 if (pos[i] < eccsize * 8) {
499 __change_bit(pos[i], (unsigned long *)dat);
500 nb_errs++;
501 }
502 }
503
504 return nb_errs;
505}
506
Christophe Kerellod1a25872020-07-31 09:53:37 +0200507static int stm32_fmc2_nfc_read_page(struct mtd_info *mtd,
508 struct nand_chip *chip, u8 *buf,
509 int oob_required, int page)
Christophe Kerelloda141682019-04-05 11:41:50 +0200510{
511 int i, s, stat, eccsize = chip->ecc.size;
512 int eccbytes = chip->ecc.bytes;
513 int eccsteps = chip->ecc.steps;
514 int eccstrength = chip->ecc.strength;
515 u8 *p = buf;
516 u8 *ecc_calc = chip->buffers->ecccalc;
517 u8 *ecc_code = chip->buffers->ecccode;
518 unsigned int max_bitflips = 0;
519
520 for (i = mtd->writesize + FMC2_BBM_LEN, s = 0; s < eccsteps;
521 s++, i += eccbytes, p += eccsize) {
522 chip->ecc.hwctl(mtd, NAND_ECC_READ);
523
524 /* Read the nand page sector (512 bytes) */
525 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, s * eccsize, -1);
526 chip->read_buf(mtd, p, eccsize);
527
528 /* Read the corresponding ECC bytes */
529 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, i, -1);
530 chip->read_buf(mtd, ecc_code, eccbytes);
531
532 /* Correct the data */
533 stat = chip->ecc.correct(mtd, p, ecc_code, ecc_calc);
534 if (stat == -EBADMSG)
535 /* Check for empty pages with bitflips */
536 stat = nand_check_erased_ecc_chunk(p, eccsize,
537 ecc_code, eccbytes,
538 NULL, 0,
539 eccstrength);
540
541 if (stat < 0) {
542 mtd->ecc_stats.failed++;
543 } else {
544 mtd->ecc_stats.corrected += stat;
545 max_bitflips = max_t(unsigned int, max_bitflips, stat);
546 }
547 }
548
549 /* Read oob */
550 if (oob_required) {
551 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
552 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
553 }
554
555 return max_bitflips;
556}
557
Christophe Kerello6276f862020-07-31 09:53:41 +0200558static void stm32_fmc2_nfc_init(struct stm32_fmc2_nfc *nfc, bool has_parent)
Christophe Kerelloda141682019-04-05 11:41:50 +0200559{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200560 u32 pcr = readl(nfc->io_base + FMC2_PCR);
Christophe Kerelloda141682019-04-05 11:41:50 +0200561
562 /* Set CS used to undefined */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200563 nfc->cs_sel = -1;
Christophe Kerelloda141682019-04-05 11:41:50 +0200564
565 /* Enable wait feature and nand flash memory bank */
566 pcr |= FMC2_PCR_PWAITEN;
567 pcr |= FMC2_PCR_PBKEN;
568
569 /* Set buswidth to 8 bits mode for identification */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200570 pcr &= ~FMC2_PCR_PWID;
Christophe Kerelloda141682019-04-05 11:41:50 +0200571
572 /* ECC logic is disabled */
573 pcr &= ~FMC2_PCR_ECCEN;
574
575 /* Default mode */
576 pcr &= ~FMC2_PCR_ECCALG;
577 pcr &= ~FMC2_PCR_BCHECC;
578 pcr &= ~FMC2_PCR_WEN;
579
580 /* Set default ECC sector size */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200581 pcr &= ~FMC2_PCR_ECCSS;
582 pcr |= FIELD_PREP(FMC2_PCR_ECCSS, FMC2_PCR_ECCSS_2048);
Christophe Kerelloda141682019-04-05 11:41:50 +0200583
584 /* Set default tclr/tar timings */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200585 pcr &= ~FMC2_PCR_TCLR;
586 pcr |= FIELD_PREP(FMC2_PCR_TCLR, FMC2_PCR_TCLR_DEFAULT);
587 pcr &= ~FMC2_PCR_TAR;
588 pcr |= FIELD_PREP(FMC2_PCR_TAR, FMC2_PCR_TAR_DEFAULT);
Christophe Kerelloda141682019-04-05 11:41:50 +0200589
590 /* Enable FMC2 controller */
Christophe Kerello6276f862020-07-31 09:53:41 +0200591 if (!has_parent)
592 setbits_le32(nfc->io_base + FMC2_BCR1, FMC2_BCR1_FMC2EN);
Christophe Kerelloda141682019-04-05 11:41:50 +0200593
Christophe Kerellod1a25872020-07-31 09:53:37 +0200594 writel(pcr, nfc->io_base + FMC2_PCR);
595 writel(FMC2_PMEM_DEFAULT, nfc->io_base + FMC2_PMEM);
596 writel(FMC2_PATT_DEFAULT, nfc->io_base + FMC2_PATT);
Christophe Kerelloda141682019-04-05 11:41:50 +0200597}
598
Christophe Kerellod1a25872020-07-31 09:53:37 +0200599static void stm32_fmc2_nfc_calc_timings(struct nand_chip *chip,
600 const struct nand_sdr_timings *sdrt)
Christophe Kerelloda141682019-04-05 11:41:50 +0200601{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200602 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200603 struct stm32_fmc2_nand *nand = to_fmc2_nand(chip);
604 struct stm32_fmc2_timings *tims = &nand->timings;
Christophe Kerellod1a25872020-07-31 09:53:37 +0200605 unsigned long hclk = clk_get_rate(&nfc->clk);
Christophe Kerelloda141682019-04-05 11:41:50 +0200606 unsigned long hclkp = FMC2_NSEC_PER_SEC / (hclk / 1000);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200607 unsigned long timing, tar, tclr, thiz, twait;
608 unsigned long tset_mem, tset_att, thold_mem, thold_att;
Christophe Kerelloda141682019-04-05 11:41:50 +0200609
Patrick Delaunay804858a2019-06-21 15:26:54 +0200610 tar = max_t(unsigned long, hclkp, sdrt->tAR_min);
611 timing = DIV_ROUND_UP(tar, hclkp) - 1;
612 tims->tar = min_t(unsigned long, timing, FMC2_PCR_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200613
Patrick Delaunay804858a2019-06-21 15:26:54 +0200614 tclr = max_t(unsigned long, hclkp, sdrt->tCLR_min);
615 timing = DIV_ROUND_UP(tclr, hclkp) - 1;
616 tims->tclr = min_t(unsigned long, timing, FMC2_PCR_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200617
618 tims->thiz = FMC2_THIZ;
619 thiz = (tims->thiz + 1) * hclkp;
620
621 /*
622 * tWAIT > tRP
623 * tWAIT > tWP
624 * tWAIT > tREA + tIO
625 */
Patrick Delaunay804858a2019-06-21 15:26:54 +0200626 twait = max_t(unsigned long, hclkp, sdrt->tRP_min);
627 twait = max_t(unsigned long, twait, sdrt->tWP_min);
628 twait = max_t(unsigned long, twait, sdrt->tREA_max + FMC2_TIO);
629 timing = DIV_ROUND_UP(twait, hclkp);
630 tims->twait = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200631
632 /*
633 * tSETUP_MEM > tCS - tWAIT
634 * tSETUP_MEM > tALS - tWAIT
635 * tSETUP_MEM > tDS - (tWAIT - tHIZ)
636 */
637 tset_mem = hclkp;
638 if (sdrt->tCS_min > twait && (tset_mem < sdrt->tCS_min - twait))
639 tset_mem = sdrt->tCS_min - twait;
640 if (sdrt->tALS_min > twait && (tset_mem < sdrt->tALS_min - twait))
641 tset_mem = sdrt->tALS_min - twait;
642 if (twait > thiz && (sdrt->tDS_min > twait - thiz) &&
643 (tset_mem < sdrt->tDS_min - (twait - thiz)))
644 tset_mem = sdrt->tDS_min - (twait - thiz);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200645 timing = DIV_ROUND_UP(tset_mem, hclkp);
646 tims->tset_mem = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200647
648 /*
649 * tHOLD_MEM > tCH
650 * tHOLD_MEM > tREH - tSETUP_MEM
651 * tHOLD_MEM > max(tRC, tWC) - (tSETUP_MEM + tWAIT)
652 */
Patrick Delaunay804858a2019-06-21 15:26:54 +0200653 thold_mem = max_t(unsigned long, hclkp, sdrt->tCH_min);
Christophe Kerelloda141682019-04-05 11:41:50 +0200654 if (sdrt->tREH_min > tset_mem &&
655 (thold_mem < sdrt->tREH_min - tset_mem))
656 thold_mem = sdrt->tREH_min - tset_mem;
657 if ((sdrt->tRC_min > tset_mem + twait) &&
658 (thold_mem < sdrt->tRC_min - (tset_mem + twait)))
659 thold_mem = sdrt->tRC_min - (tset_mem + twait);
660 if ((sdrt->tWC_min > tset_mem + twait) &&
661 (thold_mem < sdrt->tWC_min - (tset_mem + twait)))
662 thold_mem = sdrt->tWC_min - (tset_mem + twait);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200663 timing = DIV_ROUND_UP(thold_mem, hclkp);
664 tims->thold_mem = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200665
666 /*
667 * tSETUP_ATT > tCS - tWAIT
668 * tSETUP_ATT > tCLS - tWAIT
669 * tSETUP_ATT > tALS - tWAIT
670 * tSETUP_ATT > tRHW - tHOLD_MEM
671 * tSETUP_ATT > tDS - (tWAIT - tHIZ)
672 */
673 tset_att = hclkp;
674 if (sdrt->tCS_min > twait && (tset_att < sdrt->tCS_min - twait))
675 tset_att = sdrt->tCS_min - twait;
676 if (sdrt->tCLS_min > twait && (tset_att < sdrt->tCLS_min - twait))
677 tset_att = sdrt->tCLS_min - twait;
678 if (sdrt->tALS_min > twait && (tset_att < sdrt->tALS_min - twait))
679 tset_att = sdrt->tALS_min - twait;
680 if (sdrt->tRHW_min > thold_mem &&
681 (tset_att < sdrt->tRHW_min - thold_mem))
682 tset_att = sdrt->tRHW_min - thold_mem;
683 if (twait > thiz && (sdrt->tDS_min > twait - thiz) &&
684 (tset_att < sdrt->tDS_min - (twait - thiz)))
685 tset_att = sdrt->tDS_min - (twait - thiz);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200686 timing = DIV_ROUND_UP(tset_att, hclkp);
687 tims->tset_att = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200688
689 /*
690 * tHOLD_ATT > tALH
691 * tHOLD_ATT > tCH
692 * tHOLD_ATT > tCLH
693 * tHOLD_ATT > tCOH
694 * tHOLD_ATT > tDH
695 * tHOLD_ATT > tWB + tIO + tSYNC - tSETUP_MEM
696 * tHOLD_ATT > tADL - tSETUP_MEM
697 * tHOLD_ATT > tWH - tSETUP_MEM
698 * tHOLD_ATT > tWHR - tSETUP_MEM
699 * tHOLD_ATT > tRC - (tSETUP_ATT + tWAIT)
700 * tHOLD_ATT > tWC - (tSETUP_ATT + tWAIT)
701 */
Patrick Delaunay804858a2019-06-21 15:26:54 +0200702 thold_att = max_t(unsigned long, hclkp, sdrt->tALH_min);
703 thold_att = max_t(unsigned long, thold_att, sdrt->tCH_min);
704 thold_att = max_t(unsigned long, thold_att, sdrt->tCLH_min);
705 thold_att = max_t(unsigned long, thold_att, sdrt->tCOH_min);
706 thold_att = max_t(unsigned long, thold_att, sdrt->tDH_min);
Christophe Kerelloda141682019-04-05 11:41:50 +0200707 if ((sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC > tset_mem) &&
708 (thold_att < sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC - tset_mem))
709 thold_att = sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC - tset_mem;
710 if (sdrt->tADL_min > tset_mem &&
711 (thold_att < sdrt->tADL_min - tset_mem))
712 thold_att = sdrt->tADL_min - tset_mem;
713 if (sdrt->tWH_min > tset_mem &&
714 (thold_att < sdrt->tWH_min - tset_mem))
715 thold_att = sdrt->tWH_min - tset_mem;
716 if (sdrt->tWHR_min > tset_mem &&
717 (thold_att < sdrt->tWHR_min - tset_mem))
718 thold_att = sdrt->tWHR_min - tset_mem;
719 if ((sdrt->tRC_min > tset_att + twait) &&
720 (thold_att < sdrt->tRC_min - (tset_att + twait)))
721 thold_att = sdrt->tRC_min - (tset_att + twait);
722 if ((sdrt->tWC_min > tset_att + twait) &&
723 (thold_att < sdrt->tWC_min - (tset_att + twait)))
724 thold_att = sdrt->tWC_min - (tset_att + twait);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200725 timing = DIV_ROUND_UP(thold_att, hclkp);
726 tims->thold_att = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200727}
728
Christophe Kerellod1a25872020-07-31 09:53:37 +0200729static int stm32_fmc2_nfc_setup_interface(struct mtd_info *mtd, int chipnr,
730 const struct nand_data_interface *cf)
Christophe Kerelloda141682019-04-05 11:41:50 +0200731{
732 struct nand_chip *chip = mtd_to_nand(mtd);
733 const struct nand_sdr_timings *sdrt;
734
Christophe Kerellod1a25872020-07-31 09:53:37 +0200735 sdrt = nand_get_sdr_timings(cf);
Christophe Kerelloda141682019-04-05 11:41:50 +0200736 if (IS_ERR(sdrt))
737 return PTR_ERR(sdrt);
738
Christophe Kerello1b4662a2023-03-30 11:16:21 +0200739 if (sdrt->tRC_min < 30000)
740 return -EOPNOTSUPP;
741
Christophe Kerelloda141682019-04-05 11:41:50 +0200742 if (chipnr == NAND_DATA_IFACE_CHECK_ONLY)
743 return 0;
744
Christophe Kerellod1a25872020-07-31 09:53:37 +0200745 stm32_fmc2_nfc_calc_timings(chip, sdrt);
746 stm32_fmc2_nfc_timings_init(chip);
Christophe Kerelloda141682019-04-05 11:41:50 +0200747
748 return 0;
749}
750
Christophe Kerellod1a25872020-07-31 09:53:37 +0200751static void stm32_fmc2_nfc_nand_callbacks_setup(struct nand_chip *chip)
Christophe Kerelloda141682019-04-05 11:41:50 +0200752{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200753 chip->ecc.hwctl = stm32_fmc2_nfc_hwctl;
Christophe Kerelloda141682019-04-05 11:41:50 +0200754
755 /*
756 * Specific callbacks to read/write a page depending on
757 * the algo used (Hamming, BCH).
758 */
759 if (chip->ecc.strength == FMC2_ECC_HAM) {
760 /* Hamming is used */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200761 chip->ecc.calculate = stm32_fmc2_nfc_ham_calculate;
762 chip->ecc.correct = stm32_fmc2_nfc_ham_correct;
Christophe Kerelloda141682019-04-05 11:41:50 +0200763 chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 4 : 3;
764 chip->ecc.options |= NAND_ECC_GENERIC_ERASED_CHECK;
765 return;
766 }
767
768 /* BCH is used */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200769 chip->ecc.read_page = stm32_fmc2_nfc_read_page;
770 chip->ecc.calculate = stm32_fmc2_nfc_bch_calculate;
771 chip->ecc.correct = stm32_fmc2_nfc_bch_correct;
Christophe Kerelloda141682019-04-05 11:41:50 +0200772
773 if (chip->ecc.strength == FMC2_ECC_BCH8)
774 chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 14 : 13;
775 else
776 chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 8 : 7;
777}
778
Christophe Kerellod1a25872020-07-31 09:53:37 +0200779static int stm32_fmc2_nfc_calc_ecc_bytes(int step_size, int strength)
Christophe Kerelloda141682019-04-05 11:41:50 +0200780{
781 /* Hamming */
782 if (strength == FMC2_ECC_HAM)
783 return 4;
784
785 /* BCH8 */
786 if (strength == FMC2_ECC_BCH8)
787 return 14;
788
789 /* BCH4 */
790 return 8;
791}
792
Christophe Kerellod1a25872020-07-31 09:53:37 +0200793NAND_ECC_CAPS_SINGLE(stm32_fmc2_nfc_ecc_caps, stm32_fmc2_nfc_calc_ecc_bytes,
Christophe Kerelloda141682019-04-05 11:41:50 +0200794 FMC2_ECC_STEP_SIZE,
795 FMC2_ECC_HAM, FMC2_ECC_BCH4, FMC2_ECC_BCH8);
796
Christophe Kerellod1a25872020-07-31 09:53:37 +0200797static int stm32_fmc2_nfc_parse_child(struct stm32_fmc2_nfc *nfc, ofnode node)
Christophe Kerelloda141682019-04-05 11:41:50 +0200798{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200799 struct stm32_fmc2_nand *nand = &nfc->nand;
Christophe Kerelloda141682019-04-05 11:41:50 +0200800 u32 cs[FMC2_MAX_CE];
801 int ret, i;
802
803 if (!ofnode_get_property(node, "reg", &nand->ncs))
804 return -EINVAL;
805
806 nand->ncs /= sizeof(u32);
807 if (!nand->ncs) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100808 log_err("Invalid reg property size\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200809 return -EINVAL;
810 }
811
812 ret = ofnode_read_u32_array(node, "reg", cs, nand->ncs);
813 if (ret < 0) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100814 log_err("Could not retrieve reg property\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200815 return -EINVAL;
816 }
817
818 for (i = 0; i < nand->ncs; i++) {
Christophe Kerello45dd1ee2020-07-31 09:53:34 +0200819 if (cs[i] >= FMC2_MAX_CE) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100820 log_err("Invalid reg value: %d\n", nand->cs_used[i]);
Christophe Kerelloda141682019-04-05 11:41:50 +0200821 return -EINVAL;
822 }
823
Christophe Kerellod1a25872020-07-31 09:53:37 +0200824 if (nfc->cs_assigned & BIT(cs[i])) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100825 log_err("Cs already assigned: %d\n", nand->cs_used[i]);
Christophe Kerelloda141682019-04-05 11:41:50 +0200826 return -EINVAL;
827 }
828
Christophe Kerellod1a25872020-07-31 09:53:37 +0200829 nfc->cs_assigned |= BIT(cs[i]);
Christophe Kerelloda141682019-04-05 11:41:50 +0200830 nand->cs_used[i] = cs[i];
831 }
832
Christophe Kerelloe389a152022-02-22 17:38:49 +0100833 gpio_request_by_name_nodev(node, "wp-gpios", 0, &nand->wp_gpio,
834 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
835
Patrice Chotard33d2cf92021-09-13 16:25:53 +0200836 nand->chip.flash_node = node;
Christophe Kerelloda141682019-04-05 11:41:50 +0200837
838 return 0;
839}
840
Christophe Kerellod1a25872020-07-31 09:53:37 +0200841static int stm32_fmc2_nfc_parse_dt(struct udevice *dev,
842 struct stm32_fmc2_nfc *nfc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200843{
844 ofnode child;
845 int ret, nchips = 0;
846
847 dev_for_each_subnode(child, dev)
848 nchips++;
849
850 if (!nchips) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100851 log_err("NAND chip not defined\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200852 return -EINVAL;
853 }
854
855 if (nchips > 1) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100856 log_err("Too many NAND chips defined\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200857 return -EINVAL;
858 }
859
860 dev_for_each_subnode(child, dev) {
Christophe Kerellod1a25872020-07-31 09:53:37 +0200861 ret = stm32_fmc2_nfc_parse_child(nfc, child);
Christophe Kerelloda141682019-04-05 11:41:50 +0200862 if (ret)
863 return ret;
864 }
865
866 return 0;
867}
868
Christophe Kerello6276f862020-07-31 09:53:41 +0200869static struct udevice *stm32_fmc2_nfc_get_cdev(struct udevice *dev)
870{
871 struct udevice *pdev = dev_get_parent(dev);
872 struct udevice *cdev = NULL;
873 bool ebi_found = false;
874
875 if (pdev && ofnode_device_is_compatible(dev_ofnode(pdev),
876 "st,stm32mp1-fmc2-ebi"))
877 ebi_found = true;
878
879 if (ofnode_device_is_compatible(dev_ofnode(dev),
880 "st,stm32mp1-fmc2-nfc")) {
881 if (ebi_found)
882 cdev = pdev;
883
884 return cdev;
885 }
886
887 if (!ebi_found)
888 cdev = dev;
889
890 return cdev;
891}
892
Christophe Kerellod1a25872020-07-31 09:53:37 +0200893static int stm32_fmc2_nfc_probe(struct udevice *dev)
Christophe Kerelloda141682019-04-05 11:41:50 +0200894{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200895 struct stm32_fmc2_nfc *nfc = dev_get_priv(dev);
896 struct stm32_fmc2_nand *nand = &nfc->nand;
Christophe Kerelloda141682019-04-05 11:41:50 +0200897 struct nand_chip *chip = &nand->chip;
898 struct mtd_info *mtd = &chip->mtd;
899 struct nand_ecclayout *ecclayout;
Christophe Kerello6276f862020-07-31 09:53:41 +0200900 struct udevice *cdev;
Christophe Kerelloda141682019-04-05 11:41:50 +0200901 struct reset_ctl reset;
Patrick Delaunay804858a2019-06-21 15:26:54 +0200902 int oob_index, chip_cs, mem_region, ret;
903 unsigned int i;
Christophe Kerello6276f862020-07-31 09:53:41 +0200904 int start_region = 0;
905 fdt_addr_t addr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200906
Christophe Kerellod1a25872020-07-31 09:53:37 +0200907 spin_lock_init(&nfc->controller.lock);
908 init_waitqueue_head(&nfc->controller.wq);
Christophe Kerelloda141682019-04-05 11:41:50 +0200909
Christophe Kerello6276f862020-07-31 09:53:41 +0200910 cdev = stm32_fmc2_nfc_get_cdev(dev);
911 if (!cdev)
912 return -EINVAL;
913
Christophe Kerellod1a25872020-07-31 09:53:37 +0200914 ret = stm32_fmc2_nfc_parse_dt(dev, nfc);
Christophe Kerelloda141682019-04-05 11:41:50 +0200915 if (ret)
916 return ret;
917
Christophe Kerello6276f862020-07-31 09:53:41 +0200918 nfc->io_base = dev_read_addr(cdev);
919 if (nfc->io_base == FDT_ADDR_T_NONE)
920 return -EINVAL;
921
922 if (dev == cdev)
923 start_region = 1;
Christophe Kerelloda141682019-04-05 11:41:50 +0200924
Christophe Kerello6276f862020-07-31 09:53:41 +0200925 for (chip_cs = 0, mem_region = start_region; chip_cs < FMC2_MAX_CE;
Christophe Kerelloda141682019-04-05 11:41:50 +0200926 chip_cs++, mem_region += 3) {
Christophe Kerellod1a25872020-07-31 09:53:37 +0200927 if (!(nfc->cs_assigned & BIT(chip_cs)))
Christophe Kerelloda141682019-04-05 11:41:50 +0200928 continue;
929
Christophe Kerello6276f862020-07-31 09:53:41 +0200930 addr = dev_read_addr_index(dev, mem_region);
931 if (addr == FDT_ADDR_T_NONE) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100932 dev_err(dev, "Resource data_base not found for cs%d", chip_cs);
Christophe Kerelloda141682019-04-05 11:41:50 +0200933 return ret;
934 }
Christophe Kerello6276f862020-07-31 09:53:41 +0200935 nfc->data_base[chip_cs] = addr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200936
Christophe Kerello6276f862020-07-31 09:53:41 +0200937 addr = dev_read_addr_index(dev, mem_region + 1);
938 if (addr == FDT_ADDR_T_NONE) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100939 dev_err(dev, "Resource cmd_base not found for cs%d", chip_cs);
Christophe Kerelloda141682019-04-05 11:41:50 +0200940 return ret;
941 }
Christophe Kerello6276f862020-07-31 09:53:41 +0200942 nfc->cmd_base[chip_cs] = addr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200943
Christophe Kerello6276f862020-07-31 09:53:41 +0200944 addr = dev_read_addr_index(dev, mem_region + 2);
945 if (addr == FDT_ADDR_T_NONE) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100946 dev_err(dev, "Resource addr_base not found for cs%d", chip_cs);
Christophe Kerelloda141682019-04-05 11:41:50 +0200947 return ret;
948 }
Christophe Kerello6276f862020-07-31 09:53:41 +0200949 nfc->addr_base[chip_cs] = addr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200950 }
951
952 /* Enable the clock */
Christophe Kerello6276f862020-07-31 09:53:41 +0200953 ret = clk_get_by_index(cdev, 0, &nfc->clk);
Christophe Kerelloda141682019-04-05 11:41:50 +0200954 if (ret)
955 return ret;
956
Christophe Kerellod1a25872020-07-31 09:53:37 +0200957 ret = clk_enable(&nfc->clk);
Christophe Kerelloda141682019-04-05 11:41:50 +0200958 if (ret)
959 return ret;
960
961 /* Reset */
962 ret = reset_get_by_index(dev, 0, &reset);
963 if (!ret) {
964 reset_assert(&reset);
965 udelay(2);
966 reset_deassert(&reset);
967 }
968
Christophe Kerello6276f862020-07-31 09:53:41 +0200969 stm32_fmc2_nfc_init(nfc, dev != cdev);
Christophe Kerelloda141682019-04-05 11:41:50 +0200970
Christophe Kerellod1a25872020-07-31 09:53:37 +0200971 chip->controller = &nfc->base;
972 chip->select_chip = stm32_fmc2_nfc_select_chip;
973 chip->setup_data_interface = stm32_fmc2_nfc_setup_interface;
974 chip->cmd_ctrl = stm32_fmc2_nfc_cmd_ctrl;
Christophe Kerelloda141682019-04-05 11:41:50 +0200975 chip->chip_delay = FMC2_RB_DELAY_US;
976 chip->options |= NAND_BUSWIDTH_AUTO | NAND_NO_SUBPAGE_WRITE |
977 NAND_USE_BOUNCE_BUFFER;
978
979 /* Default ECC settings */
980 chip->ecc.mode = NAND_ECC_HW;
981 chip->ecc.size = FMC2_ECC_STEP_SIZE;
982 chip->ecc.strength = FMC2_ECC_BCH8;
983
Christophe Kerelloe389a152022-02-22 17:38:49 +0100984 /* Disable Write Protect */
985 if (dm_gpio_is_valid(&nand->wp_gpio))
986 dm_gpio_set_value(&nand->wp_gpio, 0);
987
Christophe Kerelloda141682019-04-05 11:41:50 +0200988 ret = nand_scan_ident(mtd, nand->ncs, NULL);
989 if (ret)
990 return ret;
991
992 /*
993 * Only NAND_ECC_HW mode is actually supported
994 * Hamming => ecc.strength = 1
995 * BCH4 => ecc.strength = 4
996 * BCH8 => ecc.strength = 8
997 * ECC sector size = 512
998 */
999 if (chip->ecc.mode != NAND_ECC_HW) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +01001000 dev_err(dev, "Nand_ecc_mode is not well defined in the DT\n");
Christophe Kerelloda141682019-04-05 11:41:50 +02001001 return -EINVAL;
1002 }
1003
Christophe Kerellod1a25872020-07-31 09:53:37 +02001004 ret = nand_check_ecc_caps(chip, &stm32_fmc2_nfc_ecc_caps,
Christophe Kerelloda141682019-04-05 11:41:50 +02001005 mtd->oobsize - FMC2_BBM_LEN);
1006 if (ret) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +01001007 dev_err(dev, "No valid ECC settings set\n");
Christophe Kerelloda141682019-04-05 11:41:50 +02001008 return ret;
1009 }
1010
1011 if (chip->bbt_options & NAND_BBT_USE_FLASH)
1012 chip->bbt_options |= NAND_BBT_NO_OOB;
1013
Christophe Kerellod1a25872020-07-31 09:53:37 +02001014 stm32_fmc2_nfc_nand_callbacks_setup(chip);
Christophe Kerelloda141682019-04-05 11:41:50 +02001015
1016 /* Define ECC layout */
Christophe Kerellod1a25872020-07-31 09:53:37 +02001017 ecclayout = &nfc->ecclayout;
Christophe Kerelloda141682019-04-05 11:41:50 +02001018 ecclayout->eccbytes = chip->ecc.bytes *
1019 (mtd->writesize / chip->ecc.size);
1020 oob_index = FMC2_BBM_LEN;
1021 for (i = 0; i < ecclayout->eccbytes; i++, oob_index++)
1022 ecclayout->eccpos[i] = oob_index;
1023 ecclayout->oobfree->offset = oob_index;
1024 ecclayout->oobfree->length = mtd->oobsize - ecclayout->oobfree->offset;
1025 chip->ecc.layout = ecclayout;
1026
Christophe Kerelloda141682019-04-05 11:41:50 +02001027 if (chip->options & NAND_BUSWIDTH_16)
Christophe Kerellod1a25872020-07-31 09:53:37 +02001028 stm32_fmc2_nfc_set_buswidth_16(nfc, true);
Christophe Kerelloda141682019-04-05 11:41:50 +02001029
Christophe Kerelloda141682019-04-05 11:41:50 +02001030 ret = nand_scan_tail(mtd);
1031 if (ret)
1032 return ret;
1033
1034 return nand_register(0, mtd);
1035}
1036
Christophe Kerellod1a25872020-07-31 09:53:37 +02001037static const struct udevice_id stm32_fmc2_nfc_match[] = {
Christophe Kerelloda141682019-04-05 11:41:50 +02001038 { .compatible = "st,stm32mp15-fmc2" },
Christophe Kerello6276f862020-07-31 09:53:41 +02001039 { .compatible = "st,stm32mp1-fmc2-nfc" },
Christophe Kerelloda141682019-04-05 11:41:50 +02001040 { /* Sentinel */ }
1041};
1042
Christophe Kerellod1a25872020-07-31 09:53:37 +02001043U_BOOT_DRIVER(stm32_fmc2_nfc) = {
1044 .name = "stm32_fmc2_nfc",
Christophe Kerelloda141682019-04-05 11:41:50 +02001045 .id = UCLASS_MTD,
Christophe Kerellod1a25872020-07-31 09:53:37 +02001046 .of_match = stm32_fmc2_nfc_match,
1047 .probe = stm32_fmc2_nfc_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001048 .priv_auto = sizeof(struct stm32_fmc2_nfc),
Christophe Kerelloda141682019-04-05 11:41:50 +02001049};
1050
1051void board_nand_init(void)
1052{
1053 struct udevice *dev;
1054 int ret;
1055
1056 ret = uclass_get_device_by_driver(UCLASS_MTD,
Simon Glass65130cd2020-12-28 20:34:56 -07001057 DM_DRIVER_GET(stm32_fmc2_nfc),
Christophe Kerelloda141682019-04-05 11:41:50 +02001058 &dev);
1059 if (ret && ret != -ENODEV)
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +01001060 log_err("Failed to initialize STM32 FMC2 NFC controller. (error %d)\n",
1061 ret);
Christophe Kerelloda141682019-04-05 11:41:50 +02001062}