blob: 48b52c985a55d0d2201912c9ec67b8682aa6a405 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michal Simek4e779ad2013-04-24 10:01:20 +02002/*
Michal Simek10441ec2018-07-12 16:05:46 +02003 * Copyright (c) 2013 - 2018 Xilinx, Michal Simek
Michal Simek4e779ad2013-04-24 10:01:20 +02004 */
5
6#include <common.h>
7#include <errno.h>
8#include <malloc.h>
9#include <linux/list.h>
10#include <asm/io.h>
11#include <asm/gpio.h>
Michal Simek10441ec2018-07-12 16:05:46 +020012#include <dm.h>
Michal Simek4e779ad2013-04-24 10:01:20 +020013
14static LIST_HEAD(gpio_list);
15
16enum gpio_direction {
17 GPIO_DIRECTION_OUT = 0,
18 GPIO_DIRECTION_IN = 1,
19};
20
21/* Gpio simple map */
22struct gpio_regs {
23 u32 gpiodata;
24 u32 gpiodir;
25};
26
Michal Simek10441ec2018-07-12 16:05:46 +020027#if !defined(CONFIG_DM_GPIO)
28
Michal Simek4e779ad2013-04-24 10:01:20 +020029#define GPIO_NAME_SIZE 10
30
31struct gpio_names {
32 char name[GPIO_NAME_SIZE];
33};
34
35/* Initialized, rxbd_current, rx_first_buf must be 0 after init */
36struct xilinx_gpio_priv {
37 struct gpio_regs *regs;
38 u32 gpio_min;
39 u32 gpio_max;
40 u32 gpiodata_store;
41 char name[GPIO_NAME_SIZE];
42 struct list_head list;
43 struct gpio_names *gpio_name;
44};
45
46/* Store number of allocated gpio pins */
47static u32 xilinx_gpio_max;
48
49/* Get associated gpio controller */
50static struct xilinx_gpio_priv *gpio_get_controller(unsigned gpio)
51{
52 struct list_head *entry;
53 struct xilinx_gpio_priv *priv = NULL;
54
55 list_for_each(entry, &gpio_list) {
56 priv = list_entry(entry, struct xilinx_gpio_priv, list);
57 if (gpio >= priv->gpio_min && gpio <= priv->gpio_max) {
58 debug("%s: reg: %x, min-max: %d-%d\n", __func__,
59 (u32)priv->regs, priv->gpio_min, priv->gpio_max);
60 return priv;
61 }
62 }
63 puts("!!!Can't get gpio controller!!!\n");
64 return NULL;
65}
66
67/* Get gpio pin name if used/setup */
68static char *get_name(unsigned gpio)
69{
70 u32 gpio_priv;
71 struct xilinx_gpio_priv *priv;
72
73 debug("%s\n", __func__);
74
75 priv = gpio_get_controller(gpio);
76 if (priv) {
77 gpio_priv = gpio - priv->gpio_min;
78
79 return *priv->gpio_name[gpio_priv].name ?
80 priv->gpio_name[gpio_priv].name : "UNKNOWN";
81 }
82 return "UNKNOWN";
83}
84
85/* Get output value */
86static int gpio_get_output_value(unsigned gpio)
87{
88 u32 val, gpio_priv;
89 struct xilinx_gpio_priv *priv = gpio_get_controller(gpio);
90
91 if (priv) {
92 gpio_priv = gpio - priv->gpio_min;
93 val = !!(priv->gpiodata_store & (1 << gpio_priv));
94 debug("%s: reg: %x, gpio_no: %d, dir: %d\n", __func__,
95 (u32)priv->regs, gpio_priv, val);
96
97 return val;
98 }
99 return -1;
100}
101
102/* Get input value */
103static int gpio_get_input_value(unsigned gpio)
104{
105 u32 val, gpio_priv;
106 struct gpio_regs *regs;
107 struct xilinx_gpio_priv *priv = gpio_get_controller(gpio);
108
109 if (priv) {
110 regs = priv->regs;
111 gpio_priv = gpio - priv->gpio_min;
112 val = readl(&regs->gpiodata);
113 val = !!(val & (1 << gpio_priv));
114 debug("%s: reg: %x, gpio_no: %d, dir: %d\n", __func__,
115 (u32)priv->regs, gpio_priv, val);
116
117 return val;
118 }
119 return -1;
120}
121
122/* Set gpio direction */
123static int gpio_set_direction(unsigned gpio, enum gpio_direction direction)
124{
125 u32 val, gpio_priv;
126 struct gpio_regs *regs;
127 struct xilinx_gpio_priv *priv = gpio_get_controller(gpio);
128
129 if (priv) {
130 regs = priv->regs;
131 val = readl(&regs->gpiodir);
132
133 gpio_priv = gpio - priv->gpio_min;
134 if (direction == GPIO_DIRECTION_OUT)
135 val &= ~(1 << gpio_priv);
136 else
137 val |= 1 << gpio_priv;
138
139 writel(val, &regs->gpiodir);
140 debug("%s: reg: %x, gpio_no: %d, dir: %d\n", __func__,
141 (u32)priv->regs, gpio_priv, val);
142
143 return 0;
144 }
145
146 return -1;
147}
148
149/* Get gpio direction */
150static int gpio_get_direction(unsigned gpio)
151{
152 u32 val, gpio_priv;
153 struct gpio_regs *regs;
154 struct xilinx_gpio_priv *priv = gpio_get_controller(gpio);
155
156 if (priv) {
157 regs = priv->regs;
158 gpio_priv = gpio - priv->gpio_min;
159 val = readl(&regs->gpiodir);
160 val = !!(val & (1 << gpio_priv));
161 debug("%s: reg: %x, gpio_no: %d, dir: %d\n", __func__,
162 (u32)priv->regs, gpio_priv, val);
163
164 return val;
165 }
166
167 return -1;
168}
169
170/*
171 * Get input value
172 * for example gpio setup to output only can't get input value
173 * which is breaking gpio toggle command
174 */
175int gpio_get_value(unsigned gpio)
176{
177 u32 val;
178
179 if (gpio_get_direction(gpio) == GPIO_DIRECTION_OUT)
180 val = gpio_get_output_value(gpio);
181 else
182 val = gpio_get_input_value(gpio);
183
184 return val;
185}
186
187/* Set output value */
188static int gpio_set_output_value(unsigned gpio, int value)
189{
190 u32 val, gpio_priv;
191 struct gpio_regs *regs;
192 struct xilinx_gpio_priv *priv = gpio_get_controller(gpio);
193
194 if (priv) {
195 regs = priv->regs;
196 gpio_priv = gpio - priv->gpio_min;
197 val = priv->gpiodata_store;
198 if (value)
199 val |= 1 << gpio_priv;
200 else
201 val &= ~(1 << gpio_priv);
202
203 writel(val, &regs->gpiodata);
204 debug("%s: reg: %x, gpio_no: %d, output_val: %d\n", __func__,
205 (u32)priv->regs, gpio_priv, val);
206 priv->gpiodata_store = val;
207
208 return 0;
209 }
210
211 return -1;
212}
213
214int gpio_set_value(unsigned gpio, int value)
215{
216 if (gpio_get_direction(gpio) == GPIO_DIRECTION_OUT)
217 return gpio_set_output_value(gpio, value);
218
219 return -1;
220}
221
222/* Set GPIO as input */
223int gpio_direction_input(unsigned gpio)
224{
225 debug("%s\n", __func__);
226 return gpio_set_direction(gpio, GPIO_DIRECTION_IN);
227}
228
229/* Setup GPIO as output and set output value */
230int gpio_direction_output(unsigned gpio, int value)
231{
232 int ret = gpio_set_direction(gpio, GPIO_DIRECTION_OUT);
233
234 debug("%s\n", __func__);
235
236 if (ret < 0)
237 return ret;
238
239 return gpio_set_output_value(gpio, value);
240}
241
242/* Show gpio status */
243void gpio_info(void)
244{
245 unsigned gpio;
246
247 struct list_head *entry;
248 struct xilinx_gpio_priv *priv = NULL;
249
250 list_for_each(entry, &gpio_list) {
251 priv = list_entry(entry, struct xilinx_gpio_priv, list);
252 printf("\n%s: %s/%x (%d-%d)\n", __func__, priv->name,
253 (u32)priv->regs, priv->gpio_min, priv->gpio_max);
254
255 for (gpio = priv->gpio_min; gpio <= priv->gpio_max; gpio++) {
256 printf("GPIO_%d:\t%s is an ", gpio, get_name(gpio));
257 if (gpio_get_direction(gpio) == GPIO_DIRECTION_OUT)
258 printf("OUTPUT value = %d\n",
259 gpio_get_output_value(gpio));
260 else
261 printf("INPUT value = %d\n",
262 gpio_get_input_value(gpio));
263 }
264 }
265}
266
267int gpio_request(unsigned gpio, const char *label)
268{
269 u32 gpio_priv;
270 struct xilinx_gpio_priv *priv;
271
272 if (gpio >= xilinx_gpio_max)
273 return -EINVAL;
274
275 priv = gpio_get_controller(gpio);
276 if (priv) {
277 gpio_priv = gpio - priv->gpio_min;
278
279 if (label != NULL) {
280 strncpy(priv->gpio_name[gpio_priv].name, label,
281 GPIO_NAME_SIZE);
282 priv->gpio_name[gpio_priv].name[GPIO_NAME_SIZE - 1] =
283 '\0';
284 }
285 return 0;
286 }
287
288 return -1;
289}
290
291int gpio_free(unsigned gpio)
292{
293 u32 gpio_priv;
294 struct xilinx_gpio_priv *priv;
295
296 if (gpio >= xilinx_gpio_max)
297 return -EINVAL;
298
299 priv = gpio_get_controller(gpio);
300 if (priv) {
301 gpio_priv = gpio - priv->gpio_min;
302 priv->gpio_name[gpio_priv].name[0] = '\0';
303
304 /* Do nothing here */
305 return 0;
306 }
307
308 return -1;
309}
310
311int gpio_alloc(u32 baseaddr, const char *name, u32 gpio_no)
312{
313 struct xilinx_gpio_priv *priv;
314
315 priv = calloc(1, sizeof(struct xilinx_gpio_priv));
316
317 /* Setup gpio name */
318 if (name != NULL) {
319 strncpy(priv->name, name, GPIO_NAME_SIZE);
320 priv->name[GPIO_NAME_SIZE - 1] = '\0';
321 }
322 priv->regs = (struct gpio_regs *)baseaddr;
323
324 priv->gpio_min = xilinx_gpio_max;
325 xilinx_gpio_max = priv->gpio_min + gpio_no;
326 priv->gpio_max = xilinx_gpio_max - 1;
327
328 priv->gpio_name = calloc(gpio_no, sizeof(struct gpio_names));
329
330 INIT_LIST_HEAD(&priv->list);
331 list_add_tail(&priv->list, &gpio_list);
332
333 printf("%s: Add %s (%d-%d)\n", __func__, name,
334 priv->gpio_min, priv->gpio_max);
335
336 /* Return the first gpio allocated for this device */
337 return priv->gpio_min;
338}
339
340/* Dual channel gpio is one IP with two independent channels */
341int gpio_alloc_dual(u32 baseaddr, const char *name, u32 gpio_no0, u32 gpio_no1)
342{
343 int ret;
344
345 ret = gpio_alloc(baseaddr, name, gpio_no0);
346 gpio_alloc(baseaddr + 8, strcat((char *)name, "_1"), gpio_no1);
347
348 /* Return the first gpio allocated for this device */
349 return ret;
350}
Michal Simek10441ec2018-07-12 16:05:46 +0200351#else
352#include <dt-bindings/gpio/gpio.h>
353
354#define XILINX_GPIO_MAX_BANK 2
355
356struct xilinx_gpio_platdata {
357 struct gpio_regs *regs;
358 int bank_max[XILINX_GPIO_MAX_BANK];
359 int bank_input[XILINX_GPIO_MAX_BANK];
360 int bank_output[XILINX_GPIO_MAX_BANK];
361};
362
363static int xilinx_gpio_get_bank_pin(unsigned offset, u32 *bank_num,
364 u32 *bank_pin_num, struct udevice *dev)
365{
366 struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev);
367 u32 bank, max_pins;
368 /* the first gpio is 0 not 1 */
369 u32 pin_num = offset;
370
371 for (bank = 0; bank < XILINX_GPIO_MAX_BANK; bank++) {
372 max_pins = platdata->bank_max[bank];
373 if (pin_num < max_pins) {
374 debug("%s: found at bank 0x%x pin 0x%x\n", __func__,
375 bank, pin_num);
376 *bank_num = bank;
377 *bank_pin_num = pin_num;
378 return 0;
379 }
380 pin_num -= max_pins;
381 }
382
383 return -EINVAL;
384}
385
386static int xilinx_gpio_set_value(struct udevice *dev, unsigned offset,
387 int value)
388{
389 struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev);
390 int val, ret;
391 u32 bank, pin;
392
393 ret = xilinx_gpio_get_bank_pin(offset, &bank, &pin, dev);
394 if (ret)
395 return ret;
396
397 debug("%s: regs: %lx, value: %x, gpio: %x, bank %x, pin %x\n",
398 __func__, (ulong)platdata->regs, value, offset, bank, pin);
399
400 if (value) {
401 val = readl(&platdata->regs->gpiodata + bank * 2);
402 val = val | (1 << pin);
403 writel(val, &platdata->regs->gpiodata + bank * 2);
404 } else {
405 val = readl(&platdata->regs->gpiodata + bank * 2);
406 val = val & ~(1 << pin);
407 writel(val, &platdata->regs->gpiodata + bank * 2);
408 }
409
410 return val;
411};
412
413static int xilinx_gpio_get_value(struct udevice *dev, unsigned offset)
414{
415 struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev);
416 int val, ret;
417 u32 bank, pin;
418
419 ret = xilinx_gpio_get_bank_pin(offset, &bank, &pin, dev);
420 if (ret)
421 return ret;
422
423 debug("%s: regs: %lx, gpio: %x, bank %x, pin %x\n", __func__,
424 (ulong)platdata->regs, offset, bank, pin);
425
426 val = readl(&platdata->regs->gpiodata + bank * 2);
427 val = !!(val & (1 << pin));
428
429 return val;
430};
431
432static int xilinx_gpio_get_function(struct udevice *dev, unsigned offset)
433{
434 struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev);
435 int val, ret;
436 u32 bank, pin;
437
438 /* Check if all pins are inputs */
439 if (platdata->bank_input[bank])
440 return GPIOF_INPUT;
441
442 /* Check if all pins are outputs */
443 if (platdata->bank_output[bank])
444 return GPIOF_OUTPUT;
445
446 ret = xilinx_gpio_get_bank_pin(offset, &bank, &pin, dev);
447 if (ret)
448 return ret;
449
450 /* FIXME test on dual */
451 val = readl(&platdata->regs->gpiodir + bank * 2);
452 val = !(val & (1 << pin));
453
454 /* input is 1 in reg but GPIOF_INPUT is 0 */
455 /* output is 0 in reg but GPIOF_OUTPUT is 1 */
456
457 return val;
458}
459
460static int xilinx_gpio_direction_output(struct udevice *dev, unsigned offset,
461 int value)
462{
463 struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev);
464 int val, ret;
465 u32 bank, pin;
466
467 ret = xilinx_gpio_get_bank_pin(offset, &bank, &pin, dev);
468 if (ret)
469 return ret;
470
471 /* can't change it if all is input by default */
472 if (platdata->bank_input[bank])
473 return -EINVAL;
474
475 if (!platdata->bank_output[bank]) {
476 val = readl(&platdata->regs->gpiodir + bank * 2);
477 val = val & ~(1 << pin);
478 writel(val, &platdata->regs->gpiodir + bank * 2);
479 }
480
481 xilinx_gpio_set_value(dev, offset, value);
482
483 return 0;
484}
485
486static int xilinx_gpio_direction_input(struct udevice *dev, unsigned offset)
487{
488 struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev);
489 int val, ret;
490 u32 bank, pin;
491
492 ret = xilinx_gpio_get_bank_pin(offset, &bank, &pin, dev);
493 if (ret)
494 return ret;
495
496 /* Already input */
497 if (platdata->bank_input[bank])
498 return 0;
499
500 /* can't change it if all is output by default */
501 if (platdata->bank_output[bank])
502 return -EINVAL;
503
504 val = readl(&platdata->regs->gpiodir + bank * 2);
505 val = val | (1 << pin);
506 writel(val, &platdata->regs->gpiodir + bank * 2);
507
508 return 0;
509}
510
511static int xilinx_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
512 struct ofnode_phandle_args *args)
513{
514 struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev);
515
516 desc->offset = args->args[0];
517
518 debug("%s: argc: %x, [0]: %x, [1]: %x, [2]: %x\n", __func__,
519 args->args_count, args->args[0], args->args[1], args->args[2]);
520
521 /*
522 * The second cell is channel offset:
523 * 0 is first channel, 8 is second channel
524 *
525 * U-Boot driver just combine channels together that's why simply
526 * add amount of pins in second channel if present.
527 */
528 if (args->args[1]) {
529 if (!platdata->bank_max[1]) {
530 printf("%s: %s has no second channel\n",
531 __func__, dev->name);
532 return -EINVAL;
533 }
534
535 desc->offset += platdata->bank_max[0];
536 }
537
538 /* The third cell is optional */
539 if (args->args_count > 2)
540 desc->flags = (args->args[2] &
541 GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0);
542
543 debug("%s: offset %x, flags %lx\n",
544 __func__, desc->offset, desc->flags);
545 return 0;
546}
547
548static const struct dm_gpio_ops xilinx_gpio_ops = {
549 .direction_input = xilinx_gpio_direction_input,
550 .direction_output = xilinx_gpio_direction_output,
551 .get_value = xilinx_gpio_get_value,
552 .set_value = xilinx_gpio_set_value,
553 .get_function = xilinx_gpio_get_function,
554 .xlate = xilinx_gpio_xlate,
555};
556
557static int xilinx_gpio_probe(struct udevice *dev)
558{
559 struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev);
560 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
561
562 uc_priv->bank_name = dev->name;
563
564 uc_priv->gpio_count = platdata->bank_max[0] + platdata->bank_max[1];
565
566 return 0;
567}
568
569static int xilinx_gpio_ofdata_to_platdata(struct udevice *dev)
570{
571 struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev);
572 int is_dual;
573
574 platdata->regs = (struct gpio_regs *)dev_read_addr(dev);
575
576 platdata->bank_max[0] = dev_read_u32_default(dev,
577 "xlnx,gpio-width", 0);
578 platdata->bank_input[0] = dev_read_u32_default(dev,
579 "xlnx,all-inputs", 0);
580 platdata->bank_output[0] = dev_read_u32_default(dev,
581 "xlnx,all-outputs", 0);
582
583 is_dual = dev_read_u32_default(dev, "xlnx,is-dual", 0);
584 if (is_dual) {
585 platdata->bank_max[1] = dev_read_u32_default(dev,
586 "xlnx,gpio2-width", 0);
587 platdata->bank_input[1] = dev_read_u32_default(dev,
588 "xlnx,all-inputs-2", 0);
589 platdata->bank_output[1] = dev_read_u32_default(dev,
590 "xlnx,all-outputs-2", 0);
591 }
592
593 return 0;
594}
595
596static const struct udevice_id xilinx_gpio_ids[] = {
597 { .compatible = "xlnx,xps-gpio-1.00.a",},
598 { }
599};
600
601U_BOOT_DRIVER(xilinx_gpio) = {
602 .name = "xlnx_gpio",
603 .id = UCLASS_GPIO,
604 .ops = &xilinx_gpio_ops,
605 .of_match = xilinx_gpio_ids,
606 .ofdata_to_platdata = xilinx_gpio_ofdata_to_platdata,
607 .probe = xilinx_gpio_probe,
608 .platdata_auto_alloc_size = sizeof(struct xilinx_gpio_platdata),
609};
610#endif