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
272
273
274
275
276
277
278
279
280
|
/* SPDX-License-Identifier: GPL-2.0-only */
#include <assert.h>
#include <device/mmio.h>
#include <gpio.h>
enum {
GPIO_DIRECTION_IN = 0,
GPIO_DIRECTION_OUT = 1,
};
enum {
GPIO_MODE = 0,
};
static void pos_bit_calc(gpio_t gpio, u32 *pos, u32 *bit)
{
*pos = gpio.id / MAX_GPIO_REG_BITS;
*bit = gpio.id % MAX_GPIO_REG_BITS;
}
static void pos_bit_calc_for_mode(gpio_t gpio, u32 *pos, u32 *bit)
{
*pos = gpio.id / MAX_GPIO_MODE_PER_REG;
*bit = (gpio.id % MAX_GPIO_MODE_PER_REG) * GPIO_MODE_BITS;
}
static s32 gpio_set_dir(gpio_t gpio, u32 dir)
{
u32 pos;
u32 bit;
u32 *reg;
pos_bit_calc(gpio, &pos, &bit);
if (dir == GPIO_DIRECTION_IN)
reg = &mtk_gpio->dir[pos].rst;
else
reg = &mtk_gpio->dir[pos].set;
write32(reg, 1L << bit);
return 0;
}
void gpio_set_mode(gpio_t gpio, int mode)
{
u32 pos;
u32 bit;
u32 mask = (1L << GPIO_MODE_BITS) - 1;
pos_bit_calc_for_mode(gpio, &pos, &bit);
clrsetbits32(&mtk_gpio->mode[pos].val, mask << bit, mode << bit);
}
int gpio_get(gpio_t gpio)
{
u32 pos;
u32 bit;
u32 *reg;
u32 data;
pos_bit_calc(gpio, &pos, &bit);
reg = &mtk_gpio->din[pos].val;
data = read32(reg);
return (data & (1L << bit)) ? 1 : 0;
}
void gpio_set(gpio_t gpio, int output)
{
u32 pos;
u32 bit;
u32 *reg;
pos_bit_calc(gpio, &pos, &bit);
if (output == 0)
reg = &mtk_gpio->dout[pos].rst;
else
reg = &mtk_gpio->dout[pos].set;
write32(reg, 1L << bit);
}
void gpio_input_pulldown(gpio_t gpio)
{
gpio_set_pull(gpio, GPIO_PULL_ENABLE, GPIO_PULL_DOWN);
gpio_set_dir(gpio, GPIO_DIRECTION_IN);
gpio_set_mode(gpio, GPIO_MODE);
}
void gpio_input_pullup(gpio_t gpio)
{
gpio_set_pull(gpio, GPIO_PULL_ENABLE, GPIO_PULL_UP);
gpio_set_dir(gpio, GPIO_DIRECTION_IN);
gpio_set_mode(gpio, GPIO_MODE);
}
void gpio_input(gpio_t gpio)
{
gpio_set_pull(gpio, GPIO_PULL_DISABLE, GPIO_PULL_DOWN);
gpio_set_dir(gpio, GPIO_DIRECTION_IN);
gpio_set_mode(gpio, GPIO_MODE);
}
void gpio_output(gpio_t gpio, int value)
{
gpio_set_pull(gpio, GPIO_PULL_DISABLE, GPIO_PULL_DOWN);
gpio_set(gpio, value);
gpio_set_dir(gpio, GPIO_DIRECTION_OUT);
gpio_set_mode(gpio, GPIO_MODE);
}
int gpio_eint_poll(gpio_t gpio)
{
u32 pos;
u32 bit;
u32 status;
struct eint_regs *mtk_eint;
gpio_calc_eint_pos_bit(gpio, &pos, &bit);
mtk_eint = gpio_get_eint_reg(gpio);
assert(mtk_eint);
status = (read32(&mtk_eint->sta.regs[pos]) >> bit) & 0x1;
if (status)
write32(&mtk_eint->ack.regs[pos], 1 << bit);
return status;
}
void gpio_eint_configure(gpio_t gpio, enum gpio_irq_type type)
{
u32 pos;
u32 bit, mask;
struct eint_regs *mtk_eint;
gpio_calc_eint_pos_bit(gpio, &pos, &bit);
mtk_eint = gpio_get_eint_reg(gpio);
assert(mtk_eint);
mask = 1 << bit;
/* Make it an input first. */
gpio_input_pullup(gpio);
write32(&mtk_eint->d0en[pos], mask);
switch (type) {
case IRQ_TYPE_EDGE_FALLING:
write32(&mtk_eint->sens_clr.regs[pos], mask);
write32(&mtk_eint->pol_clr.regs[pos], mask);
break;
case IRQ_TYPE_EDGE_RISING:
write32(&mtk_eint->sens_clr.regs[pos], mask);
write32(&mtk_eint->pol_set.regs[pos], mask);
break;
case IRQ_TYPE_LEVEL_LOW:
write32(&mtk_eint->sens_set.regs[pos], mask);
write32(&mtk_eint->pol_clr.regs[pos], mask);
break;
case IRQ_TYPE_LEVEL_HIGH:
write32(&mtk_eint->sens_set.regs[pos], mask);
write32(&mtk_eint->pol_set.regs[pos], mask);
break;
}
write32(&mtk_eint->mask_clr.regs[pos], mask);
}
static inline bool is_valid_drv(uint8_t drv)
{
return drv <= GPIO_DRV_16_MA;
}
static inline bool is_valid_drv_adv(enum gpio_drv_adv drv)
{
return drv <= GPIO_DRV_ADV_1_MA && drv >= GPIO_DRV_ADV_125_UA;
}
int gpio_set_driving(gpio_t gpio, uint8_t drv)
{
uint32_t mask;
const struct gpio_drv_info *info = get_gpio_driving_info(gpio.id);
const struct gpio_drv_info *adv_info = get_gpio_driving_adv_info(gpio.id);
void *reg, *reg_adv, *reg_addr;
if (!info)
return -1;
if (!is_valid_drv(drv))
return -1;
if (info->width == 0)
return -1;
mask = BIT(info->width) - 1;
/* Check setting value is not beyond width */
if ((uint32_t)drv > mask)
return -1;
reg_addr = gpio_find_reg_addr(gpio);
reg = reg_addr + info->offset;
clrsetbits32(reg, mask << info->shift, drv << info->shift);
/* Disable EH if supported */
if (adv_info && adv_info->width != 0) {
reg_adv = reg_addr + adv_info->offset;
clrbits32(reg_adv, BIT(adv_info->shift));
}
return 0;
}
int gpio_get_driving(gpio_t gpio)
{
const struct gpio_drv_info *info = get_gpio_driving_info(gpio.id);
void *reg;
if (!info)
return -1;
if (info->width == 0)
return -1;
reg = gpio_find_reg_addr(gpio) + info->offset;
return (read32(reg) >> info->shift) & (BIT(info->width) - 1);
}
int gpio_set_driving_adv(gpio_t gpio, enum gpio_drv_adv drv)
{
uint32_t mask;
const struct gpio_drv_info *adv_info = get_gpio_driving_adv_info(gpio.id);
void *reg_adv;
if (!adv_info)
return -1;
if (!is_valid_drv_adv(drv))
return -1;
if (adv_info->width == 0)
return -1;
/* Not include EH bit (the lowest bit) */
if ((uint32_t)drv > (BIT(adv_info->width - 1) - 1))
return -1;
reg_adv = gpio_find_reg_addr(gpio) + adv_info->offset;
mask = BIT(adv_info->width) - 1;
/* EH enable */
drv = (drv << 1) | BIT(0);
clrsetbits32(reg_adv, mask << adv_info->shift, drv << adv_info->shift);
return 0;
}
int gpio_get_driving_adv(gpio_t gpio)
{
const struct gpio_drv_info *adv_info = get_gpio_driving_adv_info(gpio.id);
void *reg_adv;
uint32_t drv;
if (!adv_info)
return -1;
if (adv_info->width == 0)
return -1;
reg_adv = gpio_find_reg_addr(gpio) + adv_info->offset;
drv = (read32(reg_adv) >> adv_info->shift) & (BIT(adv_info->width) - 1);
/* Drop EH bit */
return drv >> 1;
}
|