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