2 * Copyright (C) 2013-2014 Altera Corporation
3 * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
4 * Copyright (C) 2004 Microtronix Datacom Ltd.
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
11 #include <linux/export.h>
12 #include <linux/interrupt.h>
13 #include <linux/clockchips.h>
14 #include <linux/clocksource.h>
15 #include <linux/delay.h>
17 #include <linux/of_address.h>
18 #include <linux/of_irq.h>
20 #include <linux/slab.h>
22 #define ALTR_TIMER_COMPATIBLE "altr,timer-1.0"
24 #define ALTERA_TIMER_STATUS_REG 0
25 #define ALTERA_TIMER_CONTROL_REG 4
26 #define ALTERA_TIMER_PERIODL_REG 8
27 #define ALTERA_TIMER_PERIODH_REG 12
28 #define ALTERA_TIMER_SNAPL_REG 16
29 #define ALTERA_TIMER_SNAPH_REG 20
31 #define ALTERA_TIMER_CONTROL_ITO_MSK (0x1)
32 #define ALTERA_TIMER_CONTROL_CONT_MSK (0x2)
33 #define ALTERA_TIMER_CONTROL_START_MSK (0x4)
34 #define ALTERA_TIMER_CONTROL_STOP_MSK (0x8)
41 struct nios2_clockevent_dev
{
42 struct nios2_timer timer
;
43 struct clock_event_device ced
;
46 struct nios2_clocksource
{
47 struct nios2_timer timer
;
48 struct clocksource cs
;
51 static inline struct nios2_clockevent_dev
*
52 to_nios2_clkevent(struct clock_event_device
*evt
)
54 return container_of(evt
, struct nios2_clockevent_dev
, ced
);
57 static inline struct nios2_clocksource
*
58 to_nios2_clksource(struct clocksource
*cs
)
60 return container_of(cs
, struct nios2_clocksource
, cs
);
63 static u16
timer_readw(struct nios2_timer
*timer
, u32 offs
)
65 return readw(timer
->base
+ offs
);
68 static void timer_writew(struct nios2_timer
*timer
, u16 val
, u32 offs
)
70 writew(val
, timer
->base
+ offs
);
73 static inline unsigned long read_timersnapshot(struct nios2_timer
*timer
)
77 timer_writew(timer
, 0, ALTERA_TIMER_SNAPL_REG
);
78 count
= timer_readw(timer
, ALTERA_TIMER_SNAPH_REG
) << 16 |
79 timer_readw(timer
, ALTERA_TIMER_SNAPL_REG
);
84 static cycle_t
nios2_timer_read(struct clocksource
*cs
)
86 struct nios2_clocksource
*nios2_cs
= to_nios2_clksource(cs
);
90 local_irq_save(flags
);
91 count
= read_timersnapshot(&nios2_cs
->timer
);
92 local_irq_restore(flags
);
94 /* Counter is counting down */
98 static struct nios2_clocksource nios2_cs
= {
100 .name
= "nios2-clksrc",
102 .read
= nios2_timer_read
,
103 .mask
= CLOCKSOURCE_MASK(32),
104 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
108 cycles_t
get_cycles(void)
110 return nios2_timer_read(&nios2_cs
.cs
);
112 EXPORT_SYMBOL(get_cycles
);
114 static void nios2_timer_start(struct nios2_timer
*timer
)
118 ctrl
= timer_readw(timer
, ALTERA_TIMER_CONTROL_REG
);
119 ctrl
|= ALTERA_TIMER_CONTROL_START_MSK
;
120 timer_writew(timer
, ctrl
, ALTERA_TIMER_CONTROL_REG
);
123 static void nios2_timer_stop(struct nios2_timer
*timer
)
127 ctrl
= timer_readw(timer
, ALTERA_TIMER_CONTROL_REG
);
128 ctrl
|= ALTERA_TIMER_CONTROL_STOP_MSK
;
129 timer_writew(timer
, ctrl
, ALTERA_TIMER_CONTROL_REG
);
132 static void nios2_timer_config(struct nios2_timer
*timer
, unsigned long period
,
133 enum clock_event_mode mode
)
137 /* The timer's actual period is one cycle greater than the value
138 * stored in the period register. */
141 ctrl
= timer_readw(timer
, ALTERA_TIMER_CONTROL_REG
);
143 timer_writew(timer
, ctrl
| ALTERA_TIMER_CONTROL_STOP_MSK
,
144 ALTERA_TIMER_CONTROL_REG
);
146 /* write new count */
147 timer_writew(timer
, period
, ALTERA_TIMER_PERIODL_REG
);
148 timer_writew(timer
, period
>> 16, ALTERA_TIMER_PERIODH_REG
);
150 ctrl
|= ALTERA_TIMER_CONTROL_START_MSK
| ALTERA_TIMER_CONTROL_ITO_MSK
;
151 if (mode
== CLOCK_EVT_MODE_PERIODIC
)
152 ctrl
|= ALTERA_TIMER_CONTROL_CONT_MSK
;
154 ctrl
&= ~ALTERA_TIMER_CONTROL_CONT_MSK
;
155 timer_writew(timer
, ctrl
, ALTERA_TIMER_CONTROL_REG
);
158 static int nios2_timer_set_next_event(unsigned long delta
,
159 struct clock_event_device
*evt
)
161 struct nios2_clockevent_dev
*nios2_ced
= to_nios2_clkevent(evt
);
163 nios2_timer_config(&nios2_ced
->timer
, delta
, evt
->mode
);
168 static void nios2_timer_set_mode(enum clock_event_mode mode
,
169 struct clock_event_device
*evt
)
171 unsigned long period
;
172 struct nios2_clockevent_dev
*nios2_ced
= to_nios2_clkevent(evt
);
173 struct nios2_timer
*timer
= &nios2_ced
->timer
;
176 case CLOCK_EVT_MODE_PERIODIC
:
177 period
= DIV_ROUND_UP(timer
->freq
, HZ
);
178 nios2_timer_config(timer
, period
, CLOCK_EVT_MODE_PERIODIC
);
180 case CLOCK_EVT_MODE_ONESHOT
:
181 case CLOCK_EVT_MODE_UNUSED
:
182 case CLOCK_EVT_MODE_SHUTDOWN
:
183 nios2_timer_stop(timer
);
185 case CLOCK_EVT_MODE_RESUME
:
186 nios2_timer_start(timer
);
191 irqreturn_t
timer_interrupt(int irq
, void *dev_id
)
193 struct clock_event_device
*evt
= (struct clock_event_device
*) dev_id
;
194 struct nios2_clockevent_dev
*nios2_ced
= to_nios2_clkevent(evt
);
196 /* Clear the interrupt condition */
197 timer_writew(&nios2_ced
->timer
, 0, ALTERA_TIMER_STATUS_REG
);
198 evt
->event_handler(evt
);
203 static void __init
nios2_timer_get_base_and_freq(struct device_node
*np
,
204 void __iomem
**base
, u32
*freq
)
206 *base
= of_iomap(np
, 0);
208 panic("Unable to map reg for %s\n", np
->name
);
210 if (of_property_read_u32(np
, "clock-frequency", freq
))
211 panic("Unable to get %s clock frequency\n", np
->name
);
214 static struct nios2_clockevent_dev nios2_ce
= {
216 .name
= "nios2-clkevent",
217 .features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
,
220 .set_next_event
= nios2_timer_set_next_event
,
221 .set_mode
= nios2_timer_set_mode
,
225 static __init
void nios2_clockevent_init(struct device_node
*timer
)
227 void __iomem
*iobase
;
231 nios2_timer_get_base_and_freq(timer
, &iobase
, &freq
);
233 irq
= irq_of_parse_and_map(timer
, 0);
235 panic("Unable to parse timer irq\n");
237 nios2_ce
.timer
.base
= iobase
;
238 nios2_ce
.timer
.freq
= freq
;
240 nios2_ce
.ced
.cpumask
= cpumask_of(0);
241 nios2_ce
.ced
.irq
= irq
;
243 nios2_timer_stop(&nios2_ce
.timer
);
244 /* clear pending interrupt */
245 timer_writew(&nios2_ce
.timer
, 0, ALTERA_TIMER_STATUS_REG
);
247 if (request_irq(irq
, timer_interrupt
, IRQF_TIMER
, timer
->name
,
249 panic("Unable to setup timer irq\n");
251 clockevents_config_and_register(&nios2_ce
.ced
, freq
, 1, ULONG_MAX
);
254 static __init
void nios2_clocksource_init(struct device_node
*timer
)
257 void __iomem
*iobase
;
260 nios2_timer_get_base_and_freq(timer
, &iobase
, &freq
);
262 nios2_cs
.timer
.base
= iobase
;
263 nios2_cs
.timer
.freq
= freq
;
265 clocksource_register_hz(&nios2_cs
.cs
, freq
);
267 timer_writew(&nios2_cs
.timer
, USHRT_MAX
, ALTERA_TIMER_PERIODL_REG
);
268 timer_writew(&nios2_cs
.timer
, USHRT_MAX
, ALTERA_TIMER_PERIODH_REG
);
270 /* interrupt disable + continuous + start */
271 ctrl
= ALTERA_TIMER_CONTROL_CONT_MSK
| ALTERA_TIMER_CONTROL_START_MSK
;
272 timer_writew(&nios2_cs
.timer
, ctrl
, ALTERA_TIMER_CONTROL_REG
);
274 /* Calibrate the delay loop directly */
275 lpj_fine
= freq
/ HZ
;
279 * The first timer instance will use as a clockevent. If there are two or
280 * more instances, the second one gets used as clocksource and all
283 static void __init
nios2_time_init(struct device_node
*timer
)
285 static int num_called
;
287 switch (num_called
) {
289 nios2_clockevent_init(timer
);
292 nios2_clocksource_init(timer
);
301 void read_persistent_clock(struct timespec
*ts
)
303 ts
->tv_sec
= mktime(2007, 1, 1, 0, 0, 0);
307 void __init
time_init(void)
309 struct device_node
*np
;
312 for_each_compatible_node(np
, NULL
, ALTR_TIMER_COMPATIBLE
)
316 panic("%d timer is found, it needs 2 timers in system\n", count
);
318 clocksource_of_init();
321 CLOCKSOURCE_OF_DECLARE(nios2_timer
, ALTR_TIMER_COMPATIBLE
, nios2_time_init
);