2 * Copyright (C) 2017 Marek Vasut <marek.vasut@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0+
14 #define GPIO_IOINTSEL 0x00 /* General IO/Interrupt Switching Register */
15 #define GPIO_INOUTSEL 0x04 /* General Input/Output Switching Register */
16 #define GPIO_OUTDT 0x08 /* General Output Register */
17 #define GPIO_INDT 0x0c /* General Input Register */
18 #define GPIO_INTDT 0x10 /* Interrupt Display Register */
19 #define GPIO_INTCLR 0x14 /* Interrupt Clear Register */
20 #define GPIO_INTMSK 0x18 /* Interrupt Mask Register */
21 #define GPIO_MSKCLR 0x1c /* Interrupt Mask Clear Register */
22 #define GPIO_POSNEG 0x20 /* Positive/Negative Logic Select Register */
23 #define GPIO_EDGLEVEL 0x24 /* Edge/level Select Register */
24 #define GPIO_FILONOFF 0x28 /* Chattering Prevention On/Off Register */
25 #define GPIO_BOTHEDGE 0x4c /* One Edge/Both Edge Select Register */
27 #define RCAR_MAX_GPIO_PER_BANK 32
29 DECLARE_GLOBAL_DATA_PTR;
31 struct rcar_gpio_priv {
35 static int rcar_gpio_get_value(struct udevice *dev, unsigned offset)
37 struct rcar_gpio_priv *priv = dev_get_priv(dev);
38 const u32 bit = BIT(offset);
41 * Testing on r8a7790 shows that INDT does not show correct pin state
42 * when configured as output, so use OUTDT in case of output pins.
44 if (readl(priv->regs + GPIO_INOUTSEL) & bit)
45 return !!(readl(priv->regs + GPIO_OUTDT) & bit);
47 return !!(readl(priv->regs + GPIO_INDT) & bit);
50 static int rcar_gpio_set_value(struct udevice *dev, unsigned offset,
53 struct rcar_gpio_priv *priv = dev_get_priv(dev);
56 setbits_le32(priv->regs + GPIO_OUTDT, BIT(offset));
58 clrbits_le32(priv->regs + GPIO_OUTDT, BIT(offset));
63 static void rcar_gpio_set_direction(void __iomem *regs, unsigned offset,
67 * follow steps in the GPIO documentation for
68 * "Setting General Output Mode" and
69 * "Setting General Input Mode"
72 /* Configure postive logic in POSNEG */
73 clrbits_le32(regs + GPIO_POSNEG, BIT(offset));
75 /* Select "General Input/Output Mode" in IOINTSEL */
76 clrbits_le32(regs + GPIO_IOINTSEL, BIT(offset));
78 /* Select Input Mode or Output Mode in INOUTSEL */
80 setbits_le32(regs + GPIO_INOUTSEL, BIT(offset));
82 clrbits_le32(regs + GPIO_INOUTSEL, BIT(offset));
85 static int rcar_gpio_direction_input(struct udevice *dev, unsigned offset)
87 struct rcar_gpio_priv *priv = dev_get_priv(dev);
89 rcar_gpio_set_direction(priv->regs, offset, false);
94 static int rcar_gpio_direction_output(struct udevice *dev, unsigned offset,
97 struct rcar_gpio_priv *priv = dev_get_priv(dev);
99 /* write GPIO value to output before selecting output mode of pin */
100 rcar_gpio_set_value(dev, offset, value);
101 rcar_gpio_set_direction(priv->regs, offset, true);
106 static int rcar_gpio_get_function(struct udevice *dev, unsigned offset)
108 struct rcar_gpio_priv *priv = dev_get_priv(dev);
110 if (readl(priv->regs + GPIO_INOUTSEL) & BIT(offset))
116 static const struct dm_gpio_ops rcar_gpio_ops = {
117 .direction_input = rcar_gpio_direction_input,
118 .direction_output = rcar_gpio_direction_output,
119 .get_value = rcar_gpio_get_value,
120 .set_value = rcar_gpio_set_value,
121 .get_function = rcar_gpio_get_function,
124 static int rcar_gpio_probe(struct udevice *dev)
126 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
127 struct rcar_gpio_priv *priv = dev_get_priv(dev);
128 struct fdtdec_phandle_args args;
130 int node = dev_of_offset(dev);
133 priv->regs = (void __iomem *)devfdt_get_addr(dev);
134 uc_priv->bank_name = dev->name;
136 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, node, "gpio-ranges",
138 uc_priv->gpio_count = ret == 0 ? args.args[2] : RCAR_MAX_GPIO_PER_BANK;
140 ret = clk_get_by_index(dev, 0, &clk);
142 dev_err(dev, "Failed to get GPIO bank clock\n");
146 ret = clk_enable(&clk);
149 dev_err(dev, "Failed to enable GPIO bank clock\n");
156 static const struct udevice_id rcar_gpio_ids[] = {
157 { .compatible = "renesas,gpio-r8a7795" },
158 { .compatible = "renesas,gpio-r8a7796" },
162 U_BOOT_DRIVER(rcar_gpio) = {
165 .of_match = rcar_gpio_ids,
166 .ops = &rcar_gpio_ops,
167 .priv_auto_alloc_size = sizeof(struct rcar_gpio_priv),
168 .probe = rcar_gpio_probe,