Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /***************************************************************************/ |
| 2 | |
| 3 | /* |
| 4 | * timers.c -- generic ColdFire hardware timer support. |
| 5 | * |
Greg Ungerer | a7f61fa | 2008-02-01 17:40:26 +1000 | [diff] [blame] | 6 | * Copyright (C) 1999-2008, Greg Ungerer <gerg@snapgear.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | /***************************************************************************/ |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/init.h> |
Greg Ungerer | 2f2c267 | 2007-10-23 14:37:54 +1000 | [diff] [blame] | 13 | #include <linux/sched.h> |
| 14 | #include <linux/interrupt.h> |
Greg Ungerer | c52a2cd | 2007-07-27 01:09:00 +1000 | [diff] [blame] | 15 | #include <linux/irq.h> |
Greg Ungerer | a7f61fa | 2008-02-01 17:40:26 +1000 | [diff] [blame] | 16 | #include <linux/profile.h> |
| 17 | #include <linux/clocksource.h> |
Greg Ungerer | 0b7ac8e | 2006-06-26 10:33:10 +1000 | [diff] [blame] | 18 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/traps.h> |
| 20 | #include <asm/machdep.h> |
| 21 | #include <asm/coldfire.h> |
| 22 | #include <asm/mcftimer.h> |
| 23 | #include <asm/mcfsim.h> |
| 24 | |
| 25 | /***************************************************************************/ |
| 26 | |
| 27 | /* |
Greg Ungerer | 0b7ac8e | 2006-06-26 10:33:10 +1000 | [diff] [blame] | 28 | * By default use timer1 as the system clock timer. |
| 29 | */ |
Greg Ungerer | a7f61fa | 2008-02-01 17:40:26 +1000 | [diff] [blame] | 30 | #define FREQ (MCF_BUSCLK / 16) |
Greg Ungerer | 58f0ac9 | 2011-03-09 09:57:14 +1000 | [diff] [blame] | 31 | #define TA(a) (MCFTIMER_BASE1 + (a)) |
Greg Ungerer | 0b7ac8e | 2006-06-26 10:33:10 +1000 | [diff] [blame] | 32 | |
| 33 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | * These provide the underlying interrupt vector support. |
| 35 | * Unfortunately it is a little different on each ColdFire. |
| 36 | */ |
Greg Ungerer | a7f61fa | 2008-02-01 17:40:26 +1000 | [diff] [blame] | 37 | void coldfire_profile_init(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Greg Ungerer | deb77c8 | 2006-12-06 11:36:59 +1000 | [diff] [blame] | 39 | #if defined(CONFIG_M532x) |
| 40 | #define __raw_readtrr __raw_readl |
| 41 | #define __raw_writetrr __raw_writel |
| 42 | #else |
| 43 | #define __raw_readtrr __raw_readw |
| 44 | #define __raw_writetrr __raw_writew |
| 45 | #endif |
| 46 | |
Greg Ungerer | a7f61fa | 2008-02-01 17:40:26 +1000 | [diff] [blame] | 47 | static u32 mcftmr_cycles_per_jiffy; |
| 48 | static u32 mcftmr_cnt; |
| 49 | |
Greg Ungerer | 35aefb2 | 2012-01-23 15:34:58 +1000 | [diff] [blame] | 50 | static irq_handler_t timer_interrupt; |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | /***************************************************************************/ |
| 53 | |
Greg Ungerer | a7f61fa | 2008-02-01 17:40:26 +1000 | [diff] [blame] | 54 | static irqreturn_t mcftmr_tick(int irq, void *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
| 56 | /* Reset the ColdFire timer */ |
Greg Ungerer | 0b7ac8e | 2006-06-26 10:33:10 +1000 | [diff] [blame] | 57 | __raw_writeb(MCFTIMER_TER_CAP | MCFTIMER_TER_REF, TA(MCFTIMER_TER)); |
Greg Ungerer | 2f2c267 | 2007-10-23 14:37:54 +1000 | [diff] [blame] | 58 | |
Greg Ungerer | a7f61fa | 2008-02-01 17:40:26 +1000 | [diff] [blame] | 59 | mcftmr_cnt += mcftmr_cycles_per_jiffy; |
Greg Ungerer | 35aefb2 | 2012-01-23 15:34:58 +1000 | [diff] [blame] | 60 | return timer_interrupt(irq, dummy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /***************************************************************************/ |
| 64 | |
Greg Ungerer | a7f61fa | 2008-02-01 17:40:26 +1000 | [diff] [blame] | 65 | static struct irqaction mcftmr_timer_irq = { |
Greg Ungerer | 2f2c267 | 2007-10-23 14:37:54 +1000 | [diff] [blame] | 66 | .name = "timer", |
| 67 | .flags = IRQF_DISABLED | IRQF_TIMER, |
Greg Ungerer | a7f61fa | 2008-02-01 17:40:26 +1000 | [diff] [blame] | 68 | .handler = mcftmr_tick, |
Greg Ungerer | c52a2cd | 2007-07-27 01:09:00 +1000 | [diff] [blame] | 69 | }; |
| 70 | |
Greg Ungerer | 2f2c267 | 2007-10-23 14:37:54 +1000 | [diff] [blame] | 71 | /***************************************************************************/ |
| 72 | |
Magnus Damm | 8e19608 | 2009-04-21 12:24:00 -0700 | [diff] [blame] | 73 | static cycle_t mcftmr_read_clk(struct clocksource *cs) |
Greg Ungerer | a7f61fa | 2008-02-01 17:40:26 +1000 | [diff] [blame] | 74 | { |
| 75 | unsigned long flags; |
| 76 | u32 cycles; |
| 77 | u16 tcn; |
| 78 | |
| 79 | local_irq_save(flags); |
| 80 | tcn = __raw_readw(TA(MCFTIMER_TCN)); |
| 81 | cycles = mcftmr_cnt; |
| 82 | local_irq_restore(flags); |
| 83 | |
| 84 | return cycles + tcn; |
| 85 | } |
| 86 | |
| 87 | /***************************************************************************/ |
| 88 | |
| 89 | static struct clocksource mcftmr_clk = { |
| 90 | .name = "tmr", |
| 91 | .rating = 250, |
| 92 | .read = mcftmr_read_clk, |
Greg Ungerer | a7f61fa | 2008-02-01 17:40:26 +1000 | [diff] [blame] | 93 | .mask = CLOCKSOURCE_MASK(32), |
| 94 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 95 | }; |
| 96 | |
| 97 | /***************************************************************************/ |
Greg Ungerer | 4342f4a | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 98 | |
Greg Ungerer | 35aefb2 | 2012-01-23 15:34:58 +1000 | [diff] [blame] | 99 | void hw_timer_init(irq_handler_t handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
Greg Ungerer | 0b7ac8e | 2006-06-26 10:33:10 +1000 | [diff] [blame] | 101 | __raw_writew(MCFTIMER_TMR_DISABLE, TA(MCFTIMER_TMR)); |
Greg Ungerer | a7f61fa | 2008-02-01 17:40:26 +1000 | [diff] [blame] | 102 | mcftmr_cycles_per_jiffy = FREQ / HZ; |
Philippe De Muyter | 6c38d85 | 2008-06-12 15:21:36 -0700 | [diff] [blame] | 103 | /* |
| 104 | * The coldfire timer runs from 0 to TRR included, then 0 |
| 105 | * again and so on. It counts thus actually TRR + 1 steps |
| 106 | * for 1 tick, not TRR. So if you want n cycles, |
| 107 | * initialize TRR with n - 1. |
| 108 | */ |
| 109 | __raw_writetrr(mcftmr_cycles_per_jiffy - 1, TA(MCFTIMER_TRR)); |
Greg Ungerer | 0b7ac8e | 2006-06-26 10:33:10 +1000 | [diff] [blame] | 110 | __raw_writew(MCFTIMER_TMR_ENORI | MCFTIMER_TMR_CLK16 | |
| 111 | MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENABLE, TA(MCFTIMER_TMR)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
John Stultz | 010f3f1 | 2010-04-26 20:21:52 -0700 | [diff] [blame] | 113 | clocksource_register_hz(&mcftmr_clk, FREQ); |
Greg Ungerer | a7f61fa | 2008-02-01 17:40:26 +1000 | [diff] [blame] | 114 | |
Greg Ungerer | 35aefb2 | 2012-01-23 15:34:58 +1000 | [diff] [blame] | 115 | timer_interrupt = handler; |
Greg Ungerer | 3945ca0f | 2009-05-22 13:50:53 +1000 | [diff] [blame] | 116 | setup_irq(MCF_IRQ_TIMER, &mcftmr_timer_irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | #ifdef CONFIG_HIGHPROFILE |
| 119 | coldfire_profile_init(); |
| 120 | #endif |
| 121 | } |
| 122 | |
| 123 | /***************************************************************************/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | #ifdef CONFIG_HIGHPROFILE |
| 125 | /***************************************************************************/ |
| 126 | |
| 127 | /* |
Greg Ungerer | 0b7ac8e | 2006-06-26 10:33:10 +1000 | [diff] [blame] | 128 | * By default use timer2 as the profiler clock timer. |
| 129 | */ |
Greg Ungerer | 58f0ac9 | 2011-03-09 09:57:14 +1000 | [diff] [blame] | 130 | #define PA(a) (MCFTIMER_BASE2 + (a)) |
Greg Ungerer | 0b7ac8e | 2006-06-26 10:33:10 +1000 | [diff] [blame] | 131 | |
| 132 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | * Choose a reasonably fast profile timer. Make it an odd value to |
Robert P. J. Day | d08df60 | 2007-02-17 19:07:33 +0100 | [diff] [blame] | 134 | * try and get good coverage of kernel operations. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | */ |
| 136 | #define PROFILEHZ 1013 |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | /* |
| 139 | * Use the other timer to provide high accuracy profiling info. |
| 140 | */ |
Greg Ungerer | c051b01 | 2007-02-07 12:03:01 +1000 | [diff] [blame] | 141 | irqreturn_t coldfire_profile_tick(int irq, void *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
| 143 | /* Reset ColdFire timer2 */ |
Greg Ungerer | 0b7ac8e | 2006-06-26 10:33:10 +1000 | [diff] [blame] | 144 | __raw_writeb(MCFTIMER_TER_CAP | MCFTIMER_TER_REF, PA(MCFTIMER_TER)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | if (current->pid) |
Matt Waddel | 6ef1e56 | 2008-02-14 19:31:27 -0800 | [diff] [blame] | 146 | profile_tick(CPU_PROFILING); |
Greg Ungerer | c051b01 | 2007-02-07 12:03:01 +1000 | [diff] [blame] | 147 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /***************************************************************************/ |
| 151 | |
Matt Waddel | 6ef1e56 | 2008-02-14 19:31:27 -0800 | [diff] [blame] | 152 | static struct irqaction coldfire_profile_irq = { |
| 153 | .name = "profile timer", |
| 154 | .flags = IRQF_DISABLED | IRQF_TIMER, |
| 155 | .handler = coldfire_profile_tick, |
| 156 | }; |
| 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | void coldfire_profile_init(void) |
| 159 | { |
Matt Waddel | 6ef1e56 | 2008-02-14 19:31:27 -0800 | [diff] [blame] | 160 | printk(KERN_INFO "PROFILE: lodging TIMER2 @ %dHz as profile timer\n", |
| 161 | PROFILEHZ); |
| 162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | /* Set up TIMER 2 as high speed profile clock */ |
Greg Ungerer | 0b7ac8e | 2006-06-26 10:33:10 +1000 | [diff] [blame] | 164 | __raw_writew(MCFTIMER_TMR_DISABLE, PA(MCFTIMER_TMR)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
Matt Waddel | 6ef1e56 | 2008-02-14 19:31:27 -0800 | [diff] [blame] | 166 | __raw_writetrr(((MCF_BUSCLK / 16) / PROFILEHZ), PA(MCFTIMER_TRR)); |
Greg Ungerer | 0b7ac8e | 2006-06-26 10:33:10 +1000 | [diff] [blame] | 167 | __raw_writew(MCFTIMER_TMR_ENORI | MCFTIMER_TMR_CLK16 | |
| 168 | MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENABLE, PA(MCFTIMER_TMR)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Greg Ungerer | 3945ca0f | 2009-05-22 13:50:53 +1000 | [diff] [blame] | 170 | setup_irq(MCF_IRQ_PROFILER, &coldfire_profile_irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /***************************************************************************/ |
| 174 | #endif /* CONFIG_HIGHPROFILE */ |
| 175 | /***************************************************************************/ |