timer.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2015. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. */
  31. #include <sys/types.h>
  32. #include <debug.h>
  33. #include <err.h>
  34. #include <reg.h>
  35. #include <platform/timer.h>
  36. #include <platform/mt_typedefs.h>
  37. #include <platform/mt_reg_base.h>
  38. #include <platform/mt_gpt.h>
  39. #include <platform/mt_irq.h>
  40. #include <kernel/thread.h>
  41. #include <mt_gic.h> // for mt_irq_ack(), mt_irq_set_sens(), mt_irq_set_polarity()
  42. static volatile time_t ticks = 0;
  43. static time_t tick_interval;
  44. static platform_timer_callback time_callback;
  45. static void *callback_arg;
  46. #define AP_PERI_GLOBALCON_PDN0 (PERICFG_BASE+0x10)
  47. #ifdef MACH_FPGA
  48. #define TIMER_TICK_RATE 6000000
  49. #else
  50. #define TIMER_TICK_RATE 32768
  51. #endif
  52. static enum handler_return timer_irq(void *arg)
  53. {
  54. ticks += tick_interval;
  55. return time_callback(callback_arg, ticks);
  56. }
  57. void lk_scheduler(void)
  58. {
  59. //static enum handler_return ret;
  60. /* ack GPT5 irq */
  61. /* configure gpt5 to use 13MHZ clock during irq ack.
  62. * Reason: In 32K domain, the de-assert signal to GIC might delay several clocks. So
  63. * there must have delay between ack GTP5 irq and ack GIC irq. Ohterwise the
  64. * irq will come twice at one tick point.
  65. * Switching to 13M, ack gpt5 irq flow speeds up to avoid problem.
  66. */
  67. DRV_WriteReg32(GPT5_CLK_REG , 0x00);
  68. DRV_WriteReg32(GPT_IRQACK_REG, 0x10);
  69. DRV_WriteReg32(GPT5_CON_REG, GPT_CLEAR);
  70. DRV_WriteReg32(GPT5_CON_REG, GPT_DISABLE);
  71. DRV_WriteReg32(GPT5_CLK_REG , 0x10);
  72. timer_irq(0);
  73. /*
  74. * CAUTION! The de-assert signal to GIC might delay serveral clocks.
  75. * Here must have enough delay to make sure the GPT signal had arrived GIC.
  76. */
  77. /* ack GIC irq */
  78. mt_irq_ack(MT_GPT_IRQ_ID);
  79. /* enable GPT5 */
  80. DRV_WriteReg32(GPT5_CON_REG, GPT_ENABLE|GPT_MODE4_ONE_SHOT);
  81. }
  82. status_t platform_set_periodic_timer(platform_timer_callback callback, void *arg, time_t interval)
  83. {
  84. time_callback = callback;
  85. tick_interval = interval;
  86. callback_arg = arg;
  87. DRV_WriteReg32(GPT_IRQEN_REG, 0);
  88. DRV_WriteReg32(GPT_IRQACK_REG, 0x3f);
  89. mt_irq_set_sens(MT_GPT_IRQ_ID, MT65xx_LEVEL_SENSITIVE);
  90. mt_irq_set_polarity(MT_GPT_IRQ_ID, MT65xx_POLARITY_LOW);
  91. DRV_WriteReg32(GPT5_CON_REG, 0x02);
  92. DRV_WriteReg32(GPT_IRQACK_REG, 0x10);
  93. DRV_WriteReg32(GPT5_CLK_REG , 0x10);
  94. DRV_WriteReg32(GPT5_COMPARE_REG, TIMER_TICK_RATE*interval/1000);
  95. DRV_WriteReg32(GPT_IRQEN_REG, 0x10);
  96. mt_irq_unmask(MT_GPT_IRQ_ID);
  97. DRV_WriteReg32(GPT5_CON_REG, GPT_ENABLE|GPT_MODE4_ONE_SHOT);
  98. return NO_ERROR;
  99. }
  100. time_t current_time(void)
  101. {
  102. return ticks;
  103. }
  104. static void gpt_power_on (bool bPowerOn)
  105. {
  106. if (!bPowerOn) {
  107. DRV_SetReg32(AP_PERI_GLOBALCON_PDN0, 1<<13);
  108. } else {
  109. DRV_ClrReg32(AP_PERI_GLOBALCON_PDN0, 1<<13);
  110. }
  111. }
  112. void platform_early_init_timer(void)
  113. {
  114. //dprintf(SPEW, "platform_early_init_timer\n");
  115. gpt_power_on(1);
  116. }