blob: c0a92378b035aa0b5da35f3fbc699acda4b78df2 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michal Simek23ccda02013-04-24 10:01:20 +02002/*
Michal Simek68877972018-07-12 16:05:46 +02003 * Copyright (c) 2013 - 2018 Xilinx, Michal Simek
Michal Simek23ccda02013-04-24 10:01:20 +02004 */
5
Michal Simek23ccda02013-04-24 10:01:20 +02006#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Michal Simek23ccda02013-04-24 10:01:20 +02008#include <malloc.h>
9#include <linux/list.h>
10#include <asm/io.h>
11#include <asm/gpio.h>
Michal Simek68877972018-07-12 16:05:46 +020012#include <dm.h>
Michal Simeke2f91e52018-07-23 13:40:01 +020013#include <dt-bindings/gpio/gpio.h>
Michal Simek23ccda02013-04-24 10:01:20 +020014
Michal Simeke2f91e52018-07-23 13:40:01 +020015#define XILINX_GPIO_MAX_BANK 2
Michal Simek23ccda02013-04-24 10:01:20 +020016
17/* Gpio simple map */
18struct gpio_regs {
19 u32 gpiodata;
20 u32 gpiodir;
21};
22
Simon Glassb75b15b2020-12-03 16:55:23 -070023struct xilinx_gpio_plat {
Michal Simek68877972018-07-12 16:05:46 +020024 struct gpio_regs *regs;
25 int bank_max[XILINX_GPIO_MAX_BANK];
26 int bank_input[XILINX_GPIO_MAX_BANK];
27 int bank_output[XILINX_GPIO_MAX_BANK];
Michal Simek810e4bc2018-07-23 12:40:36 +020028 u32 dout_default[XILINX_GPIO_MAX_BANK];
Michal Simek68877972018-07-12 16:05:46 +020029};
30
Michal Simek810e4bc2018-07-23 12:40:36 +020031struct xilinx_gpio_privdata {
32 u32 output_val[XILINX_GPIO_MAX_BANK];
33};
34
Michal Simek68877972018-07-12 16:05:46 +020035static int xilinx_gpio_get_bank_pin(unsigned offset, u32 *bank_num,
36 u32 *bank_pin_num, struct udevice *dev)
37{
Simon Glassb75b15b2020-12-03 16:55:23 -070038 struct xilinx_gpio_plat *plat = dev_get_plat(dev);
Michal Simek68877972018-07-12 16:05:46 +020039 u32 bank, max_pins;
40 /* the first gpio is 0 not 1 */
41 u32 pin_num = offset;
42
43 for (bank = 0; bank < XILINX_GPIO_MAX_BANK; bank++) {
Simon Glass71fa5b42020-12-03 16:55:18 -070044 max_pins = plat->bank_max[bank];
Michal Simek68877972018-07-12 16:05:46 +020045 if (pin_num < max_pins) {
46 debug("%s: found at bank 0x%x pin 0x%x\n", __func__,
47 bank, pin_num);
48 *bank_num = bank;
49 *bank_pin_num = pin_num;
50 return 0;
51 }
52 pin_num -= max_pins;
53 }
54
55 return -EINVAL;
56}
57
58static int xilinx_gpio_set_value(struct udevice *dev, unsigned offset,
59 int value)
60{
Simon Glassb75b15b2020-12-03 16:55:23 -070061 struct xilinx_gpio_plat *plat = dev_get_plat(dev);
Michal Simek810e4bc2018-07-23 12:40:36 +020062 struct xilinx_gpio_privdata *priv = dev_get_priv(dev);
Michal Simek68877972018-07-12 16:05:46 +020063 int val, ret;
64 u32 bank, pin;
65
66 ret = xilinx_gpio_get_bank_pin(offset, &bank, &pin, dev);
67 if (ret)
68 return ret;
69
Michal Simek810e4bc2018-07-23 12:40:36 +020070 val = priv->output_val[bank];
Michal Simekee39afb2018-07-30 14:29:27 +020071
Michal Simek810e4bc2018-07-23 12:40:36 +020072 debug("%s: regs: %lx, value: %x, gpio: %x, bank %x, pin %x, out %x\n",
Simon Glass71fa5b42020-12-03 16:55:18 -070073 __func__, (ulong)plat->regs, value, offset, bank, pin, val);
Michal Simek68877972018-07-12 16:05:46 +020074
Michal Simekee39afb2018-07-30 14:29:27 +020075 if (value)
Michal Simek68877972018-07-12 16:05:46 +020076 val = val | (1 << pin);
Michal Simekee39afb2018-07-30 14:29:27 +020077 else
Michal Simek68877972018-07-12 16:05:46 +020078 val = val & ~(1 << pin);
Michal Simekee39afb2018-07-30 14:29:27 +020079
Simon Glass71fa5b42020-12-03 16:55:18 -070080 writel(val, &plat->regs->gpiodata + bank * 2);
Michal Simek68877972018-07-12 16:05:46 +020081
Michal Simek810e4bc2018-07-23 12:40:36 +020082 priv->output_val[bank] = val;
83
Michal Simek7fa52c22018-08-06 07:42:40 +020084 return 0;
Michal Simek68877972018-07-12 16:05:46 +020085};
86
87static int xilinx_gpio_get_value(struct udevice *dev, unsigned offset)
88{
Simon Glassb75b15b2020-12-03 16:55:23 -070089 struct xilinx_gpio_plat *plat = dev_get_plat(dev);
Michal Simek810e4bc2018-07-23 12:40:36 +020090 struct xilinx_gpio_privdata *priv = dev_get_priv(dev);
Michal Simek68877972018-07-12 16:05:46 +020091 int val, ret;
92 u32 bank, pin;
93
94 ret = xilinx_gpio_get_bank_pin(offset, &bank, &pin, dev);
95 if (ret)
96 return ret;
97
98 debug("%s: regs: %lx, gpio: %x, bank %x, pin %x\n", __func__,
Simon Glass71fa5b42020-12-03 16:55:18 -070099 (ulong)plat->regs, offset, bank, pin);
Michal Simek68877972018-07-12 16:05:46 +0200100
Simon Glass71fa5b42020-12-03 16:55:18 -0700101 if (plat->bank_output[bank]) {
Michal Simek810e4bc2018-07-23 12:40:36 +0200102 debug("%s: Read saved output value\n", __func__);
103 val = priv->output_val[bank];
104 } else {
105 debug("%s: Read input value from reg\n", __func__);
Simon Glass71fa5b42020-12-03 16:55:18 -0700106 val = readl(&plat->regs->gpiodata + bank * 2);
Michal Simek810e4bc2018-07-23 12:40:36 +0200107 }
108
Michal Simek68877972018-07-12 16:05:46 +0200109 val = !!(val & (1 << pin));
110
111 return val;
112};
113
114static int xilinx_gpio_get_function(struct udevice *dev, unsigned offset)
115{
Simon Glassb75b15b2020-12-03 16:55:23 -0700116 struct xilinx_gpio_plat *plat = dev_get_plat(dev);
Michal Simek68877972018-07-12 16:05:46 +0200117 int val, ret;
118 u32 bank, pin;
119
Michal Simek9260d992018-07-23 12:08:49 +0200120 ret = xilinx_gpio_get_bank_pin(offset, &bank, &pin, dev);
121 if (ret)
122 return ret;
123
Michal Simek68877972018-07-12 16:05:46 +0200124 /* Check if all pins are inputs */
Simon Glass71fa5b42020-12-03 16:55:18 -0700125 if (plat->bank_input[bank])
Michal Simek68877972018-07-12 16:05:46 +0200126 return GPIOF_INPUT;
127
128 /* Check if all pins are outputs */
Simon Glass71fa5b42020-12-03 16:55:18 -0700129 if (plat->bank_output[bank])
Michal Simek68877972018-07-12 16:05:46 +0200130 return GPIOF_OUTPUT;
131
Michal Simek68877972018-07-12 16:05:46 +0200132 /* FIXME test on dual */
Simon Glass71fa5b42020-12-03 16:55:18 -0700133 val = readl(&plat->regs->gpiodir + bank * 2);
Michal Simek68877972018-07-12 16:05:46 +0200134 val = !(val & (1 << pin));
135
136 /* input is 1 in reg but GPIOF_INPUT is 0 */
137 /* output is 0 in reg but GPIOF_OUTPUT is 1 */
138
139 return val;
140}
141
142static int xilinx_gpio_direction_output(struct udevice *dev, unsigned offset,
143 int value)
144{
Simon Glassb75b15b2020-12-03 16:55:23 -0700145 struct xilinx_gpio_plat *plat = dev_get_plat(dev);
Michal Simek68877972018-07-12 16:05:46 +0200146 int val, ret;
147 u32 bank, pin;
148
149 ret = xilinx_gpio_get_bank_pin(offset, &bank, &pin, dev);
150 if (ret)
151 return ret;
152
153 /* can't change it if all is input by default */
Simon Glass71fa5b42020-12-03 16:55:18 -0700154 if (plat->bank_input[bank])
Michal Simek68877972018-07-12 16:05:46 +0200155 return -EINVAL;
156
Michal Simekc2116f32018-07-30 10:02:53 +0200157 xilinx_gpio_set_value(dev, offset, value);
158
Simon Glass71fa5b42020-12-03 16:55:18 -0700159 if (!plat->bank_output[bank]) {
160 val = readl(&plat->regs->gpiodir + bank * 2);
Michal Simek68877972018-07-12 16:05:46 +0200161 val = val & ~(1 << pin);
Simon Glass71fa5b42020-12-03 16:55:18 -0700162 writel(val, &plat->regs->gpiodir + bank * 2);
Michal Simek68877972018-07-12 16:05:46 +0200163 }
164
Michal Simek68877972018-07-12 16:05:46 +0200165 return 0;
166}
167
168static int xilinx_gpio_direction_input(struct udevice *dev, unsigned offset)
169{
Simon Glassb75b15b2020-12-03 16:55:23 -0700170 struct xilinx_gpio_plat *plat = dev_get_plat(dev);
Michal Simek68877972018-07-12 16:05:46 +0200171 int val, ret;
172 u32 bank, pin;
173
174 ret = xilinx_gpio_get_bank_pin(offset, &bank, &pin, dev);
175 if (ret)
176 return ret;
177
178 /* Already input */
Simon Glass71fa5b42020-12-03 16:55:18 -0700179 if (plat->bank_input[bank])
Michal Simek68877972018-07-12 16:05:46 +0200180 return 0;
181
182 /* can't change it if all is output by default */
Simon Glass71fa5b42020-12-03 16:55:18 -0700183 if (plat->bank_output[bank])
Michal Simek68877972018-07-12 16:05:46 +0200184 return -EINVAL;
185
Simon Glass71fa5b42020-12-03 16:55:18 -0700186 val = readl(&plat->regs->gpiodir + bank * 2);
Michal Simek68877972018-07-12 16:05:46 +0200187 val = val | (1 << pin);
Simon Glass71fa5b42020-12-03 16:55:18 -0700188 writel(val, &plat->regs->gpiodir + bank * 2);
Michal Simek68877972018-07-12 16:05:46 +0200189
190 return 0;
191}
192
193static int xilinx_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
194 struct ofnode_phandle_args *args)
195{
Simon Glassb75b15b2020-12-03 16:55:23 -0700196 struct xilinx_gpio_plat *plat = dev_get_plat(dev);
Michal Simek68877972018-07-12 16:05:46 +0200197
198 desc->offset = args->args[0];
199
200 debug("%s: argc: %x, [0]: %x, [1]: %x, [2]: %x\n", __func__,
201 args->args_count, args->args[0], args->args[1], args->args[2]);
202
203 /*
204 * The second cell is channel offset:
205 * 0 is first channel, 8 is second channel
206 *
207 * U-Boot driver just combine channels together that's why simply
208 * add amount of pins in second channel if present.
209 */
210 if (args->args[1]) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700211 if (!plat->bank_max[1]) {
Michal Simek68877972018-07-12 16:05:46 +0200212 printf("%s: %s has no second channel\n",
213 __func__, dev->name);
214 return -EINVAL;
215 }
216
Simon Glass71fa5b42020-12-03 16:55:18 -0700217 desc->offset += plat->bank_max[0];
Michal Simek68877972018-07-12 16:05:46 +0200218 }
219
220 /* The third cell is optional */
221 if (args->args_count > 2)
222 desc->flags = (args->args[2] &
223 GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0);
224
225 debug("%s: offset %x, flags %lx\n",
226 __func__, desc->offset, desc->flags);
227 return 0;
228}
229
230static const struct dm_gpio_ops xilinx_gpio_ops = {
231 .direction_input = xilinx_gpio_direction_input,
232 .direction_output = xilinx_gpio_direction_output,
233 .get_value = xilinx_gpio_get_value,
234 .set_value = xilinx_gpio_set_value,
235 .get_function = xilinx_gpio_get_function,
236 .xlate = xilinx_gpio_xlate,
237};
238
239static int xilinx_gpio_probe(struct udevice *dev)
240{
Simon Glassb75b15b2020-12-03 16:55:23 -0700241 struct xilinx_gpio_plat *plat = dev_get_plat(dev);
Michal Simek810e4bc2018-07-23 12:40:36 +0200242 struct xilinx_gpio_privdata *priv = dev_get_priv(dev);
Michal Simek68877972018-07-12 16:05:46 +0200243 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Michal Simek305f1c52018-08-02 12:58:54 +0200244 const void *label_ptr;
Michal Simek68877972018-07-12 16:05:46 +0200245
Michal Simek305f1c52018-08-02 12:58:54 +0200246 label_ptr = dev_read_prop(dev, "label", NULL);
247 if (label_ptr) {
248 uc_priv->bank_name = strdup(label_ptr);
249 if (!uc_priv->bank_name)
250 return -ENOMEM;
251 } else {
252 uc_priv->bank_name = dev->name;
253 }
Michal Simek68877972018-07-12 16:05:46 +0200254
Simon Glass71fa5b42020-12-03 16:55:18 -0700255 uc_priv->gpio_count = plat->bank_max[0] + plat->bank_max[1];
Michal Simek68877972018-07-12 16:05:46 +0200256
Simon Glass71fa5b42020-12-03 16:55:18 -0700257 priv->output_val[0] = plat->dout_default[0];
Michal Simek810e4bc2018-07-23 12:40:36 +0200258
Simon Glass71fa5b42020-12-03 16:55:18 -0700259 if (plat->bank_max[1])
260 priv->output_val[1] = plat->dout_default[1];
Michal Simek810e4bc2018-07-23 12:40:36 +0200261
Michal Simek68877972018-07-12 16:05:46 +0200262 return 0;
263}
264
Simon Glassaad29ae2020-12-03 16:55:21 -0700265static int xilinx_gpio_of_to_plat(struct udevice *dev)
Michal Simek68877972018-07-12 16:05:46 +0200266{
Simon Glassb75b15b2020-12-03 16:55:23 -0700267 struct xilinx_gpio_plat *plat = dev_get_plat(dev);
Michal Simek68877972018-07-12 16:05:46 +0200268 int is_dual;
269
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100270 plat->regs = dev_read_addr_ptr(dev);
Michal Simek68877972018-07-12 16:05:46 +0200271
Simon Glass71fa5b42020-12-03 16:55:18 -0700272 plat->bank_max[0] = dev_read_u32_default(dev, "xlnx,gpio-width", 0);
273 plat->bank_input[0] = dev_read_u32_default(dev, "xlnx,all-inputs", 0);
274 plat->bank_output[0] = dev_read_u32_default(dev, "xlnx,all-outputs", 0);
275 plat->dout_default[0] = dev_read_u32_default(dev, "xlnx,dout-default",
276 0);
Michal Simek68877972018-07-12 16:05:46 +0200277
278 is_dual = dev_read_u32_default(dev, "xlnx,is-dual", 0);
279 if (is_dual) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700280 plat->bank_max[1] = dev_read_u32_default(dev,
281 "xlnx,gpio2-width", 0);
282 plat->bank_input[1] = dev_read_u32_default(dev,
Michal Simek68877972018-07-12 16:05:46 +0200283 "xlnx,all-inputs-2", 0);
Simon Glass71fa5b42020-12-03 16:55:18 -0700284 plat->bank_output[1] = dev_read_u32_default(dev,
Michal Simek68877972018-07-12 16:05:46 +0200285 "xlnx,all-outputs-2", 0);
Simon Glass71fa5b42020-12-03 16:55:18 -0700286 plat->dout_default[1] = dev_read_u32_default(dev,
Michal Simek810e4bc2018-07-23 12:40:36 +0200287 "xlnx,dout-default-2", 0);
Michal Simek68877972018-07-12 16:05:46 +0200288 }
289
290 return 0;
291}
292
293static const struct udevice_id xilinx_gpio_ids[] = {
294 { .compatible = "xlnx,xps-gpio-1.00.a",},
295 { }
296};
297
298U_BOOT_DRIVER(xilinx_gpio) = {
299 .name = "xlnx_gpio",
300 .id = UCLASS_GPIO,
301 .ops = &xilinx_gpio_ops,
302 .of_match = xilinx_gpio_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700303 .of_to_plat = xilinx_gpio_of_to_plat,
Michal Simek68877972018-07-12 16:05:46 +0200304 .probe = xilinx_gpio_probe,
Simon Glassb75b15b2020-12-03 16:55:23 -0700305 .plat_auto = sizeof(struct xilinx_gpio_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700306 .priv_auto = sizeof(struct xilinx_gpio_privdata),
Michal Simek68877972018-07-12 16:05:46 +0200307};