xref: /freebsd/sys/powerpc/powermac/uninorth.c (revision a18eacbefdfa1085ca3db829e86ece78cd416493)
1 /*-
2  * Copyright (C) 2002 Benno Rice.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/module.h>
31 #include <sys/bus.h>
32 #include <sys/conf.h>
33 #include <sys/kernel.h>
34 
35 #include <dev/ofw/openfirm.h>
36 #include <dev/ofw/ofw_pci.h>
37 #include <dev/ofw/ofw_bus.h>
38 #include <dev/ofw/ofw_bus_subr.h>
39 
40 #include <dev/pci/pcivar.h>
41 #include <dev/pci/pcireg.h>
42 
43 #include <machine/bus.h>
44 #include <machine/intr_machdep.h>
45 #include <machine/md_var.h>
46 #include <machine/pio.h>
47 #include <machine/resource.h>
48 
49 #include <sys/rman.h>
50 
51 #include <powerpc/powermac/uninorthvar.h>
52 
53 #include <vm/vm.h>
54 #include <vm/pmap.h>
55 
56 /*
57  * Driver for the Uninorth chip itself.
58  */
59 
60 static MALLOC_DEFINE(M_UNIN, "unin", "unin device information");
61 
62 /*
63  * Device interface.
64  */
65 
66 static int  unin_chip_probe(device_t);
67 static int  unin_chip_attach(device_t);
68 
69 /*
70  * Bus interface.
71  */
72 static int  unin_chip_print_child(device_t dev, device_t child);
73 static void unin_chip_probe_nomatch(device_t, device_t);
74 static struct resource *unin_chip_alloc_resource(device_t, device_t, int, int *,
75 						 u_long, u_long, u_long, u_int);
76 static int  unin_chip_activate_resource(device_t, device_t, int, int,
77 					struct resource *);
78 static int  unin_chip_deactivate_resource(device_t, device_t, int, int,
79 					  struct resource *);
80 static int  unin_chip_release_resource(device_t, device_t, int, int,
81 				       struct resource *);
82 static struct resource_list *unin_chip_get_resource_list (device_t, device_t);
83 
84 /*
85  * OFW Bus interface
86  */
87 
88 static ofw_bus_get_devinfo_t unin_chip_get_devinfo;
89 
90 /*
91  * Local routines
92  */
93 
94 static void		unin_enable_gmac(device_t dev);
95 static void		unin_enable_mpic(device_t dev);
96 
97 /*
98  * Driver methods.
99  */
100 static device_method_t unin_chip_methods[] = {
101 
102 	/* Device interface */
103 	DEVMETHOD(device_probe,         unin_chip_probe),
104 	DEVMETHOD(device_attach,        unin_chip_attach),
105 
106 	/* Bus interface */
107 	DEVMETHOD(bus_print_child,      unin_chip_print_child),
108 	DEVMETHOD(bus_probe_nomatch,    unin_chip_probe_nomatch),
109 	DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
110 	DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
111 
112 	DEVMETHOD(bus_alloc_resource,   unin_chip_alloc_resource),
113 	DEVMETHOD(bus_release_resource, unin_chip_release_resource),
114 	DEVMETHOD(bus_activate_resource, unin_chip_activate_resource),
115 	DEVMETHOD(bus_deactivate_resource, unin_chip_deactivate_resource),
116 	DEVMETHOD(bus_get_resource_list, unin_chip_get_resource_list),
117 
118 	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
119 
120         /* ofw_bus interface */
121 	DEVMETHOD(ofw_bus_get_devinfo,	unin_chip_get_devinfo),
122 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
123 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
124 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
125 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
126 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
127 
128 	{ 0, 0 }
129 };
130 
131 static driver_t	unin_chip_driver = {
132 	"unin",
133 	unin_chip_methods,
134 	sizeof(struct unin_chip_softc)
135 };
136 
137 static devclass_t	unin_chip_devclass;
138 
139 DRIVER_MODULE(unin, nexus, unin_chip_driver, unin_chip_devclass, 0, 0);
140 
141 /*
142  * Add an interrupt to the dev's resource list if present
143  */
144 static void
145 unin_chip_add_intr(phandle_t devnode, struct unin_chip_devinfo *dinfo)
146 {
147 	phandle_t iparent;
148 	int	*intr;
149 	int	i, nintr;
150 	int 	icells;
151 
152 	if (dinfo->udi_ninterrupts >= 6) {
153 		printf("unin: device has more than 6 interrupts\n");
154 		return;
155 	}
156 
157 	nintr = OF_getprop_alloc(devnode, "interrupts", sizeof(*intr),
158 		(void **)&intr);
159 	if (nintr == -1) {
160 		nintr = OF_getprop_alloc(devnode, "AAPL,interrupts",
161 			sizeof(*intr), (void **)&intr);
162 		if (nintr == -1)
163 			return;
164 	}
165 
166 	if (intr[0] == -1)
167 		return;
168 
169 	if (OF_getprop(devnode, "interrupt-parent", &iparent, sizeof(iparent))
170 	    <= 0)
171 		panic("Interrupt but no interrupt parent!\n");
172 
173 	if (OF_searchprop(iparent, "#interrupt-cells", &icells, sizeof(icells))
174 	    <= 0)
175 		icells = 1;
176 
177 	for (i = 0; i < nintr; i+=icells) {
178 		u_int irq = MAP_IRQ(iparent, intr[i]);
179 
180 		resource_list_add(&dinfo->udi_resources, SYS_RES_IRQ,
181 		    dinfo->udi_ninterrupts, irq, irq, 1);
182 
183 		if (icells > 1) {
184 			powerpc_config_intr(irq,
185 			    (intr[i+1] & 1) ? INTR_TRIGGER_LEVEL :
186 			    INTR_TRIGGER_EDGE, INTR_POLARITY_LOW);
187 		}
188 
189 		dinfo->udi_interrupts[dinfo->udi_ninterrupts] = irq;
190 		dinfo->udi_ninterrupts++;
191 	}
192 }
193 
194 static void
195 unin_chip_add_reg(phandle_t devnode, struct unin_chip_devinfo *dinfo)
196 {
197 	struct	unin_chip_reg *reg;
198 	int	i, nreg;
199 
200 	nreg = OF_getprop_alloc(devnode, "reg", sizeof(*reg), (void **)&reg);
201 	if (nreg == -1)
202 		return;
203 
204 	for (i = 0; i < nreg; i++) {
205 		resource_list_add(&dinfo->udi_resources, SYS_RES_MEMORY, i,
206 				  reg[i].mr_base,
207 				  reg[i].mr_base + reg[i].mr_size,
208 				  reg[i].mr_size);
209 	}
210 }
211 
212 static void
213 unin_enable_gmac(device_t dev)
214 {
215 	volatile u_int *clkreg;
216 	struct unin_chip_softc *sc;
217 	u_int32_t tmpl;
218 
219 	sc = device_get_softc(dev);
220 	clkreg = (void *)(sc->sc_addr + UNIN_CLOCKCNTL);
221 	tmpl = inl(clkreg);
222 	tmpl |= UNIN_CLOCKCNTL_GMAC;
223 	outl(clkreg, tmpl);
224 }
225 
226 static void
227 unin_enable_mpic(device_t dev)
228 {
229 	volatile u_int *toggle;
230 	struct unin_chip_softc *sc;
231 	u_int32_t tmpl;
232 
233 	sc = device_get_softc(dev);
234 	toggle = (void *)(sc->sc_addr + UNIN_TOGGLE_REG);
235 	tmpl = inl(toggle);
236 	tmpl |= UNIN_MPIC_RESET | UNIN_MPIC_OUTPUT_ENABLE;
237 	outl(toggle, tmpl);
238 }
239 
240 static int
241 unin_chip_probe(device_t dev)
242 {
243 	const char	*name;
244 
245 	name = ofw_bus_get_name(dev);
246 
247 	if (name == NULL)
248 		return (ENXIO);
249 
250 	if (strcmp(name, "uni-n") != 0 && strcmp(name, "u3") != 0
251 	    && strcmp(name, "u4") != 0)
252 		return (ENXIO);
253 
254 	device_set_desc(dev, "Apple UniNorth System Controller");
255 	return (0);
256 }
257 
258 static int
259 unin_chip_attach(device_t dev)
260 {
261 	struct unin_chip_softc *sc;
262 	struct unin_chip_devinfo *dinfo;
263 	phandle_t  root;
264 	phandle_t  child;
265 	phandle_t  iparent;
266 	device_t   cdev;
267 	cell_t     acells, scells;
268 	char compat[32];
269 	char name[32];
270 	u_int irq, reg[3];
271 	int error, i = 0;
272 
273 	sc = device_get_softc(dev);
274 	root = ofw_bus_get_node(dev);
275 
276 	if (OF_getprop(root, "reg", reg, sizeof(reg)) < 8)
277 		return (ENXIO);
278 
279 	acells = scells = 1;
280 	OF_getprop(OF_parent(root), "#address-cells", &acells, sizeof(acells));
281 	OF_getprop(OF_parent(root), "#size-cells", &scells, sizeof(scells));
282 
283 	i = 0;
284 	sc->sc_physaddr = reg[i++];
285 	if (acells == 2) {
286 		sc->sc_physaddr <<= 32;
287 		sc->sc_physaddr |= reg[i++];
288 	}
289 	sc->sc_size = reg[i++];
290 	if (scells == 2) {
291 		sc->sc_size <<= 32;
292 		sc->sc_size |= reg[i++];
293 	}
294 
295 	sc->sc_mem_rman.rm_type = RMAN_ARRAY;
296 	sc->sc_mem_rman.rm_descr = "UniNorth Device Memory";
297 
298 	error = rman_init(&sc->sc_mem_rman);
299 
300 	if (error) {
301 		device_printf(dev, "rman_init() failed. error = %d\n", error);
302 		return (error);
303 	}
304 
305 	error = rman_manage_region(&sc->sc_mem_rman, sc->sc_physaddr,
306 				   sc->sc_physaddr + sc->sc_size - 1);
307 	if (error) {
308 		device_printf(dev,
309 			      "rman_manage_region() failed. error = %d\n",
310 			      error);
311 		return (error);
312 	}
313 
314         /*
315 	 * Iterate through the sub-devices
316 	 */
317 	for (child = OF_child(root); child != 0; child = OF_peer(child)) {
318 		dinfo = malloc(sizeof(*dinfo), M_UNIN, M_WAITOK | M_ZERO);
319 		if (ofw_bus_gen_setup_devinfo(&dinfo->udi_obdinfo, child)
320 		    != 0)
321 		{
322 			free(dinfo, M_UNIN);
323 			continue;
324 		}
325 
326 		resource_list_init(&dinfo->udi_resources);
327 		dinfo->udi_ninterrupts = 0;
328 		unin_chip_add_intr(child, dinfo);
329 
330 		/*
331 		 * Some Apple machines do have a bug in OF, they miss
332 		 * the interrupt entries on the U3 I2C node. That means they
333 		 * do not have an entry with number of interrupts nor the
334 		 * entry of the interrupt parent handle.
335 		 * We define an interrupt and hardwire it to the /u3/mpic
336 		 * handle.
337 		 */
338 
339 		if (OF_getprop(child, "name", name, sizeof(name)) <= 0)
340 			device_printf(dev, "device has no name!\n");
341 		if (dinfo->udi_ninterrupts == 0 &&
342 		    (strcmp(name, "i2c-bus") == 0 ||
343 		     strcmp(name, "i2c")  == 0)) {
344 			if (OF_getprop(child, "interrupt-parent", &iparent,
345 				       sizeof(iparent)) <= 0) {
346 				iparent = OF_finddevice("/u3/mpic");
347 				device_printf(dev, "Set /u3/mpic as iparent!\n");
348 			}
349 			/* Add an interrupt number 0 to the parent. */
350 			irq = MAP_IRQ(iparent, 0);
351 			resource_list_add(&dinfo->udi_resources, SYS_RES_IRQ,
352 					  dinfo->udi_ninterrupts, irq, irq, 1);
353 			dinfo->udi_interrupts[dinfo->udi_ninterrupts] = irq;
354 			dinfo->udi_ninterrupts++;
355 		}
356 
357 		unin_chip_add_reg(child, dinfo);
358 
359 		cdev = device_add_child(dev, NULL, -1);
360 		if (cdev == NULL) {
361 			device_printf(dev, "<%s>: device_add_child failed\n",
362 				      dinfo->udi_obdinfo.obd_name);
363 			resource_list_free(&dinfo->udi_resources);
364 			ofw_bus_gen_destroy_devinfo(&dinfo->udi_obdinfo);
365 			free(dinfo, M_UNIN);
366 			continue;
367 		}
368 
369 		device_set_ivars(cdev, dinfo);
370 	}
371 
372 	/*
373 	 * Only map the first page, since that is where the registers
374 	 * of interest lie.
375 	 */
376 	sc->sc_addr = (vm_offset_t)pmap_mapdev(sc->sc_physaddr, PAGE_SIZE);
377 
378 	sc->sc_version = *(u_int *)sc->sc_addr;
379 	device_printf(dev, "Version %d\n", sc->sc_version);
380 
381 	/*
382 	 * Enable the GMAC Ethernet cell and the integrated OpenPIC
383 	 * if Open Firmware says they are used.
384 	 */
385 	for (child = OF_child(root); child; child = OF_peer(child)) {
386 		memset(compat, 0, sizeof(compat));
387 		OF_getprop(child, "compatible", compat, sizeof(compat));
388 		if (strcmp(compat, "gmac") == 0)
389 			unin_enable_gmac(dev);
390 		if (strcmp(compat, "chrp,open-pic") == 0)
391 			unin_enable_mpic(dev);
392 	}
393 
394 	/*
395 	 * GMAC lives under the PCI bus, so just check if enet is gmac.
396 	 */
397 	child = OF_finddevice("enet");
398 	memset(compat, 0, sizeof(compat));
399 	OF_getprop(child, "compatible", compat, sizeof(compat));
400 	if (strcmp(compat, "gmac") == 0)
401 		unin_enable_gmac(dev);
402 
403 	return (bus_generic_attach(dev));
404 }
405 
406 static int
407 unin_chip_print_child(device_t dev, device_t child)
408 {
409         struct unin_chip_devinfo *dinfo;
410         struct resource_list *rl;
411         int retval = 0;
412 
413         dinfo = device_get_ivars(child);
414         rl = &dinfo->udi_resources;
415 
416         retval += bus_print_child_header(dev, child);
417 
418         retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
419         retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
420 
421         retval += bus_print_child_footer(dev, child);
422 
423         return (retval);
424 }
425 
426 static void
427 unin_chip_probe_nomatch(device_t dev, device_t child)
428 {
429         struct unin_chip_devinfo *dinfo;
430         struct resource_list *rl;
431 	const char *type;
432 
433 	if (bootverbose) {
434 		dinfo = device_get_ivars(child);
435 		rl = &dinfo->udi_resources;
436 
437 		if ((type = ofw_bus_get_type(child)) == NULL)
438 			type = "(unknown)";
439 		device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child));
440 		resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
441 		resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
442 		printf(" (no driver attached)\n");
443 	}
444 }
445 
446 
447 static struct resource *
448 unin_chip_alloc_resource(device_t bus, device_t child, int type, int *rid,
449 			 u_long start, u_long end, u_long count, u_int flags)
450 {
451 	struct		unin_chip_softc *sc;
452 	int		needactivate;
453 	struct		resource *rv;
454 	struct		rman *rm;
455 	u_long		adjstart, adjend, adjcount;
456 	struct		unin_chip_devinfo *dinfo;
457 	struct		resource_list_entry *rle;
458 
459 	sc = device_get_softc(bus);
460 	dinfo = device_get_ivars(child);
461 
462 	needactivate = flags & RF_ACTIVE;
463 	flags &= ~RF_ACTIVE;
464 
465 	switch (type) {
466 	case SYS_RES_MEMORY:
467 	case SYS_RES_IOPORT:
468 		rle = resource_list_find(&dinfo->udi_resources, SYS_RES_MEMORY,
469 					 *rid);
470 		if (rle == NULL) {
471 			device_printf(bus, "no rle for %s memory %d\n",
472 				      device_get_nameunit(child), *rid);
473 			return (NULL);
474 		}
475 
476 		rle->end = rle->end - 1; /* Hack? */
477 
478 		if (start < rle->start)
479 			adjstart = rle->start;
480 		else if (start > rle->end)
481 			adjstart = rle->end;
482 		else
483 			adjstart = start;
484 
485 		if (end < rle->start)
486 			adjend = rle->start;
487 		else if (end > rle->end)
488 			adjend = rle->end;
489 		else
490 			adjend = end;
491 
492 		adjcount = adjend - adjstart;
493 
494 		rm = &sc->sc_mem_rman;
495 		break;
496 
497 	case SYS_RES_IRQ:
498 		/* Check for passthrough from subattachments. */
499 		if (device_get_parent(child) != bus)
500 			return BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
501 						  type, rid, start, end, count,
502 						  flags);
503 
504 		rle = resource_list_find(&dinfo->udi_resources, SYS_RES_IRQ,
505 		    *rid);
506 		if (rle == NULL) {
507 			if (dinfo->udi_ninterrupts >= 6) {
508 				device_printf(bus,
509 					      "%s has more than 6 interrupts\n",
510 					      device_get_nameunit(child));
511 				return (NULL);
512 			}
513 			resource_list_add(&dinfo->udi_resources, SYS_RES_IRQ,
514 					  dinfo->udi_ninterrupts, start, start,
515 					  1);
516 
517 			dinfo->udi_interrupts[dinfo->udi_ninterrupts] = start;
518 			dinfo->udi_ninterrupts++;
519 		}
520 
521 		return (resource_list_alloc(&dinfo->udi_resources, bus, child,
522 					    type, rid, start, end, count,
523 					    flags));
524 	default:
525 		device_printf(bus, "unknown resource request from %s\n",
526 			      device_get_nameunit(child));
527 		return (NULL);
528 	}
529 
530 	rv = rman_reserve_resource(rm, adjstart, adjend, adjcount, flags,
531 				   child);
532 	if (rv == NULL) {
533 		device_printf(bus,
534 			      "failed to reserve resource %#lx - %#lx (%#lx)"
535 			      " for %s\n", adjstart, adjend, adjcount,
536 			      device_get_nameunit(child));
537 		return (NULL);
538 	}
539 
540 	rman_set_rid(rv, *rid);
541 
542 	if (needactivate) {
543 		if (bus_activate_resource(child, type, *rid, rv) != 0) {
544                         device_printf(bus,
545 				      "failed to activate resource for %s\n",
546 				      device_get_nameunit(child));
547 			rman_release_resource(rv);
548 			return (NULL);
549                 }
550         }
551 
552 	return (rv);
553 }
554 
555 static int
556 unin_chip_release_resource(device_t bus, device_t child, int type, int rid,
557 			   struct resource *res)
558 {
559 	if (rman_get_flags(res) & RF_ACTIVE) {
560 		int error = bus_deactivate_resource(child, type, rid, res);
561 		if (error)
562 			return error;
563 	}
564 
565 	return (rman_release_resource(res));
566 }
567 
568 static int
569 unin_chip_activate_resource(device_t bus, device_t child, int type, int rid,
570 			    struct resource *res)
571 {
572 	void    *p;
573 
574 	if (type == SYS_RES_IRQ)
575                 return (bus_activate_resource(bus, type, rid, res));
576 
577 	if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
578 		vm_offset_t start;
579 
580 		start = (vm_offset_t) rman_get_start(res);
581 
582 		if (bootverbose)
583 			printf("unin mapdev: start %zx, len %ld\n", start,
584 			       rman_get_size(res));
585 
586 		p = pmap_mapdev(start, (vm_size_t) rman_get_size(res));
587 		if (p == NULL)
588 			return (ENOMEM);
589 		rman_set_virtual(res, p);
590 		rman_set_bustag(res, &bs_be_tag);
591 		rman_set_bushandle(res, (u_long)p);
592 	}
593 
594 	return (rman_activate_resource(res));
595 }
596 
597 
598 static int
599 unin_chip_deactivate_resource(device_t bus, device_t child, int type, int rid,
600 			      struct resource *res)
601 {
602         /*
603          * If this is a memory resource, unmap it.
604          */
605         if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
606 		u_int32_t psize;
607 
608 		psize = rman_get_size(res);
609 		pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
610 	}
611 
612 	return (rman_deactivate_resource(res));
613 }
614 
615 
616 static struct resource_list *
617 unin_chip_get_resource_list (device_t dev, device_t child)
618 {
619 	struct unin_chip_devinfo *dinfo;
620 
621 	dinfo = device_get_ivars(child);
622 	return (&dinfo->udi_resources);
623 }
624 
625 static const struct ofw_bus_devinfo *
626 unin_chip_get_devinfo(device_t dev, device_t child)
627 {
628 	struct unin_chip_devinfo *dinfo;
629 
630 	dinfo = device_get_ivars(child);
631 	return (&dinfo->udi_obdinfo);
632 }
633 
634