xref: /openbmc/linux/drivers/clk/clk-aspeed.c (revision ebd5f82d32ade6f864917bf868bfa32f4d3c0486)
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)
27565b9937SJoel 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 */
90defb149bSEddie James 	[ASPEED_CLK_GATE_ECLK] =	{  0,  6, "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 */
93defb149bSEddie James 	[ASPEED_CLK_GATE_VCLK] =	{  3, -1, "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 */
112cd88259aSLei YU 	[ASPEED_CLK_GATE_SDCLK] =	{ 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 
116defb149bSEddie James static const char * const eclk_parent_names[] = {
117defb149bSEddie James 	"mpll",
118defb149bSEddie James 	"hpll",
119defb149bSEddie James 	"dpll",
120defb149bSEddie James };
121defb149bSEddie James 
122defb149bSEddie James static const struct clk_div_table ast2500_eclk_div_table[] = {
123defb149bSEddie James 	{ 0x0, 2 },
124defb149bSEddie James 	{ 0x1, 2 },
125defb149bSEddie James 	{ 0x2, 3 },
126defb149bSEddie James 	{ 0x3, 4 },
127defb149bSEddie James 	{ 0x4, 5 },
128defb149bSEddie James 	{ 0x5, 6 },
129defb149bSEddie James 	{ 0x6, 7 },
130defb149bSEddie James 	{ 0x7, 8 },
131defb149bSEddie James 	{ 0 }
132defb149bSEddie James };
133defb149bSEddie James 
13498f3118dSJoel Stanley static const struct clk_div_table ast2500_mac_div_table[] = {
13598f3118dSJoel Stanley 	{ 0x0, 4 }, /* Yep, really. Aspeed confirmed this is correct */
13698f3118dSJoel Stanley 	{ 0x1, 4 },
13798f3118dSJoel Stanley 	{ 0x2, 6 },
13898f3118dSJoel Stanley 	{ 0x3, 8 },
13998f3118dSJoel Stanley 	{ 0x4, 10 },
14098f3118dSJoel Stanley 	{ 0x5, 12 },
14198f3118dSJoel Stanley 	{ 0x6, 14 },
14298f3118dSJoel Stanley 	{ 0x7, 16 },
14398f3118dSJoel Stanley 	{ 0 }
14498f3118dSJoel Stanley };
14598f3118dSJoel Stanley 
14699d01e0eSJoel Stanley static const struct clk_div_table ast2400_div_table[] = {
14799d01e0eSJoel Stanley 	{ 0x0, 2 },
14899d01e0eSJoel Stanley 	{ 0x1, 4 },
14999d01e0eSJoel Stanley 	{ 0x2, 6 },
15099d01e0eSJoel Stanley 	{ 0x3, 8 },
15199d01e0eSJoel Stanley 	{ 0x4, 10 },
15299d01e0eSJoel Stanley 	{ 0x5, 12 },
15399d01e0eSJoel Stanley 	{ 0x6, 14 },
15499d01e0eSJoel Stanley 	{ 0x7, 16 },
15599d01e0eSJoel Stanley 	{ 0 }
15699d01e0eSJoel Stanley };
15799d01e0eSJoel Stanley 
15899d01e0eSJoel Stanley static const struct clk_div_table ast2500_div_table[] = {
15999d01e0eSJoel Stanley 	{ 0x0, 4 },
16099d01e0eSJoel Stanley 	{ 0x1, 8 },
16199d01e0eSJoel Stanley 	{ 0x2, 12 },
16299d01e0eSJoel Stanley 	{ 0x3, 16 },
16399d01e0eSJoel Stanley 	{ 0x4, 20 },
16499d01e0eSJoel Stanley 	{ 0x5, 24 },
16599d01e0eSJoel Stanley 	{ 0x6, 28 },
16699d01e0eSJoel Stanley 	{ 0x7, 32 },
16799d01e0eSJoel Stanley 	{ 0 }
16899d01e0eSJoel Stanley };
16999d01e0eSJoel Stanley 
17099d01e0eSJoel Stanley static struct clk_hw *aspeed_ast2400_calc_pll(const char *name, u32 val)
17199d01e0eSJoel Stanley {
17299d01e0eSJoel Stanley 	unsigned int mult, div;
17399d01e0eSJoel Stanley 
17499d01e0eSJoel Stanley 	if (val & AST2400_HPLL_BYPASS_EN) {
17599d01e0eSJoel Stanley 		/* Pass through mode */
17699d01e0eSJoel Stanley 		mult = div = 1;
17799d01e0eSJoel Stanley 	} else {
17899d01e0eSJoel Stanley 		/* F = 24Mhz * (2-OD) * [(N + 2) / (D + 1)] */
17999d01e0eSJoel Stanley 		u32 n = (val >> 5) & 0x3f;
18099d01e0eSJoel Stanley 		u32 od = (val >> 4) & 0x1;
18199d01e0eSJoel Stanley 		u32 d = val & 0xf;
18299d01e0eSJoel Stanley 
18399d01e0eSJoel Stanley 		mult = (2 - od) * (n + 2);
18499d01e0eSJoel Stanley 		div = d + 1;
18599d01e0eSJoel Stanley 	}
18699d01e0eSJoel Stanley 	return clk_hw_register_fixed_factor(NULL, name, "clkin", 0,
18799d01e0eSJoel Stanley 			mult, div);
18899d01e0eSJoel Stanley };
18999d01e0eSJoel Stanley 
19099d01e0eSJoel Stanley static struct clk_hw *aspeed_ast2500_calc_pll(const char *name, u32 val)
19199d01e0eSJoel Stanley {
19299d01e0eSJoel Stanley 	unsigned int mult, div;
19399d01e0eSJoel Stanley 
19499d01e0eSJoel Stanley 	if (val & AST2500_HPLL_BYPASS_EN) {
19599d01e0eSJoel Stanley 		/* Pass through mode */
19699d01e0eSJoel Stanley 		mult = div = 1;
19799d01e0eSJoel Stanley 	} else {
19899d01e0eSJoel Stanley 		/* F = clkin * [(M+1) / (N+1)] / (P + 1) */
19999d01e0eSJoel Stanley 		u32 p = (val >> 13) & 0x3f;
20099d01e0eSJoel Stanley 		u32 m = (val >> 5) & 0xff;
20199d01e0eSJoel Stanley 		u32 n = val & 0x1f;
20299d01e0eSJoel Stanley 
20399d01e0eSJoel Stanley 		mult = (m + 1) / (n + 1);
20499d01e0eSJoel Stanley 		div = p + 1;
20599d01e0eSJoel Stanley 	}
20699d01e0eSJoel Stanley 
20799d01e0eSJoel Stanley 	return clk_hw_register_fixed_factor(NULL, name, "clkin", 0,
20899d01e0eSJoel Stanley 			mult, div);
20999d01e0eSJoel Stanley }
21099d01e0eSJoel Stanley 
21198f3118dSJoel Stanley struct aspeed_clk_soc_data {
21298f3118dSJoel Stanley 	const struct clk_div_table *div_table;
213defb149bSEddie James 	const struct clk_div_table *eclk_div_table;
21498f3118dSJoel Stanley 	const struct clk_div_table *mac_div_table;
21598f3118dSJoel Stanley 	struct clk_hw *(*calc_pll)(const char *name, u32 val);
21698f3118dSJoel Stanley };
21798f3118dSJoel Stanley 
21898f3118dSJoel Stanley static const struct aspeed_clk_soc_data ast2500_data = {
21998f3118dSJoel Stanley 	.div_table = ast2500_div_table,
220defb149bSEddie James 	.eclk_div_table = ast2500_eclk_div_table,
22198f3118dSJoel Stanley 	.mac_div_table = ast2500_mac_div_table,
22298f3118dSJoel Stanley 	.calc_pll = aspeed_ast2500_calc_pll,
22398f3118dSJoel Stanley };
22498f3118dSJoel Stanley 
22598f3118dSJoel Stanley static const struct aspeed_clk_soc_data ast2400_data = {
22698f3118dSJoel Stanley 	.div_table = ast2400_div_table,
227defb149bSEddie James 	.eclk_div_table = ast2400_div_table,
22898f3118dSJoel Stanley 	.mac_div_table = ast2400_div_table,
22998f3118dSJoel Stanley 	.calc_pll = aspeed_ast2400_calc_pll,
23098f3118dSJoel Stanley };
23198f3118dSJoel Stanley 
2328a53fc51SEddie James static int aspeed_clk_is_enabled(struct clk_hw *hw)
2338a53fc51SEddie James {
2348a53fc51SEddie James 	struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
2358a53fc51SEddie James 	u32 clk = BIT(gate->clock_idx);
236edc6f7e9SBenjamin Herrenschmidt 	u32 rst = BIT(gate->reset_idx);
2378a53fc51SEddie James 	u32 enval = (gate->flags & CLK_GATE_SET_TO_DISABLE) ? 0 : clk;
2388a53fc51SEddie James 	u32 reg;
2398a53fc51SEddie James 
240edc6f7e9SBenjamin Herrenschmidt 	/*
241edc6f7e9SBenjamin Herrenschmidt 	 * If the IP is in reset, treat the clock as not enabled,
242edc6f7e9SBenjamin Herrenschmidt 	 * this happens with some clocks such as the USB one when
243edc6f7e9SBenjamin Herrenschmidt 	 * coming from cold reset. Without this, aspeed_clk_enable()
244edc6f7e9SBenjamin Herrenschmidt 	 * will fail to lift the reset.
245edc6f7e9SBenjamin Herrenschmidt 	 */
246edc6f7e9SBenjamin Herrenschmidt 	if (gate->reset_idx >= 0) {
247edc6f7e9SBenjamin Herrenschmidt 		regmap_read(gate->map, ASPEED_RESET_CTRL, &reg);
248edc6f7e9SBenjamin Herrenschmidt 		if (reg & rst)
249edc6f7e9SBenjamin Herrenschmidt 			return 0;
250edc6f7e9SBenjamin Herrenschmidt 	}
251edc6f7e9SBenjamin Herrenschmidt 
2528a53fc51SEddie James 	regmap_read(gate->map, ASPEED_CLK_STOP_CTRL, &reg);
2538a53fc51SEddie James 
2548a53fc51SEddie James 	return ((reg & clk) == enval) ? 1 : 0;
2558a53fc51SEddie James }
2568a53fc51SEddie James 
25715ed8ce5SJoel Stanley static int aspeed_clk_enable(struct clk_hw *hw)
25815ed8ce5SJoel Stanley {
25915ed8ce5SJoel Stanley 	struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
26015ed8ce5SJoel Stanley 	unsigned long flags;
26115ed8ce5SJoel Stanley 	u32 clk = BIT(gate->clock_idx);
26215ed8ce5SJoel Stanley 	u32 rst = BIT(gate->reset_idx);
2636671507fSBenjamin Herrenschmidt 	u32 enval;
26415ed8ce5SJoel Stanley 
26515ed8ce5SJoel Stanley 	spin_lock_irqsave(gate->lock, flags);
26615ed8ce5SJoel Stanley 
2678a53fc51SEddie James 	if (aspeed_clk_is_enabled(hw)) {
2688a53fc51SEddie James 		spin_unlock_irqrestore(gate->lock, flags);
2698a53fc51SEddie James 		return 0;
2708a53fc51SEddie James 	}
2718a53fc51SEddie James 
27215ed8ce5SJoel Stanley 	if (gate->reset_idx >= 0) {
27315ed8ce5SJoel Stanley 		/* Put IP in reset */
27415ed8ce5SJoel Stanley 		regmap_update_bits(gate->map, ASPEED_RESET_CTRL, rst, rst);
27515ed8ce5SJoel Stanley 
27615ed8ce5SJoel Stanley 		/* Delay 100us */
27715ed8ce5SJoel Stanley 		udelay(100);
27815ed8ce5SJoel Stanley 	}
27915ed8ce5SJoel Stanley 
28015ed8ce5SJoel Stanley 	/* Enable clock */
2816671507fSBenjamin Herrenschmidt 	enval = (gate->flags & CLK_GATE_SET_TO_DISABLE) ? 0 : clk;
2826671507fSBenjamin Herrenschmidt 	regmap_update_bits(gate->map, ASPEED_CLK_STOP_CTRL, clk, enval);
28315ed8ce5SJoel Stanley 
28415ed8ce5SJoel Stanley 	if (gate->reset_idx >= 0) {
28515ed8ce5SJoel Stanley 		/* A delay of 10ms is specified by the ASPEED docs */
28615ed8ce5SJoel Stanley 		mdelay(10);
28715ed8ce5SJoel Stanley 
28815ed8ce5SJoel Stanley 		/* Take IP out of reset */
28915ed8ce5SJoel Stanley 		regmap_update_bits(gate->map, ASPEED_RESET_CTRL, rst, 0);
29015ed8ce5SJoel Stanley 	}
29115ed8ce5SJoel Stanley 
29215ed8ce5SJoel Stanley 	spin_unlock_irqrestore(gate->lock, flags);
29315ed8ce5SJoel Stanley 
29415ed8ce5SJoel Stanley 	return 0;
29515ed8ce5SJoel Stanley }
29615ed8ce5SJoel Stanley 
29715ed8ce5SJoel Stanley static void aspeed_clk_disable(struct clk_hw *hw)
29815ed8ce5SJoel Stanley {
29915ed8ce5SJoel Stanley 	struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
30015ed8ce5SJoel Stanley 	unsigned long flags;
30115ed8ce5SJoel Stanley 	u32 clk = BIT(gate->clock_idx);
3026671507fSBenjamin Herrenschmidt 	u32 enval;
30315ed8ce5SJoel Stanley 
30415ed8ce5SJoel Stanley 	spin_lock_irqsave(gate->lock, flags);
30515ed8ce5SJoel Stanley 
3066671507fSBenjamin Herrenschmidt 	enval = (gate->flags & CLK_GATE_SET_TO_DISABLE) ? clk : 0;
3076671507fSBenjamin Herrenschmidt 	regmap_update_bits(gate->map, ASPEED_CLK_STOP_CTRL, clk, enval);
30815ed8ce5SJoel Stanley 
30915ed8ce5SJoel Stanley 	spin_unlock_irqrestore(gate->lock, flags);
31015ed8ce5SJoel Stanley }
31115ed8ce5SJoel Stanley 
31215ed8ce5SJoel Stanley static const struct clk_ops aspeed_clk_gate_ops = {
31315ed8ce5SJoel Stanley 	.enable = aspeed_clk_enable,
31415ed8ce5SJoel Stanley 	.disable = aspeed_clk_disable,
31515ed8ce5SJoel Stanley 	.is_enabled = aspeed_clk_is_enabled,
31615ed8ce5SJoel Stanley };
31715ed8ce5SJoel Stanley 
318f7989839SJoel Stanley /**
319f7989839SJoel Stanley  * struct aspeed_reset - Aspeed reset controller
320f7989839SJoel Stanley  * @map: regmap to access the containing system controller
321f7989839SJoel Stanley  * @rcdev: reset controller device
322f7989839SJoel Stanley  */
323f7989839SJoel Stanley struct aspeed_reset {
324f7989839SJoel Stanley 	struct regmap			*map;
325f7989839SJoel Stanley 	struct reset_controller_dev	rcdev;
326f7989839SJoel Stanley };
327f7989839SJoel Stanley 
328f7989839SJoel Stanley #define to_aspeed_reset(p) container_of((p), struct aspeed_reset, rcdev)
329f7989839SJoel Stanley 
330f7989839SJoel Stanley static const u8 aspeed_resets[] = {
331dcb899c4SJoel Stanley 	/* SCU04 resets */
332f7989839SJoel Stanley 	[ASPEED_RESET_XDMA]	= 25,
333f7989839SJoel Stanley 	[ASPEED_RESET_MCTP]	= 24,
334f7989839SJoel Stanley 	[ASPEED_RESET_ADC]	= 23,
335f7989839SJoel Stanley 	[ASPEED_RESET_JTAG_MASTER] = 22,
336f7989839SJoel Stanley 	[ASPEED_RESET_MIC]	= 18,
337f7989839SJoel Stanley 	[ASPEED_RESET_PWM]	=  9,
338e76e5682SJae Hyun Yoo 	[ASPEED_RESET_PECI]	= 10,
339f7989839SJoel Stanley 	[ASPEED_RESET_I2C]	=  2,
340f7989839SJoel Stanley 	[ASPEED_RESET_AHB]	=  1,
341dcb899c4SJoel Stanley 
342dcb899c4SJoel Stanley 	/*
343dcb899c4SJoel Stanley 	 * SCUD4 resets start at an offset to separate them from
344dcb899c4SJoel Stanley 	 * the SCU04 resets.
345dcb899c4SJoel Stanley 	 */
346dcb899c4SJoel Stanley 	[ASPEED_RESET_CRT1]	= ASPEED_RESET2_OFFSET + 5,
347f7989839SJoel Stanley };
348f7989839SJoel Stanley 
349f7989839SJoel Stanley static int aspeed_reset_deassert(struct reset_controller_dev *rcdev,
350f7989839SJoel Stanley 				 unsigned long id)
351f7989839SJoel Stanley {
352f7989839SJoel Stanley 	struct aspeed_reset *ar = to_aspeed_reset(rcdev);
353dcb899c4SJoel Stanley 	u32 reg = ASPEED_RESET_CTRL;
354dcb899c4SJoel Stanley 	u32 bit = aspeed_resets[id];
355f7989839SJoel Stanley 
356dcb899c4SJoel Stanley 	if (bit >= ASPEED_RESET2_OFFSET) {
357dcb899c4SJoel Stanley 		bit -= ASPEED_RESET2_OFFSET;
358dcb899c4SJoel Stanley 		reg = ASPEED_RESET_CTRL2;
359dcb899c4SJoel Stanley 	}
360dcb899c4SJoel Stanley 
361dcb899c4SJoel Stanley 	return regmap_update_bits(ar->map, reg, BIT(bit), 0);
362f7989839SJoel Stanley }
363f7989839SJoel Stanley 
364f7989839SJoel Stanley static int aspeed_reset_assert(struct reset_controller_dev *rcdev,
365f7989839SJoel Stanley 			       unsigned long id)
366f7989839SJoel Stanley {
367f7989839SJoel Stanley 	struct aspeed_reset *ar = to_aspeed_reset(rcdev);
368dcb899c4SJoel Stanley 	u32 reg = ASPEED_RESET_CTRL;
369dcb899c4SJoel Stanley 	u32 bit = aspeed_resets[id];
370f7989839SJoel Stanley 
371dcb899c4SJoel Stanley 	if (bit >= ASPEED_RESET2_OFFSET) {
372dcb899c4SJoel Stanley 		bit -= ASPEED_RESET2_OFFSET;
373dcb899c4SJoel Stanley 		reg = ASPEED_RESET_CTRL2;
374dcb899c4SJoel Stanley 	}
375dcb899c4SJoel Stanley 
376dcb899c4SJoel Stanley 	return regmap_update_bits(ar->map, reg, BIT(bit), BIT(bit));
377f7989839SJoel Stanley }
378f7989839SJoel Stanley 
379f7989839SJoel Stanley static int aspeed_reset_status(struct reset_controller_dev *rcdev,
380f7989839SJoel Stanley 			       unsigned long id)
381f7989839SJoel Stanley {
382f7989839SJoel Stanley 	struct aspeed_reset *ar = to_aspeed_reset(rcdev);
383dcb899c4SJoel Stanley 	u32 reg = ASPEED_RESET_CTRL;
384dcb899c4SJoel Stanley 	u32 bit = aspeed_resets[id];
385dcb899c4SJoel Stanley 	int ret, val;
386f7989839SJoel Stanley 
387dcb899c4SJoel Stanley 	if (bit >= ASPEED_RESET2_OFFSET) {
388dcb899c4SJoel Stanley 		bit -= ASPEED_RESET2_OFFSET;
389dcb899c4SJoel Stanley 		reg = ASPEED_RESET_CTRL2;
390dcb899c4SJoel Stanley 	}
391dcb899c4SJoel Stanley 
392dcb899c4SJoel Stanley 	ret = regmap_read(ar->map, reg, &val);
393f7989839SJoel Stanley 	if (ret)
394f7989839SJoel Stanley 		return ret;
395f7989839SJoel Stanley 
396dcb899c4SJoel Stanley 	return !!(val & BIT(bit));
397f7989839SJoel Stanley }
398f7989839SJoel Stanley 
399f7989839SJoel Stanley static const struct reset_control_ops aspeed_reset_ops = {
400f7989839SJoel Stanley 	.assert = aspeed_reset_assert,
401f7989839SJoel Stanley 	.deassert = aspeed_reset_deassert,
402f7989839SJoel Stanley 	.status = aspeed_reset_status,
403f7989839SJoel Stanley };
404f7989839SJoel Stanley 
40515ed8ce5SJoel Stanley static struct clk_hw *aspeed_clk_hw_register_gate(struct device *dev,
40615ed8ce5SJoel Stanley 		const char *name, const char *parent_name, unsigned long flags,
40715ed8ce5SJoel Stanley 		struct regmap *map, u8 clock_idx, u8 reset_idx,
40815ed8ce5SJoel Stanley 		u8 clk_gate_flags, spinlock_t *lock)
40915ed8ce5SJoel Stanley {
41015ed8ce5SJoel Stanley 	struct aspeed_clk_gate *gate;
41115ed8ce5SJoel Stanley 	struct clk_init_data init;
41215ed8ce5SJoel Stanley 	struct clk_hw *hw;
41315ed8ce5SJoel Stanley 	int ret;
41415ed8ce5SJoel Stanley 
41515ed8ce5SJoel Stanley 	gate = kzalloc(sizeof(*gate), GFP_KERNEL);
41615ed8ce5SJoel Stanley 	if (!gate)
41715ed8ce5SJoel Stanley 		return ERR_PTR(-ENOMEM);
41815ed8ce5SJoel Stanley 
41915ed8ce5SJoel Stanley 	init.name = name;
42015ed8ce5SJoel Stanley 	init.ops = &aspeed_clk_gate_ops;
42115ed8ce5SJoel Stanley 	init.flags = flags;
42215ed8ce5SJoel Stanley 	init.parent_names = parent_name ? &parent_name : NULL;
42315ed8ce5SJoel Stanley 	init.num_parents = parent_name ? 1 : 0;
42415ed8ce5SJoel Stanley 
42515ed8ce5SJoel Stanley 	gate->map = map;
42615ed8ce5SJoel Stanley 	gate->clock_idx = clock_idx;
42715ed8ce5SJoel Stanley 	gate->reset_idx = reset_idx;
42815ed8ce5SJoel Stanley 	gate->flags = clk_gate_flags;
42915ed8ce5SJoel Stanley 	gate->lock = lock;
43015ed8ce5SJoel Stanley 	gate->hw.init = &init;
43115ed8ce5SJoel Stanley 
43215ed8ce5SJoel Stanley 	hw = &gate->hw;
43315ed8ce5SJoel Stanley 	ret = clk_hw_register(dev, hw);
43415ed8ce5SJoel Stanley 	if (ret) {
43515ed8ce5SJoel Stanley 		kfree(gate);
43615ed8ce5SJoel Stanley 		hw = ERR_PTR(ret);
43715ed8ce5SJoel Stanley 	}
43815ed8ce5SJoel Stanley 
43915ed8ce5SJoel Stanley 	return hw;
44015ed8ce5SJoel Stanley }
44115ed8ce5SJoel Stanley 
44298f3118dSJoel Stanley static int aspeed_clk_probe(struct platform_device *pdev)
44398f3118dSJoel Stanley {
44498f3118dSJoel Stanley 	const struct aspeed_clk_soc_data *soc_data;
44598f3118dSJoel Stanley 	struct device *dev = &pdev->dev;
446f7989839SJoel Stanley 	struct aspeed_reset *ar;
44798f3118dSJoel Stanley 	struct regmap *map;
44898f3118dSJoel Stanley 	struct clk_hw *hw;
44998f3118dSJoel Stanley 	u32 val, rate;
450f7989839SJoel Stanley 	int i, ret;
45198f3118dSJoel Stanley 
45298f3118dSJoel Stanley 	map = syscon_node_to_regmap(dev->of_node);
45398f3118dSJoel Stanley 	if (IS_ERR(map)) {
45498f3118dSJoel Stanley 		dev_err(dev, "no syscon regmap\n");
45598f3118dSJoel Stanley 		return PTR_ERR(map);
45698f3118dSJoel Stanley 	}
45798f3118dSJoel Stanley 
458f7989839SJoel Stanley 	ar = devm_kzalloc(dev, sizeof(*ar), GFP_KERNEL);
459f7989839SJoel Stanley 	if (!ar)
460f7989839SJoel Stanley 		return -ENOMEM;
461f7989839SJoel Stanley 
462f7989839SJoel Stanley 	ar->map = map;
463f7989839SJoel Stanley 	ar->rcdev.owner = THIS_MODULE;
464f7989839SJoel Stanley 	ar->rcdev.nr_resets = ARRAY_SIZE(aspeed_resets);
465f7989839SJoel Stanley 	ar->rcdev.ops = &aspeed_reset_ops;
466f7989839SJoel Stanley 	ar->rcdev.of_node = dev->of_node;
467f7989839SJoel Stanley 
468f7989839SJoel Stanley 	ret = devm_reset_controller_register(dev, &ar->rcdev);
469f7989839SJoel Stanley 	if (ret) {
470f7989839SJoel Stanley 		dev_err(dev, "could not register reset controller\n");
471f7989839SJoel Stanley 		return ret;
472f7989839SJoel Stanley 	}
473f7989839SJoel Stanley 
47498f3118dSJoel Stanley 	/* SoC generations share common layouts but have different divisors */
47598f3118dSJoel Stanley 	soc_data = of_device_get_match_data(dev);
47698f3118dSJoel Stanley 	if (!soc_data) {
47798f3118dSJoel Stanley 		dev_err(dev, "no match data for platform\n");
47898f3118dSJoel Stanley 		return -EINVAL;
47998f3118dSJoel Stanley 	}
48098f3118dSJoel Stanley 
48198f3118dSJoel Stanley 	/* UART clock div13 setting */
48298f3118dSJoel Stanley 	regmap_read(map, ASPEED_MISC_CTRL, &val);
48398f3118dSJoel Stanley 	if (val & UART_DIV13_EN)
48498f3118dSJoel Stanley 		rate = 24000000 / 13;
48598f3118dSJoel Stanley 	else
48698f3118dSJoel Stanley 		rate = 24000000;
48798f3118dSJoel Stanley 	/* TODO: Find the parent data for the uart clock */
48898f3118dSJoel Stanley 	hw = clk_hw_register_fixed_rate(dev, "uart", NULL, 0, rate);
48998f3118dSJoel Stanley 	if (IS_ERR(hw))
49098f3118dSJoel Stanley 		return PTR_ERR(hw);
49198f3118dSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_UART] = hw;
49298f3118dSJoel Stanley 
49398f3118dSJoel Stanley 	/*
49498f3118dSJoel Stanley 	 * Memory controller (M-PLL) PLL. This clock is configured by the
49598f3118dSJoel Stanley 	 * bootloader, and is exposed to Linux as a read-only clock rate.
49698f3118dSJoel Stanley 	 */
49798f3118dSJoel Stanley 	regmap_read(map, ASPEED_MPLL_PARAM, &val);
49898f3118dSJoel Stanley 	hw = soc_data->calc_pll("mpll", val);
49998f3118dSJoel Stanley 	if (IS_ERR(hw))
50098f3118dSJoel Stanley 		return PTR_ERR(hw);
50198f3118dSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_MPLL] =	hw;
50298f3118dSJoel Stanley 
503*ebd5f82dSJoel Stanley 	/* SD/SDIO clock divider and gate */
504*ebd5f82dSJoel Stanley 	hw = clk_hw_register_gate(dev, "sd_extclk_gate", "hpll", 0,
505*ebd5f82dSJoel Stanley 				  scu_base + ASPEED_CLK_SELECTION, 15, 0,
506*ebd5f82dSJoel Stanley 				  &aspeed_clk_lock);
507*ebd5f82dSJoel Stanley 	if (IS_ERR(hw))
508*ebd5f82dSJoel Stanley 		return PTR_ERR(hw);
509*ebd5f82dSJoel Stanley 	hw = clk_hw_register_divider_table(dev, "sd_extclk", "sd_extclk_gate",
510*ebd5f82dSJoel Stanley 			0, scu_base + ASPEED_CLK_SELECTION, 12, 3, 0,
51198f3118dSJoel Stanley 			soc_data->div_table,
51298f3118dSJoel Stanley 			&aspeed_clk_lock);
51398f3118dSJoel Stanley 	if (IS_ERR(hw))
51498f3118dSJoel Stanley 		return PTR_ERR(hw);
51598f3118dSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_SDIO] = hw;
51698f3118dSJoel Stanley 
51798f3118dSJoel Stanley 	/* MAC AHB bus clock divider */
51898f3118dSJoel Stanley 	hw = clk_hw_register_divider_table(dev, "mac", "hpll", 0,
51998f3118dSJoel Stanley 			scu_base + ASPEED_CLK_SELECTION, 16, 3, 0,
52098f3118dSJoel Stanley 			soc_data->mac_div_table,
52198f3118dSJoel Stanley 			&aspeed_clk_lock);
52298f3118dSJoel Stanley 	if (IS_ERR(hw))
52398f3118dSJoel Stanley 		return PTR_ERR(hw);
52498f3118dSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_MAC] = hw;
52598f3118dSJoel Stanley 
52698f3118dSJoel Stanley 	/* LPC Host (LHCLK) clock divider */
52798f3118dSJoel Stanley 	hw = clk_hw_register_divider_table(dev, "lhclk", "hpll", 0,
52898f3118dSJoel Stanley 			scu_base + ASPEED_CLK_SELECTION, 20, 3, 0,
52998f3118dSJoel Stanley 			soc_data->div_table,
53098f3118dSJoel Stanley 			&aspeed_clk_lock);
53198f3118dSJoel Stanley 	if (IS_ERR(hw))
53298f3118dSJoel Stanley 		return PTR_ERR(hw);
53398f3118dSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_LHCLK] = hw;
53498f3118dSJoel Stanley 
53598f3118dSJoel Stanley 	/* P-Bus (BCLK) clock divider */
53698f3118dSJoel Stanley 	hw = clk_hw_register_divider_table(dev, "bclk", "hpll", 0,
53798f3118dSJoel Stanley 			scu_base + ASPEED_CLK_SELECTION_2, 0, 2, 0,
53898f3118dSJoel Stanley 			soc_data->div_table,
53998f3118dSJoel Stanley 			&aspeed_clk_lock);
54098f3118dSJoel Stanley 	if (IS_ERR(hw))
54198f3118dSJoel Stanley 		return PTR_ERR(hw);
54298f3118dSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_BCLK] = hw;
54398f3118dSJoel Stanley 
54467b6e5cfSLei YU 	/* Fixed 24MHz clock */
54567b6e5cfSLei YU 	hw = clk_hw_register_fixed_rate(NULL, "fixed-24m", "clkin",
54667b6e5cfSLei YU 					0, 24000000);
54767b6e5cfSLei YU 	if (IS_ERR(hw))
54867b6e5cfSLei YU 		return PTR_ERR(hw);
54967b6e5cfSLei YU 	aspeed_clk_data->hws[ASPEED_CLK_24M] = hw;
55067b6e5cfSLei YU 
551defb149bSEddie James 	hw = clk_hw_register_mux(dev, "eclk-mux", eclk_parent_names,
552defb149bSEddie James 				 ARRAY_SIZE(eclk_parent_names), 0,
553defb149bSEddie James 				 scu_base + ASPEED_CLK_SELECTION, 2, 0x3, 0,
554defb149bSEddie James 				 &aspeed_clk_lock);
555defb149bSEddie James 	if (IS_ERR(hw))
556defb149bSEddie James 		return PTR_ERR(hw);
557defb149bSEddie James 	aspeed_clk_data->hws[ASPEED_CLK_ECLK_MUX] = hw;
558defb149bSEddie James 
559defb149bSEddie James 	hw = clk_hw_register_divider_table(dev, "eclk", "eclk-mux", 0,
560defb149bSEddie James 					   scu_base + ASPEED_CLK_SELECTION, 28,
561defb149bSEddie James 					   3, 0, soc_data->eclk_div_table,
562defb149bSEddie James 					   &aspeed_clk_lock);
563defb149bSEddie James 	if (IS_ERR(hw))
564defb149bSEddie James 		return PTR_ERR(hw);
565defb149bSEddie James 	aspeed_clk_data->hws[ASPEED_CLK_ECLK] = hw;
566defb149bSEddie James 
56715ed8ce5SJoel Stanley 	/*
56815ed8ce5SJoel Stanley 	 * TODO: There are a number of clocks that not included in this driver
56915ed8ce5SJoel Stanley 	 * as more information is required:
57015ed8ce5SJoel Stanley 	 *   D2-PLL
57115ed8ce5SJoel Stanley 	 *   D-PLL
57215ed8ce5SJoel Stanley 	 *   YCLK
57315ed8ce5SJoel Stanley 	 *   RGMII
57415ed8ce5SJoel Stanley 	 *   RMII
57515ed8ce5SJoel Stanley 	 *   UART[1..5] clock source mux
57615ed8ce5SJoel Stanley 	 */
57715ed8ce5SJoel Stanley 
57815ed8ce5SJoel Stanley 	for (i = 0; i < ARRAY_SIZE(aspeed_gates); i++) {
57915ed8ce5SJoel Stanley 		const struct aspeed_gate_data *gd = &aspeed_gates[i];
5806671507fSBenjamin Herrenschmidt 		u32 gate_flags;
58115ed8ce5SJoel Stanley 
5826671507fSBenjamin Herrenschmidt 		/* Special case: the USB port 1 clock (bit 14) is always
5836671507fSBenjamin Herrenschmidt 		 * working the opposite way from the other ones.
5846671507fSBenjamin Herrenschmidt 		 */
5856671507fSBenjamin Herrenschmidt 		gate_flags = (gd->clock_idx == 14) ? 0 : CLK_GATE_SET_TO_DISABLE;
58615ed8ce5SJoel Stanley 		hw = aspeed_clk_hw_register_gate(dev,
58715ed8ce5SJoel Stanley 				gd->name,
58815ed8ce5SJoel Stanley 				gd->parent_name,
58915ed8ce5SJoel Stanley 				gd->flags,
59015ed8ce5SJoel Stanley 				map,
59115ed8ce5SJoel Stanley 				gd->clock_idx,
59215ed8ce5SJoel Stanley 				gd->reset_idx,
5936671507fSBenjamin Herrenschmidt 				gate_flags,
59415ed8ce5SJoel Stanley 				&aspeed_clk_lock);
59515ed8ce5SJoel Stanley 		if (IS_ERR(hw))
59615ed8ce5SJoel Stanley 			return PTR_ERR(hw);
59715ed8ce5SJoel Stanley 		aspeed_clk_data->hws[i] = hw;
59815ed8ce5SJoel Stanley 	}
59915ed8ce5SJoel Stanley 
60098f3118dSJoel Stanley 	return 0;
60198f3118dSJoel Stanley };
60298f3118dSJoel Stanley 
60398f3118dSJoel Stanley static const struct of_device_id aspeed_clk_dt_ids[] = {
60498f3118dSJoel Stanley 	{ .compatible = "aspeed,ast2400-scu", .data = &ast2400_data },
60598f3118dSJoel Stanley 	{ .compatible = "aspeed,ast2500-scu", .data = &ast2500_data },
60698f3118dSJoel Stanley 	{ }
60798f3118dSJoel Stanley };
60898f3118dSJoel Stanley 
60998f3118dSJoel Stanley static struct platform_driver aspeed_clk_driver = {
61098f3118dSJoel Stanley 	.probe  = aspeed_clk_probe,
61198f3118dSJoel Stanley 	.driver = {
61298f3118dSJoel Stanley 		.name = "aspeed-clk",
61398f3118dSJoel Stanley 		.of_match_table = aspeed_clk_dt_ids,
61498f3118dSJoel Stanley 		.suppress_bind_attrs = true,
61598f3118dSJoel Stanley 	},
61698f3118dSJoel Stanley };
61798f3118dSJoel Stanley builtin_platform_driver(aspeed_clk_driver);
61898f3118dSJoel Stanley 
61999d01e0eSJoel Stanley static void __init aspeed_ast2400_cc(struct regmap *map)
62099d01e0eSJoel Stanley {
62199d01e0eSJoel Stanley 	struct clk_hw *hw;
622565b9937SJoel Stanley 	u32 val, div, clkin, hpll;
623565b9937SJoel Stanley 	const u16 hpll_rates[][4] = {
624565b9937SJoel Stanley 		{384, 360, 336, 408},
625565b9937SJoel Stanley 		{400, 375, 350, 425},
626565b9937SJoel Stanley 	};
627565b9937SJoel Stanley 	int rate;
62899d01e0eSJoel Stanley 
62999d01e0eSJoel Stanley 	/*
63099d01e0eSJoel Stanley 	 * CLKIN is the crystal oscillator, 24, 48 or 25MHz selected by
63199d01e0eSJoel Stanley 	 * strapping
63299d01e0eSJoel Stanley 	 */
63399d01e0eSJoel Stanley 	regmap_read(map, ASPEED_STRAP, &val);
634565b9937SJoel Stanley 	rate = (val >> 8) & 3;
635565b9937SJoel Stanley 	if (val & CLKIN_25MHZ_EN) {
636565b9937SJoel Stanley 		clkin = 25000000;
637565b9937SJoel Stanley 		hpll = hpll_rates[1][rate];
638565b9937SJoel Stanley 	} else if (val & AST2400_CLK_SOURCE_SEL) {
639565b9937SJoel Stanley 		clkin = 48000000;
640565b9937SJoel Stanley 		hpll = hpll_rates[0][rate];
641565b9937SJoel Stanley 	} else {
642565b9937SJoel Stanley 		clkin = 24000000;
643565b9937SJoel Stanley 		hpll = hpll_rates[0][rate];
644565b9937SJoel Stanley 	}
645565b9937SJoel Stanley 	hw = clk_hw_register_fixed_rate(NULL, "clkin", NULL, 0, clkin);
646565b9937SJoel Stanley 	pr_debug("clkin @%u MHz\n", clkin / 1000000);
64799d01e0eSJoel Stanley 
64899d01e0eSJoel Stanley 	/*
64999d01e0eSJoel Stanley 	 * High-speed PLL clock derived from the crystal. This the CPU clock,
650565b9937SJoel Stanley 	 * and we assume that it is enabled. It can be configured through the
651565b9937SJoel Stanley 	 * HPLL_PARAM register, or set to a specified frequency by strapping.
65299d01e0eSJoel Stanley 	 */
65399d01e0eSJoel Stanley 	regmap_read(map, ASPEED_HPLL_PARAM, &val);
654565b9937SJoel Stanley 	if (val & AST2400_HPLL_PROGRAMMED)
655565b9937SJoel Stanley 		hw = aspeed_ast2400_calc_pll("hpll", val);
656565b9937SJoel Stanley 	else
657565b9937SJoel Stanley 		hw = clk_hw_register_fixed_rate(NULL, "hpll", "clkin", 0,
658565b9937SJoel Stanley 				hpll * 1000000);
659565b9937SJoel Stanley 
660565b9937SJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_HPLL] = hw;
66199d01e0eSJoel Stanley 
66299d01e0eSJoel Stanley 	/*
66399d01e0eSJoel Stanley 	 * Strap bits 11:10 define the CPU/AHB clock frequency ratio (aka HCLK)
66499d01e0eSJoel Stanley 	 *   00: Select CPU:AHB = 1:1
66599d01e0eSJoel Stanley 	 *   01: Select CPU:AHB = 2:1
66699d01e0eSJoel Stanley 	 *   10: Select CPU:AHB = 4:1
66799d01e0eSJoel Stanley 	 *   11: Select CPU:AHB = 3:1
66899d01e0eSJoel Stanley 	 */
66999d01e0eSJoel Stanley 	regmap_read(map, ASPEED_STRAP, &val);
67099d01e0eSJoel Stanley 	val = (val >> 10) & 0x3;
67199d01e0eSJoel Stanley 	div = val + 1;
67299d01e0eSJoel Stanley 	if (div == 3)
67399d01e0eSJoel Stanley 		div = 4;
67499d01e0eSJoel Stanley 	else if (div == 4)
67599d01e0eSJoel Stanley 		div = 3;
67699d01e0eSJoel Stanley 	hw = clk_hw_register_fixed_factor(NULL, "ahb", "hpll", 0, 1, div);
67799d01e0eSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_AHB] = hw;
67899d01e0eSJoel Stanley 
67999d01e0eSJoel Stanley 	/* APB clock clock selection register SCU08 (aka PCLK) */
68099d01e0eSJoel Stanley 	hw = clk_hw_register_divider_table(NULL, "apb", "hpll", 0,
68199d01e0eSJoel Stanley 			scu_base + ASPEED_CLK_SELECTION, 23, 3, 0,
68299d01e0eSJoel Stanley 			ast2400_div_table,
68399d01e0eSJoel Stanley 			&aspeed_clk_lock);
68499d01e0eSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_APB] = hw;
68599d01e0eSJoel Stanley }
68699d01e0eSJoel Stanley 
68799d01e0eSJoel Stanley static void __init aspeed_ast2500_cc(struct regmap *map)
68899d01e0eSJoel Stanley {
68999d01e0eSJoel Stanley 	struct clk_hw *hw;
69099d01e0eSJoel Stanley 	u32 val, freq, div;
69199d01e0eSJoel Stanley 
69299d01e0eSJoel Stanley 	/* CLKIN is the crystal oscillator, 24 or 25MHz selected by strapping */
69399d01e0eSJoel Stanley 	regmap_read(map, ASPEED_STRAP, &val);
69499d01e0eSJoel Stanley 	if (val & CLKIN_25MHZ_EN)
69599d01e0eSJoel Stanley 		freq = 25000000;
69699d01e0eSJoel Stanley 	else
69799d01e0eSJoel Stanley 		freq = 24000000;
69899d01e0eSJoel Stanley 	hw = clk_hw_register_fixed_rate(NULL, "clkin", NULL, 0, freq);
69999d01e0eSJoel Stanley 	pr_debug("clkin @%u MHz\n", freq / 1000000);
70099d01e0eSJoel Stanley 
70199d01e0eSJoel Stanley 	/*
70299d01e0eSJoel Stanley 	 * High-speed PLL clock derived from the crystal. This the CPU clock,
70399d01e0eSJoel Stanley 	 * and we assume that it is enabled
70499d01e0eSJoel Stanley 	 */
70599d01e0eSJoel Stanley 	regmap_read(map, ASPEED_HPLL_PARAM, &val);
70699d01e0eSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_HPLL] = aspeed_ast2500_calc_pll("hpll", val);
70799d01e0eSJoel Stanley 
70899d01e0eSJoel Stanley 	/* Strap bits 11:9 define the AXI/AHB clock frequency ratio (aka HCLK)*/
70999d01e0eSJoel Stanley 	regmap_read(map, ASPEED_STRAP, &val);
71099d01e0eSJoel Stanley 	val = (val >> 9) & 0x7;
71199d01e0eSJoel Stanley 	WARN(val == 0, "strapping is zero: cannot determine ahb clock");
71299d01e0eSJoel Stanley 	div = 2 * (val + 1);
71399d01e0eSJoel Stanley 	hw = clk_hw_register_fixed_factor(NULL, "ahb", "hpll", 0, 1, div);
71499d01e0eSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_AHB] = hw;
71599d01e0eSJoel Stanley 
71699d01e0eSJoel Stanley 	/* APB clock clock selection register SCU08 (aka PCLK) */
71799d01e0eSJoel Stanley 	regmap_read(map, ASPEED_CLK_SELECTION, &val);
71899d01e0eSJoel Stanley 	val = (val >> 23) & 0x7;
71999d01e0eSJoel Stanley 	div = 4 * (val + 1);
72099d01e0eSJoel Stanley 	hw = clk_hw_register_fixed_factor(NULL, "apb", "hpll", 0, 1, div);
72199d01e0eSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_APB] = hw;
72299d01e0eSJoel Stanley };
72399d01e0eSJoel Stanley 
7245eda5d79SJoel Stanley static void __init aspeed_cc_init(struct device_node *np)
7255eda5d79SJoel Stanley {
7265eda5d79SJoel Stanley 	struct regmap *map;
7275eda5d79SJoel Stanley 	u32 val;
7285eda5d79SJoel Stanley 	int ret;
7295eda5d79SJoel Stanley 	int i;
7305eda5d79SJoel Stanley 
7315eda5d79SJoel Stanley 	scu_base = of_iomap(np, 0);
732accf475aSWei Yongjun 	if (!scu_base)
7335eda5d79SJoel Stanley 		return;
7345eda5d79SJoel Stanley 
735acafe7e3SKees Cook 	aspeed_clk_data = kzalloc(struct_size(aspeed_clk_data, hws,
736acafe7e3SKees Cook 					      ASPEED_NUM_CLKS),
7375eda5d79SJoel Stanley 				  GFP_KERNEL);
7385eda5d79SJoel Stanley 	if (!aspeed_clk_data)
7395eda5d79SJoel Stanley 		return;
7405eda5d79SJoel Stanley 
7415eda5d79SJoel Stanley 	/*
7425eda5d79SJoel Stanley 	 * This way all clocks fetched before the platform device probes,
7435eda5d79SJoel Stanley 	 * except those we assign here for early use, will be deferred.
7445eda5d79SJoel Stanley 	 */
7455eda5d79SJoel Stanley 	for (i = 0; i < ASPEED_NUM_CLKS; i++)
7465eda5d79SJoel Stanley 		aspeed_clk_data->hws[i] = ERR_PTR(-EPROBE_DEFER);
7475eda5d79SJoel Stanley 
7485eda5d79SJoel Stanley 	map = syscon_node_to_regmap(np);
7495eda5d79SJoel Stanley 	if (IS_ERR(map)) {
7505eda5d79SJoel Stanley 		pr_err("no syscon regmap\n");
7515eda5d79SJoel Stanley 		return;
7525eda5d79SJoel Stanley 	}
7535eda5d79SJoel Stanley 	/*
7545eda5d79SJoel Stanley 	 * We check that the regmap works on this very first access,
7555eda5d79SJoel Stanley 	 * but as this is an MMIO-backed regmap, subsequent regmap
7565eda5d79SJoel Stanley 	 * access is not going to fail and we skip error checks from
7575eda5d79SJoel Stanley 	 * this point.
7585eda5d79SJoel Stanley 	 */
7595eda5d79SJoel Stanley 	ret = regmap_read(map, ASPEED_STRAP, &val);
7605eda5d79SJoel Stanley 	if (ret) {
7615eda5d79SJoel Stanley 		pr_err("failed to read strapping register\n");
7625eda5d79SJoel Stanley 		return;
7635eda5d79SJoel Stanley 	}
7645eda5d79SJoel Stanley 
76599d01e0eSJoel Stanley 	if (of_device_is_compatible(np, "aspeed,ast2400-scu"))
76699d01e0eSJoel Stanley 		aspeed_ast2400_cc(map);
76799d01e0eSJoel Stanley 	else if (of_device_is_compatible(np, "aspeed,ast2500-scu"))
76899d01e0eSJoel Stanley 		aspeed_ast2500_cc(map);
76999d01e0eSJoel Stanley 	else
77099d01e0eSJoel Stanley 		pr_err("unknown platform, failed to add clocks\n");
77199d01e0eSJoel Stanley 
7725eda5d79SJoel Stanley 	aspeed_clk_data->num = ASPEED_NUM_CLKS;
7735eda5d79SJoel Stanley 	ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, aspeed_clk_data);
7745eda5d79SJoel Stanley 	if (ret)
7755eda5d79SJoel Stanley 		pr_err("failed to add DT provider: %d\n", ret);
7765eda5d79SJoel Stanley };
7775eda5d79SJoel Stanley CLK_OF_DECLARE_DRIVER(aspeed_cc_g5, "aspeed,ast2500-scu", aspeed_cc_init);
7785eda5d79SJoel Stanley CLK_OF_DECLARE_DRIVER(aspeed_cc_g4, "aspeed,ast2400-scu", aspeed_cc_init);
779