xref: /openbmc/linux/drivers/clk/ingenic/jz4740-cgu.c (revision 4afe2d1a6ed5cba794aeeaa816e7c97a45167b01)
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>
2241dd641eSPaul 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
2741dd641eSPaul Burton #define CGU_REG_LCR		0x04
28ff1930c6SPaul Burton #define CGU_REG_CPPCR		0x10
29ed286ca5SPaul Burton #define CGU_REG_CLKGR		0x20
30ff1930c6SPaul Burton #define CGU_REG_SCR		0x24
31ff1930c6SPaul Burton #define CGU_REG_I2SCDR		0x60
32ff1930c6SPaul Burton #define CGU_REG_LPCDR		0x64
33ff1930c6SPaul Burton #define CGU_REG_MSCCDR		0x68
34ff1930c6SPaul Burton #define CGU_REG_UHCCDR		0x6c
35ff1930c6SPaul Burton #define CGU_REG_SSICDR		0x74
36ff1930c6SPaul Burton 
37ff1930c6SPaul Burton /* bits within a PLL control register */
38ff1930c6SPaul Burton #define PLLCTL_M_SHIFT		23
39ff1930c6SPaul Burton #define PLLCTL_M_MASK		(0x1ff << PLLCTL_M_SHIFT)
40ff1930c6SPaul Burton #define PLLCTL_N_SHIFT		18
41ff1930c6SPaul Burton #define PLLCTL_N_MASK		(0x1f << PLLCTL_N_SHIFT)
42ff1930c6SPaul Burton #define PLLCTL_OD_SHIFT		16
43ff1930c6SPaul Burton #define PLLCTL_OD_MASK		(0x3 << PLLCTL_OD_SHIFT)
44ff1930c6SPaul Burton #define PLLCTL_STABLE		(1 << 10)
45ff1930c6SPaul Burton #define PLLCTL_BYPASS		(1 << 9)
46ff1930c6SPaul Burton #define PLLCTL_ENABLE		(1 << 8)
47ff1930c6SPaul Burton 
4841dd641eSPaul Burton /* bits within the LCR register */
4941dd641eSPaul Burton #define LCR_SLEEP		(1 << 0)
5041dd641eSPaul Burton 
51ed286ca5SPaul Burton /* bits within the CLKGR register */
52ed286ca5SPaul Burton #define CLKGR_UDC		(1 << 11)
53ed286ca5SPaul Burton 
54ff1930c6SPaul Burton static struct ingenic_cgu *cgu;
55ff1930c6SPaul Burton 
56ff1930c6SPaul Burton static const s8 pll_od_encoding[4] = {
57ff1930c6SPaul Burton 	0x0, 0x1, -1, 0x3,
58ff1930c6SPaul Burton };
59ff1930c6SPaul Burton 
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 },
93*4afe2d1aSHarvey Hunt 		.div = { CGU_REG_CPCCR, 21, 1, 1, -1, -1, -1 },
94ff1930c6SPaul Burton 	},
95ff1930c6SPaul Burton 
96ff1930c6SPaul Burton 	[JZ4740_CLK_CCLK] = {
97ff1930c6SPaul Burton 		"cclk", CGU_CLK_DIV,
98ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
99*4afe2d1aSHarvey Hunt 		.div = { CGU_REG_CPCCR, 0, 1, 4, 22, -1, -1 },
100ff1930c6SPaul Burton 	},
101ff1930c6SPaul Burton 
102ff1930c6SPaul Burton 	[JZ4740_CLK_HCLK] = {
103ff1930c6SPaul Burton 		"hclk", CGU_CLK_DIV,
104ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
105*4afe2d1aSHarvey Hunt 		.div = { CGU_REG_CPCCR, 4, 1, 4, 22, -1, -1 },
106ff1930c6SPaul Burton 	},
107ff1930c6SPaul Burton 
108ff1930c6SPaul Burton 	[JZ4740_CLK_PCLK] = {
109ff1930c6SPaul Burton 		"pclk", CGU_CLK_DIV,
110ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
111*4afe2d1aSHarvey Hunt 		.div = { CGU_REG_CPCCR, 8, 1, 4, 22, -1, -1 },
112ff1930c6SPaul Burton 	},
113ff1930c6SPaul Burton 
114ff1930c6SPaul Burton 	[JZ4740_CLK_MCLK] = {
115ff1930c6SPaul Burton 		"mclk", CGU_CLK_DIV,
116ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
117*4afe2d1aSHarvey Hunt 		.div = { CGU_REG_CPCCR, 12, 1, 4, 22, -1, -1 },
118ff1930c6SPaul Burton 	},
119ff1930c6SPaul Burton 
120ff1930c6SPaul Burton 	[JZ4740_CLK_LCD] = {
121ff1930c6SPaul Burton 		"lcd", CGU_CLK_DIV | CGU_CLK_GATE,
122ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 },
123*4afe2d1aSHarvey Hunt 		.div = { CGU_REG_CPCCR, 16, 1, 5, 22, -1, -1 },
124ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 10 },
125ff1930c6SPaul Burton 	},
126ff1930c6SPaul Burton 
127ff1930c6SPaul Burton 	[JZ4740_CLK_LCD_PCLK] = {
128ff1930c6SPaul Burton 		"lcd_pclk", CGU_CLK_DIV,
129ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 },
130*4afe2d1aSHarvey Hunt 		.div = { CGU_REG_LPCDR, 0, 1, 11, -1, -1, -1 },
131ff1930c6SPaul Burton 	},
132ff1930c6SPaul Burton 
133ff1930c6SPaul Burton 	[JZ4740_CLK_I2S] = {
134ff1930c6SPaul Burton 		"i2s", CGU_CLK_MUX | CGU_CLK_DIV | CGU_CLK_GATE,
135ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL_HALF, -1, -1 },
136ff1930c6SPaul Burton 		.mux = { CGU_REG_CPCCR, 31, 1 },
137*4afe2d1aSHarvey Hunt 		.div = { CGU_REG_I2SCDR, 0, 1, 8, -1, -1, -1 },
138ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 6 },
139ff1930c6SPaul Burton 	},
140ff1930c6SPaul Burton 
141ff1930c6SPaul Burton 	[JZ4740_CLK_SPI] = {
142ff1930c6SPaul Burton 		"spi", CGU_CLK_MUX | CGU_CLK_DIV | CGU_CLK_GATE,
143ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL, -1, -1 },
144ff1930c6SPaul Burton 		.mux = { CGU_REG_SSICDR, 31, 1 },
145*4afe2d1aSHarvey Hunt 		.div = { CGU_REG_SSICDR, 0, 1, 4, -1, -1, -1 },
146ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 4 },
147ff1930c6SPaul Burton 	},
148ff1930c6SPaul Burton 
149ff1930c6SPaul Burton 	[JZ4740_CLK_MMC] = {
150ff1930c6SPaul Burton 		"mmc", CGU_CLK_DIV | CGU_CLK_GATE,
151ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 },
152*4afe2d1aSHarvey Hunt 		.div = { CGU_REG_MSCCDR, 0, 1, 5, -1, -1, -1 },
153ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 7 },
154ff1930c6SPaul Burton 	},
155ff1930c6SPaul Burton 
156ff1930c6SPaul Burton 	[JZ4740_CLK_UHC] = {
157ff1930c6SPaul Burton 		"uhc", CGU_CLK_DIV | CGU_CLK_GATE,
158ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 },
159*4afe2d1aSHarvey Hunt 		.div = { CGU_REG_UHCCDR, 0, 1, 4, -1, -1, -1 },
160ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 14 },
161ff1930c6SPaul Burton 	},
162ff1930c6SPaul Burton 
163ff1930c6SPaul Burton 	[JZ4740_CLK_UDC] = {
164ff1930c6SPaul Burton 		"udc", CGU_CLK_MUX | CGU_CLK_DIV,
165ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL_HALF, -1, -1 },
166ff1930c6SPaul Burton 		.mux = { CGU_REG_CPCCR, 29, 1 },
167*4afe2d1aSHarvey Hunt 		.div = { CGU_REG_CPCCR, 23, 1, 6, -1, -1, -1 },
168ff1930c6SPaul Burton 		.gate = { CGU_REG_SCR, 6 },
169ff1930c6SPaul Burton 	},
170ff1930c6SPaul Burton 
171ff1930c6SPaul Burton 	/* Gate-only clocks */
172ff1930c6SPaul Burton 
173ff1930c6SPaul Burton 	[JZ4740_CLK_UART0] = {
174ff1930c6SPaul Burton 		"uart0", CGU_CLK_GATE,
175ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
176ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 0 },
177ff1930c6SPaul Burton 	},
178ff1930c6SPaul Burton 
179ff1930c6SPaul Burton 	[JZ4740_CLK_UART1] = {
180ff1930c6SPaul Burton 		"uart1", CGU_CLK_GATE,
181ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
182ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 15 },
183ff1930c6SPaul Burton 	},
184ff1930c6SPaul Burton 
185ff1930c6SPaul Burton 	[JZ4740_CLK_DMA] = {
186ff1930c6SPaul Burton 		"dma", CGU_CLK_GATE,
187ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PCLK, -1, -1, -1 },
188ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 12 },
189ff1930c6SPaul Burton 	},
190ff1930c6SPaul Burton 
191ff1930c6SPaul Burton 	[JZ4740_CLK_IPU] = {
192ff1930c6SPaul Burton 		"ipu", CGU_CLK_GATE,
193ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PCLK, -1, -1, -1 },
194ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 13 },
195ff1930c6SPaul Burton 	},
196ff1930c6SPaul Burton 
197ff1930c6SPaul Burton 	[JZ4740_CLK_ADC] = {
198ff1930c6SPaul Burton 		"adc", CGU_CLK_GATE,
199ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
200ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 8 },
201ff1930c6SPaul Burton 	},
202ff1930c6SPaul Burton 
203ff1930c6SPaul Burton 	[JZ4740_CLK_I2C] = {
204ff1930c6SPaul Burton 		"i2c", CGU_CLK_GATE,
205ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
206ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 3 },
207ff1930c6SPaul Burton 	},
208ff1930c6SPaul Burton 
209ff1930c6SPaul Burton 	[JZ4740_CLK_AIC] = {
210ff1930c6SPaul Burton 		"aic", CGU_CLK_GATE,
211ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
212ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 5 },
213ff1930c6SPaul Burton 	},
214ff1930c6SPaul Burton };
215ff1930c6SPaul Burton 
216ff1930c6SPaul Burton static void __init jz4740_cgu_init(struct device_node *np)
217ff1930c6SPaul Burton {
218ff1930c6SPaul Burton 	int retval;
219ff1930c6SPaul Burton 
220ff1930c6SPaul Burton 	cgu = ingenic_cgu_new(jz4740_cgu_clocks,
221ff1930c6SPaul Burton 			      ARRAY_SIZE(jz4740_cgu_clocks), np);
222ff1930c6SPaul Burton 	if (!cgu) {
223ff1930c6SPaul Burton 		pr_err("%s: failed to initialise CGU\n", __func__);
224ff1930c6SPaul Burton 		return;
225ff1930c6SPaul Burton 	}
226ff1930c6SPaul Burton 
227ff1930c6SPaul Burton 	retval = ingenic_cgu_register_clocks(cgu);
228ff1930c6SPaul Burton 	if (retval)
229ff1930c6SPaul Burton 		pr_err("%s: failed to register CGU Clocks\n", __func__);
230ff1930c6SPaul Burton }
231ff1930c6SPaul Burton CLK_OF_DECLARE(jz4740_cgu, "ingenic,jz4740-cgu", jz4740_cgu_init);
23241dd641eSPaul Burton 
23341dd641eSPaul Burton void jz4740_clock_set_wait_mode(enum jz4740_wait_mode mode)
23441dd641eSPaul Burton {
23541dd641eSPaul Burton 	uint32_t lcr = readl(cgu->base + CGU_REG_LCR);
23641dd641eSPaul Burton 
23741dd641eSPaul Burton 	switch (mode) {
23841dd641eSPaul Burton 	case JZ4740_WAIT_MODE_IDLE:
23941dd641eSPaul Burton 		lcr &= ~LCR_SLEEP;
24041dd641eSPaul Burton 		break;
24141dd641eSPaul Burton 
24241dd641eSPaul Burton 	case JZ4740_WAIT_MODE_SLEEP:
24341dd641eSPaul Burton 		lcr |= LCR_SLEEP;
24441dd641eSPaul Burton 		break;
24541dd641eSPaul Burton 	}
24641dd641eSPaul Burton 
24741dd641eSPaul Burton 	writel(lcr, cgu->base + CGU_REG_LCR);
24841dd641eSPaul Burton }
249ed286ca5SPaul Burton 
250ed286ca5SPaul Burton void jz4740_clock_udc_disable_auto_suspend(void)
251ed286ca5SPaul Burton {
252ed286ca5SPaul Burton 	uint32_t clkgr = readl(cgu->base + CGU_REG_CLKGR);
253ed286ca5SPaul Burton 
254ed286ca5SPaul Burton 	clkgr &= ~CLKGR_UDC;
255ed286ca5SPaul Burton 	writel(clkgr, cgu->base + CGU_REG_CLKGR);
256ed286ca5SPaul Burton }
257ed286ca5SPaul Burton EXPORT_SYMBOL_GPL(jz4740_clock_udc_disable_auto_suspend);
258ed286ca5SPaul Burton 
259ed286ca5SPaul Burton void jz4740_clock_udc_enable_auto_suspend(void)
260ed286ca5SPaul Burton {
261ed286ca5SPaul Burton 	uint32_t clkgr = readl(cgu->base + CGU_REG_CLKGR);
262ed286ca5SPaul Burton 
263ed286ca5SPaul Burton 	clkgr |= CLKGR_UDC;
264ed286ca5SPaul Burton 	writel(clkgr, cgu->base + CGU_REG_CLKGR);
265ed286ca5SPaul Burton }
266ed286ca5SPaul Burton EXPORT_SYMBOL_GPL(jz4740_clock_udc_enable_auto_suspend);
26750d893ffSPaul Burton 
26850d893ffSPaul Burton #define JZ_CLOCK_GATE_UART0	BIT(0)
26950d893ffSPaul Burton #define JZ_CLOCK_GATE_TCU	BIT(1)
27050d893ffSPaul Burton #define JZ_CLOCK_GATE_DMAC	BIT(12)
27150d893ffSPaul Burton 
27250d893ffSPaul Burton void jz4740_clock_suspend(void)
27350d893ffSPaul Burton {
27450d893ffSPaul Burton 	uint32_t clkgr, cppcr;
27550d893ffSPaul Burton 
27650d893ffSPaul Burton 	clkgr = readl(cgu->base + CGU_REG_CLKGR);
27750d893ffSPaul Burton 	clkgr |= JZ_CLOCK_GATE_TCU | JZ_CLOCK_GATE_DMAC | JZ_CLOCK_GATE_UART0;
27850d893ffSPaul Burton 	writel(clkgr, cgu->base + CGU_REG_CLKGR);
27950d893ffSPaul Burton 
28050d893ffSPaul Burton 	cppcr = readl(cgu->base + CGU_REG_CPPCR);
28150d893ffSPaul Burton 	cppcr &= ~BIT(jz4740_cgu_clocks[JZ4740_CLK_PLL].pll.enable_bit);
28250d893ffSPaul Burton 	writel(cppcr, cgu->base + CGU_REG_CPPCR);
28350d893ffSPaul Burton }
28450d893ffSPaul Burton 
28550d893ffSPaul Burton void jz4740_clock_resume(void)
28650d893ffSPaul Burton {
28750d893ffSPaul Burton 	uint32_t clkgr, cppcr, stable;
28850d893ffSPaul Burton 
28950d893ffSPaul Burton 	cppcr = readl(cgu->base + CGU_REG_CPPCR);
29050d893ffSPaul Burton 	cppcr |= BIT(jz4740_cgu_clocks[JZ4740_CLK_PLL].pll.enable_bit);
29150d893ffSPaul Burton 	writel(cppcr, cgu->base + CGU_REG_CPPCR);
29250d893ffSPaul Burton 
29350d893ffSPaul Burton 	stable = BIT(jz4740_cgu_clocks[JZ4740_CLK_PLL].pll.stable_bit);
29450d893ffSPaul Burton 	do {
29550d893ffSPaul Burton 		cppcr = readl(cgu->base + CGU_REG_CPPCR);
29650d893ffSPaul Burton 	} while (!(cppcr & stable));
29750d893ffSPaul Burton 
29850d893ffSPaul Burton 	clkgr = readl(cgu->base + CGU_REG_CLKGR);
29950d893ffSPaul Burton 	clkgr &= ~JZ_CLOCK_GATE_TCU;
30050d893ffSPaul Burton 	clkgr &= ~JZ_CLOCK_GATE_DMAC;
30150d893ffSPaul Burton 	clkgr &= ~JZ_CLOCK_GATE_UART0;
30250d893ffSPaul Burton 	writel(clkgr, cgu->base + CGU_REG_CLKGR);
30350d893ffSPaul Burton }
304