Home | History | Annotate | Line # | Download | only in dev
clock.c revision 1.8
      1  1.8  chopps /*	$NetBSD: clock.c,v 1.8 1994/12/28 09:25:00 chopps 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.1  chopps #include <sys/param.h>
     46  1.1  chopps #include <sys/kernel.h>
     47  1.1  chopps #include <sys/device.h>
     48  1.1  chopps #include <machine/psl.h>
     49  1.1  chopps #include <machine/cpu.h>
     50  1.1  chopps #include <amiga/amiga/device.h>
     51  1.1  chopps #include <amiga/amiga/custom.h>
     52  1.1  chopps #include <amiga/amiga/cia.h>
     53  1.1  chopps #include <amiga/dev/rtc.h>
     54  1.8  chopps #include <amiga/dev/zbusvar.h>
     55  1.1  chopps 
     56  1.1  chopps #if defined(PROF) && defined(PROFTIMER)
     57  1.1  chopps #include <sys/PROF.h>
     58  1.1  chopps #endif
     59  1.1  chopps 
     60  1.1  chopps /* the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz.
     61  1.1  chopps    We're using a 100 Hz clock. */
     62  1.1  chopps 
     63  1.1  chopps #define CLK_INTERVAL amiga_clk_interval
     64  1.4  chopps int amiga_clk_interval;
     65  1.4  chopps int eclockfreq;
     66  1.4  chopps 
     67  1.1  chopps /*
     68  1.1  chopps  * Machine-dependent clock routines.
     69  1.1  chopps  *
     70  1.1  chopps  * Startrtclock restarts the real-time clock, which provides
     71  1.1  chopps  * hardclock interrupts to kern_clock.c.
     72  1.1  chopps  *
     73  1.1  chopps  * Inittodr initializes the time of day hardware which provides
     74  1.1  chopps  * date functions.
     75  1.1  chopps  *
     76  1.1  chopps  * Resettodr restores the time of day hardware after a time change.
     77  1.1  chopps  *
     78  1.1  chopps  * A note on the real-time clock:
     79  1.1  chopps  * We actually load the clock with CLK_INTERVAL-1 instead of CLK_INTERVAL.
     80  1.1  chopps  * This is because the counter decrements to zero after N+1 enabled clock
     81  1.1  chopps  * periods where N is the value loaded into the counter.
     82  1.1  chopps  */
     83  1.1  chopps 
     84  1.1  chopps int clockmatch __P((struct device *, struct cfdata *, void *));
     85  1.1  chopps void clockattach __P((struct device *, struct device *, void *));
     86  1.1  chopps 
     87  1.1  chopps struct cfdriver clockcd = {
     88  1.7  chopps 	NULL, "clock", (cfmatch_t)clockmatch, clockattach,
     89  1.1  chopps 	DV_DULL, sizeof(struct device), NULL, 0 };
     90  1.1  chopps 
     91  1.1  chopps int
     92  1.1  chopps clockmatch(pdp, cfp, auxp)
     93  1.1  chopps 	struct device *pdp;
     94  1.1  chopps 	struct cfdata *cfp;
     95  1.1  chopps 	void *auxp;
     96  1.1  chopps {
     97  1.1  chopps 	if (matchname("clock", auxp))
     98  1.1  chopps 		return(1);
     99  1.1  chopps 	return(0);
    100  1.1  chopps }
    101  1.1  chopps 
    102  1.1  chopps /*
    103  1.1  chopps  * Start the real-time clock.
    104  1.1  chopps  */
    105  1.1  chopps void
    106  1.1  chopps clockattach(pdp, dp, auxp)
    107  1.1  chopps 	struct device *pdp, *dp;
    108  1.1  chopps 	void *auxp;
    109  1.1  chopps {
    110  1.1  chopps 	unsigned short interval;
    111  1.1  chopps 
    112  1.4  chopps 	if (eclockfreq == 0)
    113  1.4  chopps 		eclockfreq = 715909;	/* guess NTSC */
    114  1.4  chopps 
    115  1.4  chopps 	CLK_INTERVAL = (eclockfreq / 100);
    116  1.4  chopps 
    117  1.5  chopps 	printf(": system hz %d hardware hz %d\n", hz, eclockfreq);
    118  1.4  chopps 
    119  1.1  chopps 	/*
    120  1.1  chopps 	 * stop timer A
    121  1.1  chopps 	 */
    122  1.1  chopps 	ciab.cra = ciab.cra & 0xc0;
    123  1.3  chopps 	ciab.icr = 1 << 0;		/* disable timer A interrupt */
    124  1.3  chopps 	interval = ciab.icr;		/* and make sure it's clear */
    125  1.1  chopps 
    126  1.1  chopps 	/*
    127  1.1  chopps 	 * load interval into registers.
    128  1.1  chopps          * the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz
    129  1.1  chopps 	 * supprort for PAL WHEN?!?! XXX
    130  1.1  chopps 	 */
    131  1.1  chopps 	interval = CLK_INTERVAL - 1;
    132  1.1  chopps 
    133  1.1  chopps 	/*
    134  1.1  chopps 	 * order of setting is important !
    135  1.1  chopps 	 */
    136  1.1  chopps 	ciab.talo = interval & 0xff;
    137  1.1  chopps 	ciab.tahi = interval >> 8;
    138  1.1  chopps }
    139  1.1  chopps 
    140  1.1  chopps void
    141  1.1  chopps cpu_initclocks()
    142  1.1  chopps {
    143  1.1  chopps 	/*
    144  1.1  chopps 	 * enable interrupts for timer A
    145  1.1  chopps 	 */
    146  1.1  chopps 	ciab.icr = (1<<7) | (1<<0);
    147  1.1  chopps 
    148  1.1  chopps 	/*
    149  1.1  chopps 	 * start timer A in continuous shot mode
    150  1.1  chopps 	 */
    151  1.1  chopps 	ciab.cra = (ciab.cra & 0xc0) | 1;
    152  1.1  chopps 
    153  1.1  chopps 	/*
    154  1.1  chopps 	 * and globally enable interrupts for ciab
    155  1.1  chopps 	 */
    156  1.1  chopps 	custom.intena = INTF_SETCLR | INTF_EXTER;
    157  1.1  chopps }
    158  1.1  chopps 
    159  1.1  chopps setstatclockrate(hz)
    160  1.1  chopps 	int hz;
    161  1.1  chopps {
    162  1.1  chopps }
    163  1.1  chopps 
    164  1.1  chopps /*
    165  1.1  chopps  * Returns number of usec since last recorded clock "tick"
    166  1.1  chopps  * (i.e. clock interrupt).
    167  1.1  chopps  */
    168  1.1  chopps clkread()
    169  1.1  chopps {
    170  1.1  chopps 	u_char hi, hi2, lo;
    171  1.1  chopps 	u_int interval;
    172  1.1  chopps 
    173  1.1  chopps 	hi  = ciab.tahi;
    174  1.1  chopps 	lo  = ciab.talo;
    175  1.1  chopps 	hi2 = ciab.tahi;
    176  1.1  chopps 	if (hi != hi2) {
    177  1.1  chopps 		lo = ciab.talo;
    178  1.1  chopps 		hi = hi2;
    179  1.1  chopps 	}
    180  1.1  chopps 
    181  1.1  chopps 	interval = (CLK_INTERVAL - 1) - ((hi<<8) | lo);
    182  1.1  chopps 
    183  1.1  chopps 	/*
    184  1.1  chopps 	 * should read ICR and if there's an int pending, adjust interval.
    185  1.1  chopps 	 * However, * since reading ICR clears the interrupt, we'd lose a
    186  1.1  chopps 	 * hardclock int, and * this is not tolerable.
    187  1.1  chopps 	 */
    188  1.1  chopps 
    189  1.1  chopps 	return((interval * tick) / CLK_INTERVAL);
    190  1.1  chopps }
    191  1.1  chopps 
    192  1.1  chopps u_int micspertick;
    193  1.1  chopps 
    194  1.1  chopps /*
    195  1.1  chopps  * we set up as much of the CIAa as possible
    196  1.1  chopps  * as all access to chip memory are very slow.
    197  1.1  chopps  */
    198  1.1  chopps void
    199  1.1  chopps setmicspertick()
    200  1.1  chopps {
    201  1.1  chopps 	micspertick = (1000000ULL << 20) / 715909;
    202  1.1  chopps 
    203  1.1  chopps 	/*
    204  1.1  chopps 	 * disable interrupts (just in case.)
    205  1.1  chopps 	 */
    206  1.1  chopps 	ciaa.icr = 0x3;
    207  1.1  chopps 
    208  1.1  chopps 	/*
    209  1.1  chopps 	 * stop both timers if not already
    210  1.1  chopps 	 */
    211  1.1  chopps 	ciaa.cra &= ~1;
    212  1.1  chopps 	ciaa.crb &= ~1;
    213  1.1  chopps 
    214  1.1  chopps 	/*
    215  1.1  chopps 	 * set timer B in "count timer A underflows" mode
    216  1.1  chopps 	 * set tiemr A in one-shot mode
    217  1.1  chopps 	 */
    218  1.1  chopps 	ciaa.crb = (ciaa.crb & 0x80) | 0x48;
    219  1.1  chopps 	ciaa.cra = (ciaa.cra & 0xc0) | 0x08;
    220  1.1  chopps }
    221  1.1  chopps 
    222  1.1  chopps /*
    223  1.1  chopps  * this function assumes that on any entry beyond the first
    224  1.1  chopps  * the following condintions exist:
    225  1.1  chopps  * Interrupts for Timers A and B are disabled.
    226  1.1  chopps  * Timers A and B are stoped.
    227  1.1  chopps  * Timers A and B are in one-shot mode with B counting timer A underflows
    228  1.1  chopps  *
    229  1.1  chopps  */
    230  1.1  chopps void
    231  1.1  chopps delay(mic)
    232  1.1  chopps 	int mic;
    233  1.1  chopps {
    234  1.1  chopps 	u_int temp;
    235  1.1  chopps 	int s;
    236  1.1  chopps 
    237  1.1  chopps 	if (micspertick == 0)
    238  1.1  chopps 		setmicspertick();
    239  1.1  chopps 
    240  1.1  chopps 	if (mic <= 1)
    241  1.1  chopps 		return;
    242  1.1  chopps 
    243  1.1  chopps 	/*
    244  1.1  chopps 	 * basically this is going to do an integer
    245  1.1  chopps 	 * usec / (1000000 / 715909) with no loss of
    246  1.1  chopps 	 * precision
    247  1.1  chopps 	 */
    248  1.1  chopps 	temp = mic >> 12;
    249  1.1  chopps 	asm("divul %3,%1:%0" : "=d" (temp) : "d" (mic >> 12), "0" (mic << 20),
    250  1.1  chopps 	    "d" (micspertick));
    251  1.1  chopps 
    252  1.1  chopps 	if ((temp & 0xffff0000) > 0x10000) {
    253  1.1  chopps 		mic = (temp >> 16) - 1;
    254  1.1  chopps 		temp &= 0xffff;
    255  1.1  chopps 
    256  1.1  chopps 		/*
    257  1.1  chopps 		 * set timer A in continous mode
    258  1.1  chopps 		 */
    259  1.1  chopps 		ciaa.cra = (ciaa.cra & 0xc0) | 0x00;
    260  1.1  chopps 
    261  1.1  chopps 		/*
    262  1.1  chopps 		 * latch/load/start "counts of timer A underflows" in B
    263  1.1  chopps 		 */
    264  1.1  chopps 		ciaa.tblo = mic & 0xff;
    265  1.1  chopps 		ciaa.tbhi = mic >> 8;
    266  1.1  chopps 
    267  1.1  chopps 		/*
    268  1.1  chopps 		 * timer A latches 0xffff
    269  1.1  chopps 		 * and start it.
    270  1.1  chopps 		 */
    271  1.1  chopps 		ciaa.talo = 0xff;
    272  1.1  chopps 		ciaa.tahi = 0xff;
    273  1.1  chopps 		ciaa.cra |= 1;
    274  1.1  chopps 
    275  1.1  chopps 		while (ciaa.crb & 1)
    276  1.1  chopps 			;
    277  1.1  chopps 
    278  1.1  chopps 		/*
    279  1.1  chopps 		 * stop timer A
    280  1.1  chopps 		 */
    281  1.1  chopps 		ciaa.cra &= ~1;
    282  1.1  chopps 
    283  1.1  chopps 		/*
    284  1.1  chopps 		 * set timer A in one shot mode
    285  1.1  chopps 		 */
    286  1.1  chopps 		ciaa.cra = (ciaa.cra & 0xc0) | 0x08;
    287  1.1  chopps 	} else if ((temp & 0xffff0000) == 0x10000) {
    288  1.1  chopps 		temp &= 0xffff;
    289  1.1  chopps 
    290  1.1  chopps 		/*
    291  1.1  chopps 		 * timer A is in one shot latch/load/start 1 full turn
    292  1.1  chopps 		 */
    293  1.1  chopps 		ciaa.talo = 0xff;
    294  1.1  chopps 		ciaa.tahi = 0xff;
    295  1.1  chopps 		while (ciaa.cra & 1)
    296  1.1  chopps 			;
    297  1.1  chopps 	}
    298  1.1  chopps 	if (temp < 1)
    299  1.1  chopps 		return;
    300  1.1  chopps 
    301  1.1  chopps 	/*
    302  1.1  chopps 	 * temp is now residual ammount, latch/load/start it.
    303  1.1  chopps 	 */
    304  1.1  chopps 	ciaa.talo = temp & 0xff;
    305  1.1  chopps 	ciaa.tahi = temp >> 8;
    306  1.1  chopps 	while (ciaa.cra & 1)
    307  1.1  chopps 		;
    308  1.1  chopps }
    309  1.1  chopps 
    310  1.1  chopps /*
    311  1.1  chopps  * Needs to be calibrated for use, its way off most of the time
    312  1.1  chopps  */
    313  1.2  chopps void
    314  1.2  chopps DELAY(mic)
    315  1.2  chopps 	int mic;
    316  1.1  chopps {
    317  1.2  chopps 	u_long n;
    318  1.2  chopps 	short hpos;
    319  1.1  chopps 
    320  1.2  chopps 	/*
    321  1.2  chopps 	 * this function uses HSync pulses as base units. The custom chips
    322  1.2  chopps 	 * display only deals with 31.6kHz/2 refresh, this gives us a
    323  1.2  chopps 	 * resolution of 1/15800 s, which is ~63us (add some fuzz so we really
    324  1.2  chopps 	 * wait awhile, even if using small timeouts)
    325  1.2  chopps 	 */
    326  1.2  chopps 	n = mic/63 + 2;
    327  1.2  chopps 	do {
    328  1.2  chopps 		hpos = custom.vhposr & 0xff00;
    329  1.2  chopps 		while (hpos == (custom.vhposr & 0xff00))
    330  1.2  chopps 			;
    331  1.2  chopps 	} while (n--);
    332  1.1  chopps }
    333  1.1  chopps 
    334  1.1  chopps #if notyet
    335  1.1  chopps 
    336  1.1  chopps /* implement this later. I'd suggest using both timers in CIA-A, they're
    337  1.1  chopps    not yet used. */
    338  1.1  chopps 
    339  1.1  chopps #include "clock.h"
    340  1.1  chopps #if NCLOCK > 0
    341  1.1  chopps /*
    342  1.1  chopps  * /dev/clock: mappable high resolution timer.
    343  1.1  chopps  *
    344  1.1  chopps  * This code implements a 32-bit recycling counter (with a 4 usec period)
    345  1.1  chopps  * using timers 2 & 3 on the 6840 clock chip.  The counter can be mapped
    346  1.1  chopps  * RO into a user's address space to achieve low overhead (no system calls),
    347  1.1  chopps  * high-precision timing.
    348  1.1  chopps  *
    349  1.1  chopps  * Note that timer 3 is also used for the high precision profiling timer
    350  1.1  chopps  * (PROFTIMER code above).  Care should be taken when both uses are
    351  1.1  chopps  * configured as only a token effort is made to avoid conflicting use.
    352  1.1  chopps  */
    353  1.1  chopps #include <sys/proc.h>
    354  1.1  chopps #include <sys/resourcevar.h>
    355  1.1  chopps #include <sys/ioctl.h>
    356  1.1  chopps #include <sys/malloc.h>
    357  1.1  chopps #include <vm/vm.h>
    358  1.1  chopps #include <amiga/amiga/clockioctl.h>
    359  1.1  chopps #include <sys/specdev.h>
    360  1.1  chopps #include <sys/vnode.h>
    361  1.1  chopps #include <sys/mman.h>
    362  1.1  chopps 
    363  1.1  chopps int clockon = 0;		/* non-zero if high-res timer enabled */
    364  1.1  chopps #ifdef PROFTIMER
    365  1.1  chopps int  profprocs = 0;		/* # of procs using profiling timer */
    366  1.1  chopps #endif
    367  1.1  chopps #ifdef DEBUG
    368  1.1  chopps int clockdebug = 0;
    369  1.1  chopps #endif
    370  1.1  chopps 
    371  1.1  chopps /*ARGSUSED*/
    372  1.1  chopps clockopen(dev, flags)
    373  1.1  chopps 	dev_t dev;
    374  1.1  chopps {
    375  1.1  chopps #ifdef PROFTIMER
    376  1.1  chopps #ifdef PROF
    377  1.1  chopps 	/*
    378  1.1  chopps 	 * Kernel profiling enabled, give up.
    379  1.1  chopps 	 */
    380  1.1  chopps 	if (profiling)
    381  1.1  chopps 		return(EBUSY);
    382  1.1  chopps #endif
    383  1.1  chopps 	/*
    384  1.1  chopps 	 * If any user processes are profiling, give up.
    385  1.1  chopps 	 */
    386  1.1  chopps 	if (profprocs)
    387  1.1  chopps 		return(EBUSY);
    388  1.1  chopps #endif
    389  1.1  chopps 	if (!clockon) {
    390  1.1  chopps 		startclock();
    391  1.1  chopps 		clockon++;
    392  1.1  chopps 	}
    393  1.1  chopps 	return(0);
    394  1.1  chopps }
    395  1.1  chopps 
    396  1.1  chopps /*ARGSUSED*/
    397  1.1  chopps clockclose(dev, flags)
    398  1.1  chopps 	dev_t dev;
    399  1.1  chopps {
    400  1.1  chopps 	(void) clockunmmap(dev, (caddr_t)0, curproc);	/* XXX */
    401  1.1  chopps 	stopclock();
    402  1.1  chopps 	clockon = 0;
    403  1.1  chopps 	return(0);
    404  1.1  chopps }
    405  1.1  chopps 
    406  1.1  chopps /*ARGSUSED*/
    407  1.1  chopps clockioctl(dev, cmd, data, flag, p)
    408  1.1  chopps 	dev_t dev;
    409  1.7  chopps 	u_long cmd;
    410  1.1  chopps 	caddr_t data;
    411  1.1  chopps 	struct proc *p;
    412  1.1  chopps {
    413  1.1  chopps 	int error = 0;
    414  1.1  chopps 
    415  1.1  chopps 	switch (cmd) {
    416  1.1  chopps 
    417  1.1  chopps 	case CLOCKMAP:
    418  1.1  chopps 		error = clockmmap(dev, (caddr_t *)data, p);
    419  1.1  chopps 		break;
    420  1.1  chopps 
    421  1.1  chopps 	case CLOCKUNMAP:
    422  1.1  chopps 		error = clockunmmap(dev, *(caddr_t *)data, p);
    423  1.1  chopps 		break;
    424  1.1  chopps 
    425  1.1  chopps 	case CLOCKGETRES:
    426  1.1  chopps 		*(int *)data = CLK_RESOLUTION;
    427  1.1  chopps 		break;
    428  1.1  chopps 
    429  1.1  chopps 	default:
    430  1.1  chopps 		error = EINVAL;
    431  1.1  chopps 		break;
    432  1.1  chopps 	}
    433  1.1  chopps 	return(error);
    434  1.1  chopps }
    435  1.1  chopps 
    436  1.1  chopps /*ARGSUSED*/
    437  1.1  chopps clockmap(dev, off, prot)
    438  1.1  chopps 	dev_t dev;
    439  1.1  chopps {
    440  1.1  chopps 	return((off + (INTIOBASE+CLKBASE+CLKSR-1)) >> PGSHIFT);
    441  1.1  chopps }
    442  1.1  chopps 
    443  1.1  chopps clockmmap(dev, addrp, p)
    444  1.1  chopps 	dev_t dev;
    445  1.1  chopps 	caddr_t *addrp;
    446  1.1  chopps 	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.1  chopps clockunmmap(dev, addr, p)
    467  1.1  chopps 	dev_t dev;
    468  1.1  chopps 	caddr_t addr;
    469  1.1  chopps 	struct proc *p;
    470  1.1  chopps {
    471  1.1  chopps 	int rv;
    472  1.1  chopps 
    473  1.1  chopps 	if (addr == 0)
    474  1.1  chopps 		return(EINVAL);		/* XXX: how do we deal with this? */
    475  1.1  chopps 	rv = vm_deallocate(p->p_vmspace->vm_map, (vm_offset_t)addr, PAGE_SIZE);
    476  1.1  chopps 	return(rv == KERN_SUCCESS ? 0 : EINVAL);
    477  1.1  chopps }
    478  1.1  chopps 
    479  1.1  chopps startclock()
    480  1.1  chopps {
    481  1.1  chopps 	register struct clkreg *clk = (struct clkreg *)clkstd[0];
    482  1.1  chopps 
    483  1.1  chopps 	clk->clk_msb2 = -1; clk->clk_lsb2 = -1;
    484  1.1  chopps 	clk->clk_msb3 = -1; clk->clk_lsb3 = -1;
    485  1.1  chopps 
    486  1.1  chopps 	clk->clk_cr2 = CLK_CR3;
    487  1.1  chopps 	clk->clk_cr3 = CLK_OENAB|CLK_8BIT;
    488  1.1  chopps 	clk->clk_cr2 = CLK_CR1;
    489  1.1  chopps 	clk->clk_cr1 = CLK_IENAB;
    490  1.1  chopps }
    491  1.1  chopps 
    492  1.1  chopps stopclock()
    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.1  chopps  * 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.1  chopps initprofclock()
    530  1.1  chopps {
    531  1.1  chopps #if NCLOCK > 0
    532  1.1  chopps 	struct proc *p = curproc;		/* XXX */
    533  1.1  chopps 
    534  1.1  chopps 	/*
    535  1.1  chopps 	 * If the high-res timer is running, force profiling off.
    536  1.1  chopps 	 * Unfortunately, this gets reflected back to the user not as
    537  1.1  chopps 	 * an error but as a lack of results.
    538  1.1  chopps 	 */
    539  1.1  chopps 	if (clockon) {
    540  1.1  chopps 		p->p_stats->p_prof.pr_scale = 0;
    541  1.1  chopps 		return;
    542  1.1  chopps 	}
    543  1.1  chopps 	/*
    544  1.1  chopps 	 * Keep track of the number of user processes that are profiling
    545  1.1  chopps 	 * by checking the scale value.
    546  1.1  chopps 	 *
    547  1.1  chopps 	 * XXX: this all assumes that the profiling code is well behaved;
    548  1.1  chopps 	 * i.e. profil() is called once per process with pcscale non-zero
    549  1.1  chopps 	 * to turn it on, and once with pcscale zero to turn it off.
    550  1.1  chopps 	 * Also assumes you don't do any forks or execs.  Oh well, there
    551  1.1  chopps 	 * is always adb...
    552  1.1  chopps 	 */
    553  1.1  chopps 	if (p->p_stats->p_prof.pr_scale)
    554  1.1  chopps 		profprocs++;
    555  1.1  chopps 	else
    556  1.1  chopps 		profprocs--;
    557  1.1  chopps #endif
    558  1.1  chopps 	/*
    559  1.1  chopps 	 * The profile interrupt interval must be an even divisor
    560  1.1  chopps 	 * of the CLK_INTERVAL so that scaling from a system clock
    561  1.1  chopps 	 * tick to a profile clock tick is possible using integer math.
    562  1.1  chopps 	 */
    563  1.1  chopps 	if (profint > CLK_INTERVAL || (CLK_INTERVAL % profint) != 0)
    564  1.1  chopps 		profint = CLK_INTERVAL;
    565  1.1  chopps 	profscale = CLK_INTERVAL / profint;
    566  1.1  chopps }
    567  1.1  chopps 
    568  1.1  chopps startprofclock()
    569  1.1  chopps {
    570  1.1  chopps   unsigned short interval;
    571  1.1  chopps 
    572  1.1  chopps   /* stop timer B */
    573  1.1  chopps   ciab.crb = ciab.crb & 0xc0;
    574  1.1  chopps 
    575  1.1  chopps   /* load interval into registers.
    576  1.1  chopps      the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz */
    577  1.1  chopps 
    578  1.1  chopps   interval = profint - 1;
    579  1.1  chopps 
    580  1.1  chopps   /* order of setting is important ! */
    581  1.1  chopps   ciab.tblo = interval & 0xff;
    582  1.1  chopps   ciab.tbhi = interval >> 8;
    583  1.1  chopps 
    584  1.1  chopps   /* enable interrupts for timer B */
    585  1.1  chopps   ciab.icr = (1<<7) | (1<<1);
    586  1.1  chopps 
    587  1.1  chopps   /* start timer B in continuous shot mode */
    588  1.1  chopps   ciab.crb = (ciab.crb & 0xc0) | 1;
    589  1.1  chopps }
    590  1.1  chopps 
    591  1.1  chopps stopprofclock()
    592  1.1  chopps {
    593  1.1  chopps   /* stop timer B */
    594  1.1  chopps   ciab.crb = ciab.crb & 0xc0;
    595  1.1  chopps }
    596  1.1  chopps 
    597  1.1  chopps #ifdef PROF
    598  1.1  chopps /*
    599  1.1  chopps  * profclock() is expanded in line in lev6intr() unless profiling kernel.
    600  1.1  chopps  * Assumes it is called with clock interrupts blocked.
    601  1.1  chopps  */
    602  1.1  chopps profclock(pc, ps)
    603  1.1  chopps 	caddr_t pc;
    604  1.1  chopps 	int ps;
    605  1.1  chopps {
    606  1.1  chopps 	/*
    607  1.1  chopps 	 * Came from user mode.
    608  1.1  chopps 	 * If this process is being profiled record the tick.
    609  1.1  chopps 	 */
    610  1.1  chopps 	if (USERMODE(ps)) {
    611  1.1  chopps 		if (p->p_stats.p_prof.pr_scale)
    612  1.1  chopps 			addupc(pc, &curproc->p_stats.p_prof, 1);
    613  1.1  chopps 	}
    614  1.1  chopps 	/*
    615  1.1  chopps 	 * Came from kernel (supervisor) mode.
    616  1.1  chopps 	 * If we are profiling the kernel, record the tick.
    617  1.1  chopps 	 */
    618  1.1  chopps 	else if (profiling < 2) {
    619  1.1  chopps 		register int s = pc - s_lowpc;
    620  1.1  chopps 
    621  1.1  chopps 		if (s < s_textsize)
    622  1.1  chopps 			kcount[s / (HISTFRACTION * sizeof (*kcount))]++;
    623  1.1  chopps 	}
    624  1.1  chopps 	/*
    625  1.1  chopps 	 * Kernel profiling was on but has been disabled.
    626  1.1  chopps 	 * Mark as no longer profiling kernel and if all profiling done,
    627  1.1  chopps 	 * disable the clock.
    628  1.1  chopps 	 */
    629  1.1  chopps 	if (profiling && (profon & PRF_KERNEL)) {
    630  1.1  chopps 		profon &= ~PRF_KERNEL;
    631  1.1  chopps 		if (profon == PRF_NONE)
    632  1.1  chopps 			stopprofclock();
    633  1.1  chopps 	}
    634  1.1  chopps }
    635  1.1  chopps #endif
    636  1.1  chopps #endif
    637  1.1  chopps 
    638  1.1  chopps /* this is a hook set by a clock driver for the configured realtime clock,
    639  1.1  chopps    returning plain current unix-time */
    640  1.1  chopps long (*gettod) __P((void));
    641  1.1  chopps int (*settod) __P((long));
    642  1.1  chopps void *clockaddr;
    643  1.1  chopps 
    644  1.1  chopps long a3gettod __P((void));
    645  1.1  chopps long a2gettod __P((void));
    646  1.1  chopps int a3settod __P((long));
    647  1.1  chopps int a2settod __P((long));
    648  1.1  chopps int rtcinit __P((void));
    649  1.1  chopps 
    650  1.1  chopps /*
    651  1.1  chopps  * Initialize the time of day register, based on the time base which is, e.g.
    652  1.1  chopps  * from a filesystem.
    653  1.1  chopps  */
    654  1.1  chopps inittodr(base)
    655  1.1  chopps 	time_t base;
    656  1.1  chopps {
    657  1.1  chopps 	u_long timbuf = base;	/* assume no battery clock exists */
    658  1.1  chopps 
    659  1.1  chopps 	if (gettod == NULL && rtcinit() == 0)
    660  1.1  chopps 		printf("WARNING: no battery clock\n");
    661  1.1  chopps 	else
    662  1.1  chopps 		timbuf = gettod();
    663  1.1  chopps 
    664  1.1  chopps 	if (timbuf < base) {
    665  1.1  chopps 		printf("WARNING: bad date in battery clock\n");
    666  1.1  chopps 		timbuf = base;
    667  1.1  chopps 	}
    668  1.1  chopps 
    669  1.1  chopps 	/* Battery clock does not store usec's, so forget about it. */
    670  1.1  chopps 	time.tv_sec = timbuf;
    671  1.1  chopps }
    672  1.1  chopps 
    673  1.1  chopps resettodr()
    674  1.1  chopps {
    675  1.1  chopps 	if (settod && settod(time.tv_sec) == 1)
    676  1.1  chopps 		return;
    677  1.1  chopps 	printf("Cannot set battery backed clock\n");
    678  1.1  chopps }
    679  1.1  chopps 
    680  1.1  chopps int
    681  1.1  chopps rtcinit()
    682  1.1  chopps {
    683  1.1  chopps 	clockaddr = (void *)ztwomap(0xdc0000);
    684  1.1  chopps 	if (is_a3000() || is_a4000()) {
    685  1.1  chopps 		if (a3gettod() == 0)
    686  1.1  chopps 			return(0);
    687  1.1  chopps 		gettod = a3gettod;
    688  1.1  chopps 		settod = a3settod;
    689  1.1  chopps 	} else {
    690  1.1  chopps 		if (a2gettod() == 0)
    691  1.1  chopps 			return(0);
    692  1.1  chopps 		gettod = a2gettod;
    693  1.1  chopps 		settod = a2settod;
    694  1.1  chopps 	}
    695  1.1  chopps 	return(1);
    696  1.1  chopps }
    697  1.1  chopps 
    698  1.1  chopps static int month_days[12] = {
    699  1.1  chopps 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    700  1.1  chopps };
    701  1.1  chopps 
    702  1.1  chopps long
    703  1.1  chopps a3gettod()
    704  1.1  chopps {
    705  1.1  chopps 	struct rtclock3000 *rt;
    706  1.1  chopps 	int i, year, month, day, hour, min, sec;
    707  1.1  chopps 	u_long tmp;
    708  1.1  chopps 
    709  1.1  chopps 	rt = clockaddr;
    710  1.1  chopps 
    711  1.1  chopps 	/* hold clock */
    712  1.1  chopps 	rt->control1 = A3CONTROL1_HOLD_CLOCK;
    713  1.1  chopps 
    714  1.1  chopps 	/* read it */
    715  1.1  chopps 	sec   = rt->second1 * 10 + rt->second2;
    716  1.1  chopps 	min   = rt->minute1 * 10 + rt->minute2;
    717  1.1  chopps 	hour  = rt->hour1   * 10 + rt->hour2;
    718  1.1  chopps 	day   = rt->day1    * 10 + rt->day2;
    719  1.1  chopps 	month = rt->month1  * 10 + rt->month2;
    720  1.1  chopps 	year  = rt->year1   * 10 + rt->year2   + 1900;
    721  1.1  chopps 
    722  1.1  chopps 	/* let it run again.. */
    723  1.1  chopps 	rt->control1 = A3CONTROL1_FREE_CLOCK;
    724  1.1  chopps 
    725  1.1  chopps 	if (range_test(hour, 0, 23))
    726  1.1  chopps 		return(0);
    727  1.1  chopps 	if (range_test(day, 1, 31))
    728  1.1  chopps 		return(0);
    729  1.1  chopps 	if (range_test(month, 1, 12))
    730  1.1  chopps 		return(0);
    731  1.1  chopps 	if (range_test(year, STARTOFTIME, 2000))
    732  1.1  chopps 		return(0);
    733  1.1  chopps 
    734  1.1  chopps 	tmp = 0;
    735  1.1  chopps 
    736  1.1  chopps 	for (i = STARTOFTIME; i < year; i++)
    737  1.1  chopps 		tmp += days_in_year(i);
    738  1.1  chopps 	if (leapyear(year) && month > FEBRUARY)
    739  1.1  chopps 		tmp++;
    740  1.1  chopps 
    741  1.1  chopps 	for (i = 1; i < month; i++)
    742  1.1  chopps 		tmp += days_in_month(i);
    743  1.1  chopps 
    744  1.1  chopps 	tmp += (day - 1);
    745  1.1  chopps 	tmp = ((tmp * 24 + hour) * 60 + min) * 60 + sec;
    746  1.1  chopps 
    747  1.1  chopps 	return(tmp);
    748  1.1  chopps }
    749  1.1  chopps 
    750  1.1  chopps int
    751  1.1  chopps a3settod(tim)
    752  1.1  chopps 	long tim;
    753  1.1  chopps {
    754  1.1  chopps 	register int i;
    755  1.1  chopps 	register long hms, day;
    756  1.1  chopps 	u_char sec1, sec2;
    757  1.1  chopps 	u_char min1, min2;
    758  1.1  chopps 	u_char hour1, hour2;
    759  1.1  chopps 	u_char day1, day2;
    760  1.1  chopps 	u_char mon1, mon2;
    761  1.1  chopps 	u_char year1, year2;
    762  1.1  chopps 	struct rtclock3000 *rt;
    763  1.1  chopps 
    764  1.1  chopps 	rt = clockaddr;
    765  1.1  chopps 	/*
    766  1.1  chopps 	 * there seem to be problems with the bitfield addressing
    767  1.1  chopps 	 * currently used..
    768  1.1  chopps 	 */
    769  1.1  chopps return(0);
    770  1.1  chopps #if not_yet
    771  1.1  chopps 	if (rt)
    772  1.1  chopps 		return 0;
    773  1.1  chopps 
    774  1.1  chopps 	/* prepare values to be written to clock */
    775  1.1  chopps 	day = tim / SECDAY;
    776  1.1  chopps 	hms = tim % SECDAY;
    777  1.1  chopps 
    778  1.1  chopps 	hour2 = hms / 3600;
    779  1.1  chopps 	hour1 = hour2 / 10;
    780  1.1  chopps 	hour2 %= 10;
    781  1.1  chopps 
    782  1.1  chopps 	min2 = (hms % 3600) / 60;
    783  1.1  chopps 	min1 = min2 / 10;
    784  1.1  chopps 	min2 %= 10;
    785  1.1  chopps 
    786  1.1  chopps 
    787  1.1  chopps 	sec2 = (hms % 3600) % 60;
    788  1.1  chopps 	sec1 = sec2 / 10;
    789  1.1  chopps 	sec2 %= 10;
    790  1.1  chopps 
    791  1.1  chopps 	/* Number of years in days */
    792  1.1  chopps 	for (i = STARTOFTIME - 1900; day >= days_in_year(i); i++)
    793  1.1  chopps 		day -= days_in_year(i);
    794  1.1  chopps 	year1 = i / 10;
    795  1.1  chopps 	year2 = i % 10;
    796  1.1  chopps 
    797  1.1  chopps 	/* Number of months in days left */
    798  1.1  chopps 	if (leapyear(i))
    799  1.1  chopps 		days_in_month(FEBRUARY) = 29;
    800  1.1  chopps 	for (i = 1; day >= days_in_month(i); i++)
    801  1.1  chopps 		day -= days_in_month(i);
    802  1.1  chopps 	days_in_month(FEBRUARY) = 28;
    803  1.1  chopps 
    804  1.1  chopps 	mon1 = i / 10;
    805  1.1  chopps 	mon2 = i % 10;
    806  1.1  chopps 
    807  1.1  chopps 	/* Days are what is left over (+1) from all that. */
    808  1.1  chopps 	day ++;
    809  1.1  chopps 	day1 = day / 10;
    810  1.1  chopps 	day2 = day % 10;
    811  1.1  chopps 
    812  1.1  chopps 	rt->control1 = CONTROL1_HOLD_CLOCK;
    813  1.1  chopps 	rt->second1 = sec1;
    814  1.1  chopps 	rt->second2 = sec2;
    815  1.1  chopps 	rt->minute1 = min1;
    816  1.1  chopps 	rt->minute2 = min2;
    817  1.1  chopps 	rt->hour1   = hour1;
    818  1.1  chopps 	rt->hour2   = hour2;
    819  1.1  chopps 	rt->day1    = day1;
    820  1.1  chopps 	rt->day2    = day2;
    821  1.1  chopps 	rt->month1  = mon1;
    822  1.1  chopps 	rt->month2  = mon2;
    823  1.1  chopps 	rt->year1   = year1;
    824  1.1  chopps 	rt->year2   = year2;
    825  1.1  chopps 	rt->control2 = CONTROL1_FREE_CLOCK;
    826  1.1  chopps 
    827  1.1  chopps 	return 1;
    828  1.1  chopps #endif
    829  1.1  chopps }
    830  1.1  chopps 
    831  1.1  chopps long
    832  1.1  chopps a2gettod()
    833  1.1  chopps {
    834  1.1  chopps 	struct rtclock2000 *rt;
    835  1.1  chopps 	int i, year, month, day, hour, min, sec;
    836  1.1  chopps 	u_long tmp;
    837  1.1  chopps 
    838  1.1  chopps 	rt = clockaddr;
    839  1.1  chopps 
    840  1.1  chopps 	/*
    841  1.1  chopps 	 * hold clock
    842  1.1  chopps 	 */
    843  1.1  chopps 	rt->control1 |= A2CONTROL1_HOLD;
    844  1.1  chopps 	while (rt->control1 & A2CONTROL1_BUSY)
    845  1.1  chopps 		;
    846  1.1  chopps 
    847  1.1  chopps 	/*
    848  1.1  chopps 	 * read it
    849  1.1  chopps 	 */
    850  1.1  chopps 	sec = rt->second1 * 10 + rt->second2;
    851  1.1  chopps 	min = rt->minute1 * 10 + rt->minute2;
    852  1.1  chopps 	hour = (rt->hour1 & 3)  * 10 + rt->hour2;
    853  1.1  chopps 	day = rt->day1 * 10 + rt->day2;
    854  1.1  chopps 	month = rt->month1 * 10 + rt->month2;
    855  1.1  chopps 	year = rt->year1 * 10 + rt->year2   + 1900;
    856  1.1  chopps 
    857  1.1  chopps 	if ((rt->control3 & A2CONTROL3_24HMODE) == 0) {
    858  1.1  chopps 		if ((rt->hour1 & A2HOUR1_PM) == 0 && hour == 12)
    859  1.1  chopps 			hour = 0;
    860  1.1  chopps 		else if ((rt->hour1 & A2HOUR1_PM) && hour != 12)
    861  1.1  chopps 			hour += 12;
    862  1.1  chopps 	}
    863  1.1  chopps 
    864  1.1  chopps 	/*
    865  1.1  chopps 	 * release the clock
    866  1.1  chopps 	 */
    867  1.1  chopps 	rt->control1 &= ~A2CONTROL1_HOLD;
    868  1.1  chopps 
    869  1.1  chopps 	if (range_test(hour, 0, 23))
    870  1.1  chopps 		return(0);
    871  1.1  chopps 	if (range_test(day, 1, 31))
    872  1.1  chopps 		return(0);
    873  1.1  chopps 	if (range_test(month, 1, 12))
    874  1.1  chopps 		return(0);
    875  1.1  chopps 	if (range_test(year, STARTOFTIME, 2000))
    876  1.1  chopps 		return(0);
    877  1.1  chopps 
    878  1.1  chopps 	tmp = 0;
    879  1.1  chopps 
    880  1.1  chopps 	for (i = STARTOFTIME; i < year; i++)
    881  1.1  chopps 		tmp += days_in_year(i);
    882  1.1  chopps 	if (leapyear(year) && month > FEBRUARY)
    883  1.1  chopps 		tmp++;
    884  1.1  chopps 
    885  1.1  chopps 	for (i = 1; i < month; i++)
    886  1.1  chopps 		tmp += days_in_month(i);
    887  1.1  chopps 
    888  1.1  chopps 	tmp += (day - 1);
    889  1.1  chopps 	tmp = ((tmp * 24 + hour) * 60 + min) * 60 + sec;
    890  1.1  chopps 
    891  1.1  chopps 	return(tmp);
    892  1.1  chopps }
    893  1.1  chopps 
    894  1.1  chopps /*
    895  1.1  chopps  * there is some question as to whether this works
    896  1.1  chopps  * I guess
    897  1.1  chopps  */
    898  1.1  chopps int
    899  1.1  chopps a2settod(tim)
    900  1.1  chopps 	long tim;
    901  1.1  chopps {
    902  1.1  chopps 
    903  1.1  chopps 	int i;
    904  1.1  chopps 	long hms, day;
    905  1.1  chopps 	u_char sec1, sec2;
    906  1.1  chopps 	u_char min1, min2;
    907  1.1  chopps 	u_char hour1, hour2;
    908  1.1  chopps 	u_char day1, day2;
    909  1.1  chopps 	u_char mon1, mon2;
    910  1.1  chopps 	u_char year1, year2;
    911  1.1  chopps 	struct rtclock2000 *rt;
    912  1.1  chopps 
    913  1.1  chopps 	rt = clockaddr;
    914  1.1  chopps 	/*
    915  1.1  chopps 	 * there seem to be problems with the bitfield addressing
    916  1.1  chopps 	 * currently used..
    917  1.1  chopps 	 *
    918  1.1  chopps 	 * XXX Check out the above where we (hour1 & 3)
    919  1.1  chopps 	 */
    920  1.1  chopps return(0);
    921  1.1  chopps #if not_yet
    922  1.1  chopps 	if (! rt)
    923  1.1  chopps 		return 0;
    924  1.1  chopps 
    925  1.1  chopps 	/* prepare values to be written to clock */
    926  1.1  chopps 	day = tim / SECDAY;
    927  1.1  chopps 	hms = tim % SECDAY;
    928  1.1  chopps 
    929  1.1  chopps 	hour2 = hms / 3600;
    930  1.1  chopps 	hour1 = hour2 / 10;
    931  1.1  chopps 	hour2 %= 10;
    932  1.1  chopps 
    933  1.1  chopps 	min2 = (hms % 3600) / 60;
    934  1.1  chopps 	min1 = min2 / 10;
    935  1.1  chopps 	min2 %= 10;
    936  1.1  chopps 
    937  1.1  chopps 
    938  1.1  chopps 	sec2 = (hms % 3600) % 60;
    939  1.1  chopps 	sec1 = sec2 / 10;
    940  1.1  chopps 	sec2 %= 10;
    941  1.1  chopps 
    942  1.1  chopps 	/* Number of years in days */
    943  1.1  chopps 	for (i = STARTOFTIME - 1900; day >= days_in_year(i); i++)
    944  1.1  chopps 		day -= days_in_year(i);
    945  1.1  chopps 	year1 = i / 10;
    946  1.1  chopps 	year2 = i % 10;
    947  1.1  chopps 
    948  1.1  chopps 	/* Number of months in days left */
    949  1.1  chopps 	if (leapyear(i))
    950  1.1  chopps 		days_in_month(FEBRUARY) = 29;
    951  1.1  chopps 	for (i = 1; day >= days_in_month(i); i++)
    952  1.1  chopps 		day -= days_in_month(i);
    953  1.1  chopps 	days_in_month(FEBRUARY) = 28;
    954  1.1  chopps 
    955  1.1  chopps 	mon1 = i / 10;
    956  1.1  chopps 	mon2 = i % 10;
    957  1.1  chopps 
    958  1.1  chopps 	/* Days are what is left over (+1) from all that. */
    959  1.1  chopps 	day ++;
    960  1.1  chopps 	day1 = day / 10;
    961  1.1  chopps 	day2 = day % 10;
    962  1.1  chopps 
    963  1.1  chopps 	/*
    964  1.1  chopps 	 * XXXX spin wait as with reading???
    965  1.1  chopps 	 */
    966  1.1  chopps 	rt->control1 = A2CONTROL1_HOLD_CLOCK;
    967  1.1  chopps 	rt->second1 = sec1;
    968  1.1  chopps 	rt->second2 = sec2;
    969  1.1  chopps 	rt->minute1 = min1;
    970  1.1  chopps 	rt->minute2 = min2;
    971  1.1  chopps 	rt->hour1   = hour1;
    972  1.1  chopps 	rt->hour2   = hour2;
    973  1.1  chopps 	rt->day1    = day1;
    974  1.1  chopps 	rt->day2    = day2;
    975  1.1  chopps 	rt->month1  = mon1;
    976  1.1  chopps 	rt->month2  = mon2;
    977  1.1  chopps 	rt->year1   = year1;
    978  1.1  chopps 	rt->year2   = year2;
    979  1.1  chopps 	rt->control2 = CONTROL1_FREE_CLOCK;
    980  1.1  chopps 
    981  1.1  chopps   return 1;
    982  1.1  chopps #endif
    983  1.1  chopps }
    984