time.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. static int openrisc_timer_set_next_event(unsigned long delta,
  27. struct clock_event_device *dev)
  28. {
  29. u32 c;
  30. /* Read 32-bit counter value, add delta, mask off the low 28 bits.
  31. * We're guaranteed delta won't be bigger than 28 bits because the
  32. * generic timekeeping code ensures that for us.
  33. */
  34. c = mfspr(SPR_TTCR);
  35. c += delta;
  36. c &= SPR_TTMR_TP;
  37. /* Set counter and enable interrupt.
  38. * Keep timer in continuous mode always.
  39. */
  40. mtspr(SPR_TTMR, SPR_TTMR_CR | SPR_TTMR_IE | c);
  41. return 0;
  42. }
  43. static void openrisc_timer_set_mode(enum clock_event_mode mode,
  44. struct clock_event_device *evt)
  45. {
  46. switch (mode) {
  47. case CLOCK_EVT_MODE_PERIODIC:
  48. pr_debug(KERN_INFO "%s: periodic\n", __func__);
  49. BUG();
  50. break;
  51. case CLOCK_EVT_MODE_ONESHOT:
  52. pr_debug(KERN_INFO "%s: oneshot\n", __func__);
  53. break;
  54. case CLOCK_EVT_MODE_UNUSED:
  55. pr_debug(KERN_INFO "%s: unused\n", __func__);
  56. break;
  57. case CLOCK_EVT_MODE_SHUTDOWN:
  58. pr_debug(KERN_INFO "%s: shutdown\n", __func__);
  59. break;
  60. case CLOCK_EVT_MODE_RESUME:
  61. pr_debug(KERN_INFO "%s: resume\n", __func__);
  62. break;
  63. }
  64. }
  65. /* This is the clock event device based on the OR1K tick timer.
  66. * As the timer is being used as a continuous clock-source (required for HR
  67. * timers) we cannot enable the PERIODIC feature. The tick timer can run using
  68. * one-shot events, so no problem.
  69. */
  70. static struct clock_event_device clockevent_openrisc_timer = {
  71. .name = "openrisc_timer_clockevent",
  72. .features = CLOCK_EVT_FEAT_ONESHOT,
  73. .rating = 300,
  74. .set_next_event = openrisc_timer_set_next_event,
  75. .set_mode = openrisc_timer_set_mode,
  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. struct clock_event_device *evt = &clockevent_openrisc_timer;
  98. timer_ack();
  99. /*
  100. * update_process_times() expects us to have called irq_enter().
  101. */
  102. irq_enter();
  103. evt->event_handler(evt);
  104. irq_exit();
  105. set_irq_regs(old_regs);
  106. return IRQ_HANDLED;
  107. }
  108. static __init void openrisc_clockevent_init(void)
  109. {
  110. clockevents_calc_mult_shift(&clockevent_openrisc_timer,
  111. cpuinfo.clock_frequency, 4);
  112. /* We only have 28 bits */
  113. clockevent_openrisc_timer.max_delta_ns =
  114. clockevent_delta2ns((u32) 0x0fffffff, &clockevent_openrisc_timer);
  115. clockevent_openrisc_timer.min_delta_ns =
  116. clockevent_delta2ns(1, &clockevent_openrisc_timer);
  117. clockevent_openrisc_timer.cpumask = cpumask_of(0);
  118. clockevents_register_device(&clockevent_openrisc_timer);
  119. }
  120. /**
  121. * Clocksource: Based on OpenRISC timer/counter
  122. *
  123. * This sets up the OpenRISC Tick Timer as a clock source. The tick timer
  124. * is 32 bits wide and runs at the CPU clock frequency.
  125. */
  126. static cycle_t openrisc_timer_read(struct clocksource *cs)
  127. {
  128. return (cycle_t) mfspr(SPR_TTCR);
  129. }
  130. static struct clocksource openrisc_timer = {
  131. .name = "openrisc_timer",
  132. .rating = 200,
  133. .read = openrisc_timer_read,
  134. .mask = CLOCKSOURCE_MASK(32),
  135. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  136. };
  137. static int __init openrisc_timer_init(void)
  138. {
  139. if (clocksource_register_hz(&openrisc_timer, cpuinfo.clock_frequency))
  140. panic("failed to register clocksource");
  141. /* Enable the incrementer: 'continuous' mode with interrupt disabled */
  142. mtspr(SPR_TTMR, SPR_TTMR_CR);
  143. return 0;
  144. }
  145. void __init time_init(void)
  146. {
  147. u32 upr;
  148. upr = mfspr(SPR_UPR);
  149. if (!(upr & SPR_UPR_TTP))
  150. panic("Linux not supported on devices without tick timer");
  151. openrisc_timer_init();
  152. openrisc_clockevent_init();
  153. }