OSDN Git Service

Enable to track git://github.com/monaka/binutils.git
[pf3gnuchains/pf3gnuchains3x.git] / libgloss / moxie / qemu-time.c
1 /* qemu-time.c -- stubs so clock can be linked in.
2  *
3  * Copyright (c) 2008 Anthony Green
4  *
5  * The authors hereby grant permission to use, copy, modify, distribute,
6  * and license this software and its documentation for any purpose, provided
7  * that existing copyright notices are retained in all copies and that this
8  * notice is included verbatim in any distributions. No written agreement,
9  * license, or royalty fee is required for any of the authorized uses.
10  * Modifications to this software may be copyrighted by their authors
11  * and need not follow the licensing terms described here, provided that
12  * the new terms are clearly indicated on the first page of each file where
13  * they apply.
14  */
15 #include <errno.h>
16 #include <time.h>
17 #include <sys/time.h>
18 #include <sys/times.h>
19 #include "glue.h"
20
21 /* This is the base address of the mc146818 RTC i/o port. */
22 #define RTC_PORT 0x400
23
24 #define RTC_SECONDS             0x00
25 #define RTC_SECONDS_ALARM       0x01
26 #define RTC_MINUTES             0x02
27 #define RTC_MINUTES_ALARM       0x03
28 #define RTC_HOURS               0x04
29 #define RTC_HOURS_ALARM         0x05
30 #define RTC_DAY_OF_WEEK         0x06
31 #define RTC_DATE_OF_MONTH       0x07
32 #define RTC_MONTH               0x08
33 #define RTC_YEAR                0x09
34 #define RTC_CONFIG_A            0x0A
35 #define RTC_CONFIG_B            0x0B
36 #define RTC_CONFIG_C            0x0C
37 #define RTC_CONFIG_D            0x0D
38
39 /*
40  * _times -- FIXME
41  */
42 int
43 _DEFUN (_times, _times (buf),
44         struct tms *buf)
45 {
46   errno = EINVAL;
47   return (-1);
48 }
49
50 /* Read a value from the RTC port.  */
51 static unsigned char 
52 rtc_read (unsigned char reg)
53 {
54   *(volatile unsigned char *)(RTC_PORT) = reg;
55   return *(volatile unsigned char *)(RTC_PORT + 1);
56 }
57
58 /* Write a value to the RTC port.  */
59 static void 
60 rtc_write (unsigned char reg, unsigned char val)
61 {
62   *(volatile unsigned char *)(RTC_PORT) = reg;
63   *(volatile unsigned char *)(RTC_PORT + 1) = val;
64 }
65
66 /* Convert BCD to Decimal.  */
67 #define BCD2DEC(BYTE) ((((BYTE >> 4) & 0xF) * 10) + (BYTE & 0xF))
68
69 /*
70  * time -- return current time in seconds.
71  */
72 time_t
73 _DEFUN (time, time (t),
74         time_t *t)
75 {
76   struct tm tm;
77   time_t ret;
78
79   tm.tm_sec = BCD2DEC(rtc_read(RTC_SECONDS));
80   tm.tm_min = BCD2DEC(rtc_read(RTC_MINUTES));
81   tm.tm_hour = BCD2DEC(rtc_read(RTC_HOURS));
82   tm.tm_wday = BCD2DEC(rtc_read(RTC_DAY_OF_WEEK)) - 1;
83   tm.tm_mday = BCD2DEC(rtc_read(RTC_DATE_OF_MONTH));
84   tm.tm_mon = BCD2DEC(rtc_read(RTC_MONTH)) - 1;
85   tm.tm_year = BCD2DEC(rtc_read(RTC_YEAR)) + 100;
86
87   /* FIXME.  Not really sure how to handle this.  */
88   tm.tm_isdst = 1;
89
90   ret = mktime(&tm);
91
92   if (t)
93     *t = ret;
94
95   return ret;
96 }
97
98 /*
99  * _gettimeofday -- implement in terms of time, which means we can't
100  * return the microseconds.
101  */
102 int
103 _DEFUN (_gettimeofday, _gettimeofday (tv, tz),
104         struct timeval *tv _AND
105         void *tzvp)
106 {
107   struct timezone *tz = tzvp;
108   struct tm tm;
109   if (tz)
110     tz->tz_minuteswest = tz->tz_dsttime = 0;
111
112   tv->tv_usec = 0;
113   tv->tv_sec = time(0);
114
115   return 0;
116 }