summaryrefslogtreecommitdiff
path: root/drivers/gpio/octeon_gpio.c
blob: e6a8e1a5212c569f341cc9005328bef9ba342fdf (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// SPDX-License-Identifier:    GPL-2.0
/*
 * Copyright (C) 2018 Marvell International Ltd.
 *
 * (C) Copyright 2011
 * eInfochips Ltd. <www.einfochips.com>
 * Written-by: Ajay Bhargav <ajay.bhargav@einfochips.com>
 *
 * (C) Copyright 2010
 * Marvell Semiconductor <www.marvell.com>
 */

#include <dm.h>
#include <pci.h>
#include <pci_ids.h>
#include <asm/gpio.h>
#include <asm/io.h>
#include <linux/bitfield.h>
#include <linux/compat.h>
#include <dt-bindings/gpio/gpio.h>

/* Returns the bit value to write or read based on the offset */
#define GPIO_BIT(x)		BIT_ULL((x) & 0x3f)

#define GPIO_RX_DAT		0x00
#define GPIO_TX_SET		0x08
#define GPIO_TX_CLR		0x10
#define GPIO_CONST		0x90	/* OcteonTX only */

/* Offset to register-set for 2nd GPIOs (> 63), OcteonTX only */
#define GPIO1_OFFSET		0x1400

/* GPIO_CONST register bits */
#define GPIO_CONST_GPIOS_MASK	GENMASK_ULL(7, 0)

/* GPIO_BIT_CFG register bits */
#define GPIO_BIT_CFG_TX_OE	BIT_ULL(0)
#define GPIO_BIT_CFG_PIN_XOR	BIT_ULL(1)
#define GPIO_BIT_CFG_INT_EN	BIT_ULL(2)
#define GPIO_BIT_CFG_PIN_SEL_MASK GENMASK_ULL(26, 16)

enum {
	PROBE_PCI = 0,		/* PCI based probing */
	PROBE_DT,		/* DT based probing */
};

struct octeon_gpio_data {
	int probe;
	u32 reg_offs;
	u32 gpio_bit_cfg_offs;
};

struct octeon_gpio {
	void __iomem *base;
	const struct octeon_gpio_data *data;
};

/* Returns the offset to the output register based on the offset and value */
static u32 gpio_tx_reg(int offset, int value)
{
	u32 ret;

	ret = value ? GPIO_TX_SET : GPIO_TX_CLR;
	if (offset > 63)
		ret += GPIO1_OFFSET;

	return ret;
}

/* Returns the offset to the input data register based on the offset */
static u32 gpio_rx_dat_reg(int offset)
{
	u32 ret;

	ret = GPIO_RX_DAT;
	if (offset > 63)
		ret += GPIO1_OFFSET;

	return ret;
}

static int octeon_gpio_dir_input(struct udevice *dev, unsigned int offset)
{
	struct octeon_gpio *gpio = dev_get_priv(dev);

	debug("%s(%s, %u)\n", __func__, dev->name, offset);
	clrbits_64(gpio->base + gpio->data->gpio_bit_cfg_offs + 8 * offset,
		   GPIO_BIT_CFG_TX_OE | GPIO_BIT_CFG_PIN_XOR |
		   GPIO_BIT_CFG_INT_EN | GPIO_BIT_CFG_PIN_SEL_MASK);

	return 0;
}

static int octeon_gpio_dir_output(struct udevice *dev, unsigned int offset,
				  int value)
{
	struct octeon_gpio *gpio = dev_get_priv(dev);

	debug("%s(%s, %u, %d)\n", __func__, dev->name, offset, value);
	writeq(GPIO_BIT(offset), gpio->base + gpio->data->reg_offs +
	       gpio_tx_reg(offset, value));

	clrsetbits_64(gpio->base + gpio->data->gpio_bit_cfg_offs + 8 * offset,
		      GPIO_BIT_CFG_PIN_SEL_MASK | GPIO_BIT_CFG_INT_EN,
		      GPIO_BIT_CFG_TX_OE);

	return 0;
}

static int octeon_gpio_get_value(struct udevice *dev, unsigned int offset)
{
	struct octeon_gpio *gpio = dev_get_priv(dev);
	u64 reg = readq(gpio->base + gpio->data->reg_offs +
			gpio_rx_dat_reg(offset));

	debug("%s(%s, %u): value: %d\n", __func__, dev->name, offset,
	      !!(reg & GPIO_BIT(offset)));

	return !!(reg & GPIO_BIT(offset));
}

static int octeon_gpio_set_value(struct udevice *dev,
				 unsigned int offset, int value)
{
	struct octeon_gpio *gpio = dev_get_priv(dev);

	debug("%s(%s, %u, %d)\n", __func__, dev->name, offset, value);
	writeq(GPIO_BIT(offset), gpio->base + gpio->data->reg_offs +
	       gpio_tx_reg(offset, value));

	return 0;
}

static int octeon_gpio_get_function(struct udevice *dev, unsigned int offset)
{
	struct octeon_gpio *gpio = dev_get_priv(dev);
	u64 val = readq(gpio->base + gpio->data->gpio_bit_cfg_offs +
			8 * offset);
	int pin_sel;

	debug("%s(%s, %u): GPIO_BIT_CFG: 0x%llx\n", __func__, dev->name,
	      offset, val);
	pin_sel = FIELD_GET(GPIO_BIT_CFG_PIN_SEL_MASK, val);
	if (pin_sel)
		return GPIOF_FUNC;
	else if (val & GPIO_BIT_CFG_TX_OE)
		return GPIOF_OUTPUT;
	else
		return GPIOF_INPUT;
}

static int octeon_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
			     struct ofnode_phandle_args *args)
{
	if (args->args_count < 1)
		return -EINVAL;

