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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
/*
* Copyright (C) 2021 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "exynos4412_gpios_data.h"
/*
* Generic functions
*/
static int get_2bit_field_value(uint32_t register_value, int gpio_offset)
{
return ((register_value >> (2*gpio_offset)) & 3);
}
/*
* <GPIO>CON
*/
static char *gpio_direction_str(int value)
{
/* The GP<x>CON field size is 4 bits. So the maximum value it can take
* is 0xf. So to convert that to string, if we add the trailing \0, the
* size of the the string is 4.
*/
static char result[4] = { 0 };
switch (value) {
case GPIO_INPUT:
return "GPIO_INPUT";
case GPIO_OUTPUT:
return "GPIO_OUTPUT";
default:
snprintf(result, sizeof(result), "0x%x", value);
return (char*)result;
}
}
static int get_con_value(uint32_t register_value, int gpio_offset)
{
return ((register_value >> (gpio_offset * 4)) & 0xf);
}
/*
* <GPIO>DAT
*/
static char *gpio_value_str(int value)
{
/* The GP<x>DAT field size is 1 bit. But when the pin is not input nor
* output, its value is undefined.
*/
switch (value) {
case GPIO_VALUE_LOW:
return "GPIO_VALUE_LOW";
case GPIO_VALUE_HIGH:
return "GPIO_VALUE_HIGH";
case GPIO_VALUE_UNDEFINED:
return "GPIO_VALUE_UNDEFINED";
}
assert(false); /* This should normally not be reached */
return ""; /* Avoid -Werror=return-type */
}
static int get_dat_value(uint32_t register_value, int gpio_offset)
{
return ((register_value >> (gpio_offset)) & 1);
}
/*
* <GPIO>DRV
*/
static char *gpio_drive_str(int value)
{
/* The GP<x>DRV field size is 2 bit. */
switch (value) {
case GPIO_DRIVE_1X:
return "GPIO_DRIVE_1X";
case GPIO_DRIVE_2X:
return "GPIO_DRIVE_2X";
case GPIO_DRIVE_3X:
return "GPIO_DRIVE_3X";
case GPIO_DRIVE_4X:
return "GPIO_DRIVE_4X";
}
assert(false); /* This should normally not be reached */
return ""; /* Avoid -Werror=return-type */
}
/*
* <GPIO>PUD
*/
static char *gpio_resistor_str(int value)
{
/* The GP<x>PUD field size is 2 bit. */
switch (value) {
case GPIO_RESISTORS_PULLDOWN_DISABLE:
return "GPIO_RESISTORS_PULLDOWN_DISABLE";
case GPIO_RESISTORS_ENABLE_PULL_DOWN:
return "GPIO_RESISTORS_ENABLE_PULL_DOWN";
case GPIO_RESISTORS_RESERVED:
return "GPIO_RESISTORS_RESERVED";
case GPIO_RESISTORS_ENABLE_PULL_UP:
return "GPIO_RESISTORS_ENABLE_PULL_UP";
}
assert(false); /* This should normally not be reached */
return ""; /* Avoid -Werror=return-type */
}
/*
* <GPIO>CONPDN
*/
static char *gpio_power_down_str(int value)
{
/* The GP<x>CON field size is 2 bits. */
switch (value) {
case GPIO_POWER_DOWN_OUTPUT_LOW:
return "GPIO_POWER_DOWN_OUTPUT_LOW";
case GPIO_POWER_DOWN_OUTPUT_HIGH:
return "GPIO_POWER_DOWN_OUTPUT_HIGH";
case GPIO_POWER_DOWN_INPUT:
return "GPIO_POWER_DOWN_INPUT";
case GPIO_POWER_DOWN_PREVIOUS_STATE:
return "GPIO_POWER_DOWN_PREVIOUS_STATE";
}
assert(false); /* This should normally not be reached */
return ""; /* Avoid -Werror=return-type */
}
struct gpio_bank_data gpio_banks_data[] = {
{
.name = "gpf0",
.nr_gpios = 8,
.base = 0x11400000,
.offset = 0x0180,
},
{
.name = "gpf1",
.nr_gpios = 8,
.base = 0x11400000,
.offset = 0x01a0,
},
{
.name = "gpf2",
.nr_gpios = 8,
.base = 0x11400000,
.offset = 0x01c0,
},
{
.name = "gpf3",
.nr_gpios = 6,
.base = 0x11400000,
.offset = 0x01e0,
},
{
.name = "gpj0",
.nr_gpios = 8,
.base = 0x11400000,
.offset = 0x0240,
},
{
.name = "gpj1",
.nr_gpios = 5,
.base = 0x11400000,
.offset = 0x0260,
},
{
.name = "gpl0",
.nr_gpios = 7,
.base = 0x11000000,
.offset = 0x00c0,
},
{
.name = "gpl1",
.nr_gpios = 2,
.base = 0x11000000,
.offset = 0x00e0,
},
{
.name = "gpl2",
.nr_gpios = 8,
.base = 0x11000000,
.offset = 0x0100,
},
{
.name = "gpm0",
.nr_gpios = 8,
.base = 0x11000000,
.offset = 0x0260,
},
{
.name = "gpm1",
.nr_gpios = 7,
.base = 0x11000000,
.offset = 0x0280,
},
{
.name = "gpm2",
.nr_gpios = 5,
.base = 0x11000000,
.offset = 0x02a0,
},
{
.name = "gpm3",
.nr_gpios = 8,
.base = 0x11000000,
.offset = 0x02c0,
},
{
.name = "gpm4",
.nr_gpios = 8,
.base = 0x11000000,
.offset = 0x02e0,
},
{
.name = "gpx0",
.nr_gpios = 8,
.base = 0x11000000,
.offset = 0x0c00,
},
{
.name = "gpx1",
.nr_gpios = 8,
.base = 0x11000000,
.offset = 0x0c20,
},
{
.name = "gpx2",
.nr_gpios = 8,
.base = 0x11000000,
.offset = 0x0c40,
},
{
.name = "gpx3",
.nr_gpios = 8,
.base = 0x11000000,
.offset = 0x0c60,
},
{
/* Sentinel */
},
};
struct gpio_register_data gpio_registers_data[] = {
{
.name = "con",
.get_value = get_con_value,
.offset = 0x00,
.value_str = gpio_direction_str,
},
{
.name = "dat",
.get_value = get_dat_value,
.offset = 0x04,
.value_str = gpio_value_str,
},
{
.name = "pud",
.get_value = get_2bit_field_value,
.offset = 0x08,
.value_str = gpio_resistor_str,
},
{
.name = "drv",
.get_value = get_2bit_field_value,
.offset = 0x0c,
.value_str = gpio_drive_str,
},
{
.name = "con-pdn",
.get_value = get_2bit_field_value,
.offset = 0x10,
.value_str = gpio_power_down_str,
},
{
.name = "pud-pdn",
.get_value = get_2bit_field_value,
.offset = 0x14,
.value_str = gpio_resistor_str,
},
};
struct gpio_bank_data *get_gpio_bank_data(char *bank)
{
int i = 0;
while (1) {
if (gpio_banks_data[i].name == NULL)
break;
if (!strcmp(gpio_banks_data[i].name, bank))
return &(gpio_banks_data[i]);
i++;
}
return NULL;
}
struct gpio_register_data *get_gpio_register_data(char *register_name)
{
int i = 0;
while (1) {
if (gpio_registers_data[i].name == NULL)
break;
if (!strcmp(gpio_registers_data[i].name, register_name))
return &(gpio_registers_data[i]);
i++;
}
return NULL;
}
void print_gpio_banks_data(void)
{
int i = 0;
while (1) {
if (gpio_banks_data[i].name == NULL)
break;
printf("%s[%d] {\n", gpio_banks_data[i].name,
gpio_banks_data[i].nr_gpios);
printf("\toffset: 0x%x\n", gpio_banks_data[i].offset);
printf("}\n");
i++;
}
}
int decode_gpio_data(int debug, char *bank, uint32_t gpio_offset,
char* gpio_register_name, uint32_t *virt_addr)
{
struct gpio_bank_data *gpio_bank_data;
struct gpio_register_data *gpio_register_data;
uint32_t *addr;
uint32_t register_value;
gpio_bank_data = get_gpio_bank_data(bank);
if (gpio_bank_data == NULL) {
errno = EINVAL;
return -1;
}
gpio_register_data = get_gpio_register_data(gpio_register_name);
if (gpio_register_data == NULL) {
errno = EINVAL;
return -1;
}
addr = virt_addr + gpio_bank_data->offset + gpio_register_data->offset;
register_value = *virt_addr;
if (debug)
printf("%s: %s: offset 0x%x value: 0x%x\n", __func__, bank,
addr - virt_addr, register_value);
return gpio_register_data->get_value(register_value, gpio_offset);
}
char *gpio_data_str(char* gpio_register_name, uint32_t register_value)
{
struct gpio_register_data *gpio_register_data;
gpio_register_data = get_gpio_register_data(gpio_register_name);
if (gpio_register_data == NULL) {
errno = EINVAL;
return NULL;
}
return gpio_register_data->value_str(register_value);
}
off_t get_gpio_register_offset(int debug, char *gpio_bank_name,
char *register_name)
{
struct gpio_bank_data *gpio_bank_data;
struct gpio_register_data *gpio_register_data;
off_t gpio_hardware_block_offset;
off_t offset = 0;
gpio_bank_data = get_gpio_bank_data(gpio_bank_name);
if (gpio_bank_data == NULL) {
errno = EINVAL;
return -1;
}
gpio_register_data = get_gpio_register_data(register_name);
if (gpio_register_data == NULL) {
errno = EINVAL;
return -1;
}
if (gpio_bank_data->base == 0x11400000)
gpio_hardware_block_offset = 0x400000;
offset = gpio_hardware_block_offset + gpio_bank_data->offset + gpio_register_data->offset;
return offset;
}
|