Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/soc/gpio.c
1476 views
1
/*
2
* Copyright (c) 2018 naehrwert
3
* Copyright (c) 2019-2023 CTCaer
4
*
5
* This program is free software; you can redistribute it and/or modify it
6
* under the terms and conditions of the GNU General Public License,
7
* version 2, as published by the Free Software Foundation.
8
*
9
* This program is distributed in the hope it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12
* more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
#include <soc/gpio.h>
19
#include <soc/t210.h>
20
21
#define GPIO_BANK_IDX(port) ((port) >> 2)
22
#define GPIO_PORT_OFFSET(port) ((GPIO_BANK_IDX(port) << 8) + (((port) % 4) << 2))
23
24
#define GPIO_CNF_OFFSET(port) (0x00 + GPIO_PORT_OFFSET(port))
25
#define GPIO_OE_OFFSET(port) (0x10 + GPIO_PORT_OFFSET(port))
26
#define GPIO_OUT_OFFSET(port) (0x20 + GPIO_PORT_OFFSET(port))
27
#define GPIO_IN_OFFSET(port) (0x30 + GPIO_PORT_OFFSET(port))
28
#define GPIO_INT_STA_OFFSET(port) (0x40 + GPIO_PORT_OFFSET(port))
29
#define GPIO_INT_ENB_OFFSET(port) (0x50 + GPIO_PORT_OFFSET(port))
30
#define GPIO_INT_LVL_OFFSET(port) (0x60 + GPIO_PORT_OFFSET(port))
31
#define GPIO_INT_CLR_OFFSET(port) (0x70 + GPIO_PORT_OFFSET(port))
32
33
#define GPIO_CNF_MASKED_OFFSET(port) (0x80 + GPIO_PORT_OFFSET(port))
34
#define GPIO_OE_MASKED_OFFSET(port) (0x90 + GPIO_PORT_OFFSET(port))
35
#define GPIO_OUT_MASKED_OFFSET(port) (0xA0 + GPIO_PORT_OFFSET(port))
36
#define GPIO_INT_STA_MASKED_OFFSET(port) (0xC0 + GPIO_PORT_OFFSET(port))
37
#define GPIO_INT_ENB_MASKED_OFFSET(port) (0xD0 + GPIO_PORT_OFFSET(port))
38
#define GPIO_INT_LVL_MASKED_OFFSET(port) (0xE0 + GPIO_PORT_OFFSET(port))
39
40
#define GPIO_DB_CTRL_OFFSET(port) (0xB0 + GPIO_PORT_OFFSET(port))
41
#define GPIO_DB_CNT_OFFSET(port) (0xF0 + GPIO_PORT_OFFSET(port))
42
43
#define GPIO_IRQ_BANK1 32
44
#define GPIO_IRQ_BANK2 33
45
#define GPIO_IRQ_BANK3 34
46
#define GPIO_IRQ_BANK4 35
47
#define GPIO_IRQ_BANK5 55
48
#define GPIO_IRQ_BANK6 87
49
#define GPIO_IRQ_BANK7 89
50
#define GPIO_IRQ_BANK8 125
51
52
static u8 gpio_bank_irq_ids[8] = {
53
GPIO_IRQ_BANK1, GPIO_IRQ_BANK2, GPIO_IRQ_BANK3, GPIO_IRQ_BANK4,
54
GPIO_IRQ_BANK5, GPIO_IRQ_BANK6, GPIO_IRQ_BANK7, GPIO_IRQ_BANK8
55
};
56
57
void gpio_config(u32 port, u32 pins, int mode)
58
{
59
const u32 offset = GPIO_CNF_OFFSET(port);
60
61
if (mode)
62
GPIO(offset) |= pins;
63
else
64
GPIO(offset) &= ~pins;
65
66
(void)GPIO(offset); // Commit the write.
67
}
68
69
void gpio_output_enable(u32 port, u32 pins, int enable)
70
{
71
const u32 port_offset = GPIO_OE_OFFSET(port);
72
73
if (enable)
74
GPIO(port_offset) |= pins;
75
else
76
GPIO(port_offset) &= ~pins;
77
78
(void)GPIO(port_offset); // Commit the write.
79
}
80
81
void gpio_write(u32 port, u32 pins, int high)
82
{
83
const u32 port_offset = GPIO_OUT_OFFSET(port);
84
85
if (high)
86
GPIO(port_offset) |= pins;
87
else
88
GPIO(port_offset) &= ~pins;
89
90
(void)GPIO(port_offset); // Commit the write.
91
}
92
93
void gpio_direction_input(u32 port, u32 pins)
94
{
95
gpio_config(port, pins, GPIO_MODE_GPIO);
96
gpio_output_enable(port, pins, GPIO_OUTPUT_DISABLE);
97
}
98
99
void gpio_direction_output(u32 port, u32 pins, int high)
100
{
101
gpio_config(port, pins, GPIO_MODE_GPIO);
102
gpio_write(port, pins, high);
103
gpio_output_enable(port, pins, GPIO_OUTPUT_ENABLE);
104
}
105
106
int gpio_read(u32 port, u32 pins)
107
{
108
const u32 port_offset = GPIO_IN_OFFSET(port);
109
110
return (GPIO(port_offset) & pins) ? 1 : 0;
111
}
112
113
void gpio_set_debounce(u32 port, u32 pins, u32 ms)
114
{
115
const u32 db_ctrl_offset = GPIO_DB_CTRL_OFFSET(port);
116
const u32 db_cnt_offset = GPIO_DB_CNT_OFFSET(port);
117
118
if (ms)
119
{
120
if (ms > 255)
121
ms = 255;
122
123
// Debounce time affects all pins of the same port.
124
GPIO(db_cnt_offset) = ms;
125
GPIO(db_ctrl_offset) = (pins << 8) | pins;
126
}
127
else
128
GPIO(db_ctrl_offset) = (pins << 8) | 0;
129
130
(void)GPIO(db_ctrl_offset); // Commit the write.
131
}
132
133
static void _gpio_interrupt_clear(u32 port, u32 pins)
134
{
135
const u32 port_offset = GPIO_INT_CLR_OFFSET(port);
136
137
GPIO(port_offset) |= pins;
138
139
(void)GPIO(port_offset); // Commit the write.
140
}
141
142
int gpio_interrupt_status(u32 port, u32 pins)
143
{
144
const u32 port_offset = GPIO_INT_STA_OFFSET(port);
145
const u32 enabled_mask = GPIO(GPIO_INT_ENB_OFFSET(port)) & pins;
146
147
int status = ((GPIO(port_offset) & pins) && enabled_mask) ? 1 : 0;
148
149
// Clear the interrupt status.
150
if (status)
151
_gpio_interrupt_clear(port, pins);
152
153
return status;
154
}
155
156
void gpio_interrupt_enable(u32 port, u32 pins, int enable)
157
{
158
const u32 port_offset = GPIO_INT_ENB_OFFSET(port);
159
160
// Clear any possible stray interrupt.
161
_gpio_interrupt_clear(port, pins);
162
163
if (enable)
164
GPIO(port_offset) |= pins;
165
else
166
GPIO(port_offset) &= ~pins;
167
168
(void)GPIO(port_offset); // Commit the write.
169
}
170
171
void gpio_interrupt_level(u32 port, u32 pins, int high, int edge, int delta)
172
{
173
const u32 port_offset = GPIO_INT_LVL_OFFSET(port);
174
175
u32 val = GPIO(port_offset);
176
177
if (high)
178
val |= pins;
179
else
180
val &= ~pins;
181
182
if (edge)
183
val |= pins << 8;
184
else
185
val &= ~(pins << 8);
186
187
if (delta)
188
val |= pins << 16;
189
else
190
val &= ~(pins << 16);
191
192
GPIO(port_offset) = val;
193
194
(void)GPIO(port_offset); // Commit the write.
195
196
// Clear any possible stray interrupt.
197
_gpio_interrupt_clear(port, pins);
198
}
199
200
u32 gpio_get_bank_irq_id(u32 port)
201
{
202
const u32 bank_idx = GPIO_BANK_IDX(port);
203
204
return gpio_bank_irq_ids[bank_idx];
205
}
206
207