Skip to content

Commit f8ede0f

Browse files
lzufalconralfbaechle
authored andcommitted
MIPS: Loongson 2F: Add CPU frequency scaling support
Loongson 2F supports CPU clock scaling. When put it into wait mode by setting the frequency as ZERO it will stay in this mode until an external interrupt wakes the CPU again. To enable clock scaling support, an external timer of a known stable rate is required. Signed-off-by: Wu Zhangjin <wuzhangjin@gmail.com> Cc: linux-mips@linux-mips.org Cc: cpufreq@vger.kernel.org, Cc: Dave Jones <davej@redhat.com>, Cc: Dominik Brodowski <linux@dominikbrodowski.net>, Cc: yanh@lemote.com Cc: huhb@lemote.com, Patchwork: http://patchwork.linux-mips.org/patch/660/ Patchwork: http://patchwork.linux-mips.org/patch/751/ Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
1 parent 9726b43 commit f8ede0f

File tree

13 files changed

+524
-4
lines changed

13 files changed

+524
-4
lines changed

arch/mips/include/asm/clock.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#ifndef __ASM_MIPS_CLOCK_H
2+
#define __ASM_MIPS_CLOCK_H
3+
4+
#include <linux/kref.h>
5+
#include <linux/list.h>
6+
#include <linux/seq_file.h>
7+
#include <linux/clk.h>
8+
9+
extern void (*cpu_wait) (void);
10+
11+
struct clk;
12+
13+
struct clk_ops {
14+
void (*init) (struct clk *clk);
15+
void (*enable) (struct clk *clk);
16+
void (*disable) (struct clk *clk);
17+
void (*recalc) (struct clk *clk);
18+
int (*set_rate) (struct clk *clk, unsigned long rate, int algo_id);
19+
long (*round_rate) (struct clk *clk, unsigned long rate);
20+
};
21+
22+
struct clk {
23+
struct list_head node;
24+
const char *name;
25+
int id;
26+
struct module *owner;
27+
28+
struct clk *parent;
29+
struct clk_ops *ops;
30+
31+
struct kref kref;
32+
33+
unsigned long rate;
34+
unsigned long flags;
35+
};
36+
37+
#define CLK_ALWAYS_ENABLED (1 << 0)
38+
#define CLK_RATE_PROPAGATES (1 << 1)
39+
40+
/* Should be defined by processor-specific code */
41+
void arch_init_clk_ops(struct clk_ops **, int type);
42+
43+
int clk_init(void);
44+
45+
int __clk_enable(struct clk *);
46+
void __clk_disable(struct clk *);
47+
48+
void clk_recalc_rate(struct clk *);
49+
50+
int clk_register(struct clk *);
51+
void clk_unregister(struct clk *);
52+
53+
/* the exported API, in addition to clk_set_rate */
54+
/**
55+
* clk_set_rate_ex - set the clock rate for a clock source, with additional parameter
56+
* @clk: clock source
57+
* @rate: desired clock rate in Hz
58+
* @algo_id: algorithm id to be passed down to ops->set_rate
59+
*
60+
* Returns success (0) or negative errno.
61+
*/
62+
int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id);
63+
64+
#endif /* __ASM_MIPS_CLOCK_H */

arch/mips/include/asm/cpu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@
154154
#define PRID_REV_VR4181A 0x0070 /* Same as VR4122 */
155155
#define PRID_REV_VR4130 0x0080
156156
#define PRID_REV_34K_V1_0_2 0x0022
157+
#define PRID_REV_LOONGSON2E 0x0002
158+
#define PRID_REV_LOONGSON2F 0x0003
157159

158160
/*
159161
* Older processors used to encode processor version and revision in two

arch/mips/include/asm/mach-loongson/loongson.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,12 @@ extern void mach_irq_dispatch(unsigned int pending);
226226
#define LOONGSON_PCIMAP_WIN(WIN, ADDR) \
227227
((((ADDR)>>26) & LOONGSON_PCIMAP_PCIMAP_LO0) << ((WIN)*6))
228228

229-
/* Chip Config */
230229
#ifdef CONFIG_CPU_SUPPORTS_CPUFREQ
230+
#include <linux/cpufreq.h>
231+
extern void loongson2_cpu_wait(void);
232+
extern struct cpufreq_frequency_table loongson2_clockmod_table[];
233+
234+
/* Chip Config */
231235
#define LOONGSON_CHIPCFG0 LOONGSON_REG(LOONGSON_REGBASE + 0x80)
232236
#endif
233237

