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/cdefs.h>
28 #include <sys/param.h>
29 #include <sys/conf.h>
30 #include <sys/bus.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
33
34 #include <machine/bus.h>
35
36 #include <dev/extres/clk/clk_gate.h>
37
38 #include "clkdev_if.h"
39
40 #define WR4(_clk, off, val) \
41 CLKDEV_WRITE_4(clknode_get_device(_clk), off, val)
42 #define RD4(_clk, off, val) \
43 CLKDEV_READ_4(clknode_get_device(_clk), off, val)
44 #define MD4(_clk, off, clr, set ) \
45 CLKDEV_MODIFY_4(clknode_get_device(_clk), off, clr, set)
46 #define DEVICE_LOCK(_clk) \
47 CLKDEV_DEVICE_LOCK(clknode_get_device(_clk))
48 #define DEVICE_UNLOCK(_clk) \
49 CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk))
50
51 static int clknode_gate_init(struct clknode *clk, device_t dev);
52 static int clknode_gate_set_gate(struct clknode *clk, bool enable);
53 static int clknode_gate_get_gate(struct clknode *clk, bool *enable);
54 struct clknode_gate_sc {
55 uint32_t offset;
56 uint32_t shift;
57 uint32_t mask;
58 uint32_t on_value;
59 uint32_t off_value;
60 int gate_flags;
61 };
62
63 static clknode_method_t clknode_gate_methods[] = {
64 /* Device interface */
65 CLKNODEMETHOD(clknode_init, clknode_gate_init),
66 CLKNODEMETHOD(clknode_set_gate, clknode_gate_set_gate),
67 CLKNODEMETHOD(clknode_get_gate, clknode_gate_get_gate),
68 CLKNODEMETHOD_END
69 };
70 DEFINE_CLASS_1(clknode_gate, clknode_gate_class, clknode_gate_methods,
71 sizeof(struct clknode_gate_sc), clknode_class);
72
73 static int
clknode_gate_init(struct clknode * clk,device_t dev)74 clknode_gate_init(struct clknode *clk, device_t dev)
75 {
76
77 clknode_init_parent_idx(clk, 0);
78 return(0);
79 }
80
81 static int
clknode_gate_set_gate(struct clknode * clk,bool enable)82 clknode_gate_set_gate(struct clknode *clk, bool enable)
83 {
84 uint32_t reg;
85 struct clknode_gate_sc *sc;
86 int rv;
87
88 sc = clknode_get_softc(clk);
89 DEVICE_LOCK(clk);
90 rv = MD4(clk, sc->offset, sc->mask << sc->shift,
91 (enable ? sc->on_value : sc->off_value) << sc->shift);
92 if (rv != 0) {
93 DEVICE_UNLOCK(clk);
94 return (rv);
95 }
96 RD4(clk, sc->offset, ®);
97 DEVICE_UNLOCK(clk);
98 return(0);
99 }
100
101 static int
clknode_gate_get_gate(struct clknode * clk,bool * enabled)102 clknode_gate_get_gate(struct clknode *clk, bool *enabled)
103 {
104 uint32_t reg;
105 struct clknode_gate_sc *sc;
106 int rv;
107
108 sc = clknode_get_softc(clk);
109 DEVICE_LOCK(clk);
110 rv = RD4(clk, sc->offset, ®);
111 DEVICE_UNLOCK(clk);
112 if (rv != 0)
113 return (rv);
114 reg = (reg >> sc->shift) & sc->mask;
115 *enabled = reg == sc->on_value;
116 return(0);
117 }
118
119 int
clknode_gate_register(struct clkdom * clkdom,struct clk_gate_def * clkdef)120 clknode_gate_register(struct clkdom *clkdom, struct clk_gate_def *clkdef)
121 {
122 struct clknode *clk;
123 struct clknode_gate_sc *sc;
124
125 clk = clknode_create(clkdom, &clknode_gate_class, &clkdef->clkdef);
126 if (clk == NULL)
127 return (1);
128
129 sc = clknode_get_softc(clk);
130 sc->offset = clkdef->offset;
131 sc->shift = clkdef->shift;
132 sc->mask = clkdef->mask;
133 sc->on_value = clkdef->on_value;
134 sc->off_value = clkdef->off_value;
135 sc->gate_flags = clkdef->gate_flags;
136
137 clknode_register(clkdom, clk);
138 return (0);
139 }
140