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
|
/*
* 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 <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <sys/mman.h>
/* uint32_t bank_con_addr; */
/* uint32_t bank_dat_addr; */
/* uint32_t bank_pud_addr; */
/* uint32_t bank_drv_addr; */
/* uint32_t bank_con_pdn_addr; */
/* uint32_t bank_pud_pdn_addr; */
/* if (!strcmp(bank, "gpj1")){ */
/* bank_con_addr = 0x11400260; /\* GPJ1CON *\/ */
/* bank_dat_addr = 0x11400264; /\* GPJ1DAT *\/ */
/* bank_pud_addr = 0x11400268; /\* GPJ1PUD *\/ */
/* bank_drv_addr = 0x1140026c; /\* GPJ1DRV *\/ */
/* bank_con_pdn_addr = 0x11440270; /\* GPJ1CONPDN *\/ */
/* bank_pud_pdn_addr = 0x11440274; /\* GPJ1PUDPDN *\/ */
/* } else { */
/* errno = -EINVAL; */
/* return NULL; */
/* } */
enum gpio_direction {
GPIO_INPUT = 0,
GPIO_OUTPUT = 1,
/* The rest of the values up to 0xf seem to be dependent on the pin */
};
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 decode_bank_con(char *bank, uint32_t gpio_offset, uint32_t *virt_addr)
{
int shift = 0;
int result = 0;
char* direction;
uint32_t *addr;
uint32_t value;
printf("%s: Mapped at %p\n", __func__, virt_addr);
if (!strcmp(bank, "gpj1")) {
addr = virt_addr + 0x260;
value = *virt_addr;
printf("%s: %s: offset %p value: 0x%x\n", __func__, bank,
addr - virt_addr, value);
} else {
errno = EINVAL;
return -1;
}
result = ((value >> (gpio_offset * 4)) & 0xf);
direction = gpio_direction_str(result);
printf("%s: %s[%d]: %s\n", __func__, bank, gpio_offset,
gpio_direction_str(result));
return 0;
}
static void *mmap_gpio_bank(char *devmem, int fd, size_t page_size, char *bank)
{
/* TODO: also map GPIO_right
* GPIO_right: base_addr: 0x11000000 len: 0x400000
* GPIO_left: base_addr: 0x11400000 len: 0x400000
*/
uint32_t base_addr = 0x11400000;
size_t len = 0x400000;
void *map_base_addr;
uint32_t *virt_addr;
int rc;
map_base_addr = mmap(0, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd,
base_addr & ~(page_size -1));
if (map_base_addr == MAP_FAILED) {
rc = errno;
printf("mmap on %s failed with error %d: %s\n",
devmem, rc, strerror(rc));
}
virt_addr = map_base_addr + (base_addr & 4095) + 0x260;
return virt_addr;
}
int dump_gpio_infos(char *devmem, int fd, size_t page_size, char *bank,
uint32_t gpio_offset)
{
uint32_t *addr;
int rc;
addr = mmap_gpio_bank(devmem, fd, page_size, "gpj1");
printf("%s: Mapped at %p\n", __func__, addr);
rc = decode_bank_con("gpj1", 1, addr);
if (rc == -1) {
rc = errno;
printf("decode_gpio(\"%s\", %d, 0x%x); "
"failed with with error %d: %s\n",
"gpj1", 1, *addr, rc, strerror(rc));
return -1;
}
}
|