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 int kirkwood_cpufreq_target(struct cpufreq_policy *policy,
61 unsigned int state = kirkwood_freq_table[index].driver_data;
66 /* Disable interrupts to the CPU */
67 reg = readl_relaxed(priv.base);
68 reg |= CPU_SW_INT_BLK;
69 writel_relaxed(reg, priv.base);
73 clk_disable(priv.powersave_clk);
76 clk_enable(priv.powersave_clk);
80 /* Wait-for-Interrupt, while the hardware changes frequency */
83 /* Enable interrupts to the CPU */
84 reg = readl_relaxed(priv.base);
85 reg &= ~CPU_SW_INT_BLK;
86 writel_relaxed(reg, priv.base);
93 /* Module init and exit code */
94 static int kirkwood_cpufreq_cpu_init(struct cpufreq_policy *policy)
96 return cpufreq_generic_init(policy, kirkwood_freq_table, 5000);
99 static struct cpufreq_driver kirkwood_cpufreq_driver = {
100 .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
101 .get = kirkwood_cpufreq_get_cpu_frequency,
102 .verify = cpufreq_generic_frequency_table_verify,
103 .target_index = kirkwood_cpufreq_target,
104 .init = kirkwood_cpufreq_cpu_init,
105 .exit = cpufreq_generic_exit,
106 .name = "kirkwood-cpufreq",
107 .attr = cpufreq_generic_attr,
110 static int kirkwood_cpufreq_probe(struct platform_device *pdev)
112 struct device_node *np;
113 struct resource *res;
116 priv.dev = &pdev->dev;
118 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
119 priv.base = devm_ioremap_resource(&pdev->dev, res);
120 if (IS_ERR(priv.base))
121 return PTR_ERR(priv.base);
123 np = of_cpu_device_node_get(0);
125 dev_err(&pdev->dev, "failed to get cpu device node\n");
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);
135 clk_prepare_enable(priv.cpu_clk);
136 kirkwood_freq_table[0].frequency = clk_get_rate(priv.cpu_clk) / 1000;
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);
145 clk_prepare_enable(priv.ddr_clk);
146 kirkwood_freq_table[1].frequency = clk_get_rate(priv.ddr_clk) / 1000;
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);
154 clk_prepare(priv.powersave_clk);
159 err = cpufreq_register_driver(&kirkwood_cpufreq_driver);
163 dev_err(priv.dev, "Failed to register cpufreq driver");
165 clk_disable_unprepare(priv.powersave_clk);
167 clk_disable_unprepare(priv.ddr_clk);
169 clk_disable_unprepare(priv.cpu_clk);
175 static int kirkwood_cpufreq_remove(struct platform_device *pdev)
177 cpufreq_unregister_driver(&kirkwood_cpufreq_driver);
179 clk_disable_unprepare(priv.powersave_clk);
180 clk_disable_unprepare(priv.ddr_clk);
181 clk_disable_unprepare(priv.cpu_clk);
186 static struct platform_driver kirkwood_cpufreq_platform_driver = {
187 .probe = kirkwood_cpufreq_probe,
188 .remove = kirkwood_cpufreq_remove,
190 .name = "kirkwood-cpufreq",
191 .owner = THIS_MODULE,
195 module_platform_driver(kirkwood_cpufreq_platform_driver);
197 MODULE_LICENSE("GPL v2");
198 MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch");
199 MODULE_DESCRIPTION("cpufreq driver for Marvell's kirkwood CPU");
200 MODULE_ALIAS("platform:kirkwood-cpufreq");