blob: f9b9688cbd1f6b450f37d8a1f9064e1e7c154aac [file] [log] [blame]
Joel Stanley5eda5d72017-12-22 13:15:18 +10301// SPDX-License-Identifier: GPL-2.0+
2
3#define pr_fmt(fmt) "clk-aspeed: " fmt
4
5#include <linux/clk-provider.h>
6#include <linux/mfd/syscon.h>
7#include <linux/of_address.h>
Joel Stanley98f31182017-12-22 13:15:20 +10308#include <linux/of_device.h>
9#include <linux/platform_device.h>
Joel Stanley5eda5d72017-12-22 13:15:18 +103010#include <linux/regmap.h>
Joel Stanleyf7989832017-12-22 13:15:22 +103011#include <linux/reset-controller.h>
Joel Stanley5eda5d72017-12-22 13:15:18 +103012#include <linux/slab.h>
13#include <linux/spinlock.h>
14
15#include <dt-bindings/clock/aspeed-clock.h>
16
17#define ASPEED_NUM_CLKS 35
18
Joel Stanley99d01e02017-12-22 13:15:19 +103019#define ASPEED_RESET_CTRL 0x04
20#define ASPEED_CLK_SELECTION 0x08
21#define ASPEED_CLK_STOP_CTRL 0x0c
22#define ASPEED_MPLL_PARAM 0x20
23#define ASPEED_HPLL_PARAM 0x24
24#define AST2500_HPLL_BYPASS_EN BIT(20)
25#define AST2400_HPLL_STRAPPED BIT(18)
26#define AST2400_HPLL_BYPASS_EN BIT(17)
27#define ASPEED_MISC_CTRL 0x2c
28#define UART_DIV13_EN BIT(12)
Joel Stanley5eda5d72017-12-22 13:15:18 +103029#define ASPEED_STRAP 0x70
Joel Stanley99d01e02017-12-22 13:15:19 +103030#define CLKIN_25MHZ_EN BIT(23)
31#define AST2400_CLK_SOURCE_SEL BIT(18)
32#define ASPEED_CLK_SELECTION_2 0xd8
33
34/* Globally visible clocks */
35static DEFINE_SPINLOCK(aspeed_clk_lock);
Joel Stanley5eda5d72017-12-22 13:15:18 +103036
37/* Keeps track of all clocks */
38static struct clk_hw_onecell_data *aspeed_clk_data;
39
40static void __iomem *scu_base;
41
42/**
43 * struct aspeed_gate_data - Aspeed gated clocks
44 * @clock_idx: bit used to gate this clock in the clock register
45 * @reset_idx: bit used to reset this IP in the reset register. -1 if no
46 * reset is required when enabling the clock
47 * @name: the clock name
48 * @parent_name: the name of the parent clock
49 * @flags: standard clock framework flags
50 */
51struct aspeed_gate_data {
52 u8 clock_idx;
53 s8 reset_idx;
54 const char *name;
55 const char *parent_name;
56 unsigned long flags;
57};
58
59/**
60 * struct aspeed_clk_gate - Aspeed specific clk_gate structure
61 * @hw: handle between common and hardware-specific interfaces
62 * @reg: register controlling gate
63 * @clock_idx: bit used to gate this clock in the clock register
64 * @reset_idx: bit used to reset this IP in the reset register. -1 if no
65 * reset is required when enabling the clock
66 * @flags: hardware-specific flags
67 * @lock: register lock
68 *
69 * Some of the clocks in the Aspeed SoC must be put in reset before enabling.
70 * This modified version of clk_gate allows an optional reset bit to be
71 * specified.
72 */
73struct aspeed_clk_gate {
74 struct clk_hw hw;
75 struct regmap *map;
76 u8 clock_idx;
77 s8 reset_idx;
78 u8 flags;
79 spinlock_t *lock;
80};
81
82#define to_aspeed_clk_gate(_hw) container_of(_hw, struct aspeed_clk_gate, hw)
83
84/* TODO: ask Aspeed about the actual parent data */
85static const struct aspeed_gate_data aspeed_gates[] = {
86 /* clk rst name parent flags */
87 [ASPEED_CLK_GATE_ECLK] = { 0, -1, "eclk-gate", "eclk", 0 }, /* Video Engine */
88 [ASPEED_CLK_GATE_GCLK] = { 1, 7, "gclk-gate", NULL, 0 }, /* 2D engine */
89 [ASPEED_CLK_GATE_MCLK] = { 2, -1, "mclk-gate", "mpll", CLK_IS_CRITICAL }, /* SDRAM */
90 [ASPEED_CLK_GATE_VCLK] = { 3, 6, "vclk-gate", NULL, 0 }, /* Video Capture */
91 [ASPEED_CLK_GATE_BCLK] = { 4, 10, "bclk-gate", "bclk", 0 }, /* PCIe/PCI */
92 [ASPEED_CLK_GATE_DCLK] = { 5, -1, "dclk-gate", NULL, 0 }, /* DAC */
93 [ASPEED_CLK_GATE_REFCLK] = { 6, -1, "refclk-gate", "clkin", CLK_IS_CRITICAL },
94 [ASPEED_CLK_GATE_USBPORT2CLK] = { 7, 3, "usb-port2-gate", NULL, 0 }, /* USB2.0 Host port 2 */
95 [ASPEED_CLK_GATE_LCLK] = { 8, 5, "lclk-gate", NULL, 0 }, /* LPC */
96 [ASPEED_CLK_GATE_USBUHCICLK] = { 9, 15, "usb-uhci-gate", NULL, 0 }, /* USB1.1 (requires port 2 enabled) */
97 [ASPEED_CLK_GATE_D1CLK] = { 10, 13, "d1clk-gate", NULL, 0 }, /* GFX CRT */
98 [ASPEED_CLK_GATE_YCLK] = { 13, 4, "yclk-gate", NULL, 0 }, /* HAC */
99 [ASPEED_CLK_GATE_USBPORT1CLK] = { 14, 14, "usb-port1-gate", NULL, 0 }, /* USB2 hub/USB2 host port 1/USB1.1 dev */
100 [ASPEED_CLK_GATE_UART1CLK] = { 15, -1, "uart1clk-gate", "uart", 0 }, /* UART1 */
101 [ASPEED_CLK_GATE_UART2CLK] = { 16, -1, "uart2clk-gate", "uart", 0 }, /* UART2 */
102 [ASPEED_CLK_GATE_UART5CLK] = { 17, -1, "uart5clk-gate", "uart", 0 }, /* UART5 */
103 [ASPEED_CLK_GATE_ESPICLK] = { 19, -1, "espiclk-gate", NULL, 0 }, /* eSPI */
104 [ASPEED_CLK_GATE_MAC1CLK] = { 20, 11, "mac1clk-gate", "mac", 0 }, /* MAC1 */
105 [ASPEED_CLK_GATE_MAC2CLK] = { 21, 12, "mac2clk-gate", "mac", 0 }, /* MAC2 */
106 [ASPEED_CLK_GATE_RSACLK] = { 24, -1, "rsaclk-gate", NULL, 0 }, /* RSA */
107 [ASPEED_CLK_GATE_UART3CLK] = { 25, -1, "uart3clk-gate", "uart", 0 }, /* UART3 */
108 [ASPEED_CLK_GATE_UART4CLK] = { 26, -1, "uart4clk-gate", "uart", 0 }, /* UART4 */
109 [ASPEED_CLK_GATE_SDCLKCLK] = { 27, 16, "sdclk-gate", NULL, 0 }, /* SDIO/SD */
110 [ASPEED_CLK_GATE_LHCCLK] = { 28, -1, "lhclk-gate", "lhclk", 0 }, /* LPC master/LPC+ */
111};
112
Joel Stanley98f31182017-12-22 13:15:20 +1030113static const struct clk_div_table ast2500_mac_div_table[] = {
114 { 0x0, 4 }, /* Yep, really. Aspeed confirmed this is correct */
115 { 0x1, 4 },
116 { 0x2, 6 },
117 { 0x3, 8 },
118 { 0x4, 10 },
119 { 0x5, 12 },
120 { 0x6, 14 },
121 { 0x7, 16 },
122 { 0 }
123};
124
Joel Stanley99d01e02017-12-22 13:15:19 +1030125static const struct clk_div_table ast2400_div_table[] = {
126 { 0x0, 2 },
127 { 0x1, 4 },
128 { 0x2, 6 },
129 { 0x3, 8 },
130 { 0x4, 10 },
131 { 0x5, 12 },
132 { 0x6, 14 },
133 { 0x7, 16 },
134 { 0 }
135};
136
137static const struct clk_div_table ast2500_div_table[] = {
138 { 0x0, 4 },
139 { 0x1, 8 },
140 { 0x2, 12 },
141 { 0x3, 16 },
142 { 0x4, 20 },
143 { 0x5, 24 },
144 { 0x6, 28 },
145 { 0x7, 32 },
146 { 0 }
147};
148
149static struct clk_hw *aspeed_ast2400_calc_pll(const char *name, u32 val)
150{
151 unsigned int mult, div;
152
153 if (val & AST2400_HPLL_BYPASS_EN) {
154 /* Pass through mode */
155 mult = div = 1;
156 } else {
157 /* F = 24Mhz * (2-OD) * [(N + 2) / (D + 1)] */
158 u32 n = (val >> 5) & 0x3f;
159 u32 od = (val >> 4) & 0x1;
160 u32 d = val & 0xf;
161
162 mult = (2 - od) * (n + 2);
163 div = d + 1;
164 }
165 return clk_hw_register_fixed_factor(NULL, name, "clkin", 0,
166 mult, div);
167};
168
169static struct clk_hw *aspeed_ast2500_calc_pll(const char *name, u32 val)
170{
171 unsigned int mult, div;
172
173 if (val & AST2500_HPLL_BYPASS_EN) {
174 /* Pass through mode */
175 mult = div = 1;
176 } else {
177 /* F = clkin * [(M+1) / (N+1)] / (P + 1) */
178 u32 p = (val >> 13) & 0x3f;
179 u32 m = (val >> 5) & 0xff;
180 u32 n = val & 0x1f;
181
182 mult = (m + 1) / (n + 1);
183 div = p + 1;
184 }
185
186 return clk_hw_register_fixed_factor(NULL, name, "clkin", 0,
187 mult, div);
188}
189
Joel Stanley98f31182017-12-22 13:15:20 +1030190struct aspeed_clk_soc_data {
191 const struct clk_div_table *div_table;
192 const struct clk_div_table *mac_div_table;
193 struct clk_hw *(*calc_pll)(const char *name, u32 val);
194};
195
196static const struct aspeed_clk_soc_data ast2500_data = {
197 .div_table = ast2500_div_table,
198 .mac_div_table = ast2500_mac_div_table,
199 .calc_pll = aspeed_ast2500_calc_pll,
200};
201
202static const struct aspeed_clk_soc_data ast2400_data = {
203 .div_table = ast2400_div_table,
204 .mac_div_table = ast2400_div_table,
205 .calc_pll = aspeed_ast2400_calc_pll,
206};
207
Joel Stanley15ed8ce52017-12-22 13:15:21 +1030208static int aspeed_clk_enable(struct clk_hw *hw)
209{
210 struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
211 unsigned long flags;
212 u32 clk = BIT(gate->clock_idx);
213 u32 rst = BIT(gate->reset_idx);
214
215 spin_lock_irqsave(gate->lock, flags);
216
217 if (gate->reset_idx >= 0) {
218 /* Put IP in reset */
219 regmap_update_bits(gate->map, ASPEED_RESET_CTRL, rst, rst);
220
221 /* Delay 100us */
222 udelay(100);
223 }
224
225 /* Enable clock */
226 regmap_update_bits(gate->map, ASPEED_CLK_STOP_CTRL, clk, 0);
227
228 if (gate->reset_idx >= 0) {
229 /* A delay of 10ms is specified by the ASPEED docs */
230 mdelay(10);
231
232 /* Take IP out of reset */
233 regmap_update_bits(gate->map, ASPEED_RESET_CTRL, rst, 0);
234 }
235
236 spin_unlock_irqrestore(gate->lock, flags);
237
238 return 0;
239}
240
241static void aspeed_clk_disable(struct clk_hw *hw)
242{
243 struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
244 unsigned long flags;
245 u32 clk = BIT(gate->clock_idx);
246
247 spin_lock_irqsave(gate->lock, flags);
248
249 regmap_update_bits(gate->map, ASPEED_CLK_STOP_CTRL, clk, clk);
250
251 spin_unlock_irqrestore(gate->lock, flags);
252}
253
254static int aspeed_clk_is_enabled(struct clk_hw *hw)
255{
256 struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
257 u32 clk = BIT(gate->clock_idx);
258 u32 reg;
259
260 regmap_read(gate->map, ASPEED_CLK_STOP_CTRL, &reg);
261
262 return (reg & clk) ? 0 : 1;
263}
264
265static const struct clk_ops aspeed_clk_gate_ops = {
266 .enable = aspeed_clk_enable,
267 .disable = aspeed_clk_disable,
268 .is_enabled = aspeed_clk_is_enabled,
269};
270
Joel Stanleyf7989832017-12-22 13:15:22 +1030271/**
272 * struct aspeed_reset - Aspeed reset controller
273 * @map: regmap to access the containing system controller
274 * @rcdev: reset controller device
275 */
276struct aspeed_reset {
277 struct regmap *map;
278 struct reset_controller_dev rcdev;
279};
280
281#define to_aspeed_reset(p) container_of((p), struct aspeed_reset, rcdev)
282
283static const u8 aspeed_resets[] = {
284 [ASPEED_RESET_XDMA] = 25,
285 [ASPEED_RESET_MCTP] = 24,
286 [ASPEED_RESET_ADC] = 23,
287 [ASPEED_RESET_JTAG_MASTER] = 22,
288 [ASPEED_RESET_MIC] = 18,
289 [ASPEED_RESET_PWM] = 9,
290 [ASPEED_RESET_PCIVGA] = 8,
291 [ASPEED_RESET_I2C] = 2,
292 [ASPEED_RESET_AHB] = 1,
293};
294
295static int aspeed_reset_deassert(struct reset_controller_dev *rcdev,
296 unsigned long id)
297{
298 struct aspeed_reset *ar = to_aspeed_reset(rcdev);
299 u32 rst = BIT(aspeed_resets[id]);
300
301 return regmap_update_bits(ar->map, ASPEED_RESET_CTRL, rst, 0);
302}
303
304static int aspeed_reset_assert(struct reset_controller_dev *rcdev,
305 unsigned long id)
306{
307 struct aspeed_reset *ar = to_aspeed_reset(rcdev);
308 u32 rst = BIT(aspeed_resets[id]);
309
310 return regmap_update_bits(ar->map, ASPEED_RESET_CTRL, rst, rst);
311}
312
313static int aspeed_reset_status(struct reset_controller_dev *rcdev,
314 unsigned long id)
315{
316 struct aspeed_reset *ar = to_aspeed_reset(rcdev);
317 u32 val, rst = BIT(aspeed_resets[id]);
318 int ret;
319
320 ret = regmap_read(ar->map, ASPEED_RESET_CTRL, &val);
321 if (ret)
322 return ret;
323
324 return !!(val & rst);
325}
326
327static const struct reset_control_ops aspeed_reset_ops = {
328 .assert = aspeed_reset_assert,
329 .deassert = aspeed_reset_deassert,
330 .status = aspeed_reset_status,
331};
332
Joel Stanley15ed8ce52017-12-22 13:15:21 +1030333static struct clk_hw *aspeed_clk_hw_register_gate(struct device *dev,
334 const char *name, const char *parent_name, unsigned long flags,
335 struct regmap *map, u8 clock_idx, u8 reset_idx,
336 u8 clk_gate_flags, spinlock_t *lock)
337{
338 struct aspeed_clk_gate *gate;
339 struct clk_init_data init;
340 struct clk_hw *hw;
341 int ret;
342
343 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
344 if (!gate)
345 return ERR_PTR(-ENOMEM);
346
347 init.name = name;
348 init.ops = &aspeed_clk_gate_ops;
349 init.flags = flags;
350 init.parent_names = parent_name ? &parent_name : NULL;
351 init.num_parents = parent_name ? 1 : 0;
352
353 gate->map = map;
354 gate->clock_idx = clock_idx;
355 gate->reset_idx = reset_idx;
356 gate->flags = clk_gate_flags;
357 gate->lock = lock;
358 gate->hw.init = &init;
359
360 hw = &gate->hw;
361 ret = clk_hw_register(dev, hw);
362 if (ret) {
363 kfree(gate);
364 hw = ERR_PTR(ret);
365 }
366
367 return hw;
368}
369
Joel Stanley98f31182017-12-22 13:15:20 +1030370static int aspeed_clk_probe(struct platform_device *pdev)
371{
372 const struct aspeed_clk_soc_data *soc_data;
373 struct device *dev = &pdev->dev;
Joel Stanleyf7989832017-12-22 13:15:22 +1030374 struct aspeed_reset *ar;
Joel Stanley98f31182017-12-22 13:15:20 +1030375 struct regmap *map;
376 struct clk_hw *hw;
377 u32 val, rate;
Joel Stanleyf7989832017-12-22 13:15:22 +1030378 int i, ret;
Joel Stanley98f31182017-12-22 13:15:20 +1030379
380 map = syscon_node_to_regmap(dev->of_node);
381 if (IS_ERR(map)) {
382 dev_err(dev, "no syscon regmap\n");
383 return PTR_ERR(map);
384 }
385
Joel Stanleyf7989832017-12-22 13:15:22 +1030386 ar = devm_kzalloc(dev, sizeof(*ar), GFP_KERNEL);
387 if (!ar)
388 return -ENOMEM;
389
390 ar->map = map;
391 ar->rcdev.owner = THIS_MODULE;
392 ar->rcdev.nr_resets = ARRAY_SIZE(aspeed_resets);
393 ar->rcdev.ops = &aspeed_reset_ops;
394 ar->rcdev.of_node = dev->of_node;
395
396 ret = devm_reset_controller_register(dev, &ar->rcdev);
397 if (ret) {
398 dev_err(dev, "could not register reset controller\n");
399 return ret;
400 }
401
Joel Stanley98f31182017-12-22 13:15:20 +1030402 /* SoC generations share common layouts but have different divisors */
403 soc_data = of_device_get_match_data(dev);
404 if (!soc_data) {
405 dev_err(dev, "no match data for platform\n");
406 return -EINVAL;
407 }
408
409 /* UART clock div13 setting */
410 regmap_read(map, ASPEED_MISC_CTRL, &val);
411 if (val & UART_DIV13_EN)
412 rate = 24000000 / 13;
413 else
414 rate = 24000000;
415 /* TODO: Find the parent data for the uart clock */
416 hw = clk_hw_register_fixed_rate(dev, "uart", NULL, 0, rate);
417 if (IS_ERR(hw))
418 return PTR_ERR(hw);
419 aspeed_clk_data->hws[ASPEED_CLK_UART] = hw;
420
421 /*
422 * Memory controller (M-PLL) PLL. This clock is configured by the
423 * bootloader, and is exposed to Linux as a read-only clock rate.
424 */
425 regmap_read(map, ASPEED_MPLL_PARAM, &val);
426 hw = soc_data->calc_pll("mpll", val);
427 if (IS_ERR(hw))
428 return PTR_ERR(hw);
429 aspeed_clk_data->hws[ASPEED_CLK_MPLL] = hw;
430
431 /* SD/SDIO clock divider (TODO: There's a gate too) */
432 hw = clk_hw_register_divider_table(dev, "sdio", "hpll", 0,
433 scu_base + ASPEED_CLK_SELECTION, 12, 3, 0,
434 soc_data->div_table,
435 &aspeed_clk_lock);
436 if (IS_ERR(hw))
437 return PTR_ERR(hw);
438 aspeed_clk_data->hws[ASPEED_CLK_SDIO] = hw;
439
440 /* MAC AHB bus clock divider */
441 hw = clk_hw_register_divider_table(dev, "mac", "hpll", 0,
442 scu_base + ASPEED_CLK_SELECTION, 16, 3, 0,
443 soc_data->mac_div_table,
444 &aspeed_clk_lock);
445 if (IS_ERR(hw))
446 return PTR_ERR(hw);
447 aspeed_clk_data->hws[ASPEED_CLK_MAC] = hw;
448
449 /* LPC Host (LHCLK) clock divider */
450 hw = clk_hw_register_divider_table(dev, "lhclk", "hpll", 0,
451 scu_base + ASPEED_CLK_SELECTION, 20, 3, 0,
452 soc_data->div_table,
453 &aspeed_clk_lock);
454 if (IS_ERR(hw))
455 return PTR_ERR(hw);
456 aspeed_clk_data->hws[ASPEED_CLK_LHCLK] = hw;
457
458 /* P-Bus (BCLK) clock divider */
459 hw = clk_hw_register_divider_table(dev, "bclk", "hpll", 0,
460 scu_base + ASPEED_CLK_SELECTION_2, 0, 2, 0,
461 soc_data->div_table,
462 &aspeed_clk_lock);
463 if (IS_ERR(hw))
464 return PTR_ERR(hw);
465 aspeed_clk_data->hws[ASPEED_CLK_BCLK] = hw;
466
Joel Stanley15ed8ce52017-12-22 13:15:21 +1030467 /*
468 * TODO: There are a number of clocks that not included in this driver
469 * as more information is required:
470 * D2-PLL
471 * D-PLL
472 * YCLK
473 * RGMII
474 * RMII
475 * UART[1..5] clock source mux
476 * Video Engine (ECLK) mux and clock divider
477 */
478
479 for (i = 0; i < ARRAY_SIZE(aspeed_gates); i++) {
480 const struct aspeed_gate_data *gd = &aspeed_gates[i];
481
482 hw = aspeed_clk_hw_register_gate(dev,
483 gd->name,
484 gd->parent_name,
485 gd->flags,
486 map,
487 gd->clock_idx,
488 gd->reset_idx,
489 CLK_GATE_SET_TO_DISABLE,
490 &aspeed_clk_lock);
491 if (IS_ERR(hw))
492 return PTR_ERR(hw);
493 aspeed_clk_data->hws[i] = hw;
494 }
495
Joel Stanley98f31182017-12-22 13:15:20 +1030496 return 0;
497};
498
499static const struct of_device_id aspeed_clk_dt_ids[] = {
500 { .compatible = "aspeed,ast2400-scu", .data = &ast2400_data },
501 { .compatible = "aspeed,ast2500-scu", .data = &ast2500_data },
502 { }
503};
504
505static struct platform_driver aspeed_clk_driver = {
506 .probe = aspeed_clk_probe,
507 .driver = {
508 .name = "aspeed-clk",
509 .of_match_table = aspeed_clk_dt_ids,
510 .suppress_bind_attrs = true,
511 },
512};
513builtin_platform_driver(aspeed_clk_driver);
514
Joel Stanley99d01e02017-12-22 13:15:19 +1030515static void __init aspeed_ast2400_cc(struct regmap *map)
516{
517 struct clk_hw *hw;
518 u32 val, freq, div;
519
520 /*
521 * CLKIN is the crystal oscillator, 24, 48 or 25MHz selected by
522 * strapping
523 */
524 regmap_read(map, ASPEED_STRAP, &val);
525 if (val & CLKIN_25MHZ_EN)
526 freq = 25000000;
527 else if (val & AST2400_CLK_SOURCE_SEL)
528 freq = 48000000;
529 else
530 freq = 24000000;
531 hw = clk_hw_register_fixed_rate(NULL, "clkin", NULL, 0, freq);
532 pr_debug("clkin @%u MHz\n", freq / 1000000);
533
534 /*
535 * High-speed PLL clock derived from the crystal. This the CPU clock,
536 * and we assume that it is enabled
537 */
538 regmap_read(map, ASPEED_HPLL_PARAM, &val);
539 WARN(val & AST2400_HPLL_STRAPPED, "hpll is strapped not configured");
540 aspeed_clk_data->hws[ASPEED_CLK_HPLL] = aspeed_ast2400_calc_pll("hpll", val);
541
542 /*
543 * Strap bits 11:10 define the CPU/AHB clock frequency ratio (aka HCLK)
544 * 00: Select CPU:AHB = 1:1
545 * 01: Select CPU:AHB = 2:1
546 * 10: Select CPU:AHB = 4:1
547 * 11: Select CPU:AHB = 3:1
548 */
549 regmap_read(map, ASPEED_STRAP, &val);
550 val = (val >> 10) & 0x3;
551 div = val + 1;
552 if (div == 3)
553 div = 4;
554 else if (div == 4)
555 div = 3;
556 hw = clk_hw_register_fixed_factor(NULL, "ahb", "hpll", 0, 1, div);
557 aspeed_clk_data->hws[ASPEED_CLK_AHB] = hw;
558
559 /* APB clock clock selection register SCU08 (aka PCLK) */
560 hw = clk_hw_register_divider_table(NULL, "apb", "hpll", 0,
561 scu_base + ASPEED_CLK_SELECTION, 23, 3, 0,
562 ast2400_div_table,
563 &aspeed_clk_lock);
564 aspeed_clk_data->hws[ASPEED_CLK_APB] = hw;
565}
566
567static void __init aspeed_ast2500_cc(struct regmap *map)
568{
569 struct clk_hw *hw;
570 u32 val, freq, div;
571
572 /* CLKIN is the crystal oscillator, 24 or 25MHz selected by strapping */
573 regmap_read(map, ASPEED_STRAP, &val);
574 if (val & CLKIN_25MHZ_EN)
575 freq = 25000000;
576 else
577 freq = 24000000;
578 hw = clk_hw_register_fixed_rate(NULL, "clkin", NULL, 0, freq);
579 pr_debug("clkin @%u MHz\n", freq / 1000000);
580
581 /*
582 * High-speed PLL clock derived from the crystal. This the CPU clock,
583 * and we assume that it is enabled
584 */
585 regmap_read(map, ASPEED_HPLL_PARAM, &val);
586 aspeed_clk_data->hws[ASPEED_CLK_HPLL] = aspeed_ast2500_calc_pll("hpll", val);
587
588 /* Strap bits 11:9 define the AXI/AHB clock frequency ratio (aka HCLK)*/
589 regmap_read(map, ASPEED_STRAP, &val);
590 val = (val >> 9) & 0x7;
591 WARN(val == 0, "strapping is zero: cannot determine ahb clock");
592 div = 2 * (val + 1);
593 hw = clk_hw_register_fixed_factor(NULL, "ahb", "hpll", 0, 1, div);
594 aspeed_clk_data->hws[ASPEED_CLK_AHB] = hw;
595
596 /* APB clock clock selection register SCU08 (aka PCLK) */
597 regmap_read(map, ASPEED_CLK_SELECTION, &val);
598 val = (val >> 23) & 0x7;
599 div = 4 * (val + 1);
600 hw = clk_hw_register_fixed_factor(NULL, "apb", "hpll", 0, 1, div);
601 aspeed_clk_data->hws[ASPEED_CLK_APB] = hw;
602};
603
Joel Stanley5eda5d72017-12-22 13:15:18 +1030604static void __init aspeed_cc_init(struct device_node *np)
605{
606 struct regmap *map;
607 u32 val;
608 int ret;
609 int i;
610
611 scu_base = of_iomap(np, 0);
Wei Yongjunaccf4752018-01-05 01:41:01 +0000612 if (!scu_base)
Joel Stanley5eda5d72017-12-22 13:15:18 +1030613 return;
614
615 aspeed_clk_data = kzalloc(sizeof(*aspeed_clk_data) +
616 sizeof(*aspeed_clk_data->hws) * ASPEED_NUM_CLKS,
617 GFP_KERNEL);
618 if (!aspeed_clk_data)
619 return;
620
621 /*
622 * This way all clocks fetched before the platform device probes,
623 * except those we assign here for early use, will be deferred.
624 */
625 for (i = 0; i < ASPEED_NUM_CLKS; i++)
626 aspeed_clk_data->hws[i] = ERR_PTR(-EPROBE_DEFER);
627
628 map = syscon_node_to_regmap(np);
629 if (IS_ERR(map)) {
630 pr_err("no syscon regmap\n");
631 return;
632 }
633 /*
634 * We check that the regmap works on this very first access,
635 * but as this is an MMIO-backed regmap, subsequent regmap
636 * access is not going to fail and we skip error checks from
637 * this point.
638 */
639 ret = regmap_read(map, ASPEED_STRAP, &val);
640 if (ret) {
641 pr_err("failed to read strapping register\n");
642 return;
643 }
644
Joel Stanley99d01e02017-12-22 13:15:19 +1030645 if (of_device_is_compatible(np, "aspeed,ast2400-scu"))
646 aspeed_ast2400_cc(map);
647 else if (of_device_is_compatible(np, "aspeed,ast2500-scu"))
648 aspeed_ast2500_cc(map);
649 else
650 pr_err("unknown platform, failed to add clocks\n");
651
Joel Stanley5eda5d72017-12-22 13:15:18 +1030652 aspeed_clk_data->num = ASPEED_NUM_CLKS;
653 ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, aspeed_clk_data);
654 if (ret)
655 pr_err("failed to add DT provider: %d\n", ret);
656};
657CLK_OF_DECLARE_DRIVER(aspeed_cc_g5, "aspeed,ast2500-scu", aspeed_cc_init);
658CLK_OF_DECLARE_DRIVER(aspeed_cc_g4, "aspeed,ast2400-scu", aspeed_cc_init);