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