blob: 083ea4c5a74b782ba655e1b9eac87823be9cce4e [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 <clk.h>
10#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Christophe Kerelloda141682019-04-05 11:41:50 +020012#include <nand.h>
13#include <reset.h>
Christophe Kerelloe389a152022-02-22 17:38:49 +010014#include <asm/gpio.h>
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +010015#include <dm/device_compat.h>
Christophe Kerellof4aca872020-07-31 09:53:38 +020016#include <linux/bitfield.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060018#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070019#include <linux/err.h>
Christophe Kerelloda141682019-04-05 11:41:50 +020020#include <linux/iopoll.h>
21#include <linux/ioport.h>
Tom Rini3bde7e22021-09-22 14:50:35 -040022#include <linux/mtd/rawnand.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060023#include <linux/printk.h>
Igor Prusovc3421ea2023-11-09 20:10:04 +030024#include <linux/time.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 */
Christophe Kerello7ea25d62024-03-06 10:54:06 +010036#define FMC2_MAX_CE 4
Christophe Kerelloda141682019-04-05 11:41:50 +020037
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
Christophe Kerello92693e32020-07-31 09:53:36 +0200130#define FMC2_TIMEOUT_5S 5000000
131
Christophe Kerelloda141682019-04-05 11:41:50 +0200132enum stm32_fmc2_ecc {
133 FMC2_ECC_HAM = 1,
134 FMC2_ECC_BCH4 = 4,
135 FMC2_ECC_BCH8 = 8
136};
137
138struct stm32_fmc2_timings {
139 u8 tclr;
140 u8 tar;
141 u8 thiz;
142 u8 twait;
143 u8 thold_mem;
144 u8 tset_mem;
145 u8 thold_att;
146 u8 tset_att;
147};
148
149struct stm32_fmc2_nand {
150 struct nand_chip chip;
151 struct stm32_fmc2_timings timings;
Christophe Kerelloe389a152022-02-22 17:38:49 +0100152 struct gpio_desc wp_gpio;
Christophe Kerelloda141682019-04-05 11:41:50 +0200153 int ncs;
154 int cs_used[FMC2_MAX_CE];
155};
156
157static inline struct stm32_fmc2_nand *to_fmc2_nand(struct nand_chip *chip)
158{
159 return container_of(chip, struct stm32_fmc2_nand, chip);
160}
161
Christophe Kerello7ea25d62024-03-06 10:54:06 +0100162struct stm32_fmc2_nfc_data {
163 int max_ncs;
164 struct udevice *(*get_cdev)(struct udevice *dev);
165};
166
Christophe Kerelloda141682019-04-05 11:41:50 +0200167struct stm32_fmc2_nfc {
168 struct nand_hw_control base;
169 struct stm32_fmc2_nand nand;
170 struct nand_ecclayout ecclayout;
Christophe Kerello6276f862020-07-31 09:53:41 +0200171 fdt_addr_t io_base;
172 fdt_addr_t data_base[FMC2_MAX_CE];
173 fdt_addr_t cmd_base[FMC2_MAX_CE];
174 fdt_addr_t addr_base[FMC2_MAX_CE];
Christophe Kerelloda141682019-04-05 11:41:50 +0200175 struct clk clk;
Christophe Kerello7ea25d62024-03-06 10:54:06 +0100176 const struct stm32_fmc2_nfc_data *data;
Christophe Kerelloda141682019-04-05 11:41:50 +0200177
178 u8 cs_assigned;
179 int cs_sel;
180};
181
182static inline struct stm32_fmc2_nfc *to_stm32_nfc(struct nand_hw_control *base)
183{
184 return container_of(base, struct stm32_fmc2_nfc, base);
185}
186
Christophe Kerellod1a25872020-07-31 09:53:37 +0200187static void stm32_fmc2_nfc_timings_init(struct nand_chip *chip)
Christophe Kerelloda141682019-04-05 11:41:50 +0200188{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200189 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200190 struct stm32_fmc2_nand *nand = to_fmc2_nand(chip);
191 struct stm32_fmc2_timings *timings = &nand->timings;
Christophe Kerelloda141682019-04-05 11:41:50 +0200192 u32 pmem, patt;
193
194 /* Set tclr/tar timings */
Christophe Kerello9de081d2020-07-31 09:53:39 +0200195 clrsetbits_le32(nfc->io_base + FMC2_PCR,
196 FMC2_PCR_TCLR | FMC2_PCR_TAR,
197 FIELD_PREP(FMC2_PCR_TCLR, timings->tclr) |
198 FIELD_PREP(FMC2_PCR_TAR, timings->tar));
Christophe Kerelloda141682019-04-05 11:41:50 +0200199
200 /* Set tset/twait/thold/thiz timings in common bank */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200201 pmem = FIELD_PREP(FMC2_PMEM_MEMSET, timings->tset_mem);
202 pmem |= FIELD_PREP(FMC2_PMEM_MEMWAIT, timings->twait);
203 pmem |= FIELD_PREP(FMC2_PMEM_MEMHOLD, timings->thold_mem);
204 pmem |= FIELD_PREP(FMC2_PMEM_MEMHIZ, timings->thiz);
Christophe Kerello9de081d2020-07-31 09:53:39 +0200205 writel(pmem, nfc->io_base + FMC2_PMEM);
Christophe Kerelloda141682019-04-05 11:41:50 +0200206
207 /* Set tset/twait/thold/thiz timings in attribut bank */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200208 patt = FIELD_PREP(FMC2_PATT_ATTSET, timings->tset_att);
209 patt |= FIELD_PREP(FMC2_PATT_ATTWAIT, timings->twait);
210 patt |= FIELD_PREP(FMC2_PATT_ATTHOLD, timings->thold_att);
211 patt |= FIELD_PREP(FMC2_PATT_ATTHIZ, timings->thiz);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200212 writel(patt, nfc->io_base + FMC2_PATT);
Christophe Kerelloda141682019-04-05 11:41:50 +0200213}
214
Christophe Kerellod1a25872020-07-31 09:53:37 +0200215static void stm32_fmc2_nfc_setup(struct nand_chip *chip)
Christophe Kerelloda141682019-04-05 11:41:50 +0200216{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200217 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerello9de081d2020-07-31 09:53:39 +0200218 u32 pcr = 0, pcr_mask;
Christophe Kerelloda141682019-04-05 11:41:50 +0200219
220 /* Configure ECC algorithm (default configuration is Hamming) */
Christophe Kerello9de081d2020-07-31 09:53:39 +0200221 pcr_mask = FMC2_PCR_ECCALG;
222 pcr_mask |= FMC2_PCR_BCHECC;
Christophe Kerelloda141682019-04-05 11:41:50 +0200223 if (chip->ecc.strength == FMC2_ECC_BCH8) {
224 pcr |= FMC2_PCR_ECCALG;
225 pcr |= FMC2_PCR_BCHECC;
226 } else if (chip->ecc.strength == FMC2_ECC_BCH4) {
227 pcr |= FMC2_PCR_ECCALG;
228 }
229
230 /* Set buswidth */
Christophe Kerello9de081d2020-07-31 09:53:39 +0200231 pcr_mask |= FMC2_PCR_PWID;
Christophe Kerelloda141682019-04-05 11:41:50 +0200232 if (chip->options & NAND_BUSWIDTH_16)
Christophe Kerellof4aca872020-07-31 09:53:38 +0200233 pcr |= FIELD_PREP(FMC2_PCR_PWID, FMC2_PCR_PWID_BUSWIDTH_16);
Christophe Kerelloda141682019-04-05 11:41:50 +0200234
235 /* Set ECC sector size */
Christophe Kerello9de081d2020-07-31 09:53:39 +0200236 pcr_mask |= FMC2_PCR_ECCSS;
Christophe Kerellof4aca872020-07-31 09:53:38 +0200237 pcr |= FIELD_PREP(FMC2_PCR_ECCSS, FMC2_PCR_ECCSS_512);
Christophe Kerelloda141682019-04-05 11:41:50 +0200238
Christophe Kerello9de081d2020-07-31 09:53:39 +0200239 clrsetbits_le32(nfc->io_base + FMC2_PCR, pcr_mask, pcr);
Christophe Kerelloda141682019-04-05 11:41:50 +0200240}
241
Christophe Kerellod1a25872020-07-31 09:53:37 +0200242static void stm32_fmc2_nfc_select_chip(struct mtd_info *mtd, int chipnr)
Christophe Kerelloda141682019-04-05 11:41:50 +0200243{
244 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200245 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200246 struct stm32_fmc2_nand *nand = to_fmc2_nand(chip);
247
248 if (chipnr < 0 || chipnr >= nand->ncs)
249 return;
250
Christophe Kerellod1a25872020-07-31 09:53:37 +0200251 if (nand->cs_used[chipnr] == nfc->cs_sel)
Christophe Kerelloda141682019-04-05 11:41:50 +0200252 return;
253
Christophe Kerellod1a25872020-07-31 09:53:37 +0200254 nfc->cs_sel = nand->cs_used[chipnr];
Christophe Kerello6276f862020-07-31 09:53:41 +0200255 chip->IO_ADDR_R = (void __iomem *)nfc->data_base[nfc->cs_sel];
256 chip->IO_ADDR_W = (void __iomem *)nfc->data_base[nfc->cs_sel];
Christophe Kerelloda141682019-04-05 11:41:50 +0200257
Christophe Kerellod1a25872020-07-31 09:53:37 +0200258 stm32_fmc2_nfc_setup(chip);
259 stm32_fmc2_nfc_timings_init(chip);
Christophe Kerelloda141682019-04-05 11:41:50 +0200260}
261
Christophe Kerellod1a25872020-07-31 09:53:37 +0200262static void stm32_fmc2_nfc_set_buswidth_16(struct stm32_fmc2_nfc *nfc,
263 bool set)
Christophe Kerelloda141682019-04-05 11:41:50 +0200264{
Christophe Kerello9de081d2020-07-31 09:53:39 +0200265 u32 pcr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200266
Christophe Kerello9de081d2020-07-31 09:53:39 +0200267 pcr = set ? FIELD_PREP(FMC2_PCR_PWID, FMC2_PCR_PWID_BUSWIDTH_16) :
268 FIELD_PREP(FMC2_PCR_PWID, FMC2_PCR_PWID_BUSWIDTH_8);
269
270 clrsetbits_le32(nfc->io_base + FMC2_PCR, FMC2_PCR_PWID, pcr);
Christophe Kerelloda141682019-04-05 11:41:50 +0200271}
272
Christophe Kerellod1a25872020-07-31 09:53:37 +0200273static void stm32_fmc2_nfc_set_ecc(struct stm32_fmc2_nfc *nfc, bool enable)
Christophe Kerelloda141682019-04-05 11:41:50 +0200274{
Christophe Kerello9de081d2020-07-31 09:53:39 +0200275 clrsetbits_le32(nfc->io_base + FMC2_PCR, FMC2_PCR_ECCEN,
276 enable ? FMC2_PCR_ECCEN : 0);
Christophe Kerelloda141682019-04-05 11:41:50 +0200277}
278
Christophe Kerellod1a25872020-07-31 09:53:37 +0200279static void stm32_fmc2_nfc_clear_bch_irq(struct stm32_fmc2_nfc *nfc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200280{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200281 writel(FMC2_BCHICR_CLEAR_IRQ, nfc->io_base + FMC2_BCHICR);
Christophe Kerelloda141682019-04-05 11:41:50 +0200282}
283
Christophe Kerellod1a25872020-07-31 09:53:37 +0200284static void stm32_fmc2_nfc_cmd_ctrl(struct mtd_info *mtd, int cmd,
285 unsigned int ctrl)
Christophe Kerelloda141682019-04-05 11:41:50 +0200286{
287 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200288 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200289
290 if (cmd == NAND_CMD_NONE)
291 return;
292
293 if (ctrl & NAND_CLE) {
Christophe Kerellod1a25872020-07-31 09:53:37 +0200294 writeb(cmd, nfc->cmd_base[nfc->cs_sel]);
Christophe Kerelloda141682019-04-05 11:41:50 +0200295 return;
296 }
297
Christophe Kerellod1a25872020-07-31 09:53:37 +0200298 writeb(cmd, nfc->addr_base[nfc->cs_sel]);
Christophe Kerelloda141682019-04-05 11:41:50 +0200299}
300
301/*
302 * Enable ECC logic and reset syndrome/parity bits previously calculated
303 * Syndrome/parity bits is cleared by setting the ECCEN bit to 0
304 */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200305static void stm32_fmc2_nfc_hwctl(struct mtd_info *mtd, int mode)
Christophe Kerelloda141682019-04-05 11:41:50 +0200306{
307 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200308 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200309
Christophe Kerellod1a25872020-07-31 09:53:37 +0200310 stm32_fmc2_nfc_set_ecc(nfc, false);
Christophe Kerelloda141682019-04-05 11:41:50 +0200311
312 if (chip->ecc.strength != FMC2_ECC_HAM) {
Christophe Kerello9de081d2020-07-31 09:53:39 +0200313 clrsetbits_le32(nfc->io_base + FMC2_PCR, FMC2_PCR_WEN,
314 mode == NAND_ECC_WRITE ? FMC2_PCR_WEN : 0);
Christophe Kerelloda141682019-04-05 11:41:50 +0200315
Christophe Kerellod1a25872020-07-31 09:53:37 +0200316 stm32_fmc2_nfc_clear_bch_irq(nfc);
Christophe Kerelloda141682019-04-05 11:41:50 +0200317 }
318
Christophe Kerellod1a25872020-07-31 09:53:37 +0200319 stm32_fmc2_nfc_set_ecc(nfc, true);
Christophe Kerelloda141682019-04-05 11:41:50 +0200320}
321
322/*
323 * ECC Hamming calculation
324 * ECC is 3 bytes for 512 bytes of data (supports error correction up to
325 * max of 1-bit)
326 */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200327static int stm32_fmc2_nfc_ham_calculate(struct mtd_info *mtd, const u8 *data,
328 u8 *ecc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200329{
330 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200331 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200332 u32 heccr, sr;
333 int ret;
334
Christophe Kerellod1a25872020-07-31 09:53:37 +0200335 ret = readl_poll_timeout(nfc->io_base + FMC2_SR, sr,
Christophe Kerello92693e32020-07-31 09:53:36 +0200336 sr & FMC2_SR_NWRF, FMC2_TIMEOUT_5S);
Christophe Kerelloda141682019-04-05 11:41:50 +0200337 if (ret < 0) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100338 log_err("Ham timeout\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200339 return ret;
340 }
341
Christophe Kerellod1a25872020-07-31 09:53:37 +0200342 heccr = readl(nfc->io_base + FMC2_HECCR);
Christophe Kerelloda141682019-04-05 11:41:50 +0200343
344 ecc[0] = heccr;
345 ecc[1] = heccr >> 8;
346 ecc[2] = heccr >> 16;
347
Christophe Kerellod1a25872020-07-31 09:53:37 +0200348 stm32_fmc2_nfc_set_ecc(nfc, false);
Christophe Kerelloda141682019-04-05 11:41:50 +0200349
350 return 0;
351}
352
Christophe Kerellod1a25872020-07-31 09:53:37 +0200353static int stm32_fmc2_nfc_ham_correct(struct mtd_info *mtd, u8 *dat,
354 u8 *read_ecc, u8 *calc_ecc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200355{
356 u8 bit_position = 0, b0, b1, b2;
357 u32 byte_addr = 0, b;
358 u32 i, shifting = 1;
359
360 /* Indicate which bit and byte is faulty (if any) */
361 b0 = read_ecc[0] ^ calc_ecc[0];
362 b1 = read_ecc[1] ^ calc_ecc[1];
363 b2 = read_ecc[2] ^ calc_ecc[2];
364 b = b0 | (b1 << 8) | (b2 << 16);
365
366 /* No errors */
367 if (likely(!b))
368 return 0;
369
370 /* Calculate bit position */
371 for (i = 0; i < 3; i++) {
372 switch (b % 4) {
373 case 2:
374 bit_position += shifting;
375 case 1:
376 break;
377 default:
378 return -EBADMSG;
379 }
380 shifting <<= 1;
381 b >>= 2;
382 }
383
384 /* Calculate byte position */
385 shifting = 1;
386 for (i = 0; i < 9; i++) {
387 switch (b % 4) {
388 case 2:
389 byte_addr += shifting;
390 case 1:
391 break;
392 default:
393 return -EBADMSG;
394 }
395 shifting <<= 1;
396 b >>= 2;
397 }
398
399 /* Flip the bit */
400 dat[byte_addr] ^= (1 << bit_position);
401
402 return 1;
403}
404
405/*
406 * ECC BCH calculation and correction
407 * ECC is 7/13 bytes for 512 bytes of data (supports error correction up to
408 * max of 4-bit/8-bit)
409 */
410
Christophe Kerellod1a25872020-07-31 09:53:37 +0200411static int stm32_fmc2_nfc_bch_calculate(struct mtd_info *mtd, const u8 *data,
412 u8 *ecc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200413{
414 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200415 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200416 u32 bchpbr, bchisr;
417 int ret;
418
419 /* Wait until the BCH code is ready */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200420 ret = readl_poll_timeout(nfc->io_base + FMC2_BCHISR, bchisr,
Christophe Kerello92693e32020-07-31 09:53:36 +0200421 bchisr & FMC2_BCHISR_EPBRF, FMC2_TIMEOUT_5S);
Christophe Kerelloda141682019-04-05 11:41:50 +0200422 if (ret < 0) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100423 log_err("Bch timeout\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200424 return ret;
425 }
426
427 /* Read parity bits */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200428 bchpbr = readl(nfc->io_base + FMC2_BCHPBR1);
Christophe Kerelloda141682019-04-05 11:41:50 +0200429 ecc[0] = bchpbr;
430 ecc[1] = bchpbr >> 8;
431 ecc[2] = bchpbr >> 16;
432 ecc[3] = bchpbr >> 24;
433
Christophe Kerellod1a25872020-07-31 09:53:37 +0200434 bchpbr = readl(nfc->io_base + FMC2_BCHPBR2);
Christophe Kerelloda141682019-04-05 11:41:50 +0200435 ecc[4] = bchpbr;
436 ecc[5] = bchpbr >> 8;
437 ecc[6] = bchpbr >> 16;
438
439 if (chip->ecc.strength == FMC2_ECC_BCH8) {
440 ecc[7] = bchpbr >> 24;
441
Christophe Kerellod1a25872020-07-31 09:53:37 +0200442 bchpbr = readl(nfc->io_base + FMC2_BCHPBR3);
Christophe Kerelloda141682019-04-05 11:41:50 +0200443 ecc[8] = bchpbr;
444 ecc[9] = bchpbr >> 8;
445 ecc[10] = bchpbr >> 16;
446 ecc[11] = bchpbr >> 24;
447
Christophe Kerellod1a25872020-07-31 09:53:37 +0200448 bchpbr = readl(nfc->io_base + FMC2_BCHPBR4);
Christophe Kerelloda141682019-04-05 11:41:50 +0200449 ecc[12] = bchpbr;
450 }
451
Christophe Kerellod1a25872020-07-31 09:53:37 +0200452 stm32_fmc2_nfc_set_ecc(nfc, false);
Christophe Kerelloda141682019-04-05 11:41:50 +0200453
454 return 0;
455}
456
Christophe Kerellod1a25872020-07-31 09:53:37 +0200457static int stm32_fmc2_nfc_bch_correct(struct mtd_info *mtd, u8 *dat,
458 u8 *read_ecc, u8 *calc_ecc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200459{
460 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200461 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200462 u32 bchdsr0, bchdsr1, bchdsr2, bchdsr3, bchdsr4, bchisr;
463 u16 pos[8];
464 int i, ret, den, eccsize = chip->ecc.size;
465 unsigned int nb_errs = 0;
466
467 /* Wait until the decoding error is ready */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200468 ret = readl_poll_timeout(nfc->io_base + FMC2_BCHISR, bchisr,
Christophe Kerello92693e32020-07-31 09:53:36 +0200469 bchisr & FMC2_BCHISR_DERF, FMC2_TIMEOUT_5S);
Christophe Kerelloda141682019-04-05 11:41:50 +0200470 if (ret < 0) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100471 log_err("Bch timeout\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200472 return ret;
473 }
474
Christophe Kerellod1a25872020-07-31 09:53:37 +0200475 bchdsr0 = readl(nfc->io_base + FMC2_BCHDSR0);
476 bchdsr1 = readl(nfc->io_base + FMC2_BCHDSR1);
477 bchdsr2 = readl(nfc->io_base + FMC2_BCHDSR2);
478 bchdsr3 = readl(nfc->io_base + FMC2_BCHDSR3);
479 bchdsr4 = readl(nfc->io_base + FMC2_BCHDSR4);
Christophe Kerelloda141682019-04-05 11:41:50 +0200480
Christophe Kerellod1a25872020-07-31 09:53:37 +0200481 stm32_fmc2_nfc_set_ecc(nfc, false);
Christophe Kerelloda141682019-04-05 11:41:50 +0200482
483 /* No errors found */
484 if (likely(!(bchdsr0 & FMC2_BCHDSR0_DEF)))
485 return 0;
486
487 /* Too many errors detected */
488 if (unlikely(bchdsr0 & FMC2_BCHDSR0_DUE))
489 return -EBADMSG;
490
Christophe Kerellof4aca872020-07-31 09:53:38 +0200491 pos[0] = FIELD_GET(FMC2_BCHDSR1_EBP1, bchdsr1);
492 pos[1] = FIELD_GET(FMC2_BCHDSR1_EBP2, bchdsr1);
493 pos[2] = FIELD_GET(FMC2_BCHDSR2_EBP3, bchdsr2);
494 pos[3] = FIELD_GET(FMC2_BCHDSR2_EBP4, bchdsr2);
495 pos[4] = FIELD_GET(FMC2_BCHDSR3_EBP5, bchdsr3);
496 pos[5] = FIELD_GET(FMC2_BCHDSR3_EBP6, bchdsr3);
497 pos[6] = FIELD_GET(FMC2_BCHDSR4_EBP7, bchdsr4);
498 pos[7] = FIELD_GET(FMC2_BCHDSR4_EBP8, bchdsr4);
Christophe Kerelloda141682019-04-05 11:41:50 +0200499
Christophe Kerellof4aca872020-07-31 09:53:38 +0200500 den = FIELD_GET(FMC2_BCHDSR0_DEN, bchdsr0);
Christophe Kerelloda141682019-04-05 11:41:50 +0200501 for (i = 0; i < den; i++) {
502 if (pos[i] < eccsize * 8) {
503 __change_bit(pos[i], (unsigned long *)dat);
504 nb_errs++;
505 }
506 }
507
508 return nb_errs;
509}
510
Christophe Kerellod1a25872020-07-31 09:53:37 +0200511static int stm32_fmc2_nfc_read_page(struct mtd_info *mtd,
512 struct nand_chip *chip, u8 *buf,
513 int oob_required, int page)
Christophe Kerelloda141682019-04-05 11:41:50 +0200514{
515 int i, s, stat, eccsize = chip->ecc.size;
516 int eccbytes = chip->ecc.bytes;
517 int eccsteps = chip->ecc.steps;
518 int eccstrength = chip->ecc.strength;
519 u8 *p = buf;
520 u8 *ecc_calc = chip->buffers->ecccalc;
521 u8 *ecc_code = chip->buffers->ecccode;
522 unsigned int max_bitflips = 0;
523
524 for (i = mtd->writesize + FMC2_BBM_LEN, s = 0; s < eccsteps;
525 s++, i += eccbytes, p += eccsize) {
526 chip->ecc.hwctl(mtd, NAND_ECC_READ);
527
528 /* Read the nand page sector (512 bytes) */
529 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, s * eccsize, -1);
530 chip->read_buf(mtd, p, eccsize);
531
532 /* Read the corresponding ECC bytes */
533 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, i, -1);
534 chip->read_buf(mtd, ecc_code, eccbytes);
535
536 /* Correct the data */
537 stat = chip->ecc.correct(mtd, p, ecc_code, ecc_calc);
538 if (stat == -EBADMSG)
539 /* Check for empty pages with bitflips */
540 stat = nand_check_erased_ecc_chunk(p, eccsize,
541 ecc_code, eccbytes,
542 NULL, 0,
543 eccstrength);
544
545 if (stat < 0) {
546 mtd->ecc_stats.failed++;
547 } else {
548 mtd->ecc_stats.corrected += stat;
549 max_bitflips = max_t(unsigned int, max_bitflips, stat);
550 }
551 }
552
553 /* Read oob */
554 if (oob_required) {
555 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
556 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
557 }
558
559 return max_bitflips;
560}
561
Christophe Kerello6276f862020-07-31 09:53:41 +0200562static void stm32_fmc2_nfc_init(struct stm32_fmc2_nfc *nfc, bool has_parent)
Christophe Kerelloda141682019-04-05 11:41:50 +0200563{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200564 u32 pcr = readl(nfc->io_base + FMC2_PCR);
Christophe Kerelloda141682019-04-05 11:41:50 +0200565
566 /* Set CS used to undefined */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200567 nfc->cs_sel = -1;
Christophe Kerelloda141682019-04-05 11:41:50 +0200568
569 /* Enable wait feature and nand flash memory bank */
570 pcr |= FMC2_PCR_PWAITEN;
571 pcr |= FMC2_PCR_PBKEN;
572
573 /* Set buswidth to 8 bits mode for identification */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200574 pcr &= ~FMC2_PCR_PWID;
Christophe Kerelloda141682019-04-05 11:41:50 +0200575
576 /* ECC logic is disabled */
577 pcr &= ~FMC2_PCR_ECCEN;
578
579 /* Default mode */
580 pcr &= ~FMC2_PCR_ECCALG;
581 pcr &= ~FMC2_PCR_BCHECC;
582 pcr &= ~FMC2_PCR_WEN;
583
584 /* Set default ECC sector size */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200585 pcr &= ~FMC2_PCR_ECCSS;
586 pcr |= FIELD_PREP(FMC2_PCR_ECCSS, FMC2_PCR_ECCSS_2048);
Christophe Kerelloda141682019-04-05 11:41:50 +0200587
588 /* Set default tclr/tar timings */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200589 pcr &= ~FMC2_PCR_TCLR;
590 pcr |= FIELD_PREP(FMC2_PCR_TCLR, FMC2_PCR_TCLR_DEFAULT);
591 pcr &= ~FMC2_PCR_TAR;
592 pcr |= FIELD_PREP(FMC2_PCR_TAR, FMC2_PCR_TAR_DEFAULT);
Christophe Kerelloda141682019-04-05 11:41:50 +0200593
594 /* Enable FMC2 controller */
Christophe Kerello6276f862020-07-31 09:53:41 +0200595 if (!has_parent)
596 setbits_le32(nfc->io_base + FMC2_BCR1, FMC2_BCR1_FMC2EN);
Christophe Kerelloda141682019-04-05 11:41:50 +0200597
Christophe Kerellod1a25872020-07-31 09:53:37 +0200598 writel(pcr, nfc->io_base + FMC2_PCR);
599 writel(FMC2_PMEM_DEFAULT, nfc->io_base + FMC2_PMEM);
600 writel(FMC2_PATT_DEFAULT, nfc->io_base + FMC2_PATT);
Christophe Kerelloda141682019-04-05 11:41:50 +0200601}
602
Christophe Kerellod1a25872020-07-31 09:53:37 +0200603static void stm32_fmc2_nfc_calc_timings(struct nand_chip *chip,
604 const struct nand_sdr_timings *sdrt)
Christophe Kerelloda141682019-04-05 11:41:50 +0200605{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200606 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200607 struct stm32_fmc2_nand *nand = to_fmc2_nand(chip);
608 struct stm32_fmc2_timings *tims = &nand->timings;
Christophe Kerellod1a25872020-07-31 09:53:37 +0200609 unsigned long hclk = clk_get_rate(&nfc->clk);
Igor Prusovc3421ea2023-11-09 20:10:04 +0300610 unsigned long hclkp = NSEC_PER_SEC / (hclk / 1000);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200611 unsigned long timing, tar, tclr, thiz, twait;
612 unsigned long tset_mem, tset_att, thold_mem, thold_att;
Christophe Kerelloda141682019-04-05 11:41:50 +0200613
Patrick Delaunay804858a2019-06-21 15:26:54 +0200614 tar = max_t(unsigned long, hclkp, sdrt->tAR_min);
615 timing = DIV_ROUND_UP(tar, hclkp) - 1;
616 tims->tar = min_t(unsigned long, timing, FMC2_PCR_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200617
Patrick Delaunay804858a2019-06-21 15:26:54 +0200618 tclr = max_t(unsigned long, hclkp, sdrt->tCLR_min);
619 timing = DIV_ROUND_UP(tclr, hclkp) - 1;
620 tims->tclr = min_t(unsigned long, timing, FMC2_PCR_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200621
622 tims->thiz = FMC2_THIZ;
623 thiz = (tims->thiz + 1) * hclkp;
624
625 /*
626 * tWAIT > tRP
627 * tWAIT > tWP
628 * tWAIT > tREA + tIO
629 */
Patrick Delaunay804858a2019-06-21 15:26:54 +0200630 twait = max_t(unsigned long, hclkp, sdrt->tRP_min);
631 twait = max_t(unsigned long, twait, sdrt->tWP_min);
632 twait = max_t(unsigned long, twait, sdrt->tREA_max + FMC2_TIO);
633 timing = DIV_ROUND_UP(twait, hclkp);
634 tims->twait = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200635
636 /*
637 * tSETUP_MEM > tCS - tWAIT
638 * tSETUP_MEM > tALS - tWAIT
639 * tSETUP_MEM > tDS - (tWAIT - tHIZ)
640 */
641 tset_mem = hclkp;
642 if (sdrt->tCS_min > twait && (tset_mem < sdrt->tCS_min - twait))
643 tset_mem = sdrt->tCS_min - twait;
644 if (sdrt->tALS_min > twait && (tset_mem < sdrt->tALS_min - twait))
645 tset_mem = sdrt->tALS_min - twait;
646 if (twait > thiz && (sdrt->tDS_min > twait - thiz) &&
647 (tset_mem < sdrt->tDS_min - (twait - thiz)))
648 tset_mem = sdrt->tDS_min - (twait - thiz);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200649 timing = DIV_ROUND_UP(tset_mem, hclkp);
650 tims->tset_mem = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200651
652 /*
653 * tHOLD_MEM > tCH
654 * tHOLD_MEM > tREH - tSETUP_MEM
655 * tHOLD_MEM > max(tRC, tWC) - (tSETUP_MEM + tWAIT)
656 */
Patrick Delaunay804858a2019-06-21 15:26:54 +0200657 thold_mem = max_t(unsigned long, hclkp, sdrt->tCH_min);
Christophe Kerelloda141682019-04-05 11:41:50 +0200658 if (sdrt->tREH_min > tset_mem &&
659 (thold_mem < sdrt->tREH_min - tset_mem))
660 thold_mem = sdrt->tREH_min - tset_mem;
661 if ((sdrt->tRC_min > tset_mem + twait) &&
662 (thold_mem < sdrt->tRC_min - (tset_mem + twait)))
663 thold_mem = sdrt->tRC_min - (tset_mem + twait);
664 if ((sdrt->tWC_min > tset_mem + twait) &&
665 (thold_mem < sdrt->tWC_min - (tset_mem + twait)))
666 thold_mem = sdrt->tWC_min - (tset_mem + twait);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200667 timing = DIV_ROUND_UP(thold_mem, hclkp);
668 tims->thold_mem = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200669
670 /*
671 * tSETUP_ATT > tCS - tWAIT
672 * tSETUP_ATT > tCLS - tWAIT
673 * tSETUP_ATT > tALS - tWAIT
674 * tSETUP_ATT > tRHW - tHOLD_MEM
675 * tSETUP_ATT > tDS - (tWAIT - tHIZ)
676 */
677 tset_att = hclkp;
678 if (sdrt->tCS_min > twait && (tset_att < sdrt->tCS_min - twait))
679 tset_att = sdrt->tCS_min - twait;
680 if (sdrt->tCLS_min > twait && (tset_att < sdrt->tCLS_min - twait))
681 tset_att = sdrt->tCLS_min - twait;
682 if (sdrt->tALS_min > twait && (tset_att < sdrt->tALS_min - twait))
683 tset_att = sdrt->tALS_min - twait;
684 if (sdrt->tRHW_min > thold_mem &&
685 (tset_att < sdrt->tRHW_min - thold_mem))
686 tset_att = sdrt->tRHW_min - thold_mem;
687 if (twait > thiz && (sdrt->tDS_min > twait - thiz) &&
688 (tset_att < sdrt->tDS_min - (twait - thiz)))
689 tset_att = sdrt->tDS_min - (twait - thiz);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200690 timing = DIV_ROUND_UP(tset_att, hclkp);
691 tims->tset_att = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200692
693 /*
694 * tHOLD_ATT > tALH
695 * tHOLD_ATT > tCH
696 * tHOLD_ATT > tCLH
697 * tHOLD_ATT > tCOH
698 * tHOLD_ATT > tDH
699 * tHOLD_ATT > tWB + tIO + tSYNC - tSETUP_MEM
700 * tHOLD_ATT > tADL - tSETUP_MEM
701 * tHOLD_ATT > tWH - tSETUP_MEM
702 * tHOLD_ATT > tWHR - tSETUP_MEM
703 * tHOLD_ATT > tRC - (tSETUP_ATT + tWAIT)
704 * tHOLD_ATT > tWC - (tSETUP_ATT + tWAIT)
705 */
Patrick Delaunay804858a2019-06-21 15:26:54 +0200706 thold_att = max_t(unsigned long, hclkp, sdrt->tALH_min);
707 thold_att = max_t(unsigned long, thold_att, sdrt->tCH_min);
708 thold_att = max_t(unsigned long, thold_att, sdrt->tCLH_min);
709 thold_att = max_t(unsigned long, thold_att, sdrt->tCOH_min);
710 thold_att = max_t(unsigned long, thold_att, sdrt->tDH_min);
Christophe Kerelloda141682019-04-05 11:41:50 +0200711 if ((sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC > tset_mem) &&
712 (thold_att < sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC - tset_mem))
713 thold_att = sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC - tset_mem;
714 if (sdrt->tADL_min > tset_mem &&
715 (thold_att < sdrt->tADL_min - tset_mem))
716 thold_att = sdrt->tADL_min - tset_mem;
717 if (sdrt->tWH_min > tset_mem &&
718 (thold_att < sdrt->tWH_min - tset_mem))
719 thold_att = sdrt->tWH_min - tset_mem;
720 if (sdrt->tWHR_min > tset_mem &&
721 (thold_att < sdrt->tWHR_min - tset_mem))
722 thold_att = sdrt->tWHR_min - tset_mem;
723 if ((sdrt->tRC_min > tset_att + twait) &&
724 (thold_att < sdrt->tRC_min - (tset_att + twait)))
725 thold_att = sdrt->tRC_min - (tset_att + twait);
726 if ((sdrt->tWC_min > tset_att + twait) &&
727 (thold_att < sdrt->tWC_min - (tset_att + twait)))
728 thold_att = sdrt->tWC_min - (tset_att + twait);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200729 timing = DIV_ROUND_UP(thold_att, hclkp);
730 tims->thold_att = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200731}
732
Christophe Kerellod1a25872020-07-31 09:53:37 +0200733static int stm32_fmc2_nfc_setup_interface(struct mtd_info *mtd, int chipnr,
734 const struct nand_data_interface *cf)
Christophe Kerelloda141682019-04-05 11:41:50 +0200735{
736 struct nand_chip *chip = mtd_to_nand(mtd);
737 const struct nand_sdr_timings *sdrt;
738
Christophe Kerellod1a25872020-07-31 09:53:37 +0200739 sdrt = nand_get_sdr_timings(cf);
Christophe Kerelloda141682019-04-05 11:41:50 +0200740 if (IS_ERR(sdrt))
741 return PTR_ERR(sdrt);
742
Christophe Kerello1b4662a2023-03-30 11:16:21 +0200743 if (sdrt->tRC_min < 30000)
744 return -EOPNOTSUPP;
745
Christophe Kerelloda141682019-04-05 11:41:50 +0200746 if (chipnr == NAND_DATA_IFACE_CHECK_ONLY)
747 return 0;
748
Christophe Kerellod1a25872020-07-31 09:53:37 +0200749 stm32_fmc2_nfc_calc_timings(chip, sdrt);
750 stm32_fmc2_nfc_timings_init(chip);
Christophe Kerelloda141682019-04-05 11:41:50 +0200751
752 return 0;
753}
754
Christophe Kerellod1a25872020-07-31 09:53:37 +0200755static void stm32_fmc2_nfc_nand_callbacks_setup(struct nand_chip *chip)
Christophe Kerelloda141682019-04-05 11:41:50 +0200756{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200757 chip->ecc.hwctl = stm32_fmc2_nfc_hwctl;
Christophe Kerelloda141682019-04-05 11:41:50 +0200758
759 /*
760 * Specific callbacks to read/write a page depending on
761 * the algo used (Hamming, BCH).
762 */
763 if (chip->ecc.strength == FMC2_ECC_HAM) {
764 /* Hamming is used */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200765 chip->ecc.calculate = stm32_fmc2_nfc_ham_calculate;
766 chip->ecc.correct = stm32_fmc2_nfc_ham_correct;
Christophe Kerelloda141682019-04-05 11:41:50 +0200767 chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 4 : 3;
768 chip->ecc.options |= NAND_ECC_GENERIC_ERASED_CHECK;
769 return;
770 }
771
772 /* BCH is used */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200773 chip->ecc.read_page = stm32_fmc2_nfc_read_page;
774 chip->ecc.calculate = stm32_fmc2_nfc_bch_calculate;
775 chip->ecc.correct = stm32_fmc2_nfc_bch_correct;
Christophe Kerelloda141682019-04-05 11:41:50 +0200776
777 if (chip->ecc.strength == FMC2_ECC_BCH8)
778 chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 14 : 13;
779 else
780 chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 8 : 7;
781}
782
Christophe Kerellod1a25872020-07-31 09:53:37 +0200783static int stm32_fmc2_nfc_calc_ecc_bytes(int step_size, int strength)
Christophe Kerelloda141682019-04-05 11:41:50 +0200784{
785 /* Hamming */
786 if (strength == FMC2_ECC_HAM)
787 return 4;
788
789 /* BCH8 */
790 if (strength == FMC2_ECC_BCH8)
791 return 14;
792
793 /* BCH4 */
794 return 8;
795}
796
Christophe Kerellod1a25872020-07-31 09:53:37 +0200797NAND_ECC_CAPS_SINGLE(stm32_fmc2_nfc_ecc_caps, stm32_fmc2_nfc_calc_ecc_bytes,
Christophe Kerelloda141682019-04-05 11:41:50 +0200798 FMC2_ECC_STEP_SIZE,
799 FMC2_ECC_HAM, FMC2_ECC_BCH4, FMC2_ECC_BCH8);
800
Christophe Kerellod1a25872020-07-31 09:53:37 +0200801static int stm32_fmc2_nfc_parse_child(struct stm32_fmc2_nfc *nfc, ofnode node)
Christophe Kerelloda141682019-04-05 11:41:50 +0200802{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200803 struct stm32_fmc2_nand *nand = &nfc->nand;
Christophe Kerelloda141682019-04-05 11:41:50 +0200804 u32 cs[FMC2_MAX_CE];
805 int ret, i;
806
807 if (!ofnode_get_property(node, "reg", &nand->ncs))
808 return -EINVAL;
809
810 nand->ncs /= sizeof(u32);
811 if (!nand->ncs) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100812 log_err("Invalid reg property size\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200813 return -EINVAL;
814 }
815
816 ret = ofnode_read_u32_array(node, "reg", cs, nand->ncs);
817 if (ret < 0) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100818 log_err("Could not retrieve reg property\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200819 return -EINVAL;
820 }
821
822 for (i = 0; i < nand->ncs; i++) {
Christophe Kerello7ea25d62024-03-06 10:54:06 +0100823 if (cs[i] >= nfc->data->max_ncs) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100824 log_err("Invalid reg value: %d\n", nand->cs_used[i]);
Christophe Kerelloda141682019-04-05 11:41:50 +0200825 return -EINVAL;
826 }
827
Christophe Kerellod1a25872020-07-31 09:53:37 +0200828 if (nfc->cs_assigned & BIT(cs[i])) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100829 log_err("Cs already assigned: %d\n", nand->cs_used[i]);
Christophe Kerelloda141682019-04-05 11:41:50 +0200830 return -EINVAL;
831 }
832
Christophe Kerellod1a25872020-07-31 09:53:37 +0200833 nfc->cs_assigned |= BIT(cs[i]);
Christophe Kerelloda141682019-04-05 11:41:50 +0200834 nand->cs_used[i] = cs[i];
835 }
836
Christophe Kerelloe389a152022-02-22 17:38:49 +0100837 gpio_request_by_name_nodev(node, "wp-gpios", 0, &nand->wp_gpio,
838 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
839
Patrice Chotard33d2cf92021-09-13 16:25:53 +0200840 nand->chip.flash_node = node;
Christophe Kerelloda141682019-04-05 11:41:50 +0200841
842 return 0;
843}
844
Christophe Kerellod1a25872020-07-31 09:53:37 +0200845static int stm32_fmc2_nfc_parse_dt(struct udevice *dev,
846 struct stm32_fmc2_nfc *nfc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200847{
848 ofnode child;
849 int ret, nchips = 0;
850
851 dev_for_each_subnode(child, dev)
852 nchips++;
853
854 if (!nchips) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100855 log_err("NAND chip not defined\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200856 return -EINVAL;
857 }
858
859 if (nchips > 1) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100860 log_err("Too many NAND chips defined\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200861 return -EINVAL;
862 }
863
864 dev_for_each_subnode(child, dev) {
Christophe Kerellod1a25872020-07-31 09:53:37 +0200865 ret = stm32_fmc2_nfc_parse_child(nfc, child);
Christophe Kerelloda141682019-04-05 11:41:50 +0200866 if (ret)
867 return ret;
868 }
869
870 return 0;
871}
872
Christophe Kerello6276f862020-07-31 09:53:41 +0200873static struct udevice *stm32_fmc2_nfc_get_cdev(struct udevice *dev)
874{
875 struct udevice *pdev = dev_get_parent(dev);
876 struct udevice *cdev = NULL;
877 bool ebi_found = false;
878
879 if (pdev && ofnode_device_is_compatible(dev_ofnode(pdev),
880 "st,stm32mp1-fmc2-ebi"))
881 ebi_found = true;
882
883 if (ofnode_device_is_compatible(dev_ofnode(dev),
884 "st,stm32mp1-fmc2-nfc")) {
885 if (ebi_found)
886 cdev = pdev;
887
888 return cdev;
889 }
890
891 if (!ebi_found)
892 cdev = dev;
893
894 return cdev;
895}
896
Christophe Kerellod1a25872020-07-31 09:53:37 +0200897static int stm32_fmc2_nfc_probe(struct udevice *dev)
Christophe Kerelloda141682019-04-05 11:41:50 +0200898{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200899 struct stm32_fmc2_nfc *nfc = dev_get_priv(dev);
900 struct stm32_fmc2_nand *nand = &nfc->nand;
Christophe Kerelloda141682019-04-05 11:41:50 +0200901 struct nand_chip *chip = &nand->chip;
902 struct mtd_info *mtd = &chip->mtd;
903 struct nand_ecclayout *ecclayout;
Christophe Kerello6276f862020-07-31 09:53:41 +0200904 struct udevice *cdev;
Christophe Kerelloda141682019-04-05 11:41:50 +0200905 struct reset_ctl reset;
Patrick Delaunay804858a2019-06-21 15:26:54 +0200906 int oob_index, chip_cs, mem_region, ret;
907 unsigned int i;
Christophe Kerello6276f862020-07-31 09:53:41 +0200908 int start_region = 0;
909 fdt_addr_t addr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200910
Christophe Kerellod1a25872020-07-31 09:53:37 +0200911 spin_lock_init(&nfc->controller.lock);
912 init_waitqueue_head(&nfc->controller.wq);
Christophe Kerelloda141682019-04-05 11:41:50 +0200913
Christophe Kerello7ea25d62024-03-06 10:54:06 +0100914 nfc->data = (void *)dev_get_driver_data(dev);
915 if (!nfc->data)
Christophe Kerello6276f862020-07-31 09:53:41 +0200916 return -EINVAL;
917
Christophe Kerello7ea25d62024-03-06 10:54:06 +0100918 if (nfc->data->get_cdev) {
919 cdev = nfc->data->get_cdev(dev);
920 if (!cdev)
921 return -EINVAL;
922 } else {
923 cdev = dev->parent;
924 }
925
Christophe Kerellod1a25872020-07-31 09:53:37 +0200926 ret = stm32_fmc2_nfc_parse_dt(dev, nfc);
Christophe Kerelloda141682019-04-05 11:41:50 +0200927 if (ret)
928 return ret;
929
Christophe Kerello6276f862020-07-31 09:53:41 +0200930 nfc->io_base = dev_read_addr(cdev);
931 if (nfc->io_base == FDT_ADDR_T_NONE)
932 return -EINVAL;
933
934 if (dev == cdev)
935 start_region = 1;
Christophe Kerelloda141682019-04-05 11:41:50 +0200936
Christophe Kerello7ea25d62024-03-06 10:54:06 +0100937 for (chip_cs = 0, mem_region = start_region; chip_cs < nfc->data->max_ncs;
Christophe Kerelloda141682019-04-05 11:41:50 +0200938 chip_cs++, mem_region += 3) {
Christophe Kerellod1a25872020-07-31 09:53:37 +0200939 if (!(nfc->cs_assigned & BIT(chip_cs)))
Christophe Kerelloda141682019-04-05 11:41:50 +0200940 continue;
941
Christophe Kerello6276f862020-07-31 09:53:41 +0200942 addr = dev_read_addr_index(dev, mem_region);
943 if (addr == FDT_ADDR_T_NONE) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100944 dev_err(dev, "Resource data_base not found for cs%d", chip_cs);
Christophe Kerelloda141682019-04-05 11:41:50 +0200945 return ret;
946 }
Christophe Kerello6276f862020-07-31 09:53:41 +0200947 nfc->data_base[chip_cs] = addr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200948
Christophe Kerello6276f862020-07-31 09:53:41 +0200949 addr = dev_read_addr_index(dev, mem_region + 1);
950 if (addr == FDT_ADDR_T_NONE) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100951 dev_err(dev, "Resource cmd_base not found for cs%d", chip_cs);
Christophe Kerelloda141682019-04-05 11:41:50 +0200952 return ret;
953 }
Christophe Kerello6276f862020-07-31 09:53:41 +0200954 nfc->cmd_base[chip_cs] = addr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200955
Christophe Kerello6276f862020-07-31 09:53:41 +0200956 addr = dev_read_addr_index(dev, mem_region + 2);
957 if (addr == FDT_ADDR_T_NONE) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100958 dev_err(dev, "Resource addr_base not found for cs%d", chip_cs);
Christophe Kerelloda141682019-04-05 11:41:50 +0200959 return ret;
960 }
Christophe Kerello6276f862020-07-31 09:53:41 +0200961 nfc->addr_base[chip_cs] = addr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200962 }
963
964 /* Enable the clock */
Christophe Kerello6276f862020-07-31 09:53:41 +0200965 ret = clk_get_by_index(cdev, 0, &nfc->clk);
Christophe Kerelloda141682019-04-05 11:41:50 +0200966 if (ret)
967 return ret;
968
Christophe Kerellod1a25872020-07-31 09:53:37 +0200969 ret = clk_enable(&nfc->clk);
Christophe Kerelloda141682019-04-05 11:41:50 +0200970 if (ret)
971 return ret;
972
973 /* Reset */
974 ret = reset_get_by_index(dev, 0, &reset);
975 if (!ret) {
976 reset_assert(&reset);
977 udelay(2);
978 reset_deassert(&reset);
979 }
980
Christophe Kerello6276f862020-07-31 09:53:41 +0200981 stm32_fmc2_nfc_init(nfc, dev != cdev);
Christophe Kerelloda141682019-04-05 11:41:50 +0200982
Christophe Kerellod1a25872020-07-31 09:53:37 +0200983 chip->controller = &nfc->base;
984 chip->select_chip = stm32_fmc2_nfc_select_chip;
985 chip->setup_data_interface = stm32_fmc2_nfc_setup_interface;
986 chip->cmd_ctrl = stm32_fmc2_nfc_cmd_ctrl;
Christophe Kerelloda141682019-04-05 11:41:50 +0200987 chip->chip_delay = FMC2_RB_DELAY_US;
988 chip->options |= NAND_BUSWIDTH_AUTO | NAND_NO_SUBPAGE_WRITE |
989 NAND_USE_BOUNCE_BUFFER;
990
991 /* Default ECC settings */
992 chip->ecc.mode = NAND_ECC_HW;
993 chip->ecc.size = FMC2_ECC_STEP_SIZE;
994 chip->ecc.strength = FMC2_ECC_BCH8;
995
Christophe Kerelloe389a152022-02-22 17:38:49 +0100996 /* Disable Write Protect */
997 if (dm_gpio_is_valid(&nand->wp_gpio))
998 dm_gpio_set_value(&nand->wp_gpio, 0);
999
Christophe Kerelloda141682019-04-05 11:41:50 +02001000 ret = nand_scan_ident(mtd, nand->ncs, NULL);
1001 if (ret)
1002 return ret;
1003
1004 /*
1005 * Only NAND_ECC_HW mode is actually supported
1006 * Hamming => ecc.strength = 1
1007 * BCH4 => ecc.strength = 4
1008 * BCH8 => ecc.strength = 8
1009 * ECC sector size = 512
1010 */
1011 if (chip->ecc.mode != NAND_ECC_HW) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +01001012 dev_err(dev, "Nand_ecc_mode is not well defined in the DT\n");
Christophe Kerelloda141682019-04-05 11:41:50 +02001013 return -EINVAL;
1014 }
1015
Christophe Kerellod1a25872020-07-31 09:53:37 +02001016 ret = nand_check_ecc_caps(chip, &stm32_fmc2_nfc_ecc_caps,
Christophe Kerelloda141682019-04-05 11:41:50 +02001017 mtd->oobsize - FMC2_BBM_LEN);
1018 if (ret) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +01001019 dev_err(dev, "No valid ECC settings set\n");
Christophe Kerelloda141682019-04-05 11:41:50 +02001020 return ret;
1021 }
1022
1023 if (chip->bbt_options & NAND_BBT_USE_FLASH)
1024 chip->bbt_options |= NAND_BBT_NO_OOB;
1025
Christophe Kerellod1a25872020-07-31 09:53:37 +02001026 stm32_fmc2_nfc_nand_callbacks_setup(chip);
Christophe Kerelloda141682019-04-05 11:41:50 +02001027
1028 /* Define ECC layout */
Christophe Kerellod1a25872020-07-31 09:53:37 +02001029 ecclayout = &nfc->ecclayout;
Christophe Kerelloda141682019-04-05 11:41:50 +02001030 ecclayout->eccbytes = chip->ecc.bytes *
1031 (mtd->writesize / chip->ecc.size);
1032 oob_index = FMC2_BBM_LEN;
1033 for (i = 0; i < ecclayout->eccbytes; i++, oob_index++)
1034 ecclayout->eccpos[i] = oob_index;
1035 ecclayout->oobfree->offset = oob_index;
1036 ecclayout->oobfree->length = mtd->oobsize - ecclayout->oobfree->offset;
1037 chip->ecc.layout = ecclayout;
1038
Christophe Kerelloda141682019-04-05 11:41:50 +02001039 if (chip->options & NAND_BUSWIDTH_16)
Christophe Kerellod1a25872020-07-31 09:53:37 +02001040 stm32_fmc2_nfc_set_buswidth_16(nfc, true);
Christophe Kerelloda141682019-04-05 11:41:50 +02001041
Christophe Kerelloda141682019-04-05 11:41:50 +02001042 ret = nand_scan_tail(mtd);
1043 if (ret)
1044 return ret;
1045
1046 return nand_register(0, mtd);
1047}
1048
Christophe Kerello7ea25d62024-03-06 10:54:06 +01001049static const struct stm32_fmc2_nfc_data stm32_fmc2_nfc_mp1_data = {
1050 .max_ncs = 2,
1051 .get_cdev = stm32_fmc2_nfc_get_cdev,
1052};
1053
1054static const struct stm32_fmc2_nfc_data stm32_fmc2_nfc_mp25_data = {
1055 .max_ncs = 4,
1056};
1057
Christophe Kerellod1a25872020-07-31 09:53:37 +02001058static const struct udevice_id stm32_fmc2_nfc_match[] = {
Christophe Kerello7ea25d62024-03-06 10:54:06 +01001059 {
1060 .compatible = "st,stm32mp15-fmc2",
1061 .data = (ulong)&stm32_fmc2_nfc_mp1_data,
1062 },
1063 {
1064 .compatible = "st,stm32mp1-fmc2-nfc",
1065 .data = (ulong)&stm32_fmc2_nfc_mp1_data,
1066 },
1067 {
1068 .compatible = "st,stm32mp25-fmc2-nfc",
1069 .data = (ulong)&stm32_fmc2_nfc_mp25_data,
1070 },
Christophe Kerelloda141682019-04-05 11:41:50 +02001071 { /* Sentinel */ }
1072};
1073
Christophe Kerellod1a25872020-07-31 09:53:37 +02001074U_BOOT_DRIVER(stm32_fmc2_nfc) = {
1075 .name = "stm32_fmc2_nfc",
Christophe Kerelloda141682019-04-05 11:41:50 +02001076 .id = UCLASS_MTD,
Christophe Kerellod1a25872020-07-31 09:53:37 +02001077 .of_match = stm32_fmc2_nfc_match,
1078 .probe = stm32_fmc2_nfc_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001079 .priv_auto = sizeof(struct stm32_fmc2_nfc),
Christophe Kerelloda141682019-04-05 11:41:50 +02001080};
1081
1082void board_nand_init(void)
1083{
1084 struct udevice *dev;
1085 int ret;
1086
1087 ret = uclass_get_device_by_driver(UCLASS_MTD,
Simon Glass65130cd2020-12-28 20:34:56 -07001088 DM_DRIVER_GET(stm32_fmc2_nfc),
Christophe Kerelloda141682019-04-05 11:41:50 +02001089 &dev);
1090 if (ret && ret != -ENODEV)
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +01001091 log_err("Failed to initialize STM32 FMC2 NFC controller. (error %d)\n",
1092 ret);
Christophe Kerelloda141682019-04-05 11:41:50 +02001093}