]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/ofw/ofw_pcibus.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[FreeBSD/FreeBSD.git] / sys / powerpc / ofw / ofw_pcibus.c
1 /*-
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>
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice unmodified, this list of conditions, and the following
15  *    disclaimer.
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.
19  *
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.
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/kernel.h>
38 #include <sys/libkern.h>
39 #include <sys/module.h>
40 #include <sys/pciio.h>
41
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>
46
47 #include <machine/bus.h>
48 #include <machine/resource.h>
49
50 #include <dev/pci/pcireg.h>
51 #include <dev/pci/pcivar.h>
52 #include <dev/pci/pci_private.h>
53
54 #include "pcib_if.h"
55 #include "pci_if.h"
56
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);
61
62 /* Methods */
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;
67
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),
72
73         /* PCI interface */
74         DEVMETHOD(pci_assign_interrupt, ofw_pcibus_assign_interrupt),
75
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),
83
84         { 0, 0 }
85 };
86
87 struct ofw_pcibus_devinfo {
88         struct pci_devinfo      opd_dinfo;
89         struct ofw_bus_devinfo  opd_obdinfo;
90 };
91
92 static devclass_t pci_devclass;
93
94 DEFINE_CLASS_1(pci, ofw_pcibus_driver, ofw_pcibus_methods, 1 /* no softc */,
95     pci_driver);
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);
99
100 static int
101 ofw_pcibus_probe(device_t dev)
102 {
103         if (ofw_bus_get_node(dev) == 0)
104                 return (ENXIO);
105         device_set_desc(dev, "OFW PCI bus");
106
107         return (0);
108 }
109
110 static int
111 ofw_pcibus_attach(device_t dev)
112 {
113         device_t pcib;
114         struct ofw_pci_register pcir;
115         struct ofw_pcibus_devinfo *dinfo;
116         phandle_t node, child;
117         u_int busno, domain, func, slot;
118
119         pcib = device_get_parent(dev);
120         domain = pcib_get_domain(dev);
121         busno = pcib_get_bus(dev);
122         if (bootverbose)
123                 device_printf(dev, "domain=%d, physical bus=%d\n",
124                     domain, busno);
125         node = ofw_bus_get_node(dev);
126
127         for (child = OF_child(node); child != 0; child = OF_peer(child)) {
128                 if (OF_getprop(child, "reg", &pcir, sizeof(pcir)) == -1)
129                         continue;
130                 slot = OFW_PCI_PHYS_HI_DEVICE(pcir.phys_hi);
131                 func = OFW_PCI_PHYS_HI_FUNCTION(pcir.phys_hi);
132
133                 /* Some OFW device trees contain dupes. */
134                 if (pci_find_dbsf(domain, busno, slot, func) != NULL)
135                         continue;
136
137                 ofw_pci_fixup_node(pcib, child);
138
139                 dinfo = (struct ofw_pcibus_devinfo *)pci_read_device(pcib,
140                     domain, busno, slot, func, sizeof(*dinfo));
141
142                 if (dinfo == NULL)
143                         continue;
144
145                 /* Set up OFW devinfo */
146                 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) !=
147                     0) {
148                         pci_freecfg((struct pci_devinfo *)dinfo);
149                         continue;
150                 }
151
152                 pci_add_child(dev, (struct pci_devinfo *)dinfo);
153
154                 /*
155                  * Some devices don't have an intpin set, but do have
156                  * interrupts. Add them to the appropriate resource list.
157                  */
158                 if (dinfo->opd_dinfo.cfg.intpin == 0) {
159                         uint32_t irqs[4];
160
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);
164                 }
165         }
166
167         return (bus_generic_attach(dev));
168 }
169
170 static int
171 ofw_pcibus_assign_interrupt(device_t dev, device_t child)
172 {
173         uint32_t irqs[4];
174
175         device_printf(child,"Assigning interrupt\n");
176
177         if (ofw_pci_find_intline(ofw_bus_get_node(child), irqs) < 0)
178                 return PCI_INVALID_IRQ;
179
180         device_printf(child,"IRQ %d\n",irqs[0]);
181
182         return irqs[0];
183
184 //      return (PCIB_ROUTE_INTERRUPT(device_get_parent(dev), child, intr));
185 }
186
187 static const struct ofw_bus_devinfo *
188 ofw_pcibus_get_devinfo(device_t bus, device_t dev)
189 {
190         struct ofw_pcibus_devinfo *dinfo;
191
192         dinfo = device_get_ivars(dev);
193         return (&dinfo->opd_obdinfo);
194 }
195
196 static void
197 ofw_pci_fixup_node(device_t dev, phandle_t node)
198 {
199         uint32_t        csr, intr, irqs[4];
200         struct          ofw_pci_register addr[8];
201         int             len, i;
202
203         len = OF_getprop(node, "assigned-addresses", addr, sizeof(addr));
204         if (len < (int)sizeof(struct ofw_pci_register)) {
205                 return;
206         }
207
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);
212
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;
217                         break;
218                 case OFW_PCI_PHYS_HI_SPACE_MEM32:
219                         csr |= PCIM_CMD_MEMEN;
220                         break;
221                 }
222         }
223
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);
227
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);
233                 intr &= ~(0xff);
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,
239                     intr, 2);
240         }
241 }
242
243 static int
244 ofw_pci_find_intline(phandle_t node, uint32_t *irqs)
245 {
246         uint32_t        npintr, paddr[4];
247         struct          ofw_pci_register addr[8];
248         int             len;
249
250         len = OF_getprop(node, "assigned-addresses", addr, sizeof(addr));
251         if (len < (int)sizeof(struct ofw_pci_register)) 
252                 return -1;
253         /*
254          * Create PCI interrupt-map array element. pci-mid/pci-lo
255          * aren't required, but the 'interrupts' property needs
256          * to be appended
257          */
258         npintr = 0;
259         OF_getprop(node, "interrupts", &npintr, 4);
260         paddr[0] = addr[0].phys_hi;
261         paddr[1] = 0;
262         paddr[2] = 0;
263         paddr[3] = npintr;
264
265         return find_node_intr(node, paddr, irqs);
266 }
267
268 static int
269 find_node_intr(phandle_t node, u_int32_t *addr, u_int32_t *intr)
270 {
271         phandle_t       parent, iparent;
272         int             len, mlen, match, i;
273         u_int32_t       map[160], *mp, imask[8], maskedaddr[8], icells;
274         char            name[32];
275
276         len = OF_getprop(node, "AAPL,interrupts", intr, 4);
277         if (len == 4) {
278                 return (len);
279         }
280
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));
284
285         if (len == -1 || mlen == -1)
286                 goto nomap;
287
288         memcpy(maskedaddr, addr, mlen);
289         for (i = 0; i < mlen/4; i++)
290                 maskedaddr[i] &= imask[i];
291
292         mp = map;
293         while (len > mlen) {
294                 match = bcmp(maskedaddr, mp, mlen);
295                 mp += mlen / 4;
296                 len -= mlen;
297
298                 /*
299                  * We must read "#interrupt-cells" for each time because
300                  * interrupt-parent may be different.
301                  */
302                 iparent = *mp++;
303                 len -= 4;
304                 if (OF_getprop(iparent, "#interrupt-cells", &icells, 4) != 4)
305                         goto nomap;
306
307                 /* Found. */
308                 if (match == 0) {
309                         bcopy(mp, intr, icells * 4);
310                         return (icells * 4);
311                 }
312
313                 mp += icells;
314                 len -= icells * 4;
315         }
316
317 nomap:
318         /*
319          * Check for local properties indicating interrupts
320          */
321
322         len = OF_getprop(node, "interrupts", intr, 16);
323         if (OF_getprop(node, "interrupt-parent", &iparent, sizeof(iparent)) ==
324            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];
328
329                 return (len);
330         }
331         
332
333         /*
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.
336          */
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);
341                 if (len == 4) {
342                         return (len);
343                 }
344
345                 /*
346                  * XXX I don't know what is the correct local address.
347                  * XXX Use the first entry for now.
348                  */
349                 len = OF_getprop(parent, "interrupt-map", map, sizeof(map));
350                 if (len >= 36) {
351                         addr = &map[5];
352                         /* XXX Use 0 for 'interrupts' for compat */
353                         return (find_node_intr(parent, addr, intr));
354                 }
355         }
356
357         return (-1);
358 }
359