blob: edc5a8093b0c46e0ad8fab58c905dc410baf6c2f [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Joachim Foerster03d67e12011-10-21 15:48:50 +02002/*
Thomas Chou88d5ecf2015-10-21 21:33:45 +08003 * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
Joachim Foerster03d67e12011-10-21 15:48:50 +02004 * Copyright (C) 2011 Missing Link Electronics
5 * Joachim Foerster <joachim@missinglinkelectronics.com>
Joachim Foerster03d67e12011-10-21 15:48:50 +02006 */
7#include <common.h>
Thomas Chou88d5ecf2015-10-21 21:33:45 +08008#include <dm.h>
9#include <errno.h>
10#include <malloc.h>
11#include <fdtdec.h>
Simon Glass401d1c42020-10-30 21:38:53 -060012#include <asm/global_data.h>
Joachim Foerster03d67e12011-10-21 15:48:50 +020013#include <asm/io.h>
14#include <asm/gpio.h>
15
Thomas Chou88d5ecf2015-10-21 21:33:45 +080016DECLARE_GLOBAL_DATA_PTR;
Joachim Foerster03d67e12011-10-21 15:48:50 +020017
Thomas Chou88d5ecf2015-10-21 21:33:45 +080018struct altera_pio_regs {
19 u32 data; /* Data register */
20 u32 direction; /* Direction register */
21};
Joachim Foerster03d67e12011-10-21 15:48:50 +020022
Simon Glass8a8d24b2020-12-03 16:55:23 -070023struct altera_pio_plat {
Thomas Chou88d5ecf2015-10-21 21:33:45 +080024 struct altera_pio_regs *regs;
25 int gpio_count;
26 const char *bank_name;
27};
Joachim Foerster03d67e12011-10-21 15:48:50 +020028
Thomas Chou88d5ecf2015-10-21 21:33:45 +080029static int altera_pio_direction_input(struct udevice *dev, unsigned pin)
Joachim Foerster03d67e12011-10-21 15:48:50 +020030{
Simon Glass8a8d24b2020-12-03 16:55:23 -070031 struct altera_pio_plat *plat = dev_get_plat(dev);
Thomas Chou88d5ecf2015-10-21 21:33:45 +080032 struct altera_pio_regs *const regs = plat->regs;
Joachim Foerster03d67e12011-10-21 15:48:50 +020033
Thomas Chou88d5ecf2015-10-21 21:33:45 +080034 clrbits_le32(&regs->direction, 1 << pin);
Joachim Foerster03d67e12011-10-21 15:48:50 +020035
Joachim Foerster03d67e12011-10-21 15:48:50 +020036 return 0;
37}
38
Thomas Chou88d5ecf2015-10-21 21:33:45 +080039static int altera_pio_direction_output(struct udevice *dev, unsigned pin,
40 int val)
Joachim Foerster03d67e12011-10-21 15:48:50 +020041{
Simon Glass8a8d24b2020-12-03 16:55:23 -070042 struct altera_pio_plat *plat = dev_get_plat(dev);
Thomas Chou88d5ecf2015-10-21 21:33:45 +080043 struct altera_pio_regs *const regs = plat->regs;
Joachim Foerster03d67e12011-10-21 15:48:50 +020044
Thomas Chou88d5ecf2015-10-21 21:33:45 +080045 if (val)
46 setbits_le32(&regs->data, 1 << pin);
Joachim Foerster03d67e12011-10-21 15:48:50 +020047 else
Thomas Chou88d5ecf2015-10-21 21:33:45 +080048 clrbits_le32(&regs->data, 1 << pin);
49 /* change the data first, then the direction. to avoid glitch */
50 setbits_le32(&regs->direction, 1 << pin);
51
Joachim Foerster03d67e12011-10-21 15:48:50 +020052 return 0;
53}
54
Thomas Chou88d5ecf2015-10-21 21:33:45 +080055static int altera_pio_get_value(struct udevice *dev, unsigned pin)
Joachim Foerster03d67e12011-10-21 15:48:50 +020056{
Simon Glass8a8d24b2020-12-03 16:55:23 -070057 struct altera_pio_plat *plat = dev_get_plat(dev);
Thomas Chou88d5ecf2015-10-21 21:33:45 +080058 struct altera_pio_regs *const regs = plat->regs;
Joachim Foerster03d67e12011-10-21 15:48:50 +020059
Julien BĂ©raud97b26272019-01-07 09:17:46 +000060 return !!(readl(&regs->data) & (1 << pin));
Joachim Foerster03d67e12011-10-21 15:48:50 +020061}
62
Thomas Chou88d5ecf2015-10-21 21:33:45 +080063
64static int altera_pio_set_value(struct udevice *dev, unsigned pin, int val)
Joachim Foerster03d67e12011-10-21 15:48:50 +020065{
Simon Glass8a8d24b2020-12-03 16:55:23 -070066 struct altera_pio_plat *plat = dev_get_plat(dev);
Thomas Chou88d5ecf2015-10-21 21:33:45 +080067 struct altera_pio_regs *const regs = plat->regs;
Joachim Foerster03d67e12011-10-21 15:48:50 +020068
Thomas Chou88d5ecf2015-10-21 21:33:45 +080069 if (val)
70 setbits_le32(&regs->data, 1 << pin);
Joachim Foerster03d67e12011-10-21 15:48:50 +020071 else
Thomas Chou88d5ecf2015-10-21 21:33:45 +080072 clrbits_le32(&regs->data, 1 << pin);
Joachim Foerster03d67e12011-10-21 15:48:50 +020073
Joachim Foerster03d67e12011-10-21 15:48:50 +020074 return 0;
75}
Thomas Chou88d5ecf2015-10-21 21:33:45 +080076
77static int altera_pio_probe(struct udevice *dev)
78{
79 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass8a8d24b2020-12-03 16:55:23 -070080 struct altera_pio_plat *plat = dev_get_plat(dev);
Thomas Chou88d5ecf2015-10-21 21:33:45 +080081
82 uc_priv->gpio_count = plat->gpio_count;
83 uc_priv->bank_name = plat->bank_name;
84
85 return 0;
86}
87
Simon Glassd1998a92020-12-03 16:55:21 -070088static int altera_pio_of_to_plat(struct udevice *dev)
Thomas Chou88d5ecf2015-10-21 21:33:45 +080089{
Simon Glass8a8d24b2020-12-03 16:55:23 -070090 struct altera_pio_plat *plat = dev_get_plat(dev);
Thomas Chou88d5ecf2015-10-21 21:33:45 +080091
Masahiro Yamada25484932020-07-17 14:36:48 +090092 plat->regs = map_physmem(dev_read_addr(dev),
Thomas Chou079bfc52015-11-14 11:26:51 +080093 sizeof(struct altera_pio_regs),
94 MAP_NOCACHE);
Simon Glasse160f7d2017-01-17 16:52:55 -070095 plat->gpio_count = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Thomas Chou88d5ecf2015-10-21 21:33:45 +080096 "altr,gpio-bank-width", 32);
Simon Glasse160f7d2017-01-17 16:52:55 -070097 plat->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
Thomas Chou88d5ecf2015-10-21 21:33:45 +080098 "gpio-bank-name", NULL);
99
100 return 0;
101}
102
103static const struct dm_gpio_ops altera_pio_ops = {
104 .direction_input = altera_pio_direction_input,
105 .direction_output = altera_pio_direction_output,
106 .get_value = altera_pio_get_value,
107 .set_value = altera_pio_set_value,
108};
109
110static const struct udevice_id altera_pio_ids[] = {
111 { .compatible = "altr,pio-1.0" },
112 { }
113};
114
115U_BOOT_DRIVER(altera_pio) = {
116 .name = "altera_pio",
117 .id = UCLASS_GPIO,
118 .of_match = altera_pio_ids,
119 .ops = &altera_pio_ops,
Simon Glassd1998a92020-12-03 16:55:21 -0700120 .of_to_plat = altera_pio_of_to_plat,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700121 .plat_auto = sizeof(struct altera_pio_plat),
Thomas Chou88d5ecf2015-10-21 21:33:45 +0800122 .probe = altera_pio_probe,
123};