blob: 9dc4cd6042271263f98c421e6b39268ec8573b4e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasutfefed7a2017-09-15 21:13:56 +02002/*
3 * Copyright (C) 2017 Marek Vasut <marek.vasut@gmail.com>
Marek Vasutfefed7a2017-09-15 21:13:56 +02004 */
5
6#include <common.h>
7#include <clk.h>
8#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -07009#include <malloc.h>
10#include <dm/device_compat.h>
Marek Vasutf071c0a2019-04-21 22:46:25 +020011#include <dm/pinctrl.h>
Marek Vasutfefed7a2017-09-15 21:13:56 +020012#include <errno.h>
13#include <asm/gpio.h>
14#include <asm/io.h>
Marek Vasutf22c40a2017-11-26 18:08:53 +010015#include "../pinctrl/renesas/sh_pfc.h"
Marek Vasutfefed7a2017-09-15 21:13:56 +020016
17#define GPIO_IOINTSEL 0x00 /* General IO/Interrupt Switching Register */
18#define GPIO_INOUTSEL 0x04 /* General Input/Output Switching Register */
19#define GPIO_OUTDT 0x08 /* General Output Register */
20#define GPIO_INDT 0x0c /* General Input Register */
21#define GPIO_INTDT 0x10 /* Interrupt Display Register */
22#define GPIO_INTCLR 0x14 /* Interrupt Clear Register */
23#define GPIO_INTMSK 0x18 /* Interrupt Mask Register */
24#define GPIO_MSKCLR 0x1c /* Interrupt Mask Clear Register */
25#define GPIO_POSNEG 0x20 /* Positive/Negative Logic Select Register */
26#define GPIO_EDGLEVEL 0x24 /* Edge/level Select Register */
27#define GPIO_FILONOFF 0x28 /* Chattering Prevention On/Off Register */
28#define GPIO_BOTHEDGE 0x4c /* One Edge/Both Edge Select Register */
29
30#define RCAR_MAX_GPIO_PER_BANK 32
31
32DECLARE_GLOBAL_DATA_PTR;
33
34struct rcar_gpio_priv {
Marek Vasutf22c40a2017-11-26 18:08:53 +010035 void __iomem *regs;
36 int pfc_offset;
Marek Vasutfefed7a2017-09-15 21:13:56 +020037};
38
39static int rcar_gpio_get_value(struct udevice *dev, unsigned offset)
40{
41 struct rcar_gpio_priv *priv = dev_get_priv(dev);
42 const u32 bit = BIT(offset);
43
44 /*
45 * Testing on r8a7790 shows that INDT does not show correct pin state
46 * when configured as output, so use OUTDT in case of output pins.
47 */
48 if (readl(priv->regs + GPIO_INOUTSEL) & bit)
49 return !!(readl(priv->regs + GPIO_OUTDT) & bit);
50 else
51 return !!(readl(priv->regs + GPIO_INDT) & bit);
52}
53
54static int rcar_gpio_set_value(struct udevice *dev, unsigned offset,
55 int value)
56{
57 struct rcar_gpio_priv *priv = dev_get_priv(dev);
58
59 if (value)
60 setbits_le32(priv->regs + GPIO_OUTDT, BIT(offset));
61 else
62 clrbits_le32(priv->regs + GPIO_OUTDT, BIT(offset));
63
64 return 0;
65}
66
67static void rcar_gpio_set_direction(void __iomem *regs, unsigned offset,
68 bool output)
69{
70 /*
71 * follow steps in the GPIO documentation for
72 * "Setting General Output Mode" and
73 * "Setting General Input Mode"
74 */
75
76 /* Configure postive logic in POSNEG */
77 clrbits_le32(regs + GPIO_POSNEG, BIT(offset));
78
79 /* Select "General Input/Output Mode" in IOINTSEL */
80 clrbits_le32(regs + GPIO_IOINTSEL, BIT(offset));
81
82 /* Select Input Mode or Output Mode in INOUTSEL */
83 if (output)
84 setbits_le32(regs + GPIO_INOUTSEL, BIT(offset));
85 else
86 clrbits_le32(regs + GPIO_INOUTSEL, BIT(offset));
87}
88
89static int rcar_gpio_direction_input(struct udevice *dev, unsigned offset)
90{
91 struct rcar_gpio_priv *priv = dev_get_priv(dev);
92
93 rcar_gpio_set_direction(priv->regs, offset, false);
94
95 return 0;
96}
97
98static int rcar_gpio_direction_output(struct udevice *dev, unsigned offset,
99 int value)
100{
101 struct rcar_gpio_priv *priv = dev_get_priv(dev);
102
103 /* write GPIO value to output before selecting output mode of pin */
104 rcar_gpio_set_value(dev, offset, value);
105 rcar_gpio_set_direction(priv->regs, offset, true);
106
107 return 0;
108}
109
110static int rcar_gpio_get_function(struct udevice *dev, unsigned offset)
111{
112 struct rcar_gpio_priv *priv = dev_get_priv(dev);
113
114 if (readl(priv->regs + GPIO_INOUTSEL) & BIT(offset))
115 return GPIOF_OUTPUT;
116 else
117 return GPIOF_INPUT;
118}
119
Marek Vasutf22c40a2017-11-26 18:08:53 +0100120static int rcar_gpio_request(struct udevice *dev, unsigned offset,
121 const char *label)
122{
Marek Vasutf071c0a2019-04-21 22:46:25 +0200123 return pinctrl_gpio_request(dev, offset);
124}
Marek Vasutf22c40a2017-11-26 18:08:53 +0100125
Marek Vasutf071c0a2019-04-21 22:46:25 +0200126static int rcar_gpio_free(struct udevice *dev, unsigned offset)
127{
128 return pinctrl_gpio_free(dev, offset);
Marek Vasutf22c40a2017-11-26 18:08:53 +0100129}
130
Marek Vasutfefed7a2017-09-15 21:13:56 +0200131static const struct dm_gpio_ops rcar_gpio_ops = {
Marek Vasutf22c40a2017-11-26 18:08:53 +0100132 .request = rcar_gpio_request,
Simon Glassb3a47542020-02-04 20:15:17 -0700133 .rfree = rcar_gpio_free,
Marek Vasutfefed7a2017-09-15 21:13:56 +0200134 .direction_input = rcar_gpio_direction_input,
135 .direction_output = rcar_gpio_direction_output,
136 .get_value = rcar_gpio_get_value,
137 .set_value = rcar_gpio_set_value,
138 .get_function = rcar_gpio_get_function,
139};
140
141static int rcar_gpio_probe(struct udevice *dev)
142{
143 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
144 struct rcar_gpio_priv *priv = dev_get_priv(dev);
145 struct fdtdec_phandle_args args;
146 struct clk clk;
147 int node = dev_of_offset(dev);
148 int ret;
149
150 priv->regs = (void __iomem *)devfdt_get_addr(dev);
151 uc_priv->bank_name = dev->name;
152
153 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, node, "gpio-ranges",
154 NULL, 3, 0, &args);
Marek Vasutf22c40a2017-11-26 18:08:53 +0100155 priv->pfc_offset = ret == 0 ? args.args[1] : -1;
Marek Vasutfefed7a2017-09-15 21:13:56 +0200156 uc_priv->gpio_count = ret == 0 ? args.args[2] : RCAR_MAX_GPIO_PER_BANK;
157
158 ret = clk_get_by_index(dev, 0, &clk);
159 if (ret < 0) {
160 dev_err(dev, "Failed to get GPIO bank clock\n");
161 return ret;
162 }
163
164 ret = clk_enable(&clk);
165 clk_free(&clk);
166 if (ret) {
167 dev_err(dev, "Failed to enable GPIO bank clock\n");
168 return ret;
169 }
170
171 return 0;
172}
173
174static const struct udevice_id rcar_gpio_ids[] = {
175 { .compatible = "renesas,gpio-r8a7795" },
176 { .compatible = "renesas,gpio-r8a7796" },
Marek Vasut87f22c22018-02-26 10:35:15 +0100177 { .compatible = "renesas,gpio-r8a77965" },
Marek Vasut6403ab82017-10-21 11:27:04 +0200178 { .compatible = "renesas,gpio-r8a77970" },
Marek Vasutca4b4eb2018-04-26 13:18:45 +0200179 { .compatible = "renesas,gpio-r8a77990" },
Marek Vasutc129f2b2017-10-21 11:28:06 +0200180 { .compatible = "renesas,gpio-r8a77995" },
Marek Vasute4f6e9b2018-01-18 00:52:15 +0100181 { .compatible = "renesas,rcar-gen2-gpio" },
Marek Vasut54a34ed2017-10-21 11:30:41 +0200182 { .compatible = "renesas,rcar-gen3-gpio" },
Marek Vasutfefed7a2017-09-15 21:13:56 +0200183 { /* sentinel */ }
184};
185
186U_BOOT_DRIVER(rcar_gpio) = {
187 .name = "rcar-gpio",
188 .id = UCLASS_GPIO,
189 .of_match = rcar_gpio_ids,
190 .ops = &rcar_gpio_ops,
191 .priv_auto_alloc_size = sizeof(struct rcar_gpio_priv),
192 .probe = rcar_gpio_probe,
193};