time.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Common time service routines for parisc machines.
  4. * based on arch/loongarch/kernel/time.c
  5. *
  6. * Copyright (C) 2024 Helge Deller <deller@gmx.de>
  7. */
  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>
  19. static u64 cr16_clock_freq;
  20. static unsigned long clocktick;
  21. int time_keeper_id; /* CPU used for timekeeping */
  22. static DEFINE_PER_CPU(struct clock_event_device, parisc_clockevent_device);
  23. static void parisc_event_handler(struct clock_event_device *dev)
  24. {
  25. }
  26. static int parisc_timer_next_event(unsigned long delta, struct clock_event_device *evt)
  27. {
  28. unsigned long new_cr16;
  29. new_cr16 = mfctl(16) + delta;
  30. mtctl(new_cr16, 16);
  31. return 0;
  32. }
  33. irqreturn_t timer_interrupt(int irq, void *data)
  34. {
  35. struct clock_event_device *cd;
  36. int cpu = smp_processor_id();
  37. cd = &per_cpu(parisc_clockevent_device, cpu);
  38. if (clockevent_state_periodic(cd))
  39. parisc_timer_next_event(clocktick, cd);
  40. if (clockevent_state_periodic(cd) || clockevent_state_oneshot(cd))
  41. cd->event_handler(cd);
  42. return IRQ_HANDLED;
  43. }
  44. static int parisc_set_state_oneshot(struct clock_event_device *evt)
  45. {
  46. parisc_timer_next_event(clocktick, evt);
  47. return 0;
  48. }
  49. static int parisc_set_state_periodic(struct clock_event_device *evt)
  50. {
  51. parisc_timer_next_event(clocktick, evt);
  52. return 0;
  53. }
  54. static int parisc_set_state_shutdown(struct clock_event_device *evt)
  55. {
  56. return 0;
  57. }
  58. void parisc_clockevent_init(void)
  59. {
  60. unsigned int cpu = smp_processor_id();
  61. unsigned long min_delta = 0x600; /* XXX */
  62. unsigned long max_delta = (1UL << (BITS_PER_LONG - 1));
  63. struct clock_event_device *cd;
  64. cd = &per_cpu(parisc_clockevent_device, cpu);
  65. cd->name = "cr16_clockevent";
  66. cd->features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC |
  67. CLOCK_EVT_FEAT_PERCPU;
  68. cd->irq = TIMER_IRQ;
  69. cd->rating = 320;
  70. cd->cpumask = cpumask_of(cpu);
  71. cd->set_state_oneshot = parisc_set_state_oneshot;
  72. cd->set_state_oneshot_stopped = parisc_set_state_shutdown;
  73. cd->set_state_periodic = parisc_set_state_periodic;
  74. cd->set_state_shutdown = parisc_set_state_shutdown;
  75. cd->set_next_event = parisc_timer_next_event;
  76. cd->event_handler = parisc_event_handler;
  77. clockevents_config_and_register(cd, cr16_clock_freq, min_delta, max_delta);
  78. }
  79. unsigned long notrace profile_pc(struct pt_regs *regs)
  80. {
  81. unsigned long pc = instruction_pointer(regs);
  82. if (regs->gr[0] & PSW_N)
  83. pc -= 4;
  84. #ifdef CONFIG_SMP
  85. if (in_lock_functions(pc))
  86. pc = regs->gr[2];
  87. #endif
  88. return pc;
  89. }
  90. EXPORT_SYMBOL(profile_pc);
  91. #if IS_ENABLED(CONFIG_RTC_DRV_GENERIC)
  92. static int rtc_generic_get_time(struct device *dev, struct rtc_time *tm)
  93. {
  94. struct pdc_tod tod_data;
  95. memset(tm, 0, sizeof(*tm));
  96. if (pdc_tod_read(&tod_data) < 0)
  97. return -EOPNOTSUPP;
  98. /* we treat tod_sec as unsigned, so this can work until year 2106 */
  99. rtc_time64_to_tm(tod_data.tod_sec, tm);
  100. return 0;
  101. }
  102. static int rtc_generic_set_time(struct device *dev, struct rtc_time *tm)
  103. {
  104. time64_t secs = rtc_tm_to_time64(tm);
  105. int ret;
  106. /* hppa has Y2K38 problem: pdc_tod_set() takes an u32 value! */
  107. ret = pdc_tod_set(secs, 0);
  108. if (ret != 0) {
  109. pr_warn("pdc_tod_set(%lld) returned error %d\n", secs, ret);
  110. if (ret == PDC_INVALID_ARG)
  111. return -EINVAL;
  112. return -EOPNOTSUPP;
  113. }
  114. return 0;
  115. }
  116. static const struct rtc_class_ops rtc_generic_ops = {
  117. .read_time = rtc_generic_get_time,
  118. .set_time = rtc_generic_set_time,
  119. };
  120. static int __init rtc_init(void)
  121. {
  122. struct platform_device *pdev;
  123. pdev = platform_device_register_data(NULL, "rtc-generic", -1,
  124. &rtc_generic_ops,
  125. sizeof(rtc_generic_ops));
  126. return PTR_ERR_OR_ZERO(pdev);
  127. }
  128. device_initcall(rtc_init);
  129. #endif
  130. void read_persistent_clock64(struct timespec64 *ts)
  131. {
  132. static struct pdc_tod tod_data;
  133. if (pdc_tod_read(&tod_data) == 0) {
  134. ts->tv_sec = tod_data.tod_sec;
  135. ts->tv_nsec = tod_data.tod_usec * 1000;
  136. } else {
  137. printk(KERN_ERR "Error reading tod clock\n");
  138. ts->tv_sec = 0;
  139. ts->tv_nsec = 0;
  140. }
  141. }
  142. static u64 notrace read_cr16_sched_clock(void)
  143. {
  144. return get_cycles();
  145. }
  146. static u64 notrace read_cr16(struct clocksource *cs)
  147. {
  148. return get_cycles();
  149. }
  150. static struct clocksource clocksource_cr16 = {
  151. .name = "cr16",
  152. .rating = 300,
  153. .read = read_cr16,
  154. .mask = CLOCKSOURCE_MASK(BITS_PER_LONG),
  155. .flags = CLOCK_SOURCE_IS_CONTINUOUS |
  156. CLOCK_SOURCE_VALID_FOR_HRES |
  157. CLOCK_SOURCE_MUST_VERIFY |
  158. CLOCK_SOURCE_VERIFY_PERCPU,
  159. };
  160. /*
  161. * timer interrupt and sched_clock() initialization
  162. */
  163. void __init time_init(void)
  164. {
  165. cr16_clock_freq = 100 * PAGE0->mem_10msec; /* Hz */
  166. clocktick = cr16_clock_freq / HZ;
  167. /* register as sched_clock source */
  168. sched_clock_register(read_cr16_sched_clock, BITS_PER_LONG, cr16_clock_freq);
  169. parisc_clockevent_init();
  170. /* register at clocksource framework */
  171. clocksource_register_hz(&clocksource_cr16, cr16_clock_freq);
  172. }