2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2018 Advanced Micro Devices
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
38 #include <sys/interrupt.h>
39 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
45 #include <sys/sysctl.h>
47 #include <machine/bus.h>
48 #include <machine/resource.h>
50 #include <contrib/dev/acpica/include/acpi.h>
51 #include <contrib/dev/acpica/include/accommon.h>
53 #include <dev/acpica/acpivar.h>
54 #include <dev/gpio/gpiobusvar.h>
59 static struct resource_spec amdgpio_spec[] = {
60 { SYS_RES_MEMORY, 0, RF_ACTIVE },
64 static inline uint32_t
65 amdgpio_read_4(struct amdgpio_softc *sc, bus_size_t off)
67 return (bus_read_4(sc->sc_res[0], off));
71 amdgpio_write_4(struct amdgpio_softc *sc, bus_size_t off,
74 bus_write_4(sc->sc_res[0], off, val);
78 amdgpio_is_pin_output(struct amdgpio_softc *sc, uint32_t pin)
83 /* Get the current pin state */
86 reg = AMDGPIO_PIN_REGISTER(pin);
87 val = amdgpio_read_4(sc, reg);
89 if (val & BIT(OUTPUT_ENABLE_OFF))
100 amdgpio_get_bus(device_t dev)
102 struct amdgpio_softc *sc;
104 sc = device_get_softc(dev);
106 dprintf("busdev %p\n", sc->sc_busdev);
107 return (sc->sc_busdev);
111 amdgpio_pin_max(device_t dev, int *maxpin)
113 struct amdgpio_softc *sc;
115 sc = device_get_softc(dev);
117 *maxpin = sc->sc_npins - 1;
118 dprintf("npins %d maxpin %d\n", sc->sc_npins, *maxpin);
124 amdgpio_valid_pin(struct amdgpio_softc *sc, int pin)
126 dprintf("pin %d\n", pin);
127 if (sc->sc_res[0] == NULL)
130 if ((sc->sc_gpio_pins[pin].gp_pin == pin) &&
131 (sc->sc_gpio_pins[pin].gp_caps != 0))
138 amdgpio_pin_getname(device_t dev, uint32_t pin, char *name)
140 struct amdgpio_softc *sc;
142 dprintf("pin %d\n", pin);
143 sc = device_get_softc(dev);
145 if (!amdgpio_valid_pin(sc, pin))
148 /* Set a very simple name */
149 snprintf(name, GPIOMAXNAME, "%s", sc->sc_gpio_pins[pin].gp_name);
150 name[GPIOMAXNAME - 1] = '\0';
152 dprintf("pin %d name %s\n", pin, name);
158 amdgpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
160 struct amdgpio_softc *sc;
162 sc = device_get_softc(dev);
164 dprintf("pin %d\n", pin);
165 if (!amdgpio_valid_pin(sc, pin))
168 *caps = sc->sc_gpio_pins[pin].gp_caps;
170 dprintf("pin %d caps 0x%x\n", pin, *caps);
176 amdgpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
178 struct amdgpio_softc *sc;
180 sc = device_get_softc(dev);
182 dprintf("pin %d\n", pin);
183 if (!amdgpio_valid_pin(sc, pin))
188 *flags = sc->sc_gpio_pins[pin].gp_flags;
190 dprintf("pin %d flags 0x%x\n", pin, *flags);
198 amdgpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
200 struct amdgpio_softc *sc;
201 uint32_t reg, val, allowed;
203 sc = device_get_softc(dev);
205 dprintf("pin %d flags 0x%x\n", pin, flags);
206 if (!amdgpio_valid_pin(sc, pin))
209 allowed = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
212 * Only directtion flag allowed
214 if (flags & ~allowed)
218 * Not both directions simultaneously
220 if ((flags & allowed) == allowed)
223 /* Set the GPIO mode and state */
226 reg = AMDGPIO_PIN_REGISTER(pin);
227 val = amdgpio_read_4(sc, reg);
229 if (flags & GPIO_PIN_INPUT) {
230 val &= ~BIT(OUTPUT_ENABLE_OFF);
231 sc->sc_gpio_pins[pin].gp_flags = GPIO_PIN_INPUT;
233 val |= BIT(OUTPUT_ENABLE_OFF);
234 sc->sc_gpio_pins[pin].gp_flags = GPIO_PIN_OUTPUT;
237 amdgpio_write_4(sc, reg, val);
239 dprintf("pin %d flags 0x%x val 0x%x gp_flags 0x%x\n",
240 pin, flags, val, sc->sc_gpio_pins[pin].gp_flags);
248 amdgpio_pin_get(device_t dev, uint32_t pin, unsigned int *value)
250 struct amdgpio_softc *sc;
253 sc = device_get_softc(dev);
255 dprintf("pin %d\n", pin);
256 if (!amdgpio_valid_pin(sc, pin))
263 reg = AMDGPIO_PIN_REGISTER(pin);
264 val = amdgpio_read_4(sc, reg);
266 if ((sc->sc_gpio_pins[pin].gp_flags & GPIO_PIN_OUTPUT) != 0) {
267 if (val & BIT(OUTPUT_VALUE_OFF))
268 *value = GPIO_PIN_HIGH;
270 *value = GPIO_PIN_LOW;
272 if (val & BIT(PIN_STS_OFF))
273 *value = GPIO_PIN_HIGH;
275 *value = GPIO_PIN_LOW;
278 dprintf("pin %d value 0x%x\n", pin, *value);
286 amdgpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
288 struct amdgpio_softc *sc;
291 sc = device_get_softc(dev);
293 dprintf("pin %d value 0x%x\n", pin, value);
294 if (!amdgpio_valid_pin(sc, pin))
297 if (!amdgpio_is_pin_output(sc, pin))
302 reg = AMDGPIO_PIN_REGISTER(pin);
303 val = amdgpio_read_4(sc, reg);
305 if (value == GPIO_PIN_LOW)
306 val &= ~BIT(OUTPUT_VALUE_OFF);
308 val |= BIT(OUTPUT_VALUE_OFF);
310 amdgpio_write_4(sc, reg, val);
312 dprintf("pin %d value 0x%x val 0x%x\n", pin, value, val);
320 amdgpio_pin_toggle(device_t dev, uint32_t pin)
322 struct amdgpio_softc *sc;
325 sc = device_get_softc(dev);
327 dprintf("pin %d\n", pin);
328 if (!amdgpio_valid_pin(sc, pin))
331 if (!amdgpio_is_pin_output(sc, pin))
337 reg = AMDGPIO_PIN_REGISTER(pin);
338 val = amdgpio_read_4(sc, reg);
339 dprintf("pin %d value before 0x%x\n", pin, val);
340 val = val ^ BIT(OUTPUT_VALUE_OFF);
341 dprintf("pin %d value after 0x%x\n", pin, val);
342 amdgpio_write_4(sc, reg, val);
350 amdgpio_probe(device_t dev)
352 static char *gpio_ids[] = { "AMD0030", "AMDI0030", NULL };
355 if (acpi_disabled("gpio"))
357 rv = ACPI_ID_PROBE(device_get_parent(dev), dev, gpio_ids, NULL);
359 device_set_desc(dev, "AMD GPIO Controller");
365 amdgpio_attach(device_t dev)
367 struct amdgpio_softc *sc;
370 sc = device_get_softc(dev);
372 sc->sc_handle = acpi_get_handle(dev);
374 AMDGPIO_LOCK_INIT(sc);
376 sc->sc_nbanks = AMD_GPIO_NUM_PIN_BANK;
377 sc->sc_npins = AMD_GPIO_PINS_MAX;
378 sc->sc_bank_prefix = AMD_GPIO_PREFIX;
379 sc->sc_pin_info = kernzp_pins;
380 sc->sc_ngroups = nitems(kernzp_groups);
381 sc->sc_groups = kernzp_groups;
383 if (bus_alloc_resources(dev, amdgpio_spec, sc->sc_res)) {
384 device_printf(dev, "could not allocate resources\n");
388 sc->sc_bst = rman_get_bustag(sc->sc_res[0]);
389 sc->sc_bsh = rman_get_bushandle(sc->sc_res[0]);
391 /* Initialize all possible pins to be Invalid */
392 for (i = 0; i < AMD_GPIO_PINS_MAX ; i++) {
393 snprintf(sc->sc_gpio_pins[i].gp_name, GPIOMAXNAME,
394 "Unexposed PIN %d", i);
395 sc->sc_gpio_pins[i].gp_pin = -1;
396 sc->sc_gpio_pins[i].gp_caps = 0;
397 sc->sc_gpio_pins[i].gp_flags = 0;
400 /* Initialize only driver exposed pins with appropriate capabilities */
401 for (i = 0; i < AMD_GPIO_PINS_EXPOSED ; i++) {
402 pin = kernzp_pins[i].pin_num;
403 bank = pin/AMD_GPIO_PINS_PER_BANK;
404 snprintf(sc->sc_gpio_pins[pin].gp_name, GPIOMAXNAME, "%s%d_%s",
405 AMD_GPIO_PREFIX, bank, kernzp_pins[i].pin_name);
406 sc->sc_gpio_pins[pin].gp_pin = pin;
407 sc->sc_gpio_pins[pin].gp_caps = AMDGPIO_DEFAULT_CAPS;
408 sc->sc_gpio_pins[pin].gp_flags =
409 amdgpio_is_pin_output(sc, pin) ?
410 GPIO_PIN_OUTPUT : GPIO_PIN_INPUT;
413 sc->sc_busdev = gpiobus_attach_bus(dev);
414 if (sc->sc_busdev == NULL) {
415 device_printf(dev, "could not attach gpiobus\n");
422 bus_release_resources(dev, amdgpio_spec, sc->sc_res);
425 AMDGPIO_LOCK_DESTROY(sc);
431 amdgpio_detach(device_t dev)
433 struct amdgpio_softc *sc;
434 sc = device_get_softc(dev);
437 gpiobus_detach_bus(dev);
439 bus_release_resources(dev, amdgpio_spec, sc->sc_res);
441 AMDGPIO_LOCK_DESTROY(sc);
446 static device_method_t amdgpio_methods[] = {
447 /* Device interface */
448 DEVMETHOD(device_probe, amdgpio_probe),
449 DEVMETHOD(device_attach, amdgpio_attach),
450 DEVMETHOD(device_detach, amdgpio_detach),
453 DEVMETHOD(gpio_get_bus, amdgpio_get_bus),
454 DEVMETHOD(gpio_pin_max, amdgpio_pin_max),
455 DEVMETHOD(gpio_pin_getname, amdgpio_pin_getname),
456 DEVMETHOD(gpio_pin_getcaps, amdgpio_pin_getcaps),
457 DEVMETHOD(gpio_pin_getflags, amdgpio_pin_getflags),
458 DEVMETHOD(gpio_pin_setflags, amdgpio_pin_setflags),
459 DEVMETHOD(gpio_pin_get, amdgpio_pin_get),
460 DEVMETHOD(gpio_pin_set, amdgpio_pin_set),
461 DEVMETHOD(gpio_pin_toggle, amdgpio_pin_toggle),
466 static driver_t amdgpio_driver = {
469 sizeof(struct amdgpio_softc),
472 static devclass_t amdgpio_devclass;
473 DRIVER_MODULE(amdgpio, acpi, amdgpio_driver, amdgpio_devclass, 0, 0);
474 MODULE_DEPEND(amdgpio, acpi, 1, 1, 1);
475 MODULE_DEPEND(amdgpio, gpiobus, 1, 1, 1);
476 MODULE_VERSION(amdgpio, 1);