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