timer.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. static time_t system_time = 0;
  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. #define TIMER_TICK_RATE 32768
  40. static enum handler_return timer_irq(void *arg)
  41. {
  42. ticks += tick_interval;
  43. return time_callback(callback_arg, ticks);
  44. }
  45. void lk_scheduler(void)
  46. {
  47. //static enum handler_return ret;
  48. /* ack GPT5 irq */
  49. /* configure gpt5 to use 13MHZ clock during irq ack.
  50. * Reason: In 32K domain, the de-assert signal to GIC might delay several clocks. So
  51. * there must have delay between ack GTP5 irq and ack GIC irq. Ohterwise the
  52. * irq will come twice at one tick point.
  53. * Switching to 13M, ack gpt5 irq flow speeds up to avoid problem.
  54. */
  55. DRV_WriteReg32(GPT5_CLK_REG , 0x00);
  56. DRV_WriteReg32(GPT_IRQACK_REG, 0x10);
  57. DRV_WriteReg32(GPT5_CON_REG, GPT_CLEAR);
  58. DRV_WriteReg32(GPT5_CON_REG, GPT_DISABLE);
  59. DRV_WriteReg32(GPT5_CLK_REG , 0x10);
  60. timer_irq(0);
  61. /*
  62. * CAUTION! The de-assert signal to GIC might delay serveral clocks.
  63. * Here must have enough delay to make sure the GPT signal had arrived GIC.
  64. */
  65. /* ack GIC irq */
  66. mt_irq_ack(MT_GPT_IRQ_ID);
  67. /* enable GPT5 */
  68. DRV_WriteReg32(GPT5_CON_REG, GPT_ENABLE|GPT_MODE4_ONE_SHOT);
  69. }
  70. status_t platform_set_periodic_timer(platform_timer_callback callback, void *arg, time_t interval)
  71. {
  72. time_callback = callback;
  73. tick_interval = interval;
  74. callback_arg = arg;
  75. DRV_WriteReg32(GPT_IRQEN_REG, 0);
  76. DRV_WriteReg32(GPT_IRQACK_REG, 0x3f);
  77. mt_irq_set_sens(MT_GPT_IRQ_ID, MT65xx_LEVEL_SENSITIVE);
  78. mt_irq_set_polarity(MT_GPT_IRQ_ID, MT65xx_POLARITY_LOW);
  79. DRV_WriteReg32(GPT5_CON_REG, 0x02);
  80. DRV_WriteReg32(GPT_IRQACK_REG, 0x10);
  81. DRV_WriteReg32(GPT5_CLK_REG , 0x10);
  82. DRV_WriteReg32(GPT5_COMPARE_REG, TIMER_TICK_RATE*interval/1000);
  83. DRV_WriteReg32(GPT_IRQEN_REG, 0x10);
  84. mt_irq_unmask(MT_GPT_IRQ_ID);
  85. DRV_WriteReg32(GPT5_CON_REG, GPT_ENABLE|GPT_MODE4_ONE_SHOT);
  86. return NO_ERROR;
  87. }
  88. time_t current_time(void)
  89. {
  90. return ticks;
  91. }
  92. static void gpt_power_on (bool bPowerOn)
  93. {
  94. if(!bPowerOn){
  95. DRV_SetReg32(AP_PERI_GLOBALCON_PDN0, 1<<13);
  96. }else{
  97. DRV_ClrReg32(AP_PERI_GLOBALCON_PDN0, 1<<13);
  98. }
  99. }
  100. void platform_early_init_timer(void)
  101. {
  102. //dprintf(SPEW, "platform_early_init_timer\n");
  103. gpt_power_on(1);
  104. }