Path: blob/master/arch/cris/arch-v10/kernel/time.c
15125 views
/*1* linux/arch/cris/arch-v10/kernel/time.c2*3* Copyright (C) 1991, 1992, 1995 Linus Torvalds4* Copyright (C) 1999-2002 Axis Communications AB5*6*/78#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/mm.h>16#include <arch/svinto.h>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_regs.h>2324/* define this if you need to use print_timestamp */25/* it will make jiffies at 96 hz instead of 100 hz though */26#undef USE_CASCADE_TIMERS2728extern int set_rtc_mmss(unsigned long nowtime);29extern int have_rtc;3031unsigned long get_ns_in_jiffie(void)32{33unsigned char timer_count, t1;34unsigned short presc_count;35unsigned long ns;36unsigned long flags;3738local_irq_save(flags);39timer_count = *R_TIMER0_DATA;40presc_count = *R_TIM_PRESC_STATUS;41/* presc_count might be wrapped */42t1 = *R_TIMER0_DATA;4344if (timer_count != t1){45/* it wrapped, read prescaler again... */46presc_count = *R_TIM_PRESC_STATUS;47timer_count = t1;48}49local_irq_restore(flags);50if (presc_count >= PRESCALE_VALUE/2 ){51presc_count = PRESCALE_VALUE - presc_count + PRESCALE_VALUE/2;52} else {53presc_count = PRESCALE_VALUE - presc_count - PRESCALE_VALUE/2;54}5556ns = ( (TIMER0_DIV - timer_count) * ((1000000000/HZ)/TIMER0_DIV )) +57( (presc_count) * (1000000000/PRESCALE_FREQ));58return ns;59}6061unsigned long do_slow_gettimeoffset(void)62{63unsigned long count;6465/* The timer interrupt comes from Etrax timer 0. In order to get66* better precision, we check the current value. It might have67* underflowed already though.68*/69count = *R_TIMER0_DATA;7071/* Convert timer value to usec */72return (TIMER0_DIV - count) * ((NSEC_PER_SEC/1000)/HZ)/TIMER0_DIV;73}7475/* Excerpt from the Etrax100 HSDD about the built-in watchdog:76*77* 3.10.4 Watchdog timer7879* When the watchdog timer is started, it generates an NMI if the watchdog80* isn't restarted or stopped within 0.1 s. If it still isn't restarted or81* stopped after an additional 3.3 ms, the watchdog resets the chip.82* The watchdog timer is stopped after reset. The watchdog timer is controlled83* by the R_WATCHDOG register. The R_WATCHDOG register contains an enable bit84* and a 3-bit key value. The effect of writing to the R_WATCHDOG register is85* described in the table below:86*87* Watchdog Value written:88* state: To enable: To key: Operation:89* -------- ---------- ------- ----------90* stopped 0 X No effect.91* stopped 1 key_val Start watchdog with key = key_val.92* started 0 ~key Stop watchdog93* started 1 ~key Restart watchdog with key = ~key.94* started X new_key_val Change key to new_key_val.95*96* Note: '~' is the bitwise NOT operator.97*98*/99100/* right now, starting the watchdog is the same as resetting it */101#define start_watchdog reset_watchdog102103#if defined(CONFIG_ETRAX_WATCHDOG) && !defined(CONFIG_SVINTO_SIM)104static int watchdog_key = 0; /* arbitrary number */105#endif106107/* number of pages to consider "out of memory". it is normal that the memory108* is used though, so put this really low.109*/110111#define WATCHDOG_MIN_FREE_PAGES 8112113void114reset_watchdog(void)115{116#if defined(CONFIG_ETRAX_WATCHDOG) && !defined(CONFIG_SVINTO_SIM)117/* only keep watchdog happy as long as we have memory left! */118if(nr_free_pages() > WATCHDOG_MIN_FREE_PAGES) {119/* reset the watchdog with the inverse of the old key */120watchdog_key ^= 0x7; /* invert key, which is 3 bits */121*R_WATCHDOG = IO_FIELD(R_WATCHDOG, key, watchdog_key) |122IO_STATE(R_WATCHDOG, enable, start);123}124#endif125}126127/* stop the watchdog - we still need the correct key */128129void130stop_watchdog(void)131{132#if defined(CONFIG_ETRAX_WATCHDOG) && !defined(CONFIG_SVINTO_SIM)133watchdog_key ^= 0x7; /* invert key, which is 3 bits */134*R_WATCHDOG = IO_FIELD(R_WATCHDOG, key, watchdog_key) |135IO_STATE(R_WATCHDOG, enable, stop);136#endif137}138139140/*141* timer_interrupt() needs to keep up the real-time clock,142* as well as call the "xtime_update()" routine every clocktick143*/144145//static unsigned short myjiff; /* used by our debug routine print_timestamp */146147extern void cris_do_profile(struct pt_regs *regs);148149static inline irqreturn_t150timer_interrupt(int irq, void *dev_id)151{152struct pt_regs *regs = get_irq_regs();153/* acknowledge the timer irq */154155#ifdef USE_CASCADE_TIMERS156*R_TIMER_CTRL =157IO_FIELD( R_TIMER_CTRL, timerdiv1, 0) |158IO_FIELD( R_TIMER_CTRL, timerdiv0, 0) |159IO_STATE( R_TIMER_CTRL, i1, clr) |160IO_STATE( R_TIMER_CTRL, tm1, run) |161IO_STATE( R_TIMER_CTRL, clksel1, cascade0) |162IO_STATE( R_TIMER_CTRL, i0, clr) |163IO_STATE( R_TIMER_CTRL, tm0, run) |164IO_STATE( R_TIMER_CTRL, clksel0, c6250kHz);165#else166*R_TIMER_CTRL = r_timer_ctrl_shadow |167IO_STATE(R_TIMER_CTRL, i0, clr);168#endif169170/* reset watchdog otherwise it resets us! */171reset_watchdog();172173/* Update statistics. */174update_process_times(user_mode(regs));175176/* call the real timer interrupt handler */177178xtime_update(1);179180cris_do_profile(regs); /* Save profiling information */181return IRQ_HANDLED;182}183184/* timer is IRQF_SHARED so drivers can add stuff to the timer irq chain185* it needs to be IRQF_DISABLED to make the jiffies update work properly186*/187188static struct irqaction irq2 = {189.handler = timer_interrupt,190.flags = IRQF_SHARED | IRQF_DISABLED,191.name = "timer",192};193194void __init195time_init(void)196{197/* probe for the RTC and read it if it exists198* Before the RTC can be probed the loops_per_usec variable needs199* to be initialized to make usleep work. A better value for200* loops_per_usec is calculated by the kernel later once the201* clock has started.202*/203loops_per_usec = 50;204205if(RTC_INIT() < 0)206have_rtc = 0;207else208have_rtc = 1;209210/* Setup the etrax timers211* Base frequency is 25000 hz, divider 250 -> 100 HZ212* In normal mode, we use timer0, so timer1 is free. In cascade213* mode (which we sometimes use for debugging) both timers are used.214* Remember that linux/timex.h contains #defines that rely on the215* timer settings below (hz and divide factor) !!!216*/217218#ifdef USE_CASCADE_TIMERS219*R_TIMER_CTRL =220IO_FIELD( R_TIMER_CTRL, timerdiv1, 0) |221IO_FIELD( R_TIMER_CTRL, timerdiv0, 0) |222IO_STATE( R_TIMER_CTRL, i1, nop) |223IO_STATE( R_TIMER_CTRL, tm1, stop_ld) |224IO_STATE( R_TIMER_CTRL, clksel1, cascade0) |225IO_STATE( R_TIMER_CTRL, i0, nop) |226IO_STATE( R_TIMER_CTRL, tm0, stop_ld) |227IO_STATE( R_TIMER_CTRL, clksel0, c6250kHz);228229*R_TIMER_CTRL = r_timer_ctrl_shadow =230IO_FIELD( R_TIMER_CTRL, timerdiv1, 0) |231IO_FIELD( R_TIMER_CTRL, timerdiv0, 0) |232IO_STATE( R_TIMER_CTRL, i1, nop) |233IO_STATE( R_TIMER_CTRL, tm1, run) |234IO_STATE( R_TIMER_CTRL, clksel1, cascade0) |235IO_STATE( R_TIMER_CTRL, i0, nop) |236IO_STATE( R_TIMER_CTRL, tm0, run) |237IO_STATE( R_TIMER_CTRL, clksel0, c6250kHz);238#else239*R_TIMER_CTRL =240IO_FIELD(R_TIMER_CTRL, timerdiv1, 192) |241IO_FIELD(R_TIMER_CTRL, timerdiv0, TIMER0_DIV) |242IO_STATE(R_TIMER_CTRL, i1, nop) |243IO_STATE(R_TIMER_CTRL, tm1, stop_ld) |244IO_STATE(R_TIMER_CTRL, clksel1, c19k2Hz) |245IO_STATE(R_TIMER_CTRL, i0, nop) |246IO_STATE(R_TIMER_CTRL, tm0, stop_ld) |247IO_STATE(R_TIMER_CTRL, clksel0, flexible);248249*R_TIMER_CTRL = r_timer_ctrl_shadow =250IO_FIELD(R_TIMER_CTRL, timerdiv1, 192) |251IO_FIELD(R_TIMER_CTRL, timerdiv0, TIMER0_DIV) |252IO_STATE(R_TIMER_CTRL, i1, nop) |253IO_STATE(R_TIMER_CTRL, tm1, run) |254IO_STATE(R_TIMER_CTRL, clksel1, c19k2Hz) |255IO_STATE(R_TIMER_CTRL, i0, nop) |256IO_STATE(R_TIMER_CTRL, tm0, run) |257IO_STATE(R_TIMER_CTRL, clksel0, flexible);258259*R_TIMER_PRESCALE = PRESCALE_VALUE;260#endif261262*R_IRQ_MASK0_SET =263IO_STATE(R_IRQ_MASK0_SET, timer0, set); /* unmask the timer irq */264265/* now actually register the timer irq handler that calls timer_interrupt() */266267setup_irq(2, &irq2); /* irq 2 is the timer0 irq in etrax */268269/* enable watchdog if we should use one */270271#if defined(CONFIG_ETRAX_WATCHDOG) && !defined(CONFIG_SVINTO_SIM)272printk("Enabling watchdog...\n");273start_watchdog();274275/* If we use the hardware watchdog, we want to trap it as an NMI276and dump registers before it resets us. For this to happen, we277must set the "m" NMI enable flag (which once set, is unset only278when an NMI is taken).279280The same goes for the external NMI, but that doesn't have any281driver or infrastructure support yet. */282asm ("setf m");283284*R_IRQ_MASK0_SET =285IO_STATE(R_IRQ_MASK0_SET, watchdog_nmi, set);286*R_VECT_MASK_SET =287IO_STATE(R_VECT_MASK_SET, nmi, set);288#endif289}290291292