mt_gpt_v1.c 8.6 KB

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