xref: /openbmc/linux/drivers/clk/ingenic/jz4740-cgu.c (revision 62e59c4e69b3cdbad67e3c2d49e4df4cfe1679e3)
1ff1930c6SPaul Burton /*
2ff1930c6SPaul Burton  * Ingenic JZ4740 SoC CGU driver
3ff1930c6SPaul Burton  *
4ff1930c6SPaul Burton  * Copyright (c) 2015 Imagination Technologies
5fb615d61SPaul Burton  * Author: Paul Burton <paul.burton@mips.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>
20*62e59c4eSStephen Boyd #include <linux/io.h>
21ff1930c6SPaul Burton #include <linux/of.h>
22ff1930c6SPaul Burton #include <dt-bindings/clock/jz4740-cgu.h>
2341dd641eSPaul Burton #include <asm/mach-jz4740/clock.h>
24ff1930c6SPaul Burton #include "cgu.h"
25ff1930c6SPaul Burton 
26ff1930c6SPaul Burton /* CGU register offsets */
27ff1930c6SPaul Burton #define CGU_REG_CPCCR		0x00
2841dd641eSPaul Burton #define CGU_REG_LCR		0x04
29ff1930c6SPaul Burton #define CGU_REG_CPPCR		0x10
30ed286ca5SPaul Burton #define CGU_REG_CLKGR		0x20
31ff1930c6SPaul Burton #define CGU_REG_SCR		0x24
32ff1930c6SPaul Burton #define CGU_REG_I2SCDR		0x60
33ff1930c6SPaul Burton #define CGU_REG_LPCDR		0x64
34ff1930c6SPaul Burton #define CGU_REG_MSCCDR		0x68
35ff1930c6SPaul Burton #define CGU_REG_UHCCDR		0x6c
36ff1930c6SPaul Burton #define CGU_REG_SSICDR		0x74
37ff1930c6SPaul Burton 
38ff1930c6SPaul Burton /* bits within a PLL control register */
39ff1930c6SPaul Burton #define PLLCTL_M_SHIFT		23
40ff1930c6SPaul Burton #define PLLCTL_M_MASK		(0x1ff << PLLCTL_M_SHIFT)
41ff1930c6SPaul Burton #define PLLCTL_N_SHIFT		18
42ff1930c6SPaul Burton #define PLLCTL_N_MASK		(0x1f << PLLCTL_N_SHIFT)
43ff1930c6SPaul Burton #define PLLCTL_OD_SHIFT		16
44ff1930c6SPaul Burton #define PLLCTL_OD_MASK		(0x3 << PLLCTL_OD_SHIFT)
45ff1930c6SPaul Burton #define PLLCTL_STABLE		(1 << 10)
46ff1930c6SPaul Burton #define PLLCTL_BYPASS		(1 << 9)
47ff1930c6SPaul Burton #define PLLCTL_ENABLE		(1 << 8)
48ff1930c6SPaul Burton 
4941dd641eSPaul Burton /* bits within the LCR register */
5041dd641eSPaul Burton #define LCR_SLEEP		(1 << 0)
5141dd641eSPaul Burton 
52ed286ca5SPaul Burton /* bits within the CLKGR register */
53ed286ca5SPaul Burton #define CLKGR_UDC		(1 << 11)
54ed286ca5SPaul Burton 
55ff1930c6SPaul Burton static struct ingenic_cgu *cgu;
56ff1930c6SPaul Burton 
57ff1930c6SPaul Burton static const s8 pll_od_encoding[4] = {
58ff1930c6SPaul Burton 	0x0, 0x1, -1, 0x3,
59ff1930c6SPaul Burton };
60ff1930c6SPaul Burton 
61ff1930c6SPaul Burton static const struct ingenic_cgu_clk_info jz4740_cgu_clocks[] = {
62ff1930c6SPaul Burton 
63ff1930c6SPaul Burton 	/* External clocks */
64ff1930c6SPaul Burton 
65ff1930c6SPaul Burton 	[JZ4740_CLK_EXT] = { "ext", CGU_CLK_EXT },
66ff1930c6SPaul Burton 	[JZ4740_CLK_RTC] = { "rtc", CGU_CLK_EXT },
67ff1930c6SPaul Burton 
68ff1930c6SPaul Burton 	[JZ4740_CLK_PLL] = {
69ff1930c6SPaul Burton 		"pll", CGU_CLK_PLL,
70ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
71ff1930c6SPaul Burton 		.pll = {
72ff1930c6SPaul Burton 			.reg = CGU_REG_CPPCR,
73ff1930c6SPaul Burton 			.m_shift = 23,
74ff1930c6SPaul Burton 			.m_bits = 9,
75ff1930c6SPaul Burton 			.m_offset = 2,
76ff1930c6SPaul Burton 			.n_shift = 18,
77ff1930c6SPaul Burton 			.n_bits = 5,
78ff1930c6SPaul Burton 			.n_offset = 2,
79ff1930c6SPaul Burton 			.od_shift = 16,
80ff1930c6SPaul Burton 			.od_bits = 2,
81ff1930c6SPaul Burton 			.od_max = 4,
82ff1930c6SPaul Burton 			.od_encoding = pll_od_encoding,
83ff1930c6SPaul Burton 			.stable_bit = 10,
84ff1930c6SPaul Burton 			.bypass_bit = 9,
85ff1930c6SPaul Burton 			.enable_bit = 8,
86ff1930c6SPaul Burton 		},
87ff1930c6SPaul Burton 	},
88ff1930c6SPaul Burton 
89ff1930c6SPaul Burton 	/* Muxes & dividers */
90ff1930c6SPaul Burton 
91ff1930c6SPaul Burton 	[JZ4740_CLK_PLL_HALF] = {
92ff1930c6SPaul Burton 		"pll half", CGU_CLK_DIV,
93ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
944afe2d1aSHarvey Hunt 		.div = { CGU_REG_CPCCR, 21, 1, 1, -1, -1, -1 },
95ff1930c6SPaul Burton 	},
96ff1930c6SPaul Burton 
97ff1930c6SPaul Burton 	[JZ4740_CLK_CCLK] = {
98ff1930c6SPaul Burton 		"cclk", CGU_CLK_DIV,
99ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
1004afe2d1aSHarvey Hunt 		.div = { CGU_REG_CPCCR, 0, 1, 4, 22, -1, -1 },
101ff1930c6SPaul Burton 	},
102ff1930c6SPaul Burton 
103ff1930c6SPaul Burton 	[JZ4740_CLK_HCLK] = {
104ff1930c6SPaul Burton 		"hclk", CGU_CLK_DIV,
105ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
1064afe2d1aSHarvey Hunt 		.div = { CGU_REG_CPCCR, 4, 1, 4, 22, -1, -1 },
107ff1930c6SPaul Burton 	},
108ff1930c6SPaul Burton 
109ff1930c6SPaul Burton 	[JZ4740_CLK_PCLK] = {
110ff1930c6SPaul Burton 		"pclk", CGU_CLK_DIV,
111ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
1124afe2d1aSHarvey Hunt 		.div = { CGU_REG_CPCCR, 8, 1, 4, 22, -1, -1 },
113ff1930c6SPaul Burton 	},
114ff1930c6SPaul Burton 
115ff1930c6SPaul Burton 	[JZ4740_CLK_MCLK] = {
116ff1930c6SPaul Burton 		"mclk", CGU_CLK_DIV,
117ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL, -1, -1, -1 },
1184afe2d1aSHarvey Hunt 		.div = { CGU_REG_CPCCR, 12, 1, 4, 22, -1, -1 },
119ff1930c6SPaul Burton 	},
120ff1930c6SPaul Burton 
121ff1930c6SPaul Burton 	[JZ4740_CLK_LCD] = {
122ff1930c6SPaul Burton 		"lcd", CGU_CLK_DIV | CGU_CLK_GATE,
123ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 },
1244afe2d1aSHarvey Hunt 		.div = { CGU_REG_CPCCR, 16, 1, 5, 22, -1, -1 },
125ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 10 },
126ff1930c6SPaul Burton 	},
127ff1930c6SPaul Burton 
128ff1930c6SPaul Burton 	[JZ4740_CLK_LCD_PCLK] = {
129ff1930c6SPaul Burton 		"lcd_pclk", CGU_CLK_DIV,
130ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 },
1314afe2d1aSHarvey Hunt 		.div = { CGU_REG_LPCDR, 0, 1, 11, -1, -1, -1 },
132ff1930c6SPaul Burton 	},
133ff1930c6SPaul Burton 
134ff1930c6SPaul Burton 	[JZ4740_CLK_I2S] = {
135ff1930c6SPaul Burton 		"i2s", CGU_CLK_MUX | CGU_CLK_DIV | CGU_CLK_GATE,
136ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL_HALF, -1, -1 },
137ff1930c6SPaul Burton 		.mux = { CGU_REG_CPCCR, 31, 1 },
138574f4e80SPaul Cercueil 		.div = { CGU_REG_I2SCDR, 0, 1, 9, -1, -1, -1 },
139ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 6 },
140ff1930c6SPaul Burton 	},
141ff1930c6SPaul Burton 
142ff1930c6SPaul Burton 	[JZ4740_CLK_SPI] = {
143ff1930c6SPaul Burton 		"spi", CGU_CLK_MUX | CGU_CLK_DIV | CGU_CLK_GATE,
144ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL, -1, -1 },
145ff1930c6SPaul Burton 		.mux = { CGU_REG_SSICDR, 31, 1 },
1464afe2d1aSHarvey Hunt 		.div = { CGU_REG_SSICDR, 0, 1, 4, -1, -1, -1 },
147ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 4 },
148ff1930c6SPaul Burton 	},
149ff1930c6SPaul Burton 
150ff1930c6SPaul Burton 	[JZ4740_CLK_MMC] = {
151ff1930c6SPaul Burton 		"mmc", CGU_CLK_DIV | CGU_CLK_GATE,
152ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 },
1534afe2d1aSHarvey Hunt 		.div = { CGU_REG_MSCCDR, 0, 1, 5, -1, -1, -1 },
154ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 7 },
155ff1930c6SPaul Burton 	},
156ff1930c6SPaul Burton 
157ff1930c6SPaul Burton 	[JZ4740_CLK_UHC] = {
158ff1930c6SPaul Burton 		"uhc", CGU_CLK_DIV | CGU_CLK_GATE,
159ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 },
1604afe2d1aSHarvey Hunt 		.div = { CGU_REG_UHCCDR, 0, 1, 4, -1, -1, -1 },
161ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 14 },
162ff1930c6SPaul Burton 	},
163ff1930c6SPaul Burton 
164ff1930c6SPaul Burton 	[JZ4740_CLK_UDC] = {
1652b555a4bSPaul Cercueil 		"udc", CGU_CLK_MUX | CGU_CLK_DIV | CGU_CLK_GATE,
166ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL_HALF, -1, -1 },
167ff1930c6SPaul Burton 		.mux = { CGU_REG_CPCCR, 29, 1 },
1684afe2d1aSHarvey Hunt 		.div = { CGU_REG_CPCCR, 23, 1, 6, -1, -1, -1 },
169b7e29924SPaul Cercueil 		.gate = { CGU_REG_SCR, 6, true },
170ff1930c6SPaul Burton 	},
171ff1930c6SPaul Burton 
172ff1930c6SPaul Burton 	/* Gate-only clocks */
173ff1930c6SPaul Burton 
174ff1930c6SPaul Burton 	[JZ4740_CLK_UART0] = {
175ff1930c6SPaul Burton 		"uart0", CGU_CLK_GATE,
176ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
177ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 0 },
178ff1930c6SPaul Burton 	},
179ff1930c6SPaul Burton 
180ff1930c6SPaul Burton 	[JZ4740_CLK_UART1] = {
181ff1930c6SPaul Burton 		"uart1", CGU_CLK_GATE,
182ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
183ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 15 },
184ff1930c6SPaul Burton 	},
185ff1930c6SPaul Burton 
186ff1930c6SPaul Burton 	[JZ4740_CLK_DMA] = {
187ff1930c6SPaul Burton 		"dma", CGU_CLK_GATE,
188ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PCLK, -1, -1, -1 },
189ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 12 },
190ff1930c6SPaul Burton 	},
191ff1930c6SPaul Burton 
192ff1930c6SPaul Burton 	[JZ4740_CLK_IPU] = {
193ff1930c6SPaul Burton 		"ipu", CGU_CLK_GATE,
194ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_PCLK, -1, -1, -1 },
195ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 13 },
196ff1930c6SPaul Burton 	},
197ff1930c6SPaul Burton 
198ff1930c6SPaul Burton 	[JZ4740_CLK_ADC] = {
199ff1930c6SPaul Burton 		"adc", CGU_CLK_GATE,
200ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
201ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 8 },
202ff1930c6SPaul Burton 	},
203ff1930c6SPaul Burton 
204ff1930c6SPaul Burton 	[JZ4740_CLK_I2C] = {
205ff1930c6SPaul Burton 		"i2c", CGU_CLK_GATE,
206ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
207ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 3 },
208ff1930c6SPaul Burton 	},
209ff1930c6SPaul Burton 
210ff1930c6SPaul Burton 	[JZ4740_CLK_AIC] = {
211ff1930c6SPaul Burton 		"aic", CGU_CLK_GATE,
212ff1930c6SPaul Burton 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
213ff1930c6SPaul Burton 		.gate = { CGU_REG_CLKGR, 5 },
214ff1930c6SPaul Burton 	},
215ff1930c6SPaul Burton };
216ff1930c6SPaul Burton 
217ff1930c6SPaul Burton static void __init jz4740_cgu_init(struct device_node *np)
218ff1930c6SPaul Burton {
219ff1930c6SPaul Burton 	int retval;
220ff1930c6SPaul Burton 
221ff1930c6SPaul Burton 	cgu = ingenic_cgu_new(jz4740_cgu_clocks,
222ff1930c6SPaul Burton 			      ARRAY_SIZE(jz4740_cgu_clocks), np);
223ff1930c6SPaul Burton 	if (!cgu) {
224ff1930c6SPaul Burton 		pr_err("%s: failed to initialise CGU\n", __func__);
225ff1930c6SPaul Burton 		return;
226ff1930c6SPaul Burton 	}
227ff1930c6SPaul Burton 
228ff1930c6SPaul Burton 	retval = ingenic_cgu_register_clocks(cgu);
229ff1930c6SPaul Burton 	if (retval)
230ff1930c6SPaul Burton 		pr_err("%s: failed to register CGU Clocks\n", __func__);
231ff1930c6SPaul Burton }
232ff1930c6SPaul Burton CLK_OF_DECLARE(jz4740_cgu, "ingenic,jz4740-cgu", jz4740_cgu_init);
23341dd641eSPaul Burton 
23441dd641eSPaul Burton void jz4740_clock_set_wait_mode(enum jz4740_wait_mode mode)
23541dd641eSPaul Burton {
23641dd641eSPaul Burton 	uint32_t lcr = readl(cgu->base + CGU_REG_LCR);
23741dd641eSPaul Burton 
23841dd641eSPaul Burton 	switch (mode) {
23941dd641eSPaul Burton 	case JZ4740_WAIT_MODE_IDLE:
24041dd641eSPaul Burton 		lcr &= ~LCR_SLEEP;
24141dd641eSPaul Burton 		break;
24241dd641eSPaul Burton 
24341dd641eSPaul Burton 	case JZ4740_WAIT_MODE_SLEEP:
24441dd641eSPaul Burton 		lcr |= LCR_SLEEP;
24541dd641eSPaul Burton 		break;
24641dd641eSPaul Burton 	}
24741dd641eSPaul Burton 
24841dd641eSPaul Burton 	writel(lcr, cgu->base + CGU_REG_LCR);
24941dd641eSPaul Burton }
250ed286ca5SPaul Burton 
251ed286ca5SPaul Burton void jz4740_clock_udc_disable_auto_suspend(void)
252ed286ca5SPaul Burton {
253ed286ca5SPaul Burton 	uint32_t clkgr = readl(cgu->base + CGU_REG_CLKGR);
254ed286ca5SPaul Burton 
255ed286ca5SPaul Burton 	clkgr &= ~CLKGR_UDC;
256ed286ca5SPaul Burton 	writel(clkgr, cgu->base + CGU_REG_CLKGR);
257ed286ca5SPaul Burton }
258ed286ca5SPaul Burton EXPORT_SYMBOL_GPL(jz4740_clock_udc_disable_auto_suspend);
259ed286ca5SPaul Burton 
260ed286ca5SPaul Burton void jz4740_clock_udc_enable_auto_suspend(void)
261ed286ca5SPaul Burton {
262ed286ca5SPaul Burton 	uint32_t clkgr = readl(cgu->base + CGU_REG_CLKGR);
263ed286ca5SPaul Burton 
264ed286ca5SPaul Burton 	clkgr |= CLKGR_UDC;
265ed286ca5SPaul Burton 	writel(clkgr, cgu->base + CGU_REG_CLKGR);
266ed286ca5SPaul Burton }
267ed286ca5SPaul Burton EXPORT_SYMBOL_GPL(jz4740_clock_udc_enable_auto_suspend);
26850d893ffSPaul Burton 
26950d893ffSPaul Burton #define JZ_CLOCK_GATE_UART0	BIT(0)
27050d893ffSPaul Burton #define JZ_CLOCK_GATE_TCU	BIT(1)
27150d893ffSPaul Burton #define JZ_CLOCK_GATE_DMAC	BIT(12)
27250d893ffSPaul Burton 
27350d893ffSPaul Burton void jz4740_clock_suspend(void)
27450d893ffSPaul Burton {
27550d893ffSPaul Burton 	uint32_t clkgr, cppcr;
27650d893ffSPaul Burton 
27750d893ffSPaul Burton 	clkgr = readl(cgu->base + CGU_REG_CLKGR);
27850d893ffSPaul Burton 	clkgr |= JZ_CLOCK_GATE_TCU | JZ_CLOCK_GATE_DMAC | JZ_CLOCK_GATE_UART0;
27950d893ffSPaul Burton 	writel(clkgr, cgu->base + CGU_REG_CLKGR);
28050d893ffSPaul Burton 
28150d893ffSPaul Burton 	cppcr = readl(cgu->base + CGU_REG_CPPCR);
28250d893ffSPaul Burton 	cppcr &= ~BIT(jz4740_cgu_clocks[JZ4740_CLK_PLL].pll.enable_bit);
28350d893ffSPaul Burton 	writel(cppcr, cgu->base + CGU_REG_CPPCR);
28450d893ffSPaul Burton }
28550d893ffSPaul Burton 
28650d893ffSPaul Burton void jz4740_clock_resume(void)
28750d893ffSPaul Burton {
28850d893ffSPaul Burton 	uint32_t clkgr, cppcr, stable;
28950d893ffSPaul Burton 
29050d893ffSPaul Burton 	cppcr = readl(cgu->base + CGU_REG_CPPCR);
29150d893ffSPaul Burton 	cppcr |= BIT(jz4740_cgu_clocks[JZ4740_CLK_PLL].pll.enable_bit);
29250d893ffSPaul Burton 	writel(cppcr, cgu->base + CGU_REG_CPPCR);
29350d893ffSPaul Burton 
29450d893ffSPaul Burton 	stable = BIT(jz4740_cgu_clocks[JZ4740_CLK_PLL].pll.stable_bit);
29550d893ffSPaul Burton 	do {
29650d893ffSPaul Burton 		cppcr = readl(cgu->base + CGU_REG_CPPCR);
29750d893ffSPaul Burton 	} while (!(cppcr & stable));
29850d893ffSPaul Burton 
29950d893ffSPaul Burton 	clkgr = readl(cgu->base + CGU_REG_CLKGR);
30050d893ffSPaul Burton 	clkgr &= ~JZ_CLOCK_GATE_TCU;
30150d893ffSPaul Burton 	clkgr &= ~JZ_CLOCK_GATE_DMAC;
30250d893ffSPaul Burton 	clkgr &= ~JZ_CLOCK_GATE_UART0;
30350d893ffSPaul Burton 	writel(clkgr, cgu->base + CGU_REG_CLKGR);
30450d893ffSPaul Burton }
305