blob: 79d55e812db985adcaa4d9e3ee8f5926ba4aa5d4 [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
7#include <common.h>
8#include <clk.h>
9#include <dm.h>
10#include <fdtdec.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Vikas Manocha77417102017-04-10 15:02:57 -070012#include <asm/arch/gpio.h>
13#include <asm/arch/stm32.h>
14#include <asm/gpio.h>
15#include <asm/io.h>
Simon Glass336d4612020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Simon Glasscd93d622020-05-10 11:40:13 -060017#include <linux/bitops.h>
Vikas Manocha77417102017-04-10 15:02:57 -070018#include <linux/errno.h>
19#include <linux/io.h>
20
Patrick Delaunay6af78d02020-10-02 14:08:54 +020021#define STM32_GPIOS_PER_BANK 16
22
Patrick Delaunayf13ff882020-06-04 14:30:25 +020023#define MODE_BITS(gpio_pin) ((gpio_pin) * 2)
Vikas Manocha77417102017-04-10 15:02:57 -070024#define MODE_BITS_MASK 3
Patrick Delaunayf13ff882020-06-04 14:30:25 +020025#define BSRR_BIT(gpio_pin, value) BIT((gpio_pin) + (value ? 0 : 16))
26
27#define PUPD_BITS(gpio_pin) ((gpio_pin) * 2)
28#define PUPD_MASK 3
29
30#define OTYPE_BITS(gpio_pin) (gpio_pin)
31#define OTYPE_MSK 1
32
33static void stm32_gpio_set_moder(struct stm32_gpio_regs *regs,
34 int idx,
35 int mode)
36{
37 int bits_index;
38 int mask;
39
40 bits_index = MODE_BITS(idx);
41 mask = MODE_BITS_MASK << bits_index;
42
43 clrsetbits_le32(&regs->moder, mask, mode << bits_index);
44}
45
Patrick Delaunay43efbb62020-06-04 14:30:26 +020046static int stm32_gpio_get_moder(struct stm32_gpio_regs *regs, int idx)
47{
48 return (readl(&regs->moder) >> MODE_BITS(idx)) & MODE_BITS_MASK;
49}
50
Patrick Delaunayf13ff882020-06-04 14:30:25 +020051static void stm32_gpio_set_otype(struct stm32_gpio_regs *regs,
52 int idx,
53 enum stm32_gpio_otype otype)
54{
55 int bits;
56
57 bits = OTYPE_BITS(idx);
58 clrsetbits_le32(&regs->otyper, OTYPE_MSK << bits, otype << bits);
59}
60
Patrick Delaunay43efbb62020-06-04 14:30:26 +020061static enum stm32_gpio_otype stm32_gpio_get_otype(struct stm32_gpio_regs *regs,
62 int idx)
63{
64 return (readl(&regs->otyper) >> OTYPE_BITS(idx)) & OTYPE_MSK;
65}
66
Patrick Delaunayf13ff882020-06-04 14:30:25 +020067static void stm32_gpio_set_pupd(struct stm32_gpio_regs *regs,
68 int idx,
69 enum stm32_gpio_pupd pupd)
70{
71 int bits;
72
73 bits = PUPD_BITS(idx);
74 clrsetbits_le32(&regs->pupdr, PUPD_MASK << bits, pupd << bits);
75}
Vikas Manocha77417102017-04-10 15:02:57 -070076
Patrick Delaunay43efbb62020-06-04 14:30:26 +020077static enum stm32_gpio_pupd stm32_gpio_get_pupd(struct stm32_gpio_regs *regs,
78 int idx)
79{
80 return (readl(&regs->pupdr) >> PUPD_BITS(idx)) & PUPD_MASK;
81}
82
Patrice Chotarddbf928d2018-12-03 10:52:51 +010083/*
84 * convert gpio offset to gpio index taking into account gpio holes
85 * into gpio bank
86 */
87int stm32_offset_to_index(struct udevice *dev, unsigned int offset)
88{
89 struct stm32_gpio_priv *priv = dev_get_priv(dev);
Patrick Delaunay99e14b22019-06-21 15:26:46 +020090 unsigned int idx = 0;
Patrice Chotarddbf928d2018-12-03 10:52:51 +010091 int i;
92
93 for (i = 0; i < STM32_GPIOS_PER_BANK; i++) {
94 if (priv->gpio_range & BIT(i)) {
95 if (idx == offset)
96 return idx;
97 idx++;
98 }
99 }
100 /* shouldn't happen */
101 return -EINVAL;
102}
103
Vikas Manocha77417102017-04-10 15:02:57 -0700104static int stm32_gpio_direction_input(struct udevice *dev, unsigned offset)
105{
106 struct stm32_gpio_priv *priv = dev_get_priv(dev);
107 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100108 int idx;
109
110 idx = stm32_offset_to_index(dev, offset);
111 if (idx < 0)
112 return idx;
113
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200114 stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_IN);
Vikas Manocha77417102017-04-10 15:02:57 -0700115
116 return 0;
117}
118
119static int stm32_gpio_direction_output(struct udevice *dev, unsigned offset,
120 int value)
121{
122 struct stm32_gpio_priv *priv = dev_get_priv(dev);
123 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100124 int idx;
125
126 idx = stm32_offset_to_index(dev, offset);
127 if (idx < 0)
128 return idx;
129
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200130 stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_OUT);
Patrice Chotard798cd702018-08-09 11:57:57 +0200131
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100132 writel(BSRR_BIT(idx, value), &regs->bsrr);
Vikas Manocha77417102017-04-10 15:02:57 -0700133
134 return 0;
135}
136
137static int stm32_gpio_get_value(struct udevice *dev, unsigned offset)
138{
139 struct stm32_gpio_priv *priv = dev_get_priv(dev);
140 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100141 int idx;
Vikas Manocha77417102017-04-10 15:02:57 -0700142
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100143 idx = stm32_offset_to_index(dev, offset);
144 if (idx < 0)
145 return idx;
146
147 return readl(&regs->idr) & BIT(idx) ? 1 : 0;
Vikas Manocha77417102017-04-10 15:02:57 -0700148}
149
150static int stm32_gpio_set_value(struct udevice *dev, unsigned offset, int value)
151{
152 struct stm32_gpio_priv *priv = dev_get_priv(dev);
153 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100154 int idx;
Vikas Manocha77417102017-04-10 15:02:57 -0700155
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100156 idx = stm32_offset_to_index(dev, offset);
157 if (idx < 0)
158 return idx;
159
160 writel(BSRR_BIT(idx, value), &regs->bsrr);
Vikas Manocha77417102017-04-10 15:02:57 -0700161
162 return 0;
163}
164
Patrice Chotardcad73242018-10-24 14:10:21 +0200165static int stm32_gpio_get_function(struct udevice *dev, unsigned int offset)
166{
167 struct stm32_gpio_priv *priv = dev_get_priv(dev);
168 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100169 int bits_index;
170 int mask;
171 int idx;
Patrice Chotardcad73242018-10-24 14:10:21 +0200172 u32 mode;
173
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100174 idx = stm32_offset_to_index(dev, offset);
175 if (idx < 0)
176 return idx;
177
178 bits_index = MODE_BITS(idx);
179 mask = MODE_BITS_MASK << bits_index;
180
Patrice Chotardcad73242018-10-24 14:10:21 +0200181 mode = (readl(&regs->moder) & mask) >> bits_index;
182 if (mode == STM32_GPIO_MODE_OUT)
183 return GPIOF_OUTPUT;
184 if (mode == STM32_GPIO_MODE_IN)
185 return GPIOF_INPUT;
186 if (mode == STM32_GPIO_MODE_AN)
187 return GPIOF_UNUSED;
188
189 return GPIOF_FUNC;
190}
191
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200192static int stm32_gpio_set_dir_flags(struct udevice *dev, unsigned int offset,
193 ulong flags)
194{
195 struct stm32_gpio_priv *priv = dev_get_priv(dev);
196 struct stm32_gpio_regs *regs = priv->regs;
197 int idx;
198
199 idx = stm32_offset_to_index(dev, offset);
200 if (idx < 0)
201 return idx;
202
203 if (flags & GPIOD_IS_OUT) {
204 int value = GPIOD_FLAGS_OUTPUT(flags);
205
206 if (flags & GPIOD_OPEN_DRAIN)
207 stm32_gpio_set_otype(regs, idx, STM32_GPIO_OTYPE_OD);
208 else
209 stm32_gpio_set_otype(regs, idx, STM32_GPIO_OTYPE_PP);
210 stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_OUT);
211 writel(BSRR_BIT(idx, value), &regs->bsrr);
212
213 } else if (flags & GPIOD_IS_IN) {
214 stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_IN);
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200215 }
Patrick Delaunay2c6df942020-10-28 10:49:08 +0100216 if (flags & GPIOD_PULL_UP)
217 stm32_gpio_set_pupd(regs, idx, STM32_GPIO_PUPD_UP);
218 else if (flags & GPIOD_PULL_DOWN)
219 stm32_gpio_set_pupd(regs, idx, STM32_GPIO_PUPD_DOWN);
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200220
221 return 0;
222}
223
Patrick Delaunay43efbb62020-06-04 14:30:26 +0200224static int stm32_gpio_get_dir_flags(struct udevice *dev, unsigned int offset,
225 ulong *flags)
226{
227 struct stm32_gpio_priv *priv = dev_get_priv(dev);
228 struct stm32_gpio_regs *regs = priv->regs;
229 int idx;
230 ulong dir_flags = 0;
231
232 idx = stm32_offset_to_index(dev, offset);
233 if (idx < 0)
234 return idx;
235
236 switch (stm32_gpio_get_moder(regs, idx)) {
237 case STM32_GPIO_MODE_OUT:
238 dir_flags |= GPIOD_IS_OUT;
239 if (stm32_gpio_get_otype(regs, idx) == STM32_GPIO_OTYPE_OD)
240 dir_flags |= GPIOD_OPEN_DRAIN;
241 if (readl(&regs->idr) & BIT(idx))
242 dir_flags |= GPIOD_IS_OUT_ACTIVE;
243 break;
244 case STM32_GPIO_MODE_IN:
245 dir_flags |= GPIOD_IS_IN;
Patrick Delaunay2c6df942020-10-28 10:49:08 +0100246 break;
247 default:
248 break;
249 }
250 switch (stm32_gpio_get_pupd(regs, idx)) {
251 case STM32_GPIO_PUPD_UP:
252 dir_flags |= GPIOD_PULL_UP;
253 break;
254 case STM32_GPIO_PUPD_DOWN:
255 dir_flags |= GPIOD_PULL_DOWN;
Patrick Delaunay43efbb62020-06-04 14:30:26 +0200256 break;
257 default:
258 break;
259 }
260 *flags = dir_flags;
261
262 return 0;
263}
264
Vikas Manocha77417102017-04-10 15:02:57 -0700265static const struct dm_gpio_ops gpio_stm32_ops = {
266 .direction_input = stm32_gpio_direction_input,
267 .direction_output = stm32_gpio_direction_output,
268 .get_value = stm32_gpio_get_value,
269 .set_value = stm32_gpio_set_value,
Patrice Chotardcad73242018-10-24 14:10:21 +0200270 .get_function = stm32_gpio_get_function,
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200271 .set_dir_flags = stm32_gpio_set_dir_flags,
Patrick Delaunay43efbb62020-06-04 14:30:26 +0200272 .get_dir_flags = stm32_gpio_get_dir_flags,
Vikas Manocha77417102017-04-10 15:02:57 -0700273};
274
275static int gpio_stm32_probe(struct udevice *dev)
276{
Vikas Manocha77417102017-04-10 15:02:57 -0700277 struct stm32_gpio_priv *priv = dev_get_priv(dev);
Patrick Delaunay15c8cbf2020-09-09 18:28:33 +0200278 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
279 struct ofnode_phandle_args args;
280 const char *name;
Patrice Chotard8b6d45a2018-12-03 10:52:53 +0100281 struct clk clk;
Vikas Manocha77417102017-04-10 15:02:57 -0700282 fdt_addr_t addr;
Patrick Delaunay15c8cbf2020-09-09 18:28:33 +0200283 int ret, i;
Vikas Manocha77417102017-04-10 15:02:57 -0700284
Patrick Delaunayd876eaf2018-03-12 10:46:07 +0100285 addr = dev_read_addr(dev);
Vikas Manocha77417102017-04-10 15:02:57 -0700286 if (addr == FDT_ADDR_T_NONE)
287 return -EINVAL;
288
289 priv->regs = (struct stm32_gpio_regs *)addr;
Patrice Chotard4fb22462019-01-04 10:55:06 +0100290
Patrick Delaunayd876eaf2018-03-12 10:46:07 +0100291 name = dev_read_string(dev, "st,bank-name");
Vikas Manocha77417102017-04-10 15:02:57 -0700292 if (!name)
293 return -EINVAL;
294 uc_priv->bank_name = name;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100295
296 i = 0;
297 ret = dev_read_phandle_with_args(dev, "gpio-ranges",
298 NULL, 3, i, &args);
299
Patrick Delaunaycb08e842020-09-09 18:28:34 +0200300 if (!ret && args.args_count < 3)
301 return -EINVAL;
302
Patrice Chotard39a8f0b2019-01-04 10:55:05 +0100303 if (ret == -ENOENT) {
304 uc_priv->gpio_count = STM32_GPIOS_PER_BANK;
305 priv->gpio_range = GENMASK(STM32_GPIOS_PER_BANK - 1, 0);
306 }
307
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100308 while (ret != -ENOENT) {
309 priv->gpio_range |= GENMASK(args.args[2] + args.args[0] - 1,
310 args.args[0]);
311
312 uc_priv->gpio_count += args.args[2];
313
314 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
315 ++i, &args);
Patrick Delaunaycb08e842020-09-09 18:28:34 +0200316 if (!ret && args.args_count < 3)
317 return -EINVAL;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100318 }
319
320 dev_dbg(dev, "addr = 0x%p bank_name = %s gpio_count = %d gpio_range = 0x%x\n",
321 (u32 *)priv->regs, uc_priv->bank_name, uc_priv->gpio_count,
322 priv->gpio_range);
Patrick Delaunayf17412e2020-04-22 14:29:17 +0200323
Vikas Manocha77417102017-04-10 15:02:57 -0700324 ret = clk_get_by_index(dev, 0, &clk);
325 if (ret < 0)
326 return ret;
327
328 ret = clk_enable(&clk);
329
330 if (ret) {
331 dev_err(dev, "failed to enable clock\n");
332 return ret;
333 }
334 debug("clock enabled for device %s\n", dev->name);
Vikas Manocha77417102017-04-10 15:02:57 -0700335
336 return 0;
337}
338
Vikas Manocha77417102017-04-10 15:02:57 -0700339U_BOOT_DRIVER(gpio_stm32) = {
340 .name = "gpio_stm32",
341 .id = UCLASS_GPIO,
Vikas Manocha77417102017-04-10 15:02:57 -0700342 .probe = gpio_stm32_probe,
343 .ops = &gpio_stm32_ops,
Bin Meng695c4992018-10-24 06:36:30 -0700344 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass41575d82020-12-03 16:55:17 -0700345 .priv_auto = sizeof(struct stm32_gpio_priv),
Vikas Manocha77417102017-04-10 15:02:57 -0700346};