time.c 4.6 KB

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