2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2017-04-10 22:02:57 +00:00
|
|
|
/*
|
2017-10-23 07:53:58 +00:00
|
|
|
* Copyright (C) 2017, STMicroelectronics - All Rights Reserved
|
|
|
|
* Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics.
|
2017-04-10 22:02:57 +00:00
|
|
|
*/
|
|
|
|
|
2020-11-06 18:01:33 +00:00
|
|
|
#define LOG_CATEGORY UCLASS_GPIO
|
|
|
|
|
2017-04-10 22:02:57 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <clk.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <fdtdec.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2017-04-10 22:02:57 +00:00
|
|
|
#include <asm/arch/gpio.h>
|
|
|
|
#include <asm/arch/stm32.h>
|
|
|
|
#include <asm/gpio.h>
|
|
|
|
#include <asm/io.h>
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <dm/device_compat.h>
|
2020-05-10 17:40:13 +00:00
|
|
|
#include <linux/bitops.h>
|
2017-04-10 22:02:57 +00:00
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
|
2020-10-02 12:08:54 +00:00
|
|
|
#define STM32_GPIOS_PER_BANK 16
|
|
|
|
|
2020-06-04 12:30:25 +00:00
|
|
|
#define MODE_BITS(gpio_pin) ((gpio_pin) * 2)
|
2017-04-10 22:02:57 +00:00
|
|
|
#define MODE_BITS_MASK 3
|
2020-06-04 12:30:25 +00:00
|
|
|
#define BSRR_BIT(gpio_pin, value) BIT((gpio_pin) + (value ? 0 : 16))
|
|
|
|
|
|
|
|
#define PUPD_BITS(gpio_pin) ((gpio_pin) * 2)
|
|
|
|
#define PUPD_MASK 3
|
|
|
|
|
|
|
|
#define OTYPE_BITS(gpio_pin) (gpio_pin)
|
|
|
|
#define OTYPE_MSK 1
|
|
|
|
|
|
|
|
static void stm32_gpio_set_moder(struct stm32_gpio_regs *regs,
|
|
|
|
int idx,
|
|
|
|
int mode)
|
|
|
|
{
|
|
|
|
int bits_index;
|
|
|
|
int mask;
|
|
|
|
|
|
|
|
bits_index = MODE_BITS(idx);
|
|
|
|
mask = MODE_BITS_MASK << bits_index;
|
|
|
|
|
|
|
|
clrsetbits_le32(®s->moder, mask, mode << bits_index);
|
|
|
|
}
|
|
|
|
|
2020-06-04 12:30:26 +00:00
|
|
|
static int stm32_gpio_get_moder(struct stm32_gpio_regs *regs, int idx)
|
|
|
|
{
|
|
|
|
return (readl(®s->moder) >> MODE_BITS(idx)) & MODE_BITS_MASK;
|
|
|
|
}
|
|
|
|
|
2020-06-04 12:30:25 +00:00
|
|
|
static void stm32_gpio_set_otype(struct stm32_gpio_regs *regs,
|
|
|
|
int idx,
|
|
|
|
enum stm32_gpio_otype otype)
|
|
|
|
{
|
|
|
|
int bits;
|
|
|
|
|
|
|
|
bits = OTYPE_BITS(idx);
|
|
|
|
clrsetbits_le32(®s->otyper, OTYPE_MSK << bits, otype << bits);
|
|
|
|
}
|
|
|
|
|
2020-06-04 12:30:26 +00:00
|
|
|
static enum stm32_gpio_otype stm32_gpio_get_otype(struct stm32_gpio_regs *regs,
|
|
|
|
int idx)
|
|
|
|
{
|
|
|
|
return (readl(®s->otyper) >> OTYPE_BITS(idx)) & OTYPE_MSK;
|
|
|
|
}
|
|
|
|
|
2020-06-04 12:30:25 +00:00
|
|
|
static void stm32_gpio_set_pupd(struct stm32_gpio_regs *regs,
|
|
|
|
int idx,
|
|
|
|
enum stm32_gpio_pupd pupd)
|
|
|
|
{
|
|
|
|
int bits;
|
|
|
|
|
|
|
|
bits = PUPD_BITS(idx);
|
|
|
|
clrsetbits_le32(®s->pupdr, PUPD_MASK << bits, pupd << bits);
|
|
|
|
}
|
2017-04-10 22:02:57 +00:00
|
|
|
|
2020-06-04 12:30:26 +00:00
|
|
|
static enum stm32_gpio_pupd stm32_gpio_get_pupd(struct stm32_gpio_regs *regs,
|
|
|
|
int idx)
|
|
|
|
{
|
|
|
|
return (readl(®s->pupdr) >> PUPD_BITS(idx)) & PUPD_MASK;
|
|
|
|
}
|
|
|
|
|
2018-12-03 09:52:51 +00:00
|
|
|
/*
|
|
|
|
* convert gpio offset to gpio index taking into account gpio holes
|
|
|
|
* into gpio bank
|
|
|
|
*/
|
|
|
|
int stm32_offset_to_index(struct udevice *dev, unsigned int offset)
|
|
|
|
{
|
|
|
|
struct stm32_gpio_priv *priv = dev_get_priv(dev);
|
2019-06-21 13:26:46 +00:00
|
|
|
unsigned int idx = 0;
|
2018-12-03 09:52:51 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < STM32_GPIOS_PER_BANK; i++) {
|
|
|
|
if (priv->gpio_range & BIT(i)) {
|
|
|
|
if (idx == offset)
|
|
|
|
return idx;
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* shouldn't happen */
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-04-10 22:02:57 +00:00
|
|
|
static int stm32_gpio_direction_input(struct udevice *dev, unsigned offset)
|
|
|
|
{
|
|
|
|
struct stm32_gpio_priv *priv = dev_get_priv(dev);
|
|
|
|
struct stm32_gpio_regs *regs = priv->regs;
|
2018-12-03 09:52:51 +00:00
|
|
|
int idx;
|
|
|
|
|
|
|
|
idx = stm32_offset_to_index(dev, offset);
|
|
|
|
if (idx < 0)
|
|
|
|
return idx;
|
|
|
|
|
2020-06-04 12:30:25 +00:00
|
|
|
stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_IN);
|
2017-04-10 22:02:57 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stm32_gpio_direction_output(struct udevice *dev, unsigned offset,
|
|
|
|
int value)
|
|
|
|
{
|
|
|
|
struct stm32_gpio_priv *priv = dev_get_priv(dev);
|
|
|
|
struct stm32_gpio_regs *regs = priv->regs;
|
2018-12-03 09:52:51 +00:00
|
|
|
int idx;
|
|
|
|
|
|
|
|
idx = stm32_offset_to_index(dev, offset);
|
|
|
|
if (idx < 0)
|
|
|
|
return idx;
|
|
|
|
|
2020-06-04 12:30:25 +00:00
|
|
|
stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_OUT);
|
2018-08-09 09:57:57 +00:00
|
|
|
|
2018-12-03 09:52:51 +00:00
|
|
|
writel(BSRR_BIT(idx, value), ®s->bsrr);
|
2017-04-10 22:02:57 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stm32_gpio_get_value(struct udevice *dev, unsigned offset)
|
|
|
|
{
|
|
|
|
struct stm32_gpio_priv *priv = dev_get_priv(dev);
|
|
|
|
struct stm32_gpio_regs *regs = priv->regs;
|
2018-12-03 09:52:51 +00:00
|
|
|
int idx;
|
|
|
|
|
|
|
|
idx = stm32_offset_to_index(dev, offset);
|
|
|
|
if (idx < 0)
|
|
|
|
return idx;
|
2017-04-10 22:02:57 +00:00
|
|
|
|
2018-12-03 09:52:51 +00:00
|
|
|
return readl(®s->idr) & BIT(idx) ? 1 : 0;
|
2017-04-10 22:02:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int stm32_gpio_set_value(struct udevice *dev, unsigned offset, int value)
|
|
|
|
{
|
|
|
|
struct stm32_gpio_priv *priv = dev_get_priv(dev);
|
|
|
|
struct stm32_gpio_regs *regs = priv->regs;
|
2018-12-03 09:52:51 +00:00
|
|
|
int idx;
|
2017-04-10 22:02:57 +00:00
|
|
|
|
2018-12-03 09:52:51 +00:00
|
|
|
idx = stm32_offset_to_index(dev, offset);
|
|
|
|
if (idx < 0)
|
|
|
|
return idx;
|
|
|
|
|
|
|
|
writel(BSRR_BIT(idx, value), ®s->bsrr);
|
2017-04-10 22:02:57 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-24 12:10:21 +00:00
|
|
|
static int stm32_gpio_get_function(struct udevice *dev, unsigned int offset)
|
|
|
|
{
|
|
|
|
struct stm32_gpio_priv *priv = dev_get_priv(dev);
|
|
|
|
struct stm32_gpio_regs *regs = priv->regs;
|
2018-12-03 09:52:51 +00:00
|
|
|
int bits_index;
|
|
|
|
int mask;
|
|
|
|
int idx;
|
2018-10-24 12:10:21 +00:00
|
|
|
u32 mode;
|
|
|
|
|
2018-12-03 09:52:51 +00:00
|
|
|
idx = stm32_offset_to_index(dev, offset);
|
|
|
|
if (idx < 0)
|
|
|
|
return idx;
|
|
|
|
|
|
|
|
bits_index = MODE_BITS(idx);
|
|
|
|
mask = MODE_BITS_MASK << bits_index;
|
|
|
|
|
2018-10-24 12:10:21 +00:00
|
|
|
mode = (readl(®s->moder) & mask) >> bits_index;
|
|
|
|
if (mode == STM32_GPIO_MODE_OUT)
|
|
|
|
return GPIOF_OUTPUT;
|
|
|
|
if (mode == STM32_GPIO_MODE_IN)
|
|
|
|
return GPIOF_INPUT;
|
|
|
|
if (mode == STM32_GPIO_MODE_AN)
|
|
|
|
return GPIOF_UNUSED;
|
|
|
|
|
|
|
|
return GPIOF_FUNC;
|
|
|
|
}
|
|
|
|
|
2021-02-05 04:21:55 +00:00
|
|
|
static int stm32_gpio_set_flags(struct udevice *dev, unsigned int offset,
|
|
|
|
ulong flags)
|
2020-06-04 12:30:25 +00:00
|
|
|
{
|
|
|
|
struct stm32_gpio_priv *priv = dev_get_priv(dev);
|
|
|
|
struct stm32_gpio_regs *regs = priv->regs;
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
idx = stm32_offset_to_index(dev, offset);
|
|
|
|
if (idx < 0)
|
|
|
|
return idx;
|
|
|
|
|
|
|
|
if (flags & GPIOD_IS_OUT) {
|
2021-02-05 04:22:03 +00:00
|
|
|
bool value = flags & GPIOD_IS_OUT_ACTIVE;
|
2020-06-04 12:30:25 +00:00
|
|
|
|
|
|
|
if (flags & GPIOD_OPEN_DRAIN)
|
|
|
|
stm32_gpio_set_otype(regs, idx, STM32_GPIO_OTYPE_OD);
|
|
|
|
else
|
|
|
|
stm32_gpio_set_otype(regs, idx, STM32_GPIO_OTYPE_PP);
|
2021-02-05 04:22:03 +00:00
|
|
|
|
2020-06-04 12:30:25 +00:00
|
|
|
stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_OUT);
|
|
|
|
writel(BSRR_BIT(idx, value), ®s->bsrr);
|
|
|
|
|
|
|
|
} else if (flags & GPIOD_IS_IN) {
|
|
|
|
stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_IN);
|
|
|
|
}
|
2020-10-28 09:49:08 +00:00
|
|
|
if (flags & GPIOD_PULL_UP)
|
|
|
|
stm32_gpio_set_pupd(regs, idx, STM32_GPIO_PUPD_UP);
|
|
|
|
else if (flags & GPIOD_PULL_DOWN)
|
|
|
|
stm32_gpio_set_pupd(regs, idx, STM32_GPIO_PUPD_DOWN);
|
2020-06-04 12:30:25 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-05 04:21:56 +00:00
|
|
|
static int stm32_gpio_get_flags(struct udevice *dev, unsigned int offset,
|
|
|
|
ulong *flagsp)
|
2020-06-04 12:30:26 +00:00
|
|
|
{
|
|
|
|
struct stm32_gpio_priv *priv = dev_get_priv(dev);
|
|
|
|
struct stm32_gpio_regs *regs = priv->regs;
|
|
|
|
int idx;
|
|
|
|
ulong dir_flags = 0;
|
|
|
|
|
|
|
|
idx = stm32_offset_to_index(dev, offset);
|
|
|
|
if (idx < 0)
|
|
|
|
return idx;
|
|
|
|
|
|
|
|
switch (stm32_gpio_get_moder(regs, idx)) {
|
|
|
|
case STM32_GPIO_MODE_OUT:
|
|
|
|
dir_flags |= GPIOD_IS_OUT;
|
|
|
|
if (stm32_gpio_get_otype(regs, idx) == STM32_GPIO_OTYPE_OD)
|
|
|
|
dir_flags |= GPIOD_OPEN_DRAIN;
|
|
|
|
if (readl(®s->idr) & BIT(idx))
|
|
|
|
dir_flags |= GPIOD_IS_OUT_ACTIVE;
|
|
|
|
break;
|
|
|
|
case STM32_GPIO_MODE_IN:
|
|
|
|
dir_flags |= GPIOD_IS_IN;
|
2020-10-28 09:49:08 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (stm32_gpio_get_pupd(regs, idx)) {
|
|
|
|
case STM32_GPIO_PUPD_UP:
|
|
|
|
dir_flags |= GPIOD_PULL_UP;
|
|
|
|
break;
|
|
|
|
case STM32_GPIO_PUPD_DOWN:
|
|
|
|
dir_flags |= GPIOD_PULL_DOWN;
|
2020-06-04 12:30:26 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2021-02-05 04:21:56 +00:00
|
|
|
*flagsp = dir_flags;
|
2020-06-04 12:30:26 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-10 22:02:57 +00:00
|
|
|
static const struct dm_gpio_ops gpio_stm32_ops = {
|
|
|
|
.direction_input = stm32_gpio_direction_input,
|
|
|
|
.direction_output = stm32_gpio_direction_output,
|
|
|
|
.get_value = stm32_gpio_get_value,
|
|
|
|
.set_value = stm32_gpio_set_value,
|
2018-10-24 12:10:21 +00:00
|
|
|
.get_function = stm32_gpio_get_function,
|
2021-02-05 04:21:55 +00:00
|
|
|
.set_flags = stm32_gpio_set_flags,
|
2021-02-05 04:21:56 +00:00
|
|
|
.get_flags = stm32_gpio_get_flags,
|
2017-04-10 22:02:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int gpio_stm32_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct stm32_gpio_priv *priv = dev_get_priv(dev);
|
2020-09-09 16:28:33 +00:00
|
|
|
struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
|
|
|
|
struct ofnode_phandle_args args;
|
|
|
|
const char *name;
|
2018-12-03 09:52:53 +00:00
|
|
|
struct clk clk;
|
2017-04-10 22:02:57 +00:00
|
|
|
fdt_addr_t addr;
|
2020-09-09 16:28:33 +00:00
|
|
|
int ret, i;
|
2017-04-10 22:02:57 +00:00
|
|
|
|
2018-03-12 09:46:07 +00:00
|
|
|
addr = dev_read_addr(dev);
|
2017-04-10 22:02:57 +00:00
|
|
|
if (addr == FDT_ADDR_T_NONE)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
priv->regs = (struct stm32_gpio_regs *)addr;
|
2019-01-04 09:55:06 +00:00
|
|
|
|
2018-03-12 09:46:07 +00:00
|
|
|
name = dev_read_string(dev, "st,bank-name");
|
2017-04-10 22:02:57 +00:00
|
|
|
if (!name)
|
|
|
|
return -EINVAL;
|
|
|
|
uc_priv->bank_name = name;
|
2018-12-03 09:52:51 +00:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
ret = dev_read_phandle_with_args(dev, "gpio-ranges",
|
|
|
|
NULL, 3, i, &args);
|
|
|
|
|
2020-09-09 16:28:34 +00:00
|
|
|
if (!ret && args.args_count < 3)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2019-01-04 09:55:05 +00:00
|
|
|
if (ret == -ENOENT) {
|
|
|
|
uc_priv->gpio_count = STM32_GPIOS_PER_BANK;
|
|
|
|
priv->gpio_range = GENMASK(STM32_GPIOS_PER_BANK - 1, 0);
|
|
|
|
}
|
|
|
|
|
2018-12-03 09:52:51 +00:00
|
|
|
while (ret != -ENOENT) {
|
|
|
|
priv->gpio_range |= GENMASK(args.args[2] + args.args[0] - 1,
|
|
|
|
args.args[0]);
|
|
|
|
|
|
|
|
uc_priv->gpio_count += args.args[2];
|
|
|
|
|
|
|
|
ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
|
|
|
|
++i, &args);
|
2020-09-09 16:28:34 +00:00
|
|
|
if (!ret && args.args_count < 3)
|
|
|
|
return -EINVAL;
|
2018-12-03 09:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dev_dbg(dev, "addr = 0x%p bank_name = %s gpio_count = %d gpio_range = 0x%x\n",
|
|
|
|
(u32 *)priv->regs, uc_priv->bank_name, uc_priv->gpio_count,
|
|
|
|
priv->gpio_range);
|
2020-04-22 12:29:17 +00:00
|
|
|
|
2017-04-10 22:02:57 +00:00
|
|
|
ret = clk_get_by_index(dev, 0, &clk);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = clk_enable(&clk);
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "failed to enable clock\n");
|
|
|
|
return ret;
|
|
|
|
}
|
2020-11-06 18:01:33 +00:00
|
|
|
dev_dbg(dev, "clock enabled\n");
|
2017-04-10 22:02:57 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(gpio_stm32) = {
|
|
|
|
.name = "gpio_stm32",
|
|
|
|
.id = UCLASS_GPIO,
|
|
|
|
.probe = gpio_stm32_probe,
|
|
|
|
.ops = &gpio_stm32_ops,
|
2018-10-24 13:36:30 +00:00
|
|
|
.flags = DM_UC_FLAG_SEQ_ALIAS,
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct stm32_gpio_priv),
|
2017-04-10 22:02:57 +00:00
|
|
|
};
|