Home | History | Annotate | Line # | Download | only in dev
clock.c revision 1.27
      1  1.27        is /*	$NetBSD: clock.c,v 1.27 1997/07/06 22:27:19 is 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.13     veego #include <sys/systm.h>
     49   1.1    chopps #include <machine/psl.h>
     50   1.1    chopps #include <machine/cpu.h>
     51   1.1    chopps #include <amiga/amiga/device.h>
     52   1.1    chopps #include <amiga/amiga/custom.h>
     53   1.1    chopps #include <amiga/amiga/cia.h>
     54  1.14        is #ifdef DRACO
     55  1.27        is #include <dev/ic/ds.h>
     56  1.14        is #include <amiga/amiga/drcustom.h>
     57  1.14        is #endif
     58   1.1    chopps #include <amiga/dev/rtc.h>
     59   1.8    chopps #include <amiga/dev/zbusvar.h>
     60   1.1    chopps 
     61  1.26     veego #include <dev/clock_subr.h>
     62  1.26     veego 
     63   1.1    chopps #if defined(PROF) && defined(PROFTIMER)
     64   1.1    chopps #include <sys/PROF.h>
     65   1.1    chopps #endif
     66   1.1    chopps 
     67   1.1    chopps /* the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz.
     68   1.1    chopps    We're using a 100 Hz clock. */
     69   1.1    chopps 
     70   1.1    chopps #define CLK_INTERVAL amiga_clk_interval
     71   1.4    chopps int amiga_clk_interval;
     72   1.4    chopps int eclockfreq;
     73  1.14        is struct CIA *clockcia;
     74   1.4    chopps 
     75   1.1    chopps /*
     76   1.1    chopps  * Machine-dependent clock routines.
     77   1.1    chopps  *
     78   1.1    chopps  * Startrtclock restarts the real-time clock, which provides
     79   1.1    chopps  * hardclock interrupts to kern_clock.c.
     80   1.1    chopps  *
     81   1.1    chopps  * Inittodr initializes the time of day hardware which provides
     82   1.1    chopps  * date functions.
     83   1.1    chopps  *
     84   1.1    chopps  * Resettodr restores the time of day hardware after a time change.
     85   1.1    chopps  *
     86   1.1    chopps  * A note on the real-time clock:
     87   1.1    chopps  * We actually load the clock with CLK_INTERVAL-1 instead of CLK_INTERVAL.
     88   1.1    chopps  * This is because the counter decrements to zero after N+1 enabled clock
     89   1.1    chopps  * periods where N is the value loaded into the counter.
     90   1.1    chopps  */
     91   1.1    chopps 
     92  1.24     veego int clockmatch __P((struct device *, struct cfdata *, void *));
     93   1.1    chopps void clockattach __P((struct device *, struct device *, void *));
     94  1.13     veego void cpu_initclocks __P((void));
     95  1.23        is void calibrate_delay __P((struct device *));
     96   1.1    chopps 
     97  1.11   thorpej struct cfattach clock_ca = {
     98  1.11   thorpej 	sizeof(struct device), clockmatch, clockattach
     99  1.11   thorpej };
    100  1.11   thorpej 
    101  1.11   thorpej struct cfdriver clock_cd = {
    102  1.11   thorpej 	NULL, "clock", DV_DULL, NULL, 0 };
    103   1.1    chopps 
    104   1.1    chopps int
    105  1.24     veego clockmatch(pdp, cfp, auxp)
    106   1.1    chopps 	struct device *pdp;
    107  1.24     veego 	struct cfdata *cfp;
    108  1.24     veego 	void *auxp;
    109   1.1    chopps {
    110  1.18        is 	if (matchname("clock", auxp))
    111   1.1    chopps 		return(1);
    112   1.1    chopps 	return(0);
    113   1.1    chopps }
    114   1.1    chopps 
    115   1.1    chopps /*
    116   1.1    chopps  * Start the real-time clock.
    117   1.1    chopps  */
    118   1.1    chopps void
    119   1.1    chopps clockattach(pdp, dp, auxp)
    120   1.1    chopps 	struct device *pdp, *dp;
    121   1.1    chopps 	void *auxp;
    122   1.1    chopps {
    123  1.18        is 	char *clockchip;
    124   1.1    chopps 	unsigned short interval;
    125  1.18        is #ifdef DRACO
    126  1.18        is 	u_char dracorev;
    127  1.18        is #endif
    128   1.1    chopps 
    129   1.4    chopps 	if (eclockfreq == 0)
    130   1.4    chopps 		eclockfreq = 715909;	/* guess NTSC */
    131   1.4    chopps 
    132   1.4    chopps 	CLK_INTERVAL = (eclockfreq / 100);
    133   1.4    chopps 
    134  1.14        is #ifdef DRACO
    135  1.18        is 	dracorev = is_draco();
    136  1.18        is 	if (dracorev >= 4) {
    137  1.18        is 		CLK_INTERVAL = (eclockfreq / 700);
    138  1.18        is 		clockchip = "QuickLogic";
    139  1.18        is 	} else if (dracorev) {
    140  1.14        is 		clockcia = (struct CIA *)CIAAbase;
    141  1.18        is 		clockchip = "CIA A";
    142  1.14        is 	} else
    143  1.14        is #endif
    144  1.14        is 	{
    145  1.14        is 		clockcia = (struct CIA *)CIABbase;
    146  1.18        is 		clockchip = "CIA B";
    147  1.14        is 	}
    148  1.14        is 
    149  1.25        is 	if (dp)
    150  1.23        is 		printf(": %s system hz %d hardware hz %d\n", clockchip, hz,
    151  1.20    mhitch #ifdef DRACO
    152  1.18        is 		dracorev >= 4 ? eclockfreq / 7 : eclockfreq);
    153  1.20    mhitch #else
    154  1.20    mhitch 		eclockfreq);
    155  1.20    mhitch #endif
    156  1.18        is 
    157  1.18        is #ifdef DRACO
    158  1.18        is 	if (dracorev >= 4) {
    159  1.18        is 		/*
    160  1.18        is 		 * can't preload anything beforehand, timer is free_running;
    161  1.18        is 		 * but need this for delay calibration.
    162  1.18        is 		 */
    163  1.18        is 
    164  1.18        is 		draco_ioct->io_timerlo = CLK_INTERVAL & 0xff;
    165  1.18        is 		draco_ioct->io_timerhi = CLK_INTERVAL >> 8;
    166   1.4    chopps 
    167  1.25        is 		calibrate_delay(dp);
    168  1.18        is 
    169  1.18        is 		return;
    170  1.18        is 	}
    171  1.18        is #endif
    172   1.1    chopps 	/*
    173   1.1    chopps 	 * stop timer A
    174   1.1    chopps 	 */
    175  1.14        is 	clockcia->cra = clockcia->cra & 0xc0;
    176  1.14        is 	clockcia->icr = 1 << 0;		/* disable timer A interrupt */
    177  1.14        is 	interval = clockcia->icr;		/* and make sure it's clear */
    178   1.1    chopps 
    179   1.1    chopps 	/*
    180   1.1    chopps 	 * load interval into registers.
    181   1.1    chopps          * the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz
    182   1.1    chopps 	 * supprort for PAL WHEN?!?! XXX
    183   1.1    chopps 	 */
    184   1.1    chopps 	interval = CLK_INTERVAL - 1;
    185   1.1    chopps 
    186   1.1    chopps 	/*
    187   1.1    chopps 	 * order of setting is important !
    188   1.1    chopps 	 */
    189  1.14        is 	clockcia->talo = interval & 0xff;
    190  1.14        is 	clockcia->tahi = interval >> 8;
    191  1.18        is 	/*
    192  1.18        is 	 * start timer A in continuous mode
    193  1.18        is 	 */
    194  1.18        is 	clockcia->cra = (clockcia->cra & 0xc0) | 1;
    195  1.18        is 
    196  1.25        is 	calibrate_delay(dp);
    197  1.18        is }
    198  1.18        is 
    199  1.18        is /*
    200  1.18        is  * Calibrate delay loop.
    201  1.18        is  * We use two iterations because we don't have enough bits to do a factor of
    202  1.18        is  * 8 with better than 1%.
    203  1.18        is  *
    204  1.18        is  * XXX Note that we MUST stay below 1 tick if using clkread(), even for
    205  1.18        is  * underestimated values of delaydivisor.
    206  1.18        is  *
    207  1.18        is  * XXX the "ns" below is only correct for a shift of 10 bits, and even then
    208  1.18        is  * off by 2.4%
    209  1.18        is  */
    210  1.18        is 
    211  1.25        is void calibrate_delay(dp)
    212  1.25        is 	struct device *dp;
    213  1.18        is {
    214  1.18        is 	unsigned long t1, t2;
    215  1.18        is 	extern u_int32_t delaydivisor;
    216  1.18        is 		/* XXX this should be defined elsewhere */
    217  1.18        is 
    218  1.25        is 	if (dp)
    219  1.23        is 		printf("Calibrating delay loop... ");
    220  1.18        is 
    221  1.18        is 	do {
    222  1.18        is 		t1 = clkread();
    223  1.18        is 		delay(1024);
    224  1.18        is 		t2 = clkread();
    225  1.18        is 	} while (t2 <= t1);
    226  1.18        is 	t2 -= t1;
    227  1.18        is 	delaydivisor = (delaydivisor * t2 + 1023) >> 10;
    228  1.18        is #ifdef DIAGNOSTIC
    229  1.25        is 	if (dp)
    230  1.25        is 		printf("\ndiff %ld us, new divisor %u/1024 us\n", t2,
    231  1.25        is 		    delaydivisor);
    232  1.18        is 	do {
    233  1.18        is 		t1 = clkread();
    234  1.18        is 		delay(1024);
    235  1.18        is 		t2 = clkread();
    236  1.18        is 	} while (t2 <= t1);
    237  1.18        is 	t2 -= t1;
    238  1.18        is 	delaydivisor = (delaydivisor * t2 + 1023) >> 10;
    239  1.25        is 	if (dp)
    240  1.25        is 		printf("diff %ld us, new divisor %u/1024 us\n", t2,
    241  1.25        is 		    delaydivisor);
    242  1.18        is #endif
    243  1.18        is 	do {
    244  1.18        is 		t1 = clkread();
    245  1.18        is 		delay(1024);
    246  1.18        is 		t2 = clkread();
    247  1.18        is 	} while (t2 <= t1);
    248  1.18        is 	t2 -= t1;
    249  1.18        is 	delaydivisor = (delaydivisor * t2 + 1023) >> 10;
    250  1.18        is #ifdef DIAGNOSTIC
    251  1.25        is 	if (dp)
    252  1.23        is 		printf("diff %ld us, new divisor ", t2);
    253  1.18        is #endif
    254  1.25        is 	if (dp)
    255  1.25        is 		printf("%u/1024 us\n", delaydivisor);
    256   1.1    chopps }
    257   1.1    chopps 
    258   1.1    chopps void
    259   1.1    chopps cpu_initclocks()
    260   1.1    chopps {
    261  1.20    mhitch #ifdef DRACO
    262  1.18        is 	unsigned char dracorev;
    263  1.18        is 	dracorev = is_draco();
    264  1.18        is 	if (dracorev >= 4) {
    265  1.18        is 		draco_ioct->io_timerlo = CLK_INTERVAL & 0xFF;
    266  1.18        is 		draco_ioct->io_timerhi = CLK_INTERVAL >> 8;
    267  1.18        is 		draco_ioct->io_timerrst = 0;	/* any value resets */
    268  1.18        is 		draco_ioct->io_status2 |= DRSTAT2_TMRINTENA;
    269  1.18        is 
    270  1.18        is 		return;
    271  1.18        is 	}
    272  1.18        is #endif
    273   1.1    chopps 	/*
    274   1.1    chopps 	 * enable interrupts for timer A
    275   1.1    chopps 	 */
    276  1.14        is 	clockcia->icr = (1<<7) | (1<<0);
    277   1.1    chopps 
    278   1.1    chopps 	/*
    279   1.1    chopps 	 * start timer A in continuous shot mode
    280   1.1    chopps 	 */
    281  1.14        is 	clockcia->cra = (clockcia->cra & 0xc0) | 1;
    282   1.1    chopps 
    283   1.1    chopps 	/*
    284   1.1    chopps 	 * and globally enable interrupts for ciab
    285   1.1    chopps 	 */
    286  1.14        is #ifdef DRACO
    287  1.18        is 	if (dracorev)		/* we use cia a on DraCo */
    288  1.14        is 		*draco_intena |= DRIRQ_INT2;
    289  1.14        is 	else
    290  1.14        is #endif
    291  1.14        is 		custom.intena = INTF_SETCLR | INTF_EXTER;
    292  1.18        is 
    293   1.1    chopps }
    294   1.1    chopps 
    295  1.13     veego void
    296   1.1    chopps setstatclockrate(hz)
    297   1.1    chopps 	int hz;
    298   1.1    chopps {
    299   1.1    chopps }
    300   1.1    chopps 
    301   1.1    chopps /*
    302   1.1    chopps  * Returns number of usec since last recorded clock "tick"
    303   1.1    chopps  * (i.e. clock interrupt).
    304   1.1    chopps  */
    305  1.13     veego u_long
    306   1.1    chopps clkread()
    307   1.1    chopps {
    308  1.18        is 	u_int interval;
    309   1.1    chopps 	u_char hi, hi2, lo;
    310   1.1    chopps 
    311  1.14        is #ifdef DRACO
    312  1.18        is 	if (is_draco() >= 4) {
    313  1.18        is 		hi2 = draco_ioct->io_chiprev;	/* latch timer */
    314  1.18        is 		hi = draco_ioct->io_timerhi;
    315  1.18        is 		lo = draco_ioct->io_timerlo;
    316  1.18        is 		interval = ((hi<<8) | lo);
    317  1.18        is 		if (interval > CLK_INTERVAL)	/* timer underflow */
    318  1.18        is 			interval = 65536 + CLK_INTERVAL - interval;
    319  1.18        is 		else
    320  1.18        is 			interval = CLK_INTERVAL - interval;
    321   1.1    chopps 
    322  1.18        is 	} else
    323  1.14        is #endif
    324  1.18        is 	{
    325  1.18        is 		hi  = clockcia->tahi;
    326  1.18        is 		lo  = clockcia->talo;
    327  1.18        is 		hi2 = clockcia->tahi;
    328  1.18        is 		if (hi != hi2) {
    329  1.18        is 			lo = clockcia->talo;
    330  1.18        is 			hi = hi2;
    331  1.18        is 		}
    332   1.1    chopps 
    333  1.18        is 		interval = (CLK_INTERVAL - 1) - ((hi<<8) | lo);
    334  1.18        is 
    335   1.1    chopps 		/*
    336  1.18        is 		 * should read ICR and if there's an int pending, adjust
    337  1.18        is 		 * interval. However, since reading ICR clears the interrupt,
    338  1.18        is 		 * we'd lose a hardclock int, and this is not tolerable.
    339   1.1    chopps 		 */
    340   1.1    chopps 	}
    341   1.1    chopps 
    342  1.18        is 	return((interval * tick) / CLK_INTERVAL);
    343   1.1    chopps }
    344   1.1    chopps 
    345   1.1    chopps #if notyet
    346   1.1    chopps 
    347   1.1    chopps /* implement this later. I'd suggest using both timers in CIA-A, they're
    348   1.1    chopps    not yet used. */
    349   1.1    chopps 
    350   1.1    chopps #include "clock.h"
    351   1.1    chopps #if NCLOCK > 0
    352   1.1    chopps /*
    353   1.1    chopps  * /dev/clock: mappable high resolution timer.
    354   1.1    chopps  *
    355   1.1    chopps  * This code implements a 32-bit recycling counter (with a 4 usec period)
    356   1.1    chopps  * using timers 2 & 3 on the 6840 clock chip.  The counter can be mapped
    357   1.1    chopps  * RO into a user's address space to achieve low overhead (no system calls),
    358   1.1    chopps  * high-precision timing.
    359   1.1    chopps  *
    360   1.1    chopps  * Note that timer 3 is also used for the high precision profiling timer
    361   1.1    chopps  * (PROFTIMER code above).  Care should be taken when both uses are
    362   1.1    chopps  * configured as only a token effort is made to avoid conflicting use.
    363   1.1    chopps  */
    364   1.1    chopps #include <sys/proc.h>
    365   1.1    chopps #include <sys/resourcevar.h>
    366   1.1    chopps #include <sys/ioctl.h>
    367   1.1    chopps #include <sys/malloc.h>
    368   1.1    chopps #include <vm/vm.h>
    369   1.1    chopps #include <amiga/amiga/clockioctl.h>
    370   1.1    chopps #include <sys/specdev.h>
    371   1.1    chopps #include <sys/vnode.h>
    372   1.1    chopps #include <sys/mman.h>
    373   1.1    chopps 
    374   1.1    chopps int clockon = 0;		/* non-zero if high-res timer enabled */
    375   1.1    chopps #ifdef PROFTIMER
    376   1.1    chopps int  profprocs = 0;		/* # of procs using profiling timer */
    377   1.1    chopps #endif
    378   1.1    chopps #ifdef DEBUG
    379   1.1    chopps int clockdebug = 0;
    380   1.1    chopps #endif
    381   1.1    chopps 
    382   1.1    chopps /*ARGSUSED*/
    383   1.1    chopps clockopen(dev, flags)
    384   1.1    chopps 	dev_t dev;
    385   1.1    chopps {
    386   1.1    chopps #ifdef PROFTIMER
    387   1.1    chopps #ifdef PROF
    388   1.1    chopps 	/*
    389   1.1    chopps 	 * Kernel profiling enabled, give up.
    390   1.1    chopps 	 */
    391   1.1    chopps 	if (profiling)
    392   1.1    chopps 		return(EBUSY);
    393   1.1    chopps #endif
    394   1.1    chopps 	/*
    395   1.1    chopps 	 * If any user processes are profiling, give up.
    396   1.1    chopps 	 */
    397   1.1    chopps 	if (profprocs)
    398   1.1    chopps 		return(EBUSY);
    399   1.1    chopps #endif
    400   1.1    chopps 	if (!clockon) {
    401   1.1    chopps 		startclock();
    402   1.1    chopps 		clockon++;
    403   1.1    chopps 	}
    404   1.1    chopps 	return(0);
    405   1.1    chopps }
    406   1.1    chopps 
    407   1.1    chopps /*ARGSUSED*/
    408   1.1    chopps clockclose(dev, flags)
    409   1.1    chopps 	dev_t dev;
    410   1.1    chopps {
    411   1.1    chopps 	(void) clockunmmap(dev, (caddr_t)0, curproc);	/* XXX */
    412   1.1    chopps 	stopclock();
    413   1.1    chopps 	clockon = 0;
    414   1.1    chopps 	return(0);
    415   1.1    chopps }
    416   1.1    chopps 
    417   1.1    chopps /*ARGSUSED*/
    418   1.1    chopps clockioctl(dev, cmd, data, flag, p)
    419   1.1    chopps 	dev_t dev;
    420   1.7    chopps 	u_long cmd;
    421   1.1    chopps 	caddr_t data;
    422   1.1    chopps 	struct proc *p;
    423   1.1    chopps {
    424   1.1    chopps 	int error = 0;
    425   1.1    chopps 
    426   1.1    chopps 	switch (cmd) {
    427   1.1    chopps 
    428   1.1    chopps 	case CLOCKMAP:
    429   1.1    chopps 		error = clockmmap(dev, (caddr_t *)data, p);
    430   1.1    chopps 		break;
    431   1.1    chopps 
    432   1.1    chopps 	case CLOCKUNMAP:
    433   1.1    chopps 		error = clockunmmap(dev, *(caddr_t *)data, p);
    434   1.1    chopps 		break;
    435   1.1    chopps 
    436   1.1    chopps 	case CLOCKGETRES:
    437   1.1    chopps 		*(int *)data = CLK_RESOLUTION;
    438   1.1    chopps 		break;
    439   1.1    chopps 
    440   1.1    chopps 	default:
    441   1.1    chopps 		error = EINVAL;
    442   1.1    chopps 		break;
    443   1.1    chopps 	}
    444   1.1    chopps 	return(error);
    445   1.1    chopps }
    446   1.1    chopps 
    447   1.1    chopps /*ARGSUSED*/
    448   1.1    chopps clockmap(dev, off, prot)
    449   1.1    chopps 	dev_t dev;
    450   1.1    chopps {
    451   1.1    chopps 	return((off + (INTIOBASE+CLKBASE+CLKSR-1)) >> PGSHIFT);
    452   1.1    chopps }
    453   1.1    chopps 
    454   1.1    chopps clockmmap(dev, addrp, p)
    455   1.1    chopps 	dev_t dev;
    456   1.1    chopps 	caddr_t *addrp;
    457   1.1    chopps 	struct proc *p;
    458   1.1    chopps {
    459   1.1    chopps 	int error;
    460   1.1    chopps 	struct vnode vn;
    461   1.1    chopps 	struct specinfo si;
    462   1.1    chopps 	int flags;
    463   1.1    chopps 
    464   1.1    chopps 	flags = MAP_FILE|MAP_SHARED;
    465   1.1    chopps 	if (*addrp)
    466   1.1    chopps 		flags |= MAP_FIXED;
    467   1.1    chopps 	else
    468   1.1    chopps 		*addrp = (caddr_t)0x1000000;	/* XXX */
    469   1.1    chopps 	vn.v_type = VCHR;			/* XXX */
    470   1.1    chopps 	vn.v_specinfo = &si;			/* XXX */
    471   1.1    chopps 	vn.v_rdev = dev;			/* XXX */
    472   1.1    chopps 	error = vm_mmap(&p->p_vmspace->vm_map, (vm_offset_t *)addrp,
    473   1.1    chopps 			PAGE_SIZE, VM_PROT_ALL, flags, (caddr_t)&vn, 0);
    474   1.1    chopps 	return(error);
    475   1.1    chopps }
    476   1.1    chopps 
    477   1.1    chopps clockunmmap(dev, addr, p)
    478   1.1    chopps 	dev_t dev;
    479   1.1    chopps 	caddr_t addr;
    480   1.1    chopps 	struct proc *p;
    481   1.1    chopps {
    482   1.1    chopps 	int rv;
    483   1.1    chopps 
    484   1.1    chopps 	if (addr == 0)
    485   1.1    chopps 		return(EINVAL);		/* XXX: how do we deal with this? */
    486   1.1    chopps 	rv = vm_deallocate(p->p_vmspace->vm_map, (vm_offset_t)addr, PAGE_SIZE);
    487   1.1    chopps 	return(rv == KERN_SUCCESS ? 0 : EINVAL);
    488   1.1    chopps }
    489   1.1    chopps 
    490   1.1    chopps startclock()
    491   1.1    chopps {
    492   1.1    chopps 	register struct clkreg *clk = (struct clkreg *)clkstd[0];
    493   1.1    chopps 
    494   1.1    chopps 	clk->clk_msb2 = -1; clk->clk_lsb2 = -1;
    495   1.1    chopps 	clk->clk_msb3 = -1; clk->clk_lsb3 = -1;
    496   1.1    chopps 
    497   1.1    chopps 	clk->clk_cr2 = CLK_CR3;
    498   1.1    chopps 	clk->clk_cr3 = CLK_OENAB|CLK_8BIT;
    499   1.1    chopps 	clk->clk_cr2 = CLK_CR1;
    500   1.1    chopps 	clk->clk_cr1 = CLK_IENAB;
    501   1.1    chopps }
    502   1.1    chopps 
    503   1.1    chopps stopclock()
    504   1.1    chopps {
    505   1.1    chopps 	register struct clkreg *clk = (struct clkreg *)clkstd[0];
    506   1.1    chopps 
    507   1.1    chopps 	clk->clk_cr2 = CLK_CR3;
    508   1.1    chopps 	clk->clk_cr3 = 0;
    509   1.1    chopps 	clk->clk_cr2 = CLK_CR1;
    510   1.1    chopps 	clk->clk_cr1 = CLK_IENAB;
    511   1.1    chopps }
    512   1.1    chopps #endif
    513   1.1    chopps 
    514   1.1    chopps #endif
    515   1.1    chopps 
    516   1.1    chopps 
    517   1.1    chopps #ifdef PROFTIMER
    518   1.1    chopps /*
    519   1.1    chopps  * This code allows the amiga kernel to use one of the extra timers on
    520   1.1    chopps  * the clock chip for profiling, instead of the regular system timer.
    521   1.1    chopps  * The advantage of this is that the profiling timer can be turned up to
    522   1.1    chopps  * a higher interrupt rate, giving finer resolution timing. The profclock
    523   1.1    chopps  * routine is called from the lev6intr in locore, and is a specialized
    524   1.1    chopps  * routine that calls addupc. The overhead then is far less than if
    525   1.1    chopps  * hardclock/softclock was called. Further, the context switch code in
    526   1.1    chopps  * locore has been changed to turn the profile clock on/off when switching
    527   1.1    chopps  * into/out of a process that is profiling (startprofclock/stopprofclock).
    528   1.1    chopps  * This reduces the impact of the profiling clock on other users, and might
    529   1.1    chopps  * possibly increase the accuracy of the profiling.
    530   1.1    chopps  */
    531   1.1    chopps int  profint   = PRF_INTERVAL;	/* Clock ticks between interrupts */
    532   1.1    chopps int  profscale = 0;		/* Scale factor from sys clock to prof clock */
    533   1.1    chopps char profon    = 0;		/* Is profiling clock on? */
    534   1.1    chopps 
    535   1.1    chopps /* profon values - do not change, locore.s assumes these values */
    536   1.1    chopps #define PRF_NONE	0x00
    537   1.1    chopps #define	PRF_USER	0x01
    538   1.1    chopps #define	PRF_KERNEL	0x80
    539   1.1    chopps 
    540   1.1    chopps initprofclock()
    541   1.1    chopps {
    542   1.1    chopps #if NCLOCK > 0
    543   1.1    chopps 	struct proc *p = curproc;		/* XXX */
    544   1.1    chopps 
    545   1.1    chopps 	/*
    546   1.1    chopps 	 * If the high-res timer is running, force profiling off.
    547   1.1    chopps 	 * Unfortunately, this gets reflected back to the user not as
    548   1.1    chopps 	 * an error but as a lack of results.
    549   1.1    chopps 	 */
    550   1.1    chopps 	if (clockon) {
    551   1.1    chopps 		p->p_stats->p_prof.pr_scale = 0;
    552   1.1    chopps 		return;
    553   1.1    chopps 	}
    554   1.1    chopps 	/*
    555   1.1    chopps 	 * Keep track of the number of user processes that are profiling
    556   1.1    chopps 	 * by checking the scale value.
    557   1.1    chopps 	 *
    558   1.1    chopps 	 * XXX: this all assumes that the profiling code is well behaved;
    559   1.1    chopps 	 * i.e. profil() is called once per process with pcscale non-zero
    560   1.1    chopps 	 * to turn it on, and once with pcscale zero to turn it off.
    561   1.1    chopps 	 * Also assumes you don't do any forks or execs.  Oh well, there
    562   1.1    chopps 	 * is always adb...
    563   1.1    chopps 	 */
    564   1.1    chopps 	if (p->p_stats->p_prof.pr_scale)
    565   1.1    chopps 		profprocs++;
    566   1.1    chopps 	else
    567   1.1    chopps 		profprocs--;
    568   1.1    chopps #endif
    569   1.1    chopps 	/*
    570   1.1    chopps 	 * The profile interrupt interval must be an even divisor
    571   1.1    chopps 	 * of the CLK_INTERVAL so that scaling from a system clock
    572   1.1    chopps 	 * tick to a profile clock tick is possible using integer math.
    573   1.1    chopps 	 */
    574   1.1    chopps 	if (profint > CLK_INTERVAL || (CLK_INTERVAL % profint) != 0)
    575   1.1    chopps 		profint = CLK_INTERVAL;
    576   1.1    chopps 	profscale = CLK_INTERVAL / profint;
    577   1.1    chopps }
    578   1.1    chopps 
    579   1.1    chopps startprofclock()
    580   1.1    chopps {
    581   1.1    chopps   unsigned short interval;
    582   1.1    chopps 
    583   1.1    chopps   /* stop timer B */
    584  1.14        is   clockcia->crb = clockcia->crb & 0xc0;
    585   1.1    chopps 
    586   1.1    chopps   /* load interval into registers.
    587   1.1    chopps      the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz */
    588   1.1    chopps 
    589   1.1    chopps   interval = profint - 1;
    590   1.1    chopps 
    591   1.1    chopps   /* order of setting is important ! */
    592  1.14        is   clockcia->tblo = interval & 0xff;
    593  1.14        is   clockcia->tbhi = interval >> 8;
    594   1.1    chopps 
    595   1.1    chopps   /* enable interrupts for timer B */
    596  1.14        is   clockcia->icr = (1<<7) | (1<<1);
    597   1.1    chopps 
    598   1.1    chopps   /* start timer B in continuous shot mode */
    599  1.14        is   clockcia->crb = (clockcia->crb & 0xc0) | 1;
    600   1.1    chopps }
    601   1.1    chopps 
    602   1.1    chopps stopprofclock()
    603   1.1    chopps {
    604   1.1    chopps   /* stop timer B */
    605  1.14        is   clockcia->crb = clockcia->crb & 0xc0;
    606   1.1    chopps }
    607   1.1    chopps 
    608   1.1    chopps #ifdef PROF
    609   1.1    chopps /*
    610   1.1    chopps  * profclock() is expanded in line in lev6intr() unless profiling kernel.
    611   1.1    chopps  * Assumes it is called with clock interrupts blocked.
    612   1.1    chopps  */
    613   1.1    chopps profclock(pc, ps)
    614   1.1    chopps 	caddr_t pc;
    615   1.1    chopps 	int ps;
    616   1.1    chopps {
    617   1.1    chopps 	/*
    618   1.1    chopps 	 * Came from user mode.
    619   1.1    chopps 	 * If this process is being profiled record the tick.
    620   1.1    chopps 	 */
    621   1.1    chopps 	if (USERMODE(ps)) {
    622   1.1    chopps 		if (p->p_stats.p_prof.pr_scale)
    623   1.1    chopps 			addupc(pc, &curproc->p_stats.p_prof, 1);
    624   1.1    chopps 	}
    625   1.1    chopps 	/*
    626   1.1    chopps 	 * Came from kernel (supervisor) mode.
    627   1.1    chopps 	 * If we are profiling the kernel, record the tick.
    628   1.1    chopps 	 */
    629   1.1    chopps 	else if (profiling < 2) {
    630   1.1    chopps 		register int s = pc - s_lowpc;
    631   1.1    chopps 
    632   1.1    chopps 		if (s < s_textsize)
    633   1.1    chopps 			kcount[s / (HISTFRACTION * sizeof (*kcount))]++;
    634   1.1    chopps 	}
    635   1.1    chopps 	/*
    636   1.1    chopps 	 * Kernel profiling was on but has been disabled.
    637   1.1    chopps 	 * Mark as no longer profiling kernel and if all profiling done,
    638   1.1    chopps 	 * disable the clock.
    639   1.1    chopps 	 */
    640   1.1    chopps 	if (profiling && (profon & PRF_KERNEL)) {
    641   1.1    chopps 		profon &= ~PRF_KERNEL;
    642   1.1    chopps 		if (profon == PRF_NONE)
    643   1.1    chopps 			stopprofclock();
    644   1.1    chopps 	}
    645   1.1    chopps }
    646   1.1    chopps #endif
    647   1.1    chopps #endif
    648   1.1    chopps 
    649   1.1    chopps /* this is a hook set by a clock driver for the configured realtime clock,
    650   1.1    chopps    returning plain current unix-time */
    651  1.26     veego time_t (*gettod) __P((void));
    652  1.26     veego int (*settod) __P((time_t));
    653   1.1    chopps void *clockaddr;
    654   1.1    chopps 
    655  1.26     veego time_t a3gettod __P((void));
    656  1.26     veego time_t a2gettod __P((void));
    657  1.26     veego int a3settod __P((time_t));
    658  1.26     veego int a2settod __P((time_t));
    659  1.27        is 
    660  1.27        is #ifdef DRACO
    661  1.27        is int draco_ds_read_bit __P((void *));
    662  1.27        is void draco_ds_write_bit __P((void *, int));
    663  1.27        is void draco_ds_reset __P((void *));
    664  1.27        is 
    665  1.27        is time_t dracogettod __P((void));
    666  1.27        is 
    667  1.27        is #ifdef __NOTYET__
    668  1.27        is int dracosettod __P((time_t));
    669  1.27        is #endif
    670  1.27        is #endif
    671  1.27        is 
    672   1.1    chopps int rtcinit __P((void));
    673   1.1    chopps 
    674   1.1    chopps /*
    675   1.1    chopps  * Initialize the time of day register, based on the time base which is, e.g.
    676   1.1    chopps  * from a filesystem.
    677   1.1    chopps  */
    678  1.13     veego void
    679   1.1    chopps inittodr(base)
    680   1.1    chopps 	time_t base;
    681   1.1    chopps {
    682  1.26     veego 	time_t timbuf = base;	/* assume no battery clock exists */
    683   1.1    chopps 
    684   1.1    chopps 	if (gettod == NULL && rtcinit() == 0)
    685  1.21  christos 		printf("WARNING: no battery clock\n");
    686   1.1    chopps 	else
    687   1.1    chopps 		timbuf = gettod();
    688  1.26     veego 
    689   1.1    chopps 	if (timbuf < base) {
    690  1.21  christos 		printf("WARNING: bad date in battery clock\n");
    691   1.1    chopps 		timbuf = base;
    692   1.1    chopps 	}
    693   1.1    chopps 
    694   1.1    chopps 	/* Battery clock does not store usec's, so forget about it. */
    695   1.1    chopps 	time.tv_sec = timbuf;
    696   1.1    chopps }
    697   1.1    chopps 
    698  1.13     veego void
    699   1.1    chopps resettodr()
    700   1.1    chopps {
    701  1.13     veego 	if (settod && settod(time.tv_sec) == 0)
    702  1.21  christos 		printf("Cannot set battery backed clock\n");
    703   1.1    chopps }
    704   1.1    chopps 
    705   1.1    chopps int
    706   1.1    chopps rtcinit()
    707   1.1    chopps {
    708   1.1    chopps 	clockaddr = (void *)ztwomap(0xdc0000);
    709  1.14        is #ifdef DRACO
    710  1.14        is 	if (is_draco()) {
    711  1.14        is 		/* XXX to be done */
    712  1.27        is 		gettod = dracogettod;
    713  1.14        is 		settod = (void *)0;
    714  1.14        is 	} else
    715  1.14        is #endif
    716   1.1    chopps 	if (is_a3000() || is_a4000()) {
    717   1.1    chopps 		if (a3gettod() == 0)
    718   1.1    chopps 			return(0);
    719   1.1    chopps 		gettod = a3gettod;
    720   1.1    chopps 		settod = a3settod;
    721   1.1    chopps 	} else {
    722   1.1    chopps 		if (a2gettod() == 0)
    723   1.1    chopps 			return(0);
    724   1.1    chopps 		gettod = a2gettod;
    725   1.1    chopps 		settod = a2settod;
    726   1.1    chopps 	}
    727   1.1    chopps 	return(1);
    728   1.1    chopps }
    729   1.1    chopps 
    730  1.26     veego time_t
    731   1.1    chopps a3gettod()
    732   1.1    chopps {
    733   1.1    chopps 	struct rtclock3000 *rt;
    734  1.26     veego 	struct clock_ymdhms dt;
    735  1.26     veego 	time_t secs;
    736   1.1    chopps 
    737   1.1    chopps 	rt = clockaddr;
    738   1.1    chopps 
    739   1.1    chopps 	/* hold clock */
    740   1.1    chopps 	rt->control1 = A3CONTROL1_HOLD_CLOCK;
    741   1.1    chopps 
    742  1.26     veego 	/* Copy the info.  Careful about the order! */
    743  1.26     veego 	dt.dt_sec   = rt->second1 * 10 + rt->second2;
    744  1.26     veego 	dt.dt_min   = rt->minute1 * 10 + rt->minute2;
    745  1.26     veego 	dt.dt_hour  = rt->hour1   * 10 + rt->hour2;
    746  1.26     veego 	dt.dt_wday  = rt->weekday;
    747  1.26     veego 	dt.dt_day   = rt->day1    * 10 + rt->day2;
    748  1.26     veego 	dt.dt_mon   = rt->month1  * 10 + rt->month2;
    749  1.26     veego 	dt.dt_year  = rt->year1   * 10 + rt->year2;
    750  1.26     veego 
    751  1.26     veego 	dt.dt_year += CLOCK_BASE_YEAR;
    752   1.1    chopps 
    753   1.1    chopps 	/* let it run again.. */
    754   1.1    chopps 	rt->control1 = A3CONTROL1_FREE_CLOCK;
    755   1.1    chopps 
    756  1.26     veego 	if ((dt.dt_hour > 23) ||
    757  1.26     veego 	    (dt.dt_wday > 6) ||
    758  1.26     veego 	    (dt.dt_day  > 31) ||
    759  1.26     veego 	    (dt.dt_mon  > 12) ||
    760  1.26     veego 	    (dt.dt_year < STARTOFTIME) || (dt.dt_year > 2036))
    761  1.26     veego 		return (0);
    762   1.1    chopps 
    763  1.26     veego 	secs = clock_ymdhms_to_secs(&dt);
    764  1.26     veego 	return (secs);
    765   1.1    chopps }
    766   1.1    chopps 
    767   1.1    chopps int
    768  1.26     veego a3settod(secs)
    769  1.26     veego 	time_t secs;
    770   1.1    chopps {
    771   1.1    chopps 	struct rtclock3000 *rt;
    772  1.26     veego 	struct clock_ymdhms dt;
    773   1.1    chopps 
    774   1.1    chopps 	rt = clockaddr;
    775   1.1    chopps 	/*
    776   1.1    chopps 	 * there seem to be problems with the bitfield addressing
    777   1.1    chopps 	 * currently used..
    778   1.1    chopps 	 */
    779  1.10    chopps 
    780  1.10    chopps 	if (! rt)
    781  1.26     veego 		return (0);
    782   1.1    chopps 
    783  1.26     veego 	clock_secs_to_ymdhms(secs, &dt);
    784  1.26     veego 	dt.dt_year -= CLOCK_BASE_YEAR;
    785   1.1    chopps 
    786  1.10    chopps 	rt->control1 = A3CONTROL1_HOLD_CLOCK;
    787  1.26     veego 	rt->second1 = dt.dt_sec / 10;
    788  1.26     veego 	rt->second2 = dt.dt_sec % 10;
    789  1.26     veego 	rt->minute1 = dt.dt_min / 10;
    790  1.26     veego 	rt->minute2 = dt.dt_min % 10;
    791  1.26     veego 	rt->hour1   = dt.dt_hour / 10;
    792  1.26     veego 	rt->hour2   = dt.dt_hour % 10;
    793  1.26     veego 	rt->weekday = dt.dt_wday;
    794  1.26     veego 	rt->day1    = dt.dt_day / 10;
    795  1.26     veego 	rt->day2    = dt.dt_day % 10;
    796  1.26     veego 	rt->month1  = dt.dt_mon / 10;
    797  1.26     veego 	rt->month2  = dt.dt_mon % 10;
    798  1.26     veego 	rt->year1   = dt.dt_year / 10;
    799  1.26     veego 	rt->year2   = dt.dt_year % 10;
    800  1.10    chopps 	rt->control1 = A3CONTROL1_FREE_CLOCK;
    801   1.1    chopps 
    802  1.26     veego 	return (1);
    803   1.1    chopps }
    804   1.1    chopps 
    805  1.26     veego time_t
    806   1.1    chopps a2gettod()
    807   1.1    chopps {
    808   1.1    chopps 	struct rtclock2000 *rt;
    809  1.26     veego 	struct clock_ymdhms dt;
    810  1.26     veego 	time_t secs;
    811  1.26     veego 	int i;
    812   1.1    chopps 
    813   1.1    chopps 	rt = clockaddr;
    814   1.1    chopps 
    815   1.1    chopps 	/*
    816   1.1    chopps 	 * hold clock
    817   1.1    chopps 	 */
    818   1.1    chopps 	rt->control1 |= A2CONTROL1_HOLD;
    819   1.9    chopps 	i = 0x1000;
    820   1.9    chopps 	while (rt->control1 & A2CONTROL1_BUSY && i--)
    821   1.1    chopps 		;
    822   1.9    chopps 	if (rt->control1 & A2CONTROL1_BUSY)
    823   1.9    chopps 		return (0);	/* Give up and say it's not there */
    824   1.1    chopps 
    825  1.26     veego 	/* Copy the info.  Careful about the order! */
    826  1.26     veego 	dt.dt_sec   = rt->second1 * 10 + rt->second2;
    827  1.26     veego 	dt.dt_min   = rt->minute1 * 10 + rt->minute2;
    828  1.26     veego 	dt.dt_hour  = (rt->hour1 & 3) * 10 + rt->hour2;
    829  1.26     veego 	dt.dt_day   = rt->day1    * 10 + rt->day2;
    830  1.26     veego 	dt.dt_mon   = rt->month1  * 10 + rt->month2;
    831  1.26     veego 	dt.dt_year  = rt->year1   * 10 + rt->year2;
    832  1.26     veego 	dt.dt_wday  = rt->weekday;
    833  1.26     veego 
    834   1.1    chopps 	/*
    835  1.26     veego 	 * The oki clock chip has a register to put the clock into
    836  1.26     veego 	 * 12/24h mode.
    837  1.26     veego 	 *
    838  1.26     veego 	 *  clockmode   |    A2HOUR1_PM
    839  1.26     veego 	 *  24h   12h   |  am = 0, pm = 1
    840  1.26     veego 	 * ---------------------------------
    841  1.26     veego 	 *   0    12    |       0
    842  1.26     veego 	 *   1     1    |       0
    843  1.26     veego 	 *  ..    ..    |       0
    844  1.26     veego 	 *  11    11    |       0
    845  1.26     veego 	 *  12    12    |       1
    846  1.26     veego 	 *  13     1    |       1
    847  1.26     veego 	 *  ..    ..    |       1
    848  1.26     veego 	 *  23    11    |       1
    849  1.26     veego 	 *
    850   1.1    chopps 	 */
    851   1.1    chopps 
    852   1.1    chopps 	if ((rt->control3 & A2CONTROL3_24HMODE) == 0) {
    853  1.26     veego 		if ((rt->hour1 & A2HOUR1_PM) == 0 && dt.dt_hour == 12)
    854  1.26     veego 			dt.dt_hour = 0;
    855  1.26     veego 		else if ((rt->hour1 & A2HOUR1_PM) && dt.dt_hour != 12)
    856  1.26     veego 			dt.dt_hour += 12;
    857   1.1    chopps 	}
    858   1.1    chopps 
    859   1.1    chopps 	/*
    860   1.1    chopps 	 * release the clock
    861   1.1    chopps 	 */
    862   1.1    chopps 	rt->control1 &= ~A2CONTROL1_HOLD;
    863   1.1    chopps 
    864  1.26     veego 	dt.dt_year += CLOCK_BASE_YEAR;
    865  1.26     veego 
    866  1.26     veego 	if ((dt.dt_hour > 23) ||
    867  1.26     veego 	    (dt.dt_day  > 31) ||
    868  1.26     veego 	    (dt.dt_mon  > 12) ||
    869  1.26     veego 	    (dt.dt_year < STARTOFTIME) || (dt.dt_year > 2036))
    870  1.26     veego 		return (0);
    871   1.1    chopps 
    872  1.26     veego 	secs = clock_ymdhms_to_secs(&dt);
    873  1.26     veego 	return (secs);
    874   1.1    chopps }
    875   1.1    chopps 
    876   1.1    chopps int
    877  1.26     veego a2settod(secs)
    878  1.26     veego 	time_t secs;
    879   1.1    chopps {
    880   1.1    chopps 	struct rtclock2000 *rt;
    881  1.26     veego 	struct clock_ymdhms dt;
    882  1.26     veego 	int ampm, i;
    883   1.1    chopps 
    884   1.1    chopps 	rt = clockaddr;
    885   1.1    chopps 	/*
    886   1.1    chopps 	 * there seem to be problems with the bitfield addressing
    887   1.1    chopps 	 * currently used..
    888   1.1    chopps 	 */
    889   1.1    chopps 	if (! rt)
    890  1.26     veego 		return (0);
    891  1.26     veego 
    892  1.26     veego 	clock_secs_to_ymdhms(secs, &dt);
    893  1.26     veego 	dt.dt_year -= CLOCK_BASE_YEAR;
    894   1.1    chopps 
    895  1.26     veego 	/*
    896  1.26     veego 	 * hold clock
    897  1.26     veego 	 */
    898  1.26     veego 	rt->control1 |= A2CONTROL1_HOLD;
    899  1.26     veego 	i = 0x1000;
    900  1.26     veego 	while (rt->control1 & A2CONTROL1_BUSY && i--)
    901  1.26     veego 		;
    902  1.26     veego 	if (rt->control1 & A2CONTROL1_BUSY)
    903  1.26     veego 		return (0);	/* Give up and say it's not there */
    904   1.1    chopps 
    905  1.26     veego 	ampm = 0;
    906  1.26     veego 	if ((rt->control3 & A2CONTROL3_24HMODE) == 0) {
    907  1.26     veego 		if (dt.dt_hour >= 12) {
    908  1.26     veego 			ampm = A2HOUR1_PM;
    909  1.26     veego 			if (dt.dt_hour != 12)
    910  1.26     veego 				dt.dt_hour -= 12;
    911  1.26     veego 		} else if (dt.dt_hour == 0) {
    912  1.26     veego 			dt.dt_hour = 12;
    913  1.26     veego 		}
    914  1.26     veego 	}
    915  1.26     veego 	rt->hour1   = (dt.dt_hour / 10) | ampm;
    916  1.26     veego 	rt->hour2   = dt.dt_hour % 10;
    917  1.26     veego 	rt->second1 = dt.dt_sec / 10;
    918  1.26     veego 	rt->second2 = dt.dt_sec % 10;
    919  1.26     veego 	rt->minute1 = dt.dt_min / 10;
    920  1.26     veego 	rt->minute2 = dt.dt_min % 10;
    921  1.26     veego 	rt->day1    = dt.dt_day / 10;
    922  1.26     veego 	rt->day2    = dt.dt_day % 10;
    923  1.26     veego 	rt->month1  = dt.dt_mon / 10;
    924  1.26     veego 	rt->month2  = dt.dt_mon % 10;
    925  1.26     veego 	rt->year1   = dt.dt_year / 10;
    926  1.26     veego 	rt->year2   = dt.dt_year % 10;
    927  1.26     veego 	rt->weekday = dt.dt_wday;
    928   1.1    chopps 
    929   1.1    chopps 	/*
    930  1.26     veego 	 * release the clock
    931   1.1    chopps 	 */
    932  1.10    chopps 	rt->control2 &= ~A2CONTROL1_HOLD;
    933   1.1    chopps 
    934  1.26     veego 	return (1);
    935   1.1    chopps }
    936  1.27        is 
    937  1.27        is #ifdef DRACO
    938  1.27        is 
    939  1.27        is 
    940  1.27        is int
    941  1.27        is draco_ds_read_bit(p)
    942  1.27        is 	void *p;
    943  1.27        is {
    944  1.27        is 	struct drioct *draco_ioct;
    945  1.27        is 
    946  1.27        is 	draco_ioct = p;
    947  1.27        is 
    948  1.27        is 	while (draco_ioct->io_status & DRSTAT_CLKBUSY);
    949  1.27        is 
    950  1.27        is 	draco_ioct->io_clockw1 = 0;
    951  1.27        is 
    952  1.27        is 	while (draco_ioct->io_status & DRSTAT_CLKBUSY);
    953  1.27        is 
    954  1.27        is 	return (draco_ioct->io_status & DRSTAT_CLKDAT);
    955  1.27        is }
    956  1.27        is 
    957  1.27        is void
    958  1.27        is draco_ds_write_bit(p, b)
    959  1.27        is 	void *p;
    960  1.27        is 	int b;
    961  1.27        is {
    962  1.27        is 	struct drioct *draco_ioct;
    963  1.27        is 
    964  1.27        is 	draco_ioct = p;
    965  1.27        is 
    966  1.27        is 	while (draco_ioct->io_status & DRSTAT_CLKBUSY);
    967  1.27        is 
    968  1.27        is 	if (b)
    969  1.27        is 		draco_ioct->io_clockw1 = 0;
    970  1.27        is 	else
    971  1.27        is 		draco_ioct->io_clockw0 = 0;
    972  1.27        is }
    973  1.27        is 
    974  1.27        is void
    975  1.27        is draco_ds_reset(p)
    976  1.27        is 	void *p;
    977  1.27        is {
    978  1.27        is 	struct drioct *draco_ioct;
    979  1.27        is 
    980  1.27        is 	draco_ioct = p;
    981  1.27        is 
    982  1.27        is 	draco_ioct->io_clockrst = 0;
    983  1.27        is }
    984  1.27        is 
    985  1.27        is /*
    986  1.27        is  * We could return 1/256 of a seconds, but would need to change the interface
    987  1.27        is  */
    988  1.27        is 
    989  1.27        is time_t
    990  1.27        is dracogettod()
    991  1.27        is {
    992  1.27        is 	u_int32_t clkbuf;
    993  1.27        is 	u_int8_t rombuf[8];
    994  1.27        is 	int i;
    995  1.27        is 	struct ds_handle draco_dsh;
    996  1.27        is 
    997  1.27        is 	draco_dsh.ds_read_bit = draco_ds_read_bit;
    998  1.27        is 	draco_dsh.ds_write_bit = draco_ds_write_bit;
    999  1.27        is 	draco_dsh.ds_reset = draco_ds_reset;
   1000  1.27        is 	draco_dsh.ds_hw_handle = (void *)(DRCCADDR + DRIOCTLPG*NBPG);
   1001  1.27        is 
   1002  1.27        is 	draco_dsh.ds_reset(draco_dsh.ds_hw_handle);
   1003  1.27        is 
   1004  1.27        is 	ds_write_byte(&draco_dsh, DS_ROM_READ);
   1005  1.27        is 	for (i=0; i<8; ++i)
   1006  1.27        is 		rombuf[i] = ds_read_byte(&draco_dsh);
   1007  1.27        is 
   1008  1.27        is 	printf("DraCo RTC: sernum %d (ROM %02x%02x%02x%02x%02x%02x%02x%02x)\n",
   1009  1.27        is 		(rombuf[3] << 24) + (rombuf[2] << 16) +
   1010  1.27        is 		(rombuf[1] << 8) + rombuf[7],
   1011  1.27        is 		rombuf[7], rombuf[6], rombuf[5], rombuf[4],
   1012  1.27        is 		rombuf[3], rombuf[2], rombuf[1], rombuf[0]);
   1013  1.27        is 
   1014  1.27        is 
   1015  1.27        is 	ds_write_byte(&draco_dsh, DS_MEM_READ_MEMORY);
   1016  1.27        is 	ds_write_byte(&draco_dsh, 0x03); /* low ads byte of realtime second */
   1017  1.27        is 	ds_write_byte(&draco_dsh, 0x02); /* high ads byte of realtime second */
   1018  1.27        is 
   1019  1.27        is 	clkbuf = ds_read_byte(&draco_dsh)
   1020  1.27        is 	    + (ds_read_byte(&draco_dsh)<<8)
   1021  1.27        is 	    + (ds_read_byte(&draco_dsh)<<16)
   1022  1.27        is 	    + (ds_read_byte(&draco_dsh)<<24);
   1023  1.27        is 
   1024  1.27        is 	/* BSD time is wr. 1.1.1970; AmigaOS time wrt. 1.1.1978 */
   1025  1.27        is 
   1026  1.27        is 	clkbuf += (8*365 + 2) * 86400;
   1027  1.27        is 
   1028  1.27        is 	return ((time_t)clkbuf);
   1029  1.27        is }
   1030  1.27        is 
   1031  1.27        is #endif
   1032