Debian Codegraph @

src/southbridge/intel/common/gpio.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <assert.h>
4 #include <arch/io.h>
5 #include <device/pci_ops.h>
6 #include <device/pci_type.h>
7 #include <gpio.h>
8 #include <stdint.h>
9 #include <gpio.h>
10
11 #include "southbridge/intel/common/gpio.h"
12
13 #define MAX_GPIO_NUMBER 75 /* zero based */
14
15 /*
16 * If you want to use these macros outside this file, consider making
17 * more helper functions to expose the functionality you want instead.
18 */
19
20 /* LPC GPIO Base Address Register */
21 #define GPIO_BASE 0x48
22
23 /* ICH7 GPIOBASE */
24 #define GPIO_USE_SEL 0x00
25 #define GP_IO_SEL 0x04
26 #define GP_LVL 0x0c
27 #define GPO_BLINK 0x18
28 #define GPI_INV 0x2c
29 #define GPIO_USE_SEL2 0x30
30 #define GP_IO_SEL2 0x34
31 #define GP_LVL2 0x38
32 #define GPIO_USE_SEL3 0x40
33 #define GP_IO_SEL3 0x44
34 #define GP_LVL3 0x48
35 #define GP_RST_SEL1 0x60
36 #define GP_RST_SEL2 0x64
37 #define GP_RST_SEL3 0x68
38
39
40 static u16 get_gpio_base(void)
41 {
42 #ifdef __SIMPLE_DEVICE__
43 /* Don't assume GPIO_BASE is still the same */
44 return pci_read_config16(PCI_DEV(0, 0x1f, 0), GPIO_BASE) & 0xfffe;
45 #else
46 static u16 gpiobase;
47
48 if (gpiobase)
49 return gpiobase;
50
51 gpiobase = pci_read_config16(pcidev_on_root(0x1f, 0), GPIO_BASE) & 0xfffe;
52
53 return gpiobase;
54 #endif
55 }
56
57 void setup_pch_gpios(const struct pch_gpio_map *gpio)
58 {
59 u16 gpiobase = get_gpio_base();
60
61 /* The order of these calls does matter on ICH9M and prior.
62 * The level has to be set on pins configured as gpio,
63 * but on newer platforms we want to change the level first
64 * to make sure there are no glitches on the lines !
65 * Write the gpio level twice to satisfy both requirements.
66 */
67
68 /* GPIO Set 1 */
69 if (gpio->set1.level)
70 outl(*((u32 *)gpio->set1.level), gpiobase + GP_LVL);
71 if (gpio->set1.mode)
72 outl(*((u32 *)gpio->set1.mode), gpiobase + GPIO_USE_SEL);
73 if (gpio->set1.direction)
74 outl(*((u32 *)gpio->set1.direction), gpiobase + GP_IO_SEL);
75 if (gpio->set1.level)
76 outl(*((u32 *)gpio->set1.level), gpiobase + GP_LVL);
77 if (gpio->set1.reset)
78 outl(*((u32 *)gpio->set1.reset), gpiobase + GP_RST_SEL1);
79 if (gpio->set1.invert)
80 outl(*((u32 *)gpio->set1.invert), gpiobase + GPI_INV);
81 if (gpio->set1.blink)
82 outl(*((u32 *)gpio->set1.blink), gpiobase + GPO_BLINK);
83
84 /* GPIO Set 2 */
85 if (gpio->set2.level)
86 outl(*((u32 *)gpio->set2.level), gpiobase + GP_LVL2);
87 if (gpio->set2.mode)
88 outl(*((u32 *)gpio->set2.mode), gpiobase + GPIO_USE_SEL2);
89 if (gpio->set2.direction)
90 outl(*((u32 *)gpio->set2.direction), gpiobase + GP_IO_SEL2);
91 if (gpio->set2.level)
92 outl(*((u32 *)gpio->set2.level), gpiobase + GP_LVL2);
93 if (gpio->set2.reset)
94 outl(*((u32 *)gpio->set2.reset), gpiobase + GP_RST_SEL2);
95
96 /* GPIO Set 3 */
97 if (gpio->set3.level)
98 outl(*((u32 *)gpio->set3.level), gpiobase + GP_LVL3);
99 if (gpio->set3.mode)
100 outl(*((u32 *)gpio->set3.mode), gpiobase + GPIO_USE_SEL3);
101 if (gpio->set3.direction)
102 outl(*((u32 *)gpio->set3.direction), gpiobase + GP_IO_SEL3);
103 if (gpio->set3.level)
104 outl(*((u32 *)gpio->set3.level), gpiobase + GP_LVL3);
105 if (gpio->set3.reset)
106 outl(*((u32 *)gpio->set3.reset), gpiobase + GP_RST_SEL3);
107 }
108
109 /*
110 * return current gpio level.
111 */
112 int gpio_get(gpio_t gpio_num)
113 {
114 static const int gpio_reg_offsets[] = {GP_LVL, GP_LVL2, GP_LVL3};
115 u16 gpio_base = get_gpio_base();
116 int index, bit;
117
118 if (gpio_num > MAX_GPIO_NUMBER)
119 return 0; /* Just ignore wrong gpio numbers. */
120
121 index = gpio_num / 32;
122 bit = gpio_num % 32;
123
124 return (inl(gpio_base + gpio_reg_offsets[index]) >> bit) & 1;
125 }
126
127 /*
128 * set gpio output to level.
129 */
130 void gpio_set(gpio_t gpio_num, int value)
131 {
132 static const int gpio_reg_offsets[] = {
133 GP_LVL, GP_LVL2, GP_LVL3
134 };
135 u16 gpio_base = get_gpio_base();
136 int index, bit;
137 u32 config;
138
139 if (gpio_num > MAX_GPIO_NUMBER)
140 return; /* Just ignore wrong gpio numbers. */
141
142 index = gpio_num / 32;
143 bit = gpio_num % 32;
144
145 config = inl(gpio_base + gpio_reg_offsets[index]);
146 config &= ~(1 << bit);
147 if (value != 0)
148 config |= (1 << bit);
149 outl(config, gpio_base + gpio_reg_offsets[index]);
150 }
151
152 int gpio_is_native(int gpio_num)
153 {
154 static const int gpio_reg_offsets[] = {
155 GPIO_USE_SEL, GPIO_USE_SEL2, GPIO_USE_SEL3
156 };
157 u16 gpio_base = get_gpio_base();
158 int index, bit;
159 u32 config;
160
161 if (gpio_num > MAX_GPIO_NUMBER)
162 return 0; /* Just ignore wrong gpio numbers. */
163
164 index = gpio_num / 32;
165 bit = gpio_num % 32;
166
167 config = inl(gpio_base + gpio_reg_offsets[index]);
168 return !(config & (1 << bit));
169 }
170
171 static void gpio_set_gpio(gpio_t gpio_num)
172 {
173 static const int gpio_use_reg_offsets[] = {
174 GPIO_USE_SEL, GPIO_USE_SEL2, GPIO_USE_SEL3
175 };
176 u16 gpio_base = get_gpio_base();
177 int index, bit;
178 u32 config;
179
180 if (gpio_num > MAX_GPIO_NUMBER)
181 return; /* Just ignore wrong gpio numbers. */
182
183 index = gpio_num / 32;
184 bit = gpio_num % 32;
185
186 config = inl(gpio_base + gpio_use_reg_offsets[index]);
187 if (config & (GPIO_MODE_GPIO << bit))
188 return;
189
190 config |= GPIO_MODE_GPIO << bit;
191 outl(gpio_base + gpio_use_reg_offsets[index], config);
192 }
193
194 void gpio_input(gpio_t gpio_num)
195 {
196 static const int gpio_io_reg_offsets[] = {
197 GP_IO_SEL, GP_IO_SEL2, GP_IO_SEL3
198 };
199 u16 gpio_base = get_gpio_base();
200 int index, bit;
201 u32 config;
202
203 if (gpio_num > MAX_GPIO_NUMBER)
204 return; /* Just ignore wrong gpio numbers. */
205
206 gpio_set_gpio(gpio_num);
207
208 index = gpio_num / 32;
209 bit = gpio_num % 32;
210
211 config = inl(gpio_base + gpio_io_reg_offsets[index]);
212 if (((config >> bit) & 1) == GPIO_DIR_INPUT)
213 return;
214
215 config |= 1 << bit;
216 outl(gpio_base + gpio_io_reg_offsets[index], config);
217 }
218
219 void gpio_output(gpio_t gpio_num, int value)
220 {
221 static const int gpio_io_reg_offsets[] = {
222 GP_IO_SEL, GP_IO_SEL2, GP_IO_SEL3
223 };
224 u16 gpio_base = get_gpio_base();
225 int index, bit;
226 u32 config;
227
228 if (gpio_num > MAX_GPIO_NUMBER)
229 return; /* Just ignore wrong gpio numbers. */
230
231 gpio_set_gpio(gpio_num);
232 gpio_set(gpio_num, value);
233
234 index = gpio_num / 32;
235 bit = gpio_num % 32;
236
237 config = inl(gpio_base + gpio_io_reg_offsets[index]);
238 if (((config >> bit) & 1) == GPIO_DIR_OUTPUT)
239 return;
240
241 config &= ~(1 << bit);
242 outl(gpio_base + gpio_io_reg_offsets[index], config);
243
244 /* Set value again in case output register was gated */
245 gpio_set(gpio_num, value);
246 }
247
248 void gpio_invert(gpio_t gpio_num, bool invert)
249 {
250 u16 gpio_blink_reg = get_gpio_base() + GPI_INV;
251 u32 config;
252
253 if (gpio_num >= 32)
254 return; /* Just ignore wrong gpio numbers. */
255
256 config = inl(gpio_blink_reg);
257 if (invert)
258 config |= BIT(gpio_num);
259 else
260 config &= ~BIT(gpio_num);
261 outl(gpio_blink_reg, config);
262 }