xref: /freebsd/sys/arm/ti/ti_scm_syscon.c (revision 6580f5c38dd5b01aeeaed16b370f1a12423437f0)
1 /*-
2  * Copyright (c) 2019 Emmanuel Vadot <manu@FreeBSD.org>
3  *
4  * Copyright (c) 2020 Oskar Holmlund <oskar.holmlund@ohdata.se>
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 ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23  * 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 /* Based on sys/arm/ti/ti_sysc.c */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/bus.h>
34 #include <sys/resource.h>
35 #include <sys/rman.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 
42 #include <dev/fdt/simplebus.h>
43 
44 #include <dev/ofw/openfirm.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47 
48 #include "syscon_if.h"
49 #include <dev/syscon/syscon.h>
50 #include "clkdev_if.h"
51 
52 #include <arm/ti/ti_cpuid.h>
53 
54 #if 0
55 #define DPRINTF(dev, msg...) device_printf(dev, msg)
56 #else
57 #define DPRINTF(dev, msg...)
58 #endif
59 
60 MALLOC_DECLARE(M_SYSCON);
61 
62 struct ti_scm_syscon_softc {
63 	struct simplebus_softc	sc_simplebus;
64 	device_t		dev;
65 	struct syscon *		syscon;
66 	struct resource *	res[1];
67 	bus_space_tag_t		bst;
68 	bus_space_handle_t	bsh;
69 	struct mtx		mtx;
70 };
71 
72 static struct resource_spec ti_scm_syscon_res_spec[] = {
73 	{ SYS_RES_MEMORY, 0, RF_ACTIVE | RF_SHAREABLE },
74 	{ -1, 0 }
75 };
76 
77 /* Device */
78 static struct ofw_compat_data compat_data[] = {
79 	{ "syscon",	1 },
80 	{ NULL,		0 }
81 };
82 
83 /* --- dev/extres/syscon syscon_method_t interface --- */
84 static int
85 ti_scm_syscon_write_4(struct syscon *syscon, bus_size_t offset, uint32_t val)
86 {
87 	struct ti_scm_syscon_softc *sc;
88 
89 	sc = device_get_softc(syscon->pdev);
90 	DPRINTF(sc->dev, "offset=%lx write %x\n", offset, val);
91 	mtx_lock(&sc->mtx);
92 	bus_space_write_4(sc->bst, sc->bsh, offset, val);
93 	mtx_unlock(&sc->mtx);
94 	return (0);
95 }
96 
97 static uint32_t
98 ti_scm_syscon_read_4(struct syscon *syscon, bus_size_t offset)
99 {
100 	struct ti_scm_syscon_softc *sc;
101 	uint32_t val;
102 
103 	sc = device_get_softc(syscon->pdev);
104 
105 	mtx_lock(&sc->mtx);
106 	val = bus_space_read_4(sc->bst, sc->bsh, offset);
107 	mtx_unlock(&sc->mtx);
108 	DPRINTF(sc->dev, "offset=%lx Read %x\n", offset, val);
109 	return (val);
110 }
111 static int
112 ti_scm_syscon_modify_4(struct syscon *syscon, bus_size_t offset, uint32_t clr, uint32_t set)
113 {
114 	struct ti_scm_syscon_softc *sc;
115 	uint32_t reg;
116 
117 	sc = device_get_softc(syscon->pdev);
118 
119 	mtx_lock(&sc->mtx);
120 	reg = bus_space_read_4(sc->bst, sc->bsh, offset);
121 	reg &= ~clr;
122 	reg |= set;
123 	bus_space_write_4(sc->bst, sc->bsh, offset, reg);
124 	mtx_unlock(&sc->mtx);
125 	DPRINTF(sc->dev, "offset=%lx reg: %x (clr %x set %x)\n", offset, reg, clr, set);
126 
127 	return (0);
128 }
129 
130 static syscon_method_t ti_scm_syscon_reg_methods[] = {
131 	SYSCONMETHOD(syscon_read_4,	ti_scm_syscon_read_4),
132 	SYSCONMETHOD(syscon_write_4,	ti_scm_syscon_write_4),
133 	SYSCONMETHOD(syscon_modify_4,	ti_scm_syscon_modify_4),
134 
135 	SYSCONMETHOD_END
136 };
137 
138 DEFINE_CLASS_1(ti_scm_syscon_reg, ti_scm_syscon_reg_class, ti_scm_syscon_reg_methods,
139     0, syscon_class);
140 
141 /* device interface */
142 static int
143 ti_scm_syscon_probe(device_t dev)
144 {
145 	if (!ofw_bus_status_okay(dev))
146 		return (ENXIO);
147 
148 	if (!ti_soc_is_supported())
149 		return (ENXIO);
150 
151 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
152 		return (ENXIO);
153 
154 	device_set_desc(dev, "TI OMAP Control Module Syscon");
155 	return(BUS_PROBE_DEFAULT);
156 }
157 
158 static int
159 ti_scm_syscon_attach(device_t dev)
160 {
161 	struct ti_scm_syscon_softc *sc;
162 	phandle_t node, child;
163 
164  	sc = device_get_softc(dev);
165 	sc->dev = dev;
166 
167 	if (bus_alloc_resources(dev, ti_scm_syscon_res_spec, sc->res)) {
168 		device_printf(sc->dev, "Cant allocate resources\n");
169 		return (ENXIO);
170 	}
171 
172 	sc->dev = dev;
173 	sc->bst = rman_get_bustag(sc->res[0]);
174 	sc->bsh = rman_get_bushandle(sc->res[0]);
175 
176 	mtx_init(&sc->mtx, device_get_nameunit(sc->dev), NULL, MTX_DEF);
177 	node = ofw_bus_get_node(sc->dev);
178 
179 	/* dev/extres/syscon interface */
180 	sc->syscon = syscon_create_ofw_node(dev, &ti_scm_syscon_reg_class, node);
181 	if (sc->syscon == NULL) {
182 		device_printf(dev, "Failed to create/register syscon\n");
183 		return (ENXIO);
184 	}
185 
186 	simplebus_init(sc->dev, node);
187 
188 	bus_generic_probe(sc->dev);
189 	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
190 		simplebus_add_device(sc->dev, child, 0, NULL, -1, NULL);
191 	}
192 
193 	return (bus_generic_attach(sc->dev));
194 }
195 
196 /* syscon interface */
197 static int
198 ti_scm_syscon_get_handle(device_t dev, struct syscon **syscon)
199 {
200 	struct ti_scm_syscon_softc *sc;
201 
202 	sc = device_get_softc(dev);
203 	*syscon = sc->syscon;
204 	if (*syscon == NULL)
205 		return (ENODEV);
206 	return (0);
207 }
208 
209 /* clkdev interface */
210 static int
211 ti_scm_syscon_clk_write_4(device_t dev, bus_addr_t addr, uint32_t val)
212 {
213 	struct ti_scm_syscon_softc *sc;
214 
215 	sc = device_get_softc(dev);
216 	DPRINTF(sc->dev, "offset=%lx write %x\n", addr, val);
217 	bus_space_write_4(sc->bst, sc->bsh, addr, val);
218 	return (0);
219 }
220 
221 static int
222 ti_scm_syscon_clk_read_4(device_t dev, bus_addr_t addr, uint32_t *val)
223 {
224 	struct ti_scm_syscon_softc *sc;
225 
226 	sc = device_get_softc(dev);
227 
228 	*val = bus_space_read_4(sc->bst, sc->bsh, addr);
229 	DPRINTF(sc->dev, "offset=%lx Read %x\n", addr, *val);
230 	return (0);
231 }
232 
233 static int
234 ti_scm_syscon_clk_modify_4(device_t dev, bus_addr_t addr, uint32_t clr, uint32_t set)
235 {
236 	struct ti_scm_syscon_softc *sc;
237 	uint32_t reg;
238 
239 	sc = device_get_softc(dev);
240 
241 	reg = bus_space_read_4(sc->bst, sc->bsh, addr);
242 	reg &= ~clr;
243 	reg |= set;
244 	bus_space_write_4(sc->bst, sc->bsh, addr, reg);
245 	DPRINTF(sc->dev, "offset=%lx reg: %x (clr %x set %x)\n", addr, reg, clr, set);
246 
247 	return (0);
248 }
249 
250 static void
251 ti_scm_syscon_clk_device_lock(device_t dev)
252 {
253 	struct ti_scm_syscon_softc *sc;
254 
255 	sc = device_get_softc(dev);
256 	mtx_lock(&sc->mtx);
257 }
258 
259 static void
260 ti_scm_syscon_clk_device_unlock(device_t dev)
261 {
262 	struct ti_scm_syscon_softc *sc;
263 	sc = device_get_softc(dev);
264 	mtx_unlock(&sc->mtx);
265 }
266 
267 static device_method_t ti_scm_syscon_methods[] = {
268 	DEVMETHOD(device_probe,		ti_scm_syscon_probe),
269 	DEVMETHOD(device_attach,	ti_scm_syscon_attach),
270 
271 	/* syscon interface */
272 	DEVMETHOD(syscon_get_handle,	ti_scm_syscon_get_handle),
273 
274 	/* clkdev interface */
275 	DEVMETHOD(clkdev_write_4,	ti_scm_syscon_clk_write_4),
276 	DEVMETHOD(clkdev_read_4,	ti_scm_syscon_clk_read_4),
277 	DEVMETHOD(clkdev_modify_4,	ti_scm_syscon_clk_modify_4),
278 	DEVMETHOD(clkdev_device_lock,	ti_scm_syscon_clk_device_lock),
279 	DEVMETHOD(clkdev_device_unlock,	ti_scm_syscon_clk_device_unlock),
280 
281 	DEVMETHOD_END
282 };
283 
284 DEFINE_CLASS_1(ti_scm_syscon, ti_scm_syscon_driver, ti_scm_syscon_methods,
285     sizeof(struct ti_scm_syscon_softc), simplebus_driver);
286 
287 EARLY_DRIVER_MODULE(ti_scm_syscon, simplebus, ti_scm_syscon_driver, 0, 0,
288     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
289 MODULE_VERSION(ti_scm_syscon, 1);
290 MODULE_DEPEND(ti_scm_syscon, ti_scm, 1, 1, 1);
291