1 /* 2 * arch/xtensa/kernel/time.c 3 * 4 * Timer and clock support. 5 * 6 * This file is subject to the terms and conditions of the GNU General Public 7 * License. See the file "COPYING" in the main directory of this archive 8 * for more details. 9 * 10 * Copyright (C) 2005 Tensilica Inc. 11 * 12 * Chris Zankel <chris@zankel.net> 13 */ 14 15 #include <linux/errno.h> 16 #include <linux/sched.h> 17 #include <linux/time.h> 18 #include <linux/clk-provider.h> 19 #include <linux/clocksource.h> 20 #include <linux/clockchips.h> 21 #include <linux/interrupt.h> 22 #include <linux/module.h> 23 #include <linux/init.h> 24 #include <linux/irq.h> 25 #include <linux/profile.h> 26 #include <linux/delay.h> 27 #include <linux/irqdomain.h> 28 #include <linux/sched_clock.h> 29 30 #include <asm/timex.h> 31 #include <asm/platform.h> 32 33 unsigned long ccount_freq; /* ccount Hz */ 34 EXPORT_SYMBOL(ccount_freq); 35 36 static cycle_t ccount_read(struct clocksource *cs) 37 { 38 return (cycle_t)get_ccount(); 39 } 40 41 static u64 notrace ccount_sched_clock_read(void) 42 { 43 return get_ccount(); 44 } 45 46 static struct clocksource ccount_clocksource = { 47 .name = "ccount", 48 .rating = 200, 49 .read = ccount_read, 50 .mask = CLOCKSOURCE_MASK(32), 51 .flags = CLOCK_SOURCE_IS_CONTINUOUS, 52 }; 53 54 static int ccount_timer_set_next_event(unsigned long delta, 55 struct clock_event_device *dev); 56 struct ccount_timer { 57 struct clock_event_device evt; 58 int irq_enabled; 59 char name[24]; 60 }; 61 static DEFINE_PER_CPU(struct ccount_timer, ccount_timer); 62 63 static int ccount_timer_set_next_event(unsigned long delta, 64 struct clock_event_device *dev) 65 { 66 unsigned long flags, next; 67 int ret = 0; 68 69 local_irq_save(flags); 70 next = get_ccount() + delta; 71 set_linux_timer(next); 72 if (next - get_ccount() > delta) 73 ret = -ETIME; 74 local_irq_restore(flags); 75 76 return ret; 77 } 78 79 /* 80 * There is no way to disable the timer interrupt at the device level, 81 * only at the intenable register itself. Since enable_irq/disable_irq 82 * calls are nested, we need to make sure that these calls are 83 * balanced. 84 */ 85 static int ccount_timer_shutdown(struct clock_event_device *evt) 86 { 87 struct ccount_timer *timer = 88 container_of(evt, struct ccount_timer, evt); 89 90 if (timer->irq_enabled) { 91 disable_irq(evt->irq); 92 timer->irq_enabled = 0; 93 } 94 return 0; 95 } 96 97 static int ccount_timer_set_oneshot(struct clock_event_device *evt) 98 { 99 struct ccount_timer *timer = 100 container_of(evt, struct ccount_timer, evt); 101 102 if (!timer->irq_enabled) { 103 enable_irq(evt->irq); 104 timer->irq_enabled = 1; 105 } 106 return 0; 107 } 108 109 static irqreturn_t timer_interrupt(int irq, void *dev_id); 110 static struct irqaction timer_irqaction = { 111 .handler = timer_interrupt, 112 .flags = IRQF_TIMER, 113 .name = "timer", 114 }; 115 116 void local_timer_setup(unsigned cpu) 117 { 118 struct ccount_timer *timer = &per_cpu(ccount_timer, cpu); 119 struct clock_event_device *clockevent = &timer->evt; 120 121 timer->irq_enabled = 1; 122 clockevent->name = timer->name; 123 snprintf(timer->name, sizeof(timer->name), "ccount_clockevent_%u", cpu); 124 clockevent->features = CLOCK_EVT_FEAT_ONESHOT; 125 clockevent->rating = 300; 126 clockevent->set_next_event = ccount_timer_set_next_event; 127 clockevent->set_state_shutdown = ccount_timer_shutdown; 128 clockevent->set_state_oneshot = ccount_timer_set_oneshot; 129 clockevent->tick_resume = ccount_timer_set_oneshot; 130 clockevent->cpumask = cpumask_of(cpu); 131 clockevent->irq = irq_create_mapping(NULL, LINUX_TIMER_INT); 132 if (WARN(!clockevent->irq, "error: can't map timer irq")) 133 return; 134 clockevents_config_and_register(clockevent, ccount_freq, 135 0xf, 0xffffffff); 136 } 137 138 void __init time_init(void) 139 { 140 #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT 141 printk("Calibrating CPU frequency "); 142 platform_calibrate_ccount(); 143 printk("%d.%02d MHz\n", (int)ccount_freq/1000000, 144 (int)(ccount_freq/10000)%100); 145 #else 146 ccount_freq = CONFIG_XTENSA_CPU_CLOCK*1000000UL; 147 #endif 148 clocksource_register_hz(&ccount_clocksource, ccount_freq); 149 local_timer_setup(0); 150 setup_irq(this_cpu_ptr(&ccount_timer)->evt.irq, &timer_irqaction); 151 sched_clock_register(ccount_sched_clock_read, 32, ccount_freq); 152 of_clk_init(NULL); 153 clocksource_probe(); 154 } 155 156 /* 157 * The timer interrupt is called HZ times per second. 158 */ 159 160 irqreturn_t timer_interrupt(int irq, void *dev_id) 161 { 162 struct clock_event_device *evt = &this_cpu_ptr(&ccount_timer)->evt; 163 164 set_linux_timer(get_linux_timer()); 165 evt->event_handler(evt); 166 167 /* Allow platform to do something useful (Wdog). */ 168 platform_heartbeat(); 169 170 return IRQ_HANDLED; 171 } 172 173 #ifndef CONFIG_GENERIC_CALIBRATE_DELAY 174 void calibrate_delay(void) 175 { 176 loops_per_jiffy = ccount_freq / HZ; 177 printk("Calibrating delay loop (skipped)... " 178 "%lu.%02lu BogoMIPS preset\n", 179 loops_per_jiffy/(1000000/HZ), 180 (loops_per_jiffy/(10000/HZ)) % 100); 181 } 182 #endif 183