-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
bcm_host.c
318 lines (272 loc) · 9.01 KB
/
bcm_host.c
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
/*
Copyright (c) 2012, Broadcom Europe Ltd
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include "include/bcm_host.h"
#include "interface/vmcs_host/vc_dispmanx.h"
#include "interface/vmcs_host/vc_vchi_gencmd.h"
#include "interface/vmcs_host/vc_vchi_bufman.h"
#include "interface/vmcs_host/vc_tvservice.h"
#include "interface/vmcs_host/vc_cecservice.h"
#include "interface/vchiq_arm/vchiq_if.h"
static VCHI_INSTANCE_T global_initialise_instance;
static VCHI_CONNECTION_T *global_connection;
int32_t graphics_get_display_size( const uint16_t display_number,
uint32_t *width,
uint32_t *height)
{
DISPMANX_DISPLAY_HANDLE_T display_handle = 0;
DISPMANX_MODEINFO_T mode_info;
int32_t success = -1;
// Display must be opened first.
display_handle = vc_dispmanx_display_open(display_number);
if (display_handle) {
success = vc_dispmanx_display_get_info(display_handle, &mode_info);
if( success >= 0 )
{
if( NULL != width )
{
*width = mode_info.width;
}
if( NULL != height )
{
*height = mode_info.height;
}
}
vc_dispmanx_display_close(display_handle);
display_handle = 0;
}
return success;
}
void host_app_message_handler(void)
{
printf("host_app_message_handler\n");
}
void vc_host_get_vchi_state(VCHI_INSTANCE_T *initialise_instance, VCHI_CONNECTION_T **connection)
{
if (initialise_instance) *initialise_instance = global_initialise_instance;
if (connection) *connection = global_connection;
}
void bcm_host_init(void)
{
VCHIQ_INSTANCE_T vchiq_instance;
static int initted;
int success = -1;
char response[ 128 ];
if (initted)
return;
initted = 1;
vcos_init();
if (vchiq_initialise(&vchiq_instance) != VCHIQ_SUCCESS)
{
printf("* failed to open vchiq instance\n");
exit(-1);
}
vcos_log("vchi_initialise");
success = vchi_initialise( &global_initialise_instance);
vcos_assert(success == 0);
vchiq_instance = (VCHIQ_INSTANCE_T)global_initialise_instance;
global_connection = vchi_create_connection(single_get_func_table(),
vchi_mphi_message_driver_func_table());
vcos_log("vchi_connect");
vchi_connect(&global_connection, 1, global_initialise_instance);
vc_vchi_gencmd_init (global_initialise_instance, &global_connection, 1);
vc_vchi_dispmanx_init (global_initialise_instance, &global_connection, 1);
vc_vchi_tv_init (global_initialise_instance, &global_connection, 1);
vc_vchi_cec_init (global_initialise_instance, &global_connection, 1);
//vc_vchi_bufman_init (global_initialise_instance, &global_connection, 1);
if ( success == 0 )
{
success = vc_gencmd( response, sizeof(response), "set_vll_dir /sd/vlls" );
vcos_assert( success == 0 );
}
}
void bcm_host_deinit(void)
{
}
// Fix linking problems. These are referenced by libs, but shouldn't be called
void wfc_stream_await_buffer(void * stream)
{
vcos_assert(0);
}
static unsigned get_dt_ranges(const char *filename, unsigned offset)
{
unsigned address = ~0;
FILE *fp = fopen(filename, "rb");
if (fp)
{
unsigned char buf[4];
fseek(fp, offset, SEEK_SET);
if (fread(buf, 1, sizeof buf, fp) == sizeof buf)
address = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3] << 0;
fclose(fp);
}
return address;
}
unsigned bcm_host_get_peripheral_address(void)
{
unsigned address = get_dt_ranges("/proc/device-tree/soc/ranges", 4);
if (address == 0)
address = get_dt_ranges("/proc/device-tree/soc/ranges", 8);
return address == ~0 ? 0x20000000 : address;
}
unsigned bcm_host_get_peripheral_size(void)
{
unsigned address = get_dt_ranges("/proc/device-tree/soc/ranges", 4);
address = get_dt_ranges("/proc/device-tree/soc/ranges", (address == 0) ? 12 : 8);
return address == ~0 ? 0x01000000 : address;
}
unsigned bcm_host_get_sdram_address(void)
{
unsigned address = get_dt_ranges("/proc/device-tree/axi/vc_mem/reg", 8);
return address == ~0 ? 0x40000000 : address;
}
static int read_string_from_file(const char *filename, const char *format, unsigned int *value)
{
FILE *fin;
char str[256];
int found = 0;
fin = fopen(filename, "rt");
if (fin == NULL)
return 0;
while (fgets(str, sizeof(str), fin) != NULL)
{
if (value)
{
if (sscanf(str, format, value) == 1)
{
found = 1;
break;
}
}
else
{
if (!strcmp(str, format))
{
found = 1;
break;
}
}
}
fclose(fin);
return found;
}
static unsigned int get_revision_code()
{
static unsigned int revision_num = -1;
unsigned int num;
if (revision_num == -1 && read_string_from_file("/proc/cpuinfo", "Revision : %x", &num))
revision_num = num;
return revision_num;
}
/* Returns the type of the Pi being used
*/
int bcm_host_get_model_type(void)
{
static int model_type = -1;
if (model_type != -1)
return model_type;
unsigned int revision_num = get_revision_code();
if (!revision_num)
model_type = 0;
// Check for old/new style revision code. Bit 23 will be guaranteed one for new style
else if (revision_num & 0x800000)
model_type = (revision_num & 0xff0) >> 4;
else
{
// Mask off warrantee and overclock bits.
revision_num &= 0xffffff;
// Map old style to new Type code
if (revision_num < 2 || revision_num > 21)
return 0;
static const unsigned char type_map[] =
{
1, // B rev 1.0 2
1, // B rev 1.0 3
1, // B rev 2.0 4
1, // B rev 2.0 5
1, // B rev 2.0 6
0, // A rev 2 7
0, // A rev 2 8
0, // A rev 2 9
0, 0, 0, // unused a,b,c
1, // B rev 2.0 d
1, // B rev 2.0 e
1, // B rev 2.0 f
3, // B+ rev 1.2 10
6, // CM1 11
2, // A+ rev1.1 12
3, // B+ rev 1.2 13
6, // CM1 14
2 // A+ 15
};
model_type = type_map[revision_num-2];
}
return model_type;
}
/* Test if the host is a member of the Pi 4 family (4B, 400 and CM4)
*/
int bcm_host_is_model_pi4(void)
{
return bcm_host_get_processor_id() == BCM_HOST_PROCESSOR_BCM2711;
}
/* returns the processor ID
*/
int bcm_host_get_processor_id(void)
{
unsigned int revision_num = get_revision_code();
if (revision_num & 0x800000)
{
return (revision_num & 0xf000) >> 12;
}
else
{
// Old style number only used 2835
return BCM_HOST_PROCESSOR_BCM2835;
}
}
static int bcm_host_is_fkms_or_kms_active(int kms)
{
if (!read_string_from_file("/proc/device-tree/soc/v3d@7ec00000/status", "okay", NULL) &&
!read_string_from_file("/proc/device-tree/v3dbus/v3d@7ec04000/status", "okay", NULL) &&
!read_string_from_file("/proc/device-tree/axi/v3d@2000000/status", "okay", NULL))
return 0;
else
return (read_string_from_file("/proc/device-tree/soc/firmwarekms@7e600000/status", "okay", NULL) ||
read_string_from_file("/proc/device-tree/soc/firmwarekms@7d503000/status", "okay", NULL)) ^ kms;
}
int bcm_host_is_fkms_active(void)
{
static int active = -1;
if (active == -1)
active = bcm_host_is_fkms_or_kms_active(0);
return active;
}
int bcm_host_is_kms_active(void)
{
static int active = -1;
if (active == -1)
active = bcm_host_is_fkms_or_kms_active(1);
return active;
}