1
2
3
4
5
6
7
8
9#include <linux/init.h>
10#include <linux/kernel.h>
11#include <linux/errno.h>
12#include <linux/cpufreq.h>
13#include <linux/seq_file.h>
14#include <linux/io.h>
15#include <linux/slab.h>
16
17#include <mach/map.h>
18#include <mach/regs-clock.h>
19
20#include <plat/cpu-freq-core.h>
21
22#include "regs-mem.h"
23
24#define print_ns(x) ((x) / 10), ((x) % 10)
25
26
27
28
29
30
31static void s3c2410_print_timing(const char *pfx,
32 struct s3c_iotimings *timings)
33{
34 struct s3c2410_iobank_timing *bt;
35 int bank;
36
37 for (bank = 0; bank < MAX_BANKS; bank++) {
38 bt = timings->bank[bank].io_2410;
39 if (!bt)
40 continue;
41
42 printk(KERN_DEBUG "%s %d: Tacs=%d.%d, Tcos=%d.%d, Tacc=%d.%d, "
43 "Tcoh=%d.%d, Tcah=%d.%d\n", pfx, bank,
44 print_ns(bt->tacs),
45 print_ns(bt->tcos),
46 print_ns(bt->tacc),
47 print_ns(bt->tcoh),
48 print_ns(bt->tcah));
49 }
50}
51
52
53
54
55
56static inline void __iomem *bank_reg(unsigned int bank)
57{
58 return S3C2410_BANKCON0 + (bank << 2);
59}
60
61
62
63
64
65
66
67
68
69
70static inline int bank_is_io(unsigned long bankcon)
71{
72 return !(bankcon & S3C2410_BANKCON_SDRAM);
73}
74
75
76
77
78
79
80
81
82
83static inline unsigned int to_div(unsigned int cyc, unsigned int hclk_tns)
84{
85 if (cyc == 0)
86 return 0;
87
88 return DIV_ROUND_UP(cyc, hclk_tns);
89}
90
91
92
93
94
95
96
97
98
99
100
101static unsigned int calc_0124(unsigned int cyc, unsigned long hclk_tns,
102 unsigned long *v, int shift)
103{
104 unsigned int div = to_div(cyc, hclk_tns);
105 unsigned long val;
106
107 s3c_freq_iodbg("%s: cyc=%d, hclk=%lu, shift=%d => div %d\n",
108 __func__, cyc, hclk_tns, shift, div);
109
110 switch (div) {
111 case 0:
112 val = 0;
113 break;
114 case 1:
115 val = 1;
116 break;
117 case 2:
118 val = 2;
119 break;
120 case 3:
121 case 4:
122 val = 3;
123 break;
124 default:
125 return -1;
126 }
127
128 *v |= val << shift;
129 return 0;
130}
131
132int calc_tacp(unsigned int cyc, unsigned long hclk, unsigned long *v)
133{
134
135 return 0;
136}
137
138
139
140
141
142
143
144
145
146
147
148
149static int calc_tacc(unsigned int cyc, int nwait_en,
150 unsigned long hclk_tns, unsigned long *v)
151{
152 unsigned int div = to_div(cyc, hclk_tns);
153 unsigned long val;
154
155 s3c_freq_iodbg("%s: cyc=%u, nwait=%d, hclk=%lu => div=%u\n",
156 __func__, cyc, nwait_en, hclk_tns, div);
157
158
159 if (nwait_en && div < 4)
160 div = 4;
161
162 switch (div) {
163 case 0:
164 val = 0;
165 break;
166
167 case 1:
168 case 2:
169 case 3:
170 case 4:
171 val = div - 1;
172 break;
173
174 case 5:
175 case 6:
176 val = 4;
177 break;
178
179 case 7:
180 case 8:
181 val = 5;
182 break;
183
184 case 9:
185 case 10:
186 val = 6;
187 break;
188
189 case 11:
190 case 12:
191 case 13:
192 case 14:
193 val = 7;
194 break;
195
196 default:
197 return -1;
198 }
199
200 *v |= val << 8;
201 return 0;
202}
203
204
205
206
207
208
209
210
211
212
213static int s3c2410_calc_bank(struct s3c_cpufreq_config *cfg,
214 struct s3c2410_iobank_timing *bt)
215{
216 unsigned long hclk = cfg->freq.hclk_tns;
217 unsigned long res;
218 int ret;
219
220 res = bt->bankcon;
221 res &= (S3C2410_BANKCON_SDRAM | S3C2410_BANKCON_PMC16);
222
223
224
225
226
227
228
229
230 ret = calc_0124(bt->tacs, hclk, &res, S3C2410_BANKCON_Tacs_SHIFT);
231 ret |= calc_0124(bt->tcos, hclk, &res, S3C2410_BANKCON_Tcos_SHIFT);
232 ret |= calc_0124(bt->tcah, hclk, &res, S3C2410_BANKCON_Tcah_SHIFT);
233 ret |= calc_0124(bt->tcoh, hclk, &res, S3C2410_BANKCON_Tcoh_SHIFT);
234
235 if (ret)
236 return -EINVAL;
237
238 ret |= calc_tacp(bt->tacp, hclk, &res);
239 ret |= calc_tacc(bt->tacc, bt->nwait_en, hclk, &res);
240
241 if (ret)
242 return -EINVAL;
243
244 bt->bankcon = res;
245 return 0;
246}
247
248static const unsigned int tacc_tab[] = {
249 [0] = 1,
250 [1] = 2,
251 [2] = 3,
252 [3] = 4,
253 [4] = 6,
254 [5] = 9,
255 [6] = 10,
256 [7] = 14,
257};
258
259
260
261
262
263
264static unsigned int get_tacc(unsigned long hclk_tns,
265 unsigned long val)
266{
267 val &= 7;
268 return hclk_tns * tacc_tab[val];
269}
270
271
272
273
274
275
276static unsigned int get_0124(unsigned long hclk_tns,
277 unsigned long val)
278{
279 val &= 3;
280 return hclk_tns * ((val == 3) ? 4 : val);
281}
282
283
284
285
286
287
288
289
290
291void s3c2410_iotiming_getbank(struct s3c_cpufreq_config *cfg,
292 struct s3c2410_iobank_timing *bt)
293{
294 unsigned long bankcon = bt->bankcon;
295 unsigned long hclk = cfg->freq.hclk_tns;
296
297 bt->tcah = get_0124(hclk, bankcon >> S3C2410_BANKCON_Tcah_SHIFT);
298 bt->tcoh = get_0124(hclk, bankcon >> S3C2410_BANKCON_Tcoh_SHIFT);
299 bt->tcos = get_0124(hclk, bankcon >> S3C2410_BANKCON_Tcos_SHIFT);
300 bt->tacs = get_0124(hclk, bankcon >> S3C2410_BANKCON_Tacs_SHIFT);
301 bt->tacc = get_tacc(hclk, bankcon >> S3C2410_BANKCON_Tacc_SHIFT);
302}
303
304
305
306
307
308
309
310void s3c2410_iotiming_debugfs(struct seq_file *seq,
311 struct s3c_cpufreq_config *cfg,
312 union s3c_iobank *iob)
313{
314 struct s3c2410_iobank_timing *bt = iob->io_2410;
315 unsigned long bankcon = bt->bankcon;
316 unsigned long hclk = cfg->freq.hclk_tns;
317 unsigned int tacs;
318 unsigned int tcos;
319 unsigned int tacc;
320 unsigned int tcoh;
321 unsigned int tcah;
322
323 seq_printf(seq, "BANKCON=0x%08lx\n", bankcon);
324
325 tcah = get_0124(hclk, bankcon >> S3C2410_BANKCON_Tcah_SHIFT);
326 tcoh = get_0124(hclk, bankcon >> S3C2410_BANKCON_Tcoh_SHIFT);
327 tcos = get_0124(hclk, bankcon >> S3C2410_BANKCON_Tcos_SHIFT);
328 tacs = get_0124(hclk, bankcon >> S3C2410_BANKCON_Tacs_SHIFT);
329 tacc = get_tacc(hclk, bankcon >> S3C2410_BANKCON_Tacc_SHIFT);
330
331 seq_printf(seq,
332 "\tRead: Tacs=%d.%d, Tcos=%d.%d, Tacc=%d.%d, Tcoh=%d.%d, Tcah=%d.%d\n",
333 print_ns(bt->tacs),
334 print_ns(bt->tcos),
335 print_ns(bt->tacc),
336 print_ns(bt->tcoh),
337 print_ns(bt->tcah));
338
339 seq_printf(seq,
340 "\t Set: Tacs=%d.%d, Tcos=%d.%d, Tacc=%d.%d, Tcoh=%d.%d, Tcah=%d.%d\n",
341 print_ns(tacs),
342 print_ns(tcos),
343 print_ns(tacc),
344 print_ns(tcoh),
345 print_ns(tcah));
346}
347
348
349
350
351
352
353
354
355
356
357int s3c2410_iotiming_calc(struct s3c_cpufreq_config *cfg,
358 struct s3c_iotimings *iot)
359{
360 struct s3c2410_iobank_timing *bt;
361 unsigned long bankcon;
362 int bank;
363 int ret;
364
365 for (bank = 0; bank < MAX_BANKS; bank++) {
366 bankcon = __raw_readl(bank_reg(bank));
367 bt = iot->bank[bank].io_2410;
368
369 if (!bt)
370 continue;
371
372 bt->bankcon = bankcon;
373
374 ret = s3c2410_calc_bank(cfg, bt);
375 if (ret) {
376 printk(KERN_ERR "%s: cannot calculate bank %d io\n",
377 __func__, bank);
378 goto err;
379 }
380
381 s3c_freq_iodbg("%s: bank %d: con=%08lx\n",
382 __func__, bank, bt->bankcon);
383 }
384
385 return 0;
386 err:
387 return ret;
388}
389
390
391
392
393
394
395
396
397
398
399void s3c2410_iotiming_set(struct s3c_cpufreq_config *cfg,
400 struct s3c_iotimings *iot)
401{
402 struct s3c2410_iobank_timing *bt;
403 int bank;
404
405
406
407 for (bank = 0; bank < MAX_BANKS; bank++) {
408 bt = iot->bank[bank].io_2410;
409 if (!bt)
410 continue;
411
412 __raw_writel(bt->bankcon, bank_reg(bank));
413 }
414}
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431int s3c2410_iotiming_get(struct s3c_cpufreq_config *cfg,
432 struct s3c_iotimings *timings)
433{
434 struct s3c2410_iobank_timing *bt;
435 unsigned long bankcon;
436 unsigned long bwscon;
437 int bank;
438
439 bwscon = __raw_readl(S3C2410_BWSCON);
440
441
442
443 for (bank = 0; bank < MAX_BANKS; bank++) {
444 bankcon = __raw_readl(bank_reg(bank));
445
446 if (!bank_is_io(bankcon))
447 continue;
448
449 s3c_freq_iodbg("%s: bank %d: con %08lx\n",
450 __func__, bank, bankcon);
451
452 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
453 if (!bt)
454 return -ENOMEM;
455
456
457
458 if (bank != 0) {
459 unsigned long tmp = S3C2410_BWSCON_GET(bwscon, bank);
460 if (tmp & S3C2410_BWSCON_WS)
461 bt->nwait_en = 1;
462 }
463
464 timings->bank[bank].io_2410 = bt;
465 bt->bankcon = bankcon;
466
467 s3c2410_iotiming_getbank(cfg, bt);
468 }
469
470 s3c2410_print_timing("get", timings);
471 return 0;
472}
473