Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The Chromium OS Authors. |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 3 | * SPDX-License-Identifier: GPL-2.0+ |
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 | |
| 29 | #include <common.h> |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 30 | #include <dm.h> |
| 31 | #include <errno.h> |
| 32 | #include <fdtdec.h> |
Bin Meng | 6e916cc | 2016-02-01 01:40:47 -0800 | [diff] [blame] | 33 | #include <pch.h> |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 34 | #include <pci.h> |
Simon Glass | fb8757b | 2016-03-11 22:07:14 -0700 | [diff] [blame] | 35 | #include <asm/cpu.h> |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 36 | #include <asm/gpio.h> |
| 37 | #include <asm/io.h> |
Simon Glass | 60af017 | 2014-11-12 22:42:24 -0700 | [diff] [blame] | 38 | #include <asm/pci.h> |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 39 | |
Simon Glass | daa93d9 | 2015-07-31 09:31:31 -0600 | [diff] [blame] | 40 | DECLARE_GLOBAL_DATA_PTR; |
| 41 | |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 42 | #define GPIO_PER_BANK 32 |
| 43 | |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 44 | struct ich6_bank_priv { |
| 45 | /* These are I/O addresses */ |
Bin Meng | 9b64969 | 2014-12-17 15:50:38 +0800 | [diff] [blame] | 46 | uint16_t use_sel; |
| 47 | uint16_t io_sel; |
| 48 | uint16_t lvl; |
Bill Richardson | 50a5ebe | 2012-10-20 11:44:36 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
Gabriel Huau | f813548 | 2015-05-25 22:27:37 -0700 | [diff] [blame] | 51 | #define GPIO_USESEL_OFFSET(x) (x) |
| 52 | #define GPIO_IOSEL_OFFSET(x) (x + 4) |
| 53 | #define GPIO_LVL_OFFSET(x) (x + 8) |
| 54 | |
Gabriel Huau | f813548 | 2015-05-25 22:27:37 -0700 | [diff] [blame] | 55 | static int _ich6_gpio_set_value(uint16_t base, unsigned offset, int value) |
| 56 | { |
| 57 | u32 val; |
| 58 | |
| 59 | val = inl(base); |
| 60 | if (value) |
| 61 | val |= (1UL << offset); |
| 62 | else |
| 63 | val &= ~(1UL << offset); |
| 64 | outl(val, base); |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
Gabriel Huau | f813548 | 2015-05-25 22:27:37 -0700 | [diff] [blame] | 69 | static int _ich6_gpio_set_direction(uint16_t base, unsigned offset, int dir) |
| 70 | { |
| 71 | u32 val; |
| 72 | |
| 73 | if (!dir) { |
| 74 | val = inl(base); |
| 75 | val |= (1UL << offset); |
| 76 | outl(val, base); |
| 77 | } else { |
| 78 | val = inl(base); |
| 79 | val &= ~(1UL << offset); |
| 80 | outl(val, base); |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
Gabriel Huau | f813548 | 2015-05-25 22:27:37 -0700 | [diff] [blame] | 86 | static int gpio_ich6_ofdata_to_platdata(struct udevice *dev) |
| 87 | { |
| 88 | struct ich6_bank_platdata *plat = dev_get_platdata(dev); |
Bin Meng | 6e916cc | 2016-02-01 01:40:47 -0800 | [diff] [blame] | 89 | u32 gpiobase; |
Gabriel Huau | f813548 | 2015-05-25 22:27:37 -0700 | [diff] [blame] | 90 | int offset; |
Bin Meng | 6e916cc | 2016-02-01 01:40:47 -0800 | [diff] [blame] | 91 | int ret; |
| 92 | |
| 93 | ret = pch_get_gpio_base(dev->parent, &gpiobase); |
| 94 | if (ret) |
| 95 | return ret; |
Gabriel Huau | f813548 | 2015-05-25 22:27:37 -0700 | [diff] [blame] | 96 | |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 97 | offset = fdtdec_get_int(gd->fdt_blob, dev->of_offset, "reg", -1); |
| 98 | if (offset == -1) { |
| 99 | debug("%s: Invalid register offset %d\n", __func__, offset); |
| 100 | return -EINVAL; |
| 101 | } |
Simon Glass | ec5c786 | 2016-03-06 19:28:13 -0700 | [diff] [blame] | 102 | plat->offset = offset; |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 103 | plat->base_addr = gpiobase + offset; |
| 104 | plat->bank_name = fdt_getprop(gd->fdt_blob, dev->of_offset, |
| 105 | "bank-name", NULL); |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 106 | |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
Simon Glass | 60af017 | 2014-11-12 22:42:24 -0700 | [diff] [blame] | 110 | static int ich6_gpio_probe(struct udevice *dev) |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 111 | { |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 112 | struct ich6_bank_platdata *plat = dev_get_platdata(dev); |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 113 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 114 | struct ich6_bank_priv *bank = dev_get_priv(dev); |
Bin Meng | a55173f | 2014-12-12 21:05:23 +0800 | [diff] [blame] | 115 | |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 116 | uc_priv->gpio_count = GPIO_PER_BANK; |
| 117 | uc_priv->bank_name = plat->bank_name; |
| 118 | bank->use_sel = plat->base_addr; |
| 119 | bank->io_sel = plat->base_addr + 4; |
| 120 | bank->lvl = plat->base_addr + 8; |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 121 | |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
Simon Glass | 60af017 | 2014-11-12 22:42:24 -0700 | [diff] [blame] | 125 | static int ich6_gpio_request(struct udevice *dev, unsigned offset, |
| 126 | const char *label) |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 127 | { |
| 128 | struct ich6_bank_priv *bank = dev_get_priv(dev); |
| 129 | u32 tmplong; |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 130 | |
| 131 | /* |
| 132 | * Make sure that the GPIO pin we want isn't already in use for some |
| 133 | * built-in hardware function. We have to check this for every |
| 134 | * requested pin. |
| 135 | */ |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 136 | tmplong = inl(bank->use_sel); |
| 137 | if (!(tmplong & (1UL << offset))) { |
Bill Richardson | 50a5ebe | 2012-10-20 11:44:36 +0000 | [diff] [blame] | 138 | debug("%s: gpio %d is reserved for internal use\n", __func__, |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 139 | offset); |
| 140 | return -EPERM; |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 143 | return 0; |
| 144 | } |
| 145 | |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 146 | static int ich6_gpio_direction_input(struct udevice *dev, unsigned offset) |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 147 | { |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 148 | struct ich6_bank_priv *bank = dev_get_priv(dev); |
Bill Richardson | 50a5ebe | 2012-10-20 11:44:36 +0000 | [diff] [blame] | 149 | |
Simon Glass | 94641cb | 2015-08-22 15:58:58 -0600 | [diff] [blame] | 150 | return _ich6_gpio_set_direction(bank->io_sel, offset, 0); |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 153 | static int ich6_gpio_direction_output(struct udevice *dev, unsigned offset, |
| 154 | int value) |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 155 | { |
Gabriel Huau | f813548 | 2015-05-25 22:27:37 -0700 | [diff] [blame] | 156 | int ret; |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 157 | struct ich6_bank_priv *bank = dev_get_priv(dev); |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 158 | |
Simon Glass | 94641cb | 2015-08-22 15:58:58 -0600 | [diff] [blame] | 159 | ret = _ich6_gpio_set_direction(bank->io_sel, offset, 1); |
Gabriel Huau | f813548 | 2015-05-25 22:27:37 -0700 | [diff] [blame] | 160 | if (ret) |
| 161 | return ret; |
Axel Lin | 6df0e9e | 2014-12-07 12:48:27 +0800 | [diff] [blame] | 162 | |
Gabriel Huau | f813548 | 2015-05-25 22:27:37 -0700 | [diff] [blame] | 163 | return _ich6_gpio_set_value(bank->lvl, offset, value); |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 166 | static int ich6_gpio_get_value(struct udevice *dev, unsigned offset) |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 167 | { |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 168 | struct ich6_bank_priv *bank = dev_get_priv(dev); |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 169 | u32 tmplong; |
Bill Richardson | 50a5ebe | 2012-10-20 11:44:36 +0000 | [diff] [blame] | 170 | int r; |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 171 | |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 172 | tmplong = inl(bank->lvl); |
| 173 | r = (tmplong & (1UL << offset)) ? 1 : 0; |
Bill Richardson | 50a5ebe | 2012-10-20 11:44:36 +0000 | [diff] [blame] | 174 | return r; |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 177 | static int ich6_gpio_set_value(struct udevice *dev, unsigned offset, |
| 178 | int value) |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 179 | { |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 180 | struct ich6_bank_priv *bank = dev_get_priv(dev); |
Gabriel Huau | f813548 | 2015-05-25 22:27:37 -0700 | [diff] [blame] | 181 | return _ich6_gpio_set_value(bank->lvl, offset, value); |
Bill Richardson | eece432 | 2012-10-20 11:44:34 +0000 | [diff] [blame] | 182 | } |
Simon Glass | 2b4071b | 2014-10-10 07:49:18 -0600 | [diff] [blame] | 183 | |
| 184 | static int ich6_gpio_get_function(struct udevice *dev, unsigned offset) |
| 185 | { |
| 186 | struct ich6_bank_priv *bank = dev_get_priv(dev); |
| 187 | u32 mask = 1UL << offset; |
| 188 | |
| 189 | if (!(inl(bank->use_sel) & mask)) |
| 190 | return GPIOF_FUNC; |
| 191 | if (inl(bank->io_sel) & mask) |
| 192 | return GPIOF_INPUT; |
| 193 | else |
| 194 | return GPIOF_OUTPUT; |
| 195 | } |
| 196 | |
| 197 | static const struct dm_gpio_ops gpio_ich6_ops = { |
| 198 | .request = ich6_gpio_request, |
| 199 | .direction_input = ich6_gpio_direction_input, |
| 200 | .direction_output = ich6_gpio_direction_output, |
| 201 | .get_value = ich6_gpio_get_value, |
| 202 | .set_value = ich6_gpio_set_value, |
| 203 | .get_function = ich6_gpio_get_function, |
| 204 | }; |
| 205 | |
| 206 | static const struct udevice_id intel_ich6_gpio_ids[] = { |
| 207 | { .compatible = "intel,ich6-gpio" }, |
| 208 | { } |
| 209 | }; |
| 210 | |
| 211 | U_BOOT_DRIVER(gpio_ich6) = { |
| 212 | .name = "gpio_ich6", |
| 213 | .id = UCLASS_GPIO, |
| 214 | .of_match = intel_ich6_gpio_ids, |
| 215 | .ops = &gpio_ich6_ops, |
| 216 | .ofdata_to_platdata = gpio_ich6_ofdata_to_platdata, |
| 217 | .probe = ich6_gpio_probe, |
| 218 | .priv_auto_alloc_size = sizeof(struct ich6_bank_priv), |
| 219 | .platdata_auto_alloc_size = sizeof(struct ich6_bank_platdata), |
| 220 | }; |