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