1N/A/* A couple of routines to implement a low-overhead timer for drivers */
1N/A
1N/A /*
1N/A * This program is free software; you can redistribute it and/or
1N/A * modify it under the terms of the GNU General Public License as
1N/A * published by the Free Software Foundation; either version 2, or (at
1N/A * your option) any later version.
1N/A */
1N/A#include "grub.h"
1N/A#include "osdep.h"
1N/A#include "io.h"
1N/A#include "timer.h"
1N/A#include "latch.h"
1N/A
1N/Avoid __load_timer2(unsigned int ticks)
1N/A{
1N/A /*
1N/A * Now let's take care of PPC channel 2
1N/A *
1N/A * Set the Gate high, program PPC channel 2 for mode 0,
1N/A * (interrupt on terminal count mode), binary count,
1N/A * load 5 * LATCH count, (LSB and MSB) to begin countdown.
1N/A *
1N/A * Note some implementations have a bug where the high bits byte
1N/A * of channel 2 is ignored.
1N/A */
1N/A /* Set up the timer gate, turn off the speaker */
1N/A /* Set the Gate high, disable speaker */
1N/A outb((inb(PPC_PORTB) & ~PPCB_SPKR) | PPCB_T2GATE, PPC_PORTB);
1N/A /* binary, mode 0, LSB/MSB, Ch 2 */
1N/A outb(TIMER2_SEL|WORD_ACCESS|MODE0|BINARY_COUNT, TIMER_MODE_PORT);
1N/A /* LSB of ticks */
1N/A outb(ticks & 0xFF, TIMER2_PORT);
1N/A /* MSB of ticks */
1N/A outb(ticks >> 8, TIMER2_PORT);
1N/A}
1N/A
1N/Astatic int __timer2_running(void)
1N/A{
1N/A return ((inb(PPC_PORTB) & PPCB_T2OUT) == 0);
1N/A}
1N/A
1N/A#if !defined(CONFIG_TSC_CURRTICKS)
1N/Avoid setup_timers(void)
1N/A{
1N/A return;
1N/A}
1N/A
1N/Avoid load_timer2(unsigned int ticks)
1N/A{
1N/A return __load_timer2(ticks);
1N/A}
1N/A
1N/Aint timer2_running(void)
1N/A{
1N/A return __timer2_running();
1N/A}
1N/A
1N/Avoid ndelay(unsigned int nsecs)
1N/A{
1N/A waiton_timer2((nsecs * CLOCK_TICK_RATE)/1000000000);
1N/A}
1N/Avoid udelay(unsigned int usecs)
1N/A{
1N/A waiton_timer2((usecs * TICKS_PER_MS)/1000);
1N/A}
1N/A#endif /* !defined(CONFIG_TSC_CURRTICKS) */
1N/A
1N/A#if defined(CONFIG_TSC_CURRTICKS)
1N/A
1N/A#define rdtsc(low,high) \
1N/A __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high))
1N/A
1N/A#define rdtscll(val) \
1N/A __asm__ __volatile__ ("rdtsc" : "=A" (val))
1N/A
1N/A
1N/A/* Number of clock ticks to time with the rtc */
1N/A#define LATCH 0xFF
1N/A
1N/A#define LATCHES_PER_SEC ((CLOCK_TICK_RATE + (LATCH/2))/LATCH)
1N/A#define TICKS_PER_LATCH ((LATCHES_PER_SEC + (TICKS_PER_SEC/2))/TICKS_PER_SEC)
1N/A
1N/Astatic void sleep_latch(void)
1N/A{
1N/A __load_timer2(LATCH);
1N/A while(__timer2_running());
1N/A}
1N/A
1N/A/* ------ Calibrate the TSC -------
1N/A * Time how long it takes to excute a loop that runs in known time.
1N/A * And find the convertion needed to get to CLOCK_TICK_RATE
1N/A */
1N/A
1N/A
1N/Astatic unsigned long long calibrate_tsc(void)
1N/A{
1N/A unsigned long startlow, starthigh;
1N/A unsigned long endlow, endhigh;
1N/A
1N/A rdtsc(startlow,starthigh);
1N/A sleep_latch();
1N/A rdtsc(endlow,endhigh);
1N/A
1N/A /* 64-bit subtract - gcc just messes up with long longs */
1N/A __asm__("subl %2,%0\n\t"
1N/A "sbbl %3,%1"
1N/A :"=a" (endlow), "=d" (endhigh)
1N/A :"g" (startlow), "g" (starthigh),
1N/A "0" (endlow), "1" (endhigh));
1N/A
1N/A /* Error: ECPUTOOFAST */
1N/A if (endhigh)
1N/A goto bad_ctc;
1N/A
1N/A endlow *= TICKS_PER_LATCH;
1N/A return endlow;
1N/A
1N/A /*
1N/A * The CTC wasn't reliable: we got a hit on the very first read,
1N/A * or the CPU was so fast/slow that the quotient wouldn't fit in
1N/A * 32 bits..
1N/A */
1N/Abad_ctc:
1N/A printf("bad_ctc\n");
1N/A return 0;
1N/A}
1N/A
1N/Astatic unsigned long clocks_per_tick;
1N/Avoid setup_timers(void)
1N/A{
1N/A if (!clocks_per_tick) {
1N/A clocks_per_tick = calibrate_tsc();
1N/A /* Display the CPU Mhz to easily test if the calibration was bad */
1N/A printf("CPU %ld Mhz\n", (clocks_per_tick/1000 * TICKS_PER_SEC)/1000);
1N/A }
1N/A}
1N/A
1N/Aunsigned long currticks(void)
1N/A{
1N/A unsigned long clocks_high, clocks_low;
1N/A unsigned long currticks;
1N/A /* Read the Time Stamp Counter */
1N/A rdtsc(clocks_low, clocks_high);
1N/A
1N/A /* currticks = clocks / clocks_per_tick; */
1N/A __asm__("divl %1"
1N/A :"=a" (currticks)
1N/A :"r" (clocks_per_tick), "0" (clocks_low), "d" (clocks_high));
1N/A
1N/A
1N/A return currticks;
1N/A}
1N/A
1N/Astatic unsigned long long timer_timeout;
1N/Astatic int __timer_running(void)
1N/A{
1N/A unsigned long long now;
1N/A rdtscll(now);
1N/A return now < timer_timeout;
1N/A}
1N/A
1N/Avoid udelay(unsigned int usecs)
1N/A{
1N/A unsigned long long now;
1N/A rdtscll(now);
1N/A timer_timeout = now + usecs * ((clocks_per_tick * TICKS_PER_SEC)/(1000*1000));
1N/A while(__timer_running());
1N/A}
1N/Avoid ndelay(unsigned int nsecs)
1N/A{
1N/A unsigned long long now;
1N/A rdtscll(now);
1N/A timer_timeout = now + nsecs * ((clocks_per_tick * TICKS_PER_SEC)/(1000*1000*1000));
1N/A while(__timer_running());
1N/A}
1N/A
1N/Avoid load_timer2(unsigned int timer2_ticks)
1N/A{
1N/A unsigned long long now;
1N/A unsigned long clocks;
1N/A rdtscll(now);
1N/A clocks = timer2_ticks * ((clocks_per_tick * TICKS_PER_SEC)/CLOCK_TICK_RATE);
1N/A timer_timeout = now + clocks;
1N/A}
1N/A
1N/Aint timer2_running(void)
1N/A{
1N/A return __timer_running();
1N/A}
1N/A
1N/A#endif /* RTC_CURRTICKS */