summaryrefslogtreecommitdiff
path: root/sys/src/9/kw/clock.c
blob: 2f8bc36a15c5982cffc97070a4f28791e79b3535 (plain)
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
/*
 * kirkwood clocks
 *
 * timers count down to zero.
 */
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "io.h"

#include "ureg.h"

enum {
	Tcycles		= CLOCKFREQ / HZ,	/* cycles per clock tick */
	Dogperiod	= 15 * CLOCKFREQ, /* at most 21 s.; must fit in ulong */
	MaxPeriod	= Tcycles,
	MinPeriod	= MaxPeriod / 100,

	/* timer ctl bits */
	Tmr0enable	= 1<<0,
	Tmr0reload	= 1<<1,	/* at 0 count, load timer0 from reload0 */
	Tmr1enable	= 1<<2,
	Tmr1reload	= 1<<3,	/* at 0 count, load timer1 from reload1 */
	TmrWDenable	= 1<<4,
	TmrWDreload	= 1<<5,
};

typedef struct TimerReg TimerReg;
struct TimerReg
{
	ulong	ctl;
	ulong	pad[3];
	ulong	reload0;
	ulong	timer0;			/* cycles until zero */
	ulong	reload1;
	ulong	timer1;			/* cycles until zero */
	ulong	reloadwd;
	ulong	timerwd;
};

static int ticks; /* for sanity checking; m->ticks doesn't always get updated */

static void
clockintr(Ureg *ureg, void *arg)
{
	TimerReg *tmr = arg;
	static int nesting;

	tmr->timerwd = Dogperiod;		/* reassure the watchdog */
	ticks++;
	coherence();

	if (nesting == 0) {	/* if the clock interrupted itself, bail out */
		++nesting;
		timerintr(ureg, 0);
		--nesting;
	}

	intrclear(Irqbridge, IRQcputimer0);
}

/* stop clock interrupts and disable the watchdog timer */
void
clockshutdown(void)
{
	TimerReg *tmr = (TimerReg *)soc.clock;

	tmr->ctl = 0;
	coherence();
}

void
clockinit(void)
{
	int i, s;
	CpucsReg *cpu = (CpucsReg *)soc.cpu;
	TimerReg *tmr = (TimerReg *)soc.clock;

	clockshutdown();

	/*
	 * verify sanity of timer0
	 */

	intrenable(Irqbridge, IRQcputimer0, clockintr, tmr, "clock0");
	s = spllo();			/* risky */
	/* take any deferred clock (& other) interrupts here */
	splx(s);

	/* adjust m->bootdelay, used by delay()? */
	m->ticks = ticks = 0;
	m->fastclock = 0;

	tmr->timer0 = 1;
	tmr->ctl = Tmr0enable;		/* just once */
	coherence();

	s = spllo();			/* risky */
	for (i = 0; i < 10 && ticks == 0; i++) {
		delay(1);
		coherence();
	}
	splx(s);
	if (ticks == 0) {
		serialputc('?');
		if (tmr->timer0 == 0)
			panic("clock not interrupting");
		else if (tmr->timer0 == tmr->reload0)
			panic("clock not ticking");
		else
			panic("clock running very slowly");
	}

	/*
	 * configure all timers
	 */
	clockshutdown();
	tmr->reload0 = tmr->timer0 = Tcycles;	/* tick clock */
	tmr->reload1 = tmr->timer1 = ~0;	/* cycle clock */
	tmr->timerwd = Dogperiod;		/* watch dog timer */
	coherence();
	tmr->ctl = Tmr0enable | Tmr0reload | Tmr1enable | Tmr1reload |
		TmrWDenable;
	cpu->rstout |= RstoutWatchdog;
	coherence();
}

void
timerset(Tval next)
{
	int offset;
	TimerReg *tmr = (TimerReg *)soc.clock;

	offset = next - fastticks(nil);
	if(offset < MinPeriod)
		offset = MinPeriod;
	else if(offset > MaxPeriod)
		offset = MaxPeriod;
	tmr->timer0 = offset;
	coherence();
}

uvlong
fastticks(uvlong *hz)
{
	uvlong now;
	int s;

	if(hz)
		*hz = CLOCKFREQ;
	s = splhi();
	/* zero low ulong of fastclock */
	now = (m->fastclock & ~(uvlong)~0ul) | perfticks();
	if(now < m->fastclock)		/* low bits must have wrapped */
		now += 1ll << 32;
	m->fastclock = now;
	splx(s);
	return now;
}

ulong
perfticks(void)
{
	TimerReg *tmr = (TimerReg *)soc.clock;

	return ~tmr->timer1;
}

long
lcycles(void)
{
	return perfticks();
}

ulong
µs(void)
{
	return fastticks2us(fastticks(nil));
}

void
microdelay(int l)
{
	int i;

	l *= m->delayloop;
	l /= 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++)
			;
}