18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2020 Yangtao Li <frank@allwinnertech.com>
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
78c2ecf20Sopenharmony_ci#include <linux/module.h>
88c2ecf20Sopenharmony_ci#include <linux/of_address.h>
98c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include "ccu_common.h"
128c2ecf20Sopenharmony_ci#include "ccu_reset.h"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include "ccu_div.h"
158c2ecf20Sopenharmony_ci#include "ccu_gate.h"
168c2ecf20Sopenharmony_ci#include "ccu_mp.h"
178c2ecf20Sopenharmony_ci#include "ccu_nm.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include "ccu-sun50i-a100-r.h"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistatic const char * const cpus_r_apb2_parents[] = { "dcxo24M", "osc32k",
228c2ecf20Sopenharmony_ci						     "iosc", "pll-periph0" };
238c2ecf20Sopenharmony_cistatic const struct ccu_mux_var_prediv cpus_r_apb2_predivs[] = {
248c2ecf20Sopenharmony_ci	{ .index = 3, .shift = 0, .width = 5 },
258c2ecf20Sopenharmony_ci};
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic struct ccu_div r_cpus_clk = {
288c2ecf20Sopenharmony_ci	.div		= _SUNXI_CCU_DIV_FLAGS(8, 2, CLK_DIVIDER_POWER_OF_TWO),
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	.mux		= {
318c2ecf20Sopenharmony_ci		.shift	= 24,
328c2ecf20Sopenharmony_ci		.width	= 2,
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci		.var_predivs	= cpus_r_apb2_predivs,
358c2ecf20Sopenharmony_ci		.n_var_predivs	= ARRAY_SIZE(cpus_r_apb2_predivs),
368c2ecf20Sopenharmony_ci	},
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	.common		= {
398c2ecf20Sopenharmony_ci		.reg		= 0x000,
408c2ecf20Sopenharmony_ci		.features	= CCU_FEATURE_VARIABLE_PREDIV,
418c2ecf20Sopenharmony_ci		.hw.init	= CLK_HW_INIT_PARENTS("cpus",
428c2ecf20Sopenharmony_ci						      cpus_r_apb2_parents,
438c2ecf20Sopenharmony_ci						      &ccu_div_ops,
448c2ecf20Sopenharmony_ci						      0),
458c2ecf20Sopenharmony_ci	},
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic CLK_FIXED_FACTOR_HW(r_ahb_clk, "r-ahb", &r_cpus_clk.common.hw, 1, 1, 0);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic struct ccu_div r_apb1_clk = {
518c2ecf20Sopenharmony_ci	.div		= _SUNXI_CCU_DIV(0, 2),
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	.common		= {
548c2ecf20Sopenharmony_ci		.reg		= 0x00c,
558c2ecf20Sopenharmony_ci		.hw.init	= CLK_HW_INIT("r-apb1",
568c2ecf20Sopenharmony_ci					      "r-ahb",
578c2ecf20Sopenharmony_ci					      &ccu_div_ops,
588c2ecf20Sopenharmony_ci					      0),
598c2ecf20Sopenharmony_ci	},
608c2ecf20Sopenharmony_ci};
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic struct ccu_div r_apb2_clk = {
638c2ecf20Sopenharmony_ci	.div		= _SUNXI_CCU_DIV_FLAGS(8, 2, CLK_DIVIDER_POWER_OF_TWO),
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	.mux		= {
668c2ecf20Sopenharmony_ci		.shift	= 24,
678c2ecf20Sopenharmony_ci		.width	= 2,
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci		.var_predivs	= cpus_r_apb2_predivs,
708c2ecf20Sopenharmony_ci		.n_var_predivs	= ARRAY_SIZE(cpus_r_apb2_predivs),
718c2ecf20Sopenharmony_ci	},
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	.common		= {
748c2ecf20Sopenharmony_ci		.reg		= 0x010,
758c2ecf20Sopenharmony_ci		.features	= CCU_FEATURE_VARIABLE_PREDIV,
768c2ecf20Sopenharmony_ci		.hw.init	= CLK_HW_INIT_PARENTS("r-apb2",
778c2ecf20Sopenharmony_ci						      cpus_r_apb2_parents,
788c2ecf20Sopenharmony_ci						      &ccu_div_ops,
798c2ecf20Sopenharmony_ci						      0),
808c2ecf20Sopenharmony_ci	},
818c2ecf20Sopenharmony_ci};
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic const struct clk_parent_data clk_parent_r_apb1[] = {
848c2ecf20Sopenharmony_ci	{ .hw = &r_apb1_clk.common.hw },
858c2ecf20Sopenharmony_ci};
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic const struct clk_parent_data clk_parent_r_apb2[] = {
888c2ecf20Sopenharmony_ci	{ .hw = &r_apb2_clk.common.hw },
898c2ecf20Sopenharmony_ci};
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE_DATA(r_apb1_timer_clk, "r-apb1-timer", clk_parent_r_apb1,
928c2ecf20Sopenharmony_ci			   0x11c, BIT(0), 0);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE_DATA(r_apb1_twd_clk, "r-apb1-twd", clk_parent_r_apb1,
958c2ecf20Sopenharmony_ci			   0x12c, BIT(0), 0);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic const char * const r_apb1_pwm_clk_parents[] = { "dcxo24M", "osc32k",
988c2ecf20Sopenharmony_ci						       "iosc" };
998c2ecf20Sopenharmony_cistatic SUNXI_CCU_MUX(r_apb1_pwm_clk, "r-apb1-pwm", r_apb1_pwm_clk_parents,
1008c2ecf20Sopenharmony_ci		     0x130, 24, 2, 0);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE_DATA(r_apb1_bus_pwm_clk, "r-apb1-bus-pwm",
1038c2ecf20Sopenharmony_ci			   clk_parent_r_apb1, 0x13c, BIT(0), 0);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE_DATA(r_apb1_ppu_clk, "r-apb1-ppu", clk_parent_r_apb1,
1068c2ecf20Sopenharmony_ci			   0x17c, BIT(0), 0);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE_DATA(r_apb2_uart_clk, "r-apb2-uart", clk_parent_r_apb2,
1098c2ecf20Sopenharmony_ci			   0x18c, BIT(0), 0);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE_DATA(r_apb2_i2c0_clk, "r-apb2-i2c0", clk_parent_r_apb2,
1128c2ecf20Sopenharmony_ci			   0x19c, BIT(0), 0);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE_DATA(r_apb2_i2c1_clk, "r-apb2-i2c1", clk_parent_r_apb2,
1158c2ecf20Sopenharmony_ci			   0x19c, BIT(1), 0);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic const char * const r_apb1_ir_rx_parents[] = { "osc32k", "dcxo24M" };
1188c2ecf20Sopenharmony_cistatic SUNXI_CCU_MP_WITH_MUX_GATE(r_apb1_ir_rx_clk, "r-apb1-ir-rx",
1198c2ecf20Sopenharmony_ci				  r_apb1_ir_rx_parents, 0x1c0,
1208c2ecf20Sopenharmony_ci				  0, 5,		/* M */
1218c2ecf20Sopenharmony_ci				  8, 2,		/* P */
1228c2ecf20Sopenharmony_ci				  24, 1,	/* mux */
1238c2ecf20Sopenharmony_ci				  BIT(31),	/* gate */
1248c2ecf20Sopenharmony_ci				  0);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE_DATA(r_apb1_bus_ir_rx_clk, "r-apb1-bus-ir-rx",
1278c2ecf20Sopenharmony_ci			   clk_parent_r_apb1, 0x1cc, BIT(0), 0);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE(r_ahb_bus_rtc_clk, "r-ahb-rtc", "r-ahb",
1308c2ecf20Sopenharmony_ci		      0x20c, BIT(0), 0);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic struct ccu_common *sun50i_a100_r_ccu_clks[] = {
1338c2ecf20Sopenharmony_ci	&r_cpus_clk.common,
1348c2ecf20Sopenharmony_ci	&r_apb1_clk.common,
1358c2ecf20Sopenharmony_ci	&r_apb2_clk.common,
1368c2ecf20Sopenharmony_ci	&r_apb1_timer_clk.common,
1378c2ecf20Sopenharmony_ci	&r_apb1_twd_clk.common,
1388c2ecf20Sopenharmony_ci	&r_apb1_pwm_clk.common,
1398c2ecf20Sopenharmony_ci	&r_apb1_bus_pwm_clk.common,
1408c2ecf20Sopenharmony_ci	&r_apb1_ppu_clk.common,
1418c2ecf20Sopenharmony_ci	&r_apb2_uart_clk.common,
1428c2ecf20Sopenharmony_ci	&r_apb2_i2c0_clk.common,
1438c2ecf20Sopenharmony_ci	&r_apb2_i2c1_clk.common,
1448c2ecf20Sopenharmony_ci	&r_apb1_ir_rx_clk.common,
1458c2ecf20Sopenharmony_ci	&r_apb1_bus_ir_rx_clk.common,
1468c2ecf20Sopenharmony_ci	&r_ahb_bus_rtc_clk.common,
1478c2ecf20Sopenharmony_ci};
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistatic struct clk_hw_onecell_data sun50i_a100_r_hw_clks = {
1508c2ecf20Sopenharmony_ci	.hws	= {
1518c2ecf20Sopenharmony_ci		[CLK_R_CPUS]		= &r_cpus_clk.common.hw,
1528c2ecf20Sopenharmony_ci		[CLK_R_AHB]		= &r_ahb_clk.hw,
1538c2ecf20Sopenharmony_ci		[CLK_R_APB1]		= &r_apb1_clk.common.hw,
1548c2ecf20Sopenharmony_ci		[CLK_R_APB2]		= &r_apb2_clk.common.hw,
1558c2ecf20Sopenharmony_ci		[CLK_R_APB1_TIMER]	= &r_apb1_timer_clk.common.hw,
1568c2ecf20Sopenharmony_ci		[CLK_R_APB1_TWD]	= &r_apb1_twd_clk.common.hw,
1578c2ecf20Sopenharmony_ci		[CLK_R_APB1_PWM]	= &r_apb1_pwm_clk.common.hw,
1588c2ecf20Sopenharmony_ci		[CLK_R_APB1_BUS_PWM]	= &r_apb1_bus_pwm_clk.common.hw,
1598c2ecf20Sopenharmony_ci		[CLK_R_APB1_PPU]	= &r_apb1_ppu_clk.common.hw,
1608c2ecf20Sopenharmony_ci		[CLK_R_APB2_UART]	= &r_apb2_uart_clk.common.hw,
1618c2ecf20Sopenharmony_ci		[CLK_R_APB2_I2C0]	= &r_apb2_i2c0_clk.common.hw,
1628c2ecf20Sopenharmony_ci		[CLK_R_APB2_I2C1]	= &r_apb2_i2c1_clk.common.hw,
1638c2ecf20Sopenharmony_ci		[CLK_R_APB1_IR]		= &r_apb1_ir_rx_clk.common.hw,
1648c2ecf20Sopenharmony_ci		[CLK_R_APB1_BUS_IR]	= &r_apb1_bus_ir_rx_clk.common.hw,
1658c2ecf20Sopenharmony_ci		[CLK_R_AHB_BUS_RTC]	= &r_ahb_bus_rtc_clk.common.hw,
1668c2ecf20Sopenharmony_ci	},
1678c2ecf20Sopenharmony_ci	.num	= CLK_NUMBER,
1688c2ecf20Sopenharmony_ci};
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic struct ccu_reset_map sun50i_a100_r_ccu_resets[] = {
1718c2ecf20Sopenharmony_ci	[RST_R_APB1_TIMER]	=  { 0x11c, BIT(16) },
1728c2ecf20Sopenharmony_ci	[RST_R_APB1_BUS_PWM]	=  { 0x13c, BIT(16) },
1738c2ecf20Sopenharmony_ci	[RST_R_APB1_PPU]	=  { 0x17c, BIT(16) },
1748c2ecf20Sopenharmony_ci	[RST_R_APB2_UART]	=  { 0x18c, BIT(16) },
1758c2ecf20Sopenharmony_ci	[RST_R_APB2_I2C0]	=  { 0x19c, BIT(16) },
1768c2ecf20Sopenharmony_ci	[RST_R_APB2_I2C1]	=  { 0x19c, BIT(17) },
1778c2ecf20Sopenharmony_ci	[RST_R_APB1_BUS_IR]	=  { 0x1cc, BIT(16) },
1788c2ecf20Sopenharmony_ci	[RST_R_AHB_BUS_RTC]	=  { 0x20c, BIT(16) },
1798c2ecf20Sopenharmony_ci};
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_cistatic const struct sunxi_ccu_desc sun50i_a100_r_ccu_desc = {
1828c2ecf20Sopenharmony_ci	.ccu_clks	= sun50i_a100_r_ccu_clks,
1838c2ecf20Sopenharmony_ci	.num_ccu_clks	= ARRAY_SIZE(sun50i_a100_r_ccu_clks),
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	.hw_clks	= &sun50i_a100_r_hw_clks,
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	.resets		= sun50i_a100_r_ccu_resets,
1888c2ecf20Sopenharmony_ci	.num_resets	= ARRAY_SIZE(sun50i_a100_r_ccu_resets),
1898c2ecf20Sopenharmony_ci};
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cistatic int sun50i_a100_r_ccu_probe(struct platform_device *pdev)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	void __iomem *reg;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	reg = devm_platform_ioremap_resource(pdev, 0);
1968c2ecf20Sopenharmony_ci	if (IS_ERR(reg))
1978c2ecf20Sopenharmony_ci		return PTR_ERR(reg);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	return sunxi_ccu_probe(pdev->dev.of_node, reg, &sun50i_a100_r_ccu_desc);
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic const struct of_device_id sun50i_a100_r_ccu_ids[] = {
2038c2ecf20Sopenharmony_ci	{ .compatible = "allwinner,sun50i-a100-r-ccu" },
2048c2ecf20Sopenharmony_ci	{ }
2058c2ecf20Sopenharmony_ci};
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic struct platform_driver sun50i_a100_r_ccu_driver = {
2088c2ecf20Sopenharmony_ci	.probe	= sun50i_a100_r_ccu_probe,
2098c2ecf20Sopenharmony_ci	.driver	= {
2108c2ecf20Sopenharmony_ci		.name	= "sun50i-a100-r-ccu",
2118c2ecf20Sopenharmony_ci		.of_match_table	= sun50i_a100_r_ccu_ids,
2128c2ecf20Sopenharmony_ci	},
2138c2ecf20Sopenharmony_ci};
2148c2ecf20Sopenharmony_cimodule_platform_driver(sun50i_a100_r_ccu_driver);
215