2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2012 Damjan Marion <dmarion@Freebsd.org>
7 * Copyright (c) 2020 Oskar Holmlund <oskar.holmlund@ohdata.se>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
33 /* Based on sys/arm/ti/am335x/am335x_prcm.c */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/malloc.h>
45 #include <sys/timeet.h>
46 #include <sys/timetc.h>
47 #include <sys/watchdog.h>
48 #include <machine/bus.h>
49 #include <machine/cpu.h>
50 #include <machine/intr.h>
52 #include <arm/ti/ti_cpuid.h>
53 #include <arm/ti/ti_prcm.h>
54 #include <arm/ti/tivar.h>
56 #include <dev/fdt/simplebus.h>
58 #include <dev/ofw/openfirm.h>
59 #include <dev/ofw/ofw_bus.h>
60 #include <dev/ofw/ofw_bus_subr.h>
62 #include "clkdev_if.h"
65 #define DPRINTF(dev, msg...) device_printf(dev, msg)
67 #define DPRINTF(dev, msg...)
70 struct ti_prcm_softc {
71 struct simplebus_softc sc_simplebus;
73 struct resource * mem_res;
75 bus_space_handle_t bsh;
80 static struct ti_prcm_softc *ti_prcm_sc = NULL;
81 static void omap4_prcm_reset(void);
82 static void am335x_prcm_reset(void);
84 #define TI_AM3_PRCM 18
85 #define TI_AM4_PRCM 17
86 #define TI_OMAP2_PRCM 16
87 #define TI_OMAP3_PRM 15
88 #define TI_OMAP3_CM 14
89 #define TI_OMAP4_CM1 13
90 #define TI_OMAP4_PRM 12
91 #define TI_OMAP4_CM2 11
92 #define TI_OMAP4_SCRM 10
93 #define TI_OMAP5_PRM 9
94 #define TI_OMAP5_CM_CORE_AON 8
95 #define TI_OMAP5_SCRM 7
96 #define TI_OMAP5_CM_CORE 6
98 #define TI_DRA7_CM_CORE_AON 4
99 #define TI_DRA7_CM_CORE 3
100 #define TI_DM814_PRCM 2
101 #define TI_DM816_PRCM 1
102 #define TI_PRCM_END 0
104 static struct ofw_compat_data compat_data[] = {
105 { "ti,am3-prcm", TI_AM3_PRCM },
106 { "ti,am4-prcm", TI_AM4_PRCM },
107 { "ti,omap2-prcm", TI_OMAP2_PRCM },
108 { "ti,omap3-prm", TI_OMAP3_PRM },
109 { "ti,omap3-cm", TI_OMAP3_CM },
110 { "ti,omap4-cm1", TI_OMAP4_CM1 },
111 { "ti,omap4-prm", TI_OMAP4_PRM },
112 { "ti,omap4-cm2", TI_OMAP4_CM2 },
113 { "ti,omap4-scrm", TI_OMAP4_SCRM },
114 { "ti,omap5-prm", TI_OMAP5_PRM },
115 { "ti,omap5-cm-core-aon", TI_OMAP5_CM_CORE_AON },
116 { "ti,omap5-scrm", TI_OMAP5_SCRM },
117 { "ti,omap5-cm-core", TI_OMAP5_CM_CORE },
118 { "ti,dra7-prm", TI_DRA7_PRM },
119 { "ti,dra7-cm-core-aon", TI_DRA7_CM_CORE_AON },
120 { "ti,dra7-cm-core", TI_DRA7_CM_CORE },
121 { "ti,dm814-prcm", TI_DM814_PRCM },
122 { "ti,dm816-prcm", TI_DM816_PRCM },
127 ti_prcm_probe(device_t dev)
129 if (!ofw_bus_status_okay(dev))
132 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) {
136 device_set_desc(dev, "TI Power and Clock Management");
137 return(BUS_PROBE_DEFAULT);
141 ti_prcm_attach(device_t dev)
143 struct ti_prcm_softc *sc;
144 phandle_t node, child;
147 sc = device_get_softc(dev);
150 node = ofw_bus_get_node(sc->dev);
151 simplebus_init(sc->dev, node);
153 if (simplebus_fill_ranges(node, &sc->sc_simplebus) < 0) {
154 device_printf(sc->dev, "could not get ranges\n");
157 if (sc->sc_simplebus.nranges == 0) {
158 device_printf(sc->dev, "nranges == 0\n");
162 sc->mem_res = bus_alloc_resource(sc->dev, SYS_RES_MEMORY, &rid,
163 sc->sc_simplebus.ranges[0].host,
164 (sc->sc_simplebus.ranges[0].host +
165 sc->sc_simplebus.ranges[0].size - 1),
166 sc->sc_simplebus.ranges[0].size,
167 RF_ACTIVE | RF_SHAREABLE);
169 if (sc->mem_res == NULL) {
173 sc->bst = rman_get_bustag(sc->mem_res);
174 sc->bsh = rman_get_bushandle(sc->mem_res);
176 mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
178 /* Fixme: for xxx_prcm_reset functions.
179 * Get rid of global variables?
186 ti_cpu_reset = omap4_prcm_reset;
191 ti_cpu_reset = am335x_prcm_reset;
196 bus_generic_probe(sc->dev);
197 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
198 simplebus_add_device(dev, child, 0, NULL, -1, NULL);
201 return (bus_generic_attach(sc->dev));
205 ti_prcm_write_4(device_t dev, bus_addr_t addr, uint32_t val)
207 struct ti_prcm_softc *sc;
209 sc = device_get_softc(dev);
210 DPRINTF(sc->dev, "offset=%lx write %x\n", addr, val);
211 bus_space_write_4(sc->bst, sc->bsh, addr, val);
215 ti_prcm_read_4(device_t dev, bus_addr_t addr, uint32_t *val)
217 struct ti_prcm_softc *sc;
219 sc = device_get_softc(dev);
221 *val = bus_space_read_4(sc->bst, sc->bsh, addr);
222 DPRINTF(sc->dev, "offset=%lx Read %x\n", addr, *val);
227 ti_prcm_modify_4(device_t dev, bus_addr_t addr, uint32_t clr, uint32_t set)
229 struct ti_prcm_softc *sc;
232 sc = device_get_softc(dev);
234 reg = bus_space_read_4(sc->bst, sc->bsh, addr);
237 bus_space_write_4(sc->bst, sc->bsh, addr, reg);
238 DPRINTF(sc->dev, "offset=%lx reg: %x (clr %x set %x)\n", addr, reg, clr, set);
244 ti_prcm_device_lock(device_t dev)
246 struct ti_prcm_softc *sc;
248 sc = device_get_softc(dev);
253 ti_prcm_device_unlock(device_t dev)
255 struct ti_prcm_softc *sc;
257 sc = device_get_softc(dev);
258 mtx_unlock(&sc->mtx);
261 static device_method_t ti_prcm_methods[] = {
262 DEVMETHOD(device_probe, ti_prcm_probe),
263 DEVMETHOD(device_attach, ti_prcm_attach),
265 /* clkdev interface */
266 DEVMETHOD(clkdev_write_4, ti_prcm_write_4),
267 DEVMETHOD(clkdev_read_4, ti_prcm_read_4),
268 DEVMETHOD(clkdev_modify_4, ti_prcm_modify_4),
269 DEVMETHOD(clkdev_device_lock, ti_prcm_device_lock),
270 DEVMETHOD(clkdev_device_unlock, ti_prcm_device_unlock),
275 DEFINE_CLASS_1(ti_prcm, ti_prcm_driver, ti_prcm_methods,
276 sizeof(struct ti_prcm_softc), simplebus_driver);
278 EARLY_DRIVER_MODULE(ti_prcm, ofwbus, ti_prcm_driver, 0, 0, BUS_PASS_BUS);
279 EARLY_DRIVER_MODULE(ti_prcm, simplebus, ti_prcm_driver, 0, 0,
280 BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
281 MODULE_VERSION(ti_prcm, 1);
282 MODULE_DEPEND(ti_prcm, ti_scm, 1, 1, 1);
284 /* From sys/arm/ti/am335x/am335x_prcm.c
285 * Copyright (c) 2012 Damjan Marion <dmarion@Freebsd.org>
287 #define PRM_DEVICE_OFFSET 0xF00
288 #define AM335x_PRM_RSTCTRL (PRM_DEVICE_OFFSET + 0x00)
291 am335x_prcm_reset(void)
293 ti_prcm_write_4(ti_prcm_sc->dev, AM335x_PRM_RSTCTRL, (1<<1));
296 /* FIXME: Is this correct - or should the license part be ontop? */
298 /* From sys/arm/ti/omap4/omap4_prcm_clks.c */
300 * SPDX-License-Identifier: BSD-3-Clause
303 * Ben Gray <ben.r.gray@gmail.com>.
304 * All rights reserved.
306 * Redistribution and use in source and binary forms, with or without
307 * modification, are permitted provided that the following conditions
309 * 1. Redistributions of source code must retain the above copyright
310 * notice, this list of conditions and the following disclaimer.
311 * 2. Redistributions in binary form must reproduce the above copyright
312 * notice, this list of conditions and the following disclaimer in the
313 * documentation and/or other materials provided with the distribution.
314 * 3. The name of the company nor the name of the author may be used to
315 * endorse or promote products derived from this software without specific
316 * prior written permission.
318 * THIS SOFTWARE IS PROVIDED BY BEN GRAY ``AS IS'' AND ANY EXPRESS OR
319 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
320 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
321 * IN NO EVENT SHALL BEN GRAY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
322 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
323 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
324 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
325 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
326 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
327 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
329 #define PRM_RSTCTRL 0x1b00
330 #define PRM_RSTCTRL_RESET 0x2
333 omap4_prcm_reset(void)
337 ti_prcm_read_4(ti_prcm_sc->dev, PRM_RSTCTRL, ®);
338 reg = reg | PRM_RSTCTRL_RESET;
339 ti_prcm_write_4(ti_prcm_sc->dev, PRM_RSTCTRL, reg);
340 ti_prcm_read_4(ti_prcm_sc->dev, PRM_RSTCTRL, ®);