2 * Copyright (c) 2012 The Chromium OS Authors.
3 * See file CREDITS for list of people who contributed to this
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * This is a GPIO driver for Intel ICH6 and later. The x86 GPIOs are accessed
24 * through the PCI bus. Each PCI device has 256 bytes of configuration space,
25 * consisting of a standard header and a device-specific set of registers. PCI
26 * bus 0, device 31, function 0 gives us access to the chipset GPIOs (among
27 * other things). Within the PCI configuration space, the GPIOBASE register
28 * tells us where in the device's I/O region we can find more registers to
29 * actually access the GPIOs.
31 * PCI bus/device/function 0:1f:0 => PCI config registers
32 * PCI config register "GPIOBASE"
33 * PCI I/O space + [GPIOBASE] => start of GPIO registers
34 * GPIO registers => gpio pin function, direction, value
42 /* Where in config space is the register that points to the GPIO registers? */
43 #define PCI_CFG_GPIOBASE 0x48
46 * There are often more than 32 GPIOs, depending on the ICH version.
47 * For now, we just support bank 0 because it's the same for all.
51 /* Within the I/O space, where are the registers to control the GPIOs? */
52 #define OFS_GPIO_USE_SEL 0x00
53 #define OFS_GPIO_IO_SEL 0x04
54 #define OFS_GP_LVL 0x0C
56 static pci_dev_t dev; /* handle for 0:1f:0 */
57 static u32 gpiobase; /* offset into I/O space */
58 static int found_it_once; /* valid GPIO device? */
59 static int in_use[GPIO_MAX]; /* "lock" for access to pins */
61 static int gpio_init(void)
67 /* Have we already done this? */
71 /* Where should it be? */
72 dev = PCI_BDF(0, 0x1f, 0);
74 /* Is the device present? */
75 pci_read_config_word(dev, PCI_VENDOR_ID, &tmpword);
76 if (tmpword != PCI_VENDOR_ID_INTEL) {
77 debug("%s: wrong VendorID\n", __func__);
81 * We'd like to check the Device ID too, but pretty much any
82 * value is either a) correct with slight differences, or b)
83 * correct but undocumented. We'll have to check other things
87 /* I/O should already be enabled (it's a RO bit). */
88 pci_read_config_word(dev, PCI_COMMAND, &tmpword);
89 if (!(tmpword & PCI_COMMAND_IO)) {
90 debug("%s: device IO not enabled\n", __func__);
94 /* Header Type must be normal (bits 6-0 only; see spec.) */
95 pci_read_config_byte(dev, PCI_HEADER_TYPE, &tmpbyte);
96 if ((tmpbyte & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
97 debug("%s: invalid Header type\n", __func__);
101 /* Base Class must be a bridge device */
102 pci_read_config_byte(dev, PCI_CLASS_CODE, &tmpbyte);
103 if (tmpbyte != PCI_CLASS_CODE_BRIDGE) {
104 debug("%s: invalid class\n", __func__);
107 /* Sub Class must be ISA */
108 pci_read_config_byte(dev, PCI_CLASS_SUB_CODE, &tmpbyte);
109 if (tmpbyte != PCI_CLASS_SUB_CODE_BRIDGE_ISA) {
110 debug("%s: invalid subclass\n", __func__);
114 /* Programming Interface must be 0x00 (no others exist) */
115 pci_read_config_byte(dev, PCI_CLASS_PROG, &tmpbyte);
116 if (tmpbyte != 0x00) {
117 debug("%s: invalid interface type\n", __func__);
122 * GPIOBASE moved to its current offset with ICH6, but prior to
123 * that it was unused (or undocumented). Check that it looks
124 * okay: not all ones or zeros, and mapped to I/O space (bit 0).
126 pci_read_config_dword(dev, PCI_CFG_GPIOBASE, &tmplong);
127 if (tmplong == 0x00000000 || tmplong == 0xffffffff ||
128 !(tmplong & 0x00000001)) {
129 debug("%s: unexpected GPIOBASE value\n", __func__);
134 * Okay, I guess we're looking at the right device. The actual
135 * GPIO registers are in the PCI device's I/O space, starting
136 * at the offset that we just read. Bit 0 indicates that it's
137 * an I/O address, not a memory address, so mask that off.
139 gpiobase = tmplong & 0xfffffffe;
141 /* Finally. These are the droids we're looking for. */
146 int gpio_request(unsigned gpio, const char *label /* UNUSED */)
150 /* Are we doing it wrong? */
151 if (gpio > GPIO_MAX || in_use[gpio]) {
152 debug("%s: gpio unavailable\n", __func__);
156 /* Is the hardware ready? */
158 debug("%s: gpio_init failed\n", __func__);
163 * Make sure that the GPIO pin we want isn't already in use for some
164 * built-in hardware function. We have to check this for every
167 tmplong = inl(gpiobase + OFS_GPIO_USE_SEL);
168 if (!(tmplong & (1UL << gpio))) {
169 debug("%s: reserved for internal use\n", __func__);
177 int gpio_free(unsigned gpio)
179 if (gpio > GPIO_MAX || !in_use[gpio]) {
180 debug("%s: gpio unavailable\n", __func__);
187 int gpio_direction_input(unsigned gpio)
191 if (gpio > GPIO_MAX || !in_use[gpio]) {
192 debug("%s: gpio unavailable\n", __func__);
195 tmplong = inl(gpiobase + OFS_GPIO_IO_SEL);
196 tmplong |= (1UL << gpio);
197 outl(gpiobase + OFS_GPIO_IO_SEL, tmplong);
201 int gpio_direction_output(unsigned gpio, int value)
205 if (gpio > GPIO_MAX || !in_use[gpio]) {
206 debug("%s: gpio unavailable\n", __func__);
209 tmplong = inl(gpiobase + OFS_GPIO_IO_SEL);
210 tmplong &= ~(1UL << gpio);
211 outl(gpiobase + OFS_GPIO_IO_SEL, tmplong);
215 int gpio_get_value(unsigned gpio)
219 if (gpio > GPIO_MAX || !in_use[gpio]) {
220 debug("%s: gpio unavailable\n", __func__);
223 tmplong = inl(gpiobase + OFS_GP_LVL);
224 return (tmplong & (1UL << gpio)) ? 1 : 0;
227 int gpio_set_value(unsigned gpio, int value)
231 if (gpio > GPIO_MAX || !in_use[gpio]) {
232 debug("%s: gpio unavailable\n", __func__);
235 tmplong = inl(gpiobase + OFS_GP_LVL);
237 tmplong |= (1UL << gpio);
239 tmplong &= ~(1UL << gpio);
240 outl(gpiobase + OFS_GP_LVL, tmplong);