1c6ca7616SDamien Le Moal // SPDX-License-Identifier: GPL-2.0-or-later
2c6ca7616SDamien Le Moal /*
3c6ca7616SDamien Le Moal * Copyright (C) 2019-20 Sean Anderson <seanga2@gmail.com>
4c6ca7616SDamien Le Moal * Copyright (c) 2019 Western Digital Corporation or its affiliates.
5c6ca7616SDamien Le Moal */
6c6ca7616SDamien Le Moal #define pr_fmt(fmt) "k210-clk: " fmt
7c6ca7616SDamien Le Moal
8c6ca7616SDamien Le Moal #include <linux/io.h>
9c6ca7616SDamien Le Moal #include <linux/slab.h>
10c6ca7616SDamien Le Moal #include <linux/spinlock.h>
11c6ca7616SDamien Le Moal #include <linux/platform_device.h>
12c6ca7616SDamien Le Moal #include <linux/of.h>
13c6ca7616SDamien Le Moal #include <linux/of_clk.h>
14c6ca7616SDamien Le Moal #include <linux/of_address.h>
15c6ca7616SDamien Le Moal #include <linux/clk-provider.h>
16c6ca7616SDamien Le Moal #include <linux/bitfield.h>
17c6ca7616SDamien Le Moal #include <linux/delay.h>
18c6ca7616SDamien Le Moal #include <soc/canaan/k210-sysctl.h>
19c6ca7616SDamien Le Moal
20c6ca7616SDamien Le Moal #include <dt-bindings/clock/k210-clk.h>
21c6ca7616SDamien Le Moal
22c6ca7616SDamien Le Moal struct k210_sysclk;
23c6ca7616SDamien Le Moal
24c6ca7616SDamien Le Moal struct k210_clk {
25c6ca7616SDamien Le Moal int id;
26c6ca7616SDamien Le Moal struct k210_sysclk *ksc;
27c6ca7616SDamien Le Moal struct clk_hw hw;
28c6ca7616SDamien Le Moal };
29c6ca7616SDamien Le Moal
30c6ca7616SDamien Le Moal struct k210_clk_cfg {
31c6ca7616SDamien Le Moal const char *name;
32c6ca7616SDamien Le Moal u8 gate_reg;
33c6ca7616SDamien Le Moal u8 gate_bit;
34c6ca7616SDamien Le Moal u8 div_reg;
35c6ca7616SDamien Le Moal u8 div_shift;
36c6ca7616SDamien Le Moal u8 div_width;
37c6ca7616SDamien Le Moal u8 div_type;
38c6ca7616SDamien Le Moal u8 mux_reg;
39c6ca7616SDamien Le Moal u8 mux_bit;
40c6ca7616SDamien Le Moal };
41c6ca7616SDamien Le Moal
42c6ca7616SDamien Le Moal enum k210_clk_div_type {
43c6ca7616SDamien Le Moal K210_DIV_NONE,
44c6ca7616SDamien Le Moal K210_DIV_ONE_BASED,
45c6ca7616SDamien Le Moal K210_DIV_DOUBLE_ONE_BASED,
46c6ca7616SDamien Le Moal K210_DIV_POWER_OF_TWO,
47c6ca7616SDamien Le Moal };
48c6ca7616SDamien Le Moal
49c6ca7616SDamien Le Moal #define K210_GATE(_reg, _bit) \
50c6ca7616SDamien Le Moal .gate_reg = (_reg), \
51c6ca7616SDamien Le Moal .gate_bit = (_bit)
52c6ca7616SDamien Le Moal
53c6ca7616SDamien Le Moal #define K210_DIV(_reg, _shift, _width, _type) \
54c6ca7616SDamien Le Moal .div_reg = (_reg), \
55c6ca7616SDamien Le Moal .div_shift = (_shift), \
56c6ca7616SDamien Le Moal .div_width = (_width), \
57c6ca7616SDamien Le Moal .div_type = (_type)
58c6ca7616SDamien Le Moal
59c6ca7616SDamien Le Moal #define K210_MUX(_reg, _bit) \
60c6ca7616SDamien Le Moal .mux_reg = (_reg), \
61c6ca7616SDamien Le Moal .mux_bit = (_bit)
62c6ca7616SDamien Le Moal
63c6ca7616SDamien Le Moal static struct k210_clk_cfg k210_clk_cfgs[K210_NUM_CLKS] = {
64c6ca7616SDamien Le Moal /* Gated clocks, no mux, no divider */
65c6ca7616SDamien Le Moal [K210_CLK_CPU] = {
66c6ca7616SDamien Le Moal .name = "cpu",
67c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_CENT, 0)
68c6ca7616SDamien Le Moal },
69c6ca7616SDamien Le Moal [K210_CLK_DMA] = {
70c6ca7616SDamien Le Moal .name = "dma",
71c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 1)
72c6ca7616SDamien Le Moal },
73c6ca7616SDamien Le Moal [K210_CLK_FFT] = {
74c6ca7616SDamien Le Moal .name = "fft",
75c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 4)
76c6ca7616SDamien Le Moal },
77c6ca7616SDamien Le Moal [K210_CLK_GPIO] = {
78c6ca7616SDamien Le Moal .name = "gpio",
79c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 5)
80c6ca7616SDamien Le Moal },
81c6ca7616SDamien Le Moal [K210_CLK_UART1] = {
82c6ca7616SDamien Le Moal .name = "uart1",
83c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 16)
84c6ca7616SDamien Le Moal },
85c6ca7616SDamien Le Moal [K210_CLK_UART2] = {
86c6ca7616SDamien Le Moal .name = "uart2",
87c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 17)
88c6ca7616SDamien Le Moal },
89c6ca7616SDamien Le Moal [K210_CLK_UART3] = {
90c6ca7616SDamien Le Moal .name = "uart3",
91c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 18)
92c6ca7616SDamien Le Moal },
93c6ca7616SDamien Le Moal [K210_CLK_FPIOA] = {
94c6ca7616SDamien Le Moal .name = "fpioa",
95c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 20)
96c6ca7616SDamien Le Moal },
97c6ca7616SDamien Le Moal [K210_CLK_SHA] = {
98c6ca7616SDamien Le Moal .name = "sha",
99c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 26)
100c6ca7616SDamien Le Moal },
101c6ca7616SDamien Le Moal [K210_CLK_AES] = {
102c6ca7616SDamien Le Moal .name = "aes",
103c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 19)
104c6ca7616SDamien Le Moal },
105c6ca7616SDamien Le Moal [K210_CLK_OTP] = {
106c6ca7616SDamien Le Moal .name = "otp",
107c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 27)
108c6ca7616SDamien Le Moal },
109c6ca7616SDamien Le Moal [K210_CLK_RTC] = {
110c6ca7616SDamien Le Moal .name = "rtc",
111c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 29)
112c6ca7616SDamien Le Moal },
113c6ca7616SDamien Le Moal
114c6ca7616SDamien Le Moal /* Gated divider clocks */
115c6ca7616SDamien Le Moal [K210_CLK_SRAM0] = {
116c6ca7616SDamien Le Moal .name = "sram0",
117c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_CENT, 1),
118c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_THR0, 0, 4, K210_DIV_ONE_BASED)
119c6ca7616SDamien Le Moal },
120c6ca7616SDamien Le Moal [K210_CLK_SRAM1] = {
121c6ca7616SDamien Le Moal .name = "sram1",
122c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_CENT, 2),
123c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_THR0, 4, 4, K210_DIV_ONE_BASED)
124c6ca7616SDamien Le Moal },
125c6ca7616SDamien Le Moal [K210_CLK_ROM] = {
126c6ca7616SDamien Le Moal .name = "rom",
127c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 0),
128c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_THR0, 16, 4, K210_DIV_ONE_BASED)
129c6ca7616SDamien Le Moal },
130c6ca7616SDamien Le Moal [K210_CLK_DVP] = {
131c6ca7616SDamien Le Moal .name = "dvp",
132c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 3),
133c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_THR0, 12, 4, K210_DIV_ONE_BASED)
134c6ca7616SDamien Le Moal },
135c6ca7616SDamien Le Moal [K210_CLK_APB0] = {
136c6ca7616SDamien Le Moal .name = "apb0",
137c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_CENT, 3),
138c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_SEL0, 3, 3, K210_DIV_ONE_BASED)
139c6ca7616SDamien Le Moal },
140c6ca7616SDamien Le Moal [K210_CLK_APB1] = {
141c6ca7616SDamien Le Moal .name = "apb1",
142c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_CENT, 4),
143c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_SEL0, 6, 3, K210_DIV_ONE_BASED)
144c6ca7616SDamien Le Moal },
145c6ca7616SDamien Le Moal [K210_CLK_APB2] = {
146c6ca7616SDamien Le Moal .name = "apb2",
147c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_CENT, 5),
148c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_SEL0, 9, 3, K210_DIV_ONE_BASED)
149c6ca7616SDamien Le Moal },
150c6ca7616SDamien Le Moal [K210_CLK_AI] = {
151c6ca7616SDamien Le Moal .name = "ai",
152c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 2),
153c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_THR0, 8, 4, K210_DIV_ONE_BASED)
154c6ca7616SDamien Le Moal },
155c6ca7616SDamien Le Moal [K210_CLK_SPI0] = {
156c6ca7616SDamien Le Moal .name = "spi0",
157c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 6),
158c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_THR1, 0, 8, K210_DIV_DOUBLE_ONE_BASED)
159c6ca7616SDamien Le Moal },
160c6ca7616SDamien Le Moal [K210_CLK_SPI1] = {
161c6ca7616SDamien Le Moal .name = "spi1",
162c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 7),
163c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_THR1, 8, 8, K210_DIV_DOUBLE_ONE_BASED)
164c6ca7616SDamien Le Moal },
165c6ca7616SDamien Le Moal [K210_CLK_SPI2] = {
166c6ca7616SDamien Le Moal .name = "spi2",
167c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 8),
168c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_THR1, 16, 8, K210_DIV_DOUBLE_ONE_BASED)
169c6ca7616SDamien Le Moal },
170c6ca7616SDamien Le Moal [K210_CLK_I2C0] = {
171c6ca7616SDamien Le Moal .name = "i2c0",
172c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 13),
173c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_THR5, 8, 8, K210_DIV_DOUBLE_ONE_BASED)
174c6ca7616SDamien Le Moal },
175c6ca7616SDamien Le Moal [K210_CLK_I2C1] = {
176c6ca7616SDamien Le Moal .name = "i2c1",
177c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 14),
178c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_THR5, 16, 8, K210_DIV_DOUBLE_ONE_BASED)
179c6ca7616SDamien Le Moal },
180c6ca7616SDamien Le Moal [K210_CLK_I2C2] = {
181c6ca7616SDamien Le Moal .name = "i2c2",
182c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 15),
183c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_THR5, 24, 8, K210_DIV_DOUBLE_ONE_BASED)
184c6ca7616SDamien Le Moal },
185c6ca7616SDamien Le Moal [K210_CLK_WDT0] = {
186c6ca7616SDamien Le Moal .name = "wdt0",
187c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 24),
188c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_THR6, 0, 8, K210_DIV_DOUBLE_ONE_BASED)
189c6ca7616SDamien Le Moal },
190c6ca7616SDamien Le Moal [K210_CLK_WDT1] = {
191c6ca7616SDamien Le Moal .name = "wdt1",
192c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 25),
193c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_THR6, 8, 8, K210_DIV_DOUBLE_ONE_BASED)
194c6ca7616SDamien Le Moal },
195c6ca7616SDamien Le Moal [K210_CLK_I2S0] = {
196c6ca7616SDamien Le Moal .name = "i2s0",
197c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 10),
198c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_THR3, 0, 16, K210_DIV_DOUBLE_ONE_BASED)
199c6ca7616SDamien Le Moal },
200c6ca7616SDamien Le Moal [K210_CLK_I2S1] = {
201c6ca7616SDamien Le Moal .name = "i2s1",
202c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 11),
203c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_THR3, 16, 16, K210_DIV_DOUBLE_ONE_BASED)
204c6ca7616SDamien Le Moal },
205c6ca7616SDamien Le Moal [K210_CLK_I2S2] = {
206c6ca7616SDamien Le Moal .name = "i2s2",
207c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 12),
208c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_THR4, 0, 16, K210_DIV_DOUBLE_ONE_BASED)
209c6ca7616SDamien Le Moal },
210c6ca7616SDamien Le Moal
211c6ca7616SDamien Le Moal /* Divider clocks, no gate, no mux */
212c6ca7616SDamien Le Moal [K210_CLK_I2S0_M] = {
213c6ca7616SDamien Le Moal .name = "i2s0_m",
214c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_THR4, 16, 8, K210_DIV_DOUBLE_ONE_BASED)
215c6ca7616SDamien Le Moal },
216c6ca7616SDamien Le Moal [K210_CLK_I2S1_M] = {
217c6ca7616SDamien Le Moal .name = "i2s1_m",
218c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_THR4, 24, 8, K210_DIV_DOUBLE_ONE_BASED)
219c6ca7616SDamien Le Moal },
220c6ca7616SDamien Le Moal [K210_CLK_I2S2_M] = {
221c6ca7616SDamien Le Moal .name = "i2s2_m",
222c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_THR4, 0, 8, K210_DIV_DOUBLE_ONE_BASED)
223c6ca7616SDamien Le Moal },
224c6ca7616SDamien Le Moal
225c6ca7616SDamien Le Moal /* Muxed gated divider clocks */
226c6ca7616SDamien Le Moal [K210_CLK_SPI3] = {
227c6ca7616SDamien Le Moal .name = "spi3",
228c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 9),
229c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_THR1, 24, 8, K210_DIV_DOUBLE_ONE_BASED),
230c6ca7616SDamien Le Moal K210_MUX(K210_SYSCTL_SEL0, 12)
231c6ca7616SDamien Le Moal },
232c6ca7616SDamien Le Moal [K210_CLK_TIMER0] = {
233c6ca7616SDamien Le Moal .name = "timer0",
234c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 21),
235c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_THR2, 0, 8, K210_DIV_DOUBLE_ONE_BASED),
236c6ca7616SDamien Le Moal K210_MUX(K210_SYSCTL_SEL0, 13)
237c6ca7616SDamien Le Moal },
238c6ca7616SDamien Le Moal [K210_CLK_TIMER1] = {
239c6ca7616SDamien Le Moal .name = "timer1",
240c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 22),
241c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_THR2, 8, 8, K210_DIV_DOUBLE_ONE_BASED),
242c6ca7616SDamien Le Moal K210_MUX(K210_SYSCTL_SEL0, 14)
243c6ca7616SDamien Le Moal },
244c6ca7616SDamien Le Moal [K210_CLK_TIMER2] = {
245c6ca7616SDamien Le Moal .name = "timer2",
246c6ca7616SDamien Le Moal K210_GATE(K210_SYSCTL_EN_PERI, 23),
247c6ca7616SDamien Le Moal K210_DIV(K210_SYSCTL_THR2, 16, 8, K210_DIV_DOUBLE_ONE_BASED),
248c6ca7616SDamien Le Moal K210_MUX(K210_SYSCTL_SEL0, 15)
249c6ca7616SDamien Le Moal },
250c6ca7616SDamien Le Moal };
251c6ca7616SDamien Le Moal
252c6ca7616SDamien Le Moal /*
253c6ca7616SDamien Le Moal * PLL control register bits.
254c6ca7616SDamien Le Moal */
255c6ca7616SDamien Le Moal #define K210_PLL_CLKR GENMASK(3, 0)
256c6ca7616SDamien Le Moal #define K210_PLL_CLKF GENMASK(9, 4)
257c6ca7616SDamien Le Moal #define K210_PLL_CLKOD GENMASK(13, 10)
258c6ca7616SDamien Le Moal #define K210_PLL_BWADJ GENMASK(19, 14)
259c6ca7616SDamien Le Moal #define K210_PLL_RESET (1 << 20)
260c6ca7616SDamien Le Moal #define K210_PLL_PWRD (1 << 21)
261c6ca7616SDamien Le Moal #define K210_PLL_INTFB (1 << 22)
262c6ca7616SDamien Le Moal #define K210_PLL_BYPASS (1 << 23)
263c6ca7616SDamien Le Moal #define K210_PLL_TEST (1 << 24)
264c6ca7616SDamien Le Moal #define K210_PLL_EN (1 << 25)
265c6ca7616SDamien Le Moal #define K210_PLL_SEL GENMASK(27, 26) /* PLL2 only */
266c6ca7616SDamien Le Moal
267c6ca7616SDamien Le Moal /*
268c6ca7616SDamien Le Moal * PLL lock register bits.
269c6ca7616SDamien Le Moal */
270c6ca7616SDamien Le Moal #define K210_PLL_LOCK 0
271c6ca7616SDamien Le Moal #define K210_PLL_CLEAR_SLIP 2
272c6ca7616SDamien Le Moal #define K210_PLL_TEST_OUT 3
273c6ca7616SDamien Le Moal
274c6ca7616SDamien Le Moal /*
275c6ca7616SDamien Le Moal * Clock selector register bits.
276c6ca7616SDamien Le Moal */
277c6ca7616SDamien Le Moal #define K210_ACLK_SEL BIT(0)
278c6ca7616SDamien Le Moal #define K210_ACLK_DIV GENMASK(2, 1)
279c6ca7616SDamien Le Moal
280c6ca7616SDamien Le Moal /*
281c6ca7616SDamien Le Moal * PLLs.
282c6ca7616SDamien Le Moal */
283c6ca7616SDamien Le Moal enum k210_pll_id {
284c6ca7616SDamien Le Moal K210_PLL0, K210_PLL1, K210_PLL2, K210_PLL_NUM
285c6ca7616SDamien Le Moal };
286c6ca7616SDamien Le Moal
287c6ca7616SDamien Le Moal struct k210_pll {
288c6ca7616SDamien Le Moal enum k210_pll_id id;
289c6ca7616SDamien Le Moal struct k210_sysclk *ksc;
290c6ca7616SDamien Le Moal void __iomem *base;
291c6ca7616SDamien Le Moal void __iomem *reg;
292c6ca7616SDamien Le Moal void __iomem *lock;
293c6ca7616SDamien Le Moal u8 lock_shift;
294c6ca7616SDamien Le Moal u8 lock_width;
295c6ca7616SDamien Le Moal struct clk_hw hw;
296c6ca7616SDamien Le Moal };
297c6ca7616SDamien Le Moal #define to_k210_pll(_hw) container_of(_hw, struct k210_pll, hw)
298c6ca7616SDamien Le Moal
299c6ca7616SDamien Le Moal /*
300c6ca7616SDamien Le Moal * PLLs configuration: by default PLL0 runs at 780 MHz and PLL1 at 299 MHz.
301c6ca7616SDamien Le Moal * The first 2 SRAM banks depend on ACLK/CPU clock which is by default PLL0
302c6ca7616SDamien Le Moal * rate divided by 2. Set PLL1 to 390 MHz so that the third SRAM bank has the
303c6ca7616SDamien Le Moal * same clock as the first 2.
304c6ca7616SDamien Le Moal */
305c6ca7616SDamien Le Moal struct k210_pll_cfg {
306c6ca7616SDamien Le Moal u32 reg;
307c6ca7616SDamien Le Moal u8 lock_shift;
308c6ca7616SDamien Le Moal u8 lock_width;
309c6ca7616SDamien Le Moal u32 r;
310c6ca7616SDamien Le Moal u32 f;
311c6ca7616SDamien Le Moal u32 od;
312c6ca7616SDamien Le Moal u32 bwadj;
313c6ca7616SDamien Le Moal };
314c6ca7616SDamien Le Moal
315c6ca7616SDamien Le Moal static struct k210_pll_cfg k210_plls_cfg[] = {
316c6ca7616SDamien Le Moal { K210_SYSCTL_PLL0, 0, 2, 0, 59, 1, 59 }, /* 780 MHz */
317c6ca7616SDamien Le Moal { K210_SYSCTL_PLL1, 8, 1, 0, 59, 3, 59 }, /* 390 MHz */
318c6ca7616SDamien Le Moal { K210_SYSCTL_PLL2, 16, 1, 0, 22, 1, 22 }, /* 299 MHz */
319c6ca7616SDamien Le Moal };
320c6ca7616SDamien Le Moal
321c6ca7616SDamien Le Moal /**
322c6ca7616SDamien Le Moal * struct k210_sysclk - sysclk driver data
323c6ca7616SDamien Le Moal * @regs: system controller registers start address
324c6ca7616SDamien Le Moal * @clk_lock: clock setting spinlock
325c6ca7616SDamien Le Moal * @plls: SoC PLLs descriptors
326c6ca7616SDamien Le Moal * @aclk: ACLK clock
327c6ca7616SDamien Le Moal * @clks: All other clocks
328c6ca7616SDamien Le Moal */
329c6ca7616SDamien Le Moal struct k210_sysclk {
330c6ca7616SDamien Le Moal void __iomem *regs;
331c6ca7616SDamien Le Moal spinlock_t clk_lock;
332c6ca7616SDamien Le Moal struct k210_pll plls[K210_PLL_NUM];
333c6ca7616SDamien Le Moal struct clk_hw aclk;
334c6ca7616SDamien Le Moal struct k210_clk clks[K210_NUM_CLKS];
335c6ca7616SDamien Le Moal };
336c6ca7616SDamien Le Moal
337c6ca7616SDamien Le Moal #define to_k210_sysclk(_hw) container_of(_hw, struct k210_sysclk, aclk)
338c6ca7616SDamien Le Moal
339c6ca7616SDamien Le Moal /*
340c6ca7616SDamien Le Moal * Set ACLK parent selector: 0 for IN0, 1 for PLL0.
341c6ca7616SDamien Le Moal */
k210_aclk_set_selector(void __iomem * regs,u8 sel)342c6ca7616SDamien Le Moal static void k210_aclk_set_selector(void __iomem *regs, u8 sel)
343c6ca7616SDamien Le Moal {
344c6ca7616SDamien Le Moal u32 reg = readl(regs + K210_SYSCTL_SEL0);
345c6ca7616SDamien Le Moal
346c6ca7616SDamien Le Moal if (sel)
347c6ca7616SDamien Le Moal reg |= K210_ACLK_SEL;
348c6ca7616SDamien Le Moal else
349c6ca7616SDamien Le Moal reg &= K210_ACLK_SEL;
350c6ca7616SDamien Le Moal writel(reg, regs + K210_SYSCTL_SEL0);
351c6ca7616SDamien Le Moal }
352c6ca7616SDamien Le Moal
k210_init_pll(void __iomem * regs,enum k210_pll_id pllid,struct k210_pll * pll)353c6ca7616SDamien Le Moal static void k210_init_pll(void __iomem *regs, enum k210_pll_id pllid,
354c6ca7616SDamien Le Moal struct k210_pll *pll)
355c6ca7616SDamien Le Moal {
356c6ca7616SDamien Le Moal pll->id = pllid;
357c6ca7616SDamien Le Moal pll->reg = regs + k210_plls_cfg[pllid].reg;
358c6ca7616SDamien Le Moal pll->lock = regs + K210_SYSCTL_PLL_LOCK;
359c6ca7616SDamien Le Moal pll->lock_shift = k210_plls_cfg[pllid].lock_shift;
360c6ca7616SDamien Le Moal pll->lock_width = k210_plls_cfg[pllid].lock_width;
361c6ca7616SDamien Le Moal }
362c6ca7616SDamien Le Moal
k210_pll_wait_for_lock(struct k210_pll * pll)363c6ca7616SDamien Le Moal static void k210_pll_wait_for_lock(struct k210_pll *pll)
364c6ca7616SDamien Le Moal {
365c6ca7616SDamien Le Moal u32 reg, mask = GENMASK(pll->lock_shift + pll->lock_width - 1,
366c6ca7616SDamien Le Moal pll->lock_shift);
367c6ca7616SDamien Le Moal
368c6ca7616SDamien Le Moal while (true) {
369c6ca7616SDamien Le Moal reg = readl(pll->lock);
370c6ca7616SDamien Le Moal if ((reg & mask) == mask)
371c6ca7616SDamien Le Moal break;
372c6ca7616SDamien Le Moal
373c6ca7616SDamien Le Moal reg |= BIT(pll->lock_shift + K210_PLL_CLEAR_SLIP);
374c6ca7616SDamien Le Moal writel(reg, pll->lock);
375c6ca7616SDamien Le Moal }
376c6ca7616SDamien Le Moal }
377c6ca7616SDamien Le Moal
k210_pll_hw_is_enabled(struct k210_pll * pll)378c6ca7616SDamien Le Moal static bool k210_pll_hw_is_enabled(struct k210_pll *pll)
379c6ca7616SDamien Le Moal {
380c6ca7616SDamien Le Moal u32 reg = readl(pll->reg);
381c6ca7616SDamien Le Moal u32 mask = K210_PLL_PWRD | K210_PLL_EN;
382c6ca7616SDamien Le Moal
383c6ca7616SDamien Le Moal if (reg & K210_PLL_RESET)
384c6ca7616SDamien Le Moal return false;
385c6ca7616SDamien Le Moal
386c6ca7616SDamien Le Moal return (reg & mask) == mask;
387c6ca7616SDamien Le Moal }
388c6ca7616SDamien Le Moal
k210_pll_enable_hw(void __iomem * regs,struct k210_pll * pll)389c6ca7616SDamien Le Moal static void k210_pll_enable_hw(void __iomem *regs, struct k210_pll *pll)
390c6ca7616SDamien Le Moal {
391c6ca7616SDamien Le Moal struct k210_pll_cfg *pll_cfg = &k210_plls_cfg[pll->id];
392c6ca7616SDamien Le Moal u32 reg;
393c6ca7616SDamien Le Moal
394c6ca7616SDamien Le Moal if (k210_pll_hw_is_enabled(pll))
395c6ca7616SDamien Le Moal return;
396c6ca7616SDamien Le Moal
397c6ca7616SDamien Le Moal /*
398c6ca7616SDamien Le Moal * For PLL0, we need to re-parent ACLK to IN0 to keep the CPU cores and
399c6ca7616SDamien Le Moal * SRAM running.
400c6ca7616SDamien Le Moal */
401c6ca7616SDamien Le Moal if (pll->id == K210_PLL0)
402c6ca7616SDamien Le Moal k210_aclk_set_selector(regs, 0);
403c6ca7616SDamien Le Moal
404c6ca7616SDamien Le Moal /* Set PLL factors */
405c6ca7616SDamien Le Moal reg = readl(pll->reg);
406c6ca7616SDamien Le Moal reg &= ~GENMASK(19, 0);
407c6ca7616SDamien Le Moal reg |= FIELD_PREP(K210_PLL_CLKR, pll_cfg->r);
408c6ca7616SDamien Le Moal reg |= FIELD_PREP(K210_PLL_CLKF, pll_cfg->f);
409c6ca7616SDamien Le Moal reg |= FIELD_PREP(K210_PLL_CLKOD, pll_cfg->od);
410c6ca7616SDamien Le Moal reg |= FIELD_PREP(K210_PLL_BWADJ, pll_cfg->bwadj);
411c6ca7616SDamien Le Moal reg |= K210_PLL_PWRD;
412c6ca7616SDamien Le Moal writel(reg, pll->reg);
413c6ca7616SDamien Le Moal
414c6ca7616SDamien Le Moal /*
415c6ca7616SDamien Le Moal * Reset the PLL: ensure reset is low before asserting it.
416c6ca7616SDamien Le Moal * The magic NOPs come from the Kendryte reference SDK.
417c6ca7616SDamien Le Moal */
418c6ca7616SDamien Le Moal reg &= ~K210_PLL_RESET;
419c6ca7616SDamien Le Moal writel(reg, pll->reg);
420c6ca7616SDamien Le Moal reg |= K210_PLL_RESET;
421c6ca7616SDamien Le Moal writel(reg, pll->reg);
422c6ca7616SDamien Le Moal nop();
423c6ca7616SDamien Le Moal nop();
424c6ca7616SDamien Le Moal reg &= ~K210_PLL_RESET;
425c6ca7616SDamien Le Moal writel(reg, pll->reg);
426c6ca7616SDamien Le Moal
427c6ca7616SDamien Le Moal k210_pll_wait_for_lock(pll);
428c6ca7616SDamien Le Moal
429c6ca7616SDamien Le Moal reg &= ~K210_PLL_BYPASS;
430c6ca7616SDamien Le Moal reg |= K210_PLL_EN;
431c6ca7616SDamien Le Moal writel(reg, pll->reg);
432c6ca7616SDamien Le Moal
433c6ca7616SDamien Le Moal if (pll->id == K210_PLL0)
434c6ca7616SDamien Le Moal k210_aclk_set_selector(regs, 1);
435c6ca7616SDamien Le Moal }
436c6ca7616SDamien Le Moal
k210_pll_enable(struct clk_hw * hw)437c6ca7616SDamien Le Moal static int k210_pll_enable(struct clk_hw *hw)
438c6ca7616SDamien Le Moal {
439c6ca7616SDamien Le Moal struct k210_pll *pll = to_k210_pll(hw);
440c6ca7616SDamien Le Moal struct k210_sysclk *ksc = pll->ksc;
441c6ca7616SDamien Le Moal unsigned long flags;
442c6ca7616SDamien Le Moal
443c6ca7616SDamien Le Moal spin_lock_irqsave(&ksc->clk_lock, flags);
444c6ca7616SDamien Le Moal
445c6ca7616SDamien Le Moal k210_pll_enable_hw(ksc->regs, pll);
446c6ca7616SDamien Le Moal
447c6ca7616SDamien Le Moal spin_unlock_irqrestore(&ksc->clk_lock, flags);
448c6ca7616SDamien Le Moal
449c6ca7616SDamien Le Moal return 0;
450c6ca7616SDamien Le Moal }
451c6ca7616SDamien Le Moal
k210_pll_disable(struct clk_hw * hw)452c6ca7616SDamien Le Moal static void k210_pll_disable(struct clk_hw *hw)
453c6ca7616SDamien Le Moal {
454c6ca7616SDamien Le Moal struct k210_pll *pll = to_k210_pll(hw);
455c6ca7616SDamien Le Moal struct k210_sysclk *ksc = pll->ksc;
456c6ca7616SDamien Le Moal unsigned long flags;
457c6ca7616SDamien Le Moal u32 reg;
458c6ca7616SDamien Le Moal
459c6ca7616SDamien Le Moal /*
460c6ca7616SDamien Le Moal * Bypassing before powering off is important so child clocks do not
461c6ca7616SDamien Le Moal * stop working. This is especially important for pll0, the indirect
462c6ca7616SDamien Le Moal * parent of the cpu clock.
463c6ca7616SDamien Le Moal */
464c6ca7616SDamien Le Moal spin_lock_irqsave(&ksc->clk_lock, flags);
465c6ca7616SDamien Le Moal reg = readl(pll->reg);
466c6ca7616SDamien Le Moal reg |= K210_PLL_BYPASS;
467c6ca7616SDamien Le Moal writel(reg, pll->reg);
468c6ca7616SDamien Le Moal
469c6ca7616SDamien Le Moal reg &= ~K210_PLL_PWRD;
470c6ca7616SDamien Le Moal reg &= ~K210_PLL_EN;
471c6ca7616SDamien Le Moal writel(reg, pll->reg);
472c6ca7616SDamien Le Moal spin_unlock_irqrestore(&ksc->clk_lock, flags);
473c6ca7616SDamien Le Moal }
474c6ca7616SDamien Le Moal
k210_pll_is_enabled(struct clk_hw * hw)475c6ca7616SDamien Le Moal static int k210_pll_is_enabled(struct clk_hw *hw)
476c6ca7616SDamien Le Moal {
477c6ca7616SDamien Le Moal return k210_pll_hw_is_enabled(to_k210_pll(hw));
478c6ca7616SDamien Le Moal }
479c6ca7616SDamien Le Moal
k210_pll_get_rate(struct clk_hw * hw,unsigned long parent_rate)480c6ca7616SDamien Le Moal static unsigned long k210_pll_get_rate(struct clk_hw *hw,
481c6ca7616SDamien Le Moal unsigned long parent_rate)
482c6ca7616SDamien Le Moal {
483c6ca7616SDamien Le Moal struct k210_pll *pll = to_k210_pll(hw);
484c6ca7616SDamien Le Moal u32 reg = readl(pll->reg);
485c6ca7616SDamien Le Moal u32 r, f, od;
486c6ca7616SDamien Le Moal
487c6ca7616SDamien Le Moal if (reg & K210_PLL_BYPASS)
488c6ca7616SDamien Le Moal return parent_rate;
489c6ca7616SDamien Le Moal
490c6ca7616SDamien Le Moal if (!(reg & K210_PLL_PWRD))
491c6ca7616SDamien Le Moal return 0;
492c6ca7616SDamien Le Moal
493c6ca7616SDamien Le Moal r = FIELD_GET(K210_PLL_CLKR, reg) + 1;
494c6ca7616SDamien Le Moal f = FIELD_GET(K210_PLL_CLKF, reg) + 1;
495c6ca7616SDamien Le Moal od = FIELD_GET(K210_PLL_CLKOD, reg) + 1;
496c6ca7616SDamien Le Moal
49789dc65a7SConor Dooley return div_u64((u64)parent_rate * f, r * od);
498c6ca7616SDamien Le Moal }
499c6ca7616SDamien Le Moal
500c6ca7616SDamien Le Moal static const struct clk_ops k210_pll_ops = {
501c6ca7616SDamien Le Moal .enable = k210_pll_enable,
502c6ca7616SDamien Le Moal .disable = k210_pll_disable,
503c6ca7616SDamien Le Moal .is_enabled = k210_pll_is_enabled,
504c6ca7616SDamien Le Moal .recalc_rate = k210_pll_get_rate,
505c6ca7616SDamien Le Moal };
506c6ca7616SDamien Le Moal
k210_pll2_set_parent(struct clk_hw * hw,u8 index)507c6ca7616SDamien Le Moal static int k210_pll2_set_parent(struct clk_hw *hw, u8 index)
508c6ca7616SDamien Le Moal {
509c6ca7616SDamien Le Moal struct k210_pll *pll = to_k210_pll(hw);
510c6ca7616SDamien Le Moal struct k210_sysclk *ksc = pll->ksc;
511c6ca7616SDamien Le Moal unsigned long flags;
512c6ca7616SDamien Le Moal u32 reg;
513c6ca7616SDamien Le Moal
514c6ca7616SDamien Le Moal spin_lock_irqsave(&ksc->clk_lock, flags);
515c6ca7616SDamien Le Moal
516c6ca7616SDamien Le Moal reg = readl(pll->reg);
517c6ca7616SDamien Le Moal reg &= ~K210_PLL_SEL;
518c6ca7616SDamien Le Moal reg |= FIELD_PREP(K210_PLL_SEL, index);
519c6ca7616SDamien Le Moal writel(reg, pll->reg);
520c6ca7616SDamien Le Moal
521c6ca7616SDamien Le Moal spin_unlock_irqrestore(&ksc->clk_lock, flags);
522c6ca7616SDamien Le Moal
523c6ca7616SDamien Le Moal return 0;
524c6ca7616SDamien Le Moal }
525c6ca7616SDamien Le Moal
k210_pll2_get_parent(struct clk_hw * hw)526c6ca7616SDamien Le Moal static u8 k210_pll2_get_parent(struct clk_hw *hw)
527c6ca7616SDamien Le Moal {
528c6ca7616SDamien Le Moal struct k210_pll *pll = to_k210_pll(hw);
529c6ca7616SDamien Le Moal u32 reg = readl(pll->reg);
530c6ca7616SDamien Le Moal
531c6ca7616SDamien Le Moal return FIELD_GET(K210_PLL_SEL, reg);
532c6ca7616SDamien Le Moal }
533c6ca7616SDamien Le Moal
534c6ca7616SDamien Le Moal static const struct clk_ops k210_pll2_ops = {
535c6ca7616SDamien Le Moal .enable = k210_pll_enable,
536c6ca7616SDamien Le Moal .disable = k210_pll_disable,
537c6ca7616SDamien Le Moal .is_enabled = k210_pll_is_enabled,
538c6ca7616SDamien Le Moal .recalc_rate = k210_pll_get_rate,
5398e3f1560SMaxime Ripard .determine_rate = clk_hw_determine_rate_no_reparent,
540c6ca7616SDamien Le Moal .set_parent = k210_pll2_set_parent,
541c6ca7616SDamien Le Moal .get_parent = k210_pll2_get_parent,
542c6ca7616SDamien Le Moal };
543c6ca7616SDamien Le Moal
k210_register_pll(struct device_node * np,struct k210_sysclk * ksc,enum k210_pll_id pllid,const char * name,int num_parents,const struct clk_ops * ops)544c6ca7616SDamien Le Moal static int __init k210_register_pll(struct device_node *np,
545c6ca7616SDamien Le Moal struct k210_sysclk *ksc,
546c6ca7616SDamien Le Moal enum k210_pll_id pllid, const char *name,
547c6ca7616SDamien Le Moal int num_parents, const struct clk_ops *ops)
548c6ca7616SDamien Le Moal {
549c6ca7616SDamien Le Moal struct k210_pll *pll = &ksc->plls[pllid];
550c6ca7616SDamien Le Moal struct clk_init_data init = {};
551c6ca7616SDamien Le Moal const struct clk_parent_data parent_data[] = {
552c6ca7616SDamien Le Moal { /* .index = 0 for in0 */ },
553c6ca7616SDamien Le Moal { .hw = &ksc->plls[K210_PLL0].hw },
554c6ca7616SDamien Le Moal { .hw = &ksc->plls[K210_PLL1].hw },
555c6ca7616SDamien Le Moal };
556c6ca7616SDamien Le Moal
557c6ca7616SDamien Le Moal init.name = name;
558c6ca7616SDamien Le Moal init.parent_data = parent_data;
559c6ca7616SDamien Le Moal init.num_parents = num_parents;
560c6ca7616SDamien Le Moal init.ops = ops;
561c6ca7616SDamien Le Moal
562c6ca7616SDamien Le Moal pll->hw.init = &init;
563c6ca7616SDamien Le Moal pll->ksc = ksc;
564c6ca7616SDamien Le Moal
565c6ca7616SDamien Le Moal return of_clk_hw_register(np, &pll->hw);
566c6ca7616SDamien Le Moal }
567c6ca7616SDamien Le Moal
k210_register_plls(struct device_node * np,struct k210_sysclk * ksc)568c6ca7616SDamien Le Moal static int __init k210_register_plls(struct device_node *np,
569c6ca7616SDamien Le Moal struct k210_sysclk *ksc)
570c6ca7616SDamien Le Moal {
571c6ca7616SDamien Le Moal int i, ret;
572c6ca7616SDamien Le Moal
573c6ca7616SDamien Le Moal for (i = 0; i < K210_PLL_NUM; i++)
574c6ca7616SDamien Le Moal k210_init_pll(ksc->regs, i, &ksc->plls[i]);
575c6ca7616SDamien Le Moal
576c6ca7616SDamien Le Moal /* PLL0 and PLL1 only have IN0 as parent */
577c6ca7616SDamien Le Moal ret = k210_register_pll(np, ksc, K210_PLL0, "pll0", 1, &k210_pll_ops);
578c6ca7616SDamien Le Moal if (ret) {
579c6ca7616SDamien Le Moal pr_err("%pOFP: register PLL0 failed\n", np);
580c6ca7616SDamien Le Moal return ret;
581c6ca7616SDamien Le Moal }
582c6ca7616SDamien Le Moal ret = k210_register_pll(np, ksc, K210_PLL1, "pll1", 1, &k210_pll_ops);
583c6ca7616SDamien Le Moal if (ret) {
584c6ca7616SDamien Le Moal pr_err("%pOFP: register PLL1 failed\n", np);
585c6ca7616SDamien Le Moal return ret;
586c6ca7616SDamien Le Moal }
587c6ca7616SDamien Le Moal
588c6ca7616SDamien Le Moal /* PLL2 has IN0, PLL0 and PLL1 as parents */
589c6ca7616SDamien Le Moal ret = k210_register_pll(np, ksc, K210_PLL2, "pll2", 3, &k210_pll2_ops);
590c6ca7616SDamien Le Moal if (ret) {
591c6ca7616SDamien Le Moal pr_err("%pOFP: register PLL2 failed\n", np);
592c6ca7616SDamien Le Moal return ret;
593c6ca7616SDamien Le Moal }
594c6ca7616SDamien Le Moal
595c6ca7616SDamien Le Moal return 0;
596c6ca7616SDamien Le Moal }
597c6ca7616SDamien Le Moal
k210_aclk_set_parent(struct clk_hw * hw,u8 index)598c6ca7616SDamien Le Moal static int k210_aclk_set_parent(struct clk_hw *hw, u8 index)
599c6ca7616SDamien Le Moal {
600c6ca7616SDamien Le Moal struct k210_sysclk *ksc = to_k210_sysclk(hw);
601c6ca7616SDamien Le Moal unsigned long flags;
602c6ca7616SDamien Le Moal
603c6ca7616SDamien Le Moal spin_lock_irqsave(&ksc->clk_lock, flags);
604c6ca7616SDamien Le Moal
605c6ca7616SDamien Le Moal k210_aclk_set_selector(ksc->regs, index);
606c6ca7616SDamien Le Moal
607c6ca7616SDamien Le Moal spin_unlock_irqrestore(&ksc->clk_lock, flags);
608c6ca7616SDamien Le Moal
609c6ca7616SDamien Le Moal return 0;
610c6ca7616SDamien Le Moal }
611c6ca7616SDamien Le Moal
k210_aclk_get_parent(struct clk_hw * hw)612c6ca7616SDamien Le Moal static u8 k210_aclk_get_parent(struct clk_hw *hw)
613c6ca7616SDamien Le Moal {
614c6ca7616SDamien Le Moal struct k210_sysclk *ksc = to_k210_sysclk(hw);
615c6ca7616SDamien Le Moal u32 sel;
616c6ca7616SDamien Le Moal
617c6ca7616SDamien Le Moal sel = readl(ksc->regs + K210_SYSCTL_SEL0) & K210_ACLK_SEL;
618c6ca7616SDamien Le Moal
619c6ca7616SDamien Le Moal return sel ? 1 : 0;
620c6ca7616SDamien Le Moal }
621c6ca7616SDamien Le Moal
k210_aclk_get_rate(struct clk_hw * hw,unsigned long parent_rate)622c6ca7616SDamien Le Moal static unsigned long k210_aclk_get_rate(struct clk_hw *hw,
623c6ca7616SDamien Le Moal unsigned long parent_rate)
624c6ca7616SDamien Le Moal {
625c6ca7616SDamien Le Moal struct k210_sysclk *ksc = to_k210_sysclk(hw);
626c6ca7616SDamien Le Moal u32 reg = readl(ksc->regs + K210_SYSCTL_SEL0);
627c6ca7616SDamien Le Moal unsigned int shift;
628c6ca7616SDamien Le Moal
629c6ca7616SDamien Le Moal if (!(reg & 0x1))
630c6ca7616SDamien Le Moal return parent_rate;
631c6ca7616SDamien Le Moal
632c6ca7616SDamien Le Moal shift = FIELD_GET(K210_ACLK_DIV, reg);
633c6ca7616SDamien Le Moal
634c6ca7616SDamien Le Moal return parent_rate / (2UL << shift);
635c6ca7616SDamien Le Moal }
636c6ca7616SDamien Le Moal
637c6ca7616SDamien Le Moal static const struct clk_ops k210_aclk_ops = {
638d0f775d0SMaxime Ripard .determine_rate = clk_hw_determine_rate_no_reparent,
639c6ca7616SDamien Le Moal .set_parent = k210_aclk_set_parent,
640c6ca7616SDamien Le Moal .get_parent = k210_aclk_get_parent,
641c6ca7616SDamien Le Moal .recalc_rate = k210_aclk_get_rate,
642c6ca7616SDamien Le Moal };
643c6ca7616SDamien Le Moal
644c6ca7616SDamien Le Moal /*
645c6ca7616SDamien Le Moal * ACLK has IN0 and PLL0 as parents.
646c6ca7616SDamien Le Moal */
k210_register_aclk(struct device_node * np,struct k210_sysclk * ksc)647c6ca7616SDamien Le Moal static int __init k210_register_aclk(struct device_node *np,
648c6ca7616SDamien Le Moal struct k210_sysclk *ksc)
649c6ca7616SDamien Le Moal {
650c6ca7616SDamien Le Moal struct clk_init_data init = {};
651c6ca7616SDamien Le Moal const struct clk_parent_data parent_data[] = {
652c6ca7616SDamien Le Moal { /* .index = 0 for in0 */ },
653c6ca7616SDamien Le Moal { .hw = &ksc->plls[K210_PLL0].hw },
654c6ca7616SDamien Le Moal };
655c6ca7616SDamien Le Moal int ret;
656c6ca7616SDamien Le Moal
657c6ca7616SDamien Le Moal init.name = "aclk";
658c6ca7616SDamien Le Moal init.parent_data = parent_data;
659c6ca7616SDamien Le Moal init.num_parents = 2;
660c6ca7616SDamien Le Moal init.ops = &k210_aclk_ops;
661c6ca7616SDamien Le Moal ksc->aclk.init = &init;
662c6ca7616SDamien Le Moal
663c6ca7616SDamien Le Moal ret = of_clk_hw_register(np, &ksc->aclk);
664c6ca7616SDamien Le Moal if (ret) {
665c6ca7616SDamien Le Moal pr_err("%pOFP: register aclk failed\n", np);
666c6ca7616SDamien Le Moal return ret;
667c6ca7616SDamien Le Moal }
668c6ca7616SDamien Le Moal
669c6ca7616SDamien Le Moal return 0;
670c6ca7616SDamien Le Moal }
671c6ca7616SDamien Le Moal
672c6ca7616SDamien Le Moal #define to_k210_clk(_hw) container_of(_hw, struct k210_clk, hw)
673c6ca7616SDamien Le Moal
k210_clk_enable(struct clk_hw * hw)674c6ca7616SDamien Le Moal static int k210_clk_enable(struct clk_hw *hw)
675c6ca7616SDamien Le Moal {
676c6ca7616SDamien Le Moal struct k210_clk *kclk = to_k210_clk(hw);
677c6ca7616SDamien Le Moal struct k210_sysclk *ksc = kclk->ksc;
678c6ca7616SDamien Le Moal struct k210_clk_cfg *cfg = &k210_clk_cfgs[kclk->id];
679c6ca7616SDamien Le Moal unsigned long flags;
680c6ca7616SDamien Le Moal u32 reg;
681c6ca7616SDamien Le Moal
682c6ca7616SDamien Le Moal if (!cfg->gate_reg)
683c6ca7616SDamien Le Moal return 0;
684c6ca7616SDamien Le Moal
685c6ca7616SDamien Le Moal spin_lock_irqsave(&ksc->clk_lock, flags);
686c6ca7616SDamien Le Moal reg = readl(ksc->regs + cfg->gate_reg);
687c6ca7616SDamien Le Moal reg |= BIT(cfg->gate_bit);
688c6ca7616SDamien Le Moal writel(reg, ksc->regs + cfg->gate_reg);
689c6ca7616SDamien Le Moal spin_unlock_irqrestore(&ksc->clk_lock, flags);
690c6ca7616SDamien Le Moal
691c6ca7616SDamien Le Moal return 0;
692c6ca7616SDamien Le Moal }
693c6ca7616SDamien Le Moal
k210_clk_disable(struct clk_hw * hw)694c6ca7616SDamien Le Moal static void k210_clk_disable(struct clk_hw *hw)
695c6ca7616SDamien Le Moal {
696c6ca7616SDamien Le Moal struct k210_clk *kclk = to_k210_clk(hw);
697c6ca7616SDamien Le Moal struct k210_sysclk *ksc = kclk->ksc;
698c6ca7616SDamien Le Moal struct k210_clk_cfg *cfg = &k210_clk_cfgs[kclk->id];
699c6ca7616SDamien Le Moal unsigned long flags;
700c6ca7616SDamien Le Moal u32 reg;
701c6ca7616SDamien Le Moal
702c6ca7616SDamien Le Moal if (!cfg->gate_reg)
703c6ca7616SDamien Le Moal return;
704c6ca7616SDamien Le Moal
705c6ca7616SDamien Le Moal spin_lock_irqsave(&ksc->clk_lock, flags);
706c6ca7616SDamien Le Moal reg = readl(ksc->regs + cfg->gate_reg);
707c6ca7616SDamien Le Moal reg &= ~BIT(cfg->gate_bit);
708c6ca7616SDamien Le Moal writel(reg, ksc->regs + cfg->gate_reg);
709c6ca7616SDamien Le Moal spin_unlock_irqrestore(&ksc->clk_lock, flags);
710c6ca7616SDamien Le Moal }
711c6ca7616SDamien Le Moal
k210_clk_set_parent(struct clk_hw * hw,u8 index)712c6ca7616SDamien Le Moal static int k210_clk_set_parent(struct clk_hw *hw, u8 index)
713c6ca7616SDamien Le Moal {
714c6ca7616SDamien Le Moal struct k210_clk *kclk = to_k210_clk(hw);
715c6ca7616SDamien Le Moal struct k210_sysclk *ksc = kclk->ksc;
716c6ca7616SDamien Le Moal struct k210_clk_cfg *cfg = &k210_clk_cfgs[kclk->id];
717c6ca7616SDamien Le Moal unsigned long flags;
718c6ca7616SDamien Le Moal u32 reg;
719c6ca7616SDamien Le Moal
720c6ca7616SDamien Le Moal spin_lock_irqsave(&ksc->clk_lock, flags);
721c6ca7616SDamien Le Moal reg = readl(ksc->regs + cfg->mux_reg);
722c6ca7616SDamien Le Moal if (index)
723c6ca7616SDamien Le Moal reg |= BIT(cfg->mux_bit);
724c6ca7616SDamien Le Moal else
725c6ca7616SDamien Le Moal reg &= ~BIT(cfg->mux_bit);
726faa0e307SDamien Le Moal writel(reg, ksc->regs + cfg->mux_reg);
727c6ca7616SDamien Le Moal spin_unlock_irqrestore(&ksc->clk_lock, flags);
728c6ca7616SDamien Le Moal
729c6ca7616SDamien Le Moal return 0;
730c6ca7616SDamien Le Moal }
731c6ca7616SDamien Le Moal
k210_clk_get_parent(struct clk_hw * hw)732c6ca7616SDamien Le Moal static u8 k210_clk_get_parent(struct clk_hw *hw)
733c6ca7616SDamien Le Moal {
734c6ca7616SDamien Le Moal struct k210_clk *kclk = to_k210_clk(hw);
735c6ca7616SDamien Le Moal struct k210_sysclk *ksc = kclk->ksc;
736c6ca7616SDamien Le Moal struct k210_clk_cfg *cfg = &k210_clk_cfgs[kclk->id];
737c6ca7616SDamien Le Moal unsigned long flags;
738c6ca7616SDamien Le Moal u32 reg, idx;
739c6ca7616SDamien Le Moal
740c6ca7616SDamien Le Moal spin_lock_irqsave(&ksc->clk_lock, flags);
741c6ca7616SDamien Le Moal reg = readl(ksc->regs + cfg->mux_reg);
742c6ca7616SDamien Le Moal idx = (reg & BIT(cfg->mux_bit)) ? 1 : 0;
743c6ca7616SDamien Le Moal spin_unlock_irqrestore(&ksc->clk_lock, flags);
744c6ca7616SDamien Le Moal
745c6ca7616SDamien Le Moal return idx;
746c6ca7616SDamien Le Moal }
747c6ca7616SDamien Le Moal
k210_clk_get_rate(struct clk_hw * hw,unsigned long parent_rate)748c6ca7616SDamien Le Moal static unsigned long k210_clk_get_rate(struct clk_hw *hw,
749c6ca7616SDamien Le Moal unsigned long parent_rate)
750c6ca7616SDamien Le Moal {
751c6ca7616SDamien Le Moal struct k210_clk *kclk = to_k210_clk(hw);
752c6ca7616SDamien Le Moal struct k210_sysclk *ksc = kclk->ksc;
753c6ca7616SDamien Le Moal struct k210_clk_cfg *cfg = &k210_clk_cfgs[kclk->id];
754c6ca7616SDamien Le Moal u32 reg, div_val;
755c6ca7616SDamien Le Moal
756c6ca7616SDamien Le Moal if (!cfg->div_reg)
757c6ca7616SDamien Le Moal return parent_rate;
758c6ca7616SDamien Le Moal
759c6ca7616SDamien Le Moal reg = readl(ksc->regs + cfg->div_reg);
760c6ca7616SDamien Le Moal div_val = (reg >> cfg->div_shift) & GENMASK(cfg->div_width - 1, 0);
761c6ca7616SDamien Le Moal
762c6ca7616SDamien Le Moal switch (cfg->div_type) {
763c6ca7616SDamien Le Moal case K210_DIV_ONE_BASED:
764c6ca7616SDamien Le Moal return parent_rate / (div_val + 1);
765c6ca7616SDamien Le Moal case K210_DIV_DOUBLE_ONE_BASED:
766c6ca7616SDamien Le Moal return parent_rate / ((div_val + 1) * 2);
767c6ca7616SDamien Le Moal case K210_DIV_POWER_OF_TWO:
768c6ca7616SDamien Le Moal return parent_rate / (2UL << div_val);
769c6ca7616SDamien Le Moal case K210_DIV_NONE:
770c6ca7616SDamien Le Moal default:
771c6ca7616SDamien Le Moal return 0;
772c6ca7616SDamien Le Moal }
773c6ca7616SDamien Le Moal }
774c6ca7616SDamien Le Moal
775c6ca7616SDamien Le Moal static const struct clk_ops k210_clk_mux_ops = {
776c6ca7616SDamien Le Moal .enable = k210_clk_enable,
777c6ca7616SDamien Le Moal .disable = k210_clk_disable,
778*f6a01564SMaxime Ripard .determine_rate = clk_hw_determine_rate_no_reparent,
779c6ca7616SDamien Le Moal .set_parent = k210_clk_set_parent,
780c6ca7616SDamien Le Moal .get_parent = k210_clk_get_parent,
781c6ca7616SDamien Le Moal .recalc_rate = k210_clk_get_rate,
782c6ca7616SDamien Le Moal };
783c6ca7616SDamien Le Moal
784c6ca7616SDamien Le Moal static const struct clk_ops k210_clk_ops = {
785c6ca7616SDamien Le Moal .enable = k210_clk_enable,
786c6ca7616SDamien Le Moal .disable = k210_clk_disable,
787c6ca7616SDamien Le Moal .recalc_rate = k210_clk_get_rate,
788c6ca7616SDamien Le Moal };
789c6ca7616SDamien Le Moal
k210_register_clk(struct device_node * np,struct k210_sysclk * ksc,int id,const struct clk_parent_data * parent_data,int num_parents,unsigned long flags)790c6ca7616SDamien Le Moal static void __init k210_register_clk(struct device_node *np,
791c6ca7616SDamien Le Moal struct k210_sysclk *ksc, int id,
792c6ca7616SDamien Le Moal const struct clk_parent_data *parent_data,
793c6ca7616SDamien Le Moal int num_parents, unsigned long flags)
794c6ca7616SDamien Le Moal {
795c6ca7616SDamien Le Moal struct k210_clk *kclk = &ksc->clks[id];
796c6ca7616SDamien Le Moal struct clk_init_data init = {};
797c6ca7616SDamien Le Moal int ret;
798c6ca7616SDamien Le Moal
799c6ca7616SDamien Le Moal init.name = k210_clk_cfgs[id].name;
800c6ca7616SDamien Le Moal init.flags = flags;
801c6ca7616SDamien Le Moal init.parent_data = parent_data;
802c6ca7616SDamien Le Moal init.num_parents = num_parents;
803c6ca7616SDamien Le Moal if (num_parents > 1)
804c6ca7616SDamien Le Moal init.ops = &k210_clk_mux_ops;
805c6ca7616SDamien Le Moal else
806c6ca7616SDamien Le Moal init.ops = &k210_clk_ops;
807c6ca7616SDamien Le Moal
808c6ca7616SDamien Le Moal kclk->id = id;
809c6ca7616SDamien Le Moal kclk->ksc = ksc;
810c6ca7616SDamien Le Moal kclk->hw.init = &init;
811c6ca7616SDamien Le Moal
812c6ca7616SDamien Le Moal ret = of_clk_hw_register(np, &kclk->hw);
813c6ca7616SDamien Le Moal if (ret) {
814c6ca7616SDamien Le Moal pr_err("%pOFP: register clock %s failed\n",
815c6ca7616SDamien Le Moal np, k210_clk_cfgs[id].name);
816c6ca7616SDamien Le Moal kclk->id = -1;
817c6ca7616SDamien Le Moal }
818c6ca7616SDamien Le Moal }
819c6ca7616SDamien Le Moal
820c6ca7616SDamien Le Moal /*
821c6ca7616SDamien Le Moal * All muxed clocks have IN0 and PLL0 as parents.
822c6ca7616SDamien Le Moal */
k210_register_mux_clk(struct device_node * np,struct k210_sysclk * ksc,int id)823c6ca7616SDamien Le Moal static inline void __init k210_register_mux_clk(struct device_node *np,
824c6ca7616SDamien Le Moal struct k210_sysclk *ksc, int id)
825c6ca7616SDamien Le Moal {
826c6ca7616SDamien Le Moal const struct clk_parent_data parent_data[2] = {
827c6ca7616SDamien Le Moal { /* .index = 0 for in0 */ },
828c6ca7616SDamien Le Moal { .hw = &ksc->plls[K210_PLL0].hw }
829c6ca7616SDamien Le Moal };
830c6ca7616SDamien Le Moal
831c6ca7616SDamien Le Moal k210_register_clk(np, ksc, id, parent_data, 2, 0);
832c6ca7616SDamien Le Moal }
833c6ca7616SDamien Le Moal
k210_register_in0_child(struct device_node * np,struct k210_sysclk * ksc,int id)834c6ca7616SDamien Le Moal static inline void __init k210_register_in0_child(struct device_node *np,
835c6ca7616SDamien Le Moal struct k210_sysclk *ksc, int id)
836c6ca7616SDamien Le Moal {
837c6ca7616SDamien Le Moal const struct clk_parent_data parent_data = {
838c6ca7616SDamien Le Moal /* .index = 0 for in0 */
839c6ca7616SDamien Le Moal };
840c6ca7616SDamien Le Moal
841c6ca7616SDamien Le Moal k210_register_clk(np, ksc, id, &parent_data, 1, 0);
842c6ca7616SDamien Le Moal }
843c6ca7616SDamien Le Moal
k210_register_pll_child(struct device_node * np,struct k210_sysclk * ksc,int id,enum k210_pll_id pllid,unsigned long flags)844c6ca7616SDamien Le Moal static inline void __init k210_register_pll_child(struct device_node *np,
845c6ca7616SDamien Le Moal struct k210_sysclk *ksc, int id,
846c6ca7616SDamien Le Moal enum k210_pll_id pllid,
847c6ca7616SDamien Le Moal unsigned long flags)
848c6ca7616SDamien Le Moal {
849c6ca7616SDamien Le Moal const struct clk_parent_data parent_data = {
850c6ca7616SDamien Le Moal .hw = &ksc->plls[pllid].hw,
851c6ca7616SDamien Le Moal };
852c6ca7616SDamien Le Moal
853c6ca7616SDamien Le Moal k210_register_clk(np, ksc, id, &parent_data, 1, flags);
854c6ca7616SDamien Le Moal }
855c6ca7616SDamien Le Moal
k210_register_aclk_child(struct device_node * np,struct k210_sysclk * ksc,int id,unsigned long flags)856c6ca7616SDamien Le Moal static inline void __init k210_register_aclk_child(struct device_node *np,
857c6ca7616SDamien Le Moal struct k210_sysclk *ksc, int id,
858c6ca7616SDamien Le Moal unsigned long flags)
859c6ca7616SDamien Le Moal {
860c6ca7616SDamien Le Moal const struct clk_parent_data parent_data = {
861c6ca7616SDamien Le Moal .hw = &ksc->aclk,
862c6ca7616SDamien Le Moal };
863c6ca7616SDamien Le Moal
864c6ca7616SDamien Le Moal k210_register_clk(np, ksc, id, &parent_data, 1, flags);
865c6ca7616SDamien Le Moal }
866c6ca7616SDamien Le Moal
k210_register_clk_child(struct device_node * np,struct k210_sysclk * ksc,int id,int parent_id)867c6ca7616SDamien Le Moal static inline void __init k210_register_clk_child(struct device_node *np,
868c6ca7616SDamien Le Moal struct k210_sysclk *ksc, int id,
869c6ca7616SDamien Le Moal int parent_id)
870c6ca7616SDamien Le Moal {
871c6ca7616SDamien Le Moal const struct clk_parent_data parent_data = {
872c6ca7616SDamien Le Moal .hw = &ksc->clks[parent_id].hw,
873c6ca7616SDamien Le Moal };
874c6ca7616SDamien Le Moal
875c6ca7616SDamien Le Moal k210_register_clk(np, ksc, id, &parent_data, 1, 0);
876c6ca7616SDamien Le Moal }
877c6ca7616SDamien Le Moal
k210_clk_hw_onecell_get(struct of_phandle_args * clkspec,void * data)878c6ca7616SDamien Le Moal static struct clk_hw *k210_clk_hw_onecell_get(struct of_phandle_args *clkspec,
879c6ca7616SDamien Le Moal void *data)
880c6ca7616SDamien Le Moal {
881c6ca7616SDamien Le Moal struct k210_sysclk *ksc = data;
882c6ca7616SDamien Le Moal unsigned int idx = clkspec->args[0];
883c6ca7616SDamien Le Moal
884c6ca7616SDamien Le Moal if (idx >= K210_NUM_CLKS)
885c6ca7616SDamien Le Moal return ERR_PTR(-EINVAL);
886c6ca7616SDamien Le Moal
887c6ca7616SDamien Le Moal return &ksc->clks[idx].hw;
888c6ca7616SDamien Le Moal }
889c6ca7616SDamien Le Moal
k210_clk_init(struct device_node * np)890c6ca7616SDamien Le Moal static void __init k210_clk_init(struct device_node *np)
891c6ca7616SDamien Le Moal {
892c6ca7616SDamien Le Moal struct device_node *sysctl_np;
893c6ca7616SDamien Le Moal struct k210_sysclk *ksc;
894c6ca7616SDamien Le Moal int i, ret;
895c6ca7616SDamien Le Moal
896c6ca7616SDamien Le Moal ksc = kzalloc(sizeof(*ksc), GFP_KERNEL);
897c6ca7616SDamien Le Moal if (!ksc)
898c6ca7616SDamien Le Moal return;
899c6ca7616SDamien Le Moal
900c6ca7616SDamien Le Moal spin_lock_init(&ksc->clk_lock);
901c6ca7616SDamien Le Moal sysctl_np = of_get_parent(np);
902c6ca7616SDamien Le Moal ksc->regs = of_iomap(sysctl_np, 0);
903c6ca7616SDamien Le Moal of_node_put(sysctl_np);
904c6ca7616SDamien Le Moal if (!ksc->regs) {
905c6ca7616SDamien Le Moal pr_err("%pOFP: failed to map registers\n", np);
906c6ca7616SDamien Le Moal return;
907c6ca7616SDamien Le Moal }
908c6ca7616SDamien Le Moal
909c6ca7616SDamien Le Moal ret = k210_register_plls(np, ksc);
910c6ca7616SDamien Le Moal if (ret)
911c6ca7616SDamien Le Moal return;
912c6ca7616SDamien Le Moal
913c6ca7616SDamien Le Moal ret = k210_register_aclk(np, ksc);
914c6ca7616SDamien Le Moal if (ret)
915c6ca7616SDamien Le Moal return;
916c6ca7616SDamien Le Moal
917c6ca7616SDamien Le Moal /*
918c6ca7616SDamien Le Moal * Critical clocks: there are no consumers of the SRAM clocks,
919c6ca7616SDamien Le Moal * including the AI clock for the third SRAM bank. The CPU clock
920c6ca7616SDamien Le Moal * is only referenced by the uarths serial device and so would be
921c6ca7616SDamien Le Moal * disabled if the serial console is disabled to switch to another
922c6ca7616SDamien Le Moal * console. Mark all these clocks as critical so that they are never
923c6ca7616SDamien Le Moal * disabled by the core clock management.
924c6ca7616SDamien Le Moal */
925c6ca7616SDamien Le Moal k210_register_aclk_child(np, ksc, K210_CLK_CPU, CLK_IS_CRITICAL);
926c6ca7616SDamien Le Moal k210_register_aclk_child(np, ksc, K210_CLK_SRAM0, CLK_IS_CRITICAL);
927c6ca7616SDamien Le Moal k210_register_aclk_child(np, ksc, K210_CLK_SRAM1, CLK_IS_CRITICAL);
928c6ca7616SDamien Le Moal k210_register_pll_child(np, ksc, K210_CLK_AI, K210_PLL1,
929c6ca7616SDamien Le Moal CLK_IS_CRITICAL);
930c6ca7616SDamien Le Moal
931c6ca7616SDamien Le Moal /* Clocks with aclk as source */
932c6ca7616SDamien Le Moal k210_register_aclk_child(np, ksc, K210_CLK_DMA, 0);
933c6ca7616SDamien Le Moal k210_register_aclk_child(np, ksc, K210_CLK_FFT, 0);
934c6ca7616SDamien Le Moal k210_register_aclk_child(np, ksc, K210_CLK_ROM, 0);
935c6ca7616SDamien Le Moal k210_register_aclk_child(np, ksc, K210_CLK_DVP, 0);
936c6ca7616SDamien Le Moal k210_register_aclk_child(np, ksc, K210_CLK_APB0, 0);
937c6ca7616SDamien Le Moal k210_register_aclk_child(np, ksc, K210_CLK_APB1, 0);
938c6ca7616SDamien Le Moal k210_register_aclk_child(np, ksc, K210_CLK_APB2, 0);
939c6ca7616SDamien Le Moal
940c6ca7616SDamien Le Moal /* Clocks with PLL0 as source */
941c6ca7616SDamien Le Moal k210_register_pll_child(np, ksc, K210_CLK_SPI0, K210_PLL0, 0);
942c6ca7616SDamien Le Moal k210_register_pll_child(np, ksc, K210_CLK_SPI1, K210_PLL0, 0);
943c6ca7616SDamien Le Moal k210_register_pll_child(np, ksc, K210_CLK_SPI2, K210_PLL0, 0);
944c6ca7616SDamien Le Moal k210_register_pll_child(np, ksc, K210_CLK_I2C0, K210_PLL0, 0);
945c6ca7616SDamien Le Moal k210_register_pll_child(np, ksc, K210_CLK_I2C1, K210_PLL0, 0);
946c6ca7616SDamien Le Moal k210_register_pll_child(np, ksc, K210_CLK_I2C2, K210_PLL0, 0);
947c6ca7616SDamien Le Moal
948c6ca7616SDamien Le Moal /* Clocks with PLL2 as source */
949c6ca7616SDamien Le Moal k210_register_pll_child(np, ksc, K210_CLK_I2S0, K210_PLL2, 0);
950c6ca7616SDamien Le Moal k210_register_pll_child(np, ksc, K210_CLK_I2S1, K210_PLL2, 0);
951c6ca7616SDamien Le Moal k210_register_pll_child(np, ksc, K210_CLK_I2S2, K210_PLL2, 0);
952c6ca7616SDamien Le Moal k210_register_pll_child(np, ksc, K210_CLK_I2S0_M, K210_PLL2, 0);
953c6ca7616SDamien Le Moal k210_register_pll_child(np, ksc, K210_CLK_I2S1_M, K210_PLL2, 0);
954c6ca7616SDamien Le Moal k210_register_pll_child(np, ksc, K210_CLK_I2S2_M, K210_PLL2, 0);
955c6ca7616SDamien Le Moal
956c6ca7616SDamien Le Moal /* Clocks with IN0 as source */
957c6ca7616SDamien Le Moal k210_register_in0_child(np, ksc, K210_CLK_WDT0);
958c6ca7616SDamien Le Moal k210_register_in0_child(np, ksc, K210_CLK_WDT1);
959c6ca7616SDamien Le Moal k210_register_in0_child(np, ksc, K210_CLK_RTC);
960c6ca7616SDamien Le Moal
961c6ca7616SDamien Le Moal /* Clocks with APB0 as source */
962c6ca7616SDamien Le Moal k210_register_clk_child(np, ksc, K210_CLK_GPIO, K210_CLK_APB0);
963c6ca7616SDamien Le Moal k210_register_clk_child(np, ksc, K210_CLK_UART1, K210_CLK_APB0);
964c6ca7616SDamien Le Moal k210_register_clk_child(np, ksc, K210_CLK_UART2, K210_CLK_APB0);
965c6ca7616SDamien Le Moal k210_register_clk_child(np, ksc, K210_CLK_UART3, K210_CLK_APB0);
966c6ca7616SDamien Le Moal k210_register_clk_child(np, ksc, K210_CLK_FPIOA, K210_CLK_APB0);
967c6ca7616SDamien Le Moal k210_register_clk_child(np, ksc, K210_CLK_SHA, K210_CLK_APB0);
968c6ca7616SDamien Le Moal
969c6ca7616SDamien Le Moal /* Clocks with APB1 as source */
970c6ca7616SDamien Le Moal k210_register_clk_child(np, ksc, K210_CLK_AES, K210_CLK_APB1);
971c6ca7616SDamien Le Moal k210_register_clk_child(np, ksc, K210_CLK_OTP, K210_CLK_APB1);
972c6ca7616SDamien Le Moal
973c6ca7616SDamien Le Moal /* Mux clocks with in0 or pll0 as source */
974c6ca7616SDamien Le Moal k210_register_mux_clk(np, ksc, K210_CLK_SPI3);
975c6ca7616SDamien Le Moal k210_register_mux_clk(np, ksc, K210_CLK_TIMER0);
976c6ca7616SDamien Le Moal k210_register_mux_clk(np, ksc, K210_CLK_TIMER1);
977c6ca7616SDamien Le Moal k210_register_mux_clk(np, ksc, K210_CLK_TIMER2);
978c6ca7616SDamien Le Moal
979c6ca7616SDamien Le Moal /* Check for registration errors */
980c6ca7616SDamien Le Moal for (i = 0; i < K210_NUM_CLKS; i++) {
981c6ca7616SDamien Le Moal if (ksc->clks[i].id != i)
982c6ca7616SDamien Le Moal return;
983c6ca7616SDamien Le Moal }
984c6ca7616SDamien Le Moal
985c6ca7616SDamien Le Moal ret = of_clk_add_hw_provider(np, k210_clk_hw_onecell_get, ksc);
986c6ca7616SDamien Le Moal if (ret) {
987c6ca7616SDamien Le Moal pr_err("%pOFP: add clock provider failed %d\n", np, ret);
988c6ca7616SDamien Le Moal return;
989c6ca7616SDamien Le Moal }
990c6ca7616SDamien Le Moal
991c6ca7616SDamien Le Moal pr_info("%pOFP: CPU running at %lu MHz\n",
992c6ca7616SDamien Le Moal np, clk_hw_get_rate(&ksc->clks[K210_CLK_CPU].hw) / 1000000);
993c6ca7616SDamien Le Moal }
994c6ca7616SDamien Le Moal
995c6ca7616SDamien Le Moal CLK_OF_DECLARE(k210_clk, "canaan,k210-clk", k210_clk_init);
996c6ca7616SDamien Le Moal
997c6ca7616SDamien Le Moal /*
998c6ca7616SDamien Le Moal * Enable PLL1 to be able to use the AI SRAM.
999c6ca7616SDamien Le Moal */
k210_clk_early_init(void __iomem * regs)1000c6ca7616SDamien Le Moal void __init k210_clk_early_init(void __iomem *regs)
1001c6ca7616SDamien Le Moal {
1002c6ca7616SDamien Le Moal struct k210_pll pll1;
1003c6ca7616SDamien Le Moal
1004c6ca7616SDamien Le Moal /* Make sure ACLK selector is set to PLL0 */
1005c6ca7616SDamien Le Moal k210_aclk_set_selector(regs, 1);
1006c6ca7616SDamien Le Moal
1007c6ca7616SDamien Le Moal /* Startup PLL1 to enable the aisram bank for general memory use */
1008c6ca7616SDamien Le Moal k210_init_pll(regs, K210_PLL1, &pll1);
1009c6ca7616SDamien Le Moal k210_pll_enable_hw(regs, &pll1);
1010c6ca7616SDamien Le Moal }
1011