timer.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. int i = 0;
  60. //static enum handler_return ret;
  61. /* ack GPT5 irq */
  62. /* configure gpt5 to use 13MHZ clock during irq ack.
  63. * Reason: In 32K domain, the de-assert signal to GIC might delay several clocks. So
  64. * there must have delay between ack GTP5 irq and ack GIC irq. Ohterwise the
  65. * irq will come twice at one tick point.
  66. * Switching to 13M, ack gpt5 irq flow speeds up to avoid problem.
  67. */
  68. DRV_WriteReg32(GPT5_CLK_REG , 0x00);
  69. DRV_WriteReg32(GPT_IRQACK_REG, 0x10);
  70. DRV_WriteReg32(GPT5_CON_REG, GPT_CLEAR);
  71. DRV_WriteReg32(GPT5_CON_REG, GPT_DISABLE);
  72. /* make sure GPT5 irq is de-assert, the max delay should be less than 3T */
  73. while (i++ < 5 && (DRV_Reg32(GPT_IRQSTA_REG) & 0x10 == 0x10))
  74. udelay(1);
  75. DRV_WriteReg32(GPT5_CLK_REG , 0x10);
  76. timer_irq(0);
  77. /*
  78. * CAUTION! The de-assert signal to GIC might delay serveral clocks.
  79. * Here must have enough delay to make sure the GPT signal had arrived GIC.
  80. */
  81. /* ack GIC irq */
  82. mt_irq_ack(MT_GPT_IRQ_ID);
  83. /* enable GPT5 */
  84. DRV_WriteReg32(GPT5_CON_REG, GPT_ENABLE|GPT_MODE4_ONE_SHOT);
  85. }
  86. status_t platform_set_periodic_timer(platform_timer_callback callback, void *arg, time_t interval)
  87. {
  88. time_callback = callback;
  89. tick_interval = interval;
  90. callback_arg = arg;
  91. DRV_WriteReg32(GPT_IRQEN_REG, 0);
  92. DRV_WriteReg32(GPT_IRQACK_REG, 0x3f);
  93. mt_irq_set_sens(MT_GPT_IRQ_ID, MT65xx_LEVEL_SENSITIVE);
  94. mt_irq_set_polarity(MT_GPT_IRQ_ID, MT65xx_POLARITY_LOW);
  95. DRV_WriteReg32(GPT5_CON_REG, 0x02);
  96. DRV_WriteReg32(GPT_IRQACK_REG, 0x10);
  97. DRV_WriteReg32(GPT5_CLK_REG , 0x10);
  98. DRV_WriteReg32(GPT5_COMPARE_REG, TIMER_TICK_RATE*interval/1000);
  99. DRV_WriteReg32(GPT_IRQEN_REG, 0x10);
  100. mt_irq_unmask(MT_GPT_IRQ_ID);
  101. DRV_WriteReg32(GPT5_CON_REG, GPT_ENABLE|GPT_MODE4_ONE_SHOT);
  102. return NO_ERROR;
  103. }
  104. time_t current_time(void)
  105. {
  106. return ticks;
  107. }
  108. static void gpt_power_on (bool bPowerOn)
  109. {
  110. if (!bPowerOn) {
  111. DRV_SetReg32(AP_PERI_GLOBALCON_PDN0, 1<<13);
  112. } else {
  113. DRV_ClrReg32(AP_PERI_GLOBALCON_PDN0, 1<<13);
  114. }
  115. }
  116. void platform_early_init_timer(void)
  117. {
  118. //dprintf(SPEW, "platform_early_init_timer\n");
  119. gpt_power_on(1);
  120. }