blob: d3381b0369c1a4122f30c0e80aecd154d4e4e896 [file] [log] [blame]
Bill Richardson55ae10f2012-10-20 11:44:34 +00001/*
2 * Copyright (c) 2012 The Chromium OS Authors.
Wolfgang Denk1a459662013-07-08 09:37:19 +02003 * SPDX-License-Identifier: GPL-2.0+
Bill Richardson55ae10f2012-10-20 11:44:34 +00004 */
5
6/*
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.
14 *
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
Bill Richardson57be9172012-10-20 11:44:36 +000019 *
20 *
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.
Bill Richardson55ae10f2012-10-20 11:44:34 +000027 */
28
29#include <common.h>
Simon Glass74141122014-10-10 07:49:18 -060030#include <dm.h>
31#include <errno.h>
32#include <fdtdec.h>
Bill Richardson55ae10f2012-10-20 11:44:34 +000033#include <pci.h>
34#include <asm/gpio.h>
35#include <asm/io.h>
36
Simon Glass74141122014-10-10 07:49:18 -060037#define GPIO_PER_BANK 32
38
Bill Richardson55ae10f2012-10-20 11:44:34 +000039/* Where in config space is the register that points to the GPIO registers? */
40#define PCI_CFG_GPIOBASE 0x48
41
Simon Glass74141122014-10-10 07:49:18 -060042struct ich6_bank_priv {
43 /* These are I/O addresses */
44 uint32_t use_sel;
45 uint32_t io_sel;
46 uint32_t lvl;
Bill Richardson57be9172012-10-20 11:44:36 +000047};
Bill Richardson55ae10f2012-10-20 11:44:34 +000048
Simon Glass74141122014-10-10 07:49:18 -060049static int gpio_ich6_ofdata_to_platdata(struct udevice *dev)
Bill Richardson57be9172012-10-20 11:44:36 +000050{
Simon Glass74141122014-10-10 07:49:18 -060051 struct ich6_bank_platdata *plat = dev_get_platdata(dev);
52 pci_dev_t pci_dev; /* handle for 0:1f:0 */
Bill Richardson55ae10f2012-10-20 11:44:34 +000053 u8 tmpbyte;
54 u16 tmpword;
55 u32 tmplong;
Simon Glass74141122014-10-10 07:49:18 -060056 u32 gpiobase;
57 int offset;
Bill Richardson55ae10f2012-10-20 11:44:34 +000058
59 /* Where should it be? */
Simon Glass74141122014-10-10 07:49:18 -060060 pci_dev = PCI_BDF(0, 0x1f, 0);
Bill Richardson55ae10f2012-10-20 11:44:34 +000061
62 /* Is the device present? */
Simon Glass74141122014-10-10 07:49:18 -060063 pci_read_config_word(pci_dev, PCI_VENDOR_ID, &tmpword);
Bill Richardson55ae10f2012-10-20 11:44:34 +000064 if (tmpword != PCI_VENDOR_ID_INTEL) {
65 debug("%s: wrong VendorID\n", __func__);
Simon Glass74141122014-10-10 07:49:18 -060066 return -ENODEV;
Bill Richardson55ae10f2012-10-20 11:44:34 +000067 }
Bill Richardson57be9172012-10-20 11:44:36 +000068
Simon Glass74141122014-10-10 07:49:18 -060069 pci_read_config_word(pci_dev, PCI_DEVICE_ID, &tmpword);
Bill Richardson57be9172012-10-20 11:44:36 +000070 debug("Found %04x:%04x\n", PCI_VENDOR_ID_INTEL, tmpword);
Bill Richardson55ae10f2012-10-20 11:44:34 +000071 /*
Bill Richardson57be9172012-10-20 11:44:36 +000072 * We'd like to validate the Device ID too, but pretty much any
Bill Richardson55ae10f2012-10-20 11:44:34 +000073 * value is either a) correct with slight differences, or b)
Bill Richardson57be9172012-10-20 11:44:36 +000074 * correct but undocumented. We'll have to check a bunch of other
75 * things instead...
Bill Richardson55ae10f2012-10-20 11:44:34 +000076 */
77
78 /* I/O should already be enabled (it's a RO bit). */
Simon Glass74141122014-10-10 07:49:18 -060079 pci_read_config_word(pci_dev, PCI_COMMAND, &tmpword);
Bill Richardson55ae10f2012-10-20 11:44:34 +000080 if (!(tmpword & PCI_COMMAND_IO)) {
81 debug("%s: device IO not enabled\n", __func__);
Simon Glass74141122014-10-10 07:49:18 -060082 return -ENODEV;
Bill Richardson55ae10f2012-10-20 11:44:34 +000083 }
84
85 /* Header Type must be normal (bits 6-0 only; see spec.) */
Simon Glass74141122014-10-10 07:49:18 -060086 pci_read_config_byte(pci_dev, PCI_HEADER_TYPE, &tmpbyte);
Bill Richardson55ae10f2012-10-20 11:44:34 +000087 if ((tmpbyte & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
88 debug("%s: invalid Header type\n", __func__);
Simon Glass74141122014-10-10 07:49:18 -060089 return -ENODEV;
Bill Richardson55ae10f2012-10-20 11:44:34 +000090 }
91
92 /* Base Class must be a bridge device */
Simon Glass74141122014-10-10 07:49:18 -060093 pci_read_config_byte(pci_dev, PCI_CLASS_CODE, &tmpbyte);
Bill Richardson55ae10f2012-10-20 11:44:34 +000094 if (tmpbyte != PCI_CLASS_CODE_BRIDGE) {
95 debug("%s: invalid class\n", __func__);
Simon Glass74141122014-10-10 07:49:18 -060096 return -ENODEV;
Bill Richardson55ae10f2012-10-20 11:44:34 +000097 }
98 /* Sub Class must be ISA */
Simon Glass74141122014-10-10 07:49:18 -060099 pci_read_config_byte(pci_dev, PCI_CLASS_SUB_CODE, &tmpbyte);
Bill Richardson55ae10f2012-10-20 11:44:34 +0000100 if (tmpbyte != PCI_CLASS_SUB_CODE_BRIDGE_ISA) {
101 debug("%s: invalid subclass\n", __func__);
Simon Glass74141122014-10-10 07:49:18 -0600102 return -ENODEV;
Bill Richardson55ae10f2012-10-20 11:44:34 +0000103 }
104
105 /* Programming Interface must be 0x00 (no others exist) */
Simon Glass74141122014-10-10 07:49:18 -0600106 pci_read_config_byte(pci_dev, PCI_CLASS_PROG, &tmpbyte);
Bill Richardson55ae10f2012-10-20 11:44:34 +0000107 if (tmpbyte != 0x00) {
108 debug("%s: invalid interface type\n", __func__);
Simon Glass74141122014-10-10 07:49:18 -0600109 return -ENODEV;
Bill Richardson55ae10f2012-10-20 11:44:34 +0000110 }
111
112 /*
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).
116 */
Simon Glass74141122014-10-10 07:49:18 -0600117 pci_read_config_dword(pci_dev, PCI_CFG_GPIOBASE, &tmplong);
Bill Richardson55ae10f2012-10-20 11:44:34 +0000118 if (tmplong == 0x00000000 || tmplong == 0xffffffff ||
119 !(tmplong & 0x00000001)) {
120 debug("%s: unexpected GPIOBASE value\n", __func__);
Simon Glass74141122014-10-10 07:49:18 -0600121 return -ENODEV;
Bill Richardson55ae10f2012-10-20 11:44:34 +0000122 }
123
124 /*
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.
129 */
130 gpiobase = tmplong & 0xfffffffe;
Simon Glass74141122014-10-10 07:49:18 -0600131 offset = fdtdec_get_int(gd->fdt_blob, dev->of_offset, "reg", -1);
132 if (offset == -1) {
133 debug("%s: Invalid register offset %d\n", __func__, offset);
134 return -EINVAL;
135 }
136 plat->base_addr = gpiobase + offset;
137 plat->bank_name = fdt_getprop(gd->fdt_blob, dev->of_offset,
138 "bank-name", NULL);
Bill Richardson55ae10f2012-10-20 11:44:34 +0000139
Bill Richardson55ae10f2012-10-20 11:44:34 +0000140 return 0;
141}
142
Simon Glass74141122014-10-10 07:49:18 -0600143int ich6_gpio_probe(struct udevice *dev)
Bill Richardson55ae10f2012-10-20 11:44:34 +0000144{
Simon Glass74141122014-10-10 07:49:18 -0600145 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);
148
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;
154
155 return 0;
156}
157
158int ich6_gpio_request(struct udevice *dev, unsigned offset, const char *label)
159{
160 struct ich6_bank_priv *bank = dev_get_priv(dev);
Bill Richardson55ae10f2012-10-20 11:44:34 +0000161 u32 tmplong;
Bill Richardson55ae10f2012-10-20 11:44:34 +0000162
163 /*
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
166 * requested pin.
167 */
Simon Glass74141122014-10-10 07:49:18 -0600168 tmplong = inl(bank->use_sel);
169 if (!(tmplong & (1UL << offset))) {
Bill Richardson57be9172012-10-20 11:44:36 +0000170 debug("%s: gpio %d is reserved for internal use\n", __func__,
Simon Glass74141122014-10-10 07:49:18 -0600171 offset);
172 return -EPERM;
Bill Richardson55ae10f2012-10-20 11:44:34 +0000173 }
174
Bill Richardson55ae10f2012-10-20 11:44:34 +0000175 return 0;
176}
177
Simon Glass74141122014-10-10 07:49:18 -0600178static int ich6_gpio_direction_input(struct udevice *dev, unsigned offset)
Bill Richardson55ae10f2012-10-20 11:44:34 +0000179{
Simon Glass74141122014-10-10 07:49:18 -0600180 struct ich6_bank_priv *bank = dev_get_priv(dev);
Bill Richardson57be9172012-10-20 11:44:36 +0000181 u32 tmplong;
Bill Richardson57be9172012-10-20 11:44:36 +0000182
Simon Glass74141122014-10-10 07:49:18 -0600183 tmplong = inl(bank->io_sel);
184 tmplong |= (1UL << offset);
185 outl(bank->io_sel, tmplong);
Bill Richardson55ae10f2012-10-20 11:44:34 +0000186 return 0;
187}
188
Simon Glass74141122014-10-10 07:49:18 -0600189static int ich6_gpio_direction_output(struct udevice *dev, unsigned offset,
190 int value)
Bill Richardson55ae10f2012-10-20 11:44:34 +0000191{
Simon Glass74141122014-10-10 07:49:18 -0600192 struct ich6_bank_priv *bank = dev_get_priv(dev);
Bill Richardson55ae10f2012-10-20 11:44:34 +0000193 u32 tmplong;
194
Simon Glass74141122014-10-10 07:49:18 -0600195 tmplong = inl(bank->io_sel);
196 tmplong &= ~(1UL << offset);
197 outl(bank->io_sel, tmplong);
Bill Richardson55ae10f2012-10-20 11:44:34 +0000198 return 0;
199}
200
Simon Glass74141122014-10-10 07:49:18 -0600201static int ich6_gpio_get_value(struct udevice *dev, unsigned offset)
202
Bill Richardson55ae10f2012-10-20 11:44:34 +0000203{
Simon Glass74141122014-10-10 07:49:18 -0600204 struct ich6_bank_priv *bank = dev_get_priv(dev);
Bill Richardson55ae10f2012-10-20 11:44:34 +0000205 u32 tmplong;
Bill Richardson57be9172012-10-20 11:44:36 +0000206 int r;
Bill Richardson55ae10f2012-10-20 11:44:34 +0000207
Simon Glass74141122014-10-10 07:49:18 -0600208 tmplong = inl(bank->lvl);
209 r = (tmplong & (1UL << offset)) ? 1 : 0;
Bill Richardson57be9172012-10-20 11:44:36 +0000210 return r;
Bill Richardson55ae10f2012-10-20 11:44:34 +0000211}
212
Simon Glass74141122014-10-10 07:49:18 -0600213static int ich6_gpio_set_value(struct udevice *dev, unsigned offset,
214 int value)
Bill Richardson55ae10f2012-10-20 11:44:34 +0000215{
Simon Glass74141122014-10-10 07:49:18 -0600216 struct ich6_bank_priv *bank = dev_get_priv(dev);
Bill Richardson55ae10f2012-10-20 11:44:34 +0000217 u32 tmplong;
218
Simon Glass74141122014-10-10 07:49:18 -0600219 tmplong = inl(bank->lvl);
Bill Richardson55ae10f2012-10-20 11:44:34 +0000220 if (value)
Simon Glass74141122014-10-10 07:49:18 -0600221 tmplong |= (1UL << offset);
Bill Richardson55ae10f2012-10-20 11:44:34 +0000222 else
Simon Glass74141122014-10-10 07:49:18 -0600223 tmplong &= ~(1UL << offset);
224 outl(bank->lvl, tmplong);
Bill Richardson55ae10f2012-10-20 11:44:34 +0000225 return 0;
226}
Simon Glass74141122014-10-10 07:49:18 -0600227
228static int ich6_gpio_get_function(struct udevice *dev, unsigned offset)
229{
230 struct ich6_bank_priv *bank = dev_get_priv(dev);
231 u32 mask = 1UL << offset;
232
233 if (!(inl(bank->use_sel) & mask))
234 return GPIOF_FUNC;
235 if (inl(bank->io_sel) & mask)
236 return GPIOF_INPUT;
237 else
238 return GPIOF_OUTPUT;
239}
240
241static 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,
248};
249
250static const struct udevice_id intel_ich6_gpio_ids[] = {
251 { .compatible = "intel,ich6-gpio" },
252 { }
253};
254
255U_BOOT_DRIVER(gpio_ich6) = {
256 .name = "gpio_ich6",
257 .id = UCLASS_GPIO,
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),
264};