xref: /freebsd/sys/dev/clk/clk_gate.c (revision 1edb7116)
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_gate.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_gate_init(struct clknode *clk, device_t dev);
51 static int clknode_gate_set_gate(struct clknode *clk, bool enable);
52 static int clknode_gate_get_gate(struct clknode *clk, bool *enable);
53 struct clknode_gate_sc {
54 	uint32_t	offset;
55 	uint32_t	shift;
56 	uint32_t	mask;
57 	uint32_t	on_value;
58 	uint32_t	off_value;
59 	int		gate_flags;
60 };
61 
62 static clknode_method_t clknode_gate_methods[] = {
63 	/* Device interface */
64 	CLKNODEMETHOD(clknode_init,	clknode_gate_init),
65 	CLKNODEMETHOD(clknode_set_gate,	clknode_gate_set_gate),
66 	CLKNODEMETHOD(clknode_get_gate,	clknode_gate_get_gate),
67 	CLKNODEMETHOD_END
68 };
69 DEFINE_CLASS_1(clknode_gate, clknode_gate_class, clknode_gate_methods,
70    sizeof(struct clknode_gate_sc), clknode_class);
71 
72 static int
73 clknode_gate_init(struct clknode *clk, device_t dev)
74 {
75 
76 	clknode_init_parent_idx(clk, 0);
77 	return(0);
78 }
79 
80 static int
81 clknode_gate_set_gate(struct clknode *clk, bool enable)
82 {
83 	uint32_t reg;
84 	struct clknode_gate_sc *sc;
85 	int rv;
86 
87 	sc = clknode_get_softc(clk);
88 	DEVICE_LOCK(clk);
89 	rv = MD4(clk, sc->offset, sc->mask << sc->shift,
90 	    (enable ? sc->on_value : sc->off_value) << sc->shift);
91 	if (rv != 0) {
92 		DEVICE_UNLOCK(clk);
93 		return (rv);
94 	}
95 	RD4(clk, sc->offset, &reg);
96 	DEVICE_UNLOCK(clk);
97 	return(0);
98 }
99 
100 static int
101 clknode_gate_get_gate(struct clknode *clk, bool *enabled)
102 {
103 	uint32_t reg;
104 	struct clknode_gate_sc *sc;
105 	int rv;
106 
107 	sc = clknode_get_softc(clk);
108 	DEVICE_LOCK(clk);
109 	rv = RD4(clk, sc->offset, &reg);
110 	DEVICE_UNLOCK(clk);
111 	if (rv != 0)
112 		return (rv);
113 	reg = (reg >> sc->shift) & sc->mask;
114 	*enabled = reg == sc->on_value;
115 	return(0);
116 }
117 
118 int
119 clknode_gate_register(struct clkdom *clkdom, struct clk_gate_def *clkdef)
120 {
121 	struct clknode *clk;
122 	struct clknode_gate_sc *sc;
123 
124 	clk = clknode_create(clkdom, &clknode_gate_class, &clkdef->clkdef);
125 	if (clk == NULL)
126 		return (1);
127 
128 	sc = clknode_get_softc(clk);
129 	sc->offset = clkdef->offset;
130 	sc->shift = clkdef->shift;
131 	sc->mask =  clkdef->mask;
132 	sc->on_value = clkdef->on_value;
133 	sc->off_value = clkdef->off_value;
134 	sc->gate_flags = clkdef->gate_flags;
135 
136 	clknode_register(clkdom, clk);
137 	return (0);
138 }
139