Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 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> |
| 15 | #include <linux/clk-provider.h> |
| 16 | #include <linux/cpufreq.h> |
Sudeep KarkadaNagesha | e768f35 | 2013-06-17 15:09:51 +0100 | [diff] [blame] | 17 | #include <linux/of_device.h> |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 18 | #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 | |
| 24 | static 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 | */ |
| 45 | static struct cpufreq_frequency_table kirkwood_freq_table[] = { |
Viresh Kumar | 7f4b046 | 2014-03-28 19:11:47 +0530 | [diff] [blame] | 46 | {0, STATE_CPU_FREQ, 0}, /* CPU uses cpuclk */ |
| 47 | {0, STATE_DDR_FREQ, 0}, /* CPU uses ddrclk */ |
| 48 | {0, 0, CPUFREQ_TABLE_END}, |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | static 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 Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 58 | static int kirkwood_cpufreq_target(struct cpufreq_policy *policy, |
| 59 | unsigned int index) |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 60 | { |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 61 | unsigned int state = kirkwood_freq_table[index].driver_data; |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 62 | unsigned long reg; |
| 63 | |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 64 | local_irq_disable(); |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 65 | |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 66 | /* Disable interrupts to the CPU */ |
| 67 | reg = readl_relaxed(priv.base); |
| 68 | reg |= CPU_SW_INT_BLK; |
| 69 | writel_relaxed(reg, priv.base); |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 70 | |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 71 | 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 Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 78 | } |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 79 | |
| 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 Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | /* Module init and exit code */ |
| 94 | static int kirkwood_cpufreq_cpu_init(struct cpufreq_policy *policy) |
| 95 | { |
Viresh Kumar | fa6fa66 | 2013-10-03 20:29:15 +0530 | [diff] [blame] | 96 | return cpufreq_generic_init(policy, kirkwood_freq_table, 5000); |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 97 | } |
| 98 | |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 99 | static struct cpufreq_driver kirkwood_cpufreq_driver = { |
Viresh Kumar | ae6b427 | 2013-12-03 11:20:45 +0530 | [diff] [blame] | 100 | .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK, |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 101 | .get = kirkwood_cpufreq_get_cpu_frequency, |
Viresh Kumar | a86a41a | 2013-10-03 20:28:09 +0530 | [diff] [blame] | 102 | .verify = cpufreq_generic_frequency_table_verify, |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 103 | .target_index = kirkwood_cpufreq_target, |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 104 | .init = kirkwood_cpufreq_cpu_init, |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 105 | .name = "kirkwood-cpufreq", |
Viresh Kumar | a86a41a | 2013-10-03 20:28:09 +0530 | [diff] [blame] | 106 | .attr = cpufreq_generic_attr, |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | static int kirkwood_cpufreq_probe(struct platform_device *pdev) |
| 110 | { |
| 111 | struct device_node *np; |
| 112 | struct resource *res; |
| 113 | int err; |
| 114 | |
| 115 | priv.dev = &pdev->dev; |
| 116 | |
| 117 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Silviu-Mihai Popescu | cc721c4 | 2013-03-22 00:10:22 +0000 | [diff] [blame] | 118 | priv.base = devm_ioremap_resource(&pdev->dev, res); |
| 119 | if (IS_ERR(priv.base)) |
| 120 | return PTR_ERR(priv.base); |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 121 | |
Sudeep KarkadaNagesha | e768f35 | 2013-06-17 15:09:51 +0100 | [diff] [blame] | 122 | np = of_cpu_device_node_get(0); |
| 123 | if (!np) { |
| 124 | dev_err(&pdev->dev, "failed to get cpu device node\n"); |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 125 | return -ENODEV; |
Sudeep KarkadaNagesha | e768f35 | 2013-06-17 15:09:51 +0100 | [diff] [blame] | 126 | } |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 127 | |
| 128 | priv.cpu_clk = of_clk_get_by_name(np, "cpu_clk"); |
| 129 | if (IS_ERR(priv.cpu_clk)) { |
| 130 | dev_err(priv.dev, "Unable to get cpuclk"); |
| 131 | return PTR_ERR(priv.cpu_clk); |
| 132 | } |
| 133 | |
| 134 | clk_prepare_enable(priv.cpu_clk); |
| 135 | kirkwood_freq_table[0].frequency = clk_get_rate(priv.cpu_clk) / 1000; |
| 136 | |
| 137 | priv.ddr_clk = of_clk_get_by_name(np, "ddrclk"); |
| 138 | if (IS_ERR(priv.ddr_clk)) { |
| 139 | dev_err(priv.dev, "Unable to get ddrclk"); |
| 140 | err = PTR_ERR(priv.ddr_clk); |
| 141 | goto out_cpu; |
| 142 | } |
| 143 | |
| 144 | clk_prepare_enable(priv.ddr_clk); |
| 145 | kirkwood_freq_table[1].frequency = clk_get_rate(priv.ddr_clk) / 1000; |
| 146 | |
| 147 | priv.powersave_clk = of_clk_get_by_name(np, "powersave"); |
| 148 | if (IS_ERR(priv.powersave_clk)) { |
| 149 | dev_err(priv.dev, "Unable to get powersave"); |
| 150 | err = PTR_ERR(priv.powersave_clk); |
| 151 | goto out_ddr; |
| 152 | } |
| 153 | clk_prepare(priv.powersave_clk); |
| 154 | |
| 155 | of_node_put(np); |
| 156 | np = NULL; |
| 157 | |
| 158 | err = cpufreq_register_driver(&kirkwood_cpufreq_driver); |
| 159 | if (!err) |
| 160 | return 0; |
| 161 | |
| 162 | dev_err(priv.dev, "Failed to register cpufreq driver"); |
| 163 | |
| 164 | clk_disable_unprepare(priv.powersave_clk); |
| 165 | out_ddr: |
| 166 | clk_disable_unprepare(priv.ddr_clk); |
| 167 | out_cpu: |
| 168 | clk_disable_unprepare(priv.cpu_clk); |
| 169 | of_node_put(np); |
| 170 | |
| 171 | return err; |
| 172 | } |
| 173 | |
| 174 | static int kirkwood_cpufreq_remove(struct platform_device *pdev) |
| 175 | { |
| 176 | cpufreq_unregister_driver(&kirkwood_cpufreq_driver); |
| 177 | |
| 178 | clk_disable_unprepare(priv.powersave_clk); |
| 179 | clk_disable_unprepare(priv.ddr_clk); |
| 180 | clk_disable_unprepare(priv.cpu_clk); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static struct platform_driver kirkwood_cpufreq_platform_driver = { |
| 186 | .probe = kirkwood_cpufreq_probe, |
| 187 | .remove = kirkwood_cpufreq_remove, |
| 188 | .driver = { |
| 189 | .name = "kirkwood-cpufreq", |
| 190 | .owner = THIS_MODULE, |
| 191 | }, |
| 192 | }; |
| 193 | |
| 194 | module_platform_driver(kirkwood_cpufreq_platform_driver); |
| 195 | |
| 196 | MODULE_LICENSE("GPL v2"); |
| 197 | MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch"); |
| 198 | MODULE_DESCRIPTION("cpufreq driver for Marvell's kirkwood CPU"); |
| 199 | MODULE_ALIAS("platform:kirkwood-cpufreq"); |