11.10Smatt/*	$NetBSD: clock.c,v 1.10 2010/03/02 21:52:32 matt 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.10Smatt#include <powerpc/oea/spr.h>
391.1Snonaka
401.1Snonaka#include "boot.h"
411.1Snonaka
421.1Snonakau_long ns_per_tick = NS_PER_TICK;
431.1Snonaka
441.9Sgarbledstatic inline u_quad_t mftb(void);
451.9Sgarbledstatic inline void mfrtc(u_long *, u_long *);
461.1Snonaka
471.1Snonakastatic inline u_quad_t
481.9Sgarbledmftb(void)
491.1Snonaka{
501.1Snonaka	u_long scratch;
511.1Snonaka	u_quad_t tb;
521.1Snonaka
531.8Sperry	__asm volatile ("1: mftbu %0; mftb %0+1; mftbu %1; cmpw %0,%1; bne 1b"
541.1Snonaka	    : "=r"(tb), "=r"(scratch));
551.1Snonaka	return (tb);
561.1Snonaka}
571.1Snonaka
581.4Skleinkstatic inline void
591.9Sgarbledmfrtc(u_long *up, u_long *lp)
601.4Skleink{
611.4Skleink	u_long scratch;
621.4Skleink
631.7Sperry	__asm volatile ("1: mfspr %0,%3; mfspr %1,%4; mfspr %2,%3;"
641.4Skleink	    "cmpw %0,%2; bne 1b"
651.4Skleink	    : "=r"(*up), "=r"(*lp), "=r"(scratch)
661.4Skleink	    : "n"(SPR_RTCU_R), "n"(SPR_RTCL_R));
671.4Skleink}
681.4Skleink
691.1Snonaka/*
701.1Snonaka * Wait for about n microseconds (at least!).
711.1Snonaka */
721.1Snonakavoid
731.9Sgarbleddelay(u_int n)
741.1Snonaka{
751.1Snonaka	u_quad_t tb;
761.1Snonaka	u_long tbh, tbl, scratch;
771.4Skleink	unsigned int cpuvers;
781.4Skleink
791.7Sperry	__asm volatile ("mfpvr %0" : "=r"(cpuvers));
801.4Skleink	cpuvers >>= 16;
811.1Snonaka
821.4Skleink	if (cpuvers == MPC601) {
831.4Skleink		mfrtc(&tbh, &tbl);
841.4Skleink		while (n >= 1000000) {
851.4Skleink			tbh++;
861.4Skleink			n -= 1000000;
871.4Skleink		}
881.4Skleink		tbl += n * 1000;
891.4Skleink		if (tbl >= 1000000000) {
901.4Skleink			tbh++;
911.4Skleink			tbl -= 1000000000;
921.4Skleink		}
931.7Sperry		__asm volatile ("1: mfspr %0,%3; cmplw %0,%1; blt 1b; bgt 2f;"
941.4Skleink		    "mfspr %0,%4; cmplw %0,%2; blt 1b; 2:"
951.5Skleink		    : "=&r"(scratch)
961.4Skleink		    : "r"(tbh), "r"(tbl), "n"(SPR_RTCU_R), "n"(SPR_RTCL_R));
971.4Skleink	} else {
981.4Skleink		tb = mftb();
991.4Skleink		tb += (n * 1000 + ns_per_tick - 1) / ns_per_tick;
1001.4Skleink		tbh = tb >> 32;
1011.4Skleink		tbl = tb;
1021.7Sperry		__asm volatile ("1: mftbu %0; cmpw %0,%1; blt 1b; bgt 2f;"
1031.4Skleink		                  "mftb %0; cmpw %0,%2; blt 1b; 2:"
1041.5Skleink		                  : "=&r"(scratch)
1051.4Skleink		                  : "r"(tbh), "r"(tbl));
1061.4Skleink	}
1071.1Snonaka}
108