-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathchipid.c
More file actions
239 lines (214 loc) · 6.24 KB
/
chipid.c
File metadata and controls
239 lines (214 loc) · 6.24 KB
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
#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "chipid.h"
#include "hal/common.h"
#include "tools.h"
int chip_generation;
char chip_name[128];
char nor_chip_name[128];
char nor_chip_id[128];
static char chip_manufacturer[128];
static long get_uart0_address() {
char buf[256];
if (!line_from_file("/proc/iomem", "^(\\w+)-.+:.+uart",
buf, sizeof(buf))) {
return -1;
}
return strtol(buf, NULL, 16);
}
typedef struct {
const char *pattern;
bool (*detect_fn)(char *);
const char *override_vendor;
void (*setup_hal_fn)(void);
} manufacturers_t;
static const manufacturers_t manufacturers[] = {
#if defined(mips) || defined(__mips__) || defined(__mips)
{"isvp", ingenic_detect_cpu, VENDOR_INGENIC, setup_hal_ingenic},
{"ingenic", ingenic_detect_cpu, VENDOR_INGENIC, setup_hal_ingenic},
#endif
#ifdef __arm__
{"SStar", sstar_detect_cpu, VENDOR_SSTAR, sstar_setup_hal},
{"MStar", mstar_detect_cpu, NULL, sstar_setup_hal},
{"Novatek", novatek_detect_cpu, VENDOR_NOVATEK, novatek_setup_hal},
{"Grain", gm_detect_cpu, VENDOR_GM, gm_setup_hal},
{"FH", fh_detect_cpu, VENDOR_FH, fh_setup_hal},
{NULL /* Generic */, rockchip_detect_cpu, VENDOR_ROCKCHIP, rockchip_setup_hal},
{"Xilinx", xilinx_detect_cpu, NULL, xilinx_setup_hal},
{"BCM", bcm_detect_cpu, VENDOR_BCM, bcm_setup_hal},
{NULL, allwinner_detect_cpu, VENDOR_ALLWINNER, allwinner_setup_hal}
#endif
#if defined(__aarch64__) || defined(_M_ARM64)
{NULL, novatek_detect_cpu, VENDOR_NOVATEK, novatek_setup_hal},
{NULL, tegra_detect_cpu, "Nvidia", tegra_setup_hal},
#endif
};
static bool generic_detect_cpu() {
char buf[256] = "unknown";
strcpy(chip_name, "unknown");
bool res = line_from_file("/proc/cpuinfo", "Hardware.+:.(\\w+)",
buf, sizeof(buf));
if (!res) {
res = line_from_file("/proc/cpuinfo", "vendor_id.+:.(\\w+)",
buf, sizeof(buf));
}
if (!res) {
res = line_from_file("/proc/cpuinfo", "machine.+:.(\\w+)",
buf, sizeof(buf));
}
strcpy(chip_manufacturer, buf);
for (size_t i = 0; i < ARRCNT(manufacturers); i++) {
if (manufacturers[i].pattern &&
strncmp(manufacturers[i].pattern, chip_manufacturer,
strlen(manufacturers[i].pattern)))
continue;
if (manufacturers[i].detect_fn(chip_name)) {
if (manufacturers[i].override_vendor)
strcpy(chip_manufacturer, manufacturers[i].override_vendor);
manufacturers[i].setup_hal_fn();
return true;
}
}
return false;
}
static bool detect_and_set(const char *manufacturer,
bool (*detect_fn)(char *, uint32_t),
void (*setup_hal_fn)(void), uint32_t base) {
bool ret = detect_fn(chip_name, base);
if (ret) {
strcpy(chip_manufacturer, manufacturer);
setup_hal_fn();
}
return ret;
}
static bool hw_detect_system() {
long uart_base = get_uart0_address();
switch (uart_base) {
#ifdef __arm__
// xm510
case 0x10030000:
return detect_and_set("Xiongmai", xm_detect_cpu, setup_hal_xm, 0);
// hi3516cv610
case 0x11040000: {
int ret = detect_and_set(VENDOR_HISI, hisi_detect_cpu, setup_hal_hisi,
0x11020000);
return ret;
}
// hi3516cv300
case 0x12100000:
// hi3516ev200
case 0x120a0000:
case 0x12040000: {
int ret = detect_and_set(VENDOR_HISI, hisi_detect_cpu, setup_hal_hisi,
0x12020000);
if (ret && *chip_name == '7')
strcpy(chip_manufacturer, VENDOR_GOKE);
return ret;
}
// hi3536c
case 0x12080000:
return detect_and_set(VENDOR_HISI, hisi_detect_cpu, setup_hal_hisi,
0x12050000);
// hi3516av100
// hi3516cv100
// hi3518ev200
case 0x20080000:
return detect_and_set(VENDOR_HISI, hisi_detect_cpu, setup_hal_hisi,
0x20050000);
#endif
default:
return generic_detect_cpu();
}
}
static char sysid[255];
const char *getchipname() {
// if system wasn't detected previously
if (*sysid)
return sysid;
setup_hal_fallback();
if (!hw_detect_system())
return NULL;
if (!strcmp(chip_manufacturer, VENDOR_HISI))
strcpy(sysid, "hi");
else if (!strcmp(chip_manufacturer, VENDOR_GOKE))
strcpy(sysid, "gk");
int nlen = strlen(sysid);
lsnprintf(sysid + nlen, sizeof(sysid) - nlen, "%s", chip_name);
return sysid;
}
const char *getchipfamily() {
const char *chip_name = getchipname();
switch (chip_generation) {
case HISI_V1:
return "hi3516cv100";
case HISI_V2A:
return "hi3516av100";
case HISI_V2:
return "hi3516cv200";
case HISI_V3A:
return "hi3519v100";
case HISI_V3:
return "hi3516cv300";
case HISI_V4A:
return "hi3516cv500";
case HISI_V4:
if (*chip_name == 'g')
return "gk7205v200";
else
return "hi3516ev200";
case INFINITY3:
return "infinity3";
case INFINITY5:
return "infinity5";
case INFINITY6:
return "infinity6";
case INFINITY6B:
return "infinity6b0";
case INFINITY6C:
return "infinity6c";
case INFINITY6E:
return "infinity6e";
case T10:
return "t10";
case T20:
return "t20";
case T21:
return "t21";
case T23:
return "t23";
case T30:
return "t30";
case T31:
return "t31";
case T40:
return "t40";
case T41:
return "t41";
case RV1106:
return "rv1106";
default:
return chip_name;
}
}
const char *getchipvendor() {
getchipname();
return chip_manufacturer;
}
#ifndef STANDALONE_LIBRARY
cJSON *detect_chip() {
cJSON *j_inner = cJSON_CreateObject();
ADD_PARAM("vendor", chip_manufacturer);
ADD_PARAM("model", chip_name);
if (hal_chip_properties)
hal_chip_properties(j_inner);
return j_inner;
}
#endif