blob: 76f35ac5d9258c7746eed571d5d80032ea47c9f3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Minkyu Kang1edf0f22010-02-12 18:17:52 +09002/*
3 * (C) Copyright 2009 Samsung Electronics
4 * Minkyu Kang <mk7.kang@samsung.com>
Minkyu Kang1edf0f22010-02-12 18:17:52 +09005 */
6
7#include <common.h>
Simon Glass9b4e0e82014-10-20 19:48:40 -06008#include <dm.h>
9#include <errno.h>
10#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass9b4e0e82014-10-20 19:48:40 -060012#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Minkyu Kang1edf0f22010-02-12 18:17:52 +090014#include <asm/io.h>
Joe Hershbergerf8928f12011-11-11 15:55:36 -060015#include <asm/gpio.h>
Simon Glass9b4e0e82014-10-20 19:48:40 -060016#include <dm/device-internal.h>
17
18DECLARE_GLOBAL_DATA_PTR;
Minkyu Kang1edf0f22010-02-12 18:17:52 +090019
Akshay Saraswat1376cdd2014-05-13 10:30:14 +053020#define S5P_GPIO_GET_PIN(x) (x % GPIO_PER_BANK)
Przemyslaw Marczakb18c47e2014-01-22 11:24:10 +010021
Simon Glass3898ab72014-10-20 19:48:38 -060022#define CON_MASK(val) (0xf << ((val) << 2))
23#define CON_SFR(gpio, cfg) ((cfg) << ((gpio) << 2))
24#define CON_SFR_UNSHIFT(val, gpio) ((val) >> ((gpio) << 2))
Minkyu Kang1edf0f22010-02-12 18:17:52 +090025
Simon Glass3898ab72014-10-20 19:48:38 -060026#define DAT_MASK(gpio) (0x1 << (gpio))
27#define DAT_SET(gpio) (0x1 << (gpio))
Minkyu Kang1edf0f22010-02-12 18:17:52 +090028
Simon Glass3898ab72014-10-20 19:48:38 -060029#define PULL_MASK(gpio) (0x3 << ((gpio) << 1))
30#define PULL_MODE(gpio, pull) ((pull) << ((gpio) << 1))
Minkyu Kang1edf0f22010-02-12 18:17:52 +090031
Simon Glass3898ab72014-10-20 19:48:38 -060032#define DRV_MASK(gpio) (0x3 << ((gpio) << 1))
33#define DRV_SET(gpio, mode) ((mode) << ((gpio) << 1))
34#define RATE_MASK(gpio) (0x1 << (gpio + 16))
35#define RATE_SET(gpio) (0x1 << (gpio + 16))
Minkyu Kang1edf0f22010-02-12 18:17:52 +090036
Simon Glass9b4e0e82014-10-20 19:48:40 -060037/* Platform data for each bank */
Simon Glassb75b15b2020-12-03 16:55:23 -070038struct exynos_gpio_plat {
Simon Glass9b4e0e82014-10-20 19:48:40 -060039 struct s5p_gpio_bank *bank;
40 const char *bank_name; /* Name of port, e.g. 'gpa0" */
41};
Akshay Saraswat1376cdd2014-05-13 10:30:14 +053042
Simon Glass9b4e0e82014-10-20 19:48:40 -060043/* Information about each bank at run-time */
44struct exynos_bank_info {
Simon Glass9b4e0e82014-10-20 19:48:40 -060045 struct s5p_gpio_bank *bank;
46};
Akshay Saraswat1376cdd2014-05-13 10:30:14 +053047
Simon Glass9b4e0e82014-10-20 19:48:40 -060048static struct s5p_gpio_bank *s5p_gpio_get_bank(unsigned int gpio)
49{
50 const struct gpio_info *data;
51 unsigned int upto;
52 int i, count;
53
54 data = get_gpio_data();
55 count = get_bank_num();
56 upto = 0;
Akshay Saraswat1376cdd2014-05-13 10:30:14 +053057
Simon Glass9b4e0e82014-10-20 19:48:40 -060058 for (i = 0; i < count; i++) {
59 debug("i=%d, upto=%d\n", i, upto);
60 if (gpio < data->max_gpio) {
61 struct s5p_gpio_bank *bank;
62 bank = (struct s5p_gpio_bank *)data->reg_addr;
63 bank += (gpio - upto) / GPIO_PER_BANK;
64 debug("gpio=%d, bank=%p\n", gpio, bank);
65 return bank;
Akshay Saraswat1376cdd2014-05-13 10:30:14 +053066 }
Simon Glass9b4e0e82014-10-20 19:48:40 -060067
68 upto = data->max_gpio;
69 data++;
70 }
Akshay Saraswat1376cdd2014-05-13 10:30:14 +053071
Simon Glass9b4e0e82014-10-20 19:48:40 -060072 return NULL;
Akshay Saraswat1376cdd2014-05-13 10:30:14 +053073}
74
75static void s5p_gpio_cfg_pin(struct s5p_gpio_bank *bank, int gpio, int cfg)
Minkyu Kang1edf0f22010-02-12 18:17:52 +090076{
77 unsigned int value;
78
79 value = readl(&bank->con);
80 value &= ~CON_MASK(gpio);
81 value |= CON_SFR(gpio, cfg);
82 writel(value, &bank->con);
83}
84
Akshay Saraswat1376cdd2014-05-13 10:30:14 +053085static void s5p_gpio_set_value(struct s5p_gpio_bank *bank, int gpio, int en)
Minkyu Kang1edf0f22010-02-12 18:17:52 +090086{
87 unsigned int value;
88
89 value = readl(&bank->dat);
90 value &= ~DAT_MASK(gpio);
91 if (en)
92 value |= DAT_SET(gpio);
93 writel(value, &bank->dat);
94}
95
Simon Glass9b4e0e82014-10-20 19:48:40 -060096#ifdef CONFIG_SPL_BUILD
97/* Common GPIO API - SPL does not support driver model yet */
98int gpio_set_value(unsigned gpio, int value)
Akshay Saraswat1376cdd2014-05-13 10:30:14 +053099{
Simon Glass9b4e0e82014-10-20 19:48:40 -0600100 s5p_gpio_set_value(s5p_gpio_get_bank(gpio),
101 s5p_gpio_get_pin(gpio), value);
Akshay Saraswat1376cdd2014-05-13 10:30:14 +0530102
Simon Glass9b4e0e82014-10-20 19:48:40 -0600103 return 0;
104}
105#else
106static int s5p_gpio_get_cfg_pin(struct s5p_gpio_bank *bank, int gpio)
Akshay Saraswat1376cdd2014-05-13 10:30:14 +0530107{
Simon Glass9b4e0e82014-10-20 19:48:40 -0600108 unsigned int value;
109
110 value = readl(&bank->con);
111 value &= CON_MASK(gpio);
112 return CON_SFR_UNSHIFT(value, gpio);
Akshay Saraswat1376cdd2014-05-13 10:30:14 +0530113}
114
115static unsigned int s5p_gpio_get_value(struct s5p_gpio_bank *bank, int gpio)
Minkyu Kang1edf0f22010-02-12 18:17:52 +0900116{
117 unsigned int value;
118
119 value = readl(&bank->dat);
120 return !!(value & DAT_MASK(gpio));
121}
Simon Glass9b4e0e82014-10-20 19:48:40 -0600122#endif /* CONFIG_SPL_BUILD */
Minkyu Kang1edf0f22010-02-12 18:17:52 +0900123
Akshay Saraswat1376cdd2014-05-13 10:30:14 +0530124static void s5p_gpio_set_pull(struct s5p_gpio_bank *bank, int gpio, int mode)
Minkyu Kang1edf0f22010-02-12 18:17:52 +0900125{
126 unsigned int value;
127
128 value = readl(&bank->pull);
129 value &= ~PULL_MASK(gpio);
130
131 switch (mode) {
Akshay Saraswat1376cdd2014-05-13 10:30:14 +0530132 case S5P_GPIO_PULL_DOWN:
133 case S5P_GPIO_PULL_UP:
Minkyu Kang1edf0f22010-02-12 18:17:52 +0900134 value |= PULL_MODE(gpio, mode);
135 break;
136 default:
Minkyu Kangced528f2010-05-28 12:34:29 +0900137 break;
Minkyu Kang1edf0f22010-02-12 18:17:52 +0900138 }
139
140 writel(value, &bank->pull);
141}
142
Akshay Saraswat1376cdd2014-05-13 10:30:14 +0530143static void s5p_gpio_set_drv(struct s5p_gpio_bank *bank, int gpio, int mode)
Minkyu Kang1edf0f22010-02-12 18:17:52 +0900144{
145 unsigned int value;
146
147 value = readl(&bank->drv);
148 value &= ~DRV_MASK(gpio);
149
150 switch (mode) {
Akshay Saraswat1376cdd2014-05-13 10:30:14 +0530151 case S5P_GPIO_DRV_1X:
152 case S5P_GPIO_DRV_2X:
153 case S5P_GPIO_DRV_3X:
154 case S5P_GPIO_DRV_4X:
Minkyu Kang1edf0f22010-02-12 18:17:52 +0900155 value |= DRV_SET(gpio, mode);
156 break;
157 default:
158 return;
159 }
160
161 writel(value, &bank->drv);
162}
163
Akshay Saraswat1376cdd2014-05-13 10:30:14 +0530164static void s5p_gpio_set_rate(struct s5p_gpio_bank *bank, int gpio, int mode)
Minkyu Kang1edf0f22010-02-12 18:17:52 +0900165{
166 unsigned int value;
167
168 value = readl(&bank->drv);
169 value &= ~RATE_MASK(gpio);
170
171 switch (mode) {
Akshay Saraswat1376cdd2014-05-13 10:30:14 +0530172 case S5P_GPIO_DRV_FAST:
173 case S5P_GPIO_DRV_SLOW:
Minkyu Kang1edf0f22010-02-12 18:17:52 +0900174 value |= RATE_SET(gpio);
175 break;
176 default:
177 return;
178 }
179
180 writel(value, &bank->drv);
181}
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000182
Simon Glass9b4e0e82014-10-20 19:48:40 -0600183int s5p_gpio_get_pin(unsigned gpio)
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000184{
Simon Glass9b4e0e82014-10-20 19:48:40 -0600185 return S5P_GPIO_GET_PIN(gpio);
186}
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000187
Simon Glass9b4e0e82014-10-20 19:48:40 -0600188/* Driver model interface */
189#ifndef CONFIG_SPL_BUILD
Simon Glass9b4e0e82014-10-20 19:48:40 -0600190/* set GPIO pin 'gpio' as an input */
191static int exynos_gpio_direction_input(struct udevice *dev, unsigned offset)
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000192{
Simon Glass9b4e0e82014-10-20 19:48:40 -0600193 struct exynos_bank_info *state = dev_get_priv(dev);
Simon Glass9b4e0e82014-10-20 19:48:40 -0600194
195 /* Configure GPIO direction as input. */
196 s5p_gpio_cfg_pin(state->bank, offset, S5P_GPIO_INPUT);
197
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000198 return 0;
199}
200
Simon Glass9b4e0e82014-10-20 19:48:40 -0600201/* set GPIO pin 'gpio' as an output, with polarity 'value' */
202static int exynos_gpio_direction_output(struct udevice *dev, unsigned offset,
203 int value)
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000204{
Simon Glass9b4e0e82014-10-20 19:48:40 -0600205 struct exynos_bank_info *state = dev_get_priv(dev);
Simon Glass9b4e0e82014-10-20 19:48:40 -0600206
207 /* Configure GPIO output value. */
208 s5p_gpio_set_value(state->bank, offset, value);
209
210 /* Configure GPIO direction as output. */
211 s5p_gpio_cfg_pin(state->bank, offset, S5P_GPIO_OUTPUT);
212
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000213 return 0;
214}
215
Simon Glass9b4e0e82014-10-20 19:48:40 -0600216/* read GPIO IN value of pin 'gpio' */
217static int exynos_gpio_get_value(struct udevice *dev, unsigned offset)
218{
219 struct exynos_bank_info *state = dev_get_priv(dev);
Simon Glass9b4e0e82014-10-20 19:48:40 -0600220
221 return s5p_gpio_get_value(state->bank, offset);
222}
223
224/* write GPIO OUT value to pin 'gpio' */
225static int exynos_gpio_set_value(struct udevice *dev, unsigned offset,
226 int value)
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000227{
Simon Glass9b4e0e82014-10-20 19:48:40 -0600228 struct exynos_bank_info *state = dev_get_priv(dev);
Simon Glass9b4e0e82014-10-20 19:48:40 -0600229
230 s5p_gpio_set_value(state->bank, offset, value);
231
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600232 return 0;
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000233}
Simon Glass9b4e0e82014-10-20 19:48:40 -0600234#endif /* nCONFIG_SPL_BUILD */
Akshay Saraswat1376cdd2014-05-13 10:30:14 +0530235
Simon Glass9b4e0e82014-10-20 19:48:40 -0600236/*
237 * There is no common GPIO API for pull, drv, pin, rate (yet). These
238 * functions are kept here to preserve function ordering for review.
239 */
Akshay Saraswat1376cdd2014-05-13 10:30:14 +0530240void gpio_set_pull(int gpio, int mode)
241{
242 s5p_gpio_set_pull(s5p_gpio_get_bank(gpio),
243 s5p_gpio_get_pin(gpio), mode);
244}
245
246void gpio_set_drv(int gpio, int mode)
247{
248 s5p_gpio_set_drv(s5p_gpio_get_bank(gpio),
249 s5p_gpio_get_pin(gpio), mode);
250}
251
252void gpio_cfg_pin(int gpio, int cfg)
253{
254 s5p_gpio_cfg_pin(s5p_gpio_get_bank(gpio),
255 s5p_gpio_get_pin(gpio), cfg);
256}
257
258void gpio_set_rate(int gpio, int mode)
259{
260 s5p_gpio_set_rate(s5p_gpio_get_bank(gpio),
261 s5p_gpio_get_pin(gpio), mode);
262}
Simon Glass9b4e0e82014-10-20 19:48:40 -0600263
264#ifndef CONFIG_SPL_BUILD
265static int exynos_gpio_get_function(struct udevice *dev, unsigned offset)
266{
267 struct exynos_bank_info *state = dev_get_priv(dev);
268 int cfg;
269
Simon Glass9b4e0e82014-10-20 19:48:40 -0600270 cfg = s5p_gpio_get_cfg_pin(state->bank, offset);
271 if (cfg == S5P_GPIO_OUTPUT)
272 return GPIOF_OUTPUT;
273 else if (cfg == S5P_GPIO_INPUT)
274 return GPIOF_INPUT;
275 else
276 return GPIOF_FUNC;
277}
278
279static const struct dm_gpio_ops gpio_exynos_ops = {
Simon Glass9b4e0e82014-10-20 19:48:40 -0600280 .direction_input = exynos_gpio_direction_input,
281 .direction_output = exynos_gpio_direction_output,
282 .get_value = exynos_gpio_get_value,
283 .set_value = exynos_gpio_set_value,
284 .get_function = exynos_gpio_get_function,
Simon Glass9b4e0e82014-10-20 19:48:40 -0600285};
286
287static int gpio_exynos_probe(struct udevice *dev)
288{
Simon Glassde0977b2015-03-05 12:25:20 -0700289 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass95588622020-12-22 19:30:28 -0700290 struct exynos_bank_info *priv = dev_get_priv(dev);
291 struct exynos_gpio_plat *plat = dev_get_plat(dev);
Simon Glass9b4e0e82014-10-20 19:48:40 -0600292
293 /* Only child devices have ports */
294 if (!plat)
295 return 0;
296
297 priv->bank = plat->bank;
298
299 uc_priv->gpio_count = GPIO_PER_BANK;
300 uc_priv->bank_name = plat->bank_name;
301
302 return 0;
303}
304
305/**
306 * We have a top-level GPIO device with no actual GPIOs. It has a child
307 * device for each Exynos GPIO bank.
308 */
309static int gpio_exynos_bind(struct udevice *parent)
310{
Simon Glass95588622020-12-22 19:30:28 -0700311 struct exynos_gpio_plat *plat = dev_get_plat(parent);
Simon Glass9b4e0e82014-10-20 19:48:40 -0600312 struct s5p_gpio_bank *bank, *base;
313 const void *blob = gd->fdt_blob;
314 int node;
315
316 /* If this is a child device, there is nothing to do here */
317 if (plat)
318 return 0;
319
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900320 base = dev_read_addr_ptr(parent);
Simon Glassdd79d6e2017-01-17 16:52:55 -0700321 for (node = fdt_first_subnode(blob, dev_of_offset(parent)), bank = base;
Simon Glass9b4e0e82014-10-20 19:48:40 -0600322 node > 0;
323 node = fdt_next_subnode(blob, node), bank++) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700324 struct exynos_gpio_plat *plat;
Simon Glass9b4e0e82014-10-20 19:48:40 -0600325 struct udevice *dev;
326 fdt_addr_t reg;
327 int ret;
328
329 if (!fdtdec_get_bool(blob, node, "gpio-controller"))
330 continue;
331 plat = calloc(1, sizeof(*plat));
332 if (!plat)
333 return -ENOMEM;
Simon Glass9b4e0e82014-10-20 19:48:40 -0600334
Przemyslaw Marczak85ed2b42015-09-30 13:14:51 +0200335 plat->bank_name = fdt_get_name(blob, node, NULL);
Simon Glass6996c662020-11-28 17:50:03 -0700336 ret = device_bind(parent, parent->driver, plat->bank_name, plat,
Simon Glass9030b392020-11-28 17:50:04 -0700337 offset_to_ofnode(node), &dev);
Simon Glass9b4e0e82014-10-20 19:48:40 -0600338 if (ret)
339 return ret;
Przemyslaw Marczak85ed2b42015-09-30 13:14:51 +0200340
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900341 reg = dev_read_addr(dev);
Przemyslaw Marczak85ed2b42015-09-30 13:14:51 +0200342 if (reg != FDT_ADDR_T_NONE)
343 bank = (struct s5p_gpio_bank *)((ulong)base + reg);
344
345 plat->bank = bank;
346
347 debug("dev at %p: %s\n", bank, plat->bank_name);
Simon Glass9b4e0e82014-10-20 19:48:40 -0600348 }
349
350 return 0;
351}
352
353static const struct udevice_id exynos_gpio_ids[] = {
354 { .compatible = "samsung,s5pc100-pinctrl" },
355 { .compatible = "samsung,s5pc110-pinctrl" },
356 { .compatible = "samsung,exynos4210-pinctrl" },
357 { .compatible = "samsung,exynos4x12-pinctrl" },
358 { .compatible = "samsung,exynos5250-pinctrl" },
359 { .compatible = "samsung,exynos5420-pinctrl" },
360 { }
361};
362
363U_BOOT_DRIVER(gpio_exynos) = {
364 .name = "gpio_exynos",
365 .id = UCLASS_GPIO,
366 .of_match = exynos_gpio_ids,
367 .bind = gpio_exynos_bind,
368 .probe = gpio_exynos_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700369 .priv_auto = sizeof(struct exynos_bank_info),
Simon Glass9b4e0e82014-10-20 19:48:40 -0600370 .ops = &gpio_exynos_ops,
371};
372#endif