hal_chip_8850.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _HAL_CHIP_8850_H_
  13. #define _HAL_CHIP_8850_H_
  14. #include "hal_chip.h"
  15. #include "hwregs.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. // #define HAL_RESUME_SRC_PMIC (1 << 0)
  20. // #define HAL_RESUME_SRC_VAD (1 << 1)
  21. // #define HAL_RESUME_SRC_KEY (1 << 2)
  22. // #define HAL_RESUME_SRC_GPIO1 (1 << 3)
  23. // #define HAL_RESUME_SRC_UART1 (1 << 4)
  24. // #define HAL_RESUME_SRC_UART1_RXD (1 << 5)
  25. // #define HAL_RESUME_SRC_WCN2SYS (1 << 6)
  26. // #define HAL_RESUME_SRC_WCN_OSC (1 << 7)
  27. // #define HAL_RESUME_SRC_IDLE_TIMER1 (1 << 8)
  28. // #define HAL_RESUME_SRC_IDLE_TIMER2 (1 << 9)
  29. // #define HAL_RESUME_SRC_SELF (1 << 10)
  30. // #define HAL_RESUME_SRC_USB_MON (1 << 11)
  31. #define HAL_RESUME_SRC_PMIC (1 << 0)
  32. #define HAL_RESUME_SRC_UART1 (1 << 1)
  33. #define HAL_RESUME_SRC_KEYPAD (1 << 2)
  34. #define HAL_RESUME_SRC_GPIO1 (1 << 3)
  35. #define HAL_RESUME_SRC_GPT1 (1 << 4)
  36. #define HAL_RESUME_SRC_UART1_RX (1 << 5)
  37. #define HAL_RESUME_SRC_MAILBOX_AP (1 << 6)
  38. #define HAL_RESUME_SRC_MAILBOX_CP (1 << 7)
  39. #define HAL_RESUME_SRC_UART2 (1 << 8)
  40. #define HAL_RESUME_SRC_UART3 (1 << 9)
  41. #define HAL_RESUME_SRC_GPIO2 (1 << 10)
  42. #define HAL_RESUME_SRC_GPT2_IRQ0 (1 << 11)
  43. #define HAL_RESUME_SRC_GPT2_IRQ1 (1 << 12)
  44. #define HAL_RESUME_SRC_GPT2_IRQ2 (1 << 13)
  45. #define HAL_RESUME_SRC_UART2_RX (1 << 14)
  46. #define HAL_RESUME_SRC_UART3_RX (1 << 15)
  47. #define HAL_RESUME_SRC_USB (1 << 16)
  48. #define HAL_RESUME_SRC_SPI2 (1 << 17)
  49. #define HAL_RESUME_SRC_USB_SE0 (1 << 18)
  50. #define HAL_RESUME_SRC_RTC_TIMER (1 << 19)
  51. #define HAL_RESUME_SRC_P1 (1 << 24)
  52. #define HAL_RESUME_SRC_T1 (1 << 25)
  53. #define HAL_RESUME_SRC_T2 (1 << 26)
  54. #define HAL_RESUME_SRC_T3 (1 << 27)
  55. #define HAL_RESUME_SRC_T4 (1 << 28)
  56. #define HAL_RESUME_SRC_T5 (1 << 29)
  57. #define HAL_RESUME_SRC_T6 (1 << 30)
  58. #define HAL_RESUME_SRC_P2 (1 << 23)
  59. #define HAL_RESUME_SRC_T7 (1 << 20)
  60. #define HAL_RESUME_SRC_T8 (1 << 21)
  61. #define HAL_RESUME_SRC_T9 (1 << 22)
  62. #define HAL_RESUME_ABORT (1 << 31)
  63. #define HAL_32KSLEEP_SRC_USB_RESUME (1 << 0)
  64. // The followings are output by: halMmuShowDescriptors
  65. #define HAL_DESCL1_SECTION_NORMAL_RWX 0x00005c06
  66. #define HAL_DESCL1_SECTION_NORMAL_RW 0x00005c16
  67. #define HAL_DESCL1_SECTION_NORMAL_RX 0x0000dc06
  68. #define HAL_DESCL1_SECTION_NORMAL_R 0x0000dc16
  69. #define HAL_DESCL1_SECTION_NC_RWX 0x00004c02
  70. #define HAL_DESCL1_SECTION_NC_RW 0x00004c12
  71. #define HAL_DESCL1_SECTION_NC_RX 0x0000cc02
  72. #define HAL_DESCL1_SECTION_NC_R 0x0000cc12
  73. #define HAL_DESCL1_SECTION_DEVICE_RW 0x00000c12
  74. #define HAL_DESCL1_SECTION_DEVICE_R 0x00008c12
  75. #define HAL_DESCL1_SECTION_NO_ACCESS 0x00000012
  76. #define HAL_DESCL1_SECTION_PAGE64K 0x00000001
  77. #define HAL_DESCL2_PAGE64K_NORMAL_RWX 0x00005035
  78. #define HAL_DESCL2_PAGE64K_NORMAL_RW 0x0000d035
  79. #define HAL_DESCL2_PAGE64K_NORMAL_RX 0x00005235
  80. #define HAL_DESCL2_PAGE64K_NORMAL_R 0x0000d235
  81. #define HAL_DESCL2_PAGE64K_NC_RWX 0x00004031
  82. #define HAL_DESCL2_PAGE64K_NC_RW 0x0000c031
  83. #define HAL_DESCL2_PAGE64K_NC_RX 0x00004231
  84. #define HAL_DESCL2_PAGE64K_NC_R 0x0000c231
  85. #define HAL_DESCL2_PAGE64K_DEVICE_RW 0x00008031
  86. #define HAL_DESCL2_PAGE64K_DEVICE_R 0x00008231
  87. #define HAL_DESCL2_PAGE64K_NO_ACCESS 0x00008001
  88. #define HAL_DESCL1_SECTION_PAGE4K 0x00000001
  89. #define HAL_DESCL2_PAGE4K_NORMAL_RWX 0x00000176
  90. #define HAL_DESCL2_PAGE4K_NORMAL_RW 0x00000177
  91. #define HAL_DESCL2_PAGE4K_NORMAL_RX 0x00000376
  92. #define HAL_DESCL2_PAGE4K_NORMAL_R 0x00000377
  93. #define HAL_DESCL2_PAGE4K_NC_RWX 0x00000132
  94. #define HAL_DESCL2_PAGE4K_NC_RW 0x00000133
  95. #define HAL_DESCL2_PAGE4K_NC_RX 0x00000332
  96. #define HAL_DESCL2_PAGE4K_NC_R 0x00000333
  97. #define HAL_DESCL2_PAGE4K_DEVICE_RW 0x00000033
  98. #define HAL_DESCL2_PAGE4K_DEVICE_R 0x00000233
  99. #define HAL_DESCL2_PAGE4K_NO_ACCESS 0x00000003
  100. #define HAL_TIMER_FREQ (2000000)
  101. #define HAL_TIMER_IRQ_NUM HAL_SYSIRQ_NUM(SYS_IRQ_ID_TIMER2_OS)
  102. #define HAL_TIMER_IRQ_PRIO SYS_IRQ_PRIO_TIMER2_OS
  103. #define HAL_TIMER_16K_CURVAL (hwp_timer1->hwtimer_curval) // ??
  104. #define HAL_CHIP_FLASH_DEVICE_NAME(address) ({ \
  105. uintptr_t _p = (address); \
  106. _p &= 0xff000000; \
  107. (_p == CONFIG_NOR_PHY_ADDRESS) \
  108. ? DRV_NAME_SPI_FLASH \
  109. : (_p == CONFIG_NOR_EXT_PHY_ADDRESS) \
  110. ? DRV_NAME_SPI_FLASH_EXT \
  111. : DRV_NAME_INVALID; \
  112. })
  113. OSI_FORCE_INLINE static bool HAL_CHIP_ADDR_IS_ADI(uintptr_t p)
  114. {
  115. return (p & 0xfffff000) == 0x51108000;
  116. }
  117. OSI_FORCE_INLINE static bool HAL_CHIP_ADDR_IS_SRAM(uintptr_t p)
  118. {
  119. return p >= CONFIG_SRAM_PHY_ADDRESS &&
  120. p < CONFIG_SRAM_PHY_ADDRESS + CONFIG_SRAM_SIZE;
  121. }
  122. OSI_FORCE_INLINE static bool HAL_CHIP_ADDR_IS_AON_SRAM(uintptr_t p)
  123. {
  124. return p >= CONFIG_AON_SRAM_PHY_ADDRESS &&
  125. p < CONFIG_AON_SRAM_PHY_ADDRESS + CONFIG_AON_SRAM_SIZE;
  126. }
  127. OSI_FORCE_INLINE static bool HAL_CHIP_ADDR_IS_RAM(uintptr_t p)
  128. {
  129. return p >= CONFIG_RAM_PHY_ADDRESS &&
  130. p < CONFIG_RAM_PHY_ADDRESS + CONFIG_RAM_SIZE;
  131. }
  132. OSI_FORCE_INLINE static bool HAL_CHIP_ADDR_IS_SRAM_RAM(uintptr_t p)
  133. {
  134. return HAL_CHIP_ADDR_IS_SRAM(p) || HAL_CHIP_ADDR_IS_AON_SRAM(p) || HAL_CHIP_ADDR_IS_RAM(p);
  135. }
  136. /**
  137. * \brief enable hardware timer
  138. */
  139. OSI_FORCE_INLINE static void halTimerEnable(void)
  140. {
  141. hwp_timer2->ostimer_ctrl = 0;
  142. REGT_FIELD_WRITE(hwp_timer2->timer_irq_mask_set,
  143. REG_TIMER_AP_TIMER_IRQ_MASK_SET_T,
  144. ostimer_mask, 1);
  145. }
  146. /**
  147. * \brief disable hardware timer
  148. */
  149. OSI_FORCE_INLINE static void halTimerDisable(void)
  150. {
  151. hwp_timer2->ostimer_ctrl = 0;
  152. }
  153. /**
  154. * \brief clear hardware timer interrupt
  155. */
  156. OSI_FORCE_INLINE static void halTimerIrqClear(void)
  157. {
  158. hwp_timer2->timer_irq_clr = hwp_timer2->timer_irq_cause;
  159. }
  160. /**
  161. * \brief start hardware timer
  162. *
  163. * \param loadval expire tick count, negative shall be considered inside
  164. */
  165. OSI_FORCE_INLINE static void halTimerReload(int64_t loadval)
  166. {
  167. if (loadval < 0)
  168. loadval = 0;
  169. hwp_timer2->ostimer_ctrl = 0;
  170. hwp_timer2->ostimer_loadval_l = (uint32_t)loadval;
  171. REGT_FIELD_WRITE(hwp_timer2->ostimer_ctrl,
  172. REG_TIMER_AP_OSTIMER_CTRL_T,
  173. loadval_h, loadval >> 32,
  174. enable, 1,
  175. load, 1);
  176. }
  177. /**
  178. * \brief get current tick in 32bits
  179. */
  180. OSI_FORCE_INLINE static uint32_t halTimerTick32(void)
  181. {
  182. return hwp_timer2->hwtimer_curval_l;
  183. }
  184. /**
  185. * \brief get current tick in 64bits
  186. */
  187. OSI_FORCE_INLINE static uint64_t halTimerTick64(void)
  188. {
  189. unsigned hi = hwp_timer2->hwtimer_curval_h;
  190. unsigned lo = hwp_timer2->hwtimer_curval_l;
  191. unsigned hi_next = hwp_timer2->hwtimer_curval_h;
  192. if (hi_next != hi)
  193. lo = hwp_timer2->hwtimer_curval_l;
  194. return ((uint64_t)hi_next << 32) | lo;
  195. }
  196. /**
  197. * \brief Config pmic Power key 7s reset funtion
  198. *
  199. * \param enable
  200. * - true enable 7s reset funtion.
  201. * - false disable 7s reset funtion.
  202. */
  203. void halPmuSet7sReset(bool enable);
  204. typedef enum
  205. {
  206. CLK_26M_USER_AUDIO = (1 << 0),
  207. CLK_26M_USER_WCN = (1 << 1),
  208. CLK_26M_USER_ADC = (1 << 2),
  209. CLK_26M_USER_AUX1 = (1 << 3),
  210. } clock26MUser_t;
  211. typedef enum
  212. {
  213. CLK_CAMA_USER_CAMERA = (1 << 0),
  214. CLK_CAMA_USER_AUDIO = (1 << 1),
  215. } cameraUser_t;
  216. // enum: mclk of chip output
  217. typedef enum
  218. {
  219. CAMA_CLK_OUT_FREQ_12M = 12,
  220. CAMA_CLK_OUT_FREQ_13M = 13,
  221. CAMA_CLK_OUT_FREQ_24M = 24,
  222. CAMA_CLK_OUT_FREQ_26M = 26,
  223. CAMA_CLK_OUT_FREQ_MAX
  224. } cameraClk_t;
  225. /**
  226. * \brief request 26M clock
  227. *
  228. * \param user the 26M user
  229. */
  230. void halClock26MRequest(clock26MUser_t user);
  231. /**
  232. * \brief release 26M clock
  233. *
  234. * \param user the 26M user
  235. */
  236. void halClock26MRelease(clock26MUser_t user);
  237. /**
  238. * \brief measured rc26m real frequency
  239. */
  240. extern unsigned gClkRc26mCalibFreq;
  241. /**
  242. * \brief rc26m_calib frequency, through divider
  243. */
  244. extern unsigned gClkRc26mFreq;
  245. /**
  246. * \brief get clk_rc26m_calib clock frequency
  247. *
  248. * It is determined by the real frequency after calibration, and the
  249. * compensation divider register.
  250. *
  251. * \return clk_rc26m_calib frequency
  252. */
  253. unsigned halGetRc26mCalibFreq(void);
  254. /**
  255. * \brief start clk_rc26m calibration
  256. *
  257. * During clk_rc26m calibration, mcu_pll should be always on.
  258. *
  259. * \param count calibration time, in cycle count of clk_rc26m
  260. * \param ent_enable whether to enable interrupt
  261. */
  262. void halClkRc26mCalibStart(unsigned count, bool int_enable);
  263. /**
  264. * \brief whether clk_rc26m calibration is done
  265. * \return
  266. * - true if clk_rc26m calibration is done
  267. * - false if not done
  268. */
  269. bool halClkRc26mCalibIsDone(void);
  270. /**
  271. * \brief calibrated clk_rc26m frequency
  272. * \return calibrated clk_rc26m frequency
  273. */
  274. unsigned halClkRc26mCalibFreq(void);
  275. /**
  276. * \brief initial clock calibration
  277. *
  278. * In the initial clock calibration, rc26m will be calibrated. When
  279. * \p CONFIG_RC26M_CALIB_FIXED_FREQ is 0, the rc26m calibrated frequency
  280. * will be set to 80% of the measured rc26m frequency.
  281. *
  282. */
  283. void halClkInitCalib(void);
  284. /**
  285. * \brief rc26m and xtal32k calibration
  286. *
  287. * It is assumed that clk32k is xtal32k.
  288. *
  289. * When MCUPLL frequency is changed during calibration, the calibration
  290. * result will be dropped.
  291. *
  292. * \return
  293. * - true if calibration is done
  294. * - false if calibration is canceled or dropped
  295. */
  296. bool halClkReCalib(void);
  297. /**
  298. * \brief request Mclk clock for user
  299. *
  300. * \param user and clock
  301. */
  302. void halCameraClockRequest(cameraUser_t user, cameraClk_t Mclk);
  303. /**
  304. * \brief release clock for user
  305. *
  306. * \param user the user
  307. */
  308. void halCameraClockRelease(cameraUser_t user);
  309. /**
  310. * \brief get uart1/2/3/4/5/6 frequency by read registers
  311. * \param name device name
  312. * \return uart clock frequency
  313. */
  314. unsigned halGetUartClkFreq(uint32_t name);
  315. /**
  316. * \brief init hw aes trng module
  317. */
  318. void halAesTrngInit();
  319. /**
  320. * \brief reset hw aes trng module
  321. */
  322. void halAesTrngReset();
  323. /**
  324. * \brief start hw aes trng module
  325. */
  326. void halAesTrngEnable();
  327. /**
  328. * \brief get hw aes generated random data, two word generated each time
  329. * \param v0 data0
  330. * \param v1 data1
  331. */
  332. void halAesTrngGetValue(uint32_t *v0, uint32_t *v1);
  333. /**
  334. * \brief check hw aes trng module if has already generated random data
  335. * \return
  336. * - true if done else false
  337. */
  338. bool halAesCheckTrngComplete();
  339. /**
  340. * \brief read 2720 RTC and convert to second
  341. *
  342. * \return rtc second
  343. */
  344. int64_t halPmuRtcReadSecond(void);
  345. /**
  346. * \brief trigger watchdog reset immediately
  347. */
  348. void halWatchdogReset(void);
  349. #ifdef __cplusplus
  350. }
  351. #endif
  352. #endif