Home | History | Annotate | Line # | Download | only in dev
clock.c revision 1.36.8.3
      1  1.36.8.3  nathanw /*	$NetBSD: clock.c,v 1.36.8.3 2002/02/28 04:06:34 nathanw Exp $ */
      2  1.36.8.2      scw 
      3  1.36.8.2      scw /*
      4  1.36.8.2      scw  * Copyright (c) 1988 University of Utah.
      5  1.36.8.2      scw  * Copyright (c) 1982, 1990 The Regents of the University of California.
      6  1.36.8.2      scw  * All rights reserved.
      7  1.36.8.2      scw  *
      8  1.36.8.2      scw  * This code is derived from software contributed to Berkeley by
      9  1.36.8.2      scw  * the Systems Programming Group of the University of Utah Computer
     10  1.36.8.2      scw  * Science Department.
     11  1.36.8.2      scw  *
     12  1.36.8.2      scw  * Redistribution and use in source and binary forms, with or without
     13  1.36.8.2      scw  * modification, are permitted provided that the following conditions
     14  1.36.8.2      scw  * are met:
     15  1.36.8.2      scw  * 1. Redistributions of source code must retain the above copyright
     16  1.36.8.2      scw  *    notice, this list of conditions and the following disclaimer.
     17  1.36.8.2      scw  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.36.8.2      scw  *    notice, this list of conditions and the following disclaimer in the
     19  1.36.8.2      scw  *    documentation and/or other materials provided with the distribution.
     20  1.36.8.2      scw  * 3. All advertising materials mentioning features or use of this software
     21  1.36.8.2      scw  *    must display the following acknowledgement:
     22  1.36.8.2      scw  *	This product includes software developed by the University of
     23  1.36.8.2      scw  *	California, Berkeley and its contributors.
     24  1.36.8.2      scw  * 4. Neither the name of the University nor the names of its contributors
     25  1.36.8.2      scw  *    may be used to endorse or promote products derived from this software
     26  1.36.8.2      scw  *    without specific prior written permission.
     27  1.36.8.2      scw  *
     28  1.36.8.2      scw  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  1.36.8.2      scw  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  1.36.8.2      scw  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  1.36.8.2      scw  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  1.36.8.2      scw  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  1.36.8.2      scw  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  1.36.8.2      scw  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  1.36.8.2      scw  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  1.36.8.2      scw  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  1.36.8.2      scw  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  1.36.8.2      scw  * SUCH DAMAGE.
     39  1.36.8.2      scw  *
     40  1.36.8.2      scw  * from: Utah $Hdr: clock.c 1.18 91/01/21$
     41  1.36.8.2      scw  *
     42  1.36.8.2      scw  *	@(#)clock.c	7.6 (Berkeley) 5/7/91
     43  1.36.8.2      scw  */
     44  1.36.8.2      scw 
     45  1.36.8.3  nathanw #include <sys/cdefs.h>
     46  1.36.8.3  nathanw __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.36.8.3 2002/02/28 04:06:34 nathanw Exp $");
     47  1.36.8.3  nathanw 
     48  1.36.8.2      scw #include <sys/param.h>
     49  1.36.8.2      scw #include <sys/kernel.h>
     50  1.36.8.2      scw #include <sys/device.h>
     51  1.36.8.2      scw #include <sys/systm.h>
     52  1.36.8.2      scw #include <machine/psl.h>
     53  1.36.8.2      scw #include <machine/cpu.h>
     54  1.36.8.2      scw #include <amiga/amiga/device.h>
     55  1.36.8.2      scw #include <amiga/amiga/custom.h>
     56  1.36.8.2      scw #include <amiga/amiga/cia.h>
     57  1.36.8.2      scw #ifdef DRACO
     58  1.36.8.2      scw #include <amiga/amiga/drcustom.h>
     59  1.36.8.2      scw #include <m68k/include/asm_single.h>
     60  1.36.8.2      scw #endif
     61  1.36.8.2      scw #include <amiga/dev/rtc.h>
     62  1.36.8.2      scw #include <amiga/dev/zbusvar.h>
     63  1.36.8.2      scw 
     64  1.36.8.2      scw #if defined(PROF) && defined(PROFTIMER)
     65  1.36.8.2      scw #include <sys/PROF.h>
     66  1.36.8.2      scw #endif
     67  1.36.8.2      scw 
     68  1.36.8.3  nathanw /* the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz.
     69  1.36.8.2      scw    We're using a 100 Hz clock. */
     70  1.36.8.2      scw 
     71  1.36.8.2      scw #define CLK_INTERVAL amiga_clk_interval
     72  1.36.8.2      scw int amiga_clk_interval;
     73  1.36.8.2      scw int eclockfreq;
     74  1.36.8.2      scw struct CIA *clockcia;
     75  1.36.8.2      scw 
     76  1.36.8.2      scw /*
     77  1.36.8.2      scw  * Machine-dependent clock routines.
     78  1.36.8.2      scw  *
     79  1.36.8.2      scw  * Startrtclock restarts the real-time clock, which provides
     80  1.36.8.2      scw  * hardclock interrupts to kern_clock.c.
     81  1.36.8.2      scw  *
     82  1.36.8.2      scw  * Inittodr initializes the time of day hardware which provides
     83  1.36.8.2      scw  * date functions.
     84  1.36.8.2      scw  *
     85  1.36.8.2      scw  * Resettodr restores the time of day hardware after a time change.
     86  1.36.8.2      scw  *
     87  1.36.8.2      scw  * A note on the real-time clock:
     88  1.36.8.2      scw  * We actually load the clock with CLK_INTERVAL-1 instead of CLK_INTERVAL.
     89  1.36.8.2      scw  * This is because the counter decrements to zero after N+1 enabled clock
     90  1.36.8.2      scw  * periods where N is the value loaded into the counter.
     91  1.36.8.2      scw  */
     92  1.36.8.2      scw 
     93  1.36.8.3  nathanw int clockmatch(struct device *, struct cfdata *, void *);
     94  1.36.8.3  nathanw void clockattach(struct device *, struct device *, void *);
     95  1.36.8.3  nathanw void cpu_initclocks(void);
     96  1.36.8.3  nathanw void calibrate_delay(struct device *);
     97  1.36.8.2      scw 
     98  1.36.8.2      scw struct cfattach clock_ca = {
     99  1.36.8.2      scw 	sizeof(struct device), clockmatch, clockattach
    100  1.36.8.2      scw };
    101  1.36.8.2      scw 
    102  1.36.8.2      scw int
    103  1.36.8.3  nathanw clockmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
    104  1.36.8.2      scw {
    105  1.36.8.2      scw 	if (matchname("clock", auxp))
    106  1.36.8.2      scw 		return(1);
    107  1.36.8.2      scw 	return(0);
    108  1.36.8.2      scw }
    109  1.36.8.2      scw 
    110  1.36.8.2      scw /*
    111  1.36.8.2      scw  * Start the real-time clock.
    112  1.36.8.2      scw  */
    113  1.36.8.2      scw void
    114  1.36.8.3  nathanw clockattach(struct device *pdp, struct device *dp, void *auxp)
    115  1.36.8.2      scw {
    116  1.36.8.2      scw 	char *clockchip;
    117  1.36.8.2      scw 	unsigned short interval;
    118  1.36.8.2      scw #ifdef DRACO
    119  1.36.8.2      scw 	u_char dracorev;
    120  1.36.8.2      scw #endif
    121  1.36.8.2      scw 
    122  1.36.8.2      scw 	if (eclockfreq == 0)
    123  1.36.8.2      scw 		eclockfreq = 715909;	/* guess NTSC */
    124  1.36.8.3  nathanw 
    125  1.36.8.2      scw 	CLK_INTERVAL = (eclockfreq / 100);
    126  1.36.8.2      scw 
    127  1.36.8.2      scw #ifdef DRACO
    128  1.36.8.2      scw 	dracorev = is_draco();
    129  1.36.8.2      scw 	if (dracorev >= 4) {
    130  1.36.8.2      scw 		CLK_INTERVAL = (eclockfreq / 700);
    131  1.36.8.2      scw 		clockchip = "QuickLogic";
    132  1.36.8.2      scw 	} else if (dracorev) {
    133  1.36.8.2      scw 		clockcia = (struct CIA *)CIAAbase;
    134  1.36.8.2      scw 		clockchip = "CIA A";
    135  1.36.8.3  nathanw 	} else
    136  1.36.8.2      scw #endif
    137  1.36.8.2      scw 	{
    138  1.36.8.2      scw 		clockcia = (struct CIA *)CIABbase;
    139  1.36.8.2      scw 		clockchip = "CIA B";
    140  1.36.8.2      scw 	}
    141  1.36.8.2      scw 
    142  1.36.8.2      scw 	if (dp)
    143  1.36.8.2      scw 		printf(": %s system hz %d hardware hz %d\n", clockchip, hz,
    144  1.36.8.2      scw #ifdef DRACO
    145  1.36.8.2      scw 		dracorev >= 4 ? eclockfreq / 7 : eclockfreq);
    146  1.36.8.2      scw #else
    147  1.36.8.2      scw 		eclockfreq);
    148  1.36.8.2      scw #endif
    149  1.36.8.2      scw 
    150  1.36.8.2      scw #ifdef DRACO
    151  1.36.8.2      scw 	if (dracorev >= 4) {
    152  1.36.8.3  nathanw 		/*
    153  1.36.8.2      scw 		 * can't preload anything beforehand, timer is free_running;
    154  1.36.8.2      scw 		 * but need this for delay calibration.
    155  1.36.8.2      scw 		 */
    156  1.36.8.2      scw 
    157  1.36.8.2      scw 		draco_ioct->io_timerlo = CLK_INTERVAL & 0xff;
    158  1.36.8.2      scw 		draco_ioct->io_timerhi = CLK_INTERVAL >> 8;
    159  1.36.8.2      scw 
    160  1.36.8.2      scw 		calibrate_delay(dp);
    161  1.36.8.2      scw 
    162  1.36.8.2      scw 		return;
    163  1.36.8.2      scw 	}
    164  1.36.8.2      scw #endif
    165  1.36.8.2      scw 	/*
    166  1.36.8.3  nathanw 	 * stop timer A
    167  1.36.8.2      scw 	 */
    168  1.36.8.2      scw 	clockcia->cra = clockcia->cra & 0xc0;
    169  1.36.8.2      scw 	clockcia->icr = 1 << 0;		/* disable timer A interrupt */
    170  1.36.8.2      scw 	interval = clockcia->icr;		/* and make sure it's clear */
    171  1.36.8.2      scw 
    172  1.36.8.2      scw 	/*
    173  1.36.8.2      scw 	 * load interval into registers.
    174  1.36.8.2      scw          * the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz
    175  1.36.8.2      scw 	 * supprort for PAL WHEN?!?! XXX
    176  1.36.8.2      scw 	 */
    177  1.36.8.2      scw 	interval = CLK_INTERVAL - 1;
    178  1.36.8.2      scw 
    179  1.36.8.2      scw 	/*
    180  1.36.8.2      scw 	 * order of setting is important !
    181  1.36.8.2      scw 	 */
    182  1.36.8.2      scw 	clockcia->talo = interval & 0xff;
    183  1.36.8.2      scw 	clockcia->tahi = interval >> 8;
    184  1.36.8.2      scw 	/*
    185  1.36.8.2      scw 	 * start timer A in continuous mode
    186  1.36.8.2      scw 	 */
    187  1.36.8.2      scw 	clockcia->cra = (clockcia->cra & 0xc0) | 1;
    188  1.36.8.2      scw 
    189  1.36.8.2      scw 	calibrate_delay(dp);
    190  1.36.8.2      scw }
    191  1.36.8.2      scw 
    192  1.36.8.2      scw /*
    193  1.36.8.2      scw  * Calibrate delay loop.
    194  1.36.8.2      scw  * We use two iterations because we don't have enough bits to do a factor of
    195  1.36.8.2      scw  * 8 with better than 1%.
    196  1.36.8.2      scw  *
    197  1.36.8.3  nathanw  * XXX Note that we MUST stay below 1 tick if using clkread(), even for
    198  1.36.8.3  nathanw  * underestimated values of delaydivisor.
    199  1.36.8.2      scw  *
    200  1.36.8.2      scw  * XXX the "ns" below is only correct for a shift of 10 bits, and even then
    201  1.36.8.2      scw  * off by 2.4%
    202  1.36.8.2      scw  */
    203  1.36.8.2      scw 
    204  1.36.8.3  nathanw void
    205  1.36.8.3  nathanw calibrate_delay(struct device *dp)
    206  1.36.8.2      scw {
    207  1.36.8.2      scw 	unsigned long t1, t2;
    208  1.36.8.2      scw 	extern u_int32_t delaydivisor;
    209  1.36.8.2      scw 		/* XXX this should be defined elsewhere */
    210  1.36.8.2      scw 
    211  1.36.8.2      scw 	if (dp)
    212  1.36.8.3  nathanw 		printf("Calibrating delay loop... ");
    213  1.36.8.2      scw 
    214  1.36.8.2      scw 	do {
    215  1.36.8.2      scw 		t1 = clkread();
    216  1.36.8.2      scw 		delay(1024);
    217  1.36.8.2      scw 		t2 = clkread();
    218  1.36.8.2      scw 	} while (t2 <= t1);
    219  1.36.8.2      scw 	t2 -= t1;
    220  1.36.8.2      scw 	delaydivisor = (delaydivisor * t2 + 1023) >> 10;
    221  1.36.8.2      scw #ifdef DEBUG
    222  1.36.8.2      scw 	if (dp)
    223  1.36.8.2      scw 		printf("\ndiff %ld us, new divisor %u/1024 us\n", t2,
    224  1.36.8.3  nathanw 		    delaydivisor);
    225  1.36.8.2      scw 	do {
    226  1.36.8.2      scw 		t1 = clkread();
    227  1.36.8.2      scw 		delay(1024);
    228  1.36.8.2      scw 		t2 = clkread();
    229  1.36.8.2      scw 	} while (t2 <= t1);
    230  1.36.8.2      scw 	t2 -= t1;
    231  1.36.8.2      scw 	delaydivisor = (delaydivisor * t2 + 1023) >> 10;
    232  1.36.8.2      scw 	if (dp)
    233  1.36.8.2      scw 		printf("diff %ld us, new divisor %u/1024 us\n", t2,
    234  1.36.8.3  nathanw 		    delaydivisor);
    235  1.36.8.2      scw #endif
    236  1.36.8.2      scw 	do {
    237  1.36.8.2      scw 		t1 = clkread();
    238  1.36.8.2      scw 		delay(1024);
    239  1.36.8.2      scw 		t2 = clkread();
    240  1.36.8.2      scw 	} while (t2 <= t1);
    241  1.36.8.2      scw 	t2 -= t1;
    242  1.36.8.2      scw 	delaydivisor = (delaydivisor * t2 + 1023) >> 10;
    243  1.36.8.2      scw #ifdef DEBUG
    244  1.36.8.2      scw 	if (dp)
    245  1.36.8.2      scw 		printf("diff %ld us, new divisor ", t2);
    246  1.36.8.2      scw #endif
    247  1.36.8.2      scw 	if (dp)
    248  1.36.8.3  nathanw 		printf("%u/1024 us\n", delaydivisor);
    249  1.36.8.2      scw }
    250  1.36.8.2      scw 
    251  1.36.8.2      scw void
    252  1.36.8.3  nathanw cpu_initclocks(void)
    253  1.36.8.2      scw {
    254  1.36.8.2      scw #ifdef DRACO
    255  1.36.8.2      scw 	unsigned char dracorev;
    256  1.36.8.2      scw 	dracorev = is_draco();
    257  1.36.8.2      scw 	if (dracorev >= 4) {
    258  1.36.8.2      scw 		draco_ioct->io_timerlo = CLK_INTERVAL & 0xFF;
    259  1.36.8.2      scw 		draco_ioct->io_timerhi = CLK_INTERVAL >> 8;
    260  1.36.8.2      scw 		draco_ioct->io_timerrst = 0;	/* any value resets */
    261  1.36.8.2      scw 		single_inst_bset_b(draco_ioct->io_status2, DRSTAT2_TMRINTENA);
    262  1.36.8.2      scw 
    263  1.36.8.2      scw 		return;
    264  1.36.8.2      scw 	}
    265  1.36.8.2      scw #endif
    266  1.36.8.2      scw 	/*
    267  1.36.8.2      scw 	 * enable interrupts for timer A
    268  1.36.8.2      scw 	 */
    269  1.36.8.2      scw 	clockcia->icr = (1<<7) | (1<<0);
    270  1.36.8.2      scw 
    271  1.36.8.2      scw 	/*
    272  1.36.8.2      scw 	 * start timer A in continuous shot mode
    273  1.36.8.2      scw 	 */
    274  1.36.8.2      scw 	clockcia->cra = (clockcia->cra & 0xc0) | 1;
    275  1.36.8.3  nathanw 
    276  1.36.8.2      scw 	/*
    277  1.36.8.2      scw 	 * and globally enable interrupts for ciab
    278  1.36.8.2      scw 	 */
    279  1.36.8.2      scw #ifdef DRACO
    280  1.36.8.2      scw 	if (dracorev)		/* we use cia a on DraCo */
    281  1.36.8.2      scw 		single_inst_bset_b(*draco_intena, DRIRQ_INT2);
    282  1.36.8.2      scw 	else
    283  1.36.8.2      scw #endif
    284  1.36.8.2      scw 		custom.intena = INTF_SETCLR | INTF_EXTER;
    285  1.36.8.2      scw 
    286  1.36.8.2      scw }
    287  1.36.8.2      scw 
    288  1.36.8.2      scw void
    289  1.36.8.3  nathanw setstatclockrate(int hz)
    290  1.36.8.2      scw {
    291  1.36.8.2      scw }
    292  1.36.8.2      scw 
    293  1.36.8.2      scw /*
    294  1.36.8.2      scw  * Returns number of usec since last recorded clock "tick"
    295  1.36.8.2      scw  * (i.e. clock interrupt).
    296  1.36.8.2      scw  */
    297  1.36.8.2      scw u_long
    298  1.36.8.3  nathanw clkread(void)
    299  1.36.8.2      scw {
    300  1.36.8.2      scw 	u_int interval;
    301  1.36.8.2      scw 	u_char hi, hi2, lo;
    302  1.36.8.2      scw 
    303  1.36.8.2      scw #ifdef DRACO
    304  1.36.8.2      scw 	if (is_draco() >= 4) {
    305  1.36.8.2      scw 		hi2 = draco_ioct->io_chiprev;	/* latch timer */
    306  1.36.8.2      scw 		hi = draco_ioct->io_timerhi;
    307  1.36.8.2      scw 		lo = draco_ioct->io_timerlo;
    308  1.36.8.2      scw 		interval = ((hi<<8) | lo);
    309  1.36.8.2      scw 		if (interval > CLK_INTERVAL)	/* timer underflow */
    310  1.36.8.2      scw 			interval = 65536 + CLK_INTERVAL - interval;
    311  1.36.8.2      scw 		else
    312  1.36.8.2      scw 			interval = CLK_INTERVAL - interval;
    313  1.36.8.2      scw 
    314  1.36.8.2      scw 	} else
    315  1.36.8.2      scw #endif
    316  1.36.8.2      scw 	{
    317  1.36.8.2      scw 		hi  = clockcia->tahi;
    318  1.36.8.2      scw 		lo  = clockcia->talo;
    319  1.36.8.2      scw 		hi2 = clockcia->tahi;
    320  1.36.8.2      scw 		if (hi != hi2) {
    321  1.36.8.2      scw 			lo = clockcia->talo;
    322  1.36.8.2      scw 			hi = hi2;
    323  1.36.8.2      scw 		}
    324  1.36.8.2      scw 
    325  1.36.8.2      scw 		interval = (CLK_INTERVAL - 1) - ((hi<<8) | lo);
    326  1.36.8.3  nathanw 
    327  1.36.8.2      scw 		/*
    328  1.36.8.2      scw 		 * should read ICR and if there's an int pending, adjust
    329  1.36.8.2      scw 		 * interval. However, since reading ICR clears the interrupt,
    330  1.36.8.2      scw 		 * we'd lose a hardclock int, and this is not tolerable.
    331  1.36.8.2      scw 		 */
    332  1.36.8.2      scw 	}
    333  1.36.8.2      scw 
    334  1.36.8.2      scw 	return((interval * tick) / CLK_INTERVAL);
    335  1.36.8.2      scw }
    336  1.36.8.2      scw 
    337  1.36.8.2      scw #if notyet
    338  1.36.8.2      scw 
    339  1.36.8.2      scw /* implement this later. I'd suggest using both timers in CIA-A, they're
    340  1.36.8.2      scw    not yet used. */
    341  1.36.8.2      scw 
    342  1.36.8.2      scw #include "clock.h"
    343  1.36.8.2      scw #if NCLOCK > 0
    344  1.36.8.2      scw /*
    345  1.36.8.2      scw  * /dev/clock: mappable high resolution timer.
    346  1.36.8.2      scw  *
    347  1.36.8.2      scw  * This code implements a 32-bit recycling counter (with a 4 usec period)
    348  1.36.8.2      scw  * using timers 2 & 3 on the 6840 clock chip.  The counter can be mapped
    349  1.36.8.2      scw  * RO into a user's address space to achieve low overhead (no system calls),
    350  1.36.8.2      scw  * high-precision timing.
    351  1.36.8.2      scw  *
    352  1.36.8.2      scw  * Note that timer 3 is also used for the high precision profiling timer
    353  1.36.8.2      scw  * (PROFTIMER code above).  Care should be taken when both uses are
    354  1.36.8.2      scw  * configured as only a token effort is made to avoid conflicting use.
    355  1.36.8.2      scw  */
    356  1.36.8.2      scw #include <sys/proc.h>
    357  1.36.8.2      scw #include <sys/resourcevar.h>
    358  1.36.8.2      scw #include <sys/ioctl.h>
    359  1.36.8.2      scw #include <sys/malloc.h>
    360  1.36.8.2      scw #include <uvm/uvm_extern.h>
    361  1.36.8.2      scw #include <amiga/amiga/clockioctl.h>
    362  1.36.8.2      scw #include <sys/specdev.h>
    363  1.36.8.2      scw #include <sys/vnode.h>
    364  1.36.8.2      scw #include <sys/mman.h>
    365  1.36.8.2      scw 
    366  1.36.8.2      scw int clockon = 0;		/* non-zero if high-res timer enabled */
    367  1.36.8.2      scw #ifdef PROFTIMER
    368  1.36.8.2      scw int  profprocs = 0;		/* # of procs using profiling timer */
    369  1.36.8.2      scw #endif
    370  1.36.8.2      scw #ifdef DEBUG
    371  1.36.8.2      scw int clockdebug = 0;
    372  1.36.8.2      scw #endif
    373  1.36.8.2      scw 
    374  1.36.8.2      scw /*ARGSUSED*/
    375  1.36.8.3  nathanw int
    376  1.36.8.3  nathanw clockopen(dev_t dev, int flags)
    377  1.36.8.2      scw {
    378  1.36.8.2      scw #ifdef PROFTIMER
    379  1.36.8.2      scw #ifdef PROF
    380  1.36.8.2      scw 	/*
    381  1.36.8.2      scw 	 * Kernel profiling enabled, give up.
    382  1.36.8.2      scw 	 */
    383  1.36.8.2      scw 	if (profiling)
    384  1.36.8.2      scw 		return(EBUSY);
    385  1.36.8.2      scw #endif
    386  1.36.8.2      scw 	/*
    387  1.36.8.2      scw 	 * If any user processes are profiling, give up.
    388  1.36.8.2      scw 	 */
    389  1.36.8.2      scw 	if (profprocs)
    390  1.36.8.2      scw 		return(EBUSY);
    391  1.36.8.2      scw #endif
    392  1.36.8.2      scw 	if (!clockon) {
    393  1.36.8.2      scw 		startclock();
    394  1.36.8.2      scw 		clockon++;
    395  1.36.8.2      scw 	}
    396  1.36.8.2      scw 	return(0);
    397  1.36.8.2      scw }
    398  1.36.8.2      scw 
    399  1.36.8.2      scw /*ARGSUSED*/
    400  1.36.8.3  nathanw int
    401  1.36.8.3  nathanw clockclose(dev_t dev, int flags)
    402  1.36.8.2      scw {
    403  1.36.8.2      scw 	(void) clockunmmap(dev, (caddr_t)0, curproc->l_proc);	/* XXX */
    404  1.36.8.2      scw 	stopclock();
    405  1.36.8.2      scw 	clockon = 0;
    406  1.36.8.2      scw 	return(0);
    407  1.36.8.2      scw }
    408  1.36.8.2      scw 
    409  1.36.8.2      scw /*ARGSUSED*/
    410  1.36.8.3  nathanw int
    411  1.36.8.3  nathanw clockioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    412  1.36.8.2      scw {
    413  1.36.8.2      scw 	int error = 0;
    414  1.36.8.3  nathanw 
    415  1.36.8.2      scw 	switch (cmd) {
    416  1.36.8.2      scw 
    417  1.36.8.2      scw 	case CLOCKMAP:
    418  1.36.8.2      scw 		error = clockmmap(dev, (caddr_t *)data, p);
    419  1.36.8.2      scw 		break;
    420  1.36.8.2      scw 
    421  1.36.8.2      scw 	case CLOCKUNMAP:
    422  1.36.8.2      scw 		error = clockunmmap(dev, *(caddr_t *)data, p);
    423  1.36.8.2      scw 		break;
    424  1.36.8.2      scw 
    425  1.36.8.2      scw 	case CLOCKGETRES:
    426  1.36.8.2      scw 		*(int *)data = CLK_RESOLUTION;
    427  1.36.8.2      scw 		break;
    428  1.36.8.2      scw 
    429  1.36.8.2      scw 	default:
    430  1.36.8.2      scw 		error = EINVAL;
    431  1.36.8.2      scw 		break;
    432  1.36.8.2      scw 	}
    433  1.36.8.2      scw 	return(error);
    434  1.36.8.2      scw }
    435  1.36.8.2      scw 
    436  1.36.8.2      scw /*ARGSUSED*/
    437  1.36.8.3  nathanw void
    438  1.36.8.3  nathanw clockmap(dev_t dev, int off, int prot)
    439  1.36.8.2      scw {
    440  1.36.8.2      scw 	return((off + (INTIOBASE+CLKBASE+CLKSR-1)) >> PGSHIFT);
    441  1.36.8.2      scw }
    442  1.36.8.2      scw 
    443  1.36.8.3  nathanw int
    444  1.36.8.3  nathanw clockmmap(dev_t dev, caddr_t *addrp, struct proc *p)
    445  1.36.8.2      scw {
    446  1.36.8.2      scw 	int error;
    447  1.36.8.2      scw 	struct vnode vn;
    448  1.36.8.2      scw 	struct specinfo si;
    449  1.36.8.2      scw 	int flags;
    450  1.36.8.2      scw 
    451  1.36.8.2      scw 	flags = MAP_FILE|MAP_SHARED;
    452  1.36.8.2      scw 	if (*addrp)
    453  1.36.8.2      scw 		flags |= MAP_FIXED;
    454  1.36.8.2      scw 	else
    455  1.36.8.2      scw 		*addrp = (caddr_t)0x1000000;	/* XXX */
    456  1.36.8.2      scw 	vn.v_type = VCHR;			/* XXX */
    457  1.36.8.2      scw 	vn.v_specinfo = &si;			/* XXX */
    458  1.36.8.2      scw 	vn.v_rdev = dev;			/* XXX */
    459  1.36.8.2      scw 	error = vm_mmap(&p->p_vmspace->vm_map, (vm_offset_t *)addrp,
    460  1.36.8.2      scw 			PAGE_SIZE, VM_PROT_ALL, flags, (caddr_t)&vn, 0);
    461  1.36.8.2      scw 	return(error);
    462  1.36.8.2      scw }
    463  1.36.8.2      scw 
    464  1.36.8.3  nathanw int
    465  1.36.8.3  nathanw clockunmmap(dev_t dev, caddr_t addr, struct proc *p)
    466  1.36.8.2      scw {
    467  1.36.8.2      scw 	int rv;
    468  1.36.8.2      scw 
    469  1.36.8.2      scw 	if (addr == 0)
    470  1.36.8.2      scw 		return(EINVAL);		/* XXX: how do we deal with this? */
    471  1.36.8.2      scw 	uvm_deallocate(p->p_vmspace->vm_map, (vm_offset_t)addr, PAGE_SIZE);
    472  1.36.8.2      scw 	return 0;
    473  1.36.8.2      scw }
    474  1.36.8.2      scw 
    475  1.36.8.3  nathanw void
    476  1.36.8.3  nathanw startclock(void)
    477  1.36.8.2      scw {
    478  1.36.8.2      scw 	register struct clkreg *clk = (struct clkreg *)clkstd[0];
    479  1.36.8.2      scw 
    480  1.36.8.2      scw 	clk->clk_msb2 = -1; clk->clk_lsb2 = -1;
    481  1.36.8.2      scw 	clk->clk_msb3 = -1; clk->clk_lsb3 = -1;
    482  1.36.8.2      scw 
    483  1.36.8.2      scw 	clk->clk_cr2 = CLK_CR3;
    484  1.36.8.2      scw 	clk->clk_cr3 = CLK_OENAB|CLK_8BIT;
    485  1.36.8.2      scw 	clk->clk_cr2 = CLK_CR1;
    486  1.36.8.2      scw 	clk->clk_cr1 = CLK_IENAB;
    487  1.36.8.2      scw }
    488  1.36.8.2      scw 
    489  1.36.8.3  nathanw void
    490  1.36.8.3  nathanw stopclock(void)
    491  1.36.8.2      scw {
    492  1.36.8.2      scw 	register struct clkreg *clk = (struct clkreg *)clkstd[0];
    493  1.36.8.2      scw 
    494  1.36.8.2      scw 	clk->clk_cr2 = CLK_CR3;
    495  1.36.8.2      scw 	clk->clk_cr3 = 0;
    496  1.36.8.2      scw 	clk->clk_cr2 = CLK_CR1;
    497  1.36.8.2      scw 	clk->clk_cr1 = CLK_IENAB;
    498  1.36.8.2      scw }
    499  1.36.8.2      scw #endif
    500  1.36.8.2      scw 
    501  1.36.8.2      scw #endif
    502  1.36.8.2      scw 
    503  1.36.8.2      scw 
    504  1.36.8.2      scw #ifdef PROFTIMER
    505  1.36.8.2      scw /*
    506  1.36.8.2      scw  * This code allows the amiga kernel to use one of the extra timers on
    507  1.36.8.2      scw  * the clock chip for profiling, instead of the regular system timer.
    508  1.36.8.2      scw  * The advantage of this is that the profiling timer can be turned up to
    509  1.36.8.2      scw  * a higher interrupt rate, giving finer resolution timing. The profclock
    510  1.36.8.2      scw  * routine is called from the lev6intr in locore, and is a specialized
    511  1.36.8.2      scw  * routine that calls addupc. The overhead then is far less than if
    512  1.36.8.2      scw  * hardclock/softclock was called. Further, the context switch code in
    513  1.36.8.2      scw  * locore has been changed to turn the profile clock on/off when switching
    514  1.36.8.2      scw  * into/out of a process that is profiling (startprofclock/stopprofclock).
    515  1.36.8.2      scw  * This reduces the impact of the profiling clock on other users, and might
    516  1.36.8.3  nathanw  * possibly increase the accuracy of the profiling.
    517  1.36.8.2      scw  */
    518  1.36.8.2      scw int  profint   = PRF_INTERVAL;	/* Clock ticks between interrupts */
    519  1.36.8.2      scw int  profscale = 0;		/* Scale factor from sys clock to prof clock */
    520  1.36.8.2      scw char profon    = 0;		/* Is profiling clock on? */
    521  1.36.8.2      scw 
    522  1.36.8.2      scw /* profon values - do not change, locore.s assumes these values */
    523  1.36.8.2      scw #define PRF_NONE	0x00
    524  1.36.8.2      scw #define	PRF_USER	0x01
    525  1.36.8.2      scw #define	PRF_KERNEL	0x80
    526  1.36.8.2      scw 
    527  1.36.8.3  nathanw void
    528  1.36.8.3  nathanw initprofclock(void)
    529  1.36.8.2      scw {
    530  1.36.8.2      scw #if NCLOCK > 0
    531  1.36.8.2      scw 	struct proc *p = curproc->l_proc;		/* XXX */
    532  1.36.8.2      scw 
    533  1.36.8.2      scw 	/*
    534  1.36.8.2      scw 	 * If the high-res timer is running, force profiling off.
    535  1.36.8.2      scw 	 * Unfortunately, this gets reflected back to the user not as
    536  1.36.8.2      scw 	 * an error but as a lack of results.
    537  1.36.8.2      scw 	 */
    538  1.36.8.2      scw 	if (clockon) {
    539  1.36.8.2      scw 		p->p_stats->p_prof.pr_scale = 0;
    540  1.36.8.2      scw 		return;
    541  1.36.8.2      scw 	}
    542  1.36.8.2      scw 	/*
    543  1.36.8.2      scw 	 * Keep track of the number of user processes that are profiling
    544  1.36.8.2      scw 	 * by checking the scale value.
    545  1.36.8.2      scw 	 *
    546  1.36.8.2      scw 	 * XXX: this all assumes that the profiling code is well behaved;
    547  1.36.8.2      scw 	 * i.e. profil() is called once per process with pcscale non-zero
    548  1.36.8.2      scw 	 * to turn it on, and once with pcscale zero to turn it off.
    549  1.36.8.2      scw 	 * Also assumes you don't do any forks or execs.  Oh well, there
    550  1.36.8.2      scw 	 * is always adb...
    551  1.36.8.2      scw 	 */
    552  1.36.8.2      scw 	if (p->p_stats->p_prof.pr_scale)
    553  1.36.8.2      scw 		profprocs++;
    554  1.36.8.2      scw 	else
    555  1.36.8.2      scw 		profprocs--;
    556  1.36.8.2      scw #endif
    557  1.36.8.2      scw 	/*
    558  1.36.8.2      scw 	 * The profile interrupt interval must be an even divisor
    559  1.36.8.2      scw 	 * of the CLK_INTERVAL so that scaling from a system clock
    560  1.36.8.2      scw 	 * tick to a profile clock tick is possible using integer math.
    561  1.36.8.2      scw 	 */
    562  1.36.8.2      scw 	if (profint > CLK_INTERVAL || (CLK_INTERVAL % profint) != 0)
    563  1.36.8.2      scw 		profint = CLK_INTERVAL;
    564  1.36.8.2      scw 	profscale = CLK_INTERVAL / profint;
    565  1.36.8.2      scw }
    566  1.36.8.2      scw 
    567  1.36.8.3  nathanw void
    568  1.36.8.3  nathanw startprofclock(void)
    569  1.36.8.2      scw {
    570  1.36.8.2      scw   unsigned short interval;
    571  1.36.8.2      scw 
    572  1.36.8.2      scw   /* stop timer B */
    573  1.36.8.2      scw   clockcia->crb = clockcia->crb & 0xc0;
    574  1.36.8.2      scw 
    575  1.36.8.2      scw   /* load interval into registers.
    576  1.36.8.2      scw      the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz */
    577  1.36.8.2      scw 
    578  1.36.8.2      scw   interval = profint - 1;
    579  1.36.8.2      scw 
    580  1.36.8.2      scw   /* order of setting is important ! */
    581  1.36.8.2      scw   clockcia->tblo = interval & 0xff;
    582  1.36.8.2      scw   clockcia->tbhi = interval >> 8;
    583  1.36.8.2      scw 
    584  1.36.8.2      scw   /* enable interrupts for timer B */
    585  1.36.8.2      scw   clockcia->icr = (1<<7) | (1<<1);
    586  1.36.8.2      scw 
    587  1.36.8.2      scw   /* start timer B in continuous shot mode */
    588  1.36.8.2      scw   clockcia->crb = (clockcia->crb & 0xc0) | 1;
    589  1.36.8.2      scw }
    590  1.36.8.2      scw 
    591  1.36.8.3  nathanw void
    592  1.36.8.3  nathanw stopprofclock(void)
    593  1.36.8.2      scw {
    594  1.36.8.2      scw   /* stop timer B */
    595  1.36.8.2      scw   clockcia->crb = clockcia->crb & 0xc0;
    596  1.36.8.2      scw }
    597  1.36.8.2      scw 
    598  1.36.8.2      scw #ifdef PROF
    599  1.36.8.2      scw /*
    600  1.36.8.2      scw  * profclock() is expanded in line in lev6intr() unless profiling kernel.
    601  1.36.8.2      scw  * Assumes it is called with clock interrupts blocked.
    602  1.36.8.2      scw  */
    603  1.36.8.3  nathanw void
    604  1.36.8.3  nathanw profclock(caddr_t pc, int ps)
    605  1.36.8.2      scw {
    606  1.36.8.2      scw 	/*
    607  1.36.8.2      scw 	 * Came from user mode.
    608  1.36.8.2      scw 	 * If this process is being profiled record the tick.
    609  1.36.8.2      scw 	 */
    610  1.36.8.2      scw 	if (USERMODE(ps)) {
    611  1.36.8.2      scw 		if (p->p_stats.p_prof.pr_scale)
    612  1.36.8.2      scw 			addupc(pc, &curproc->l_proc->p_stats.p_prof, 1);
    613  1.36.8.2      scw 	}
    614  1.36.8.2      scw 	/*
    615  1.36.8.2      scw 	 * Came from kernel (supervisor) mode.
    616  1.36.8.2      scw 	 * If we are profiling the kernel, record the tick.
    617  1.36.8.2      scw 	 */
    618  1.36.8.2      scw 	else if (profiling < 2) {
    619  1.36.8.2      scw 		register int s = pc - s_lowpc;
    620  1.36.8.2      scw 
    621  1.36.8.2      scw 		if (s < s_textsize)
    622  1.36.8.2      scw 			kcount[s / (HISTFRACTION * sizeof (*kcount))]++;
    623  1.36.8.2      scw 	}
    624  1.36.8.2      scw 	/*
    625  1.36.8.2      scw 	 * Kernel profiling was on but has been disabled.
    626  1.36.8.2      scw 	 * Mark as no longer profiling kernel and if all profiling done,
    627  1.36.8.2      scw 	 * disable the clock.
    628  1.36.8.2      scw 	 */
    629  1.36.8.2      scw 	if (profiling && (profon & PRF_KERNEL)) {
    630  1.36.8.2      scw 		profon &= ~PRF_KERNEL;
    631  1.36.8.2      scw 		if (profon == PRF_NONE)
    632  1.36.8.2      scw 			stopprofclock();
    633  1.36.8.2      scw 	}
    634  1.36.8.2      scw }
    635  1.36.8.2      scw #endif
    636  1.36.8.2      scw #endif
    637  1.36.8.2      scw 
    638  1.36.8.2      scw /*
    639  1.36.8.2      scw  * Initialize the time of day register, based on the time base which is, e.g.
    640  1.36.8.2      scw  * from a filesystem.
    641  1.36.8.2      scw  */
    642  1.36.8.2      scw void
    643  1.36.8.3  nathanw inittodr(time_t base)
    644  1.36.8.2      scw {
    645  1.36.8.2      scw 	struct timeval tvbuf;
    646  1.36.8.2      scw 
    647  1.36.8.2      scw 	tvbuf.tv_usec = 0;
    648  1.36.8.2      scw 	tvbuf.tv_sec = base;	/* assume no battery clock exists */
    649  1.36.8.3  nathanw 
    650  1.36.8.2      scw 	if (ugettod == NULL)
    651  1.36.8.2      scw 		printf("WARNING: no battery clock\n");
    652  1.36.8.2      scw 	else {
    653  1.36.8.2      scw 		ugettod(&tvbuf);
    654  1.36.8.2      scw 		tvbuf.tv_sec +=  rtc_offset * 60;
    655  1.36.8.2      scw 	}
    656  1.36.8.2      scw 
    657  1.36.8.2      scw 	if (tvbuf.tv_sec < base) {
    658  1.36.8.2      scw 		printf("WARNING: bad date in battery clock\n");
    659  1.36.8.2      scw 		tvbuf.tv_sec = base;
    660  1.36.8.2      scw 	}
    661  1.36.8.3  nathanw 
    662  1.36.8.2      scw 	time = tvbuf;
    663  1.36.8.2      scw }
    664  1.36.8.2      scw 
    665  1.36.8.2      scw void
    666  1.36.8.3  nathanw resettodr(void)
    667  1.36.8.2      scw {
    668  1.36.8.2      scw 	struct timeval tvbuf;
    669  1.36.8.2      scw 
    670  1.36.8.2      scw 	if (!usettod)
    671  1.36.8.2      scw 		return;
    672  1.36.8.2      scw 
    673  1.36.8.2      scw 	tvbuf = time;
    674  1.36.8.2      scw 
    675  1.36.8.2      scw 	tvbuf.tv_sec -= rtc_offset * 60;
    676  1.36.8.2      scw 
    677  1.36.8.2      scw 	if (!usettod(&tvbuf))
    678  1.36.8.2      scw 		printf("Cannot set battery backed clock\n");
    679  1.36.8.2      scw }
    680