xref: /freebsd/sys/powerpc/powermac/uninorth.c (revision eaef5f0af874690ec58b14be728d2e3d3df66579)
160727d8bSWarner Losh /*-
298f8e6c0SBenno Rice  * Copyright (C) 2002 Benno Rice.
398f8e6c0SBenno Rice  * All rights reserved.
498f8e6c0SBenno Rice  *
598f8e6c0SBenno Rice  * Redistribution and use in source and binary forms, with or without
698f8e6c0SBenno Rice  * modification, are permitted provided that the following conditions
798f8e6c0SBenno Rice  * are met:
898f8e6c0SBenno Rice  * 1. Redistributions of source code must retain the above copyright
998f8e6c0SBenno Rice  *    notice, this list of conditions and the following disclaimer.
1098f8e6c0SBenno Rice  * 2. Redistributions in binary form must reproduce the above copyright
1198f8e6c0SBenno Rice  *    notice, this list of conditions and the following disclaimer in the
1298f8e6c0SBenno Rice  *    documentation and/or other materials provided with the distribution.
1398f8e6c0SBenno Rice  *
1498f8e6c0SBenno Rice  * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
1598f8e6c0SBenno Rice  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1698f8e6c0SBenno Rice  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1798f8e6c0SBenno Rice  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1898f8e6c0SBenno Rice  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1998f8e6c0SBenno Rice  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2098f8e6c0SBenno Rice  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2198f8e6c0SBenno Rice  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2298f8e6c0SBenno Rice  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2398f8e6c0SBenno Rice  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2498f8e6c0SBenno Rice  *
2598f8e6c0SBenno Rice  * $FreeBSD$
2698f8e6c0SBenno Rice  */
2798f8e6c0SBenno Rice 
2898f8e6c0SBenno Rice #include <sys/param.h>
2998f8e6c0SBenno Rice #include <sys/systm.h>
3040cdee9dSPeter Grehan #include <sys/module.h>
3198f8e6c0SBenno Rice #include <sys/bus.h>
3298f8e6c0SBenno Rice #include <sys/conf.h>
3398f8e6c0SBenno Rice #include <sys/kernel.h>
3498f8e6c0SBenno Rice 
3598f8e6c0SBenno Rice #include <dev/ofw/openfirm.h>
3698f8e6c0SBenno Rice #include <dev/ofw/ofw_pci.h>
3751d163d3SNathan Whitehorn #include <dev/ofw/ofw_bus.h>
3894b4a038SNathan Whitehorn #include <dev/ofw/ofw_bus_subr.h>
3998f8e6c0SBenno Rice 
4098f8e6c0SBenno Rice #include <dev/pci/pcivar.h>
4198f8e6c0SBenno Rice #include <dev/pci/pcireg.h>
4298f8e6c0SBenno Rice 
4398f8e6c0SBenno Rice #include <machine/bus.h>
44ca2c1931SNathan Whitehorn #include <machine/intr_machdep.h>
4598f8e6c0SBenno Rice #include <machine/md_var.h>
46de2fa7b8SMarcel Moolenaar #include <machine/pio.h>
4798f8e6c0SBenno Rice #include <machine/resource.h>
4898f8e6c0SBenno Rice 
4998f8e6c0SBenno Rice #include <sys/rman.h>
5098f8e6c0SBenno Rice 
5198f8e6c0SBenno Rice #include <powerpc/powermac/uninorthvar.h>
5298f8e6c0SBenno Rice 
5398f8e6c0SBenno Rice #include <vm/vm.h>
5498f8e6c0SBenno Rice #include <vm/pmap.h>
5598f8e6c0SBenno Rice 
56ca2c1931SNathan Whitehorn /*
57ca2c1931SNathan Whitehorn  * Driver for the Uninorth chip itself.
58ca2c1931SNathan Whitehorn  */
5998f8e6c0SBenno Rice 
60ca2c1931SNathan Whitehorn static MALLOC_DEFINE(M_UNIN, "unin", "unin device information");
6198f8e6c0SBenno Rice 
6298f8e6c0SBenno Rice /*
6398f8e6c0SBenno Rice  * Device interface.
6498f8e6c0SBenno Rice  */
65ca2c1931SNathan Whitehorn 
66ca2c1931SNathan Whitehorn static int  unin_chip_probe(device_t);
67ca2c1931SNathan Whitehorn static int  unin_chip_attach(device_t);
6898f8e6c0SBenno Rice 
6998f8e6c0SBenno Rice /*
7098f8e6c0SBenno Rice  * Bus interface.
7198f8e6c0SBenno Rice  */
72ca2c1931SNathan Whitehorn static int  unin_chip_print_child(device_t dev, device_t child);
73ca2c1931SNathan Whitehorn static void unin_chip_probe_nomatch(device_t, device_t);
74ca2c1931SNathan Whitehorn static struct resource *unin_chip_alloc_resource(device_t, device_t, int, int *,
75ca2c1931SNathan Whitehorn 						 u_long, u_long, u_long, u_int);
76ca2c1931SNathan Whitehorn static int  unin_chip_activate_resource(device_t, device_t, int, int,
77ca2c1931SNathan Whitehorn 					struct resource *);
78ca2c1931SNathan Whitehorn static int  unin_chip_deactivate_resource(device_t, device_t, int, int,
79ca2c1931SNathan Whitehorn 					  struct resource *);
80ca2c1931SNathan Whitehorn static int  unin_chip_release_resource(device_t, device_t, int, int,
81ca2c1931SNathan Whitehorn 				       struct resource *);
82ca2c1931SNathan Whitehorn static struct resource_list *unin_chip_get_resource_list (device_t, device_t);
8398f8e6c0SBenno Rice 
8498f8e6c0SBenno Rice /*
8551d163d3SNathan Whitehorn  * OFW Bus interface
8651d163d3SNathan Whitehorn  */
8751d163d3SNathan Whitehorn 
88ca2c1931SNathan Whitehorn static ofw_bus_get_devinfo_t unin_chip_get_devinfo;
8951d163d3SNathan Whitehorn 
9051d163d3SNathan Whitehorn /*
91ca2c1931SNathan Whitehorn  * Local routines
9298f8e6c0SBenno Rice  */
93ca2c1931SNathan Whitehorn 
94ca2c1931SNathan Whitehorn static void		unin_enable_gmac(device_t dev);
95ca2c1931SNathan Whitehorn static void		unin_enable_mpic(device_t dev);
9698f8e6c0SBenno Rice 
9798f8e6c0SBenno Rice /*
9898f8e6c0SBenno Rice  * Driver methods.
9998f8e6c0SBenno Rice  */
100ca2c1931SNathan Whitehorn static device_method_t unin_chip_methods[] = {
101ca2c1931SNathan Whitehorn 
10298f8e6c0SBenno Rice 	/* Device interface */
103ca2c1931SNathan Whitehorn 	DEVMETHOD(device_probe,         unin_chip_probe),
104ca2c1931SNathan Whitehorn 	DEVMETHOD(device_attach,        unin_chip_attach),
10598f8e6c0SBenno Rice 
10698f8e6c0SBenno Rice 	/* Bus interface */
107ca2c1931SNathan Whitehorn 	DEVMETHOD(bus_print_child,      unin_chip_print_child),
108ca2c1931SNathan Whitehorn 	DEVMETHOD(bus_probe_nomatch,    unin_chip_probe_nomatch),
10998f8e6c0SBenno Rice 	DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
11098f8e6c0SBenno Rice 	DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
11198f8e6c0SBenno Rice 
112ca2c1931SNathan Whitehorn 	DEVMETHOD(bus_alloc_resource,   unin_chip_alloc_resource),
113ca2c1931SNathan Whitehorn 	DEVMETHOD(bus_release_resource, unin_chip_release_resource),
114ca2c1931SNathan Whitehorn 	DEVMETHOD(bus_activate_resource, unin_chip_activate_resource),
115ca2c1931SNathan Whitehorn 	DEVMETHOD(bus_deactivate_resource, unin_chip_deactivate_resource),
116ca2c1931SNathan Whitehorn 	DEVMETHOD(bus_get_resource_list, unin_chip_get_resource_list),
11798f8e6c0SBenno Rice 
1184fe8025dSNathan Whitehorn 	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
1194fe8025dSNathan Whitehorn 
12051d163d3SNathan Whitehorn         /* ofw_bus interface */
121ca2c1931SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_devinfo,	unin_chip_get_devinfo),
122ca2c1931SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
123ca2c1931SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
124ca2c1931SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
125ca2c1931SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
126ca2c1931SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
12751d163d3SNathan Whitehorn 
12898f8e6c0SBenno Rice 	{ 0, 0 }
12998f8e6c0SBenno Rice };
13098f8e6c0SBenno Rice 
131ca2c1931SNathan Whitehorn static driver_t	unin_chip_driver = {
132ca2c1931SNathan Whitehorn 	"unin",
133ca2c1931SNathan Whitehorn 	unin_chip_methods,
134ca2c1931SNathan Whitehorn 	sizeof(struct unin_chip_softc)
13598f8e6c0SBenno Rice };
13698f8e6c0SBenno Rice 
137ca2c1931SNathan Whitehorn static devclass_t	unin_chip_devclass;
13898f8e6c0SBenno Rice 
139ca2c1931SNathan Whitehorn DRIVER_MODULE(unin, nexus, unin_chip_driver, unin_chip_devclass, 0, 0);
14098f8e6c0SBenno Rice 
14151d163d3SNathan Whitehorn /*
142ca2c1931SNathan Whitehorn  * Add an interrupt to the dev's resource list if present
14351d163d3SNathan Whitehorn  */
144ca2c1931SNathan Whitehorn static void
145ca2c1931SNathan Whitehorn unin_chip_add_intr(phandle_t devnode, struct unin_chip_devinfo *dinfo)
146ca2c1931SNathan Whitehorn {
147*eaef5f0aSNathan Whitehorn 	phandle_t iparent;
148ca2c1931SNathan Whitehorn 	int	*intr;
149ca2c1931SNathan Whitehorn 	int	i, nintr;
150ca2c1931SNathan Whitehorn 	int 	icells;
15151d163d3SNathan Whitehorn 
152ca2c1931SNathan Whitehorn 	if (dinfo->udi_ninterrupts >= 6) {
153ca2c1931SNathan Whitehorn 		printf("unin: device has more than 6 interrupts\n");
154ca2c1931SNathan Whitehorn 		return;
15551d163d3SNathan Whitehorn 	}
15698f8e6c0SBenno Rice 
157ca2c1931SNathan Whitehorn 	nintr = OF_getprop_alloc(devnode, "interrupts", sizeof(*intr),
158ca2c1931SNathan Whitehorn 		(void **)&intr);
159ca2c1931SNathan Whitehorn 	if (nintr == -1) {
160ca2c1931SNathan Whitehorn 		nintr = OF_getprop_alloc(devnode, "AAPL,interrupts",
161ca2c1931SNathan Whitehorn 			sizeof(*intr), (void **)&intr);
162ca2c1931SNathan Whitehorn 		if (nintr == -1)
163ca2c1931SNathan Whitehorn 			return;
164ca2c1931SNathan Whitehorn 	}
165ca2c1931SNathan Whitehorn 
166ca2c1931SNathan Whitehorn 	if (intr[0] == -1)
167ca2c1931SNathan Whitehorn 		return;
168ca2c1931SNathan Whitehorn 
169ca2c1931SNathan Whitehorn 	if (OF_getprop(devnode, "interrupt-parent", &iparent, sizeof(iparent))
170ca2c1931SNathan Whitehorn 	    <= 0)
171ca2c1931SNathan Whitehorn 		panic("Interrupt but no interrupt parent!\n");
172ca2c1931SNathan Whitehorn 
173ca2c1931SNathan Whitehorn 	if (OF_searchprop(iparent, "#interrupt-cells", &icells, sizeof(icells))
174ca2c1931SNathan Whitehorn 	    <= 0)
175ca2c1931SNathan Whitehorn 		icells = 1;
176ca2c1931SNathan Whitehorn 
177ca2c1931SNathan Whitehorn 	for (i = 0; i < nintr; i+=icells) {
178ca2c1931SNathan Whitehorn 		resource_list_add(&dinfo->udi_resources, SYS_RES_IRQ,
179*eaef5f0aSNathan Whitehorn 		    dinfo->udi_ninterrupts, INTR_VEC(iparent, intr[i]),
180*eaef5f0aSNathan Whitehorn 		    INTR_VEC(iparent, intr[i]), 1);
181ca2c1931SNathan Whitehorn 
182*eaef5f0aSNathan Whitehorn 		if (icells > 1) {
183*eaef5f0aSNathan Whitehorn 			powerpc_config_intr(INTR_VEC(iparent, intr[i]),
184*eaef5f0aSNathan Whitehorn 			    (intr[i+1] & 1) ? INTR_TRIGGER_LEVEL :
185*eaef5f0aSNathan Whitehorn 			    INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH);
186*eaef5f0aSNathan Whitehorn 		}
187*eaef5f0aSNathan Whitehorn 
188*eaef5f0aSNathan Whitehorn 		dinfo->udi_interrupts[dinfo->udi_ninterrupts] =
189*eaef5f0aSNathan Whitehorn 		    INTR_VEC(iparent, intr[i]);
190ca2c1931SNathan Whitehorn 		dinfo->udi_ninterrupts++;
191ca2c1931SNathan Whitehorn 	}
192ca2c1931SNathan Whitehorn }
193ca2c1931SNathan Whitehorn 
194ca2c1931SNathan Whitehorn static void
195ca2c1931SNathan Whitehorn unin_chip_add_reg(phandle_t devnode, struct unin_chip_devinfo *dinfo)
196ca2c1931SNathan Whitehorn {
197ca2c1931SNathan Whitehorn 	struct	unin_chip_reg *reg;
198ca2c1931SNathan Whitehorn 	int	i, nreg;
199ca2c1931SNathan Whitehorn 
200ca2c1931SNathan Whitehorn 	nreg = OF_getprop_alloc(devnode, "reg", sizeof(*reg), (void **)&reg);
201ca2c1931SNathan Whitehorn 	if (nreg == -1)
202ca2c1931SNathan Whitehorn 		return;
203ca2c1931SNathan Whitehorn 
204ca2c1931SNathan Whitehorn 	for (i = 0; i < nreg; i++) {
205ca2c1931SNathan Whitehorn 		resource_list_add(&dinfo->udi_resources, SYS_RES_MEMORY, i,
206ca2c1931SNathan Whitehorn 				  reg[i].mr_base,
207ca2c1931SNathan Whitehorn 				  reg[i].mr_base + reg[i].mr_size,
208ca2c1931SNathan Whitehorn 				  reg[i].mr_size);
209ca2c1931SNathan Whitehorn 	}
210ca2c1931SNathan Whitehorn }
211ca2c1931SNathan Whitehorn 
212ca2c1931SNathan Whitehorn static void
213ca2c1931SNathan Whitehorn unin_enable_gmac(device_t dev)
214ca2c1931SNathan Whitehorn {
215ca2c1931SNathan Whitehorn 	volatile u_int *clkreg;
216ca2c1931SNathan Whitehorn 	struct unin_chip_softc *sc;
217ca2c1931SNathan Whitehorn 	u_int32_t tmpl;
218ca2c1931SNathan Whitehorn 
219ca2c1931SNathan Whitehorn 	sc = device_get_softc(dev);
220ca2c1931SNathan Whitehorn 	clkreg = (void *)(sc->sc_addr + UNIN_CLOCKCNTL);
221ca2c1931SNathan Whitehorn 	tmpl = inl(clkreg);
222ca2c1931SNathan Whitehorn 	tmpl |= UNIN_CLOCKCNTL_GMAC;
223ca2c1931SNathan Whitehorn 	outl(clkreg, tmpl);
224ca2c1931SNathan Whitehorn }
225ca2c1931SNathan Whitehorn 
226ca2c1931SNathan Whitehorn static void
227ca2c1931SNathan Whitehorn unin_enable_mpic(device_t dev)
228ca2c1931SNathan Whitehorn {
229ca2c1931SNathan Whitehorn 	volatile u_int *toggle;
230ca2c1931SNathan Whitehorn 	struct unin_chip_softc *sc;
231ca2c1931SNathan Whitehorn 	u_int32_t tmpl;
232ca2c1931SNathan Whitehorn 
233ca2c1931SNathan Whitehorn 	sc = device_get_softc(dev);
234ca2c1931SNathan Whitehorn 	toggle = (void *)(sc->sc_addr + UNIN_TOGGLE_REG);
235ca2c1931SNathan Whitehorn 	tmpl = inl(toggle);
236ca2c1931SNathan Whitehorn 	tmpl |= UNIN_MPIC_RESET | UNIN_MPIC_OUTPUT_ENABLE;
237ca2c1931SNathan Whitehorn 	outl(toggle, tmpl);
238ca2c1931SNathan Whitehorn }
239ca2c1931SNathan Whitehorn 
240ca2c1931SNathan Whitehorn static int
241ca2c1931SNathan Whitehorn unin_chip_probe(device_t dev)
242ca2c1931SNathan Whitehorn {
243ca2c1931SNathan Whitehorn 	const char	*name;
244ca2c1931SNathan Whitehorn 
245ca2c1931SNathan Whitehorn 	name = ofw_bus_get_name(dev);
246ca2c1931SNathan Whitehorn 
247ca2c1931SNathan Whitehorn 	if (name == NULL)
24898f8e6c0SBenno Rice 		return (ENXIO);
24998f8e6c0SBenno Rice 
250ca2c1931SNathan Whitehorn 	if (strcmp(name, "uni-n") != 0 && strcmp(name, "u3") != 0
251ca2c1931SNathan Whitehorn 	    && strcmp(name, "u4") != 0)
25298f8e6c0SBenno Rice 		return (ENXIO);
253ca2c1931SNathan Whitehorn 
254ca2c1931SNathan Whitehorn 	device_set_desc(dev, "Apple UniNorth System Controller");
255ca2c1931SNathan Whitehorn 	return (0);
25698f8e6c0SBenno Rice }
25798f8e6c0SBenno Rice 
258ca2c1931SNathan Whitehorn static int
259ca2c1931SNathan Whitehorn unin_chip_attach(device_t dev)
260ca2c1931SNathan Whitehorn {
261ca2c1931SNathan Whitehorn 	struct unin_chip_softc *sc;
262ca2c1931SNathan Whitehorn 	struct unin_chip_devinfo *dinfo;
263ca2c1931SNathan Whitehorn 	phandle_t  root;
264ca2c1931SNathan Whitehorn 	phandle_t  child;
265ca2c1931SNathan Whitehorn 	device_t   cdev;
266ca2c1931SNathan Whitehorn 	char compat[32];
267ca2c1931SNathan Whitehorn 	u_int reg[3];
268ca2c1931SNathan Whitehorn 	int error, i = 0;
269ca2c1931SNathan Whitehorn 
270ca2c1931SNathan Whitehorn 	sc = device_get_softc(dev);
271ca2c1931SNathan Whitehorn 	root = ofw_bus_get_node(dev);
272ca2c1931SNathan Whitehorn 
273ca2c1931SNathan Whitehorn 	if (OF_getprop(root, "reg", reg, sizeof(reg)) < 8)
27498f8e6c0SBenno Rice 		return (ENXIO);
275ca2c1931SNathan Whitehorn 
276ca2c1931SNathan Whitehorn 	if (strcmp(ofw_bus_get_name(dev), "u3") == 0
277ca2c1931SNathan Whitehorn 	    || strcmp(ofw_bus_get_name(dev), "u4") == 0)
278ca2c1931SNathan Whitehorn 		i = 1; /* #address-cells lies */
279ca2c1931SNathan Whitehorn 
280ca2c1931SNathan Whitehorn 	sc->sc_physaddr = reg[i];
281ca2c1931SNathan Whitehorn 	sc->sc_size = reg[i+1];
282ca2c1931SNathan Whitehorn 
28398f8e6c0SBenno Rice 	sc->sc_mem_rman.rm_type = RMAN_ARRAY;
284ca2c1931SNathan Whitehorn 	sc->sc_mem_rman.rm_descr = "UniNorth Device Memory";
285ca2c1931SNathan Whitehorn 
286bda386dbSKevin Lo 	error = rman_init(&sc->sc_mem_rman);
287ca2c1931SNathan Whitehorn 
288bda386dbSKevin Lo 	if (error) {
289bda386dbSKevin Lo 		device_printf(dev, "rman_init() failed. error = %d\n", error);
290bda386dbSKevin Lo 		return (error);
29198f8e6c0SBenno Rice 	}
292ca2c1931SNathan Whitehorn 
293ca2c1931SNathan Whitehorn 	error = rman_manage_region(&sc->sc_mem_rman, sc->sc_physaddr,
294ca2c1931SNathan Whitehorn 				   sc->sc_physaddr + sc->sc_size - 1);
295bda386dbSKevin Lo 	if (error) {
29698f8e6c0SBenno Rice 		device_printf(dev,
297ca2c1931SNathan Whitehorn 			      "rman_manage_region() failed. error = %d\n",
298ca2c1931SNathan Whitehorn 			      error);
299bda386dbSKevin Lo 		return (error);
30098f8e6c0SBenno Rice 	}
30198f8e6c0SBenno Rice 
302d48bbef3SPeter Grehan         /*
303ca2c1931SNathan Whitehorn 	 * Iterate through the sub-devices
304d48bbef3SPeter Grehan 	 */
305ca2c1931SNathan Whitehorn 	for (child = OF_child(root); child != 0; child = OF_peer(child)) {
306ca2c1931SNathan Whitehorn 		dinfo = malloc(sizeof(*dinfo), M_UNIN, M_WAITOK | M_ZERO);
307ca2c1931SNathan Whitehorn 		if (ofw_bus_gen_setup_devinfo(&dinfo->udi_obdinfo, child)
308ca2c1931SNathan Whitehorn 		    != 0)
309ca2c1931SNathan Whitehorn 		{
310ca2c1931SNathan Whitehorn 			free(dinfo, M_UNIN);
311ca2c1931SNathan Whitehorn 			continue;
312ca2c1931SNathan Whitehorn 		}
313d48bbef3SPeter Grehan 
314ca2c1931SNathan Whitehorn 		resource_list_init(&dinfo->udi_resources);
315ca2c1931SNathan Whitehorn 		dinfo->udi_ninterrupts = 0;
316ca2c1931SNathan Whitehorn 		unin_chip_add_intr(child, dinfo);
317ca2c1931SNathan Whitehorn 
318ca2c1931SNathan Whitehorn 		unin_chip_add_reg(child, dinfo);
319ca2c1931SNathan Whitehorn 
320ca2c1931SNathan Whitehorn 		cdev = device_add_child(dev, NULL, -1);
321ca2c1931SNathan Whitehorn 		if (cdev == NULL) {
322ca2c1931SNathan Whitehorn 			device_printf(dev, "<%s>: device_add_child failed\n",
323ca2c1931SNathan Whitehorn 				      dinfo->udi_obdinfo.obd_name);
324ca2c1931SNathan Whitehorn 			resource_list_free(&dinfo->udi_resources);
325ca2c1931SNathan Whitehorn 			ofw_bus_gen_destroy_devinfo(&dinfo->udi_obdinfo);
326ca2c1931SNathan Whitehorn 			free(dinfo, M_UNIN);
327ca2c1931SNathan Whitehorn 			continue;
328ca2c1931SNathan Whitehorn 		}
329ca2c1931SNathan Whitehorn 
330ca2c1931SNathan Whitehorn 		device_set_ivars(cdev, dinfo);
331ca2c1931SNathan Whitehorn 	}
332ca2c1931SNathan Whitehorn 
333ca2c1931SNathan Whitehorn 	/*
334ca2c1931SNathan Whitehorn 	 * Only map the first page, since that is where the registers
335ca2c1931SNathan Whitehorn 	 * of interest lie.
336ca2c1931SNathan Whitehorn 	 */
337ca2c1931SNathan Whitehorn 	sc->sc_addr = (vm_offset_t)pmap_mapdev(sc->sc_physaddr, PAGE_SIZE);
338ca2c1931SNathan Whitehorn 
339ca2c1931SNathan Whitehorn 	sc->sc_version = *(u_int *)sc->sc_addr;
340ca2c1931SNathan Whitehorn 	device_printf(dev, "Version %d\n", sc->sc_version);
341ca2c1931SNathan Whitehorn 
342ca2c1931SNathan Whitehorn 	/*
343ca2c1931SNathan Whitehorn 	 * Enable the GMAC Ethernet cell and the integrated OpenPIC
344ca2c1931SNathan Whitehorn 	 * if Open Firmware says they are used.
345ca2c1931SNathan Whitehorn 	 */
346ca2c1931SNathan Whitehorn 	for (child = OF_child(root); child; child = OF_peer(child)) {
347d48bbef3SPeter Grehan 		memset(compat, 0, sizeof(compat));
348d48bbef3SPeter Grehan 		OF_getprop(child, "compatible", compat, sizeof(compat));
349ca2c1931SNathan Whitehorn 		if (strcmp(compat, "gmac") == 0)
350ca2c1931SNathan Whitehorn 			unin_enable_gmac(dev);
351ca2c1931SNathan Whitehorn 		if (strcmp(compat, "chrp,open-pic") == 0)
352ca2c1931SNathan Whitehorn 			unin_enable_mpic(dev);
353d48bbef3SPeter Grehan 	}
354d48bbef3SPeter Grehan 
355ca2c1931SNathan Whitehorn 	/*
356ca2c1931SNathan Whitehorn 	 * GMAC lives under the PCI bus, so just check if enet is gmac.
357ca2c1931SNathan Whitehorn 	 */
358ca2c1931SNathan Whitehorn 	child = OF_finddevice("enet");
359ca2c1931SNathan Whitehorn 	memset(compat, 0, sizeof(compat));
360ca2c1931SNathan Whitehorn 	OF_getprop(child, "compatible", compat, sizeof(compat));
361ca2c1931SNathan Whitehorn 	if (strcmp(compat, "gmac") == 0)
362ca2c1931SNathan Whitehorn 		unin_enable_gmac(dev);
36394b4a038SNathan Whitehorn 
36498f8e6c0SBenno Rice 	return (bus_generic_attach(dev));
36598f8e6c0SBenno Rice }
36698f8e6c0SBenno Rice 
36798f8e6c0SBenno Rice static int
368ca2c1931SNathan Whitehorn unin_chip_print_child(device_t dev, device_t child)
36998f8e6c0SBenno Rice {
370ca2c1931SNathan Whitehorn         struct unin_chip_devinfo *dinfo;
371ca2c1931SNathan Whitehorn         struct resource_list *rl;
372ca2c1931SNathan Whitehorn         int retval = 0;
37398f8e6c0SBenno Rice 
374ca2c1931SNathan Whitehorn         dinfo = device_get_ivars(child);
375ca2c1931SNathan Whitehorn         rl = &dinfo->udi_resources;
37698f8e6c0SBenno Rice 
377ca2c1931SNathan Whitehorn         retval += bus_print_child_header(dev, child);
37898f8e6c0SBenno Rice 
379ca2c1931SNathan Whitehorn         retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
380ca2c1931SNathan Whitehorn         retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
38198f8e6c0SBenno Rice 
382ca2c1931SNathan Whitehorn         retval += bus_print_child_footer(dev, child);
38398f8e6c0SBenno Rice 
384ca2c1931SNathan Whitehorn         return (retval);
38598f8e6c0SBenno Rice }
38698f8e6c0SBenno Rice 
38798f8e6c0SBenno Rice static void
388ca2c1931SNathan Whitehorn unin_chip_probe_nomatch(device_t dev, device_t child)
38998f8e6c0SBenno Rice {
390ca2c1931SNathan Whitehorn         struct unin_chip_devinfo *dinfo;
391ca2c1931SNathan Whitehorn         struct resource_list *rl;
392ca2c1931SNathan Whitehorn 	const char *type;
39398f8e6c0SBenno Rice 
394ca2c1931SNathan Whitehorn 	if (bootverbose) {
395ca2c1931SNathan Whitehorn 		dinfo = device_get_ivars(child);
396ca2c1931SNathan Whitehorn 		rl = &dinfo->udi_resources;
39798f8e6c0SBenno Rice 
398ca2c1931SNathan Whitehorn 		if ((type = ofw_bus_get_type(child)) == NULL)
399ca2c1931SNathan Whitehorn 			type = "(unknown)";
400ca2c1931SNathan Whitehorn 		device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child));
401ca2c1931SNathan Whitehorn 		resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
402ca2c1931SNathan Whitehorn 		resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
403ca2c1931SNathan Whitehorn 		printf(" (no driver attached)\n");
40498f8e6c0SBenno Rice 	}
40598f8e6c0SBenno Rice }
40698f8e6c0SBenno Rice 
40798f8e6c0SBenno Rice 
40898f8e6c0SBenno Rice static struct resource *
409ca2c1931SNathan Whitehorn unin_chip_alloc_resource(device_t bus, device_t child, int type, int *rid,
41098f8e6c0SBenno Rice 			 u_long start, u_long end, u_long count, u_int flags)
41198f8e6c0SBenno Rice {
412ca2c1931SNathan Whitehorn 	struct		unin_chip_softc *sc;
413ca2c1931SNathan Whitehorn 	int		needactivate;
41498f8e6c0SBenno Rice 	struct		resource *rv;
41598f8e6c0SBenno Rice 	struct		rman *rm;
416ca2c1931SNathan Whitehorn 	u_long		adjstart, adjend, adjcount;
417ca2c1931SNathan Whitehorn 	struct		unin_chip_devinfo *dinfo;
418ca2c1931SNathan Whitehorn 	struct		resource_list_entry *rle;
419ca2c1931SNathan Whitehorn 
420ca2c1931SNathan Whitehorn 	sc = device_get_softc(bus);
421ca2c1931SNathan Whitehorn 	dinfo = device_get_ivars(child);
42298f8e6c0SBenno Rice 
42398f8e6c0SBenno Rice 	needactivate = flags & RF_ACTIVE;
42498f8e6c0SBenno Rice 	flags &= ~RF_ACTIVE;
42598f8e6c0SBenno Rice 
42698f8e6c0SBenno Rice 	switch (type) {
42798f8e6c0SBenno Rice 	case SYS_RES_MEMORY:
428ca2c1931SNathan Whitehorn 	case SYS_RES_IOPORT:
429ca2c1931SNathan Whitehorn 		rle = resource_list_find(&dinfo->udi_resources, SYS_RES_MEMORY,
430ca2c1931SNathan Whitehorn 					 *rid);
431ca2c1931SNathan Whitehorn 		if (rle == NULL) {
432ca2c1931SNathan Whitehorn 			device_printf(bus, "no rle for %s memory %d\n",
433ca2c1931SNathan Whitehorn 				      device_get_nameunit(child), *rid);
434ca2c1931SNathan Whitehorn 			return (NULL);
435ca2c1931SNathan Whitehorn 		}
436ca2c1931SNathan Whitehorn 
437ca2c1931SNathan Whitehorn 		rle->end = rle->end - 1; /* Hack? */
438ca2c1931SNathan Whitehorn 
439ca2c1931SNathan Whitehorn 		if (start < rle->start)
440ca2c1931SNathan Whitehorn 			adjstart = rle->start;
441ca2c1931SNathan Whitehorn 		else if (start > rle->end)
442ca2c1931SNathan Whitehorn 			adjstart = rle->end;
443ca2c1931SNathan Whitehorn 		else
444ca2c1931SNathan Whitehorn 			adjstart = start;
445ca2c1931SNathan Whitehorn 
446ca2c1931SNathan Whitehorn 		if (end < rle->start)
447ca2c1931SNathan Whitehorn 			adjend = rle->start;
448ca2c1931SNathan Whitehorn 		else if (end > rle->end)
449ca2c1931SNathan Whitehorn 			adjend = rle->end;
450ca2c1931SNathan Whitehorn 		else
451ca2c1931SNathan Whitehorn 			adjend = end;
452ca2c1931SNathan Whitehorn 
453ca2c1931SNathan Whitehorn 		adjcount = adjend - adjstart;
454ca2c1931SNathan Whitehorn 
45598f8e6c0SBenno Rice 		rm = &sc->sc_mem_rman;
45698f8e6c0SBenno Rice 		break;
457714aa5b9SAndrew Gallatin 
45898f8e6c0SBenno Rice 	case SYS_RES_IRQ:
459ca2c1931SNathan Whitehorn 		/* Check for passthrough from subattachments. */
460ca2c1931SNathan Whitehorn 		if (device_get_parent(child) != bus)
461ca2c1931SNathan Whitehorn 			return BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
462ca2c1931SNathan Whitehorn 						  type, rid, start, end, count,
463ca2c1931SNathan Whitehorn 						  flags);
464de2fa7b8SMarcel Moolenaar 
465ca2c1931SNathan Whitehorn 		rle = resource_list_find(&dinfo->udi_resources, SYS_RES_IRQ,
466ca2c1931SNathan Whitehorn 		    *rid);
467ca2c1931SNathan Whitehorn 		if (rle == NULL) {
468ca2c1931SNathan Whitehorn 			if (dinfo->udi_ninterrupts >= 6) {
469ca2c1931SNathan Whitehorn 				device_printf(bus,
470ca2c1931SNathan Whitehorn 					      "%s has more than 6 interrupts\n",
471ca2c1931SNathan Whitehorn 					      device_get_nameunit(child));
472ca2c1931SNathan Whitehorn 				return (NULL);
473ca2c1931SNathan Whitehorn 			}
474ca2c1931SNathan Whitehorn 			resource_list_add(&dinfo->udi_resources, SYS_RES_IRQ,
475ca2c1931SNathan Whitehorn 					  dinfo->udi_ninterrupts, start, start,
476ca2c1931SNathan Whitehorn 					  1);
477ca2c1931SNathan Whitehorn 
478ca2c1931SNathan Whitehorn 			dinfo->udi_interrupts[dinfo->udi_ninterrupts] = start;
479ca2c1931SNathan Whitehorn 			dinfo->udi_ninterrupts++;
480ca2c1931SNathan Whitehorn 		}
481ca2c1931SNathan Whitehorn 
482ca2c1931SNathan Whitehorn 		return (resource_list_alloc(&dinfo->udi_resources, bus, child,
483ca2c1931SNathan Whitehorn 					    type, rid, start, end, count,
484ca2c1931SNathan Whitehorn 					    flags));
48598f8e6c0SBenno Rice 	default:
48698f8e6c0SBenno Rice 		device_printf(bus, "unknown resource request from %s\n",
48798f8e6c0SBenno Rice 			      device_get_nameunit(child));
48898f8e6c0SBenno Rice 		return (NULL);
48998f8e6c0SBenno Rice 	}
49098f8e6c0SBenno Rice 
491ca2c1931SNathan Whitehorn 	rv = rman_reserve_resource(rm, adjstart, adjend, adjcount, flags,
492ca2c1931SNathan Whitehorn 				   child);
49398f8e6c0SBenno Rice 	if (rv == NULL) {
494ca2c1931SNathan Whitehorn 		device_printf(bus,
495ca2c1931SNathan Whitehorn 			      "failed to reserve resource %#lx - %#lx (%#lx)"
496ca2c1931SNathan Whitehorn 			      " for %s\n", adjstart, adjend, adjcount,
49798f8e6c0SBenno Rice 			      device_get_nameunit(child));
49898f8e6c0SBenno Rice 		return (NULL);
49998f8e6c0SBenno Rice 	}
50098f8e6c0SBenno Rice 
501bdfb66f8SWarner Losh 	rman_set_rid(rv, *rid);
50298f8e6c0SBenno Rice 
50398f8e6c0SBenno Rice 	if (needactivate) {
50498f8e6c0SBenno Rice 		if (bus_activate_resource(child, type, *rid, rv) != 0) {
50598f8e6c0SBenno Rice                         device_printf(bus,
50698f8e6c0SBenno Rice 				      "failed to activate resource for %s\n",
50798f8e6c0SBenno Rice 				      device_get_nameunit(child));
50898f8e6c0SBenno Rice 			rman_release_resource(rv);
50998f8e6c0SBenno Rice 			return (NULL);
51098f8e6c0SBenno Rice                 }
51198f8e6c0SBenno Rice         }
51298f8e6c0SBenno Rice 
51398f8e6c0SBenno Rice 	return (rv);
51498f8e6c0SBenno Rice }
51598f8e6c0SBenno Rice 
51698f8e6c0SBenno Rice static int
517ca2c1931SNathan Whitehorn unin_chip_release_resource(device_t bus, device_t child, int type, int rid,
51898f8e6c0SBenno Rice 			   struct resource *res)
51998f8e6c0SBenno Rice {
520ca2c1931SNathan Whitehorn 	if (rman_get_flags(res) & RF_ACTIVE) {
521ca2c1931SNathan Whitehorn 		int error = bus_deactivate_resource(child, type, rid, res);
522ca2c1931SNathan Whitehorn 		if (error)
523ca2c1931SNathan Whitehorn 			return error;
524ca2c1931SNathan Whitehorn 	}
525ca2c1931SNathan Whitehorn 
526ca2c1931SNathan Whitehorn 	return (rman_release_resource(res));
527ca2c1931SNathan Whitehorn }
528ca2c1931SNathan Whitehorn 
529ca2c1931SNathan Whitehorn static int
530ca2c1931SNathan Whitehorn unin_chip_activate_resource(device_t bus, device_t child, int type, int rid,
531ca2c1931SNathan Whitehorn 			    struct resource *res)
532ca2c1931SNathan Whitehorn {
533ca2c1931SNathan Whitehorn 	struct unin_chip_softc *sc;
53498f8e6c0SBenno Rice 	void    *p;
535714aa5b9SAndrew Gallatin 
536714aa5b9SAndrew Gallatin 	sc = device_get_softc(bus);
53798f8e6c0SBenno Rice 
53898f8e6c0SBenno Rice 	if (type == SYS_RES_IRQ)
53998f8e6c0SBenno Rice                 return (bus_activate_resource(bus, type, rid, res));
54098f8e6c0SBenno Rice 
541ca2c1931SNathan Whitehorn 	if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
542714aa5b9SAndrew Gallatin 		vm_offset_t start;
543714aa5b9SAndrew Gallatin 
544714aa5b9SAndrew Gallatin 		start = (vm_offset_t) rman_get_start(res);
545714aa5b9SAndrew Gallatin 
546714aa5b9SAndrew Gallatin 		if (bootverbose)
547ca2c1931SNathan Whitehorn 			printf("unin mapdev: start %zx, len %ld\n", start,
548714aa5b9SAndrew Gallatin 			       rman_get_size(res));
549714aa5b9SAndrew Gallatin 
550714aa5b9SAndrew Gallatin 		p = pmap_mapdev(start, (vm_size_t) rman_get_size(res));
55198f8e6c0SBenno Rice 		if (p == NULL)
55298f8e6c0SBenno Rice 			return (ENOMEM);
55398f8e6c0SBenno Rice 		rman_set_virtual(res, p);
554ca2c1931SNathan Whitehorn 		rman_set_bustag(res, &bs_be_tag);
55598f8e6c0SBenno Rice 		rman_set_bushandle(res, (u_long)p);
55698f8e6c0SBenno Rice 	}
55798f8e6c0SBenno Rice 
55898f8e6c0SBenno Rice 	return (rman_activate_resource(res));
55998f8e6c0SBenno Rice }
56098f8e6c0SBenno Rice 
561ca2c1931SNathan Whitehorn 
56298f8e6c0SBenno Rice static int
563ca2c1931SNathan Whitehorn unin_chip_deactivate_resource(device_t bus, device_t child, int type, int rid,
564ca2c1931SNathan Whitehorn 			      struct resource *res)
56598f8e6c0SBenno Rice {
56698f8e6c0SBenno Rice         /*
567ca2c1931SNathan Whitehorn          * If this is a memory resource, unmap it.
56898f8e6c0SBenno Rice          */
569ca2c1931SNathan Whitehorn         if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
570ca2c1931SNathan Whitehorn 		u_int32_t psize;
57198f8e6c0SBenno Rice 
572ca2c1931SNathan Whitehorn 		psize = rman_get_size(res);
573ca2c1931SNathan Whitehorn 		pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
57498f8e6c0SBenno Rice 	}
57598f8e6c0SBenno Rice 
576ca2c1931SNathan Whitehorn 	return (rman_deactivate_resource(res));
57798f8e6c0SBenno Rice }
57898f8e6c0SBenno Rice 
579ca2c1931SNathan Whitehorn 
580ca2c1931SNathan Whitehorn static struct resource_list *
581ca2c1931SNathan Whitehorn unin_chip_get_resource_list (device_t dev, device_t child)
58251d163d3SNathan Whitehorn {
583ca2c1931SNathan Whitehorn 	struct unin_chip_devinfo *dinfo;
58451d163d3SNathan Whitehorn 
585ca2c1931SNathan Whitehorn 	dinfo = device_get_ivars(child);
586ca2c1931SNathan Whitehorn 	return (&dinfo->udi_resources);
58751d163d3SNathan Whitehorn }
58851d163d3SNathan Whitehorn 
589ca2c1931SNathan Whitehorn static const struct ofw_bus_devinfo *
590ca2c1931SNathan Whitehorn unin_chip_get_devinfo(device_t dev, device_t child)
59198f8e6c0SBenno Rice {
592ca2c1931SNathan Whitehorn 	struct unin_chip_devinfo *dinfo;
59398f8e6c0SBenno Rice 
594ca2c1931SNathan Whitehorn 	dinfo = device_get_ivars(child);
595ca2c1931SNathan Whitehorn 	return (&dinfo->udi_obdinfo);
59698f8e6c0SBenno Rice }
59798f8e6c0SBenno Rice 
598