blob: 469c50a6016d33d13197730b8108f0787bb6a100 [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 */
Thomas Choufb1a4bf2015-10-21 21:33:45 +08007#include <dm.h>
8#include <errno.h>
9#include <malloc.h>
10#include <fdtdec.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Joachim Foersterc932eed2011-10-21 15:48:50 +020012#include <asm/io.h>
13#include <asm/gpio.h>
14
Thomas Choufb1a4bf2015-10-21 21:33:45 +080015DECLARE_GLOBAL_DATA_PTR;
Joachim Foersterc932eed2011-10-21 15:48:50 +020016
Thomas Choufb1a4bf2015-10-21 21:33:45 +080017struct altera_pio_regs {
18 u32 data; /* Data register */
19 u32 direction; /* Direction register */
20};
Joachim Foersterc932eed2011-10-21 15:48:50 +020021
Simon Glassb75b15b2020-12-03 16:55:23 -070022struct altera_pio_plat {
Thomas Choufb1a4bf2015-10-21 21:33:45 +080023 struct altera_pio_regs *regs;
24 int gpio_count;
25 const char *bank_name;
26};
Joachim Foersterc932eed2011-10-21 15:48:50 +020027
Thomas Choufb1a4bf2015-10-21 21:33:45 +080028static int altera_pio_direction_input(struct udevice *dev, unsigned pin)
Joachim Foersterc932eed2011-10-21 15:48:50 +020029{
Simon Glassb75b15b2020-12-03 16:55:23 -070030 struct altera_pio_plat *plat = dev_get_plat(dev);
Thomas Choufb1a4bf2015-10-21 21:33:45 +080031 struct altera_pio_regs *const regs = plat->regs;
Joachim Foersterc932eed2011-10-21 15:48:50 +020032
Thomas Choufb1a4bf2015-10-21 21:33:45 +080033 clrbits_le32(&regs->direction, 1 << pin);
Joachim Foersterc932eed2011-10-21 15:48:50 +020034
Thomas Choufb1a4bf2015-10-21 21:33:45 +080035 return 0;
Joachim Foersterc932eed2011-10-21 15:48:50 +020036}
37
Thomas Choufb1a4bf2015-10-21 21:33:45 +080038static int altera_pio_direction_output(struct udevice *dev, unsigned pin,
39 int val)
Joachim Foersterc932eed2011-10-21 15:48:50 +020040{
Simon Glassb75b15b2020-12-03 16:55:23 -070041 struct altera_pio_plat *plat = dev_get_plat(dev);
Thomas Choufb1a4bf2015-10-21 21:33:45 +080042 struct altera_pio_regs *const regs = plat->regs;
Joachim Foersterc932eed2011-10-21 15:48:50 +020043
Thomas Choufb1a4bf2015-10-21 21:33:45 +080044 if (val)
45 setbits_le32(&regs->data, 1 << pin);
46 else
47 clrbits_le32(&regs->data, 1 << pin);
48 /* change the data first, then the direction. to avoid glitch */
49 setbits_le32(&regs->direction, 1 << pin);
Joachim Foersterc932eed2011-10-21 15:48:50 +020050
Thomas Choufb1a4bf2015-10-21 21:33:45 +080051 return 0;
Joachim Foersterc932eed2011-10-21 15:48:50 +020052}
53
Thomas Choufb1a4bf2015-10-21 21:33:45 +080054static int altera_pio_get_value(struct udevice *dev, unsigned pin)
Joachim Foersterc932eed2011-10-21 15:48:50 +020055{
Simon Glassb75b15b2020-12-03 16:55:23 -070056 struct altera_pio_plat *plat = dev_get_plat(dev);
Thomas Choufb1a4bf2015-10-21 21:33:45 +080057 struct altera_pio_regs *const regs = plat->regs;
Joachim Foersterc932eed2011-10-21 15:48:50 +020058
Julien BĂ©raudfd8b8022019-01-07 09:17:46 +000059 return !!(readl(&regs->data) & (1 << pin));
Joachim Foersterc932eed2011-10-21 15:48:50 +020060}
61
Thomas Choufb1a4bf2015-10-21 21:33:45 +080062static int altera_pio_set_value(struct udevice *dev, unsigned pin, int val)
Joachim Foersterc932eed2011-10-21 15:48:50 +020063{
Simon Glassb75b15b2020-12-03 16:55:23 -070064 struct altera_pio_plat *plat = dev_get_plat(dev);
Thomas Choufb1a4bf2015-10-21 21:33:45 +080065 struct altera_pio_regs *const regs = plat->regs;
Joachim Foersterc932eed2011-10-21 15:48:50 +020066
Thomas Choufb1a4bf2015-10-21 21:33:45 +080067 if (val)
68 setbits_le32(&regs->data, 1 << pin);
69 else
70 clrbits_le32(&regs->data, 1 << pin);
Joachim Foersterc932eed2011-10-21 15:48:50 +020071
Joachim Foersterc932eed2011-10-21 15:48:50 +020072 return 0;
73}
74
Thomas Choufb1a4bf2015-10-21 21:33:45 +080075static int altera_pio_probe(struct udevice *dev)
Joachim Foersterc932eed2011-10-21 15:48:50 +020076{
Thomas Choufb1a4bf2015-10-21 21:33:45 +080077 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb75b15b2020-12-03 16:55:23 -070078 struct altera_pio_plat *plat = dev_get_plat(dev);
Joachim Foersterc932eed2011-10-21 15:48:50 +020079
Thomas Choufb1a4bf2015-10-21 21:33:45 +080080 uc_priv->gpio_count = plat->gpio_count;
81 uc_priv->bank_name = plat->bank_name;
Joachim Foersterc932eed2011-10-21 15:48:50 +020082
Joachim Foersterc932eed2011-10-21 15:48:50 +020083 return 0;
84}
85
Simon Glassaad29ae2020-12-03 16:55:21 -070086static int altera_pio_of_to_plat(struct udevice *dev)
Joachim Foersterc932eed2011-10-21 15:48:50 +020087{
Simon Glassb75b15b2020-12-03 16:55:23 -070088 struct altera_pio_plat *plat = dev_get_plat(dev);
Joachim Foersterc932eed2011-10-21 15:48:50 +020089
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +090090 plat->regs = map_physmem(dev_read_addr(dev),
Thomas Chouee650e22015-11-14 11:26:51 +080091 sizeof(struct altera_pio_regs),
92 MAP_NOCACHE);
Simon Glassdd79d6e2017-01-17 16:52:55 -070093 plat->gpio_count = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Thomas Choufb1a4bf2015-10-21 21:33:45 +080094 "altr,gpio-bank-width", 32);
Simon Glassdd79d6e2017-01-17 16:52:55 -070095 plat->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
Thomas Choufb1a4bf2015-10-21 21:33:45 +080096 "gpio-bank-name", NULL);
Joachim Foersterc932eed2011-10-21 15:48:50 +020097
Joachim Foersterc932eed2011-10-21 15:48:50 +020098 return 0;
99}
100
Thomas Choufb1a4bf2015-10-21 21:33:45 +0800101static const struct dm_gpio_ops altera_pio_ops = {
102 .direction_input = altera_pio_direction_input,
103 .direction_output = altera_pio_direction_output,
104 .get_value = altera_pio_get_value,
105 .set_value = altera_pio_set_value,
106};
Joachim Foersterc932eed2011-10-21 15:48:50 +0200107
Thomas Choufb1a4bf2015-10-21 21:33:45 +0800108static const struct udevice_id altera_pio_ids[] = {
109 { .compatible = "altr,pio-1.0" },
110 { }
111};
Joachim Foersterc932eed2011-10-21 15:48:50 +0200112
Thomas Choufb1a4bf2015-10-21 21:33:45 +0800113U_BOOT_DRIVER(altera_pio) = {
114 .name = "altera_pio",
115 .id = UCLASS_GPIO,
116 .of_match = altera_pio_ids,
117 .ops = &altera_pio_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700118 .of_to_plat = altera_pio_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700119 .plat_auto = sizeof(struct altera_pio_plat),
Thomas Choufb1a4bf2015-10-21 21:33:45 +0800120 .probe = altera_pio_probe,
121};