blob: ab46a94dbc15a7cd1109f21122435ebc57b1e423 [file] [log] [blame]
Simon Glass83490512019-12-06 21:42:54 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2019 Google LLC
4 */
5
Simon Glassac379392021-02-04 21:22:06 -07006#define LOG_CATEGORY UCLASS_GPIO
7
Simon Glass83490512019-12-06 21:42:54 -07008#include <common.h>
9#include <dm.h>
10#include <errno.h>
11#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass83490512019-12-06 21:42:54 -070013#include <p2sb.h>
14#include <pch.h>
15#include <pci.h>
16#include <syscon.h>
Simon Glassaac86da2020-07-07 21:32:26 -060017#include <acpi/acpi_device.h>
Simon Glass83490512019-12-06 21:42:54 -070018#include <asm/cpu.h>
19#include <asm/gpio.h>
20#include <asm/intel_pinctrl.h>
21#include <asm/intel_pinctrl_defs.h>
22#include <asm/io.h>
23#include <asm/pci.h>
24#include <asm/arch/gpio.h>
Simon Glassaac86da2020-07-07 21:32:26 -060025#include <dm/acpi.h>
Simon Glass83490512019-12-06 21:42:54 -070026#include <dt-bindings/gpio/x86-gpio.h>
27
Simon Glass83490512019-12-06 21:42:54 -070028static int intel_gpio_get_value(struct udevice *dev, uint offset)
29{
30 struct udevice *pinctrl = dev_get_parent(dev);
31 uint mode, rx_tx;
32 u32 reg;
33
34 reg = intel_pinctrl_get_config_reg(pinctrl, offset);
35 mode = (reg & PAD_CFG0_MODE_MASK) >> PAD_CFG0_MODE_SHIFT;
36 if (!mode) {
37 rx_tx = reg & (PAD_CFG0_TX_DISABLE | PAD_CFG0_RX_DISABLE);
38 if (rx_tx == PAD_CFG0_TX_DISABLE)
Wolfgang Wallner1ec382a2020-02-03 11:38:06 +010039 return reg & PAD_CFG0_RX_STATE ? 1 : 0;
Simon Glass83490512019-12-06 21:42:54 -070040 else if (rx_tx == PAD_CFG0_RX_DISABLE)
Wolfgang Wallner1ec382a2020-02-03 11:38:06 +010041 return reg & PAD_CFG0_TX_STATE ? 1 : 0;
Simon Glass83490512019-12-06 21:42:54 -070042 }
43
44 return 0;
45}
46
Simon Glass25f16c12020-07-07 21:32:19 -060047static int intel_gpio_set_value(struct udevice *dev, unsigned int offset,
48 int value)
Simon Glass83490512019-12-06 21:42:54 -070049{
50 struct udevice *pinctrl = dev_get_parent(dev);
Simon Glass25f16c12020-07-07 21:32:19 -060051 uint config_offset;
52
53 config_offset = intel_pinctrl_get_config_reg_offset(pinctrl, offset);
Simon Glass83490512019-12-06 21:42:54 -070054
Wolfgang Wallnerc3d53e12020-02-03 11:38:04 +010055 pcr_clrsetbits32(pinctrl, config_offset, PAD_CFG0_TX_STATE,
Simon Glass83490512019-12-06 21:42:54 -070056 value ? PAD_CFG0_TX_STATE : 0);
57
58 return 0;
59}
60
61static int intel_gpio_get_function(struct udevice *dev, uint offset)
62{
63 struct udevice *pinctrl = dev_get_parent(dev);
64 uint mode, rx_tx;
65 u32 reg;
66
67 reg = intel_pinctrl_get_config_reg(pinctrl, offset);
68 mode = (reg & PAD_CFG0_MODE_MASK) >> PAD_CFG0_MODE_SHIFT;
69 if (!mode) {
70 rx_tx = reg & (PAD_CFG0_TX_DISABLE | PAD_CFG0_RX_DISABLE);
71 if (rx_tx == PAD_CFG0_TX_DISABLE)
72 return GPIOF_INPUT;
73 else if (rx_tx == PAD_CFG0_RX_DISABLE)
74 return GPIOF_OUTPUT;
75 }
76
77 return GPIOF_FUNC;
78}
79
80static int intel_gpio_xlate(struct udevice *orig_dev, struct gpio_desc *desc,
81 struct ofnode_phandle_args *args)
82{
83 struct udevice *pinctrl, *dev;
84 int gpio, ret;
85
86 /*
87 * GPIO numbers are global in the device tree so it doesn't matter
88 * which one is used
89 */
90 gpio = args->args[0];
91 ret = intel_pinctrl_get_pad(gpio, &pinctrl, &desc->offset);
92 if (ret)
93 return log_msg_ret("bad", ret);
94 device_find_first_child(pinctrl, &dev);
95 if (!dev)
96 return log_msg_ret("no child", -ENOENT);
97 desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;
98 desc->dev = dev;
99
100 return 0;
101}
102
Simon Glassac379392021-02-04 21:22:06 -0700103static int intel_gpio_set_flags(struct udevice *dev, unsigned int offset,
104 ulong flags)
105{
106 struct udevice *pinctrl = dev_get_parent(dev);
107 u32 bic0 = 0, bic1 = 0;
108 u32 or0, or1;
109 uint config_offset;
110
111 config_offset = intel_pinctrl_get_config_reg_offset(pinctrl, offset);
112
113 if (flags & GPIOD_IS_OUT) {
114 bic0 |= PAD_CFG0_MODE_MASK | PAD_CFG0_RX_STATE |
115 PAD_CFG0_TX_DISABLE;
116 or0 |= PAD_CFG0_MODE_GPIO | PAD_CFG0_RX_DISABLE;
117 } else if (flags & GPIOD_IS_IN) {
118 bic0 |= PAD_CFG0_MODE_MASK | PAD_CFG0_TX_STATE |
119 PAD_CFG0_RX_DISABLE;
120 or0 |= PAD_CFG0_MODE_GPIO | PAD_CFG0_TX_DISABLE;
121 }
122 if (flags & GPIOD_PULL_UP) {
123 bic1 |= PAD_CFG1_PULL_MASK;
124 or1 |= PAD_CFG1_PULL_UP_20K;
125 } else if (flags & GPIOD_PULL_DOWN) {
126 bic1 |= PAD_CFG1_PULL_MASK;
127 or1 |= PAD_CFG1_PULL_DN_20K;
128 }
129
130 pcr_clrsetbits32(pinctrl, PAD_CFG0_OFFSET(config_offset), bic0, or0);
131 pcr_clrsetbits32(pinctrl, PAD_CFG1_OFFSET(config_offset), bic1, or1);
132 log_debug("%s: flags=%lx, offset=%x, config_offset=%x, %x/%x %x/%x\n",
133 dev->name, flags, offset, config_offset, bic0, or0, bic1, or1);
134
135 return 0;
136}
137
Simon Glassaac86da2020-07-07 21:32:26 -0600138#if CONFIG_IS_ENABLED(ACPIGEN)
139static int intel_gpio_get_acpi(const struct gpio_desc *desc,
140 struct acpi_gpio *gpio)
141{
142 struct udevice *pinctrl;
143 int ret;
144
145 if (!dm_gpio_is_valid(desc))
146 return -ENOENT;
147 pinctrl = dev_get_parent(desc->dev);
148
149 memset(gpio, '\0', sizeof(*gpio));
150
151 gpio->type = ACPI_GPIO_TYPE_IO;
152 gpio->pull = ACPI_GPIO_PULL_DEFAULT;
153 gpio->io_restrict = ACPI_GPIO_IO_RESTRICT_OUTPUT;
154 gpio->polarity = ACPI_GPIO_ACTIVE_HIGH;
155 gpio->pin_count = 1;
156 gpio->pins[0] = intel_pinctrl_get_acpi_pin(pinctrl, desc->offset);
157 gpio->pin0_addr = intel_pinctrl_get_config_reg_addr(pinctrl,
158 desc->offset);
159 ret = acpi_get_path(pinctrl, gpio->resource, sizeof(gpio->resource));
160 if (ret)
161 return log_msg_ret("resource", ret);
162
163 return 0;
164}
165#endif
166
Simon Glass83490512019-12-06 21:42:54 -0700167static int intel_gpio_probe(struct udevice *dev)
168{
169 return 0;
170}
171
Simon Glassaad29ae2020-12-03 16:55:21 -0700172static int intel_gpio_of_to_plat(struct udevice *dev)
Simon Glass83490512019-12-06 21:42:54 -0700173{
174 struct gpio_dev_priv *upriv = dev_get_uclass_priv(dev);
175 struct intel_pinctrl_priv *pinctrl_priv = dev_get_priv(dev->parent);
176 const struct pad_community *comm = pinctrl_priv->comm;
177
178 upriv->gpio_count = comm->last_pad - comm->first_pad + 1;
179 upriv->bank_name = dev->name;
180
181 return 0;
182}
183
184static const struct dm_gpio_ops gpio_intel_ops = {
Simon Glass83490512019-12-06 21:42:54 -0700185 .get_value = intel_gpio_get_value,
186 .set_value = intel_gpio_set_value,
187 .get_function = intel_gpio_get_function,
188 .xlate = intel_gpio_xlate,
Simon Glassac379392021-02-04 21:22:06 -0700189 .set_flags = intel_gpio_set_flags,
Simon Glassaac86da2020-07-07 21:32:26 -0600190#if CONFIG_IS_ENABLED(ACPIGEN)
191 .get_acpi = intel_gpio_get_acpi,
192#endif
Simon Glass83490512019-12-06 21:42:54 -0700193};
194
Simon Glassec8ae8a2020-12-23 08:11:30 -0700195#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass83490512019-12-06 21:42:54 -0700196static const struct udevice_id intel_intel_gpio_ids[] = {
197 { .compatible = "intel,gpio" },
198 { }
199};
Simon Glassec8ae8a2020-12-23 08:11:30 -0700200#endif
Simon Glass83490512019-12-06 21:42:54 -0700201
Simon Glassa055da82020-10-05 05:27:01 -0600202U_BOOT_DRIVER(intel_gpio) = {
203 .name = "intel_gpio",
Simon Glass83490512019-12-06 21:42:54 -0700204 .id = UCLASS_GPIO,
Simon Glassec8ae8a2020-12-23 08:11:30 -0700205 .of_match = of_match_ptr(intel_intel_gpio_ids),
Simon Glass83490512019-12-06 21:42:54 -0700206 .ops = &gpio_intel_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700207 .of_to_plat = intel_gpio_of_to_plat,
Simon Glass83490512019-12-06 21:42:54 -0700208 .probe = intel_gpio_probe,
209};