1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Marvell International Ltd.
6 * eInfochips Ltd. <www.einfochips.com>
7 * Written-by: Ajay Bhargav <ajay.bhargav@einfochips.com>
10 * Marvell Semiconductor <www.marvell.com>
18 #include <linux/bitfield.h>
19 #include <linux/compat.h>
20 #include <dt-bindings/gpio/gpio.h>
22 /* Returns the bit value to write or read based on the offset */
23 #define GPIO_BIT(x) BIT_ULL((x) & 0x3f)
25 #define GPIO_RX_DAT 0x00
26 #define GPIO_TX_SET 0x08
27 #define GPIO_TX_CLR 0x10
28 #define GPIO_CONST 0x90 /* OcteonTX only */
30 /* Offset to register-set for 2nd GPIOs (> 63), OcteonTX only */
31 #define GPIO1_OFFSET 0x1400
33 /* GPIO_CONST register bits */
34 #define GPIO_CONST_GPIOS_MASK GENMASK_ULL(7, 0)
36 /* GPIO_BIT_CFG register bits */
37 #define GPIO_BIT_CFG_TX_OE BIT_ULL(0)
38 #define GPIO_BIT_CFG_PIN_XOR BIT_ULL(1)
39 #define GPIO_BIT_CFG_INT_EN BIT_ULL(2)
40 #define GPIO_BIT_CFG_PIN_SEL_MASK GENMASK_ULL(26, 16)
43 PROBE_PCI = 0, /* PCI based probing */
44 PROBE_DT, /* DT based probing */
47 struct octeon_gpio_data {
50 u32 gpio_bit_cfg_offs;
55 const struct octeon_gpio_data *data;
58 /* Returns the offset to the output register based on the offset and value */
59 static u32 gpio_tx_reg(int offset, int value)
63 ret = value ? GPIO_TX_SET : GPIO_TX_CLR;
70 /* Returns the offset to the input data register based on the offset */
71 static u32 gpio_rx_dat_reg(int offset)
82 static int octeon_gpio_dir_input(struct udevice *dev, unsigned int offset)
84 struct octeon_gpio *gpio = dev_get_priv(dev);
86 debug("%s(%s, %u)\n", __func__, dev->name, offset);
87 clrbits_64(gpio->base + gpio->data->gpio_bit_cfg_offs + 8 * offset,
88 GPIO_BIT_CFG_TX_OE | GPIO_BIT_CFG_PIN_XOR |
89 GPIO_BIT_CFG_INT_EN | GPIO_BIT_CFG_PIN_SEL_MASK);
94 static int octeon_gpio_dir_output(struct udevice *dev, unsigned int offset,
97 struct octeon_gpio *gpio = dev_get_priv(dev);
99 debug("%s(%s, %u, %d)\n", __func__, dev->name, offset, value);
100 writeq(GPIO_BIT(offset), gpio->base + gpio->data->reg_offs +
101 gpio_tx_reg(offset, value));
103 clrsetbits_64(gpio->base + gpio->data->gpio_bit_cfg_offs + 8 * offset,
104 GPIO_BIT_CFG_PIN_SEL_MASK | GPIO_BIT_CFG_INT_EN,
110 static int octeon_gpio_get_value(struct udevice *dev, unsigned int offset)
112 struct octeon_gpio *gpio = dev_get_priv(dev);
113 u64 reg = readq(gpio->base + gpio->data->reg_offs +
114 gpio_rx_dat_reg(offset));
116 debug("%s(%s, %u): value: %d\n", __func__, dev->name, offset,
117 !!(reg & GPIO_BIT(offset)));
119 return !!(reg & GPIO_BIT(offset));
122 static int octeon_gpio_set_value(struct udevice *dev,
123 unsigned int offset, int value)
125 struct octeon_gpio *gpio = dev_get_priv(dev);
127 debug("%s(%s, %u, %d)\n", __func__, dev->name, offset, value);
128 writeq(GPIO_BIT(offset), gpio->base + gpio->data->reg_offs +
129 gpio_tx_reg(offset, value));
134 static int octeon_gpio_get_function(struct udevice *dev, unsigned int offset)
136 struct octeon_gpio *gpio = dev_get_priv(dev);
137 u64 val = readq(gpio->base + gpio->data->gpio_bit_cfg_offs +
141 debug("%s(%s, %u): GPIO_BIT_CFG: 0x%llx\n", __func__, dev->name,
143 pin_sel = FIELD_GET(GPIO_BIT_CFG_PIN_SEL_MASK, val);
146 else if (val & GPIO_BIT_CFG_TX_OE)
152 static int octeon_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
153 struct ofnode_phandle_args *args)
155 if (args->args_count < 1)
158 desc->offset = args->args[0];
160 if (args->args_count > 1) {
161 if (args->args[1] & GPIO_ACTIVE_LOW)
162 desc->flags |= GPIOD_ACTIVE_LOW;
163 /* In the future add tri-state flag support */
168 static const struct dm_gpio_ops octeon_gpio_ops = {
169 .direction_input = octeon_gpio_dir_input,
170 .direction_output = octeon_gpio_dir_output,
171 .get_value = octeon_gpio_get_value,
172 .set_value = octeon_gpio_set_value,
173 .get_function = octeon_gpio_get_function,
174 .xlate = octeon_gpio_xlate,
177 static int octeon_gpio_probe(struct udevice *dev)
179 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
180 struct octeon_gpio *priv = dev_get_priv(dev);
183 priv->data = (const struct octeon_gpio_data *)dev_get_driver_data(dev);
185 if (priv->data->probe == PROBE_PCI) {
186 priv->base = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0, 0, 0, PCI_REGION_TYPE,
188 uc_priv->gpio_count = readq(priv->base +
189 priv->data->reg_offs + GPIO_CONST) &
190 GPIO_CONST_GPIOS_MASK;
192 priv->base = dev_remap_addr(dev);
193 uc_priv->gpio_count = ofnode_read_u32_default(dev_ofnode(dev),
198 debug("%s(%s): Could not get base address\n",
199 __func__, dev->name);
203 uc_priv->bank_name = strdup(dev->name);
204 end = strchr(uc_priv->bank_name, '@');
205 end[0] = 'A' + dev_seq(dev);
208 debug("%s(%s): base address: %p, pin count: %d\n",
209 __func__, dev->name, priv->base, uc_priv->gpio_count);
214 static const struct octeon_gpio_data gpio_octeon_data = {
217 .gpio_bit_cfg_offs = 0x100,
220 static const struct octeon_gpio_data gpio_octeontx_data = {
223 .gpio_bit_cfg_offs = 0x400,
226 static const struct udevice_id octeon_gpio_ids[] = {
227 { .compatible = "cavium,thunder-8890-gpio",
228 .data = (ulong)&gpio_octeontx_data },
229 { .compatible = "cavium,octeon-7890-gpio",
230 .data = (ulong)&gpio_octeon_data },
234 U_BOOT_DRIVER(octeon_gpio) = {
235 .name = "octeon_gpio",
237 .of_match = of_match_ptr(octeon_gpio_ids),
238 .probe = octeon_gpio_probe,
239 .priv_auto = sizeof(struct octeon_gpio),
240 .ops = &octeon_gpio_ops,
241 .flags = DM_FLAG_PRE_RELOC,