2 * kirkwood_freq.c: cpufreq driver for the Marvell kirkwood
4 * Copyright (C) 2013 Andrew Lunn <andrew@lunn.ch>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/clk.h>
15 #include <linux/clk-provider.h>
16 #include <linux/cpufreq.h>
17 #include <linux/of_device.h>
18 #include <linux/platform_device.h>
20 #include <asm/proc-fns.h>
22 #define CPU_SW_INT_BLK BIT(28)
28 struct clk
*powersave_clk
;
33 #define STATE_CPU_FREQ 0x01
34 #define STATE_DDR_FREQ 0x02
37 * Kirkwood can swap the clock to the CPU between two clocks:
42 * The frequencies are set at runtime before registering this *
45 static struct cpufreq_frequency_table kirkwood_freq_table
[] = {
46 {STATE_CPU_FREQ
, 0}, /* CPU uses cpuclk */
47 {STATE_DDR_FREQ
, 0}, /* CPU uses ddrclk */
48 {0, CPUFREQ_TABLE_END
},
51 static unsigned int kirkwood_cpufreq_get_cpu_frequency(unsigned int cpu
)
53 if (__clk_is_enabled(priv
.powersave_clk
))
54 return kirkwood_freq_table
[1].frequency
;
55 return kirkwood_freq_table
[0].frequency
;
58 static void kirkwood_cpufreq_set_cpu_state(struct cpufreq_policy
*policy
,
61 struct cpufreq_freqs freqs
;
62 unsigned int state
= kirkwood_freq_table
[index
].driver_data
;
65 freqs
.old
= kirkwood_cpufreq_get_cpu_frequency(0);
66 freqs
.new = kirkwood_freq_table
[index
].frequency
;
68 cpufreq_notify_transition(policy
, &freqs
, CPUFREQ_PRECHANGE
);
70 dev_dbg(priv
.dev
, "Attempting to set frequency to %i KHz\n",
71 kirkwood_freq_table
[index
].frequency
);
72 dev_dbg(priv
.dev
, "old frequency was %i KHz\n",
73 kirkwood_cpufreq_get_cpu_frequency(0));
75 if (freqs
.old
!= freqs
.new) {
78 /* Disable interrupts to the CPU */
79 reg
= readl_relaxed(priv
.base
);
80 reg
|= CPU_SW_INT_BLK
;
81 writel_relaxed(reg
, priv
.base
);
85 clk_disable(priv
.powersave_clk
);
88 clk_enable(priv
.powersave_clk
);
92 /* Wait-for-Interrupt, while the hardware changes frequency */
95 /* Enable interrupts to the CPU */
96 reg
= readl_relaxed(priv
.base
);
97 reg
&= ~CPU_SW_INT_BLK
;
98 writel_relaxed(reg
, priv
.base
);
102 cpufreq_notify_transition(policy
, &freqs
, CPUFREQ_POSTCHANGE
);
105 static int kirkwood_cpufreq_verify(struct cpufreq_policy
*policy
)
107 return cpufreq_frequency_table_verify(policy
, kirkwood_freq_table
);
110 static int kirkwood_cpufreq_target(struct cpufreq_policy
*policy
,
111 unsigned int target_freq
,
112 unsigned int relation
)
114 unsigned int index
= 0;
116 if (cpufreq_frequency_table_target(policy
, kirkwood_freq_table
,
117 target_freq
, relation
, &index
))
120 kirkwood_cpufreq_set_cpu_state(policy
, index
);
125 /* Module init and exit code */
126 static int kirkwood_cpufreq_cpu_init(struct cpufreq_policy
*policy
)
130 /* cpuinfo and default policy values */
131 policy
->cpuinfo
.transition_latency
= 5000; /* 5uS */
132 policy
->cur
= kirkwood_cpufreq_get_cpu_frequency(0);
134 result
= cpufreq_frequency_table_cpuinfo(policy
, kirkwood_freq_table
);
138 cpufreq_frequency_table_get_attr(kirkwood_freq_table
, policy
->cpu
);
143 static int kirkwood_cpufreq_cpu_exit(struct cpufreq_policy
*policy
)
145 cpufreq_frequency_table_put_attr(policy
->cpu
);
149 static struct freq_attr
*kirkwood_cpufreq_attr
[] = {
150 &cpufreq_freq_attr_scaling_available_freqs
,
154 static struct cpufreq_driver kirkwood_cpufreq_driver
= {
155 .get
= kirkwood_cpufreq_get_cpu_frequency
,
156 .verify
= kirkwood_cpufreq_verify
,
157 .target
= kirkwood_cpufreq_target
,
158 .init
= kirkwood_cpufreq_cpu_init
,
159 .exit
= kirkwood_cpufreq_cpu_exit
,
160 .name
= "kirkwood-cpufreq",
161 .attr
= kirkwood_cpufreq_attr
,
164 static int kirkwood_cpufreq_probe(struct platform_device
*pdev
)
166 struct device_node
*np
;
167 struct resource
*res
;
170 priv
.dev
= &pdev
->dev
;
172 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
173 priv
.base
= devm_ioremap_resource(&pdev
->dev
, res
);
174 if (IS_ERR(priv
.base
))
175 return PTR_ERR(priv
.base
);
177 np
= of_cpu_device_node_get(0);
179 dev_err(&pdev
->dev
, "failed to get cpu device node\n");
183 priv
.cpu_clk
= of_clk_get_by_name(np
, "cpu_clk");
184 if (IS_ERR(priv
.cpu_clk
)) {
185 dev_err(priv
.dev
, "Unable to get cpuclk");
186 return PTR_ERR(priv
.cpu_clk
);
189 clk_prepare_enable(priv
.cpu_clk
);
190 kirkwood_freq_table
[0].frequency
= clk_get_rate(priv
.cpu_clk
) / 1000;
192 priv
.ddr_clk
= of_clk_get_by_name(np
, "ddrclk");
193 if (IS_ERR(priv
.ddr_clk
)) {
194 dev_err(priv
.dev
, "Unable to get ddrclk");
195 err
= PTR_ERR(priv
.ddr_clk
);
199 clk_prepare_enable(priv
.ddr_clk
);
200 kirkwood_freq_table
[1].frequency
= clk_get_rate(priv
.ddr_clk
) / 1000;
202 priv
.powersave_clk
= of_clk_get_by_name(np
, "powersave");
203 if (IS_ERR(priv
.powersave_clk
)) {
204 dev_err(priv
.dev
, "Unable to get powersave");
205 err
= PTR_ERR(priv
.powersave_clk
);
208 clk_prepare(priv
.powersave_clk
);
213 err
= cpufreq_register_driver(&kirkwood_cpufreq_driver
);
217 dev_err(priv
.dev
, "Failed to register cpufreq driver");
219 clk_disable_unprepare(priv
.powersave_clk
);
221 clk_disable_unprepare(priv
.ddr_clk
);
223 clk_disable_unprepare(priv
.cpu_clk
);
229 static int kirkwood_cpufreq_remove(struct platform_device
*pdev
)
231 cpufreq_unregister_driver(&kirkwood_cpufreq_driver
);
233 clk_disable_unprepare(priv
.powersave_clk
);
234 clk_disable_unprepare(priv
.ddr_clk
);
235 clk_disable_unprepare(priv
.cpu_clk
);
240 static struct platform_driver kirkwood_cpufreq_platform_driver
= {
241 .probe
= kirkwood_cpufreq_probe
,
242 .remove
= kirkwood_cpufreq_remove
,
244 .name
= "kirkwood-cpufreq",
245 .owner
= THIS_MODULE
,
249 module_platform_driver(kirkwood_cpufreq_platform_driver
);
251 MODULE_LICENSE("GPL v2");
252 MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch");
253 MODULE_DESCRIPTION("cpufreq driver for Marvell's kirkwood CPU");
254 MODULE_ALIAS("platform:kirkwood-cpufreq");