2 * Copyright (C) 2006 - 2008 Lemote Inc. & Institute 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/list.h>
14 #include <linux/mutex.h>
15 #include <linux/spinlock.h>
17 #include <asm/clock.h>
18 #include <asm/mach-loongson64/loongson.h>
20 static LIST_HEAD(clock_list
);
21 static DEFINE_SPINLOCK(clock_lock
);
22 static DEFINE_MUTEX(clock_list_sem
);
24 /* Minimum CLK support */
26 DC_ZERO
, DC_25PT
= 2, DC_37PT
, DC_50PT
, DC_62PT
, DC_75PT
,
27 DC_87PT
, DC_DISABLE
, DC_RESV
30 struct cpufreq_frequency_table loongson2_clockmod_table
[] = {
31 {0, DC_RESV
, CPUFREQ_ENTRY_INVALID
},
32 {0, DC_ZERO
, CPUFREQ_ENTRY_INVALID
},
40 {0, DC_RESV
, CPUFREQ_TABLE_END
},
42 EXPORT_SYMBOL_GPL(loongson2_clockmod_table
);
44 static struct clk cpu_clk
= {
46 .flags
= CLK_ALWAYS_ENABLED
| CLK_RATE_PROPAGATES
,
50 struct clk
*clk_get(struct device
*dev
, const char *id
)
54 EXPORT_SYMBOL(clk_get
);
56 static void propagate_rate(struct clk
*clk
)
60 list_for_each_entry(clkp
, &clock_list
, node
) {
61 if (likely(clkp
->parent
!= clk
))
63 if (likely(clkp
->ops
&& clkp
->ops
->recalc
))
64 clkp
->ops
->recalc(clkp
);
65 if (unlikely(clkp
->flags
& CLK_RATE_PROPAGATES
))
70 int clk_enable(struct clk
*clk
)
74 EXPORT_SYMBOL(clk_enable
);
76 void clk_disable(struct clk
*clk
)
79 EXPORT_SYMBOL(clk_disable
);
81 unsigned long clk_get_rate(struct clk
*clk
)
86 return (unsigned long)clk
->rate
;
88 EXPORT_SYMBOL(clk_get_rate
);
90 void clk_put(struct clk
*clk
)
93 EXPORT_SYMBOL(clk_put
);
95 int clk_set_rate(struct clk
*clk
, unsigned long rate
)
97 unsigned int rate_khz
= rate
/ 1000;
98 struct cpufreq_frequency_table
*pos
;
102 if (likely(clk
->ops
&& clk
->ops
->set_rate
)) {
105 spin_lock_irqsave(&clock_lock
, flags
);
106 ret
= clk
->ops
->set_rate(clk
, rate
, 0);
107 spin_unlock_irqrestore(&clock_lock
, flags
);
110 if (unlikely(clk
->flags
& CLK_RATE_PROPAGATES
))
113 cpufreq_for_each_valid_entry(pos
, loongson2_clockmod_table
)
114 if (rate_khz
== pos
->frequency
)
116 if (rate_khz
!= pos
->frequency
)
121 regval
= LOONGSON_CHIPCFG(0);
122 regval
= (regval
& ~0x7) | (pos
->driver_data
- 1);
123 LOONGSON_CHIPCFG(0) = regval
;
127 EXPORT_SYMBOL_GPL(clk_set_rate
);
129 long clk_round_rate(struct clk
*clk
, unsigned long rate
)
131 if (likely(clk
->ops
&& clk
->ops
->round_rate
)) {
132 unsigned long flags
, rounded
;
134 spin_lock_irqsave(&clock_lock
, flags
);
135 rounded
= clk
->ops
->round_rate(clk
, rate
);
136 spin_unlock_irqrestore(&clock_lock
, flags
);
143 EXPORT_SYMBOL_GPL(clk_round_rate
);