xref: /freebsd/sys/dev/clk/clk_fixed.c (revision 1cb9f6f6413e9d242de47e730ec7d4ce3e5688fe)
1be82b3a0SEmmanuel Vadot /*-
2be82b3a0SEmmanuel Vadot  * Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
3be82b3a0SEmmanuel Vadot  * All rights reserved.
4be82b3a0SEmmanuel Vadot  *
5be82b3a0SEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
6be82b3a0SEmmanuel Vadot  * modification, are permitted provided that the following conditions
7be82b3a0SEmmanuel Vadot  * are met:
8be82b3a0SEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
9be82b3a0SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
10be82b3a0SEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
11be82b3a0SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
12be82b3a0SEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
13be82b3a0SEmmanuel Vadot  *
14be82b3a0SEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15be82b3a0SEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16be82b3a0SEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17be82b3a0SEmmanuel Vadot  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18be82b3a0SEmmanuel Vadot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19be82b3a0SEmmanuel Vadot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20be82b3a0SEmmanuel Vadot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21be82b3a0SEmmanuel Vadot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22be82b3a0SEmmanuel Vadot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23be82b3a0SEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24be82b3a0SEmmanuel Vadot  * SUCH DAMAGE.
25be82b3a0SEmmanuel Vadot  */
26be82b3a0SEmmanuel Vadot 
27be82b3a0SEmmanuel Vadot #include <sys/cdefs.h>
28be82b3a0SEmmanuel Vadot #include "opt_platform.h"
29be82b3a0SEmmanuel Vadot #include <sys/param.h>
30be82b3a0SEmmanuel Vadot #include <sys/conf.h>
31be82b3a0SEmmanuel Vadot #include <sys/bus.h>
32be82b3a0SEmmanuel Vadot #include <sys/kernel.h>
33be82b3a0SEmmanuel Vadot #include <sys/kobj.h>
34be82b3a0SEmmanuel Vadot #include <sys/malloc.h>
35be82b3a0SEmmanuel Vadot #include <sys/mutex.h>
36be82b3a0SEmmanuel Vadot #include <sys/module.h>
37be82b3a0SEmmanuel Vadot #include <sys/rman.h>
38be82b3a0SEmmanuel Vadot #include <sys/systm.h>
39be82b3a0SEmmanuel Vadot 
40be82b3a0SEmmanuel Vadot #include <machine/bus.h>
41be82b3a0SEmmanuel Vadot 
42be82b3a0SEmmanuel Vadot #ifdef FDT
43be82b3a0SEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
44be82b3a0SEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
45be82b3a0SEmmanuel Vadot #endif
46be82b3a0SEmmanuel Vadot 
47be82b3a0SEmmanuel Vadot #include <dev/clk/clk_fixed.h>
48be82b3a0SEmmanuel Vadot 
49be82b3a0SEmmanuel Vadot #define	CLK_TYPE_FIXED		1
50be82b3a0SEmmanuel Vadot #define	CLK_TYPE_FIXED_FACTOR	2
51be82b3a0SEmmanuel Vadot 
52be82b3a0SEmmanuel Vadot static int clknode_fixed_init(struct clknode *clk, device_t dev);
53be82b3a0SEmmanuel Vadot static int clknode_fixed_recalc(struct clknode *clk, uint64_t *freq);
54be82b3a0SEmmanuel Vadot static int clknode_fixed_set_freq(struct clknode *clk, uint64_t fin,
55be82b3a0SEmmanuel Vadot     uint64_t *fout, int flags, int *stop);
56be82b3a0SEmmanuel Vadot 
57be82b3a0SEmmanuel Vadot struct clknode_fixed_sc {
58be82b3a0SEmmanuel Vadot 	int		fixed_flags;
59be82b3a0SEmmanuel Vadot 	uint64_t	freq;
60be82b3a0SEmmanuel Vadot 	uint32_t	mult;
61be82b3a0SEmmanuel Vadot 	uint32_t	div;
62be82b3a0SEmmanuel Vadot };
63be82b3a0SEmmanuel Vadot 
64be82b3a0SEmmanuel Vadot static clknode_method_t clknode_fixed_methods[] = {
65be82b3a0SEmmanuel Vadot 	/* Device interface */
66be82b3a0SEmmanuel Vadot 	CLKNODEMETHOD(clknode_init,	   clknode_fixed_init),
67be82b3a0SEmmanuel Vadot 	CLKNODEMETHOD(clknode_recalc_freq, clknode_fixed_recalc),
68be82b3a0SEmmanuel Vadot 	CLKNODEMETHOD(clknode_set_freq,    clknode_fixed_set_freq),
69be82b3a0SEmmanuel Vadot 	CLKNODEMETHOD_END
70be82b3a0SEmmanuel Vadot };
71be82b3a0SEmmanuel Vadot DEFINE_CLASS_1(clknode_fixed, clknode_fixed_class, clknode_fixed_methods,
72be82b3a0SEmmanuel Vadot    sizeof(struct clknode_fixed_sc), clknode_class);
73be82b3a0SEmmanuel Vadot 
74be82b3a0SEmmanuel Vadot static int
clknode_fixed_init(struct clknode * clk,device_t dev)75be82b3a0SEmmanuel Vadot clknode_fixed_init(struct clknode *clk, device_t dev)
76be82b3a0SEmmanuel Vadot {
77be82b3a0SEmmanuel Vadot 	struct clknode_fixed_sc *sc;
78be82b3a0SEmmanuel Vadot 
79be82b3a0SEmmanuel Vadot 	sc = clknode_get_softc(clk);
80be82b3a0SEmmanuel Vadot 	if (sc->freq == 0)
81be82b3a0SEmmanuel Vadot 		clknode_init_parent_idx(clk, 0);
82be82b3a0SEmmanuel Vadot 	return(0);
83be82b3a0SEmmanuel Vadot }
84be82b3a0SEmmanuel Vadot 
85be82b3a0SEmmanuel Vadot static int
clknode_fixed_recalc(struct clknode * clk,uint64_t * freq)86be82b3a0SEmmanuel Vadot clknode_fixed_recalc(struct clknode *clk, uint64_t *freq)
87be82b3a0SEmmanuel Vadot {
88be82b3a0SEmmanuel Vadot 	struct clknode_fixed_sc *sc;
89be82b3a0SEmmanuel Vadot 
90be82b3a0SEmmanuel Vadot 	sc = clknode_get_softc(clk);
91be82b3a0SEmmanuel Vadot 
92be82b3a0SEmmanuel Vadot 	if ((sc->mult != 0) && (sc->div != 0))
93be82b3a0SEmmanuel Vadot 		*freq = (*freq / sc->div) * sc->mult;
94be82b3a0SEmmanuel Vadot 	else
95be82b3a0SEmmanuel Vadot 		*freq = sc->freq;
96be82b3a0SEmmanuel Vadot 	return (0);
97be82b3a0SEmmanuel Vadot }
98be82b3a0SEmmanuel Vadot 
99be82b3a0SEmmanuel Vadot static int
clknode_fixed_set_freq(struct clknode * clk,uint64_t fin,uint64_t * fout,int flags,int * stop)100be82b3a0SEmmanuel Vadot clknode_fixed_set_freq(struct clknode *clk, uint64_t fin, uint64_t *fout,
101be82b3a0SEmmanuel Vadot     int flags, int *stop)
102be82b3a0SEmmanuel Vadot {
103be82b3a0SEmmanuel Vadot 	struct clknode_fixed_sc *sc;
104be82b3a0SEmmanuel Vadot 
105be82b3a0SEmmanuel Vadot 	sc = clknode_get_softc(clk);
106be82b3a0SEmmanuel Vadot 	if (sc->mult == 0 || sc->div == 0) {
107be82b3a0SEmmanuel Vadot 		/* Fixed frequency clock. */
108be82b3a0SEmmanuel Vadot 		*stop = 1;
109be82b3a0SEmmanuel Vadot 		if (*fout != sc->freq)
110be82b3a0SEmmanuel Vadot 			return (ERANGE);
111be82b3a0SEmmanuel Vadot 		return (0);
112be82b3a0SEmmanuel Vadot 	}
113be82b3a0SEmmanuel Vadot 	/* Fixed factor clock. */
114be82b3a0SEmmanuel Vadot 	*stop = 0;
115be82b3a0SEmmanuel Vadot 	*fout = (*fout / sc->mult) *  sc->div;
116be82b3a0SEmmanuel Vadot 	return (0);
117be82b3a0SEmmanuel Vadot }
118be82b3a0SEmmanuel Vadot 
119be82b3a0SEmmanuel Vadot int
clknode_fixed_register(struct clkdom * clkdom,struct clk_fixed_def * clkdef)120be82b3a0SEmmanuel Vadot clknode_fixed_register(struct clkdom *clkdom, struct clk_fixed_def *clkdef)
121be82b3a0SEmmanuel Vadot {
122be82b3a0SEmmanuel Vadot 	struct clknode *clk;
123be82b3a0SEmmanuel Vadot 	struct clknode_fixed_sc *sc;
124be82b3a0SEmmanuel Vadot 
125be82b3a0SEmmanuel Vadot 	clk = clknode_create(clkdom, &clknode_fixed_class, &clkdef->clkdef);
126be82b3a0SEmmanuel Vadot 	if (clk == NULL)
127be82b3a0SEmmanuel Vadot 		return (1);
128be82b3a0SEmmanuel Vadot 
129be82b3a0SEmmanuel Vadot 	sc = clknode_get_softc(clk);
130be82b3a0SEmmanuel Vadot 	sc->fixed_flags = clkdef->fixed_flags;
131be82b3a0SEmmanuel Vadot 	sc->freq = clkdef->freq;
132be82b3a0SEmmanuel Vadot 	sc->mult = clkdef->mult;
133be82b3a0SEmmanuel Vadot 	sc->div = clkdef->div;
134be82b3a0SEmmanuel Vadot 
135be82b3a0SEmmanuel Vadot 	clknode_register(clkdom, clk);
136be82b3a0SEmmanuel Vadot 	return (0);
137be82b3a0SEmmanuel Vadot }
138be82b3a0SEmmanuel Vadot 
139be82b3a0SEmmanuel Vadot #ifdef FDT
140be82b3a0SEmmanuel Vadot 
141be82b3a0SEmmanuel Vadot static struct ofw_compat_data compat_data[] = {
142be82b3a0SEmmanuel Vadot 	{"fixed-clock",		CLK_TYPE_FIXED},
143be82b3a0SEmmanuel Vadot 	{"fixed-factor-clock",  CLK_TYPE_FIXED_FACTOR},
144be82b3a0SEmmanuel Vadot 	{NULL,		 	0},
145be82b3a0SEmmanuel Vadot };
146be82b3a0SEmmanuel Vadot 
147be82b3a0SEmmanuel Vadot struct clk_fixed_softc {
148be82b3a0SEmmanuel Vadot 	device_t	dev;
149be82b3a0SEmmanuel Vadot 	struct clkdom	*clkdom;
150be82b3a0SEmmanuel Vadot };
151be82b3a0SEmmanuel Vadot 
152be82b3a0SEmmanuel Vadot static int
clk_fixed_probe(device_t dev)153be82b3a0SEmmanuel Vadot clk_fixed_probe(device_t dev)
154be82b3a0SEmmanuel Vadot {
155be82b3a0SEmmanuel Vadot 	intptr_t clk_type;
156be82b3a0SEmmanuel Vadot 
157be82b3a0SEmmanuel Vadot 	clk_type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
158be82b3a0SEmmanuel Vadot 	switch (clk_type) {
159be82b3a0SEmmanuel Vadot 	case CLK_TYPE_FIXED:
160be82b3a0SEmmanuel Vadot 		if (OF_hasprop(ofw_bus_get_node(dev), "clock-frequency") == 0) {
161be82b3a0SEmmanuel Vadot 			device_printf(dev,
162be82b3a0SEmmanuel Vadot 			    "clock-fixed has no clock-frequency\n");
163be82b3a0SEmmanuel Vadot 			return (ENXIO);
164be82b3a0SEmmanuel Vadot 		}
165be82b3a0SEmmanuel Vadot 		device_set_desc(dev, "Fixed clock");
166*1cb9f6f6SMitchell Horne 		break;
167be82b3a0SEmmanuel Vadot 	case CLK_TYPE_FIXED_FACTOR:
168be82b3a0SEmmanuel Vadot 		device_set_desc(dev, "Fixed factor clock");
169*1cb9f6f6SMitchell Horne 		break;
170be82b3a0SEmmanuel Vadot 	default:
171be82b3a0SEmmanuel Vadot 		return (ENXIO);
172be82b3a0SEmmanuel Vadot 	}
173*1cb9f6f6SMitchell Horne 
174*1cb9f6f6SMitchell Horne 	if (!bootverbose)
175*1cb9f6f6SMitchell Horne 		device_quiet(dev);
176*1cb9f6f6SMitchell Horne 
177*1cb9f6f6SMitchell Horne 	return (BUS_PROBE_DEFAULT);
178be82b3a0SEmmanuel Vadot }
179be82b3a0SEmmanuel Vadot 
180be82b3a0SEmmanuel Vadot static int
clk_fixed_init_fixed(struct clk_fixed_softc * sc,phandle_t node,struct clk_fixed_def * def)181be82b3a0SEmmanuel Vadot clk_fixed_init_fixed(struct clk_fixed_softc *sc, phandle_t node,
182be82b3a0SEmmanuel Vadot     struct clk_fixed_def *def)
183be82b3a0SEmmanuel Vadot {
184be82b3a0SEmmanuel Vadot 	uint32_t freq;
185be82b3a0SEmmanuel Vadot 	int rv;
186be82b3a0SEmmanuel Vadot 
187be82b3a0SEmmanuel Vadot 	def->clkdef.id = 1;
188be82b3a0SEmmanuel Vadot 	rv = OF_getencprop(node, "clock-frequency", &freq,  sizeof(freq));
189be82b3a0SEmmanuel Vadot 	if (rv <= 0)
190be82b3a0SEmmanuel Vadot 		return (ENXIO);
191be82b3a0SEmmanuel Vadot 	def->freq = freq;
192be82b3a0SEmmanuel Vadot 	return (0);
193be82b3a0SEmmanuel Vadot }
194be82b3a0SEmmanuel Vadot 
195be82b3a0SEmmanuel Vadot static int
clk_fixed_init_fixed_factor(struct clk_fixed_softc * sc,phandle_t node,struct clk_fixed_def * def)196be82b3a0SEmmanuel Vadot clk_fixed_init_fixed_factor(struct clk_fixed_softc *sc, phandle_t node,
197be82b3a0SEmmanuel Vadot     struct clk_fixed_def *def)
198be82b3a0SEmmanuel Vadot {
199be82b3a0SEmmanuel Vadot 	int rv;
200be82b3a0SEmmanuel Vadot 	clk_t  parent;
201be82b3a0SEmmanuel Vadot 
202be82b3a0SEmmanuel Vadot 	def->clkdef.id = 1;
203be82b3a0SEmmanuel Vadot 	rv = OF_getencprop(node, "clock-mult", &def->mult,  sizeof(def->mult));
204be82b3a0SEmmanuel Vadot 	if (rv <= 0)
205be82b3a0SEmmanuel Vadot 		return (ENXIO);
206be82b3a0SEmmanuel Vadot 	rv = OF_getencprop(node, "clock-div", &def->div,  sizeof(def->div));
207be82b3a0SEmmanuel Vadot 	if (rv <= 0)
208be82b3a0SEmmanuel Vadot 		return (ENXIO);
209be82b3a0SEmmanuel Vadot 	/* Get name of parent clock */
210be82b3a0SEmmanuel Vadot 	rv = clk_get_by_ofw_index(sc->dev, 0, 0, &parent);
211be82b3a0SEmmanuel Vadot 	if (rv != 0)
212be82b3a0SEmmanuel Vadot 		return (ENXIO);
213be82b3a0SEmmanuel Vadot 	def->clkdef.parent_names = malloc(sizeof(char *), M_OFWPROP, M_WAITOK);
214be82b3a0SEmmanuel Vadot 	def->clkdef.parent_names[0] = clk_get_name(parent);
215be82b3a0SEmmanuel Vadot 	def->clkdef.parent_cnt  = 1;
216be82b3a0SEmmanuel Vadot 	clk_release(parent);
217be82b3a0SEmmanuel Vadot 	return (0);
218be82b3a0SEmmanuel Vadot }
219be82b3a0SEmmanuel Vadot 
220be82b3a0SEmmanuel Vadot static int
clk_fixed_attach(device_t dev)221be82b3a0SEmmanuel Vadot clk_fixed_attach(device_t dev)
222be82b3a0SEmmanuel Vadot {
223be82b3a0SEmmanuel Vadot 	struct clk_fixed_softc *sc;
224be82b3a0SEmmanuel Vadot 	intptr_t clk_type;
225be82b3a0SEmmanuel Vadot 	phandle_t node;
226be82b3a0SEmmanuel Vadot 	struct clk_fixed_def def;
227be82b3a0SEmmanuel Vadot 	int rv;
228be82b3a0SEmmanuel Vadot 
229be82b3a0SEmmanuel Vadot 	sc = device_get_softc(dev);
230be82b3a0SEmmanuel Vadot 	sc->dev = dev;
231be82b3a0SEmmanuel Vadot 	node  = ofw_bus_get_node(dev);
232be82b3a0SEmmanuel Vadot 	clk_type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
233be82b3a0SEmmanuel Vadot 
234be82b3a0SEmmanuel Vadot 	bzero(&def, sizeof(def));
235be82b3a0SEmmanuel Vadot 	if (clk_type == CLK_TYPE_FIXED)
236be82b3a0SEmmanuel Vadot 		rv = clk_fixed_init_fixed(sc, node, &def);
237be82b3a0SEmmanuel Vadot 	else if (clk_type == CLK_TYPE_FIXED_FACTOR)
238be82b3a0SEmmanuel Vadot 		rv = clk_fixed_init_fixed_factor(sc, node, &def);
239be82b3a0SEmmanuel Vadot 	else
240be82b3a0SEmmanuel Vadot 		rv = ENXIO;
241be82b3a0SEmmanuel Vadot 	if (rv != 0) {
242be82b3a0SEmmanuel Vadot 		device_printf(sc->dev, "Cannot FDT parameters.\n");
243be82b3a0SEmmanuel Vadot 		goto fail;
244be82b3a0SEmmanuel Vadot 	}
245be82b3a0SEmmanuel Vadot 	rv = clk_parse_ofw_clk_name(dev, node, &def.clkdef.name);
246be82b3a0SEmmanuel Vadot 	if (rv != 0) {
247be82b3a0SEmmanuel Vadot 		device_printf(sc->dev, "Cannot parse clock name.\n");
248be82b3a0SEmmanuel Vadot 		goto fail;
249be82b3a0SEmmanuel Vadot 	}
250be82b3a0SEmmanuel Vadot 	sc->clkdom = clkdom_create(dev);
251be82b3a0SEmmanuel Vadot 	KASSERT(sc->clkdom != NULL, ("Clock domain is NULL"));
252be82b3a0SEmmanuel Vadot 
253be82b3a0SEmmanuel Vadot 	rv = clknode_fixed_register(sc->clkdom, &def);
254be82b3a0SEmmanuel Vadot 	if (rv != 0) {
255be82b3a0SEmmanuel Vadot 		device_printf(sc->dev, "Cannot register fixed clock.\n");
256be82b3a0SEmmanuel Vadot 		rv = ENXIO;
257be82b3a0SEmmanuel Vadot 		goto fail;
258be82b3a0SEmmanuel Vadot 	}
259be82b3a0SEmmanuel Vadot 
260be82b3a0SEmmanuel Vadot 	rv = clkdom_finit(sc->clkdom);
261be82b3a0SEmmanuel Vadot 	if (rv != 0) {
262be82b3a0SEmmanuel Vadot 		device_printf(sc->dev, "Clk domain finit fails.\n");
263be82b3a0SEmmanuel Vadot 		rv = ENXIO;
264be82b3a0SEmmanuel Vadot 		goto fail;
265be82b3a0SEmmanuel Vadot 	}
2666e66bb9fSMitchell Horne 
2676e66bb9fSMitchell Horne 	if (bootverbose)
268be82b3a0SEmmanuel Vadot 		clkdom_dump(sc->clkdom);
2696e66bb9fSMitchell Horne 
270be82b3a0SEmmanuel Vadot 	OF_prop_free(__DECONST(char *, def.clkdef.name));
271be82b3a0SEmmanuel Vadot 	OF_prop_free(def.clkdef.parent_names);
272be82b3a0SEmmanuel Vadot 	return (bus_generic_attach(dev));
273be82b3a0SEmmanuel Vadot 
274be82b3a0SEmmanuel Vadot fail:
275be82b3a0SEmmanuel Vadot 	OF_prop_free(__DECONST(char *, def.clkdef.name));
276be82b3a0SEmmanuel Vadot 	OF_prop_free(def.clkdef.parent_names);
277be82b3a0SEmmanuel Vadot 	return (rv);
278be82b3a0SEmmanuel Vadot }
279be82b3a0SEmmanuel Vadot 
280be82b3a0SEmmanuel Vadot static device_method_t clk_fixed_methods[] = {
281be82b3a0SEmmanuel Vadot 	/* Device interface */
282be82b3a0SEmmanuel Vadot 	DEVMETHOD(device_probe,		clk_fixed_probe),
283be82b3a0SEmmanuel Vadot 	DEVMETHOD(device_attach,	clk_fixed_attach),
284be82b3a0SEmmanuel Vadot 
285be82b3a0SEmmanuel Vadot 	DEVMETHOD_END
286be82b3a0SEmmanuel Vadot };
287be82b3a0SEmmanuel Vadot 
288be82b3a0SEmmanuel Vadot DEFINE_CLASS_0(clk_fixed, clk_fixed_driver, clk_fixed_methods,
289be82b3a0SEmmanuel Vadot     sizeof(struct clk_fixed_softc));
290be82b3a0SEmmanuel Vadot EARLY_DRIVER_MODULE(clk_fixed, simplebus, clk_fixed_driver, 0, 0,
291be82b3a0SEmmanuel Vadot     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
292be82b3a0SEmmanuel Vadot MODULE_VERSION(clk_fixed, 1);
293be82b3a0SEmmanuel Vadot 
294be82b3a0SEmmanuel Vadot #endif
295