1*be82b3a0SEmmanuel Vadot /*-
2*be82b3a0SEmmanuel Vadot * Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
3*be82b3a0SEmmanuel Vadot * All rights reserved.
4*be82b3a0SEmmanuel Vadot *
5*be82b3a0SEmmanuel Vadot * Redistribution and use in source and binary forms, with or without
6*be82b3a0SEmmanuel Vadot * modification, are permitted provided that the following conditions
7*be82b3a0SEmmanuel Vadot * are met:
8*be82b3a0SEmmanuel Vadot * 1. Redistributions of source code must retain the above copyright
9*be82b3a0SEmmanuel Vadot * notice, this list of conditions and the following disclaimer.
10*be82b3a0SEmmanuel Vadot * 2. Redistributions in binary form must reproduce the above copyright
11*be82b3a0SEmmanuel Vadot * notice, this list of conditions and the following disclaimer in the
12*be82b3a0SEmmanuel Vadot * documentation and/or other materials provided with the distribution.
13*be82b3a0SEmmanuel Vadot *
14*be82b3a0SEmmanuel Vadot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*be82b3a0SEmmanuel Vadot * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*be82b3a0SEmmanuel Vadot * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*be82b3a0SEmmanuel Vadot * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*be82b3a0SEmmanuel Vadot * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*be82b3a0SEmmanuel Vadot * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*be82b3a0SEmmanuel Vadot * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*be82b3a0SEmmanuel Vadot * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*be82b3a0SEmmanuel Vadot * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*be82b3a0SEmmanuel Vadot * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*be82b3a0SEmmanuel Vadot * SUCH DAMAGE.
25*be82b3a0SEmmanuel Vadot */
26*be82b3a0SEmmanuel Vadot
27*be82b3a0SEmmanuel Vadot #include <sys/param.h>
28*be82b3a0SEmmanuel Vadot #include <sys/conf.h>
29*be82b3a0SEmmanuel Vadot #include <sys/bus.h>
30*be82b3a0SEmmanuel Vadot #include <sys/kernel.h>
31*be82b3a0SEmmanuel Vadot #include <sys/systm.h>
32*be82b3a0SEmmanuel Vadot
33*be82b3a0SEmmanuel Vadot #include <machine/bus.h>
34*be82b3a0SEmmanuel Vadot
35*be82b3a0SEmmanuel Vadot #include <dev/clk/clk_mux.h>
36*be82b3a0SEmmanuel Vadot
37*be82b3a0SEmmanuel Vadot #include "clkdev_if.h"
38*be82b3a0SEmmanuel Vadot
39*be82b3a0SEmmanuel Vadot #define WR4(_clk, off, val) \
40*be82b3a0SEmmanuel Vadot CLKDEV_WRITE_4(clknode_get_device(_clk), off, val)
41*be82b3a0SEmmanuel Vadot #define RD4(_clk, off, val) \
42*be82b3a0SEmmanuel Vadot CLKDEV_READ_4(clknode_get_device(_clk), off, val)
43*be82b3a0SEmmanuel Vadot #define MD4(_clk, off, clr, set ) \
44*be82b3a0SEmmanuel Vadot CLKDEV_MODIFY_4(clknode_get_device(_clk), off, clr, set)
45*be82b3a0SEmmanuel Vadot #define DEVICE_LOCK(_clk) \
46*be82b3a0SEmmanuel Vadot CLKDEV_DEVICE_LOCK(clknode_get_device(_clk))
47*be82b3a0SEmmanuel Vadot #define DEVICE_UNLOCK(_clk) \
48*be82b3a0SEmmanuel Vadot CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk))
49*be82b3a0SEmmanuel Vadot
50*be82b3a0SEmmanuel Vadot static int clknode_mux_init(struct clknode *clk, device_t dev);
51*be82b3a0SEmmanuel Vadot static int clknode_mux_set_mux(struct clknode *clk, int idx);
52*be82b3a0SEmmanuel Vadot
53*be82b3a0SEmmanuel Vadot struct clknode_mux_sc {
54*be82b3a0SEmmanuel Vadot uint32_t offset;
55*be82b3a0SEmmanuel Vadot uint32_t shift;
56*be82b3a0SEmmanuel Vadot uint32_t mask;
57*be82b3a0SEmmanuel Vadot int mux_flags;
58*be82b3a0SEmmanuel Vadot };
59*be82b3a0SEmmanuel Vadot
60*be82b3a0SEmmanuel Vadot static clknode_method_t clknode_mux_methods[] = {
61*be82b3a0SEmmanuel Vadot /* Device interface */
62*be82b3a0SEmmanuel Vadot CLKNODEMETHOD(clknode_init, clknode_mux_init),
63*be82b3a0SEmmanuel Vadot CLKNODEMETHOD(clknode_set_mux, clknode_mux_set_mux),
64*be82b3a0SEmmanuel Vadot CLKNODEMETHOD_END
65*be82b3a0SEmmanuel Vadot };
66*be82b3a0SEmmanuel Vadot DEFINE_CLASS_1(clknode_mux, clknode_mux_class, clknode_mux_methods,
67*be82b3a0SEmmanuel Vadot sizeof(struct clknode_mux_sc), clknode_class);
68*be82b3a0SEmmanuel Vadot
69*be82b3a0SEmmanuel Vadot
70*be82b3a0SEmmanuel Vadot static int
clknode_mux_init(struct clknode * clk,device_t dev)71*be82b3a0SEmmanuel Vadot clknode_mux_init(struct clknode *clk, device_t dev)
72*be82b3a0SEmmanuel Vadot {
73*be82b3a0SEmmanuel Vadot uint32_t reg;
74*be82b3a0SEmmanuel Vadot struct clknode_mux_sc *sc;
75*be82b3a0SEmmanuel Vadot int rv;
76*be82b3a0SEmmanuel Vadot
77*be82b3a0SEmmanuel Vadot sc = clknode_get_softc(clk);
78*be82b3a0SEmmanuel Vadot
79*be82b3a0SEmmanuel Vadot DEVICE_LOCK(clk);
80*be82b3a0SEmmanuel Vadot rv = RD4(clk, sc->offset, ®);
81*be82b3a0SEmmanuel Vadot DEVICE_UNLOCK(clk);
82*be82b3a0SEmmanuel Vadot if (rv != 0) {
83*be82b3a0SEmmanuel Vadot return (rv);
84*be82b3a0SEmmanuel Vadot }
85*be82b3a0SEmmanuel Vadot reg = (reg >> sc->shift) & sc->mask;
86*be82b3a0SEmmanuel Vadot clknode_init_parent_idx(clk, reg);
87*be82b3a0SEmmanuel Vadot return(0);
88*be82b3a0SEmmanuel Vadot }
89*be82b3a0SEmmanuel Vadot
90*be82b3a0SEmmanuel Vadot static int
clknode_mux_set_mux(struct clknode * clk,int idx)91*be82b3a0SEmmanuel Vadot clknode_mux_set_mux(struct clknode *clk, int idx)
92*be82b3a0SEmmanuel Vadot {
93*be82b3a0SEmmanuel Vadot uint32_t reg;
94*be82b3a0SEmmanuel Vadot struct clknode_mux_sc *sc;
95*be82b3a0SEmmanuel Vadot int rv;
96*be82b3a0SEmmanuel Vadot
97*be82b3a0SEmmanuel Vadot sc = clknode_get_softc(clk);
98*be82b3a0SEmmanuel Vadot
99*be82b3a0SEmmanuel Vadot DEVICE_LOCK(clk);
100*be82b3a0SEmmanuel Vadot rv = MD4(clk, sc->offset, sc->mask << sc->shift,
101*be82b3a0SEmmanuel Vadot (idx & sc->mask) << sc->shift);
102*be82b3a0SEmmanuel Vadot if (rv != 0) {
103*be82b3a0SEmmanuel Vadot DEVICE_UNLOCK(clk);
104*be82b3a0SEmmanuel Vadot return (rv);
105*be82b3a0SEmmanuel Vadot }
106*be82b3a0SEmmanuel Vadot RD4(clk, sc->offset, ®);
107*be82b3a0SEmmanuel Vadot DEVICE_UNLOCK(clk);
108*be82b3a0SEmmanuel Vadot
109*be82b3a0SEmmanuel Vadot return(0);
110*be82b3a0SEmmanuel Vadot }
111*be82b3a0SEmmanuel Vadot
112*be82b3a0SEmmanuel Vadot int
clknode_mux_register(struct clkdom * clkdom,struct clk_mux_def * clkdef)113*be82b3a0SEmmanuel Vadot clknode_mux_register(struct clkdom *clkdom, struct clk_mux_def *clkdef)
114*be82b3a0SEmmanuel Vadot {
115*be82b3a0SEmmanuel Vadot struct clknode *clk;
116*be82b3a0SEmmanuel Vadot struct clknode_mux_sc *sc;
117*be82b3a0SEmmanuel Vadot
118*be82b3a0SEmmanuel Vadot clk = clknode_create(clkdom, &clknode_mux_class, &clkdef->clkdef);
119*be82b3a0SEmmanuel Vadot if (clk == NULL)
120*be82b3a0SEmmanuel Vadot return (1);
121*be82b3a0SEmmanuel Vadot
122*be82b3a0SEmmanuel Vadot sc = clknode_get_softc(clk);
123*be82b3a0SEmmanuel Vadot sc->offset = clkdef->offset;
124*be82b3a0SEmmanuel Vadot sc->shift = clkdef->shift;
125*be82b3a0SEmmanuel Vadot sc->mask = (1 << clkdef->width) - 1;
126*be82b3a0SEmmanuel Vadot sc->mux_flags = clkdef->mux_flags;
127*be82b3a0SEmmanuel Vadot
128*be82b3a0SEmmanuel Vadot clknode_register(clkdom, clk);
129*be82b3a0SEmmanuel Vadot return (0);
130*be82b3a0SEmmanuel Vadot }
131