xref: /freebsd/sys/powerpc/powermac/uninorth.c (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
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 #define __RMAN_RESOURCE_VISIBLE
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/module.h>
32 #include <sys/bus.h>
33 #include <sys/conf.h>
34 #include <sys/kernel.h>
35 
36 #include <dev/ofw/openfirm.h>
37 #include <dev/ofw/ofw_pci.h>
38 
39 #include <dev/pci/pcivar.h>
40 #include <dev/pci/pcireg.h>
41 
42 #include <machine/bus.h>
43 #include <machine/md_var.h>
44 #include <machine/nexusvar.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 	if (rman_init(&sc->sc_io_rman) != 0 ||
207 	    rman_manage_region(&sc->sc_io_rman, io->pci_lo,
208 	    io->pci_lo + io->size_lo) != 0) {
209 		device_printf(dev, "failed to set up io range management\n");
210 		return (ENXIO);
211 	}
212 
213 	if (nmem == 0) {
214 		device_printf(dev, "can't find mem ranges\n");
215 		return (ENXIO);
216 	}
217 	sc->sc_mem_rman.rm_type = RMAN_ARRAY;
218 	sc->sc_mem_rman.rm_descr = "UniNorth PCI Memory";
219 	if (rman_init(&sc->sc_mem_rman) != 0) {
220 		device_printf(dev,
221 		    "failed to init mem range resources\n");
222 		return (ENXIO);
223 	}
224 	for (i = 0; i < nmem; i++) {
225 		if (rman_manage_region(&sc->sc_mem_rman, mem[i]->pci_lo,
226 		    mem[i]->pci_lo + mem[i]->size_lo) != 0) {
227 			device_printf(dev,
228 			    "failed to set up memory range management\n");
229 			return (ENXIO);
230 		}
231 	}
232 
233 	/*
234 	 * Enable the GMAC ethernet cell if OpenFirmware says it is
235 	 * used
236 	 */
237 	for (child = OF_child(node); child; child = OF_peer(child)) {
238 		char compat[32];
239 
240 		memset(compat, 0, sizeof(compat));
241 		OF_getprop(child, "compatible", compat, sizeof(compat));
242 		if (strcmp(compat, "gmac") == 0) {
243 			unin_enable_gmac();
244 		}
245 	}
246 
247 	/*
248 	 * Write out the correct PIC interrupt values to config space
249 	 * of all devices on the bus. This has to be done after the GEM
250 	 * cell is enabled above.
251 	 */
252 	ofw_pci_fixup(dev, sc->sc_bus, node);
253 
254 	device_add_child(dev, "pci", device_get_unit(dev));
255 	return (bus_generic_attach(dev));
256 }
257 
258 static int
259 uninorth_maxslots(device_t dev)
260 {
261 
262 	return (PCI_SLOTMAX);
263 }
264 
265 static u_int32_t
266 uninorth_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
267     int width)
268 {
269 	struct		uninorth_softc *sc;
270 	vm_offset_t	caoff;
271 
272 	sc = device_get_softc(dev);
273 	caoff = sc->sc_data + (reg & 0x07);
274 
275 	if (uninorth_enable_config(sc, bus, slot, func, reg) != 0) {
276 		switch (width) {
277 		case 1:
278 			return (in8rb(caoff));
279 			break;
280 		case 2:
281 			return (in16rb(caoff));
282 			break;
283 		case 4:
284 			return (in32rb(caoff));
285 			break;
286 		}
287 	}
288 
289 	return (0xffffffff);
290 }
291 
292 static void
293 uninorth_write_config(device_t dev, u_int bus, u_int slot, u_int func,
294     u_int reg, u_int32_t val, int width)
295 {
296 	struct		uninorth_softc *sc;
297 	vm_offset_t	caoff;
298 
299 	sc = device_get_softc(dev);
300 	caoff = sc->sc_data + (reg & 0x07);
301 
302 	if (uninorth_enable_config(sc, bus, slot, func, reg)) {
303 		switch (width) {
304 		case 1:
305 			out8rb(caoff, val);
306 			(void)in8rb(caoff);
307 			break;
308 		case 2:
309 			out16rb(caoff, val);
310 			(void)in16rb(caoff);
311 			break;
312 		case 4:
313 			out32rb(caoff, val);
314 			(void)in32rb(caoff);
315 			break;
316 		}
317 	}
318 }
319 
320 static int
321 uninorth_route_interrupt(device_t bus, device_t dev, int pin)
322 {
323 
324 	return (0);
325 }
326 
327 static int
328 uninorth_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
329 {
330 	struct	uninorth_softc *sc;
331 
332 	sc = device_get_softc(dev);
333 
334 	switch (which) {
335 	case PCIB_IVAR_BUS:
336 		*result = sc->sc_bus;
337 		return (0);
338 		break;
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 	bus_space_tag_t		bt;
352 	int			needactivate;
353 
354 	needactivate = flags & RF_ACTIVE;
355 	flags &= ~RF_ACTIVE;
356 
357 	sc = device_get_softc(bus);
358 
359 	switch (type) {
360 	case SYS_RES_MEMORY:
361 		rm = &sc->sc_mem_rman;
362 		bt = PPC_BUS_SPACE_MEM;
363 		break;
364 	case SYS_RES_IRQ:
365 		return (bus_alloc_resource(bus, type, rid, start, end, count,
366 		    flags));
367 		break;
368 	default:
369 		device_printf(bus, "unknown resource request from %s\n",
370 		    device_get_nameunit(child));
371 		return (NULL);
372 	}
373 
374 	rv = rman_reserve_resource(rm, start, end, count, flags, child);
375 	if (rv == NULL) {
376 		device_printf(bus, "failed to reserve resource for %s\n",
377 		    device_get_nameunit(child));
378 		return (NULL);
379 	}
380 
381 	rman_set_bustag(rv, bt);
382 	rman_set_bushandle(rv, rman_get_start(rv));
383 
384 	if (needactivate) {
385 		if (bus_activate_resource(child, type, *rid, rv) != 0) {
386 			device_printf(bus,
387 			    "failed to activate resource for %s\n",
388 			    device_get_nameunit(child));
389 			rman_release_resource(rv);
390 			return (NULL);
391 		}
392 	}
393 
394 	return (rv);
395 }
396 
397 static int
398 uninorth_activate_resource(device_t bus, device_t child, int type, int rid,
399     struct resource *res)
400 {
401 	void	*p;
402 
403 	if (type == SYS_RES_IRQ)
404 		return (bus_activate_resource(bus, type, rid, res));
405 
406 	if (type == SYS_RES_MEMORY) {
407 		p = pmap_mapdev((vm_offset_t)rman_get_start(res),
408 		    (vm_size_t)rman_get_size(res));
409 		if (p == NULL)
410 			return (ENOMEM);
411 		rman_set_virtual(res, p);
412 		rman_set_bushandle(res, (u_long)p);
413 	}
414 
415 	return (rman_activate_resource(res));
416 }
417 
418 static int
419 uninorth_enable_config(struct uninorth_softc *sc, u_int bus, u_int slot,
420     u_int func, u_int reg)
421 {
422 	u_int32_t	cfgval;
423 
424 	if (sc->sc_bus == bus) {
425 		/*
426 		 * No slots less than 11 on the primary bus
427 		 */
428 		if (slot < 11)
429 			return (0);
430 
431 		cfgval = (1 << slot) | (func << 8) | (reg & 0xfc);
432 	} else {
433 		cfgval = (bus << 16) | (slot << 11) | (func << 8) |
434 		    (reg & 0xfc) | 1;
435 	}
436 
437 	do {
438 		out32rb(sc->sc_addr, cfgval);
439 	} while (in32rb(sc->sc_addr) != cfgval);
440 
441 	return (1);
442 }
443 
444 /*
445  * Driver to swallow UniNorth host bridges from the PCI bus side.
446  */
447 static int
448 unhb_probe(device_t dev)
449 {
450 
451 	if (pci_get_class(dev) == PCIC_BRIDGE &&
452 	    pci_get_subclass(dev) == PCIS_BRIDGE_HOST) {
453 		device_set_desc(dev, "Host to PCI bridge");
454 		device_quiet(dev);
455 		return (-10000);
456 	}
457 
458 	return (ENXIO);
459 }
460 
461 static int
462 unhb_attach(device_t dev)
463 {
464 
465 	return (0);
466 }
467 
468 static device_method_t unhb_methods[] = {
469 	/* Device interface */
470 	DEVMETHOD(device_probe,         unhb_probe),
471 	DEVMETHOD(device_attach,        unhb_attach),
472 
473 	{ 0, 0 }
474 };
475 
476 static driver_t unhb_driver = {
477 	"unhb",
478 	unhb_methods,
479 	1,
480 };
481 static devclass_t unhb_devclass;
482 
483 DRIVER_MODULE(unhb, pci, unhb_driver, unhb_devclass, 0, 0);
484 
485 
486 /*
487  * Small stub driver for the Uninorth chip itself, to allow setting
488  * of various parameters and cell enables
489  */
490 static struct unin_chip_softc *uncsc;
491 
492 static void
493 unin_enable_gmac(void)
494 {
495 	volatile u_int *clkreg;
496 	u_int32_t tmpl;
497 
498 	if (uncsc == NULL)
499 		panic("unin_enable_gmac: device not found");
500 
501 	clkreg = (void *)(uncsc->sc_addr + UNIN_CLOCKCNTL);
502 	tmpl = inl(clkreg);
503 	tmpl |= UNIN_CLOCKCNTL_GMAC;
504 	outl(clkreg, tmpl);
505 }
506 
507 static int
508 unin_chip_probe(device_t dev)
509 {
510 	char	*name;
511 
512 	name = nexus_get_name(dev);
513 
514 	if (name == NULL)
515 		return (ENXIO);
516 
517 	if (strcmp(name, "uni-n") != 0)
518 		return (ENXIO);
519 
520 	device_set_desc(dev, "Apple UniNorth System Controller");
521 	return (0);
522 }
523 
524 static int
525 unin_chip_attach(device_t dev)
526 {
527 	phandle_t node;
528 	u_int reg[2];
529 
530 	uncsc = device_get_softc(dev);
531 	node = nexus_get_node(dev);
532 
533 	if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)
534 		return (ENXIO);
535 
536 	uncsc->sc_physaddr = reg[0];
537 	uncsc->sc_size = reg[1];
538 
539 	/*
540 	 * Only map the first page, since that is where the registers
541 	 * of interest lie.
542 	 */
543 	uncsc->sc_addr = (vm_offset_t) pmap_mapdev(reg[0], PAGE_SIZE);
544 
545 	uncsc->sc_version = *(u_int *)uncsc->sc_addr;
546 	device_printf(dev, "Version %d\n", uncsc->sc_version);
547 
548 	return (0);
549 }
550 
551 static device_method_t unin_chip_methods[] = {
552 	/* Device interface */
553 	DEVMETHOD(device_probe,         unin_chip_probe),
554 	DEVMETHOD(device_attach,        unin_chip_attach),
555 
556 	{ 0, 0 }
557 };
558 
559 static driver_t	unin_chip_driver = {
560 	"unin",
561 	unin_chip_methods,
562 	sizeof(struct unin_chip_softc)
563 };
564 
565 static devclass_t	unin_chip_devclass;
566 
567 DRIVER_MODULE(unin, nexus, unin_chip_driver, unin_chip_devclass, 0, 0);
568 
569 
570 
571 
572