Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * File: arch/blackfin/kernel/time.c |
| 3 | * Based on: none - original work |
| 4 | * Author: |
| 5 | * |
| 6 | * Created: |
| 7 | * Description: This file contains the bfin-specific time handling details. |
| 8 | * Most of the stuff is located in the machine specific files. |
| 9 | * |
| 10 | * Modified: |
| 11 | * Copyright 2004-2006 Analog Devices Inc. |
| 12 | * |
| 13 | * Bugs: Enter bugs at http://blackfin.uclinux.org/ |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License as published by |
| 17 | * the Free Software Foundation; either version 2 of the License, or |
| 18 | * (at your option) any later version. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | * GNU General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License |
| 26 | * along with this program; if not, see the file COPYING, or write |
| 27 | * to the Free Software Foundation, Inc., |
| 28 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 29 | */ |
| 30 | |
| 31 | #include <linux/module.h> |
| 32 | #include <linux/profile.h> |
| 33 | #include <linux/interrupt.h> |
| 34 | #include <linux/time.h> |
| 35 | #include <linux/irq.h> |
| 36 | |
| 37 | #include <asm/blackfin.h> |
| 38 | |
| 39 | /* This is an NTP setting */ |
| 40 | #define TICK_SIZE (tick_nsec / 1000) |
| 41 | |
| 42 | static void time_sched_init(irqreturn_t(*timer_routine) |
| 43 | (int, void *)); |
| 44 | static unsigned long gettimeoffset(void); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 45 | |
| 46 | static struct irqaction bfin_timer_irq = { |
| 47 | .name = "BFIN Timer Tick", |
| 48 | .flags = IRQF_DISABLED |
| 49 | }; |
| 50 | |
| 51 | /* |
| 52 | * The way that the Blackfin core timer works is: |
| 53 | * - CCLK is divided by a programmable 8-bit pre-scaler (TSCALE) |
| 54 | * - Every time TSCALE ticks, a 32bit is counted down (TCOUNT) |
| 55 | * |
| 56 | * If you take the fastest clock (1ns, or 1GHz to make the math work easier) |
| 57 | * 10ms is 10,000,000 clock ticks, which fits easy into a 32-bit counter |
| 58 | * (32 bit counter is 4,294,967,296ns or 4.2 seconds) so, we don't need |
| 59 | * to use TSCALE, and program it to zero (which is pass CCLK through). |
| 60 | * If you feel like using it, try to keep HZ * TIMESCALE to some |
| 61 | * value that divides easy (like power of 2). |
| 62 | */ |
| 63 | |
| 64 | #define TIME_SCALE 1 |
| 65 | |
| 66 | static void |
| 67 | time_sched_init(irqreturn_t(*timer_routine) (int, void *)) |
| 68 | { |
| 69 | u32 tcount; |
| 70 | |
| 71 | /* power up the timer, but don't enable it just yet */ |
| 72 | bfin_write_TCNTL(1); |
| 73 | CSYNC(); |
| 74 | |
| 75 | /* |
| 76 | * the TSCALE prescaler counter. |
| 77 | */ |
| 78 | bfin_write_TSCALE((TIME_SCALE - 1)); |
| 79 | |
| 80 | tcount = ((get_cclk() / (HZ * TIME_SCALE)) - 1); |
| 81 | bfin_write_TPERIOD(tcount); |
| 82 | bfin_write_TCOUNT(tcount); |
| 83 | |
| 84 | /* now enable the timer */ |
| 85 | CSYNC(); |
| 86 | |
| 87 | bfin_write_TCNTL(7); |
| 88 | |
| 89 | bfin_timer_irq.handler = (irq_handler_t)timer_routine; |
| 90 | /* call setup_irq instead of request_irq because request_irq calls |
| 91 | * kmalloc which has not been initialized yet |
| 92 | */ |
| 93 | setup_irq(IRQ_CORETMR, &bfin_timer_irq); |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Should return useconds since last timer tick |
| 98 | */ |
| 99 | static unsigned long gettimeoffset(void) |
| 100 | { |
| 101 | unsigned long offset; |
| 102 | unsigned long clocks_per_jiffy; |
| 103 | |
| 104 | clocks_per_jiffy = bfin_read_TPERIOD(); |
| 105 | offset = |
| 106 | (clocks_per_jiffy - |
| 107 | bfin_read_TCOUNT()) / (((clocks_per_jiffy + 1) * HZ) / |
| 108 | USEC_PER_SEC); |
| 109 | |
| 110 | /* Check if we just wrapped the counters and maybe missed a tick */ |
| 111 | if ((bfin_read_ILAT() & (1 << IRQ_CORETMR)) |
| 112 | && (offset < (100000 / HZ / 2))) |
| 113 | offset += (USEC_PER_SEC / HZ); |
| 114 | |
| 115 | return offset; |
| 116 | } |
| 117 | |
| 118 | static inline int set_rtc_mmss(unsigned long nowtime) |
| 119 | { |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * timer_interrupt() needs to keep up the real-time clock, |
| 125 | * as well as call the "do_timer()" routine every clocktick |
| 126 | */ |
| 127 | #ifdef CONFIG_CORE_TIMER_IRQ_L1 |
| 128 | irqreturn_t timer_interrupt(int irq, void *dummy)__attribute__((l1_text)); |
| 129 | #endif |
| 130 | |
| 131 | irqreturn_t timer_interrupt(int irq, void *dummy) |
| 132 | { |
| 133 | /* last time the cmos clock got updated */ |
Mike Frysinger | 1f83b8f | 2007-07-12 22:58:21 +0800 | [diff] [blame] | 134 | static long last_rtc_update; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 135 | |
| 136 | write_seqlock(&xtime_lock); |
| 137 | |
| 138 | do_timer(1); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 139 | |
| 140 | #ifndef CONFIG_SMP |
| 141 | update_process_times(user_mode(get_irq_regs())); |
| 142 | #endif |
| 143 | profile_tick(CPU_PROFILING); |
| 144 | |
| 145 | /* |
| 146 | * If we have an externally synchronized Linux clock, then update |
| 147 | * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be |
| 148 | * called as close as possible to 500 ms before the new second starts. |
| 149 | */ |
| 150 | |
| 151 | if (ntp_synced() && |
| 152 | xtime.tv_sec > last_rtc_update + 660 && |
| 153 | (xtime.tv_nsec / NSEC_PER_USEC) >= |
| 154 | 500000 - ((unsigned)TICK_SIZE) / 2 |
| 155 | && (xtime.tv_nsec / NSEC_PER_USEC) <= |
| 156 | 500000 + ((unsigned)TICK_SIZE) / 2) { |
| 157 | if (set_rtc_mmss(xtime.tv_sec) == 0) |
| 158 | last_rtc_update = xtime.tv_sec; |
| 159 | else |
| 160 | /* Do it again in 60s. */ |
| 161 | last_rtc_update = xtime.tv_sec - 600; |
| 162 | } |
| 163 | write_sequnlock(&xtime_lock); |
| 164 | return IRQ_HANDLED; |
| 165 | } |
| 166 | |
| 167 | void __init time_init(void) |
| 168 | { |
| 169 | time_t secs_since_1970 = (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */ |
| 170 | |
| 171 | #ifdef CONFIG_RTC_DRV_BFIN |
| 172 | /* [#2663] hack to filter junk RTC values that would cause |
| 173 | * userspace to have to deal with time values greater than |
| 174 | * 2^31 seconds (which uClibc cannot cope with yet) |
| 175 | */ |
| 176 | if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) { |
| 177 | printk(KERN_NOTICE "bfin-rtc: invalid date; resetting\n"); |
| 178 | bfin_write_RTC_STAT(0); |
| 179 | } |
| 180 | #endif |
| 181 | |
| 182 | /* Initialize xtime. From now on, xtime is updated with timer interrupts */ |
| 183 | xtime.tv_sec = secs_since_1970; |
| 184 | xtime.tv_nsec = 0; |
| 185 | |
| 186 | wall_to_monotonic.tv_sec = -xtime.tv_sec; |
| 187 | |
| 188 | time_sched_init(timer_interrupt); |
| 189 | } |
| 190 | |
| 191 | #ifndef CONFIG_GENERIC_TIME |
| 192 | void do_gettimeofday(struct timeval *tv) |
| 193 | { |
| 194 | unsigned long flags; |
| 195 | unsigned long seq; |
| 196 | unsigned long usec, sec; |
| 197 | |
| 198 | do { |
| 199 | seq = read_seqbegin_irqsave(&xtime_lock, flags); |
| 200 | usec = gettimeoffset(); |
| 201 | sec = xtime.tv_sec; |
| 202 | usec += (xtime.tv_nsec / NSEC_PER_USEC); |
| 203 | } |
| 204 | while (read_seqretry_irqrestore(&xtime_lock, seq, flags)); |
| 205 | |
| 206 | while (usec >= USEC_PER_SEC) { |
| 207 | usec -= USEC_PER_SEC; |
| 208 | sec++; |
| 209 | } |
| 210 | |
| 211 | tv->tv_sec = sec; |
| 212 | tv->tv_usec = usec; |
| 213 | } |
| 214 | EXPORT_SYMBOL(do_gettimeofday); |
| 215 | |
| 216 | int do_settimeofday(struct timespec *tv) |
| 217 | { |
| 218 | time_t wtm_sec, sec = tv->tv_sec; |
| 219 | long wtm_nsec, nsec = tv->tv_nsec; |
| 220 | |
| 221 | if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC) |
| 222 | return -EINVAL; |
| 223 | |
| 224 | write_seqlock_irq(&xtime_lock); |
| 225 | /* |
| 226 | * This is revolting. We need to set the xtime.tv_usec |
| 227 | * correctly. However, the value in this location is |
| 228 | * is value at the last tick. |
| 229 | * Discover what correction gettimeofday |
| 230 | * would have done, and then undo it! |
| 231 | */ |
| 232 | nsec -= (gettimeoffset() * NSEC_PER_USEC); |
| 233 | |
| 234 | wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec); |
| 235 | wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec); |
| 236 | |
| 237 | set_normalized_timespec(&xtime, sec, nsec); |
| 238 | set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec); |
| 239 | |
| 240 | ntp_clear(); |
| 241 | |
| 242 | write_sequnlock_irq(&xtime_lock); |
| 243 | clock_was_set(); |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | EXPORT_SYMBOL(do_settimeofday); |
| 248 | #endif /* !CONFIG_GENERIC_TIME */ |
| 249 | |
| 250 | /* |
| 251 | * Scheduler clock - returns current time in nanosec units. |
| 252 | */ |
| 253 | unsigned long long sched_clock(void) |
| 254 | { |
| 255 | return (unsigned long long)jiffies *(NSEC_PER_SEC / HZ); |
| 256 | } |