blob: 4f710b6b6afa096fd1652aff61615216f01c0ad5 [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
Vikas Manocha77417102017-04-10 15:02:57 -070021#define MODE_BITS(gpio_pin) (gpio_pin * 2)
22#define MODE_BITS_MASK 3
Patrice Chotard798cd702018-08-09 11:57:57 +020023#define BSRR_BIT(gpio_pin, value) BIT(gpio_pin + (value ? 0 : 16))
Vikas Manocha77417102017-04-10 15:02:57 -070024
Patrice Chotarddbf928d2018-12-03 10:52:51 +010025/*
26 * convert gpio offset to gpio index taking into account gpio holes
27 * into gpio bank
28 */
29int stm32_offset_to_index(struct udevice *dev, unsigned int offset)
30{
31 struct stm32_gpio_priv *priv = dev_get_priv(dev);
Patrick Delaunay99e14b22019-06-21 15:26:46 +020032 unsigned int idx = 0;
Patrice Chotarddbf928d2018-12-03 10:52:51 +010033 int i;
34
35 for (i = 0; i < STM32_GPIOS_PER_BANK; i++) {
36 if (priv->gpio_range & BIT(i)) {
37 if (idx == offset)
38 return idx;
39 idx++;
40 }
41 }
42 /* shouldn't happen */
43 return -EINVAL;
44}
45
Vikas Manocha77417102017-04-10 15:02:57 -070046static int stm32_gpio_direction_input(struct udevice *dev, unsigned offset)
47{
48 struct stm32_gpio_priv *priv = dev_get_priv(dev);
49 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +010050 int bits_index;
51 int mask;
52 int idx;
53
54 idx = stm32_offset_to_index(dev, offset);
55 if (idx < 0)
56 return idx;
57
58 bits_index = MODE_BITS(idx);
59 mask = MODE_BITS_MASK << bits_index;
Vikas Manocha77417102017-04-10 15:02:57 -070060
61 clrsetbits_le32(&regs->moder, mask, STM32_GPIO_MODE_IN << bits_index);
62
63 return 0;
64}
65
66static int stm32_gpio_direction_output(struct udevice *dev, unsigned offset,
67 int value)
68{
69 struct stm32_gpio_priv *priv = dev_get_priv(dev);
70 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +010071 int bits_index;
72 int mask;
73 int idx;
74
75 idx = stm32_offset_to_index(dev, offset);
76 if (idx < 0)
77 return idx;
78
79 bits_index = MODE_BITS(idx);
80 mask = MODE_BITS_MASK << bits_index;
Vikas Manocha77417102017-04-10 15:02:57 -070081
82 clrsetbits_le32(&regs->moder, mask, STM32_GPIO_MODE_OUT << bits_index);
Patrice Chotard798cd702018-08-09 11:57:57 +020083
Patrice Chotarddbf928d2018-12-03 10:52:51 +010084 writel(BSRR_BIT(idx, value), &regs->bsrr);
Vikas Manocha77417102017-04-10 15:02:57 -070085
86 return 0;
87}
88
89static int stm32_gpio_get_value(struct udevice *dev, unsigned offset)
90{
91 struct stm32_gpio_priv *priv = dev_get_priv(dev);
92 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +010093 int idx;
Vikas Manocha77417102017-04-10 15:02:57 -070094
Patrice Chotarddbf928d2018-12-03 10:52:51 +010095 idx = stm32_offset_to_index(dev, offset);
96 if (idx < 0)
97 return idx;
98
99 return readl(&regs->idr) & BIT(idx) ? 1 : 0;
Vikas Manocha77417102017-04-10 15:02:57 -0700100}
101
102static int stm32_gpio_set_value(struct udevice *dev, unsigned offset, int value)
103{
104 struct stm32_gpio_priv *priv = dev_get_priv(dev);
105 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100106 int idx;
Vikas Manocha77417102017-04-10 15:02:57 -0700107
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100108 idx = stm32_offset_to_index(dev, offset);
109 if (idx < 0)
110 return idx;
111
112 writel(BSRR_BIT(idx, value), &regs->bsrr);
Vikas Manocha77417102017-04-10 15:02:57 -0700113
114 return 0;
115}
116
Patrice Chotardcad73242018-10-24 14:10:21 +0200117static int stm32_gpio_get_function(struct udevice *dev, unsigned int offset)
118{
119 struct stm32_gpio_priv *priv = dev_get_priv(dev);
120 struct stm32_gpio_regs *regs = priv->regs;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100121 int bits_index;
122 int mask;
123 int idx;
Patrice Chotardcad73242018-10-24 14:10:21 +0200124 u32 mode;
125
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100126 idx = stm32_offset_to_index(dev, offset);
127 if (idx < 0)
128 return idx;
129
130 bits_index = MODE_BITS(idx);
131 mask = MODE_BITS_MASK << bits_index;
132
Patrice Chotardcad73242018-10-24 14:10:21 +0200133 mode = (readl(&regs->moder) & mask) >> bits_index;
134 if (mode == STM32_GPIO_MODE_OUT)
135 return GPIOF_OUTPUT;
136 if (mode == STM32_GPIO_MODE_IN)
137 return GPIOF_INPUT;
138 if (mode == STM32_GPIO_MODE_AN)
139 return GPIOF_UNUSED;
140
141 return GPIOF_FUNC;
142}
143
Vikas Manocha77417102017-04-10 15:02:57 -0700144static const struct dm_gpio_ops gpio_stm32_ops = {
145 .direction_input = stm32_gpio_direction_input,
146 .direction_output = stm32_gpio_direction_output,
147 .get_value = stm32_gpio_get_value,
148 .set_value = stm32_gpio_set_value,
Patrice Chotardcad73242018-10-24 14:10:21 +0200149 .get_function = stm32_gpio_get_function,
Vikas Manocha77417102017-04-10 15:02:57 -0700150};
151
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
Patrice Chotard4fb22462019-01-04 10:55:06 +0100165 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
166 struct ofnode_phandle_args args;
167 const char *name;
168 int i;
169
Patrick Delaunayd876eaf2018-03-12 10:46:07 +0100170 name = dev_read_string(dev, "st,bank-name");
Vikas Manocha77417102017-04-10 15:02:57 -0700171 if (!name)
172 return -EINVAL;
173 uc_priv->bank_name = name;
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100174
175 i = 0;
176 ret = dev_read_phandle_with_args(dev, "gpio-ranges",
177 NULL, 3, i, &args);
178
Patrice Chotard39a8f0b2019-01-04 10:55:05 +0100179 if (ret == -ENOENT) {
180 uc_priv->gpio_count = STM32_GPIOS_PER_BANK;
181 priv->gpio_range = GENMASK(STM32_GPIOS_PER_BANK - 1, 0);
182 }
183
Patrice Chotarddbf928d2018-12-03 10:52:51 +0100184 while (ret != -ENOENT) {
185 priv->gpio_range |= GENMASK(args.args[2] + args.args[0] - 1,
186 args.args[0]);
187
188 uc_priv->gpio_count += args.args[2];
189
190 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
191 ++i, &args);
192 }
193
194 dev_dbg(dev, "addr = 0x%p bank_name = %s gpio_count = %d gpio_range = 0x%x\n",
195 (u32 *)priv->regs, uc_priv->bank_name, uc_priv->gpio_count,
196 priv->gpio_range);
Patrick Delaunayf17412e2020-04-22 14:29:17 +0200197
Vikas Manocha77417102017-04-10 15:02:57 -0700198 ret = clk_get_by_index(dev, 0, &clk);
199 if (ret < 0)
200 return ret;
201
202 ret = clk_enable(&clk);
203
204 if (ret) {
205 dev_err(dev, "failed to enable clock\n");
206 return ret;
207 }
208 debug("clock enabled for device %s\n", dev->name);
Vikas Manocha77417102017-04-10 15:02:57 -0700209
210 return 0;
211}
212
Vikas Manocha77417102017-04-10 15:02:57 -0700213U_BOOT_DRIVER(gpio_stm32) = {
214 .name = "gpio_stm32",
215 .id = UCLASS_GPIO,
Vikas Manocha77417102017-04-10 15:02:57 -0700216 .probe = gpio_stm32_probe,
217 .ops = &gpio_stm32_ops,
Bin Meng695c4992018-10-24 06:36:30 -0700218 .flags = DM_UC_FLAG_SEQ_ALIAS,
Vikas Manocha77417102017-04-10 15:02:57 -0700219 .priv_auto_alloc_size = sizeof(struct stm32_gpio_priv),
220};