blob: 63a07b9592a0cdda102db5b14580f7938eeac55f [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bill Richardson55ae10f2012-10-20 11:44:34 +00002/*
3 * Copyright (c) 2012 The Chromium OS Authors.
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>
Simon Glassf7ae49f2020-05-10 11:40:05 -060033#include <log.h>
Bin Meng3ddc1c72016-02-01 01:40:47 -080034#include <pch.h>
Bill Richardson55ae10f2012-10-20 11:44:34 +000035#include <pci.h>
Simon Glass15cf75e2016-03-11 22:07:14 -070036#include <asm/cpu.h>
Simon Glass401d1c42020-10-30 21:38:53 -060037#include <asm/global_data.h>
Bill Richardson55ae10f2012-10-20 11:44:34 +000038#include <asm/gpio.h>
39#include <asm/io.h>
Simon Glass1b4f25f2014-11-12 22:42:24 -070040#include <asm/pci.h>
Bill Richardson55ae10f2012-10-20 11:44:34 +000041
Simon Glass8b097912015-07-31 09:31:31 -060042DECLARE_GLOBAL_DATA_PTR;
43
Simon Glass74141122014-10-10 07:49:18 -060044#define GPIO_PER_BANK 32
45
Simon Glass74141122014-10-10 07:49:18 -060046struct ich6_bank_priv {
47 /* These are I/O addresses */
Bin Mengb71eec32014-12-17 15:50:38 +080048 uint16_t use_sel;
49 uint16_t io_sel;
50 uint16_t lvl;
Bin Meng770ee012017-05-07 19:52:29 -070051 u32 lvl_write_cache;
52 bool use_lvl_write_cache;
Bill Richardson57be9172012-10-20 11:44:36 +000053};
Bill Richardson55ae10f2012-10-20 11:44:34 +000054
Gabriel Huau5318f182015-05-25 22:27:37 -070055#define GPIO_USESEL_OFFSET(x) (x)
56#define GPIO_IOSEL_OFFSET(x) (x + 4)
57#define GPIO_LVL_OFFSET(x) (x + 8)
58
Bin Meng770ee012017-05-07 19:52:29 -070059static int _ich6_gpio_set_value(struct ich6_bank_priv *bank, unsigned offset,
60 int value)
Gabriel Huau5318f182015-05-25 22:27:37 -070061{
62 u32 val;
63
Bin Meng770ee012017-05-07 19:52:29 -070064 if (bank->use_lvl_write_cache)
65 val = bank->lvl_write_cache;
66 else
67 val = inl(bank->lvl);
68
Gabriel Huau5318f182015-05-25 22:27:37 -070069 if (value)
70 val |= (1UL << offset);
71 else
72 val &= ~(1UL << offset);
Bin Meng770ee012017-05-07 19:52:29 -070073 outl(val, bank->lvl);
74 if (bank->use_lvl_write_cache)
75 bank->lvl_write_cache = val;
Gabriel Huau5318f182015-05-25 22:27:37 -070076
77 return 0;
78}
79
Gabriel Huau5318f182015-05-25 22:27:37 -070080static int _ich6_gpio_set_direction(uint16_t base, unsigned offset, int dir)
81{
82 u32 val;
83
84 if (!dir) {
85 val = inl(base);
86 val |= (1UL << offset);
87 outl(val, base);
88 } else {
89 val = inl(base);
90 val &= ~(1UL << offset);
91 outl(val, base);
92 }
93
94 return 0;
95}
96
Simon Glassd1998a92020-12-03 16:55:21 -070097static int gpio_ich6_of_to_plat(struct udevice *dev)
Gabriel Huau5318f182015-05-25 22:27:37 -070098{
Simon Glass8a8d24b2020-12-03 16:55:23 -070099 struct ich6_bank_plat *plat = dev_get_plat(dev);
Bin Meng3ddc1c72016-02-01 01:40:47 -0800100 u32 gpiobase;
Gabriel Huau5318f182015-05-25 22:27:37 -0700101 int offset;
Bin Meng3ddc1c72016-02-01 01:40:47 -0800102 int ret;
Gabriel Huau5318f182015-05-25 22:27:37 -0700103
Bin Meng3ddc1c72016-02-01 01:40:47 -0800104 ret = pch_get_gpio_base(dev->parent, &gpiobase);
105 if (ret)
106 return ret;
107
Simon Glasse160f7d2017-01-17 16:52:55 -0700108 offset = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "reg", -1);
Simon Glass74141122014-10-10 07:49:18 -0600109 if (offset == -1) {
110 debug("%s: Invalid register offset %d\n", __func__, offset);
111 return -EINVAL;
112 }
Simon Glassd6d50db2016-03-06 19:28:13 -0700113 plat->offset = offset;
Simon Glass74141122014-10-10 07:49:18 -0600114 plat->base_addr = gpiobase + offset;
Simon Glasse160f7d2017-01-17 16:52:55 -0700115 plat->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
Simon Glass74141122014-10-10 07:49:18 -0600116 "bank-name", NULL);
Bill Richardson55ae10f2012-10-20 11:44:34 +0000117
Bill Richardson55ae10f2012-10-20 11:44:34 +0000118 return 0;
119}
120
Simon Glass1b4f25f2014-11-12 22:42:24 -0700121static int ich6_gpio_probe(struct udevice *dev)
Bill Richardson55ae10f2012-10-20 11:44:34 +0000122{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700123 struct ich6_bank_plat *plat = dev_get_plat(dev);
Simon Glasse564f052015-03-05 12:25:20 -0700124 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass74141122014-10-10 07:49:18 -0600125 struct ich6_bank_priv *bank = dev_get_priv(dev);
Bin Meng770ee012017-05-07 19:52:29 -0700126 const void *prop;
Bin Meng27955732014-12-12 21:05:23 +0800127
Simon Glass74141122014-10-10 07:49:18 -0600128 uc_priv->gpio_count = GPIO_PER_BANK;
129 uc_priv->bank_name = plat->bank_name;
130 bank->use_sel = plat->base_addr;
131 bank->io_sel = plat->base_addr + 4;
132 bank->lvl = plat->base_addr + 8;
133
Simon Glassda409cc2017-05-17 17:18:09 -0600134 prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
Bin Meng770ee012017-05-07 19:52:29 -0700135 "use-lvl-write-cache", NULL);
136 if (prop)
137 bank->use_lvl_write_cache = true;
138 else
139 bank->use_lvl_write_cache = false;
140 bank->lvl_write_cache = 0;
141
Simon Glass74141122014-10-10 07:49:18 -0600142 return 0;
143}
144
Simon Glass1b4f25f2014-11-12 22:42:24 -0700145static int ich6_gpio_request(struct udevice *dev, unsigned offset,
146 const char *label)
Simon Glass74141122014-10-10 07:49:18 -0600147{
148 struct ich6_bank_priv *bank = dev_get_priv(dev);
Bill Richardson55ae10f2012-10-20 11:44:34 +0000149 u32 tmplong;
Bill Richardson55ae10f2012-10-20 11:44:34 +0000150
151 /*
152 * Make sure that the GPIO pin we want isn't already in use for some
153 * built-in hardware function. We have to check this for every
154 * requested pin.
155 */
Simon Glass74141122014-10-10 07:49:18 -0600156 tmplong = inl(bank->use_sel);
157 if (!(tmplong & (1UL << offset))) {
Bill Richardson57be9172012-10-20 11:44:36 +0000158 debug("%s: gpio %d is reserved for internal use\n", __func__,
Simon Glass74141122014-10-10 07:49:18 -0600159 offset);
160 return -EPERM;
Bill Richardson55ae10f2012-10-20 11:44:34 +0000161 }
162
Bill Richardson55ae10f2012-10-20 11:44:34 +0000163 return 0;
164}
165
Simon Glass74141122014-10-10 07:49:18 -0600166static int ich6_gpio_direction_input(struct udevice *dev, unsigned offset)
Bill Richardson55ae10f2012-10-20 11:44:34 +0000167{
Simon Glass74141122014-10-10 07:49:18 -0600168 struct ich6_bank_priv *bank = dev_get_priv(dev);
Bill Richardson57be9172012-10-20 11:44:36 +0000169
Simon Glasse7cc0b62015-08-22 15:58:58 -0600170 return _ich6_gpio_set_direction(bank->io_sel, offset, 0);
Bill Richardson55ae10f2012-10-20 11:44:34 +0000171}
172
Simon Glass74141122014-10-10 07:49:18 -0600173static int ich6_gpio_direction_output(struct udevice *dev, unsigned offset,
174 int value)
Bill Richardson55ae10f2012-10-20 11:44:34 +0000175{
Gabriel Huau5318f182015-05-25 22:27:37 -0700176 int ret;
Simon Glass74141122014-10-10 07:49:18 -0600177 struct ich6_bank_priv *bank = dev_get_priv(dev);
Bill Richardson55ae10f2012-10-20 11:44:34 +0000178
Simon Glasse7cc0b62015-08-22 15:58:58 -0600179 ret = _ich6_gpio_set_direction(bank->io_sel, offset, 1);
Gabriel Huau5318f182015-05-25 22:27:37 -0700180 if (ret)
181 return ret;
Axel Lin0a547452014-12-07 12:48:27 +0800182
Bin Meng770ee012017-05-07 19:52:29 -0700183 return _ich6_gpio_set_value(bank, offset, value);
Bill Richardson55ae10f2012-10-20 11:44:34 +0000184}
185
Simon Glass74141122014-10-10 07:49:18 -0600186static int ich6_gpio_get_value(struct udevice *dev, unsigned offset)
Bill Richardson55ae10f2012-10-20 11:44:34 +0000187{
Simon Glass74141122014-10-10 07:49:18 -0600188 struct ich6_bank_priv *bank = dev_get_priv(dev);
Bill Richardson55ae10f2012-10-20 11:44:34 +0000189 u32 tmplong;
Bill Richardson57be9172012-10-20 11:44:36 +0000190 int r;
Bill Richardson55ae10f2012-10-20 11:44:34 +0000191
Simon Glass74141122014-10-10 07:49:18 -0600192 tmplong = inl(bank->lvl);
Bin Meng770ee012017-05-07 19:52:29 -0700193 if (bank->use_lvl_write_cache)
194 tmplong |= bank->lvl_write_cache;
Simon Glass74141122014-10-10 07:49:18 -0600195 r = (tmplong & (1UL << offset)) ? 1 : 0;
Bill Richardson57be9172012-10-20 11:44:36 +0000196 return r;
Bill Richardson55ae10f2012-10-20 11:44:34 +0000197}
198
Simon Glass74141122014-10-10 07:49:18 -0600199static int ich6_gpio_set_value(struct udevice *dev, unsigned offset,
200 int value)
Bill Richardson55ae10f2012-10-20 11:44:34 +0000201{
Simon Glass74141122014-10-10 07:49:18 -0600202 struct ich6_bank_priv *bank = dev_get_priv(dev);
Bin Meng770ee012017-05-07 19:52:29 -0700203 return _ich6_gpio_set_value(bank, offset, value);
Bill Richardson55ae10f2012-10-20 11:44:34 +0000204}
Simon Glass74141122014-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 Glassd1998a92020-12-03 16:55:21 -0700238 .of_to_plat = gpio_ich6_of_to_plat,
Simon Glass74141122014-10-10 07:49:18 -0600239 .probe = ich6_gpio_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700240 .priv_auto = sizeof(struct ich6_bank_priv),
Simon Glass8a8d24b2020-12-03 16:55:23 -0700241 .plat_auto = sizeof(struct ich6_bank_plat),
Simon Glass74141122014-10-10 07:49:18 -0600242};