.. | .. |
---|
16 | 16 | #include <linux/init.h> |
---|
17 | 17 | #include <linux/rtc.h> |
---|
18 | 18 | #include <linux/bcd.h> |
---|
| 19 | +#include <linux/clocksource.h> |
---|
19 | 20 | #include <linux/delay.h> |
---|
20 | 21 | #include <linux/export.h> |
---|
21 | 22 | |
---|
.. | .. |
---|
24 | 25 | DEFINE_SPINLOCK(rtc_lock); |
---|
25 | 26 | EXPORT_SYMBOL_GPL(rtc_lock); |
---|
26 | 27 | |
---|
| 28 | +static u64 atari_read_clk(struct clocksource *cs); |
---|
| 29 | + |
---|
| 30 | +static struct clocksource atari_clk = { |
---|
| 31 | + .name = "mfp", |
---|
| 32 | + .rating = 100, |
---|
| 33 | + .read = atari_read_clk, |
---|
| 34 | + .mask = CLOCKSOURCE_MASK(32), |
---|
| 35 | + .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
---|
| 36 | +}; |
---|
| 37 | + |
---|
| 38 | +static u32 clk_total; |
---|
| 39 | +static u8 last_timer_count; |
---|
| 40 | + |
---|
27 | 41 | static irqreturn_t mfp_timer_c_handler(int irq, void *dev_id) |
---|
28 | 42 | { |
---|
29 | 43 | irq_handler_t timer_routine = dev_id; |
---|
30 | 44 | unsigned long flags; |
---|
31 | 45 | |
---|
32 | 46 | local_irq_save(flags); |
---|
| 47 | + do { |
---|
| 48 | + last_timer_count = st_mfp.tim_dt_c; |
---|
| 49 | + } while (last_timer_count == 1); |
---|
| 50 | + clk_total += INT_TICKS; |
---|
33 | 51 | timer_routine(0, NULL); |
---|
34 | 52 | local_irq_restore(flags); |
---|
35 | 53 | |
---|
.. | .. |
---|
44 | 62 | /* start timer C, div = 1:100 */ |
---|
45 | 63 | st_mfp.tim_ct_cd = (st_mfp.tim_ct_cd & 15) | 0x60; |
---|
46 | 64 | /* install interrupt service routine for MFP Timer C */ |
---|
47 | | - if (request_irq(IRQ_MFP_TIMC, mfp_timer_c_handler, 0, "timer", |
---|
| 65 | + if (request_irq(IRQ_MFP_TIMC, mfp_timer_c_handler, IRQF_TIMER, "timer", |
---|
48 | 66 | timer_routine)) |
---|
49 | 67 | pr_err("Couldn't register timer interrupt\n"); |
---|
| 68 | + |
---|
| 69 | + clocksource_register_hz(&atari_clk, INT_CLK); |
---|
50 | 70 | } |
---|
51 | 71 | |
---|
52 | 72 | /* ++andreas: gettimeoffset fixed to check for pending interrupt */ |
---|
53 | 73 | |
---|
54 | | -#define TICK_SIZE 10000 |
---|
55 | | - |
---|
56 | | -/* This is always executed with interrupts disabled. */ |
---|
57 | | -u32 atari_gettimeoffset(void) |
---|
| 74 | +static u64 atari_read_clk(struct clocksource *cs) |
---|
58 | 75 | { |
---|
59 | | - u32 ticks, offset = 0; |
---|
| 76 | + unsigned long flags; |
---|
| 77 | + u8 count; |
---|
| 78 | + u32 ticks; |
---|
60 | 79 | |
---|
61 | | - /* read MFP timer C current value */ |
---|
62 | | - ticks = st_mfp.tim_dt_c; |
---|
63 | | - /* The probability of underflow is less than 2% */ |
---|
64 | | - if (ticks > INT_TICKS - INT_TICKS / 50) |
---|
65 | | - /* Check for pending timer interrupt */ |
---|
66 | | - if (st_mfp.int_pn_b & (1 << 5)) |
---|
67 | | - offset = TICK_SIZE; |
---|
| 80 | + local_irq_save(flags); |
---|
| 81 | + /* Ensure that the count is monotonically decreasing, even though |
---|
| 82 | + * the result may briefly stop changing after counter wrap-around. |
---|
| 83 | + */ |
---|
| 84 | + count = min(st_mfp.tim_dt_c, last_timer_count); |
---|
| 85 | + last_timer_count = count; |
---|
68 | 86 | |
---|
69 | | - ticks = INT_TICKS - ticks; |
---|
70 | | - ticks = ticks * 10000L / INT_TICKS; |
---|
| 87 | + ticks = INT_TICKS - count; |
---|
| 88 | + ticks += clk_total; |
---|
| 89 | + local_irq_restore(flags); |
---|
71 | 90 | |
---|
72 | | - return (ticks + offset) * 1000; |
---|
| 91 | + return ticks; |
---|
73 | 92 | } |
---|
74 | 93 | |
---|
75 | 94 | |
---|