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