Home | History | Annotate | Line # | Download | only in samsung
exynos_gpio.c revision 1.3
      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.3 2014/05/10 21:46:15 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_OFFSET(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_OFFSET(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, I2C0,  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 	/* find out how many pins we can offer */
    253 	pin_count = 0;
    254 	for (i = 0; i < exynos_n_pin_groups; i++) {
    255 		grp = &exynos_pin_groups[i];
    256 		mask = grp->grp_pin_mask & ~grp->grp_pin_inuse_mask;
    257 		pin_count += popcount32(mask);
    258 	}
    259 
    260 	/* if no pins available, don't proceed */
    261 	if (pin_count == 0)
    262 		return;
    263 
    264 	/* allocate pin data */
    265 	pins = kmem_zalloc(sizeof(gpio_pin_t) * pin_count, KM_SLEEP);
    266 	KASSERT(pins);
    267 
    268 	pincaps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT |
    269 		GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN;
    270 
    271 	/* add all pins */
    272 	pin = pins;
    273 	for (i = 0; i < exynos_n_pin_groups; i++) {
    274 		grp = &exynos_pin_groups[i];
    275 		mask = grp->grp_pin_mask & ~grp->grp_pin_inuse_mask;
    276 		if (mask == 0)
    277 			continue;
    278 		gba.gba_gc = &grp->grp_gc_tag;
    279 		gba.gba_pins = pin;
    280 		data = bus_space_read_1(sc->sc_bst, grp->grp_bsh,
    281 				EXYNOS_GPIO_DAT);
    282 		for (bit = 0; mask != 0; mask >>= 1, data >>= 1, bit++) {
    283 			if (mask & 1) {
    284 				pin->pin_num = bit + (i << 3);
    285 				pin->pin_caps = pincaps;
    286 				pin->pin_flags = pincaps;
    287 				pin->pin_state = (data & 1) != 0;
    288 				pin++;
    289 			}
    290 		}
    291 		gba.gba_npins = pin - gba.gba_pins;
    292 		config_found_ia(self, "gpiobus", &gba, gpiobus_print);
    293 	}
    294 }
    295 #endif
    296 
    297 
    298 static void
    299 exynos_gpio_attach(device_t parent, device_t self, void *aux)
    300 {
    301 	struct exynos_gpio_softc * const sc = &exynos_gpio_sc;
    302 	struct exyo_attach_args * const exyoaa = aux;
    303 	struct exynos_gpio_pin_group *grp;
    304 	prop_dictionary_t dict = device_properties(self);
    305 	uint32_t nc;
    306 	char scrap[16];
    307 	int i;
    308 
    309 	KASSERT(exynos_pin_groups);
    310 	KASSERT(exynos_n_pin_groups);
    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 	aprint_naive("\n");
    320 	aprint_normal("\n");
    321 
    322 	/* go trough all pin groups */
    323 	for (i = 0; i < exynos_n_pin_groups; i++) {
    324 		grp = &exynos_pin_groups[i];
    325 		snprintf(scrap, sizeof(scrap), "nc-%s", grp->grp_name);
    326 		if (prop_dictionary_get_uint32(dict, scrap, &nc)) {
    327 			KASSERT((~grp->grp_pin_mask & nc) == 0);
    328 			KASSERT((grp->grp_pin_inuse_mask & ~nc) == 0);
    329 			grp->grp_pin_mask &= ~nc;
    330 		}
    331 	}
    332 
    333 #if NGPIO > 0
    334 	config_defer(self, exynos_gpio_config_pins);
    335 #endif
    336 }
    337 
    338 
    339 /* pin access functions */
    340 static u_int
    341 exynos_gpio_get_pin_func(const struct exynos_gpio_pin_cfg *cfg, int pin)
    342 {
    343 	const u_int shift = (pin & 7) << 4;
    344 
    345 	return (cfg->cfg >> shift) & 0x0f;
    346 }
    347 
    348 
    349 static void
    350 exynos_gpio_set_pin_func(struct exynos_gpio_pin_cfg *cfg,
    351 	int pin, int func)
    352 {
    353 	const u_int shift = (pin & 7) << 4;
    354 
    355 	cfg->cfg &= ~(0x0f << shift);
    356 	cfg->cfg |= func << shift;
    357 }
    358 
    359 
    360 static void
    361 exynos_gpio_set_pin_pull(struct exynos_gpio_pin_cfg *cfg, int pin, int pull)
    362 {
    363 	const u_int shift = (pin & 7) << 1;
    364 
    365 	cfg->pud &= ~(0x3 << shift);
    366 	cfg->pud |= pull << shift;
    367 }
    368 
    369 
    370 static int
    371 exynos_gpio_pin_read(void *cookie, int pin)
    372 {
    373 	struct exynos_gpio_pin_group * const grp = cookie;
    374 
    375 	KASSERT(pin < grp->grp_bits);
    376 	return (bus_space_read_1(exynos_gpio_sc.sc_bst, grp->grp_bsh,
    377 		EXYNOS_GPIO_DAT) >> pin) & 1;
    378 }
    379 
    380 
    381 static void
    382 exynos_gpio_pin_write(void *cookie, int pin, int value)
    383 {
    384 	struct exynos_gpio_pin_group * const grp = cookie;
    385 	int val;
    386 
    387 	KASSERT(pin < grp->grp_bits);
    388 	val = bus_space_read_1(exynos_gpio_sc.sc_bst, grp->grp_bsh,
    389 		EXYNOS_GPIO_DAT);
    390 	val &= ~__BIT(pin);
    391 	if (value)
    392 		val |= __BIT(pin);
    393 	bus_space_write_1(exynos_gpio_sc.sc_bst, grp->grp_bsh,
    394 		EXYNOS_GPIO_DAT, val);
    395 }
    396 
    397 
    398 static void
    399 exynos_gpio_update_cfg_regs(struct exynos_gpio_pin_group *grp,
    400 	const struct exynos_gpio_pin_cfg *ncfg)
    401 {
    402 	bus_space_tag_t bst = &exynos_bs_tag;
    403 
    404 	if (grp->grp_cfg.cfg != ncfg->cfg) {
    405 		bus_space_write_4(bst, grp->grp_bsh,
    406 			EXYNOS_GPIO_CON, ncfg->cfg);
    407 		grp->grp_cfg.cfg = ncfg->cfg;
    408 	}
    409 	if (grp->grp_cfg.pud != ncfg->pud) {
    410 		bus_space_write_4(bst, grp->grp_bsh,
    411 			EXYNOS_GPIO_PUD, ncfg->pud);
    412 		grp->grp_cfg.pud = ncfg->pud;
    413 	}
    414 
    415 	/* the following attributes are not yet setable */
    416 #if 0
    417 	if (grp->grp_cfg.drv != ncfg->drv) {
    418 		bus_space_write_4(bst, grp->grp_bsh,
    419 			EXYNOS_GPIO_DRV, ncfg->drv);
    420 		grp->grp_cfg.drv = ncfg->drv;
    421 	}
    422 	if (grp->grp_cfg.conpwd != ncfg->conpwd) {
    423 		bus_space_write_4(bst, grp->grp_bsh,
    424 			EXYNOS_GPIO_CONPWD, ncfg->conpwd);
    425 		grp->grp_cfg.conpwd = ncfg->conpwd;
    426 	}
    427 	if (grp->grp_cfg.pudpwd != ncfg->pudpwd) {
    428 		bus_space_write_4(bst, grp->grp_bsh,
    429 			EXYNOS_GPIO_PUDPWD, ncfg->pudpwd);
    430 		grp->grp_cfg.pudpwd = ncfg->pudpwd;
    431 	}
    432 #endif
    433 }
    434 
    435 
    436 static void
    437 exynos_gpio_pin_ctl(void *cookie, int pin, int flags)
    438 {
    439 	struct exynos_gpio_pin_group * const grp = cookie;
    440 	struct exynos_gpio_pin_cfg ncfg = grp->grp_cfg;
    441 	int pull;
    442 
    443 	/* honour pullup requests */
    444 	pull = EXYNOS_GPIO_PIN_FLOAT;
    445 	if (flags & GPIO_PIN_PULLUP)
    446 		pull = EXYNOS_GPIO_PIN_PULL_UP;
    447 	if (flags & GPIO_PIN_PULLDOWN)
    448 		pull = EXYNOS_GPIO_PIN_PULL_DOWN;
    449 	exynos_gpio_set_pin_pull(&ncfg, pin, pull);
    450 
    451 	/* honour i/o */
    452 	if (flags & GPIO_PIN_INPUT)
    453 		exynos_gpio_set_pin_func(&ncfg, pin, EXYNOS_GPIO_FUNC_INPUT);
    454 	if (flags & GPIO_PIN_OUTPUT)
    455 		exynos_gpio_set_pin_func(&ncfg, pin, EXYNOS_GPIO_FUNC_OUTPUT);
    456 
    457 	/* update any config registers that changed */
    458 	exynos_gpio_update_cfg_regs(grp, &ncfg);
    459 }
    460 
    461 
    462 bool
    463 exynos_gpio_pinset_available(const struct exynos_gpio_pinset *req)
    464 {
    465 	struct exynos_gpio_pin_group *grp;
    466 	int i, n, inuse;
    467 
    468 	KASSERT(req);
    469 
    470 	/* we need a pinset group */
    471 	if (strlen(req->pinset_group) == 0)
    472 		return false;
    473 
    474 	/* determine which group is requested */
    475 	grp = NULL;
    476 	for (i = 0; i < exynos_n_pin_groups; i++) {
    477 		grp = &exynos_pin_groups[i];
    478 		if (strcmp(req->pinset_group, grp->grp_name) == 0)
    479 			break;
    480 	}
    481 	/* found? */
    482 	if (i == exynos_n_pin_groups)
    483 		return false;
    484 	KASSERT(grp);
    485 
    486 	/* fail unconnected pins */
    487 	if (req->pinset_mask & ~grp->grp_pin_mask)
    488 		return false;
    489 
    490 	/* if none in use, they are available */
    491 	if (req->pinset_mask & ~grp->grp_pin_inuse_mask)
    492 		return true;
    493 
    494 	/* OK, so some are in use; now see if the request is compatible */
    495 	inuse = req->pinset_mask & grp->grp_pin_inuse_mask;
    496 	for (i = 0; inuse; i++, inuse >>= 1) {
    497 		/* try to be smart by skipping zero's */
    498 		n = ffs(inuse) -1;
    499 		i += n;
    500 		inuse >>= n;
    501 		/* this pin is in use, check its usage */
    502 		if (exynos_gpio_get_pin_func(&grp->grp_cfg, i) != req->pinset_func)
    503 			return false;
    504 	}
    505 
    506 	/* seems to be OK */
    507 	return true;
    508 }
    509 
    510 
    511 void
    512 exynos_gpio_pinset_acquire(const struct exynos_gpio_pinset *req)
    513 {
    514 	struct exynos_gpio_pin_group *grp;
    515 	struct exynos_gpio_pin_cfg ncfg;
    516 	int i, n, todo;
    517 
    518 	KASSERT(req);
    519 	KASSERT(exynos_gpio_pinset_available(req));
    520 
    521 	/* determine which group is requested */
    522 	grp = NULL;
    523 	for (i = 0; i < exynos_n_pin_groups; i++) {
    524 		grp = &exynos_pin_groups[i];
    525 		if (strcmp(req->pinset_group, grp->grp_name) == 0)
    526 			break;
    527 	}
    528 	KASSERT(grp);
    529 
    530 	/* check if all the pins have the right function */
    531 	if ((req->pinset_mask & ~grp->grp_pin_inuse_mask) == 0)
    532 		return;
    533 
    534 	/* copy current config for update routine */
    535 	ncfg = grp->grp_cfg;
    536 
    537 	/* update the function of each pin that is not in use */
    538 	todo = req->pinset_mask & grp->grp_pin_inuse_mask;
    539 	for (i = 0; todo; i++, todo >>= 1) {
    540 		/* try to be smart by skipping zero's */
    541 		n = ffs(todo) -1;
    542 		i += n;
    543 		todo >>= n;
    544 		/* change the function of this pin */
    545 		exynos_gpio_set_pin_func(&ncfg, i, req->pinset_func);
    546 	}
    547 
    548 	/* update config registers */
    549 	exynos_gpio_update_cfg_regs(grp, &ncfg);
    550 
    551 	/* mark pins in use */
    552 	grp->grp_pin_inuse_mask |= req->pinset_mask;
    553 }
    554 
    555 
    556 /* XXXRPZ This release doesn't grock multiple usages! */
    557 void
    558 exynos_gpio_pinset_release(const struct exynos_gpio_pinset *req)
    559 {
    560 	struct exynos_gpio_pin_group *grp;
    561 	int i;
    562 
    563 	KASSERT(!exynos_gpio_pinset_available(req));
    564 
    565 	/* determine which group is requested */
    566 	grp = NULL;
    567 	for (i = 0; i < exynos_n_pin_groups; i++) {
    568 		grp = &exynos_pin_groups[i];
    569 		if (strcmp(req->pinset_group, grp->grp_name) == 0)
    570 			break;
    571 	}
    572 	KASSERT(grp);
    573 
    574 	/* bluntly mark as not being in use */
    575 	grp->grp_pin_inuse_mask &= ~req->pinset_mask;
    576 }
    577 
    578 
    579 /*
    580  * name convention :
    581  * pin   = <func><groupname><pinnr>[<pud>]
    582  * func  = '<' | '>'
    583  * pinnr = '['['0'-'7']']'
    584  * pud   =  'F' | 'U' | 'D'
    585  *
    586  * example "<GPC1[0]", ">GPB[0]"
    587  */
    588 
    589 bool
    590 exynos_gpio_pin_reserve(const char *name, struct exynos_gpio_pindata *pd)
    591 {
    592 	struct exynos_gpio_softc * const sc = &exynos_gpio_sc;
    593 	struct exynos_gpio_pin_group *grp;
    594 	struct exynos_gpio_pin_cfg ncfg;
    595 	prop_dictionary_t dict = device_properties(sc->sc_dev);
    596 	const char *pin_data;
    597 	char grp_name[15], *pos;
    598 	int func, pud, pinnr;
    599 	int pi, i;
    600 
    601 	/* do we have a named pin description? */
    602 	if (!prop_dictionary_get_cstring_nocopy(dict, name, &pin_data))
    603 		return false;
    604 
    605 	KASSERT(strlen(pin_data) < 10);
    606 	if (!(pin_data[0] == '>' || pin_data[0] == '<')) {
    607 		printf("%s: malformed pin data in '%s', missing direction\n",
    608 			__func__, pin_data);
    609 		return false;
    610 	}
    611 
    612 	func = (pin_data[0] == '<') ?
    613 		EXYNOS_GPIO_FUNC_INPUT : EXYNOS_GPIO_FUNC_OUTPUT;
    614 
    615 	/* find groupname */
    616 	pi = 1; pos = grp_name;
    617 	while (pin_data[pi] && pin_data[pi] != '[') {
    618 		*pos++ = pin_data[pi++];
    619 	}
    620 	if (pin_data[pi] != '[') {
    621 		printf("%s: malformed pin data in '%s', missing '['\n",
    622 			__func__, pin_data);
    623 		return false;
    624 	}
    625 	*pos++ = (char) 0;
    626 
    627 	/* skip '[' */
    628 	pi++;
    629 	if (!(pin_data[pi] >= '0' && pin_data[pi] <= '7')) {
    630 		printf("%s: malformed pin data in '%s', bad pin number\n",
    631 			__func__, pin_data);
    632 		return false;
    633 	}
    634 	pinnr = pin_data[pi] - '0';
    635 
    636 	/* skip digit */
    637 	pi++;
    638 	if ((pin_data[pi] != ']')) {
    639 		printf("%s: malformed pin data in '%s', missing end ']'\n",
    640 			__func__, pin_data);
    641 		return false;
    642 	}
    643 
    644 	/* skip ']' */
    645 	pi++;
    646 	pud = EXYNOS_GPIO_PIN_FLOAT;
    647 	switch (tolower(pin_data[pi])) {
    648 		case (char) 0:
    649 			break;
    650 		case 'f':
    651 			pud = EXYNOS_GPIO_PIN_FLOAT;
    652 			break;
    653 		case 'u':
    654 			pud = EXYNOS_GPIO_PIN_PULL_UP;
    655 			break;
    656 		case 'd':
    657 			pud = EXYNOS_GPIO_PIN_PULL_DOWN;
    658 			break;
    659 		default:
    660 			printf("%s: malformed pin data in '%s', expecting "
    661 				"optional pull up/down or float argument\n",
    662 				__func__, pin_data);
    663 		return false;
    664 	}
    665 
    666 	/* determine which group is requested */
    667 	grp = NULL;
    668 	for (i = 0; i < exynos_n_pin_groups; i++) {
    669 		grp = &exynos_pin_groups[i];
    670 		if (strcmp(grp_name, grp->grp_name) == 0)
    671 			break;
    672 	}
    673 
    674 	/* found? */
    675 	if (i >= exynos_n_pin_groups) {
    676 		printf("%s: malformed pin data in '%s', "
    677 			"no such pin group name\n",
    678 			__func__, grp_name);
    679 		return false;
    680 	}
    681 	KASSERT(grp);
    682 
    683 	KASSERT(pinnr < grp->grp_bits);
    684 	KASSERT(grp->grp_pin_mask & __BIT(pinnr));
    685 	KASSERT((grp->grp_pin_inuse_mask & __BIT(pinnr)) == 0);
    686 
    687 	/* update our pin configuration */
    688 	ncfg = grp->grp_cfg;
    689 	exynos_gpio_set_pin_func(&ncfg, pinnr, func);
    690 	exynos_gpio_set_pin_pull(&ncfg, pinnr, pud);
    691 	exynos_gpio_update_cfg_regs(grp, &ncfg);
    692 
    693 	grp->grp_pin_inuse_mask &= ~__BIT(pinnr);
    694 
    695 	pd->pd_gc = &grp->grp_gc_tag;
    696 	pd->pd_pin = pinnr;
    697 
    698 	return true;
    699 }
    700 
    701 
    702 /* bootstrapping */
    703 void
    704 exynos_gpio_bootstrap(void)
    705 {
    706 	bus_space_tag_t bst = &exynos_bs_tag;
    707 	struct exynos_gpio_pin_group *grp;
    708 	struct gpio_chipset_tag *gc_tag;
    709 	int i, j, func, mask;
    710 
    711 	/* determine what we're running on */
    712 #ifdef EXYNOS4
    713 	if (IS_EXYNOS4_P()) {
    714 		exynos_pin_groups = exynos4_pin_groups;
    715 		exynos_n_pin_groups = __arraycount(exynos4_pin_groups);
    716 	}
    717 #endif
    718 #ifdef EXYNOS5
    719 	if (IS_EXYNOS5_P()) {
    720 		exynos_pin_groups = exynos5_pin_groups;
    721 		exynos_n_pin_groups = __arraycount(exynos5_pin_groups);
    722 	}
    723 #endif
    724 
    725 #ifdef VERBOSE_INIT_ARM
    726 	printf("gpio");
    727 #endif
    728 	if (exynos_n_pin_groups == 0) {
    729 #ifdef VERBOSE_INIT_ARM
    730 		printf("  (disabled)\n");
    731 #endif
    732 		return;
    733 	}
    734 
    735 #ifdef VERBOSE_INIT_ARM
    736 	printf(" free");
    737 #endif
    738 	/* init groups */
    739 	for (i = 0; i < exynos_n_pin_groups; i++) {
    740 		grp = &exynos_pin_groups[i];
    741 		gc_tag = &grp->grp_gc_tag;
    742 
    743 		bus_space_subregion(&exynos_bs_tag, exynos_core_bsh,
    744 			grp->grp_core_offset, EXYNOS_GPIO_GRP_SIZE,
    745 			&grp->grp_bsh);
    746 		grp->grp_pin_mask = __BIT(grp->grp_bits) - 1;
    747 		grp->grp_pin_inuse_mask = 0;
    748 
    749 		gc_tag->gp_cookie = grp;
    750 		gc_tag->gp_pin_read  = exynos_gpio_pin_read;
    751 		gc_tag->gp_pin_write = exynos_gpio_pin_write;
    752 		gc_tag->gp_pin_ctl   = exynos_gpio_pin_ctl;
    753 
    754 		/* read in our initial settings */
    755 		grp->grp_cfg.cfg = bus_space_read_4(bst, grp->grp_bsh,
    756 			EXYNOS_GPIO_CON);
    757 		grp->grp_cfg.pud = bus_space_read_4(bst, grp->grp_bsh,
    758 			EXYNOS_GPIO_PUD);
    759 		grp->grp_cfg.drv = bus_space_read_4(bst, grp->grp_bsh,
    760 			EXYNOS_GPIO_DRV);
    761 		grp->grp_cfg.conpwd = bus_space_read_4(bst, grp->grp_bsh,
    762 			EXYNOS_GPIO_CONPWD);
    763 		grp->grp_cfg.pudpwd = bus_space_read_4(bst, grp->grp_bsh,
    764 			EXYNOS_GPIO_PUDPWD);
    765 
    766 		/* count number of busy pins */
    767 		for (j = 0, mask = 1;
    768 		     (mask & grp->grp_pin_mask) != 0;
    769 		     j++, mask <<= 1) {
    770 			func = exynos_gpio_get_pin_func(&grp->grp_cfg, j);
    771 			if (func > EXYNOS_GPIO_FUNC_INPUT) {
    772 				grp->grp_pin_inuse_mask |= mask;
    773 			}
    774 		}
    775 #ifdef VERBOSE_INIT_ARM
    776 		printf(" P%s = %d", grp->grp_name,
    777 			popcount32(grp->grp_pin_mask & ~grp->grp_pin_inuse_mask));
    778 #endif
    779 	}
    780 #ifdef VERBOSE_INIT_ARM
    781 	printf("\n");
    782 #if 0
    783 	/* enable this for default NC pins list generation */
    784 	for (i = 0; i < exynos_n_pin_groups; i++) {
    785 		grp = &exynos_pin_groups[i];
    786 		printf("prop_dictionary_set_uint32(dict, \"nc-%s\", 0x%02x - 0x00);\n",
    787 			grp->grp_name, grp->grp_pin_mask);
    788 	}
    789 #endif
    790 #endif
    791 }
    792 
    793