Home | History | Annotate | Line # | Download | only in ti
      1  1.1  yurix /* $NetBSD: am18xx_gpio.c,v 1.1 2026/09/27 19:56:21 yurix Exp $ */
      2  1.1  yurix 
      3  1.1  yurix /*-
      4  1.1  yurix  * Copyright (c) 2026 The NetBSD Foundation, Inc.
      5  1.1  yurix  * All rights reserved.
      6  1.1  yurix  *
      7  1.1  yurix  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  yurix  * by Yuri Honegger.
      9  1.1  yurix  *
     10  1.1  yurix  * Redistribution and use in source and binary forms, with or without
     11  1.1  yurix  * modification, are permitted provided that the following conditions
     12  1.1  yurix  * are met:
     13  1.1  yurix  * 1. Redistributions of source code must retain the above copyright
     14  1.1  yurix  *    notice, this list of conditions and the following disclaimer.
     15  1.1  yurix  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  yurix  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  yurix  *    documentation and/or other materials provided with the distribution.
     18  1.1  yurix  *
     19  1.1  yurix  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  yurix  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  yurix  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  yurix  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  yurix  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  yurix  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  yurix  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  yurix  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  yurix  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  yurix  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  yurix  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  yurix  */
     31  1.1  yurix 
     32  1.1  yurix /*
     33  1.1  yurix  * GPIOs for the TI AM18XX SoC
     34  1.1  yurix  */
     35  1.1  yurix 
     36  1.1  yurix #include <sys/param.h>
     37  1.1  yurix #include <sys/cdefs.h>
     38  1.1  yurix #include <sys/device.h>
     39  1.1  yurix #include <sys/gpio.h>
     40  1.1  yurix #include <sys/kmem.h>
     41  1.1  yurix #include <sys/mutex.h>
     42  1.1  yurix 
     43  1.1  yurix #include <dev/fdt/fdtvar.h>
     44  1.1  yurix #include <dev/gpio/gpiovar.h>
     45  1.1  yurix 
     46  1.1  yurix struct am18xx_gpio_fdt_pin {
     47  1.1  yurix 	int pin_nr;
     48  1.1  yurix 	bool pin_actlo;
     49  1.1  yurix };
     50  1.1  yurix 
     51  1.1  yurix struct am18xx_gpio_softc {
     52  1.1  yurix 	bus_space_tag_t sc_bst;
     53  1.1  yurix 	bus_space_handle_t sc_bsh;
     54  1.1  yurix 	kmutex_t sc_lock;
     55  1.1  yurix 	struct gpio_chipset_tag sc_gc;
     56  1.1  yurix 	int sc_num_gpios;
     57  1.1  yurix };
     58  1.1  yurix 
     59  1.1  yurix static int 	am18xx_gpio_match(device_t, cfdata_t, void *);
     60  1.1  yurix static void 	am18xx_gpio_attach(device_t, device_t, void *);
     61  1.1  yurix static void *	am18xx_gpio_fdt_acquire(device_t, const void *, size_t, int);
     62  1.1  yurix static void 	am18xx_gpio_fdt_release(device_t, void *);
     63  1.1  yurix static int 	am18xx_gpio_fdt_read(device_t, void *, bool);
     64  1.1  yurix static void 	am18xx_gpio_fdt_write(device_t, void *, int, bool);
     65  1.1  yurix static int	am18xx_gpio_gp_pin_read(void *, int);
     66  1.1  yurix static void	am18xx_gpio_gp_pin_write(void *, int, int);
     67  1.1  yurix static void	am18xx_gpio_gp_pin_ctl(void *, int, int);
     68  1.1  yurix 
     69  1.1  yurix CFATTACH_DECL_NEW(am18xxgpio, sizeof(struct am18xx_gpio_softc),
     70  1.1  yurix     am18xx_gpio_match, am18xx_gpio_attach, NULL, NULL);
     71  1.1  yurix 
     72  1.1  yurix #define AM18XX_GPIO_BANK_BASE	0x10
     73  1.1  yurix #define AM18XX_GPIO_BANK_SIZE	0x28
     74  1.1  yurix #define AM18XX_GPIO_BANK_DIR	0x0
     75  1.1  yurix #define AM18XX_GPIO_BANK_SET	0x8
     76  1.1  yurix #define AM18XX_GPIO_BANK_CLEAR	0xC
     77  1.1  yurix #define AM18XX_GPIO_BANK_INPUT	0x10
     78  1.1  yurix 
     79  1.1  yurix #define	GPIO_READ(sc, reg)					\
     80  1.1  yurix 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, reg)
     81  1.1  yurix #define	GPIO_WRITE(sc, reg, val)				\
     82  1.1  yurix 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, reg, val)
     83  1.1  yurix 
     84  1.1  yurix static const struct device_compatible_entry compat_data[] = {
     85  1.1  yurix 	{ .compat = "ti,dm6441-gpio" },
     86  1.1  yurix 	DEVICE_COMPAT_EOL
     87  1.1  yurix };
     88  1.1  yurix 
     89  1.1  yurix static const struct fdtbus_gpio_controller_func am18xx_gpio_fdt_funcs = {
     90  1.1  yurix 	.acquire = am18xx_gpio_fdt_acquire,
     91  1.1  yurix 	.release = am18xx_gpio_fdt_release,
     92  1.1  yurix 	.read = am18xx_gpio_fdt_read,
     93  1.1  yurix 	.write = am18xx_gpio_fdt_write,
     94  1.1  yurix };
     95  1.1  yurix 
     96  1.1  yurix static void *
     97  1.1  yurix am18xx_gpio_fdt_acquire(device_t dev, const void *rdata, size_t len, int flags)
     98  1.1  yurix {
     99  1.1  yurix 	struct am18xx_gpio_softc * const sc = device_private(dev);
    100  1.1  yurix 	const uint32_t *data = rdata;
    101  1.1  yurix 	struct am18xx_gpio_fdt_pin *pin;
    102  1.1  yurix 
    103  1.1  yurix 	if (len != 12)
    104  1.1  yurix 		return NULL;
    105  1.1  yurix 
    106  1.1  yurix 	int pin_nr = be32toh(data[1]);
    107  1.1  yurix 	int actlo = be32toh(data[2]) & 1;
    108  1.1  yurix 
    109  1.1  yurix 	if (pin_nr < 0 || pin_nr >= sc->sc_num_gpios)
    110  1.1  yurix 		return NULL;
    111  1.1  yurix 
    112  1.1  yurix 	pin = kmem_zalloc(sizeof(struct am18xx_gpio_fdt_pin), KM_SLEEP);
    113  1.1  yurix 	if (pin == NULL) {
    114  1.1  yurix 		return NULL;
    115  1.1  yurix 	}
    116  1.1  yurix 
    117  1.1  yurix 	pin->pin_nr = pin_nr;
    118  1.1  yurix 	pin->pin_actlo = actlo;
    119  1.1  yurix 
    120  1.1  yurix 	gpiobus_pin_ctl(&sc->sc_gc, pin->pin_nr, flags);
    121  1.1  yurix 
    122  1.1  yurix 	return pin;
    123  1.1  yurix }
    124  1.1  yurix 
    125  1.1  yurix static void
    126  1.1  yurix am18xx_gpio_fdt_release(device_t dev, void *priv)
    127  1.1  yurix {
    128  1.1  yurix 	struct am18xx_gpio_softc * const sc = device_private(dev);
    129  1.1  yurix 	struct am18xx_gpio_fdt_pin * const pin = priv;
    130  1.1  yurix 
    131  1.1  yurix 	am18xx_gpio_gp_pin_ctl(sc, pin->pin_nr, GPIO_PIN_INPUT);
    132  1.1  yurix 
    133  1.1  yurix 	kmem_free(pin, sizeof(*pin));
    134  1.1  yurix }
    135  1.1  yurix 
    136  1.1  yurix static int
    137  1.1  yurix am18xx_gpio_fdt_read(device_t dev, void *priv, bool raw)
    138  1.1  yurix {
    139  1.1  yurix 	struct am18xx_gpio_softc * const sc = device_private(dev);
    140  1.1  yurix 	struct am18xx_gpio_fdt_pin * const pin = priv;
    141  1.1  yurix 	int val;
    142  1.1  yurix 
    143  1.1  yurix 	val = am18xx_gpio_gp_pin_read(sc, pin->pin_nr);
    144  1.1  yurix 
    145  1.1  yurix 	if (!raw && pin->pin_actlo)
    146  1.1  yurix 		val = !val;
    147  1.1  yurix 
    148  1.1  yurix 	return val;
    149  1.1  yurix }
    150  1.1  yurix 
    151  1.1  yurix static void
    152  1.1  yurix am18xx_gpio_fdt_write(device_t dev, void *priv, int val, bool raw)
    153  1.1  yurix {
    154  1.1  yurix 	struct am18xx_gpio_softc * const sc = device_private(dev);
    155  1.1  yurix 	struct am18xx_gpio_fdt_pin * const pin = priv;
    156  1.1  yurix 
    157  1.1  yurix 	if (!raw && pin->pin_actlo)
    158  1.1  yurix 		val = !val;
    159  1.1  yurix 
    160  1.1  yurix 	am18xx_gpio_gp_pin_write(sc, pin->pin_nr, val);
    161  1.1  yurix }
    162  1.1  yurix 
    163  1.1  yurix static int
    164  1.1  yurix am18xx_gpio_gp_pin_read(void *priv, int pin)
    165  1.1  yurix {
    166  1.1  yurix 	struct am18xx_gpio_softc * const sc = priv;
    167  1.1  yurix 
    168  1.1  yurix 	KASSERT(pin >= 0 && pin < sc->sc_num_gpios);
    169  1.1  yurix 
    170  1.1  yurix 	int bank_group = pin / 32;
    171  1.1  yurix 	uint32_t group_bit = __BIT(pin % 32);
    172  1.1  yurix 	int bank_base = AM18XX_GPIO_BANK_BASE +
    173  1.1  yurix 			bank_group * AM18XX_GPIO_BANK_SIZE;
    174  1.1  yurix 
    175  1.1  yurix 	/* GPIO input value reads are built to be atomic */
    176  1.1  yurix 	uint32_t val = GPIO_READ(sc, bank_base + AM18XX_GPIO_BANK_INPUT);
    177  1.1  yurix 
    178  1.1  yurix 	return (val & group_bit) != 0;
    179  1.1  yurix }
    180  1.1  yurix 
    181  1.1  yurix static void
    182  1.1  yurix am18xx_gpio_gp_pin_write(void *priv, int pin, int val)
    183  1.1  yurix {
    184  1.1  yurix 	struct am18xx_gpio_softc * const sc = priv;
    185  1.1  yurix 
    186  1.1  yurix 	KASSERT(pin >= 0 && pin < sc->sc_num_gpios);
    187  1.1  yurix 
    188  1.1  yurix 	int bank_group = pin / 32;
    189  1.1  yurix 	uint32_t group_bit = __BIT(pin % 32);
    190  1.1  yurix 	int bank_base = AM18XX_GPIO_BANK_BASE +
    191  1.1  yurix 			bank_group * AM18XX_GPIO_BANK_SIZE;
    192  1.1  yurix 
    193  1.1  yurix 	/*
    194  1.1  yurix 	 * We do *not* need the lock because BANK_SET/BANK_CLEAR is built to
    195  1.1  yurix 	 * be set by multiple threads at once.
    196  1.1  yurix 	 */
    197  1.1  yurix 	if (val) {
    198  1.1  yurix 		GPIO_WRITE(sc, bank_base + AM18XX_GPIO_BANK_SET, group_bit);
    199  1.1  yurix 	} else {
    200  1.1  yurix 		GPIO_WRITE(sc, bank_base + AM18XX_GPIO_BANK_CLEAR, group_bit);
    201  1.1  yurix 	}
    202  1.1  yurix }
    203  1.1  yurix 
    204  1.1  yurix static void
    205  1.1  yurix am18xx_gpio_gp_pin_ctl(void *priv, int pin, int flags)
    206  1.1  yurix {
    207  1.1  yurix 	struct am18xx_gpio_softc * const sc = priv;
    208  1.1  yurix 
    209  1.1  yurix 	KASSERT(pin >= 0 && pin < sc->sc_num_gpios);
    210  1.1  yurix 
    211  1.1  yurix 	int bank_group = pin / 32;
    212  1.1  yurix 	uint32_t group_bit = __BIT(pin % 32);
    213  1.1  yurix 	int bank_base = AM18XX_GPIO_BANK_BASE +
    214  1.1  yurix 			bank_group * AM18XX_GPIO_BANK_SIZE;
    215  1.1  yurix 
    216  1.1  yurix 	mutex_enter(&sc->sc_lock);
    217  1.1  yurix 	if (flags & GPIO_PIN_OUTPUT) {
    218  1.1  yurix 		uint32_t val = GPIO_READ(sc, bank_base + AM18XX_GPIO_BANK_DIR);
    219  1.1  yurix 		val &= ~group_bit;
    220  1.1  yurix 		GPIO_WRITE(sc, bank_base + AM18XX_GPIO_BANK_DIR, val);
    221  1.1  yurix 	} else if (flags & GPIO_PIN_INPUT) {
    222  1.1  yurix 		uint32_t val = GPIO_READ(sc, bank_base + AM18XX_GPIO_BANK_DIR);
    223  1.1  yurix 		val |= group_bit;
    224  1.1  yurix 		GPIO_WRITE(sc, bank_base + AM18XX_GPIO_BANK_DIR, val);
    225  1.1  yurix 	}
    226  1.1  yurix 	mutex_exit(&sc->sc_lock);
    227  1.1  yurix }
    228  1.1  yurix 
    229  1.1  yurix int
    230  1.1  yurix am18xx_gpio_match(device_t parent, cfdata_t cf, void *aux)
    231  1.1  yurix {
    232  1.1  yurix 	struct fdt_attach_args * const faa = aux;
    233  1.1  yurix 
    234  1.1  yurix 	return of_compatible_match(faa->faa_phandle, compat_data);
    235  1.1  yurix }
    236  1.1  yurix 
    237  1.1  yurix void
    238  1.1  yurix am18xx_gpio_attach(device_t parent, device_t self, void *aux)
    239  1.1  yurix {
    240  1.1  yurix 	struct am18xx_gpio_softc * const sc = device_private(self);
    241  1.1  yurix 	struct fdt_attach_args * const faa = aux;
    242  1.1  yurix 	const int phandle = faa->faa_phandle;
    243  1.1  yurix 	bus_addr_t addr;
    244  1.1  yurix 	bus_size_t size;
    245  1.1  yurix 
    246  1.1  yurix 	sc->sc_bst = faa->faa_bst;
    247  1.1  yurix 
    248  1.1  yurix 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    249  1.1  yurix 
    250  1.1  yurix 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    251  1.1  yurix 		aprint_error(": couldn't get registers\n");
    252  1.1  yurix 		return;
    253  1.1  yurix 	}
    254  1.1  yurix 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    255  1.1  yurix 		aprint_error(": couldn't map registers\n");
    256  1.1  yurix 		return;
    257  1.1  yurix 	}
    258  1.1  yurix 
    259  1.1  yurix 	if (fdtbus_clock_enable(phandle, "gpio", true) != 0) {
    260  1.1  yurix 		aprint_error(": couldn't enable clock\n");
    261  1.1  yurix 		return;
    262  1.1  yurix 	}
    263  1.1  yurix 
    264  1.1  yurix 	if (of_getprop_uint32(phandle, "ti,ngpio", &sc->sc_num_gpios) != 0) {
    265  1.1  yurix 		aprint_error(": couldn't get gpio count\n");
    266  1.1  yurix 		return;
    267  1.1  yurix 	}
    268  1.1  yurix 
    269  1.1  yurix 	gpio_pin_t *gpio_pin_desc = kmem_alloc(
    270  1.1  yurix 	    sc->sc_num_gpios * sizeof(gpio_pin_t), KM_SLEEP);
    271  1.1  yurix 	if (gpio_pin_desc == NULL) {
    272  1.1  yurix 		aprint_error(": couldn't allocate memory\n");
    273  1.1  yurix 		return;
    274  1.1  yurix 	}
    275  1.1  yurix 	memset(gpio_pin_desc, 0, sc->sc_num_gpios * sizeof(gpio_pin_t));
    276  1.1  yurix 	for (int i = 0; i < sc->sc_num_gpios; i++) {
    277  1.1  yurix 		gpio_pin_desc[i].pin_num = i;
    278  1.1  yurix 		gpio_pin_desc[i].pin_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT |
    279  1.1  yurix 					    GPIO_PIN_INOUT | GPIO_PIN_PUSHPULL;
    280  1.1  yurix 		gpio_pin_desc[i].pin_gc = &sc->sc_gc;
    281  1.1  yurix 	}
    282  1.1  yurix 
    283  1.1  yurix 	sc->sc_gc.gp_cookie = sc;
    284  1.1  yurix 	sc->sc_gc.gp_pin_read = am18xx_gpio_gp_pin_read;
    285  1.1  yurix 	sc->sc_gc.gp_pin_write = am18xx_gpio_gp_pin_write;
    286  1.1  yurix 	sc->sc_gc.gp_pin_ctl = am18xx_gpio_gp_pin_ctl;
    287  1.1  yurix 
    288  1.1  yurix 	struct gpiobus_attach_args gpiobus_aa;
    289  1.1  yurix 	gpiobus_aa.gba_gc = &sc->sc_gc;
    290  1.1  yurix 	gpiobus_aa.gba_npins = sc->sc_num_gpios;
    291  1.1  yurix 	gpiobus_aa.gba_pins = gpio_pin_desc;
    292  1.1  yurix 
    293  1.1  yurix 	aprint_naive("\n");
    294  1.1  yurix 	aprint_normal("\n");
    295  1.1  yurix 
    296  1.1  yurix 	config_found(self, &gpiobus_aa, gpiobus_print, CFARGS_NONE);
    297  1.1  yurix 
    298  1.1  yurix 	fdtbus_register_gpio_controller(self, phandle, &am18xx_gpio_fdt_funcs);
    299  1.1  yurix }
    300  1.1  yurix 
    301