Simon Glass | 8349051 | 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 | ac37939 | 2021-02-04 21:22:06 -0700 | [diff] [blame^] | 6 | #define LOG_CATEGORY UCLASS_GPIO |
| 7 | |
Simon Glass | 8349051 | 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 | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 8349051 | 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 | aac86da | 2020-07-07 21:32:26 -0600 | [diff] [blame] | 17 | #include <acpi/acpi_device.h> |
Simon Glass | 8349051 | 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 | aac86da | 2020-07-07 21:32:26 -0600 | [diff] [blame] | 25 | #include <dm/acpi.h> |
Simon Glass | 8349051 | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 26 | #include <dt-bindings/gpio/x86-gpio.h> |
| 27 | |
Simon Glass | 8349051 | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 28 | static 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 Wallner | 1ec382a | 2020-02-03 11:38:06 +0100 | [diff] [blame] | 39 | return reg & PAD_CFG0_RX_STATE ? 1 : 0; |
Simon Glass | 8349051 | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 40 | else if (rx_tx == PAD_CFG0_RX_DISABLE) |
Wolfgang Wallner | 1ec382a | 2020-02-03 11:38:06 +0100 | [diff] [blame] | 41 | return reg & PAD_CFG0_TX_STATE ? 1 : 0; |
Simon Glass | 8349051 | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
Simon Glass | 25f16c1 | 2020-07-07 21:32:19 -0600 | [diff] [blame] | 47 | static int intel_gpio_set_value(struct udevice *dev, unsigned int offset, |
| 48 | int value) |
Simon Glass | 8349051 | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 49 | { |
| 50 | struct udevice *pinctrl = dev_get_parent(dev); |
Simon Glass | 25f16c1 | 2020-07-07 21:32:19 -0600 | [diff] [blame] | 51 | uint config_offset; |
| 52 | |
| 53 | config_offset = intel_pinctrl_get_config_reg_offset(pinctrl, offset); |
Simon Glass | 8349051 | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 54 | |
Wolfgang Wallner | c3d53e1 | 2020-02-03 11:38:04 +0100 | [diff] [blame] | 55 | pcr_clrsetbits32(pinctrl, config_offset, PAD_CFG0_TX_STATE, |
Simon Glass | 8349051 | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 56 | value ? PAD_CFG0_TX_STATE : 0); |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static 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 | |
| 80 | static 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 Glass | ac37939 | 2021-02-04 21:22:06 -0700 | [diff] [blame^] | 103 | static 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 Glass | aac86da | 2020-07-07 21:32:26 -0600 | [diff] [blame] | 138 | #if CONFIG_IS_ENABLED(ACPIGEN) |
| 139 | static 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 Glass | 8349051 | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 167 | static int intel_gpio_probe(struct udevice *dev) |
| 168 | { |
| 169 | return 0; |
| 170 | } |
| 171 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 172 | static int intel_gpio_of_to_plat(struct udevice *dev) |
Simon Glass | 8349051 | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 173 | { |
| 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 | |
| 184 | static const struct dm_gpio_ops gpio_intel_ops = { |
Simon Glass | 8349051 | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 185 | .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 Glass | ac37939 | 2021-02-04 21:22:06 -0700 | [diff] [blame^] | 189 | .set_flags = intel_gpio_set_flags, |
Simon Glass | aac86da | 2020-07-07 21:32:26 -0600 | [diff] [blame] | 190 | #if CONFIG_IS_ENABLED(ACPIGEN) |
| 191 | .get_acpi = intel_gpio_get_acpi, |
| 192 | #endif |
Simon Glass | 8349051 | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 193 | }; |
| 194 | |
Simon Glass | ec8ae8a | 2020-12-23 08:11:30 -0700 | [diff] [blame] | 195 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 8349051 | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 196 | static const struct udevice_id intel_intel_gpio_ids[] = { |
| 197 | { .compatible = "intel,gpio" }, |
| 198 | { } |
| 199 | }; |
Simon Glass | ec8ae8a | 2020-12-23 08:11:30 -0700 | [diff] [blame] | 200 | #endif |
Simon Glass | 8349051 | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 201 | |
Simon Glass | a055da8 | 2020-10-05 05:27:01 -0600 | [diff] [blame] | 202 | U_BOOT_DRIVER(intel_gpio) = { |
| 203 | .name = "intel_gpio", |
Simon Glass | 8349051 | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 204 | .id = UCLASS_GPIO, |
Simon Glass | ec8ae8a | 2020-12-23 08:11:30 -0700 | [diff] [blame] | 205 | .of_match = of_match_ptr(intel_intel_gpio_ids), |
Simon Glass | 8349051 | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 206 | .ops = &gpio_intel_ops, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 207 | .of_to_plat = intel_gpio_of_to_plat, |
Simon Glass | 8349051 | 2019-12-06 21:42:54 -0700 | [diff] [blame] | 208 | .probe = intel_gpio_probe, |
| 209 | }; |