cputimer.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * QEMU OpenRISC timer support
  3. *
  4. * Copyright (c) 2011-2012 Jia Liu <proljc@gmail.com>
  5. * Zhizhou Zhang <etouzh@gmail.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "cpu.h"
  21. #include "hw/hw.h"
  22. #include "qemu/timer.h"
  23. #define TIMER_FREQ (20 * 1000 * 1000) /* 20MHz */
  24. /* The time when TTCR changes */
  25. static uint64_t last_clk;
  26. static int is_counting;
  27. void cpu_openrisc_count_update(OpenRISCCPU *cpu)
  28. {
  29. uint64_t now;
  30. if (!is_counting) {
  31. return;
  32. }
  33. now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  34. cpu->env.ttcr += (uint32_t)muldiv64(now - last_clk, TIMER_FREQ,
  35. get_ticks_per_sec());
  36. last_clk = now;
  37. }
  38. void cpu_openrisc_timer_update(OpenRISCCPU *cpu)
  39. {
  40. uint32_t wait;
  41. uint64_t now, next;
  42. if (!is_counting) {
  43. return;
  44. }
  45. cpu_openrisc_count_update(cpu);
  46. now = last_clk;
  47. if ((cpu->env.ttmr & TTMR_TP) <= (cpu->env.ttcr & TTMR_TP)) {
  48. wait = TTMR_TP - (cpu->env.ttcr & TTMR_TP) + 1;
  49. wait += cpu->env.ttmr & TTMR_TP;
  50. } else {
  51. wait = (cpu->env.ttmr & TTMR_TP) - (cpu->env.ttcr & TTMR_TP);
  52. }
  53. next = now + muldiv64(wait, get_ticks_per_sec(), TIMER_FREQ);
  54. timer_mod(cpu->env.timer, next);
  55. }
  56. void cpu_openrisc_count_start(OpenRISCCPU *cpu)
  57. {
  58. is_counting = 1;
  59. cpu_openrisc_count_update(cpu);
  60. }
  61. void cpu_openrisc_count_stop(OpenRISCCPU *cpu)
  62. {
  63. timer_del(cpu->env.timer);
  64. cpu_openrisc_count_update(cpu);
  65. is_counting = 0;
  66. }
  67. static void openrisc_timer_cb(void *opaque)
  68. {
  69. OpenRISCCPU *cpu = opaque;
  70. if ((cpu->env.ttmr & TTMR_IE) &&
  71. timer_expired(cpu->env.timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL))) {
  72. CPUState *cs = CPU(cpu);
  73. cpu->env.ttmr |= TTMR_IP;
  74. cs->interrupt_request |= CPU_INTERRUPT_TIMER;
  75. }
  76. switch (cpu->env.ttmr & TTMR_M) {
  77. case TIMER_NONE:
  78. break;
  79. case TIMER_INTR:
  80. cpu->env.ttcr = 0;
  81. break;
  82. case TIMER_SHOT:
  83. cpu_openrisc_count_stop(cpu);
  84. break;
  85. case TIMER_CONT:
  86. break;
  87. }
  88. cpu_openrisc_timer_update(cpu);
  89. }
  90. void cpu_openrisc_clock_init(OpenRISCCPU *cpu)
  91. {
  92. cpu->env.timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &openrisc_timer_cb, cpu);
  93. cpu->env.ttmr = 0x00000000;
  94. cpu->env.ttcr = 0x00000000;
  95. }