blob: 096bc3b05bb7aa9064a9a6346b43a96a9bd90839 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bill Richardsoneece4322012-10-20 11:44:34 +00002/*
3 * Copyright (c) 2012 The Chromium OS Authors.
Bill Richardsoneece4322012-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 Richardson50a5ebe2012-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 Richardsoneece4322012-10-20 11:44:34 +000027 */
28
Simon Glass3a1d96f2023-07-15 21:39:11 -060029#define LOG_CATEGORY UCLASS_GPIO
30
Simon Glass2b4071b2014-10-10 07:49:18 -060031#include <dm.h>
32#include <errno.h>
33#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060034#include <log.h>
Bin Meng6e916cc2016-02-01 01:40:47 -080035#include <pch.h>
Bill Richardsoneece4322012-10-20 11:44:34 +000036#include <pci.h>
Simon Glassfb8757b2016-03-11 22:07:14 -070037#include <asm/cpu.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060038#include <asm/global_data.h>
Bill Richardsoneece4322012-10-20 11:44:34 +000039#include <asm/gpio.h>
40#include <asm/io.h>
Simon Glass60af0172014-11-12 22:42:24 -070041#include <asm/pci.h>
Bill Richardsoneece4322012-10-20 11:44:34 +000042
Simon Glassdaa93d92015-07-31 09:31:31 -060043DECLARE_GLOBAL_DATA_PTR;
44
Simon Glass2b4071b2014-10-10 07:49:18 -060045#define GPIO_PER_BANK 32
46
Simon Glass2b4071b2014-10-10 07:49:18 -060047struct ich6_bank_priv {
48 /* These are I/O addresses */
Bin Meng9b649692014-12-17 15:50:38 +080049 uint16_t use_sel;
50 uint16_t io_sel;
51 uint16_t lvl;
Bin Meng59be2c02017-05-07 19:52:29 -070052 u32 lvl_write_cache;
53 bool use_lvl_write_cache;
Bill Richardson50a5ebe2012-10-20 11:44:36 +000054};
55
Gabriel Huauf8135482015-05-25 22:27:37 -070056#define GPIO_USESEL_OFFSET(x) (x)
57#define GPIO_IOSEL_OFFSET(x) (x + 4)
58#define GPIO_LVL_OFFSET(x) (x + 8)
59
Bin Meng59be2c02017-05-07 19:52:29 -070060static int _ich6_gpio_set_value(struct ich6_bank_priv *bank, unsigned offset,
61 int value)
Gabriel Huauf8135482015-05-25 22:27:37 -070062{
63 u32 val;
64
Bin Meng59be2c02017-05-07 19:52:29 -070065 if (bank->use_lvl_write_cache)
66 val = bank->lvl_write_cache;
67 else
68 val = inl(bank->lvl);
69
Gabriel Huauf8135482015-05-25 22:27:37 -070070 if (value)
71 val |= (1UL << offset);
72 else
73 val &= ~(1UL << offset);
Bin Meng59be2c02017-05-07 19:52:29 -070074 outl(val, bank->lvl);
75 if (bank->use_lvl_write_cache)
76 bank->lvl_write_cache = val;
Gabriel Huauf8135482015-05-25 22:27:37 -070077
78 return 0;
79}
80
Gabriel Huauf8135482015-05-25 22:27:37 -070081static int _ich6_gpio_set_direction(uint16_t base, unsigned offset, int dir)
82{
83 u32 val;
84
85 if (!dir) {
86 val = inl(base);
87 val |= (1UL << offset);
88 outl(val, base);
89 } else {
90 val = inl(base);
91 val &= ~(1UL << offset);
92 outl(val, base);
93 }
94
95 return 0;
96}
97
Simon Glassaad29ae2020-12-03 16:55:21 -070098static int gpio_ich6_of_to_plat(struct udevice *dev)
Gabriel Huauf8135482015-05-25 22:27:37 -070099{
Simon Glassb75b15b2020-12-03 16:55:23 -0700100 struct ich6_bank_plat *plat = dev_get_plat(dev);
Bin Meng6e916cc2016-02-01 01:40:47 -0800101 u32 gpiobase;
Gabriel Huauf8135482015-05-25 22:27:37 -0700102 int offset;
Bin Meng6e916cc2016-02-01 01:40:47 -0800103 int ret;
104
105 ret = pch_get_gpio_base(dev->parent, &gpiobase);
106 if (ret)
107 return ret;
Gabriel Huauf8135482015-05-25 22:27:37 -0700108
Simon Glassdd79d6e2017-01-17 16:52:55 -0700109 offset = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "reg", -1);
Simon Glass2b4071b2014-10-10 07:49:18 -0600110 if (offset == -1) {
111 debug("%s: Invalid register offset %d\n", __func__, offset);
112 return -EINVAL;
113 }
Simon Glassec5c7862016-03-06 19:28:13 -0700114 plat->offset = offset;
Simon Glass2b4071b2014-10-10 07:49:18 -0600115 plat->base_addr = gpiobase + offset;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700116 plat->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
Simon Glass2b4071b2014-10-10 07:49:18 -0600117 "bank-name", NULL);
Bill Richardsoneece4322012-10-20 11:44:34 +0000118
Bill Richardsoneece4322012-10-20 11:44:34 +0000119 return 0;
120}
121
Simon Glass60af0172014-11-12 22:42:24 -0700122static int ich6_gpio_probe(struct udevice *dev)
Bill Richardsoneece4322012-10-20 11:44:34 +0000123{
Simon Glassb75b15b2020-12-03 16:55:23 -0700124 struct ich6_bank_plat *plat = dev_get_plat(dev);
Simon Glassde0977b2015-03-05 12:25:20 -0700125 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass2b4071b2014-10-10 07:49:18 -0600126 struct ich6_bank_priv *bank = dev_get_priv(dev);
Bin Meng59be2c02017-05-07 19:52:29 -0700127 const void *prop;
Bin Menga55173f2014-12-12 21:05:23 +0800128
Simon Glass2b4071b2014-10-10 07:49:18 -0600129 uc_priv->gpio_count = GPIO_PER_BANK;
130 uc_priv->bank_name = plat->bank_name;
131 bank->use_sel = plat->base_addr;
132 bank->io_sel = plat->base_addr + 4;
133 bank->lvl = plat->base_addr + 8;
Bill Richardsoneece4322012-10-20 11:44:34 +0000134
Simon Glass7a494432017-05-17 17:18:09 -0600135 prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
Bin Meng59be2c02017-05-07 19:52:29 -0700136 "use-lvl-write-cache", NULL);
137 if (prop)
138 bank->use_lvl_write_cache = true;
139 else
140 bank->use_lvl_write_cache = false;
141 bank->lvl_write_cache = 0;
142
Simon Glass2b4071b2014-10-10 07:49:18 -0600143 return 0;
144}
145
Simon Glass60af0172014-11-12 22:42:24 -0700146static int ich6_gpio_request(struct udevice *dev, unsigned offset,
147 const char *label)
Simon Glass2b4071b2014-10-10 07:49:18 -0600148{
149 struct ich6_bank_priv *bank = dev_get_priv(dev);
150 u32 tmplong;
Bill Richardsoneece4322012-10-20 11:44:34 +0000151
152 /*
153 * Make sure that the GPIO pin we want isn't already in use for some
154 * built-in hardware function. We have to check this for every
155 * requested pin.
156 */
Simon Glass2b4071b2014-10-10 07:49:18 -0600157 tmplong = inl(bank->use_sel);
158 if (!(tmplong & (1UL << offset))) {
Simon Glass3a1d96f2023-07-15 21:39:11 -0600159 log_debug("gpio %d is reserved for internal use\n", offset);
Simon Glass2b4071b2014-10-10 07:49:18 -0600160 return -EPERM;
Bill Richardsoneece4322012-10-20 11:44:34 +0000161 }
162
Bill Richardsoneece4322012-10-20 11:44:34 +0000163 return 0;
164}
165
Simon Glass2b4071b2014-10-10 07:49:18 -0600166static int ich6_gpio_direction_input(struct udevice *dev, unsigned offset)
Bill Richardsoneece4322012-10-20 11:44:34 +0000167{
Simon Glass2b4071b2014-10-10 07:49:18 -0600168 struct ich6_bank_priv *bank = dev_get_priv(dev);
Bill Richardson50a5ebe2012-10-20 11:44:36 +0000169
Simon Glass94641cb2015-08-22 15:58:58 -0600170 return _ich6_gpio_set_direction(bank->io_sel, offset, 0);
Bill Richardsoneece4322012-10-20 11:44:34 +0000171}
172
Simon Glass2b4071b2014-10-10 07:49:18 -0600173static int ich6_gpio_direction_output(struct udevice *dev, unsigned offset,
174 int value)
Bill Richardsoneece4322012-10-20 11:44:34 +0000175{
Gabriel Huauf8135482015-05-25 22:27:37 -0700176 int ret;
Simon Glass2b4071b2014-10-10 07:49:18 -0600177 struct ich6_bank_priv *bank = dev_get_priv(dev);
Bill Richardsoneece4322012-10-20 11:44:34 +0000178
Simon Glass94641cb2015-08-22 15:58:58 -0600179 ret = _ich6_gpio_set_direction(bank->io_sel, offset, 1);
Gabriel Huauf8135482015-05-25 22:27:37 -0700180 if (ret)
181 return ret;
Axel Lin6df0e9e2014-12-07 12:48:27 +0800182
Bin Meng59be2c02017-05-07 19:52:29 -0700183 return _ich6_gpio_set_value(bank, offset, value);
Bill Richardsoneece4322012-10-20 11:44:34 +0000184}
185
Simon Glass2b4071b2014-10-10 07:49:18 -0600186static int ich6_gpio_get_value(struct udevice *dev, unsigned offset)
Bill Richardsoneece4322012-10-20 11:44:34 +0000187{
Simon Glass2b4071b2014-10-10 07:49:18 -0600188 struct ich6_bank_priv *bank = dev_get_priv(dev);
Bill Richardsoneece4322012-10-20 11:44:34 +0000189 u32 tmplong;
Bill Richardson50a5ebe2012-10-20 11:44:36 +0000190 int r;
Bill Richardsoneece4322012-10-20 11:44:34 +0000191
Simon Glass2b4071b2014-10-10 07:49:18 -0600192 tmplong = inl(bank->lvl);
Bin Meng59be2c02017-05-07 19:52:29 -0700193 if (bank->use_lvl_write_cache)
194 tmplong |= bank->lvl_write_cache;
Simon Glass2b4071b2014-10-10 07:49:18 -0600195 r = (tmplong & (1UL << offset)) ? 1 : 0;
Bill Richardson50a5ebe2012-10-20 11:44:36 +0000196 return r;
Bill Richardsoneece4322012-10-20 11:44:34 +0000197}
198
Simon Glass2b4071b2014-10-10 07:49:18 -0600199static int ich6_gpio_set_value(struct udevice *dev, unsigned offset,
200 int value)
Bill Richardsoneece4322012-10-20 11:44:34 +0000201{
Simon Glass2b4071b2014-10-10 07:49:18 -0600202 struct ich6_bank_priv *bank = dev_get_priv(dev);
Bin Meng59be2c02017-05-07 19:52:29 -0700203 return _ich6_gpio_set_value(bank, offset, value);
Bill Richardsoneece4322012-10-20 11:44:34 +0000204}
Simon Glass2b4071b2014-10-10 07:49:18 -0600205
206static int ich6_gpio_get_function(struct udevice *dev, unsigned offset)
207{
208 struct ich6_bank_priv *bank = dev_get_priv(dev);
209 u32 mask = 1UL << offset;
210
211 if (!(inl(bank->use_sel) & mask))
212 return GPIOF_FUNC;
213 if (inl(bank->io_sel) & mask)
214 return GPIOF_INPUT;
215 else
216 return GPIOF_OUTPUT;
217}
218
219static const struct dm_gpio_ops gpio_ich6_ops = {
220 .request = ich6_gpio_request,
221 .direction_input = ich6_gpio_direction_input,
222 .direction_output = ich6_gpio_direction_output,
223 .get_value = ich6_gpio_get_value,
224 .set_value = ich6_gpio_set_value,
225 .get_function = ich6_gpio_get_function,
226};
227
228static const struct udevice_id intel_ich6_gpio_ids[] = {
229 { .compatible = "intel,ich6-gpio" },
230 { }
231};
232
233U_BOOT_DRIVER(gpio_ich6) = {
234 .name = "gpio_ich6",
235 .id = UCLASS_GPIO,
236 .of_match = intel_ich6_gpio_ids,
237 .ops = &gpio_ich6_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700238 .of_to_plat = gpio_ich6_of_to_plat,
Simon Glass2b4071b2014-10-10 07:49:18 -0600239 .probe = ich6_gpio_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700240 .priv_auto = sizeof(struct ich6_bank_priv),
Simon Glassb75b15b2020-12-03 16:55:23 -0700241 .plat_auto = sizeof(struct ich6_bank_plat),
Simon Glass2b4071b2014-10-10 07:49:18 -0600242};