Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 2 | /* |
Masahiro Yamada | c5fb1c2 | 2017-10-13 19:21:51 +0900 | [diff] [blame] | 3 | * Copyright (C) 2016-2017 Socionext Inc. |
Masahiro Yamada | 4e3d840 | 2016-07-19 21:56:13 +0900 | [diff] [blame] | 4 | * Author: Masahiro Yamada <yamada.masahiro@socionext.com> |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | 9d92245 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 8 | #include <dm.h> |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 9 | #include <linux/bitops.h> |
| 10 | #include <linux/io.h> |
Masahiro Yamada | bc82a13 | 2016-03-24 22:32:41 +0900 | [diff] [blame] | 11 | #include <linux/sizes.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 12 | #include <linux/errno.h> |
Masahiro Yamada | c5fb1c2 | 2017-10-13 19:21:51 +0900 | [diff] [blame] | 13 | #include <asm/global_data.h> |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 14 | #include <asm/gpio.h> |
Masahiro Yamada | e9986a4 | 2017-11-25 00:25:34 +0900 | [diff] [blame] | 15 | #include <dt-bindings/gpio/uniphier-gpio.h> |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 16 | |
Masahiro Yamada | c5fb1c2 | 2017-10-13 19:21:51 +0900 | [diff] [blame] | 17 | #define UNIPHIER_GPIO_PORT_DATA 0x0 /* data */ |
| 18 | #define UNIPHIER_GPIO_PORT_DIR 0x4 /* direction (1:in, 0:out) */ |
| 19 | #define UNIPHIER_GPIO_IRQ_EN 0x90 /* irq enable */ |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 20 | |
| 21 | struct uniphier_gpio_priv { |
Masahiro Yamada | c5fb1c2 | 2017-10-13 19:21:51 +0900 | [diff] [blame] | 22 | void __iomem *regs; |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 23 | }; |
| 24 | |
Masahiro Yamada | c5fb1c2 | 2017-10-13 19:21:51 +0900 | [diff] [blame] | 25 | static unsigned int uniphier_gpio_bank_to_reg(unsigned int bank) |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 26 | { |
Masahiro Yamada | c5fb1c2 | 2017-10-13 19:21:51 +0900 | [diff] [blame] | 27 | unsigned int reg; |
| 28 | |
| 29 | reg = (bank + 1) * 8; |
| 30 | |
| 31 | /* |
| 32 | * Unfortunately, the GPIO port registers are not contiguous because |
| 33 | * offset 0x90-0x9f is used for IRQ. Add 0x10 when crossing the region. |
| 34 | */ |
| 35 | if (reg >= UNIPHIER_GPIO_IRQ_EN) |
| 36 | reg += 0x10; |
| 37 | |
| 38 | return reg; |
| 39 | } |
| 40 | |
| 41 | static void uniphier_gpio_get_bank_and_mask(unsigned int offset, |
| 42 | unsigned int *bank, u32 *mask) |
| 43 | { |
| 44 | *bank = offset / UNIPHIER_GPIO_LINES_PER_BANK; |
| 45 | *mask = BIT(offset % UNIPHIER_GPIO_LINES_PER_BANK); |
| 46 | } |
| 47 | |
| 48 | static void uniphier_gpio_reg_update(struct uniphier_gpio_priv *priv, |
| 49 | unsigned int reg, u32 mask, u32 val) |
| 50 | { |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 51 | u32 tmp; |
| 52 | |
Masahiro Yamada | c5fb1c2 | 2017-10-13 19:21:51 +0900 | [diff] [blame] | 53 | tmp = readl(priv->regs + reg); |
| 54 | tmp &= ~mask; |
| 55 | tmp |= mask & val; |
| 56 | writel(tmp, priv->regs + reg); |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 57 | } |
| 58 | |
Masahiro Yamada | c5fb1c2 | 2017-10-13 19:21:51 +0900 | [diff] [blame] | 59 | static void uniphier_gpio_bank_write(struct udevice *dev, unsigned int bank, |
| 60 | unsigned int reg, u32 mask, u32 val) |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 61 | { |
| 62 | struct uniphier_gpio_priv *priv = dev_get_priv(dev); |
| 63 | |
Masahiro Yamada | c5fb1c2 | 2017-10-13 19:21:51 +0900 | [diff] [blame] | 64 | if (!mask) |
| 65 | return; |
| 66 | |
| 67 | uniphier_gpio_reg_update(priv, uniphier_gpio_bank_to_reg(bank) + reg, |
| 68 | mask, val); |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 69 | } |
| 70 | |
Masahiro Yamada | c5fb1c2 | 2017-10-13 19:21:51 +0900 | [diff] [blame] | 71 | static void uniphier_gpio_offset_write(struct udevice *dev, unsigned int offset, |
| 72 | unsigned int reg, int val) |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 73 | { |
Masahiro Yamada | c5fb1c2 | 2017-10-13 19:21:51 +0900 | [diff] [blame] | 74 | unsigned int bank; |
| 75 | u32 mask; |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 76 | |
Masahiro Yamada | c5fb1c2 | 2017-10-13 19:21:51 +0900 | [diff] [blame] | 77 | uniphier_gpio_get_bank_and_mask(offset, &bank, &mask); |
| 78 | |
| 79 | uniphier_gpio_bank_write(dev, bank, reg, mask, val ? mask : 0); |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 80 | } |
| 81 | |
Masahiro Yamada | c5fb1c2 | 2017-10-13 19:21:51 +0900 | [diff] [blame] | 82 | static int uniphier_gpio_offset_read(struct udevice *dev, |
| 83 | unsigned int offset, unsigned int reg) |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 84 | { |
Masahiro Yamada | c5fb1c2 | 2017-10-13 19:21:51 +0900 | [diff] [blame] | 85 | struct uniphier_gpio_priv *priv = dev_get_priv(dev); |
| 86 | unsigned int bank, reg_offset; |
| 87 | u32 mask; |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 88 | |
Masahiro Yamada | c5fb1c2 | 2017-10-13 19:21:51 +0900 | [diff] [blame] | 89 | uniphier_gpio_get_bank_and_mask(offset, &bank, &mask); |
| 90 | reg_offset = uniphier_gpio_bank_to_reg(bank) + reg; |
| 91 | |
| 92 | return !!(readl(priv->regs + reg_offset) & mask); |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 93 | } |
| 94 | |
Masahiro Yamada | c5fb1c2 | 2017-10-13 19:21:51 +0900 | [diff] [blame] | 95 | static int uniphier_gpio_get_function(struct udevice *dev, unsigned int offset) |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 96 | { |
Masahiro Yamada | c5fb1c2 | 2017-10-13 19:21:51 +0900 | [diff] [blame] | 97 | return uniphier_gpio_offset_read(dev, offset, UNIPHIER_GPIO_PORT_DIR) ? |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 98 | GPIOF_INPUT : GPIOF_OUTPUT; |
| 99 | } |
| 100 | |
Masahiro Yamada | c5fb1c2 | 2017-10-13 19:21:51 +0900 | [diff] [blame] | 101 | static int uniphier_gpio_direction_input(struct udevice *dev, |
| 102 | unsigned int offset) |
| 103 | { |
| 104 | uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_PORT_DIR, 1); |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static int uniphier_gpio_direction_output(struct udevice *dev, |
| 110 | unsigned int offset, int value) |
| 111 | { |
| 112 | uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_PORT_DATA, value); |
| 113 | uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_PORT_DIR, 0); |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static int uniphier_gpio_get_value(struct udevice *dev, unsigned int offset) |
| 119 | { |
| 120 | return uniphier_gpio_offset_read(dev, offset, UNIPHIER_GPIO_PORT_DATA); |
| 121 | } |
| 122 | |
| 123 | static int uniphier_gpio_set_value(struct udevice *dev, |
| 124 | unsigned int offset, int value) |
| 125 | { |
| 126 | uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_PORT_DATA, value); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 131 | static const struct dm_gpio_ops uniphier_gpio_ops = { |
| 132 | .direction_input = uniphier_gpio_direction_input, |
| 133 | .direction_output = uniphier_gpio_direction_output, |
| 134 | .get_value = uniphier_gpio_get_value, |
| 135 | .set_value = uniphier_gpio_set_value, |
| 136 | .get_function = uniphier_gpio_get_function, |
| 137 | }; |
| 138 | |
| 139 | static int uniphier_gpio_probe(struct udevice *dev) |
| 140 | { |
| 141 | struct uniphier_gpio_priv *priv = dev_get_priv(dev); |
| 142 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 143 | fdt_addr_t addr; |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 144 | |
Masahiro Yamada | 2548493 | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 145 | addr = dev_read_addr(dev); |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 146 | if (addr == FDT_ADDR_T_NONE) |
| 147 | return -EINVAL; |
| 148 | |
Masahiro Yamada | c5fb1c2 | 2017-10-13 19:21:51 +0900 | [diff] [blame] | 149 | priv->regs = devm_ioremap(dev, addr, SZ_512); |
| 150 | if (!priv->regs) |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 151 | return -ENOMEM; |
| 152 | |
Masahiro Yamada | c5fb1c2 | 2017-10-13 19:21:51 +0900 | [diff] [blame] | 153 | uc_priv->gpio_count = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev), |
| 154 | "ngpios", 0); |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 159 | static const struct udevice_id uniphier_gpio_match[] = { |
| 160 | { .compatible = "socionext,uniphier-gpio" }, |
| 161 | { /* sentinel */ } |
| 162 | }; |
| 163 | |
| 164 | U_BOOT_DRIVER(uniphier_gpio) = { |
Masahiro Yamada | c5fb1c2 | 2017-10-13 19:21:51 +0900 | [diff] [blame] | 165 | .name = "uniphier-gpio", |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 166 | .id = UCLASS_GPIO, |
| 167 | .of_match = uniphier_gpio_match, |
| 168 | .probe = uniphier_gpio_probe, |
Masahiro Yamada | b9a66b6 | 2016-02-16 17:03:48 +0900 | [diff] [blame] | 169 | .priv_auto_alloc_size = sizeof(struct uniphier_gpio_priv), |
| 170 | .ops = &uniphier_gpio_ops, |
| 171 | }; |