Home | History | Annotate | Line # | Download | only in samsung
exynos_gpio.c revision 1.6.2.2
      1  1.6.2.2  yamt /*-
      2  1.6.2.2  yamt * Copyright (c) 2014 The NetBSD Foundation, Inc.
      3  1.6.2.2  yamt * All rights reserved.
      4  1.6.2.2  yamt *
      5  1.6.2.2  yamt * This code is derived from software contributed to The NetBSD Foundation
      6  1.6.2.2  yamt * by Reinoud Zandijk
      7  1.6.2.2  yamt *
      8  1.6.2.2  yamt * Redistribution and use in source and binary forms, with or without
      9  1.6.2.2  yamt * modification, are permitted provided that the following conditions
     10  1.6.2.2  yamt * are met:
     11  1.6.2.2  yamt * 1. Redistributions of source code must retain the above copyright
     12  1.6.2.2  yamt *    notice, this list of conditions and the following disclaimer.
     13  1.6.2.2  yamt * 2. Redistributions in binary form must reproduce the above copyright
     14  1.6.2.2  yamt *    notice, this list of conditions and the following disclaimer in the
     15  1.6.2.2  yamt *    documentation and/or other materials provided with the distribution.
     16  1.6.2.2  yamt *
     17  1.6.2.2  yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  1.6.2.2  yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  1.6.2.2  yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  1.6.2.2  yamt * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  1.6.2.2  yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  1.6.2.2  yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  1.6.2.2  yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  1.6.2.2  yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  1.6.2.2  yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  1.6.2.2  yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  1.6.2.2  yamt * POSSIBILITY OF SUCH DAMAGE.
     28  1.6.2.2  yamt */
     29  1.6.2.2  yamt 
     30  1.6.2.2  yamt #include "opt_exynos.h"
     31  1.6.2.2  yamt #include "opt_arm_debug.h"
     32  1.6.2.2  yamt #include "gpio.h"
     33  1.6.2.2  yamt 
     34  1.6.2.2  yamt #include <sys/cdefs.h>
     35  1.6.2.2  yamt __KERNEL_RCSID(1, "$NetBSD: exynos_gpio.c,v 1.6.2.2 2014/05/22 11:39:34 yamt Exp $");
     36  1.6.2.2  yamt 
     37  1.6.2.2  yamt #include <sys/param.h>
     38  1.6.2.2  yamt #include <sys/bus.h>
     39  1.6.2.2  yamt #include <sys/device.h>
     40  1.6.2.2  yamt #include <sys/intr.h>
     41  1.6.2.2  yamt #include <sys/systm.h>
     42  1.6.2.2  yamt #include <sys/kmem.h>
     43  1.6.2.2  yamt 
     44  1.6.2.2  yamt #include <arm/samsung/exynos_reg.h>
     45  1.6.2.2  yamt #include <arm/samsung/exynos_io.h>
     46  1.6.2.2  yamt #include <arm/samsung/exynos_intr.h>
     47  1.6.2.2  yamt 
     48  1.6.2.2  yamt #include <sys/gpio.h>
     49  1.6.2.2  yamt #include <dev/gpio/gpiovar.h>
     50  1.6.2.2  yamt 
     51  1.6.2.2  yamt static int exynos_gpio_match(device_t, cfdata_t, void *);
     52  1.6.2.2  yamt static void exynos_gpio_attach(device_t, device_t, void *);
     53  1.6.2.2  yamt 
     54  1.6.2.2  yamt static int exynos_gpio_pin_read(void *, int);
     55  1.6.2.2  yamt static void exynos_gpio_pin_write(void *, int, int);
     56  1.6.2.2  yamt static void exynos_gpio_pin_ctl(void *, int, int);
     57  1.6.2.2  yamt 
     58  1.6.2.2  yamt struct exynos_gpio_pin_cfg {
     59  1.6.2.2  yamt 	uint32_t cfg;
     60  1.6.2.2  yamt 	uint32_t pud;
     61  1.6.2.2  yamt 	uint32_t drv;
     62  1.6.2.2  yamt 	uint32_t conpwd;
     63  1.6.2.2  yamt 	uint32_t pudpwd;
     64  1.6.2.2  yamt };
     65  1.6.2.2  yamt 
     66  1.6.2.2  yamt struct exynos_gpio_pin_group {
     67  1.6.2.2  yamt 	const char		grp_name[6];
     68  1.6.2.2  yamt 	const bus_addr_t	grp_core_offset;
     69  1.6.2.2  yamt 	const uint8_t		grp_bits;
     70  1.6.2.2  yamt 
     71  1.6.2.2  yamt 	uint8_t			grp_pin_mask;
     72  1.6.2.2  yamt 	uint8_t			grp_pin_inuse_mask;
     73  1.6.2.2  yamt 	bus_space_handle_t	grp_bsh;
     74  1.6.2.2  yamt 	struct exynos_gpio_pin_cfg grp_cfg;
     75  1.6.2.2  yamt 	struct gpio_chipset_tag grp_gc_tag;
     76  1.6.2.2  yamt };
     77  1.6.2.2  yamt 
     78  1.6.2.2  yamt 
     79  1.6.2.2  yamt #define GPIO_OFFSET(v,s,o) (EXYNOS##v##_GPIO_##s##_OFFSET + (o))
     80  1.6.2.2  yamt #define GPIO_GRP(v, s, o, n, b) \
     81  1.6.2.2  yamt 	{ \
     82  1.6.2.2  yamt 		.grp_name = #n, \
     83  1.6.2.2  yamt 		.grp_core_offset = GPIO_OFFSET(v,s,o), \
     84  1.6.2.2  yamt 		.grp_bits = b,\
     85  1.6.2.2  yamt 	}
     86  1.6.2.2  yamt 
     87  1.6.2.2  yamt #ifdef EXYNOS4
     88  1.6.2.2  yamt /*
     89  1.6.2.2  yamt  * Exynos 4412 contains 304 multi-functional input/output port pins and 164
     90  1.6.2.2  yamt  * memory port pins. There are 37 general port groups and two memory port
     91  1.6.2.2  yamt  * groups. They are:
     92  1.6.2.2  yamt  *
     93  1.6.2.2  yamt  *  GPA0, GPA1: 14 in/out ports-3xUART with flow control, UART without flow
     94  1.6.2.2  yamt  *  control, and/or 2xI2C
     95  1.6.2.2  yamt  *
     96  1.6.2.2  yamt  *  GPB: 8 in/out ports-2xSPI and/or 2xI2C and/ or IEM
     97  1.6.2.2  yamt  *
     98  1.6.2.2  yamt  *  GPC0, GPC1: 10 in/out ports-2xI2S, and/or 2xPCM, and/or AC97, SPDIF, I2C,
     99  1.6.2.2  yamt  *  and/or SPI
    100  1.6.2.2  yamt  *
    101  1.6.2.2  yamt  *  GPD0, GPD1: 8 in/out ports-PWM, 2xI2C, and/ or LCD I/F, MIPI
    102  1.6.2.2  yamt  *
    103  1.6.2.2  yamt  *  GPM0, GPM1, GPM2, GPM3, GPM4: 35 in/out ports-CAM I/F, and/ or TS I/F,
    104  1.6.2.2  yamt  *  HSI, and/ or Trace I/F
    105  1.6.2.2  yamt  *
    106  1.6.2.2  yamt  *  GPF0, GPF1, GPF2, GPF3: 30 in/out ports-LCD I/F
    107  1.6.2.2  yamt  *
    108  1.6.2.2  yamt  *  GPJ0, GPJ1: 13 in/out ports-CAM I/F
    109  1.6.2.2  yamt  *
    110  1.6.2.2  yamt  *  GPK0, GPK1, GPK2, GPK3: 28 in/out ports-4xMMC (4-bit MMC), and/or 2xMMC
    111  1.6.2.2  yamt  *  (8-bit MMC)), and/or GPS debugging I/F
    112  1.6.2.2  yamt  *
    113  1.6.2.2  yamt  *  GPL0, GPL1: 11 in/out ports-GPS I/F
    114  1.6.2.2  yamt  *
    115  1.6.2.2  yamt  *  GPL2: 8 in/out ports-GPS debugging I/F or Key pad I/F
    116  1.6.2.2  yamt  *
    117  1.6.2.2  yamt  *  GPX0, GPX1, GPX2, GPX3: 32 in/out ports-External wake-up, and/or Key pad
    118  1.6.2.2  yamt  *  I/F
    119  1.6.2.2  yamt  *
    120  1.6.2.2  yamt  *  GPZ: 7 in/out ports-low Power I2S and/or PCM
    121  1.6.2.2  yamt  *
    122  1.6.2.2  yamt  *  GPY0, GPY1, GPY2: 16 in/out ports-Control signals of EBI (SROM, NF, One
    123  1.6.2.2  yamt  *  NAND)
    124  1.6.2.2  yamt  *
    125  1.6.2.2  yamt  *  GPY3, GPY4, GPY5, GPY6: 32 in/out memory ports-EBI (For more information
    126  1.6.2.2  yamt  *  about EBI configuration, refer to Chapter 5, and 6)
    127  1.6.2.2  yamt  *
    128  1.6.2.2  yamt  *  MP1_0-MP1_9: 78 DRAM1 ports. NOTE: GPIO registers does not control these
    129  1.6.2.2  yamt  *  ports.
    130  1.6.2.2  yamt  *
    131  1.6.2.2  yamt  *  MP2_0-MP2_9: 78 DRAM2 ports. NOTE: GPIO registers does not control these
    132  1.6.2.2  yamt  *  ports.
    133  1.6.2.2  yamt  *
    134  1.6.2.2  yamt  *  ETC0, ETC1, ETC6: 18 in/out ETC ports-JTAG, SLIMBUS, RESET, CLOCK
    135  1.6.2.2  yamt  *
    136  1.6.2.2  yamt  *  ETC7, ETC8 : 4 clock port for C2C
    137  1.6.2.2  yamt  *
    138  1.6.2.2  yamt  */
    139  1.6.2.2  yamt 
    140  1.6.2.2  yamt static struct exynos_gpio_pin_group exynos4_pin_groups[] = {
    141  1.6.2.2  yamt 	GPIO_GRP(4, LEFT,  0x0000, GPA0, 8),
    142  1.6.2.2  yamt 	GPIO_GRP(4, LEFT,  0x0020, GPA1, 6),
    143  1.6.2.2  yamt 	GPIO_GRP(4, LEFT,  0x0040, GPB,  8),
    144  1.6.2.2  yamt 	GPIO_GRP(4, LEFT,  0x0060, GPC0, 5),
    145  1.6.2.2  yamt 	GPIO_GRP(4, LEFT,  0x0080, GPC1, 5),
    146  1.6.2.2  yamt 	GPIO_GRP(4, LEFT,  0x00A0, GPD0, 4),
    147  1.6.2.2  yamt 	GPIO_GRP(4, LEFT,  0x00C0, GPD1, 4),
    148  1.6.2.2  yamt 	GPIO_GRP(4, LEFT,  0x0180, GPF0, 8),
    149  1.6.2.2  yamt 	GPIO_GRP(4, LEFT,  0x01A0, GPF1, 8),
    150  1.6.2.2  yamt 	GPIO_GRP(4, LEFT,  0x01C0, GPF2, 8),
    151  1.6.2.2  yamt 	GPIO_GRP(4, LEFT,  0x01E0, GPF3, 8),
    152  1.6.2.2  yamt 	GPIO_GRP(4, LEFT,  0x0240, GPJ0, 8),
    153  1.6.2.2  yamt 	GPIO_GRP(4, LEFT,  0x0260, GPJ1, 5),
    154  1.6.2.2  yamt 	/* EXTINT skipped */
    155  1.6.2.2  yamt 
    156  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x0040, GPK0, 8),
    157  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x0060, GPK1, 8),
    158  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x0080, GPK2, 7),
    159  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x00A0, GPK3, 7),
    160  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x00C0, GPL0, 7),
    161  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x00E0, GPL1, 2),
    162  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x0100, GPL2, 8),
    163  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x0120, GPY0, 6),
    164  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x0140, GPY1, 4),
    165  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x0160, GPY2, 6),
    166  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x0180, GPY3, 8),
    167  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x01A0, GPY4, 8),
    168  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x01C0, GPY5, 8),
    169  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x01E0, GPY6, 8),
    170  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x0200, ETC0, 6),
    171  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x0220, ETC6, 7),
    172  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x0260, GPM0, 8),
    173  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x0280, GPM1, 7),
    174  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x02A0, GPM2, 5),
    175  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x02C0, GPM3, 8),
    176  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x02E0, GPM4, 8),
    177  1.6.2.2  yamt 	/* EXTINT skipped */
    178  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x0C00, GPX0, 8),
    179  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x0C20, GPX1, 8),
    180  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x0C40, GPX2, 8),
    181  1.6.2.2  yamt 	GPIO_GRP(4, RIGHT, 0x0C60, GPX3, 8),
    182  1.6.2.2  yamt 	/* EXTINT skipped */
    183  1.6.2.2  yamt 
    184  1.6.2.2  yamt 	GPIO_GRP(4, I2C0,  0x0000, GPZ,  8),
    185  1.6.2.2  yamt 	/* EXTINT skipped */
    186  1.6.2.2  yamt 
    187  1.6.2.2  yamt 	GPIO_GRP(4, C2C,   0x0000, GPV0, 8),
    188  1.6.2.2  yamt 	GPIO_GRP(4, C2C,   0x0020, GPV1, 8),
    189  1.6.2.2  yamt 	GPIO_GRP(4, C2C,   0x0040, ETC7, 2),
    190  1.6.2.2  yamt 	GPIO_GRP(4, C2C,   0x0060, GPV2, 8),
    191  1.6.2.2  yamt 	GPIO_GRP(4, C2C,   0x0080, GPV3, 8),
    192  1.6.2.2  yamt 	GPIO_GRP(4, C2C,   0x00A0, ETC8, 2),
    193  1.6.2.2  yamt 	GPIO_GRP(4, C2C,   0x00C0, GPV4, 2),
    194  1.6.2.2  yamt 	/* EXTINT skipped */
    195  1.6.2.2  yamt };
    196  1.6.2.2  yamt #endif
    197  1.6.2.2  yamt 
    198  1.6.2.2  yamt 
    199  1.6.2.2  yamt #ifdef EXYNOS5
    200  1.6.2.2  yamt static struct exynos_gpio_pin_group exynos5_pin_groups[] = {
    201  1.6.2.2  yamt };
    202  1.6.2.2  yamt #endif
    203  1.6.2.2  yamt 
    204  1.6.2.2  yamt 
    205  1.6.2.2  yamt struct exynos_gpio_softc {
    206  1.6.2.2  yamt 	device_t		sc_dev;
    207  1.6.2.2  yamt 	bus_space_tag_t		sc_bst;
    208  1.6.2.2  yamt 	bus_space_handle_t	sc_bsh;
    209  1.6.2.2  yamt };
    210  1.6.2.2  yamt 
    211  1.6.2.2  yamt 
    212  1.6.2.2  yamt /* force these structures in DATA segment */
    213  1.6.2.2  yamt static struct exynos_gpio_pin_group *exynos_pin_groups = NULL;
    214  1.6.2.2  yamt static int exynos_n_pin_groups = 0;
    215  1.6.2.2  yamt 
    216  1.6.2.2  yamt static struct exynos_gpio_softc exynos_gpio_sc = {};
    217  1.6.2.2  yamt 
    218  1.6.2.2  yamt 
    219  1.6.2.2  yamt CFATTACH_DECL_NEW(exynos_gpio, sizeof(struct exynos_gpio_softc),
    220  1.6.2.2  yamt 	exynos_gpio_match, exynos_gpio_attach, NULL, NULL);
    221  1.6.2.2  yamt 
    222  1.6.2.2  yamt 
    223  1.6.2.2  yamt static int
    224  1.6.2.2  yamt exynos_gpio_match(device_t parent, cfdata_t cf, void *aux)
    225  1.6.2.2  yamt {
    226  1.6.2.2  yamt 	struct exyo_attach_args * const exyoaa = aux;
    227  1.6.2.2  yamt 	struct exyo_locators *loc = &exyoaa->exyo_loc;
    228  1.6.2.2  yamt 
    229  1.6.2.2  yamt 	/* no locators expected */
    230  1.6.2.2  yamt 	KASSERT(loc->loc_offset == 0);
    231  1.6.2.2  yamt 	KASSERT(loc->loc_size   == 0);
    232  1.6.2.2  yamt 	KASSERT(loc->loc_port   == EXYOCF_PORT_DEFAULT);
    233  1.6.2.2  yamt 
    234  1.6.2.2  yamt 	/* there can only be one */
    235  1.6.2.2  yamt 	if (exynos_gpio_sc.sc_dev != NULL)
    236  1.6.2.2  yamt 		return 0;
    237  1.6.2.2  yamt 	return 1;
    238  1.6.2.2  yamt }
    239  1.6.2.2  yamt 
    240  1.6.2.2  yamt 
    241  1.6.2.2  yamt #if NGPIO > 0
    242  1.6.2.2  yamt static void
    243  1.6.2.2  yamt exynos_gpio_config_pins(device_t self)
    244  1.6.2.2  yamt {
    245  1.6.2.2  yamt 	struct exynos_gpio_softc * const sc = &exynos_gpio_sc;
    246  1.6.2.2  yamt 	struct exynos_gpio_pin_group *grp;
    247  1.6.2.2  yamt 	struct gpiobus_attach_args gba;
    248  1.6.2.2  yamt 	gpio_pin_t *pin, *pins;
    249  1.6.2.2  yamt 	size_t pin_count = 0;
    250  1.6.2.2  yamt 	int i, bit, mask, pincaps, data;
    251  1.6.2.2  yamt 
    252  1.6.2.2  yamt 	/* find out how many pins we can offer */
    253  1.6.2.2  yamt 	pin_count = 0;
    254  1.6.2.2  yamt 	for (i = 0; i < exynos_n_pin_groups; i++) {
    255  1.6.2.2  yamt 		grp = &exynos_pin_groups[i];
    256  1.6.2.2  yamt 		mask = grp->grp_pin_mask & ~grp->grp_pin_inuse_mask;
    257  1.6.2.2  yamt 		pin_count += popcount32(mask);
    258  1.6.2.2  yamt 	}
    259  1.6.2.2  yamt 
    260  1.6.2.2  yamt 	/* if no pins available, don't proceed */
    261  1.6.2.2  yamt 	if (pin_count == 0)
    262  1.6.2.2  yamt 		return;
    263  1.6.2.2  yamt 
    264  1.6.2.2  yamt 	/* allocate pin data */
    265  1.6.2.2  yamt 	pins = kmem_zalloc(sizeof(gpio_pin_t) * pin_count, KM_SLEEP);
    266  1.6.2.2  yamt 	KASSERT(pins);
    267  1.6.2.2  yamt 
    268  1.6.2.2  yamt 	pincaps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT |
    269  1.6.2.2  yamt 		GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN;
    270  1.6.2.2  yamt 
    271  1.6.2.2  yamt 	/* add all pins */
    272  1.6.2.2  yamt 	pin = pins;
    273  1.6.2.2  yamt 	for (i = 0; i < exynos_n_pin_groups; i++) {
    274  1.6.2.2  yamt 		grp = &exynos_pin_groups[i];
    275  1.6.2.2  yamt 		mask = grp->grp_pin_mask & ~grp->grp_pin_inuse_mask;
    276  1.6.2.2  yamt 		if (mask == 0)
    277  1.6.2.2  yamt 			continue;
    278  1.6.2.2  yamt 		gba.gba_gc = &grp->grp_gc_tag;
    279  1.6.2.2  yamt 		gba.gba_pins = pin;
    280  1.6.2.2  yamt 		data = bus_space_read_1(sc->sc_bst, grp->grp_bsh,
    281  1.6.2.2  yamt 				EXYNOS_GPIO_DAT);
    282  1.6.2.2  yamt 		for (bit = 0; mask != 0; mask >>= 1, data >>= 1, bit++) {
    283  1.6.2.2  yamt 			if (mask & 1) {
    284  1.6.2.2  yamt 				pin->pin_num = bit + (i << 3);
    285  1.6.2.2  yamt 				pin->pin_caps = pincaps;
    286  1.6.2.2  yamt 				pin->pin_flags = pincaps;
    287  1.6.2.2  yamt 				pin->pin_state = (data & 1) != 0;
    288  1.6.2.2  yamt 				pin++;
    289  1.6.2.2  yamt 			}
    290  1.6.2.2  yamt 		}
    291  1.6.2.2  yamt 		gba.gba_npins = pin - gba.gba_pins;
    292  1.6.2.2  yamt 		config_found_ia(self, "gpiobus", &gba, gpiobus_print);
    293  1.6.2.2  yamt 	}
    294  1.6.2.2  yamt }
    295  1.6.2.2  yamt #endif
    296  1.6.2.2  yamt 
    297  1.6.2.2  yamt 
    298  1.6.2.2  yamt static void
    299  1.6.2.2  yamt exynos_gpio_attach(device_t parent, device_t self, void *aux)
    300  1.6.2.2  yamt {
    301  1.6.2.2  yamt 	struct exynos_gpio_softc * const sc = &exynos_gpio_sc;
    302  1.6.2.2  yamt 	struct exyo_attach_args * const exyoaa = aux;
    303  1.6.2.2  yamt 	struct exynos_gpio_pin_group *grp;
    304  1.6.2.2  yamt 	prop_dictionary_t dict = device_properties(self);
    305  1.6.2.2  yamt 	uint32_t nc;
    306  1.6.2.2  yamt 	char scrap[16];
    307  1.6.2.2  yamt 	int i;
    308  1.6.2.2  yamt 
    309  1.6.2.2  yamt 	KASSERT(exynos_pin_groups);
    310  1.6.2.2  yamt 	KASSERT(exynos_n_pin_groups);
    311  1.6.2.2  yamt 
    312  1.6.2.2  yamt 	/* construct softc */
    313  1.6.2.2  yamt 	sc->sc_dev = self;
    314  1.6.2.2  yamt 
    315  1.6.2.2  yamt 	/* we use the core bushandle here */
    316  1.6.2.2  yamt 	sc->sc_bst = exyoaa->exyo_core_bst;
    317  1.6.2.2  yamt 	sc->sc_bsh = exyoaa->exyo_core_bsh;
    318  1.6.2.2  yamt 
    319  1.6.2.2  yamt 	aprint_naive("\n");
    320  1.6.2.2  yamt 	aprint_normal("\n");
    321  1.6.2.2  yamt 
    322  1.6.2.2  yamt 	/* go trough all pin groups */
    323  1.6.2.2  yamt 	for (i = 0; i < exynos_n_pin_groups; i++) {
    324  1.6.2.2  yamt 		grp = &exynos_pin_groups[i];
    325  1.6.2.2  yamt 		snprintf(scrap, sizeof(scrap), "nc-%s", grp->grp_name);
    326  1.6.2.2  yamt 		if (prop_dictionary_get_uint32(dict, scrap, &nc)) {
    327  1.6.2.2  yamt 			KASSERT((~grp->grp_pin_mask & nc) == 0);
    328  1.6.2.2  yamt 			KASSERT((grp->grp_pin_inuse_mask & ~nc) == 0);
    329  1.6.2.2  yamt 			grp->grp_pin_mask &= ~nc;
    330  1.6.2.2  yamt 		}
    331  1.6.2.2  yamt 	}
    332  1.6.2.2  yamt 
    333  1.6.2.2  yamt #if NGPIO > 0
    334  1.6.2.2  yamt 	config_defer(self, exynos_gpio_config_pins);
    335  1.6.2.2  yamt #endif
    336  1.6.2.2  yamt }
    337  1.6.2.2  yamt 
    338  1.6.2.2  yamt 
    339  1.6.2.2  yamt /* pin access functions */
    340  1.6.2.2  yamt static u_int
    341  1.6.2.2  yamt exynos_gpio_get_pin_func(const struct exynos_gpio_pin_cfg *cfg, int pin)
    342  1.6.2.2  yamt {
    343  1.6.2.2  yamt 	const u_int shift = (pin & 7) << 4;
    344  1.6.2.2  yamt 
    345  1.6.2.2  yamt 	return (cfg->cfg >> shift) & 0x0f;
    346  1.6.2.2  yamt }
    347  1.6.2.2  yamt 
    348  1.6.2.2  yamt 
    349  1.6.2.2  yamt static void
    350  1.6.2.2  yamt exynos_gpio_set_pin_func(struct exynos_gpio_pin_cfg *cfg,
    351  1.6.2.2  yamt 	int pin, int func)
    352  1.6.2.2  yamt {
    353  1.6.2.2  yamt 	const u_int shift = (pin & 7) << 4;
    354  1.6.2.2  yamt 
    355  1.6.2.2  yamt 	cfg->cfg &= ~(0x0f << shift);
    356  1.6.2.2  yamt 	cfg->cfg |= func << shift;
    357  1.6.2.2  yamt }
    358  1.6.2.2  yamt 
    359  1.6.2.2  yamt 
    360  1.6.2.2  yamt static void
    361  1.6.2.2  yamt exynos_gpio_set_pin_pull(struct exynos_gpio_pin_cfg *cfg, int pin, int pull)
    362  1.6.2.2  yamt {
    363  1.6.2.2  yamt 	const u_int shift = (pin & 7) << 1;
    364  1.6.2.2  yamt 
    365  1.6.2.2  yamt 	cfg->pud &= ~(0x3 << shift);
    366  1.6.2.2  yamt 	cfg->pud |= pull << shift;
    367  1.6.2.2  yamt }
    368  1.6.2.2  yamt 
    369  1.6.2.2  yamt 
    370  1.6.2.2  yamt static int
    371  1.6.2.2  yamt exynos_gpio_pin_read(void *cookie, int pin)
    372  1.6.2.2  yamt {
    373  1.6.2.2  yamt 	struct exynos_gpio_pin_group * const grp = cookie;
    374  1.6.2.2  yamt 
    375  1.6.2.2  yamt 	KASSERT(pin < grp->grp_bits);
    376  1.6.2.2  yamt 	return (bus_space_read_1(exynos_gpio_sc.sc_bst, grp->grp_bsh,
    377  1.6.2.2  yamt 		EXYNOS_GPIO_DAT) >> pin) & 1;
    378  1.6.2.2  yamt }
    379  1.6.2.2  yamt 
    380  1.6.2.2  yamt 
    381  1.6.2.2  yamt static void
    382  1.6.2.2  yamt exynos_gpio_pin_write(void *cookie, int pin, int value)
    383  1.6.2.2  yamt {
    384  1.6.2.2  yamt 	struct exynos_gpio_pin_group * const grp = cookie;
    385  1.6.2.2  yamt 	int val;
    386  1.6.2.2  yamt 
    387  1.6.2.2  yamt 	KASSERT(pin < grp->grp_bits);
    388  1.6.2.2  yamt 	val = bus_space_read_1(exynos_gpio_sc.sc_bst, grp->grp_bsh,
    389  1.6.2.2  yamt 		EXYNOS_GPIO_DAT);
    390  1.6.2.2  yamt 	val &= ~__BIT(pin);
    391  1.6.2.2  yamt 	if (value)
    392  1.6.2.2  yamt 		val |= __BIT(pin);
    393  1.6.2.2  yamt 	bus_space_write_1(exynos_gpio_sc.sc_bst, grp->grp_bsh,
    394  1.6.2.2  yamt 		EXYNOS_GPIO_DAT, val);
    395  1.6.2.2  yamt }
    396  1.6.2.2  yamt 
    397  1.6.2.2  yamt 
    398  1.6.2.2  yamt static void
    399  1.6.2.2  yamt exynos_gpio_update_cfg_regs(struct exynos_gpio_pin_group *grp,
    400  1.6.2.2  yamt 	const struct exynos_gpio_pin_cfg *ncfg)
    401  1.6.2.2  yamt {
    402  1.6.2.2  yamt 	bus_space_tag_t bst = &exynos_bs_tag;
    403  1.6.2.2  yamt 
    404  1.6.2.2  yamt 	if (grp->grp_cfg.cfg != ncfg->cfg) {
    405  1.6.2.2  yamt 		bus_space_write_4(bst, grp->grp_bsh,
    406  1.6.2.2  yamt 			EXYNOS_GPIO_CON, ncfg->cfg);
    407  1.6.2.2  yamt 		grp->grp_cfg.cfg = ncfg->cfg;
    408  1.6.2.2  yamt 	}
    409  1.6.2.2  yamt 	if (grp->grp_cfg.pud != ncfg->pud) {
    410  1.6.2.2  yamt 		bus_space_write_4(bst, grp->grp_bsh,
    411  1.6.2.2  yamt 			EXYNOS_GPIO_PUD, ncfg->pud);
    412  1.6.2.2  yamt 		grp->grp_cfg.pud = ncfg->pud;
    413  1.6.2.2  yamt 	}
    414  1.6.2.2  yamt 
    415  1.6.2.2  yamt 	/* the following attributes are not yet setable */
    416  1.6.2.2  yamt #if 0
    417  1.6.2.2  yamt 	if (grp->grp_cfg.drv != ncfg->drv) {
    418  1.6.2.2  yamt 		bus_space_write_4(bst, grp->grp_bsh,
    419  1.6.2.2  yamt 			EXYNOS_GPIO_DRV, ncfg->drv);
    420  1.6.2.2  yamt 		grp->grp_cfg.drv = ncfg->drv;
    421  1.6.2.2  yamt 	}
    422  1.6.2.2  yamt 	if (grp->grp_cfg.conpwd != ncfg->conpwd) {
    423  1.6.2.2  yamt 		bus_space_write_4(bst, grp->grp_bsh,
    424  1.6.2.2  yamt 			EXYNOS_GPIO_CONPWD, ncfg->conpwd);
    425  1.6.2.2  yamt 		grp->grp_cfg.conpwd = ncfg->conpwd;
    426  1.6.2.2  yamt 	}
    427  1.6.2.2  yamt 	if (grp->grp_cfg.pudpwd != ncfg->pudpwd) {
    428  1.6.2.2  yamt 		bus_space_write_4(bst, grp->grp_bsh,
    429  1.6.2.2  yamt 			EXYNOS_GPIO_PUDPWD, ncfg->pudpwd);
    430  1.6.2.2  yamt 		grp->grp_cfg.pudpwd = ncfg->pudpwd;
    431  1.6.2.2  yamt 	}
    432  1.6.2.2  yamt #endif
    433  1.6.2.2  yamt }
    434  1.6.2.2  yamt 
    435  1.6.2.2  yamt 
    436  1.6.2.2  yamt static void
    437  1.6.2.2  yamt exynos_gpio_pin_ctl(void *cookie, int pin, int flags)
    438  1.6.2.2  yamt {
    439  1.6.2.2  yamt 	struct exynos_gpio_pin_group * const grp = cookie;
    440  1.6.2.2  yamt 	struct exynos_gpio_pin_cfg ncfg = grp->grp_cfg;
    441  1.6.2.2  yamt 	int pull;
    442  1.6.2.2  yamt 
    443  1.6.2.2  yamt 	/* honour pullup requests */
    444  1.6.2.2  yamt 	pull = EXYNOS_GPIO_PIN_FLOAT;
    445  1.6.2.2  yamt 	if (flags & GPIO_PIN_PULLUP)
    446  1.6.2.2  yamt 		pull = EXYNOS_GPIO_PIN_PULL_UP;
    447  1.6.2.2  yamt 	if (flags & GPIO_PIN_PULLDOWN)
    448  1.6.2.2  yamt 		pull = EXYNOS_GPIO_PIN_PULL_DOWN;
    449  1.6.2.2  yamt 	exynos_gpio_set_pin_pull(&ncfg, pin, pull);
    450  1.6.2.2  yamt 
    451  1.6.2.2  yamt 	/* honour i/o */
    452  1.6.2.2  yamt 	if (flags & GPIO_PIN_INPUT)
    453  1.6.2.2  yamt 		exynos_gpio_set_pin_func(&ncfg, pin, EXYNOS_GPIO_FUNC_INPUT);
    454  1.6.2.2  yamt 	if (flags & GPIO_PIN_OUTPUT)
    455  1.6.2.2  yamt 		exynos_gpio_set_pin_func(&ncfg, pin, EXYNOS_GPIO_FUNC_OUTPUT);
    456  1.6.2.2  yamt 
    457  1.6.2.2  yamt 	/* update any config registers that changed */
    458  1.6.2.2  yamt 	exynos_gpio_update_cfg_regs(grp, &ncfg);
    459  1.6.2.2  yamt }
    460  1.6.2.2  yamt 
    461  1.6.2.2  yamt 
    462  1.6.2.2  yamt bool
    463  1.6.2.2  yamt exynos_gpio_pinset_available(const struct exynos_gpio_pinset *req)
    464  1.6.2.2  yamt {
    465  1.6.2.2  yamt 	struct exynos_gpio_pin_group *grp;
    466  1.6.2.2  yamt 	int i, n, inuse;
    467  1.6.2.2  yamt 
    468  1.6.2.2  yamt 	KASSERT(req);
    469  1.6.2.2  yamt 
    470  1.6.2.2  yamt 	/* we need a pinset group */
    471  1.6.2.2  yamt 	if (strlen(req->pinset_group) == 0)
    472  1.6.2.2  yamt 		return false;
    473  1.6.2.2  yamt 
    474  1.6.2.2  yamt 	/* determine which group is requested */
    475  1.6.2.2  yamt 	grp = NULL;
    476  1.6.2.2  yamt 	for (i = 0; i < exynos_n_pin_groups; i++) {
    477  1.6.2.2  yamt 		grp = &exynos_pin_groups[i];
    478  1.6.2.2  yamt 		if (strcmp(req->pinset_group, grp->grp_name) == 0)
    479  1.6.2.2  yamt 			break;
    480  1.6.2.2  yamt 	}
    481  1.6.2.2  yamt 	/* found? */
    482  1.6.2.2  yamt 	if (i == exynos_n_pin_groups)
    483  1.6.2.2  yamt 		return false;
    484  1.6.2.2  yamt 	KASSERT(grp);
    485  1.6.2.2  yamt 
    486  1.6.2.2  yamt 	/* fail unconnected pins */
    487  1.6.2.2  yamt 	if (req->pinset_mask & ~grp->grp_pin_mask)
    488  1.6.2.2  yamt 		return false;
    489  1.6.2.2  yamt 
    490  1.6.2.2  yamt 	/* if none in use, they are available */
    491  1.6.2.2  yamt 	if (req->pinset_mask & ~grp->grp_pin_inuse_mask)
    492  1.6.2.2  yamt 		return true;
    493  1.6.2.2  yamt 
    494  1.6.2.2  yamt 	/* OK, so some are in use; now see if the request is compatible */
    495  1.6.2.2  yamt 	inuse = req->pinset_mask & grp->grp_pin_inuse_mask;
    496  1.6.2.2  yamt 	for (i = 0; inuse; i++, inuse >>= 1) {
    497  1.6.2.2  yamt 		/* try to be smart by skipping zero's */
    498  1.6.2.2  yamt 		n = ffs(inuse) -1;
    499  1.6.2.2  yamt 		i += n;
    500  1.6.2.2  yamt 		inuse >>= n;
    501  1.6.2.2  yamt 		/* this pin is in use, check its usage */
    502  1.6.2.2  yamt 		if (exynos_gpio_get_pin_func(&grp->grp_cfg, i) != req->pinset_func)
    503  1.6.2.2  yamt 			return false;
    504  1.6.2.2  yamt 	}
    505  1.6.2.2  yamt 
    506  1.6.2.2  yamt 	/* seems to be OK */
    507  1.6.2.2  yamt 	return true;
    508  1.6.2.2  yamt }
    509  1.6.2.2  yamt 
    510  1.6.2.2  yamt 
    511  1.6.2.2  yamt void
    512  1.6.2.2  yamt exynos_gpio_pinset_acquire(const struct exynos_gpio_pinset *req)
    513  1.6.2.2  yamt {
    514  1.6.2.2  yamt 	struct exynos_gpio_pin_group *grp;
    515  1.6.2.2  yamt 	struct exynos_gpio_pin_cfg ncfg;
    516  1.6.2.2  yamt 	int i, n, todo;
    517  1.6.2.2  yamt 
    518  1.6.2.2  yamt 	KASSERT(req);
    519  1.6.2.2  yamt 	KASSERT(exynos_gpio_pinset_available(req));
    520  1.6.2.2  yamt 
    521  1.6.2.2  yamt 	/* determine which group is requested */
    522  1.6.2.2  yamt 	grp = NULL;
    523  1.6.2.2  yamt 	for (i = 0; i < exynos_n_pin_groups; i++) {
    524  1.6.2.2  yamt 		grp = &exynos_pin_groups[i];
    525  1.6.2.2  yamt 		if (strcmp(req->pinset_group, grp->grp_name) == 0)
    526  1.6.2.2  yamt 			break;
    527  1.6.2.2  yamt 	}
    528  1.6.2.2  yamt 	KASSERT(grp);
    529  1.6.2.2  yamt 
    530  1.6.2.2  yamt 	/* check if all the pins have the right function */
    531  1.6.2.2  yamt 	if ((req->pinset_mask & ~grp->grp_pin_inuse_mask) == 0)
    532  1.6.2.2  yamt 		return;
    533  1.6.2.2  yamt 
    534  1.6.2.2  yamt 	/* copy current config for update routine */
    535  1.6.2.2  yamt 	ncfg = grp->grp_cfg;
    536  1.6.2.2  yamt 
    537  1.6.2.2  yamt 	/* update the function of each pin that is not in use */
    538  1.6.2.2  yamt 	todo = req->pinset_mask & grp->grp_pin_inuse_mask;
    539  1.6.2.2  yamt 	for (i = 0; todo; i++, todo >>= 1) {
    540  1.6.2.2  yamt 		/* try to be smart by skipping zero's */
    541  1.6.2.2  yamt 		n = ffs(todo) -1;
    542  1.6.2.2  yamt 		i += n;
    543  1.6.2.2  yamt 		todo >>= n;
    544  1.6.2.2  yamt 		/* change the function of this pin */
    545  1.6.2.2  yamt 		exynos_gpio_set_pin_func(&ncfg, i, req->pinset_func);
    546  1.6.2.2  yamt 	}
    547  1.6.2.2  yamt 
    548  1.6.2.2  yamt 	/* update config registers */
    549  1.6.2.2  yamt 	exynos_gpio_update_cfg_regs(grp, &ncfg);
    550  1.6.2.2  yamt 
    551  1.6.2.2  yamt 	/* mark pins in use */
    552  1.6.2.2  yamt 	grp->grp_pin_inuse_mask |= req->pinset_mask;
    553  1.6.2.2  yamt }
    554  1.6.2.2  yamt 
    555  1.6.2.2  yamt 
    556  1.6.2.2  yamt /* XXXRPZ This release doesn't grock multiple usages! */
    557  1.6.2.2  yamt void
    558  1.6.2.2  yamt exynos_gpio_pinset_release(const struct exynos_gpio_pinset *req)
    559  1.6.2.2  yamt {
    560  1.6.2.2  yamt 	struct exynos_gpio_pin_group *grp;
    561  1.6.2.2  yamt 	int i;
    562  1.6.2.2  yamt 
    563  1.6.2.2  yamt 	KASSERT(!exynos_gpio_pinset_available(req));
    564  1.6.2.2  yamt 
    565  1.6.2.2  yamt 	/* determine which group is requested */
    566  1.6.2.2  yamt 	grp = NULL;
    567  1.6.2.2  yamt 	for (i = 0; i < exynos_n_pin_groups; i++) {
    568  1.6.2.2  yamt 		grp = &exynos_pin_groups[i];
    569  1.6.2.2  yamt 		if (strcmp(req->pinset_group, grp->grp_name) == 0)
    570  1.6.2.2  yamt 			break;
    571  1.6.2.2  yamt 	}
    572  1.6.2.2  yamt 	KASSERT(grp);
    573  1.6.2.2  yamt 
    574  1.6.2.2  yamt 	/* bluntly mark as not being in use */
    575  1.6.2.2  yamt 	grp->grp_pin_inuse_mask &= ~req->pinset_mask;
    576  1.6.2.2  yamt }
    577  1.6.2.2  yamt 
    578  1.6.2.2  yamt 
    579  1.6.2.2  yamt /*
    580  1.6.2.2  yamt  * name convention :
    581  1.6.2.2  yamt  * pin   = <func><groupname><pinnr>[<pud>]
    582  1.6.2.2  yamt  * func  = '<' | '>'
    583  1.6.2.2  yamt  * pinnr = '['['0'-'7']']'
    584  1.6.2.2  yamt  * pud   =  'F' | 'U' | 'D'
    585  1.6.2.2  yamt  *
    586  1.6.2.2  yamt  * example "<GPC1[0]", ">GPB[0]"
    587  1.6.2.2  yamt  */
    588  1.6.2.2  yamt 
    589  1.6.2.2  yamt bool
    590  1.6.2.2  yamt exynos_gpio_pin_reserve(const char *name, struct exynos_gpio_pindata *pd)
    591  1.6.2.2  yamt {
    592  1.6.2.2  yamt 	struct exynos_gpio_softc * const sc = &exynos_gpio_sc;
    593  1.6.2.2  yamt 	struct exynos_gpio_pin_group *grp;
    594  1.6.2.2  yamt 	struct exynos_gpio_pin_cfg ncfg;
    595  1.6.2.2  yamt 	prop_dictionary_t dict = device_properties(sc->sc_dev);
    596  1.6.2.2  yamt 	const char *pin_data;
    597  1.6.2.2  yamt 	char grp_name[15], *pos;
    598  1.6.2.2  yamt 	int func, pud, pinnr;
    599  1.6.2.2  yamt 	int pi, i;
    600  1.6.2.2  yamt 
    601  1.6.2.2  yamt 	/* do we have a named pin description? */
    602  1.6.2.2  yamt 	if (!prop_dictionary_get_cstring_nocopy(dict, name, &pin_data))
    603  1.6.2.2  yamt 		return false;
    604  1.6.2.2  yamt 
    605  1.6.2.2  yamt 	KASSERT(strlen(pin_data) < 10);
    606  1.6.2.2  yamt 	if (!(pin_data[0] == '>' || pin_data[0] == '<')) {
    607  1.6.2.2  yamt 		printf("%s: malformed pin data in '%s', missing direction\n",
    608  1.6.2.2  yamt 			__func__, pin_data);
    609  1.6.2.2  yamt 		return false;
    610  1.6.2.2  yamt 	}
    611  1.6.2.2  yamt 
    612  1.6.2.2  yamt 	func = (pin_data[0] == '<') ?
    613  1.6.2.2  yamt 		EXYNOS_GPIO_FUNC_INPUT : EXYNOS_GPIO_FUNC_OUTPUT;
    614  1.6.2.2  yamt 
    615  1.6.2.2  yamt 	/* find groupname */
    616  1.6.2.2  yamt 	pi = 1; pos = grp_name;
    617  1.6.2.2  yamt 	while (pin_data[pi] && pin_data[pi] != '[') {
    618  1.6.2.2  yamt 		*pos++ = pin_data[pi++];
    619  1.6.2.2  yamt 	}
    620  1.6.2.2  yamt 	if (pin_data[pi] != '[') {
    621  1.6.2.2  yamt 		printf("%s: malformed pin data in '%s', missing '['\n",
    622  1.6.2.2  yamt 			__func__, pin_data);
    623  1.6.2.2  yamt 		return false;
    624  1.6.2.2  yamt 	}
    625  1.6.2.2  yamt 	*pos++ = (char) 0;
    626  1.6.2.2  yamt 
    627  1.6.2.2  yamt 	/* skip '[' */
    628  1.6.2.2  yamt 	pi++;
    629  1.6.2.2  yamt 	if (!(pin_data[pi] >= '0' && pin_data[pi] <= '7')) {
    630  1.6.2.2  yamt 		printf("%s: malformed pin data in '%s', bad pin number\n",
    631  1.6.2.2  yamt 			__func__, pin_data);
    632  1.6.2.2  yamt 		return false;
    633  1.6.2.2  yamt 	}
    634  1.6.2.2  yamt 	pinnr = pin_data[pi] - '0';
    635  1.6.2.2  yamt 
    636  1.6.2.2  yamt 	/* skip digit */
    637  1.6.2.2  yamt 	pi++;
    638  1.6.2.2  yamt 	if ((pin_data[pi] != ']')) {
    639  1.6.2.2  yamt 		printf("%s: malformed pin data in '%s', missing end ']'\n",
    640  1.6.2.2  yamt 			__func__, pin_data);
    641  1.6.2.2  yamt 		return false;
    642  1.6.2.2  yamt 	}
    643  1.6.2.2  yamt 
    644  1.6.2.2  yamt 	/* skip ']' */
    645  1.6.2.2  yamt 	pi++;
    646  1.6.2.2  yamt 	pud = EXYNOS_GPIO_PIN_FLOAT;
    647  1.6.2.2  yamt 	switch (tolower(pin_data[pi])) {
    648  1.6.2.2  yamt 		case (char) 0:
    649  1.6.2.2  yamt 			break;
    650  1.6.2.2  yamt 		case 'f':
    651  1.6.2.2  yamt 			pud = EXYNOS_GPIO_PIN_FLOAT;
    652  1.6.2.2  yamt 			break;
    653  1.6.2.2  yamt 		case 'u':
    654  1.6.2.2  yamt 			pud = EXYNOS_GPIO_PIN_PULL_UP;
    655  1.6.2.2  yamt 			break;
    656  1.6.2.2  yamt 		case 'd':
    657  1.6.2.2  yamt 			pud = EXYNOS_GPIO_PIN_PULL_DOWN;
    658  1.6.2.2  yamt 			break;
    659  1.6.2.2  yamt 		default:
    660  1.6.2.2  yamt 			printf("%s: malformed pin data in '%s', expecting "
    661  1.6.2.2  yamt 				"optional pull up/down or float argument\n",
    662  1.6.2.2  yamt 				__func__, pin_data);
    663  1.6.2.2  yamt 		return false;
    664  1.6.2.2  yamt 	}
    665  1.6.2.2  yamt 
    666  1.6.2.2  yamt 	/* determine which group is requested */
    667  1.6.2.2  yamt 	grp = NULL;
    668  1.6.2.2  yamt 	for (i = 0; i < exynos_n_pin_groups; i++) {
    669  1.6.2.2  yamt 		grp = &exynos_pin_groups[i];
    670  1.6.2.2  yamt 		if (strcmp(grp_name, grp->grp_name) == 0)
    671  1.6.2.2  yamt 			break;
    672  1.6.2.2  yamt 	}
    673  1.6.2.2  yamt 
    674  1.6.2.2  yamt 	/* found? */
    675  1.6.2.2  yamt 	if (i >= exynos_n_pin_groups) {
    676  1.6.2.2  yamt 		printf("%s: malformed pin data in '%s', "
    677  1.6.2.2  yamt 			"no such pin group name\n",
    678  1.6.2.2  yamt 			__func__, grp_name);
    679  1.6.2.2  yamt 		return false;
    680  1.6.2.2  yamt 	}
    681  1.6.2.2  yamt 	KASSERT(grp);
    682  1.6.2.2  yamt 
    683  1.6.2.2  yamt 	KASSERT(pinnr < grp->grp_bits);
    684  1.6.2.2  yamt 	KASSERT(grp->grp_pin_mask & __BIT(pinnr));
    685  1.6.2.2  yamt 	KASSERT((grp->grp_pin_inuse_mask & __BIT(pinnr)) == 0);
    686  1.6.2.2  yamt 
    687  1.6.2.2  yamt 	/* update our pin configuration */
    688  1.6.2.2  yamt 	ncfg = grp->grp_cfg;
    689  1.6.2.2  yamt 	exynos_gpio_set_pin_func(&ncfg, pinnr, func);
    690  1.6.2.2  yamt 	exynos_gpio_set_pin_pull(&ncfg, pinnr, pud);
    691  1.6.2.2  yamt 	exynos_gpio_update_cfg_regs(grp, &ncfg);
    692  1.6.2.2  yamt 
    693  1.6.2.2  yamt 	grp->grp_pin_inuse_mask &= ~__BIT(pinnr);
    694  1.6.2.2  yamt 
    695  1.6.2.2  yamt 	pd->pd_gc = &grp->grp_gc_tag;
    696  1.6.2.2  yamt 	pd->pd_pin = pinnr;
    697  1.6.2.2  yamt 
    698  1.6.2.2  yamt 	return true;
    699  1.6.2.2  yamt }
    700  1.6.2.2  yamt 
    701  1.6.2.2  yamt 
    702  1.6.2.2  yamt /* bootstrapping */
    703  1.6.2.2  yamt void
    704  1.6.2.2  yamt exynos_gpio_bootstrap(void)
    705  1.6.2.2  yamt {
    706  1.6.2.2  yamt 	bus_space_tag_t bst = &exynos_bs_tag;
    707  1.6.2.2  yamt 	struct exynos_gpio_pin_group *grp;
    708  1.6.2.2  yamt 	struct gpio_chipset_tag *gc_tag;
    709  1.6.2.2  yamt 	int i, j, func, mask;
    710  1.6.2.2  yamt 
    711  1.6.2.2  yamt 	/* determine what we're running on */
    712  1.6.2.2  yamt #ifdef EXYNOS4
    713  1.6.2.2  yamt 	if (IS_EXYNOS4_P()) {
    714  1.6.2.2  yamt 		exynos_pin_groups = exynos4_pin_groups;
    715  1.6.2.2  yamt 		exynos_n_pin_groups = __arraycount(exynos4_pin_groups);
    716  1.6.2.2  yamt 	}
    717  1.6.2.2  yamt #endif
    718  1.6.2.2  yamt #ifdef EXYNOS5
    719  1.6.2.2  yamt 	if (IS_EXYNOS5_P()) {
    720  1.6.2.2  yamt 		exynos_pin_groups = exynos5_pin_groups;
    721  1.6.2.2  yamt 		exynos_n_pin_groups = __arraycount(exynos5_pin_groups);
    722  1.6.2.2  yamt 	}
    723  1.6.2.2  yamt #endif
    724  1.6.2.2  yamt 
    725  1.6.2.2  yamt #ifdef VERBOSE_INIT_ARM
    726  1.6.2.2  yamt 	printf("gpio");
    727  1.6.2.2  yamt #endif
    728  1.6.2.2  yamt 	if (exynos_n_pin_groups == 0) {
    729  1.6.2.2  yamt #ifdef VERBOSE_INIT_ARM
    730  1.6.2.2  yamt 		printf("  (disabled)\n");
    731  1.6.2.2  yamt #endif
    732  1.6.2.2  yamt 		return;
    733  1.6.2.2  yamt 	}
    734  1.6.2.2  yamt 
    735  1.6.2.2  yamt #ifdef VERBOSE_INIT_ARM
    736  1.6.2.2  yamt 	printf(" free");
    737  1.6.2.2  yamt #endif
    738  1.6.2.2  yamt 	/* init groups */
    739  1.6.2.2  yamt 	for (i = 0; i < exynos_n_pin_groups; i++) {
    740  1.6.2.2  yamt 		grp = &exynos_pin_groups[i];
    741  1.6.2.2  yamt 		gc_tag = &grp->grp_gc_tag;
    742  1.6.2.2  yamt 
    743  1.6.2.2  yamt 		bus_space_subregion(&exynos_bs_tag, exynos_core_bsh,
    744  1.6.2.2  yamt 			grp->grp_core_offset, EXYNOS_GPIO_GRP_SIZE,
    745  1.6.2.2  yamt 			&grp->grp_bsh);
    746  1.6.2.2  yamt 		grp->grp_pin_mask = __BIT(grp->grp_bits) - 1;
    747  1.6.2.2  yamt 		grp->grp_pin_inuse_mask = 0;
    748  1.6.2.2  yamt 
    749  1.6.2.2  yamt 		gc_tag->gp_cookie = grp;
    750  1.6.2.2  yamt 		gc_tag->gp_pin_read  = exynos_gpio_pin_read;
    751  1.6.2.2  yamt 		gc_tag->gp_pin_write = exynos_gpio_pin_write;
    752  1.6.2.2  yamt 		gc_tag->gp_pin_ctl   = exynos_gpio_pin_ctl;
    753  1.6.2.2  yamt 
    754  1.6.2.2  yamt 		/* read in our initial settings */
    755  1.6.2.2  yamt 		grp->grp_cfg.cfg = bus_space_read_4(bst, grp->grp_bsh,
    756  1.6.2.2  yamt 			EXYNOS_GPIO_CON);
    757  1.6.2.2  yamt 		grp->grp_cfg.pud = bus_space_read_4(bst, grp->grp_bsh,
    758  1.6.2.2  yamt 			EXYNOS_GPIO_PUD);
    759  1.6.2.2  yamt 		grp->grp_cfg.drv = bus_space_read_4(bst, grp->grp_bsh,
    760  1.6.2.2  yamt 			EXYNOS_GPIO_DRV);
    761  1.6.2.2  yamt 		grp->grp_cfg.conpwd = bus_space_read_4(bst, grp->grp_bsh,
    762  1.6.2.2  yamt 			EXYNOS_GPIO_CONPWD);
    763  1.6.2.2  yamt 		grp->grp_cfg.pudpwd = bus_space_read_4(bst, grp->grp_bsh,
    764  1.6.2.2  yamt 			EXYNOS_GPIO_PUDPWD);
    765  1.6.2.2  yamt 
    766  1.6.2.2  yamt 		/* count number of busy pins */
    767  1.6.2.2  yamt 		for (j = 0, mask = 1;
    768  1.6.2.2  yamt 		     (mask & grp->grp_pin_mask) != 0;
    769  1.6.2.2  yamt 		     j++, mask <<= 1) {
    770  1.6.2.2  yamt 			func = exynos_gpio_get_pin_func(&grp->grp_cfg, j);
    771  1.6.2.2  yamt 			if (func > EXYNOS_GPIO_FUNC_INPUT) {
    772  1.6.2.2  yamt 				grp->grp_pin_inuse_mask |= mask;
    773  1.6.2.2  yamt 			}
    774  1.6.2.2  yamt 		}
    775  1.6.2.2  yamt #ifdef VERBOSE_INIT_ARM
    776  1.6.2.2  yamt 		printf(" P%s = %d", grp->grp_name,
    777  1.6.2.2  yamt 			popcount32(grp->grp_pin_mask & ~grp->grp_pin_inuse_mask));
    778  1.6.2.2  yamt #endif
    779  1.6.2.2  yamt 	}
    780  1.6.2.2  yamt #ifdef VERBOSE_INIT_ARM
    781  1.6.2.2  yamt 	printf("\n");
    782  1.6.2.2  yamt #if 0
    783  1.6.2.2  yamt 	/* enable this for default NC pins list generation */
    784  1.6.2.2  yamt 	for (i = 0; i < exynos_n_pin_groups; i++) {
    785  1.6.2.2  yamt 		grp = &exynos_pin_groups[i];
    786  1.6.2.2  yamt 		printf("prop_dictionary_set_uint32(dict, \"nc-%s\", 0x%02x - 0x00);\n",
    787  1.6.2.2  yamt 			grp->grp_name, grp->grp_pin_mask);
    788  1.6.2.2  yamt 	}
    789  1.6.2.2  yamt #endif
    790  1.6.2.2  yamt #endif
    791  1.6.2.2  yamt }
    792  1.6.2.2  yamt 
    793