blob: 125c237551cae5c59d04c52bafbf5713006b209c [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 <common.h>
10#include <clk.h>
11#include <dm.h>
12#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Vikas Manochaec8630a2017-04-10 15:02:57 -070014#include <asm/arch/gpio.h>
15#include <asm/arch/stm32.h>
16#include <asm/gpio.h>
17#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070018#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060019#include <linux/bitops.h>
Vikas Manochaec8630a2017-04-10 15:02:57 -070020#include <linux/errno.h>
21#include <linux/io.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 Chotard0099c1e2018-12-03 10:52:51 +010085/*
86 * convert gpio offset to gpio index taking into account gpio holes
87 * into gpio bank
88 */
89int stm32_offset_to_index(struct udevice *dev, unsigned int offset)
90{
91 struct stm32_gpio_priv *priv = dev_get_priv(dev);
Patrick Delaunay2a6c7ad2019-06-21 15:26:46 +020092 unsigned int idx = 0;
Patrice Chotard0099c1e2018-12-03 10:52:51 +010093 int i;
94
95 for (i = 0; i < STM32_GPIOS_PER_BANK; i++) {
96 if (priv->gpio_range & BIT(i)) {
97 if (idx == offset)
98 return idx;
99 idx++;
100 }
101 }
102 /* shouldn't happen */
103 return -EINVAL;
104}
105
Vikas Manochaec8630a2017-04-10 15:02:57 -0700106static int stm32_gpio_direction_input(struct udevice *dev, unsigned offset)
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 int idx;
111
112 idx = stm32_offset_to_index(dev, offset);
113 if (idx < 0)
114 return idx;
115
Patrick Delaunayc6d455c2020-06-04 14:30:25 +0200116 stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_IN);
Vikas Manochaec8630a2017-04-10 15:02:57 -0700117
118 return 0;
119}
120
121static int stm32_gpio_direction_output(struct udevice *dev, unsigned offset,
122 int value)
123{
124 struct stm32_gpio_priv *priv = dev_get_priv(dev);
125 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotard0099c1e2018-12-03 10:52:51 +0100126 int idx;
127
128 idx = stm32_offset_to_index(dev, offset);
129 if (idx < 0)
130 return idx;
131
Patrick Delaunayc6d455c2020-06-04 14:30:25 +0200132 stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_OUT);
Patrice Chotard4e915002018-08-09 11:57:57 +0200133
Patrice Chotard0099c1e2018-12-03 10:52:51 +0100134 writel(BSRR_BIT(idx, value), &regs->bsrr);
Vikas Manochaec8630a2017-04-10 15:02:57 -0700135
136 return 0;
137}
138
139static int stm32_gpio_get_value(struct udevice *dev, unsigned offset)
140{
141 struct stm32_gpio_priv *priv = dev_get_priv(dev);
142 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotard0099c1e2018-12-03 10:52:51 +0100143 int idx;
144
145 idx = stm32_offset_to_index(dev, offset);
146 if (idx < 0)
147 return idx;
Vikas Manochaec8630a2017-04-10 15:02:57 -0700148
Patrice Chotard0099c1e2018-12-03 10:52:51 +0100149 return readl(&regs->idr) & BIT(idx) ? 1 : 0;
Vikas Manochaec8630a2017-04-10 15:02:57 -0700150}
151
152static int stm32_gpio_set_value(struct udevice *dev, unsigned offset, int value)
153{
154 struct stm32_gpio_priv *priv = dev_get_priv(dev);
155 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotard0099c1e2018-12-03 10:52:51 +0100156 int idx;
Vikas Manochaec8630a2017-04-10 15:02:57 -0700157
Patrice Chotard0099c1e2018-12-03 10:52:51 +0100158 idx = stm32_offset_to_index(dev, offset);
159 if (idx < 0)
160 return idx;
161
162 writel(BSRR_BIT(idx, value), &regs->bsrr);
Vikas Manochaec8630a2017-04-10 15:02:57 -0700163
164 return 0;
165}
166
Patrice Chotard10561232018-10-24 14:10:21 +0200167static int stm32_gpio_get_function(struct udevice *dev, unsigned int offset)
168{
169 struct stm32_gpio_priv *priv = dev_get_priv(dev);
170 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotard0099c1e2018-12-03 10:52:51 +0100171 int bits_index;
172 int mask;
173 int idx;
Patrice Chotard10561232018-10-24 14:10:21 +0200174 u32 mode;
175
Patrice Chotard0099c1e2018-12-03 10:52:51 +0100176 idx = stm32_offset_to_index(dev, offset);
177 if (idx < 0)
178 return idx;
179
180 bits_index = MODE_BITS(idx);
181 mask = MODE_BITS_MASK << bits_index;
182
Patrice Chotard10561232018-10-24 14:10:21 +0200183 mode = (readl(&regs->moder) & mask) >> bits_index;
184 if (mode == STM32_GPIO_MODE_OUT)
185 return GPIOF_OUTPUT;
186 if (mode == STM32_GPIO_MODE_IN)
187 return GPIOF_INPUT;
188 if (mode == STM32_GPIO_MODE_AN)
189 return GPIOF_UNUSED;
190
191 return GPIOF_FUNC;
192}
193
Simon Glass54befdd2021-02-04 21:21:55 -0700194static int stm32_gpio_set_flags(struct udevice *dev, unsigned int offset,
195 ulong flags)
Patrick Delaunayc6d455c2020-06-04 14:30:25 +0200196{
197 struct stm32_gpio_priv *priv = dev_get_priv(dev);
198 struct stm32_gpio_regs *regs = priv->regs;
199 int idx;
200
201 idx = stm32_offset_to_index(dev, offset);
202 if (idx < 0)
203 return idx;
204
205 if (flags & GPIOD_IS_OUT) {
Simon Glass7b893f92021-02-04 21:22:03 -0700206 bool value = flags & GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayc6d455c2020-06-04 14:30:25 +0200207
208 if (flags & GPIOD_OPEN_DRAIN)
209 stm32_gpio_set_otype(regs, idx, STM32_GPIO_OTYPE_OD);
210 else
211 stm32_gpio_set_otype(regs, idx, STM32_GPIO_OTYPE_PP);
Simon Glass7b893f92021-02-04 21:22:03 -0700212
Patrick Delaunayc6d455c2020-06-04 14:30:25 +0200213 stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_OUT);
214 writel(BSRR_BIT(idx, value), &regs->bsrr);
215
216 } else if (flags & GPIOD_IS_IN) {
217 stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_IN);
Patrick Delaunayc6d455c2020-06-04 14:30:25 +0200218 }
Patrick Delaunayf60bc022020-10-28 10:49:08 +0100219 if (flags & GPIOD_PULL_UP)
220 stm32_gpio_set_pupd(regs, idx, STM32_GPIO_PUPD_UP);
221 else if (flags & GPIOD_PULL_DOWN)
222 stm32_gpio_set_pupd(regs, idx, STM32_GPIO_PUPD_DOWN);
Patrick Delaunayb087cab2020-06-04 14:30:26 +0200223
224 return 0;
225}
226
Simon Glassd063ce92021-02-04 21:21:56 -0700227static int stm32_gpio_get_flags(struct udevice *dev, unsigned int offset,
228 ulong *flagsp)
Patrick Delaunayb087cab2020-06-04 14:30:26 +0200229{
230 struct stm32_gpio_priv *priv = dev_get_priv(dev);
231 struct stm32_gpio_regs *regs = priv->regs;
232 int idx;
233 ulong dir_flags = 0;
234
235 idx = stm32_offset_to_index(dev, offset);
236 if (idx < 0)
237 return idx;
238
239 switch (stm32_gpio_get_moder(regs, idx)) {
240 case STM32_GPIO_MODE_OUT:
241 dir_flags |= GPIOD_IS_OUT;
242 if (stm32_gpio_get_otype(regs, idx) == STM32_GPIO_OTYPE_OD)
243 dir_flags |= GPIOD_OPEN_DRAIN;
244 if (readl(&regs->idr) & BIT(idx))
245 dir_flags |= GPIOD_IS_OUT_ACTIVE;
246 break;
247 case STM32_GPIO_MODE_IN:
248 dir_flags |= GPIOD_IS_IN;
Patrick Delaunayf60bc022020-10-28 10:49:08 +0100249 break;
250 default:
251 break;
252 }
253 switch (stm32_gpio_get_pupd(regs, idx)) {
254 case STM32_GPIO_PUPD_UP:
255 dir_flags |= GPIOD_PULL_UP;
256 break;
257 case STM32_GPIO_PUPD_DOWN:
258 dir_flags |= GPIOD_PULL_DOWN;
Patrick Delaunayb087cab2020-06-04 14:30:26 +0200259 break;
260 default:
261 break;
262 }
Simon Glassd063ce92021-02-04 21:21:56 -0700263 *flagsp = dir_flags;
Patrick Delaunayc6d455c2020-06-04 14:30:25 +0200264
265 return 0;
266}
267
Vikas Manochaec8630a2017-04-10 15:02:57 -0700268static const struct dm_gpio_ops gpio_stm32_ops = {
269 .direction_input = stm32_gpio_direction_input,
270 .direction_output = stm32_gpio_direction_output,
271 .get_value = stm32_gpio_get_value,
272 .set_value = stm32_gpio_set_value,
Patrice Chotard10561232018-10-24 14:10:21 +0200273 .get_function = stm32_gpio_get_function,
Simon Glass54befdd2021-02-04 21:21:55 -0700274 .set_flags = stm32_gpio_set_flags,
Simon Glassd063ce92021-02-04 21:21:56 -0700275 .get_flags = stm32_gpio_get_flags,
Vikas Manochaec8630a2017-04-10 15:02:57 -0700276};
277
278static int gpio_stm32_probe(struct udevice *dev)
279{
Vikas Manochaec8630a2017-04-10 15:02:57 -0700280 struct stm32_gpio_priv *priv = dev_get_priv(dev);
Patrick Delaunaycd7c9512020-09-09 18:28:33 +0200281 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
282 struct ofnode_phandle_args args;
283 const char *name;
Patrice Chotard159d1572018-12-03 10:52:53 +0100284 struct clk clk;
Vikas Manochaec8630a2017-04-10 15:02:57 -0700285 fdt_addr_t addr;
Patrick Delaunaycd7c9512020-09-09 18:28:33 +0200286 int ret, i;
Vikas Manochaec8630a2017-04-10 15:02:57 -0700287
Patrick Delaunayd78f9682018-03-12 10:46:07 +0100288 addr = dev_read_addr(dev);
Vikas Manochaec8630a2017-04-10 15:02:57 -0700289 if (addr == FDT_ADDR_T_NONE)
290 return -EINVAL;
291
292 priv->regs = (struct stm32_gpio_regs *)addr;
Patrice Chotard9f62b082019-01-04 10:55:06 +0100293
Patrick Delaunayd78f9682018-03-12 10:46:07 +0100294 name = dev_read_string(dev, "st,bank-name");
Vikas Manochaec8630a2017-04-10 15:02:57 -0700295 if (!name)
296 return -EINVAL;
297 uc_priv->bank_name = name;
Patrice Chotard0099c1e2018-12-03 10:52:51 +0100298
299 i = 0;
300 ret = dev_read_phandle_with_args(dev, "gpio-ranges",
301 NULL, 3, i, &args);
302
Patrick Delaunaye7f66382020-09-09 18:28:34 +0200303 if (!ret && args.args_count < 3)
304 return -EINVAL;
305
Patrice Chotard62253052019-01-04 10:55:05 +0100306 if (ret == -ENOENT) {
307 uc_priv->gpio_count = STM32_GPIOS_PER_BANK;
308 priv->gpio_range = GENMASK(STM32_GPIOS_PER_BANK - 1, 0);
309 }
310
Patrice Chotard0099c1e2018-12-03 10:52:51 +0100311 while (ret != -ENOENT) {
312 priv->gpio_range |= GENMASK(args.args[2] + args.args[0] - 1,
313 args.args[0]);
314
315 uc_priv->gpio_count += args.args[2];
316
317 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
318 ++i, &args);
Patrick Delaunaye7f66382020-09-09 18:28:34 +0200319 if (!ret && args.args_count < 3)
320 return -EINVAL;
Patrice Chotard0099c1e2018-12-03 10:52:51 +0100321 }
322
323 dev_dbg(dev, "addr = 0x%p bank_name = %s gpio_count = %d gpio_range = 0x%x\n",
324 (u32 *)priv->regs, uc_priv->bank_name, uc_priv->gpio_count,
325 priv->gpio_range);
Patrick Delaunayb1c60142020-04-22 14:29:17 +0200326
Vikas Manochaec8630a2017-04-10 15:02:57 -0700327 ret = clk_get_by_index(dev, 0, &clk);
328 if (ret < 0)
329 return ret;
330
331 ret = clk_enable(&clk);
332
333 if (ret) {
334 dev_err(dev, "failed to enable clock\n");
335 return ret;
336 }
Patrick Delaunay09e325e2020-11-06 19:01:33 +0100337 dev_dbg(dev, "clock enabled\n");
Vikas Manochaec8630a2017-04-10 15:02:57 -0700338
339 return 0;
340}
341
Vikas Manochaec8630a2017-04-10 15:02:57 -0700342U_BOOT_DRIVER(gpio_stm32) = {
343 .name = "gpio_stm32",
344 .id = UCLASS_GPIO,
Vikas Manochaec8630a2017-04-10 15:02:57 -0700345 .probe = gpio_stm32_probe,
346 .ops = &gpio_stm32_ops,
Bin Mengb508ee52018-10-24 06:36:30 -0700347 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700348 .priv_auto = sizeof(struct stm32_gpio_priv),
Vikas Manochaec8630a2017-04-10 15:02:57 -0700349};