blob: 6f42bf8a2269018f17004943fd4e328efc948f0e [file] [log] [blame]
Joachim Foerster03d67e12011-10-21 15:48:50 +02001/*
Thomas Chou88d5ecf2015-10-21 21:33:45 +08002 * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
Joachim Foerster03d67e12011-10-21 15:48:50 +02003 * Copyright (C) 2011 Missing Link Electronics
4 * Joachim Foerster <joachim@missinglinkelectronics.com>
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Joachim Foerster03d67e12011-10-21 15:48:50 +02007 */
8#include <common.h>
Thomas Chou88d5ecf2015-10-21 21:33:45 +08009#include <dm.h>
10#include <errno.h>
11#include <malloc.h>
12#include <fdtdec.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
Thomas Chou88d5ecf2015-10-21 21:33:45 +080023struct altera_pio_platdata {
24 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{
Thomas Chou88d5ecf2015-10-21 21:33:45 +080031 struct altera_pio_platdata *plat = dev_get_platdata(dev);
32 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{
Thomas Chou88d5ecf2015-10-21 21:33:45 +080042 struct altera_pio_platdata *plat = dev_get_platdata(dev);
43 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{
Thomas Chou88d5ecf2015-10-21 21:33:45 +080057 struct altera_pio_platdata *plat = dev_get_platdata(dev);
58 struct altera_pio_regs *const regs = plat->regs;
Joachim Foerster03d67e12011-10-21 15:48:50 +020059
Thomas Chou88d5ecf2015-10-21 21:33:45 +080060 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{
Thomas Chou88d5ecf2015-10-21 21:33:45 +080066 struct altera_pio_platdata *plat = dev_get_platdata(dev);
67 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);
80 struct altera_pio_platdata *plat = dev_get_platdata(dev);
81
82 uc_priv->gpio_count = plat->gpio_count;
83 uc_priv->bank_name = plat->bank_name;
84
85 return 0;
86}
87
88static int altera_pio_ofdata_to_platdata(struct udevice *dev)
89{
90 struct altera_pio_platdata *plat = dev_get_platdata(dev);
91
Thomas Chou079bfc52015-11-14 11:26:51 +080092 plat->regs = map_physmem(dev_get_addr(dev),
93 sizeof(struct altera_pio_regs),
94 MAP_NOCACHE);
Thomas Chou88d5ecf2015-10-21 21:33:45 +080095 plat->gpio_count = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
96 "altr,gpio-bank-width", 32);
97 plat->bank_name = fdt_getprop(gd->fdt_blob, dev->of_offset,
98 "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,
120 .ofdata_to_platdata = altera_pio_ofdata_to_platdata,
121 .platdata_auto_alloc_size = sizeof(struct altera_pio_platdata),
122 .probe = altera_pio_probe,
123};