[SeaBIOS] [PATCH 1/4] Move PIT setup from clock.c to hw/timer.c.

Kevin O'Connor kevin at koconnor.net
Thu Sep 19 03:58:16 CEST 2013


Move the hardware setup to the hw/timer.c code.  This eliminates the
need for a separate hw/pit.h file with definitions.

Also, move the IRQ counting code (which is dependent on the BDA) from
hw/timer.c to clock.c.

Signed-off-by: Kevin O'Connor <kevin at koconnor.net>
---
 src/clock.c    | 41 ++++++++++++++++++++++++++++++-----------
 src/hw/pit.h   | 29 -----------------------------
 src/hw/timer.c | 58 +++++++++++++++++++++++++++++++++-------------------------
 src/util.h     |  7 ++++---
 4 files changed, 67 insertions(+), 68 deletions(-)
 delete mode 100644 src/hw/pit.h

diff --git a/src/clock.c b/src/clock.c
index 7e24626..e54944b 100644
--- a/src/clock.c
+++ b/src/clock.c
@@ -9,7 +9,6 @@
 #include "bregs.h" // struct bregs
 #include "hw/cmos.h" // inb_cmos
 #include "hw/pic.h" // pic_eoi1
-#include "hw/pit.h" // PM_SEL_TIMER0
 #include "hw/usb-hid.h" // usb_check_event
 #include "output.h" // debug_enter
 #include "stacks.h" // yield
@@ -57,16 +56,6 @@ rtc_updating(void)
 }
 
 static void
-pit_setup(void)
-{
-    // timer0: binary count, 16bit count, mode 2
-    outb(PM_SEL_TIMER0|PM_ACCESS_WORD|PM_MODE2|PM_CNT_BINARY, PORT_PIT_MODE);
-    // maximum count of 0000H = 18.2Hz
-    outb(0x0, PORT_PIT_COUNTER0);
-    outb(0x0, PORT_PIT_COUNTER0);
-}
-
-static void
 rtc_setup(void)
 {
     outb_cmos(0x26, CMOS_STATUS_A);    // 32,768Khz src, 976.5625us updates
@@ -352,6 +341,36 @@ handle_08(void)
 
 
 /****************************************************************
+ * IRQ based timer
+ ****************************************************************/
+
+// Calculate the timer value at 'count' number of full timer ticks in
+// the future.
+u32
+irqtimer_calc_ticks(u32 count)
+{
+    return (GET_BDA(timer_counter) + count + 1) % TICKS_PER_DAY;
+}
+
+// Return the timer value that is 'msecs' time in the future.
+u32
+irqtimer_calc(u32 msecs)
+{
+    if (!msecs)
+        return GET_BDA(timer_counter);
+    return irqtimer_calc_ticks(ticks_from_ms(msecs));
+}
+
+// Check if the given timer value has passed.
+int
+irqtimer_check(u32 end)
+{
+    return (((GET_BDA(timer_counter) + TICKS_PER_DAY - end) % TICKS_PER_DAY)
+            < (TICKS_PER_DAY/2));
+}
+
+
+/****************************************************************
  * Periodic timer
  ****************************************************************/
 
diff --git a/src/hw/pit.h b/src/hw/pit.h
deleted file mode 100644
index 098f270..0000000
--- a/src/hw/pit.h
+++ /dev/null
@@ -1,29 +0,0 @@
-// Definitions for the Intel 8253 Programmable Interrupt Timer (PIT).
-#ifndef __PIT_H
-#define __PIT_H
-
-// Bits for PORT_PIT_MODE
-#define PM_SEL_TIMER0   (0<<6)
-#define PM_SEL_TIMER1   (1<<6)
-#define PM_SEL_TIMER2   (2<<6)
-#define PM_SEL_READBACK (3<<6)
-#define PM_ACCESS_LATCH  (0<<4)
-#define PM_ACCESS_LOBYTE (1<<4)
-#define PM_ACCESS_HIBYTE (2<<4)
-#define PM_ACCESS_WORD   (3<<4)
-#define PM_MODE0 (0<<1)
-#define PM_MODE1 (1<<1)
-#define PM_MODE2 (2<<1)
-#define PM_MODE3 (3<<1)
-#define PM_MODE4 (4<<1)
-#define PM_MODE5 (5<<1)
-#define PM_CNT_BINARY (0<<0)
-#define PM_CNT_BCD    (1<<0)
-#define PM_READ_COUNTER0 (1<<1)
-#define PM_READ_COUNTER1 (1<<2)
-#define PM_READ_COUNTER2 (1<<3)
-#define PM_READ_STATUSVALUE (0<<4)
-#define PM_READ_VALUE       (1<<4)
-#define PM_READ_STATUS      (2<<4)
-
-#endif // pit.h
diff --git a/src/hw/timer.c b/src/hw/timer.c
index 6477313..ec1e3c5 100644
--- a/src/hw/timer.c
+++ b/src/hw/timer.c
@@ -8,11 +8,34 @@
 #include "config.h" // CONFIG_*
 #include "ioport.h" // PORT_PIT_MODE
 #include "output.h" // dprintf
-#include "pit.h" // PM_SEL_TIMER0
 #include "stacks.h" // yield
 #include "util.h" // timer_setup
 #include "x86.h" // cpuid
 
+// Bits for PORT_PIT_MODE
+#define PM_SEL_TIMER0   (0<<6)
+#define PM_SEL_TIMER1   (1<<6)
+#define PM_SEL_TIMER2   (2<<6)
+#define PM_SEL_READBACK (3<<6)
+#define PM_ACCESS_LATCH  (0<<4)
+#define PM_ACCESS_LOBYTE (1<<4)
+#define PM_ACCESS_HIBYTE (2<<4)
+#define PM_ACCESS_WORD   (3<<4)
+#define PM_MODE0 (0<<1)
+#define PM_MODE1 (1<<1)
+#define PM_MODE2 (2<<1)
+#define PM_MODE3 (3<<1)
+#define PM_MODE4 (4<<1)
+#define PM_MODE5 (5<<1)
+#define PM_CNT_BINARY (0<<0)
+#define PM_CNT_BCD    (1<<0)
+#define PM_READ_COUNTER0 (1<<1)
+#define PM_READ_COUNTER1 (1<<2)
+#define PM_READ_COUNTER2 (1<<3)
+#define PM_READ_STATUSVALUE (0<<4)
+#define PM_READ_VALUE       (1<<4)
+#define PM_READ_STATUS      (2<<4)
+
 // Bits for PORT_PS2_CTRLB
 #define PPCB_T2GATE (1<<0)
 #define PPCB_SPKR   (1<<1)
@@ -27,7 +50,7 @@ u8 ShiftTSC VARFSEG;
 
 
 /****************************************************************
- * Timer setup
+ * Internal timer setup
  ****************************************************************/
 
 #define CALIBRATE_COUNT 0x800   // Approx 1.7ms
@@ -197,7 +220,7 @@ timer_calc_usec(u32 usecs)
 
 
 /****************************************************************
- * IRQ based timer
+ * PIT setup
  ****************************************************************/
 
 #define PIT_TICK_INTERVAL 65536 // Default interval for 18.2Hz timer
@@ -218,27 +241,12 @@ ticks_from_ms(u32 ms)
     return DIV_ROUND_UP(t, 1000 * PMTIMER_TO_PIT);
 }
 
