Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The Chromium OS Authors. |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 3 | * SPDX-License-Identifier: GPL-2.0+ |
Bill Richardson | 55ae10f | 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 | 57be917 | 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 | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | #include <common.h> |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 30 | #include <dm.h> |
| 31 | #include <errno.h> |
| 32 | #include <fdtdec.h> |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 33 | #include <pci.h> |
| 34 | #include <asm/gpio.h> |
| 35 | #include <asm/io.h> |
| 36 | |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 37 | #define GPIO_PER_BANK 32 |
| 38 | |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 39 | /* Where in config space is the register that points to the GPIO registers? */ |
| 40 | #define PCI_CFG_GPIOBASE 0x48 |
| 41 | |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 42 | struct ich6_bank_priv { |
| 43 | /* These are I/O addresses */ |
| 44 | uint32_t use_sel; |
| 45 | uint32_t io_sel; |
| 46 | uint32_t lvl; |
Bill Richardson | 57be917 | 2012-10-20 11:44:36 +0000 | [diff] [blame] | 47 | }; |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 48 | |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 49 | static int gpio_ich6_ofdata_to_platdata(struct udevice *dev) |
Bill Richardson | 57be917 | 2012-10-20 11:44:36 +0000 | [diff] [blame] | 50 | { |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 51 | struct ich6_bank_platdata *plat = dev_get_platdata(dev); |
| 52 | pci_dev_t pci_dev; /* handle for 0:1f:0 */ |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 53 | u8 tmpbyte; |
| 54 | u16 tmpword; |
| 55 | u32 tmplong; |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 56 | u32 gpiobase; |
| 57 | int offset; |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 58 | |
| 59 | /* Where should it be? */ |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 60 | pci_dev = PCI_BDF(0, 0x1f, 0); |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 61 | |
| 62 | /* Is the device present? */ |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 63 | pci_read_config_word(pci_dev, PCI_VENDOR_ID, &tmpword); |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 64 | if (tmpword != PCI_VENDOR_ID_INTEL) { |
| 65 | debug("%s: wrong VendorID\n", __func__); |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 66 | return -ENODEV; |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 67 | } |
Bill Richardson | 57be917 | 2012-10-20 11:44:36 +0000 | [diff] [blame] | 68 | |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 69 | pci_read_config_word(pci_dev, PCI_DEVICE_ID, &tmpword); |
Bill Richardson | 57be917 | 2012-10-20 11:44:36 +0000 | [diff] [blame] | 70 | debug("Found %04x:%04x\n", PCI_VENDOR_ID_INTEL, tmpword); |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 71 | /* |
Bill Richardson | 57be917 | 2012-10-20 11:44:36 +0000 | [diff] [blame] | 72 | * We'd like to validate the Device ID too, but pretty much any |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 73 | * value is either a) correct with slight differences, or b) |
Bill Richardson | 57be917 | 2012-10-20 11:44:36 +0000 | [diff] [blame] | 74 | * correct but undocumented. We'll have to check a bunch of other |
| 75 | * things instead... |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 76 | */ |
| 77 | |
| 78 | /* I/O should already be enabled (it's a RO bit). */ |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 79 | pci_read_config_word(pci_dev, PCI_COMMAND, &tmpword); |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 80 | if (!(tmpword & PCI_COMMAND_IO)) { |
| 81 | debug("%s: device IO not enabled\n", __func__); |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 82 | return -ENODEV; |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /* Header Type must be normal (bits 6-0 only; see spec.) */ |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 86 | pci_read_config_byte(pci_dev, PCI_HEADER_TYPE, &tmpbyte); |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 87 | if ((tmpbyte & 0x7f) != PCI_HEADER_TYPE_NORMAL) { |
| 88 | debug("%s: invalid Header type\n", __func__); |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 89 | return -ENODEV; |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /* Base Class must be a bridge device */ |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 93 | pci_read_config_byte(pci_dev, PCI_CLASS_CODE, &tmpbyte); |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 94 | if (tmpbyte != PCI_CLASS_CODE_BRIDGE) { |
| 95 | debug("%s: invalid class\n", __func__); |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 96 | return -ENODEV; |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 97 | } |
| 98 | /* Sub Class must be ISA */ |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 99 | pci_read_config_byte(pci_dev, PCI_CLASS_SUB_CODE, &tmpbyte); |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 100 | if (tmpbyte != PCI_CLASS_SUB_CODE_BRIDGE_ISA) { |
| 101 | debug("%s: invalid subclass\n", __func__); |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 102 | return -ENODEV; |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* Programming Interface must be 0x00 (no others exist) */ |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 106 | pci_read_config_byte(pci_dev, PCI_CLASS_PROG, &tmpbyte); |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 107 | if (tmpbyte != 0x00) { |
| 108 | debug("%s: invalid interface type\n", __func__); |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 109 | return -ENODEV; |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /* |
| 113 | * GPIOBASE moved to its current offset with ICH6, but prior to |
| 114 | * that it was unused (or undocumented). Check that it looks |
| 115 | * okay: not all ones or zeros, and mapped to I/O space (bit 0). |
| 116 | */ |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 117 | pci_read_config_dword(pci_dev, PCI_CFG_GPIOBASE, &tmplong); |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 118 | if (tmplong == 0x00000000 || tmplong == 0xffffffff || |
| 119 | !(tmplong & 0x00000001)) { |
| 120 | debug("%s: unexpected GPIOBASE value\n", __func__); |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 121 | return -ENODEV; |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Okay, I guess we're looking at the right device. The actual |
| 126 | * GPIO registers are in the PCI device's I/O space, starting |
| 127 | * at the offset that we just read. Bit 0 indicates that it's |
| 128 | * an I/O address, not a memory address, so mask that off. |
| 129 | */ |
| 130 | gpiobase = tmplong & 0xfffffffe; |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 131 | offset = fdtdec_get_int(gd->fdt_blob, dev->of_offset, "reg", -1); |
| 132 | if (offset == -1) { |
| 133 | debug("%s: Invalid register offset %d\n", __func__, offset); |
| 134 | return -EINVAL; |
| 135 | } |
| 136 | plat->base_addr = gpiobase + offset; |
| 137 | plat->bank_name = fdt_getprop(gd->fdt_blob, dev->of_offset, |
| 138 | "bank-name", NULL); |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 139 | |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 140 | return 0; |
| 141 | } |
| 142 | |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 143 | int ich6_gpio_probe(struct udevice *dev) |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 144 | { |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 145 | struct ich6_bank_platdata *plat = dev_get_platdata(dev); |
| 146 | struct gpio_dev_priv *uc_priv = dev->uclass_priv; |
| 147 | struct ich6_bank_priv *bank = dev_get_priv(dev); |
| 148 | |
| 149 | uc_priv->gpio_count = GPIO_PER_BANK; |
| 150 | uc_priv->bank_name = plat->bank_name; |
| 151 | bank->use_sel = plat->base_addr; |
| 152 | bank->io_sel = plat->base_addr + 4; |
| 153 | bank->lvl = plat->base_addr + 8; |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | int ich6_gpio_request(struct udevice *dev, unsigned offset, const char *label) |
| 159 | { |
| 160 | struct ich6_bank_priv *bank = dev_get_priv(dev); |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 161 | u32 tmplong; |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 162 | |
| 163 | /* |
| 164 | * Make sure that the GPIO pin we want isn't already in use for some |
| 165 | * built-in hardware function. We have to check this for every |
| 166 | * requested pin. |
| 167 | */ |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 168 | tmplong = inl(bank->use_sel); |
| 169 | if (!(tmplong & (1UL << offset))) { |
Bill Richardson | 57be917 | 2012-10-20 11:44:36 +0000 | [diff] [blame] | 170 | debug("%s: gpio %d is reserved for internal use\n", __func__, |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 171 | offset); |
| 172 | return -EPERM; |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 175 | return 0; |
| 176 | } |
| 177 | |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 178 | static int ich6_gpio_direction_input(struct udevice *dev, unsigned offset) |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 179 | { |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 180 | struct ich6_bank_priv *bank = dev_get_priv(dev); |
Bill Richardson | 57be917 | 2012-10-20 11:44:36 +0000 | [diff] [blame] | 181 | u32 tmplong; |
Bill Richardson | 57be917 | 2012-10-20 11:44:36 +0000 | [diff] [blame] | 182 | |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 183 | tmplong = inl(bank->io_sel); |
| 184 | tmplong |= (1UL << offset); |
| 185 | outl(bank->io_sel, tmplong); |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 186 | return 0; |
| 187 | } |
| 188 | |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 189 | static int ich6_gpio_direction_output(struct udevice *dev, unsigned offset, |
| 190 | int value) |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 191 | { |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 192 | struct ich6_bank_priv *bank = dev_get_priv(dev); |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 193 | u32 tmplong; |
| 194 | |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 195 | tmplong = inl(bank->io_sel); |
| 196 | tmplong &= ~(1UL << offset); |
| 197 | outl(bank->io_sel, tmplong); |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 201 | static int ich6_gpio_get_value(struct udevice *dev, unsigned offset) |
| 202 | |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 203 | { |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 204 | struct ich6_bank_priv *bank = dev_get_priv(dev); |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 205 | u32 tmplong; |
Bill Richardson | 57be917 | 2012-10-20 11:44:36 +0000 | [diff] [blame] | 206 | int r; |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 207 | |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 208 | tmplong = inl(bank->lvl); |
| 209 | r = (tmplong & (1UL << offset)) ? 1 : 0; |
Bill Richardson | 57be917 | 2012-10-20 11:44:36 +0000 | [diff] [blame] | 210 | return r; |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 213 | static int ich6_gpio_set_value(struct udevice *dev, unsigned offset, |
| 214 | int value) |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 215 | { |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 216 | struct ich6_bank_priv *bank = dev_get_priv(dev); |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 217 | u32 tmplong; |
| 218 | |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 219 | tmplong = inl(bank->lvl); |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 220 | if (value) |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 221 | tmplong |= (1UL << offset); |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 222 | else |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 223 | tmplong &= ~(1UL << offset); |
| 224 | outl(bank->lvl, tmplong); |
Bill Richardson | 55ae10f | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 225 | return 0; |
| 226 | } |
Simon Glass | 7414112 | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 227 | |
| 228 | static int ich6_gpio_get_function(struct udevice *dev, unsigned offset) |
| 229 | { |
| 230 | struct ich6_bank_priv *bank = dev_get_priv(dev); |
| 231 | u32 mask = 1UL << offset; |
| 232 | |
| 233 | if (!(inl(bank->use_sel) & mask)) |
| 234 | return GPIOF_FUNC; |
| 235 | if (inl(bank->io_sel) & mask) |
| 236 | return GPIOF_INPUT; |
| 237 | else |
| 238 | return GPIOF_OUTPUT; |
| 239 | } |
| 240 | |
| 241 | static const struct dm_gpio_ops gpio_ich6_ops = { |
| 242 | .request = ich6_gpio_request, |
| 243 | .direction_input = ich6_gpio_direction_input, |
| 244 | .direction_output = ich6_gpio_direction_output, |
| 245 | .get_value = ich6_gpio_get_value, |
| 246 | .set_value = ich6_gpio_set_value, |
| 247 | .get_function = ich6_gpio_get_function, |
| 248 | }; |
| 249 | |
| 250 | static const struct udevice_id intel_ich6_gpio_ids[] = { |
| 251 | { .compatible = "intel,ich6-gpio" }, |
| 252 | { } |
| 253 | }; |
| 254 | |
| 255 | U_BOOT_DRIVER(gpio_ich6) = { |
| 256 | .name = "gpio_ich6", |
| 257 | .id = UCLASS_GPIO, |
| 258 | .of_match = intel_ich6_gpio_ids, |
| 259 | .ops = &gpio_ich6_ops, |
| 260 | .ofdata_to_platdata = gpio_ich6_ofdata_to_platdata, |
| 261 | .probe = ich6_gpio_probe, |
| 262 | .priv_auto_alloc_size = sizeof(struct ich6_bank_priv), |
| 263 | .platdata_auto_alloc_size = sizeof(struct ich6_bank_platdata), |
| 264 | }; |