1 /* linux/arch/arm/plat-s3c24xx/s3c2412-iotiming.c
3 * Copyright (c) 2006-2008 Simtec Electronics
4 * http://armlinux.simtec.co.uk/
5 * Ben Dooks <ben@simtec.co.uk>
7 * S3C2412/S3C2443 (PL093 based) IO timing support
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/init.h>
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/ioport.h>
18 #include <linux/cpufreq.h>
19 #include <linux/seq_file.h>
20 #include <linux/sysdev.h>
21 #include <linux/delay.h>
22 #include <linux/clk.h>
23 #include <linux/err.h>
25 #include <linux/amba/pl093.h>
27 #include <asm/mach/arch.h>
28 #include <asm/mach/map.h>
30 #include <mach/regs-s3c2412-mem.h>
33 #include <plat/cpu-freq-core.h>
34 #include <plat/clock.h>
36 #define print_ns(x) ((x) / 10), ((x) % 10)
39 * s3c2412_print_timing - print timing infromation via printk.
40 * @pfx: The prefix to print each line with.
41 * @iot: The IO timing information
43 static void s3c2412_print_timing(const char *pfx
, struct s3c_iotimings
*iot
)
45 struct s3c2412_iobank_timing
*bt
;
48 for (bank
= 0; bank
< MAX_BANKS
; bank
++) {
49 bt
= iot
->bank
[bank
].io_2412
;
53 printk(KERN_DEBUG
"%s: %d: idcy=%d.%d wstrd=%d.%d wstwr=%d,%d"
54 "wstoen=%d.%d wstwen=%d.%d wstbrd=%d.%d\n", pfx
, bank
,
60 print_ns(bt
->wstbrd
));
65 * to_div - turn a cycle length into a divisor setting.
66 * @cyc_tns: The cycle time in 10ths of nanoseconds.
67 * @clk_tns: The clock period in 10ths of nanoseconds.
69 static inline unsigned int to_div(unsigned int cyc_tns
, unsigned int clk_tns
)
71 return cyc_tns
? DIV_ROUND_UP(cyc_tns
, clk_tns
) : 0;
75 * calc_timing - calculate timing divisor value and check in range.
76 * @hwtm: The hardware timing in 10ths of nanoseconds.
77 * @clk_tns: The clock period in 10ths of nanoseconds.
78 * @err: Pointer to err variable to update in event of failure.
80 static unsigned int calc_timing(unsigned int hwtm
, unsigned int clk_tns
,
83 unsigned int ret
= to_div(hwtm
, clk_tns
);
92 * s3c2412_calc_bank - calculate the bank divisor settings.
93 * @cfg: The current frequency configuration.
94 * @bt: The bank timing.
96 static int s3c2412_calc_bank(struct s3c_cpufreq_config
*cfg
,
97 struct s3c2412_iobank_timing
*bt
)
99 unsigned int hclk
= cfg
->freq
.hclk_tns
;
102 bt
->smbidcyr
= calc_timing(bt
->idcy
, hclk
, &err
);
103 bt
->smbwstrd
= calc_timing(bt
->wstrd
, hclk
, &err
);
104 bt
->smbwstwr
= calc_timing(bt
->wstwr
, hclk
, &err
);
105 bt
->smbwstoen
= calc_timing(bt
->wstoen
, hclk
, &err
);
106 bt
->smbwstwen
= calc_timing(bt
->wstwen
, hclk
, &err
);
107 bt
->smbwstbrd
= calc_timing(bt
->wstbrd
, hclk
, &err
);
113 * s3c2412_iotiming_debugfs - debugfs show io bank timing information
114 * @seq: The seq_file to write output to using seq_printf().
115 * @cfg: The current configuration.
116 * @iob: The IO bank information to decode.
118 void s3c2412_iotiming_debugfs(struct seq_file
*seq
,
119 struct s3c_cpufreq_config
*cfg
,
120 union s3c_iobank
*iob
)
122 struct s3c2412_iobank_timing
*bt
= iob
->io_2412
;
125 "\tRead: idcy=%d.%d wstrd=%d.%d wstwr=%d,%d"
126 "wstoen=%d.%d wstwen=%d.%d wstbrd=%d.%d\n",
130 print_ns(bt
->wstoen
),
131 print_ns(bt
->wstwen
),
132 print_ns(bt
->wstbrd
));
136 * s3c2412_iotiming_calc - calculate all the bank divisor settings.
137 * @cfg: The current frequency configuration.
138 * @iot: The bank timing information.
140 * Calculate the timing information for all the banks that are
141 * configured as IO, using s3c2412_calc_bank().
143 int s3c2412_iotiming_calc(struct s3c_cpufreq_config
*cfg
,
144 struct s3c_iotimings
*iot
)
146 struct s3c2412_iobank_timing
*bt
;
150 for (bank
= 0; bank
< MAX_BANKS
; bank
++) {
151 bt
= iot
->bank
[bank
].io_2412
;
155 ret
= s3c2412_calc_bank(cfg
, bt
);
157 printk(KERN_ERR
"%s: cannot calculate bank %d io\n",
169 * s3c2412_iotiming_set - set the timing information
170 * @cfg: The current frequency configuration.
171 * @iot: The bank timing information.
173 * Set the IO bank information from the details calculated earlier from
174 * calling s3c2412_iotiming_calc().
176 void s3c2412_iotiming_set(struct s3c_cpufreq_config
*cfg
,
177 struct s3c_iotimings
*iot
)
179 struct s3c2412_iobank_timing
*bt
;
183 /* set the io timings from the specifier */
185 for (bank
= 0; bank
< MAX_BANKS
; bank
++) {
186 bt
= iot
->bank
[bank
].io_2412
;
190 regs
= S3C2412_SSMC_BANK(bank
);
192 __raw_writel(bt
->smbidcyr
, regs
+ SMBIDCYR
);
193 __raw_writel(bt
->smbwstrd
, regs
+ SMBWSTRDR
);
194 __raw_writel(bt
->smbwstwr
, regs
+ SMBWSTWRR
);
195 __raw_writel(bt
->smbwstoen
, regs
+ SMBWSTOENR
);
196 __raw_writel(bt
->smbwstwen
, regs
+ SMBWSTWENR
);
197 __raw_writel(bt
->smbwstbrd
, regs
+ SMBWSTBRDR
);
201 static inline unsigned int s3c2412_decode_timing(unsigned int clock
, u32 reg
)
203 return (reg
& 0xf) * clock
;
206 static void s3c2412_iotiming_getbank(struct s3c_cpufreq_config
*cfg
,
207 struct s3c2412_iobank_timing
*bt
,
210 unsigned long clk
= cfg
->freq
.hclk_tns
; /* ssmc clock??? */
211 void __iomem
*regs
= S3C2412_SSMC_BANK(bank
);
213 bt
->idcy
= s3c2412_decode_timing(clk
, __raw_readl(regs
+ SMBIDCYR
));
214 bt
->wstrd
= s3c2412_decode_timing(clk
, __raw_readl(regs
+ SMBWSTRDR
));
215 bt
->wstoen
= s3c2412_decode_timing(clk
, __raw_readl(regs
+ SMBWSTOENR
));
216 bt
->wstwen
= s3c2412_decode_timing(clk
, __raw_readl(regs
+ SMBWSTWENR
));
217 bt
->wstbrd
= s3c2412_decode_timing(clk
, __raw_readl(regs
+ SMBWSTBRDR
));
221 * bank_is_io - return true if bank is (possibly) IO.
222 * @bank: The bank number.
223 * @bankcfg: The value of S3C2412_EBI_BANKCFG.
225 static inline bool bank_is_io(unsigned int bank
, u32 bankcfg
)
230 return !(bankcfg
& (1 << bank
));
233 int s3c2412_iotiming_get(struct s3c_cpufreq_config
*cfg
,
234 struct s3c_iotimings
*timings
)
236 struct s3c2412_iobank_timing
*bt
;
237 u32 bankcfg
= __raw_readl(S3C2412_EBI_BANKCFG
);
240 /* look through all banks to see what is currently set. */
242 for (bank
= 0; bank
< MAX_BANKS
; bank
++) {
243 if (!bank_is_io(bank
, bankcfg
))
246 bt
= kzalloc(sizeof(struct s3c2412_iobank_timing
), GFP_KERNEL
);
248 printk(KERN_ERR
"%s: no memory for bank\n", __func__
);
252 timings
->bank
[bank
].io_2412
= bt
;
253 s3c2412_iotiming_getbank(cfg
, bt
, bank
);
256 s3c2412_print_timing("get", timings
);
260 /* this is in here as it is so small, it doesn't currently warrant a file
261 * to itself. We expect that any s3c24xx needing this is going to also
262 * need the iotiming support.
264 void s3c2412_cpufreq_setrefresh(struct s3c_cpufreq_config
*cfg
)
266 struct s3c_cpufreq_board
*board
= cfg
->board
;
269 WARN_ON(board
== NULL
);
271 /* Reduce both the refresh time (in ns) and the frequency (in MHz)
272 * down to ensure that we do not overflow 32 bit numbers.
274 * This should work for HCLK up to 133MHz and refresh period up
278 refresh
= (cfg
->freq
.hclk
/ 100) * (board
->refresh
/ 10);
279 refresh
= DIV_ROUND_UP(refresh
, (1000 * 1000)); /* apply scale */
280 refresh
&= ((1 << 16) - 1);
282 s3c_freq_dbg("%s: refresh value %u\n", __func__
, (unsigned int)refresh
);
284 __raw_writel(refresh
, S3C2412_REFRESH
);