2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2017-03-22 10:26:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) EETS GmbH, 2017, Felix Brack <f.brack@eets.ch>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2017-05-17 23:18:03 +00:00
|
|
|
#include <dm.h>
|
2017-03-22 10:26:44 +00:00
|
|
|
#include <dm/pinctrl.h>
|
2018-03-04 16:20:11 +00:00
|
|
|
#include <linux/libfdt.h>
|
2017-03-22 10:26:44 +00:00
|
|
|
#include <asm/io.h>
|
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
|
|
|
struct single_pdata {
|
|
|
|
fdt_addr_t base; /* first configuration register */
|
|
|
|
int offset; /* index of last configuration register */
|
|
|
|
u32 mask; /* configuration-value mask bits */
|
|
|
|
int width; /* configuration register bit width */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct single_fdt_pin_cfg {
|
|
|
|
fdt32_t reg; /* configuration register offset */
|
|
|
|
fdt32_t val; /* configuration register value */
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* single_configure_pins() - Configure pins based on FDT data
|
|
|
|
*
|
|
|
|
* @dev: Pointer to single pin configuration device which is the parent of
|
|
|
|
* the pins node holding the pin configuration data.
|
|
|
|
* @pins: Pointer to the first element of an array of register/value pairs
|
|
|
|
* of type 'struct single_fdt_pin_cfg'. Each such pair describes the
|
|
|
|
* the pin to be configured and the value to be used for configuration.
|
|
|
|
* This pointer points to a 'pinctrl-single,pins' property in the
|
|
|
|
* device-tree.
|
|
|
|
* @size: Size of the 'pins' array in bytes.
|
|
|
|
* The number of register/value pairs in the 'pins' array therefore
|
|
|
|
* equals to 'size / sizeof(struct single_fdt_pin_cfg)'.
|
|
|
|
*/
|
|
|
|
static int single_configure_pins(struct udevice *dev,
|
|
|
|
const struct single_fdt_pin_cfg *pins,
|
|
|
|
int size)
|
|
|
|
{
|
|
|
|
struct single_pdata *pdata = dev->platdata;
|
|
|
|
int count = size / sizeof(struct single_fdt_pin_cfg);
|
2018-08-16 13:11:49 +00:00
|
|
|
phys_addr_t n, reg;
|
2017-03-22 10:26:44 +00:00
|
|
|
u32 val;
|
|
|
|
|
2017-04-19 02:06:35 +00:00
|
|
|
for (n = 0; n < count; n++, pins++) {
|
2017-03-22 10:26:44 +00:00
|
|
|
reg = fdt32_to_cpu(pins->reg);
|
|
|
|
if ((reg < 0) || (reg > pdata->offset)) {
|
2018-08-16 13:11:49 +00:00
|
|
|
dev_dbg(dev, " invalid register offset 0x%pa\n", ®);
|
2017-03-22 10:26:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
reg += pdata->base;
|
2017-04-19 02:06:35 +00:00
|
|
|
val = fdt32_to_cpu(pins->val) & pdata->mask;
|
2017-03-22 10:26:44 +00:00
|
|
|
switch (pdata->width) {
|
2017-04-19 02:06:35 +00:00
|
|
|
case 16:
|
|
|
|
writew((readw(reg) & ~pdata->mask) | val, reg);
|
|
|
|
break;
|
2017-03-22 10:26:44 +00:00
|
|
|
case 32:
|
2017-04-19 02:06:35 +00:00
|
|
|
writel((readl(reg) & ~pdata->mask) | val, reg);
|
2017-03-22 10:26:44 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dev_warn(dev, "unsupported register width %i\n",
|
|
|
|
pdata->width);
|
2017-04-19 02:06:35 +00:00
|
|
|
continue;
|
2017-03-22 10:26:44 +00:00
|
|
|
}
|
2018-08-16 13:11:49 +00:00
|
|
|
dev_dbg(dev, " reg/val 0x%pa/0x%08x\n", ®, val);
|
2017-03-22 10:26:44 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int single_set_state(struct udevice *dev,
|
|
|
|
struct udevice *config)
|
|
|
|
{
|
|
|
|
const void *fdt = gd->fdt_blob;
|
|
|
|
const struct single_fdt_pin_cfg *prop;
|
|
|
|
int len;
|
|
|
|
|
2017-05-17 23:18:09 +00:00
|
|
|
prop = fdt_getprop(fdt, dev_of_offset(config), "pinctrl-single,pins",
|
|
|
|
&len);
|
2017-03-22 10:26:44 +00:00
|
|
|
if (prop) {
|
|
|
|
dev_dbg(dev, "configuring pins for %s\n", config->name);
|
|
|
|
if (len % sizeof(struct single_fdt_pin_cfg)) {
|
|
|
|
dev_dbg(dev, " invalid pin configuration in fdt\n");
|
|
|
|
return -FDT_ERR_BADSTRUCTURE;
|
|
|
|
}
|
|
|
|
single_configure_pins(dev, prop, len);
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int single_ofdata_to_platdata(struct udevice *dev)
|
|
|
|
{
|
|
|
|
fdt_addr_t addr;
|
|
|
|
u32 of_reg[2];
|
|
|
|
int res;
|
|
|
|
struct single_pdata *pdata = dev->platdata;
|
|
|
|
|
2017-05-17 23:18:09 +00:00
|
|
|
pdata->width = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
|
2017-03-22 10:26:44 +00:00
|
|
|
"pinctrl-single,register-width", 0);
|
|
|
|
|
2017-05-17 23:18:09 +00:00
|
|
|
res = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
|
2017-03-22 10:26:44 +00:00
|
|
|
"reg", of_reg, 2);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
pdata->offset = of_reg[1] - pdata->width / 8;
|
|
|
|
|
2017-05-17 23:18:05 +00:00
|
|
|
addr = devfdt_get_addr(dev);
|
2017-03-22 10:26:44 +00:00
|
|
|
if (addr == FDT_ADDR_T_NONE) {
|
|
|
|
dev_dbg(dev, "no valid base register address\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
pdata->base = addr;
|
|
|
|
|
2017-05-17 23:18:09 +00:00
|
|
|
pdata->mask = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
|
2017-03-22 10:26:44 +00:00
|
|
|
"pinctrl-single,function-mask",
|
|
|
|
0xffffffff);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct pinctrl_ops single_pinctrl_ops = {
|
|
|
|
.set_state = single_set_state,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id single_pinctrl_match[] = {
|
|
|
|
{ .compatible = "pinctrl-single" },
|
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(single_pinctrl) = {
|
|
|
|
.name = "single-pinctrl",
|
|
|
|
.id = UCLASS_PINCTRL,
|
|
|
|
.of_match = single_pinctrl_match,
|
|
|
|
.ops = &single_pinctrl_ops,
|
|
|
|
.platdata_auto_alloc_size = sizeof(struct single_pdata),
|
|
|
|
.ofdata_to_platdata = single_ofdata_to_platdata,
|
|
|
|
};
|