blob: ceb2bf63dfe200c091e8276bb322a3cf01c75da6 [file] [log] [blame]
Bryan Wu1394f032007-05-06 14:50:22 -07001/*
Mike Frysinger1b047d82008-11-18 17:48:22 +08002 * arch/blackfin/kernel/time.c
Bryan Wu1394f032007-05-06 14:50:22 -07003 *
Mike Frysinger1b047d82008-11-18 17:48:22 +08004 * This file contains the Blackfin-specific time handling details.
5 * Most of the stuff is located in the machine specific files.
Bryan Wu1394f032007-05-06 14:50:22 -07006 *
Mike Frysinger1b047d82008-11-18 17:48:22 +08007 * Copyright 2004-2008 Analog Devices Inc.
8 * Licensed under the GPL-2 or later.
Bryan Wu1394f032007-05-06 14:50:22 -07009 */
10
11#include <linux/module.h>
12#include <linux/profile.h>
13#include <linux/interrupt.h>
14#include <linux/time.h>
15#include <linux/irq.h>
Graf Yang8f658732008-11-18 17:48:22 +080016#include <linux/delay.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040017#include <linux/sched.h>
Bryan Wu1394f032007-05-06 14:50:22 -070018
19#include <asm/blackfin.h>
Michael Henneriche6c91b62008-04-25 04:58:29 +080020#include <asm/time.h>
Graf Yang8f658732008-11-18 17:48:22 +080021#include <asm/gptimers.h>
Bryan Wu1394f032007-05-06 14:50:22 -070022
23/* This is an NTP setting */
24#define TICK_SIZE (tick_nsec / 1000)
25
Bryan Wu1394f032007-05-06 14:50:22 -070026static struct irqaction bfin_timer_irq = {
Mike Frysinger1b047d82008-11-18 17:48:22 +080027 .name = "Blackfin Timer Tick",
Bryan Wu1394f032007-05-06 14:50:22 -070028 .flags = IRQF_DISABLED
29};
30
Graf Yang9b9bfde2009-05-27 09:58:35 +000031#if defined(CONFIG_IPIPE)
Mike Frysingera1ee74c2009-01-07 23:14:38 +080032void __init setup_system_timer0(void)
Graf Yang8f658732008-11-18 17:48:22 +080033{
34 /* Power down the core timer, just to play safe. */
35 bfin_write_TCNTL(0);
36
37 disable_gptimers(TIMER0bit);
38 set_gptimer_status(0, TIMER_STATUS_TRUN0);
39 while (get_gptimer_status(0) & TIMER_STATUS_TRUN0)
40 udelay(10);
41
42 set_gptimer_config(0, 0x59); /* IRQ enable, periodic, PWM_OUT, SCLKed, OUT PAD disabled */
43 set_gptimer_period(TIMER0_id, get_sclk() / HZ);
44 set_gptimer_pwidth(TIMER0_id, 1);
45 SSYNC();
46 enable_gptimers(TIMER0bit);
47}
Mike Frysinger1b047d82008-11-18 17:48:22 +080048#else
Mike Frysingera1ee74c2009-01-07 23:14:38 +080049void __init setup_core_timer(void)
Mike Frysinger1b047d82008-11-18 17:48:22 +080050{
51 u32 tcount;
52
53 /* power up the timer, but don't enable it just yet */
Mike Frysinger072a5cf2011-05-29 23:11:42 -040054 bfin_write_TCNTL(TMPWR);
Mike Frysinger1b047d82008-11-18 17:48:22 +080055 CSYNC();
56
57 /* the TSCALE prescaler counter */
58 bfin_write_TSCALE(TIME_SCALE - 1);
59
60 tcount = ((get_cclk() / (HZ * TIME_SCALE)) - 1);
61 bfin_write_TPERIOD(tcount);
62 bfin_write_TCOUNT(tcount);
63
64 /* now enable the timer */
65 CSYNC();
66
Mike Frysinger072a5cf2011-05-29 23:11:42 -040067 bfin_write_TCNTL(TAUTORLD | TMREN | TMPWR);
Mike Frysinger1b047d82008-11-18 17:48:22 +080068}
Graf Yang8f658732008-11-18 17:48:22 +080069#endif
70
Mike Frysingera1ee74c2009-01-07 23:14:38 +080071static void __init
Graf Yang8f658732008-11-18 17:48:22 +080072time_sched_init(irqreturn_t(*timer_routine) (int, void *))
73{
Graf Yang9b9bfde2009-05-27 09:58:35 +000074#if defined(CONFIG_IPIPE)
Graf Yang8f658732008-11-18 17:48:22 +080075 setup_system_timer0();
Mike Frysinger1b047d82008-11-18 17:48:22 +080076 bfin_timer_irq.handler = timer_routine;
Graf Yang8f658732008-11-18 17:48:22 +080077 setup_irq(IRQ_TIMER0, &bfin_timer_irq);
78#else
Mike Frysinger1b047d82008-11-18 17:48:22 +080079 setup_core_timer();
80 bfin_timer_irq.handler = timer_routine;
Bryan Wu1394f032007-05-06 14:50:22 -070081 setup_irq(IRQ_CORETMR, &bfin_timer_irq);
Graf Yang8f658732008-11-18 17:48:22 +080082#endif
Bryan Wu1394f032007-05-06 14:50:22 -070083}
84
john stultz10f03f12009-09-15 21:17:19 -070085#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
Bryan Wu1394f032007-05-06 14:50:22 -070086/*
87 * Should return useconds since last timer tick
88 */
john stultz10f03f12009-09-15 21:17:19 -070089u32 arch_gettimeoffset(void)
Bryan Wu1394f032007-05-06 14:50:22 -070090{
91 unsigned long offset;
92 unsigned long clocks_per_jiffy;
93
Graf Yang9b9bfde2009-05-27 09:58:35 +000094#if defined(CONFIG_IPIPE)
Mike Frysinger1b047d82008-11-18 17:48:22 +080095 clocks_per_jiffy = bfin_read_TIMER0_PERIOD();
96 offset = bfin_read_TIMER0_COUNTER() / \
Graf Yang8f658732008-11-18 17:48:22 +080097 (((clocks_per_jiffy + 1) * HZ) / USEC_PER_SEC);
98
99 if ((get_gptimer_status(0) & TIMER_STATUS_TIMIL0) && offset < (100000 / HZ / 2))
100 offset += (USEC_PER_SEC / HZ);
101#else
Bryan Wu1394f032007-05-06 14:50:22 -0700102 clocks_per_jiffy = bfin_read_TPERIOD();
Graf Yang8f658732008-11-18 17:48:22 +0800103 offset = (clocks_per_jiffy - bfin_read_TCOUNT()) / \
Mike Frysinger1b047d82008-11-18 17:48:22 +0800104 (((clocks_per_jiffy + 1) * HZ) / USEC_PER_SEC);
Bryan Wu1394f032007-05-06 14:50:22 -0700105
106 /* Check if we just wrapped the counters and maybe missed a tick */
107 if ((bfin_read_ILAT() & (1 << IRQ_CORETMR))
Graf Yang8f658732008-11-18 17:48:22 +0800108 && (offset < (100000 / HZ / 2)))
Bryan Wu1394f032007-05-06 14:50:22 -0700109 offset += (USEC_PER_SEC / HZ);
Graf Yang8f658732008-11-18 17:48:22 +0800110#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700111 return offset;
112}
Mike Frysinger1b047d82008-11-18 17:48:22 +0800113#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700114
Bryan Wu1394f032007-05-06 14:50:22 -0700115/*
116 * timer_interrupt() needs to keep up the real-time clock,
Torben Hohn4196b892011-01-27 15:59:31 +0100117 * as well as call the "xtime_update()" routine every clocktick
Bryan Wu1394f032007-05-06 14:50:22 -0700118 */
119#ifdef CONFIG_CORE_TIMER_IRQ_L1
Mike Frysinger1b047d82008-11-18 17:48:22 +0800120__attribute__((l1_text))
Bryan Wu1394f032007-05-06 14:50:22 -0700121#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700122irqreturn_t timer_interrupt(int irq, void *dummy)
123{
Torben Hohn4196b892011-01-27 15:59:31 +0100124 xtime_update(1);
Peter Zijlstraaa02cd22008-02-13 21:33:16 +0100125
Yi Li6a01f232009-01-07 23:14:39 +0800126#ifdef CONFIG_IPIPE
127 update_root_process_times(get_irq_regs());
128#else
Peter Zijlstraaa02cd22008-02-13 21:33:16 +0100129 update_process_times(user_mode(get_irq_regs()));
Yi Li6a01f232009-01-07 23:14:39 +0800130#endif
Graf Yang8f658732008-11-18 17:48:22 +0800131 profile_tick(CPU_PROFILING);
Peter Zijlstraaa02cd22008-02-13 21:33:16 +0100132
Bryan Wu1394f032007-05-06 14:50:22 -0700133 return IRQ_HANDLED;
134}
135
John Stultzcb0e9962010-03-03 19:57:24 -0800136void read_persistent_clock(struct timespec *ts)
Bryan Wu1394f032007-05-06 14:50:22 -0700137{
138 time_t secs_since_1970 = (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */
John Stultzcb0e9962010-03-03 19:57:24 -0800139 ts->tv_sec = secs_since_1970;
140 ts->tv_nsec = 0;
141}
Bryan Wu1394f032007-05-06 14:50:22 -0700142
John Stultzcb0e9962010-03-03 19:57:24 -0800143void __init time_init(void)
144{
Bryan Wu1394f032007-05-06 14:50:22 -0700145#ifdef CONFIG_RTC_DRV_BFIN
146 /* [#2663] hack to filter junk RTC values that would cause
147 * userspace to have to deal with time values greater than
148 * 2^31 seconds (which uClibc cannot cope with yet)
149 */
150 if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) {
151 printk(KERN_NOTICE "bfin-rtc: invalid date; resetting\n");
152 bfin_write_RTC_STAT(0);
153 }
154#endif
155
Bryan Wu1394f032007-05-06 14:50:22 -0700156 time_sched_init(timer_interrupt);
157}