2 * Atmel SMC (Static Memory Controller) helper functions.
4 * Copyright (C) 2017 Atmel
5 * Copyright (C) 2017 Free Electrons
7 * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/mfd/syscon/atmel-smc.h>
17 * atmel_smc_cs_conf_init - initialize a SMC CS conf
18 * @conf: the SMC CS conf to initialize
20 * Set all fields to 0 so that one can start defining a new config.
22 void atmel_smc_cs_conf_init(struct atmel_smc_cs_conf
*conf
)
24 memset(conf
, 0, sizeof(*conf
));
26 EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_init
);
29 * atmel_smc_cs_encode_ncycles - encode a number of MCK clk cycles in the
30 * format expected by the SMC engine
31 * @ncycles: number of MCK clk cycles
32 * @msbpos: position of the MSB part of the timing field
33 * @msbwidth: width of the MSB part of the timing field
34 * @msbfactor: factor applied to the MSB
35 * @encodedval: param used to store the encoding result
37 * This function encodes the @ncycles value as described in the datasheet
38 * (section "SMC Setup/Pulse/Cycle/Timings Register"). This is a generic
39 * helper which called with different parameter depending on the encoding
42 * If the @ncycles value is too big to be encoded, -ERANGE is returned and
43 * the encodedval is contains the maximum val. Otherwise, 0 is returned.
45 static int atmel_smc_cs_encode_ncycles(unsigned int ncycles
,
47 unsigned int msbwidth
,
48 unsigned int msbfactor
,
49 unsigned int *encodedval
)
51 unsigned int lsbmask
= GENMASK(msbpos
- 1, 0);
52 unsigned int msbmask
= GENMASK(msbwidth
- 1, 0);
53 unsigned int msb
, lsb
;
56 msb
= ncycles
/ msbfactor
;
57 lsb
= ncycles
% msbfactor
;
65 * Let's just put the maximum we can if the requested setting does
66 * not fit in the register field.
67 * We still return -ERANGE in case the caller cares.
75 *encodedval
= (msb
<< msbpos
) | lsb
;
81 * atmel_smc_cs_conf_set_timing - set the SMC CS conf Txx parameter to a
83 * @conf: SMC CS conf descriptor
84 * @shift: the position of the Txx field in the TIMINGS register
85 * @ncycles: value (expressed in MCK clk cycles) to assign to this Txx
88 * This function encodes the @ncycles value as described in the datasheet
89 * (section "SMC Timings Register"), and then stores the result in the
90 * @conf->timings field at @shift position.
92 * Returns -EINVAL if shift is invalid, -ERANGE if ncycles does not fit in
93 * the field, and 0 otherwise.
95 int atmel_smc_cs_conf_set_timing(struct atmel_smc_cs_conf
*conf
,
96 unsigned int shift
, unsigned int ncycles
)
101 if (shift
!= ATMEL_HSMC_TIMINGS_TCLR_SHIFT
&&
102 shift
!= ATMEL_HSMC_TIMINGS_TADL_SHIFT
&&
103 shift
!= ATMEL_HSMC_TIMINGS_TAR_SHIFT
&&
104 shift
!= ATMEL_HSMC_TIMINGS_TRR_SHIFT
&&
105 shift
!= ATMEL_HSMC_TIMINGS_TWB_SHIFT
)
109 * The formula described in atmel datasheets (section "HSMC Timings
112 * ncycles = (Txx[3] * 64) + Txx[2:0]
114 ret
= atmel_smc_cs_encode_ncycles(ncycles
, 3, 1, 64, &val
);
115 conf
->timings
&= ~GENMASK(shift
+ 3, shift
);
116 conf
->timings
|= val
<< shift
;
120 EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_set_timing
);
123 * atmel_smc_cs_conf_set_setup - set the SMC CS conf xx_SETUP parameter to a
125 * @conf: SMC CS conf descriptor
126 * @shift: the position of the xx_SETUP field in the SETUP register
127 * @ncycles: value (expressed in MCK clk cycles) to assign to this xx_SETUP
130 * This function encodes the @ncycles value as described in the datasheet
131 * (section "SMC Setup Register"), and then stores the result in the
132 * @conf->setup field at @shift position.
134 * Returns -EINVAL if @shift is invalid, -ERANGE if @ncycles does not fit in
135 * the field, and 0 otherwise.
137 int atmel_smc_cs_conf_set_setup(struct atmel_smc_cs_conf
*conf
,
138 unsigned int shift
, unsigned int ncycles
)
143 if (shift
!= ATMEL_SMC_NWE_SHIFT
&& shift
!= ATMEL_SMC_NCS_WR_SHIFT
&&
144 shift
!= ATMEL_SMC_NRD_SHIFT
&& shift
!= ATMEL_SMC_NCS_RD_SHIFT
)
148 * The formula described in atmel datasheets (section "SMC Setup
151 * ncycles = (128 * xx_SETUP[5]) + xx_SETUP[4:0]
153 ret
= atmel_smc_cs_encode_ncycles(ncycles
, 5, 1, 128, &val
);
154 conf
->setup
&= ~GENMASK(shift
+ 7, shift
);
155 conf
->setup
|= val
<< shift
;
159 EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_set_setup
);
162 * atmel_smc_cs_conf_set_pulse - set the SMC CS conf xx_PULSE parameter to a
164 * @conf: SMC CS conf descriptor
165 * @shift: the position of the xx_PULSE field in the PULSE register
166 * @ncycles: value (expressed in MCK clk cycles) to assign to this xx_PULSE
169 * This function encodes the @ncycles value as described in the datasheet
170 * (section "SMC Pulse Register"), and then stores the result in the
171 * @conf->setup field at @shift position.
173 * Returns -EINVAL if @shift is invalid, -ERANGE if @ncycles does not fit in
174 * the field, and 0 otherwise.
176 int atmel_smc_cs_conf_set_pulse(struct atmel_smc_cs_conf
*conf
,
177 unsigned int shift
, unsigned int ncycles
)
182 if (shift
!= ATMEL_SMC_NWE_SHIFT
&& shift
!= ATMEL_SMC_NCS_WR_SHIFT
&&
183 shift
!= ATMEL_SMC_NRD_SHIFT
&& shift
!= ATMEL_SMC_NCS_RD_SHIFT
)
187 * The formula described in atmel datasheets (section "SMC Pulse
190 * ncycles = (256 * xx_PULSE[6]) + xx_PULSE[5:0]
192 ret
= atmel_smc_cs_encode_ncycles(ncycles
, 6, 1, 256, &val
);
193 conf
->pulse
&= ~GENMASK(shift
+ 7, shift
);
194 conf
->pulse
|= val
<< shift
;
198 EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_set_pulse
);
201 * atmel_smc_cs_conf_set_cycle - set the SMC CS conf xx_CYCLE parameter to a
203 * @conf: SMC CS conf descriptor
204 * @shift: the position of the xx_CYCLE field in the CYCLE register
205 * @ncycles: value (expressed in MCK clk cycles) to assign to this xx_CYCLE
208 * This function encodes the @ncycles value as described in the datasheet
209 * (section "SMC Pulse Register"), and then stores the result in the
210 * @conf->setup field at @shift position.
212 * Returns -EINVAL if @shift is invalid, -ERANGE if @ncycles does not fit in
213 * the field, and 0 otherwise.
215 int atmel_smc_cs_conf_set_cycle(struct atmel_smc_cs_conf
*conf
,
216 unsigned int shift
, unsigned int ncycles
)
221 if (shift
!= ATMEL_SMC_NWE_SHIFT
&& shift
!= ATMEL_SMC_NRD_SHIFT
)
225 * The formula described in atmel datasheets (section "SMC Cycle
228 * ncycles = (xx_CYCLE[8:7] * 256) + xx_CYCLE[6:0]
230 ret
= atmel_smc_cs_encode_ncycles(ncycles
, 7, 2, 256, &val
);
231 conf
->cycle
&= ~GENMASK(shift
+ 15, shift
);
232 conf
->cycle
|= val
<< shift
;
236 EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_set_cycle
);
239 * atmel_smc_cs_conf_apply - apply an SMC CS conf
240 * @regmap: the SMC regmap
242 * @conf the SMC CS conf to apply
244 * Applies an SMC CS configuration.
245 * Only valid on at91sam9/avr32 SoCs.
247 void atmel_smc_cs_conf_apply(struct regmap
*regmap
, int cs
,
248 const struct atmel_smc_cs_conf
*conf
)
250 regmap_write(regmap
, ATMEL_SMC_SETUP(cs
), conf
->setup
);
251 regmap_write(regmap
, ATMEL_SMC_PULSE(cs
), conf
->pulse
);
252 regmap_write(regmap
, ATMEL_SMC_CYCLE(cs
), conf
->cycle
);
253 regmap_write(regmap
, ATMEL_SMC_MODE(cs
), conf
->mode
);
255 EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_apply
);
258 * atmel_hsmc_cs_conf_apply - apply an SMC CS conf
259 * @regmap: the HSMC regmap
261 * @conf the SMC CS conf to apply
263 * Applies an SMC CS configuration.
264 * Only valid on post-sama5 SoCs.
266 void atmel_hsmc_cs_conf_apply(struct regmap
*regmap
, int cs
,
267 const struct atmel_smc_cs_conf
*conf
)
269 regmap_write(regmap
, ATMEL_HSMC_SETUP(cs
), conf
->setup
);
270 regmap_write(regmap
, ATMEL_HSMC_PULSE(cs
), conf
->pulse
);
271 regmap_write(regmap
, ATMEL_HSMC_CYCLE(cs
), conf
->cycle
);
272 regmap_write(regmap
, ATMEL_HSMC_TIMINGS(cs
), conf
->timings
);
273 regmap_write(regmap
, ATMEL_HSMC_MODE(cs
), conf
->mode
);
275 EXPORT_SYMBOL_GPL(atmel_hsmc_cs_conf_apply
);
278 * atmel_smc_cs_conf_get - retrieve the current SMC CS conf
279 * @regmap: the SMC regmap
281 * @conf: the SMC CS conf object to store the current conf
283 * Retrieve the SMC CS configuration.
284 * Only valid on at91sam9/avr32 SoCs.
286 void atmel_smc_cs_conf_get(struct regmap
*regmap
, int cs
,
287 struct atmel_smc_cs_conf
*conf
)
289 regmap_read(regmap
, ATMEL_SMC_SETUP(cs
), &conf
->setup
);
290 regmap_read(regmap
, ATMEL_SMC_PULSE(cs
), &conf
->pulse
);
291 regmap_read(regmap
, ATMEL_SMC_CYCLE(cs
), &conf
->cycle
);
292 regmap_read(regmap
, ATMEL_SMC_MODE(cs
), &conf
->mode
);
294 EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_get
);
297 * atmel_hsmc_cs_conf_get - retrieve the current SMC CS conf
298 * @regmap: the HSMC regmap
300 * @conf: the SMC CS conf object to store the current conf
302 * Retrieve the SMC CS configuration.
303 * Only valid on post-sama5 SoCs.
305 void atmel_hsmc_cs_conf_get(struct regmap
*regmap
, int cs
,
306 struct atmel_smc_cs_conf
*conf
)
308 regmap_read(regmap
, ATMEL_HSMC_SETUP(cs
), &conf
->setup
);
309 regmap_read(regmap
, ATMEL_HSMC_PULSE(cs
), &conf
->pulse
);
310 regmap_read(regmap
, ATMEL_HSMC_CYCLE(cs
), &conf
->cycle
);
311 regmap_read(regmap
, ATMEL_HSMC_TIMINGS(cs
), &conf
->timings
);
312 regmap_read(regmap
, ATMEL_HSMC_MODE(cs
), &conf
->mode
);
314 EXPORT_SYMBOL_GPL(atmel_hsmc_cs_conf_get
);