2017-02-12 18:25:49 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <dm/pinctrl.h>
|
2017-04-10 22:02:57 +00:00
|
|
|
#include <asm/arch/gpio.h>
|
|
|
|
#include <asm/gpio.h>
|
|
|
|
#include <asm/io.h>
|
2017-02-12 18:25:49 +00:00
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2017-04-10 22:03:04 +00:00
|
|
|
#define MAX_PINS_ONE_IP 70
|
2017-04-10 22:02:57 +00:00
|
|
|
#define MODE_BITS_MASK 3
|
|
|
|
#define OSPEED_MASK 3
|
|
|
|
#define PUPD_MASK 3
|
|
|
|
#define OTYPE_MSK 1
|
|
|
|
#define AFR_MASK 0xF
|
|
|
|
|
|
|
|
static int stm32_gpio_config(struct gpio_desc *desc,
|
|
|
|
const struct stm32_gpio_ctl *ctl)
|
|
|
|
{
|
|
|
|
struct stm32_gpio_priv *priv = dev_get_priv(desc->dev);
|
|
|
|
struct stm32_gpio_regs *regs = priv->regs;
|
|
|
|
u32 index;
|
|
|
|
|
|
|
|
if (!ctl || ctl->af > 15 || ctl->mode > 3 || ctl->otype > 1 ||
|
|
|
|
ctl->pupd > 2 || ctl->speed > 3)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
index = (desc->offset & 0x07) * 4;
|
|
|
|
clrsetbits_le32(®s->afr[desc->offset >> 3], AFR_MASK << index,
|
|
|
|
ctl->af << index);
|
|
|
|
|
|
|
|
index = desc->offset * 2;
|
|
|
|
clrsetbits_le32(®s->moder, MODE_BITS_MASK << index,
|
|
|
|
ctl->mode << index);
|
|
|
|
clrsetbits_le32(®s->ospeedr, OSPEED_MASK << index,
|
|
|
|
ctl->speed << index);
|
|
|
|
clrsetbits_le32(®s->pupdr, PUPD_MASK << index, ctl->pupd << index);
|
|
|
|
|
|
|
|
index = desc->offset;
|
|
|
|
clrsetbits_le32(®s->otyper, OTYPE_MSK << index, ctl->otype << index);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2018-03-12 09:46:13 +00:00
|
|
|
|
2017-02-12 18:25:49 +00:00
|
|
|
static int prep_gpio_dsc(struct stm32_gpio_dsc *gpio_dsc, u32 port_pin)
|
|
|
|
{
|
2018-03-12 09:46:13 +00:00
|
|
|
gpio_dsc->port = (port_pin & 0x1F000) >> 12;
|
2017-02-12 18:25:49 +00:00
|
|
|
gpio_dsc->pin = (port_pin & 0x0F00) >> 8;
|
|
|
|
debug("%s: GPIO:port= %d, pin= %d\n", __func__, gpio_dsc->port,
|
|
|
|
gpio_dsc->pin);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int prep_gpio_ctl(struct stm32_gpio_ctl *gpio_ctl, u32 gpio_fn, int node)
|
|
|
|
{
|
|
|
|
gpio_fn &= 0x00FF;
|
2017-04-10 22:02:57 +00:00
|
|
|
gpio_ctl->af = 0;
|
2017-02-12 18:25:49 +00:00
|
|
|
|
|
|
|
switch (gpio_fn) {
|
|
|
|
case 0:
|
|
|
|
gpio_ctl->mode = STM32_GPIO_MODE_IN;
|
|
|
|
break;
|
|
|
|
case 1 ... 16:
|
|
|
|
gpio_ctl->mode = STM32_GPIO_MODE_AF;
|
|
|
|
gpio_ctl->af = gpio_fn - 1;
|
|
|
|
break;
|
|
|
|
case 17:
|
|
|
|
gpio_ctl->mode = STM32_GPIO_MODE_AN;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
gpio_ctl->mode = STM32_GPIO_MODE_OUT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpio_ctl->speed = fdtdec_get_int(gd->fdt_blob, node, "slew-rate", 0);
|
|
|
|
|
|
|
|
if (fdtdec_get_bool(gd->fdt_blob, node, "drive-open-drain"))
|
|
|
|
gpio_ctl->otype = STM32_GPIO_OTYPE_OD;
|
|
|
|
else
|
|
|
|
gpio_ctl->otype = STM32_GPIO_OTYPE_PP;
|
|
|
|
|
|
|
|
if (fdtdec_get_bool(gd->fdt_blob, node, "bias-pull-up"))
|
|
|
|
gpio_ctl->pupd = STM32_GPIO_PUPD_UP;
|
|
|
|
else if (fdtdec_get_bool(gd->fdt_blob, node, "bias-pull-down"))
|
|
|
|
gpio_ctl->pupd = STM32_GPIO_PUPD_DOWN;
|
|
|
|
else
|
|
|
|
gpio_ctl->pupd = STM32_GPIO_PUPD_NO;
|
|
|
|
|
|
|
|
debug("%s: gpio fn= %d, slew-rate= %x, op type= %x, pull-upd is = %x\n",
|
|
|
|
__func__, gpio_fn, gpio_ctl->speed, gpio_ctl->otype,
|
|
|
|
gpio_ctl->pupd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-20 15:04:18 +00:00
|
|
|
static int stm32_pinctrl_config(int offset)
|
2017-02-12 18:25:49 +00:00
|
|
|
{
|
2017-04-10 22:03:04 +00:00
|
|
|
u32 pin_mux[MAX_PINS_ONE_IP];
|
2017-02-12 18:25:49 +00:00
|
|
|
int rv, len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check for "pinmux" property in each subnode (e.g. pins1 and pins2 for
|
|
|
|
* usart1) of pin controller phandle "pinctrl-0"
|
|
|
|
* */
|
2017-06-20 15:04:18 +00:00
|
|
|
fdt_for_each_subnode(offset, gd->fdt_blob, offset) {
|
2017-02-12 18:25:49 +00:00
|
|
|
struct stm32_gpio_dsc gpio_dsc;
|
|
|
|
struct stm32_gpio_ctl gpio_ctl;
|
|
|
|
int i;
|
|
|
|
|
2017-06-20 15:04:18 +00:00
|
|
|
len = fdtdec_get_int_array_count(gd->fdt_blob, offset,
|
2017-02-12 18:25:49 +00:00
|
|
|
"pinmux", pin_mux,
|
|
|
|
ARRAY_SIZE(pin_mux));
|
2017-06-20 15:04:18 +00:00
|
|
|
debug("%s: no of pinmux entries= %d\n", __func__, len);
|
2017-02-12 18:25:49 +00:00
|
|
|
if (len < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
for (i = 0; i < len; i++) {
|
2017-04-10 22:02:59 +00:00
|
|
|
struct gpio_desc desc;
|
2018-03-12 09:46:13 +00:00
|
|
|
|
2017-02-12 18:25:49 +00:00
|
|
|
debug("%s: pinmux = %x\n", __func__, *(pin_mux + i));
|
|
|
|
prep_gpio_dsc(&gpio_dsc, *(pin_mux + i));
|
2017-06-20 15:04:18 +00:00
|
|
|
prep_gpio_ctl(&gpio_ctl, *(pin_mux + i), offset);
|
2017-04-10 22:02:59 +00:00
|
|
|
rv = uclass_get_device_by_seq(UCLASS_GPIO,
|
2018-03-12 09:46:13 +00:00
|
|
|
gpio_dsc.port,
|
|
|
|
&desc.dev);
|
2017-04-10 22:02:59 +00:00
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
desc.offset = gpio_dsc.pin;
|
|
|
|
rv = stm32_gpio_config(&desc, &gpio_ctl);
|
2017-02-12 18:25:49 +00:00
|
|
|
debug("%s: rv = %d\n\n", __func__, rv);
|
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-20 15:04:19 +00:00
|
|
|
#if CONFIG_IS_ENABLED(PINCTRL_FULL)
|
|
|
|
static int stm32_pinctrl_set_state(struct udevice *dev, struct udevice *config)
|
|
|
|
{
|
|
|
|
return stm32_pinctrl_config(dev_of_offset(config));
|
|
|
|
}
|
|
|
|
#else /* PINCTRL_FULL */
|
2017-06-20 15:04:18 +00:00
|
|
|
static int stm32_pinctrl_set_state_simple(struct udevice *dev,
|
|
|
|
struct udevice *periph)
|
|
|
|
{
|
|
|
|
const void *fdt = gd->fdt_blob;
|
|
|
|
const fdt32_t *list;
|
|
|
|
uint32_t phandle;
|
|
|
|
int config_node;
|
|
|
|
int size, i, ret;
|
|
|
|
|
|
|
|
list = fdt_getprop(fdt, dev_of_offset(periph), "pinctrl-0", &size);
|
|
|
|
if (!list)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
debug("%s: periph->name = %s\n", __func__, periph->name);
|
|
|
|
|
|
|
|
size /= sizeof(*list);
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
phandle = fdt32_to_cpu(*list++);
|
|
|
|
|
|
|
|
config_node = fdt_node_offset_by_phandle(fdt, phandle);
|
|
|
|
if (config_node < 0) {
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("prop pinctrl-0 index %d invalid phandle\n", i);
|
2017-06-20 15:04:18 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = stm32_pinctrl_config(config_node);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-06-20 15:04:19 +00:00
|
|
|
#endif /* PINCTRL_FULL */
|
2017-06-20 15:04:18 +00:00
|
|
|
|
2017-02-12 18:25:49 +00:00
|
|
|
static struct pinctrl_ops stm32_pinctrl_ops = {
|
2017-06-20 15:04:19 +00:00
|
|
|
#if CONFIG_IS_ENABLED(PINCTRL_FULL)
|
|
|
|
.set_state = stm32_pinctrl_set_state,
|
|
|
|
#else /* PINCTRL_FULL */
|
2017-02-12 18:25:49 +00:00
|
|
|
.set_state_simple = stm32_pinctrl_set_state_simple,
|
2017-06-20 15:04:19 +00:00
|
|
|
#endif /* PINCTRL_FULL */
|
2017-02-12 18:25:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id stm32_pinctrl_ids[] = {
|
2017-12-12 08:49:35 +00:00
|
|
|
{ .compatible = "st,stm32f429-pinctrl" },
|
|
|
|
{ .compatible = "st,stm32f469-pinctrl" },
|
2017-02-12 18:25:49 +00:00
|
|
|
{ .compatible = "st,stm32f746-pinctrl" },
|
2017-09-13 16:00:04 +00:00
|
|
|
{ .compatible = "st,stm32h743-pinctrl" },
|
2018-03-12 09:46:13 +00:00
|
|
|
{ .compatible = "st,stm32mp157-pinctrl" },
|
|
|
|
{ .compatible = "st,stm32mp157-z-pinctrl" },
|
2017-02-12 18:25:49 +00:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(pinctrl_stm32) = {
|
|
|
|
.name = "pinctrl_stm32",
|
|
|
|
.id = UCLASS_PINCTRL,
|
|
|
|
.of_match = stm32_pinctrl_ids,
|
|
|
|
.ops = &stm32_pinctrl_ops,
|
|
|
|
.bind = dm_scan_fdt_dev,
|
|
|
|
};
|