blob: 47fe61090da4a6e86925f5917470bfbd9424e3e8 [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
7#include <common.h>
8#include <clk.h>
9#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Christophe Kerelloda141682019-04-05 11:41:50 +020011#include <nand.h>
12#include <reset.h>
Christophe Kerellof4aca872020-07-31 09:53:38 +020013#include <linux/bitfield.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060014#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070016#include <linux/err.h>
Christophe Kerelloda141682019-04-05 11:41:50 +020017#include <linux/iopoll.h>
18#include <linux/ioport.h>
19
20/* Bad block marker length */
21#define FMC2_BBM_LEN 2
22
23/* ECC step size */
24#define FMC2_ECC_STEP_SIZE 512
25
26/* Command delay */
27#define FMC2_RB_DELAY_US 30
28
29/* Max chip enable */
30#define FMC2_MAX_CE 2
31
32/* Timings */
33#define FMC2_THIZ 1
34#define FMC2_TIO 8000
35#define FMC2_TSYNC 3000
36#define FMC2_PCR_TIMING_MASK 0xf
37#define FMC2_PMEM_PATT_TIMING_MASK 0xff
38
39/* FMC2 Controller Registers */
40#define FMC2_BCR1 0x0
41#define FMC2_PCR 0x80
42#define FMC2_SR 0x84
43#define FMC2_PMEM 0x88
44#define FMC2_PATT 0x8c
45#define FMC2_HECCR 0x94
46#define FMC2_BCHISR 0x254
47#define FMC2_BCHICR 0x258
48#define FMC2_BCHPBR1 0x260
49#define FMC2_BCHPBR2 0x264
50#define FMC2_BCHPBR3 0x268
51#define FMC2_BCHPBR4 0x26c
52#define FMC2_BCHDSR0 0x27c
53#define FMC2_BCHDSR1 0x280
54#define FMC2_BCHDSR2 0x284
55#define FMC2_BCHDSR3 0x288
56#define FMC2_BCHDSR4 0x28c
57
58/* Register: FMC2_BCR1 */
59#define FMC2_BCR1_FMC2EN BIT(31)
60
61/* Register: FMC2_PCR */
62#define FMC2_PCR_PWAITEN BIT(1)
63#define FMC2_PCR_PBKEN BIT(2)
Christophe Kerellof4aca872020-07-31 09:53:38 +020064#define FMC2_PCR_PWID GENMASK(5, 4)
Christophe Kerelloda141682019-04-05 11:41:50 +020065#define FMC2_PCR_PWID_BUSWIDTH_8 0
66#define FMC2_PCR_PWID_BUSWIDTH_16 1
67#define FMC2_PCR_ECCEN BIT(6)
68#define FMC2_PCR_ECCALG BIT(8)
Christophe Kerellof4aca872020-07-31 09:53:38 +020069#define FMC2_PCR_TCLR GENMASK(12, 9)
Christophe Kerelloda141682019-04-05 11:41:50 +020070#define FMC2_PCR_TCLR_DEFAULT 0xf
Christophe Kerellof4aca872020-07-31 09:53:38 +020071#define FMC2_PCR_TAR GENMASK(16, 13)
Christophe Kerelloda141682019-04-05 11:41:50 +020072#define FMC2_PCR_TAR_DEFAULT 0xf
Christophe Kerellof4aca872020-07-31 09:53:38 +020073#define FMC2_PCR_ECCSS GENMASK(19, 17)
Christophe Kerelloda141682019-04-05 11:41:50 +020074#define FMC2_PCR_ECCSS_512 1
75#define FMC2_PCR_ECCSS_2048 3
76#define FMC2_PCR_BCHECC BIT(24)
77#define FMC2_PCR_WEN BIT(25)
78
79/* Register: FMC2_SR */
80#define FMC2_SR_NWRF BIT(6)
81
82/* Register: FMC2_PMEM */
Christophe Kerellof4aca872020-07-31 09:53:38 +020083#define FMC2_PMEM_MEMSET GENMASK(7, 0)
84#define FMC2_PMEM_MEMWAIT GENMASK(15, 8)
85#define FMC2_PMEM_MEMHOLD GENMASK(23, 16)
86#define FMC2_PMEM_MEMHIZ GENMASK(31, 24)
Christophe Kerelloda141682019-04-05 11:41:50 +020087#define FMC2_PMEM_DEFAULT 0x0a0a0a0a
88
89/* Register: FMC2_PATT */
Christophe Kerellof4aca872020-07-31 09:53:38 +020090#define FMC2_PATT_ATTSET GENMASK(7, 0)
91#define FMC2_PATT_ATTWAIT GENMASK(15, 8)
92#define FMC2_PATT_ATTHOLD GENMASK(23, 16)
93#define FMC2_PATT_ATTHIZ GENMASK(31, 24)
Christophe Kerelloda141682019-04-05 11:41:50 +020094#define FMC2_PATT_DEFAULT 0x0a0a0a0a
95
96/* Register: FMC2_BCHISR */
97#define FMC2_BCHISR_DERF BIT(1)
98#define FMC2_BCHISR_EPBRF BIT(4)
99
100/* Register: FMC2_BCHICR */
101#define FMC2_BCHICR_CLEAR_IRQ GENMASK(4, 0)
102
103/* Register: FMC2_BCHDSR0 */
104#define FMC2_BCHDSR0_DUE BIT(0)
105#define FMC2_BCHDSR0_DEF BIT(1)
Christophe Kerellof4aca872020-07-31 09:53:38 +0200106#define FMC2_BCHDSR0_DEN GENMASK(7, 4)
Christophe Kerelloda141682019-04-05 11:41:50 +0200107
108/* Register: FMC2_BCHDSR1 */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200109#define FMC2_BCHDSR1_EBP1 GENMASK(12, 0)
110#define FMC2_BCHDSR1_EBP2 GENMASK(28, 16)
Christophe Kerelloda141682019-04-05 11:41:50 +0200111
112/* Register: FMC2_BCHDSR2 */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200113#define FMC2_BCHDSR2_EBP3 GENMASK(12, 0)
114#define FMC2_BCHDSR2_EBP4 GENMASK(28, 16)
Christophe Kerelloda141682019-04-05 11:41:50 +0200115
116/* Register: FMC2_BCHDSR3 */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200117#define FMC2_BCHDSR3_EBP5 GENMASK(12, 0)
118#define FMC2_BCHDSR3_EBP6 GENMASK(28, 16)
Christophe Kerelloda141682019-04-05 11:41:50 +0200119
120/* Register: FMC2_BCHDSR4 */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200121#define FMC2_BCHDSR4_EBP7 GENMASK(12, 0)
122#define FMC2_BCHDSR4_EBP8 GENMASK(28, 16)
Christophe Kerelloda141682019-04-05 11:41:50 +0200123
124#define FMC2_NSEC_PER_SEC 1000000000L
125
Christophe Kerello92693e32020-07-31 09:53:36 +0200126#define FMC2_TIMEOUT_5S 5000000
127
Christophe Kerelloda141682019-04-05 11:41:50 +0200128enum stm32_fmc2_ecc {
129 FMC2_ECC_HAM = 1,
130 FMC2_ECC_BCH4 = 4,
131 FMC2_ECC_BCH8 = 8
132};
133
134struct stm32_fmc2_timings {
135 u8 tclr;
136 u8 tar;
137 u8 thiz;
138 u8 twait;
139 u8 thold_mem;
140 u8 tset_mem;
141 u8 thold_att;
142 u8 tset_att;
143};
144
145struct stm32_fmc2_nand {
146 struct nand_chip chip;
147 struct stm32_fmc2_timings timings;
148 int ncs;
149 int cs_used[FMC2_MAX_CE];
150};
151
152static inline struct stm32_fmc2_nand *to_fmc2_nand(struct nand_chip *chip)
153{
154 return container_of(chip, struct stm32_fmc2_nand, chip);
155}
156
157struct stm32_fmc2_nfc {
158 struct nand_hw_control base;
159 struct stm32_fmc2_nand nand;
160 struct nand_ecclayout ecclayout;
Christophe Kerello6276f862020-07-31 09:53:41 +0200161 fdt_addr_t io_base;
162 fdt_addr_t data_base[FMC2_MAX_CE];
163 fdt_addr_t cmd_base[FMC2_MAX_CE];
164 fdt_addr_t addr_base[FMC2_MAX_CE];
Christophe Kerelloda141682019-04-05 11:41:50 +0200165 struct clk clk;
166
167 u8 cs_assigned;
168 int cs_sel;
169};
170
171static inline struct stm32_fmc2_nfc *to_stm32_nfc(struct nand_hw_control *base)
172{
173 return container_of(base, struct stm32_fmc2_nfc, base);
174}
175
Christophe Kerellod1a25872020-07-31 09:53:37 +0200176static void stm32_fmc2_nfc_timings_init(struct nand_chip *chip)
Christophe Kerelloda141682019-04-05 11:41:50 +0200177{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200178 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200179 struct stm32_fmc2_nand *nand = to_fmc2_nand(chip);
180 struct stm32_fmc2_timings *timings = &nand->timings;
Christophe Kerelloda141682019-04-05 11:41:50 +0200181 u32 pmem, patt;
182
183 /* Set tclr/tar timings */
Christophe Kerello9de081d2020-07-31 09:53:39 +0200184 clrsetbits_le32(nfc->io_base + FMC2_PCR,
185 FMC2_PCR_TCLR | FMC2_PCR_TAR,
186 FIELD_PREP(FMC2_PCR_TCLR, timings->tclr) |
187 FIELD_PREP(FMC2_PCR_TAR, timings->tar));
Christophe Kerelloda141682019-04-05 11:41:50 +0200188
189 /* Set tset/twait/thold/thiz timings in common bank */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200190 pmem = FIELD_PREP(FMC2_PMEM_MEMSET, timings->tset_mem);
191 pmem |= FIELD_PREP(FMC2_PMEM_MEMWAIT, timings->twait);
192 pmem |= FIELD_PREP(FMC2_PMEM_MEMHOLD, timings->thold_mem);
193 pmem |= FIELD_PREP(FMC2_PMEM_MEMHIZ, timings->thiz);
Christophe Kerello9de081d2020-07-31 09:53:39 +0200194 writel(pmem, nfc->io_base + FMC2_PMEM);
Christophe Kerelloda141682019-04-05 11:41:50 +0200195
196 /* Set tset/twait/thold/thiz timings in attribut bank */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200197 patt = FIELD_PREP(FMC2_PATT_ATTSET, timings->tset_att);
198 patt |= FIELD_PREP(FMC2_PATT_ATTWAIT, timings->twait);
199 patt |= FIELD_PREP(FMC2_PATT_ATTHOLD, timings->thold_att);
200 patt |= FIELD_PREP(FMC2_PATT_ATTHIZ, timings->thiz);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200201 writel(patt, nfc->io_base + FMC2_PATT);
Christophe Kerelloda141682019-04-05 11:41:50 +0200202}
203
Christophe Kerellod1a25872020-07-31 09:53:37 +0200204static void stm32_fmc2_nfc_setup(struct nand_chip *chip)
Christophe Kerelloda141682019-04-05 11:41:50 +0200205{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200206 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerello9de081d2020-07-31 09:53:39 +0200207 u32 pcr = 0, pcr_mask;
Christophe Kerelloda141682019-04-05 11:41:50 +0200208
209 /* Configure ECC algorithm (default configuration is Hamming) */
Christophe Kerello9de081d2020-07-31 09:53:39 +0200210 pcr_mask = FMC2_PCR_ECCALG;
211 pcr_mask |= FMC2_PCR_BCHECC;
Christophe Kerelloda141682019-04-05 11:41:50 +0200212 if (chip->ecc.strength == FMC2_ECC_BCH8) {
213 pcr |= FMC2_PCR_ECCALG;
214 pcr |= FMC2_PCR_BCHECC;
215 } else if (chip->ecc.strength == FMC2_ECC_BCH4) {
216 pcr |= FMC2_PCR_ECCALG;
217 }
218
219 /* Set buswidth */
Christophe Kerello9de081d2020-07-31 09:53:39 +0200220 pcr_mask |= FMC2_PCR_PWID;
Christophe Kerelloda141682019-04-05 11:41:50 +0200221 if (chip->options & NAND_BUSWIDTH_16)
Christophe Kerellof4aca872020-07-31 09:53:38 +0200222 pcr |= FIELD_PREP(FMC2_PCR_PWID, FMC2_PCR_PWID_BUSWIDTH_16);
Christophe Kerelloda141682019-04-05 11:41:50 +0200223
224 /* Set ECC sector size */
Christophe Kerello9de081d2020-07-31 09:53:39 +0200225 pcr_mask |= FMC2_PCR_ECCSS;
Christophe Kerellof4aca872020-07-31 09:53:38 +0200226 pcr |= FIELD_PREP(FMC2_PCR_ECCSS, FMC2_PCR_ECCSS_512);
Christophe Kerelloda141682019-04-05 11:41:50 +0200227
Christophe Kerello9de081d2020-07-31 09:53:39 +0200228 clrsetbits_le32(nfc->io_base + FMC2_PCR, pcr_mask, pcr);
Christophe Kerelloda141682019-04-05 11:41:50 +0200229}
230
Christophe Kerellod1a25872020-07-31 09:53:37 +0200231static void stm32_fmc2_nfc_select_chip(struct mtd_info *mtd, int chipnr)
Christophe Kerelloda141682019-04-05 11:41:50 +0200232{
233 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200234 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200235 struct stm32_fmc2_nand *nand = to_fmc2_nand(chip);
236
237 if (chipnr < 0 || chipnr >= nand->ncs)
238 return;
239
Christophe Kerellod1a25872020-07-31 09:53:37 +0200240 if (nand->cs_used[chipnr] == nfc->cs_sel)
Christophe Kerelloda141682019-04-05 11:41:50 +0200241 return;
242
Christophe Kerellod1a25872020-07-31 09:53:37 +0200243 nfc->cs_sel = nand->cs_used[chipnr];
Christophe Kerello6276f862020-07-31 09:53:41 +0200244 chip->IO_ADDR_R = (void __iomem *)nfc->data_base[nfc->cs_sel];
245 chip->IO_ADDR_W = (void __iomem *)nfc->data_base[nfc->cs_sel];
Christophe Kerelloda141682019-04-05 11:41:50 +0200246
Christophe Kerellod1a25872020-07-31 09:53:37 +0200247 stm32_fmc2_nfc_setup(chip);
248 stm32_fmc2_nfc_timings_init(chip);
Christophe Kerelloda141682019-04-05 11:41:50 +0200249}
250
Christophe Kerellod1a25872020-07-31 09:53:37 +0200251static void stm32_fmc2_nfc_set_buswidth_16(struct stm32_fmc2_nfc *nfc,
252 bool set)
Christophe Kerelloda141682019-04-05 11:41:50 +0200253{
Christophe Kerello9de081d2020-07-31 09:53:39 +0200254 u32 pcr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200255
Christophe Kerello9de081d2020-07-31 09:53:39 +0200256 pcr = set ? FIELD_PREP(FMC2_PCR_PWID, FMC2_PCR_PWID_BUSWIDTH_16) :
257 FIELD_PREP(FMC2_PCR_PWID, FMC2_PCR_PWID_BUSWIDTH_8);
258
259 clrsetbits_le32(nfc->io_base + FMC2_PCR, FMC2_PCR_PWID, pcr);
Christophe Kerelloda141682019-04-05 11:41:50 +0200260}
261
Christophe Kerellod1a25872020-07-31 09:53:37 +0200262static void stm32_fmc2_nfc_set_ecc(struct stm32_fmc2_nfc *nfc, bool enable)
Christophe Kerelloda141682019-04-05 11:41:50 +0200263{
Christophe Kerello9de081d2020-07-31 09:53:39 +0200264 clrsetbits_le32(nfc->io_base + FMC2_PCR, FMC2_PCR_ECCEN,
265 enable ? FMC2_PCR_ECCEN : 0);
Christophe Kerelloda141682019-04-05 11:41:50 +0200266}
267
Christophe Kerellod1a25872020-07-31 09:53:37 +0200268static void stm32_fmc2_nfc_clear_bch_irq(struct stm32_fmc2_nfc *nfc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200269{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200270 writel(FMC2_BCHICR_CLEAR_IRQ, nfc->io_base + FMC2_BCHICR);
Christophe Kerelloda141682019-04-05 11:41:50 +0200271}
272
Christophe Kerellod1a25872020-07-31 09:53:37 +0200273static void stm32_fmc2_nfc_cmd_ctrl(struct mtd_info *mtd, int cmd,
274 unsigned int ctrl)
Christophe Kerelloda141682019-04-05 11:41:50 +0200275{
276 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200277 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200278
279 if (cmd == NAND_CMD_NONE)
280 return;
281
282 if (ctrl & NAND_CLE) {
Christophe Kerellod1a25872020-07-31 09:53:37 +0200283 writeb(cmd, nfc->cmd_base[nfc->cs_sel]);
Christophe Kerelloda141682019-04-05 11:41:50 +0200284 return;
285 }
286
Christophe Kerellod1a25872020-07-31 09:53:37 +0200287 writeb(cmd, nfc->addr_base[nfc->cs_sel]);
Christophe Kerelloda141682019-04-05 11:41:50 +0200288}
289
290/*
291 * Enable ECC logic and reset syndrome/parity bits previously calculated
292 * Syndrome/parity bits is cleared by setting the ECCEN bit to 0
293 */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200294static void stm32_fmc2_nfc_hwctl(struct mtd_info *mtd, int mode)
Christophe Kerelloda141682019-04-05 11:41:50 +0200295{
296 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200297 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200298
Christophe Kerellod1a25872020-07-31 09:53:37 +0200299 stm32_fmc2_nfc_set_ecc(nfc, false);
Christophe Kerelloda141682019-04-05 11:41:50 +0200300
301 if (chip->ecc.strength != FMC2_ECC_HAM) {
Christophe Kerello9de081d2020-07-31 09:53:39 +0200302 clrsetbits_le32(nfc->io_base + FMC2_PCR, FMC2_PCR_WEN,
303 mode == NAND_ECC_WRITE ? FMC2_PCR_WEN : 0);
Christophe Kerelloda141682019-04-05 11:41:50 +0200304
Christophe Kerellod1a25872020-07-31 09:53:37 +0200305 stm32_fmc2_nfc_clear_bch_irq(nfc);
Christophe Kerelloda141682019-04-05 11:41:50 +0200306 }
307
Christophe Kerellod1a25872020-07-31 09:53:37 +0200308 stm32_fmc2_nfc_set_ecc(nfc, true);
Christophe Kerelloda141682019-04-05 11:41:50 +0200309}
310
311/*
312 * ECC Hamming calculation
313 * ECC is 3 bytes for 512 bytes of data (supports error correction up to
314 * max of 1-bit)
315 */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200316static int stm32_fmc2_nfc_ham_calculate(struct mtd_info *mtd, const u8 *data,
317 u8 *ecc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200318{
319 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200320 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200321 u32 heccr, sr;
322 int ret;
323
Christophe Kerellod1a25872020-07-31 09:53:37 +0200324 ret = readl_poll_timeout(nfc->io_base + FMC2_SR, sr,
Christophe Kerello92693e32020-07-31 09:53:36 +0200325 sr & FMC2_SR_NWRF, FMC2_TIMEOUT_5S);
Christophe Kerelloda141682019-04-05 11:41:50 +0200326 if (ret < 0) {
327 pr_err("Ham timeout\n");
328 return ret;
329 }
330
Christophe Kerellod1a25872020-07-31 09:53:37 +0200331 heccr = readl(nfc->io_base + FMC2_HECCR);
Christophe Kerelloda141682019-04-05 11:41:50 +0200332
333 ecc[0] = heccr;
334 ecc[1] = heccr >> 8;
335 ecc[2] = heccr >> 16;
336
Christophe Kerellod1a25872020-07-31 09:53:37 +0200337 stm32_fmc2_nfc_set_ecc(nfc, false);
Christophe Kerelloda141682019-04-05 11:41:50 +0200338
339 return 0;
340}
341
Christophe Kerellod1a25872020-07-31 09:53:37 +0200342static int stm32_fmc2_nfc_ham_correct(struct mtd_info *mtd, u8 *dat,
343 u8 *read_ecc, u8 *calc_ecc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200344{
345 u8 bit_position = 0, b0, b1, b2;
346 u32 byte_addr = 0, b;
347 u32 i, shifting = 1;
348
349 /* Indicate which bit and byte is faulty (if any) */
350 b0 = read_ecc[0] ^ calc_ecc[0];
351 b1 = read_ecc[1] ^ calc_ecc[1];
352 b2 = read_ecc[2] ^ calc_ecc[2];
353 b = b0 | (b1 << 8) | (b2 << 16);
354
355 /* No errors */
356 if (likely(!b))
357 return 0;
358
359 /* Calculate bit position */
360 for (i = 0; i < 3; i++) {
361 switch (b % 4) {
362 case 2:
363 bit_position += shifting;
364 case 1:
365 break;
366 default:
367 return -EBADMSG;
368 }
369 shifting <<= 1;
370 b >>= 2;
371 }
372
373 /* Calculate byte position */
374 shifting = 1;
375 for (i = 0; i < 9; i++) {
376 switch (b % 4) {
377 case 2:
378 byte_addr += shifting;
379 case 1:
380 break;
381 default:
382 return -EBADMSG;
383 }
384 shifting <<= 1;
385 b >>= 2;
386 }
387
388 /* Flip the bit */
389 dat[byte_addr] ^= (1 << bit_position);
390
391 return 1;
392}
393
394/*
395 * ECC BCH calculation and correction
396 * ECC is 7/13 bytes for 512 bytes of data (supports error correction up to
397 * max of 4-bit/8-bit)
398 */
399
Christophe Kerellod1a25872020-07-31 09:53:37 +0200400static int stm32_fmc2_nfc_bch_calculate(struct mtd_info *mtd, const u8 *data,
401 u8 *ecc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200402{
403 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200404 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200405 u32 bchpbr, bchisr;
406 int ret;
407
408 /* Wait until the BCH code is ready */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200409 ret = readl_poll_timeout(nfc->io_base + FMC2_BCHISR, bchisr,
Christophe Kerello92693e32020-07-31 09:53:36 +0200410 bchisr & FMC2_BCHISR_EPBRF, FMC2_TIMEOUT_5S);
Christophe Kerelloda141682019-04-05 11:41:50 +0200411 if (ret < 0) {
412 pr_err("Bch timeout\n");
413 return ret;
414 }
415
416 /* Read parity bits */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200417 bchpbr = readl(nfc->io_base + FMC2_BCHPBR1);
Christophe Kerelloda141682019-04-05 11:41:50 +0200418 ecc[0] = bchpbr;
419 ecc[1] = bchpbr >> 8;
420 ecc[2] = bchpbr >> 16;
421 ecc[3] = bchpbr >> 24;
422
Christophe Kerellod1a25872020-07-31 09:53:37 +0200423 bchpbr = readl(nfc->io_base + FMC2_BCHPBR2);
Christophe Kerelloda141682019-04-05 11:41:50 +0200424 ecc[4] = bchpbr;
425 ecc[5] = bchpbr >> 8;
426 ecc[6] = bchpbr >> 16;
427
428 if (chip->ecc.strength == FMC2_ECC_BCH8) {
429 ecc[7] = bchpbr >> 24;
430
Christophe Kerellod1a25872020-07-31 09:53:37 +0200431 bchpbr = readl(nfc->io_base + FMC2_BCHPBR3);
Christophe Kerelloda141682019-04-05 11:41:50 +0200432 ecc[8] = bchpbr;
433 ecc[9] = bchpbr >> 8;
434 ecc[10] = bchpbr >> 16;
435 ecc[11] = bchpbr >> 24;
436
Christophe Kerellod1a25872020-07-31 09:53:37 +0200437 bchpbr = readl(nfc->io_base + FMC2_BCHPBR4);
Christophe Kerelloda141682019-04-05 11:41:50 +0200438 ecc[12] = bchpbr;
439 }
440
Christophe Kerellod1a25872020-07-31 09:53:37 +0200441 stm32_fmc2_nfc_set_ecc(nfc, false);
Christophe Kerelloda141682019-04-05 11:41:50 +0200442
443 return 0;
444}
445
Christophe Kerellod1a25872020-07-31 09:53:37 +0200446static int stm32_fmc2_nfc_bch_correct(struct mtd_info *mtd, u8 *dat,
447 u8 *read_ecc, u8 *calc_ecc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200448{
449 struct nand_chip *chip = mtd_to_nand(mtd);
Christophe Kerellod1a25872020-07-31 09:53:37 +0200450 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200451 u32 bchdsr0, bchdsr1, bchdsr2, bchdsr3, bchdsr4, bchisr;
452 u16 pos[8];
453 int i, ret, den, eccsize = chip->ecc.size;
454 unsigned int nb_errs = 0;
455
456 /* Wait until the decoding error is ready */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200457 ret = readl_poll_timeout(nfc->io_base + FMC2_BCHISR, bchisr,
Christophe Kerello92693e32020-07-31 09:53:36 +0200458 bchisr & FMC2_BCHISR_DERF, FMC2_TIMEOUT_5S);
Christophe Kerelloda141682019-04-05 11:41:50 +0200459 if (ret < 0) {
460 pr_err("Bch timeout\n");
461 return ret;
462 }
463
Christophe Kerellod1a25872020-07-31 09:53:37 +0200464 bchdsr0 = readl(nfc->io_base + FMC2_BCHDSR0);
465 bchdsr1 = readl(nfc->io_base + FMC2_BCHDSR1);
466 bchdsr2 = readl(nfc->io_base + FMC2_BCHDSR2);
467 bchdsr3 = readl(nfc->io_base + FMC2_BCHDSR3);
468 bchdsr4 = readl(nfc->io_base + FMC2_BCHDSR4);
Christophe Kerelloda141682019-04-05 11:41:50 +0200469
Christophe Kerellod1a25872020-07-31 09:53:37 +0200470 stm32_fmc2_nfc_set_ecc(nfc, false);
Christophe Kerelloda141682019-04-05 11:41:50 +0200471
472 /* No errors found */
473 if (likely(!(bchdsr0 & FMC2_BCHDSR0_DEF)))
474 return 0;
475
476 /* Too many errors detected */
477 if (unlikely(bchdsr0 & FMC2_BCHDSR0_DUE))
478 return -EBADMSG;
479
Christophe Kerellof4aca872020-07-31 09:53:38 +0200480 pos[0] = FIELD_GET(FMC2_BCHDSR1_EBP1, bchdsr1);
481 pos[1] = FIELD_GET(FMC2_BCHDSR1_EBP2, bchdsr1);
482 pos[2] = FIELD_GET(FMC2_BCHDSR2_EBP3, bchdsr2);
483 pos[3] = FIELD_GET(FMC2_BCHDSR2_EBP4, bchdsr2);
484 pos[4] = FIELD_GET(FMC2_BCHDSR3_EBP5, bchdsr3);
485 pos[5] = FIELD_GET(FMC2_BCHDSR3_EBP6, bchdsr3);
486 pos[6] = FIELD_GET(FMC2_BCHDSR4_EBP7, bchdsr4);
487 pos[7] = FIELD_GET(FMC2_BCHDSR4_EBP8, bchdsr4);
Christophe Kerelloda141682019-04-05 11:41:50 +0200488
Christophe Kerellof4aca872020-07-31 09:53:38 +0200489 den = FIELD_GET(FMC2_BCHDSR0_DEN, bchdsr0);
Christophe Kerelloda141682019-04-05 11:41:50 +0200490 for (i = 0; i < den; i++) {
491 if (pos[i] < eccsize * 8) {
492 __change_bit(pos[i], (unsigned long *)dat);
493 nb_errs++;
494 }
495 }
496
497 return nb_errs;
498}
499
Christophe Kerellod1a25872020-07-31 09:53:37 +0200500static int stm32_fmc2_nfc_read_page(struct mtd_info *mtd,
501 struct nand_chip *chip, u8 *buf,
502 int oob_required, int page)
Christophe Kerelloda141682019-04-05 11:41:50 +0200503{
504 int i, s, stat, eccsize = chip->ecc.size;
505 int eccbytes = chip->ecc.bytes;
506 int eccsteps = chip->ecc.steps;
507 int eccstrength = chip->ecc.strength;
508 u8 *p = buf;
509 u8 *ecc_calc = chip->buffers->ecccalc;
510 u8 *ecc_code = chip->buffers->ecccode;
511 unsigned int max_bitflips = 0;
512
513 for (i = mtd->writesize + FMC2_BBM_LEN, s = 0; s < eccsteps;
514 s++, i += eccbytes, p += eccsize) {
515 chip->ecc.hwctl(mtd, NAND_ECC_READ);
516
517 /* Read the nand page sector (512 bytes) */
518 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, s * eccsize, -1);
519 chip->read_buf(mtd, p, eccsize);
520
521 /* Read the corresponding ECC bytes */
522 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, i, -1);
523 chip->read_buf(mtd, ecc_code, eccbytes);
524
525 /* Correct the data */
526 stat = chip->ecc.correct(mtd, p, ecc_code, ecc_calc);
527 if (stat == -EBADMSG)
528 /* Check for empty pages with bitflips */
529 stat = nand_check_erased_ecc_chunk(p, eccsize,
530 ecc_code, eccbytes,
531 NULL, 0,
532 eccstrength);
533
534 if (stat < 0) {
535 mtd->ecc_stats.failed++;
536 } else {
537 mtd->ecc_stats.corrected += stat;
538 max_bitflips = max_t(unsigned int, max_bitflips, stat);
539 }
540 }
541
542 /* Read oob */
543 if (oob_required) {
544 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
545 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
546 }
547
548 return max_bitflips;
549}
550
Christophe Kerello6276f862020-07-31 09:53:41 +0200551static void stm32_fmc2_nfc_init(struct stm32_fmc2_nfc *nfc, bool has_parent)
Christophe Kerelloda141682019-04-05 11:41:50 +0200552{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200553 u32 pcr = readl(nfc->io_base + FMC2_PCR);
Christophe Kerelloda141682019-04-05 11:41:50 +0200554
555 /* Set CS used to undefined */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200556 nfc->cs_sel = -1;
Christophe Kerelloda141682019-04-05 11:41:50 +0200557
558 /* Enable wait feature and nand flash memory bank */
559 pcr |= FMC2_PCR_PWAITEN;
560 pcr |= FMC2_PCR_PBKEN;
561
562 /* Set buswidth to 8 bits mode for identification */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200563 pcr &= ~FMC2_PCR_PWID;
Christophe Kerelloda141682019-04-05 11:41:50 +0200564
565 /* ECC logic is disabled */
566 pcr &= ~FMC2_PCR_ECCEN;
567
568 /* Default mode */
569 pcr &= ~FMC2_PCR_ECCALG;
570 pcr &= ~FMC2_PCR_BCHECC;
571 pcr &= ~FMC2_PCR_WEN;
572
573 /* Set default ECC sector size */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200574 pcr &= ~FMC2_PCR_ECCSS;
575 pcr |= FIELD_PREP(FMC2_PCR_ECCSS, FMC2_PCR_ECCSS_2048);
Christophe Kerelloda141682019-04-05 11:41:50 +0200576
577 /* Set default tclr/tar timings */
Christophe Kerellof4aca872020-07-31 09:53:38 +0200578 pcr &= ~FMC2_PCR_TCLR;
579 pcr |= FIELD_PREP(FMC2_PCR_TCLR, FMC2_PCR_TCLR_DEFAULT);
580 pcr &= ~FMC2_PCR_TAR;
581 pcr |= FIELD_PREP(FMC2_PCR_TAR, FMC2_PCR_TAR_DEFAULT);
Christophe Kerelloda141682019-04-05 11:41:50 +0200582
583 /* Enable FMC2 controller */
Christophe Kerello6276f862020-07-31 09:53:41 +0200584 if (!has_parent)
585 setbits_le32(nfc->io_base + FMC2_BCR1, FMC2_BCR1_FMC2EN);
Christophe Kerelloda141682019-04-05 11:41:50 +0200586
Christophe Kerellod1a25872020-07-31 09:53:37 +0200587 writel(pcr, nfc->io_base + FMC2_PCR);
588 writel(FMC2_PMEM_DEFAULT, nfc->io_base + FMC2_PMEM);
589 writel(FMC2_PATT_DEFAULT, nfc->io_base + FMC2_PATT);
Christophe Kerelloda141682019-04-05 11:41:50 +0200590}
591
Christophe Kerellod1a25872020-07-31 09:53:37 +0200592static void stm32_fmc2_nfc_calc_timings(struct nand_chip *chip,
593 const struct nand_sdr_timings *sdrt)
Christophe Kerelloda141682019-04-05 11:41:50 +0200594{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200595 struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller);
Christophe Kerelloda141682019-04-05 11:41:50 +0200596 struct stm32_fmc2_nand *nand = to_fmc2_nand(chip);
597 struct stm32_fmc2_timings *tims = &nand->timings;
Christophe Kerellod1a25872020-07-31 09:53:37 +0200598 unsigned long hclk = clk_get_rate(&nfc->clk);
Christophe Kerelloda141682019-04-05 11:41:50 +0200599 unsigned long hclkp = FMC2_NSEC_PER_SEC / (hclk / 1000);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200600 unsigned long timing, tar, tclr, thiz, twait;
601 unsigned long tset_mem, tset_att, thold_mem, thold_att;
Christophe Kerelloda141682019-04-05 11:41:50 +0200602
Patrick Delaunay804858a2019-06-21 15:26:54 +0200603 tar = max_t(unsigned long, hclkp, sdrt->tAR_min);
604 timing = DIV_ROUND_UP(tar, hclkp) - 1;
605 tims->tar = min_t(unsigned long, timing, FMC2_PCR_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200606
Patrick Delaunay804858a2019-06-21 15:26:54 +0200607 tclr = max_t(unsigned long, hclkp, sdrt->tCLR_min);
608 timing = DIV_ROUND_UP(tclr, hclkp) - 1;
609 tims->tclr = min_t(unsigned long, timing, FMC2_PCR_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200610
611 tims->thiz = FMC2_THIZ;
612 thiz = (tims->thiz + 1) * hclkp;
613
614 /*
615 * tWAIT > tRP
616 * tWAIT > tWP
617 * tWAIT > tREA + tIO
618 */
Patrick Delaunay804858a2019-06-21 15:26:54 +0200619 twait = max_t(unsigned long, hclkp, sdrt->tRP_min);
620 twait = max_t(unsigned long, twait, sdrt->tWP_min);
621 twait = max_t(unsigned long, twait, sdrt->tREA_max + FMC2_TIO);
622 timing = DIV_ROUND_UP(twait, hclkp);
623 tims->twait = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200624
625 /*
626 * tSETUP_MEM > tCS - tWAIT
627 * tSETUP_MEM > tALS - tWAIT
628 * tSETUP_MEM > tDS - (tWAIT - tHIZ)
629 */
630 tset_mem = hclkp;
631 if (sdrt->tCS_min > twait && (tset_mem < sdrt->tCS_min - twait))
632 tset_mem = sdrt->tCS_min - twait;
633 if (sdrt->tALS_min > twait && (tset_mem < sdrt->tALS_min - twait))
634 tset_mem = sdrt->tALS_min - twait;
635 if (twait > thiz && (sdrt->tDS_min > twait - thiz) &&
636 (tset_mem < sdrt->tDS_min - (twait - thiz)))
637 tset_mem = sdrt->tDS_min - (twait - thiz);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200638 timing = DIV_ROUND_UP(tset_mem, hclkp);
639 tims->tset_mem = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200640
641 /*
642 * tHOLD_MEM > tCH
643 * tHOLD_MEM > tREH - tSETUP_MEM
644 * tHOLD_MEM > max(tRC, tWC) - (tSETUP_MEM + tWAIT)
645 */
Patrick Delaunay804858a2019-06-21 15:26:54 +0200646 thold_mem = max_t(unsigned long, hclkp, sdrt->tCH_min);
Christophe Kerelloda141682019-04-05 11:41:50 +0200647 if (sdrt->tREH_min > tset_mem &&
648 (thold_mem < sdrt->tREH_min - tset_mem))
649 thold_mem = sdrt->tREH_min - tset_mem;
650 if ((sdrt->tRC_min > tset_mem + twait) &&
651 (thold_mem < sdrt->tRC_min - (tset_mem + twait)))
652 thold_mem = sdrt->tRC_min - (tset_mem + twait);
653 if ((sdrt->tWC_min > tset_mem + twait) &&
654 (thold_mem < sdrt->tWC_min - (tset_mem + twait)))
655 thold_mem = sdrt->tWC_min - (tset_mem + twait);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200656 timing = DIV_ROUND_UP(thold_mem, hclkp);
657 tims->thold_mem = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200658
659 /*
660 * tSETUP_ATT > tCS - tWAIT
661 * tSETUP_ATT > tCLS - tWAIT
662 * tSETUP_ATT > tALS - tWAIT
663 * tSETUP_ATT > tRHW - tHOLD_MEM
664 * tSETUP_ATT > tDS - (tWAIT - tHIZ)
665 */
666 tset_att = hclkp;
667 if (sdrt->tCS_min > twait && (tset_att < sdrt->tCS_min - twait))
668 tset_att = sdrt->tCS_min - twait;
669 if (sdrt->tCLS_min > twait && (tset_att < sdrt->tCLS_min - twait))
670 tset_att = sdrt->tCLS_min - twait;
671 if (sdrt->tALS_min > twait && (tset_att < sdrt->tALS_min - twait))
672 tset_att = sdrt->tALS_min - twait;
673 if (sdrt->tRHW_min > thold_mem &&
674 (tset_att < sdrt->tRHW_min - thold_mem))
675 tset_att = sdrt->tRHW_min - thold_mem;
676 if (twait > thiz && (sdrt->tDS_min > twait - thiz) &&
677 (tset_att < sdrt->tDS_min - (twait - thiz)))
678 tset_att = sdrt->tDS_min - (twait - thiz);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200679 timing = DIV_ROUND_UP(tset_att, hclkp);
680 tims->tset_att = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200681
682 /*
683 * tHOLD_ATT > tALH
684 * tHOLD_ATT > tCH
685 * tHOLD_ATT > tCLH
686 * tHOLD_ATT > tCOH
687 * tHOLD_ATT > tDH
688 * tHOLD_ATT > tWB + tIO + tSYNC - tSETUP_MEM
689 * tHOLD_ATT > tADL - tSETUP_MEM
690 * tHOLD_ATT > tWH - tSETUP_MEM
691 * tHOLD_ATT > tWHR - tSETUP_MEM
692 * tHOLD_ATT > tRC - (tSETUP_ATT + tWAIT)
693 * tHOLD_ATT > tWC - (tSETUP_ATT + tWAIT)
694 */
Patrick Delaunay804858a2019-06-21 15:26:54 +0200695 thold_att = max_t(unsigned long, hclkp, sdrt->tALH_min);
696 thold_att = max_t(unsigned long, thold_att, sdrt->tCH_min);
697 thold_att = max_t(unsigned long, thold_att, sdrt->tCLH_min);
698 thold_att = max_t(unsigned long, thold_att, sdrt->tCOH_min);
699 thold_att = max_t(unsigned long, thold_att, sdrt->tDH_min);
Christophe Kerelloda141682019-04-05 11:41:50 +0200700 if ((sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC > tset_mem) &&
701 (thold_att < sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC - tset_mem))
702 thold_att = sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC - tset_mem;
703 if (sdrt->tADL_min > tset_mem &&
704 (thold_att < sdrt->tADL_min - tset_mem))
705 thold_att = sdrt->tADL_min - tset_mem;
706 if (sdrt->tWH_min > tset_mem &&
707 (thold_att < sdrt->tWH_min - tset_mem))
708 thold_att = sdrt->tWH_min - tset_mem;
709 if (sdrt->tWHR_min > tset_mem &&
710 (thold_att < sdrt->tWHR_min - tset_mem))
711 thold_att = sdrt->tWHR_min - tset_mem;
712 if ((sdrt->tRC_min > tset_att + twait) &&
713 (thold_att < sdrt->tRC_min - (tset_att + twait)))
714 thold_att = sdrt->tRC_min - (tset_att + twait);
715 if ((sdrt->tWC_min > tset_att + twait) &&
716 (thold_att < sdrt->tWC_min - (tset_att + twait)))
717 thold_att = sdrt->tWC_min - (tset_att + twait);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200718 timing = DIV_ROUND_UP(thold_att, hclkp);
719 tims->thold_att = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200720}
721
Christophe Kerellod1a25872020-07-31 09:53:37 +0200722static int stm32_fmc2_nfc_setup_interface(struct mtd_info *mtd, int chipnr,
723 const struct nand_data_interface *cf)
Christophe Kerelloda141682019-04-05 11:41:50 +0200724{
725 struct nand_chip *chip = mtd_to_nand(mtd);
726 const struct nand_sdr_timings *sdrt;
727
Christophe Kerellod1a25872020-07-31 09:53:37 +0200728 sdrt = nand_get_sdr_timings(cf);
Christophe Kerelloda141682019-04-05 11:41:50 +0200729 if (IS_ERR(sdrt))
730 return PTR_ERR(sdrt);
731
732 if (chipnr == NAND_DATA_IFACE_CHECK_ONLY)
733 return 0;
734
Christophe Kerellod1a25872020-07-31 09:53:37 +0200735 stm32_fmc2_nfc_calc_timings(chip, sdrt);
736 stm32_fmc2_nfc_timings_init(chip);
Christophe Kerelloda141682019-04-05 11:41:50 +0200737
738 return 0;
739}
740
Christophe Kerellod1a25872020-07-31 09:53:37 +0200741static void stm32_fmc2_nfc_nand_callbacks_setup(struct nand_chip *chip)
Christophe Kerelloda141682019-04-05 11:41:50 +0200742{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200743 chip->ecc.hwctl = stm32_fmc2_nfc_hwctl;
Christophe Kerelloda141682019-04-05 11:41:50 +0200744
745 /*
746 * Specific callbacks to read/write a page depending on
747 * the algo used (Hamming, BCH).
748 */
749 if (chip->ecc.strength == FMC2_ECC_HAM) {
750 /* Hamming is used */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200751 chip->ecc.calculate = stm32_fmc2_nfc_ham_calculate;
752 chip->ecc.correct = stm32_fmc2_nfc_ham_correct;
Christophe Kerelloda141682019-04-05 11:41:50 +0200753 chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 4 : 3;
754 chip->ecc.options |= NAND_ECC_GENERIC_ERASED_CHECK;
755 return;
756 }
757
758 /* BCH is used */
Christophe Kerellod1a25872020-07-31 09:53:37 +0200759 chip->ecc.read_page = stm32_fmc2_nfc_read_page;
760 chip->ecc.calculate = stm32_fmc2_nfc_bch_calculate;
761 chip->ecc.correct = stm32_fmc2_nfc_bch_correct;
Christophe Kerelloda141682019-04-05 11:41:50 +0200762
763 if (chip->ecc.strength == FMC2_ECC_BCH8)
764 chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 14 : 13;
765 else
766 chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 8 : 7;
767}
768
Christophe Kerellod1a25872020-07-31 09:53:37 +0200769static int stm32_fmc2_nfc_calc_ecc_bytes(int step_size, int strength)
Christophe Kerelloda141682019-04-05 11:41:50 +0200770{
771 /* Hamming */
772 if (strength == FMC2_ECC_HAM)
773 return 4;
774
775 /* BCH8 */
776 if (strength == FMC2_ECC_BCH8)
777 return 14;
778
779 /* BCH4 */
780 return 8;
781}
782
Christophe Kerellod1a25872020-07-31 09:53:37 +0200783NAND_ECC_CAPS_SINGLE(stm32_fmc2_nfc_ecc_caps, stm32_fmc2_nfc_calc_ecc_bytes,
Christophe Kerelloda141682019-04-05 11:41:50 +0200784 FMC2_ECC_STEP_SIZE,
785 FMC2_ECC_HAM, FMC2_ECC_BCH4, FMC2_ECC_BCH8);
786
Christophe Kerellod1a25872020-07-31 09:53:37 +0200787static int stm32_fmc2_nfc_parse_child(struct stm32_fmc2_nfc *nfc, ofnode node)
Christophe Kerelloda141682019-04-05 11:41:50 +0200788{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200789 struct stm32_fmc2_nand *nand = &nfc->nand;
Christophe Kerelloda141682019-04-05 11:41:50 +0200790 u32 cs[FMC2_MAX_CE];
791 int ret, i;
792
793 if (!ofnode_get_property(node, "reg", &nand->ncs))
794 return -EINVAL;
795
796 nand->ncs /= sizeof(u32);
797 if (!nand->ncs) {
798 pr_err("Invalid reg property size\n");
799 return -EINVAL;
800 }
801
802 ret = ofnode_read_u32_array(node, "reg", cs, nand->ncs);
803 if (ret < 0) {
804 pr_err("Could not retrieve reg property\n");
805 return -EINVAL;
806 }
807
808 for (i = 0; i < nand->ncs; i++) {
Christophe Kerello45dd1ee2020-07-31 09:53:34 +0200809 if (cs[i] >= FMC2_MAX_CE) {
Christophe Kerelloda141682019-04-05 11:41:50 +0200810 pr_err("Invalid reg value: %d\n",
811 nand->cs_used[i]);
812 return -EINVAL;
813 }
814
Christophe Kerellod1a25872020-07-31 09:53:37 +0200815 if (nfc->cs_assigned & BIT(cs[i])) {
Christophe Kerelloda141682019-04-05 11:41:50 +0200816 pr_err("Cs already assigned: %d\n",
817 nand->cs_used[i]);
818 return -EINVAL;
819 }
820
Christophe Kerellod1a25872020-07-31 09:53:37 +0200821 nfc->cs_assigned |= BIT(cs[i]);
Christophe Kerelloda141682019-04-05 11:41:50 +0200822 nand->cs_used[i] = cs[i];
823 }
824
825 nand->chip.flash_node = ofnode_to_offset(node);
826
827 return 0;
828}
829
Christophe Kerellod1a25872020-07-31 09:53:37 +0200830static int stm32_fmc2_nfc_parse_dt(struct udevice *dev,
831 struct stm32_fmc2_nfc *nfc)
Christophe Kerelloda141682019-04-05 11:41:50 +0200832{
833 ofnode child;
834 int ret, nchips = 0;
835
836 dev_for_each_subnode(child, dev)
837 nchips++;
838
839 if (!nchips) {
840 pr_err("NAND chip not defined\n");
841 return -EINVAL;
842 }
843
844 if (nchips > 1) {
845 pr_err("Too many NAND chips defined\n");
846 return -EINVAL;
847 }
848
849 dev_for_each_subnode(child, dev) {
Christophe Kerellod1a25872020-07-31 09:53:37 +0200850 ret = stm32_fmc2_nfc_parse_child(nfc, child);
Christophe Kerelloda141682019-04-05 11:41:50 +0200851 if (ret)
852 return ret;
853 }
854
855 return 0;
856}
857
Christophe Kerello6276f862020-07-31 09:53:41 +0200858static struct udevice *stm32_fmc2_nfc_get_cdev(struct udevice *dev)
859{
860 struct udevice *pdev = dev_get_parent(dev);
861 struct udevice *cdev = NULL;
862 bool ebi_found = false;
863
864 if (pdev && ofnode_device_is_compatible(dev_ofnode(pdev),
865 "st,stm32mp1-fmc2-ebi"))
866 ebi_found = true;
867
868 if (ofnode_device_is_compatible(dev_ofnode(dev),
869 "st,stm32mp1-fmc2-nfc")) {
870 if (ebi_found)
871 cdev = pdev;
872
873 return cdev;
874 }
875
876 if (!ebi_found)
877 cdev = dev;
878
879 return cdev;
880}
881
Christophe Kerellod1a25872020-07-31 09:53:37 +0200882static int stm32_fmc2_nfc_probe(struct udevice *dev)
Christophe Kerelloda141682019-04-05 11:41:50 +0200883{
Christophe Kerellod1a25872020-07-31 09:53:37 +0200884 struct stm32_fmc2_nfc *nfc = dev_get_priv(dev);
885 struct stm32_fmc2_nand *nand = &nfc->nand;
Christophe Kerelloda141682019-04-05 11:41:50 +0200886 struct nand_chip *chip = &nand->chip;
887 struct mtd_info *mtd = &chip->mtd;
888 struct nand_ecclayout *ecclayout;
Christophe Kerello6276f862020-07-31 09:53:41 +0200889 struct udevice *cdev;
Christophe Kerelloda141682019-04-05 11:41:50 +0200890 struct reset_ctl reset;
Patrick Delaunay804858a2019-06-21 15:26:54 +0200891 int oob_index, chip_cs, mem_region, ret;
892 unsigned int i;
Christophe Kerello6276f862020-07-31 09:53:41 +0200893 int start_region = 0;
894 fdt_addr_t addr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200895
Christophe Kerellod1a25872020-07-31 09:53:37 +0200896 spin_lock_init(&nfc->controller.lock);
897 init_waitqueue_head(&nfc->controller.wq);
Christophe Kerelloda141682019-04-05 11:41:50 +0200898
Christophe Kerello6276f862020-07-31 09:53:41 +0200899 cdev = stm32_fmc2_nfc_get_cdev(dev);
900 if (!cdev)
901 return -EINVAL;
902
Christophe Kerellod1a25872020-07-31 09:53:37 +0200903 ret = stm32_fmc2_nfc_parse_dt(dev, nfc);
Christophe Kerelloda141682019-04-05 11:41:50 +0200904 if (ret)
905 return ret;
906
Christophe Kerello6276f862020-07-31 09:53:41 +0200907 nfc->io_base = dev_read_addr(cdev);
908 if (nfc->io_base == FDT_ADDR_T_NONE)
909 return -EINVAL;
910
911 if (dev == cdev)
912 start_region = 1;
Christophe Kerelloda141682019-04-05 11:41:50 +0200913
Christophe Kerello6276f862020-07-31 09:53:41 +0200914 for (chip_cs = 0, mem_region = start_region; chip_cs < FMC2_MAX_CE;
Christophe Kerelloda141682019-04-05 11:41:50 +0200915 chip_cs++, mem_region += 3) {
Christophe Kerellod1a25872020-07-31 09:53:37 +0200916 if (!(nfc->cs_assigned & BIT(chip_cs)))
Christophe Kerelloda141682019-04-05 11:41:50 +0200917 continue;
918
Christophe Kerello6276f862020-07-31 09:53:41 +0200919 addr = dev_read_addr_index(dev, mem_region);
920 if (addr == FDT_ADDR_T_NONE) {
Christophe Kerelloda141682019-04-05 11:41:50 +0200921 pr_err("Resource data_base not found for cs%d",
922 chip_cs);
923 return ret;
924 }
Christophe Kerello6276f862020-07-31 09:53:41 +0200925 nfc->data_base[chip_cs] = addr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200926
Christophe Kerello6276f862020-07-31 09:53:41 +0200927 addr = dev_read_addr_index(dev, mem_region + 1);
928 if (addr == FDT_ADDR_T_NONE) {
Christophe Kerelloda141682019-04-05 11:41:50 +0200929 pr_err("Resource cmd_base not found for cs%d",
930 chip_cs);
931 return ret;
932 }
Christophe Kerello6276f862020-07-31 09:53:41 +0200933 nfc->cmd_base[chip_cs] = addr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200934
Christophe Kerello6276f862020-07-31 09:53:41 +0200935 addr = dev_read_addr_index(dev, mem_region + 2);
936 if (addr == FDT_ADDR_T_NONE) {
Christophe Kerelloda141682019-04-05 11:41:50 +0200937 pr_err("Resource addr_base not found for cs%d",
938 chip_cs);
939 return ret;
940 }
Christophe Kerello6276f862020-07-31 09:53:41 +0200941 nfc->addr_base[chip_cs] = addr;
Christophe Kerelloda141682019-04-05 11:41:50 +0200942 }
943
944 /* Enable the clock */
Christophe Kerello6276f862020-07-31 09:53:41 +0200945 ret = clk_get_by_index(cdev, 0, &nfc->clk);
Christophe Kerelloda141682019-04-05 11:41:50 +0200946 if (ret)
947 return ret;
948
Christophe Kerellod1a25872020-07-31 09:53:37 +0200949 ret = clk_enable(&nfc->clk);
Christophe Kerelloda141682019-04-05 11:41:50 +0200950 if (ret)
951 return ret;
952
953 /* Reset */
954 ret = reset_get_by_index(dev, 0, &reset);
955 if (!ret) {
956 reset_assert(&reset);
957 udelay(2);
958 reset_deassert(&reset);
959 }
960
Christophe Kerello6276f862020-07-31 09:53:41 +0200961 stm32_fmc2_nfc_init(nfc, dev != cdev);
Christophe Kerelloda141682019-04-05 11:41:50 +0200962
Christophe Kerellod1a25872020-07-31 09:53:37 +0200963 chip->controller = &nfc->base;
964 chip->select_chip = stm32_fmc2_nfc_select_chip;
965 chip->setup_data_interface = stm32_fmc2_nfc_setup_interface;
966 chip->cmd_ctrl = stm32_fmc2_nfc_cmd_ctrl;
Christophe Kerelloda141682019-04-05 11:41:50 +0200967 chip->chip_delay = FMC2_RB_DELAY_US;
968 chip->options |= NAND_BUSWIDTH_AUTO | NAND_NO_SUBPAGE_WRITE |
969 NAND_USE_BOUNCE_BUFFER;
970
971 /* Default ECC settings */
972 chip->ecc.mode = NAND_ECC_HW;
973 chip->ecc.size = FMC2_ECC_STEP_SIZE;
974 chip->ecc.strength = FMC2_ECC_BCH8;
975
Christophe Kerelloda141682019-04-05 11:41:50 +0200976 ret = nand_scan_ident(mtd, nand->ncs, NULL);
977 if (ret)
978 return ret;
979
980 /*
981 * Only NAND_ECC_HW mode is actually supported
982 * Hamming => ecc.strength = 1
983 * BCH4 => ecc.strength = 4
984 * BCH8 => ecc.strength = 8
985 * ECC sector size = 512
986 */
987 if (chip->ecc.mode != NAND_ECC_HW) {
988 pr_err("Nand_ecc_mode is not well defined in the DT\n");
989 return -EINVAL;
990 }
991
Christophe Kerellod1a25872020-07-31 09:53:37 +0200992 ret = nand_check_ecc_caps(chip, &stm32_fmc2_nfc_ecc_caps,
Christophe Kerelloda141682019-04-05 11:41:50 +0200993 mtd->oobsize - FMC2_BBM_LEN);
994 if (ret) {
995 pr_err("No valid ECC settings set\n");
996 return ret;
997 }
998
999 if (chip->bbt_options & NAND_BBT_USE_FLASH)
1000 chip->bbt_options |= NAND_BBT_NO_OOB;
1001
Christophe Kerellod1a25872020-07-31 09:53:37 +02001002 stm32_fmc2_nfc_nand_callbacks_setup(chip);
Christophe Kerelloda141682019-04-05 11:41:50 +02001003
1004 /* Define ECC layout */
Christophe Kerellod1a25872020-07-31 09:53:37 +02001005 ecclayout = &nfc->ecclayout;
Christophe Kerelloda141682019-04-05 11:41:50 +02001006 ecclayout->eccbytes = chip->ecc.bytes *
1007 (mtd->writesize / chip->ecc.size);
1008 oob_index = FMC2_BBM_LEN;
1009 for (i = 0; i < ecclayout->eccbytes; i++, oob_index++)
1010 ecclayout->eccpos[i] = oob_index;
1011 ecclayout->oobfree->offset = oob_index;
1012 ecclayout->oobfree->length = mtd->oobsize - ecclayout->oobfree->offset;
1013 chip->ecc.layout = ecclayout;
1014
Christophe Kerelloda141682019-04-05 11:41:50 +02001015 if (chip->options & NAND_BUSWIDTH_16)
Christophe Kerellod1a25872020-07-31 09:53:37 +02001016 stm32_fmc2_nfc_set_buswidth_16(nfc, true);
Christophe Kerelloda141682019-04-05 11:41:50 +02001017
Christophe Kerelloda141682019-04-05 11:41:50 +02001018 ret = nand_scan_tail(mtd);
1019 if (ret)
1020 return ret;
1021
1022 return nand_register(0, mtd);
1023}
1024
Christophe Kerellod1a25872020-07-31 09:53:37 +02001025static const struct udevice_id stm32_fmc2_nfc_match[] = {
Christophe Kerelloda141682019-04-05 11:41:50 +02001026 { .compatible = "st,stm32mp15-fmc2" },
Christophe Kerello6276f862020-07-31 09:53:41 +02001027 { .compatible = "st,stm32mp1-fmc2-nfc" },
Christophe Kerelloda141682019-04-05 11:41:50 +02001028 { /* Sentinel */ }
1029};
1030
Christophe Kerellod1a25872020-07-31 09:53:37 +02001031U_BOOT_DRIVER(stm32_fmc2_nfc) = {
1032 .name = "stm32_fmc2_nfc",
Christophe Kerelloda141682019-04-05 11:41:50 +02001033 .id = UCLASS_MTD,
Christophe Kerellod1a25872020-07-31 09:53:37 +02001034 .of_match = stm32_fmc2_nfc_match,
1035 .probe = stm32_fmc2_nfc_probe,
Christophe Kerelloda141682019-04-05 11:41:50 +02001036 .priv_auto_alloc_size = sizeof(struct stm32_fmc2_nfc),
1037};
1038
1039void board_nand_init(void)
1040{
1041 struct udevice *dev;
1042 int ret;
1043
1044 ret = uclass_get_device_by_driver(UCLASS_MTD,
Christophe Kerellod1a25872020-07-31 09:53:37 +02001045 DM_GET_DRIVER(stm32_fmc2_nfc),
Christophe Kerelloda141682019-04-05 11:41:50 +02001046 &dev);
1047 if (ret && ret != -ENODEV)
Christophe Kerellod1a25872020-07-31 09:53:37 +02001048 pr_err("Failed to initialize STM32 FMC2 NFC controller. (error %d)\n",
Christophe Kerelloda141682019-04-05 11:41:50 +02001049 ret);
1050}