]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/powerpc/powerpc/nexus.c
MFC r256994, r257016, r257055, r257059, r257060, r257075
[FreeBSD/stable/10.git] / sys / powerpc / powerpc / nexus.c
1 /*-
2  * Copyright 1998 Massachusetts Institute of Technology
3  * Copyright 2001 by Thomas Moestl <tmm@FreeBSD.org>.
4  * Copyright 2006 by Marius Strobl <marius@FreeBSD.org>.
5  * All rights reserved.
6  *
7  * Permission to use, copy, modify, and distribute this software and
8  * its documentation for any purpose and without fee is hereby
9  * granted, provided that both the above copyright notice and this
10  * permission notice appear in all copies, that both the above
11  * copyright notice and this permission notice appear in all
12  * supporting documentation, and that the name of M.I.T. not be used
13  * in advertising or publicity pertaining to distribution of the
14  * software without specific, written prior permission.  M.I.T. makes
15  * no representations about the suitability of this software for any
16  * purpose.  It is provided "as is" without express or implied
17  * warranty.
18  *
19  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
20  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
21  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
23  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *      from: FreeBSD: src/sys/i386/i386/nexus.c,v 1.43 2001/02/09
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/pcpu.h>
45 #include <sys/rman.h>
46
47 #include <vm/vm.h>
48 #include <vm/pmap.h>
49
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
52 #include <dev/ofw/ofw_nexus.h>
53 #include <dev/ofw/openfirm.h>
54
55 #include <machine/bus.h>
56 #include <machine/intr_machdep.h>
57 #include <machine/resource.h>
58
59 /*
60  * The nexus (which is a pseudo-bus actually) iterates over the nodes that
61  * hang from the Open Firmware root node and adds them as devices to this bus
62  * (except some special nodes which are excluded) so that drivers can be
63  * attached to them.
64  *
65  * Additionally, interrupt setup/teardown and some resource management are
66  * done at this level.
67  */
68
69 static bus_setup_intr_t nexus_setup_intr;
70 static bus_teardown_intr_t nexus_teardown_intr;
71 static bus_activate_resource_t nexus_activate_resource;
72 static bus_deactivate_resource_t nexus_deactivate_resource;
73 #ifdef SMP
74 static bus_bind_intr_t nexus_bind_intr;
75 #endif
76 static bus_config_intr_t nexus_config_intr;
77 static ofw_bus_map_intr_t nexus_ofw_map_intr;
78 static ofw_bus_config_intr_t nexus_ofw_config_intr;
79
80 static device_method_t nexus_methods[] = {
81         /* Bus interface */
82         DEVMETHOD(bus_activate_resource,        nexus_activate_resource),
83         DEVMETHOD(bus_deactivate_resource,      nexus_deactivate_resource),
84         DEVMETHOD(bus_setup_intr,       nexus_setup_intr),
85         DEVMETHOD(bus_teardown_intr,    nexus_teardown_intr),
86 #ifdef SMP
87         DEVMETHOD(bus_bind_intr,        nexus_bind_intr),
88 #endif
89         DEVMETHOD(bus_config_intr,      nexus_config_intr),
90
91         /* ofw_bus interface */
92         DEVMETHOD(ofw_bus_map_intr,     nexus_ofw_map_intr),
93         DEVMETHOD(ofw_bus_config_intr,  nexus_ofw_config_intr),
94
95         DEVMETHOD_END
96 };
97
98 static devclass_t nexus_devclass;
99
100 DEFINE_CLASS_1(nexus, nexus_driver, nexus_methods,
101     sizeof(struct ofw_nexus_softc), ofw_nexus_driver);
102 EARLY_DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0,
103     BUS_PASS_BUS);
104 MODULE_VERSION(nexus, 1);
105
106 static int
107 nexus_setup_intr(device_t bus __unused, device_t child, struct resource *r,
108     int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg,
109     void **cookiep)
110 {
111         int error;
112
113         if (r == NULL)
114                 panic("%s: NULL interrupt resource!", __func__);
115
116         if ((rman_get_flags(r) & RF_SHAREABLE) == 0)
117                 flags |= INTR_EXCL;
118
119         /* We depend here on rman_activate_resource() being idempotent. */
120         error = rman_activate_resource(r);
121         if (error)
122                 return (error);
123
124         error = powerpc_setup_intr(device_get_nameunit(child),
125             rman_get_start(r), filt, intr, arg, flags, cookiep);
126
127         return (error);
128 }
129
130 static int
131 nexus_teardown_intr(device_t bus __unused, device_t child __unused,
132     struct resource *r, void *ih)
133 {
134         
135         if (r == NULL)
136                 return (EINVAL);
137
138         return (powerpc_teardown_intr(ih));
139 }
140
141 #ifdef SMP
142 static int
143 nexus_bind_intr(device_t bus __unused, device_t child __unused,
144     struct resource *r, int cpu)
145 {
146
147         return (powerpc_bind_intr(rman_get_start(r), cpu));
148 }
149 #endif
150
151 static int
152 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
153     enum intr_polarity pol)
154 {
155  
156         return (powerpc_config_intr(irq, trig, pol));
157
158
159 static int
160 nexus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent, int irq)
161 {
162         return (MAP_IRQ(iparent, irq));
163 }
164
165 static int
166 nexus_ofw_config_intr(device_t dev, device_t child, int irq, int sense)
167 {
168  
169         return (powerpc_fw_config_intr(irq, sense));
170
171
172 static int
173 nexus_activate_resource(device_t bus __unused, device_t child __unused,
174     int type, int rid __unused, struct resource *r)
175 {
176
177         if (type == SYS_RES_MEMORY) {
178                 vm_offset_t start;
179                 void *p;
180
181                 start = (vm_offset_t) rman_get_start(r);
182                 if (bootverbose)
183                         printf("nexus mapdev: start %zx, len %ld\n", start,
184                             rman_get_size(r));
185
186                 p = pmap_mapdev(start, (vm_size_t) rman_get_size(r));
187                 if (p == NULL)
188                         return (ENOMEM);
189                 rman_set_virtual(r, p);
190                 rman_set_bustag(r, &bs_be_tag);
191                 rman_set_bushandle(r, (u_long)p);
192         }
193         return (rman_activate_resource(r));
194 }
195
196 static int
197 nexus_deactivate_resource(device_t bus __unused, device_t child __unused,
198     int type __unused, int rid __unused, struct resource *r)
199 {
200
201         /*
202          * If this is a memory resource, unmap it.
203          */
204         if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
205                 bus_size_t psize;
206
207                 psize = rman_get_size(r);
208                 pmap_unmapdev((vm_offset_t)rman_get_virtual(r), psize);
209         }
210
211         return (rman_deactivate_resource(r));
212 }
213