xref: /openbmc/linux/drivers/clk/clk-aspeed.c (revision 565b9937f44d5ab7956339b6c105c03471ce3243)
15eda5d79SJoel Stanley // SPDX-License-Identifier: GPL-2.0+
25eda5d79SJoel Stanley 
35eda5d79SJoel Stanley #define pr_fmt(fmt) "clk-aspeed: " fmt
45eda5d79SJoel Stanley 
55eda5d79SJoel Stanley #include <linux/clk-provider.h>
65eda5d79SJoel Stanley #include <linux/mfd/syscon.h>
75eda5d79SJoel Stanley #include <linux/of_address.h>
898f3118dSJoel Stanley #include <linux/of_device.h>
998f3118dSJoel Stanley #include <linux/platform_device.h>
105eda5d79SJoel Stanley #include <linux/regmap.h>
11f7989839SJoel Stanley #include <linux/reset-controller.h>
125eda5d79SJoel Stanley #include <linux/slab.h>
135eda5d79SJoel Stanley #include <linux/spinlock.h>
145eda5d79SJoel Stanley 
155eda5d79SJoel Stanley #include <dt-bindings/clock/aspeed-clock.h>
165eda5d79SJoel Stanley 
1767b6e5cfSLei YU #define ASPEED_NUM_CLKS		36
185eda5d79SJoel Stanley 
19dcb899c4SJoel Stanley #define ASPEED_RESET2_OFFSET	32
205eda5d79SJoel Stanley 
2199d01e0eSJoel Stanley #define ASPEED_RESET_CTRL	0x04
2299d01e0eSJoel Stanley #define ASPEED_CLK_SELECTION	0x08
2399d01e0eSJoel Stanley #define ASPEED_CLK_STOP_CTRL	0x0c
2499d01e0eSJoel Stanley #define ASPEED_MPLL_PARAM	0x20
2599d01e0eSJoel Stanley #define ASPEED_HPLL_PARAM	0x24
2699d01e0eSJoel Stanley #define  AST2500_HPLL_BYPASS_EN	BIT(20)
27*565b9937SJoel Stanley #define  AST2400_HPLL_PROGRAMMED BIT(18)
2899d01e0eSJoel Stanley #define  AST2400_HPLL_BYPASS_EN	BIT(17)
2999d01e0eSJoel Stanley #define ASPEED_MISC_CTRL	0x2c
3099d01e0eSJoel Stanley #define  UART_DIV13_EN		BIT(12)
315eda5d79SJoel Stanley #define ASPEED_STRAP		0x70
3299d01e0eSJoel Stanley #define  CLKIN_25MHZ_EN		BIT(23)
3399d01e0eSJoel Stanley #define  AST2400_CLK_SOURCE_SEL	BIT(18)
3499d01e0eSJoel Stanley #define ASPEED_CLK_SELECTION_2	0xd8
35dcb899c4SJoel Stanley #define ASPEED_RESET_CTRL2	0xd4
3699d01e0eSJoel Stanley 
3799d01e0eSJoel Stanley /* Globally visible clocks */
3899d01e0eSJoel Stanley static DEFINE_SPINLOCK(aspeed_clk_lock);
395eda5d79SJoel Stanley 
405eda5d79SJoel Stanley /* Keeps track of all clocks */
415eda5d79SJoel Stanley static struct clk_hw_onecell_data *aspeed_clk_data;
425eda5d79SJoel Stanley 
435eda5d79SJoel Stanley static void __iomem *scu_base;
445eda5d79SJoel Stanley 
455eda5d79SJoel Stanley /**
465eda5d79SJoel Stanley  * struct aspeed_gate_data - Aspeed gated clocks
475eda5d79SJoel Stanley  * @clock_idx: bit used to gate this clock in the clock register
485eda5d79SJoel Stanley  * @reset_idx: bit used to reset this IP in the reset register. -1 if no
495eda5d79SJoel Stanley  *             reset is required when enabling the clock
505eda5d79SJoel Stanley  * @name: the clock name
515eda5d79SJoel Stanley  * @parent_name: the name of the parent clock
525eda5d79SJoel Stanley  * @flags: standard clock framework flags
535eda5d79SJoel Stanley  */
545eda5d79SJoel Stanley struct aspeed_gate_data {
555eda5d79SJoel Stanley 	u8		clock_idx;
565eda5d79SJoel Stanley 	s8		reset_idx;
575eda5d79SJoel Stanley 	const char	*name;
585eda5d79SJoel Stanley 	const char	*parent_name;
595eda5d79SJoel Stanley 	unsigned long	flags;
605eda5d79SJoel Stanley };
615eda5d79SJoel Stanley 
625eda5d79SJoel Stanley /**
635eda5d79SJoel Stanley  * struct aspeed_clk_gate - Aspeed specific clk_gate structure
645eda5d79SJoel Stanley  * @hw:		handle between common and hardware-specific interfaces
655eda5d79SJoel Stanley  * @reg:	register controlling gate
665eda5d79SJoel Stanley  * @clock_idx:	bit used to gate this clock in the clock register
675eda5d79SJoel Stanley  * @reset_idx:	bit used to reset this IP in the reset register. -1 if no
685eda5d79SJoel Stanley  *		reset is required when enabling the clock
695eda5d79SJoel Stanley  * @flags:	hardware-specific flags
705eda5d79SJoel Stanley  * @lock:	register lock
715eda5d79SJoel Stanley  *
725eda5d79SJoel Stanley  * Some of the clocks in the Aspeed SoC must be put in reset before enabling.
735eda5d79SJoel Stanley  * This modified version of clk_gate allows an optional reset bit to be
745eda5d79SJoel Stanley  * specified.
755eda5d79SJoel Stanley  */
765eda5d79SJoel Stanley struct aspeed_clk_gate {
775eda5d79SJoel Stanley 	struct clk_hw	hw;
785eda5d79SJoel Stanley 	struct regmap	*map;
795eda5d79SJoel Stanley 	u8		clock_idx;
805eda5d79SJoel Stanley 	s8		reset_idx;
815eda5d79SJoel Stanley 	u8		flags;
825eda5d79SJoel Stanley 	spinlock_t	*lock;
835eda5d79SJoel Stanley };
845eda5d79SJoel Stanley 
855eda5d79SJoel Stanley #define to_aspeed_clk_gate(_hw) container_of(_hw, struct aspeed_clk_gate, hw)
865eda5d79SJoel Stanley 
875eda5d79SJoel Stanley /* TODO: ask Aspeed about the actual parent data */
885eda5d79SJoel Stanley static const struct aspeed_gate_data aspeed_gates[] = {
895eda5d79SJoel Stanley 	/*				 clk rst   name			parent	flags */
905eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_ECLK] =	{  0, -1, "eclk-gate",		"eclk",	0 }, /* Video Engine */
915eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_GCLK] =	{  1,  7, "gclk-gate",		NULL,	0 }, /* 2D engine */
925eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_MCLK] =	{  2, -1, "mclk-gate",		"mpll",	CLK_IS_CRITICAL }, /* SDRAM */
935eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_VCLK] =	{  3,  6, "vclk-gate",		NULL,	0 }, /* Video Capture */
94974c7c6dSJoel Stanley 	[ASPEED_CLK_GATE_BCLK] =	{  4,  8, "bclk-gate",		"bclk",	CLK_IS_CRITICAL }, /* PCIe/PCI */
95974c7c6dSJoel Stanley 	[ASPEED_CLK_GATE_DCLK] =	{  5, -1, "dclk-gate",		NULL,	CLK_IS_CRITICAL }, /* DAC */
965eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_REFCLK] =	{  6, -1, "refclk-gate",	"clkin", CLK_IS_CRITICAL },
975eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_USBPORT2CLK] =	{  7,  3, "usb-port2-gate",	NULL,	0 }, /* USB2.0 Host port 2 */
985eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_LCLK] =	{  8,  5, "lclk-gate",		NULL,	0 }, /* LPC */
995eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_USBUHCICLK] =	{  9, 15, "usb-uhci-gate",	NULL,	0 }, /* USB1.1 (requires port 2 enabled) */
1005eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_D1CLK] =	{ 10, 13, "d1clk-gate",		NULL,	0 }, /* GFX CRT */
1015eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_YCLK] =	{ 13,  4, "yclk-gate",		NULL,	0 }, /* HAC */
1025eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_USBPORT1CLK] = { 14, 14, "usb-port1-gate",	NULL,	0 }, /* USB2 hub/USB2 host port 1/USB1.1 dev */
1035eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_UART1CLK] =	{ 15, -1, "uart1clk-gate",	"uart",	0 }, /* UART1 */
1045eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_UART2CLK] =	{ 16, -1, "uart2clk-gate",	"uart",	0 }, /* UART2 */
1055eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_UART5CLK] =	{ 17, -1, "uart5clk-gate",	"uart",	0 }, /* UART5 */
1065eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_ESPICLK] =	{ 19, -1, "espiclk-gate",	NULL,	0 }, /* eSPI */
1075eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_MAC1CLK] =	{ 20, 11, "mac1clk-gate",	"mac",	0 }, /* MAC1 */
1085eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_MAC2CLK] =	{ 21, 12, "mac2clk-gate",	"mac",	0 }, /* MAC2 */
1095eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_RSACLK] =	{ 24, -1, "rsaclk-gate",	NULL,	0 }, /* RSA */
1105eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_UART3CLK] =	{ 25, -1, "uart3clk-gate",	"uart",	0 }, /* UART3 */
1115eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_UART4CLK] =	{ 26, -1, "uart4clk-gate",	"uart",	0 }, /* UART4 */
1125eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_SDCLKCLK] =	{ 27, 16, "sdclk-gate",		NULL,	0 }, /* SDIO/SD */
1135eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_LHCCLK] =	{ 28, -1, "lhclk-gate",		"lhclk", 0 }, /* LPC master/LPC+ */
1145eda5d79SJoel Stanley };
1155eda5d79SJoel Stanley 
11698f3118dSJoel Stanley static const struct clk_div_table ast2500_mac_div_table[] = {
11798f3118dSJoel Stanley 	{ 0x0, 4 }, /* Yep, really. Aspeed confirmed this is correct */
11898f3118dSJoel Stanley 	{ 0x1, 4 },
11998f3118dSJoel Stanley 	{ 0x2, 6 },
12098f3118dSJoel Stanley 	{ 0x3, 8 },
12198f3118dSJoel Stanley 	{ 0x4, 10 },
12298f3118dSJoel Stanley 	{ 0x5, 12 },
12398f3118dSJoel Stanley 	{ 0x6, 14 },
12498f3118dSJoel Stanley 	{ 0x7, 16 },
12598f3118dSJoel Stanley 	{ 0 }
12698f3118dSJoel Stanley };
12798f3118dSJoel Stanley 
12899d01e0eSJoel Stanley static const struct clk_div_table ast2400_div_table[] = {
12999d01e0eSJoel Stanley 	{ 0x0, 2 },
13099d01e0eSJoel Stanley 	{ 0x1, 4 },
13199d01e0eSJoel Stanley 	{ 0x2, 6 },
13299d01e0eSJoel Stanley 	{ 0x3, 8 },
13399d01e0eSJoel Stanley 	{ 0x4, 10 },
13499d01e0eSJoel Stanley 	{ 0x5, 12 },
13599d01e0eSJoel Stanley 	{ 0x6, 14 },
13699d01e0eSJoel Stanley 	{ 0x7, 16 },
13799d01e0eSJoel Stanley 	{ 0 }
13899d01e0eSJoel Stanley };
13999d01e0eSJoel Stanley 
14099d01e0eSJoel Stanley static const struct clk_div_table ast2500_div_table[] = {
14199d01e0eSJoel Stanley 	{ 0x0, 4 },
14299d01e0eSJoel Stanley 	{ 0x1, 8 },
14399d01e0eSJoel Stanley 	{ 0x2, 12 },
14499d01e0eSJoel Stanley 	{ 0x3, 16 },
14599d01e0eSJoel Stanley 	{ 0x4, 20 },
14699d01e0eSJoel Stanley 	{ 0x5, 24 },
14799d01e0eSJoel Stanley 	{ 0x6, 28 },
14899d01e0eSJoel Stanley 	{ 0x7, 32 },
14999d01e0eSJoel Stanley 	{ 0 }
15099d01e0eSJoel Stanley };
15199d01e0eSJoel Stanley 
15299d01e0eSJoel Stanley static struct clk_hw *aspeed_ast2400_calc_pll(const char *name, u32 val)
15399d01e0eSJoel Stanley {
15499d01e0eSJoel Stanley 	unsigned int mult, div;
15599d01e0eSJoel Stanley 
15699d01e0eSJoel Stanley 	if (val & AST2400_HPLL_BYPASS_EN) {
15799d01e0eSJoel Stanley 		/* Pass through mode */
15899d01e0eSJoel Stanley 		mult = div = 1;
15999d01e0eSJoel Stanley 	} else {
16099d01e0eSJoel Stanley 		/* F = 24Mhz * (2-OD) * [(N + 2) / (D + 1)] */
16199d01e0eSJoel Stanley 		u32 n = (val >> 5) & 0x3f;
16299d01e0eSJoel Stanley 		u32 od = (val >> 4) & 0x1;
16399d01e0eSJoel Stanley 		u32 d = val & 0xf;
16499d01e0eSJoel Stanley 
16599d01e0eSJoel Stanley 		mult = (2 - od) * (n + 2);
16699d01e0eSJoel Stanley 		div = d + 1;
16799d01e0eSJoel Stanley 	}
16899d01e0eSJoel Stanley 	return clk_hw_register_fixed_factor(NULL, name, "clkin", 0,
16999d01e0eSJoel Stanley 			mult, div);
17099d01e0eSJoel Stanley };
17199d01e0eSJoel Stanley 
17299d01e0eSJoel Stanley static struct clk_hw *aspeed_ast2500_calc_pll(const char *name, u32 val)
17399d01e0eSJoel Stanley {
17499d01e0eSJoel Stanley 	unsigned int mult, div;
17599d01e0eSJoel Stanley 
17699d01e0eSJoel Stanley 	if (val & AST2500_HPLL_BYPASS_EN) {
17799d01e0eSJoel Stanley 		/* Pass through mode */
17899d01e0eSJoel Stanley 		mult = div = 1;
17999d01e0eSJoel Stanley 	} else {
18099d01e0eSJoel Stanley 		/* F = clkin * [(M+1) / (N+1)] / (P + 1) */
18199d01e0eSJoel Stanley 		u32 p = (val >> 13) & 0x3f;
18299d01e0eSJoel Stanley 		u32 m = (val >> 5) & 0xff;
18399d01e0eSJoel Stanley 		u32 n = val & 0x1f;
18499d01e0eSJoel Stanley 
18599d01e0eSJoel Stanley 		mult = (m + 1) / (n + 1);
18699d01e0eSJoel Stanley 		div = p + 1;
18799d01e0eSJoel Stanley 	}
18899d01e0eSJoel Stanley 
18999d01e0eSJoel Stanley 	return clk_hw_register_fixed_factor(NULL, name, "clkin", 0,
19099d01e0eSJoel Stanley 			mult, div);
19199d01e0eSJoel Stanley }
19299d01e0eSJoel Stanley 
19398f3118dSJoel Stanley struct aspeed_clk_soc_data {
19498f3118dSJoel Stanley 	const struct clk_div_table *div_table;
19598f3118dSJoel Stanley 	const struct clk_div_table *mac_div_table;
19698f3118dSJoel Stanley 	struct clk_hw *(*calc_pll)(const char *name, u32 val);
19798f3118dSJoel Stanley };
19898f3118dSJoel Stanley 
19998f3118dSJoel Stanley static const struct aspeed_clk_soc_data ast2500_data = {
20098f3118dSJoel Stanley 	.div_table = ast2500_div_table,
20198f3118dSJoel Stanley 	.mac_div_table = ast2500_mac_div_table,
20298f3118dSJoel Stanley 	.calc_pll = aspeed_ast2500_calc_pll,
20398f3118dSJoel Stanley };
20498f3118dSJoel Stanley 
20598f3118dSJoel Stanley static const struct aspeed_clk_soc_data ast2400_data = {
20698f3118dSJoel Stanley 	.div_table = ast2400_div_table,
20798f3118dSJoel Stanley 	.mac_div_table = ast2400_div_table,
20898f3118dSJoel Stanley 	.calc_pll = aspeed_ast2400_calc_pll,
20998f3118dSJoel Stanley };
21098f3118dSJoel Stanley 
2118a53fc51SEddie James static int aspeed_clk_is_enabled(struct clk_hw *hw)
2128a53fc51SEddie James {
2138a53fc51SEddie James 	struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
2148a53fc51SEddie James 	u32 clk = BIT(gate->clock_idx);
215edc6f7e9SBenjamin Herrenschmidt 	u32 rst = BIT(gate->reset_idx);
2168a53fc51SEddie James 	u32 enval = (gate->flags & CLK_GATE_SET_TO_DISABLE) ? 0 : clk;
2178a53fc51SEddie James 	u32 reg;
2188a53fc51SEddie James 
219edc6f7e9SBenjamin Herrenschmidt 	/*
220edc6f7e9SBenjamin Herrenschmidt 	 * If the IP is in reset, treat the clock as not enabled,
221edc6f7e9SBenjamin Herrenschmidt 	 * this happens with some clocks such as the USB one when
222edc6f7e9SBenjamin Herrenschmidt 	 * coming from cold reset. Without this, aspeed_clk_enable()
223edc6f7e9SBenjamin Herrenschmidt 	 * will fail to lift the reset.
224edc6f7e9SBenjamin Herrenschmidt 	 */
225edc6f7e9SBenjamin Herrenschmidt 	if (gate->reset_idx >= 0) {
226edc6f7e9SBenjamin Herrenschmidt 		regmap_read(gate->map, ASPEED_RESET_CTRL, &reg);
227edc6f7e9SBenjamin Herrenschmidt 		if (reg & rst)
228edc6f7e9SBenjamin Herrenschmidt 			return 0;
229edc6f7e9SBenjamin Herrenschmidt 	}
230edc6f7e9SBenjamin Herrenschmidt 
2318a53fc51SEddie James 	regmap_read(gate->map, ASPEED_CLK_STOP_CTRL, &reg);
2328a53fc51SEddie James 
2338a53fc51SEddie James 	return ((reg & clk) == enval) ? 1 : 0;
2348a53fc51SEddie James }
2358a53fc51SEddie James 
23615ed8ce5SJoel Stanley static int aspeed_clk_enable(struct clk_hw *hw)
23715ed8ce5SJoel Stanley {
23815ed8ce5SJoel Stanley 	struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
23915ed8ce5SJoel Stanley 	unsigned long flags;
24015ed8ce5SJoel Stanley 	u32 clk = BIT(gate->clock_idx);
24115ed8ce5SJoel Stanley 	u32 rst = BIT(gate->reset_idx);
2426671507fSBenjamin Herrenschmidt 	u32 enval;
24315ed8ce5SJoel Stanley 
24415ed8ce5SJoel Stanley 	spin_lock_irqsave(gate->lock, flags);
24515ed8ce5SJoel Stanley 
2468a53fc51SEddie James 	if (aspeed_clk_is_enabled(hw)) {
2478a53fc51SEddie James 		spin_unlock_irqrestore(gate->lock, flags);
2488a53fc51SEddie James 		return 0;
2498a53fc51SEddie James 	}
2508a53fc51SEddie James 
25115ed8ce5SJoel Stanley 	if (gate->reset_idx >= 0) {
25215ed8ce5SJoel Stanley 		/* Put IP in reset */
25315ed8ce5SJoel Stanley 		regmap_update_bits(gate->map, ASPEED_RESET_CTRL, rst, rst);
25415ed8ce5SJoel Stanley 
25515ed8ce5SJoel Stanley 		/* Delay 100us */
25615ed8ce5SJoel Stanley 		udelay(100);
25715ed8ce5SJoel Stanley 	}
25815ed8ce5SJoel Stanley 
25915ed8ce5SJoel Stanley 	/* Enable clock */
2606671507fSBenjamin Herrenschmidt 	enval = (gate->flags & CLK_GATE_SET_TO_DISABLE) ? 0 : clk;
2616671507fSBenjamin Herrenschmidt 	regmap_update_bits(gate->map, ASPEED_CLK_STOP_CTRL, clk, enval);
26215ed8ce5SJoel Stanley 
26315ed8ce5SJoel Stanley 	if (gate->reset_idx >= 0) {
26415ed8ce5SJoel Stanley 		/* A delay of 10ms is specified by the ASPEED docs */
26515ed8ce5SJoel Stanley 		mdelay(10);
26615ed8ce5SJoel Stanley 
26715ed8ce5SJoel Stanley 		/* Take IP out of reset */
26815ed8ce5SJoel Stanley 		regmap_update_bits(gate->map, ASPEED_RESET_CTRL, rst, 0);
26915ed8ce5SJoel Stanley 	}
27015ed8ce5SJoel Stanley 
27115ed8ce5SJoel Stanley 	spin_unlock_irqrestore(gate->lock, flags);
27215ed8ce5SJoel Stanley 
27315ed8ce5SJoel Stanley 	return 0;
27415ed8ce5SJoel Stanley }
27515ed8ce5SJoel Stanley 
27615ed8ce5SJoel Stanley static void aspeed_clk_disable(struct clk_hw *hw)
27715ed8ce5SJoel Stanley {
27815ed8ce5SJoel Stanley 	struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
27915ed8ce5SJoel Stanley 	unsigned long flags;
28015ed8ce5SJoel Stanley 	u32 clk = BIT(gate->clock_idx);
2816671507fSBenjamin Herrenschmidt 	u32 enval;
28215ed8ce5SJoel Stanley 
28315ed8ce5SJoel Stanley 	spin_lock_irqsave(gate->lock, flags);
28415ed8ce5SJoel Stanley 
2856671507fSBenjamin Herrenschmidt 	enval = (gate->flags & CLK_GATE_SET_TO_DISABLE) ? clk : 0;
2866671507fSBenjamin Herrenschmidt 	regmap_update_bits(gate->map, ASPEED_CLK_STOP_CTRL, clk, enval);
28715ed8ce5SJoel Stanley 
28815ed8ce5SJoel Stanley 	spin_unlock_irqrestore(gate->lock, flags);
28915ed8ce5SJoel Stanley }
29015ed8ce5SJoel Stanley 
29115ed8ce5SJoel Stanley static const struct clk_ops aspeed_clk_gate_ops = {
29215ed8ce5SJoel Stanley 	.enable = aspeed_clk_enable,
29315ed8ce5SJoel Stanley 	.disable = aspeed_clk_disable,
29415ed8ce5SJoel Stanley 	.is_enabled = aspeed_clk_is_enabled,
29515ed8ce5SJoel Stanley };
29615ed8ce5SJoel Stanley 
297f7989839SJoel Stanley /**
298f7989839SJoel Stanley  * struct aspeed_reset - Aspeed reset controller
299f7989839SJoel Stanley  * @map: regmap to access the containing system controller
300f7989839SJoel Stanley  * @rcdev: reset controller device
301f7989839SJoel Stanley  */
302f7989839SJoel Stanley struct aspeed_reset {
303f7989839SJoel Stanley 	struct regmap			*map;
304f7989839SJoel Stanley 	struct reset_controller_dev	rcdev;
305f7989839SJoel Stanley };
306f7989839SJoel Stanley 
307f7989839SJoel Stanley #define to_aspeed_reset(p) container_of((p), struct aspeed_reset, rcdev)
308f7989839SJoel Stanley 
309f7989839SJoel Stanley static const u8 aspeed_resets[] = {
310dcb899c4SJoel Stanley 	/* SCU04 resets */
311f7989839SJoel Stanley 	[ASPEED_RESET_XDMA]	= 25,
312f7989839SJoel Stanley 	[ASPEED_RESET_MCTP]	= 24,
313f7989839SJoel Stanley 	[ASPEED_RESET_ADC]	= 23,
314f7989839SJoel Stanley 	[ASPEED_RESET_JTAG_MASTER] = 22,
315f7989839SJoel Stanley 	[ASPEED_RESET_MIC]	= 18,
316f7989839SJoel Stanley 	[ASPEED_RESET_PWM]	=  9,
317e76e5682SJae Hyun Yoo 	[ASPEED_RESET_PECI]	= 10,
318f7989839SJoel Stanley 	[ASPEED_RESET_I2C]	=  2,
319f7989839SJoel Stanley 	[ASPEED_RESET_AHB]	=  1,
320dcb899c4SJoel Stanley 
321dcb899c4SJoel Stanley 	/*
322dcb899c4SJoel Stanley 	 * SCUD4 resets start at an offset to separate them from
323dcb899c4SJoel Stanley 	 * the SCU04 resets.
324dcb899c4SJoel Stanley 	 */
325dcb899c4SJoel Stanley 	[ASPEED_RESET_CRT1]	= ASPEED_RESET2_OFFSET + 5,
326f7989839SJoel Stanley };
327f7989839SJoel Stanley 
328f7989839SJoel Stanley static int aspeed_reset_deassert(struct reset_controller_dev *rcdev,
329f7989839SJoel Stanley 				 unsigned long id)
330f7989839SJoel Stanley {
331f7989839SJoel Stanley 	struct aspeed_reset *ar = to_aspeed_reset(rcdev);
332dcb899c4SJoel Stanley 	u32 reg = ASPEED_RESET_CTRL;
333dcb899c4SJoel Stanley 	u32 bit = aspeed_resets[id];
334f7989839SJoel Stanley 
335dcb899c4SJoel Stanley 	if (bit >= ASPEED_RESET2_OFFSET) {
336dcb899c4SJoel Stanley 		bit -= ASPEED_RESET2_OFFSET;
337dcb899c4SJoel Stanley 		reg = ASPEED_RESET_CTRL2;
338dcb899c4SJoel Stanley 	}
339dcb899c4SJoel Stanley 
340dcb899c4SJoel Stanley 	return regmap_update_bits(ar->map, reg, BIT(bit), 0);
341f7989839SJoel Stanley }
342f7989839SJoel Stanley 
343f7989839SJoel Stanley static int aspeed_reset_assert(struct reset_controller_dev *rcdev,
344f7989839SJoel Stanley 			       unsigned long id)
345f7989839SJoel Stanley {
346f7989839SJoel Stanley 	struct aspeed_reset *ar = to_aspeed_reset(rcdev);
347dcb899c4SJoel Stanley 	u32 reg = ASPEED_RESET_CTRL;
348dcb899c4SJoel Stanley 	u32 bit = aspeed_resets[id];
349f7989839SJoel Stanley 
350dcb899c4SJoel Stanley 	if (bit >= ASPEED_RESET2_OFFSET) {
351dcb899c4SJoel Stanley 		bit -= ASPEED_RESET2_OFFSET;
352dcb899c4SJoel Stanley 		reg = ASPEED_RESET_CTRL2;
353dcb899c4SJoel Stanley 	}
354dcb899c4SJoel Stanley 
355dcb899c4SJoel Stanley 	return regmap_update_bits(ar->map, reg, BIT(bit), BIT(bit));
356f7989839SJoel Stanley }
357f7989839SJoel Stanley 
358f7989839SJoel Stanley static int aspeed_reset_status(struct reset_controller_dev *rcdev,
359f7989839SJoel Stanley 			       unsigned long id)
360f7989839SJoel Stanley {
361f7989839SJoel Stanley 	struct aspeed_reset *ar = to_aspeed_reset(rcdev);
362dcb899c4SJoel Stanley 	u32 reg = ASPEED_RESET_CTRL;
363dcb899c4SJoel Stanley 	u32 bit = aspeed_resets[id];
364dcb899c4SJoel Stanley 	int ret, val;
365f7989839SJoel Stanley 
366dcb899c4SJoel Stanley 	if (bit >= ASPEED_RESET2_OFFSET) {
367dcb899c4SJoel Stanley 		bit -= ASPEED_RESET2_OFFSET;
368dcb899c4SJoel Stanley 		reg = ASPEED_RESET_CTRL2;
369dcb899c4SJoel Stanley 	}
370dcb899c4SJoel Stanley 
371dcb899c4SJoel Stanley 	ret = regmap_read(ar->map, reg, &val);
372f7989839SJoel Stanley 	if (ret)
373f7989839SJoel Stanley 		return ret;
374f7989839SJoel Stanley 
375dcb899c4SJoel Stanley 	return !!(val & BIT(bit));
376f7989839SJoel Stanley }
377f7989839SJoel Stanley 
378f7989839SJoel Stanley static const struct reset_control_ops aspeed_reset_ops = {
379f7989839SJoel Stanley 	.assert = aspeed_reset_assert,
380f7989839SJoel Stanley 	.deassert = aspeed_reset_deassert,
381f7989839SJoel Stanley 	.status = aspeed_reset_status,
382f7989839SJoel Stanley };
383f7989839SJoel Stanley 
38415ed8ce5SJoel Stanley static struct clk_hw *aspeed_clk_hw_register_gate(struct device *dev,
38515ed8ce5SJoel Stanley 		const char *name, const char *parent_name, unsigned long flags,
38615ed8ce5SJoel Stanley 		struct regmap *map, u8 clock_idx, u8 reset_idx,
38715ed8ce5SJoel Stanley 		u8 clk_gate_flags, spinlock_t *lock)
38815ed8ce5SJoel Stanley {
38915ed8ce5SJoel Stanley 	struct aspeed_clk_gate *gate;
39015ed8ce5SJoel Stanley 	struct clk_init_data init;
39115ed8ce5SJoel Stanley 	struct clk_hw *hw;
39215ed8ce5SJoel Stanley 	int ret;
39315ed8ce5SJoel Stanley 
39415ed8ce5SJoel Stanley 	gate = kzalloc(sizeof(*gate), GFP_KERNEL);
39515ed8ce5SJoel Stanley 	if (!gate)
39615ed8ce5SJoel Stanley 		return ERR_PTR(-ENOMEM);
39715ed8ce5SJoel Stanley 
39815ed8ce5SJoel Stanley 	init.name = name;
39915ed8ce5SJoel Stanley 	init.ops = &aspeed_clk_gate_ops;
40015ed8ce5SJoel Stanley 	init.flags = flags;
40115ed8ce5SJoel Stanley 	init.parent_names = parent_name ? &parent_name : NULL;
40215ed8ce5SJoel Stanley 	init.num_parents = parent_name ? 1 : 0;
40315ed8ce5SJoel Stanley 
40415ed8ce5SJoel Stanley 	gate->map = map;
40515ed8ce5SJoel Stanley 	gate->clock_idx = clock_idx;
40615ed8ce5SJoel Stanley 	gate->reset_idx = reset_idx;
40715ed8ce5SJoel Stanley 	gate->flags = clk_gate_flags;
40815ed8ce5SJoel Stanley 	gate->lock = lock;
40915ed8ce5SJoel Stanley 	gate->hw.init = &init;
41015ed8ce5SJoel Stanley 
41115ed8ce5SJoel Stanley 	hw = &gate->hw;
41215ed8ce5SJoel Stanley 	ret = clk_hw_register(dev, hw);
41315ed8ce5SJoel Stanley 	if (ret) {
41415ed8ce5SJoel Stanley 		kfree(gate);
41515ed8ce5SJoel Stanley 		hw = ERR_PTR(ret);
41615ed8ce5SJoel Stanley 	}
41715ed8ce5SJoel Stanley 
41815ed8ce5SJoel Stanley 	return hw;
41915ed8ce5SJoel Stanley }
42015ed8ce5SJoel Stanley 
42198f3118dSJoel Stanley static int aspeed_clk_probe(struct platform_device *pdev)
42298f3118dSJoel Stanley {
42398f3118dSJoel Stanley 	const struct aspeed_clk_soc_data *soc_data;
42498f3118dSJoel Stanley 	struct device *dev = &pdev->dev;
425f7989839SJoel Stanley 	struct aspeed_reset *ar;
42698f3118dSJoel Stanley 	struct regmap *map;
42798f3118dSJoel Stanley 	struct clk_hw *hw;
42898f3118dSJoel Stanley 	u32 val, rate;
429f7989839SJoel Stanley 	int i, ret;
43098f3118dSJoel Stanley 
43198f3118dSJoel Stanley 	map = syscon_node_to_regmap(dev->of_node);
43298f3118dSJoel Stanley 	if (IS_ERR(map)) {
43398f3118dSJoel Stanley 		dev_err(dev, "no syscon regmap\n");
43498f3118dSJoel Stanley 		return PTR_ERR(map);
43598f3118dSJoel Stanley 	}
43698f3118dSJoel Stanley 
437f7989839SJoel Stanley 	ar = devm_kzalloc(dev, sizeof(*ar), GFP_KERNEL);
438f7989839SJoel Stanley 	if (!ar)
439f7989839SJoel Stanley 		return -ENOMEM;
440f7989839SJoel Stanley 
441f7989839SJoel Stanley 	ar->map = map;
442f7989839SJoel Stanley 	ar->rcdev.owner = THIS_MODULE;
443f7989839SJoel Stanley 	ar->rcdev.nr_resets = ARRAY_SIZE(aspeed_resets);
444f7989839SJoel Stanley 	ar->rcdev.ops = &aspeed_reset_ops;
445f7989839SJoel Stanley 	ar->rcdev.of_node = dev->of_node;
446f7989839SJoel Stanley 
447f7989839SJoel Stanley 	ret = devm_reset_controller_register(dev, &ar->rcdev);
448f7989839SJoel Stanley 	if (ret) {
449f7989839SJoel Stanley 		dev_err(dev, "could not register reset controller\n");
450f7989839SJoel Stanley 		return ret;
451f7989839SJoel Stanley 	}
452f7989839SJoel Stanley 
45398f3118dSJoel Stanley 	/* SoC generations share common layouts but have different divisors */
45498f3118dSJoel Stanley 	soc_data = of_device_get_match_data(dev);
45598f3118dSJoel Stanley 	if (!soc_data) {
45698f3118dSJoel Stanley 		dev_err(dev, "no match data for platform\n");
45798f3118dSJoel Stanley 		return -EINVAL;
45898f3118dSJoel Stanley 	}
45998f3118dSJoel Stanley 
46098f3118dSJoel Stanley 	/* UART clock div13 setting */
46198f3118dSJoel Stanley 	regmap_read(map, ASPEED_MISC_CTRL, &val);
46298f3118dSJoel Stanley 	if (val & UART_DIV13_EN)
46398f3118dSJoel Stanley 		rate = 24000000 / 13;
46498f3118dSJoel Stanley 	else
46598f3118dSJoel Stanley 		rate = 24000000;
46698f3118dSJoel Stanley 	/* TODO: Find the parent data for the uart clock */
46798f3118dSJoel Stanley 	hw = clk_hw_register_fixed_rate(dev, "uart", NULL, 0, rate);
46898f3118dSJoel Stanley 	if (IS_ERR(hw))
46998f3118dSJoel Stanley 		return PTR_ERR(hw);
47098f3118dSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_UART] = hw;
47198f3118dSJoel Stanley 
47298f3118dSJoel Stanley 	/*
47398f3118dSJoel Stanley 	 * Memory controller (M-PLL) PLL. This clock is configured by the
47498f3118dSJoel Stanley 	 * bootloader, and is exposed to Linux as a read-only clock rate.
47598f3118dSJoel Stanley 	 */
47698f3118dSJoel Stanley 	regmap_read(map, ASPEED_MPLL_PARAM, &val);
47798f3118dSJoel Stanley 	hw = soc_data->calc_pll("mpll", val);
47898f3118dSJoel Stanley 	if (IS_ERR(hw))
47998f3118dSJoel Stanley 		return PTR_ERR(hw);
48098f3118dSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_MPLL] =	hw;
48198f3118dSJoel Stanley 
48298f3118dSJoel Stanley 	/* SD/SDIO clock divider (TODO: There's a gate too) */
48398f3118dSJoel Stanley 	hw = clk_hw_register_divider_table(dev, "sdio", "hpll", 0,
48498f3118dSJoel Stanley 			scu_base + ASPEED_CLK_SELECTION, 12, 3, 0,
48598f3118dSJoel Stanley 			soc_data->div_table,
48698f3118dSJoel Stanley 			&aspeed_clk_lock);
48798f3118dSJoel Stanley 	if (IS_ERR(hw))
48898f3118dSJoel Stanley 		return PTR_ERR(hw);
48998f3118dSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_SDIO] = hw;
49098f3118dSJoel Stanley 
49198f3118dSJoel Stanley 	/* MAC AHB bus clock divider */
49298f3118dSJoel Stanley 	hw = clk_hw_register_divider_table(dev, "mac", "hpll", 0,
49398f3118dSJoel Stanley 			scu_base + ASPEED_CLK_SELECTION, 16, 3, 0,
49498f3118dSJoel Stanley 			soc_data->mac_div_table,
49598f3118dSJoel Stanley 			&aspeed_clk_lock);
49698f3118dSJoel Stanley 	if (IS_ERR(hw))
49798f3118dSJoel Stanley 		return PTR_ERR(hw);
49898f3118dSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_MAC] = hw;
49998f3118dSJoel Stanley 
50098f3118dSJoel Stanley 	/* LPC Host (LHCLK) clock divider */
50198f3118dSJoel Stanley 	hw = clk_hw_register_divider_table(dev, "lhclk", "hpll", 0,
50298f3118dSJoel Stanley 			scu_base + ASPEED_CLK_SELECTION, 20, 3, 0,
50398f3118dSJoel Stanley 			soc_data->div_table,
50498f3118dSJoel Stanley 			&aspeed_clk_lock);
50598f3118dSJoel Stanley 	if (IS_ERR(hw))
50698f3118dSJoel Stanley 		return PTR_ERR(hw);
50798f3118dSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_LHCLK] = hw;
50898f3118dSJoel Stanley 
50998f3118dSJoel Stanley 	/* P-Bus (BCLK) clock divider */
51098f3118dSJoel Stanley 	hw = clk_hw_register_divider_table(dev, "bclk", "hpll", 0,
51198f3118dSJoel Stanley 			scu_base + ASPEED_CLK_SELECTION_2, 0, 2, 0,
51298f3118dSJoel Stanley 			soc_data->div_table,
51398f3118dSJoel Stanley 			&aspeed_clk_lock);
51498f3118dSJoel Stanley 	if (IS_ERR(hw))
51598f3118dSJoel Stanley 		return PTR_ERR(hw);
51698f3118dSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_BCLK] = hw;
51798f3118dSJoel Stanley 
51867b6e5cfSLei YU 	/* Fixed 24MHz clock */
51967b6e5cfSLei YU 	hw = clk_hw_register_fixed_rate(NULL, "fixed-24m", "clkin",
52067b6e5cfSLei YU 					0, 24000000);
52167b6e5cfSLei YU 	if (IS_ERR(hw))
52267b6e5cfSLei YU 		return PTR_ERR(hw);
52367b6e5cfSLei YU 	aspeed_clk_data->hws[ASPEED_CLK_24M] = hw;
52467b6e5cfSLei YU 
52515ed8ce5SJoel Stanley 	/*
52615ed8ce5SJoel Stanley 	 * TODO: There are a number of clocks that not included in this driver
52715ed8ce5SJoel Stanley 	 * as more information is required:
52815ed8ce5SJoel Stanley 	 *   D2-PLL
52915ed8ce5SJoel Stanley 	 *   D-PLL
53015ed8ce5SJoel Stanley 	 *   YCLK
53115ed8ce5SJoel Stanley 	 *   RGMII
53215ed8ce5SJoel Stanley 	 *   RMII
53315ed8ce5SJoel Stanley 	 *   UART[1..5] clock source mux
53415ed8ce5SJoel Stanley 	 *   Video Engine (ECLK) mux and clock divider
53515ed8ce5SJoel Stanley 	 */
53615ed8ce5SJoel Stanley 
53715ed8ce5SJoel Stanley 	for (i = 0; i < ARRAY_SIZE(aspeed_gates); i++) {
53815ed8ce5SJoel Stanley 		const struct aspeed_gate_data *gd = &aspeed_gates[i];
5396671507fSBenjamin Herrenschmidt 		u32 gate_flags;
54015ed8ce5SJoel Stanley 
5416671507fSBenjamin Herrenschmidt 		/* Special case: the USB port 1 clock (bit 14) is always
5426671507fSBenjamin Herrenschmidt 		 * working the opposite way from the other ones.
5436671507fSBenjamin Herrenschmidt 		 */
5446671507fSBenjamin Herrenschmidt 		gate_flags = (gd->clock_idx == 14) ? 0 : CLK_GATE_SET_TO_DISABLE;
54515ed8ce5SJoel Stanley 		hw = aspeed_clk_hw_register_gate(dev,
54615ed8ce5SJoel Stanley 				gd->name,
54715ed8ce5SJoel Stanley 				gd->parent_name,
54815ed8ce5SJoel Stanley 				gd->flags,
54915ed8ce5SJoel Stanley 				map,
55015ed8ce5SJoel Stanley 				gd->clock_idx,
55115ed8ce5SJoel Stanley 				gd->reset_idx,
5526671507fSBenjamin Herrenschmidt 				gate_flags,
55315ed8ce5SJoel Stanley 				&aspeed_clk_lock);
55415ed8ce5SJoel Stanley 		if (IS_ERR(hw))
55515ed8ce5SJoel Stanley 			return PTR_ERR(hw);
55615ed8ce5SJoel Stanley 		aspeed_clk_data->hws[i] = hw;
55715ed8ce5SJoel Stanley 	}
55815ed8ce5SJoel Stanley 
55998f3118dSJoel Stanley 	return 0;
56098f3118dSJoel Stanley };
56198f3118dSJoel Stanley 
56298f3118dSJoel Stanley static const struct of_device_id aspeed_clk_dt_ids[] = {
56398f3118dSJoel Stanley 	{ .compatible = "aspeed,ast2400-scu", .data = &ast2400_data },
56498f3118dSJoel Stanley 	{ .compatible = "aspeed,ast2500-scu", .data = &ast2500_data },
56598f3118dSJoel Stanley 	{ }
56698f3118dSJoel Stanley };
56798f3118dSJoel Stanley 
56898f3118dSJoel Stanley static struct platform_driver aspeed_clk_driver = {
56998f3118dSJoel Stanley 	.probe  = aspeed_clk_probe,
57098f3118dSJoel Stanley 	.driver = {
57198f3118dSJoel Stanley 		.name = "aspeed-clk",
57298f3118dSJoel Stanley 		.of_match_table = aspeed_clk_dt_ids,
57398f3118dSJoel Stanley 		.suppress_bind_attrs = true,
57498f3118dSJoel Stanley 	},
57598f3118dSJoel Stanley };
57698f3118dSJoel Stanley builtin_platform_driver(aspeed_clk_driver);
57798f3118dSJoel Stanley 
57899d01e0eSJoel Stanley static void __init aspeed_ast2400_cc(struct regmap *map)
57999d01e0eSJoel Stanley {
58099d01e0eSJoel Stanley 	struct clk_hw *hw;
581*565b9937SJoel Stanley 	u32 val, div, clkin, hpll;
582*565b9937SJoel Stanley 	const u16 hpll_rates[][4] = {
583*565b9937SJoel Stanley 		{384, 360, 336, 408},
584*565b9937SJoel Stanley 		{400, 375, 350, 425},
585*565b9937SJoel Stanley 	};
586*565b9937SJoel Stanley 	int rate;
58799d01e0eSJoel Stanley 
58899d01e0eSJoel Stanley 	/*
58999d01e0eSJoel Stanley 	 * CLKIN is the crystal oscillator, 24, 48 or 25MHz selected by
59099d01e0eSJoel Stanley 	 * strapping
59199d01e0eSJoel Stanley 	 */
59299d01e0eSJoel Stanley 	regmap_read(map, ASPEED_STRAP, &val);
593*565b9937SJoel Stanley 	rate = (val >> 8) & 3;
594*565b9937SJoel Stanley 	if (val & CLKIN_25MHZ_EN) {
595*565b9937SJoel Stanley 		clkin = 25000000;
596*565b9937SJoel Stanley 		hpll = hpll_rates[1][rate];
597*565b9937SJoel Stanley 	} else if (val & AST2400_CLK_SOURCE_SEL) {
598*565b9937SJoel Stanley 		clkin = 48000000;
599*565b9937SJoel Stanley 		hpll = hpll_rates[0][rate];
600*565b9937SJoel Stanley 	} else {
601*565b9937SJoel Stanley 		clkin = 24000000;
602*565b9937SJoel Stanley 		hpll = hpll_rates[0][rate];
603*565b9937SJoel Stanley 	}
604*565b9937SJoel Stanley 	hw = clk_hw_register_fixed_rate(NULL, "clkin", NULL, 0, clkin);
605*565b9937SJoel Stanley 	pr_debug("clkin @%u MHz\n", clkin / 1000000);
60699d01e0eSJoel Stanley 
60799d01e0eSJoel Stanley 	/*
60899d01e0eSJoel Stanley 	 * High-speed PLL clock derived from the crystal. This the CPU clock,
609*565b9937SJoel Stanley 	 * and we assume that it is enabled. It can be configured through the
610*565b9937SJoel Stanley 	 * HPLL_PARAM register, or set to a specified frequency by strapping.
61199d01e0eSJoel Stanley 	 */
61299d01e0eSJoel Stanley 	regmap_read(map, ASPEED_HPLL_PARAM, &val);
613*565b9937SJoel Stanley 	if (val & AST2400_HPLL_PROGRAMMED)
614*565b9937SJoel Stanley 		hw = aspeed_ast2400_calc_pll("hpll", val);
615*565b9937SJoel Stanley 	else
616*565b9937SJoel Stanley 		hw = clk_hw_register_fixed_rate(NULL, "hpll", "clkin", 0,
617*565b9937SJoel Stanley 				hpll * 1000000);
618*565b9937SJoel Stanley 
619*565b9937SJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_HPLL] = hw;
62099d01e0eSJoel Stanley 
62199d01e0eSJoel Stanley 	/*
62299d01e0eSJoel Stanley 	 * Strap bits 11:10 define the CPU/AHB clock frequency ratio (aka HCLK)
62399d01e0eSJoel Stanley 	 *   00: Select CPU:AHB = 1:1
62499d01e0eSJoel Stanley 	 *   01: Select CPU:AHB = 2:1
62599d01e0eSJoel Stanley 	 *   10: Select CPU:AHB = 4:1
62699d01e0eSJoel Stanley 	 *   11: Select CPU:AHB = 3:1
62799d01e0eSJoel Stanley 	 */
62899d01e0eSJoel Stanley 	regmap_read(map, ASPEED_STRAP, &val);
62999d01e0eSJoel Stanley 	val = (val >> 10) & 0x3;
63099d01e0eSJoel Stanley 	div = val + 1;
63199d01e0eSJoel Stanley 	if (div == 3)
63299d01e0eSJoel Stanley 		div = 4;
63399d01e0eSJoel Stanley 	else if (div == 4)
63499d01e0eSJoel Stanley 		div = 3;
63599d01e0eSJoel Stanley 	hw = clk_hw_register_fixed_factor(NULL, "ahb", "hpll", 0, 1, div);
63699d01e0eSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_AHB] = hw;
63799d01e0eSJoel Stanley 
63899d01e0eSJoel Stanley 	/* APB clock clock selection register SCU08 (aka PCLK) */
63999d01e0eSJoel Stanley 	hw = clk_hw_register_divider_table(NULL, "apb", "hpll", 0,
64099d01e0eSJoel Stanley 			scu_base + ASPEED_CLK_SELECTION, 23, 3, 0,
64199d01e0eSJoel Stanley 			ast2400_div_table,
64299d01e0eSJoel Stanley 			&aspeed_clk_lock);
64399d01e0eSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_APB] = hw;
64499d01e0eSJoel Stanley }
64599d01e0eSJoel Stanley 
64699d01e0eSJoel Stanley static void __init aspeed_ast2500_cc(struct regmap *map)
64799d01e0eSJoel Stanley {
64899d01e0eSJoel Stanley 	struct clk_hw *hw;
64999d01e0eSJoel Stanley 	u32 val, freq, div;
65099d01e0eSJoel Stanley 
65199d01e0eSJoel Stanley 	/* CLKIN is the crystal oscillator, 24 or 25MHz selected by strapping */
65299d01e0eSJoel Stanley 	regmap_read(map, ASPEED_STRAP, &val);
65399d01e0eSJoel Stanley 	if (val & CLKIN_25MHZ_EN)
65499d01e0eSJoel Stanley 		freq = 25000000;
65599d01e0eSJoel Stanley 	else
65699d01e0eSJoel Stanley 		freq = 24000000;
65799d01e0eSJoel Stanley 	hw = clk_hw_register_fixed_rate(NULL, "clkin", NULL, 0, freq);
65899d01e0eSJoel Stanley 	pr_debug("clkin @%u MHz\n", freq / 1000000);
65999d01e0eSJoel Stanley 
66099d01e0eSJoel Stanley 	/*
66199d01e0eSJoel Stanley 	 * High-speed PLL clock derived from the crystal. This the CPU clock,
66299d01e0eSJoel Stanley 	 * and we assume that it is enabled
66399d01e0eSJoel Stanley 	 */
66499d01e0eSJoel Stanley 	regmap_read(map, ASPEED_HPLL_PARAM, &val);
66599d01e0eSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_HPLL] = aspeed_ast2500_calc_pll("hpll", val);
66699d01e0eSJoel Stanley 
66799d01e0eSJoel Stanley 	/* Strap bits 11:9 define the AXI/AHB clock frequency ratio (aka HCLK)*/
66899d01e0eSJoel Stanley 	regmap_read(map, ASPEED_STRAP, &val);
66999d01e0eSJoel Stanley 	val = (val >> 9) & 0x7;
67099d01e0eSJoel Stanley 	WARN(val == 0, "strapping is zero: cannot determine ahb clock");
67199d01e0eSJoel Stanley 	div = 2 * (val + 1);
67299d01e0eSJoel Stanley 	hw = clk_hw_register_fixed_factor(NULL, "ahb", "hpll", 0, 1, div);
67399d01e0eSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_AHB] = hw;
67499d01e0eSJoel Stanley 
67599d01e0eSJoel Stanley 	/* APB clock clock selection register SCU08 (aka PCLK) */
67699d01e0eSJoel Stanley 	regmap_read(map, ASPEED_CLK_SELECTION, &val);
67799d01e0eSJoel Stanley 	val = (val >> 23) & 0x7;
67899d01e0eSJoel Stanley 	div = 4 * (val + 1);
67999d01e0eSJoel Stanley 	hw = clk_hw_register_fixed_factor(NULL, "apb", "hpll", 0, 1, div);
68099d01e0eSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_APB] = hw;
68199d01e0eSJoel Stanley };
68299d01e0eSJoel Stanley 
6835eda5d79SJoel Stanley static void __init aspeed_cc_init(struct device_node *np)
6845eda5d79SJoel Stanley {
6855eda5d79SJoel Stanley 	struct regmap *map;
6865eda5d79SJoel Stanley 	u32 val;
6875eda5d79SJoel Stanley 	int ret;
6885eda5d79SJoel Stanley 	int i;
6895eda5d79SJoel Stanley 
6905eda5d79SJoel Stanley 	scu_base = of_iomap(np, 0);
691accf475aSWei Yongjun 	if (!scu_base)
6925eda5d79SJoel Stanley 		return;
6935eda5d79SJoel Stanley 
694acafe7e3SKees Cook 	aspeed_clk_data = kzalloc(struct_size(aspeed_clk_data, hws,
695acafe7e3SKees Cook 					      ASPEED_NUM_CLKS),
6965eda5d79SJoel Stanley 				  GFP_KERNEL);
6975eda5d79SJoel Stanley 	if (!aspeed_clk_data)
6985eda5d79SJoel Stanley 		return;
6995eda5d79SJoel Stanley 
7005eda5d79SJoel Stanley 	/*
7015eda5d79SJoel Stanley 	 * This way all clocks fetched before the platform device probes,
7025eda5d79SJoel Stanley 	 * except those we assign here for early use, will be deferred.
7035eda5d79SJoel Stanley 	 */
7045eda5d79SJoel Stanley 	for (i = 0; i < ASPEED_NUM_CLKS; i++)
7055eda5d79SJoel Stanley 		aspeed_clk_data->hws[i] = ERR_PTR(-EPROBE_DEFER);
7065eda5d79SJoel Stanley 
7075eda5d79SJoel Stanley 	map = syscon_node_to_regmap(np);
7085eda5d79SJoel Stanley 	if (IS_ERR(map)) {
7095eda5d79SJoel Stanley 		pr_err("no syscon regmap\n");
7105eda5d79SJoel Stanley 		return;
7115eda5d79SJoel Stanley 	}
7125eda5d79SJoel Stanley 	/*
7135eda5d79SJoel Stanley 	 * We check that the regmap works on this very first access,
7145eda5d79SJoel Stanley 	 * but as this is an MMIO-backed regmap, subsequent regmap
7155eda5d79SJoel Stanley 	 * access is not going to fail and we skip error checks from
7165eda5d79SJoel Stanley 	 * this point.
7175eda5d79SJoel Stanley 	 */
7185eda5d79SJoel Stanley 	ret = regmap_read(map, ASPEED_STRAP, &val);
7195eda5d79SJoel Stanley 	if (ret) {
7205eda5d79SJoel Stanley 		pr_err("failed to read strapping register\n");
7215eda5d79SJoel Stanley 		return;
7225eda5d79SJoel Stanley 	}
7235eda5d79SJoel Stanley 
72499d01e0eSJoel Stanley 	if (of_device_is_compatible(np, "aspeed,ast2400-scu"))
72599d01e0eSJoel Stanley 		aspeed_ast2400_cc(map);
72699d01e0eSJoel Stanley 	else if (of_device_is_compatible(np, "aspeed,ast2500-scu"))
72799d01e0eSJoel Stanley 		aspeed_ast2500_cc(map);
72899d01e0eSJoel Stanley 	else
72999d01e0eSJoel Stanley 		pr_err("unknown platform, failed to add clocks\n");
73099d01e0eSJoel Stanley 
7315eda5d79SJoel Stanley 	aspeed_clk_data->num = ASPEED_NUM_CLKS;
7325eda5d79SJoel Stanley 	ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, aspeed_clk_data);
7335eda5d79SJoel Stanley 	if (ret)
7345eda5d79SJoel Stanley 		pr_err("failed to add DT provider: %d\n", ret);
7355eda5d79SJoel Stanley };
7365eda5d79SJoel Stanley CLK_OF_DECLARE_DRIVER(aspeed_cc_g5, "aspeed,ast2500-scu", aspeed_cc_init);
7375eda5d79SJoel Stanley CLK_OF_DECLARE_DRIVER(aspeed_cc_g4, "aspeed,ast2400-scu", aspeed_cc_init);
738