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