| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 1 | /* | 
| Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 2 | * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL | 
|  | 4 | */ | 
|  | 5 |  | 
| Jeff Dike | c5d4bb1 | 2008-02-04 22:31:14 -0800 | [diff] [blame] | 6 | #include <linux/clockchips.h> | 
| Adrian Bunk | 7d195a5 | 2008-04-29 00:59:18 -0700 | [diff] [blame] | 7 | #include <linux/init.h> | 
| Jeff Dike | c5d4bb1 | 2008-02-04 22:31:14 -0800 | [diff] [blame] | 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> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "kern_util.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include "os.h" | 
|  | 15 |  | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 16 | void timer_handler(int sig, struct uml_pt_regs *regs) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | { | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 18 | unsigned long flags; | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 19 |  | 
|  | 20 | local_irq_save(flags); | 
| Jeff Dike | d2753a6d | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 21 | do_IRQ(TIMER_IRQ, regs); | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 22 | local_irq_restore(flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | } | 
|  | 24 |  | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 25 | static void itimer_set_mode(enum clock_event_mode mode, | 
|  | 26 | struct clock_event_device *evt) | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 27 | { | 
| Jeff Dike | c5d4bb1 | 2008-02-04 22:31:14 -0800 | [diff] [blame] | 28 | switch (mode) { | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 29 | case CLOCK_EVT_MODE_PERIODIC: | 
|  | 30 | set_interval(); | 
|  | 31 | break; | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 32 |  | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 33 | case CLOCK_EVT_MODE_SHUTDOWN: | 
|  | 34 | case CLOCK_EVT_MODE_UNUSED: | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 35 | case CLOCK_EVT_MODE_ONESHOT: | 
| Jeff Dike | d2753a6d | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 36 | disable_timer(); | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 37 | break; | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 38 |  | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 39 | case CLOCK_EVT_MODE_RESUME: | 
|  | 40 | break; | 
|  | 41 | } | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 42 | } | 
|  | 43 |  | 
| Jeff Dike | d2753a6d | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 44 | static int itimer_next_event(unsigned long delta, | 
|  | 45 | struct clock_event_device *evt) | 
|  | 46 | { | 
|  | 47 | return timer_one_shot(delta + 1); | 
|  | 48 | } | 
|  | 49 |  | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 50 | static struct clock_event_device itimer_clockevent = { | 
|  | 51 | .name		= "itimer", | 
|  | 52 | .rating		= 250, | 
| Rusty Russell | 320ab2b | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 53 | .cpumask	= cpu_all_mask, | 
| Jeff Dike | d2753a6d | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 54 | .features	= CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT, | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 55 | .set_mode	= itimer_set_mode, | 
| Jeff Dike | d2753a6d | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 56 | .set_next_event = itimer_next_event, | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 57 | .shift		= 32, | 
|  | 58 | .irq		= 0, | 
|  | 59 | }; | 
|  | 60 |  | 
|  | 61 | static irqreturn_t um_timer(int irq, void *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 63 | (*itimer_clockevent.event_handler)(&itimer_clockevent); | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 64 |  | 
| Jeff Dike | 572e614 | 2006-06-30 01:55:56 -0700 | [diff] [blame] | 65 | return IRQ_HANDLED; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } | 
|  | 67 |  | 
| Magnus Damm | 8e19608 | 2009-04-21 12:24:00 -0700 | [diff] [blame] | 68 | static cycle_t itimer_read(struct clocksource *cs) | 
| Jeff Dike | 791a644 | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 69 | { | 
| Jeff Dike | cfd28f6 | 2008-05-12 14:01:53 -0700 | [diff] [blame] | 70 | return os_nsecs() / 1000; | 
| Jeff Dike | 791a644 | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 71 | } | 
|  | 72 |  | 
|  | 73 | static struct clocksource itimer_clocksource = { | 
|  | 74 | .name		= "itimer", | 
|  | 75 | .rating		= 300, | 
|  | 76 | .read		= itimer_read, | 
|  | 77 | .mask		= CLOCKSOURCE_MASK(64), | 
| Jeff Dike | cfd28f6 | 2008-05-12 14:01:53 -0700 | [diff] [blame] | 78 | .mult		= 1000, | 
| Jeff Dike | 791a644 | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 79 | .shift		= 0, | 
|  | 80 | .flags		= CLOCK_SOURCE_IS_CONTINUOUS, | 
|  | 81 | }; | 
|  | 82 |  | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 83 | static void __init setup_itimer(void) | 
| Jeff Dike | aceb343 | 2006-07-10 04:45:05 -0700 | [diff] [blame] | 84 | { | 
|  | 85 | int err; | 
|  | 86 |  | 
|  | 87 | err = request_irq(TIMER_IRQ, um_timer, IRQF_DISABLED, "timer", NULL); | 
| Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 88 | if (err != 0) | 
| Jeff Dike | 537ae94 | 2006-09-25 23:33:05 -0700 | [diff] [blame] | 89 | printk(KERN_ERR "register_timer : request_irq failed - " | 
| Jeff Dike | aceb343 | 2006-07-10 04:45:05 -0700 | [diff] [blame] | 90 | "errno = %d\n", -err); | 
|  | 91 |  | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 92 | itimer_clockevent.mult = div_sc(HZ, NSEC_PER_SEC, 32); | 
|  | 93 | itimer_clockevent.max_delta_ns = | 
|  | 94 | clockevent_delta2ns(60 * HZ, &itimer_clockevent); | 
|  | 95 | itimer_clockevent.min_delta_ns = | 
|  | 96 | clockevent_delta2ns(1, &itimer_clockevent); | 
| Jeff Dike | 791a644 | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 97 | err = clocksource_register(&itimer_clocksource); | 
|  | 98 | if (err) { | 
|  | 99 | printk(KERN_ERR "clocksource_register returned %d\n", err); | 
|  | 100 | return; | 
|  | 101 | } | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 102 | clockevents_register_device(&itimer_clockevent); | 
| Jeff Dike | aceb343 | 2006-07-10 04:45:05 -0700 | [diff] [blame] | 103 | } | 
|  | 104 |  | 
| John Stultz | 9f31f57 | 2010-07-13 17:56:24 -0700 | [diff] [blame] | 105 | void read_persistent_clock(struct timespec *ts) | 
|  | 106 | { | 
| Thomas Gleixner | b292307 | 2010-08-03 20:34:48 +0200 | [diff] [blame] | 107 | long long nsecs = os_nsecs(); | 
|  | 108 |  | 
| John Stultz | 9f31f57 | 2010-07-13 17:56:24 -0700 | [diff] [blame] | 109 | set_normalized_timespec(ts, nsecs / NSEC_PER_SEC, | 
|  | 110 | nsecs % NSEC_PER_SEC); | 
|  | 111 | } | 
|  | 112 |  | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 113 | void __init time_init(void) | 
| Jeff Dike | aceb343 | 2006-07-10 04:45:05 -0700 | [diff] [blame] | 114 | { | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 115 | timer_init(); | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 116 | late_time_init = setup_itimer; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |