2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright 2020 Michal Meloun <mmel@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/param.h>
29 #include <sys/systm.h>
32 #include <sys/mutex.h>
35 #include <machine/bus.h>
37 #include <dev/extres/clk/clk.h>
39 #include <dt-bindings/clock/tegra210-car.h>
40 #include "tegra210_car.h"
42 struct super_mux_def {
43 struct clknode_init_def clkdef;
48 #define PLIST(x) static const char *x[]
49 #define SM(_id, cn, pl, r) \
53 .clkdef.parent_names = pl, \
54 .clkdef.parent_cnt = nitems(pl), \
55 .clkdef.flags = CLK_NODE_STATIC_STRINGS, \
60 PLIST(cclk_g_parents) = {
61 "clk_m", NULL, "clk_s", NULL,
62 "pllP_out0", "pllP_out4", NULL, NULL,
63 "pllX_out0", "dfllCPU_out_alias", NULL, NULL,
64 NULL, NULL, "pllX_out0_alias", "dfllCPU_out",
67 PLIST(cclk_lp_parents) = {
68 "clk_m", NULL, "clk_s", NULL,
69 "pllP_out0", "pllP_out4", NULL, NULL,
70 "pllX_out0", "dfllCPU_out_alias", NULL, NULL,
71 NULL, NULL, "pllX_out0_alias", "dfllCPU_out",
74 PLIST(sclk_parents) = {
75 "clk_m", "pllC_out1", "pllC4_out3", "pllP_out0",
76 "pllP_out2", "pllC4_out1", "clk_s", "pllC4_out1",
79 static struct super_mux_def super_mux_def[] = {
80 SM(TEGRA210_CLK_CCLK_G, "cclk_g", cclk_g_parents, CCLKG_BURST_POLICY),
81 SM(TEGRA210_CLK_CCLK_LP, "cclk_lp", cclk_lp_parents, CCLKLP_BURST_POLICY),
82 SM(TEGRA210_CLK_SCLK, "sclk", sclk_parents, SCLK_BURST_POLICY),
85 static int super_mux_init(struct clknode *clk, device_t dev);
86 static int super_mux_set_mux(struct clknode *clk, int idx);
96 static clknode_method_t super_mux_methods[] = {
97 /* Device interface */
98 CLKNODEMETHOD(clknode_init, super_mux_init),
99 CLKNODEMETHOD(clknode_set_mux, super_mux_set_mux),
102 DEFINE_CLASS_1(tegra210_super_mux, tegra210_super_mux_class, super_mux_methods,
103 sizeof(struct super_mux_sc), clknode_class);
106 #define SUPER_MUX_STATE_STDBY 0
107 #define SUPER_MUX_STATE_IDLE 1
108 #define SUPER_MUX_STATE_RUN 2
109 #define SUPER_MUX_STATE_IRQ 3
110 #define SUPER_MUX_STATE_FIQ 4
112 /* Mux register bits. */
113 #define SUPER_MUX_STATE_BIT_SHIFT 28
114 #define SUPER_MUX_STATE_BIT_MASK 0xF
115 /* State is Priority encoded */
116 #define SUPER_MUX_STATE_BIT_STDBY 0x00
117 #define SUPER_MUX_STATE_BIT_IDLE 0x01
118 #define SUPER_MUX_STATE_BIT_RUN 0x02
119 #define SUPER_MUX_STATE_BIT_IRQ 0x04
120 #define SUPER_MUX_STATE_BIT_FIQ 0x08
122 #define SUPER_MUX_MUX_WIDTH 4
125 super_mux_get_state(uint32_t reg)
127 reg = (reg >> SUPER_MUX_STATE_BIT_SHIFT) & SUPER_MUX_STATE_BIT_MASK;
128 if (reg & SUPER_MUX_STATE_BIT_FIQ)
129 return (SUPER_MUX_STATE_FIQ);
130 if (reg & SUPER_MUX_STATE_BIT_IRQ)
131 return (SUPER_MUX_STATE_IRQ);
132 if (reg & SUPER_MUX_STATE_BIT_RUN)
133 return (SUPER_MUX_STATE_RUN);
134 if (reg & SUPER_MUX_STATE_BIT_IDLE)
135 return (SUPER_MUX_STATE_IDLE);
136 return (SUPER_MUX_STATE_STDBY);
140 super_mux_init(struct clknode *clk, device_t dev)
142 struct super_mux_sc *sc;
146 sc = clknode_get_softc(clk);
149 RD4(sc, sc->base_reg, ®);
151 state = super_mux_get_state(reg);
153 if ((state != SUPER_MUX_STATE_RUN) &&
154 (state != SUPER_MUX_STATE_IDLE)) {
155 panic("Unexpected super mux state: %u", state);
158 shift = state * SUPER_MUX_MUX_WIDTH;
159 sc->mux = (reg >> shift) & ((1 << SUPER_MUX_MUX_WIDTH) - 1);
161 clknode_init_parent_idx(clk, sc->mux);
167 super_mux_set_mux(struct clknode *clk, int idx)
170 struct super_mux_sc *sc;
174 sc = clknode_get_softc(clk);
177 RD4(sc, sc->base_reg, ®);
178 state = super_mux_get_state(reg);
180 if ((state != SUPER_MUX_STATE_RUN) &&
181 (state != SUPER_MUX_STATE_IDLE)) {
182 panic("Unexpected super mux state: %u", state);
185 shift = (state - 1) * SUPER_MUX_MUX_WIDTH;
187 reg &= ~(((1 << SUPER_MUX_MUX_WIDTH) - 1) << shift);
190 WR4(sc, sc->base_reg, reg);
191 RD4(sc, sc->base_reg, &dummy);
198 super_mux_register(struct clkdom *clkdom, struct super_mux_def *clkdef)
201 struct super_mux_sc *sc;
203 clk = clknode_create(clkdom, &tegra210_super_mux_class,
208 sc = clknode_get_softc(clk);
209 sc->clkdev = clknode_get_device(clk);
210 sc->base_reg = clkdef->base_reg;
211 sc->flags = clkdef->flags;
213 clknode_register(clkdom, clk);
218 tegra210_super_mux_clock(struct tegra210_car_softc *sc)
222 for (i = 0; i < nitems(super_mux_def); i++) {
223 rv = super_mux_register(sc->clkdom, &super_mux_def[i]);
225 panic("super_mux_register failed");