David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 1 | /* MN10300 Low level time management |
| 2 | * |
David Howells | 08ec3c2 | 2008-09-24 17:48:31 +0100 | [diff] [blame] | 3 | * Copyright (C) 2007-2008 Red Hat, Inc. All Rights Reserved. |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * - Derived from arch/i386/kernel/time.c |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public Licence |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the Licence, or (at your option) any later version. |
| 11 | */ |
| 12 | #include <linux/sched.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/time.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/smp.h> |
| 18 | #include <linux/profile.h> |
David Howells | 08ec3c2 | 2008-09-24 17:48:31 +0100 | [diff] [blame] | 19 | #include <linux/cnt32_to_63.h> |
Mark Salter | 730c1fa | 2010-10-27 17:28:57 +0100 | [diff] [blame^] | 20 | #include <linux/clocksource.h> |
| 21 | #include <linux/clockchips.h> |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 22 | #include <asm/irq.h> |
| 23 | #include <asm/div64.h> |
| 24 | #include <asm/processor.h> |
| 25 | #include <asm/intctl-regs.h> |
| 26 | #include <asm/rtc.h> |
Akira Takeuchi | 368dd5a | 2010-10-27 17:28:55 +0100 | [diff] [blame] | 27 | #include "internal.h" |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 28 | |
| 29 | static unsigned long mn10300_last_tsc; /* time-stamp counter at last time |
| 30 | * interrupt occurred */ |
| 31 | |
David Howells | 08ec3c2 | 2008-09-24 17:48:31 +0100 | [diff] [blame] | 32 | static unsigned long sched_clock_multiplier; |
| 33 | |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 34 | /* |
| 35 | * scheduler clock - returns current time in nanosec units. |
| 36 | */ |
| 37 | unsigned long long sched_clock(void) |
| 38 | { |
| 39 | union { |
David Howells | 08ec3c2 | 2008-09-24 17:48:31 +0100 | [diff] [blame] | 40 | unsigned long long ll; |
| 41 | unsigned l[2]; |
| 42 | } tsc64, result; |
| 43 | unsigned long tsc, tmp; |
| 44 | unsigned product[3]; /* 96-bit intermediate value */ |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 45 | |
David Howells | 3b950de | 2010-10-27 17:28:35 +0100 | [diff] [blame] | 46 | /* cnt32_to_63() is not safe with preemption */ |
| 47 | preempt_disable(); |
| 48 | |
David Howells | 08ec3c2 | 2008-09-24 17:48:31 +0100 | [diff] [blame] | 49 | /* read the TSC value |
| 50 | */ |
Mark Salter | 730c1fa | 2010-10-27 17:28:57 +0100 | [diff] [blame^] | 51 | tsc = get_cycles(); |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 52 | |
David Howells | 08ec3c2 | 2008-09-24 17:48:31 +0100 | [diff] [blame] | 53 | /* expand to 64-bits. |
| 54 | * - sched_clock() must be called once a minute or better or the |
| 55 | * following will go horribly wrong - see cnt32_to_63() |
| 56 | */ |
| 57 | tsc64.ll = cnt32_to_63(tsc) & 0x7fffffffffffffffULL; |
| 58 | |
David Howells | 3b950de | 2010-10-27 17:28:35 +0100 | [diff] [blame] | 59 | preempt_enable(); |
| 60 | |
David Howells | 08ec3c2 | 2008-09-24 17:48:31 +0100 | [diff] [blame] | 61 | /* scale the 64-bit TSC value to a nanosecond value via a 96-bit |
| 62 | * intermediate |
| 63 | */ |
| 64 | asm("mulu %2,%0,%3,%0 \n" /* LSW * mult -> 0:%3:%0 */ |
| 65 | "mulu %2,%1,%2,%1 \n" /* MSW * mult -> %2:%1:0 */ |
| 66 | "add %3,%1 \n" |
| 67 | "addc 0,%2 \n" /* result in %2:%1:%0 */ |
| 68 | : "=r"(product[0]), "=r"(product[1]), "=r"(product[2]), "=r"(tmp) |
| 69 | : "0"(tsc64.l[0]), "1"(tsc64.l[1]), "2"(sched_clock_multiplier) |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 70 | : "cc"); |
| 71 | |
David Howells | 08ec3c2 | 2008-09-24 17:48:31 +0100 | [diff] [blame] | 72 | result.l[0] = product[1] << 16 | product[0] >> 16; |
| 73 | result.l[1] = product[2] << 16 | product[1] >> 16; |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 74 | |
David Howells | 08ec3c2 | 2008-09-24 17:48:31 +0100 | [diff] [blame] | 75 | return result.ll; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * initialise the scheduler clock |
| 80 | */ |
| 81 | static void __init mn10300_sched_clock_init(void) |
| 82 | { |
| 83 | sched_clock_multiplier = |
| 84 | __muldiv64u(NSEC_PER_SEC, 1 << 16, MN10300_TSCCLK); |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Akira Takeuchi | 368dd5a | 2010-10-27 17:28:55 +0100 | [diff] [blame] | 87 | /** |
| 88 | * local_timer_interrupt - Local timer interrupt handler |
| 89 | * |
| 90 | * Handle local timer interrupts for this CPU. They may have been propagated |
| 91 | * to this CPU from the CPU that actually gets them by way of an IPI. |
| 92 | */ |
| 93 | irqreturn_t local_timer_interrupt(void) |
| 94 | { |
| 95 | profile_tick(CPU_PROFILING); |
| 96 | update_process_times(user_mode(get_irq_regs())); |
| 97 | return IRQ_HANDLED; |
| 98 | } |
| 99 | |
Mark Salter | 730c1fa | 2010-10-27 17:28:57 +0100 | [diff] [blame^] | 100 | #ifndef CONFIG_GENERIC_TIME |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 101 | /* |
| 102 | * advance the kernel's time keeping clocks (xtime and jiffies) |
| 103 | * - we use Timer 0 & 1 cascaded as a clock to nudge us the next time |
| 104 | * there's a need to update |
| 105 | */ |
| 106 | static irqreturn_t timer_interrupt(int irq, void *dev_id) |
| 107 | { |
| 108 | unsigned tsc, elapse; |
Akira Takeuchi | 368dd5a | 2010-10-27 17:28:55 +0100 | [diff] [blame] | 109 | irqreturn_t ret; |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 110 | |
| 111 | write_seqlock(&xtime_lock); |
| 112 | |
| 113 | while (tsc = get_cycles(), |
Mark Salter | 730c1fa | 2010-10-27 17:28:57 +0100 | [diff] [blame^] | 114 | elapse = tsc - mn10300_last_tsc, /* time elapsed since last |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 115 | * tick */ |
| 116 | elapse > MN10300_TSC_PER_HZ |
| 117 | ) { |
Mark Salter | 730c1fa | 2010-10-27 17:28:57 +0100 | [diff] [blame^] | 118 | mn10300_last_tsc += MN10300_TSC_PER_HZ; |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 119 | |
| 120 | /* advance the kernel's time tracking system */ |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 121 | do_timer(1); |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | write_sequnlock(&xtime_lock); |
David Howells | 2b79aac | 2008-02-19 18:58:49 +0000 | [diff] [blame] | 125 | |
Akira Takeuchi | 368dd5a | 2010-10-27 17:28:55 +0100 | [diff] [blame] | 126 | ret = local_timer_interrupt(); |
| 127 | #ifdef CONFIG_SMP |
| 128 | send_IPI_allbutself(LOCAL_TIMER_IPI); |
| 129 | #endif |
| 130 | return ret; |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Mark Salter | 730c1fa | 2010-10-27 17:28:57 +0100 | [diff] [blame^] | 133 | static struct irqaction timer_irq = { |
| 134 | .handler = timer_interrupt, |
| 135 | .flags = IRQF_DISABLED | IRQF_SHARED | IRQF_TIMER, |
| 136 | .name = "timer", |
| 137 | }; |
| 138 | #endif /* CONFIG_GENERIC_TIME */ |
| 139 | |
| 140 | #ifdef CONFIG_CSRC_MN10300 |
| 141 | void __init clocksource_set_clock(struct clocksource *cs, unsigned int clock) |
| 142 | { |
| 143 | u64 temp; |
| 144 | u32 shift; |
| 145 | |
| 146 | /* Find a shift value */ |
| 147 | for (shift = 32; shift > 0; shift--) { |
| 148 | temp = (u64) NSEC_PER_SEC << shift; |
| 149 | do_div(temp, clock); |
| 150 | if ((temp >> 32) == 0) |
| 151 | break; |
| 152 | } |
| 153 | cs->shift = shift; |
| 154 | cs->mult = (u32) temp; |
| 155 | } |
| 156 | #endif |
| 157 | |
| 158 | #if CONFIG_CEVT_MN10300 |
| 159 | void __cpuinit clockevent_set_clock(struct clock_event_device *cd, |
| 160 | unsigned int clock) |
| 161 | { |
| 162 | u64 temp; |
| 163 | u32 shift; |
| 164 | |
| 165 | /* Find a shift value */ |
| 166 | for (shift = 32; shift > 0; shift--) { |
| 167 | temp = (u64) clock << shift; |
| 168 | do_div(temp, NSEC_PER_SEC); |
| 169 | if ((temp >> 32) == 0) |
| 170 | break; |
| 171 | } |
| 172 | cd->shift = shift; |
| 173 | cd->mult = (u32) temp; |
| 174 | } |
| 175 | #endif |
| 176 | |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 177 | /* |
| 178 | * initialise the various timers used by the main part of the kernel |
| 179 | */ |
| 180 | void __init time_init(void) |
| 181 | { |
| 182 | /* we need the prescalar running to be able to use IOCLK/8 |
| 183 | * - IOCLK runs at 1/4 (ST5 open) or 1/8 (ST5 closed) internal CPU clock |
| 184 | * - IOCLK runs at Fosc rate (crystal speed) |
| 185 | */ |
| 186 | TMPSCNT |= TMPSCNT_ENABLE; |
| 187 | |
Mark Salter | 730c1fa | 2010-10-27 17:28:57 +0100 | [diff] [blame^] | 188 | #ifdef CONFIG_GENERIC_TIME |
| 189 | init_clocksource(); |
| 190 | #else |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 191 | startup_timestamp_counter(); |
Mark Salter | 730c1fa | 2010-10-27 17:28:57 +0100 | [diff] [blame^] | 192 | #endif |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 193 | |
| 194 | printk(KERN_INFO |
| 195 | "timestamp counter I/O clock running at %lu.%02lu" |
| 196 | " (calibrated against RTC)\n", |
| 197 | MN10300_TSCCLK / 1000000, (MN10300_TSCCLK / 10000) % 100); |
| 198 | |
Mark Salter | 730c1fa | 2010-10-27 17:28:57 +0100 | [diff] [blame^] | 199 | mn10300_last_tsc = read_timestamp_counter(); |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 200 | |
Mark Salter | 730c1fa | 2010-10-27 17:28:57 +0100 | [diff] [blame^] | 201 | #ifdef CONFIG_GENERIC_CLOCKEVENTS |
| 202 | init_clockevents(); |
| 203 | #else |
| 204 | reload_jiffies_counter(MN10300_JC_PER_HZ - 1); |
| 205 | setup_jiffies_interrupt(TMJCIRQ, &timer_irq, CONFIG_TIMER_IRQ_LEVEL); |
| 206 | #endif |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 207 | |
| 208 | #ifdef CONFIG_MN10300_WD_TIMER |
| 209 | /* start the watchdog timer */ |
| 210 | watchdog_go(); |
| 211 | #endif |
David Howells | 08ec3c2 | 2008-09-24 17:48:31 +0100 | [diff] [blame] | 212 | |
| 213 | mn10300_sched_clock_init(); |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 214 | } |