xref: /freebsd/sys/powerpc/powermac/uninorth.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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 
38 #include <dev/pci/pcivar.h>
39 #include <dev/pci/pcireg.h>
40 
41 #include <machine/bus.h>
42 #include <machine/md_var.h>
43 #include <machine/nexusvar.h>
44 #include <machine/pio.h>
45 #include <machine/resource.h>
46 
47 #include <sys/rman.h>
48 
49 #include <powerpc/ofw/ofw_pci.h>
50 #include <powerpc/powermac/uninorthvar.h>
51 
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54 
55 #include "pcib_if.h"
56 
57 #define	UNINORTH_DEBUG	0
58 
59 /*
60  * Device interface.
61  */
62 static int		uninorth_probe(device_t);
63 static int		uninorth_attach(device_t);
64 
65 /*
66  * Bus interface.
67  */
68 static int		uninorth_read_ivar(device_t, device_t, int,
69 			    uintptr_t *);
70 static struct		resource * uninorth_alloc_resource(device_t bus,
71 			    device_t child, int type, int *rid, u_long start,
72 			    u_long end, u_long count, u_int flags);
73 static int		uninorth_activate_resource(device_t bus, device_t child,
74 			    int type, int rid, struct resource *res);
75 
76 /*
77  * pcib interface.
78  */
79 static int		uninorth_maxslots(device_t);
80 static u_int32_t	uninorth_read_config(device_t, u_int, u_int, u_int,
81 			    u_int, int);
82 static void		uninorth_write_config(device_t, u_int, u_int, u_int,
83 			    u_int, u_int32_t, int);
84 static int		uninorth_route_interrupt(device_t, device_t, int);
85 
86 /*
87  * Local routines.
88  */
89 static int		uninorth_enable_config(struct uninorth_softc *, u_int,
90 			    u_int, u_int, u_int);
91 static void		unin_enable_gmac(void);
92 
93 /*
94  * Driver methods.
95  */
96 static device_method_t	uninorth_methods[] = {
97 	/* Device interface */
98 	DEVMETHOD(device_probe,		uninorth_probe),
99 	DEVMETHOD(device_attach,	uninorth_attach),
100 
101 	/* Bus interface */
102 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
103 	DEVMETHOD(bus_read_ivar,	uninorth_read_ivar),
104 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
105 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
106 	DEVMETHOD(bus_alloc_resource,	uninorth_alloc_resource),
107 	DEVMETHOD(bus_activate_resource,	uninorth_activate_resource),
108 
109 	/* pcib interface */
110 	DEVMETHOD(pcib_maxslots,	uninorth_maxslots),
111 	DEVMETHOD(pcib_read_config,	uninorth_read_config),
112 	DEVMETHOD(pcib_write_config,	uninorth_write_config),
113 	DEVMETHOD(pcib_route_interrupt,	uninorth_route_interrupt),
114 
115 	{ 0, 0 }
116 };
117 
118 static driver_t	uninorth_driver = {
119 	"pcib",
120 	uninorth_methods,
121 	sizeof(struct uninorth_softc)
122 };
123 
124 static devclass_t	uninorth_devclass;
125 
126 DRIVER_MODULE(uninorth, nexus, uninorth_driver, uninorth_devclass, 0, 0);
127 
128 static int
129 uninorth_probe(device_t dev)
130 {
131 	char	*type, *compatible;
132 
133 	type = nexus_get_device_type(dev);
134 	compatible = nexus_get_compatible(dev);
135 
136 	if (type == NULL || compatible == NULL)
137 		return (ENXIO);
138 
139 	if (strcmp(type, "pci") != 0 || strcmp(compatible, "uni-north") != 0)
140 		return (ENXIO);
141 
142 	device_set_desc(dev, "Apple UniNorth Host-PCI bridge");
143 	return (0);
144 }
145 
146 static int
147 uninorth_attach(device_t dev)
148 {
149 	struct		uninorth_softc *sc;
150 	phandle_t	node;
151 	phandle_t	child;
152 	u_int32_t	reg[2], busrange[2];
153 	struct		uninorth_range *rp, *io, *mem[2];
154 	int		nmem, i;
155 
156 	node = nexus_get_node(dev);
157 	sc = device_get_softc(dev);
158 
159 	if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)
160 		return (ENXIO);
161 
162 	if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
163 		return (ENXIO);
164 
165 	sc->sc_dev = dev;
166 	sc->sc_node = node;
167 	sc->sc_addr = (vm_offset_t)pmap_mapdev(reg[0] + 0x800000, PAGE_SIZE);
168 	sc->sc_data = (vm_offset_t)pmap_mapdev(reg[0] + 0xc00000, PAGE_SIZE);
169 	sc->sc_bus = busrange[0];
170 
171 	bzero(sc->sc_range, sizeof(sc->sc_range));
172 	sc->sc_nrange = OF_getprop(node, "ranges", sc->sc_range,
173 	    sizeof(sc->sc_range));
174 
175 	if (sc->sc_nrange == -1) {
176 		device_printf(dev, "could not get ranges\n");
177 		return (ENXIO);
178 	}
179 
180 	sc->sc_range[6].pci_hi = 0;
181 	io = NULL;
182 	nmem = 0;
183 
184 	for (rp = sc->sc_range; rp->pci_hi != 0; rp++) {
185 		switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
186 		case OFW_PCI_PHYS_HI_SPACE_CONFIG:
187 			break;
188 		case OFW_PCI_PHYS_HI_SPACE_IO:
189 			io = rp;
190 			break;
191 		case OFW_PCI_PHYS_HI_SPACE_MEM32:
192 			mem[nmem] = rp;
193 			nmem++;
194 			break;
195 		case OFW_PCI_PHYS_HI_SPACE_MEM64:
196 			break;
197 		}
198 	}
199 
200 	if (io == NULL) {
201 		device_printf(dev, "can't find io range\n");
202 		return (ENXIO);
203 	}
204 	sc->sc_io_rman.rm_type = RMAN_ARRAY;
205 	sc->sc_io_rman.rm_descr = "UniNorth PCI I/O Ports";
206 	sc->sc_iostart = io->host;
207 	if (rman_init(&sc->sc_io_rman) != 0 ||
208 	    rman_manage_region(&sc->sc_io_rman, io->pci_lo,
209 	    io->pci_lo + io->size_lo - 1) != 0) {
210 		device_printf(dev, "failed to set up io range management\n");
211 		return (ENXIO);
212 	}
213 
214 	if (nmem == 0) {
215 		device_printf(dev, "can't find mem ranges\n");
216 		return (ENXIO);
217 	}
218 	sc->sc_mem_rman.rm_type = RMAN_ARRAY;
219 	sc->sc_mem_rman.rm_descr = "UniNorth PCI Memory";
220 	if (rman_init(&sc->sc_mem_rman) != 0) {
221 		device_printf(dev,
222 		    "failed to init mem range resources\n");
223 		return (ENXIO);
224 	}
225 	for (i = 0; i < nmem; i++) {
226 		if (rman_manage_region(&sc->sc_mem_rman, mem[i]->pci_lo,
227 		    mem[i]->pci_lo + mem[i]->size_lo - 1) != 0) {
228 			device_printf(dev,
229 			    "failed to set up memory range management\n");
230 			return (ENXIO);
231 		}
232 	}
233 
234 	/*
235 	 * Enable the GMAC Ethernet cell if Open Firmware says it is
236 	 * used.
237 	 */
238 	for (child = OF_child(node); child; child = OF_peer(child)) {
239 		char compat[32];
240 
241 		memset(compat, 0, sizeof(compat));
242 		OF_getprop(child, "compatible", compat, sizeof(compat));
243 		if (strcmp(compat, "gmac") == 0) {
244 			unin_enable_gmac();
245 		}
246 	}
247 
248 	/*
249 	 * Write out the correct PIC interrupt values to config space
250 	 * of all devices on the bus. This has to be done after the GEM
251 	 * cell is enabled above.
252 	 */
253 	ofw_pci_fixup(dev, sc->sc_bus, node);
254 
255 	device_add_child(dev, "pci", device_get_unit(dev));
256 	return (bus_generic_attach(dev));
257 }
258 
259 static int
260 uninorth_maxslots(device_t dev)
261 {
262 
263 	return (PCI_SLOTMAX);
264 }
265 
266 static u_int32_t
267 uninorth_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
268     int width)
269 {
270 	struct		uninorth_softc *sc;
271 	vm_offset_t	caoff;
272 
273 	sc = device_get_softc(dev);
274 	caoff = sc->sc_data + (reg & 0x07);
275 
276 	if (uninorth_enable_config(sc, bus, slot, func, reg) != 0) {
277 		switch (width) {
278 		case 1:
279 			return (in8rb(caoff));
280 			break;
281 		case 2:
282 			return (in16rb(caoff));
283 			break;
284 		case 4:
285 			return (in32rb(caoff));
286 			break;
287 		}
288 	}
289 
290 	return (0xffffffff);
291 }
292 
293 static void
294 uninorth_write_config(device_t dev, u_int bus, u_int slot, u_int func,
295     u_int reg, u_int32_t val, int width)
296 {
297 	struct		uninorth_softc *sc;
298 	vm_offset_t	caoff;
299 
300 	sc = device_get_softc(dev);
301 	caoff = sc->sc_data + (reg & 0x07);
302 
303 	if (uninorth_enable_config(sc, bus, slot, func, reg)) {
304 		switch (width) {
305 		case 1:
306 			out8rb(caoff, val);
307 			break;
308 		case 2:
309 			out16rb(caoff, val);
310 			break;
311 		case 4:
312 			out32rb(caoff, val);
313 			break;
314 		}
315 	}
316 }
317 
318 static int
319 uninorth_route_interrupt(device_t bus, device_t dev, int pin)
320 {
321 
322 	return (0);
323 }
324 
325 static int
326 uninorth_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
327 {
328 	struct	uninorth_softc *sc;
329 
330 	sc = device_get_softc(dev);
331 
332 	switch (which) {
333 	case PCIB_IVAR_DOMAIN:
334 		*result = device_get_unit(dev);
335 		return (0);
336 	case PCIB_IVAR_BUS:
337 		*result = sc->sc_bus;
338 		return (0);
339 	}
340 
341 	return (ENOENT);
342 }
343 
344 static struct resource *
345 uninorth_alloc_resource(device_t bus, device_t child, int type, int *rid,
346     u_long start, u_long end, u_long count, u_int flags)
347 {
348 	struct			uninorth_softc *sc;
349 	struct			resource *rv;
350 	struct			rman *rm;
351 	int			needactivate;
352 
353 	needactivate = flags & RF_ACTIVE;
354 	flags &= ~RF_ACTIVE;
355 
356 	sc = device_get_softc(bus);
357 
358 	switch (type) {
359 	case SYS_RES_MEMORY:
360 		rm = &sc->sc_mem_rman;
361 		break;
362 
363 	case SYS_RES_IOPORT:
364 		rm = &sc->sc_io_rman;
365 		break;
366 
367 	case SYS_RES_IRQ:
368 		return (bus_alloc_resource(bus, type, rid, start, end, count,
369 		    flags));
370 
371 	default:
372 		device_printf(bus, "unknown resource request from %s\n",
373 		    device_get_nameunit(child));
374 		return (NULL);
375 	}
376 
377 	rv = rman_reserve_resource(rm, start, end, count, flags, child);
378 	if (rv == NULL) {
379 		device_printf(bus, "failed to reserve resource for %s\n",
380 		    device_get_nameunit(child));
381 		return (NULL);
382 	}
383 
384 	rman_set_rid(rv, *rid);
385 
386 	if (needactivate) {
387 		if (bus_activate_resource(child, type, *rid, rv) != 0) {
388 			device_printf(bus,
389 			    "failed to activate resource for %s\n",
390 			    device_get_nameunit(child));
391 			rman_release_resource(rv);
392 			return (NULL);
393 		}
394 	}
395 
396 	return (rv);
397 }
398 
399 static int
400 uninorth_activate_resource(device_t bus, device_t child, int type, int rid,
401     struct resource *res)
402 {
403 	void	*p;
404 	struct	uninorth_softc *sc;
405 
406 	sc = device_get_softc(bus);
407 
408 	if (type == SYS_RES_IRQ)
409 		return (bus_activate_resource(bus, type, rid, res));
410 
411 	if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
412 		vm_offset_t start;
413 
414 		start = (vm_offset_t)rman_get_start(res);
415 		/*
416 		 * For i/o-ports, convert the start address to the
417 		 * uninorth PCI i/o window
418 		 */
419 		if (type == SYS_RES_IOPORT)
420 			start += sc->sc_iostart;
421 
422 		if (bootverbose)
423 			printf("uninorth mapdev: start %x, len %ld\n", start,
424 			    rman_get_size(res));
425 
426 		p = pmap_mapdev(start, (vm_size_t)rman_get_size(res));
427 		if (p == NULL)
428 			return (ENOMEM);
429 		rman_set_virtual(res, p);
430 		rman_set_bustag(res, &bs_le_tag);
431 		rman_set_bushandle(res, (u_long)p);
432 	}
433 
434 	return (rman_activate_resource(res));
435 }
436 
437 static int
438 uninorth_enable_config(struct uninorth_softc *sc, u_int bus, u_int slot,
439     u_int func, u_int reg)
440 {
441 	uint32_t	cfgval;
442 	uint32_t	pass;
443 
444 	if (resource_int_value(device_get_name(sc->sc_dev),
445 	        device_get_unit(sc->sc_dev), "skipslot", &pass) == 0) {
446 		if (pass == slot)
447 			return (0);
448 	}
449 
450 	if (sc->sc_bus == bus) {
451 		/*
452 		 * No slots less than 11 on the primary bus
453 		 */
454 		if (slot < 11)
455 			return (0);
456 
457 		cfgval = (1 << slot) | (func << 8) | (reg & 0xfc);
458 	} else {
459 		cfgval = (bus << 16) | (slot << 11) | (func << 8) |
460 		    (reg & 0xfc) | 1;
461 	}
462 
463 	do {
464 		out32rb(sc->sc_addr, cfgval);
465 	} while (in32rb(sc->sc_addr) != cfgval);
466 
467 	return (1);
468 }
469 
470 /*
471  * Driver to swallow UniNorth host bridges from the PCI bus side.
472  */
473 static int
474 unhb_probe(device_t dev)
475 {
476 
477 	if (pci_get_class(dev) == PCIC_BRIDGE &&
478 	    pci_get_subclass(dev) == PCIS_BRIDGE_HOST) {
479 		device_set_desc(dev, "Host to PCI bridge");
480 		device_quiet(dev);
481 		return (-10000);
482 	}
483 
484 	return (ENXIO);
485 }
486 
487 static int
488 unhb_attach(device_t dev)
489 {
490 
491 	return (0);
492 }
493 
494 static device_method_t unhb_methods[] = {
495 	/* Device interface */
496 	DEVMETHOD(device_probe,         unhb_probe),
497 	DEVMETHOD(device_attach,        unhb_attach),
498 
499 	{ 0, 0 }
500 };
501 
502 static driver_t unhb_driver = {
503 	"unhb",
504 	unhb_methods,
505 	1,
506 };
507 static devclass_t unhb_devclass;
508 
509 DRIVER_MODULE(unhb, pci, unhb_driver, unhb_devclass, 0, 0);
510 
511 
512 /*
513  * Small stub driver for the Uninorth chip itself, to allow setting
514  * of various parameters and cell enables
515  */
516 static struct unin_chip_softc *uncsc;
517 
518 static void
519 unin_enable_gmac(void)
520 {
521 	volatile u_int *clkreg;
522 	u_int32_t tmpl;
523 
524 	if (uncsc == NULL)
525 		panic("unin_enable_gmac: device not found");
526 
527 	clkreg = (void *)(uncsc->sc_addr + UNIN_CLOCKCNTL);
528 	tmpl = inl(clkreg);
529 	tmpl |= UNIN_CLOCKCNTL_GMAC;
530 	outl(clkreg, tmpl);
531 }
532 
533 static int
534 unin_chip_probe(device_t dev)
535 {
536 	char	*name;
537 
538 	name = nexus_get_name(dev);
539 
540 	if (name == NULL)
541 		return (ENXIO);
542 
543 	if (strcmp(name, "uni-n") != 0)
544 		return (ENXIO);
545 
546 	device_set_desc(dev, "Apple UniNorth System Controller");
547 	return (0);
548 }
549 
550 static int
551 unin_chip_attach(device_t dev)
552 {
553 	phandle_t node;
554 	u_int reg[2];
555 
556 	uncsc = device_get_softc(dev);
557 	node = nexus_get_node(dev);
558 
559 	if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)
560 		return (ENXIO);
561 
562 	uncsc->sc_physaddr = reg[0];
563 	uncsc->sc_size = reg[1];
564 
565 	/*
566 	 * Only map the first page, since that is where the registers
567 	 * of interest lie.
568 	 */
569 	uncsc->sc_addr = (vm_offset_t) pmap_mapdev(reg[0], PAGE_SIZE);
570 
571 	uncsc->sc_version = *(u_int *)uncsc->sc_addr;
572 	device_printf(dev, "Version %d\n", uncsc->sc_version);
573 
574 	return (0);
575 }
576 
577 static device_method_t unin_chip_methods[] = {
578 	/* Device interface */
579 	DEVMETHOD(device_probe,         unin_chip_probe),
580 	DEVMETHOD(device_attach,        unin_chip_attach),
581 
582 	{ 0, 0 }
583 };
584 
585 static driver_t	unin_chip_driver = {
586 	"unin",
587 	unin_chip_methods,
588 	sizeof(struct unin_chip_softc)
589 };
590 
591 static devclass_t	unin_chip_devclass;
592 
593 DRIVER_MODULE(unin, nexus, unin_chip_driver, unin_chip_devclass, 0, 0);
594 
595 
596 
597 
598