Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 2 | /* |
| 3 | * kirkwood_freq.c: cpufreq driver for the Marvell kirkwood |
| 4 | * |
| 5 | * Copyright (C) 2013 Andrew Lunn <andrew@lunn.ch> |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/clk.h> |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 11 | #include <linux/cpufreq.h> |
Sudeep KarkadaNagesha | e768f35 | 2013-06-17 15:09:51 +0100 | [diff] [blame] | 12 | #include <linux/of_device.h> |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/io.h> |
| 15 | #include <asm/proc-fns.h> |
| 16 | |
| 17 | #define CPU_SW_INT_BLK BIT(28) |
| 18 | |
| 19 | static struct priv |
| 20 | { |
| 21 | struct clk *cpu_clk; |
| 22 | struct clk *ddr_clk; |
| 23 | struct clk *powersave_clk; |
| 24 | struct device *dev; |
| 25 | void __iomem *base; |
| 26 | } priv; |
| 27 | |
| 28 | #define STATE_CPU_FREQ 0x01 |
| 29 | #define STATE_DDR_FREQ 0x02 |
| 30 | |
| 31 | /* |
| 32 | * Kirkwood can swap the clock to the CPU between two clocks: |
| 33 | * |
| 34 | * - cpu clk |
| 35 | * - ddr clk |
| 36 | * |
Mike Turquette | 1052993 | 2014-08-18 17:30:29 +0200 | [diff] [blame] | 37 | * The frequencies are set at runtime before registering this table. |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 38 | */ |
| 39 | static struct cpufreq_frequency_table kirkwood_freq_table[] = { |
Viresh Kumar | 7f4b046 | 2014-03-28 19:11:47 +0530 | [diff] [blame] | 40 | {0, STATE_CPU_FREQ, 0}, /* CPU uses cpuclk */ |
| 41 | {0, STATE_DDR_FREQ, 0}, /* CPU uses ddrclk */ |
| 42 | {0, 0, CPUFREQ_TABLE_END}, |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | static unsigned int kirkwood_cpufreq_get_cpu_frequency(unsigned int cpu) |
| 46 | { |
Mike Turquette | 1052993 | 2014-08-18 17:30:29 +0200 | [diff] [blame] | 47 | return clk_get_rate(priv.powersave_clk) / 1000; |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 48 | } |
| 49 | |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 50 | static int kirkwood_cpufreq_target(struct cpufreq_policy *policy, |
| 51 | unsigned int index) |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 52 | { |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 53 | unsigned int state = kirkwood_freq_table[index].driver_data; |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 54 | unsigned long reg; |
| 55 | |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 56 | local_irq_disable(); |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 57 | |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 58 | /* Disable interrupts to the CPU */ |
| 59 | reg = readl_relaxed(priv.base); |
| 60 | reg |= CPU_SW_INT_BLK; |
| 61 | writel_relaxed(reg, priv.base); |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 62 | |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 63 | switch (state) { |
| 64 | case STATE_CPU_FREQ: |
Mike Turquette | 1052993 | 2014-08-18 17:30:29 +0200 | [diff] [blame] | 65 | clk_set_parent(priv.powersave_clk, priv.cpu_clk); |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 66 | break; |
| 67 | case STATE_DDR_FREQ: |
Mike Turquette | 1052993 | 2014-08-18 17:30:29 +0200 | [diff] [blame] | 68 | clk_set_parent(priv.powersave_clk, priv.ddr_clk); |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 69 | break; |
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 | |
| 72 | /* Wait-for-Interrupt, while the hardware changes frequency */ |
| 73 | cpu_do_idle(); |
| 74 | |
| 75 | /* Enable interrupts to the CPU */ |
| 76 | reg = readl_relaxed(priv.base); |
| 77 | reg &= ~CPU_SW_INT_BLK; |
| 78 | writel_relaxed(reg, priv.base); |
| 79 | |
| 80 | local_irq_enable(); |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | /* Module init and exit code */ |
| 86 | static int kirkwood_cpufreq_cpu_init(struct cpufreq_policy *policy) |
| 87 | { |
Viresh Kumar | c4dcc8a | 2019-07-16 09:36:08 +0530 | [diff] [blame] | 88 | cpufreq_generic_init(policy, kirkwood_freq_table, 5000); |
| 89 | return 0; |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 92 | static struct cpufreq_driver kirkwood_cpufreq_driver = { |
Viresh Kumar | ae6b427 | 2013-12-03 11:20:45 +0530 | [diff] [blame] | 93 | .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK, |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 94 | .get = kirkwood_cpufreq_get_cpu_frequency, |
Viresh Kumar | a86a41a | 2013-10-03 20:28:09 +0530 | [diff] [blame] | 95 | .verify = cpufreq_generic_frequency_table_verify, |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 96 | .target_index = kirkwood_cpufreq_target, |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 97 | .init = kirkwood_cpufreq_cpu_init, |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 98 | .name = "kirkwood-cpufreq", |
Viresh Kumar | a86a41a | 2013-10-03 20:28:09 +0530 | [diff] [blame] | 99 | .attr = cpufreq_generic_attr, |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | static int kirkwood_cpufreq_probe(struct platform_device *pdev) |
| 103 | { |
| 104 | struct device_node *np; |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 105 | int err; |
| 106 | |
| 107 | priv.dev = &pdev->dev; |
| 108 | |
Yangtao Li | af9c8e9 | 2019-12-15 14:05:21 +0000 | [diff] [blame] | 109 | priv.base = devm_platform_ioremap_resource(pdev, 0); |
Silviu-Mihai Popescu | cc721c4 | 2013-03-22 00:10:22 +0000 | [diff] [blame] | 110 | if (IS_ERR(priv.base)) |
| 111 | return PTR_ERR(priv.base); |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 112 | |
Sudeep KarkadaNagesha | e768f35 | 2013-06-17 15:09:51 +0100 | [diff] [blame] | 113 | np = of_cpu_device_node_get(0); |
| 114 | if (!np) { |
| 115 | dev_err(&pdev->dev, "failed to get cpu device node\n"); |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 116 | return -ENODEV; |
Sudeep KarkadaNagesha | e768f35 | 2013-06-17 15:09:51 +0100 | [diff] [blame] | 117 | } |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 118 | |
| 119 | priv.cpu_clk = of_clk_get_by_name(np, "cpu_clk"); |
| 120 | if (IS_ERR(priv.cpu_clk)) { |
Colin Ian King | 4c232f9 | 2016-09-25 14:35:43 -0700 | [diff] [blame] | 121 | dev_err(priv.dev, "Unable to get cpuclk\n"); |
Wen Yang | 7c46896 | 2019-04-01 09:37:50 +0800 | [diff] [blame] | 122 | err = PTR_ERR(priv.cpu_clk); |
| 123 | goto out_node; |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Arvind Yadav | 7575f82 | 2017-05-17 12:20:35 +0530 | [diff] [blame] | 126 | err = clk_prepare_enable(priv.cpu_clk); |
| 127 | if (err) { |
| 128 | dev_err(priv.dev, "Unable to prepare cpuclk\n"); |
Wen Yang | 7c46896 | 2019-04-01 09:37:50 +0800 | [diff] [blame] | 129 | goto out_node; |
Arvind Yadav | 7575f82 | 2017-05-17 12:20:35 +0530 | [diff] [blame] | 130 | } |
| 131 | |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 132 | kirkwood_freq_table[0].frequency = clk_get_rate(priv.cpu_clk) / 1000; |
| 133 | |
| 134 | priv.ddr_clk = of_clk_get_by_name(np, "ddrclk"); |
| 135 | if (IS_ERR(priv.ddr_clk)) { |
Colin Ian King | 4c232f9 | 2016-09-25 14:35:43 -0700 | [diff] [blame] | 136 | dev_err(priv.dev, "Unable to get ddrclk\n"); |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 137 | err = PTR_ERR(priv.ddr_clk); |
| 138 | goto out_cpu; |
| 139 | } |
| 140 | |
Arvind Yadav | 7575f82 | 2017-05-17 12:20:35 +0530 | [diff] [blame] | 141 | err = clk_prepare_enable(priv.ddr_clk); |
| 142 | if (err) { |
| 143 | dev_err(priv.dev, "Unable to prepare ddrclk\n"); |
| 144 | goto out_cpu; |
| 145 | } |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 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)) { |
Colin Ian King | 4c232f9 | 2016-09-25 14:35:43 -0700 | [diff] [blame] | 150 | dev_err(priv.dev, "Unable to get powersave\n"); |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 151 | err = PTR_ERR(priv.powersave_clk); |
| 152 | goto out_ddr; |
| 153 | } |
Arvind Yadav | 7575f82 | 2017-05-17 12:20:35 +0530 | [diff] [blame] | 154 | err = clk_prepare_enable(priv.powersave_clk); |
| 155 | if (err) { |
| 156 | dev_err(priv.dev, "Unable to prepare powersave clk\n"); |
| 157 | goto out_ddr; |
| 158 | } |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 159 | |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 160 | err = cpufreq_register_driver(&kirkwood_cpufreq_driver); |
Wen Yang | 7c46896 | 2019-04-01 09:37:50 +0800 | [diff] [blame] | 161 | if (err) { |
| 162 | dev_err(priv.dev, "Failed to register cpufreq driver\n"); |
| 163 | goto out_powersave; |
| 164 | } |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 165 | |
Wen Yang | 7c46896 | 2019-04-01 09:37:50 +0800 | [diff] [blame] | 166 | of_node_put(np); |
| 167 | return 0; |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 168 | |
Wen Yang | 7c46896 | 2019-04-01 09:37:50 +0800 | [diff] [blame] | 169 | out_powersave: |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 170 | clk_disable_unprepare(priv.powersave_clk); |
| 171 | out_ddr: |
| 172 | clk_disable_unprepare(priv.ddr_clk); |
| 173 | out_cpu: |
| 174 | clk_disable_unprepare(priv.cpu_clk); |
Wen Yang | 7c46896 | 2019-04-01 09:37:50 +0800 | [diff] [blame] | 175 | out_node: |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 176 | of_node_put(np); |
| 177 | |
| 178 | return err; |
| 179 | } |
| 180 | |
| 181 | static int kirkwood_cpufreq_remove(struct platform_device *pdev) |
| 182 | { |
| 183 | cpufreq_unregister_driver(&kirkwood_cpufreq_driver); |
| 184 | |
| 185 | clk_disable_unprepare(priv.powersave_clk); |
| 186 | clk_disable_unprepare(priv.ddr_clk); |
| 187 | clk_disable_unprepare(priv.cpu_clk); |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static struct platform_driver kirkwood_cpufreq_platform_driver = { |
| 193 | .probe = kirkwood_cpufreq_probe, |
| 194 | .remove = kirkwood_cpufreq_remove, |
| 195 | .driver = { |
| 196 | .name = "kirkwood-cpufreq", |
Andrew Lunn | 2a4bd9f | 2013-02-05 22:52:51 +0100 | [diff] [blame] | 197 | }, |
| 198 | }; |
| 199 | |
| 200 | module_platform_driver(kirkwood_cpufreq_platform_driver); |
| 201 | |
| 202 | MODULE_LICENSE("GPL v2"); |
| 203 | MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch"); |
| 204 | MODULE_DESCRIPTION("cpufreq driver for Marvell's kirkwood CPU"); |
| 205 | MODULE_ALIAS("platform:kirkwood-cpufreq"); |