2 * Copyright (c) 1994 Charles M. Hannum. All rights reserved.
3 * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
4 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
5 * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
6 * Copyright (c) 2000, BSDi
7 * Copyright (c) 2003, Thomas Moestl <tmm@FreeBSD.org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice unmodified, this list of conditions, and the following
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/libkern.h>
39 #include <sys/module.h>
40 #include <sys/pciio.h>
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/ofw_bus_subr.h>
44 #include <dev/ofw/ofw_pci.h>
45 #include <dev/ofw/openfirm.h>
47 #include <machine/bus.h>
48 #include <machine/resource.h>
50 #include <dev/pci/pcireg.h>
51 #include <dev/pci/pcivar.h>
52 #include <dev/pci/pci_private.h>
57 /* Helper functions */
58 static int find_node_intr(phandle_t, u_int32_t *, u_int32_t *);
59 static int ofw_pci_find_intline(phandle_t node, uint32_t *irqs);
60 static void ofw_pci_fixup_node(device_t dev, phandle_t node);
63 static device_probe_t ofw_pcibus_probe;
64 static device_attach_t ofw_pcibus_attach;
65 static pci_assign_interrupt_t ofw_pcibus_assign_interrupt;
66 static ofw_bus_get_devinfo_t ofw_pcibus_get_devinfo;
68 static device_method_t ofw_pcibus_methods[] = {
69 /* Device interface */
70 DEVMETHOD(device_probe, ofw_pcibus_probe),
71 DEVMETHOD(device_attach, ofw_pcibus_attach),
74 DEVMETHOD(pci_assign_interrupt, ofw_pcibus_assign_interrupt),
76 /* ofw_bus interface */
77 DEVMETHOD(ofw_bus_get_devinfo, ofw_pcibus_get_devinfo),
78 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
79 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
80 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
81 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
82 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
87 struct ofw_pcibus_devinfo {
88 struct pci_devinfo opd_dinfo;
89 struct ofw_bus_devinfo opd_obdinfo;
92 static devclass_t pci_devclass;
94 DEFINE_CLASS_1(pci, ofw_pcibus_driver, ofw_pcibus_methods, 1 /* no softc */,
96 DRIVER_MODULE(ofw_pcibus, pcib, ofw_pcibus_driver, pci_devclass, 0, 0);
97 MODULE_VERSION(ofw_pcibus, 1);
98 MODULE_DEPEND(ofw_pcibus, pci, 1, 1, 1);
101 ofw_pcibus_probe(device_t dev)
103 if (ofw_bus_get_node(dev) == 0)
105 device_set_desc(dev, "OFW PCI bus");
111 ofw_pcibus_attach(device_t dev)
114 struct ofw_pci_register pcir;
115 struct ofw_pcibus_devinfo *dinfo;
116 phandle_t node, child;
117 u_int busno, domain, func, slot;
119 pcib = device_get_parent(dev);
120 domain = pcib_get_domain(dev);
121 busno = pcib_get_bus(dev);
123 device_printf(dev, "domain=%d, physical bus=%d\n",
125 node = ofw_bus_get_node(dev);
127 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
128 if (OF_getprop(child, "reg", &pcir, sizeof(pcir)) == -1)
130 slot = OFW_PCI_PHYS_HI_DEVICE(pcir.phys_hi);
131 func = OFW_PCI_PHYS_HI_FUNCTION(pcir.phys_hi);
133 /* Some OFW device trees contain dupes. */
134 if (pci_find_dbsf(domain, busno, slot, func) != NULL)
137 ofw_pci_fixup_node(pcib, child);
139 dinfo = (struct ofw_pcibus_devinfo *)pci_read_device(pcib,
140 domain, busno, slot, func, sizeof(*dinfo));
145 /* Set up OFW devinfo */
146 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) !=
148 pci_freecfg((struct pci_devinfo *)dinfo);
152 pci_add_child(dev, (struct pci_devinfo *)dinfo);
155 * Some devices don't have an intpin set, but do have
156 * interrupts. Add them to the appropriate resource list.
158 if (dinfo->opd_dinfo.cfg.intpin == 0) {
161 if (ofw_pci_find_intline(child, irqs) > 0)
162 resource_list_add(&dinfo->opd_dinfo.resources,
163 SYS_RES_IRQ, 0, irqs[0], irqs[0], 1);
167 return (bus_generic_attach(dev));
171 ofw_pcibus_assign_interrupt(device_t dev, device_t child)
175 device_printf(child,"Assigning interrupt\n");
177 if (ofw_pci_find_intline(ofw_bus_get_node(child), irqs) < 0)
178 return PCI_INVALID_IRQ;
180 device_printf(child,"IRQ %d\n",irqs[0]);
184 // return (PCIB_ROUTE_INTERRUPT(device_get_parent(dev), child, intr));
187 static const struct ofw_bus_devinfo *
188 ofw_pcibus_get_devinfo(device_t bus, device_t dev)
190 struct ofw_pcibus_devinfo *dinfo;
192 dinfo = device_get_ivars(dev);
193 return (&dinfo->opd_obdinfo);
197 ofw_pci_fixup_node(device_t dev, phandle_t node)
199 uint32_t csr, intr, irqs[4];
200 struct ofw_pci_register addr[8];
203 len = OF_getprop(node, "assigned-addresses", addr, sizeof(addr));
204 if (len < (int)sizeof(struct ofw_pci_register)) {
208 csr = PCIB_READ_CONFIG(dev, OFW_PCI_PHYS_HI_BUS(addr[0].phys_hi),
209 OFW_PCI_PHYS_HI_DEVICE(addr[0].phys_hi),
210 OFW_PCI_PHYS_HI_FUNCTION(addr[0].phys_hi), PCIR_COMMAND, 4);
211 csr &= ~(PCIM_CMD_PORTEN | PCIM_CMD_MEMEN);
213 for (i = 0; i < len / sizeof(struct ofw_pci_register); i++) {
214 switch (addr[i].phys_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
215 case OFW_PCI_PHYS_HI_SPACE_IO:
216 csr |= PCIM_CMD_PORTEN;
218 case OFW_PCI_PHYS_HI_SPACE_MEM32:
219 csr |= PCIM_CMD_MEMEN;
224 PCIB_WRITE_CONFIG(dev, OFW_PCI_PHYS_HI_BUS(addr[0].phys_hi),
225 OFW_PCI_PHYS_HI_DEVICE(addr[0].phys_hi),
226 OFW_PCI_PHYS_HI_FUNCTION(addr[0].phys_hi), PCIR_COMMAND, csr, 4);
228 if (ofw_pci_find_intline(node, irqs) != -1) {
229 intr = PCIB_READ_CONFIG(dev,
230 OFW_PCI_PHYS_HI_BUS(addr[0].phys_hi),
231 OFW_PCI_PHYS_HI_DEVICE(addr[0].phys_hi),
232 OFW_PCI_PHYS_HI_FUNCTION(addr[0].phys_hi), PCIR_INTLINE, 2);
234 intr |= irqs[0] & 0xff;
235 PCIB_WRITE_CONFIG(dev,
236 OFW_PCI_PHYS_HI_BUS(addr[0].phys_hi),
237 OFW_PCI_PHYS_HI_DEVICE(addr[0].phys_hi),
238 OFW_PCI_PHYS_HI_FUNCTION(addr[0].phys_hi), PCIR_INTLINE,
244 ofw_pci_find_intline(phandle_t node, uint32_t *irqs)
246 uint32_t npintr, paddr[4];
247 struct ofw_pci_register addr[8];
250 len = OF_getprop(node, "assigned-addresses", addr, sizeof(addr));
251 if (len < (int)sizeof(struct ofw_pci_register))
254 * Create PCI interrupt-map array element. pci-mid/pci-lo
255 * aren't required, but the 'interrupts' property needs
259 OF_getprop(node, "interrupts", &npintr, 4);
260 paddr[0] = addr[0].phys_hi;
265 return find_node_intr(node, paddr, irqs);
269 find_node_intr(phandle_t node, u_int32_t *addr, u_int32_t *intr)
271 phandle_t parent, iparent;
272 int len, mlen, match, i;
273 u_int32_t map[160], *mp, imask[8], maskedaddr[8], icells;
276 len = OF_getprop(node, "AAPL,interrupts", intr, 4);
281 parent = OF_parent(node);
282 len = OF_getprop(parent, "interrupt-map", map, sizeof(map));
283 mlen = OF_getprop(parent, "interrupt-map-mask", imask, sizeof(imask));
285 if (len == -1 || mlen == -1)
288 memcpy(maskedaddr, addr, mlen);
289 for (i = 0; i < mlen/4; i++)
290 maskedaddr[i] &= imask[i];
294 match = bcmp(maskedaddr, mp, mlen);
299 * We must read "#interrupt-cells" for each time because
300 * interrupt-parent may be different.
304 if (OF_getprop(iparent, "#interrupt-cells", &icells, 4) != 4)
309 bcopy(mp, intr, icells * 4);
319 * Check for local properties indicating interrupts
322 len = OF_getprop(node, "interrupts", intr, 16);
323 if (OF_getprop(node, "interrupt-parent", &iparent, sizeof(iparent)) ==
325 OF_getprop(iparent, "#interrupt-cells", &icells, sizeof(icells));
326 for (i = 0; i < len/icells/4; i++)
327 intr[i] = intr[i*icells];
334 * If the node has no interrupt property and the parent is a PCI
335 * bridge, use the parent's interrupt. This occurs on a PCI slot.
337 bzero(name, sizeof(name));
338 OF_getprop(parent, "name", name, sizeof(name));
339 if (strcmp(name, "pci-bridge") == 0) {
340 len = OF_getprop(parent, "AAPL,interrupts", intr, 4);
346 * XXX I don't know what is the correct local address.
347 * XXX Use the first entry for now.
349 len = OF_getprop(parent, "interrupt-map", map, sizeof(map));
352 /* XXX Use 0 for 'interrupts' for compat */
353 return (find_node_intr(parent, addr, intr));