Add ticks_to_ms() and ticks_from_ms() helpers.
Signed-off-by: Kevin O'Connor kevin@koconnor.net --- src/clock.c | 3 +-- src/timer.c | 19 ++++++++++++++++--- src/usb-ehci.c | 3 +-- src/usb-ohci.c | 3 +-- src/usb-uhci.c | 3 +-- src/util.h | 2 ++ 6 files changed, 22 insertions(+), 11 deletions(-)
diff --git a/src/clock.c b/src/clock.c index b9a708b..2f2ca07 100644 --- a/src/clock.c +++ b/src/clock.c @@ -94,8 +94,7 @@ clock_setup(void) u32 seconds = bcd2bin(inb_cmos(CMOS_RTC_SECONDS)); u32 minutes = bcd2bin(inb_cmos(CMOS_RTC_MINUTES)); u32 hours = bcd2bin(inb_cmos(CMOS_RTC_HOURS)); - u32 ticks = (hours * 60 + minutes) * 60 + seconds; - ticks = ((u64)ticks * PIT_TICK_RATE) / PIT_TICK_INTERVAL; + u32 ticks = ticks_from_ms(((hours * 60 + minutes) * 60 + seconds) * 1000); SET_BDA(timer_counter, ticks);
// Setup Century storage diff --git a/src/timer.c b/src/timer.c index 87df97b..0fd0194 100644 --- a/src/timer.c +++ b/src/timer.c @@ -198,6 +198,21 @@ calc_future_tsc_usec(u32 usecs) * IRQ based timer ****************************************************************/
+// Return the number of milliseconds in 'ticks' number of timer irqs. +u32 +ticks_to_ms(u32 ticks) +{ + return DIV_ROUND_UP(PIT_TICK_INTERVAL * 1000 * ticks, PIT_TICK_RATE); +} + +// Return the number of timer irqs in 'ms' number of milliseconds. +u32 +ticks_from_ms(u32 ms) +{ + u32 kticks = DIV_ROUND_UP((u64)ms * PIT_TICK_RATE, PIT_TICK_INTERVAL); + return DIV_ROUND_UP(kticks, 1000); +} + // Calculate the timer value at 'count' number of full timer ticks in // the future. u32 @@ -212,9 +227,7 @@ calc_future_timer(u32 msecs) { if (!msecs) return GET_BDA(timer_counter); - u32 kticks = DIV_ROUND_UP((u64)msecs * PIT_TICK_RATE, PIT_TICK_INTERVAL); - u32 ticks = DIV_ROUND_UP(kticks, 1000); - return calc_future_timer_ticks(ticks); + return calc_future_timer_ticks(ticks_from_ms(msecs)); }
// Check if the given timer value has passed. diff --git a/src/usb-ehci.c b/src/usb-ehci.c index e1e659b..c1638e4 100644 --- a/src/usb-ehci.c +++ b/src/usb-ehci.c @@ -15,7 +15,6 @@ #include "biosvar.h" // GET_LOWFLAT #include "usb-uhci.h" // uhci_setup #include "usb-ohci.h" // ohci_setup -#include "pit.h" // PIT_TICK_RATE
struct usb_ehci_s { struct usb_s usb; @@ -427,7 +426,7 @@ ehci_alloc_intr_pipe(struct usbdevice_s *usbdev int maxpacket = epdesc->wMaxPacketSize; // Determine number of entries needed for 2 timer ticks. int ms = 1<<frameexp; - int count = DIV_ROUND_UP(PIT_TICK_INTERVAL * 1000 * 2, PIT_TICK_RATE * ms); + int count = DIV_ROUND_UP(ticks_to_ms(2), ms); struct ehci_pipe *pipe = memalign_low(EHCI_QH_ALIGN, sizeof(*pipe)); struct ehci_qtd *tds = memalign_low(EHCI_QTD_ALIGN, sizeof(*tds) * count); void *data = malloc_low(maxpacket * count); diff --git a/src/usb-ohci.c b/src/usb-ohci.c index f3a3b3b..b2e1d7f 100644 --- a/src/usb-ohci.c +++ b/src/usb-ohci.c @@ -11,7 +11,6 @@ #include "pci_regs.h" // PCI_BASE_ADDRESS_0 #include "usb.h" // struct usb_s #include "biosvar.h" // GET_LOWFLAT -#include "pit.h" // PIT_TICK_RATE
#define FIT (1 << 31)
@@ -328,7 +327,7 @@ ohci_alloc_intr_pipe(struct usbdevice_s *usbdev int maxpacket = epdesc->wMaxPacketSize; // Determine number of entries needed for 2 timer ticks. int ms = 1<<frameexp; - int count = DIV_ROUND_UP(PIT_TICK_INTERVAL * 1000 * 2, PIT_TICK_RATE * ms)+1; + int count = DIV_ROUND_UP(ticks_to_ms(2), ms) + 1; struct ohci_pipe *pipe = malloc_low(sizeof(*pipe)); struct ohci_td *tds = malloc_low(sizeof(*tds) * count); void *data = malloc_low(maxpacket * count); diff --git a/src/usb-uhci.c b/src/usb-uhci.c index e77f5c1..5fa05a8 100644 --- a/src/usb-uhci.c +++ b/src/usb-uhci.c @@ -12,7 +12,6 @@ #include "pci_regs.h" // PCI_BASE_ADDRESS_4 #include "usb.h" // struct usb_s #include "biosvar.h" // GET_LOWFLAT -#include "pit.h" // PIT_TICK_RATE
struct usb_uhci_s { struct usb_s usb; @@ -283,7 +282,7 @@ uhci_alloc_intr_pipe(struct usbdevice_s *usbdev int maxpacket = epdesc->wMaxPacketSize; // Determine number of entries needed for 2 timer ticks. int ms = 1<<frameexp; - int count = DIV_ROUND_UP(PIT_TICK_INTERVAL * 1000 * 2, PIT_TICK_RATE * ms); + int count = DIV_ROUND_UP(ticks_to_ms(2), ms); count = ALIGN(count, 2); struct uhci_pipe *pipe = malloc_low(sizeof(*pipe)); struct uhci_td *tds = malloc_low(sizeof(*tds) * count); diff --git a/src/util.h b/src/util.h index 15982e2..a91b7f8 100644 --- a/src/util.h +++ b/src/util.h @@ -282,6 +282,8 @@ void useRTC(void); void releaseRTC(void);
// timer.c +u32 ticks_to_ms(u32 ticks); +u32 ticks_from_ms(u32 ms); void pmtimer_setup(u16 ioport); int check_tsc(u64 end); void timer_setup(void);