xref: /freebsd/sys/arm/broadcom/bcm2835/bcm2835_clkman.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/cpu.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/rman.h>
40 #include <sys/sema.h>
41 #include <sys/sysctl.h>
42 
43 #include <machine/bus.h>
44 #include <machine/cpu.h>
45 
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
48 
49 #include <arm/broadcom/bcm2835/bcm2835_clkman.h>
50 
51 static struct ofw_compat_data compat_data[] = {
52 	{"brcm,bcm2711-cprman",		1},
53 	{"brcm,bcm2835-cprman",		1},
54 	{"broadcom,bcm2835-cprman",	1},
55 	{NULL,				0}
56 };
57 
58 struct bcm2835_clkman_softc {
59 	device_t		sc_dev;
60 
61 	struct resource *	sc_m_res;
62 	bus_space_tag_t		sc_m_bst;
63 	bus_space_handle_t	sc_m_bsh;
64 };
65 
66 #define BCM_CLKMAN_WRITE(_sc, _off, _val)              \
67     bus_space_write_4(_sc->sc_m_bst, _sc->sc_m_bsh, _off, _val)
68 #define BCM_CLKMAN_READ(_sc, _off)                     \
69     bus_space_read_4(_sc->sc_m_bst, _sc->sc_m_bsh, _off)
70 
71 #define W_CMCLK(_sc, unit, _val) BCM_CLKMAN_WRITE(_sc, unit, 0x5a000000 | (_val))
72 #define R_CMCLK(_sc, unit) BCM_CLKMAN_READ(_sc, unit)
73 #define W_CMDIV(_sc, unit, _val) BCM_CLKMAN_WRITE(_sc, (unit) + 4, 0x5a000000 | (_val))
74 #define R_CMDIV(_sc,  unit) BCM_CLKMAN_READ(_sc, (unit) + 4)
75 
76 static int
77 bcm2835_clkman_probe(device_t dev)
78 {
79 
80 	if (!ofw_bus_status_okay(dev))
81 		return (ENXIO);
82 
83 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
84 		return (ENXIO);
85 
86 	device_set_desc(dev, "BCM283x Clock Manager");
87 
88 	return (BUS_PROBE_DEFAULT);
89 }
90 
91 static int
92 bcm2835_clkman_attach(device_t dev)
93 {
94 	struct bcm2835_clkman_softc *sc;
95 	int rid;
96 
97 	if (device_get_unit(dev) != 0) {
98 		device_printf(dev, "only one clk manager supported\n");
99 		return (ENXIO);
100 	}
101 
102 	sc = device_get_softc(dev);
103 	sc->sc_dev = dev;
104 
105 	rid = 0;
106 	sc->sc_m_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
107 	    RF_ACTIVE);
108 	if (!sc->sc_m_res) {
109 		device_printf(dev, "cannot allocate memory window\n");
110 		return (ENXIO);
111 	}
112 
113 	sc->sc_m_bst = rman_get_bustag(sc->sc_m_res);
114 	sc->sc_m_bsh = rman_get_bushandle(sc->sc_m_res);
115 
116 	return (bus_generic_attach(dev));
117 }
118 
119 uint32_t
120 bcm2835_clkman_set_frequency(device_t dev, uint32_t unit, uint32_t hz)
121 {
122 	struct bcm2835_clkman_softc *sc;
123 	int i;
124 	uint32_t u;
125 
126 	sc = device_get_softc(dev);
127 
128 	if (unit != BCM_PWM_CLKSRC) {
129 		device_printf(sc->sc_dev,
130 		    "Unsupported unit 0x%x", unit);
131 		return (0);
132 	}
133 
134 	W_CMCLK(sc, unit, 6);
135 	for (i = 0; i < 10; i++) {
136 		u = R_CMCLK(sc, unit);
137 		if (!(u&0x80))
138 			break;
139 		DELAY(1000);
140 	}
141 	if (u & 0x80) {
142 		device_printf(sc->sc_dev,
143 		    "Failed to stop clock for unit 0x%x", unit);
144 		return (0);
145 	}
146 	if (hz == 0)
147 		return (0);
148 
149 	u = 500000000/hz;
150 	if (u < 4) {
151 		device_printf(sc->sc_dev,
152 		    "Frequency too high for unit 0x%x (max: 125 MHz)",
153 		    unit);
154 		return (0);
155 	}
156 	if (u > 0xfff) {
157 		device_printf(sc->sc_dev,
158 		    "Frequency too low for unit 0x%x (min: 123 kHz)",
159 		    unit);
160 		return (0);
161 	}
162 	hz = 500000000/u;
163 	W_CMDIV(sc, unit, u << 12);
164 
165 	W_CMCLK(sc, unit, 0x16);
166 	for (i = 0; i < 10; i++) {
167 		u = R_CMCLK(sc, unit);
168 		if ((u&0x80))
169 			break;
170 		DELAY(1000);
171 	}
172 	if (!(u & 0x80)) {
173 		device_printf(sc->sc_dev,
174 		    "Failed to start clock for unit 0x%x", unit);
175 		return (0);
176 	}
177 	return (hz);
178 }
179 
180 static int
181 bcm2835_clkman_detach(device_t dev)
182 {
183 	struct bcm2835_clkman_softc *sc;
184 
185 	bus_generic_detach(dev);
186 
187 	sc = device_get_softc(dev);
188 	if (sc->sc_m_res)
189 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_m_res);
190 
191 	return (0);
192 }
193 
194 static device_method_t bcm2835_clkman_methods[] = {
195 	/* Device interface */
196 	DEVMETHOD(device_probe,		bcm2835_clkman_probe),
197 	DEVMETHOD(device_attach,	bcm2835_clkman_attach),
198 	DEVMETHOD(device_detach,	bcm2835_clkman_detach),
199 
200 	DEVMETHOD_END
201 };
202 
203 static driver_t bcm2835_clkman_driver = {
204 	"bcm2835_clkman",
205 	bcm2835_clkman_methods,
206 	sizeof(struct bcm2835_clkman_softc),
207 };
208 
209 DRIVER_MODULE(bcm2835_clkman, simplebus, bcm2835_clkman_driver, 0, 0);
210 MODULE_VERSION(bcm2835_clkman, 1);
211