]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/wii/wii_pic.c
MFC
[FreeBSD/FreeBSD.git] / sys / powerpc / wii / wii_pic.c
1 /*-
2  * Copyright (C) 2012 Margarida Gouveia
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/module.h>
32 #include <sys/bus.h>
33 #include <sys/conf.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/rman.h>
37
38 #include <machine/bus.h>
39 #include <machine/platform.h>
40 #include <machine/intr_machdep.h>
41 #include <machine/resource.h>
42
43 #include <powerpc/wii/wii_picreg.h>
44
45 #include "pic_if.h"
46
47 static int      wiipic_probe(device_t);
48 static int      wiipic_attach(device_t);
49 static void     wiipic_dispatch(device_t, struct trapframe *);
50 static void     wiipic_enable(device_t, unsigned int, unsigned int);
51 static void     wiipic_eoi(device_t, unsigned int);
52 static void     wiipic_mask(device_t, unsigned int);
53 static void     wiipic_unmask(device_t, unsigned int);
54
55 struct wiipic_softc {
56         device_t                 sc_dev;
57         struct resource         *sc_rres;
58         bus_space_tag_t          sc_bt;
59         bus_space_handle_t       sc_bh;
60         int                      sc_rrid;
61         int                      sc_vector[WIIPIC_NIRQ];
62 };
63
64 static device_method_t wiipic_methods[] = {
65         /* Device interface */
66         DEVMETHOD(device_probe,         wiipic_probe),
67         DEVMETHOD(device_attach,        wiipic_attach),
68
69         /* PIC interface */
70         DEVMETHOD(pic_dispatch,         wiipic_dispatch),
71         DEVMETHOD(pic_enable,           wiipic_enable),
72         DEVMETHOD(pic_eoi,              wiipic_eoi),
73         DEVMETHOD(pic_mask,             wiipic_mask),
74         DEVMETHOD(pic_unmask,           wiipic_unmask),
75
76         DEVMETHOD_END
77 };
78
79 static driver_t wiipic_driver = {
80         "wiipic",
81         wiipic_methods,
82         sizeof(struct wiipic_softc)
83 };
84
85 static devclass_t wiipic_devclass;
86
87 DRIVER_MODULE(wiipic, wiibus, wiipic_driver, wiipic_devclass, 0, 0);
88
89 static __inline uint32_t
90 wiipic_imr_read(struct wiipic_softc *sc)
91 {
92
93         return (bus_space_read_4(sc->sc_bt, sc->sc_bh, WIIPIC_IMR));
94 }
95
96 static __inline void
97 wiipic_imr_write(struct wiipic_softc *sc, uint32_t imr)
98 {
99
100         bus_space_write_4(sc->sc_bt, sc->sc_bh, WIIPIC_IMR, imr);
101 }
102
103 static __inline uint32_t
104 wiipic_icr_read(struct wiipic_softc *sc)
105 {
106
107         return (bus_space_read_4(sc->sc_bt, sc->sc_bh, WIIPIC_ICR));
108 }
109
110 static __inline void
111 wiipic_icr_write(struct wiipic_softc *sc, uint32_t icr)
112 {
113
114         bus_space_write_4(sc->sc_bt, sc->sc_bh, WIIPIC_ICR, icr);
115 }
116
117 static int
118 wiipic_probe(device_t dev)
119 {
120         device_set_desc(dev, "Nintendo Wii PIC");
121
122         return (BUS_PROBE_NOWILDCARD);
123 }
124
125 static int
126 wiipic_attach(device_t dev)
127 {
128         struct wiipic_softc *sc;
129
130         sc = device_get_softc(dev);
131         sc->sc_dev = dev;
132
133         sc->sc_rrid = 0;
134         sc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
135             &sc->sc_rrid, RF_ACTIVE);
136         if (sc->sc_rres == NULL) {
137                 device_printf(dev, "could not alloc mem resource\n");
138                 return (ENXIO);
139         }
140         sc->sc_bt = rman_get_bustag(sc->sc_rres);
141         sc->sc_bh = rman_get_bushandle(sc->sc_rres);
142
143         /* Turn off all interrupts */
144         wiipic_imr_write(sc, 0x00000000);
145         wiipic_icr_write(sc, 0xffffffff);
146
147         powerpc_register_pic(dev, 0, WIIPIC_NIRQ, 0, FALSE);
148
149         return (0);
150 }
151
152 static void
153 wiipic_dispatch(device_t dev, struct trapframe *tf)
154 {
155         struct wiipic_softc *sc;
156         uint32_t irq;
157
158         sc = device_get_softc(dev);
159         irq = ffs(wiipic_icr_read(sc) & wiipic_imr_read(sc));
160         KASSERT(irq < WIIPIC_NIRQ, ("bogus irq %d", irq));
161         powerpc_dispatch_intr(sc->sc_vector[irq], tf);
162 }
163
164 static void
165 wiipic_enable(device_t dev, unsigned int irq, unsigned int vector)
166 {
167         struct wiipic_softc *sc;
168
169         KASSERT(irq < WIIPIC_NIRQ, ("bogus irq %d", irq));
170         sc = device_get_softc(dev);
171         sc->sc_vector[irq] = vector;
172         wiipic_unmask(dev, irq);
173 }
174
175 static void
176 wiipic_eoi(device_t dev, unsigned int irq)
177 {
178         struct wiipic_softc *sc;
179         uint32_t icr;
180
181         sc = device_get_softc(dev);
182         icr = wiipic_icr_read(sc);
183         icr |= (1 << irq);
184         wiipic_icr_write(sc, icr);
185 }
186
187 static void
188 wiipic_mask(device_t dev, unsigned int irq)
189 {
190         struct wiipic_softc *sc;
191         uint32_t imr;
192
193         sc = device_get_softc(dev);
194         imr = wiipic_imr_read(sc);
195         imr &= ~(1 << irq);
196         wiipic_imr_write(sc, imr);
197 }
198
199 static void
200 wiipic_unmask(device_t dev, unsigned int irq)
201 {
202         struct wiipic_softc *sc;
203         uint32_t imr;
204
205         sc = device_get_softc(dev);
206         imr = wiipic_imr_read(sc);
207         imr |= (1 << irq);
208         wiipic_imr_write(sc, imr);
209 }