xref: /openbmc/linux/drivers/clk/ingenic/jz4740-cgu.c (revision 03d570e1a4dc669457af2888999ecc9548fc0d2a)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ff1930c6SPaul Burton /*
3ff1930c6SPaul Burton  * Ingenic JZ4740 SoC CGU driver
4ff1930c6SPaul Burton  *
5ff1930c6SPaul Burton  * Copyright (c) 2015 Imagination Technologies
6fb615d61SPaul Burton  * Author: Paul Burton <paul.burton@mips.com>
7ff1930c6SPaul Burton  */
8ff1930c6SPaul Burton 
9ff1930c6SPaul Burton #include <linux/clk-provider.h>
10ff1930c6SPaul Burton #include <linux/delay.h>
1162e59c4eSStephen Boyd #include <linux/io.h>
12ff1930c6SPaul Burton #include <linux/of.h>
13ff1930c6SPaul Burton #include <dt-bindings/clock/jz4740-cgu.h>
14ff1930c6SPaul Burton #include "cgu.h"
152ee93e3cSPaul Cercueil #include "pm.h"
16ff1930c6SPaul Burton 
17ff1930c6SPaul Burton /* CGU register offsets */
18ff1930c6SPaul Burton #define CGU_REG_CPCCR		0x00
1941dd641eSPaul Burton #define CGU_REG_LCR		0x04
20ff1930c6SPaul Burton #define CGU_REG_CPPCR		0x10
21ed286ca5SPaul Burton #define CGU_REG_CLKGR		0x20
22ff1930c6SPaul Burton #define CGU_REG_SCR		0x24
23ff1930c6SPaul Burton #define CGU_REG_I2SCDR		0x60
24ff1930c6SPaul Burton #define CGU_REG_LPCDR		0x64
25ff1930c6SPaul Burton #define CGU_REG_MSCCDR		0x68
26ff1930c6SPaul Burton #define CGU_REG_UHCCDR		0x6c
27ff1930c6SPaul Burton #define CGU_REG_SSICDR		0x74
28ff1930c6SPaul Burton 
29ff1930c6SPaul Burton /* bits within a PLL control register */
30ff1930c6SPaul Burton #define PLLCTL_M_SHIFT		23
31ff1930c6SPaul Burton #define PLLCTL_M_MASK		(0x1ff << PLLCTL_M_SHIFT)
32ff1930c6SPaul Burton #define PLLCTL_N_SHIFT		18
33ff1930c6SPaul Burton #define PLLCTL_N_MASK		(0x1f << PLLCTL_N_SHIFT)
34ff1930c6SPaul Burton #define PLLCTL_OD_SHIFT		16
35ff1930c6SPaul Burton #define PLLCTL_OD_MASK		(0x3 << PLLCTL_OD_SHIFT)
36ff1930c6SPaul Burton #define PLLCTL_STABLE		(1 << 10)
37ff1930c6SPaul Burton #define PLLCTL_BYPASS		(1 << 9)
38ff1930c6SPaul Burton #define PLLCTL_ENABLE		(1 << 8)
39ff1930c6SPaul Burton 
4041dd641eSPaul Burton /* bits within the LCR register */
4141dd641eSPaul Burton #define LCR_SLEEP		(1 << 0)
4241dd641eSPaul Burton 
43ed286ca5SPaul Burton /* bits within the CLKGR register */
44ed286ca5SPaul Burton #define CLKGR_UDC		(1 << 11)
45ed286ca5SPaul Burton 
46ff1930c6SPaul Burton static struct ingenic_cgu *cgu;
47ff1930c6SPaul Burton 
48ff1930c6SPaul Burton static const s8 pll_od_encoding[4] = {
49ff1930c6SPaul Burton 	0x0, 0x1, -1, 0x3,
50ff1930c6SPaul Burton };
51ff1930c6SPaul Burton 
522a1a7036SPaul Cercueil static const u8 jz4740_cgu_cpccr_div_table[] = {
532a1a7036SPaul Cercueil 	1, 2, 3, 4, 6, 8, 12, 16, 24, 32,
542a1a7036SPaul Cercueil };
552a1a7036SPaul Cercueil 
56568b9de4SPaul Cercueil static const u8 jz4740_cgu_pll_half_div_table[] = {
57568b9de4SPaul Cercueil 	2, 1,
58568b9de4SPaul Cercueil };
59568b9de4SPaul Cercueil 
60ff1930c6SPaul Burton static const struct ingenic_cgu_clk_info jz4740_cgu_clocks[] = {
61ff1930c6SPaul Burton 
62ff1930c6SPaul Burton 	/* External clocks */
63ff1930c6SPaul Burton 
64ff1930c6SPaul Burton 	[JZ4740_CLK_EXT] = { "ext", CGU_CLK_EXT },
65ff1930c6SPaul Burton 	[JZ4740_CLK_RTC] = { "rtc", CGU_CLK_EXT },
66ff1930c6SPaul Burton 
67ff1930c6SPaul Burton 	[JZ4740_CLK_PLL] = {
68ff1930c6SPaul Burton 		"pll", CGU_CLK_PLL,
69ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
70ff1930c6SPaul Burton 		.pll = {
71ff1930c6SPaul Burton 			.reg = CGU_REG_CPPCR,
72ff1930c6SPaul Burton 			.m_shift = 23,
73ff1930c6SPaul Burton 			.m_bits = 9,
74ff1930c6SPaul Burton 			.m_offset = 2,
75ff1930c6SPaul Burton 			.n_shift = 18,
76ff1930c6SPaul Burton 			.n_bits = 5,
77ff1930c6SPaul Burton 			.n_offset = 2,
78ff1930c6SPaul Burton 			.od_shift = 16,
79ff1930c6SPaul Burton 			.od_bits = 2,
80ff1930c6SPaul Burton 			.od_max = 4,
81ff1930c6SPaul Burton 			.od_encoding = pll_od_encoding,
82ff1930c6SPaul Burton 			.stable_bit = 10,
83ff1930c6SPaul Burton 			.bypass_bit = 9,
84ff1930c6SPaul Burton 			.enable_bit = 8,
85ff1930c6SPaul Burton 		},
86ff1930c6SPaul Burton 	},
87ff1930c6SPaul Burton 
88ff1930c6SPaul Burton 	/* Muxes & dividers */
89ff1930c6SPaul Burton 
90ff1930c6SPaul Burton 	[JZ4740_CLK_PLL_HALF] = {
91ff1930c6SPaul Burton 		"pll half", CGU_CLK_DIV,
92ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
93568b9de4SPaul Cercueil 		.div = {
94568b9de4SPaul Cercueil 			CGU_REG_CPCCR, 21, 1, 1, -1, -1, -1,
95568b9de4SPaul Cercueil 			jz4740_cgu_pll_half_div_table,
96568b9de4SPaul Cercueil 		},
97ff1930c6SPaul Burton 	},
98ff1930c6SPaul Burton 
99ff1930c6SPaul Burton 	[JZ4740_CLK_CCLK] = {
100ff1930c6SPaul Burton 		"cclk", CGU_CLK_DIV,
101ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
1022a1a7036SPaul Cercueil 		.div = {
1032a1a7036SPaul Cercueil 			CGU_REG_CPCCR, 0, 1, 4, 22, -1, -1,
1042a1a7036SPaul Cercueil 			jz4740_cgu_cpccr_div_table,
1052a1a7036SPaul Cercueil 		},
106ff1930c6SPaul Burton 	},
107ff1930c6SPaul Burton 
108ff1930c6SPaul Burton 	[JZ4740_CLK_HCLK] = {
109ff1930c6SPaul Burton 		"hclk", CGU_CLK_DIV,
110ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
1112a1a7036SPaul Cercueil 		.div = {
1122a1a7036SPaul Cercueil 			CGU_REG_CPCCR, 4, 1, 4, 22, -1, -1,
1132a1a7036SPaul Cercueil 			jz4740_cgu_cpccr_div_table,
1142a1a7036SPaul Cercueil 		},
115ff1930c6SPaul Burton 	},
116ff1930c6SPaul Burton 
117ff1930c6SPaul Burton 	[JZ4740_CLK_PCLK] = {
118ff1930c6SPaul Burton 		"pclk", CGU_CLK_DIV,
119ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
1202a1a7036SPaul Cercueil 		.div = {
1212a1a7036SPaul Cercueil 			CGU_REG_CPCCR, 8, 1, 4, 22, -1, -1,
1222a1a7036SPaul Cercueil 			jz4740_cgu_cpccr_div_table,
1232a1a7036SPaul Cercueil 		},
124ff1930c6SPaul Burton 	},
125ff1930c6SPaul Burton 
126ff1930c6SPaul Burton 	[JZ4740_CLK_MCLK] = {
127ff1930c6SPaul Burton 		"mclk", CGU_CLK_DIV,
128ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
1292a1a7036SPaul Cercueil 		.div = {
1302a1a7036SPaul Cercueil 			CGU_REG_CPCCR, 12, 1, 4, 22, -1, -1,
1312a1a7036SPaul Cercueil 			jz4740_cgu_cpccr_div_table,
1322a1a7036SPaul Cercueil 		},
133ff1930c6SPaul Burton 	},
134ff1930c6SPaul Burton 
135ff1930c6SPaul Burton 	[JZ4740_CLK_LCD] = {
136ff1930c6SPaul Burton 		"lcd", CGU_CLK_DIV | CGU_CLK_GATE,
137ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 },
1382a1a7036SPaul Cercueil 		.div = {
1392a1a7036SPaul Cercueil 			CGU_REG_CPCCR, 16, 1, 5, 22, -1, -1,
1402a1a7036SPaul Cercueil 			jz4740_cgu_cpccr_div_table,
1412a1a7036SPaul Cercueil 		},
142ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 10 },
143ff1930c6SPaul Burton 	},
144ff1930c6SPaul Burton 
145ff1930c6SPaul Burton 	[JZ4740_CLK_LCD_PCLK] = {
146ff1930c6SPaul Burton 		"lcd_pclk", CGU_CLK_DIV,
147ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 },
1484afe2d1aSHarvey Hunt 		.div = { CGU_REG_LPCDR, 0, 1, 11, -1, -1, -1 },
149ff1930c6SPaul Burton 	},
150ff1930c6SPaul Burton 
151ff1930c6SPaul Burton 	[JZ4740_CLK_I2S] = {
152ff1930c6SPaul Burton 		"i2s", CGU_CLK_MUX | CGU_CLK_DIV | CGU_CLK_GATE,
153ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL_HALF, -1, -1 },
154ff1930c6SPaul Burton 		.mux = { CGU_REG_CPCCR, 31, 1 },
155574f4e80SPaul Cercueil 		.div = { CGU_REG_I2SCDR, 0, 1, 9, -1, -1, -1 },
156ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 6 },
157ff1930c6SPaul Burton 	},
158ff1930c6SPaul Burton 
159ff1930c6SPaul Burton 	[JZ4740_CLK_SPI] = {
160ff1930c6SPaul Burton 		"spi", CGU_CLK_MUX | CGU_CLK_DIV | CGU_CLK_GATE,
161ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL, -1, -1 },
162ff1930c6SPaul Burton 		.mux = { CGU_REG_SSICDR, 31, 1 },
1634afe2d1aSHarvey Hunt 		.div = { CGU_REG_SSICDR, 0, 1, 4, -1, -1, -1 },
164ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 4 },
165ff1930c6SPaul Burton 	},
166ff1930c6SPaul Burton 
167ff1930c6SPaul Burton 	[JZ4740_CLK_MMC] = {
168ff1930c6SPaul Burton 		"mmc", CGU_CLK_DIV | CGU_CLK_GATE,
169ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 },
1704afe2d1aSHarvey Hunt 		.div = { CGU_REG_MSCCDR, 0, 1, 5, -1, -1, -1 },
171ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 7 },
172ff1930c6SPaul Burton 	},
173ff1930c6SPaul Burton 
174ff1930c6SPaul Burton 	[JZ4740_CLK_UHC] = {
175ff1930c6SPaul Burton 		"uhc", CGU_CLK_DIV | CGU_CLK_GATE,
176ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 },
1774afe2d1aSHarvey Hunt 		.div = { CGU_REG_UHCCDR, 0, 1, 4, -1, -1, -1 },
178ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 14 },
179ff1930c6SPaul Burton 	},
180ff1930c6SPaul Burton 
181ff1930c6SPaul Burton 	[JZ4740_CLK_UDC] = {
1822b555a4bSPaul Cercueil 		"udc", CGU_CLK_MUX | CGU_CLK_DIV | CGU_CLK_GATE,
183ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL_HALF, -1, -1 },
184ff1930c6SPaul Burton 		.mux = { CGU_REG_CPCCR, 29, 1 },
1854afe2d1aSHarvey Hunt 		.div = { CGU_REG_CPCCR, 23, 1, 6, -1, -1, -1 },
186b7e29924SPaul Cercueil 		.gate = { CGU_REG_SCR, 6, true },
187ff1930c6SPaul Burton 	},
188ff1930c6SPaul Burton 
189ff1930c6SPaul Burton 	/* Gate-only clocks */
190ff1930c6SPaul Burton 
191ff1930c6SPaul Burton 	[JZ4740_CLK_UART0] = {
192ff1930c6SPaul Burton 		"uart0", CGU_CLK_GATE,
193ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
194ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 0 },
195ff1930c6SPaul Burton 	},
196ff1930c6SPaul Burton 
197ff1930c6SPaul Burton 	[JZ4740_CLK_UART1] = {
198ff1930c6SPaul Burton 		"uart1", CGU_CLK_GATE,
199ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
200ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 15 },
201ff1930c6SPaul Burton 	},
202ff1930c6SPaul Burton 
203ff1930c6SPaul Burton 	[JZ4740_CLK_DMA] = {
204ff1930c6SPaul Burton 		"dma", CGU_CLK_GATE,
205ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PCLK, -1, -1, -1 },
206ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 12 },
207ff1930c6SPaul Burton 	},
208ff1930c6SPaul Burton 
209ff1930c6SPaul Burton 	[JZ4740_CLK_IPU] = {
210ff1930c6SPaul Burton 		"ipu", CGU_CLK_GATE,
211ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PCLK, -1, -1, -1 },
212ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 13 },
213ff1930c6SPaul Burton 	},
214ff1930c6SPaul Burton 
215ff1930c6SPaul Burton 	[JZ4740_CLK_ADC] = {
216ff1930c6SPaul Burton 		"adc", CGU_CLK_GATE,
217ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
218ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 8 },
219ff1930c6SPaul Burton 	},
220ff1930c6SPaul Burton 
221ff1930c6SPaul Burton 	[JZ4740_CLK_I2C] = {
222ff1930c6SPaul Burton 		"i2c", CGU_CLK_GATE,
223ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
224ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 3 },
225ff1930c6SPaul Burton 	},
226ff1930c6SPaul Burton 
227ff1930c6SPaul Burton 	[JZ4740_CLK_AIC] = {
228ff1930c6SPaul Burton 		"aic", CGU_CLK_GATE,
229ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
230ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 5 },
231ff1930c6SPaul Burton 	},
232ff1930c6SPaul Burton };
233ff1930c6SPaul Burton 
234ff1930c6SPaul Burton static void __init jz4740_cgu_init(struct device_node *np)
235ff1930c6SPaul Burton {
236ff1930c6SPaul Burton 	int retval;
237ff1930c6SPaul Burton 
238ff1930c6SPaul Burton 	cgu = ingenic_cgu_new(jz4740_cgu_clocks,
239ff1930c6SPaul Burton 			      ARRAY_SIZE(jz4740_cgu_clocks), np);
240ff1930c6SPaul Burton 	if (!cgu) {
241ff1930c6SPaul Burton 		pr_err("%s: failed to initialise CGU\n", __func__);
242ff1930c6SPaul Burton 		return;
243ff1930c6SPaul Burton 	}
244ff1930c6SPaul Burton 
245ff1930c6SPaul Burton 	retval = ingenic_cgu_register_clocks(cgu);
246ff1930c6SPaul Burton 	if (retval)
247ff1930c6SPaul Burton 		pr_err("%s: failed to register CGU Clocks\n", __func__);
2482ee93e3cSPaul Cercueil 
2492ee93e3cSPaul Cercueil 	ingenic_cgu_register_syscore_ops(cgu);
250ff1930c6SPaul Burton }
251*03d570e1SPaul Cercueil CLK_OF_DECLARE_DRIVER(jz4740_cgu, "ingenic,jz4740-cgu", jz4740_cgu_init);
252