1 /*-
2 * Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/param.h>
28 #include <sys/conf.h>
29 #include <sys/bus.h>
30 #include <sys/kernel.h>
31 #include <sys/systm.h>
32
33 #include <machine/bus.h>
34
35 #include <dev/clk/clk_mux.h>
36
37 #include "clkdev_if.h"
38
39 #define WR4(_clk, off, val) \
40 CLKDEV_WRITE_4(clknode_get_device(_clk), off, val)
41 #define RD4(_clk, off, val) \
42 CLKDEV_READ_4(clknode_get_device(_clk), off, val)
43 #define MD4(_clk, off, clr, set ) \
44 CLKDEV_MODIFY_4(clknode_get_device(_clk), off, clr, set)
45 #define DEVICE_LOCK(_clk) \
46 CLKDEV_DEVICE_LOCK(clknode_get_device(_clk))
47 #define DEVICE_UNLOCK(_clk) \
48 CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk))
49
50 static int clknode_mux_init(struct clknode *clk, device_t dev);
51 static int clknode_mux_set_mux(struct clknode *clk, int idx);
52
53 struct clknode_mux_sc {
54 uint32_t offset;
55 uint32_t shift;
56 uint32_t mask;
57 int mux_flags;
58 };
59
60 static clknode_method_t clknode_mux_methods[] = {
61 /* Device interface */
62 CLKNODEMETHOD(clknode_init, clknode_mux_init),
63 CLKNODEMETHOD(clknode_set_mux, clknode_mux_set_mux),
64 CLKNODEMETHOD_END
65 };
66 DEFINE_CLASS_1(clknode_mux, clknode_mux_class, clknode_mux_methods,
67 sizeof(struct clknode_mux_sc), clknode_class);
68
69
70 static int
clknode_mux_init(struct clknode * clk,device_t dev)71 clknode_mux_init(struct clknode *clk, device_t dev)
72 {
73 uint32_t reg;
74 struct clknode_mux_sc *sc;
75 int rv;
76
77 sc = clknode_get_softc(clk);
78
79 DEVICE_LOCK(clk);
80 rv = RD4(clk, sc->offset, ®);
81 DEVICE_UNLOCK(clk);
82 if (rv != 0) {
83 return (rv);
84 }
85 reg = (reg >> sc->shift) & sc->mask;
86 clknode_init_parent_idx(clk, reg);
87 return(0);
88 }
89
90 static int
clknode_mux_set_mux(struct clknode * clk,int idx)91 clknode_mux_set_mux(struct clknode *clk, int idx)
92 {
93 uint32_t reg;
94 struct clknode_mux_sc *sc;
95 int rv;
96
97 sc = clknode_get_softc(clk);
98
99 DEVICE_LOCK(clk);
100 rv = MD4(clk, sc->offset, sc->mask << sc->shift,
101 (idx & sc->mask) << sc->shift);
102 if (rv != 0) {
103 DEVICE_UNLOCK(clk);
104 return (rv);
105 }
106 RD4(clk, sc->offset, ®);
107 DEVICE_UNLOCK(clk);
108
109 return(0);
110 }
111
112 int
clknode_mux_register(struct clkdom * clkdom,struct clk_mux_def * clkdef)113 clknode_mux_register(struct clkdom *clkdom, struct clk_mux_def *clkdef)
114 {
115 struct clknode *clk;
116 struct clknode_mux_sc *sc;
117
118 clk = clknode_create(clkdom, &clknode_mux_class, &clkdef->clkdef);
119 if (clk == NULL)
120 return (1);
121
122 sc = clknode_get_softc(clk);
123 sc->offset = clkdef->offset;
124 sc->shift = clkdef->shift;
125 sc->mask = (1 << clkdef->width) - 1;
126 sc->mux_flags = clkdef->mux_flags;
127
128 clknode_register(clkdom, clk);
129 return (0);
130 }
131