blob: b8eb55465d3c2d83a988c699658035b4a4ce92b2 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vikas Manochaec8630a2017-04-10 15:02:57 -07002/*
Patrice Chotard789ee0e2017-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 Manochaec8630a2017-04-10 15:02:57 -07005 */
6
Patrick Delaunay09e325e2020-11-06 19:01:33 +01007#define LOG_CATEGORY UCLASS_GPIO
8
Vikas Manochaec8630a2017-04-10 15:02:57 -07009#include <clk.h>
10#include <dm.h>
11#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Vikas Manochaec8630a2017-04-10 15:02:57 -070013#include <asm/arch/stm32.h>
14#include <asm/gpio.h>
15#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Vikas Manochaec8630a2017-04-10 15:02:57 -070018#include <linux/errno.h>
19#include <linux/io.h>
20
Patrick Delaunay7dccd892021-10-22 20:12:34 +020021#include "stm32_gpio_priv.h"
22
Patrick Delaunaycf6da852020-10-02 14:08:54 +020023#define STM32_GPIOS_PER_BANK 16
24
Patrick Delaunayc6d455c2020-06-04 14:30:25 +020025#define MODE_BITS(gpio_pin) ((gpio_pin) * 2)
Vikas Manochaec8630a2017-04-10 15:02:57 -070026#define MODE_BITS_MASK 3
Patrick Delaunayc6d455c2020-06-04 14:30:25 +020027#define BSRR_BIT(gpio_pin, value) BIT((gpio_pin) + (value ? 0 : 16))
28
29#define PUPD_BITS(gpio_pin) ((gpio_pin) * 2)
30#define PUPD_MASK 3
31
32#define OTYPE_BITS(gpio_pin) (gpio_pin)
33#define OTYPE_MSK 1
34
35static void stm32_gpio_set_moder(struct stm32_gpio_regs *regs,
36 int idx,
37 int mode)
38{
39 int bits_index;
40 int mask;
41
42 bits_index = MODE_BITS(idx);
43 mask = MODE_BITS_MASK << bits_index;
44
45 clrsetbits_le32(&regs->moder, mask, mode << bits_index);
46}
47
Patrick Delaunayb087cab2020-06-04 14:30:26 +020048static int stm32_gpio_get_moder(struct stm32_gpio_regs *regs, int idx)
49{
50 return (readl(&regs->moder) >> MODE_BITS(idx)) & MODE_BITS_MASK;
51}
52
Patrick Delaunayc6d455c2020-06-04 14:30:25 +020053static void stm32_gpio_set_otype(struct stm32_gpio_regs *regs,
54 int idx,
55 enum stm32_gpio_otype otype)
56{
57 int bits;
58
59 bits = OTYPE_BITS(idx);
60 clrsetbits_le32(&regs->otyper, OTYPE_MSK << bits, otype << bits);
61}
62
Patrick Delaunayb087cab2020-06-04 14:30:26 +020063static enum stm32_gpio_otype stm32_gpio_get_otype(struct stm32_gpio_regs *regs,
64 int idx)
65{
66 return (readl(&regs->otyper) >> OTYPE_BITS(idx)) & OTYPE_MSK;
67}
68
Patrick Delaunayc6d455c2020-06-04 14:30:25 +020069static void stm32_gpio_set_pupd(struct stm32_gpio_regs *regs,
70 int idx,
71 enum stm32_gpio_pupd pupd)
72{
73 int bits;
74
75 bits = PUPD_BITS(idx);
76 clrsetbits_le32(&regs->pupdr, PUPD_MASK << bits, pupd << bits);
77}
Vikas Manochaec8630a2017-04-10 15:02:57 -070078
Patrick Delaunayb087cab2020-06-04 14:30:26 +020079static enum stm32_gpio_pupd stm32_gpio_get_pupd(struct stm32_gpio_regs *regs,
80 int idx)
81{
82 return (readl(&regs->pupdr) >> PUPD_BITS(idx)) & PUPD_MASK;
83}
84
Patrice Chotard5554a542022-04-22 09:38:31 +020085static bool stm32_gpio_is_mapped(struct udevice *dev, int offset)
Patrice Chotard0099c1e2018-12-03 10:52:51 +010086{
87 struct stm32_gpio_priv *priv = dev_get_priv(dev);
Patrice Chotard0099c1e2018-12-03 10:52:51 +010088
Patrice Chotard5554a542022-04-22 09:38:31 +020089 return !!(priv->gpio_range & BIT(offset));
Patrice Chotard0099c1e2018-12-03 10:52:51 +010090}
91
Vikas Manochaec8630a2017-04-10 15:02:57 -070092static int stm32_gpio_direction_input(struct udevice *dev, unsigned offset)
93{
94 struct stm32_gpio_priv *priv = dev_get_priv(dev);
95 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotard0099c1e2018-12-03 10:52:51 +010096
Patrice Chotard5554a542022-04-22 09:38:31 +020097 if (!stm32_gpio_is_mapped(dev, offset))
98 return -ENXIO;
Patrice Chotard0099c1e2018-12-03 10:52:51 +010099
Patrice Chotard5554a542022-04-22 09:38:31 +0200100 stm32_gpio_set_moder(regs, offset, STM32_GPIO_MODE_IN);
Vikas Manochaec8630a2017-04-10 15:02:57 -0700101
102 return 0;
103}
104
105static int stm32_gpio_direction_output(struct udevice *dev, unsigned offset,
106 int value)
107{
108 struct stm32_gpio_priv *priv = dev_get_priv(dev);
109 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotard0099c1e2018-12-03 10:52:51 +0100110
Patrice Chotard5554a542022-04-22 09:38:31 +0200111 if (!stm32_gpio_is_mapped(dev, offset))
112 return -ENXIO;
Patrice Chotard0099c1e2018-12-03 10:52:51 +0100113
Patrice Chotard5554a542022-04-22 09:38:31 +0200114 stm32_gpio_set_moder(regs, offset, STM32_GPIO_MODE_OUT);
Patrice Chotard4e915002018-08-09 11:57:57 +0200115
Patrice Chotard5554a542022-04-22 09:38:31 +0200116 writel(BSRR_BIT(offset, value), &regs->bsrr);
Vikas Manochaec8630a2017-04-10 15:02:57 -0700117
118 return 0;
119}
120
121static int stm32_gpio_get_value(struct udevice *dev, unsigned offset)
122{
123 struct stm32_gpio_priv *priv = dev_get_priv(dev);
124 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotard0099c1e2018-12-03 10:52:51 +0100125
Patrice Chotard5554a542022-04-22 09:38:31 +0200126 if (!stm32_gpio_is_mapped(dev, offset))
127 return -ENXIO;
Vikas Manochaec8630a2017-04-10 15:02:57 -0700128
Patrice Chotard5554a542022-04-22 09:38:31 +0200129 return readl(&regs->idr) & BIT(offset) ? 1 : 0;
Vikas Manochaec8630a2017-04-10 15:02:57 -0700130}
131
132static int stm32_gpio_set_value(struct udevice *dev, unsigned offset, int value)
133{
134 struct stm32_gpio_priv *priv = dev_get_priv(dev);
135 struct stm32_gpio_regs *regs = priv->regs;
Vikas Manochaec8630a2017-04-10 15:02:57 -0700136
Patrice Chotard5554a542022-04-22 09:38:31 +0200137 if (!stm32_gpio_is_mapped(dev, offset))
138 return -ENXIO;
Patrice Chotard0099c1e2018-12-03 10:52:51 +0100139
Patrice Chotard5554a542022-04-22 09:38:31 +0200140 writel(BSRR_BIT(offset, value), &regs->bsrr);
Vikas Manochaec8630a2017-04-10 15:02:57 -0700141
142 return 0;
143}
144
Patrice Chotard10561232018-10-24 14:10:21 +0200145static int stm32_gpio_get_function(struct udevice *dev, unsigned int offset)
146{
147 struct stm32_gpio_priv *priv = dev_get_priv(dev);
148 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotard0099c1e2018-12-03 10:52:51 +0100149 int bits_index;
150 int mask;
Patrice Chotard10561232018-10-24 14:10:21 +0200151 u32 mode;
152
Patrice Chotard5554a542022-04-22 09:38:31 +0200153 if (!stm32_gpio_is_mapped(dev, offset))
154 return GPIOF_UNKNOWN;
Patrice Chotard0099c1e2018-12-03 10:52:51 +0100155
Patrice Chotard5554a542022-04-22 09:38:31 +0200156 bits_index = MODE_BITS(offset);
Patrice Chotard0099c1e2018-12-03 10:52:51 +0100157 mask = MODE_BITS_MASK << bits_index;
158
Patrice Chotard10561232018-10-24 14:10:21 +0200159 mode = (readl(&regs->moder) & mask) >> bits_index;
160 if (mode == STM32_GPIO_MODE_OUT)
161 return GPIOF_OUTPUT;
162 if (mode == STM32_GPIO_MODE_IN)
163 return GPIOF_INPUT;
164 if (mode == STM32_GPIO_MODE_AN)
165 return GPIOF_UNUSED;
166
167 return GPIOF_FUNC;
168}
169
Simon Glass54befdd2021-02-04 21:21:55 -0700170static int stm32_gpio_set_flags(struct udevice *dev, unsigned int offset,
171 ulong flags)
Patrick Delaunayc6d455c2020-06-04 14:30:25 +0200172{
173 struct stm32_gpio_priv *priv = dev_get_priv(dev);
174 struct stm32_gpio_regs *regs = priv->regs;
Patrick Delaunayc6d455c2020-06-04 14:30:25 +0200175
Patrice Chotard5554a542022-04-22 09:38:31 +0200176 if (!stm32_gpio_is_mapped(dev, offset))
177 return -ENXIO;
Patrick Delaunayc6d455c2020-06-04 14:30:25 +0200178
179 if (flags & GPIOD_IS_OUT) {
Simon Glass7b893f92021-02-04 21:22:03 -0700180 bool value = flags & GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayc6d455c2020-06-04 14:30:25 +0200181
182 if (flags & GPIOD_OPEN_DRAIN)
Patrice Chotard5554a542022-04-22 09:38:31 +0200183 stm32_gpio_set_otype(regs, offset, STM32_GPIO_OTYPE_OD);
Patrick Delaunayc6d455c2020-06-04 14:30:25 +0200184 else
Patrice Chotard5554a542022-04-22 09:38:31 +0200185 stm32_gpio_set_otype(regs, offset, STM32_GPIO_OTYPE_PP);
Simon Glass7b893f92021-02-04 21:22:03 -0700186
Patrice Chotard5554a542022-04-22 09:38:31 +0200187 stm32_gpio_set_moder(regs, offset, STM32_GPIO_MODE_OUT);
188 writel(BSRR_BIT(offset, value), &regs->bsrr);
Patrick Delaunayc6d455c2020-06-04 14:30:25 +0200189
190 } else if (flags & GPIOD_IS_IN) {
Patrice Chotard5554a542022-04-22 09:38:31 +0200191 stm32_gpio_set_moder(regs, offset, STM32_GPIO_MODE_IN);
Patrick Delaunayc6d455c2020-06-04 14:30:25 +0200192 }
Patrick Delaunayf60bc022020-10-28 10:49:08 +0100193 if (flags & GPIOD_PULL_UP)
Patrice Chotard5554a542022-04-22 09:38:31 +0200194 stm32_gpio_set_pupd(regs, offset, STM32_GPIO_PUPD_UP);
Patrick Delaunayf60bc022020-10-28 10:49:08 +0100195 else if (flags & GPIOD_PULL_DOWN)
Patrice Chotard5554a542022-04-22 09:38:31 +0200196 stm32_gpio_set_pupd(regs, offset, STM32_GPIO_PUPD_DOWN);
Patrick Delaunayb087cab2020-06-04 14:30:26 +0200197
198 return 0;
199}
200
Simon Glassd063ce92021-02-04 21:21:56 -0700201static int stm32_gpio_get_flags(struct udevice *dev, unsigned int offset,
202 ulong *flagsp)
Patrick Delaunayb087cab2020-06-04 14:30:26 +0200203{
204 struct stm32_gpio_priv *priv = dev_get_priv(dev);
205 struct stm32_gpio_regs *regs = priv->regs;
Patrick Delaunayb087cab2020-06-04 14:30:26 +0200206 ulong dir_flags = 0;
207
Patrice Chotard5554a542022-04-22 09:38:31 +0200208 if (!stm32_gpio_is_mapped(dev, offset))
209 return -ENXIO;
Patrick Delaunayb087cab2020-06-04 14:30:26 +0200210
Patrice Chotard5554a542022-04-22 09:38:31 +0200211 switch (stm32_gpio_get_moder(regs, offset)) {
Patrick Delaunayb087cab2020-06-04 14:30:26 +0200212 case STM32_GPIO_MODE_OUT:
213 dir_flags |= GPIOD_IS_OUT;
Patrice Chotard5554a542022-04-22 09:38:31 +0200214 if (stm32_gpio_get_otype(regs, offset) == STM32_GPIO_OTYPE_OD)
Patrick Delaunayb087cab2020-06-04 14:30:26 +0200215 dir_flags |= GPIOD_OPEN_DRAIN;
Patrice Chotard5554a542022-04-22 09:38:31 +0200216 if (readl(&regs->idr) & BIT(offset))
Patrick Delaunayb087cab2020-06-04 14:30:26 +0200217 dir_flags |= GPIOD_IS_OUT_ACTIVE;
218 break;
219 case STM32_GPIO_MODE_IN:
220 dir_flags |= GPIOD_IS_IN;
Patrick Delaunayf60bc022020-10-28 10:49:08 +0100221 break;
222 default:
223 break;
224 }
Patrice Chotard5554a542022-04-22 09:38:31 +0200225 switch (stm32_gpio_get_pupd(regs, offset)) {
Patrick Delaunayf60bc022020-10-28 10:49:08 +0100226 case STM32_GPIO_PUPD_UP:
227 dir_flags |= GPIOD_PULL_UP;
228 break;
229 case STM32_GPIO_PUPD_DOWN:
230 dir_flags |= GPIOD_PULL_DOWN;
Patrick Delaunayb087cab2020-06-04 14:30:26 +0200231 break;
232 default:
233 break;
234 }
Simon Glassd063ce92021-02-04 21:21:56 -0700235 *flagsp = dir_flags;
Patrick Delaunayc6d455c2020-06-04 14:30:25 +0200236
237 return 0;
238}
239
Vikas Manochaec8630a2017-04-10 15:02:57 -0700240static const struct dm_gpio_ops gpio_stm32_ops = {
241 .direction_input = stm32_gpio_direction_input,
242 .direction_output = stm32_gpio_direction_output,
243 .get_value = stm32_gpio_get_value,
244 .set_value = stm32_gpio_set_value,
Patrice Chotard10561232018-10-24 14:10:21 +0200245 .get_function = stm32_gpio_get_function,
Simon Glass54befdd2021-02-04 21:21:55 -0700246 .set_flags = stm32_gpio_set_flags,
Simon Glassd063ce92021-02-04 21:21:56 -0700247 .get_flags = stm32_gpio_get_flags,
Vikas Manochaec8630a2017-04-10 15:02:57 -0700248};
249
250static int gpio_stm32_probe(struct udevice *dev)
251{
Vikas Manochaec8630a2017-04-10 15:02:57 -0700252 struct stm32_gpio_priv *priv = dev_get_priv(dev);
Patrick Delaunaycd7c9512020-09-09 18:28:33 +0200253 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
254 struct ofnode_phandle_args args;
255 const char *name;
Patrice Chotard159d1572018-12-03 10:52:53 +0100256 struct clk clk;
Vikas Manochaec8630a2017-04-10 15:02:57 -0700257 fdt_addr_t addr;
Patrick Delaunaycd7c9512020-09-09 18:28:33 +0200258 int ret, i;
Vikas Manochaec8630a2017-04-10 15:02:57 -0700259
Patrick Delaunayd78f9682018-03-12 10:46:07 +0100260 addr = dev_read_addr(dev);
Vikas Manochaec8630a2017-04-10 15:02:57 -0700261 if (addr == FDT_ADDR_T_NONE)
262 return -EINVAL;
263
264 priv->regs = (struct stm32_gpio_regs *)addr;
Patrice Chotard9f62b082019-01-04 10:55:06 +0100265
Patrick Delaunayd78f9682018-03-12 10:46:07 +0100266 name = dev_read_string(dev, "st,bank-name");
Vikas Manochaec8630a2017-04-10 15:02:57 -0700267 if (!name)
268 return -EINVAL;
269 uc_priv->bank_name = name;
Patrice Chotard0099c1e2018-12-03 10:52:51 +0100270
271 i = 0;
272 ret = dev_read_phandle_with_args(dev, "gpio-ranges",
273 NULL, 3, i, &args);
274
Patrick Delaunaye7f66382020-09-09 18:28:34 +0200275 if (!ret && args.args_count < 3)
276 return -EINVAL;
277
Patrice Chotard5554a542022-04-22 09:38:31 +0200278 uc_priv->gpio_count = STM32_GPIOS_PER_BANK;
279 if (ret == -ENOENT)
Patrice Chotard62253052019-01-04 10:55:05 +0100280 priv->gpio_range = GENMASK(STM32_GPIOS_PER_BANK - 1, 0);
Patrice Chotard62253052019-01-04 10:55:05 +0100281
Patrice Chotard0099c1e2018-12-03 10:52:51 +0100282 while (ret != -ENOENT) {
283 priv->gpio_range |= GENMASK(args.args[2] + args.args[0] - 1,
284 args.args[0]);
285
Patrice Chotard0099c1e2018-12-03 10:52:51 +0100286 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
287 ++i, &args);
Patrick Delaunaye7f66382020-09-09 18:28:34 +0200288 if (!ret && args.args_count < 3)
289 return -EINVAL;
Patrice Chotard0099c1e2018-12-03 10:52:51 +0100290 }
291
292 dev_dbg(dev, "addr = 0x%p bank_name = %s gpio_count = %d gpio_range = 0x%x\n",
293 (u32 *)priv->regs, uc_priv->bank_name, uc_priv->gpio_count,
294 priv->gpio_range);
Patrick Delaunayb1c60142020-04-22 14:29:17 +0200295
Vikas Manochaec8630a2017-04-10 15:02:57 -0700296 ret = clk_get_by_index(dev, 0, &clk);
297 if (ret < 0)
298 return ret;
299
300 ret = clk_enable(&clk);
301
302 if (ret) {
303 dev_err(dev, "failed to enable clock\n");
304 return ret;
305 }
Patrick Delaunay09e325e2020-11-06 19:01:33 +0100306 dev_dbg(dev, "clock enabled\n");
Vikas Manochaec8630a2017-04-10 15:02:57 -0700307
308 return 0;
309}
310
Vikas Manochaec8630a2017-04-10 15:02:57 -0700311U_BOOT_DRIVER(gpio_stm32) = {
312 .name = "gpio_stm32",
313 .id = UCLASS_GPIO,
Vikas Manochaec8630a2017-04-10 15:02:57 -0700314 .probe = gpio_stm32_probe,
315 .ops = &gpio_stm32_ops,
Bin Mengb508ee52018-10-24 06:36:30 -0700316 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700317 .priv_auto = sizeof(struct stm32_gpio_priv),
Vikas Manochaec8630a2017-04-10 15:02:57 -0700318};