xref: /freebsd/sys/arm/broadcom/bcm2835/bcm2835_clkman.c (revision 47dd1d1b619cc035b82b49a91a25544309ff95ae)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2017 Poul-Henning Kamp <phk@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
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 #include <sys/cpu.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/rman.h>
42 #include <sys/sema.h>
43 #include <sys/sysctl.h>
44 
45 #include <machine/bus.h>
46 #include <machine/cpu.h>
47 
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
50 
51 #include <arm/broadcom/bcm2835/bcm2835_clkman.h>
52 
53 static struct ofw_compat_data compat_data[] = {
54 	{"brcm,bcm2835-cprman",		1},
55 	{"broadcom,bcm2835-cprman",	1},
56 	{NULL,				0}
57 };
58 
59 struct bcm2835_clkman_softc {
60 	device_t		sc_dev;
61 
62 	struct resource *	sc_m_res;
63 	bus_space_tag_t		sc_m_bst;
64 	bus_space_handle_t	sc_m_bsh;
65 };
66 
67 #define BCM_CLKMAN_WRITE(_sc, _off, _val)              \
68     bus_space_write_4(_sc->sc_m_bst, _sc->sc_m_bsh, _off, _val)
69 #define BCM_CLKMAN_READ(_sc, _off)                     \
70     bus_space_read_4(_sc->sc_m_bst, _sc->sc_m_bsh, _off)
71 
72 #define W_CMCLK(_sc, unit, _val) BCM_CLKMAN_WRITE(_sc, unit, 0x5a000000 | (_val))
73 #define R_CMCLK(_sc, unit) BCM_CLKMAN_READ(_sc, unit)
74 #define W_CMDIV(_sc, unit, _val) BCM_CLKMAN_WRITE(_sc, (unit) + 4, 0x5a000000 | (_val))
75 #define R_CMDIV(_sc,  unit) BCM_CLKMAN_READ(_sc, (unit) + 4)
76 
77 static int
78 bcm2835_clkman_probe(device_t dev)
79 {
80 
81 	if (!ofw_bus_status_okay(dev))
82 		return (ENXIO);
83 
84 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
85 		return (ENXIO);
86 
87 	device_set_desc(dev, "BCM283x Clock Manager");
88 
89 	return (BUS_PROBE_DEFAULT);
90 }
91 
92 static int
93 bcm2835_clkman_attach(device_t dev)
94 {
95 	struct bcm2835_clkman_softc *sc;
96 	int rid;
97 
98 	if (device_get_unit(dev) != 0) {
99 		device_printf(dev, "only one clk manager supported\n");
100 		return (ENXIO);
101 	}
102 
103 	sc = device_get_softc(dev);
104 	sc->sc_dev = dev;
105 
106 	rid = 0;
107 	sc->sc_m_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
108 	    RF_ACTIVE);
109 	if (!sc->sc_m_res) {
110 		device_printf(dev, "cannot allocate memory window\n");
111 		return (ENXIO);
112 	}
113 
114 	sc->sc_m_bst = rman_get_bustag(sc->sc_m_res);
115 	sc->sc_m_bsh = rman_get_bushandle(sc->sc_m_res);
116 
117 	return (bus_generic_attach(dev));
118 }
119 
120 uint32_t
121 bcm2835_clkman_set_frequency(device_t dev, uint32_t unit, uint32_t hz)
122 {
123 	struct bcm2835_clkman_softc *sc;
124 	int i;
125 	uint32_t u;
126 
127 	sc = device_get_softc(dev);
128 
129 	if (unit != BCM_PWM_CLKSRC) {
130 		device_printf(sc->sc_dev,
131 		    "Unsupported unit 0x%x", unit);
132 		return (0);
133 	}
134 
135 	W_CMCLK(sc, unit, 6);
136 	for (i = 0; i < 10; i++) {
137 		u = R_CMCLK(sc, unit);
138 		if (!(u&0x80))
139 			break;
140 		DELAY(1000);
141 	}
142 	if (u & 0x80) {
143 		device_printf(sc->sc_dev,
144 		    "Failed to stop clock for unit 0x%x", unit);
145 		return (0);
146 	}
147 	if (hz == 0)
148 		return (0);
149 
150 	u = 500000000/hz;
151 	if (u < 4) {
152 		device_printf(sc->sc_dev,
153 		    "Frequency too high for unit 0x%x (max: 125 MHz)",
154 		    unit);
155 		return (0);
156 	}
157 	if (u > 0xfff) {
158 		device_printf(sc->sc_dev,
159 		    "Frequency too low for unit 0x%x (min: 123 kHz)",
160 		    unit);
161 		return (0);
162 	}
163 	hz = 500000000/u;
164 	W_CMDIV(sc, unit, u << 12);
165 
166 	W_CMCLK(sc, unit, 0x16);
167 	for (i = 0; i < 10; i++) {
168 		u = R_CMCLK(sc, unit);
169 		if ((u&0x80))
170 			break;
171 		DELAY(1000);
172 	}
173 	if (!(u & 0x80)) {
174 		device_printf(sc->sc_dev,
175 		    "Failed to start clock for unit 0x%x", unit);
176 		return (0);
177 	}
178 	return (hz);
179 }
180 
181 static int
182 bcm2835_clkman_detach(device_t dev)
183 {
184 	struct bcm2835_clkman_softc *sc;
185 
186 	bus_generic_detach(dev);
187 
188 	sc = device_get_softc(dev);
189 	if (sc->sc_m_res)
190 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_m_res);
191 
192 	return (0);
193 }
194 
195 static device_method_t bcm2835_clkman_methods[] = {
196 	/* Device interface */
197 	DEVMETHOD(device_probe,		bcm2835_clkman_probe),
198 	DEVMETHOD(device_attach,	bcm2835_clkman_attach),
199 	DEVMETHOD(device_detach,	bcm2835_clkman_detach),
200 
201 	DEVMETHOD_END
202 };
203 
204 static devclass_t bcm2835_clkman_devclass;
205 static driver_t bcm2835_clkman_driver = {
206 	"bcm2835_clkman",
207 	bcm2835_clkman_methods,
208 	sizeof(struct bcm2835_clkman_softc),
209 };
210 
211 DRIVER_MODULE(bcm2835_clkman, simplebus, bcm2835_clkman_driver,
212     bcm2835_clkman_devclass, 0, 0);
213 MODULE_VERSION(bcm2835_clkman, 1);
214