Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 1 | /* |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 2 | * Based on arm clockevents implementation and old bfin time tick. |
| 3 | * |
Robin Getz | 96f1050 | 2009-09-24 14:11:24 +0000 | [diff] [blame] | 4 | * Copyright 2008-2009 Analog Devics Inc. |
| 5 | * 2008 GeoTechnologies |
| 6 | * Vitja Makarov |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 7 | * |
Robin Getz | 96f1050 | 2009-09-24 14:11:24 +0000 | [diff] [blame] | 8 | * Licensed under the GPL-2 |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 9 | */ |
Robin Getz | 96f1050 | 2009-09-24 14:11:24 +0000 | [diff] [blame] | 10 | |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 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> |
Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 19 | #include <linux/cpufreq.h> |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 20 | |
| 21 | #include <asm/blackfin.h> |
Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 22 | #include <asm/time.h> |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 23 | #include <asm/gptimers.h> |
Graf Yang | 60ffdb3 | 2010-01-20 10:56:24 +0000 | [diff] [blame] | 24 | #include <asm/nmi.h> |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 25 | |
Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 26 | /* Accelerators for sched_clock() |
| 27 | * convert from cycles(64bits) => nanoseconds (64bits) |
| 28 | * basic equation: |
| 29 | * ns = cycles / (freq / ns_per_sec) |
| 30 | * ns = cycles * (ns_per_sec / freq) |
| 31 | * ns = cycles * (10^9 / (cpu_khz * 10^3)) |
| 32 | * ns = cycles * (10^6 / cpu_khz) |
| 33 | * |
| 34 | * Then we use scaling math (suggested by george@mvista.com) to get: |
| 35 | * ns = cycles * (10^6 * SC / cpu_khz) / SC |
| 36 | * ns = cycles * cyc2ns_scale / SC |
| 37 | * |
| 38 | * And since SC is a constant power of two, we can convert the div |
| 39 | * into a shift. |
| 40 | * |
| 41 | * We can use khz divisor instead of mhz to keep a better precision, since |
| 42 | * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits. |
| 43 | * (mathieu.desnoyers@polymtl.ca) |
| 44 | * |
| 45 | * -johnstul@us.ibm.com "math is hard, lets go shopping!" |
| 46 | */ |
| 47 | |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 48 | #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */ |
| 49 | |
Yi Li | ceb33be | 2009-09-15 06:50:51 +0000 | [diff] [blame] | 50 | #if defined(CONFIG_CYCLES_CLOCKSOURCE) |
| 51 | |
Yi Li | ceb33be | 2009-09-15 06:50:51 +0000 | [diff] [blame] | 52 | static notrace cycle_t bfin_read_cycles(struct clocksource *cs) |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 53 | { |
Graf Yang | 6c2b707 | 2010-01-27 11:16:32 +0000 | [diff] [blame] | 54 | #ifdef CONFIG_CPU_FREQ |
Vitja Makarov | 1bfb4b2 | 2008-05-07 11:41:26 +0800 | [diff] [blame] | 55 | return __bfin_cycles_off + (get_cycles() << __bfin_cycles_mod); |
Graf Yang | 6c2b707 | 2010-01-27 11:16:32 +0000 | [diff] [blame] | 56 | #else |
| 57 | return get_cycles(); |
| 58 | #endif |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 59 | } |
| 60 | |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 61 | static struct clocksource bfin_cs_cycles = { |
| 62 | .name = "bfin_cs_cycles", |
Graf Yang | e78feaa | 2009-09-14 04:41:00 +0000 | [diff] [blame] | 63 | .rating = 400, |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 64 | .read = bfin_read_cycles, |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 65 | .mask = CLOCKSOURCE_MASK(64), |
Yi Li | 2985712 | 2009-09-15 08:55:47 +0000 | [diff] [blame] | 66 | .shift = CYC2NS_SCALE_FACTOR, |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 67 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 68 | }; |
| 69 | |
Yi Li | ceb33be | 2009-09-15 06:50:51 +0000 | [diff] [blame] | 70 | static inline unsigned long long bfin_cs_cycles_sched_clock(void) |
Magnus Damm | 8e19608 | 2009-04-21 12:24:00 -0700 | [diff] [blame] | 71 | { |
Mike Frysinger | c768a94 | 2009-12-04 03:32:11 +0000 | [diff] [blame] | 72 | return clocksource_cyc2ns(bfin_read_cycles(&bfin_cs_cycles), |
| 73 | bfin_cs_cycles.mult, bfin_cs_cycles.shift); |
Magnus Damm | 8e19608 | 2009-04-21 12:24:00 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 76 | static int __init bfin_cs_cycles_init(void) |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 77 | { |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 78 | bfin_cs_cycles.mult = \ |
| 79 | clocksource_hz2mult(get_cclk(), bfin_cs_cycles.shift); |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 80 | |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 81 | if (clocksource_register(&bfin_cs_cycles)) |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 82 | panic("failed to register clocksource"); |
| 83 | |
| 84 | return 0; |
| 85 | } |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 86 | #else |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 87 | # define bfin_cs_cycles_init() |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 88 | #endif |
| 89 | |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 90 | #ifdef CONFIG_GPTMR0_CLOCKSOURCE |
| 91 | |
| 92 | void __init setup_gptimer0(void) |
| 93 | { |
| 94 | disable_gptimers(TIMER0bit); |
| 95 | |
| 96 | set_gptimer_config(TIMER0_id, \ |
| 97 | TIMER_OUT_DIS | TIMER_PERIOD_CNT | TIMER_MODE_PWM); |
| 98 | set_gptimer_period(TIMER0_id, -1); |
| 99 | set_gptimer_pwidth(TIMER0_id, -2); |
| 100 | SSYNC(); |
| 101 | enable_gptimers(TIMER0bit); |
| 102 | } |
| 103 | |
Yi Li | f7036d6 | 2009-09-15 02:08:50 +0000 | [diff] [blame] | 104 | static cycle_t bfin_read_gptimer0(struct clocksource *cs) |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 105 | { |
| 106 | return bfin_read_TIMER0_COUNTER(); |
| 107 | } |
| 108 | |
| 109 | static struct clocksource bfin_cs_gptimer0 = { |
| 110 | .name = "bfin_cs_gptimer0", |
Graf Yang | e78feaa | 2009-09-14 04:41:00 +0000 | [diff] [blame] | 111 | .rating = 350, |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 112 | .read = bfin_read_gptimer0, |
| 113 | .mask = CLOCKSOURCE_MASK(32), |
Yi Li | 2985712 | 2009-09-15 08:55:47 +0000 | [diff] [blame] | 114 | .shift = CYC2NS_SCALE_FACTOR, |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 115 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 116 | }; |
| 117 | |
Yi Li | ceb33be | 2009-09-15 06:50:51 +0000 | [diff] [blame] | 118 | static inline unsigned long long bfin_cs_gptimer0_sched_clock(void) |
| 119 | { |
Mike Frysinger | c768a94 | 2009-12-04 03:32:11 +0000 | [diff] [blame] | 120 | return clocksource_cyc2ns(bfin_read_TIMER0_COUNTER(), |
| 121 | bfin_cs_gptimer0.mult, bfin_cs_gptimer0.shift); |
Yi Li | ceb33be | 2009-09-15 06:50:51 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 124 | static int __init bfin_cs_gptimer0_init(void) |
| 125 | { |
| 126 | setup_gptimer0(); |
| 127 | |
| 128 | bfin_cs_gptimer0.mult = \ |
| 129 | clocksource_hz2mult(get_sclk(), bfin_cs_gptimer0.shift); |
| 130 | |
| 131 | if (clocksource_register(&bfin_cs_gptimer0)) |
| 132 | panic("failed to register clocksource"); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | #else |
| 137 | # define bfin_cs_gptimer0_init() |
| 138 | #endif |
| 139 | |
Yi Li | ceb33be | 2009-09-15 06:50:51 +0000 | [diff] [blame] | 140 | #if defined(CONFIG_GPTMR0_CLOCKSOURCE) || defined(CONFIG_CYCLES_CLOCKSOURCE) |
| 141 | /* prefer to use cycles since it has higher rating */ |
| 142 | notrace unsigned long long sched_clock(void) |
| 143 | { |
| 144 | #if defined(CONFIG_CYCLES_CLOCKSOURCE) |
| 145 | return bfin_cs_cycles_sched_clock(); |
| 146 | #else |
| 147 | return bfin_cs_gptimer0_sched_clock(); |
| 148 | #endif |
| 149 | } |
| 150 | #endif |
| 151 | |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 152 | #if defined(CONFIG_TICKSOURCE_GPTMR0) |
Yi Li | 0d152c2 | 2009-12-28 10:21:49 +0000 | [diff] [blame] | 153 | static int bfin_gptmr0_set_next_event(unsigned long cycles, |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 154 | struct clock_event_device *evt) |
| 155 | { |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 156 | disable_gptimers(TIMER0bit); |
| 157 | |
| 158 | /* it starts counting three SCLK cycles after the TIMENx bit is set */ |
| 159 | set_gptimer_pwidth(TIMER0_id, cycles - 3); |
| 160 | enable_gptimers(TIMER0bit); |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
Yi Li | 0d152c2 | 2009-12-28 10:21:49 +0000 | [diff] [blame] | 164 | static void bfin_gptmr0_set_mode(enum clock_event_mode mode, |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 165 | struct clock_event_device *evt) |
| 166 | { |
| 167 | switch (mode) { |
| 168 | case CLOCK_EVT_MODE_PERIODIC: { |
| 169 | set_gptimer_config(TIMER0_id, \ |
| 170 | TIMER_OUT_DIS | TIMER_IRQ_ENA | \ |
| 171 | TIMER_PERIOD_CNT | TIMER_MODE_PWM); |
| 172 | set_gptimer_period(TIMER0_id, get_sclk() / HZ); |
| 173 | set_gptimer_pwidth(TIMER0_id, get_sclk() / HZ - 1); |
| 174 | enable_gptimers(TIMER0bit); |
| 175 | break; |
| 176 | } |
| 177 | case CLOCK_EVT_MODE_ONESHOT: |
| 178 | disable_gptimers(TIMER0bit); |
| 179 | set_gptimer_config(TIMER0_id, \ |
| 180 | TIMER_OUT_DIS | TIMER_IRQ_ENA | TIMER_MODE_PWM); |
| 181 | set_gptimer_period(TIMER0_id, 0); |
| 182 | break; |
| 183 | case CLOCK_EVT_MODE_UNUSED: |
| 184 | case CLOCK_EVT_MODE_SHUTDOWN: |
| 185 | disable_gptimers(TIMER0bit); |
| 186 | break; |
| 187 | case CLOCK_EVT_MODE_RESUME: |
| 188 | break; |
| 189 | } |
| 190 | } |
| 191 | |
Yi Li | 0d152c2 | 2009-12-28 10:21:49 +0000 | [diff] [blame] | 192 | static void bfin_gptmr0_ack(void) |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 193 | { |
| 194 | set_gptimer_status(TIMER_GROUP1, TIMER_STATUS_TIMIL0); |
| 195 | } |
| 196 | |
Yi Li | 0d152c2 | 2009-12-28 10:21:49 +0000 | [diff] [blame] | 197 | static void __init bfin_gptmr0_init(void) |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 198 | { |
| 199 | disable_gptimers(TIMER0bit); |
| 200 | } |
| 201 | |
Yi Li | 0d152c2 | 2009-12-28 10:21:49 +0000 | [diff] [blame] | 202 | #ifdef CONFIG_CORE_TIMER_IRQ_L1 |
| 203 | __attribute__((l1_text)) |
| 204 | #endif |
| 205 | irqreturn_t bfin_gptmr0_interrupt(int irq, void *dev_id) |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 206 | { |
Yi Li | 0d152c2 | 2009-12-28 10:21:49 +0000 | [diff] [blame] | 207 | struct clock_event_device *evt = dev_id; |
| 208 | smp_mb(); |
Mike Frysinger | 0bf02ce | 2011-04-04 15:26:11 +0000 | [diff] [blame^] | 209 | /* |
| 210 | * We want to ACK before we handle so that we can handle smaller timer |
| 211 | * intervals. This way if the timer expires again while we're handling |
| 212 | * things, we're more likely to see that 2nd int rather than swallowing |
| 213 | * it by ACKing the int at the end of this handler. |
| 214 | */ |
Yi Li | 0d152c2 | 2009-12-28 10:21:49 +0000 | [diff] [blame] | 215 | bfin_gptmr0_ack(); |
Mike Frysinger | 0bf02ce | 2011-04-04 15:26:11 +0000 | [diff] [blame^] | 216 | evt->event_handler(evt); |
Yi Li | 0d152c2 | 2009-12-28 10:21:49 +0000 | [diff] [blame] | 217 | return IRQ_HANDLED; |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Yi Li | 0d152c2 | 2009-12-28 10:21:49 +0000 | [diff] [blame] | 220 | static struct irqaction gptmr0_irq = { |
| 221 | .name = "Blackfin GPTimer0", |
| 222 | .flags = IRQF_DISABLED | IRQF_TIMER | \ |
| 223 | IRQF_IRQPOLL | IRQF_PERCPU, |
| 224 | .handler = bfin_gptmr0_interrupt, |
| 225 | }; |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 226 | |
Yi Li | 0d152c2 | 2009-12-28 10:21:49 +0000 | [diff] [blame] | 227 | static struct clock_event_device clockevent_gptmr0 = { |
| 228 | .name = "bfin_gptimer0", |
| 229 | .rating = 300, |
| 230 | .irq = IRQ_TIMER0, |
| 231 | .shift = 32, |
| 232 | .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT, |
| 233 | .set_next_event = bfin_gptmr0_set_next_event, |
| 234 | .set_mode = bfin_gptmr0_set_mode, |
| 235 | }; |
| 236 | |
| 237 | static void __init bfin_gptmr0_clockevent_init(struct clock_event_device *evt) |
| 238 | { |
| 239 | unsigned long clock_tick; |
| 240 | |
| 241 | clock_tick = get_sclk(); |
| 242 | evt->mult = div_sc(clock_tick, NSEC_PER_SEC, evt->shift); |
| 243 | evt->max_delta_ns = clockevent_delta2ns(-1, evt); |
| 244 | evt->min_delta_ns = clockevent_delta2ns(100, evt); |
| 245 | |
| 246 | evt->cpumask = cpumask_of(0); |
| 247 | |
| 248 | clockevents_register_device(evt); |
| 249 | } |
| 250 | #endif /* CONFIG_TICKSOURCE_GPTMR0 */ |
| 251 | |
| 252 | #if defined(CONFIG_TICKSOURCE_CORETMR) |
| 253 | /* per-cpu local core timer */ |
| 254 | static DEFINE_PER_CPU(struct clock_event_device, coretmr_events); |
| 255 | |
| 256 | static int bfin_coretmr_set_next_event(unsigned long cycles, |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 257 | struct clock_event_device *evt) |
| 258 | { |
| 259 | bfin_write_TCNTL(TMPWR); |
| 260 | CSYNC(); |
| 261 | bfin_write_TCOUNT(cycles); |
| 262 | CSYNC(); |
| 263 | bfin_write_TCNTL(TMPWR | TMREN); |
| 264 | return 0; |
| 265 | } |
| 266 | |
Yi Li | 0d152c2 | 2009-12-28 10:21:49 +0000 | [diff] [blame] | 267 | static void bfin_coretmr_set_mode(enum clock_event_mode mode, |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 268 | struct clock_event_device *evt) |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 269 | { |
| 270 | switch (mode) { |
| 271 | case CLOCK_EVT_MODE_PERIODIC: { |
Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 272 | unsigned long tcount = ((get_cclk() / (HZ * TIME_SCALE)) - 1); |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 273 | bfin_write_TCNTL(TMPWR); |
| 274 | CSYNC(); |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 275 | bfin_write_TSCALE(TIME_SCALE - 1); |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 276 | bfin_write_TPERIOD(tcount); |
| 277 | bfin_write_TCOUNT(tcount); |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 278 | CSYNC(); |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 279 | bfin_write_TCNTL(TMPWR | TMREN | TAUTORLD); |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 280 | break; |
| 281 | } |
| 282 | case CLOCK_EVT_MODE_ONESHOT: |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 283 | bfin_write_TCNTL(TMPWR); |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 284 | CSYNC(); |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 285 | bfin_write_TSCALE(TIME_SCALE - 1); |
| 286 | bfin_write_TPERIOD(0); |
| 287 | bfin_write_TCOUNT(0); |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 288 | break; |
| 289 | case CLOCK_EVT_MODE_UNUSED: |
| 290 | case CLOCK_EVT_MODE_SHUTDOWN: |
| 291 | bfin_write_TCNTL(0); |
| 292 | CSYNC(); |
| 293 | break; |
| 294 | case CLOCK_EVT_MODE_RESUME: |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | |
Yi Li | 0d152c2 | 2009-12-28 10:21:49 +0000 | [diff] [blame] | 299 | void bfin_coretmr_init(void) |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 300 | { |
| 301 | /* power up the timer, but don't enable it just yet */ |
| 302 | bfin_write_TCNTL(TMPWR); |
| 303 | CSYNC(); |
| 304 | |
Yi Li | 0d152c2 | 2009-12-28 10:21:49 +0000 | [diff] [blame] | 305 | /* the TSCALE prescaler counter. */ |
Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 306 | bfin_write_TSCALE(TIME_SCALE - 1); |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 307 | bfin_write_TPERIOD(0); |
| 308 | bfin_write_TCOUNT(0); |
| 309 | |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 310 | CSYNC(); |
| 311 | } |
| 312 | |
Yi Li | 0d152c2 | 2009-12-28 10:21:49 +0000 | [diff] [blame] | 313 | #ifdef CONFIG_CORE_TIMER_IRQ_L1 |
| 314 | __attribute__((l1_text)) |
| 315 | #endif |
| 316 | irqreturn_t bfin_coretmr_interrupt(int irq, void *dev_id) |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 317 | { |
Yi Li | 0d152c2 | 2009-12-28 10:21:49 +0000 | [diff] [blame] | 318 | int cpu = smp_processor_id(); |
| 319 | struct clock_event_device *evt = &per_cpu(coretmr_events, cpu); |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 320 | |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 321 | smp_mb(); |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 322 | evt->event_handler(evt); |
Graf Yang | 60ffdb3 | 2010-01-20 10:56:24 +0000 | [diff] [blame] | 323 | |
| 324 | touch_nmi_watchdog(); |
| 325 | |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 326 | return IRQ_HANDLED; |
| 327 | } |
| 328 | |
Yi Li | 0d152c2 | 2009-12-28 10:21:49 +0000 | [diff] [blame] | 329 | static struct irqaction coretmr_irq = { |
| 330 | .name = "Blackfin CoreTimer", |
| 331 | .flags = IRQF_DISABLED | IRQF_TIMER | \ |
| 332 | IRQF_IRQPOLL | IRQF_PERCPU, |
| 333 | .handler = bfin_coretmr_interrupt, |
| 334 | }; |
| 335 | |
| 336 | void bfin_coretmr_clockevent_init(void) |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 337 | { |
Yi Li | 0d152c2 | 2009-12-28 10:21:49 +0000 | [diff] [blame] | 338 | unsigned long clock_tick; |
| 339 | unsigned int cpu = smp_processor_id(); |
| 340 | struct clock_event_device *evt = &per_cpu(coretmr_events, cpu); |
Vitja Makarov | 1bfb4b2 | 2008-05-07 11:41:26 +0800 | [diff] [blame] | 341 | |
Yi Li | 0d152c2 | 2009-12-28 10:21:49 +0000 | [diff] [blame] | 342 | evt->name = "bfin_core_timer"; |
| 343 | evt->rating = 350; |
| 344 | evt->irq = -1; |
| 345 | evt->shift = 32; |
| 346 | evt->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT; |
| 347 | evt->set_next_event = bfin_coretmr_set_next_event; |
| 348 | evt->set_mode = bfin_coretmr_set_mode; |
Vitja Makarov | 1bfb4b2 | 2008-05-07 11:41:26 +0800 | [diff] [blame] | 349 | |
Yi Li | 0d152c2 | 2009-12-28 10:21:49 +0000 | [diff] [blame] | 350 | clock_tick = get_cclk() / TIME_SCALE; |
| 351 | evt->mult = div_sc(clock_tick, NSEC_PER_SEC, evt->shift); |
| 352 | evt->max_delta_ns = clockevent_delta2ns(-1, evt); |
| 353 | evt->min_delta_ns = clockevent_delta2ns(100, evt); |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 354 | |
Yi Li | 0d152c2 | 2009-12-28 10:21:49 +0000 | [diff] [blame] | 355 | evt->cpumask = cpumask_of(cpu); |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 356 | |
Yi Li | 0d152c2 | 2009-12-28 10:21:49 +0000 | [diff] [blame] | 357 | clockevents_register_device(evt); |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 358 | } |
Yi Li | 0d152c2 | 2009-12-28 10:21:49 +0000 | [diff] [blame] | 359 | #endif /* CONFIG_TICKSOURCE_CORETMR */ |
| 360 | |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 361 | |
John Stultz | cb0e996 | 2010-03-03 19:57:24 -0800 | [diff] [blame] | 362 | void read_persistent_clock(struct timespec *ts) |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 363 | { |
| 364 | time_t secs_since_1970 = (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */ |
John Stultz | cb0e996 | 2010-03-03 19:57:24 -0800 | [diff] [blame] | 365 | ts->tv_sec = secs_since_1970; |
| 366 | ts->tv_nsec = 0; |
| 367 | } |
| 368 | |
| 369 | void __init time_init(void) |
| 370 | { |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 371 | |
| 372 | #ifdef CONFIG_RTC_DRV_BFIN |
| 373 | /* [#2663] hack to filter junk RTC values that would cause |
| 374 | * userspace to have to deal with time values greater than |
| 375 | * 2^31 seconds (which uClibc cannot cope with yet) |
| 376 | */ |
| 377 | if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) { |
| 378 | printk(KERN_NOTICE "bfin-rtc: invalid date; resetting\n"); |
| 379 | bfin_write_RTC_STAT(0); |
| 380 | } |
| 381 | #endif |
| 382 | |
Graf Yang | 1fa9be7 | 2009-05-15 11:01:59 +0000 | [diff] [blame] | 383 | bfin_cs_cycles_init(); |
| 384 | bfin_cs_gptimer0_init(); |
Yi Li | 0d152c2 | 2009-12-28 10:21:49 +0000 | [diff] [blame] | 385 | |
| 386 | #if defined(CONFIG_TICKSOURCE_CORETMR) |
| 387 | bfin_coretmr_init(); |
| 388 | setup_irq(IRQ_CORETMR, &coretmr_irq); |
| 389 | bfin_coretmr_clockevent_init(); |
| 390 | #endif |
| 391 | |
| 392 | #if defined(CONFIG_TICKSOURCE_GPTMR0) |
| 393 | bfin_gptmr0_init(); |
| 394 | setup_irq(IRQ_TIMER0, &gptmr0_irq); |
| 395 | gptmr0_irq.dev_id = &clockevent_gptmr0; |
| 396 | bfin_gptmr0_clockevent_init(&clockevent_gptmr0); |
| 397 | #endif |
| 398 | |
| 399 | #if !defined(CONFIG_TICKSOURCE_CORETMR) && !defined(CONFIG_TICKSOURCE_GPTMR0) |
| 400 | # error at least one clock event device is required |
| 401 | #endif |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 402 | } |