boot_platform.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /* Copyright (C) 2017 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. #include "boot_platform.h"
  13. #include "hal_adi_bus.h"
  14. #include "hwregs.h"
  15. #include "osi_compiler.h"
  16. #include "osi_api.h"
  17. #include "osi_tick_unit.h"
  18. #include "hal_config.h"
  19. #include "hal_chip.h"
  20. #include "hal_blue_screen.h"
  21. #include "cmsis_core.h"
  22. #include "quec_proj_config.h"
  23. #include "quec_cust_patch.h"
  24. #define PSM_MODE_REG (hwp_pmicPsm->reserved_2) // should be sync with psm sleep
  25. #define PSM_WR_PROT_MAGIC (0x454e)
  26. void bootReset(bootResetMode_t mode)
  27. {
  28. REG_LPS_APB_CFG_BOOT_MODE_T cfg_boot_mode = {};
  29. if (mode == BOOT_RESET_FORCE_DOWNLOAD)
  30. {
  31. cfg_boot_mode.v = hwp_lpsApb->cfg_boot_mode;
  32. cfg_boot_mode.b.boot_mode_pin |= 1;
  33. cfg_boot_mode.b.boot_mode_pin &= ~1; // force download, clear product test
  34. cfg_boot_mode.b.boot_mode_sw = 0;
  35. hwp_lpsApb->cfg_boot_mode = cfg_boot_mode.v;
  36. REG_AP_APB_AP_RST0_T ap_rst0 = {};
  37. ap_rst0.b.rst_ap_a5 = 1;
  38. hwp_apApb->ap_rst0 = ap_rst0.v;
  39. OSI_DEAD_LOOP;
  40. }
  41. else
  42. {
  43. #ifdef QUEC_PATCH_BOOT_RESET_MMU_PSRAM
  44. #if 1
  45. //pmic reset:通过pmic对cpu就行复位,同时pmic也会清除自身的某些寄存器,但是LDO不掉电除非配置了掉电操作
  46. REG_PMIC_RTC_ANA_SWRST_CTRL0_T swrst_ctrl0 = {halAdiBusRead(&hwp_pmicRtcAna->swrst_ctrl0)};
  47. swrst_ctrl0.b.reg_rst_en = 1;
  48. halAdiBusWrite(&hwp_pmicRtcAna->swrst_ctrl0, swrst_ctrl0.v);
  49. REG_PMIC_RTC_ANA_SOFT_RST_HW_T soft_rst_hw;
  50. soft_rst_hw.b.reg_soft_rst_sw = 1;
  51. halAdiBusWrite(&hwp_pmicRtcAna->soft_rst_hw, soft_rst_hw.v);
  52. OSI_DEAD_LOOP;
  53. #else
  54. //chip reset:没经过pmic的复位,CPU自身复位,pmic的状态不变
  55. REG_LPS_APB_RESET_SYS_SOFT_T reset_enable = {hwp_lpsApb->reset_sys_soft};
  56. reset_enable.b.chip_soft_reset = 1;
  57. hwp_lpsApb->reset_sys_soft = reset_enable.v;
  58. OSI_DEAD_LOOP;
  59. #endif
  60. #else
  61. REG_PMIC_RTC_ANA_SWRST_CTRL0_T swrst_ctrl0;
  62. swrst_ctrl0.b.reg_rst_en = 1;
  63. halAdiBusWrite(&hwp_pmicRtcAna->swrst_ctrl0, swrst_ctrl0.v);
  64. REG_PMIC_RTC_ANA_SOFT_RST_HW_T soft_rst_hw;
  65. soft_rst_hw.b.reg_soft_rst_sw = 1;
  66. halAdiBusWrite(&hwp_pmicRtcAna->soft_rst_hw, soft_rst_hw.v);
  67. #endif
  68. }
  69. }
  70. void _psmPrepare(void)
  71. {
  72. // psm pclk enable
  73. halAdiBusWrite(&hwp_pmicRtcAna->module_en0, 0x12);
  74. //rtc soft_reset
  75. halAdiBusWrite(&hwp_pmicRtcAna->soft_rst0, 0x20);
  76. //powkey_time
  77. osiDelayUS(10);
  78. halAdiBusWrite(&hwp_pmicRtcAna->soft_rst0, 0x00);
  79. //config psm register
  80. //REG_PMIC_PSM_PSM_REG_WR_PROTECT_T
  81. halAdiBusWrite(&hwp_pmicPsm->psm_reg_wr_protect, PSM_WR_PROT_MAGIC);
  82. // psm apb soft_reset
  83. halAdiBusWrite(&hwp_pmicPsm->psm_ctrl, 0x100);
  84. osiDelayUS(10);
  85. // open PSM protect register
  86. halAdiBusWrite(&hwp_pmicPsm->psm_reg_wr_protect, PSM_WR_PROT_MAGIC);
  87. // psm module soft_reset, auto clear
  88. halAdiBusWrite(&hwp_pmicPsm->psm_ctrl, 0xc00);
  89. halAdiBusWrite(&hwp_pmicPsm->psm_ctrl, 0x0);
  90. REG_PMIC_RTC_ANA_CLK32KLESS_CTRL0_T clk32kless_ctrl0;
  91. clk32kless_ctrl0.v = halAdiBusRead(&hwp_pmicRtcAna->clk32kless_ctrl0);
  92. if (clk32kless_ctrl0.b.rtc_mode == 0)
  93. {
  94. REG_PMIC_PSM_CLK_32K_XTAL_CALIBRA_SEL_T clk_32k_xtal_cal_sel;
  95. REG_ADI_CHANGE1(hwp_pmicPsm->clk_32k_xtal_calibra_sel, clk_32k_xtal_cal_sel, clk_32k_xtal_calibra_sel, 0);
  96. REG_PMIC_PSM_XTAL_POR_1ST_CLK_SEL_T xtal_por_1st_clk;
  97. REG_ADI_CHANGE1(hwp_pmicPsm->xtal_por_1st_clk_sel, xtal_por_1st_clk, xtal_por_1st_clk_sel, 0);
  98. halAdiBusWrite(&hwp_pmicPsm->psm_rc_clk_div, 0x470);
  99. }
  100. else
  101. {
  102. halAdiBusWrite(&hwp_pmicPsm->psm_rc_clk_div, 0x4f0);
  103. }
  104. halAdiBusWrite(&hwp_pmicPsm->psm_32k_cal_th, 0x808);
  105. halAdiBusWrite(&hwp_pmicPsm->psm_26m_cal_dn_th, 0x0);
  106. halAdiBusWrite(&hwp_pmicPsm->psm_26m_cal_up_th, 0xffff);
  107. halAdiBusWrite(&hwp_pmicPsm->rtc_pwr_off_th1, 0x1810);
  108. halAdiBusWrite(&hwp_pmicPsm->rtc_pwr_off_th2, 0x2820);
  109. halAdiBusWrite(&hwp_pmicPsm->rtc_pwr_off_th3, 0x40);
  110. halAdiBusWrite(&hwp_pmicPsm->rtc_pwr_on_th1, 0x2004);
  111. halAdiBusWrite(&hwp_pmicPsm->rtc_pwr_on_th2, 0x0506);
  112. halAdiBusWrite(&hwp_pmicPsm->rtc_pwr_on_th3, 0x48ff);
  113. // configure psm cal interval
  114. halAdiBusWrite(&hwp_pmicPsm->psm_cnt_interval_th, 0x6);
  115. // configure psm cal phase, <=psm_cnt_interval_th
  116. halAdiBusWrite(&hwp_pmicPsm->psm_cnt_interval_phase, 0x6);
  117. }
  118. void bootPowerOff(void)
  119. {
  120. bool pwrkey_en = false;
  121. bool wakeup_en = false;
  122. _psmPrepare();
  123. //halAdiBusWrite(&PSM_MODE_REG, OSI_SHUTDOWN_POWER_OFF);
  124. #ifdef CONFIG_PWRKEY_POWERUP
  125. pwrkey_en = true;
  126. #else
  127. pwrkey_en = false;
  128. #endif
  129. #ifdef CONFIG_WAKEUP_PIN_POWERUP
  130. wakeup_en = true;
  131. #endif
  132. // configure psm each function enable signal
  133. REG_PMIC_PSM_PSM_CTRL_T psm_ctrl = {
  134. .b.psm_en = 1,
  135. .b.rtc_pwr_on_timeout_en = 0,
  136. .b.ext_int_pwr_en = wakeup_en ? 1 : 0,
  137. .b.pbint1_pwr_en = pwrkey_en ? 1 : 0,
  138. .b.pbint2_pwr_en = 0,
  139. #ifdef CONFIG_CHARGER_POWERUP
  140. .b.charger_pwr_en = 1,
  141. #else
  142. .b.charger_pwr_en = 0,
  143. #endif
  144. .b.psm_cnt_alarm_en = 0,
  145. .b.psm_cnt_alm_en = 0,
  146. .b.psm_software_reset = 0,
  147. .b.psm_cnt_update = 1,
  148. .b.psm_cnt_en = 1,
  149. .b.psm_status_clr = 0,
  150. .b.psm_cal_en = 1,
  151. .b.rtc_32k_clk_sel = 0, // 32k less
  152. };
  153. //halAdiBusWrite(&hwp_pmicPsm->psm_alarm_cnt_l_th, 0x880);
  154. //halAdiBusWrite(&hwp_pmicPsm->psm_alarm_cnt_h_th, 0x0);
  155. //halAdiBusWrite(&hwp_pmicPsm->psm_cnt_interval_th, 0x2);
  156. //halAdiBusWrite(&hwp_pmicPsm->psm_cnt_interval_phase, 0x2);
  157. REG_PMIC_RTC_ANA_CLK32KLESS_CTRL0_T clk32kless_ctrl0;
  158. clk32kless_ctrl0.v = halAdiBusRead(&hwp_pmicRtcAna->clk32kless_ctrl0);
  159. if (clk32kless_ctrl0.b.rtc_mode == 0)
  160. {
  161. psm_ctrl.b.psm_cal_en = 0;
  162. }
  163. halAdiBusWrite(&hwp_pmicPsm->psm_ctrl, psm_ctrl.v);
  164. //halAdiBusWrite(&hwp_pmicPsm->psm_ctrl, 0x1641);
  165. REG_PMIC_PSM_PSM_STATUS_T psm_psm_status;
  166. REG_ADI_WAIT_FIELD_NEZ(psm_psm_status, hwp_pmicPsm->psm_status, psm_cnt_update_vld);
  167. halAdiBusWrite(&(hwp_pmicPsm->reserved_4), halAdiBusRead(&hwp_pmicPsm->psm_fsm_status));
  168. // power off rtc
  169. //REG_PMIC_RTC_ANA_POWER_PD_HW_T rtc_ana_power_pd_hw;
  170. //rtc_ana_power_pd_hw.b.pwr_off_seq_en = 1;
  171. //halAdiBusWrite(&hwp_pmicRtcAna->power_pd_hw, rtc_ana_power_pd_hw.v);
  172. halAdiBusWrite(&hwp_pmicRtcAna->power_pd_hw, 1);
  173. //OSI_DEAD_LOOP;
  174. }
  175. bool bootIsFromPsmSleep(void)
  176. {
  177. REG_PMIC_RTC_ANA_MODULE_EN0_T module_en0;
  178. // enable PSM module
  179. module_en0.v = halAdiBusRead(&hwp_pmicRtcAna->module_en0);
  180. module_en0.b.psm_topa_en = 1;
  181. halAdiBusWrite(&hwp_pmicRtcAna->module_en0, module_en0.v);
  182. uint32_t psm_magic = halAdiBusRead(&PSM_MODE_REG);
  183. // disable PSM module
  184. module_en0.b.psm_topa_en = 0;
  185. halAdiBusWrite(&hwp_pmicRtcAna->module_en0, module_en0.v);
  186. return (psm_magic == OSI_SHUTDOWN_PSM_SLEEP);
  187. }
  188. uint32_t bootSecondOffsetBytes(void)
  189. {
  190. return 0;
  191. }
  192. int bootPowerOnCause()
  193. {
  194. REG_PMIC_RTC_ANA_SWRST_CTRL0_T swrst_ctrl0;
  195. swrst_ctrl0.v = halAdiBusRead(&hwp_pmicRtcAna->swrst_ctrl0);
  196. if (swrst_ctrl0.b.reg_rst_en == 0)
  197. {
  198. REG_PMIC_RTC_ANA_POR_SRC_FLAG_T por_src_flag;
  199. por_src_flag.v = halAdiBusRead(&hwp_pmicRtcAna->por_src_flag);
  200. if (por_src_flag.v & (1 << 11))
  201. return OSI_BOOTCAUSE_PIN_RESET;
  202. if (por_src_flag.v & ((1 << 12) | (1 << 9) | (1 << 8)))
  203. return OSI_BOOTCAUSE_PWRKEY;
  204. if (por_src_flag.v & (1 << 6))
  205. return OSI_BOOTCAUSE_ALARM;
  206. if (por_src_flag.v & ((1 << 4) | (1 << 5) | (1 << 10)))
  207. return OSI_BOOTCAUSE_CHARGE;
  208. }
  209. return OSI_BOOTCAUSE_UNKNOWN;
  210. }
  211. void bootResetPinEnable(void)
  212. {
  213. //ext_rstn_mode : EXT_RSTN PIN function mode when 1key 7S reset0: EXT_INT 1: RESET
  214. //key2_7s_rst_en: 1: One-key Reset Mode;0: Two-key Reset Mode;
  215. //pbint_7s_rst_disable: 0: enable 7s reset function; 1: disable 7s reset function;
  216. REG_PMIC_RTC_ANA_POR_7S_CTRL_T por_7s_ctrl;
  217. REG_PMIC_RTC_ANA_MIXED_CTRL_T bat;
  218. bat.v = halAdiBusRead(&hwp_pmicRtcAna->mixed_ctrl);
  219. if (bat.b.batdet_ok == 1)
  220. {
  221. REG_ADI_CHANGE2(hwp_pmicRtcAna->por_7s_ctrl, por_7s_ctrl,
  222. ext_rstn_mode, 1, key2_7s_rst_en, 1);
  223. }
  224. #ifdef CONFIG_BOOT_PB_7S_RESET_ENABLE
  225. REG_ADI_CHANGE1(hwp_pmicRtcAna->por_7s_ctrl, por_7s_ctrl,
  226. pbint_7s_rst_disable, 0);
  227. #else
  228. REG_ADI_CHANGE1(hwp_pmicRtcAna->por_7s_ctrl, por_7s_ctrl,
  229. pbint_7s_rst_disable, 1);
  230. #endif
  231. REG_PMIC_RTC_ANA_SWRST_CTRL0_T swrst_ctrl0 = {halAdiBusRead(&hwp_pmicRtcAna->swrst_ctrl0)};
  232. swrst_ctrl0.b.ext_rstn_pd_en = 1;
  233. swrst_ctrl0.b.pb_7s_rst_pd_en = 1;
  234. swrst_ctrl0.b.reg_rst_pd_en = 1;
  235. swrst_ctrl0.b.wdg_rst_pd_en = 0;
  236. halAdiBusWrite(&hwp_pmicRtcAna->swrst_ctrl0, swrst_ctrl0.v);
  237. REG_PMIC_RTC_ANA_SWRST_CTRL1_T swrst_ctrl1 = {};
  238. //swrst_ctrl1.b.sw_rst_sdcore_pd_en = 0;
  239. //swrst_ctrl1.b.sw_rst_sdio_pd_en = 0;
  240. swrst_ctrl1.b.sw_rst_vio33_pd_en = 0;
  241. swrst_ctrl1.b.sw_rst_usb_pd_en = 0;
  242. swrst_ctrl1.b.sw_rst_rf15_pd_en = 0;
  243. swrst_ctrl1.b.sw_rst_ana_pd_en = 0;
  244. swrst_ctrl1.b.sw_rst_rf12_pd_en = 0;
  245. swrst_ctrl1.b.sw_rst_dcxo_pd_en = 0;
  246. #ifdef QUEC_PATCH_BOOT_RESET_MMU_PSRAM
  247. swrst_ctrl1.b.sw_rst_mem_pd_en = 1; //reset时Vmem掉电
  248. #else
  249. swrst_ctrl1.b.sw_rst_mem_pd_en = 0; //Software reset LDO_MEM_PD enable when global reset valid. 0: disable 1: enable
  250. #endif
  251. swrst_ctrl1.b.sw_rst_dcdccore_pd_en = 0;
  252. swrst_ctrl1.b.sw_rst_dcdcgen_pd_en = 0;
  253. swrst_ctrl1.b.sw_rst_vio18_pd_en = 0;
  254. swrst_ctrl1.b.sw_rst_spimem_pd_en = 0;
  255. halAdiBusWrite(&hwp_pmicRtcAna->swrst_ctrl1, swrst_ctrl1.v);
  256. }
  257. static void prvMedConfig(void)
  258. {
  259. REG_MED_MED_WORK_MODE_T med_work_mode = {hwp_med->med_work_mode};
  260. med_work_mode.b.med_key_iv_sel = 1;
  261. hwp_med->med_work_mode = med_work_mode.v;
  262. REG_MED_MED_INT_EN_T med_int_en = {};
  263. med_int_en.b.med_wr_done_int_en = 1;
  264. hwp_med->med_int_en = med_int_en.v;
  265. REG_MED_MED_CH0_WORK_CFG_T med_ch0_work_cfg = {
  266. .b.med_ch0_enable = 1,
  267. .b.med_ch0_bypass_en = 1,
  268. };
  269. #ifdef CONFIG_MED_CODE_ENCRYPT
  270. med_ch0_work_cfg.b.med_ch0_bypass_en = 0;
  271. hwp_med->med_ch0_work_cfg = med_ch0_work_cfg.v;
  272. hwp_med->med_ch0_addr_size_cfg = HAL_FLASH_OFFSET(CONFIG_FS_SYS_FLASH_ADDRESS);
  273. hwp_med->med_ch0_base_addr_cfg = CONFIG_NOR_PHY_ADDRESS;
  274. hwp_med->med_ch0_read_addr_remap = CONFIG_NOR_PHY_ADDRESS;
  275. REG_MED_MED_CH1_WORK_CFG_T med_ch1_work_cfg = {
  276. .b.med_ch1_enable = 1,
  277. .b.med_ch1_bypass_en = 1,
  278. };
  279. hwp_med->med_ch1_work_cfg = med_ch1_work_cfg.v;
  280. hwp_med->med_ch1_addr_size_cfg = 0xffffff;
  281. hwp_med->med_ch1_base_addr_cfg = CONFIG_FS_SYS_FLASH_ADDRESS;
  282. hwp_med->med_ch1_read_addr_remap = hwp_med->med_ch1_base_addr_cfg;
  283. #else
  284. hwp_med->med_ch0_work_cfg = med_ch0_work_cfg.v;
  285. hwp_med->med_ch0_addr_size_cfg = 0xffffff;
  286. hwp_med->med_ch0_base_addr_cfg = CONFIG_NOR_PHY_ADDRESS;
  287. hwp_med->med_ch0_read_addr_remap = hwp_med->med_ch0_base_addr_cfg;
  288. #endif
  289. hwp_med->med_clr = 0xffffffff;
  290. }
  291. void bootPlatformInit(void)
  292. {
  293. halSysWdtStop();
  294. prvMedConfig();
  295. }
  296. OSI_NO_RETURN OSI_WEAK void bootBlueScreen(void *ctx)
  297. {
  298. halBlueScreenSaveContext();
  299. osiDCacheCleanInvalidateAll();
  300. OSI_DEAD_LOOP;
  301. }