blob: 59e30979f0be117a47a60d84fbfcfe186302c8cd [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>
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
Thomas Choufb1a4bf2015-10-21 21:33:45 +080022struct altera_pio_platdata {
23 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{
Thomas Choufb1a4bf2015-10-21 21:33:45 +080030 struct altera_pio_platdata *plat = dev_get_platdata(dev);
31 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{
Thomas Choufb1a4bf2015-10-21 21:33:45 +080041 struct altera_pio_platdata *plat = dev_get_platdata(dev);
42 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{
Thomas Choufb1a4bf2015-10-21 21:33:45 +080056 struct altera_pio_platdata *plat = dev_get_platdata(dev);
57 struct altera_pio_regs *const regs = plat->regs;
Joachim Foersterc932eed2011-10-21 15:48:50 +020058
Thomas Choufb1a4bf2015-10-21 21:33:45 +080059 return readl(&regs->data) & (1 << pin);
Joachim Foersterc932eed2011-10-21 15:48:50 +020060}
61
62
Thomas Choufb1a4bf2015-10-21 21:33:45 +080063static int altera_pio_set_value(struct udevice *dev, unsigned pin, int val)
Joachim Foersterc932eed2011-10-21 15:48:50 +020064{
Thomas Choufb1a4bf2015-10-21 21:33:45 +080065 struct altera_pio_platdata *plat = dev_get_platdata(dev);
66 struct altera_pio_regs *const regs = plat->regs;
Joachim Foersterc932eed2011-10-21 15:48:50 +020067
Thomas Choufb1a4bf2015-10-21 21:33:45 +080068 if (val)
69 setbits_le32(&regs->data, 1 << pin);
70 else
71 clrbits_le32(&regs->data, 1 << pin);
Joachim Foersterc932eed2011-10-21 15:48:50 +020072
Joachim Foersterc932eed2011-10-21 15:48:50 +020073 return 0;
74}
75
Thomas Choufb1a4bf2015-10-21 21:33:45 +080076static int altera_pio_probe(struct udevice *dev)
Joachim Foersterc932eed2011-10-21 15:48:50 +020077{
Thomas Choufb1a4bf2015-10-21 21:33:45 +080078 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
79 struct altera_pio_platdata *plat = dev_get_platdata(dev);
Joachim Foersterc932eed2011-10-21 15:48:50 +020080
Thomas Choufb1a4bf2015-10-21 21:33:45 +080081 uc_priv->gpio_count = plat->gpio_count;
82 uc_priv->bank_name = plat->bank_name;
Joachim Foersterc932eed2011-10-21 15:48:50 +020083
Joachim Foersterc932eed2011-10-21 15:48:50 +020084 return 0;
85}
86
Thomas Choufb1a4bf2015-10-21 21:33:45 +080087static int altera_pio_ofdata_to_platdata(struct udevice *dev)
Joachim Foersterc932eed2011-10-21 15:48:50 +020088{
Thomas Choufb1a4bf2015-10-21 21:33:45 +080089 struct altera_pio_platdata *plat = dev_get_platdata(dev);
Joachim Foersterc932eed2011-10-21 15:48:50 +020090
Simon Glassba1dea42017-05-17 17:18:05 -060091 plat->regs = map_physmem(devfdt_get_addr(dev),
Thomas Chouee650e22015-11-14 11:26:51 +080092 sizeof(struct altera_pio_regs),
93 MAP_NOCACHE);
Simon Glassdd79d6e2017-01-17 16:52:55 -070094 plat->gpio_count = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Thomas Choufb1a4bf2015-10-21 21:33:45 +080095 "altr,gpio-bank-width", 32);
Simon Glassdd79d6e2017-01-17 16:52:55 -070096 plat->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
Thomas Choufb1a4bf2015-10-21 21:33:45 +080097 "gpio-bank-name", NULL);
Joachim Foersterc932eed2011-10-21 15:48:50 +020098
Joachim Foersterc932eed2011-10-21 15:48:50 +020099 return 0;
100}
101
Thomas Choufb1a4bf2015-10-21 21:33:45 +0800102static const struct dm_gpio_ops altera_pio_ops = {
103 .direction_input = altera_pio_direction_input,
104 .direction_output = altera_pio_direction_output,
105 .get_value = altera_pio_get_value,
106 .set_value = altera_pio_set_value,
107};
Joachim Foersterc932eed2011-10-21 15:48:50 +0200108
Thomas Choufb1a4bf2015-10-21 21:33:45 +0800109static const struct udevice_id altera_pio_ids[] = {
110 { .compatible = "altr,pio-1.0" },
111 { }
112};
Joachim Foersterc932eed2011-10-21 15:48:50 +0200113
Thomas Choufb1a4bf2015-10-21 21:33:45 +0800114U_BOOT_DRIVER(altera_pio) = {
115 .name = "altera_pio",
116 .id = UCLASS_GPIO,
117 .of_match = altera_pio_ids,
118 .ops = &altera_pio_ops,
119 .ofdata_to_platdata = altera_pio_ofdata_to_platdata,
120 .platdata_auto_alloc_size = sizeof(struct altera_pio_platdata),
121 .probe = altera_pio_probe,
122};