Move internal timer code from clock.c to a new file timer.c.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Don't pass khz to pmtimer_setup - it's always PM_TIMER_FREQUENCY.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Add helper functions to convert timer irqs to milliseconds.

Add ticks_to_ms() and ticks_from_ms() helpers.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Improve accuracy of internal timers.

The TICKS_PER_DAY setting is a bios standard and needs to be 1573040
for compatibility.  However, there are actually ~1573042.24 ticks in a
day.  So, only use TICKS_PER_DAY when working with the BDA
timer_counter - not when calculating any internal times.

The PIT hz is actually 143181800 / 12 (~1193181.667).  This can be
accurately encoded as PMTIMER hz / 3.  Because the PIT hz is usually
multiplied and divided by other numbers, we can use the PMTIMER hz and
defer the division by 3 to improve accuracy.

When doing division for delay time calculations, always round up the
division so the delay is never less than the requested time.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Rename cpu_khz to TimerKHz.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Shift CPU TSC down to reduce need for 64bit variables.

The time-stamp-counter has a higher accuracy than is needed in
SeaBIOS.  Down shift it to ensure it safely fits in a 32bit variable.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Rename check_timer() function (and similar) to irqtimer_check().

Rename functions to be more consistent and so they are not confused
with the normal timer functions.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Rename check_tsc() (and similar) to timer_check() and use u32.

Rename the check_tsc() function to timer_check().  The CPU TSC is
often not the basis of the timer, so use a more appropriate name.

Convert all callers that were using u64 for the timers to use u32.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Separate out timer setup code.

Split timer_setup() - put the tsc calibration code in its own
function.

Group all the timer setup functions together.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Unify pmtimer_read() and pittimer_read() code.

These two functions both need to add in extra high bits to their
timers, and this code is the bulk of these functions.  Factor out the
duplicate code.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Default unused UMB areas to be read-only.

It looks like MSDOS 6 wants the UMB area to be read-only in order for
it to use it.  FreeDOS doesn't care, but it always maps a page from
high mem when it does use the area.  So, add an option to control
whether unused UMB ram is marked read-only and default it to read-only
as that seems to be more compatible.

This also fixes an off-by-one bug in the shadow range checking code.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Add missing mathcp_setup() call to CSM code.

It looks like when mathcp_setup() got moved from interface_init() to
platform_hardware_setup() in 3a735baa the corresponding change was not
made to the CSM code.  Update it now.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>