]> xenbits.xensource.com Git - people/andrewcoop/seabios.git/commitdiff
Add helper functions to convert timer irqs to milliseconds.
authorKevin O'Connor <kevin@koconnor.net>
Sat, 20 Jul 2013 16:08:48 +0000 (12:08 -0400)
committerKevin O'Connor <kevin@koconnor.net>
Sat, 20 Jul 2013 23:29:54 +0000 (19:29 -0400)
Add ticks_to_ms() and ticks_from_ms() helpers.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
src/clock.c
src/timer.c
src/usb-ehci.c
src/usb-ohci.c
src/usb-uhci.c
src/util.h

index b9a708b99bd91d7ea183a91c17ca54c7f67dd2c7..2f2ca07d03f439a78f899700d474a1910ec9e8ba 100644 (file)
@@ -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
index 87df97b13ac0dd3264d797558b7af89d809f825b..0fd019449374db56d8cbdf0a6bcec438a8bf3343 100644 (file)
@@ -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.
index e1e659b90a20917188f8628b0da47bd6f7a47993..c1638e45d448bc83b1a76eebb88619ba3ba3504e 100644 (file)
@@ -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);
index f3a3b3b5c6f2d93666afb640114b6e0630afb8bc..b2e1d7f273cd33de00ba7bc3952eeb3143e8bfc4 100644 (file)
@@ -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);
index e77f5c1a729e403288aa63e5227940af86351a43..5fa05a8bda59ae892b5f2a22e663da01993d352a 100644 (file)
@@ -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);
index 15982e25d44ec025a8e26f2f1475c63e12010ebf..a91b7f8e0b17bee69fedc313b2b77bd2b8e9b7e1 100644 (file)
@@ -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);