blob: 1ce8cb1e4982659dfefd7f8e9de52282c58c3e42 [file] [log] [blame]
Vitja Makarov8b5f79f2008-02-29 12:24:23 +08001/*
2 * linux/arch/kernel/time-ts.c
3 *
4 * Based on arm clockevents implementation and old bfin time tick.
5 *
6 * Copyright(C) 2008, GeoTechnologies, Vitja Makarov
7 *
8 * This code is licenced under the GPL version 2. For details see
9 * kernel-base/COPYING.
10 */
11#include <linux/module.h>
12#include <linux/profile.h>
13#include <linux/interrupt.h>
14#include <linux/time.h>
Mike Frysinger764cb812008-04-24 05:07:29 +080015#include <linux/timex.h>
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080016#include <linux/irq.h>
17#include <linux/clocksource.h>
18#include <linux/clockchips.h>
19
20#include <asm/blackfin.h>
21
22#ifdef CONFIG_CYCLES_CLOCKSOURCE
23
24static unsigned long cyc2ns_scale;
25#define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
26
27static inline void set_cyc2ns_scale(unsigned long cpu_khz)
28{
29 cyc2ns_scale = (1000000 << CYC2NS_SCALE_FACTOR) / cpu_khz;
30}
31
32static inline unsigned long long cycles_2_ns(cycle_t cyc)
33{
34 return (cyc * cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
35}
36
37static cycle_t read_cycles(void)
38{
Mike Frysinger764cb812008-04-24 05:07:29 +080039 return get_cycles();
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080040}
41
42unsigned long long sched_clock(void)
43{
44 return cycles_2_ns(read_cycles());
45}
46
47static struct clocksource clocksource_bfin = {
48 .name = "bfin_cycles",
49 .rating = 350,
50 .read = read_cycles,
51 .mask = CLOCKSOURCE_MASK(64),
52 .shift = 22,
53 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
54};
55
56static int __init bfin_clocksource_init(void)
57{
58 set_cyc2ns_scale(get_cclk() / 1000);
59
60 clocksource_bfin.mult = clocksource_hz2mult(get_cclk(), clocksource_bfin.shift);
61
62 if (clocksource_register(&clocksource_bfin))
63 panic("failed to register clocksource");
64
65 return 0;
66}
67
68#else
69# define bfin_clocksource_init()
70#endif
71
72static int bfin_timer_set_next_event(unsigned long cycles,
73 struct clock_event_device *evt)
74{
75 bfin_write_TCOUNT(cycles);
76 CSYNC();
77 return 0;
78}
79
80static void bfin_timer_set_mode(enum clock_event_mode mode,
81 struct clock_event_device *evt)
82{
83 switch (mode) {
84 case CLOCK_EVT_MODE_PERIODIC: {
85 unsigned long tcount = ((get_cclk() / (HZ * 1)) - 1);
86 bfin_write_TCNTL(TMPWR);
87 CSYNC();
88 bfin_write_TPERIOD(tcount);
89 bfin_write_TCOUNT(tcount);
90 bfin_write_TCNTL(TMPWR | TMREN | TAUTORLD);
91 CSYNC();
92 break;
93 }
94 case CLOCK_EVT_MODE_ONESHOT:
95 bfin_write_TCOUNT(0);
96 bfin_write_TCNTL(TMPWR | TMREN);
97 CSYNC();
98 break;
99 case CLOCK_EVT_MODE_UNUSED:
100 case CLOCK_EVT_MODE_SHUTDOWN:
101 bfin_write_TCNTL(0);
102 CSYNC();
103 break;
104 case CLOCK_EVT_MODE_RESUME:
105 break;
106 }
107}
108
109static void __init bfin_timer_init(void)
110{
111 /* power up the timer, but don't enable it just yet */
112 bfin_write_TCNTL(TMPWR);
113 CSYNC();
114
115 /*
116 * the TSCALE prescaler counter.
117 */
118 bfin_write_TSCALE(0);
119 bfin_write_TPERIOD(0);
120 bfin_write_TCOUNT(0);
121
122 /* now enable the timer */
123 CSYNC();
124}
125
126/*
127 * timer_interrupt() needs to keep up the real-time clock,
128 * as well as call the "do_timer()" routine every clocktick
129 */
130#ifdef CONFIG_CORE_TIMER_IRQ_L1
131__attribute__((l1_text))
132#endif
133irqreturn_t timer_interrupt(int irq, void *dev_id);
134
135static struct clock_event_device clockevent_bfin = {
136 .name = "bfin_core_timer",
137 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
138 .shift = 32,
139 .cpumask = CPU_MASK_CPU0,
140 .set_next_event = bfin_timer_set_next_event,
141 .set_mode = bfin_timer_set_mode,
142};
143
144static struct irqaction bfin_timer_irq = {
145 .name = "Blackfin Core Timer",
146 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
147 .handler = timer_interrupt,
148 .dev_id = &clockevent_bfin,
149};
150
151irqreturn_t timer_interrupt(int irq, void *dev_id)
152{
153 struct clock_event_device *evt = dev_id;
154 evt->event_handler(evt);
155 return IRQ_HANDLED;
156}
157
158static int __init bfin_clockevent_init(void)
159{
160 setup_irq(IRQ_CORETMR, &bfin_timer_irq);
161 bfin_timer_init();
162
163 clockevent_bfin.mult = div_sc(get_cclk(), NSEC_PER_SEC, clockevent_bfin.shift);
164 clockevent_bfin.max_delta_ns = clockevent_delta2ns(-1, &clockevent_bfin);
165 clockevent_bfin.min_delta_ns = clockevent_delta2ns(100, &clockevent_bfin);
166 clockevents_register_device(&clockevent_bfin);
167
168 return 0;
169}
170
171void __init time_init(void)
172{
173 time_t secs_since_1970 = (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */
174
175#ifdef CONFIG_RTC_DRV_BFIN
176 /* [#2663] hack to filter junk RTC values that would cause
177 * userspace to have to deal with time values greater than
178 * 2^31 seconds (which uClibc cannot cope with yet)
179 */
180 if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) {
181 printk(KERN_NOTICE "bfin-rtc: invalid date; resetting\n");
182 bfin_write_RTC_STAT(0);
183 }
184#endif
185
186 /* Initialize xtime. From now on, xtime is updated with timer interrupts */
187 xtime.tv_sec = secs_since_1970;
188 xtime.tv_nsec = 0;
189 set_normalized_timespec(&wall_to_monotonic, -xtime.tv_sec, -xtime.tv_nsec);
190
191 bfin_clocksource_init();
192 bfin_clockevent_init();
193}