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