Home | History | Annotate | Line # | Download | only in dev
clock.c revision 1.40.10.2
      1  1.40.10.2      yamt /*	$NetBSD: clock.c,v 1.40.10.2 2009/07/18 14:52:52 yamt Exp $	*/
      2        1.1       leo 
      3        1.1       leo /*
      4        1.1       leo  * Copyright (c) 1982, 1990 The Regents of the University of California.
      5        1.1       leo  * All rights reserved.
      6        1.1       leo  *
      7        1.1       leo  * This code is derived from software contributed to Berkeley by
      8        1.1       leo  * the Systems Programming Group of the University of Utah Computer
      9        1.1       leo  * Science Department.
     10        1.1       leo  *
     11        1.1       leo  * Redistribution and use in source and binary forms, with or without
     12        1.1       leo  * modification, are permitted provided that the following conditions
     13        1.1       leo  * are met:
     14        1.1       leo  * 1. Redistributions of source code must retain the above copyright
     15        1.1       leo  *    notice, this list of conditions and the following disclaimer.
     16        1.1       leo  * 2. Redistributions in binary form must reproduce the above copyright
     17        1.1       leo  *    notice, this list of conditions and the following disclaimer in the
     18        1.1       leo  *    documentation and/or other materials provided with the distribution.
     19       1.34       agc  * 3. Neither the name of the University nor the names of its contributors
     20       1.34       agc  *    may be used to endorse or promote products derived from this software
     21       1.34       agc  *    without specific prior written permission.
     22       1.34       agc  *
     23       1.34       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24       1.34       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25       1.34       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26       1.34       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27       1.34       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28       1.34       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29       1.34       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30       1.34       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31       1.34       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32       1.34       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33       1.34       agc  * SUCH DAMAGE.
     34       1.34       agc  *
     35       1.34       agc  * from: Utah $Hdr: clock.c 1.18 91/01/21$
     36       1.34       agc  *
     37       1.34       agc  *	@(#)clock.c	7.6 (Berkeley) 5/7/91
     38       1.34       agc  */
     39       1.34       agc /*
     40       1.34       agc  * Copyright (c) 1988 University of Utah.
     41       1.34       agc  *
     42       1.34       agc  * This code is derived from software contributed to Berkeley by
     43       1.34       agc  * the Systems Programming Group of the University of Utah Computer
     44       1.34       agc  * Science Department.
     45       1.34       agc  *
     46       1.34       agc  * Redistribution and use in source and binary forms, with or without
     47       1.34       agc  * modification, are permitted provided that the following conditions
     48       1.34       agc  * are met:
     49       1.34       agc  * 1. Redistributions of source code must retain the above copyright
     50       1.34       agc  *    notice, this list of conditions and the following disclaimer.
     51       1.34       agc  * 2. Redistributions in binary form must reproduce the above copyright
     52       1.34       agc  *    notice, this list of conditions and the following disclaimer in the
     53       1.34       agc  *    documentation and/or other materials provided with the distribution.
     54        1.1       leo  * 3. All advertising materials mentioning features or use of this software
     55        1.1       leo  *    must display the following acknowledgement:
     56        1.1       leo  *	This product includes software developed by the University of
     57        1.1       leo  *	California, Berkeley and its contributors.
     58        1.1       leo  * 4. Neither the name of the University nor the names of its contributors
     59        1.1       leo  *    may be used to endorse or promote products derived from this software
     60        1.1       leo  *    without specific prior written permission.
     61        1.1       leo  *
     62        1.1       leo  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     63        1.1       leo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     64        1.1       leo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     65        1.1       leo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     66        1.1       leo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     67        1.1       leo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     68        1.1       leo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     69        1.1       leo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     70        1.1       leo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     71        1.1       leo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     72        1.1       leo  * SUCH DAMAGE.
     73        1.1       leo  *
     74        1.1       leo  * from: Utah $Hdr: clock.c 1.18 91/01/21$
     75        1.1       leo  *
     76        1.1       leo  *	@(#)clock.c	7.6 (Berkeley) 5/7/91
     77        1.1       leo  */
     78       1.33     lukem 
     79       1.33     lukem #include <sys/cdefs.h>
     80  1.40.10.2      yamt __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.40.10.2 2009/07/18 14:52:52 yamt Exp $");
     81        1.1       leo 
     82        1.1       leo #include <sys/param.h>
     83        1.1       leo #include <sys/kernel.h>
     84        1.9       leo #include <sys/systm.h>
     85        1.1       leo #include <sys/device.h>
     86       1.14       leo #include <sys/uio.h>
     87       1.14       leo #include <sys/conf.h>
     88       1.32   thorpej #include <sys/proc.h>
     89       1.31  jdolecek #include <sys/event.h>
     90       1.40     joerg #include <sys/timetc.h>
     91       1.18       leo 
     92       1.18       leo #include <dev/clock_subr.h>
     93       1.18       leo 
     94        1.1       leo #include <machine/psl.h>
     95        1.1       leo #include <machine/cpu.h>
     96        1.1       leo #include <machine/iomap.h>
     97        1.1       leo #include <machine/mfp.h>
     98        1.1       leo #include <atari/dev/clockreg.h>
     99  1.40.10.2      yamt #include <atari/dev/clockvar.h>
    100       1.14       leo #include <atari/atari/device.h>
    101        1.1       leo 
    102        1.4       leo #if defined(GPROF) && defined(PROFTIMER)
    103        1.4       leo #include <machine/profile.h>
    104        1.1       leo #endif
    105        1.1       leo 
    106       1.40     joerg static int	atari_rtc_get(todr_chip_handle_t, struct clock_ymdhms *);
    107       1.40     joerg static int	atari_rtc_set(todr_chip_handle_t, struct clock_ymdhms *);
    108       1.40     joerg 
    109        1.1       leo /*
    110        1.5       leo  * The MFP clock runs at 2457600Hz. We use a {system,stat,prof}clock divider
    111        1.5       leo  * of 200. Therefore the timer runs at an effective rate of:
    112        1.5       leo  * 2457600/200 = 12288Hz.
    113        1.5       leo  */
    114        1.5       leo #define CLOCK_HZ	12288
    115        1.5       leo 
    116       1.40     joerg static u_int clk_getcounter(struct timecounter *);
    117       1.40     joerg 
    118       1.40     joerg static struct timecounter clk_timecounter = {
    119       1.40     joerg 	clk_getcounter,	/* get_timecount */
    120       1.40     joerg 	0,		/* no poll_pps */
    121       1.40     joerg 	~0u,		/* counter_mask */
    122       1.40     joerg 	CLOCK_HZ,	/* frequency */
    123       1.40     joerg 	"clock",	/* name, overriden later */
    124       1.40     joerg 	100,		/* quality */
    125       1.40     joerg 	NULL,		/* prev */
    126       1.40     joerg 	NULL,		/* next */
    127       1.40     joerg };
    128       1.40     joerg 
    129        1.5       leo /*
    130        1.1       leo  * Machine-dependent clock routines.
    131        1.1       leo  *
    132        1.1       leo  * Inittodr initializes the time of day hardware which provides
    133        1.1       leo  * date functions.
    134        1.1       leo  *
    135        1.1       leo  * Resettodr restores the time of day hardware after a time change.
    136        1.1       leo  */
    137        1.1       leo 
    138       1.14       leo struct clock_softc {
    139       1.14       leo 	struct device	sc_dev;
    140       1.14       leo 	int		sc_flags;
    141       1.14       leo };
    142       1.14       leo 
    143       1.14       leo /*
    144       1.14       leo  *  'sc_flags' state info. Only used by the rtc-device functions.
    145       1.14       leo  */
    146       1.14       leo #define	RTC_OPEN	1
    147       1.14       leo 
    148       1.14       leo dev_type_open(rtcopen);
    149       1.14       leo dev_type_close(rtcclose);
    150       1.14       leo dev_type_read(rtcread);
    151       1.14       leo dev_type_write(rtcwrite);
    152       1.14       leo 
    153  1.40.10.1      yamt static void	clockattach(struct device *, struct device *, void *);
    154  1.40.10.1      yamt static int	clockmatch(struct device *, struct cfdata *, void *);
    155        1.1       leo 
    156       1.30   thorpej CFATTACH_DECL(clock, sizeof(struct clock_softc),
    157       1.30   thorpej     clockmatch, clockattach, NULL, NULL);
    158       1.10   thorpej 
    159       1.19   thorpej extern struct cfdriver clock_cd;
    160       1.28   gehenna 
    161       1.28   gehenna const struct cdevsw rtc_cdevsw = {
    162       1.28   gehenna 	rtcopen, rtcclose, rtcread, rtcwrite, noioctl,
    163       1.31  jdolecek 	nostop, notty, nopoll, nommap, nokqfilter,
    164       1.28   gehenna };
    165        1.1       leo 
    166  1.40.10.1      yamt void statintr(struct clockframe);
    167        1.9       leo 
    168  1.40.10.1      yamt static int	twodigits(char *, int);
    169        1.1       leo 
    170        1.5       leo static int	divisor;	/* Systemclock divisor	*/
    171        1.5       leo 
    172        1.5       leo /*
    173        1.5       leo  * Statistics and profile clock intervals and variances. Variance must
    174        1.5       leo  * be a power of 2. Since this gives us an even number, not an odd number,
    175        1.5       leo  * we discard one case and compensate. That is, a variance of 64 would
    176        1.5       leo  * give us offsets in [0..63]. Instead, we take offsets in [1..63].
    177       1.26       wiz  * This is symmetric around the point 32, or statvar/2, and thus averages
    178        1.5       leo  * to that value (assuming uniform random numbers).
    179        1.5       leo  */
    180        1.5       leo #ifdef STATCLOCK
    181        1.5       leo static int	statvar = 32;	/* {stat,prof}clock variance		*/
    182        1.5       leo static int	statmin;	/* statclock divisor - variance/2	*/
    183        1.5       leo static int	profmin;	/* profclock divisor - variance/2	*/
    184       1.27       wiz static int	clk2min;	/* current, from above choices		*/
    185        1.5       leo #endif
    186        1.1       leo 
    187        1.1       leo int
    188  1.40.10.1      yamt clockmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
    189        1.1       leo {
    190       1.15       leo 
    191  1.40.10.2      yamt 	if (!strcmp("clock", auxp))
    192  1.40.10.2      yamt 		return 1;
    193  1.40.10.2      yamt 	return 0;
    194        1.1       leo }
    195        1.1       leo 
    196        1.1       leo /*
    197        1.1       leo  * Start the real-time clock.
    198        1.1       leo  */
    199  1.40.10.2      yamt void clockattach(struct device *pdp, struct device *dp, void *auxp)
    200        1.1       leo {
    201  1.40.10.2      yamt 
    202       1.14       leo 	struct clock_softc *sc = (void *)dp;
    203       1.40     joerg 	static struct todr_chip_handle	tch;
    204       1.40     joerg 
    205       1.40     joerg 	tch.todr_gettime_ymdhms = atari_rtc_get;
    206       1.40     joerg 	tch.todr_settime_ymdhms = atari_rtc_set;
    207       1.40     joerg 	tch.todr_setwen = NULL;
    208       1.40     joerg 
    209       1.40     joerg 	todr_attach(&tch);
    210       1.14       leo 
    211       1.14       leo 	sc->sc_flags = 0;
    212       1.14       leo 
    213        1.1       leo 	/*
    214        1.3       leo 	 * Initialize Timer-A in the ST-MFP. We use a divisor of 200.
    215        1.3       leo 	 * The MFP clock runs at 2457600Hz. Therefore the timer runs
    216        1.3       leo 	 * at an effective rate of: 2457600/200 = 12288Hz. The
    217        1.3       leo 	 * following expression works for 48, 64 or 96 hz.
    218        1.1       leo 	 */
    219        1.5       leo 	divisor       = CLOCK_HZ/hz;
    220        1.2       leo 	MFP->mf_tacr  = 0;		/* Stop timer			*/
    221        1.2       leo 	MFP->mf_iera &= ~IA_TIMA;	/* Disable timer interrupts	*/
    222        1.2       leo 	MFP->mf_tadr  = divisor;	/* Set divisor			*/
    223        1.1       leo 
    224       1.40     joerg 	clk_timecounter.tc_frequency = CLOCK_HZ;
    225       1.40     joerg 
    226        1.5       leo 	if (hz != 48 && hz != 64 && hz != 96) { /* XXX */
    227       1.13  christos 		printf (": illegal value %d for systemclock, reset to %d\n\t",
    228        1.5       leo 								hz, 64);
    229        1.5       leo 		hz = 64;
    230        1.5       leo 	}
    231       1.13  christos 	printf(": system hz %d timer-A divisor 200/%d\n", hz, divisor);
    232  1.40.10.1      yamt 	tc_init(&clk_timecounter);
    233        1.1       leo 
    234        1.5       leo #ifdef STATCLOCK
    235        1.5       leo 	if ((stathz == 0) || (stathz > hz) || (CLOCK_HZ % stathz))
    236        1.5       leo 		stathz = hz;
    237        1.5       leo 	if ((profhz == 0) || (profhz > (hz << 1)) || (CLOCK_HZ % profhz))
    238        1.5       leo 		profhz = hz << 1;
    239        1.5       leo 
    240        1.5       leo 	MFP->mf_tcdcr &= 0x7;			/* Stop timer		*/
    241        1.5       leo 	MFP->mf_ierb  &= ~IB_TIMC;		/* Disable timer inter.	*/
    242        1.5       leo 	MFP->mf_tcdr   = CLOCK_HZ/stathz;	/* Set divisor		*/
    243        1.5       leo 
    244        1.5       leo 	statmin  = (CLOCK_HZ/stathz) - (statvar >> 1);
    245        1.5       leo 	profmin  = (CLOCK_HZ/profhz) - (statvar >> 1);
    246        1.5       leo 	clk2min  = statmin;
    247        1.5       leo #endif /* STATCLOCK */
    248        1.1       leo }
    249        1.1       leo 
    250  1.40.10.1      yamt void cpu_initclocks(void)
    251        1.1       leo {
    252  1.40.10.2      yamt 
    253        1.3       leo 	MFP->mf_tacr  = T_Q200;		/* Start timer			*/
    254       1.20       leo 	MFP->mf_ipra  = (u_int8_t)~IA_TIMA;/* Clear pending interrupts	*/
    255        1.2       leo 	MFP->mf_iera |= IA_TIMA;	/* Enable timer interrupts	*/
    256        1.2       leo 	MFP->mf_imra |= IA_TIMA;	/*    .....			*/
    257        1.5       leo 
    258        1.5       leo #ifdef STATCLOCK
    259        1.5       leo 	MFP->mf_tcdcr = (MFP->mf_tcdcr & 0x7) | (T_Q200<<4); /* Start	*/
    260       1.20       leo 	MFP->mf_iprb  = (u_int8_t)~IB_TIMC;/* Clear pending interrupts	*/
    261        1.5       leo 	MFP->mf_ierb |= IB_TIMC;	/* Enable timer interrupts	*/
    262        1.5       leo 	MFP->mf_imrb |= IB_TIMC;	/*    .....			*/
    263        1.5       leo #endif /* STATCLOCK */
    264        1.1       leo }
    265        1.1       leo 
    266        1.9       leo void
    267  1.40.10.1      yamt setstatclockrate(int newhz)
    268        1.1       leo {
    269  1.40.10.2      yamt 
    270        1.5       leo #ifdef STATCLOCK
    271        1.5       leo 	if (newhz == stathz)
    272        1.5       leo 		clk2min = statmin;
    273        1.5       leo 	else clk2min = profmin;
    274        1.5       leo #endif /* STATCLOCK */
    275        1.1       leo }
    276        1.1       leo 
    277        1.5       leo #ifdef STATCLOCK
    278        1.5       leo void
    279  1.40.10.1      yamt statintr(struct clockframe frame)
    280        1.5       leo {
    281        1.5       leo 	register int	var, r;
    282        1.5       leo 
    283        1.5       leo 	var = statvar - 1;
    284        1.5       leo 	do {
    285        1.5       leo 		r = random() & var;
    286  1.40.10.2      yamt 	} while (r == 0);
    287        1.5       leo 
    288        1.5       leo 	/*
    289        1.5       leo 	 * Note that we are always lagging behind as the new divisor
    290        1.5       leo 	 * value will not be loaded until the next interrupt. This
    291        1.5       leo 	 * shouldn't disturb the median frequency (I think ;-) ) as
    292        1.5       leo 	 * only the value used when switching frequencies is used
    293        1.5       leo 	 * twice. This shouldn't happen very often.
    294        1.5       leo 	 */
    295        1.5       leo 	MFP->mf_tcdr = clk2min + r;
    296        1.5       leo 
    297       1.16       leo 	statclock(&frame);
    298        1.5       leo }
    299        1.5       leo #endif /* STATCLOCK */
    300        1.5       leo 
    301       1.40     joerg static u_int
    302       1.40     joerg clk_getcounter(struct timecounter *tc)
    303        1.1       leo {
    304  1.40.10.2      yamt 	uint32_t delta, count, cur_hardclock;
    305  1.40.10.2      yamt 	uint8_t ipra, tadr;
    306  1.40.10.2      yamt 	int s;
    307  1.40.10.2      yamt 	static uint32_t lastcount;
    308        1.3       leo 
    309       1.40     joerg 	s = splhigh();
    310  1.40.10.2      yamt 	cur_hardclock = hardclock_ticks;
    311       1.40     joerg 	ipra = MFP->mf_ipra;
    312       1.40     joerg 	tadr = MFP->mf_tadr;
    313       1.40     joerg 	delta = divisor - tadr;
    314       1.40     joerg 
    315       1.40     joerg 	if (ipra & IA_TIMA)
    316       1.40     joerg 		delta += divisor;
    317       1.40     joerg 	splx(s);
    318       1.22       leo 
    319  1.40.10.2      yamt 	count = (divisor * cur_hardclock) + delta;
    320  1.40.10.2      yamt 	if ((int32_t)(count - lastcount) < 0) {
    321  1.40.10.2      yamt 		/* XXX wrapped; maybe hardclock() is blocked more than 2/HZ */
    322  1.40.10.2      yamt 		count = lastcount + 1;
    323  1.40.10.2      yamt 	}
    324  1.40.10.2      yamt 	lastcount = count;
    325  1.40.10.2      yamt 
    326  1.40.10.2      yamt 	return count;
    327        1.1       leo }
    328        1.1       leo 
    329        1.2       leo #define TIMB_FREQ	614400
    330        1.2       leo #define TIMB_LIMIT	256
    331        1.1       leo 
    332  1.40.10.2      yamt void
    333  1.40.10.2      yamt init_delay(void)
    334  1.40.10.2      yamt {
    335  1.40.10.2      yamt 
    336  1.40.10.2      yamt 	/*
    337  1.40.10.2      yamt 	 * Initialize Timer-B in the ST-MFP. This timer is used by
    338  1.40.10.2      yamt 	 * the 'delay' function below. This timer is setup to be
    339  1.40.10.2      yamt 	 * continueously counting from 255 back to zero at a
    340  1.40.10.2      yamt 	 * frequency of 614400Hz. We do this *early* in the
    341  1.40.10.2      yamt 	 * initialisation process.
    342  1.40.10.2      yamt 	 */
    343  1.40.10.2      yamt 	MFP->mf_tbcr  = 0;		/* Stop timer			*/
    344  1.40.10.2      yamt 	MFP->mf_iera &= ~IA_TIMB;	/* Disable timer interrupts	*/
    345  1.40.10.2      yamt 	MFP->mf_tbdr  = 0;
    346  1.40.10.2      yamt 	MFP->mf_tbcr  = T_Q004;	/* Start timer			*/
    347  1.40.10.2      yamt }
    348  1.40.10.2      yamt 
    349        1.1       leo /*
    350        1.1       leo  * Wait "n" microseconds.
    351        1.2       leo  * Relies on MFP-Timer B counting down from TIMB_LIMIT at TIMB_FREQ Hz.
    352        1.1       leo  * Note: timer had better have been programmed before this is first used!
    353        1.1       leo  */
    354       1.14       leo void
    355       1.39     joerg delay(unsigned int n)
    356        1.1       leo {
    357       1.39     joerg 	int	ticks, otick, remaining;
    358        1.1       leo 
    359        1.1       leo 	/*
    360        1.1       leo 	 * Read the counter first, so that the rest of the setup overhead is
    361        1.1       leo 	 * counted.
    362        1.1       leo 	 */
    363        1.2       leo 	otick = MFP->mf_tbdr;
    364        1.1       leo 
    365       1.39     joerg 	if (n <= UINT_MAX / TIMB_FREQ) {
    366       1.39     joerg 		/*
    367       1.39     joerg 		 * For unsigned arithmetic, division can be replaced with
    368       1.39     joerg 		 * multiplication with the inverse and a shift.
    369       1.39     joerg 		 */
    370       1.39     joerg 		remaining = n * TIMB_FREQ / 1000000;
    371       1.39     joerg 	} else {
    372       1.39     joerg 		/* This is a very long delay.
    373       1.39     joerg 		 * Being slow here doesn't matter.
    374       1.39     joerg 		 */
    375       1.39     joerg 		remaining = (unsigned long long) n * TIMB_FREQ / 1000000;
    376        1.1       leo 	}
    377        1.1       leo 
    378  1.40.10.2      yamt 	while (remaining > 0) {
    379       1.35        he 		ticks = MFP->mf_tbdr;
    380  1.40.10.2      yamt 		if (ticks > otick)
    381       1.39     joerg 			remaining -= TIMB_LIMIT - (ticks - otick);
    382       1.39     joerg 		else
    383       1.39     joerg 			remaining -= otick - ticks;
    384       1.35        he 		otick = ticks;
    385        1.1       leo 	}
    386        1.1       leo }
    387        1.1       leo 
    388        1.4       leo #ifdef GPROF
    389        1.1       leo /*
    390        1.1       leo  * profclock() is expanded in line in lev6intr() unless profiling kernel.
    391        1.1       leo  * Assumes it is called with clock interrupts blocked.
    392        1.1       leo  */
    393  1.40.10.1      yamt profclock(void *pc, int ps)
    394        1.1       leo {
    395  1.40.10.2      yamt 
    396        1.1       leo 	/*
    397        1.1       leo 	 * Came from user mode.
    398        1.1       leo 	 * If this process is being profiled record the tick.
    399        1.1       leo 	 */
    400        1.1       leo 	if (USERMODE(ps)) {
    401        1.1       leo 		if (p->p_stats.p_prof.pr_scale)
    402        1.1       leo 			addupc(pc, &curproc->p_stats.p_prof, 1);
    403        1.1       leo 	}
    404        1.1       leo 	/*
    405        1.1       leo 	 * Came from kernel (supervisor) mode.
    406        1.1       leo 	 * If we are profiling the kernel, record the tick.
    407        1.1       leo 	 */
    408        1.1       leo 	else if (profiling < 2) {
    409        1.1       leo 		register int s = pc - s_lowpc;
    410        1.1       leo 
    411        1.1       leo 		if (s < s_textsize)
    412  1.40.10.2      yamt 			kcount[s / (HISTFRACTION * sizeof(*kcount))]++;
    413        1.1       leo 	}
    414        1.1       leo 	/*
    415        1.1       leo 	 * Kernel profiling was on but has been disabled.
    416        1.1       leo 	 * Mark as no longer profiling kernel and if all profiling done,
    417        1.1       leo 	 * disable the clock.
    418        1.1       leo 	 */
    419        1.1       leo 	if (profiling && (profon & PRF_KERNEL)) {
    420        1.1       leo 		profon &= ~PRF_KERNEL;
    421        1.1       leo 		if (profon == PRF_NONE)
    422        1.1       leo 			stopprofclock();
    423        1.1       leo 	}
    424        1.1       leo }
    425        1.1       leo #endif
    426        1.7       leo 
    427        1.7       leo /***********************************************************************
    428        1.7       leo  *                   Real Time Clock support                           *
    429        1.7       leo  ***********************************************************************/
    430        1.7       leo 
    431  1.40.10.2      yamt u_int mc146818_read(void *rtc, u_int regno)
    432        1.7       leo {
    433  1.40.10.2      yamt 
    434        1.7       leo 	((struct rtc *)rtc)->rtc_regno = regno;
    435  1.40.10.2      yamt 	return ((struct rtc *)rtc)->rtc_data & 0377;
    436        1.7       leo }
    437        1.7       leo 
    438  1.40.10.2      yamt void mc146818_write(void *rtc, u_int regno, u_int value)
    439        1.7       leo {
    440  1.40.10.2      yamt 
    441        1.7       leo 	((struct rtc *)rtc)->rtc_regno = regno;
    442        1.7       leo 	((struct rtc *)rtc)->rtc_data  = value;
    443        1.7       leo }
    444        1.1       leo 
    445       1.40     joerg static int
    446       1.40     joerg atari_rtc_get(todr_chip_handle_t todr, struct clock_ymdhms *dtp)
    447        1.1       leo {
    448       1.18       leo 	int			sps;
    449       1.18       leo 	mc_todregs		clkregs;
    450       1.25       leo 	u_int			regb;
    451        1.3       leo 
    452        1.3       leo 	sps = splhigh();
    453       1.25       leo 	regb = mc146818_read(RTC, MC_REGB);
    454        1.3       leo 	MC146818_GETTOD(RTC, &clkregs);
    455        1.3       leo 	splx(sps);
    456        1.1       leo 
    457       1.25       leo 	regb &= MC_REGB_24HR|MC_REGB_BINARY;
    458       1.25       leo 	if (regb != (MC_REGB_24HR|MC_REGB_BINARY)) {
    459       1.25       leo 		printf("Error: Nonstandard RealTimeClock Configuration -"
    460       1.25       leo 			" value ignored\n"
    461       1.25       leo 			"       A write to /dev/rtc will correct this.\n");
    462  1.40.10.2      yamt 			return 0;
    463       1.25       leo 	}
    464  1.40.10.2      yamt 	if (clkregs[MC_SEC] > 59)
    465       1.40     joerg 		return -1;
    466  1.40.10.2      yamt 	if (clkregs[MC_MIN] > 59)
    467       1.40     joerg 		return -1;
    468  1.40.10.2      yamt 	if (clkregs[MC_HOUR] > 23)
    469       1.40     joerg 		return -1;
    470  1.40.10.2      yamt 	if (range_test(clkregs[MC_DOM], 1, 31))
    471       1.40     joerg 		return -1;
    472        1.3       leo 	if (range_test(clkregs[MC_MONTH], 1, 12))
    473       1.40     joerg 		return -1;
    474  1.40.10.2      yamt 	if (clkregs[MC_YEAR] > 99)
    475       1.40     joerg 		return -1;
    476       1.40     joerg 
    477       1.40     joerg 	dtp->dt_year = clkregs[MC_YEAR] + GEMSTARTOFTIME;
    478       1.40     joerg 	dtp->dt_mon  = clkregs[MC_MONTH];
    479       1.40     joerg 	dtp->dt_day  = clkregs[MC_DOM];
    480       1.40     joerg 	dtp->dt_hour = clkregs[MC_HOUR];
    481       1.40     joerg 	dtp->dt_min  = clkregs[MC_MIN];
    482       1.40     joerg 	dtp->dt_sec  = clkregs[MC_SEC];
    483       1.40     joerg 
    484       1.40     joerg 	return 0;
    485       1.40     joerg }
    486       1.40     joerg 
    487       1.40     joerg static int
    488       1.40     joerg atari_rtc_set(todr_chip_handle_t todr, struct clock_ymdhms *dtp)
    489       1.40     joerg {
    490       1.40     joerg 	int s;
    491       1.40     joerg 	mc_todregs clkregs;
    492        1.1       leo 
    493       1.40     joerg 	clkregs[MC_YEAR] = dtp->dt_year - GEMSTARTOFTIME;
    494       1.40     joerg 	clkregs[MC_MONTH] = dtp->dt_mon;
    495       1.40     joerg 	clkregs[MC_DOM] = dtp->dt_day;
    496       1.40     joerg 	clkregs[MC_HOUR] = dtp->dt_hour;
    497       1.40     joerg 	clkregs[MC_MIN] = dtp->dt_min;
    498       1.40     joerg 	clkregs[MC_SEC] = dtp->dt_sec;
    499        1.1       leo 
    500       1.40     joerg 	s = splclock();
    501       1.40     joerg 	MC146818_PUTTOD(RTC, &clkregs);
    502       1.40     joerg 	splx(s);
    503       1.40     joerg 
    504       1.40     joerg 	return 0;
    505        1.1       leo }
    506       1.40     joerg 
    507       1.14       leo /***********************************************************************
    508       1.14       leo  *                   RTC-device support				       *
    509       1.14       leo  ***********************************************************************/
    510       1.14       leo int
    511  1.40.10.1      yamt rtcopen(dev_t dev, int flag, int mode, struct lwp *l)
    512       1.14       leo {
    513       1.14       leo 	int			unit = minor(dev);
    514       1.14       leo 	struct clock_softc	*sc;
    515       1.14       leo 
    516  1.40.10.1      yamt 	sc = device_lookup_private(&clock_cd, unit);
    517  1.40.10.1      yamt 	if (sc == NULL)
    518       1.14       leo 		return ENXIO;
    519       1.14       leo 	if (sc->sc_flags & RTC_OPEN)
    520       1.14       leo 		return EBUSY;
    521       1.14       leo 
    522       1.14       leo 	sc->sc_flags = RTC_OPEN;
    523       1.14       leo 	return 0;
    524       1.14       leo }
    525        1.1       leo 
    526       1.14       leo int
    527  1.40.10.1      yamt rtcclose(dev_t dev, int flag, int mode, struct lwp *l)
    528        1.1       leo {
    529       1.14       leo 	int			unit = minor(dev);
    530  1.40.10.1      yamt 	struct clock_softc	*sc = device_lookup_private(&clock_cd, unit);
    531       1.14       leo 
    532       1.14       leo 	sc->sc_flags = 0;
    533       1.14       leo 	return 0;
    534       1.14       leo }
    535       1.14       leo 
    536       1.14       leo int
    537  1.40.10.1      yamt rtcread(dev_t dev, struct uio *uio, int flags)
    538       1.14       leo {
    539       1.14       leo 	struct clock_softc	*sc;
    540       1.14       leo 	mc_todregs		clkregs;
    541       1.14       leo 	int			s, length;
    542       1.14       leo 	char			buffer[16];
    543       1.14       leo 
    544  1.40.10.1      yamt 	sc = device_lookup_private(&clock_cd, minor(dev));
    545       1.14       leo 
    546       1.14       leo 	s = splhigh();
    547       1.14       leo 	MC146818_GETTOD(RTC, &clkregs);
    548       1.14       leo 	splx(s);
    549       1.14       leo 
    550       1.21       leo 	sprintf(buffer, "%4d%02d%02d%02d%02d.%02d\n",
    551       1.21       leo 	    clkregs[MC_YEAR] + GEMSTARTOFTIME,
    552       1.14       leo 	    clkregs[MC_MONTH], clkregs[MC_DOM],
    553       1.14       leo 	    clkregs[MC_HOUR], clkregs[MC_MIN], clkregs[MC_SEC]);
    554       1.14       leo 
    555       1.14       leo 	if (uio->uio_offset > strlen(buffer))
    556       1.14       leo 		return 0;
    557        1.1       leo 
    558       1.14       leo 	length = strlen(buffer) - uio->uio_offset;
    559       1.14       leo 	if (length > uio->uio_resid)
    560       1.14       leo 		length = uio->uio_resid;
    561        1.1       leo 
    562  1.40.10.2      yamt 	return uiomove((void *)buffer, length, uio);
    563       1.14       leo }
    564       1.14       leo 
    565       1.14       leo static int
    566  1.40.10.1      yamt twodigits(char *buffer, int pos)
    567       1.14       leo {
    568       1.14       leo 	int result = 0;
    569       1.14       leo 
    570       1.14       leo 	if (buffer[pos] >= '0' && buffer[pos] <= '9')
    571       1.14       leo 		result = (buffer[pos] - '0') * 10;
    572       1.14       leo 	if (buffer[pos+1] >= '0' && buffer[pos+1] <= '9')
    573       1.14       leo 		result += (buffer[pos+1] - '0');
    574  1.40.10.2      yamt 	return result;
    575       1.14       leo }
    576        1.1       leo 
    577       1.14       leo int
    578  1.40.10.1      yamt rtcwrite(dev_t dev, struct uio *uio, int flags)
    579       1.14       leo {
    580       1.14       leo 	mc_todregs		clkregs;
    581       1.14       leo 	int			s, length, error;
    582       1.21       leo 	char			buffer[16];
    583       1.14       leo 
    584       1.14       leo 	/*
    585       1.14       leo 	 * We require atomic updates!
    586       1.14       leo 	 */
    587       1.14       leo 	length = uio->uio_resid;
    588       1.14       leo 	if (uio->uio_offset || (length != sizeof(buffer)
    589       1.14       leo 	  && length != sizeof(buffer - 1)))
    590  1.40.10.2      yamt 		return EINVAL;
    591       1.14       leo 
    592       1.38  christos 	if ((error = uiomove((void *)buffer, sizeof(buffer), uio)))
    593  1.40.10.2      yamt 		return error;
    594        1.1       leo 
    595       1.14       leo 	if (length == sizeof(buffer) && buffer[sizeof(buffer) - 1] != '\n')
    596  1.40.10.2      yamt 		return EINVAL;
    597        1.1       leo 
    598       1.14       leo 	s = splclock();
    599       1.25       leo 	mc146818_write(RTC, MC_REGB,
    600  1.40.10.2      yamt 	    mc146818_read(RTC, MC_REGB) | MC_REGB_24HR | MC_REGB_BINARY);
    601        1.3       leo 	MC146818_GETTOD(RTC, &clkregs);
    602       1.14       leo 	splx(s);
    603       1.14       leo 
    604       1.21       leo 	clkregs[MC_SEC]   = twodigits(buffer, 13);
    605       1.21       leo 	clkregs[MC_MIN]   = twodigits(buffer, 10);
    606       1.21       leo 	clkregs[MC_HOUR]  = twodigits(buffer, 8);
    607       1.21       leo 	clkregs[MC_DOM]   = twodigits(buffer, 6);
    608       1.21       leo 	clkregs[MC_MONTH] = twodigits(buffer, 4);
    609       1.21       leo 	s = twodigits(buffer, 0) * 100 + twodigits(buffer, 2);
    610       1.14       leo 	clkregs[MC_YEAR]  = s - GEMSTARTOFTIME;
    611       1.14       leo 
    612       1.14       leo 	s = splclock();
    613        1.3       leo 	MC146818_PUTTOD(RTC, &clkregs);
    614       1.14       leo 	splx(s);
    615        1.1       leo 
    616  1.40.10.2      yamt 	return 0;
    617        1.1       leo }
    618