xref: /freebsd/sys/powerpc/powermac/uninorth.c (revision 3157ba21)
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 	int	*intr;
148 	int	i, nintr;
149 	phandle_t iparent;
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 		resource_list_add(&dinfo->udi_resources, SYS_RES_IRQ,
179 		    dinfo->udi_ninterrupts, intr[i], intr[i], 1);
180 
181 		dinfo->udi_interrupts[dinfo->udi_ninterrupts] = intr[i];
182 		dinfo->udi_ninterrupts++;
183 	}
184 }
185 
186 static void
187 unin_chip_add_reg(phandle_t devnode, struct unin_chip_devinfo *dinfo)
188 {
189 	struct	unin_chip_reg *reg;
190 	int	i, nreg;
191 
192 	nreg = OF_getprop_alloc(devnode, "reg", sizeof(*reg), (void **)&reg);
193 	if (nreg == -1)
194 		return;
195 
196 	for (i = 0; i < nreg; i++) {
197 		resource_list_add(&dinfo->udi_resources, SYS_RES_MEMORY, i,
198 				  reg[i].mr_base,
199 				  reg[i].mr_base + reg[i].mr_size,
200 				  reg[i].mr_size);
201 	}
202 }
203 
204 static void
205 unin_enable_gmac(device_t dev)
206 {
207 	volatile u_int *clkreg;
208 	struct unin_chip_softc *sc;
209 	u_int32_t tmpl;
210 
211 	sc = device_get_softc(dev);
212 	clkreg = (void *)(sc->sc_addr + UNIN_CLOCKCNTL);
213 	tmpl = inl(clkreg);
214 	tmpl |= UNIN_CLOCKCNTL_GMAC;
215 	outl(clkreg, tmpl);
216 }
217 
218 static void
219 unin_enable_mpic(device_t dev)
220 {
221 	volatile u_int *toggle;
222 	struct unin_chip_softc *sc;
223 	u_int32_t tmpl;
224 
225 	sc = device_get_softc(dev);
226 	toggle = (void *)(sc->sc_addr + UNIN_TOGGLE_REG);
227 	tmpl = inl(toggle);
228 	tmpl |= UNIN_MPIC_RESET | UNIN_MPIC_OUTPUT_ENABLE;
229 	outl(toggle, tmpl);
230 }
231 
232 static int
233 unin_chip_probe(device_t dev)
234 {
235 	const char	*name;
236 
237 	name = ofw_bus_get_name(dev);
238 
239 	if (name == NULL)
240 		return (ENXIO);
241 
242 	if (strcmp(name, "uni-n") != 0 && strcmp(name, "u3") != 0
243 	    && strcmp(name, "u4") != 0)
244 		return (ENXIO);
245 
246 	device_set_desc(dev, "Apple UniNorth System Controller");
247 	return (0);
248 }
249 
250 static int
251 unin_chip_attach(device_t dev)
252 {
253 	struct unin_chip_softc *sc;
254 	struct unin_chip_devinfo *dinfo;
255 	phandle_t  root;
256 	phandle_t  child;
257 	device_t   cdev;
258 	char compat[32];
259 	u_int reg[3];
260 	int error, i = 0;
261 
262 	sc = device_get_softc(dev);
263 	root = ofw_bus_get_node(dev);
264 
265 	if (OF_getprop(root, "reg", reg, sizeof(reg)) < 8)
266 		return (ENXIO);
267 
268 	if (strcmp(ofw_bus_get_name(dev), "u3") == 0
269 	    || strcmp(ofw_bus_get_name(dev), "u4") == 0)
270 		i = 1; /* #address-cells lies */
271 
272 	sc->sc_physaddr = reg[i];
273 	sc->sc_size = reg[i+1];
274 
275 	sc->sc_mem_rman.rm_type = RMAN_ARRAY;
276 	sc->sc_mem_rman.rm_descr = "UniNorth Device Memory";
277 
278 	error = rman_init(&sc->sc_mem_rman);
279 
280 	if (error) {
281 		device_printf(dev, "rman_init() failed. error = %d\n", error);
282 		return (error);
283 	}
284 
285 	error = rman_manage_region(&sc->sc_mem_rman, sc->sc_physaddr,
286 				   sc->sc_physaddr + sc->sc_size - 1);
287 	if (error) {
288 		device_printf(dev,
289 			      "rman_manage_region() failed. error = %d\n",
290 			      error);
291 		return (error);
292 	}
293 
294         /*
295 	 * Iterate through the sub-devices
296 	 */
297 	for (child = OF_child(root); child != 0; child = OF_peer(child)) {
298 		dinfo = malloc(sizeof(*dinfo), M_UNIN, M_WAITOK | M_ZERO);
299 		if (ofw_bus_gen_setup_devinfo(&dinfo->udi_obdinfo, child)
300 		    != 0)
301 		{
302 			free(dinfo, M_UNIN);
303 			continue;
304 		}
305 
306 		resource_list_init(&dinfo->udi_resources);
307 		dinfo->udi_ninterrupts = 0;
308 		unin_chip_add_intr(child, dinfo);
309 
310 		unin_chip_add_reg(child, dinfo);
311 
312 		cdev = device_add_child(dev, NULL, -1);
313 		if (cdev == NULL) {
314 			device_printf(dev, "<%s>: device_add_child failed\n",
315 				      dinfo->udi_obdinfo.obd_name);
316 			resource_list_free(&dinfo->udi_resources);
317 			ofw_bus_gen_destroy_devinfo(&dinfo->udi_obdinfo);
318 			free(dinfo, M_UNIN);
319 			continue;
320 		}
321 
322 		device_set_ivars(cdev, dinfo);
323 	}
324 
325 	/*
326 	 * Only map the first page, since that is where the registers
327 	 * of interest lie.
328 	 */
329 	sc->sc_addr = (vm_offset_t)pmap_mapdev(sc->sc_physaddr, PAGE_SIZE);
330 
331 	sc->sc_version = *(u_int *)sc->sc_addr;
332 	device_printf(dev, "Version %d\n", sc->sc_version);
333 
334 	/*
335 	 * Enable the GMAC Ethernet cell and the integrated OpenPIC
336 	 * if Open Firmware says they are used.
337 	 */
338 	for (child = OF_child(root); child; child = OF_peer(child)) {
339 		memset(compat, 0, sizeof(compat));
340 		OF_getprop(child, "compatible", compat, sizeof(compat));
341 		if (strcmp(compat, "gmac") == 0)
342 			unin_enable_gmac(dev);
343 		if (strcmp(compat, "chrp,open-pic") == 0)
344 			unin_enable_mpic(dev);
345 	}
346 
347 	/*
348 	 * GMAC lives under the PCI bus, so just check if enet is gmac.
349 	 */
350 	child = OF_finddevice("enet");
351 	memset(compat, 0, sizeof(compat));
352 	OF_getprop(child, "compatible", compat, sizeof(compat));
353 	if (strcmp(compat, "gmac") == 0)
354 		unin_enable_gmac(dev);
355 
356 	return (bus_generic_attach(dev));
357 }
358 
359 static int
360 unin_chip_print_child(device_t dev, device_t child)
361 {
362         struct unin_chip_devinfo *dinfo;
363         struct resource_list *rl;
364         int retval = 0;
365 
366         dinfo = device_get_ivars(child);
367         rl = &dinfo->udi_resources;
368 
369         retval += bus_print_child_header(dev, child);
370 
371         retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
372         retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
373 
374         retval += bus_print_child_footer(dev, child);
375 
376         return (retval);
377 }
378 
379 static void
380 unin_chip_probe_nomatch(device_t dev, device_t child)
381 {
382         struct unin_chip_devinfo *dinfo;
383         struct resource_list *rl;
384 	const char *type;
385 
386 	if (bootverbose) {
387 		dinfo = device_get_ivars(child);
388 		rl = &dinfo->udi_resources;
389 
390 		if ((type = ofw_bus_get_type(child)) == NULL)
391 			type = "(unknown)";
392 		device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child));
393 		resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
394 		resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
395 		printf(" (no driver attached)\n");
396 	}
397 }
398 
399 
400 static struct resource *
401 unin_chip_alloc_resource(device_t bus, device_t child, int type, int *rid,
402 			 u_long start, u_long end, u_long count, u_int flags)
403 {
404 	struct		unin_chip_softc *sc;
405 	int		needactivate;
406 	struct		resource *rv;
407 	struct		rman *rm;
408 	u_long		adjstart, adjend, adjcount;
409 	struct		unin_chip_devinfo *dinfo;
410 	struct		resource_list_entry *rle;
411 
412 	sc = device_get_softc(bus);
413 	dinfo = device_get_ivars(child);
414 
415 	needactivate = flags & RF_ACTIVE;
416 	flags &= ~RF_ACTIVE;
417 
418 	switch (type) {
419 	case SYS_RES_MEMORY:
420 	case SYS_RES_IOPORT:
421 		rle = resource_list_find(&dinfo->udi_resources, SYS_RES_MEMORY,
422 					 *rid);
423 		if (rle == NULL) {
424 			device_printf(bus, "no rle for %s memory %d\n",
425 				      device_get_nameunit(child), *rid);
426 			return (NULL);
427 		}
428 
429 		rle->end = rle->end - 1; /* Hack? */
430 
431 		if (start < rle->start)
432 			adjstart = rle->start;
433 		else if (start > rle->end)
434 			adjstart = rle->end;
435 		else
436 			adjstart = start;
437 
438 		if (end < rle->start)
439 			adjend = rle->start;
440 		else if (end > rle->end)
441 			adjend = rle->end;
442 		else
443 			adjend = end;
444 
445 		adjcount = adjend - adjstart;
446 
447 		rm = &sc->sc_mem_rman;
448 		break;
449 
450 	case SYS_RES_IRQ:
451 		/* Check for passthrough from subattachments. */
452 		if (device_get_parent(child) != bus)
453 			return BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
454 						  type, rid, start, end, count,
455 						  flags);
456 
457 		rle = resource_list_find(&dinfo->udi_resources, SYS_RES_IRQ,
458 		    *rid);
459 		if (rle == NULL) {
460 			if (dinfo->udi_ninterrupts >= 6) {
461 				device_printf(bus,
462 					      "%s has more than 6 interrupts\n",
463 					      device_get_nameunit(child));
464 				return (NULL);
465 			}
466 			resource_list_add(&dinfo->udi_resources, SYS_RES_IRQ,
467 					  dinfo->udi_ninterrupts, start, start,
468 					  1);
469 
470 			dinfo->udi_interrupts[dinfo->udi_ninterrupts] = start;
471 			dinfo->udi_ninterrupts++;
472 		}
473 
474 		return (resource_list_alloc(&dinfo->udi_resources, bus, child,
475 					    type, rid, start, end, count,
476 					    flags));
477 	default:
478 		device_printf(bus, "unknown resource request from %s\n",
479 			      device_get_nameunit(child));
480 		return (NULL);
481 	}
482 
483 	rv = rman_reserve_resource(rm, adjstart, adjend, adjcount, flags,
484 				   child);
485 	if (rv == NULL) {
486 		device_printf(bus,
487 			      "failed to reserve resource %#lx - %#lx (%#lx)"
488 			      " for %s\n", adjstart, adjend, adjcount,
489 			      device_get_nameunit(child));
490 		return (NULL);
491 	}
492 
493 	rman_set_rid(rv, *rid);
494 
495 	if (needactivate) {
496 		if (bus_activate_resource(child, type, *rid, rv) != 0) {
497                         device_printf(bus,
498 				      "failed to activate resource for %s\n",
499 				      device_get_nameunit(child));
500 			rman_release_resource(rv);
501 			return (NULL);
502                 }
503         }
504 
505 	return (rv);
506 }
507 
508 static int
509 unin_chip_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 static int
522 unin_chip_activate_resource(device_t bus, device_t child, int type, int rid,
523 			    struct resource *res)
524 {
525 	struct unin_chip_softc *sc;
526 	void    *p;
527 
528 	sc = device_get_softc(bus);
529 
530 	if (type == SYS_RES_IRQ)
531                 return (bus_activate_resource(bus, type, rid, res));
532 
533 	if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
534 		vm_offset_t start;
535 
536 		start = (vm_offset_t) rman_get_start(res);
537 
538 		if (bootverbose)
539 			printf("unin mapdev: start %zx, len %ld\n", start,
540 			       rman_get_size(res));
541 
542 		p = pmap_mapdev(start, (vm_size_t) rman_get_size(res));
543 		if (p == NULL)
544 			return (ENOMEM);
545 		rman_set_virtual(res, p);
546 		rman_set_bustag(res, &bs_be_tag);
547 		rman_set_bushandle(res, (u_long)p);
548 	}
549 
550 	return (rman_activate_resource(res));
551 }
552 
553 
554 static int
555 unin_chip_deactivate_resource(device_t bus, device_t child, int type, int rid,
556 			      struct resource *res)
557 {
558         /*
559          * If this is a memory resource, unmap it.
560          */
561         if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
562 		u_int32_t psize;
563 
564 		psize = rman_get_size(res);
565 		pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
566 	}
567 
568 	return (rman_deactivate_resource(res));
569 }
570 
571 
572 static struct resource_list *
573 unin_chip_get_resource_list (device_t dev, device_t child)
574 {
575 	struct unin_chip_devinfo *dinfo;
576 
577 	dinfo = device_get_ivars(child);
578 	return (&dinfo->udi_resources);
579 }
580 
581 static const struct ofw_bus_devinfo *
582 unin_chip_get_devinfo(device_t dev, device_t child)
583 {
584 	struct unin_chip_devinfo *dinfo;
585 
586 	dinfo = device_get_ivars(child);
587 	return (&dinfo->udi_obdinfo);
588 }
589 
590