2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU General Public License version 2 as published
4 * by the Free Software Foundation.
6 * Copyright (C) 2010 John Crispin <blogic@openwrt.org>
10 #include <linux/export.h>
11 #include <linux/init.h>
12 #include <linux/clk.h>
16 #include <asm/div64.h>
18 #include <lantiq_soc.h>
20 static unsigned int ltq_ram_clocks
[] = {
21 CLOCK_167M
, CLOCK_133M
, CLOCK_111M
, CLOCK_83M
};
22 #define DDR_HZ ltq_ram_clocks[ltq_cgu_r32(LTQ_CGU_SYS) & 0x3]
24 #define BASIC_FREQUENCY_1 35328000
25 #define BASIC_FREQUENCY_2 36000000
26 #define BASIS_REQUENCY_USB 12000000
28 #define GET_BITS(x, msb, lsb) \
29 (((x) & ((1 << ((msb) + 1)) - 1)) >> (lsb))
31 #define LTQ_CGU_PLL0_CFG 0x0004
32 #define LTQ_CGU_PLL1_CFG 0x0008
33 #define LTQ_CGU_PLL2_CFG 0x000C
34 #define LTQ_CGU_SYS 0x0010
35 #define LTQ_CGU_UPDATE 0x0014
36 #define LTQ_CGU_IF_CLK 0x0018
37 #define LTQ_CGU_OSC_CON 0x001C
38 #define LTQ_CGU_SMD 0x0020
39 #define LTQ_CGU_CT1SR 0x0028
40 #define LTQ_CGU_CT2SR 0x002C
41 #define LTQ_CGU_PCMCR 0x0030
42 #define LTQ_CGU_PCI_CR 0x0034
43 #define LTQ_CGU_PD_PC 0x0038
44 #define LTQ_CGU_FMR 0x003C
46 #define CGU_PLL0_PHASE_DIVIDER_ENABLE \
47 (ltq_cgu_r32(LTQ_CGU_PLL0_CFG) & (1 << 31))
48 #define CGU_PLL0_BYPASS \
49 (ltq_cgu_r32(LTQ_CGU_PLL0_CFG) & (1 << 30))
50 #define CGU_PLL0_CFG_DSMSEL \
51 (ltq_cgu_r32(LTQ_CGU_PLL0_CFG) & (1 << 28))
52 #define CGU_PLL0_CFG_FRAC_EN \
53 (ltq_cgu_r32(LTQ_CGU_PLL0_CFG) & (1 << 27))
54 #define CGU_PLL1_SRC \
55 (ltq_cgu_r32(LTQ_CGU_PLL1_CFG) & (1 << 31))
56 #define CGU_PLL2_PHASE_DIVIDER_ENABLE \
57 (ltq_cgu_r32(LTQ_CGU_PLL2_CFG) & (1 << 20))
58 #define CGU_SYS_FPI_SEL (1 << 6)
59 #define CGU_SYS_DDR_SEL 0x3
60 #define CGU_PLL0_SRC (1 << 29)
62 #define CGU_PLL0_CFG_PLLK GET_BITS(ltq_cgu_r32(LTQ_CGU_PLL0_CFG), 26, 17)
63 #define CGU_PLL0_CFG_PLLN GET_BITS(ltq_cgu_r32(LTQ_CGU_PLL0_CFG), 12, 6)
64 #define CGU_PLL0_CFG_PLLM GET_BITS(ltq_cgu_r32(LTQ_CGU_PLL0_CFG), 5, 2)
65 #define CGU_PLL2_SRC GET_BITS(ltq_cgu_r32(LTQ_CGU_PLL2_CFG), 18, 17)
66 #define CGU_PLL2_CFG_INPUT_DIV GET_BITS(ltq_cgu_r32(LTQ_CGU_PLL2_CFG), 16, 13)
68 static unsigned int ltq_get_pll0_fdiv(void);
70 static inline unsigned int get_input_clock(int pll
)
74 if (ltq_cgu_r32(LTQ_CGU_PLL0_CFG
) & CGU_PLL0_SRC
)
75 return BASIS_REQUENCY_USB
;
76 else if (CGU_PLL0_PHASE_DIVIDER_ENABLE
)
77 return BASIC_FREQUENCY_1
;
79 return BASIC_FREQUENCY_2
;
82 return BASIS_REQUENCY_USB
;
83 else if (CGU_PLL0_PHASE_DIVIDER_ENABLE
)
84 return BASIC_FREQUENCY_1
;
86 return BASIC_FREQUENCY_2
;
88 switch (CGU_PLL2_SRC
) {
90 return ltq_get_pll0_fdiv();
92 return CGU_PLL2_PHASE_DIVIDER_ENABLE
?
96 return BASIS_REQUENCY_USB
;
103 static inline unsigned int cal_dsm(int pll
, unsigned int num
, unsigned int den
)
105 u64 res
, clock
= get_input_clock(pll
);
112 static inline unsigned int mash_dsm(int pll
, unsigned int M
, unsigned int N
,
115 unsigned int num
= ((N
+ 1) << 10) + K
;
116 unsigned int den
= (M
+ 1) << 10;
118 return cal_dsm(pll
, num
, den
);
121 static inline unsigned int ssff_dsm_1(int pll
, unsigned int M
, unsigned int N
,
124 unsigned int num
= ((N
+ 1) << 11) + K
+ 512;
125 unsigned int den
= (M
+ 1) << 11;
127 return cal_dsm(pll
, num
, den
);
130 static inline unsigned int ssff_dsm_2(int pll
, unsigned int M
, unsigned int N
,
133 unsigned int num
= K
>= 512 ?
134 ((N
+ 1) << 12) + K
- 512 : ((N
+ 1) << 12) + K
+ 3584;
135 unsigned int den
= (M
+ 1) << 12;
137 return cal_dsm(pll
, num
, den
);
140 static inline unsigned int dsm(int pll
, unsigned int M
, unsigned int N
,
141 unsigned int K
, unsigned int dsmsel
, unsigned int phase_div_en
)
144 return mash_dsm(pll
, M
, N
, K
);
145 else if (!phase_div_en
)
146 return mash_dsm(pll
, M
, N
, K
);
148 return ssff_dsm_2(pll
, M
, N
, K
);
151 static inline unsigned int ltq_get_pll0_fosc(void)
154 return get_input_clock(0);
156 return !CGU_PLL0_CFG_FRAC_EN
157 ? dsm(0, CGU_PLL0_CFG_PLLM
, CGU_PLL0_CFG_PLLN
, 0,
159 CGU_PLL0_PHASE_DIVIDER_ENABLE
)
160 : dsm(0, CGU_PLL0_CFG_PLLM
, CGU_PLL0_CFG_PLLN
,
161 CGU_PLL0_CFG_PLLK
, CGU_PLL0_CFG_DSMSEL
,
162 CGU_PLL0_PHASE_DIVIDER_ENABLE
);
165 static unsigned int ltq_get_pll0_fdiv(void)
167 unsigned int div
= CGU_PLL2_CFG_INPUT_DIV
+ 1;
169 return (ltq_get_pll0_fosc() + (div
>> 1)) / div
;
172 unsigned int ltq_get_io_region_clock(void)
174 unsigned int ret
= ltq_get_pll0_fosc();
176 switch (ltq_cgu_r32(LTQ_CGU_PLL2_CFG
) & CGU_SYS_DDR_SEL
) {
179 return (ret
+ 1) / 2;
181 return (ret
* 2 + 2) / 5;
183 return (ret
+ 1) / 3;
185 return (ret
+ 2) / 4;
188 EXPORT_SYMBOL(ltq_get_io_region_clock
);
190 unsigned int ltq_get_fpi_bus_clock(int fpi
)
192 unsigned int ret
= ltq_get_io_region_clock();
194 if ((fpi
== 2) && (ltq_cgu_r32(LTQ_CGU_SYS
) & CGU_SYS_FPI_SEL
))
198 EXPORT_SYMBOL(ltq_get_fpi_bus_clock
);
200 unsigned int ltq_get_cpu_hz(void)
202 switch (ltq_cgu_r32(LTQ_CGU_SYS
) & 0xc) {
213 EXPORT_SYMBOL(ltq_get_cpu_hz
);
215 unsigned int ltq_get_fpi_hz(void)
217 unsigned int ddr_clock
= DDR_HZ
;
219 if (ltq_cgu_r32(LTQ_CGU_SYS
) & 0x40)
220 return ddr_clock
>> 1;
223 EXPORT_SYMBOL(ltq_get_fpi_hz
);