xref: /freebsd/sys/powerpc/powermac/macio.c (revision 732a02b4e77866604a120a275c082bb6221bd2ff)
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  * Driver for KeyLargo/Pangea, the MacPPC south bridge ASIC.
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/bus.h>
42 #include <sys/rman.h>
43 
44 #include <vm/vm.h>
45 #include <vm/pmap.h>
46 
47 #include <machine/bus.h>
48 #include <machine/intr_machdep.h>
49 #include <machine/resource.h>
50 #include <machine/vmparam.h>
51 
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
54 #include <dev/ofw/openfirm.h>
55 
56 #include <powerpc/powermac/maciovar.h>
57 
58 #include <dev/pci/pcivar.h>
59 #include <dev/pci/pcireg.h>
60 
61 /*
62  * Macio softc
63  */
64 struct macio_softc {
65 	phandle_t    sc_node;
66 	vm_offset_t  sc_base;
67 	vm_offset_t  sc_size;
68 	struct rman  sc_mem_rman;
69 
70 	/* FCR registers */
71 	int          sc_memrid;
72 	struct resource	*sc_memr;
73 };
74 
75 static MALLOC_DEFINE(M_MACIO, "macio", "macio device information");
76 
77 static int  macio_probe(device_t);
78 static int  macio_attach(device_t);
79 static int  macio_print_child(device_t dev, device_t child);
80 static void macio_probe_nomatch(device_t, device_t);
81 static struct   resource *macio_alloc_resource(device_t, device_t, int, int *,
82 					       rman_res_t, rman_res_t, rman_res_t,
83 					       u_int);
84 static int  macio_activate_resource(device_t, device_t, int, int,
85 				    struct resource *);
86 static int  macio_deactivate_resource(device_t, device_t, int, int,
87 				      struct resource *);
88 static int  macio_release_resource(device_t, device_t, int, int,
89 				   struct resource *);
90 static struct resource_list *macio_get_resource_list (device_t, device_t);
91 static ofw_bus_get_devinfo_t macio_get_devinfo;
92 
93 /*
94  * Bus interface definition
95  */
96 static device_method_t macio_methods[] = {
97 	/* Device interface */
98 	DEVMETHOD(device_probe,         macio_probe),
99 	DEVMETHOD(device_attach,        macio_attach),
100 	DEVMETHOD(device_detach,        bus_generic_detach),
101 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
102 	DEVMETHOD(device_suspend,       bus_generic_suspend),
103 	DEVMETHOD(device_resume,        bus_generic_resume),
104 
105 	/* Bus interface */
106 	DEVMETHOD(bus_print_child,      macio_print_child),
107 	DEVMETHOD(bus_probe_nomatch,    macio_probe_nomatch),
108 	DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
109 	DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
110 
111         DEVMETHOD(bus_alloc_resource,   macio_alloc_resource),
112         DEVMETHOD(bus_release_resource, macio_release_resource),
113         DEVMETHOD(bus_activate_resource, macio_activate_resource),
114         DEVMETHOD(bus_deactivate_resource, macio_deactivate_resource),
115         DEVMETHOD(bus_get_resource_list, macio_get_resource_list),
116 
117 	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
118 
119 	/* ofw_bus interface */
120 	DEVMETHOD(ofw_bus_get_devinfo,	macio_get_devinfo),
121 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
122 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
123 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
124 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
125 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
126 
127 	{ 0, 0 }
128 };
129 
130 static driver_t macio_pci_driver = {
131         "macio",
132         macio_methods,
133 	sizeof(struct macio_softc)
134 };
135 
136 devclass_t macio_devclass;
137 
138 EARLY_DRIVER_MODULE(macio, pci, macio_pci_driver, macio_devclass, 0, 0,
139     BUS_PASS_BUS);
140 
141 /*
142  * PCI ID search table
143  */
144 static struct macio_pci_dev {
145         u_int32_t  mpd_devid;
146 	char    *mpd_desc;
147 } macio_pci_devlist[] = {
148 	{ 0x0017106b, "Paddington I/O Controller" },
149 	{ 0x0022106b, "KeyLargo I/O Controller" },
150 	{ 0x0025106b, "Pangea I/O Controller" },
151 	{ 0x003e106b, "Intrepid I/O Controller" },
152 	{ 0x0041106b, "K2 KeyLargo I/O Controller" },
153 	{ 0x004f106b, "Shasta I/O Controller" },
154 	{ 0, NULL }
155 };
156 
157 /*
158  * Devices to exclude from the probe
159  * XXX some of these may be required in the future...
160  */
161 #define	MACIO_QUIRK_IGNORE		0x00000001
162 #define	MACIO_QUIRK_CHILD_HAS_INTR	0x00000002
163 #define	MACIO_QUIRK_USE_CHILD_REG	0x00000004
164 
165 struct macio_quirk_entry {
166 	const char	*mq_name;
167 	int		mq_quirks;
168 };
169 
170 static struct macio_quirk_entry macio_quirks[] = {
171 	{ "escc-legacy",		MACIO_QUIRK_IGNORE },
172 	{ "timer",			MACIO_QUIRK_IGNORE },
173 	{ "escc",			MACIO_QUIRK_CHILD_HAS_INTR },
174         { "i2s", 			MACIO_QUIRK_CHILD_HAS_INTR |
175 					MACIO_QUIRK_USE_CHILD_REG },
176 	{ NULL,				0 }
177 };
178 
179 static int
180 macio_get_quirks(const char *name)
181 {
182         struct	macio_quirk_entry *mqe;
183 
184         for (mqe = macio_quirks; mqe->mq_name != NULL; mqe++)
185                 if (strcmp(name, mqe->mq_name) == 0)
186                         return (mqe->mq_quirks);
187         return (0);
188 }
189 
190 
191 /*
192  * Add an interrupt to the dev's resource list if present
193  */
194 static void
195 macio_add_intr(phandle_t devnode, struct macio_devinfo *dinfo)
196 {
197 	phandle_t iparent;
198 	int	*intr;
199 	int	i, nintr;
200 	int 	icells;
201 
202 	if (dinfo->mdi_ninterrupts >= 6) {
203 		printf("macio: device has more than 6 interrupts\n");
204 		return;
205 	}
206 
207 	nintr = OF_getprop_alloc_multi(devnode, "interrupts", sizeof(*intr),
208 		(void **)&intr);
209 	if (nintr == -1) {
210 		nintr = OF_getprop_alloc_multi(devnode, "AAPL,interrupts",
211 			sizeof(*intr), (void **)&intr);
212 		if (nintr == -1)
213 			return;
214 	}
215 
216 	if (intr[0] == -1)
217 		return;
218 
219 	if (OF_getprop(devnode, "interrupt-parent", &iparent, sizeof(iparent))
220 	    <= 0)
221 		panic("Interrupt but no interrupt parent!\n");
222 
223 	if (OF_getprop(OF_node_from_xref(iparent), "#interrupt-cells", &icells,
224 	    sizeof(icells)) <= 0)
225 		icells = 1;
226 
227 	for (i = 0; i < nintr; i+=icells) {
228 		u_int irq = MAP_IRQ(iparent, intr[i]);
229 
230 		resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ,
231 		    dinfo->mdi_ninterrupts, irq, irq, 1);
232 
233 		dinfo->mdi_interrupts[dinfo->mdi_ninterrupts] = irq;
234 		dinfo->mdi_ninterrupts++;
235 	}
236 }
237 
238 
239 static void
240 macio_add_reg(phandle_t devnode, struct macio_devinfo *dinfo)
241 {
242 	struct		macio_reg *reg, *regp;
243 	phandle_t 	child;
244 	char		buf[8];
245 	int		i, layout_id = 0, nreg, res;
246 
247 	nreg = OF_getprop_alloc_multi(devnode, "reg", sizeof(*reg), (void **)&reg);
248 	if (nreg == -1)
249 		return;
250 
251         /*
252          *  Some G5's have broken properties in the i2s-a area. If so we try
253          *  to fix it. Right now we know of two different cases, one for
254          *  sound layout-id 36 and the other one for sound layout-id 76.
255          *  What is missing is the base address for the memory addresses.
256          *  We take them from the parent node (i2s) and use the size
257          *  information from the child.
258          */
259 
260         if (reg[0].mr_base == 0) {
261 		child = OF_child(devnode);
262 		while (child != 0) {
263 			res = OF_getprop(child, "name", buf, sizeof(buf));
264 			if (res > 0 && strcmp(buf, "sound") == 0)
265 				break;
266 			child = OF_peer(child);
267 		}
268 
269                 res = OF_getprop(child, "layout-id", &layout_id,
270 				sizeof(layout_id));
271 
272                 if (res > 0 && (layout_id == 36 || layout_id == 76)) {
273                         res = OF_getprop_alloc_multi(OF_parent(devnode), "reg",
274 						sizeof(*regp), (void **)&regp);
275                         reg[0] = regp[0];
276                         reg[1].mr_base = regp[1].mr_base;
277                         reg[2].mr_base = regp[1].mr_base + reg[1].mr_size;
278                 }
279         }
280 
281 	for (i = 0; i < nreg; i++) {
282 		resource_list_add(&dinfo->mdi_resources, SYS_RES_MEMORY, i,
283 		    reg[i].mr_base, reg[i].mr_base + reg[i].mr_size,
284 		    reg[i].mr_size);
285 	}
286 }
287 
288 /*
289  * PCI probe
290  */
291 static int
292 macio_probe(device_t dev)
293 {
294         int i;
295         u_int32_t devid;
296 
297         devid = pci_get_devid(dev);
298         for (i = 0; macio_pci_devlist[i].mpd_desc != NULL; i++) {
299                 if (devid == macio_pci_devlist[i].mpd_devid) {
300                         device_set_desc(dev, macio_pci_devlist[i].mpd_desc);
301                         return (0);
302                 }
303         }
304 
305         return (ENXIO);
306 }
307 
308 /*
309  * PCI attach: scan Open Firmware child nodes, and attach these as children
310  * of the macio bus
311  */
312 static int
313 macio_attach(device_t dev)
314 {
315 	struct macio_softc *sc;
316         struct macio_devinfo *dinfo;
317         phandle_t  root;
318 	phandle_t  child;
319 	phandle_t  subchild;
320         device_t cdev;
321         u_int reg[3];
322 	char compat[32];
323 	int error, quirks;
324 
325 	sc = device_get_softc(dev);
326 	root = sc->sc_node = ofw_bus_get_node(dev);
327 
328 	/*
329 	 * Locate the device node and it's base address
330 	 */
331 	if (OF_getprop(root, "assigned-addresses",
332 		       reg, sizeof(reg)) < (ssize_t)sizeof(reg)) {
333 		return (ENXIO);
334 	}
335 
336 	/* Used later to see if we have to enable the I2S part. */
337 	OF_getprop(root, "compatible", compat, sizeof(compat));
338 
339 	sc->sc_base = reg[2];
340 	sc->sc_size = MACIO_REG_SIZE;
341 
342 	sc->sc_memrid = PCIR_BAR(0);
343 	sc->sc_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
344 	    &sc->sc_memrid, RF_ACTIVE);
345 
346 	sc->sc_mem_rman.rm_type = RMAN_ARRAY;
347 	sc->sc_mem_rman.rm_descr = "MacIO Device Memory";
348 	error = rman_init(&sc->sc_mem_rman);
349 	if (error) {
350 		device_printf(dev, "rman_init() failed. error = %d\n", error);
351 		return (error);
352 	}
353 	error = rman_manage_region(&sc->sc_mem_rman, 0, sc->sc_size);
354 	if (error) {
355 		device_printf(dev,
356 		    "rman_manage_region() failed. error = %d\n", error);
357 		return (error);
358 	}
359 
360 	/*
361 	 * Iterate through the sub-devices
362 	 */
363 	for (child = OF_child(root); child != 0; child = OF_peer(child)) {
364 		dinfo = malloc(sizeof(*dinfo), M_MACIO, M_WAITOK | M_ZERO);
365 		if (ofw_bus_gen_setup_devinfo(&dinfo->mdi_obdinfo, child) !=
366 		    0) {
367 			free(dinfo, M_MACIO);
368 			continue;
369 		}
370 		quirks = macio_get_quirks(dinfo->mdi_obdinfo.obd_name);
371 		if ((quirks & MACIO_QUIRK_IGNORE) != 0) {
372 			ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
373 			free(dinfo, M_MACIO);
374 			continue;
375 		}
376 		resource_list_init(&dinfo->mdi_resources);
377 		dinfo->mdi_ninterrupts = 0;
378 		macio_add_intr(child, dinfo);
379 		if ((quirks & MACIO_QUIRK_USE_CHILD_REG) != 0)
380 			macio_add_reg(OF_child(child), dinfo);
381 		else
382 			macio_add_reg(child, dinfo);
383 		if ((quirks & MACIO_QUIRK_CHILD_HAS_INTR) != 0)
384 			for (subchild = OF_child(child); subchild != 0;
385 			    subchild = OF_peer(subchild))
386 				macio_add_intr(subchild, dinfo);
387 		cdev = device_add_child(dev, NULL, -1);
388 		if (cdev == NULL) {
389 			device_printf(dev, "<%s>: device_add_child failed\n",
390 			    dinfo->mdi_obdinfo.obd_name);
391 			resource_list_free(&dinfo->mdi_resources);
392 			ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
393 			free(dinfo, M_MACIO);
394 			continue;
395 		}
396 		device_set_ivars(cdev, dinfo);
397 
398 		/* Set FCRs to enable some devices */
399 		if (sc->sc_memr == NULL)
400 			continue;
401 
402 		if (strcmp(ofw_bus_get_name(cdev), "bmac") == 0 ||
403 		    (ofw_bus_get_compat(cdev) != NULL &&
404 		    strcmp(ofw_bus_get_compat(cdev), "bmac+") == 0)) {
405 			uint32_t fcr;
406 
407 			fcr = bus_read_4(sc->sc_memr, HEATHROW_FCR);
408 
409 			fcr |= FCR_ENET_ENABLE & ~FCR_ENET_RESET;
410 			bus_write_4(sc->sc_memr, HEATHROW_FCR, fcr);
411 			DELAY(50000);
412 			fcr |= FCR_ENET_RESET;
413 			bus_write_4(sc->sc_memr, HEATHROW_FCR, fcr);
414 			DELAY(50000);
415 			fcr &= ~FCR_ENET_RESET;
416 			bus_write_4(sc->sc_memr, HEATHROW_FCR, fcr);
417 			DELAY(50000);
418 
419 			bus_write_4(sc->sc_memr, HEATHROW_FCR, fcr);
420 		}
421 
422 		/*
423 		 * Make sure the I2S0 and the I2S0_CLK are enabled.
424 		 * On certain G5's they are not.
425 		 */
426 		if ((strcmp(ofw_bus_get_name(cdev), "i2s") == 0) &&
427 		    (strcmp(compat, "K2-Keylargo") == 0)) {
428 
429 			uint32_t fcr1;
430 
431 			fcr1 = bus_read_4(sc->sc_memr, KEYLARGO_FCR1);
432 			fcr1 |= FCR1_I2S0_CLK_ENABLE | FCR1_I2S0_ENABLE;
433 			bus_write_4(sc->sc_memr, KEYLARGO_FCR1, fcr1);
434 		}
435 
436 	}
437 
438 	return (bus_generic_attach(dev));
439 }
440 
441 
442 static int
443 macio_print_child(device_t dev, device_t child)
444 {
445         struct macio_devinfo *dinfo;
446         struct resource_list *rl;
447         int retval = 0;
448 
449         dinfo = device_get_ivars(child);
450         rl = &dinfo->mdi_resources;
451 
452         retval += bus_print_child_header(dev, child);
453 
454         retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#jx");
455         retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
456 
457         retval += bus_print_child_footer(dev, child);
458 
459         return (retval);
460 }
461 
462 
463 static void
464 macio_probe_nomatch(device_t dev, device_t child)
465 {
466         struct macio_devinfo *dinfo;
467         struct resource_list *rl;
468 	const char *type;
469 
470 	if (bootverbose) {
471 		dinfo = device_get_ivars(child);
472 		rl = &dinfo->mdi_resources;
473 
474 		if ((type = ofw_bus_get_type(child)) == NULL)
475 			type = "(unknown)";
476 		device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child));
477 		resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#jx");
478 		resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
479 		printf(" (no driver attached)\n");
480 	}
481 }
482 
483 
484 static struct resource *
485 macio_alloc_resource(device_t bus, device_t child, int type, int *rid,
486 		     rman_res_t start, rman_res_t end, rman_res_t count,
487 		     u_int flags)
488 {
489 	struct		macio_softc *sc;
490 	int		needactivate;
491 	struct		resource *rv;
492 	struct		rman *rm;
493 	u_long		adjstart, adjend, adjcount;
494 	struct		macio_devinfo *dinfo;
495 	struct		resource_list_entry *rle;
496 
497 	sc = device_get_softc(bus);
498 	dinfo = device_get_ivars(child);
499 
500 	needactivate = flags & RF_ACTIVE;
501 	flags &= ~RF_ACTIVE;
502 
503 	switch (type) {
504 	case SYS_RES_MEMORY:
505 	case SYS_RES_IOPORT:
506 		rle = resource_list_find(&dinfo->mdi_resources, SYS_RES_MEMORY,
507 		    *rid);
508 		if (rle == NULL) {
509 			device_printf(bus, "no rle for %s memory %d\n",
510 			    device_get_nameunit(child), *rid);
511 			return (NULL);
512 		}
513 
514 		if (start < rle->start)
515 			adjstart = rle->start;
516 		else if (start > rle->end)
517 			adjstart = rle->end;
518 		else
519 			adjstart = start;
520 
521 		if (end < rle->start)
522 			adjend = rle->start;
523 		else if (end > rle->end)
524 			adjend = rle->end;
525 		else
526 			adjend = end;
527 
528 		adjcount = adjend - adjstart;
529 
530 		rm = &sc->sc_mem_rman;
531 		break;
532 
533 	case SYS_RES_IRQ:
534 		/* Check for passthrough from subattachments like macgpio */
535 		if (device_get_parent(child) != bus)
536 			return BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
537 			    type, rid, start, end, count, flags);
538 
539 		rle = resource_list_find(&dinfo->mdi_resources, SYS_RES_IRQ,
540 		    *rid);
541 		if (rle == NULL) {
542 			if (dinfo->mdi_ninterrupts >= 6) {
543 				device_printf(bus,
544 				    "%s has more than 6 interrupts\n",
545 				    device_get_nameunit(child));
546 				return (NULL);
547 			}
548 			resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ,
549 			    dinfo->mdi_ninterrupts, start, start, 1);
550 
551 			dinfo->mdi_interrupts[dinfo->mdi_ninterrupts] = start;
552 			dinfo->mdi_ninterrupts++;
553 		}
554 
555 		return (resource_list_alloc(&dinfo->mdi_resources, bus, child,
556 		    type, rid, start, end, count, flags));
557 
558 	default:
559 		device_printf(bus, "unknown resource request from %s\n",
560 			      device_get_nameunit(child));
561 		return (NULL);
562 	}
563 
564 	rv = rman_reserve_resource(rm, adjstart, adjend, adjcount, flags,
565 	    child);
566 	if (rv == NULL) {
567 		device_printf(bus,
568 		    "failed to reserve resource %#lx - %#lx (%#lx) for %s\n",
569 		    adjstart, adjend, adjcount, device_get_nameunit(child));
570 		return (NULL);
571 	}
572 
573 	rman_set_rid(rv, *rid);
574 
575 	if (needactivate) {
576 		if (bus_activate_resource(child, type, *rid, rv) != 0) {
577                         device_printf(bus,
578 				      "failed to activate resource for %s\n",
579 				      device_get_nameunit(child));
580 			rman_release_resource(rv);
581 			return (NULL);
582                 }
583         }
584 
585 	return (rv);
586 }
587 
588 
589 static int
590 macio_release_resource(device_t bus, device_t child, int type, int rid,
591 		       struct resource *res)
592 {
593 	if (rman_get_flags(res) & RF_ACTIVE) {
594 		int error = bus_deactivate_resource(child, type, rid, res);
595 		if (error)
596 			return error;
597 	}
598 
599 	return (rman_release_resource(res));
600 }
601 
602 
603 static int
604 macio_activate_resource(device_t bus, device_t child, int type, int rid,
605 			   struct resource *res)
606 {
607 	struct macio_softc *sc;
608 	void    *p;
609 
610 	sc = device_get_softc(bus);
611 
612 	if (type == SYS_RES_IRQ)
613                 return (bus_activate_resource(bus, type, rid, res));
614 
615 	if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
616 		p = pmap_mapdev((vm_offset_t)rman_get_start(res) + sc->sc_base,
617 				(vm_size_t)rman_get_size(res));
618 		if (p == NULL)
619 			return (ENOMEM);
620 		rman_set_virtual(res, p);
621 		rman_set_bustag(res, &bs_le_tag);
622 		rman_set_bushandle(res, (u_long)p);
623 	}
624 
625 	return (rman_activate_resource(res));
626 }
627 
628 
629 static int
630 macio_deactivate_resource(device_t bus, device_t child, int type, int rid,
631 			  struct resource *res)
632 {
633         /*
634          * If this is a memory resource, unmap it.
635          */
636         if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
637 		u_int32_t psize;
638 
639 		psize = rman_get_size(res);
640 		pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
641 	}
642 
643 	return (rman_deactivate_resource(res));
644 }
645 
646 
647 static struct resource_list *
648 macio_get_resource_list (device_t dev, device_t child)
649 {
650 	struct macio_devinfo *dinfo;
651 
652 	dinfo = device_get_ivars(child);
653 	return (&dinfo->mdi_resources);
654 }
655 
656 static const struct ofw_bus_devinfo *
657 macio_get_devinfo(device_t dev, device_t child)
658 {
659 	struct macio_devinfo *dinfo;
660 
661 	dinfo = device_get_ivars(child);
662 	return (&dinfo->mdi_obdinfo);
663 }
664 
665 int
666 macio_enable_wireless(device_t dev, bool enable)
667 {
668 	struct macio_softc *sc = device_get_softc(dev);
669 	uint32_t x;
670 
671 	if (enable) {
672 		x = bus_read_4(sc->sc_memr, KEYLARGO_FCR2);
673 		x |= 0x4;
674 		bus_write_4(sc->sc_memr, KEYLARGO_FCR2, x);
675 
676 		/* Enable card slot. */
677 		bus_write_1(sc->sc_memr, KEYLARGO_GPIO_BASE + 0x0f, 5);
678 		DELAY(1000);
679 		bus_write_1(sc->sc_memr, KEYLARGO_GPIO_BASE + 0x0f, 4);
680 		DELAY(1000);
681 		x = bus_read_4(sc->sc_memr, KEYLARGO_FCR2);
682 		x &= ~0x80000000;
683 
684 		bus_write_4(sc->sc_memr, KEYLARGO_FCR2, x);
685 		/* out8(gpio + 0x10, 4); */
686 
687 		bus_write_1(sc->sc_memr, KEYLARGO_EXTINT_GPIO_REG_BASE + 0x0b, 0);
688 		bus_write_1(sc->sc_memr, KEYLARGO_EXTINT_GPIO_REG_BASE + 0x0a, 0x28);
689 		bus_write_1(sc->sc_memr, KEYLARGO_EXTINT_GPIO_REG_BASE + 0x0d, 0x28);
690 		bus_write_1(sc->sc_memr, KEYLARGO_GPIO_BASE + 0x0d, 0x28);
691 		bus_write_1(sc->sc_memr, KEYLARGO_GPIO_BASE + 0x0e, 0x28);
692 		bus_write_4(sc->sc_memr, 0x1c000, 0);
693 
694 		/* Initialize the card. */
695 		bus_write_4(sc->sc_memr, 0x1a3e0, 0x41);
696 		x = bus_read_4(sc->sc_memr, KEYLARGO_FCR2);
697 		x |= 0x80000000;
698 		bus_write_4(sc->sc_memr, KEYLARGO_FCR2, x);
699 	} else {
700 		x = bus_read_4(sc->sc_memr, KEYLARGO_FCR2);
701 		x &= ~0x4;
702 		bus_write_4(sc->sc_memr, KEYLARGO_FCR2, x);
703 		/* out8(gpio + 0x10, 0); */
704 	}
705 
706 	return (0);
707 }
708