blob: 8667ed3835c4ffb0de93e2954c3f445c4b380fcb [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 Chotarddbf928d2018-12-03 10:52:51 +010086/*
87 * convert gpio offset to gpio index taking into account gpio holes
88 * into gpio bank
89 */
90int stm32_offset_to_index(struct udevice *dev, unsigned int offset)
91{
92 struct stm32_gpio_priv *priv = dev_get_priv(dev);
Patrick Delaunay99e14b22019-06-21 15:26:46 +020093 unsigned int idx = 0;
Patrice Chotarddbf928d2018-12-03 10:52:51 +010094 int i;
95
96 for (i = 0; i < STM32_GPIOS_PER_BANK; i++) {
97 if (priv->gpio_range & BIT(i)) {
98 if (idx == offset)
99 return idx;
100 idx++;
101 }
102 }
103 /* shouldn't happen */
104 return -EINVAL;
105}
106
Vikas Manocha77417102017-04-10 15:02:57 -0700107static int stm32_gpio_direction_input(struct udevice *dev, unsigned offset)
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 int idx;
112
113 idx = stm32_offset_to_index(dev, offset);
114 if (idx < 0)
115 return idx;
116
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200117 stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_IN);
Vikas Manocha77417102017-04-10 15:02:57 -0700118
119 return 0;
120}
121
122static int stm32_gpio_direction_output(struct udevice *dev, unsigned offset,
123 int value)
124{
125 struct stm32_gpio_priv *priv = dev_get_priv(dev);
126 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100127 int idx;
128
129 idx = stm32_offset_to_index(dev, offset);
130 if (idx < 0)
131 return idx;
132
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200133 stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_OUT);
Patrice Chotard798cd702018-08-09 11:57:57 +0200134
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100135 writel(BSRR_BIT(idx, value), &regs->bsrr);
Vikas Manocha77417102017-04-10 15:02:57 -0700136
137 return 0;
138}
139
140static int stm32_gpio_get_value(struct udevice *dev, unsigned offset)
141{
142 struct stm32_gpio_priv *priv = dev_get_priv(dev);
143 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100144 int idx;
Vikas Manocha77417102017-04-10 15:02:57 -0700145
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100146 idx = stm32_offset_to_index(dev, offset);
147 if (idx < 0)
148 return idx;
149
150 return readl(&regs->idr) & BIT(idx) ? 1 : 0;
Vikas Manocha77417102017-04-10 15:02:57 -0700151}
152
153static int stm32_gpio_set_value(struct udevice *dev, unsigned offset, int value)
154{
155 struct stm32_gpio_priv *priv = dev_get_priv(dev);
156 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100157 int idx;
Vikas Manocha77417102017-04-10 15:02:57 -0700158
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100159 idx = stm32_offset_to_index(dev, offset);
160 if (idx < 0)
161 return idx;
162
163 writel(BSRR_BIT(idx, value), &regs->bsrr);
Vikas Manocha77417102017-04-10 15:02:57 -0700164
165 return 0;
166}
167
Patrice Chotardcad73242018-10-24 14:10:21 +0200168static int stm32_gpio_get_function(struct udevice *dev, unsigned int offset)
169{
170 struct stm32_gpio_priv *priv = dev_get_priv(dev);
171 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100172 int bits_index;
173 int mask;
174 int idx;
Patrice Chotardcad73242018-10-24 14:10:21 +0200175 u32 mode;
176
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100177 idx = stm32_offset_to_index(dev, offset);
178 if (idx < 0)
179 return idx;
180
181 bits_index = MODE_BITS(idx);
182 mask = MODE_BITS_MASK << bits_index;
183
Patrice Chotardcad73242018-10-24 14:10:21 +0200184 mode = (readl(&regs->moder) & mask) >> bits_index;
185 if (mode == STM32_GPIO_MODE_OUT)
186 return GPIOF_OUTPUT;
187 if (mode == STM32_GPIO_MODE_IN)
188 return GPIOF_INPUT;
189 if (mode == STM32_GPIO_MODE_AN)
190 return GPIOF_UNUSED;
191
192 return GPIOF_FUNC;
193}
194
Simon Glass13979fc2021-02-04 21:21:55 -0700195static int stm32_gpio_set_flags(struct udevice *dev, unsigned int offset,
196 ulong flags)
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200197{
198 struct stm32_gpio_priv *priv = dev_get_priv(dev);
199 struct stm32_gpio_regs *regs = priv->regs;
200 int idx;
201
202 idx = stm32_offset_to_index(dev, offset);
203 if (idx < 0)
204 return idx;
205
206 if (flags & GPIOD_IS_OUT) {
Simon Glass7e0a96d2021-02-04 21:22:03 -0700207 bool value = flags & GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200208
209 if (flags & GPIOD_OPEN_DRAIN)
210 stm32_gpio_set_otype(regs, idx, STM32_GPIO_OTYPE_OD);
211 else
212 stm32_gpio_set_otype(regs, idx, STM32_GPIO_OTYPE_PP);
Simon Glass7e0a96d2021-02-04 21:22:03 -0700213
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200214 stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_OUT);
215 writel(BSRR_BIT(idx, value), &regs->bsrr);
216
217 } else if (flags & GPIOD_IS_IN) {
218 stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_IN);
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200219 }
Patrick Delaunay2c6df942020-10-28 10:49:08 +0100220 if (flags & GPIOD_PULL_UP)
221 stm32_gpio_set_pupd(regs, idx, STM32_GPIO_PUPD_UP);
222 else if (flags & GPIOD_PULL_DOWN)
223 stm32_gpio_set_pupd(regs, idx, STM32_GPIO_PUPD_DOWN);
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200224
225 return 0;
226}
227
Simon Glass96487892021-02-04 21:21:56 -0700228static int stm32_gpio_get_flags(struct udevice *dev, unsigned int offset,
229 ulong *flagsp)
Patrick Delaunay43efbb62020-06-04 14:30:26 +0200230{
231 struct stm32_gpio_priv *priv = dev_get_priv(dev);
232 struct stm32_gpio_regs *regs = priv->regs;
233 int idx;
234 ulong dir_flags = 0;
235
236 idx = stm32_offset_to_index(dev, offset);
237 if (idx < 0)
238 return idx;
239
240 switch (stm32_gpio_get_moder(regs, idx)) {
241 case STM32_GPIO_MODE_OUT:
242 dir_flags |= GPIOD_IS_OUT;
243 if (stm32_gpio_get_otype(regs, idx) == STM32_GPIO_OTYPE_OD)
244 dir_flags |= GPIOD_OPEN_DRAIN;
245 if (readl(&regs->idr) & BIT(idx))
246 dir_flags |= GPIOD_IS_OUT_ACTIVE;
247 break;
248 case STM32_GPIO_MODE_IN:
249 dir_flags |= GPIOD_IS_IN;
Patrick Delaunay2c6df942020-10-28 10:49:08 +0100250 break;
251 default:
252 break;
253 }
254 switch (stm32_gpio_get_pupd(regs, idx)) {
255 case STM32_GPIO_PUPD_UP:
256 dir_flags |= GPIOD_PULL_UP;
257 break;
258 case STM32_GPIO_PUPD_DOWN:
259 dir_flags |= GPIOD_PULL_DOWN;
Patrick Delaunay43efbb62020-06-04 14:30:26 +0200260 break;
261 default:
262 break;
263 }
Simon Glass96487892021-02-04 21:21:56 -0700264 *flagsp = dir_flags;
Patrick Delaunay43efbb62020-06-04 14:30:26 +0200265
266 return 0;
267}
268
Vikas Manocha77417102017-04-10 15:02:57 -0700269static const struct dm_gpio_ops gpio_stm32_ops = {
270 .direction_input = stm32_gpio_direction_input,
271 .direction_output = stm32_gpio_direction_output,
272 .get_value = stm32_gpio_get_value,
273 .set_value = stm32_gpio_set_value,
Patrice Chotardcad73242018-10-24 14:10:21 +0200274 .get_function = stm32_gpio_get_function,
Simon Glass13979fc2021-02-04 21:21:55 -0700275 .set_flags = stm32_gpio_set_flags,
Simon Glass96487892021-02-04 21:21:56 -0700276 .get_flags = stm32_gpio_get_flags,
Vikas Manocha77417102017-04-10 15:02:57 -0700277};
278
279static int gpio_stm32_probe(struct udevice *dev)
280{
Vikas Manocha77417102017-04-10 15:02:57 -0700281 struct stm32_gpio_priv *priv = dev_get_priv(dev);
Patrick Delaunay15c8cbf2020-09-09 18:28:33 +0200282 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
283 struct ofnode_phandle_args args;
284 const char *name;
Patrice Chotard8b6d45a2018-12-03 10:52:53 +0100285 struct clk clk;
Vikas Manocha77417102017-04-10 15:02:57 -0700286 fdt_addr_t addr;
Patrick Delaunay15c8cbf2020-09-09 18:28:33 +0200287 int ret, i;
Vikas Manocha77417102017-04-10 15:02:57 -0700288
Patrick Delaunayd876eaf2018-03-12 10:46:07 +0100289 addr = dev_read_addr(dev);
Vikas Manocha77417102017-04-10 15:02:57 -0700290 if (addr == FDT_ADDR_T_NONE)
291 return -EINVAL;
292
293 priv->regs = (struct stm32_gpio_regs *)addr;
Patrice Chotard4fb22462019-01-04 10:55:06 +0100294
Patrick Delaunayd876eaf2018-03-12 10:46:07 +0100295 name = dev_read_string(dev, "st,bank-name");
Vikas Manocha77417102017-04-10 15:02:57 -0700296 if (!name)
297 return -EINVAL;
298 uc_priv->bank_name = name;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100299
300 i = 0;
301 ret = dev_read_phandle_with_args(dev, "gpio-ranges",
302 NULL, 3, i, &args);
303
Patrick Delaunaycb08e842020-09-09 18:28:34 +0200304 if (!ret && args.args_count < 3)
305 return -EINVAL;
306
Patrice Chotard39a8f0b2019-01-04 10:55:05 +0100307 if (ret == -ENOENT) {
308 uc_priv->gpio_count = STM32_GPIOS_PER_BANK;
309 priv->gpio_range = GENMASK(STM32_GPIOS_PER_BANK - 1, 0);
310 }
311
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100312 while (ret != -ENOENT) {
313 priv->gpio_range |= GENMASK(args.args[2] + args.args[0] - 1,
314 args.args[0]);
315
316 uc_priv->gpio_count += args.args[2];
317
318 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
319 ++i, &args);
Patrick Delaunaycb08e842020-09-09 18:28:34 +0200320 if (!ret && args.args_count < 3)
321 return -EINVAL;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100322 }
323
324 dev_dbg(dev, "addr = 0x%p bank_name = %s gpio_count = %d gpio_range = 0x%x\n",
325 (u32 *)priv->regs, uc_priv->bank_name, uc_priv->gpio_count,
326 priv->gpio_range);
Patrick Delaunayf17412e2020-04-22 14:29:17 +0200327
Vikas Manocha77417102017-04-10 15:02:57 -0700328 ret = clk_get_by_index(dev, 0, &clk);
329 if (ret < 0)
330 return ret;
331
332 ret = clk_enable(&clk);
333
334 if (ret) {
335 dev_err(dev, "failed to enable clock\n");
336 return ret;
337 }
Patrick Delaunay6dd89d92020-11-06 19:01:33 +0100338 dev_dbg(dev, "clock enabled\n");
Vikas Manocha77417102017-04-10 15:02:57 -0700339
340 return 0;
341}
342
Vikas Manocha77417102017-04-10 15:02:57 -0700343U_BOOT_DRIVER(gpio_stm32) = {
344 .name = "gpio_stm32",
345 .id = UCLASS_GPIO,
Vikas Manocha77417102017-04-10 15:02:57 -0700346 .probe = gpio_stm32_probe,
347 .ops = &gpio_stm32_ops,
Bin Meng695c4992018-10-24 06:36:30 -0700348 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass41575d82020-12-03 16:55:17 -0700349 .priv_auto = sizeof(struct stm32_gpio_priv),
Vikas Manocha77417102017-04-10 15:02:57 -0700350};