blob: f55f834e7d667dcbd98665137a3c95eff5b51cc3 [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>
11#include <asm/arch/gpio.h>
12#include <asm/arch/stm32.h>
13#include <asm/gpio.h>
14#include <asm/io.h>
Simon Glass336d4612020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Vikas Manocha77417102017-04-10 15:02:57 -070016#include <linux/errno.h>
17#include <linux/io.h>
18
Vikas Manocha77417102017-04-10 15:02:57 -070019#define MODE_BITS(gpio_pin) (gpio_pin * 2)
20#define MODE_BITS_MASK 3
Patrice Chotard798cd702018-08-09 11:57:57 +020021#define BSRR_BIT(gpio_pin, value) BIT(gpio_pin + (value ? 0 : 16))
Vikas Manocha77417102017-04-10 15:02:57 -070022
Patrice Chotard4fb22462019-01-04 10:55:06 +010023#ifndef CONFIG_SPL_BUILD
Patrice Chotarddbf928d2018-12-03 10:52:51 +010024/*
25 * convert gpio offset to gpio index taking into account gpio holes
26 * into gpio bank
27 */
28int stm32_offset_to_index(struct udevice *dev, unsigned int offset)
29{
30 struct stm32_gpio_priv *priv = dev_get_priv(dev);
Patrick Delaunay99e14b22019-06-21 15:26:46 +020031 unsigned int idx = 0;
Patrice Chotarddbf928d2018-12-03 10:52:51 +010032 int i;
33
34 for (i = 0; i < STM32_GPIOS_PER_BANK; i++) {
35 if (priv->gpio_range & BIT(i)) {
36 if (idx == offset)
37 return idx;
38 idx++;
39 }
40 }
41 /* shouldn't happen */
42 return -EINVAL;
43}
44
Vikas Manocha77417102017-04-10 15:02:57 -070045static int stm32_gpio_direction_input(struct udevice *dev, unsigned offset)
46{
47 struct stm32_gpio_priv *priv = dev_get_priv(dev);
48 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +010049 int bits_index;
50 int mask;
51 int idx;
52
53 idx = stm32_offset_to_index(dev, offset);
54 if (idx < 0)
55 return idx;
56
57 bits_index = MODE_BITS(idx);
58 mask = MODE_BITS_MASK << bits_index;
Vikas Manocha77417102017-04-10 15:02:57 -070059
60 clrsetbits_le32(&regs->moder, mask, STM32_GPIO_MODE_IN << bits_index);
61
62 return 0;
63}
64
65static int stm32_gpio_direction_output(struct udevice *dev, unsigned offset,
66 int value)
67{
68 struct stm32_gpio_priv *priv = dev_get_priv(dev);
69 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +010070 int bits_index;
71 int mask;
72 int idx;
73
74 idx = stm32_offset_to_index(dev, offset);
75 if (idx < 0)
76 return idx;
77
78 bits_index = MODE_BITS(idx);
79 mask = MODE_BITS_MASK << bits_index;
Vikas Manocha77417102017-04-10 15:02:57 -070080
81 clrsetbits_le32(&regs->moder, mask, STM32_GPIO_MODE_OUT << bits_index);
Patrice Chotard798cd702018-08-09 11:57:57 +020082
Patrice Chotarddbf928d2018-12-03 10:52:51 +010083 writel(BSRR_BIT(idx, value), &regs->bsrr);
Vikas Manocha77417102017-04-10 15:02:57 -070084
85 return 0;
86}
87
88static int stm32_gpio_get_value(struct udevice *dev, unsigned offset)
89{
90 struct stm32_gpio_priv *priv = dev_get_priv(dev);
91 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +010092 int idx;
Vikas Manocha77417102017-04-10 15:02:57 -070093
Patrice Chotarddbf928d2018-12-03 10:52:51 +010094 idx = stm32_offset_to_index(dev, offset);
95 if (idx < 0)
96 return idx;
97
98 return readl(&regs->idr) & BIT(idx) ? 1 : 0;
Vikas Manocha77417102017-04-10 15:02:57 -070099}
100
101static int stm32_gpio_set_value(struct udevice *dev, unsigned offset, 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;
Vikas Manocha77417102017-04-10 15:02:57 -0700106
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100107 idx = stm32_offset_to_index(dev, offset);
108 if (idx < 0)
109 return idx;
110
111 writel(BSRR_BIT(idx, value), &regs->bsrr);
Vikas Manocha77417102017-04-10 15:02:57 -0700112
113 return 0;
114}
115
Patrice Chotardcad73242018-10-24 14:10:21 +0200116static int stm32_gpio_get_function(struct udevice *dev, unsigned int offset)
117{
118 struct stm32_gpio_priv *priv = dev_get_priv(dev);
119 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100120 int bits_index;
121 int mask;
122 int idx;
Patrice Chotardcad73242018-10-24 14:10:21 +0200123 u32 mode;
124
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100125 idx = stm32_offset_to_index(dev, offset);
126 if (idx < 0)
127 return idx;
128
129 bits_index = MODE_BITS(idx);
130 mask = MODE_BITS_MASK << bits_index;
131
Patrice Chotardcad73242018-10-24 14:10:21 +0200132 mode = (readl(&regs->moder) & mask) >> bits_index;
133 if (mode == STM32_GPIO_MODE_OUT)
134 return GPIOF_OUTPUT;
135 if (mode == STM32_GPIO_MODE_IN)
136 return GPIOF_INPUT;
137 if (mode == STM32_GPIO_MODE_AN)
138 return GPIOF_UNUSED;
139
140 return GPIOF_FUNC;
141}
142
Vikas Manocha77417102017-04-10 15:02:57 -0700143static const struct dm_gpio_ops gpio_stm32_ops = {
144 .direction_input = stm32_gpio_direction_input,
145 .direction_output = stm32_gpio_direction_output,
146 .get_value = stm32_gpio_get_value,
147 .set_value = stm32_gpio_set_value,
Patrice Chotardcad73242018-10-24 14:10:21 +0200148 .get_function = stm32_gpio_get_function,
Vikas Manocha77417102017-04-10 15:02:57 -0700149};
Patrice Chotard4fb22462019-01-04 10:55:06 +0100150#endif
Vikas Manocha77417102017-04-10 15:02:57 -0700151
152static int gpio_stm32_probe(struct udevice *dev)
153{
Vikas Manocha77417102017-04-10 15:02:57 -0700154 struct stm32_gpio_priv *priv = dev_get_priv(dev);
Patrice Chotard8b6d45a2018-12-03 10:52:53 +0100155 struct clk clk;
Vikas Manocha77417102017-04-10 15:02:57 -0700156 fdt_addr_t addr;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100157 int ret;
Vikas Manocha77417102017-04-10 15:02:57 -0700158
Patrick Delaunayd876eaf2018-03-12 10:46:07 +0100159 addr = dev_read_addr(dev);
Vikas Manocha77417102017-04-10 15:02:57 -0700160 if (addr == FDT_ADDR_T_NONE)
161 return -EINVAL;
162
163 priv->regs = (struct stm32_gpio_regs *)addr;
Patrice Chotard4fb22462019-01-04 10:55:06 +0100164
165#ifndef CONFIG_SPL_BUILD
166 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
167 struct ofnode_phandle_args args;
168 const char *name;
169 int i;
170
Patrick Delaunayd876eaf2018-03-12 10:46:07 +0100171 name = dev_read_string(dev, "st,bank-name");
Vikas Manocha77417102017-04-10 15:02:57 -0700172 if (!name)
173 return -EINVAL;
174 uc_priv->bank_name = name;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100175
176 i = 0;
177 ret = dev_read_phandle_with_args(dev, "gpio-ranges",
178 NULL, 3, i, &args);
179
Patrice Chotard39a8f0b2019-01-04 10:55:05 +0100180 if (ret == -ENOENT) {
181 uc_priv->gpio_count = STM32_GPIOS_PER_BANK;
182 priv->gpio_range = GENMASK(STM32_GPIOS_PER_BANK - 1, 0);
183 }
184
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100185 while (ret != -ENOENT) {
186 priv->gpio_range |= GENMASK(args.args[2] + args.args[0] - 1,
187 args.args[0]);
188
189 uc_priv->gpio_count += args.args[2];
190
191 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
192 ++i, &args);
193 }
194
195 dev_dbg(dev, "addr = 0x%p bank_name = %s gpio_count = %d gpio_range = 0x%x\n",
196 (u32 *)priv->regs, uc_priv->bank_name, uc_priv->gpio_count,
197 priv->gpio_range);
Patrice Chotard4fb22462019-01-04 10:55:06 +0100198#endif
Vikas Manocha77417102017-04-10 15:02:57 -0700199 ret = clk_get_by_index(dev, 0, &clk);
200 if (ret < 0)
201 return ret;
202
203 ret = clk_enable(&clk);
204
205 if (ret) {
206 dev_err(dev, "failed to enable clock\n");
207 return ret;
208 }
209 debug("clock enabled for device %s\n", dev->name);
Vikas Manocha77417102017-04-10 15:02:57 -0700210
211 return 0;
212}
213
Vikas Manocha77417102017-04-10 15:02:57 -0700214U_BOOT_DRIVER(gpio_stm32) = {
215 .name = "gpio_stm32",
216 .id = UCLASS_GPIO,
Vikas Manocha77417102017-04-10 15:02:57 -0700217 .probe = gpio_stm32_probe,
Patrice Chotard4fb22462019-01-04 10:55:06 +0100218#ifndef CONFIG_SPL_BUILD
Vikas Manocha77417102017-04-10 15:02:57 -0700219 .ops = &gpio_stm32_ops,
Patrice Chotard4fb22462019-01-04 10:55:06 +0100220#endif
Bin Meng695c4992018-10-24 06:36:30 -0700221 .flags = DM_UC_FLAG_SEQ_ALIAS,
Vikas Manocha77417102017-04-10 15:02:57 -0700222 .priv_auto_alloc_size = sizeof(struct stm32_gpio_priv),
223};