Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * OpenRISC time.c
4 *
5 * Linux architectural port borrowing liberally from similar works of
6 * others. All original copyrights apply as per the original source
7 * declaration.
8 *
9 * Modifications for the OpenRISC architecture:
10 * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
11 */
12
13#include <linux/kernel.h>
14#include <linux/time.h>
15#include <linux/timex.h>
16#include <linux/interrupt.h>
17#include <linux/ftrace.h>
18
19#include <linux/clocksource.h>
20#include <linux/clockchips.h>
21#include <linux/irq.h>
22#include <linux/io.h>
23#include <linux/of_clk.h>
24
25#include <asm/cpuinfo.h>
26#include <asm/time.h>
27
28/* Test the timer ticks to count, used in sync routine */
29inline void openrisc_timer_set(unsigned long count)
30{
31 mtspr(SPR_TTCR, count);
32}
33
34/* Set the timer to trigger in delta cycles */
35inline void openrisc_timer_set_next(unsigned long delta)
36{
37 u32 c;
38
39 /* Read 32-bit counter value, add delta, mask off the low 28 bits.
40 * We're guaranteed delta won't be bigger than 28 bits because the
41 * generic timekeeping code ensures that for us.
42 */
43 c = mfspr(SPR_TTCR);
44 c += delta;
45 c &= SPR_TTMR_TP;
46
47 /* Set counter and enable interrupt.
48 * Keep timer in continuous mode always.
49 */
50 mtspr(SPR_TTMR, SPR_TTMR_CR | SPR_TTMR_IE | c);
51}
52
53static int openrisc_timer_set_next_event(unsigned long delta,
54 struct clock_event_device *dev)
55{
56 openrisc_timer_set_next(delta);
57 return 0;
58}
59
60/* This is the clock event device based on the OR1K tick timer.
61 * As the timer is being used as a continuous clock-source (required for HR
62 * timers) we cannot enable the PERIODIC feature. The tick timer can run using
63 * one-shot events, so no problem.
64 */
65static DEFINE_PER_CPU(struct clock_event_device, clockevent_openrisc_timer);
66
67void openrisc_clockevent_init(void)
68{
69 unsigned int cpu = smp_processor_id();
70 struct clock_event_device *evt =
71 &per_cpu(clockevent_openrisc_timer, cpu);
72 struct cpuinfo_or1k *cpuinfo = &cpuinfo_or1k[cpu];
73
74 mtspr(SPR_TTMR, SPR_TTMR_CR);
75
76#ifdef CONFIG_SMP
77 evt->broadcast = tick_broadcast;
78#endif
79 evt->name = "openrisc_timer_clockevent",
80 evt->features = CLOCK_EVT_FEAT_ONESHOT,
81 evt->rating = 300,
82 evt->set_next_event = openrisc_timer_set_next_event,
83
84 evt->cpumask = cpumask_of(cpu);
85
86 /* We only have 28 bits */
87 clockevents_config_and_register(evt, cpuinfo->clock_frequency,
88 100, 0x0fffffff);
89
90}
91
92static inline void timer_ack(void)
93{
94 /* Clear the IP bit and disable further interrupts */
95 /* This can be done very simply... we just need to keep the timer
96 running, so just maintain the CR bits while clearing the rest
97 of the register
98 */
99 mtspr(SPR_TTMR, SPR_TTMR_CR);
100}
101
102/*
103 * The timer interrupt is mostly handled in generic code nowadays... this
104 * function just acknowledges the interrupt and fires the event handler that
105 * has been set on the clockevent device by the generic time management code.
106 *
107 * This function needs to be called by the timer exception handler and that's
108 * all the exception handler needs to do.
109 */
110
111irqreturn_t __irq_entry timer_interrupt(struct pt_regs *regs)
112{
113 struct pt_regs *old_regs = set_irq_regs(regs);
114 unsigned int cpu = smp_processor_id();
115 struct clock_event_device *evt =
116 &per_cpu(clockevent_openrisc_timer, cpu);
117
118 timer_ack();
119
120 /*
121 * update_process_times() expects us to have called irq_enter().
122 */
123 irq_enter();
124 evt->event_handler(evt);
125 irq_exit();
126
127 set_irq_regs(old_regs);
128
129 return IRQ_HANDLED;
130}
131
132/*
133 * Clocksource: Based on OpenRISC timer/counter
134 *
135 * This sets up the OpenRISC Tick Timer as a clock source. The tick timer
136 * is 32 bits wide and runs at the CPU clock frequency.
137 */
138static u64 openrisc_timer_read(struct clocksource *cs)
139{
140 return (u64) mfspr(SPR_TTCR);
141}
142
143static struct clocksource openrisc_timer = {
144 .name = "openrisc_timer",
145 .rating = 200,
146 .read = openrisc_timer_read,
147 .mask = CLOCKSOURCE_MASK(32),
148 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
149};
150
151static int __init openrisc_timer_init(void)
152{
153 struct cpuinfo_or1k *cpuinfo = &cpuinfo_or1k[smp_processor_id()];
154
155 if (clocksource_register_hz(&openrisc_timer, cpuinfo->clock_frequency))
156 panic("failed to register clocksource");
157
158 /* Enable the incrementer: 'continuous' mode with interrupt disabled */
159 mtspr(SPR_TTMR, SPR_TTMR_CR);
160
161 return 0;
162}
163
164void __init time_init(void)
165{
166 u32 upr;
167
168 upr = mfspr(SPR_UPR);
169 if (!(upr & SPR_UPR_TTP))
170 panic("Linux not supported on devices without tick timer");
171
172 openrisc_timer_init();
173 openrisc_clockevent_init();
174
175 of_clk_init(NULL);
176 timer_probe();
177}
1/*
2 * OpenRISC time.c
3 *
4 * Linux architectural port borrowing liberally from similar works of
5 * others. All original copyrights apply as per the original source
6 * declaration.
7 *
8 * Modifications for the OpenRISC architecture:
9 * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 */
16
17#include <linux/kernel.h>
18#include <linux/time.h>
19#include <linux/timex.h>
20#include <linux/interrupt.h>
21#include <linux/ftrace.h>
22
23#include <linux/clocksource.h>
24#include <linux/clockchips.h>
25#include <linux/irq.h>
26#include <linux/io.h>
27
28#include <asm/cpuinfo.h>
29
30static int openrisc_timer_set_next_event(unsigned long delta,
31 struct clock_event_device *dev)
32{
33 u32 c;
34
35 /* Read 32-bit counter value, add delta, mask off the low 28 bits.
36 * We're guaranteed delta won't be bigger than 28 bits because the
37 * generic timekeeping code ensures that for us.
38 */
39 c = mfspr(SPR_TTCR);
40 c += delta;
41 c &= SPR_TTMR_TP;
42
43 /* Set counter and enable interrupt.
44 * Keep timer in continuous mode always.
45 */
46 mtspr(SPR_TTMR, SPR_TTMR_CR | SPR_TTMR_IE | c);
47
48 return 0;
49}
50
51/* This is the clock event device based on the OR1K tick timer.
52 * As the timer is being used as a continuous clock-source (required for HR
53 * timers) we cannot enable the PERIODIC feature. The tick timer can run using
54 * one-shot events, so no problem.
55 */
56
57static struct clock_event_device clockevent_openrisc_timer = {
58 .name = "openrisc_timer_clockevent",
59 .features = CLOCK_EVT_FEAT_ONESHOT,
60 .rating = 300,
61 .set_next_event = openrisc_timer_set_next_event,
62};
63
64static inline void timer_ack(void)
65{
66 /* Clear the IP bit and disable further interrupts */
67 /* This can be done very simply... we just need to keep the timer
68 running, so just maintain the CR bits while clearing the rest
69 of the register
70 */
71 mtspr(SPR_TTMR, SPR_TTMR_CR);
72}
73
74/*
75 * The timer interrupt is mostly handled in generic code nowadays... this
76 * function just acknowledges the interrupt and fires the event handler that
77 * has been set on the clockevent device by the generic time management code.
78 *
79 * This function needs to be called by the timer exception handler and that's
80 * all the exception handler needs to do.
81 */
82
83irqreturn_t __irq_entry timer_interrupt(struct pt_regs *regs)
84{
85 struct pt_regs *old_regs = set_irq_regs(regs);
86 struct clock_event_device *evt = &clockevent_openrisc_timer;
87
88 timer_ack();
89
90 /*
91 * update_process_times() expects us to have called irq_enter().
92 */
93 irq_enter();
94 evt->event_handler(evt);
95 irq_exit();
96
97 set_irq_regs(old_regs);
98
99 return IRQ_HANDLED;
100}
101
102static __init void openrisc_clockevent_init(void)
103{
104 clockevent_openrisc_timer.cpumask = cpumask_of(0);
105
106 /* We only have 28 bits */
107 clockevents_config_and_register(&clockevent_openrisc_timer,
108 cpuinfo.clock_frequency,
109 100, 0x0fffffff);
110
111}
112
113/**
114 * Clocksource: Based on OpenRISC timer/counter
115 *
116 * This sets up the OpenRISC Tick Timer as a clock source. The tick timer
117 * is 32 bits wide and runs at the CPU clock frequency.
118 */
119
120static cycle_t openrisc_timer_read(struct clocksource *cs)
121{
122 return (cycle_t) mfspr(SPR_TTCR);
123}
124
125static struct clocksource openrisc_timer = {
126 .name = "openrisc_timer",
127 .rating = 200,
128 .read = openrisc_timer_read,
129 .mask = CLOCKSOURCE_MASK(32),
130 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
131};
132
133static int __init openrisc_timer_init(void)
134{
135 if (clocksource_register_hz(&openrisc_timer, cpuinfo.clock_frequency))
136 panic("failed to register clocksource");
137
138 /* Enable the incrementer: 'continuous' mode with interrupt disabled */
139 mtspr(SPR_TTMR, SPR_TTMR_CR);
140
141 return 0;
142}
143
144void __init time_init(void)
145{
146 u32 upr;
147
148 upr = mfspr(SPR_UPR);
149 if (!(upr & SPR_UPR_TTP))
150 panic("Linux not supported on devices without tick timer");
151
152 openrisc_timer_init();
153 openrisc_clockevent_init();
154}