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