xref: /freebsd/sys/powerpc/powermac/uninorthpci.c (revision 9a35e64eb4b7d65b687e954bc5d0abd73725d60e)
1ca2c1931SNathan Whitehorn /*-
2ca2c1931SNathan Whitehorn  * Copyright (C) 2002 Benno Rice.
3ca2c1931SNathan Whitehorn  * All rights reserved.
4ca2c1931SNathan Whitehorn  *
5ca2c1931SNathan Whitehorn  * Redistribution and use in source and binary forms, with or without
6ca2c1931SNathan Whitehorn  * modification, are permitted provided that the following conditions
7ca2c1931SNathan Whitehorn  * are met:
8ca2c1931SNathan Whitehorn  * 1. Redistributions of source code must retain the above copyright
9ca2c1931SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer.
10ca2c1931SNathan Whitehorn  * 2. Redistributions in binary form must reproduce the above copyright
11ca2c1931SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer in the
12ca2c1931SNathan Whitehorn  *    documentation and/or other materials provided with the distribution.
13ca2c1931SNathan Whitehorn  *
14ca2c1931SNathan Whitehorn  * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
15ca2c1931SNathan Whitehorn  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16ca2c1931SNathan Whitehorn  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17ca2c1931SNathan Whitehorn  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18ca2c1931SNathan Whitehorn  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19ca2c1931SNathan Whitehorn  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20ca2c1931SNathan Whitehorn  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21ca2c1931SNathan Whitehorn  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22ca2c1931SNathan Whitehorn  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23ca2c1931SNathan Whitehorn  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24ca2c1931SNathan Whitehorn  */
25ca2c1931SNathan Whitehorn 
264b7ec270SMarius Strobl #include <sys/cdefs.h>
274b7ec270SMarius Strobl __FBSDID("$FreeBSD$");
284b7ec270SMarius Strobl 
29ca2c1931SNathan Whitehorn #include <sys/param.h>
30ca2c1931SNathan Whitehorn #include <sys/systm.h>
31ca2c1931SNathan Whitehorn #include <sys/module.h>
32ca2c1931SNathan Whitehorn #include <sys/bus.h>
33ca2c1931SNathan Whitehorn #include <sys/conf.h>
34ca2c1931SNathan Whitehorn #include <sys/kernel.h>
35ca2c1931SNathan Whitehorn 
36ca2c1931SNathan Whitehorn #include <dev/ofw/openfirm.h>
37ca2c1931SNathan Whitehorn #include <dev/ofw/ofw_pci.h>
38ca2c1931SNathan Whitehorn #include <dev/ofw/ofw_bus.h>
39ca2c1931SNathan Whitehorn #include <dev/ofw/ofw_bus_subr.h>
40ca2c1931SNathan Whitehorn 
41ca2c1931SNathan Whitehorn #include <dev/pci/pcivar.h>
42ca2c1931SNathan Whitehorn #include <dev/pci/pcireg.h>
43ca2c1931SNathan Whitehorn 
44ca2c1931SNathan Whitehorn #include <machine/bus.h>
45ca2c1931SNathan Whitehorn #include <machine/intr_machdep.h>
46ca2c1931SNathan Whitehorn #include <machine/md_var.h>
47ca2c1931SNathan Whitehorn #include <machine/pio.h>
48ca2c1931SNathan Whitehorn #include <machine/resource.h>
49ca2c1931SNathan Whitehorn 
50ca2c1931SNathan Whitehorn #include <sys/rman.h>
51ca2c1931SNathan Whitehorn 
52*9a35e64eSNathan Whitehorn #include <powerpc/ofw/ofw_pci.h>
53ca2c1931SNathan Whitehorn #include <powerpc/powermac/uninorthvar.h>
54ca2c1931SNathan Whitehorn 
55ca2c1931SNathan Whitehorn #include <vm/vm.h>
56ca2c1931SNathan Whitehorn #include <vm/pmap.h>
57ca2c1931SNathan Whitehorn 
58ca2c1931SNathan Whitehorn #include "pcib_if.h"
59ca2c1931SNathan Whitehorn 
60ca2c1931SNathan Whitehorn #define	UNINORTH_DEBUG	0
61ca2c1931SNathan Whitehorn 
62ca2c1931SNathan Whitehorn /*
63ca2c1931SNathan Whitehorn  * Device interface.
64ca2c1931SNathan Whitehorn  */
65ca2c1931SNathan Whitehorn static int		uninorth_probe(device_t);
66ca2c1931SNathan Whitehorn static int		uninorth_attach(device_t);
67ca2c1931SNathan Whitehorn 
68ca2c1931SNathan Whitehorn /*
69ca2c1931SNathan Whitehorn  * pcib interface.
70ca2c1931SNathan Whitehorn  */
71ca2c1931SNathan Whitehorn static u_int32_t	uninorth_read_config(device_t, u_int, u_int, u_int,
72ca2c1931SNathan Whitehorn 			    u_int, int);
73ca2c1931SNathan Whitehorn static void		uninorth_write_config(device_t, u_int, u_int, u_int,
74ca2c1931SNathan Whitehorn 			    u_int, u_int32_t, int);
75ca2c1931SNathan Whitehorn 
76ca2c1931SNathan Whitehorn /*
77ca2c1931SNathan Whitehorn  * Local routines.
78ca2c1931SNathan Whitehorn  */
79ca2c1931SNathan Whitehorn static int		uninorth_enable_config(struct uninorth_softc *, u_int,
80ca2c1931SNathan Whitehorn 			    u_int, u_int, u_int);
81ca2c1931SNathan Whitehorn 
82ca2c1931SNathan Whitehorn /*
83ca2c1931SNathan Whitehorn  * Driver methods.
84ca2c1931SNathan Whitehorn  */
85ca2c1931SNathan Whitehorn static device_method_t	uninorth_methods[] = {
86ca2c1931SNathan Whitehorn 	/* Device interface */
87ca2c1931SNathan Whitehorn 	DEVMETHOD(device_probe,		uninorth_probe),
88ca2c1931SNathan Whitehorn 	DEVMETHOD(device_attach,	uninorth_attach),
89ca2c1931SNathan Whitehorn 
90ca2c1931SNathan Whitehorn 	/* pcib interface */
91ca2c1931SNathan Whitehorn 	DEVMETHOD(pcib_read_config,	uninorth_read_config),
92ca2c1931SNathan Whitehorn 	DEVMETHOD(pcib_write_config,	uninorth_write_config),
93ca2c1931SNathan Whitehorn 
944b7ec270SMarius Strobl 	DEVMETHOD_END
95ca2c1931SNathan Whitehorn };
96ca2c1931SNathan Whitehorn 
97ca2c1931SNathan Whitehorn static devclass_t	uninorth_devclass;
98ca2c1931SNathan Whitehorn 
99*9a35e64eSNathan Whitehorn DEFINE_CLASS_1(pcib, uninorth_driver, uninorth_methods,
100*9a35e64eSNathan Whitehorn     sizeof(struct uninorth_softc), ofw_pci_driver);
101ca2c1931SNathan Whitehorn DRIVER_MODULE(uninorth, nexus, uninorth_driver, uninorth_devclass, 0, 0);
102ca2c1931SNathan Whitehorn 
103ca2c1931SNathan Whitehorn static int
104ca2c1931SNathan Whitehorn uninorth_probe(device_t dev)
105ca2c1931SNathan Whitehorn {
106ca2c1931SNathan Whitehorn 	const char	*type, *compatible;
107ca2c1931SNathan Whitehorn 
108ca2c1931SNathan Whitehorn 	type = ofw_bus_get_type(dev);
109ca2c1931SNathan Whitehorn 	compatible = ofw_bus_get_compat(dev);
110ca2c1931SNathan Whitehorn 
111ca2c1931SNathan Whitehorn 	if (type == NULL || compatible == NULL)
112ca2c1931SNathan Whitehorn 		return (ENXIO);
113ca2c1931SNathan Whitehorn 
114ca2c1931SNathan Whitehorn 	if (strcmp(type, "pci") != 0)
115ca2c1931SNathan Whitehorn 		return (ENXIO);
116ca2c1931SNathan Whitehorn 
117ca2c1931SNathan Whitehorn 	if (strcmp(compatible, "uni-north") == 0) {
118ca2c1931SNathan Whitehorn 		device_set_desc(dev, "Apple UniNorth Host-PCI bridge");
119ca2c1931SNathan Whitehorn 		return (0);
120ca2c1931SNathan Whitehorn 	} else if (strcmp(compatible, "u3-agp") == 0) {
121ca2c1931SNathan Whitehorn 		device_set_desc(dev, "Apple U3 Host-AGP bridge");
122ca2c1931SNathan Whitehorn 		return (0);
123ca2c1931SNathan Whitehorn 	} else if (strcmp(compatible, "u4-pcie") == 0) {
124ca2c1931SNathan Whitehorn 		device_set_desc(dev, "IBM CPC945 PCI Express Root");
125ca2c1931SNathan Whitehorn 		return (0);
126ca2c1931SNathan Whitehorn 	}
127ca2c1931SNathan Whitehorn 
128ca2c1931SNathan Whitehorn 	return (ENXIO);
129ca2c1931SNathan Whitehorn }
130ca2c1931SNathan Whitehorn 
131ca2c1931SNathan Whitehorn static int
132ca2c1931SNathan Whitehorn uninorth_attach(device_t dev)
133ca2c1931SNathan Whitehorn {
134ca2c1931SNathan Whitehorn 	struct		uninorth_softc *sc;
135ca2c1931SNathan Whitehorn 	const char	*compatible;
136ca2c1931SNathan Whitehorn 	phandle_t	node;
137*9a35e64eSNathan Whitehorn 	u_int32_t	reg[3];
138ca2c1931SNathan Whitehorn 
139ca2c1931SNathan Whitehorn 	node = ofw_bus_get_node(dev);
140ca2c1931SNathan Whitehorn 	sc = device_get_softc(dev);
141ca2c1931SNathan Whitehorn 
142ca2c1931SNathan Whitehorn 	if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)
143ca2c1931SNathan Whitehorn 		return (ENXIO);
144ca2c1931SNathan Whitehorn 
145ca2c1931SNathan Whitehorn 	sc->sc_ver = 0;
146ca2c1931SNathan Whitehorn 	compatible = ofw_bus_get_compat(dev);
147ca2c1931SNathan Whitehorn 	if (strcmp(compatible, "u3-agp") == 0)
148ca2c1931SNathan Whitehorn 		sc->sc_ver = 3;
149ca2c1931SNathan Whitehorn 	if (strcmp(compatible, "u4-pcie") == 0)
150ca2c1931SNathan Whitehorn 		sc->sc_ver = 4;
151ca2c1931SNathan Whitehorn 
152ca2c1931SNathan Whitehorn 	if (sc->sc_ver >= 3) {
153ca2c1931SNathan Whitehorn 	   sc->sc_addr = (vm_offset_t)pmap_mapdev(reg[1] + 0x800000, PAGE_SIZE);
154ca2c1931SNathan Whitehorn 	   sc->sc_data = (vm_offset_t)pmap_mapdev(reg[1] + 0xc00000, PAGE_SIZE);
155ca2c1931SNathan Whitehorn 	} else {
156ca2c1931SNathan Whitehorn 	   sc->sc_addr = (vm_offset_t)pmap_mapdev(reg[0] + 0x800000, PAGE_SIZE);
157ca2c1931SNathan Whitehorn 	   sc->sc_data = (vm_offset_t)pmap_mapdev(reg[0] + 0xc00000, PAGE_SIZE);
158ca2c1931SNathan Whitehorn 	}
159ca2c1931SNathan Whitehorn 
160*9a35e64eSNathan Whitehorn 	return (ofw_pci_attach(dev));
161ca2c1931SNathan Whitehorn }
162ca2c1931SNathan Whitehorn 
163ca2c1931SNathan Whitehorn static u_int32_t
164ca2c1931SNathan Whitehorn uninorth_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
165ca2c1931SNathan Whitehorn     int width)
166ca2c1931SNathan Whitehorn {
167ca2c1931SNathan Whitehorn 	struct		uninorth_softc *sc;
168ca2c1931SNathan Whitehorn 	vm_offset_t	caoff;
169ca2c1931SNathan Whitehorn 
170ca2c1931SNathan Whitehorn 	sc = device_get_softc(dev);
171ca2c1931SNathan Whitehorn 	caoff = sc->sc_data + (reg & 0x07);
172ca2c1931SNathan Whitehorn 
173ca2c1931SNathan Whitehorn 	if (uninorth_enable_config(sc, bus, slot, func, reg) != 0) {
174ca2c1931SNathan Whitehorn 		switch (width) {
175ca2c1931SNathan Whitehorn 		case 1:
176ca2c1931SNathan Whitehorn 			return (in8rb(caoff));
177ca2c1931SNathan Whitehorn 			break;
178ca2c1931SNathan Whitehorn 		case 2:
179ca2c1931SNathan Whitehorn 			return (in16rb(caoff));
180ca2c1931SNathan Whitehorn 			break;
181ca2c1931SNathan Whitehorn 		case 4:
182ca2c1931SNathan Whitehorn 			return (in32rb(caoff));
183ca2c1931SNathan Whitehorn 			break;
184ca2c1931SNathan Whitehorn 		}
185ca2c1931SNathan Whitehorn 	}
186ca2c1931SNathan Whitehorn 
187ca2c1931SNathan Whitehorn 	return (0xffffffff);
188ca2c1931SNathan Whitehorn }
189ca2c1931SNathan Whitehorn 
190ca2c1931SNathan Whitehorn static void
191ca2c1931SNathan Whitehorn uninorth_write_config(device_t dev, u_int bus, u_int slot, u_int func,
192ca2c1931SNathan Whitehorn     u_int reg, u_int32_t val, int width)
193ca2c1931SNathan Whitehorn {
194ca2c1931SNathan Whitehorn 	struct		uninorth_softc *sc;
195ca2c1931SNathan Whitehorn 	vm_offset_t	caoff;
196ca2c1931SNathan Whitehorn 
197ca2c1931SNathan Whitehorn 	sc = device_get_softc(dev);
198ca2c1931SNathan Whitehorn 	caoff = sc->sc_data + (reg & 0x07);
199ca2c1931SNathan Whitehorn 
200ca2c1931SNathan Whitehorn 	if (uninorth_enable_config(sc, bus, slot, func, reg)) {
201ca2c1931SNathan Whitehorn 		switch (width) {
202ca2c1931SNathan Whitehorn 		case 1:
203ca2c1931SNathan Whitehorn 			out8rb(caoff, val);
204ca2c1931SNathan Whitehorn 			break;
205ca2c1931SNathan Whitehorn 		case 2:
206ca2c1931SNathan Whitehorn 			out16rb(caoff, val);
207ca2c1931SNathan Whitehorn 			break;
208ca2c1931SNathan Whitehorn 		case 4:
209ca2c1931SNathan Whitehorn 			out32rb(caoff, val);
210ca2c1931SNathan Whitehorn 			break;
211ca2c1931SNathan Whitehorn 		}
212ca2c1931SNathan Whitehorn 	}
213ca2c1931SNathan Whitehorn }
214ca2c1931SNathan Whitehorn 
215ca2c1931SNathan Whitehorn static int
216ca2c1931SNathan Whitehorn uninorth_enable_config(struct uninorth_softc *sc, u_int bus, u_int slot,
217ca2c1931SNathan Whitehorn     u_int func, u_int reg)
218ca2c1931SNathan Whitehorn {
219ca2c1931SNathan Whitehorn 	uint32_t	cfgval;
220ca2c1931SNathan Whitehorn 	uint32_t	pass;
221ca2c1931SNathan Whitehorn 
222*9a35e64eSNathan Whitehorn 	if (resource_int_value(device_get_name(sc->pci_sc.sc_dev),
223*9a35e64eSNathan Whitehorn 	        device_get_unit(sc->pci_sc.sc_dev), "skipslot", &pass) == 0) {
224ca2c1931SNathan Whitehorn 		if (pass == slot)
225ca2c1931SNathan Whitehorn 			return (0);
226ca2c1931SNathan Whitehorn 	}
227ca2c1931SNathan Whitehorn 
228ca2c1931SNathan Whitehorn 	/*
229ca2c1931SNathan Whitehorn 	 * Issue type 0 configuration space accesses for the root bus.
230ca2c1931SNathan Whitehorn 	 *
231ca2c1931SNathan Whitehorn 	 * NOTE: On U4, issue only type 1 accesses. There is a secret
232ca2c1931SNathan Whitehorn 	 * PCI Express <-> PCI Express bridge not present in the device tree,
233ca2c1931SNathan Whitehorn 	 * and we need to route all of our configuration space through it.
234ca2c1931SNathan Whitehorn 	 */
235*9a35e64eSNathan Whitehorn 	if (sc->pci_sc.sc_bus == bus && sc->sc_ver < 4) {
236ca2c1931SNathan Whitehorn 		/*
237ca2c1931SNathan Whitehorn 		 * No slots less than 11 on the primary bus on U3 and lower
238ca2c1931SNathan Whitehorn 		 */
239ca2c1931SNathan Whitehorn 		if (slot < 11)
240ca2c1931SNathan Whitehorn 			return (0);
241ca2c1931SNathan Whitehorn 
242ca2c1931SNathan Whitehorn 		cfgval = (1 << slot) | (func << 8) | (reg & 0xfc);
243ca2c1931SNathan Whitehorn 	} else {
244ca2c1931SNathan Whitehorn 		cfgval = (bus << 16) | (slot << 11) | (func << 8) |
245ca2c1931SNathan Whitehorn 		    (reg & 0xfc) | 1;
246ca2c1931SNathan Whitehorn 	}
247ca2c1931SNathan Whitehorn 
248ca2c1931SNathan Whitehorn 	/* Set extended register bits on U4 */
249ca2c1931SNathan Whitehorn 	if (sc->sc_ver == 4)
250ca2c1931SNathan Whitehorn 		cfgval |= (reg >> 8) << 28;
251ca2c1931SNathan Whitehorn 
252ca2c1931SNathan Whitehorn 	do {
253ca2c1931SNathan Whitehorn 		out32rb(sc->sc_addr, cfgval);
254ca2c1931SNathan Whitehorn 	} while (in32rb(sc->sc_addr) != cfgval);
255ca2c1931SNathan Whitehorn 
256ca2c1931SNathan Whitehorn 	return (1);
257ca2c1931SNathan Whitehorn }
258ca2c1931SNathan Whitehorn 
259