blob: d17245ac232c585bda5a7166b638417127f9dbed [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
Simon Glassba1dea42017-05-17 17:18:05 -060092 plat->regs = map_physmem(devfdt_get_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,
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};