1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/mfd/syscon/atmel-smc.h>
15#include <linux/string.h>
16
17
18
19
20
21
22
23void atmel_smc_cs_conf_init(struct atmel_smc_cs_conf *conf)
24{
25 memset(conf, 0, sizeof(*conf));
26}
27EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_init);
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46static int atmel_smc_cs_encode_ncycles(unsigned int ncycles,
47 unsigned int msbpos,
48 unsigned int msbwidth,
49 unsigned int msbfactor,
50 unsigned int *encodedval)
51{
52 unsigned int lsbmask = GENMASK(msbpos - 1, 0);
53 unsigned int msbmask = GENMASK(msbwidth - 1, 0);
54 unsigned int msb, lsb;
55 int ret = 0;
56
57 msb = ncycles / msbfactor;
58 lsb = ncycles % msbfactor;
59
60 if (lsb > lsbmask) {
61 lsb = 0;
62 msb++;
63 }
64
65
66
67
68
69
70 if (msb > msbmask) {
71 msb = msbmask;
72 lsb = lsbmask;
73 ret = -ERANGE;
74 }
75
76 *encodedval = (msb << msbpos) | lsb;
77
78 return ret;
79}
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96int atmel_smc_cs_conf_set_timing(struct atmel_smc_cs_conf *conf,
97 unsigned int shift, unsigned int ncycles)
98{
99 unsigned int val;
100 int ret;
101
102 if (shift != ATMEL_HSMC_TIMINGS_TCLR_SHIFT &&
103 shift != ATMEL_HSMC_TIMINGS_TADL_SHIFT &&
104 shift != ATMEL_HSMC_TIMINGS_TAR_SHIFT &&
105 shift != ATMEL_HSMC_TIMINGS_TRR_SHIFT &&
106 shift != ATMEL_HSMC_TIMINGS_TWB_SHIFT)
107 return -EINVAL;
108
109
110
111
112
113
114
115 ret = atmel_smc_cs_encode_ncycles(ncycles, 3, 1, 64, &val);
116 conf->timings &= ~GENMASK(shift + 3, shift);
117 conf->timings |= val << shift;
118
119 return ret;
120}
121EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_set_timing);
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138int atmel_smc_cs_conf_set_setup(struct atmel_smc_cs_conf *conf,
139 unsigned int shift, unsigned int ncycles)
140{
141 unsigned int val;
142 int ret;
143
144 if (shift != ATMEL_SMC_NWE_SHIFT && shift != ATMEL_SMC_NCS_WR_SHIFT &&
145 shift != ATMEL_SMC_NRD_SHIFT && shift != ATMEL_SMC_NCS_RD_SHIFT)
146 return -EINVAL;
147
148
149
150
151
152
153
154 ret = atmel_smc_cs_encode_ncycles(ncycles, 5, 1, 128, &val);
155 conf->setup &= ~GENMASK(shift + 7, shift);
156 conf->setup |= val << shift;
157
158 return ret;
159}
160EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_set_setup);
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177int atmel_smc_cs_conf_set_pulse(struct atmel_smc_cs_conf *conf,
178 unsigned int shift, unsigned int ncycles)
179{
180 unsigned int val;
181 int ret;
182
183 if (shift != ATMEL_SMC_NWE_SHIFT && shift != ATMEL_SMC_NCS_WR_SHIFT &&
184 shift != ATMEL_SMC_NRD_SHIFT && shift != ATMEL_SMC_NCS_RD_SHIFT)
185 return -EINVAL;
186
187
188
189
190
191
192
193 ret = atmel_smc_cs_encode_ncycles(ncycles, 6, 1, 256, &val);
194 conf->pulse &= ~GENMASK(shift + 7, shift);
195 conf->pulse |= val << shift;
196
197 return ret;
198}
199EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_set_pulse);
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216int atmel_smc_cs_conf_set_cycle(struct atmel_smc_cs_conf *conf,
217 unsigned int shift, unsigned int ncycles)
218{
219 unsigned int val;
220 int ret;
221
222 if (shift != ATMEL_SMC_NWE_SHIFT && shift != ATMEL_SMC_NRD_SHIFT)
223 return -EINVAL;
224
225
226
227
228
229
230
231 ret = atmel_smc_cs_encode_ncycles(ncycles, 7, 2, 256, &val);
232 conf->cycle &= ~GENMASK(shift + 15, shift);
233 conf->cycle |= val << shift;
234
235 return ret;
236}
237EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_set_cycle);
238
239
240
241
242
243
244
245
246
247
248void atmel_smc_cs_conf_apply(struct regmap *regmap, int cs,
249 const struct atmel_smc_cs_conf *conf)
250{
251 regmap_write(regmap, ATMEL_SMC_SETUP(cs), conf->setup);
252 regmap_write(regmap, ATMEL_SMC_PULSE(cs), conf->pulse);
253 regmap_write(regmap, ATMEL_SMC_CYCLE(cs), conf->cycle);
254 regmap_write(regmap, ATMEL_SMC_MODE(cs), conf->mode);
255}
256EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_apply);
257
258
259
260
261
262
263
264
265
266
267
268void atmel_hsmc_cs_conf_apply(struct regmap *regmap,
269 const struct atmel_hsmc_reg_layout *layout,
270 int cs, const struct atmel_smc_cs_conf *conf)
271{
272 regmap_write(regmap, ATMEL_HSMC_SETUP(layout, cs), conf->setup);
273 regmap_write(regmap, ATMEL_HSMC_PULSE(layout, cs), conf->pulse);
274 regmap_write(regmap, ATMEL_HSMC_CYCLE(layout, cs), conf->cycle);
275 regmap_write(regmap, ATMEL_HSMC_TIMINGS(layout, cs), conf->timings);
276 regmap_write(regmap, ATMEL_HSMC_MODE(layout, cs), conf->mode);
277}
278EXPORT_SYMBOL_GPL(atmel_hsmc_cs_conf_apply);
279
280
281
282
283
284
285
286
287
288
289void atmel_smc_cs_conf_get(struct regmap *regmap, int cs,
290 struct atmel_smc_cs_conf *conf)
291{
292 regmap_read(regmap, ATMEL_SMC_SETUP(cs), &conf->setup);
293 regmap_read(regmap, ATMEL_SMC_PULSE(cs), &conf->pulse);
294 regmap_read(regmap, ATMEL_SMC_CYCLE(cs), &conf->cycle);
295 regmap_read(regmap, ATMEL_SMC_MODE(cs), &conf->mode);
296}
297EXPORT_SYMBOL_GPL(atmel_smc_cs_conf_get);
298
299
300
301
302
303
304
305
306
307
308
309void atmel_hsmc_cs_conf_get(struct regmap *regmap,
310 const struct atmel_hsmc_reg_layout *layout,
311 int cs, struct atmel_smc_cs_conf *conf)
312{
313 regmap_read(regmap, ATMEL_HSMC_SETUP(layout, cs), &conf->setup);
314 regmap_read(regmap, ATMEL_HSMC_PULSE(layout, cs), &conf->pulse);
315 regmap_read(regmap, ATMEL_HSMC_CYCLE(layout, cs), &conf->cycle);
316 regmap_read(regmap, ATMEL_HSMC_TIMINGS(layout, cs), &conf->timings);
317 regmap_read(regmap, ATMEL_HSMC_MODE(layout, cs), &conf->mode);
318}
319EXPORT_SYMBOL_GPL(atmel_hsmc_cs_conf_get);
320
321static const struct atmel_hsmc_reg_layout sama5d3_reg_layout = {
322 .timing_regs_offset = 0x600,
323};
324
325static const struct atmel_hsmc_reg_layout sama5d2_reg_layout = {
326 .timing_regs_offset = 0x700,
327};
328
329static const struct of_device_id atmel_smc_ids[] = {
330 { .compatible = "atmel,at91sam9260-smc", .data = NULL },
331 { .compatible = "atmel,sama5d3-smc", .data = &sama5d3_reg_layout },
332 { .compatible = "atmel,sama5d2-smc", .data = &sama5d2_reg_layout },
333 { },
334};
335
336
337
338
339
340
341
342
343
344
345const struct atmel_hsmc_reg_layout *
346atmel_hsmc_get_reg_layout(struct device_node *np)
347{
348 const struct of_device_id *match;
349
350 match = of_match_node(atmel_smc_ids, np);
351
352 return match ? match->data : ERR_PTR(-EINVAL);
353}
354EXPORT_SYMBOL_GPL(atmel_hsmc_get_reg_layout);
355