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
37 * Danger Will Robinson! Bank 0 (GPIOs 0-31) seems to be fairly stable. Most
38 * ICH versions have more, but the decoding the matrix that describes them is
39 * absurdly complex and constantly changing. We'll provide Bank 1 and Bank 2,
40 * but they will ONLY work for certain unspecified chipsets because the offset
41 * from GPIOBASE changes randomly. Even then, many GPIOs are unimplemented or
42 * reserved or subject to arcane restrictions.
50 /* Where in config space is the register that points to the GPIO registers? */
51 #define PCI_CFG_GPIOBASE 0x48
55 /* Within the I/O space, where are the registers to control the GPIOs? */
60 } gpio_bank[NUM_BANKS] = {
61 { 0x00, 0x04, 0x0c }, /* Bank 0 */
62 { 0x30, 0x34, 0x38 }, /* Bank 1 */
63 { 0x40, 0x44, 0x48 } /* Bank 2 */
66 static pci_dev_t dev; /* handle for 0:1f:0 */
67 static u32 gpiobase; /* offset into I/O space */
68 static int found_it_once; /* valid GPIO device? */
69 static u32 lock[NUM_BANKS]; /* "lock" for access to pins */
71 static int bad_arg(int num, int *bank, int *bitnum)
76 if (num < 0 || i > NUM_BANKS) {
77 debug("%s: bogus gpio num: %d\n", __func__, num);
85 static int mark_gpio(int bank, int bitnum)
87 if (lock[bank] & (1UL << bitnum)) {
88 debug("%s: %d.%d already marked\n", __func__, bank, bitnum);
91 lock[bank] |= (1 << bitnum);
95 static void clear_gpio(int bank, int bitnum)
97 lock[bank] &= ~(1 << bitnum);
100 static int notmine(int num, int *bank, int *bitnum)
102 if (bad_arg(num, bank, bitnum))
104 return !(lock[*bank] & (1UL << *bitnum));
107 static int gpio_init(void)
113 /* Have we already done this? */
117 /* Where should it be? */
118 dev = PCI_BDF(0, 0x1f, 0);
120 /* Is the device present? */
121 pci_read_config_word(dev, PCI_VENDOR_ID, &tmpword);
122 if (tmpword != PCI_VENDOR_ID_INTEL) {
123 debug("%s: wrong VendorID\n", __func__);
127 pci_read_config_word(dev, PCI_DEVICE_ID, &tmpword);
128 debug("Found %04x:%04x\n", PCI_VENDOR_ID_INTEL, tmpword);
130 * We'd like to validate the Device ID too, but pretty much any
131 * value is either a) correct with slight differences, or b)
132 * correct but undocumented. We'll have to check a bunch of other
136 /* I/O should already be enabled (it's a RO bit). */
137 pci_read_config_word(dev, PCI_COMMAND, &tmpword);
138 if (!(tmpword & PCI_COMMAND_IO)) {
139 debug("%s: device IO not enabled\n", __func__);
143 /* Header Type must be normal (bits 6-0 only; see spec.) */
144 pci_read_config_byte(dev, PCI_HEADER_TYPE, &tmpbyte);
145 if ((tmpbyte & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
146 debug("%s: invalid Header type\n", __func__);
150 /* Base Class must be a bridge device */
151 pci_read_config_byte(dev, PCI_CLASS_CODE, &tmpbyte);
152 if (tmpbyte != PCI_CLASS_CODE_BRIDGE) {
153 debug("%s: invalid class\n", __func__);
156 /* Sub Class must be ISA */
157 pci_read_config_byte(dev, PCI_CLASS_SUB_CODE, &tmpbyte);
158 if (tmpbyte != PCI_CLASS_SUB_CODE_BRIDGE_ISA) {
159 debug("%s: invalid subclass\n", __func__);
163 /* Programming Interface must be 0x00 (no others exist) */
164 pci_read_config_byte(dev, PCI_CLASS_PROG, &tmpbyte);
165 if (tmpbyte != 0x00) {
166 debug("%s: invalid interface type\n", __func__);
171 * GPIOBASE moved to its current offset with ICH6, but prior to
172 * that it was unused (or undocumented). Check that it looks
173 * okay: not all ones or zeros, and mapped to I/O space (bit 0).
175 pci_read_config_dword(dev, PCI_CFG_GPIOBASE, &tmplong);
176 if (tmplong == 0x00000000 || tmplong == 0xffffffff ||
177 !(tmplong & 0x00000001)) {
178 debug("%s: unexpected GPIOBASE value\n", __func__);
183 * Okay, I guess we're looking at the right device. The actual
184 * GPIO registers are in the PCI device's I/O space, starting
185 * at the offset that we just read. Bit 0 indicates that it's
186 * an I/O address, not a memory address, so mask that off.
188 gpiobase = tmplong & 0xfffffffe;
190 /* Finally. These are the droids we're looking for. */
195 int gpio_request(unsigned num, const char *label /* UNUSED */)
200 /* Is the hardware ready? */
204 if (bad_arg(num, &i, &j))
208 * Make sure that the GPIO pin we want isn't already in use for some
209 * built-in hardware function. We have to check this for every
212 tmplong = inl(gpiobase + gpio_bank[i].use_sel);
213 if (!(tmplong & (1UL << j))) {
214 debug("%s: gpio %d is reserved for internal use\n", __func__,
219 return mark_gpio(i, j);
222 int gpio_free(unsigned num)
226 if (notmine(num, &i, &j))
233 int gpio_direction_input(unsigned num)
238 if (notmine(num, &i, &j))
241 tmplong = inl(gpiobase + gpio_bank[i].io_sel);
242 tmplong |= (1UL << j);
243 outl(gpiobase + gpio_bank[i].io_sel, tmplong);
247 int gpio_direction_output(unsigned num, int value)
252 if (notmine(num, &i, &j))
255 tmplong = inl(gpiobase + gpio_bank[i].io_sel);
256 tmplong &= ~(1UL << j);
257 outl(gpiobase + gpio_bank[i].io_sel, tmplong);
261 int gpio_get_value(unsigned num)
267 if (notmine(num, &i, &j))
270 tmplong = inl(gpiobase + gpio_bank[i].lvl);
271 r = (tmplong & (1UL << j)) ? 1 : 0;
275 int gpio_set_value(unsigned num, int value)
280 if (notmine(num, &i, &j))
283 tmplong = inl(gpiobase + gpio_bank[i].lvl);
285 tmplong |= (1UL << j);
287 tmplong &= ~(1UL << j);
288 outl(gpiobase + gpio_bank[i].lvl, tmplong);