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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
|
/*
* omap3530 clocks
*
* timers count up to zero.
*
* the source clock signals for the timers are sometimes selectable. for
* WDTIMER[23] and GPTIMER12, it's always the 32kHz clock. for the
* others, it can be the 32kHz clock or the system clock. we use only
* WDTIMER2 and GPTIMER[12], and configure GPTIMER[12] in archomap.c to
* use the 32kHZ clock. WDTIMER1 is not accessible to us on GP
* (general-purpose) omaps.
*/
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "arm.h"
enum {
Debug = 0,
Tn0 = PHYSTIMER1,
Tn1 = PHYSTIMER2,
/* irq 36 is watchdog timer module 3 overflow */
Tn0irq = 37, /* base IRQ for all timers */
Freebase = 1, /* base of free-running timer */
/*
* clock is 32K (32,768) Hz, so one tick is 30.517µs,
* so 327.68 ticks is 10ms, 32.768 ticks is 1ms.
*/
Clockfreqbase = 32 * 1024, /* base rate in Hz */
Tcycles = Clockfreqbase / HZ, /* cycles per clock tick */
MinPeriod = (Tcycles / 100 < 2? 2: Tcycles / 100),
MaxPeriod = Tcycles,
Dogtimeout = 20 * Clockfreqbase, /* was 4 s.; must be ≤ 21 s. */
};
enum {
/* ticpcfg bits */
Noidle = 1<<3,
Softreset = 1<<1,
/* tistat bits */
Resetdone = 1<<0,
/* tisr/tier bits */
Ovf_it = 1<<1, /* gp: overflow intr */
Mat_it = 1<<0, /* gp: match intr */
Wdovf_it = 1<<0, /* wdog: overflow intr */
/* tclr bits */
Ar = 1<<1, /* gp only: autoreload mode overflow */
St = 1<<0, /* gp only: start the timer */
};
/* omap35x timer registers */
typedef struct Timerregs Timerregs;
struct Timerregs {
/* common to all timers, gp and watchdog */
uchar pad0[0x10];
ulong ticpcfg;
ulong tistat; /* ro: low bit: reset done */
ulong tisr;
ulong tier;
ulong twer;
ulong tclr;
ulong tcrr; /* counter: cycles to zero */
ulong tldr;
ulong ttgr; /* trigger */
ulong twps; /* ro: write posted pending */
/* gp timers only, unused by us */
ulong tmar; /* value to compare with counter */
ulong tcar1; /* ro */
ulong tsicr;
ulong tcar2; /* ro */
union {
ulong tpir; /* gp: 1 ms tick generation: +ve */
ulong wspr; /* wdog: start/stop control */
};
ulong tnir; /* 1 ms tick generation: -ve */
ulong tcvr; /* 1 ms tick generation: next counter value */
ulong tocr; /* intr mask for n ticks */
ulong towr;
};
static int ticks; /* for sanity checking; m->ticks doesn't always get called */
static Lock clklck;
static ulong rdcycles(void), rdbaseticks(void);
/* write a watchdog timer's start/stop register */
static void
wdogwrss(Timerregs *tn, ulong val)
{
while (tn->twps & (1 << 4)) /* pending write to start/stop reg? */
;
tn->wspr = val;
coherence();
while (tn->twps & (1 << 4)) /* pending write to start/stop reg? */
;
}
static void
resetwait(Timerregs *tn)
{
long bound;
for (bound = 400*Mhz; !(tn->tistat & Resetdone) && bound > 0; bound--)
;
if (bound <= 0)
iprint("clock reset didn't complete\n");
}
static void
wdogoff(Timerregs *tn)
{
resetwait(tn);
wdogwrss(tn, 0xaaaa); /* magic off sequence */
wdogwrss(tn, 0x5555);
tn->tldr = 1;
coherence();
tn->tcrr = 1; /* paranoia */
coherence();
}
static void wdogassure(void);
static void
wdogon(Timerregs *tn)
{
static int beenhere;
resetwait(tn);
tn->tldr = -Dogtimeout;
tn->tcrr = -Dogtimeout;
coherence();
wdogwrss(tn, 0xbbbb); /* magic on sequence */
wdogwrss(tn, 0x4444); /* magic on sequence */
if (!beenhere) {
beenhere = 1;
/* touching the dog is not quick, so do it infrequently */
addclock0link(wdogassure, HZ);
}
}
static void
wdogassure(void) /* reset the watch dog's counter */
{
Timerregs *tn;
tn = (Timerregs *)PHYSWDOG;
wdogoff(tn);
tn->tcrr = -Dogtimeout;
coherence();
wdogon(tn);
}
static void
clockintr(Ureg* ureg, void *arg)
{
Timerregs *tn;
static int nesting;
ticks++;
coherence();
if (nesting == 0) { /* if the clock interrupted itself, bail out */
++nesting;
timerintr(ureg, 0);
--nesting;
}
tn = arg;
tn->tisr = Ovf_it; /* dismiss the interrupt */
coherence();
}
static void
clockreset(Timerregs *tn)
{
if (probeaddr((uintptr)&tn->ticpcfg) < 0)
panic("no clock at %#p", tn);
tn->ticpcfg = Softreset | Noidle;
coherence();
resetwait(tn);
tn->tier = tn->tclr = 0;
coherence();
}
/* stop clock interrupts and disable the watchdog timer */
void
clockshutdown(void)
{
clockreset((Timerregs *)PHYSWDT2);
wdogoff((Timerregs *)PHYSWDT2);
clockreset((Timerregs *)PHYSWDT3);
wdogoff((Timerregs *)PHYSWDT3);
clockreset((Timerregs *)Tn0);
clockreset((Timerregs *)Tn1);
}
enum {
Instrs = 10*Mhz,
};
static long
issue1loop(void)
{
register int i;
long st;
st = rdbaseticks();
i = Instrs;
do {
--i; --i; --i; --i; --i;
--i; --i; --i; --i;
} while(--i >= 0);
return rdbaseticks() - st;
}
static long
issue2loop(void)
{
register int i, j;
long st;
st = rdbaseticks();
i = Instrs / 2;
j = 0;
do {
--i; --j; --i; --j;
--i; --j; --i; --j;
--j;
} while(--i >= 0);
return rdbaseticks() - st;
}
/* estimate instructions/s. using 32kHz clock */
static void
guessmips(long (*loop)(void), char *lab)
{
int s;
long tcks;
do {
s = splhi();
tcks = loop();
splx(s);
if (tcks < 0)
iprint("again...");
} while (tcks < 0);
/*
* Instrs instructions took tcks ticks @ Clockfreqbase Hz.
*/
s = ((vlong)Clockfreqbase * Instrs) / tcks / 1000000;
if (Debug)
iprint("%ud mips (%s-issue)", s, lab);
USED(s);
}
void
clockinit(void)
{
int i, s;
Timerregs *tn;
clockshutdown();
/* turn cycle counter on */
cpwrsc(0, CpCLD, CpCLDena, CpCLDenacyc, 1<<31);
/* turn all counters on and clear the cycle counter */
cpwrsc(0, CpCLD, CpCLDena, CpCLDenapmnc, 1<<2 | 1);
/* let users read the cycle counter directly */
cpwrsc(0, CpCLD, CpCLDena, CpCLDenapmnc, 1);
ilock(&clklck);
m->fastclock = 1;
m->ticks = ticks = 0;
/*
* T0 is a freerunning timer (cycle counter); it wraps,
* automatically reloads, and does not dispatch interrupts.
*/
tn = (Timerregs *)Tn0;
tn->tcrr = Freebase; /* count up to 0 */
tn->tldr = Freebase;
coherence();
tn->tclr = Ar | St;
iunlock(&clklck);
/*
* T1 is the interrupting timer and does not participate
* in measuring time. It is initially set to HZ.
*/
tn = (Timerregs *)Tn1;
irqenable(Tn0irq+1, clockintr, tn, "clock");
ilock(&clklck);
tn->tcrr = -Tcycles; /* approx.; count up to 0 */
tn->tldr = -Tcycles;
coherence();
tn->tclr = Ar | St;
coherence();
tn->tier = Ovf_it;
coherence();
iunlock(&clklck);
/*
* verify sanity of timer1
*/
s = spllo(); /* risky */
for (i = 0; i < 5 && ticks == 0; i++) {
delay(10);
cachedwbinvse(&ticks, sizeof ticks);
}
splx(s);
if (ticks == 0) {
if (tn->tcrr == 0)
panic("clock not interrupting");
else if (tn->tcrr == tn->tldr)
panic("clock not ticking at all");
#ifdef PARANOID
else
panic("clock running very slowly");
#endif
}
guessmips(issue1loop, "single");
if (Debug)
iprint(", ");
guessmips(issue2loop, "dual");
if (Debug)
iprint("\n");
/*
* m->delayloop should be the number of delay loop iterations
* needed to consume 1 ms. 2 is min. instructions in the delay loop.
*/
m->delayloop = m->cpuhz / (1000 * 2);
// iprint("m->delayloop = %lud\n", m->delayloop);
/*
* desynchronize the processor clocks so that they all don't
* try to resched at the same time.
*/
delay(m->machno*2);
}
void
watchdoginit(void)
{
wdogassure();
}
ulong
µs(void)
{
return fastticks2us(fastticks(nil));
}
void
timerset(Tval next)
{
long offset;
Timerregs *tn = (Timerregs *)Tn1;
static Lock setlck;
ilock(&setlck);
offset = next - fastticks(nil);
if(offset < MinPeriod)
offset = MinPeriod;
else if(offset > MaxPeriod)
offset = MaxPeriod;
tn->tcrr = -offset;
coherence();
iunlock(&setlck);
}
static ulong
rdcycles(void)
{
ulong v;
/* reads 32-bit cycle counter (counting up) */
v = cprdsc(0, CpCLD, CpCLDcyc, 0);
/* keep it positive; prevent m->fastclock ever going to 0 */
return v == 0? 1: v;
}
static ulong
rdbaseticks(void)
{
ulong v;
v = ((Timerregs *)Tn0)->tcrr; /* tcrr should be counting up */
/* keep it positive; prevent m->fastclock ever going to 0 */
return v == 0? 1: v;
}
ulong
perfticks(void)
{
return rdcycles();
}
long
lcycles(void)
{
return perfticks();
}
/*
* until 5[cal] inline vlong ops, avoid them where possible,
* they are currently slow function calls.
*/
typedef union Counter Counter;
union Counter {
uvlong uvl;
struct { /* little-endian */
ulong low;
ulong high;
};
};
enum {
Fastvlongops = 0,
};
uvlong
fastticks(uvlong *hz)
{
Counter now, sclnow;
if(hz)
*hz = m->cpuhz;
ilock(&clklck);
if (m->ticks > HZ/10 && m->fastclock == 0)
panic("fastticks: zero m->fastclock; ticks %lud fastclock %#llux",
m->ticks, m->fastclock);
now.uvl = m->fastclock;
now.low = rdcycles();
if(now.uvl < m->fastclock) /* low bits must have wrapped */
now.high++;
m->fastclock = now.uvl;
coherence();
sclnow.uvl = now.uvl;
iunlock(&clklck);
return sclnow.uvl;
}
void
microdelay(int l)
{
int i;
l = l * (vlong)m->delayloop / 1000;
if(l <= 0)
l = 1;
for(i = 0; i < l; i++)
;
}
void
delay(int l)
{
ulong i, j;
j = m->delayloop;
while(l-- > 0)
for(i=0; i < j; i++)
;
}
|