2 * ########################################################################
4 * This program is free software; you can distribute it and/or modify it
5 * under the terms of the GNU General Public License (Version 2) as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
17 * ########################################################################
22 * device driver of clock generation unit of Danube chip
27 * History & Modification Tag:
28 * ___________________________________________________________________________
29 * | Tag | Comments | Modifier & Time |
30 * |--------+---------------------------------------------+------------------|
31 * | S0.0 | First version of this driver and the tag is | Samuels Xu Liang |
32 * | | implied. | 19 Jul 2005 |
33 * ---------------------------------------------------------------------------
39 * ####################################
41 * ####################################
47 #include <linux/config.h>
48 #include <linux/kernel.h>
49 #include <linux/module.h>
50 #include <linux/version.h>
51 #include <linux/types.h>
53 #include <linux/miscdevice.h>
54 #include <linux/init.h>
55 #include <asm/uaccess.h>
56 #include <asm/unistd.h>
58 #include <linux/errno.h>
61 * Chip Specific Head File
67 * ####################################
69 * ####################################
72 #define DEBUG_ON_AMAZON 1
73 #define DEBUG_PRINT_INFO 1
76 * Frequency of Clock Direct Feed from The Analog Line Driver Chip
78 #define BASIC_INPUT_CLOCK_FREQUENCY 35328000
83 #define GET_BITS(x, msb, lsb) (((x) & ((1 << ((msb) + 1)) - 1)) >> (lsb))
84 #define SET_BITS(x, msb, lsb, value) (((x) & ~(((1 << ((msb) + 1)) - 1) ^ ((1 << (lsb)) - 1))) | (((value) & ((1 << (1 + (msb) - (lsb))) - 1)) << (lsb)))
87 * CGU Register Mapping
89 #define DANUBE_CGU (KSEG1 + 0x1F103000)
90 #define DANUBE_CGU_DIV ((volatile u32*)(DANUBE_CGU + 0x0000))
91 #define DANUBE_CGU_PLL_NMK0 ((volatile u32*)(DANUBE_CGU + 0x0004))
92 #define DANUBE_CGU_PLL_SR0 ((volatile u32*)(DANUBE_CGU + 0x0008))
93 #define DANUBE_CGU_PLL_NMK1 ((volatile u32*)(DANUBE_CGU + 0x000C))
94 #define DANUBE_CGU_PLL_SR1 ((volatile u32*)(DANUBE_CGU + 0x0010))
95 #define DANUBE_CGU_PLL_SR2 ((volatile u32*)(DANUBE_CGU + 0x0014))
96 #define DANUBE_CGU_IF_CLK ((volatile u32*)(DANUBE_CGU + 0x0018))
97 #define DANUBE_CGU_OSC_CTRL ((volatile u32*)(DANUBE_CGU + 0x001C))
98 #define DANUBE_CGU_SMD ((volatile u32*)(DANUBE_CGU + 0x0020))
99 #define DANUBE_CGU_CRD ((volatile u32*)(DANUBE_CGU + 0x0024))
100 #define DANUBE_CGU_CT1SR ((volatile u32*)(DANUBE_CGU + 0x0028))
101 #define DANUBE_CGU_CT2SR ((volatile u32*)(DANUBE_CGU + 0x002C))
102 #define DANUBE_CGU_PCMCR ((volatile u32*)(DANUBE_CGU + 0x0030))
103 #define DANUBE_CGU_MUX ((volatile u32*)(DANUBE_CGU + 0x0034))
106 * CGU Divider Register
108 #define CGU_DIV_SFTR (*DANUBE_CGU_DIV & (1 << 31))
109 #define CGU_DIV_DIVE (*DANUBE_CGU_DIV & (1 << 16))
110 #define CGU_DIV_IOR GET_BITS(*DANUBE_CGU_DIV, 5, 4)
111 #define CGU_DIV_FKS GET_BITS(*DANUBE_CGU_DIV, 3, 2)
112 #define CGU_DIV_FBS GET_BITS(*DANUBE_CGU_DIV, 1, 0)
115 * CGU PLL0 NMK Register
117 #define CGU_PLL_NMK0_PLLN ((*DANUBE_CGU_PLL_NMK0 & (0xFFFFFFFF ^ ((1 << 24) - 1))) >> 24)
118 #define CGU_PLL_NMK0_PLLM GET_BITS(*DANUBE_CGU_PLL_NMK0, 23, 20)
119 #define CGU_PLL_NMK0_PLLK GET_BITS(*DANUBE_CGU_PLL_NMK0, 19, 0)
122 * CGU PLL0 Status Register
124 #define CGU_PLL_SR0_PLLDIV ((*DANUBE_CGU_PLL_SR0 & (0xFFFFFFFF ^ ((1 << 28) - 1))) >> 28)
125 #define CGU_PLL_SR0_PLLDEN (*DANUBE_CGU_PLL_SR0 & (1 << 26))
126 #define CGU_PLL_SR0_PLLPSE GET_BITS(*DANUBE_CGU_PLL_SR0, 5, 4)
127 #define CGU_PLL_SR0_PLLB (*DANUBE_CGU_PLL_SR0 & (1 << 2))
128 #define CGU_PLL_SR0_PLLL (*DANUBE_CGU_PLL_SR0 & (1 << 1))
129 #define CGU_PLL_SR0_PLLEN (*DANUBE_CGU_PLL_SR0 & (1 << 0))
131 #define CGU_PLL_SR0_DSMSEL 1
132 #define CGU_PLL_SR0_PHASE_DIV_EN 1
135 * CGU PLL1 NMK Register
137 #define CGU_PLL_NMK1_PLLN ((*DANUBE_CGU_PLL_NMK1 & (0xFFFFFFFF ^ ((1 << 24) - 1))) >> 24)
138 #define CGU_PLL_NMK1_PLLM GET_BITS(*DANUBE_CGU_PLL_NMK1, 23, 20)
139 #define CGU_PLL_NMK1_PLLK GET_BITS(*DANUBE_CGU_PLL_NMK1, 19, 0)
142 * CGU PLL1 Status Register
144 #define CGU_PLL_SR1_PLLDIV ((*DANUBE_CGU_PLL_SR1 & (0xFFFFFFFF ^ ((1 << 28) - 1))) >> 28)
145 #define CGU_PLL_SR1_PLLDEN (*DANUBE_CGU_PLL_SR1 & (1 << 26))
146 #define CGU_PLL_SR1_PLLPSE GET_BITS(*DANUBE_CGU_PLL_SR1, 5, 4)
147 #define CGU_PLL_SR1_PLLB (*DANUBE_CGU_PLL_SR1 & (1 << 2))
148 #define CGU_PLL_SR1_PLLL (*DANUBE_CGU_PLL_SR1 & (1 << 1))
149 #define CGU_PLL_SR1_PLLEN (*DANUBE_CGU_PLL_SR1 & (1 << 0))
151 #define CGU_PLL_SR1_DSMSEL 1
152 #define CGU_PLL_SR1_PHASE_DIV_EN 1
155 * CGU PLL2 Status Register
157 #define CGU_PLL_SR2_PLLDIV ((*DANUBE_CGU_PLL_SR2 & (0xFFFFFFFF ^ ((1 << 28) - 1))) >> 28)
158 #define CGU_PLL_SR2_PLLDEN (*DANUBE_CGU_PLL_SR2 & (1 << 27))
159 #define CGU_PLL_SR2_PLLN GET_BITS(*DANUBE_CGU_PLL_SR2, 25, 20)
160 #define CGU_PLL_SR2_PLLM GET_BITS(*DANUBE_CGU_PLL_SR2, 19, 16)
161 #define CGU_PLL_SR2_PLLPS (*DANUBE_CGU_PLL_SR2 & (1 << 5))
162 #define CGU_PLL_SR2_PLLPE (*DANUBE_CGU_PLL_SR2 & (1 << 4))
163 #define CGU_PLL_SR2_PLLB (*DANUBE_CGU_PLL_SR2 & (1 << 2))
164 #define CGU_PLL_SR2_PLLL (*DANUBE_CGU_PLL_SR2 & (1 << 1))
165 #define CGU_PLL_SR2_PLLEN (*DANUBE_CGU_PLL_SR2 & (1 << 0))
168 * CGU Interface Clock Register
170 #define CGU_IF_CLK_CLKOD0 GET_BITS(*DANUBE_CGU_IF_CLK, 27, 26)
171 #define CGU_IF_CLK_CLKOD1 GET_BITS(*DANUBE_CGU_IF_CLK, 25, 24)
172 #define CGU_IF_CLK_CLKOD2 GET_BITS(*DANUBE_CGU_IF_CLK, 23, 22)
173 #define CGU_IF_CLK_CLKOD3 GET_BITS(*DANUBE_CGU_IF_CLK, 21, 20)
174 #define CGU_IF_CLK_PDA (*DANUBE_CGU_IF_CLK & (1 << 18))
175 #define CGU_IF_CLK_PCI_B (*DANUBE_CGU_IF_CLK & (1 << 17))
176 #define CGU_IF_CLK_PCIBM (*DANUBE_CGU_IF_CLK & (1 << 16))
177 #define CGU_IF_CLK_MIICS (*DANUBE_CGU_IF_CLK & (1 << 3))
178 #define CGU_IF_CLK_USBCS (*DANUBE_CGU_IF_CLK & (1 << 2))
179 #define CGU_IF_CLK_PCIF (*DANUBE_CGU_IF_CLK & (1 << 1))
180 #define CGU_IF_CLK_PCIS (*DANUBE_CGU_IF_CLK & (1 << 0))
183 * CGU Oscillator Control Register
185 #define CGU_OSC_CTRL GET_BITS(*DANUBE_CGU_OSC_CTRL, 1, 0)
188 * CGU SDRAM Memory Delay Register
190 #define CGU_SMD_CLKI (*DANUBE_CGU_SMD & (1 << 31))
191 #define CGU_SMD_MIDS GET_BITS(*DANUBE_CGU_SMD, 17, 12)
192 #define CGU_SMD_MODS GET_BITS(*DANUBE_CGU_SMD, 11, 6)
193 #define CGU_SMD_MDSEL GET_BITS(*DANUBE_CGU_SMD, 5, 0)
196 * CGU CPU Clock Reduction Register
198 #define CGU_CRD_SFTR (*DANUBE_CGU_CRD & (1 << 31))
199 #define CGU_CRD_DIVE (*DANUBE_CGU_CRD & (1 << 16))
200 #define CGU_CRD_CRD1 GET_BITS(*DANUBE_CGU_CRD, 3, 2)
201 #define CGU_CRD_CRD GET_BITS(*DANUBE_CGU_CRD, 1, 0)
204 * CGU CT Status Register 1
206 #define CGU_CT1SR_PDOUT GET_BITS(*DANUBE_CGU_CT1SR, 13, 0)
209 * CGU CT Status Register 2
211 #define CGU_CT2SR_PLL1K GET_BITS(*DANUBE_CGU_CT2SR, 9, 0)
214 * CGU PCM Control Register
216 #define CGU_PCMCR_DCL1 GET_BITS(*DANUBE_CGU_PCMCR, 27, 25)
217 #define CGU_PCMCR_MUXDCL (*DANUBE_CGU_PCMCR & (1 << 22))
218 #define CGU_PCMCR_MUXFSC (*DANUBE_CGU_PCMCR & (1 << 18))
219 #define CGU_PCMCR_PCM_SL (*DANUBE_CGU_PCMCR & (1 << 13))
220 #define CGU_PCMCR_DNTR (*DANUBE_CGU_PCMCR & (1 << 12))
223 * CGU Clock Mux Register
225 #define CGU_MUX_MII_CLK (*DANUBE_CGU_MUX & (1 << 6))
226 #define CGU_MUX_SUB_SYS GET_BITS(*DANUBE_CGU_MUX, 5, 3)
227 #define CGU_MUX_PP32 GET_BITS(*DANUBE_CGU_MUX, 1, 0)
231 * ####################################
232 * Preparation of Debug on Amazon Chip
233 * ####################################
237 * If try module on Amazon chip, prepare some tricks to prevent invalid memory write.
239 #if defined(DEBUG_ON_AMAZON) && DEBUG_ON_AMAZON
240 u32 g_pFakeRegisters
[0x0100];
243 #define DANUBE_CGU ((u32)g_pFakeRegisters)
244 #endif // defined(DEBUG_ON_AMAZON) && DEBUG_ON_AMAZON
248 * ####################################
250 * ####################################
255 * ####################################
257 * ####################################
261 * Pre-declaration of File Operations
263 static ssize_t
cgu_read(struct file
*, char *, size_t, loff_t
*);
264 static ssize_t
cgu_write(struct file
*, const char *, size_t, loff_t
*);
265 static int cgu_ioctl(struct inode
*, struct file
*, unsigned int, unsigned long);
266 static int cgu_open(struct inode
*, struct file
*);
267 static int cgu_release(struct inode
*, struct file
*);
270 * Pre-declaration of 64-bit Unsigned Integer Operation
272 static inline void uint64_multiply(unsigned int, unsigned int, unsigned int *);
273 static inline void uint64_divide(unsigned int *, unsigned int, unsigned int *, unsigned int *);
276 * Calculate PLL Frequency
278 static inline u32
cal_dsm(u32
, u32
);
279 static inline u32
mash_dsm(u32
, u32
, u32
);
280 static inline u32
ssff_dsm_1(u32
, u32
, u32
);
281 static inline u32
ssff_dsm_2(u32
, u32
, u32
);
282 static inline u32
dsm(u32 M
, u32
, u32
, int, int);
283 static inline u32
cgu_get_pll0_fosc(void);
284 static inline u32
cgu_get_pll0_fps(void);
285 static inline u32
cgu_get_pll0_fdiv(void);
286 static inline u32
cgu_get_pll1_fosc(void);
287 static inline u32
cgu_get_pll1_fps(void);
288 static inline u32
cgu_get_pll1_fdiv(void);
289 static inline u32
cgu_get_pll2_fosc(void);
290 static inline u32
cgu_get_pll2_fps(void);
295 u32
cgu_get_mips_clock(int);
296 u32
cgu_get_cpu_clock(void);
297 u32
cgu_get_io_region_clock(void);
298 u32
cgu_get_fpi_bus_clock(int);
299 u32
cgu_get_pp32_clock(void);
300 u32
cgu_get_pci_clock(void);
301 u32
cgu_get_ethernet_clock(void);
302 u32
cgu_get_usb_clock(void);
303 u32
cgu_get_clockout(int);
307 * ####################################
309 * ####################################
312 static struct file_operations cgu_fops
= {
322 static struct miscdevice cgu_miscdev
= {
330 * ####################################
332 * ####################################
337 * ####################################
339 * ####################################
342 static ssize_t
cgu_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ppos
)
347 static ssize_t
cgu_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
352 static int cgu_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
355 struct cgu_clock_rates rates
;
357 if ( _IOC_TYPE(cmd
) != CGU_IOC_MAGIC
358 || _IOC_NR(cmd
) >= CGU_IOC_MAXNR
)
361 if ( _IOC_DIR(cmd
) & _IOC_READ
)
362 ret
= !access_ok(VERIFY_WRITE
, arg
, _IOC_SIZE(cmd
));
363 else if ( _IOC_DIR(cmd
) & _IOC_WRITE
)
364 ret
= !access_ok(VERIFY_READ
, arg
, _IOC_SIZE(cmd
));
370 case CGU_GET_CLOCK_RATES
:
371 /* Calculate Clock Rates */
372 rates
.mips0
= cgu_get_mips_clock(0);
373 rates
.mips1
= cgu_get_mips_clock(1);
374 rates
.cpu
= cgu_get_cpu_clock();
375 rates
.io_region
= cgu_get_io_region_clock();
376 rates
.fpi_bus1
= cgu_get_fpi_bus_clock(1);
377 rates
.fpi_bus2
= cgu_get_fpi_bus_clock(2);
378 rates
.pp32
= cgu_get_pp32_clock();
379 rates
.pci
= cgu_get_pci_clock();
380 rates
.ethernet
= cgu_get_ethernet_clock();
381 rates
.usb
= cgu_get_usb_clock();
382 rates
.clockout0
= cgu_get_clockout(0);
383 rates
.clockout1
= cgu_get_clockout(1);
384 rates
.clockout2
= cgu_get_clockout(2);
385 rates
.clockout3
= cgu_get_clockout(3);
386 /* Copy to User Space */
387 copy_to_user((char*)arg
, (char*)&rates
, sizeof(rates
));
398 static int cgu_open(struct inode
*inode
, struct file
*file
)
403 static int cgu_release(struct inode
*inode
, struct file
*file
)
410 * calculate 64-bit multiplication result of two 32-bit unsigned integer
412 * u32Multiplier1 --- u32 (32-bit), one of the multipliers
413 * u32Multiplier2 --- u32 (32-bit), the other multiplier
414 * u32Result --- u32[2], array to retrieve the multiplication result,
415 * index 0 is high word, index 1 is low word
419 static inline void uint64_multiply(u32 u32Multiplier1
, u32 u32Multiplier2
, u32 u32Result
[2])
421 u32 u32Multiplier1LowWord
= u32Multiplier1
& 0xFFFF;
422 u32 u32Multiplier1HighWord
= u32Multiplier1
>> 16;
423 u32 u32Multiplier2LowWord
= u32Multiplier2
& 0xFFFF;
424 u32 u32Multiplier2HighWord
= u32Multiplier2
>> 16;
425 u32 u32Combo1
, u32Combo2
, u32Combo3
, u32Combo4
;
426 u32 u32Word1
, u32Word2
, u32Word3
, u32Word4
;
428 u32Combo1
= u32Multiplier1LowWord
* u32Multiplier2LowWord
;
429 u32Combo2
= u32Multiplier1HighWord
* u32Multiplier2LowWord
;
430 u32Combo3
= u32Multiplier1LowWord
* u32Multiplier2HighWord
;
431 u32Combo4
= u32Multiplier1HighWord
* u32Multiplier2HighWord
;
433 u32Word1
= u32Combo1
& 0xFFFF;
434 u32Word2
= (u32Combo1
>> 16) + (u32Combo2
& 0xFFFF) + (u32Combo3
& 0xFFFF);
435 u32Word3
= (u32Combo2
>> 16) + (u32Combo3
>> 16) + (u32Combo4
& 0xFFFF) + (u32Word2
>> 16);
436 u32Word4
= (u32Combo4
>> 16) + (u32Word3
>> 16);
438 u32Result
[0] = (u32Word4
<< 16) | u32Word3
;
439 u32Result
[1] = (u32Word2
<< 16) | u32Word1
;
444 * divide 64-bit unsigned integer with 32-bit unsigned integer
446 * u32Numerator --- u32[2], index 0 is high word of numerator, while
447 * index 1 is low word of numerator
448 * u32Denominator --- u32 (32-bit), the denominator in division, this
449 * parameter can not be zero, or lead to unpredictable
451 * pu32Quotient --- u32 *, the pointer to retrieve 32-bit quotient, null
452 * pointer means ignore quotient
453 * pu32Residue --- u32 *, the pointer to retrieve 32-bit residue null
454 * pointer means ignore residue
458 static inline void uint64_divide(u32 u32Numerator
[2], u32 u32Denominator
, u32
*pu32Quotient
, u32
*pu32Residue
)
460 u32 u32DWord1
, u32DWord2
, u32DWord3
;
465 u32DWord2
= u32Numerator
[0];
466 u32DWord1
= u32Numerator
[1];
470 for ( i
= 0; i
< 64; i
++ )
472 u32DWord3
= (u32DWord3
<< 1) | (u32DWord2
>> 31);
473 u32DWord2
= (u32DWord2
<< 1) | (u32DWord1
>> 31);
476 if ( u32DWord3
>= u32Denominator
)
478 u32DWord3
-= u32Denominator
;
483 *pu32Quotient
= u32Quotient
;
485 *pu32Residue
= u32DWord3
;
490 * common routine to calculate PLL frequency
492 * num --- u32, numerator
493 * den --- u32, denominator
495 * u32 --- frequency the PLL output
497 static inline u32
cal_dsm(u32 num
, u32 den
)
503 uint64_multiply(num
, BASIC_INPUT_CLOCK_FREQUENCY
, temp
);
504 uint64_divide(temp
, den
, &ret
, &residue
);
505 if ( (residue
<< 1) >= den
)
513 * calculate PLL frequency following MASH-DSM
515 * M --- u32, denominator coefficient
516 * N --- u32, numerator integer coefficient
517 * K --- u32, numerator fraction coefficient
519 * u32 --- frequency the PLL output
521 static inline u32
mash_dsm(u32 M
, u32 N
, u32 K
)
523 u32 num
= ((N
+ 1) << 10) + K
;
524 u32 den
= (M
+ 1) << 10;
526 return cal_dsm(num
, den
);
531 * calculate PLL frequency following SSFF-DSM (0.25 < fraction < 0.75)
533 * M --- u32, denominator coefficient
534 * N --- u32, numerator integer coefficient
535 * K --- u32, numerator fraction coefficient
537 * u32 --- frequency the PLL output
539 static inline u32
ssff_dsm_1(u32 M
, u32 N
, u32 K
)
541 u32 num
= ((N
+ 1) << 11) + K
+ 512;
542 u32 den
= (M
+ 1) << 11;
544 return cal_dsm(num
, den
);
549 * calculate PLL frequency following SSFF-DSM
550 * (fraction < 0.125 || fraction > 0.875)
552 * M --- u32, denominator coefficient
553 * N --- u32, numerator integer coefficient
554 * K --- u32, numerator fraction coefficient
556 * u32 --- frequency the PLL output
558 static inline u32
ssff_dsm_2(u32 M
, u32 N
, u32 K
)
560 u32 num
= K
>= 512 ? ((N
+ 1) << 12) + K
- 512 : ((N
+ 1) << 12) + K
+ 3584;
561 u32 den
= (M
+ 1) << 12;
563 return cal_dsm(num
, den
);
568 * calculate PLL frequency
570 * M --- u32, denominator coefficient
571 * N --- u32, numerator integer coefficient
572 * K --- u32, numerator fraction coefficient
573 * dsmsel --- int, 0: MASH-DSM, 1: SSFF-DSM
574 * phase_div_en --- int, 0: 0.25 < fraction < 0.75
575 * 1: fraction < 0.125 || fraction > 0.875
577 * u32 --- frequency the PLL output
579 static inline u32
dsm(u32 M
, u32 N
, u32 K
, int dsmsel
, int phase_div_en
)
582 return mash_dsm(M
, N
, K
);
585 return ssff_dsm_1(M
, N
, K
);
587 return ssff_dsm_2(M
, N
, K
);
592 * get oscillate frequency of PLL0
596 * u32 --- frequency of PLL0 Fosc
598 static inline u32
cgu_get_pll0_fosc(void)
600 return CGU_PLL_SR0_PLLB
? BASIC_INPUT_CLOCK_FREQUENCY
: dsm(CGU_PLL_NMK0_PLLM
, CGU_PLL_NMK0_PLLN
, CGU_PLL_NMK0_PLLK
, CGU_PLL_SR0_DSMSEL
, CGU_PLL_SR0_PHASE_DIV_EN
);
605 * get output frequency of PLL0 phase shifter
609 * u32 --- frequency of PLL0 Fps
611 static inline u32
cgu_get_pll0_fps(void)
613 register u32 fps
= cgu_get_pll0_fosc();
615 switch ( CGU_PLL_SR0_PLLPSE
)
619 fps
= ((fps
<< 1) + 1) / 3; break;
622 fps
= ((fps
<< 2) + 2) / 5; break;
625 fps
= ((fps
<< 1) + 3) / 7;
632 * get output frequency of PLL0 output divider
636 * u32 --- frequency of PLL0 Fdiv
638 static inline u32
cgu_get_pll0_fdiv(void)
640 register u32 fdiv
= cgu_get_pll0_fosc();
642 if ( CGU_PLL_SR0_PLLDEN
)
643 fdiv
= (fdiv
+ (CGU_PLL_SR0_PLLDIV
+ 1) / 2) / (CGU_PLL_SR0_PLLDIV
+ 1);
649 * get oscillate frequency of PLL1
653 * u32 --- frequency of PLL1 Fosc
655 static inline u32
cgu_get_pll1_fosc(void)
657 return CGU_PLL_SR1_PLLB
? BASIC_INPUT_CLOCK_FREQUENCY
: dsm(CGU_PLL_NMK1_PLLM
, CGU_PLL_NMK1_PLLN
, CGU_PLL_NMK1_PLLK
, CGU_PLL_SR1_DSMSEL
, CGU_PLL_SR1_PHASE_DIV_EN
);
662 * get output frequency of PLL1 phase shifter
666 * u32 --- frequency of PLL1 Fps
668 static inline u32
cgu_get_pll1_fps(void)
670 register u32 fps
= cgu_get_pll1_fosc();
672 switch ( CGU_PLL_SR1_PLLPSE
)
676 fps
= ((fps
<< 1) + 1) / 3; break;
679 fps
= ((fps
<< 2) + 2) / 5; break;
682 fps
= ((fps
<< 1) + 3) / 7;
689 * get output frequency of PLL1 output divider
693 * u32 --- frequency of PLL1 Fdiv
695 static inline u32
cgu_get_pll1_fdiv(void)
697 register u32 fdiv
= cgu_get_pll1_fosc();
699 if ( CGU_PLL_SR1_PLLDEN
)
700 fdiv
= (fdiv
+ (CGU_PLL_SR1_PLLDIV
+ 1) / 2) / (CGU_PLL_SR1_PLLDIV
+ 1);
706 * get oscillate frequency of PLL2
710 * u32 --- frequency of PLL2 Fosc
712 static inline u32
cgu_get_pll2_fosc(void)
718 uint64_multiply((CGU_PLL_SR2_PLLN
+ 1) * 8, cgu_get_pll0_fdiv(), temp
);
719 uint64_divide(temp
, CGU_PLL_SR2_PLLM
+ 1, &ret
, &residue
);
720 if ( (residue
<< 1) >= CGU_PLL_SR2_PLLM
)
728 * get output frequency of PLL2 phase shifter
732 * u32 --- frequency of PLL2 Fps
734 static inline u32
cgu_get_pll2_fps(void)
736 register u32 fps
= cgu_get_pll2_fosc();
738 if ( CGU_PLL_SR2_PLLPE
)
740 if ( CGU_PLL_SR2_PLLPS
)
742 fps
= ((fps
<< 3) + 4) / 9;
745 fps
= ((fps
<< 2) + 2) / 5;
753 * ####################################
755 * ####################################
760 * get frequency of MIPS (0: core, 1: DSP)
762 * cpu --- int, 0: core, 1: DSP
764 * u32 --- frequency of MIPS coprocessor (0: core, 1: DSP)
766 u32
cgu_get_mips_clock(int cpu
)
768 register u32 ret
= cgu_get_pll0_fosc();
771 ret
= (ret
+ (CGU_CRD_CRD
>> 1)) / (CGU_CRD_CRD
+ 1);
772 if ( cpu
== 0 && CGU_CRD_CRD1
)
773 ret
>>= CGU_CRD_CRD1
;
779 * get frequency of MIPS core
783 * u32 --- frequency of MIPS core
785 u32
cgu_get_cpu_clock(void)
787 return cgu_get_mips_clock(0);
792 * get frequency of sub-system and memory controller
796 * u32 --- frequency of sub-system and memory controller
798 u32
cgu_get_io_region_clock(void)
800 register u32 ret
= (CGU_MUX_SUB_SYS
> 4) ? cgu_get_pll0_fosc() : cgu_get_mips_clock(1);
802 switch ( CGU_MUX_SUB_SYS
)
808 ret
= (ret
+ 1) >> 1; break;
810 ret
= (ret
+ 1) / 3; break;
812 ret
= (ret
+ 2) >> 2; break;
814 ret
= ((ret
<< 1) + 1) / 3; break;
816 ret
= ((ret
<< 1) + 2) / 5;
824 * get frequency of FPI bus
826 * fpi --- int, 1: FPI bus 1 (FBS1/Fast FPI Bus), 2: FPI bus 2 (FBS2)
828 * u32 --- frequency of FPI bus
830 u32
cgu_get_fpi_bus_clock(int fpi
)
832 register u32 ret
= cgu_get_io_region_clock();
841 * get frequency of PP32 processor
845 * u32 --- frequency of PP32 processor
847 u32
cgu_get_pp32_clock(void)
851 switch ( CGU_MUX_PP32
)
855 ret
= ((cgu_get_pll2_fosc() << 2) + 2) / 5; break;
857 ret
= ((cgu_get_pll2_fosc() << 3) + 4) / 9; break;
859 ret
= cgu_get_fpi_bus_clock(1); break;
861 ret
= cgu_get_mips_clock(1);
869 * get frequency of PCI bus
873 * u32 --- frequency of PCI bus
875 u32
cgu_get_pci_clock(void)
877 register u32 ret
= 0;
879 if ( !CGU_IF_CLK_PCIS
)
881 ret
= cgu_get_pll2_fosc();
882 if ( CGU_IF_CLK_PCIF
)
893 * get frequency of ethernet module (MII)
897 * u32 --- frequency of ethernet module
899 u32
cgu_get_ethernet_clock(void)
901 register u32 ret
= 0;
903 if ( !CGU_IF_CLK_MIICS
)
905 ret
= cgu_get_pll2_fosc();
906 if ( CGU_MUX_MII_CLK
)
909 ret
= (ret
+ 6) / 12;
917 * get frequency of USB
921 * u32 --- frequency of USB
923 u32
cgu_get_usb_clock(void)
925 return CGU_IF_CLK_USBCS
? 12000000 : (cgu_get_pll2_fosc() + 12) / 25;
930 * get frequency of CLK_OUT pin
932 * clkout --- int, clock out pin number
934 * u32 --- frequency of CLK_OUT pin
936 u32
cgu_get_clockout(int clkout
)
938 u32 fosc1
= cgu_get_pll1_fosc();
939 u32 fosc2
= cgu_get_pll2_fosc();
941 if ( clkout
> 3 || clkout
< 0 )
944 switch ( ((u32
)clkout
<< 2) | GET_BITS(*DANUBE_CGU_IF_CLK
, 21 + clkout
* 2, 20 + clkout
* 2) )
946 case 0: /* 32.768KHz */
948 return (fosc1
+ 6000) / 12000;
949 case 1: /* 1.536MHz */
950 return (fosc1
+ 128) / 256;
952 return (fosc2
+ 60) / 120;
956 return (fosc2
+ 12) / 25;
958 return (fosc2
* 2 + 7) / 15;
960 return (fosc2
* 2 + 12) / 25;
962 return (fosc2
* 4 + 12) / 25;
965 return (fosc2
+ 6) / 12;
968 return (fosc2
+ 3) / 6;
970 return (fosc2
+ 5) / 10;
972 return (fosc2
+ 2) / 5;
980 * ####################################
982 * ####################################
992 * else --- failure, usually it is negative value of error code
994 int __init
danube_cgu_init(void)
998 ret
= misc_register(&cgu_miscdev
);
1001 printk(KERN_ERR
"cgu: can't misc_register\n");
1005 printk(KERN_INFO
"cgu: misc_register on minor = %d\n", cgu_miscdev
.minor
);
1008 * initialize fake registers to do testing on Amazon
1010 #if defined(DEBUG_ON_AMAZON) && DEBUG_ON_AMAZON
1011 #ifdef DEBUG_PRINT_INFO
1012 #undef DEBUG_PRINT_INFO
1014 #define DEBUG_PRINT_INFO 1
1016 *DANUBE_CGU_DIV
= 0x00010019;
1017 *DANUBE_CGU_PLL_NMK0
= 0x416002C3;
1018 *DANUBE_CGU_PLL_SR0
= 0x74000013;
1019 *DANUBE_CGU_PLL_NMK1
= 0x4C60009C;
1020 *DANUBE_CGU_PLL_SR1
= 0x54000013;
1021 *DANUBE_CGU_PLL_SR2
= 0x58890013;
1022 *DANUBE_CGU_IF_CLK
= 0x00000000;
1023 *DANUBE_CGU_OSC_CTRL
= 0x00000000;
1024 *DANUBE_CGU_SMD
= 0x00000000;
1025 *DANUBE_CGU_CRD
= 0x00010000;
1026 *DANUBE_CGU_CT1SR
= 0x00000000;
1027 *DANUBE_CGU_CT2SR
= CGU_PLL_NMK1_PLLK
;
1028 *DANUBE_CGU_PCMCR
= 0x00000000;
1029 *DANUBE_CGU_MUX
= 0x00000008;
1030 #endif // defined(DEBUG_ON_AMAZON) && DEBUG_ON_AMAZON
1035 #if defined(DEBUG_PRINT_INFO) && DEBUG_PRINT_INFO
1036 printk("pll0 N = %d, M = %d, K = %d, DIV = %d\n", CGU_PLL_NMK0_PLLN
, CGU_PLL_NMK0_PLLM
, CGU_PLL_NMK0_PLLK
, CGU_PLL_SR0_PLLDIV
);
1037 printk("pll1 N = %d, M = %d, K = %d, DIV = %d\n", CGU_PLL_NMK1_PLLN
, CGU_PLL_NMK1_PLLM
, CGU_PLL_NMK1_PLLK
, CGU_PLL_SR1_PLLDIV
);
1038 printk("pll2 N = %d, M = %d, DIV = %d\n", CGU_PLL_SR2_PLLN
, CGU_PLL_SR2_PLLM
, CGU_PLL_SR2_PLLDIV
);
1039 printk("pll0_fosc = %d\n", cgu_get_pll0_fosc());
1040 printk("pll0_fps = %d\n", cgu_get_pll0_fps());
1041 printk("pll0_fdiv = %d\n", cgu_get_pll0_fdiv());
1042 printk("pll1_fosc = %d\n", cgu_get_pll1_fosc());
1043 printk("pll1_fps = %d\n", cgu_get_pll1_fps());
1044 printk("pll1_fdiv = %d\n", cgu_get_pll1_fdiv());
1045 printk("pll2_fosc = %d\n", cgu_get_pll2_fosc());
1046 printk("pll2_fps = %d\n", cgu_get_pll2_fps());
1047 printk("mips0 clock = %d\n", cgu_get_mips_clock(0));
1048 printk("mips1 clock = %d\n", cgu_get_mips_clock(1));
1049 printk("cpu clock = %d\n", cgu_get_cpu_clock());
1050 printk("IO region = %d\n", cgu_get_io_region_clock());
1051 printk("FPI bus 1 = %d\n", cgu_get_fpi_bus_clock(1));
1052 printk("FPI bus 2 = %d\n", cgu_get_fpi_bus_clock(2));
1053 printk("PP32 clock = %d\n", cgu_get_pp32_clock());
1054 printk("PCI clock = %d\n", cgu_get_pci_clock());
1055 printk("Ethernet = %d\n", cgu_get_ethernet_clock());
1056 printk("USB clock = %d\n", cgu_get_usb_clock());
1057 printk("Clockout0 = %d\n", cgu_get_clockout(0));
1058 printk("Clockout1 = %d\n", cgu_get_clockout(1));
1059 printk("Clockout2 = %d\n", cgu_get_clockout(2));
1060 printk("Clockout3 = %d\n", cgu_get_clockout(3));
1061 #endif // defined(DEBUG_PRINT_INFO) && DEBUG_PRINT_INFO
1074 void __exit
danube_cgu_exit(void)
1078 ret
= misc_deregister(&cgu_miscdev
);
1080 printk(KERN_ERR
"cgu: can't misc_deregister, get error number %d\n", -ret
);
1082 printk(KERN_INFO
"cgu: misc_deregister successfully\n");
1085 module_init(danube_cgu_init
);
1086 module_exit(danube_cgu_exit
);