blob: 1f0412c84915ca3c13781d0074ae4ad669b89dda [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2012-2015 Travis Geiselbrecht
3 * Copyright (c) 2015 Christopher Anderson
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files
7 * (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge,
9 * publish, distribute, sublicense, and/or sell copies of the Software,
10 * and to permit persons to whom the Software is furnished to do so,
11 * subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24#include <assert.h>
25#include <debug.h>
26#include <reg.h>
27#include <stdio.h>
28#include <string.h>
29#include <dev/gpio.h>
30#include <platform/gpio.h>
31#include <platform/interrupts.h>
32#include <target/gpioconfig.h>
33
34#define MAX_GPIO 128
35
36static inline uint16_t extract_bank(unsigned gpio_id) { return gpio_id / 32; }
37static inline uint16_t extract_bit (unsigned gpio_id) { return gpio_id % 32; }
38
39struct {
40 int_handler callback;
41 void *args;
42} irq_callbacks[MAX_GPIO];
43
44static enum handler_return gpio_int_handler(void *arg) {
45
46 /* The mask register uses 1 to respresent masked, 0 for unmasked. Comparing that
47 * register with the interrupt status register is the only way to determine
48 * which gpio triggered the interrupt in the gic. */
49 for (uint32_t bank = 0; bank < 4; bank++) {
50 uint32_t mask = *REG32(GPIO_INT_MASK(bank));
51 uint32_t stat = *REG32(GPIO_INT_STAT(bank));
52 uint32_t active = ~mask & stat;
53
54 if (active == 0) {
55 continue;
56 }
57
58 //printf("mask 0x%08x stat 0x%08x active 0x%08x\n", mask, stat, active);
59 while (active) {
60 /* Find the rightmost set bit, calculate the associated gpio, and call the callback */
61 uint16_t bit = 32 - clz(active) - 1;
62 uint16_t gpio = bit + (bank * 32);
63
64 active ^= (1 << bit);
65 if (irq_callbacks[gpio].callback) {
66 irq_callbacks[gpio].callback(irq_callbacks[gpio].args);
67 }
68 //printf("bit %u bank %u gpio %u was triggered\n", bit, bank, gpio);
69 }
70
71 *REG32(GPIO_INT_STAT(bank)) = stat;
72 }
73
74 return 0;
75}
76
77void zynq_unmask_gpio_interrupt(unsigned gpio)
78{
79 uint16_t bank = extract_bank(gpio);
80 uint16_t bit = extract_bit(gpio);
81
82 RMWREG32(GPIO_INT_EN(bank), bit, 1, 1);
83 RMWREG32(GPIO_INT_STAT(bank), bit, 1, 1);
84}
85
86void zynq_mask_gpio_interrupt(unsigned gpio)
87{
88 uint16_t bank = extract_bank(gpio);
89 uint16_t bit = extract_bit(gpio);
90
91 RMWREG32(GPIO_INT_DIS(bank), bit, 1, 1);
92}
93
94
95void zynq_gpio_init(void)
96{
97 register_int_handler(GPIO_INT, gpio_int_handler, NULL);
98 unmask_interrupt(GPIO_INT);
99
100 // Note(johngro):
101 //
102 // The Zynq 700 series documentation describes two bits which affect the
103 // input vs. output nature of the GPIOs (DIRM and OEN, or output enable).
104 // On the surface, the docs seem to indicate that they do the same thing,
105 // which is to enable or disable the output driver on the pin. The docs
106 // make it clear that the input function is always enabled, regardless of
107 // the settings of either the DIRM or OEN bits (input state is readable via
108 // the DATA_RO registers). Also, they state that the output drivers cannot
109 // be enabled if the TRISTATE bit is set on the pin in the SLCR unit.
110 //
111 // In practice, however, there seems to be a subtle, undocumented,
112 // difference between these bits (At least whent the GPIOs in question are
113 // MIOs, this behavior has not been verified on EMIOs).
114 //
115 // The OEN bit seems to do what you would want it to do (toggle the output
116 // drivers on and off). The desired drive state of the pin (reflected in
117 // the DATA and MASK_DATA registers) holds the user setting regardless of
118 // the state of the OEN bit, and the state of the line can be read via the
119 // DATA_RO bit.
120 //
121 // When the DIRM bit is cleared, however, the state of the line seems to
122 // become latched into the desired drive state of the pin instead of holding
123 // its last programmed state. For example; say the pin is being used to
124 // as a line in an open collector bus, such as i2c. The output drivers
125 // are enabled (OEN and DIRM == 1), and the pin is being driven low (DATA ==
126 // 0). When it comes time to release the line, if a user clears OEN, the
127 // driver will be disabled and the line will be pulled high by the external
128 // pullup. DATA will still read 0 (the desired drive state of the pin) and
129 // DATA_RO will read 1 (the actual state of the line). If, on the other
130 // hand, the user clears the DIRM bit instead of the OEN bit, the driver
131 // will be disabled and the line will be pulled high again, but this time
132 // DATA will latch the value of the line itself (DATA == 1, DATA_RO == 1).
133 //
134 // Because of this behvior, we NEVER use the DIRM bit to control the driver
135 // state of the pin. During init, set all pins to input mode by first
136 // clearing the OEN bits, and then making sure that the DIRM bits are all
137 // set.
138 for (unsigned int bank = 0; bank < 4; bank++) {
139 *REG32(GPIO_OEN(bank)) = 0x00000000;
140 *REG32(GPIO_DIRM(bank)) = 0xFFFFFFFF;
141 }
142}
143
144void zynq_gpio_early_init(void)
145{
146}
147
148void register_gpio_int_handler(unsigned gpio, int_handler handler, void *args)
149{
150 DEBUG_ASSERT(gpio < MAX_GPIO);
151 DEBUG_ASSERT(handler);
152
153 irq_callbacks[gpio].callback = handler;
154 irq_callbacks[gpio].args = args;
155}
156
157void unregister_gpio_int_handler(unsigned gpio)
158{
159 DEBUG_ASSERT(gpio < MAX_GPIO);
160
161 irq_callbacks[gpio].callback = NULL;
162 irq_callbacks[gpio].args = NULL;
163}
164
165int gpio_config(unsigned gpio, unsigned flags)
166{
167 DEBUG_ASSERT(gpio < MAX_GPIO);
168
169 uint16_t bank = extract_bank(gpio);
170 uint16_t bit = extract_bit(gpio);
171
172 /* MIO region, exclude EMIO. MIO needs to be configured before the GPIO block. */
173 if (bank < 2) {
174 // Preserve all of the fields of the current pin configuration, except
175 // for PULLUP and the MUX configurtaion. Force the mux config to select
176 // GPIO mode, and turn the pullup on or off as requested by the user.
177 uint32_t mio_cfg = SLCR_REG(MIO_PIN_00 + (gpio * 4));
178 mio_cfg &= MIO_TRI_ENABLE |
179 MIO_SPEED_FAST |
180 MIO_IO_TYPE_MASK |
181 MIO_DISABLE_RCVR;
182
183 // No need to set any bits in the L[0123]_SEL fields; we want them to
184 // all be zero.
185
186 if (flags & GPIO_PULLUP) {
187 mio_cfg |= MIO_PULLUP;
188 }
189
190 SLCR_REG(MIO_PIN_00 + (gpio * 4)) = mio_cfg;
191 }
192
193 if (flags & GPIO_OUTPUT || flags & GPIO_INPUT) {
194 if (flags & GPIO_OUTPUT && flags & GPIO_INPUT) {
195 printf("Cannot configure a gpio as both an input and output direction.\n");
196 return -1;
197 }
198
199 // Note(johngro): use only the OEN bit to control the output driver. Do
200 // not use the DIRM bit; see the note in zynq_gpio_init.
201 RMWREG32(GPIO_OEN(bank), bit, 1, ((flags & GPIO_OUTPUT) > 0));
202 }
203
204 if (flags & GPIO_EDGE || flags & GPIO_LEVEL) {
205 if (flags & GPIO_EDGE && flags & GPIO_LEVEL) {
206 printf("Cannot configure a gpio as both edge and level sensitive.\n");
207 return -1;
208 }
209 RMWREG32(GPIO_INT_TYPE(bank), bit, 1, ((flags & GPIO_EDGE) > 0));
210 }
211
212 if (flags & GPIO_RISING || flags & GPIO_FALLING) {
213 /* Zynq has a specific INT_ANY register for handling interrupts that trigger on both
214 * rising and falling edges, but it specifically must only be used in edge mode */
215 if (flags & GPIO_RISING && flags & GPIO_FALLING) {
216 if ((flags & GPIO_EDGE) == 0) {
217 printf("polarity must be rising or falling if level sensitivity is used.\n");
218 return -1;
219 }
220
221 RMWREG32(GPIO_INT_ANY(bank), bit, 1, 1);
222 } else {
223 RMWREG32(GPIO_INT_POLARITY(bank), bit, 1, ((flags & GPIO_RISING) > 0));
224 RMWREG32(GPIO_INT_ANY(bank), bit, 1, 0);
225 }
226 }
227
228 return 0;
229}
230
231void gpio_set(unsigned gpio, unsigned on)
232{
233 DEBUG_ASSERT(gpio < MAX_GPIO);
234
235 uint16_t bank = extract_bank(gpio);
236 uint16_t bit = extract_bit(gpio);
237 uintptr_t reg;
238
239 if (bit < 16) {
240 reg = GPIO_MASK_DATA_LSW(bank);
241 } else {
242 reg = GPIO_MASK_DATA_MSW(bank);
243 bit -= 16;
244 }
245
246 *REG32(reg) = (~(1 << bit) << 16) | (!!on << bit);
247}
248
249int gpio_get(unsigned gpio)
250{
251 DEBUG_ASSERT(gpio < MAX_GPIO);
252
253 uint16_t bank = extract_bank(gpio);
254 uint16_t bit = extract_bit(gpio);
255
256 return ((*REG32(GPIO_DATA_RO(bank)) & (1 << bit)) > 0);
257}
258
259#include <lib/console.h>
260#ifdef WITH_LIB_CONSOLE
261static int cmd_zynq_gpio(int argc, const cmd_args *argv)
262{
263 for (unsigned int bank = 0; bank < 4; bank++) {
264 printf("DIRM_%u (0x%08x): 0x%08x\n", bank, GPIO_DIRM(bank), *REG32(GPIO_DIRM(bank)));
265 printf("OEN_%u (0x%08x): 0x%08x\n", bank, GPIO_OEN(bank), *REG32(GPIO_OEN(bank)));
266 printf("MASK_DATA_LSW_%u (0x%08x): 0x%08x\n", bank, GPIO_MASK_DATA_LSW(bank), *REG32(GPIO_MASK_DATA_LSW(bank)));
267 printf("MASK_DATA_MSW_%u (0x%08x): 0x%08x\n", bank, GPIO_MASK_DATA_MSW(bank), *REG32(GPIO_MASK_DATA_MSW(bank)));
268 printf("DATA_%u (0x%08x): 0x%08x\n", bank, GPIO_DATA(bank), *REG32(GPIO_DATA(bank)));
269 printf("DATA_RO_%u (0x%08x): 0x%08x\n", bank, GPIO_DATA_RO(bank), *REG32(GPIO_DATA_RO(bank)));
270 printf("INT_MASK_%u (0x%08x): 0x%08x\n", bank, GPIO_INT_MASK(bank), *REG32(GPIO_INT_MASK(bank)));
271 printf("INT_STAT_%u (0x%08x): 0x%08x\n", bank, GPIO_INT_STAT(bank), *REG32(GPIO_INT_STAT(bank)));
272 printf("INT_TYPE_%u (0x%08x): 0x%08x\n", bank, GPIO_INT_TYPE(bank), *REG32(GPIO_INT_TYPE(bank)));
273 printf("INT_POLARITY_%u (0x%08x): 0x%08x\n", bank, GPIO_INT_POLARITY(bank), *REG32(GPIO_INT_POLARITY(bank)));
274 printf("INT_ANY_%u (0x%08x): 0x%08x\n", bank, GPIO_INT_ANY(bank), *REG32(GPIO_INT_ANY(bank)));
275 }
276 return 0;
277}
278STATIC_COMMAND_START
279#if LK_DEBUGLEVEL > 1
280STATIC_COMMAND("zynq_gpio", "Dump Zynq GPIO registers", &cmd_zynq_gpio)
281#endif
282STATIC_COMMAND_END(zynq_gpio);
283
284#endif