blob: 4a3ec6d6350230ae36a8a870cedb0a088d81a889 [file] [log] [blame]
Simon Glass7e589bc2019-12-06 21:42:54 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2019 Google LLC
4 */
5
Simon Glassedab1142021-02-04 21:22:06 -07006#define LOG_CATEGORY UCLASS_GPIO
7
Simon Glass7e589bc2019-12-06 21:42:54 -07008#include <common.h>
9#include <dm.h>
10#include <errno.h>
11#include <fdtdec.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glass7e589bc2019-12-06 21:42:54 -070013#include <p2sb.h>
14#include <pch.h>
15#include <pci.h>
16#include <syscon.h>
Simon Glass43ee86c2020-07-07 21:32:26 -060017#include <acpi/acpi_device.h>
Simon Glass7e589bc2019-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 Glass43ee86c2020-07-07 21:32:26 -060025#include <dm/acpi.h>
Simon Glass7d252d02021-03-15 18:00:04 +130026#include <dm/device-internal.h>
Simon Glass7e589bc2019-12-06 21:42:54 -070027#include <dt-bindings/gpio/x86-gpio.h>
28
Simon Glass7e589bc2019-12-06 21:42:54 -070029static int intel_gpio_get_value(struct udevice *dev, uint offset)
30{
31 struct udevice *pinctrl = dev_get_parent(dev);
32 uint mode, rx_tx;
33 u32 reg;
34
35 reg = intel_pinctrl_get_config_reg(pinctrl, offset);
36 mode = (reg & PAD_CFG0_MODE_MASK) >> PAD_CFG0_MODE_SHIFT;
37 if (!mode) {
38 rx_tx = reg & (PAD_CFG0_TX_DISABLE | PAD_CFG0_RX_DISABLE);
39 if (rx_tx == PAD_CFG0_TX_DISABLE)
Wolfgang Wallnerea86e722020-02-03 11:38:06 +010040 return reg & PAD_CFG0_RX_STATE ? 1 : 0;
Simon Glass7e589bc2019-12-06 21:42:54 -070041 else if (rx_tx == PAD_CFG0_RX_DISABLE)
Wolfgang Wallnerea86e722020-02-03 11:38:06 +010042 return reg & PAD_CFG0_TX_STATE ? 1 : 0;
Simon Glass7e589bc2019-12-06 21:42:54 -070043 }
44
45 return 0;
46}
47
Simon Glass4916f452020-07-07 21:32:19 -060048static int intel_gpio_set_value(struct udevice *dev, unsigned int offset,
49 int value)
Simon Glass7e589bc2019-12-06 21:42:54 -070050{
51 struct udevice *pinctrl = dev_get_parent(dev);
Simon Glass4916f452020-07-07 21:32:19 -060052 uint config_offset;
53
54 config_offset = intel_pinctrl_get_config_reg_offset(pinctrl, offset);
Simon Glass7e589bc2019-12-06 21:42:54 -070055
Wolfgang Wallnerb840c392020-02-03 11:38:04 +010056 pcr_clrsetbits32(pinctrl, config_offset, PAD_CFG0_TX_STATE,
Simon Glass7e589bc2019-12-06 21:42:54 -070057 value ? PAD_CFG0_TX_STATE : 0);
58
59 return 0;
60}
61
62static int intel_gpio_get_function(struct udevice *dev, uint offset)
63{
64 struct udevice *pinctrl = dev_get_parent(dev);
65 uint mode, rx_tx;
66 u32 reg;
67
68 reg = intel_pinctrl_get_config_reg(pinctrl, offset);
69 mode = (reg & PAD_CFG0_MODE_MASK) >> PAD_CFG0_MODE_SHIFT;
70 if (!mode) {
71 rx_tx = reg & (PAD_CFG0_TX_DISABLE | PAD_CFG0_RX_DISABLE);
72 if (rx_tx == PAD_CFG0_TX_DISABLE)
73 return GPIOF_INPUT;
74 else if (rx_tx == PAD_CFG0_RX_DISABLE)
75 return GPIOF_OUTPUT;
76 }
77
78 return GPIOF_FUNC;
79}
80
81static int intel_gpio_xlate(struct udevice *orig_dev, struct gpio_desc *desc,
82 struct ofnode_phandle_args *args)
83{
84 struct udevice *pinctrl, *dev;
85 int gpio, ret;
86
87 /*
88 * GPIO numbers are global in the device tree so it doesn't matter
Simon Glass7d252d02021-03-15 18:00:04 +130089 * which @orig_dev is used
Simon Glass7e589bc2019-12-06 21:42:54 -070090 */
91 gpio = args->args[0];
92 ret = intel_pinctrl_get_pad(gpio, &pinctrl, &desc->offset);
93 if (ret)
94 return log_msg_ret("bad", ret);
95 device_find_first_child(pinctrl, &dev);
96 if (!dev)
97 return log_msg_ret("no child", -ENOENT);
98 desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;
99 desc->dev = dev;
100
Simon Glass7d252d02021-03-15 18:00:04 +1300101 /*
102 * Handle the case where the wrong GPIO device was provided, since this
103 * will not have been probed by the GPIO uclass before calling here
104 * (see gpio_request_tail()).
105 */
106 if (orig_dev != dev) {
107 ret = device_probe(dev);
108 if (ret)
109 return log_msg_ret("probe", ret);
110 }
111
Simon Glass7e589bc2019-12-06 21:42:54 -0700112 return 0;
113}
114
Simon Glassedab1142021-02-04 21:22:06 -0700115static int intel_gpio_set_flags(struct udevice *dev, unsigned int offset,
116 ulong flags)
117{
118 struct udevice *pinctrl = dev_get_parent(dev);
119 u32 bic0 = 0, bic1 = 0;
120 u32 or0, or1;
121 uint config_offset;
122
123 config_offset = intel_pinctrl_get_config_reg_offset(pinctrl, offset);
124
125 if (flags & GPIOD_IS_OUT) {
126 bic0 |= PAD_CFG0_MODE_MASK | PAD_CFG0_RX_STATE |
127 PAD_CFG0_TX_DISABLE;
128 or0 |= PAD_CFG0_MODE_GPIO | PAD_CFG0_RX_DISABLE;
129 } else if (flags & GPIOD_IS_IN) {
130 bic0 |= PAD_CFG0_MODE_MASK | PAD_CFG0_TX_STATE |
131 PAD_CFG0_RX_DISABLE;
132 or0 |= PAD_CFG0_MODE_GPIO | PAD_CFG0_TX_DISABLE;
133 }
134 if (flags & GPIOD_PULL_UP) {
135 bic1 |= PAD_CFG1_PULL_MASK;
136 or1 |= PAD_CFG1_PULL_UP_20K;
137 } else if (flags & GPIOD_PULL_DOWN) {
138 bic1 |= PAD_CFG1_PULL_MASK;
139 or1 |= PAD_CFG1_PULL_DN_20K;
140 }
141
142 pcr_clrsetbits32(pinctrl, PAD_CFG0_OFFSET(config_offset), bic0, or0);
143 pcr_clrsetbits32(pinctrl, PAD_CFG1_OFFSET(config_offset), bic1, or1);
144 log_debug("%s: flags=%lx, offset=%x, config_offset=%x, %x/%x %x/%x\n",
145 dev->name, flags, offset, config_offset, bic0, or0, bic1, or1);
146
147 return 0;
148}
149
Simon Glass43ee86c2020-07-07 21:32:26 -0600150#if CONFIG_IS_ENABLED(ACPIGEN)
151static int intel_gpio_get_acpi(const struct gpio_desc *desc,
152 struct acpi_gpio *gpio)
153{
154 struct udevice *pinctrl;
155 int ret;
156
157 if (!dm_gpio_is_valid(desc))
158 return -ENOENT;
159 pinctrl = dev_get_parent(desc->dev);
160
161 memset(gpio, '\0', sizeof(*gpio));
162
163 gpio->type = ACPI_GPIO_TYPE_IO;
164 gpio->pull = ACPI_GPIO_PULL_DEFAULT;
165 gpio->io_restrict = ACPI_GPIO_IO_RESTRICT_OUTPUT;
166 gpio->polarity = ACPI_GPIO_ACTIVE_HIGH;
167 gpio->pin_count = 1;
168 gpio->pins[0] = intel_pinctrl_get_acpi_pin(pinctrl, desc->offset);
169 gpio->pin0_addr = intel_pinctrl_get_config_reg_addr(pinctrl,
170 desc->offset);
171 ret = acpi_get_path(pinctrl, gpio->resource, sizeof(gpio->resource));
172 if (ret)
173 return log_msg_ret("resource", ret);
174
175 return 0;
176}
177#endif
178
Simon Glass7e589bc2019-12-06 21:42:54 -0700179static int intel_gpio_probe(struct udevice *dev)
180{
181 return 0;
182}
183
Simon Glassd1998a92020-12-03 16:55:21 -0700184static int intel_gpio_of_to_plat(struct udevice *dev)
Simon Glass7e589bc2019-12-06 21:42:54 -0700185{
186 struct gpio_dev_priv *upriv = dev_get_uclass_priv(dev);
187 struct intel_pinctrl_priv *pinctrl_priv = dev_get_priv(dev->parent);
188 const struct pad_community *comm = pinctrl_priv->comm;
189
190 upriv->gpio_count = comm->last_pad - comm->first_pad + 1;
191 upriv->bank_name = dev->name;
192
193 return 0;
194}
195
196static const struct dm_gpio_ops gpio_intel_ops = {
Simon Glass7e589bc2019-12-06 21:42:54 -0700197 .get_value = intel_gpio_get_value,
198 .set_value = intel_gpio_set_value,
199 .get_function = intel_gpio_get_function,
200 .xlate = intel_gpio_xlate,
Simon Glassedab1142021-02-04 21:22:06 -0700201 .set_flags = intel_gpio_set_flags,
Simon Glass43ee86c2020-07-07 21:32:26 -0600202#if CONFIG_IS_ENABLED(ACPIGEN)
203 .get_acpi = intel_gpio_get_acpi,
204#endif
Simon Glass7e589bc2019-12-06 21:42:54 -0700205};
206
Simon Glass95397382021-08-07 07:24:04 -0600207#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glass7e589bc2019-12-06 21:42:54 -0700208static const struct udevice_id intel_intel_gpio_ids[] = {
209 { .compatible = "intel,gpio" },
210 { }
211};
Simon Glass8b842be2020-12-23 08:11:30 -0700212#endif
Simon Glass7e589bc2019-12-06 21:42:54 -0700213
Simon Glass9d20db02020-10-05 05:27:01 -0600214U_BOOT_DRIVER(intel_gpio) = {
215 .name = "intel_gpio",
Simon Glass7e589bc2019-12-06 21:42:54 -0700216 .id = UCLASS_GPIO,
Simon Glass8b842be2020-12-23 08:11:30 -0700217 .of_match = of_match_ptr(intel_intel_gpio_ids),
Simon Glass7e589bc2019-12-06 21:42:54 -0700218 .ops = &gpio_intel_ops,
Simon Glassd1998a92020-12-03 16:55:21 -0700219 .of_to_plat = intel_gpio_of_to_plat,
Simon Glass7e589bc2019-12-06 21:42:54 -0700220 .probe = intel_gpio_probe,
221};