]>
Commit | Line | Data |
---|---|---|
4e5ae09e | 1 | /* |
00a2749d | 2 | * NVIDIA Tegra20 GPIO handling. |
fe82857c | 3 | * (C) Copyright 2010-2012,2015 |
4e5ae09e TW |
4 | * NVIDIA Corporation <www.nvidia.com> |
5 | * | |
1a459660 | 6 | * SPDX-License-Identifier: GPL-2.0+ |
4e5ae09e TW |
7 | */ |
8 | ||
9 | /* | |
10 | * Based on (mostly copied from) kw_gpio.c based Linux 2.6 kernel driver. | |
11 | * Tom Warren ([email protected]) | |
12 | */ | |
13 | ||
14 | #include <common.h> | |
2fccd2d9 SG |
15 | #include <dm.h> |
16 | #include <malloc.h> | |
17 | #include <errno.h> | |
18 | #include <fdtdec.h> | |
4e5ae09e TW |
19 | #include <asm/io.h> |
20 | #include <asm/bitops.h> | |
150c2493 | 21 | #include <asm/arch/tegra.h> |
4e5ae09e | 22 | #include <asm/gpio.h> |
2fccd2d9 | 23 | #include <dm/device-internal.h> |
838aa5c9 | 24 | #include <dt-bindings/gpio/gpio.h> |
2fccd2d9 SG |
25 | |
26 | DECLARE_GLOBAL_DATA_PTR; | |
4e5ae09e | 27 | |
fe82857c SW |
28 | static const int CONFIG_SFIO = 0; |
29 | static const int CONFIG_GPIO = 1; | |
30 | static const int DIRECTION_INPUT = 0; | |
31 | static const int DIRECTION_OUTPUT = 1; | |
32 | ||
2fccd2d9 SG |
33 | struct tegra_gpio_platdata { |
34 | struct gpio_ctlr_bank *bank; | |
35 | const char *port_name; /* Name of port, e.g. "B" */ | |
36 | int base_gpio; /* Port number for this port (0, 1,.., n-1) */ | |
37 | }; | |
4e5ae09e | 38 | |
2fccd2d9 SG |
39 | /* Information about each port at run-time */ |
40 | struct tegra_port_info { | |
2fccd2d9 SG |
41 | struct gpio_ctlr_bank *bank; |
42 | int base_gpio; /* Port number for this port (0, 1,.., n-1) */ | |
43 | }; | |
4e5ae09e | 44 | |
fe82857c | 45 | /* Return config of pin 'gpio' as GPIO (1) or SFIO (0) */ |
365d6070 | 46 | static int get_config(unsigned gpio) |
4e5ae09e | 47 | { |
365d6070 JH |
48 | struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE; |
49 | struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)]; | |
4e5ae09e TW |
50 | u32 u; |
51 | int type; | |
52 | ||
365d6070 | 53 | u = readl(&bank->gpio_config[GPIO_PORT(gpio)]); |
fe82857c | 54 | type = (u >> GPIO_BIT(gpio)) & 1; |
4e5ae09e TW |
55 | |
56 | debug("get_config: port = %d, bit = %d is %s\n", | |
365d6070 | 57 | GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO"); |
4e5ae09e | 58 | |
fe82857c | 59 | return type ? CONFIG_GPIO : CONFIG_SFIO; |
4e5ae09e TW |
60 | } |
61 | ||
fe82857c | 62 | /* Config pin 'gpio' as GPIO or SFIO, based on 'type' */ |
365d6070 | 63 | static void set_config(unsigned gpio, int type) |
4e5ae09e | 64 | { |
365d6070 JH |
65 | struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE; |
66 | struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)]; | |
4e5ae09e TW |
67 | u32 u; |
68 | ||
69 | debug("set_config: port = %d, bit = %d, %s\n", | |
365d6070 | 70 | GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO"); |
4e5ae09e | 71 | |
365d6070 | 72 | u = readl(&bank->gpio_config[GPIO_PORT(gpio)]); |
fe82857c | 73 | if (type != CONFIG_SFIO) |
365d6070 | 74 | u |= 1 << GPIO_BIT(gpio); |
4e5ae09e | 75 | else |
365d6070 JH |
76 | u &= ~(1 << GPIO_BIT(gpio)); |
77 | writel(u, &bank->gpio_config[GPIO_PORT(gpio)]); | |
4e5ae09e TW |
78 | } |
79 | ||
365d6070 JH |
80 | /* Return GPIO pin 'gpio' direction - 0 = input or 1 = output */ |
81 | static int get_direction(unsigned gpio) | |
4e5ae09e | 82 | { |
365d6070 JH |
83 | struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE; |
84 | struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)]; | |
4e5ae09e TW |
85 | u32 u; |
86 | int dir; | |
87 | ||
365d6070 JH |
88 | u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]); |
89 | dir = (u >> GPIO_BIT(gpio)) & 1; | |
4e5ae09e TW |
90 | |
91 | debug("get_direction: port = %d, bit = %d, %s\n", | |
365d6070 | 92 | GPIO_FULLPORT(gpio), GPIO_BIT(gpio), dir ? "OUT" : "IN"); |
4e5ae09e | 93 | |
fe82857c | 94 | return dir ? DIRECTION_OUTPUT : DIRECTION_INPUT; |
4e5ae09e TW |
95 | } |
96 | ||
365d6070 JH |
97 | /* Config GPIO pin 'gpio' as input or output (OE) as per 'output' */ |
98 | static void set_direction(unsigned gpio, int output) | |
4e5ae09e | 99 | { |
365d6070 JH |
100 | struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE; |
101 | struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)]; | |
4e5ae09e TW |
102 | u32 u; |
103 | ||
104 | debug("set_direction: port = %d, bit = %d, %s\n", | |
365d6070 | 105 | GPIO_FULLPORT(gpio), GPIO_BIT(gpio), output ? "OUT" : "IN"); |
4e5ae09e | 106 | |
365d6070 | 107 | u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]); |
fe82857c | 108 | if (output != DIRECTION_INPUT) |
365d6070 | 109 | u |= 1 << GPIO_BIT(gpio); |
4e5ae09e | 110 | else |
365d6070 JH |
111 | u &= ~(1 << GPIO_BIT(gpio)); |
112 | writel(u, &bank->gpio_dir_out[GPIO_PORT(gpio)]); | |
4e5ae09e TW |
113 | } |
114 | ||
365d6070 JH |
115 | /* set GPIO pin 'gpio' output bit as 0 or 1 as per 'high' */ |
116 | static void set_level(unsigned gpio, int high) | |
4e5ae09e | 117 | { |
365d6070 JH |
118 | struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE; |
119 | struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)]; | |
4e5ae09e TW |
120 | u32 u; |
121 | ||
122 | debug("set_level: port = %d, bit %d == %d\n", | |
365d6070 | 123 | GPIO_FULLPORT(gpio), GPIO_BIT(gpio), high); |
4e5ae09e | 124 | |
365d6070 | 125 | u = readl(&bank->gpio_out[GPIO_PORT(gpio)]); |
4e5ae09e | 126 | if (high) |
365d6070 | 127 | u |= 1 << GPIO_BIT(gpio); |
4e5ae09e | 128 | else |
365d6070 JH |
129 | u &= ~(1 << GPIO_BIT(gpio)); |
130 | writel(u, &bank->gpio_out[GPIO_PORT(gpio)]); | |
4e5ae09e TW |
131 | } |
132 | ||
133 | /* | |
134 | * Generic_GPIO primitives. | |
135 | */ | |
136 | ||
365d6070 | 137 | /* set GPIO pin 'gpio' as an input */ |
2fccd2d9 | 138 | static int tegra_gpio_direction_input(struct udevice *dev, unsigned offset) |
4e5ae09e | 139 | { |
2fccd2d9 | 140 | struct tegra_port_info *state = dev_get_priv(dev); |
4e5ae09e TW |
141 | |
142 | /* Configure GPIO direction as input. */ | |
fe82857c | 143 | set_direction(state->base_gpio + offset, DIRECTION_INPUT); |
4e5ae09e | 144 | |
0c35e3a8 SW |
145 | /* Enable the pin as a GPIO */ |
146 | set_config(state->base_gpio + offset, 1); | |
147 | ||
4e5ae09e TW |
148 | return 0; |
149 | } | |
150 | ||
365d6070 | 151 | /* set GPIO pin 'gpio' as an output, with polarity 'value' */ |
2fccd2d9 SG |
152 | static int tegra_gpio_direction_output(struct udevice *dev, unsigned offset, |
153 | int value) | |
4e5ae09e | 154 | { |
2fccd2d9 SG |
155 | struct tegra_port_info *state = dev_get_priv(dev); |
156 | int gpio = state->base_gpio + offset; | |
4e5ae09e TW |
157 | |
158 | /* Configure GPIO output value. */ | |
365d6070 | 159 | set_level(gpio, value); |
4e5ae09e TW |
160 | |
161 | /* Configure GPIO direction as output. */ | |
fe82857c | 162 | set_direction(gpio, DIRECTION_OUTPUT); |
4e5ae09e | 163 | |
0c35e3a8 SW |
164 | /* Enable the pin as a GPIO */ |
165 | set_config(state->base_gpio + offset, 1); | |
166 | ||
4e5ae09e TW |
167 | return 0; |
168 | } | |
169 | ||
365d6070 | 170 | /* read GPIO IN value of pin 'gpio' */ |
2fccd2d9 | 171 | static int tegra_gpio_get_value(struct udevice *dev, unsigned offset) |
4e5ae09e | 172 | { |
2fccd2d9 SG |
173 | struct tegra_port_info *state = dev_get_priv(dev); |
174 | int gpio = state->base_gpio + offset; | |
4e5ae09e TW |
175 | int val; |
176 | ||
2fccd2d9 SG |
177 | debug("%s: pin = %d (port %d:bit %d)\n", __func__, |
178 | gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio)); | |
4e5ae09e | 179 | |
651827c0 SG |
180 | if (get_direction(gpio) == DIRECTION_INPUT) |
181 | val = readl(&state->bank->gpio_in[GPIO_PORT(gpio)]); | |
182 | else | |
183 | val = readl(&state->bank->gpio_out[GPIO_PORT(gpio)]); | |
4e5ae09e | 184 | |
365d6070 | 185 | return (val >> GPIO_BIT(gpio)) & 1; |
4e5ae09e TW |
186 | } |
187 | ||
365d6070 | 188 | /* write GPIO OUT value to pin 'gpio' */ |
2fccd2d9 | 189 | static int tegra_gpio_set_value(struct udevice *dev, unsigned offset, int value) |
4e5ae09e | 190 | { |
2fccd2d9 SG |
191 | struct tegra_port_info *state = dev_get_priv(dev); |
192 | int gpio = state->base_gpio + offset; | |
2fccd2d9 | 193 | |
4e5ae09e | 194 | debug("gpio_set_value: pin = %d (port %d:bit %d), value = %d\n", |
2fccd2d9 | 195 | gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio), value); |
4e5ae09e TW |
196 | |
197 | /* Configure GPIO output value. */ | |
365d6070 JH |
198 | set_level(gpio, value); |
199 | ||
200 | return 0; | |
4e5ae09e TW |
201 | } |
202 | ||
eceb3f26 SW |
203 | void gpio_config_table(const struct tegra_gpio_config *config, int len) |
204 | { | |
205 | int i; | |
206 | ||
207 | for (i = 0; i < len; i++) { | |
208 | switch (config[i].init) { | |
209 | case TEGRA_GPIO_INIT_IN: | |
fe82857c | 210 | set_direction(config[i].gpio, DIRECTION_INPUT); |
eceb3f26 SW |
211 | break; |
212 | case TEGRA_GPIO_INIT_OUT0: | |
f9d3cab0 | 213 | set_level(config[i].gpio, 0); |
fe82857c | 214 | set_direction(config[i].gpio, DIRECTION_OUTPUT); |
eceb3f26 SW |
215 | break; |
216 | case TEGRA_GPIO_INIT_OUT1: | |
f9d3cab0 | 217 | set_level(config[i].gpio, 1); |
fe82857c | 218 | set_direction(config[i].gpio, DIRECTION_OUTPUT); |
eceb3f26 SW |
219 | break; |
220 | } | |
fe82857c | 221 | set_config(config[i].gpio, CONFIG_GPIO); |
eceb3f26 SW |
222 | } |
223 | } | |
224 | ||
2fccd2d9 SG |
225 | static int tegra_gpio_get_function(struct udevice *dev, unsigned offset) |
226 | { | |
227 | struct tegra_port_info *state = dev_get_priv(dev); | |
228 | int gpio = state->base_gpio + offset; | |
229 | ||
2fccd2d9 SG |
230 | if (!get_config(gpio)) |
231 | return GPIOF_FUNC; | |
232 | else if (get_direction(gpio)) | |
233 | return GPIOF_OUTPUT; | |
234 | else | |
235 | return GPIOF_INPUT; | |
236 | } | |
237 | ||
838aa5c9 | 238 | static int tegra_gpio_xlate(struct udevice *dev, struct gpio_desc *desc, |
3a57123e | 239 | struct ofnode_phandle_args *args) |
838aa5c9 SG |
240 | { |
241 | int gpio, port, ret; | |
242 | ||
243 | gpio = args->args[0]; | |
244 | port = gpio / TEGRA_GPIOS_PER_PORT; | |
245 | ret = device_get_child(dev, port, &desc->dev); | |
246 | if (ret) | |
247 | return ret; | |
248 | desc->offset = gpio % TEGRA_GPIOS_PER_PORT; | |
249 | desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0; | |
250 | ||
251 | return 0; | |
252 | } | |
253 | ||
2fccd2d9 | 254 | static const struct dm_gpio_ops gpio_tegra_ops = { |
2fccd2d9 SG |
255 | .direction_input = tegra_gpio_direction_input, |
256 | .direction_output = tegra_gpio_direction_output, | |
257 | .get_value = tegra_gpio_get_value, | |
258 | .set_value = tegra_gpio_set_value, | |
259 | .get_function = tegra_gpio_get_function, | |
838aa5c9 | 260 | .xlate = tegra_gpio_xlate, |
2fccd2d9 SG |
261 | }; |
262 | ||
263 | /** | |
264 | * Returns the name of a GPIO port | |
265 | * | |
266 | * GPIOs are named A, B, C, ..., Z, AA, BB, CC, ... | |
267 | * | |
268 | * @base_port: Base port number (0, 1..n-1) | |
269 | * @return allocated string containing the name | |
4e5ae09e | 270 | */ |
2fccd2d9 | 271 | static char *gpio_port_name(int base_port) |
4e5ae09e | 272 | { |
2fccd2d9 SG |
273 | char *name, *s; |
274 | ||
275 | name = malloc(3); | |
276 | if (name) { | |
277 | s = name; | |
278 | *s++ = 'A' + (base_port % 26); | |
279 | if (base_port >= 26) | |
280 | *s++ = *name; | |
281 | *s = '\0'; | |
282 | } | |
4e5ae09e | 283 | |
2fccd2d9 SG |
284 | return name; |
285 | } | |
286 | ||
287 | static const struct udevice_id tegra_gpio_ids[] = { | |
288 | { .compatible = "nvidia,tegra30-gpio" }, | |
289 | { .compatible = "nvidia,tegra20-gpio" }, | |
290 | { } | |
291 | }; | |
292 | ||
293 | static int gpio_tegra_probe(struct udevice *dev) | |
294 | { | |
e564f054 | 295 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
2fccd2d9 SG |
296 | struct tegra_port_info *priv = dev->priv; |
297 | struct tegra_gpio_platdata *plat = dev->platdata; | |
298 | ||
299 | /* Only child devices have ports */ | |
300 | if (!plat) | |
301 | return 0; | |
302 | ||
303 | priv->bank = plat->bank; | |
304 | priv->base_gpio = plat->base_gpio; | |
305 | ||
306 | uc_priv->gpio_count = TEGRA_GPIOS_PER_PORT; | |
307 | uc_priv->bank_name = plat->port_name; | |
308 | ||
309 | return 0; | |
310 | } | |
311 | ||
312 | /** | |
313 | * We have a top-level GPIO device with no actual GPIOs. It has a child | |
314 | * device for each Tegra port. | |
315 | */ | |
316 | static int gpio_tegra_bind(struct udevice *parent) | |
317 | { | |
318 | struct tegra_gpio_platdata *plat = parent->platdata; | |
319 | struct gpio_ctlr *ctlr; | |
320 | int bank_count; | |
321 | int bank; | |
322 | int ret; | |
2fccd2d9 SG |
323 | |
324 | /* If this is a child device, there is nothing to do here */ | |
325 | if (plat) | |
326 | return 0; | |
327 | ||
bdfb3416 SG |
328 | /* TODO([email protected]): Remove once SPL supports device tree */ |
329 | #ifdef CONFIG_SPL_BUILD | |
330 | ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE; | |
331 | bank_count = TEGRA_GPIO_BANKS; | |
332 | #else | |
333 | { | |
334 | int len; | |
335 | ||
2fccd2d9 SG |
336 | /* |
337 | * This driver does not make use of interrupts, other than to figure | |
338 | * out the number of GPIO banks | |
339 | */ | |
e160f7d4 SG |
340 | if (!fdt_getprop(gd->fdt_blob, dev_of_offset(parent), "interrupts", |
341 | &len)) | |
2fccd2d9 SG |
342 | return -EINVAL; |
343 | bank_count = len / 3 / sizeof(u32); | |
a821c4af | 344 | ctlr = (struct gpio_ctlr *)devfdt_get_addr(parent); |
bdfb3416 SG |
345 | } |
346 | #endif | |
2fccd2d9 SG |
347 | for (bank = 0; bank < bank_count; bank++) { |
348 | int port; | |
349 | ||
350 | for (port = 0; port < TEGRA_PORTS_PER_BANK; port++) { | |
351 | struct tegra_gpio_platdata *plat; | |
352 | struct udevice *dev; | |
353 | int base_port; | |
354 | ||
355 | plat = calloc(1, sizeof(*plat)); | |
356 | if (!plat) | |
357 | return -ENOMEM; | |
358 | plat->bank = &ctlr->gpio_bank[bank]; | |
359 | base_port = bank * TEGRA_PORTS_PER_BANK + port; | |
360 | plat->base_gpio = TEGRA_GPIOS_PER_PORT * base_port; | |
361 | plat->port_name = gpio_port_name(base_port); | |
362 | ||
363 | ret = device_bind(parent, parent->driver, | |
364 | plat->port_name, plat, -1, &dev); | |
365 | if (ret) | |
366 | return ret; | |
e160f7d4 | 367 | dev_set_of_offset(dev, dev_of_offset(parent)); |
2fccd2d9 | 368 | } |
4e5ae09e | 369 | } |
2fccd2d9 SG |
370 | |
371 | return 0; | |
4e5ae09e | 372 | } |
2fccd2d9 SG |
373 | |
374 | U_BOOT_DRIVER(gpio_tegra) = { | |
375 | .name = "gpio_tegra", | |
376 | .id = UCLASS_GPIO, | |
377 | .of_match = tegra_gpio_ids, | |
378 | .bind = gpio_tegra_bind, | |
379 | .probe = gpio_tegra_probe, | |
380 | .priv_auto_alloc_size = sizeof(struct tegra_port_info), | |
381 | .ops = &gpio_tegra_ops, | |
bdfb3416 | 382 | .flags = DM_FLAG_PRE_RELOC, |
2fccd2d9 | 383 | }; |