blob: 2e901ac5c7343571b30ebdb2ef7d31640794afb3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass8fa4d5a2015-08-30 16:55:27 -06002/*
3 * (C) Copyright 2015 Google, Inc
4 *
5 * (C) Copyright 2008-2014 Rockchip Electronics
6 * Peter, Software Engineering, <superpeter.cai@gmail.com>.
Simon Glass8fa4d5a2015-08-30 16:55:27 -06007 */
8
9#include <common.h>
10#include <dm.h>
Simon Glass9c73e742016-01-21 19:44:09 -070011#include <syscon.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090012#include <linux/errno.h>
Simon Glass8fa4d5a2015-08-30 16:55:27 -060013#include <asm/gpio.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080014#include <asm/arch-rockchip/clock.h>
Jonas Karlmanc8078412023-03-19 18:39:51 +000015#include <asm/arch-rockchip/hardware.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080016#include <asm/arch-rockchip/gpio.h>
Simon Glass9c73e742016-01-21 19:44:09 -070017#include <dm/pinctrl.h>
Jonas Karlmanc8078412023-03-19 18:39:51 +000018#include <dm/read.h>
19#include <dt-bindings/pinctrl/rockchip.h>
20
21#define SWPORT_DR 0x0000
22#define SWPORT_DDR 0x0004
23#define EXT_PORT 0x0050
24#define SWPORT_DR_L 0x0000
25#define SWPORT_DR_H 0x0004
26#define SWPORT_DDR_L 0x0008
27#define SWPORT_DDR_H 0x000C
28#define EXT_PORT_V2 0x0070
29#define VER_ID_V2 0x0078
Simon Glass8fa4d5a2015-08-30 16:55:27 -060030
31enum {
32 ROCKCHIP_GPIOS_PER_BANK = 32,
33};
34
Simon Glass8fa4d5a2015-08-30 16:55:27 -060035struct rockchip_gpio_priv {
Jonas Karlmanc8078412023-03-19 18:39:51 +000036 void __iomem *regs;
Simon Glass9c73e742016-01-21 19:44:09 -070037 struct udevice *pinctrl;
38 int bank;
Simon Glass8fa4d5a2015-08-30 16:55:27 -060039 char name[2];
Jonas Karlmanc8078412023-03-19 18:39:51 +000040 u32 version;
Simon Glass8fa4d5a2015-08-30 16:55:27 -060041};
42
Jonas Karlmanc8078412023-03-19 18:39:51 +000043static int rockchip_gpio_get_value(struct udevice *dev, unsigned offset)
Simon Glass8fa4d5a2015-08-30 16:55:27 -060044{
45 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
Jonas Karlmanc8078412023-03-19 18:39:51 +000046 u32 mask = BIT(offset), data;
Simon Glass8fa4d5a2015-08-30 16:55:27 -060047
Jonas Karlmanc8078412023-03-19 18:39:51 +000048 if (priv->version)
49 data = readl(priv->regs + EXT_PORT_V2);
50 else
51 data = readl(priv->regs + EXT_PORT);
Simon Glass8fa4d5a2015-08-30 16:55:27 -060052
Jonas Karlmanc8078412023-03-19 18:39:51 +000053 return (data & mask) ? 1 : 0;
Simon Glass8fa4d5a2015-08-30 16:55:27 -060054}
55
Jonas Karlmanc8078412023-03-19 18:39:51 +000056static int rockchip_gpio_set_value(struct udevice *dev, unsigned offset,
57 int value)
Simon Glass8fa4d5a2015-08-30 16:55:27 -060058{
59 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
Jonas Karlmanc8078412023-03-19 18:39:51 +000060 u32 mask = BIT(offset), data = value ? mask : 0;
Simon Glass8fa4d5a2015-08-30 16:55:27 -060061
Jonas Karlmanc8078412023-03-19 18:39:51 +000062 if (priv->version && offset >= 16)
63 rk_clrsetreg(priv->regs + SWPORT_DR_H, mask >> 16, data >> 16);
64 else if (priv->version)
65 rk_clrsetreg(priv->regs + SWPORT_DR_L, mask, data);
66 else
67 clrsetbits_le32(priv->regs + SWPORT_DR, mask, data);
Simon Glass8fa4d5a2015-08-30 16:55:27 -060068
69 return 0;
70}
71
Jonas Karlmanc8078412023-03-19 18:39:51 +000072static int rockchip_gpio_direction_input(struct udevice *dev, unsigned offset)
Simon Glass8fa4d5a2015-08-30 16:55:27 -060073{
74 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
Jonas Karlmanc8078412023-03-19 18:39:51 +000075 u32 mask = BIT(offset);
76
77 if (priv->version && offset >= 16)
78 rk_clrreg(priv->regs + SWPORT_DDR_H, mask >> 16);
79 else if (priv->version)
80 rk_clrreg(priv->regs + SWPORT_DDR_L, mask);
81 else
82 clrbits_le32(priv->regs + SWPORT_DDR, mask);
Simon Glass8fa4d5a2015-08-30 16:55:27 -060083
Jonas Karlmanc8078412023-03-19 18:39:51 +000084 return 0;
Simon Glass8fa4d5a2015-08-30 16:55:27 -060085}
86
Jonas Karlmanc8078412023-03-19 18:39:51 +000087static int rockchip_gpio_direction_output(struct udevice *dev, unsigned offset,
88 int value)
Simon Glass8fa4d5a2015-08-30 16:55:27 -060089{
90 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
Jonas Karlmanc8078412023-03-19 18:39:51 +000091 u32 mask = BIT(offset);
92
93 rockchip_gpio_set_value(dev, offset, value);
Simon Glass8fa4d5a2015-08-30 16:55:27 -060094
Jonas Karlmanc8078412023-03-19 18:39:51 +000095 if (priv->version && offset >= 16)
96 rk_setreg(priv->regs + SWPORT_DDR_H, mask >> 16);
97 else if (priv->version)
98 rk_setreg(priv->regs + SWPORT_DDR_L, mask);
99 else
100 setbits_le32(priv->regs + SWPORT_DDR, mask);
Simon Glass8fa4d5a2015-08-30 16:55:27 -0600101
102 return 0;
103}
104
105static int rockchip_gpio_get_function(struct udevice *dev, unsigned offset)
106{
Simon Glass9c73e742016-01-21 19:44:09 -0700107 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
Jonas Karlmanc8078412023-03-19 18:39:51 +0000108 u32 mask = BIT(offset), data;
Simon Glass9c73e742016-01-21 19:44:09 -0700109 int ret;
110
Jonas Karlmanc8078412023-03-19 18:39:51 +0000111 if (CONFIG_IS_ENABLED(PINCTRL)) {
112 ret = pinctrl_get_gpio_mux(priv->pinctrl, priv->bank, offset);
113 if (ret < 0)
114 return ret;
115 else if (ret != RK_FUNC_GPIO)
116 return GPIOF_FUNC;
117 }
118
119 if (priv->version && offset >= 16)
120 data = readl(priv->regs + SWPORT_DDR_H) << 16;
121 else if (priv->version)
122 data = readl(priv->regs + SWPORT_DDR_L);
123 else
124 data = readl(priv->regs + SWPORT_DDR);
Simon Glass9c73e742016-01-21 19:44:09 -0700125
Jonas Karlmanc8078412023-03-19 18:39:51 +0000126 return (data & mask) ? GPIOF_OUTPUT : GPIOF_INPUT;
Simon Glass8fa4d5a2015-08-30 16:55:27 -0600127}
128
Simon Glassf642c532019-01-21 14:53:34 -0700129/* Simple SPL interface to GPIOs */
130#ifdef CONFIG_SPL_BUILD
131
132enum {
133 PULL_NONE_1V8 = 0,
134 PULL_DOWN_1V8 = 1,
135 PULL_UP_1V8 = 3,
136};
137
138int spl_gpio_set_pull(void *vregs, uint gpio, int pull)
139{
140 u32 *regs = vregs;
141 uint val;
142
143 regs += gpio >> GPIO_BANK_SHIFT;
144 gpio &= GPIO_OFFSET_MASK;
145 switch (pull) {
146 case GPIO_PULL_UP:
147 val = PULL_UP_1V8;
148 break;
149 case GPIO_PULL_DOWN:
150 val = PULL_DOWN_1V8;
151 break;
152 case GPIO_PULL_NORMAL:
153 default:
154 val = PULL_NONE_1V8;
155 break;
156 }
157 clrsetbits_le32(regs, 3 << (gpio * 2), val << (gpio * 2));
158
159 return 0;
160}
161
162int spl_gpio_output(void *vregs, uint gpio, int value)
163{
164 struct rockchip_gpio_regs * const regs = vregs;
165
166 clrsetbits_le32(&regs->swport_dr, 1 << gpio, value << gpio);
167
168 /* Set direction */
169 clrsetbits_le32(&regs->swport_ddr, 1 << gpio, 1 << gpio);
170
171 return 0;
172}
173#endif /* CONFIG_SPL_BUILD */
174
Simon Glass8fa4d5a2015-08-30 16:55:27 -0600175static int rockchip_gpio_probe(struct udevice *dev)
176{
177 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
178 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
Chris Morgan01c69eb2023-02-13 16:27:34 -0600179 struct ofnode_phandle_args args;
Simon Glass8fa4d5a2015-08-30 16:55:27 -0600180 char *end;
Simon Glass9c73e742016-01-21 19:44:09 -0700181 int ret;
Simon Glass8fa4d5a2015-08-30 16:55:27 -0600182
Philipp Tomsich66482052017-09-11 22:04:24 +0200183 priv->regs = dev_read_addr_ptr(dev);
Jonas Karlmanc8078412023-03-19 18:39:51 +0000184
185 if (CONFIG_IS_ENABLED(PINCTRL)) {
186 ret = uclass_first_device_err(UCLASS_PINCTRL, &priv->pinctrl);
187 if (ret)
188 return ret;
189 }
Simon Glass9c73e742016-01-21 19:44:09 -0700190
Chris Morgan01c69eb2023-02-13 16:27:34 -0600191 /*
192 * If "gpio-ranges" is present in the devicetree use it to parse
193 * the GPIO bank ID, otherwise use the legacy method.
194 */
195 ret = ofnode_parse_phandle_with_args(dev_ofnode(dev),
196 "gpio-ranges", NULL, 3,
197 0, &args);
198 if (!ret || ret != -ENOENT) {
199 uc_priv->gpio_count = args.args[2];
Johan Jonker7fd2fdb2023-03-19 16:02:18 +0100200 priv->bank = args.args[1] / ROCKCHIP_GPIOS_PER_BANK;
Chris Morgan01c69eb2023-02-13 16:27:34 -0600201 } else {
202 uc_priv->gpio_count = ROCKCHIP_GPIOS_PER_BANK;
Jonas Karlmanf54f8ea2024-02-17 00:22:35 +0000203 ret = dev_read_alias_seq(dev, &priv->bank);
204 if (ret) {
205 end = strrchr(dev->name, '@');
206 priv->bank = trailing_strtoln(dev->name, end);
207 }
Chris Morgan01c69eb2023-02-13 16:27:34 -0600208 }
209
Simon Glass9c73e742016-01-21 19:44:09 -0700210 priv->name[0] = 'A' + priv->bank;
Simon Glass8fa4d5a2015-08-30 16:55:27 -0600211 uc_priv->bank_name = priv->name;
212
Jonas Karlmanc8078412023-03-19 18:39:51 +0000213 priv->version = readl(priv->regs + VER_ID_V2);
214
Simon Glass8fa4d5a2015-08-30 16:55:27 -0600215 return 0;
216}
217
218static const struct dm_gpio_ops gpio_rockchip_ops = {
219 .direction_input = rockchip_gpio_direction_input,
220 .direction_output = rockchip_gpio_direction_output,
221 .get_value = rockchip_gpio_get_value,
222 .set_value = rockchip_gpio_set_value,
223 .get_function = rockchip_gpio_get_function,
Simon Glass8fa4d5a2015-08-30 16:55:27 -0600224};
225
226static const struct udevice_id rockchip_gpio_ids[] = {
227 { .compatible = "rockchip,gpio-bank" },
228 { }
229};
230
Walter Lozano2901ac62020-06-25 01:10:04 -0300231U_BOOT_DRIVER(rockchip_gpio_bank) = {
232 .name = "rockchip_gpio_bank",
Simon Glass8fa4d5a2015-08-30 16:55:27 -0600233 .id = UCLASS_GPIO,
234 .of_match = rockchip_gpio_ids,
235 .ops = &gpio_rockchip_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700236 .priv_auto = sizeof(struct rockchip_gpio_priv),
Simon Glass8fa4d5a2015-08-30 16:55:27 -0600237 .probe = rockchip_gpio_probe,
238};