summaryrefslogtreecommitdiffstats
path: root/sys/powerpc/wii/wii_pic.c
blob: 0844a00d3627b40b9fba54ee3336cb562088588b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*-
 * Copyright (C) 2012 Margarida Gouveia
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/rman.h>
#include <sys/reboot.h>

#include <machine/bus.h>
#include <machine/platform.h>
#include <machine/intr_machdep.h>
#include <machine/resource.h>

#include <powerpc/wii/wii_picreg.h>

#include "pic_if.h"

static int	wiipic_probe(device_t);
static int	wiipic_attach(device_t);
static void	wiipic_dispatch(device_t, struct trapframe *);
static void	wiipic_enable(device_t, unsigned int, unsigned int);
static void	wiipic_eoi(device_t, unsigned int);
static void	wiipic_mask(device_t, unsigned int);
static void	wiipic_unmask(device_t, unsigned int);
static void	wiipic_intr(void *);

struct wiipic_softc {
	device_t		 sc_dev;
	struct resource		*sc_rres;
	bus_space_tag_t		 sc_bt;
	bus_space_handle_t	 sc_bh;
	int			 sc_rrid;
	int			 sc_irqid;
	struct resource		*sc_irq;
	void			*sc_irqctx;
	int			 sc_vector[WIIPIC_NIRQ];
};

static device_method_t wiipic_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe,		wiipic_probe),
	DEVMETHOD(device_attach,	wiipic_attach),

	/* PIC interface */
	DEVMETHOD(pic_dispatch,		wiipic_dispatch),
	DEVMETHOD(pic_enable,		wiipic_enable),
	DEVMETHOD(pic_eoi,		wiipic_eoi),
	DEVMETHOD(pic_mask,		wiipic_mask),
	DEVMETHOD(pic_unmask,		wiipic_unmask),

        DEVMETHOD_END
};

static driver_t wiipic_driver = {
	"wiipic",
	wiipic_methods,
	sizeof(struct wiipic_softc)
};

static devclass_t wiipic_devclass;

DRIVER_MODULE(wiipic, wiibus, wiipic_driver, wiipic_devclass, 0, 0);

static __inline uint32_t
wiipic_imr_read(struct wiipic_softc *sc)
{

	return (bus_space_read_4(sc->sc_bt, sc->sc_bh, WIIPIC_IMR));
}

static __inline void
wiipic_imr_write(struct wiipic_softc *sc, uint32_t imr)
{

	bus_space_write_4(sc->sc_bt, sc->sc_bh, WIIPIC_IMR, imr);
}

static __inline uint32_t
wiipic_icr_read(struct wiipic_softc *sc)
{

	return (bus_space_read_4(sc->sc_bt, sc->sc_bh, WIIPIC_ICR));
}

static __inline void
wiipic_icr_write(struct wiipic_softc *sc, uint32_t icr)
{

	bus_space_write_4(sc->sc_bt, sc->sc_bh, WIIPIC_ICR, icr);
}

static int
wiipic_probe(device_t dev)
{
        device_set_desc(dev, "Nintendo Wii PIC");

        return (BUS_PROBE_NOWILDCARD);
}

static int
wiipic_attach(device_t dev)
{
	struct wiipic_softc *sc;

	sc = device_get_softc(dev);
	sc->sc_dev = dev;

	sc->sc_rrid = 0;
	sc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
	    &sc->sc_rrid, RF_ACTIVE);
	if (sc->sc_rres == NULL) {
		device_printf(dev, "could not alloc mem resource\n");
		return (ENXIO);
	}
	sc->sc_bt = rman_get_bustag(sc->sc_rres);
	sc->sc_bh = rman_get_bushandle(sc->sc_rres);

	/* Turn off all interrupts */
	wiipic_imr_write(sc, 0x00000000);
	wiipic_icr_write(sc, 0xffffffff);

	powerpc_register_pic(dev, 0, WIIPIC_NIRQ, 0, FALSE);

	/*
	 * Setup the interrupt handler.
	 */
	sc->sc_irqid = 0;
	sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irqid,
	    RF_ACTIVE);
	if (sc->sc_irq == NULL) {
		device_printf(dev, "could not alloc IRQ resource\n");
		return (ENXIO);
	}
	bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC | INTR_MPSAFE,
	    NULL, wiipic_intr, sc, &sc->sc_irqctx);

	return (0);
}

static void
wiipic_dispatch(device_t dev, struct trapframe *tf)
{
	struct wiipic_softc *sc;
	uint32_t irq;

	sc = device_get_softc(dev);
	irq = wiipic_icr_read(sc) & wiipic_imr_read(sc);
	if (irq == 0)
		return;
	irq = ffs(irq) - 1;
	KASSERT(irq < WIIPIC_NIRQ, ("bogus irq %d", irq));
	powerpc_dispatch_intr(sc->sc_vector[irq], tf);
}

static void
wiipic_enable(device_t dev, unsigned int irq, unsigned int vector)
{
	struct wiipic_softc *sc;

	KASSERT(irq < WIIPIC_NIRQ, ("bogus irq %d", irq));
	sc = device_get_softc(dev);
	sc->sc_vector[irq] = vector;
	wiipic_unmask(dev, irq);
}

static void
wiipic_eoi(device_t dev, unsigned int irq)
{
	struct wiipic_softc *sc;
	uint32_t icr;

	sc = device_get_softc(dev);
	icr = wiipic_icr_read(sc);
	icr |= (1 << irq);
	wiipic_icr_write(sc, icr);
}

static void
wiipic_mask(device_t dev, unsigned int irq)
{
	struct wiipic_softc *sc;
	uint32_t imr;

	sc = device_get_softc(dev);
	imr = wiipic_imr_read(sc);
	imr &= ~(1 << irq);
	wiipic_imr_write(sc, imr);
}

static void
wiipic_unmask(device_t dev, unsigned int irq)
{
	struct wiipic_softc *sc;
	uint32_t imr;

	sc = device_get_softc(dev);
	imr = wiipic_imr_read(sc);
	imr |= (1 << irq);
	wiipic_imr_write(sc, imr);
}

/*
 * Reset button interrupt.
 */
static void
wiipic_intr(void *xsc)
{
	struct wiipic_softc *sc;

	sc = (struct wiipic_softc *)xsc;
	if (wiipic_icr_read(sc) & WIIPIC_RBS)
		shutdown_nice(RB_AUTOBOOT);
}

OpenPOWER on IntegriCloud