mt_gpt.c 9.0 KB

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