2 * Copyright (C) 2007 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2007 Eugene Konev <ejka@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/types.h>
23 #include <linux/module.h>
24 #include <linux/delay.h>
25 #include <linux/gcd.h>
28 #include <asm/addrspace.h>
29 #include <asm/mach-ar7/ar7.h>
31 #define BOOT_PLL_SOURCE_MASK 0x3
32 #define CPU_PLL_SOURCE_SHIFT 16
33 #define BUS_PLL_SOURCE_SHIFT 14
34 #define USB_PLL_SOURCE_SHIFT 18
35 #define DSP_PLL_SOURCE_SHIFT 22
36 #define BOOT_PLL_SOURCE_AFE 0
37 #define BOOT_PLL_SOURCE_BUS 0
38 #define BOOT_PLL_SOURCE_REF 1
39 #define BOOT_PLL_SOURCE_XTAL 2
40 #define BOOT_PLL_SOURCE_CPU 3
41 #define BOOT_PLL_BYPASS 0x00000020
42 #define BOOT_PLL_ASYNC_MODE 0x02000000
43 #define BOOT_PLL_2TO1_MODE 0x00008000
45 #define TNETD7200_CLOCK_ID_CPU 0
46 #define TNETD7200_CLOCK_ID_DSP 1
47 #define TNETD7200_CLOCK_ID_USB 2
49 #define TNETD7200_DEF_CPU_CLK 211000000
50 #define TNETD7200_DEF_DSP_CLK 125000000
51 #define TNETD7200_DEF_USB_CLK 48000000
53 struct tnetd7300_clock
{
55 #define PREDIV_MASK 0x001f0000
56 #define PREDIV_SHIFT 16
57 #define POSTDIV_MASK 0x0000001f
60 #define MUL_MASK 0x0000f000
62 #define PLL_MODE_MASK 0x00000001
63 #define PLL_NDIV 0x00000800
64 #define PLL_DIV 0x00000002
65 #define PLL_STATUS 0x00000001
69 struct tnetd7300_clocks
{
70 struct tnetd7300_clock bus
;
71 struct tnetd7300_clock cpu
;
72 struct tnetd7300_clock usb
;
73 struct tnetd7300_clock dsp
;
76 struct tnetd7200_clock
{
79 #define DIVISOR_ENABLE_MASK 0x00008000
91 struct tnetd7200_clocks
{
92 struct tnetd7200_clock cpu
;
93 struct tnetd7200_clock dsp
;
94 struct tnetd7200_clock usb
;
97 int ar7_cpu_clock
= 150000000;
98 EXPORT_SYMBOL(ar7_cpu_clock
);
99 int ar7_bus_clock
= 125000000;
100 EXPORT_SYMBOL(ar7_bus_clock
);
102 EXPORT_SYMBOL(ar7_dsp_clock
);
104 static void approximate(int base
, int target
, int *prediv
,
105 int *postdiv
, int *mul
)
107 int i
, j
, k
, freq
, res
= target
;
108 for (i
= 1; i
<= 16; i
++)
109 for (j
= 1; j
<= 32; j
++)
110 for (k
= 1; k
<= 32; k
++) {
111 freq
= abs(base
/ j
* i
/ k
- target
);
121 static void calculate(int base
, int target
, int *prediv
, int *postdiv
,
124 int tmp_gcd
, tmp_base
, tmp_freq
;
126 for (*prediv
= 1; *prediv
<= 32; (*prediv
)++) {
127 tmp_base
= base
/ *prediv
;
128 tmp_gcd
= gcd(target
, tmp_base
);
129 *mul
= target
/ tmp_gcd
;
130 *postdiv
= tmp_base
/ tmp_gcd
;
131 if ((*mul
< 1) || (*mul
>= 16))
133 if ((*postdiv
> 0) & (*postdiv
<= 32))
137 if (base
/ *prediv
* *mul
/ *postdiv
!= target
) {
138 approximate(base
, target
, prediv
, postdiv
, mul
);
139 tmp_freq
= base
/ *prediv
* *mul
/ *postdiv
;
141 "Adjusted requested frequency %d to %d\n",
145 printk(KERN_DEBUG
"Clocks: prediv: %d, postdiv: %d, mul: %d\n",
146 *prediv
, *postdiv
, *mul
);
149 static int tnetd7300_dsp_clock(void)
152 u8 rev
= ar7_chip_rev();
153 didr1
= readl((void *)KSEG1ADDR(AR7_REGS_GPIO
+ 0x18));
154 didr2
= readl((void *)KSEG1ADDR(AR7_REGS_GPIO
+ 0x1c));
155 if (didr2
& (1 << 23))
157 if ((rev
>= 0x23) && (rev
!= 0x57))
159 if ((((didr2
& 0x1fff) << 10) | ((didr1
& 0xffc00000) >> 22))
165 static int tnetd7300_get_clock(u32 shift
, struct tnetd7300_clock
*clock
,
166 u32
*bootcr
, u32 bus_clock
)
169 int base_clock
= AR7_REF_CLOCK
;
170 u32 ctrl
= readl(&clock
->ctrl
);
171 u32 pll
= readl(&clock
->pll
);
172 int prediv
= ((ctrl
& PREDIV_MASK
) >> PREDIV_SHIFT
) + 1;
173 int postdiv
= (ctrl
& POSTDIV_MASK
) + 1;
174 int divisor
= prediv
* postdiv
;
175 int mul
= ((pll
& MUL_MASK
) >> MUL_SHIFT
) + 1;
177 switch ((*bootcr
& (BOOT_PLL_SOURCE_MASK
<< shift
)) >> shift
) {
178 case BOOT_PLL_SOURCE_BUS
:
179 base_clock
= bus_clock
;
181 case BOOT_PLL_SOURCE_REF
:
182 base_clock
= AR7_REF_CLOCK
;
184 case BOOT_PLL_SOURCE_XTAL
:
185 base_clock
= AR7_XTAL_CLOCK
;
187 case BOOT_PLL_SOURCE_CPU
:
188 base_clock
= ar7_cpu_clock
;
192 if (*bootcr
& BOOT_PLL_BYPASS
)
193 return base_clock
/ divisor
;
195 if ((pll
& PLL_MODE_MASK
) == 0)
196 return (base_clock
>> (mul
/ 16 + 1)) / divisor
;
198 if ((pll
& (PLL_NDIV
| PLL_DIV
)) == (PLL_NDIV
| PLL_DIV
)) {
199 product
= (mul
& 1) ?
200 (base_clock
* mul
) >> 1 :
201 (base_clock
* (mul
- 1)) >> 2;
202 return product
/ divisor
;
206 return base_clock
/ divisor
;
208 return base_clock
* mul
/ divisor
;
211 static void tnetd7300_set_clock(u32 shift
, struct tnetd7300_clock
*clock
,
212 u32
*bootcr
, u32 frequency
)
214 int prediv
, postdiv
, mul
;
215 int base_clock
= ar7_bus_clock
;
217 switch ((*bootcr
& (BOOT_PLL_SOURCE_MASK
<< shift
)) >> shift
) {
218 case BOOT_PLL_SOURCE_BUS
:
219 base_clock
= ar7_bus_clock
;
221 case BOOT_PLL_SOURCE_REF
:
222 base_clock
= AR7_REF_CLOCK
;
224 case BOOT_PLL_SOURCE_XTAL
:
225 base_clock
= AR7_XTAL_CLOCK
;
227 case BOOT_PLL_SOURCE_CPU
:
228 base_clock
= ar7_cpu_clock
;
232 calculate(base_clock
, frequency
, &prediv
, &postdiv
, &mul
);
234 writel(((prediv
- 1) << PREDIV_SHIFT
) | (postdiv
- 1), &clock
->ctrl
);
236 writel(4, &clock
->pll
);
237 while (readl(&clock
->pll
) & PLL_STATUS
)
239 writel(((mul
- 1) << MUL_SHIFT
) | (0xff << 3) | 0x0e, &clock
->pll
);
243 static void __init
tnetd7300_init_clocks(void)
245 u32
*bootcr
= (u32
*)ioremap_nocache(AR7_REGS_DCL
, 4);
246 struct tnetd7300_clocks
*clocks
=
247 ioremap_nocache(UR8_REGS_CLOCKS
,
248 sizeof(struct tnetd7300_clocks
));
250 ar7_bus_clock
= tnetd7300_get_clock(BUS_PLL_SOURCE_SHIFT
,
251 &clocks
->bus
, bootcr
, AR7_AFE_CLOCK
);
253 if (*bootcr
& BOOT_PLL_ASYNC_MODE
)
254 ar7_cpu_clock
= tnetd7300_get_clock(CPU_PLL_SOURCE_SHIFT
,
255 &clocks
->cpu
, bootcr
, AR7_AFE_CLOCK
);
257 ar7_cpu_clock
= ar7_bus_clock
;
259 if (ar7_dsp_clock
== 250000000)
260 tnetd7300_set_clock(DSP_PLL_SOURCE_SHIFT
, &clocks
->dsp
,
261 bootcr
, ar7_dsp_clock
);
267 static void tnetd7200_set_clock(int base
, struct tnetd7200_clock
*clock
,
268 int prediv
, int postdiv
, int postdiv2
, int mul
, u32 frequency
)
271 "Clocks: base = %d, frequency = %u, prediv = %d, "
272 "postdiv = %d, postdiv2 = %d, mul = %d\n",
273 base
, frequency
, prediv
, postdiv
, postdiv2
, mul
);
275 writel(0, &clock
->ctrl
);
276 writel(DIVISOR_ENABLE_MASK
| ((prediv
- 1) & 0x1F), &clock
->prediv
);
277 writel((mul
- 1) & 0xF, &clock
->mul
);
279 while (readl(&clock
->status
) & 0x1)
282 writel(DIVISOR_ENABLE_MASK
| ((postdiv
- 1) & 0x1F), &clock
->postdiv
);
284 writel(readl(&clock
->cmden
) | 1, &clock
->cmden
);
285 writel(readl(&clock
->cmd
) | 1, &clock
->cmd
);
287 while (readl(&clock
->status
) & 0x1)
290 writel(DIVISOR_ENABLE_MASK
| ((postdiv2
- 1) & 0x1F), &clock
->postdiv2
);
292 writel(readl(&clock
->cmden
) | 1, &clock
->cmden
);
293 writel(readl(&clock
->cmd
) | 1, &clock
->cmd
);
295 while (readl(&clock
->status
) & 0x1)
298 writel(readl(&clock
->ctrl
) | 1, &clock
->ctrl
);
301 static int tnetd7200_get_clock_base(int clock_id
, u32
*bootcr
)
303 if (*bootcr
& BOOT_PLL_ASYNC_MODE
)
306 case TNETD7200_CLOCK_ID_DSP
:
307 return AR7_REF_CLOCK
;
309 return AR7_AFE_CLOCK
;
313 if (*bootcr
& BOOT_PLL_2TO1_MODE
)
316 case TNETD7200_CLOCK_ID_DSP
:
317 return AR7_REF_CLOCK
;
319 return AR7_AFE_CLOCK
;
323 return AR7_REF_CLOCK
;
327 static void __init
tnetd7200_init_clocks(void)
329 u32
*bootcr
= (u32
*)ioremap_nocache(AR7_REGS_DCL
, 4);
330 struct tnetd7200_clocks
*clocks
=
331 ioremap_nocache(AR7_REGS_CLOCKS
,
332 sizeof(struct tnetd7200_clocks
));
333 int cpu_base
, cpu_mul
, cpu_prediv
, cpu_postdiv
;
334 int dsp_base
, dsp_mul
, dsp_prediv
, dsp_postdiv
;
335 int usb_base
, usb_mul
, usb_prediv
, usb_postdiv
;
337 cpu_base
= tnetd7200_get_clock_base(TNETD7200_CLOCK_ID_CPU
, bootcr
);
338 dsp_base
= tnetd7200_get_clock_base(TNETD7200_CLOCK_ID_DSP
, bootcr
);
340 if (*bootcr
& BOOT_PLL_ASYNC_MODE
) {
341 printk(KERN_INFO
"Clocks: Async mode\n");
343 printk(KERN_INFO
"Clocks: Setting DSP clock\n");
344 calculate(dsp_base
, TNETD7200_DEF_DSP_CLK
,
345 &dsp_prediv
, &dsp_postdiv
, &dsp_mul
);
347 ((dsp_base
/ dsp_prediv
) * dsp_mul
) / dsp_postdiv
;
348 tnetd7200_set_clock(dsp_base
, &clocks
->dsp
,
349 dsp_prediv
, dsp_postdiv
* 2, dsp_postdiv
, dsp_mul
* 2,
352 printk(KERN_INFO
"Clocks: Setting CPU clock\n");
353 calculate(cpu_base
, TNETD7200_DEF_CPU_CLK
, &cpu_prediv
,
354 &cpu_postdiv
, &cpu_mul
);
356 ((cpu_base
/ cpu_prediv
) * cpu_mul
) / cpu_postdiv
;
357 tnetd7200_set_clock(cpu_base
, &clocks
->cpu
,
358 cpu_prediv
, cpu_postdiv
, -1, cpu_mul
,
362 if (*bootcr
& BOOT_PLL_2TO1_MODE
) {
363 printk(KERN_INFO
"Clocks: Sync 2:1 mode\n");
365 printk(KERN_INFO
"Clocks: Setting CPU clock\n");
366 calculate(cpu_base
, TNETD7200_DEF_CPU_CLK
, &cpu_prediv
,
367 &cpu_postdiv
, &cpu_mul
);
368 ar7_cpu_clock
= ((cpu_base
/ cpu_prediv
) * cpu_mul
)
370 tnetd7200_set_clock(cpu_base
, &clocks
->cpu
,
371 cpu_prediv
, cpu_postdiv
, -1, cpu_mul
,
374 printk(KERN_INFO
"Clocks: Setting DSP clock\n");
375 calculate(dsp_base
, TNETD7200_DEF_DSP_CLK
, &dsp_prediv
,
376 &dsp_postdiv
, &dsp_mul
);
377 ar7_bus_clock
= ar7_cpu_clock
/ 2;
378 tnetd7200_set_clock(dsp_base
, &clocks
->dsp
,
379 dsp_prediv
, dsp_postdiv
* 2, dsp_postdiv
,
380 dsp_mul
* 2, ar7_bus_clock
);
382 printk(KERN_INFO
"Clocks: Sync 1:1 mode\n");
384 printk(KERN_INFO
"Clocks: Setting DSP clock\n");
385 calculate(dsp_base
, TNETD7200_DEF_DSP_CLK
, &dsp_prediv
,
386 &dsp_postdiv
, &dsp_mul
);
387 ar7_bus_clock
= ((dsp_base
/ dsp_prediv
) * dsp_mul
)
389 tnetd7200_set_clock(dsp_base
, &clocks
->dsp
,
390 dsp_prediv
, dsp_postdiv
* 2, dsp_postdiv
,
391 dsp_mul
* 2, ar7_bus_clock
);
393 ar7_cpu_clock
= ar7_bus_clock
;
396 printk(KERN_INFO
"Clocks: Setting USB clock\n");
397 usb_base
= ar7_bus_clock
;
398 calculate(usb_base
, TNETD7200_DEF_USB_CLK
, &usb_prediv
,
399 &usb_postdiv
, &usb_mul
);
400 tnetd7200_set_clock(usb_base
, &clocks
->usb
,
401 usb_prediv
, usb_postdiv
, -1, usb_mul
,
402 TNETD7200_DEF_USB_CLK
);
404 ar7_dsp_clock
= ar7_cpu_clock
;
410 int __init
ar7_init_clocks(void)
412 switch (ar7_chip_id()) {
415 tnetd7200_init_clocks();
418 ar7_dsp_clock
= tnetd7300_dsp_clock();
419 tnetd7300_init_clocks();
427 arch_initcall(ar7_init_clocks
);