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/cpufreq.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
19 #include <asm/proc-fns.h>
21 #define CPU_SW_INT_BLK BIT(28)
27 struct clk *powersave_clk;
32 #define STATE_CPU_FREQ 0x01
33 #define STATE_DDR_FREQ 0x02
36 * Kirkwood can swap the clock to the CPU between two clocks:
41 * The frequencies are set at runtime before registering this table.
43 static struct cpufreq_frequency_table kirkwood_freq_table[] = {
44 {0, STATE_CPU_FREQ, 0}, /* CPU uses cpuclk */
45 {0, STATE_DDR_FREQ, 0}, /* CPU uses ddrclk */
46 {0, 0, CPUFREQ_TABLE_END},
49 static unsigned int kirkwood_cpufreq_get_cpu_frequency(unsigned int cpu)
51 return clk_get_rate(priv.powersave_clk) / 1000;
54 static int kirkwood_cpufreq_target(struct cpufreq_policy *policy,
57 unsigned int state = kirkwood_freq_table[index].driver_data;
62 /* Disable interrupts to the CPU */
63 reg = readl_relaxed(priv.base);
64 reg |= CPU_SW_INT_BLK;
65 writel_relaxed(reg, priv.base);
69 clk_set_parent(priv.powersave_clk, priv.cpu_clk);
72 clk_set_parent(priv.powersave_clk, priv.ddr_clk);
76 /* Wait-for-Interrupt, while the hardware changes frequency */
79 /* Enable interrupts to the CPU */
80 reg = readl_relaxed(priv.base);
81 reg &= ~CPU_SW_INT_BLK;
82 writel_relaxed(reg, priv.base);
89 /* Module init and exit code */
90 static int kirkwood_cpufreq_cpu_init(struct cpufreq_policy *policy)
92 return cpufreq_generic_init(policy, kirkwood_freq_table, 5000);
95 static struct cpufreq_driver kirkwood_cpufreq_driver = {
96 .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
97 .get = kirkwood_cpufreq_get_cpu_frequency,
98 .verify = cpufreq_generic_frequency_table_verify,
99 .target_index = kirkwood_cpufreq_target,
100 .init = kirkwood_cpufreq_cpu_init,
101 .name = "kirkwood-cpufreq",
102 .attr = cpufreq_generic_attr,
105 static int kirkwood_cpufreq_probe(struct platform_device *pdev)
107 struct device_node *np;
108 struct resource *res;
111 priv.dev = &pdev->dev;
113 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
114 priv.base = devm_ioremap_resource(&pdev->dev, res);
115 if (IS_ERR(priv.base))
116 return PTR_ERR(priv.base);
118 np = of_cpu_device_node_get(0);
120 dev_err(&pdev->dev, "failed to get cpu device node\n");
124 priv.cpu_clk = of_clk_get_by_name(np, "cpu_clk");
125 if (IS_ERR(priv.cpu_clk)) {
126 dev_err(priv.dev, "Unable to get cpuclk");
127 return PTR_ERR(priv.cpu_clk);
130 clk_prepare_enable(priv.cpu_clk);
131 kirkwood_freq_table[0].frequency = clk_get_rate(priv.cpu_clk) / 1000;
133 priv.ddr_clk = of_clk_get_by_name(np, "ddrclk");
134 if (IS_ERR(priv.ddr_clk)) {
135 dev_err(priv.dev, "Unable to get ddrclk");
136 err = PTR_ERR(priv.ddr_clk);
140 clk_prepare_enable(priv.ddr_clk);
141 kirkwood_freq_table[1].frequency = clk_get_rate(priv.ddr_clk) / 1000;
143 priv.powersave_clk = of_clk_get_by_name(np, "powersave");
144 if (IS_ERR(priv.powersave_clk)) {
145 dev_err(priv.dev, "Unable to get powersave");
146 err = PTR_ERR(priv.powersave_clk);
149 clk_prepare_enable(priv.powersave_clk);
154 err = cpufreq_register_driver(&kirkwood_cpufreq_driver);
158 dev_err(priv.dev, "Failed to register cpufreq driver");
160 clk_disable_unprepare(priv.powersave_clk);
162 clk_disable_unprepare(priv.ddr_clk);
164 clk_disable_unprepare(priv.cpu_clk);
170 static int kirkwood_cpufreq_remove(struct platform_device *pdev)
172 cpufreq_unregister_driver(&kirkwood_cpufreq_driver);
174 clk_disable_unprepare(priv.powersave_clk);
175 clk_disable_unprepare(priv.ddr_clk);
176 clk_disable_unprepare(priv.cpu_clk);
181 static struct platform_driver kirkwood_cpufreq_platform_driver = {
182 .probe = kirkwood_cpufreq_probe,
183 .remove = kirkwood_cpufreq_remove,
185 .name = "kirkwood-cpufreq",
189 module_platform_driver(kirkwood_cpufreq_platform_driver);
191 MODULE_LICENSE("GPL v2");
192 MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch");
193 MODULE_DESCRIPTION("cpufreq driver for Marvell's kirkwood CPU");
194 MODULE_ALIAS("platform:kirkwood-cpufreq");