Commit | Line | Data |
---|---|---|
2a4bd9f0 AL |
1 | /* |
2 | * kirkwood_freq.c: cpufreq driver for the Marvell kirkwood | |
3 | * | |
4 | * Copyright (C) 2013 Andrew Lunn <andrew@lunn.ch> | |
5 | * | |
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. | |
10 | */ | |
11 | ||
12 | #include <linux/kernel.h> | |
13 | #include <linux/module.h> | |
14 | #include <linux/clk.h> | |
2a4bd9f0 | 15 | #include <linux/cpufreq.h> |
e768f350 | 16 | #include <linux/of_device.h> |
2a4bd9f0 AL |
17 | #include <linux/platform_device.h> |
18 | #include <linux/io.h> | |
19 | #include <asm/proc-fns.h> | |
20 | ||
21 | #define CPU_SW_INT_BLK BIT(28) | |
22 | ||
23 | static struct priv | |
24 | { | |
25 | struct clk *cpu_clk; | |
26 | struct clk *ddr_clk; | |
27 | struct clk *powersave_clk; | |
28 | struct device *dev; | |
29 | void __iomem *base; | |
30 | } priv; | |
31 | ||
32 | #define STATE_CPU_FREQ 0x01 | |
33 | #define STATE_DDR_FREQ 0x02 | |
34 | ||
35 | /* | |
36 | * Kirkwood can swap the clock to the CPU between two clocks: | |
37 | * | |
38 | * - cpu clk | |
39 | * - ddr clk | |
40 | * | |
10529938 | 41 | * The frequencies are set at runtime before registering this table. |
2a4bd9f0 AL |
42 | */ |
43 | static struct cpufreq_frequency_table kirkwood_freq_table[] = { | |
7f4b0461 VK |
44 | {0, STATE_CPU_FREQ, 0}, /* CPU uses cpuclk */ |
45 | {0, STATE_DDR_FREQ, 0}, /* CPU uses ddrclk */ | |
46 | {0, 0, CPUFREQ_TABLE_END}, | |
2a4bd9f0 AL |
47 | }; |
48 | ||
49 | static unsigned int kirkwood_cpufreq_get_cpu_frequency(unsigned int cpu) | |
50 | { | |
10529938 | 51 | return clk_get_rate(priv.powersave_clk) / 1000; |
2a4bd9f0 AL |
52 | } |
53 | ||
9c0ebcf7 VK |
54 | static int kirkwood_cpufreq_target(struct cpufreq_policy *policy, |
55 | unsigned int index) | |
2a4bd9f0 | 56 | { |
50701588 | 57 | unsigned int state = kirkwood_freq_table[index].driver_data; |
2a4bd9f0 AL |
58 | unsigned long reg; |
59 | ||
d4019f0a | 60 | local_irq_disable(); |
2a4bd9f0 | 61 | |
d4019f0a VK |
62 | /* Disable interrupts to the CPU */ |
63 | reg = readl_relaxed(priv.base); | |
64 | reg |= CPU_SW_INT_BLK; | |
65 | writel_relaxed(reg, priv.base); | |
2a4bd9f0 | 66 | |
d4019f0a VK |
67 | switch (state) { |
68 | case STATE_CPU_FREQ: | |
10529938 | 69 | clk_set_parent(priv.powersave_clk, priv.cpu_clk); |
d4019f0a VK |
70 | break; |
71 | case STATE_DDR_FREQ: | |
10529938 | 72 | clk_set_parent(priv.powersave_clk, priv.ddr_clk); |
d4019f0a VK |
73 | break; |
74 | } | |
2a4bd9f0 | 75 | |
d4019f0a VK |
76 | /* Wait-for-Interrupt, while the hardware changes frequency */ |
77 | cpu_do_idle(); | |
2a4bd9f0 | 78 | |
d4019f0a VK |
79 | /* Enable interrupts to the CPU */ |
80 | reg = readl_relaxed(priv.base); | |
81 | reg &= ~CPU_SW_INT_BLK; | |
82 | writel_relaxed(reg, priv.base); | |
2a4bd9f0 | 83 | |
d4019f0a | 84 | local_irq_enable(); |
2a4bd9f0 AL |
85 | |
86 | return 0; | |
87 | } | |
88 | ||
89 | /* Module init and exit code */ | |
90 | static int kirkwood_cpufreq_cpu_init(struct cpufreq_policy *policy) | |
91 | { | |
fa6fa663 | 92 | return cpufreq_generic_init(policy, kirkwood_freq_table, 5000); |
2a4bd9f0 AL |
93 | } |
94 | ||
2a4bd9f0 | 95 | static struct cpufreq_driver kirkwood_cpufreq_driver = { |
ae6b4271 | 96 | .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK, |
2a4bd9f0 | 97 | .get = kirkwood_cpufreq_get_cpu_frequency, |
a86a41a1 | 98 | .verify = cpufreq_generic_frequency_table_verify, |
9c0ebcf7 | 99 | .target_index = kirkwood_cpufreq_target, |
2a4bd9f0 | 100 | .init = kirkwood_cpufreq_cpu_init, |
2a4bd9f0 | 101 | .name = "kirkwood-cpufreq", |
a86a41a1 | 102 | .attr = cpufreq_generic_attr, |
2a4bd9f0 AL |
103 | }; |
104 | ||
105 | static int kirkwood_cpufreq_probe(struct platform_device *pdev) | |
106 | { | |
107 | struct device_node *np; | |
108 | struct resource *res; | |
109 | int err; | |
110 | ||
111 | priv.dev = &pdev->dev; | |
112 | ||
113 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
cc721c4f SMP |
114 | priv.base = devm_ioremap_resource(&pdev->dev, res); |
115 | if (IS_ERR(priv.base)) | |
116 | return PTR_ERR(priv.base); | |
2a4bd9f0 | 117 | |
e768f350 SK |
118 | np = of_cpu_device_node_get(0); |
119 | if (!np) { | |
120 | dev_err(&pdev->dev, "failed to get cpu device node\n"); | |
2a4bd9f0 | 121 | return -ENODEV; |
e768f350 | 122 | } |
2a4bd9f0 AL |
123 | |
124 | priv.cpu_clk = of_clk_get_by_name(np, "cpu_clk"); | |
125 | if (IS_ERR(priv.cpu_clk)) { | |
4c232f94 | 126 | dev_err(priv.dev, "Unable to get cpuclk\n"); |
2a4bd9f0 AL |
127 | return PTR_ERR(priv.cpu_clk); |
128 | } | |
129 | ||
7575f825 AY |
130 | err = clk_prepare_enable(priv.cpu_clk); |
131 | if (err) { | |
132 | dev_err(priv.dev, "Unable to prepare cpuclk\n"); | |
133 | return err; | |
134 | } | |
135 | ||
2a4bd9f0 AL |
136 | kirkwood_freq_table[0].frequency = clk_get_rate(priv.cpu_clk) / 1000; |
137 | ||
138 | priv.ddr_clk = of_clk_get_by_name(np, "ddrclk"); | |
139 | if (IS_ERR(priv.ddr_clk)) { | |
4c232f94 | 140 | dev_err(priv.dev, "Unable to get ddrclk\n"); |
2a4bd9f0 AL |
141 | err = PTR_ERR(priv.ddr_clk); |
142 | goto out_cpu; | |
143 | } | |
144 | ||
7575f825 AY |
145 | err = clk_prepare_enable(priv.ddr_clk); |
146 | if (err) { | |
147 | dev_err(priv.dev, "Unable to prepare ddrclk\n"); | |
148 | goto out_cpu; | |
149 | } | |
2a4bd9f0 AL |
150 | kirkwood_freq_table[1].frequency = clk_get_rate(priv.ddr_clk) / 1000; |
151 | ||
152 | priv.powersave_clk = of_clk_get_by_name(np, "powersave"); | |
153 | if (IS_ERR(priv.powersave_clk)) { | |
4c232f94 | 154 | dev_err(priv.dev, "Unable to get powersave\n"); |
2a4bd9f0 AL |
155 | err = PTR_ERR(priv.powersave_clk); |
156 | goto out_ddr; | |
157 | } | |
7575f825 AY |
158 | err = clk_prepare_enable(priv.powersave_clk); |
159 | if (err) { | |
160 | dev_err(priv.dev, "Unable to prepare powersave clk\n"); | |
161 | goto out_ddr; | |
162 | } | |
2a4bd9f0 AL |
163 | |
164 | of_node_put(np); | |
165 | np = NULL; | |
166 | ||
167 | err = cpufreq_register_driver(&kirkwood_cpufreq_driver); | |
168 | if (!err) | |
169 | return 0; | |
170 | ||
4c232f94 | 171 | dev_err(priv.dev, "Failed to register cpufreq driver\n"); |
2a4bd9f0 AL |
172 | |
173 | clk_disable_unprepare(priv.powersave_clk); | |
174 | out_ddr: | |
175 | clk_disable_unprepare(priv.ddr_clk); | |
176 | out_cpu: | |
177 | clk_disable_unprepare(priv.cpu_clk); | |
178 | of_node_put(np); | |
179 | ||
180 | return err; | |
181 | } | |
182 | ||
183 | static int kirkwood_cpufreq_remove(struct platform_device *pdev) | |
184 | { | |
185 | cpufreq_unregister_driver(&kirkwood_cpufreq_driver); | |
186 | ||
187 | clk_disable_unprepare(priv.powersave_clk); | |
188 | clk_disable_unprepare(priv.ddr_clk); | |
189 | clk_disable_unprepare(priv.cpu_clk); | |
190 | ||
191 | return 0; | |
192 | } | |
193 | ||
194 | static struct platform_driver kirkwood_cpufreq_platform_driver = { | |
195 | .probe = kirkwood_cpufreq_probe, | |
196 | .remove = kirkwood_cpufreq_remove, | |
197 | .driver = { | |
198 | .name = "kirkwood-cpufreq", | |
2a4bd9f0 AL |
199 | }, |
200 | }; | |
201 | ||
202 | module_platform_driver(kirkwood_cpufreq_platform_driver); | |
203 | ||
204 | MODULE_LICENSE("GPL v2"); | |
205 | MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch"); | |
206 | MODULE_DESCRIPTION("cpufreq driver for Marvell's kirkwood CPU"); | |
207 | MODULE_ALIAS("platform:kirkwood-cpufreq"); |