]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/sparc64/pci/apb.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / sparc64 / pci / apb.c
1 /*-
2  * Copyright (c) 1994,1995 Stefan Esser, Wolfgang StanglMeier
3  * Copyright (c) 2000 Michael Smith <msmith@freebsd.org>
4  * Copyright (c) 2000 BSDi
5  * Copyright (c) 2001, 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, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *      from: FreeBSD: src/sys/dev/pci/pci_pci.c,v 1.3 2000/12/13
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 /*
38  * Support for the Sun APB (Advanced PCI Bridge) PCI-PCI bridge.
39  * This bridge does not fully comply to the PCI bridge specification, and is
40  * therefore not supported by the generic driver.
41  * We can use some of the pcib methods anyway.
42  */
43
44 #include "opt_ofw_pci.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/module.h>
50 #include <sys/bus.h>
51 #include <sys/rman.h>
52
53 #include <dev/ofw/ofw_bus.h>
54 #include <dev/ofw/openfirm.h>
55
56 #include <machine/bus.h>
57 #include <machine/resource.h>
58
59 #include <dev/pci/pcireg.h>
60 #include <dev/pci/pcivar.h>
61 #include <dev/pci/pcib_private.h>
62
63 #include "pcib_if.h"
64
65 #include <sparc64/pci/ofw_pci.h>
66 #include <sparc64/pci/ofw_pcib_subr.h>
67
68 /*
69  * Bridge-specific data.
70  */
71 struct apb_softc {
72         struct ofw_pcib_gen_softc       sc_bsc;
73         uint8_t         sc_iomap;
74         uint8_t         sc_memmap;
75 };
76
77 static device_probe_t apb_probe;
78 static device_attach_t apb_attach;
79 static bus_alloc_resource_t apb_alloc_resource;
80
81 static device_method_t apb_methods[] = {
82         /* Device interface */
83         DEVMETHOD(device_probe,         apb_probe),
84         DEVMETHOD(device_attach,        apb_attach),
85
86         /* Bus interface */
87         DEVMETHOD(bus_alloc_resource,   apb_alloc_resource),
88
89         /* pcib interface */
90         DEVMETHOD(pcib_route_interrupt, ofw_pcib_gen_route_interrupt),
91
92         /* ofw_bus interface */
93         DEVMETHOD(ofw_bus_get_node,     ofw_pcib_gen_get_node),
94
95         KOBJMETHOD_END
96 };
97
98 static devclass_t pcib_devclass;
99
100 DEFINE_CLASS_1(pcib, apb_driver, apb_methods, sizeof(struct apb_softc),
101     pcib_driver);
102 EARLY_DRIVER_MODULE(apb, pci, apb_driver, pcib_devclass, 0, 0, BUS_PASS_BUS);
103 MODULE_DEPEND(apb, pci, 1, 1, 1);
104
105 /* APB specific registers */
106 #define APBR_IOMAP      0xde
107 #define APBR_MEMMAP     0xdf
108
109 /* Definitions for the mapping registers */
110 #define APB_IO_SCALE    0x200000
111 #define APB_MEM_SCALE   0x20000000
112
113 /*
114  * Generic device interface
115  */
116 static int
117 apb_probe(device_t dev)
118 {
119
120         if (pci_get_vendor(dev) == 0x108e &&    /* Sun */
121             pci_get_device(dev) == 0x5000)  {   /* APB */
122                 device_set_desc(dev, "APB PCI-PCI bridge");
123                 return (0);
124         }
125         return (ENXIO);
126 }
127
128 static void
129 apb_map_print(uint8_t map, u_long scale)
130 {
131         int i, first;
132
133         for (first = 1, i = 0; i < 8; i++) {
134                 if ((map & (1 << i)) != 0) {
135                         printf("%s0x%lx-0x%lx", first ? "" : ", ",
136                             i * scale, (i + 1) * scale - 1);
137                         first = 0;
138                 }
139         }
140 }
141
142 static int
143 apb_checkrange(uint8_t map, u_long scale, u_long start, u_long end)
144 {
145         int i, ei;
146
147         i = start / scale;
148         ei = end / scale;
149         if (i > 7 || ei > 7)
150                 return (0);
151         for (; i <= ei; i++)
152                 if ((map & (1 << i)) == 0)
153                         return (0);
154         return (1);
155 }
156
157 static int
158 apb_attach(device_t dev)
159 {
160         struct apb_softc *sc;
161
162         sc = device_get_softc(dev);
163
164         /*
165          * Get current bridge configuration.
166          */
167         sc->sc_bsc.ops_pcib_sc.domain = pci_get_domain(dev);
168         sc->sc_bsc.ops_pcib_sc.secbus =
169             pci_read_config(dev, PCIR_SECBUS_1, 1);
170         sc->sc_bsc.ops_pcib_sc.subbus =
171             pci_read_config(dev, PCIR_SUBBUS_1, 1);
172         sc->sc_iomap = pci_read_config(dev, APBR_IOMAP, 1);
173         sc->sc_memmap = pci_read_config(dev, APBR_MEMMAP, 1);
174         ofw_pcib_gen_setup(dev);
175
176         if (bootverbose) {
177                 device_printf(dev, "  domain            %d\n",
178                     sc->sc_bsc.ops_pcib_sc.domain);
179                 device_printf(dev, "  secondary bus     %d\n",
180                     sc->sc_bsc.ops_pcib_sc.secbus);
181                 device_printf(dev, "  subordinate bus   %d\n",
182                     sc->sc_bsc.ops_pcib_sc.subbus);
183                 device_printf(dev, "  I/O decode        ");
184                 apb_map_print(sc->sc_iomap, APB_IO_SCALE);
185                 printf("\n");
186                 device_printf(dev, "  memory decode     ");
187                 apb_map_print(sc->sc_memmap, APB_MEM_SCALE);
188                 printf("\n");
189         }
190
191         device_add_child(dev, "pci", -1);
192         return (bus_generic_attach(dev));
193 }
194
195 /*
196  * We have to trap resource allocation requests and ensure that the bridge
197  * is set up to, or capable of handling them.
198  */
199 static struct resource *
200 apb_alloc_resource(device_t dev, device_t child, int type, int *rid,
201     u_long start, u_long end, u_long count, u_int flags)
202 {
203         struct apb_softc *sc;
204
205         sc = device_get_softc(dev);
206
207         /*
208          * If this is a "default" allocation against this rid, we can't work
209          * out where it's coming from (we should actually never see these) so
210          * we just have to punt.
211          */
212         if (start == 0 && end == ~0) {
213                 device_printf(dev, "can't decode default resource id %d for "
214                     "%s, bypassing\n", *rid, device_get_nameunit(child));
215                 goto passup;
216         }
217
218         /*
219          * Fail the allocation for this range if it's not supported.
220          * XXX we should probably just fix up the bridge decode and
221          * soldier on.
222          */
223         switch (type) {
224         case SYS_RES_IOPORT:
225                 if (!apb_checkrange(sc->sc_iomap, APB_IO_SCALE, start, end)) {
226                         device_printf(dev, "device %s requested unsupported "
227                             "I/O range 0x%lx-0x%lx\n",
228                             device_get_nameunit(child), start, end);
229                         return (NULL);
230                 }
231                 if (bootverbose)
232                         device_printf(sc->sc_bsc.ops_pcib_sc.dev, "device "
233                             "%s requested decoded I/O range 0x%lx-0x%lx\n",
234                             device_get_nameunit(child), start, end);
235                 break;
236
237         case SYS_RES_MEMORY:
238                 if (!apb_checkrange(sc->sc_memmap, APB_MEM_SCALE, start, end)) {
239                         device_printf(dev, "device %s requested unsupported "
240                             "memory range 0x%lx-0x%lx\n",
241                             device_get_nameunit(child), start, end);
242                         return (NULL);
243                 }
244                 if (bootverbose)
245                         device_printf(sc->sc_bsc.ops_pcib_sc.dev, "device "
246                             "%s requested decoded memory range 0x%lx-0x%lx\n",
247                             device_get_nameunit(child), start, end);
248                 break;
249
250         default:
251                 break;
252         }
253
254  passup:
255         /*
256          * Bridge is OK decoding this resource, so pass it up.
257          */
258         return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
259             count, flags));
260 }