2 * Copyright (c) 2017 Emmanuel Vadot <manu@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <dev/extres/clk/clk_div.h>
37 #include <dev/extres/clk/clk_fixed.h>
38 #include <dev/extres/clk/clk_mux.h>
40 #include <arm/allwinner/clkng/aw_ccung.h>
41 #include <arm/allwinner/clkng/aw_clk.h>
42 #include <arm/allwinner/clkng/aw_clk_nm.h>
43 #include <arm/allwinner/clkng/aw_clk_nkmp.h>
44 #include <arm/allwinner/clkng/aw_clk_prediv_mux.h>
46 #include <dt-bindings/clock/sun5i-ccu.h>
47 #include <dt-bindings/reset/sun5i-ccu.h>
51 /* Non-exported clocks */
53 #define CLK_PLL_CORE 2
54 #define CLK_PLL_AUDIO_BASE 3
55 #define CLK_PLL_AUDIO 4
56 #define CLK_PLL_AUDIO_2X 5
57 #define CLK_PLL_AUDIO_4X 6
58 #define CLK_PLL_AUDIO_8X 7
59 #define CLK_PLL_VIDEO0 8
62 #define CLK_PLL_DDR_BASE 11
63 #define CLK_PLL_DDR 12
64 #define CLK_PLL_DDR_OTHER 13
65 #define CLK_PLL_PERIPH 14
66 #define CLK_PLL_VIDEO1 15
72 #define CLK_DRAM_AXI 22
74 #define CLK_TCON_CH1_SCLK 91
78 static struct aw_ccung_reset a13_ccu_resets[] = {
79 CCU_RESET(RST_USB_PHY0, 0xcc, 0)
80 CCU_RESET(RST_USB_PHY1, 0xcc, 1)
82 CCU_RESET(RST_GPS, 0xd0, 30)
84 CCU_RESET(RST_DE_BE, 0x104, 30)
86 CCU_RESET(RST_DE_FE, 0x10c, 30)
88 CCU_RESET(RST_TVE, 0x118, 29)
89 CCU_RESET(RST_LCD, 0x118, 30)
91 CCU_RESET(RST_CSI, 0x134, 30)
93 CCU_RESET(RST_VE, 0x13c, 0)
94 CCU_RESET(RST_GPU, 0x154, 30)
95 CCU_RESET(RST_IEP, 0x160, 30)
99 static struct aw_ccung_gate a13_ccu_gates[] = {
100 CCU_GATE(CLK_HOSC, "hosc", "osc24M", 0x50, 0)
102 CCU_GATE(CLK_DRAM_AXI, "axi-dram", "axi", 0x5c, 0)
104 CCU_GATE(CLK_AHB_OTG, "ahb-otg", "ahb", 0x60, 0)
105 CCU_GATE(CLK_AHB_EHCI, "ahb-ehci", "ahb", 0x60, 1)
106 CCU_GATE(CLK_AHB_OHCI, "ahb-ohci", "ahb", 0x60, 2)
107 CCU_GATE(CLK_AHB_SS, "ahb-ss", "ahb", 0x60, 5)
108 CCU_GATE(CLK_AHB_DMA, "ahb-dma", "ahb", 0x60, 6)
109 CCU_GATE(CLK_AHB_BIST, "ahb-bist", "ahb", 0x60, 7)
110 CCU_GATE(CLK_AHB_MMC0, "ahb-mmc0", "ahb", 0x60, 8)
111 CCU_GATE(CLK_AHB_MMC1, "ahb-mmc1", "ahb", 0x60, 9)
112 CCU_GATE(CLK_AHB_MMC2, "ahb-mmc2", "ahb", 0x60, 10)
113 CCU_GATE(CLK_AHB_NAND, "ahb-nand", "ahb", 0x60, 13)
114 CCU_GATE(CLK_AHB_SDRAM, "ahb-sdram", "ahb", 0x60, 14)
115 CCU_GATE(CLK_AHB_SPI0, "ahb-spi0", "ahb", 0x60, 20)
116 CCU_GATE(CLK_AHB_SPI1, "ahb-spi1", "ahb", 0x60, 21)
117 CCU_GATE(CLK_AHB_SPI2, "ahb-spi2", "ahb", 0x60, 22)
118 CCU_GATE(CLK_AHB_GPS, "ahb-gps", "ahb", 0x60, 26)
119 CCU_GATE(CLK_AHB_HSTIMER, "ahb-hstimer", "ahb", 0x60, 28)
121 CCU_GATE(CLK_AHB_VE, "ahb-ve", "ahb", 0x64, 0)
122 CCU_GATE(CLK_AHB_LCD, "ahb-lcd", "ahb", 0x64, 4)
123 CCU_GATE(CLK_AHB_CSI, "ahb-csi", "ahb", 0x64, 8)
124 CCU_GATE(CLK_AHB_DE_BE, "ahb-de-be", "ahb", 0x64, 12)
125 CCU_GATE(CLK_AHB_DE_FE, "ahb-de-fe", "ahb", 0x64, 14)
126 CCU_GATE(CLK_AHB_IEP, "ahb-iep", "ahb", 0x64, 19)
127 CCU_GATE(CLK_AHB_GPU, "ahb-gpu", "ahb", 0x64, 20)
129 CCU_GATE(CLK_APB0_CODEC, "apb0-codec", "apb0", 0x68, 0)
130 CCU_GATE(CLK_APB0_PIO, "apb0-pio", "apb0", 0x68, 5)
131 CCU_GATE(CLK_APB0_IR, "apb0-ir", "apb0", 0x68, 6)
133 CCU_GATE(CLK_APB1_I2C0, "apb1-i2c0", "apb1", 0x6c, 0)
134 CCU_GATE(CLK_APB1_I2C1, "apb1-i2c1", "apb1", 0x6c, 1)
135 CCU_GATE(CLK_APB1_I2C2, "apb1-i2c2", "apb1", 0x6c, 2)
136 CCU_GATE(CLK_APB1_UART1, "apb1-uart1", "apb1", 0x6c, 17)
137 CCU_GATE(CLK_APB1_UART3, "apb1-uart3", "apb1", 0x6c, 19)
139 CCU_GATE(CLK_DRAM_VE, "dram-ve", "pll-ddr", 0x100, 0)
140 CCU_GATE(CLK_DRAM_CSI, "dram-csi", "pll-ddr", 0x100, 1)
141 CCU_GATE(CLK_DRAM_DE_FE, "dram-de-fe", "pll-ddr", 0x100, 25)
142 CCU_GATE(CLK_DRAM_DE_BE, "dram-de-be", "pll-ddr", 0x100, 26)
143 CCU_GATE(CLK_DRAM_ACE, "dram-ace", "pll-ddr", 0x100, 29)
144 CCU_GATE(CLK_DRAM_IEP, "dram-iep", "pll-ddr", 0x100, 31)
146 CCU_GATE(CLK_CODEC, "codec", "pll-audio", 0x140, 31)
148 CCU_GATE(CLK_AVS, "avs", "hosc", 0x144, 31)
151 static const char *pll_parents[] = {"hosc"};
152 static struct aw_clk_nkmp_def pll_core = {
156 .parent_names = pll_parents,
157 .parent_cnt = nitems(pll_parents),
160 .n = {.shift = 8, .width = 5},
161 .k = {.shift = 4, .width = 2},
162 .m = {.shift = 0, .width = 2},
163 .p = {.shift = 16, .width = 2},
165 .flags = AW_CLK_HAS_GATE,
169 * We only implement pll-audio for now
170 * For pll-audio-2/4/8 x we need a way to change the frequency
171 * of the parent clocks
173 static struct aw_clk_nkmp_def pll_audio = {
177 .parent_names = pll_parents,
178 .parent_cnt = nitems(pll_parents),
181 .n = {.shift = 8, .width = 7},
182 .k = {.value = 1, .flags = AW_CLK_FACTOR_FIXED},
183 .m = {.shift = 0, .width = 5},
184 .p = {.shift = 26, .width = 4},
186 .flags = AW_CLK_HAS_GATE,
189 /* Missing PLL3-Video */
190 /* Missing PLL4-VE */
192 static struct aw_clk_nkmp_def pll_ddr_base = {
194 .id = CLK_PLL_DDR_BASE,
195 .name = "pll-ddr-base",
196 .parent_names = pll_parents,
197 .parent_cnt = nitems(pll_parents),
200 .n = {.shift = 8, .width = 5},
201 .k = {.shift = 4, .width = 2},
202 .m = {.value = 1, .flags = AW_CLK_FACTOR_FIXED},
203 .p = {.value = 1, .flags = AW_CLK_FACTOR_FIXED},
205 .flags = AW_CLK_HAS_GATE,
208 static const char *pll_ddr_parents[] = {"pll-ddr-base"};
209 static struct clk_div_def pll_ddr = {
213 .parent_names = pll_ddr_parents,
214 .parent_cnt = nitems(pll_ddr_parents),
221 static const char *pll_ddr_other_parents[] = {"pll-ddr-base"};
222 static struct clk_div_def pll_ddr_other = {
224 .id = CLK_PLL_DDR_OTHER,
225 .name = "pll-ddr-other",
226 .parent_names = pll_ddr_other_parents,
227 .parent_cnt = nitems(pll_ddr_other_parents),
234 static struct aw_clk_nkmp_def pll_periph = {
236 .id = CLK_PLL_PERIPH,
237 .name = "pll-periph",
238 .parent_names = pll_parents,
239 .parent_cnt = nitems(pll_parents),
242 .n = {.shift = 8, .width = 5},
243 .k = {.shift = 4, .width = 2},
244 .m = {.shift = 0, .width = 2},
245 .p = {.value = 2, .flags = AW_CLK_FACTOR_FIXED},
247 .flags = AW_CLK_HAS_GATE,
250 /* Missing PLL7-VIDEO1 */
252 static const char *cpu_parents[] = {"osc32k", "hosc", "pll-core", "pll-periph"};
253 static struct aw_clk_prediv_mux_def cpu_clk = {
257 .parent_names = cpu_parents,
258 .parent_cnt = nitems(cpu_parents),
261 .mux_shift = 16, .mux_width = 2,
264 .flags = AW_CLK_FACTOR_FIXED,
271 static const char *axi_parents[] = {"cpu"};
272 static struct clk_div_def axi_clk = {
276 .parent_names = axi_parents,
277 .parent_cnt = nitems(axi_parents),
280 .i_shift = 0, .i_width = 2,
283 static const char *ahb_parents[] = {"axi", "cpu", "pll-periph"};
284 static struct aw_clk_prediv_mux_def ahb_clk = {
288 .parent_names = ahb_parents,
289 .parent_cnt = nitems(ahb_parents),
297 .flags = AW_CLK_FACTOR_POWER_OF_TWO
301 .flags = AW_CLK_FACTOR_FIXED,
308 static const char *apb0_parents[] = {"ahb"};
309 static struct clk_div_table apb0_div_table[] = {
310 { .value = 0, .divider = 2, },
311 { .value = 1, .divider = 2, },
312 { .value = 2, .divider = 4, },
313 { .value = 3, .divider = 8, },
316 static struct clk_div_def apb0_clk = {
320 .parent_names = apb0_parents,
321 .parent_cnt = nitems(apb0_parents),
324 .i_shift = 8, .i_width = 2,
325 .div_flags = CLK_DIV_WITH_TABLE,
326 .div_table = apb0_div_table,
329 static const char *apb1_parents[] = {"hosc", "pll-periph", "osc32k"};
330 static struct aw_clk_nm_def apb1_clk = {
334 .parent_names = apb1_parents,
335 .parent_cnt = nitems(apb1_parents),
338 .n = {.shift = 16, .width = 2, .flags = AW_CLK_FACTOR_POWER_OF_TWO, },
339 .m = {.shift = 0, .width = 5},
342 .flags = AW_CLK_HAS_MUX,
345 static const char *mod_parents[] = {"hosc", "pll-periph", "pll-ddr-other"};
347 static struct aw_clk_nm_def nand_clk = {
351 .parent_names = mod_parents,
352 .parent_cnt = nitems(mod_parents),
355 .n = {.shift = 16, .width = 2, .flags = AW_CLK_FACTOR_POWER_OF_TWO, },
356 .m = {.shift = 0, .width = 4},
360 .flags = AW_CLK_HAS_MUX | AW_CLK_HAS_GATE | AW_CLK_REPARENT
363 static struct aw_clk_nm_def mmc0_clk = {
367 .parent_names = mod_parents,
368 .parent_cnt = nitems(mod_parents),
371 .n = {.shift = 16, .width = 2, .flags = AW_CLK_FACTOR_POWER_OF_TWO, },
372 .m = {.shift = 0, .width = 4},
376 .flags = AW_CLK_HAS_MUX | AW_CLK_HAS_GATE | AW_CLK_REPARENT
379 static struct aw_clk_nm_def mmc1_clk = {
383 .parent_names = mod_parents,
384 .parent_cnt = nitems(mod_parents),
387 .n = {.shift = 16, .width = 2, .flags = AW_CLK_FACTOR_POWER_OF_TWO, },
388 .m = {.shift = 0, .width = 4},
392 .flags = AW_CLK_HAS_MUX | AW_CLK_HAS_GATE | AW_CLK_REPARENT
395 static struct aw_clk_nm_def mmc2_clk = {
399 .parent_names = mod_parents,
400 .parent_cnt = nitems(mod_parents),
403 .n = {.shift = 16, .width = 2, .flags = AW_CLK_FACTOR_POWER_OF_TWO, },
404 .m = {.shift = 0, .width = 4},
408 .flags = AW_CLK_HAS_MUX | AW_CLK_HAS_GATE | AW_CLK_REPARENT
411 static struct aw_clk_nm_def ss_clk = {
415 .parent_names = mod_parents,
416 .parent_cnt = nitems(mod_parents),
419 .n = {.shift = 16, .width = 2, .flags = AW_CLK_FACTOR_POWER_OF_TWO, },
420 .m = {.shift = 0, .width = 4},
424 .flags = AW_CLK_HAS_MUX | AW_CLK_HAS_GATE | AW_CLK_REPARENT
427 static struct aw_clk_nm_def spi0_clk = {
431 .parent_names = mod_parents,
432 .parent_cnt = nitems(mod_parents),
435 .n = {.shift = 16, .width = 2, .flags = AW_CLK_FACTOR_POWER_OF_TWO, },
436 .m = {.shift = 0, .width = 4},
440 .flags = AW_CLK_HAS_MUX | AW_CLK_HAS_GATE | AW_CLK_REPARENT
443 static struct aw_clk_nm_def spi1_clk = {
447 .parent_names = mod_parents,
448 .parent_cnt = nitems(mod_parents),
451 .n = {.shift = 16, .width = 2, .flags = AW_CLK_FACTOR_POWER_OF_TWO, },
452 .m = {.shift = 0, .width = 4},
456 .flags = AW_CLK_HAS_MUX | AW_CLK_HAS_GATE | AW_CLK_REPARENT
459 static struct aw_clk_nm_def spi2_clk = {
463 .parent_names = mod_parents,
464 .parent_cnt = nitems(mod_parents),
467 .n = {.shift = 16, .width = 2, .flags = AW_CLK_FACTOR_POWER_OF_TWO, },
468 .m = {.shift = 0, .width = 4},
472 .flags = AW_CLK_HAS_MUX | AW_CLK_HAS_GATE | AW_CLK_REPARENT
475 static struct aw_clk_nm_def ir_clk = {
479 .parent_names = mod_parents,
480 .parent_cnt = nitems(mod_parents),
483 .n = {.shift = 16, .width = 2, .flags = AW_CLK_FACTOR_POWER_OF_TWO, },
484 .m = {.shift = 0, .width = 4},
488 .flags = AW_CLK_HAS_MUX | AW_CLK_HAS_GATE | AW_CLK_REPARENT
491 /* Missing DE-BE clock */
492 /* Missing DE-FE clock */
493 /* Missing LCD CH1 clock */
494 /* Missing CSI clock */
495 /* Missing VE clock */
500 static struct aw_clk_nkmp_def *nkmp_clks[] = {
507 static struct aw_clk_nm_def *nm_clks[] = {
520 static struct aw_clk_prediv_mux_def *prediv_mux_clks[] = {
525 static struct clk_div_def *div_clks[] = {
532 static struct clk_mux_def *mux_clks[] = {
535 static struct clk_fixed_def *fixed_factor_clks[] = {
538 static struct aw_clk_init init_clks[] = {
542 ccu_a13_register_clocks(struct aw_ccung_softc *sc)
546 sc->resets = a13_ccu_resets;
547 sc->nresets = nitems(a13_ccu_resets);
548 sc->gates = a13_ccu_gates;
549 sc->ngates = nitems(a13_ccu_gates);
550 sc->clk_init = init_clks;
551 sc->n_clk_init = nitems(init_clks);
553 for (i = 0; i < nitems(nkmp_clks); i++)
554 aw_clk_nkmp_register(sc->clkdom, nkmp_clks[i]);
555 for (i = 0; i < nitems(nm_clks); i++)
556 aw_clk_nm_register(sc->clkdom, nm_clks[i]);
557 for (i = 0; i < nitems(prediv_mux_clks); i++)
558 aw_clk_prediv_mux_register(sc->clkdom, prediv_mux_clks[i]);
560 for (i = 0; i < nitems(mux_clks); i++)
561 clknode_mux_register(sc->clkdom, mux_clks[i]);
562 for (i = 0; i < nitems(div_clks); i++)
563 clknode_div_register(sc->clkdom, div_clks[i]);
564 for (i = 0; i < nitems(fixed_factor_clks); i++)
565 clknode_fixed_register(sc->clkdom, fixed_factor_clks[i]);