time.c 4.4 KB

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