clock.c revision 1.24 1 1.24 veego /* $NetBSD: clock.c,v 1.24 1996/12/23 09:09:55 veego Exp $ */
2 1.6 cgd
3 1.1 chopps /*
4 1.1 chopps * Copyright (c) 1988 University of Utah.
5 1.1 chopps * Copyright (c) 1982, 1990 The Regents of the University of California.
6 1.1 chopps * All rights reserved.
7 1.1 chopps *
8 1.1 chopps * This code is derived from software contributed to Berkeley by
9 1.1 chopps * the Systems Programming Group of the University of Utah Computer
10 1.1 chopps * Science Department.
11 1.1 chopps *
12 1.1 chopps * Redistribution and use in source and binary forms, with or without
13 1.1 chopps * modification, are permitted provided that the following conditions
14 1.1 chopps * are met:
15 1.1 chopps * 1. Redistributions of source code must retain the above copyright
16 1.1 chopps * notice, this list of conditions and the following disclaimer.
17 1.1 chopps * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 chopps * notice, this list of conditions and the following disclaimer in the
19 1.1 chopps * documentation and/or other materials provided with the distribution.
20 1.1 chopps * 3. All advertising materials mentioning features or use of this software
21 1.1 chopps * must display the following acknowledgement:
22 1.1 chopps * This product includes software developed by the University of
23 1.1 chopps * California, Berkeley and its contributors.
24 1.1 chopps * 4. Neither the name of the University nor the names of its contributors
25 1.1 chopps * may be used to endorse or promote products derived from this software
26 1.1 chopps * without specific prior written permission.
27 1.1 chopps *
28 1.1 chopps * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.1 chopps * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.1 chopps * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.1 chopps * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.1 chopps * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.1 chopps * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.1 chopps * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.1 chopps * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.1 chopps * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.1 chopps * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.1 chopps * SUCH DAMAGE.
39 1.1 chopps *
40 1.1 chopps * from: Utah $Hdr: clock.c 1.18 91/01/21$
41 1.1 chopps *
42 1.1 chopps * @(#)clock.c 7.6 (Berkeley) 5/7/91
43 1.1 chopps */
44 1.1 chopps
45 1.1 chopps #include <sys/param.h>
46 1.1 chopps #include <sys/kernel.h>
47 1.1 chopps #include <sys/device.h>
48 1.13 veego #include <sys/systm.h>
49 1.1 chopps #include <machine/psl.h>
50 1.1 chopps #include <machine/cpu.h>
51 1.1 chopps #include <amiga/amiga/device.h>
52 1.1 chopps #include <amiga/amiga/custom.h>
53 1.1 chopps #include <amiga/amiga/cia.h>
54 1.14 is #ifdef DRACO
55 1.14 is #include <amiga/amiga/drcustom.h>
56 1.14 is #endif
57 1.1 chopps #include <amiga/dev/rtc.h>
58 1.8 chopps #include <amiga/dev/zbusvar.h>
59 1.1 chopps
60 1.1 chopps #if defined(PROF) && defined(PROFTIMER)
61 1.1 chopps #include <sys/PROF.h>
62 1.1 chopps #endif
63 1.1 chopps
64 1.1 chopps /* the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz.
65 1.1 chopps We're using a 100 Hz clock. */
66 1.1 chopps
67 1.1 chopps #define CLK_INTERVAL amiga_clk_interval
68 1.4 chopps int amiga_clk_interval;
69 1.4 chopps int eclockfreq;
70 1.14 is struct CIA *clockcia;
71 1.4 chopps
72 1.1 chopps /*
73 1.1 chopps * Machine-dependent clock routines.
74 1.1 chopps *
75 1.1 chopps * Startrtclock restarts the real-time clock, which provides
76 1.1 chopps * hardclock interrupts to kern_clock.c.
77 1.1 chopps *
78 1.1 chopps * Inittodr initializes the time of day hardware which provides
79 1.1 chopps * date functions.
80 1.1 chopps *
81 1.1 chopps * Resettodr restores the time of day hardware after a time change.
82 1.1 chopps *
83 1.1 chopps * A note on the real-time clock:
84 1.1 chopps * We actually load the clock with CLK_INTERVAL-1 instead of CLK_INTERVAL.
85 1.1 chopps * This is because the counter decrements to zero after N+1 enabled clock
86 1.1 chopps * periods where N is the value loaded into the counter.
87 1.1 chopps */
88 1.1 chopps
89 1.24 veego int clockmatch __P((struct device *, struct cfdata *, void *));
90 1.1 chopps void clockattach __P((struct device *, struct device *, void *));
91 1.13 veego void cpu_initclocks __P((void));
92 1.23 is void calibrate_delay __P((struct device *));
93 1.1 chopps
94 1.11 thorpej struct cfattach clock_ca = {
95 1.11 thorpej sizeof(struct device), clockmatch, clockattach
96 1.11 thorpej };
97 1.11 thorpej
98 1.11 thorpej struct cfdriver clock_cd = {
99 1.11 thorpej NULL, "clock", DV_DULL, NULL, 0 };
100 1.1 chopps
101 1.1 chopps int
102 1.24 veego clockmatch(pdp, cfp, auxp)
103 1.1 chopps struct device *pdp;
104 1.24 veego struct cfdata *cfp;
105 1.24 veego void *auxp;
106 1.1 chopps {
107 1.18 is if (matchname("clock", auxp))
108 1.1 chopps return(1);
109 1.1 chopps return(0);
110 1.1 chopps }
111 1.1 chopps
112 1.1 chopps /*
113 1.1 chopps * Start the real-time clock.
114 1.1 chopps */
115 1.1 chopps void
116 1.1 chopps clockattach(pdp, dp, auxp)
117 1.1 chopps struct device *pdp, *dp;
118 1.1 chopps void *auxp;
119 1.1 chopps {
120 1.18 is char *clockchip;
121 1.1 chopps unsigned short interval;
122 1.18 is #ifdef DRACO
123 1.18 is u_char dracorev;
124 1.18 is #endif
125 1.1 chopps
126 1.4 chopps if (eclockfreq == 0)
127 1.4 chopps eclockfreq = 715909; /* guess NTSC */
128 1.4 chopps
129 1.4 chopps CLK_INTERVAL = (eclockfreq / 100);
130 1.4 chopps
131 1.14 is #ifdef DRACO
132 1.18 is dracorev = is_draco();
133 1.18 is if (dracorev >= 4) {
134 1.18 is CLK_INTERVAL = (eclockfreq / 700);
135 1.18 is clockchip = "QuickLogic";
136 1.18 is } else if (dracorev) {
137 1.14 is clockcia = (struct CIA *)CIAAbase;
138 1.18 is clockchip = "CIA A";
139 1.14 is } else
140 1.14 is #endif
141 1.14 is {
142 1.14 is clockcia = (struct CIA *)CIABbase;
143 1.18 is clockchip = "CIA B";
144 1.14 is }
145 1.14 is
146 1.23 is if (pdp)
147 1.23 is printf(": %s system hz %d hardware hz %d\n", clockchip, hz,
148 1.20 mhitch #ifdef DRACO
149 1.18 is dracorev >= 4 ? eclockfreq / 7 : eclockfreq);
150 1.20 mhitch #else
151 1.20 mhitch eclockfreq);
152 1.20 mhitch #endif
153 1.18 is
154 1.18 is #ifdef DRACO
155 1.18 is if (dracorev >= 4) {
156 1.18 is /*
157 1.18 is * can't preload anything beforehand, timer is free_running;
158 1.18 is * but need this for delay calibration.
159 1.18 is */
160 1.18 is
161 1.18 is draco_ioct->io_timerlo = CLK_INTERVAL & 0xff;
162 1.18 is draco_ioct->io_timerhi = CLK_INTERVAL >> 8;
163 1.4 chopps
164 1.23 is calibrate_delay(pdp);
165 1.18 is
166 1.18 is return;
167 1.18 is }
168 1.18 is #endif
169 1.1 chopps /*
170 1.1 chopps * stop timer A
171 1.1 chopps */
172 1.14 is clockcia->cra = clockcia->cra & 0xc0;
173 1.14 is clockcia->icr = 1 << 0; /* disable timer A interrupt */
174 1.14 is interval = clockcia->icr; /* and make sure it's clear */
175 1.1 chopps
176 1.1 chopps /*
177 1.1 chopps * load interval into registers.
178 1.1 chopps * the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz
179 1.1 chopps * supprort for PAL WHEN?!?! XXX
180 1.1 chopps */
181 1.1 chopps interval = CLK_INTERVAL - 1;
182 1.1 chopps
183 1.1 chopps /*
184 1.1 chopps * order of setting is important !
185 1.1 chopps */
186 1.14 is clockcia->talo = interval & 0xff;
187 1.14 is clockcia->tahi = interval >> 8;
188 1.18 is /*
189 1.18 is * start timer A in continuous mode
190 1.18 is */
191 1.18 is clockcia->cra = (clockcia->cra & 0xc0) | 1;
192 1.18 is
193 1.23 is calibrate_delay(pdp);
194 1.18 is }
195 1.18 is
196 1.18 is /*
197 1.18 is * Calibrate delay loop.
198 1.18 is * We use two iterations because we don't have enough bits to do a factor of
199 1.18 is * 8 with better than 1%.
200 1.18 is *
201 1.18 is * XXX Note that we MUST stay below 1 tick if using clkread(), even for
202 1.18 is * underestimated values of delaydivisor.
203 1.18 is *
204 1.18 is * XXX the "ns" below is only correct for a shift of 10 bits, and even then
205 1.18 is * off by 2.4%
206 1.18 is */
207 1.18 is
208 1.23 is void calibrate_delay(pdp)
209 1.23 is struct device *pdp;
210 1.18 is {
211 1.18 is unsigned long t1, t2;
212 1.18 is extern u_int32_t delaydivisor;
213 1.18 is /* XXX this should be defined elsewhere */
214 1.18 is
215 1.23 is if (pdp)
216 1.23 is printf("Calibrating delay loop... ");
217 1.18 is
218 1.18 is do {
219 1.18 is t1 = clkread();
220 1.18 is delay(1024);
221 1.18 is t2 = clkread();
222 1.18 is } while (t2 <= t1);
223 1.18 is t2 -= t1;
224 1.18 is delaydivisor = (delaydivisor * t2 + 1023) >> 10;
225 1.18 is #ifdef DIAGNOSTIC
226 1.23 is if (pdp)
227 1.23 is printf("\ndiff %ld us, new divisor %u ns\n", t2, delaydivisor);
228 1.18 is do {
229 1.18 is t1 = clkread();
230 1.18 is delay(1024);
231 1.18 is t2 = clkread();
232 1.18 is } while (t2 <= t1);
233 1.18 is t2 -= t1;
234 1.18 is delaydivisor = (delaydivisor * t2 + 1023) >> 10;
235 1.23 is if (pdp)
236 1.23 is printf("diff %ld us, new divisor %u ns\n", t2, delaydivisor);
237 1.18 is #endif
238 1.18 is do {
239 1.18 is t1 = clkread();
240 1.18 is delay(1024);
241 1.18 is t2 = clkread();
242 1.18 is } while (t2 <= t1);
243 1.18 is t2 -= t1;
244 1.18 is delaydivisor = (delaydivisor * t2 + 1023) >> 10;
245 1.18 is #ifdef DIAGNOSTIC
246 1.23 is if (pdp)
247 1.23 is printf("diff %ld us, new divisor ", t2);
248 1.18 is #endif
249 1.23 is if (pdp)
250 1.23 is printf("%u ns\n", delaydivisor);
251 1.1 chopps }
252 1.1 chopps
253 1.1 chopps void
254 1.1 chopps cpu_initclocks()
255 1.1 chopps {
256 1.20 mhitch #ifdef DRACO
257 1.18 is unsigned char dracorev;
258 1.18 is dracorev = is_draco();
259 1.18 is if (dracorev >= 4) {
260 1.18 is draco_ioct->io_timerlo = CLK_INTERVAL & 0xFF;
261 1.18 is draco_ioct->io_timerhi = CLK_INTERVAL >> 8;
262 1.18 is draco_ioct->io_timerrst = 0; /* any value resets */
263 1.18 is draco_ioct->io_status2 |= DRSTAT2_TMRINTENA;
264 1.18 is
265 1.18 is return;
266 1.18 is }
267 1.18 is #endif
268 1.1 chopps /*
269 1.1 chopps * enable interrupts for timer A
270 1.1 chopps */
271 1.14 is clockcia->icr = (1<<7) | (1<<0);
272 1.1 chopps
273 1.1 chopps /*
274 1.1 chopps * start timer A in continuous shot mode
275 1.1 chopps */
276 1.14 is clockcia->cra = (clockcia->cra & 0xc0) | 1;
277 1.1 chopps
278 1.1 chopps /*
279 1.1 chopps * and globally enable interrupts for ciab
280 1.1 chopps */
281 1.14 is #ifdef DRACO
282 1.18 is if (dracorev) /* we use cia a on DraCo */
283 1.14 is *draco_intena |= DRIRQ_INT2;
284 1.14 is else
285 1.14 is #endif
286 1.14 is custom.intena = INTF_SETCLR | INTF_EXTER;
287 1.18 is
288 1.1 chopps }
289 1.1 chopps
290 1.13 veego void
291 1.1 chopps setstatclockrate(hz)
292 1.1 chopps int hz;
293 1.1 chopps {
294 1.1 chopps }
295 1.1 chopps
296 1.1 chopps /*
297 1.1 chopps * Returns number of usec since last recorded clock "tick"
298 1.1 chopps * (i.e. clock interrupt).
299 1.1 chopps */
300 1.13 veego u_long
301 1.1 chopps clkread()
302 1.1 chopps {
303 1.18 is u_int interval;
304 1.1 chopps u_char hi, hi2, lo;
305 1.1 chopps
306 1.14 is #ifdef DRACO
307 1.18 is if (is_draco() >= 4) {
308 1.18 is hi2 = draco_ioct->io_chiprev; /* latch timer */
309 1.18 is hi = draco_ioct->io_timerhi;
310 1.18 is lo = draco_ioct->io_timerlo;
311 1.18 is interval = ((hi<<8) | lo);
312 1.18 is if (interval > CLK_INTERVAL) /* timer underflow */
313 1.18 is interval = 65536 + CLK_INTERVAL - interval;
314 1.18 is else
315 1.18 is interval = CLK_INTERVAL - interval;
316 1.1 chopps
317 1.18 is } else
318 1.14 is #endif
319 1.18 is {
320 1.18 is hi = clockcia->tahi;
321 1.18 is lo = clockcia->talo;
322 1.18 is hi2 = clockcia->tahi;
323 1.18 is if (hi != hi2) {
324 1.18 is lo = clockcia->talo;
325 1.18 is hi = hi2;
326 1.18 is }
327 1.1 chopps
328 1.18 is interval = (CLK_INTERVAL - 1) - ((hi<<8) | lo);
329 1.18 is
330 1.1 chopps /*
331 1.18 is * should read ICR and if there's an int pending, adjust
332 1.18 is * interval. However, since reading ICR clears the interrupt,
333 1.18 is * we'd lose a hardclock int, and this is not tolerable.
334 1.1 chopps */
335 1.1 chopps }
336 1.1 chopps
337 1.18 is return((interval * tick) / CLK_INTERVAL);
338 1.1 chopps }
339 1.1 chopps
340 1.1 chopps #if notyet
341 1.1 chopps
342 1.1 chopps /* implement this later. I'd suggest using both timers in CIA-A, they're
343 1.1 chopps not yet used. */
344 1.1 chopps
345 1.1 chopps #include "clock.h"
346 1.1 chopps #if NCLOCK > 0
347 1.1 chopps /*
348 1.1 chopps * /dev/clock: mappable high resolution timer.
349 1.1 chopps *
350 1.1 chopps * This code implements a 32-bit recycling counter (with a 4 usec period)
351 1.1 chopps * using timers 2 & 3 on the 6840 clock chip. The counter can be mapped
352 1.1 chopps * RO into a user's address space to achieve low overhead (no system calls),
353 1.1 chopps * high-precision timing.
354 1.1 chopps *
355 1.1 chopps * Note that timer 3 is also used for the high precision profiling timer
356 1.1 chopps * (PROFTIMER code above). Care should be taken when both uses are
357 1.1 chopps * configured as only a token effort is made to avoid conflicting use.
358 1.1 chopps */
359 1.1 chopps #include <sys/proc.h>
360 1.1 chopps #include <sys/resourcevar.h>
361 1.1 chopps #include <sys/ioctl.h>
362 1.1 chopps #include <sys/malloc.h>
363 1.1 chopps #include <vm/vm.h>
364 1.1 chopps #include <amiga/amiga/clockioctl.h>
365 1.1 chopps #include <sys/specdev.h>
366 1.1 chopps #include <sys/vnode.h>
367 1.1 chopps #include <sys/mman.h>
368 1.1 chopps
369 1.1 chopps int clockon = 0; /* non-zero if high-res timer enabled */
370 1.1 chopps #ifdef PROFTIMER
371 1.1 chopps int profprocs = 0; /* # of procs using profiling timer */
372 1.1 chopps #endif
373 1.1 chopps #ifdef DEBUG
374 1.1 chopps int clockdebug = 0;
375 1.1 chopps #endif
376 1.1 chopps
377 1.1 chopps /*ARGSUSED*/
378 1.1 chopps clockopen(dev, flags)
379 1.1 chopps dev_t dev;
380 1.1 chopps {
381 1.1 chopps #ifdef PROFTIMER
382 1.1 chopps #ifdef PROF
383 1.1 chopps /*
384 1.1 chopps * Kernel profiling enabled, give up.
385 1.1 chopps */
386 1.1 chopps if (profiling)
387 1.1 chopps return(EBUSY);
388 1.1 chopps #endif
389 1.1 chopps /*
390 1.1 chopps * If any user processes are profiling, give up.
391 1.1 chopps */
392 1.1 chopps if (profprocs)
393 1.1 chopps return(EBUSY);
394 1.1 chopps #endif
395 1.1 chopps if (!clockon) {
396 1.1 chopps startclock();
397 1.1 chopps clockon++;
398 1.1 chopps }
399 1.1 chopps return(0);
400 1.1 chopps }
401 1.1 chopps
402 1.1 chopps /*ARGSUSED*/
403 1.1 chopps clockclose(dev, flags)
404 1.1 chopps dev_t dev;
405 1.1 chopps {
406 1.1 chopps (void) clockunmmap(dev, (caddr_t)0, curproc); /* XXX */
407 1.1 chopps stopclock();
408 1.1 chopps clockon = 0;
409 1.1 chopps return(0);
410 1.1 chopps }
411 1.1 chopps
412 1.1 chopps /*ARGSUSED*/
413 1.1 chopps clockioctl(dev, cmd, data, flag, p)
414 1.1 chopps dev_t dev;
415 1.7 chopps u_long cmd;
416 1.1 chopps caddr_t data;
417 1.1 chopps struct proc *p;
418 1.1 chopps {
419 1.1 chopps int error = 0;
420 1.1 chopps
421 1.1 chopps switch (cmd) {
422 1.1 chopps
423 1.1 chopps case CLOCKMAP:
424 1.1 chopps error = clockmmap(dev, (caddr_t *)data, p);
425 1.1 chopps break;
426 1.1 chopps
427 1.1 chopps case CLOCKUNMAP:
428 1.1 chopps error = clockunmmap(dev, *(caddr_t *)data, p);
429 1.1 chopps break;
430 1.1 chopps
431 1.1 chopps case CLOCKGETRES:
432 1.1 chopps *(int *)data = CLK_RESOLUTION;
433 1.1 chopps break;
434 1.1 chopps
435 1.1 chopps default:
436 1.1 chopps error = EINVAL;
437 1.1 chopps break;
438 1.1 chopps }
439 1.1 chopps return(error);
440 1.1 chopps }
441 1.1 chopps
442 1.1 chopps /*ARGSUSED*/
443 1.1 chopps clockmap(dev, off, prot)
444 1.1 chopps dev_t dev;
445 1.1 chopps {
446 1.1 chopps return((off + (INTIOBASE+CLKBASE+CLKSR-1)) >> PGSHIFT);
447 1.1 chopps }
448 1.1 chopps
449 1.1 chopps clockmmap(dev, addrp, p)
450 1.1 chopps dev_t dev;
451 1.1 chopps caddr_t *addrp;
452 1.1 chopps struct proc *p;
453 1.1 chopps {
454 1.1 chopps int error;
455 1.1 chopps struct vnode vn;
456 1.1 chopps struct specinfo si;
457 1.1 chopps int flags;
458 1.1 chopps
459 1.1 chopps flags = MAP_FILE|MAP_SHARED;
460 1.1 chopps if (*addrp)
461 1.1 chopps flags |= MAP_FIXED;
462 1.1 chopps else
463 1.1 chopps *addrp = (caddr_t)0x1000000; /* XXX */
464 1.1 chopps vn.v_type = VCHR; /* XXX */
465 1.1 chopps vn.v_specinfo = &si; /* XXX */
466 1.1 chopps vn.v_rdev = dev; /* XXX */
467 1.1 chopps error = vm_mmap(&p->p_vmspace->vm_map, (vm_offset_t *)addrp,
468 1.1 chopps PAGE_SIZE, VM_PROT_ALL, flags, (caddr_t)&vn, 0);
469 1.1 chopps return(error);
470 1.1 chopps }
471 1.1 chopps
472 1.1 chopps clockunmmap(dev, addr, p)
473 1.1 chopps dev_t dev;
474 1.1 chopps caddr_t addr;
475 1.1 chopps struct proc *p;
476 1.1 chopps {
477 1.1 chopps int rv;
478 1.1 chopps
479 1.1 chopps if (addr == 0)
480 1.1 chopps return(EINVAL); /* XXX: how do we deal with this? */
481 1.1 chopps rv = vm_deallocate(p->p_vmspace->vm_map, (vm_offset_t)addr, PAGE_SIZE);
482 1.1 chopps return(rv == KERN_SUCCESS ? 0 : EINVAL);
483 1.1 chopps }
484 1.1 chopps
485 1.1 chopps startclock()
486 1.1 chopps {
487 1.1 chopps register struct clkreg *clk = (struct clkreg *)clkstd[0];
488 1.1 chopps
489 1.1 chopps clk->clk_msb2 = -1; clk->clk_lsb2 = -1;
490 1.1 chopps clk->clk_msb3 = -1; clk->clk_lsb3 = -1;
491 1.1 chopps
492 1.1 chopps clk->clk_cr2 = CLK_CR3;
493 1.1 chopps clk->clk_cr3 = CLK_OENAB|CLK_8BIT;
494 1.1 chopps clk->clk_cr2 = CLK_CR1;
495 1.1 chopps clk->clk_cr1 = CLK_IENAB;
496 1.1 chopps }
497 1.1 chopps
498 1.1 chopps stopclock()
499 1.1 chopps {
500 1.1 chopps register struct clkreg *clk = (struct clkreg *)clkstd[0];
501 1.1 chopps
502 1.1 chopps clk->clk_cr2 = CLK_CR3;
503 1.1 chopps clk->clk_cr3 = 0;
504 1.1 chopps clk->clk_cr2 = CLK_CR1;
505 1.1 chopps clk->clk_cr1 = CLK_IENAB;
506 1.1 chopps }
507 1.1 chopps #endif
508 1.1 chopps
509 1.1 chopps #endif
510 1.1 chopps
511 1.1 chopps
512 1.1 chopps #ifdef PROFTIMER
513 1.1 chopps /*
514 1.1 chopps * This code allows the amiga kernel to use one of the extra timers on
515 1.1 chopps * the clock chip for profiling, instead of the regular system timer.
516 1.1 chopps * The advantage of this is that the profiling timer can be turned up to
517 1.1 chopps * a higher interrupt rate, giving finer resolution timing. The profclock
518 1.1 chopps * routine is called from the lev6intr in locore, and is a specialized
519 1.1 chopps * routine that calls addupc. The overhead then is far less than if
520 1.1 chopps * hardclock/softclock was called. Further, the context switch code in
521 1.1 chopps * locore has been changed to turn the profile clock on/off when switching
522 1.1 chopps * into/out of a process that is profiling (startprofclock/stopprofclock).
523 1.1 chopps * This reduces the impact of the profiling clock on other users, and might
524 1.1 chopps * possibly increase the accuracy of the profiling.
525 1.1 chopps */
526 1.1 chopps int profint = PRF_INTERVAL; /* Clock ticks between interrupts */
527 1.1 chopps int profscale = 0; /* Scale factor from sys clock to prof clock */
528 1.1 chopps char profon = 0; /* Is profiling clock on? */
529 1.1 chopps
530 1.1 chopps /* profon values - do not change, locore.s assumes these values */
531 1.1 chopps #define PRF_NONE 0x00
532 1.1 chopps #define PRF_USER 0x01
533 1.1 chopps #define PRF_KERNEL 0x80
534 1.1 chopps
535 1.1 chopps initprofclock()
536 1.1 chopps {
537 1.1 chopps #if NCLOCK > 0
538 1.1 chopps struct proc *p = curproc; /* XXX */
539 1.1 chopps
540 1.1 chopps /*
541 1.1 chopps * If the high-res timer is running, force profiling off.
542 1.1 chopps * Unfortunately, this gets reflected back to the user not as
543 1.1 chopps * an error but as a lack of results.
544 1.1 chopps */
545 1.1 chopps if (clockon) {
546 1.1 chopps p->p_stats->p_prof.pr_scale = 0;
547 1.1 chopps return;
548 1.1 chopps }
549 1.1 chopps /*
550 1.1 chopps * Keep track of the number of user processes that are profiling
551 1.1 chopps * by checking the scale value.
552 1.1 chopps *
553 1.1 chopps * XXX: this all assumes that the profiling code is well behaved;
554 1.1 chopps * i.e. profil() is called once per process with pcscale non-zero
555 1.1 chopps * to turn it on, and once with pcscale zero to turn it off.
556 1.1 chopps * Also assumes you don't do any forks or execs. Oh well, there
557 1.1 chopps * is always adb...
558 1.1 chopps */
559 1.1 chopps if (p->p_stats->p_prof.pr_scale)
560 1.1 chopps profprocs++;
561 1.1 chopps else
562 1.1 chopps profprocs--;
563 1.1 chopps #endif
564 1.1 chopps /*
565 1.1 chopps * The profile interrupt interval must be an even divisor
566 1.1 chopps * of the CLK_INTERVAL so that scaling from a system clock
567 1.1 chopps * tick to a profile clock tick is possible using integer math.
568 1.1 chopps */
569 1.1 chopps if (profint > CLK_INTERVAL || (CLK_INTERVAL % profint) != 0)
570 1.1 chopps profint = CLK_INTERVAL;
571 1.1 chopps profscale = CLK_INTERVAL / profint;
572 1.1 chopps }
573 1.1 chopps
574 1.1 chopps startprofclock()
575 1.1 chopps {
576 1.1 chopps unsigned short interval;
577 1.1 chopps
578 1.1 chopps /* stop timer B */
579 1.14 is clockcia->crb = clockcia->crb & 0xc0;
580 1.1 chopps
581 1.1 chopps /* load interval into registers.
582 1.1 chopps the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz */
583 1.1 chopps
584 1.1 chopps interval = profint - 1;
585 1.1 chopps
586 1.1 chopps /* order of setting is important ! */
587 1.14 is clockcia->tblo = interval & 0xff;
588 1.14 is clockcia->tbhi = interval >> 8;
589 1.1 chopps
590 1.1 chopps /* enable interrupts for timer B */
591 1.14 is clockcia->icr = (1<<7) | (1<<1);
592 1.1 chopps
593 1.1 chopps /* start timer B in continuous shot mode */
594 1.14 is clockcia->crb = (clockcia->crb & 0xc0) | 1;
595 1.1 chopps }
596 1.1 chopps
597 1.1 chopps stopprofclock()
598 1.1 chopps {
599 1.1 chopps /* stop timer B */
600 1.14 is clockcia->crb = clockcia->crb & 0xc0;
601 1.1 chopps }
602 1.1 chopps
603 1.1 chopps #ifdef PROF
604 1.1 chopps /*
605 1.1 chopps * profclock() is expanded in line in lev6intr() unless profiling kernel.
606 1.1 chopps * Assumes it is called with clock interrupts blocked.
607 1.1 chopps */
608 1.1 chopps profclock(pc, ps)
609 1.1 chopps caddr_t pc;
610 1.1 chopps int ps;
611 1.1 chopps {
612 1.1 chopps /*
613 1.1 chopps * Came from user mode.
614 1.1 chopps * If this process is being profiled record the tick.
615 1.1 chopps */
616 1.1 chopps if (USERMODE(ps)) {
617 1.1 chopps if (p->p_stats.p_prof.pr_scale)
618 1.1 chopps addupc(pc, &curproc->p_stats.p_prof, 1);
619 1.1 chopps }
620 1.1 chopps /*
621 1.1 chopps * Came from kernel (supervisor) mode.
622 1.1 chopps * If we are profiling the kernel, record the tick.
623 1.1 chopps */
624 1.1 chopps else if (profiling < 2) {
625 1.1 chopps register int s = pc - s_lowpc;
626 1.1 chopps
627 1.1 chopps if (s < s_textsize)
628 1.1 chopps kcount[s / (HISTFRACTION * sizeof (*kcount))]++;
629 1.1 chopps }
630 1.1 chopps /*
631 1.1 chopps * Kernel profiling was on but has been disabled.
632 1.1 chopps * Mark as no longer profiling kernel and if all profiling done,
633 1.1 chopps * disable the clock.
634 1.1 chopps */
635 1.1 chopps if (profiling && (profon & PRF_KERNEL)) {
636 1.1 chopps profon &= ~PRF_KERNEL;
637 1.1 chopps if (profon == PRF_NONE)
638 1.1 chopps stopprofclock();
639 1.1 chopps }
640 1.1 chopps }
641 1.1 chopps #endif
642 1.1 chopps #endif
643 1.1 chopps
644 1.1 chopps /* this is a hook set by a clock driver for the configured realtime clock,
645 1.1 chopps returning plain current unix-time */
646 1.1 chopps long (*gettod) __P((void));
647 1.1 chopps int (*settod) __P((long));
648 1.1 chopps void *clockaddr;
649 1.1 chopps
650 1.1 chopps long a3gettod __P((void));
651 1.1 chopps long a2gettod __P((void));
652 1.1 chopps int a3settod __P((long));
653 1.1 chopps int a2settod __P((long));
654 1.1 chopps int rtcinit __P((void));
655 1.1 chopps
656 1.1 chopps /*
657 1.1 chopps * Initialize the time of day register, based on the time base which is, e.g.
658 1.1 chopps * from a filesystem.
659 1.1 chopps */
660 1.13 veego void
661 1.1 chopps inittodr(base)
662 1.1 chopps time_t base;
663 1.1 chopps {
664 1.1 chopps u_long timbuf = base; /* assume no battery clock exists */
665 1.1 chopps
666 1.1 chopps if (gettod == NULL && rtcinit() == 0)
667 1.21 christos printf("WARNING: no battery clock\n");
668 1.1 chopps else
669 1.1 chopps timbuf = gettod();
670 1.1 chopps
671 1.1 chopps if (timbuf < base) {
672 1.21 christos printf("WARNING: bad date in battery clock\n");
673 1.1 chopps timbuf = base;
674 1.1 chopps }
675 1.1 chopps
676 1.1 chopps /* Battery clock does not store usec's, so forget about it. */
677 1.1 chopps time.tv_sec = timbuf;
678 1.1 chopps }
679 1.1 chopps
680 1.13 veego void
681 1.1 chopps resettodr()
682 1.1 chopps {
683 1.13 veego if (settod && settod(time.tv_sec) == 0)
684 1.21 christos printf("Cannot set battery backed clock\n");
685 1.1 chopps }
686 1.1 chopps
687 1.1 chopps int
688 1.1 chopps rtcinit()
689 1.1 chopps {
690 1.1 chopps clockaddr = (void *)ztwomap(0xdc0000);
691 1.14 is #ifdef DRACO
692 1.14 is if (is_draco()) {
693 1.14 is /* XXX to be done */
694 1.14 is gettod = (void *)0;
695 1.14 is settod = (void *)0;
696 1.14 is return 0;
697 1.14 is } else
698 1.14 is #endif
699 1.1 chopps if (is_a3000() || is_a4000()) {
700 1.1 chopps if (a3gettod() == 0)
701 1.1 chopps return(0);
702 1.1 chopps gettod = a3gettod;
703 1.1 chopps settod = a3settod;
704 1.1 chopps } else {
705 1.1 chopps if (a2gettod() == 0)
706 1.1 chopps return(0);
707 1.1 chopps gettod = a2gettod;
708 1.1 chopps settod = a2settod;
709 1.1 chopps }
710 1.1 chopps return(1);
711 1.1 chopps }
712 1.1 chopps
713 1.1 chopps static int month_days[12] = {
714 1.1 chopps 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
715 1.1 chopps };
716 1.1 chopps
717 1.1 chopps long
718 1.1 chopps a3gettod()
719 1.1 chopps {
720 1.1 chopps struct rtclock3000 *rt;
721 1.10 chopps int i, year, month, day, wday, hour, min, sec;
722 1.1 chopps u_long tmp;
723 1.1 chopps
724 1.1 chopps rt = clockaddr;
725 1.1 chopps
726 1.1 chopps /* hold clock */
727 1.1 chopps rt->control1 = A3CONTROL1_HOLD_CLOCK;
728 1.1 chopps
729 1.1 chopps /* read it */
730 1.1 chopps sec = rt->second1 * 10 + rt->second2;
731 1.1 chopps min = rt->minute1 * 10 + rt->minute2;
732 1.1 chopps hour = rt->hour1 * 10 + rt->hour2;
733 1.10 chopps wday = rt->weekday;
734 1.1 chopps day = rt->day1 * 10 + rt->day2;
735 1.1 chopps month = rt->month1 * 10 + rt->month2;
736 1.1 chopps year = rt->year1 * 10 + rt->year2 + 1900;
737 1.1 chopps
738 1.1 chopps /* let it run again.. */
739 1.1 chopps rt->control1 = A3CONTROL1_FREE_CLOCK;
740 1.1 chopps
741 1.1 chopps if (range_test(hour, 0, 23))
742 1.1 chopps return(0);
743 1.10 chopps if (range_test(wday, 0, 6))
744 1.10 chopps return(0);
745 1.1 chopps if (range_test(day, 1, 31))
746 1.1 chopps return(0);
747 1.1 chopps if (range_test(month, 1, 12))
748 1.1 chopps return(0);
749 1.1 chopps if (range_test(year, STARTOFTIME, 2000))
750 1.1 chopps return(0);
751 1.1 chopps
752 1.1 chopps tmp = 0;
753 1.1 chopps
754 1.1 chopps for (i = STARTOFTIME; i < year; i++)
755 1.1 chopps tmp += days_in_year(i);
756 1.1 chopps if (leapyear(year) && month > FEBRUARY)
757 1.1 chopps tmp++;
758 1.1 chopps
759 1.1 chopps for (i = 1; i < month; i++)
760 1.1 chopps tmp += days_in_month(i);
761 1.1 chopps
762 1.1 chopps tmp += (day - 1);
763 1.1 chopps tmp = ((tmp * 24 + hour) * 60 + min) * 60 + sec;
764 1.1 chopps
765 1.1 chopps return(tmp);
766 1.1 chopps }
767 1.1 chopps
768 1.1 chopps int
769 1.1 chopps a3settod(tim)
770 1.1 chopps long tim;
771 1.1 chopps {
772 1.1 chopps register int i;
773 1.1 chopps register long hms, day;
774 1.1 chopps u_char sec1, sec2;
775 1.1 chopps u_char min1, min2;
776 1.1 chopps u_char hour1, hour2;
777 1.10 chopps /* u_char wday; */
778 1.1 chopps u_char day1, day2;
779 1.1 chopps u_char mon1, mon2;
780 1.1 chopps u_char year1, year2;
781 1.1 chopps struct rtclock3000 *rt;
782 1.1 chopps
783 1.1 chopps rt = clockaddr;
784 1.1 chopps /*
785 1.1 chopps * there seem to be problems with the bitfield addressing
786 1.1 chopps * currently used..
787 1.1 chopps */
788 1.10 chopps
789 1.10 chopps if (! rt)
790 1.1 chopps return 0;
791 1.1 chopps
792 1.1 chopps /* prepare values to be written to clock */
793 1.1 chopps day = tim / SECDAY;
794 1.1 chopps hms = tim % SECDAY;
795 1.1 chopps
796 1.1 chopps hour2 = hms / 3600;
797 1.1 chopps hour1 = hour2 / 10;
798 1.1 chopps hour2 %= 10;
799 1.1 chopps
800 1.1 chopps min2 = (hms % 3600) / 60;
801 1.1 chopps min1 = min2 / 10;
802 1.1 chopps min2 %= 10;
803 1.1 chopps
804 1.1 chopps
805 1.1 chopps sec2 = (hms % 3600) % 60;
806 1.1 chopps sec1 = sec2 / 10;
807 1.1 chopps sec2 %= 10;
808 1.1 chopps
809 1.1 chopps /* Number of years in days */
810 1.1 chopps for (i = STARTOFTIME - 1900; day >= days_in_year(i); i++)
811 1.1 chopps day -= days_in_year(i);
812 1.1 chopps year1 = i / 10;
813 1.1 chopps year2 = i % 10;
814 1.1 chopps
815 1.1 chopps /* Number of months in days left */
816 1.1 chopps if (leapyear(i))
817 1.1 chopps days_in_month(FEBRUARY) = 29;
818 1.1 chopps for (i = 1; day >= days_in_month(i); i++)
819 1.1 chopps day -= days_in_month(i);
820 1.1 chopps days_in_month(FEBRUARY) = 28;
821 1.1 chopps
822 1.1 chopps mon1 = i / 10;
823 1.1 chopps mon2 = i % 10;
824 1.1 chopps
825 1.1 chopps /* Days are what is left over (+1) from all that. */
826 1.1 chopps day ++;
827 1.1 chopps day1 = day / 10;
828 1.1 chopps day2 = day % 10;
829 1.1 chopps
830 1.10 chopps rt->control1 = A3CONTROL1_HOLD_CLOCK;
831 1.1 chopps rt->second1 = sec1;
832 1.1 chopps rt->second2 = sec2;
833 1.1 chopps rt->minute1 = min1;
834 1.1 chopps rt->minute2 = min2;
835 1.1 chopps rt->hour1 = hour1;
836 1.1 chopps rt->hour2 = hour2;
837 1.10 chopps /* rt->weekday = wday; */
838 1.1 chopps rt->day1 = day1;
839 1.1 chopps rt->day2 = day2;
840 1.1 chopps rt->month1 = mon1;
841 1.1 chopps rt->month2 = mon2;
842 1.1 chopps rt->year1 = year1;
843 1.1 chopps rt->year2 = year2;
844 1.10 chopps rt->control1 = A3CONTROL1_FREE_CLOCK;
845 1.1 chopps
846 1.1 chopps return 1;
847 1.1 chopps }
848 1.1 chopps
849 1.1 chopps long
850 1.1 chopps a2gettod()
851 1.1 chopps {
852 1.1 chopps struct rtclock2000 *rt;
853 1.1 chopps int i, year, month, day, hour, min, sec;
854 1.1 chopps u_long tmp;
855 1.1 chopps
856 1.1 chopps rt = clockaddr;
857 1.1 chopps
858 1.1 chopps /*
859 1.1 chopps * hold clock
860 1.1 chopps */
861 1.1 chopps rt->control1 |= A2CONTROL1_HOLD;
862 1.9 chopps i = 0x1000;
863 1.9 chopps while (rt->control1 & A2CONTROL1_BUSY && i--)
864 1.1 chopps ;
865 1.9 chopps if (rt->control1 & A2CONTROL1_BUSY)
866 1.9 chopps return (0); /* Give up and say it's not there */
867 1.1 chopps
868 1.1 chopps /*
869 1.1 chopps * read it
870 1.1 chopps */
871 1.1 chopps sec = rt->second1 * 10 + rt->second2;
872 1.1 chopps min = rt->minute1 * 10 + rt->minute2;
873 1.1 chopps hour = (rt->hour1 & 3) * 10 + rt->hour2;
874 1.1 chopps day = rt->day1 * 10 + rt->day2;
875 1.1 chopps month = rt->month1 * 10 + rt->month2;
876 1.1 chopps year = rt->year1 * 10 + rt->year2 + 1900;
877 1.1 chopps
878 1.1 chopps if ((rt->control3 & A2CONTROL3_24HMODE) == 0) {
879 1.1 chopps if ((rt->hour1 & A2HOUR1_PM) == 0 && hour == 12)
880 1.1 chopps hour = 0;
881 1.1 chopps else if ((rt->hour1 & A2HOUR1_PM) && hour != 12)
882 1.1 chopps hour += 12;
883 1.1 chopps }
884 1.1 chopps
885 1.1 chopps /*
886 1.1 chopps * release the clock
887 1.1 chopps */
888 1.1 chopps rt->control1 &= ~A2CONTROL1_HOLD;
889 1.1 chopps
890 1.1 chopps if (range_test(hour, 0, 23))
891 1.1 chopps return(0);
892 1.1 chopps if (range_test(day, 1, 31))
893 1.1 chopps return(0);
894 1.1 chopps if (range_test(month, 1, 12))
895 1.1 chopps return(0);
896 1.1 chopps if (range_test(year, STARTOFTIME, 2000))
897 1.1 chopps return(0);
898 1.1 chopps
899 1.1 chopps tmp = 0;
900 1.1 chopps
901 1.1 chopps for (i = STARTOFTIME; i < year; i++)
902 1.1 chopps tmp += days_in_year(i);
903 1.1 chopps if (leapyear(year) && month > FEBRUARY)
904 1.1 chopps tmp++;
905 1.1 chopps
906 1.1 chopps for (i = 1; i < month; i++)
907 1.1 chopps tmp += days_in_month(i);
908 1.1 chopps
909 1.1 chopps tmp += (day - 1);
910 1.1 chopps tmp = ((tmp * 24 + hour) * 60 + min) * 60 + sec;
911 1.1 chopps
912 1.1 chopps return(tmp);
913 1.1 chopps }
914 1.1 chopps
915 1.1 chopps /*
916 1.1 chopps * there is some question as to whether this works
917 1.1 chopps * I guess
918 1.1 chopps */
919 1.1 chopps int
920 1.1 chopps a2settod(tim)
921 1.1 chopps long tim;
922 1.1 chopps {
923 1.1 chopps
924 1.1 chopps int i;
925 1.1 chopps long hms, day;
926 1.1 chopps u_char sec1, sec2;
927 1.1 chopps u_char min1, min2;
928 1.1 chopps u_char hour1, hour2;
929 1.1 chopps u_char day1, day2;
930 1.1 chopps u_char mon1, mon2;
931 1.1 chopps u_char year1, year2;
932 1.1 chopps struct rtclock2000 *rt;
933 1.1 chopps
934 1.1 chopps rt = clockaddr;
935 1.1 chopps /*
936 1.1 chopps * there seem to be problems with the bitfield addressing
937 1.1 chopps * currently used..
938 1.1 chopps *
939 1.1 chopps * XXX Check out the above where we (hour1 & 3)
940 1.1 chopps */
941 1.1 chopps if (! rt)
942 1.1 chopps return 0;
943 1.1 chopps
944 1.1 chopps /* prepare values to be written to clock */
945 1.1 chopps day = tim / SECDAY;
946 1.1 chopps hms = tim % SECDAY;
947 1.1 chopps
948 1.1 chopps hour2 = hms / 3600;
949 1.1 chopps hour1 = hour2 / 10;
950 1.1 chopps hour2 %= 10;
951 1.1 chopps
952 1.1 chopps min2 = (hms % 3600) / 60;
953 1.1 chopps min1 = min2 / 10;
954 1.1 chopps min2 %= 10;
955 1.1 chopps
956 1.1 chopps
957 1.1 chopps sec2 = (hms % 3600) % 60;
958 1.1 chopps sec1 = sec2 / 10;
959 1.1 chopps sec2 %= 10;
960 1.1 chopps
961 1.1 chopps /* Number of years in days */
962 1.1 chopps for (i = STARTOFTIME - 1900; day >= days_in_year(i); i++)
963 1.1 chopps day -= days_in_year(i);
964 1.1 chopps year1 = i / 10;
965 1.1 chopps year2 = i % 10;
966 1.1 chopps
967 1.1 chopps /* Number of months in days left */
968 1.1 chopps if (leapyear(i))
969 1.1 chopps days_in_month(FEBRUARY) = 29;
970 1.1 chopps for (i = 1; day >= days_in_month(i); i++)
971 1.1 chopps day -= days_in_month(i);
972 1.1 chopps days_in_month(FEBRUARY) = 28;
973 1.1 chopps
974 1.1 chopps mon1 = i / 10;
975 1.1 chopps mon2 = i % 10;
976 1.1 chopps
977 1.1 chopps /* Days are what is left over (+1) from all that. */
978 1.1 chopps day ++;
979 1.1 chopps day1 = day / 10;
980 1.1 chopps day2 = day % 10;
981 1.1 chopps
982 1.1 chopps /*
983 1.1 chopps * XXXX spin wait as with reading???
984 1.1 chopps */
985 1.10 chopps rt->control1 |= A2CONTROL1_HOLD;
986 1.1 chopps rt->second1 = sec1;
987 1.1 chopps rt->second2 = sec2;
988 1.1 chopps rt->minute1 = min1;
989 1.1 chopps rt->minute2 = min2;
990 1.1 chopps rt->hour1 = hour1;
991 1.1 chopps rt->hour2 = hour2;
992 1.1 chopps rt->day1 = day1;
993 1.1 chopps rt->day2 = day2;
994 1.1 chopps rt->month1 = mon1;
995 1.1 chopps rt->month2 = mon2;
996 1.1 chopps rt->year1 = year1;
997 1.1 chopps rt->year2 = year2;
998 1.10 chopps rt->control2 &= ~A2CONTROL1_HOLD;
999 1.1 chopps
1000 1.22 is return 1;
1001 1.1 chopps }
1002