1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
/* Copyright 2019 The ChromiumOS Authors
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/* MAX32660 GPIO module for Chrome EC */
#include "clock.h"
#include "console.h"
#include "common.h"
#include "gpio.h"
#include "hooks.h"
#include "switch.h"
#include "task.h"
#include "timer.h"
#include "util.h"
#include "registers.h"
#include "gpio_regs.h"
#define CPRINTF(format, args...) cprintf(CC_GPIO, format, ##args)
#define CPRINTS(format, args...) cprints(CC_GPIO, format, ##args)
/* 0-terminated list of GPIO base addresses */
static mxc_gpio_regs_t *gpio_bases[] = { MXC_GPIO0, 0 };
void gpio_set_alternate_function(uint32_t port, uint32_t mask,
enum gpio_alternate_func func)
{
mxc_gpio_regs_t *gpio = MXC_GPIO_GET_GPIO(port);
switch (func) {
case GPIO_ALT_FUNC_1:
gpio->en_clr = mask;
gpio->en1_clr = mask;
break;
case GPIO_ALT_FUNC_2:
gpio->en_clr = mask;
gpio->en1_set = mask;
break;
case GPIO_ALT_FUNC_3:
gpio->en_set = mask;
gpio->en1_set = mask;
break;
default:
/* Default as input */
gpio->out_en_clr = mask;
gpio->en_set = mask;
gpio->en1_clr = mask;
break;
}
}
test_mockable int gpio_get_level(enum gpio_signal signal)
{
mxc_gpio_regs_t *gpio = MXC_GPIO_GET_GPIO(gpio_list[signal].port);
return (gpio->in & gpio_list[signal].mask);
}
void gpio_set_level(enum gpio_signal signal, int value)
{
mxc_gpio_regs_t *gpio = MXC_GPIO_GET_GPIO(gpio_list[signal].port);
if (value) {
gpio->out_set = gpio_list[signal].mask;
} else {
gpio->out_clr = gpio_list[signal].mask;
}
}
void gpio_set_flags_by_mask(uint32_t port, uint32_t mask, uint32_t flags)
{
mxc_gpio_regs_t *gpio = MXC_GPIO_GET_GPIO(port);
/* Setup for either an output or input. */
if (flags & GPIO_OUTPUT) {
gpio->out_en_set = mask;
gpio->en_set = mask;
gpio->en1_clr = mask;
} else {
gpio->out_en_clr = mask;
gpio->en_set = mask;
gpio->en1_clr = mask;
}
/* Handle pull up, pull down or neither. */
if (flags & GPIO_PULL_UP) {
gpio->pad_cfg1 |= mask;
gpio->pad_cfg2 &= ~mask;
gpio->ps |= mask;
} else if (flags & GPIO_PULL_DOWN) {
gpio->pad_cfg1 &= ~mask;
gpio->pad_cfg2 |= mask;
gpio->ps &= ~mask;
} else {
/* No pull up/down */
gpio->pad_cfg1 &= ~mask;
gpio->pad_cfg2 &= ~mask;
gpio->ps &= ~mask;
}
/* Handle setting up level interrupts. */
if (flags & GPIO_INT_F_HIGH) {
/* Level triggered mode. */
gpio->int_mod &= ~mask;
/* Level high generates interrupt. */
gpio->int_pol |= mask;
} else if (flags & GPIO_INT_F_LOW) {
/* Level triggered mode. */
gpio->int_mod &= ~mask;
/* Level low generates interrupt. */
gpio->int_pol &= ~mask;
}
/* Handle setting up edge interrupts. */
if (flags & GPIO_INT_F_RISING) {
/* Edge triggered mode. */
gpio->int_mod |= mask;
/* Rising edge generates interrupt. */
gpio->int_pol |= mask;
} else if (flags & GPIO_INT_F_FALLING) {
/* Edge triggered mode. */
gpio->int_mod |= mask;
/* Falling edge generates interrupt. */
gpio->int_pol &= ~mask;
}
/* Handle interrupting on both edges. */
if ((flags & GPIO_INT_F_RISING) && (flags & GPIO_INT_F_FALLING)) {
/* Dual edge triggered mode. */
gpio->int_dual_edge |= mask;
} else {
/* Not dual edge triggered mode. */
gpio->int_dual_edge &= ~mask;
}
/* Set the gpio pin high or low. */
if (flags & GPIO_HIGH) {
gpio->out_set = mask;
} else if (flags & GPIO_LOW) {
gpio->out_clr = mask;
}
}
int gpio_enable_interrupt(enum gpio_signal signal)
{
mxc_gpio_regs_t *gpio = MXC_GPIO_GET_GPIO(gpio_list[signal].port);
gpio->int_en_set = gpio_list[signal].mask;
return EC_SUCCESS;
}
int gpio_disable_interrupt(enum gpio_signal signal)
{
mxc_gpio_regs_t *gpio = MXC_GPIO_GET_GPIO(gpio_list[signal].port);
gpio->int_en_clr = gpio_list[signal].mask;
return EC_SUCCESS;
}
int gpio_clear_pending_interrupt(enum gpio_signal signal)
{
mxc_gpio_regs_t *gpio = MXC_GPIO_GET_GPIO(gpio_list[signal].port);
gpio->int_clr = gpio_list[signal].mask;
return EC_SUCCESS;
}
void gpio_pre_init(void)
{
const struct gpio_info *g = gpio_list;
int i;
/* Mask all GPIO interrupts */
for (i = 0; gpio_bases[i]; i++)
gpio_bases[i]->int_en = 0;
/* Set all GPIOs to defaults */
for (i = 0; i < GPIO_COUNT; i++, g++) {
int flags = g->flags;
if (flags & GPIO_DEFAULT)
continue;
/* Use as GPIO, not alternate function */
gpio_set_alternate_function(g->port, g->mask,
GPIO_ALT_FUNC_NONE);
/* Set up GPIO based on flags */
gpio_set_flags_by_mask(g->port, g->mask, flags);
}
}
static void gpio_init(void)
{
/*
* Enable global GPIO0 Port interrupt. Note that interrupts still need
* to be enabled at the per pin level.
*/
task_enable_irq(EC_GPIO0_IRQn);
}
DECLARE_HOOK(HOOK_INIT, gpio_init, HOOK_PRIO_DEFAULT);
/*****************************************************************************/
/* Interrupt handlers */
/**
* Handle a GPIO interrupt.
*
* port GPIO port
* mis Masked interrupt status value for that port
*/
static void gpio_interrupt(int port, uint32_t mis)
{
int i = 0;
const struct gpio_info *g = gpio_list;
for (i = 0; i < GPIO_IH_COUNT && mis; i++, g++) {
if (port == g->port && (mis & g->mask)) {
gpio_irq_handlers[i](i);
mis &= ~g->mask;
}
}
}
/**
* Handlers for each GPIO port. Read the interrupt status, call the common GPIO
* interrupt handler and clear the GPIO hardware interrupt status.
*/
#define GPIO_IRQ_FUNC(irqfunc, gpiobase) \
static void irqfunc(void) \
{ \
mxc_gpio_regs_t *gpio = MXC_GPIO_GET_GPIO(gpiobase); \
uint32_t mis = gpio->int_stat; \
gpio_interrupt(gpiobase, mis); \
gpio->int_clr = mis; \
}
GPIO_IRQ_FUNC(__gpio_0_interrupt, PORT_0);
#undef GPIO_IRQ_FUNC
DECLARE_IRQ(EC_GPIO0_IRQn, __gpio_0_interrupt, 1);
|