blob: 7ceb80e9510f3320956a1253603b0e6e9717a0c6 [file] [log] [blame]
Joachim Foersterc932eed2011-10-21 15:48:50 +02001/*
Thomas Choufb1a4bf2015-10-21 21:33:45 +08002 * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
Joachim Foersterc932eed2011-10-21 15:48:50 +02003 * Copyright (C) 2011 Missing Link Electronics
4 * Joachim Foerster <joachim@missinglinkelectronics.com>
5 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Joachim Foersterc932eed2011-10-21 15:48:50 +02007 */
8#include <common.h>
Thomas Choufb1a4bf2015-10-21 21:33:45 +08009#include <dm.h>
10#include <errno.h>
11#include <malloc.h>
12#include <fdtdec.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
Thomas Choufb1a4bf2015-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 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{
Thomas Choufb1a4bf2015-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 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{
Thomas Choufb1a4bf2015-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 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{
Thomas Choufb1a4bf2015-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 Foersterc932eed2011-10-21 15:48:50 +020059
Thomas Choufb1a4bf2015-10-21 21:33:45 +080060 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{
Thomas Choufb1a4bf2015-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 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);
80 struct altera_pio_platdata *plat = dev_get_platdata(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
Thomas Choufb1a4bf2015-10-21 21:33:45 +080088static int altera_pio_ofdata_to_platdata(struct udevice *dev)
Joachim Foersterc932eed2011-10-21 15:48:50 +020089{
Thomas Choufb1a4bf2015-10-21 21:33:45 +080090 struct altera_pio_platdata *plat = dev_get_platdata(dev);
Joachim Foersterc932eed2011-10-21 15:48:50 +020091
Thomas Choufb1a4bf2015-10-21 21:33:45 +080092 plat->regs = ioremap(dev_get_addr(dev),
93 sizeof(struct altera_pio_regs));
94 plat->gpio_count = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
95 "altr,gpio-bank-width", 32);
96 plat->bank_name = fdt_getprop(gd->fdt_blob, dev->of_offset,
97 "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};