blob: fb3279b405ee942786d62e5755e0c672a9730717 [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
738 if (chipnr == NAND_DATA_IFACE_CHECK_ONLY)
739 return 0;
740
Christophe Kerellod1a25872020-07-31 09:53:37 +0200741 stm32_fmc2_nfc_calc_timings(chip, sdrt);
742 stm32_fmc2_nfc_timings_init(chip);
Christophe Kerelloda141682019-04-05 11:41:50 +0200743
744 return 0;
745}
746
Christophe Kerellod1a25872020-07-31 09:53:37 +0200747static void stm32_fmc2_nfc_nand_callbacks_setup(struct nand_chip *chip)
Christophe Kerelloda141682019-04-05 11:41:50 +0200748{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200749 chip->ecc.hwctl = stm32_fmc2_nfc_hwctl;
Christophe Kerelloda141682019-04-05 11:41:50 +0200750
751 /*
752 * Specific callbacks to read/write a page depending on
753 * the algo used (Hamming, BCH).
754 */
755 if (chip->ecc.strength == FMC2_ECC_HAM) {
756 /* Hamming is used */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200757 chip->ecc.calculate = stm32_fmc2_nfc_ham_calculate;
758 chip->ecc.correct = stm32_fmc2_nfc_ham_correct;
Christophe Kerelloda141682019-04-05 11:41:50 +0200759 chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 4 : 3;
760 chip->ecc.options |= NAND_ECC_GENERIC_ERASED_CHECK;
761 return;
762 }
763
764 /* BCH is used */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200765 chip->ecc.read_page = stm32_fmc2_nfc_read_page;
766 chip->ecc.calculate = stm32_fmc2_nfc_bch_calculate;
767 chip->ecc.correct = stm32_fmc2_nfc_bch_correct;
Christophe Kerelloda141682019-04-05 11:41:50 +0200768
769 if (chip->ecc.strength == FMC2_ECC_BCH8)
770 chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 14 : 13;
771 else
772 chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 8 : 7;
773}
774
Christophe Kerellod1a25872020-07-31 09:53:37 +0200775static int stm32_fmc2_nfc_calc_ecc_bytes(int step_size, int strength)
Christophe Kerelloda141682019-04-05 11:41:50 +0200776{
777 /* Hamming */
778 if (strength == FMC2_ECC_HAM)
779 return 4;
780
781 /* BCH8 */
782 if (strength == FMC2_ECC_BCH8)
783 return 14;
784
785 /* BCH4 */
786 return 8;
787}
788
Christophe Kerellod1a25872020-07-31 09:53:37 +0200789NAND_ECC_CAPS_SINGLE(stm32_fmc2_nfc_ecc_caps, stm32_fmc2_nfc_calc_ecc_bytes,
Christophe Kerelloda141682019-04-05 11:41:50 +0200790 FMC2_ECC_STEP_SIZE,
791 FMC2_ECC_HAM, FMC2_ECC_BCH4, FMC2_ECC_BCH8);
792
Christophe Kerellod1a25872020-07-31 09:53:37 +0200793static int stm32_fmc2_nfc_parse_child(struct stm32_fmc2_nfc *nfc, ofnode node)
Christophe Kerelloda141682019-04-05 11:41:50 +0200794{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200795 struct stm32_fmc2_nand *nand = &nfc->nand;
Christophe Kerelloda141682019-04-05 11:41:50 +0200796 u32 cs[FMC2_MAX_CE];
797 int ret, i;
798
799 if (!ofnode_get_property(node, "reg", &nand->ncs))
800 return -EINVAL;
801
802 nand->ncs /= sizeof(u32);
803 if (!nand->ncs) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100804 log_err("Invalid reg property size\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200805 return -EINVAL;
806 }
807
808 ret = ofnode_read_u32_array(node, "reg", cs, nand->ncs);
809 if (ret < 0) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100810 log_err("Could not retrieve reg property\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200811 return -EINVAL;
812 }
813
814 for (i = 0; i < nand->ncs; i++) {
Christophe Kerello45dd1ee2020-07-31 09:53:34 +0200815 if (cs[i] >= FMC2_MAX_CE) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100816 log_err("Invalid reg value: %d\n", nand->cs_used[i]);
Christophe Kerelloda141682019-04-05 11:41:50 +0200817 return -EINVAL;
818 }
819
Christophe Kerellod1a25872020-07-31 09:53:37 +0200820 if (nfc->cs_assigned & BIT(cs[i])) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100821 log_err("Cs already assigned: %d\n", nand->cs_used[i]);
Christophe Kerelloda141682019-04-05 11:41:50 +0200822 return -EINVAL;
823 }
824
Christophe Kerellod1a25872020-07-31 09:53:37 +0200825 nfc->cs_assigned |= BIT(cs[i]);
Christophe Kerelloda141682019-04-05 11:41:50 +0200826 nand->cs_used[i] = cs[i];
827 }
828
Christophe Kerelloe389a152022-02-22 17:38:49 +0100829 gpio_request_by_name_nodev(node, "wp-gpios", 0, &nand->wp_gpio,
830 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
831
Patrice Chotard33d2cf92021-09-13 16:25:53 +0200832 nand->chip.flash_node = node;
Christophe Kerelloda141682019-04-05 11:41:50 +0200833
834 return 0;
835}
836
Christophe Kerellod1a25872020-07-31 09:53:37 +0200837static int stm32_fmc2_nfc_parse_dt(struct udevice *dev,
838 struct stm32_fmc2_nfc *nfc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200839{
840 ofnode child;
841 int ret, nchips = 0;
842
843 dev_for_each_subnode(child, dev)
844 nchips++;
845
846 if (!nchips) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100847 log_err("NAND chip not defined\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200848 return -EINVAL;
849 }
850
851 if (nchips > 1) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100852 log_err("Too many NAND chips defined\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200853 return -EINVAL;
854 }
855
856 dev_for_each_subnode(child, dev) {
Christophe Kerellod1a25872020-07-31 09:53:37 +0200857 ret = stm32_fmc2_nfc_parse_child(nfc, child);
Christophe Kerelloda141682019-04-05 11:41:50 +0200858 if (ret)
859 return ret;
860 }
861
862 return 0;
863}
864
Christophe Kerello6276f862020-07-31 09:53:41 +0200865static struct udevice *stm32_fmc2_nfc_get_cdev(struct udevice *dev)
866{
867 struct udevice *pdev = dev_get_parent(dev);
868 struct udevice *cdev = NULL;
869 bool ebi_found = false;
870
871 if (pdev && ofnode_device_is_compatible(dev_ofnode(pdev),
872 "st,stm32mp1-fmc2-ebi"))
873 ebi_found = true;
874
875 if (ofnode_device_is_compatible(dev_ofnode(dev),
876 "st,stm32mp1-fmc2-nfc")) {
877 if (ebi_found)
878 cdev = pdev;
879
880 return cdev;
881 }
882
883 if (!ebi_found)
884 cdev = dev;
885
886 return cdev;
887}
888
Christophe Kerellod1a25872020-07-31 09:53:37 +0200889static int stm32_fmc2_nfc_probe(struct udevice *dev)
Christophe Kerelloda141682019-04-05 11:41:50 +0200890{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200891 struct stm32_fmc2_nfc *nfc = dev_get_priv(dev);
892 struct stm32_fmc2_nand *nand = &nfc->nand;
Christophe Kerelloda141682019-04-05 11:41:50 +0200893 struct nand_chip *chip = &nand->chip;
894 struct mtd_info *mtd = &chip->mtd;
895 struct nand_ecclayout *ecclayout;
Christophe Kerello6276f862020-07-31 09:53:41 +0200896 struct udevice *cdev;
Christophe Kerelloda141682019-04-05 11:41:50 +0200897 struct reset_ctl reset;
Patrick Delaunay804858a2019-06-21 15:26:54 +0200898 int oob_index, chip_cs, mem_region, ret;
899 unsigned int i;
Christophe Kerello6276f862020-07-31 09:53:41 +0200900 int start_region = 0;
901 fdt_addr_t addr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200902
Christophe Kerellod1a25872020-07-31 09:53:37 +0200903 spin_lock_init(&nfc->controller.lock);
904 init_waitqueue_head(&nfc->controller.wq);
Christophe Kerelloda141682019-04-05 11:41:50 +0200905
Christophe Kerello6276f862020-07-31 09:53:41 +0200906 cdev = stm32_fmc2_nfc_get_cdev(dev);
907 if (!cdev)
908 return -EINVAL;
909
Christophe Kerellod1a25872020-07-31 09:53:37 +0200910 ret = stm32_fmc2_nfc_parse_dt(dev, nfc);
Christophe Kerelloda141682019-04-05 11:41:50 +0200911 if (ret)
912 return ret;
913
Christophe Kerello6276f862020-07-31 09:53:41 +0200914 nfc->io_base = dev_read_addr(cdev);
915 if (nfc->io_base == FDT_ADDR_T_NONE)
916 return -EINVAL;
917
918 if (dev == cdev)
919 start_region = 1;
Christophe Kerelloda141682019-04-05 11:41:50 +0200920
Christophe Kerello6276f862020-07-31 09:53:41 +0200921 for (chip_cs = 0, mem_region = start_region; chip_cs < FMC2_MAX_CE;
Christophe Kerelloda141682019-04-05 11:41:50 +0200922 chip_cs++, mem_region += 3) {
Christophe Kerellod1a25872020-07-31 09:53:37 +0200923 if (!(nfc->cs_assigned & BIT(chip_cs)))
Christophe Kerelloda141682019-04-05 11:41:50 +0200924 continue;
925
Christophe Kerello6276f862020-07-31 09:53:41 +0200926 addr = dev_read_addr_index(dev, mem_region);
927 if (addr == FDT_ADDR_T_NONE) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100928 dev_err(dev, "Resource data_base not found for cs%d", chip_cs);
Christophe Kerelloda141682019-04-05 11:41:50 +0200929 return ret;
930 }
Christophe Kerello6276f862020-07-31 09:53:41 +0200931 nfc->data_base[chip_cs] = addr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200932
Christophe Kerello6276f862020-07-31 09:53:41 +0200933 addr = dev_read_addr_index(dev, mem_region + 1);
934 if (addr == FDT_ADDR_T_NONE) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100935 dev_err(dev, "Resource cmd_base not found for cs%d", chip_cs);
Christophe Kerelloda141682019-04-05 11:41:50 +0200936 return ret;
937 }
Christophe Kerello6276f862020-07-31 09:53:41 +0200938 nfc->cmd_base[chip_cs] = addr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200939
Christophe Kerello6276f862020-07-31 09:53:41 +0200940 addr = dev_read_addr_index(dev, mem_region + 2);
941 if (addr == FDT_ADDR_T_NONE) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100942 dev_err(dev, "Resource addr_base not found for cs%d", chip_cs);
Christophe Kerelloda141682019-04-05 11:41:50 +0200943 return ret;
944 }
Christophe Kerello6276f862020-07-31 09:53:41 +0200945 nfc->addr_base[chip_cs] = addr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200946 }
947
948 /* Enable the clock */
Christophe Kerello6276f862020-07-31 09:53:41 +0200949 ret = clk_get_by_index(cdev, 0, &nfc->clk);
Christophe Kerelloda141682019-04-05 11:41:50 +0200950 if (ret)
951 return ret;
952
Christophe Kerellod1a25872020-07-31 09:53:37 +0200953 ret = clk_enable(&nfc->clk);
Christophe Kerelloda141682019-04-05 11:41:50 +0200954 if (ret)
955 return ret;
956
957 /* Reset */
958 ret = reset_get_by_index(dev, 0, &reset);
959 if (!ret) {
960 reset_assert(&reset);
961 udelay(2);
962 reset_deassert(&reset);
963 }
964
Christophe Kerello6276f862020-07-31 09:53:41 +0200965 stm32_fmc2_nfc_init(nfc, dev != cdev);
Christophe Kerelloda141682019-04-05 11:41:50 +0200966
Christophe Kerellod1a25872020-07-31 09:53:37 +0200967 chip->controller = &nfc->base;
968 chip->select_chip = stm32_fmc2_nfc_select_chip;
969 chip->setup_data_interface = stm32_fmc2_nfc_setup_interface;
970 chip->cmd_ctrl = stm32_fmc2_nfc_cmd_ctrl;
Christophe Kerelloda141682019-04-05 11:41:50 +0200971 chip->chip_delay = FMC2_RB_DELAY_US;
972 chip->options |= NAND_BUSWIDTH_AUTO | NAND_NO_SUBPAGE_WRITE |
973 NAND_USE_BOUNCE_BUFFER;
974
975 /* Default ECC settings */
976 chip->ecc.mode = NAND_ECC_HW;
977 chip->ecc.size = FMC2_ECC_STEP_SIZE;
978 chip->ecc.strength = FMC2_ECC_BCH8;
979
Christophe Kerelloe389a152022-02-22 17:38:49 +0100980 /* Disable Write Protect */
981 if (dm_gpio_is_valid(&nand->wp_gpio))
982 dm_gpio_set_value(&nand->wp_gpio, 0);
983
Christophe Kerelloda141682019-04-05 11:41:50 +0200984 ret = nand_scan_ident(mtd, nand->ncs, NULL);
985 if (ret)
986 return ret;
987
988 /*
989 * Only NAND_ECC_HW mode is actually supported
990 * Hamming => ecc.strength = 1
991 * BCH4 => ecc.strength = 4
992 * BCH8 => ecc.strength = 8
993 * ECC sector size = 512
994 */
995 if (chip->ecc.mode != NAND_ECC_HW) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +0100996 dev_err(dev, "Nand_ecc_mode is not well defined in the DT\n");
Christophe Kerelloda141682019-04-05 11:41:50 +0200997 return -EINVAL;
998 }
999
Christophe Kerellod1a25872020-07-31 09:53:37 +02001000 ret = nand_check_ecc_caps(chip, &stm32_fmc2_nfc_ecc_caps,
Christophe Kerelloda141682019-04-05 11:41:50 +02001001 mtd->oobsize - FMC2_BBM_LEN);
1002 if (ret) {
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +01001003 dev_err(dev, "No valid ECC settings set\n");
Christophe Kerelloda141682019-04-05 11:41:50 +02001004 return ret;
1005 }
1006
1007 if (chip->bbt_options & NAND_BBT_USE_FLASH)
1008 chip->bbt_options |= NAND_BBT_NO_OOB;
1009
Christophe Kerellod1a25872020-07-31 09:53:37 +02001010 stm32_fmc2_nfc_nand_callbacks_setup(chip);
Christophe Kerelloda141682019-04-05 11:41:50 +02001011
1012 /* Define ECC layout */
Christophe Kerellod1a25872020-07-31 09:53:37 +02001013 ecclayout = &nfc->ecclayout;
Christophe Kerelloda141682019-04-05 11:41:50 +02001014 ecclayout->eccbytes = chip->ecc.bytes *
1015 (mtd->writesize / chip->ecc.size);
1016 oob_index = FMC2_BBM_LEN;
1017 for (i = 0; i < ecclayout->eccbytes; i++, oob_index++)
1018 ecclayout->eccpos[i] = oob_index;
1019 ecclayout->oobfree->offset = oob_index;
1020 ecclayout->oobfree->length = mtd->oobsize - ecclayout->oobfree->offset;
1021 chip->ecc.layout = ecclayout;
1022
Christophe Kerelloda141682019-04-05 11:41:50 +02001023 if (chip->options & NAND_BUSWIDTH_16)
Christophe Kerellod1a25872020-07-31 09:53:37 +02001024 stm32_fmc2_nfc_set_buswidth_16(nfc, true);
Christophe Kerelloda141682019-04-05 11:41:50 +02001025
Christophe Kerelloda141682019-04-05 11:41:50 +02001026 ret = nand_scan_tail(mtd);
1027 if (ret)
1028 return ret;
1029
1030 return nand_register(0, mtd);
1031}
1032
Christophe Kerellod1a25872020-07-31 09:53:37 +02001033static const struct udevice_id stm32_fmc2_nfc_match[] = {
Christophe Kerelloda141682019-04-05 11:41:50 +02001034 { .compatible = "st,stm32mp15-fmc2" },
Christophe Kerello6276f862020-07-31 09:53:41 +02001035 { .compatible = "st,stm32mp1-fmc2-nfc" },
Christophe Kerelloda141682019-04-05 11:41:50 +02001036 { /* Sentinel */ }
1037};
1038
Christophe Kerellod1a25872020-07-31 09:53:37 +02001039U_BOOT_DRIVER(stm32_fmc2_nfc) = {
1040 .name = "stm32_fmc2_nfc",
Christophe Kerelloda141682019-04-05 11:41:50 +02001041 .id = UCLASS_MTD,
Christophe Kerellod1a25872020-07-31 09:53:37 +02001042 .of_match = stm32_fmc2_nfc_match,
1043 .probe = stm32_fmc2_nfc_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001044 .priv_auto = sizeof(struct stm32_fmc2_nfc),
Christophe Kerelloda141682019-04-05 11:41:50 +02001045};
1046
1047void board_nand_init(void)
1048{
1049 struct udevice *dev;
1050 int ret;
1051
1052 ret = uclass_get_device_by_driver(UCLASS_MTD,
Simon Glass65130cd2020-12-28 20:34:56 -07001053 DM_DRIVER_GET(stm32_fmc2_nfc),
Christophe Kerelloda141682019-04-05 11:41:50 +02001054 &dev);
1055 if (ret && ret != -ENODEV)
Patrick Delaunayb18ccfa2020-11-06 19:01:54 +01001056 log_err("Failed to initialize STM32 FMC2 NFC controller. (error %d)\n",
1057 ret);
Christophe Kerelloda141682019-04-05 11:41:50 +02001058}