2018-05-07 05:58:06 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2015-08-31 06:55:27 +08:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2015 Google, Inc
|
|
|
|
*
|
|
|
|
* (C) Copyright 2008-2014 Rockchip Electronics
|
|
|
|
* Peter, Software Engineering, <superpeter.cai@gmail.com>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
2016-01-22 10:44:09 +08:00
|
|
|
#include <syscon.h>
|
2016-09-21 10:28:55 +08:00
|
|
|
#include <linux/errno.h>
|
2015-08-31 06:55:27 +08:00
|
|
|
#include <asm/gpio.h>
|
2019-03-28 11:01:23 +08:00
|
|
|
#include <asm/arch-rockchip/clock.h>
|
2023-03-20 02:39:51 +08:00
|
|
|
#include <asm/arch-rockchip/hardware.h>
|
2019-03-28 11:01:23 +08:00
|
|
|
#include <asm/arch-rockchip/gpio.h>
|
2016-01-22 10:44:09 +08:00
|
|
|
#include <dm/pinctrl.h>
|
2023-03-20 02:39:51 +08:00
|
|
|
#include <dm/read.h>
|
|
|
|
#include <dt-bindings/pinctrl/rockchip.h>
|
|
|
|
|
|
|
|
#define SWPORT_DR 0x0000
|
|
|
|
#define SWPORT_DDR 0x0004
|
|
|
|
#define EXT_PORT 0x0050
|
|
|
|
#define SWPORT_DR_L 0x0000
|
|
|
|
#define SWPORT_DR_H 0x0004
|
|
|
|
#define SWPORT_DDR_L 0x0008
|
|
|
|
#define SWPORT_DDR_H 0x000C
|
|
|
|
#define EXT_PORT_V2 0x0070
|
|
|
|
#define VER_ID_V2 0x0078
|
2015-08-31 06:55:27 +08:00
|
|
|
|
|
|
|
enum {
|
|
|
|
ROCKCHIP_GPIOS_PER_BANK = 32,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct rockchip_gpio_priv {
|
2023-03-20 02:39:51 +08:00
|
|
|
void __iomem *regs;
|
2016-01-22 10:44:09 +08:00
|
|
|
struct udevice *pinctrl;
|
|
|
|
int bank;
|
2015-08-31 06:55:27 +08:00
|
|
|
char name[2];
|
2023-03-20 02:39:51 +08:00
|
|
|
u32 version;
|
2015-08-31 06:55:27 +08:00
|
|
|
};
|
|
|
|
|
2023-03-20 02:39:51 +08:00
|
|
|
static int rockchip_gpio_get_value(struct udevice *dev, unsigned offset)
|
2015-08-31 06:55:27 +08:00
|
|
|
{
|
|
|
|
struct rockchip_gpio_priv *priv = dev_get_priv(dev);
|
2023-03-20 02:39:51 +08:00
|
|
|
u32 mask = BIT(offset), data;
|
2015-08-31 06:55:27 +08:00
|
|
|
|
2023-03-20 02:39:51 +08:00
|
|
|
if (priv->version)
|
|
|
|
data = readl(priv->regs + EXT_PORT_V2);
|
|
|
|
else
|
|
|
|
data = readl(priv->regs + EXT_PORT);
|
2015-08-31 06:55:27 +08:00
|
|
|
|
2023-03-20 02:39:51 +08:00
|
|
|
return (data & mask) ? 1 : 0;
|
2015-08-31 06:55:27 +08:00
|
|
|
}
|
|
|
|
|
2023-03-20 02:39:51 +08:00
|
|
|
static int rockchip_gpio_set_value(struct udevice *dev, unsigned offset,
|
|
|
|
int value)
|
2015-08-31 06:55:27 +08:00
|
|
|
{
|
|
|
|
struct rockchip_gpio_priv *priv = dev_get_priv(dev);
|
2023-03-20 02:39:51 +08:00
|
|
|
u32 mask = BIT(offset), data = value ? mask : 0;
|
2015-08-31 06:55:27 +08:00
|
|
|
|
2023-03-20 02:39:51 +08:00
|
|
|
if (priv->version && offset >= 16)
|
|
|
|
rk_clrsetreg(priv->regs + SWPORT_DR_H, mask >> 16, data >> 16);
|
|
|
|
else if (priv->version)
|
|
|
|
rk_clrsetreg(priv->regs + SWPORT_DR_L, mask, data);
|
|
|
|
else
|
|
|
|
clrsetbits_le32(priv->regs + SWPORT_DR, mask, data);
|
2015-08-31 06:55:27 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-20 02:39:51 +08:00
|
|
|
static int rockchip_gpio_direction_input(struct udevice *dev, unsigned offset)
|
2015-08-31 06:55:27 +08:00
|
|
|
{
|
|
|
|
struct rockchip_gpio_priv *priv = dev_get_priv(dev);
|
2023-03-20 02:39:51 +08:00
|
|
|
u32 mask = BIT(offset);
|
|
|
|
|
|
|
|
if (priv->version && offset >= 16)
|
|
|
|
rk_clrreg(priv->regs + SWPORT_DDR_H, mask >> 16);
|
|
|
|
else if (priv->version)
|
|
|
|
rk_clrreg(priv->regs + SWPORT_DDR_L, mask);
|
|
|
|
else
|
|
|
|
clrbits_le32(priv->regs + SWPORT_DDR, mask);
|
2015-08-31 06:55:27 +08:00
|
|
|
|
2023-03-20 02:39:51 +08:00
|
|
|
return 0;
|
2015-08-31 06:55:27 +08:00
|
|
|
}
|
|
|
|
|
2023-03-20 02:39:51 +08:00
|
|
|
static int rockchip_gpio_direction_output(struct udevice *dev, unsigned offset,
|
|
|
|
int value)
|
2015-08-31 06:55:27 +08:00
|
|
|
{
|
|
|
|
struct rockchip_gpio_priv *priv = dev_get_priv(dev);
|
2023-03-20 02:39:51 +08:00
|
|
|
u32 mask = BIT(offset);
|
|
|
|
|
|
|
|
rockchip_gpio_set_value(dev, offset, value);
|
2015-08-31 06:55:27 +08:00
|
|
|
|
2023-03-20 02:39:51 +08:00
|
|
|
if (priv->version && offset >= 16)
|
|
|
|
rk_setreg(priv->regs + SWPORT_DDR_H, mask >> 16);
|
|
|
|
else if (priv->version)
|
|
|
|
rk_setreg(priv->regs + SWPORT_DDR_L, mask);
|
|
|
|
else
|
|
|
|
setbits_le32(priv->regs + SWPORT_DDR, mask);
|
2015-08-31 06:55:27 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rockchip_gpio_get_function(struct udevice *dev, unsigned offset)
|
|
|
|
{
|
2016-01-22 10:44:09 +08:00
|
|
|
struct rockchip_gpio_priv *priv = dev_get_priv(dev);
|
2023-03-20 02:39:51 +08:00
|
|
|
u32 mask = BIT(offset), data;
|
2016-01-22 10:44:09 +08:00
|
|
|
int ret;
|
|
|
|
|
2023-03-20 02:39:51 +08:00
|
|
|
if (CONFIG_IS_ENABLED(PINCTRL)) {
|
|
|
|
ret = pinctrl_get_gpio_mux(priv->pinctrl, priv->bank, offset);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
else if (ret != RK_FUNC_GPIO)
|
|
|
|
return GPIOF_FUNC;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->version && offset >= 16)
|
|
|
|
data = readl(priv->regs + SWPORT_DDR_H) << 16;
|
|
|
|
else if (priv->version)
|
|
|
|
data = readl(priv->regs + SWPORT_DDR_L);
|
|
|
|
else
|
|
|
|
data = readl(priv->regs + SWPORT_DDR);
|
2016-01-22 10:44:09 +08:00
|
|
|
|
2023-03-20 02:39:51 +08:00
|
|
|
return (data & mask) ? GPIOF_OUTPUT : GPIOF_INPUT;
|
2015-08-31 06:55:27 +08:00
|
|
|
}
|
|
|
|
|
2019-01-22 05:53:34 +08:00
|
|
|
/* Simple SPL interface to GPIOs */
|
|
|
|
#ifdef CONFIG_SPL_BUILD
|
|
|
|
|
|
|
|
enum {
|
|
|
|
PULL_NONE_1V8 = 0,
|
|
|
|
PULL_DOWN_1V8 = 1,
|
|
|
|
PULL_UP_1V8 = 3,
|
|
|
|
};
|
|
|
|
|
|
|
|
int spl_gpio_set_pull(void *vregs, uint gpio, int pull)
|
|
|
|
{
|
|
|
|
u32 *regs = vregs;
|
|
|
|
uint val;
|
|
|
|
|
|
|
|
regs += gpio >> GPIO_BANK_SHIFT;
|
|
|
|
gpio &= GPIO_OFFSET_MASK;
|
|
|
|
switch (pull) {
|
|
|
|
case GPIO_PULL_UP:
|
|
|
|
val = PULL_UP_1V8;
|
|
|
|
break;
|
|
|
|
case GPIO_PULL_DOWN:
|
|
|
|
val = PULL_DOWN_1V8;
|
|
|
|
break;
|
|
|
|
case GPIO_PULL_NORMAL:
|
|
|
|
default:
|
|
|
|
val = PULL_NONE_1V8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
clrsetbits_le32(regs, 3 << (gpio * 2), val << (gpio * 2));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int spl_gpio_output(void *vregs, uint gpio, int value)
|
|
|
|
{
|
|
|
|
struct rockchip_gpio_regs * const regs = vregs;
|
|
|
|
|
|
|
|
clrsetbits_le32(®s->swport_dr, 1 << gpio, value << gpio);
|
|
|
|
|
|
|
|
/* Set direction */
|
|
|
|
clrsetbits_le32(®s->swport_ddr, 1 << gpio, 1 << gpio);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_SPL_BUILD */
|
|
|
|
|
2015-08-31 06:55:27 +08:00
|
|
|
static int rockchip_gpio_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
|
|
|
|
struct rockchip_gpio_priv *priv = dev_get_priv(dev);
|
2023-02-14 06:27:34 +08:00
|
|
|
struct ofnode_phandle_args args;
|
2015-08-31 06:55:27 +08:00
|
|
|
char *end;
|
2016-01-22 10:44:09 +08:00
|
|
|
int ret;
|
2015-08-31 06:55:27 +08:00
|
|
|
|
2017-09-12 04:04:24 +08:00
|
|
|
priv->regs = dev_read_addr_ptr(dev);
|
2023-03-20 02:39:51 +08:00
|
|
|
|
|
|
|
if (CONFIG_IS_ENABLED(PINCTRL)) {
|
|
|
|
ret = uclass_first_device_err(UCLASS_PINCTRL, &priv->pinctrl);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2016-01-22 10:44:09 +08:00
|
|
|
|
2023-02-14 06:27:34 +08:00
|
|
|
/*
|
|
|
|
* If "gpio-ranges" is present in the devicetree use it to parse
|
|
|
|
* the GPIO bank ID, otherwise use the legacy method.
|
|
|
|
*/
|
|
|
|
ret = ofnode_parse_phandle_with_args(dev_ofnode(dev),
|
|
|
|
"gpio-ranges", NULL, 3,
|
|
|
|
0, &args);
|
|
|
|
if (!ret || ret != -ENOENT) {
|
|
|
|
uc_priv->gpio_count = args.args[2];
|
2023-03-19 23:02:18 +08:00
|
|
|
priv->bank = args.args[1] / ROCKCHIP_GPIOS_PER_BANK;
|
2023-02-14 06:27:34 +08:00
|
|
|
} else {
|
|
|
|
uc_priv->gpio_count = ROCKCHIP_GPIOS_PER_BANK;
|
2024-02-17 08:22:35 +08:00
|
|
|
ret = dev_read_alias_seq(dev, &priv->bank);
|
|
|
|
if (ret) {
|
|
|
|
end = strrchr(dev->name, '@');
|
|
|
|
priv->bank = trailing_strtoln(dev->name, end);
|
|
|
|
}
|
2023-02-14 06:27:34 +08:00
|
|
|
}
|
|
|
|
|
2016-01-22 10:44:09 +08:00
|
|
|
priv->name[0] = 'A' + priv->bank;
|
2015-08-31 06:55:27 +08:00
|
|
|
uc_priv->bank_name = priv->name;
|
|
|
|
|
2023-03-20 02:39:51 +08:00
|
|
|
priv->version = readl(priv->regs + VER_ID_V2);
|
|
|
|
|
2015-08-31 06:55:27 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dm_gpio_ops gpio_rockchip_ops = {
|
|
|
|
.direction_input = rockchip_gpio_direction_input,
|
|
|
|
.direction_output = rockchip_gpio_direction_output,
|
|
|
|
.get_value = rockchip_gpio_get_value,
|
|
|
|
.set_value = rockchip_gpio_set_value,
|
|
|
|
.get_function = rockchip_gpio_get_function,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id rockchip_gpio_ids[] = {
|
|
|
|
{ .compatible = "rockchip,gpio-bank" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2020-06-25 12:10:04 +08:00
|
|
|
U_BOOT_DRIVER(rockchip_gpio_bank) = {
|
|
|
|
.name = "rockchip_gpio_bank",
|
2015-08-31 06:55:27 +08:00
|
|
|
.id = UCLASS_GPIO,
|
|
|
|
.of_match = rockchip_gpio_ids,
|
|
|
|
.ops = &gpio_rockchip_ops,
|
2020-12-04 07:55:17 +08:00
|
|
|
.priv_auto = sizeof(struct rockchip_gpio_priv),
|
2015-08-31 06:55:27 +08:00
|
|
|
.probe = rockchip_gpio_probe,
|
|
|
|
};
|