]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/gpio/intel_ich6_gpio.c
misc: add "call" uclass op
[people/ms/u-boot.git] / drivers / gpio / intel_ich6_gpio.c
CommitLineData
55ae10f8
BR
1/*
2 * Copyright (c) 2012 The Chromium OS Authors.
1a459660 3 * SPDX-License-Identifier: GPL-2.0+
55ae10f8
BR
4 */
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
57be9172
BR
19 *
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.
55ae10f8
BR
27 */
28
29#include <common.h>
7414112d
SG
30#include <dm.h>
31#include <errno.h>
32#include <fdtdec.h>
3ddc1c7b 33#include <pch.h>
55ae10f8 34#include <pci.h>
15cf75ec 35#include <asm/cpu.h>
55ae10f8
BR
36#include <asm/gpio.h>
37#include <asm/io.h>
1b4f25ff 38#include <asm/pci.h>
55ae10f8 39
8b097916
SG
40DECLARE_GLOBAL_DATA_PTR;
41
7414112d
SG
42#define GPIO_PER_BANK 32
43
7414112d
SG
44struct ich6_bank_priv {
45 /* These are I/O addresses */
b71eec31
BM
46 uint16_t use_sel;
47 uint16_t io_sel;
48 uint16_t lvl;
57be9172
BR
49};
50
5318f18d
GH
51#define GPIO_USESEL_OFFSET(x) (x)
52#define GPIO_IOSEL_OFFSET(x) (x + 4)
53#define GPIO_LVL_OFFSET(x) (x + 8)
54
5318f18d
GH
55static int _ich6_gpio_set_value(uint16_t base, unsigned offset, int value)
56{
57 u32 val;
58
59 val = inl(base);
60 if (value)
61 val |= (1UL << offset);
62 else
63 val &= ~(1UL << offset);
64 outl(val, base);
65
66 return 0;
67}
68
5318f18d
GH
69static int _ich6_gpio_set_direction(uint16_t base, unsigned offset, int dir)
70{
71 u32 val;
72
73 if (!dir) {
74 val = inl(base);
75 val |= (1UL << offset);
76 outl(val, base);
77 } else {
78 val = inl(base);
79 val &= ~(1UL << offset);
80 outl(val, base);
81 }
82
83 return 0;
84}
85
5318f18d
GH
86static int gpio_ich6_ofdata_to_platdata(struct udevice *dev)
87{
88 struct ich6_bank_platdata *plat = dev_get_platdata(dev);
3ddc1c7b 89 u32 gpiobase;
5318f18d 90 int offset;
3ddc1c7b
BM
91 int ret;
92
93 ret = pch_get_gpio_base(dev->parent, &gpiobase);
94 if (ret)
95 return ret;
5318f18d 96
7414112d
SG
97 offset = fdtdec_get_int(gd->fdt_blob, dev->of_offset, "reg", -1);
98 if (offset == -1) {
99 debug("%s: Invalid register offset %d\n", __func__, offset);
100 return -EINVAL;
101 }
d6d50db8 102 plat->offset = offset;
7414112d
SG
103 plat->base_addr = gpiobase + offset;
104 plat->bank_name = fdt_getprop(gd->fdt_blob, dev->of_offset,
105 "bank-name", NULL);
55ae10f8 106
55ae10f8
BR
107 return 0;
108}
109
1b4f25ff 110static int ich6_gpio_probe(struct udevice *dev)
55ae10f8 111{
7414112d 112 struct ich6_bank_platdata *plat = dev_get_platdata(dev);
e564f054 113 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
7414112d 114 struct ich6_bank_priv *bank = dev_get_priv(dev);
2795573a 115
7414112d
SG
116 uc_priv->gpio_count = GPIO_PER_BANK;
117 uc_priv->bank_name = plat->bank_name;
118 bank->use_sel = plat->base_addr;
119 bank->io_sel = plat->base_addr + 4;
120 bank->lvl = plat->base_addr + 8;
55ae10f8 121
7414112d
SG
122 return 0;
123}
55ae10f8 124
1b4f25ff
SG
125static int ich6_gpio_request(struct udevice *dev, unsigned offset,
126 const char *label)
7414112d
SG
127{
128 struct ich6_bank_priv *bank = dev_get_priv(dev);
129 u32 tmplong;
55ae10f8
BR
130
131 /*
132 * Make sure that the GPIO pin we want isn't already in use for some
133 * built-in hardware function. We have to check this for every
134 * requested pin.
135 */
7414112d
SG
136 tmplong = inl(bank->use_sel);
137 if (!(tmplong & (1UL << offset))) {
57be9172 138 debug("%s: gpio %d is reserved for internal use\n", __func__,
7414112d
SG
139 offset);
140 return -EPERM;
55ae10f8
BR
141 }
142
55ae10f8
BR
143 return 0;
144}
145
7414112d 146static int ich6_gpio_direction_input(struct udevice *dev, unsigned offset)
55ae10f8 147{
7414112d 148 struct ich6_bank_priv *bank = dev_get_priv(dev);
57be9172 149
e7cc0b6f 150 return _ich6_gpio_set_direction(bank->io_sel, offset, 0);
55ae10f8
BR
151}
152
7414112d
SG
153static int ich6_gpio_direction_output(struct udevice *dev, unsigned offset,
154 int value)
55ae10f8 155{
5318f18d 156 int ret;
7414112d 157 struct ich6_bank_priv *bank = dev_get_priv(dev);
55ae10f8 158
e7cc0b6f 159 ret = _ich6_gpio_set_direction(bank->io_sel, offset, 1);
5318f18d
GH
160 if (ret)
161 return ret;
0a54745f 162
5318f18d 163 return _ich6_gpio_set_value(bank->lvl, offset, value);
55ae10f8
BR
164}
165
7414112d 166static int ich6_gpio_get_value(struct udevice *dev, unsigned offset)
55ae10f8 167{
7414112d 168 struct ich6_bank_priv *bank = dev_get_priv(dev);
55ae10f8 169 u32 tmplong;
57be9172 170 int r;
55ae10f8 171
7414112d
SG
172 tmplong = inl(bank->lvl);
173 r = (tmplong & (1UL << offset)) ? 1 : 0;
57be9172 174 return r;
55ae10f8
BR
175}
176
7414112d
SG
177static int ich6_gpio_set_value(struct udevice *dev, unsigned offset,
178 int value)
55ae10f8 179{
7414112d 180 struct ich6_bank_priv *bank = dev_get_priv(dev);
5318f18d 181 return _ich6_gpio_set_value(bank->lvl, offset, value);
55ae10f8 182}
7414112d
SG
183
184static int ich6_gpio_get_function(struct udevice *dev, unsigned offset)
185{
186 struct ich6_bank_priv *bank = dev_get_priv(dev);
187 u32 mask = 1UL << offset;
188
189 if (!(inl(bank->use_sel) & mask))
190 return GPIOF_FUNC;
191 if (inl(bank->io_sel) & mask)
192 return GPIOF_INPUT;
193 else
194 return GPIOF_OUTPUT;
195}
196
197static const struct dm_gpio_ops gpio_ich6_ops = {
198 .request = ich6_gpio_request,
199 .direction_input = ich6_gpio_direction_input,
200 .direction_output = ich6_gpio_direction_output,
201 .get_value = ich6_gpio_get_value,
202 .set_value = ich6_gpio_set_value,
203 .get_function = ich6_gpio_get_function,
204};
205
206static const struct udevice_id intel_ich6_gpio_ids[] = {
207 { .compatible = "intel,ich6-gpio" },
208 { }
209};
210
211U_BOOT_DRIVER(gpio_ich6) = {
212 .name = "gpio_ich6",
213 .id = UCLASS_GPIO,
214 .of_match = intel_ich6_gpio_ids,
215 .ops = &gpio_ich6_ops,
216 .ofdata_to_platdata = gpio_ich6_ofdata_to_platdata,
217 .probe = ich6_gpio_probe,
218 .priv_auto_alloc_size = sizeof(struct ich6_bank_priv),
219 .platdata_auto_alloc_size = sizeof(struct ich6_bank_platdata),
220};