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.
37 #define GPIO_PER_BANK 32
39 /* Where in config space is the register that points to the GPIO registers? */
40 #define PCI_CFG_GPIOBASE 0x48
42 struct ich6_bank_priv
{
43 /* These are I/O addresses */
49 static int gpio_ich6_ofdata_to_platdata(struct udevice
*dev
)
51 struct ich6_bank_platdata
*plat
= dev_get_platdata(dev
);
52 pci_dev_t pci_dev
; /* handle for 0:1f:0 */
59 /* Where should it be? */
60 pci_dev
= PCI_BDF(0, 0x1f, 0);
62 /* Is the device present? */
63 pci_read_config_word(pci_dev
, PCI_VENDOR_ID
, &tmpword
);
64 if (tmpword
!= PCI_VENDOR_ID_INTEL
) {
65 debug("%s: wrong VendorID\n", __func__
);
69 pci_read_config_word(pci_dev
, PCI_DEVICE_ID
, &tmpword
);
70 debug("Found %04x:%04x\n", PCI_VENDOR_ID_INTEL
, tmpword
);
72 * We'd like to validate the Device ID too, but pretty much any
73 * value is either a) correct with slight differences, or b)
74 * correct but undocumented. We'll have to check a bunch of other
78 /* I/O should already be enabled (it's a RO bit). */
79 pci_read_config_word(pci_dev
, PCI_COMMAND
, &tmpword
);
80 if (!(tmpword
& PCI_COMMAND_IO
)) {
81 debug("%s: device IO not enabled\n", __func__
);
85 /* Header Type must be normal (bits 6-0 only; see spec.) */
86 pci_read_config_byte(pci_dev
, PCI_HEADER_TYPE
, &tmpbyte
);
87 if ((tmpbyte
& 0x7f) != PCI_HEADER_TYPE_NORMAL
) {
88 debug("%s: invalid Header type\n", __func__
);
92 /* Base Class must be a bridge device */
93 pci_read_config_byte(pci_dev
, PCI_CLASS_CODE
, &tmpbyte
);
94 if (tmpbyte
!= PCI_CLASS_CODE_BRIDGE
) {
95 debug("%s: invalid class\n", __func__
);
98 /* Sub Class must be ISA */
99 pci_read_config_byte(pci_dev
, PCI_CLASS_SUB_CODE
, &tmpbyte
);
100 if (tmpbyte
!= PCI_CLASS_SUB_CODE_BRIDGE_ISA
) {
101 debug("%s: invalid subclass\n", __func__
);
105 /* Programming Interface must be 0x00 (no others exist) */
106 pci_read_config_byte(pci_dev
, PCI_CLASS_PROG
, &tmpbyte
);
107 if (tmpbyte
!= 0x00) {
108 debug("%s: invalid interface type\n", __func__
);
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).
117 pci_read_config_dword(pci_dev
, PCI_CFG_GPIOBASE
, &tmplong
);
118 if (tmplong
== 0x00000000 || tmplong
== 0xffffffff ||
119 !(tmplong
& 0x00000001)) {
120 debug("%s: unexpected GPIOBASE value\n", __func__
);
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.
130 gpiobase
= tmplong
& 0xfffffffe;
131 offset
= fdtdec_get_int(gd
->fdt_blob
, dev
->of_offset
, "reg", -1);
133 debug("%s: Invalid register offset %d\n", __func__
, offset
);
136 plat
->base_addr
= gpiobase
+ offset
;
137 plat
->bank_name
= fdt_getprop(gd
->fdt_blob
, dev
->of_offset
,
143 int ich6_gpio_probe(struct udevice
*dev
)
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
);
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;
158 int ich6_gpio_request(struct udevice
*dev
, unsigned offset
, const char *label
)
160 struct ich6_bank_priv
*bank
= dev_get_priv(dev
);
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
168 tmplong
= inl(bank
->use_sel
);
169 if (!(tmplong
& (1UL << offset
))) {
170 debug("%s: gpio %d is reserved for internal use\n", __func__
,
178 static int ich6_gpio_direction_input(struct udevice
*dev
, unsigned offset
)
180 struct ich6_bank_priv
*bank
= dev_get_priv(dev
);
183 tmplong
= inl(bank
->io_sel
);
184 tmplong
|= (1UL << offset
);
185 outl(bank
->io_sel
, tmplong
);
189 static int ich6_gpio_direction_output(struct udevice
*dev
, unsigned offset
,
192 struct ich6_bank_priv
*bank
= dev_get_priv(dev
);
195 tmplong
= inl(bank
->io_sel
);
196 tmplong
&= ~(1UL << offset
);
197 outl(bank
->io_sel
, tmplong
);
201 static int ich6_gpio_get_value(struct udevice
*dev
, unsigned offset
)
204 struct ich6_bank_priv
*bank
= dev_get_priv(dev
);
208 tmplong
= inl(bank
->lvl
);
209 r
= (tmplong
& (1UL << offset
)) ? 1 : 0;
213 static int ich6_gpio_set_value(struct udevice
*dev
, unsigned offset
,
216 struct ich6_bank_priv
*bank
= dev_get_priv(dev
);
219 tmplong
= inl(bank
->lvl
);
221 tmplong
|= (1UL << offset
);
223 tmplong
&= ~(1UL << offset
);
224 outl(bank
->lvl
, tmplong
);
228 static int ich6_gpio_get_function(struct udevice
*dev
, unsigned offset
)
230 struct ich6_bank_priv
*bank
= dev_get_priv(dev
);
231 u32 mask
= 1UL << offset
;
233 if (!(inl(bank
->use_sel
) & mask
))
235 if (inl(bank
->io_sel
) & mask
)
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
,
250 static const struct udevice_id intel_ich6_gpio_ids
[] = {
251 { .compatible
= "intel,ich6-gpio" },
255 U_BOOT_DRIVER(gpio_ich6
) = {
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
),