2017-10-16 13:21:32 +00:00
|
|
|
/*
|
|
|
|
* Synopsys HSDK SDP Generic PLL clock driver
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 Synopsys
|
|
|
|
* Author: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
|
|
|
|
*
|
|
|
|
* This file is licensed under the terms of the GNU General Public
|
|
|
|
* License version 2. This program is licensed "as is" without any
|
|
|
|
* warranty of any kind, whether express or implied.
|
|
|
|
*/
|
|
|
|
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2017-10-16 13:21:32 +00:00
|
|
|
#include <asm-generic/gpio.h>
|
|
|
|
#include <asm/io.h>
|
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <errno.h>
|
2020-05-10 17:40:13 +00:00
|
|
|
#include <linux/bitops.h>
|
2017-10-16 13:21:32 +00:00
|
|
|
#include <linux/printk.h>
|
|
|
|
|
2018-06-08 14:58:23 +00:00
|
|
|
#define DRV_NAME "gpio_creg"
|
2017-10-16 13:21:32 +00:00
|
|
|
|
|
|
|
struct hsdk_creg_gpio {
|
2018-06-08 14:58:23 +00:00
|
|
|
u32 *regs;
|
|
|
|
u8 shift;
|
|
|
|
u8 activate;
|
|
|
|
u8 deactivate;
|
|
|
|
u8 bit_per_gpio;
|
2017-10-16 13:21:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int hsdk_creg_gpio_set_value(struct udevice *dev, unsigned oft, int val)
|
|
|
|
{
|
|
|
|
struct hsdk_creg_gpio *hcg = dev_get_priv(dev);
|
2018-06-08 14:58:23 +00:00
|
|
|
u8 reg_shift = oft * hcg->bit_per_gpio + hcg->shift;
|
|
|
|
u32 reg = readl(hcg->regs);
|
2017-10-16 13:21:32 +00:00
|
|
|
|
2018-06-08 14:58:23 +00:00
|
|
|
reg &= ~(GENMASK(hcg->bit_per_gpio - 1, 0) << reg_shift);
|
|
|
|
reg |= ((val ? hcg->deactivate : hcg->activate) << reg_shift);
|
2017-10-16 13:21:32 +00:00
|
|
|
|
|
|
|
writel(reg, hcg->regs);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hsdk_creg_gpio_direction_output(struct udevice *dev, unsigned oft,
|
|
|
|
int val)
|
|
|
|
{
|
|
|
|
hsdk_creg_gpio_set_value(dev, oft, val);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hsdk_creg_gpio_direction_input(struct udevice *dev, unsigned oft)
|
|
|
|
{
|
2018-06-08 14:58:23 +00:00
|
|
|
struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
|
|
|
|
|
|
|
|
pr_err("%s can't be used as input!\n", uc_priv->bank_name);
|
2017-10-16 13:21:32 +00:00
|
|
|
|
|
|
|
return -ENOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hsdk_creg_gpio_get_value(struct udevice *dev, unsigned int oft)
|
|
|
|
{
|
|
|
|
struct hsdk_creg_gpio *hcg = dev_get_priv(dev);
|
2018-06-08 14:58:23 +00:00
|
|
|
u32 val = readl(hcg->regs);
|
2017-10-16 13:21:32 +00:00
|
|
|
|
2018-06-08 14:58:23 +00:00
|
|
|
val >>= oft * hcg->bit_per_gpio + hcg->shift;
|
|
|
|
val &= GENMASK(hcg->bit_per_gpio - 1, 0);
|
|
|
|
return (val == hcg->deactivate) ? 1 : 0;
|
2017-10-16 13:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dm_gpio_ops hsdk_creg_gpio_ops = {
|
|
|
|
.direction_output = hsdk_creg_gpio_direction_output,
|
|
|
|
.direction_input = hsdk_creg_gpio_direction_input,
|
|
|
|
.set_value = hsdk_creg_gpio_set_value,
|
|
|
|
.get_value = hsdk_creg_gpio_get_value,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int hsdk_creg_gpio_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
|
|
|
|
struct hsdk_creg_gpio *hcg = dev_get_priv(dev);
|
2018-06-08 14:58:23 +00:00
|
|
|
u32 shift, bit_per_gpio, activate, deactivate, gpio_count;
|
|
|
|
const u8 *defaults;
|
2017-10-16 13:21:32 +00:00
|
|
|
|
2020-08-04 05:14:43 +00:00
|
|
|
hcg->regs = dev_read_addr_ptr(dev);
|
2018-06-08 14:58:23 +00:00
|
|
|
gpio_count = dev_read_u32_default(dev, "gpio-count", 1);
|
|
|
|
shift = dev_read_u32_default(dev, "gpio-first-shift", 0);
|
|
|
|
bit_per_gpio = dev_read_u32_default(dev, "gpio-bit-per-line", 1);
|
|
|
|
activate = dev_read_u32_default(dev, "gpio-activate-val", 1);
|
|
|
|
deactivate = dev_read_u32_default(dev, "gpio-deactivate-val", 0);
|
|
|
|
defaults = dev_read_u8_array_ptr(dev, "gpio-default-val", gpio_count);
|
2017-10-16 13:21:32 +00:00
|
|
|
|
|
|
|
uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
|
|
|
|
if (!uc_priv->bank_name)
|
|
|
|
uc_priv->bank_name = dev_read_name(dev);
|
|
|
|
|
2018-06-08 14:58:23 +00:00
|
|
|
if (!bit_per_gpio) {
|
|
|
|
pr_err("%s: 'gpio-bit-per-line' can't be 0\n",
|
|
|
|
uc_priv->bank_name);
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gpio_count) {
|
|
|
|
pr_err("%s: 'gpio-count' can't be 0\n",
|
|
|
|
uc_priv->bank_name);
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((gpio_count * bit_per_gpio + shift) > 32) {
|
|
|
|
pr_err("%s: u32 io register overflow: try to use %u bits\n",
|
|
|
|
uc_priv->bank_name, gpio_count * bit_per_gpio + shift);
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GENMASK(31, bit_per_gpio) & activate) {
|
|
|
|
pr_err("%s: 'gpio-activate-val' can't be more than %lu\n",
|
|
|
|
uc_priv->bank_name, GENMASK(bit_per_gpio - 1, 0));
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GENMASK(31, bit_per_gpio) & deactivate) {
|
|
|
|
pr_err("%s: 'gpio-deactivate-val' can't be more than %lu\n",
|
|
|
|
uc_priv->bank_name, GENMASK(bit_per_gpio - 1, 0));
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (activate == deactivate) {
|
|
|
|
pr_err("%s: 'gpio-deactivate-val' and 'gpio-activate-val' can't be equal\n",
|
|
|
|
uc_priv->bank_name);
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
hcg->shift = (u8)shift;
|
|
|
|
hcg->bit_per_gpio = (u8)bit_per_gpio;
|
|
|
|
hcg->activate = (u8)activate;
|
|
|
|
hcg->deactivate = (u8)deactivate;
|
|
|
|
uc_priv->gpio_count = gpio_count;
|
|
|
|
|
|
|
|
/* Setup default GPIO value if we have "gpio-default-val" array */
|
|
|
|
if (defaults)
|
|
|
|
for (u8 i = 0; i < gpio_count; i++)
|
|
|
|
hsdk_creg_gpio_set_value(dev, i, defaults[i]);
|
|
|
|
|
2017-10-16 13:21:32 +00:00
|
|
|
pr_debug("%s GPIO [0x%p] controller with %d gpios probed\n",
|
|
|
|
uc_priv->bank_name, hcg->regs, uc_priv->gpio_count);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct udevice_id hsdk_creg_gpio_ids[] = {
|
2018-06-08 14:58:23 +00:00
|
|
|
{ .compatible = "snps,creg-gpio" },
|
2017-10-16 13:21:32 +00:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(gpio_hsdk_creg) = {
|
2018-06-08 14:58:23 +00:00
|
|
|
.name = DRV_NAME,
|
2017-10-16 13:21:32 +00:00
|
|
|
.id = UCLASS_GPIO,
|
|
|
|
.ops = &hsdk_creg_gpio_ops,
|
|
|
|
.probe = hsdk_creg_gpio_probe,
|
|
|
|
.of_match = hsdk_creg_gpio_ids,
|
2020-12-03 23:55:18 +00:00
|
|
|
.plat_auto = sizeof(struct hsdk_creg_gpio),
|
2017-10-16 13:21:32 +00:00
|
|
|
};
|