plgpio_acpi.c revision 1.6 1 /* $NetBSD: plgpio_acpi.c,v 1.6 2020/12/07 10:02:51 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jared McNeill <jmcneill (at) invisible.ca>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: plgpio_acpi.c,v 1.6 2020/12/07 10:02:51 jmcneill Exp $");
34
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/cpu.h>
38 #include <sys/device.h>
39 #include <sys/gpio.h>
40
41 #include <dev/acpi/acpireg.h>
42 #include <dev/acpi/acpivar.h>
43 #include <dev/acpi/acpi_intr.h>
44 #include <dev/acpi/acpi_event.h>
45
46 #include <dev/gpio/gpiovar.h>
47 #include <dev/ic/pl061var.h>
48 #include <dev/ic/pl061reg.h>
49
50 struct plgpio_acpi_softc;
51
52 struct plgpio_acpi_softc {
53 struct plgpio_softc sc_base;
54
55 ACPI_HANDLE sc_handle;
56
57 struct acpi_event * sc_event[8];
58 };
59
60 static int plgpio_acpi_match(device_t, cfdata_t, void *);
61 static void plgpio_acpi_attach(device_t, device_t, void *);
62
63 static void plgpio_acpi_register_event(void *, struct acpi_event *, ACPI_RESOURCE_GPIO *);
64 static int plgpio_acpi_intr(void *);
65
66 CFATTACH_DECL_NEW(plgpio_acpi, sizeof(struct plgpio_acpi_softc), plgpio_acpi_match, plgpio_acpi_attach, NULL, NULL);
67
68 static const char * const compatible[] = {
69 "ARMH0061",
70 NULL
71 };
72
73 static int
74 plgpio_acpi_match(device_t parent, cfdata_t cf, void *aux)
75 {
76 struct acpi_attach_args *aa = aux;
77
78 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
79 return 0;
80
81 return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
82 }
83
84 static void
85 plgpio_acpi_attach(device_t parent, device_t self, void *aux)
86 {
87 struct plgpio_acpi_softc * const asc = device_private(self);
88 struct plgpio_softc * const sc = &asc->sc_base;
89 struct acpi_attach_args *aa = aux;
90 struct acpi_resources res;
91 struct acpi_mem *mem;
92 struct acpi_irq *irq;
93 ACPI_STATUS rv;
94 int error;
95 void *ih;
96
97 sc->sc_dev = self;
98 asc->sc_handle = aa->aa_node->ad_handle;
99
100 rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
101 &res, &acpi_resource_parse_ops_default);
102 if (ACPI_FAILURE(rv))
103 return;
104
105 mem = acpi_res_mem(&res, 0);
106 if (mem == NULL) {
107 aprint_error_dev(self, "couldn't find mem resource\n");
108 goto done;
109 }
110
111 irq = acpi_res_irq(&res, 0);
112 if (irq == NULL) {
113 aprint_error_dev(self, "couldn't find irq resource\n");
114 goto done;
115 }
116
117 sc->sc_dev = self;
118 sc->sc_bst = aa->aa_memt;
119 error = bus_space_map(sc->sc_bst, mem->ar_base, mem->ar_length, 0, &sc->sc_bsh);
120 if (error) {
121 aprint_error_dev(self, "couldn't map registers\n");
122 return;
123 }
124
125 plgpio_attach(sc);
126
127 rv = acpi_event_create_gpio(self, asc->sc_handle, plgpio_acpi_register_event, asc);
128 if (ACPI_FAILURE(rv)) {
129 if (rv != AE_NOT_FOUND)
130 aprint_error_dev(self, "failed to create events: %s\n", AcpiFormatException(rv));
131 goto done;
132 }
133
134 ih = acpi_intr_establish(self,
135 (uint64_t)(uintptr_t)asc->sc_handle,
136 IPL_VM, false, plgpio_acpi_intr, asc, device_xname(self));
137 if (ih == NULL)
138 aprint_error_dev(self, "couldn't establish interrupt\n");
139
140 done:
141 acpi_resource_cleanup(&res);
142 }
143
144 static void
145 plgpio_acpi_register_event(void *priv, struct acpi_event *ev, ACPI_RESOURCE_GPIO *gpio)
146 {
147 struct plgpio_acpi_softc * const asc = priv;
148 struct plgpio_softc * const sc = &asc->sc_base;
149 uint32_t ibe, iev, is, ie;
150
151 const int pin = gpio->PinTable[0];
152
153 if (pin >= __arraycount(asc->sc_event)) {
154 aprint_error_dev(asc->sc_base.sc_dev,
155 "ignoring event for pin %u (out of range)\n", pin);
156 return;
157 }
158 if (asc->sc_event[pin] != NULL) {
159 aprint_error_dev(asc->sc_base.sc_dev,
160 "ignoring duplicate pin %u\n", pin);
161 return;
162 }
163
164 asc->sc_event[pin] = ev;
165 asc->sc_base.sc_reserved_mask |= __BIT(pin);
166
167 /*
168 * Configure and enable interrupts for this pin.
169 */
170
171 ibe = PLGPIO_READ(sc, PL061_GPIOIBE_REG);
172 iev = PLGPIO_READ(sc, PL061_GPIOIEV_REG);
173 switch (gpio->Polarity) {
174 case ACPI_ACTIVE_HIGH:
175 ibe &= ~__BIT(pin);
176 iev |= __BIT(pin);
177 break;
178 case ACPI_ACTIVE_LOW:
179 ibe &= ~__BIT(pin);
180 iev &= ~__BIT(pin);
181 break;
182 case ACPI_ACTIVE_BOTH:
183 ibe |= __BIT(pin);
184 break;
185 }
186 PLGPIO_WRITE(sc, PL061_GPIOIBE_REG, ibe);
187 PLGPIO_WRITE(sc, PL061_GPIOIEV_REG, iev);
188
189 is = PLGPIO_READ(sc, PL061_GPIOIS_REG);
190 switch (gpio->Triggering) {
191 case ACPI_LEVEL_SENSITIVE:
192 is |= __BIT(pin);
193 break;
194 case ACPI_EDGE_SENSITIVE:
195 is &= ~__BIT(pin);
196 break;
197 }
198 PLGPIO_WRITE(sc, PL061_GPIOIS_REG, is);
199
200 delay(20);
201
202 PLGPIO_WRITE(sc, PL061_GPIOIC_REG, __BIT(pin));
203
204 ie = PLGPIO_READ(sc, PL061_GPIOIE_REG);
205 ie |= __BIT(pin);
206 PLGPIO_WRITE(sc, PL061_GPIOIE_REG, ie);
207 }
208
209 static int
210 plgpio_acpi_intr(void *priv)
211 {
212 struct plgpio_acpi_softc * const asc = priv;
213 struct plgpio_softc * const sc = &asc->sc_base;
214 uint32_t mis;
215 int bit;
216
217 mis = PLGPIO_READ(sc, PL061_GPIOMIS_REG);
218 PLGPIO_WRITE(sc, PL061_GPIOIC_REG, mis);
219
220 while ((bit = __builtin_ffs(mis)) != 0) {
221 const int pin = bit - 1;
222 struct acpi_event * const ev = asc->sc_event[pin];
223 KASSERT(ev != NULL);
224
225 acpi_event_notify(ev);
226
227 mis &= ~__BIT(pin);
228 }
229
230 return 1;
231 }
232