wm_pmu.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. #include <string.h>
  2. #include "wm_debug.h"
  3. #include "wm_regs.h"
  4. #include "wm_irq.h"
  5. #include "wm_pwm.h"
  6. #include "wm_gpio.h"
  7. #include "wm_timer.h"
  8. #include "wm_cpu.h"
  9. #include "tls_common.h"
  10. #include "wm_pmu.h"
  11. #include "wm_wifi.h"
  12. struct pmu_irq_context {
  13. tls_pmu_irq_callback callback;
  14. void *arg;
  15. };
  16. static struct pmu_irq_context pmu_timer1_context = {0};
  17. static struct pmu_irq_context pmu_timer0_context = {0};
  18. static struct pmu_irq_context pmu_gpio_wake_context = {0};
  19. static struct pmu_irq_context pmu_sdio_wake_context = {0};
  20. void PMU_TIMER1_IRQHandler(void)
  21. {
  22. tls_reg_write32(HR_PMU_INTERRUPT_SRC, BIT(1)|0x180); /* clear timer1 interrupt */
  23. tls_reg_write32(HR_PMU_TIMER1, tls_reg_read32(HR_PMU_TIMER1) & (~BIT(16)));
  24. if (NULL != pmu_timer1_context.callback)
  25. {
  26. pmu_timer1_context.callback(pmu_timer1_context.arg);
  27. }
  28. return;
  29. }
  30. void PMU_TIMER0_IRQHandler(void)
  31. {
  32. tls_reg_write32(HR_PMU_INTERRUPT_SRC, BIT(0)|0x180); /* clear timer0 interrupt */
  33. tls_reg_write32(HR_PMU_TIMER0, tls_reg_read32(HR_PMU_TIMER0) & (~BIT(16)));
  34. if (NULL != pmu_timer0_context.callback)
  35. pmu_timer0_context.callback(pmu_timer0_context.arg);
  36. return;
  37. }
  38. void PMU_GPIO_WAKE_IRQHandler(void)
  39. {
  40. tls_reg_write32(HR_PMU_INTERRUPT_SRC, BIT(2)|0x180); /* clear gpio wake interrupt */
  41. if (NULL != pmu_gpio_wake_context.callback)
  42. pmu_gpio_wake_context.callback(pmu_gpio_wake_context.arg);
  43. return;
  44. }
  45. void PMU_SDIO_WAKE_IRQHandler(void)
  46. {
  47. tls_reg_write32(HR_PMU_INTERRUPT_SRC, BIT(3)|0x180); /* clear sdio wake interrupt */
  48. if (NULL != pmu_sdio_wake_context.callback)
  49. pmu_sdio_wake_context.callback(pmu_sdio_wake_context.arg);
  50. return;
  51. }
  52. /**
  53. * @brief This function is used to register pmu timer1 interrupt
  54. *
  55. * @param[in] callback the pmu timer1 interrupt call back function
  56. * @param[in] arg parameter of call back function
  57. *
  58. * @return None
  59. *
  60. * @note
  61. * user not need clear interrupt flag.
  62. * pmu timer1 callback function is called in interrupt,
  63. * so can not operate the critical data in the callback fuuction,
  64. * recommendation to send messages to other tasks to operate it.
  65. */
  66. void tls_pmu_timer1_isr_register(tls_pmu_irq_callback callback, void *arg)
  67. {
  68. pmu_timer1_context.callback = callback;
  69. pmu_timer1_context.arg = arg;
  70. tls_irq_enable(PMU_IRQn);
  71. return;
  72. }
  73. /**
  74. * @brief This function is used to register pmu timer0 interrupt
  75. *
  76. * @param[in] callback the pmu timer0 interrupt call back function
  77. * @param[in] arg parameter of call back function
  78. *
  79. * @return None
  80. *
  81. * @note
  82. * user not need clear interrupt flag.
  83. * pmu timer0 callback function is called in interrupt,
  84. * so can not operate the critical data in the callback fuuction,
  85. * recommendation to send messages to other tasks to operate it.
  86. */
  87. void tls_pmu_timer0_isr_register(tls_pmu_irq_callback callback, void *arg)
  88. {
  89. pmu_timer0_context.callback = callback;
  90. pmu_timer0_context.arg = arg;
  91. tls_irq_enable(PMU_IRQn);
  92. return;
  93. }
  94. /**
  95. * @brief This function is used to register pmu gpio interrupt
  96. *
  97. * @param[in] callback the pmu gpio interrupt call back function
  98. * @param[in] arg parameter of call back function
  99. *
  100. * @return None
  101. *
  102. * @note
  103. * user not need clear interrupt flag.
  104. * pmu gpio callback function is called in interrupt,
  105. * so can not operate the critical data in the callback fuuction,
  106. * recommendation to send messages to other tasks to operate it.
  107. */
  108. void tls_pmu_gpio_isr_register(tls_pmu_irq_callback callback, void *arg)
  109. {
  110. pmu_gpio_wake_context.callback = callback;
  111. pmu_gpio_wake_context.arg = arg;
  112. tls_irq_enable(PMU_IRQn);
  113. return;
  114. }
  115. /**
  116. * @brief This function is used to register pmu sdio interrupt
  117. *
  118. * @param[in] callback the pmu sdio interrupt call back function
  119. * @param[in] arg parameter of call back function
  120. *
  121. * @return None
  122. *
  123. * @note
  124. * user not need clear interrupt flag.
  125. * pmu sdio callback function is called in interrupt,
  126. * so can not operate the critical data in the callback fuuction,
  127. * recommendation to send messages to other tasks to operate it.
  128. */
  129. void tls_pmu_sdio_isr_register(tls_pmu_irq_callback callback, void *arg)
  130. {
  131. pmu_sdio_wake_context.callback = callback;
  132. pmu_sdio_wake_context.arg = arg;
  133. tls_irq_enable(PMU_IRQn);
  134. return;
  135. }
  136. /**
  137. * @brief This function is used to select pmu clk
  138. *
  139. * @param[in] bypass pmu clk whether or not use bypass mode
  140. * ohter pmu clk use 32K by 40MHZ
  141. * 0 pmu clk 32K by calibration circuit
  142. *
  143. * @return None
  144. *
  145. * @note None
  146. */
  147. void tls_pmu_clk_select(u8 bypass)
  148. {
  149. u32 val;
  150. val = tls_reg_read32(HR_PMU_PS_CR);
  151. if(bypass)
  152. {
  153. val |= BIT(4);
  154. }
  155. else
  156. {
  157. val &= ~BIT(4);
  158. }
  159. val |= BIT(3);
  160. tls_reg_write32(HR_PMU_PS_CR, val);
  161. }
  162. /**
  163. * @brief This function is used to start pmu timer0
  164. *
  165. * @param[in] second vlaue of timer0 count[s]
  166. *
  167. * @return None
  168. *
  169. * @note None
  170. */
  171. void tls_pmu_timer0_start(u16 second)
  172. {
  173. u32 val;
  174. val = tls_reg_read32(HR_PMU_INTERRUPT_SRC);
  175. if (val&0x180)
  176. {
  177. tls_reg_write32(HR_PMU_INTERRUPT_SRC,val);
  178. }
  179. val = tls_reg_read32(HR_PMU_PS_CR);
  180. /*cal 32K osc*/
  181. val |= BIT(3);
  182. tls_reg_write32(HR_PMU_PS_CR, val);
  183. val = second;
  184. val |= BIT(16);
  185. tls_reg_write32(HR_PMU_TIMER0, val);
  186. }
  187. /**
  188. * @brief This function is used to stop pmu timer0
  189. *
  190. * @param None
  191. *
  192. * @return None
  193. *
  194. * @note None
  195. */
  196. void tls_pmu_timer0_stop(void)
  197. {
  198. u32 val;
  199. val = tls_reg_read32(HR_PMU_TIMER0);
  200. val &= ~BIT(16);
  201. tls_reg_write32(HR_PMU_TIMER0, val);
  202. }
  203. /**
  204. * @brief This function is used to start pmu timer1
  205. *
  206. * @param[in] second vlaue of timer1 count[ms]
  207. *
  208. * @return None
  209. *
  210. * @note None
  211. */
  212. void tls_pmu_timer1_start(u16 msec)
  213. {
  214. u32 val;
  215. val = tls_reg_read32(HR_PMU_INTERRUPT_SRC);
  216. if (val&0x180)
  217. {
  218. tls_reg_write32(HR_PMU_INTERRUPT_SRC,val);
  219. }
  220. val = tls_reg_read32(HR_PMU_PS_CR);
  221. /*cal 32K osc*/
  222. val |= BIT(3);
  223. if (!(val & BIT(4)))
  224. {
  225. tls_reg_write32(HR_PMU_PS_CR, val);
  226. if (msec < 5)
  227. {
  228. val = 5;
  229. }
  230. else
  231. {
  232. val = msec;
  233. }
  234. //默认采用最小单位1ms
  235. val = (val - 5) | (1<<16) | (0<<17) | (0<<20) | (0<<24);
  236. }
  237. else
  238. {
  239. //默认采用最小单位1ms
  240. val = (msec-1)|(1<<16) | (0<<17) | (0<<20) | (0<<24);
  241. }
  242. tls_reg_write32(HR_PMU_TIMER1, val);
  243. }
  244. /**
  245. * @brief This function is used to stop pmu timer1
  246. *
  247. * @param None
  248. *
  249. * @return None
  250. *
  251. * @note None
  252. */
  253. void tls_pmu_timer1_stop(void)
  254. {
  255. u32 val;
  256. val = tls_reg_read32(HR_PMU_TIMER1);
  257. val &= ~BIT(16);
  258. val &= ~BIT(17);
  259. tls_reg_write32(HR_PMU_TIMER1, val);
  260. }
  261. /**
  262. * @brief This function is used to start pmu goto standby
  263. *
  264. * @param None
  265. *
  266. * @return None
  267. *
  268. * @note None
  269. */
  270. void tls_pmu_standby_start(void)
  271. {
  272. u32 val;
  273. tls_irq_enable(PMU_IRQn); //默认打开中断为了清楚IO唤醒的中断标记
  274. /*Clear Sleep status after exit sleep mode and enter standby mode*/
  275. val = tls_reg_read32(HR_PMU_INTERRUPT_SRC);
  276. if (val&0x180)
  277. {
  278. tls_reg_write32(HR_PMU_INTERRUPT_SRC,val);
  279. }
  280. val = tls_reg_read32(HR_PMU_PS_CR);
  281. TLS_DBGPRT_INFO("goto standby here\n");
  282. val |= BIT(0);
  283. tls_reg_write32(HR_PMU_PS_CR, val);
  284. }
  285. /**
  286. * @brief This function is used to start pmu goto sleep
  287. *
  288. * @param None
  289. *
  290. * @return None
  291. *
  292. * @note None
  293. */
  294. void tls_pmu_sleep_start(void)
  295. {
  296. u32 val;
  297. //u32 use40M;
  298. tls_irq_enable(PMU_IRQn); //默认打开中断为了清楚IO唤醒的中断标记
  299. /*Clear Standby status after exit standby mode and enter sleep mode*/
  300. val = tls_reg_read32(HR_PMU_INTERRUPT_SRC);
  301. if (val&0x180)
  302. {
  303. tls_reg_write32(HR_PMU_INTERRUPT_SRC,val);
  304. }
  305. //val = tls_reg_read32(HR_PMU_PS_CR);
  306. //if (val&BIT(4))
  307. //{
  308. //use40M = tls_reg_read32(HR_PMU_WLAN_STTS);
  309. //use40M |= BIT(8);
  310. //tls_reg_write32(HR_PMU_WLAN_STTS, use40M);
  311. //}
  312. TLS_DBGPRT_INFO("goto sleep here\n");
  313. val |= BIT(1);
  314. tls_reg_write32(HR_PMU_PS_CR, val);
  315. }
  316. /**
  317. * @brief This function is used to close peripheral's clock
  318. *
  319. * @param[in] devices peripherals
  320. *
  321. * @return None
  322. *
  323. * @note None
  324. */
  325. void tls_close_peripheral_clock(tls_peripheral_type_s devices)
  326. {
  327. tls_reg_write32(HR_CLK_BASE_ADDR, tls_reg_read32(HR_CLK_BASE_ADDR) & ~(devices));
  328. return;
  329. }
  330. /**
  331. * @brief This function is used to open peripheral's clock
  332. *
  333. * @param[in] devices peripherals
  334. *
  335. * @return None
  336. *
  337. * @note None
  338. */
  339. void tls_open_peripheral_clock(tls_peripheral_type_s devices)
  340. {
  341. tls_reg_write32(HR_CLK_BASE_ADDR, tls_reg_read32(HR_CLK_BASE_ADDR) | devices);
  342. return;
  343. }