21 #include <linux/kernel.h>
23 #include <linux/types.h>
24 #include <linux/module.h>
31 #include <asm/addrspace.h>
34 #define BOOT_PLL_SOURCE_MASK 0x3
35 #define CPU_PLL_SOURCE_SHIFT 16
36 #define BUS_PLL_SOURCE_SHIFT 14
37 #define USB_PLL_SOURCE_SHIFT 18
38 #define DSP_PLL_SOURCE_SHIFT 22
39 #define BOOT_PLL_SOURCE_AFE 0
40 #define BOOT_PLL_SOURCE_BUS 0
41 #define BOOT_PLL_SOURCE_REF 1
42 #define BOOT_PLL_SOURCE_XTAL 2
43 #define BOOT_PLL_SOURCE_CPU 3
44 #define BOOT_PLL_BYPASS 0x00000020
45 #define BOOT_PLL_ASYNC_MODE 0x02000000
46 #define BOOT_PLL_2TO1_MODE 0x00008000
48 #define TNETD7200_CLOCK_ID_CPU 0
49 #define TNETD7200_CLOCK_ID_DSP 1
50 #define TNETD7200_CLOCK_ID_USB 2
52 #define TNETD7200_DEF_CPU_CLK 211000000
53 #define TNETD7200_DEF_DSP_CLK 125000000
54 #define TNETD7200_DEF_USB_CLK 48000000
58 #define PREDIV_MASK 0x001f0000
59 #define PREDIV_SHIFT 16
60 #define POSTDIV_MASK 0x0000001f
63 #define MUL_MASK 0x0000f000
65 #define PLL_MODE_MASK 0x00000001
66 #define PLL_NDIV 0x00000800
67 #define PLL_DIV 0x00000002
68 #define PLL_STATUS 0x00000001
82 #define DIVISOR_ENABLE_MASK 0x00008000
100 static struct clk bus_clk = {
104 static struct clk cpu_clk = {
108 static struct clk dsp_clk;
109 static struct clk vbus_clk;
111 static void approximate(
int base,
int target,
int *prediv,
112 int *postdiv,
int *mul)
115 for (i = 1; i <= 16; i++)
116 for (j = 1; j <= 32; j++)
117 for (k = 1; k <= 32; k++) {
118 freq =
abs(base / j * i / k - target);
128 static void calculate(
int base,
int target,
int *prediv,
int *postdiv,
131 int tmp_gcd, tmp_base, tmp_freq;
133 for (*prediv = 1; *prediv <= 32; (*prediv)++) {
134 tmp_base = base / *prediv;
135 tmp_gcd =
gcd(target, tmp_base);
136 *mul = target / tmp_gcd;
137 *postdiv = tmp_base / tmp_gcd;
138 if ((*mul < 1) || (*mul >= 16))
140 if ((*postdiv > 0) & (*postdiv <= 32))
144 if (base / *prediv * *mul / *postdiv != target) {
145 approximate(base, target, prediv, postdiv, mul);
146 tmp_freq = base / *prediv * *mul / *postdiv;
148 "Adjusted requested frequency %d to %d\n",
153 *prediv, *postdiv, *mul);
156 static int tnetd7300_dsp_clock(
void)
159 u8 rev = ar7_chip_rev();
162 if (didr2 & (1 << 23))
164 if ((rev >= 0x23) && (rev != 0x57))
166 if ((((didr2 & 0x1fff) << 10) | ((didr1 & 0xffc00000) >> 22))
173 u32 *bootcr,
u32 bus_clock)
181 int divisor = prediv * postdiv;
186 base_clock = bus_clock;
195 base_clock = cpu_clk.
rate;
203 return (base_clock >> (mul / 16 + 1)) / divisor;
206 product = (mul & 1) ?
207 (base_clock * mul) >> 1 :
208 (base_clock * (mul - 1)) >> 2;
215 return base_clock * mul /
divisor;
221 int prediv, postdiv, mul;
222 int base_clock = bus_clk.
rate;
226 base_clock = bus_clk.
rate;
235 base_clock = cpu_clk.
rate;
239 calculate(base_clock, frequency, &prediv, &postdiv, &mul);
250 static void __init tnetd7300_init_clocks(
void)
266 if (dsp_clk.rate == 250000000)
268 bootcr, dsp_clk.rate);
274 static void tnetd7200_set_clock(
int base,
struct tnetd7200_clock *clock,
275 int prediv,
int postdiv,
int postdiv2,
int mul,
u32 frequency)
278 "Clocks: base = %d, frequency = %u, prediv = %d, "
279 "postdiv = %d, postdiv2 = %d, mul = %d\n",
280 base, frequency, prediv, postdiv, postdiv2, mul);
308 static int tnetd7200_get_clock_base(
int clock_id,
u32 *bootcr)
310 if (*bootcr & BOOT_PLL_ASYNC_MODE)
334 static void __init tnetd7200_init_clocks(
void)
340 int cpu_base, cpu_mul, cpu_prediv, cpu_postdiv;
341 int dsp_base, dsp_mul, dsp_prediv, dsp_postdiv;
342 int usb_base, usb_mul, usb_prediv, usb_postdiv;
347 if (*bootcr & BOOT_PLL_ASYNC_MODE) {
352 &dsp_prediv, &dsp_postdiv, &dsp_mul);
354 ((dsp_base / dsp_prediv) * dsp_mul) / dsp_postdiv;
355 tnetd7200_set_clock(dsp_base, &clocks->
dsp,
356 dsp_prediv, dsp_postdiv * 2, dsp_postdiv, dsp_mul * 2,
361 &cpu_postdiv, &cpu_mul);
363 ((cpu_base / cpu_prediv) * cpu_mul) / cpu_postdiv;
364 tnetd7200_set_clock(cpu_base, &clocks->
cpu,
365 cpu_prediv, cpu_postdiv, -1, cpu_mul,
369 if (*bootcr & BOOT_PLL_2TO1_MODE) {
374 &cpu_postdiv, &cpu_mul);
375 cpu_clk.
rate = ((cpu_base / cpu_prediv) * cpu_mul)
377 tnetd7200_set_clock(cpu_base, &clocks->
cpu,
378 cpu_prediv, cpu_postdiv, -1, cpu_mul,
383 &dsp_postdiv, &dsp_mul);
385 tnetd7200_set_clock(dsp_base, &clocks->
dsp,
386 dsp_prediv, dsp_postdiv * 2, dsp_postdiv,
387 dsp_mul * 2, bus_clk.
rate);
393 &dsp_postdiv, &dsp_mul);
394 bus_clk.
rate = ((dsp_base / dsp_prediv) * dsp_mul)
396 tnetd7200_set_clock(dsp_base, &clocks->
dsp,
397 dsp_prediv, dsp_postdiv * 2, dsp_postdiv,
398 dsp_mul * 2, bus_clk.
rate);
404 usb_base = bus_clk.
rate;
406 &usb_postdiv, &usb_mul);
407 tnetd7200_set_clock(usb_base, &clocks->
usb,
408 usb_prediv, usb_postdiv, -1, usb_mul,
411 dsp_clk.rate = cpu_clk.
rate;
461 switch (ar7_chip_id()) {
464 tnetd7200_init_clocks();
467 dsp_clk.rate = tnetd7300_dsp_clock();
468 tnetd7300_init_clocks();
474 vbus_clk.rate = bus_clk.
rate / 2;