openrisc_timer.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.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, next;
  30. uint32_t wait;
  31. now = qemu_get_clock_ns(vm_clock);
  32. if (!is_counting) {
  33. qemu_del_timer(cpu->env.timer);
  34. last_clk = now;
  35. return;
  36. }
  37. cpu->env.ttcr += (uint32_t)muldiv64(now - last_clk, TIMER_FREQ,
  38. get_ticks_per_sec());
  39. last_clk = now;
  40. if ((cpu->env.ttmr & TTMR_TP) <= (cpu->env.ttcr & TTMR_TP)) {
  41. wait = TTMR_TP - (cpu->env.ttcr & TTMR_TP) + 1;
  42. wait += cpu->env.ttmr & TTMR_TP;
  43. } else {
  44. wait = (cpu->env.ttmr & TTMR_TP) - (cpu->env.ttcr & TTMR_TP);
  45. }
  46. next = now + muldiv64(wait, get_ticks_per_sec(), TIMER_FREQ);
  47. qemu_mod_timer(cpu->env.timer, next);
  48. }
  49. void cpu_openrisc_count_start(OpenRISCCPU *cpu)
  50. {
  51. is_counting = 1;
  52. cpu_openrisc_count_update(cpu);
  53. }
  54. void cpu_openrisc_count_stop(OpenRISCCPU *cpu)
  55. {
  56. is_counting = 0;
  57. cpu_openrisc_count_update(cpu);
  58. }
  59. static void openrisc_timer_cb(void *opaque)
  60. {
  61. OpenRISCCPU *cpu = opaque;
  62. if ((cpu->env.ttmr & TTMR_IE) &&
  63. qemu_timer_expired(cpu->env.timer, qemu_get_clock_ns(vm_clock))) {
  64. cpu->env.ttmr |= TTMR_IP;
  65. cpu->env.interrupt_request |= CPU_INTERRUPT_TIMER;
  66. }
  67. switch (cpu->env.ttmr & TTMR_M) {
  68. case TIMER_NONE:
  69. break;
  70. case TIMER_INTR:
  71. cpu->env.ttcr = 0;
  72. cpu_openrisc_count_start(cpu);
  73. break;
  74. case TIMER_SHOT:
  75. cpu_openrisc_count_stop(cpu);
  76. break;
  77. case TIMER_CONT:
  78. cpu_openrisc_count_start(cpu);
  79. break;
  80. }
  81. }
  82. void cpu_openrisc_clock_init(OpenRISCCPU *cpu)
  83. {
  84. cpu->env.timer = qemu_new_timer_ns(vm_clock, &openrisc_timer_cb, cpu);
  85. cpu->env.ttmr = 0x00000000;
  86. cpu->env.ttcr = 0x00000000;
  87. }