1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2012 The Chromium OS Authors.
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.
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
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.
37 #include <asm/global_data.h>
42 DECLARE_GLOBAL_DATA_PTR;
44 #define GPIO_PER_BANK 32
46 struct ich6_bank_priv {
47 /* These are I/O addresses */
52 bool use_lvl_write_cache;
55 #define GPIO_USESEL_OFFSET(x) (x)
56 #define GPIO_IOSEL_OFFSET(x) (x + 4)
57 #define GPIO_LVL_OFFSET(x) (x + 8)
59 static int _ich6_gpio_set_value(struct ich6_bank_priv *bank, unsigned offset,
64 if (bank->use_lvl_write_cache)
65 val = bank->lvl_write_cache;
70 val |= (1UL << offset);
72 val &= ~(1UL << offset);
74 if (bank->use_lvl_write_cache)
75 bank->lvl_write_cache = val;
80 static int _ich6_gpio_set_direction(uint16_t base, unsigned offset, int dir)
86 val |= (1UL << offset);
90 val &= ~(1UL << offset);
97 static int gpio_ich6_of_to_plat(struct udevice *dev)
99 struct ich6_bank_plat *plat = dev_get_plat(dev);
104 ret = pch_get_gpio_base(dev->parent, &gpiobase);
108 offset = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "reg", -1);
110 debug("%s: Invalid register offset %d\n", __func__, offset);
113 plat->offset = offset;
114 plat->base_addr = gpiobase + offset;
115 plat->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
121 static int ich6_gpio_probe(struct udevice *dev)
123 struct ich6_bank_plat *plat = dev_get_plat(dev);
124 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
125 struct ich6_bank_priv *bank = dev_get_priv(dev);
128 uc_priv->gpio_count = GPIO_PER_BANK;
129 uc_priv->bank_name = plat->bank_name;
130 bank->use_sel = plat->base_addr;
131 bank->io_sel = plat->base_addr + 4;
132 bank->lvl = plat->base_addr + 8;
134 prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
135 "use-lvl-write-cache", NULL);
137 bank->use_lvl_write_cache = true;
139 bank->use_lvl_write_cache = false;
140 bank->lvl_write_cache = 0;
145 static int ich6_gpio_request(struct udevice *dev, unsigned offset,
148 struct ich6_bank_priv *bank = dev_get_priv(dev);
152 * Make sure that the GPIO pin we want isn't already in use for some
153 * built-in hardware function. We have to check this for every
156 tmplong = inl(bank->use_sel);
157 if (!(tmplong & (1UL << offset))) {
158 debug("%s: gpio %d is reserved for internal use\n", __func__,
166 static int ich6_gpio_direction_input(struct udevice *dev, unsigned offset)
168 struct ich6_bank_priv *bank = dev_get_priv(dev);
170 return _ich6_gpio_set_direction(bank->io_sel, offset, 0);
173 static int ich6_gpio_direction_output(struct udevice *dev, unsigned offset,
177 struct ich6_bank_priv *bank = dev_get_priv(dev);
179 ret = _ich6_gpio_set_direction(bank->io_sel, offset, 1);
183 return _ich6_gpio_set_value(bank, offset, value);
186 static int ich6_gpio_get_value(struct udevice *dev, unsigned offset)
188 struct ich6_bank_priv *bank = dev_get_priv(dev);
192 tmplong = inl(bank->lvl);
193 if (bank->use_lvl_write_cache)
194 tmplong |= bank->lvl_write_cache;
195 r = (tmplong & (1UL << offset)) ? 1 : 0;
199 static int ich6_gpio_set_value(struct udevice *dev, unsigned offset,
202 struct ich6_bank_priv *bank = dev_get_priv(dev);
203 return _ich6_gpio_set_value(bank, offset, value);
206 static int ich6_gpio_get_function(struct udevice *dev, unsigned offset)
208 struct ich6_bank_priv *bank = dev_get_priv(dev);
209 u32 mask = 1UL << offset;
211 if (!(inl(bank->use_sel) & mask))
213 if (inl(bank->io_sel) & mask)
219 static const struct dm_gpio_ops gpio_ich6_ops = {
220 .request = ich6_gpio_request,
221 .direction_input = ich6_gpio_direction_input,
222 .direction_output = ich6_gpio_direction_output,
223 .get_value = ich6_gpio_get_value,
224 .set_value = ich6_gpio_set_value,
225 .get_function = ich6_gpio_get_function,
228 static const struct udevice_id intel_ich6_gpio_ids[] = {
229 { .compatible = "intel,ich6-gpio" },
233 U_BOOT_DRIVER(gpio_ich6) = {
236 .of_match = intel_ich6_gpio_ids,
237 .ops = &gpio_ich6_ops,
238 .of_to_plat = gpio_ich6_of_to_plat,
239 .probe = ich6_gpio_probe,
240 .priv_auto = sizeof(struct ich6_bank_priv),
241 .plat_auto = sizeof(struct ich6_bank_plat),