Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/m32r/kernel/time.c |
| 3 | * |
| 4 | * Copyright (c) 2001, 2002 Hiroyuki Kondo, Hirokazu Takata, |
| 5 | * Hitoshi Yamamoto |
| 6 | * Taken from i386 version. |
| 7 | * Copyright (C) 1991, 1992, 1995 Linus Torvalds |
| 8 | * Copyright (C) 1996, 1997, 1998 Ralf Baechle |
| 9 | * |
| 10 | * This file contains the time handling details for PC-style clocks as |
| 11 | * found in some MIPS systems. |
| 12 | * |
| 13 | * Some code taken from sh version. |
| 14 | * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka |
| 15 | * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org> |
| 16 | */ |
| 17 | |
| 18 | #undef DEBUG_TIMER |
| 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/errno.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/sched.h> |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/param.h> |
| 26 | #include <linux/string.h> |
| 27 | #include <linux/mm.h> |
| 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/profile.h> |
| 30 | |
| 31 | #include <asm/io.h> |
| 32 | #include <asm/m32r.h> |
| 33 | |
| 34 | #include <asm/hw_irq.h> |
| 35 | |
| 36 | #ifdef CONFIG_SMP |
Al Viro | 9c8e7f5 | 2006-10-07 16:29:18 +0100 | [diff] [blame] | 37 | extern void smp_local_timer_interrupt(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #endif |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #define TICK_SIZE (tick_nsec / 1000) |
| 41 | |
| 42 | /* |
| 43 | * Change this if you have some constant time drift |
| 44 | */ |
| 45 | |
| 46 | /* This is for machines which generate the exact clock. */ |
| 47 | #define USECS_PER_JIFFY (1000000/HZ) |
| 48 | |
| 49 | static unsigned long latch; |
| 50 | |
john stultz | 95ad759 | 2009-09-21 17:04:04 -0700 | [diff] [blame] | 51 | u32 arch_gettimeoffset(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
| 53 | unsigned long elapsed_time = 0; /* [us] */ |
| 54 | |
| 55 | #if defined(CONFIG_CHIP_M32102) || defined(CONFIG_CHIP_XNUX2) \ |
| 56 | || defined(CONFIG_CHIP_VDEC2) || defined(CONFIG_CHIP_M32700) \ |
Hirokazu Takata | 9287d95 | 2006-01-06 00:18:41 -0800 | [diff] [blame] | 57 | || defined(CONFIG_CHIP_OPSP) || defined(CONFIG_CHIP_M32104) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #ifndef CONFIG_SMP |
| 59 | |
| 60 | unsigned long count; |
| 61 | |
| 62 | /* timer count may underflow right here */ |
| 63 | count = inl(M32R_MFT2CUT_PORTL); |
| 64 | |
| 65 | if (inl(M32R_ICU_CR18_PORTL) & 0x00000100) /* underflow check */ |
| 66 | count = 0; |
| 67 | |
| 68 | count = (latch - count) * TICK_SIZE; |
| 69 | elapsed_time = (count + latch / 2) / latch; |
| 70 | /* NOTE: LATCH is equal to the "interval" value (= reload count). */ |
| 71 | |
| 72 | #else /* CONFIG_SMP */ |
| 73 | unsigned long count; |
| 74 | static unsigned long p_jiffies = -1; |
| 75 | static unsigned long p_count = 0; |
| 76 | |
| 77 | /* timer count may underflow right here */ |
| 78 | count = inl(M32R_MFT2CUT_PORTL); |
| 79 | |
| 80 | if (jiffies == p_jiffies && count > p_count) |
| 81 | count = 0; |
| 82 | |
| 83 | p_jiffies = jiffies; |
| 84 | p_count = count; |
| 85 | |
| 86 | count = (latch - count) * TICK_SIZE; |
| 87 | elapsed_time = (count + latch / 2) / latch; |
| 88 | /* NOTE: LATCH is equal to the "interval" value (= reload count). */ |
| 89 | #endif /* CONFIG_SMP */ |
| 90 | #elif defined(CONFIG_CHIP_M32310) |
| 91 | #warning do_gettimeoffse not implemented |
| 92 | #else |
| 93 | #error no chip configuration |
| 94 | #endif |
| 95 | |
john stultz | 95ad759 | 2009-09-21 17:04:04 -0700 | [diff] [blame] | 96 | return elapsed_time * 1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | * In order to set the CMOS clock precisely, set_rtc_mmss has to be |
| 101 | * called 500 ms after the second nowtime has started, because when |
| 102 | * nowtime is written into the registers of the CMOS clock, it will |
| 103 | * jump to the next second precisely 500 ms later. Check the Motorola |
| 104 | * MC146818A or Dallas DS12887 data sheet for details. |
| 105 | * |
| 106 | * BUG: This routine does not handle hour overflow properly; it just |
| 107 | * sets the minutes. Usually you won't notice until after reboot! |
| 108 | */ |
| 109 | static inline int set_rtc_mmss(unsigned long nowtime) |
| 110 | { |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | /* last time the cmos clock got updated */ |
| 115 | static long last_rtc_update = 0; |
| 116 | |
| 117 | /* |
| 118 | * timer_interrupt() needs to keep up the real-time clock, |
| 119 | * as well as call the "do_timer()" routine every clocktick |
| 120 | */ |
Adrian Bunk | 81e4807 | 2008-09-24 15:01:47 +0900 | [diff] [blame] | 121 | static irqreturn_t timer_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
| 123 | #ifndef CONFIG_SMP |
Al Viro | 9c8e7f5 | 2006-10-07 16:29:18 +0100 | [diff] [blame] | 124 | profile_tick(CPU_PROFILING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | #endif |
john stultz | 95ad759 | 2009-09-21 17:04:04 -0700 | [diff] [blame] | 126 | /* XXX FIXME. Uh, the xtime_lock should be held here, no? */ |
Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 127 | do_timer(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
| 129 | #ifndef CONFIG_SMP |
Al Viro | 9c8e7f5 | 2006-10-07 16:29:18 +0100 | [diff] [blame] | 130 | update_process_times(user_mode(get_irq_regs())); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | #endif |
| 132 | /* |
| 133 | * If we have an externally synchronized Linux clock, then update |
| 134 | * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be |
| 135 | * called as close as possible to 500 ms before the new second starts. |
| 136 | */ |
Hirokazu Takata | 2757a71 | 2005-08-01 21:11:35 -0700 | [diff] [blame] | 137 | write_seqlock(&xtime_lock); |
john stultz | b149ee2 | 2005-09-06 15:17:46 -0700 | [diff] [blame] | 138 | if (ntp_synced() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | && xtime.tv_sec > last_rtc_update + 660 |
| 140 | && (xtime.tv_nsec / 1000) >= 500000 - ((unsigned)TICK_SIZE) / 2 |
| 141 | && (xtime.tv_nsec / 1000) <= 500000 + ((unsigned)TICK_SIZE) / 2) |
| 142 | { |
| 143 | if (set_rtc_mmss(xtime.tv_sec) == 0) |
| 144 | last_rtc_update = xtime.tv_sec; |
| 145 | else /* do it again in 60 s */ |
| 146 | last_rtc_update = xtime.tv_sec - 600; |
| 147 | } |
Hirokazu Takata | 2757a71 | 2005-08-01 21:11:35 -0700 | [diff] [blame] | 148 | write_sequnlock(&xtime_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | /* As we return to user mode fire off the other CPU schedulers.. |
| 150 | this is basically because we don't yet share IRQ's around. |
| 151 | This message is rigged to be safe on the 386 - basically it's |
| 152 | a hack, so don't look closely for now.. */ |
| 153 | |
| 154 | #ifdef CONFIG_SMP |
Al Viro | 9c8e7f5 | 2006-10-07 16:29:18 +0100 | [diff] [blame] | 155 | smp_local_timer_interrupt(); |
Hirokazu Takata | 2757a71 | 2005-08-01 21:11:35 -0700 | [diff] [blame] | 156 | smp_send_timer(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | return IRQ_HANDLED; |
| 160 | } |
| 161 | |
Adrian Bunk | 81e4807 | 2008-09-24 15:01:47 +0900 | [diff] [blame] | 162 | static struct irqaction irq0 = { |
Thomas Gleixner | 977676a | 2007-10-16 01:26:36 -0700 | [diff] [blame] | 163 | .handler = timer_interrupt, |
| 164 | .flags = IRQF_DISABLED, |
Thomas Gleixner | 977676a | 2007-10-16 01:26:36 -0700 | [diff] [blame] | 165 | .name = "MFT2", |
| 166 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
| 168 | void __init time_init(void) |
| 169 | { |
| 170 | unsigned int epoch, year, mon, day, hour, min, sec; |
| 171 | |
| 172 | sec = min = hour = day = mon = year = 0; |
| 173 | epoch = 0; |
| 174 | |
| 175 | year = 23; |
| 176 | mon = 4; |
| 177 | day = 17; |
| 178 | |
| 179 | /* Attempt to guess the epoch. This is the same heuristic as in rtc.c |
| 180 | so no stupid things will happen to timekeeping. Who knows, maybe |
| 181 | Ultrix also uses 1952 as epoch ... */ |
| 182 | if (year > 10 && year < 44) |
| 183 | epoch = 1980; |
| 184 | else if (year < 96) |
| 185 | epoch = 1952; |
| 186 | year += epoch; |
| 187 | |
| 188 | xtime.tv_sec = mktime(year, mon, day, hour, min, sec); |
| 189 | xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ); |
| 190 | set_normalized_timespec(&wall_to_monotonic, |
| 191 | -xtime.tv_sec, -xtime.tv_nsec); |
| 192 | |
| 193 | #if defined(CONFIG_CHIP_M32102) || defined(CONFIG_CHIP_XNUX2) \ |
| 194 | || defined(CONFIG_CHIP_VDEC2) || defined(CONFIG_CHIP_M32700) \ |
Hirokazu Takata | 9287d95 | 2006-01-06 00:18:41 -0800 | [diff] [blame] | 195 | || defined(CONFIG_CHIP_OPSP) || defined(CONFIG_CHIP_M32104) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
| 197 | /* M32102 MFT setup */ |
| 198 | setup_irq(M32R_IRQ_MFT2, &irq0); |
| 199 | { |
| 200 | unsigned long bus_clock; |
| 201 | unsigned short divide; |
| 202 | |
| 203 | bus_clock = boot_cpu_data.bus_clock; |
| 204 | divide = boot_cpu_data.timer_divide; |
| 205 | latch = (bus_clock/divide + HZ / 2) / HZ; |
| 206 | |
| 207 | printk("Timer start : latch = %ld\n", latch); |
| 208 | |
| 209 | outl((M32R_MFTMOD_CC_MASK | M32R_MFTMOD_TCCR \ |
| 210 | |M32R_MFTMOD_CSSEL011), M32R_MFT2MOD_PORTL); |
| 211 | outl(latch, M32R_MFT2RLD_PORTL); |
| 212 | outl(latch, M32R_MFT2CUT_PORTL); |
| 213 | outl(0, M32R_MFT2CMPRLD_PORTL); |
| 214 | outl((M32R_MFTCR_MFT2MSK|M32R_MFTCR_MFT2EN), M32R_MFTCR_PORTL); |
| 215 | } |
| 216 | |
| 217 | #elif defined(CONFIG_CHIP_M32310) |
| 218 | #warning time_init not implemented |
| 219 | #else |
| 220 | #error no chip configuration |
| 221 | #endif |
| 222 | } |