Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 1 | /* |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 2 | * linux/arch/cris/arch-v32/kernel/time.c |
| 3 | * |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 4 | * Copyright (C) 2003-2007 Axis Communications AB |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 5 | * |
| 6 | */ |
| 7 | |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 8 | #include <linux/timex.h> |
| 9 | #include <linux/time.h> |
| 10 | #include <linux/jiffies.h> |
| 11 | #include <linux/interrupt.h> |
| 12 | #include <linux/swap.h> |
| 13 | #include <linux/sched.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/threads.h> |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 16 | #include <linux/cpufreq.h> |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 17 | #include <asm/types.h> |
| 18 | #include <asm/signal.h> |
| 19 | #include <asm/io.h> |
| 20 | #include <asm/delay.h> |
| 21 | #include <asm/rtc.h> |
| 22 | #include <asm/irq.h> |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 23 | #include <asm/irq_regs.h> |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 24 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 25 | #include <hwregs/reg_map.h> |
| 26 | #include <hwregs/reg_rdwr.h> |
| 27 | #include <hwregs/timer_defs.h> |
| 28 | #include <hwregs/intr_vect_defs.h> |
| 29 | #ifdef CONFIG_CRIS_MACH_ARTPEC3 |
| 30 | #include <hwregs/clkgen_defs.h> |
| 31 | #endif |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 32 | |
| 33 | /* Watchdog defines */ |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 34 | #define ETRAX_WD_KEY_MASK 0x7F /* key is 7 bit */ |
| 35 | #define ETRAX_WD_HZ 763 /* watchdog counts at 763 Hz */ |
| 36 | /* Number of 763 counts before watchdog bites */ |
| 37 | #define ETRAX_WD_CNT ((2*ETRAX_WD_HZ)/HZ + 1) |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 38 | |
| 39 | unsigned long timer_regs[NR_CPUS] = |
| 40 | { |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 41 | regi_timer0, |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 42 | #ifdef CONFIG_SMP |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 43 | regi_timer2 |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 44 | #endif |
| 45 | }; |
| 46 | |
| 47 | extern void update_xtime_from_cmos(void); |
| 48 | extern int set_rtc_mmss(unsigned long nowtime); |
| 49 | extern int setup_irq(int, struct irqaction *); |
| 50 | extern int have_rtc; |
| 51 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 52 | #ifdef CONFIG_CPU_FREQ |
| 53 | static int |
| 54 | cris_time_freq_notifier(struct notifier_block *nb, unsigned long val, |
| 55 | void *data); |
| 56 | |
| 57 | static struct notifier_block cris_time_freq_notifier_block = { |
| 58 | .notifier_call = cris_time_freq_notifier, |
| 59 | }; |
| 60 | #endif |
| 61 | |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 62 | unsigned long get_ns_in_jiffie(void) |
| 63 | { |
| 64 | reg_timer_r_tmr0_data data; |
| 65 | unsigned long ns; |
| 66 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 67 | data = REG_RD(timer, regi_timer0, r_tmr0_data); |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 68 | ns = (TIMER0_DIV - data) * 10; |
| 69 | return ns; |
| 70 | } |
| 71 | |
| 72 | unsigned long do_slow_gettimeoffset(void) |
| 73 | { |
| 74 | unsigned long count; |
| 75 | unsigned long usec_count = 0; |
| 76 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 77 | /* For the first call after boot */ |
| 78 | static unsigned long count_p = TIMER0_DIV; |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 79 | static unsigned long jiffies_p = 0; |
| 80 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 81 | /* Cache volatile jiffies temporarily; we have IRQs turned off. */ |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 82 | unsigned long jiffies_t; |
| 83 | |
| 84 | /* The timer interrupt comes from Etrax timer 0. In order to get |
| 85 | * better precision, we check the current value. It might have |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 86 | * underflowed already though. */ |
| 87 | count = REG_RD(timer, regi_timer0, r_tmr0_data); |
| 88 | jiffies_t = jiffies; |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 89 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 90 | /* Avoiding timer inconsistencies (they are rare, but they happen) |
| 91 | * There is one problem that must be avoided here: |
| 92 | * 1. the timer counter underflows |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 93 | */ |
| 94 | if( jiffies_t == jiffies_p ) { |
| 95 | if( count > count_p ) { |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 96 | /* Timer wrapped, use new count and prescale. |
| 97 | * Increase the time corresponding to one jiffy. |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 98 | */ |
| 99 | usec_count = 1000000/HZ; |
| 100 | } |
| 101 | } else |
| 102 | jiffies_p = jiffies_t; |
| 103 | count_p = count; |
| 104 | /* Convert timer value to usec */ |
| 105 | /* 100 MHz timer, divide by 100 to get usec */ |
| 106 | usec_count += (TIMER0_DIV - count) / 100; |
| 107 | return usec_count; |
| 108 | } |
| 109 | |
| 110 | /* From timer MDS describing the hardware watchdog: |
| 111 | * 4.3.1 Watchdog Operation |
| 112 | * The watchdog timer is an 8-bit timer with a configurable start value. |
Simon Arlott | 49b4ff3 | 2007-10-20 01:08:50 +0200 | [diff] [blame] | 113 | * Once started the watchdog counts downwards with a frequency of 763 Hz |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 114 | * (100/131072 MHz). When the watchdog counts down to 1, it generates an |
| 115 | * NMI (Non Maskable Interrupt), and when it counts down to 0, it resets the |
| 116 | * chip. |
| 117 | */ |
| 118 | /* This gives us 1.3 ms to do something useful when the NMI comes */ |
| 119 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 120 | /* Right now, starting the watchdog is the same as resetting it */ |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 121 | #define start_watchdog reset_watchdog |
| 122 | |
| 123 | #if defined(CONFIG_ETRAX_WATCHDOG) |
| 124 | static short int watchdog_key = 42; /* arbitrary 7 bit number */ |
| 125 | #endif |
| 126 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 127 | /* Number of pages to consider "out of memory". It is normal that the memory |
| 128 | * is used though, so set this really low. */ |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 129 | #define WATCHDOG_MIN_FREE_PAGES 8 |
| 130 | |
| 131 | void |
| 132 | reset_watchdog(void) |
| 133 | { |
| 134 | #if defined(CONFIG_ETRAX_WATCHDOG) |
| 135 | reg_timer_rw_wd_ctrl wd_ctrl = { 0 }; |
| 136 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 137 | /* Only keep watchdog happy as long as we have memory left! */ |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 138 | if(nr_free_pages() > WATCHDOG_MIN_FREE_PAGES) { |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 139 | /* Reset the watchdog with the inverse of the old key */ |
| 140 | /* Invert key, which is 7 bits */ |
| 141 | watchdog_key ^= ETRAX_WD_KEY_MASK; |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 142 | wd_ctrl.cnt = ETRAX_WD_CNT; |
| 143 | wd_ctrl.cmd = regk_timer_start; |
| 144 | wd_ctrl.key = watchdog_key; |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 145 | REG_WR(timer, regi_timer0, rw_wd_ctrl, wd_ctrl); |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 146 | } |
| 147 | #endif |
| 148 | } |
| 149 | |
| 150 | /* stop the watchdog - we still need the correct key */ |
| 151 | |
| 152 | void |
| 153 | stop_watchdog(void) |
| 154 | { |
| 155 | #if defined(CONFIG_ETRAX_WATCHDOG) |
| 156 | reg_timer_rw_wd_ctrl wd_ctrl = { 0 }; |
| 157 | watchdog_key ^= ETRAX_WD_KEY_MASK; /* invert key, which is 7 bits */ |
| 158 | wd_ctrl.cnt = ETRAX_WD_CNT; |
| 159 | wd_ctrl.cmd = regk_timer_stop; |
| 160 | wd_ctrl.key = watchdog_key; |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 161 | REG_WR(timer, regi_timer0, rw_wd_ctrl, wd_ctrl); |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 162 | #endif |
| 163 | } |
| 164 | |
| 165 | extern void show_registers(struct pt_regs *regs); |
| 166 | |
| 167 | void |
| 168 | handle_watchdog_bite(struct pt_regs* regs) |
| 169 | { |
| 170 | #if defined(CONFIG_ETRAX_WATCHDOG) |
| 171 | extern int cause_of_death; |
| 172 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 173 | oops_in_progress = 1; |
| 174 | printk(KERN_WARNING "Watchdog bite\n"); |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 175 | |
| 176 | /* Check if forced restart or unexpected watchdog */ |
| 177 | if (cause_of_death == 0xbedead) { |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 178 | #ifdef CONFIG_CRIS_MACH_ARTPEC3 |
| 179 | /* There is a bug in Artpec-3 (voodoo TR 78) that requires |
| 180 | * us to go to lower frequency for the reset to be reliable |
| 181 | */ |
| 182 | reg_clkgen_rw_clk_ctrl ctrl = |
| 183 | REG_RD(clkgen, regi_clkgen, rw_clk_ctrl); |
| 184 | ctrl.pll = 0; |
| 185 | REG_WR(clkgen, regi_clkgen, rw_clk_ctrl, ctrl); |
| 186 | #endif |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 187 | while(1); |
| 188 | } |
| 189 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 190 | /* Unexpected watchdog, stop the watchdog and dump registers. */ |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 191 | stop_watchdog(); |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 192 | printk(KERN_WARNING "Oops: bitten by watchdog\n"); |
| 193 | show_registers(regs); |
| 194 | oops_in_progress = 0; |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 195 | #ifndef CONFIG_ETRAX_WATCHDOG_NICE_DOGGY |
| 196 | reset_watchdog(); |
| 197 | #endif |
| 198 | while(1) /* nothing */; |
| 199 | #endif |
| 200 | } |
| 201 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 202 | /* Last time the cmos clock got updated. */ |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 203 | static long last_rtc_update = 0; |
| 204 | |
| 205 | /* |
| 206 | * timer_interrupt() needs to keep up the real-time clock, |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 207 | * as well as call the "do_timer()" routine every clocktick. |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 208 | */ |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 209 | extern void cris_do_profile(struct pt_regs *regs); |
| 210 | |
| 211 | static inline irqreturn_t |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 212 | timer_interrupt(int irq, void *dev_id) |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 213 | { |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 214 | struct pt_regs *regs = get_irq_regs(); |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 215 | int cpu = smp_processor_id(); |
| 216 | reg_timer_r_masked_intr masked_intr; |
| 217 | reg_timer_rw_ack_intr ack_intr = { 0 }; |
| 218 | |
| 219 | /* Check if the timer interrupt is for us (a tmr0 int) */ |
| 220 | masked_intr = REG_RD(timer, timer_regs[cpu], r_masked_intr); |
| 221 | if (!masked_intr.tmr0) |
| 222 | return IRQ_NONE; |
| 223 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 224 | /* Acknowledge the timer irq. */ |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 225 | ack_intr.tmr0 = 1; |
| 226 | REG_WR(timer, timer_regs[cpu], rw_ack_intr, ack_intr); |
| 227 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 228 | /* Reset watchdog otherwise it resets us! */ |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 229 | reset_watchdog(); |
| 230 | |
| 231 | /* Update statistics. */ |
| 232 | update_process_times(user_mode(regs)); |
| 233 | |
| 234 | cris_do_profile(regs); /* Save profiling information */ |
| 235 | |
| 236 | /* The master CPU is responsible for the time keeping. */ |
| 237 | if (cpu != 0) |
| 238 | return IRQ_HANDLED; |
| 239 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 240 | /* Call the real timer interrupt handler */ |
Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 241 | do_timer(1); |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 242 | |
| 243 | /* |
| 244 | * If we have an externally synchronized Linux clock, then update |
| 245 | * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be |
| 246 | * called as close as possible to 500 ms before the new second starts. |
| 247 | * |
| 248 | * The division here is not time critical since it will run once in |
| 249 | * 11 minutes |
| 250 | */ |
| 251 | if ((time_status & STA_UNSYNC) == 0 && |
| 252 | xtime.tv_sec > last_rtc_update + 660 && |
| 253 | (xtime.tv_nsec / 1000) >= 500000 - (tick_nsec / 1000) / 2 && |
| 254 | (xtime.tv_nsec / 1000) <= 500000 + (tick_nsec / 1000) / 2) { |
| 255 | if (set_rtc_mmss(xtime.tv_sec) == 0) |
| 256 | last_rtc_update = xtime.tv_sec; |
| 257 | else |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 258 | /* Do it again in 60 s */ |
| 259 | last_rtc_update = xtime.tv_sec - 600; |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 260 | } |
| 261 | return IRQ_HANDLED; |
| 262 | } |
| 263 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 264 | /* Timer is IRQF_SHARED so drivers can add stuff to the timer irq chain. |
| 265 | * It needs to be IRQF_DISABLED to make the jiffies update work properly. |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 266 | */ |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 267 | static struct irqaction irq_timer = { |
| 268 | .handler = timer_interrupt, |
Thomas Gleixner | aa7135f | 2006-07-01 19:29:14 -0700 | [diff] [blame] | 269 | .flags = IRQF_SHARED | IRQF_DISABLED, |
| 270 | .mask = CPU_MASK_NONE, |
| 271 | .name = "timer" |
| 272 | }; |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 273 | |
| 274 | void __init |
| 275 | cris_timer_init(void) |
| 276 | { |
| 277 | int cpu = smp_processor_id(); |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 278 | reg_timer_rw_tmr0_ctrl tmr0_ctrl = { 0 }; |
| 279 | reg_timer_rw_tmr0_div tmr0_div = TIMER0_DIV; |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 280 | reg_timer_rw_intr_mask timer_intr_mask; |
| 281 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 282 | /* Setup the etrax timers. |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 283 | * Base frequency is 100MHz, divider 1000000 -> 100 HZ |
| 284 | * We use timer0, so timer1 is free. |
| 285 | * The trig timer is used by the fasttimer API if enabled. |
| 286 | */ |
| 287 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 288 | tmr0_ctrl.op = regk_timer_ld; |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 289 | tmr0_ctrl.freq = regk_timer_f100; |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 290 | REG_WR(timer, timer_regs[cpu], rw_tmr0_div, tmr0_div); |
| 291 | REG_WR(timer, timer_regs[cpu], rw_tmr0_ctrl, tmr0_ctrl); /* Load */ |
| 292 | tmr0_ctrl.op = regk_timer_run; |
| 293 | REG_WR(timer, timer_regs[cpu], rw_tmr0_ctrl, tmr0_ctrl); /* Start */ |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 294 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 295 | /* Enable the timer irq. */ |
| 296 | timer_intr_mask = REG_RD(timer, timer_regs[cpu], rw_intr_mask); |
| 297 | timer_intr_mask.tmr0 = 1; |
| 298 | REG_WR(timer, timer_regs[cpu], rw_intr_mask, timer_intr_mask); |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | void __init |
| 302 | time_init(void) |
| 303 | { |
| 304 | reg_intr_vect_rw_mask intr_mask; |
| 305 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 306 | /* Probe for the RTC and read it if it exists. |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 307 | * Before the RTC can be probed the loops_per_usec variable needs |
| 308 | * to be initialized to make usleep work. A better value for |
| 309 | * loops_per_usec is calculated by the kernel later once the |
| 310 | * clock has started. |
| 311 | */ |
| 312 | loops_per_usec = 50; |
| 313 | |
| 314 | if(RTC_INIT() < 0) { |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 315 | /* No RTC, start at 1980 */ |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 316 | xtime.tv_sec = 0; |
| 317 | xtime.tv_nsec = 0; |
| 318 | have_rtc = 0; |
| 319 | } else { |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 320 | /* Get the current time */ |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 321 | have_rtc = 1; |
| 322 | update_xtime_from_cmos(); |
| 323 | } |
| 324 | |
| 325 | /* |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 326 | * Initialize wall_to_monotonic such that adding it to |
| 327 | * xtime will yield zero, the tv_nsec field must be normalized |
| 328 | * (i.e., 0 <= nsec < NSEC_PER_SEC). |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 329 | */ |
| 330 | set_normalized_timespec(&wall_to_monotonic, -xtime.tv_sec, -xtime.tv_nsec); |
| 331 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 332 | /* Start CPU local timer. */ |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 333 | cris_timer_init(); |
| 334 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 335 | /* Enable the timer irq in global config. */ |
| 336 | intr_mask = REG_RD_VECT(intr_vect, regi_irq, rw_mask, 1); |
| 337 | intr_mask.timer0 = 1; |
| 338 | REG_WR_VECT(intr_vect, regi_irq, rw_mask, 1, intr_mask); |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 339 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 340 | /* Now actually register the timer irq handler that calls |
| 341 | * timer_interrupt(). */ |
| 342 | setup_irq(TIMER0_INTR_VECT, &irq_timer); |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 343 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 344 | /* Enable watchdog if we should use one. */ |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 345 | |
| 346 | #if defined(CONFIG_ETRAX_WATCHDOG) |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 347 | printk(KERN_INFO "Enabling watchdog...\n"); |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 348 | start_watchdog(); |
| 349 | |
| 350 | /* If we use the hardware watchdog, we want to trap it as an NMI |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 351 | * and dump registers before it resets us. For this to happen, we |
| 352 | * must set the "m" NMI enable flag (which once set, is unset only |
| 353 | * when an NMI is taken). */ |
| 354 | { |
| 355 | unsigned long flags; |
| 356 | local_save_flags(flags); |
| 357 | flags |= (1<<30); /* NMI M flag is at bit 30 */ |
| 358 | local_irq_restore(flags); |
| 359 | } |
| 360 | #endif |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 361 | |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 362 | #ifdef CONFIG_CPU_FREQ |
| 363 | cpufreq_register_notifier(&cris_time_freq_notifier_block, |
| 364 | CPUFREQ_TRANSITION_NOTIFIER); |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 365 | #endif |
| 366 | } |
Jesper Nilsson | fbdb5f8 | 2007-12-04 17:25:45 +0100 | [diff] [blame^] | 367 | |
| 368 | #ifdef CONFIG_CPU_FREQ |
| 369 | static int |
| 370 | cris_time_freq_notifier(struct notifier_block *nb, unsigned long val, |
| 371 | void *data) |
| 372 | { |
| 373 | struct cpufreq_freqs *freqs = data; |
| 374 | if (val == CPUFREQ_POSTCHANGE) { |
| 375 | reg_timer_r_tmr0_data data; |
| 376 | reg_timer_rw_tmr0_div div = (freqs->new * 500) / HZ; |
| 377 | do { |
| 378 | data = REG_RD(timer, timer_regs[freqs->cpu], |
| 379 | r_tmr0_data); |
| 380 | } while (data > 20); |
| 381 | REG_WR(timer, timer_regs[freqs->cpu], rw_tmr0_div, div); |
| 382 | } |
| 383 | return 0; |
| 384 | } |
| 385 | #endif |