arch/mips/kernel/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,6 @@ CFLAGS_cpu-bugs64.o = $(shell if $(CC) $(KBUILD_CFLAGS) -Wa,-mdaddi -c -o /dev/n
9393

9494
obj-$(CONFIG_HAVE_STD_PC_SERIAL_PORT) += 8250-platform.o
9595

96+
obj-$(CONFIG_MIPS_CPUFREQ) += cpufreq/
97+
9698
EXTRA_CFLAGS += -Werror

arch/mips/kernel/cpu-probe.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/ptrace.h>
1717
#include <linux/smp.h>
1818
#include <linux/stddef.h>
19+
#include <linux/module.h>
1920

2021
#include <asm/bugs.h>
2122
#include <asm/cpu.h>
@@ -32,6 +33,7 @@
3233
* the CPU very much.
3334
*/
3435
void (*cpu_wait)(void);
36+
EXPORT_SYMBOL(cpu_wait);
3537

3638
static void r3081_wait(void)
3739
{

arch/mips/kernel/cpufreq/Kconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ if CPU_FREQ
2020

2121
comment "CPUFreq processor drivers"
2222

23+
config LOONGSON2_CPUFREQ
24+
tristate "Loongson2 CPUFreq Driver"
25+
select CPU_FREQ_TABLE
26+
depends on MIPS_CPUFREQ
27+
help
28+
This option adds a CPUFreq driver for loongson processors which
29+
support software configurable cpu frequency.
30+
31+
Loongson2F and it's successors support this feature.
32+
33+
For details, take a look at <file:Documentation/cpu-freq/>.
34+
35+
If in doubt, say N.
36+
2337
endif # CPU_FREQ
2438

2539
endmenu

arch/mips/kernel/cpufreq/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#
2+
# Makefile for the Linux/MIPS cpufreq.
3+
#
4+
5+
obj-$(CONFIG_LOONGSON2_CPUFREQ) += loongson2_cpufreq.o loongson2_clock.o
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* Copyright (C) 2006 - 2008 Lemote Inc. & Insititute of Computing Technology
3+
* Author: Yanhua, yanh@lemote.com
4+
*
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
7+
* for more details.
8+
*/
9+
10+
#include <linux/cpufreq.h>
11+
#include <linux/platform_device.h>
12+
13+
#include <asm/clock.h>
14+
15+
#include <loongson.h>
16+
17+
static LIST_HEAD(clock_list);
18+
static DEFINE_SPINLOCK(clock_lock);
19+
static DEFINE_MUTEX(clock_list_sem);
20+
21+
/* Minimum CLK support */
22+
enum {
23+
DC_ZERO, DC_25PT = 2, DC_37PT, DC_50PT, DC_62PT, DC_75PT,
24+
DC_87PT, DC_DISABLE, DC_RESV
25+
};
26+
27+
struct cpufreq_frequency_table loongson2_clockmod_table[] = {
28+
{DC_RESV, CPUFREQ_ENTRY_INVALID},
29+
{DC_ZERO, CPUFREQ_ENTRY_INVALID},
30+
{DC_25PT, 0},
31+
{DC_37PT, 0},
32+
{DC_50PT, 0},
33+
{DC_62PT, 0},
34+
{DC_75PT, 0},
35+
{DC_87PT, 0},
36+
{DC_DISABLE, 0},
37+
{DC_RESV, CPUFREQ_TABLE_END},
38+
};
39+
EXPORT_SYMBOL_GPL(loongson2_clockmod_table);
40+
41+
static struct clk cpu_clk = {
42+
.name = "cpu_clk",
43+
.flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
44+
.rate = 800000000,
45+
};
46+
47+
struct clk *clk_get(struct device *dev, const char *id)
48+
{
49+
return &cpu_clk;
50+
}
51+
EXPORT_SYMBOL(clk_get);
52+
53+
static void propagate_rate(struct clk *clk)
54+
{
55+
struct clk *clkp;
56+
57+
list_for_each_entry(clkp, &clock_list, node) {
58+
if (likely(clkp->parent != clk))
59+
continue;
60+
if (likely(clkp->ops && clkp->ops->recalc))
61+
clkp->ops->recalc(clkp);
62+
if (unlikely(clkp->flags & CLK_RATE_PROPAGATES))
63+
propagate_rate(clkp);
64+
}
65+
}
66+
67+
int clk_enable(struct clk *clk)
68+
{
69+
return 0;
70+
}
71+
EXPORT_SYMBOL(clk_enable);
72+
73+
void clk_disable(struct clk *clk)
74+
{
75+
}
76+
EXPORT_SYMBOL(clk_disable);
77+
78+
unsigned long clk_get_rate(struct clk *clk)
79+
{
80+
return (unsigned long)clk->rate;
81+
}
82+
EXPORT_SYMBOL(clk_get_rate);
83+
84+
void clk_put(struct clk *clk)
85+
{
86+
}
87+
EXPORT_SYMBOL(clk_put);
88+
89+
int clk_set_rate(struct clk *clk, unsigned long rate)
90+
{
91+
return clk_set_rate_ex(clk, rate, 0);
92+
}
93+
EXPORT_SYMBOL_GPL(clk_set_rate);
94+
95+
int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
96+
{
97+
int ret = 0;
98+
int regval;
99+
int i;
100+
101+
if (likely(clk->ops && clk->ops->set_rate)) {
102+
unsigned long flags;
103+
104+
spin_lock_irqsave(&clock_lock, flags);
105+
ret = clk->ops->set_rate(clk, rate, algo_id);
106+
spin_unlock_irqrestore(&clock_lock, flags);
107+
}
108+
109+
if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
110+
propagate_rate(clk);
111+
112+
for (i = 0; loongson2_clockmod_table[i].frequency != CPUFREQ_TABLE_END;
113+
i++) {
114+
if (loongson2_clockmod_table[i].frequency ==
115+
CPUFREQ_ENTRY_INVALID)
116+
continue;
117+
if (rate == loongson2_clockmod_table[i].frequency)
118+
break;
119+
}
120+
if (rate != loongson2_clockmod_table[i].frequency)
121+
return -ENOTSUPP;
122+
123+
clk->rate = rate;
124+
125+
regval = LOONGSON_CHIPCFG0;
126+
regval = (regval & ~0x7) | (loongson2_clockmod_table[i].index - 1);
127+
LOONGSON_CHIPCFG0 = regval;
128+
129+
return ret;
130+
}
131+
EXPORT_SYMBOL_GPL(clk_set_rate_ex);
132+
133+
long clk_round_rate(struct clk *clk, unsigned long rate)
134+
{
135+
if (likely(clk->ops && clk->ops->round_rate)) {
136+
unsigned long flags, rounded;
137+
138+
spin_lock_irqsave(&clock_lock, flags);
139+
rounded = clk->ops->round_rate(clk, rate);
140+
spin_unlock_irqrestore(&clock_lock, flags);
141+
142+
return rounded;
143+
}
144+
145+
return rate;
146+
}
147+
EXPORT_SYMBOL_GPL(clk_round_rate);
148+
149+
/*
150+
* This is the simple version of Loongson-2 wait, Maybe we need do this in
151+
* interrupt disabled content
152+
*/
153+
154+
DEFINE_SPINLOCK(loongson2_wait_lock);
155+
void loongson2_cpu_wait(void)
156+
{
157+
u32 cpu_freq;
158+
unsigned long flags;
159+
160+
spin_lock_irqsave(&loongson2_wait_lock, flags);
161+
cpu_freq = LOONGSON_CHIPCFG0;
162+
LOONGSON_CHIPCFG0 &= ~0x7; /* Put CPU into wait mode */
163+
LOONGSON_CHIPCFG0 = cpu_freq; /* Restore CPU state */
164+
spin_unlock_irqrestore(&loongson2_wait_lock, flags);
165+
}
166+
EXPORT_SYMBOL_GPL(loongson2_cpu_wait);

0 commit comments

Comments
 (0)