1*f86b9e03SGreg Ungerer /***************************************************************************/ 2*f86b9e03SGreg Ungerer 3*f86b9e03SGreg Ungerer /* 4*f86b9e03SGreg Ungerer * sltimers.c -- generic ColdFire slice timer support. 5*f86b9e03SGreg Ungerer * 6*f86b9e03SGreg Ungerer * Copyright (C) 2009-2010, Philippe De Muyter <phdm@macqel.be> 7*f86b9e03SGreg Ungerer * based on 8*f86b9e03SGreg Ungerer * timers.c -- generic ColdFire hardware timer support. 9*f86b9e03SGreg Ungerer * Copyright (C) 1999-2008, Greg Ungerer <gerg@snapgear.com> 10*f86b9e03SGreg Ungerer */ 11*f86b9e03SGreg Ungerer 12*f86b9e03SGreg Ungerer /***************************************************************************/ 13*f86b9e03SGreg Ungerer 14*f86b9e03SGreg Ungerer #include <linux/kernel.h> 15*f86b9e03SGreg Ungerer #include <linux/init.h> 16*f86b9e03SGreg Ungerer #include <linux/sched.h> 17*f86b9e03SGreg Ungerer #include <linux/interrupt.h> 18*f86b9e03SGreg Ungerer #include <linux/irq.h> 19*f86b9e03SGreg Ungerer #include <linux/profile.h> 20*f86b9e03SGreg Ungerer #include <linux/clocksource.h> 21*f86b9e03SGreg Ungerer #include <asm/io.h> 22*f86b9e03SGreg Ungerer #include <asm/traps.h> 23*f86b9e03SGreg Ungerer #include <asm/machdep.h> 24*f86b9e03SGreg Ungerer #include <asm/coldfire.h> 25*f86b9e03SGreg Ungerer #include <asm/mcfslt.h> 26*f86b9e03SGreg Ungerer #include <asm/mcfsim.h> 27*f86b9e03SGreg Ungerer 28*f86b9e03SGreg Ungerer /***************************************************************************/ 29*f86b9e03SGreg Ungerer 30*f86b9e03SGreg Ungerer #ifdef CONFIG_HIGHPROFILE 31*f86b9e03SGreg Ungerer 32*f86b9e03SGreg Ungerer /* 33*f86b9e03SGreg Ungerer * By default use Slice Timer 1 as the profiler clock timer. 34*f86b9e03SGreg Ungerer */ 35*f86b9e03SGreg Ungerer #define PA(a) (MCFSLT_TIMER1 + (a)) 36*f86b9e03SGreg Ungerer 37*f86b9e03SGreg Ungerer /* 38*f86b9e03SGreg Ungerer * Choose a reasonably fast profile timer. Make it an odd value to 39*f86b9e03SGreg Ungerer * try and get good coverage of kernel operations. 40*f86b9e03SGreg Ungerer */ 41*f86b9e03SGreg Ungerer #define PROFILEHZ 1013 42*f86b9e03SGreg Ungerer 43*f86b9e03SGreg Ungerer irqreturn_t mcfslt_profile_tick(int irq, void *dummy) 44*f86b9e03SGreg Ungerer { 45*f86b9e03SGreg Ungerer /* Reset Slice Timer 1 */ 46*f86b9e03SGreg Ungerer __raw_writel(MCFSLT_SSR_BE | MCFSLT_SSR_TE, PA(MCFSLT_SSR)); 47*f86b9e03SGreg Ungerer if (current->pid) 48*f86b9e03SGreg Ungerer profile_tick(CPU_PROFILING); 49*f86b9e03SGreg Ungerer return IRQ_HANDLED; 50*f86b9e03SGreg Ungerer } 51*f86b9e03SGreg Ungerer 52*f86b9e03SGreg Ungerer static struct irqaction mcfslt_profile_irq = { 53*f86b9e03SGreg Ungerer .name = "profile timer", 54*f86b9e03SGreg Ungerer .flags = IRQF_TIMER, 55*f86b9e03SGreg Ungerer .handler = mcfslt_profile_tick, 56*f86b9e03SGreg Ungerer }; 57*f86b9e03SGreg Ungerer 58*f86b9e03SGreg Ungerer void mcfslt_profile_init(void) 59*f86b9e03SGreg Ungerer { 60*f86b9e03SGreg Ungerer printk(KERN_INFO "PROFILE: lodging TIMER 1 @ %dHz as profile timer\n", 61*f86b9e03SGreg Ungerer PROFILEHZ); 62*f86b9e03SGreg Ungerer 63*f86b9e03SGreg Ungerer setup_irq(MCF_IRQ_PROFILER, &mcfslt_profile_irq); 64*f86b9e03SGreg Ungerer 65*f86b9e03SGreg Ungerer /* Set up TIMER 2 as high speed profile clock */ 66*f86b9e03SGreg Ungerer __raw_writel(MCF_BUSCLK / PROFILEHZ - 1, PA(MCFSLT_STCNT)); 67*f86b9e03SGreg Ungerer __raw_writel(MCFSLT_SCR_RUN | MCFSLT_SCR_IEN | MCFSLT_SCR_TEN, 68*f86b9e03SGreg Ungerer PA(MCFSLT_SCR)); 69*f86b9e03SGreg Ungerer 70*f86b9e03SGreg Ungerer } 71*f86b9e03SGreg Ungerer 72*f86b9e03SGreg Ungerer #endif /* CONFIG_HIGHPROFILE */ 73*f86b9e03SGreg Ungerer 74*f86b9e03SGreg Ungerer /***************************************************************************/ 75*f86b9e03SGreg Ungerer 76*f86b9e03SGreg Ungerer /* 77*f86b9e03SGreg Ungerer * By default use Slice Timer 0 as the system clock timer. 78*f86b9e03SGreg Ungerer */ 79*f86b9e03SGreg Ungerer #define TA(a) (MCFSLT_TIMER0 + (a)) 80*f86b9e03SGreg Ungerer 81*f86b9e03SGreg Ungerer static u32 mcfslt_cycles_per_jiffy; 82*f86b9e03SGreg Ungerer static u32 mcfslt_cnt; 83*f86b9e03SGreg Ungerer 84*f86b9e03SGreg Ungerer static irq_handler_t timer_interrupt; 85*f86b9e03SGreg Ungerer 86*f86b9e03SGreg Ungerer static irqreturn_t mcfslt_tick(int irq, void *dummy) 87*f86b9e03SGreg Ungerer { 88*f86b9e03SGreg Ungerer /* Reset Slice Timer 0 */ 89*f86b9e03SGreg Ungerer __raw_writel(MCFSLT_SSR_BE | MCFSLT_SSR_TE, TA(MCFSLT_SSR)); 90*f86b9e03SGreg Ungerer mcfslt_cnt += mcfslt_cycles_per_jiffy; 91*f86b9e03SGreg Ungerer return timer_interrupt(irq, dummy); 92*f86b9e03SGreg Ungerer } 93*f86b9e03SGreg Ungerer 94*f86b9e03SGreg Ungerer static struct irqaction mcfslt_timer_irq = { 95*f86b9e03SGreg Ungerer .name = "timer", 96*f86b9e03SGreg Ungerer .flags = IRQF_TIMER, 97*f86b9e03SGreg Ungerer .handler = mcfslt_tick, 98*f86b9e03SGreg Ungerer }; 99*f86b9e03SGreg Ungerer 100*f86b9e03SGreg Ungerer static cycle_t mcfslt_read_clk(struct clocksource *cs) 101*f86b9e03SGreg Ungerer { 102*f86b9e03SGreg Ungerer unsigned long flags; 103*f86b9e03SGreg Ungerer u32 cycles, scnt; 104*f86b9e03SGreg Ungerer 105*f86b9e03SGreg Ungerer local_irq_save(flags); 106*f86b9e03SGreg Ungerer scnt = __raw_readl(TA(MCFSLT_SCNT)); 107*f86b9e03SGreg Ungerer cycles = mcfslt_cnt; 108*f86b9e03SGreg Ungerer if (__raw_readl(TA(MCFSLT_SSR)) & MCFSLT_SSR_TE) { 109*f86b9e03SGreg Ungerer cycles += mcfslt_cycles_per_jiffy; 110*f86b9e03SGreg Ungerer scnt = __raw_readl(TA(MCFSLT_SCNT)); 111*f86b9e03SGreg Ungerer } 112*f86b9e03SGreg Ungerer local_irq_restore(flags); 113*f86b9e03SGreg Ungerer 114*f86b9e03SGreg Ungerer /* subtract because slice timers count down */ 115*f86b9e03SGreg Ungerer return cycles + ((mcfslt_cycles_per_jiffy - 1) - scnt); 116*f86b9e03SGreg Ungerer } 117*f86b9e03SGreg Ungerer 118*f86b9e03SGreg Ungerer static struct clocksource mcfslt_clk = { 119*f86b9e03SGreg Ungerer .name = "slt", 120*f86b9e03SGreg Ungerer .rating = 250, 121*f86b9e03SGreg Ungerer .read = mcfslt_read_clk, 122*f86b9e03SGreg Ungerer .mask = CLOCKSOURCE_MASK(32), 123*f86b9e03SGreg Ungerer .flags = CLOCK_SOURCE_IS_CONTINUOUS, 124*f86b9e03SGreg Ungerer }; 125*f86b9e03SGreg Ungerer 126*f86b9e03SGreg Ungerer void hw_timer_init(irq_handler_t handler) 127*f86b9e03SGreg Ungerer { 128*f86b9e03SGreg Ungerer mcfslt_cycles_per_jiffy = MCF_BUSCLK / HZ; 129*f86b9e03SGreg Ungerer /* 130*f86b9e03SGreg Ungerer * The coldfire slice timer (SLT) runs from STCNT to 0 included, 131*f86b9e03SGreg Ungerer * then STCNT again and so on. It counts thus actually 132*f86b9e03SGreg Ungerer * STCNT + 1 steps for 1 tick, not STCNT. So if you want 133*f86b9e03SGreg Ungerer * n cycles, initialize STCNT with n - 1. 134*f86b9e03SGreg Ungerer */ 135*f86b9e03SGreg Ungerer __raw_writel(mcfslt_cycles_per_jiffy - 1, TA(MCFSLT_STCNT)); 136*f86b9e03SGreg Ungerer __raw_writel(MCFSLT_SCR_RUN | MCFSLT_SCR_IEN | MCFSLT_SCR_TEN, 137*f86b9e03SGreg Ungerer TA(MCFSLT_SCR)); 138*f86b9e03SGreg Ungerer /* initialize mcfslt_cnt knowing that slice timers count down */ 139*f86b9e03SGreg Ungerer mcfslt_cnt = mcfslt_cycles_per_jiffy; 140*f86b9e03SGreg Ungerer 141*f86b9e03SGreg Ungerer timer_interrupt = handler; 142*f86b9e03SGreg Ungerer setup_irq(MCF_IRQ_TIMER, &mcfslt_timer_irq); 143*f86b9e03SGreg Ungerer 144*f86b9e03SGreg Ungerer clocksource_register_hz(&mcfslt_clk, MCF_BUSCLK); 145*f86b9e03SGreg Ungerer 146*f86b9e03SGreg Ungerer #ifdef CONFIG_HIGHPROFILE 147*f86b9e03SGreg Ungerer mcfslt_profile_init(); 148*f86b9e03SGreg Ungerer #endif 149*f86b9e03SGreg Ungerer } 150