2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2016-02-16 08:03:48 +00:00
|
|
|
/*
|
2017-10-13 10:21:51 +00:00
|
|
|
* Copyright (C) 2016-2017 Socionext Inc.
|
2016-07-19 12:56:13 +00:00
|
|
|
* Author: Masahiro Yamada <yamada.masahiro@socionext.com>
|
2016-02-16 08:03:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2017-05-17 23:18:03 +00:00
|
|
|
#include <dm.h>
|
2016-02-16 08:03:48 +00:00
|
|
|
#include <linux/bitops.h>
|
|
|
|
#include <linux/io.h>
|
2016-03-24 13:32:41 +00:00
|
|
|
#include <linux/sizes.h>
|
2016-09-21 02:28:55 +00:00
|
|
|
#include <linux/errno.h>
|
2017-10-13 10:21:51 +00:00
|
|
|
#include <asm/global_data.h>
|
2016-02-16 08:03:48 +00:00
|
|
|
#include <asm/gpio.h>
|
2017-11-24 15:25:34 +00:00
|
|
|
#include <dt-bindings/gpio/uniphier-gpio.h>
|
2016-02-16 08:03:48 +00:00
|
|
|
|
2017-10-13 10:21:51 +00:00
|
|
|
#define UNIPHIER_GPIO_PORT_DATA 0x0 /* data */
|
|
|
|
#define UNIPHIER_GPIO_PORT_DIR 0x4 /* direction (1:in, 0:out) */
|
|
|
|
#define UNIPHIER_GPIO_IRQ_EN 0x90 /* irq enable */
|
2016-02-16 08:03:48 +00:00
|
|
|
|
|
|
|
struct uniphier_gpio_priv {
|
2017-10-13 10:21:51 +00:00
|
|
|
void __iomem *regs;
|
2016-02-16 08:03:48 +00:00
|
|
|
};
|
|
|
|
|
2017-10-13 10:21:51 +00:00
|
|
|
static unsigned int uniphier_gpio_bank_to_reg(unsigned int bank)
|
|
|
|
{
|
|
|
|
unsigned int reg;
|
|
|
|
|
|
|
|
reg = (bank + 1) * 8;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unfortunately, the GPIO port registers are not contiguous because
|
|
|
|
* offset 0x90-0x9f is used for IRQ. Add 0x10 when crossing the region.
|
|
|
|
*/
|
|
|
|
if (reg >= UNIPHIER_GPIO_IRQ_EN)
|
|
|
|
reg += 0x10;
|
|
|
|
|
|
|
|
return reg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uniphier_gpio_get_bank_and_mask(unsigned int offset,
|
|
|
|
unsigned int *bank, u32 *mask)
|
|
|
|
{
|
|
|
|
*bank = offset / UNIPHIER_GPIO_LINES_PER_BANK;
|
|
|
|
*mask = BIT(offset % UNIPHIER_GPIO_LINES_PER_BANK);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uniphier_gpio_reg_update(struct uniphier_gpio_priv *priv,
|
|
|
|
unsigned int reg, u32 mask, u32 val)
|
2016-02-16 08:03:48 +00:00
|
|
|
{
|
|
|
|
u32 tmp;
|
|
|
|
|
2017-10-13 10:21:51 +00:00
|
|
|
tmp = readl(priv->regs + reg);
|
|
|
|
tmp &= ~mask;
|
|
|
|
tmp |= mask & val;
|
|
|
|
writel(tmp, priv->regs + reg);
|
2016-02-16 08:03:48 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 10:21:51 +00:00
|
|
|
static void uniphier_gpio_bank_write(struct udevice *dev, unsigned int bank,
|
|
|
|
unsigned int reg, u32 mask, u32 val)
|
2016-02-16 08:03:48 +00:00
|
|
|
{
|
|
|
|
struct uniphier_gpio_priv *priv = dev_get_priv(dev);
|
|
|
|
|
2017-10-13 10:21:51 +00:00
|
|
|
if (!mask)
|
|
|
|
return;
|
|
|
|
|
|
|
|
uniphier_gpio_reg_update(priv, uniphier_gpio_bank_to_reg(bank) + reg,
|
|
|
|
mask, val);
|
2016-02-16 08:03:48 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 10:21:51 +00:00
|
|
|
static void uniphier_gpio_offset_write(struct udevice *dev, unsigned int offset,
|
|
|
|
unsigned int reg, int val)
|
2016-02-16 08:03:48 +00:00
|
|
|
{
|
2017-10-13 10:21:51 +00:00
|
|
|
unsigned int bank;
|
|
|
|
u32 mask;
|
2016-02-16 08:03:48 +00:00
|
|
|
|
2017-10-13 10:21:51 +00:00
|
|
|
uniphier_gpio_get_bank_and_mask(offset, &bank, &mask);
|
|
|
|
|
|
|
|
uniphier_gpio_bank_write(dev, bank, reg, mask, val ? mask : 0);
|
2016-02-16 08:03:48 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 10:21:51 +00:00
|
|
|
static int uniphier_gpio_offset_read(struct udevice *dev,
|
|
|
|
unsigned int offset, unsigned int reg)
|
2016-02-16 08:03:48 +00:00
|
|
|
{
|
2017-10-13 10:21:51 +00:00
|
|
|
struct uniphier_gpio_priv *priv = dev_get_priv(dev);
|
|
|
|
unsigned int bank, reg_offset;
|
|
|
|
u32 mask;
|
2016-02-16 08:03:48 +00:00
|
|
|
|
2017-10-13 10:21:51 +00:00
|
|
|
uniphier_gpio_get_bank_and_mask(offset, &bank, &mask);
|
|
|
|
reg_offset = uniphier_gpio_bank_to_reg(bank) + reg;
|
|
|
|
|
|
|
|
return !!(readl(priv->regs + reg_offset) & mask);
|
2016-02-16 08:03:48 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 10:21:51 +00:00
|
|
|
static int uniphier_gpio_get_function(struct udevice *dev, unsigned int offset)
|
2016-02-16 08:03:48 +00:00
|
|
|
{
|
2017-10-13 10:21:51 +00:00
|
|
|
return uniphier_gpio_offset_read(dev, offset, UNIPHIER_GPIO_PORT_DIR) ?
|
|
|
|
GPIOF_INPUT : GPIOF_OUTPUT;
|
2016-02-16 08:03:48 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 10:21:51 +00:00
|
|
|
static int uniphier_gpio_direction_input(struct udevice *dev,
|
|
|
|
unsigned int offset)
|
2016-02-16 08:03:48 +00:00
|
|
|
{
|
2017-10-13 10:21:51 +00:00
|
|
|
uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_PORT_DIR, 1);
|
2016-02-16 08:03:48 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-13 10:21:51 +00:00
|
|
|
static int uniphier_gpio_direction_output(struct udevice *dev,
|
|
|
|
unsigned int offset, int value)
|
2016-02-16 08:03:48 +00:00
|
|
|
{
|
2017-10-13 10:21:51 +00:00
|
|
|
uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_PORT_DATA, value);
|
|
|
|
uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_PORT_DIR, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int uniphier_gpio_get_value(struct udevice *dev, unsigned int offset)
|
|
|
|
{
|
|
|
|
return uniphier_gpio_offset_read(dev, offset, UNIPHIER_GPIO_PORT_DATA);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int uniphier_gpio_set_value(struct udevice *dev,
|
|
|
|
unsigned int offset, int value)
|
|
|
|
{
|
|
|
|
uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_PORT_DATA, value);
|
|
|
|
|
|
|
|
return 0;
|
2016-02-16 08:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dm_gpio_ops uniphier_gpio_ops = {
|
|
|
|
.direction_input = uniphier_gpio_direction_input,
|
|
|
|
.direction_output = uniphier_gpio_direction_output,
|
|
|
|
.get_value = uniphier_gpio_get_value,
|
|
|
|
.set_value = uniphier_gpio_set_value,
|
|
|
|
.get_function = uniphier_gpio_get_function,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int uniphier_gpio_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct uniphier_gpio_priv *priv = dev_get_priv(dev);
|
|
|
|
struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
|
|
|
|
fdt_addr_t addr;
|
|
|
|
|
2020-07-24 12:42:06 +00:00
|
|
|
addr = devfdt_get_addr(dev);
|
2016-02-16 08:03:48 +00:00
|
|
|
if (addr == FDT_ADDR_T_NONE)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2017-10-13 10:21:51 +00:00
|
|
|
priv->regs = devm_ioremap(dev, addr, SZ_512);
|
|
|
|
if (!priv->regs)
|
2016-02-16 08:03:48 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2017-10-13 10:21:51 +00:00
|
|
|
uc_priv->gpio_count = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev),
|
|
|
|
"ngpios", 0);
|
2016-02-16 08:03:48 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct udevice_id uniphier_gpio_match[] = {
|
|
|
|
{ .compatible = "socionext,uniphier-gpio" },
|
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(uniphier_gpio) = {
|
2017-10-13 10:21:51 +00:00
|
|
|
.name = "uniphier-gpio",
|
2016-02-16 08:03:48 +00:00
|
|
|
.id = UCLASS_GPIO,
|
|
|
|
.of_match = uniphier_gpio_match,
|
|
|
|
.probe = uniphier_gpio_probe,
|
|
|
|
.priv_auto_alloc_size = sizeof(struct uniphier_gpio_priv),
|
|
|
|
.ops = &uniphier_gpio_ops,
|
|
|
|
};
|