04b49cc1065a7df1ea536a5bc66d1a7f13865220
[librecmc/librecmc.git] / target / linux / ar7-2.6 / files / arch / mips / ar7 / clock.c
1 /*
2  * $Id$
3  * 
4  * Copyright (C) 2007 OpenWrt.org
5  * 
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * 
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include <linux/init.h>
22 #include <linux/types.h>
23 #include <linux/module.h>
24 #include <linux/delay.h>
25 #include <asm/addrspace.h>
26 #include <asm/io.h>
27 #include <asm/ar7/ar7.h>
28
29 #define BOOT_PLL_SOURCE_MASK 0x3
30 #define CPU_PLL_SOURCE_SHIFT 16
31 #define BUS_PLL_SOURCE_SHIFT 14
32 #define USB_PLL_SOURCE_SHIFT 18
33 #define DSP_PLL_SOURCE_SHIFT 22
34 #define BOOT_PLL_SOURCE_AFE 0
35 #define BOOT_PLL_SOURCE_BUS 0
36 #define BOOT_PLL_SOURCE_REF 1
37 #define BOOT_PLL_SOURCE_XTAL 2
38 #define BOOT_PLL_SOURCE_CPU 3
39 #define BOOT_PLL_BYPASS 0x00000020
40 #define BOOT_PLL_ASYNC_MODE 0x02000000
41 #define BOOT_PLL_2TO1_MODE 0x00008000
42
43 struct tnetd7300_clock {
44         volatile u32 ctrl;
45 #define PREDIV_MASK 0x001f0000
46 #define PREDIV_SHIFT 16
47 #define POSTDIV_MASK 0x0000001f
48         u32 unused1[3];
49         volatile u32 pll;
50 #define MUL_MASK 0x0000f000
51 #define MUL_SHIFT 12
52 #define PLL_MODE_MASK 0x00000001
53 #define PLL_NDIV 0x00000800
54 #define PLL_DIV 0x00000002
55 #define PLL_STATUS 0x00000001
56         u32 unused2[3];
57 } __attribute__ ((packed));
58
59 struct tnetd7300_clocks {
60         struct tnetd7300_clock bus;
61         struct tnetd7300_clock cpu;
62         struct tnetd7300_clock usb;
63         struct tnetd7300_clock dsp;
64 } __attribute__ ((packed));
65
66 struct tnetd7200_clock {
67         volatile u32 ctrl;
68         u32 unused1[3];
69 #define DIVISOR_ENABLE_MASK 0x00008000
70         volatile u32 mul;
71         volatile u32 prediv;
72         volatile u32 postdiv;
73         u32 unused2[7];
74         volatile u32 cmd;
75         volatile u32 status;
76         volatile u32 cmden;
77         u32 padding[15];
78 };
79
80 struct tnetd7200_clocks {
81         struct tnetd7200_clock cpu;
82         struct tnetd7200_clock dsp;
83         struct tnetd7200_clock usb;
84 };
85
86 int ar7_afe_clock = 35328000;
87 int ar7_ref_clock = 25000000;
88 int ar7_xtal_clock = 24000000;
89
90 int ar7_cpu_clock = 150000000;
91 EXPORT_SYMBOL(ar7_cpu_clock);
92 int ar7_bus_clock = 125000000;
93 EXPORT_SYMBOL(ar7_bus_clock);
94 int ar7_dsp_clock = 0;
95 EXPORT_SYMBOL(ar7_dsp_clock);
96
97 static int gcd(int x, int y)
98 {
99         if (x > y)
100                 return (x % y) ? gcd(y, x % y) : y;
101         return (y % x) ? gcd(x, y % x) : x;
102 }
103
104 static inline int ABS(int x)
105 {
106         return (x >= 0) ? x : -x;
107 }
108
109 static void approximate(int base, int target, int *prediv,
110                         int *postdiv, int *mul)
111 {
112         int i, j, k, freq, res = target;
113         for (i = 1; i <= 16; i++) {
114                 for (j = 1; j <= 32; j++) {
115                         for (k = 1; k <= 32; k++) {
116                                 freq = ABS(base / j * i / k - target);
117                                 if (freq < res) {
118                                         res = freq;
119                                         *mul = i;
120                                         *prediv = j;
121                                         *postdiv = k;
122                                 }
123                         }
124                 }
125         }
126 }
127
128 static void calculate(int base, int target, int *prediv, int *postdiv,
129                       int *mul)
130 {
131         int tmp_gcd, tmp_base, tmp_freq;
132
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))
139                         continue;
140                 if ((*postdiv > 0) & (*postdiv <= 32))
141                         break;
142         }
143
144         if (base / (*prediv) * (*mul) / (*postdiv) != target) {
145                 approximate(base, target, prediv, postdiv, mul);
146                 tmp_freq = base / (*prediv) * (*mul) / (*postdiv);
147                 printk(KERN_WARNING
148                        "Adjusted requested frequency %d to %d\n",
149                        target, tmp_freq);
150         }
151
152         printk(KERN_DEBUG "Clocks: prediv: %d, postdiv: %d, mul: %d\n",
153                *prediv, *postdiv, *mul);
154 }
155
156 static int tnetd7300_dsp_clock(void)
157 {
158         u32 didr1, didr2;
159         u8 rev = ar7_chip_rev();
160         didr1 = readl((void *)KSEG1ADDR(AR7_REGS_GPIO + 0x18));
161         didr2 = readl((void *)KSEG1ADDR(AR7_REGS_GPIO + 0x1c));
162         if (didr2 & (1 << 23))
163                 return 0;
164         if ((rev >= 0x23) && (rev != 0x57))
165                 return 250000000;
166         if ((((didr2 & 0x1fff) << 10) | ((didr1 & 0xffc00000) >> 22))
167             > 4208000)
168                 return 250000000;
169         return 0;
170 }
171
172 static int tnetd7300_get_clock(u32 shift, struct tnetd7300_clock *clock,
173                                u32 *bootcr, u32 bus_clock)
174 {
175         int product;
176         int base_clock = ar7_ref_clock;
177         int prediv = ((clock->ctrl & PREDIV_MASK) >> PREDIV_SHIFT) + 1;
178         int postdiv = (clock->ctrl & POSTDIV_MASK) + 1;
179         int divisor = prediv * postdiv;
180         int mul = ((clock->pll & MUL_MASK) >> MUL_SHIFT) + 1;
181
182         switch ((*bootcr & (BOOT_PLL_SOURCE_MASK << shift)) >> shift) {
183         case BOOT_PLL_SOURCE_BUS:
184                 base_clock = bus_clock;
185                 break;
186         case BOOT_PLL_SOURCE_REF:
187                 base_clock = ar7_ref_clock;
188                 break;
189         case BOOT_PLL_SOURCE_XTAL:
190                 base_clock = ar7_xtal_clock;
191                 break;
192         case BOOT_PLL_SOURCE_CPU:
193                 base_clock = ar7_cpu_clock;
194                 break;
195         }
196
197         if (*bootcr & BOOT_PLL_BYPASS)
198                 return base_clock / divisor;
199
200         if ((clock->pll & PLL_MODE_MASK) == 0)
201                 return (base_clock >> (mul / 16 + 1)) / divisor;
202
203         if ((clock->pll & (PLL_NDIV | PLL_DIV)) == (PLL_NDIV | PLL_DIV)) {
204                 product = (mul & 1) ? 
205                         (base_clock * mul) >> 1 :
206                         (base_clock * (mul - 1)) >> 2;
207                 return product / divisor;
208         }
209
210         if (mul == 16)
211                 return base_clock / divisor;
212
213         return base_clock * mul / divisor;
214 }
215
216 static void tnetd7300_set_clock(u32 shift, struct tnetd7300_clock *clock,
217                                 u32 *bootcr, u32 frequency)
218 {
219         u32 status;
220         int prediv, postdiv, mul;
221         int base_clock = ar7_bus_clock;
222
223         switch ((*bootcr & (BOOT_PLL_SOURCE_MASK << shift)) >> shift) {
224         case BOOT_PLL_SOURCE_BUS:
225                 base_clock = ar7_bus_clock;
226                 break;
227         case BOOT_PLL_SOURCE_REF:
228                 base_clock = ar7_ref_clock;
229                 break;
230         case BOOT_PLL_SOURCE_XTAL:
231                 base_clock = ar7_xtal_clock;
232                 break;
233         case BOOT_PLL_SOURCE_CPU:
234                 base_clock = ar7_cpu_clock;
235                 break;
236         }
237
238         calculate(base_clock, frequency, &prediv, &postdiv, &mul);
239
240         clock->ctrl = ((prediv - 1) << PREDIV_SHIFT) | (postdiv - 1);
241         mdelay(1);
242         clock->pll = 4;
243         do {
244                 status = clock->pll;
245         } while (status & PLL_STATUS);
246         clock->pll = ((mul - 1) << MUL_SHIFT) | (0xff << 3) | 0x0e;
247         mdelay(75);
248 }
249
250 static void __init tnetd7300_init_clocks(void)
251 {
252         u32 *bootcr = (u32 *)ioremap_nocache(AR7_REGS_DCL, 4);
253         struct tnetd7300_clocks *clocks = (struct tnetd7300_clocks *)ioremap_nocache(AR7_REGS_POWER + 0x20, sizeof(struct tnetd7300_clocks)); 
254
255         ar7_bus_clock = tnetd7300_get_clock(BUS_PLL_SOURCE_SHIFT, 
256                                             &clocks->bus, bootcr,
257                                             ar7_afe_clock);
258
259         if (*bootcr & BOOT_PLL_ASYNC_MODE) {
260                 ar7_cpu_clock = tnetd7300_get_clock(CPU_PLL_SOURCE_SHIFT, 
261                                                     &clocks->cpu,
262                                                     bootcr, ar7_afe_clock);
263         } else {
264                 ar7_cpu_clock = ar7_bus_clock;
265         }
266
267         tnetd7300_set_clock(USB_PLL_SOURCE_SHIFT, &clocks->usb,
268                             bootcr, 48000000);
269
270         if (ar7_dsp_clock == 250000000)
271                 tnetd7300_set_clock(DSP_PLL_SOURCE_SHIFT, &clocks->dsp,
272                                     bootcr, ar7_dsp_clock);
273
274         iounmap(clocks);
275         iounmap(bootcr);
276 }
277
278 static int tnetd7200_get_clock(int base, struct tnetd7200_clock *clock,
279                                u32 *bootcr, u32 bus_clock)
280 {
281         int divisor = ((clock->prediv & 0x1f) + 1) * 
282                 ((clock->postdiv & 0x1f) + 1);
283
284         if (*bootcr & BOOT_PLL_BYPASS)
285                 return base / divisor;
286
287         return base * ((clock->mul & 0xf) + 1) / divisor;
288 }
289
290 static void tnetd7200_set_clock(int base, struct tnetd7200_clock *clock,
291                                 u32 *bootcr, u32 frequency) 
292 {
293         u32 status;
294         int prediv, postdiv, mul;
295
296         calculate(base, frequency, &prediv, &postdiv, &mul);
297
298         clock->ctrl = 0;
299         clock->prediv = DIVISOR_ENABLE_MASK | prediv;
300         clock->mul = mul;
301         mdelay(1);
302         do {
303                 status = clock->status;
304         } while (status & PLL_STATUS);
305         clock->postdiv = DIVISOR_ENABLE_MASK | postdiv;
306         clock->cmden = 1;
307         clock->cmd = 1;
308         do {
309                 status = clock->status;
310         } while (status & PLL_STATUS);
311         clock->ctrl = 1;
312 }
313
314 static void __init tnetd7200_init_clocks(void)
315 {
316         u32 *bootcr = (u32 *)ioremap_nocache(AR7_REGS_DCL, 4);
317         struct tnetd7200_clocks *clocks = (struct tnetd7200_clocks *)ioremap_nocache(AR7_REGS_POWER + 0x80, sizeof(struct tnetd7200_clocks)); 
318
319         ar7_cpu_clock = tnetd7200_get_clock(ar7_afe_clock,
320                                             &clocks->cpu,
321                                             bootcr, ar7_afe_clock);
322
323         if (*bootcr & BOOT_PLL_ASYNC_MODE) {
324                 ar7_bus_clock = 125000000;
325         } else {
326                 if (*bootcr & BOOT_PLL_2TO1_MODE) {
327                         ar7_bus_clock = ar7_cpu_clock / 2;
328                 } else {
329                         ar7_bus_clock = ar7_cpu_clock;
330                 }
331         }
332
333         tnetd7200_set_clock(ar7_ref_clock * 5, &clocks->usb,
334                             bootcr, 48000000);
335
336         if (ar7_dsp_clock == 250000000)
337                 tnetd7200_set_clock(ar7_ref_clock, &clocks->dsp,
338                                     bootcr, ar7_dsp_clock);
339
340         iounmap(clocks);
341         iounmap(bootcr);
342 }
343
344 void __init ar7_init_clocks(void)
345 {
346         switch (ar7_chip_id()) {
347         case AR7_CHIP_7100:
348                 tnetd7200_init_clocks();
349                 break;
350         case AR7_CHIP_7200:
351 #warning FIXME: check revision
352                 ar7_dsp_clock = 250000000;
353                 tnetd7200_init_clocks();
354                 break;
355         case AR7_CHIP_7300:
356                 ar7_dsp_clock = tnetd7300_dsp_clock();
357                 tnetd7300_init_clocks();
358                 break;
359         default:
360                 break;
361         }
362 }