1 // SPDX-License-Identifier: GPL-2.0-only
3 * Atmel SMC (Static Memory Controller) helper functions.
5 * Copyright (C) 2022 Microchip Technology Inc.
6 * Copyright (C) 2017 Free Electrons
8 * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
12 #include <dm/device.h>
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/mfd/syscon/atmel-smc.h>
16 #include <linux/string.h>
19 * atmel_smc_cs_conf_init - initialize a SMC CS conf
20 * @conf: the SMC CS conf to initialize
22 * Set all fields to 0 so that one can start defining a new config.
24 void atmel_smc_cs_conf_init(struct atmel_smc_cs_conf
*conf
)
26 memset(conf
, 0, sizeof(*conf
));
28 EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_init
);
31 * atmel_smc_cs_encode_ncycles - encode a number of MCK clk cycles in the
32 * format expected by the SMC engine
33 * @ncycles: number of MCK clk cycles
34 * @msbpos: position of the MSB part of the timing field
35 * @msbwidth: width of the MSB part of the timing field
36 * @msbfactor: factor applied to the MSB
37 * @encodedval: param used to store the encoding result
39 * This function encodes the @ncycles value as described in the datasheet
40 * (section "SMC Setup/Pulse/Cycle/Timings Register"). This is a generic
41 * helper which called with different parameter depending on the encoding
44 * If the @ncycles value is too big to be encoded, -ERANGE is returned and
45 * the encodedval is contains the maximum val. Otherwise, 0 is returned.
47 static int atmel_smc_cs_encode_ncycles(unsigned int ncycles
,
49 unsigned int msbwidth
,
50 unsigned int msbfactor
,
51 unsigned int *encodedval
)
53 unsigned int lsbmask
= GENMASK(msbpos
- 1, 0);
54 unsigned int msbmask
= GENMASK(msbwidth
- 1, 0);
55 unsigned int msb
, lsb
;
58 msb
= ncycles
/ msbfactor
;
59 lsb
= ncycles
% msbfactor
;
67 * Let's just put the maximum we can if the requested setting does
68 * not fit in the register field.
69 * We still return -ERANGE in case the caller cares.
77 *encodedval
= (msb
<< msbpos
) | lsb
;
83 * atmel_smc_cs_conf_set_timing - set the SMC CS conf Txx parameter to a
85 * @conf: SMC CS conf descriptor
86 * @shift: the position of the Txx field in the TIMINGS register
87 * @ncycles: value (expressed in MCK clk cycles) to assign to this Txx
90 * This function encodes the @ncycles value as described in the datasheet
91 * (section "SMC Timings Register"), and then stores the result in the
92 * @conf->timings field at @shift position.
94 * Returns -EINVAL if shift is invalid, -ERANGE if ncycles does not fit in
95 * the field, and 0 otherwise.
97 int atmel_smc_cs_conf_set_timing(struct atmel_smc_cs_conf
*conf
,
98 unsigned int shift
, unsigned int ncycles
)
103 if (shift
!= ATMEL_HSMC_TIMINGS_TCLR_SHIFT
&&
104 shift
!= ATMEL_HSMC_TIMINGS_TADL_SHIFT
&&
105 shift
!= ATMEL_HSMC_TIMINGS_TAR_SHIFT
&&
106 shift
!= ATMEL_HSMC_TIMINGS_TRR_SHIFT
&&
107 shift
!= ATMEL_HSMC_TIMINGS_TWB_SHIFT
)
111 * The formula described in atmel datasheets (section "HSMC Timings
114 * ncycles = (Txx[3] * 64) + Txx[2:0]
116 ret
= atmel_smc_cs_encode_ncycles(ncycles
, 3, 1, 64, &val
);
117 conf
->timings
&= ~GENMASK(shift
+ 3, shift
);
118 conf
->timings
|= val
<< shift
;
122 EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_set_timing
);
125 * atmel_smc_cs_conf_set_setup - set the SMC CS conf xx_SETUP parameter to a
127 * @conf: SMC CS conf descriptor
128 * @shift: the position of the xx_SETUP field in the SETUP register
129 * @ncycles: value (expressed in MCK clk cycles) to assign to this xx_SETUP
132 * This function encodes the @ncycles value as described in the datasheet
133 * (section "SMC Setup Register"), and then stores the result in the
134 * @conf->setup field at @shift position.
136 * Returns -EINVAL if @shift is invalid, -ERANGE if @ncycles does not fit in
137 * the field, and 0 otherwise.
139 int atmel_smc_cs_conf_set_setup(struct atmel_smc_cs_conf
*conf
,
140 unsigned int shift
, unsigned int ncycles
)
145 if (shift
!= ATMEL_SMC_NWE_SHIFT
&& shift
!= ATMEL_SMC_NCS_WR_SHIFT
&&
146 shift
!= ATMEL_SMC_NRD_SHIFT
&& shift
!= ATMEL_SMC_NCS_RD_SHIFT
)
150 * The formula described in atmel datasheets (section "SMC Setup
153 * ncycles = (128 * xx_SETUP[5]) + xx_SETUP[4:0]
155 ret
= atmel_smc_cs_encode_ncycles(ncycles
, 5, 1, 128, &val
);
156 conf
->setup
&= ~GENMASK(shift
+ 7, shift
);
157 conf
->setup
|= val
<< shift
;
161 EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_set_setup
);
164 * atmel_smc_cs_conf_set_pulse - set the SMC CS conf xx_PULSE parameter to a
166 * @conf: SMC CS conf descriptor
167 * @shift: the position of the xx_PULSE field in the PULSE register
168 * @ncycles: value (expressed in MCK clk cycles) to assign to this xx_PULSE
171 * This function encodes the @ncycles value as described in the datasheet
172 * (section "SMC Pulse Register"), and then stores the result in the
173 * @conf->setup field at @shift position.
175 * Returns -EINVAL if @shift is invalid, -ERANGE if @ncycles does not fit in
176 * the field, and 0 otherwise.
178 int atmel_smc_cs_conf_set_pulse(struct atmel_smc_cs_conf
*conf
,
179 unsigned int shift
, unsigned int ncycles
)
184 if (shift
!= ATMEL_SMC_NWE_SHIFT
&& shift
!= ATMEL_SMC_NCS_WR_SHIFT
&&
185 shift
!= ATMEL_SMC_NRD_SHIFT
&& shift
!= ATMEL_SMC_NCS_RD_SHIFT
)
189 * The formula described in atmel datasheets (section "SMC Pulse
192 * ncycles = (256 * xx_PULSE[6]) + xx_PULSE[5:0]
194 ret
= atmel_smc_cs_encode_ncycles(ncycles
, 6, 1, 256, &val
);
195 conf
->pulse
&= ~GENMASK(shift
+ 7, shift
);
196 conf
->pulse
|= val
<< shift
;
200 EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_set_pulse
);
203 * atmel_smc_cs_conf_set_cycle - set the SMC CS conf xx_CYCLE parameter to a
205 * @conf: SMC CS conf descriptor
206 * @shift: the position of the xx_CYCLE field in the CYCLE register
207 * @ncycles: value (expressed in MCK clk cycles) to assign to this xx_CYCLE
210 * This function encodes the @ncycles value as described in the datasheet
211 * (section "SMC Cycle Register"), and then stores the result in the
212 * @conf->setup field at @shift position.
214 * Returns -EINVAL if @shift is invalid, -ERANGE if @ncycles does not fit in
215 * the field, and 0 otherwise.
217 int atmel_smc_cs_conf_set_cycle(struct atmel_smc_cs_conf
*conf
,
218 unsigned int shift
, unsigned int ncycles
)
223 if (shift
!= ATMEL_SMC_NWE_SHIFT
&& shift
!= ATMEL_SMC_NRD_SHIFT
)
227 * The formula described in atmel datasheets (section "SMC Cycle
230 * ncycles = (xx_CYCLE[8:7] * 256) + xx_CYCLE[6:0]
232 ret
= atmel_smc_cs_encode_ncycles(ncycles
, 7, 2, 256, &val
);
233 conf
->cycle
&= ~GENMASK(shift
+ 15, shift
);
234 conf
->cycle
|= val
<< shift
;
238 EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_set_cycle
);
241 * atmel_smc_cs_conf_apply - apply an SMC CS conf
242 * @regmap: the SMC regmap
244 * @conf: the SMC CS conf to apply
246 * Applies an SMC CS configuration.
247 * Only valid on at91sam9/avr32 SoCs.
249 void atmel_smc_cs_conf_apply(struct regmap
*regmap
, int cs
,
250 const struct atmel_smc_cs_conf
*conf
)
252 regmap_write(regmap
, ATMEL_SMC_SETUP(cs
), conf
->setup
);
253 regmap_write(regmap
, ATMEL_SMC_PULSE(cs
), conf
->pulse
);
254 regmap_write(regmap
, ATMEL_SMC_CYCLE(cs
), conf
->cycle
);
255 regmap_write(regmap
, ATMEL_SMC_MODE(cs
), conf
->mode
);
257 EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_apply
);
260 * atmel_hsmc_cs_conf_apply - apply an SMC CS conf
261 * @regmap: the HSMC regmap
263 * @layout: the layout of registers
264 * @conf: the SMC CS conf to apply
266 * Applies an SMC CS configuration.
267 * Only valid on post-sama5 SoCs.
269 void atmel_hsmc_cs_conf_apply(struct regmap
*regmap
,
270 const struct atmel_hsmc_reg_layout
*layout
,
271 int cs
, const struct atmel_smc_cs_conf
*conf
)
273 regmap_write(regmap
, ATMEL_HSMC_SETUP(layout
, cs
), conf
->setup
);
274 regmap_write(regmap
, ATMEL_HSMC_PULSE(layout
, cs
), conf
->pulse
);
275 regmap_write(regmap
, ATMEL_HSMC_CYCLE(layout
, cs
), conf
->cycle
);
276 regmap_write(regmap
, ATMEL_HSMC_TIMINGS(layout
, cs
), conf
->timings
);
277 regmap_write(regmap
, ATMEL_HSMC_MODE(layout
, cs
), conf
->mode
);
279 EXPORT_SYMBOL_GPL(atmel_hsmc_cs_conf_apply
);
282 * atmel_smc_cs_conf_get - retrieve the current SMC CS conf
283 * @regmap: the SMC regmap
285 * @conf: the SMC CS conf object to store the current conf
287 * Retrieve the SMC CS configuration.
288 * Only valid on at91sam9/avr32 SoCs.
290 void atmel_smc_cs_conf_get(struct regmap
*regmap
, int cs
,
291 struct atmel_smc_cs_conf
*conf
)
293 regmap_read(regmap
, ATMEL_SMC_SETUP(cs
), &conf
->setup
);
294 regmap_read(regmap
, ATMEL_SMC_PULSE(cs
), &conf
->pulse
);
295 regmap_read(regmap
, ATMEL_SMC_CYCLE(cs
), &conf
->cycle
);
296 regmap_read(regmap
, ATMEL_SMC_MODE(cs
), &conf
->mode
);
298 EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_get
);
301 * atmel_hsmc_cs_conf_get - retrieve the current SMC CS conf
302 * @regmap: the HSMC regmap
304 * @layout: the layout of registers
305 * @conf: the SMC CS conf object to store the current conf
307 * Retrieve the SMC CS configuration.
308 * Only valid on post-sama5 SoCs.
310 void atmel_hsmc_cs_conf_get(struct regmap
*regmap
,
311 const struct atmel_hsmc_reg_layout
*layout
,
312 int cs
, struct atmel_smc_cs_conf
*conf
)
314 regmap_read(regmap
, ATMEL_HSMC_SETUP(layout
, cs
), &conf
->setup
);
315 regmap_read(regmap
, ATMEL_HSMC_PULSE(layout
, cs
), &conf
->pulse
);
316 regmap_read(regmap
, ATMEL_HSMC_CYCLE(layout
, cs
), &conf
->cycle
);
317 regmap_read(regmap
, ATMEL_HSMC_TIMINGS(layout
, cs
), &conf
->timings
);
318 regmap_read(regmap
, ATMEL_HSMC_MODE(layout
, cs
), &conf
->mode
);
320 EXPORT_SYMBOL_GPL(atmel_hsmc_cs_conf_get
);
322 static const struct atmel_hsmc_reg_layout sama5d3_reg_layout
= {
323 .timing_regs_offset
= 0x600,
326 static const struct atmel_hsmc_reg_layout sama5d2_reg_layout
= {
327 .timing_regs_offset
= 0x700,
330 static const struct udevice_id atmel_smc_ids
[] = {
331 { .compatible
= "atmel,at91sam9260-smc", .data
= (ulong
)0 },
332 { .compatible
= "atmel,sama5d3-smc", .data
= (ulong
)&sama5d3_reg_layout
},
333 { .compatible
= "atmel,sama5d2-smc", .data
= (ulong
)&sama5d2_reg_layout
},
338 * atmel_hsmc_get_reg_layout - retrieve the layout of HSMC registers
339 * @np: the HSMC regmap
341 * Retrieve the layout of HSMC registers.
343 * Returns NULL in case of SMC, a struct atmel_hsmc_reg_layout pointer
344 * in HSMC case, otherwise ERR_PTR(-EINVAL).
346 const struct atmel_hsmc_reg_layout
*
347 atmel_hsmc_get_reg_layout(ofnode np
)
350 const struct udevice_id
*match
;
354 name
= ofnode_get_property(np
, "compatible", &len
);
356 for (i
= 0; i
< ARRAY_SIZE(atmel_smc_ids
); i
++) {
357 if (!strcmp(name
, atmel_smc_ids
[i
].compatible
)) {
358 match
= &atmel_smc_ids
[i
];
363 return match
? (struct atmel_hsmc_reg_layout
*)match
->data
: ERR_PTR(-EINVAL
);