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/conf.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 
38 #include <machine/bus.h>
39 
40 #include <dev/extres/clk/clk.h>
41 
42 #include <arm64/rockchip/clk/rk_cru.h>
43 #include <arm64/rockchip/clk/rk_clk_mux.h>
44 
45 #include "clkdev_if.h"
46 
47 #define	WR4(_clk, off, val)						\
48 	CLKDEV_WRITE_4(clknode_get_device(_clk), off, val)
49 #define	RD4(_clk, off, val)						\
50 	CLKDEV_READ_4(clknode_get_device(_clk), off, val)
51 #define	MD4(_clk, off, clr, set )					\
52 	CLKDEV_MODIFY_4(clknode_get_device(_clk), off, clr, set)
53 #define	DEVICE_LOCK(_clk)						\
54 	CLKDEV_DEVICE_LOCK(clknode_get_device(_clk))
55 #define	DEVICE_UNLOCK(_clk)						\
56 	CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk))
57 
58 #if 0
59 #define	dprintf(format, arg...)						\
60 	printf("%s:(%s)" format, __func__, clknode_get_name(clk), arg)
61 #else
62 #define	dprintf(format, arg...)
63 #endif
64 
65 static int rk_clk_mux_init(struct clknode *clk, device_t dev);
66 static int rk_clk_mux_set_mux(struct clknode *clk, int idx);
67 static int rk_clk_mux_set_freq(struct clknode *clk, uint64_t fparent,
68     uint64_t *fout, int flags, int *stop);
69 
70 struct rk_clk_mux_sc {
71 	uint32_t	offset;
72 	uint32_t	shift;
73 	uint32_t	mask;
74 	int		mux_flags;
75 };
76 
77 static clknode_method_t rk_clk_mux_methods[] = {
78 	/* Device interface */
79 	CLKNODEMETHOD(clknode_init, 	rk_clk_mux_init),
80 	CLKNODEMETHOD(clknode_set_mux, 	rk_clk_mux_set_mux),
81 	CLKNODEMETHOD(clknode_set_freq,	rk_clk_mux_set_freq),
82 	CLKNODEMETHOD_END
83 };
84 DEFINE_CLASS_1(rk_clk_mux, rk_clk_mux_class, rk_clk_mux_methods,
85    sizeof(struct rk_clk_mux_sc), clknode_class);
86 
87 static int
rk_clk_mux_init(struct clknode * clk,device_t dev)88 rk_clk_mux_init(struct clknode *clk, device_t dev)
89 {
90 	uint32_t reg;
91 	struct rk_clk_mux_sc *sc;
92 	int rv;
93 
94 	sc = clknode_get_softc(clk);
95 
96 	DEVICE_LOCK(clk);
97 	rv = RD4(clk, sc->offset, &reg);
98 	DEVICE_UNLOCK(clk);
99 	if (rv != 0) {
100 		return (rv);
101 	}
102 	reg = (reg >> sc->shift) & sc->mask;
103 	clknode_init_parent_idx(clk, reg);
104 	return(0);
105 }
106 
107 static int
rk_clk_mux_set_mux(struct clknode * clk,int idx)108 rk_clk_mux_set_mux(struct clknode *clk, int idx)
109 {
110 	uint32_t reg;
111 	struct rk_clk_mux_sc *sc;
112 	int rv;
113 
114 	sc = clknode_get_softc(clk);
115 
116 	DEVICE_LOCK(clk);
117 	rv = MD4(clk, sc->offset, sc->mask << sc->shift,
118 	    ((idx & sc->mask) << sc->shift) | RK_CLK_MUX_MASK);
119 	if (rv != 0) {
120 		DEVICE_UNLOCK(clk);
121 		return (rv);
122 	}
123 	RD4(clk, sc->offset, &reg);
124 	DEVICE_UNLOCK(clk);
125 
126 	return(0);
127 }
128 
129 static int
rk_clk_mux_set_freq(struct clknode * clk,uint64_t fparent,uint64_t * fout,int flags,int * stop)130 rk_clk_mux_set_freq(struct clknode *clk, uint64_t fparent, uint64_t *fout,
131     int flags, int *stop)
132 {
133 	struct rk_clk_mux_sc *sc;
134 	struct clknode *p_clk, *p_best_clk;
135 	const char **p_names;
136 	int p_idx, best_parent;
137 	int rv;
138 
139 	sc = clknode_get_softc(clk);
140 
141 	if ((sc->mux_flags & RK_CLK_MUX_REPARENT) == 0) {
142 		*stop = 0;
143 		return (0);
144 	}
145 
146 	dprintf("Finding best parent for target freq of %ju\n", *fout);
147 	p_names = clknode_get_parent_names(clk);
148 	for (p_idx = 0; p_idx != clknode_get_parents_num(clk); p_idx++) {
149 		p_clk = clknode_find_by_name(p_names[p_idx]);
150 		dprintf("Testing with parent %s (%d)\n",
151 		    clknode_get_name(p_clk), p_idx);
152 
153 		rv = clknode_set_freq(p_clk, *fout, flags | CLK_SET_DRYRUN, 0);
154 		dprintf("Testing with parent %s (%d) rv=%d\n",
155 		    clknode_get_name(p_clk), p_idx, rv);
156 		if (rv == 0) {
157 			best_parent = p_idx;
158 			p_best_clk = p_clk;
159 			*stop = 1;
160 		}
161 	}
162 
163 	if (!*stop)
164 		return (0);
165 
166 	if ((flags & CLK_SET_DRYRUN) != 0)
167 		return (0);
168 
169 	p_idx = clknode_get_parent_idx(clk);
170 	if (p_idx != best_parent) {
171 		dprintf("Switching parent index from %d to %d\n", p_idx,
172 		    best_parent);
173 		clknode_set_parent_by_idx(clk, best_parent);
174 	}
175 
176 	clknode_set_freq(p_best_clk, *fout, flags, 0);
177 	clknode_get_freq(p_best_clk, fout);
178 
179 	return (0);
180 }
181 
182 int
rk_clk_mux_register(struct clkdom * clkdom,struct rk_clk_mux_def * clkdef)183 rk_clk_mux_register(struct clkdom *clkdom, struct rk_clk_mux_def *clkdef)
184 {
185 	struct clknode *clk;
186 	struct rk_clk_mux_sc *sc;
187 
188 	clk = clknode_create(clkdom, &rk_clk_mux_class, &clkdef->clkdef);
189 	if (clk == NULL)
190 		return (1);
191 
192 	sc = clknode_get_softc(clk);
193 	sc->offset = clkdef->offset;
194 	sc->shift = clkdef->shift;
195 	sc->mask =  (1 << clkdef->width) - 1;
196 	sc->mux_flags = clkdef->mux_flags;
197 
198 	clknode_register(clkdom, clk);
199 	return (0);
200 }
201