blob: f120ee9808620faf7790dad15abef4183011024b [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 Delaunayf13ff882020-06-04 14:30:25 +020021#define MODE_BITS(gpio_pin) ((gpio_pin) * 2)
Vikas Manocha77417102017-04-10 15:02:57 -070022#define MODE_BITS_MASK 3
Patrick Delaunayf13ff882020-06-04 14:30:25 +020023#define BSRR_BIT(gpio_pin, value) BIT((gpio_pin) + (value ? 0 : 16))
24
25#define PUPD_BITS(gpio_pin) ((gpio_pin) * 2)
26#define PUPD_MASK 3
27
28#define OTYPE_BITS(gpio_pin) (gpio_pin)
29#define OTYPE_MSK 1
30
31static void stm32_gpio_set_moder(struct stm32_gpio_regs *regs,
32 int idx,
33 int mode)
34{
35 int bits_index;
36 int mask;
37
38 bits_index = MODE_BITS(idx);
39 mask = MODE_BITS_MASK << bits_index;
40
41 clrsetbits_le32(&regs->moder, mask, mode << bits_index);
42}
43
44static void stm32_gpio_set_otype(struct stm32_gpio_regs *regs,
45 int idx,
46 enum stm32_gpio_otype otype)
47{
48 int bits;
49
50 bits = OTYPE_BITS(idx);
51 clrsetbits_le32(&regs->otyper, OTYPE_MSK << bits, otype << bits);
52}
53
54static void stm32_gpio_set_pupd(struct stm32_gpio_regs *regs,
55 int idx,
56 enum stm32_gpio_pupd pupd)
57{
58 int bits;
59
60 bits = PUPD_BITS(idx);
61 clrsetbits_le32(&regs->pupdr, PUPD_MASK << bits, pupd << bits);
62}
Vikas Manocha77417102017-04-10 15:02:57 -070063
Patrice Chotarddbf928d2018-12-03 10:52:51 +010064/*
65 * convert gpio offset to gpio index taking into account gpio holes
66 * into gpio bank
67 */
68int stm32_offset_to_index(struct udevice *dev, unsigned int offset)
69{
70 struct stm32_gpio_priv *priv = dev_get_priv(dev);
Patrick Delaunay99e14b22019-06-21 15:26:46 +020071 unsigned int idx = 0;
Patrice Chotarddbf928d2018-12-03 10:52:51 +010072 int i;
73
74 for (i = 0; i < STM32_GPIOS_PER_BANK; i++) {
75 if (priv->gpio_range & BIT(i)) {
76 if (idx == offset)
77 return idx;
78 idx++;
79 }
80 }
81 /* shouldn't happen */
82 return -EINVAL;
83}
84
Vikas Manocha77417102017-04-10 15:02:57 -070085static int stm32_gpio_direction_input(struct udevice *dev, unsigned offset)
86{
87 struct stm32_gpio_priv *priv = dev_get_priv(dev);
88 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +010089 int idx;
90
91 idx = stm32_offset_to_index(dev, offset);
92 if (idx < 0)
93 return idx;
94
Patrick Delaunayf13ff882020-06-04 14:30:25 +020095 stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_IN);
Vikas Manocha77417102017-04-10 15:02:57 -070096
97 return 0;
98}
99
100static int stm32_gpio_direction_output(struct udevice *dev, unsigned offset,
101 int value)
102{
103 struct stm32_gpio_priv *priv = dev_get_priv(dev);
104 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100105 int idx;
106
107 idx = stm32_offset_to_index(dev, offset);
108 if (idx < 0)
109 return idx;
110
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200111 stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_OUT);
Patrice Chotard798cd702018-08-09 11:57:57 +0200112
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100113 writel(BSRR_BIT(idx, value), &regs->bsrr);
Vikas Manocha77417102017-04-10 15:02:57 -0700114
115 return 0;
116}
117
118static int stm32_gpio_get_value(struct udevice *dev, unsigned offset)
119{
120 struct stm32_gpio_priv *priv = dev_get_priv(dev);
121 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100122 int idx;
Vikas Manocha77417102017-04-10 15:02:57 -0700123
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100124 idx = stm32_offset_to_index(dev, offset);
125 if (idx < 0)
126 return idx;
127
128 return readl(&regs->idr) & BIT(idx) ? 1 : 0;
Vikas Manocha77417102017-04-10 15:02:57 -0700129}
130
131static int stm32_gpio_set_value(struct udevice *dev, unsigned offset, int value)
132{
133 struct stm32_gpio_priv *priv = dev_get_priv(dev);
134 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100135 int idx;
Vikas Manocha77417102017-04-10 15:02:57 -0700136
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100137 idx = stm32_offset_to_index(dev, offset);
138 if (idx < 0)
139 return idx;
140
141 writel(BSRR_BIT(idx, 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;
152 int idx;
Patrice Chotardcad73242018-10-24 14:10:21 +0200153 u32 mode;
154
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100155 idx = stm32_offset_to_index(dev, offset);
156 if (idx < 0)
157 return idx;
158
159 bits_index = MODE_BITS(idx);
160 mask = MODE_BITS_MASK << bits_index;
161
Patrice Chotardcad73242018-10-24 14:10:21 +0200162 mode = (readl(&regs->moder) & mask) >> bits_index;
163 if (mode == STM32_GPIO_MODE_OUT)
164 return GPIOF_OUTPUT;
165 if (mode == STM32_GPIO_MODE_IN)
166 return GPIOF_INPUT;
167 if (mode == STM32_GPIO_MODE_AN)
168 return GPIOF_UNUSED;
169
170 return GPIOF_FUNC;
171}
172
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200173static int stm32_gpio_set_dir_flags(struct udevice *dev, unsigned int offset,
174 ulong flags)
175{
176 struct stm32_gpio_priv *priv = dev_get_priv(dev);
177 struct stm32_gpio_regs *regs = priv->regs;
178 int idx;
179
180 idx = stm32_offset_to_index(dev, offset);
181 if (idx < 0)
182 return idx;
183
184 if (flags & GPIOD_IS_OUT) {
185 int value = GPIOD_FLAGS_OUTPUT(flags);
186
187 if (flags & GPIOD_OPEN_DRAIN)
188 stm32_gpio_set_otype(regs, idx, STM32_GPIO_OTYPE_OD);
189 else
190 stm32_gpio_set_otype(regs, idx, STM32_GPIO_OTYPE_PP);
191 stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_OUT);
192 writel(BSRR_BIT(idx, value), &regs->bsrr);
193
194 } else if (flags & GPIOD_IS_IN) {
195 stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_IN);
196 if (flags & GPIOD_PULL_UP)
197 stm32_gpio_set_pupd(regs, idx, STM32_GPIO_PUPD_UP);
198 else if (flags & GPIOD_PULL_DOWN)
199 stm32_gpio_set_pupd(regs, idx, STM32_GPIO_PUPD_DOWN);
200 }
201
202 return 0;
203}
204
Vikas Manocha77417102017-04-10 15:02:57 -0700205static const struct dm_gpio_ops gpio_stm32_ops = {
206 .direction_input = stm32_gpio_direction_input,
207 .direction_output = stm32_gpio_direction_output,
208 .get_value = stm32_gpio_get_value,
209 .set_value = stm32_gpio_set_value,
Patrice Chotardcad73242018-10-24 14:10:21 +0200210 .get_function = stm32_gpio_get_function,
Patrick Delaunayf13ff882020-06-04 14:30:25 +0200211 .set_dir_flags = stm32_gpio_set_dir_flags,
Vikas Manocha77417102017-04-10 15:02:57 -0700212};
213
214static int gpio_stm32_probe(struct udevice *dev)
215{
Vikas Manocha77417102017-04-10 15:02:57 -0700216 struct stm32_gpio_priv *priv = dev_get_priv(dev);
Patrice Chotard8b6d45a2018-12-03 10:52:53 +0100217 struct clk clk;
Vikas Manocha77417102017-04-10 15:02:57 -0700218 fdt_addr_t addr;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100219 int ret;
Vikas Manocha77417102017-04-10 15:02:57 -0700220
Patrick Delaunayd876eaf2018-03-12 10:46:07 +0100221 addr = dev_read_addr(dev);
Vikas Manocha77417102017-04-10 15:02:57 -0700222 if (addr == FDT_ADDR_T_NONE)
223 return -EINVAL;
224
225 priv->regs = (struct stm32_gpio_regs *)addr;
Patrice Chotard4fb22462019-01-04 10:55:06 +0100226
Patrice Chotard4fb22462019-01-04 10:55:06 +0100227 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
228 struct ofnode_phandle_args args;
229 const char *name;
230 int i;
231
Patrick Delaunayd876eaf2018-03-12 10:46:07 +0100232 name = dev_read_string(dev, "st,bank-name");
Vikas Manocha77417102017-04-10 15:02:57 -0700233 if (!name)
234 return -EINVAL;
235 uc_priv->bank_name = name;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100236
237 i = 0;
238 ret = dev_read_phandle_with_args(dev, "gpio-ranges",
239 NULL, 3, i, &args);
240
Patrice Chotard39a8f0b2019-01-04 10:55:05 +0100241 if (ret == -ENOENT) {
242 uc_priv->gpio_count = STM32_GPIOS_PER_BANK;
243 priv->gpio_range = GENMASK(STM32_GPIOS_PER_BANK - 1, 0);
244 }
245
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100246 while (ret != -ENOENT) {
247 priv->gpio_range |= GENMASK(args.args[2] + args.args[0] - 1,
248 args.args[0]);
249
250 uc_priv->gpio_count += args.args[2];
251
252 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
253 ++i, &args);
254 }
255
256 dev_dbg(dev, "addr = 0x%p bank_name = %s gpio_count = %d gpio_range = 0x%x\n",
257 (u32 *)priv->regs, uc_priv->bank_name, uc_priv->gpio_count,
258 priv->gpio_range);
Patrick Delaunayf17412e2020-04-22 14:29:17 +0200259
Vikas Manocha77417102017-04-10 15:02:57 -0700260 ret = clk_get_by_index(dev, 0, &clk);
261 if (ret < 0)
262 return ret;
263
264 ret = clk_enable(&clk);
265
266 if (ret) {
267 dev_err(dev, "failed to enable clock\n");
268 return ret;
269 }
270 debug("clock enabled for device %s\n", dev->name);
Vikas Manocha77417102017-04-10 15:02:57 -0700271
272 return 0;
273}
274
Vikas Manocha77417102017-04-10 15:02:57 -0700275U_BOOT_DRIVER(gpio_stm32) = {
276 .name = "gpio_stm32",
277 .id = UCLASS_GPIO,
Vikas Manocha77417102017-04-10 15:02:57 -0700278 .probe = gpio_stm32_probe,
279 .ops = &gpio_stm32_ops,
Bin Meng695c4992018-10-24 06:36:30 -0700280 .flags = DM_UC_FLAG_SEQ_ALIAS,
Vikas Manocha77417102017-04-10 15:02:57 -0700281 .priv_auto_alloc_size = sizeof(struct stm32_gpio_priv),
282};