xref: /openbmc/linux/drivers/clk/ingenic/jz4740-cgu.c (revision 41dd641e9a1a7229383f9b342a57cb6720e7ea46)
1ff1930c6SPaul Burton /*
2ff1930c6SPaul Burton  * Ingenic JZ4740 SoC CGU driver
3ff1930c6SPaul Burton  *
4ff1930c6SPaul Burton  * Copyright (c) 2015 Imagination Technologies
5ff1930c6SPaul Burton  * Author: Paul Burton <paul.burton@imgtec.com>
6ff1930c6SPaul Burton  *
7ff1930c6SPaul Burton  * This program is free software; you can redistribute it and/or
8ff1930c6SPaul Burton  * modify it under the terms of the GNU General Public License as
9ff1930c6SPaul Burton  * published by the Free Software Foundation; either version 2 of
10ff1930c6SPaul Burton  * the License, or (at your option) any later version.
11ff1930c6SPaul Burton  *
12ff1930c6SPaul Burton  * This program is distributed in the hope that it will be useful,
13ff1930c6SPaul Burton  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14ff1930c6SPaul Burton  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15ff1930c6SPaul Burton  * GNU General Public License for more details.
16ff1930c6SPaul Burton  */
17ff1930c6SPaul Burton 
18ff1930c6SPaul Burton #include <linux/clk-provider.h>
19ff1930c6SPaul Burton #include <linux/delay.h>
20ff1930c6SPaul Burton #include <linux/of.h>
21ff1930c6SPaul Burton #include <dt-bindings/clock/jz4740-cgu.h>
22*41dd641eSPaul Burton #include <asm/mach-jz4740/clock.h>
23ff1930c6SPaul Burton #include "cgu.h"
24ff1930c6SPaul Burton 
25ff1930c6SPaul Burton /* CGU register offsets */
26ff1930c6SPaul Burton #define CGU_REG_CPCCR		0x00
27*41dd641eSPaul Burton #define CGU_REG_LCR		0x04
28ff1930c6SPaul Burton #define CGU_REG_CPPCR		0x10
29ff1930c6SPaul Burton #define CGU_REG_SCR		0x24
30ff1930c6SPaul Burton #define CGU_REG_I2SCDR		0x60
31ff1930c6SPaul Burton #define CGU_REG_LPCDR		0x64
32ff1930c6SPaul Burton #define CGU_REG_MSCCDR		0x68
33ff1930c6SPaul Burton #define CGU_REG_UHCCDR		0x6c
34ff1930c6SPaul Burton #define CGU_REG_SSICDR		0x74
35ff1930c6SPaul Burton 
36ff1930c6SPaul Burton /* bits within a PLL control register */
37ff1930c6SPaul Burton #define PLLCTL_M_SHIFT		23
38ff1930c6SPaul Burton #define PLLCTL_M_MASK		(0x1ff << PLLCTL_M_SHIFT)
39ff1930c6SPaul Burton #define PLLCTL_N_SHIFT		18
40ff1930c6SPaul Burton #define PLLCTL_N_MASK		(0x1f << PLLCTL_N_SHIFT)
41ff1930c6SPaul Burton #define PLLCTL_OD_SHIFT		16
42ff1930c6SPaul Burton #define PLLCTL_OD_MASK		(0x3 << PLLCTL_OD_SHIFT)
43ff1930c6SPaul Burton #define PLLCTL_STABLE		(1 << 10)
44ff1930c6SPaul Burton #define PLLCTL_BYPASS		(1 << 9)
45ff1930c6SPaul Burton #define PLLCTL_ENABLE		(1 << 8)
46ff1930c6SPaul Burton 
47*41dd641eSPaul Burton /* bits within the LCR register */
48*41dd641eSPaul Burton #define LCR_SLEEP		(1 << 0)
49*41dd641eSPaul Burton 
50ff1930c6SPaul Burton static struct ingenic_cgu *cgu;
51ff1930c6SPaul Burton 
52ff1930c6SPaul Burton static const s8 pll_od_encoding[4] = {
53ff1930c6SPaul Burton 	0x0, 0x1, -1, 0x3,
54ff1930c6SPaul Burton };
55ff1930c6SPaul Burton 
56ff1930c6SPaul Burton static const struct ingenic_cgu_clk_info jz4740_cgu_clocks[] = {
57ff1930c6SPaul Burton 
58ff1930c6SPaul Burton 	/* External clocks */
59ff1930c6SPaul Burton 
60ff1930c6SPaul Burton 	[JZ4740_CLK_EXT] = { "ext", CGU_CLK_EXT },
61ff1930c6SPaul Burton 	[JZ4740_CLK_RTC] = { "rtc", CGU_CLK_EXT },
62ff1930c6SPaul Burton 
63ff1930c6SPaul Burton 	[JZ4740_CLK_PLL] = {
64ff1930c6SPaul Burton 		"pll", CGU_CLK_PLL,
65ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
66ff1930c6SPaul Burton 		.pll = {
67ff1930c6SPaul Burton 			.reg = CGU_REG_CPPCR,
68ff1930c6SPaul Burton 			.m_shift = 23,
69ff1930c6SPaul Burton 			.m_bits = 9,
70ff1930c6SPaul Burton 			.m_offset = 2,
71ff1930c6SPaul Burton 			.n_shift = 18,
72ff1930c6SPaul Burton 			.n_bits = 5,
73ff1930c6SPaul Burton 			.n_offset = 2,
74ff1930c6SPaul Burton 			.od_shift = 16,
75ff1930c6SPaul Burton 			.od_bits = 2,
76ff1930c6SPaul Burton 			.od_max = 4,
77ff1930c6SPaul Burton 			.od_encoding = pll_od_encoding,
78ff1930c6SPaul Burton 			.stable_bit = 10,
79ff1930c6SPaul Burton 			.bypass_bit = 9,
80ff1930c6SPaul Burton 			.enable_bit = 8,
81ff1930c6SPaul Burton 		},
82ff1930c6SPaul Burton 	},
83ff1930c6SPaul Burton 
84ff1930c6SPaul Burton 	/* Muxes & dividers */
85ff1930c6SPaul Burton 
86ff1930c6SPaul Burton 	[JZ4740_CLK_PLL_HALF] = {
87ff1930c6SPaul Burton 		"pll half", CGU_CLK_DIV,
88ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
89ff1930c6SPaul Burton 		.div = { CGU_REG_CPCCR, 21, 1, -1, -1, -1 },
90ff1930c6SPaul Burton 	},
91ff1930c6SPaul Burton 
92ff1930c6SPaul Burton 	[JZ4740_CLK_CCLK] = {
93ff1930c6SPaul Burton 		"cclk", CGU_CLK_DIV,
94ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
95ff1930c6SPaul Burton 		.div = { CGU_REG_CPCCR, 0, 4, 22, -1, -1 },
96ff1930c6SPaul Burton 	},
97ff1930c6SPaul Burton 
98ff1930c6SPaul Burton 	[JZ4740_CLK_HCLK] = {
99ff1930c6SPaul Burton 		"hclk", CGU_CLK_DIV,
100ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
101ff1930c6SPaul Burton 		.div = { CGU_REG_CPCCR, 4, 4, 22, -1, -1 },
102ff1930c6SPaul Burton 	},
103ff1930c6SPaul Burton 
104ff1930c6SPaul Burton 	[JZ4740_CLK_PCLK] = {
105ff1930c6SPaul Burton 		"pclk", CGU_CLK_DIV,
106ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
107ff1930c6SPaul Burton 		.div = { CGU_REG_CPCCR, 8, 4, 22, -1, -1 },
108ff1930c6SPaul Burton 	},
109ff1930c6SPaul Burton 
110ff1930c6SPaul Burton 	[JZ4740_CLK_MCLK] = {
111ff1930c6SPaul Burton 		"mclk", CGU_CLK_DIV,
112ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
113ff1930c6SPaul Burton 		.div = { CGU_REG_CPCCR, 12, 4, 22, -1, -1 },
114ff1930c6SPaul Burton 	},
115ff1930c6SPaul Burton 
116ff1930c6SPaul Burton 	[JZ4740_CLK_LCD] = {
117ff1930c6SPaul Burton 		"lcd", CGU_CLK_DIV | CGU_CLK_GATE,
118ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 },
119ff1930c6SPaul Burton 		.div = { CGU_REG_CPCCR, 16, 5, 22, -1, -1 },
120ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 10 },
121ff1930c6SPaul Burton 	},
122ff1930c6SPaul Burton 
123ff1930c6SPaul Burton 	[JZ4740_CLK_LCD_PCLK] = {
124ff1930c6SPaul Burton 		"lcd_pclk", CGU_CLK_DIV,
125ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 },
126ff1930c6SPaul Burton 		.div = { CGU_REG_LPCDR, 0, 11, -1, -1, -1 },
127ff1930c6SPaul Burton 	},
128ff1930c6SPaul Burton 
129ff1930c6SPaul Burton 	[JZ4740_CLK_I2S] = {
130ff1930c6SPaul Burton 		"i2s", CGU_CLK_MUX | CGU_CLK_DIV | CGU_CLK_GATE,
131ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL_HALF, -1, -1 },
132ff1930c6SPaul Burton 		.mux = { CGU_REG_CPCCR, 31, 1 },
133ff1930c6SPaul Burton 		.div = { CGU_REG_I2SCDR, 0, 8, -1, -1, -1 },
134ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 6 },
135ff1930c6SPaul Burton 	},
136ff1930c6SPaul Burton 
137ff1930c6SPaul Burton 	[JZ4740_CLK_SPI] = {
138ff1930c6SPaul Burton 		"spi", CGU_CLK_MUX | CGU_CLK_DIV | CGU_CLK_GATE,
139ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL, -1, -1 },
140ff1930c6SPaul Burton 		.mux = { CGU_REG_SSICDR, 31, 1 },
141ff1930c6SPaul Burton 		.div = { CGU_REG_SSICDR, 0, 4, -1, -1, -1 },
142ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 4 },
143ff1930c6SPaul Burton 	},
144ff1930c6SPaul Burton 
145ff1930c6SPaul Burton 	[JZ4740_CLK_MMC] = {
146ff1930c6SPaul Burton 		"mmc", CGU_CLK_DIV | CGU_CLK_GATE,
147ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 },
148ff1930c6SPaul Burton 		.div = { CGU_REG_MSCCDR, 0, 5, -1, -1, -1 },
149ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 7 },
150ff1930c6SPaul Burton 	},
151ff1930c6SPaul Burton 
152ff1930c6SPaul Burton 	[JZ4740_CLK_UHC] = {
153ff1930c6SPaul Burton 		"uhc", CGU_CLK_DIV | CGU_CLK_GATE,
154ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 },
155ff1930c6SPaul Burton 		.div = { CGU_REG_UHCCDR, 0, 4, -1, -1, -1 },
156ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 14 },
157ff1930c6SPaul Burton 	},
158ff1930c6SPaul Burton 
159ff1930c6SPaul Burton 	[JZ4740_CLK_UDC] = {
160ff1930c6SPaul Burton 		"udc", CGU_CLK_MUX | CGU_CLK_DIV,
161ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL_HALF, -1, -1 },
162ff1930c6SPaul Burton 		.mux = { CGU_REG_CPCCR, 29, 1 },
163ff1930c6SPaul Burton 		.div = { CGU_REG_CPCCR, 23, 6, -1, -1, -1 },
164ff1930c6SPaul Burton 		.gate = { CGU_REG_SCR, 6 },
165ff1930c6SPaul Burton 	},
166ff1930c6SPaul Burton 
167ff1930c6SPaul Burton 	/* Gate-only clocks */
168ff1930c6SPaul Burton 
169ff1930c6SPaul Burton 	[JZ4740_CLK_UART0] = {
170ff1930c6SPaul Burton 		"uart0", CGU_CLK_GATE,
171ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
172ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 0 },
173ff1930c6SPaul Burton 	},
174ff1930c6SPaul Burton 
175ff1930c6SPaul Burton 	[JZ4740_CLK_UART1] = {
176ff1930c6SPaul Burton 		"uart1", CGU_CLK_GATE,
177ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
178ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 15 },
179ff1930c6SPaul Burton 	},
180ff1930c6SPaul Burton 
181ff1930c6SPaul Burton 	[JZ4740_CLK_DMA] = {
182ff1930c6SPaul Burton 		"dma", CGU_CLK_GATE,
183ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PCLK, -1, -1, -1 },
184ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 12 },
185ff1930c6SPaul Burton 	},
186ff1930c6SPaul Burton 
187ff1930c6SPaul Burton 	[JZ4740_CLK_IPU] = {
188ff1930c6SPaul Burton 		"ipu", CGU_CLK_GATE,
189ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PCLK, -1, -1, -1 },
190ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 13 },
191ff1930c6SPaul Burton 	},
192ff1930c6SPaul Burton 
193ff1930c6SPaul Burton 	[JZ4740_CLK_ADC] = {
194ff1930c6SPaul Burton 		"adc", CGU_CLK_GATE,
195ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
196ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 8 },
197ff1930c6SPaul Burton 	},
198ff1930c6SPaul Burton 
199ff1930c6SPaul Burton 	[JZ4740_CLK_I2C] = {
200ff1930c6SPaul Burton 		"i2c", CGU_CLK_GATE,
201ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
202ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 3 },
203ff1930c6SPaul Burton 	},
204ff1930c6SPaul Burton 
205ff1930c6SPaul Burton 	[JZ4740_CLK_AIC] = {
206ff1930c6SPaul Burton 		"aic", CGU_CLK_GATE,
207ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
208ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 5 },
209ff1930c6SPaul Burton 	},
210ff1930c6SPaul Burton };
211ff1930c6SPaul Burton 
212ff1930c6SPaul Burton static void __init jz4740_cgu_init(struct device_node *np)
213ff1930c6SPaul Burton {
214ff1930c6SPaul Burton 	int retval;
215ff1930c6SPaul Burton 
216ff1930c6SPaul Burton 	cgu = ingenic_cgu_new(jz4740_cgu_clocks,
217ff1930c6SPaul Burton 			      ARRAY_SIZE(jz4740_cgu_clocks), np);
218ff1930c6SPaul Burton 	if (!cgu) {
219ff1930c6SPaul Burton 		pr_err("%s: failed to initialise CGU\n", __func__);
220ff1930c6SPaul Burton 		return;
221ff1930c6SPaul Burton 	}
222ff1930c6SPaul Burton 
223ff1930c6SPaul Burton 	retval = ingenic_cgu_register_clocks(cgu);
224ff1930c6SPaul Burton 	if (retval)
225ff1930c6SPaul Burton 		pr_err("%s: failed to register CGU Clocks\n", __func__);
226ff1930c6SPaul Burton }
227ff1930c6SPaul Burton CLK_OF_DECLARE(jz4740_cgu, "ingenic,jz4740-cgu", jz4740_cgu_init);
228*41dd641eSPaul Burton 
229*41dd641eSPaul Burton void jz4740_clock_set_wait_mode(enum jz4740_wait_mode mode)
230*41dd641eSPaul Burton {
231*41dd641eSPaul Burton 	uint32_t lcr = readl(cgu->base + CGU_REG_LCR);
232*41dd641eSPaul Burton 
233*41dd641eSPaul Burton 	switch (mode) {
234*41dd641eSPaul Burton 	case JZ4740_WAIT_MODE_IDLE:
235*41dd641eSPaul Burton 		lcr &= ~LCR_SLEEP;
236*41dd641eSPaul Burton 		break;
237*41dd641eSPaul Burton 
238*41dd641eSPaul Burton 	case JZ4740_WAIT_MODE_SLEEP:
239*41dd641eSPaul Burton 		lcr |= LCR_SLEEP;
240*41dd641eSPaul Burton 		break;
241*41dd641eSPaul Burton 	}
242*41dd641eSPaul Burton 
243*41dd641eSPaul Burton 	writel(lcr, cgu->base + CGU_REG_LCR);
244*41dd641eSPaul Burton }
245