xref: /linux/drivers/clk/sunxi-ng/ccu-sun8i-r.c (revision 13e0dde8b2ed043aa3e65437342d501715d975c1)
1cdb8b80bSIcenowy Zheng /*
2cdb8b80bSIcenowy Zheng  * Copyright (c) 2016 Icenowy Zheng <icenowy@aosc.xyz>
3cdb8b80bSIcenowy Zheng  *
4cdb8b80bSIcenowy Zheng  * This software is licensed under the terms of the GNU General Public
5cdb8b80bSIcenowy Zheng  * License version 2, as published by the Free Software Foundation, and
6cdb8b80bSIcenowy Zheng  * may be copied, distributed, and modified under those terms.
7cdb8b80bSIcenowy Zheng  *
8cdb8b80bSIcenowy Zheng  * This program is distributed in the hope that it will be useful,
9cdb8b80bSIcenowy Zheng  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10cdb8b80bSIcenowy Zheng  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11cdb8b80bSIcenowy Zheng  * GNU General Public License for more details.
12cdb8b80bSIcenowy Zheng  */
13cdb8b80bSIcenowy Zheng 
14cdb8b80bSIcenowy Zheng #include <linux/clk-provider.h>
15cdb8b80bSIcenowy Zheng #include <linux/of_address.h>
16cdb8b80bSIcenowy Zheng #include <linux/platform_device.h>
17cdb8b80bSIcenowy Zheng 
18cdb8b80bSIcenowy Zheng #include "ccu_common.h"
19cdb8b80bSIcenowy Zheng #include "ccu_reset.h"
20cdb8b80bSIcenowy Zheng 
21cdb8b80bSIcenowy Zheng #include "ccu_div.h"
22cdb8b80bSIcenowy Zheng #include "ccu_gate.h"
23cdb8b80bSIcenowy Zheng #include "ccu_mp.h"
24cdb8b80bSIcenowy Zheng #include "ccu_nm.h"
25cdb8b80bSIcenowy Zheng 
26cdb8b80bSIcenowy Zheng #include "ccu-sun8i-r.h"
27cdb8b80bSIcenowy Zheng 
28cdb8b80bSIcenowy Zheng static const char * const ar100_parents[] = { "osc32k", "osc24M",
29cdb8b80bSIcenowy Zheng 					     "pll-periph0", "iosc" };
30*13e0dde8SChen-Yu Tsai static const struct ccu_mux_var_prediv ar100_predivs[] = {
31*13e0dde8SChen-Yu Tsai 	{ .index = 2, .shift = 8, .width = 5 },
32*13e0dde8SChen-Yu Tsai };
33cdb8b80bSIcenowy Zheng 
34cdb8b80bSIcenowy Zheng static struct ccu_div ar100_clk = {
35cdb8b80bSIcenowy Zheng 	.div		= _SUNXI_CCU_DIV_FLAGS(4, 2, CLK_DIVIDER_POWER_OF_TWO),
36cdb8b80bSIcenowy Zheng 
37cdb8b80bSIcenowy Zheng 	.mux		= {
38cdb8b80bSIcenowy Zheng 		.shift	= 16,
39cdb8b80bSIcenowy Zheng 		.width	= 2,
40cdb8b80bSIcenowy Zheng 
41*13e0dde8SChen-Yu Tsai 		.var_predivs	= ar100_predivs,
42*13e0dde8SChen-Yu Tsai 		.n_var_predivs	= ARRAY_SIZE(ar100_predivs),
43cdb8b80bSIcenowy Zheng 	},
44cdb8b80bSIcenowy Zheng 
45cdb8b80bSIcenowy Zheng 	.common		= {
46cdb8b80bSIcenowy Zheng 		.reg		= 0x00,
47cdb8b80bSIcenowy Zheng 		.features	= CCU_FEATURE_VARIABLE_PREDIV,
48cdb8b80bSIcenowy Zheng 		.hw.init	= CLK_HW_INIT_PARENTS("ar100",
49cdb8b80bSIcenowy Zheng 						      ar100_parents,
50cdb8b80bSIcenowy Zheng 						      &ccu_div_ops,
51cdb8b80bSIcenowy Zheng 						      0),
52cdb8b80bSIcenowy Zheng 	},
53cdb8b80bSIcenowy Zheng };
54cdb8b80bSIcenowy Zheng 
55cdb8b80bSIcenowy Zheng static CLK_FIXED_FACTOR(ahb0_clk, "ahb0", "ar100", 1, 1, 0);
56cdb8b80bSIcenowy Zheng 
57cdb8b80bSIcenowy Zheng static struct ccu_div apb0_clk = {
58cdb8b80bSIcenowy Zheng 	.div		= _SUNXI_CCU_DIV_FLAGS(0, 2, CLK_DIVIDER_POWER_OF_TWO),
59cdb8b80bSIcenowy Zheng 
60cdb8b80bSIcenowy Zheng 	.common		= {
61cdb8b80bSIcenowy Zheng 		.reg		= 0x0c,
62cdb8b80bSIcenowy Zheng 		.hw.init	= CLK_HW_INIT("apb0",
63cdb8b80bSIcenowy Zheng 					      "ahb0",
64cdb8b80bSIcenowy Zheng 					      &ccu_div_ops,
65cdb8b80bSIcenowy Zheng 					      0),
66cdb8b80bSIcenowy Zheng 	},
67cdb8b80bSIcenowy Zheng };
68cdb8b80bSIcenowy Zheng 
69cdb8b80bSIcenowy Zheng static SUNXI_CCU_GATE(apb0_pio_clk,	"apb0-pio",	"apb0",
70cdb8b80bSIcenowy Zheng 		      0x28, BIT(0), 0);
71cdb8b80bSIcenowy Zheng static SUNXI_CCU_GATE(apb0_ir_clk,	"apb0-ir",	"apb0",
72cdb8b80bSIcenowy Zheng 		      0x28, BIT(1), 0);
73cdb8b80bSIcenowy Zheng static SUNXI_CCU_GATE(apb0_timer_clk,	"apb0-timer",	"apb0",
74cdb8b80bSIcenowy Zheng 		      0x28, BIT(2), 0);
75cdb8b80bSIcenowy Zheng static SUNXI_CCU_GATE(apb0_rsb_clk,	"apb0-rsb",	"apb0",
76cdb8b80bSIcenowy Zheng 		      0x28, BIT(3), 0);
77cdb8b80bSIcenowy Zheng static SUNXI_CCU_GATE(apb0_uart_clk,	"apb0-uart",	"apb0",
78cdb8b80bSIcenowy Zheng 		      0x28, BIT(4), 0);
79cdb8b80bSIcenowy Zheng static SUNXI_CCU_GATE(apb0_i2c_clk,	"apb0-i2c",	"apb0",
80cdb8b80bSIcenowy Zheng 		      0x28, BIT(6), 0);
81cdb8b80bSIcenowy Zheng static SUNXI_CCU_GATE(apb0_twd_clk,	"apb0-twd",	"apb0",
82cdb8b80bSIcenowy Zheng 		      0x28, BIT(7), 0);
83cdb8b80bSIcenowy Zheng 
8437cabc74SIcenowy Zheng static const char * const r_mod0_default_parents[] = { "osc32k", "osc24M" };
85cdb8b80bSIcenowy Zheng static SUNXI_CCU_MP_WITH_MUX_GATE(ir_clk, "ir",
86cdb8b80bSIcenowy Zheng 				  r_mod0_default_parents, 0x54,
87cdb8b80bSIcenowy Zheng 				  0, 4,		/* M */
88cdb8b80bSIcenowy Zheng 				  16, 2,	/* P */
89cdb8b80bSIcenowy Zheng 				  24, 2,	/* mux */
90cdb8b80bSIcenowy Zheng 				  BIT(31),	/* gate */
91cdb8b80bSIcenowy Zheng 				  0);
92cdb8b80bSIcenowy Zheng 
93cdb8b80bSIcenowy Zheng static struct ccu_common *sun8i_h3_r_ccu_clks[] = {
94cdb8b80bSIcenowy Zheng 	&ar100_clk.common,
95cdb8b80bSIcenowy Zheng 	&apb0_clk.common,
96cdb8b80bSIcenowy Zheng 	&apb0_pio_clk.common,
97cdb8b80bSIcenowy Zheng 	&apb0_ir_clk.common,
98cdb8b80bSIcenowy Zheng 	&apb0_timer_clk.common,
99cdb8b80bSIcenowy Zheng 	&apb0_uart_clk.common,
100cdb8b80bSIcenowy Zheng 	&apb0_i2c_clk.common,
101cdb8b80bSIcenowy Zheng 	&apb0_twd_clk.common,
102cdb8b80bSIcenowy Zheng 	&ir_clk.common,
103cdb8b80bSIcenowy Zheng };
104cdb8b80bSIcenowy Zheng 
105cdb8b80bSIcenowy Zheng static struct ccu_common *sun50i_a64_r_ccu_clks[] = {
106cdb8b80bSIcenowy Zheng 	&ar100_clk.common,
107cdb8b80bSIcenowy Zheng 	&apb0_clk.common,
108cdb8b80bSIcenowy Zheng 	&apb0_pio_clk.common,
109cdb8b80bSIcenowy Zheng 	&apb0_ir_clk.common,
110cdb8b80bSIcenowy Zheng 	&apb0_timer_clk.common,
111cdb8b80bSIcenowy Zheng 	&apb0_rsb_clk.common,
112cdb8b80bSIcenowy Zheng 	&apb0_uart_clk.common,
113cdb8b80bSIcenowy Zheng 	&apb0_i2c_clk.common,
114cdb8b80bSIcenowy Zheng 	&apb0_twd_clk.common,
115cdb8b80bSIcenowy Zheng 	&ir_clk.common,
116cdb8b80bSIcenowy Zheng };
117cdb8b80bSIcenowy Zheng 
118cdb8b80bSIcenowy Zheng static struct clk_hw_onecell_data sun8i_h3_r_hw_clks = {
119cdb8b80bSIcenowy Zheng 	.hws	= {
120cdb8b80bSIcenowy Zheng 		[CLK_AR100]		= &ar100_clk.common.hw,
121cdb8b80bSIcenowy Zheng 		[CLK_AHB0]		= &ahb0_clk.hw,
122cdb8b80bSIcenowy Zheng 		[CLK_APB0]		= &apb0_clk.common.hw,
123cdb8b80bSIcenowy Zheng 		[CLK_APB0_PIO]		= &apb0_pio_clk.common.hw,
124cdb8b80bSIcenowy Zheng 		[CLK_APB0_IR]		= &apb0_ir_clk.common.hw,
125cdb8b80bSIcenowy Zheng 		[CLK_APB0_TIMER]	= &apb0_timer_clk.common.hw,
126cdb8b80bSIcenowy Zheng 		[CLK_APB0_UART]		= &apb0_uart_clk.common.hw,
127cdb8b80bSIcenowy Zheng 		[CLK_APB0_I2C]		= &apb0_i2c_clk.common.hw,
128cdb8b80bSIcenowy Zheng 		[CLK_APB0_TWD]		= &apb0_twd_clk.common.hw,
129cdb8b80bSIcenowy Zheng 		[CLK_IR]		= &ir_clk.common.hw,
130cdb8b80bSIcenowy Zheng 	},
131cdb8b80bSIcenowy Zheng 	.num	= CLK_NUMBER,
132cdb8b80bSIcenowy Zheng };
133cdb8b80bSIcenowy Zheng 
134cdb8b80bSIcenowy Zheng static struct clk_hw_onecell_data sun50i_a64_r_hw_clks = {
135cdb8b80bSIcenowy Zheng 	.hws	= {
136cdb8b80bSIcenowy Zheng 		[CLK_AR100]		= &ar100_clk.common.hw,
137cdb8b80bSIcenowy Zheng 		[CLK_AHB0]		= &ahb0_clk.hw,
138cdb8b80bSIcenowy Zheng 		[CLK_APB0]		= &apb0_clk.common.hw,
139cdb8b80bSIcenowy Zheng 		[CLK_APB0_PIO]		= &apb0_pio_clk.common.hw,
140cdb8b80bSIcenowy Zheng 		[CLK_APB0_IR]		= &apb0_ir_clk.common.hw,
141cdb8b80bSIcenowy Zheng 		[CLK_APB0_TIMER]	= &apb0_timer_clk.common.hw,
142cdb8b80bSIcenowy Zheng 		[CLK_APB0_RSB]		= &apb0_rsb_clk.common.hw,
143cdb8b80bSIcenowy Zheng 		[CLK_APB0_UART]		= &apb0_uart_clk.common.hw,
144cdb8b80bSIcenowy Zheng 		[CLK_APB0_I2C]		= &apb0_i2c_clk.common.hw,
145cdb8b80bSIcenowy Zheng 		[CLK_APB0_TWD]		= &apb0_twd_clk.common.hw,
146cdb8b80bSIcenowy Zheng 		[CLK_IR]		= &ir_clk.common.hw,
147cdb8b80bSIcenowy Zheng 	},
148cdb8b80bSIcenowy Zheng 	.num	= CLK_NUMBER,
149cdb8b80bSIcenowy Zheng };
150cdb8b80bSIcenowy Zheng 
151cdb8b80bSIcenowy Zheng static struct ccu_reset_map sun8i_h3_r_ccu_resets[] = {
152cdb8b80bSIcenowy Zheng 	[RST_APB0_IR]		=  { 0xb0, BIT(1) },
153cdb8b80bSIcenowy Zheng 	[RST_APB0_TIMER]	=  { 0xb0, BIT(2) },
154cdb8b80bSIcenowy Zheng 	[RST_APB0_UART]		=  { 0xb0, BIT(4) },
155cdb8b80bSIcenowy Zheng 	[RST_APB0_I2C]		=  { 0xb0, BIT(6) },
156cdb8b80bSIcenowy Zheng };
157cdb8b80bSIcenowy Zheng 
158cdb8b80bSIcenowy Zheng static struct ccu_reset_map sun50i_a64_r_ccu_resets[] = {
159cdb8b80bSIcenowy Zheng 	[RST_APB0_IR]		=  { 0xb0, BIT(1) },
160cdb8b80bSIcenowy Zheng 	[RST_APB0_TIMER]	=  { 0xb0, BIT(2) },
161cdb8b80bSIcenowy Zheng 	[RST_APB0_RSB]		=  { 0xb0, BIT(3) },
162cdb8b80bSIcenowy Zheng 	[RST_APB0_UART]		=  { 0xb0, BIT(4) },
163cdb8b80bSIcenowy Zheng 	[RST_APB0_I2C]		=  { 0xb0, BIT(6) },
164cdb8b80bSIcenowy Zheng };
165cdb8b80bSIcenowy Zheng 
166cdb8b80bSIcenowy Zheng static const struct sunxi_ccu_desc sun8i_h3_r_ccu_desc = {
167cdb8b80bSIcenowy Zheng 	.ccu_clks	= sun8i_h3_r_ccu_clks,
168cdb8b80bSIcenowy Zheng 	.num_ccu_clks	= ARRAY_SIZE(sun8i_h3_r_ccu_clks),
169cdb8b80bSIcenowy Zheng 
170cdb8b80bSIcenowy Zheng 	.hw_clks	= &sun8i_h3_r_hw_clks,
171cdb8b80bSIcenowy Zheng 
172cdb8b80bSIcenowy Zheng 	.resets		= sun8i_h3_r_ccu_resets,
173cdb8b80bSIcenowy Zheng 	.num_resets	= ARRAY_SIZE(sun8i_h3_r_ccu_resets),
174cdb8b80bSIcenowy Zheng };
175cdb8b80bSIcenowy Zheng 
176cdb8b80bSIcenowy Zheng static const struct sunxi_ccu_desc sun50i_a64_r_ccu_desc = {
177cdb8b80bSIcenowy Zheng 	.ccu_clks	= sun50i_a64_r_ccu_clks,
178cdb8b80bSIcenowy Zheng 	.num_ccu_clks	= ARRAY_SIZE(sun50i_a64_r_ccu_clks),
179cdb8b80bSIcenowy Zheng 
180cdb8b80bSIcenowy Zheng 	.hw_clks	= &sun50i_a64_r_hw_clks,
181cdb8b80bSIcenowy Zheng 
182cdb8b80bSIcenowy Zheng 	.resets		= sun50i_a64_r_ccu_resets,
183cdb8b80bSIcenowy Zheng 	.num_resets	= ARRAY_SIZE(sun50i_a64_r_ccu_resets),
184cdb8b80bSIcenowy Zheng };
185cdb8b80bSIcenowy Zheng 
186cdb8b80bSIcenowy Zheng static void __init sunxi_r_ccu_init(struct device_node *node,
187cdb8b80bSIcenowy Zheng 				    const struct sunxi_ccu_desc *desc)
188cdb8b80bSIcenowy Zheng {
189cdb8b80bSIcenowy Zheng 	void __iomem *reg;
190cdb8b80bSIcenowy Zheng 
191cdb8b80bSIcenowy Zheng 	reg = of_io_request_and_map(node, 0, of_node_full_name(node));
192cdb8b80bSIcenowy Zheng 	if (IS_ERR(reg)) {
193cdb8b80bSIcenowy Zheng 		pr_err("%s: Could not map the clock registers\n",
194cdb8b80bSIcenowy Zheng 		       of_node_full_name(node));
195cdb8b80bSIcenowy Zheng 		return;
196cdb8b80bSIcenowy Zheng 	}
197cdb8b80bSIcenowy Zheng 
198cdb8b80bSIcenowy Zheng 	sunxi_ccu_probe(node, reg, desc);
199cdb8b80bSIcenowy Zheng }
200cdb8b80bSIcenowy Zheng 
201cdb8b80bSIcenowy Zheng static void __init sun8i_h3_r_ccu_setup(struct device_node *node)
202cdb8b80bSIcenowy Zheng {
203cdb8b80bSIcenowy Zheng 	sunxi_r_ccu_init(node, &sun8i_h3_r_ccu_desc);
204cdb8b80bSIcenowy Zheng }
205cdb8b80bSIcenowy Zheng CLK_OF_DECLARE(sun8i_h3_r_ccu, "allwinner,sun8i-h3-r-ccu",
206cdb8b80bSIcenowy Zheng 	       sun8i_h3_r_ccu_setup);
207cdb8b80bSIcenowy Zheng 
208cdb8b80bSIcenowy Zheng static void __init sun50i_a64_r_ccu_setup(struct device_node *node)
209cdb8b80bSIcenowy Zheng {
210cdb8b80bSIcenowy Zheng 	sunxi_r_ccu_init(node, &sun50i_a64_r_ccu_desc);
211cdb8b80bSIcenowy Zheng }
212cdb8b80bSIcenowy Zheng CLK_OF_DECLARE(sun50i_a64_r_ccu, "allwinner,sun50i-a64-r-ccu",
213cdb8b80bSIcenowy Zheng 	       sun50i_a64_r_ccu_setup);
214