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
|
/* $NetBSD: clock.c,v 1.5 2003/07/15 01:37:35 lukem Exp $ */
/*
* Copyright (C) 1995, 1996 Wolfgang Solfrank.
* Copyright (C) 1995, 1996 TooLs GmbH.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by TooLs GmbH.
* 4. The name of TooLs GmbH may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``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 TOOLS GMBH 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 <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.5 2003/07/15 01:37:35 lukem Exp $");
#include "opt_ppcparam.h"
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <uvm/uvm_extern.h>
#include <dev/clock_subr.h>
#include <machine/cpu.h>
#include <powerpc/marvell/watchdog.h>
#include "opt_kgdb.h"
#if (defined(KGDB) || defined(DDB)) && 0
#include <machine/db_machdep.h>
struct stop_time stop_time;
#endif
/*
* Initially we assume a processor with a bus frequency of 12.5 MHz.
*/
u_long tbhz = 0; /* global timebase ticks/sec */
u_long ticks_per_sec = 25000000;
u_long ns_per_tick = 40;
static long ticks_per_intr;
#if NRTC > 0
static inline int yeartoday(int);
#endif
void decr_intr(struct clockframe *frame);
static __inline void
mttb(u_quad_t tb)
{
__asm __volatile ("mttbl %0; mttbu %1; mttbl %1+1"
:: "r" (0), "r" (tb));
}
#if NRTC > 0
static int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static int clockinitted = 0;
static inline int
yeartoday(int year)
{
return((year % 4) ? 365 : 366);
}
#endif /* NRTC */
#define SECPERDAY (24*60*60)
#define SECPERNYEAR (365*SECPERDAY)
#define SECPER4YEARS (4*SECPERNYEAR+SECPERDAY)
#define EPOCHYEAR 1970
/*
* Initialze the time of day register, based on the time base which is, e.g.
* from a filesystem. Base provides the time to within six months,
* and the time of year clock (if any) provides the rest.
*/
#define MINYEAR 2002 /* minimum plausible year */
void
inittodr(base)
time_t base;
{
#if NRTC > 0
rtc_t rtc;
int year;
struct clock_ymdhms dt;
time_t deltat;
int badbase;
int s;
if (base < (MINYEAR-1970)*SECYR) {
printf("WARNING: preposterous time in file system");
/* read the system clock anyway */
base = (MINYEAR-1970)*SECYR;
badbase = 1;
} else
badbase = 0;
s = splclock();
rtc_read(&rtc);
(void)splx(s);
#if defined(DEBUG) && 0
printf("inittodr: %02d%02d/%02d/%02d %02d:%02d:%02d\n",
rtc.rtc_century, rtc.rtc_year, rtc.rtc_month, rtc.rtc_day,
rtc.rtc_hour, rtc.rtc_minute, rtc.rtc_second);
#endif
clockinitted = 1;
year = (rtc.rtc_century * 100) + rtc.rtc_year;
/* simple sanity checks (2037 = time_t overflow) */
if (year < MINYEAR || year > 2037 ||
rtc.rtc_month < 1 || rtc.rtc_month > 12 || rtc.rtc_day < 1 ||
rtc.rtc_day > 31 || rtc.rtc_hour > 23 || rtc.rtc_minute > 59 ||
rtc.rtc_second > 59) {
/*
* Believe the time in the file system for lack of
* anything better, resetting the TODR.
*/
time.tv_sec = base;
if (!badbase) {
printf("WARNING: preposterous clock chip time\n");
resettodr();
}
goto bad;
}
dt.dt_year = year;
dt.dt_mon = rtc.rtc_month;
dt.dt_day = rtc.rtc_day;
dt.dt_hour = rtc.rtc_hour;
dt.dt_min = rtc.rtc_minute;
dt.dt_sec = rtc.rtc_second;
time.tv_sec = clock_ymdhms_to_secs(&dt);
if (!badbase) {
/*
* See if we gained/lost two or more days;
* if so, assume something is amiss.
*/
deltat = time.tv_sec - base;
if (deltat < 0)
deltat = -deltat;
if (deltat < 2 * SECDAY)
return; /* all is well */
printf("WARNING: clock %s %ld days\n",
time.tv_sec < base ? "lost" : "gained",
(long)deltat / SECDAY);
}
bad:
printf("WARNING: CHECK AND RESET THE DATE!\n");
#else /* NRTC */
time.tv_sec = base;
#endif /* NRTC */
}
/*
* Reset the TODR based on the time value; used when the TODR
* has a preposterous value and also when the time is reset
* by the stime system call. Also called when the TODR goes past
* TODRZERO + 100*(SECYEAR+2*SECDAY) (e.g. on Jan 2 just after midnight)
* to wrap the TODR around.
*/
void
resettodr()
{
#if NRTC > 0
struct clock_ymdhms dt;
rtc_t rtc;
int s;
if (!clockinitted)
return;
clock_secs_to_ymdhms(time.tv_sec, &dt);
rtc.rtc_century = dt.dt_year / 100;
rtc.rtc_year = dt.dt_year % 100;
rtc.rtc_month = dt.dt_mon;
rtc.rtc_day = dt.dt_day;
rtc.rtc_hour = dt.dt_hour;
rtc.rtc_minute = dt.dt_min;
rtc.rtc_second = dt.dt_sec;
#if defined(DEBUG) && 0
printf("resettodr: %02d%02d/%02d/%02d %02d:%02d:%02d\n",
rtc.rtc_century, rtc.rtc_year, rtc.rtc_month, rtc.rtc_day,
rtc.rtc_hour, rtc.rtc_minute, rtc.rtc_second);
#endif
s = splclock();
rtc_write(&rtc);
(void)splx(s);
#endif /* NRTC */
}
#ifdef DEBUG
struct clockframe *clockframe = 0;
#endif
void
decr_intr(struct clockframe *frame)
{
struct cpu_info * const ci = curcpu();
u_quad_t tb;
long tick;
int nticks;
int oipl;
#ifdef DEBUG
struct clockframe *oframe;
#endif
WATCHDOG_SERVICE();
EXT_INTR_STATS_DEPTH();
/*
* Check whether we are initialized.
*/
if (!ticks_per_intr)
return;
#ifdef DEBUG
if (extintr_disable() & PSL_EE)
panic("decr_intr: msr & PSL_EE");
oframe = clockframe;
clockframe = frame;
#endif
/*
* Based on the actual time delay since the last decrementer reload,
* we arrange for earlier interrupt next time.
*/
tb = mftb();
__asm __volatile ("mfdec %0" : "=r"(tick));
for (nticks = 0; tick < 0; nticks++)
tick += ticks_per_intr;
__asm __volatile ("mtdec %0" :: "r"(tick));
/*
* lasttb is used during microtime. Set it to the virtual
* start of this tick interval.
*/
ci->ci_lasttb = tb + (tick - ticks_per_intr);
uvmexp.intrs++;
curcpu()->ci_ev_clock.ev_count++;
EXT_INTR_STATS_CAUSE(0, 0, 0, SIBIT(SIR_HWCLOCK));
if (ci->ci_cpl >= IPL_CLOCK) {
/*
* we interrupted while at higher
* priority, so defer this tick.
*/
ci->ci_tickspending += nticks;
EXT_INTR_STATS_PEND_IRQ(SIR_HWCLOCK);
} else {
int oframepri;
EXT_INTR_STATS_DECL(tstart);
EXT_INTR_STATS_COMMIT_IRQ(SIR_HWCLOCK);
EXT_INTR_STATS_PRE(SIR_HWCLOCK, tstart);
nticks += ci->ci_tickspending;
ci->ci_tickspending = 0;
oipl = ci->ci_cpl;
ci->ci_cpl = IPL_CLOCK;
SPL_STATS_LOG(IPL_CLOCK, 0);
(void)extintr_enable();
/*
* Do standard timer interrupt stuff.
* prevent softclock stuff until the last iteration.
*/
oframepri = frame->pri;
frame->pri = oipl;
if (oipl < IPL_SOFTCLOCK)
frame->pri = IPL_SOFTCLOCK;
while (--nticks > 0)
hardclock(frame);
frame->pri = oipl;
hardclock(frame);
frame->pri = oframepri;
(void)extintr_disable();
ci->ci_cpl = oipl;
SPL_STATS_LOG(oipl, 0);
EXT_INTR_STATS_POST(SIR_HWCLOCK, tstart);
}
if (imask_test_v(&ipending, &imask[ci->ci_cpl]))
intr_dispatch();
#ifdef DEBUG
if ((frame->srr1 & PSL_EE) == 0)
panic("decr_intr: frame->srr1 & PSL_EE == 0");
clockframe = oframe;
#endif
}
void
cpu_initclocks()
{
}
void calc_delayconst(void);
void
calc_delayconst()
{
/*
* Get this info during autoconf? XXX
*/
#ifdef CLOCKBASE
ticks_per_sec = CLOCKBASE / 4; /* from config file */
#endif
cpu_timebase = ticks_per_sec;
/*
* Should check for correct CPU here? XXX
*/
ns_per_tick = 1000000000 / ticks_per_sec;
ticks_per_intr = ticks_per_sec / hz;
curcpu()->ci_lasttb = mftb();
__asm __volatile ("mtdec %0" :: "r"(ticks_per_intr));
}
/*
* Fill in *tvp with current time with microsecond resolution.
*/
void
microtime(tvp)
struct timeval *tvp;
{
u_quad_t tb;
u_long t;
int msr;
msr = extintr_disable();
*tvp = time;
tb = mftb();
t = (u_long)(tb - curcpu()->ci_lasttb);
extintr_restore(msr);
t *= ns_per_tick;
t /= 1000;
t += tvp->tv_usec;
while (t >= 1000000) {
t -= 1000000;
tvp->tv_sec++;
}
tvp->tv_usec = t;
}
/*
* Wait for about n microseconds (at least!).
*/
void
delay(n)
unsigned n;
{
u_quad_t tb;
#ifdef DEBUG
if (!ticks_per_intr)
panic("delay: !ticks_per_intr");
#endif
if (n < 20)
n = 20;
tb = mftb();
tb += (n * 1000 + ns_per_tick - 1) / ns_per_tick;
while (tb > mftb())
;
}
/*
* Nothing to do.
*/
void
setstatclockrate(arg)
int arg;
{
/* Do nothing */
}
#if (defined(KGDB) || defined(DDB)) && 0
/* Stop Time facility */
int stop_time_disable;
void
clock_stop_time(struct stop_time *stp)
{
if (stop_time_disable) {
stp->st_state |= STS_DISABLED;
return;
}
if (stp->st_state & STS_STOPPED)
return;
stp->st_msr = extintr_disable();
__asm __volatile ("mfdec %0" : "=r"(stp->st_decr));
stp->st_tb = mftb();
stp->st_state = STS_STOPPED;
}
void
clock_restart_time(struct stop_time *stp)
{
if ((stp->st_state & STS_STOPPED) == 0) {
if (stp->st_state & STS_DISABLED)
return;
else {
#ifdef DIAGNOSTIC
panic("clock_restart_time: was not stopped.");
#else
return;
#endif
}
}
stp->st_state = 0;
mttb(stp->st_tb);
__asm __volatile ("mtdec %0" :: "r"(stp->st_decr));
extintr_restore(stp->st_msr);
}
#endif /* KGDB || DDB */
|