1 /* 2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 3 * Licensed under the GPL 4 */ 5 6 #include <linux/clockchips.h> 7 #include <linux/init.h> 8 #include <linux/interrupt.h> 9 #include <linux/jiffies.h> 10 #include <linux/threads.h> 11 #include <asm/irq.h> 12 #include <asm/param.h> 13 #include <kern_util.h> 14 #include <os.h> 15 16 void timer_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs) 17 { 18 unsigned long flags; 19 20 local_irq_save(flags); 21 do_IRQ(TIMER_IRQ, regs); 22 local_irq_restore(flags); 23 } 24 25 static int itimer_shutdown(struct clock_event_device *evt) 26 { 27 disable_timer(); 28 return 0; 29 } 30 31 static int itimer_set_periodic(struct clock_event_device *evt) 32 { 33 set_interval(); 34 return 0; 35 } 36 37 static int itimer_next_event(unsigned long delta, 38 struct clock_event_device *evt) 39 { 40 return timer_one_shot(delta + 1); 41 } 42 43 static struct clock_event_device itimer_clockevent = { 44 .name = "itimer", 45 .rating = 250, 46 .cpumask = cpu_all_mask, 47 .features = CLOCK_EVT_FEAT_PERIODIC | 48 CLOCK_EVT_FEAT_ONESHOT, 49 .set_state_shutdown = itimer_shutdown, 50 .set_state_periodic = itimer_set_periodic, 51 .set_state_oneshot = itimer_shutdown, 52 .set_next_event = itimer_next_event, 53 .shift = 32, 54 .irq = 0, 55 }; 56 57 static irqreturn_t um_timer(int irq, void *dev) 58 { 59 (*itimer_clockevent.event_handler)(&itimer_clockevent); 60 61 return IRQ_HANDLED; 62 } 63 64 static cycle_t itimer_read(struct clocksource *cs) 65 { 66 return os_nsecs() / 1000; 67 } 68 69 static struct clocksource itimer_clocksource = { 70 .name = "itimer", 71 .rating = 300, 72 .read = itimer_read, 73 .mask = CLOCKSOURCE_MASK(64), 74 .flags = CLOCK_SOURCE_IS_CONTINUOUS, 75 }; 76 77 static void __init setup_itimer(void) 78 { 79 int err; 80 81 err = request_irq(TIMER_IRQ, um_timer, 0, "timer", NULL); 82 if (err != 0) 83 printk(KERN_ERR "register_timer : request_irq failed - " 84 "errno = %d\n", -err); 85 86 itimer_clockevent.mult = div_sc(HZ, NSEC_PER_SEC, 32); 87 itimer_clockevent.max_delta_ns = 88 clockevent_delta2ns(60 * HZ, &itimer_clockevent); 89 itimer_clockevent.min_delta_ns = 90 clockevent_delta2ns(1, &itimer_clockevent); 91 err = clocksource_register_hz(&itimer_clocksource, USEC_PER_SEC); 92 if (err) { 93 printk(KERN_ERR "clocksource_register_hz returned %d\n", err); 94 return; 95 } 96 clockevents_register_device(&itimer_clockevent); 97 } 98 99 void read_persistent_clock(struct timespec *ts) 100 { 101 long long nsecs = os_nsecs(); 102 103 set_normalized_timespec(ts, nsecs / NSEC_PER_SEC, 104 nsecs % NSEC_PER_SEC); 105 } 106 107 void __init time_init(void) 108 { 109 timer_init(); 110 late_time_init = setup_itimer; 111 } 112