18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci// Copyright IBM Corp
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "clk-aspeed: " fmt
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
78c2ecf20Sopenharmony_ci#include <linux/of_address.h>
88c2ecf20Sopenharmony_ci#include <linux/of_device.h>
98c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
108c2ecf20Sopenharmony_ci#include <linux/regmap.h>
118c2ecf20Sopenharmony_ci#include <linux/slab.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <dt-bindings/clock/aspeed-clock.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include "clk-aspeed.h"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define ASPEED_NUM_CLKS		38
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define ASPEED_RESET2_OFFSET	32
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define ASPEED_RESET_CTRL	0x04
228c2ecf20Sopenharmony_ci#define ASPEED_CLK_SELECTION	0x08
238c2ecf20Sopenharmony_ci#define ASPEED_CLK_STOP_CTRL	0x0c
248c2ecf20Sopenharmony_ci#define ASPEED_MPLL_PARAM	0x20
258c2ecf20Sopenharmony_ci#define ASPEED_HPLL_PARAM	0x24
268c2ecf20Sopenharmony_ci#define  AST2500_HPLL_BYPASS_EN	BIT(20)
278c2ecf20Sopenharmony_ci#define  AST2400_HPLL_PROGRAMMED BIT(18)
288c2ecf20Sopenharmony_ci#define  AST2400_HPLL_BYPASS_EN	BIT(17)
298c2ecf20Sopenharmony_ci#define ASPEED_MISC_CTRL	0x2c
308c2ecf20Sopenharmony_ci#define  UART_DIV13_EN		BIT(12)
318c2ecf20Sopenharmony_ci#define ASPEED_MAC_CLK_DLY	0x48
328c2ecf20Sopenharmony_ci#define ASPEED_STRAP		0x70
338c2ecf20Sopenharmony_ci#define  CLKIN_25MHZ_EN		BIT(23)
348c2ecf20Sopenharmony_ci#define  AST2400_CLK_SOURCE_SEL	BIT(18)
358c2ecf20Sopenharmony_ci#define ASPEED_CLK_SELECTION_2	0xd8
368c2ecf20Sopenharmony_ci#define ASPEED_RESET_CTRL2	0xd4
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci/* Globally visible clocks */
398c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(aspeed_clk_lock);
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/* Keeps track of all clocks */
428c2ecf20Sopenharmony_cistatic struct clk_hw_onecell_data *aspeed_clk_data;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic void __iomem *scu_base;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci/* TODO: ask Aspeed about the actual parent data */
478c2ecf20Sopenharmony_cistatic const struct aspeed_gate_data aspeed_gates[] = {
488c2ecf20Sopenharmony_ci	/*				 clk rst   name			parent	flags */
498c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_ECLK] =	{  0,  6, "eclk-gate",		"eclk",	0 }, /* Video Engine */
508c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_GCLK] =	{  1,  7, "gclk-gate",		NULL,	0 }, /* 2D engine */
518c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_MCLK] =	{  2, -1, "mclk-gate",		"mpll",	CLK_IS_CRITICAL }, /* SDRAM */
528c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_VCLK] =	{  3, -1, "vclk-gate",		NULL,	0 }, /* Video Capture */
538c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_BCLK] =	{  4,  8, "bclk-gate",		"bclk",	CLK_IS_CRITICAL }, /* PCIe/PCI */
548c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_DCLK] =	{  5, -1, "dclk-gate",		NULL,	CLK_IS_CRITICAL }, /* DAC */
558c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_REFCLK] =	{  6, -1, "refclk-gate",	"clkin", CLK_IS_CRITICAL },
568c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_USBPORT2CLK] =	{  7,  3, "usb-port2-gate",	NULL,	0 }, /* USB2.0 Host port 2 */
578c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_LCLK] =	{  8,  5, "lclk-gate",		NULL,	0 }, /* LPC */
588c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_USBUHCICLK] =	{  9, 15, "usb-uhci-gate",	NULL,	0 }, /* USB1.1 (requires port 2 enabled) */
598c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_D1CLK] =	{ 10, 13, "d1clk-gate",		NULL,	0 }, /* GFX CRT */
608c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_YCLK] =	{ 13,  4, "yclk-gate",		NULL,	0 }, /* HAC */
618c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_USBPORT1CLK] = { 14, 14, "usb-port1-gate",	NULL,	0 }, /* USB2 hub/USB2 host port 1/USB1.1 dev */
628c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_UART1CLK] =	{ 15, -1, "uart1clk-gate",	"uart",	0 }, /* UART1 */
638c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_UART2CLK] =	{ 16, -1, "uart2clk-gate",	"uart",	0 }, /* UART2 */
648c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_UART5CLK] =	{ 17, -1, "uart5clk-gate",	"uart",	0 }, /* UART5 */
658c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_ESPICLK] =	{ 19, -1, "espiclk-gate",	NULL,	0 }, /* eSPI */
668c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_MAC1CLK] =	{ 20, 11, "mac1clk-gate",	"mac",	0 }, /* MAC1 */
678c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_MAC2CLK] =	{ 21, 12, "mac2clk-gate",	"mac",	0 }, /* MAC2 */
688c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_RSACLK] =	{ 24, -1, "rsaclk-gate",	NULL,	0 }, /* RSA */
698c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_UART3CLK] =	{ 25, -1, "uart3clk-gate",	"uart",	0 }, /* UART3 */
708c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_UART4CLK] =	{ 26, -1, "uart4clk-gate",	"uart",	0 }, /* UART4 */
718c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_SDCLK] =	{ 27, 16, "sdclk-gate",		NULL,	0 }, /* SDIO/SD */
728c2ecf20Sopenharmony_ci	[ASPEED_CLK_GATE_LHCCLK] =	{ 28, -1, "lhclk-gate",		"lhclk", 0 }, /* LPC master/LPC+ */
738c2ecf20Sopenharmony_ci};
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic const char * const eclk_parent_names[] = {
768c2ecf20Sopenharmony_ci	"mpll",
778c2ecf20Sopenharmony_ci	"hpll",
788c2ecf20Sopenharmony_ci	"dpll",
798c2ecf20Sopenharmony_ci};
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic const struct clk_div_table ast2500_eclk_div_table[] = {
828c2ecf20Sopenharmony_ci	{ 0x0, 2 },
838c2ecf20Sopenharmony_ci	{ 0x1, 2 },
848c2ecf20Sopenharmony_ci	{ 0x2, 3 },
858c2ecf20Sopenharmony_ci	{ 0x3, 4 },
868c2ecf20Sopenharmony_ci	{ 0x4, 5 },
878c2ecf20Sopenharmony_ci	{ 0x5, 6 },
888c2ecf20Sopenharmony_ci	{ 0x6, 7 },
898c2ecf20Sopenharmony_ci	{ 0x7, 8 },
908c2ecf20Sopenharmony_ci	{ 0 }
918c2ecf20Sopenharmony_ci};
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic const struct clk_div_table ast2500_mac_div_table[] = {
948c2ecf20Sopenharmony_ci	{ 0x0, 4 }, /* Yep, really. Aspeed confirmed this is correct */
958c2ecf20Sopenharmony_ci	{ 0x1, 4 },
968c2ecf20Sopenharmony_ci	{ 0x2, 6 },
978c2ecf20Sopenharmony_ci	{ 0x3, 8 },
988c2ecf20Sopenharmony_ci	{ 0x4, 10 },
998c2ecf20Sopenharmony_ci	{ 0x5, 12 },
1008c2ecf20Sopenharmony_ci	{ 0x6, 14 },
1018c2ecf20Sopenharmony_ci	{ 0x7, 16 },
1028c2ecf20Sopenharmony_ci	{ 0 }
1038c2ecf20Sopenharmony_ci};
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cistatic const struct clk_div_table ast2400_div_table[] = {
1068c2ecf20Sopenharmony_ci	{ 0x0, 2 },
1078c2ecf20Sopenharmony_ci	{ 0x1, 4 },
1088c2ecf20Sopenharmony_ci	{ 0x2, 6 },
1098c2ecf20Sopenharmony_ci	{ 0x3, 8 },
1108c2ecf20Sopenharmony_ci	{ 0x4, 10 },
1118c2ecf20Sopenharmony_ci	{ 0x5, 12 },
1128c2ecf20Sopenharmony_ci	{ 0x6, 14 },
1138c2ecf20Sopenharmony_ci	{ 0x7, 16 },
1148c2ecf20Sopenharmony_ci	{ 0 }
1158c2ecf20Sopenharmony_ci};
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic const struct clk_div_table ast2500_div_table[] = {
1188c2ecf20Sopenharmony_ci	{ 0x0, 4 },
1198c2ecf20Sopenharmony_ci	{ 0x1, 8 },
1208c2ecf20Sopenharmony_ci	{ 0x2, 12 },
1218c2ecf20Sopenharmony_ci	{ 0x3, 16 },
1228c2ecf20Sopenharmony_ci	{ 0x4, 20 },
1238c2ecf20Sopenharmony_ci	{ 0x5, 24 },
1248c2ecf20Sopenharmony_ci	{ 0x6, 28 },
1258c2ecf20Sopenharmony_ci	{ 0x7, 32 },
1268c2ecf20Sopenharmony_ci	{ 0 }
1278c2ecf20Sopenharmony_ci};
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic struct clk_hw *aspeed_ast2400_calc_pll(const char *name, u32 val)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	unsigned int mult, div;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	if (val & AST2400_HPLL_BYPASS_EN) {
1348c2ecf20Sopenharmony_ci		/* Pass through mode */
1358c2ecf20Sopenharmony_ci		mult = div = 1;
1368c2ecf20Sopenharmony_ci	} else {
1378c2ecf20Sopenharmony_ci		/* F = 24Mhz * (2-OD) * [(N + 2) / (D + 1)] */
1388c2ecf20Sopenharmony_ci		u32 n = (val >> 5) & 0x3f;
1398c2ecf20Sopenharmony_ci		u32 od = (val >> 4) & 0x1;
1408c2ecf20Sopenharmony_ci		u32 d = val & 0xf;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci		mult = (2 - od) * (n + 2);
1438c2ecf20Sopenharmony_ci		div = d + 1;
1448c2ecf20Sopenharmony_ci	}
1458c2ecf20Sopenharmony_ci	return clk_hw_register_fixed_factor(NULL, name, "clkin", 0,
1468c2ecf20Sopenharmony_ci			mult, div);
1478c2ecf20Sopenharmony_ci};
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistatic struct clk_hw *aspeed_ast2500_calc_pll(const char *name, u32 val)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	unsigned int mult, div;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	if (val & AST2500_HPLL_BYPASS_EN) {
1548c2ecf20Sopenharmony_ci		/* Pass through mode */
1558c2ecf20Sopenharmony_ci		mult = div = 1;
1568c2ecf20Sopenharmony_ci	} else {
1578c2ecf20Sopenharmony_ci		/* F = clkin * [(M+1) / (N+1)] / (P + 1) */
1588c2ecf20Sopenharmony_ci		u32 p = (val >> 13) & 0x3f;
1598c2ecf20Sopenharmony_ci		u32 m = (val >> 5) & 0xff;
1608c2ecf20Sopenharmony_ci		u32 n = val & 0x1f;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci		mult = (m + 1) / (n + 1);
1638c2ecf20Sopenharmony_ci		div = p + 1;
1648c2ecf20Sopenharmony_ci	}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	return clk_hw_register_fixed_factor(NULL, name, "clkin", 0,
1678c2ecf20Sopenharmony_ci			mult, div);
1688c2ecf20Sopenharmony_ci}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic const struct aspeed_clk_soc_data ast2500_data = {
1718c2ecf20Sopenharmony_ci	.div_table = ast2500_div_table,
1728c2ecf20Sopenharmony_ci	.eclk_div_table = ast2500_eclk_div_table,
1738c2ecf20Sopenharmony_ci	.mac_div_table = ast2500_mac_div_table,
1748c2ecf20Sopenharmony_ci	.calc_pll = aspeed_ast2500_calc_pll,
1758c2ecf20Sopenharmony_ci};
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic const struct aspeed_clk_soc_data ast2400_data = {
1788c2ecf20Sopenharmony_ci	.div_table = ast2400_div_table,
1798c2ecf20Sopenharmony_ci	.eclk_div_table = ast2400_div_table,
1808c2ecf20Sopenharmony_ci	.mac_div_table = ast2400_div_table,
1818c2ecf20Sopenharmony_ci	.calc_pll = aspeed_ast2400_calc_pll,
1828c2ecf20Sopenharmony_ci};
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic int aspeed_clk_is_enabled(struct clk_hw *hw)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
1878c2ecf20Sopenharmony_ci	u32 clk = BIT(gate->clock_idx);
1888c2ecf20Sopenharmony_ci	u32 rst = BIT(gate->reset_idx);
1898c2ecf20Sopenharmony_ci	u32 enval = (gate->flags & CLK_GATE_SET_TO_DISABLE) ? 0 : clk;
1908c2ecf20Sopenharmony_ci	u32 reg;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	/*
1938c2ecf20Sopenharmony_ci	 * If the IP is in reset, treat the clock as not enabled,
1948c2ecf20Sopenharmony_ci	 * this happens with some clocks such as the USB one when
1958c2ecf20Sopenharmony_ci	 * coming from cold reset. Without this, aspeed_clk_enable()
1968c2ecf20Sopenharmony_ci	 * will fail to lift the reset.
1978c2ecf20Sopenharmony_ci	 */
1988c2ecf20Sopenharmony_ci	if (gate->reset_idx >= 0) {
1998c2ecf20Sopenharmony_ci		regmap_read(gate->map, ASPEED_RESET_CTRL, &reg);
2008c2ecf20Sopenharmony_ci		if (reg & rst)
2018c2ecf20Sopenharmony_ci			return 0;
2028c2ecf20Sopenharmony_ci	}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	regmap_read(gate->map, ASPEED_CLK_STOP_CTRL, &reg);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	return ((reg & clk) == enval) ? 1 : 0;
2078c2ecf20Sopenharmony_ci}
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_cistatic int aspeed_clk_enable(struct clk_hw *hw)
2108c2ecf20Sopenharmony_ci{
2118c2ecf20Sopenharmony_ci	struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
2128c2ecf20Sopenharmony_ci	unsigned long flags;
2138c2ecf20Sopenharmony_ci	u32 clk = BIT(gate->clock_idx);
2148c2ecf20Sopenharmony_ci	u32 rst = BIT(gate->reset_idx);
2158c2ecf20Sopenharmony_ci	u32 enval;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	spin_lock_irqsave(gate->lock, flags);
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	if (aspeed_clk_is_enabled(hw)) {
2208c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(gate->lock, flags);
2218c2ecf20Sopenharmony_ci		return 0;
2228c2ecf20Sopenharmony_ci	}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	if (gate->reset_idx >= 0) {
2258c2ecf20Sopenharmony_ci		/* Put IP in reset */
2268c2ecf20Sopenharmony_ci		regmap_update_bits(gate->map, ASPEED_RESET_CTRL, rst, rst);
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci		/* Delay 100us */
2298c2ecf20Sopenharmony_ci		udelay(100);
2308c2ecf20Sopenharmony_ci	}
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	/* Enable clock */
2338c2ecf20Sopenharmony_ci	enval = (gate->flags & CLK_GATE_SET_TO_DISABLE) ? 0 : clk;
2348c2ecf20Sopenharmony_ci	regmap_update_bits(gate->map, ASPEED_CLK_STOP_CTRL, clk, enval);
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	if (gate->reset_idx >= 0) {
2378c2ecf20Sopenharmony_ci		/* A delay of 10ms is specified by the ASPEED docs */
2388c2ecf20Sopenharmony_ci		mdelay(10);
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci		/* Take IP out of reset */
2418c2ecf20Sopenharmony_ci		regmap_update_bits(gate->map, ASPEED_RESET_CTRL, rst, 0);
2428c2ecf20Sopenharmony_ci	}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(gate->lock, flags);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	return 0;
2478c2ecf20Sopenharmony_ci}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_cistatic void aspeed_clk_disable(struct clk_hw *hw)
2508c2ecf20Sopenharmony_ci{
2518c2ecf20Sopenharmony_ci	struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
2528c2ecf20Sopenharmony_ci	unsigned long flags;
2538c2ecf20Sopenharmony_ci	u32 clk = BIT(gate->clock_idx);
2548c2ecf20Sopenharmony_ci	u32 enval;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	spin_lock_irqsave(gate->lock, flags);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	enval = (gate->flags & CLK_GATE_SET_TO_DISABLE) ? clk : 0;
2598c2ecf20Sopenharmony_ci	regmap_update_bits(gate->map, ASPEED_CLK_STOP_CTRL, clk, enval);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(gate->lock, flags);
2628c2ecf20Sopenharmony_ci}
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_cistatic const struct clk_ops aspeed_clk_gate_ops = {
2658c2ecf20Sopenharmony_ci	.enable = aspeed_clk_enable,
2668c2ecf20Sopenharmony_ci	.disable = aspeed_clk_disable,
2678c2ecf20Sopenharmony_ci	.is_enabled = aspeed_clk_is_enabled,
2688c2ecf20Sopenharmony_ci};
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_cistatic const u8 aspeed_resets[] = {
2718c2ecf20Sopenharmony_ci	/* SCU04 resets */
2728c2ecf20Sopenharmony_ci	[ASPEED_RESET_XDMA]	= 25,
2738c2ecf20Sopenharmony_ci	[ASPEED_RESET_MCTP]	= 24,
2748c2ecf20Sopenharmony_ci	[ASPEED_RESET_ADC]	= 23,
2758c2ecf20Sopenharmony_ci	[ASPEED_RESET_JTAG_MASTER] = 22,
2768c2ecf20Sopenharmony_ci	[ASPEED_RESET_MIC]	= 18,
2778c2ecf20Sopenharmony_ci	[ASPEED_RESET_PWM]	=  9,
2788c2ecf20Sopenharmony_ci	[ASPEED_RESET_PECI]	= 10,
2798c2ecf20Sopenharmony_ci	[ASPEED_RESET_I2C]	=  2,
2808c2ecf20Sopenharmony_ci	[ASPEED_RESET_AHB]	=  1,
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	/*
2838c2ecf20Sopenharmony_ci	 * SCUD4 resets start at an offset to separate them from
2848c2ecf20Sopenharmony_ci	 * the SCU04 resets.
2858c2ecf20Sopenharmony_ci	 */
2868c2ecf20Sopenharmony_ci	[ASPEED_RESET_CRT1]	= ASPEED_RESET2_OFFSET + 5,
2878c2ecf20Sopenharmony_ci};
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_cistatic int aspeed_reset_deassert(struct reset_controller_dev *rcdev,
2908c2ecf20Sopenharmony_ci				 unsigned long id)
2918c2ecf20Sopenharmony_ci{
2928c2ecf20Sopenharmony_ci	struct aspeed_reset *ar = to_aspeed_reset(rcdev);
2938c2ecf20Sopenharmony_ci	u32 reg = ASPEED_RESET_CTRL;
2948c2ecf20Sopenharmony_ci	u32 bit = aspeed_resets[id];
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	if (bit >= ASPEED_RESET2_OFFSET) {
2978c2ecf20Sopenharmony_ci		bit -= ASPEED_RESET2_OFFSET;
2988c2ecf20Sopenharmony_ci		reg = ASPEED_RESET_CTRL2;
2998c2ecf20Sopenharmony_ci	}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	return regmap_update_bits(ar->map, reg, BIT(bit), 0);
3028c2ecf20Sopenharmony_ci}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_cistatic int aspeed_reset_assert(struct reset_controller_dev *rcdev,
3058c2ecf20Sopenharmony_ci			       unsigned long id)
3068c2ecf20Sopenharmony_ci{
3078c2ecf20Sopenharmony_ci	struct aspeed_reset *ar = to_aspeed_reset(rcdev);
3088c2ecf20Sopenharmony_ci	u32 reg = ASPEED_RESET_CTRL;
3098c2ecf20Sopenharmony_ci	u32 bit = aspeed_resets[id];
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	if (bit >= ASPEED_RESET2_OFFSET) {
3128c2ecf20Sopenharmony_ci		bit -= ASPEED_RESET2_OFFSET;
3138c2ecf20Sopenharmony_ci		reg = ASPEED_RESET_CTRL2;
3148c2ecf20Sopenharmony_ci	}
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	return regmap_update_bits(ar->map, reg, BIT(bit), BIT(bit));
3178c2ecf20Sopenharmony_ci}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_cistatic int aspeed_reset_status(struct reset_controller_dev *rcdev,
3208c2ecf20Sopenharmony_ci			       unsigned long id)
3218c2ecf20Sopenharmony_ci{
3228c2ecf20Sopenharmony_ci	struct aspeed_reset *ar = to_aspeed_reset(rcdev);
3238c2ecf20Sopenharmony_ci	u32 reg = ASPEED_RESET_CTRL;
3248c2ecf20Sopenharmony_ci	u32 bit = aspeed_resets[id];
3258c2ecf20Sopenharmony_ci	int ret, val;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	if (bit >= ASPEED_RESET2_OFFSET) {
3288c2ecf20Sopenharmony_ci		bit -= ASPEED_RESET2_OFFSET;
3298c2ecf20Sopenharmony_ci		reg = ASPEED_RESET_CTRL2;
3308c2ecf20Sopenharmony_ci	}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	ret = regmap_read(ar->map, reg, &val);
3338c2ecf20Sopenharmony_ci	if (ret)
3348c2ecf20Sopenharmony_ci		return ret;
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	return !!(val & BIT(bit));
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_cistatic const struct reset_control_ops aspeed_reset_ops = {
3408c2ecf20Sopenharmony_ci	.assert = aspeed_reset_assert,
3418c2ecf20Sopenharmony_ci	.deassert = aspeed_reset_deassert,
3428c2ecf20Sopenharmony_ci	.status = aspeed_reset_status,
3438c2ecf20Sopenharmony_ci};
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_cistatic struct clk_hw *aspeed_clk_hw_register_gate(struct device *dev,
3468c2ecf20Sopenharmony_ci		const char *name, const char *parent_name, unsigned long flags,
3478c2ecf20Sopenharmony_ci		struct regmap *map, u8 clock_idx, u8 reset_idx,
3488c2ecf20Sopenharmony_ci		u8 clk_gate_flags, spinlock_t *lock)
3498c2ecf20Sopenharmony_ci{
3508c2ecf20Sopenharmony_ci	struct aspeed_clk_gate *gate;
3518c2ecf20Sopenharmony_ci	struct clk_init_data init;
3528c2ecf20Sopenharmony_ci	struct clk_hw *hw;
3538c2ecf20Sopenharmony_ci	int ret;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	gate = kzalloc(sizeof(*gate), GFP_KERNEL);
3568c2ecf20Sopenharmony_ci	if (!gate)
3578c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	init.name = name;
3608c2ecf20Sopenharmony_ci	init.ops = &aspeed_clk_gate_ops;
3618c2ecf20Sopenharmony_ci	init.flags = flags;
3628c2ecf20Sopenharmony_ci	init.parent_names = parent_name ? &parent_name : NULL;
3638c2ecf20Sopenharmony_ci	init.num_parents = parent_name ? 1 : 0;
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	gate->map = map;
3668c2ecf20Sopenharmony_ci	gate->clock_idx = clock_idx;
3678c2ecf20Sopenharmony_ci	gate->reset_idx = reset_idx;
3688c2ecf20Sopenharmony_ci	gate->flags = clk_gate_flags;
3698c2ecf20Sopenharmony_ci	gate->lock = lock;
3708c2ecf20Sopenharmony_ci	gate->hw.init = &init;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	hw = &gate->hw;
3738c2ecf20Sopenharmony_ci	ret = clk_hw_register(dev, hw);
3748c2ecf20Sopenharmony_ci	if (ret) {
3758c2ecf20Sopenharmony_ci		kfree(gate);
3768c2ecf20Sopenharmony_ci		hw = ERR_PTR(ret);
3778c2ecf20Sopenharmony_ci	}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	return hw;
3808c2ecf20Sopenharmony_ci}
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_cistatic int aspeed_clk_probe(struct platform_device *pdev)
3838c2ecf20Sopenharmony_ci{
3848c2ecf20Sopenharmony_ci	const struct aspeed_clk_soc_data *soc_data;
3858c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
3868c2ecf20Sopenharmony_ci	struct aspeed_reset *ar;
3878c2ecf20Sopenharmony_ci	struct regmap *map;
3888c2ecf20Sopenharmony_ci	struct clk_hw *hw;
3898c2ecf20Sopenharmony_ci	u32 val, rate;
3908c2ecf20Sopenharmony_ci	int i, ret;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	map = syscon_node_to_regmap(dev->of_node);
3938c2ecf20Sopenharmony_ci	if (IS_ERR(map)) {
3948c2ecf20Sopenharmony_ci		dev_err(dev, "no syscon regmap\n");
3958c2ecf20Sopenharmony_ci		return PTR_ERR(map);
3968c2ecf20Sopenharmony_ci	}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	ar = devm_kzalloc(dev, sizeof(*ar), GFP_KERNEL);
3998c2ecf20Sopenharmony_ci	if (!ar)
4008c2ecf20Sopenharmony_ci		return -ENOMEM;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	ar->map = map;
4038c2ecf20Sopenharmony_ci	ar->rcdev.owner = THIS_MODULE;
4048c2ecf20Sopenharmony_ci	ar->rcdev.nr_resets = ARRAY_SIZE(aspeed_resets);
4058c2ecf20Sopenharmony_ci	ar->rcdev.ops = &aspeed_reset_ops;
4068c2ecf20Sopenharmony_ci	ar->rcdev.of_node = dev->of_node;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	ret = devm_reset_controller_register(dev, &ar->rcdev);
4098c2ecf20Sopenharmony_ci	if (ret) {
4108c2ecf20Sopenharmony_ci		dev_err(dev, "could not register reset controller\n");
4118c2ecf20Sopenharmony_ci		return ret;
4128c2ecf20Sopenharmony_ci	}
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	/* SoC generations share common layouts but have different divisors */
4158c2ecf20Sopenharmony_ci	soc_data = of_device_get_match_data(dev);
4168c2ecf20Sopenharmony_ci	if (!soc_data) {
4178c2ecf20Sopenharmony_ci		dev_err(dev, "no match data for platform\n");
4188c2ecf20Sopenharmony_ci		return -EINVAL;
4198c2ecf20Sopenharmony_ci	}
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	/* UART clock div13 setting */
4228c2ecf20Sopenharmony_ci	regmap_read(map, ASPEED_MISC_CTRL, &val);
4238c2ecf20Sopenharmony_ci	if (val & UART_DIV13_EN)
4248c2ecf20Sopenharmony_ci		rate = 24000000 / 13;
4258c2ecf20Sopenharmony_ci	else
4268c2ecf20Sopenharmony_ci		rate = 24000000;
4278c2ecf20Sopenharmony_ci	/* TODO: Find the parent data for the uart clock */
4288c2ecf20Sopenharmony_ci	hw = clk_hw_register_fixed_rate(dev, "uart", NULL, 0, rate);
4298c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
4308c2ecf20Sopenharmony_ci		return PTR_ERR(hw);
4318c2ecf20Sopenharmony_ci	aspeed_clk_data->hws[ASPEED_CLK_UART] = hw;
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	/*
4348c2ecf20Sopenharmony_ci	 * Memory controller (M-PLL) PLL. This clock is configured by the
4358c2ecf20Sopenharmony_ci	 * bootloader, and is exposed to Linux as a read-only clock rate.
4368c2ecf20Sopenharmony_ci	 */
4378c2ecf20Sopenharmony_ci	regmap_read(map, ASPEED_MPLL_PARAM, &val);
4388c2ecf20Sopenharmony_ci	hw = soc_data->calc_pll("mpll", val);
4398c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
4408c2ecf20Sopenharmony_ci		return PTR_ERR(hw);
4418c2ecf20Sopenharmony_ci	aspeed_clk_data->hws[ASPEED_CLK_MPLL] =	hw;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	/* SD/SDIO clock divider and gate */
4448c2ecf20Sopenharmony_ci	hw = clk_hw_register_gate(dev, "sd_extclk_gate", "hpll", 0,
4458c2ecf20Sopenharmony_ci				  scu_base + ASPEED_CLK_SELECTION, 15, 0,
4468c2ecf20Sopenharmony_ci				  &aspeed_clk_lock);
4478c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
4488c2ecf20Sopenharmony_ci		return PTR_ERR(hw);
4498c2ecf20Sopenharmony_ci	hw = clk_hw_register_divider_table(dev, "sd_extclk", "sd_extclk_gate",
4508c2ecf20Sopenharmony_ci			0, scu_base + ASPEED_CLK_SELECTION, 12, 3, 0,
4518c2ecf20Sopenharmony_ci			soc_data->div_table,
4528c2ecf20Sopenharmony_ci			&aspeed_clk_lock);
4538c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
4548c2ecf20Sopenharmony_ci		return PTR_ERR(hw);
4558c2ecf20Sopenharmony_ci	aspeed_clk_data->hws[ASPEED_CLK_SDIO] = hw;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	/* MAC AHB bus clock divider */
4588c2ecf20Sopenharmony_ci	hw = clk_hw_register_divider_table(dev, "mac", "hpll", 0,
4598c2ecf20Sopenharmony_ci			scu_base + ASPEED_CLK_SELECTION, 16, 3, 0,
4608c2ecf20Sopenharmony_ci			soc_data->mac_div_table,
4618c2ecf20Sopenharmony_ci			&aspeed_clk_lock);
4628c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
4638c2ecf20Sopenharmony_ci		return PTR_ERR(hw);
4648c2ecf20Sopenharmony_ci	aspeed_clk_data->hws[ASPEED_CLK_MAC] = hw;
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	if (of_device_is_compatible(pdev->dev.of_node, "aspeed,ast2500-scu")) {
4678c2ecf20Sopenharmony_ci		/* RMII 50MHz RCLK */
4688c2ecf20Sopenharmony_ci		hw = clk_hw_register_fixed_rate(dev, "mac12rclk", "hpll", 0,
4698c2ecf20Sopenharmony_ci						50000000);
4708c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
4718c2ecf20Sopenharmony_ci			return PTR_ERR(hw);
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci		/* RMII1 50MHz (RCLK) output enable */
4748c2ecf20Sopenharmony_ci		hw = clk_hw_register_gate(dev, "mac1rclk", "mac12rclk", 0,
4758c2ecf20Sopenharmony_ci				scu_base + ASPEED_MAC_CLK_DLY, 29, 0,
4768c2ecf20Sopenharmony_ci				&aspeed_clk_lock);
4778c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
4788c2ecf20Sopenharmony_ci			return PTR_ERR(hw);
4798c2ecf20Sopenharmony_ci		aspeed_clk_data->hws[ASPEED_CLK_MAC1RCLK] = hw;
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci		/* RMII2 50MHz (RCLK) output enable */
4828c2ecf20Sopenharmony_ci		hw = clk_hw_register_gate(dev, "mac2rclk", "mac12rclk", 0,
4838c2ecf20Sopenharmony_ci				scu_base + ASPEED_MAC_CLK_DLY, 30, 0,
4848c2ecf20Sopenharmony_ci				&aspeed_clk_lock);
4858c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
4868c2ecf20Sopenharmony_ci			return PTR_ERR(hw);
4878c2ecf20Sopenharmony_ci		aspeed_clk_data->hws[ASPEED_CLK_MAC2RCLK] = hw;
4888c2ecf20Sopenharmony_ci	}
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	/* LPC Host (LHCLK) clock divider */
4918c2ecf20Sopenharmony_ci	hw = clk_hw_register_divider_table(dev, "lhclk", "hpll", 0,
4928c2ecf20Sopenharmony_ci			scu_base + ASPEED_CLK_SELECTION, 20, 3, 0,
4938c2ecf20Sopenharmony_ci			soc_data->div_table,
4948c2ecf20Sopenharmony_ci			&aspeed_clk_lock);
4958c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
4968c2ecf20Sopenharmony_ci		return PTR_ERR(hw);
4978c2ecf20Sopenharmony_ci	aspeed_clk_data->hws[ASPEED_CLK_LHCLK] = hw;
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	/* P-Bus (BCLK) clock divider */
5008c2ecf20Sopenharmony_ci	hw = clk_hw_register_divider_table(dev, "bclk", "hpll", 0,
5018c2ecf20Sopenharmony_ci			scu_base + ASPEED_CLK_SELECTION_2, 0, 2, 0,
5028c2ecf20Sopenharmony_ci			soc_data->div_table,
5038c2ecf20Sopenharmony_ci			&aspeed_clk_lock);
5048c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
5058c2ecf20Sopenharmony_ci		return PTR_ERR(hw);
5068c2ecf20Sopenharmony_ci	aspeed_clk_data->hws[ASPEED_CLK_BCLK] = hw;
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	/* Fixed 24MHz clock */
5098c2ecf20Sopenharmony_ci	hw = clk_hw_register_fixed_rate(NULL, "fixed-24m", "clkin",
5108c2ecf20Sopenharmony_ci					0, 24000000);
5118c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
5128c2ecf20Sopenharmony_ci		return PTR_ERR(hw);
5138c2ecf20Sopenharmony_ci	aspeed_clk_data->hws[ASPEED_CLK_24M] = hw;
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	hw = clk_hw_register_mux(dev, "eclk-mux", eclk_parent_names,
5168c2ecf20Sopenharmony_ci				 ARRAY_SIZE(eclk_parent_names), 0,
5178c2ecf20Sopenharmony_ci				 scu_base + ASPEED_CLK_SELECTION, 2, 0x3, 0,
5188c2ecf20Sopenharmony_ci				 &aspeed_clk_lock);
5198c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
5208c2ecf20Sopenharmony_ci		return PTR_ERR(hw);
5218c2ecf20Sopenharmony_ci	aspeed_clk_data->hws[ASPEED_CLK_ECLK_MUX] = hw;
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	hw = clk_hw_register_divider_table(dev, "eclk", "eclk-mux", 0,
5248c2ecf20Sopenharmony_ci					   scu_base + ASPEED_CLK_SELECTION, 28,
5258c2ecf20Sopenharmony_ci					   3, 0, soc_data->eclk_div_table,
5268c2ecf20Sopenharmony_ci					   &aspeed_clk_lock);
5278c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
5288c2ecf20Sopenharmony_ci		return PTR_ERR(hw);
5298c2ecf20Sopenharmony_ci	aspeed_clk_data->hws[ASPEED_CLK_ECLK] = hw;
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	/*
5328c2ecf20Sopenharmony_ci	 * TODO: There are a number of clocks that not included in this driver
5338c2ecf20Sopenharmony_ci	 * as more information is required:
5348c2ecf20Sopenharmony_ci	 *   D2-PLL
5358c2ecf20Sopenharmony_ci	 *   D-PLL
5368c2ecf20Sopenharmony_ci	 *   YCLK
5378c2ecf20Sopenharmony_ci	 *   RGMII
5388c2ecf20Sopenharmony_ci	 *   RMII
5398c2ecf20Sopenharmony_ci	 *   UART[1..5] clock source mux
5408c2ecf20Sopenharmony_ci	 */
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(aspeed_gates); i++) {
5438c2ecf20Sopenharmony_ci		const struct aspeed_gate_data *gd = &aspeed_gates[i];
5448c2ecf20Sopenharmony_ci		u32 gate_flags;
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci		/* Special case: the USB port 1 clock (bit 14) is always
5478c2ecf20Sopenharmony_ci		 * working the opposite way from the other ones.
5488c2ecf20Sopenharmony_ci		 */
5498c2ecf20Sopenharmony_ci		gate_flags = (gd->clock_idx == 14) ? 0 : CLK_GATE_SET_TO_DISABLE;
5508c2ecf20Sopenharmony_ci		hw = aspeed_clk_hw_register_gate(dev,
5518c2ecf20Sopenharmony_ci				gd->name,
5528c2ecf20Sopenharmony_ci				gd->parent_name,
5538c2ecf20Sopenharmony_ci				gd->flags,
5548c2ecf20Sopenharmony_ci				map,
5558c2ecf20Sopenharmony_ci				gd->clock_idx,
5568c2ecf20Sopenharmony_ci				gd->reset_idx,
5578c2ecf20Sopenharmony_ci				gate_flags,
5588c2ecf20Sopenharmony_ci				&aspeed_clk_lock);
5598c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
5608c2ecf20Sopenharmony_ci			return PTR_ERR(hw);
5618c2ecf20Sopenharmony_ci		aspeed_clk_data->hws[i] = hw;
5628c2ecf20Sopenharmony_ci	}
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	return 0;
5658c2ecf20Sopenharmony_ci};
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_cistatic const struct of_device_id aspeed_clk_dt_ids[] = {
5688c2ecf20Sopenharmony_ci	{ .compatible = "aspeed,ast2400-scu", .data = &ast2400_data },
5698c2ecf20Sopenharmony_ci	{ .compatible = "aspeed,ast2500-scu", .data = &ast2500_data },
5708c2ecf20Sopenharmony_ci	{ }
5718c2ecf20Sopenharmony_ci};
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_cistatic struct platform_driver aspeed_clk_driver = {
5748c2ecf20Sopenharmony_ci	.probe  = aspeed_clk_probe,
5758c2ecf20Sopenharmony_ci	.driver = {
5768c2ecf20Sopenharmony_ci		.name = "aspeed-clk",
5778c2ecf20Sopenharmony_ci		.of_match_table = aspeed_clk_dt_ids,
5788c2ecf20Sopenharmony_ci		.suppress_bind_attrs = true,
5798c2ecf20Sopenharmony_ci	},
5808c2ecf20Sopenharmony_ci};
5818c2ecf20Sopenharmony_cibuiltin_platform_driver(aspeed_clk_driver);
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_cistatic void __init aspeed_ast2400_cc(struct regmap *map)
5848c2ecf20Sopenharmony_ci{
5858c2ecf20Sopenharmony_ci	struct clk_hw *hw;
5868c2ecf20Sopenharmony_ci	u32 val, div, clkin, hpll;
5878c2ecf20Sopenharmony_ci	const u16 hpll_rates[][4] = {
5888c2ecf20Sopenharmony_ci		{384, 360, 336, 408},
5898c2ecf20Sopenharmony_ci		{400, 375, 350, 425},
5908c2ecf20Sopenharmony_ci	};
5918c2ecf20Sopenharmony_ci	int rate;
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	/*
5948c2ecf20Sopenharmony_ci	 * CLKIN is the crystal oscillator, 24, 48 or 25MHz selected by
5958c2ecf20Sopenharmony_ci	 * strapping
5968c2ecf20Sopenharmony_ci	 */
5978c2ecf20Sopenharmony_ci	regmap_read(map, ASPEED_STRAP, &val);
5988c2ecf20Sopenharmony_ci	rate = (val >> 8) & 3;
5998c2ecf20Sopenharmony_ci	if (val & CLKIN_25MHZ_EN) {
6008c2ecf20Sopenharmony_ci		clkin = 25000000;
6018c2ecf20Sopenharmony_ci		hpll = hpll_rates[1][rate];
6028c2ecf20Sopenharmony_ci	} else if (val & AST2400_CLK_SOURCE_SEL) {
6038c2ecf20Sopenharmony_ci		clkin = 48000000;
6048c2ecf20Sopenharmony_ci		hpll = hpll_rates[0][rate];
6058c2ecf20Sopenharmony_ci	} else {
6068c2ecf20Sopenharmony_ci		clkin = 24000000;
6078c2ecf20Sopenharmony_ci		hpll = hpll_rates[0][rate];
6088c2ecf20Sopenharmony_ci	}
6098c2ecf20Sopenharmony_ci	hw = clk_hw_register_fixed_rate(NULL, "clkin", NULL, 0, clkin);
6108c2ecf20Sopenharmony_ci	pr_debug("clkin @%u MHz\n", clkin / 1000000);
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	/*
6138c2ecf20Sopenharmony_ci	 * High-speed PLL clock derived from the crystal. This the CPU clock,
6148c2ecf20Sopenharmony_ci	 * and we assume that it is enabled. It can be configured through the
6158c2ecf20Sopenharmony_ci	 * HPLL_PARAM register, or set to a specified frequency by strapping.
6168c2ecf20Sopenharmony_ci	 */
6178c2ecf20Sopenharmony_ci	regmap_read(map, ASPEED_HPLL_PARAM, &val);
6188c2ecf20Sopenharmony_ci	if (val & AST2400_HPLL_PROGRAMMED)
6198c2ecf20Sopenharmony_ci		hw = aspeed_ast2400_calc_pll("hpll", val);
6208c2ecf20Sopenharmony_ci	else
6218c2ecf20Sopenharmony_ci		hw = clk_hw_register_fixed_rate(NULL, "hpll", "clkin", 0,
6228c2ecf20Sopenharmony_ci				hpll * 1000000);
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	aspeed_clk_data->hws[ASPEED_CLK_HPLL] = hw;
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	/*
6278c2ecf20Sopenharmony_ci	 * Strap bits 11:10 define the CPU/AHB clock frequency ratio (aka HCLK)
6288c2ecf20Sopenharmony_ci	 *   00: Select CPU:AHB = 1:1
6298c2ecf20Sopenharmony_ci	 *   01: Select CPU:AHB = 2:1
6308c2ecf20Sopenharmony_ci	 *   10: Select CPU:AHB = 4:1
6318c2ecf20Sopenharmony_ci	 *   11: Select CPU:AHB = 3:1
6328c2ecf20Sopenharmony_ci	 */
6338c2ecf20Sopenharmony_ci	regmap_read(map, ASPEED_STRAP, &val);
6348c2ecf20Sopenharmony_ci	val = (val >> 10) & 0x3;
6358c2ecf20Sopenharmony_ci	div = val + 1;
6368c2ecf20Sopenharmony_ci	if (div == 3)
6378c2ecf20Sopenharmony_ci		div = 4;
6388c2ecf20Sopenharmony_ci	else if (div == 4)
6398c2ecf20Sopenharmony_ci		div = 3;
6408c2ecf20Sopenharmony_ci	hw = clk_hw_register_fixed_factor(NULL, "ahb", "hpll", 0, 1, div);
6418c2ecf20Sopenharmony_ci	aspeed_clk_data->hws[ASPEED_CLK_AHB] = hw;
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	/* APB clock clock selection register SCU08 (aka PCLK) */
6448c2ecf20Sopenharmony_ci	hw = clk_hw_register_divider_table(NULL, "apb", "hpll", 0,
6458c2ecf20Sopenharmony_ci			scu_base + ASPEED_CLK_SELECTION, 23, 3, 0,
6468c2ecf20Sopenharmony_ci			ast2400_div_table,
6478c2ecf20Sopenharmony_ci			&aspeed_clk_lock);
6488c2ecf20Sopenharmony_ci	aspeed_clk_data->hws[ASPEED_CLK_APB] = hw;
6498c2ecf20Sopenharmony_ci}
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_cistatic void __init aspeed_ast2500_cc(struct regmap *map)
6528c2ecf20Sopenharmony_ci{
6538c2ecf20Sopenharmony_ci	struct clk_hw *hw;
6548c2ecf20Sopenharmony_ci	u32 val, freq, div;
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	/* CLKIN is the crystal oscillator, 24 or 25MHz selected by strapping */
6578c2ecf20Sopenharmony_ci	regmap_read(map, ASPEED_STRAP, &val);
6588c2ecf20Sopenharmony_ci	if (val & CLKIN_25MHZ_EN)
6598c2ecf20Sopenharmony_ci		freq = 25000000;
6608c2ecf20Sopenharmony_ci	else
6618c2ecf20Sopenharmony_ci		freq = 24000000;
6628c2ecf20Sopenharmony_ci	hw = clk_hw_register_fixed_rate(NULL, "clkin", NULL, 0, freq);
6638c2ecf20Sopenharmony_ci	pr_debug("clkin @%u MHz\n", freq / 1000000);
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	/*
6668c2ecf20Sopenharmony_ci	 * High-speed PLL clock derived from the crystal. This the CPU clock,
6678c2ecf20Sopenharmony_ci	 * and we assume that it is enabled
6688c2ecf20Sopenharmony_ci	 */
6698c2ecf20Sopenharmony_ci	regmap_read(map, ASPEED_HPLL_PARAM, &val);
6708c2ecf20Sopenharmony_ci	aspeed_clk_data->hws[ASPEED_CLK_HPLL] = aspeed_ast2500_calc_pll("hpll", val);
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci	/* Strap bits 11:9 define the AXI/AHB clock frequency ratio (aka HCLK)*/
6738c2ecf20Sopenharmony_ci	regmap_read(map, ASPEED_STRAP, &val);
6748c2ecf20Sopenharmony_ci	val = (val >> 9) & 0x7;
6758c2ecf20Sopenharmony_ci	WARN(val == 0, "strapping is zero: cannot determine ahb clock");
6768c2ecf20Sopenharmony_ci	div = 2 * (val + 1);
6778c2ecf20Sopenharmony_ci	hw = clk_hw_register_fixed_factor(NULL, "ahb", "hpll", 0, 1, div);
6788c2ecf20Sopenharmony_ci	aspeed_clk_data->hws[ASPEED_CLK_AHB] = hw;
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci	/* APB clock clock selection register SCU08 (aka PCLK) */
6818c2ecf20Sopenharmony_ci	regmap_read(map, ASPEED_CLK_SELECTION, &val);
6828c2ecf20Sopenharmony_ci	val = (val >> 23) & 0x7;
6838c2ecf20Sopenharmony_ci	div = 4 * (val + 1);
6848c2ecf20Sopenharmony_ci	hw = clk_hw_register_fixed_factor(NULL, "apb", "hpll", 0, 1, div);
6858c2ecf20Sopenharmony_ci	aspeed_clk_data->hws[ASPEED_CLK_APB] = hw;
6868c2ecf20Sopenharmony_ci};
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_cistatic void __init aspeed_cc_init(struct device_node *np)
6898c2ecf20Sopenharmony_ci{
6908c2ecf20Sopenharmony_ci	struct regmap *map;
6918c2ecf20Sopenharmony_ci	u32 val;
6928c2ecf20Sopenharmony_ci	int ret;
6938c2ecf20Sopenharmony_ci	int i;
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci	scu_base = of_iomap(np, 0);
6968c2ecf20Sopenharmony_ci	if (!scu_base)
6978c2ecf20Sopenharmony_ci		return;
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	aspeed_clk_data = kzalloc(struct_size(aspeed_clk_data, hws,
7008c2ecf20Sopenharmony_ci					      ASPEED_NUM_CLKS),
7018c2ecf20Sopenharmony_ci				  GFP_KERNEL);
7028c2ecf20Sopenharmony_ci	if (!aspeed_clk_data)
7038c2ecf20Sopenharmony_ci		return;
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	/*
7068c2ecf20Sopenharmony_ci	 * This way all clocks fetched before the platform device probes,
7078c2ecf20Sopenharmony_ci	 * except those we assign here for early use, will be deferred.
7088c2ecf20Sopenharmony_ci	 */
7098c2ecf20Sopenharmony_ci	for (i = 0; i < ASPEED_NUM_CLKS; i++)
7108c2ecf20Sopenharmony_ci		aspeed_clk_data->hws[i] = ERR_PTR(-EPROBE_DEFER);
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	map = syscon_node_to_regmap(np);
7138c2ecf20Sopenharmony_ci	if (IS_ERR(map)) {
7148c2ecf20Sopenharmony_ci		pr_err("no syscon regmap\n");
7158c2ecf20Sopenharmony_ci		return;
7168c2ecf20Sopenharmony_ci	}
7178c2ecf20Sopenharmony_ci	/*
7188c2ecf20Sopenharmony_ci	 * We check that the regmap works on this very first access,
7198c2ecf20Sopenharmony_ci	 * but as this is an MMIO-backed regmap, subsequent regmap
7208c2ecf20Sopenharmony_ci	 * access is not going to fail and we skip error checks from
7218c2ecf20Sopenharmony_ci	 * this point.
7228c2ecf20Sopenharmony_ci	 */
7238c2ecf20Sopenharmony_ci	ret = regmap_read(map, ASPEED_STRAP, &val);
7248c2ecf20Sopenharmony_ci	if (ret) {
7258c2ecf20Sopenharmony_ci		pr_err("failed to read strapping register\n");
7268c2ecf20Sopenharmony_ci		return;
7278c2ecf20Sopenharmony_ci	}
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	if (of_device_is_compatible(np, "aspeed,ast2400-scu"))
7308c2ecf20Sopenharmony_ci		aspeed_ast2400_cc(map);
7318c2ecf20Sopenharmony_ci	else if (of_device_is_compatible(np, "aspeed,ast2500-scu"))
7328c2ecf20Sopenharmony_ci		aspeed_ast2500_cc(map);
7338c2ecf20Sopenharmony_ci	else
7348c2ecf20Sopenharmony_ci		pr_err("unknown platform, failed to add clocks\n");
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci	aspeed_clk_data->num = ASPEED_NUM_CLKS;
7378c2ecf20Sopenharmony_ci	ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, aspeed_clk_data);
7388c2ecf20Sopenharmony_ci	if (ret)
7398c2ecf20Sopenharmony_ci		pr_err("failed to add DT provider: %d\n", ret);
7408c2ecf20Sopenharmony_ci};
7418c2ecf20Sopenharmony_ciCLK_OF_DECLARE_DRIVER(aspeed_cc_g5, "aspeed,ast2500-scu", aspeed_cc_init);
7428c2ecf20Sopenharmony_ciCLK_OF_DECLARE_DRIVER(aspeed_cc_g4, "aspeed,ast2400-scu", aspeed_cc_init);
743