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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/* GPIO module for MEC1322 */
#include "common.h"
#include "gpio.h"
#include "hooks.h"
#include "registers.h"
#include "system.h"
#include "task.h"
#include "timer.h"
#include "util.h"
struct gpio_int_mapping {
int8_t girq_id;
int8_t port_offset;
};
/* Mapping from GPIO port to GIRQ info */
static const struct gpio_int_mapping int_map[22] = {
{11, 0}, {11, 0}, {11, 0}, {11, 0},
{10, 4}, {10, 4}, {10, 4}, {-1, -1},
{-1, -1}, {-1, -1}, {9, 10}, {9, 10},
{9, 10}, {9, 10}, {8, 14}, {8, 14},
{8, 14}, {-1, -1}, {-1, -1}, {-1, -1},
{20, 20}, {20, 20}
};
void gpio_set_alternate_function(uint32_t port, uint32_t mask, int func)
{
int i;
uint32_t val;
while (mask) {
i = __builtin_ffs(mask) - 1;
val = MEC1322_GPIO_CTL(port, i);
val &= ~((1 << 12) | (1 << 13));
/* mux_control = 0 indicates GPIO */
if (func > 0)
val |= (func & 0x3) << 12;
MEC1322_GPIO_CTL(port, i) = val;
mask &= ~(1 << i);
}
}
test_mockable int gpio_get_level(enum gpio_signal signal)
{
uint32_t mask = gpio_list[signal].mask;
int i;
uint32_t val;
if (mask == 0)
return 0;
i = GPIO_MASK_TO_NUM(mask);
val = MEC1322_GPIO_CTL(gpio_list[signal].port, i);
return (val & (1 << 24)) ? 1 : 0;
}
void gpio_set_level(enum gpio_signal signal, int value)
{
uint32_t mask = gpio_list[signal].mask;
int i;
if (mask == 0)
return;
i = GPIO_MASK_TO_NUM(mask);
if (value)
MEC1322_GPIO_CTL(gpio_list[signal].port, i) |= (1 << 16);
else
MEC1322_GPIO_CTL(gpio_list[signal].port, i) &= ~(1 << 16);
}
void gpio_set_flags_by_mask(uint32_t port, uint32_t mask, uint32_t flags)
{
int i;
uint32_t val;
while (mask) {
i = GPIO_MASK_TO_NUM(mask);
mask &= ~(1 << i);
val = MEC1322_GPIO_CTL(port, i);
/*
* Select open drain first, so that we don't glitch the signal
* when changing the line to an output.
*/
if (flags & GPIO_OPEN_DRAIN)
val |= (1 << 8);
else
val &= ~(1 << 8);
if (flags & GPIO_OUTPUT) {
val |= (1 << 9);
val &= ~(1 << 10);
} else {
val &= ~(1 << 9);
val |= (1 << 10);
}
/* Handle pullup / pulldown */
if (flags & GPIO_PULL_UP)
val = (val & ~0x3) | 0x1;
else if (flags & GPIO_PULL_DOWN)
val = (val & ~0x3) | 0x2;
else
val &= ~0x3;
/* Set up interrupt */
if (flags & (GPIO_INT_F_RISING | GPIO_INT_F_FALLING))
val |= (1 << 7);
else
val &= ~(1 << 7);
val &= ~(0x7 << 4);
if ((flags & GPIO_INT_F_RISING) && (flags & GPIO_INT_F_FALLING))
val |= 0x7 << 4;
else if (flags & GPIO_INT_F_RISING)
val |= 0x5 << 4;
else if (flags & GPIO_INT_F_FALLING)
val |= 0x6 << 4;
else if (flags & GPIO_INT_F_HIGH)
val |= 0x1 << 4;
else if (!(flags & GPIO_INT_F_LOW)) /* No interrupt flag set */
val |= 0x4 << 4;
/* Set up level */
if (flags & GPIO_HIGH)
val |= (1 << 16);
else if (flags & GPIO_LOW)
val &= ~(1 << 16);
MEC1322_GPIO_CTL(port, i) = val;
}
}
int gpio_enable_interrupt(enum gpio_signal signal)
{
int i, port, girq_id, bit_id;
if (gpio_list[signal].mask == 0)
return EC_SUCCESS;
i = GPIO_MASK_TO_NUM(gpio_list[signal].mask);
port = gpio_list[signal].port;
girq_id = int_map[port].girq_id;
bit_id = (port - int_map[port].port_offset) * 8 + i;
MEC1322_INT_ENABLE(girq_id) |= (1 << bit_id);
MEC1322_INT_BLK_EN |= (1 << girq_id);
return EC_SUCCESS;
}
int gpio_disable_interrupt(enum gpio_signal signal)
{
int i, port, girq_id, bit_id;
if (gpio_list[signal].mask == 0)
return EC_SUCCESS;
i = GPIO_MASK_TO_NUM(gpio_list[signal].mask);
port = gpio_list[signal].port;
girq_id = int_map[port].girq_id;
bit_id = (port - int_map[port].port_offset) * 8 + i;
MEC1322_INT_DISABLE(girq_id) = (1 << bit_id);
return EC_SUCCESS;
}
void gpio_pre_init(void)
{
int i;
int flags;
int is_warm = system_is_reboot_warm();
const struct gpio_info *g = gpio_list;
for (i = 0; i < GPIO_COUNT; i++, g++) {
flags = g->flags;
if (flags & GPIO_DEFAULT)
continue;
/*
* If this is a warm reboot, don't set the output levels or
* we'll shut off the AP.
*/
if (is_warm)
flags &= ~(GPIO_LOW | GPIO_HIGH);
gpio_set_flags_by_mask(g->port, g->mask, flags);
/* Use as GPIO, not alternate function */
gpio_set_alternate_function(g->port, g->mask, -1);
}
}
/* Clear any interrupt flags before enabling GPIO interrupt */
#define ENABLE_GPIO_GIRQ(x) \
do { \
MEC1322_INT_SOURCE(x) |= MEC1322_INT_RESULT(x); \
task_enable_irq(MEC1322_IRQ_GIRQ ## x); \
} while (0)
static void gpio_init(void)
{
ENABLE_GPIO_GIRQ(8);
ENABLE_GPIO_GIRQ(9);
ENABLE_GPIO_GIRQ(10);
ENABLE_GPIO_GIRQ(11);
ENABLE_GPIO_GIRQ(20);
}
DECLARE_HOOK(HOOK_INIT, gpio_init, HOOK_PRIO_DEFAULT);
/*****************************************************************************/
/* Interrupt handlers */
/**
* Handler for each GIRQ interrupt. This reads and clears the interrupt bits for
* the GIRQ interrupt, then finds and calls the corresponding GPIO interrupt
* handlers.
*
* @param girq GIRQ index
* @param port_offset GPIO port offset for the given GIRQ
*/
static void gpio_interrupt(int girq, int port_offset)
{
int i, bit;
const struct gpio_info *g = gpio_list;
uint32_t sts = MEC1322_INT_RESULT(girq);
MEC1322_INT_SOURCE(girq) |= sts;
for (i = 0; i < GPIO_IH_COUNT && sts; ++i, ++g) {
bit = (g->port - port_offset) * 8 + __builtin_ffs(g->mask) - 1;
if (sts & (1 << bit))
gpio_irq_handlers[i](i);
sts &= ~(1 << bit);
}
}
#define GPIO_IRQ_FUNC(irqfunc, girq, port_offset) \
void irqfunc(void) \
{ \
gpio_interrupt(girq, port_offset); \
}
GPIO_IRQ_FUNC(__girq_8_interrupt, 8, 14);
GPIO_IRQ_FUNC(__girq_9_interrupt, 9, 10);
GPIO_IRQ_FUNC(__girq_10_interrupt, 10, 4);
GPIO_IRQ_FUNC(__girq_11_interrupt, 11, 0);
GPIO_IRQ_FUNC(__girq_20_interrupt, 20, 20);
#undef GPIO_IRQ_FUNC
/*
* Declare IRQs. Nesting this macro inside the GPIO_IRQ_FUNC macro works
* poorly because DECLARE_IRQ() stringizes its inputs.
*/
DECLARE_IRQ(MEC1322_IRQ_GIRQ8, __girq_8_interrupt, 1);
DECLARE_IRQ(MEC1322_IRQ_GIRQ9, __girq_9_interrupt, 1);
DECLARE_IRQ(MEC1322_IRQ_GIRQ10, __girq_10_interrupt, 1);
DECLARE_IRQ(MEC1322_IRQ_GIRQ11, __girq_11_interrupt, 1);
DECLARE_IRQ(MEC1322_IRQ_GIRQ20, __girq_20_interrupt, 1);
|