2 * Copyright (C) 2006 - 2008 Lemote Inc. & Insititute of Computing Technology
3 * Author: Yanhua, yanh@lemote.com
5 * This file is subject to the terms and conditions of the GNU General Public
6 * License. See the file "COPYING" in the main directory of this archive
10 #include <linux/cpufreq.h>
11 #include <linux/errno.h>
12 #include <linux/export.h>
13 #include <linux/init.h>
14 #include <linux/list.h>
15 #include <linux/mutex.h>
16 #include <linux/spinlock.h>
18 #include <asm/clock.h>
19 #include <asm/mach-loongson/loongson.h>
21 static LIST_HEAD(clock_list
);
22 static DEFINE_SPINLOCK(clock_lock
);
23 static DEFINE_MUTEX(clock_list_sem
);
25 /* Minimum CLK support */
27 DC_ZERO
, DC_25PT
= 2, DC_37PT
, DC_50PT
, DC_62PT
, DC_75PT
,
28 DC_87PT
, DC_DISABLE
, DC_RESV
31 struct cpufreq_frequency_table loongson2_clockmod_table
[] = {
32 {DC_RESV
, CPUFREQ_ENTRY_INVALID
},
33 {DC_ZERO
, CPUFREQ_ENTRY_INVALID
},
41 {DC_RESV
, CPUFREQ_TABLE_END
},
43 EXPORT_SYMBOL_GPL(loongson2_clockmod_table
);
45 static struct clk cpu_clk
= {
47 .flags
= CLK_ALWAYS_ENABLED
| CLK_RATE_PROPAGATES
,
51 struct clk
*clk_get(struct device
*dev
, const char *id
)
55 EXPORT_SYMBOL(clk_get
);
57 static void propagate_rate(struct clk
*clk
)
61 list_for_each_entry(clkp
, &clock_list
, node
) {
62 if (likely(clkp
->parent
!= clk
))
64 if (likely(clkp
->ops
&& clkp
->ops
->recalc
))
65 clkp
->ops
->recalc(clkp
);
66 if (unlikely(clkp
->flags
& CLK_RATE_PROPAGATES
))
71 int clk_enable(struct clk
*clk
)
75 EXPORT_SYMBOL(clk_enable
);
77 void clk_disable(struct clk
*clk
)
80 EXPORT_SYMBOL(clk_disable
);
82 unsigned long clk_get_rate(struct clk
*clk
)
84 return (unsigned long)clk
->rate
;
86 EXPORT_SYMBOL(clk_get_rate
);
88 void clk_put(struct clk
*clk
)
91 EXPORT_SYMBOL(clk_put
);
93 int clk_set_rate(struct clk
*clk
, unsigned long rate
)
99 if (likely(clk
->ops
&& clk
->ops
->set_rate
)) {
102 spin_lock_irqsave(&clock_lock
, flags
);
103 ret
= clk
->ops
->set_rate(clk
, rate
, 0);
104 spin_unlock_irqrestore(&clock_lock
, flags
);
107 if (unlikely(clk
->flags
& CLK_RATE_PROPAGATES
))
110 for (i
= 0; loongson2_clockmod_table
[i
].frequency
!= CPUFREQ_TABLE_END
;
112 if (loongson2_clockmod_table
[i
].frequency
==
113 CPUFREQ_ENTRY_INVALID
)
115 if (rate
== loongson2_clockmod_table
[i
].frequency
)
118 if (rate
!= loongson2_clockmod_table
[i
].frequency
)
123 regval
= LOONGSON_CHIPCFG0
;
124 regval
= (regval
& ~0x7) | (loongson2_clockmod_table
[i
].index
- 1);
125 LOONGSON_CHIPCFG0
= regval
;
129 EXPORT_SYMBOL_GPL(clk_set_rate
);
131 long clk_round_rate(struct clk
*clk
, unsigned long rate
)
133 if (likely(clk
->ops
&& clk
->ops
->round_rate
)) {
134 unsigned long flags
, rounded
;
136 spin_lock_irqsave(&clock_lock
, flags
);
137 rounded
= clk
->ops
->round_rate(clk
, rate
);
138 spin_unlock_irqrestore(&clock_lock
, flags
);
145 EXPORT_SYMBOL_GPL(clk_round_rate
);