xref: /freebsd/sys/powerpc/powermac/macio.c (revision 10b9d77bf1ccf2f3affafa6261692cb92cf7e992)
1 /*-
2  * Copyright 2002 by Peter Grehan. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
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  * $FreeBSD$
28  */
29 
30 /*
31  * Driver for KeyLargo/Pangea, the MacPPC south bridge ASIC.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/bus.h>
40 #include <sys/rman.h>
41 
42 #include <vm/vm.h>
43 #include <vm/pmap.h>
44 
45 #include <machine/bus.h>
46 #include <machine/intr_machdep.h>
47 #include <machine/pmap.h>
48 #include <machine/resource.h>
49 #include <machine/vmparam.h>
50 
51 #include <dev/ofw/ofw_bus.h>
52 #include <dev/ofw/ofw_bus_subr.h>
53 #include <dev/ofw/openfirm.h>
54 
55 #include <powerpc/powermac/maciovar.h>
56 
57 #include <dev/pci/pcivar.h>
58 #include <dev/pci/pcireg.h>
59 
60 /*
61  * Macio softc
62  */
63 struct macio_softc {
64 	phandle_t    sc_node;
65 	vm_offset_t  sc_base;
66 	vm_offset_t  sc_size;
67 	struct rman  sc_mem_rman;
68 };
69 
70 static MALLOC_DEFINE(M_MACIO, "macio", "macio device information");
71 
72 static int  macio_probe(device_t);
73 static int  macio_attach(device_t);
74 static int  macio_print_child(device_t dev, device_t child);
75 static void macio_probe_nomatch(device_t, device_t);
76 static struct   resource *macio_alloc_resource(device_t, device_t, int, int *,
77 					       u_long, u_long, u_long, u_int);
78 static int  macio_activate_resource(device_t, device_t, int, int,
79 				    struct resource *);
80 static int  macio_deactivate_resource(device_t, device_t, int, int,
81 				      struct resource *);
82 static int  macio_release_resource(device_t, device_t, int, int,
83 				   struct resource *);
84 static struct resource_list *macio_get_resource_list (device_t, device_t);
85 static ofw_bus_get_devinfo_t macio_get_devinfo;
86 
87 /*
88  * Bus interface definition
89  */
90 static device_method_t macio_methods[] = {
91 	/* Device interface */
92 	DEVMETHOD(device_probe,         macio_probe),
93 	DEVMETHOD(device_attach,        macio_attach),
94 	DEVMETHOD(device_detach,        bus_generic_detach),
95 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
96 	DEVMETHOD(device_suspend,       bus_generic_suspend),
97 	DEVMETHOD(device_resume,        bus_generic_resume),
98 
99 	/* Bus interface */
100 	DEVMETHOD(bus_print_child,      macio_print_child),
101 	DEVMETHOD(bus_probe_nomatch,    macio_probe_nomatch),
102 	DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
103 	DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
104 
105         DEVMETHOD(bus_alloc_resource,   macio_alloc_resource),
106         DEVMETHOD(bus_release_resource, macio_release_resource),
107         DEVMETHOD(bus_activate_resource, macio_activate_resource),
108         DEVMETHOD(bus_deactivate_resource, macio_deactivate_resource),
109         DEVMETHOD(bus_get_resource_list, macio_get_resource_list),
110 
111 	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
112 
113 	/* ofw_bus interface */
114 	DEVMETHOD(ofw_bus_get_devinfo,	macio_get_devinfo),
115 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
116 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
117 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
118 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
119 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
120 
121 	{ 0, 0 }
122 };
123 
124 static driver_t macio_pci_driver = {
125         "macio",
126         macio_methods,
127 	sizeof(struct macio_softc)
128 };
129 
130 devclass_t macio_devclass;
131 
132 DRIVER_MODULE(macio, pci, macio_pci_driver, macio_devclass, 0, 0);
133 
134 /*
135  * PCI ID search table
136  */
137 static struct macio_pci_dev {
138         u_int32_t  mpd_devid;
139 	char    *mpd_desc;
140 } macio_pci_devlist[] = {
141 	{ 0x0017106b, "Paddington I/O Controller" },
142 	{ 0x0022106b, "KeyLargo I/O Controller" },
143 	{ 0x0025106b, "Pangea I/O Controller" },
144 	{ 0x003e106b, "Intrepid I/O Controller" },
145 	{ 0x0041106b, "K2 KeyLargo I/O Controller" },
146 	{ 0x004f106b, "Shasta I/O Controller" },
147 	{ 0, NULL }
148 };
149 
150 /*
151  * Devices to exclude from the probe
152  * XXX some of these may be required in the future...
153  */
154 #define	MACIO_QUIRK_IGNORE		0x00000001
155 #define	MACIO_QUIRK_CHILD_HAS_INTR	0x00000002
156 #define	MACIO_QUIRK_USE_CHILD_REG	0x00000004
157 
158 struct macio_quirk_entry {
159 	const char	*mq_name;
160 	int		mq_quirks;
161 };
162 
163 static struct macio_quirk_entry macio_quirks[] = {
164 	{ "escc-legacy",		MACIO_QUIRK_IGNORE },
165 	{ "timer",			MACIO_QUIRK_IGNORE },
166 	{ "escc",			MACIO_QUIRK_CHILD_HAS_INTR },
167         { "i2s", 			MACIO_QUIRK_CHILD_HAS_INTR |
168 					MACIO_QUIRK_USE_CHILD_REG },
169 	{ NULL,				0 }
170 };
171 
172 static int
173 macio_get_quirks(const char *name)
174 {
175         struct	macio_quirk_entry *mqe;
176 
177         for (mqe = macio_quirks; mqe->mq_name != NULL; mqe++)
178                 if (strcmp(name, mqe->mq_name) == 0)
179                         return (mqe->mq_quirks);
180         return (0);
181 }
182 
183 
184 /*
185  * Add an interrupt to the dev's resource list if present
186  */
187 static void
188 macio_add_intr(phandle_t devnode, struct macio_devinfo *dinfo)
189 {
190 	phandle_t iparent;
191 	int	*intr;
192 	int	i, nintr;
193 	int 	icells;
194 
195 	if (dinfo->mdi_ninterrupts >= 6) {
196 		printf("macio: device has more than 6 interrupts\n");
197 		return;
198 	}
199 
200 	if (OF_searchprop(devnode, "#interrupt-cells", &icells, sizeof(icells))
201 	    <= 0)
202 		icells = 1;
203 
204 	nintr = OF_getprop_alloc(devnode, "interrupts", sizeof(*intr),
205 		(void **)&intr);
206 	if (nintr == -1) {
207 		nintr = OF_getprop_alloc(devnode, "AAPL,interrupts",
208 			sizeof(*intr), (void **)&intr);
209 		if (nintr == -1)
210 			return;
211 	}
212 
213 	if (intr[0] == -1)
214 		return;
215 
216 	if (OF_getprop(devnode, "interrupt-parent", &iparent, sizeof(iparent))
217 	    <= 0)
218 		panic("Interrupt but no interrupt parent!\n");
219 
220 	for (i = 0; i < nintr; i+=icells) {
221 		u_int irq = MAP_IRQ(iparent, intr[i]);
222 
223 		resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ,
224 		    dinfo->mdi_ninterrupts, irq, irq, 1);
225 
226 		dinfo->mdi_interrupts[dinfo->mdi_ninterrupts] = irq;
227 		dinfo->mdi_ninterrupts++;
228 	}
229 }
230 
231 
232 static void
233 macio_add_reg(phandle_t devnode, struct macio_devinfo *dinfo)
234 {
235 	struct	macio_reg *reg;
236 	int	i, nreg;
237 
238 	nreg = OF_getprop_alloc(devnode, "reg", sizeof(*reg), (void **)&reg);
239 	if (nreg == -1)
240 		return;
241 
242 	for (i = 0; i < nreg; i++) {
243 		resource_list_add(&dinfo->mdi_resources, SYS_RES_MEMORY, i,
244 		    reg[i].mr_base, reg[i].mr_base + reg[i].mr_size,
245 		    reg[i].mr_size);
246 	}
247 }
248 
249 /*
250  * PCI probe
251  */
252 static int
253 macio_probe(device_t dev)
254 {
255         int i;
256         u_int32_t devid;
257 
258         devid = pci_get_devid(dev);
259         for (i = 0; macio_pci_devlist[i].mpd_desc != NULL; i++) {
260                 if (devid == macio_pci_devlist[i].mpd_devid) {
261                         device_set_desc(dev, macio_pci_devlist[i].mpd_desc);
262                         return (0);
263                 }
264         }
265 
266         return (ENXIO);
267 }
268 
269 /*
270  * PCI attach: scan Open Firmware child nodes, and attach these as children
271  * of the macio bus
272  */
273 static int
274 macio_attach(device_t dev)
275 {
276 	struct macio_softc *sc;
277         struct macio_devinfo *dinfo;
278         phandle_t  root;
279 	phandle_t  child;
280 	phandle_t  subchild;
281         device_t cdev;
282         u_int reg[3];
283 	int error, quirks;
284 
285 	sc = device_get_softc(dev);
286 	root = sc->sc_node = ofw_bus_get_node(dev);
287 
288 	/*
289 	 * Locate the device node and it's base address
290 	 */
291 	if (OF_getprop(root, "assigned-addresses",
292 		       reg, sizeof(reg)) < sizeof(reg)) {
293 		return (ENXIO);
294 	}
295 
296 	sc->sc_base = reg[2];
297 	sc->sc_size = MACIO_REG_SIZE;
298 
299 	sc->sc_mem_rman.rm_type = RMAN_ARRAY;
300 	sc->sc_mem_rman.rm_descr = "MacIO Device Memory";
301 	error = rman_init(&sc->sc_mem_rman);
302 	if (error) {
303 		device_printf(dev, "rman_init() failed. error = %d\n", error);
304 		return (error);
305 	}
306 	error = rman_manage_region(&sc->sc_mem_rman, 0, sc->sc_size);
307 	if (error) {
308 		device_printf(dev,
309 		    "rman_manage_region() failed. error = %d\n", error);
310 		return (error);
311 	}
312 
313 	/*
314 	 * Iterate through the sub-devices
315 	 */
316 	for (child = OF_child(root); child != 0; child = OF_peer(child)) {
317 		dinfo = malloc(sizeof(*dinfo), M_MACIO, M_WAITOK | M_ZERO);
318 		if (ofw_bus_gen_setup_devinfo(&dinfo->mdi_obdinfo, child) !=
319 		    0) {
320 			free(dinfo, M_MACIO);
321 			continue;
322 		}
323 		quirks = macio_get_quirks(dinfo->mdi_obdinfo.obd_name);
324 		if ((quirks & MACIO_QUIRK_IGNORE) != 0) {
325 			ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
326 			free(dinfo, M_MACIO);
327 			continue;
328 		}
329 		resource_list_init(&dinfo->mdi_resources);
330 		dinfo->mdi_ninterrupts = 0;
331 		macio_add_intr(child, dinfo);
332 		if ((quirks & MACIO_QUIRK_USE_CHILD_REG) != 0)
333 			macio_add_reg(OF_child(child), dinfo);
334 		else
335 			macio_add_reg(child, dinfo);
336 		if ((quirks & MACIO_QUIRK_CHILD_HAS_INTR) != 0)
337 			for (subchild = OF_child(child); subchild != 0;
338 			    subchild = OF_peer(subchild))
339 				macio_add_intr(subchild, dinfo);
340 		cdev = device_add_child(dev, NULL, -1);
341 		if (cdev == NULL) {
342 			device_printf(dev, "<%s>: device_add_child failed\n",
343 			    dinfo->mdi_obdinfo.obd_name);
344 			resource_list_free(&dinfo->mdi_resources);
345 			ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
346 			free(dinfo, M_MACIO);
347 			continue;
348 		}
349 		device_set_ivars(cdev, dinfo);
350 	}
351 
352 	return (bus_generic_attach(dev));
353 }
354 
355 
356 static int
357 macio_print_child(device_t dev, device_t child)
358 {
359         struct macio_devinfo *dinfo;
360         struct resource_list *rl;
361         int retval = 0;
362 
363         dinfo = device_get_ivars(child);
364         rl = &dinfo->mdi_resources;
365 
366         retval += bus_print_child_header(dev, child);
367 
368         retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
369         retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
370 
371         retval += bus_print_child_footer(dev, child);
372 
373         return (retval);
374 }
375 
376 
377 static void
378 macio_probe_nomatch(device_t dev, device_t child)
379 {
380         struct macio_devinfo *dinfo;
381         struct resource_list *rl;
382 	const char *type;
383 
384 	if (bootverbose) {
385 		dinfo = device_get_ivars(child);
386 		rl = &dinfo->mdi_resources;
387 
388 		if ((type = ofw_bus_get_type(child)) == NULL)
389 			type = "(unknown)";
390 		device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child));
391 		resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
392 		resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
393 		printf(" (no driver attached)\n");
394 	}
395 }
396 
397 
398 static struct resource *
399 macio_alloc_resource(device_t bus, device_t child, int type, int *rid,
400 		     u_long start, u_long end, u_long count, u_int flags)
401 {
402 	struct		macio_softc *sc;
403 	int		needactivate;
404 	struct		resource *rv;
405 	struct		rman *rm;
406 	u_long		adjstart, adjend, adjcount;
407 	struct		macio_devinfo *dinfo;
408 	struct		resource_list_entry *rle;
409 
410 	sc = device_get_softc(bus);
411 	dinfo = device_get_ivars(child);
412 
413 	needactivate = flags & RF_ACTIVE;
414 	flags &= ~RF_ACTIVE;
415 
416 	switch (type) {
417 	case SYS_RES_MEMORY:
418 	case SYS_RES_IOPORT:
419 		rle = resource_list_find(&dinfo->mdi_resources, SYS_RES_MEMORY,
420 		    *rid);
421 		if (rle == NULL) {
422 			device_printf(bus, "no rle for %s memory %d\n",
423 			    device_get_nameunit(child), *rid);
424 			return (NULL);
425 		}
426 
427 		if (start < rle->start)
428 			adjstart = rle->start;
429 		else if (start > rle->end)
430 			adjstart = rle->end;
431 		else
432 			adjstart = start;
433 
434 		if (end < rle->start)
435 			adjend = rle->start;
436 		else if (end > rle->end)
437 			adjend = rle->end;
438 		else
439 			adjend = end;
440 
441 		adjcount = adjend - adjstart;
442 
443 		rm = &sc->sc_mem_rman;
444 		break;
445 
446 	case SYS_RES_IRQ:
447 		/* Check for passthrough from subattachments like macgpio */
448 		if (device_get_parent(child) != bus)
449 			return BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
450 			    type, rid, start, end, count, flags);
451 
452 		rle = resource_list_find(&dinfo->mdi_resources, SYS_RES_IRQ,
453 		    *rid);
454 		if (rle == NULL) {
455 			if (dinfo->mdi_ninterrupts >= 6) {
456 				device_printf(bus,
457 				    "%s has more than 6 interrupts\n",
458 				    device_get_nameunit(child));
459 				return (NULL);
460 			}
461 			resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ,
462 			    dinfo->mdi_ninterrupts, start, start, 1);
463 
464 			dinfo->mdi_interrupts[dinfo->mdi_ninterrupts] = start;
465 			dinfo->mdi_ninterrupts++;
466 		}
467 
468 		return (resource_list_alloc(&dinfo->mdi_resources, bus, child,
469 		    type, rid, start, end, count, flags));
470 
471 	default:
472 		device_printf(bus, "unknown resource request from %s\n",
473 			      device_get_nameunit(child));
474 		return (NULL);
475 	}
476 
477 	rv = rman_reserve_resource(rm, adjstart, adjend, adjcount, flags,
478 	    child);
479 	if (rv == NULL) {
480 		device_printf(bus,
481 		    "failed to reserve resource %#lx - %#lx (%#lx) for %s\n",
482 		    adjstart, adjend, adjcount, device_get_nameunit(child));
483 		return (NULL);
484 	}
485 
486 	rman_set_rid(rv, *rid);
487 
488 	if (needactivate) {
489 		if (bus_activate_resource(child, type, *rid, rv) != 0) {
490                         device_printf(bus,
491 				      "failed to activate resource for %s\n",
492 				      device_get_nameunit(child));
493 			rman_release_resource(rv);
494 			return (NULL);
495                 }
496         }
497 
498 	return (rv);
499 }
500 
501 
502 static int
503 macio_release_resource(device_t bus, device_t child, int type, int rid,
504 		       struct resource *res)
505 {
506 	if (rman_get_flags(res) & RF_ACTIVE) {
507 		int error = bus_deactivate_resource(child, type, rid, res);
508 		if (error)
509 			return error;
510 	}
511 
512 	return (rman_release_resource(res));
513 }
514 
515 
516 static int
517 macio_activate_resource(device_t bus, device_t child, int type, int rid,
518 			   struct resource *res)
519 {
520 	struct macio_softc *sc;
521 	void    *p;
522 
523 	sc = device_get_softc(bus);
524 
525 	if (type == SYS_RES_IRQ)
526                 return (bus_activate_resource(bus, type, rid, res));
527 
528 	if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
529 		p = pmap_mapdev((vm_offset_t)rman_get_start(res) + sc->sc_base,
530 				(vm_size_t)rman_get_size(res));
531 		if (p == NULL)
532 			return (ENOMEM);
533 		rman_set_virtual(res, p);
534 		rman_set_bustag(res, &bs_le_tag);
535 		rman_set_bushandle(res, (u_long)p);
536 	}
537 
538 	return (rman_activate_resource(res));
539 }
540 
541 
542 static int
543 macio_deactivate_resource(device_t bus, device_t child, int type, int rid,
544 			  struct resource *res)
545 {
546         /*
547          * If this is a memory resource, unmap it.
548          */
549         if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
550 		u_int32_t psize;
551 
552 		psize = rman_get_size(res);
553 		pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
554 	}
555 
556 	return (rman_deactivate_resource(res));
557 }
558 
559 
560 static struct resource_list *
561 macio_get_resource_list (device_t dev, device_t child)
562 {
563 	struct macio_devinfo *dinfo;
564 
565 	dinfo = device_get_ivars(child);
566 	return (&dinfo->mdi_resources);
567 }
568 
569 static const struct ofw_bus_devinfo *
570 macio_get_devinfo(device_t dev, device_t child)
571 {
572 	struct macio_devinfo *dinfo;
573 
574 	dinfo = device_get_ivars(child);
575 	return (&dinfo->mdi_obdinfo);
576 }
577