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
|
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
/*
* Mailbox interface with videocore gpu
*/
#define MAILBOX (VIRTIO+0xB880)
typedef struct Prophdr Prophdr;
typedef struct Fbinfo Fbinfo;
typedef struct Vgpio Vgpio;
enum {
Read = 0x00>>2,
Write = 0x00>>2,
Peek = 0x10>>2,
Sender = 0x14>>2,
Status = 0x18>>2,
Full = 1<<31,
Empty = 1<<30,
Config = 0x1C>>2,
NRegs = 0x20>>2,
ChanMask = 0xF,
ChanProps = 8,
ChanFb = 1,
Req = 0x0,
RspOk = 0x80000000,
TagResp = 1<<31,
TagGetfwrev = 0x00000001,
TagGetrev = 0x00010002,
TagGetmac = 0x00010003,
TagGetram = 0x00010005,
TagGetpower = 0x00020001,
TagSetpower = 0x00028001,
Powerwait = 1<<1,
TagGetclkstate = 0x00030001,
TagGetclkspd = 0x00030002,
TagGetclkmax = 0x00030004,
TagSetclkstate = 0x00038001,
TagSetclkspd = 0x00038002,
TagGetEgpioState= 0x00030041,
TagSetEgpioState= 0x00038041,
TagSetSdhostClk = 0x00038042,
TagGetEgpioConf = 0x00030043,
TagSetEgpioConf = 0x00038043,
TagGettemp = 0x00030006,
TagFballoc = 0x00040001,
TagFbfree = 0x00048001,
TagFbblank = 0x00040002,
TagGetres = 0x00040003,
TagSetres = 0x00048003,
TagGetvres = 0x00040004,
TagSetvres = 0x00048004,
TagGetdepth = 0x00040005,
TagSetdepth = 0x00048005,
TagGetrgb = 0x00040006,
TagSetrgb = 0x00048006,
TagGetGpio = 0x00040010,
Nvgpio = 2,
};
struct Fbinfo {
u32int xres;
u32int yres;
u32int xresvirtual;
u32int yresvirtual;
u32int pitch; /* returned by gpu */
u32int bpp;
u32int xoffset;
u32int yoffset;
u32int base; /* returned by gpu */
u32int screensize; /* returned by gpu */
};
struct Prophdr {
u32int len;
u32int req;
u32int tag;
u32int tagbuflen;
u32int taglen;
u32int data[1];
};
struct Vgpio {
u32int *counts;
u16int incs;
u16int decs;
int ison;
};
static Vgpio vgpio;
static void
vcwrite(uint chan, int val)
{
u32int *r;
r = (u32int*)MAILBOX + NRegs;
val &= ~ChanMask;
while(r[Status]&Full)
;
coherence();
r[Write] = val | chan;
}
static int
vcread(uint chan)
{
u32int *r;
int x;
r = (u32int*)MAILBOX;
do{
while(r[Status]&Empty)
;
coherence();
x = r[Read];
}while((x&ChanMask) != chan);
return x & ~ChanMask;
}
/*
* Property interface
*/
static int
vcreq(int tag, void *buf, int vallen, int rsplen)
{
uintptr r;
int n;
Prophdr *prop;
uintptr aprop;
static int busaddr = 1;
if(rsplen < vallen)
rsplen = vallen;
rsplen = (rsplen+3) & ~3;
prop = (Prophdr*)(VCBUFFER);
n = sizeof(Prophdr) + rsplen + 8;
memset(prop, 0, n);
prop->len = n;
prop->req = Req;
prop->tag = tag;
prop->tagbuflen = rsplen;
prop->taglen = vallen;
if(vallen > 0)
memmove(prop->data, buf, vallen);
cachedwbinvse(prop, n);
for(;;){
aprop = busaddr? dmaaddr(prop) : (uintptr)prop;
vcwrite(ChanProps, aprop);
r = vcread(ChanProps);
if(r == aprop)
break;
if(!busaddr)
return -1;
busaddr = 0;
}
cachedinvse(prop, n);
if(prop->req == RspOk &&
prop->tag == tag &&
(prop->taglen&TagResp)) {
if((n = prop->taglen & ~TagResp) < rsplen)
rsplen = n;
memmove(buf, prop->data, rsplen);
}else
rsplen = -1;
return rsplen;
}
/*
* Framebuffer
*/
static int
fbdefault(int *width, int *height, int *depth)
{
u32int buf[3];
char *p;
if(vcreq(TagGetres, &buf[0], 0, 2*4) != 2*4 ||
vcreq(TagGetdepth, &buf[2], 0, 4) != 4)
return -1;
*width = buf[0];
*height = buf[1];
if((p = getconf("bcm2708_fb.fbdepth")) != nil)
*depth = atoi(p);
else
*depth = buf[2];
return 0;
}
void*
fbinit(int set, int *width, int *height, int *depth)
{
Fbinfo *fi;
uintptr va;
if(!set)
fbdefault(width, height, depth);
/* Screen width must be a multiple of 16 */
*width &= ~0xF;
fi = (Fbinfo*)(VCBUFFER);
memset(fi, 0, sizeof(*fi));
fi->xres = fi->xresvirtual = *width;
fi->yres = fi->yresvirtual = *height;
fi->bpp = *depth;
cachedwbinvse(fi, sizeof(*fi));
vcwrite(ChanFb, dmaaddr(fi));
if(vcread(ChanFb) != 0)
return 0;
va = mmukmap(FRAMEBUFFER, (fi->base&~0xC0000000)|PHYSDRAM, fi->screensize);
if(va)
memset((char*)va, 0x7F, fi->screensize);
return (void*)va;
}
int
fbblank(int blank)
{
u32int buf[1];
buf[0] = blank;
if(vcreq(TagFbblank, buf, sizeof buf, sizeof buf) != sizeof buf)
return -1;
return buf[0] & 1;
}
/*
* Power management
*/
void
setpower(int dev, int on)
{
u32int buf[2];
buf[0] = dev;
buf[1] = Powerwait | (on? 1 : 0);
vcreq(TagSetpower, buf, sizeof buf, sizeof buf);
}
int
getpower(int dev)
{
u32int buf[2];
buf[0] = dev;
buf[1] = 0;
if(vcreq(TagGetpower, buf, sizeof buf[0], sizeof buf) != sizeof buf)
return -1;
return buf[0] & 1;
}
/*
* Get ethernet address (as hex string)
* [not reentrant]
*/
char *
getethermac(void)
{
uchar ea[8];
char *p;
int i;
static char buf[16];
memset(ea, 0, sizeof ea);
vcreq(TagGetmac, ea, 0, sizeof ea);
p = buf;
for(i = 0; i < 6; i++)
p += sprint(p, "%.2x", ea[i]);
return buf;
}
/*
* Get board revision
*/
uint
getboardrev(void)
{
u32int buf[1];
if(vcreq(TagGetrev, buf, 0, sizeof buf) != sizeof buf)
return 0;
return buf[0];
}
/*
* Get firmware revision
*/
uint
getfirmware(void)
{
u32int buf[1];
if(vcreq(TagGetfwrev, buf, 0, sizeof buf) != sizeof buf)
return 0;
return buf[0];
}
/*
* Get ARM ram
*/
void
getramsize(Confmem *mem)
{
u32int buf[2];
if(vcreq(TagGetram, buf, 0, sizeof buf) != sizeof buf)
return;
mem->base = buf[0];
mem->limit = buf[1];
}
/*
* Get clock rate
*/
ulong
getclkrate(int clkid)
{
u32int buf[2];
buf[0] = clkid;
if(vcreq(TagGetclkspd, buf, sizeof(buf[0]), sizeof(buf)) != sizeof buf)
return 0;
return buf[1];
}
/*
* Set clock rate to hz (or max speed if hz == 0)
*/
void
setclkrate(int clkid, ulong hz)
{
u32int buf[2];
buf[0] = clkid;
if(hz != 0)
buf[1] = hz;
else if(vcreq(TagGetclkmax, buf, sizeof(buf[0]), sizeof(buf)) != sizeof buf)
return;
vcreq(TagSetclkspd, buf, sizeof(buf), sizeof(buf));
}
/*
* Get cpu temperature
*/
uint
getcputemp(void)
{
u32int buf[2];
buf[0] = 0;
if(vcreq(TagGettemp, buf, sizeof(buf[0]), sizeof buf) != sizeof buf)
return 0;
return buf[1];
}
/*
* Virtual GPIO - used for ACT LED on pi3
*/
void
vgpinit(void)
{
u32int buf[1];
uintptr va;
buf[0] = 0;
if(vcreq(TagGetGpio, buf, 0, sizeof(buf)) != sizeof buf || buf[0] == 0)
return;
va = mmukmap(VGPIO, buf[0] & ~0xC0000000, BY2PG);
if(va == 0)
return;
vgpio.counts = (u32int*)va;
}
void
vgpset(uint port, int on)
{
if(vgpio.counts == nil || port >= Nvgpio || on == vgpio.ison)
return;
if(on)
vgpio.incs++;
else
vgpio.decs++;
vgpio.counts[port] = (vgpio.incs << 16) | vgpio.decs;
vgpio.ison = on;
}
/*
* Raspberry Pi GPIO expander (Pi 3 and 4)
*/
void
egpset(uint port, int on)
{
u32int buf[2];
if(port >= 8)
return;
buf[0] = 128 + port;
buf[1] = on;
vcreq(TagSetEgpioState, buf, sizeof(buf), sizeof(buf));
}
|