]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/sparc64/pci/ofw_pcibus.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / sparc64 / pci / ofw_pcibus.c
1 /*-
2  * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3  * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
4  * Copyright (c) 2000, BSDi
5  * Copyright (c) 2003, Thomas Moestl <tmm@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include "opt_ofw_pci.h"
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 #ifndef SUN4V
49 #include <machine/bus_common.h>
50 #include <machine/iommureg.h>
51 #endif
52 #include <machine/resource.h>
53
54 #include <dev/pci/pcireg.h>
55 #include <dev/pci/pcivar.h>
56 #include <dev/pci/pci_private.h>
57
58 #include <sparc64/pci/ofw_pci.h>
59
60 #include "pcib_if.h"
61 #include "pci_if.h"
62
63 /* Helper functions */
64 static void ofw_pcibus_setup_device(device_t bridge, uint32_t clock,
65     u_int busno, u_int slot, u_int func);
66
67 /* Methods */
68 static device_probe_t ofw_pcibus_probe;
69 static device_attach_t ofw_pcibus_attach;
70 static pci_assign_interrupt_t ofw_pcibus_assign_interrupt;
71 static ofw_bus_get_devinfo_t ofw_pcibus_get_devinfo;
72
73 static device_method_t ofw_pcibus_methods[] = {
74         /* Device interface */
75         DEVMETHOD(device_probe,         ofw_pcibus_probe),
76         DEVMETHOD(device_attach,        ofw_pcibus_attach),
77
78         /* Bus interface */
79
80         /* PCI interface */
81         DEVMETHOD(pci_assign_interrupt, ofw_pcibus_assign_interrupt),
82
83         /* ofw_bus interface */
84         DEVMETHOD(ofw_bus_get_devinfo,  ofw_pcibus_get_devinfo),
85         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
86         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
87         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
88         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
89         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
90
91         KOBJMETHOD_END
92 };
93
94 struct ofw_pcibus_devinfo {
95         struct pci_devinfo      opd_dinfo;
96         struct ofw_bus_devinfo  opd_obdinfo;
97 };
98
99 static devclass_t pci_devclass;
100
101 DEFINE_CLASS_1(pci, ofw_pcibus_driver, ofw_pcibus_methods, 1 /* no softc */,
102     pci_driver);
103 DRIVER_MODULE(ofw_pcibus, pcib, ofw_pcibus_driver, pci_devclass, 0, 0);
104 MODULE_VERSION(ofw_pcibus, 1);
105 MODULE_DEPEND(ofw_pcibus, pci, 1, 1, 1);
106
107 static int
108 ofw_pcibus_probe(device_t dev)
109 {
110
111         if (ofw_bus_get_node(dev) == 0)
112                 return (ENXIO);
113         device_set_desc(dev, "OFW PCI bus");
114
115         return (0);
116 }
117
118 /*
119  * Perform miscellaneous setups the firmware usually does not do for us.
120  */
121 static void
122 ofw_pcibus_setup_device(device_t bridge, uint32_t clock, u_int busno,
123     u_int slot, u_int func)
124 {
125 #ifndef SUN4V
126         uint32_t reg;
127
128         /*
129          * Initialize the latency timer register for busmaster devices to
130          * work properly.  This is another task which the firmware doesn't
131          * always perform.  The Min_Gnt register can be used to compute its
132          * recommended value: it contains the desired latency in units of
133          * 1/4 us assuming a clock rate of 33MHz.  To calculate the correct
134          * latency timer value, the clock frequency of the bus (defaulting
135          * to 33MHz) should be used and no wait states assumed.
136          * For bridges, we additionally set up the bridge control and the
137          * secondary latency registers.
138          */
139         if ((PCIB_READ_CONFIG(bridge, busno, slot, func, PCIR_HDRTYPE, 1) &
140             PCIM_HDRTYPE) == PCIM_HDRTYPE_BRIDGE) {
141                 reg = PCIB_READ_CONFIG(bridge, busno, slot, func,
142                     PCIR_BRIDGECTL_1, 1);
143 #if 0
144                 reg |= PCIB_BCR_MASTER_ABORT_MODE | PCIB_BCR_SERR_ENABLE |
145 #else
146                 reg |= PCIB_BCR_SERR_ENABLE |
147 #endif
148                     PCIB_BCR_PERR_ENABLE;
149 #ifdef OFW_PCI_DEBUG
150                 device_printf(bridge,
151                     "bridge %d/%d/%d: control 0x%x -> 0x%x\n",
152                     busno, slot, func, PCIB_READ_CONFIG(bridge, busno, slot,
153                     func, PCIR_BRIDGECTL_1, 1), reg);
154 #endif /* OFW_PCI_DEBUG */
155                 PCIB_WRITE_CONFIG(bridge, busno, slot, func, PCIR_BRIDGECTL_1,
156                     reg, 1);
157
158                 reg = OFW_PCI_LATENCY;
159 #ifdef OFW_PCI_DEBUG
160                 device_printf(bridge,
161                     "bridge %d/%d/%d: latency timer %d -> %d\n",
162                     busno, slot, func, PCIB_READ_CONFIG(bridge, busno, slot,
163                     func, PCIR_SECLAT_1, 1), reg);
164 #endif /* OFW_PCI_DEBUG */
165                 PCIB_WRITE_CONFIG(bridge, busno, slot, func, PCIR_SECLAT_1,
166                     reg, 1);
167         } else {
168                 reg = PCIB_READ_CONFIG(bridge, busno, slot, func,
169                     PCIR_MINGNT, 1);
170                 if (reg != 0) {
171                         switch (clock) {
172                         case 33000000:
173                                 reg *= 8;
174                                 break;
175                         case 66000000:
176                                 reg *= 4;
177                                 break;
178                         }
179                         reg = min(reg, 255);
180                 } else
181                         reg = OFW_PCI_LATENCY;
182         }
183 #ifdef OFW_PCI_DEBUG
184         device_printf(bridge, "device %d/%d/%d: latency timer %d -> %d\n",
185             busno, slot, func, PCIB_READ_CONFIG(bridge, busno, slot, func,
186             PCIR_LATTIMER, 1), reg);
187 #endif /* OFW_PCI_DEBUG */
188         PCIB_WRITE_CONFIG(bridge, busno, slot, func, PCIR_LATTIMER, reg, 1);
189
190         /*
191          * Compute a value to write into the cache line size register.
192          * The role of the streaming cache is unclear in write invalidate
193          * transfers, so it is made sure that it's line size is always
194          * reached.  Generally, the cache line size is fixed at 64 bytes
195          * by Fireplane/Safari, JBus and UPA.
196          */
197         PCIB_WRITE_CONFIG(bridge, busno, slot, func, PCIR_CACHELNSZ,
198             STRBUF_LINESZ / sizeof(uint32_t), 1);
199 #endif
200
201         /*
202          * The preset in the intline register is usually wrong.  Reset
203          * it to 255, so that the PCI code will reroute the interrupt if
204          * needed.
205          */
206         PCIB_WRITE_CONFIG(bridge, busno, slot, func, PCIR_INTLINE,
207             PCI_INVALID_IRQ, 1);
208 }
209
210 static int
211 ofw_pcibus_attach(device_t dev)
212 {
213         device_t pcib;
214         struct ofw_pci_register pcir;
215         struct ofw_pcibus_devinfo *dinfo;
216         phandle_t node, child;
217         uint32_t clock;
218         u_int busno, domain, func, slot;
219
220         pcib = device_get_parent(dev);
221         domain = pcib_get_domain(dev);
222         busno = pcib_get_bus(dev);
223         if (bootverbose)
224                 device_printf(dev, "domain=%d, physical bus=%d\n",
225                     domain, busno);
226         node = ofw_bus_get_node(dev);
227
228 #ifndef SUN4V
229         /* Add the PCI side of the HOST-PCI bridge itself to the bus. */
230         if (strcmp(device_get_name(device_get_parent(pcib)), "nexus") == 0 &&
231             (dinfo = (struct ofw_pcibus_devinfo *)pci_read_device(pcib,
232             domain, busno, 0, 0, sizeof(*dinfo))) != NULL) {
233                 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, node) != 0)
234                         pci_freecfg((struct pci_devinfo *)dinfo);
235                 else
236                         pci_add_child(dev, (struct pci_devinfo *)dinfo);
237         }
238 #endif
239
240         if (OF_getprop(ofw_bus_get_node(pcib), "clock-frequency", &clock,
241             sizeof(clock)) == -1)
242                 clock = 33000000;
243         for (child = OF_child(node); child != 0; child = OF_peer(child)) {
244                 if (OF_getprop(child, "reg", &pcir, sizeof(pcir)) == -1)
245                         continue;
246                 slot = OFW_PCI_PHYS_HI_DEVICE(pcir.phys_hi);
247                 func = OFW_PCI_PHYS_HI_FUNCTION(pcir.phys_hi);
248                 /* Some OFW device trees contain dupes. */
249                 if (pci_find_dbsf(domain, busno, slot, func) != NULL)
250                         continue;
251                 ofw_pcibus_setup_device(pcib, clock, busno, slot, func);
252                 dinfo = (struct ofw_pcibus_devinfo *)pci_read_device(pcib,
253                     domain, busno, slot, func, sizeof(*dinfo));
254                 if (dinfo == NULL)
255                         continue;
256                 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) !=
257                     0) {
258                         pci_freecfg((struct pci_devinfo *)dinfo);
259                         continue;
260                 }
261                 pci_add_child(dev, (struct pci_devinfo *)dinfo);
262         }
263
264         return (bus_generic_attach(dev));
265 }
266
267 static int
268 ofw_pcibus_assign_interrupt(device_t dev, device_t child)
269 {
270         ofw_pci_intr_t intr;
271         int isz;
272
273         isz = OF_getprop(ofw_bus_get_node(child), "interrupts", &intr,
274             sizeof(intr));
275         if (isz != sizeof(intr)) {
276                 /* No property; our best guess is the intpin. */
277                 intr = pci_get_intpin(child);
278 #ifndef SUN4V
279         } else if (intr >= 255) {
280                 /*
281                  * A fully specified interrupt (including IGN), as present on
282                  * SPARCengine Ultra AX and E450.  Extract the INO and return
283                  * it.
284                  */
285                 return (INTINO(intr));
286 #endif
287         }
288         /*
289          * If we got intr from a property, it may or may not be an intpin.
290          * For on-board devices, it frequently is not, and is completely out
291          * of the valid intpin range.  For PCI slots, it hopefully is,
292          * otherwise we will have trouble interfacing with non-OFW buses
293          * such as cardbus.
294          * Since we cannot tell which it is without violating layering, we
295          * will always use the route_interrupt method, and treat exceptions
296          * on the level they become apparent.
297          */
298         return (PCIB_ROUTE_INTERRUPT(device_get_parent(dev), child, intr));
299 }
300
301 static const struct ofw_bus_devinfo *
302 ofw_pcibus_get_devinfo(device_t bus, device_t dev)
303 {
304         struct ofw_pcibus_devinfo *dinfo;
305
306         dinfo = device_get_ivars(dev);
307         return (&dinfo->opd_obdinfo);
308 }