1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (C) 2020 Broadcom
*/
#include <common.h>
#include <errno.h>
#include <asm/gpio.h>
#include <asm/io.h>
#include <dm.h>
#include <dm/device_compat.h>
#include <dm/devres.h>
#include <dm/pinctrl.h>
/*
* There are five GPIO bank register. Each bank can configure max of 32 gpios.
* BANK0 - gpios 0 to 31
* BANK1 - gpios 32 to 63
* BANK2 - gpios 64 to 95
* BANK3 - gpios 96 to 127
* BANK4 - gpios 128 to 150
*
* Offset difference between consecutive bank register is 0x200
*/
#define NGPIO_PER_BANK 32
#define GPIO_BANK_SIZE 0x200
#define GPIO_BANK(pin) ((pin) / NGPIO_PER_BANK)
#define GPIO_SHIFT(pin) ((pin) % NGPIO_PER_BANK)
#define GPIO_REG(pin, reg) (GPIO_BANK_SIZE * GPIO_BANK(pin) + (reg))
/* device register offset */
#define DATA_IN_OFFSET 0x00
#define DATA_OUT_OFFSET 0x04
#define OUT_EN_OFFSET 0x08
/**
* struct iproc_gpio_pctrl_map - gpio and pinctrl mapping
* @gpio_pin: start of gpio number in gpio-ranges
* @pctrl_pin: start of pinctrl number in gpio-ranges
* @npins: total number of pins in gpio-ranges
* @node: list node
*/
struct iproc_gpio_pctrl_map {
u32 gpio_pin;
u32 pctrl_pin;
u32 npins;
struct list_head node;
};
/**
* struct iproc_gpio_pctrl_map - gpio device instance
* @pinctrl_dev:pointer to pinctrl device
* @gpiomap: list node having mapping between gpio and pinctrl
* @base: I/O register base address of gpio device
* @name: gpio device name, ex GPIO0, GPIO1
* @ngpios: total number of gpios
*/
struct iproc_gpio_plat {
struct udevice *pinctrl_dev;
struct list_head gpiomap;
void __iomem *base;
char *name;
u32 ngpios;
};
/**
* iproc_gpio_set_bit - set or clear one bit in an iproc GPIO register.
*
* The bit relates to a GPIO pin.
*
* @plat: iproc GPIO device
* @reg: register offset
* @gpio: GPIO pin
* @set: set or clear
*/
static inline void iproc_gpio_set_bit(struct iproc_gpio_plat *plat,
u32 reg, u32 gpio, bool set)
{
u32 offset = GPIO_REG(gpio, reg);
u32 shift = GPIO_SHIFT(gpio);
clrsetbits_le32(plat->base + offset, BIT(shift),
(set ? BIT(shift) : 0));
}
static inline bool iproc_gpio_get_bit(struct iproc_gpio_plat *plat,
u32 reg, u32 gpio)
{
u32 offset = GPIO_REG(gpio, reg);
u32 shift = GPIO_SHIFT(gpio);
return readl(plat->base + offset) & BIT(shift);
}
/**
* iproc_get_gpio_pctrl_mapping() - get associated pinctrl pin from gpio pin
*
* @plat: iproc GPIO device
* @gpio: GPIO pin
*/
static u32 iproc_get_pctrl_from_gpio(struct iproc_gpio_plat *plat, u32 gpio)
{
struct iproc_gpio_pctrl_map *range = NULL;
struct list_head *pos, *tmp;
u32 ret = 0;
list_for_each_safe(pos, tmp, &plat->gpiomap) {
range = list_entry(pos, struct iproc_gpio_pctrl_map, node);
if (gpio == range->gpio_pin ||
gpio < (range->gpio_pin + range->npins)) {
ret = range->pctrl_pin + (gpio - range->gpio_pin);
break;
}
}
return ret;
}
/**
* iproc_get_gpio_pctrl_mapping() - get mapping between gpio and pinctrl
*
* Read dt node "gpio-ranges" to get gpio and pinctrl mapping and store
* in private data structure to use it later while enabling gpio.
*
* @dev: pointer to GPIO device
* @return 0 on success and -ENOMEM on failure
*/
static int iproc_get_gpio_pctrl_mapping(struct udevice *dev)
{
struct iproc_gpio_plat *plat = dev_get_plat(dev);
struct iproc_gpio_pctrl_map *range = NULL;
struct ofnode_phandle_args args;
int index = 0, ret;
for (;; index++) {
ret = dev_read_phandle_with_args(dev, "gpio-ranges",
NULL, 3, index, &args);
if (ret)
break;
range = devm_kzalloc(dev, sizeof(*range), GFP_KERNEL);
if (!range)
return -ENOMEM;
range->gpio_pin = args.args[0];
range->pctrl_pin = args.args[1];
range->npins = args.args[2];
list_add_tail(&range->node, &plat->gpiomap);
}
return 0;
}
static int iproc_gpio_request(struct udevice *dev, u32 gpio, const char *label)
{
struct iproc_gpio_plat *plat = dev_get_plat(dev);
u32 pctrl;
/* nothing to do if there is no corresponding pinctrl device */
if (!plat->pinctrl_dev)
return 0;
pctrl = iproc_get_pctrl_from_gpio(plat, gpio);
return pinctrl_request(plat->pinctrl_dev, pctrl, 0);
}
static int iproc_gpio_direction_input(struct udevice *dev, u32 gpio)
{
struct iproc_gpio_plat *plat = dev_get_plat(dev);
iproc_gpio_set_bit(plat, OUT_EN_OFFSET, gpio, false);
dev_dbg(dev, "gpio:%u set input\n", gpio);
return 0;
}
static int iproc_gpio_direction_output(struct udevice *dev, u32 gpio, int value)
{
struct iproc_gpio_plat *plat = dev_get_plat(dev);
iproc_gpio_set_bit(plat, OUT_EN_OFFSET, gpio, true);
iproc_gpio_set_bit(plat, DATA_OUT_OFFSET, gpio, value);
dev_dbg(dev, "gpio:%u set output, value:%d\n", gpio, value);
return 0;
}
static int iproc_gpio_get_value(struct udevice *dev, u32 gpio)
{
struct iproc_gpio_plat *plat = dev_get_plat(dev);
int value;
value = iproc_gpio_get_bit(plat, DATA_IN_OFFSET, gpio);
dev_dbg(dev, "gpio:%u get, value:%d\n", gpio, value);
return value;
}
static int iproc_gpio_set_value(struct udevice *dev, u32 gpio, int value)
{
struct iproc_gpio_plat *plat = dev_get_plat(dev);
if (iproc_gpio_get_bit(plat, OUT_EN_OFFSET, gpio))
iproc_gpio_set_bit(plat, DATA_OUT_OFFSET, gpio, value);
dev_dbg(dev, "gpio:%u set, value:%d\n", gpio, value);
return 0;
}
static int iproc_gpio_get_function(struct udevice *dev, u32 gpio)
{
struct iproc_gpio_plat *plat = dev_get_plat(dev);
if (iproc_gpio_get_bit(plat, OUT_EN_OFFSET, gpio))
return GPIOF_OUTPUT;
else
return GPIOF_INPUT;
}
static int iproc_gpio_of_to_plat(struct udevice *dev)
{
struct iproc_gpio_plat *plat = dev_get_plat(dev);
struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
int ret;
char name[10];
plat->base = dev_read_addr_ptr(dev);
if (!plat->base) {
debug("%s: Failed to get base address\n", __func__);
return -EINVAL;
}
ret = dev_read_u32(dev, "ngpios", &plat->ngpios);
if (ret < 0) {
dev_err(dev, "%s: Failed to get ngpios\n", __func__);
return ret;
}
uclass_get_device_by_phandle(UCLASS_PINCTRL, dev, "gpio-ranges",
&plat->pinctrl_dev);
if (ret < 0) {
dev_err(dev, "%s: Failed to get pinctrl phandle\n", __func__);
return ret;
}
INIT_LIST_HEAD(&plat->gpiomap);
ret = iproc_get_gpio_pctrl_mapping(dev);
if (ret < 0) {
dev_err(dev, "%s: Failed to get gpio to pctrl map ret(%d)\n",
__func__, ret);
return ret;
}
snprintf(name, sizeof(name), "GPIO%d", dev_seq(dev));
plat->name = strdup(name);
if (!plat->name)
return -ENOMEM;
uc_priv->gpio_count = plat->ngpios;
uc_priv->bank_name = plat->name;
dev_info(dev, ":bank name(%s) base %p, #gpios %d\n",
plat->name, plat->base, plat->ngpios);
return 0;
}
static const struct dm_gpio_ops iproc_gpio_ops = {
.request = iproc_gpio_request,
.direction_input = iproc_gpio_direction_input,
.direction_output = iproc_gpio_direction_output,
.get_value = iproc_gpio_get_value,
.set_value = iproc_gpio_set_value,
.get_function = iproc_gpio_get_function,
};
static const struct udevice_id iproc_gpio_ids[] = {
{ .compatible = "brcm,iproc-gpio" },
{ }
};
U_BOOT_DRIVER(iproc_gpio) = {
.name = "iproc_gpio",
.id = UCLASS_GPIO,
.of_match = iproc_gpio_ids,
.ops = &iproc_gpio_ops,
.of_to_plat = iproc_gpio_of_to_plat,
.plat_auto = sizeof(struct iproc_gpio_plat),
};
|