time.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #include <linux/kernel.h>
  13. #include <linux/time.h>
  14. #include <linux/timex.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/ftrace.h>
  17. #include <linux/clocksource.h>
  18. #include <linux/clockchips.h>
  19. #include <linux/irq.h>
  20. #include <linux/io.h>
  21. #include <linux/of_clk.h>
  22. #include <asm/cpuinfo.h>
  23. #include <asm/time.h>
  24. irqreturn_t __irq_entry timer_interrupt(struct pt_regs *regs);
  25. /* Test the timer ticks to count, used in sync routine */
  26. inline void openrisc_timer_set(unsigned long count)
  27. {
  28. mtspr(SPR_TTCR, count);
  29. }
  30. /* Set the timer to trigger in delta cycles */
  31. inline void openrisc_timer_set_next(unsigned long delta)
  32. {
  33. u32 c;
  34. /* Read 32-bit counter value, add delta, mask off the low 28 bits.
  35. * We're guaranteed delta won't be bigger than 28 bits because the
  36. * generic timekeeping code ensures that for us.
  37. */
  38. c = mfspr(SPR_TTCR);
  39. c += delta;
  40. c &= SPR_TTMR_TP;
  41. /* Set counter and enable interrupt.
  42. * Keep timer in continuous mode always.
  43. */
  44. mtspr(SPR_TTMR, SPR_TTMR_CR | SPR_TTMR_IE | c);
  45. }
  46. static int openrisc_timer_set_next_event(unsigned long delta,
  47. struct clock_event_device *dev)
  48. {
  49. openrisc_timer_set_next(delta);
  50. return 0;
  51. }
  52. /* This is the clock event device based on the OR1K tick timer.
  53. * As the timer is being used as a continuous clock-source (required for HR
  54. * timers) we cannot enable the PERIODIC feature. The tick timer can run using
  55. * one-shot events, so no problem.
  56. */
  57. static DEFINE_PER_CPU(struct clock_event_device, clockevent_openrisc_timer);
  58. void openrisc_clockevent_init(void)
  59. {
  60. unsigned int cpu = smp_processor_id();
  61. struct clock_event_device *evt =
  62. &per_cpu(clockevent_openrisc_timer, cpu);
  63. struct cpuinfo_or1k *cpuinfo = &cpuinfo_or1k[cpu];
  64. mtspr(SPR_TTMR, SPR_TTMR_CR);
  65. #ifdef CONFIG_SMP
  66. evt->broadcast = tick_broadcast;
  67. #endif
  68. evt->name = "openrisc_timer_clockevent",
  69. evt->features = CLOCK_EVT_FEAT_ONESHOT,
  70. evt->rating = 300,
  71. evt->set_next_event = openrisc_timer_set_next_event,
  72. evt->cpumask = cpumask_of(cpu);
  73. /* We only have 28 bits */
  74. clockevents_config_and_register(evt, cpuinfo->clock_frequency,
  75. 100, 0x0fffffff);
  76. }
  77. static inline void timer_ack(void)
  78. {
  79. /* Clear the IP bit and disable further interrupts */
  80. /* This can be done very simply... we just need to keep the timer
  81. running, so just maintain the CR bits while clearing the rest
  82. of the register
  83. */
  84. mtspr(SPR_TTMR, SPR_TTMR_CR);
  85. }
  86. /*
  87. * The timer interrupt is mostly handled in generic code nowadays... this
  88. * function just acknowledges the interrupt and fires the event handler that
  89. * has been set on the clockevent device by the generic time management code.
  90. *
  91. * This function needs to be called by the timer exception handler and that's
  92. * all the exception handler needs to do.
  93. */
  94. irqreturn_t __irq_entry timer_interrupt(struct pt_regs *regs)
  95. {
  96. struct pt_regs *old_regs = set_irq_regs(regs);
  97. unsigned int cpu = smp_processor_id();
  98. struct clock_event_device *evt =
  99. &per_cpu(clockevent_openrisc_timer, cpu);
  100. timer_ack();
  101. /*
  102. * update_process_times() expects us to have called irq_enter().
  103. */
  104. irq_enter();
  105. evt->event_handler(evt);
  106. irq_exit();
  107. set_irq_regs(old_regs);
  108. return IRQ_HANDLED;
  109. }
  110. /*
  111. * Clocksource: Based on OpenRISC timer/counter
  112. *
  113. * This sets up the OpenRISC Tick Timer as a clock source. The tick timer
  114. * is 32 bits wide and runs at the CPU clock frequency.
  115. */
  116. static u64 openrisc_timer_read(struct clocksource *cs)
  117. {
  118. return (u64) mfspr(SPR_TTCR);
  119. }
  120. static struct clocksource openrisc_timer = {
  121. .name = "openrisc_timer",
  122. .rating = 200,
  123. .read = openrisc_timer_read,
  124. .mask = CLOCKSOURCE_MASK(32),
  125. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  126. };
  127. static int __init openrisc_timer_init(void)
  128. {
  129. struct cpuinfo_or1k *cpuinfo = &cpuinfo_or1k[smp_processor_id()];
  130. if (clocksource_register_hz(&openrisc_timer, cpuinfo->clock_frequency))
  131. panic("failed to register clocksource");
  132. /* Enable the incrementer: 'continuous' mode with interrupt disabled */
  133. mtspr(SPR_TTMR, SPR_TTMR_CR);
  134. return 0;
  135. }
  136. void __init time_init(void)
  137. {
  138. u32 upr;
  139. upr = mfspr(SPR_UPR);
  140. if (!(upr & SPR_UPR_TTP))
  141. panic("Linux not supported on devices without tick timer");
  142. openrisc_timer_init();
  143. openrisc_clockevent_init();
  144. of_clk_init(NULL);
  145. timer_probe();
  146. }