2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2011-10-21 13:48:50 +00:00
|
|
|
/*
|
2015-10-21 13:33:45 +00:00
|
|
|
* Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
|
2011-10-21 13:48:50 +00:00
|
|
|
* Copyright (C) 2011 Missing Link Electronics
|
|
|
|
* Joachim Foerster <joachim@missinglinkelectronics.com>
|
|
|
|
*/
|
|
|
|
#include <common.h>
|
2015-10-21 13:33:45 +00:00
|
|
|
#include <dm.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <fdtdec.h>
|
2011-10-21 13:48:50 +00:00
|
|
|
#include <asm/io.h>
|
|
|
|
#include <asm/gpio.h>
|
|
|
|
|
2015-10-21 13:33:45 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
2011-10-21 13:48:50 +00:00
|
|
|
|
2015-10-21 13:33:45 +00:00
|
|
|
struct altera_pio_regs {
|
|
|
|
u32 data; /* Data register */
|
|
|
|
u32 direction; /* Direction register */
|
|
|
|
};
|
2011-10-21 13:48:50 +00:00
|
|
|
|
2015-10-21 13:33:45 +00:00
|
|
|
struct altera_pio_platdata {
|
|
|
|
struct altera_pio_regs *regs;
|
|
|
|
int gpio_count;
|
|
|
|
const char *bank_name;
|
|
|
|
};
|
2011-10-21 13:48:50 +00:00
|
|
|
|
2015-10-21 13:33:45 +00:00
|
|
|
static int altera_pio_direction_input(struct udevice *dev, unsigned pin)
|
2011-10-21 13:48:50 +00:00
|
|
|
{
|
2015-10-21 13:33:45 +00:00
|
|
|
struct altera_pio_platdata *plat = dev_get_platdata(dev);
|
|
|
|
struct altera_pio_regs *const regs = plat->regs;
|
2011-10-21 13:48:50 +00:00
|
|
|
|
2015-10-21 13:33:45 +00:00
|
|
|
clrbits_le32(®s->direction, 1 << pin);
|
2011-10-21 13:48:50 +00:00
|
|
|
|
2015-10-21 13:33:45 +00:00
|
|
|
return 0;
|
2011-10-21 13:48:50 +00:00
|
|
|
}
|
|
|
|
|
2015-10-21 13:33:45 +00:00
|
|
|
static int altera_pio_direction_output(struct udevice *dev, unsigned pin,
|
|
|
|
int val)
|
2011-10-21 13:48:50 +00:00
|
|
|
{
|
2015-10-21 13:33:45 +00:00
|
|
|
struct altera_pio_platdata *plat = dev_get_platdata(dev);
|
|
|
|
struct altera_pio_regs *const regs = plat->regs;
|
2011-10-21 13:48:50 +00:00
|
|
|
|
2015-10-21 13:33:45 +00:00
|
|
|
if (val)
|
|
|
|
setbits_le32(®s->data, 1 << pin);
|
|
|
|
else
|
|
|
|
clrbits_le32(®s->data, 1 << pin);
|
|
|
|
/* change the data first, then the direction. to avoid glitch */
|
|
|
|
setbits_le32(®s->direction, 1 << pin);
|
2011-10-21 13:48:50 +00:00
|
|
|
|
2015-10-21 13:33:45 +00:00
|
|
|
return 0;
|
2011-10-21 13:48:50 +00:00
|
|
|
}
|
|
|
|
|
2015-10-21 13:33:45 +00:00
|
|
|
static int altera_pio_get_value(struct udevice *dev, unsigned pin)
|
2011-10-21 13:48:50 +00:00
|
|
|
{
|
2015-10-21 13:33:45 +00:00
|
|
|
struct altera_pio_platdata *plat = dev_get_platdata(dev);
|
|
|
|
struct altera_pio_regs *const regs = plat->regs;
|
2011-10-21 13:48:50 +00:00
|
|
|
|
2019-01-07 09:17:46 +00:00
|
|
|
return !!(readl(®s->data) & (1 << pin));
|
2011-10-21 13:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-21 13:33:45 +00:00
|
|
|
static int altera_pio_set_value(struct udevice *dev, unsigned pin, int val)
|
2011-10-21 13:48:50 +00:00
|
|
|
{
|
2015-10-21 13:33:45 +00:00
|
|
|
struct altera_pio_platdata *plat = dev_get_platdata(dev);
|
|
|
|
struct altera_pio_regs *const regs = plat->regs;
|
2011-10-21 13:48:50 +00:00
|
|
|
|
2015-10-21 13:33:45 +00:00
|
|
|
if (val)
|
|
|
|
setbits_le32(®s->data, 1 << pin);
|
|
|
|
else
|
|
|
|
clrbits_le32(®s->data, 1 << pin);
|
2011-10-21 13:48:50 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-21 13:33:45 +00:00
|
|
|
static int altera_pio_probe(struct udevice *dev)
|
2011-10-21 13:48:50 +00:00
|
|
|
{
|
2015-10-21 13:33:45 +00:00
|
|
|
struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
|
|
|
|
struct altera_pio_platdata *plat = dev_get_platdata(dev);
|
2011-10-21 13:48:50 +00:00
|
|
|
|
2015-10-21 13:33:45 +00:00
|
|
|
uc_priv->gpio_count = plat->gpio_count;
|
|
|
|
uc_priv->bank_name = plat->bank_name;
|
2011-10-21 13:48:50 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-21 13:33:45 +00:00
|
|
|
static int altera_pio_ofdata_to_platdata(struct udevice *dev)
|
2011-10-21 13:48:50 +00:00
|
|
|
{
|
2015-10-21 13:33:45 +00:00
|
|
|
struct altera_pio_platdata *plat = dev_get_platdata(dev);
|
2011-10-21 13:48:50 +00:00
|
|
|
|
2017-05-17 23:18:05 +00:00
|
|
|
plat->regs = map_physmem(devfdt_get_addr(dev),
|
2015-11-14 03:26:51 +00:00
|
|
|
sizeof(struct altera_pio_regs),
|
|
|
|
MAP_NOCACHE);
|
2017-01-17 23:52:55 +00:00
|
|
|
plat->gpio_count = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
|
2015-10-21 13:33:45 +00:00
|
|
|
"altr,gpio-bank-width", 32);
|
2017-01-17 23:52:55 +00:00
|
|
|
plat->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
|
2015-10-21 13:33:45 +00:00
|
|
|
"gpio-bank-name", NULL);
|
2011-10-21 13:48:50 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-21 13:33:45 +00:00
|
|
|
static const struct dm_gpio_ops altera_pio_ops = {
|
|
|
|
.direction_input = altera_pio_direction_input,
|
|
|
|
.direction_output = altera_pio_direction_output,
|
|
|
|
.get_value = altera_pio_get_value,
|
|
|
|
.set_value = altera_pio_set_value,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id altera_pio_ids[] = {
|
|
|
|
{ .compatible = "altr,pio-1.0" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(altera_pio) = {
|
|
|
|
.name = "altera_pio",
|
|
|
|
.id = UCLASS_GPIO,
|
|
|
|
.of_match = altera_pio_ids,
|
|
|
|
.ops = &altera_pio_ops,
|
|
|
|
.ofdata_to_platdata = altera_pio_ofdata_to_platdata,
|
|
|
|
.platdata_auto_alloc_size = sizeof(struct altera_pio_platdata),
|
|
|
|
.probe = altera_pio_probe,
|
|
|
|
};
|