timer.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2008 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <sys/types.h>
  24. #include <debug.h>
  25. #include <err.h>
  26. #include <reg.h>
  27. #include <platform/timer.h>
  28. #include <platform/mt_typedefs.h>
  29. #include <platform/mt_reg_base.h>
  30. #include <platform/mt_gpt.h>
  31. #include <platform/mt_irq.h>
  32. #include <kernel/thread.h>
  33. #include <mt_gic.h> // for mt_irq_ack(), mt_irq_set_sens(), mt_irq_set_polarity()
  34. static volatile time_t ticks = 0;
  35. static time_t tick_interval;
  36. static platform_timer_callback time_callback;
  37. static void *callback_arg;
  38. #define AP_PERI_GLOBALCON_PDN0 (PERICFG_BASE+0x10)
  39. #ifdef MACH_FPGA
  40. #define TIMER_TICK_RATE 6000000
  41. #else
  42. #define TIMER_TICK_RATE 32768
  43. #endif
  44. static enum handler_return timer_irq(void *arg)
  45. {
  46. ticks += tick_interval;
  47. return time_callback(callback_arg, ticks);
  48. }
  49. void lk_scheduler(void)
  50. {
  51. //static enum handler_return ret;
  52. /* ack GPT5 irq */
  53. /* configure gpt5 to use 13MHZ clock during irq ack.
  54. * Reason: In 32K domain, the de-assert signal to GIC might delay several clocks. So
  55. * there must have delay between ack GTP5 irq and ack GIC irq. Ohterwise the
  56. * irq will come twice at one tick point.
  57. * Switching to 13M, ack gpt5 irq flow speeds up to avoid problem.
  58. */
  59. DRV_WriteReg32(GPT5_CLK_REG , 0x00);
  60. DRV_WriteReg32(GPT_IRQACK_REG, 0x10);
  61. DRV_WriteReg32(GPT5_CON_REG, GPT_CLEAR);
  62. DRV_WriteReg32(GPT5_CON_REG, GPT_DISABLE);
  63. DRV_WriteReg32(GPT5_CLK_REG , 0x10);
  64. timer_irq(0);
  65. /*
  66. * CAUTION! The de-assert signal to GIC might delay serveral clocks.
  67. * Here must have enough delay to make sure the GPT signal had arrived GIC.
  68. */
  69. /* ack GIC irq */
  70. mt_irq_ack(MT_GPT_IRQ_ID);
  71. /* enable GPT5 */
  72. DRV_WriteReg32(GPT5_CON_REG, GPT_ENABLE|GPT_MODE4_ONE_SHOT);
  73. }
  74. status_t platform_set_periodic_timer(platform_timer_callback callback, void *arg, time_t interval)
  75. {
  76. time_callback = callback;
  77. tick_interval = interval;
  78. callback_arg = arg;
  79. DRV_WriteReg32(GPT_IRQEN_REG, 0);
  80. DRV_WriteReg32(GPT_IRQACK_REG, 0x3f);
  81. mt_irq_set_sens(MT_GPT_IRQ_ID, MT65xx_LEVEL_SENSITIVE);
  82. mt_irq_set_polarity(MT_GPT_IRQ_ID, MT65xx_POLARITY_LOW);
  83. DRV_WriteReg32(GPT5_CON_REG, 0x02);
  84. DRV_WriteReg32(GPT_IRQACK_REG, 0x10);
  85. DRV_WriteReg32(GPT5_CLK_REG , 0x10);
  86. DRV_WriteReg32(GPT5_COMPARE_REG, TIMER_TICK_RATE*interval/1000);
  87. DRV_WriteReg32(GPT_IRQEN_REG, 0x10);
  88. mt_irq_unmask(MT_GPT_IRQ_ID);
  89. DRV_WriteReg32(GPT5_CON_REG, GPT_ENABLE|GPT_MODE4_ONE_SHOT);
  90. return NO_ERROR;
  91. }
  92. time_t current_time(void)
  93. {
  94. return ticks;
  95. }
  96. static void gpt_power_on (bool bPowerOn)
  97. {
  98. if (!bPowerOn) {
  99. DRV_SetReg32(AP_PERI_GLOBALCON_PDN0, 1<<13);
  100. } else {
  101. DRV_ClrReg32(AP_PERI_GLOBALCON_PDN0, 1<<13);
  102. }
  103. }
  104. void platform_early_init_timer(void)
  105. {
  106. //dprintf(SPEW, "platform_early_init_timer\n");
  107. gpt_power_on(1);
  108. }