1 // SPDX-License-Identifier: GPL-2.0+
3 #define pr_fmt(fmt) "clk-aspeed: " fmt
5 #include <linux/clk-provider.h>
6 #include <linux/mfd/syscon.h>
7 #include <linux/of_address.h>
8 #include <linux/of_device.h>
9 #include <linux/platform_device.h>
10 #include <linux/regmap.h>
11 #include <linux/reset-controller.h>
12 #include <linux/slab.h>
13 #include <linux/spinlock.h>
15 #include <dt-bindings/clock/aspeed-clock.h>
17 #define ASPEED_NUM_CLKS 35
19 #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)
29 #define ASPEED_STRAP 0x70
30 #define CLKIN_25MHZ_EN BIT(23)
31 #define AST2400_CLK_SOURCE_SEL BIT(18)
32 #define ASPEED_CLK_SELECTION_2 0xd8
34 /* Globally visible clocks */
35 static DEFINE_SPINLOCK(aspeed_clk_lock
);
37 /* Keeps track of all clocks */
38 static struct clk_hw_onecell_data
*aspeed_clk_data
;
40 static void __iomem
*scu_base
;
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
51 struct aspeed_gate_data
{
55 const char *parent_name
;
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
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
73 struct aspeed_clk_gate
{
82 #define to_aspeed_clk_gate(_hw) container_of(_hw, struct aspeed_clk_gate, hw)
84 /* TODO: ask Aspeed about the actual parent data */
85 static 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+ */
113 static const struct clk_div_table ast2500_mac_div_table
[] = {
114 { 0x0, 4 }, /* Yep, really. Aspeed confirmed this is correct */
125 static const struct clk_div_table ast2400_div_table
[] = {
137 static const struct clk_div_table ast2500_div_table
[] = {
149 static struct clk_hw
*aspeed_ast2400_calc_pll(const char *name
, u32 val
)
151 unsigned int mult
, div
;
153 if (val
& AST2400_HPLL_BYPASS_EN
) {
154 /* Pass through mode */
157 /* F = 24Mhz * (2-OD) * [(N + 2) / (D + 1)] */
158 u32 n
= (val
>> 5) & 0x3f;
159 u32 od
= (val
>> 4) & 0x1;
162 mult
= (2 - od
) * (n
+ 2);
165 return clk_hw_register_fixed_factor(NULL
, name
, "clkin", 0,
169 static struct clk_hw
*aspeed_ast2500_calc_pll(const char *name
, u32 val
)
171 unsigned int mult
, div
;
173 if (val
& AST2500_HPLL_BYPASS_EN
) {
174 /* Pass through mode */
177 /* F = clkin * [(M+1) / (N+1)] / (P + 1) */
178 u32 p
= (val
>> 13) & 0x3f;
179 u32 m
= (val
>> 5) & 0xff;
182 mult
= (m
+ 1) / (n
+ 1);
186 return clk_hw_register_fixed_factor(NULL
, name
, "clkin", 0,
190 struct 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
);
196 static 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
,
202 static 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
,
208 static int aspeed_clk_enable(struct clk_hw
*hw
)
210 struct aspeed_clk_gate
*gate
= to_aspeed_clk_gate(hw
);
212 u32 clk
= BIT(gate
->clock_idx
);
213 u32 rst
= BIT(gate
->reset_idx
);
216 spin_lock_irqsave(gate
->lock
, flags
);
218 if (gate
->reset_idx
>= 0) {
219 /* Put IP in reset */
220 regmap_update_bits(gate
->map
, ASPEED_RESET_CTRL
, rst
, rst
);
227 enval
= (gate
->flags
& CLK_GATE_SET_TO_DISABLE
) ? 0 : clk
;
228 regmap_update_bits(gate
->map
, ASPEED_CLK_STOP_CTRL
, clk
, enval
);
230 if (gate
->reset_idx
>= 0) {
231 /* A delay of 10ms is specified by the ASPEED docs */
234 /* Take IP out of reset */
235 regmap_update_bits(gate
->map
, ASPEED_RESET_CTRL
, rst
, 0);
238 spin_unlock_irqrestore(gate
->lock
, flags
);
243 static void aspeed_clk_disable(struct clk_hw
*hw
)
245 struct aspeed_clk_gate
*gate
= to_aspeed_clk_gate(hw
);
247 u32 clk
= BIT(gate
->clock_idx
);
250 spin_lock_irqsave(gate
->lock
, flags
);
252 enval
= (gate
->flags
& CLK_GATE_SET_TO_DISABLE
) ? clk
: 0;
253 regmap_update_bits(gate
->map
, ASPEED_CLK_STOP_CTRL
, clk
, enval
);
255 spin_unlock_irqrestore(gate
->lock
, flags
);
258 static int aspeed_clk_is_enabled(struct clk_hw
*hw
)
260 struct aspeed_clk_gate
*gate
= to_aspeed_clk_gate(hw
);
261 u32 clk
= BIT(gate
->clock_idx
);
264 regmap_read(gate
->map
, ASPEED_CLK_STOP_CTRL
, ®
);
266 return (reg
& clk
) ? 0 : 1;
269 static const struct clk_ops aspeed_clk_gate_ops
= {
270 .enable
= aspeed_clk_enable
,
271 .disable
= aspeed_clk_disable
,
272 .is_enabled
= aspeed_clk_is_enabled
,
276 * struct aspeed_reset - Aspeed reset controller
277 * @map: regmap to access the containing system controller
278 * @rcdev: reset controller device
280 struct aspeed_reset
{
282 struct reset_controller_dev rcdev
;
285 #define to_aspeed_reset(p) container_of((p), struct aspeed_reset, rcdev)
287 static const u8 aspeed_resets
[] = {
288 [ASPEED_RESET_XDMA
] = 25,
289 [ASPEED_RESET_MCTP
] = 24,
290 [ASPEED_RESET_ADC
] = 23,
291 [ASPEED_RESET_JTAG_MASTER
] = 22,
292 [ASPEED_RESET_MIC
] = 18,
293 [ASPEED_RESET_PWM
] = 9,
294 [ASPEED_RESET_PCIVGA
] = 8,
295 [ASPEED_RESET_I2C
] = 2,
296 [ASPEED_RESET_AHB
] = 1,
299 static int aspeed_reset_deassert(struct reset_controller_dev
*rcdev
,
302 struct aspeed_reset
*ar
= to_aspeed_reset(rcdev
);
303 u32 rst
= BIT(aspeed_resets
[id
]);
305 return regmap_update_bits(ar
->map
, ASPEED_RESET_CTRL
, rst
, 0);
308 static int aspeed_reset_assert(struct reset_controller_dev
*rcdev
,
311 struct aspeed_reset
*ar
= to_aspeed_reset(rcdev
);
312 u32 rst
= BIT(aspeed_resets
[id
]);
314 return regmap_update_bits(ar
->map
, ASPEED_RESET_CTRL
, rst
, rst
);
317 static int aspeed_reset_status(struct reset_controller_dev
*rcdev
,
320 struct aspeed_reset
*ar
= to_aspeed_reset(rcdev
);
321 u32 val
, rst
= BIT(aspeed_resets
[id
]);
324 ret
= regmap_read(ar
->map
, ASPEED_RESET_CTRL
, &val
);
328 return !!(val
& rst
);
331 static const struct reset_control_ops aspeed_reset_ops
= {
332 .assert = aspeed_reset_assert
,
333 .deassert
= aspeed_reset_deassert
,
334 .status
= aspeed_reset_status
,
337 static struct clk_hw
*aspeed_clk_hw_register_gate(struct device
*dev
,
338 const char *name
, const char *parent_name
, unsigned long flags
,
339 struct regmap
*map
, u8 clock_idx
, u8 reset_idx
,
340 u8 clk_gate_flags
, spinlock_t
*lock
)
342 struct aspeed_clk_gate
*gate
;
343 struct clk_init_data init
;
347 gate
= kzalloc(sizeof(*gate
), GFP_KERNEL
);
349 return ERR_PTR(-ENOMEM
);
352 init
.ops
= &aspeed_clk_gate_ops
;
354 init
.parent_names
= parent_name
? &parent_name
: NULL
;
355 init
.num_parents
= parent_name
? 1 : 0;
358 gate
->clock_idx
= clock_idx
;
359 gate
->reset_idx
= reset_idx
;
360 gate
->flags
= clk_gate_flags
;
362 gate
->hw
.init
= &init
;
365 ret
= clk_hw_register(dev
, hw
);
374 static int aspeed_clk_probe(struct platform_device
*pdev
)
376 const struct aspeed_clk_soc_data
*soc_data
;
377 struct device
*dev
= &pdev
->dev
;
378 struct aspeed_reset
*ar
;
384 map
= syscon_node_to_regmap(dev
->of_node
);
386 dev_err(dev
, "no syscon regmap\n");
390 ar
= devm_kzalloc(dev
, sizeof(*ar
), GFP_KERNEL
);
395 ar
->rcdev
.owner
= THIS_MODULE
;
396 ar
->rcdev
.nr_resets
= ARRAY_SIZE(aspeed_resets
);
397 ar
->rcdev
.ops
= &aspeed_reset_ops
;
398 ar
->rcdev
.of_node
= dev
->of_node
;
400 ret
= devm_reset_controller_register(dev
, &ar
->rcdev
);
402 dev_err(dev
, "could not register reset controller\n");
406 /* SoC generations share common layouts but have different divisors */
407 soc_data
= of_device_get_match_data(dev
);
409 dev_err(dev
, "no match data for platform\n");
413 /* UART clock div13 setting */
414 regmap_read(map
, ASPEED_MISC_CTRL
, &val
);
415 if (val
& UART_DIV13_EN
)
416 rate
= 24000000 / 13;
419 /* TODO: Find the parent data for the uart clock */
420 hw
= clk_hw_register_fixed_rate(dev
, "uart", NULL
, 0, rate
);
423 aspeed_clk_data
->hws
[ASPEED_CLK_UART
] = hw
;
426 * Memory controller (M-PLL) PLL. This clock is configured by the
427 * bootloader, and is exposed to Linux as a read-only clock rate.
429 regmap_read(map
, ASPEED_MPLL_PARAM
, &val
);
430 hw
= soc_data
->calc_pll("mpll", val
);
433 aspeed_clk_data
->hws
[ASPEED_CLK_MPLL
] = hw
;
435 /* SD/SDIO clock divider (TODO: There's a gate too) */
436 hw
= clk_hw_register_divider_table(dev
, "sdio", "hpll", 0,
437 scu_base
+ ASPEED_CLK_SELECTION
, 12, 3, 0,
442 aspeed_clk_data
->hws
[ASPEED_CLK_SDIO
] = hw
;
444 /* MAC AHB bus clock divider */
445 hw
= clk_hw_register_divider_table(dev
, "mac", "hpll", 0,
446 scu_base
+ ASPEED_CLK_SELECTION
, 16, 3, 0,
447 soc_data
->mac_div_table
,
451 aspeed_clk_data
->hws
[ASPEED_CLK_MAC
] = hw
;
453 /* LPC Host (LHCLK) clock divider */
454 hw
= clk_hw_register_divider_table(dev
, "lhclk", "hpll", 0,
455 scu_base
+ ASPEED_CLK_SELECTION
, 20, 3, 0,
460 aspeed_clk_data
->hws
[ASPEED_CLK_LHCLK
] = hw
;
462 /* P-Bus (BCLK) clock divider */
463 hw
= clk_hw_register_divider_table(dev
, "bclk", "hpll", 0,
464 scu_base
+ ASPEED_CLK_SELECTION_2
, 0, 2, 0,
469 aspeed_clk_data
->hws
[ASPEED_CLK_BCLK
] = hw
;
472 * TODO: There are a number of clocks that not included in this driver
473 * as more information is required:
479 * UART[1..5] clock source mux
480 * Video Engine (ECLK) mux and clock divider
483 for (i
= 0; i
< ARRAY_SIZE(aspeed_gates
); i
++) {
484 const struct aspeed_gate_data
*gd
= &aspeed_gates
[i
];
487 /* Special case: the USB port 1 clock (bit 14) is always
488 * working the opposite way from the other ones.
490 gate_flags
= (gd
->clock_idx
== 14) ? 0 : CLK_GATE_SET_TO_DISABLE
;
491 hw
= aspeed_clk_hw_register_gate(dev
,
502 aspeed_clk_data
->hws
[i
] = hw
;
508 static const struct of_device_id aspeed_clk_dt_ids
[] = {
509 { .compatible
= "aspeed,ast2400-scu", .data
= &ast2400_data
},
510 { .compatible
= "aspeed,ast2500-scu", .data
= &ast2500_data
},
514 static struct platform_driver aspeed_clk_driver
= {
515 .probe
= aspeed_clk_probe
,
517 .name
= "aspeed-clk",
518 .of_match_table
= aspeed_clk_dt_ids
,
519 .suppress_bind_attrs
= true,
522 builtin_platform_driver(aspeed_clk_driver
);
524 static void __init
aspeed_ast2400_cc(struct regmap
*map
)
530 * CLKIN is the crystal oscillator, 24, 48 or 25MHz selected by
533 regmap_read(map
, ASPEED_STRAP
, &val
);
534 if (val
& CLKIN_25MHZ_EN
)
536 else if (val
& AST2400_CLK_SOURCE_SEL
)
540 hw
= clk_hw_register_fixed_rate(NULL
, "clkin", NULL
, 0, freq
);
541 pr_debug("clkin @%u MHz\n", freq
/ 1000000);
544 * High-speed PLL clock derived from the crystal. This the CPU clock,
545 * and we assume that it is enabled
547 regmap_read(map
, ASPEED_HPLL_PARAM
, &val
);
548 WARN(val
& AST2400_HPLL_STRAPPED
, "hpll is strapped not configured");
549 aspeed_clk_data
->hws
[ASPEED_CLK_HPLL
] = aspeed_ast2400_calc_pll("hpll", val
);
552 * Strap bits 11:10 define the CPU/AHB clock frequency ratio (aka HCLK)
553 * 00: Select CPU:AHB = 1:1
554 * 01: Select CPU:AHB = 2:1
555 * 10: Select CPU:AHB = 4:1
556 * 11: Select CPU:AHB = 3:1
558 regmap_read(map
, ASPEED_STRAP
, &val
);
559 val
= (val
>> 10) & 0x3;
565 hw
= clk_hw_register_fixed_factor(NULL
, "ahb", "hpll", 0, 1, div
);
566 aspeed_clk_data
->hws
[ASPEED_CLK_AHB
] = hw
;
568 /* APB clock clock selection register SCU08 (aka PCLK) */
569 hw
= clk_hw_register_divider_table(NULL
, "apb", "hpll", 0,
570 scu_base
+ ASPEED_CLK_SELECTION
, 23, 3, 0,
573 aspeed_clk_data
->hws
[ASPEED_CLK_APB
] = hw
;
576 static void __init
aspeed_ast2500_cc(struct regmap
*map
)
581 /* CLKIN is the crystal oscillator, 24 or 25MHz selected by strapping */
582 regmap_read(map
, ASPEED_STRAP
, &val
);
583 if (val
& CLKIN_25MHZ_EN
)
587 hw
= clk_hw_register_fixed_rate(NULL
, "clkin", NULL
, 0, freq
);
588 pr_debug("clkin @%u MHz\n", freq
/ 1000000);
591 * High-speed PLL clock derived from the crystal. This the CPU clock,
592 * and we assume that it is enabled
594 regmap_read(map
, ASPEED_HPLL_PARAM
, &val
);
595 aspeed_clk_data
->hws
[ASPEED_CLK_HPLL
] = aspeed_ast2500_calc_pll("hpll", val
);
597 /* Strap bits 11:9 define the AXI/AHB clock frequency ratio (aka HCLK)*/
598 regmap_read(map
, ASPEED_STRAP
, &val
);
599 val
= (val
>> 9) & 0x7;
600 WARN(val
== 0, "strapping is zero: cannot determine ahb clock");
602 hw
= clk_hw_register_fixed_factor(NULL
, "ahb", "hpll", 0, 1, div
);
603 aspeed_clk_data
->hws
[ASPEED_CLK_AHB
] = hw
;
605 /* APB clock clock selection register SCU08 (aka PCLK) */
606 regmap_read(map
, ASPEED_CLK_SELECTION
, &val
);
607 val
= (val
>> 23) & 0x7;
609 hw
= clk_hw_register_fixed_factor(NULL
, "apb", "hpll", 0, 1, div
);
610 aspeed_clk_data
->hws
[ASPEED_CLK_APB
] = hw
;
613 static void __init
aspeed_cc_init(struct device_node
*np
)
620 scu_base
= of_iomap(np
, 0);
624 aspeed_clk_data
= kzalloc(sizeof(*aspeed_clk_data
) +
625 sizeof(*aspeed_clk_data
->hws
) * ASPEED_NUM_CLKS
,
627 if (!aspeed_clk_data
)
631 * This way all clocks fetched before the platform device probes,
632 * except those we assign here for early use, will be deferred.
634 for (i
= 0; i
< ASPEED_NUM_CLKS
; i
++)
635 aspeed_clk_data
->hws
[i
] = ERR_PTR(-EPROBE_DEFER
);
637 map
= syscon_node_to_regmap(np
);
639 pr_err("no syscon regmap\n");
643 * We check that the regmap works on this very first access,
644 * but as this is an MMIO-backed regmap, subsequent regmap
645 * access is not going to fail and we skip error checks from
648 ret
= regmap_read(map
, ASPEED_STRAP
, &val
);
650 pr_err("failed to read strapping register\n");
654 if (of_device_is_compatible(np
, "aspeed,ast2400-scu"))
655 aspeed_ast2400_cc(map
);
656 else if (of_device_is_compatible(np
, "aspeed,ast2500-scu"))
657 aspeed_ast2500_cc(map
);
659 pr_err("unknown platform, failed to add clocks\n");
661 aspeed_clk_data
->num
= ASPEED_NUM_CLKS
;
662 ret
= of_clk_add_hw_provider(np
, of_clk_hw_onecell_get
, aspeed_clk_data
);
664 pr_err("failed to add DT provider: %d\n", ret
);
666 CLK_OF_DECLARE_DRIVER(aspeed_cc_g5
, "aspeed,ast2500-scu", aspeed_cc_init
);
667 CLK_OF_DECLARE_DRIVER(aspeed_cc_g4
, "aspeed,ast2400-scu", aspeed_cc_init
);