xref: /freebsd/sys/dev/clk/clk_fixed.c (revision a3cefe7f2b4df0f70ff92d4570ce18e517af43ec)
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 "opt_platform.h"
29 #include <sys/param.h>
30 #include <sys/conf.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/kobj.h>
34 #include <sys/malloc.h>
35 #include <sys/mutex.h>
36 #include <sys/module.h>
37 #include <sys/rman.h>
38 #include <sys/systm.h>
39 
40 #include <machine/bus.h>
41 
42 #ifdef FDT
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45 #endif
46 
47 #include <dev/clk/clk_fixed.h>
48 
49 #define	CLK_TYPE_FIXED		1
50 #define	CLK_TYPE_FIXED_FACTOR	2
51 
52 static int clknode_fixed_init(struct clknode *clk, device_t dev);
53 static int clknode_fixed_recalc(struct clknode *clk, uint64_t *freq);
54 static int clknode_fixed_set_freq(struct clknode *clk, uint64_t fin,
55     uint64_t *fout, int flags, int *stop);
56 
57 struct clknode_fixed_sc {
58 	int		fixed_flags;
59 	uint64_t	freq;
60 	uint32_t	mult;
61 	uint32_t	div;
62 };
63 
64 static clknode_method_t clknode_fixed_methods[] = {
65 	/* Device interface */
66 	CLKNODEMETHOD(clknode_init,	   clknode_fixed_init),
67 	CLKNODEMETHOD(clknode_recalc_freq, clknode_fixed_recalc),
68 	CLKNODEMETHOD(clknode_set_freq,    clknode_fixed_set_freq),
69 	CLKNODEMETHOD_END
70 };
71 DEFINE_CLASS_1(clknode_fixed, clknode_fixed_class, clknode_fixed_methods,
72    sizeof(struct clknode_fixed_sc), clknode_class);
73 
74 static int
75 clknode_fixed_init(struct clknode *clk, device_t dev)
76 {
77 	struct clknode_fixed_sc *sc;
78 
79 	sc = clknode_get_softc(clk);
80 	if (sc->freq == 0)
81 		clknode_init_parent_idx(clk, 0);
82 	return(0);
83 }
84 
85 static int
86 clknode_fixed_recalc(struct clknode *clk, uint64_t *freq)
87 {
88 	struct clknode_fixed_sc *sc;
89 
90 	sc = clknode_get_softc(clk);
91 
92 	if ((sc->mult != 0) && (sc->div != 0))
93 		*freq = (*freq / sc->div) * sc->mult;
94 	else
95 		*freq = sc->freq;
96 	return (0);
97 }
98 
99 static int
100 clknode_fixed_set_freq(struct clknode *clk, uint64_t fin, uint64_t *fout,
101     int flags, int *stop)
102 {
103 	struct clknode_fixed_sc *sc;
104 
105 	sc = clknode_get_softc(clk);
106 	if (sc->mult == 0 || sc->div == 0) {
107 		/* Fixed frequency clock. */
108 		*stop = 1;
109 		if (*fout != sc->freq)
110 			return (ERANGE);
111 		return (0);
112 	}
113 	/* Fixed factor clock. */
114 	*stop = 0;
115 	*fout = (*fout / sc->mult) *  sc->div;
116 	return (0);
117 }
118 
119 int
120 clknode_fixed_register(struct clkdom *clkdom, struct clk_fixed_def *clkdef)
121 {
122 	struct clknode *clk;
123 	struct clknode_fixed_sc *sc;
124 
125 	clk = clknode_create(clkdom, &clknode_fixed_class, &clkdef->clkdef);
126 	if (clk == NULL)
127 		return (1);
128 
129 	sc = clknode_get_softc(clk);
130 	sc->fixed_flags = clkdef->fixed_flags;
131 	sc->freq = clkdef->freq;
132 	sc->mult = clkdef->mult;
133 	sc->div = clkdef->div;
134 
135 	clknode_register(clkdom, clk);
136 	return (0);
137 }
138 
139 #ifdef FDT
140 
141 static struct ofw_compat_data compat_data[] = {
142 	{"fixed-clock",		CLK_TYPE_FIXED},
143 	{"fixed-factor-clock",  CLK_TYPE_FIXED_FACTOR},
144 	{NULL,		 	0},
145 };
146 
147 struct clk_fixed_softc {
148 	device_t	dev;
149 	struct clkdom	*clkdom;
150 };
151 
152 static int
153 clk_fixed_probe(device_t dev)
154 {
155 	intptr_t clk_type;
156 
157 	clk_type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
158 	switch (clk_type) {
159 	case CLK_TYPE_FIXED:
160 		if (!OF_hasprop(ofw_bus_get_node(dev), "clock-frequency")) {
161 			if (bootverbose) {
162 				device_printf(dev,
163 				    "clock-fixed has no clock-frequency\n");
164 			}
165 			return (ENXIO);
166 		}
167 		device_set_desc(dev, "Fixed clock");
168 		break;
169 	case CLK_TYPE_FIXED_FACTOR:
170 		device_set_desc(dev, "Fixed factor clock");
171 		break;
172 	default:
173 		return (ENXIO);
174 	}
175 
176 	if (!bootverbose)
177 		device_quiet(dev);
178 
179 	return (BUS_PROBE_DEFAULT);
180 }
181 
182 static int
183 clk_fixed_init_fixed(struct clk_fixed_softc *sc, phandle_t node,
184     struct clk_fixed_def *def)
185 {
186 	uint32_t freq;
187 	int rv;
188 
189 	def->clkdef.id = 1;
190 	rv = OF_getencprop(node, "clock-frequency", &freq,  sizeof(freq));
191 	if (rv <= 0)
192 		return (ENXIO);
193 	def->freq = freq;
194 	return (0);
195 }
196 
197 static int
198 clk_fixed_init_fixed_factor(struct clk_fixed_softc *sc, phandle_t node,
199     struct clk_fixed_def *def)
200 {
201 	int rv;
202 	clk_t  parent;
203 
204 	def->clkdef.id = 1;
205 	rv = OF_getencprop(node, "clock-mult", &def->mult,  sizeof(def->mult));
206 	if (rv <= 0)
207 		return (ENXIO);
208 	rv = OF_getencprop(node, "clock-div", &def->div,  sizeof(def->div));
209 	if (rv <= 0)
210 		return (ENXIO);
211 	/* Get name of parent clock */
212 	rv = clk_get_by_ofw_index(sc->dev, 0, 0, &parent);
213 	if (rv != 0)
214 		return (ENXIO);
215 	def->clkdef.parent_names = malloc(sizeof(char *), M_OFWPROP, M_WAITOK);
216 	def->clkdef.parent_names[0] = clk_get_name(parent);
217 	def->clkdef.parent_cnt  = 1;
218 	clk_release(parent);
219 	return (0);
220 }
221 
222 static int
223 clk_fixed_attach(device_t dev)
224 {
225 	struct clk_fixed_softc *sc;
226 	intptr_t clk_type;
227 	phandle_t node;
228 	struct clk_fixed_def def;
229 	int rv;
230 
231 	sc = device_get_softc(dev);
232 	sc->dev = dev;
233 	node  = ofw_bus_get_node(dev);
234 	clk_type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
235 
236 	bzero(&def, sizeof(def));
237 	if (clk_type == CLK_TYPE_FIXED)
238 		rv = clk_fixed_init_fixed(sc, node, &def);
239 	else if (clk_type == CLK_TYPE_FIXED_FACTOR)
240 		rv = clk_fixed_init_fixed_factor(sc, node, &def);
241 	else
242 		rv = ENXIO;
243 	if (rv != 0) {
244 		device_printf(sc->dev, "Cannot FDT parameters.\n");
245 		goto fail;
246 	}
247 	rv = clk_parse_ofw_clk_name(dev, node, &def.clkdef.name);
248 	if (rv != 0) {
249 		device_printf(sc->dev, "Cannot parse clock name.\n");
250 		goto fail;
251 	}
252 	sc->clkdom = clkdom_create(dev);
253 	KASSERT(sc->clkdom != NULL, ("Clock domain is NULL"));
254 
255 	rv = clknode_fixed_register(sc->clkdom, &def);
256 	if (rv != 0) {
257 		device_printf(sc->dev, "Cannot register fixed clock.\n");
258 		rv = ENXIO;
259 		goto fail;
260 	}
261 
262 	rv = clkdom_finit(sc->clkdom);
263 	if (rv != 0) {
264 		device_printf(sc->dev, "Clk domain finit fails.\n");
265 		rv = ENXIO;
266 		goto fail;
267 	}
268 
269 	if (bootverbose)
270 		clkdom_dump(sc->clkdom);
271 
272 	OF_prop_free(__DECONST(char *, def.clkdef.name));
273 	OF_prop_free(def.clkdef.parent_names);
274 	bus_attach_children(dev);
275 	return (0);
276 
277 fail:
278 	OF_prop_free(__DECONST(char *, def.clkdef.name));
279 	OF_prop_free(def.clkdef.parent_names);
280 	return (rv);
281 }
282 
283 static device_method_t clk_fixed_methods[] = {
284 	/* Device interface */
285 	DEVMETHOD(device_probe,		clk_fixed_probe),
286 	DEVMETHOD(device_attach,	clk_fixed_attach),
287 
288 	DEVMETHOD_END
289 };
290 
291 DEFINE_CLASS_0(clk_fixed, clk_fixed_driver, clk_fixed_methods,
292     sizeof(struct clk_fixed_softc));
293 EARLY_DRIVER_MODULE(clk_fixed, simplebus, clk_fixed_driver, 0, 0,
294     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
295 MODULE_VERSION(clk_fixed, 1);
296 
297 #endif
298