]> Pileus Git - ~andy/linux/blob - arch/arm/plat-spear/time.c
Merge tag 'drivers' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[~andy/linux] / arch / arm / plat-spear / time.c
1 /*
2  * arch/arm/plat-spear/time.c
3  *
4  * Copyright (C) 2010 ST Microelectronics
5  * 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>
18 #include <linux/ioport.h>
19 #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>
24 #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
59 /*
60  * Minimum clocksource/clockevent timer range in seconds
61  */
62 #define SPEAR_MIN_RANGE 4
63
64 static __iomem void *gpt_base;
65 static struct clk *gpt_clk;
66
67 static void clockevent_set_mode(enum clock_event_mode mode,
68                                 struct clock_event_device *clk_event_dev);
69 static int clockevent_next_event(unsigned long evt,
70                                  struct clock_event_device *clk_event_dev);
71
72 static 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
91         /* register the clocksource */
92         clocksource_mmio_init(gpt_base + COUNT(CLKSRC), "tmr1", tick_rate,
93                 200, 16, clocksource_mmio_readw_up);
94 }
95
96 static 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
104 static 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
144 static int clockevent_next_event(unsigned long cycles,
145                                  struct clock_event_device *clk_event_dev)
146 {
147         u16 val = readw(gpt_base + CR(CLKEVT));
148
149         if (val & CTRL_ENABLE)
150                 writew(val & ~CTRL_ENABLE, gpt_base + CR(CLKEVT));
151
152         writew(cycles, gpt_base + LOAD(CLKEVT));
153
154         val |= CTRL_ENABLE | CTRL_INT_ENABLE;
155         writew(val, gpt_base + CR(CLKEVT));
156
157         return 0;
158 }
159
160 static 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
171 static struct irqaction spear_timer_irq = {
172         .name = "timer",
173         .flags = IRQF_DISABLED | IRQF_TIMER,
174         .handler = spear_timer_interrupt
175 };
176
177 static void __init spear_clockevent_init(int irq)
178 {
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
187         clockevents_calc_mult_shift(&clkevt, tick_rate, SPEAR_MIN_RANGE);
188
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
197         setup_irq(irq, &spear_timer_irq);
198 }
199
200 void __init spear_setup_timer(resource_size_t base, int irq)
201 {
202         int ret;
203
204         if (!request_mem_region(base, SZ_1K, "gpt0")) {
205                 pr_err("%s:cannot get IO addr\n", __func__);
206                 return;
207         }
208
209         gpt_base = ioremap(base, SZ_1K);
210         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
221         ret = clk_enable(gpt_clk);
222         if (ret < 0) {
223                 pr_err("%s:couldn't enable gpt clock\n", __func__);
224                 goto err_clk;
225         }
226
227         spear_clockevent_init(irq);
228         spear_clocksource_init();
229
230         return;
231
232 err_clk:
233         clk_put(gpt_clk);
234 err_iomap:
235         iounmap(gpt_base);
236 err_mem:
237         release_mem_region(base, SZ_1K);
238 }