blob: a3164d1647fda73bb905d746336d0493b748dffb [file] [log] [blame]
viresh kumar986435e2010-04-01 12:30:49 +01001/*
2 * arch/arm/plat-spear/time.c
3 *
Shiraz Hashim5c881d92011-02-16 07:40:32 +01004 * Copyright (C) 2010 ST Microelectronics
viresh kumar986435e2010-04-01 12:30:49 +01005 * Shiraz Hashim<shiraz.hashim@st.com>
6 *
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
10 */
11
12#include <linux/clk.h>
13#include <linux/clockchips.h>
14#include <linux/clocksource.h>
15#include <linux/err.h>
16#include <linux/init.h>
17#include <linux/interrupt.h>
Arnd Bergmann5019f0b2012-04-11 17:30:11 +000018#include <linux/ioport.h>
viresh kumar986435e2010-04-01 12:30:49 +010019#include <linux/io.h>
20#include <linux/kernel.h>
21#include <linux/time.h>
22#include <linux/irq.h>
23#include <asm/mach/time.h>
viresh kumar986435e2010-04-01 12:30:49 +010024#include <mach/generic.h>
25
26/*
27 * We would use TIMER0 and TIMER1 as clockevent and clocksource.
28 * Timer0 and Timer1 both belong to same gpt block in cpu subbsystem. Further
29 * they share same functional clock. Any change in one's functional clock will
30 * also affect other timer.
31 */
32
33#define CLKEVT 0 /* gpt0, channel0 as clockevent */
34#define CLKSRC 1 /* gpt0, channel1 as clocksource */
35
36/* Register offsets, x is channel number */
37#define CR(x) ((x) * 0x80 + 0x80)
38#define IR(x) ((x) * 0x80 + 0x84)
39#define LOAD(x) ((x) * 0x80 + 0x88)
40#define COUNT(x) ((x) * 0x80 + 0x8C)
41
42/* Reg bit definitions */
43#define CTRL_INT_ENABLE 0x0100
44#define CTRL_ENABLE 0x0020
45#define CTRL_ONE_SHOT 0x0010
46
47#define CTRL_PRESCALER1 0x0
48#define CTRL_PRESCALER2 0x1
49#define CTRL_PRESCALER4 0x2
50#define CTRL_PRESCALER8 0x3
51#define CTRL_PRESCALER16 0x4
52#define CTRL_PRESCALER32 0x5
53#define CTRL_PRESCALER64 0x6
54#define CTRL_PRESCALER128 0x7
55#define CTRL_PRESCALER256 0x8
56
57#define INT_STATUS 0x1
58
Linus Walleij4bd48942010-07-19 20:55:46 +010059/*
60 * Minimum clocksource/clockevent timer range in seconds
61 */
62#define SPEAR_MIN_RANGE 4
63
viresh kumar986435e2010-04-01 12:30:49 +010064static __iomem void *gpt_base;
65static struct clk *gpt_clk;
66
67static void clockevent_set_mode(enum clock_event_mode mode,
68 struct clock_event_device *clk_event_dev);
69static int clockevent_next_event(unsigned long evt,
70 struct clock_event_device *clk_event_dev);
71
viresh kumar986435e2010-04-01 12:30:49 +010072static void spear_clocksource_init(void)
73{
74 u32 tick_rate;
75 u16 val;
76
77 /* program the prescaler (/256)*/
78 writew(CTRL_PRESCALER256, gpt_base + CR(CLKSRC));
79
80 /* find out actual clock driving Timer */
81 tick_rate = clk_get_rate(gpt_clk);
82 tick_rate >>= CTRL_PRESCALER256;
83
84 writew(0xFFFF, gpt_base + LOAD(CLKSRC));
85
86 val = readw(gpt_base + CR(CLKSRC));
87 val &= ~CTRL_ONE_SHOT; /* autoreload mode */
88 val |= CTRL_ENABLE ;
89 writew(val, gpt_base + CR(CLKSRC));
90
viresh kumar986435e2010-04-01 12:30:49 +010091 /* register the clocksource */
Russell Kingd6e15d72011-05-08 17:10:14 +010092 clocksource_mmio_init(gpt_base + COUNT(CLKSRC), "tmr1", tick_rate,
93 200, 16, clocksource_mmio_readw_up);
viresh kumar986435e2010-04-01 12:30:49 +010094}
95
96static struct clock_event_device clkevt = {
97 .name = "tmr0",
98 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
99 .set_mode = clockevent_set_mode,
100 .set_next_event = clockevent_next_event,
101 .shift = 0, /* to be computed */
102};
103
104static void clockevent_set_mode(enum clock_event_mode mode,
105 struct clock_event_device *clk_event_dev)
106{
107 u32 period;
108 u16 val;
109
110 /* stop the timer */
111 val = readw(gpt_base + CR(CLKEVT));
112 val &= ~CTRL_ENABLE;
113 writew(val, gpt_base + CR(CLKEVT));
114
115 switch (mode) {
116 case CLOCK_EVT_MODE_PERIODIC:
117 period = clk_get_rate(gpt_clk) / HZ;
118 period >>= CTRL_PRESCALER16;
119 writew(period, gpt_base + LOAD(CLKEVT));
120
121 val = readw(gpt_base + CR(CLKEVT));
122 val &= ~CTRL_ONE_SHOT;
123 val |= CTRL_ENABLE | CTRL_INT_ENABLE;
124 writew(val, gpt_base + CR(CLKEVT));
125
126 break;
127 case CLOCK_EVT_MODE_ONESHOT:
128 val = readw(gpt_base + CR(CLKEVT));
129 val |= CTRL_ONE_SHOT;
130 writew(val, gpt_base + CR(CLKEVT));
131
132 break;
133 case CLOCK_EVT_MODE_UNUSED:
134 case CLOCK_EVT_MODE_SHUTDOWN:
135 case CLOCK_EVT_MODE_RESUME:
136
137 break;
138 default:
139 pr_err("Invalid mode requested\n");
140 break;
141 }
142}
143
144static int clockevent_next_event(unsigned long cycles,
145 struct clock_event_device *clk_event_dev)
146{
Gilles Chanteperdrix12021372012-02-24 22:50:50 +0100147 u16 val = readw(gpt_base + CR(CLKEVT));
148
149 if (val & CTRL_ENABLE)
150 writew(val & ~CTRL_ENABLE, gpt_base + CR(CLKEVT));
viresh kumar986435e2010-04-01 12:30:49 +0100151
152 writew(cycles, gpt_base + LOAD(CLKEVT));
153
viresh kumar986435e2010-04-01 12:30:49 +0100154 val |= CTRL_ENABLE | CTRL_INT_ENABLE;
155 writew(val, gpt_base + CR(CLKEVT));
156
157 return 0;
158}
159
160static irqreturn_t spear_timer_interrupt(int irq, void *dev_id)
161{
162 struct clock_event_device *evt = &clkevt;
163
164 writew(INT_STATUS, gpt_base + IR(CLKEVT));
165
166 evt->event_handler(evt);
167
168 return IRQ_HANDLED;
169}
170
171static struct irqaction spear_timer_irq = {
172 .name = "timer",
173 .flags = IRQF_DISABLED | IRQF_TIMER,
174 .handler = spear_timer_interrupt
175};
176
Arnd Bergmann5019f0b2012-04-11 17:30:11 +0000177static void __init spear_clockevent_init(int irq)
viresh kumar986435e2010-04-01 12:30:49 +0100178{
179 u32 tick_rate;
180
181 /* program the prescaler */
182 writew(CTRL_PRESCALER16, gpt_base + CR(CLKEVT));
183
184 tick_rate = clk_get_rate(gpt_clk);
185 tick_rate >>= CTRL_PRESCALER16;
186
Linus Walleij4bd48942010-07-19 20:55:46 +0100187 clockevents_calc_mult_shift(&clkevt, tick_rate, SPEAR_MIN_RANGE);
viresh kumar986435e2010-04-01 12:30:49 +0100188
189 clkevt.max_delta_ns = clockevent_delta2ns(0xfff0,
190 &clkevt);
191 clkevt.min_delta_ns = clockevent_delta2ns(3, &clkevt);
192
193 clkevt.cpumask = cpumask_of(0);
194
195 clockevents_register_device(&clkevt);
196
Arnd Bergmann5019f0b2012-04-11 17:30:11 +0000197 setup_irq(irq, &spear_timer_irq);
viresh kumar986435e2010-04-01 12:30:49 +0100198}
199
Arnd Bergmann5019f0b2012-04-11 17:30:11 +0000200void __init spear_setup_timer(resource_size_t base, int irq)
viresh kumar986435e2010-04-01 12:30:49 +0100201{
Shiraz Hashim5c881d92011-02-16 07:40:32 +0100202 int ret;
viresh kumar986435e2010-04-01 12:30:49 +0100203
Arnd Bergmann5019f0b2012-04-11 17:30:11 +0000204 if (!request_mem_region(base, SZ_1K, "gpt0")) {
viresh kumar986435e2010-04-01 12:30:49 +0100205 pr_err("%s:cannot get IO addr\n", __func__);
206 return;
207 }
208
Arnd Bergmann5019f0b2012-04-11 17:30:11 +0000209 gpt_base = ioremap(base, SZ_1K);
viresh kumar986435e2010-04-01 12:30:49 +0100210 if (!gpt_base) {
211 pr_err("%s:ioremap failed for gpt\n", __func__);
212 goto err_mem;
213 }
214
215 gpt_clk = clk_get_sys("gpt0", NULL);
216 if (!gpt_clk) {
217 pr_err("%s:couldn't get clk for gpt\n", __func__);
218 goto err_iomap;
219 }
220
Shiraz Hashim5c881d92011-02-16 07:40:32 +0100221 ret = clk_enable(gpt_clk);
222 if (ret < 0) {
223 pr_err("%s:couldn't enable gpt clock\n", __func__);
224 goto err_clk;
viresh kumar986435e2010-04-01 12:30:49 +0100225 }
226
Arnd Bergmann5019f0b2012-04-11 17:30:11 +0000227 spear_clockevent_init(irq);
viresh kumar986435e2010-04-01 12:30:49 +0100228 spear_clocksource_init();
229
230 return;
231
Shiraz Hashim5c881d92011-02-16 07:40:32 +0100232err_clk:
233 clk_put(gpt_clk);
viresh kumar986435e2010-04-01 12:30:49 +0100234err_iomap:
235 iounmap(gpt_base);
viresh kumar986435e2010-04-01 12:30:49 +0100236err_mem:
Arnd Bergmann5019f0b2012-04-11 17:30:11 +0000237 release_mem_region(base, SZ_1K);
viresh kumar986435e2010-04-01 12:30:49 +0100238}