blob: 7a2ca91c76926b070c424aacb1455fa5bf41afee [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vikas Manocha77417102017-04-10 15:02:57 -07002/*
Patrice Chotard3bc599c2017-10-23 09:53:58 +02003 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
4 * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics.
Vikas Manocha77417102017-04-10 15:02:57 -07005 */
6
Patrick Delaunay6dd89d92020-11-06 19:01:33 +01007#define LOG_CATEGORY UCLASS_GPIO
8
Vikas Manocha77417102017-04-10 15:02:57 -07009#include <common.h>
10#include <clk.h>
11#include <dm.h>
12#include <fdtdec.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Vikas Manocha77417102017-04-10 15:02:57 -070014#include <asm/arch/stm32.h>
15#include <asm/gpio.h>
16#include <asm/io.h>
Simon Glass336d4612020-02-03 07:36:16 -070017#include <dm/device_compat.h>
Simon Glasscd93d622020-05-10 11:40:13 -060018#include <linux/bitops.h>
Vikas Manocha77417102017-04-10 15:02:57 -070019#include <linux/errno.h>
20#include <linux/io.h>
21
Patrick Delaunay56a368f2021-10-22 20:12:34 +020022#include "stm32_gpio_priv.h"
23
Patrick Delaunay6af78d02020-10-02 14:08:54 +020024#define STM32_GPIOS_PER_BANK 16
25
Patrick Delaunayf13ff882020-06-04 14:30:25 +020026#define MODE_BITS(gpio_pin) ((gpio_pin) * 2)
Vikas Manocha77417102017-04-10 15:02:57 -070027#define MODE_BITS_MASK 3
Patrick Delaunayf13ff882020-06-04 14:30:25 +020028#define BSRR_BIT(gpio_pin, value) BIT((gpio_pin) + (value ? 0 : 16))
29
30#define PUPD_BITS(gpio_pin) ((gpio_pin) * 2)
31#define PUPD_MASK 3
32
33#define OTYPE_BITS(gpio_pin) (gpio_pin)
34#define OTYPE_MSK 1
35
36static void stm32_gpio_set_moder(struct stm32_gpio_regs *regs,
37 int idx,
38 int mode)
39{
40 int bits_index;
41 int mask;
42
43 bits_index = MODE_BITS(idx);
44 mask = MODE_BITS_MASK << bits_index;
45
46 clrsetbits_le32(&regs->moder, mask, mode << bits_index);
47}
48
Patrick Delaunay43efbb62020-06-04 14:30:26 +020049static int stm32_gpio_get_moder(struct stm32_gpio_regs *regs, int idx)
50{
51 return (readl(&regs->moder) >> MODE_BITS(idx)) & MODE_BITS_MASK;
52}
53
Patrick Delaunayf13ff882020-06-04 14:30:25 +020054static void stm32_gpio_set_otype(struct stm32_gpio_regs *regs,
55 int idx,
56 enum stm32_gpio_otype otype)
57{
58 int bits;
59
60 bits = OTYPE_BITS(idx);
61 clrsetbits_le32(&regs->otyper, OTYPE_MSK << bits, otype << bits);
62}
63
Patrick Delaunay43efbb62020-06-04 14:30:26 +020064static enum stm32_gpio_otype stm32_gpio_get_otype(struct stm32_gpio_regs *regs,
65 int idx)
66{
67 return (readl(&regs->otyper) >> OTYPE_BITS(idx)) & OTYPE_MSK;
68}
69
Patrick Delaunayf13ff882020-06-04 14:30:25 +020070static void stm32_gpio_set_pupd(struct stm32_gpio_regs *regs,
71 int idx,
72 enum stm32_gpio_pupd pupd)
73{
74 int bits;
75
76 bits = PUPD_BITS(idx);
77 clrsetbits_le32(&regs->pupdr, PUPD_MASK << bits, pupd << bits);
78}
Vikas Manocha77417102017-04-10 15:02:57 -070079
Patrick Delaunay43efbb62020-06-04 14:30:26 +020080static enum stm32_gpio_pupd stm32_gpio_get_pupd(struct stm32_gpio_regs *regs,
81 int idx)
82{
83 return (readl(&regs->pupdr) >> PUPD_BITS(idx)) & PUPD_MASK;
84}
85
Patrice Chotard427f4522022-04-22 09:38:31 +020086static bool stm32_gpio_is_mapped(struct udevice *dev, int offset)
Patrice Chotarddbf928d2018-12-03 10:52:51 +010087{
88 struct stm32_gpio_priv *priv = dev_get_priv(dev);
Patrice Chotarddbf928d2018-12-03 10:52:51 +010089
Patrice Chotard427f4522022-04-22 09:38:31 +020090 return !!(priv->gpio_range & BIT(offset));
Patrice Chotarddbf928d2018-12-03 10:52:51 +010091}
92
Vikas Manocha77417102017-04-10 15:02:57 -070093static int stm32_gpio_direction_input(struct udevice *dev, unsigned offset)
94{
95 struct stm32_gpio_priv *priv = dev_get_priv(dev);
96 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +010097
Patrice Chotard427f4522022-04-22 09:38:31 +020098 if (!stm32_gpio_is_mapped(dev, offset))
99 return -ENXIO;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100100
Patrice Chotard427f4522022-04-22 09:38:31 +0200101 stm32_gpio_set_moder(regs, offset, STM32_GPIO_MODE_IN);
Vikas Manocha77417102017-04-10 15:02:57 -0700102
103 return 0;
104}
105
106static int stm32_gpio_direction_output(struct udevice *dev, unsigned offset,
107 int value)
108{
109 struct stm32_gpio_priv *priv = dev_get_priv(dev);
110 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100111
Patrice Chotard427f4522022-04-22 09:38:31 +0200112 if (!stm32_gpio_is_mapped(dev, offset))
113 return -ENXIO;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100114
Patrice Chotard427f4522022-04-22 09:38:31 +0200115 stm32_gpio_set_moder(regs, offset, STM32_GPIO_MODE_OUT);
Patrice Chotard798cd702018-08-09 11:57:57 +0200116
Patrice Chotard427f4522022-04-22 09:38:31 +0200117 writel(BSRR_BIT(offset, value), &regs->bsrr);
Vikas Manocha77417102017-04-10 15:02:57 -0700118
119 return 0;
120}
121
122static int stm32_gpio_get_value(struct udevice *dev, unsigned offset)
123{
124 struct stm32_gpio_priv *priv = dev_get_priv(dev);
125 struct stm32_gpio_regs *regs = priv->regs;
126
Patrice Chotard427f4522022-04-22 09:38:31 +0200127 if (!stm32_gpio_is_mapped(dev, offset))
128 return -ENXIO;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100129
Patrice Chotard427f4522022-04-22 09:38:31 +0200130 return readl(&regs->idr) & BIT(offset) ? 1 : 0;
Vikas Manocha77417102017-04-10 15:02:57 -0700131}
132
133static int stm32_gpio_set_value(struct udevice *dev, unsigned offset, int value)
134{
135 struct stm32_gpio_priv *priv = dev_get_priv(dev);
136 struct stm32_gpio_regs *regs = priv->regs;
Vikas Manocha77417102017-04-10 15:02:57 -0700137
Patrice Chotard427f4522022-04-22 09:38:31 +0200138 if (!stm32_gpio_is_mapped(dev, offset))
139 return -ENXIO;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100140
Patrice Chotard427f4522022-04-22 09:38:31 +0200141 writel(BSRR_BIT(offset, value), &regs->bsrr);
Vikas Manocha77417102017-04-10 15:02:57 -0700142
143 return 0;
144}
145
Patrice Chotardcad73242018-10-24 14:10:21 +0200146static int stm32_gpio_get_function(struct udevice *dev, unsigned int offset)
147{
148 struct stm32_gpio_priv *priv = dev_get_priv(dev);
149 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100150 int bits_index;
151 int mask;
Patrice Chotardcad73242018-10-24 14:10:21 +0200152 u32 mode;
153
Patrice Chotard427f4522022-04-22 09:38:31 +0200154 if (!stm32_gpio_is_mapped(dev, offset))
155 return GPIOF_UNKNOWN;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100156
Patrice Chotard427f4522022-04-22 09:38:31 +0200157 bits_index = MODE_BITS(offset);
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100158 mask = MODE_BITS_MASK << bits_index;
159
Patrice Chotardcad73242018-10-24 14:10:21 +0200160 mode = (readl(&regs->moder) & mask) >> bits_index;
161 if (mode == STM32_GPIO_MODE_OUT)
162 return GPIOF_OUTPUT;
163 if (mode == STM32_GPIO_MODE_IN)
164 return GPIOF_INPUT;
165 if (mode == STM32_GPIO_MODE_AN)
166 return GPIOF_UNUSED;
167
168 return GPIOF_FUNC;
169}
170
Simon Glass13979fc2021-02-04 21:21:55 -0700171static int stm32_gpio_set_flags(struct udevice *dev, unsigned int offset,
172 ulong flags)
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200173{
174 struct stm32_gpio_priv *priv = dev_get_priv(dev);
175 struct stm32_gpio_regs *regs = priv->regs;
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200176
Patrice Chotard427f4522022-04-22 09:38:31 +0200177 if (!stm32_gpio_is_mapped(dev, offset))
178 return -ENXIO;
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200179
180 if (flags & GPIOD_IS_OUT) {
Simon Glass7e0a96d2021-02-04 21:22:03 -0700181 bool value = flags & GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200182
183 if (flags & GPIOD_OPEN_DRAIN)
Patrice Chotard427f4522022-04-22 09:38:31 +0200184 stm32_gpio_set_otype(regs, offset, STM32_GPIO_OTYPE_OD);
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200185 else
Patrice Chotard427f4522022-04-22 09:38:31 +0200186 stm32_gpio_set_otype(regs, offset, STM32_GPIO_OTYPE_PP);
Simon Glass7e0a96d2021-02-04 21:22:03 -0700187
Patrice Chotard427f4522022-04-22 09:38:31 +0200188 stm32_gpio_set_moder(regs, offset, STM32_GPIO_MODE_OUT);
189 writel(BSRR_BIT(offset, value), &regs->bsrr);
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200190
191 } else if (flags & GPIOD_IS_IN) {
Patrice Chotard427f4522022-04-22 09:38:31 +0200192 stm32_gpio_set_moder(regs, offset, STM32_GPIO_MODE_IN);
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200193 }
Patrick Delaunay2c6df942020-10-28 10:49:08 +0100194 if (flags & GPIOD_PULL_UP)
Patrice Chotard427f4522022-04-22 09:38:31 +0200195 stm32_gpio_set_pupd(regs, offset, STM32_GPIO_PUPD_UP);
Patrick Delaunay2c6df942020-10-28 10:49:08 +0100196 else if (flags & GPIOD_PULL_DOWN)
Patrice Chotard427f4522022-04-22 09:38:31 +0200197 stm32_gpio_set_pupd(regs, offset, STM32_GPIO_PUPD_DOWN);
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200198
199 return 0;
200}
201
Simon Glass96487892021-02-04 21:21:56 -0700202static int stm32_gpio_get_flags(struct udevice *dev, unsigned int offset,
203 ulong *flagsp)
Patrick Delaunay43efbb62020-06-04 14:30:26 +0200204{
205 struct stm32_gpio_priv *priv = dev_get_priv(dev);
206 struct stm32_gpio_regs *regs = priv->regs;
Patrick Delaunay43efbb62020-06-04 14:30:26 +0200207 ulong dir_flags = 0;
208
Patrice Chotard427f4522022-04-22 09:38:31 +0200209 if (!stm32_gpio_is_mapped(dev, offset))
210 return -ENXIO;
Patrick Delaunay43efbb62020-06-04 14:30:26 +0200211
Patrice Chotard427f4522022-04-22 09:38:31 +0200212 switch (stm32_gpio_get_moder(regs, offset)) {
Patrick Delaunay43efbb62020-06-04 14:30:26 +0200213 case STM32_GPIO_MODE_OUT:
214 dir_flags |= GPIOD_IS_OUT;
Patrice Chotard427f4522022-04-22 09:38:31 +0200215 if (stm32_gpio_get_otype(regs, offset) == STM32_GPIO_OTYPE_OD)
Patrick Delaunay43efbb62020-06-04 14:30:26 +0200216 dir_flags |= GPIOD_OPEN_DRAIN;
Patrice Chotard427f4522022-04-22 09:38:31 +0200217 if (readl(&regs->idr) & BIT(offset))
Patrick Delaunay43efbb62020-06-04 14:30:26 +0200218 dir_flags |= GPIOD_IS_OUT_ACTIVE;
219 break;
220 case STM32_GPIO_MODE_IN:
221 dir_flags |= GPIOD_IS_IN;
Patrick Delaunay2c6df942020-10-28 10:49:08 +0100222 break;
223 default:
224 break;
225 }
Patrice Chotard427f4522022-04-22 09:38:31 +0200226 switch (stm32_gpio_get_pupd(regs, offset)) {
Patrick Delaunay2c6df942020-10-28 10:49:08 +0100227 case STM32_GPIO_PUPD_UP:
228 dir_flags |= GPIOD_PULL_UP;
229 break;
230 case STM32_GPIO_PUPD_DOWN:
231 dir_flags |= GPIOD_PULL_DOWN;
Patrick Delaunay43efbb62020-06-04 14:30:26 +0200232 break;
233 default:
234 break;
235 }
Simon Glass96487892021-02-04 21:21:56 -0700236 *flagsp = dir_flags;
Patrick Delaunay43efbb62020-06-04 14:30:26 +0200237
238 return 0;
239}
240
Vikas Manocha77417102017-04-10 15:02:57 -0700241static const struct dm_gpio_ops gpio_stm32_ops = {
242 .direction_input = stm32_gpio_direction_input,
243 .direction_output = stm32_gpio_direction_output,
244 .get_value = stm32_gpio_get_value,
245 .set_value = stm32_gpio_set_value,
Patrice Chotardcad73242018-10-24 14:10:21 +0200246 .get_function = stm32_gpio_get_function,
Simon Glass13979fc2021-02-04 21:21:55 -0700247 .set_flags = stm32_gpio_set_flags,
Simon Glass96487892021-02-04 21:21:56 -0700248 .get_flags = stm32_gpio_get_flags,
Vikas Manocha77417102017-04-10 15:02:57 -0700249};
250
251static int gpio_stm32_probe(struct udevice *dev)
252{
Vikas Manocha77417102017-04-10 15:02:57 -0700253 struct stm32_gpio_priv *priv = dev_get_priv(dev);
Patrick Delaunay15c8cbf2020-09-09 18:28:33 +0200254 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
255 struct ofnode_phandle_args args;
256 const char *name;
Patrice Chotard8b6d45a2018-12-03 10:52:53 +0100257 struct clk clk;
Vikas Manocha77417102017-04-10 15:02:57 -0700258 fdt_addr_t addr;
Patrick Delaunay15c8cbf2020-09-09 18:28:33 +0200259 int ret, i;
Vikas Manocha77417102017-04-10 15:02:57 -0700260
Patrick Delaunayd876eaf2018-03-12 10:46:07 +0100261 addr = dev_read_addr(dev);
Vikas Manocha77417102017-04-10 15:02:57 -0700262 if (addr == FDT_ADDR_T_NONE)
263 return -EINVAL;
264
265 priv->regs = (struct stm32_gpio_regs *)addr;
Patrice Chotard4fb22462019-01-04 10:55:06 +0100266
Patrick Delaunayd876eaf2018-03-12 10:46:07 +0100267 name = dev_read_string(dev, "st,bank-name");
Vikas Manocha77417102017-04-10 15:02:57 -0700268 if (!name)
269 return -EINVAL;
270 uc_priv->bank_name = name;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100271
272 i = 0;
273 ret = dev_read_phandle_with_args(dev, "gpio-ranges",
274 NULL, 3, i, &args);
275
Patrick Delaunaycb08e842020-09-09 18:28:34 +0200276 if (!ret && args.args_count < 3)
277 return -EINVAL;
278
Patrice Chotard427f4522022-04-22 09:38:31 +0200279 uc_priv->gpio_count = STM32_GPIOS_PER_BANK;
280 if (ret == -ENOENT)
Patrice Chotard39a8f0b2019-01-04 10:55:05 +0100281 priv->gpio_range = GENMASK(STM32_GPIOS_PER_BANK - 1, 0);
Patrice Chotard39a8f0b2019-01-04 10:55:05 +0100282
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100283 while (ret != -ENOENT) {
284 priv->gpio_range |= GENMASK(args.args[2] + args.args[0] - 1,
285 args.args[0]);
286
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100287 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
288 ++i, &args);
Patrick Delaunaycb08e842020-09-09 18:28:34 +0200289 if (!ret && args.args_count < 3)
290 return -EINVAL;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100291 }
292
293 dev_dbg(dev, "addr = 0x%p bank_name = %s gpio_count = %d gpio_range = 0x%x\n",
294 (u32 *)priv->regs, uc_priv->bank_name, uc_priv->gpio_count,
295 priv->gpio_range);
Patrick Delaunayf17412e2020-04-22 14:29:17 +0200296
Vikas Manocha77417102017-04-10 15:02:57 -0700297 ret = clk_get_by_index(dev, 0, &clk);
298 if (ret < 0)
299 return ret;
300
301 ret = clk_enable(&clk);
302
303 if (ret) {
304 dev_err(dev, "failed to enable clock\n");
305 return ret;
306 }
Patrick Delaunay6dd89d92020-11-06 19:01:33 +0100307 dev_dbg(dev, "clock enabled\n");
Vikas Manocha77417102017-04-10 15:02:57 -0700308
309 return 0;
310}
311
Vikas Manocha77417102017-04-10 15:02:57 -0700312U_BOOT_DRIVER(gpio_stm32) = {
313 .name = "gpio_stm32",
314 .id = UCLASS_GPIO,
Vikas Manocha77417102017-04-10 15:02:57 -0700315 .probe = gpio_stm32_probe,
316 .ops = &gpio_stm32_ops,
Bin Meng695c4992018-10-24 06:36:30 -0700317 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass41575d82020-12-03 16:55:17 -0700318 .priv_auto = sizeof(struct stm32_gpio_priv),
Vikas Manocha77417102017-04-10 15:02:57 -0700319};