1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STMicroelectronics 2020
6 #include <linux/bitfield.h>
8 #include <linux/mfd/syscon.h>
9 #include <linux/module.h>
11 #include <linux/of_platform.h>
12 #include <linux/pinctrl/consumer.h>
13 #include <linux/platform_device.h>
14 #include <linux/regmap.h>
15 #include <linux/reset.h>
17 /* FMC2 Controller Registers */
20 #define FMC2_BCR(x) ((x) * 0x8 + FMC2_BCR1)
21 #define FMC2_BTR(x) ((x) * 0x8 + FMC2_BTR1)
22 #define FMC2_PCSCNTR 0x20
23 #define FMC2_BWTR1 0x104
24 #define FMC2_BWTR(x) ((x) * 0x8 + FMC2_BWTR1)
26 /* Register: FMC2_BCR1 */
27 #define FMC2_BCR1_CCLKEN BIT(20)
28 #define FMC2_BCR1_FMC2EN BIT(31)
30 /* Register: FMC2_BCRx */
31 #define FMC2_BCR_MBKEN BIT(0)
32 #define FMC2_BCR_MUXEN BIT(1)
33 #define FMC2_BCR_MTYP GENMASK(3, 2)
34 #define FMC2_BCR_MWID GENMASK(5, 4)
35 #define FMC2_BCR_FACCEN BIT(6)
36 #define FMC2_BCR_BURSTEN BIT(8)
37 #define FMC2_BCR_WAITPOL BIT(9)
38 #define FMC2_BCR_WAITCFG BIT(11)
39 #define FMC2_BCR_WREN BIT(12)
40 #define FMC2_BCR_WAITEN BIT(13)
41 #define FMC2_BCR_EXTMOD BIT(14)
42 #define FMC2_BCR_ASYNCWAIT BIT(15)
43 #define FMC2_BCR_CPSIZE GENMASK(18, 16)
44 #define FMC2_BCR_CBURSTRW BIT(19)
45 #define FMC2_BCR_NBLSET GENMASK(23, 22)
47 /* Register: FMC2_BTRx/FMC2_BWTRx */
48 #define FMC2_BXTR_ADDSET GENMASK(3, 0)
49 #define FMC2_BXTR_ADDHLD GENMASK(7, 4)
50 #define FMC2_BXTR_DATAST GENMASK(15, 8)
51 #define FMC2_BXTR_BUSTURN GENMASK(19, 16)
52 #define FMC2_BTR_CLKDIV GENMASK(23, 20)
53 #define FMC2_BTR_DATLAT GENMASK(27, 24)
54 #define FMC2_BXTR_ACCMOD GENMASK(29, 28)
55 #define FMC2_BXTR_DATAHLD GENMASK(31, 30)
57 /* Register: FMC2_PCSCNTR */
58 #define FMC2_PCSCNTR_CSCOUNT GENMASK(15, 0)
59 #define FMC2_PCSCNTR_CNTBEN(x) BIT((x) + 16)
61 #define FMC2_MAX_EBI_CE 4
62 #define FMC2_MAX_BANKS 5
64 #define FMC2_BCR_CPSIZE_0 0x0
65 #define FMC2_BCR_CPSIZE_128 0x1
66 #define FMC2_BCR_CPSIZE_256 0x2
67 #define FMC2_BCR_CPSIZE_512 0x3
68 #define FMC2_BCR_CPSIZE_1024 0x4
70 #define FMC2_BCR_MWID_8 0x0
71 #define FMC2_BCR_MWID_16 0x1
73 #define FMC2_BCR_MTYP_SRAM 0x0
74 #define FMC2_BCR_MTYP_PSRAM 0x1
75 #define FMC2_BCR_MTYP_NOR 0x2
77 #define FMC2_BXTR_EXTMOD_A 0x0
78 #define FMC2_BXTR_EXTMOD_B 0x1
79 #define FMC2_BXTR_EXTMOD_C 0x2
80 #define FMC2_BXTR_EXTMOD_D 0x3
82 #define FMC2_BCR_NBLSET_MAX 0x3
83 #define FMC2_BXTR_ADDSET_MAX 0xf
84 #define FMC2_BXTR_ADDHLD_MAX 0xf
85 #define FMC2_BXTR_DATAST_MAX 0xff
86 #define FMC2_BXTR_BUSTURN_MAX 0xf
87 #define FMC2_BXTR_DATAHLD_MAX 0x3
88 #define FMC2_BTR_CLKDIV_MAX 0xf
89 #define FMC2_BTR_DATLAT_MAX 0xf
90 #define FMC2_PCSCNTR_CSCOUNT_MAX 0xff
92 enum stm32_fmc2_ebi_bank {
100 enum stm32_fmc2_ebi_register_type {
107 enum stm32_fmc2_ebi_transaction_type {
108 FMC2_ASYNC_MODE_1_SRAM = 0,
109 FMC2_ASYNC_MODE_1_PSRAM,
110 FMC2_ASYNC_MODE_A_SRAM,
111 FMC2_ASYNC_MODE_A_PSRAM,
112 FMC2_ASYNC_MODE_2_NOR,
113 FMC2_ASYNC_MODE_B_NOR,
114 FMC2_ASYNC_MODE_C_NOR,
115 FMC2_ASYNC_MODE_D_NOR,
116 FMC2_SYNC_READ_SYNC_WRITE_PSRAM,
117 FMC2_SYNC_READ_ASYNC_WRITE_PSRAM,
118 FMC2_SYNC_READ_SYNC_WRITE_NOR,
119 FMC2_SYNC_READ_ASYNC_WRITE_NOR
122 enum stm32_fmc2_ebi_buswidth {
124 FMC2_BUSWIDTH_16 = 16
127 enum stm32_fmc2_ebi_cpsize {
129 FMC2_CPSIZE_128 = 128,
130 FMC2_CPSIZE_256 = 256,
131 FMC2_CPSIZE_512 = 512,
132 FMC2_CPSIZE_1024 = 1024
135 struct stm32_fmc2_ebi {
138 struct regmap *regmap;
141 u32 bcr[FMC2_MAX_EBI_CE];
142 u32 btr[FMC2_MAX_EBI_CE];
143 u32 bwtr[FMC2_MAX_EBI_CE];
148 * struct stm32_fmc2_prop - STM32 FMC2 EBI property
149 * @name: the device tree binding name of the property
150 * @bprop: indicate that it is a boolean property
151 * @mprop: indicate that it is a mandatory property
152 * @reg_type: the register that have to be modified
153 * @reg_mask: the bit that have to be modified in the selected register
154 * in case of it is a boolean property
155 * @reset_val: the default value that have to be set in case the property
156 * has not been defined in the device tree
157 * @check: this callback ckecks that the property is compliant with the
158 * transaction type selected
159 * @calculate: this callback is called to calculate for exemple a timing
160 * set in nanoseconds in the device tree in clock cycles or in
162 * @set: this callback applies the values in the registers
164 struct stm32_fmc2_prop {
171 int (*check)(struct stm32_fmc2_ebi *ebi,
172 const struct stm32_fmc2_prop *prop, int cs);
173 u32 (*calculate)(struct stm32_fmc2_ebi *ebi, int cs, u32 setup);
174 int (*set)(struct stm32_fmc2_ebi *ebi,
175 const struct stm32_fmc2_prop *prop,
179 static int stm32_fmc2_ebi_check_mux(struct stm32_fmc2_ebi *ebi,
180 const struct stm32_fmc2_prop *prop,
185 regmap_read(ebi->regmap, FMC2_BCR(cs), &bcr);
187 if (bcr & FMC2_BCR_MTYP)
193 static int stm32_fmc2_ebi_check_waitcfg(struct stm32_fmc2_ebi *ebi,
194 const struct stm32_fmc2_prop *prop,
197 u32 bcr, val = FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_NOR);
199 regmap_read(ebi->regmap, FMC2_BCR(cs), &bcr);
201 if ((bcr & FMC2_BCR_MTYP) == val && bcr & FMC2_BCR_BURSTEN)
207 static int stm32_fmc2_ebi_check_sync_trans(struct stm32_fmc2_ebi *ebi,
208 const struct stm32_fmc2_prop *prop,
213 regmap_read(ebi->regmap, FMC2_BCR(cs), &bcr);
215 if (bcr & FMC2_BCR_BURSTEN)
221 static int stm32_fmc2_ebi_check_async_trans(struct stm32_fmc2_ebi *ebi,
222 const struct stm32_fmc2_prop *prop,
227 regmap_read(ebi->regmap, FMC2_BCR(cs), &bcr);
229 if (!(bcr & FMC2_BCR_BURSTEN) || !(bcr & FMC2_BCR_CBURSTRW))
235 static int stm32_fmc2_ebi_check_cpsize(struct stm32_fmc2_ebi *ebi,
236 const struct stm32_fmc2_prop *prop,
239 u32 bcr, val = FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_PSRAM);
241 regmap_read(ebi->regmap, FMC2_BCR(cs), &bcr);
243 if ((bcr & FMC2_BCR_MTYP) == val && bcr & FMC2_BCR_BURSTEN)
249 static int stm32_fmc2_ebi_check_address_hold(struct stm32_fmc2_ebi *ebi,
250 const struct stm32_fmc2_prop *prop,
253 u32 bcr, bxtr, val = FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_D);
255 regmap_read(ebi->regmap, FMC2_BCR(cs), &bcr);
256 if (prop->reg_type == FMC2_REG_BWTR)
257 regmap_read(ebi->regmap, FMC2_BWTR(cs), &bxtr);
259 regmap_read(ebi->regmap, FMC2_BTR(cs), &bxtr);
261 if ((!(bcr & FMC2_BCR_BURSTEN) || !(bcr & FMC2_BCR_CBURSTRW)) &&
262 ((bxtr & FMC2_BXTR_ACCMOD) == val || bcr & FMC2_BCR_MUXEN))
268 static int stm32_fmc2_ebi_check_clk_period(struct stm32_fmc2_ebi *ebi,
269 const struct stm32_fmc2_prop *prop,
274 regmap_read(ebi->regmap, FMC2_BCR(cs), &bcr);
276 regmap_read(ebi->regmap, FMC2_BCR1, &bcr1);
280 if (bcr & FMC2_BCR_BURSTEN && (!cs || !(bcr1 & FMC2_BCR1_CCLKEN)))
286 static int stm32_fmc2_ebi_check_cclk(struct stm32_fmc2_ebi *ebi,
287 const struct stm32_fmc2_prop *prop,
293 return stm32_fmc2_ebi_check_sync_trans(ebi, prop, cs);
296 static u32 stm32_fmc2_ebi_ns_to_clock_cycles(struct stm32_fmc2_ebi *ebi,
299 unsigned long hclk = clk_get_rate(ebi->clk);
300 unsigned long hclkp = NSEC_PER_SEC / (hclk / 1000);
302 return DIV_ROUND_UP(setup * 1000, hclkp);
305 static u32 stm32_fmc2_ebi_ns_to_clk_period(struct stm32_fmc2_ebi *ebi,
308 u32 nb_clk_cycles = stm32_fmc2_ebi_ns_to_clock_cycles(ebi, cs, setup);
309 u32 bcr, btr, clk_period;
311 regmap_read(ebi->regmap, FMC2_BCR1, &bcr);
312 if (bcr & FMC2_BCR1_CCLKEN || !cs)
313 regmap_read(ebi->regmap, FMC2_BTR1, &btr);
315 regmap_read(ebi->regmap, FMC2_BTR(cs), &btr);
317 clk_period = FIELD_GET(FMC2_BTR_CLKDIV, btr) + 1;
319 return DIV_ROUND_UP(nb_clk_cycles, clk_period);
322 static int stm32_fmc2_ebi_get_reg(int reg_type, int cs, u32 *reg)
332 *reg = FMC2_BWTR(cs);
334 case FMC2_REG_PCSCNTR:
344 static int stm32_fmc2_ebi_set_bit_field(struct stm32_fmc2_ebi *ebi,
345 const struct stm32_fmc2_prop *prop,
351 ret = stm32_fmc2_ebi_get_reg(prop->reg_type, cs, ®);
355 regmap_update_bits(ebi->regmap, reg, prop->reg_mask,
356 setup ? prop->reg_mask : 0);
361 static int stm32_fmc2_ebi_set_trans_type(struct stm32_fmc2_ebi *ebi,
362 const struct stm32_fmc2_prop *prop,
365 u32 bcr_mask, bcr = FMC2_BCR_WREN;
366 u32 btr_mask, btr = 0;
367 u32 bwtr_mask, bwtr = 0;
369 bwtr_mask = FMC2_BXTR_ACCMOD;
370 btr_mask = FMC2_BXTR_ACCMOD;
371 bcr_mask = FMC2_BCR_MUXEN | FMC2_BCR_MTYP | FMC2_BCR_FACCEN |
372 FMC2_BCR_WREN | FMC2_BCR_WAITEN | FMC2_BCR_BURSTEN |
373 FMC2_BCR_EXTMOD | FMC2_BCR_CBURSTRW;
376 case FMC2_ASYNC_MODE_1_SRAM:
377 bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_SRAM);
379 * MUXEN = 0, MTYP = 0, FACCEN = 0, BURSTEN = 0, WAITEN = 0,
380 * WREN = 1, EXTMOD = 0, CBURSTRW = 0, ACCMOD = 0
383 case FMC2_ASYNC_MODE_1_PSRAM:
385 * MUXEN = 0, MTYP = 1, FACCEN = 0, BURSTEN = 0, WAITEN = 0,
386 * WREN = 1, EXTMOD = 0, CBURSTRW = 0, ACCMOD = 0
388 bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_PSRAM);
390 case FMC2_ASYNC_MODE_A_SRAM:
392 * MUXEN = 0, MTYP = 0, FACCEN = 0, BURSTEN = 0, WAITEN = 0,
393 * WREN = 1, EXTMOD = 1, CBURSTRW = 0, ACCMOD = 0
395 bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_SRAM);
396 bcr |= FMC2_BCR_EXTMOD;
397 btr |= FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_A);
398 bwtr |= FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_A);
400 case FMC2_ASYNC_MODE_A_PSRAM:
402 * MUXEN = 0, MTYP = 1, FACCEN = 0, BURSTEN = 0, WAITEN = 0,
403 * WREN = 1, EXTMOD = 1, CBURSTRW = 0, ACCMOD = 0
405 bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_PSRAM);
406 bcr |= FMC2_BCR_EXTMOD;
407 btr |= FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_A);
408 bwtr |= FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_A);
410 case FMC2_ASYNC_MODE_2_NOR:
412 * MUXEN = 0, MTYP = 2, FACCEN = 1, BURSTEN = 0, WAITEN = 0,
413 * WREN = 1, EXTMOD = 0, CBURSTRW = 0, ACCMOD = 0
415 bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_NOR);
416 bcr |= FMC2_BCR_FACCEN;
418 case FMC2_ASYNC_MODE_B_NOR:
420 * MUXEN = 0, MTYP = 2, FACCEN = 1, BURSTEN = 0, WAITEN = 0,
421 * WREN = 1, EXTMOD = 1, CBURSTRW = 0, ACCMOD = 1
423 bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_NOR);
424 bcr |= FMC2_BCR_FACCEN | FMC2_BCR_EXTMOD;
425 btr |= FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_B);
426 bwtr |= FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_B);
428 case FMC2_ASYNC_MODE_C_NOR:
430 * MUXEN = 0, MTYP = 2, FACCEN = 1, BURSTEN = 0, WAITEN = 0,
431 * WREN = 1, EXTMOD = 1, CBURSTRW = 0, ACCMOD = 2
433 bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_NOR);
434 bcr |= FMC2_BCR_FACCEN | FMC2_BCR_EXTMOD;
435 btr |= FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_C);
436 bwtr |= FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_C);
438 case FMC2_ASYNC_MODE_D_NOR:
440 * MUXEN = 0, MTYP = 2, FACCEN = 1, BURSTEN = 0, WAITEN = 0,
441 * WREN = 1, EXTMOD = 1, CBURSTRW = 0, ACCMOD = 3
443 bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_NOR);
444 bcr |= FMC2_BCR_FACCEN | FMC2_BCR_EXTMOD;
445 btr |= FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_D);
446 bwtr |= FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_D);
448 case FMC2_SYNC_READ_SYNC_WRITE_PSRAM:
450 * MUXEN = 0, MTYP = 1, FACCEN = 0, BURSTEN = 1, WAITEN = 0,
451 * WREN = 1, EXTMOD = 0, CBURSTRW = 1, ACCMOD = 0
453 bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_PSRAM);
454 bcr |= FMC2_BCR_BURSTEN | FMC2_BCR_CBURSTRW;
456 case FMC2_SYNC_READ_ASYNC_WRITE_PSRAM:
458 * MUXEN = 0, MTYP = 1, FACCEN = 0, BURSTEN = 1, WAITEN = 0,
459 * WREN = 1, EXTMOD = 0, CBURSTRW = 0, ACCMOD = 0
461 bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_PSRAM);
462 bcr |= FMC2_BCR_BURSTEN;
464 case FMC2_SYNC_READ_SYNC_WRITE_NOR:
466 * MUXEN = 0, MTYP = 2, FACCEN = 1, BURSTEN = 1, WAITEN = 0,
467 * WREN = 1, EXTMOD = 0, CBURSTRW = 1, ACCMOD = 0
469 bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_NOR);
470 bcr |= FMC2_BCR_FACCEN | FMC2_BCR_BURSTEN | FMC2_BCR_CBURSTRW;
472 case FMC2_SYNC_READ_ASYNC_WRITE_NOR:
474 * MUXEN = 0, MTYP = 2, FACCEN = 1, BURSTEN = 1, WAITEN = 0,
475 * WREN = 1, EXTMOD = 0, CBURSTRW = 0, ACCMOD = 0
477 bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_NOR);
478 bcr |= FMC2_BCR_FACCEN | FMC2_BCR_BURSTEN;
481 /* Type of transaction not supported */
485 if (bcr & FMC2_BCR_EXTMOD)
486 regmap_update_bits(ebi->regmap, FMC2_BWTR(cs),
488 regmap_update_bits(ebi->regmap, FMC2_BTR(cs), btr_mask, btr);
489 regmap_update_bits(ebi->regmap, FMC2_BCR(cs), bcr_mask, bcr);
494 static int stm32_fmc2_ebi_set_buswidth(struct stm32_fmc2_ebi *ebi,
495 const struct stm32_fmc2_prop *prop,
501 case FMC2_BUSWIDTH_8:
502 val = FIELD_PREP(FMC2_BCR_MWID, FMC2_BCR_MWID_8);
504 case FMC2_BUSWIDTH_16:
505 val = FIELD_PREP(FMC2_BCR_MWID, FMC2_BCR_MWID_16);
508 /* Buswidth not supported */
512 regmap_update_bits(ebi->regmap, FMC2_BCR(cs), FMC2_BCR_MWID, val);
517 static int stm32_fmc2_ebi_set_cpsize(struct stm32_fmc2_ebi *ebi,
518 const struct stm32_fmc2_prop *prop,
525 val = FIELD_PREP(FMC2_BCR_CPSIZE, FMC2_BCR_CPSIZE_0);
527 case FMC2_CPSIZE_128:
528 val = FIELD_PREP(FMC2_BCR_CPSIZE, FMC2_BCR_CPSIZE_128);
530 case FMC2_CPSIZE_256:
531 val = FIELD_PREP(FMC2_BCR_CPSIZE, FMC2_BCR_CPSIZE_256);
533 case FMC2_CPSIZE_512:
534 val = FIELD_PREP(FMC2_BCR_CPSIZE, FMC2_BCR_CPSIZE_512);
536 case FMC2_CPSIZE_1024:
537 val = FIELD_PREP(FMC2_BCR_CPSIZE, FMC2_BCR_CPSIZE_1024);
540 /* Cpsize not supported */
544 regmap_update_bits(ebi->regmap, FMC2_BCR(cs), FMC2_BCR_CPSIZE, val);
549 static int stm32_fmc2_ebi_set_bl_setup(struct stm32_fmc2_ebi *ebi,
550 const struct stm32_fmc2_prop *prop,
555 val = min_t(u32, setup, FMC2_BCR_NBLSET_MAX);
556 val = FIELD_PREP(FMC2_BCR_NBLSET, val);
557 regmap_update_bits(ebi->regmap, FMC2_BCR(cs), FMC2_BCR_NBLSET, val);
562 static int stm32_fmc2_ebi_set_address_setup(struct stm32_fmc2_ebi *ebi,
563 const struct stm32_fmc2_prop *prop,
567 u32 val = FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_D);
570 ret = stm32_fmc2_ebi_get_reg(prop->reg_type, cs, ®);
574 regmap_read(ebi->regmap, FMC2_BCR(cs), &bcr);
575 if (prop->reg_type == FMC2_REG_BWTR)
576 regmap_read(ebi->regmap, FMC2_BWTR(cs), &bxtr);
578 regmap_read(ebi->regmap, FMC2_BTR(cs), &bxtr);
580 if ((bxtr & FMC2_BXTR_ACCMOD) == val || bcr & FMC2_BCR_MUXEN)
581 val = clamp_val(setup, 1, FMC2_BXTR_ADDSET_MAX);
583 val = min_t(u32, setup, FMC2_BXTR_ADDSET_MAX);
584 val = FIELD_PREP(FMC2_BXTR_ADDSET, val);
585 regmap_update_bits(ebi->regmap, reg, FMC2_BXTR_ADDSET, val);
590 static int stm32_fmc2_ebi_set_address_hold(struct stm32_fmc2_ebi *ebi,
591 const struct stm32_fmc2_prop *prop,
597 ret = stm32_fmc2_ebi_get_reg(prop->reg_type, cs, ®);
601 val = clamp_val(setup, 1, FMC2_BXTR_ADDHLD_MAX);
602 val = FIELD_PREP(FMC2_BXTR_ADDHLD, val);
603 regmap_update_bits(ebi->regmap, reg, FMC2_BXTR_ADDHLD, val);
608 static int stm32_fmc2_ebi_set_data_setup(struct stm32_fmc2_ebi *ebi,
609 const struct stm32_fmc2_prop *prop,
615 ret = stm32_fmc2_ebi_get_reg(prop->reg_type, cs, ®);
619 val = clamp_val(setup, 1, FMC2_BXTR_DATAST_MAX);
620 val = FIELD_PREP(FMC2_BXTR_DATAST, val);
621 regmap_update_bits(ebi->regmap, reg, FMC2_BXTR_DATAST, val);
626 static int stm32_fmc2_ebi_set_bus_turnaround(struct stm32_fmc2_ebi *ebi,
627 const struct stm32_fmc2_prop *prop,
633 ret = stm32_fmc2_ebi_get_reg(prop->reg_type, cs, ®);
637 val = setup ? min_t(u32, setup - 1, FMC2_BXTR_BUSTURN_MAX) : 0;
638 val = FIELD_PREP(FMC2_BXTR_BUSTURN, val);
639 regmap_update_bits(ebi->regmap, reg, FMC2_BXTR_BUSTURN, val);
644 static int stm32_fmc2_ebi_set_data_hold(struct stm32_fmc2_ebi *ebi,
645 const struct stm32_fmc2_prop *prop,
651 ret = stm32_fmc2_ebi_get_reg(prop->reg_type, cs, ®);
655 if (prop->reg_type == FMC2_REG_BWTR)
656 val = setup ? min_t(u32, setup - 1, FMC2_BXTR_DATAHLD_MAX) : 0;
658 val = min_t(u32, setup, FMC2_BXTR_DATAHLD_MAX);
659 val = FIELD_PREP(FMC2_BXTR_DATAHLD, val);
660 regmap_update_bits(ebi->regmap, reg, FMC2_BXTR_DATAHLD, val);
665 static int stm32_fmc2_ebi_set_clk_period(struct stm32_fmc2_ebi *ebi,
666 const struct stm32_fmc2_prop *prop,
671 val = setup ? clamp_val(setup - 1, 1, FMC2_BTR_CLKDIV_MAX) : 1;
672 val = FIELD_PREP(FMC2_BTR_CLKDIV, val);
673 regmap_update_bits(ebi->regmap, FMC2_BTR(cs), FMC2_BTR_CLKDIV, val);
678 static int stm32_fmc2_ebi_set_data_latency(struct stm32_fmc2_ebi *ebi,
679 const struct stm32_fmc2_prop *prop,
684 val = setup > 1 ? min_t(u32, setup - 2, FMC2_BTR_DATLAT_MAX) : 0;
685 val = FIELD_PREP(FMC2_BTR_DATLAT, val);
686 regmap_update_bits(ebi->regmap, FMC2_BTR(cs), FMC2_BTR_DATLAT, val);
691 static int stm32_fmc2_ebi_set_max_low_pulse(struct stm32_fmc2_ebi *ebi,
692 const struct stm32_fmc2_prop *prop,
695 u32 old_val, new_val, pcscntr;
700 regmap_read(ebi->regmap, FMC2_PCSCNTR, &pcscntr);
702 /* Enable counter for the bank */
703 regmap_update_bits(ebi->regmap, FMC2_PCSCNTR,
704 FMC2_PCSCNTR_CNTBEN(cs),
705 FMC2_PCSCNTR_CNTBEN(cs));
707 new_val = min_t(u32, setup - 1, FMC2_PCSCNTR_CSCOUNT_MAX);
708 old_val = FIELD_GET(FMC2_PCSCNTR_CSCOUNT, pcscntr);
709 if (old_val && new_val > old_val)
710 /* Keep current counter value */
713 new_val = FIELD_PREP(FMC2_PCSCNTR_CSCOUNT, new_val);
714 regmap_update_bits(ebi->regmap, FMC2_PCSCNTR,
715 FMC2_PCSCNTR_CSCOUNT, new_val);
720 static const struct stm32_fmc2_prop stm32_fmc2_child_props[] = {
721 /* st,fmc2-ebi-cs-trans-type must be the first property */
723 .name = "st,fmc2-ebi-cs-transaction-type",
725 .set = stm32_fmc2_ebi_set_trans_type,
728 .name = "st,fmc2-ebi-cs-cclk-enable",
730 .reg_type = FMC2_REG_BCR,
731 .reg_mask = FMC2_BCR1_CCLKEN,
732 .check = stm32_fmc2_ebi_check_cclk,
733 .set = stm32_fmc2_ebi_set_bit_field,
736 .name = "st,fmc2-ebi-cs-mux-enable",
738 .reg_type = FMC2_REG_BCR,
739 .reg_mask = FMC2_BCR_MUXEN,
740 .check = stm32_fmc2_ebi_check_mux,
741 .set = stm32_fmc2_ebi_set_bit_field,
744 .name = "st,fmc2-ebi-cs-buswidth",
745 .reset_val = FMC2_BUSWIDTH_16,
746 .set = stm32_fmc2_ebi_set_buswidth,
749 .name = "st,fmc2-ebi-cs-waitpol-high",
751 .reg_type = FMC2_REG_BCR,
752 .reg_mask = FMC2_BCR_WAITPOL,
753 .set = stm32_fmc2_ebi_set_bit_field,
756 .name = "st,fmc2-ebi-cs-waitcfg-enable",
758 .reg_type = FMC2_REG_BCR,
759 .reg_mask = FMC2_BCR_WAITCFG,
760 .check = stm32_fmc2_ebi_check_waitcfg,
761 .set = stm32_fmc2_ebi_set_bit_field,
764 .name = "st,fmc2-ebi-cs-wait-enable",
766 .reg_type = FMC2_REG_BCR,
767 .reg_mask = FMC2_BCR_WAITEN,
768 .check = stm32_fmc2_ebi_check_sync_trans,
769 .set = stm32_fmc2_ebi_set_bit_field,
772 .name = "st,fmc2-ebi-cs-asyncwait-enable",
774 .reg_type = FMC2_REG_BCR,
775 .reg_mask = FMC2_BCR_ASYNCWAIT,
776 .check = stm32_fmc2_ebi_check_async_trans,
777 .set = stm32_fmc2_ebi_set_bit_field,
780 .name = "st,fmc2-ebi-cs-cpsize",
781 .check = stm32_fmc2_ebi_check_cpsize,
782 .set = stm32_fmc2_ebi_set_cpsize,
785 .name = "st,fmc2-ebi-cs-byte-lane-setup-ns",
786 .calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
787 .set = stm32_fmc2_ebi_set_bl_setup,
790 .name = "st,fmc2-ebi-cs-address-setup-ns",
791 .reg_type = FMC2_REG_BTR,
792 .reset_val = FMC2_BXTR_ADDSET_MAX,
793 .check = stm32_fmc2_ebi_check_async_trans,
794 .calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
795 .set = stm32_fmc2_ebi_set_address_setup,
798 .name = "st,fmc2-ebi-cs-address-hold-ns",
799 .reg_type = FMC2_REG_BTR,
800 .reset_val = FMC2_BXTR_ADDHLD_MAX,
801 .check = stm32_fmc2_ebi_check_address_hold,
802 .calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
803 .set = stm32_fmc2_ebi_set_address_hold,
806 .name = "st,fmc2-ebi-cs-data-setup-ns",
807 .reg_type = FMC2_REG_BTR,
808 .reset_val = FMC2_BXTR_DATAST_MAX,
809 .check = stm32_fmc2_ebi_check_async_trans,
810 .calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
811 .set = stm32_fmc2_ebi_set_data_setup,
814 .name = "st,fmc2-ebi-cs-bus-turnaround-ns",
815 .reg_type = FMC2_REG_BTR,
816 .reset_val = FMC2_BXTR_BUSTURN_MAX + 1,
817 .calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
818 .set = stm32_fmc2_ebi_set_bus_turnaround,
821 .name = "st,fmc2-ebi-cs-data-hold-ns",
822 .reg_type = FMC2_REG_BTR,
823 .check = stm32_fmc2_ebi_check_async_trans,
824 .calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
825 .set = stm32_fmc2_ebi_set_data_hold,
828 .name = "st,fmc2-ebi-cs-clk-period-ns",
829 .reset_val = FMC2_BTR_CLKDIV_MAX + 1,
830 .check = stm32_fmc2_ebi_check_clk_period,
831 .calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
832 .set = stm32_fmc2_ebi_set_clk_period,
835 .name = "st,fmc2-ebi-cs-data-latency-ns",
836 .check = stm32_fmc2_ebi_check_sync_trans,
837 .calculate = stm32_fmc2_ebi_ns_to_clk_period,
838 .set = stm32_fmc2_ebi_set_data_latency,
841 .name = "st,fmc2-ebi-cs-write-address-setup-ns",
842 .reg_type = FMC2_REG_BWTR,
843 .reset_val = FMC2_BXTR_ADDSET_MAX,
844 .check = stm32_fmc2_ebi_check_async_trans,
845 .calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
846 .set = stm32_fmc2_ebi_set_address_setup,
849 .name = "st,fmc2-ebi-cs-write-address-hold-ns",
850 .reg_type = FMC2_REG_BWTR,
851 .reset_val = FMC2_BXTR_ADDHLD_MAX,
852 .check = stm32_fmc2_ebi_check_address_hold,
853 .calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
854 .set = stm32_fmc2_ebi_set_address_hold,
857 .name = "st,fmc2-ebi-cs-write-data-setup-ns",
858 .reg_type = FMC2_REG_BWTR,
859 .reset_val = FMC2_BXTR_DATAST_MAX,
860 .check = stm32_fmc2_ebi_check_async_trans,
861 .calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
862 .set = stm32_fmc2_ebi_set_data_setup,
865 .name = "st,fmc2-ebi-cs-write-bus-turnaround-ns",
866 .reg_type = FMC2_REG_BWTR,
867 .reset_val = FMC2_BXTR_BUSTURN_MAX + 1,
868 .calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
869 .set = stm32_fmc2_ebi_set_bus_turnaround,
872 .name = "st,fmc2-ebi-cs-write-data-hold-ns",
873 .reg_type = FMC2_REG_BWTR,
874 .check = stm32_fmc2_ebi_check_async_trans,
875 .calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
876 .set = stm32_fmc2_ebi_set_data_hold,
879 .name = "st,fmc2-ebi-cs-max-low-pulse-ns",
880 .calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
881 .set = stm32_fmc2_ebi_set_max_low_pulse,
885 static int stm32_fmc2_ebi_parse_prop(struct stm32_fmc2_ebi *ebi,
886 struct device_node *dev_node,
887 const struct stm32_fmc2_prop *prop,
890 struct device *dev = ebi->dev;
894 dev_err(dev, "property %s is not well defined\n", prop->name);
898 if (prop->check && prop->check(ebi, prop, cs))
899 /* Skeep this property */
905 bprop = of_property_read_bool(dev_node, prop->name);
906 if (prop->mprop && !bprop) {
907 dev_err(dev, "mandatory property %s not defined in the device tree\n",
918 ret = of_property_read_u32(dev_node, prop->name, &val);
919 if (prop->mprop && ret) {
920 dev_err(dev, "mandatory property %s not defined in the device tree\n",
926 setup = prop->reset_val;
927 else if (prop->calculate)
928 setup = prop->calculate(ebi, cs, val);
933 return prop->set(ebi, prop, cs, setup);
936 static void stm32_fmc2_ebi_enable_bank(struct stm32_fmc2_ebi *ebi, int cs)
938 regmap_update_bits(ebi->regmap, FMC2_BCR(cs),
939 FMC2_BCR_MBKEN, FMC2_BCR_MBKEN);
942 static void stm32_fmc2_ebi_disable_bank(struct stm32_fmc2_ebi *ebi, int cs)
944 regmap_update_bits(ebi->regmap, FMC2_BCR(cs), FMC2_BCR_MBKEN, 0);
947 static void stm32_fmc2_ebi_save_setup(struct stm32_fmc2_ebi *ebi)
951 for (cs = 0; cs < FMC2_MAX_EBI_CE; cs++) {
952 regmap_read(ebi->regmap, FMC2_BCR(cs), &ebi->bcr[cs]);
953 regmap_read(ebi->regmap, FMC2_BTR(cs), &ebi->btr[cs]);
954 regmap_read(ebi->regmap, FMC2_BWTR(cs), &ebi->bwtr[cs]);
957 regmap_read(ebi->regmap, FMC2_PCSCNTR, &ebi->pcscntr);
960 static void stm32_fmc2_ebi_set_setup(struct stm32_fmc2_ebi *ebi)
964 for (cs = 0; cs < FMC2_MAX_EBI_CE; cs++) {
965 regmap_write(ebi->regmap, FMC2_BCR(cs), ebi->bcr[cs]);
966 regmap_write(ebi->regmap, FMC2_BTR(cs), ebi->btr[cs]);
967 regmap_write(ebi->regmap, FMC2_BWTR(cs), ebi->bwtr[cs]);
970 regmap_write(ebi->regmap, FMC2_PCSCNTR, ebi->pcscntr);
973 static void stm32_fmc2_ebi_disable_banks(struct stm32_fmc2_ebi *ebi)
977 for (cs = 0; cs < FMC2_MAX_EBI_CE; cs++) {
978 if (!(ebi->bank_assigned & BIT(cs)))
981 stm32_fmc2_ebi_disable_bank(ebi, cs);
985 /* NWAIT signal can not be connected to EBI controller and NAND controller */
986 static bool stm32_fmc2_ebi_nwait_used_by_ctrls(struct stm32_fmc2_ebi *ebi)
991 for (cs = 0; cs < FMC2_MAX_EBI_CE; cs++) {
992 if (!(ebi->bank_assigned & BIT(cs)))
995 regmap_read(ebi->regmap, FMC2_BCR(cs), &bcr);
996 if ((bcr & FMC2_BCR_WAITEN || bcr & FMC2_BCR_ASYNCWAIT) &&
997 ebi->bank_assigned & BIT(FMC2_NAND))
1004 static void stm32_fmc2_ebi_enable(struct stm32_fmc2_ebi *ebi)
1006 regmap_update_bits(ebi->regmap, FMC2_BCR1,
1007 FMC2_BCR1_FMC2EN, FMC2_BCR1_FMC2EN);
1010 static void stm32_fmc2_ebi_disable(struct stm32_fmc2_ebi *ebi)
1012 regmap_update_bits(ebi->regmap, FMC2_BCR1, FMC2_BCR1_FMC2EN, 0);
1015 static int stm32_fmc2_ebi_setup_cs(struct stm32_fmc2_ebi *ebi,
1016 struct device_node *dev_node,
1022 stm32_fmc2_ebi_disable_bank(ebi, cs);
1024 for (i = 0; i < ARRAY_SIZE(stm32_fmc2_child_props); i++) {
1025 const struct stm32_fmc2_prop *p = &stm32_fmc2_child_props[i];
1027 ret = stm32_fmc2_ebi_parse_prop(ebi, dev_node, p, cs);
1029 dev_err(ebi->dev, "property %s could not be set: %d\n",
1035 stm32_fmc2_ebi_enable_bank(ebi, cs);
1040 static int stm32_fmc2_ebi_parse_dt(struct stm32_fmc2_ebi *ebi)
1042 struct device *dev = ebi->dev;
1043 struct device_node *child;
1044 bool child_found = false;
1048 for_each_available_child_of_node(dev->of_node, child) {
1049 ret = of_property_read_u32(child, "reg", &bank);
1051 dev_err(dev, "could not retrieve reg property: %d\n",
1057 if (bank >= FMC2_MAX_BANKS) {
1058 dev_err(dev, "invalid reg value: %d\n", bank);
1063 if (ebi->bank_assigned & BIT(bank)) {
1064 dev_err(dev, "bank already assigned: %d\n", bank);
1069 if (bank < FMC2_MAX_EBI_CE) {
1070 ret = stm32_fmc2_ebi_setup_cs(ebi, child, bank);
1072 dev_err(dev, "setup chip select %d failed: %d\n",
1079 ebi->bank_assigned |= BIT(bank);
1084 dev_warn(dev, "no subnodes found, disable the driver.\n");
1088 if (stm32_fmc2_ebi_nwait_used_by_ctrls(ebi)) {
1089 dev_err(dev, "NWAIT signal connected to EBI and NAND controllers\n");
1093 stm32_fmc2_ebi_enable(ebi);
1095 return of_platform_populate(dev->of_node, NULL, NULL, dev);
1098 static int stm32_fmc2_ebi_probe(struct platform_device *pdev)
1100 struct device *dev = &pdev->dev;
1101 struct stm32_fmc2_ebi *ebi;
1102 struct reset_control *rstc;
1105 ebi = devm_kzalloc(&pdev->dev, sizeof(*ebi), GFP_KERNEL);
1111 ebi->regmap = device_node_to_regmap(dev->of_node);
1112 if (IS_ERR(ebi->regmap))
1113 return PTR_ERR(ebi->regmap);
1115 ebi->clk = devm_clk_get(dev, NULL);
1116 if (IS_ERR(ebi->clk))
1117 return PTR_ERR(ebi->clk);
1119 rstc = devm_reset_control_get(dev, NULL);
1120 if (PTR_ERR(rstc) == -EPROBE_DEFER)
1121 return -EPROBE_DEFER;
1123 ret = clk_prepare_enable(ebi->clk);
1127 if (!IS_ERR(rstc)) {
1128 reset_control_assert(rstc);
1129 reset_control_deassert(rstc);
1132 ret = stm32_fmc2_ebi_parse_dt(ebi);
1136 stm32_fmc2_ebi_save_setup(ebi);
1137 platform_set_drvdata(pdev, ebi);
1142 stm32_fmc2_ebi_disable_banks(ebi);
1143 stm32_fmc2_ebi_disable(ebi);
1144 clk_disable_unprepare(ebi->clk);
1149 static int stm32_fmc2_ebi_remove(struct platform_device *pdev)
1151 struct stm32_fmc2_ebi *ebi = platform_get_drvdata(pdev);
1153 of_platform_depopulate(&pdev->dev);
1154 stm32_fmc2_ebi_disable_banks(ebi);
1155 stm32_fmc2_ebi_disable(ebi);
1156 clk_disable_unprepare(ebi->clk);
1161 static int __maybe_unused stm32_fmc2_ebi_suspend(struct device *dev)
1163 struct stm32_fmc2_ebi *ebi = dev_get_drvdata(dev);
1165 stm32_fmc2_ebi_disable(ebi);
1166 clk_disable_unprepare(ebi->clk);
1167 pinctrl_pm_select_sleep_state(dev);
1172 static int __maybe_unused stm32_fmc2_ebi_resume(struct device *dev)
1174 struct stm32_fmc2_ebi *ebi = dev_get_drvdata(dev);
1177 pinctrl_pm_select_default_state(dev);
1179 ret = clk_prepare_enable(ebi->clk);
1183 stm32_fmc2_ebi_set_setup(ebi);
1184 stm32_fmc2_ebi_enable(ebi);
1189 static SIMPLE_DEV_PM_OPS(stm32_fmc2_ebi_pm_ops, stm32_fmc2_ebi_suspend,
1190 stm32_fmc2_ebi_resume);
1192 static const struct of_device_id stm32_fmc2_ebi_match[] = {
1193 {.compatible = "st,stm32mp1-fmc2-ebi"},
1196 MODULE_DEVICE_TABLE(of, stm32_fmc2_ebi_match);
1198 static struct platform_driver stm32_fmc2_ebi_driver = {
1199 .probe = stm32_fmc2_ebi_probe,
1200 .remove = stm32_fmc2_ebi_remove,
1202 .name = "stm32_fmc2_ebi",
1203 .of_match_table = stm32_fmc2_ebi_match,
1204 .pm = &stm32_fmc2_ebi_pm_ops,
1207 module_platform_driver(stm32_fmc2_ebi_driver);
1209 MODULE_ALIAS("platform:stm32_fmc2_ebi");
1210 MODULE_AUTHOR("Christophe Kerello <christophe.kerello@st.com>");
1211 MODULE_DESCRIPTION("STMicroelectronics STM32 FMC2 ebi driver");
1212 MODULE_LICENSE("GPL v2");