sunxi_de2_ccu.c revision 1.2.2.2 1 1.2.2.2 pgoyette /* $NetBSD: sunxi_de2_ccu.c,v 1.2.2.2 2019/01/26 22:00:01 pgoyette Exp $ */
2 1.2.2.2 pgoyette
3 1.2.2.2 pgoyette /*-
4 1.2.2.2 pgoyette * Copyright (c) 2019 Jared McNeill <jmcneill (at) invisible.ca>
5 1.2.2.2 pgoyette * All rights reserved.
6 1.2.2.2 pgoyette *
7 1.2.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
8 1.2.2.2 pgoyette * modification, are permitted provided that the following conditions
9 1.2.2.2 pgoyette * are met:
10 1.2.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
11 1.2.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
12 1.2.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
14 1.2.2.2 pgoyette * documentation and/or other materials provided with the distribution.
15 1.2.2.2 pgoyette *
16 1.2.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.2.2.2 pgoyette * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.2.2.2 pgoyette * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.2.2.2 pgoyette * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.2.2.2 pgoyette * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.2.2.2 pgoyette * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.2.2.2 pgoyette * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.2.2.2 pgoyette * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.2.2.2 pgoyette * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.2.2.2 pgoyette * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.2.2.2 pgoyette * SUCH DAMAGE.
27 1.2.2.2 pgoyette */
28 1.2.2.2 pgoyette
29 1.2.2.2 pgoyette #include <sys/cdefs.h>
30 1.2.2.2 pgoyette
31 1.2.2.2 pgoyette __KERNEL_RCSID(1, "$NetBSD: sunxi_de2_ccu.c,v 1.2.2.2 2019/01/26 22:00:01 pgoyette Exp $");
32 1.2.2.2 pgoyette
33 1.2.2.2 pgoyette #include <sys/param.h>
34 1.2.2.2 pgoyette #include <sys/bus.h>
35 1.2.2.2 pgoyette #include <sys/device.h>
36 1.2.2.2 pgoyette #include <sys/systm.h>
37 1.2.2.2 pgoyette
38 1.2.2.2 pgoyette #include <dev/fdt/fdtvar.h>
39 1.2.2.2 pgoyette
40 1.2.2.2 pgoyette #include <arm/sunxi/sunxi_ccu.h>
41 1.2.2.2 pgoyette #include <arm/sunxi/sunxi_de2_ccu.h>
42 1.2.2.2 pgoyette
43 1.2.2.2 pgoyette static int sunxi_de2_ccu_match(device_t, cfdata_t, void *);
44 1.2.2.2 pgoyette static void sunxi_de2_ccu_attach(device_t, device_t, void *);
45 1.2.2.2 pgoyette
46 1.2.2.2 pgoyette CFATTACH_DECL_NEW(sunxi_de2ccu, sizeof(struct sunxi_ccu_softc),
47 1.2.2.2 pgoyette sunxi_de2_ccu_match, sunxi_de2_ccu_attach, NULL, NULL);
48 1.2.2.2 pgoyette
49 1.2.2.2 pgoyette static struct sunxi_ccu_reset sun50i_a64_de2_ccu_resets[] = {
50 1.2.2.2 pgoyette SUNXI_CCU_RESET(DE2_RST_MIXER0, 0x08, 0),
51 1.2.2.2 pgoyette SUNXI_CCU_RESET(DE2_RST_MIXER1, 0x08, 1),
52 1.2.2.2 pgoyette SUNXI_CCU_RESET(DE2_RST_WB, 0x08, 2),
53 1.2.2.2 pgoyette };
54 1.2.2.2 pgoyette
55 1.2.2.2 pgoyette static const char *mod_parents[] = { "mod" };
56 1.2.2.2 pgoyette
57 1.2.2.2 pgoyette static struct sunxi_ccu_clk sun50i_a64_de2_ccu_clks[] = {
58 1.2.2.2 pgoyette SUNXI_CCU_GATE(DE2_CLK_BUS_MIXER0, "bus-mixer0", "bus", 0x04, 0),
59 1.2.2.2 pgoyette SUNXI_CCU_GATE(DE2_CLK_BUS_MIXER1, "bus-mixer1", "bus", 0x04, 1),
60 1.2.2.2 pgoyette SUNXI_CCU_GATE(DE2_CLK_BUS_WB, "bus-wb", "bus", 0x04, 2),
61 1.2.2.2 pgoyette
62 1.2.2.2 pgoyette SUNXI_CCU_DIV(DE2_CLK_MIXER0_DIV, "mixer0-div", mod_parents,
63 1.2.2.2 pgoyette 0x0c, __BITS(3,0), 0, SUNXI_CCU_DIV_SET_RATE_PARENT),
64 1.2.2.2 pgoyette SUNXI_CCU_DIV(DE2_CLK_MIXER1_DIV, "mixer1-div", mod_parents,
65 1.2.2.2 pgoyette 0x0c, __BITS(7,4), 0, SUNXI_CCU_DIV_SET_RATE_PARENT),
66 1.2.2.2 pgoyette SUNXI_CCU_DIV(DE2_CLK_WB_DIV, "wb-div", mod_parents,
67 1.2.2.2 pgoyette 0x0c, __BITS(11,8), 0, SUNXI_CCU_DIV_SET_RATE_PARENT),
68 1.2.2.2 pgoyette
69 1.2.2.2 pgoyette SUNXI_CCU_GATE(DE2_CLK_MIXER0, "mixer0", "mixer0-div", 0x00, 0),
70 1.2.2.2 pgoyette SUNXI_CCU_GATE(DE2_CLK_MIXER1, "mixer1", "mixer1-div", 0x00, 1),
71 1.2.2.2 pgoyette SUNXI_CCU_GATE(DE2_CLK_WB, "wb", "wb-div", 0x00, 2),
72 1.2.2.2 pgoyette };
73 1.2.2.2 pgoyette
74 1.2.2.2 pgoyette struct sunxi_de2_ccu_config {
75 1.2.2.2 pgoyette struct sunxi_ccu_reset *resets;
76 1.2.2.2 pgoyette u_int nresets;
77 1.2.2.2 pgoyette struct sunxi_ccu_clk *clks;
78 1.2.2.2 pgoyette u_int nclks;
79 1.2.2.2 pgoyette };
80 1.2.2.2 pgoyette
81 1.2.2.2 pgoyette static const struct sunxi_de2_ccu_config sun50i_a64_de2_config = {
82 1.2.2.2 pgoyette .resets = sun50i_a64_de2_ccu_resets,
83 1.2.2.2 pgoyette .nresets = __arraycount(sun50i_a64_de2_ccu_resets),
84 1.2.2.2 pgoyette .clks = sun50i_a64_de2_ccu_clks,
85 1.2.2.2 pgoyette .nclks = __arraycount(sun50i_a64_de2_ccu_clks),
86 1.2.2.2 pgoyette };
87 1.2.2.2 pgoyette
88 1.2.2.2 pgoyette static const struct of_compat_data compat_data[] = {
89 1.2.2.2 pgoyette { "allwinner,sun50i-a64-de2-clk", (uintptr_t)&sun50i_a64_de2_config },
90 1.2.2.2 pgoyette { NULL }
91 1.2.2.2 pgoyette };
92 1.2.2.2 pgoyette
93 1.2.2.2 pgoyette static int
94 1.2.2.2 pgoyette sunxi_de2_ccu_match(device_t parent, cfdata_t cf, void *aux)
95 1.2.2.2 pgoyette {
96 1.2.2.2 pgoyette struct fdt_attach_args * const faa = aux;
97 1.2.2.2 pgoyette
98 1.2.2.2 pgoyette return of_match_compat_data(faa->faa_phandle, compat_data);
99 1.2.2.2 pgoyette }
100 1.2.2.2 pgoyette
101 1.2.2.2 pgoyette static void
102 1.2.2.2 pgoyette sunxi_de2_ccu_attach(device_t parent, device_t self, void *aux)
103 1.2.2.2 pgoyette {
104 1.2.2.2 pgoyette struct sunxi_ccu_softc * const sc = device_private(self);
105 1.2.2.2 pgoyette struct fdt_attach_args * const faa = aux;
106 1.2.2.2 pgoyette const int phandle = faa->faa_phandle;
107 1.2.2.2 pgoyette const struct sunxi_de2_ccu_config *conf;
108 1.2.2.2 pgoyette struct clk *clk_bus, *clk_mod;
109 1.2.2.2 pgoyette struct fdtbus_reset *rst;
110 1.2.2.2 pgoyette
111 1.2.2.2 pgoyette sc->sc_dev = self;
112 1.2.2.2 pgoyette sc->sc_phandle = phandle;
113 1.2.2.2 pgoyette sc->sc_bst = faa->faa_bst;
114 1.2.2.2 pgoyette
115 1.2.2.2 pgoyette conf = (void *)of_search_compatible(phandle, compat_data)->data;
116 1.2.2.2 pgoyette
117 1.2.2.2 pgoyette sc->sc_resets = conf->resets;
118 1.2.2.2 pgoyette sc->sc_nresets = conf->nresets;
119 1.2.2.2 pgoyette sc->sc_clks = conf->clks;
120 1.2.2.2 pgoyette sc->sc_nclks = conf->nclks;
121 1.2.2.2 pgoyette
122 1.2.2.2 pgoyette clk_bus = fdtbus_clock_get(phandle, "bus");
123 1.2.2.2 pgoyette if (clk_bus == NULL || clk_enable(clk_bus) != 0) {
124 1.2.2.2 pgoyette aprint_error(": couldn't enable bus clock\n");
125 1.2.2.2 pgoyette return;
126 1.2.2.2 pgoyette }
127 1.2.2.2 pgoyette clk_mod = fdtbus_clock_get(phandle, "mod");
128 1.2.2.2 pgoyette if (clk_mod == NULL || clk_enable(clk_mod) != 0) {
129 1.2.2.2 pgoyette aprint_error(": couldn't enable mod clock\n");
130 1.2.2.2 pgoyette return;
131 1.2.2.2 pgoyette }
132 1.2.2.2 pgoyette rst = fdtbus_reset_get_index(phandle, 0);
133 1.2.2.2 pgoyette if (rst == NULL || fdtbus_reset_deassert(rst) != 0) {
134 1.2.2.2 pgoyette aprint_error(": couldn't de-assert reset\n");
135 1.2.2.2 pgoyette return;
136 1.2.2.2 pgoyette }
137 1.2.2.2 pgoyette
138 1.2.2.2 pgoyette if (sunxi_ccu_attach(sc) != 0)
139 1.2.2.2 pgoyette return;
140 1.2.2.2 pgoyette
141 1.2.2.2 pgoyette aprint_naive("\n");
142 1.2.2.2 pgoyette aprint_normal(": DE2 CCU\n");
143 1.2.2.2 pgoyette
144 1.2.2.2 pgoyette sunxi_ccu_print(sc);
145 1.2.2.2 pgoyette }
146