xref: /netbsd-src/sys/arch/arm/sunxi/sun4i_a10_ccu.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /* $NetBSD: sun4i_a10_ccu.c,v 1.9 2018/04/02 20:57:18 bouyer Exp $ */
2 
3 /*-
4  * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 
31 __KERNEL_RCSID(1, "$NetBSD: sun4i_a10_ccu.c,v 1.9 2018/04/02 20:57:18 bouyer Exp $");
32 
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/device.h>
36 #include <sys/systm.h>
37 
38 #include <dev/fdt/fdtvar.h>
39 
40 #include <arm/sunxi/sunxi_ccu.h>
41 #include <arm/sunxi/sun4i_a10_ccu.h>
42 #include <arm/sunxi/sun7i_a20_ccu.h>
43 
44 #define	PLL1_CFG_REG		0x000
45 #define	PLL2_CFG_REG		0x008
46 #define	PLL3_CFG_REG		0x010
47 #define	PLL5_CFG_REG		0x020
48 #define	PLL6_CFG_REG		0x028
49 #define	PLL7_CFG_REG		0x030
50 #define	OSC24M_CFG_REG		0x050
51 #define	CPU_AHB_APB0_CFG_REG	0x054
52 #define	APB1_CLK_DIV_REG	0x058
53 #define	AHB_GATING_REG0		0x060
54 #define	AHB_GATING_REG1		0x064
55 #define	APB0_GATING_REG		0x068
56 #define	APB1_GATING_REG		0x06c
57 #define	NAND_SCLK_CFG_REG	0x080
58 #define	SD0_SCLK_CFG_REG        0x088
59 #define	SD1_SCLK_CFG_REG        0x08c
60 #define	SD2_SCLK_CFG_REG        0x090
61 #define	SD3_SCLK_CFG_REG	0x094
62 #define	SATA_CFG_REG		0x0c8
63 #define	USBPHY_CFG_REG		0x0cc
64 #define	DRAM_GATING_REG		0x100
65 #define	BE0_CFG_REG		0x104
66 #define	BE1_CFG_REG		0x108
67 #define	FE0_CFG_REG		0x10c
68 #define	FE1_CFG_REG		0x110
69 #define	MP_CFG_REG		0x114
70 #define	LCD0CH0_CFG_REG		0x118
71 #define	LCD1CH0_CFG_REG		0x11c
72 #define LCD0CH1_CFG_REG		0x12c
73 #define LCD1CH1_CFG_REG		0x130
74 #define	CSI_CFG_REG		0x134
75 #define	VE_CFG_REG		0x13c
76 #define	AUDIO_CODEC_SCLK_CFG_REG 0x140
77 #define	LVDS_CFG_REG 		0x14c
78 #define	HDMI_CLOCK_CFG_REG	0x150
79 #define	MALI_CLOCK_CFG_REG	0x154
80 #define	IEP_SCLK_CFG_REG	0x160
81 
82 static int sun4i_a10_ccu_match(device_t, cfdata_t, void *);
83 static void sun4i_a10_ccu_attach(device_t, device_t, void *);
84 
85 enum sun4i_a10_ccu_type {
86 	CCU_A10 = 1,
87 	CCU_A20,
88 };
89 
90 static const struct of_compat_data compat_data[] = {
91 	{ "allwinner,sun4i-a10-ccu",	CCU_A10 },
92 	{ "allwinner,sun7i-a20-ccu",	CCU_A20 },
93 	{ NULL }
94 };
95 
96 CFATTACH_DECL_NEW(sunxi_a10_ccu, sizeof(struct sunxi_ccu_softc),
97 	sun4i_a10_ccu_match, sun4i_a10_ccu_attach, NULL, NULL);
98 
99 static struct sunxi_ccu_reset sun4i_a10_ccu_resets[] = {
100 	SUNXI_CCU_RESET(A10_RST_USB_PHY0, USBPHY_CFG_REG, 0),
101 	SUNXI_CCU_RESET(A10_RST_USB_PHY1, USBPHY_CFG_REG, 1),
102 	SUNXI_CCU_RESET(A10_RST_USB_PHY2, USBPHY_CFG_REG, 2),
103 	SUNXI_CCU_RESET(A10_RST_DE_BE0, BE0_CFG_REG, 30),
104 	SUNXI_CCU_RESET(A10_RST_DE_BE1, BE1_CFG_REG, 30),
105 	SUNXI_CCU_RESET(A10_RST_DE_FE0, FE0_CFG_REG, 30),
106 	SUNXI_CCU_RESET(A10_RST_DE_FE1, FE1_CFG_REG, 30),
107 	SUNXI_CCU_RESET(A10_RST_DE_MP, MP_CFG_REG, 30),
108 	SUNXI_CCU_RESET(A10_RST_TCON0, LCD0CH0_CFG_REG, 30),
109 	SUNXI_CCU_RESET(A10_RST_TCON1, LCD1CH0_CFG_REG, 30),
110 	SUNXI_CCU_RESET(A10_RST_LVDS, LVDS_CFG_REG, 0),
111 };
112 
113 static const char *cpu_parents[] = { "losc", "osc24m", "pll_core", "pll_periph" };
114 static const char *axi_parents[] = { "cpu" };
115 static const char *ahb_parents[] = { "axi", "pll_periph", "pll_periph_base" };
116 static const char *apb0_parents[] = { "ahb" };
117 static const char *apb1_parents[] = { "osc24m", "pll_periph", "losc" };
118 static const char *mod_parents[] = { "osc24m", "pll_periph", "pll_ddr_other" };
119 static const char *sata_parents[] = { "pll6_periph_sata", "external" };
120 static const char *de_parents[] = { "pll_video0", "pll_video1", "pll_ddr_other" };
121 static const char *lcd_parents[] = { "pll_video0", "pll_video1", "pll_video0x2", "pll_video1x2" };
122 
123 static const struct sunxi_ccu_nkmp_tbl sun4i_a10_pll1_table[] = {
124 	{ 1008000000, 21, 1, 0, 0 },
125 	{  960000000, 20, 1, 0, 0 },
126 	{  912000000, 19, 1, 0, 0 },
127 	{  864000000, 18, 1, 0, 0 },
128 	{  720000000, 30, 0, 0, 0 },
129 	{  624000000, 26, 0, 0, 0 },
130 	{  528000000, 22, 0, 0, 0 },
131 	{  312000000, 13, 0, 0, 0 },
132 	{  144000000, 12, 0, 0, 1 },
133 	{          0 }
134 };
135 
136 static const struct sunxi_ccu_nkmp_tbl sun4i_a10_ac_dig_table[] = {
137 	{ 24576000, 86, 0, 21, 4 },
138 	{ 0 }
139 };
140 
141 /*
142  * some special cases
143  * hardcode lcd0 (tcon0) to pll3 and lcd1 (tcon1) to pll7.
144  * compute pll rate based on desired pixel clock
145  */
146 
147 static int sun4i_a10_ccu_lcd0ch0_set_rate(struct sunxi_ccu_softc *,
148     struct sunxi_ccu_clk *, u_int);
149 static int sun4i_a10_ccu_lcd1ch0_set_rate(struct sunxi_ccu_softc *,
150     struct sunxi_ccu_clk *, u_int);
151 static u_int sun4i_a10_ccu_lcd0ch0_round_rate(struct sunxi_ccu_softc *,
152     struct sunxi_ccu_clk *, u_int);
153 static u_int sun4i_a10_ccu_lcd1ch0_round_rate(struct sunxi_ccu_softc *,
154     struct sunxi_ccu_clk *, u_int);
155 static int sun4i_a10_ccu_lcd0ch1_set_rate(struct sunxi_ccu_softc *,
156     struct sunxi_ccu_clk *, u_int);
157 static int sun4i_a10_ccu_lcd1ch1_set_rate(struct sunxi_ccu_softc *,
158     struct sunxi_ccu_clk *, u_int);
159 
160 static struct sunxi_ccu_clk sun4i_a10_ccu_clks[] = {
161 	SUNXI_CCU_GATE(A10_CLK_HOSC, "osc24m", "hosc",
162 	    OSC24M_CFG_REG, 0),
163 
164 	SUNXI_CCU_NKMP_TABLE(A10_CLK_PLL_CORE, "pll_core", "osc24m",
165 	    PLL1_CFG_REG,		/* reg */
166 	    __BITS(12,8),		/* n */
167 	    __BITS(5,4), 		/* k */
168 	    __BITS(1,0),		/* m */
169 	    __BITS(17,16),		/* p */
170 	    __BIT(31),			/* enable */
171 	    0,				/* lock */
172 	    sun4i_a10_pll1_table,	/* table */
173 	    SUNXI_CCU_NKMP_FACTOR_P_POW2 | SUNXI_CCU_NKMP_FACTOR_N_EXACT |
174 	    SUNXI_CCU_NKMP_FACTOR_N_ZERO_IS_ONE | SUNXI_CCU_NKMP_SCALE_CLOCK),
175 
176 	SUNXI_CCU_NKMP_TABLE(A10_CLK_PLL_AUDIO_BASE, "pll_audio", "osc24m",
177 	    PLL2_CFG_REG,		/* reg */
178 	    __BITS(14,8),		/* n */
179 	    0,				/* k */
180 	    __BITS(4,0),		/* m */
181 	    __BITS(29,26),		/* p */
182 	    __BIT(31),			/* enable */
183 	    0,				/* lock */
184 	    sun4i_a10_ac_dig_table,	/* table */
185 	    0),
186 
187 	SUNXI_CCU_NKMP(A10_CLK_PLL_PERIPH_BASE, "pll_periph_base", "osc24m",
188 	    PLL6_CFG_REG,		/* reg */
189 	    __BITS(12,8),		/* n */
190 	    __BITS(5,4), 		/* k */
191 	    0,				/* m */
192 	    0,				/* p */
193 	    __BIT(31),			/* enable */
194 	    SUNXI_CCU_NKMP_FACTOR_N_EXACT),
195 
196 	SUNXI_CCU_FIXED_FACTOR(A10_CLK_PLL_PERIPH, "pll_periph", "pll_periph_base",
197 	    2, 1),
198 
199 	SUNXI_CCU_NKMP(A10_CLK_PLL_PERIPH_SATA, "pll_periph_sata", "pll_periph_base",
200 	    PLL6_CFG_REG,		/* reg */
201 	    0,				/* n */
202 	    0,				/* k */
203 	    __BITS(1,0),		/* m */
204 	    0,				/* p */
205 	    __BIT(14),			/* enable */
206 	    0),
207 
208 	SUNXI_CCU_DIV_GATE(A10_CLK_SATA, "sata", sata_parents,
209 	    SATA_CFG_REG,		/* reg */
210 	    0,				/* div */
211 	    __BIT(24),			/* sel */
212 	    __BIT(31),			/* enable */
213 	    0),
214 
215 	SUNXI_CCU_NKMP(A10_CLK_PLL_DDR_BASE, "pll_ddr_other", "osc24m",
216 	    PLL5_CFG_REG,		/* reg */
217 	    __BITS(12, 8),		/* n */
218 	    __BITS(5,4),		/* k */
219 	    0,				/* m */
220 	    __BITS(17,16),		/* p */
221 	    __BIT(31),			/* enable */
222 	    SUNXI_CCU_NKMP_FACTOR_N_EXACT | SUNXI_CCU_NKMP_FACTOR_P_POW2),
223 
224 	SUNXI_CCU_NKMP(A10_CLK_PLL_DDR, "pll_ddr", "osc24m",
225 	    PLL5_CFG_REG,		/* reg */
226 	    __BITS(12, 8),		/* n */
227 	    __BITS(5,4),		/* k */
228 	    __BITS(1,0),		/* m */
229 	    0,				/* p */
230 	    __BIT(31),			/* enable */
231 	    SUNXI_CCU_NKMP_FACTOR_N_EXACT),
232 
233 	SUNXI_CCU_DIV(A10_CLK_CPU, "cpu", cpu_parents,
234 	    CPU_AHB_APB0_CFG_REG,	/* reg */
235 	    0,				/* div */
236 	    __BITS(17,16),		/* sel */
237 	    SUNXI_CCU_DIV_SET_RATE_PARENT),
238 
239 	SUNXI_CCU_DIV(A10_CLK_AXI, "axi", axi_parents,
240 	    CPU_AHB_APB0_CFG_REG,	/* reg */
241 	    __BITS(1,0),		/* div */
242 	    0,				/* sel */
243 	    0),
244 
245 	SUNXI_CCU_DIV(A10_CLK_AHB, "ahb", ahb_parents,
246 	    CPU_AHB_APB0_CFG_REG,	/* reg */
247 	    __BITS(5,4),		/* div */
248 	    __BITS(7,6),		/* sel */
249 	    SUNXI_CCU_DIV_POWER_OF_TWO),
250 
251 	SUNXI_CCU_DIV(A10_CLK_APB0, "apb0", apb0_parents,
252 	    CPU_AHB_APB0_CFG_REG,	/* reg */
253 	    __BITS(9,8),		/* div */
254 	    0,				/* sel */
255 	    SUNXI_CCU_DIV_ZERO_IS_ONE | SUNXI_CCU_DIV_POWER_OF_TWO),
256 
257 	SUNXI_CCU_NM(A10_CLK_APB1, "apb1", apb1_parents,
258 	    APB1_CLK_DIV_REG,		/* reg */
259 	    __BITS(17,16),		/* n */
260 	    __BITS(4,0),		/* m */
261 	    __BITS(25,24),		/* sel */
262 	    0,				/* enable */
263 	    SUNXI_CCU_NM_POWER_OF_TWO),
264 
265 	SUNXI_CCU_NM(A10_CLK_NAND, "nand", mod_parents,
266 	    NAND_SCLK_CFG_REG,		/* reg */
267 	    __BITS(17,16),		/* n */
268 	    __BITS(3,0),		/* m */
269 	    __BITS(25,24),		/* sel */
270 	    __BIT(31),			/* enable */
271 	    SUNXI_CCU_NM_POWER_OF_TWO),
272 
273 	SUNXI_CCU_NM(A10_CLK_MMC0, "mmc0", mod_parents,
274 	    SD0_SCLK_CFG_REG,		/* reg */
275 	    __BITS(17,16),		/* n */
276 	    __BITS(3,0),		/* m */
277 	    __BITS(25,24),		/* sel */
278 	    __BIT(31),			/* enable */
279 	    SUNXI_CCU_NM_POWER_OF_TWO),
280 	SUNXI_CCU_PHASE(A10_CLK_MMC0_SAMPLE, "mmc0_sample", "mmc0",
281 	    SD0_SCLK_CFG_REG, __BITS(22,20)),
282 	SUNXI_CCU_PHASE(A10_CLK_MMC0_OUTPUT, "mmc0_output", "mmc0",
283 	    SD0_SCLK_CFG_REG, __BITS(10,8)),
284 	SUNXI_CCU_NM(A10_CLK_MMC1, "mmc1", mod_parents,
285 	    SD1_SCLK_CFG_REG,		/* reg */
286 	    __BITS(17,16),		/* n */
287 	    __BITS(3,0),		/* m */
288 	    __BITS(25,24),		/* sel */
289 	    __BIT(31),			/* enable */
290 	    SUNXI_CCU_NM_POWER_OF_TWO),
291 	SUNXI_CCU_PHASE(A10_CLK_MMC1_SAMPLE, "mmc1_sample", "mmc1",
292 	    SD1_SCLK_CFG_REG, __BITS(22,20)),
293 	SUNXI_CCU_PHASE(A10_CLK_MMC1_OUTPUT, "mmc1_output", "mmc1",
294 	    SD1_SCLK_CFG_REG, __BITS(10,8)),
295 	SUNXI_CCU_NM(A10_CLK_MMC2, "mmc2", mod_parents,
296 	    SD2_SCLK_CFG_REG,		/* reg */
297 	    __BITS(17,16),		/* n */
298 	    __BITS(3,0),		/* m */
299 	    __BITS(25,24),		/* sel */
300 	    __BIT(31),			/* enable */
301 	    SUNXI_CCU_NM_POWER_OF_TWO),
302 	SUNXI_CCU_PHASE(A10_CLK_MMC2_SAMPLE, "mmc2_sample", "mmc2",
303 	    SD2_SCLK_CFG_REG, __BITS(22,20)),
304 	SUNXI_CCU_PHASE(A10_CLK_MMC2_OUTPUT, "mmc2_output", "mmc2",
305 	    SD2_SCLK_CFG_REG, __BITS(10,8)),
306 	SUNXI_CCU_NM(A10_CLK_MMC3, "mmc3", mod_parents,
307 	    SD3_SCLK_CFG_REG,		/* reg */
308 	    __BITS(17,16),		/* n */
309 	    __BITS(3,0),		/* m */
310 	    __BITS(25,24),		/* sel */
311 	    __BIT(31),			/* enable */
312 	    SUNXI_CCU_NM_POWER_OF_TWO),
313 	SUNXI_CCU_PHASE(A10_CLK_MMC3_SAMPLE, "mmc3_sample", "mmc3",
314 	    SD3_SCLK_CFG_REG, __BITS(22,20)),
315 	SUNXI_CCU_PHASE(A10_CLK_MMC3_OUTPUT, "mmc3_output", "mmc3",
316 	    SD3_SCLK_CFG_REG, __BITS(10,8)),
317 
318 	SUNXI_CCU_FRACTIONAL(A10_CLK_PLL_VIDEO0, "pll_video0", "osc24m",
319 	    PLL3_CFG_REG,		/* reg */
320 	    __BITS(7,0),		/* m */
321 	    9,				/* m_min */
322 	    127,			/* m_max */
323 	    __BIT(15),			/* div_en */
324 	    __BIT(14),			/* frac_sel */
325 	    270000000, 297000000,	/* frac values */
326 	    8,				/* prediv */
327 	    __BIT(31)			/* enable */
328 	    ),
329 	SUNXI_CCU_FRACTIONAL(A10_CLK_PLL_VIDEO1, "pll_video1", "osc24m",
330 	    PLL7_CFG_REG,		/* reg */
331 	    __BITS(7,0),		/* m */
332 	    9,				/* m_min */
333 	    127,			/* m_max */
334 	    __BIT(15),			/* div_en */
335 	    __BIT(14),			/* frac_sel */
336 	    270000000, 297000000,	/* frac values */
337 	    8,				/* prediv */
338 	    __BIT(31)			/* enable */
339 	    ),
340 	SUNXI_CCU_FIXED_FACTOR(A10_CLK_PLL_VIDEO0_2X,
341 	    "pll_video0x2", "pll_video0",
342 	    1, 2),
343 	SUNXI_CCU_FIXED_FACTOR(A10_CLK_PLL_VIDEO1_2X,
344 	    "pll_video1x2", "pll_video1",
345 	    1, 2),
346 
347 	SUNXI_CCU_DIV_GATE(A10_CLK_DE_BE0, "debe0-mod", de_parents,
348 	    BE0_CFG_REG,		/* reg */
349 	    __BITS(3,0),		/* div */
350 	    __BITS(25,24),		/* sel */
351 	    __BIT(31),			/* enable */
352 	    0				/* flags */
353 	    ),
354 	SUNXI_CCU_DIV_GATE(A10_CLK_DE_BE1, "debe1-mod", de_parents,
355 	    BE1_CFG_REG,		/* reg */
356 	    __BITS(3,0),		/* div */
357 	    __BITS(25,24),		/* sel */
358 	    __BIT(31),			/* enable */
359 	    0				/* flags */
360 	    ),
361 	SUNXI_CCU_DIV_GATE(A10_CLK_DE_FE0, "defe0-mod", de_parents,
362 	    FE0_CFG_REG,		/* reg */
363 	    __BITS(3,0),		/* div */
364 	    __BITS(25,24),		/* sel */
365 	    __BIT(31),			/* enable */
366 	    0				/* flags */
367 	    ),
368 	SUNXI_CCU_DIV_GATE(A10_CLK_DE_FE1, "defe1-mod", de_parents,
369 	    FE1_CFG_REG,		/* reg */
370 	    __BITS(3,0),		/* div */
371 	    __BITS(25,24),		/* sel */
372 	    __BIT(31),			/* enable */
373 	    0				/* flags */
374 	    ),
375 	[A10_CLK_TCON0_CH0] = {
376 	    .type = SUNXI_CCU_DIV,
377 	    .base.name = "tcon0-ch0",
378 	    .u.div.reg = LCD0CH0_CFG_REG,
379 	    .u.div.parents = lcd_parents,
380 	    .u.div.nparents = __arraycount(lcd_parents),
381 	    .u.div.div = 0,
382 	    .u.div.sel = __BITS(25,24),
383 	    .u.div.enable = __BIT(31),
384 	    .u.div.flags = 0,
385 	    .enable = sunxi_ccu_div_enable,
386 	    .get_rate = sunxi_ccu_div_get_rate,
387 	    .set_rate = sun4i_a10_ccu_lcd0ch0_set_rate,
388 	    .round_rate = sun4i_a10_ccu_lcd0ch0_round_rate,
389 	    .set_parent = sunxi_ccu_div_set_parent,
390 	    .get_parent = sunxi_ccu_div_get_parent,
391 	    },
392 	[A10_CLK_TCON1_CH0] = {
393 	    .type = SUNXI_CCU_DIV,
394 	    .base.name = "tcon1-ch0",
395 	    .u.div.reg = LCD1CH0_CFG_REG,
396 	    .u.div.parents = lcd_parents,
397 	    .u.div.nparents = __arraycount(lcd_parents),
398 	    .u.div.div = 0,
399 	    .u.div.sel = __BITS(25,24),
400 	    .u.div.enable = __BIT(31),
401 	    .u.div.flags = 0,
402 	    .enable = sunxi_ccu_div_enable,
403 	    .get_rate = sunxi_ccu_div_get_rate,
404 	    .set_rate = sun4i_a10_ccu_lcd1ch0_set_rate,
405 	    .round_rate = sun4i_a10_ccu_lcd1ch0_round_rate,
406 	    .set_parent = sunxi_ccu_div_set_parent,
407 	    .get_parent = sunxi_ccu_div_get_parent,
408 	    },
409 	[A10_CLK_TCON0_CH1] = {
410 	    .type = SUNXI_CCU_DIV,
411 	    .base.name = "tcon0-ch1",
412 	    .u.div.reg = LCD0CH1_CFG_REG,
413 	    .u.div.parents = lcd_parents,
414 	    .u.div.nparents = __arraycount(lcd_parents),
415 	    .u.div.div = __BITS(3,0),
416 	    .u.div.sel = __BITS(25,24),
417 	    .u.div.enable = __BIT(15) | __BIT(31),
418 	    .u.div.flags = 0,
419 	    .enable = sunxi_ccu_div_enable,
420 	    .get_rate = sunxi_ccu_div_get_rate,
421 	    .set_rate = sun4i_a10_ccu_lcd0ch1_set_rate,
422 	    .set_parent = sunxi_ccu_div_set_parent,
423 	    .get_parent = sunxi_ccu_div_get_parent,
424 	    },
425 	[A10_CLK_TCON1_CH1] = {
426 	    .type = SUNXI_CCU_DIV,
427 	    .base.name = "tcon1-ch1",
428 	    .u.div.reg = LCD1CH1_CFG_REG,
429 	    .u.div.parents = lcd_parents,
430 	    .u.div.nparents = __arraycount(lcd_parents),
431 	    .u.div.div = __BITS(3,0),
432 	    .u.div.sel = __BITS(25,24),
433 	    .u.div.enable = __BIT(15) | __BIT(31),
434 	    .u.div.flags = 0,
435 	    .enable = sunxi_ccu_div_enable,
436 	    .get_rate = sunxi_ccu_div_get_rate,
437 	    .set_rate = sun4i_a10_ccu_lcd1ch1_set_rate,
438 	    .set_parent = sunxi_ccu_div_set_parent,
439 	    .get_parent = sunxi_ccu_div_get_parent,
440 	    },
441 	SUNXI_CCU_DIV_GATE(A10_CLK_HDMI, "hdmi-mod", lcd_parents,
442 	    HDMI_CLOCK_CFG_REG,		/* reg */
443 	    __BITS(3,0),		/* div */
444 	    __BITS(25,24),		/* sel */
445 	    __BIT(31),			/* enable */
446 	    0				/* flags */
447 	    ),
448 
449 	/* AHB_GATING_REG0 */
450 	SUNXI_CCU_GATE(A10_CLK_AHB_OTG, "ahb-otg", "ahb",
451 	    AHB_GATING_REG0, 0),
452 	SUNXI_CCU_GATE(A10_CLK_AHB_EHCI0, "ahb-ehci0", "ahb",
453 	    AHB_GATING_REG0, 1),
454 	SUNXI_CCU_GATE(A10_CLK_AHB_OHCI0, "ahb-ohci0", "ahb",
455 	    AHB_GATING_REG0, 2),
456 	SUNXI_CCU_GATE(A10_CLK_AHB_EHCI1, "ahb-ehci1", "ahb",
457 	    AHB_GATING_REG0, 3),
458 	SUNXI_CCU_GATE(A10_CLK_AHB_OHCI1, "ahb-ohci1", "ahb",
459 	    AHB_GATING_REG0, 4),
460 	SUNXI_CCU_GATE(A10_CLK_AHB_SS, "ahb-ss", "ahb",
461 	    AHB_GATING_REG0, 5),
462 	SUNXI_CCU_GATE(A10_CLK_AHB_DMA, "ahb-dma", "ahb",
463 	    AHB_GATING_REG0, 6),
464 	SUNXI_CCU_GATE(A10_CLK_AHB_BIST, "ahb-bist", "ahb",
465 	    AHB_GATING_REG0, 7),
466 	SUNXI_CCU_GATE(A10_CLK_AHB_MMC0, "ahb-mmc0", "ahb",
467 	    AHB_GATING_REG0, 8),
468 	SUNXI_CCU_GATE(A10_CLK_AHB_MMC1, "ahb-mmc1", "ahb",
469 	    AHB_GATING_REG0, 9),
470 	SUNXI_CCU_GATE(A10_CLK_AHB_MMC2, "ahb-mmc2", "ahb",
471 	    AHB_GATING_REG0, 10),
472 	SUNXI_CCU_GATE(A10_CLK_AHB_MMC3, "ahb-mmc3", "ahb",
473 	    AHB_GATING_REG0, 11),
474 	SUNXI_CCU_GATE(A10_CLK_AHB_MS, "ahb-ms", "ahb",
475 	    AHB_GATING_REG0, 12),
476 	SUNXI_CCU_GATE(A10_CLK_AHB_NAND, "ahb-nand", "ahb",
477 	    AHB_GATING_REG0, 13),
478 	SUNXI_CCU_GATE(A10_CLK_AHB_SDRAM, "ahb-sdram", "ahb",
479 	    AHB_GATING_REG0, 14),
480 	SUNXI_CCU_GATE(A10_CLK_AHB_ACE, "ahb-ace", "ahb",
481 	    AHB_GATING_REG0, 16),
482 	SUNXI_CCU_GATE(A10_CLK_AHB_EMAC, "ahb-emac", "ahb",
483 	    AHB_GATING_REG0, 17),
484 	SUNXI_CCU_GATE(A10_CLK_AHB_TS, "ahb-ts", "ahb",
485 	    AHB_GATING_REG0, 18),
486 	SUNXI_CCU_GATE(A10_CLK_AHB_SPI0, "ahb-spi0", "ahb",
487 	    AHB_GATING_REG0, 20),
488 	SUNXI_CCU_GATE(A10_CLK_AHB_SPI1, "ahb-spi1", "ahb",
489 	    AHB_GATING_REG0, 21),
490 	SUNXI_CCU_GATE(A10_CLK_AHB_SPI2, "ahb-spi2", "ahb",
491 	    AHB_GATING_REG0, 22),
492 	SUNXI_CCU_GATE(A10_CLK_AHB_SPI3, "ahb-spi3", "ahb",
493 	    AHB_GATING_REG0, 23),
494 	SUNXI_CCU_GATE(A10_CLK_AHB_SATA, "ahb-sata", "ahb",
495 	    AHB_GATING_REG0, 25),
496 	SUNXI_CCU_GATE(A10_CLK_AHB_HSTIMER, "ahb-hstimer", "ahb",
497 	    AHB_GATING_REG0, 28),
498 
499 	/* AHB_GATING_REG1. Missing: TVE, HDMI */
500 	SUNXI_CCU_GATE(A10_CLK_AHB_VE, "ahb-ve", "ahb",
501 	    AHB_GATING_REG1, 0),
502 	SUNXI_CCU_GATE(A10_CLK_AHB_TVD, "ahb-tvd", "ahb",
503 	    AHB_GATING_REG1, 1),
504 	SUNXI_CCU_GATE(A10_CLK_AHB_TVE0, "ahb-tve0", "ahb",
505 	    AHB_GATING_REG1, 2),
506 	SUNXI_CCU_GATE(A10_CLK_AHB_TVE1, "ahb-tve1", "ahb",
507 	    AHB_GATING_REG1, 3),
508 	SUNXI_CCU_GATE(A10_CLK_AHB_LCD0, "ahb-lcd0", "ahb",
509 	    AHB_GATING_REG1, 4),
510 	SUNXI_CCU_GATE(A10_CLK_AHB_LCD1, "ahb-lcd1", "ahb",
511 	    AHB_GATING_REG1, 5),
512 	SUNXI_CCU_GATE(A10_CLK_AHB_CSI0, "ahb-csi0", "ahb",
513 	    AHB_GATING_REG1, 8),
514 	SUNXI_CCU_GATE(A10_CLK_AHB_CSI1, "ahb-csi1", "ahb",
515 	    AHB_GATING_REG1, 9),
516 	SUNXI_CCU_GATE(A10_CLK_AHB_HDMI1, "ahb-hdmi1", "ahb",
517 	    AHB_GATING_REG1, 10),
518 	SUNXI_CCU_GATE(A10_CLK_AHB_HDMI0, "ahb-hdmi0", "ahb",
519 	    AHB_GATING_REG1, 11),
520 	SUNXI_CCU_GATE(A10_CLK_AHB_DE_BE0, "ahb-de_be0", "ahb",
521 	    AHB_GATING_REG1, 12),
522 	SUNXI_CCU_GATE(A10_CLK_AHB_DE_BE1, "ahb-de_be1", "ahb",
523 	    AHB_GATING_REG1, 13),
524 	SUNXI_CCU_GATE(A10_CLK_AHB_DE_FE0, "ahb-de_fe0", "ahb",
525 	    AHB_GATING_REG1, 14),
526 	SUNXI_CCU_GATE(A10_CLK_AHB_DE_FE1, "ahb-de_fe1", "ahb",
527 	    AHB_GATING_REG1, 15),
528 	SUNXI_CCU_GATE(A10_CLK_AHB_GMAC, "ahb-gmac", "ahb",
529 	    AHB_GATING_REG1, 17),
530 	SUNXI_CCU_GATE(A10_CLK_AHB_MP, "ahb-mp", "ahb",
531 	    AHB_GATING_REG1, 18),
532 	SUNXI_CCU_GATE(A10_CLK_AHB_GPU, "ahb-gpu", "ahb",
533 	    AHB_GATING_REG1, 20),
534 
535 	/* APB0_GATING_REG */
536 	SUNXI_CCU_GATE(A10_CLK_APB0_CODEC, "apb0-codec", "apb0",
537 	    APB0_GATING_REG, 0),
538 	SUNXI_CCU_GATE(A10_CLK_APB0_SPDIF, "apb0-spdif", "apb0",
539 	    APB0_GATING_REG, 1),
540 	SUNXI_CCU_GATE(A10_CLK_APB0_AC97, "apb0-ac97", "apb0",
541 	    APB0_GATING_REG, 2),
542 	SUNXI_CCU_GATE(A10_CLK_APB0_I2S0, "apb0-i2s0", "apb0",
543 	    APB0_GATING_REG, 3),
544 	SUNXI_CCU_GATE(A10_CLK_APB0_I2S1, "apb0-i2s1", "apb0",
545 	    APB0_GATING_REG, 4),
546 	SUNXI_CCU_GATE(A10_CLK_APB0_PIO, "apb0-pio", "apb0",
547 	    APB0_GATING_REG, 5),
548 	SUNXI_CCU_GATE(A10_CLK_APB0_IR0, "apb0-ir0", "apb0",
549 	    APB0_GATING_REG, 6),
550 	SUNXI_CCU_GATE(A10_CLK_APB0_IR1, "apb0-ir1", "apb0",
551 	    APB0_GATING_REG, 7),
552 	SUNXI_CCU_GATE(A10_CLK_APB0_I2S2, "apb0-i2s2", "apb0",
553 	    APB0_GATING_REG, 8),
554 	SUNXI_CCU_GATE(A10_CLK_APB0_KEYPAD, "apb0-keypad", "apb0",
555 	    APB0_GATING_REG, 10),
556 
557 	/* APB1_GATING_REG */
558 	SUNXI_CCU_GATE(A10_CLK_APB1_I2C0, "apb1-i2c0", "apb1",
559 	    APB1_GATING_REG, 0),
560 	SUNXI_CCU_GATE(A10_CLK_APB1_I2C1, "apb1-i2c1", "apb1",
561 	    APB1_GATING_REG, 1),
562 	SUNXI_CCU_GATE(A10_CLK_APB1_I2C2, "apb1-i2c2", "apb1",
563 	    APB1_GATING_REG, 2),
564 	SUNXI_CCU_GATE(A10_CLK_APB1_I2C3, "apb1-i2c3", "apb1",
565 	    APB1_GATING_REG, 3),
566 	SUNXI_CCU_GATE(A10_CLK_APB1_CAN, "apb1-can", "apb1",
567 	    APB1_GATING_REG, 4),
568 	SUNXI_CCU_GATE(A10_CLK_APB1_SCR, "apb1-scr", "apb1",
569 	    APB1_GATING_REG, 5),
570 	SUNXI_CCU_GATE(A10_CLK_APB1_PS20, "apb1-ps20", "apb1",
571 	    APB1_GATING_REG, 6),
572 	SUNXI_CCU_GATE(A10_CLK_APB1_PS21, "apb1-ps21", "apb1",
573 	    APB1_GATING_REG, 7),
574 	SUNXI_CCU_GATE(A10_CLK_APB1_I2C4, "apb1-i2c4", "apb1",
575 	    APB1_GATING_REG, 15),
576 	SUNXI_CCU_GATE(A10_CLK_APB1_UART0, "apb1-uart0", "apb1",
577 	    APB1_GATING_REG, 16),
578 	SUNXI_CCU_GATE(A10_CLK_APB1_UART1, "apb1-uart1", "apb1",
579 	    APB1_GATING_REG, 17),
580 	SUNXI_CCU_GATE(A10_CLK_APB1_UART2, "apb1-uart2", "apb1",
581 	    APB1_GATING_REG, 18),
582 	SUNXI_CCU_GATE(A10_CLK_APB1_UART3, "apb1-uart3", "apb1",
583 	    APB1_GATING_REG, 19),
584 	SUNXI_CCU_GATE(A10_CLK_APB1_UART4, "apb1-uart4", "apb1",
585 	    APB1_GATING_REG, 20),
586 	SUNXI_CCU_GATE(A10_CLK_APB1_UART5, "apb1-uart5", "apb1",
587 	    APB1_GATING_REG, 21),
588 	SUNXI_CCU_GATE(A10_CLK_APB1_UART6, "apb1-uart6", "apb1",
589 	    APB1_GATING_REG, 22),
590 	SUNXI_CCU_GATE(A10_CLK_APB1_UART7, "apb1-uart7", "apb1",
591 	    APB1_GATING_REG, 23),
592 
593 	/* DRAM GATING */
594 	SUNXI_CCU_GATE(A10_CLK_DRAM_DE_BE0, "dram-de-be0", "pll_ddr_other",
595 	    DRAM_GATING_REG, 26),
596 	SUNXI_CCU_GATE(A10_CLK_DRAM_DE_BE1, "dram-de-be1", "pll_ddr_other",
597 	    DRAM_GATING_REG, 27),
598 	SUNXI_CCU_GATE(A10_CLK_DRAM_DE_FE0, "dram-de-fe0", "pll_ddr_other",
599 	    DRAM_GATING_REG, 25),
600 	SUNXI_CCU_GATE(A10_CLK_DRAM_DE_FE1, "dram-de-fe1", "pll_ddr_other",
601 	    DRAM_GATING_REG, 24),
602 
603 	/* AUDIO_CODEC_SCLK_CFG_REG */
604 	SUNXI_CCU_GATE(A10_CLK_CODEC, "codec", "pll_audio",
605 	    AUDIO_CODEC_SCLK_CFG_REG, 31),
606 
607 	/* USBPHY_CFG_REG */
608 	SUNXI_CCU_GATE(A10_CLK_USB_OHCI0, "usb-ohci0", "osc24m",
609 	    USBPHY_CFG_REG, 6),
610 	SUNXI_CCU_GATE(A10_CLK_USB_OHCI1, "usb-ohci1", "osc24m",
611 	    USBPHY_CFG_REG, 7),
612 	SUNXI_CCU_GATE(A10_CLK_USB_PHY, "usb-phy", "osc24m",
613 	    USBPHY_CFG_REG, 8),
614 };
615 
616 /*
617  * some special cases
618  * hardcode lcd0 (tcon0) to pll3 and lcd1 (tcon1) to pll7.
619  * compute pll rate based on desired pixel clock
620  */
621 
622 static int
623 sun4i_a10_ccu_lcd0ch0_set_rate(struct sunxi_ccu_softc *sc,
624     struct sunxi_ccu_clk * clk, u_int rate)
625 {
626 	int error;
627 	error = sunxi_ccu_lcdxch0_set_rate(sc, clk,
628 	    &sun4i_a10_ccu_clks[A10_CLK_PLL_VIDEO0],
629 	    &sun4i_a10_ccu_clks[A10_CLK_PLL_VIDEO0_2X],
630 	    rate);
631 	return error;
632 }
633 
634 static int
635 sun4i_a10_ccu_lcd1ch0_set_rate(struct sunxi_ccu_softc *sc,
636     struct sunxi_ccu_clk * clk, u_int rate)
637 {
638 	return sunxi_ccu_lcdxch0_set_rate(sc, clk,
639 	    &sun4i_a10_ccu_clks[A10_CLK_PLL_VIDEO1],
640 	    &sun4i_a10_ccu_clks[A10_CLK_PLL_VIDEO1_2X],
641 	    rate);
642 }
643 
644 static u_int
645 sun4i_a10_ccu_lcd0ch0_round_rate(struct sunxi_ccu_softc *sc,
646     struct sunxi_ccu_clk * clk, u_int rate)
647 {
648 	return sunxi_ccu_lcdxch0_round_rate(sc, clk,
649 	    &sun4i_a10_ccu_clks[A10_CLK_PLL_VIDEO0],
650 	    &sun4i_a10_ccu_clks[A10_CLK_PLL_VIDEO0_2X],
651 	    rate);
652 }
653 
654 static u_int
655 sun4i_a10_ccu_lcd1ch0_round_rate(struct sunxi_ccu_softc *sc,
656     struct sunxi_ccu_clk * clk, u_int rate)
657 {
658 	return sunxi_ccu_lcdxch0_round_rate(sc, clk,
659 	    &sun4i_a10_ccu_clks[A10_CLK_PLL_VIDEO1],
660 	    &sun4i_a10_ccu_clks[A10_CLK_PLL_VIDEO1_2X],
661 	    rate);
662 }
663 
664 static int
665 sun4i_a10_ccu_lcd0ch1_set_rate(struct sunxi_ccu_softc *sc,
666     struct sunxi_ccu_clk * clk, u_int rate)
667 {
668 	return sunxi_ccu_lcdxch1_set_rate(sc, clk,
669 	    &sun4i_a10_ccu_clks[A10_CLK_PLL_VIDEO0],
670 	    &sun4i_a10_ccu_clks[A10_CLK_PLL_VIDEO0_2X],
671 	    rate);
672 }
673 
674 static int
675 sun4i_a10_ccu_lcd1ch1_set_rate(struct sunxi_ccu_softc *sc,
676     struct sunxi_ccu_clk * clk, u_int rate)
677 {
678 	return sunxi_ccu_lcdxch1_set_rate(sc, clk,
679 	    &sun4i_a10_ccu_clks[A10_CLK_PLL_VIDEO1],
680 	    &sun4i_a10_ccu_clks[A10_CLK_PLL_VIDEO1_2X],
681 	    rate);
682 }
683 
684 #if 0
685 static int
686 sun4i_a10_ccu_lcdxch0_set_rate(struct sunxi_ccu_softc *sc,
687     struct sunxi_ccu_clk * clk, u_int rate, int unit)
688 {
689 	int parent_index;
690 	struct clk *clkp;
691 	int error;
692 
693 	parent_index = (unit == 0) ? A10_CLK_PLL_VIDEO0 : A10_CLK_PLL_VIDEO1;
694 	clkp = &sun4i_a10_ccu_clks[parent_index].base;
695 	error = clk_set_rate(clkp, rate);
696 	if (error) {
697 		error = clk_set_rate(clkp, rate / 2);
698 		if (error != 0)
699 			return error;
700 		parent_index =
701 		    (unit == 0) ? A10_CLK_PLL_VIDEO0_2X : A10_CLK_PLL_VIDEO1_2X;
702 		clkp = &sun4i_a10_ccu_clks[parent_index].base;
703 	}
704 	error = clk_set_parent(&clk->base, clkp);
705 	KASSERT(error == 0);
706 	return error;
707 }
708 
709 static u_int
710 sun4i_a10_ccu_lcdxch0_round_rate(struct sunxi_ccu_softc *sc,
711     struct sunxi_ccu_clk * clk, u_int try_rate, int unit)
712 {
713 	int parent_index;
714 	struct clk *clkp;
715 	int diff, diff_x2;
716 	int rate, rate_x2;
717 
718 	parent_index = (unit == 0) ? A10_CLK_PLL_VIDEO0 : A10_CLK_PLL_VIDEO1;
719 	clkp = &sun4i_a10_ccu_clks[parent_index].base;
720 	rate = clk_round_rate(clkp, try_rate);
721 	diff = abs(try_rate - rate);
722 
723 	rate_x2 = (clk_round_rate(clkp, try_rate / 2) * 2);
724 	diff_x2 = abs(try_rate - rate_x2);
725 
726 	if (diff_x2 < diff)
727 		return rate_x2;
728 	return rate;
729 }
730 
731 static void
732 sun4i_a10_tcon_calc_pll(int f_ref, int f_out, int *pm, int *pn, int *pd)
733 {
734 	int best = INT_MAX;
735 	for (int d = 1; d <= 2 && best != 0; d++) {
736 		for (int m = 1; m <= 16 && best != 0; m++) {
737 			for (int n = 9; n <= 127 && best != 0; n++) {
738 				int f_cur = (n * f_ref * d) / m;
739 				int diff = abs(f_out - f_cur);
740 				if (diff < best) {
741 					best = diff;
742 					*pm = m;
743 					*pn = n;
744 					*pd = d;
745 					if (diff == 0)
746 						return;
747 				}
748 			}
749 		}
750 	}
751 }
752 
753 static int
754 sun4i_a10_ccu_lcdxch1_set_rate(struct sunxi_ccu_softc *sc,
755     struct sunxi_ccu_clk *clk, u_int rate, int unit)
756 {
757 	int parent_index;
758 	struct clk *clkp, *pllclk;
759 	int error;
760         int n = 0, m = 0, d = 0;
761 
762 	parent_index = (unit == 0) ? A10_CLK_PLL_VIDEO0 : A10_CLK_PLL_VIDEO1;
763 	clkp = &sun4i_a10_ccu_clks[parent_index].base;
764 	pllclk = clkp;
765 
766         sun4i_a10_tcon_calc_pll(3000000, rate, &m, &n, &d);
767 
768         if (n == 0 || m == 0 || d == 0)
769 		return ERANGE;
770 
771         if (d == 2) {
772 		parent_index =
773 		    (unit == 0) ? A10_CLK_PLL_VIDEO0_2X : A10_CLK_PLL_VIDEO1_2X;
774 		clkp = &sun4i_a10_ccu_clks[parent_index].base;
775         }
776 
777 	error = clk_set_rate(pllclk, 3000000 * n);
778 	KASSERT(error == 0);
779 	error = clk_set_parent(&clk->base, clkp);
780 	KASSERT(error == 0);
781 	error = sunxi_ccu_div_set_rate(sc, clk, rate);
782 	KASSERT(error == 0);
783 	return error;
784 }
785 #endif
786 
787 static int
788 sun4i_a10_ccu_match(device_t parent, cfdata_t cf, void *aux)
789 {
790 	struct fdt_attach_args * const faa = aux;
791 
792 	return of_match_compat_data(faa->faa_phandle, compat_data);
793 }
794 
795 static struct sunxi_ccu_softc *sc0;
796 static void
797 sun4i_a10_ccu_attach(device_t parent, device_t self, void *aux)
798 {
799 	struct sunxi_ccu_softc * const sc = device_private(self);
800 	struct fdt_attach_args * const faa = aux;
801 	enum sun4i_a10_ccu_type type;
802 	struct clk *clk, *clkp;
803 	int error;
804 
805 	sc->sc_dev = self;
806 	sc->sc_phandle = faa->faa_phandle;
807 	sc->sc_bst = faa->faa_bst;
808 
809 	sc->sc_resets = sun4i_a10_ccu_resets;
810 	sc->sc_nresets = __arraycount(sun4i_a10_ccu_resets);
811 
812 	sc->sc_clks = sun4i_a10_ccu_clks;
813 	sc->sc_nclks = __arraycount(sun4i_a10_ccu_clks);
814 
815 	if (sunxi_ccu_attach(sc) != 0)
816 		return;
817 
818 	aprint_naive("\n");
819 
820 	type = of_search_compatible(faa->faa_phandle, compat_data)->data;
821 
822 	switch (type) {
823 	case CCU_A10:
824 		aprint_normal(": A10 CCU\n");
825 		break;
826 	case CCU_A20:
827 		aprint_normal(": A20 CCU\n");
828 		break;
829 	}
830 	/* hardcode debe clocks parent to PLL5 */
831 	clkp = &sun4i_a10_ccu_clks[A10_CLK_PLL_DDR_BASE].base;
832 	clk =  &sun4i_a10_ccu_clks[A10_CLK_DE_BE0].base;
833 	error = clk_set_parent(clk, clkp);
834 	KASSERT(error == 0);
835 	clk =  &sun4i_a10_ccu_clks[A10_CLK_DE_BE1].base;
836 	error = clk_set_parent(clk, clkp);
837 	KASSERT(error == 0);
838 
839 	(void)error;
840 	sunxi_ccu_print(sc);
841 	sc0 = sc;
842 }
843 
844 void sun4i_ccu_print(void);
845 void
846 sun4i_ccu_print(void)
847 {
848 	sunxi_ccu_print(sc0);
849 }
850