
.H 2 "Clock Calibration"

There are two clocks in Firefox that are calibrated by software.  The frequencys
of the clocks are measured at 3065 test and the information is put into
the EEPROM.  This data is read by the program that sets up the stable store
(the EEPROM) and messaged to come up with four numbers that are used by the
software to calibrate the clocks.

.H 3 "System Clock"

The system clock is the 25 MHz clock that drives the CPU.  This clock is
divided by two and used to increment the CR16 counter.  During normal
operation the operating system uses the CR16 counter to keep real time.
There are two ways that the information on the clock frequency has to be
given to the operating system.  The first is as a double precision floating
point number that represents the frequency in MHz that the CR16 counter
is incremented at (about 12.5 for Firefox).  The second is an integer
that represents the number of CR16 tics in 10 msec.  The PDC code will
read these numbers from stable store and pass them on to the operating
system.

The steps that are followed in measuring the frequency of the system clock
and massaging the data to get the numbers used by the operating system
are detailed below.  An example is used help make the steps easier to follow.

The frequency of the clock is measured by the 3065 tester.  It must be between
25,012,500 Hz and 24,987,500 Hz or it is a failure.  (For the example assume
that the frequency measured is 24,992,161 Hz.)

The lower limit is subtracted from this number (Example: this gives 4661 = 1235H).
This number is written into stable store.  The LSB is written to the fourth
location in the PDH area (address 1000H is the first location).  The MSB is written to the third
location.  Zeros are written to locations 1 and 2.  The software address for the first location is F004_1000H and the
second location is F004_0004H.  The third location is F004_0008H etc.
(Example: F004_0000H contains 35H and F004_0004H contains 12H)

The software that sets the serial number will read these two locations and
use the data to generate the numbers needed by the operating system.

For the example the CR16 frequency is:

  (24,987,500 + 4661) / 2,000,000 = 12.4960805

When converted to a double precision floating point number it becomes
4028_FDFE_4367_5DDD hex.  The format is that the first bit is the sign
(0).  The next 11 bits are the exponent plus 1023 (402 Hex).  The last 52 bits
are the fractional part.  The first one of the binary number is assumed and
so is not included.  This number is written to stable store stating at the
first location with the MSB first.

The next number that is needed is the number of tics in 10 msec.  For this
example the number is 0001_E821 hex.  This number is written to the stable
starting at the 29th (address 28) location (MSB first).

.H 3 "RTC Calibration"

The time is keep by an MM58274 (U9) when power is down.  The clock for
this part is derived from a 32.768 KHz crystal.  Before the time is set it
is first multiplied by a calibration constant.  When the time is read it
is divided by the calibration constant.  In order to speed up the code
that reads and sets the time the division is eliminated by providing both
the calibration constant and the inverse of the calibration constant in
the stable store.

The calibration constant is normalized to 8000_0000 hex.  A value of
8000_0000 hex means that the clock is exact.

The 3065 tester measures the frequency by putting the RTC chip in test mode
and measuring the frequency on pin13 of U9.  The RTC is put in test mode
by writing 15 dec to the control register (register 0).  The interrupt
register (register 15) should also be written to with zero.

The clock must be within 16384 mHz of 32768 Hz or it is considered a failure.
The number of mHz that the clock frequency is greater than 32751616 mHz is
written into the EEPROM (stable store).  The MSB is in the 6th location
and the LSB is in the 7th location.  Note that these must be read before the
system clock frequency is written because they are in the same area.  Zeros
are written to locations 4 and 5.

Example: If the number returned is 6789 hex (26505 decimal) then the clock
frequency would be 32778121 mHz.  The first calibration constant (used for
reading the clock) would be:

    (32768000 / 32778121) * 2**31 = 2146820569 = 7FF5_E1DA hex

This number is stored in stable store starting in the 9th location with the MSB
first.

The inverse of the number is calculated to be used in writing the time:

    (32778121 / 32768000) * 2**31 = 2148146943 = 800A_1EFE hex

This number is stored in stable store starting in the 13th location with the
MSB first.

