sunxi_de2_ccu.c revision 1.2.2.2 1 /* $NetBSD: sunxi_de2_ccu.c,v 1.2.2.2 2019/01/26 22:00:01 pgoyette Exp $ */
2
3 /*-
4 * Copyright (c) 2019 Jared McNeill <jmcneill (at) 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: sunxi_de2_ccu.c,v 1.2.2.2 2019/01/26 22:00:01 pgoyette 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/sunxi_de2_ccu.h>
42
43 static int sunxi_de2_ccu_match(device_t, cfdata_t, void *);
44 static void sunxi_de2_ccu_attach(device_t, device_t, void *);
45
46 CFATTACH_DECL_NEW(sunxi_de2ccu, sizeof(struct sunxi_ccu_softc),
47 sunxi_de2_ccu_match, sunxi_de2_ccu_attach, NULL, NULL);
48
49 static struct sunxi_ccu_reset sun50i_a64_de2_ccu_resets[] = {
50 SUNXI_CCU_RESET(DE2_RST_MIXER0, 0x08, 0),
51 SUNXI_CCU_RESET(DE2_RST_MIXER1, 0x08, 1),
52 SUNXI_CCU_RESET(DE2_RST_WB, 0x08, 2),
53 };
54
55 static const char *mod_parents[] = { "mod" };
56
57 static struct sunxi_ccu_clk sun50i_a64_de2_ccu_clks[] = {
58 SUNXI_CCU_GATE(DE2_CLK_BUS_MIXER0, "bus-mixer0", "bus", 0x04, 0),
59 SUNXI_CCU_GATE(DE2_CLK_BUS_MIXER1, "bus-mixer1", "bus", 0x04, 1),
60 SUNXI_CCU_GATE(DE2_CLK_BUS_WB, "bus-wb", "bus", 0x04, 2),
61
62 SUNXI_CCU_DIV(DE2_CLK_MIXER0_DIV, "mixer0-div", mod_parents,
63 0x0c, __BITS(3,0), 0, SUNXI_CCU_DIV_SET_RATE_PARENT),
64 SUNXI_CCU_DIV(DE2_CLK_MIXER1_DIV, "mixer1-div", mod_parents,
65 0x0c, __BITS(7,4), 0, SUNXI_CCU_DIV_SET_RATE_PARENT),
66 SUNXI_CCU_DIV(DE2_CLK_WB_DIV, "wb-div", mod_parents,
67 0x0c, __BITS(11,8), 0, SUNXI_CCU_DIV_SET_RATE_PARENT),
68
69 SUNXI_CCU_GATE(DE2_CLK_MIXER0, "mixer0", "mixer0-div", 0x00, 0),
70 SUNXI_CCU_GATE(DE2_CLK_MIXER1, "mixer1", "mixer1-div", 0x00, 1),
71 SUNXI_CCU_GATE(DE2_CLK_WB, "wb", "wb-div", 0x00, 2),
72 };
73
74 struct sunxi_de2_ccu_config {
75 struct sunxi_ccu_reset *resets;
76 u_int nresets;
77 struct sunxi_ccu_clk *clks;
78 u_int nclks;
79 };
80
81 static const struct sunxi_de2_ccu_config sun50i_a64_de2_config = {
82 .resets = sun50i_a64_de2_ccu_resets,
83 .nresets = __arraycount(sun50i_a64_de2_ccu_resets),
84 .clks = sun50i_a64_de2_ccu_clks,
85 .nclks = __arraycount(sun50i_a64_de2_ccu_clks),
86 };
87
88 static const struct of_compat_data compat_data[] = {
89 { "allwinner,sun50i-a64-de2-clk", (uintptr_t)&sun50i_a64_de2_config },
90 { NULL }
91 };
92
93 static int
94 sunxi_de2_ccu_match(device_t parent, cfdata_t cf, void *aux)
95 {
96 struct fdt_attach_args * const faa = aux;
97
98 return of_match_compat_data(faa->faa_phandle, compat_data);
99 }
100
101 static void
102 sunxi_de2_ccu_attach(device_t parent, device_t self, void *aux)
103 {
104 struct sunxi_ccu_softc * const sc = device_private(self);
105 struct fdt_attach_args * const faa = aux;
106 const int phandle = faa->faa_phandle;
107 const struct sunxi_de2_ccu_config *conf;
108 struct clk *clk_bus, *clk_mod;
109 struct fdtbus_reset *rst;
110
111 sc->sc_dev = self;
112 sc->sc_phandle = phandle;
113 sc->sc_bst = faa->faa_bst;
114
115 conf = (void *)of_search_compatible(phandle, compat_data)->data;
116
117 sc->sc_resets = conf->resets;
118 sc->sc_nresets = conf->nresets;
119 sc->sc_clks = conf->clks;
120 sc->sc_nclks = conf->nclks;
121
122 clk_bus = fdtbus_clock_get(phandle, "bus");
123 if (clk_bus == NULL || clk_enable(clk_bus) != 0) {
124 aprint_error(": couldn't enable bus clock\n");
125 return;
126 }
127 clk_mod = fdtbus_clock_get(phandle, "mod");
128 if (clk_mod == NULL || clk_enable(clk_mod) != 0) {
129 aprint_error(": couldn't enable mod clock\n");
130 return;
131 }
132 rst = fdtbus_reset_get_index(phandle, 0);
133 if (rst == NULL || fdtbus_reset_deassert(rst) != 0) {
134 aprint_error(": couldn't de-assert reset\n");
135 return;
136 }
137
138 if (sunxi_ccu_attach(sc) != 0)
139 return;
140
141 aprint_naive("\n");
142 aprint_normal(": DE2 CCU\n");
143
144 sunxi_ccu_print(sc);
145 }
146