blob: 3f2117b8c0d4d0c44234ed8470134e6d672b61fd [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
Heiko Stuebnerd9a3bfb2011-10-14 15:08:56 +0900163static unsigned int *armdiv;
164static int nr_armdiv;
165static int armdivmask;
166
Ben Dooksaf337f32010-04-28 18:03:57 +0900167/* usbhost
168 *
169 * usb host bus-clock, usually 48MHz to provide USB bus clock timing
170*/
171
172static struct clksrc_clk clk_usb_bus_host = {
173 .clk = {
174 .name = "usb-bus-host-parent",
Ben Dooksaf337f32010-04-28 18:03:57 +0900175 .parent = &clk_esysclk.clk,
176 .ctrlbit = S3C2443_SCLKCON_USBHOST,
177 .enable = s3c2443_clkcon_enable_s,
178 },
179 .reg_div = { .reg = S3C2443_CLKDIV1, .size = 2, .shift = 4 },
180};
181
182/* common clksrc clocks */
183
184static struct clksrc_clk clksrc_clks[] = {
185 {
186 /* ART baud-rate clock sourced from esysclk via a divisor */
187 .clk = {
188 .name = "uartclk",
Ben Dooksaf337f32010-04-28 18:03:57 +0900189 .parent = &clk_esysclk.clk,
190 },
191 .reg_div = { .reg = S3C2443_CLKDIV1, .size = 4, .shift = 8 },
192 }, {
193 /* camera interface bus-clock, divided down from esysclk */
194 .clk = {
195 .name = "camif-upll", /* same as 2440 name */
Ben Dooksaf337f32010-04-28 18:03:57 +0900196 .parent = &clk_esysclk.clk,
197 .ctrlbit = S3C2443_SCLKCON_CAMCLK,
198 .enable = s3c2443_clkcon_enable_s,
199 },
200 .reg_div = { .reg = S3C2443_CLKDIV1, .size = 4, .shift = 26 },
201 }, {
202 .clk = {
203 .name = "display-if",
Ben Dooksaf337f32010-04-28 18:03:57 +0900204 .parent = &clk_esysclk.clk,
205 .ctrlbit = S3C2443_SCLKCON_DISPCLK,
206 .enable = s3c2443_clkcon_enable_s,
207 },
208 .reg_div = { .reg = S3C2443_CLKDIV1, .size = 8, .shift = 16 },
209 },
210};
211
Heiko Stuebnere3b454f2011-09-27 08:44:37 +0900212static struct clk clk_i2s_ext = {
213 .name = "i2s-ext",
214};
215
216/* i2s_eplldiv
217 *
218 * This clock is the output from the I2S divisor of ESYSCLK, and is separate
219 * from the mux that comes after it (cannot merge into one single clock)
220*/
221
222static struct clksrc_clk clk_i2s_eplldiv = {
223 .clk = {
224 .name = "i2s-eplldiv",
225 .parent = &clk_esysclk.clk,
226 },
227 .reg_div = { .reg = S3C2443_CLKDIV1, .size = 4, .shift = 12, },
228};
229
230/* i2s-ref
231 *
232 * i2s bus reference clock, selectable from external, esysclk or epllref
233 *
234 * Note, this used to be two clocks, but was compressed into one.
235*/
236
237static struct clk *clk_i2s_srclist[] = {
238 [0] = &clk_i2s_eplldiv.clk,
239 [1] = &clk_i2s_ext,
240 [2] = &clk_epllref.clk,
241 [3] = &clk_epllref.clk,
242};
243
244static struct clksrc_clk clk_i2s = {
245 .clk = {
246 .name = "i2s-if",
247 .ctrlbit = S3C2443_SCLKCON_I2SCLK,
248 .enable = s3c2443_clkcon_enable_s,
249
250 },
251 .sources = &(struct clksrc_sources) {
252 .sources = clk_i2s_srclist,
253 .nr_sources = ARRAY_SIZE(clk_i2s_srclist),
254 },
255 .reg_src = { .reg = S3C2443_CLKSRC, .size = 2, .shift = 14 },
256};
Ben Dooksaf337f32010-04-28 18:03:57 +0900257
258static struct clk init_clocks_off[] = {
259 {
Heiko Stuebnere3b454f2011-09-27 08:44:37 +0900260 .name = "iis",
261 .parent = &clk_p,
262 .enable = s3c2443_clkcon_enable_p,
263 .ctrlbit = S3C2443_PCLKCON_IIS,
264 }, {
Heiko Stuebner8b069b72011-09-27 08:45:23 +0900265 .name = "hsspi",
266 .parent = &clk_p,
267 .enable = s3c2443_clkcon_enable_p,
268 .ctrlbit = S3C2443_PCLKCON_HSSPI,
269 }, {
Ben Dooksaf337f32010-04-28 18:03:57 +0900270 .name = "adc",
Ben Dooksaf337f32010-04-28 18:03:57 +0900271 .parent = &clk_p,
272 .enable = s3c2443_clkcon_enable_p,
273 .ctrlbit = S3C2443_PCLKCON_ADC,
274 }, {
275 .name = "i2c",
Ben Dooksaf337f32010-04-28 18:03:57 +0900276 .parent = &clk_p,
277 .enable = s3c2443_clkcon_enable_p,
278 .ctrlbit = S3C2443_PCLKCON_IIC,
279 }
280};
281
282static struct clk init_clocks[] = {
283 {
284 .name = "dma",
Ben Dooksaf337f32010-04-28 18:03:57 +0900285 .parent = &clk_h,
286 .enable = s3c2443_clkcon_enable_h,
287 .ctrlbit = S3C2443_HCLKCON_DMA0,
288 }, {
289 .name = "dma",
Ben Dooksaf337f32010-04-28 18:03:57 +0900290 .parent = &clk_h,
291 .enable = s3c2443_clkcon_enable_h,
292 .ctrlbit = S3C2443_HCLKCON_DMA1,
293 }, {
294 .name = "dma",
Ben Dooksaf337f32010-04-28 18:03:57 +0900295 .parent = &clk_h,
296 .enable = s3c2443_clkcon_enable_h,
297 .ctrlbit = S3C2443_HCLKCON_DMA2,
298 }, {
299 .name = "dma",
Ben Dooksaf337f32010-04-28 18:03:57 +0900300 .parent = &clk_h,
301 .enable = s3c2443_clkcon_enable_h,
302 .ctrlbit = S3C2443_HCLKCON_DMA3,
303 }, {
304 .name = "dma",
Ben Dooksaf337f32010-04-28 18:03:57 +0900305 .parent = &clk_h,
306 .enable = s3c2443_clkcon_enable_h,
307 .ctrlbit = S3C2443_HCLKCON_DMA4,
308 }, {
309 .name = "dma",
Ben Dooksaf337f32010-04-28 18:03:57 +0900310 .parent = &clk_h,
311 .enable = s3c2443_clkcon_enable_h,
312 .ctrlbit = S3C2443_HCLKCON_DMA5,
313 }, {
314 .name = "hsmmc",
Ben Dooksaf337f32010-04-28 18:03:57 +0900315 .parent = &clk_h,
316 .enable = s3c2443_clkcon_enable_h,
317 .ctrlbit = S3C2443_HCLKCON_HSMMC,
318 }, {
319 .name = "gpio",
Ben Dooksaf337f32010-04-28 18:03:57 +0900320 .parent = &clk_p,
321 .enable = s3c2443_clkcon_enable_p,
322 .ctrlbit = S3C2443_PCLKCON_GPIO,
323 }, {
324 .name = "usb-host",
Ben Dooksaf337f32010-04-28 18:03:57 +0900325 .parent = &clk_h,
326 .enable = s3c2443_clkcon_enable_h,
327 .ctrlbit = S3C2443_HCLKCON_USBH,
328 }, {
329 .name = "usb-device",
Ben Dooksaf337f32010-04-28 18:03:57 +0900330 .parent = &clk_h,
331 .enable = s3c2443_clkcon_enable_h,
332 .ctrlbit = S3C2443_HCLKCON_USBD,
333 }, {
334 .name = "lcd",
Ben Dooksaf337f32010-04-28 18:03:57 +0900335 .parent = &clk_h,
336 .enable = s3c2443_clkcon_enable_h,
337 .ctrlbit = S3C2443_HCLKCON_LCDC,
338
339 }, {
340 .name = "timers",
Ben Dooksaf337f32010-04-28 18:03:57 +0900341 .parent = &clk_p,
342 .enable = s3c2443_clkcon_enable_p,
343 .ctrlbit = S3C2443_PCLKCON_PWMT,
344 }, {
345 .name = "cfc",
Ben Dooksaf337f32010-04-28 18:03:57 +0900346 .parent = &clk_h,
347 .enable = s3c2443_clkcon_enable_h,
348 .ctrlbit = S3C2443_HCLKCON_CFC,
349 }, {
350 .name = "ssmc",
Ben Dooksaf337f32010-04-28 18:03:57 +0900351 .parent = &clk_h,
352 .enable = s3c2443_clkcon_enable_h,
353 .ctrlbit = S3C2443_HCLKCON_SSMC,
354 }, {
355 .name = "uart",
Thomas Abrahame83626f2011-06-14 19:12:26 +0900356 .devname = "s3c2440-uart.0",
Ben Dooksaf337f32010-04-28 18:03:57 +0900357 .parent = &clk_p,
358 .enable = s3c2443_clkcon_enable_p,
359 .ctrlbit = S3C2443_PCLKCON_UART0,
360 }, {
361 .name = "uart",
Thomas Abrahame83626f2011-06-14 19:12:26 +0900362 .devname = "s3c2440-uart.1",
Ben Dooksaf337f32010-04-28 18:03:57 +0900363 .parent = &clk_p,
364 .enable = s3c2443_clkcon_enable_p,
365 .ctrlbit = S3C2443_PCLKCON_UART1,
366 }, {
367 .name = "uart",
Thomas Abrahame83626f2011-06-14 19:12:26 +0900368 .devname = "s3c2440-uart.2",
Ben Dooksaf337f32010-04-28 18:03:57 +0900369 .parent = &clk_p,
370 .enable = s3c2443_clkcon_enable_p,
371 .ctrlbit = S3C2443_PCLKCON_UART2,
372 }, {
373 .name = "uart",
Thomas Abrahame83626f2011-06-14 19:12:26 +0900374 .devname = "s3c2440-uart.3",
Ben Dooksaf337f32010-04-28 18:03:57 +0900375 .parent = &clk_p,
376 .enable = s3c2443_clkcon_enable_p,
377 .ctrlbit = S3C2443_PCLKCON_UART3,
378 }, {
379 .name = "rtc",
Ben Dooksaf337f32010-04-28 18:03:57 +0900380 .parent = &clk_p,
381 .enable = s3c2443_clkcon_enable_p,
382 .ctrlbit = S3C2443_PCLKCON_RTC,
383 }, {
384 .name = "watchdog",
Ben Dooksaf337f32010-04-28 18:03:57 +0900385 .parent = &clk_p,
386 .ctrlbit = S3C2443_PCLKCON_WDT,
387 }, {
388 .name = "ac97",
Ben Dooksaf337f32010-04-28 18:03:57 +0900389 .parent = &clk_p,
390 .ctrlbit = S3C2443_PCLKCON_AC97,
391 }, {
392 .name = "nand",
Ben Dooksaf337f32010-04-28 18:03:57 +0900393 .parent = &clk_h,
394 }, {
395 .name = "usb-bus-host",
Ben Dooksaf337f32010-04-28 18:03:57 +0900396 .parent = &clk_usb_bus_host.clk,
397 }
398};
399
400static inline unsigned long s3c2443_get_hdiv(unsigned long clkcon0)
401{
402 clkcon0 &= S3C2443_CLKDIV0_HCLKDIV_MASK;
403
404 return clkcon0 + 1;
405}
406
407/* EPLLCON compatible enough to get on/off information */
408
409void __init_or_cpufreq s3c2443_common_setup_clocks(pll_fn get_mpll,
410 fdiv_fn get_fdiv)
411{
412 unsigned long epllcon = __raw_readl(S3C2443_EPLLCON);
413 unsigned long mpllcon = __raw_readl(S3C2443_MPLLCON);
414 unsigned long clkdiv0 = __raw_readl(S3C2443_CLKDIV0);
415 struct clk *xtal_clk;
416 unsigned long xtal;
417 unsigned long pll;
418 unsigned long fclk;
419 unsigned long hclk;
420 unsigned long pclk;
421 int ptr;
422
423 xtal_clk = clk_get(NULL, "xtal");
424 xtal = clk_get_rate(xtal_clk);
425 clk_put(xtal_clk);
426
427 pll = get_mpll(mpllcon, xtal);
428 clk_msysclk.clk.rate = pll;
429
430 fclk = pll / get_fdiv(clkdiv0);
431 hclk = s3c2443_prediv_getrate(&clk_prediv);
432 hclk /= s3c2443_get_hdiv(clkdiv0);
433 pclk = hclk / ((clkdiv0 & S3C2443_CLKDIV0_HALF_PCLK) ? 2 : 1);
434
435 s3c24xx_setup_clocks(fclk, hclk, pclk);
436
437 printk("CPU: MPLL %s %ld.%03ld MHz, cpu %ld.%03ld MHz, mem %ld.%03ld MHz, pclk %ld.%03ld MHz\n",
438 (mpllcon & S3C2443_PLLCON_OFF) ? "off":"on",
439 print_mhz(pll), print_mhz(fclk),
440 print_mhz(hclk), print_mhz(pclk));
441
442 for (ptr = 0; ptr < ARRAY_SIZE(clksrc_clks); ptr++)
443 s3c_set_clksrc(&clksrc_clks[ptr], true);
444
445 /* ensure usb bus clock is within correct rate of 48MHz */
446
447 if (clk_get_rate(&clk_usb_bus_host.clk) != (48 * 1000 * 1000)) {
448 printk(KERN_INFO "Warning: USB host bus not at 48MHz\n");
449 clk_set_rate(&clk_usb_bus_host.clk, 48*1000*1000);
450 }
451
452 printk("CPU: EPLL %s %ld.%03ld MHz, usb-bus %ld.%03ld MHz\n",
453 (epllcon & S3C2443_PLLCON_OFF) ? "off":"on",
454 print_mhz(clk_get_rate(&clk_epll)),
455 print_mhz(clk_get_rate(&clk_usb_bus)));
456}
457
458static struct clk *clks[] __initdata = {
459 &clk_prediv,
460 &clk_mpllref,
461 &clk_mdivclk,
462 &clk_ext,
463 &clk_epll,
464 &clk_usb_bus,
465};
466
467static struct clksrc_clk *clksrcs[] __initdata = {
Heiko Stuebnere3b454f2011-09-27 08:44:37 +0900468 &clk_i2s_eplldiv,
469 &clk_i2s,
Ben Dooksaf337f32010-04-28 18:03:57 +0900470 &clk_usb_bus_host,
471 &clk_epllref,
472 &clk_esysclk,
473 &clk_msysclk,
474};
475
476void __init s3c2443_common_init_clocks(int xtal, pll_fn get_mpll,
Heiko Stuebnerd9a3bfb2011-10-14 15:08:56 +0900477 fdiv_fn get_fdiv,
478 unsigned int *divs, int nr_divs,
479 int divmask)
Ben Dooksaf337f32010-04-28 18:03:57 +0900480{
481 int ptr;
482
Heiko Stuebnerd9a3bfb2011-10-14 15:08:56 +0900483 armdiv = divs;
484 nr_armdiv = nr_divs;
485 armdivmask = divmask;
486
Ben Dooksaf337f32010-04-28 18:03:57 +0900487 /* s3c2443 parents h and p clocks from prediv */
488 clk_h.parent = &clk_prediv;
489 clk_p.parent = &clk_prediv;
490
491 clk_usb_bus.parent = &clk_usb_bus_host.clk;
492 clk_epll.parent = &clk_epllref.clk;
493
494 s3c24xx_register_baseclocks(xtal);
495 s3c24xx_register_clocks(clks, ARRAY_SIZE(clks));
496
497 for (ptr = 0; ptr < ARRAY_SIZE(clksrcs); ptr++)
498 s3c_register_clksrc(clksrcs[ptr], 1);
499
500 s3c_register_clksrc(clksrc_clks, ARRAY_SIZE(clksrc_clks));
501 s3c_register_clocks(init_clocks, ARRAY_SIZE(init_clocks));
502
503 /* See s3c2443/etc notes on disabling clocks at init time */
504 s3c_register_clocks(init_clocks_off, ARRAY_SIZE(init_clocks_off));
505 s3c_disable_clocks(init_clocks_off, ARRAY_SIZE(init_clocks_off));
506
507 s3c2443_common_setup_clocks(get_mpll, get_fdiv);
508}