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