2 * Copyright (c) 2016 Michal Meloun <mmel@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 AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, 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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
34 #include <sys/mutex.h>
37 #include <machine/bus.h>
39 #include <dev/extres/clk/clk.h>
41 #include <dt-bindings/clock/tegra124-car.h>
42 #include "tegra124_car.h"
45 #define SMF_HAVE_DIVIDER_2 1
47 struct super_mux_def {
48 struct clknode_init_def clkdef;
55 #define PLIST(x) static const char *x[]
56 #define SM(_id, cn, pl, r, x, d, f) \
60 .clkdef.parent_names = pl, \
61 .clkdef.parent_cnt = nitems(pl), \
62 .clkdef.flags = CLK_NODE_STATIC_STRINGS, \
69 PLIST(cclk_g_parents) = {
70 "clk_m", "pllC_out0", "clk_s", "pllM_out0",
71 "pllP_out0", "pllP_out4", "pllC2_out0", "pllC3_out0",
72 "pllX_out", NULL, NULL, NULL,
73 NULL, NULL, NULL,NULL, // "dfllCPU_out0"
76 PLIST(cclk_lp_parents) = {
77 "clk_m", "pllC_out0", "clk_s", "pllM_out0",
78 "pllP_out0", "pllP_out4", "pllC2_out0", "pllC3_out0",
79 "pllX_out", NULL, NULL, NULL,
80 NULL, NULL, NULL, NULL,
84 PLIST(sclk_parents) = {
85 "clk_m", "pllC_out1", "pllP_out4", "pllP_out0",
86 "pllP_out2", "pllC_out0", "clk_s", "pllM_out1",
89 static struct super_mux_def super_mux_def[] = {
90 SM(TEGRA124_CLK_CCLK_G, "cclk_g", cclk_g_parents, CCLKG_BURST_POLICY, 0, 0, 0),
91 SM(TEGRA124_CLK_CCLK_LP, "cclk_lp", cclk_lp_parents, CCLKLP_BURST_POLICY, 8, 16, SMF_HAVE_DIVIDER_2),
92 SM(TEGRA124_CLK_SCLK, "sclk", sclk_parents, SCLK_BURST_POLICY, 0, 0, 0),
95 static int super_mux_init(struct clknode *clk, device_t dev);
96 static int super_mux_set_mux(struct clknode *clk, int idx);
108 static clknode_method_t super_mux_methods[] = {
109 /* Device interface */
110 CLKNODEMETHOD(clknode_init, super_mux_init),
111 CLKNODEMETHOD(clknode_set_mux, super_mux_set_mux),
114 DEFINE_CLASS_1(tegra124_super_mux, tegra124_super_mux_class, super_mux_methods,
115 sizeof(struct super_mux_sc), clknode_class);
118 #define SUPER_MUX_STATE_STDBY 0
119 #define SUPER_MUX_STATE_IDLE 1
120 #define SUPER_MUX_STATE_RUN 2
121 #define SUPER_MUX_STATE_IRQ 3
122 #define SUPER_MUX_STATE_FIQ 4
124 /* Mux register bits. */
125 #define SUPER_MUX_STATE_BIT_SHIFT 28
126 #define SUPER_MUX_STATE_BIT_MASK 0xF
127 /* State is Priority encoded */
128 #define SUPER_MUX_STATE_BIT_STDBY 0x00
129 #define SUPER_MUX_STATE_BIT_IDLE 0x01
130 #define SUPER_MUX_STATE_BIT_RUN 0x02
131 #define SUPER_MUX_STATE_BIT_IRQ 0x04
132 #define SUPER_MUX_STATE_BIT_FIQ 0x08
134 #define SUPER_MUX_MUX_WIDTH 4
135 #define SUPER_MUX_LP_DIV2_BYPASS (1 << 16)
138 super_mux_get_state(uint32_t reg)
140 reg = (reg >> SUPER_MUX_STATE_BIT_SHIFT) & SUPER_MUX_STATE_BIT_MASK;
141 if (reg & SUPER_MUX_STATE_BIT_FIQ)
142 return (SUPER_MUX_STATE_FIQ);
143 if (reg & SUPER_MUX_STATE_BIT_IRQ)
144 return (SUPER_MUX_STATE_IRQ);
145 if (reg & SUPER_MUX_STATE_BIT_RUN)
146 return (SUPER_MUX_STATE_RUN);
147 if (reg & SUPER_MUX_STATE_BIT_IDLE)
148 return (SUPER_MUX_STATE_IDLE);
149 return (SUPER_MUX_STATE_STDBY);
153 super_mux_init(struct clknode *clk, device_t dev)
155 struct super_mux_sc *sc;
159 sc = clknode_get_softc(clk);
162 RD4(sc, sc->base_reg, ®);
164 state = super_mux_get_state(reg);
166 if ((state != SUPER_MUX_STATE_RUN) &&
167 (state != SUPER_MUX_STATE_IDLE)) {
168 panic("Unexpected super mux state: %u", state);
171 shift = state * SUPER_MUX_MUX_WIDTH;
173 sc->mux = (reg >> shift) & ((1 << SUPER_MUX_MUX_WIDTH) - 1);
176 * CCLKLP uses PLLX/2 as source if LP_DIV2_BYPASS isn't set
177 * and source mux is set to PLLX.
179 if (sc->flags & SMF_HAVE_DIVIDER_2) {
180 if (((reg & SUPER_MUX_LP_DIV2_BYPASS) == 0) &&
181 (sc->mux == sc->src_pllx))
182 sc->mux = sc->src_div2;
184 clknode_init_parent_idx(clk, sc->mux);
190 super_mux_set_mux(struct clknode *clk, int idx)
193 struct super_mux_sc *sc;
197 sc = clknode_get_softc(clk);
200 RD4(sc, sc->base_reg, ®);
201 state = super_mux_get_state(reg);
203 if ((state != SUPER_MUX_STATE_RUN) &&
204 (state != SUPER_MUX_STATE_IDLE)) {
205 panic("Unexpected super mux state: %u", state);
207 shift = (state - 1) * SUPER_MUX_MUX_WIDTH;
209 if (sc->flags & SMF_HAVE_DIVIDER_2) {
210 if (idx == sc->src_div2) {
212 reg &= ~SUPER_MUX_LP_DIV2_BYPASS;
213 WR4(sc, sc->base_reg, reg);
214 RD4(sc, sc->base_reg, &dummy);
215 } else if (idx == sc->src_pllx) {
216 reg = SUPER_MUX_LP_DIV2_BYPASS;
217 WR4(sc, sc->base_reg, reg);
218 RD4(sc, sc->base_reg, &dummy);
221 reg &= ~(((1 << SUPER_MUX_MUX_WIDTH) - 1) << shift);
224 WR4(sc, sc->base_reg, reg);
225 RD4(sc, sc->base_reg, &dummy);
232 super_mux_register(struct clkdom *clkdom, struct super_mux_def *clkdef)
235 struct super_mux_sc *sc;
237 clk = clknode_create(clkdom, &tegra124_super_mux_class,
242 sc = clknode_get_softc(clk);
243 sc->clkdev = clknode_get_device(clk);
244 sc->base_reg = clkdef->base_reg;
245 sc->src_pllx = clkdef->src_pllx;
246 sc->src_div2 = clkdef->src_div2;
247 sc->flags = clkdef->flags;
249 clknode_register(clkdom, clk);
254 tegra124_super_mux_clock(struct tegra124_car_softc *sc)
258 for (i = 0; i < nitems(super_mux_def); i++) {
259 rv = super_mux_register(sc->clkdom, &super_mux_def[i]);
261 panic("super_mux_register failed");