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