xref: /freebsd/sys/arm/ti/ti_prcm.c (revision 10bb91672a02b646e6dad6cdfe10ff75a192db99)
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 am335x_prcm_reset(void);
77 
78 #define TI_AM3_PRCM		18
79 #define TI_AM4_PRCM		17
80 #define TI_OMAP2_PRCM		16
81 #define TI_OMAP3_PRM		15
82 #define TI_OMAP3_CM		14
83 #define TI_OMAP4_CM1		13
84 #define TI_OMAP4_PRM		12
85 #define TI_OMAP4_CM2		11
86 #define TI_OMAP4_SCRM		10
87 #define TI_OMAP5_PRM		9
88 #define TI_OMAP5_CM_CORE_AON	8
89 #define TI_OMAP5_SCRM		7
90 #define TI_OMAP5_CM_CORE	6
91 #define TI_DRA7_PRM		5
92 #define TI_DRA7_CM_CORE_AON	4
93 #define TI_DRA7_CM_CORE		3
94 #define TI_DM814_PRCM		2
95 #define TI_DM816_PRCM		1
96 #define TI_PRCM_END		0
97 
98 static struct ofw_compat_data compat_data[] = {
99 	{ "ti,am3-prcm",		TI_AM3_PRCM },
100 	{ "ti,am4-prcm",		TI_AM4_PRCM },
101 	{ "ti,omap2-prcm",		TI_OMAP2_PRCM },
102 	{ "ti,omap3-prm",		TI_OMAP3_PRM },
103 	{ "ti,omap3-cm",		TI_OMAP3_CM },
104 	{ "ti,omap4-cm1",		TI_OMAP4_CM1 },
105 	{ "ti,omap4-prm",		TI_OMAP4_PRM },
106 	{ "ti,omap4-cm2",		TI_OMAP4_CM2 },
107 	{ "ti,omap4-scrm",		TI_OMAP4_SCRM },
108 	{ "ti,omap5-prm",		TI_OMAP5_PRM },
109 	{ "ti,omap5-cm-core-aon",	TI_OMAP5_CM_CORE_AON },
110 	{ "ti,omap5-scrm",		TI_OMAP5_SCRM },
111 	{ "ti,omap5-cm-core",		TI_OMAP5_CM_CORE },
112 	{ "ti,dra7-prm",		TI_DRA7_PRM },
113 	{ "ti,dra7-cm-core-aon",	TI_DRA7_CM_CORE_AON },
114 	{ "ti,dra7-cm-core",		TI_DRA7_CM_CORE },
115 	{ "ti,dm814-prcm",		TI_DM814_PRCM },
116 	{ "ti,dm816-prcm",		TI_DM816_PRCM },
117 	{ NULL,				TI_PRCM_END}
118 };
119 
120 static int
ti_prcm_probe(device_t dev)121 ti_prcm_probe(device_t dev)
122 {
123 	if (!ofw_bus_status_okay(dev))
124 		return (ENXIO);
125 
126 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) {
127 		return (ENXIO);
128 	}
129 
130 	device_set_desc(dev, "TI Power and Clock Management");
131 	return(BUS_PROBE_DEFAULT);
132 }
133 
134 static int
ti_prcm_attach(device_t dev)135 ti_prcm_attach(device_t dev)
136 {
137 	struct ti_prcm_softc *sc;
138 	phandle_t node, child;
139 	int rid;
140 
141 	sc = device_get_softc(dev);
142 	sc->dev = dev;
143 
144 	node = ofw_bus_get_node(sc->dev);
145 	simplebus_init(sc->dev, node);
146 
147 	if (simplebus_fill_ranges(node, &sc->sc_simplebus) < 0) {
148 		device_printf(sc->dev, "could not get ranges\n");
149 		return (ENXIO);
150 	}
151 	if (sc->sc_simplebus.nranges == 0) {
152 		device_printf(sc->dev, "nranges == 0\n");
153 		return (ENXIO);
154 	}
155 
156 	sc->mem_res = bus_alloc_resource(sc->dev, SYS_RES_MEMORY, &rid,
157 		sc->sc_simplebus.ranges[0].host,
158 		(sc->sc_simplebus.ranges[0].host +
159 			sc->sc_simplebus.ranges[0].size - 1),
160 		sc->sc_simplebus.ranges[0].size,
161 		RF_ACTIVE | RF_SHAREABLE);
162 
163 	if (sc->mem_res == NULL) {
164 		return (ENXIO);
165 	}
166 
167 	sc->bst = rman_get_bustag(sc->mem_res);
168 	sc->bsh = rman_get_bushandle(sc->mem_res);
169 
170 	mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
171 
172 	/* Fixme: for xxx_prcm_reset functions.
173 	 * Get rid of global variables?
174 	 */
175 	ti_prcm_sc = sc;
176 
177 	switch(ti_chip()) {
178 #ifdef SOC_TI_AM335X
179 	case CHIP_AM335X:
180 		ti_cpu_reset = am335x_prcm_reset;
181 		break;
182 #endif
183 	}
184 
185 	bus_identify_children(sc->dev);
186 	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
187 		simplebus_add_device(dev, child, 0, NULL, -1, NULL);
188 	}
189 	bus_attach_children(sc->dev);
190 	return (0);
191 }
192 
193 int
ti_prcm_write_4(device_t dev,bus_addr_t addr,uint32_t val)194 ti_prcm_write_4(device_t dev, bus_addr_t addr, uint32_t val)
195 {
196 	struct ti_prcm_softc *sc;
197 
198 	sc = device_get_softc(dev);
199 	DPRINTF(sc->dev, "offset=%lx write %x\n", addr, val);
200 	bus_space_write_4(sc->bst, sc->bsh, addr, val);
201 	return (0);
202 }
203 int
ti_prcm_read_4(device_t dev,bus_addr_t addr,uint32_t * val)204 ti_prcm_read_4(device_t dev, bus_addr_t addr, uint32_t *val)
205 {
206 	struct ti_prcm_softc *sc;
207 
208 	sc = device_get_softc(dev);
209 
210 	*val = bus_space_read_4(sc->bst, sc->bsh, addr);
211 	DPRINTF(sc->dev, "offset=%lx Read %x\n", addr, *val);
212 	return (0);
213 }
214 
215 int
ti_prcm_modify_4(device_t dev,bus_addr_t addr,uint32_t clr,uint32_t set)216 ti_prcm_modify_4(device_t dev, bus_addr_t addr, uint32_t clr, uint32_t set)
217 {
218 	struct ti_prcm_softc *sc;
219 	uint32_t reg;
220 
221 	sc = device_get_softc(dev);
222 
223 	reg = bus_space_read_4(sc->bst, sc->bsh, addr);
224 	reg &= ~clr;
225 	reg |= set;
226 	bus_space_write_4(sc->bst, sc->bsh, addr, reg);
227 	DPRINTF(sc->dev, "offset=%lx reg: %x (clr %x set %x)\n", addr, reg, clr, set);
228 
229 	return (0);
230 }
231 
232 void
ti_prcm_device_lock(device_t dev)233 ti_prcm_device_lock(device_t dev)
234 {
235 	struct ti_prcm_softc *sc;
236 
237 	sc = device_get_softc(dev);
238 	mtx_lock(&sc->mtx);
239 }
240 
241 void
ti_prcm_device_unlock(device_t dev)242 ti_prcm_device_unlock(device_t dev)
243 {
244 	struct ti_prcm_softc *sc;
245 
246 	sc = device_get_softc(dev);
247 	mtx_unlock(&sc->mtx);
248 }
249 
250 static device_method_t ti_prcm_methods[] = {
251 	DEVMETHOD(device_probe,		ti_prcm_probe),
252 	DEVMETHOD(device_attach,	ti_prcm_attach),
253 
254 	/* clkdev interface */
255 	DEVMETHOD(clkdev_write_4,	ti_prcm_write_4),
256 	DEVMETHOD(clkdev_read_4,	ti_prcm_read_4),
257 	DEVMETHOD(clkdev_modify_4,	ti_prcm_modify_4),
258 	DEVMETHOD(clkdev_device_lock,	ti_prcm_device_lock),
259 	DEVMETHOD(clkdev_device_unlock, ti_prcm_device_unlock),
260 
261 	DEVMETHOD_END
262 };
263 
264 DEFINE_CLASS_1(ti_prcm, ti_prcm_driver, ti_prcm_methods,
265     sizeof(struct ti_prcm_softc), simplebus_driver);
266 
267 EARLY_DRIVER_MODULE(ti_prcm, ofwbus, ti_prcm_driver, 0, 0, BUS_PASS_BUS);
268 EARLY_DRIVER_MODULE(ti_prcm, simplebus, ti_prcm_driver, 0, 0,
269     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
270 MODULE_VERSION(ti_prcm, 1);
271 MODULE_DEPEND(ti_prcm, ti_scm, 1, 1, 1);
272 
273 /* From sys/arm/ti/am335x/am335x_prcm.c
274  * Copyright (c) 2012 Damjan Marion <dmarion@Freebsd.org>
275  */
276 #define PRM_DEVICE_OFFSET		0xF00
277 #define AM335x_PRM_RSTCTRL		(PRM_DEVICE_OFFSET + 0x00)
278 
279 static void
am335x_prcm_reset(void)280 am335x_prcm_reset(void)
281 {
282 	ti_prcm_write_4(ti_prcm_sc->dev, AM335x_PRM_RSTCTRL, (1<<1));
283 }
284