17c478bd9Sstevel@tonic-gate /* A couple of routines to implement a low-overhead timer for drivers */
27c478bd9Sstevel@tonic-gate
37c478bd9Sstevel@tonic-gate /*
47c478bd9Sstevel@tonic-gate * This program is free software; you can redistribute it and/or
57c478bd9Sstevel@tonic-gate * modify it under the terms of the GNU General Public License as
67c478bd9Sstevel@tonic-gate * published by the Free Software Foundation; either version 2, or (at
77c478bd9Sstevel@tonic-gate * your option) any later version.
87c478bd9Sstevel@tonic-gate */
97c478bd9Sstevel@tonic-gate #include "grub.h"
107c478bd9Sstevel@tonic-gate #include "osdep.h"
117c478bd9Sstevel@tonic-gate #include "io.h"
127c478bd9Sstevel@tonic-gate #include "timer.h"
137c478bd9Sstevel@tonic-gate #include "latch.h"
147c478bd9Sstevel@tonic-gate
__load_timer2(unsigned int ticks)157c478bd9Sstevel@tonic-gate void __load_timer2(unsigned int ticks)
167c478bd9Sstevel@tonic-gate {
177c478bd9Sstevel@tonic-gate /*
187c478bd9Sstevel@tonic-gate * Now let's take care of PPC channel 2
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * Set the Gate high, program PPC channel 2 for mode 0,
217c478bd9Sstevel@tonic-gate * (interrupt on terminal count mode), binary count,
227c478bd9Sstevel@tonic-gate * load 5 * LATCH count, (LSB and MSB) to begin countdown.
237c478bd9Sstevel@tonic-gate *
247c478bd9Sstevel@tonic-gate * Note some implementations have a bug where the high bits byte
257c478bd9Sstevel@tonic-gate * of channel 2 is ignored.
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate /* Set up the timer gate, turn off the speaker */
287c478bd9Sstevel@tonic-gate /* Set the Gate high, disable speaker */
297c478bd9Sstevel@tonic-gate outb((inb(PPC_PORTB) & ~PPCB_SPKR) | PPCB_T2GATE, PPC_PORTB);
307c478bd9Sstevel@tonic-gate /* binary, mode 0, LSB/MSB, Ch 2 */
317c478bd9Sstevel@tonic-gate outb(TIMER2_SEL|WORD_ACCESS|MODE0|BINARY_COUNT, TIMER_MODE_PORT);
327c478bd9Sstevel@tonic-gate /* LSB of ticks */
337c478bd9Sstevel@tonic-gate outb(ticks & 0xFF, TIMER2_PORT);
347c478bd9Sstevel@tonic-gate /* MSB of ticks */
357c478bd9Sstevel@tonic-gate outb(ticks >> 8, TIMER2_PORT);
367c478bd9Sstevel@tonic-gate }
377c478bd9Sstevel@tonic-gate
__timer2_running(void)387c478bd9Sstevel@tonic-gate static int __timer2_running(void)
397c478bd9Sstevel@tonic-gate {
407c478bd9Sstevel@tonic-gate return ((inb(PPC_PORTB) & PPCB_T2OUT) == 0);
417c478bd9Sstevel@tonic-gate }
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate #if !defined(CONFIG_TSC_CURRTICKS)
setup_timers(void)447c478bd9Sstevel@tonic-gate void setup_timers(void)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate return;
477c478bd9Sstevel@tonic-gate }
487c478bd9Sstevel@tonic-gate
load_timer2(unsigned int ticks)497c478bd9Sstevel@tonic-gate void load_timer2(unsigned int ticks)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate return __load_timer2(ticks);
527c478bd9Sstevel@tonic-gate }
537c478bd9Sstevel@tonic-gate
timer2_running(void)547c478bd9Sstevel@tonic-gate int timer2_running(void)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate return __timer2_running();
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate
ndelay(unsigned int nsecs)597c478bd9Sstevel@tonic-gate void ndelay(unsigned int nsecs)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate waiton_timer2((nsecs * CLOCK_TICK_RATE)/1000000000);
627c478bd9Sstevel@tonic-gate }
udelay(unsigned int usecs)637c478bd9Sstevel@tonic-gate void udelay(unsigned int usecs)
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate waiton_timer2((usecs * TICKS_PER_MS)/1000);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate #endif /* !defined(CONFIG_TSC_CURRTICKS) */
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate #if defined(CONFIG_TSC_CURRTICKS)
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate #define rdtsc(low,high) \
727c478bd9Sstevel@tonic-gate __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high))
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate #define rdtscll(val) \
757c478bd9Sstevel@tonic-gate __asm__ __volatile__ ("rdtsc" : "=A" (val))
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate /* Number of clock ticks to time with the rtc */
797c478bd9Sstevel@tonic-gate #define LATCH 0xFF
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate #define LATCHES_PER_SEC ((CLOCK_TICK_RATE + (LATCH/2))/LATCH)
827c478bd9Sstevel@tonic-gate #define TICKS_PER_LATCH ((LATCHES_PER_SEC + (TICKS_PER_SEC/2))/TICKS_PER_SEC)
837c478bd9Sstevel@tonic-gate
sleep_latch(void)847c478bd9Sstevel@tonic-gate static void sleep_latch(void)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate __load_timer2(LATCH);
877c478bd9Sstevel@tonic-gate while(__timer2_running());
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate /* ------ Calibrate the TSC -------
917c478bd9Sstevel@tonic-gate * Time how long it takes to excute a loop that runs in known time.
927c478bd9Sstevel@tonic-gate * And find the convertion needed to get to CLOCK_TICK_RATE
937c478bd9Sstevel@tonic-gate */
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate
calibrate_tsc(void)967c478bd9Sstevel@tonic-gate static unsigned long long calibrate_tsc(void)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate unsigned long startlow, starthigh;
997c478bd9Sstevel@tonic-gate unsigned long endlow, endhigh;
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate rdtsc(startlow,starthigh);
1027c478bd9Sstevel@tonic-gate sleep_latch();
1037c478bd9Sstevel@tonic-gate rdtsc(endlow,endhigh);
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate /* 64-bit subtract - gcc just messes up with long longs */
1067c478bd9Sstevel@tonic-gate __asm__("subl %2,%0\n\t"
1077c478bd9Sstevel@tonic-gate "sbbl %3,%1"
1087c478bd9Sstevel@tonic-gate :"=a" (endlow), "=d" (endhigh)
1097c478bd9Sstevel@tonic-gate :"g" (startlow), "g" (starthigh),
1107c478bd9Sstevel@tonic-gate "0" (endlow), "1" (endhigh));
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate /* Error: ECPUTOOFAST */
1137c478bd9Sstevel@tonic-gate if (endhigh)
1147c478bd9Sstevel@tonic-gate goto bad_ctc;
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate endlow *= TICKS_PER_LATCH;
1177c478bd9Sstevel@tonic-gate return endlow;
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate * The CTC wasn't reliable: we got a hit on the very first read,
1217c478bd9Sstevel@tonic-gate * or the CPU was so fast/slow that the quotient wouldn't fit in
1227c478bd9Sstevel@tonic-gate * 32 bits..
1237c478bd9Sstevel@tonic-gate */
1247c478bd9Sstevel@tonic-gate bad_ctc:
1257c478bd9Sstevel@tonic-gate printf("bad_ctc\n");
1267c478bd9Sstevel@tonic-gate return 0;
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate static unsigned long clocks_per_tick;
setup_timers(void)1307c478bd9Sstevel@tonic-gate void setup_timers(void)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate if (!clocks_per_tick) {
1337c478bd9Sstevel@tonic-gate clocks_per_tick = calibrate_tsc();
1347c478bd9Sstevel@tonic-gate /* Display the CPU Mhz to easily test if the calibration was bad */
1357c478bd9Sstevel@tonic-gate printf("CPU %ld Mhz\n", (clocks_per_tick/1000 * TICKS_PER_SEC)/1000);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate
currticks(void)1397c478bd9Sstevel@tonic-gate unsigned long currticks(void)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate unsigned long clocks_high, clocks_low;
1427c478bd9Sstevel@tonic-gate unsigned long currticks;
1437c478bd9Sstevel@tonic-gate /* Read the Time Stamp Counter */
1447c478bd9Sstevel@tonic-gate rdtsc(clocks_low, clocks_high);
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate /* currticks = clocks / clocks_per_tick; */
1477c478bd9Sstevel@tonic-gate __asm__("divl %1"
1487c478bd9Sstevel@tonic-gate :"=a" (currticks)
1497c478bd9Sstevel@tonic-gate :"r" (clocks_per_tick), "0" (clocks_low), "d" (clocks_high));
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate return currticks;
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate static unsigned long long timer_timeout;
__timer_running(void)1567c478bd9Sstevel@tonic-gate static int __timer_running(void)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate unsigned long long now;
1597c478bd9Sstevel@tonic-gate rdtscll(now);
1607c478bd9Sstevel@tonic-gate return now < timer_timeout;
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate
udelay(unsigned int usecs)1637c478bd9Sstevel@tonic-gate void udelay(unsigned int usecs)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate unsigned long long now;
1667c478bd9Sstevel@tonic-gate rdtscll(now);
1677c478bd9Sstevel@tonic-gate timer_timeout = now + usecs * ((clocks_per_tick * TICKS_PER_SEC)/(1000*1000));
1687c478bd9Sstevel@tonic-gate while(__timer_running());
1697c478bd9Sstevel@tonic-gate }
ndelay(unsigned int nsecs)1707c478bd9Sstevel@tonic-gate void ndelay(unsigned int nsecs)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate unsigned long long now;
1737c478bd9Sstevel@tonic-gate rdtscll(now);
1747c478bd9Sstevel@tonic-gate timer_timeout = now + nsecs * ((clocks_per_tick * TICKS_PER_SEC)/(1000*1000*1000));
1757c478bd9Sstevel@tonic-gate while(__timer_running());
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate
load_timer2(unsigned int timer2_ticks)1787c478bd9Sstevel@tonic-gate void load_timer2(unsigned int timer2_ticks)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate unsigned long long now;
1817c478bd9Sstevel@tonic-gate unsigned long clocks;
1827c478bd9Sstevel@tonic-gate rdtscll(now);
1837c478bd9Sstevel@tonic-gate clocks = timer2_ticks * ((clocks_per_tick * TICKS_PER_SEC)/CLOCK_TICK_RATE);
1847c478bd9Sstevel@tonic-gate timer_timeout = now + clocks;
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate
timer2_running(void)1877c478bd9Sstevel@tonic-gate int timer2_running(void)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate return __timer_running();
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate #endif /* RTC_CURRTICKS */
193