11.2Smatt/* $NetBSD: clock.c,v 1.2 2010/03/02 21:52:32 matt 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.2Smatt#include <powerpc/oea/spr.h> 391.1Sgarbled 401.1Sgarbled#include "boot.h" 411.1Sgarbled 421.1Sgarbledu_long ns_per_tick = NS_PER_TICK; 431.1Sgarbled 441.1Sgarbledstatic inline u_quad_t mftb(void); 451.1Sgarbledstatic inline void mfrtc(u_long *, u_long *); 461.1Sgarbled 471.1Sgarbledstatic inline u_quad_t 481.1Sgarbledmftb(void) 491.1Sgarbled{ 501.1Sgarbled u_long scratch; 511.1Sgarbled u_quad_t tb; 521.1Sgarbled 531.1Sgarbled __asm volatile ("1: mftbu %0; mftb %0+1; mftbu %1; cmpw %0,%1; bne 1b" 541.1Sgarbled : "=r"(tb), "=r"(scratch)); 551.1Sgarbled return (tb); 561.1Sgarbled} 571.1Sgarbled 581.1Sgarbledstatic inline void 591.1Sgarbledmfrtc(u_long *up, u_long *lp) 601.1Sgarbled{ 611.1Sgarbled u_long scratch; 621.1Sgarbled 631.1Sgarbled __asm volatile ("1: mfspr %0,%3; mfspr %1,%4; mfspr %2,%3;" 641.1Sgarbled "cmpw %0,%2; bne 1b" 651.1Sgarbled : "=r"(*up), "=r"(*lp), "=r"(scratch) 661.1Sgarbled : "n"(SPR_RTCU_R), "n"(SPR_RTCL_R)); 671.1Sgarbled} 681.1Sgarbled 691.1Sgarbled/* 701.1Sgarbled * Wait for about n microseconds (at least!). 711.1Sgarbled */ 721.1Sgarbledvoid 731.1Sgarbleddelay(u_int n) 741.1Sgarbled{ 751.1Sgarbled u_quad_t tb; 761.1Sgarbled u_long tbh, tbl, scratch; 771.1Sgarbled unsigned int cpuvers; 781.1Sgarbled 791.1Sgarbled __asm volatile ("mfpvr %0" : "=r"(cpuvers)); 801.1Sgarbled cpuvers >>= 16; 811.1Sgarbled /*cpuvers = MPC601; for now */ 821.1Sgarbled 831.1Sgarbled if (cpuvers == MPC601) { 841.1Sgarbled mfrtc(&tbh, &tbl); 851.1Sgarbled while (n >= 1000000) { 861.1Sgarbled tbh++; 871.1Sgarbled n -= 1000000; 881.1Sgarbled } 891.1Sgarbled tbl += n * 1000; 901.1Sgarbled if (tbl >= 1000000000) { 911.1Sgarbled tbh++; 921.1Sgarbled tbl -= 1000000000; 931.1Sgarbled } 941.1Sgarbled __asm volatile ("1: mfspr %0,%3; cmplw %0,%1; blt 1b; bgt 2f;" 951.1Sgarbled "mfspr %0,%4; cmplw %0,%2; blt 1b; 2:" 961.1Sgarbled : "=&r"(scratch) 971.1Sgarbled : "r"(tbh), "r"(tbl), "n"(SPR_RTCU_R), "n"(SPR_RTCL_R)); 981.1Sgarbled } else { 991.1Sgarbled tb = mftb(); 1001.1Sgarbled tb += (n * 1000 + ns_per_tick - 1) / ns_per_tick; 1011.1Sgarbled tbh = tb >> 32; 1021.1Sgarbled tbl = tb; 1031.1Sgarbled __asm volatile ("1: mftbu %0; cmpw %0,%1; blt 1b; bgt 2f;" 1041.1Sgarbled "mftb %0; cmpw %0,%2; blt 1b; 2:" 1051.1Sgarbled : "=&r"(scratch) 1061.1Sgarbled : "r"(tbh), "r"(tbl)); 1071.1Sgarbled } 1081.1Sgarbled} 109