blob: f630adfaf5b2634138234b2014171f30aae47818 [file] [log] [blame]
Ben Dooksaf337f32010-04-28 18:03:57 +09001/* linux/arch/arm/plat-s3c24xx/s3c2443-clock.c
2 *
3 * Copyright (c) 2007, 2010 Simtec Electronics
4 * Ben Dooks <ben@simtec.co.uk>
5 *
6 * S3C2443 Clock control suport - common code
7 */
8
9#include <linux/init.h>
10#include <linux/clk.h>
11#include <linux/io.h>
12
13#include <mach/regs-s3c2443-clock.h>
14
15#include <plat/s3c2443.h>
16#include <plat/clock.h>
17#include <plat/clock-clksrc.h>
18#include <plat/cpu.h>
19
20#include <plat/cpu-freq.h>
21
22
23static int s3c2443_gate(void __iomem *reg, struct clk *clk, int enable)
24{
25 u32 ctrlbit = clk->ctrlbit;
26 u32 con = __raw_readl(reg);
27
28 if (enable)
29 con |= ctrlbit;
30 else
31 con &= ~ctrlbit;
32
33 __raw_writel(con, reg);
34 return 0;
35}
36
37int s3c2443_clkcon_enable_h(struct clk *clk, int enable)
38{
39 return s3c2443_gate(S3C2443_HCLKCON, clk, enable);
40}
41
42int s3c2443_clkcon_enable_p(struct clk *clk, int enable)
43{
44 return s3c2443_gate(S3C2443_PCLKCON, clk, enable);
45}
46
47int s3c2443_clkcon_enable_s(struct clk *clk, int enable)
48{
49 return s3c2443_gate(S3C2443_SCLKCON, clk, enable);
50}
51
52/* mpllref is a direct descendant of clk_xtal by default, but it is not
53 * elided as the EPLL can be either sourced by the XTAL or EXTCLK and as
54 * such directly equating the two source clocks is impossible.
55 */
56struct clk clk_mpllref = {
57 .name = "mpllref",
58 .parent = &clk_xtal,
Ben Dooksaf337f32010-04-28 18:03:57 +090059};
60
61static struct clk *clk_epllref_sources[] = {
62 [0] = &clk_mpllref,
63 [1] = &clk_mpllref,
64 [2] = &clk_xtal,
65 [3] = &clk_ext,
66};
67
68struct clksrc_clk clk_epllref = {
69 .clk = {
70 .name = "epllref",
Ben Dooksaf337f32010-04-28 18:03:57 +090071 },
72 .sources = &(struct clksrc_sources) {
73 .sources = clk_epllref_sources,
74 .nr_sources = ARRAY_SIZE(clk_epllref_sources),
75 },
76 .reg_src = { .reg = S3C2443_CLKSRC, .size = 2, .shift = 7 },
77};
78
79/* esysclk
80 *
81 * this is sourced from either the EPLL or the EPLLref clock
82*/
83
84static struct clk *clk_sysclk_sources[] = {
85 [0] = &clk_epllref.clk,
86 [1] = &clk_epll,
87};
88
89struct clksrc_clk clk_esysclk = {
90 .clk = {
91 .name = "esysclk",
92 .parent = &clk_epll,
Ben Dooksaf337f32010-04-28 18:03:57 +090093 },
94 .sources = &(struct clksrc_sources) {
95 .sources = clk_sysclk_sources,
96 .nr_sources = ARRAY_SIZE(clk_sysclk_sources),
97 },
98 .reg_src = { .reg = S3C2443_CLKSRC, .size = 1, .shift = 6 },
99};
100
101static unsigned long s3c2443_getrate_mdivclk(struct clk *clk)
102{
103 unsigned long parent_rate = clk_get_rate(clk->parent);
104 unsigned long div = __raw_readl(S3C2443_CLKDIV0);
105
106 div &= S3C2443_CLKDIV0_EXTDIV_MASK;
107 div >>= (S3C2443_CLKDIV0_EXTDIV_SHIFT-1); /* x2 */
108
109 return parent_rate / (div + 1);
110}
111
112static struct clk clk_mdivclk = {
113 .name = "mdivclk",
114 .parent = &clk_mpllref,
Ben Dooksaf337f32010-04-28 18:03:57 +0900115 .ops = &(struct clk_ops) {
116 .get_rate = s3c2443_getrate_mdivclk,
117 },
118};
119
120static struct clk *clk_msysclk_sources[] = {
121 [0] = &clk_mpllref,
122 [1] = &clk_mpll,
123 [2] = &clk_mdivclk,
124 [3] = &clk_mpllref,
125};
126
127struct clksrc_clk clk_msysclk = {
128 .clk = {
129 .name = "msysclk",
130 .parent = &clk_xtal,
Ben Dooksaf337f32010-04-28 18:03:57 +0900131 },
132 .sources = &(struct clksrc_sources) {
133 .sources = clk_msysclk_sources,
134 .nr_sources = ARRAY_SIZE(clk_msysclk_sources),
135 },
136 .reg_src = { .reg = S3C2443_CLKSRC, .size = 2, .shift = 3 },
137};
138
139/* prediv
140 *
141 * this divides the msysclk down to pass to h/p/etc.
142 */
143
144static unsigned long s3c2443_prediv_getrate(struct clk *clk)
145{
146 unsigned long rate = clk_get_rate(clk->parent);
147 unsigned long clkdiv0 = __raw_readl(S3C2443_CLKDIV0);
148
149 clkdiv0 &= S3C2443_CLKDIV0_PREDIV_MASK;
150 clkdiv0 >>= S3C2443_CLKDIV0_PREDIV_SHIFT;
151
152 return rate / (clkdiv0 + 1);
153}
154
155static struct clk clk_prediv = {
156 .name = "prediv",
Ben Dooksaf337f32010-04-28 18:03:57 +0900157 .parent = &clk_msysclk.clk,
158 .ops = &(struct clk_ops) {
159 .get_rate = s3c2443_prediv_getrate,
160 },
161};
162
163/* usbhost
164 *
165 * usb host bus-clock, usually 48MHz to provide USB bus clock timing
166*/
167
168static struct clksrc_clk clk_usb_bus_host = {
169 .clk = {
170 .name = "usb-bus-host-parent",
Ben Dooksaf337f32010-04-28 18:03:57 +0900171 .parent = &clk_esysclk.clk,
172 .ctrlbit = S3C2443_SCLKCON_USBHOST,
173 .enable = s3c2443_clkcon_enable_s,
174 },
175 .reg_div = { .reg = S3C2443_CLKDIV1, .size = 2, .shift = 4 },
176};
177
178/* common clksrc clocks */
179
180static struct clksrc_clk clksrc_clks[] = {
181 {
182 /* ART baud-rate clock sourced from esysclk via a divisor */
183 .clk = {
184 .name = "uartclk",
Ben Dooksaf337f32010-04-28 18:03:57 +0900185 .parent = &clk_esysclk.clk,
186 },
187 .reg_div = { .reg = S3C2443_CLKDIV1, .size = 4, .shift = 8 },
188 }, {
189 /* camera interface bus-clock, divided down from esysclk */
190 .clk = {
191 .name = "camif-upll", /* same as 2440 name */
Ben Dooksaf337f32010-04-28 18:03:57 +0900192 .parent = &clk_esysclk.clk,
193 .ctrlbit = S3C2443_SCLKCON_CAMCLK,
194 .enable = s3c2443_clkcon_enable_s,
195 },
196 .reg_div = { .reg = S3C2443_CLKDIV1, .size = 4, .shift = 26 },
197 }, {
198 .clk = {
199 .name = "display-if",
Ben Dooksaf337f32010-04-28 18:03:57 +0900200 .parent = &clk_esysclk.clk,
201 .ctrlbit = S3C2443_SCLKCON_DISPCLK,
202 .enable = s3c2443_clkcon_enable_s,
203 },
204 .reg_div = { .reg = S3C2443_CLKDIV1, .size = 8, .shift = 16 },
205 },
206};
207
Heiko Stuebnere3b454f2011-09-27 08:44:37 +0900208static struct clk clk_i2s_ext = {
209 .name = "i2s-ext",
210};
211
212/* i2s_eplldiv
213 *
214 * This clock is the output from the I2S divisor of ESYSCLK, and is separate
215 * from the mux that comes after it (cannot merge into one single clock)
216*/
217
218static struct clksrc_clk clk_i2s_eplldiv = {
219 .clk = {
220 .name = "i2s-eplldiv",
221 .parent = &clk_esysclk.clk,
222 },
223 .reg_div = { .reg = S3C2443_CLKDIV1, .size = 4, .shift = 12, },
224};
225
226/* i2s-ref
227 *
228 * i2s bus reference clock, selectable from external, esysclk or epllref
229 *
230 * Note, this used to be two clocks, but was compressed into one.
231*/
232
233static struct clk *clk_i2s_srclist[] = {
234 [0] = &clk_i2s_eplldiv.clk,
235 [1] = &clk_i2s_ext,
236 [2] = &clk_epllref.clk,
237 [3] = &clk_epllref.clk,
238};
239
240static struct clksrc_clk clk_i2s = {
241 .clk = {
242 .name = "i2s-if",
243 .ctrlbit = S3C2443_SCLKCON_I2SCLK,
244 .enable = s3c2443_clkcon_enable_s,
245
246 },
247 .sources = &(struct clksrc_sources) {
248 .sources = clk_i2s_srclist,
249 .nr_sources = ARRAY_SIZE(clk_i2s_srclist),
250 },
251 .reg_src = { .reg = S3C2443_CLKSRC, .size = 2, .shift = 14 },
252};
Ben Dooksaf337f32010-04-28 18:03:57 +0900253
254static struct clk init_clocks_off[] = {
255 {
Heiko Stuebnere3b454f2011-09-27 08:44:37 +0900256 .name = "iis",
257 .parent = &clk_p,
258 .enable = s3c2443_clkcon_enable_p,
259 .ctrlbit = S3C2443_PCLKCON_IIS,
260 }, {
Ben Dooksaf337f32010-04-28 18:03:57 +0900261 .name = "adc",
Ben Dooksaf337f32010-04-28 18:03:57 +0900262 .parent = &clk_p,
263 .enable = s3c2443_clkcon_enable_p,
264 .ctrlbit = S3C2443_PCLKCON_ADC,
265 }, {
266 .name = "i2c",
Ben Dooksaf337f32010-04-28 18:03:57 +0900267 .parent = &clk_p,
268 .enable = s3c2443_clkcon_enable_p,
269 .ctrlbit = S3C2443_PCLKCON_IIC,
270 }
271};
272
273static struct clk init_clocks[] = {
274 {
275 .name = "dma",
Ben Dooksaf337f32010-04-28 18:03:57 +0900276 .parent = &clk_h,
277 .enable = s3c2443_clkcon_enable_h,
278 .ctrlbit = S3C2443_HCLKCON_DMA0,
279 }, {
280 .name = "dma",
Ben Dooksaf337f32010-04-28 18:03:57 +0900281 .parent = &clk_h,
282 .enable = s3c2443_clkcon_enable_h,
283 .ctrlbit = S3C2443_HCLKCON_DMA1,
284 }, {
285 .name = "dma",
Ben Dooksaf337f32010-04-28 18:03:57 +0900286 .parent = &clk_h,
287 .enable = s3c2443_clkcon_enable_h,
288 .ctrlbit = S3C2443_HCLKCON_DMA2,
289 }, {
290 .name = "dma",
Ben Dooksaf337f32010-04-28 18:03:57 +0900291 .parent = &clk_h,
292 .enable = s3c2443_clkcon_enable_h,
293 .ctrlbit = S3C2443_HCLKCON_DMA3,
294 }, {
295 .name = "dma",
Ben Dooksaf337f32010-04-28 18:03:57 +0900296 .parent = &clk_h,
297 .enable = s3c2443_clkcon_enable_h,
298 .ctrlbit = S3C2443_HCLKCON_DMA4,
299 }, {
300 .name = "dma",
Ben Dooksaf337f32010-04-28 18:03:57 +0900301 .parent = &clk_h,
302 .enable = s3c2443_clkcon_enable_h,
303 .ctrlbit = S3C2443_HCLKCON_DMA5,
304 }, {
305 .name = "hsmmc",
Ben Dooksaf337f32010-04-28 18:03:57 +0900306 .parent = &clk_h,
307 .enable = s3c2443_clkcon_enable_h,
308 .ctrlbit = S3C2443_HCLKCON_HSMMC,
309 }, {
310 .name = "gpio",
Ben Dooksaf337f32010-04-28 18:03:57 +0900311 .parent = &clk_p,
312 .enable = s3c2443_clkcon_enable_p,
313 .ctrlbit = S3C2443_PCLKCON_GPIO,
314 }, {
315 .name = "usb-host",
Ben Dooksaf337f32010-04-28 18:03:57 +0900316 .parent = &clk_h,
317 .enable = s3c2443_clkcon_enable_h,
318 .ctrlbit = S3C2443_HCLKCON_USBH,
319 }, {
320 .name = "usb-device",
Ben Dooksaf337f32010-04-28 18:03:57 +0900321 .parent = &clk_h,
322 .enable = s3c2443_clkcon_enable_h,
323 .ctrlbit = S3C2443_HCLKCON_USBD,
324 }, {
325 .name = "lcd",
Ben Dooksaf337f32010-04-28 18:03:57 +0900326 .parent = &clk_h,
327 .enable = s3c2443_clkcon_enable_h,
328 .ctrlbit = S3C2443_HCLKCON_LCDC,
329
330 }, {
331 .name = "timers",
Ben Dooksaf337f32010-04-28 18:03:57 +0900332 .parent = &clk_p,
333 .enable = s3c2443_clkcon_enable_p,
334 .ctrlbit = S3C2443_PCLKCON_PWMT,
335 }, {
336 .name = "cfc",
Ben Dooksaf337f32010-04-28 18:03:57 +0900337 .parent = &clk_h,
338 .enable = s3c2443_clkcon_enable_h,
339 .ctrlbit = S3C2443_HCLKCON_CFC,
340 }, {
341 .name = "ssmc",
Ben Dooksaf337f32010-04-28 18:03:57 +0900342 .parent = &clk_h,
343 .enable = s3c2443_clkcon_enable_h,
344 .ctrlbit = S3C2443_HCLKCON_SSMC,
345 }, {
346 .name = "uart",
Thomas Abrahame83626f2011-06-14 19:12:26 +0900347 .devname = "s3c2440-uart.0",
Ben Dooksaf337f32010-04-28 18:03:57 +0900348 .parent = &clk_p,
349 .enable = s3c2443_clkcon_enable_p,
350 .ctrlbit = S3C2443_PCLKCON_UART0,
351 }, {
352 .name = "uart",
Thomas Abrahame83626f2011-06-14 19:12:26 +0900353 .devname = "s3c2440-uart.1",
Ben Dooksaf337f32010-04-28 18:03:57 +0900354 .parent = &clk_p,
355 .enable = s3c2443_clkcon_enable_p,
356 .ctrlbit = S3C2443_PCLKCON_UART1,
357 }, {
358 .name = "uart",
Thomas Abrahame83626f2011-06-14 19:12:26 +0900359 .devname = "s3c2440-uart.2",
Ben Dooksaf337f32010-04-28 18:03:57 +0900360 .parent = &clk_p,
361 .enable = s3c2443_clkcon_enable_p,
362 .ctrlbit = S3C2443_PCLKCON_UART2,
363 }, {
364 .name = "uart",
Thomas Abrahame83626f2011-06-14 19:12:26 +0900365 .devname = "s3c2440-uart.3",
Ben Dooksaf337f32010-04-28 18:03:57 +0900366 .parent = &clk_p,
367 .enable = s3c2443_clkcon_enable_p,
368 .ctrlbit = S3C2443_PCLKCON_UART3,
369 }, {
370 .name = "rtc",
Ben Dooksaf337f32010-04-28 18:03:57 +0900371 .parent = &clk_p,
372 .enable = s3c2443_clkcon_enable_p,
373 .ctrlbit = S3C2443_PCLKCON_RTC,
374 }, {
375 .name = "watchdog",
Ben Dooksaf337f32010-04-28 18:03:57 +0900376 .parent = &clk_p,
377 .ctrlbit = S3C2443_PCLKCON_WDT,
378 }, {
379 .name = "ac97",
Ben Dooksaf337f32010-04-28 18:03:57 +0900380 .parent = &clk_p,
381 .ctrlbit = S3C2443_PCLKCON_AC97,
382 }, {
383 .name = "nand",
Ben Dooksaf337f32010-04-28 18:03:57 +0900384 .parent = &clk_h,
385 }, {
386 .name = "usb-bus-host",
Ben Dooksaf337f32010-04-28 18:03:57 +0900387 .parent = &clk_usb_bus_host.clk,
388 }
389};
390
391static inline unsigned long s3c2443_get_hdiv(unsigned long clkcon0)
392{
393 clkcon0 &= S3C2443_CLKDIV0_HCLKDIV_MASK;
394
395 return clkcon0 + 1;
396}
397
398/* EPLLCON compatible enough to get on/off information */
399
400void __init_or_cpufreq s3c2443_common_setup_clocks(pll_fn get_mpll,
401 fdiv_fn get_fdiv)
402{
403 unsigned long epllcon = __raw_readl(S3C2443_EPLLCON);
404 unsigned long mpllcon = __raw_readl(S3C2443_MPLLCON);
405 unsigned long clkdiv0 = __raw_readl(S3C2443_CLKDIV0);
406 struct clk *xtal_clk;
407 unsigned long xtal;
408 unsigned long pll;
409 unsigned long fclk;
410 unsigned long hclk;
411 unsigned long pclk;
412 int ptr;
413
414 xtal_clk = clk_get(NULL, "xtal");
415 xtal = clk_get_rate(xtal_clk);
416 clk_put(xtal_clk);
417
418 pll = get_mpll(mpllcon, xtal);
419 clk_msysclk.clk.rate = pll;
420
421 fclk = pll / get_fdiv(clkdiv0);
422 hclk = s3c2443_prediv_getrate(&clk_prediv);
423 hclk /= s3c2443_get_hdiv(clkdiv0);
424 pclk = hclk / ((clkdiv0 & S3C2443_CLKDIV0_HALF_PCLK) ? 2 : 1);
425
426 s3c24xx_setup_clocks(fclk, hclk, pclk);
427
428 printk("CPU: MPLL %s %ld.%03ld MHz, cpu %ld.%03ld MHz, mem %ld.%03ld MHz, pclk %ld.%03ld MHz\n",
429 (mpllcon & S3C2443_PLLCON_OFF) ? "off":"on",
430 print_mhz(pll), print_mhz(fclk),
431 print_mhz(hclk), print_mhz(pclk));
432
433 for (ptr = 0; ptr < ARRAY_SIZE(clksrc_clks); ptr++)
434 s3c_set_clksrc(&clksrc_clks[ptr], true);
435
436 /* ensure usb bus clock is within correct rate of 48MHz */
437
438 if (clk_get_rate(&clk_usb_bus_host.clk) != (48 * 1000 * 1000)) {
439 printk(KERN_INFO "Warning: USB host bus not at 48MHz\n");
440 clk_set_rate(&clk_usb_bus_host.clk, 48*1000*1000);
441 }
442
443 printk("CPU: EPLL %s %ld.%03ld MHz, usb-bus %ld.%03ld MHz\n",
444 (epllcon & S3C2443_PLLCON_OFF) ? "off":"on",
445 print_mhz(clk_get_rate(&clk_epll)),
446 print_mhz(clk_get_rate(&clk_usb_bus)));
447}
448
449static struct clk *clks[] __initdata = {
450 &clk_prediv,
451 &clk_mpllref,
452 &clk_mdivclk,
453 &clk_ext,
454 &clk_epll,
455 &clk_usb_bus,
456};
457
458static struct clksrc_clk *clksrcs[] __initdata = {
Heiko Stuebnere3b454f2011-09-27 08:44:37 +0900459 &clk_i2s_eplldiv,
460 &clk_i2s,
Ben Dooksaf337f32010-04-28 18:03:57 +0900461 &clk_usb_bus_host,
462 &clk_epllref,
463 &clk_esysclk,
464 &clk_msysclk,
465};
466
467void __init s3c2443_common_init_clocks(int xtal, pll_fn get_mpll,
468 fdiv_fn get_fdiv)
469{
470 int ptr;
471
472 /* s3c2443 parents h and p clocks from prediv */
473 clk_h.parent = &clk_prediv;
474 clk_p.parent = &clk_prediv;
475
476 clk_usb_bus.parent = &clk_usb_bus_host.clk;
477 clk_epll.parent = &clk_epllref.clk;
478
479 s3c24xx_register_baseclocks(xtal);
480 s3c24xx_register_clocks(clks, ARRAY_SIZE(clks));
481
482 for (ptr = 0; ptr < ARRAY_SIZE(clksrcs); ptr++)
483 s3c_register_clksrc(clksrcs[ptr], 1);
484
485 s3c_register_clksrc(clksrc_clks, ARRAY_SIZE(clksrc_clks));
486 s3c_register_clocks(init_clocks, ARRAY_SIZE(init_clocks));
487
488 /* See s3c2443/etc notes on disabling clocks at init time */
489 s3c_register_clocks(init_clocks_off, ARRAY_SIZE(init_clocks_off));
490 s3c_disable_clocks(init_clocks_off, ARRAY_SIZE(init_clocks_off));
491
492 s3c2443_common_setup_clocks(get_mpll, get_fdiv);
493}