blob: eb7abe345b50dd147351d8f9c22d6be892e5ad0a [file] [log] [blame]
Andrew Lunn2a4bd9f2013-02-05 22:52:51 +01001/*
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>
15#include <linux/clk-provider.h>
16#include <linux/cpufreq.h>
Sudeep KarkadaNageshae768f352013-06-17 15:09:51 +010017#include <linux/of_device.h>
Andrew Lunn2a4bd9f2013-02-05 22:52:51 +010018#include <linux/platform_device.h>
19#include <linux/io.h>
20#include <asm/proc-fns.h>
21
22#define CPU_SW_INT_BLK BIT(28)
23
24static struct priv
25{
26 struct clk *cpu_clk;
27 struct clk *ddr_clk;
28 struct clk *powersave_clk;
29 struct device *dev;
30 void __iomem *base;
31} priv;
32
33#define STATE_CPU_FREQ 0x01
34#define STATE_DDR_FREQ 0x02
35
36/*
37 * Kirkwood can swap the clock to the CPU between two clocks:
38 *
39 * - cpu clk
40 * - ddr clk
41 *
42 * The frequencies are set at runtime before registering this *
43 * table.
44 */
45static 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},
49};
50
51static unsigned int kirkwood_cpufreq_get_cpu_frequency(unsigned int cpu)
52{
53 if (__clk_is_enabled(priv.powersave_clk))
54 return kirkwood_freq_table[1].frequency;
55 return kirkwood_freq_table[0].frequency;
56}
57
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053058static int kirkwood_cpufreq_target(struct cpufreq_policy *policy,
59 unsigned int index)
Andrew Lunn2a4bd9f2013-02-05 22:52:51 +010060{
Viresh Kumar50701582013-03-30 16:25:15 +053061 unsigned int state = kirkwood_freq_table[index].driver_data;
Andrew Lunn2a4bd9f2013-02-05 22:52:51 +010062 unsigned long reg;
63
Viresh Kumard4019f02013-08-14 19:38:24 +053064 local_irq_disable();
Andrew Lunn2a4bd9f2013-02-05 22:52:51 +010065
Viresh Kumard4019f02013-08-14 19:38:24 +053066 /* Disable interrupts to the CPU */
67 reg = readl_relaxed(priv.base);
68 reg |= CPU_SW_INT_BLK;
69 writel_relaxed(reg, priv.base);
Andrew Lunn2a4bd9f2013-02-05 22:52:51 +010070
Viresh Kumard4019f02013-08-14 19:38:24 +053071 switch (state) {
72 case STATE_CPU_FREQ:
73 clk_disable(priv.powersave_clk);
74 break;
75 case STATE_DDR_FREQ:
76 clk_enable(priv.powersave_clk);
77 break;
Andrew Lunn2a4bd9f2013-02-05 22:52:51 +010078 }
Viresh Kumard4019f02013-08-14 19:38:24 +053079
80 /* Wait-for-Interrupt, while the hardware changes frequency */
81 cpu_do_idle();
82
83 /* Enable interrupts to the CPU */
84 reg = readl_relaxed(priv.base);
85 reg &= ~CPU_SW_INT_BLK;
86 writel_relaxed(reg, priv.base);
87
88 local_irq_enable();
Andrew Lunn2a4bd9f2013-02-05 22:52:51 +010089
90 return 0;
91}
92
93/* Module init and exit code */
94static int kirkwood_cpufreq_cpu_init(struct cpufreq_policy *policy)
95{
Viresh Kumarfa6fa662013-10-03 20:29:15 +053096 return cpufreq_generic_init(policy, kirkwood_freq_table, 5000);
Andrew Lunn2a4bd9f2013-02-05 22:52:51 +010097}
98
Andrew Lunn2a4bd9f2013-02-05 22:52:51 +010099static struct cpufreq_driver kirkwood_cpufreq_driver = {
Viresh Kumarae6b4272013-12-03 11:20:45 +0530100 .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
Andrew Lunn2a4bd9f2013-02-05 22:52:51 +0100101 .get = kirkwood_cpufreq_get_cpu_frequency,
Viresh Kumara86a41a2013-10-03 20:28:09 +0530102 .verify = cpufreq_generic_frequency_table_verify,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530103 .target_index = kirkwood_cpufreq_target,
Andrew Lunn2a4bd9f2013-02-05 22:52:51 +0100104 .init = kirkwood_cpufreq_cpu_init,
Viresh Kumara86a41a2013-10-03 20:28:09 +0530105 .exit = cpufreq_generic_exit,
Andrew Lunn2a4bd9f2013-02-05 22:52:51 +0100106 .name = "kirkwood-cpufreq",
Viresh Kumara86a41a2013-10-03 20:28:09 +0530107 .attr = cpufreq_generic_attr,
Andrew Lunn2a4bd9f2013-02-05 22:52:51 +0100108};
109
110static int kirkwood_cpufreq_probe(struct platform_device *pdev)
111{
112 struct device_node *np;
113 struct resource *res;
114 int err;
115
116 priv.dev = &pdev->dev;
117
118 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Silviu-Mihai Popescucc721c42013-03-22 00:10:22 +0000119 priv.base = devm_ioremap_resource(&pdev->dev, res);
120 if (IS_ERR(priv.base))
121 return PTR_ERR(priv.base);
Andrew Lunn2a4bd9f2013-02-05 22:52:51 +0100122
Sudeep KarkadaNageshae768f352013-06-17 15:09:51 +0100123 np = of_cpu_device_node_get(0);
124 if (!np) {
125 dev_err(&pdev->dev, "failed to get cpu device node\n");
Andrew Lunn2a4bd9f2013-02-05 22:52:51 +0100126 return -ENODEV;
Sudeep KarkadaNageshae768f352013-06-17 15:09:51 +0100127 }
Andrew Lunn2a4bd9f2013-02-05 22:52:51 +0100128
129 priv.cpu_clk = of_clk_get_by_name(np, "cpu_clk");
130 if (IS_ERR(priv.cpu_clk)) {
131 dev_err(priv.dev, "Unable to get cpuclk");
132 return PTR_ERR(priv.cpu_clk);
133 }
134
135 clk_prepare_enable(priv.cpu_clk);
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)) {
140 dev_err(priv.dev, "Unable to get ddrclk");
141 err = PTR_ERR(priv.ddr_clk);
142 goto out_cpu;
143 }
144
145 clk_prepare_enable(priv.ddr_clk);
146 kirkwood_freq_table[1].frequency = clk_get_rate(priv.ddr_clk) / 1000;
147
148 priv.powersave_clk = of_clk_get_by_name(np, "powersave");
149 if (IS_ERR(priv.powersave_clk)) {
150 dev_err(priv.dev, "Unable to get powersave");
151 err = PTR_ERR(priv.powersave_clk);
152 goto out_ddr;
153 }
154 clk_prepare(priv.powersave_clk);
155
156 of_node_put(np);
157 np = NULL;
158
159 err = cpufreq_register_driver(&kirkwood_cpufreq_driver);
160 if (!err)
161 return 0;
162
163 dev_err(priv.dev, "Failed to register cpufreq driver");
164
165 clk_disable_unprepare(priv.powersave_clk);
166out_ddr:
167 clk_disable_unprepare(priv.ddr_clk);
168out_cpu:
169 clk_disable_unprepare(priv.cpu_clk);
170 of_node_put(np);
171
172 return err;
173}
174
175static int kirkwood_cpufreq_remove(struct platform_device *pdev)
176{
177 cpufreq_unregister_driver(&kirkwood_cpufreq_driver);
178
179 clk_disable_unprepare(priv.powersave_clk);
180 clk_disable_unprepare(priv.ddr_clk);
181 clk_disable_unprepare(priv.cpu_clk);
182
183 return 0;
184}
185
186static struct platform_driver kirkwood_cpufreq_platform_driver = {
187 .probe = kirkwood_cpufreq_probe,
188 .remove = kirkwood_cpufreq_remove,
189 .driver = {
190 .name = "kirkwood-cpufreq",
191 .owner = THIS_MODULE,
192 },
193};
194
195module_platform_driver(kirkwood_cpufreq_platform_driver);
196
197MODULE_LICENSE("GPL v2");
198MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch");
199MODULE_DESCRIPTION("cpufreq driver for Marvell's kirkwood CPU");
200MODULE_ALIAS("platform:kirkwood-cpufreq");