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