Home | History | Annotate | Line # | Download | only in dev
clock.c revision 1.47.20.1.4.1
      1  1.47.20.1.4.1      matt /*	$NetBSD: clock.c,v 1.47.20.1.4.1 2010/04/21 00:33:53 matt 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.47.20.1.4.1      matt __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.47.20.1.4.1 2010/04/21 00:33:53 matt 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.47    mhitch #include <sys/timetc.h>
     87            1.1    chopps #include <machine/psl.h>
     88            1.1    chopps #include <machine/cpu.h>
     89            1.1    chopps #include <amiga/amiga/device.h>
     90            1.1    chopps #include <amiga/amiga/custom.h>
     91            1.1    chopps #include <amiga/amiga/cia.h>
     92           1.14        is #ifdef DRACO
     93           1.14        is #include <amiga/amiga/drcustom.h>
     94           1.33        is #include <m68k/include/asm_single.h>
     95           1.14        is #endif
     96            1.1    chopps #include <amiga/dev/rtc.h>
     97            1.8    chopps #include <amiga/dev/zbusvar.h>
     98            1.1    chopps 
     99            1.1    chopps #if defined(PROF) && defined(PROFTIMER)
    100            1.1    chopps #include <sys/PROF.h>
    101            1.1    chopps #endif
    102            1.1    chopps 
    103           1.37   aymeric /* the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz.
    104            1.1    chopps    We're using a 100 Hz clock. */
    105            1.4    chopps int amiga_clk_interval;
    106            1.4    chopps int eclockfreq;
    107           1.47    mhitch unsigned int fast_delay_limit;
    108           1.14        is struct CIA *clockcia;
    109            1.4    chopps 
    110           1.47    mhitch static u_int clk_getcounter(struct timecounter *);
    111           1.47    mhitch 
    112           1.47    mhitch static struct timecounter clk_timecounter = {
    113           1.47    mhitch 	clk_getcounter,	/* get_timecount */
    114           1.47    mhitch 	0,		/* no poll_pps */
    115  1.47.20.1.4.1      matt 	~0u,		/* counter_mask */
    116           1.47    mhitch 	0,		/* frequency */
    117           1.47    mhitch 	"clock",	/* name, overriden later */
    118           1.47    mhitch 	100,		/* quality */
    119           1.47    mhitch 	NULL,		/* prev */
    120           1.47    mhitch 	NULL,		/* next */
    121           1.47    mhitch };
    122           1.47    mhitch 
    123            1.1    chopps /*
    124            1.1    chopps  * Machine-dependent clock routines.
    125            1.1    chopps  *
    126            1.1    chopps  * Startrtclock restarts the real-time clock, which provides
    127            1.1    chopps  * hardclock interrupts to kern_clock.c.
    128            1.1    chopps  *
    129            1.1    chopps  * Inittodr initializes the time of day hardware which provides
    130            1.1    chopps  * date functions.
    131            1.1    chopps  *
    132            1.1    chopps  * Resettodr restores the time of day hardware after a time change.
    133            1.1    chopps  *
    134            1.1    chopps  * A note on the real-time clock:
    135           1.47    mhitch  * We actually load the clock with amiga_clk_interval-1 instead of amiga_clk_interval.
    136            1.1    chopps  * This is because the counter decrements to zero after N+1 enabled clock
    137            1.1    chopps  * periods where N is the value loaded into the counter.
    138            1.1    chopps  */
    139            1.1    chopps 
    140           1.37   aymeric int clockmatch(struct device *, struct cfdata *, void *);
    141           1.37   aymeric void clockattach(struct device *, struct device *, void *);
    142           1.37   aymeric void cpu_initclocks(void);
    143            1.1    chopps 
    144           1.41   thorpej CFATTACH_DECL(clock, sizeof(struct device),
    145           1.41   thorpej     clockmatch, clockattach, NULL, NULL);
    146            1.1    chopps 
    147            1.1    chopps int
    148           1.37   aymeric clockmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
    149            1.1    chopps {
    150           1.18        is 	if (matchname("clock", auxp))
    151            1.1    chopps 		return(1);
    152            1.1    chopps 	return(0);
    153            1.1    chopps }
    154            1.1    chopps 
    155            1.1    chopps /*
    156            1.1    chopps  * Start the real-time clock.
    157            1.1    chopps  */
    158            1.1    chopps void
    159           1.37   aymeric clockattach(struct device *pdp, struct device *dp, void *auxp)
    160            1.1    chopps {
    161           1.43       jmc 	const char *clockchip;
    162            1.1    chopps 	unsigned short interval;
    163           1.18        is #ifdef DRACO
    164           1.18        is 	u_char dracorev;
    165           1.18        is #endif
    166            1.1    chopps 
    167           1.47    mhitch #ifdef DRACO
    168           1.47    mhitch 	dracorev = is_draco();
    169           1.47    mhitch #endif
    170           1.47    mhitch 
    171            1.4    chopps 	if (eclockfreq == 0)
    172            1.4    chopps 		eclockfreq = 715909;	/* guess NTSC */
    173           1.37   aymeric 
    174           1.14        is #ifdef DRACO
    175           1.18        is 	if (dracorev >= 4) {
    176           1.47    mhitch 		if (amiga_clk_interval == 0)	/* Only do this 1st time */
    177           1.47    mhitch 			eclockfreq /= 7;
    178           1.18        is 		clockchip = "QuickLogic";
    179           1.18        is 	} else if (dracorev) {
    180           1.14        is 		clockcia = (struct CIA *)CIAAbase;
    181           1.18        is 		clockchip = "CIA A";
    182           1.37   aymeric 	} else
    183           1.14        is #endif
    184           1.14        is 	{
    185           1.14        is 		clockcia = (struct CIA *)CIABbase;
    186           1.18        is 		clockchip = "CIA B";
    187           1.14        is 	}
    188           1.14        is 
    189           1.47    mhitch 	amiga_clk_interval = (eclockfreq / hz);
    190           1.47    mhitch 
    191           1.47    mhitch 	clk_timecounter.tc_name = clockchip;
    192           1.47    mhitch 	clk_timecounter.tc_frequency = eclockfreq;
    193           1.47    mhitch 
    194           1.47    mhitch 	fast_delay_limit = UINT_MAX / amiga_clk_interval;
    195           1.47    mhitch 
    196           1.47    mhitch 	if (dp != NULL) {	/* real autoconfig? */
    197           1.23        is 		printf(": %s system hz %d hardware hz %d\n", clockchip, hz,
    198           1.47    mhitch 		    eclockfreq);
    199           1.47    mhitch 		tc_init(&clk_timecounter);
    200           1.47    mhitch 	}
    201           1.18        is 
    202           1.18        is #ifdef DRACO
    203           1.18        is 	if (dracorev >= 4) {
    204           1.37   aymeric 		/*
    205           1.18        is 		 * can't preload anything beforehand, timer is free_running;
    206           1.18        is 		 * but need this for delay calibration.
    207           1.18        is 		 */
    208           1.18        is 
    209           1.47    mhitch 		draco_ioct->io_timerlo = amiga_clk_interval & 0xff;
    210           1.47    mhitch 		draco_ioct->io_timerhi = amiga_clk_interval >> 8;
    211           1.18        is 
    212           1.18        is 		return;
    213           1.18        is 	}
    214           1.18        is #endif
    215            1.1    chopps 	/*
    216           1.37   aymeric 	 * stop timer A
    217            1.1    chopps 	 */
    218           1.14        is 	clockcia->cra = clockcia->cra & 0xc0;
    219           1.14        is 	clockcia->icr = 1 << 0;		/* disable timer A interrupt */
    220           1.14        is 	interval = clockcia->icr;		/* and make sure it's clear */
    221            1.1    chopps 
    222            1.1    chopps 	/*
    223            1.1    chopps 	 * load interval into registers.
    224            1.1    chopps          * the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz
    225            1.1    chopps 	 * supprort for PAL WHEN?!?! XXX
    226            1.1    chopps 	 */
    227           1.47    mhitch 	interval = amiga_clk_interval - 1;
    228            1.1    chopps 
    229            1.1    chopps 	/*
    230            1.1    chopps 	 * order of setting is important !
    231            1.1    chopps 	 */
    232           1.14        is 	clockcia->talo = interval & 0xff;
    233           1.14        is 	clockcia->tahi = interval >> 8;
    234           1.18        is 	/*
    235           1.18        is 	 * start timer A in continuous mode
    236           1.18        is 	 */
    237           1.18        is 	clockcia->cra = (clockcia->cra & 0xc0) | 1;
    238            1.1    chopps }
    239            1.1    chopps 
    240            1.1    chopps void
    241           1.37   aymeric cpu_initclocks(void)
    242            1.1    chopps {
    243           1.20    mhitch #ifdef DRACO
    244           1.18        is 	unsigned char dracorev;
    245           1.18        is 	dracorev = is_draco();
    246           1.18        is 	if (dracorev >= 4) {
    247           1.47    mhitch 		draco_ioct->io_timerlo = amiga_clk_interval & 0xFF;
    248           1.47    mhitch 		draco_ioct->io_timerhi = amiga_clk_interval >> 8;
    249           1.18        is 		draco_ioct->io_timerrst = 0;	/* any value resets */
    250           1.33        is 		single_inst_bset_b(draco_ioct->io_status2, DRSTAT2_TMRINTENA);
    251           1.18        is 
    252           1.18        is 		return;
    253           1.18        is 	}
    254           1.18        is #endif
    255            1.1    chopps 	/*
    256            1.1    chopps 	 * enable interrupts for timer A
    257            1.1    chopps 	 */
    258           1.14        is 	clockcia->icr = (1<<7) | (1<<0);
    259            1.1    chopps 
    260            1.1    chopps 	/*
    261            1.1    chopps 	 * start timer A in continuous shot mode
    262            1.1    chopps 	 */
    263           1.14        is 	clockcia->cra = (clockcia->cra & 0xc0) | 1;
    264           1.37   aymeric 
    265            1.1    chopps 	/*
    266            1.1    chopps 	 * and globally enable interrupts for ciab
    267            1.1    chopps 	 */
    268           1.14        is #ifdef DRACO
    269           1.18        is 	if (dracorev)		/* we use cia a on DraCo */
    270           1.33        is 		single_inst_bset_b(*draco_intena, DRIRQ_INT2);
    271           1.14        is 	else
    272           1.14        is #endif
    273           1.14        is 		custom.intena = INTF_SETCLR | INTF_EXTER;
    274           1.18        is 
    275            1.1    chopps }
    276            1.1    chopps 
    277           1.13     veego void
    278           1.43       jmc setstatclockrate(int hertz)
    279            1.1    chopps {
    280            1.1    chopps }
    281            1.1    chopps 
    282            1.1    chopps /*
    283           1.47    mhitch  * Returns ticks  since last recorded clock "tick"
    284            1.1    chopps  * (i.e. clock interrupt).
    285            1.1    chopps  */
    286           1.47    mhitch static u_int
    287           1.47    mhitch clk_gettick(void)
    288            1.1    chopps {
    289           1.18        is 	u_int interval;
    290            1.1    chopps 	u_char hi, hi2, lo;
    291            1.1    chopps 
    292           1.14        is #ifdef DRACO
    293           1.18        is 	if (is_draco() >= 4) {
    294           1.18        is 		hi2 = draco_ioct->io_chiprev;	/* latch timer */
    295           1.18        is 		hi = draco_ioct->io_timerhi;
    296           1.18        is 		lo = draco_ioct->io_timerlo;
    297           1.18        is 		interval = ((hi<<8) | lo);
    298           1.47    mhitch 		if (interval > amiga_clk_interval)	/* timer underflow */
    299           1.47    mhitch 			interval = 65536 + amiga_clk_interval - interval;
    300           1.18        is 		else
    301           1.47    mhitch 			interval = amiga_clk_interval - interval;
    302            1.1    chopps 
    303           1.18        is 	} else
    304           1.14        is #endif
    305           1.18        is 	{
    306           1.18        is 		hi  = clockcia->tahi;
    307           1.18        is 		lo  = clockcia->talo;
    308           1.18        is 		hi2 = clockcia->tahi;
    309           1.18        is 		if (hi != hi2) {
    310           1.18        is 			lo = clockcia->talo;
    311           1.18        is 			hi = hi2;
    312           1.18        is 		}
    313            1.1    chopps 
    314           1.47    mhitch 		interval = (amiga_clk_interval - 1) - ((hi<<8) | lo);
    315           1.37   aymeric 
    316            1.1    chopps 		/*
    317           1.18        is 		 * should read ICR and if there's an int pending, adjust
    318           1.18        is 		 * interval. However, since reading ICR clears the interrupt,
    319           1.18        is 		 * we'd lose a hardclock int, and this is not tolerable.
    320            1.1    chopps 		 */
    321            1.1    chopps 	}
    322            1.1    chopps 
    323           1.47    mhitch 	return interval;
    324           1.47    mhitch }
    325           1.47    mhitch 
    326           1.47    mhitch static u_int
    327           1.47    mhitch clk_getcounter(struct timecounter *tc)
    328           1.47    mhitch {
    329  1.47.20.1.4.1      matt 	static int last_hardclock_ticks;
    330  1.47.20.1.4.1      matt 	static u_int last_clock_tick = 0;
    331           1.47    mhitch 	int old_hardclock_ticks;
    332           1.47    mhitch 	u_int clock_tick;
    333           1.47    mhitch 
    334           1.47    mhitch 	do {
    335           1.47    mhitch 		old_hardclock_ticks = hardclock_ticks;
    336           1.47    mhitch 		clock_tick = clk_gettick();
    337           1.47    mhitch 	} while (old_hardclock_ticks != hardclock_ticks);
    338           1.47    mhitch 
    339  1.47.20.1.4.1      matt 	/*
    340  1.47.20.1.4.1      matt 	 * Handle the situation of a wrapped interval counter, while
    341  1.47.20.1.4.1      matt 	 * the hardclock() interrupt was not yet executed to update
    342  1.47.20.1.4.1      matt 	 * hardclock_ticks.
    343  1.47.20.1.4.1      matt 	 */
    344  1.47.20.1.4.1      matt 	if (last_hardclock_ticks > old_hardclock_ticks)
    345  1.47.20.1.4.1      matt 		old_hardclock_ticks = last_hardclock_ticks;
    346  1.47.20.1.4.1      matt 	if (clock_tick < last_clock_tick &&
    347  1.47.20.1.4.1      matt 	    old_hardclock_ticks == last_hardclock_ticks)
    348  1.47.20.1.4.1      matt 		old_hardclock_ticks++;
    349  1.47.20.1.4.1      matt 	last_hardclock_ticks = old_hardclock_ticks;
    350  1.47.20.1.4.1      matt 	last_clock_tick = clock_tick;
    351  1.47.20.1.4.1      matt 
    352           1.47    mhitch 	return old_hardclock_ticks * amiga_clk_interval + clock_tick;
    353            1.1    chopps }
    354            1.1    chopps 
    355            1.1    chopps #if notyet
    356            1.1    chopps 
    357            1.1    chopps /* implement this later. I'd suggest using both timers in CIA-A, they're
    358            1.1    chopps    not yet used. */
    359            1.1    chopps 
    360            1.1    chopps #include "clock.h"
    361            1.1    chopps #if NCLOCK > 0
    362            1.1    chopps /*
    363            1.1    chopps  * /dev/clock: mappable high resolution timer.
    364            1.1    chopps  *
    365            1.1    chopps  * This code implements a 32-bit recycling counter (with a 4 usec period)
    366            1.1    chopps  * using timers 2 & 3 on the 6840 clock chip.  The counter can be mapped
    367            1.1    chopps  * RO into a user's address space to achieve low overhead (no system calls),
    368            1.1    chopps  * high-precision timing.
    369            1.1    chopps  *
    370            1.1    chopps  * Note that timer 3 is also used for the high precision profiling timer
    371            1.1    chopps  * (PROFTIMER code above).  Care should be taken when both uses are
    372            1.1    chopps  * configured as only a token effort is made to avoid conflicting use.
    373            1.1    chopps  */
    374            1.1    chopps #include <sys/proc.h>
    375            1.1    chopps #include <sys/resourcevar.h>
    376            1.1    chopps #include <sys/ioctl.h>
    377            1.1    chopps #include <sys/malloc.h>
    378           1.35       mrg #include <uvm/uvm_extern.h>
    379            1.1    chopps #include <amiga/amiga/clockioctl.h>
    380            1.1    chopps #include <sys/specdev.h>
    381            1.1    chopps #include <sys/vnode.h>
    382            1.1    chopps #include <sys/mman.h>
    383            1.1    chopps 
    384            1.1    chopps int clockon = 0;		/* non-zero if high-res timer enabled */
    385            1.1    chopps #ifdef PROFTIMER
    386            1.1    chopps int  profprocs = 0;		/* # of procs using profiling timer */
    387            1.1    chopps #endif
    388            1.1    chopps #ifdef DEBUG
    389            1.1    chopps int clockdebug = 0;
    390            1.1    chopps #endif
    391            1.1    chopps 
    392            1.1    chopps /*ARGSUSED*/
    393           1.37   aymeric int
    394           1.37   aymeric clockopen(dev_t dev, int flags)
    395            1.1    chopps {
    396            1.1    chopps #ifdef PROFTIMER
    397            1.1    chopps #ifdef PROF
    398            1.1    chopps 	/*
    399            1.1    chopps 	 * Kernel profiling enabled, give up.
    400            1.1    chopps 	 */
    401            1.1    chopps 	if (profiling)
    402            1.1    chopps 		return(EBUSY);
    403            1.1    chopps #endif
    404            1.1    chopps 	/*
    405            1.1    chopps 	 * If any user processes are profiling, give up.
    406            1.1    chopps 	 */
    407            1.1    chopps 	if (profprocs)
    408            1.1    chopps 		return(EBUSY);
    409            1.1    chopps #endif
    410            1.1    chopps 	if (!clockon) {
    411            1.1    chopps 		startclock();
    412            1.1    chopps 		clockon++;
    413            1.1    chopps 	}
    414            1.1    chopps 	return(0);
    415            1.1    chopps }
    416            1.1    chopps 
    417            1.1    chopps /*ARGSUSED*/
    418           1.37   aymeric int
    419           1.37   aymeric clockclose(dev_t dev, int flags)
    420            1.1    chopps {
    421           1.46  christos 	(void) clockunmmap(dev, (void *)0, curproc);	/* XXX */
    422            1.1    chopps 	stopclock();
    423            1.1    chopps 	clockon = 0;
    424            1.1    chopps 	return(0);
    425            1.1    chopps }
    426            1.1    chopps 
    427            1.1    chopps /*ARGSUSED*/
    428           1.37   aymeric int
    429           1.46  christos clockioctl(dev_t dev, u_long cmd, void *data, int flag, struct proc *p)
    430            1.1    chopps {
    431            1.1    chopps 	int error = 0;
    432           1.37   aymeric 
    433            1.1    chopps 	switch (cmd) {
    434            1.1    chopps 
    435            1.1    chopps 	case CLOCKMAP:
    436           1.46  christos 		error = clockmmap(dev, (void **)data, p);
    437            1.1    chopps 		break;
    438            1.1    chopps 
    439            1.1    chopps 	case CLOCKUNMAP:
    440           1.46  christos 		error = clockunmmap(dev, *(void **)data, p);
    441            1.1    chopps 		break;
    442            1.1    chopps 
    443            1.1    chopps 	case CLOCKGETRES:
    444            1.1    chopps 		*(int *)data = CLK_RESOLUTION;
    445            1.1    chopps 		break;
    446            1.1    chopps 
    447            1.1    chopps 	default:
    448            1.1    chopps 		error = EINVAL;
    449            1.1    chopps 		break;
    450            1.1    chopps 	}
    451            1.1    chopps 	return(error);
    452            1.1    chopps }
    453            1.1    chopps 
    454            1.1    chopps /*ARGSUSED*/
    455           1.37   aymeric void
    456           1.37   aymeric clockmap(dev_t dev, int off, int prot)
    457            1.1    chopps {
    458            1.1    chopps 	return((off + (INTIOBASE+CLKBASE+CLKSR-1)) >> PGSHIFT);
    459            1.1    chopps }
    460            1.1    chopps 
    461           1.37   aymeric int
    462           1.46  christos clockmmap(dev_t dev, void **addrp, struct proc *p)
    463            1.1    chopps {
    464            1.1    chopps 	int error;
    465            1.1    chopps 	struct vnode vn;
    466            1.1    chopps 	struct specinfo si;
    467            1.1    chopps 	int flags;
    468            1.1    chopps 
    469            1.1    chopps 	flags = MAP_FILE|MAP_SHARED;
    470            1.1    chopps 	if (*addrp)
    471            1.1    chopps 		flags |= MAP_FIXED;
    472            1.1    chopps 	else
    473           1.46  christos 		*addrp = (void *)0x1000000;	/* XXX */
    474            1.1    chopps 	vn.v_type = VCHR;			/* XXX */
    475            1.1    chopps 	vn.v_specinfo = &si;			/* XXX */
    476            1.1    chopps 	vn.v_rdev = dev;			/* XXX */
    477            1.1    chopps 	error = vm_mmap(&p->p_vmspace->vm_map, (vm_offset_t *)addrp,
    478           1.46  christos 			PAGE_SIZE, VM_PROT_ALL, flags, (void *)&vn, 0);
    479            1.1    chopps 	return(error);
    480            1.1    chopps }
    481            1.1    chopps 
    482           1.37   aymeric int
    483           1.46  christos clockunmmap(dev_t dev, void *addr, struct proc *p)
    484            1.1    chopps {
    485            1.1    chopps 	int rv;
    486            1.1    chopps 
    487            1.1    chopps 	if (addr == 0)
    488            1.1    chopps 		return(EINVAL);		/* XXX: how do we deal with this? */
    489           1.36       chs 	uvm_deallocate(p->p_vmspace->vm_map, (vm_offset_t)addr, PAGE_SIZE);
    490           1.36       chs 	return 0;
    491            1.1    chopps }
    492            1.1    chopps 
    493           1.37   aymeric void
    494           1.37   aymeric startclock(void)
    495            1.1    chopps {
    496            1.1    chopps 	register struct clkreg *clk = (struct clkreg *)clkstd[0];
    497            1.1    chopps 
    498            1.1    chopps 	clk->clk_msb2 = -1; clk->clk_lsb2 = -1;
    499            1.1    chopps 	clk->clk_msb3 = -1; clk->clk_lsb3 = -1;
    500            1.1    chopps 
    501            1.1    chopps 	clk->clk_cr2 = CLK_CR3;
    502            1.1    chopps 	clk->clk_cr3 = CLK_OENAB|CLK_8BIT;
    503            1.1    chopps 	clk->clk_cr2 = CLK_CR1;
    504            1.1    chopps 	clk->clk_cr1 = CLK_IENAB;
    505            1.1    chopps }
    506            1.1    chopps 
    507           1.37   aymeric void
    508           1.37   aymeric stopclock(void)
    509            1.1    chopps {
    510            1.1    chopps 	register struct clkreg *clk = (struct clkreg *)clkstd[0];
    511            1.1    chopps 
    512            1.1    chopps 	clk->clk_cr2 = CLK_CR3;
    513            1.1    chopps 	clk->clk_cr3 = 0;
    514            1.1    chopps 	clk->clk_cr2 = CLK_CR1;
    515            1.1    chopps 	clk->clk_cr1 = CLK_IENAB;
    516            1.1    chopps }
    517            1.1    chopps #endif
    518            1.1    chopps 
    519            1.1    chopps #endif
    520            1.1    chopps 
    521            1.1    chopps 
    522            1.1    chopps #ifdef PROFTIMER
    523            1.1    chopps /*
    524            1.1    chopps  * This code allows the amiga kernel to use one of the extra timers on
    525            1.1    chopps  * the clock chip for profiling, instead of the regular system timer.
    526            1.1    chopps  * The advantage of this is that the profiling timer can be turned up to
    527            1.1    chopps  * a higher interrupt rate, giving finer resolution timing. The profclock
    528            1.1    chopps  * routine is called from the lev6intr in locore, and is a specialized
    529            1.1    chopps  * routine that calls addupc. The overhead then is far less than if
    530            1.1    chopps  * hardclock/softclock was called. Further, the context switch code in
    531            1.1    chopps  * locore has been changed to turn the profile clock on/off when switching
    532            1.1    chopps  * into/out of a process that is profiling (startprofclock/stopprofclock).
    533            1.1    chopps  * This reduces the impact of the profiling clock on other users, and might
    534           1.37   aymeric  * possibly increase the accuracy of the profiling.
    535            1.1    chopps  */
    536            1.1    chopps int  profint   = PRF_INTERVAL;	/* Clock ticks between interrupts */
    537            1.1    chopps int  profscale = 0;		/* Scale factor from sys clock to prof clock */
    538            1.1    chopps char profon    = 0;		/* Is profiling clock on? */
    539            1.1    chopps 
    540            1.1    chopps /* profon values - do not change, locore.s assumes these values */
    541            1.1    chopps #define PRF_NONE	0x00
    542            1.1    chopps #define	PRF_USER	0x01
    543            1.1    chopps #define	PRF_KERNEL	0x80
    544            1.1    chopps 
    545           1.37   aymeric void
    546           1.37   aymeric initprofclock(void)
    547            1.1    chopps {
    548            1.1    chopps #if NCLOCK > 0
    549            1.1    chopps 	struct proc *p = curproc;		/* XXX */
    550            1.1    chopps 
    551            1.1    chopps 	/*
    552            1.1    chopps 	 * If the high-res timer is running, force profiling off.
    553            1.1    chopps 	 * Unfortunately, this gets reflected back to the user not as
    554            1.1    chopps 	 * an error but as a lack of results.
    555            1.1    chopps 	 */
    556            1.1    chopps 	if (clockon) {
    557            1.1    chopps 		p->p_stats->p_prof.pr_scale = 0;
    558            1.1    chopps 		return;
    559            1.1    chopps 	}
    560            1.1    chopps 	/*
    561            1.1    chopps 	 * Keep track of the number of user processes that are profiling
    562            1.1    chopps 	 * by checking the scale value.
    563            1.1    chopps 	 *
    564            1.1    chopps 	 * XXX: this all assumes that the profiling code is well behaved;
    565            1.1    chopps 	 * i.e. profil() is called once per process with pcscale non-zero
    566            1.1    chopps 	 * to turn it on, and once with pcscale zero to turn it off.
    567            1.1    chopps 	 * Also assumes you don't do any forks or execs.  Oh well, there
    568            1.1    chopps 	 * is always adb...
    569            1.1    chopps 	 */
    570            1.1    chopps 	if (p->p_stats->p_prof.pr_scale)
    571            1.1    chopps 		profprocs++;
    572            1.1    chopps 	else
    573            1.1    chopps 		profprocs--;
    574            1.1    chopps #endif
    575            1.1    chopps 	/*
    576            1.1    chopps 	 * The profile interrupt interval must be an even divisor
    577           1.47    mhitch 	 * of the amiga_clk_interval so that scaling from a system clock
    578            1.1    chopps 	 * tick to a profile clock tick is possible using integer math.
    579            1.1    chopps 	 */
    580           1.47    mhitch 	if (profint > amiga_clk_interval || (amiga_clk_interval % profint) != 0)
    581           1.47    mhitch 		profint = amiga_clk_interval;
    582           1.47    mhitch 	profscale = amiga_clk_interval / profint;
    583            1.1    chopps }
    584            1.1    chopps 
    585           1.37   aymeric void
    586           1.37   aymeric startprofclock(void)
    587            1.1    chopps {
    588            1.1    chopps   unsigned short interval;
    589            1.1    chopps 
    590            1.1    chopps   /* stop timer B */
    591           1.14        is   clockcia->crb = clockcia->crb & 0xc0;
    592            1.1    chopps 
    593            1.1    chopps   /* load interval into registers.
    594            1.1    chopps      the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz */
    595            1.1    chopps 
    596            1.1    chopps   interval = profint - 1;
    597            1.1    chopps 
    598            1.1    chopps   /* order of setting is important ! */
    599           1.14        is   clockcia->tblo = interval & 0xff;
    600           1.14        is   clockcia->tbhi = interval >> 8;
    601            1.1    chopps 
    602            1.1    chopps   /* enable interrupts for timer B */
    603           1.14        is   clockcia->icr = (1<<7) | (1<<1);
    604            1.1    chopps 
    605            1.1    chopps   /* start timer B in continuous shot mode */
    606           1.14        is   clockcia->crb = (clockcia->crb & 0xc0) | 1;
    607            1.1    chopps }
    608            1.1    chopps 
    609           1.37   aymeric void
    610           1.37   aymeric stopprofclock(void)
    611            1.1    chopps {
    612            1.1    chopps   /* stop timer B */
    613           1.14        is   clockcia->crb = clockcia->crb & 0xc0;
    614            1.1    chopps }
    615            1.1    chopps 
    616            1.1    chopps #ifdef PROF
    617            1.1    chopps /*
    618            1.1    chopps  * profclock() is expanded in line in lev6intr() unless profiling kernel.
    619            1.1    chopps  * Assumes it is called with clock interrupts blocked.
    620            1.1    chopps  */
    621           1.37   aymeric void
    622           1.46  christos profclock(void *pc, int ps)
    623            1.1    chopps {
    624            1.1    chopps 	/*
    625            1.1    chopps 	 * Came from user mode.
    626            1.1    chopps 	 * If this process is being profiled record the tick.
    627            1.1    chopps 	 */
    628            1.1    chopps 	if (USERMODE(ps)) {
    629            1.1    chopps 		if (p->p_stats.p_prof.pr_scale)
    630            1.1    chopps 			addupc(pc, &curproc->p_stats.p_prof, 1);
    631            1.1    chopps 	}
    632            1.1    chopps 	/*
    633            1.1    chopps 	 * Came from kernel (supervisor) mode.
    634            1.1    chopps 	 * If we are profiling the kernel, record the tick.
    635            1.1    chopps 	 */
    636            1.1    chopps 	else if (profiling < 2) {
    637            1.1    chopps 		register int s = pc - s_lowpc;
    638            1.1    chopps 
    639            1.1    chopps 		if (s < s_textsize)
    640            1.1    chopps 			kcount[s / (HISTFRACTION * sizeof (*kcount))]++;
    641            1.1    chopps 	}
    642            1.1    chopps 	/*
    643            1.1    chopps 	 * Kernel profiling was on but has been disabled.
    644            1.1    chopps 	 * Mark as no longer profiling kernel and if all profiling done,
    645            1.1    chopps 	 * disable the clock.
    646            1.1    chopps 	 */
    647            1.1    chopps 	if (profiling && (profon & PRF_KERNEL)) {
    648            1.1    chopps 		profon &= ~PRF_KERNEL;
    649            1.1    chopps 		if (profon == PRF_NONE)
    650            1.1    chopps 			stopprofclock();
    651            1.1    chopps 	}
    652            1.1    chopps }
    653            1.1    chopps #endif
    654            1.1    chopps #endif
    655           1.47    mhitch 
    656           1.47    mhitch void
    657           1.47    mhitch delay(unsigned int n)
    658           1.47    mhitch {
    659           1.47    mhitch 	unsigned int cur_tick, initial_tick;
    660           1.47    mhitch 	int remaining;
    661           1.47    mhitch 
    662           1.47    mhitch 	/*
    663           1.47    mhitch 	 * Read the counter first, so that the rest of the setup overhead is
    664           1.47    mhitch 	 * counted.
    665           1.47    mhitch 	 */
    666           1.47    mhitch 	initial_tick = clk_gettick();
    667           1.47    mhitch 
    668           1.47    mhitch 	if (amiga_clk_interval == 0) {
    669           1.47    mhitch 		/*
    670           1.47    mhitch 		 * Clock is not initialised yet,
    671           1.47    mhitch 		 * so just do some ad-hoc loop.
    672           1.47    mhitch 		 */
    673           1.47    mhitch 		static uint32_t dummy;
    674           1.47    mhitch 
    675           1.47    mhitch 		n *= 4;
    676           1.47    mhitch 		while (n--)
    677           1.47    mhitch 			dummy *= eclockfreq;
    678           1.47    mhitch 		return;
    679           1.47    mhitch 	}
    680           1.47    mhitch 
    681           1.47    mhitch 	if (n <= fast_delay_limit) {
    682           1.47    mhitch 		/*
    683           1.47    mhitch 		 * For unsigned arithmetic, division can be replaced with
    684           1.47    mhitch 		 * multiplication with the inverse and a shift.
    685           1.47    mhitch 		 */
    686           1.47    mhitch 		remaining = n * eclockfreq / 1000000;
    687           1.47    mhitch 	} else {
    688           1.47    mhitch 		/* This is a very long delay.
    689           1.47    mhitch 		 * Being slow here doesn't matter.
    690           1.47    mhitch 		 */
    691           1.47    mhitch 		remaining = (unsigned long long) n * eclockfreq / 1000000;
    692           1.47    mhitch 	}
    693           1.47    mhitch 
    694           1.47    mhitch 	while (remaining > 0) {
    695           1.47    mhitch 		cur_tick = clk_gettick();
    696           1.47    mhitch 		if (cur_tick > initial_tick)
    697           1.47    mhitch 			remaining -= amiga_clk_interval - (cur_tick - initial_tick);
    698           1.47    mhitch 		else
    699           1.47    mhitch 			remaining -= initial_tick - cur_tick;
    700           1.47    mhitch 		initial_tick = cur_tick;
    701           1.47    mhitch 	}
    702           1.47    mhitch }
    703