Simon Glass | 7e589bc | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2019 Google LLC |
| 4 | */ |
| 5 | |
Simon Glass | edab114 | 2021-02-04 21:22:06 -0700 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_GPIO |
| 7 | |
Simon Glass | 7e589bc | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <errno.h> |
| 11 | #include <fdtdec.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 7e589bc | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 13 | #include <p2sb.h> |
| 14 | #include <pch.h> |
| 15 | #include <pci.h> |
| 16 | #include <syscon.h> |
Simon Glass | 43ee86c | 2020-07-07 21:32:26 -0600 | [diff] [blame] | 17 | #include <acpi/acpi_device.h> |
Simon Glass | 7e589bc | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 18 | #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 Glass | 43ee86c | 2020-07-07 21:32:26 -0600 | [diff] [blame] | 25 | #include <dm/acpi.h> |
Simon Glass | 7d252d0 | 2021-03-15 18:00:04 +1300 | [diff] [blame] | 26 | #include <dm/device-internal.h> |
Simon Glass | 7e589bc | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 27 | #include <dt-bindings/gpio/x86-gpio.h> |
| 28 | |
Simon Glass | 7e589bc | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 29 | static 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 Wallner | ea86e72 | 2020-02-03 11:38:06 +0100 | [diff] [blame] | 40 | return reg & PAD_CFG0_RX_STATE ? 1 : 0; |
Simon Glass | 7e589bc | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 41 | else if (rx_tx == PAD_CFG0_RX_DISABLE) |
Wolfgang Wallner | ea86e72 | 2020-02-03 11:38:06 +0100 | [diff] [blame] | 42 | return reg & PAD_CFG0_TX_STATE ? 1 : 0; |
Simon Glass | 7e589bc | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
Simon Glass | 4916f45 | 2020-07-07 21:32:19 -0600 | [diff] [blame] | 48 | static int intel_gpio_set_value(struct udevice *dev, unsigned int offset, |
| 49 | int value) |
Simon Glass | 7e589bc | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 50 | { |
| 51 | struct udevice *pinctrl = dev_get_parent(dev); |
Simon Glass | 4916f45 | 2020-07-07 21:32:19 -0600 | [diff] [blame] | 52 | uint config_offset; |
| 53 | |
| 54 | config_offset = intel_pinctrl_get_config_reg_offset(pinctrl, offset); |
Simon Glass | 7e589bc | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 55 | |
Wolfgang Wallner | b840c39 | 2020-02-03 11:38:04 +0100 | [diff] [blame] | 56 | pcr_clrsetbits32(pinctrl, config_offset, PAD_CFG0_TX_STATE, |
Simon Glass | 7e589bc | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 57 | value ? PAD_CFG0_TX_STATE : 0); |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static 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 | |
| 81 | static 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 Glass | 7d252d0 | 2021-03-15 18:00:04 +1300 | [diff] [blame] | 89 | * which @orig_dev is used |
Simon Glass | 7e589bc | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 90 | */ |
| 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 Glass | 7d252d0 | 2021-03-15 18:00:04 +1300 | [diff] [blame] | 101 | /* |
| 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 Glass | 7e589bc | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
Simon Glass | edab114 | 2021-02-04 21:22:06 -0700 | [diff] [blame] | 115 | static 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 Glass | 43ee86c | 2020-07-07 21:32:26 -0600 | [diff] [blame] | 150 | #if CONFIG_IS_ENABLED(ACPIGEN) |
| 151 | static 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 Glass | 7e589bc | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 179 | static int intel_gpio_probe(struct udevice *dev) |
| 180 | { |
| 181 | return 0; |
| 182 | } |
| 183 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 184 | static int intel_gpio_of_to_plat(struct udevice *dev) |
Simon Glass | 7e589bc | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 185 | { |
| 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 | |
| 196 | static const struct dm_gpio_ops gpio_intel_ops = { |
Simon Glass | 7e589bc | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 197 | .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 Glass | edab114 | 2021-02-04 21:22:06 -0700 | [diff] [blame] | 201 | .set_flags = intel_gpio_set_flags, |
Simon Glass | 43ee86c | 2020-07-07 21:32:26 -0600 | [diff] [blame] | 202 | #if CONFIG_IS_ENABLED(ACPIGEN) |
| 203 | .get_acpi = intel_gpio_get_acpi, |
| 204 | #endif |
Simon Glass | 7e589bc | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 205 | }; |
| 206 | |
Simon Glass | 9539738 | 2021-08-07 07:24:04 -0600 | [diff] [blame] | 207 | #if CONFIG_IS_ENABLED(OF_REAL) |
Simon Glass | 7e589bc | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 208 | static const struct udevice_id intel_intel_gpio_ids[] = { |
| 209 | { .compatible = "intel,gpio" }, |
| 210 | { } |
| 211 | }; |
Simon Glass | 8b842be | 2020-12-23 08:11:30 -0700 | [diff] [blame] | 212 | #endif |
Simon Glass | 7e589bc | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 213 | |
Simon Glass | 9d20db0 | 2020-10-05 05:27:01 -0600 | [diff] [blame] | 214 | U_BOOT_DRIVER(intel_gpio) = { |
| 215 | .name = "intel_gpio", |
Simon Glass | 7e589bc | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 216 | .id = UCLASS_GPIO, |
Simon Glass | 8b842be | 2020-12-23 08:11:30 -0700 | [diff] [blame] | 217 | .of_match = of_match_ptr(intel_intel_gpio_ids), |
Simon Glass | 7e589bc | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 218 | .ops = &gpio_intel_ops, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 219 | .of_to_plat = intel_gpio_of_to_plat, |
Simon Glass | 7e589bc | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 220 | .probe = intel_gpio_probe, |
| 221 | }; |