blob: edc5a8093b0c46e0ad8fab58c905dc410baf6c2f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Joachim Foersterc932eed2011-10-21 15:48:50 +02002/*
Thomas Choufb1a4bf2015-10-21 21:33:45 +08003 * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
Joachim Foersterc932eed2011-10-21 15:48:50 +02004 * Copyright (C) 2011 Missing Link Electronics
5 * Joachim Foerster <joachim@missinglinkelectronics.com>
Joachim Foersterc932eed2011-10-21 15:48:50 +02006 */
7#include <common.h>
Thomas Choufb1a4bf2015-10-21 21:33:45 +08008#include <dm.h>
9#include <errno.h>
10#include <malloc.h>
11#include <fdtdec.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Joachim Foersterc932eed2011-10-21 15:48:50 +020013#include <asm/io.h>
14#include <asm/gpio.h>
15
Thomas Choufb1a4bf2015-10-21 21:33:45 +080016DECLARE_GLOBAL_DATA_PTR;
Joachim Foersterc932eed2011-10-21 15:48:50 +020017
Thomas Choufb1a4bf2015-10-21 21:33:45 +080018struct altera_pio_regs {
19 u32 data; /* Data register */
20 u32 direction; /* Direction register */
21};
Joachim Foersterc932eed2011-10-21 15:48:50 +020022
Simon Glassb75b15b2020-12-03 16:55:23 -070023struct altera_pio_plat {
Thomas Choufb1a4bf2015-10-21 21:33:45 +080024 struct altera_pio_regs *regs;
25 int gpio_count;
26 const char *bank_name;
27};
Joachim Foersterc932eed2011-10-21 15:48:50 +020028
Thomas Choufb1a4bf2015-10-21 21:33:45 +080029static int altera_pio_direction_input(struct udevice *dev, unsigned pin)
Joachim Foersterc932eed2011-10-21 15:48:50 +020030{
Simon Glassb75b15b2020-12-03 16:55:23 -070031 struct altera_pio_plat *plat = dev_get_plat(dev);
Thomas Choufb1a4bf2015-10-21 21:33:45 +080032 struct altera_pio_regs *const regs = plat->regs;
Joachim Foersterc932eed2011-10-21 15:48:50 +020033
Thomas Choufb1a4bf2015-10-21 21:33:45 +080034 clrbits_le32(&regs->direction, 1 << pin);
Joachim Foersterc932eed2011-10-21 15:48:50 +020035
Thomas Choufb1a4bf2015-10-21 21:33:45 +080036 return 0;
Joachim Foersterc932eed2011-10-21 15:48:50 +020037}
38
Thomas Choufb1a4bf2015-10-21 21:33:45 +080039static int altera_pio_direction_output(struct udevice *dev, unsigned pin,
40 int val)
Joachim Foersterc932eed2011-10-21 15:48:50 +020041{
Simon Glassb75b15b2020-12-03 16:55:23 -070042 struct altera_pio_plat *plat = dev_get_plat(dev);
Thomas Choufb1a4bf2015-10-21 21:33:45 +080043 struct altera_pio_regs *const regs = plat->regs;
Joachim Foersterc932eed2011-10-21 15:48:50 +020044
Thomas Choufb1a4bf2015-10-21 21:33:45 +080045 if (val)
46 setbits_le32(&regs->data, 1 << pin);
47 else
48 clrbits_le32(&regs->data, 1 << pin);
49 /* change the data first, then the direction. to avoid glitch */
50 setbits_le32(&regs->direction, 1 << pin);
Joachim Foersterc932eed2011-10-21 15:48:50 +020051
Thomas Choufb1a4bf2015-10-21 21:33:45 +080052 return 0;
Joachim Foersterc932eed2011-10-21 15:48:50 +020053}
54
Thomas Choufb1a4bf2015-10-21 21:33:45 +080055static int altera_pio_get_value(struct udevice *dev, unsigned pin)
Joachim Foersterc932eed2011-10-21 15:48:50 +020056{
Simon Glassb75b15b2020-12-03 16:55:23 -070057 struct altera_pio_plat *plat = dev_get_plat(dev);
Thomas Choufb1a4bf2015-10-21 21:33:45 +080058 struct altera_pio_regs *const regs = plat->regs;
Joachim Foersterc932eed2011-10-21 15:48:50 +020059
Julien BĂ©raudfd8b8022019-01-07 09:17:46 +000060 return !!(readl(&regs->data) & (1 << pin));
Joachim Foersterc932eed2011-10-21 15:48:50 +020061}
62
63
Thomas Choufb1a4bf2015-10-21 21:33:45 +080064static int altera_pio_set_value(struct udevice *dev, unsigned pin, int val)
Joachim Foersterc932eed2011-10-21 15:48:50 +020065{
Simon Glassb75b15b2020-12-03 16:55:23 -070066 struct altera_pio_plat *plat = dev_get_plat(dev);
Thomas Choufb1a4bf2015-10-21 21:33:45 +080067 struct altera_pio_regs *const regs = plat->regs;
Joachim Foersterc932eed2011-10-21 15:48:50 +020068
Thomas Choufb1a4bf2015-10-21 21:33:45 +080069 if (val)
70 setbits_le32(&regs->data, 1 << pin);
71 else
72 clrbits_le32(&regs->data, 1 << pin);
Joachim Foersterc932eed2011-10-21 15:48:50 +020073
Joachim Foersterc932eed2011-10-21 15:48:50 +020074 return 0;
75}
76
Thomas Choufb1a4bf2015-10-21 21:33:45 +080077static int altera_pio_probe(struct udevice *dev)
Joachim Foersterc932eed2011-10-21 15:48:50 +020078{
Thomas Choufb1a4bf2015-10-21 21:33:45 +080079 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb75b15b2020-12-03 16:55:23 -070080 struct altera_pio_plat *plat = dev_get_plat(dev);
Joachim Foersterc932eed2011-10-21 15:48:50 +020081
Thomas Choufb1a4bf2015-10-21 21:33:45 +080082 uc_priv->gpio_count = plat->gpio_count;
83 uc_priv->bank_name = plat->bank_name;
Joachim Foersterc932eed2011-10-21 15:48:50 +020084
Joachim Foersterc932eed2011-10-21 15:48:50 +020085 return 0;
86}
87
Simon Glassaad29ae2020-12-03 16:55:21 -070088static int altera_pio_of_to_plat(struct udevice *dev)
Joachim Foersterc932eed2011-10-21 15:48:50 +020089{
Simon Glassb75b15b2020-12-03 16:55:23 -070090 struct altera_pio_plat *plat = dev_get_plat(dev);
Joachim Foersterc932eed2011-10-21 15:48:50 +020091
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +090092 plat->regs = map_physmem(dev_read_addr(dev),
Thomas Chouee650e22015-11-14 11:26:51 +080093 sizeof(struct altera_pio_regs),
94 MAP_NOCACHE);
Simon Glassdd79d6e2017-01-17 16:52:55 -070095 plat->gpio_count = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Thomas Choufb1a4bf2015-10-21 21:33:45 +080096 "altr,gpio-bank-width", 32);
Simon Glassdd79d6e2017-01-17 16:52:55 -070097 plat->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
Thomas Choufb1a4bf2015-10-21 21:33:45 +080098 "gpio-bank-name", NULL);
Joachim Foersterc932eed2011-10-21 15:48:50 +020099
Joachim Foersterc932eed2011-10-21 15:48:50 +0200100 return 0;
101}
102
Thomas Choufb1a4bf2015-10-21 21:33:45 +0800103static 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};
Joachim Foersterc932eed2011-10-21 15:48:50 +0200109
Thomas Choufb1a4bf2015-10-21 21:33:45 +0800110static const struct udevice_id altera_pio_ids[] = {
111 { .compatible = "altr,pio-1.0" },
112 { }
113};
Joachim Foersterc932eed2011-10-21 15:48:50 +0200114
Thomas Choufb1a4bf2015-10-21 21:33:45 +0800115U_BOOT_DRIVER(altera_pio) = {
116 .name = "altera_pio",
117 .id = UCLASS_GPIO,
118 .of_match = altera_pio_ids,
119 .ops = &altera_pio_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700120 .of_to_plat = altera_pio_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700121 .plat_auto = sizeof(struct altera_pio_plat),
Thomas Choufb1a4bf2015-10-21 21:33:45 +0800122 .probe = altera_pio_probe,
123};