From: "Oleg I. Vdovikin" <vdovikin@jscc.ru>
To: "Alan Cox" <alan@lxorguk.ukuu.org.uk>,
"Ivan Kokshaysky" <ink@jurassic.park.msu.ru>
Cc: <linux-kernel@vger.kernel.org>, "Richard Henderson" <rth@twiddle.net>
Subject: Re: alpha - generic_init_pit - why using RTC for calibration?
Date: Tue, 3 Jul 2001 17:06:52 +0400 [thread overview]
Message-ID: <016c01c103c1$07f283f0$4d28d0c3@jscc.ru> (raw)
In-Reply-To: <022901c10095$f4fca650$4d28d0c3@jscc.ru> <20010629211931.A582@jurassic.park.msu.ru> <009801c102dc$c42d7a60$4d28d0c3@jscc.ru>
[-- Attachment #1: Type: text/plain, Size: 2092 bytes --]
Here is the patch against the buggy Cypress RTC which is found on some
Alpha boards. It's tested with 2.2.16 & 2.2.19 kernels and as seems should
work with 2.4.x kernels. This patch differs from initial Ivan's version by
the "cc" variable type & different calibrate divisor usage for better
accuracy.
Please consider applying this patch against the 2.2.x tree. It's really
needed due to overall performance reasons.
Thanks,
Oleg.
--- linux/arch/alpha/kernel/time.c.orig Mon Jul 2 14:05:09 2001
+++ linux/arch/alpha/kernel/time.c Mon Jul 2 15:47:45 2001
@@ -231,6 +231,49 @@
outb(0x13, 0x42);
}
+/*
+ * Calibrate CPU clock using legacy 8254 timer/counter. Stolen from
+ * arch/i386/time.c.
+ */
+
+#define CALIBRATE_DIVISOR 0xffff
+
+static unsigned long __init
+calibrate_cc(void)
+{
+ unsigned int cc;
+ unsigned long count = 0;
+
+ /* Set the Gate high, disable speaker */
+ outb((inb(0x61) & ~0x02) | 0x01, 0x61);
+
+ /*
+ * Now let's take care of CTC channel 2
+ *
+ * Set the Gate high, program CTC channel 2 for mode 0,
+ * (interrupt on terminal count mode), binary count,
+ * load maximum divisor we can get for accuracy - 65535
+ */
+
+ outb(0xb0, 0x43); /* binary, mode 0, LSB/MSB, Ch 2 */
+ outb(CALIBRATE_DIVISOR & 0xff, 0x42); /* LSB of count */
+ outb(CALIBRATE_DIVISOR >> 8, 0x42); /* MSB of count */
+
+ /* we still should not hang if timer not runing or N/A */
+ for (cc = rpcc(); (inb(0x61) & 0x20) == 0 && !(count >> 32); count++);
+
+ /* cycles delta */
+ cc = rpcc() - cc;
+
+ /* check for the reliable result */
+ if ((count < 1) || (count >> 32))
+ return 0;
+
+ /* and the final result in HZ */
+ return ((unsigned long)cc * CLOCK_TICK_RATE) / CALIBRATE_DIVISOR;
+}
+
+
void
time_init(void)
{
@@ -239,6 +282,10 @@
unsigned long cycle_freq, ppm_error;
long diff;
+ /* Calibrate CPU clock using CTC. If this fails, use RTC. */
+ if (!est_cycle_freq)
+ est_cycle_freq = calibrate_cc();
+
/*
* The Linux interpretation of the CMOS clock register contents:
* When the Update-In-Progress (UIP) flag goes from 1 to 0, the
[-- Attachment #2: linux-2.2.x.rtc.patch --]
[-- Type: application/octet-stream, Size: 1972 bytes --]
--- linux/arch/alpha/kernel/time.c.orig Mon Jul 2 14:05:09 2001
+++ linux/arch/alpha/kernel/time.c Mon Jul 2 15:47:45 2001
@@ -231,6 +231,49 @@
outb(0x13, 0x42);
}
+/*
+ * Calibrate CPU clock using legacy 8254 timer/counter. Stolen from
+ * arch/i386/time.c.
+ */
+
+#define CALIBRATE_DIVISOR 0xffff
+
+static unsigned long __init
+calibrate_cc(void)
+{
+ unsigned int cc;
+ unsigned long count = 0;
+
+ /* Set the Gate high, disable speaker */
+ outb((inb(0x61) & ~0x02) | 0x01, 0x61);
+
+ /*
+ * Now let's take care of CTC channel 2
+ *
+ * Set the Gate high, program CTC channel 2 for mode 0,
+ * (interrupt on terminal count mode), binary count,
+ * load maximum divisor we can get for accuracy - 65535
+ */
+
+ outb(0xb0, 0x43); /* binary, mode 0, LSB/MSB, Ch 2 */
+ outb(CALIBRATE_DIVISOR & 0xff, 0x42); /* LSB of count */
+ outb(CALIBRATE_DIVISOR >> 8, 0x42); /* MSB of count */
+
+ /* we still should not hang if timer not runing or N/A */
+ for (cc = rpcc(); (inb(0x61) & 0x20) == 0 && !(count >> 32); count++);
+
+ /* cycles delta */
+ cc = rpcc() - cc;
+
+ /* check for the reliable result */
+ if ((count < 1) || (count >> 32))
+ return 0;
+
+ /* and the final result in HZ */
+ return ((unsigned long)cc * CLOCK_TICK_RATE) / CALIBRATE_DIVISOR;
+}
+
+
void
time_init(void)
{
@@ -239,6 +282,10 @@
unsigned long cycle_freq, ppm_error;
long diff;
+ /* Calibrate CPU clock using CTC. If this fails, use RTC. */
+ if (!est_cycle_freq)
+ est_cycle_freq = calibrate_cc();
+
/*
* The Linux interpretation of the CMOS clock register contents:
* When the Update-In-Progress (UIP) flag goes from 1 to 0, the
@@ -275,7 +322,7 @@
if (diff < 0)
diff = -diff;
ppm_error = (diff * 1000000L) / cycle_freq;
-#if 0
+#if 1
printk("Alpha clock init: HWRPB %lu, Measured %lu, error=%lu ppm.\n",
hwrpb->cycle_freq, est_cycle_freq, ppm_error);
#endif
next prev parent reply other threads:[~2001-07-03 13:06 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-06-29 12:20 alpha - generic_init_pit - why using RTC for calibration? Oleg I. Vdovikin
2001-06-29 17:19 ` Ivan Kokshaysky
2001-07-02 9:52 ` Oleg I. Vdovikin
2001-07-03 13:06 ` Oleg I. Vdovikin [this message]
2001-07-04 18:45 ` [patch] " Richard Henderson
2001-07-05 7:14 ` Oleg I. Vdovikin
[not found] ` <3B441618.638A3FC@mandrakesoft.com>
2001-07-05 8:36 ` Oleg I. Vdovikin
2001-07-05 8:47 ` Jeff Garzik
2001-07-06 8:45 ` Oleg I. Vdovikin
2001-07-05 9:43 ` Ivan Kokshaysky
2001-07-06 9:03 ` Oleg I. Vdovikin
2001-07-06 12:00 ` Ivan Kokshaysky
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='016c01c103c1$07f283f0$4d28d0c3@jscc.ru' \
--to=vdovikin@jscc.ru \
--cc=alan@lxorguk.ukuu.org.uk \
--cc=ink@jurassic.park.msu.ru \
--cc=linux-kernel@vger.kernel.org \
--cc=rth@twiddle.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox