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
|
/* $NetBSD: plgpio_acpi.c,v 1.7 2021/01/29 15:49:55 thorpej Exp $ */
/*-
* Copyright (c) 2018 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jared McNeill <jmcneill@invisible.ca>.
*
* 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``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 FOUNDATION OR CONTRIBUTORS
* 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>
__KERNEL_RCSID(0, "$NetBSD: plgpio_acpi.c,v 1.7 2021/01/29 15:49:55 thorpej Exp $");
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/cpu.h>
#include <sys/device.h>
#include <sys/gpio.h>
#include <dev/acpi/acpireg.h>
#include <dev/acpi/acpivar.h>
#include <dev/acpi/acpi_intr.h>
#include <dev/acpi/acpi_event.h>
#include <dev/gpio/gpiovar.h>
#include <dev/ic/pl061var.h>
#include <dev/ic/pl061reg.h>
struct plgpio_acpi_softc;
struct plgpio_acpi_softc {
struct plgpio_softc sc_base;
ACPI_HANDLE sc_handle;
struct acpi_event * sc_event[8];
};
static int plgpio_acpi_match(device_t, cfdata_t, void *);
static void plgpio_acpi_attach(device_t, device_t, void *);
static void plgpio_acpi_register_event(void *, struct acpi_event *, ACPI_RESOURCE_GPIO *);
static int plgpio_acpi_intr(void *);
CFATTACH_DECL_NEW(plgpio_acpi, sizeof(struct plgpio_acpi_softc), plgpio_acpi_match, plgpio_acpi_attach, NULL, NULL);
static const struct device_compatible_entry compat_data[] = {
{ .compat = "ARMH0061" },
DEVICE_COMPAT_EOL
};
static int
plgpio_acpi_match(device_t parent, cfdata_t cf, void *aux)
{
struct acpi_attach_args *aa = aux;
return acpi_compatible_match(aa, compat_data);
}
static void
plgpio_acpi_attach(device_t parent, device_t self, void *aux)
{
struct plgpio_acpi_softc * const asc = device_private(self);
struct plgpio_softc * const sc = &asc->sc_base;
struct acpi_attach_args *aa = aux;
struct acpi_resources res;
struct acpi_mem *mem;
struct acpi_irq *irq;
ACPI_STATUS rv;
int error;
void *ih;
sc->sc_dev = self;
asc->sc_handle = aa->aa_node->ad_handle;
rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
&res, &acpi_resource_parse_ops_default);
if (ACPI_FAILURE(rv))
return;
mem = acpi_res_mem(&res, 0);
if (mem == NULL) {
aprint_error_dev(self, "couldn't find mem resource\n");
goto done;
}
irq = acpi_res_irq(&res, 0);
if (irq == NULL) {
aprint_error_dev(self, "couldn't find irq resource\n");
goto done;
}
sc->sc_dev = self;
sc->sc_bst = aa->aa_memt;
error = bus_space_map(sc->sc_bst, mem->ar_base, mem->ar_length, 0, &sc->sc_bsh);
if (error) {
aprint_error_dev(self, "couldn't map registers\n");
return;
}
plgpio_attach(sc);
rv = acpi_event_create_gpio(self, asc->sc_handle, plgpio_acpi_register_event, asc);
if (ACPI_FAILURE(rv)) {
if (rv != AE_NOT_FOUND)
aprint_error_dev(self, "failed to create events: %s\n", AcpiFormatException(rv));
goto done;
}
ih = acpi_intr_establish(self,
(uint64_t)(uintptr_t)asc->sc_handle,
IPL_VM, false, plgpio_acpi_intr, asc, device_xname(self));
if (ih == NULL)
aprint_error_dev(self, "couldn't establish interrupt\n");
done:
acpi_resource_cleanup(&res);
}
static void
plgpio_acpi_register_event(void *priv, struct acpi_event *ev, ACPI_RESOURCE_GPIO *gpio)
{
struct plgpio_acpi_softc * const asc = priv;
struct plgpio_softc * const sc = &asc->sc_base;
uint32_t ibe, iev, is, ie;
const int pin = gpio->PinTable[0];
if (pin >= __arraycount(asc->sc_event)) {
aprint_error_dev(asc->sc_base.sc_dev,
"ignoring event for pin %u (out of range)\n", pin);
return;
}
if (asc->sc_event[pin] != NULL) {
aprint_error_dev(asc->sc_base.sc_dev,
"ignoring duplicate pin %u\n", pin);
return;
}
asc->sc_event[pin] = ev;
asc->sc_base.sc_reserved_mask |= __BIT(pin);
/*
* Configure and enable interrupts for this pin.
*/
ibe = PLGPIO_READ(sc, PL061_GPIOIBE_REG);
iev = PLGPIO_READ(sc, PL061_GPIOIEV_REG);
switch (gpio->Polarity) {
case ACPI_ACTIVE_HIGH:
ibe &= ~__BIT(pin);
iev |= __BIT(pin);
break;
case ACPI_ACTIVE_LOW:
ibe &= ~__BIT(pin);
iev &= ~__BIT(pin);
break;
case ACPI_ACTIVE_BOTH:
ibe |= __BIT(pin);
break;
}
PLGPIO_WRITE(sc, PL061_GPIOIBE_REG, ibe);
PLGPIO_WRITE(sc, PL061_GPIOIEV_REG, iev);
is = PLGPIO_READ(sc, PL061_GPIOIS_REG);
switch (gpio->Triggering) {
case ACPI_LEVEL_SENSITIVE:
is |= __BIT(pin);
break;
case ACPI_EDGE_SENSITIVE:
is &= ~__BIT(pin);
break;
}
PLGPIO_WRITE(sc, PL061_GPIOIS_REG, is);
delay(20);
PLGPIO_WRITE(sc, PL061_GPIOIC_REG, __BIT(pin));
ie = PLGPIO_READ(sc, PL061_GPIOIE_REG);
ie |= __BIT(pin);
PLGPIO_WRITE(sc, PL061_GPIOIE_REG, ie);
}
static int
plgpio_acpi_intr(void *priv)
{
struct plgpio_acpi_softc * const asc = priv;
struct plgpio_softc * const sc = &asc->sc_base;
uint32_t mis;
int bit;
mis = PLGPIO_READ(sc, PL061_GPIOMIS_REG);
PLGPIO_WRITE(sc, PL061_GPIOIC_REG, mis);
while ((bit = __builtin_ffs(mis)) != 0) {
const int pin = bit - 1;
struct acpi_event * const ev = asc->sc_event[pin];
KASSERT(ev != NULL);
acpi_event_notify(ev);
mis &= ~__BIT(pin);
}
return 1;
}
|