blob: 69dbb629e9319feb55adb0a0c47e186250f87f72 [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>
Christophe Kerelloda141682019-04-05 11:41:50 +020024
25/* Bad block marker length */
26#define FMC2_BBM_LEN 2
27
28/* ECC step size */
29#define FMC2_ECC_STEP_SIZE 512
30
31/* Command delay */
32#define FMC2_RB_DELAY_US 30
33
34/* Max chip enable */
35#define FMC2_MAX_CE 2
36
37/* Timings */
38#define FMC2_THIZ 1
39#define FMC2_TIO 8000
40#define FMC2_TSYNC 3000
41#define FMC2_PCR_TIMING_MASK 0xf
42#define FMC2_PMEM_PATT_TIMING_MASK 0xff
43
44/* FMC2 Controller Registers */
45#define FMC2_BCR1 0x0
46#define FMC2_PCR 0x80
47#define FMC2_SR 0x84
48#define FMC2_PMEM 0x88
49#define FMC2_PATT 0x8c
50#define FMC2_HECCR 0x94
51#define FMC2_BCHISR 0x254
52#define FMC2_BCHICR 0x258
53#define FMC2_BCHPBR1 0x260
54#define FMC2_BCHPBR2 0x264
55#define FMC2_BCHPBR3 0x268
56#define FMC2_BCHPBR4 0x26c
57#define FMC2_BCHDSR0 0x27c
58#define FMC2_BCHDSR1 0x280
59#define FMC2_BCHDSR2 0x284
60#define FMC2_BCHDSR3 0x288
61#define FMC2_BCHDSR4 0x28c
62
63/* Register: FMC2_BCR1 */
64#define FMC2_BCR1_FMC2EN BIT(31)
65
66/* Register: FMC2_PCR */
67#define FMC2_PCR_PWAITEN BIT(1)
68#define FMC2_PCR_PBKEN BIT(2)
Christophe Kerellof4aca872020-07-31 09:53:38 +020069#define FMC2_PCR_PWID GENMASK(5, 4)
Christophe Kerelloda141682019-04-05 11:41:50 +020070#define FMC2_PCR_PWID_BUSWIDTH_8 0
71#define FMC2_PCR_PWID_BUSWIDTH_16 1
72#define FMC2_PCR_ECCEN BIT(6)
73#define FMC2_PCR_ECCALG BIT(8)
Christophe Kerellof4aca872020-07-31 09:53:38 +020074#define FMC2_PCR_TCLR GENMASK(12, 9)
Christophe Kerelloda141682019-04-05 11:41:50 +020075#define FMC2_PCR_TCLR_DEFAULT 0xf
Christophe Kerellof4aca872020-07-31 09:53:38 +020076#define FMC2_PCR_TAR GENMASK(16, 13)
Christophe Kerelloda141682019-04-05 11:41:50 +020077#define FMC2_PCR_TAR_DEFAULT 0xf
Christophe Kerellof4aca872020-07-31 09:53:38 +020078#define FMC2_PCR_ECCSS GENMASK(19, 17)
Christophe Kerelloda141682019-04-05 11:41:50 +020079#define FMC2_PCR_ECCSS_512 1
80#define FMC2_PCR_ECCSS_2048 3
81#define FMC2_PCR_BCHECC BIT(24)
82#define FMC2_PCR_WEN BIT(25)
83
84/* Register: FMC2_SR */
85#define FMC2_SR_NWRF BIT(6)
86
87/* Register: FMC2_PMEM */
Christophe Kerellof4aca872020-07-31 09:53:38 +020088#define FMC2_PMEM_MEMSET GENMASK(7, 0)
89#define FMC2_PMEM_MEMWAIT GENMASK(15, 8)
90#define FMC2_PMEM_MEMHOLD GENMASK(23, 16)
91#define FMC2_PMEM_MEMHIZ GENMASK(31, 24)
Christophe Kerelloda141682019-04-05 11:41:50 +020092#define FMC2_PMEM_DEFAULT 0x0a0a0a0a
93
94/* Register: FMC2_PATT */
Christophe Kerellof4aca872020-07-31 09:53:38 +020095#define FMC2_PATT_ATTSET GENMASK(7, 0)
96#define FMC2_PATT_ATTWAIT GENMASK(15, 8)
97#define FMC2_PATT_ATTHOLD GENMASK(23, 16)
98#define FMC2_PATT_ATTHIZ GENMASK(31, 24)
Christophe Kerelloda141682019-04-05 11:41:50 +020099#define FMC2_PATT_DEFAULT 0x0a0a0a0a
100
101/* Register: FMC2_BCHISR */
102#define FMC2_BCHISR_DERF BIT(1)
103#define FMC2_BCHISR_EPBRF BIT(4)
104
105/* Register: FMC2_BCHICR */
106#define FMC2_BCHICR_CLEAR_IRQ GENMASK(4, 0)
107
108/* Register: FMC2_BCHDSR0 */
109#define FMC2_BCHDSR0_DUE BIT(0)
110#define FMC2_BCHDSR0_DEF BIT(1)
Christophe Kerellof4aca872020-07-31 09:53:38 +0200111#define FMC2_BCHDSR0_DEN GENMASK(7, 4)
Christophe Kerelloda141682019-04-05 11:41:50 +0200112
113/* Register: FMC2_BCHDSR1 */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200114#define FMC2_BCHDSR1_EBP1 GENMASK(12, 0)
115#define FMC2_BCHDSR1_EBP2 GENMASK(28, 16)
Christophe Kerelloda141682019-04-05 11:41:50 +0200116
117/* Register: FMC2_BCHDSR2 */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200118#define FMC2_BCHDSR2_EBP3 GENMASK(12, 0)
119#define FMC2_BCHDSR2_EBP4 GENMASK(28, 16)
Christophe Kerelloda141682019-04-05 11:41:50 +0200120
121/* Register: FMC2_BCHDSR3 */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200122#define FMC2_BCHDSR3_EBP5 GENMASK(12, 0)
123#define FMC2_BCHDSR3_EBP6 GENMASK(28, 16)
Christophe Kerelloda141682019-04-05 11:41:50 +0200124
125/* Register: FMC2_BCHDSR4 */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200126#define FMC2_BCHDSR4_EBP7 GENMASK(12, 0)
127#define FMC2_BCHDSR4_EBP8 GENMASK(28, 16)
Christophe Kerelloda141682019-04-05 11:41:50 +0200128
129#define FMC2_NSEC_PER_SEC 1000000000L
130
Christophe Kerello92693e32020-07-31 09:53:36 +0200131#define FMC2_TIMEOUT_5S 5000000
132
Christophe Kerelloda141682019-04-05 11:41:50 +0200133enum stm32_fmc2_ecc {
134 FMC2_ECC_HAM = 1,
135 FMC2_ECC_BCH4 = 4,
136 FMC2_ECC_BCH8 = 8
137};
138
139struct stm32_fmc2_timings {
140 u8 tclr;
141 u8 tar;
142 u8 thiz;
143 u8 twait;
144 u8 thold_mem;
145 u8 tset_mem;
146 u8 thold_att;
147 u8 tset_att;
148};
149
150struct stm32_fmc2_nand {
151 struct nand_chip chip;
152 struct stm32_fmc2_timings timings;
Christophe Kerelloe389a152022-02-22 17:38:49 +0100153 struct gpio_desc wp_gpio;
Christophe Kerelloda141682019-04-05 11:41:50 +0200154 int ncs;
155 int cs_used[FMC2_MAX_CE];
156};
157
158static inline struct stm32_fmc2_nand *to_fmc2_nand(struct nand_chip *chip)
159{
160 return container_of(chip, struct stm32_fmc2_nand, chip);
161}
162
163struct stm32_fmc2_nfc {
164 struct nand_hw_control base;
165 struct stm32_fmc2_nand nand;
166 struct nand_ecclayout ecclayout;
Christophe Kerello6276f862020-07-31 09:53:41 +0200167 fdt_addr_t io_base;
168 fdt_addr_t data_base[FMC2_MAX_CE];
169 fdt_addr_t cmd_base[FMC2_MAX_CE];
170 fdt_addr_t addr_base[FMC2_MAX_CE];
Christophe Kerelloda141682019-04-05 11:41:50 +0200171 struct clk clk;
172
173 u8 cs_assigned;
174 int cs_sel;
175};
176
177static inline struct stm32_fmc2_nfc *to_stm32_nfc(struct nand_hw_control *base)
178{
179 return container_of(base, struct stm32_fmc2_nfc, base);
180}
181
Christophe Kerellod1a25872020-07-31 09:53:37 +0200182static void stm32_fmc2_nfc_timings_init(struct nand_chip *chip)
Christophe Kerelloda141682019-04-05 11:41:50 +0200183{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200184 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200185 struct stm32_fmc2_nand *nand = to_fmc2_nand(chip);
186 struct stm32_fmc2_timings *timings = &nand->timings;
Christophe Kerelloda141682019-04-05 11:41:50 +0200187 u32 pmem, patt;
188
189 /* Set tclr/tar timings */
Christophe Kerello9de081d2020-07-31 09:53:39 +0200190 clrsetbits_le32(nfc->io_base + FMC2_PCR,
191 FMC2_PCR_TCLR | FMC2_PCR_TAR,
192 FIELD_PREP(FMC2_PCR_TCLR, timings->tclr) |
193 FIELD_PREP(FMC2_PCR_TAR, timings->tar));
Christophe Kerelloda141682019-04-05 11:41:50 +0200194
195 /* Set tset/twait/thold/thiz timings in common bank */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200196 pmem = FIELD_PREP(FMC2_PMEM_MEMSET, timings->tset_mem);
197 pmem |= FIELD_PREP(FMC2_PMEM_MEMWAIT, timings->twait);
198 pmem |= FIELD_PREP(FMC2_PMEM_MEMHOLD, timings->thold_mem);
199 pmem |= FIELD_PREP(FMC2_PMEM_MEMHIZ, timings->thiz);
Christophe Kerello9de081d2020-07-31 09:53:39 +0200200 writel(pmem, nfc->io_base + FMC2_PMEM);
Christophe Kerelloda141682019-04-05 11:41:50 +0200201
202 /* Set tset/twait/thold/thiz timings in attribut bank */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200203 patt = FIELD_PREP(FMC2_PATT_ATTSET, timings->tset_att);
204 patt |= FIELD_PREP(FMC2_PATT_ATTWAIT, timings->twait);
205 patt |= FIELD_PREP(FMC2_PATT_ATTHOLD, timings->thold_att);
206 patt |= FIELD_PREP(FMC2_PATT_ATTHIZ, timings->thiz);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200207 writel(patt, nfc->io_base + FMC2_PATT);
Christophe Kerelloda141682019-04-05 11:41:50 +0200208}
209
Christophe Kerellod1a25872020-07-31 09:53:37 +0200210static void stm32_fmc2_nfc_setup(struct nand_chip *chip)
Christophe Kerelloda141682019-04-05 11:41:50 +0200211{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200212 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerello9de081d2020-07-31 09:53:39 +0200213 u32 pcr = 0, pcr_mask;
Christophe Kerelloda141682019-04-05 11:41:50 +0200214
215 /* Configure ECC algorithm (default configuration is Hamming) */
Christophe Kerello9de081d2020-07-31 09:53:39 +0200216 pcr_mask = FMC2_PCR_ECCALG;
217 pcr_mask |= FMC2_PCR_BCHECC;
Christophe Kerelloda141682019-04-05 11:41:50 +0200218 if (chip->ecc.strength == FMC2_ECC_BCH8) {
219 pcr |= FMC2_PCR_ECCALG;
220 pcr |= FMC2_PCR_BCHECC;
221 } else if (chip->ecc.strength == FMC2_ECC_BCH4) {
222 pcr |= FMC2_PCR_ECCALG;
223 }
224
225 /* Set buswidth */
Christophe Kerello9de081d2020-07-31 09:53:39 +0200226 pcr_mask |= FMC2_PCR_PWID;
Christophe Kerelloda141682019-04-05 11:41:50 +0200227 if (chip->options & NAND_BUSWIDTH_16)
Christophe Kerellof4aca872020-07-31 09:53:38 +0200228 pcr |= FIELD_PREP(FMC2_PCR_PWID, FMC2_PCR_PWID_BUSWIDTH_16);
Christophe Kerelloda141682019-04-05 11:41:50 +0200229
230 /* Set ECC sector size */
Christophe Kerello9de081d2020-07-31 09:53:39 +0200231 pcr_mask |= FMC2_PCR_ECCSS;
Christophe Kerellof4aca872020-07-31 09:53:38 +0200232 pcr |= FIELD_PREP(FMC2_PCR_ECCSS, FMC2_PCR_ECCSS_512);
Christophe Kerelloda141682019-04-05 11:41:50 +0200233
Christophe Kerello9de081d2020-07-31 09:53:39 +0200234 clrsetbits_le32(nfc->io_base + FMC2_PCR, pcr_mask, pcr);
Christophe Kerelloda141682019-04-05 11:41:50 +0200235}
236
Christophe Kerellod1a25872020-07-31 09:53:37 +0200237static void stm32_fmc2_nfc_select_chip(struct mtd_info *mtd, int chipnr)
Christophe Kerelloda141682019-04-05 11:41:50 +0200238{
239 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200240 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200241 struct stm32_fmc2_nand *nand = to_fmc2_nand(chip);
242
243 if (chipnr < 0 || chipnr >= nand->ncs)
244 return;
245
Christophe Kerellod1a25872020-07-31 09:53:37 +0200246 if (nand->cs_used[chipnr] == nfc->cs_sel)
Christophe Kerelloda141682019-04-05 11:41:50 +0200247 return;
248
Christophe Kerellod1a25872020-07-31 09:53:37 +0200249 nfc->cs_sel = nand->cs_used[chipnr];
Christophe Kerello6276f862020-07-31 09:53:41 +0200250 chip->IO_ADDR_R = (void __iomem *)nfc->data_base[nfc->cs_sel];
251 chip->IO_ADDR_W = (void __iomem *)nfc->data_base[nfc->cs_sel];
Christophe Kerelloda141682019-04-05 11:41:50 +0200252
Christophe Kerellod1a25872020-07-31 09:53:37 +0200253 stm32_fmc2_nfc_setup(chip);
254 stm32_fmc2_nfc_timings_init(chip);
Christophe Kerelloda141682019-04-05 11:41:50 +0200255}
256
Christophe Kerellod1a25872020-07-31 09:53:37 +0200257static void stm32_fmc2_nfc_set_buswidth_16(struct stm32_fmc2_nfc *nfc,
258 bool set)
Christophe Kerelloda141682019-04-05 11:41:50 +0200259{
Christophe Kerello9de081d2020-07-31 09:53:39 +0200260 u32 pcr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200261
Christophe Kerello9de081d2020-07-31 09:53:39 +0200262 pcr = set ? FIELD_PREP(FMC2_PCR_PWID, FMC2_PCR_PWID_BUSWIDTH_16) :
263 FIELD_PREP(FMC2_PCR_PWID, FMC2_PCR_PWID_BUSWIDTH_8);
264
265 clrsetbits_le32(nfc->io_base + FMC2_PCR, FMC2_PCR_PWID, pcr);
Christophe Kerelloda141682019-04-05 11:41:50 +0200266}
267
Christophe Kerellod1a25872020-07-31 09:53:37 +0200268static void stm32_fmc2_nfc_set_ecc(struct stm32_fmc2_nfc *nfc, bool enable)
Christophe Kerelloda141682019-04-05 11:41:50 +0200269{
Christophe Kerello9de081d2020-07-31 09:53:39 +0200270 clrsetbits_le32(nfc->io_base + FMC2_PCR, FMC2_PCR_ECCEN,
271 enable ? FMC2_PCR_ECCEN : 0);
Christophe Kerelloda141682019-04-05 11:41:50 +0200272}
273
Christophe Kerellod1a25872020-07-31 09:53:37 +0200274static void stm32_fmc2_nfc_clear_bch_irq(struct stm32_fmc2_nfc *nfc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200275{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200276 writel(FMC2_BCHICR_CLEAR_IRQ, nfc->io_base + FMC2_BCHICR);
Christophe Kerelloda141682019-04-05 11:41:50 +0200277}
278
Christophe Kerellod1a25872020-07-31 09:53:37 +0200279static void stm32_fmc2_nfc_cmd_ctrl(struct mtd_info *mtd, int cmd,
280 unsigned int ctrl)
Christophe Kerelloda141682019-04-05 11:41:50 +0200281{
282 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200283 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200284
285 if (cmd == NAND_CMD_NONE)
286 return;
287
288 if (ctrl & NAND_CLE) {
Christophe Kerellod1a25872020-07-31 09:53:37 +0200289 writeb(cmd, nfc->cmd_base[nfc->cs_sel]);
Christophe Kerelloda141682019-04-05 11:41:50 +0200290 return;
291 }
292
Christophe Kerellod1a25872020-07-31 09:53:37 +0200293 writeb(cmd, nfc->addr_base[nfc->cs_sel]);
Christophe Kerelloda141682019-04-05 11:41:50 +0200294}
295
296/*
297 * Enable ECC logic and reset syndrome/parity bits previously calculated
298 * Syndrome/parity bits is cleared by setting the ECCEN bit to 0
299 */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200300static void stm32_fmc2_nfc_hwctl(struct mtd_info *mtd, int mode)
Christophe Kerelloda141682019-04-05 11:41:50 +0200301{
302 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200303 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200304
Christophe Kerellod1a25872020-07-31 09:53:37 +0200305 stm32_fmc2_nfc_set_ecc(nfc, false);
Christophe Kerelloda141682019-04-05 11:41:50 +0200306
307 if (chip->ecc.strength != FMC2_ECC_HAM) {
Christophe Kerello9de081d2020-07-31 09:53:39 +0200308 clrsetbits_le32(nfc->io_base + FMC2_PCR, FMC2_PCR_WEN,
309 mode == NAND_ECC_WRITE ? FMC2_PCR_WEN : 0);
Christophe Kerelloda141682019-04-05 11:41:50 +0200310
Christophe Kerellod1a25872020-07-31 09:53:37 +0200311 stm32_fmc2_nfc_clear_bch_irq(nfc);
Christophe Kerelloda141682019-04-05 11:41:50 +0200312 }
313
Christophe Kerellod1a25872020-07-31 09:53:37 +0200314 stm32_fmc2_nfc_set_ecc(nfc, true);
Christophe Kerelloda141682019-04-05 11:41:50 +0200315}
316
317/*
318 * ECC Hamming calculation
319 * ECC is 3 bytes for 512 bytes of data (supports error correction up to
320 * max of 1-bit)
321 */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200322static int stm32_fmc2_nfc_ham_calculate(struct mtd_info *mtd, const u8 *data,
323 u8 *ecc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200324{
325 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200326 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200327 u32 heccr, sr;
328 int ret;
329
Christophe Kerellod1a25872020-07-31 09:53:37 +0200330 ret = readl_poll_timeout(nfc->io_base + FMC2_SR, sr,
Christophe Kerello92693e32020-07-31 09:53:36 +0200331 sr & FMC2_SR_NWRF, FMC2_TIMEOUT_5S);
Christophe Kerelloda141682019-04-05 11:41:50 +0200332 if (ret < 0) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100333 log_err("Ham timeout\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200334 return ret;
335 }
336
Christophe Kerellod1a25872020-07-31 09:53:37 +0200337 heccr = readl(nfc->io_base + FMC2_HECCR);
Christophe Kerelloda141682019-04-05 11:41:50 +0200338
339 ecc[0] = heccr;
340 ecc[1] = heccr >> 8;
341 ecc[2] = heccr >> 16;
342
Christophe Kerellod1a25872020-07-31 09:53:37 +0200343 stm32_fmc2_nfc_set_ecc(nfc, false);
Christophe Kerelloda141682019-04-05 11:41:50 +0200344
345 return 0;
346}
347
Christophe Kerellod1a25872020-07-31 09:53:37 +0200348static int stm32_fmc2_nfc_ham_correct(struct mtd_info *mtd, u8 *dat,
349 u8 *read_ecc, u8 *calc_ecc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200350{
351 u8 bit_position = 0, b0, b1, b2;
352 u32 byte_addr = 0, b;
353 u32 i, shifting = 1;
354
355 /* Indicate which bit and byte is faulty (if any) */
356 b0 = read_ecc[0] ^ calc_ecc[0];
357 b1 = read_ecc[1] ^ calc_ecc[1];
358 b2 = read_ecc[2] ^ calc_ecc[2];
359 b = b0 | (b1 << 8) | (b2 << 16);
360
361 /* No errors */
362 if (likely(!b))
363 return 0;
364
365 /* Calculate bit position */
366 for (i = 0; i < 3; i++) {
367 switch (b % 4) {
368 case 2:
369 bit_position += shifting;
370 case 1:
371 break;
372 default:
373 return -EBADMSG;
374 }
375 shifting <<= 1;
376 b >>= 2;
377 }
378
379 /* Calculate byte position */
380 shifting = 1;
381 for (i = 0; i < 9; i++) {
382 switch (b % 4) {
383 case 2:
384 byte_addr += shifting;
385 case 1:
386 break;
387 default:
388 return -EBADMSG;
389 }
390 shifting <<= 1;
391 b >>= 2;
392 }
393
394 /* Flip the bit */
395 dat[byte_addr] ^= (1 << bit_position);
396
397 return 1;
398}
399
400/*
401 * ECC BCH calculation and correction
402 * ECC is 7/13 bytes for 512 bytes of data (supports error correction up to
403 * max of 4-bit/8-bit)
404 */
405
Christophe Kerellod1a25872020-07-31 09:53:37 +0200406static int stm32_fmc2_nfc_bch_calculate(struct mtd_info *mtd, const u8 *data,
407 u8 *ecc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200408{
409 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200410 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200411 u32 bchpbr, bchisr;
412 int ret;
413
414 /* Wait until the BCH code is ready */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200415 ret = readl_poll_timeout(nfc->io_base + FMC2_BCHISR, bchisr,
Christophe Kerello92693e32020-07-31 09:53:36 +0200416 bchisr & FMC2_BCHISR_EPBRF, FMC2_TIMEOUT_5S);
Christophe Kerelloda141682019-04-05 11:41:50 +0200417 if (ret < 0) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100418 log_err("Bch timeout\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200419 return ret;
420 }
421
422 /* Read parity bits */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200423 bchpbr = readl(nfc->io_base + FMC2_BCHPBR1);
Christophe Kerelloda141682019-04-05 11:41:50 +0200424 ecc[0] = bchpbr;
425 ecc[1] = bchpbr >> 8;
426 ecc[2] = bchpbr >> 16;
427 ecc[3] = bchpbr >> 24;
428
Christophe Kerellod1a25872020-07-31 09:53:37 +0200429 bchpbr = readl(nfc->io_base + FMC2_BCHPBR2);
Christophe Kerelloda141682019-04-05 11:41:50 +0200430 ecc[4] = bchpbr;
431 ecc[5] = bchpbr >> 8;
432 ecc[6] = bchpbr >> 16;
433
434 if (chip->ecc.strength == FMC2_ECC_BCH8) {
435 ecc[7] = bchpbr >> 24;
436
Christophe Kerellod1a25872020-07-31 09:53:37 +0200437 bchpbr = readl(nfc->io_base + FMC2_BCHPBR3);
Christophe Kerelloda141682019-04-05 11:41:50 +0200438 ecc[8] = bchpbr;
439 ecc[9] = bchpbr >> 8;
440 ecc[10] = bchpbr >> 16;
441 ecc[11] = bchpbr >> 24;
442
Christophe Kerellod1a25872020-07-31 09:53:37 +0200443 bchpbr = readl(nfc->io_base + FMC2_BCHPBR4);
Christophe Kerelloda141682019-04-05 11:41:50 +0200444 ecc[12] = bchpbr;
445 }
446
Christophe Kerellod1a25872020-07-31 09:53:37 +0200447 stm32_fmc2_nfc_set_ecc(nfc, false);
Christophe Kerelloda141682019-04-05 11:41:50 +0200448
449 return 0;
450}
451
Christophe Kerellod1a25872020-07-31 09:53:37 +0200452static int stm32_fmc2_nfc_bch_correct(struct mtd_info *mtd, u8 *dat,
453 u8 *read_ecc, u8 *calc_ecc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200454{
455 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200456 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200457 u32 bchdsr0, bchdsr1, bchdsr2, bchdsr3, bchdsr4, bchisr;
458 u16 pos[8];
459 int i, ret, den, eccsize = chip->ecc.size;
460 unsigned int nb_errs = 0;
461
462 /* Wait until the decoding error is ready */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200463 ret = readl_poll_timeout(nfc->io_base + FMC2_BCHISR, bchisr,
Christophe Kerello92693e32020-07-31 09:53:36 +0200464 bchisr & FMC2_BCHISR_DERF, FMC2_TIMEOUT_5S);
Christophe Kerelloda141682019-04-05 11:41:50 +0200465 if (ret < 0) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100466 log_err("Bch timeout\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200467 return ret;
468 }
469
Christophe Kerellod1a25872020-07-31 09:53:37 +0200470 bchdsr0 = readl(nfc->io_base + FMC2_BCHDSR0);
471 bchdsr1 = readl(nfc->io_base + FMC2_BCHDSR1);
472 bchdsr2 = readl(nfc->io_base + FMC2_BCHDSR2);
473 bchdsr3 = readl(nfc->io_base + FMC2_BCHDSR3);
474 bchdsr4 = readl(nfc->io_base + FMC2_BCHDSR4);
Christophe Kerelloda141682019-04-05 11:41:50 +0200475
Christophe Kerellod1a25872020-07-31 09:53:37 +0200476 stm32_fmc2_nfc_set_ecc(nfc, false);
Christophe Kerelloda141682019-04-05 11:41:50 +0200477
478 /* No errors found */
479 if (likely(!(bchdsr0 & FMC2_BCHDSR0_DEF)))
480 return 0;
481
482 /* Too many errors detected */
483 if (unlikely(bchdsr0 & FMC2_BCHDSR0_DUE))
484 return -EBADMSG;
485
Christophe Kerellof4aca872020-07-31 09:53:38 +0200486 pos[0] = FIELD_GET(FMC2_BCHDSR1_EBP1, bchdsr1);
487 pos[1] = FIELD_GET(FMC2_BCHDSR1_EBP2, bchdsr1);
488 pos[2] = FIELD_GET(FMC2_BCHDSR2_EBP3, bchdsr2);
489 pos[3] = FIELD_GET(FMC2_BCHDSR2_EBP4, bchdsr2);
490 pos[4] = FIELD_GET(FMC2_BCHDSR3_EBP5, bchdsr3);
491 pos[5] = FIELD_GET(FMC2_BCHDSR3_EBP6, bchdsr3);
492 pos[6] = FIELD_GET(FMC2_BCHDSR4_EBP7, bchdsr4);
493 pos[7] = FIELD_GET(FMC2_BCHDSR4_EBP8, bchdsr4);
Christophe Kerelloda141682019-04-05 11:41:50 +0200494
Christophe Kerellof4aca872020-07-31 09:53:38 +0200495 den = FIELD_GET(FMC2_BCHDSR0_DEN, bchdsr0);
Christophe Kerelloda141682019-04-05 11:41:50 +0200496 for (i = 0; i < den; i++) {
497 if (pos[i] < eccsize * 8) {
498 __change_bit(pos[i], (unsigned long *)dat);
499 nb_errs++;
500 }
501 }
502
503 return nb_errs;
504}
505
Christophe Kerellod1a25872020-07-31 09:53:37 +0200506static int stm32_fmc2_nfc_read_page(struct mtd_info *mtd,
507 struct nand_chip *chip, u8 *buf,
508 int oob_required, int page)
Christophe Kerelloda141682019-04-05 11:41:50 +0200509{
510 int i, s, stat, eccsize = chip->ecc.size;
511 int eccbytes = chip->ecc.bytes;
512 int eccsteps = chip->ecc.steps;
513 int eccstrength = chip->ecc.strength;
514 u8 *p = buf;
515 u8 *ecc_calc = chip->buffers->ecccalc;
516 u8 *ecc_code = chip->buffers->ecccode;
517 unsigned int max_bitflips = 0;
518
519 for (i = mtd->writesize + FMC2_BBM_LEN, s = 0; s < eccsteps;
520 s++, i += eccbytes, p += eccsize) {
521 chip->ecc.hwctl(mtd, NAND_ECC_READ);
522
523 /* Read the nand page sector (512 bytes) */
524 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, s * eccsize, -1);
525 chip->read_buf(mtd, p, eccsize);
526
527 /* Read the corresponding ECC bytes */
528 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, i, -1);
529 chip->read_buf(mtd, ecc_code, eccbytes);
530
531 /* Correct the data */
532 stat = chip->ecc.correct(mtd, p, ecc_code, ecc_calc);
533 if (stat == -EBADMSG)
534 /* Check for empty pages with bitflips */
535 stat = nand_check_erased_ecc_chunk(p, eccsize,
536 ecc_code, eccbytes,
537 NULL, 0,
538 eccstrength);
539
540 if (stat < 0) {
541 mtd->ecc_stats.failed++;
542 } else {
543 mtd->ecc_stats.corrected += stat;
544 max_bitflips = max_t(unsigned int, max_bitflips, stat);
545 }
546 }
547
548 /* Read oob */
549 if (oob_required) {
550 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
551 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
552 }
553
554 return max_bitflips;
555}
556
Christophe Kerello6276f862020-07-31 09:53:41 +0200557static void stm32_fmc2_nfc_init(struct stm32_fmc2_nfc *nfc, bool has_parent)
Christophe Kerelloda141682019-04-05 11:41:50 +0200558{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200559 u32 pcr = readl(nfc->io_base + FMC2_PCR);
Christophe Kerelloda141682019-04-05 11:41:50 +0200560
561 /* Set CS used to undefined */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200562 nfc->cs_sel = -1;
Christophe Kerelloda141682019-04-05 11:41:50 +0200563
564 /* Enable wait feature and nand flash memory bank */
565 pcr |= FMC2_PCR_PWAITEN;
566 pcr |= FMC2_PCR_PBKEN;
567
568 /* Set buswidth to 8 bits mode for identification */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200569 pcr &= ~FMC2_PCR_PWID;
Christophe Kerelloda141682019-04-05 11:41:50 +0200570
571 /* ECC logic is disabled */
572 pcr &= ~FMC2_PCR_ECCEN;
573
574 /* Default mode */
575 pcr &= ~FMC2_PCR_ECCALG;
576 pcr &= ~FMC2_PCR_BCHECC;
577 pcr &= ~FMC2_PCR_WEN;
578
579 /* Set default ECC sector size */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200580 pcr &= ~FMC2_PCR_ECCSS;
581 pcr |= FIELD_PREP(FMC2_PCR_ECCSS, FMC2_PCR_ECCSS_2048);
Christophe Kerelloda141682019-04-05 11:41:50 +0200582
583 /* Set default tclr/tar timings */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200584 pcr &= ~FMC2_PCR_TCLR;
585 pcr |= FIELD_PREP(FMC2_PCR_TCLR, FMC2_PCR_TCLR_DEFAULT);
586 pcr &= ~FMC2_PCR_TAR;
587 pcr |= FIELD_PREP(FMC2_PCR_TAR, FMC2_PCR_TAR_DEFAULT);
Christophe Kerelloda141682019-04-05 11:41:50 +0200588
589 /* Enable FMC2 controller */
Christophe Kerello6276f862020-07-31 09:53:41 +0200590 if (!has_parent)
591 setbits_le32(nfc->io_base + FMC2_BCR1, FMC2_BCR1_FMC2EN);
Christophe Kerelloda141682019-04-05 11:41:50 +0200592
Christophe Kerellod1a25872020-07-31 09:53:37 +0200593 writel(pcr, nfc->io_base + FMC2_PCR);
594 writel(FMC2_PMEM_DEFAULT, nfc->io_base + FMC2_PMEM);
595 writel(FMC2_PATT_DEFAULT, nfc->io_base + FMC2_PATT);
Christophe Kerelloda141682019-04-05 11:41:50 +0200596}
597
Christophe Kerellod1a25872020-07-31 09:53:37 +0200598static void stm32_fmc2_nfc_calc_timings(struct nand_chip *chip,
599 const struct nand_sdr_timings *sdrt)
Christophe Kerelloda141682019-04-05 11:41:50 +0200600{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200601 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200602 struct stm32_fmc2_nand *nand = to_fmc2_nand(chip);
603 struct stm32_fmc2_timings *tims = &nand->timings;
Christophe Kerellod1a25872020-07-31 09:53:37 +0200604 unsigned long hclk = clk_get_rate(&nfc->clk);
Christophe Kerelloda141682019-04-05 11:41:50 +0200605 unsigned long hclkp = FMC2_NSEC_PER_SEC / (hclk / 1000);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200606 unsigned long timing, tar, tclr, thiz, twait;
607 unsigned long tset_mem, tset_att, thold_mem, thold_att;
Christophe Kerelloda141682019-04-05 11:41:50 +0200608
Patrick Delaunay804858a2019-06-21 15:26:54 +0200609 tar = max_t(unsigned long, hclkp, sdrt->tAR_min);
610 timing = DIV_ROUND_UP(tar, hclkp) - 1;
611 tims->tar = min_t(unsigned long, timing, FMC2_PCR_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200612
Patrick Delaunay804858a2019-06-21 15:26:54 +0200613 tclr = max_t(unsigned long, hclkp, sdrt->tCLR_min);
614 timing = DIV_ROUND_UP(tclr, hclkp) - 1;
615 tims->tclr = min_t(unsigned long, timing, FMC2_PCR_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200616
617 tims->thiz = FMC2_THIZ;
618 thiz = (tims->thiz + 1) * hclkp;
619
620 /*
621 * tWAIT > tRP
622 * tWAIT > tWP
623 * tWAIT > tREA + tIO
624 */
Patrick Delaunay804858a2019-06-21 15:26:54 +0200625 twait = max_t(unsigned long, hclkp, sdrt->tRP_min);
626 twait = max_t(unsigned long, twait, sdrt->tWP_min);
627 twait = max_t(unsigned long, twait, sdrt->tREA_max + FMC2_TIO);
628 timing = DIV_ROUND_UP(twait, hclkp);
629 tims->twait = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200630
631 /*
632 * tSETUP_MEM > tCS - tWAIT
633 * tSETUP_MEM > tALS - tWAIT
634 * tSETUP_MEM > tDS - (tWAIT - tHIZ)
635 */
636 tset_mem = hclkp;
637 if (sdrt->tCS_min > twait && (tset_mem < sdrt->tCS_min - twait))
638 tset_mem = sdrt->tCS_min - twait;
639 if (sdrt->tALS_min > twait && (tset_mem < sdrt->tALS_min - twait))
640 tset_mem = sdrt->tALS_min - twait;
641 if (twait > thiz && (sdrt->tDS_min > twait - thiz) &&
642 (tset_mem < sdrt->tDS_min - (twait - thiz)))
643 tset_mem = sdrt->tDS_min - (twait - thiz);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200644 timing = DIV_ROUND_UP(tset_mem, hclkp);
645 tims->tset_mem = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200646
647 /*
648 * tHOLD_MEM > tCH
649 * tHOLD_MEM > tREH - tSETUP_MEM
650 * tHOLD_MEM > max(tRC, tWC) - (tSETUP_MEM + tWAIT)
651 */
Patrick Delaunay804858a2019-06-21 15:26:54 +0200652 thold_mem = max_t(unsigned long, hclkp, sdrt->tCH_min);
Christophe Kerelloda141682019-04-05 11:41:50 +0200653 if (sdrt->tREH_min > tset_mem &&
654 (thold_mem < sdrt->tREH_min - tset_mem))
655 thold_mem = sdrt->tREH_min - tset_mem;
656 if ((sdrt->tRC_min > tset_mem + twait) &&
657 (thold_mem < sdrt->tRC_min - (tset_mem + twait)))
658 thold_mem = sdrt->tRC_min - (tset_mem + twait);
659 if ((sdrt->tWC_min > tset_mem + twait) &&
660 (thold_mem < sdrt->tWC_min - (tset_mem + twait)))
661 thold_mem = sdrt->tWC_min - (tset_mem + twait);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200662 timing = DIV_ROUND_UP(thold_mem, hclkp);
663 tims->thold_mem = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200664
665 /*
666 * tSETUP_ATT > tCS - tWAIT
667 * tSETUP_ATT > tCLS - tWAIT
668 * tSETUP_ATT > tALS - tWAIT
669 * tSETUP_ATT > tRHW - tHOLD_MEM
670 * tSETUP_ATT > tDS - (tWAIT - tHIZ)
671 */
672 tset_att = hclkp;
673 if (sdrt->tCS_min > twait && (tset_att < sdrt->tCS_min - twait))
674 tset_att = sdrt->tCS_min - twait;
675 if (sdrt->tCLS_min > twait && (tset_att < sdrt->tCLS_min - twait))
676 tset_att = sdrt->tCLS_min - twait;
677 if (sdrt->tALS_min > twait && (tset_att < sdrt->tALS_min - twait))
678 tset_att = sdrt->tALS_min - twait;
679 if (sdrt->tRHW_min > thold_mem &&
680 (tset_att < sdrt->tRHW_min - thold_mem))
681 tset_att = sdrt->tRHW_min - thold_mem;
682 if (twait > thiz && (sdrt->tDS_min > twait - thiz) &&
683 (tset_att < sdrt->tDS_min - (twait - thiz)))
684 tset_att = sdrt->tDS_min - (twait - thiz);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200685 timing = DIV_ROUND_UP(tset_att, hclkp);
686 tims->tset_att = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200687
688 /*
689 * tHOLD_ATT > tALH
690 * tHOLD_ATT > tCH
691 * tHOLD_ATT > tCLH
692 * tHOLD_ATT > tCOH
693 * tHOLD_ATT > tDH
694 * tHOLD_ATT > tWB + tIO + tSYNC - tSETUP_MEM
695 * tHOLD_ATT > tADL - tSETUP_MEM
696 * tHOLD_ATT > tWH - tSETUP_MEM
697 * tHOLD_ATT > tWHR - tSETUP_MEM
698 * tHOLD_ATT > tRC - (tSETUP_ATT + tWAIT)
699 * tHOLD_ATT > tWC - (tSETUP_ATT + tWAIT)
700 */
Patrick Delaunay804858a2019-06-21 15:26:54 +0200701 thold_att = max_t(unsigned long, hclkp, sdrt->tALH_min);
702 thold_att = max_t(unsigned long, thold_att, sdrt->tCH_min);
703 thold_att = max_t(unsigned long, thold_att, sdrt->tCLH_min);
704 thold_att = max_t(unsigned long, thold_att, sdrt->tCOH_min);
705 thold_att = max_t(unsigned long, thold_att, sdrt->tDH_min);
Christophe Kerelloda141682019-04-05 11:41:50 +0200706 if ((sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC > tset_mem) &&
707 (thold_att < sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC - tset_mem))
708 thold_att = sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC - tset_mem;
709 if (sdrt->tADL_min > tset_mem &&
710 (thold_att < sdrt->tADL_min - tset_mem))
711 thold_att = sdrt->tADL_min - tset_mem;
712 if (sdrt->tWH_min > tset_mem &&
713 (thold_att < sdrt->tWH_min - tset_mem))
714 thold_att = sdrt->tWH_min - tset_mem;
715 if (sdrt->tWHR_min > tset_mem &&
716 (thold_att < sdrt->tWHR_min - tset_mem))
717 thold_att = sdrt->tWHR_min - tset_mem;
718 if ((sdrt->tRC_min > tset_att + twait) &&
719 (thold_att < sdrt->tRC_min - (tset_att + twait)))
720 thold_att = sdrt->tRC_min - (tset_att + twait);
721 if ((sdrt->tWC_min > tset_att + twait) &&
722 (thold_att < sdrt->tWC_min - (tset_att + twait)))
723 thold_att = sdrt->tWC_min - (tset_att + twait);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200724 timing = DIV_ROUND_UP(thold_att, hclkp);
725 tims->thold_att = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200726}
727
Christophe Kerellod1a25872020-07-31 09:53:37 +0200728static int stm32_fmc2_nfc_setup_interface(struct mtd_info *mtd, int chipnr,
729 const struct nand_data_interface *cf)
Christophe Kerelloda141682019-04-05 11:41:50 +0200730{
731 struct nand_chip *chip = mtd_to_nand(mtd);
732 const struct nand_sdr_timings *sdrt;
733
Christophe Kerellod1a25872020-07-31 09:53:37 +0200734 sdrt = nand_get_sdr_timings(cf);
Christophe Kerelloda141682019-04-05 11:41:50 +0200735 if (IS_ERR(sdrt))
736 return PTR_ERR(sdrt);
737
Christophe Kerello1b4662a2023-03-30 11:16:21 +0200738 if (sdrt->tRC_min < 30000)
739 return -EOPNOTSUPP;
740
Christophe Kerelloda141682019-04-05 11:41:50 +0200741 if (chipnr == NAND_DATA_IFACE_CHECK_ONLY)
742 return 0;
743
Christophe Kerellod1a25872020-07-31 09:53:37 +0200744 stm32_fmc2_nfc_calc_timings(chip, sdrt);
745 stm32_fmc2_nfc_timings_init(chip);
Christophe Kerelloda141682019-04-05 11:41:50 +0200746
747 return 0;
748}
749
Christophe Kerellod1a25872020-07-31 09:53:37 +0200750static void stm32_fmc2_nfc_nand_callbacks_setup(struct nand_chip *chip)
Christophe Kerelloda141682019-04-05 11:41:50 +0200751{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200752 chip->ecc.hwctl = stm32_fmc2_nfc_hwctl;
Christophe Kerelloda141682019-04-05 11:41:50 +0200753
754 /*
755 * Specific callbacks to read/write a page depending on
756 * the algo used (Hamming, BCH).
757 */
758 if (chip->ecc.strength == FMC2_ECC_HAM) {
759 /* Hamming is used */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200760 chip->ecc.calculate = stm32_fmc2_nfc_ham_calculate;
761 chip->ecc.correct = stm32_fmc2_nfc_ham_correct;
Christophe Kerelloda141682019-04-05 11:41:50 +0200762 chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 4 : 3;
763 chip->ecc.options |= NAND_ECC_GENERIC_ERASED_CHECK;
764 return;
765 }
766
767 /* BCH is used */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200768 chip->ecc.read_page = stm32_fmc2_nfc_read_page;
769 chip->ecc.calculate = stm32_fmc2_nfc_bch_calculate;
770 chip->ecc.correct = stm32_fmc2_nfc_bch_correct;
Christophe Kerelloda141682019-04-05 11:41:50 +0200771
772 if (chip->ecc.strength == FMC2_ECC_BCH8)
773 chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 14 : 13;
774 else
775 chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 8 : 7;
776}
777
Christophe Kerellod1a25872020-07-31 09:53:37 +0200778static int stm32_fmc2_nfc_calc_ecc_bytes(int step_size, int strength)
Christophe Kerelloda141682019-04-05 11:41:50 +0200779{
780 /* Hamming */
781 if (strength == FMC2_ECC_HAM)
782 return 4;
783
784 /* BCH8 */
785 if (strength == FMC2_ECC_BCH8)
786 return 14;
787
788 /* BCH4 */
789 return 8;
790}
791
Christophe Kerellod1a25872020-07-31 09:53:37 +0200792NAND_ECC_CAPS_SINGLE(stm32_fmc2_nfc_ecc_caps, stm32_fmc2_nfc_calc_ecc_bytes,
Christophe Kerelloda141682019-04-05 11:41:50 +0200793 FMC2_ECC_STEP_SIZE,
794 FMC2_ECC_HAM, FMC2_ECC_BCH4, FMC2_ECC_BCH8);
795
Christophe Kerellod1a25872020-07-31 09:53:37 +0200796static int stm32_fmc2_nfc_parse_child(struct stm32_fmc2_nfc *nfc, ofnode node)
Christophe Kerelloda141682019-04-05 11:41:50 +0200797{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200798 struct stm32_fmc2_nand *nand = &nfc->nand;
Christophe Kerelloda141682019-04-05 11:41:50 +0200799 u32 cs[FMC2_MAX_CE];
800 int ret, i;
801
802 if (!ofnode_get_property(node, "reg", &nand->ncs))
803 return -EINVAL;
804
805 nand->ncs /= sizeof(u32);
806 if (!nand->ncs) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100807 log_err("Invalid reg property size\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200808 return -EINVAL;
809 }
810
811 ret = ofnode_read_u32_array(node, "reg", cs, nand->ncs);
812 if (ret < 0) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100813 log_err("Could not retrieve reg property\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200814 return -EINVAL;
815 }
816
817 for (i = 0; i < nand->ncs; i++) {
Christophe Kerello45dd1ee2020-07-31 09:53:34 +0200818 if (cs[i] >= FMC2_MAX_CE) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100819 log_err("Invalid reg value: %d\n", nand->cs_used[i]);
Christophe Kerelloda141682019-04-05 11:41:50 +0200820 return -EINVAL;
821 }
822
Christophe Kerellod1a25872020-07-31 09:53:37 +0200823 if (nfc->cs_assigned & BIT(cs[i])) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100824 log_err("Cs already assigned: %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 nfc->cs_assigned |= BIT(cs[i]);
Christophe Kerelloda141682019-04-05 11:41:50 +0200829 nand->cs_used[i] = cs[i];
830 }
831
Christophe Kerelloe389a152022-02-22 17:38:49 +0100832 gpio_request_by_name_nodev(node, "wp-gpios", 0, &nand->wp_gpio,
833 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
834
Patrice Chotard33d2cf92021-09-13 16:25:53 +0200835 nand->chip.flash_node = node;
Christophe Kerelloda141682019-04-05 11:41:50 +0200836
837 return 0;
838}
839
Christophe Kerellod1a25872020-07-31 09:53:37 +0200840static int stm32_fmc2_nfc_parse_dt(struct udevice *dev,
841 struct stm32_fmc2_nfc *nfc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200842{
843 ofnode child;
844 int ret, nchips = 0;
845
846 dev_for_each_subnode(child, dev)
847 nchips++;
848
849 if (!nchips) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100850 log_err("NAND chip not defined\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200851 return -EINVAL;
852 }
853
854 if (nchips > 1) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100855 log_err("Too many NAND chips defined\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200856 return -EINVAL;
857 }
858
859 dev_for_each_subnode(child, dev) {
Christophe Kerellod1a25872020-07-31 09:53:37 +0200860 ret = stm32_fmc2_nfc_parse_child(nfc, child);
Christophe Kerelloda141682019-04-05 11:41:50 +0200861 if (ret)
862 return ret;
863 }
864
865 return 0;
866}
867
Christophe Kerello6276f862020-07-31 09:53:41 +0200868static struct udevice *stm32_fmc2_nfc_get_cdev(struct udevice *dev)
869{
870 struct udevice *pdev = dev_get_parent(dev);
871 struct udevice *cdev = NULL;
872 bool ebi_found = false;
873
874 if (pdev && ofnode_device_is_compatible(dev_ofnode(pdev),
875 "st,stm32mp1-fmc2-ebi"))
876 ebi_found = true;
877
878 if (ofnode_device_is_compatible(dev_ofnode(dev),
879 "st,stm32mp1-fmc2-nfc")) {
880 if (ebi_found)
881 cdev = pdev;
882
883 return cdev;
884 }
885
886 if (!ebi_found)
887 cdev = dev;
888
889 return cdev;
890}
891
Christophe Kerellod1a25872020-07-31 09:53:37 +0200892static int stm32_fmc2_nfc_probe(struct udevice *dev)
Christophe Kerelloda141682019-04-05 11:41:50 +0200893{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200894 struct stm32_fmc2_nfc *nfc = dev_get_priv(dev);
895 struct stm32_fmc2_nand *nand = &nfc->nand;
Christophe Kerelloda141682019-04-05 11:41:50 +0200896 struct nand_chip *chip = &nand->chip;
897 struct mtd_info *mtd = &chip->mtd;
898 struct nand_ecclayout *ecclayout;
Christophe Kerello6276f862020-07-31 09:53:41 +0200899 struct udevice *cdev;
Christophe Kerelloda141682019-04-05 11:41:50 +0200900 struct reset_ctl reset;
Patrick Delaunay804858a2019-06-21 15:26:54 +0200901 int oob_index, chip_cs, mem_region, ret;
902 unsigned int i;
Christophe Kerello6276f862020-07-31 09:53:41 +0200903 int start_region = 0;
904 fdt_addr_t addr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200905
Christophe Kerellod1a25872020-07-31 09:53:37 +0200906 spin_lock_init(&nfc->controller.lock);
907 init_waitqueue_head(&nfc->controller.wq);
Christophe Kerelloda141682019-04-05 11:41:50 +0200908
Christophe Kerello6276f862020-07-31 09:53:41 +0200909 cdev = stm32_fmc2_nfc_get_cdev(dev);
910 if (!cdev)
911 return -EINVAL;
912
Christophe Kerellod1a25872020-07-31 09:53:37 +0200913 ret = stm32_fmc2_nfc_parse_dt(dev, nfc);
Christophe Kerelloda141682019-04-05 11:41:50 +0200914 if (ret)
915 return ret;
916
Christophe Kerello6276f862020-07-31 09:53:41 +0200917 nfc->io_base = dev_read_addr(cdev);
918 if (nfc->io_base == FDT_ADDR_T_NONE)
919 return -EINVAL;
920
921 if (dev == cdev)
922 start_region = 1;
Christophe Kerelloda141682019-04-05 11:41:50 +0200923
Christophe Kerello6276f862020-07-31 09:53:41 +0200924 for (chip_cs = 0, mem_region = start_region; chip_cs < FMC2_MAX_CE;
Christophe Kerelloda141682019-04-05 11:41:50 +0200925 chip_cs++, mem_region += 3) {
Christophe Kerellod1a25872020-07-31 09:53:37 +0200926 if (!(nfc->cs_assigned & BIT(chip_cs)))
Christophe Kerelloda141682019-04-05 11:41:50 +0200927 continue;
928
Christophe Kerello6276f862020-07-31 09:53:41 +0200929 addr = dev_read_addr_index(dev, mem_region);
930 if (addr == FDT_ADDR_T_NONE) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100931 dev_err(dev, "Resource data_base not found for cs%d", chip_cs);
Christophe Kerelloda141682019-04-05 11:41:50 +0200932 return ret;
933 }
Christophe Kerello6276f862020-07-31 09:53:41 +0200934 nfc->data_base[chip_cs] = addr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200935
Christophe Kerello6276f862020-07-31 09:53:41 +0200936 addr = dev_read_addr_index(dev, mem_region + 1);
937 if (addr == FDT_ADDR_T_NONE) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100938 dev_err(dev, "Resource cmd_base not found for cs%d", chip_cs);
Christophe Kerelloda141682019-04-05 11:41:50 +0200939 return ret;
940 }
Christophe Kerello6276f862020-07-31 09:53:41 +0200941 nfc->cmd_base[chip_cs] = addr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200942
Christophe Kerello6276f862020-07-31 09:53:41 +0200943 addr = dev_read_addr_index(dev, mem_region + 2);
944 if (addr == FDT_ADDR_T_NONE) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100945 dev_err(dev, "Resource addr_base not found for cs%d", chip_cs);
Christophe Kerelloda141682019-04-05 11:41:50 +0200946 return ret;
947 }
Christophe Kerello6276f862020-07-31 09:53:41 +0200948 nfc->addr_base[chip_cs] = addr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200949 }
950
951 /* Enable the clock */
Christophe Kerello6276f862020-07-31 09:53:41 +0200952 ret = clk_get_by_index(cdev, 0, &nfc->clk);
Christophe Kerelloda141682019-04-05 11:41:50 +0200953 if (ret)
954 return ret;
955
Christophe Kerellod1a25872020-07-31 09:53:37 +0200956 ret = clk_enable(&nfc->clk);
Christophe Kerelloda141682019-04-05 11:41:50 +0200957 if (ret)
958 return ret;
959
960 /* Reset */
961 ret = reset_get_by_index(dev, 0, &reset);
962 if (!ret) {
963 reset_assert(&reset);
964 udelay(2);
965 reset_deassert(&reset);
966 }
967
Christophe Kerello6276f862020-07-31 09:53:41 +0200968 stm32_fmc2_nfc_init(nfc, dev != cdev);
Christophe Kerelloda141682019-04-05 11:41:50 +0200969
Christophe Kerellod1a25872020-07-31 09:53:37 +0200970 chip->controller = &nfc->base;
971 chip->select_chip = stm32_fmc2_nfc_select_chip;
972 chip->setup_data_interface = stm32_fmc2_nfc_setup_interface;
973 chip->cmd_ctrl = stm32_fmc2_nfc_cmd_ctrl;
Christophe Kerelloda141682019-04-05 11:41:50 +0200974 chip->chip_delay = FMC2_RB_DELAY_US;
975 chip->options |= NAND_BUSWIDTH_AUTO | NAND_NO_SUBPAGE_WRITE |
976 NAND_USE_BOUNCE_BUFFER;
977
978 /* Default ECC settings */
979 chip->ecc.mode = NAND_ECC_HW;
980 chip->ecc.size = FMC2_ECC_STEP_SIZE;
981 chip->ecc.strength = FMC2_ECC_BCH8;
982
Christophe Kerelloe389a152022-02-22 17:38:49 +0100983 /* Disable Write Protect */
984 if (dm_gpio_is_valid(&nand->wp_gpio))
985 dm_gpio_set_value(&nand->wp_gpio, 0);
986
Christophe Kerelloda141682019-04-05 11:41:50 +0200987 ret = nand_scan_ident(mtd, nand->ncs, NULL);
988 if (ret)
989 return ret;
990
991 /*
992 * Only NAND_ECC_HW mode is actually supported
993 * Hamming => ecc.strength = 1
994 * BCH4 => ecc.strength = 4
995 * BCH8 => ecc.strength = 8
996 * ECC sector size = 512
997 */
998 if (chip->ecc.mode != NAND_ECC_HW) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100999 dev_err(dev, "Nand_ecc_mode is not well defined in the DT\n");
Christophe Kerelloda141682019-04-05 11:41:50 +02001000 return -EINVAL;
1001 }
1002
Christophe Kerellod1a25872020-07-31 09:53:37 +02001003 ret = nand_check_ecc_caps(chip, &stm32_fmc2_nfc_ecc_caps,
Christophe Kerelloda141682019-04-05 11:41:50 +02001004 mtd->oobsize - FMC2_BBM_LEN);
1005 if (ret) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +01001006 dev_err(dev, "No valid ECC settings set\n");
Christophe Kerelloda141682019-04-05 11:41:50 +02001007 return ret;
1008 }
1009
1010 if (chip->bbt_options & NAND_BBT_USE_FLASH)
1011 chip->bbt_options |= NAND_BBT_NO_OOB;
1012
Christophe Kerellod1a25872020-07-31 09:53:37 +02001013 stm32_fmc2_nfc_nand_callbacks_setup(chip);
Christophe Kerelloda141682019-04-05 11:41:50 +02001014
1015 /* Define ECC layout */
Christophe Kerellod1a25872020-07-31 09:53:37 +02001016 ecclayout = &nfc->ecclayout;
Christophe Kerelloda141682019-04-05 11:41:50 +02001017 ecclayout->eccbytes = chip->ecc.bytes *
1018 (mtd->writesize / chip->ecc.size);
1019 oob_index = FMC2_BBM_LEN;
1020 for (i = 0; i < ecclayout->eccbytes; i++, oob_index++)
1021 ecclayout->eccpos[i] = oob_index;
1022 ecclayout->oobfree->offset = oob_index;
1023 ecclayout->oobfree->length = mtd->oobsize - ecclayout->oobfree->offset;
1024 chip->ecc.layout = ecclayout;
1025
Christophe Kerelloda141682019-04-05 11:41:50 +02001026 if (chip->options & NAND_BUSWIDTH_16)
Christophe Kerellod1a25872020-07-31 09:53:37 +02001027 stm32_fmc2_nfc_set_buswidth_16(nfc, true);
Christophe Kerelloda141682019-04-05 11:41:50 +02001028
Christophe Kerelloda141682019-04-05 11:41:50 +02001029 ret = nand_scan_tail(mtd);
1030 if (ret)
1031 return ret;
1032
1033 return nand_register(0, mtd);
1034}
1035
Christophe Kerellod1a25872020-07-31 09:53:37 +02001036static const struct udevice_id stm32_fmc2_nfc_match[] = {
Christophe Kerelloda141682019-04-05 11:41:50 +02001037 { .compatible = "st,stm32mp15-fmc2" },
Christophe Kerello6276f862020-07-31 09:53:41 +02001038 { .compatible = "st,stm32mp1-fmc2-nfc" },
Christophe Kerelloda141682019-04-05 11:41:50 +02001039 { /* Sentinel */ }
1040};
1041
Christophe Kerellod1a25872020-07-31 09:53:37 +02001042U_BOOT_DRIVER(stm32_fmc2_nfc) = {
1043 .name = "stm32_fmc2_nfc",
Christophe Kerelloda141682019-04-05 11:41:50 +02001044 .id = UCLASS_MTD,
Christophe Kerellod1a25872020-07-31 09:53:37 +02001045 .of_match = stm32_fmc2_nfc_match,
1046 .probe = stm32_fmc2_nfc_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001047 .priv_auto = sizeof(struct stm32_fmc2_nfc),
Christophe Kerelloda141682019-04-05 11:41:50 +02001048};
1049
1050void board_nand_init(void)
1051{
1052 struct udevice *dev;
1053 int ret;
1054
1055 ret = uclass_get_device_by_driver(UCLASS_MTD,
Simon Glass65130cd2020-12-28 20:34:56 -07001056 DM_DRIVER_GET(stm32_fmc2_nfc),
Christophe Kerelloda141682019-04-05 11:41:50 +02001057 &dev);
1058 if (ret && ret != -ENODEV)
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +01001059 log_err("Failed to initialize STM32 FMC2 NFC controller. (error %d)\n",
1060 ret);
Christophe Kerelloda141682019-04-05 11:41:50 +02001061}