clock.c revision 1.1
11.1Sgarbled/*	$NetBSD: clock.c,v 1.1 2007/12/17 19:09:46 garbled Exp $	*/
21.1Sgarbled
31.1Sgarbled/*
41.1Sgarbled * Copyright (C) 1995, 1996 Wolfgang Solfrank.
51.1Sgarbled * Copyright (C) 1995, 1996 TooLs GmbH.
61.1Sgarbled * All rights reserved.
71.1Sgarbled *
81.1Sgarbled * Redistribution and use in source and binary forms, with or without
91.1Sgarbled * modification, are permitted provided that the following conditions
101.1Sgarbled * are met:
111.1Sgarbled * 1. Redistributions of source code must retain the above copyright
121.1Sgarbled *    notice, this list of conditions and the following disclaimer.
131.1Sgarbled * 2. Redistributions in binary form must reproduce the above copyright
141.1Sgarbled *    notice, this list of conditions and the following disclaimer in the
151.1Sgarbled *    documentation and/or other materials provided with the distribution.
161.1Sgarbled * 3. All advertising materials mentioning features or use of this software
171.1Sgarbled *    must display the following acknowledgement:
181.1Sgarbled *	This product includes software developed by TooLs GmbH.
191.1Sgarbled * 4. The name of TooLs GmbH may not be used to endorse or promote products
201.1Sgarbled *    derived from this software without specific prior written permission.
211.1Sgarbled *
221.1Sgarbled * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
231.1Sgarbled * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
241.1Sgarbled * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
251.1Sgarbled * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
261.1Sgarbled * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
271.1Sgarbled * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
281.1Sgarbled * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
291.1Sgarbled * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
301.1Sgarbled * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
311.1Sgarbled * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
321.1Sgarbled */
331.1Sgarbled
341.1Sgarbled#include <lib/libsa/stand.h>
351.1Sgarbled#include <dev/isa/isareg.h>
361.1Sgarbled#include <dev/ic/i8253reg.h>
371.1Sgarbled#include <powerpc/spr.h>
381.1Sgarbled
391.1Sgarbled#include "boot.h"
401.1Sgarbled
411.1Sgarbledu_long ns_per_tick = NS_PER_TICK;
421.1Sgarbled
431.1Sgarbledstatic inline u_quad_t mftb(void);
441.1Sgarbledstatic inline void mfrtc(u_long *, u_long *);
451.1Sgarbled
461.1Sgarbledstatic inline u_quad_t
471.1Sgarbledmftb(void)
481.1Sgarbled{
491.1Sgarbled	u_long scratch;
501.1Sgarbled	u_quad_t tb;
511.1Sgarbled
521.1Sgarbled	__asm volatile ("1: mftbu %0; mftb %0+1; mftbu %1; cmpw %0,%1; bne 1b"
531.1Sgarbled	    : "=r"(tb), "=r"(scratch));
541.1Sgarbled	return (tb);
551.1Sgarbled}
561.1Sgarbled
571.1Sgarbledstatic inline void
581.1Sgarbledmfrtc(u_long *up, u_long *lp)
591.1Sgarbled{
601.1Sgarbled	u_long scratch;
611.1Sgarbled
621.1Sgarbled	__asm volatile ("1: mfspr %0,%3; mfspr %1,%4; mfspr %2,%3;"
631.1Sgarbled	    "cmpw %0,%2; bne 1b"
641.1Sgarbled	    : "=r"(*up), "=r"(*lp), "=r"(scratch)
651.1Sgarbled	    : "n"(SPR_RTCU_R), "n"(SPR_RTCL_R));
661.1Sgarbled}
671.1Sgarbled
681.1Sgarbled/*
691.1Sgarbled * Wait for about n microseconds (at least!).
701.1Sgarbled */
711.1Sgarbledvoid
721.1Sgarbleddelay(u_int n)
731.1Sgarbled{
741.1Sgarbled	u_quad_t tb;
751.1Sgarbled	u_long tbh, tbl, scratch;
761.1Sgarbled	unsigned int cpuvers;
771.1Sgarbled
781.1Sgarbled	__asm volatile ("mfpvr %0" : "=r"(cpuvers));
791.1Sgarbled	cpuvers >>= 16;
801.1Sgarbled	/*cpuvers = MPC601; for now */
811.1Sgarbled
821.1Sgarbled	if (cpuvers == MPC601) {
831.1Sgarbled		mfrtc(&tbh, &tbl);
841.1Sgarbled		while (n >= 1000000) {
851.1Sgarbled			tbh++;
861.1Sgarbled			n -= 1000000;
871.1Sgarbled		}
881.1Sgarbled		tbl += n * 1000;
891.1Sgarbled		if (tbl >= 1000000000) {
901.1Sgarbled			tbh++;
911.1Sgarbled			tbl -= 1000000000;
921.1Sgarbled		}
931.1Sgarbled		__asm volatile ("1: mfspr %0,%3; cmplw %0,%1; blt 1b; bgt 2f;"
941.1Sgarbled		    "mfspr %0,%4; cmplw %0,%2; blt 1b; 2:"
951.1Sgarbled		    : "=&r"(scratch)
961.1Sgarbled		    : "r"(tbh), "r"(tbl), "n"(SPR_RTCU_R), "n"(SPR_RTCL_R));
971.1Sgarbled	} else {
981.1Sgarbled		tb = mftb();
991.1Sgarbled		tb += (n * 1000 + ns_per_tick - 1) / ns_per_tick;
1001.1Sgarbled		tbh = tb >> 32;
1011.1Sgarbled		tbl = tb;
1021.1Sgarbled		__asm volatile ("1: mftbu %0; cmpw %0,%1; blt 1b; bgt 2f;"
1031.1Sgarbled		                  "mftb %0; cmpw %0,%2; blt 1b; 2:"
1041.1Sgarbled		                  : "=&r"(scratch)
1051.1Sgarbled		                  : "r"(tbh), "r"(tbl));
1061.1Sgarbled	}
1071.1Sgarbled}
108