blob: fe5c4217322ed5813364feb642adc75b5829dd4a [file] [log] [blame]
Sascha Hauer2cb536d2009-02-06 17:48:59 +01001/*
2 * Copyright (C) 2009 by Sascha Hauer, Pengutronix
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16 * MA 02110-1301, USA.
17 */
18
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/list.h>
22#include <linux/clk.h>
23#include <linux/io.h>
24
25#include <asm/clkdev.h>
26
27#include <mach/clock.h>
28#include <mach/hardware.h>
29#include <mach/common.h>
30
31#define CCM_BASE IO_ADDRESS(CCM_BASE_ADDR)
32
33#define CCM_CCMR 0x00
34#define CCM_PDR0 0x04
35#define CCM_PDR1 0x08
36#define CCM_PDR2 0x0C
37#define CCM_PDR3 0x10
38#define CCM_PDR4 0x14
39#define CCM_RCSR 0x18
40#define CCM_MPCTL 0x1C
41#define CCM_PPCTL 0x20
42#define CCM_ACMR 0x24
43#define CCM_COSR 0x28
44#define CCM_CGR0 0x2C
45#define CCM_CGR1 0x30
46#define CCM_CGR2 0x34
47#define CCM_CGR3 0x38
48
49#ifdef HAVE_SET_RATE_SUPPORT
50static void calc_dividers(u32 div, u32 *pre, u32 *post, u32 maxpost)
51{
52 u32 min_pre, temp_pre, old_err, err;
53
54 min_pre = (div - 1) / maxpost + 1;
55 old_err = 8;
56
57 for (temp_pre = 8; temp_pre >= min_pre; temp_pre--) {
58 if (div > (temp_pre * maxpost))
59 break;
60
61 if (div < (temp_pre * temp_pre))
62 continue;
63
64 err = div % temp_pre;
65
66 if (err == 0) {
67 *pre = temp_pre;
68 break;
69 }
70
71 err = temp_pre - err;
72
73 if (err < old_err) {
74 old_err = err;
75 *pre = temp_pre;
76 }
77 }
78
79 *post = (div + *pre - 1) / *pre;
80}
81
82/* get the best values for a 3-bit divider combined with a 6-bit divider */
83static void calc_dividers_3_6(u32 div, u32 *pre, u32 *post)
84{
85 if (div >= 512) {
86 *pre = 8;
87 *post = 64;
88 } else if (div >= 64) {
89 calc_dividers(div, pre, post, 64);
90 } else if (div <= 8) {
91 *pre = div;
92 *post = 1;
93 } else {
94 *pre = 1;
95 *post = div;
96 }
97}
98
99/* get the best values for two cascaded 3-bit dividers */
100static void calc_dividers_3_3(u32 div, u32 *pre, u32 *post)
101{
102 if (div >= 64) {
103 *pre = *post = 8;
104 } else if (div > 8) {
105 calc_dividers(div, pre, post, 8);
106 } else {
107 *pre = 1;
108 *post = div;
109 }
110}
111#endif
112
113static unsigned long get_rate_mpll(void)
114{
115 ulong mpctl = __raw_readl(CCM_BASE + CCM_MPCTL);
116
117 return mxc_decode_pll(mpctl, 24000000);
118}
119
120static unsigned long get_rate_ppll(void)
121{
122 ulong ppctl = __raw_readl(CCM_BASE + CCM_PPCTL);
123
124 return mxc_decode_pll(ppctl, 24000000);
125}
126
127struct arm_ahb_div {
128 unsigned char arm, ahb, sel;
129};
130
131static struct arm_ahb_div clk_consumer[] = {
132 { .arm = 1, .ahb = 4, .sel = 0},
133 { .arm = 1, .ahb = 3, .sel = 1},
134 { .arm = 2, .ahb = 2, .sel = 0},
135 { .arm = 0, .ahb = 0, .sel = 0},
136 { .arm = 0, .ahb = 0, .sel = 0},
137 { .arm = 0, .ahb = 0, .sel = 0},
138 { .arm = 4, .ahb = 1, .sel = 0},
139 { .arm = 1, .ahb = 5, .sel = 0},
140 { .arm = 1, .ahb = 8, .sel = 0},
141 { .arm = 1, .ahb = 6, .sel = 1},
142 { .arm = 2, .ahb = 4, .sel = 0},
143 { .arm = 0, .ahb = 0, .sel = 0},
144 { .arm = 0, .ahb = 0, .sel = 0},
145 { .arm = 0, .ahb = 0, .sel = 0},
146 { .arm = 4, .ahb = 2, .sel = 0},
147 { .arm = 0, .ahb = 0, .sel = 0},
148};
149
Sascha Hauer2cb536d2009-02-06 17:48:59 +0100150static unsigned long get_rate_arm(void)
151{
152 unsigned long pdr0 = __raw_readl(CCM_BASE + CCM_PDR0);
153 struct arm_ahb_div *aad;
154 unsigned long fref = get_rate_mpll();
155
Sascha Hauerd16caf62009-03-23 10:07:48 +0100156 aad = &clk_consumer[(pdr0 >> 16) & 0xf];
157 if (aad->sel)
158 fref = fref * 2 / 3;
159
Sascha Hauer2cb536d2009-02-06 17:48:59 +0100160 return fref / aad->arm;
161}
162
163static unsigned long get_rate_ahb(struct clk *clk)
164{
165 unsigned long pdr0 = __raw_readl(CCM_BASE + CCM_PDR0);
166 struct arm_ahb_div *aad;
167 unsigned long fref = get_rate_mpll();
168
Sascha Hauerd16caf62009-03-23 10:07:48 +0100169 aad = &clk_consumer[(pdr0 >> 16) & 0xf];
Sascha Hauer2cb536d2009-02-06 17:48:59 +0100170
171 return fref / aad->ahb;
172}
173
174static unsigned long get_rate_ipg(struct clk *clk)
175{
176 return get_rate_ahb(NULL) >> 1;
177}
178
179static unsigned long get_3_3_div(unsigned long in)
180{
181 return (((in >> 3) & 0x7) + 1) * ((in & 0x7) + 1);
182}
183
184static unsigned long get_rate_uart(struct clk *clk)
185{
186 unsigned long pdr3 = __raw_readl(CCM_BASE + CCM_PDR3);
187 unsigned long pdr4 = __raw_readl(CCM_BASE + CCM_PDR4);
188 unsigned long div = get_3_3_div(pdr4 >> 10);
189
190 if (pdr3 & (1 << 14))
191 return get_rate_arm() / div;
192 else
193 return get_rate_ppll() / div;
194}
195
196static unsigned long get_rate_sdhc(struct clk *clk)
197{
198 unsigned long pdr3 = __raw_readl(CCM_BASE + CCM_PDR3);
199 unsigned long div, rate;
200
201 if (pdr3 & (1 << 6))
202 rate = get_rate_arm();
203 else
204 rate = get_rate_ppll();
205
206 switch (clk->id) {
207 default:
208 case 0:
209 div = pdr3 & 0x3f;
210 break;
211 case 1:
212 div = (pdr3 >> 8) & 0x3f;
213 break;
214 case 2:
215 div = (pdr3 >> 16) & 0x3f;
216 break;
217 }
218
219 return rate / get_3_3_div(div);
220}
221
222static unsigned long get_rate_mshc(struct clk *clk)
223{
224 unsigned long pdr1 = __raw_readl(CCM_BASE + CCM_PDR1);
225 unsigned long div1, div2, rate;
226
227 if (pdr1 & (1 << 7))
228 rate = get_rate_arm();
229 else
230 rate = get_rate_ppll();
231
232 div1 = (pdr1 >> 29) & 0x7;
233 div2 = (pdr1 >> 22) & 0x3f;
234
235 return rate / ((div1 + 1) * (div2 + 1));
236}
237
238static unsigned long get_rate_ssi(struct clk *clk)
239{
240 unsigned long pdr2 = __raw_readl(CCM_BASE + CCM_PDR2);
241 unsigned long div1, div2, rate;
242
243 if (pdr2 & (1 << 6))
244 rate = get_rate_arm();
245 else
246 rate = get_rate_ppll();
247
248 switch (clk->id) {
249 default:
250 case 0:
251 div1 = pdr2 & 0x3f;
252 div2 = (pdr2 >> 24) & 0x7;
253 break;
254 case 1:
255 div1 = (pdr2 >> 8) & 0x3f;
256 div2 = (pdr2 >> 27) & 0x7;
257 break;
258 }
259
260 return rate / ((div1 + 1) * (div2 + 1));
261}
262
263static unsigned long get_rate_csi(struct clk *clk)
264{
265 unsigned long pdr2 = __raw_readl(CCM_BASE + CCM_PDR2);
266 unsigned long rate;
267
268 if (pdr2 & (1 << 7))
269 rate = get_rate_arm();
270 else
271 rate = get_rate_ppll();
272
273 return rate / get_3_3_div((pdr2 >> 16) & 0x3f);
274}
275
Sascha Hauer547270a2009-03-17 15:43:54 +0100276static unsigned long get_rate_otg(struct clk *clk)
277{
278 unsigned long pdr4 = __raw_readl(CCM_BASE + CCM_PDR4);
279 unsigned long rate;
280
281 if (pdr4 & (1 << 9))
282 rate = get_rate_arm();
283 else
284 rate = get_rate_ppll();
285
286 return rate / get_3_3_div((pdr4 >> 22) & 0x3f);
287}
288
Sascha Hauer2cb536d2009-02-06 17:48:59 +0100289static unsigned long get_rate_ipg_per(struct clk *clk)
290{
291 unsigned long pdr0 = __raw_readl(CCM_BASE + CCM_PDR0);
292 unsigned long pdr4 = __raw_readl(CCM_BASE + CCM_PDR4);
293 unsigned long div1, div2;
294
295 if (pdr0 & (1 << 26)) {
296 div1 = (pdr4 >> 19) & 0x7;
297 div2 = (pdr4 >> 16) & 0x7;
298 return get_rate_arm() / ((div1 + 1) * (div2 + 1));
299 } else {
300 div1 = (pdr0 >> 12) & 0x7;
301 return get_rate_ahb(NULL) / div1;
302 }
303}
304
305static int clk_cgr_enable(struct clk *clk)
306{
307 u32 reg;
308
309 reg = __raw_readl(clk->enable_reg);
310 reg |= 3 << clk->enable_shift;
311 __raw_writel(reg, clk->enable_reg);
312
313 return 0;
314}
315
316static void clk_cgr_disable(struct clk *clk)
317{
318 u32 reg;
319
320 reg = __raw_readl(clk->enable_reg);
321 reg &= ~(3 << clk->enable_shift);
322 __raw_writel(reg, clk->enable_reg);
323}
324
325#define DEFINE_CLOCK(name, i, er, es, gr, sr) \
326 static struct clk name = { \
327 .id = i, \
328 .enable_reg = CCM_BASE + er, \
329 .enable_shift = es, \
330 .get_rate = gr, \
331 .set_rate = sr, \
332 .enable = clk_cgr_enable, \
333 .disable = clk_cgr_disable, \
334 }
335
336DEFINE_CLOCK(asrc_clk, 0, CCM_CGR0, 0, NULL, NULL);
337DEFINE_CLOCK(ata_clk, 0, CCM_CGR0, 2, get_rate_ipg, NULL);
338DEFINE_CLOCK(audmux_clk, 0, CCM_CGR0, 4, NULL, NULL);
339DEFINE_CLOCK(can1_clk, 0, CCM_CGR0, 6, get_rate_ipg, NULL);
340DEFINE_CLOCK(can2_clk, 1, CCM_CGR0, 8, get_rate_ipg, NULL);
341DEFINE_CLOCK(cspi1_clk, 0, CCM_CGR0, 10, get_rate_ipg, NULL);
342DEFINE_CLOCK(cspi2_clk, 1, CCM_CGR0, 12, get_rate_ipg, NULL);
343DEFINE_CLOCK(ect_clk, 0, CCM_CGR0, 14, get_rate_ipg, NULL);
344DEFINE_CLOCK(edio_clk, 0, CCM_CGR0, 16, NULL, NULL);
345DEFINE_CLOCK(emi_clk, 0, CCM_CGR0, 18, get_rate_ipg, NULL);
346DEFINE_CLOCK(epit1_clk, 0, CCM_CGR0, 20, get_rate_ipg_per, NULL);
347DEFINE_CLOCK(epit2_clk, 1, CCM_CGR0, 22, get_rate_ipg_per, NULL);
348DEFINE_CLOCK(esai_clk, 0, CCM_CGR0, 24, NULL, NULL);
349DEFINE_CLOCK(esdhc1_clk, 0, CCM_CGR0, 26, get_rate_sdhc, NULL);
350DEFINE_CLOCK(esdhc2_clk, 1, CCM_CGR0, 28, get_rate_sdhc, NULL);
351DEFINE_CLOCK(esdhc3_clk, 2, CCM_CGR0, 30, get_rate_sdhc, NULL);
352
353DEFINE_CLOCK(fec_clk, 0, CCM_CGR1, 0, get_rate_ipg, NULL);
354DEFINE_CLOCK(gpio1_clk, 0, CCM_CGR1, 2, NULL, NULL);
355DEFINE_CLOCK(gpio2_clk, 1, CCM_CGR1, 4, NULL, NULL);
356DEFINE_CLOCK(gpio3_clk, 2, CCM_CGR1, 6, NULL, NULL);
357DEFINE_CLOCK(gpt_clk, 0, CCM_CGR1, 8, get_rate_ipg, NULL);
358DEFINE_CLOCK(i2c1_clk, 0, CCM_CGR1, 10, get_rate_ipg_per, NULL);
359DEFINE_CLOCK(i2c2_clk, 1, CCM_CGR1, 12, get_rate_ipg_per, NULL);
360DEFINE_CLOCK(i2c3_clk, 2, CCM_CGR1, 14, get_rate_ipg_per, NULL);
361DEFINE_CLOCK(iomuxc_clk, 0, CCM_CGR1, 16, NULL, NULL);
362DEFINE_CLOCK(ipu_clk, 0, CCM_CGR1, 18, NULL, NULL);
363DEFINE_CLOCK(kpp_clk, 0, CCM_CGR1, 20, get_rate_ipg, NULL);
364DEFINE_CLOCK(mlb_clk, 0, CCM_CGR1, 22, get_rate_ahb, NULL);
365DEFINE_CLOCK(mshc_clk, 0, CCM_CGR1, 24, get_rate_mshc, NULL);
366DEFINE_CLOCK(owire_clk, 0, CCM_CGR1, 26, get_rate_ipg_per, NULL);
367DEFINE_CLOCK(pwm_clk, 0, CCM_CGR1, 28, get_rate_ipg_per, NULL);
368DEFINE_CLOCK(rngc_clk, 0, CCM_CGR1, 30, get_rate_ipg, NULL);
369
370DEFINE_CLOCK(rtc_clk, 0, CCM_CGR2, 0, get_rate_ipg, NULL);
371DEFINE_CLOCK(rtic_clk, 0, CCM_CGR2, 2, get_rate_ahb, NULL);
372DEFINE_CLOCK(scc_clk, 0, CCM_CGR2, 4, get_rate_ipg, NULL);
373DEFINE_CLOCK(sdma_clk, 0, CCM_CGR2, 6, NULL, NULL);
374DEFINE_CLOCK(spba_clk, 0, CCM_CGR2, 8, get_rate_ipg, NULL);
375DEFINE_CLOCK(spdif_clk, 0, CCM_CGR2, 10, NULL, NULL);
376DEFINE_CLOCK(ssi1_clk, 0, CCM_CGR2, 12, get_rate_ssi, NULL);
377DEFINE_CLOCK(ssi2_clk, 1, CCM_CGR2, 14, get_rate_ssi, NULL);
378DEFINE_CLOCK(uart1_clk, 0, CCM_CGR2, 16, get_rate_uart, NULL);
379DEFINE_CLOCK(uart2_clk, 1, CCM_CGR2, 18, get_rate_uart, NULL);
380DEFINE_CLOCK(uart3_clk, 2, CCM_CGR2, 20, get_rate_uart, NULL);
Sascha Hauer547270a2009-03-17 15:43:54 +0100381DEFINE_CLOCK(usbotg_clk, 0, CCM_CGR2, 22, get_rate_otg, NULL);
Sascha Hauer2cb536d2009-02-06 17:48:59 +0100382DEFINE_CLOCK(wdog_clk, 0, CCM_CGR2, 24, NULL, NULL);
383DEFINE_CLOCK(max_clk, 0, CCM_CGR2, 26, NULL, NULL);
384DEFINE_CLOCK(admux_clk, 0, CCM_CGR2, 30, NULL, NULL);
385
386DEFINE_CLOCK(csi_clk, 0, CCM_CGR3, 0, get_rate_csi, NULL);
387DEFINE_CLOCK(iim_clk, 0, CCM_CGR3, 2, NULL, NULL);
388DEFINE_CLOCK(gpu2d_clk, 0, CCM_CGR3, 4, NULL, NULL);
389
390#define _REGISTER_CLOCK(d, n, c) \
391 { \
392 .dev_id = d, \
393 .con_id = n, \
394 .clk = &c, \
395 },
396
Rabin Vincent6b4bfb82009-05-26 22:31:46 +0530397static struct clk_lookup lookups[] = {
Sascha Hauer2cb536d2009-02-06 17:48:59 +0100398 _REGISTER_CLOCK(NULL, "asrc", asrc_clk)
399 _REGISTER_CLOCK(NULL, "ata", ata_clk)
400 _REGISTER_CLOCK(NULL, "audmux", audmux_clk)
401 _REGISTER_CLOCK(NULL, "can", can1_clk)
402 _REGISTER_CLOCK(NULL, "can", can2_clk)
403 _REGISTER_CLOCK("spi_imx.0", NULL, cspi1_clk)
404 _REGISTER_CLOCK("spi_imx.1", NULL, cspi2_clk)
405 _REGISTER_CLOCK(NULL, "ect", ect_clk)
406 _REGISTER_CLOCK(NULL, "edio", edio_clk)
407 _REGISTER_CLOCK(NULL, "emi", emi_clk)
408 _REGISTER_CLOCK(NULL, "epit", epit1_clk)
409 _REGISTER_CLOCK(NULL, "epit", epit2_clk)
410 _REGISTER_CLOCK(NULL, "esai", esai_clk)
411 _REGISTER_CLOCK(NULL, "sdhc", esdhc1_clk)
412 _REGISTER_CLOCK(NULL, "sdhc", esdhc2_clk)
413 _REGISTER_CLOCK(NULL, "sdhc", esdhc3_clk)
414 _REGISTER_CLOCK("fec.0", NULL, fec_clk)
415 _REGISTER_CLOCK(NULL, "gpio", gpio1_clk)
416 _REGISTER_CLOCK(NULL, "gpio", gpio2_clk)
417 _REGISTER_CLOCK(NULL, "gpio", gpio3_clk)
418 _REGISTER_CLOCK("gpt.0", NULL, gpt_clk)
419 _REGISTER_CLOCK("imx-i2c.0", NULL, i2c1_clk)
420 _REGISTER_CLOCK("imx-i2c.1", NULL, i2c2_clk)
421 _REGISTER_CLOCK("imx-i2c.2", NULL, i2c3_clk)
422 _REGISTER_CLOCK(NULL, "iomuxc", iomuxc_clk)
Sascha Hauer76b6ea02009-04-01 10:09:18 +0200423 _REGISTER_CLOCK("ipu-core", NULL, ipu_clk)
424 _REGISTER_CLOCK("mx3_sdc_fb", NULL, ipu_clk)
Sascha Hauer2cb536d2009-02-06 17:48:59 +0100425 _REGISTER_CLOCK(NULL, "kpp", kpp_clk)
426 _REGISTER_CLOCK(NULL, "mlb", mlb_clk)
427 _REGISTER_CLOCK(NULL, "mshc", mshc_clk)
428 _REGISTER_CLOCK("mxc_w1", NULL, owire_clk)
429 _REGISTER_CLOCK(NULL, "pwm", pwm_clk)
430 _REGISTER_CLOCK(NULL, "rngc", rngc_clk)
431 _REGISTER_CLOCK(NULL, "rtc", rtc_clk)
432 _REGISTER_CLOCK(NULL, "rtic", rtic_clk)
433 _REGISTER_CLOCK(NULL, "scc", scc_clk)
434 _REGISTER_CLOCK(NULL, "sdma", sdma_clk)
435 _REGISTER_CLOCK(NULL, "spba", spba_clk)
436 _REGISTER_CLOCK(NULL, "spdif", spdif_clk)
437 _REGISTER_CLOCK(NULL, "ssi", ssi1_clk)
438 _REGISTER_CLOCK(NULL, "ssi", ssi2_clk)
439 _REGISTER_CLOCK("imx-uart.0", NULL, uart1_clk)
440 _REGISTER_CLOCK("imx-uart.1", NULL, uart2_clk)
441 _REGISTER_CLOCK("imx-uart.2", NULL, uart3_clk)
Sascha Hauer547270a2009-03-17 15:43:54 +0100442 _REGISTER_CLOCK("mxc-ehci.0", "usb", usbotg_clk)
443 _REGISTER_CLOCK("mxc-ehci.1", "usb", usbotg_clk)
444 _REGISTER_CLOCK("mxc-ehci.2", "usb", usbotg_clk)
445 _REGISTER_CLOCK("fsl-usb2-udc", "usb", usbotg_clk)
Sascha Hauer2cb536d2009-02-06 17:48:59 +0100446 _REGISTER_CLOCK("mxc_wdt.0", NULL, wdog_clk)
447 _REGISTER_CLOCK(NULL, "max", max_clk)
448 _REGISTER_CLOCK(NULL, "admux", admux_clk)
449 _REGISTER_CLOCK(NULL, "csi", csi_clk)
450 _REGISTER_CLOCK(NULL, "iim", iim_clk)
451 _REGISTER_CLOCK(NULL, "gpu2d", gpu2d_clk)
452};
453
454int __init mx35_clocks_init()
455{
456 int i;
457 unsigned int ll = 0;
458
Sascha Hauer2cb536d2009-02-06 17:48:59 +0100459#ifdef CONFIG_DEBUG_LL_CONSOLE
460 ll = (3 << 16);
461#endif
462
463 for (i = 0; i < ARRAY_SIZE(lookups); i++)
464 clkdev_add(&lookups[i]);
465
466 /* Turn off all clocks except the ones we need to survive, namely:
467 * EMI, GPIO1/2/3, GPT, IOMUX, MAX and eventually uart
468 */
469 __raw_writel((3 << 18), CCM_BASE + CCM_CGR0);
470 __raw_writel((3 << 2) | (3 << 4) | (3 << 6) | (3 << 8) | (3 << 16),
471 CCM_BASE + CCM_CGR1);
472 __raw_writel((3 << 26) | ll, CCM_BASE + CCM_CGR2);
473 __raw_writel(0, CCM_BASE + CCM_CGR3);
474
Sascha Hauercf983c92009-07-15 09:31:15 +0200475 mxc_timer_init(&gpt_clk, IO_ADDRESS(GPT1_BASE_ADDR), MXC_INT_GPT);
Sascha Hauer2cb536d2009-02-06 17:48:59 +0100476
477 return 0;
478}
479