uninorth.c revision 168200
1193323Sed/*-
2193323Sed * Copyright (C) 2002 Benno Rice.
3193323Sed * All rights reserved.
4193323Sed *
5193323Sed * Redistribution and use in source and binary forms, with or without
6193323Sed * modification, are permitted provided that the following conditions
7193323Sed * are met:
8193323Sed * 1. Redistributions of source code must retain the above copyright
9193323Sed *    notice, this list of conditions and the following disclaimer.
10193323Sed * 2. Redistributions in binary form must reproduce the above copyright
11193323Sed *    notice, this list of conditions and the following disclaimer in the
12249423Sdim *    documentation and/or other materials provided with the distribution.
13251662Sdim *
14193323Sed * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
15249423Sdim * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16193323Sed * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17193323Sed * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18193323Sed * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19193323Sed * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20193323Sed * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21193323Sed * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22202375Srdivacky * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23202375Srdivacky * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24202375Srdivacky *
25203954Srdivacky * $FreeBSD: head/sys/powerpc/powermac/uninorth.c 168200 2007-04-01 06:15:53Z marcel $
26203954Srdivacky */
27193323Sed
28193323Sed#include <sys/param.h>
29202375Srdivacky#include <sys/systm.h>
30202375Srdivacky#include <sys/module.h>
31202375Srdivacky#include <sys/bus.h>
32202375Srdivacky#include <sys/conf.h>
33195340Sed#include <sys/kernel.h>
34249423Sdim
35198090Srdivacky#include <dev/ofw/openfirm.h>
36195340Sed#include <dev/ofw/ofw_pci.h>
37195340Sed
38195340Sed#include <dev/pci/pcivar.h>
39195340Sed#include <dev/pci/pcireg.h>
40195340Sed
41195340Sed#include <machine/bus.h>
42249423Sdim#include <machine/md_var.h>
43195340Sed#include <machine/nexusvar.h>
44195340Sed#include <machine/resource.h>
45195340Sed
46193323Sed#include <sys/rman.h>
47193323Sed
48249423Sdim#include <powerpc/ofw/ofw_pci.h>
49204642Srdivacky#include <powerpc/powermac/uninorthvar.h>
50204642Srdivacky
51204642Srdivacky#include <vm/vm.h>
52204642Srdivacky#include <vm/pmap.h>
53195340Sed
54249423Sdim#include "pcib_if.h"
55204642Srdivacky
56204642Srdivacky#define	UNINORTH_DEBUG	0
57204642Srdivacky
58193323Sed/*
59193323Sed * Device interface.
60193323Sed */
61193323Sedstatic int		uninorth_probe(device_t);
62249423Sdimstatic int		uninorth_attach(device_t);
63193323Sed
64204642Srdivacky/*
65193323Sed * Bus interface.
66204642Srdivacky */
67204642Srdivackystatic int		uninorth_read_ivar(device_t, device_t, int,
68204642Srdivacky			    uintptr_t *);
69204642Srdivackystatic struct		resource * uninorth_alloc_resource(device_t bus,
70204642Srdivacky			    device_t child, int type, int *rid, u_long start,
71204642Srdivacky			    u_long end, u_long count, u_int flags);
72204642Srdivackystatic int		uninorth_activate_resource(device_t bus, device_t child,
73204642Srdivacky			    int type, int rid, struct resource *res);
74204642Srdivacky
75204642Srdivacky/*
76204642Srdivacky * pcib interface.
77204642Srdivacky */
78204642Srdivackystatic int		uninorth_maxslots(device_t);
79204642Srdivackystatic u_int32_t	uninorth_read_config(device_t, u_int, u_int, u_int,
80204642Srdivacky			    u_int, int);
81204642Srdivackystatic void		uninorth_write_config(device_t, u_int, u_int, u_int,
82204642Srdivacky			    u_int, u_int32_t, int);
83204642Srdivackystatic int		uninorth_route_interrupt(device_t, device_t, int);
84204642Srdivacky
85204642Srdivacky/*
86204642Srdivacky * Local routines.
87204642Srdivacky */
88204642Srdivackystatic int		uninorth_enable_config(struct uninorth_softc *, u_int,
89204642Srdivacky			    u_int, u_int, u_int);
90static void		unin_enable_gmac(void);
91
92/*
93 * Driver methods.
94 */
95static device_method_t	uninorth_methods[] = {
96	/* Device interface */
97	DEVMETHOD(device_probe,		uninorth_probe),
98	DEVMETHOD(device_attach,	uninorth_attach),
99
100	/* Bus interface */
101	DEVMETHOD(bus_print_child,	bus_generic_print_child),
102	DEVMETHOD(bus_read_ivar,	uninorth_read_ivar),
103	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
104	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
105	DEVMETHOD(bus_alloc_resource,	uninorth_alloc_resource),
106	DEVMETHOD(bus_activate_resource,	uninorth_activate_resource),
107
108	/* pcib interface */
109	DEVMETHOD(pcib_maxslots,	uninorth_maxslots),
110	DEVMETHOD(pcib_read_config,	uninorth_read_config),
111	DEVMETHOD(pcib_write_config,	uninorth_write_config),
112	DEVMETHOD(pcib_route_interrupt,	uninorth_route_interrupt),
113
114	{ 0, 0 }
115};
116
117static driver_t	uninorth_driver = {
118	"pcib",
119	uninorth_methods,
120	sizeof(struct uninorth_softc)
121};
122
123static devclass_t	uninorth_devclass;
124
125DRIVER_MODULE(uninorth, nexus, uninorth_driver, uninorth_devclass, 0, 0);
126
127static int
128uninorth_probe(device_t dev)
129{
130	char	*type, *compatible;
131
132	type = nexus_get_device_type(dev);
133	compatible = nexus_get_compatible(dev);
134
135	if (type == NULL || compatible == NULL)
136		return (ENXIO);
137
138	if (strcmp(type, "pci") != 0 || strcmp(compatible, "uni-north") != 0)
139		return (ENXIO);
140
141	device_set_desc(dev, "Apple UniNorth Host-PCI bridge");
142	return (0);
143}
144
145static int
146uninorth_attach(device_t dev)
147{
148	struct		uninorth_softc *sc;
149	phandle_t	node;
150	phandle_t	child;
151	u_int32_t	reg[2], busrange[2];
152	struct		uninorth_range *rp, *io, *mem[2];
153	int		nmem, i;
154
155	node = nexus_get_node(dev);
156	sc = device_get_softc(dev);
157
158	if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)
159		return (ENXIO);
160
161	if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
162		return (ENXIO);
163
164	sc->sc_dev = dev;
165	sc->sc_node = node;
166	sc->sc_addr = (vm_offset_t)pmap_mapdev(reg[0] + 0x800000, PAGE_SIZE);
167	sc->sc_data = (vm_offset_t)pmap_mapdev(reg[0] + 0xc00000, PAGE_SIZE);
168	sc->sc_bus = busrange[0];
169
170	bzero(sc->sc_range, sizeof(sc->sc_range));
171	sc->sc_nrange = OF_getprop(node, "ranges", sc->sc_range,
172	    sizeof(sc->sc_range));
173
174	if (sc->sc_nrange == -1) {
175		device_printf(dev, "could not get ranges\n");
176		return (ENXIO);
177	}
178
179	sc->sc_range[6].pci_hi = 0;
180	io = NULL;
181	nmem = 0;
182
183	for (rp = sc->sc_range; rp->pci_hi != 0; rp++) {
184		switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
185		case OFW_PCI_PHYS_HI_SPACE_CONFIG:
186			break;
187		case OFW_PCI_PHYS_HI_SPACE_IO:
188			io = rp;
189			break;
190		case OFW_PCI_PHYS_HI_SPACE_MEM32:
191			mem[nmem] = rp;
192			nmem++;
193			break;
194		case OFW_PCI_PHYS_HI_SPACE_MEM64:
195			break;
196		}
197	}
198
199	if (io == NULL) {
200		device_printf(dev, "can't find io range\n");
201		return (ENXIO);
202	}
203	sc->sc_io_rman.rm_type = RMAN_ARRAY;
204	sc->sc_io_rman.rm_descr = "UniNorth PCI I/O Ports";
205	sc->sc_iostart = io->host;
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 - 1) != 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 - 1) != 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 Open Firmware 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
258static int
259uninorth_maxslots(device_t dev)
260{
261
262	return (PCI_SLOTMAX);
263}
264
265static u_int32_t
266uninorth_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
292static void
293uninorth_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			break;
307		case 2:
308			out16rb(caoff, val);
309			break;
310		case 4:
311			out32rb(caoff, val);
312			break;
313		}
314	}
315}
316
317static int
318uninorth_route_interrupt(device_t bus, device_t dev, int pin)
319{
320
321	return (0);
322}
323
324static int
325uninorth_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
326{
327	struct	uninorth_softc *sc;
328
329	sc = device_get_softc(dev);
330
331	switch (which) {
332	case PCIB_IVAR_BUS:
333		*result = sc->sc_bus;
334		return (0);
335		break;
336	}
337
338	return (ENOENT);
339}
340
341static struct resource *
342uninorth_alloc_resource(device_t bus, device_t child, int type, int *rid,
343    u_long start, u_long end, u_long count, u_int flags)
344{
345	struct			uninorth_softc *sc;
346	struct			resource *rv;
347	struct			rman *rm;
348	bus_space_tag_t		bt;
349	int			needactivate;
350
351	needactivate = flags & RF_ACTIVE;
352	flags &= ~RF_ACTIVE;
353
354	sc = device_get_softc(bus);
355
356	switch (type) {
357	case SYS_RES_MEMORY:
358		rm = &sc->sc_mem_rman;
359		bt = PPC_BUS_SPACE_MEM;
360		break;
361
362	case SYS_RES_IOPORT:
363		rm = &sc->sc_io_rman;
364		bt = PPC_BUS_SPACE_IO;
365		break;
366
367	case SYS_RES_IRQ:
368		return (bus_alloc_resource(bus, type, rid, start, end, count,
369		    flags));
370		break;
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	rman_set_bustag(rv, bt);
386	rman_set_bushandle(rv, rman_get_start(rv));
387
388	if (needactivate) {
389		if (bus_activate_resource(child, type, *rid, rv) != 0) {
390			device_printf(bus,
391			    "failed to activate resource for %s\n",
392			    device_get_nameunit(child));
393			rman_release_resource(rv);
394			return (NULL);
395		}
396	}
397
398	return (rv);
399}
400
401static int
402uninorth_activate_resource(device_t bus, device_t child, int type, int rid,
403    struct resource *res)
404{
405	void	*p;
406	struct	uninorth_softc *sc;
407
408	sc = device_get_softc(bus);
409
410	if (type == SYS_RES_IRQ)
411		return (bus_activate_resource(bus, type, rid, res));
412
413	if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
414		vm_offset_t start;
415
416		start = (vm_offset_t)rman_get_start(res);
417		/*
418		 * For i/o-ports, convert the start address to the
419		 * uninorth PCI i/o window
420		 */
421		if (type == SYS_RES_IOPORT)
422			start += sc->sc_iostart;
423
424		if (bootverbose)
425			printf("uninorth mapdev: start %x, len %ld\n", start,
426			    rman_get_size(res));
427
428		p = pmap_mapdev(start, (vm_size_t)rman_get_size(res));
429		if (p == NULL)
430			return (ENOMEM);
431		rman_set_virtual(res, p);
432		rman_set_bushandle(res, (u_long)p);
433	}
434
435	return (rman_activate_resource(res));
436}
437
438static int
439uninorth_enable_config(struct uninorth_softc *sc, u_int bus, u_int slot,
440    u_int func, u_int reg)
441{
442	uint32_t	cfgval;
443	uint32_t	pass;
444
445	if (resource_int_value(device_get_name(sc->sc_dev),
446	        device_get_unit(sc->sc_dev), "skipslot", &pass) == 0) {
447		if (pass == slot)
448			return (0);
449	}
450
451	if (sc->sc_bus == bus) {
452		/*
453		 * No slots less than 11 on the primary bus
454		 */
455		if (slot < 11)
456			return (0);
457
458		cfgval = (1 << slot) | (func << 8) | (reg & 0xfc);
459	} else {
460		cfgval = (bus << 16) | (slot << 11) | (func << 8) |
461		    (reg & 0xfc) | 1;
462	}
463
464	do {
465		out32rb(sc->sc_addr, cfgval);
466	} while (in32rb(sc->sc_addr) != cfgval);
467
468	return (1);
469}
470
471/*
472 * Driver to swallow UniNorth host bridges from the PCI bus side.
473 */
474static int
475unhb_probe(device_t dev)
476{
477
478	if (pci_get_class(dev) == PCIC_BRIDGE &&
479	    pci_get_subclass(dev) == PCIS_BRIDGE_HOST) {
480		device_set_desc(dev, "Host to PCI bridge");
481		device_quiet(dev);
482		return (-10000);
483	}
484
485	return (ENXIO);
486}
487
488static int
489unhb_attach(device_t dev)
490{
491
492	return (0);
493}
494
495static device_method_t unhb_methods[] = {
496	/* Device interface */
497	DEVMETHOD(device_probe,         unhb_probe),
498	DEVMETHOD(device_attach,        unhb_attach),
499
500	{ 0, 0 }
501};
502
503static driver_t unhb_driver = {
504	"unhb",
505	unhb_methods,
506	1,
507};
508static devclass_t unhb_devclass;
509
510DRIVER_MODULE(unhb, pci, unhb_driver, unhb_devclass, 0, 0);
511
512
513/*
514 * Small stub driver for the Uninorth chip itself, to allow setting
515 * of various parameters and cell enables
516 */
517static struct unin_chip_softc *uncsc;
518
519static void
520unin_enable_gmac(void)
521{
522	volatile u_int *clkreg;
523	u_int32_t tmpl;
524
525	if (uncsc == NULL)
526		panic("unin_enable_gmac: device not found");
527
528	clkreg = (void *)(uncsc->sc_addr + UNIN_CLOCKCNTL);
529	tmpl = inl(clkreg);
530	tmpl |= UNIN_CLOCKCNTL_GMAC;
531	outl(clkreg, tmpl);
532}
533
534static int
535unin_chip_probe(device_t dev)
536{
537	char	*name;
538
539	name = nexus_get_name(dev);
540
541	if (name == NULL)
542		return (ENXIO);
543
544	if (strcmp(name, "uni-n") != 0)
545		return (ENXIO);
546
547	device_set_desc(dev, "Apple UniNorth System Controller");
548	return (0);
549}
550
551static int
552unin_chip_attach(device_t dev)
553{
554	phandle_t node;
555	u_int reg[2];
556
557	uncsc = device_get_softc(dev);
558	node = nexus_get_node(dev);
559
560	if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)
561		return (ENXIO);
562
563	uncsc->sc_physaddr = reg[0];
564	uncsc->sc_size = reg[1];
565
566	/*
567	 * Only map the first page, since that is where the registers
568	 * of interest lie.
569	 */
570	uncsc->sc_addr = (vm_offset_t) pmap_mapdev(reg[0], PAGE_SIZE);
571
572	uncsc->sc_version = *(u_int *)uncsc->sc_addr;
573	device_printf(dev, "Version %d\n", uncsc->sc_version);
574
575	return (0);
576}
577
578static device_method_t unin_chip_methods[] = {
579	/* Device interface */
580	DEVMETHOD(device_probe,         unin_chip_probe),
581	DEVMETHOD(device_attach,        unin_chip_attach),
582
583	{ 0, 0 }
584};
585
586static driver_t	unin_chip_driver = {
587	"unin",
588	unin_chip_methods,
589	sizeof(struct unin_chip_softc)
590};
591
592static devclass_t	unin_chip_devclass;
593
594DRIVER_MODULE(unin, nexus, unin_chip_driver, unin_chip_devclass, 0, 0);
595
596
597
598
599