blob: 2e947a3400761633edce51cd4e026db385a72064 [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>
Simon Glass4dcacfc2020-05-10 11:40:13 -060013#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060014#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070015#include <linux/err.h>
Christophe Kerelloda141682019-04-05 11:41:50 +020016#include <linux/iopoll.h>
17#include <linux/ioport.h>
18
19/* Bad block marker length */
20#define FMC2_BBM_LEN 2
21
22/* ECC step size */
23#define FMC2_ECC_STEP_SIZE 512
24
25/* Command delay */
26#define FMC2_RB_DELAY_US 30
27
28/* Max chip enable */
29#define FMC2_MAX_CE 2
30
31/* Timings */
32#define FMC2_THIZ 1
33#define FMC2_TIO 8000
34#define FMC2_TSYNC 3000
35#define FMC2_PCR_TIMING_MASK 0xf
36#define FMC2_PMEM_PATT_TIMING_MASK 0xff
37
38/* FMC2 Controller Registers */
39#define FMC2_BCR1 0x0
40#define FMC2_PCR 0x80
41#define FMC2_SR 0x84
42#define FMC2_PMEM 0x88
43#define FMC2_PATT 0x8c
44#define FMC2_HECCR 0x94
45#define FMC2_BCHISR 0x254
46#define FMC2_BCHICR 0x258
47#define FMC2_BCHPBR1 0x260
48#define FMC2_BCHPBR2 0x264
49#define FMC2_BCHPBR3 0x268
50#define FMC2_BCHPBR4 0x26c
51#define FMC2_BCHDSR0 0x27c
52#define FMC2_BCHDSR1 0x280
53#define FMC2_BCHDSR2 0x284
54#define FMC2_BCHDSR3 0x288
55#define FMC2_BCHDSR4 0x28c
56
57/* Register: FMC2_BCR1 */
58#define FMC2_BCR1_FMC2EN BIT(31)
59
60/* Register: FMC2_PCR */
61#define FMC2_PCR_PWAITEN BIT(1)
62#define FMC2_PCR_PBKEN BIT(2)
63#define FMC2_PCR_PWID_MASK GENMASK(5, 4)
64#define FMC2_PCR_PWID(x) (((x) & 0x3) << 4)
65#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)
69#define FMC2_PCR_TCLR_MASK GENMASK(12, 9)
70#define FMC2_PCR_TCLR(x) (((x) & 0xf) << 9)
71#define FMC2_PCR_TCLR_DEFAULT 0xf
72#define FMC2_PCR_TAR_MASK GENMASK(16, 13)
73#define FMC2_PCR_TAR(x) (((x) & 0xf) << 13)
74#define FMC2_PCR_TAR_DEFAULT 0xf
75#define FMC2_PCR_ECCSS_MASK GENMASK(19, 17)
76#define FMC2_PCR_ECCSS(x) (((x) & 0x7) << 17)
77#define FMC2_PCR_ECCSS_512 1
78#define FMC2_PCR_ECCSS_2048 3
79#define FMC2_PCR_BCHECC BIT(24)
80#define FMC2_PCR_WEN BIT(25)
81
82/* Register: FMC2_SR */
83#define FMC2_SR_NWRF BIT(6)
84
85/* Register: FMC2_PMEM */
86#define FMC2_PMEM_MEMSET(x) (((x) & 0xff) << 0)
87#define FMC2_PMEM_MEMWAIT(x) (((x) & 0xff) << 8)
88#define FMC2_PMEM_MEMHOLD(x) (((x) & 0xff) << 16)
89#define FMC2_PMEM_MEMHIZ(x) (((x) & 0xff) << 24)
90#define FMC2_PMEM_DEFAULT 0x0a0a0a0a
91
92/* Register: FMC2_PATT */
93#define FMC2_PATT_ATTSET(x) (((x) & 0xff) << 0)
94#define FMC2_PATT_ATTWAIT(x) (((x) & 0xff) << 8)
95#define FMC2_PATT_ATTHOLD(x) (((x) & 0xff) << 16)
96#define FMC2_PATT_ATTHIZ(x) (((x) & 0xff) << 24)
97#define FMC2_PATT_DEFAULT 0x0a0a0a0a
98
99/* Register: FMC2_BCHISR */
100#define FMC2_BCHISR_DERF BIT(1)
101#define FMC2_BCHISR_EPBRF BIT(4)
102
103/* Register: FMC2_BCHICR */
104#define FMC2_BCHICR_CLEAR_IRQ GENMASK(4, 0)
105
106/* Register: FMC2_BCHDSR0 */
107#define FMC2_BCHDSR0_DUE BIT(0)
108#define FMC2_BCHDSR0_DEF BIT(1)
109#define FMC2_BCHDSR0_DEN_MASK GENMASK(7, 4)
110#define FMC2_BCHDSR0_DEN_SHIFT 4
111
112/* Register: FMC2_BCHDSR1 */
113#define FMC2_BCHDSR1_EBP1_MASK GENMASK(12, 0)
114#define FMC2_BCHDSR1_EBP2_MASK GENMASK(28, 16)
115#define FMC2_BCHDSR1_EBP2_SHIFT 16
116
117/* Register: FMC2_BCHDSR2 */
118#define FMC2_BCHDSR2_EBP3_MASK GENMASK(12, 0)
119#define FMC2_BCHDSR2_EBP4_MASK GENMASK(28, 16)
120#define FMC2_BCHDSR2_EBP4_SHIFT 16
121
122/* Register: FMC2_BCHDSR3 */
123#define FMC2_BCHDSR3_EBP5_MASK GENMASK(12, 0)
124#define FMC2_BCHDSR3_EBP6_MASK GENMASK(28, 16)
125#define FMC2_BCHDSR3_EBP6_SHIFT 16
126
127/* Register: FMC2_BCHDSR4 */
128#define FMC2_BCHDSR4_EBP7_MASK GENMASK(12, 0)
129#define FMC2_BCHDSR4_EBP8_MASK GENMASK(28, 16)
130#define FMC2_BCHDSR4_EBP8_SHIFT 16
131
132#define FMC2_NSEC_PER_SEC 1000000000L
133
Christophe Kerello92693e32020-07-31 09:53:36 +0200134#define FMC2_TIMEOUT_5S 5000000
135
Christophe Kerelloda141682019-04-05 11:41:50 +0200136enum stm32_fmc2_ecc {
137 FMC2_ECC_HAM = 1,
138 FMC2_ECC_BCH4 = 4,
139 FMC2_ECC_BCH8 = 8
140};
141
142struct stm32_fmc2_timings {
143 u8 tclr;
144 u8 tar;
145 u8 thiz;
146 u8 twait;
147 u8 thold_mem;
148 u8 tset_mem;
149 u8 thold_att;
150 u8 tset_att;
151};
152
153struct stm32_fmc2_nand {
154 struct nand_chip chip;
155 struct stm32_fmc2_timings timings;
156 int ncs;
157 int cs_used[FMC2_MAX_CE];
158};
159
160static inline struct stm32_fmc2_nand *to_fmc2_nand(struct nand_chip *chip)
161{
162 return container_of(chip, struct stm32_fmc2_nand, chip);
163}
164
165struct stm32_fmc2_nfc {
166 struct nand_hw_control base;
167 struct stm32_fmc2_nand nand;
168 struct nand_ecclayout ecclayout;
169 void __iomem *io_base;
170 void __iomem *data_base[FMC2_MAX_CE];
171 void __iomem *cmd_base[FMC2_MAX_CE];
172 void __iomem *addr_base[FMC2_MAX_CE];
173 struct clk clk;
174
175 u8 cs_assigned;
176 int cs_sel;
177};
178
179static inline struct stm32_fmc2_nfc *to_stm32_nfc(struct nand_hw_control *base)
180{
181 return container_of(base, struct stm32_fmc2_nfc, base);
182}
183
Christophe Kerelloda141682019-04-05 11:41:50 +0200184static void stm32_fmc2_timings_init(struct nand_chip *chip)
185{
186 struct stm32_fmc2_nfc *fmc2 = to_stm32_nfc(chip->controller);
187 struct stm32_fmc2_nand *nand = to_fmc2_nand(chip);
188 struct stm32_fmc2_timings *timings = &nand->timings;
189 u32 pcr = readl(fmc2->io_base + FMC2_PCR);
190 u32 pmem, patt;
191
192 /* Set tclr/tar timings */
193 pcr &= ~FMC2_PCR_TCLR_MASK;
194 pcr |= FMC2_PCR_TCLR(timings->tclr);
195 pcr &= ~FMC2_PCR_TAR_MASK;
196 pcr |= FMC2_PCR_TAR(timings->tar);
197
198 /* Set tset/twait/thold/thiz timings in common bank */
199 pmem = FMC2_PMEM_MEMSET(timings->tset_mem);
200 pmem |= FMC2_PMEM_MEMWAIT(timings->twait);
201 pmem |= FMC2_PMEM_MEMHOLD(timings->thold_mem);
202 pmem |= FMC2_PMEM_MEMHIZ(timings->thiz);
203
204 /* Set tset/twait/thold/thiz timings in attribut bank */
205 patt = FMC2_PATT_ATTSET(timings->tset_att);
206 patt |= FMC2_PATT_ATTWAIT(timings->twait);
207 patt |= FMC2_PATT_ATTHOLD(timings->thold_att);
208 patt |= FMC2_PATT_ATTHIZ(timings->thiz);
209
210 writel(pcr, fmc2->io_base + FMC2_PCR);
211 writel(pmem, fmc2->io_base + FMC2_PMEM);
212 writel(patt, fmc2->io_base + FMC2_PATT);
213}
214
Christophe Kerelloda141682019-04-05 11:41:50 +0200215static void stm32_fmc2_setup(struct nand_chip *chip)
216{
217 struct stm32_fmc2_nfc *fmc2 = to_stm32_nfc(chip->controller);
218 u32 pcr = readl(fmc2->io_base + FMC2_PCR);
219
220 /* Configure ECC algorithm (default configuration is Hamming) */
221 pcr &= ~FMC2_PCR_ECCALG;
222 pcr &= ~FMC2_PCR_BCHECC;
223 if (chip->ecc.strength == FMC2_ECC_BCH8) {
224 pcr |= FMC2_PCR_ECCALG;
225 pcr |= FMC2_PCR_BCHECC;
226 } else if (chip->ecc.strength == FMC2_ECC_BCH4) {
227 pcr |= FMC2_PCR_ECCALG;
228 }
229
230 /* Set buswidth */
231 pcr &= ~FMC2_PCR_PWID_MASK;
232 if (chip->options & NAND_BUSWIDTH_16)
233 pcr |= FMC2_PCR_PWID(FMC2_PCR_PWID_BUSWIDTH_16);
234
235 /* Set ECC sector size */
236 pcr &= ~FMC2_PCR_ECCSS_MASK;
237 pcr |= FMC2_PCR_ECCSS(FMC2_PCR_ECCSS_512);
238
239 writel(pcr, fmc2->io_base + FMC2_PCR);
240}
241
Christophe Kerelloda141682019-04-05 11:41:50 +0200242static void stm32_fmc2_select_chip(struct mtd_info *mtd, int chipnr)
243{
244 struct nand_chip *chip = mtd_to_nand(mtd);
245 struct stm32_fmc2_nfc *fmc2 = to_stm32_nfc(chip->controller);
246 struct stm32_fmc2_nand *nand = to_fmc2_nand(chip);
247
248 if (chipnr < 0 || chipnr >= nand->ncs)
249 return;
250
251 if (nand->cs_used[chipnr] == fmc2->cs_sel)
252 return;
253
254 fmc2->cs_sel = nand->cs_used[chipnr];
255 chip->IO_ADDR_R = fmc2->data_base[fmc2->cs_sel];
256 chip->IO_ADDR_W = fmc2->data_base[fmc2->cs_sel];
257
Christophe Kerelloda141682019-04-05 11:41:50 +0200258 stm32_fmc2_setup(chip);
Christophe Kerelloda141682019-04-05 11:41:50 +0200259 stm32_fmc2_timings_init(chip);
260}
261
Christophe Kerelloda141682019-04-05 11:41:50 +0200262static void stm32_fmc2_set_buswidth_16(struct stm32_fmc2_nfc *fmc2, bool set)
263{
264 u32 pcr = readl(fmc2->io_base + FMC2_PCR);
265
266 pcr &= ~FMC2_PCR_PWID_MASK;
267 if (set)
268 pcr |= FMC2_PCR_PWID(FMC2_PCR_PWID_BUSWIDTH_16);
269 writel(pcr, fmc2->io_base + FMC2_PCR);
270}
271
Christophe Kerelloda141682019-04-05 11:41:50 +0200272static void stm32_fmc2_set_ecc(struct stm32_fmc2_nfc *fmc2, bool enable)
273{
274 u32 pcr = readl(fmc2->io_base + FMC2_PCR);
275
276 pcr &= ~FMC2_PCR_ECCEN;
277 if (enable)
278 pcr |= FMC2_PCR_ECCEN;
279 writel(pcr, fmc2->io_base + FMC2_PCR);
280}
281
Christophe Kerelloda141682019-04-05 11:41:50 +0200282static inline void stm32_fmc2_clear_bch_irq(struct stm32_fmc2_nfc *fmc2)
283{
284 writel(FMC2_BCHICR_CLEAR_IRQ, fmc2->io_base + FMC2_BCHICR);
285}
286
Christophe Kerelloda141682019-04-05 11:41:50 +0200287static void stm32_fmc2_cmd_ctrl(struct mtd_info *mtd, int cmd,
288 unsigned int ctrl)
289{
290 struct nand_chip *chip = mtd_to_nand(mtd);
291 struct stm32_fmc2_nfc *fmc2 = to_stm32_nfc(chip->controller);
292
293 if (cmd == NAND_CMD_NONE)
294 return;
295
296 if (ctrl & NAND_CLE) {
297 writeb(cmd, fmc2->cmd_base[fmc2->cs_sel]);
298 return;
299 }
300
301 writeb(cmd, fmc2->addr_base[fmc2->cs_sel]);
302}
303
304/*
305 * Enable ECC logic and reset syndrome/parity bits previously calculated
306 * Syndrome/parity bits is cleared by setting the ECCEN bit to 0
307 */
308static void stm32_fmc2_hwctl(struct mtd_info *mtd, int mode)
309{
310 struct nand_chip *chip = mtd_to_nand(mtd);
311 struct stm32_fmc2_nfc *fmc2 = to_stm32_nfc(chip->controller);
312
313 stm32_fmc2_set_ecc(fmc2, false);
314
315 if (chip->ecc.strength != FMC2_ECC_HAM) {
316 u32 pcr = readl(fmc2->io_base + FMC2_PCR);
317
318 if (mode == NAND_ECC_WRITE)
319 pcr |= FMC2_PCR_WEN;
320 else
321 pcr &= ~FMC2_PCR_WEN;
322 writel(pcr, fmc2->io_base + FMC2_PCR);
323
324 stm32_fmc2_clear_bch_irq(fmc2);
325 }
326
327 stm32_fmc2_set_ecc(fmc2, true);
328}
329
330/*
331 * ECC Hamming calculation
332 * ECC is 3 bytes for 512 bytes of data (supports error correction up to
333 * max of 1-bit)
334 */
335static int stm32_fmc2_ham_calculate(struct mtd_info *mtd, const u8 *data,
336 u8 *ecc)
337{
338 struct nand_chip *chip = mtd_to_nand(mtd);
339 struct stm32_fmc2_nfc *fmc2 = to_stm32_nfc(chip->controller);
340 u32 heccr, sr;
341 int ret;
342
343 ret = readl_poll_timeout(fmc2->io_base + FMC2_SR, sr,
Christophe Kerello92693e32020-07-31 09:53:36 +0200344 sr & FMC2_SR_NWRF, FMC2_TIMEOUT_5S);
Christophe Kerelloda141682019-04-05 11:41:50 +0200345 if (ret < 0) {
346 pr_err("Ham timeout\n");
347 return ret;
348 }
349
350 heccr = readl(fmc2->io_base + FMC2_HECCR);
351
352 ecc[0] = heccr;
353 ecc[1] = heccr >> 8;
354 ecc[2] = heccr >> 16;
355
Christophe Kerelloda141682019-04-05 11:41:50 +0200356 stm32_fmc2_set_ecc(fmc2, false);
357
358 return 0;
359}
360
361static int stm32_fmc2_ham_correct(struct mtd_info *mtd, u8 *dat,
362 u8 *read_ecc, u8 *calc_ecc)
363{
364 u8 bit_position = 0, b0, b1, b2;
365 u32 byte_addr = 0, b;
366 u32 i, shifting = 1;
367
368 /* Indicate which bit and byte is faulty (if any) */
369 b0 = read_ecc[0] ^ calc_ecc[0];
370 b1 = read_ecc[1] ^ calc_ecc[1];
371 b2 = read_ecc[2] ^ calc_ecc[2];
372 b = b0 | (b1 << 8) | (b2 << 16);
373
374 /* No errors */
375 if (likely(!b))
376 return 0;
377
378 /* Calculate bit position */
379 for (i = 0; i < 3; i++) {
380 switch (b % 4) {
381 case 2:
382 bit_position += shifting;
383 case 1:
384 break;
385 default:
386 return -EBADMSG;
387 }
388 shifting <<= 1;
389 b >>= 2;
390 }
391
392 /* Calculate byte position */
393 shifting = 1;
394 for (i = 0; i < 9; i++) {
395 switch (b % 4) {
396 case 2:
397 byte_addr += shifting;
398 case 1:
399 break;
400 default:
401 return -EBADMSG;
402 }
403 shifting <<= 1;
404 b >>= 2;
405 }
406
407 /* Flip the bit */
408 dat[byte_addr] ^= (1 << bit_position);
409
410 return 1;
411}
412
413/*
414 * ECC BCH calculation and correction
415 * ECC is 7/13 bytes for 512 bytes of data (supports error correction up to
416 * max of 4-bit/8-bit)
417 */
418
419static int stm32_fmc2_bch_calculate(struct mtd_info *mtd, const u8 *data,
420 u8 *ecc)
421{
422 struct nand_chip *chip = mtd_to_nand(mtd);
423 struct stm32_fmc2_nfc *fmc2 = to_stm32_nfc(chip->controller);
424 u32 bchpbr, bchisr;
425 int ret;
426
427 /* Wait until the BCH code is ready */
428 ret = readl_poll_timeout(fmc2->io_base + FMC2_BCHISR, bchisr,
Christophe Kerello92693e32020-07-31 09:53:36 +0200429 bchisr & FMC2_BCHISR_EPBRF, FMC2_TIMEOUT_5S);
Christophe Kerelloda141682019-04-05 11:41:50 +0200430 if (ret < 0) {
431 pr_err("Bch timeout\n");
432 return ret;
433 }
434
435 /* Read parity bits */
436 bchpbr = readl(fmc2->io_base + FMC2_BCHPBR1);
437 ecc[0] = bchpbr;
438 ecc[1] = bchpbr >> 8;
439 ecc[2] = bchpbr >> 16;
440 ecc[3] = bchpbr >> 24;
441
442 bchpbr = readl(fmc2->io_base + FMC2_BCHPBR2);
443 ecc[4] = bchpbr;
444 ecc[5] = bchpbr >> 8;
445 ecc[6] = bchpbr >> 16;
446
447 if (chip->ecc.strength == FMC2_ECC_BCH8) {
448 ecc[7] = bchpbr >> 24;
449
450 bchpbr = readl(fmc2->io_base + FMC2_BCHPBR3);
451 ecc[8] = bchpbr;
452 ecc[9] = bchpbr >> 8;
453 ecc[10] = bchpbr >> 16;
454 ecc[11] = bchpbr >> 24;
455
456 bchpbr = readl(fmc2->io_base + FMC2_BCHPBR4);
457 ecc[12] = bchpbr;
458 }
459
Christophe Kerelloda141682019-04-05 11:41:50 +0200460 stm32_fmc2_set_ecc(fmc2, false);
461
462 return 0;
463}
464
Christophe Kerelloda141682019-04-05 11:41:50 +0200465static int stm32_fmc2_bch_correct(struct mtd_info *mtd, u8 *dat,
466 u8 *read_ecc, u8 *calc_ecc)
467{
468 struct nand_chip *chip = mtd_to_nand(mtd);
469 struct stm32_fmc2_nfc *fmc2 = to_stm32_nfc(chip->controller);
470 u32 bchdsr0, bchdsr1, bchdsr2, bchdsr3, bchdsr4, bchisr;
471 u16 pos[8];
472 int i, ret, den, eccsize = chip->ecc.size;
473 unsigned int nb_errs = 0;
474
475 /* Wait until the decoding error is ready */
476 ret = readl_poll_timeout(fmc2->io_base + FMC2_BCHISR, bchisr,
Christophe Kerello92693e32020-07-31 09:53:36 +0200477 bchisr & FMC2_BCHISR_DERF, FMC2_TIMEOUT_5S);
Christophe Kerelloda141682019-04-05 11:41:50 +0200478 if (ret < 0) {
479 pr_err("Bch timeout\n");
480 return ret;
481 }
482
483 bchdsr0 = readl(fmc2->io_base + FMC2_BCHDSR0);
484 bchdsr1 = readl(fmc2->io_base + FMC2_BCHDSR1);
485 bchdsr2 = readl(fmc2->io_base + FMC2_BCHDSR2);
486 bchdsr3 = readl(fmc2->io_base + FMC2_BCHDSR3);
487 bchdsr4 = readl(fmc2->io_base + FMC2_BCHDSR4);
488
Christophe Kerelloda141682019-04-05 11:41:50 +0200489 stm32_fmc2_set_ecc(fmc2, false);
490
491 /* No errors found */
492 if (likely(!(bchdsr0 & FMC2_BCHDSR0_DEF)))
493 return 0;
494
495 /* Too many errors detected */
496 if (unlikely(bchdsr0 & FMC2_BCHDSR0_DUE))
497 return -EBADMSG;
498
499 pos[0] = bchdsr1 & FMC2_BCHDSR1_EBP1_MASK;
500 pos[1] = (bchdsr1 & FMC2_BCHDSR1_EBP2_MASK) >> FMC2_BCHDSR1_EBP2_SHIFT;
501 pos[2] = bchdsr2 & FMC2_BCHDSR2_EBP3_MASK;
502 pos[3] = (bchdsr2 & FMC2_BCHDSR2_EBP4_MASK) >> FMC2_BCHDSR2_EBP4_SHIFT;
503 pos[4] = bchdsr3 & FMC2_BCHDSR3_EBP5_MASK;
504 pos[5] = (bchdsr3 & FMC2_BCHDSR3_EBP6_MASK) >> FMC2_BCHDSR3_EBP6_SHIFT;
505 pos[6] = bchdsr4 & FMC2_BCHDSR4_EBP7_MASK;
506 pos[7] = (bchdsr4 & FMC2_BCHDSR4_EBP8_MASK) >> FMC2_BCHDSR4_EBP8_SHIFT;
507
508 den = (bchdsr0 & FMC2_BCHDSR0_DEN_MASK) >> FMC2_BCHDSR0_DEN_SHIFT;
509 for (i = 0; i < den; i++) {
510 if (pos[i] < eccsize * 8) {
511 __change_bit(pos[i], (unsigned long *)dat);
512 nb_errs++;
513 }
514 }
515
516 return nb_errs;
517}
518
519static int stm32_fmc2_read_page(struct mtd_info *mtd,
520 struct nand_chip *chip, u8 *buf,
521 int oob_required, int page)
522{
523 int i, s, stat, eccsize = chip->ecc.size;
524 int eccbytes = chip->ecc.bytes;
525 int eccsteps = chip->ecc.steps;
526 int eccstrength = chip->ecc.strength;
527 u8 *p = buf;
528 u8 *ecc_calc = chip->buffers->ecccalc;
529 u8 *ecc_code = chip->buffers->ecccode;
530 unsigned int max_bitflips = 0;
531
532 for (i = mtd->writesize + FMC2_BBM_LEN, s = 0; s < eccsteps;
533 s++, i += eccbytes, p += eccsize) {
534 chip->ecc.hwctl(mtd, NAND_ECC_READ);
535
536 /* Read the nand page sector (512 bytes) */
537 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, s * eccsize, -1);
538 chip->read_buf(mtd, p, eccsize);
539
540 /* Read the corresponding ECC bytes */
541 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, i, -1);
542 chip->read_buf(mtd, ecc_code, eccbytes);
543
544 /* Correct the data */
545 stat = chip->ecc.correct(mtd, p, ecc_code, ecc_calc);
546 if (stat == -EBADMSG)
547 /* Check for empty pages with bitflips */
548 stat = nand_check_erased_ecc_chunk(p, eccsize,
549 ecc_code, eccbytes,
550 NULL, 0,
551 eccstrength);
552
553 if (stat < 0) {
554 mtd->ecc_stats.failed++;
555 } else {
556 mtd->ecc_stats.corrected += stat;
557 max_bitflips = max_t(unsigned int, max_bitflips, stat);
558 }
559 }
560
561 /* Read oob */
562 if (oob_required) {
563 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
564 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
565 }
566
567 return max_bitflips;
568}
569
Christophe Kerelloda141682019-04-05 11:41:50 +0200570static void stm32_fmc2_init(struct stm32_fmc2_nfc *fmc2)
571{
572 u32 pcr = readl(fmc2->io_base + FMC2_PCR);
573 u32 bcr1 = readl(fmc2->io_base + FMC2_BCR1);
574
575 /* Set CS used to undefined */
576 fmc2->cs_sel = -1;
577
578 /* Enable wait feature and nand flash memory bank */
579 pcr |= FMC2_PCR_PWAITEN;
580 pcr |= FMC2_PCR_PBKEN;
581
582 /* Set buswidth to 8 bits mode for identification */
583 pcr &= ~FMC2_PCR_PWID_MASK;
584
585 /* ECC logic is disabled */
586 pcr &= ~FMC2_PCR_ECCEN;
587
588 /* Default mode */
589 pcr &= ~FMC2_PCR_ECCALG;
590 pcr &= ~FMC2_PCR_BCHECC;
591 pcr &= ~FMC2_PCR_WEN;
592
593 /* Set default ECC sector size */
594 pcr &= ~FMC2_PCR_ECCSS_MASK;
595 pcr |= FMC2_PCR_ECCSS(FMC2_PCR_ECCSS_2048);
596
597 /* Set default tclr/tar timings */
598 pcr &= ~FMC2_PCR_TCLR_MASK;
599 pcr |= FMC2_PCR_TCLR(FMC2_PCR_TCLR_DEFAULT);
600 pcr &= ~FMC2_PCR_TAR_MASK;
601 pcr |= FMC2_PCR_TAR(FMC2_PCR_TAR_DEFAULT);
602
603 /* Enable FMC2 controller */
604 bcr1 |= FMC2_BCR1_FMC2EN;
605
606 writel(bcr1, fmc2->io_base + FMC2_BCR1);
607 writel(pcr, fmc2->io_base + FMC2_PCR);
608 writel(FMC2_PMEM_DEFAULT, fmc2->io_base + FMC2_PMEM);
609 writel(FMC2_PATT_DEFAULT, fmc2->io_base + FMC2_PATT);
610}
611
Christophe Kerelloda141682019-04-05 11:41:50 +0200612static void stm32_fmc2_calc_timings(struct nand_chip *chip,
613 const struct nand_sdr_timings *sdrt)
614{
615 struct stm32_fmc2_nfc *fmc2 = to_stm32_nfc(chip->controller);
616 struct stm32_fmc2_nand *nand = to_fmc2_nand(chip);
617 struct stm32_fmc2_timings *tims = &nand->timings;
618 unsigned long hclk = clk_get_rate(&fmc2->clk);
619 unsigned long hclkp = FMC2_NSEC_PER_SEC / (hclk / 1000);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200620 unsigned long timing, tar, tclr, thiz, twait;
621 unsigned long tset_mem, tset_att, thold_mem, thold_att;
Christophe Kerelloda141682019-04-05 11:41:50 +0200622
Patrick Delaunay804858a2019-06-21 15:26:54 +0200623 tar = max_t(unsigned long, hclkp, sdrt->tAR_min);
624 timing = DIV_ROUND_UP(tar, hclkp) - 1;
625 tims->tar = min_t(unsigned long, timing, FMC2_PCR_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200626
Patrick Delaunay804858a2019-06-21 15:26:54 +0200627 tclr = max_t(unsigned long, hclkp, sdrt->tCLR_min);
628 timing = DIV_ROUND_UP(tclr, hclkp) - 1;
629 tims->tclr = min_t(unsigned long, timing, FMC2_PCR_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200630
631 tims->thiz = FMC2_THIZ;
632 thiz = (tims->thiz + 1) * hclkp;
633
634 /*
635 * tWAIT > tRP
636 * tWAIT > tWP
637 * tWAIT > tREA + tIO
638 */
Patrick Delaunay804858a2019-06-21 15:26:54 +0200639 twait = max_t(unsigned long, hclkp, sdrt->tRP_min);
640 twait = max_t(unsigned long, twait, sdrt->tWP_min);
641 twait = max_t(unsigned long, twait, sdrt->tREA_max + FMC2_TIO);
642 timing = DIV_ROUND_UP(twait, hclkp);
643 tims->twait = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200644
645 /*
646 * tSETUP_MEM > tCS - tWAIT
647 * tSETUP_MEM > tALS - tWAIT
648 * tSETUP_MEM > tDS - (tWAIT - tHIZ)
649 */
650 tset_mem = hclkp;
651 if (sdrt->tCS_min > twait && (tset_mem < sdrt->tCS_min - twait))
652 tset_mem = sdrt->tCS_min - twait;
653 if (sdrt->tALS_min > twait && (tset_mem < sdrt->tALS_min - twait))
654 tset_mem = sdrt->tALS_min - twait;
655 if (twait > thiz && (sdrt->tDS_min > twait - thiz) &&
656 (tset_mem < sdrt->tDS_min - (twait - thiz)))
657 tset_mem = sdrt->tDS_min - (twait - thiz);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200658 timing = DIV_ROUND_UP(tset_mem, hclkp);
659 tims->tset_mem = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200660
661 /*
662 * tHOLD_MEM > tCH
663 * tHOLD_MEM > tREH - tSETUP_MEM
664 * tHOLD_MEM > max(tRC, tWC) - (tSETUP_MEM + tWAIT)
665 */
Patrick Delaunay804858a2019-06-21 15:26:54 +0200666 thold_mem = max_t(unsigned long, hclkp, sdrt->tCH_min);
Christophe Kerelloda141682019-04-05 11:41:50 +0200667 if (sdrt->tREH_min > tset_mem &&
668 (thold_mem < sdrt->tREH_min - tset_mem))
669 thold_mem = sdrt->tREH_min - tset_mem;
670 if ((sdrt->tRC_min > tset_mem + twait) &&
671 (thold_mem < sdrt->tRC_min - (tset_mem + twait)))
672 thold_mem = sdrt->tRC_min - (tset_mem + twait);
673 if ((sdrt->tWC_min > tset_mem + twait) &&
674 (thold_mem < sdrt->tWC_min - (tset_mem + twait)))
675 thold_mem = sdrt->tWC_min - (tset_mem + twait);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200676 timing = DIV_ROUND_UP(thold_mem, hclkp);
677 tims->thold_mem = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200678
679 /*
680 * tSETUP_ATT > tCS - tWAIT
681 * tSETUP_ATT > tCLS - tWAIT
682 * tSETUP_ATT > tALS - tWAIT
683 * tSETUP_ATT > tRHW - tHOLD_MEM
684 * tSETUP_ATT > tDS - (tWAIT - tHIZ)
685 */
686 tset_att = hclkp;
687 if (sdrt->tCS_min > twait && (tset_att < sdrt->tCS_min - twait))
688 tset_att = sdrt->tCS_min - twait;
689 if (sdrt->tCLS_min > twait && (tset_att < sdrt->tCLS_min - twait))
690 tset_att = sdrt->tCLS_min - twait;
691 if (sdrt->tALS_min > twait && (tset_att < sdrt->tALS_min - twait))
692 tset_att = sdrt->tALS_min - twait;
693 if (sdrt->tRHW_min > thold_mem &&
694 (tset_att < sdrt->tRHW_min - thold_mem))
695 tset_att = sdrt->tRHW_min - thold_mem;
696 if (twait > thiz && (sdrt->tDS_min > twait - thiz) &&
697 (tset_att < sdrt->tDS_min - (twait - thiz)))
698 tset_att = sdrt->tDS_min - (twait - thiz);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200699 timing = DIV_ROUND_UP(tset_att, hclkp);
700 tims->tset_att = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200701
702 /*
703 * tHOLD_ATT > tALH
704 * tHOLD_ATT > tCH
705 * tHOLD_ATT > tCLH
706 * tHOLD_ATT > tCOH
707 * tHOLD_ATT > tDH
708 * tHOLD_ATT > tWB + tIO + tSYNC - tSETUP_MEM
709 * tHOLD_ATT > tADL - tSETUP_MEM
710 * tHOLD_ATT > tWH - tSETUP_MEM
711 * tHOLD_ATT > tWHR - tSETUP_MEM
712 * tHOLD_ATT > tRC - (tSETUP_ATT + tWAIT)
713 * tHOLD_ATT > tWC - (tSETUP_ATT + tWAIT)
714 */
Patrick Delaunay804858a2019-06-21 15:26:54 +0200715 thold_att = max_t(unsigned long, hclkp, sdrt->tALH_min);
716 thold_att = max_t(unsigned long, thold_att, sdrt->tCH_min);
717 thold_att = max_t(unsigned long, thold_att, sdrt->tCLH_min);
718 thold_att = max_t(unsigned long, thold_att, sdrt->tCOH_min);
719 thold_att = max_t(unsigned long, thold_att, sdrt->tDH_min);
Christophe Kerelloda141682019-04-05 11:41:50 +0200720 if ((sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC > tset_mem) &&
721 (thold_att < sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC - tset_mem))
722 thold_att = sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC - tset_mem;
723 if (sdrt->tADL_min > tset_mem &&
724 (thold_att < sdrt->tADL_min - tset_mem))
725 thold_att = sdrt->tADL_min - tset_mem;
726 if (sdrt->tWH_min > tset_mem &&
727 (thold_att < sdrt->tWH_min - tset_mem))
728 thold_att = sdrt->tWH_min - tset_mem;
729 if (sdrt->tWHR_min > tset_mem &&
730 (thold_att < sdrt->tWHR_min - tset_mem))
731 thold_att = sdrt->tWHR_min - tset_mem;
732 if ((sdrt->tRC_min > tset_att + twait) &&
733 (thold_att < sdrt->tRC_min - (tset_att + twait)))
734 thold_att = sdrt->tRC_min - (tset_att + twait);
735 if ((sdrt->tWC_min > tset_att + twait) &&
736 (thold_att < sdrt->tWC_min - (tset_att + twait)))
737 thold_att = sdrt->tWC_min - (tset_att + twait);
Patrick Delaunay804858a2019-06-21 15:26:54 +0200738 timing = DIV_ROUND_UP(thold_att, hclkp);
739 tims->thold_att = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK);
Christophe Kerelloda141682019-04-05 11:41:50 +0200740}
741
742static int stm32_fmc2_setup_interface(struct mtd_info *mtd, int chipnr,
743 const struct nand_data_interface *conf)
744{
745 struct nand_chip *chip = mtd_to_nand(mtd);
746 const struct nand_sdr_timings *sdrt;
747
748 sdrt = nand_get_sdr_timings(conf);
749 if (IS_ERR(sdrt))
750 return PTR_ERR(sdrt);
751
752 if (chipnr == NAND_DATA_IFACE_CHECK_ONLY)
753 return 0;
754
755 stm32_fmc2_calc_timings(chip, sdrt);
756
Christophe Kerelloda141682019-04-05 11:41:50 +0200757 stm32_fmc2_timings_init(chip);
758
759 return 0;
760}
761
Christophe Kerelloda141682019-04-05 11:41:50 +0200762static void stm32_fmc2_nand_callbacks_setup(struct nand_chip *chip)
763{
764 chip->ecc.hwctl = stm32_fmc2_hwctl;
765
766 /*
767 * Specific callbacks to read/write a page depending on
768 * the algo used (Hamming, BCH).
769 */
770 if (chip->ecc.strength == FMC2_ECC_HAM) {
771 /* Hamming is used */
772 chip->ecc.calculate = stm32_fmc2_ham_calculate;
773 chip->ecc.correct = stm32_fmc2_ham_correct;
774 chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 4 : 3;
775 chip->ecc.options |= NAND_ECC_GENERIC_ERASED_CHECK;
776 return;
777 }
778
779 /* BCH is used */
780 chip->ecc.read_page = stm32_fmc2_read_page;
781 chip->ecc.calculate = stm32_fmc2_bch_calculate;
782 chip->ecc.correct = stm32_fmc2_bch_correct;
783
784 if (chip->ecc.strength == FMC2_ECC_BCH8)
785 chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 14 : 13;
786 else
787 chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 8 : 7;
788}
789
Christophe Kerelloda141682019-04-05 11:41:50 +0200790static int stm32_fmc2_calc_ecc_bytes(int step_size, int strength)
791{
792 /* Hamming */
793 if (strength == FMC2_ECC_HAM)
794 return 4;
795
796 /* BCH8 */
797 if (strength == FMC2_ECC_BCH8)
798 return 14;
799
800 /* BCH4 */
801 return 8;
802}
803
804NAND_ECC_CAPS_SINGLE(stm32_fmc2_ecc_caps, stm32_fmc2_calc_ecc_bytes,
805 FMC2_ECC_STEP_SIZE,
806 FMC2_ECC_HAM, FMC2_ECC_BCH4, FMC2_ECC_BCH8);
807
Christophe Kerelloda141682019-04-05 11:41:50 +0200808static int stm32_fmc2_parse_child(struct stm32_fmc2_nfc *fmc2,
809 ofnode node)
810{
811 struct stm32_fmc2_nand *nand = &fmc2->nand;
812 u32 cs[FMC2_MAX_CE];
813 int ret, i;
814
815 if (!ofnode_get_property(node, "reg", &nand->ncs))
816 return -EINVAL;
817
818 nand->ncs /= sizeof(u32);
819 if (!nand->ncs) {
820 pr_err("Invalid reg property size\n");
821 return -EINVAL;
822 }
823
824 ret = ofnode_read_u32_array(node, "reg", cs, nand->ncs);
825 if (ret < 0) {
826 pr_err("Could not retrieve reg property\n");
827 return -EINVAL;
828 }
829
830 for (i = 0; i < nand->ncs; i++) {
Christophe Kerello45dd1ee2020-07-31 09:53:34 +0200831 if (cs[i] >= FMC2_MAX_CE) {
Christophe Kerelloda141682019-04-05 11:41:50 +0200832 pr_err("Invalid reg value: %d\n",
833 nand->cs_used[i]);
834 return -EINVAL;
835 }
836
837 if (fmc2->cs_assigned & BIT(cs[i])) {
838 pr_err("Cs already assigned: %d\n",
839 nand->cs_used[i]);
840 return -EINVAL;
841 }
842
843 fmc2->cs_assigned |= BIT(cs[i]);
844 nand->cs_used[i] = cs[i];
845 }
846
847 nand->chip.flash_node = ofnode_to_offset(node);
848
849 return 0;
850}
851
852static int stm32_fmc2_parse_dt(struct udevice *dev,
853 struct stm32_fmc2_nfc *fmc2)
854{
855 ofnode child;
856 int ret, nchips = 0;
857
858 dev_for_each_subnode(child, dev)
859 nchips++;
860
861 if (!nchips) {
862 pr_err("NAND chip not defined\n");
863 return -EINVAL;
864 }
865
866 if (nchips > 1) {
867 pr_err("Too many NAND chips defined\n");
868 return -EINVAL;
869 }
870
871 dev_for_each_subnode(child, dev) {
872 ret = stm32_fmc2_parse_child(fmc2, child);
873 if (ret)
874 return ret;
875 }
876
877 return 0;
878}
879
880static int stm32_fmc2_probe(struct udevice *dev)
881{
882 struct stm32_fmc2_nfc *fmc2 = dev_get_priv(dev);
883 struct stm32_fmc2_nand *nand = &fmc2->nand;
884 struct nand_chip *chip = &nand->chip;
885 struct mtd_info *mtd = &chip->mtd;
886 struct nand_ecclayout *ecclayout;
887 struct resource resource;
888 struct reset_ctl reset;
Patrick Delaunay804858a2019-06-21 15:26:54 +0200889 int oob_index, chip_cs, mem_region, ret;
890 unsigned int i;
Christophe Kerelloda141682019-04-05 11:41:50 +0200891
892 spin_lock_init(&fmc2->controller.lock);
893 init_waitqueue_head(&fmc2->controller.wq);
894
895 ret = stm32_fmc2_parse_dt(dev, fmc2);
896 if (ret)
897 return ret;
898
899 /* Get resources */
900 ret = dev_read_resource(dev, 0, &resource);
901 if (ret) {
902 pr_err("Resource io_base not found");
903 return ret;
904 }
905 fmc2->io_base = (void __iomem *)resource.start;
906
907 for (chip_cs = 0, mem_region = 1; chip_cs < FMC2_MAX_CE;
908 chip_cs++, mem_region += 3) {
909 if (!(fmc2->cs_assigned & BIT(chip_cs)))
910 continue;
911
912 ret = dev_read_resource(dev, mem_region, &resource);
913 if (ret) {
914 pr_err("Resource data_base not found for cs%d",
915 chip_cs);
916 return ret;
917 }
918 fmc2->data_base[chip_cs] = (void __iomem *)resource.start;
919
920 ret = dev_read_resource(dev, mem_region + 1, &resource);
921 if (ret) {
922 pr_err("Resource cmd_base not found for cs%d",
923 chip_cs);
924 return ret;
925 }
926 fmc2->cmd_base[chip_cs] = (void __iomem *)resource.start;
927
928 ret = dev_read_resource(dev, mem_region + 2, &resource);
929 if (ret) {
930 pr_err("Resource addr_base not found for cs%d",
931 chip_cs);
932 return ret;
933 }
934 fmc2->addr_base[chip_cs] = (void __iomem *)resource.start;
935 }
936
937 /* Enable the clock */
938 ret = clk_get_by_index(dev, 0, &fmc2->clk);
939 if (ret)
940 return ret;
941
942 ret = clk_enable(&fmc2->clk);
943 if (ret)
944 return ret;
945
946 /* Reset */
947 ret = reset_get_by_index(dev, 0, &reset);
948 if (!ret) {
949 reset_assert(&reset);
950 udelay(2);
951 reset_deassert(&reset);
952 }
953
Christophe Kerelloda141682019-04-05 11:41:50 +0200954 stm32_fmc2_init(fmc2);
955
956 chip->controller = &fmc2->base;
957 chip->select_chip = stm32_fmc2_select_chip;
958 chip->setup_data_interface = stm32_fmc2_setup_interface;
959 chip->cmd_ctrl = stm32_fmc2_cmd_ctrl;
960 chip->chip_delay = FMC2_RB_DELAY_US;
961 chip->options |= NAND_BUSWIDTH_AUTO | NAND_NO_SUBPAGE_WRITE |
962 NAND_USE_BOUNCE_BUFFER;
963
964 /* Default ECC settings */
965 chip->ecc.mode = NAND_ECC_HW;
966 chip->ecc.size = FMC2_ECC_STEP_SIZE;
967 chip->ecc.strength = FMC2_ECC_BCH8;
968
Christophe Kerelloda141682019-04-05 11:41:50 +0200969 ret = nand_scan_ident(mtd, nand->ncs, NULL);
970 if (ret)
971 return ret;
972
973 /*
974 * Only NAND_ECC_HW mode is actually supported
975 * Hamming => ecc.strength = 1
976 * BCH4 => ecc.strength = 4
977 * BCH8 => ecc.strength = 8
978 * ECC sector size = 512
979 */
980 if (chip->ecc.mode != NAND_ECC_HW) {
981 pr_err("Nand_ecc_mode is not well defined in the DT\n");
982 return -EINVAL;
983 }
984
985 ret = nand_check_ecc_caps(chip, &stm32_fmc2_ecc_caps,
986 mtd->oobsize - FMC2_BBM_LEN);
987 if (ret) {
988 pr_err("No valid ECC settings set\n");
989 return ret;
990 }
991
992 if (chip->bbt_options & NAND_BBT_USE_FLASH)
993 chip->bbt_options |= NAND_BBT_NO_OOB;
994
Christophe Kerelloda141682019-04-05 11:41:50 +0200995 stm32_fmc2_nand_callbacks_setup(chip);
996
997 /* Define ECC layout */
998 ecclayout = &fmc2->ecclayout;
999 ecclayout->eccbytes = chip->ecc.bytes *
1000 (mtd->writesize / chip->ecc.size);
1001 oob_index = FMC2_BBM_LEN;
1002 for (i = 0; i < ecclayout->eccbytes; i++, oob_index++)
1003 ecclayout->eccpos[i] = oob_index;
1004 ecclayout->oobfree->offset = oob_index;
1005 ecclayout->oobfree->length = mtd->oobsize - ecclayout->oobfree->offset;
1006 chip->ecc.layout = ecclayout;
1007
Christophe Kerelloda141682019-04-05 11:41:50 +02001008 if (chip->options & NAND_BUSWIDTH_16)
1009 stm32_fmc2_set_buswidth_16(fmc2, true);
1010
Christophe Kerelloda141682019-04-05 11:41:50 +02001011 ret = nand_scan_tail(mtd);
1012 if (ret)
1013 return ret;
1014
1015 return nand_register(0, mtd);
1016}
1017
1018static const struct udevice_id stm32_fmc2_match[] = {
1019 { .compatible = "st,stm32mp15-fmc2" },
1020 { /* Sentinel */ }
1021};
1022
1023U_BOOT_DRIVER(stm32_fmc2_nand) = {
1024 .name = "stm32_fmc2_nand",
1025 .id = UCLASS_MTD,
1026 .of_match = stm32_fmc2_match,
1027 .probe = stm32_fmc2_probe,
1028 .priv_auto_alloc_size = sizeof(struct stm32_fmc2_nfc),
1029};
1030
1031void board_nand_init(void)
1032{
1033 struct udevice *dev;
1034 int ret;
1035
1036 ret = uclass_get_device_by_driver(UCLASS_MTD,
1037 DM_GET_DRIVER(stm32_fmc2_nand),
1038 &dev);
1039 if (ret && ret != -ENODEV)
1040 pr_err("Failed to initialize STM32 FMC2 NAND controller. (error %d)\n",
1041 ret);
1042}