1b9a66b63SMasahiro Yamada /* 2*4e3d8406SMasahiro Yamada * Copyright (C) 2016 Socionext Inc. 3*4e3d8406SMasahiro Yamada * Author: Masahiro Yamada <yamada.masahiro@socionext.com> 4b9a66b63SMasahiro Yamada * 5b9a66b63SMasahiro Yamada * SPDX-License-Identifier: GPL-2.0+ 6b9a66b63SMasahiro Yamada */ 7b9a66b63SMasahiro Yamada 8b9a66b63SMasahiro Yamada #include <common.h> 9b9a66b63SMasahiro Yamada #include <dm/device.h> 10b9a66b63SMasahiro Yamada #include <linux/bitops.h> 11b9a66b63SMasahiro Yamada #include <linux/io.h> 12bc82a131SMasahiro Yamada #include <linux/sizes.h> 13b9a66b63SMasahiro Yamada #include <asm/errno.h> 14b9a66b63SMasahiro Yamada #include <asm/gpio.h> 15b9a66b63SMasahiro Yamada 16b9a66b63SMasahiro Yamada #define UNIPHIER_GPIO_PORTS_PER_BANK 8 17b9a66b63SMasahiro Yamada 18b9a66b63SMasahiro Yamada #define UNIPHIER_GPIO_REG_DATA 0 /* data */ 19b9a66b63SMasahiro Yamada #define UNIPHIER_GPIO_REG_DIR 4 /* direction (1:in, 0:out) */ 20b9a66b63SMasahiro Yamada 21b9a66b63SMasahiro Yamada struct uniphier_gpio_priv { 22b9a66b63SMasahiro Yamada void __iomem *base; 23b9a66b63SMasahiro Yamada char bank_name[16]; 24b9a66b63SMasahiro Yamada }; 25b9a66b63SMasahiro Yamada 26b9a66b63SMasahiro Yamada static void uniphier_gpio_offset_write(struct udevice *dev, unsigned offset, 27b9a66b63SMasahiro Yamada unsigned reg, int value) 28b9a66b63SMasahiro Yamada { 29b9a66b63SMasahiro Yamada struct uniphier_gpio_priv *priv = dev_get_priv(dev); 30b9a66b63SMasahiro Yamada u32 tmp; 31b9a66b63SMasahiro Yamada 32b9a66b63SMasahiro Yamada tmp = readl(priv->base + reg); 33b9a66b63SMasahiro Yamada if (value) 34b9a66b63SMasahiro Yamada tmp |= BIT(offset); 35b9a66b63SMasahiro Yamada else 36b9a66b63SMasahiro Yamada tmp &= ~BIT(offset); 37b9a66b63SMasahiro Yamada writel(tmp, priv->base + reg); 38b9a66b63SMasahiro Yamada } 39b9a66b63SMasahiro Yamada 40b9a66b63SMasahiro Yamada static int uniphier_gpio_offset_read(struct udevice *dev, unsigned offset, 41b9a66b63SMasahiro Yamada unsigned reg) 42b9a66b63SMasahiro Yamada { 43b9a66b63SMasahiro Yamada struct uniphier_gpio_priv *priv = dev_get_priv(dev); 44b9a66b63SMasahiro Yamada 45b9a66b63SMasahiro Yamada return !!(readl(priv->base + reg) & BIT(offset)); 46b9a66b63SMasahiro Yamada } 47b9a66b63SMasahiro Yamada 48b9a66b63SMasahiro Yamada static int uniphier_gpio_direction_input(struct udevice *dev, unsigned offset) 49b9a66b63SMasahiro Yamada { 50b9a66b63SMasahiro Yamada uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_REG_DIR, 1); 51b9a66b63SMasahiro Yamada 52b9a66b63SMasahiro Yamada return 0; 53b9a66b63SMasahiro Yamada } 54b9a66b63SMasahiro Yamada 55b9a66b63SMasahiro Yamada static int uniphier_gpio_direction_output(struct udevice *dev, unsigned offset, 56b9a66b63SMasahiro Yamada int value) 57b9a66b63SMasahiro Yamada { 58b9a66b63SMasahiro Yamada uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_REG_DATA, value); 59b9a66b63SMasahiro Yamada uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_REG_DIR, 0); 60b9a66b63SMasahiro Yamada 61b9a66b63SMasahiro Yamada return 0; 62b9a66b63SMasahiro Yamada } 63b9a66b63SMasahiro Yamada 64b9a66b63SMasahiro Yamada static int uniphier_gpio_get_value(struct udevice *dev, unsigned offset) 65b9a66b63SMasahiro Yamada { 66b9a66b63SMasahiro Yamada return uniphier_gpio_offset_read(dev, offset, UNIPHIER_GPIO_REG_DATA); 67b9a66b63SMasahiro Yamada } 68b9a66b63SMasahiro Yamada 69b9a66b63SMasahiro Yamada static int uniphier_gpio_set_value(struct udevice *dev, unsigned offset, 70b9a66b63SMasahiro Yamada int value) 71b9a66b63SMasahiro Yamada { 72b9a66b63SMasahiro Yamada uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_REG_DATA, value); 73b9a66b63SMasahiro Yamada 74b9a66b63SMasahiro Yamada return 0; 75b9a66b63SMasahiro Yamada } 76b9a66b63SMasahiro Yamada 77b9a66b63SMasahiro Yamada static int uniphier_gpio_get_function(struct udevice *dev, unsigned offset) 78b9a66b63SMasahiro Yamada { 79b9a66b63SMasahiro Yamada return uniphier_gpio_offset_read(dev, offset, UNIPHIER_GPIO_REG_DIR) ? 80b9a66b63SMasahiro Yamada GPIOF_INPUT : GPIOF_OUTPUT; 81b9a66b63SMasahiro Yamada } 82b9a66b63SMasahiro Yamada 83b9a66b63SMasahiro Yamada static const struct dm_gpio_ops uniphier_gpio_ops = { 84b9a66b63SMasahiro Yamada .direction_input = uniphier_gpio_direction_input, 85b9a66b63SMasahiro Yamada .direction_output = uniphier_gpio_direction_output, 86b9a66b63SMasahiro Yamada .get_value = uniphier_gpio_get_value, 87b9a66b63SMasahiro Yamada .set_value = uniphier_gpio_set_value, 88b9a66b63SMasahiro Yamada .get_function = uniphier_gpio_get_function, 89b9a66b63SMasahiro Yamada }; 90b9a66b63SMasahiro Yamada 91b9a66b63SMasahiro Yamada static int uniphier_gpio_probe(struct udevice *dev) 92b9a66b63SMasahiro Yamada { 93b9a66b63SMasahiro Yamada struct uniphier_gpio_priv *priv = dev_get_priv(dev); 94b9a66b63SMasahiro Yamada struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); 95b9a66b63SMasahiro Yamada fdt_addr_t addr; 96b9a66b63SMasahiro Yamada unsigned int tmp; 97b9a66b63SMasahiro Yamada 98bc82a131SMasahiro Yamada addr = dev_get_addr(dev); 99b9a66b63SMasahiro Yamada if (addr == FDT_ADDR_T_NONE) 100b9a66b63SMasahiro Yamada return -EINVAL; 101b9a66b63SMasahiro Yamada 102*4e3d8406SMasahiro Yamada priv->base = devm_ioremap(dev, addr, SZ_8); 103b9a66b63SMasahiro Yamada if (!priv->base) 104b9a66b63SMasahiro Yamada return -ENOMEM; 105b9a66b63SMasahiro Yamada 106b9a66b63SMasahiro Yamada uc_priv->gpio_count = UNIPHIER_GPIO_PORTS_PER_BANK; 107b9a66b63SMasahiro Yamada 108b9a66b63SMasahiro Yamada tmp = (addr & 0xfff); 109b9a66b63SMasahiro Yamada 110b9a66b63SMasahiro Yamada /* Unfortunately, there is a register hole at offset 0x90-0x9f. */ 111b9a66b63SMasahiro Yamada if (tmp > 0x90) 112b9a66b63SMasahiro Yamada tmp -= 0x10; 113b9a66b63SMasahiro Yamada 114b9a66b63SMasahiro Yamada snprintf(priv->bank_name, sizeof(priv->bank_name) - 1, 115b9a66b63SMasahiro Yamada "port%d-", (tmp - 8) / 8); 116b9a66b63SMasahiro Yamada 117b9a66b63SMasahiro Yamada uc_priv->bank_name = priv->bank_name; 118b9a66b63SMasahiro Yamada 119b9a66b63SMasahiro Yamada return 0; 120b9a66b63SMasahiro Yamada } 121b9a66b63SMasahiro Yamada 122b9a66b63SMasahiro Yamada /* .data = the number of GPIO banks */ 123b9a66b63SMasahiro Yamada static const struct udevice_id uniphier_gpio_match[] = { 124b9a66b63SMasahiro Yamada { .compatible = "socionext,uniphier-gpio" }, 125b9a66b63SMasahiro Yamada { /* sentinel */ } 126b9a66b63SMasahiro Yamada }; 127b9a66b63SMasahiro Yamada 128b9a66b63SMasahiro Yamada U_BOOT_DRIVER(uniphier_gpio) = { 129b9a66b63SMasahiro Yamada .name = "uniphier_gpio", 130b9a66b63SMasahiro Yamada .id = UCLASS_GPIO, 131b9a66b63SMasahiro Yamada .of_match = uniphier_gpio_match, 132b9a66b63SMasahiro Yamada .probe = uniphier_gpio_probe, 133b9a66b63SMasahiro Yamada .priv_auto_alloc_size = sizeof(struct uniphier_gpio_priv), 134b9a66b63SMasahiro Yamada .ops = &uniphier_gpio_ops, 135b9a66b63SMasahiro Yamada }; 136