mt_gpt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 <common.h>
  32. #include <sys/types.h>
  33. #include <debug.h>
  34. #include <err.h>
  35. #include <reg.h>
  36. //#include <platform/timer.h>
  37. #include <platform/mt_typedefs.h>
  38. #include <platform/mt_reg_base.h>
  39. #include <platform/mt_gpt.h>
  40. #include <platform/mt_irq.h>
  41. #include <platform/boot_mode.h>
  42. typedef volatile unsigned int* P_U32;
  43. #define GPT4_CON ((P_U32)(APXGPT_BASE+0x0040))
  44. #define GPT4_CLK ((P_U32)(APXGPT_BASE+0x0044))
  45. #define GPT4_DAT ((P_U32)(APXGPT_BASE+0x0048))
  46. #define GPT4_EN 0x0001
  47. #define GPT4_FREERUN 0x0030
  48. #define GPT4_SYS_CLK 0x0000
  49. #define GPT4_MAX_TICK_CNT ((U32)0xFFFFFFFF)
  50. // 13MHz setting
  51. #define GPT4_MAX_US_TIMEOUT ((U32)330382100) // 0xFFFFFFFF /13d
  52. #define GPT4_MAX_MS_TIMEOUT ((U32)330382) // 0xFFFFFFFF /13000d
  53. #define GPT4_1US_TICK ((U32)13) // 1000 / 76.92ns = 13.000
  54. #define GPT4_1MS_TICK ((U32)13000) // 1000000 / 76.92ns = 13000.520
  55. // 13MHz: 1us = 13.000 ticks
  56. #define TIME_TO_TICK_US(us) ((us)*GPT4_1US_TICK + ((us)*0 + (1000-1))/1000)
  57. // 13MHz: 1ms = 13000.520 ticks
  58. #define TIME_TO_TICK_MS(ms) ((ms)*GPT4_1MS_TICK + ((ms)*520 + (1000-1))/1000)
  59. #define MS_TO_US 1000
  60. #define CFG_HZ 100
  61. #define MAX_REG_MS GPT4_MAX_MS_TIMEOUT
  62. #define GPT_SET_BITS(BS,REG) ((*(volatile U32*)(REG)) |= (U32)(BS))
  63. #define GPT_CLR_BITS(BS,REG) ((*(volatile U32*)(REG)) &= ~((U32)(BS)))
  64. static volatile U32 timestamp;
  65. static volatile U32 lastinc;
  66. extern BOOT_ARGUMENT *g_boot_arg;
  67. //===========================================================================
  68. // GPT4 fixed 13MHz counter
  69. //===========================================================================
  70. /*
  71. static void gpt_power_on (bool bPowerOn)
  72. {
  73. #define AP_PERI_GLOBALCON_PDN0 (PERI_CON_BASE+0x10)
  74. if(!bPowerOn){
  75. GPT_SET_BITS(1<<13, AP_PERI_GLOBALCON_PDN0);
  76. }else{
  77. GPT_CLR_BITS(1<<13, AP_PERI_GLOBALCON_PDN0);
  78. }
  79. }
  80. */
  81. static void gpt4_start (void)
  82. {
  83. *GPT4_CLK = (GPT4_SYS_CLK);
  84. *GPT4_CON = (GPT4_EN|GPT4_FREERUN);
  85. }
  86. static void gpt4_stop (void)
  87. {
  88. *GPT4_CON = 0x0; // disable
  89. *GPT4_CON = 0x2; // clear counter
  90. }
  91. static void gpt4_init (bool bStart)
  92. {
  93. // power on GPT
  94. //gpt_power_on (TRUE);
  95. // clear GPT4 first
  96. gpt4_stop ();
  97. // enable GPT4 without lock
  98. if (bStart) {
  99. gpt4_start ();
  100. }
  101. }
  102. U32 gpt4_get_current_tick (void)
  103. {
  104. U32 cnt1, cnt2, cnt3,value1;
  105. cnt1 = (*GPT4_DAT);
  106. cnt2 = (*GPT4_DAT);
  107. cnt3 = (*GPT4_DAT);
  108. if (cnt2 < cnt1) {
  109. if (cnt1 < cnt3)
  110. value1 = cnt1;
  111. else {
  112. value1 = ((cnt2 > cnt3) ? cnt2 :cnt3);
  113. }
  114. } else {
  115. if (cnt2 < cnt3)
  116. value1 = cnt2;
  117. else {
  118. value1= ((cnt1 > cnt3) ? cnt1 :cnt3);
  119. }
  120. }
  121. return value1;
  122. //return (*GPT4_DAT);
  123. }
  124. bool gpt4_timeout_tick (U32 start_tick, U32 timeout_tick)
  125. {
  126. register U32 cur_tick;
  127. register U32 elapse_tick;
  128. // get current tick
  129. cur_tick = gpt4_get_current_tick ();
  130. // check elapse time
  131. if (start_tick <= cur_tick) {
  132. elapse_tick = cur_tick - start_tick;
  133. } else {
  134. elapse_tick = (GPT4_MAX_TICK_CNT - start_tick) + cur_tick;
  135. }
  136. // check if timeout
  137. if (timeout_tick <= elapse_tick) {
  138. // timeout
  139. return TRUE;
  140. }
  141. return FALSE;
  142. }
  143. //===========================================================================
  144. // us interface
  145. //===========================================================================
  146. U32 gpt4_tick2time_us (U32 tick)
  147. {
  148. return ((tick + (GPT4_1US_TICK - 1)) / GPT4_1US_TICK);
  149. }
  150. U32 gpt4_time2tick_us (U32 time_us)
  151. {
  152. if (GPT4_MAX_US_TIMEOUT <= time_us) {
  153. return GPT4_MAX_US_TIMEOUT;
  154. } else {
  155. return TIME_TO_TICK_US (time_us);
  156. }
  157. }
  158. //===========================================================================
  159. // ms interface
  160. //===========================================================================
  161. U32 gpt4_tick2time_ms (U32 tick)
  162. {
  163. return ((tick + (GPT4_1MS_TICK - 1)) / GPT4_1MS_TICK);
  164. }
  165. U32 global_tick2time_ms (void)
  166. {
  167. volatile U32 now;
  168. if (g_boot_arg->boot_mode == DOWNLOAD_BOOT) {
  169. register U32 cur_tick;
  170. // get current tick from APXGPT because DOWNLOAD_BOOT do not ATF init
  171. cur_tick = gpt4_get_current_tick ();
  172. now = gpt4_tick2time_ms (cur_tick);
  173. } else {
  174. register U64 cur_tick;
  175. // get current tick from ARM generic timer which is enabled on ATF before LK for boottime measure
  176. cur_tick = arch_counter_get_cntpct();
  177. now = ((cur_tick & CNTPCT_BIT_MASK_L) + (CNTPCT_1MS_TICK - 1)) / CNTPCT_1MS_TICK;
  178. }
  179. return now;
  180. }
  181. U32 gpt4_time2tick_ms (U32 time_ms)
  182. {
  183. if (GPT4_MAX_MS_TIMEOUT <= time_ms) {
  184. return GPT4_MAX_MS_TIMEOUT;
  185. } else {
  186. return TIME_TO_TICK_MS (time_ms);
  187. }
  188. }
  189. //===========================================================================
  190. // bust wait
  191. //===========================================================================
  192. void gpt_busy_wait_us (U32 timeout_us)
  193. {
  194. U32 start_tick, timeout_tick;
  195. // get timeout tick
  196. timeout_tick = gpt4_time2tick_us (timeout_us);
  197. start_tick = gpt4_get_current_tick ();
  198. // wait for timeout
  199. while (!gpt4_timeout_tick (start_tick, timeout_tick));
  200. }
  201. void gpt_busy_wait_ms (U32 timeout_ms)
  202. {
  203. U32 start_tick, timeout_tick;
  204. // get timeout tick
  205. timeout_tick = gpt4_time2tick_ms (timeout_ms);
  206. start_tick = gpt4_get_current_tick ();
  207. // wait for timeout
  208. while (!gpt4_timeout_tick (start_tick, timeout_tick));
  209. }
  210. //======================================================================
  211. void reset_timer_masked (void)
  212. {
  213. lastinc = global_tick2time_ms();
  214. timestamp = 0;
  215. }
  216. ulong get_timer_masked (void)
  217. {
  218. volatile U32 now;
  219. now = global_tick2time_ms();
  220. if (now >= lastinc) {
  221. timestamp = timestamp + now - lastinc; /* normal */
  222. } else {
  223. timestamp = timestamp + MAX_REG_MS - lastinc + now; /* overflow */
  224. }
  225. lastinc = now;
  226. return timestamp;
  227. }
  228. void reset_timer (void)
  229. {
  230. reset_timer_masked ();
  231. }
  232. #define MAX_TIMESTAMP_MS 0xffffffff
  233. ulong get_timer (ulong base)
  234. {
  235. ulong current_timestamp = 0;
  236. ulong temp = 0;
  237. current_timestamp = get_timer_masked ();
  238. if (current_timestamp >= base) {
  239. /* timestamp normal */
  240. return (current_timestamp - base);
  241. }
  242. /* timestamp overflow */
  243. //dbg_print("return = 0x%x\n",MAX_TIMESTAMP_MS - ( base - current_timestamp ));
  244. temp = base - current_timestamp;
  245. return (MAX_TIMESTAMP_MS - temp);
  246. }
  247. void set_timer (ulong ticks)
  248. {
  249. timestamp = ticks;
  250. }
  251. /* delay msec mseconds */
  252. void mdelay (unsigned long msec)
  253. {
  254. gpt_busy_wait_ms(msec);
  255. }
  256. /* delay usec useconds */
  257. void udelay (unsigned long usec)
  258. {
  259. gpt_busy_wait_us(usec);
  260. }
  261. /*
  262. * This function is derived from PowerPC code (read timebase as long long).
  263. * On ARM it just returns the timer value.
  264. */
  265. unsigned long long get_ticks(void)
  266. {
  267. return (unsigned long long) get_timer (0);
  268. }
  269. /*
  270. * This function is derived from PowerPC code (timebase clock frequency).
  271. * On ARM it returns the number of timer ticks per second.
  272. */
  273. ulong get_tbclk (void)
  274. {
  275. ulong tbclk;
  276. tbclk = CFG_HZ;
  277. return tbclk;
  278. }
  279. void mtk_timer_init (void)
  280. {
  281. gpt4_init (TRUE);
  282. // init timer system
  283. reset_timer ();
  284. }
  285. /*********************************************************************************************
  286. * This following is used to wake up system for battery charge in u-boot.
  287. * Note, the maximux "ms" value of function "gpt_one_shot_irq" is 131071
  288. *********************************************************************************************/
  289. #define GPT6_CON (APXGPT_BASE+0x0060)
  290. #define GPT6_CLK (APXGPT_BASE+0x0064)
  291. #define GPT6_CMP_L (APXGPT_BASE+0x006C)
  292. #define GPT6_CMP_H (APXGPT_BASE+0x007C)
  293. #define GPT_IRQ_EN (APXGPT_BASE+0x0000)
  294. #define GPT_IRQ_ACK (APXGPT_BASE+0x0008)
  295. #define GPT6_ONE_SHOT_EN 0x0001
  296. #define GPT6_RTC_CLK 0x0010
  297. #define GPT6_IRQ_BIT (1<<5)
  298. #define GPT6_STOP_CLEAR (1<<1)
  299. void gpt_one_shot_irq(unsigned int ms)
  300. {
  301. // Using GPT6 as trigger source
  302. // 1. Stop and clear GPT
  303. DRV_WriteReg32(GPT6_CON, GPT6_STOP_CLEAR);
  304. // 2. Clear pending irq
  305. DRV_WriteReg32(GPT_IRQ_ACK, GPT6_IRQ_BIT);
  306. // 3. Configure GPT divider to 1 and using 32K clock source
  307. DRV_WriteReg32(GPT6_CLK, GPT6_RTC_CLK);
  308. // 4. Calculate and Set compare value
  309. DRV_WriteReg32(GPT6_CMP_L, 32768*ms/1000);
  310. // 5. Enabel IRQ En
  311. DRV_SetReg32(GPT_IRQ_EN, GPT6_IRQ_BIT);
  312. // 6. Start GPT one-shot
  313. DRV_WriteReg32(GPT6_CON, GPT6_ONE_SHOT_EN);
  314. }
  315. int gpt_irq_init(void)
  316. {
  317. //1. Disable all gpt irq bits
  318. DRV_WriteReg32(GPT_IRQ_EN, 0);
  319. //2. Ack all gpt irq if needed
  320. DRV_WriteReg32(GPT_IRQ_ACK, 0x3F);
  321. //3. Register gpt irq for GIC
  322. mt_irq_set_sens(MT_GPT_IRQ_ID, MT65xx_LEVEL_SENSITIVE);
  323. mt_irq_set_polarity(MT_GPT_IRQ_ID, MT65xx_POLARITY_LOW);
  324. return 0;
  325. }
  326. void gpt_irq_ack()
  327. {
  328. DRV_WriteReg32(GPT_IRQ_ACK, GPT6_IRQ_BIT);
  329. }