Home | History | Annotate | Line # | Download | only in samsung
exynos_gpio.c revision 1.20
      1 /*	$NetBSD: exynos_gpio.c,v 1.20 2015/12/27 12:22:28 jmcneill Exp $ */
      2 
      3 /*-
      4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5 * All rights reserved.
      6 *
      7 * This code is derived from software contributed to The NetBSD Foundation
      8 * by Reinoud Zandijk
      9 *
     10 * Redistribution and use in source and binary forms, with or without
     11 * modification, are permitted provided that the following conditions
     12 * are met:
     13 * 1. Redistributions of source code must retain the above copyright
     14 *    notice, this list of conditions and the following disclaimer.
     15 * 2. Redistributions in binary form must reproduce the above copyright
     16 *    notice, this list of conditions and the following disclaimer in the
     17 *    documentation and/or other materials provided with the distribution.
     18 *
     19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29 * POSSIBILITY OF SUCH DAMAGE.
     30 */
     31 
     32 #include "opt_exynos.h"
     33 #include "opt_arm_debug.h"
     34 #include "gpio.h"
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(1, "$NetBSD: exynos_gpio.c,v 1.20 2015/12/27 12:22:28 jmcneill Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/bus.h>
     41 #include <sys/device.h>
     42 #include <sys/intr.h>
     43 #include <sys/systm.h>
     44 #include <sys/kmem.h>
     45 #include <sys/gpio.h>
     46 
     47 #include <dev/gpio/gpiovar.h>
     48 
     49 #include <arm/samsung/exynos_reg.h>
     50 #include <arm/samsung/exynos_var.h>
     51 #include <arm/samsung/exynos_intr.h>
     52 #include <arm/samsung/exynos_pinctrl.h>
     53 
     54 #include <dev/fdt/fdtvar.h>
     55 
     56 struct exynos_gpio_pin_cfg {
     57 	uint32_t cfg;
     58 	uint32_t pud;
     59 	uint32_t drv;
     60 	uint32_t conpwd;
     61 	uint32_t pudpwd;
     62 };
     63 
     64 struct exynos_gpio_softc;
     65 
     66 struct exynos_gpio_bank {
     67 	const char		bank_name[6];
     68 	device_t		bank_dev;
     69 	struct gpio_chipset_tag	bank_gc;
     70 	struct exynos_gpio_softc *bank_sc;
     71 	gpio_pin_t		bank_pins[8];
     72 
     73 	const bus_addr_t	bank_core_offset;
     74 	const uint8_t		bank_bits;
     75 
     76 	uint8_t			bank_pin_mask;
     77 	uint8_t			bank_pin_inuse_mask;
     78 	bus_space_handle_t	bank_bsh;
     79 	struct exynos_gpio_pin_cfg bank_cfg;
     80 	struct exynos_gpio_bank * bank_next;
     81 };
     82 
     83 struct exynos_gpio_softc {
     84 	device_t		sc_dev;
     85 	bus_space_tag_t		sc_bst;
     86 	bus_space_handle_t	sc_bsh;
     87 };
     88 
     89 struct exynos_gpio_pin {
     90 	struct exynos_gpio_softc *pin_sc;
     91 	int			  pin_no;
     92 	u_int			  pin_flags;
     93 	int			  pin_actlo;
     94 	const struct exynos_gpio_bank   *pin_bank;
     95 };
     96 
     97 
     98 //#define GPIO_REG(v,s,o) (EXYNOS##v##_GPIO_##s##_OFFSET + (o))
     99 #define GPIO_REG(v,s,o) ((o))
    100 #define GPIO_GRP(v, s, o, n, b)	\
    101 	{ \
    102 		.bank_name = #n, \
    103 		.bank_core_offset = GPIO_REG(v,s,o), \
    104 		.bank_bits = b, \
    105 	}
    106 
    107 static struct exynos_gpio_bank exynos5_banks[] = {
    108 	GPIO_GRP(5, MUXA, 0x0000, gpy7, 8),
    109 	GPIO_GRP(5, MUXA, 0x0C00, gpx0, 8),
    110 	GPIO_GRP(5, MUXA, 0x0C20, gpx1, 8),
    111 	GPIO_GRP(5, MUXA, 0x0C40, gpx2, 8),
    112 	GPIO_GRP(5, MUXA, 0x0C60, gpx3, 8),
    113 
    114 	GPIO_GRP(5, MUXB, 0x0000, gpc0, 8),
    115 	GPIO_GRP(5, MUXB, 0x0020, gpc1, 8),
    116 	GPIO_GRP(5, MUXB, 0x0040, gpc2, 7),
    117 	GPIO_GRP(5, MUXB, 0x0060, gpc3, 4),
    118 	GPIO_GRP(5, MUXB, 0x0080, gpc4, 2),
    119 	GPIO_GRP(5, MUXB, 0x00A0, gpd1, 8),
    120 	GPIO_GRP(5, MUXB, 0x00C0, gpy0, 6),
    121 	GPIO_GRP(5, MUXB, 0x00E0, gpy1, 4),
    122 	GPIO_GRP(5, MUXB, 0x0100, gpy2, 6),
    123 	GPIO_GRP(5, MUXB, 0x0120, gpy3, 8),
    124 	GPIO_GRP(5, MUXB, 0x0140, gpy4, 8),
    125 	GPIO_GRP(5, MUXB, 0x0160, gpy5, 8),
    126 	GPIO_GRP(5, MUXB, 0x0180, gpy6, 8),
    127 
    128 	GPIO_GRP(5, MUXC, 0x0000, gpe0, 8),
    129 	GPIO_GRP(5, MUXC, 0x0020, gpe1, 2),
    130 	GPIO_GRP(5, MUXC, 0x0040, gpf0, 6),
    131 	GPIO_GRP(5, MUXC, 0x0060, gpf1, 8),
    132 	GPIO_GRP(5, MUXC, 0x0080, gpg0, 8),
    133 	GPIO_GRP(5, MUXC, 0x00A0, gpg1, 8),
    134 	GPIO_GRP(5, MUXC, 0x00C0, gpg2, 2),
    135 	GPIO_GRP(5, MUXC, 0x00E0, gpj4, 4),
    136 
    137 	GPIO_GRP(5, MUXD, 0x0000, gpa0, 8),
    138 	GPIO_GRP(5, MUXD, 0x0020, gpa1, 6),
    139 	GPIO_GRP(5, MUXD, 0x0040, gpa2, 8),
    140 	GPIO_GRP(5, MUXD, 0x0060, gpb0, 5),
    141 	GPIO_GRP(5, MUXD, 0x0080, gpb1, 5),
    142 	GPIO_GRP(5, MUXD, 0x00A0, gpb2, 4),
    143 	GPIO_GRP(5, MUXD, 0x00C0, gpb3, 8),
    144 	GPIO_GRP(5, MUXD, 0x00E0, gpb4, 2),
    145 	GPIO_GRP(5, MUXD, 0x0100, gph0, 4),
    146 
    147 	GPIO_GRP(5, MUXE, 0x0000, gpz0, 7),
    148 
    149 };
    150 
    151 struct exynos_gpio_bank *exynos_gpio_banks = exynos5_banks;
    152 
    153 static int exynos_gpio_pin_read(void *, int);
    154 static void exynos_gpio_pin_write(void *, int, int);
    155 static void exynos_gpio_pin_ctl(void *, int, int);
    156 static void *exynos_gpio_fdt_acquire(device_t, const void *,
    157 				     size_t, int);
    158 static void exynos_gpio_fdt_release(device_t, void *);
    159 
    160 static int exynos_gpio_fdt_read(device_t, void *, bool);
    161 static void exynos_gpio_fdt_write(device_t, void *, int, bool);
    162 static struct exynos_gpio_bank *exynos_gpio_bank_lookup(const char *);
    163 static int exynos_gpio_cfprint(void *, const char *);
    164 
    165 struct fdtbus_gpio_controller_func exynos_gpio_funcs = {
    166 	.acquire = exynos_gpio_fdt_acquire,
    167 	.release = exynos_gpio_fdt_release,
    168 	.read = exynos_gpio_fdt_read,
    169 	.write = exynos_gpio_fdt_write
    170 };
    171 #define GPIO_WRITE(bank, reg, val) \
    172 	bus_space_write_4((bank)->bank_sc->sc_bst, \
    173 	    (bank)->bank_sc->sc_bsh, \
    174 	    (bank)->bank_core_offset + (reg), (val))
    175 #define GPIO_READ(bank, reg) \
    176 	bus_space_read_4((bank)->bank_sc->sc_bst, \
    177 	    (bank)->bank_sc->sc_bsh, \
    178 	    (bank)->bank_core_offset + (reg))
    179 
    180 static int
    181 exynos_gpio_cfprint(void *priv, const char *pnp)
    182 {
    183 	struct gpiobus_attach_args *gba = priv;
    184 	struct exynos_gpio_bank *bank = gba->gba_gc->gp_cookie;
    185 	const char *bankname = bank->bank_name;
    186 
    187 	if (pnp)
    188 		aprint_normal("gpiobus at %s", pnp);
    189 
    190 	aprint_normal(" (%s)", bankname);
    191 
    192 	return UNCONF;
    193 }
    194 
    195 static void
    196 exynos_gpio_update_cfg_regs(struct exynos_gpio_bank *bank,
    197 	const struct exynos_gpio_pin_cfg *ncfg)
    198 {
    199 	if (bank->bank_cfg.cfg != ncfg->cfg) {
    200 		GPIO_WRITE(bank, EXYNOS_GPIO_CON, ncfg->cfg);
    201 		bank->bank_cfg.cfg = ncfg->cfg;
    202 	}
    203 	if (bank->bank_cfg.pud != ncfg->pud) {
    204 		GPIO_WRITE(bank, EXYNOS_GPIO_PUD, ncfg->pud);
    205 		bank->bank_cfg.pud = ncfg->pud;
    206 	}
    207 
    208 	if (bank->bank_cfg.drv != ncfg->drv) {
    209 		GPIO_WRITE(bank, EXYNOS_GPIO_DRV, ncfg->drv);
    210 		bank->bank_cfg.drv = ncfg->drv;
    211 	}
    212 	if (bank->bank_cfg.conpwd != ncfg->conpwd) {
    213 		GPIO_WRITE(bank, EXYNOS_GPIO_CONPWD, ncfg->conpwd);
    214 		bank->bank_cfg.conpwd = ncfg->conpwd;
    215 	}
    216 	if (bank->bank_cfg.pudpwd != ncfg->pudpwd) {
    217 		GPIO_WRITE(bank, EXYNOS_GPIO_PUDPWD, ncfg->pudpwd);
    218 		bank->bank_cfg.pudpwd = ncfg->pudpwd;
    219 	}
    220 }
    221 
    222 static int
    223 exynos_gpio_pin_read(void *cookie, int pin)
    224 {
    225 	struct exynos_gpio_bank * const bank = cookie;
    226 
    227 	KASSERT(pin < bank->bank_bits);
    228 	return (bus_space_read_1(bank->bank_sc->sc_bst,
    229 				 bank->bank_sc->sc_bsh,
    230 		EXYNOS_GPIO_DAT) >> pin) & 1;
    231 }
    232 
    233 static void
    234 exynos_gpio_pin_write(void *cookie, int pin, int value)
    235 {
    236 	struct exynos_gpio_bank * const bank = cookie;
    237 	int val;
    238 
    239 	KASSERT(pin < bank->bank_bits);
    240 	val = bus_space_read_1(bank->bank_sc->sc_bst,
    241 			       bank->bank_sc->sc_bsh,
    242 			       EXYNOS_GPIO_DAT);
    243 	val &= ~__BIT(pin);
    244 	if (value)
    245 		val |= __BIT(pin);
    246 	bus_space_write_1(bank->bank_sc->sc_bst,
    247 			  bank->bank_sc->sc_bsh,
    248 		EXYNOS_GPIO_DAT, val);
    249 }
    250 
    251 static void
    252 exynos_gpio_pin_ctl(void *cookie, int pin, int flags)
    253 {
    254 	struct exynos_gpio_bank * const bank = cookie;
    255 	struct exynos_gpio_pin_cfg ncfg = bank->bank_cfg;
    256 	u_int shift;
    257 	int pull;
    258 
    259 	/* honour pullup requests */
    260 	pull = EXYNOS_GPIO_PIN_FLOAT;
    261 	if (flags & GPIO_PIN_PULLUP)
    262 		pull = EXYNOS_GPIO_PIN_PULL_UP;
    263 	if (flags & GPIO_PIN_PULLDOWN)
    264 		pull = EXYNOS_GPIO_PIN_PULL_DOWN;
    265 	shift = (pin & 7) << 1;
    266 	ncfg.pud &= ~(0x3 << shift);
    267 	ncfg.pud |= pull << shift;
    268 
    269 	/* honour i/o */
    270 	if (flags & GPIO_PIN_INPUT) {
    271 		ncfg.cfg &= ~(0x0f << shift);
    272 		ncfg.cfg |= EXYNOS_GPIO_FUNC_INPUT << shift;
    273 	} else if (flags & GPIO_PIN_OUTPUT) {
    274 		ncfg.cfg &= ~(0x0f << shift);
    275 		ncfg.cfg |= EXYNOS_GPIO_FUNC_OUTPUT << shift;
    276 	}
    277 
    278 	/* update any config registers that changed */
    279 	exynos_gpio_update_cfg_regs(bank, &ncfg);
    280 }
    281 
    282 void
    283 exynos_gpio_bank_config(struct exynos_pinctrl_softc * parent,
    284 			const struct fdt_attach_args *faa, int node)
    285 {
    286 	struct exynos_gpio_bank *bank = kmem_zalloc(sizeof(*bank), KM_SLEEP);
    287 	struct exynos_gpio_softc *sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
    288 	struct gpiobus_attach_args gba;
    289 	struct gpio_chipset_tag *gc_tag;
    290 	char result[64];
    291 
    292 	OF_getprop(node, "name", result, sizeof(result));
    293 	bank = exynos_gpio_bank_lookup(result);
    294 	if (bank == NULL) {
    295 		aprint_error_dev(parent->sc_dev, "no bank found for %s\n",
    296 		    result);
    297 		return;
    298 	}
    299 
    300 	sc->sc_dev = parent->sc_dev;
    301 	sc->sc_bst = &armv7_generic_bs_tag;
    302 	sc->sc_bsh = parent->sc_bsh;
    303 
    304 	gc_tag = &bank->bank_gc;
    305 	gc_tag->gp_cookie = bank;
    306 	gc_tag->gp_pin_read  = exynos_gpio_pin_read;
    307 	gc_tag->gp_pin_write = exynos_gpio_pin_write;
    308 	gc_tag->gp_pin_ctl   = exynos_gpio_pin_ctl;
    309 	memset(&gba, 0, sizeof(gba));
    310 	gba.gba_gc = &bank->bank_gc;
    311 	gba.gba_pins = bank->bank_pins;
    312 	gba.gba_npins = bank->bank_bits;
    313 	bank->bank_sc = sc;
    314 	bank->bank_dev = config_found_ia(parent->sc_dev, "gpiobus", &gba,
    315 					 exynos_gpio_cfprint);
    316 
    317 	bank->bank_pin_mask = __BIT(bank->bank_bits) - 1;
    318 	bank->bank_pin_inuse_mask = 0;
    319 
    320 
    321 	/* read in our initial settings */
    322 	bank->bank_cfg.cfg = GPIO_READ(bank, EXYNOS_GPIO_CON);
    323 	bank->bank_cfg.pud = GPIO_READ(bank, EXYNOS_GPIO_PUD);
    324 	bank->bank_cfg.drv = GPIO_READ(bank, EXYNOS_GPIO_DRV);
    325 	bank->bank_cfg.conpwd = GPIO_READ(bank, EXYNOS_GPIO_CONPWD);
    326 	bank->bank_cfg.pudpwd = GPIO_READ(bank, EXYNOS_GPIO_PUDPWD);
    327 
    328 	fdtbus_register_gpio_controller(bank->bank_dev, faa->faa_phandle,
    329 					&exynos_gpio_funcs);
    330 }
    331 
    332 static struct exynos_gpio_bank *
    333 exynos_gpio_bank_lookup(const char *name)
    334 {
    335 	for (u_int n = 0; n < __arraycount(exynos5_banks); n++) {
    336 		struct exynos_gpio_bank *bank = &exynos_gpio_banks[n];
    337 		if (strncmp(bank->bank_name, name, strlen(name)) == 0) {
    338 			return bank;
    339 		}
    340 	}
    341 
    342 	return NULL;
    343 }
    344 
    345 #if notyet
    346 static int
    347 exynos_gpio_pin_lookup(const char *name)
    348 {
    349 	char *p;
    350 
    351 	p = strchr(name, '-');
    352 	if (p == NULL || p[1] < '0' || p[1] > '9')
    353 		return -1;
    354 
    355 	return p[1] - '0';
    356 }
    357 #endif
    358 
    359 static void *
    360 exynos_gpio_fdt_acquire(device_t dev, const void *data, size_t len, int flags)
    361 {
    362 	const u_int *cells = data;
    363 	const struct exynos_gpio_bank *bank = NULL;
    364 	struct exynos_gpio_pin *gpin;
    365 	int n;
    366 
    367 	if (len != 2)
    368 		return NULL;
    369 
    370 	const int pin = be32toh(cells[0]) & 0x0f;
    371 	const int actlo = be32toh(cells[1]) & 0x01;
    372 
    373 	for (n = 0; n < __arraycount(exynos5_banks); n++) {
    374 		if (exynos_gpio_banks[n].bank_sc->sc_dev == dev) {
    375 			bank = &exynos_gpio_banks[n];
    376 			break;
    377 		}
    378 	}
    379 	if (bank == NULL)
    380 		return NULL;
    381 
    382 	printf("gpio pin %s-%d being acquired\n", bank->bank_name, pin);
    383 	gpin = kmem_alloc(sizeof(*gpin), KM_SLEEP);
    384 	gpin->pin_sc = bank->bank_sc;
    385 	gpin->pin_bank = bank;
    386 	gpin->pin_no = pin;
    387 	gpin->pin_flags = flags;
    388 	gpin->pin_actlo = actlo;
    389 
    390 	exynos_gpio_pin_ctl(&gpin->pin_bank, gpin->pin_no, gpin->pin_flags);
    391 
    392 	return gpin;
    393 }
    394 
    395 static void
    396 exynos_gpio_fdt_release(device_t dev, void *priv)
    397 {
    398 	struct exynos_gpio_pin *gpin = priv;
    399 
    400 	kmem_free(gpin, sizeof(*gpin));
    401 }
    402 
    403 static int
    404 exynos_gpio_fdt_read(device_t dev, void *priv, bool raw)
    405 {
    406 	struct exynos_gpio_pin *gpin = priv;
    407 	int val;
    408 
    409 	val = (bus_space_read_1(gpin->pin_sc->sc_bst,
    410 				 gpin->pin_sc->sc_bsh,
    411 				 EXYNOS_GPIO_DAT) >> gpin->pin_no) & 1;
    412 
    413 	if (!raw && gpin->pin_actlo)
    414 		val = !val;
    415 
    416 	return val;
    417 }
    418 
    419 static void
    420 exynos_gpio_fdt_write(device_t dev, void *priv, int val, bool raw)
    421 {
    422 	struct exynos_gpio_pin *gpin = priv;
    423 
    424 	if (!raw && gpin->pin_actlo)
    425 		val = !val;
    426 
    427 	val = bus_space_read_1(gpin->pin_sc->sc_bst,
    428 			       gpin->pin_sc->sc_bsh,
    429 			       EXYNOS_GPIO_DAT);
    430 	val &= ~__BIT(gpin->pin_no);
    431 	if (val)
    432 		val |= __BIT(gpin->pin_no);
    433 	bus_space_write_1(gpin->pin_sc->sc_bst,
    434 			  gpin->pin_sc->sc_bsh,
    435 			  EXYNOS_GPIO_DAT, val);
    436 
    437 }
    438