blob: 688a5100604c943c614e5ffeb8fef25ba5c9b2fd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/h8300/kernel/time.c
3 *
4 * Yoshinori Sato <ysato@users.sourceforge.jp>
5 *
6 * Copied/hacked from:
7 *
8 * linux/arch/m68k/kernel/time.c
9 *
10 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
11 *
12 * This file contains the m68k-specific time handling details.
13 * Most of the stuff is located in the machine specific files.
14 *
15 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
16 * "A Kernel Model for Precision Timekeeping" by Dave Mills
17 */
18
19#include <linux/config.h> /* CONFIG_HEARTBEAT */
20#include <linux/errno.h>
21#include <linux/module.h>
22#include <linux/sched.h>
23#include <linux/kernel.h>
24#include <linux/param.h>
25#include <linux/string.h>
26#include <linux/mm.h>
27#include <linux/timex.h>
28#include <linux/profile.h>
29
30#include <asm/io.h>
31#include <asm/target_time.h>
32
33#define TICK_SIZE (tick_nsec / 1000)
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/*
36 * timer_interrupt() needs to keep up the real-time clock,
37 * as well as call the "do_timer()" routine every clocktick
38 */
39static void timer_interrupt(int irq, void *dummy, struct pt_regs * regs)
40{
41 /* may need to kick the hardware timer */
42 platform_timer_eoi();
43
44 do_timer(regs);
45#ifndef CONFIG_SMP
46 update_process_times(user_mode(regs));
47#endif
48 profile_tick(CPU_PROFILING, regs);
49}
50
51void time_init(void)
52{
53 unsigned int year, mon, day, hour, min, sec;
54
55 /* FIX by dqg : Set to zero for platforms that don't have tod */
56 /* without this time is undefined and can overflow time_t, causing */
57 /* very stange errors */
58 year = 1980;
59 mon = day = 1;
60 hour = min = sec = 0;
61 platform_gettod (&year, &mon, &day, &hour, &min, &sec);
62
63 if ((year += 1900) < 1970)
64 year += 100;
65 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
66 xtime.tv_nsec = 0;
67
68 platform_timer_setup(timer_interrupt);
69}
70
71/*
72 * This version of gettimeofday has near microsecond resolution.
73 */
74void do_gettimeofday(struct timeval *tv)
75{
76 unsigned long flags;
77 unsigned long usec, sec;
78
79 read_lock_irqsave(&xtime_lock, flags);
80 usec = 0;
81 sec = xtime.tv_sec;
82 usec += (xtime.tv_nsec / 1000);
83 read_unlock_irqrestore(&xtime_lock, flags);
84
85 while (usec >= 1000000) {
86 usec -= 1000000;
87 sec++;
88 }
89
90 tv->tv_sec = sec;
91 tv->tv_usec = usec;
92}
93
94EXPORT_SYMBOL(do_gettimeofday);
95
96int do_settimeofday(struct timespec *tv)
97{
98 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
99 return -EINVAL;
100
101 write_lock_irq(&xtime_lock);
102 /* This is revolting. We need to set the xtime.tv_usec
103 * correctly. However, the value in this location is
104 * is value at the last tick.
105 * Discover what correction gettimeofday
106 * would have done, and then undo it!
107 */
108 while (tv->tv_nsec < 0) {
109 tv->tv_nsec += NSEC_PER_SEC;
110 tv->tv_sec--;
111 }
112
113 xtime.tv_sec = tv->tv_sec;
114 xtime.tv_nsec = tv->tv_nsec;
john stultzb149ee22005-09-06 15:17:46 -0700115 ntp_clear();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 write_sequnlock_irq(&xtime_lock);
117 clock_was_set();
118 return 0;
119}
120
121EXPORT_SYMBOL(do_settimeofday);
122
123unsigned long long sched_clock(void)
124{
125 return (unsigned long long)jiffies * (1000000000 / HZ);
126
127}