1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (C) 2019 SiFive, Inc.
10 #include <asm/arch/gpio.h>
15 static int sifive_gpio_probe(struct udevice *dev)
17 struct sifive_gpio_platdata *plat = dev_get_platdata(dev);
18 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
21 sprintf(name, "gpio@%4lx_", (uintptr_t)plat->base);
25 uc_priv->bank_name = str;
28 * Use the gpio count mentioned in device tree,
29 * if not specified in dt, set NR_GPIOS as default
31 uc_priv->gpio_count = dev_read_u32_default(dev, "ngpios", NR_GPIOS);
36 static void sifive_update_gpio_reg(void *bptr, u32 offset, bool value)
38 void __iomem *ptr = (void __iomem *)bptr;
40 u32 bit = BIT(offset);
44 writel(old | bit, ptr);
46 writel(old & ~bit, ptr);
49 static int sifive_gpio_direction_input(struct udevice *dev, u32 offset)
51 struct sifive_gpio_platdata *plat = dev_get_platdata(dev);
52 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
54 if (offset > uc_priv->gpio_count)
57 /* Configure gpio direction as input */
58 sifive_update_gpio_reg(plat->base + GPIO_INPUT_EN, offset, true);
59 sifive_update_gpio_reg(plat->base + GPIO_OUTPUT_EN, offset, false);
64 static int sifive_gpio_direction_output(struct udevice *dev, u32 offset,
67 struct sifive_gpio_platdata *plat = dev_get_platdata(dev);
68 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
70 if (offset > uc_priv->gpio_count)
73 /* Configure gpio direction as output */
74 sifive_update_gpio_reg(plat->base + GPIO_OUTPUT_EN, offset, true);
75 sifive_update_gpio_reg(plat->base + GPIO_INPUT_EN, offset, false);
77 /* Set the output state of the pin */
78 sifive_update_gpio_reg(plat->base + GPIO_OUTPUT_VAL, offset, value);
83 static int sifive_gpio_get_value(struct udevice *dev, u32 offset)
85 struct sifive_gpio_platdata *plat = dev_get_platdata(dev);
86 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
90 if (offset > uc_priv->gpio_count)
93 /* Get direction of the pin */
94 dir = !(readl(plat->base + GPIO_OUTPUT_EN) & BIT(offset));
97 val = readl(plat->base + GPIO_INPUT_VAL) & BIT(offset);
99 val = readl(plat->base + GPIO_OUTPUT_VAL) & BIT(offset);
101 return val ? HIGH : LOW;
104 static int sifive_gpio_set_value(struct udevice *dev, u32 offset, int value)
106 struct sifive_gpio_platdata *plat = dev_get_platdata(dev);
107 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
109 if (offset > uc_priv->gpio_count)
112 sifive_update_gpio_reg(plat->base + GPIO_OUTPUT_VAL, offset, value);
117 static int sifive_gpio_get_function(struct udevice *dev, unsigned int offset)
119 struct sifive_gpio_platdata *plat = dev_get_platdata(dev);
120 u32 outdir, indir, val;
121 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
123 if (offset > uc_priv->gpio_count)
126 /* Get direction of the pin */
127 outdir = readl(plat->base + GPIO_OUTPUT_EN) & BIT(offset);
128 indir = readl(plat->base + GPIO_INPUT_EN) & BIT(offset);
131 /* Pin at specified offset is configured as output */
134 /* Pin at specified offset is configured as input */
137 /*The requested GPIO is not set as input or output */
143 static const struct udevice_id sifive_gpio_match[] = {
144 { .compatible = "sifive,gpio0" },
148 static const struct dm_gpio_ops sifive_gpio_ops = {
149 .direction_input = sifive_gpio_direction_input,
150 .direction_output = sifive_gpio_direction_output,
151 .get_value = sifive_gpio_get_value,
152 .set_value = sifive_gpio_set_value,
153 .get_function = sifive_gpio_get_function,
156 static int sifive_gpio_ofdata_to_platdata(struct udevice *dev)
158 struct sifive_gpio_platdata *plat = dev_get_platdata(dev);
161 addr = devfdt_get_addr(dev);
162 if (addr == FDT_ADDR_T_NONE)
165 plat->base = (void *)addr;
169 U_BOOT_DRIVER(gpio_sifive) = {
170 .name = "gpio_sifive",
172 .of_match = sifive_gpio_match,
173 .ofdata_to_platdata = of_match_ptr(sifive_gpio_ofdata_to_platdata),
174 .platdata_auto_alloc_size = sizeof(struct sifive_gpio_platdata),
175 .ops = &sifive_gpio_ops,
176 .probe = sifive_gpio_probe,