Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/kernel/time-ts.c |
| 3 | * |
| 4 | * Based on arm clockevents implementation and old bfin time tick. |
| 5 | * |
| 6 | * Copyright(C) 2008, GeoTechnologies, Vitja Makarov |
| 7 | * |
| 8 | * This code is licenced under the GPL version 2. For details see |
| 9 | * kernel-base/COPYING. |
| 10 | */ |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/profile.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/time.h> |
Mike Frysinger | 764cb81 | 2008-04-24 05:07:29 +0800 | [diff] [blame^] | 15 | #include <linux/timex.h> |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 16 | #include <linux/irq.h> |
| 17 | #include <linux/clocksource.h> |
| 18 | #include <linux/clockchips.h> |
| 19 | |
| 20 | #include <asm/blackfin.h> |
| 21 | |
| 22 | #ifdef CONFIG_CYCLES_CLOCKSOURCE |
| 23 | |
| 24 | static unsigned long cyc2ns_scale; |
| 25 | #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */ |
| 26 | |
| 27 | static inline void set_cyc2ns_scale(unsigned long cpu_khz) |
| 28 | { |
| 29 | cyc2ns_scale = (1000000 << CYC2NS_SCALE_FACTOR) / cpu_khz; |
| 30 | } |
| 31 | |
| 32 | static inline unsigned long long cycles_2_ns(cycle_t cyc) |
| 33 | { |
| 34 | return (cyc * cyc2ns_scale) >> CYC2NS_SCALE_FACTOR; |
| 35 | } |
| 36 | |
| 37 | static cycle_t read_cycles(void) |
| 38 | { |
Mike Frysinger | 764cb81 | 2008-04-24 05:07:29 +0800 | [diff] [blame^] | 39 | return get_cycles(); |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | unsigned long long sched_clock(void) |
| 43 | { |
| 44 | return cycles_2_ns(read_cycles()); |
| 45 | } |
| 46 | |
| 47 | static struct clocksource clocksource_bfin = { |
| 48 | .name = "bfin_cycles", |
| 49 | .rating = 350, |
| 50 | .read = read_cycles, |
| 51 | .mask = CLOCKSOURCE_MASK(64), |
| 52 | .shift = 22, |
| 53 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 54 | }; |
| 55 | |
| 56 | static int __init bfin_clocksource_init(void) |
| 57 | { |
| 58 | set_cyc2ns_scale(get_cclk() / 1000); |
| 59 | |
| 60 | clocksource_bfin.mult = clocksource_hz2mult(get_cclk(), clocksource_bfin.shift); |
| 61 | |
| 62 | if (clocksource_register(&clocksource_bfin)) |
| 63 | panic("failed to register clocksource"); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | #else |
| 69 | # define bfin_clocksource_init() |
| 70 | #endif |
| 71 | |
| 72 | static int bfin_timer_set_next_event(unsigned long cycles, |
| 73 | struct clock_event_device *evt) |
| 74 | { |
| 75 | bfin_write_TCOUNT(cycles); |
| 76 | CSYNC(); |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static void bfin_timer_set_mode(enum clock_event_mode mode, |
| 81 | struct clock_event_device *evt) |
| 82 | { |
| 83 | switch (mode) { |
| 84 | case CLOCK_EVT_MODE_PERIODIC: { |
| 85 | unsigned long tcount = ((get_cclk() / (HZ * 1)) - 1); |
| 86 | bfin_write_TCNTL(TMPWR); |
| 87 | CSYNC(); |
| 88 | bfin_write_TPERIOD(tcount); |
| 89 | bfin_write_TCOUNT(tcount); |
| 90 | bfin_write_TCNTL(TMPWR | TMREN | TAUTORLD); |
| 91 | CSYNC(); |
| 92 | break; |
| 93 | } |
| 94 | case CLOCK_EVT_MODE_ONESHOT: |
| 95 | bfin_write_TCOUNT(0); |
| 96 | bfin_write_TCNTL(TMPWR | TMREN); |
| 97 | CSYNC(); |
| 98 | break; |
| 99 | case CLOCK_EVT_MODE_UNUSED: |
| 100 | case CLOCK_EVT_MODE_SHUTDOWN: |
| 101 | bfin_write_TCNTL(0); |
| 102 | CSYNC(); |
| 103 | break; |
| 104 | case CLOCK_EVT_MODE_RESUME: |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static void __init bfin_timer_init(void) |
| 110 | { |
| 111 | /* power up the timer, but don't enable it just yet */ |
| 112 | bfin_write_TCNTL(TMPWR); |
| 113 | CSYNC(); |
| 114 | |
| 115 | /* |
| 116 | * the TSCALE prescaler counter. |
| 117 | */ |
| 118 | bfin_write_TSCALE(0); |
| 119 | bfin_write_TPERIOD(0); |
| 120 | bfin_write_TCOUNT(0); |
| 121 | |
| 122 | /* now enable the timer */ |
| 123 | CSYNC(); |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * timer_interrupt() needs to keep up the real-time clock, |
| 128 | * as well as call the "do_timer()" routine every clocktick |
| 129 | */ |
| 130 | #ifdef CONFIG_CORE_TIMER_IRQ_L1 |
| 131 | __attribute__((l1_text)) |
| 132 | #endif |
| 133 | irqreturn_t timer_interrupt(int irq, void *dev_id); |
| 134 | |
| 135 | static struct clock_event_device clockevent_bfin = { |
| 136 | .name = "bfin_core_timer", |
| 137 | .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT, |
| 138 | .shift = 32, |
| 139 | .cpumask = CPU_MASK_CPU0, |
| 140 | .set_next_event = bfin_timer_set_next_event, |
| 141 | .set_mode = bfin_timer_set_mode, |
| 142 | }; |
| 143 | |
| 144 | static struct irqaction bfin_timer_irq = { |
| 145 | .name = "Blackfin Core Timer", |
| 146 | .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL, |
| 147 | .handler = timer_interrupt, |
| 148 | .dev_id = &clockevent_bfin, |
| 149 | }; |
| 150 | |
| 151 | irqreturn_t timer_interrupt(int irq, void *dev_id) |
| 152 | { |
| 153 | struct clock_event_device *evt = dev_id; |
| 154 | evt->event_handler(evt); |
| 155 | return IRQ_HANDLED; |
| 156 | } |
| 157 | |
| 158 | static int __init bfin_clockevent_init(void) |
| 159 | { |
| 160 | setup_irq(IRQ_CORETMR, &bfin_timer_irq); |
| 161 | bfin_timer_init(); |
| 162 | |
| 163 | clockevent_bfin.mult = div_sc(get_cclk(), NSEC_PER_SEC, clockevent_bfin.shift); |
| 164 | clockevent_bfin.max_delta_ns = clockevent_delta2ns(-1, &clockevent_bfin); |
| 165 | clockevent_bfin.min_delta_ns = clockevent_delta2ns(100, &clockevent_bfin); |
| 166 | clockevents_register_device(&clockevent_bfin); |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | void __init time_init(void) |
| 172 | { |
| 173 | time_t secs_since_1970 = (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */ |
| 174 | |
| 175 | #ifdef CONFIG_RTC_DRV_BFIN |
| 176 | /* [#2663] hack to filter junk RTC values that would cause |
| 177 | * userspace to have to deal with time values greater than |
| 178 | * 2^31 seconds (which uClibc cannot cope with yet) |
| 179 | */ |
| 180 | if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) { |
| 181 | printk(KERN_NOTICE "bfin-rtc: invalid date; resetting\n"); |
| 182 | bfin_write_RTC_STAT(0); |
| 183 | } |
| 184 | #endif |
| 185 | |
| 186 | /* Initialize xtime. From now on, xtime is updated with timer interrupts */ |
| 187 | xtime.tv_sec = secs_since_1970; |
| 188 | xtime.tv_nsec = 0; |
| 189 | set_normalized_timespec(&wall_to_monotonic, -xtime.tv_sec, -xtime.tv_nsec); |
| 190 | |
| 191 | bfin_clocksource_init(); |
| 192 | bfin_clockevent_init(); |
| 193 | } |