dw_timer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Copyright (C) 2017-2019 Alibaba Group Holding Limited
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-08-20 zx.chen CSI Source File for timer Driver
  9. */
  10. #include <csi_config.h>
  11. #include <drv_irq.h>
  12. #include <drv_timer.h>
  13. #include <dw_timer.h>
  14. #include <soc.h>
  15. #include <csi_core.h>
  16. #define ERR_TIMER(errno) (CSI_DRV_ERRNO_TIMER_BASE | errno)
  17. #define TIMER_NULL_PARAM_CHK(para) HANDLE_PARAM_CHK(para, ERR_TIMER(DRV_ERROR_PARAMETER))
  18. typedef struct
  19. {
  20. #ifdef CONFIG_LPM
  21. uint8_t timer_power_status;
  22. uint32_t timer_regs_saved[2];
  23. #endif
  24. uint8_t idx;
  25. uint32_t base;
  26. uint32_t irq;
  27. timer_event_cb_t cb_event;
  28. uint32_t timeout; ///< the set time (us)
  29. uint32_t timeout_flag;
  30. } dw_timer_priv_t;
  31. extern int32_t target_get_timer_count(void);
  32. extern int32_t target_get_timer(int32_t idx, uint32_t *base, uint32_t *irq, void **handler);
  33. static dw_timer_priv_t timer_instance[CONFIG_TIMER_NUM];
  34. /**
  35. \brief Make all the timers in the idle state.
  36. \param[in] pointer to timer register base
  37. */
  38. static void timer_deactive_control(dw_timer_reg_t *addr)
  39. {
  40. /* stop the corresponding timer */
  41. addr->TxControl &= ~DW_TIMER_TXCONTROL_ENABLE;
  42. /* Disable interrupt. */
  43. addr->TxControl |= DW_TIMER_TXCONTROL_INTMASK;
  44. }
  45. void dw_timer_irqhandler(int idx)
  46. {
  47. dw_timer_priv_t *timer_priv = &timer_instance[idx];
  48. timer_priv->timeout_flag = 1;
  49. dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
  50. addr->TxEOI;
  51. if (timer_priv->cb_event)
  52. {
  53. return timer_priv->cb_event(idx, TIMER_EVENT_TIMEOUT);
  54. }
  55. }
  56. #ifdef CONFIG_LPM
  57. static void manage_clock(timer_handle_t handle, uint8_t enable)
  58. {
  59. if (handle == &timer_instance[0] || handle == &timer_instance[1])
  60. {
  61. drv_clock_manager_config(CLOCK_MANAGER_TIM, enable);
  62. } else if (handle == &timer_instance[3] || handle == &timer_instance[2])
  63. {
  64. drv_clock_manager_config(CLOCK_MANAGER_TIM1, enable);
  65. }
  66. }
  67. static void do_prepare_sleep_action(timer_handle_t handle)
  68. {
  69. dw_timer_priv_t *timer_priv = (dw_timer_priv_t *)handle;
  70. uint32_t *tbase = (uint32_t *)(timer_priv->base);
  71. registers_save(timer_priv->timer_regs_saved, tbase, 1);
  72. registers_save(&timer_priv->timer_regs_saved[1], tbase + 2, 1);
  73. }
  74. static void do_wakeup_sleep_action(timer_handle_t handle)
  75. {
  76. dw_timer_priv_t *timer_priv = (dw_timer_priv_t *)handle;
  77. uint32_t *tbase = (uint32_t *)(timer_priv->base);
  78. registers_restore(tbase, timer_priv->timer_regs_saved, 1);
  79. registers_restore(tbase + 2, &timer_priv->timer_regs_saved[1], 1);
  80. }
  81. #endif
  82. /**
  83. \brief Initialize TIMER Interface. 1. Initializes the resources needed for the TIMER interface 2.registers event callback function
  84. \param[in] idx instance timer index
  85. \param[in] cb_event Pointer to \ref timer_event_cb_t
  86. \return pointer to timer instance
  87. */
  88. timer_handle_t csi_timer_initialize(int32_t idx, timer_event_cb_t cb_event)
  89. {
  90. if (idx < 0 || idx >= CONFIG_TIMER_NUM)
  91. {
  92. return NULL;
  93. }
  94. uint32_t base = 0u;
  95. uint32_t irq = 0u;
  96. void *handler;
  97. int32_t real_idx = target_get_timer(idx, &base, &irq, &handler);
  98. if (real_idx != idx)
  99. {
  100. return NULL;
  101. }
  102. dw_timer_priv_t *timer_priv = &timer_instance[idx];
  103. timer_priv->base = base;
  104. timer_priv->irq = irq;
  105. timer_priv->idx = idx;
  106. dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
  107. timer_priv->timeout = DW_TIMER_INIT_DEFAULT_VALUE;
  108. #ifdef CONFIG_LPM
  109. csi_timer_power_control(timer_priv, DRV_POWER_FULL);
  110. #endif
  111. timer_deactive_control(addr);
  112. timer_priv->cb_event = cb_event;
  113. if (cb_event != NULL)
  114. {
  115. drv_irq_register(timer_priv->irq, handler);
  116. drv_irq_enable(timer_priv->irq);
  117. }
  118. return (timer_handle_t)timer_priv;
  119. }
  120. /**
  121. \brief De-initialize TIMER Interface. stops operation and releases the software resources used by the interface
  122. \param[in] handle timer handle to operate.
  123. \return error code
  124. */
  125. int32_t csi_timer_uninitialize(timer_handle_t handle)
  126. {
  127. TIMER_NULL_PARAM_CHK(handle);
  128. dw_timer_priv_t *timer_priv = (dw_timer_priv_t *)handle;
  129. dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
  130. timer_deactive_control(addr);
  131. timer_priv->cb_event = NULL;
  132. drv_irq_disable(timer_priv->irq);
  133. drv_irq_unregister(timer_priv->irq);
  134. #ifdef CONFIG_LPM
  135. csi_timer_power_control(timer_priv, DRV_POWER_OFF);
  136. #endif
  137. return 0;
  138. }
  139. int32_t csi_timer_power_control(timer_handle_t handle, csi_power_stat_e state)
  140. {
  141. TIMER_NULL_PARAM_CHK(handle);
  142. #ifdef CONFIG_LPM
  143. power_cb_t callback =
  144. {
  145. .wakeup = do_wakeup_sleep_action,
  146. .sleep = do_prepare_sleep_action,
  147. .manage_clock = manage_clock
  148. };
  149. return drv_soc_power_control(handle, state, &callback);
  150. #else
  151. return ERR_TIMER(DRV_ERROR_UNSUPPORTED);
  152. #endif
  153. }
  154. /**
  155. \brief config timer mode.
  156. \param[in] handle timer handle to operate.
  157. \param[in] mode \ref timer_mode_e
  158. \return error code
  159. */
  160. int32_t csi_timer_config(timer_handle_t handle, timer_mode_e mode)
  161. {
  162. TIMER_NULL_PARAM_CHK(handle);
  163. dw_timer_priv_t *timer_priv = handle;
  164. dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
  165. switch (mode)
  166. {
  167. case TIMER_MODE_FREE_RUNNING:
  168. addr->TxControl &= ~DW_TIMER_TXCONTROL_MODE;
  169. break;
  170. case TIMER_MODE_RELOAD:
  171. addr->TxControl |= DW_TIMER_TXCONTROL_MODE;
  172. break;
  173. default:
  174. return ERR_TIMER(DRV_ERROR_PARAMETER);
  175. }
  176. addr->TxControl |= (DW_TIMER_TXCONTROL_TRIGGER);
  177. return 0;
  178. }
  179. /**
  180. \brief Set timer.
  181. \param[in] instance timer instance to operate.
  182. \param[in] timeout the timeout value in microseconds(us).
  183. \return error code
  184. */
  185. int32_t csi_timer_set_timeout(timer_handle_t handle, uint32_t timeout)
  186. {
  187. TIMER_NULL_PARAM_CHK(handle);
  188. dw_timer_priv_t *timer_priv = handle;
  189. timer_priv->timeout = timeout;
  190. return 0;
  191. }
  192. /**
  193. \brief Start timer.
  194. \param[in] handle timer handle to operate.
  195. \return error code
  196. */
  197. int32_t csi_timer_start(timer_handle_t handle)
  198. {
  199. TIMER_NULL_PARAM_CHK(handle);
  200. dw_timer_priv_t *timer_priv = handle;
  201. timer_priv->timeout_flag = 0;
  202. uint32_t min_us = drv_get_timer_freq(timer_priv->idx) / 1000000;
  203. uint32_t load;
  204. if (((timer_priv->timeout * drv_get_timer_freq(timer_priv->idx)) / 1000000) > 0xffffffff)
  205. {
  206. return ERR_TIMER(DRV_ERROR_PARAMETER);
  207. }
  208. if (min_us)
  209. {
  210. load = (uint32_t)(timer_priv->timeout * min_us);
  211. } else
  212. {
  213. load = (uint32_t)(((uint64_t)(timer_priv->timeout) * drv_get_timer_freq(timer_priv->idx)) / 1000000);
  214. }
  215. dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
  216. if (timer_priv->timeout == 0) {
  217. addr->TxLoadCount = 0xffffffff; /* load time(us) */
  218. } else
  219. {
  220. if ((addr->TxControl | 0x2) == 0x2) {
  221. addr->TxLoadCount = 0xffffffff; /* load time(us) */
  222. } else
  223. {
  224. addr->TxLoadCount = load; /* load time(us) */
  225. }
  226. }
  227. addr->TxControl &= ~DW_TIMER_TXCONTROL_ENABLE; /* disable the timer */
  228. addr->TxControl |= DW_TIMER_TXCONTROL_ENABLE; /* enable the corresponding timer */
  229. addr->TxControl &= ~DW_TIMER_TXCONTROL_INTMASK; /* enable interrupt */
  230. /* avoid timer bug on yunvoice soc */
  231. #ifdef CONFIG_CHIP_YUNVOICE
  232. if (addr->TxCurrentValue > addr->TxLoadCount) {
  233. addr->TxControl &= ~DW_TIMER_TXCONTROL_ENABLE; /* disable the timer */
  234. addr->TxControl |= DW_TIMER_TXCONTROL_ENABLE; /* enable the corresponding timer */
  235. addr->TxControl &= ~DW_TIMER_TXCONTROL_INTMASK; /* enable interrupt */
  236. }
  237. #endif
  238. return 0;
  239. }
  240. /**
  241. \brief Stop timer.
  242. \param[in] handle timer handle to operate.
  243. \return error code
  244. */
  245. int32_t csi_timer_stop(timer_handle_t handle)
  246. {
  247. TIMER_NULL_PARAM_CHK(handle);
  248. dw_timer_priv_t *timer_priv = handle;
  249. dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
  250. addr->TxControl |= DW_TIMER_TXCONTROL_INTMASK; /* enable interrupt */
  251. addr->TxControl &= ~DW_TIMER_TXCONTROL_ENABLE; /* disable the timer */
  252. return 0;
  253. }
  254. /**
  255. \brief suspend timer.
  256. \param[in] instance timer instance to operate.
  257. \return error code
  258. */
  259. int32_t csi_timer_suspend(timer_handle_t handle)
  260. {
  261. TIMER_NULL_PARAM_CHK(handle);
  262. return ERR_TIMER(DRV_ERROR_UNSUPPORTED);
  263. }
  264. /**
  265. \brief resume timer.
  266. \param[in] handle timer handle to operate.
  267. \return error code
  268. */
  269. int32_t csi_timer_resume(timer_handle_t handle)
  270. {
  271. TIMER_NULL_PARAM_CHK(handle);
  272. dw_timer_priv_t *timer_priv = handle;
  273. dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
  274. addr->TxControl &= ~DW_TIMER_TXCONTROL_ENABLE; /* stop the corresponding timer */
  275. addr->TxControl &= DW_TIMER_TXCONTROL_ENABLE; /* restart the corresponding timer */
  276. return 0;
  277. }
  278. /**
  279. \brief get timer current value
  280. \param[in] handle timer handle to operate.
  281. \param[out] value timer current value
  282. \return error code
  283. */
  284. int32_t csi_timer_get_current_value(timer_handle_t handle, uint32_t *value)
  285. {
  286. TIMER_NULL_PARAM_CHK(handle);
  287. TIMER_NULL_PARAM_CHK(value);
  288. dw_timer_priv_t *timer_priv = handle;
  289. dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
  290. *value = addr->TxCurrentValue;
  291. return 0;
  292. }
  293. /**
  294. \brief Get TIMER status.
  295. \param[in] handle timer handle to operate.
  296. \return TIMER status \ref timer_status_t
  297. */
  298. timer_status_t csi_timer_get_status(timer_handle_t handle)
  299. {
  300. timer_status_t timer_status = {0};
  301. if (handle == NULL)
  302. {
  303. return timer_status;
  304. }
  305. dw_timer_priv_t *timer_priv = handle;
  306. dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
  307. if (addr->TxControl & DW_TIMER_TXCONTROL_ENABLE)
  308. {
  309. timer_status.active = 1;
  310. }
  311. if (timer_priv->timeout_flag == 1)
  312. {
  313. timer_status.timeout = 1;
  314. }
  315. return timer_status;
  316. }
  317. /**
  318. \brief get timer reload value
  319. \param[in] handle timer handle to operate.
  320. \param[out] value timer reload value
  321. \return error code
  322. */
  323. int32_t csi_timer_get_load_value(timer_handle_t handle, uint32_t *value)
  324. {
  325. TIMER_NULL_PARAM_CHK(handle);
  326. TIMER_NULL_PARAM_CHK(value);
  327. dw_timer_priv_t *timer_priv = handle;
  328. dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
  329. *value = addr->TxLoadCount;
  330. return 0;
  331. }