clock.c revision 1.9
11.9Sgarbled/*	$NetBSD: clock.c,v 1.9 2006/04/10 18:40:06 garbled Exp $	*/
21.1Snonaka
31.1Snonaka/*
41.1Snonaka * Copyright (C) 1995, 1996 Wolfgang Solfrank.
51.1Snonaka * Copyright (C) 1995, 1996 TooLs GmbH.
61.1Snonaka * All rights reserved.
71.1Snonaka *
81.1Snonaka * Redistribution and use in source and binary forms, with or without
91.1Snonaka * modification, are permitted provided that the following conditions
101.1Snonaka * are met:
111.1Snonaka * 1. Redistributions of source code must retain the above copyright
121.1Snonaka *    notice, this list of conditions and the following disclaimer.
131.1Snonaka * 2. Redistributions in binary form must reproduce the above copyright
141.1Snonaka *    notice, this list of conditions and the following disclaimer in the
151.1Snonaka *    documentation and/or other materials provided with the distribution.
161.1Snonaka * 3. All advertising materials mentioning features or use of this software
171.1Snonaka *    must display the following acknowledgement:
181.1Snonaka *	This product includes software developed by TooLs GmbH.
191.1Snonaka * 4. The name of TooLs GmbH may not be used to endorse or promote products
201.1Snonaka *    derived from this software without specific prior written permission.
211.1Snonaka *
221.1Snonaka * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
231.1Snonaka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
241.1Snonaka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
251.1Snonaka * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
261.1Snonaka * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
271.1Snonaka * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
281.1Snonaka * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
291.1Snonaka * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
301.1Snonaka * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
311.1Snonaka * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
321.1Snonaka */
331.1Snonaka
341.1Snonaka#include <lib/libsa/stand.h>
351.1Snonaka#include <dev/isa/isareg.h>
361.1Snonaka#include <dev/ic/i8253reg.h>
371.4Skleink#include <powerpc/spr.h>
381.1Snonaka
391.1Snonaka#include "boot.h"
401.1Snonaka
411.1Snonakau_long ns_per_tick = NS_PER_TICK;
421.1Snonaka
431.9Sgarbledstatic inline u_quad_t mftb(void);
441.9Sgarbledstatic inline void mfrtc(u_long *, u_long *);
451.1Snonaka
461.1Snonakastatic inline u_quad_t
471.9Sgarbledmftb(void)
481.1Snonaka{
491.1Snonaka	u_long scratch;
501.1Snonaka	u_quad_t tb;
511.1Snonaka
521.8Sperry	__asm volatile ("1: mftbu %0; mftb %0+1; mftbu %1; cmpw %0,%1; bne 1b"
531.1Snonaka	    : "=r"(tb), "=r"(scratch));
541.1Snonaka	return (tb);
551.1Snonaka}
561.1Snonaka
571.4Skleinkstatic inline void
581.9Sgarbledmfrtc(u_long *up, u_long *lp)
591.4Skleink{
601.4Skleink	u_long scratch;
611.4Skleink
621.7Sperry	__asm volatile ("1: mfspr %0,%3; mfspr %1,%4; mfspr %2,%3;"
631.4Skleink	    "cmpw %0,%2; bne 1b"
641.4Skleink	    : "=r"(*up), "=r"(*lp), "=r"(scratch)
651.4Skleink	    : "n"(SPR_RTCU_R), "n"(SPR_RTCL_R));
661.4Skleink}
671.4Skleink
681.1Snonaka/*
691.1Snonaka * Wait for about n microseconds (at least!).
701.1Snonaka */
711.1Snonakavoid
721.9Sgarbleddelay(u_int n)
731.1Snonaka{
741.1Snonaka	u_quad_t tb;
751.1Snonaka	u_long tbh, tbl, scratch;
761.4Skleink	unsigned int cpuvers;
771.4Skleink
781.7Sperry	__asm volatile ("mfpvr %0" : "=r"(cpuvers));
791.4Skleink	cpuvers >>= 16;
801.1Snonaka
811.4Skleink	if (cpuvers == MPC601) {
821.4Skleink		mfrtc(&tbh, &tbl);
831.4Skleink		while (n >= 1000000) {
841.4Skleink			tbh++;
851.4Skleink			n -= 1000000;
861.4Skleink		}
871.4Skleink		tbl += n * 1000;
881.4Skleink		if (tbl >= 1000000000) {
891.4Skleink			tbh++;
901.4Skleink			tbl -= 1000000000;
911.4Skleink		}
921.7Sperry		__asm volatile ("1: mfspr %0,%3; cmplw %0,%1; blt 1b; bgt 2f;"
931.4Skleink		    "mfspr %0,%4; cmplw %0,%2; blt 1b; 2:"
941.5Skleink		    : "=&r"(scratch)
951.4Skleink		    : "r"(tbh), "r"(tbl), "n"(SPR_RTCU_R), "n"(SPR_RTCL_R));
961.4Skleink	} else {
971.4Skleink		tb = mftb();
981.4Skleink		tb += (n * 1000 + ns_per_tick - 1) / ns_per_tick;
991.4Skleink		tbh = tb >> 32;
1001.4Skleink		tbl = tb;
1011.7Sperry		__asm volatile ("1: mftbu %0; cmpw %0,%1; blt 1b; bgt 2f;"
1021.4Skleink		                  "mftb %0; cmpw %0,%2; blt 1b; 2:"
1031.5Skleink		                  : "=&r"(scratch)
1041.4Skleink		                  : "r"(tbh), "r"(tbl));
1051.4Skleink	}
1061.1Snonaka}
107