[coreboot] r3272 - in trunk/payloads/libpayload: drivers i386 include include/arch libc

svn at coreboot.org svn at coreboot.org
Sat Apr 26 01:11:02 CEST 2008


Author: jcrouse
Date: 2008-04-26 01:11:02 +0200 (Sat, 26 Apr 2008)
New Revision: 3272

Added:
   trunk/payloads/libpayload/libc/time.c
Modified:
   trunk/payloads/libpayload/drivers/nvram.c
   trunk/payloads/libpayload/i386/timer.c
   trunk/payloads/libpayload/include/arch/rdtsc.h
   trunk/payloads/libpayload/include/arch/types.h
   trunk/payloads/libpayload/include/libpayload.h
   trunk/payloads/libpayload/libc/Makefile.inc
Log:
libpayload:  Add gettimeofday() and friends

Add a gettimeofday() implementation - it works pretty well, but it
drifts a little bit so its not very suitable for keeping time.  It
works best to track changes in time over small periods of time.

Signed-off-by: Jordan Crouse <jordan.crouse at amd.com>
Acked-by: Uwe Hermann <uwe at hermann-uwe.de>


Modified: trunk/payloads/libpayload/drivers/nvram.c
===================================================================
--- trunk/payloads/libpayload/drivers/nvram.c	2008-04-25 23:10:23 UTC (rev 3271)
+++ trunk/payloads/libpayload/drivers/nvram.c	2008-04-25 23:11:02 UTC (rev 3272)
@@ -93,3 +93,43 @@
 	outb(addr, rtc_port);
 	outb(val, rtc_port + 1);
 }
+
+/**
+ * Return 1 if the NVRAM is currently updating and a 0 otherwise
+ * @return A 1 if the NVRAM is updating and 0 otherwise
+ */
+
+int nvram_updating(void)
+{
+       return (nvram_read(NVRAM_RTC_FREQ_SELECT) & NVRAM_RTC_UIP) ? 1 : 0;
+}
+
+/**
+ * Get the current time and date from the RTC
+ *
+ * @param time A pointer to a broken-down time structure
+ */
+void rtc_read_clock(struct tm *time)
+{
+	memset(time, 0, sizeof(*time));
+
+	while(nvram_updating());
+
+	time->tm_mon = bcd2dec(nvram_read(NVRAM_RTC_MONTH)) - 1;
+	time->tm_sec = bcd2dec(nvram_read(NVRAM_RTC_SECONDS));
+	time->tm_min = bcd2dec(nvram_read(NVRAM_RTC_MINUTES));
+	time->tm_mday = bcd2dec(nvram_read(NVRAM_RTC_DAY));
+	time->tm_hour = bcd2dec(nvram_read(NVRAM_RTC_HOURS));
+
+	/* Instead of finding the century register,
+	   we just make an assumption that if the year value is
+	   less then 80, then it is 2000+
+	*/
+
+	time->tm_year = bcd2dec(nvram_read(NVRAM_RTC_YEAR));
+
+	if (time->tm_year < 80)
+		time->tm_year += 100;
+}
+
+

Modified: trunk/payloads/libpayload/i386/timer.c
===================================================================
--- trunk/payloads/libpayload/i386/timer.c	2008-04-25 23:10:23 UTC (rev 3271)
+++ trunk/payloads/libpayload/i386/timer.c	2008-04-25 23:11:02 UTC (rev 3272)
@@ -30,7 +30,7 @@
 #include <libpayload.h>
 #include <arch/rdtsc.h>
 
-static unsigned int cpu_khz;
+unsigned int cpu_khz;
 
 /**
  * Calculate the speed of the processor for use in delays.

Modified: trunk/payloads/libpayload/include/arch/rdtsc.h
===================================================================
--- trunk/payloads/libpayload/include/arch/rdtsc.h	2008-04-25 23:10:23 UTC (rev 3271)
+++ trunk/payloads/libpayload/include/arch/rdtsc.h	2008-04-25 23:11:02 UTC (rev 3272)
@@ -30,9 +30,9 @@
 #ifndef _ARCH_RDTSC_H
 #define _ARCH_RDTSC_H
 
-static inline unsigned long long rdtsc(void)
+static u64 rdtsc(void)
 {
-	unsigned long long val;
+	u64 val;
 	__asm__ __volatile__ ("rdtsc" : "=A" (val));
 	return val;
 }

Modified: trunk/payloads/libpayload/include/arch/types.h
===================================================================
--- trunk/payloads/libpayload/include/arch/types.h	2008-04-25 23:10:23 UTC (rev 3271)
+++ trunk/payloads/libpayload/include/arch/types.h	2008-04-25 23:11:02 UTC (rev 3272)
@@ -50,6 +50,9 @@
 typedef signed long long int64_t;
 typedef signed long long s64;
 
+typedef long time_t;
+typedef long suseconds_t;
+
 #ifndef NULL
 #define NULL ((void *)0)
 #endif

Modified: trunk/payloads/libpayload/include/libpayload.h
===================================================================
--- trunk/payloads/libpayload/include/libpayload.h	2008-04-25 23:10:23 UTC (rev 3271)
+++ trunk/payloads/libpayload/include/libpayload.h	2008-04-25 23:11:02 UTC (rev 3272)
@@ -61,10 +61,26 @@
 #define NVRAM_RTC_DAY            7
 #define NVRAM_RTC_MONTH          8
 #define NVRAM_RTC_YEAR           9
+#define NVRAM_RTC_FREQ_SELECT    10
+#define  NVRAM_RTC_UIP           0x80
 
+struct tm {
+	int tm_sec;
+	int tm_min;
+	int tm_hour;
+	int tm_mday;
+	int tm_mon;
+	int tm_year;
+	int tm_wday;
+	int tm_yday;
+	int tm_isdst;
+};
+
 /* drivers/nvram.c */
 u8 nvram_read(u8 addr);
 void nvram_write(u8 val, u8 addr);
