xref: /openbmc/linux/drivers/clk/clk-aspeed.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
15eda5d79SJoel Stanley // SPDX-License-Identifier: GPL-2.0+
2c1c4942eSJoel Stanley // Copyright IBM Corp
35eda5d79SJoel Stanley 
45eda5d79SJoel Stanley #define pr_fmt(fmt) "clk-aspeed: " fmt
55eda5d79SJoel Stanley 
65eda5d79SJoel Stanley #include <linux/mfd/syscon.h>
7a96cbb14SRob Herring #include <linux/of.h>
85eda5d79SJoel Stanley #include <linux/of_address.h>
998f3118dSJoel Stanley #include <linux/platform_device.h>
105eda5d79SJoel Stanley #include <linux/regmap.h>
115eda5d79SJoel Stanley #include <linux/slab.h>
125eda5d79SJoel Stanley 
135eda5d79SJoel Stanley #include <dt-bindings/clock/aspeed-clock.h>
145eda5d79SJoel Stanley 
15c1c4942eSJoel Stanley #include "clk-aspeed.h"
16c1c4942eSJoel Stanley 
17801b787aSAndrew Jeffery #define ASPEED_NUM_CLKS		38
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)
31801b787aSAndrew Jeffery #define ASPEED_MAC_CLK_DLY	0x48
325eda5d79SJoel Stanley #define ASPEED_STRAP		0x70
3399d01e0eSJoel Stanley #define  CLKIN_25MHZ_EN		BIT(23)
3499d01e0eSJoel Stanley #define  AST2400_CLK_SOURCE_SEL	BIT(18)
3599d01e0eSJoel Stanley #define ASPEED_CLK_SELECTION_2	0xd8
36dcb899c4SJoel Stanley #define ASPEED_RESET_CTRL2	0xd4
3799d01e0eSJoel Stanley 
3899d01e0eSJoel Stanley /* Globally visible clocks */
3999d01e0eSJoel Stanley static DEFINE_SPINLOCK(aspeed_clk_lock);
405eda5d79SJoel Stanley 
415eda5d79SJoel Stanley /* Keeps track of all clocks */
425eda5d79SJoel Stanley static struct clk_hw_onecell_data *aspeed_clk_data;
435eda5d79SJoel Stanley 
445eda5d79SJoel Stanley static void __iomem *scu_base;
455eda5d79SJoel Stanley 
465eda5d79SJoel Stanley /* TODO: ask Aspeed about the actual parent data */
475eda5d79SJoel Stanley static const struct aspeed_gate_data aspeed_gates[] = {
485eda5d79SJoel Stanley 	/*				 clk rst   name			parent	flags */
49defb149bSEddie James 	[ASPEED_CLK_GATE_ECLK] =	{  0,  6, "eclk-gate",		"eclk",	0 }, /* Video Engine */
505eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_GCLK] =	{  1,  7, "gclk-gate",		NULL,	0 }, /* 2D engine */
515eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_MCLK] =	{  2, -1, "mclk-gate",		"mpll",	CLK_IS_CRITICAL }, /* SDRAM */
52defb149bSEddie James 	[ASPEED_CLK_GATE_VCLK] =	{  3, -1, "vclk-gate",		NULL,	0 }, /* Video Capture */
53974c7c6dSJoel Stanley 	[ASPEED_CLK_GATE_BCLK] =	{  4,  8, "bclk-gate",		"bclk",	CLK_IS_CRITICAL }, /* PCIe/PCI */
54974c7c6dSJoel Stanley 	[ASPEED_CLK_GATE_DCLK] =	{  5, -1, "dclk-gate",		NULL,	CLK_IS_CRITICAL }, /* DAC */
555eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_REFCLK] =	{  6, -1, "refclk-gate",	"clkin", CLK_IS_CRITICAL },
565eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_USBPORT2CLK] =	{  7,  3, "usb-port2-gate",	NULL,	0 }, /* USB2.0 Host port 2 */
575eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_LCLK] =	{  8,  5, "lclk-gate",		NULL,	0 }, /* LPC */
585eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_USBUHCICLK] =	{  9, 15, "usb-uhci-gate",	NULL,	0 }, /* USB1.1 (requires port 2 enabled) */
595eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_D1CLK] =	{ 10, 13, "d1clk-gate",		NULL,	0 }, /* GFX CRT */
605eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_YCLK] =	{ 13,  4, "yclk-gate",		NULL,	0 }, /* HAC */
615eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_USBPORT1CLK] = { 14, 14, "usb-port1-gate",	NULL,	0 }, /* USB2 hub/USB2 host port 1/USB1.1 dev */
625eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_UART1CLK] =	{ 15, -1, "uart1clk-gate",	"uart",	0 }, /* UART1 */
635eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_UART2CLK] =	{ 16, -1, "uart2clk-gate",	"uart",	0 }, /* UART2 */
645eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_UART5CLK] =	{ 17, -1, "uart5clk-gate",	"uart",	0 }, /* UART5 */
655eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_ESPICLK] =	{ 19, -1, "espiclk-gate",	NULL,	0 }, /* eSPI */
665eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_MAC1CLK] =	{ 20, 11, "mac1clk-gate",	"mac",	0 }, /* MAC1 */
675eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_MAC2CLK] =	{ 21, 12, "mac2clk-gate",	"mac",	0 }, /* MAC2 */
685eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_RSACLK] =	{ 24, -1, "rsaclk-gate",	NULL,	0 }, /* RSA */
695eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_UART3CLK] =	{ 25, -1, "uart3clk-gate",	"uart",	0 }, /* UART3 */
705eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_UART4CLK] =	{ 26, -1, "uart4clk-gate",	"uart",	0 }, /* UART4 */
71cd88259aSLei YU 	[ASPEED_CLK_GATE_SDCLK] =	{ 27, 16, "sdclk-gate",		NULL,	0 }, /* SDIO/SD */
725eda5d79SJoel Stanley 	[ASPEED_CLK_GATE_LHCCLK] =	{ 28, -1, "lhclk-gate",		"lhclk", 0 }, /* LPC master/LPC+ */
735eda5d79SJoel Stanley };
745eda5d79SJoel Stanley 
75defb149bSEddie James static const char * const eclk_parent_names[] = {
76defb149bSEddie James 	"mpll",
77defb149bSEddie James 	"hpll",
78defb149bSEddie James 	"dpll",
79defb149bSEddie James };
80defb149bSEddie James 
81defb149bSEddie James static const struct clk_div_table ast2500_eclk_div_table[] = {
82defb149bSEddie James 	{ 0x0, 2 },
83defb149bSEddie James 	{ 0x1, 2 },
84defb149bSEddie James 	{ 0x2, 3 },
85defb149bSEddie James 	{ 0x3, 4 },
86defb149bSEddie James 	{ 0x4, 5 },
87defb149bSEddie James 	{ 0x5, 6 },
88defb149bSEddie James 	{ 0x6, 7 },
89defb149bSEddie James 	{ 0x7, 8 },
90defb149bSEddie James 	{ 0 }
91defb149bSEddie James };
92defb149bSEddie James 
9398f3118dSJoel Stanley static const struct clk_div_table ast2500_mac_div_table[] = {
9498f3118dSJoel Stanley 	{ 0x0, 4 }, /* Yep, really. Aspeed confirmed this is correct */
9598f3118dSJoel Stanley 	{ 0x1, 4 },
9698f3118dSJoel Stanley 	{ 0x2, 6 },
9798f3118dSJoel Stanley 	{ 0x3, 8 },
9898f3118dSJoel Stanley 	{ 0x4, 10 },
9998f3118dSJoel Stanley 	{ 0x5, 12 },
10098f3118dSJoel Stanley 	{ 0x6, 14 },
10198f3118dSJoel Stanley 	{ 0x7, 16 },
10298f3118dSJoel Stanley 	{ 0 }
10398f3118dSJoel Stanley };
10498f3118dSJoel Stanley 
10599d01e0eSJoel Stanley static const struct clk_div_table ast2400_div_table[] = {
10699d01e0eSJoel Stanley 	{ 0x0, 2 },
10799d01e0eSJoel Stanley 	{ 0x1, 4 },
10899d01e0eSJoel Stanley 	{ 0x2, 6 },
10999d01e0eSJoel Stanley 	{ 0x3, 8 },
11099d01e0eSJoel Stanley 	{ 0x4, 10 },
11199d01e0eSJoel Stanley 	{ 0x5, 12 },
11299d01e0eSJoel Stanley 	{ 0x6, 14 },
11399d01e0eSJoel Stanley 	{ 0x7, 16 },
11499d01e0eSJoel Stanley 	{ 0 }
11599d01e0eSJoel Stanley };
11699d01e0eSJoel Stanley 
11799d01e0eSJoel Stanley static const struct clk_div_table ast2500_div_table[] = {
11899d01e0eSJoel Stanley 	{ 0x0, 4 },
11999d01e0eSJoel Stanley 	{ 0x1, 8 },
12099d01e0eSJoel Stanley 	{ 0x2, 12 },
12199d01e0eSJoel Stanley 	{ 0x3, 16 },
12299d01e0eSJoel Stanley 	{ 0x4, 20 },
12399d01e0eSJoel Stanley 	{ 0x5, 24 },
12499d01e0eSJoel Stanley 	{ 0x6, 28 },
12599d01e0eSJoel Stanley 	{ 0x7, 32 },
12699d01e0eSJoel Stanley 	{ 0 }
12799d01e0eSJoel Stanley };
12899d01e0eSJoel Stanley 
aspeed_ast2400_calc_pll(const char * name,u32 val)12999d01e0eSJoel Stanley static struct clk_hw *aspeed_ast2400_calc_pll(const char *name, u32 val)
13099d01e0eSJoel Stanley {
13199d01e0eSJoel Stanley 	unsigned int mult, div;
13299d01e0eSJoel Stanley 
13399d01e0eSJoel Stanley 	if (val & AST2400_HPLL_BYPASS_EN) {
13499d01e0eSJoel Stanley 		/* Pass through mode */
13599d01e0eSJoel Stanley 		mult = div = 1;
13699d01e0eSJoel Stanley 	} else {
13799d01e0eSJoel Stanley 		/* F = 24Mhz * (2-OD) * [(N + 2) / (D + 1)] */
13899d01e0eSJoel Stanley 		u32 n = (val >> 5) & 0x3f;
13999d01e0eSJoel Stanley 		u32 od = (val >> 4) & 0x1;
14099d01e0eSJoel Stanley 		u32 d = val & 0xf;
14199d01e0eSJoel Stanley 
14299d01e0eSJoel Stanley 		mult = (2 - od) * (n + 2);
14399d01e0eSJoel Stanley 		div = d + 1;
14499d01e0eSJoel Stanley 	}
14599d01e0eSJoel Stanley 	return clk_hw_register_fixed_factor(NULL, name, "clkin", 0,
14699d01e0eSJoel Stanley 			mult, div);
14799d01e0eSJoel Stanley };
14899d01e0eSJoel Stanley 
aspeed_ast2500_calc_pll(const char * name,u32 val)14999d01e0eSJoel Stanley static struct clk_hw *aspeed_ast2500_calc_pll(const char *name, u32 val)
15099d01e0eSJoel Stanley {
15199d01e0eSJoel Stanley 	unsigned int mult, div;
15299d01e0eSJoel Stanley 
15399d01e0eSJoel Stanley 	if (val & AST2500_HPLL_BYPASS_EN) {
15499d01e0eSJoel Stanley 		/* Pass through mode */
15599d01e0eSJoel Stanley 		mult = div = 1;
15699d01e0eSJoel Stanley 	} else {
15799d01e0eSJoel Stanley 		/* F = clkin * [(M+1) / (N+1)] / (P + 1) */
15899d01e0eSJoel Stanley 		u32 p = (val >> 13) & 0x3f;
15999d01e0eSJoel Stanley 		u32 m = (val >> 5) & 0xff;
16099d01e0eSJoel Stanley 		u32 n = val & 0x1f;
16199d01e0eSJoel Stanley 
16299d01e0eSJoel Stanley 		mult = (m + 1) / (n + 1);
16399d01e0eSJoel Stanley 		div = p + 1;
16499d01e0eSJoel Stanley 	}
16599d01e0eSJoel Stanley 
16699d01e0eSJoel Stanley 	return clk_hw_register_fixed_factor(NULL, name, "clkin", 0,
16799d01e0eSJoel Stanley 			mult, div);
16899d01e0eSJoel Stanley }
16999d01e0eSJoel Stanley 
17098f3118dSJoel Stanley static const struct aspeed_clk_soc_data ast2500_data = {
17198f3118dSJoel Stanley 	.div_table = ast2500_div_table,
172defb149bSEddie James 	.eclk_div_table = ast2500_eclk_div_table,
17398f3118dSJoel Stanley 	.mac_div_table = ast2500_mac_div_table,
17498f3118dSJoel Stanley 	.calc_pll = aspeed_ast2500_calc_pll,
17598f3118dSJoel Stanley };
17698f3118dSJoel Stanley 
17798f3118dSJoel Stanley static const struct aspeed_clk_soc_data ast2400_data = {
17898f3118dSJoel Stanley 	.div_table = ast2400_div_table,
179defb149bSEddie James 	.eclk_div_table = ast2400_div_table,
18098f3118dSJoel Stanley 	.mac_div_table = ast2400_div_table,
18198f3118dSJoel Stanley 	.calc_pll = aspeed_ast2400_calc_pll,
18298f3118dSJoel Stanley };
18398f3118dSJoel Stanley 
aspeed_clk_is_enabled(struct clk_hw * hw)1848a53fc51SEddie James static int aspeed_clk_is_enabled(struct clk_hw *hw)
1858a53fc51SEddie James {
1868a53fc51SEddie James 	struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
1878a53fc51SEddie James 	u32 clk = BIT(gate->clock_idx);
188edc6f7e9SBenjamin Herrenschmidt 	u32 rst = BIT(gate->reset_idx);
1898a53fc51SEddie James 	u32 enval = (gate->flags & CLK_GATE_SET_TO_DISABLE) ? 0 : clk;
1908a53fc51SEddie James 	u32 reg;
1918a53fc51SEddie James 
192edc6f7e9SBenjamin Herrenschmidt 	/*
193edc6f7e9SBenjamin Herrenschmidt 	 * If the IP is in reset, treat the clock as not enabled,
194edc6f7e9SBenjamin Herrenschmidt 	 * this happens with some clocks such as the USB one when
195edc6f7e9SBenjamin Herrenschmidt 	 * coming from cold reset. Without this, aspeed_clk_enable()
196edc6f7e9SBenjamin Herrenschmidt 	 * will fail to lift the reset.
197edc6f7e9SBenjamin Herrenschmidt 	 */
198edc6f7e9SBenjamin Herrenschmidt 	if (gate->reset_idx >= 0) {
199edc6f7e9SBenjamin Herrenschmidt 		regmap_read(gate->map, ASPEED_RESET_CTRL, &reg);
200edc6f7e9SBenjamin Herrenschmidt 		if (reg & rst)
201edc6f7e9SBenjamin Herrenschmidt 			return 0;
202edc6f7e9SBenjamin Herrenschmidt 	}
203edc6f7e9SBenjamin Herrenschmidt 
2048a53fc51SEddie James 	regmap_read(gate->map, ASPEED_CLK_STOP_CTRL, &reg);
2058a53fc51SEddie James 
2068a53fc51SEddie James 	return ((reg & clk) == enval) ? 1 : 0;
2078a53fc51SEddie James }
2088a53fc51SEddie James 
aspeed_clk_enable(struct clk_hw * hw)20915ed8ce5SJoel Stanley static int aspeed_clk_enable(struct clk_hw *hw)
21015ed8ce5SJoel Stanley {
21115ed8ce5SJoel Stanley 	struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
21215ed8ce5SJoel Stanley 	unsigned long flags;
21315ed8ce5SJoel Stanley 	u32 clk = BIT(gate->clock_idx);
21415ed8ce5SJoel Stanley 	u32 rst = BIT(gate->reset_idx);
2156671507fSBenjamin Herrenschmidt 	u32 enval;
21615ed8ce5SJoel Stanley 
21715ed8ce5SJoel Stanley 	spin_lock_irqsave(gate->lock, flags);
21815ed8ce5SJoel Stanley 
2198a53fc51SEddie James 	if (aspeed_clk_is_enabled(hw)) {
2208a53fc51SEddie James 		spin_unlock_irqrestore(gate->lock, flags);
2218a53fc51SEddie James 		return 0;
2228a53fc51SEddie James 	}
2238a53fc51SEddie James 
22415ed8ce5SJoel Stanley 	if (gate->reset_idx >= 0) {
22515ed8ce5SJoel Stanley 		/* Put IP in reset */
22615ed8ce5SJoel Stanley 		regmap_update_bits(gate->map, ASPEED_RESET_CTRL, rst, rst);
22715ed8ce5SJoel Stanley 
22815ed8ce5SJoel Stanley 		/* Delay 100us */
22915ed8ce5SJoel Stanley 		udelay(100);
23015ed8ce5SJoel Stanley 	}
23115ed8ce5SJoel Stanley 
23215ed8ce5SJoel Stanley 	/* Enable clock */
2336671507fSBenjamin Herrenschmidt 	enval = (gate->flags & CLK_GATE_SET_TO_DISABLE) ? 0 : clk;
2346671507fSBenjamin Herrenschmidt 	regmap_update_bits(gate->map, ASPEED_CLK_STOP_CTRL, clk, enval);
23515ed8ce5SJoel Stanley 
23615ed8ce5SJoel Stanley 	if (gate->reset_idx >= 0) {
23715ed8ce5SJoel Stanley 		/* A delay of 10ms is specified by the ASPEED docs */
23815ed8ce5SJoel Stanley 		mdelay(10);
23915ed8ce5SJoel Stanley 
24015ed8ce5SJoel Stanley 		/* Take IP out of reset */
24115ed8ce5SJoel Stanley 		regmap_update_bits(gate->map, ASPEED_RESET_CTRL, rst, 0);
24215ed8ce5SJoel Stanley 	}
24315ed8ce5SJoel Stanley 
24415ed8ce5SJoel Stanley 	spin_unlock_irqrestore(gate->lock, flags);
24515ed8ce5SJoel Stanley 
24615ed8ce5SJoel Stanley 	return 0;
24715ed8ce5SJoel Stanley }
24815ed8ce5SJoel Stanley 
aspeed_clk_disable(struct clk_hw * hw)24915ed8ce5SJoel Stanley static void aspeed_clk_disable(struct clk_hw *hw)
25015ed8ce5SJoel Stanley {
25115ed8ce5SJoel Stanley 	struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
25215ed8ce5SJoel Stanley 	unsigned long flags;
25315ed8ce5SJoel Stanley 	u32 clk = BIT(gate->clock_idx);
2546671507fSBenjamin Herrenschmidt 	u32 enval;
25515ed8ce5SJoel Stanley 
25615ed8ce5SJoel Stanley 	spin_lock_irqsave(gate->lock, flags);
25715ed8ce5SJoel Stanley 
2586671507fSBenjamin Herrenschmidt 	enval = (gate->flags & CLK_GATE_SET_TO_DISABLE) ? clk : 0;
2596671507fSBenjamin Herrenschmidt 	regmap_update_bits(gate->map, ASPEED_CLK_STOP_CTRL, clk, enval);
26015ed8ce5SJoel Stanley 
26115ed8ce5SJoel Stanley 	spin_unlock_irqrestore(gate->lock, flags);
26215ed8ce5SJoel Stanley }
26315ed8ce5SJoel Stanley 
26415ed8ce5SJoel Stanley static const struct clk_ops aspeed_clk_gate_ops = {
26515ed8ce5SJoel Stanley 	.enable = aspeed_clk_enable,
26615ed8ce5SJoel Stanley 	.disable = aspeed_clk_disable,
26715ed8ce5SJoel Stanley 	.is_enabled = aspeed_clk_is_enabled,
26815ed8ce5SJoel Stanley };
26915ed8ce5SJoel Stanley 
270f7989839SJoel Stanley static const u8 aspeed_resets[] = {
271dcb899c4SJoel Stanley 	/* SCU04 resets */
272f7989839SJoel Stanley 	[ASPEED_RESET_XDMA]	= 25,
273f7989839SJoel Stanley 	[ASPEED_RESET_MCTP]	= 24,
274f7989839SJoel Stanley 	[ASPEED_RESET_ADC]	= 23,
275f7989839SJoel Stanley 	[ASPEED_RESET_JTAG_MASTER] = 22,
276f7989839SJoel Stanley 	[ASPEED_RESET_MIC]	= 18,
277f7989839SJoel Stanley 	[ASPEED_RESET_PWM]	=  9,
278e76e5682SJae Hyun Yoo 	[ASPEED_RESET_PECI]	= 10,
279f7989839SJoel Stanley 	[ASPEED_RESET_I2C]	=  2,
280f7989839SJoel Stanley 	[ASPEED_RESET_AHB]	=  1,
281dcb899c4SJoel Stanley 
282dcb899c4SJoel Stanley 	/*
283dcb899c4SJoel Stanley 	 * SCUD4 resets start at an offset to separate them from
284dcb899c4SJoel Stanley 	 * the SCU04 resets.
285dcb899c4SJoel Stanley 	 */
286dcb899c4SJoel Stanley 	[ASPEED_RESET_CRT1]	= ASPEED_RESET2_OFFSET + 5,
287f7989839SJoel Stanley };
288f7989839SJoel Stanley 
aspeed_reset_deassert(struct reset_controller_dev * rcdev,unsigned long id)289f7989839SJoel Stanley static int aspeed_reset_deassert(struct reset_controller_dev *rcdev,
290f7989839SJoel Stanley 				 unsigned long id)
291f7989839SJoel Stanley {
292f7989839SJoel Stanley 	struct aspeed_reset *ar = to_aspeed_reset(rcdev);
293dcb899c4SJoel Stanley 	u32 reg = ASPEED_RESET_CTRL;
294dcb899c4SJoel Stanley 	u32 bit = aspeed_resets[id];
295f7989839SJoel Stanley 
296dcb899c4SJoel Stanley 	if (bit >= ASPEED_RESET2_OFFSET) {
297dcb899c4SJoel Stanley 		bit -= ASPEED_RESET2_OFFSET;
298dcb899c4SJoel Stanley 		reg = ASPEED_RESET_CTRL2;
299dcb899c4SJoel Stanley 	}
300dcb899c4SJoel Stanley 
301dcb899c4SJoel Stanley 	return regmap_update_bits(ar->map, reg, BIT(bit), 0);
302f7989839SJoel Stanley }
303f7989839SJoel Stanley 
aspeed_reset_assert(struct reset_controller_dev * rcdev,unsigned long id)304f7989839SJoel Stanley static int aspeed_reset_assert(struct reset_controller_dev *rcdev,
305f7989839SJoel Stanley 			       unsigned long id)
306f7989839SJoel Stanley {
307f7989839SJoel Stanley 	struct aspeed_reset *ar = to_aspeed_reset(rcdev);
308dcb899c4SJoel Stanley 	u32 reg = ASPEED_RESET_CTRL;
309dcb899c4SJoel Stanley 	u32 bit = aspeed_resets[id];
310f7989839SJoel Stanley 
311dcb899c4SJoel Stanley 	if (bit >= ASPEED_RESET2_OFFSET) {
312dcb899c4SJoel Stanley 		bit -= ASPEED_RESET2_OFFSET;
313dcb899c4SJoel Stanley 		reg = ASPEED_RESET_CTRL2;
314dcb899c4SJoel Stanley 	}
315dcb899c4SJoel Stanley 
316dcb899c4SJoel Stanley 	return regmap_update_bits(ar->map, reg, BIT(bit), BIT(bit));
317f7989839SJoel Stanley }
318f7989839SJoel Stanley 
aspeed_reset_status(struct reset_controller_dev * rcdev,unsigned long id)319f7989839SJoel Stanley static int aspeed_reset_status(struct reset_controller_dev *rcdev,
320f7989839SJoel Stanley 			       unsigned long id)
321f7989839SJoel Stanley {
322f7989839SJoel Stanley 	struct aspeed_reset *ar = to_aspeed_reset(rcdev);
323dcb899c4SJoel Stanley 	u32 reg = ASPEED_RESET_CTRL;
324dcb899c4SJoel Stanley 	u32 bit = aspeed_resets[id];
325dcb899c4SJoel Stanley 	int ret, val;
326f7989839SJoel Stanley 
327dcb899c4SJoel Stanley 	if (bit >= ASPEED_RESET2_OFFSET) {
328dcb899c4SJoel Stanley 		bit -= ASPEED_RESET2_OFFSET;
329dcb899c4SJoel Stanley 		reg = ASPEED_RESET_CTRL2;
330dcb899c4SJoel Stanley 	}
331dcb899c4SJoel Stanley 
332dcb899c4SJoel Stanley 	ret = regmap_read(ar->map, reg, &val);
333f7989839SJoel Stanley 	if (ret)
334f7989839SJoel Stanley 		return ret;
335f7989839SJoel Stanley 
336dcb899c4SJoel Stanley 	return !!(val & BIT(bit));
337f7989839SJoel Stanley }
338f7989839SJoel Stanley 
339f7989839SJoel Stanley static const struct reset_control_ops aspeed_reset_ops = {
340f7989839SJoel Stanley 	.assert = aspeed_reset_assert,
341f7989839SJoel Stanley 	.deassert = aspeed_reset_deassert,
342f7989839SJoel Stanley 	.status = aspeed_reset_status,
343f7989839SJoel Stanley };
344f7989839SJoel Stanley 
aspeed_clk_hw_register_gate(struct device * dev,const char * name,const char * parent_name,unsigned long flags,struct regmap * map,u8 clock_idx,u8 reset_idx,u8 clk_gate_flags,spinlock_t * lock)34515ed8ce5SJoel Stanley static struct clk_hw *aspeed_clk_hw_register_gate(struct device *dev,
34615ed8ce5SJoel Stanley 		const char *name, const char *parent_name, unsigned long flags,
34715ed8ce5SJoel Stanley 		struct regmap *map, u8 clock_idx, u8 reset_idx,
34815ed8ce5SJoel Stanley 		u8 clk_gate_flags, spinlock_t *lock)
34915ed8ce5SJoel Stanley {
35015ed8ce5SJoel Stanley 	struct aspeed_clk_gate *gate;
35115ed8ce5SJoel Stanley 	struct clk_init_data init;
35215ed8ce5SJoel Stanley 	struct clk_hw *hw;
35315ed8ce5SJoel Stanley 	int ret;
35415ed8ce5SJoel Stanley 
35515ed8ce5SJoel Stanley 	gate = kzalloc(sizeof(*gate), GFP_KERNEL);
35615ed8ce5SJoel Stanley 	if (!gate)
35715ed8ce5SJoel Stanley 		return ERR_PTR(-ENOMEM);
35815ed8ce5SJoel Stanley 
35915ed8ce5SJoel Stanley 	init.name = name;
36015ed8ce5SJoel Stanley 	init.ops = &aspeed_clk_gate_ops;
36115ed8ce5SJoel Stanley 	init.flags = flags;
36215ed8ce5SJoel Stanley 	init.parent_names = parent_name ? &parent_name : NULL;
36315ed8ce5SJoel Stanley 	init.num_parents = parent_name ? 1 : 0;
36415ed8ce5SJoel Stanley 
36515ed8ce5SJoel Stanley 	gate->map = map;
36615ed8ce5SJoel Stanley 	gate->clock_idx = clock_idx;
36715ed8ce5SJoel Stanley 	gate->reset_idx = reset_idx;
36815ed8ce5SJoel Stanley 	gate->flags = clk_gate_flags;
36915ed8ce5SJoel Stanley 	gate->lock = lock;
37015ed8ce5SJoel Stanley 	gate->hw.init = &init;
37115ed8ce5SJoel Stanley 
37215ed8ce5SJoel Stanley 	hw = &gate->hw;
37315ed8ce5SJoel Stanley 	ret = clk_hw_register(dev, hw);
37415ed8ce5SJoel Stanley 	if (ret) {
37515ed8ce5SJoel Stanley 		kfree(gate);
37615ed8ce5SJoel Stanley 		hw = ERR_PTR(ret);
37715ed8ce5SJoel Stanley 	}
37815ed8ce5SJoel Stanley 
37915ed8ce5SJoel Stanley 	return hw;
38015ed8ce5SJoel Stanley }
38115ed8ce5SJoel Stanley 
aspeed_clk_probe(struct platform_device * pdev)38298f3118dSJoel Stanley static int aspeed_clk_probe(struct platform_device *pdev)
38398f3118dSJoel Stanley {
38498f3118dSJoel Stanley 	const struct aspeed_clk_soc_data *soc_data;
38598f3118dSJoel Stanley 	struct device *dev = &pdev->dev;
386f7989839SJoel Stanley 	struct aspeed_reset *ar;
38798f3118dSJoel Stanley 	struct regmap *map;
38898f3118dSJoel Stanley 	struct clk_hw *hw;
38998f3118dSJoel Stanley 	u32 val, rate;
390f7989839SJoel Stanley 	int i, ret;
39198f3118dSJoel Stanley 
39298f3118dSJoel Stanley 	map = syscon_node_to_regmap(dev->of_node);
39398f3118dSJoel Stanley 	if (IS_ERR(map)) {
39498f3118dSJoel Stanley 		dev_err(dev, "no syscon regmap\n");
39598f3118dSJoel Stanley 		return PTR_ERR(map);
39698f3118dSJoel Stanley 	}
39798f3118dSJoel Stanley 
398f7989839SJoel Stanley 	ar = devm_kzalloc(dev, sizeof(*ar), GFP_KERNEL);
399f7989839SJoel Stanley 	if (!ar)
400f7989839SJoel Stanley 		return -ENOMEM;
401f7989839SJoel Stanley 
402f7989839SJoel Stanley 	ar->map = map;
403f7989839SJoel Stanley 	ar->rcdev.owner = THIS_MODULE;
404f7989839SJoel Stanley 	ar->rcdev.nr_resets = ARRAY_SIZE(aspeed_resets);
405f7989839SJoel Stanley 	ar->rcdev.ops = &aspeed_reset_ops;
406f7989839SJoel Stanley 	ar->rcdev.of_node = dev->of_node;
407f7989839SJoel Stanley 
408f7989839SJoel Stanley 	ret = devm_reset_controller_register(dev, &ar->rcdev);
409f7989839SJoel Stanley 	if (ret) {
410f7989839SJoel Stanley 		dev_err(dev, "could not register reset controller\n");
411f7989839SJoel Stanley 		return ret;
412f7989839SJoel Stanley 	}
413f7989839SJoel Stanley 
41498f3118dSJoel Stanley 	/* SoC generations share common layouts but have different divisors */
41598f3118dSJoel Stanley 	soc_data = of_device_get_match_data(dev);
41698f3118dSJoel Stanley 	if (!soc_data) {
41798f3118dSJoel Stanley 		dev_err(dev, "no match data for platform\n");
41898f3118dSJoel Stanley 		return -EINVAL;
41998f3118dSJoel Stanley 	}
42098f3118dSJoel Stanley 
42198f3118dSJoel Stanley 	/* UART clock div13 setting */
42298f3118dSJoel Stanley 	regmap_read(map, ASPEED_MISC_CTRL, &val);
42398f3118dSJoel Stanley 	if (val & UART_DIV13_EN)
42498f3118dSJoel Stanley 		rate = 24000000 / 13;
42598f3118dSJoel Stanley 	else
42698f3118dSJoel Stanley 		rate = 24000000;
42798f3118dSJoel Stanley 	/* TODO: Find the parent data for the uart clock */
42898f3118dSJoel Stanley 	hw = clk_hw_register_fixed_rate(dev, "uart", NULL, 0, rate);
42998f3118dSJoel Stanley 	if (IS_ERR(hw))
43098f3118dSJoel Stanley 		return PTR_ERR(hw);
43198f3118dSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_UART] = hw;
43298f3118dSJoel Stanley 
43398f3118dSJoel Stanley 	/*
43498f3118dSJoel Stanley 	 * Memory controller (M-PLL) PLL. This clock is configured by the
43598f3118dSJoel Stanley 	 * bootloader, and is exposed to Linux as a read-only clock rate.
43698f3118dSJoel Stanley 	 */
43798f3118dSJoel Stanley 	regmap_read(map, ASPEED_MPLL_PARAM, &val);
43898f3118dSJoel Stanley 	hw = soc_data->calc_pll("mpll", val);
43998f3118dSJoel Stanley 	if (IS_ERR(hw))
44098f3118dSJoel Stanley 		return PTR_ERR(hw);
44198f3118dSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_MPLL] =	hw;
44298f3118dSJoel Stanley 
443ebd5f82dSJoel Stanley 	/* SD/SDIO clock divider and gate */
444ebd5f82dSJoel Stanley 	hw = clk_hw_register_gate(dev, "sd_extclk_gate", "hpll", 0,
445ebd5f82dSJoel Stanley 				  scu_base + ASPEED_CLK_SELECTION, 15, 0,
446ebd5f82dSJoel Stanley 				  &aspeed_clk_lock);
447ebd5f82dSJoel Stanley 	if (IS_ERR(hw))
448ebd5f82dSJoel Stanley 		return PTR_ERR(hw);
449ebd5f82dSJoel Stanley 	hw = clk_hw_register_divider_table(dev, "sd_extclk", "sd_extclk_gate",
450ebd5f82dSJoel Stanley 			0, scu_base + ASPEED_CLK_SELECTION, 12, 3, 0,
45198f3118dSJoel Stanley 			soc_data->div_table,
45298f3118dSJoel Stanley 			&aspeed_clk_lock);
45398f3118dSJoel Stanley 	if (IS_ERR(hw))
45498f3118dSJoel Stanley 		return PTR_ERR(hw);
45598f3118dSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_SDIO] = hw;
45698f3118dSJoel Stanley 
45798f3118dSJoel Stanley 	/* MAC AHB bus clock divider */
45898f3118dSJoel Stanley 	hw = clk_hw_register_divider_table(dev, "mac", "hpll", 0,
45998f3118dSJoel Stanley 			scu_base + ASPEED_CLK_SELECTION, 16, 3, 0,
46098f3118dSJoel Stanley 			soc_data->mac_div_table,
46198f3118dSJoel Stanley 			&aspeed_clk_lock);
46298f3118dSJoel Stanley 	if (IS_ERR(hw))
46398f3118dSJoel Stanley 		return PTR_ERR(hw);
46498f3118dSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_MAC] = hw;
46598f3118dSJoel Stanley 
466801b787aSAndrew Jeffery 	if (of_device_is_compatible(pdev->dev.of_node, "aspeed,ast2500-scu")) {
467801b787aSAndrew Jeffery 		/* RMII 50MHz RCLK */
468801b787aSAndrew Jeffery 		hw = clk_hw_register_fixed_rate(dev, "mac12rclk", "hpll", 0,
469801b787aSAndrew Jeffery 						50000000);
470801b787aSAndrew Jeffery 		if (IS_ERR(hw))
471801b787aSAndrew Jeffery 			return PTR_ERR(hw);
472801b787aSAndrew Jeffery 
473801b787aSAndrew Jeffery 		/* RMII1 50MHz (RCLK) output enable */
474801b787aSAndrew Jeffery 		hw = clk_hw_register_gate(dev, "mac1rclk", "mac12rclk", 0,
475801b787aSAndrew Jeffery 				scu_base + ASPEED_MAC_CLK_DLY, 29, 0,
476801b787aSAndrew Jeffery 				&aspeed_clk_lock);
477801b787aSAndrew Jeffery 		if (IS_ERR(hw))
478801b787aSAndrew Jeffery 			return PTR_ERR(hw);
479801b787aSAndrew Jeffery 		aspeed_clk_data->hws[ASPEED_CLK_MAC1RCLK] = hw;
480801b787aSAndrew Jeffery 
481801b787aSAndrew Jeffery 		/* RMII2 50MHz (RCLK) output enable */
482801b787aSAndrew Jeffery 		hw = clk_hw_register_gate(dev, "mac2rclk", "mac12rclk", 0,
483801b787aSAndrew Jeffery 				scu_base + ASPEED_MAC_CLK_DLY, 30, 0,
484801b787aSAndrew Jeffery 				&aspeed_clk_lock);
485801b787aSAndrew Jeffery 		if (IS_ERR(hw))
486801b787aSAndrew Jeffery 			return PTR_ERR(hw);
487801b787aSAndrew Jeffery 		aspeed_clk_data->hws[ASPEED_CLK_MAC2RCLK] = hw;
488801b787aSAndrew Jeffery 	}
489801b787aSAndrew Jeffery 
49098f3118dSJoel Stanley 	/* LPC Host (LHCLK) clock divider */
49198f3118dSJoel Stanley 	hw = clk_hw_register_divider_table(dev, "lhclk", "hpll", 0,
49298f3118dSJoel Stanley 			scu_base + ASPEED_CLK_SELECTION, 20, 3, 0,
49398f3118dSJoel Stanley 			soc_data->div_table,
49498f3118dSJoel Stanley 			&aspeed_clk_lock);
49598f3118dSJoel Stanley 	if (IS_ERR(hw))
49698f3118dSJoel Stanley 		return PTR_ERR(hw);
49798f3118dSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_LHCLK] = hw;
49898f3118dSJoel Stanley 
49998f3118dSJoel Stanley 	/* P-Bus (BCLK) clock divider */
50098f3118dSJoel Stanley 	hw = clk_hw_register_divider_table(dev, "bclk", "hpll", 0,
50198f3118dSJoel Stanley 			scu_base + ASPEED_CLK_SELECTION_2, 0, 2, 0,
50298f3118dSJoel Stanley 			soc_data->div_table,
50398f3118dSJoel Stanley 			&aspeed_clk_lock);
50498f3118dSJoel Stanley 	if (IS_ERR(hw))
50598f3118dSJoel Stanley 		return PTR_ERR(hw);
50698f3118dSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_BCLK] = hw;
50798f3118dSJoel Stanley 
50867b6e5cfSLei YU 	/* Fixed 24MHz clock */
50967b6e5cfSLei YU 	hw = clk_hw_register_fixed_rate(NULL, "fixed-24m", "clkin",
51067b6e5cfSLei YU 					0, 24000000);
51167b6e5cfSLei YU 	if (IS_ERR(hw))
51267b6e5cfSLei YU 		return PTR_ERR(hw);
51367b6e5cfSLei YU 	aspeed_clk_data->hws[ASPEED_CLK_24M] = hw;
51467b6e5cfSLei YU 
515defb149bSEddie James 	hw = clk_hw_register_mux(dev, "eclk-mux", eclk_parent_names,
516defb149bSEddie James 				 ARRAY_SIZE(eclk_parent_names), 0,
517defb149bSEddie James 				 scu_base + ASPEED_CLK_SELECTION, 2, 0x3, 0,
518defb149bSEddie James 				 &aspeed_clk_lock);
519defb149bSEddie James 	if (IS_ERR(hw))
520defb149bSEddie James 		return PTR_ERR(hw);
521defb149bSEddie James 	aspeed_clk_data->hws[ASPEED_CLK_ECLK_MUX] = hw;
522defb149bSEddie James 
523defb149bSEddie James 	hw = clk_hw_register_divider_table(dev, "eclk", "eclk-mux", 0,
524defb149bSEddie James 					   scu_base + ASPEED_CLK_SELECTION, 28,
525defb149bSEddie James 					   3, 0, soc_data->eclk_div_table,
526defb149bSEddie James 					   &aspeed_clk_lock);
527defb149bSEddie James 	if (IS_ERR(hw))
528defb149bSEddie James 		return PTR_ERR(hw);
529defb149bSEddie James 	aspeed_clk_data->hws[ASPEED_CLK_ECLK] = hw;
530defb149bSEddie James 
53115ed8ce5SJoel Stanley 	/*
53215ed8ce5SJoel Stanley 	 * TODO: There are a number of clocks that not included in this driver
53315ed8ce5SJoel Stanley 	 * as more information is required:
53415ed8ce5SJoel Stanley 	 *   D2-PLL
53515ed8ce5SJoel Stanley 	 *   D-PLL
53615ed8ce5SJoel Stanley 	 *   YCLK
53715ed8ce5SJoel Stanley 	 *   RGMII
53815ed8ce5SJoel Stanley 	 *   RMII
53915ed8ce5SJoel Stanley 	 *   UART[1..5] clock source mux
54015ed8ce5SJoel Stanley 	 */
54115ed8ce5SJoel Stanley 
54215ed8ce5SJoel Stanley 	for (i = 0; i < ARRAY_SIZE(aspeed_gates); i++) {
54315ed8ce5SJoel Stanley 		const struct aspeed_gate_data *gd = &aspeed_gates[i];
5446671507fSBenjamin Herrenschmidt 		u32 gate_flags;
54515ed8ce5SJoel Stanley 
5466671507fSBenjamin Herrenschmidt 		/* Special case: the USB port 1 clock (bit 14) is always
5476671507fSBenjamin Herrenschmidt 		 * working the opposite way from the other ones.
5486671507fSBenjamin Herrenschmidt 		 */
5496671507fSBenjamin Herrenschmidt 		gate_flags = (gd->clock_idx == 14) ? 0 : CLK_GATE_SET_TO_DISABLE;
55015ed8ce5SJoel Stanley 		hw = aspeed_clk_hw_register_gate(dev,
55115ed8ce5SJoel Stanley 				gd->name,
55215ed8ce5SJoel Stanley 				gd->parent_name,
55315ed8ce5SJoel Stanley 				gd->flags,
55415ed8ce5SJoel Stanley 				map,
55515ed8ce5SJoel Stanley 				gd->clock_idx,
55615ed8ce5SJoel Stanley 				gd->reset_idx,
5576671507fSBenjamin Herrenschmidt 				gate_flags,
55815ed8ce5SJoel Stanley 				&aspeed_clk_lock);
55915ed8ce5SJoel Stanley 		if (IS_ERR(hw))
56015ed8ce5SJoel Stanley 			return PTR_ERR(hw);
56115ed8ce5SJoel Stanley 		aspeed_clk_data->hws[i] = hw;
56215ed8ce5SJoel Stanley 	}
56315ed8ce5SJoel Stanley 
56498f3118dSJoel Stanley 	return 0;
56598f3118dSJoel Stanley };
56698f3118dSJoel Stanley 
56798f3118dSJoel Stanley static const struct of_device_id aspeed_clk_dt_ids[] = {
56898f3118dSJoel Stanley 	{ .compatible = "aspeed,ast2400-scu", .data = &ast2400_data },
56998f3118dSJoel Stanley 	{ .compatible = "aspeed,ast2500-scu", .data = &ast2500_data },
57098f3118dSJoel Stanley 	{ }
57198f3118dSJoel Stanley };
57298f3118dSJoel Stanley 
57398f3118dSJoel Stanley static struct platform_driver aspeed_clk_driver = {
57498f3118dSJoel Stanley 	.probe  = aspeed_clk_probe,
57598f3118dSJoel Stanley 	.driver = {
57698f3118dSJoel Stanley 		.name = "aspeed-clk",
57798f3118dSJoel Stanley 		.of_match_table = aspeed_clk_dt_ids,
57898f3118dSJoel Stanley 		.suppress_bind_attrs = true,
57998f3118dSJoel Stanley 	},
58098f3118dSJoel Stanley };
58198f3118dSJoel Stanley builtin_platform_driver(aspeed_clk_driver);
58298f3118dSJoel Stanley 
aspeed_ast2400_cc(struct regmap * map)58399d01e0eSJoel Stanley static void __init aspeed_ast2400_cc(struct regmap *map)
58499d01e0eSJoel Stanley {
58599d01e0eSJoel Stanley 	struct clk_hw *hw;
586565b9937SJoel Stanley 	u32 val, div, clkin, hpll;
587565b9937SJoel Stanley 	const u16 hpll_rates[][4] = {
588565b9937SJoel Stanley 		{384, 360, 336, 408},
589565b9937SJoel Stanley 		{400, 375, 350, 425},
590565b9937SJoel Stanley 	};
591565b9937SJoel Stanley 	int rate;
59299d01e0eSJoel Stanley 
59399d01e0eSJoel Stanley 	/*
59499d01e0eSJoel Stanley 	 * CLKIN is the crystal oscillator, 24, 48 or 25MHz selected by
59599d01e0eSJoel Stanley 	 * strapping
59699d01e0eSJoel Stanley 	 */
59799d01e0eSJoel Stanley 	regmap_read(map, ASPEED_STRAP, &val);
598565b9937SJoel Stanley 	rate = (val >> 8) & 3;
599565b9937SJoel Stanley 	if (val & CLKIN_25MHZ_EN) {
600565b9937SJoel Stanley 		clkin = 25000000;
601565b9937SJoel Stanley 		hpll = hpll_rates[1][rate];
602565b9937SJoel Stanley 	} else if (val & AST2400_CLK_SOURCE_SEL) {
603565b9937SJoel Stanley 		clkin = 48000000;
604565b9937SJoel Stanley 		hpll = hpll_rates[0][rate];
605565b9937SJoel Stanley 	} else {
606565b9937SJoel Stanley 		clkin = 24000000;
607565b9937SJoel Stanley 		hpll = hpll_rates[0][rate];
608565b9937SJoel Stanley 	}
609565b9937SJoel Stanley 	hw = clk_hw_register_fixed_rate(NULL, "clkin", NULL, 0, clkin);
610565b9937SJoel Stanley 	pr_debug("clkin @%u MHz\n", clkin / 1000000);
61199d01e0eSJoel Stanley 
61299d01e0eSJoel Stanley 	/*
61399d01e0eSJoel Stanley 	 * High-speed PLL clock derived from the crystal. This the CPU clock,
614565b9937SJoel Stanley 	 * and we assume that it is enabled. It can be configured through the
615565b9937SJoel Stanley 	 * HPLL_PARAM register, or set to a specified frequency by strapping.
61699d01e0eSJoel Stanley 	 */
61799d01e0eSJoel Stanley 	regmap_read(map, ASPEED_HPLL_PARAM, &val);
618565b9937SJoel Stanley 	if (val & AST2400_HPLL_PROGRAMMED)
619565b9937SJoel Stanley 		hw = aspeed_ast2400_calc_pll("hpll", val);
620565b9937SJoel Stanley 	else
621565b9937SJoel Stanley 		hw = clk_hw_register_fixed_rate(NULL, "hpll", "clkin", 0,
622565b9937SJoel Stanley 				hpll * 1000000);
623565b9937SJoel Stanley 
624565b9937SJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_HPLL] = hw;
62599d01e0eSJoel Stanley 
62699d01e0eSJoel Stanley 	/*
62799d01e0eSJoel Stanley 	 * Strap bits 11:10 define the CPU/AHB clock frequency ratio (aka HCLK)
62899d01e0eSJoel Stanley 	 *   00: Select CPU:AHB = 1:1
62999d01e0eSJoel Stanley 	 *   01: Select CPU:AHB = 2:1
63099d01e0eSJoel Stanley 	 *   10: Select CPU:AHB = 4:1
63199d01e0eSJoel Stanley 	 *   11: Select CPU:AHB = 3:1
63299d01e0eSJoel Stanley 	 */
63399d01e0eSJoel Stanley 	regmap_read(map, ASPEED_STRAP, &val);
63499d01e0eSJoel Stanley 	val = (val >> 10) & 0x3;
63599d01e0eSJoel Stanley 	div = val + 1;
63699d01e0eSJoel Stanley 	if (div == 3)
63799d01e0eSJoel Stanley 		div = 4;
63899d01e0eSJoel Stanley 	else if (div == 4)
63999d01e0eSJoel Stanley 		div = 3;
64099d01e0eSJoel Stanley 	hw = clk_hw_register_fixed_factor(NULL, "ahb", "hpll", 0, 1, div);
64199d01e0eSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_AHB] = hw;
64299d01e0eSJoel Stanley 
64399d01e0eSJoel Stanley 	/* APB clock clock selection register SCU08 (aka PCLK) */
64499d01e0eSJoel Stanley 	hw = clk_hw_register_divider_table(NULL, "apb", "hpll", 0,
64599d01e0eSJoel Stanley 			scu_base + ASPEED_CLK_SELECTION, 23, 3, 0,
64699d01e0eSJoel Stanley 			ast2400_div_table,
64799d01e0eSJoel Stanley 			&aspeed_clk_lock);
64899d01e0eSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_APB] = hw;
64999d01e0eSJoel Stanley }
65099d01e0eSJoel Stanley 
aspeed_ast2500_cc(struct regmap * map)65199d01e0eSJoel Stanley static void __init aspeed_ast2500_cc(struct regmap *map)
65299d01e0eSJoel Stanley {
65399d01e0eSJoel Stanley 	struct clk_hw *hw;
65499d01e0eSJoel Stanley 	u32 val, freq, div;
65599d01e0eSJoel Stanley 
65699d01e0eSJoel Stanley 	/* CLKIN is the crystal oscillator, 24 or 25MHz selected by strapping */
65799d01e0eSJoel Stanley 	regmap_read(map, ASPEED_STRAP, &val);
65899d01e0eSJoel Stanley 	if (val & CLKIN_25MHZ_EN)
65999d01e0eSJoel Stanley 		freq = 25000000;
66099d01e0eSJoel Stanley 	else
66199d01e0eSJoel Stanley 		freq = 24000000;
66299d01e0eSJoel Stanley 	hw = clk_hw_register_fixed_rate(NULL, "clkin", NULL, 0, freq);
66399d01e0eSJoel Stanley 	pr_debug("clkin @%u MHz\n", freq / 1000000);
66499d01e0eSJoel Stanley 
66599d01e0eSJoel Stanley 	/*
66699d01e0eSJoel Stanley 	 * High-speed PLL clock derived from the crystal. This the CPU clock,
66799d01e0eSJoel Stanley 	 * and we assume that it is enabled
66899d01e0eSJoel Stanley 	 */
66999d01e0eSJoel Stanley 	regmap_read(map, ASPEED_HPLL_PARAM, &val);
67099d01e0eSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_HPLL] = aspeed_ast2500_calc_pll("hpll", val);
67199d01e0eSJoel Stanley 
67299d01e0eSJoel Stanley 	/* Strap bits 11:9 define the AXI/AHB clock frequency ratio (aka HCLK)*/
67399d01e0eSJoel Stanley 	regmap_read(map, ASPEED_STRAP, &val);
67499d01e0eSJoel Stanley 	val = (val >> 9) & 0x7;
67599d01e0eSJoel Stanley 	WARN(val == 0, "strapping is zero: cannot determine ahb clock");
67699d01e0eSJoel Stanley 	div = 2 * (val + 1);
67799d01e0eSJoel Stanley 	hw = clk_hw_register_fixed_factor(NULL, "ahb", "hpll", 0, 1, div);
67899d01e0eSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_AHB] = hw;
67999d01e0eSJoel Stanley 
68099d01e0eSJoel Stanley 	/* APB clock clock selection register SCU08 (aka PCLK) */
68199d01e0eSJoel Stanley 	regmap_read(map, ASPEED_CLK_SELECTION, &val);
68299d01e0eSJoel Stanley 	val = (val >> 23) & 0x7;
68399d01e0eSJoel Stanley 	div = 4 * (val + 1);
68499d01e0eSJoel Stanley 	hw = clk_hw_register_fixed_factor(NULL, "apb", "hpll", 0, 1, div);
68599d01e0eSJoel Stanley 	aspeed_clk_data->hws[ASPEED_CLK_APB] = hw;
68699d01e0eSJoel Stanley };
68799d01e0eSJoel Stanley 
aspeed_cc_init(struct device_node * np)6885eda5d79SJoel Stanley static void __init aspeed_cc_init(struct device_node *np)
6895eda5d79SJoel Stanley {
6905eda5d79SJoel Stanley 	struct regmap *map;
6915eda5d79SJoel Stanley 	u32 val;
6925eda5d79SJoel Stanley 	int ret;
6935eda5d79SJoel Stanley 	int i;
6945eda5d79SJoel Stanley 
6955eda5d79SJoel Stanley 	scu_base = of_iomap(np, 0);
696accf475aSWei Yongjun 	if (!scu_base)
6975eda5d79SJoel Stanley 		return;
6985eda5d79SJoel Stanley 
699acafe7e3SKees Cook 	aspeed_clk_data = kzalloc(struct_size(aspeed_clk_data, hws,
700acafe7e3SKees Cook 					      ASPEED_NUM_CLKS),
7015eda5d79SJoel Stanley 				  GFP_KERNEL);
7025eda5d79SJoel Stanley 	if (!aspeed_clk_data)
7035eda5d79SJoel Stanley 		return;
704*f316cdffSKees Cook 	aspeed_clk_data->num = ASPEED_NUM_CLKS;
7055eda5d79SJoel Stanley 
7065eda5d79SJoel Stanley 	/*
7075eda5d79SJoel Stanley 	 * This way all clocks fetched before the platform device probes,
7085eda5d79SJoel Stanley 	 * except those we assign here for early use, will be deferred.
7095eda5d79SJoel Stanley 	 */
7105eda5d79SJoel Stanley 	for (i = 0; i < ASPEED_NUM_CLKS; i++)
7115eda5d79SJoel Stanley 		aspeed_clk_data->hws[i] = ERR_PTR(-EPROBE_DEFER);
7125eda5d79SJoel Stanley 
7135eda5d79SJoel Stanley 	map = syscon_node_to_regmap(np);
7145eda5d79SJoel Stanley 	if (IS_ERR(map)) {
7155eda5d79SJoel Stanley 		pr_err("no syscon regmap\n");
7165eda5d79SJoel Stanley 		return;
7175eda5d79SJoel Stanley 	}
7185eda5d79SJoel Stanley 	/*
7195eda5d79SJoel Stanley 	 * We check that the regmap works on this very first access,
7205eda5d79SJoel Stanley 	 * but as this is an MMIO-backed regmap, subsequent regmap
7215eda5d79SJoel Stanley 	 * access is not going to fail and we skip error checks from
7225eda5d79SJoel Stanley 	 * this point.
7235eda5d79SJoel Stanley 	 */
7245eda5d79SJoel Stanley 	ret = regmap_read(map, ASPEED_STRAP, &val);
7255eda5d79SJoel Stanley 	if (ret) {
7265eda5d79SJoel Stanley 		pr_err("failed to read strapping register\n");
7275eda5d79SJoel Stanley 		return;
7285eda5d79SJoel Stanley 	}
7295eda5d79SJoel Stanley 
73099d01e0eSJoel Stanley 	if (of_device_is_compatible(np, "aspeed,ast2400-scu"))
73199d01e0eSJoel Stanley 		aspeed_ast2400_cc(map);
73299d01e0eSJoel Stanley 	else if (of_device_is_compatible(np, "aspeed,ast2500-scu"))
73399d01e0eSJoel Stanley 		aspeed_ast2500_cc(map);
73499d01e0eSJoel Stanley 	else
73599d01e0eSJoel Stanley 		pr_err("unknown platform, failed to add clocks\n");
7365eda5d79SJoel Stanley 	ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, aspeed_clk_data);
7375eda5d79SJoel Stanley 	if (ret)
7385eda5d79SJoel Stanley 		pr_err("failed to add DT provider: %d\n", ret);
7395eda5d79SJoel Stanley };
7405eda5d79SJoel Stanley CLK_OF_DECLARE_DRIVER(aspeed_cc_g5, "aspeed,ast2500-scu", aspeed_cc_init);
7415eda5d79SJoel Stanley CLK_OF_DECLARE_DRIVER(aspeed_cc_g4, "aspeed,ast2400-scu", aspeed_cc_init);
742