OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [rc203soc/] [sw/] [uClinux/] [arch/] [or32/] [kernel/] [time.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1624 jcastillo
/*
2
 *  linux/arch/or32/kernel/time.c
3
 *
4
 *  Copied/hacked from:
5
 *
6
 *  linux/arch/m68knommu/kernel/time.c
7
 *
8
 *  Copyright (C) 1991, 1992, 1995  Linus Torvalds
9
 *
10
 * This file contains the m68k-specific time handling details.
11
 * Most of the stuff is located in the machine specific files.
12
 */
13
 
14
#include <linux/config.h>
15
#include <linux/errno.h>
16
#include <linux/sched.h>
17
#include <linux/kernel.h>
18
#include <linux/param.h>
19
#include <linux/string.h>
20
#include <linux/mm.h>
21
 
22
#include <asm/machdep.h>
23
#include <asm/segment.h>
24
#include <asm/io.h>
25
 
26
#include <linux/timex.h>
27
 
28
 
29
static inline int set_rtc_mmss(unsigned long nowtime)
30
{
31
  if (mach_set_clock_mmss)
32
    return mach_set_clock_mmss (nowtime);
33
  return -1;
34
}
35
 
36
/*
37
 * timer_interrupt() needs to keep up the real-time clock,
38
 * as well as call the "do_timer()" routine every clocktick
39
 */
40
/* void timer_interrupt(int irq, void *dummy, struct pt_regs * regs)
41
*/
42
void timer_interrupt(struct pt_regs * regs)
43
{
44
        /* last time the cmos clock got updated */
45
        static long last_rtc_update=0;
46
 
47
        /* may need to kick the hardware timer */
48
        if (mach_tick)
49
          mach_tick();
50
 
51
        do_timer(regs);
52
 
53
        /*
54
         * If we have an externally synchronized Linux clock, then update
55
         * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
56
         * called as close as possible to 500 ms before the new second starts.
57
         */
58
        if (time_state != TIME_BAD && xtime.tv_sec > last_rtc_update + 660 &&
59
            xtime.tv_usec > 500000 - (tick >> 1) &&
60
            xtime.tv_usec < 500000 + (tick >> 1)) {
61
          if (set_rtc_mmss(xtime.tv_sec) == 0)
62
            last_rtc_update = xtime.tv_sec;
63
          else
64
            last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
65
        }
66
 
67
}
68
 
69
/* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
70
 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
71
 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
72
 *
73
 * [For the Julian calendar (which was used in Russia before 1917,
74
 * Britain & colonies before 1752, anywhere else before 1582,
75
 * and is still in use by some communities) leave out the
76
 * -year/100+year/400 terms, and add 10.]
77
 *
78
 * This algorithm was first published by Gauss (I think).
79
 *
80
 * WARNING: this function will overflow on 2106-02-07 06:28:16 on
81
 * machines were long is 32-bit! (However, as time_t is signed, we
82
 * will already get problems at other places on 2038-01-19 03:14:08)
83
 */
84
static inline unsigned long mktime(unsigned int year, unsigned int mon,
85
        unsigned int day, unsigned int hour,
86
        unsigned int min, unsigned int sec)
87
{
88
        if (0 >= (int) (mon -= 2)) {     /* 1..12 -> 11,12,1..10 */
89
                mon += 12;      /* Puts Feb last since it has leap day */
90
                year -= 1;
91
        }
92
        return (((
93
            (unsigned long)(year/4 - year/100 + year/400 + 367*mon/12 + day) +
94
              year*365 - 719499
95
            )*24 + hour /* now have hours */
96
           )*60 + min /* now have minutes */
97
          )*60 + sec; /* finally seconds */
98
}
99
 
100
void time_init(void)
101
{
102
        unsigned int year, mon, day, hour, min, sec;
103
 
104
        extern void arch_gettod(int *year, int *mon, int *day, int *hour,
105
                                int *min, int *sec);
106
 
107
        arch_gettod (&year, &mon, &day, &hour, &min, &sec);
108
 
109
        if ((year += 1900) < 1970)
110
                year += 100;
111
        xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
112
        xtime.tv_usec = 0;
113
 
114
        if (mach_sched_init)
115
                mach_sched_init(timer_interrupt);
116
}
117
 
118
/*
119
 * This version of gettimeofday has near microsecond resolution.
120
 */
121
void do_gettimeofday(struct timeval *tv)
122
{
123
        unsigned long flags;
124
 
125
        save_flags(flags);
126
        cli();
127
        *tv = xtime;
128
        if (mach_gettimeoffset) {
129
          tv->tv_usec += mach_gettimeoffset();
130
          if (tv->tv_usec >= 1000000) {
131
            tv->tv_usec -= 1000000;
132
            tv->tv_sec++;
133
          }
134
        }
135
        restore_flags(flags);
136
}
137
 
138
void do_settimeofday(struct timeval *tv)
139
{
140
        cli();
141
        /* This is revolting. We need to set the xtime.tv_usec
142
         * correctly. However, the value in this location is
143
         * is value at the last tick.
144
         * Discover what correction gettimeofday
145
         * would have done, and then undo it!
146
         */
147
        if (mach_gettimeoffset)
148
          tv->tv_usec -= mach_gettimeoffset();
149
 
150
        if (tv->tv_usec < 0) {
151
                tv->tv_usec += 1000000;
152
                tv->tv_sec--;
153
        }
154
 
155
        xtime = *tv;
156
        time_state = TIME_BAD;
157
        time_maxerror = MAXPHASE;
158
        time_esterror = MAXPHASE;
159
        sti();
160
}
161
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.