	desc->offset = args->args[0];
	desc->flags = 0;
	if (args->args_count > 1) {
		if (args->args[1] & GPIO_ACTIVE_LOW)
			desc->flags |= GPIOD_ACTIVE_LOW;
		/* In the future add tri-state flag support */
	}
	return 0;
}

static const struct dm_gpio_ops octeon_gpio_ops = {
	.direction_input	= octeon_gpio_dir_input,
	.direction_output	= octeon_gpio_dir_output,
	.get_value		= octeon_gpio_get_value,
	.set_value		= octeon_gpio_set_value,
	.get_function		= octeon_gpio_get_function,
	.xlate			= octeon_gpio_xlate,
};

static int octeon_gpio_probe(struct udevice *dev)
{
	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
	struct octeon_gpio *priv = dev_get_priv(dev);
	char *end;

	priv->data = (const struct octeon_gpio_data *)dev_get_driver_data(dev);

	if (priv->data->probe == PROBE_PCI) {
		priv->base = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0, 0, 0,
					    PCI_REGION_MEM);
		uc_priv->gpio_count = readq(priv->base +
					    priv->data->reg_offs + GPIO_CONST) &
			GPIO_CONST_GPIOS_MASK;
	} else {
		priv->base = dev_remap_addr(dev);
		uc_priv->gpio_count = ofnode_read_u32_default(dev_ofnode(dev),
							      "nr-gpios", 32);
	}

	if (!priv->base) {
		debug("%s(%s): Could not get base address\n",
		      __func__, dev->name);
		return -ENODEV;
	}

	uc_priv->bank_name  = strdup(dev->name);
	end = strchr(uc_priv->bank_name, '@');
	end[0] = 'A' + dev_seq(dev);
	end[1] = '\0';

	debug("%s(%s): base address: %p, pin count: %d\n",
	      __func__, dev->name, priv->base, uc_priv->gpio_count);

	return 0;
}

static const struct octeon_gpio_data gpio_octeon_data = {
	.probe = PROBE_DT,
	.reg_offs = 0x80,
	.gpio_bit_cfg_offs = 0x100,
};

static const struct octeon_gpio_data gpio_octeontx_data = {
	.probe = PROBE_PCI,
	.reg_offs = 0x00,
	.gpio_bit_cfg_offs = 0x400,
};

static const struct udevice_id octeon_gpio_ids[] = {
	{ .compatible = "cavium,thunder-8890-gpio",
	  .data = (ulong)&gpio_octeontx_data },
	{ .compatible = "cavium,octeon-7890-gpio",
	  .data = (ulong)&gpio_octeon_data },
	{ }
};

U_BOOT_DRIVER(octeon_gpio) = {
	.name	= "octeon_gpio",
	.id	= UCLASS_GPIO,
	.of_match = of_match_ptr(octeon_gpio_ids),
	.probe = octeon_gpio_probe,
	.priv_auto	= sizeof(struct octeon_gpio),
	.ops	= &octeon_gpio_ops,
	.flags	= DM_FLAG_PRE_RELOC,
};