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