+int nvram_updating(void);
+void rtc_read_clock(struct tm *tm);
 
 /* drivers/keyboard.c */
 void keyboard_init(void);
@@ -182,6 +198,15 @@
 char *strdup(const char *s);
 char *strstr(const char *h, const char *n);
 
+/* libc/time.c */
+
+struct timeval {
+	time_t tv_sec;
+	suseconds_t tv_usec;
+};
+
+int gettimeofday(struct timeval *tv, void *tz);
+
 /* i386/coreboot.c */
 int get_coreboot_info(struct sysinfo_t *info);
 

Modified: trunk/payloads/libpayload/libc/Makefile.inc
===================================================================
--- trunk/payloads/libpayload/libc/Makefile.inc	2008-04-25 23:10:23 UTC (rev 3271)
+++ trunk/payloads/libpayload/libc/Makefile.inc	2008-04-25 23:11:02 UTC (rev 3272)
@@ -29,4 +29,4 @@
 
 TARGETS-y += libc/malloc.o libc/printf.o libc/console.o libc/string.o
 TARGETS-y += libc/memory.o libc/ctype.o libc/ipchecksum.o libc/lib.o
-TARGETS-y += libc/rand.o
+TARGETS-y += libc/rand.o libc/time.o

Added: trunk/payloads/libpayload/libc/time.c
===================================================================
--- trunk/payloads/libpayload/libc/time.c	                        (rev 0)
+++ trunk/payloads/libpayload/libc/time.c	2008-04-25 23:11:02 UTC (rev 3272)
@@ -0,0 +1,128 @@
+/*
+ * This file is part of the libpayload project.
+ *
+ * Copyright (C) 2008 Advanced Micro Devices, Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <libpayload.h>
+#include <arch/rdtsc.h>
+
+extern u32 cpu_khz;
+
+static struct {
+	u64 ticks;
+	time_t secs;
+	suseconds_t usecs;
+} clock;
+
+#define TICKS_PER_SEC (cpu_khz * 1000)
+#define TICKS_PER_USEC (cpu_khz / 1000)
+
+static void update_clock(void)
+{
+	u64 delta = rdtsc() - clock.ticks;
+	int secs;
+
+	clock.ticks += delta;
+
+	secs = (int) (delta / TICKS_PER_SEC);
+	clock.secs += secs;
+	delta -= (secs * TICKS_PER_SEC);
+	clock.usecs += (int) (delta / TICKS_PER_USEC);
+
+	if (clock.usecs > 1000000) {
+		clock.usecs -= 1000000;
+		clock.secs++;
+	}
+}
+
+#ifdef CONFIG_NVRAM
+
+static unsigned int day_of_year(int mon, int day, int year)
+{
+	static u8 mdays[12] = { 31, 28, 31, 30, 31, 30,
+				31, 31, 30, 31, 30, 31 };
+
+	int i, ret = 0;
+
+	for(i = 0; i < mon; i++) {
+		ret += mdays[i];
+
+		if (i == 1 && (year % 4))
+			ret++;
+	}
+
+	return (ret + day);
+}
+
+static void gettimeofday_init(void)
+{
+	int days, delta;
+	struct tm tm;
+
+	rtc_read_clock(&tm);
+	clock.ticks = rdtsc();
+
+	/* Calculate the number of days in the year so far */
+	days = day_of_year(tm.tm_mon, tm.tm_mday, tm.tm_year + 1900);
+
+	delta = tm.tm_year - 70;
+
+	days += (delta * 365);
+
+	/* Figure leap years */
+
+	if (delta > 2)
+	  days += (delta - 2) / 4;
+
+	clock.secs = (days * 86400) + (tm.tm_hour * 3600) +
+		(tm.tm_min * 60) + tm.tm_sec;
+}
+#else
+static void gettimeofday_init(void)
+{
+	/* Record the number of ticks */
+	clock.ticks = rdtsc();
+}
+#endif
+
+int gettimeofday(struct timeval *tv, void *tz)
+{
+	/* Call the gtod init when we need it - this keeps
+	   the code from being included in the binary if we don't
+	   need it
+	*/
+
+	if (!clock.ticks)
+		gettimeofday_init();
+
+	update_clock();
+
+	tv->tv_sec = clock.secs;
+	tv->tv_usec = clock.usecs;
+
+	return 0;
+}





More information about the coreboot mailing list