blob: d1a399388095cbade4b6a128c51cc41890cb7547 [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
Marek Vasutfefed7a2017-09-15 21:13:56 +02006#include <clk.h>
7#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -07008#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06009#include <asm/global_data.h>
Simon Glass9bc15642020-02-03 07:36:16 -070010#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>
Simon Glass4dcacfc2020-05-10 11:40:13 -060015#include <linux/bitops.h>
Marek Vasutf22c40a2017-11-26 18:08:53 +010016#include "../pinctrl/renesas/sh_pfc.h"
Marek Vasutfefed7a2017-09-15 21:13:56 +020017
18#define GPIO_IOINTSEL 0x00 /* General IO/Interrupt Switching Register */
19#define GPIO_INOUTSEL 0x04 /* General Input/Output Switching Register */
20#define GPIO_OUTDT 0x08 /* General Output Register */
21#define GPIO_INDT 0x0c /* General Input Register */
22#define GPIO_INTDT 0x10 /* Interrupt Display Register */
23#define GPIO_INTCLR 0x14 /* Interrupt Clear Register */
24#define GPIO_INTMSK 0x18 /* Interrupt Mask Register */
25#define GPIO_MSKCLR 0x1c /* Interrupt Mask Clear Register */
26#define GPIO_POSNEG 0x20 /* Positive/Negative Logic Select Register */
27#define GPIO_EDGLEVEL 0x24 /* Edge/level Select Register */
28#define GPIO_FILONOFF 0x28 /* Chattering Prevention On/Off Register */
29#define GPIO_BOTHEDGE 0x4c /* One Edge/Both Edge Select Register */
Marek Vasutd9544f82021-04-27 21:29:50 +020030#define GPIO_INEN 0x50 /* General Input Enable Register */
Marek Vasutfefed7a2017-09-15 21:13:56 +020031
32#define RCAR_MAX_GPIO_PER_BANK 32
33
Marek Vasutd9544f82021-04-27 21:29:50 +020034#define RCAR_GPIO_HAS_INEN BIT(0)
35
Marek Vasutfefed7a2017-09-15 21:13:56 +020036DECLARE_GLOBAL_DATA_PTR;
37
38struct rcar_gpio_priv {
Marek Vasutf22c40a2017-11-26 18:08:53 +010039 void __iomem *regs;
Marek Vasutd9544f82021-04-27 21:29:50 +020040 u32 quirks;
Marek Vasutf22c40a2017-11-26 18:08:53 +010041 int pfc_offset;
Marek Vasutfefed7a2017-09-15 21:13:56 +020042};
43
44static int rcar_gpio_get_value(struct udevice *dev, unsigned offset)
45{
46 struct rcar_gpio_priv *priv = dev_get_priv(dev);
47 const u32 bit = BIT(offset);
48
49 /*
50 * Testing on r8a7790 shows that INDT does not show correct pin state
51 * when configured as output, so use OUTDT in case of output pins.
52 */
53 if (readl(priv->regs + GPIO_INOUTSEL) & bit)
54 return !!(readl(priv->regs + GPIO_OUTDT) & bit);
55 else
56 return !!(readl(priv->regs + GPIO_INDT) & bit);
57}
58
59static int rcar_gpio_set_value(struct udevice *dev, unsigned offset,
60 int value)
61{
62 struct rcar_gpio_priv *priv = dev_get_priv(dev);
63
64 if (value)
65 setbits_le32(priv->regs + GPIO_OUTDT, BIT(offset));
66 else
67 clrbits_le32(priv->regs + GPIO_OUTDT, BIT(offset));
68
69 return 0;
70}
71
Marek Vasuta3136012021-04-27 21:17:43 +020072static void rcar_gpio_set_direction(struct udevice *dev, unsigned offset,
Marek Vasutfefed7a2017-09-15 21:13:56 +020073 bool output)
74{
Marek Vasuta3136012021-04-27 21:17:43 +020075 struct rcar_gpio_priv *priv = dev_get_priv(dev);
76 void __iomem *regs = priv->regs;
77
Marek Vasutfefed7a2017-09-15 21:13:56 +020078 /*
79 * follow steps in the GPIO documentation for
80 * "Setting General Output Mode" and
81 * "Setting General Input Mode"
82 */
83
84 /* Configure postive logic in POSNEG */
85 clrbits_le32(regs + GPIO_POSNEG, BIT(offset));
86
Marek Vasutd9544f82021-04-27 21:29:50 +020087 /* Select "Input Enable/Disable" in INEN */
88 if (priv->quirks & RCAR_GPIO_HAS_INEN) {
89 if (output)
90 clrbits_le32(regs + GPIO_INEN, BIT(offset));
91 else
92 setbits_le32(regs + GPIO_INEN, BIT(offset));
93 }
94
Marek Vasutfefed7a2017-09-15 21:13:56 +020095 /* Select "General Input/Output Mode" in IOINTSEL */
96 clrbits_le32(regs + GPIO_IOINTSEL, BIT(offset));
97
98 /* Select Input Mode or Output Mode in INOUTSEL */
99 if (output)
100 setbits_le32(regs + GPIO_INOUTSEL, BIT(offset));
101 else
102 clrbits_le32(regs + GPIO_INOUTSEL, BIT(offset));
103}
104
105static int rcar_gpio_direction_input(struct udevice *dev, unsigned offset)
106{
Marek Vasuta3136012021-04-27 21:17:43 +0200107 rcar_gpio_set_direction(dev, offset, false);
Marek Vasutfefed7a2017-09-15 21:13:56 +0200108
109 return 0;
110}
111
112static int rcar_gpio_direction_output(struct udevice *dev, unsigned offset,
113 int value)
114{
Marek Vasutfefed7a2017-09-15 21:13:56 +0200115 /* write GPIO value to output before selecting output mode of pin */
116 rcar_gpio_set_value(dev, offset, value);
Marek Vasuta3136012021-04-27 21:17:43 +0200117 rcar_gpio_set_direction(dev, offset, true);
Marek Vasutfefed7a2017-09-15 21:13:56 +0200118
119 return 0;
120}
121
122static int rcar_gpio_get_function(struct udevice *dev, unsigned offset)
123{
124 struct rcar_gpio_priv *priv = dev_get_priv(dev);
125
126 if (readl(priv->regs + GPIO_INOUTSEL) & BIT(offset))
127 return GPIOF_OUTPUT;
128 else
129 return GPIOF_INPUT;
130}
131
132static const struct dm_gpio_ops rcar_gpio_ops = {
Pali Rohár6916dc22022-08-02 12:06:55 +0200133 .request = pinctrl_gpio_request,
134 .rfree = pinctrl_gpio_free,
Marek Vasutfefed7a2017-09-15 21:13:56 +0200135 .direction_input = rcar_gpio_direction_input,
136 .direction_output = rcar_gpio_direction_output,
137 .get_value = rcar_gpio_get_value,
138 .set_value = rcar_gpio_set_value,
139 .get_function = rcar_gpio_get_function,
140};
141
142static int rcar_gpio_probe(struct udevice *dev)
143{
144 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
145 struct rcar_gpio_priv *priv = dev_get_priv(dev);
146 struct fdtdec_phandle_args args;
147 struct clk clk;
148 int node = dev_of_offset(dev);
149 int ret;
150
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900151 priv->regs = dev_read_addr_ptr(dev);
Marek Vasutd9544f82021-04-27 21:29:50 +0200152 priv->quirks = dev_get_driver_data(dev);
Marek Vasutfefed7a2017-09-15 21:13:56 +0200153 uc_priv->bank_name = dev->name;
154
155 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, node, "gpio-ranges",
156 NULL, 3, 0, &args);
Marek Vasutf22c40a2017-11-26 18:08:53 +0100157 priv->pfc_offset = ret == 0 ? args.args[1] : -1;
Marek Vasutfefed7a2017-09-15 21:13:56 +0200158 uc_priv->gpio_count = ret == 0 ? args.args[2] : RCAR_MAX_GPIO_PER_BANK;
159
160 ret = clk_get_by_index(dev, 0, &clk);
161 if (ret < 0) {
162 dev_err(dev, "Failed to get GPIO bank clock\n");
163 return ret;
164 }
165
166 ret = clk_enable(&clk);
Marek Vasutfefed7a2017-09-15 21:13:56 +0200167 if (ret) {
168 dev_err(dev, "Failed to enable GPIO bank clock\n");
169 return ret;
170 }
171
172 return 0;
173}
174
175static const struct udevice_id rcar_gpio_ids[] = {
176 { .compatible = "renesas,gpio-r8a7795" },
177 { .compatible = "renesas,gpio-r8a7796" },
Marek Vasut87f22c22018-02-26 10:35:15 +0100178 { .compatible = "renesas,gpio-r8a77965" },
Marek Vasut6403ab82017-10-21 11:27:04 +0200179 { .compatible = "renesas,gpio-r8a77970" },
Marek Vasutca4b4eb2018-04-26 13:18:45 +0200180 { .compatible = "renesas,gpio-r8a77990" },
Marek Vasutc129f2b2017-10-21 11:28:06 +0200181 { .compatible = "renesas,gpio-r8a77995" },
Marek Vasutd9544f82021-04-27 21:29:50 +0200182 { .compatible = "renesas,gpio-r8a779a0", .data = RCAR_GPIO_HAS_INEN },
Marek Vasute4f6e9b2018-01-18 00:52:15 +0100183 { .compatible = "renesas,rcar-gen2-gpio" },
Marek Vasut54a34ed2017-10-21 11:30:41 +0200184 { .compatible = "renesas,rcar-gen3-gpio" },
Hai Pham2120b5c2023-02-28 22:23:07 +0100185 { .compatible = "renesas,rcar-gen4-gpio", .data = RCAR_GPIO_HAS_INEN },
Marek Vasutfefed7a2017-09-15 21:13:56 +0200186 { /* sentinel */ }
187};
188
189U_BOOT_DRIVER(rcar_gpio) = {
190 .name = "rcar-gpio",
191 .id = UCLASS_GPIO,
192 .of_match = rcar_gpio_ids,
193 .ops = &rcar_gpio_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700194 .priv_auto = sizeof(struct rcar_gpio_priv),
Marek Vasutfefed7a2017-09-15 21:13:56 +0200195 .probe = rcar_gpio_probe,
196};