Home | History | Annotate | Line # | Download | only in dev
clock.c revision 1.3
      1 /*	$NetBSD: clock.c,v 1.3 1995/05/28 19:38:49 leo 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/device.h>
     48 #include <machine/psl.h>
     49 #include <machine/cpu.h>
     50 #include <machine/iomap.h>
     51 #include <machine/mfp.h>
     52 #include <atari/dev/clockreg.h>
     53 
     54 #if defined(PROF) && defined(PROFTIMER)
     55 #include <sys/PROF.h>
     56 #endif
     57 
     58 
     59 /*
     60  * Machine-dependent clock routines.
     61  *
     62  * Startrtclock restarts the real-time clock, which provides
     63  * hardclock interrupts to kern_clock.c.
     64  *
     65  * Inittodr initializes the time of day hardware which provides
     66  * date functions.
     67  *
     68  * Resettodr restores the time of day hardware after a time change.
     69  *
     70  * A note on the real-time clock:
     71  * We actually load the clock with CLK_INTERVAL-1 instead of CLK_INTERVAL.
     72  * This is because the counter decrements to zero after N+1 enabled clock
     73  * periods where N is the value loaded into the counter.
     74  */
     75 
     76 int	clockmatch __P((struct device *, struct cfdata *, void *));
     77 void	clockattach __P((struct device *, struct device *, void *));
     78 
     79 struct cfdriver clockcd = {
     80 	NULL, "clock", (cfmatch_t)clockmatch, clockattach,
     81 	DV_DULL, sizeof(struct device), NULL, 0
     82 };
     83 
     84 static u_long	gettod __P((void));
     85 static int	settod __P((u_long));
     86 
     87 static int	divisor;
     88 
     89 int
     90 clockmatch(pdp, cfp, auxp)
     91 struct device *pdp;
     92 struct cfdata *cfp;
     93 void *auxp;
     94 {
     95 	if(!strcmp("clock", auxp))
     96 		return(1);
     97 	return(0);
     98 }
     99 
    100 /*
    101  * Start the real-time clock.
    102  */
    103 void clockattach(pdp, dp, auxp)
    104 struct device	*pdp, *dp;
    105 void			*auxp;
    106 {
    107 	/*
    108 	 * Initialize Timer-A in the ST-MFP. We use a divisor of 200.
    109 	 * The MFP clock runs at 2457600Hz. Therefore the timer runs
    110 	 * at an effective rate of: 2457600/200 = 12288Hz. The
    111 	 * following expression works for 48, 64 or 96 hz.
    112 	 */
    113 	divisor       = 12288/hz;
    114 	MFP->mf_tacr  = 0;		/* Stop timer			*/
    115 	MFP->mf_iera &= ~IA_TIMA;	/* Disable timer interrupts	*/
    116 	MFP->mf_tadr  = divisor;	/* Set divisor			*/
    117 
    118 	printf(": system hz %d timer-A divisor 200/%d\n", hz, divisor);
    119 
    120 	/*
    121 	 * Initialize Timer-B in the ST-MFP. This timer is used by the 'delay'
    122 	 * function below. This time is setup to be continueously counting from
    123 	 * 255 back to zero at a frequency of 614400Hz.
    124 	 */
    125 	MFP->mf_tbcr  = 0;		/* Stop timer			*/
    126 	MFP->mf_iera &= ~IA_TIMB;	/* Disable timer interrupts	*/
    127 	MFP->mf_tbdr  = 0;
    128 	MFP->mf_tbcr  = T_Q004;	/* Start timer			*/
    129 
    130 }
    131 
    132 void cpu_initclocks()
    133 {
    134 	MFP->mf_tacr  = T_Q200;		/* Start timer			*/
    135 	MFP->mf_ipra &= ~IA_TIMA;	/* Clear pending interrupts	*/
    136 	MFP->mf_iera |= IA_TIMA;	/* Enable timer interrupts	*/
    137 	MFP->mf_imra |= IA_TIMA;	/*    .....			*/
    138 }
    139 
    140 setstatclockrate(hz)
    141 	int hz;
    142 {
    143 }
    144 
    145 /*
    146  * Returns number of usec since last recorded clock "tick"
    147  * (i.e. clock interrupt).
    148  */
    149 clkread()
    150 {
    151 	u_int	delta;
    152 
    153 	delta = ((divisor - MFP->mf_tadr) * tick) / divisor;
    154 	/*
    155 	 * Account for pending clock interrupts
    156 	 */
    157 	if(MFP->mf_iera & IA_TIMA)
    158 		return(delta + tick);
    159 	return(delta);
    160 }
    161 
    162 #define TIMB_FREQ	614400
    163 #define TIMB_LIMIT	256
    164 
    165 /*
    166  * Wait "n" microseconds.
    167  * Relies on MFP-Timer B counting down from TIMB_LIMIT at TIMB_FREQ Hz.
    168  * Note: timer had better have been programmed before this is first used!
    169  */
    170 void delay(n)
    171 int	n;
    172 {
    173 	int	tick, otick;
    174 
    175 	/*
    176 	 * Read the counter first, so that the rest of the setup overhead is
    177 	 * counted.
    178 	 */
    179 	otick = MFP->mf_tbdr;
    180 
    181 	/*
    182 	 * Calculate ((n * TIMER_FREQ) / 1e6) using explicit assembler code so
    183 	 * we can take advantage of the intermediate 64-bit quantity to prevent
    184 	 * loss of significance.
    185 	 */
    186 	n -= 5;
    187 	if(n < 0)
    188 		return;
    189 	{
    190 	    u_int	temp;
    191 
    192 	    __asm __volatile ("mulul %2,%1:%0" : "=d" (n), "=d" (temp)
    193 					       : "d" (TIMB_FREQ));
    194 	    __asm __volatile ("divul %1,%2:%0" : "=d" (n)
    195 					       : "d"(1000000),"d"(temp),"0"(n));
    196 	}
    197 
    198 	while(n > 0) {
    199 		tick = MFP->mf_tbdr;
    200 		if(tick > otick)
    201 			n -= TIMB_LIMIT - (tick - otick);
    202 		else n -= otick - tick;
    203 		otick = tick;
    204 	}
    205 }
    206 
    207 #ifdef PROFTIMER
    208 /*
    209  * This code allows the amiga kernel to use one of the extra timers on
    210  * the clock chip for profiling, instead of the regular system timer.
    211  * The advantage of this is that the profiling timer can be turned up to
    212  * a higher interrupt rate, giving finer resolution timing. The profclock
    213  * routine is called from the lev6intr in locore, and is a specialized
    214  * routine that calls addupc. The overhead then is far less than if
    215  * hardclock/softclock was called. Further, the context switch code in
    216  * locore has been changed to turn the profile clock on/off when switching
    217  * into/out of a process that is profiling (startprofclock/stopprofclock).
    218  * This reduces the impact of the profiling clock on other users, and might
    219  * possibly increase the accuracy of the profiling.
    220  */
    221 int  profint   = PRF_INTERVAL;	/* Clock ticks between interrupts */
    222 int  profscale = 0;		/* Scale factor from sys clock to prof clock */
    223 char profon    = 0;		/* Is profiling clock on? */
    224 
    225 /* profon values - do not change, locore.s assumes these values */
    226 #define PRF_NONE	0x00
    227 #define	PRF_USER	0x01
    228 #define	PRF_KERNEL	0x80
    229 
    230 initprofclock()
    231 {
    232 #if NCLOCK > 0
    233 	struct proc *p = curproc;		/* XXX */
    234 
    235 	/*
    236 	 * If the high-res timer is running, force profiling off.
    237 	 * Unfortunately, this gets reflected back to the user not as
    238 	 * an error but as a lack of results.
    239 	 */
    240 	if (clockon) {
    241 		p->p_stats->p_prof.pr_scale = 0;
    242 		return;
    243 	}
    244 	/*
    245 	 * Keep track of the number of user processes that are profiling
    246 	 * by checking the scale value.
    247 	 *
    248 	 * XXX: this all assumes that the profiling code is well behaved;
    249 	 * i.e. profil() is called once per process with pcscale non-zero
    250 	 * to turn it on, and once with pcscale zero to turn it off.
    251 	 * Also assumes you don't do any forks or execs.  Oh well, there
    252 	 * is always adb...
    253 	 */
    254 	if (p->p_stats->p_prof.pr_scale)
    255 		profprocs++;
    256 	else
    257 		profprocs--;
    258 #endif
    259 	/*
    260 	 * The profile interrupt interval must be an even divisor
    261 	 * of the CLK_INTERVAL so that scaling from a system clock
    262 	 * tick to a profile clock tick is possible using integer math.
    263 	 */
    264 	if (profint > CLK_INTERVAL || (CLK_INTERVAL % profint) != 0)
    265 		profint = CLK_INTERVAL;
    266 	profscale = CLK_INTERVAL / profint;
    267 }
    268 
    269 startprofclock()
    270 {
    271   unsigned short interval;
    272 
    273   /* stop timer B */
    274   ciab.crb = ciab.crb & 0xc0;
    275 
    276   /* load interval into registers.
    277      the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz */
    278 
    279   interval = profint - 1;
    280 
    281   /* order of setting is important ! */
    282   ciab.tblo = interval & 0xff;
    283   ciab.tbhi = interval >> 8;
    284 
    285   /* enable interrupts for timer B */
    286   ciab.icr = (1<<7) | (1<<1);
    287 
    288   /* start timer B in continuous shot mode */
    289   ciab.crb = (ciab.crb & 0xc0) | 1;
    290 }
    291 
    292 stopprofclock()
    293 {
    294   /* stop timer B */
    295   ciab.crb = ciab.crb & 0xc0;
    296 }
    297 
    298 #ifdef PROF
    299 /*
    300  * profclock() is expanded in line in lev6intr() unless profiling kernel.
    301  * Assumes it is called with clock interrupts blocked.
    302  */
    303 profclock(pc, ps)
    304 	caddr_t pc;
    305 	int ps;
    306 {
    307 	/*
    308 	 * Came from user mode.
    309 	 * If this process is being profiled record the tick.
    310 	 */
    311 	if (USERMODE(ps)) {
    312 		if (p->p_stats.p_prof.pr_scale)
    313 			addupc(pc, &curproc->p_stats.p_prof, 1);
    314 	}
    315 	/*
    316 	 * Came from kernel (supervisor) mode.
    317 	 * If we are profiling the kernel, record the tick.
    318 	 */
    319 	else if (profiling < 2) {
    320 		register int s = pc - s_lowpc;
    321 
    322 		if (s < s_textsize)
    323 			kcount[s / (HISTFRACTION * sizeof (*kcount))]++;
    324 	}
    325 	/*
    326 	 * Kernel profiling was on but has been disabled.
    327 	 * Mark as no longer profiling kernel and if all profiling done,
    328 	 * disable the clock.
    329 	 */
    330 	if (profiling && (profon & PRF_KERNEL)) {
    331 		profon &= ~PRF_KERNEL;
    332 		if (profon == PRF_NONE)
    333 			stopprofclock();
    334 	}
    335 }
    336 #endif
    337 #endif
    338 
    339 /*
    340  * Initialize the time of day register, based on the time base which is, e.g.
    341  * from a filesystem.
    342  */
    343 inittodr(base)
    344 time_t base;
    345 {
    346 	u_long timbuf = base;	/* assume no battery clock exists */
    347 
    348 	timbuf = gettod();
    349 
    350 	if(timbuf < base) {
    351 		printf("WARNING: bad date in battery clock\n");
    352 		timbuf = base;
    353 	}
    354 
    355 	/* Battery clock does not store usec's, so forget about it. */
    356 	time.tv_sec = timbuf;
    357 }
    358 
    359 resettodr()
    360 {
    361 	if(settod(time.tv_sec) == 1)
    362 		return;
    363 	printf("Cannot set battery backed clock\n");
    364 }
    365 
    366 static	char	dmsize[12] =
    367 {
    368 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    369 };
    370 
    371 static	char	ldmsize[12] =
    372 {
    373 	31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    374 };
    375 
    376 static u_long
    377 gettod()
    378 {
    379 	int		i, sps;
    380 	u_long		new_time = 0;
    381 	char		*msize;
    382 	mc_todregs	clkregs;
    383 
    384 	sps = splhigh();
    385 	MC146818_GETTOD(RTC, &clkregs);
    386 	splx(sps);
    387 
    388 	if(range_test(clkregs[MC_HOUR], 0, 23))
    389 		return(0);
    390 	if(range_test(clkregs[MC_DOM], 1, 31))
    391 		return(0);
    392 	if (range_test(clkregs[MC_MONTH], 1, 12))
    393 		return(0);
    394 	if(range_test(clkregs[MC_YEAR], 0, 2000 - GEMSTARTOFTIME))
    395 		return(0);
    396 	clkregs[MC_YEAR] += GEMSTARTOFTIME;
    397 
    398 	for(i = BSDSTARTOFTIME; i < clkregs[MC_YEAR]; i++) {
    399 		if(is_leap(i))
    400 			new_time += 366;
    401 		else new_time += 365;
    402 	}
    403 
    404 	msize = is_leap(clkregs[MC_YEAR]) ? ldmsize : dmsize;
    405 	for(i = 0; i < (clkregs[MC_MONTH] - 1); i++)
    406 		new_time += msize[i];
    407 	new_time += clkregs[MC_DOM] - 1;
    408 	new_time *= SECS_DAY;
    409 	new_time += (clkregs[MC_HOUR] * 3600) + (clkregs[MC_MIN] * 60);
    410 	return(new_time + clkregs[MC_SEC]);
    411 }
    412 
    413 static int
    414 settod(newtime)
    415 u_long	newtime;
    416 {
    417 	register long	days, rem, year;
    418 	register char	*ml;
    419 		 int	sps, sec, min, hour, month;
    420 	mc_todregs	clkregs;
    421 
    422 	/* Number of days since Jan. 1 'BSDSTARTOFTIME'	*/
    423 	days = newtime / SECS_DAY;
    424 	rem  = newtime % SECS_DAY;
    425 
    426 	/*
    427 	 * Calculate sec, min, hour
    428 	 */
    429 	hour = rem / SECS_HOUR;
    430 	rem %= SECS_HOUR;
    431 	min  = rem / 60;
    432 	sec  = rem % 60;
    433 
    434 	/*
    435 	 * Figure out the year. Day in year is left in 'days'.
    436 	 */
    437 	year = BSDSTARTOFTIME;
    438 	while(days >= (rem = is_leap(year) ? 366 : 365)) {
    439 		++year;
    440 		days -= rem;
    441 	}
    442 
    443 	/*
    444 	 * Determine the month
    445 	 */
    446 	ml = is_leap(year) ? ldmsize : dmsize;
    447 	for(month = 0; days >= ml[month]; ++month)
    448 		days -= ml[month];
    449 
    450 	/*
    451 	 * Now that everything is calculated, program the RTC
    452 	 */
    453 	mc146818_write(RTC, MC_REGA, MC_BASE_32_KHz);
    454 	mc146818_write(RTC, MC_REGB, MC_REGB_24HR | MC_REGB_BINARY);
    455 	sps = splhigh();
    456 	MC146818_GETTOD(RTC, &clkregs);
    457 	clkregs[MC_SEC]   = sec;
    458 	clkregs[MC_MIN]   = min;
    459 	clkregs[MC_HOUR]  = hour;
    460 	clkregs[MC_DOM]   = days+1;
    461 	clkregs[MC_MONTH] = month+1;
    462 	clkregs[MC_YEAR]  = year - GEMSTARTOFTIME;
    463 	MC146818_PUTTOD(RTC, &clkregs);
    464 	splx(sps);
    465 
    466 	return(1);
    467 }
    468