hpm_pcfg_drv.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * Copyright (c) 2021 hpmicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_PCFG_DRV_H
  8. #define HPM_PCFG_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_pcfg_regs.h"
  11. /**
  12. *
  13. * @brief PCFG driver APIs
  14. * @defgroup pcfg_interface PCFG driver APIs
  15. * @ingroup io_interfaces
  16. * @{
  17. */
  18. #define PCFG_DCDC_MODE_TURN_OFF (0U)
  19. #define PCFG_DCDC_MODE_PERFORMACE (1U)
  20. #define PCFG_DCDC_MODE_GENERIC (2U)
  21. #define PCFG_DCDC_MODE_EXPERT (3U)
  22. #define PCFG_CLOCK_GATE_MODE_ALWAYS_ON (0x11U)
  23. #define PCFG_CLOCK_GATE_MODE_ALWAYS_OFF (0x10U)
  24. #define PCFG_CLOCK_GATE_MODE_ALWAYS_FOLLOW_FLOW (0x01U)
  25. /* @brief PCFG modules */
  26. typedef enum {
  27. pcfg_module_fuse = 0,
  28. pcfg_module_sram,
  29. pcfg_module_vad,
  30. pcfg_module_gpio,
  31. pcfg_module_ioc,
  32. pcfg_module_timer,
  33. pcfg_module_wdog,
  34. pcfg_module_uart,
  35. pcfg_module_debug,
  36. } pcfg_module_t;
  37. /* @brief PCFG irc24m reference */
  38. typedef enum {
  39. pcfg_irc24m_reference_32k = 0,
  40. pcfg_irc24m_reference_24m_xtal = 1
  41. } pcfg_irc24m_reference_t;
  42. /* @brief PCFG status */
  43. enum {
  44. status_pcfg_ldo_out_of_range = MAKE_STATUS(status_group_pcfg, 1),
  45. };
  46. /* @brief PCFG irc24m config */
  47. typedef struct {
  48. uint32_t freq_in_hz;
  49. pcfg_irc24m_reference_t reference;
  50. bool return_to_default_on_xtal_loss;
  51. bool free_run;
  52. } pcfg_irc24m_config_t;
  53. #define PCFG_CLOCK_GATE_CONTROL_MASK(module, mode) \
  54. ((uint32_t) (mode) << ((module) << 1))
  55. #define PCFG_DEBUG_STOP_SOURCE_ENABLE_CORE0 (PCFG_DEBUG_STOP_CPU0_MASK)
  56. #define PCFG_DEBUG_STOP_SOURCE_DISABLE_CORE0 (0)
  57. #define PCFG_DEBUG_STOP_SOURCE_ENABLE_CORE1 (PCFG_DEBUG_STOP_CPU1_MASK)
  58. #define PCFG_DEBUG_STOP_SOURCE_DISABLE_CORE1 (0)
  59. #ifdef __cplusplus
  60. extern "C" {
  61. #endif
  62. /**
  63. * @brief bandgap disable power save mode
  64. *
  65. * @param ptr base address
  66. */
  67. static inline void pcfg_bandgap_disable_power_save_mode(PCFG_Type *ptr)
  68. {
  69. ptr->BANDGAP &= ~PCFG_BANDGAP_POWER_SAVE_MASK;
  70. }
  71. /**
  72. * @brief bandgap enable power save mode
  73. *
  74. * @param ptr base address
  75. */
  76. static inline void pcfg_bandgap_enable_power_save_mode(PCFG_Type *ptr)
  77. {
  78. ptr->BANDGAP |= PCFG_BANDGAP_POWER_SAVE_MASK;
  79. }
  80. static inline void pcfg_bandgap_disable_lowpower_mode(PCFG_Type *ptr)
  81. {
  82. ptr->BANDGAP &= ~PCFG_BANDGAP_LOWPOWER_MODE_MASK;
  83. }
  84. /**
  85. * @brief bandgap enable low power mode
  86. *
  87. * @param ptr base address
  88. */
  89. static inline void pcfg_bandgap_enable_lowpower_mode(PCFG_Type *ptr)
  90. {
  91. ptr->BANDGAP |= PCFG_BANDGAP_LOWPOWER_MODE_MASK;
  92. }
  93. /**
  94. * @brief check if bandgap is trimmed or not
  95. *
  96. * @param ptr base address
  97. *
  98. * @return true if bandgap is trimmed
  99. */
  100. static inline bool pcfg_bandgap_is_trimmed(PCFG_Type *ptr)
  101. {
  102. return ptr->BANDGAP & PCFG_BANDGAP_VBG_TRIMMED_MASK;
  103. }
  104. /**
  105. * @brief bandgap reload trim value
  106. *
  107. * @param ptr base address
  108. */
  109. static inline void pcfg_bandgap_reload_trim(PCFG_Type *ptr)
  110. {
  111. ptr->BANDGAP &= ~PCFG_BANDGAP_VBG_TRIMMED_MASK;
  112. }
  113. /**
  114. * @brief turn off LDO 1V
  115. *
  116. * @param ptr base address
  117. */
  118. static inline void pcfg_ldo1p1_turn_off(PCFG_Type *ptr)
  119. {
  120. ptr->LDO1P1 &= ~PCFG_LDO1P1_ENABLE_MASK;
  121. }
  122. /**
  123. * @brief turn of LDO 1V
  124. *
  125. * @param ptr base address
  126. */
  127. static inline void pcfg_ldo1p1_turn_on(PCFG_Type *ptr)
  128. {
  129. ptr->LDO1P1 |= PCFG_LDO1P1_ENABLE_MASK;
  130. }
  131. /*
  132. * @brief set output voltage of LDO 1V in mV
  133. * @param ptr base address
  134. * @param mv target voltage
  135. * @retval status_success if successfully configured
  136. */
  137. hpm_stat_t pcfg_ldo1p1_set_voltage(PCFG_Type *ptr, uint16_t mv);
  138. /**
  139. * @brief turn off LDO2P5
  140. *
  141. * @param ptr base address
  142. */
  143. static inline void pcfg_ldo2p5_turn_off(PCFG_Type *ptr)
  144. {
  145. ptr->LDO2P5 &= ~PCFG_LDO2P5_ENABLE_MASK;
  146. }
  147. /**
  148. * @brief turn on LDO 2.5V
  149. *
  150. * @param ptr base address
  151. */
  152. static inline void pcfg_ldo2p5_turn_on(PCFG_Type *ptr)
  153. {
  154. ptr->LDO2P5 |= PCFG_LDO2P5_ENABLE_MASK;
  155. }
  156. /**
  157. * @brief check if LDO 2.5V is stable
  158. *
  159. * @param ptr base address
  160. *
  161. * @return true if LDO2P5 is stable
  162. */
  163. static inline bool pcfg_ldo2p5_is_stable(PCFG_Type *ptr)
  164. {
  165. return PCFG_LDO2P5_READY_GET(ptr->LDO2P5);
  166. }
  167. /*
  168. * @brief set output voltage of LDO 2.5V in mV
  169. * @param ptr base address
  170. * @param mv target voltage
  171. * @retval status_success if successfully configured
  172. */
  173. hpm_stat_t pcfg_ldo2p5_set_voltage(PCFG_Type *ptr, uint16_t mv);
  174. /*
  175. * @brief set DCDC voltage
  176. * @param ptr base address
  177. * @param mv target voltage
  178. * @retval status_success if successfully configured
  179. */
  180. hpm_stat_t pcfg_dcdc_set_voltage(PCFG_Type *ptr, uint16_t mv);
  181. /*
  182. * @brief check if DCDC is stable or not
  183. * @param ptr base address
  184. * @retval true if DCDC is stable
  185. */
  186. static inline bool pcfg_dcdc_is_stable(PCFG_Type *ptr)
  187. {
  188. return PCFG_DCDC_MODE_READY_GET(ptr->DCDC_MODE);
  189. }
  190. /*
  191. * @brief check if DCDC is stable or not
  192. * @param ptr base address
  193. */
  194. static inline void pcfg_dcdc_set_mode(PCFG_Type *ptr, uint8_t mode)
  195. {
  196. ptr->DCDC_MODE = (ptr->DCDC_MODE & ~PCFG_DCDC_MODE_MODE_MASK) | PCFG_DCDC_MODE_MODE_SET(ptr->DCDC_MODE);
  197. }
  198. /*
  199. * @brief set DCDC voltage at standby mode
  200. * @param ptr base address
  201. * @param mv target voltage
  202. * @retval status_success if successfully configured
  203. */
  204. hpm_stat_t pcfg_dcdc_set_lpmode_voltage(PCFG_Type *ptr, uint16_t mv);
  205. /**
  206. * @brief set low power current limit
  207. *
  208. * @param ptr base address
  209. * @param limit current limit at low power mode
  210. * @param under_limit set to true means current is less than limit
  211. */
  212. static inline void pcfg_dcdc_set_lp_current_limit(PCFG_Type *ptr, uint8_t limit, bool under_limit)
  213. {
  214. ptr->DCDC_PROT = (ptr->DCDC_PROT & ~(PCFG_DCDC_PROT_ILIMIT_LP_MASK | PCFG_DCDC_PROT_OVERLOAD_LP_MASK))
  215. | PCFG_DCDC_PROT_ILIMIT_LP_SET(limit) | PCFG_DCDC_PROT_OVERLOAD_LP_SET(!under_limit);
  216. }
  217. /**
  218. * @brief disable power loss protection
  219. *
  220. * @param ptr base address
  221. */
  222. static inline void pcfg_dcdc_disable_power_loss_prot(PCFG_Type *ptr)
  223. {
  224. ptr->DCDC_PROT |= PCFG_DCDC_PROT_DISABLE_POWER_LOSS_MASK;
  225. }
  226. /**
  227. * @brief enable power loss protection
  228. *
  229. * @param ptr base address
  230. */
  231. static inline void pcfg_dcdc_enable_power_loss_prot(PCFG_Type *ptr)
  232. {
  233. ptr->DCDC_PROT &= ~PCFG_DCDC_PROT_DISABLE_POWER_LOSS_MASK;
  234. }
  235. /**
  236. * @brief check if power loss flag is set
  237. *
  238. * @param ptr base address
  239. *
  240. * @return true if power loss is set
  241. */
  242. static inline bool pcfg_dcdc_is_power_loss(PCFG_Type *ptr)
  243. {
  244. return PCFG_DCDC_PROT_POWER_LOSS_FLAG_GET(ptr->DCDC_PROT);
  245. }
  246. /**
  247. * @brief disable over voltage protection
  248. *
  249. * @param ptr base address
  250. */
  251. static inline void pcfg_dcdc_disable_over_voltage_prot(PCFG_Type *ptr)
  252. {
  253. ptr->DCDC_PROT |= PCFG_DCDC_PROT_DISABLE_OVERVOLTAGE_MASK;
  254. }
  255. /**
  256. * @brief enable over voltage protection
  257. *
  258. * @param ptr base address
  259. */
  260. static inline void pcfg_dcdc_ensable_over_voltage_prot(PCFG_Type *ptr)
  261. {
  262. ptr->DCDC_PROT &= ~PCFG_DCDC_PROT_DISABLE_OVERVOLTAGE_MASK;
  263. }
  264. /**
  265. * @brief checkover voltage flag
  266. *
  267. * @param ptr base address
  268. * @return true if flag is set
  269. */
  270. static inline bool pcfg_dcdc_is_over_voltage(PCFG_Type *ptr)
  271. {
  272. return PCFG_DCDC_PROT_OVERVOLT_FLAG_GET(ptr->DCDC_PROT) & PCFG_DCDC_PROT_OVERVOLT_FLAG_MASK;
  273. }
  274. /**
  275. * @brief disable current measurement
  276. *
  277. * @param ptr base address
  278. */
  279. static inline void pcfg_dcdc_disable_measure_current(PCFG_Type *ptr)
  280. {
  281. ptr->DCDC_CURRENT &= ~PCFG_DCDC_CURRENT_ESTI_EN_MASK;
  282. }
  283. /**
  284. * @brief enable current measurement
  285. *
  286. * @param ptr base address
  287. */
  288. static inline void pcfg_dcdc_enable_measure_current(PCFG_Type *ptr)
  289. {
  290. ptr->DCDC_CURRENT |= PCFG_DCDC_CURRENT_ESTI_EN_MASK;
  291. }
  292. /**
  293. * @brief check if measured current is valid
  294. *
  295. * @param ptr base address
  296. *
  297. * @return true if measured current is valid
  298. */
  299. static inline bool pcfg_dcdc_is_measure_current_valid(PCFG_Type *ptr)
  300. {
  301. return ptr->DCDC_CURRENT & PCFG_DCDC_CURRENT_VALID_MASK;
  302. }
  303. /*
  304. * @brief get current DCDC current level in mA
  305. *
  306. * @param ptr base address
  307. * @retval Current level at mA
  308. */
  309. uint16_t pcfg_dcdc_get_current_level(PCFG_Type *ptr);
  310. /**
  311. * @brief get DCDC start time in number of 24MHz clock cycles
  312. *
  313. * @param ptr base address
  314. *
  315. * @return dcdc start time in cycles
  316. */
  317. static inline uint32_t pcfg_get_dcdc_start_time_in_cycle(PCFG_Type *ptr)
  318. {
  319. return PCFG_DCDC_START_TIME_START_TIME_GET(ptr->DCDC_START_TIME);
  320. }
  321. /**
  322. * @brief get DCDC resume time in number of 24MHz clock cycles
  323. *
  324. * @param ptr base address
  325. *
  326. * @return dcdc resuem time in cycles
  327. */
  328. static inline uint32_t pcfg_get_dcdc_resume_time_in_cycle(PCFG_Type *ptr)
  329. {
  330. return PCFG_DCDC_RESUME_TIME_RESUME_TIME_GET(ptr->DCDC_RESUME_TIME);
  331. }
  332. /**
  333. * @brief set DCDC start time in 24MHz clock cycles
  334. *
  335. * @param ptr base address
  336. * @param cycles start time in cycles
  337. */
  338. static inline void pcfg_set_dcdc_start_time_in_cycle(PCFG_Type *ptr, uint32_t cycles)
  339. {
  340. ptr->DCDC_START_TIME = PCFG_DCDC_START_TIME_START_TIME_SET(cycles);
  341. }
  342. /**
  343. * @brief set DCDC resuem time in 24MHz clock cycles
  344. *
  345. * @param ptr base address
  346. * @param cycles resume time in cycles
  347. */
  348. static inline void pcfg_set_dcdc_resume_time_in_cycle(PCFG_Type *ptr, uint32_t cycles)
  349. {
  350. ptr->DCDC_RESUME_TIME = PCFG_DCDC_RESUME_TIME_RESUME_TIME_SET(cycles);
  351. }
  352. /**
  353. * @brief disable power trap
  354. *
  355. * @param ptr base address
  356. */
  357. static inline void pcfg_disable_power_trap(PCFG_Type *ptr)
  358. {
  359. ptr->POWER_TRAP &= ~PCFG_POWER_TRAP_TRAP_MASK;
  360. }
  361. /**
  362. * @brief enable power trap
  363. *
  364. * @param ptr base address
  365. */
  366. static inline void pcfg_enable_power_trap(PCFG_Type *ptr)
  367. {
  368. ptr->POWER_TRAP |= PCFG_POWER_TRAP_TRAP_MASK;
  369. }
  370. /**
  371. * @brief check if power trap is triggered
  372. *
  373. * @param ptr base address
  374. *
  375. * @return true if power trap is triggered
  376. */
  377. static inline bool pcfg_is_power_trap_triggered(PCFG_Type *ptr)
  378. {
  379. return ptr->POWER_TRAP & PCFG_POWER_TRAP_TRIGGERED_MASK;
  380. }
  381. /**
  382. * @brief clear power trap trigger flag
  383. *
  384. * @param ptr base address
  385. */
  386. static inline void pcfg_clear_power_trap_trigger_flag(PCFG_Type *ptr)
  387. {
  388. ptr->POWER_TRAP |= PCFG_POWER_TRAP_TRIGGERED_MASK;
  389. }
  390. /**
  391. * @brief disable dcdc retention
  392. *
  393. * @param ptr base address
  394. */
  395. static inline void pcfg_disable_dcdc_retention(PCFG_Type *ptr)
  396. {
  397. ptr->POWER_TRAP &= ~PCFG_POWER_TRAP_RETENTION_MASK;
  398. }
  399. /**
  400. * @brief enable dcdc retention to retain soc sram data
  401. *
  402. * @param ptr base address
  403. */
  404. static inline void pcfg_enable_dcdc_retention(PCFG_Type *ptr)
  405. {
  406. ptr->POWER_TRAP |= PCFG_POWER_TRAP_RETENTION_MASK;
  407. }
  408. /**
  409. * @brief clear wakeup cause flag
  410. *
  411. * @param ptr base address
  412. * @param mask mask of flags to be cleared
  413. */
  414. static inline void pcfg_clear_wakeup_cause(PCFG_Type *ptr, uint32_t mask)
  415. {
  416. ptr->WAKE_CAUSE |= mask;
  417. }
  418. /**
  419. * @brief get wakeup cause
  420. *
  421. * @param ptr base address
  422. *
  423. * @return mask of wake cause
  424. */
  425. static inline uint32_t pcfg_get_wakeup_cause(PCFG_Type *ptr)
  426. {
  427. return ptr->WAKE_CAUSE;
  428. }
  429. /**
  430. * @brief enable wakeup source
  431. *
  432. * @param ptr base address
  433. * @param mask wakeup source mask
  434. */
  435. static inline void pcfg_enable_wakeup_source(PCFG_Type *ptr, uint32_t mask)
  436. {
  437. ptr->WAKE_MASK &= ~mask;
  438. }
  439. /**
  440. * @brief disable wakeup source
  441. *
  442. * @param ptr base address
  443. * @param mask source to be disabled as wakeup source
  444. */
  445. static inline void pcfg_disable_wakeup_source(PCFG_Type *ptr, uint32_t mask)
  446. {
  447. ptr->WAKE_MASK |= mask;
  448. }
  449. /**
  450. * @brief set clock gate mode in pmic domain
  451. *
  452. * @param ptr base address
  453. * @param mode clock gate mode mask
  454. */
  455. static inline void pcfg_set_clock_gate_in_pmic(PCFG_Type *ptr, uint32_t mode)
  456. {
  457. ptr->SCG_CTRL = mode;
  458. }
  459. static inline void pcfg_config_debug_stop_source(PCFG_Type *ptr, uint8_t mask)
  460. {
  461. ptr->DEBUG_STOP = mask;
  462. }
  463. /**
  464. * @brief check if irc24m is trimmed
  465. *
  466. * @param ptr base address
  467. *
  468. * @return true if it is trimmed
  469. */
  470. static inline bool pcfg_irc24m_is_trimmed(PCFG_Type *ptr)
  471. {
  472. return ptr->RC24M & PCFG_RC24M_RC_TRIMMED_MASK;
  473. }
  474. /**
  475. * @brief reload irc24m trim value
  476. *
  477. * @param ptr base address
  478. */
  479. static inline void pcfg_irc24m_reload_trim(PCFG_Type *ptr)
  480. {
  481. ptr->RC24M &= ~PCFG_RC24M_RC_TRIMMED_MASK;
  482. }
  483. /**
  484. * @brief config irc24m track
  485. *
  486. * @param ptr base address
  487. * @param config config data
  488. */
  489. void pcfg_irc24m_config_track(PCFG_Type *ptr, pcfg_irc24m_config_t *config);
  490. #ifdef __cplusplus
  491. }
  492. #endif
  493. /**
  494. * @}
  495. */
  496. #endif /* HPM_PMIC_PCFG_DRV_H */