Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Google, Inc |
| 4 | * |
| 5 | * (C) Copyright 2008-2014 Rockchip Electronics |
| 6 | * Peter, Software Engineering, <superpeter.cai@gmail.com>. |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 7 | */ |
| 8 | |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | 9c73e74 | 2016-01-21 19:44:09 -0700 | [diff] [blame] | 10 | #include <syscon.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 11 | #include <linux/errno.h> |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 12 | #include <asm/gpio.h> |
Kever Yang | 9fbe17c | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 13 | #include <asm/arch-rockchip/clock.h> |
Jonas Karlman | c807841 | 2023-03-19 18:39:51 +0000 | [diff] [blame] | 14 | #include <asm/arch-rockchip/hardware.h> |
Kever Yang | 9fbe17c | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 15 | #include <asm/arch-rockchip/gpio.h> |
Simon Glass | 9c73e74 | 2016-01-21 19:44:09 -0700 | [diff] [blame] | 16 | #include <dm/pinctrl.h> |
Jonas Karlman | c807841 | 2023-03-19 18:39:51 +0000 | [diff] [blame] | 17 | #include <dm/read.h> |
| 18 | #include <dt-bindings/pinctrl/rockchip.h> |
| 19 | |
| 20 | #define SWPORT_DR 0x0000 |
| 21 | #define SWPORT_DDR 0x0004 |
| 22 | #define EXT_PORT 0x0050 |
| 23 | #define SWPORT_DR_L 0x0000 |
| 24 | #define SWPORT_DR_H 0x0004 |
| 25 | #define SWPORT_DDR_L 0x0008 |
| 26 | #define SWPORT_DDR_H 0x000C |
| 27 | #define EXT_PORT_V2 0x0070 |
| 28 | #define VER_ID_V2 0x0078 |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 29 | |
| 30 | enum { |
| 31 | ROCKCHIP_GPIOS_PER_BANK = 32, |
| 32 | }; |
| 33 | |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 34 | struct rockchip_gpio_priv { |
Jonas Karlman | c807841 | 2023-03-19 18:39:51 +0000 | [diff] [blame] | 35 | void __iomem *regs; |
Simon Glass | 9c73e74 | 2016-01-21 19:44:09 -0700 | [diff] [blame] | 36 | struct udevice *pinctrl; |
| 37 | int bank; |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 38 | char name[2]; |
Jonas Karlman | c807841 | 2023-03-19 18:39:51 +0000 | [diff] [blame] | 39 | u32 version; |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 40 | }; |
| 41 | |
Jonas Karlman | c807841 | 2023-03-19 18:39:51 +0000 | [diff] [blame] | 42 | static int rockchip_gpio_get_value(struct udevice *dev, unsigned offset) |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 43 | { |
| 44 | struct rockchip_gpio_priv *priv = dev_get_priv(dev); |
Jonas Karlman | c807841 | 2023-03-19 18:39:51 +0000 | [diff] [blame] | 45 | u32 mask = BIT(offset), data; |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 46 | |
Jonas Karlman | c807841 | 2023-03-19 18:39:51 +0000 | [diff] [blame] | 47 | if (priv->version) |
| 48 | data = readl(priv->regs + EXT_PORT_V2); |
| 49 | else |
| 50 | data = readl(priv->regs + EXT_PORT); |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 51 | |
Jonas Karlman | c807841 | 2023-03-19 18:39:51 +0000 | [diff] [blame] | 52 | return (data & mask) ? 1 : 0; |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 53 | } |
| 54 | |
Jonas Karlman | c807841 | 2023-03-19 18:39:51 +0000 | [diff] [blame] | 55 | static int rockchip_gpio_set_value(struct udevice *dev, unsigned offset, |
| 56 | int value) |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 57 | { |
| 58 | struct rockchip_gpio_priv *priv = dev_get_priv(dev); |
Jonas Karlman | c807841 | 2023-03-19 18:39:51 +0000 | [diff] [blame] | 59 | u32 mask = BIT(offset), data = value ? mask : 0; |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 60 | |
Jonas Karlman | c807841 | 2023-03-19 18:39:51 +0000 | [diff] [blame] | 61 | if (priv->version && offset >= 16) |
| 62 | rk_clrsetreg(priv->regs + SWPORT_DR_H, mask >> 16, data >> 16); |
| 63 | else if (priv->version) |
| 64 | rk_clrsetreg(priv->regs + SWPORT_DR_L, mask, data); |
| 65 | else |
| 66 | clrsetbits_le32(priv->regs + SWPORT_DR, mask, data); |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
Jonas Karlman | c807841 | 2023-03-19 18:39:51 +0000 | [diff] [blame] | 71 | static int rockchip_gpio_direction_input(struct udevice *dev, unsigned offset) |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 72 | { |
| 73 | struct rockchip_gpio_priv *priv = dev_get_priv(dev); |
Jonas Karlman | c807841 | 2023-03-19 18:39:51 +0000 | [diff] [blame] | 74 | u32 mask = BIT(offset); |
| 75 | |
| 76 | if (priv->version && offset >= 16) |
| 77 | rk_clrreg(priv->regs + SWPORT_DDR_H, mask >> 16); |
| 78 | else if (priv->version) |
| 79 | rk_clrreg(priv->regs + SWPORT_DDR_L, mask); |
| 80 | else |
| 81 | clrbits_le32(priv->regs + SWPORT_DDR, mask); |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 82 | |
Jonas Karlman | c807841 | 2023-03-19 18:39:51 +0000 | [diff] [blame] | 83 | return 0; |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 84 | } |
| 85 | |
Jonas Karlman | c807841 | 2023-03-19 18:39:51 +0000 | [diff] [blame] | 86 | static int rockchip_gpio_direction_output(struct udevice *dev, unsigned offset, |
| 87 | int value) |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 88 | { |
| 89 | struct rockchip_gpio_priv *priv = dev_get_priv(dev); |
Jonas Karlman | c807841 | 2023-03-19 18:39:51 +0000 | [diff] [blame] | 90 | u32 mask = BIT(offset); |
| 91 | |
| 92 | rockchip_gpio_set_value(dev, offset, value); |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 93 | |
Jonas Karlman | c807841 | 2023-03-19 18:39:51 +0000 | [diff] [blame] | 94 | if (priv->version && offset >= 16) |
| 95 | rk_setreg(priv->regs + SWPORT_DDR_H, mask >> 16); |
| 96 | else if (priv->version) |
| 97 | rk_setreg(priv->regs + SWPORT_DDR_L, mask); |
| 98 | else |
| 99 | setbits_le32(priv->regs + SWPORT_DDR, mask); |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static int rockchip_gpio_get_function(struct udevice *dev, unsigned offset) |
| 105 | { |
Simon Glass | 9c73e74 | 2016-01-21 19:44:09 -0700 | [diff] [blame] | 106 | struct rockchip_gpio_priv *priv = dev_get_priv(dev); |
Jonas Karlman | c807841 | 2023-03-19 18:39:51 +0000 | [diff] [blame] | 107 | u32 mask = BIT(offset), data; |
Simon Glass | 9c73e74 | 2016-01-21 19:44:09 -0700 | [diff] [blame] | 108 | int ret; |
| 109 | |
Jonas Karlman | c807841 | 2023-03-19 18:39:51 +0000 | [diff] [blame] | 110 | if (CONFIG_IS_ENABLED(PINCTRL)) { |
| 111 | ret = pinctrl_get_gpio_mux(priv->pinctrl, priv->bank, offset); |
| 112 | if (ret < 0) |
| 113 | return ret; |
| 114 | else if (ret != RK_FUNC_GPIO) |
| 115 | return GPIOF_FUNC; |
| 116 | } |
| 117 | |
| 118 | if (priv->version && offset >= 16) |
| 119 | data = readl(priv->regs + SWPORT_DDR_H) << 16; |
| 120 | else if (priv->version) |
| 121 | data = readl(priv->regs + SWPORT_DDR_L); |
| 122 | else |
| 123 | data = readl(priv->regs + SWPORT_DDR); |
Simon Glass | 9c73e74 | 2016-01-21 19:44:09 -0700 | [diff] [blame] | 124 | |
Jonas Karlman | c807841 | 2023-03-19 18:39:51 +0000 | [diff] [blame] | 125 | return (data & mask) ? GPIOF_OUTPUT : GPIOF_INPUT; |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 126 | } |
| 127 | |
Simon Glass | f642c53 | 2019-01-21 14:53:34 -0700 | [diff] [blame] | 128 | /* Simple SPL interface to GPIOs */ |
| 129 | #ifdef CONFIG_SPL_BUILD |
| 130 | |
| 131 | enum { |
| 132 | PULL_NONE_1V8 = 0, |
| 133 | PULL_DOWN_1V8 = 1, |
| 134 | PULL_UP_1V8 = 3, |
| 135 | }; |
| 136 | |
| 137 | int spl_gpio_set_pull(void *vregs, uint gpio, int pull) |
| 138 | { |
| 139 | u32 *regs = vregs; |
| 140 | uint val; |
| 141 | |
| 142 | regs += gpio >> GPIO_BANK_SHIFT; |
| 143 | gpio &= GPIO_OFFSET_MASK; |
| 144 | switch (pull) { |
| 145 | case GPIO_PULL_UP: |
| 146 | val = PULL_UP_1V8; |
| 147 | break; |
| 148 | case GPIO_PULL_DOWN: |
| 149 | val = PULL_DOWN_1V8; |
| 150 | break; |
| 151 | case GPIO_PULL_NORMAL: |
| 152 | default: |
| 153 | val = PULL_NONE_1V8; |
| 154 | break; |
| 155 | } |
| 156 | clrsetbits_le32(regs, 3 << (gpio * 2), val << (gpio * 2)); |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | int spl_gpio_output(void *vregs, uint gpio, int value) |
| 162 | { |
| 163 | struct rockchip_gpio_regs * const regs = vregs; |
| 164 | |
| 165 | clrsetbits_le32(®s->swport_dr, 1 << gpio, value << gpio); |
| 166 | |
| 167 | /* Set direction */ |
| 168 | clrsetbits_le32(®s->swport_ddr, 1 << gpio, 1 << gpio); |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | #endif /* CONFIG_SPL_BUILD */ |
| 173 | |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 174 | static int rockchip_gpio_probe(struct udevice *dev) |
| 175 | { |
| 176 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 177 | struct rockchip_gpio_priv *priv = dev_get_priv(dev); |
Chris Morgan | 01c69eb | 2023-02-13 16:27:34 -0600 | [diff] [blame] | 178 | struct ofnode_phandle_args args; |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 179 | char *end; |
Simon Glass | 9c73e74 | 2016-01-21 19:44:09 -0700 | [diff] [blame] | 180 | int ret; |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 181 | |
Philipp Tomsich | 6648205 | 2017-09-11 22:04:24 +0200 | [diff] [blame] | 182 | priv->regs = dev_read_addr_ptr(dev); |
Jonas Karlman | c807841 | 2023-03-19 18:39:51 +0000 | [diff] [blame] | 183 | |
| 184 | if (CONFIG_IS_ENABLED(PINCTRL)) { |
| 185 | ret = uclass_first_device_err(UCLASS_PINCTRL, &priv->pinctrl); |
| 186 | if (ret) |
| 187 | return ret; |
| 188 | } |
Simon Glass | 9c73e74 | 2016-01-21 19:44:09 -0700 | [diff] [blame] | 189 | |
Chris Morgan | 01c69eb | 2023-02-13 16:27:34 -0600 | [diff] [blame] | 190 | /* |
| 191 | * If "gpio-ranges" is present in the devicetree use it to parse |
| 192 | * the GPIO bank ID, otherwise use the legacy method. |
| 193 | */ |
| 194 | ret = ofnode_parse_phandle_with_args(dev_ofnode(dev), |
| 195 | "gpio-ranges", NULL, 3, |
| 196 | 0, &args); |
| 197 | if (!ret || ret != -ENOENT) { |
| 198 | uc_priv->gpio_count = args.args[2]; |
Johan Jonker | 7fd2fdb | 2023-03-19 16:02:18 +0100 | [diff] [blame] | 199 | priv->bank = args.args[1] / ROCKCHIP_GPIOS_PER_BANK; |
Chris Morgan | 01c69eb | 2023-02-13 16:27:34 -0600 | [diff] [blame] | 200 | } else { |
| 201 | uc_priv->gpio_count = ROCKCHIP_GPIOS_PER_BANK; |
Jonas Karlman | f54f8ea | 2024-02-17 00:22:35 +0000 | [diff] [blame] | 202 | ret = dev_read_alias_seq(dev, &priv->bank); |
| 203 | if (ret) { |
| 204 | end = strrchr(dev->name, '@'); |
| 205 | priv->bank = trailing_strtoln(dev->name, end); |
| 206 | } |
Chris Morgan | 01c69eb | 2023-02-13 16:27:34 -0600 | [diff] [blame] | 207 | } |
| 208 | |
Simon Glass | 9c73e74 | 2016-01-21 19:44:09 -0700 | [diff] [blame] | 209 | priv->name[0] = 'A' + priv->bank; |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 210 | uc_priv->bank_name = priv->name; |
| 211 | |
Jonas Karlman | c807841 | 2023-03-19 18:39:51 +0000 | [diff] [blame] | 212 | priv->version = readl(priv->regs + VER_ID_V2); |
| 213 | |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static const struct dm_gpio_ops gpio_rockchip_ops = { |
| 218 | .direction_input = rockchip_gpio_direction_input, |
| 219 | .direction_output = rockchip_gpio_direction_output, |
| 220 | .get_value = rockchip_gpio_get_value, |
| 221 | .set_value = rockchip_gpio_set_value, |
| 222 | .get_function = rockchip_gpio_get_function, |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | static const struct udevice_id rockchip_gpio_ids[] = { |
| 226 | { .compatible = "rockchip,gpio-bank" }, |
| 227 | { } |
| 228 | }; |
| 229 | |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 230 | U_BOOT_DRIVER(rockchip_gpio_bank) = { |
| 231 | .name = "rockchip_gpio_bank", |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 232 | .id = UCLASS_GPIO, |
| 233 | .of_match = rockchip_gpio_ids, |
| 234 | .ops = &gpio_rockchip_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 235 | .priv_auto = sizeof(struct rockchip_gpio_priv), |
Simon Glass | 8fa4d5a | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 236 | .probe = rockchip_gpio_probe, |
| 237 | }; |