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