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>
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <dm/device_compat.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>
|
|
|
|
|
2021-04-11 07:39:39 +00:00
|
|
|
/**
|
|
|
|
* struct single_pdata - platform data
|
|
|
|
* @base: first configuration register
|
|
|
|
* @offset: index of last configuration register
|
|
|
|
* @mask: configuration-value mask bits
|
|
|
|
* @width: configuration register bit width
|
|
|
|
* @bits_per_mux: true if one register controls more than one pin
|
|
|
|
*/
|
2017-03-22 10:26:44 +00:00
|
|
|
struct single_pdata {
|
2021-04-11 07:39:39 +00:00
|
|
|
fdt_addr_t base;
|
|
|
|
int offset;
|
|
|
|
u32 mask;
|
2021-04-11 07:39:44 +00:00
|
|
|
u32 width;
|
2019-06-10 18:15:55 +00:00
|
|
|
bool bits_per_mux;
|
2017-03-22 10:26:44 +00:00
|
|
|
};
|
|
|
|
|
2021-04-11 07:39:39 +00:00
|
|
|
/**
|
|
|
|
* struct single_fdt_pin_cfg - pin configuration
|
|
|
|
*
|
|
|
|
* This structure is used for the pin configuration parameters in case
|
|
|
|
* the register controls only one pin.
|
|
|
|
*
|
|
|
|
* @reg: configuration register offset
|
|
|
|
* @val: configuration register value
|
|
|
|
*/
|
2017-03-22 10:26:44 +00:00
|
|
|
struct single_fdt_pin_cfg {
|
2021-04-11 07:39:39 +00:00
|
|
|
fdt32_t reg;
|
|
|
|
fdt32_t val;
|
2017-03-22 10:26:44 +00:00
|
|
|
};
|
|
|
|
|
2021-04-11 07:39:39 +00:00
|
|
|
/**
|
|
|
|
* struct single_fdt_bits_cfg - pin configuration
|
|
|
|
*
|
|
|
|
* This structure is used for the pin configuration parameters in case
|
|
|
|
* the register controls more than one pin.
|
|
|
|
*
|
|
|
|
* @reg: configuration register offset
|
|
|
|
* @val: configuration register value
|
|
|
|
* @mask: configuration register mask
|
|
|
|
*/
|
2019-06-10 18:15:55 +00:00
|
|
|
struct single_fdt_bits_cfg {
|
2021-04-11 07:39:39 +00:00
|
|
|
fdt32_t reg;
|
|
|
|
fdt32_t val;
|
|
|
|
fdt32_t mask;
|
2019-06-10 18:15:55 +00:00
|
|
|
};
|
|
|
|
|
2021-04-11 07:39:46 +00:00
|
|
|
static unsigned int single_read(struct udevice *dev, fdt_addr_t reg)
|
|
|
|
{
|
|
|
|
struct single_pdata *pdata = dev_get_plat(dev);
|
|
|
|
|
|
|
|
switch (pdata->width) {
|
|
|
|
case 8:
|
|
|
|
return readb(reg);
|
|
|
|
case 16:
|
|
|
|
return readw(reg);
|
|
|
|
default: /* 32 bits */
|
|
|
|
return readl(reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return readb(reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void single_write(struct udevice *dev, unsigned int val, fdt_addr_t reg)
|
|
|
|
{
|
|
|
|
struct single_pdata *pdata = dev_get_plat(dev);
|
|
|
|
|
|
|
|
switch (pdata->width) {
|
|
|
|
case 8:
|
|
|
|
writeb(val, reg);
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
writew(val, reg);
|
|
|
|
break;
|
|
|
|
default: /* 32 bits */
|
|
|
|
writel(val, reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 10:26:44 +00:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2020-12-23 02:30:28 +00:00
|
|
|
struct single_pdata *pdata = dev_get_plat(dev);
|
2021-04-11 07:39:40 +00:00
|
|
|
int n, count = size / sizeof(struct single_fdt_pin_cfg);
|
|
|
|
phys_addr_t reg;
|
2021-04-11 07:39:41 +00:00
|
|
|
u32 offset, val;
|
2017-03-22 10:26:44 +00:00
|
|
|
|
2021-04-11 07:39:45 +00:00
|
|
|
/* If function mask is null, needn't enable it. */
|
|
|
|
if (!pdata->mask)
|
|
|
|
return 0;
|
|
|
|
|
2017-04-19 02:06:35 +00:00
|
|
|
for (n = 0; n < count; n++, pins++) {
|
2021-04-11 07:39:41 +00:00
|
|
|
offset = fdt32_to_cpu(pins->reg);
|
|
|
|
if (offset < 0 || offset > pdata->offset) {
|
|
|
|
dev_dbg(dev, " invalid register offset 0x%x\n",
|
|
|
|
offset);
|
2017-03-22 10:26:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-04-11 07:39:41 +00:00
|
|
|
|
|
|
|
reg = pdata->base + offset;
|
2017-04-19 02:06:35 +00:00
|
|
|
val = fdt32_to_cpu(pins->val) & pdata->mask;
|
2021-04-11 07:39:46 +00:00
|
|
|
single_write(dev, (single_read(dev, reg) & ~pdata->mask) | val,
|
|
|
|
reg);
|
2021-04-11 07:39:42 +00:00
|
|
|
dev_dbg(dev, " reg/val %pa/0x%08x\n", ®, val);
|
2021-04-11 07:39:46 +00:00
|
|
|
|
2017-03-22 10:26:44 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-10 18:15:55 +00:00
|
|
|
static int single_configure_bits(struct udevice *dev,
|
|
|
|
const struct single_fdt_bits_cfg *pins,
|
|
|
|
int size)
|
|
|
|
{
|
2020-12-23 02:30:28 +00:00
|
|
|
struct single_pdata *pdata = dev_get_plat(dev);
|
2021-04-11 07:39:40 +00:00
|
|
|
int n, count = size / sizeof(struct single_fdt_bits_cfg);
|
|
|
|
phys_addr_t reg;
|
2021-04-11 07:39:41 +00:00
|
|
|
u32 offset, val, mask;
|
2019-06-10 18:15:55 +00:00
|
|
|
|
|
|
|
for (n = 0; n < count; n++, pins++) {
|
2021-04-11 07:39:41 +00:00
|
|
|
offset = fdt32_to_cpu(pins->reg);
|
|
|
|
if (offset < 0 || offset > pdata->offset) {
|
|
|
|
dev_dbg(dev, " invalid register offset 0x%x\n",
|
|
|
|
offset);
|
2019-06-10 18:15:55 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-04-11 07:39:41 +00:00
|
|
|
|
|
|
|
reg = pdata->base + offset;
|
2019-06-10 18:15:55 +00:00
|
|
|
|
|
|
|
mask = fdt32_to_cpu(pins->mask);
|
|
|
|
val = fdt32_to_cpu(pins->val) & mask;
|
2021-04-11 07:39:46 +00:00
|
|
|
single_write(dev, (single_read(dev, reg) & ~mask) | val, reg);
|
2021-04-11 07:39:42 +00:00
|
|
|
dev_dbg(dev, " reg/val %pa/0x%08x\n", ®, val);
|
2019-06-10 18:15:55 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2017-03-22 10:26:44 +00:00
|
|
|
static int single_set_state(struct udevice *dev,
|
|
|
|
struct udevice *config)
|
|
|
|
{
|
|
|
|
const struct single_fdt_pin_cfg *prop;
|
2019-06-10 18:15:55 +00:00
|
|
|
const struct single_fdt_bits_cfg *prop_bits;
|
2017-03-22 10:26:44 +00:00
|
|
|
int len;
|
|
|
|
|
2020-04-22 17:25:31 +00:00
|
|
|
prop = dev_read_prop(config, "pinctrl-single,pins", &len);
|
2019-06-10 18:15:55 +00:00
|
|
|
|
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);
|
2019-06-10 18:15:55 +00:00
|
|
|
return 0;
|
2017-03-22 10:26:44 +00:00
|
|
|
}
|
|
|
|
|
2019-06-10 18:15:55 +00:00
|
|
|
/* pinctrl-single,pins not found so check for pinctrl-single,bits */
|
2020-04-22 17:25:31 +00:00
|
|
|
prop_bits = dev_read_prop(config, "pinctrl-single,bits", &len);
|
2019-06-10 18:15:55 +00:00
|
|
|
if (prop_bits) {
|
|
|
|
dev_dbg(dev, "configuring pins for %s\n", config->name);
|
|
|
|
if (len % sizeof(struct single_fdt_bits_cfg)) {
|
|
|
|
dev_dbg(dev, " invalid bits configuration in fdt\n");
|
|
|
|
return -FDT_ERR_BADSTRUCTURE;
|
|
|
|
}
|
|
|
|
single_configure_bits(dev, prop_bits, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Neither 'pinctrl-single,pins' nor 'pinctrl-single,bits' were found */
|
2017-03-22 10:26:44 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2020-12-03 23:55:21 +00:00
|
|
|
static int single_of_to_plat(struct udevice *dev)
|
2017-03-22 10:26:44 +00:00
|
|
|
{
|
|
|
|
fdt_addr_t addr;
|
2021-04-11 07:39:43 +00:00
|
|
|
fdt_size_t size;
|
2020-12-23 02:30:28 +00:00
|
|
|
struct single_pdata *pdata = dev_get_plat(dev);
|
2021-04-11 07:39:44 +00:00
|
|
|
int ret;
|
2017-03-22 10:26:44 +00:00
|
|
|
|
2021-04-11 07:39:44 +00:00
|
|
|
ret = dev_read_u32(dev, "pinctrl-single,register-width", &pdata->width);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "missing register width\n");
|
|
|
|
return ret;
|
|
|
|
}
|
2017-03-22 10:26:44 +00:00
|
|
|
|
2021-04-11 07:39:46 +00:00
|
|
|
switch (pdata->width) {
|
|
|
|
case 8:
|
|
|
|
case 16:
|
|
|
|
case 32:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dev_err(dev, "wrong register width\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2021-04-11 07:39:43 +00:00
|
|
|
addr = dev_read_addr_size(dev, "reg", &size);
|
|
|
|
if (addr == FDT_ADDR_T_NONE) {
|
|
|
|
dev_err(dev, "failed to get base register size\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pdata->offset = size - pdata->width / BITS_PER_BYTE;
|
2017-03-22 10:26:44 +00:00
|
|
|
|
2020-01-13 10:34:55 +00:00
|
|
|
addr = dev_read_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;
|
|
|
|
|
2021-04-11 07:39:45 +00:00
|
|
|
ret = dev_read_u32(dev, "pinctrl-single,function-mask", &pdata->mask);
|
|
|
|
if (ret) {
|
|
|
|
pdata->mask = 0;
|
|
|
|
dev_warn(dev, "missing function register mask\n");
|
|
|
|
}
|
|
|
|
|
2020-01-13 10:34:55 +00:00
|
|
|
pdata->bits_per_mux = dev_read_bool(dev, "pinctrl-single,bit-per-mux");
|
2019-06-10 18:15:55 +00:00
|
|
|
|
2017-03-22 10:26:44 +00:00
|
|
|
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,
|
2020-12-03 23:55:18 +00:00
|
|
|
.plat_auto = sizeof(struct single_pdata),
|
2020-12-03 23:55:21 +00:00
|
|
|
.of_to_plat = single_of_to_plat,
|
2017-03-22 10:26:44 +00:00
|
|
|
};
|