2 * Copyright (c) 2012 The Chromium OS Authors.
3 * SPDX-License-Identifier: GPL-2.0+
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.
40 DECLARE_GLOBAL_DATA_PTR;
42 #define GPIO_PER_BANK 32
44 struct ich6_bank_priv {
45 /* These are I/O addresses */
51 #define GPIO_USESEL_OFFSET(x) (x)
52 #define GPIO_IOSEL_OFFSET(x) (x + 4)
53 #define GPIO_LVL_OFFSET(x) (x + 8)
55 static int _ich6_gpio_set_value(uint16_t base, unsigned offset, int value)
61 val |= (1UL << offset);
63 val &= ~(1UL << offset);
69 static int _ich6_gpio_set_direction(uint16_t base, unsigned offset, int dir)
75 val |= (1UL << offset);
79 val &= ~(1UL << offset);
86 static int gpio_ich6_ofdata_to_platdata(struct udevice *dev)
88 struct ich6_bank_platdata *plat = dev_get_platdata(dev);
93 ret = pch_get_gpio_base(dev->parent, &gpiobase);
97 offset = fdtdec_get_int(gd->fdt_blob, dev->of_offset, "reg", -1);
99 debug("%s: Invalid register offset %d\n", __func__, offset);
102 plat->offset = offset;
103 plat->base_addr = gpiobase + offset;
104 plat->bank_name = fdt_getprop(gd->fdt_blob, dev->of_offset,
110 static int ich6_gpio_probe(struct udevice *dev)
112 struct ich6_bank_platdata *plat = dev_get_platdata(dev);
113 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
114 struct ich6_bank_priv *bank = dev_get_priv(dev);
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;
125 static int ich6_gpio_request(struct udevice *dev, unsigned offset,
128 struct ich6_bank_priv *bank = dev_get_priv(dev);
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
136 tmplong = inl(bank->use_sel);
137 if (!(tmplong & (1UL << offset))) {
138 debug("%s: gpio %d is reserved for internal use\n", __func__,
146 static int ich6_gpio_direction_input(struct udevice *dev, unsigned offset)
148 struct ich6_bank_priv *bank = dev_get_priv(dev);
150 return _ich6_gpio_set_direction(bank->io_sel, offset, 0);
153 static int ich6_gpio_direction_output(struct udevice *dev, unsigned offset,
157 struct ich6_bank_priv *bank = dev_get_priv(dev);
159 ret = _ich6_gpio_set_direction(bank->io_sel, offset, 1);
163 return _ich6_gpio_set_value(bank->lvl, offset, value);
166 static int ich6_gpio_get_value(struct udevice *dev, unsigned offset)
168 struct ich6_bank_priv *bank = dev_get_priv(dev);
172 tmplong = inl(bank->lvl);
173 r = (tmplong & (1UL << offset)) ? 1 : 0;
177 static int ich6_gpio_set_value(struct udevice *dev, unsigned offset,
180 struct ich6_bank_priv *bank = dev_get_priv(dev);
181 return _ich6_gpio_set_value(bank->lvl, offset, value);
184 static int ich6_gpio_get_function(struct udevice *dev, unsigned offset)
186 struct ich6_bank_priv *bank = dev_get_priv(dev);
187 u32 mask = 1UL << offset;
189 if (!(inl(bank->use_sel) & mask))
191 if (inl(bank->io_sel) & mask)
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,
206 static const struct udevice_id intel_ich6_gpio_ids[] = {
207 { .compatible = "intel,ich6-gpio" },
211 U_BOOT_DRIVER(gpio_ich6) = {
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),