summaryrefslogtreecommitdiff
path: root/drivers/gpio/starfive-gpio.c
blob: 9c715c1f9ca90209be9da5a3c426dad5235125c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2022 Starfive, Inc.
 * Author:	yanhong <yanhong.wang@starfivetech.com>
 *
 */

#include <common.h>
#include <dm.h>
#include <asm/arch/gpio.h>
#include <asm/io.h>
#include <errno.h>
#include <asm/gpio.h>
#include <linux/bitops.h>

static int starfive_gpio_probe(struct udevice *dev)
{
	struct starfive_gpio_platdata *plat = dev_get_plat(dev);
	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
	char name[18], *str;

	sprintf(name, "gpio@%4lx_", (uintptr_t)plat->base);
	str = strdup(name);
	if (!str)
		return -ENOMEM;
	uc_priv->bank_name = str;

	/*
	 * Use the gpio count mentioned in device tree,
	 * if not specified in dt, set NR_GPIOS as default
	 */
	uc_priv->gpio_count = dev_read_u32_default(dev, "ngpios", NR_GPIOS);

	return 0;
}

static int starfive_gpio_direction_input(struct udevice *dev, u32 offset)
{
	struct starfive_gpio_platdata *plat = dev_get_plat(dev);
	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);

	if (offset > uc_priv->gpio_count)
		return -EINVAL;

	/* Configure gpio direction as input */
	clrsetbits_le32(plat->base + GPIO_OFFSET(offset),
		GPIO_DOEN_MASK << GPIO_SHIFT(offset),
		HIGH << GPIO_SHIFT(offset));

	return 0;
}

static int starfive_gpio_direction_output(struct udevice *dev, u32 offset,
					int value)
{
	struct starfive_gpio_platdata *plat = dev_get_plat(dev);
	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);

	if (offset > uc_priv->gpio_count)
		return -EINVAL;

	/* Configure gpio direction as output */
	clrsetbits_le32(plat->base + GPIO_OFFSET(offset),
		GPIO_DOEN_MASK <<  GPIO_SHIFT(offset),
		LOW << GPIO_SHIFT(offset));

	/* Set the output value of the pin */
	clrsetbits_le32(plat->base + GPIO_DOUT + GPIO_OFFSET(offset),
		GPIO_DOUT_MASK << GPIO_SHIFT(offset),
		(value & GPIO_DOUT_MASK) << GPIO_SHIFT(offset));

	return 0;
}

static int starfive_gpio_get_value(struct udevice *dev, u32 offset)
{
	struct starfive_gpio_platdata *plat = dev_get_plat(dev);
	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
	int val;

	if (offset > uc_priv->gpio_count)
		return -EINVAL;

	/*Get dout value*/
	val = readl(plat->base + GPIO_DIN + GPIO_OFFSET(offset));
	val &= GPIO_DIN_MASK << GPIO_SHIFT(offset);

	return val ? HIGH : LOW;
}

static int starfive_gpio_set_value(struct udevice *dev, u32 offset, int value)
{
	struct starfive_gpio_platdata *plat = dev_get_plat(dev);
	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);

	if (offset > uc_priv->gpio_count)
		return -EINVAL;

	clrsetbits_le32(plat->base + GPIO_DOUT + GPIO_OFFSET(offset),
		GPIO_DOUT_MASK << GPIO_SHIFT(offset),
		(value & GPIO_DOUT_MASK) << GPIO_SHIFT(offset));

	return 0;
}

static int starfive_gpio_get_function(struct udevice *dev, unsigned int offset)
{
	struct starfive_gpio_platdata *plat = dev_get_plat(dev);
	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
	u32	dir, val;

	if (offset > uc_priv->gpio_count)
		return -1;

	/*Get doen value*/
	val = readl(plat->base + GPIO_OFFSET(offset));
	val &= (GPIO_DOEN_MASK << GPIO_SHIFT(offset));

	dir = (val > 1) ? GPIOF_UNUSED : (val ? GPIOF_INPUT : GPIOF_OUTPUT);

	return dir;
}

static int starfive_gpio_ofdata_to_platdata(struct udevice *dev)
{
	struct starfive_gpio_platdata *plat = dev_get_plat(dev);
	fdt_addr_t addr;

	addr = dev_read_addr(dev);
	if (addr == FDT_ADDR_T_NONE)
		return -EINVAL;

	plat->base = (void *)addr;

	writel(0, plat->base + GPIO_EN);
	writel(0, plat->base + GPIO_LOW_IE);
	writel(0, plat->base + GPIO_HIGH_IE);
	writel(1, plat->base + GPIO_EN);

	return 0;
}

static const struct udevice_id starfive_gpio_match[] = {
	{ .compatible = "starfive,jh7110-gpio" },
	{ }
};

static const struct dm_gpio_ops starfive_gpio_ops = {
	.direction_input        = starfive_gpio_direction_input,
	.direction_output       = starfive_gpio_direction_output,
	.get_value              = starfive_gpio_get_value,
	.set_value              = starfive_gpio_set_value,
	.get_function		= starfive_gpio_get_function,
};

U_BOOT_DRIVER(gpio_starfive) = {
	.name	= "gpio_starfive",
	.id	= UCLASS_GPIO,
	.of_match = starfive_gpio_match,
	.of_to_plat = starfive_gpio_ofdata_to_platdata,
	.plat_auto = sizeof(struct starfive_gpio_platdata),
	.ops	= &starfive_gpio_ops,
	.probe	= starfive_gpio_probe,
};