18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2017 Icenowy Zheng <icenowy@aosc.xyz>
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
78c2ecf20Sopenharmony_ci#include <linux/of_address.h>
88c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include "ccu_common.h"
118c2ecf20Sopenharmony_ci#include "ccu_reset.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include "ccu_div.h"
148c2ecf20Sopenharmony_ci#include "ccu_gate.h"
158c2ecf20Sopenharmony_ci#include "ccu_mp.h"
168c2ecf20Sopenharmony_ci#include "ccu_nm.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include "ccu-sun50i-h6-r.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/*
218c2ecf20Sopenharmony_ci * Information about AR100 and AHB/APB clocks in R_CCU are gathered from
228c2ecf20Sopenharmony_ci * clock definitions in the BSP source code.
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic const char * const ar100_r_apb2_parents[] = { "osc24M", "osc32k",
268c2ecf20Sopenharmony_ci						     "iosc", "pll-periph0" };
278c2ecf20Sopenharmony_cistatic const struct ccu_mux_var_prediv ar100_r_apb2_predivs[] = {
288c2ecf20Sopenharmony_ci	{ .index = 3, .shift = 0, .width = 5 },
298c2ecf20Sopenharmony_ci};
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic struct ccu_div ar100_clk = {
328c2ecf20Sopenharmony_ci	.div		= _SUNXI_CCU_DIV_FLAGS(8, 2, CLK_DIVIDER_POWER_OF_TWO),
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	.mux		= {
358c2ecf20Sopenharmony_ci		.shift	= 24,
368c2ecf20Sopenharmony_ci		.width	= 2,
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci		.var_predivs	= ar100_r_apb2_predivs,
398c2ecf20Sopenharmony_ci		.n_var_predivs	= ARRAY_SIZE(ar100_r_apb2_predivs),
408c2ecf20Sopenharmony_ci	},
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	.common		= {
438c2ecf20Sopenharmony_ci		.reg		= 0x000,
448c2ecf20Sopenharmony_ci		.features	= CCU_FEATURE_VARIABLE_PREDIV,
458c2ecf20Sopenharmony_ci		.hw.init	= CLK_HW_INIT_PARENTS("ar100",
468c2ecf20Sopenharmony_ci						      ar100_r_apb2_parents,
478c2ecf20Sopenharmony_ci						      &ccu_div_ops,
488c2ecf20Sopenharmony_ci						      0),
498c2ecf20Sopenharmony_ci	},
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic CLK_FIXED_FACTOR_HW(r_ahb_clk, "r-ahb", &ar100_clk.common.hw, 1, 1, 0);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic SUNXI_CCU_M(r_apb1_clk, "r-apb1", "r-ahb", 0x00c, 0, 2, 0);
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic struct ccu_div r_apb2_clk = {
578c2ecf20Sopenharmony_ci	.div		= _SUNXI_CCU_DIV_FLAGS(8, 2, CLK_DIVIDER_POWER_OF_TWO),
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	.mux		= {
608c2ecf20Sopenharmony_ci		.shift	= 24,
618c2ecf20Sopenharmony_ci		.width	= 2,
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci		.var_predivs	= ar100_r_apb2_predivs,
648c2ecf20Sopenharmony_ci		.n_var_predivs	= ARRAY_SIZE(ar100_r_apb2_predivs),
658c2ecf20Sopenharmony_ci	},
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	.common		= {
688c2ecf20Sopenharmony_ci		.reg		= 0x010,
698c2ecf20Sopenharmony_ci		.features	= CCU_FEATURE_VARIABLE_PREDIV,
708c2ecf20Sopenharmony_ci		.hw.init	= CLK_HW_INIT_PARENTS("r-apb2",
718c2ecf20Sopenharmony_ci						      ar100_r_apb2_parents,
728c2ecf20Sopenharmony_ci						      &ccu_div_ops,
738c2ecf20Sopenharmony_ci						      0),
748c2ecf20Sopenharmony_ci	},
758c2ecf20Sopenharmony_ci};
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci/*
788c2ecf20Sopenharmony_ci * Information about the gate/resets are gathered from the clock header file
798c2ecf20Sopenharmony_ci * in the BSP source code, although most of them are unused. The existence
808c2ecf20Sopenharmony_ci * of the hardware block is verified with "3.1 Memory Mapping" chapter in
818c2ecf20Sopenharmony_ci * "Allwinner H6 V200 User Manual V1.1"; and the parent APB buses are verified
828c2ecf20Sopenharmony_ci * with "3.3.2.1 System Bus Tree" chapter inthe same document.
838c2ecf20Sopenharmony_ci */
848c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE(r_apb1_timer_clk,	"r-apb1-timer",	"r-apb1",
858c2ecf20Sopenharmony_ci		      0x11c, BIT(0), 0);
868c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE(r_apb1_twd_clk,	"r-apb1-twd",	"r-apb1",
878c2ecf20Sopenharmony_ci		      0x12c, BIT(0), 0);
888c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE(r_apb1_pwm_clk,	"r-apb1-pwm",	"r-apb1",
898c2ecf20Sopenharmony_ci		      0x13c, BIT(0), 0);
908c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE(r_apb2_uart_clk,	"r-apb2-uart",	"r-apb2",
918c2ecf20Sopenharmony_ci		      0x18c, BIT(0), 0);
928c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE(r_apb2_i2c_clk,	"r-apb2-i2c",	"r-apb2",
938c2ecf20Sopenharmony_ci		      0x19c, BIT(0), 0);
948c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE(r_apb1_ir_clk,	"r-apb1-ir",	"r-apb1",
958c2ecf20Sopenharmony_ci		      0x1cc, BIT(0), 0);
968c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE(r_apb1_w1_clk,	"r-apb1-w1",	"r-apb1",
978c2ecf20Sopenharmony_ci		      0x1ec, BIT(0), 0);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci/* Information of IR(RX) mod clock is gathered from BSP source code */
1008c2ecf20Sopenharmony_cistatic const char * const r_mod0_default_parents[] = { "osc32k", "osc24M" };
1018c2ecf20Sopenharmony_cistatic SUNXI_CCU_MP_WITH_MUX_GATE(ir_clk, "ir",
1028c2ecf20Sopenharmony_ci				  r_mod0_default_parents, 0x1c0,
1038c2ecf20Sopenharmony_ci				  0, 5,		/* M */
1048c2ecf20Sopenharmony_ci				  8, 2,		/* P */
1058c2ecf20Sopenharmony_ci				  24, 1,	/* mux */
1068c2ecf20Sopenharmony_ci				  BIT(31),	/* gate */
1078c2ecf20Sopenharmony_ci				  0);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci/*
1108c2ecf20Sopenharmony_ci * BSP didn't use the 1-wire function at all now, and the information about
1118c2ecf20Sopenharmony_ci * this mod clock is guessed from the IR mod clock above. The existence of
1128c2ecf20Sopenharmony_ci * this mod clock is proven by BSP clock header, and the dividers are verified
1138c2ecf20Sopenharmony_ci * by contents in the 1-wire related chapter of the User Manual.
1148c2ecf20Sopenharmony_ci */
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic SUNXI_CCU_MP_WITH_MUX_GATE(w1_clk, "w1",
1178c2ecf20Sopenharmony_ci				  r_mod0_default_parents, 0x1e0,
1188c2ecf20Sopenharmony_ci				  0, 5,		/* M */
1198c2ecf20Sopenharmony_ci				  8, 2,		/* P */
1208c2ecf20Sopenharmony_ci				  24, 1,	/* mux */
1218c2ecf20Sopenharmony_ci				  BIT(31),	/* gate */
1228c2ecf20Sopenharmony_ci				  0);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic struct ccu_common *sun50i_h6_r_ccu_clks[] = {
1258c2ecf20Sopenharmony_ci	&ar100_clk.common,
1268c2ecf20Sopenharmony_ci	&r_apb1_clk.common,
1278c2ecf20Sopenharmony_ci	&r_apb2_clk.common,
1288c2ecf20Sopenharmony_ci	&r_apb1_timer_clk.common,
1298c2ecf20Sopenharmony_ci	&r_apb1_twd_clk.common,
1308c2ecf20Sopenharmony_ci	&r_apb1_pwm_clk.common,
1318c2ecf20Sopenharmony_ci	&r_apb2_uart_clk.common,
1328c2ecf20Sopenharmony_ci	&r_apb2_i2c_clk.common,
1338c2ecf20Sopenharmony_ci	&r_apb1_ir_clk.common,
1348c2ecf20Sopenharmony_ci	&r_apb1_w1_clk.common,
1358c2ecf20Sopenharmony_ci	&ir_clk.common,
1368c2ecf20Sopenharmony_ci	&w1_clk.common,
1378c2ecf20Sopenharmony_ci};
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic struct clk_hw_onecell_data sun50i_h6_r_hw_clks = {
1408c2ecf20Sopenharmony_ci	.hws	= {
1418c2ecf20Sopenharmony_ci		[CLK_AR100]		= &ar100_clk.common.hw,
1428c2ecf20Sopenharmony_ci		[CLK_R_AHB]		= &r_ahb_clk.hw,
1438c2ecf20Sopenharmony_ci		[CLK_R_APB1]		= &r_apb1_clk.common.hw,
1448c2ecf20Sopenharmony_ci		[CLK_R_APB2]		= &r_apb2_clk.common.hw,
1458c2ecf20Sopenharmony_ci		[CLK_R_APB1_TIMER]	= &r_apb1_timer_clk.common.hw,
1468c2ecf20Sopenharmony_ci		[CLK_R_APB1_TWD]	= &r_apb1_twd_clk.common.hw,
1478c2ecf20Sopenharmony_ci		[CLK_R_APB1_PWM]	= &r_apb1_pwm_clk.common.hw,
1488c2ecf20Sopenharmony_ci		[CLK_R_APB2_UART]	= &r_apb2_uart_clk.common.hw,
1498c2ecf20Sopenharmony_ci		[CLK_R_APB2_I2C]	= &r_apb2_i2c_clk.common.hw,
1508c2ecf20Sopenharmony_ci		[CLK_R_APB1_IR]		= &r_apb1_ir_clk.common.hw,
1518c2ecf20Sopenharmony_ci		[CLK_R_APB1_W1]		= &r_apb1_w1_clk.common.hw,
1528c2ecf20Sopenharmony_ci		[CLK_IR]		= &ir_clk.common.hw,
1538c2ecf20Sopenharmony_ci		[CLK_W1]		= &w1_clk.common.hw,
1548c2ecf20Sopenharmony_ci	},
1558c2ecf20Sopenharmony_ci	.num	= CLK_NUMBER,
1568c2ecf20Sopenharmony_ci};
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic struct ccu_reset_map sun50i_h6_r_ccu_resets[] = {
1598c2ecf20Sopenharmony_ci	[RST_R_APB1_TIMER]	=  { 0x11c, BIT(16) },
1608c2ecf20Sopenharmony_ci	[RST_R_APB1_TWD]	=  { 0x12c, BIT(16) },
1618c2ecf20Sopenharmony_ci	[RST_R_APB1_PWM]	=  { 0x13c, BIT(16) },
1628c2ecf20Sopenharmony_ci	[RST_R_APB2_UART]	=  { 0x18c, BIT(16) },
1638c2ecf20Sopenharmony_ci	[RST_R_APB2_I2C]	=  { 0x19c, BIT(16) },
1648c2ecf20Sopenharmony_ci	[RST_R_APB1_IR]		=  { 0x1cc, BIT(16) },
1658c2ecf20Sopenharmony_ci	[RST_R_APB1_W1]		=  { 0x1ec, BIT(16) },
1668c2ecf20Sopenharmony_ci};
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistatic const struct sunxi_ccu_desc sun50i_h6_r_ccu_desc = {
1698c2ecf20Sopenharmony_ci	.ccu_clks	= sun50i_h6_r_ccu_clks,
1708c2ecf20Sopenharmony_ci	.num_ccu_clks	= ARRAY_SIZE(sun50i_h6_r_ccu_clks),
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	.hw_clks	= &sun50i_h6_r_hw_clks,
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	.resets		= sun50i_h6_r_ccu_resets,
1758c2ecf20Sopenharmony_ci	.num_resets	= ARRAY_SIZE(sun50i_h6_r_ccu_resets),
1768c2ecf20Sopenharmony_ci};
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_cistatic void __init sunxi_r_ccu_init(struct device_node *node,
1798c2ecf20Sopenharmony_ci				    const struct sunxi_ccu_desc *desc)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	void __iomem *reg;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	reg = of_io_request_and_map(node, 0, of_node_full_name(node));
1848c2ecf20Sopenharmony_ci	if (IS_ERR(reg)) {
1858c2ecf20Sopenharmony_ci		pr_err("%pOF: Could not map the clock registers\n", node);
1868c2ecf20Sopenharmony_ci		return;
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	sunxi_ccu_probe(node, reg, desc);
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic void __init sun50i_h6_r_ccu_setup(struct device_node *node)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	sunxi_r_ccu_init(node, &sun50i_h6_r_ccu_desc);
1958c2ecf20Sopenharmony_ci}
1968c2ecf20Sopenharmony_ciCLK_OF_DECLARE(sun50i_h6_r_ccu, "allwinner,sun50i-h6-r-ccu",
1978c2ecf20Sopenharmony_ci	       sun50i_h6_r_ccu_setup);
198