1 // SPDX-License-Identifier: GPL-2.0
3 * Common time service routines for parisc machines.
4 * based on arch/loongarch/kernel/time.c
6 * Copyright (C) 2024 Helge Deller <deller@gmx.de>
8 #include <linux/clockchips.h>
9 #include <linux/delay.h>
10 #include <linux/export.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/sched_clock.h>
15 #include <linux/spinlock.h>
16 #include <linux/rtc.h>
17 #include <linux/platform_device.h>
18 #include <asm/processor.h>
20 static u64 cr16_clock_freq
;
21 static unsigned long clocktick
;
23 int time_keeper_id
; /* CPU used for timekeeping */
25 static DEFINE_PER_CPU(struct clock_event_device
, parisc_clockevent_device
);
27 static void parisc_event_handler(struct clock_event_device
*dev
)
31 static int parisc_timer_next_event(unsigned long delta
, struct clock_event_device
*evt
)
33 unsigned long new_cr16
;
35 new_cr16
= mfctl(16) + delta
;
41 irqreturn_t
timer_interrupt(int irq
, void *data
)
43 struct clock_event_device
*cd
;
44 int cpu
= smp_processor_id();
46 cd
= &per_cpu(parisc_clockevent_device
, cpu
);
48 if (clockevent_state_periodic(cd
))
49 parisc_timer_next_event(clocktick
, cd
);
51 if (clockevent_state_periodic(cd
) || clockevent_state_oneshot(cd
))
52 cd
->event_handler(cd
);
57 static int parisc_set_state_oneshot(struct clock_event_device
*evt
)
59 parisc_timer_next_event(clocktick
, evt
);
64 static int parisc_set_state_periodic(struct clock_event_device
*evt
)
66 parisc_timer_next_event(clocktick
, evt
);
71 static int parisc_set_state_shutdown(struct clock_event_device
*evt
)
76 void parisc_clockevent_init(void)
78 unsigned int cpu
= smp_processor_id();
79 unsigned long min_delta
= 0x600; /* XXX */
80 unsigned long max_delta
= (1UL << (BITS_PER_LONG
- 1));
81 struct clock_event_device
*cd
;
83 cd
= &per_cpu(parisc_clockevent_device
, cpu
);
85 cd
->name
= "cr16_clockevent";
86 cd
->features
= CLOCK_EVT_FEAT_ONESHOT
| CLOCK_EVT_FEAT_PERIODIC
|
87 CLOCK_EVT_FEAT_PERCPU
;
91 cd
->cpumask
= cpumask_of(cpu
);
92 cd
->set_state_oneshot
= parisc_set_state_oneshot
;
93 cd
->set_state_oneshot_stopped
= parisc_set_state_shutdown
;
94 cd
->set_state_periodic
= parisc_set_state_periodic
;
95 cd
->set_state_shutdown
= parisc_set_state_shutdown
;
96 cd
->set_next_event
= parisc_timer_next_event
;
97 cd
->event_handler
= parisc_event_handler
;
99 clockevents_config_and_register(cd
, cr16_clock_freq
, min_delta
, max_delta
);
102 unsigned long notrace
profile_pc(struct pt_regs
*regs
)
104 unsigned long pc
= instruction_pointer(regs
);
106 if (regs
->gr
[0] & PSW_N
)
110 if (in_lock_functions(pc
))
116 EXPORT_SYMBOL(profile_pc
);
118 #if IS_ENABLED(CONFIG_RTC_DRV_GENERIC)
119 static int rtc_generic_get_time(struct device
*dev
, struct rtc_time
*tm
)
121 struct pdc_tod tod_data
;
123 memset(tm
, 0, sizeof(*tm
));
124 if (pdc_tod_read(&tod_data
) < 0)
127 /* we treat tod_sec as unsigned, so this can work until year 2106 */
128 rtc_time64_to_tm(tod_data
.tod_sec
, tm
);
132 static int rtc_generic_set_time(struct device
*dev
, struct rtc_time
*tm
)
134 time64_t secs
= rtc_tm_to_time64(tm
);
137 /* hppa has Y2K38 problem: pdc_tod_set() takes an u32 value! */
138 ret
= pdc_tod_set(secs
, 0);
140 pr_warn("pdc_tod_set(%lld) returned error %d\n", secs
, ret
);
141 if (ret
== PDC_INVALID_ARG
)
149 static const struct rtc_class_ops rtc_generic_ops
= {
150 .read_time
= rtc_generic_get_time
,
151 .set_time
= rtc_generic_set_time
,
154 static int __init
rtc_init(void)
156 struct platform_device
*pdev
;
158 pdev
= platform_device_register_data(NULL
, "rtc-generic", -1,
160 sizeof(rtc_generic_ops
));
162 return PTR_ERR_OR_ZERO(pdev
);
164 device_initcall(rtc_init
);
167 void read_persistent_clock64(struct timespec64
*ts
)
169 static struct pdc_tod tod_data
;
170 if (pdc_tod_read(&tod_data
) == 0) {
171 ts
->tv_sec
= tod_data
.tod_sec
;
172 ts
->tv_nsec
= tod_data
.tod_usec
* 1000;
174 printk(KERN_ERR
"Error reading tod clock\n");
180 static u64 notrace
read_cr16_sched_clock(void)
185 static u64 notrace
read_cr16(struct clocksource
*cs
)
190 static struct clocksource clocksource_cr16
= {
194 .mask
= CLOCKSOURCE_MASK(BITS_PER_LONG
),
195 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
|
196 CLOCK_SOURCE_VALID_FOR_HRES
|
197 CLOCK_SOURCE_MUST_VERIFY
|
198 CLOCK_SOURCE_VERIFY_PERCPU
,
203 * timer interrupt and sched_clock() initialization
206 void __init
time_init(void)
208 cr16_clock_freq
= 100 * PAGE0
->mem_10msec
; /* Hz */
209 clocktick
= cr16_clock_freq
/ HZ
;
211 /* register as sched_clock source */
212 sched_clock_register(read_cr16_sched_clock
, BITS_PER_LONG
, cr16_clock_freq
);
214 parisc_clockevent_init();
216 /* register at clocksource framework */
217 clocksource_register_hz(&clocksource_cr16
, cr16_clock_freq
);