1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2024 Ahmad Khalifa <ahmadkhalifa570@gmail.com>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/types.h>
29 #include <sys/bus.h>
30 #include <sys/kernel.h>
31 #include <sys/module.h>
32 #include <sys/gpio.h>
33
34 #include <contrib/dev/acpica/include/acpi.h>
35 #include <dev/acpica/acpivar.h>
36
37 #include <dev/gpio/gpiobusvar.h>
38 #include <dev/gpio/acpi_gpiobusvar.h>
39
40 #include "gpiobus_if.h"
41
42 struct acpi_gpiobus_softc {
43 struct gpiobus_softc super_sc;
44 ACPI_CONNECTION_INFO handler_info;
45 };
46
47 struct acpi_gpiobus_ctx {
48 struct gpiobus_softc *sc;
49 ACPI_HANDLE dev_handle;
50 };
51
52 struct acpi_gpiobus_ivar
53 {
54 struct gpiobus_ivar gpiobus; /* Must come first */
55 ACPI_HANDLE dev_handle; /* ACPI handle for bus */
56 uint32_t flags;
57 };
58
59 static uint32_t
acpi_gpiobus_convflags(ACPI_RESOURCE_GPIO * gpio_res)60 acpi_gpiobus_convflags(ACPI_RESOURCE_GPIO *gpio_res)
61 {
62 uint32_t flags = 0;
63
64 /* Figure out pin flags */
65 if (gpio_res->ConnectionType == ACPI_RESOURCE_GPIO_TYPE_INT) {
66 switch (gpio_res->Polarity) {
67 case ACPI_ACTIVE_HIGH:
68 flags = gpio_res->Triggering == ACPI_LEVEL_SENSITIVE ?
69 GPIO_INTR_LEVEL_HIGH : GPIO_INTR_EDGE_RISING;
70 break;
71 case ACPI_ACTIVE_LOW:
72 flags = gpio_res->Triggering == ACPI_LEVEL_SENSITIVE ?
73 GPIO_INTR_LEVEL_LOW : GPIO_INTR_EDGE_FALLING;
74 break;
75 case ACPI_ACTIVE_BOTH:
76 flags = GPIO_INTR_EDGE_BOTH;
77 break;
78 }
79
80 flags |= GPIO_PIN_INPUT;
81 #ifdef NOT_YET
82 /* This is not currently implemented. */
83 if (gpio_res->Shareable == ACPI_SHARED)
84 flags |= GPIO_INTR_SHAREABLE;
85 #endif
86 }
87 if (gpio_res->ConnectionType == ACPI_RESOURCE_GPIO_TYPE_IO) {
88 switch (gpio_res->IoRestriction) {
89 case ACPI_IO_RESTRICT_INPUT:
90 flags |= GPIO_PIN_INPUT;
91 break;
92 case ACPI_IO_RESTRICT_OUTPUT:
93 flags |= GPIO_PIN_OUTPUT;
94 break;
95 }
96 }
97
98 switch (gpio_res->PinConfig) {
99 case ACPI_PIN_CONFIG_PULLUP:
100 flags |= GPIO_PIN_PULLUP;
101 break;
102 case ACPI_PIN_CONFIG_PULLDOWN:
103 flags |= GPIO_PIN_PULLDOWN;
104 break;
105 }
106
107 return (flags);
108 }
109
110 static ACPI_STATUS
acpi_gpiobus_enumerate_res(ACPI_RESOURCE * res,void * context)111 acpi_gpiobus_enumerate_res(ACPI_RESOURCE *res, void *context)
112 {
113 ACPI_RESOURCE_GPIO *gpio_res = &res->Data.Gpio;
114 struct acpi_gpiobus_ctx *ctx = context;
115 struct gpiobus_softc *super_sc = ctx->sc;
116 ACPI_HANDLE handle;
117 uint32_t flags, i;
118
119 if (res->Type != ACPI_RESOURCE_TYPE_GPIO)
120 return (AE_OK);
121
122 if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT,
123 gpio_res->ResourceSource.StringPtr, &handle)) ||
124 handle != ctx->dev_handle)
125 return (AE_OK);
126
127 if (__predict_false(gpio_res->PinTableLength > super_sc->sc_npins)) {
128 device_printf(super_sc->sc_busdev,
129 "invalid pin table length %hu, max: %d (bad ACPI tables?)\n",
130 gpio_res->PinTableLength, super_sc->sc_npins);
131 return (AE_LIMIT);
132 }
133
134 flags = acpi_gpiobus_convflags(gpio_res);
135 for (i = 0; i < gpio_res->PinTableLength; i++) {
136 UINT16 pin = gpio_res->PinTable[i];
137
138 if (__predict_false(pin >= super_sc->sc_npins)) {
139 device_printf(super_sc->sc_busdev,
140 "invalid pin 0x%x, max: 0x%x (bad ACPI tables?)\n",
141 pin, super_sc->sc_npins - 1);
142 return (AE_LIMIT);
143 }
144
145 GPIO_PIN_SETFLAGS(super_sc->sc_dev, pin, flags &
146 ~GPIO_INTR_MASK);
147 }
148
149 return (AE_OK);
150 }
151
152 static struct acpi_gpiobus_ivar *
acpi_gpiobus_setup_devinfo(device_t bus,device_t child,ACPI_RESOURCE_GPIO * gpio_res)153 acpi_gpiobus_setup_devinfo(device_t bus, device_t child,
154 ACPI_RESOURCE_GPIO *gpio_res)
155 {
156 struct acpi_gpiobus_ivar *devi;
157
158 devi = malloc(sizeof(*devi), M_DEVBUF, M_NOWAIT | M_ZERO);
159 if (devi == NULL)
160 return (NULL);
161 resource_list_init(&devi->gpiobus.rl);
162
163 devi->flags = acpi_gpiobus_convflags(gpio_res);
164 if (acpi_quirks & ACPI_Q_AEI_NOPULL)
165 devi->flags &= ~GPIO_PIN_PULLUP;
166
167 devi->gpiobus.npins = 1;
168 if (gpiobus_alloc_ivars(&devi->gpiobus) != 0) {
169 free(devi, M_DEVBUF);
170 return (NULL);
171 }
172
173 for (int i = 0; i < devi->gpiobus.npins; i++)
174 devi->gpiobus.pins[i] = gpio_res->PinTable[i];
175
176 return (devi);
177 }
178
179 static ACPI_STATUS
acpi_gpiobus_enumerate_aei(ACPI_RESOURCE * res,void * context)180 acpi_gpiobus_enumerate_aei(ACPI_RESOURCE *res, void *context)
181 {
182 ACPI_RESOURCE_GPIO *gpio_res = &res->Data.Gpio;
183 struct acpi_gpiobus_ctx *ctx = context;
184 device_t bus = ctx->sc->sc_busdev;
185 device_t child;
186 struct acpi_gpiobus_ivar *devi;
187
188 /* Check that we have a GpioInt object. */
189 if (res->Type != ACPI_RESOURCE_TYPE_GPIO)
190 return (AE_OK);
191 if (gpio_res->ConnectionType != ACPI_RESOURCE_GPIO_TYPE_INT)
192 return (AE_OK);
193
194 /* Add a child. */
195 child = device_add_child_ordered(bus, 0, "gpio_aei", DEVICE_UNIT_ANY);
196 if (child == NULL)
197 return (AE_OK);
198 devi = acpi_gpiobus_setup_devinfo(bus, child, gpio_res);
199 if (devi == NULL) {
200 device_delete_child(bus, child);
201 return (AE_OK);
202 }
203 device_set_ivars(child, devi);
204
205 for (int i = 0; i < devi->gpiobus.npins; i++) {
206 if (GPIOBUS_PIN_SETFLAGS(bus, child, 0, devi->flags)) {
207 device_delete_child(bus, child);
208 return (AE_OK);
209 }
210 }
211
212 /* Pass ACPI information to children. */
213 devi->dev_handle = ctx->dev_handle;
214
215 return (AE_OK);
216 }
217
218 static ACPI_STATUS
acpi_gpiobus_enumerate(ACPI_HANDLE handle,UINT32 depth,void * context,void ** result)219 acpi_gpiobus_enumerate(ACPI_HANDLE handle, UINT32 depth, void *context,
220 void **result)
221 {
222 UINT32 sta;
223
224 /*
225 * If no _STA method or if it failed, then assume that
226 * the device is present.
227 */
228 if (!ACPI_FAILURE(acpi_GetInteger(handle, "_STA", &sta)) &&
229 !ACPI_DEVICE_PRESENT(sta))
230 return (AE_OK);
231
232 if (!acpi_has_hid(handle))
233 return (AE_OK);
234
235 /* Look for GPIO resources */
236 AcpiWalkResources(handle, "_CRS", acpi_gpiobus_enumerate_res, context);
237
238 return (AE_OK);
239 }
240
241 static ACPI_STATUS
acpi_gpiobus_space_handler(UINT32 function,ACPI_PHYSICAL_ADDRESS address,UINT32 length,UINT64 * value,void * context,void * region_context)242 acpi_gpiobus_space_handler(UINT32 function, ACPI_PHYSICAL_ADDRESS address,
243 UINT32 length, UINT64 *value, void *context, void *region_context)
244 {
245 ACPI_CONNECTION_INFO *info = context;
246 ACPI_RESOURCE_GPIO *gpio_res;
247 device_t controller;
248 ACPI_RESOURCE *res;
249 ACPI_STATUS status;
250
251 status = AcpiBufferToResource(info->Connection, info->Length, &res);
252 if (ACPI_FAILURE(status) || res->Type != ACPI_RESOURCE_TYPE_GPIO)
253 goto err;
254
255 gpio_res = &res->Data.Gpio;
256 controller = __containerof(info, struct acpi_gpiobus_softc,
257 handler_info)->super_sc.sc_dev;
258
259 switch (function) {
260 case ACPI_WRITE:
261 if (__predict_false(
262 gpio_res->IoRestriction == ACPI_IO_RESTRICT_INPUT))
263 goto err;
264
265 for (int i = 0; i < length; i++)
266 if (GPIO_PIN_SET(controller,
267 gpio_res->PinTable[address + i], (*value & 1 << i) ?
268 GPIO_PIN_HIGH : GPIO_PIN_LOW) != 0)
269 goto err;
270 break;
271 case ACPI_READ:
272 if (__predict_false(
273 gpio_res->IoRestriction == ACPI_IO_RESTRICT_OUTPUT))
274 goto err;
275
276 for (int i = 0; i < length; i++) {
277 uint32_t v;
278
279 if (GPIO_PIN_GET(controller,
280 gpio_res->PinTable[address + i], &v) != 0)
281 goto err;
282 *value |= v << i;
283 }
284 break;
285 default:
286 goto err;
287 }
288
289 ACPI_FREE(res);
290 return (AE_OK);
291
292 err:
293 ACPI_FREE(res);
294 return (AE_BAD_PARAMETER);
295 }
296
297 static int
acpi_gpiobus_probe(device_t dev)298 acpi_gpiobus_probe(device_t dev)
299 {
300 device_t controller;
301
302 if (acpi_disabled("gpiobus"))
303 return (ENXIO);
304
305 controller = device_get_parent(dev);
306 if (controller == NULL)
307 return (ENXIO);
308
309 if (acpi_get_handle(controller) == NULL)
310 return (ENXIO);
311
312 device_set_desc(dev, "GPIO bus (ACPI-hinted)");
313 return (BUS_PROBE_DEFAULT);
314 }
315
316 static int
acpi_gpiobus_attach(device_t dev)317 acpi_gpiobus_attach(device_t dev)
318 {
319 struct acpi_gpiobus_softc *sc;
320 struct acpi_gpiobus_ctx ctx;
321 ACPI_HANDLE handle;
322 ACPI_STATUS status;
323 int err;
324
325 if ((err = gpiobus_attach(dev)) != 0)
326 return (err);
327
328 sc = device_get_softc(dev);
329 handle = acpi_get_handle(sc->super_sc.sc_dev);
330 if (handle == NULL) {
331 gpiobus_detach(dev);
332 return (ENXIO);
333 }
334
335 status = AcpiInstallAddressSpaceHandler(handle, ACPI_ADR_SPACE_GPIO,
336 acpi_gpiobus_space_handler, NULL, &sc->handler_info);
337
338 if (ACPI_FAILURE(status)) {
339 device_printf(dev,
340 "Failed to install GPIO address space handler\n");
341 gpiobus_detach(dev);
342 return (ENXIO);
343 }
344
345 ctx.dev_handle = handle;
346 ctx.sc = &sc->super_sc;
347
348 status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
349 ACPI_UINT32_MAX, acpi_gpiobus_enumerate, NULL, &ctx, NULL);
350
351 if (ACPI_FAILURE(status))
352 device_printf(dev, "Failed to enumerate GPIO resources\n");
353
354 /* Look for AEI children */
355 status = AcpiWalkResources(handle, "_AEI", acpi_gpiobus_enumerate_aei,
356 &ctx);
357
358 if (ACPI_FAILURE(status))
359 device_printf(dev, "Failed to enumerate GPIO resources\n");
360
361 return (0);
362 }
363
364 static int
acpi_gpiobus_detach(device_t dev)365 acpi_gpiobus_detach(device_t dev)
366 {
367 struct gpiobus_softc *super_sc;
368 ACPI_STATUS status;
369
370 super_sc = device_get_softc(dev);
371 status = AcpiRemoveAddressSpaceHandler(
372 acpi_get_handle(super_sc->sc_dev), ACPI_ADR_SPACE_GPIO,
373 acpi_gpiobus_space_handler
374 );
375
376 if (ACPI_FAILURE(status))
377 device_printf(dev,
378 "Failed to remove GPIO address space handler\n");
379
380 return (gpiobus_detach(dev));
381 }
382
383 int
gpio_pin_get_by_acpi_index(device_t consumer,uint32_t idx,gpio_pin_t * out_pin)384 gpio_pin_get_by_acpi_index(device_t consumer, uint32_t idx,
385 gpio_pin_t *out_pin)
386 {
387 struct acpi_gpiobus_ivar *devi;
388 int rv;
389
390 rv = gpio_pin_get_by_child_index(consumer, idx, out_pin);
391 if (rv != 0)
392 return (rv);
393
394 devi = device_get_ivars(consumer);
395 (*out_pin)->flags = devi->flags;
396
397 return (0);
398 }
399
400 static int
acpi_gpiobus_read_ivar(device_t dev,device_t child,int which,uintptr_t * result)401 acpi_gpiobus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
402 {
403 struct acpi_gpiobus_ivar *devi = device_get_ivars(child);
404
405 switch (which) {
406 case ACPI_GPIOBUS_IVAR_HANDLE:
407 *result = (uintptr_t)devi->dev_handle;
408 break;
409 default:
410 return (gpiobus_read_ivar(dev, child, which, result));
411 }
412
413 return (0);
414 }
415
416 static device_method_t acpi_gpiobus_methods[] = {
417 /* Device interface */
418 DEVMETHOD(device_probe, acpi_gpiobus_probe),
419 DEVMETHOD(device_attach, acpi_gpiobus_attach),
420 DEVMETHOD(device_detach, acpi_gpiobus_detach),
421
422 /* Bus interface */
423 DEVMETHOD(bus_read_ivar, acpi_gpiobus_read_ivar),
424
425 DEVMETHOD_END
426 };
427
428 DEFINE_CLASS_1(gpiobus, acpi_gpiobus_driver, acpi_gpiobus_methods,
429 sizeof(struct acpi_gpiobus_softc), gpiobus_driver);
430 EARLY_DRIVER_MODULE(acpi_gpiobus, gpio, acpi_gpiobus_driver, NULL, NULL,
431 BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
432 MODULE_VERSION(acpi_gpiobus, 1);
433 MODULE_DEPEND(acpi_gpiobus, acpi, 1, 1, 1);
434