blob: 915954507d0a006090c63a8f6d63a6f8e29f00dd [file] [log] [blame]
Maxime Ripard992a56e2014-07-10 23:55:18 +02001/*
2 * Copyright 2013 Emilio López
3 *
4 * Emilio López <emilio@elopez.com.ar>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
Stephen Boyd9dfefe82015-06-19 15:00:46 -070017#include <linux/clk.h>
Maxime Ripard992a56e2014-07-10 23:55:18 +020018#include <linux/clk-provider.h>
Stephen Boyd62e59c42019-04-18 15:20:22 -070019#include <linux/io.h>
Maxime Ripard37e10412014-07-11 18:43:18 +020020#include <linux/of_address.h>
Hans de Goede6ea39532014-12-20 11:36:49 +010021#include <linux/platform_device.h>
Stephen Boyd9dfefe82015-06-19 15:00:46 -070022#include <linux/slab.h>
Maxime Ripard992a56e2014-07-10 23:55:18 +020023
24#include "clk-factors.h"
25
26/**
Julia Lawall35b1fc22016-10-01 21:46:24 +020027 * sun4i_a10_get_mod0_factors() - calculates m, n factors for MOD0-style clocks
Maxime Ripard992a56e2014-07-10 23:55:18 +020028 * MOD0 rate is calculated as follows
29 * rate = (parent_rate >> p) / (m + 1);
30 */
31
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080032static void sun4i_a10_get_mod0_factors(struct factors_request *req)
Maxime Ripard992a56e2014-07-10 23:55:18 +020033{
34 u8 div, calcm, calcp;
35
36 /* These clocks can only divide, so we will never be able to achieve
37 * frequencies higher than the parent frequency */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080038 if (req->rate > req->parent_rate)
39 req->rate = req->parent_rate;
Maxime Ripard992a56e2014-07-10 23:55:18 +020040
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080041 div = DIV_ROUND_UP(req->parent_rate, req->rate);
Maxime Ripard992a56e2014-07-10 23:55:18 +020042
43 if (div < 16)
44 calcp = 0;
45 else if (div / 2 < 16)
46 calcp = 1;
47 else if (div / 4 < 16)
48 calcp = 2;
49 else
50 calcp = 3;
51
52 calcm = DIV_ROUND_UP(div, 1 << calcp);
53
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080054 req->rate = (req->parent_rate >> calcp) / calcm;
55 req->m = calcm - 1;
56 req->p = calcp;
Maxime Ripard992a56e2014-07-10 23:55:18 +020057}
58
59/* user manual says "n" but it's really "p" */
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +080060static const struct clk_factors_config sun4i_a10_mod0_config = {
Maxime Ripard992a56e2014-07-10 23:55:18 +020061 .mshift = 0,
62 .mwidth = 4,
63 .pshift = 16,
64 .pwidth = 2,
65};
66
Hans de Goede6ea39532014-12-20 11:36:49 +010067static const struct factors_data sun4i_a10_mod0_data = {
Maxime Ripard992a56e2014-07-10 23:55:18 +020068 .enable = 31,
69 .mux = 24,
Chen-Yu Tsaie94f8cb32014-10-20 22:10:26 +080070 .muxmask = BIT(1) | BIT(0),
Maxime Ripard992a56e2014-07-10 23:55:18 +020071 .table = &sun4i_a10_mod0_config,
72 .getter = sun4i_a10_get_mod0_factors,
73};
74
75static DEFINE_SPINLOCK(sun4i_a10_mod0_lock);
76
77static void __init sun4i_a10_mod0_setup(struct device_node *node)
78{
Hans de Goede7c74c222014-11-23 14:38:07 +010079 void __iomem *reg;
80
81 reg = of_iomap(node, 0);
82 if (!reg) {
Hans de Goede6ea39532014-12-20 11:36:49 +010083 /*
84 * This happens with mod0 clk nodes instantiated through
85 * mfd, as those do not have their resources assigned at
86 * CLK_OF_DECLARE time yet, so do not print an error.
87 */
Hans de Goede7c74c222014-11-23 14:38:07 +010088 return;
89 }
90
91 sunxi_factors_register(node, &sun4i_a10_mod0_data,
92 &sun4i_a10_mod0_lock, reg);
Maxime Ripard992a56e2014-07-10 23:55:18 +020093}
Ricardo Ribalda Delgadocb1291c2016-07-05 18:23:28 +020094CLK_OF_DECLARE_DRIVER(sun4i_a10_mod0, "allwinner,sun4i-a10-mod0-clk",
95 sun4i_a10_mod0_setup);
Maxime Ripardeaa18f52014-07-10 23:56:11 +020096
Hans de Goede6ea39532014-12-20 11:36:49 +010097static int sun4i_a10_mod0_clk_probe(struct platform_device *pdev)
98{
99 struct device_node *np = pdev->dev.of_node;
100 struct resource *r;
101 void __iomem *reg;
102
103 if (!np)
104 return -ENODEV;
105
106 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
107 reg = devm_ioremap_resource(&pdev->dev, r);
108 if (IS_ERR(reg))
109 return PTR_ERR(reg);
110
111 sunxi_factors_register(np, &sun4i_a10_mod0_data,
112 &sun4i_a10_mod0_lock, reg);
113 return 0;
114}
115
116static const struct of_device_id sun4i_a10_mod0_clk_dt_ids[] = {
117 { .compatible = "allwinner,sun4i-a10-mod0-clk" },
118 { /* sentinel */ }
119};
120
121static struct platform_driver sun4i_a10_mod0_clk_driver = {
122 .driver = {
123 .name = "sun4i-a10-mod0-clk",
124 .of_match_table = sun4i_a10_mod0_clk_dt_ids,
125 },
126 .probe = sun4i_a10_mod0_clk_probe,
127};
Paul Gortmaker77459a02015-06-03 11:20:05 -0400128builtin_platform_driver(sun4i_a10_mod0_clk_driver);
Hans de Goede6ea39532014-12-20 11:36:49 +0100129
Chen-Yu Tsai61af4d82015-01-17 13:19:26 +0800130static const struct factors_data sun9i_a80_mod0_data __initconst = {
131 .enable = 31,
132 .mux = 24,
133 .muxmask = BIT(3) | BIT(2) | BIT(1) | BIT(0),
134 .table = &sun4i_a10_mod0_config,
135 .getter = sun4i_a10_get_mod0_factors,
136};
137
138static void __init sun9i_a80_mod0_setup(struct device_node *node)
139{
140 void __iomem *reg;
141
142 reg = of_io_request_and_map(node, 0, of_node_full_name(node));
143 if (IS_ERR(reg)) {
Rob Herringe665f022018-08-28 10:44:29 -0500144 pr_err("Could not get registers for mod0-clk: %pOFn\n",
145 node);
Chen-Yu Tsai61af4d82015-01-17 13:19:26 +0800146 return;
147 }
148
149 sunxi_factors_register(node, &sun9i_a80_mod0_data,
150 &sun4i_a10_mod0_lock, reg);
151}
152CLK_OF_DECLARE(sun9i_a80_mod0, "allwinner,sun9i-a80-mod0-clk", sun9i_a80_mod0_setup);
153
Maxime Ripardeaa18f52014-07-10 23:56:11 +0200154static DEFINE_SPINLOCK(sun5i_a13_mbus_lock);
155
156static void __init sun5i_a13_mbus_setup(struct device_node *node)
157{
Hans de Goede7c74c222014-11-23 14:38:07 +0100158 void __iomem *reg;
159
160 reg = of_iomap(node, 0);
161 if (!reg) {
162 pr_err("Could not get registers for a13-mbus-clk\n");
163 return;
164 }
165
Maxime Ripardeaa18f52014-07-10 23:56:11 +0200166 /* The MBUS clocks needs to be always enabled */
Stephen Boyd9919d442018-01-02 16:50:27 -0800167 sunxi_factors_register_critical(node, &sun4i_a10_mod0_data,
168 &sun5i_a13_mbus_lock, reg);
Maxime Ripardeaa18f52014-07-10 23:56:11 +0200169}
170CLK_OF_DECLARE(sun5i_a13_mbus, "allwinner,sun5i-a13-mbus-clk", sun5i_a13_mbus_setup);
Maxime Ripard37e10412014-07-11 18:43:18 +0200171
Maxime Ripard37e10412014-07-11 18:43:18 +0200172struct mmc_phase {
173 struct clk_hw hw;
Maxime Ripard6b0b8cc2014-12-07 17:43:04 +0100174 u8 offset;
Maxime Ripard37e10412014-07-11 18:43:18 +0200175 void __iomem *reg;
Maxime Ripard37e10412014-07-11 18:43:18 +0200176 spinlock_t *lock;
177};
178
179#define to_mmc_phase(_hw) container_of(_hw, struct mmc_phase, hw)
180
181static int mmc_get_phase(struct clk_hw *hw)
182{
183 struct clk *mmc, *mmc_parent, *clk = hw->clk;
184 struct mmc_phase *phase = to_mmc_phase(hw);
185 unsigned int mmc_rate, mmc_parent_rate;
186 u16 step, mmc_div;
187 u32 value;
188 u8 delay;
189
190 value = readl(phase->reg);
Maxime Ripard6b0b8cc2014-12-07 17:43:04 +0100191 delay = (value >> phase->offset) & 0x3;
Maxime Ripard37e10412014-07-11 18:43:18 +0200192
193 if (!delay)
194 return 180;
195
196 /* Get the main MMC clock */
197 mmc = clk_get_parent(clk);
198 if (!mmc)
199 return -EINVAL;
200
201 /* And its rate */
202 mmc_rate = clk_get_rate(mmc);
203 if (!mmc_rate)
204 return -EINVAL;
205
206 /* Now, get the MMC parent (most likely some PLL) */
207 mmc_parent = clk_get_parent(mmc);
208 if (!mmc_parent)
209 return -EINVAL;
210
211 /* And its rate */
212 mmc_parent_rate = clk_get_rate(mmc_parent);
213 if (!mmc_parent_rate)
214 return -EINVAL;
215
216 /* Get MMC clock divider */
217 mmc_div = mmc_parent_rate / mmc_rate;
218
219 step = DIV_ROUND_CLOSEST(360, mmc_div);
220 return delay * step;
221}
222
223static int mmc_set_phase(struct clk_hw *hw, int degrees)
224{
225 struct clk *mmc, *mmc_parent, *clk = hw->clk;
226 struct mmc_phase *phase = to_mmc_phase(hw);
227 unsigned int mmc_rate, mmc_parent_rate;
228 unsigned long flags;
229 u32 value;
230 u8 delay;
231
232 /* Get the main MMC clock */
233 mmc = clk_get_parent(clk);
234 if (!mmc)
235 return -EINVAL;
236
237 /* And its rate */
238 mmc_rate = clk_get_rate(mmc);
239 if (!mmc_rate)
240 return -EINVAL;
241
242 /* Now, get the MMC parent (most likely some PLL) */
243 mmc_parent = clk_get_parent(mmc);
244 if (!mmc_parent)
245 return -EINVAL;
246
247 /* And its rate */
248 mmc_parent_rate = clk_get_rate(mmc_parent);
249 if (!mmc_parent_rate)
250 return -EINVAL;
251
252 if (degrees != 180) {
253 u16 step, mmc_div;
254
255 /* Get MMC clock divider */
256 mmc_div = mmc_parent_rate / mmc_rate;
257
258 /*
259 * We can only outphase the clocks by multiple of the
260 * PLL's period.
261 *
262 * Since the MMC clock in only a divider, and the
263 * formula to get the outphasing in degrees is deg =
264 * 360 * delta / period
265 *
266 * If we simplify this formula, we can see that the
267 * only thing that we're concerned about is the number
268 * of period we want to outphase our clock from, and
269 * the divider set by the MMC clock.
270 */
271 step = DIV_ROUND_CLOSEST(360, mmc_div);
272 delay = DIV_ROUND_CLOSEST(degrees, step);
273 } else {
274 delay = 0;
275 }
276
277 spin_lock_irqsave(phase->lock, flags);
278 value = readl(phase->reg);
Maxime Ripard6b0b8cc2014-12-07 17:43:04 +0100279 value &= ~GENMASK(phase->offset + 3, phase->offset);
280 value |= delay << phase->offset;
Maxime Ripard37e10412014-07-11 18:43:18 +0200281 writel(value, phase->reg);
282 spin_unlock_irqrestore(phase->lock, flags);
283
284 return 0;
285}
286
287static const struct clk_ops mmc_clk_ops = {
288 .get_phase = mmc_get_phase,
289 .set_phase = mmc_set_phase,
290};
291
Chen-Yu Tsaieb378df2015-01-13 09:37:23 +0800292/*
293 * sunxi_mmc_setup - Common setup function for mmc module clocks
294 *
295 * The only difference between module clocks on different platforms is the
296 * width of the mux register bits and the valid values, which are passed in
297 * through struct factors_data. The phase clocks parts are identical.
298 */
299static void __init sunxi_mmc_setup(struct device_node *node,
300 const struct factors_data *data,
301 spinlock_t *lock)
Maxime Ripard37e10412014-07-11 18:43:18 +0200302{
Maxime Ripard6b0b8cc2014-12-07 17:43:04 +0100303 struct clk_onecell_data *clk_data;
304 const char *parent;
305 void __iomem *reg;
306 int i;
Maxime Ripard37e10412014-07-11 18:43:18 +0200307
Maxime Ripard6b0b8cc2014-12-07 17:43:04 +0100308 reg = of_io_request_and_map(node, 0, of_node_full_name(node));
309 if (IS_ERR(reg)) {
Rob Herringe665f022018-08-28 10:44:29 -0500310 pr_err("Couldn't map the %pOFn clock registers\n", node);
Maxime Ripard6b0b8cc2014-12-07 17:43:04 +0100311 return;
312 }
Maxime Ripard37e10412014-07-11 18:43:18 +0200313
Maxime Ripard6b0b8cc2014-12-07 17:43:04 +0100314 clk_data = kmalloc(sizeof(*clk_data), GFP_KERNEL);
315 if (!clk_data)
Maxime Ripard37e10412014-07-11 18:43:18 +0200316 return;
317
Maxime Ripard6b0b8cc2014-12-07 17:43:04 +0100318 clk_data->clks = kcalloc(3, sizeof(*clk_data->clks), GFP_KERNEL);
319 if (!clk_data->clks)
320 goto err_free_data;
Maxime Ripard37e10412014-07-11 18:43:18 +0200321
Maxime Ripard6b0b8cc2014-12-07 17:43:04 +0100322 clk_data->clk_num = 3;
Chen-Yu Tsaieb378df2015-01-13 09:37:23 +0800323 clk_data->clks[0] = sunxi_factors_register(node, data, lock, reg);
Maxime Ripard6b0b8cc2014-12-07 17:43:04 +0100324 if (!clk_data->clks[0])
325 goto err_free_clks;
Maxime Ripard37e10412014-07-11 18:43:18 +0200326
Maxime Ripard6b0b8cc2014-12-07 17:43:04 +0100327 parent = __clk_get_name(clk_data->clks[0]);
Maxime Ripard37e10412014-07-11 18:43:18 +0200328
Maxime Ripard6b0b8cc2014-12-07 17:43:04 +0100329 for (i = 1; i < 3; i++) {
330 struct clk_init_data init = {
331 .num_parents = 1,
332 .parent_names = &parent,
333 .ops = &mmc_clk_ops,
334 };
335 struct mmc_phase *phase;
Maxime Ripard37e10412014-07-11 18:43:18 +0200336
Maxime Ripard6b0b8cc2014-12-07 17:43:04 +0100337 phase = kmalloc(sizeof(*phase), GFP_KERNEL);
338 if (!phase)
339 continue;
Maxime Ripard37e10412014-07-11 18:43:18 +0200340
Maxime Ripard6b0b8cc2014-12-07 17:43:04 +0100341 phase->hw.init = &init;
342 phase->reg = reg;
Chen-Yu Tsaieb378df2015-01-13 09:37:23 +0800343 phase->lock = lock;
Maxime Ripard6b0b8cc2014-12-07 17:43:04 +0100344
345 if (i == 1)
346 phase->offset = 8;
347 else
348 phase->offset = 20;
349
350 if (of_property_read_string_index(node, "clock-output-names",
351 i, &init.name))
352 init.name = node->name;
353
354 clk_data->clks[i] = clk_register(NULL, &phase->hw);
355 if (IS_ERR(clk_data->clks[i])) {
356 kfree(phase);
357 continue;
358 }
359 }
360
361 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
Maxime Ripard37e10412014-07-11 18:43:18 +0200362
363 return;
364
Maxime Ripard6b0b8cc2014-12-07 17:43:04 +0100365err_free_clks:
366 kfree(clk_data->clks);
367err_free_data:
368 kfree(clk_data);
Maxime Ripard37e10412014-07-11 18:43:18 +0200369}
Chen-Yu Tsaieb378df2015-01-13 09:37:23 +0800370
371static DEFINE_SPINLOCK(sun4i_a10_mmc_lock);
372
373static void __init sun4i_a10_mmc_setup(struct device_node *node)
374{
375 sunxi_mmc_setup(node, &sun4i_a10_mod0_data, &sun4i_a10_mmc_lock);
376}
Maxime Ripard6b0b8cc2014-12-07 17:43:04 +0100377CLK_OF_DECLARE(sun4i_a10_mmc, "allwinner,sun4i-a10-mmc-clk", sun4i_a10_mmc_setup);
Chen-Yu Tsai61af4d82015-01-17 13:19:26 +0800378
379static DEFINE_SPINLOCK(sun9i_a80_mmc_lock);
380
381static void __init sun9i_a80_mmc_setup(struct device_node *node)
382{
383 sunxi_mmc_setup(node, &sun9i_a80_mod0_data, &sun9i_a80_mmc_lock);
384}
385CLK_OF_DECLARE(sun9i_a80_mmc, "allwinner,sun9i-a80-mmc-clk", sun9i_a80_mmc_setup);