xref: /freebsd/sys/arm/ti/ti_prcm.c (revision 2ff63af9b88c7413b7d71715b5532625752a248e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012 Damjan Marion <dmarion@Freebsd.org>
5  * All rights reserved.
6  *
7  * Copyright (c) 2020 Oskar Holmlund <oskar.holmlund@ohdata.se>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 /* Based on sys/arm/ti/am335x/am335x_prcm.c */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/malloc.h>
42 #include <sys/rman.h>
43 #include <sys/timeet.h>
44 #include <sys/timetc.h>
45 #include <sys/watchdog.h>
46 #include <machine/bus.h>
47 #include <machine/cpu.h>
48 #include <machine/intr.h>
49 
50 #include <arm/ti/ti_cpuid.h>
51 #include <arm/ti/ti_prcm.h>
52 #include <arm/ti/tivar.h>
53 
54 #include <dev/fdt/simplebus.h>
55 
56 #include <dev/ofw/openfirm.h>
57 #include <dev/ofw/ofw_bus.h>
58 #include <dev/ofw/ofw_bus_subr.h>
59 
60 #include "clkdev_if.h"
61 
62 #if 0
63 #define DPRINTF(dev, msg...) device_printf(dev, msg)
64 #else
65 #define DPRINTF(dev, msg...)
66 #endif
67 
68 struct ti_prcm_softc {
69 	struct simplebus_softc  sc_simplebus;
70 	device_t		dev;
71 	struct resource *	mem_res;
72 	bus_space_tag_t		bst;
73 	bus_space_handle_t	bsh;
74 	int			attach_done;
75 	struct mtx		mtx;
76 };
77 
78 static struct ti_prcm_softc *ti_prcm_sc = NULL;
79 static void omap4_prcm_reset(void);
80 static void am335x_prcm_reset(void);
81 
82 #define TI_AM3_PRCM		18
83 #define TI_AM4_PRCM		17
84 #define TI_OMAP2_PRCM		16
85 #define TI_OMAP3_PRM		15
86 #define TI_OMAP3_CM		14
87 #define TI_OMAP4_CM1		13
88 #define TI_OMAP4_PRM		12
89 #define TI_OMAP4_CM2		11
90 #define TI_OMAP4_SCRM		10
91 #define TI_OMAP5_PRM		9
92 #define TI_OMAP5_CM_CORE_AON	8
93 #define TI_OMAP5_SCRM		7
94 #define TI_OMAP5_CM_CORE	6
95 #define TI_DRA7_PRM		5
96 #define TI_DRA7_CM_CORE_AON	4
97 #define TI_DRA7_CM_CORE		3
98 #define TI_DM814_PRCM		2
99 #define TI_DM816_PRCM		1
100 #define TI_PRCM_END		0
101 
102 static struct ofw_compat_data compat_data[] = {
103 	{ "ti,am3-prcm",		TI_AM3_PRCM },
104 	{ "ti,am4-prcm",		TI_AM4_PRCM },
105 	{ "ti,omap2-prcm",		TI_OMAP2_PRCM },
106 	{ "ti,omap3-prm",		TI_OMAP3_PRM },
107 	{ "ti,omap3-cm",		TI_OMAP3_CM },
108 	{ "ti,omap4-cm1",		TI_OMAP4_CM1 },
109 	{ "ti,omap4-prm",		TI_OMAP4_PRM },
110 	{ "ti,omap4-cm2",		TI_OMAP4_CM2 },
111 	{ "ti,omap4-scrm",		TI_OMAP4_SCRM },
112 	{ "ti,omap5-prm",		TI_OMAP5_PRM },
113 	{ "ti,omap5-cm-core-aon",	TI_OMAP5_CM_CORE_AON },
114 	{ "ti,omap5-scrm",		TI_OMAP5_SCRM },
115 	{ "ti,omap5-cm-core",		TI_OMAP5_CM_CORE },
116 	{ "ti,dra7-prm",		TI_DRA7_PRM },
117 	{ "ti,dra7-cm-core-aon",	TI_DRA7_CM_CORE_AON },
118 	{ "ti,dra7-cm-core",		TI_DRA7_CM_CORE },
119 	{ "ti,dm814-prcm",		TI_DM814_PRCM },
120 	{ "ti,dm816-prcm",		TI_DM816_PRCM },
121 	{ NULL,				TI_PRCM_END}
122 };
123 
124 static int
125 ti_prcm_probe(device_t dev)
126 {
127 	if (!ofw_bus_status_okay(dev))
128 		return (ENXIO);
129 
130 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) {
131 		return (ENXIO);
132 	}
133 
134 	device_set_desc(dev, "TI Power and Clock Management");
135 	return(BUS_PROBE_DEFAULT);
136 }
137 
138 static int
139 ti_prcm_attach(device_t dev)
140 {
141 	struct ti_prcm_softc *sc;
142 	phandle_t node, child;
143 	int rid;
144 
145 	sc = device_get_softc(dev);
146 	sc->dev = dev;
147 
148 	node = ofw_bus_get_node(sc->dev);
149 	simplebus_init(sc->dev, node);
150 
151 	if (simplebus_fill_ranges(node, &sc->sc_simplebus) < 0) {
152 		device_printf(sc->dev, "could not get ranges\n");
153 		return (ENXIO);
154 	}
155 	if (sc->sc_simplebus.nranges == 0) {
156 		device_printf(sc->dev, "nranges == 0\n");
157 		return (ENXIO);
158 	}
159 
160 	sc->mem_res = bus_alloc_resource(sc->dev, SYS_RES_MEMORY, &rid,
161 		sc->sc_simplebus.ranges[0].host,
162 		(sc->sc_simplebus.ranges[0].host +
163 			sc->sc_simplebus.ranges[0].size - 1),
164 		sc->sc_simplebus.ranges[0].size,
165 		RF_ACTIVE | RF_SHAREABLE);
166 
167 	if (sc->mem_res == NULL) {
168 		return (ENXIO);
169 	}
170 
171 	sc->bst = rman_get_bustag(sc->mem_res);
172 	sc->bsh = rman_get_bushandle(sc->mem_res);
173 
174 	mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
175 
176 	/* Fixme: for xxx_prcm_reset functions.
177 	 * Get rid of global variables?
178 	 */
179 	ti_prcm_sc = sc;
180 
181 	switch(ti_chip()) {
182 #ifdef SOC_OMAP4
183 	case CHIP_OMAP_4:
184 		ti_cpu_reset = omap4_prcm_reset;
185 		break;
186 #endif
187 #ifdef SOC_TI_AM335X
188 	case CHIP_AM335X:
189 		ti_cpu_reset = am335x_prcm_reset;
190 		break;
191 #endif
192 	}
193 
194 	bus_generic_probe(sc->dev);
195 	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
196 		simplebus_add_device(dev, child, 0, NULL, -1, NULL);
197 	}
198 
199 	return (bus_generic_attach(sc->dev));
200 }
201 
202 int
203 ti_prcm_write_4(device_t dev, bus_addr_t addr, uint32_t val)
204 {
205 	struct ti_prcm_softc *sc;
206 
207 	sc = device_get_softc(dev);
208 	DPRINTF(sc->dev, "offset=%lx write %x\n", addr, val);
209 	bus_space_write_4(sc->bst, sc->bsh, addr, val);
210 	return (0);
211 }
212 int
213 ti_prcm_read_4(device_t dev, bus_addr_t addr, uint32_t *val)
214 {
215 	struct ti_prcm_softc *sc;
216 
217 	sc = device_get_softc(dev);
218 
219 	*val = bus_space_read_4(sc->bst, sc->bsh, addr);
220 	DPRINTF(sc->dev, "offset=%lx Read %x\n", addr, *val);
221 	return (0);
222 }
223 
224 int
225 ti_prcm_modify_4(device_t dev, bus_addr_t addr, uint32_t clr, uint32_t set)
226 {
227 	struct ti_prcm_softc *sc;
228 	uint32_t reg;
229 
230 	sc = device_get_softc(dev);
231 
232 	reg = bus_space_read_4(sc->bst, sc->bsh, addr);
233 	reg &= ~clr;
234 	reg |= set;
235 	bus_space_write_4(sc->bst, sc->bsh, addr, reg);
236 	DPRINTF(sc->dev, "offset=%lx reg: %x (clr %x set %x)\n", addr, reg, clr, set);
237 
238 	return (0);
239 }
240 
241 void
242 ti_prcm_device_lock(device_t dev)
243 {
244 	struct ti_prcm_softc *sc;
245 
246 	sc = device_get_softc(dev);
247 	mtx_lock(&sc->mtx);
248 }
249 
250 void
251 ti_prcm_device_unlock(device_t dev)
252 {
253 	struct ti_prcm_softc *sc;
254 
255 	sc = device_get_softc(dev);
256 	mtx_unlock(&sc->mtx);
257 }
258 
259 static device_method_t ti_prcm_methods[] = {
260 	DEVMETHOD(device_probe,		ti_prcm_probe),
261 	DEVMETHOD(device_attach,	ti_prcm_attach),
262 
263 	/* clkdev interface */
264 	DEVMETHOD(clkdev_write_4,	ti_prcm_write_4),
265 	DEVMETHOD(clkdev_read_4,	ti_prcm_read_4),
266 	DEVMETHOD(clkdev_modify_4,	ti_prcm_modify_4),
267 	DEVMETHOD(clkdev_device_lock,	ti_prcm_device_lock),
268 	DEVMETHOD(clkdev_device_unlock, ti_prcm_device_unlock),
269 
270 	DEVMETHOD_END
271 };
272 
273 DEFINE_CLASS_1(ti_prcm, ti_prcm_driver, ti_prcm_methods,
274     sizeof(struct ti_prcm_softc), simplebus_driver);
275 
276 EARLY_DRIVER_MODULE(ti_prcm, ofwbus, ti_prcm_driver, 0, 0, BUS_PASS_BUS);
277 EARLY_DRIVER_MODULE(ti_prcm, simplebus, ti_prcm_driver, 0, 0,
278     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
279 MODULE_VERSION(ti_prcm, 1);
280 MODULE_DEPEND(ti_prcm, ti_scm, 1, 1, 1);
281 
282 /* From sys/arm/ti/am335x/am335x_prcm.c
283  * Copyright (c) 2012 Damjan Marion <dmarion@Freebsd.org>
284  */
285 #define PRM_DEVICE_OFFSET		0xF00
286 #define AM335x_PRM_RSTCTRL		(PRM_DEVICE_OFFSET + 0x00)
287 
288 static void
289 am335x_prcm_reset(void)
290 {
291 	ti_prcm_write_4(ti_prcm_sc->dev, AM335x_PRM_RSTCTRL, (1<<1));
292 }
293 
294 /* FIXME: Is this correct - or should the license part be ontop? */
295 
296 /* From sys/arm/ti/omap4/omap4_prcm_clks.c */
297 /*-
298  * SPDX-License-Identifier: BSD-3-Clause
299  *
300  * Copyright (c) 2011
301  *      Ben Gray <ben.r.gray@gmail.com>.
302  * All rights reserved.
303  *
304  * Redistribution and use in source and binary forms, with or without
305  * modification, are permitted provided that the following conditions
306  * are met:
307  * 1. Redistributions of source code must retain the above copyright
308  *    notice, this list of conditions and the following disclaimer.
309  * 2. Redistributions in binary form must reproduce the above copyright
310  *    notice, this list of conditions and the following disclaimer in the
311  *    documentation and/or other materials provided with the distribution.
312  * 3. The name of the company nor the name of the author may be used to
313  *    endorse or promote products derived from this software without specific
314  *    prior written permission.
315  *
316  * THIS SOFTWARE IS PROVIDED BY BEN GRAY ``AS IS'' AND ANY EXPRESS OR
317  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
318  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
319  * IN NO EVENT SHALL BEN GRAY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
320  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
321  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
322  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
323  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
324  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
325  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
326  */
327 #define PRM_RSTCTRL		0x1b00
328 #define PRM_RSTCTRL_RESET	0x2
329 
330 static void
331 omap4_prcm_reset(void)
332 {
333 	uint32_t reg;
334 
335 	ti_prcm_read_4(ti_prcm_sc->dev, PRM_RSTCTRL, &reg);
336 	reg = reg | PRM_RSTCTRL_RESET;
337 	ti_prcm_write_4(ti_prcm_sc->dev, PRM_RSTCTRL, reg);
338 	ti_prcm_read_4(ti_prcm_sc->dev, PRM_RSTCTRL, &reg);
339 }
340