Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012 The Chromium OS Authors. |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * This is a GPIO driver for Intel ICH6 and later. The x86 GPIOs are accessed |
| 8 | * through the PCI bus. Each PCI device has 256 bytes of configuration space, |
| 9 | * consisting of a standard header and a device-specific set of registers. PCI |
| 10 | * bus 0, device 31, function 0 gives us access to the chipset GPIOs (among |
| 11 | * other things). Within the PCI configuration space, the GPIOBASE register |
| 12 | * tells us where in the device's I/O region we can find more registers to |
| 13 | * actually access the GPIOs. |
| 14 | * |
| 15 | * PCI bus/device/function 0:1f:0 => PCI config registers |
| 16 | * PCI config register "GPIOBASE" |
| 17 | * PCI I/O space + [GPIOBASE] => start of GPIO registers |
| 18 | * GPIO registers => gpio pin function, direction, value |
Bill Richardson | 50a5ebe | 2012-10-20 11:44:36 +0000 | [diff] [blame] | 19 | * |
| 20 | * |
| 21 | * Danger Will Robinson! Bank 0 (GPIOs 0-31) seems to be fairly stable. Most |
| 22 | * ICH versions have more, but the decoding the matrix that describes them is |
| 23 | * absurdly complex and constantly changing. We'll provide Bank 1 and Bank 2, |
| 24 | * but they will ONLY work for certain unspecified chipsets because the offset |
| 25 | * from GPIOBASE changes randomly. Even then, many GPIOs are unimplemented or |
| 26 | * reserved or subject to arcane restrictions. |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 27 | */ |
| 28 | |
Simon Glass | 3a1d96f | 2023-07-15 21:39:11 -0600 | [diff] [blame] | 29 | #define LOG_CATEGORY UCLASS_GPIO |
| 30 | |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 31 | #include <common.h> |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 32 | #include <dm.h> |
| 33 | #include <errno.h> |
| 34 | #include <fdtdec.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 35 | #include <log.h> |
Bin Meng | 6e916cc | 2016-02-01 01:40:47 -0800 | [diff] [blame] | 36 | #include <pch.h> |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 37 | #include <pci.h> |
Simon Glass | fb8757b | 2016-03-11 22:07:14 -0700 | [diff] [blame] | 38 | #include <asm/cpu.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 39 | #include <asm/global_data.h> |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 40 | #include <asm/gpio.h> |
| 41 | #include <asm/io.h> |
Simon Glass | 60af017 | 2014-11-12 22:42:24 -0700 | [diff] [blame] | 42 | #include <asm/pci.h> |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 43 | |
Simon Glass | daa93d9 | 2015-07-31 09:31:31 -0600 | [diff] [blame] | 44 | DECLARE_GLOBAL_DATA_PTR; |
| 45 | |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 46 | #define GPIO_PER_BANK 32 |
| 47 | |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 48 | struct ich6_bank_priv { |
| 49 | /* These are I/O addresses */ |
Bin Meng | 9b64969 | 2014-12-17 15:50:38 +0800 | [diff] [blame] | 50 | uint16_t use_sel; |
| 51 | uint16_t io_sel; |
| 52 | uint16_t lvl; |
Bin Meng | 59be2c0 | 2017-05-07 19:52:29 -0700 | [diff] [blame] | 53 | u32 lvl_write_cache; |
| 54 | bool use_lvl_write_cache; |
Bill Richardson | 50a5ebe | 2012-10-20 11:44:36 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
Gabriel Huau | f813548 | 2015-05-25 22:27:37 -0700 | [diff] [blame] | 57 | #define GPIO_USESEL_OFFSET(x) (x) |
| 58 | #define GPIO_IOSEL_OFFSET(x) (x + 4) |
| 59 | #define GPIO_LVL_OFFSET(x) (x + 8) |
| 60 | |
Bin Meng | 59be2c0 | 2017-05-07 19:52:29 -0700 | [diff] [blame] | 61 | static int _ich6_gpio_set_value(struct ich6_bank_priv *bank, unsigned offset, |
| 62 | int value) |
Gabriel Huau | f813548 | 2015-05-25 22:27:37 -0700 | [diff] [blame] | 63 | { |
| 64 | u32 val; |
| 65 | |
Bin Meng | 59be2c0 | 2017-05-07 19:52:29 -0700 | [diff] [blame] | 66 | if (bank->use_lvl_write_cache) |
| 67 | val = bank->lvl_write_cache; |
| 68 | else |
| 69 | val = inl(bank->lvl); |
| 70 | |
Gabriel Huau | f813548 | 2015-05-25 22:27:37 -0700 | [diff] [blame] | 71 | if (value) |
| 72 | val |= (1UL << offset); |
| 73 | else |
| 74 | val &= ~(1UL << offset); |
Bin Meng | 59be2c0 | 2017-05-07 19:52:29 -0700 | [diff] [blame] | 75 | outl(val, bank->lvl); |
| 76 | if (bank->use_lvl_write_cache) |
| 77 | bank->lvl_write_cache = val; |
Gabriel Huau | f813548 | 2015-05-25 22:27:37 -0700 | [diff] [blame] | 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
Gabriel Huau | f813548 | 2015-05-25 22:27:37 -0700 | [diff] [blame] | 82 | static int _ich6_gpio_set_direction(uint16_t base, unsigned offset, int dir) |
| 83 | { |
| 84 | u32 val; |
| 85 | |
| 86 | if (!dir) { |
| 87 | val = inl(base); |
| 88 | val |= (1UL << offset); |
| 89 | outl(val, base); |
| 90 | } else { |
| 91 | val = inl(base); |
| 92 | val &= ~(1UL << offset); |
| 93 | outl(val, base); |
| 94 | } |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 99 | static int gpio_ich6_of_to_plat(struct udevice *dev) |
Gabriel Huau | f813548 | 2015-05-25 22:27:37 -0700 | [diff] [blame] | 100 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 101 | struct ich6_bank_plat *plat = dev_get_plat(dev); |
Bin Meng | 6e916cc | 2016-02-01 01:40:47 -0800 | [diff] [blame] | 102 | u32 gpiobase; |
Gabriel Huau | f813548 | 2015-05-25 22:27:37 -0700 | [diff] [blame] | 103 | int offset; |
Bin Meng | 6e916cc | 2016-02-01 01:40:47 -0800 | [diff] [blame] | 104 | int ret; |
| 105 | |
| 106 | ret = pch_get_gpio_base(dev->parent, &gpiobase); |
| 107 | if (ret) |
| 108 | return ret; |
Gabriel Huau | f813548 | 2015-05-25 22:27:37 -0700 | [diff] [blame] | 109 | |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 110 | offset = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "reg", -1); |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 111 | if (offset == -1) { |
| 112 | debug("%s: Invalid register offset %d\n", __func__, offset); |
| 113 | return -EINVAL; |
| 114 | } |
Simon Glass | ec5c786 | 2016-03-06 19:28:13 -0700 | [diff] [blame] | 115 | plat->offset = offset; |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 116 | plat->base_addr = gpiobase + offset; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 117 | plat->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 118 | "bank-name", NULL); |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 119 | |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
Simon Glass | 60af017 | 2014-11-12 22:42:24 -0700 | [diff] [blame] | 123 | static int ich6_gpio_probe(struct udevice *dev) |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 124 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 125 | struct ich6_bank_plat *plat = dev_get_plat(dev); |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 126 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 127 | struct ich6_bank_priv *bank = dev_get_priv(dev); |
Bin Meng | 59be2c0 | 2017-05-07 19:52:29 -0700 | [diff] [blame] | 128 | const void *prop; |
Bin Meng | a55173f | 2014-12-12 21:05:23 +0800 | [diff] [blame] | 129 | |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 130 | uc_priv->gpio_count = GPIO_PER_BANK; |
| 131 | uc_priv->bank_name = plat->bank_name; |
| 132 | bank->use_sel = plat->base_addr; |
| 133 | bank->io_sel = plat->base_addr + 4; |
| 134 | bank->lvl = plat->base_addr + 8; |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 135 | |
Simon Glass | 7a49443 | 2017-05-17 17:18:09 -0600 | [diff] [blame] | 136 | prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), |
Bin Meng | 59be2c0 | 2017-05-07 19:52:29 -0700 | [diff] [blame] | 137 | "use-lvl-write-cache", NULL); |
| 138 | if (prop) |
| 139 | bank->use_lvl_write_cache = true; |
| 140 | else |
| 141 | bank->use_lvl_write_cache = false; |
| 142 | bank->lvl_write_cache = 0; |
| 143 | |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 144 | return 0; |
| 145 | } |
| 146 | |
Simon Glass | 60af017 | 2014-11-12 22:42:24 -0700 | [diff] [blame] | 147 | static int ich6_gpio_request(struct udevice *dev, unsigned offset, |
| 148 | const char *label) |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 149 | { |
| 150 | struct ich6_bank_priv *bank = dev_get_priv(dev); |
| 151 | u32 tmplong; |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | * Make sure that the GPIO pin we want isn't already in use for some |
| 155 | * built-in hardware function. We have to check this for every |
| 156 | * requested pin. |
| 157 | */ |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 158 | tmplong = inl(bank->use_sel); |
| 159 | if (!(tmplong & (1UL << offset))) { |
Simon Glass | 3a1d96f | 2023-07-15 21:39:11 -0600 | [diff] [blame] | 160 | log_debug("gpio %d is reserved for internal use\n", offset); |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 161 | return -EPERM; |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 164 | return 0; |
| 165 | } |
| 166 | |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 167 | static int ich6_gpio_direction_input(struct udevice *dev, unsigned offset) |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 168 | { |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 169 | struct ich6_bank_priv *bank = dev_get_priv(dev); |
Bill Richardson | 50a5ebe | 2012-10-20 11:44:36 +0000 | [diff] [blame] | 170 | |
Simon Glass | 94641cb | 2015-08-22 15:58:58 -0600 | [diff] [blame] | 171 | return _ich6_gpio_set_direction(bank->io_sel, offset, 0); |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 174 | static int ich6_gpio_direction_output(struct udevice *dev, unsigned offset, |
| 175 | int value) |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 176 | { |
Gabriel Huau | f813548 | 2015-05-25 22:27:37 -0700 | [diff] [blame] | 177 | int ret; |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 178 | struct ich6_bank_priv *bank = dev_get_priv(dev); |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 179 | |
Simon Glass | 94641cb | 2015-08-22 15:58:58 -0600 | [diff] [blame] | 180 | ret = _ich6_gpio_set_direction(bank->io_sel, offset, 1); |
Gabriel Huau | f813548 | 2015-05-25 22:27:37 -0700 | [diff] [blame] | 181 | if (ret) |
| 182 | return ret; |
Axel Lin | 6df0e9e | 2014-12-07 12:48:27 +0800 | [diff] [blame] | 183 | |
Bin Meng | 59be2c0 | 2017-05-07 19:52:29 -0700 | [diff] [blame] | 184 | return _ich6_gpio_set_value(bank, offset, value); |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 187 | static int ich6_gpio_get_value(struct udevice *dev, unsigned offset) |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 188 | { |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 189 | struct ich6_bank_priv *bank = dev_get_priv(dev); |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 190 | u32 tmplong; |
Bill Richardson | 50a5ebe | 2012-10-20 11:44:36 +0000 | [diff] [blame] | 191 | int r; |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 192 | |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 193 | tmplong = inl(bank->lvl); |
Bin Meng | 59be2c0 | 2017-05-07 19:52:29 -0700 | [diff] [blame] | 194 | if (bank->use_lvl_write_cache) |
| 195 | tmplong |= bank->lvl_write_cache; |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 196 | r = (tmplong & (1UL << offset)) ? 1 : 0; |
Bill Richardson | 50a5ebe | 2012-10-20 11:44:36 +0000 | [diff] [blame] | 197 | return r; |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 200 | static int ich6_gpio_set_value(struct udevice *dev, unsigned offset, |
| 201 | int value) |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 202 | { |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 203 | struct ich6_bank_priv *bank = dev_get_priv(dev); |
Bin Meng | 59be2c0 | 2017-05-07 19:52:29 -0700 | [diff] [blame] | 204 | return _ich6_gpio_set_value(bank, offset, value); |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 205 | } |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 206 | |
| 207 | static int ich6_gpio_get_function(struct udevice *dev, unsigned offset) |
| 208 | { |
| 209 | struct ich6_bank_priv *bank = dev_get_priv(dev); |
| 210 | u32 mask = 1UL << offset; |
| 211 | |
| 212 | if (!(inl(bank->use_sel) & mask)) |
| 213 | return GPIOF_FUNC; |
| 214 | if (inl(bank->io_sel) & mask) |
| 215 | return GPIOF_INPUT; |
| 216 | else |
| 217 | return GPIOF_OUTPUT; |
| 218 | } |
| 219 | |
| 220 | static const struct dm_gpio_ops gpio_ich6_ops = { |
| 221 | .request = ich6_gpio_request, |
| 222 | .direction_input = ich6_gpio_direction_input, |
| 223 | .direction_output = ich6_gpio_direction_output, |
| 224 | .get_value = ich6_gpio_get_value, |
| 225 | .set_value = ich6_gpio_set_value, |
| 226 | .get_function = ich6_gpio_get_function, |
| 227 | }; |
| 228 | |
| 229 | static const struct udevice_id intel_ich6_gpio_ids[] = { |
| 230 | { .compatible = "intel,ich6-gpio" }, |
| 231 | { } |
| 232 | }; |
| 233 | |
| 234 | U_BOOT_DRIVER(gpio_ich6) = { |
| 235 | .name = "gpio_ich6", |
| 236 | .id = UCLASS_GPIO, |
| 237 | .of_match = intel_ich6_gpio_ids, |
| 238 | .ops = &gpio_ich6_ops, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 239 | .of_to_plat = gpio_ich6_of_to_plat, |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 240 | .probe = ich6_gpio_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 241 | .priv_auto = sizeof(struct ich6_bank_priv), |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 242 | .plat_auto = sizeof(struct ich6_bank_plat), |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 243 | }; |