-// Calculate the timer value at 'count' number of full timer ticks in
-// the future.
-u32
-irqtimer_calc_ticks(u32 count)
-{
-    return (GET_BDA(timer_counter) + count + 1) % TICKS_PER_DAY;
-}
-
-// Return the timer value that is 'msecs' time in the future.
-u32
-irqtimer_calc(u32 msecs)
-{
-    if (!msecs)
-        return GET_BDA(timer_counter);
-    return irqtimer_calc_ticks(ticks_from_ms(msecs));
-}
-
-// Check if the given timer value has passed.
-int
-irqtimer_check(u32 end)
+void
+pit_setup(void)
 {
-    return (((GET_BDA(timer_counter) + TICKS_PER_DAY - end) % TICKS_PER_DAY)
-            < (TICKS_PER_DAY/2));
+    // timer0: binary count, 16bit count, mode 2
+    outb(PM_SEL_TIMER0|PM_ACCESS_WORD|PM_MODE2|PM_CNT_BINARY, PORT_PIT_MODE);
+    // maximum count of 0000H = 18.2Hz
+    outb(0x0, PORT_PIT_COUNTER0);
+    outb(0x0, PORT_PIT_COUNTER0);
 }
diff --git a/src/util.h b/src/util.h
index 880c04a..57c3ae9 100644
--- a/src/util.h
+++ b/src/util.h
@@ -55,6 +55,9 @@ int cdrom_boot(struct drive_s *drive_g);
 // clock.c
 void clock_setup(void);
 void handle_1583(struct bregs *regs);
+u32 irqtimer_calc_ticks(u32 count);
+u32 irqtimer_calc(u32 msecs);
+int irqtimer_check(u32 end);
 void handle_1586(struct bregs *regs);
 void useRTC(void);
 void releaseRTC(void);
@@ -149,9 +152,7 @@ void usleep(u32 count);
 void msleep(u32 count);
 u32 ticks_to_ms(u32 ticks);
 u32 ticks_from_ms(u32 ms);
-u32 irqtimer_calc_ticks(u32 count);
-u32 irqtimer_calc(u32 msecs);
-int irqtimer_check(u32 end);
+void pit_setup(void);
 
 // jpeg.c
 struct jpeg_decdata *jpeg_alloc(void);
-- 
1.8.3.1




More information about the SeaBIOS mailing list