xref: /freebsd/sys/powerpc/psim/iobus.c (revision 25ecdc7d52770caf1c9b44b5ec11f468f6b636f3)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright 2002 by Peter Grehan. All rights reserved.
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  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 /*
33  *  PSIM 'iobus' local bus. Should be set up in the device tree like:
34  *
35  *     /iobus@0x80000000/name psim-iobus
36  *
37  *  Code borrowed from various nexus.c and uninorth.c :-)
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/bus.h>
46 #include <machine/bus.h>
47 #include <sys/rman.h>
48 
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/openfirm.h>
51 
52 #include <machine/vmparam.h>
53 #include <vm/vm.h>
54 #include <vm/pmap.h>
55 
56 #include <machine/resource.h>
57 
58 #include <powerpc/psim/iobusvar.h>
59 
60 struct iobus_softc {
61 	phandle_t     sc_node;
62 	vm_offset_t   sc_addr;
63 	vm_offset_t   sc_size;
64 	struct        rman sc_mem_rman;
65 };
66 
67 static MALLOC_DEFINE(M_IOBUS, "iobus", "iobus device information");
68 
69 static int  iobus_probe(device_t);
70 static int  iobus_attach(device_t);
71 static int  iobus_print_child(device_t dev, device_t child);
72 static void iobus_probe_nomatch(device_t, device_t);
73 static int  iobus_read_ivar(device_t, device_t, int, uintptr_t *);
74 static int  iobus_write_ivar(device_t, device_t, int, uintptr_t);
75 static struct   resource *iobus_alloc_resource(device_t, device_t, int, int *,
76 					       rman_res_t, rman_res_t, rman_res_t,
77 					       u_int);
78 static int  iobus_activate_resource(device_t, device_t, int, int,
79 				    struct resource *);
80 static int  iobus_deactivate_resource(device_t, device_t, int, int,
81 				      struct resource *);
82 static int  iobus_release_resource(device_t, device_t, int, int,
83 				   struct resource *);
84 
85 /*
86  * Bus interface definition
87  */
88 static device_method_t iobus_methods[] = {
89         /* Device interface */
90         DEVMETHOD(device_probe,         iobus_probe),
91         DEVMETHOD(device_attach,        iobus_attach),
92         DEVMETHOD(device_detach,        bus_generic_detach),
93         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
94         DEVMETHOD(device_suspend,       bus_generic_suspend),
95         DEVMETHOD(device_resume,        bus_generic_resume),
96 
97         /* Bus interface */
98         DEVMETHOD(bus_print_child,      iobus_print_child),
99         DEVMETHOD(bus_probe_nomatch,    iobus_probe_nomatch),
100         DEVMETHOD(bus_read_ivar,        iobus_read_ivar),
101         DEVMETHOD(bus_write_ivar,       iobus_write_ivar),
102         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
103         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
104 
105         DEVMETHOD(bus_alloc_resource,   iobus_alloc_resource),
106         DEVMETHOD(bus_release_resource, iobus_release_resource),
107         DEVMETHOD(bus_activate_resource, iobus_activate_resource),
108         DEVMETHOD(bus_deactivate_resource, iobus_deactivate_resource),
109         { 0, 0 }
110 };
111 
112 static driver_t iobus_driver = {
113         "iobus",
114         iobus_methods,
115         sizeof(struct iobus_softc)
116 };
117 
118 devclass_t iobus_devclass;
119 
120 DRIVER_MODULE(iobus, ofwbus, iobus_driver, iobus_devclass, 0, 0);
121 
122 static int
123 iobus_probe(device_t dev)
124 {
125 	const char *type = ofw_bus_get_name(dev);
126 
127 	if (strcmp(type, "psim-iobus") != 0)
128 		return (ENXIO);
129 
130 	device_set_desc(dev, "PSIM local bus");
131 	return (0);
132 }
133 
134 /*
135  * Add interrupt/addr range to the dev's resource list if present
136  */
137 static void
138 iobus_add_intr(phandle_t devnode, struct iobus_devinfo *dinfo)
139 {
140 	u_int intr = -1;
141 
142 	if (OF_getprop(devnode, "interrupt", &intr, sizeof(intr)) != -1) {
143 		resource_list_add(&dinfo->id_resources,
144 				  SYS_RES_IRQ, 0, intr, intr, 1);
145 	}
146 	dinfo->id_interrupt = intr;
147 }
148 
149 static void
150 iobus_add_reg(phandle_t devnode, struct iobus_devinfo *dinfo,
151 	      vm_offset_t iobus_off)
152 {
153 	u_int size;
154 	int i;
155 
156 	size = OF_getprop(devnode, "reg", dinfo->id_reg,sizeof(dinfo->id_reg));
157 
158 	if (size != -1) {
159 		dinfo->id_nregs = size / (sizeof(dinfo->id_reg[0]));
160 
161 		for (i = 0; i < dinfo->id_nregs; i+= 3) {
162 			/*
163 			 * Scale the absolute addresses back to iobus
164 			 * relative offsets. This is to better simulate
165 			 * macio
166 			 */
167 			dinfo->id_reg[i+1] -= iobus_off;
168 
169 			resource_list_add(&dinfo->id_resources,
170 					  SYS_RES_MEMORY, 0,
171 					  dinfo->id_reg[i+1],
172 					  dinfo->id_reg[i+1] +
173 					      dinfo->id_reg[i+2],
174 					  dinfo->id_reg[i+2]);
175 		}
176 	}
177 }
178 
179 static int
180 iobus_attach(device_t dev)
181 {
182 	struct iobus_softc *sc;
183         struct iobus_devinfo *dinfo;
184         phandle_t  root;
185         phandle_t  child;
186         device_t   cdev;
187         char *name;
188 	u_int reg[2];
189 	int size;
190 
191 	sc = device_get_softc(dev);
192 	sc->sc_node = ofw_bus_get_node(dev);
193 
194 	/*
195 	 * Find the base addr/size of the iobus, and initialize the
196 	 * resource manager
197 	 */
198 	size = OF_getprop(sc->sc_node, "reg", reg, sizeof(reg));
199 	if (size == sizeof(reg)) {
200 		sc->sc_addr = reg[0];
201 		sc->sc_size = reg[1];
202 	} else {
203 		return (ENXIO);
204 	}
205 
206 	sc->sc_mem_rman.rm_type = RMAN_ARRAY;
207         sc->sc_mem_rman.rm_descr = "IOBus Device Memory";
208         if (rman_init(&sc->sc_mem_rman) != 0) {
209 		device_printf(dev,
210                     "failed to init mem range resources\n");
211                 return (ENXIO);
212 	}
213 	rman_manage_region(&sc->sc_mem_rman, 0, sc->sc_size);
214 
215         /*
216          * Iterate through the sub-devices
217          */
218         root = sc->sc_node;
219 
220         for (child = OF_child(root); child != 0; child = OF_peer(child)) {
221                 OF_getprop_alloc(child, "name", (void **)&name);
222 
223                 cdev = device_add_child(dev, NULL, -1);
224                 if (cdev != NULL) {
225                         dinfo = malloc(sizeof(*dinfo), M_IOBUS, M_WAITOK);
226 			memset(dinfo, 0, sizeof(*dinfo));
227 			resource_list_init(&dinfo->id_resources);
228                         dinfo->id_node = child;
229                         dinfo->id_name = name;
230 			iobus_add_intr(child, dinfo);
231 			iobus_add_reg(child, dinfo, sc->sc_addr);
232                         device_set_ivars(cdev, dinfo);
233                 } else {
234                         OF_prop_free(name);
235                 }
236         }
237 
238         return (bus_generic_attach(dev));
239 }
240 
241 static int
242 iobus_print_child(device_t dev, device_t child)
243 {
244         struct iobus_devinfo *dinfo;
245         struct resource_list *rl;
246         int retval = 0;
247 
248 	dinfo = device_get_ivars(child);
249         rl = &dinfo->id_resources;
250 
251 	retval += bus_print_child_header(dev, child);
252 
253         retval += printf(" offset 0x%x", dinfo->id_reg[1]);
254         retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
255 
256         retval += bus_print_child_footer(dev, child);
257 
258         return (retval);
259 }
260 
261 static void
262 iobus_probe_nomatch(device_t dev, device_t child)
263 {
264 }
265 
266 static int
267 iobus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
268 {
269         struct iobus_devinfo *dinfo;
270 
271         if ((dinfo = device_get_ivars(child)) == NULL)
272                 return (ENOENT);
273 
274         switch (which) {
275         case IOBUS_IVAR_NODE:
276                 *result = dinfo->id_node;
277                 break;
278         case IOBUS_IVAR_NAME:
279                 *result = (uintptr_t)dinfo->id_name;
280                 break;
281 	case IOBUS_IVAR_NREGS:
282 		*result = dinfo->id_nregs;
283 		break;
284 	case IOBUS_IVAR_REGS:
285 		*result = (uintptr_t)dinfo->id_reg;
286 		break;
287         default:
288                 return (ENOENT);
289         }
290 
291         return (0);
292 }
293 
294 static int
295 iobus_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
296 {
297         return (EINVAL);
298 }
299 
300 static struct resource *
301 iobus_alloc_resource(device_t bus, device_t child, int type, int *rid,
302 		     rman_res_t start, rman_res_t end, rman_res_t count,
303 		     u_int flags)
304 {
305 	struct iobus_softc *sc;
306 	int  needactivate;
307 	struct  resource *rv;
308 	struct  rman *rm;
309 
310 	sc = device_get_softc(bus);
311 
312 	needactivate = flags & RF_ACTIVE;
313 	flags &= ~RF_ACTIVE;
314 
315 	switch (type) {
316 	case SYS_RES_MEMORY:
317 	case SYS_RES_IOPORT:
318 		rm = &sc->sc_mem_rman;
319 		break;
320 	case SYS_RES_IRQ:
321 		return (bus_alloc_resource(bus, type, rid, start, end, count,
322 		    flags));
323 	default:
324 		device_printf(bus, "unknown resource request from %s\n",
325 		    device_get_nameunit(child));
326 		return (NULL);
327 	}
328 
329 	rv = rman_reserve_resource(rm, start, end, count, flags, child);
330 	if (rv == NULL) {
331 		device_printf(bus, "failed to reserve resource for %s\n",
332 			      device_get_nameunit(child));
333 		return (NULL);
334 	}
335 
336 	rman_set_rid(rv, *rid);
337 
338 	if (needactivate) {
339 		if (bus_activate_resource(child, type, *rid, rv) != 0) {
340                         device_printf(bus,
341 				      "failed to activate resource for %s\n",
342 				      device_get_nameunit(child));
343 			rman_release_resource(rv);
344 			return (NULL);
345                 }
346         }
347 
348 	return (rv);
349 }
350 
351 static int
352 iobus_release_resource(device_t bus, device_t child, int type, int rid,
353 		       struct resource *res)
354 {
355 	if (rman_get_flags(res) & RF_ACTIVE) {
356 		int error = bus_deactivate_resource(child, type, rid, res);
357 		if (error)
358 			return error;
359 	}
360 
361 	return (rman_release_resource(res));
362 }
363 
364 static int
365 iobus_activate_resource(device_t bus, device_t child, int type, int rid,
366 			   struct resource *res)
367 {
368 	struct iobus_softc *sc;
369 	void    *p;
370 
371 	sc = device_get_softc(bus);
372 
373 	if (type == SYS_RES_IRQ)
374                 return (bus_activate_resource(bus, type, rid, res));
375 
376 	if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
377 		p = pmap_mapdev((vm_offset_t)rman_get_start(res) + sc->sc_addr,
378 				(vm_size_t)rman_get_size(res));
379 		if (p == NULL)
380 			return (ENOMEM);
381 		rman_set_virtual(res, p);
382 		rman_set_bustag(res, &bs_le_tag);
383 		rman_set_bushandle(res, (u_long)p);
384 	}
385 
386 	return (rman_activate_resource(res));
387 }
388 
389 static int
390 iobus_deactivate_resource(device_t bus, device_t child, int type, int rid,
391 			  struct resource *res)
392 {
393         /*
394          * If this is a memory resource, unmap it.
395          */
396         if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
397 		u_int32_t psize;
398 
399 		psize = rman_get_size(res);
400 		pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
401 	}
402 
403 	return (rman_deactivate_resource(res));
404 }
405