soc_sleep.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file soc_sleep.c sleep for Actions SoC
  8. */
  9. #include <zephyr.h>
  10. #include <board.h>
  11. #include <arch/arm/aarch32/cortex_m/cmsis.h>
  12. #include <string.h>
  13. #include <drivers/timer/system_timer.h>
  14. #include <linker/linker-defs.h>
  15. #include <partition/partition.h>
  16. #include "spicache.h"
  17. #include <act_arm_mpu.h>
  18. #define HOSC_CTL_READY_SHIFT 28
  19. #define HOSC_CTL_READY_MASK (0x1 << HOSC_CTL_READY_SHIFT)
  20. //#define CONFIG_SLEEP_PRINTK_DEBUG
  21. //#define CONFIG_SLEEP_STAGE_DEBUG
  22. //#define CONFIG_SLEEP_DISABLE_BT
  23. //#define CONFIG_JTAG_DEBUG
  24. //#define CONFIG_SLEEP_WD_DIS_DEBUG
  25. //#define CONFIG_SLEEP_DUMP_INFO
  26. /*sleep mode*/
  27. #define SLEEP_MODE_NORMAL 0 /*normal sleep*/
  28. #define SLEEP_MODE_BT_OFF 1 /*sleep bt power off*/
  29. #define SLEEP_MODE_LOWPOWER 2 /*sleep bt power off, only onoff wakeup*/
  30. #define PSRAM_START_ADDR (0x38000000)
  31. #define MEMORY_CHECK_INTEGRITY_SIZE (8 * 1024 * 1024)
  32. struct sleep_wk_data {
  33. uint16_t wksrc;
  34. uint16_t irq_en_bit; /* wksrc settings only need irq_en for version-A */
  35. uint16_t wksrc_en_bit; /* wksrc additional settings for version-B */
  36. const char *wksrc_msg;
  37. };
  38. static const struct sleep_wk_data wk_msg[] = {
  39. {SLEEP_WK_SRC_PMU, IRQ_ID_PMU, WK_ID_PMU, "PMU" },
  40. {SLEEP_WK_SRC_RTC, IRQ_ID_RTC, WK_ID_RTC, "RTC" },
  41. {SLEEP_WK_SRC_BT, IRQ_ID_BT, WK_ID_BT, "BT" },
  42. {SLEEP_WK_SRC_GPIO, IRQ_ID_GPIO, WK_ID_GPIO, "GPIO" },
  43. {SLEEP_WK_SRC_T0, IRQ_ID_TIMER0, WK_ID_TIMER0, "T0" },
  44. {SLEEP_WK_SRC_T1, IRQ_ID_TIMER1, WK_ID_TIMER1, "T1" },
  45. {SLEEP_WK_SRC_T2, IRQ_ID_TIMER2, WK_ID_TIMER2, "T2" },
  46. {SLEEP_WK_SRC_T3, IRQ_ID_TIMER3, WK_ID_TIMER3, "T3" },
  47. {SLEEP_WK_SRC_T4, IRQ_ID_TIMER4, WK_ID_TIMER4, "T4" },
  48. {SLEEP_WK_SRC_T5, IRQ_ID_TIMER5, WK_ID_TIMER5, "T5" },
  49. {SLEEP_WK_SRC_TWS, IRQ_ID_TWS, WK_ID_TWS0, "TWS" },
  50. {SLEEP_WK_SRC_SPI0MT, IRQ_ID_SPI0MT, WK_ID_SPI0MT, "SPI0MT" },
  51. {SLEEP_WK_SRC_SPI1MT, IRQ_ID_SPI1MT, WK_ID_SPI1MT, "SPI1MT" },
  52. {SLEEP_WK_SRC_IIC0MT, IRQ_ID_IIC0MT, WK_ID_IIC0MT, "IIC0MT" },
  53. {SLEEP_WK_SRC_IIC1MT, IRQ_ID_IIC1MT, WK_ID_IIC1MT, "IIC1MT" },
  54. };
  55. #define SLEEP_WKSRC_NUM ARRAY_SIZE(wk_msg)
  56. struct sleep_wk_cb {
  57. sleep_wk_callback_t wk_cb;
  58. enum S_WK_SRC_TYPE src;
  59. };
  60. /* sleep context structure */
  61. struct sleep_context_t {
  62. uint32_t sleep_mode; /*control sleep mode*/
  63. uint32_t g_sleep_cycle;
  64. enum S_WK_SRC_TYPE g_sleep_wksrc_src;
  65. struct sleep_wk_cb g_syste_cb[SLEEP_WKSRC_NUM];
  66. uint32_t g_num_cb;
  67. #ifdef CONFIG_SLEEP_MEMORY_CHECK_INTEGRITY
  68. uint32_t check_start_addr;
  69. uint32_t check_len;
  70. uint32_t check_sum;
  71. uint32_t resume_sum;
  72. #endif
  73. };
  74. static struct sleep_context_t sleep_context_obj;
  75. #define current_sleep (&sleep_context_obj)
  76. static struct sleep_wk_fun_data __act_s2_sleep_data *g_wk_fun[SLEEP_WKSRC_NUM] ;
  77. static volatile uint16_t __act_s2_sleep_data g_sleep_wksrc_en;
  78. static int64_t __act_s2_sleep_data g_sleep_update_time, g_sleep_ms;
  79. static uint32_t __act_s2_sleep_data g_sleep_st, check_cnt, g_sleep_t2cnt;
  80. #define SOC_DEEP_SLEEP_BACKUP_NUM (16)
  81. /* backup context: R0 - R14 except R12(ip) and R15(pc) ; MSP + PSP */
  82. uint32_t __act_s2_sleep_data soc_sleep_backup[SOC_DEEP_SLEEP_BACKUP_NUM];
  83. static uint32_t __act_s2_sleep_data pwrgat_irq_en0, pwrgat_irq_en1;
  84. static uint32_t __act_s2_sleep_data pwrgat_wksrc_en;
  85. void __cpu_suspend();
  86. void __cpu_resume();
  87. void arm_floating_point_init(void);
  88. /* Save and restore the registers */
  89. static const uint32_t backup_regs_addr[] = {
  90. CMU_S1CLKCTL,
  91. PMUADC_CTL,
  92. CMU_MEMCLKEN0,
  93. CMU_MEMCLKEN1,
  94. NVIC_ISER0,
  95. NVIC_ISER1,
  96. PWRGATE_DIG,
  97. CMU_SPI0CLK,
  98. };
  99. static uint32_t __act_s2_sleep_data s2_reg_backups[ARRAY_SIZE(backup_regs_addr)];
  100. /*gpio36-48, GPIO0-3 ,6,7 handle in sleep fun*/
  101. /*gpio not use int sleep*/
  102. static const uint32_t backup_regs_gpio[] = {
  103. #if CONFIG_SPINAND_3 || CONFIG_SPI_FLASH_2
  104. /*spinand */
  105. GPION_CTL(8),
  106. GPION_CTL(9),
  107. GPION_CTL(10),
  108. GPION_CTL(11),
  109. GPION_CTL(12),
  110. GPION_CTL(13),
  111. #endif
  112. #ifndef CONFIG_SENSOR_MANAGER
  113. /*g_sensor*/
  114. GPION_CTL(54), /*int*/
  115. GPION_CTL(55), /*i2cmt1 clk*/
  116. GPION_CTL(56), /*i2cmt1 dat*/
  117. /*heart_rate meter*/
  118. GPION_CTL(18), /*i2cmt0 clk*/
  119. GPION_CTL(19), /*i2cmt0 dat*/
  120. GPION_CTL(63), /*int*/
  121. #endif
  122. #if 0
  123. /*sensor*/
  124. //GPION_CTL(20), /*not use defaut highz*/
  125. GPION_CTL(21), /*sensor irq ,use in sleep*/
  126. //GPION_CTL(24), /* HR_PWR_EN ,use in sleep*/
  127. GPION_CTL(25), /*VDD1.8 eanble ,use in sleep*/
  128. GPION_CTL(33), /*GPS wake up Host ,use in sleep*/
  129. #endif
  130. #ifdef SLEEP_GPIO_REG_SET_HIGHZ
  131. SLEEP_GPIO_REG_SET_HIGHZ
  132. #endif
  133. #if IS_ENABLED(CONFIG_KNOB_ENCODER)
  134. SLEEP_KNOB_REG_SET_HIGHZ
  135. #endif
  136. };
  137. #ifdef SLEEP_AOD_GPIO_REG_UNSET_HIGHZ
  138. static const uint32_t backup_regs_aod_gpio[] = {
  139. SLEEP_AOD_GPIO_REG_UNSET_HIGHZ
  140. };
  141. #endif
  142. __sleepfunc bool sleep_policy_is_pwrgat(void)
  143. {
  144. #ifdef CONFIG_CPU_PWRGAT
  145. if (soc_dvfs_opt())
  146. return true;
  147. else
  148. return false;
  149. #else
  150. return false;
  151. #endif
  152. }
  153. static uint32_t __act_s2_sleep_data s2_gpio_reg_backups[ARRAY_SIZE(backup_regs_gpio)];
  154. #if defined(CONFIG_BOARD_NANDBOOT) || !defined(CONFIG_SPI_FLASH_ACTS)
  155. void sys_norflash_power_ctrl(uint32_t is_powerdown)
  156. {
  157. }
  158. #else
  159. extern void sys_norflash_power_ctrl(uint32_t is_powerdown);
  160. #endif
  161. /*g_cyc2ms_mul = (1000<<16) / soc_rc32K_freq() / */
  162. static uint32_t __act_s2_sleep_data g_cyc2ms_mul; //
  163. static __sleepfunc uint32_t rc32k_cyc_to_ms(uint32_t cycle)
  164. {
  165. //return (uint32_t)((uint64_t)cycle * 1000 / soc_rc32K_freq());
  166. uint64_t tmp = g_cyc2ms_mul;
  167. tmp = tmp * cycle;
  168. return (tmp >> 16);
  169. }
  170. #ifdef CONFIG_SLEEP_PRINTK_DEBUG
  171. __sleepfunc int uart_out_ch(int c, void *ctx)
  172. {
  173. ARG_UNUSED(ctx);
  174. /* send a character */
  175. if(c == '\n'){
  176. /* Wait for transmitter to be ready */
  177. while (sys_read32(UART0_REG_BASE + 12) & BIT(6));
  178. sys_write32('\r', UART0_REG_BASE + 8);
  179. }
  180. /* Wait for transmitter to be ready */
  181. while (sys_read32(UART0_REG_BASE + 12) & BIT(6));
  182. sys_write32(c, UART0_REG_BASE+8);
  183. return 0;
  184. }
  185. __sleepfunc void uart_out_flush(void)
  186. {
  187. /* Wait for transmitter complete */
  188. while (sys_read32(UART0_REG_BASE + 12) & BIT(21));
  189. }
  190. __sleepfunc int sl_printk(const char *fmt, ...)
  191. {
  192. va_list ap;
  193. va_start(ap, fmt);
  194. sl_vprintk(fmt, ap);
  195. va_end(ap);
  196. return 0;
  197. }
  198. __sleepfunc int sl_vprintk(const char *fmt, va_list ap)
  199. {
  200. uart_out_flush();
  201. pbrom_libc_api->p_cbvprintf(uart_out_ch, NULL, fmt, ap);
  202. uart_out_flush();
  203. return 0;
  204. }
  205. #else
  206. __sleepfunc int sl_printk(const char *fmt, ...)
  207. {
  208. ARG_UNUSED(fmt);
  209. return 0;
  210. }
  211. __sleepfunc int sl_vprintk(const char *fmt, va_list ap)
  212. {
  213. ARG_UNUSED(fmt);
  214. return 0;
  215. }
  216. #endif
  217. #ifdef CONFIG_SLEEP_STAGE_DEBUG
  218. __sleepfunc void sleep_stage(unsigned int index)
  219. {
  220. unsigned int val,stage;
  221. stage = (index&0x7f)+0x80;
  222. val = sys_read32(RTC_REMAIN2)& (~(0xff<<8));
  223. sys_write32( val | (stage << 8), RTC_REMAIN2);
  224. }
  225. #else
  226. void sleep_stage(int index)
  227. {
  228. }
  229. #endif
  230. void dump_stage_timer(uint32_t st, uint32_t end)
  231. {
  232. #ifdef CONFIG_SLEEP_DUMP_INFO
  233. printk("sleep use %d ms, check_cnt=%d\n", rc32k_cyc_to_ms(end - st), check_cnt);
  234. #endif
  235. }
  236. #ifdef CONFIG_SLEEP_MEMORY_CHECK_INTEGRITY
  237. /* check_sum should be in SRAM when checking 8M psram */
  238. static uint32_t __act_s2_sleep_data suspend_sum;
  239. static uint32_t check_sum(uint32_t *buf, int len)
  240. {
  241. int i;
  242. uint32_t chk = 0;
  243. for (i = 0; i < (len / 4); i++)
  244. chk += buf[i];
  245. return chk;
  246. }
  247. static void suspend_check(void)
  248. {
  249. uint32_t chk;
  250. chk = check_sum((uint32_t *)current_sleep->check_start_addr,
  251. current_sleep->check_len);
  252. suspend_sum = chk;
  253. //printk("suspend checksum:0x%x\n", chk);
  254. }
  255. static void resume_check(void)
  256. {
  257. uint32_t chk;
  258. chk = check_sum((uint32_t *)current_sleep->check_start_addr,
  259. current_sleep->check_len);
  260. current_sleep->resume_sum = chk;
  261. }
  262. static void wakeup_check_dump(void)
  263. {
  264. //uint32_t chk;
  265. //chk = check_sum((uint32_t *)current_sleep->check_start_addr,
  266. //current_sleep->check_len);
  267. printk("checksum resume:0x%x suspend:0x%x,len=0x%x\n", current_sleep->resume_sum, suspend_sum, current_sleep->check_len);
  268. if (suspend_sum != current_sleep->resume_sum){
  269. printk("\n----- error memory integrity check -----------\n");
  270. //while(1);
  271. }
  272. }
  273. #endif
  274. int sleep_register_wk_callback(enum S_WK_SRC_TYPE wk_src, struct sleep_wk_fun_data *fn_data)
  275. {
  276. if (fn_data == NULL)
  277. return -1;
  278. fn_data->next = g_wk_fun[wk_src];
  279. g_wk_fun[wk_src] = fn_data;
  280. return 0;
  281. }
  282. void sys_s3_wksrc_set(enum S_WK_SRC_TYPE src)
  283. {
  284. g_sleep_wksrc_en |= (1 << src);
  285. }
  286. void sys_s3_wksrc_clr(enum S_WK_SRC_TYPE src)
  287. {
  288. g_sleep_wksrc_en &= ~(1 << src);
  289. }
  290. enum S_WK_SRC_TYPE sys_s3_wksrc_get(void)
  291. {
  292. return current_sleep->g_sleep_wksrc_src;
  293. }
  294. void sys_s3_wksrc_init(void)
  295. {
  296. current_sleep->g_sleep_wksrc_src = SLEEP_WK_SRC_T1;
  297. }
  298. static enum S_WK_SRC_TYPE sys_sleep_check_wksrc(void)
  299. {
  300. int i;
  301. uint32_t wk_pd0, wk_pd1,wkbit;
  302. current_sleep->g_sleep_wksrc_src = 0;
  303. wk_pd0 = sys_read32(NVIC_ISPR0);
  304. wk_pd1 = sys_read32(NVIC_ISPR1);
  305. //printk("WK NVIC_ISPR0=0x%x\n", wk_pd0);
  306. //printk("WK NVIC_ISPR1=0x%x\n", wk_pd1);
  307. for (i = 0; i < SLEEP_WKSRC_NUM; i++) {
  308. if ((1 << wk_msg[i].wksrc) & g_sleep_wksrc_en) {
  309. wkbit = wk_msg[i].irq_en_bit;
  310. if (wkbit >= 32) {
  311. wkbit -= 32;
  312. if (wk_pd1 & (1 << wkbit))
  313. break;
  314. } else {
  315. if (wk_pd0 & (1 << wkbit))
  316. break;
  317. }
  318. }
  319. }
  320. if (i != SLEEP_WKSRC_NUM) {
  321. current_sleep->g_sleep_wksrc_src = wk_msg[i].wksrc;
  322. sl_dbg("wksrc=%s\n", wk_msg[i].wksrc_msg);
  323. } else {
  324. sl_dbg("no wksrc\n");
  325. current_sleep->g_sleep_wksrc_src = 0;
  326. }
  327. return current_sleep->g_sleep_wksrc_src;
  328. }
  329. static void sys_set_wksrc_before_sleep(void)
  330. {
  331. int i;
  332. uint32_t irq_en0, irq_en1;
  333. uint32_t wksrc_en;
  334. if(current_sleep->sleep_mode !=SLEEP_MODE_NORMAL){
  335. g_sleep_wksrc_en = 1<<SLEEP_WK_SRC_PMU;
  336. if(current_sleep->sleep_mode == SLEEP_MODE_BT_OFF)
  337. g_sleep_wksrc_en |= 1 << SLEEP_WK_SRC_IIC1MT;
  338. }
  339. sys_write32(sys_read32(NVIC_ISER0), NVIC_ICER0);
  340. sys_write32(sys_read32(NVIC_ISER1), NVIC_ICER1);
  341. //sys_write32(0xffffffff, NVIC_ICPR0);
  342. //sys_write32(0xffffffff, NVIC_ICPR1);
  343. if (g_sleep_wksrc_en) {
  344. irq_en0 = irq_en1 = wksrc_en = 0;
  345. for (i = 0; i < SLEEP_WKSRC_NUM; i++) {
  346. if ((1 << wk_msg[i].wksrc) & g_sleep_wksrc_en){
  347. sl_dbg("%d wksrc=%s \n",i, wk_msg[i].wksrc_msg);
  348. if (wk_msg[i].irq_en_bit >= 32) {
  349. irq_en1 |= 1 << (wk_msg[i].irq_en_bit - 32);
  350. } else {
  351. irq_en0 |= 1 << (wk_msg[i].irq_en_bit);
  352. }
  353. wksrc_en |= 1 << (wk_msg[i].wksrc_en_bit);
  354. }
  355. }
  356. if(irq_en0)
  357. sys_write32(irq_en0, NVIC_ISER0);
  358. if(irq_en1)
  359. sys_write32(irq_en1, NVIC_ISER1);
  360. /* wksrc additional settings for version-B */
  361. if (soc_dvfs_opt()) {
  362. sys_write32((sys_read32(MEMORYCTL2) & ~(0x1ff<<15)) | wksrc_en, MEMORYCTL2);
  363. pwrgat_wksrc_en = wksrc_en;
  364. }
  365. pwrgat_irq_en0 = irq_en0;
  366. pwrgat_irq_en1 = irq_en1;
  367. }
  368. #if 0
  369. printk("NVIC_ISPR0-1=0x%x,0x%x\n", sys_read32(NVIC_ISPR0), sys_read32(NVIC_ISPR1));
  370. printk("NVIC_ISER0-1=0x%x,0x%x\n", sys_read32(NVIC_ISER0), sys_read32(NVIC_ISER1));
  371. printk("NVIC_IABR0-1=0x%x,0x%x\n", sys_read32(NVIC_IABR0), sys_read32(NVIC_IABR1));
  372. #endif
  373. }
  374. __sleepfunc static void sys_restore_wksrc(void)
  375. {
  376. if(pwrgat_irq_en0)
  377. sys_write32(pwrgat_irq_en0, NVIC_ISER0);
  378. if(pwrgat_irq_en1)
  379. sys_write32(pwrgat_irq_en1, NVIC_ISER1);
  380. if (soc_dvfs_opt() && pwrgat_wksrc_en)
  381. sys_write32((sys_read32(MEMORYCTL2) & ~(0x1ff<<15)) | pwrgat_wksrc_en, MEMORYCTL2);
  382. }
  383. #ifdef SLEEP_AOD_GPIO_REG_UNSET_HIGHZ
  384. static uint32_t __act_s2_sleep_data s2_aod_gpio_reg_backups[ARRAY_SIZE(backup_regs_aod_gpio)];
  385. #endif
  386. static void sys_pm_backup_registers(void)
  387. {
  388. int i;
  389. for (i = 0; i < ARRAY_SIZE(backup_regs_gpio); i++){ /* set gpio highz */
  390. s2_gpio_reg_backups[i] = sys_read32(backup_regs_gpio[i]);
  391. sys_write32(0x1000, backup_regs_gpio[i]);
  392. }
  393. #ifdef SLEEP_AOD_GPIO_REG_UNSET_HIGHZ
  394. if (soc_get_aod_mode() == 0) {
  395. for (i = 0; i < ARRAY_SIZE(backup_regs_aod_gpio); i++) { // set gpio highz
  396. s2_aod_gpio_reg_backups[i] = sys_read32(backup_regs_aod_gpio[i]);
  397. sys_write32(0x1000, backup_regs_aod_gpio[i]);
  398. }
  399. }
  400. #endif
  401. for (i = 0; i < ARRAY_SIZE(backup_regs_addr); i++)
  402. s2_reg_backups[i] = sys_read32(backup_regs_addr[i]);
  403. }
  404. static void sys_pm_restore_registers(void)
  405. {
  406. int i;
  407. for (i = ARRAY_SIZE(backup_regs_gpio) - 1; i >= 0; i--)
  408. sys_write32(s2_gpio_reg_backups[i], backup_regs_gpio[i]);
  409. #ifdef SLEEP_AOD_GPIO_REG_UNSET_HIGHZ
  410. if (soc_get_aod_mode() == 0) {
  411. for (i = ARRAY_SIZE(backup_regs_aod_gpio) - 1; i >= 0; i--) {
  412. sys_write32(s2_aod_gpio_reg_backups[i], backup_regs_aod_gpio[i]);
  413. }
  414. }
  415. #endif
  416. for (i = ARRAY_SIZE(backup_regs_addr) - 1; i >= 0; i--) {
  417. sys_write32(s2_reg_backups[i], backup_regs_addr[i]);
  418. }
  419. }
  420. /* Save and restore the icache registers */
  421. static uint32_t __act_s2_sleep_data icache_backup_regs_addr[] = {
  422. SPI0_CTL,
  423. SPI0_DELAYCHAIN,
  424. SPICACHE_CTL,
  425. MEMORYCTL,
  426. MEMORYCTL2,
  427. MEMORYCTL3,
  428. };
  429. /* Save and restore the dcache registers */
  430. static uint32_t __act_s2_sleep_data dcache_backup_regs_addr[] = {
  431. SPI1_CTL,
  432. SPI1_DDR_MODE_CTL,
  433. SPI1_DELAYCHAIN,
  434. SPI1_DQS1_DELAYCHAIN,
  435. SPI1_CACHE_CTL,
  436. CACHE_OPERATE_ADDR_START,
  437. CACHE_OPERATE_ADDR_END,
  438. SPI1_PSRAM_MAPPING_MISS_ADDR,
  439. SPI1_GPU_CTL,
  440. };
  441. enum pwrgat_gpio_pin {
  442. NOR_CS_PIN = GPIO_0,
  443. NOR_PWR_PIN = GPIO_64,
  444. PSRAM_CS_PIN = GPIO_40,
  445. DEBUG_UART_TX_PIN = GPIO_28,
  446. DEBUG_UART_RX_PIN = GPIO_29,
  447. };
  448. enum ram_index {
  449. RAM0_IDX = 0,
  450. RAM1_IDX,
  451. RAM2_IDX,
  452. RAM3_IDX,
  453. RAM4_IDX,
  454. RAM5_IDX,
  455. RAM6_IDX,
  456. RAM7_IDX,
  457. RAM8_IDX,
  458. RAM9_IDX,
  459. RAM10_IDX,
  460. RAM11_IDX,
  461. RAM12_IDX,
  462. RAM_REGION_NUM,
  463. };
  464. #define RAM_START_ADDR 0x2FF60000
  465. #define RAM_STEP 0x00010000
  466. #define DMAIE (DMA_REG_BASE + 0x04)
  467. #define DMADEBUG (DMA_REG_BASE + 0x80)
  468. #define SDMAIE (SDMA_REG_BASE + 0x04)
  469. #define SDMA_PRIORITY (SDMA_REG_BASE + 0x70)
  470. #define SDMADEBUG (SDMA_REG_BASE + 0x80)
  471. #define SDMA_COUPLE_REG_BASE (SDMA_REG_BASE + 0xa0)
  472. #define SPI_CACHE_MAPPING_BACKUP_REG_NUM 16
  473. #define NVIC_IPR_REG_NUM 16
  474. #define DMA_NUM 10
  475. #define SDMA_NUM 5
  476. #define REG_NUM_PER_DMA 7
  477. #define REG_NUM_SDMA_COUPLE 6
  478. #define DMAX_REG_NUM (DMA_NUM * REG_NUM_PER_DMA)
  479. #define SDMAX_REG_NUM (SDMA_NUM * REG_NUM_PER_DMA)
  480. /* RAM LS-DS */
  481. #define RAM0_DS (0x1 << 0)
  482. #define RAM1_12_DS (0xfff << 1)
  483. #define RAM13_14_DS (0x3 << 13)
  484. #define RAM15_16_DS (0x3 << 15)
  485. /* CMU_MEMCLKEN0 */
  486. #define ROM_CLK_EN (0x1 << 0)
  487. #define RAM0_CLK_EN (0x1 << 1)
  488. #define RAM1_12_CLK_EN (0x3fff << 2)
  489. #define RAM15_16_CLK_EN (0xf << 20)
  490. #define SRAM_CLK_EN (0xf << 24)
  491. #define SPICACHE0_1_RAM_CLK_EN (0xf << 28)
  492. /* CMU_MEMCLKEN1 */
  493. #define BTROM_RAM_CLK_EN (0x1 << 16)
  494. static uint32_t __act_s2_sleep_data uart_ctl, uart_br;
  495. static uint32_t __act_s2_sleep_data sleep_mode;
  496. static uint32_t __act_s2_sleep_data spi_cache_mapping_backup[SPI_CACHE_MAPPING_BACKUP_REG_NUM];
  497. static uint32_t __act_s2_sleep_data icache_backups[ARRAY_SIZE(icache_backup_regs_addr)];
  498. static uint32_t __act_s2_sleep_data dcache_backups[ARRAY_SIZE(dcache_backup_regs_addr)];
  499. static uint32_t __act_s2_sleep_data vector_tbloff_backup, nvic_ipr_backup[NVIC_IPR_REG_NUM];
  500. /* dma backup */
  501. static uint32_t __act_s2_sleep_data dmax_backup[DMAX_REG_NUM];
  502. static uint32_t __act_s2_sleep_data dmaie_backup[3];
  503. /* sdma backup */
  504. static uint32_t __act_s2_sleep_data sdmaie_backup;
  505. static uint32_t __act_s2_sleep_data sdmaprio_backup;
  506. static uint32_t __act_s2_sleep_data sdmadebug_backup;
  507. static uint32_t __act_s2_sleep_data sdma_couple_backup[REG_NUM_SDMA_COUPLE];
  508. static uint32_t __act_s2_sleep_data sdmax_backup[SDMAX_REG_NUM];
  509. /* psram/nor/uart pwrgat gpio backup */
  510. static uint32_t __act_s2_sleep_data pwrgat_gpio_backups[5];
  511. void uart_suspend(void)
  512. {
  513. sleep_mode = current_sleep->sleep_mode;
  514. uart_ctl = sys_read32(UART0_REG_BASE);
  515. uart_br = sys_read32(UART0_REG_BASE + 0x10);
  516. }
  517. __sleepfunc void uart_resume(void)
  518. {
  519. /* resume uart */
  520. sys_write32(sys_read32(RMU_MRCR0) | (1 << RESET_ID_UART0), RMU_MRCR0);
  521. sys_write32(sys_read32(CMU_DEVCLKEN0) | (1 << CLOCK_ID_UART0), CMU_DEVCLKEN0);
  522. sys_write32(uart_ctl, UART0_REG_BASE);
  523. sys_write32(uart_br, UART0_REG_BASE + 0x10);
  524. }
  525. static void icache_backup_registers(void)
  526. {
  527. unsigned int i;
  528. for (i = 0; i < SPI_CACHE_MAPPING_BACKUP_REG_NUM; i++)
  529. spi_cache_mapping_backup[i] = sys_read32(SPI_CACHE_MAPPING_ADDR0 + 4 * i);
  530. for (i = 0; i < ARRAY_SIZE(icache_backup_regs_addr); i++)
  531. icache_backups[i] = sys_read32(icache_backup_regs_addr[i]);
  532. }
  533. static void dcache_backup_registers(void)
  534. {
  535. unsigned int i;
  536. for (i = 0; i < ARRAY_SIZE(dcache_backup_regs_addr); i++)
  537. dcache_backups[i] = sys_read32(dcache_backup_regs_addr[i]);
  538. }
  539. __sleepfunc static void icache_restore_registers(void)
  540. {
  541. unsigned int i;
  542. for (i = 0; i < ARRAY_SIZE(icache_backup_regs_addr); i++)
  543. sys_write32(icache_backups[i], icache_backup_regs_addr[i]);
  544. for (i = 0; i < SPI_CACHE_MAPPING_BACKUP_REG_NUM; i++)
  545. sys_write32(spi_cache_mapping_backup[i], SPI_CACHE_MAPPING_ADDR0 + 4 * i);
  546. }
  547. __sleepfunc static void dcache_restore_registers(void)
  548. {
  549. unsigned int i;
  550. for (i = 0; i < ARRAY_SIZE(dcache_backup_regs_addr); i++)
  551. sys_write32(dcache_backups[i], dcache_backup_regs_addr[i]);
  552. }
  553. static void nvic_backup_registers(void)
  554. {
  555. unsigned int i;
  556. for (i = 0; i < NVIC_IPR_REG_NUM; i++)
  557. nvic_ipr_backup[i] = sys_read32(NVIC_IPR0 + 4 * i);
  558. vector_tbloff_backup = SCB->VTOR;
  559. }
  560. __sleepfunc static void nvic_restore_registers(void)
  561. {
  562. unsigned int i;
  563. SCB->VTOR = vector_tbloff_backup;
  564. for (i = 0; i < NVIC_IPR_REG_NUM; i++)
  565. sys_write32(nvic_ipr_backup[i], NVIC_IPR0 + 4 * i);
  566. }
  567. static void dma_backup_registers(void)
  568. {
  569. unsigned int i, j;
  570. for (i = 0; i < 2; i++) {
  571. /* DMAIP DMAIE */
  572. dmaie_backup[i] = sys_read32(DMA_REG_BASE + 4 * i);
  573. }
  574. /* DMADEBUG */
  575. dmaie_backup[2] = sys_read32(DMADEBUG);
  576. for (i = 0; i < DMA_NUM; i++) {
  577. for (j = 0; j < REG_NUM_PER_DMA; j++) {
  578. /* DMAxCTL~DMAxBC */
  579. dmax_backup[i*REG_NUM_PER_DMA + j] = sys_read32(DMA_REG_BASE + 0x100*(i+1) + 4*j);
  580. }
  581. }
  582. }
  583. __sleepfunc static void dma_restore_registers(void)
  584. {
  585. unsigned int i, j;
  586. for (i = 0; i < DMA_NUM; i++) {
  587. for (j = 0; j < REG_NUM_PER_DMA; j++) {
  588. /* DMAxCTL~DMAxBC */
  589. sys_write32(dmax_backup[i*REG_NUM_PER_DMA + j], DMA_REG_BASE + 0x100*(i+1) + 4*j);
  590. }
  591. }
  592. sys_write32(dmaie_backup[2], DMADEBUG);
  593. sys_write32(dmaie_backup[1], DMAIE);
  594. }
  595. static void sdma_backup_registers(void)
  596. {
  597. unsigned int i, j;
  598. sdmaie_backup = sys_read32(SDMAIE);
  599. sdmaprio_backup = sys_read32(SDMA_PRIORITY);
  600. sdmadebug_backup = sys_read32(SDMADEBUG);
  601. for (i = 0; i < REG_NUM_SDMA_COUPLE; i++)
  602. sdma_couple_backup[i] = sys_read32(SDMA_COUPLE_REG_BASE + 4*i);
  603. for (i = 0; i < SDMA_NUM; i++) {
  604. for (j = 0; j < REG_NUM_PER_DMA; j++) {
  605. /* SDMAxCTL~SDMAxBC */
  606. sdmax_backup[i*REG_NUM_PER_DMA + j] = sys_read32(SDMA_REG_BASE + 0x100*(i+1) + 4*j);
  607. }
  608. }
  609. }
  610. __sleepfunc static void sdma_restore_registers(void)
  611. {
  612. unsigned int i, j;
  613. for (i = 0; i < SDMA_NUM; i++) {
  614. for (j = 0; j < REG_NUM_PER_DMA; j++) {
  615. /* SDMAxCTL~SDMAxBC */
  616. sys_write32(sdmax_backup[i*REG_NUM_PER_DMA + j], SDMA_REG_BASE + 0x100*(i+1) + 4*j);
  617. }
  618. }
  619. for (i = 0; i < REG_NUM_SDMA_COUPLE; i++)
  620. sys_write32(sdma_couple_backup[i], SDMA_COUPLE_REG_BASE + 4*i);
  621. sys_write32(sdmadebug_backup, SDMADEBUG);
  622. sys_write32(sdmaprio_backup, SDMA_PRIORITY);
  623. sys_write32(sdmaie_backup, SDMAIE);
  624. }
  625. __sleepfunc static void pwrgat_gpio_psram_backup_registers(void)
  626. {
  627. /* make psram cs high */
  628. pwrgat_gpio_backups[0] = sys_read32(GPION_CTL(PSRAM_CS_PIN));
  629. sys_write32(1 << (PSRAM_CS_PIN % 32), GPION_BSR(PSRAM_CS_PIN));
  630. sys_write32(0x1040, GPION_CTL(PSRAM_CS_PIN));
  631. sys_write32(1 << (PSRAM_CS_PIN % 32), GPION_BSR(PSRAM_CS_PIN));
  632. }
  633. __sleepfunc static void pwrgat_gpio_nor_backup_registers(void)
  634. {
  635. /* make nor cs high */
  636. pwrgat_gpio_backups[1] = sys_read32(GPION_CTL(NOR_CS_PIN));
  637. sys_write32(1 << (NOR_CS_PIN % 32), GPION_BSR(NOR_CS_PIN));
  638. sys_write32(0x1040, GPION_CTL(NOR_CS_PIN));
  639. sys_write32(1 << (NOR_CS_PIN % 32), GPION_BSR(NOR_CS_PIN));
  640. }
  641. __sleepfunc static void pwrgat_set_unused_uart_highz(void)
  642. {
  643. /* if debug uart is not used, we need HighZ it.
  644. * in pwrgat, need do it, otherwize consume more 180uA.
  645. */
  646. #ifdef CONFIG_SLEEP_DISABLE_BT
  647. sys_write32(0x1000, GPION_CTL(DEBUG_UART_TX_PIN));
  648. sys_write32(0x1000, GPION_CTL(DEBUG_UART_RX_PIN));
  649. #endif
  650. if (sleep_mode != SLEEP_MODE_NORMAL) {
  651. sys_write32(0x1000, GPION_CTL(DEBUG_UART_TX_PIN));
  652. sys_write32(0x1000, GPION_CTL(DEBUG_UART_RX_PIN));
  653. }
  654. }
  655. __sleepfunc static void pwrgat_gpio_nor_restore_registers(void)
  656. {
  657. sys_write32(pwrgat_gpio_backups[1], GPION_CTL(NOR_CS_PIN));
  658. }
  659. __sleepfunc static void pwrgat_gpio_psram_restore_registers(void)
  660. {
  661. sys_write32(pwrgat_gpio_backups[0], GPION_CTL(PSRAM_CS_PIN));
  662. }
  663. __sleepfunc static void pwrgat_gpio_uart_restore_registers(void)
  664. {
  665. sys_write32(pwrgat_gpio_backups[3], GPION_CTL(DEBUG_UART_TX_PIN));
  666. sys_write32(pwrgat_gpio_backups[4], GPION_CTL(DEBUG_UART_RX_PIN));
  667. }
  668. #ifdef CONFIG_SPI0_NOR_DTR_MODE
  669. struct delaychain_tbl {
  670. uint16_t volt_mv;
  671. uint16_t delaychain;
  672. };
  673. static struct delaychain_tbl __act_s2_sleep_data delay_array[] = {
  674. {900, 19}, {950, 20},
  675. {1000, 22}, {1050, 23},
  676. {1100, 25}, {1150, 26},
  677. {1200, 28}
  678. };
  679. __sleepfunc static void nor_dtr_set_matched_delaychain(uint16_t vdd_mv)
  680. {
  681. int i;
  682. for (i = 0; i < ARRAY_SIZE(delay_array); i++) {
  683. if (vdd_mv == delay_array[i].volt_mv) {
  684. sys_write32((sys_read32(SPI0_DELAYCHAIN) & ~(0x3F)) \
  685. | delay_array[i].delaychain, SPI0_DELAYCHAIN);
  686. }
  687. }
  688. }
  689. __sleepfunc static void nor_dtr_backup_delaychain(void)
  690. {
  691. icache_backups[1] = sys_read32(SPI0_DELAYCHAIN);
  692. }
  693. #endif
  694. static void sys_pwrctrl_assigned_sram(bool is_pwrdown)
  695. {
  696. uint32_t start_addr = (uint32_t)_sleep_shutdown_ram_start;
  697. uint32_t end_addr = (uint32_t)_sleep_shutdown_ram_end;
  698. uint16_t ram_start_idx = (start_addr - RAM_START_ADDR) / RAM_STEP;
  699. uint16_t ram_end_idx = (end_addr - RAM_START_ADDR) / RAM_STEP;
  700. uint32_t ram_sleep_reg = 0;
  701. uint32_t ram_clken_reg = 0;
  702. uint16_t i;
  703. if (start_addr < RAM_START_ADDR)
  704. return ;
  705. if (end_addr >= (RAM_START_ADDR + RAM_REGION_NUM * RAM_STEP))
  706. return ;
  707. /* ram region between ram_start_idx and ram_end_idx need be pwrdown or resume */
  708. for (i = ram_start_idx + 1; i < ram_end_idx; i++) {
  709. /* ram pwrdown available bit */
  710. ram_sleep_reg |= (0x1 << i);
  711. /* ram clk available bit */
  712. if (i <= RAM11_IDX)
  713. ram_clken_reg |= (0x1 << (i + 1));
  714. else if (i < RAM_REGION_NUM)
  715. ram_clken_reg |= (0x1 << (RAM11_IDX + 1 + 2*(i - RAM11_IDX)));
  716. }
  717. if (is_pwrdown) {
  718. /* shut down ram */
  719. sys_write32(sys_read32(RAM_LIGHTSLEEP) | ram_sleep_reg, RAM_LIGHTSLEEP);
  720. sys_write32(sys_read32(RAM_DEEPSLEEP) | ram_sleep_reg, RAM_DEEPSLEEP);
  721. sys_write32(sys_read32(PWRGATE_RAM) | ram_sleep_reg, PWRGATE_RAM);
  722. /* close ram clk */
  723. sys_write32(sys_read32(CMU_MEMCLKEN0) & ~ram_clken_reg, CMU_MEMCLKEN0);
  724. }
  725. else {
  726. /* open ram clk */
  727. sys_write32(sys_read32(CMU_MEMCLKEN0) | ram_clken_reg, CMU_MEMCLKEN0);
  728. /* restore ram */
  729. sys_write32(sys_read32(PWRGATE_RAM) & ~ram_sleep_reg, PWRGATE_RAM);
  730. sys_write32(sys_read32(RAM_DEEPSLEEP) & ~ram_sleep_reg, RAM_DEEPSLEEP);
  731. sys_write32(sys_read32(RAM_LIGHTSLEEP) & ~ram_sleep_reg, RAM_LIGHTSLEEP);
  732. }
  733. }
  734. static enum WK_CB_RC wakeup_system_callback(void)
  735. {
  736. int i;
  737. enum WK_CB_RC rc;
  738. if (current_sleep->g_num_cb) {
  739. rc = WK_CB_SLEEP_AGAIN;
  740. printk("wake call fun=%d\n", current_sleep->g_num_cb);
  741. for (i = 0; i < current_sleep->g_num_cb; i++){
  742. printk("call wksrc=%d fun\n", current_sleep->g_syste_cb[i].src);
  743. if (current_sleep->g_syste_cb[i].wk_cb(current_sleep->g_syste_cb[i].src)
  744. == WK_CB_RUN_SYSTEM) /* need run system */
  745. rc = WK_CB_RUN_SYSTEM;
  746. }
  747. current_sleep->g_num_cb = 0;
  748. } else {
  749. rc = WK_CB_RUN_SYSTEM;
  750. }
  751. return rc;
  752. }
  753. static void soc_pmu_onoff_wk_set(void)
  754. {
  755. //sys_write32(0x3, WIO0_CTL);
  756. sys_write32(sys_read32(PMU_INTMASK) | (1 << 1), PMU_INTMASK); /* ONOFF SHORT WAKEUP */
  757. sys_s3_wksrc_set(SLEEP_WK_SRC_PMU);
  758. //printk("PMUINTMASK=0X%X\n", sys_read32(PMU_INTMASK));
  759. }
  760. //#define CONFIG_GPIO_WAKEUP_TEST
  761. #ifdef CONFIG_GPIO_WAKEUP_TEST
  762. #define GPIO_N_WK GPIO_21
  763. //#define WIO_N_WK WIO_1
  764. static void soc_gpio_wakeup_test(void)
  765. {
  766. #ifdef GPIO_N_WK
  767. printk("gpio=%d wakeup test \n", GPIO_N_WK);
  768. sys_write32(GPIO_CTL_GPIO_INEN|GPIO_CTL_SMIT|GPIO_CTL_PULLUP| GPIO_CTL_INTC_EN | GPIO_CTL_INTC_MASK |
  769. GPIO_CTL_INC_TRIGGER_RISING_EDGE|GPIO_CTL_PADDRV_LEVEL(3), GPION_CTL(GPIO_N_WK));
  770. #else
  771. printk("wio =%d wakeup test \n", WIO_N_WK);
  772. sys_write32(GPIO_CTL_GPIO_INEN|GPIO_CTL_SMIT|GPIO_CTL_PULLUP| GPIO_CTL_INTC_EN | GPIO_CTL_INTC_MASK |
  773. GPIO_CTL_INC_TRIGGER_RISING_EDGE|GPIO_CTL_PADDRV_LEVEL(3), WIO_REG_CTL(WIO_N_WK));
  774. #endif
  775. sys_s3_wksrc_set(SLEEP_WK_SRC_GPIO);
  776. }
  777. static void soc_gpio_check(void)
  778. {
  779. #ifdef GPIO_N_WK
  780. printk("*******gpio=%d check----\n", GPIO_N_WK);
  781. if(sys_read32(GPIO_REG_IRQ_PD(GPIO_REG_BASE, GPIO_N_WK)) & GPIO_BIT(GPIO_N_WK)){
  782. printk("*******gpio=%d wakeup----\n", GPIO_N_WK);
  783. sys_write32(GPIO_BIT(GPIO_N_WK), GPIO_REG_IRQ_PD(GPIO_REG_BASE, GPIO_N_WK));
  784. }
  785. #else
  786. printk("*******wigo=%d check----\n", WIO_N_WK);
  787. if(sys_read32(WIO_REG_CTL(WIO_N_WK)) & WIO_CTL_INT_PD_MASK){
  788. printk("*******wio=%d wakeup----\n", WIO_N_WK);
  789. sys_write32(WIO_REG_CTL(WIO_N_WK), sys_read32(WIO_REG_CTL(WIO_N_WK)));
  790. }
  791. #endif
  792. }
  793. #endif
  794. static enum WK_CB_RC soc_cmu_sleep_exit(void)
  795. {
  796. SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
  797. #ifdef CONFIG_GPIO_WAKEUP_TEST
  798. soc_gpio_check();
  799. #endif
  800. sys_pm_restore_registers();
  801. sys_sleep_check_wksrc();
  802. #ifdef CONFIG_SLEEP_MEMORY_CHECK_INTEGRITY
  803. wakeup_check_dump();
  804. #endif
  805. return wakeup_system_callback();
  806. }
  807. static void soc_cmu_sleep_prepare(int force_bt_pg)
  808. {
  809. sys_pm_backup_registers(); /* backup reg */
  810. sys_s3_wksrc_set(SLEEP_WK_SRC_BT); /* set bt wakeup src */
  811. soc_pmu_onoff_wk_set(); /* set onoff wakeup src */
  812. #ifdef CONFIG_GPIO_WAKEUP_TEST
  813. soc_gpio_wakeup_test();
  814. #endif
  815. /**
  816. * RMU_MRCR0=0x1b0d5fb1
  817. * RMU_MRCR1=0xc10c000c
  818. * CMU_DEVCLKEN0=0x5b0413b1
  819. * CMU_DEVCLKEN1=0x1f0c000c bit24-28 is bt
  820. */
  821. sys_write32(0x0, PMUADC_CTL);// close ADC
  822. /* pwrgat peripheral: DISPLAY_PG/GPU_PG/DSP_AU_PG */
  823. sys_write32(sys_read32(PWRGATE_DIG)
  824. & ~(1 << PWRGATE_DIG_DISPLAY_PG)
  825. & ~(1 << PWRGATE_DIG_GPU_PG)
  826. & ~(1 << PWRGATE_DIG_DSP_AU_PG), PWRGATE_DIG);
  827. /* force bt pwrgate in debug mode(CONFIG_SLEEP_DISABLE_BT=y or sleepmode1/2) */
  828. if (force_bt_pg == 0) {
  829. sys_write32(sys_read32(RMU_MRCR1) & ~(1 << (RESET_ID_BT-32)), RMU_MRCR1); /* disable bluetooth hub */
  830. /* pwrgat peripheral: BT_PG(BT_FORCE) */
  831. sys_write32((sys_read32(PWRGATE_DIG) | (1 << PWRGATE_DIG_BT_FORCE))
  832. & ~(1 << PWRGATE_DIG_BT_PG), PWRGATE_DIG);
  833. }
  834. /*spi0 clk switch to hosc*/
  835. sys_write32(sys_read32(CMU_SPI0CLK) & ~(0x3 << 8) & ~(0xf << 0), CMU_SPI0CLK);
  836. sys_set_wksrc_before_sleep(); /* init wakeup src */
  837. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk ; /* deepsleep */
  838. }
  839. #ifdef CONFIG_DISABLE_IRQ_STAT
  840. static inline unsigned int n_irq_lock(void)
  841. {
  842. unsigned int key;
  843. unsigned int tmp;
  844. __asm__ volatile(
  845. "mov %1, %2;"
  846. "mrs %0, BASEPRI;"
  847. "msr BASEPRI, %1;"
  848. "isb;"
  849. : "=r"(key), "=r"(tmp)
  850. : "i"(_EXC_IRQ_DEFAULT_PRIO)
  851. : "memory");
  852. return key;
  853. }
  854. static inline void n_irq_unlock(unsigned int key)
  855. {
  856. __asm__ volatile(
  857. "msr BASEPRI, %0;"
  858. "isb;"
  859. : : "r"(key) : "memory");
  860. }
  861. #endif
  862. __sleepfunc static void __cpu_enter_sleep(void)
  863. {
  864. uint32_t devclk[2];
  865. uint32_t val;
  866. uint32_t wd_ctl;
  867. uint32_t reg_trace_bak;
  868. #ifdef CONFIG_SPI0_NOR_DTR_MODE
  869. uint32_t spi0_delaychain_bak;
  870. #endif
  871. //jtag_enable();
  872. /*spi0 cache disable*/
  873. //sys_clear_bit(SPICACHE_CTL, 0);
  874. sys_write32((1<<CLOCK_ID_EXINT), CMU_DEVCLKEN0);
  875. sys_write32(0x9f000000 | (1 << (CLOCK_ID_I2CMT1 - 32)), CMU_DEVCLKEN1);
  876. sys_write32(sys_read32(CMU_DEVCLKEN1) |
  877. (1 << (CLOCK_ID_TIMER0 - 32)) |
  878. (1 << (CLOCK_ID_TIMER1 - 32)) |
  879. (1 << (CLOCK_ID_TIMER2 - 32)) |
  880. (1 << (CLOCK_ID_TIMER3 - 32)) |
  881. (1 << (CLOCK_ID_TIMER4 - 32)) |
  882. (1 << (CLOCK_ID_TIMER5 - 32)), CMU_DEVCLKEN1);
  883. sys_write32(0x0, CMU_GPIOCLKCTL); /* select gpio clk RC32K */
  884. devclk[0] = sys_read32(CMU_DEVCLKEN0);
  885. devclk[1] = sys_read32(CMU_DEVCLKEN1);
  886. /* RC64M enable for spi0 clk switch to rc64m*/
  887. sys_set_bit(CMU_S1CLKCTL,2);
  888. /*cpu clk select rc4M*/
  889. if (soc_dvfs_opt())
  890. sys_write32(0x0, CMU_SYSCLK);
  891. else
  892. sys_write32(0x02000230, CMU_SYSCLK);
  893. /* NOR use RC64M */
  894. sys_write32((sys_read32(CMU_SPI0CLK) & ~(0x3 << 8) & ~(0xf << 0)) | 0x301, CMU_SPI0CLK);
  895. #ifdef CONFIG_SPI0_NOR_DTR_MODE
  896. spi0_delaychain_bak = sys_read32(SPI0_DELAYCHAIN);
  897. nor_dtr_set_matched_delaychain(950);
  898. #endif
  899. val = sys_read32(VOUT_CTL1_S1M);
  900. sys_write32((val & ~(0xfF)) | 0x88, VOUT_CTL1_S1M); //0.95
  901. soc_udelay(3);/*delay for cpuclk&spi0 clk switch ok*/
  902. /* RC64M + RC4M enable, hosc enable. hosc need be enabled to
  903. * prevent vc18 no-output happening in hardware S1<=>S3.
  904. */
  905. sys_write32(0x7, CMU_S1CLKCTL);
  906. #ifdef CONFIG_SLEEP_PRINTK_DEBUG
  907. /* hosc enable for uart */
  908. sys_set_bit(CMU_S1CLKCTL, 1);
  909. sys_set_bit(CMU_S3CLKCTL, 1);
  910. #endif
  911. if (sleep_policy_is_pwrgat()) {
  912. sys_write32((uint32_t)__cpu_resume, RTC_REMAIN4);
  913. sys_write32(0xb5, RTC_REMAIN5);
  914. }
  915. wd_ctl = sys_read32(WD_CTL);
  916. reg_trace_bak = sys_read32(0xe0043004); //trace reg bakup
  917. /* enter sleep */
  918. __asm__ volatile("cpsid i");
  919. #ifdef CONFIG_DISABLE_IRQ_STAT
  920. n_irq_unlock(0);
  921. #else
  922. irq_unlock(0);
  923. #endif
  924. if (sleep_policy_is_pwrgat()) {
  925. __cpu_suspend();
  926. }
  927. else {
  928. __asm__ volatile("dsb");
  929. __asm__ volatile("wfi");
  930. }
  931. #ifdef CONFIG_DISABLE_IRQ_STAT
  932. n_irq_lock();
  933. #else
  934. irq_lock();
  935. #endif
  936. __asm__ volatile("cpsie i");
  937. /* hosc disable for save power */
  938. sys_write32(0x5, CMU_S1CLKCTL);
  939. sys_write32(wd_ctl, WD_CTL);
  940. sys_write32(reg_trace_bak, 0xe0043004);// trace reg restore
  941. #ifdef CONFIG_SLEEP_PRINTK_DEBUG
  942. /* hosc enable for uart */
  943. sys_set_bit(CMU_S1CLKCTL, 1);
  944. sys_set_bit(CMU_S3CLKCTL, 1);
  945. #endif
  946. #ifdef CONFIG_SPI0_NOR_DTR_MODE
  947. sys_write32(spi0_delaychain_bak, SPI0_DELAYCHAIN);
  948. #endif
  949. sys_write32(val, VOUT_CTL1_S1M);
  950. soc_udelay(30);/*delay for vdd*/
  951. /* CPU USE RC64M */
  952. if (soc_dvfs_opt())
  953. sys_write32(0x03, CMU_SYSCLK);//AHB /2
  954. else
  955. sys_write32(0x203, CMU_SYSCLK);//AHB /1
  956. sys_write32(0x1, CMU_GPIOCLKCTL); /* select gpio clk RC4M */
  957. /* resume nor devclk for execute sensor algo, i2cmt0 for sampling heart rate data */
  958. sys_write32(devclk[0] | (1<<CLOCK_ID_SPI0CACHE) | (1<<CLOCK_ID_SPI0), CMU_DEVCLKEN0);
  959. sys_write32(devclk[1] | (1<<(CLOCK_ID_I2CMT0 - 32)), CMU_DEVCLKEN1);
  960. if(sys_test_bit(WD_CTL, 4))/*if wdt enable, feed wdt*/
  961. sys_set_bit(WD_CTL, 0);
  962. #ifdef CONFIG_SLEEP_WD_DIS_DEBUG
  963. sys_clear_bit(WD_CTL, 4);
  964. #endif
  965. g_sleep_t2cnt = sys_read32(T2_CNT);/*T2 counter before clock sync*/
  966. }
  967. __sleepfunc static enum WK_CB_RC check_wk_run_sram_nor(uint16_t *irq_en_bit,
  968. struct sleep_wk_cb *cb, int *cb_num)
  969. {
  970. int i;
  971. enum WK_CB_RC rc = WK_CB_SLEEP_AGAIN;
  972. enum WK_CB_RC rc_t ;
  973. enum WK_RUN_TYPE runt;
  974. uint32_t wk_pd0, wk_pd1,wkbit;
  975. bool b_nor_wk = false;
  976. struct sleep_wk_fun_data *p;
  977. wk_pd0 = sys_read32(NVIC_ISPR0);
  978. wk_pd1 = sys_read32(NVIC_ISPR1);
  979. *cb_num = 0;
  980. for(i = 0; i < SLEEP_WKSRC_NUM; i++){
  981. if(!((1 << i) & g_sleep_wksrc_en))
  982. continue;
  983. wkbit = irq_en_bit[i];
  984. if(wkbit >= 32){
  985. if(!(wk_pd1 & (1<<(wkbit - 32))))
  986. continue;
  987. }else{
  988. if(!(wk_pd0 & (1<<wkbit)))
  989. continue;
  990. }
  991. if(g_wk_fun[i]){
  992. p = g_wk_fun[i];
  993. do{
  994. if(p->wk_prep){
  995. runt = p->wk_prep(i);//
  996. if(runt == WK_RUN_IN_SRAM){
  997. rc_t = p->wk_cb(i);
  998. //if(WK_CB_RUN_SYSTEM == p->wk_cb(i))
  999. //rc = WK_CB_RUN_SYSTEM;
  1000. }else if (runt == WK_RUN_IN_NOR) {
  1001. if(!b_nor_wk){
  1002. b_nor_wk = true;
  1003. }
  1004. rc_t = p->wk_cb(i);
  1005. //if(WK_CB_RUN_SYSTEM == p->wk_cb(i))
  1006. //rc = WK_CB_RUN_SYSTEM;
  1007. }else{
  1008. rc_t = WK_CB_RUN_SYSTEM;
  1009. if(*cb_num < SLEEP_WKSRC_NUM){
  1010. cb[*cb_num].wk_cb = p->wk_cb;
  1011. cb[*cb_num].src = i;
  1012. (*cb_num)++;
  1013. }
  1014. }
  1015. if(rc_t == WK_CB_SLEEP_AGAIN){
  1016. if(wkbit < 32)
  1017. sys_write32(1<<wkbit, NVIC_ICPR0);
  1018. else
  1019. sys_write32(1<<(wkbit-32), NVIC_ICPR1);
  1020. }
  1021. }else{
  1022. rc_t = WK_CB_RUN_SYSTEM;
  1023. if(*cb_num < SLEEP_WKSRC_NUM){
  1024. cb[*cb_num].wk_cb = p->wk_cb;
  1025. cb[*cb_num].src = i;
  1026. (*cb_num)++;
  1027. }
  1028. }
  1029. p = p->next;
  1030. if(rc_t == WK_CB_RUN_SYSTEM)// if wksrc need wake up system ,wakeup sysytem
  1031. rc = WK_CB_RUN_SYSTEM;
  1032. }while(p);
  1033. if(rc_t == WK_CB_CARELESS) /*if all callback not care of this wksrc, wakeup system handle*/
  1034. rc = WK_CB_RUN_SYSTEM;
  1035. }else{
  1036. rc = WK_CB_RUN_SYSTEM; /*not wake up callback , system handle*/
  1037. }
  1038. }
  1039. if(rc == WK_CB_SLEEP_AGAIN){
  1040. //sys_write32(0xffffffff, NVIC_ICPR0);
  1041. //sys_write32(0xffffffff, NVIC_ICPR1);
  1042. if(b_nor_wk){
  1043. sys_norflash_power_ctrl(1);
  1044. }
  1045. }
  1046. return rc;
  1047. }
  1048. #ifndef CONFIG_SOC_NO_PSRAM
  1049. static void soc_psram_flush(void)
  1050. {
  1051. spi1_cache_ops(SPI_WRITEBUF_FLUSH, (void *)SPI1_BASE_ADDR, 0x1000);
  1052. spi1_cache_ops(SPI_CACHE_FLUSH_ALL, (void *)SPI1_BASE_ADDR, 0x1000);
  1053. spi1_cache_ops_wait_finshed();
  1054. }
  1055. #endif
  1056. __sleepfunc static void cpu_enter_sleep(void)
  1057. {
  1058. uint32_t corepll_backup, sysclk_bak;
  1059. unsigned int i, num_cb;
  1060. uint16_t irq_en_bit[SLEEP_WKSRC_NUM];
  1061. struct sleep_wk_cb cb[SLEEP_WKSRC_NUM];
  1062. uint32_t bk_clksrc0, bk_clksrc1, bk_vout_s1;
  1063. uint32_t devclk[2], memclk[2];
  1064. #ifdef CONFIG_SPI0_NOR_DTR_MODE
  1065. uint32_t bk_spi0_delaychain;
  1066. #endif
  1067. devclk[0] = sys_read32(CMU_DEVCLKEN0);
  1068. devclk[1] = sys_read32(CMU_DEVCLKEN1);
  1069. memclk[0] = sys_read32(CMU_MEMCLKEN0);
  1070. memclk[1] = sys_read32(CMU_MEMCLKEN1);
  1071. uart_suspend();
  1072. icache_backup_registers();
  1073. dcache_backup_registers();
  1074. nvic_backup_registers();
  1075. dma_backup_registers();
  1076. sdma_backup_registers();
  1077. sys_pwrctrl_assigned_sram(true);
  1078. /* only enable btrom_ram clk in cmu_memclken1 */
  1079. sys_write32(memclk[1] & BTROM_RAM_CLK_EN, CMU_MEMCLKEN1);
  1080. #ifndef CONFIG_SOC_NO_PSRAM
  1081. /* flush spi1 cache */
  1082. soc_psram_flush();
  1083. #ifdef CONFIG_SLEEP_MEMORY_CHECK_INTEGRITY
  1084. suspend_check();
  1085. #endif
  1086. /* disable SPI1 cache for PSRAM */
  1087. sys_clear_bit(SPI1_CACHE_CTL, 0);
  1088. #endif
  1089. for(i = 0; i < SLEEP_WKSRC_NUM; i++) // copy nor to sram, nor is not use in sleep func
  1090. irq_en_bit[i] = wk_msg[i].irq_en_bit;
  1091. #if defined(CONFIG_BOARD_EMMCBOOT) || defined(CONFIG_BOARD_NANDBOOT)
  1092. uint32_t spi1_clk, ddr_mode;
  1093. sys_set_bit(CMU_S1CLKCTL,2); // enable rc64m for psram
  1094. spi1_clk = sys_read32(CMU_SPI1CLK); // bak spi1 clk
  1095. ddr_mode = sys_read32(SPI1_DDR_MODE_CTL);
  1096. if(!soc_boot_is_mini())
  1097. sys_write32((ddr_mode & ~(0xf << 16)) | (4<<16), SPI1_DDR_MODE_CTL); // spi1 clk 64MHZ,must adjust RLC to 4 ( default is 5)
  1098. sys_write32(0x300, CMU_SPI1CLK); // psram now use rc64m
  1099. #endif
  1100. /*nor enter deep power down */
  1101. sys_norflash_power_ctrl(1);
  1102. sleep_stage(0);
  1103. /* psram enter low self refresh mode */
  1104. psram_self_refresh_control(true);
  1105. /* psram enter low power mode */
  1106. psram_power_control(true);
  1107. /* make psram cs pin in fixed state when cpu enters deepsleep */
  1108. pwrgat_gpio_psram_backup_registers();
  1109. sysclk_bak = sys_read32(CMU_SYSCLK);
  1110. /*first switch cpu clk source (hosc)*/
  1111. sys_write32((sysclk_bak&(~0x7)) | 0x1, CMU_SYSCLK);
  1112. soc_udelay(1);
  1113. /*cpu clk select HOSC*/
  1114. if (soc_dvfs_opt())
  1115. sys_write32(0x1, CMU_SYSCLK); /* AHB /2 */
  1116. else
  1117. sys_write32(0x201, CMU_SYSCLK); /* AHB /1 */
  1118. corepll_backup = sys_read32(COREPLL_CTL);
  1119. bk_vout_s1 = sys_read32(VOUT_CTL1_S1M);
  1120. /* disable COREPLL */
  1121. sys_write32(sys_read32(COREPLL_CTL) & ~(1 << 7), COREPLL_CTL);
  1122. sys_write32(0, COREPLL_CTL);
  1123. /*disable avdd, corepll use must enable*/
  1124. sys_clear_bit(AVDDLDO_CTL, 0);
  1125. /* S1 VDD 1.05V
  1126. * TODO use 0.95V
  1127. */
  1128. #ifdef CONFIG_SPI0_NOR_DTR_MODE
  1129. /* backup S1 delaychain */
  1130. bk_spi0_delaychain = sys_read32(SPI0_DELAYCHAIN);
  1131. /* set delaychain used in ds sensor algo cycle */
  1132. nor_dtr_set_matched_delaychain(950);
  1133. /* backup delaychain in ds sensor algo cycle */
  1134. nor_dtr_backup_delaychain();
  1135. #endif
  1136. //sys_write32((sys_read32(VOUT_CTL1_S1M) & ~(0xff)) | 0xaa, VOUT_CTL1_S1M);
  1137. sys_write32((sys_read32(VOUT_CTL1_S1M) & ~(0xfff)) | 0x988, VOUT_CTL1_S1M);//VDD_S1M=0.95V, VD12_S1M=0.95+0.1V
  1138. soc_udelay(30);/*delay for vdd*/
  1139. pwrgat_gpio_backups[3] = sys_read32(GPION_CTL(DEBUG_UART_TX_PIN));
  1140. pwrgat_gpio_backups[4] = sys_read32(GPION_CTL(DEBUG_UART_RX_PIN));
  1141. bk_clksrc0 = sys_read32(CMU_MEMCLKSRC0);
  1142. bk_clksrc1 = sys_read32(CMU_MEMCLKSRC1);
  1143. while(1) {
  1144. /* RAM4 shareRAM select RC4MHZ in s2) */
  1145. sys_write32((bk_clksrc0 & ~(7 << 25)) | (0 << 25), CMU_MEMCLKSRC0); /*shareRAM select RC4M */
  1146. pwrgat_gpio_nor_backup_registers();
  1147. pwrgat_set_unused_uart_highz();
  1148. sleep_stage(1);
  1149. __cpu_enter_sleep();
  1150. sleep_stage(2);
  1151. sys_write32((bk_clksrc0 & ~(7 << 25)) | (4 << 25), CMU_MEMCLKSRC0); /* shareRAM select RC64M */
  1152. //sys_write32((bk_clksrc1 & (~0xe)) | (0x4 << 1), CMU_MEMCLKSRC1); /* RC64M and ANX ram */
  1153. sleep_stage(3);
  1154. pwrgat_gpio_nor_restore_registers();
  1155. icache_restore_registers();
  1156. sys_norflash_power_ctrl(0);
  1157. if (sleep_policy_is_pwrgat()) {
  1158. sys_write32(1, SPICACHE_INVALIDATE);
  1159. while((sys_read32(SPICACHE_INVALIDATE) & 0x1) == 1);
  1160. }
  1161. #if defined(CONFIG_BOARD_EMMCBOOT) || defined(CONFIG_BOARD_NANDBOOT)
  1162. sys_write32(sys_read32(CMU_DEVCLKEN0) | (1<<CLOCK_ID_SPI1CACHE) | (1<<CLOCK_ID_SPI1), CMU_DEVCLKEN0);
  1163. pwrgat_gpio_psram_restore_registers();
  1164. dcache_restore_registers();
  1165. sys_set_bit(CMU_S1CLKCTL,2); // enable rc64m for psram
  1166. if(!soc_boot_is_mini())
  1167. sys_write32((ddr_mode & ~(0xf << 16)) | (4<<16), SPI1_DDR_MODE_CTL); // spi1 clk 64MHZ,must adjust RLC to 4 ( default is 5)
  1168. sys_write32(0x300, CMU_SPI1CLK); // psram now use rc64m
  1169. psram_power_control(false); // psram resume for sensor run
  1170. #endif
  1171. arm_floating_point_init();
  1172. /* Resume vector offset before sensor algo for algo fault debug.
  1173. * Avoid all sections of MTB brush off if running into BROM fault.
  1174. */
  1175. nvic_restore_registers();
  1176. #ifdef CONFIG_SLEEP_PRINTK_DEBUG
  1177. pwrgat_gpio_uart_restore_registers();
  1178. uart_resume();
  1179. #endif
  1180. if (WK_CB_RUN_SYSTEM == check_wk_run_sram_nor(irq_en_bit, cb, &num_cb)) {
  1181. sleep_stage(4);
  1182. break;
  1183. }
  1184. else {
  1185. sys_restore_wksrc();
  1186. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  1187. }
  1188. #if defined(CONFIG_BOARD_EMMCBOOT) || defined(CONFIG_BOARD_NANDBOOT)
  1189. psram_power_control(true); // now psram suspend
  1190. /* make psram cs pin in fixed state when cpu enters deepsleep */
  1191. pwrgat_gpio_psram_backup_registers();
  1192. #endif
  1193. }
  1194. sleep_stage(5);
  1195. /* restore memory clock sources */
  1196. sys_write32(bk_clksrc0, CMU_MEMCLKSRC0);
  1197. sys_write32(bk_clksrc1, CMU_MEMCLKSRC1);
  1198. /* hosc+rc4m+64M */
  1199. sys_write32(0x7, CMU_S1CLKCTL);
  1200. /* cmu/memclk/ram_ds go to normal */
  1201. sys_write32(devclk[0], CMU_DEVCLKEN0);
  1202. sys_write32(devclk[1], CMU_DEVCLKEN1);
  1203. sys_write32(memclk[0], CMU_MEMCLKEN0);
  1204. sys_write32(memclk[1], CMU_MEMCLKEN1);
  1205. pwrgat_gpio_psram_restore_registers();
  1206. pwrgat_gpio_uart_restore_registers();
  1207. dcache_restore_registers();
  1208. uart_resume();
  1209. dma_restore_registers();
  1210. sdma_restore_registers();
  1211. __DSB();
  1212. __ISB();
  1213. #ifndef CONFIG_SOC_NO_PSRAM
  1214. /* enable spi1 cache */
  1215. sys_set_bit(SPI1_CACHE_CTL, 0);
  1216. #endif
  1217. #ifdef CONFIG_SPI0_NOR_DTR_MODE
  1218. /* restore S1 delaychain */
  1219. sys_write32(bk_spi0_delaychain, SPI0_DELAYCHAIN);
  1220. #endif
  1221. /* restore VDD */
  1222. sys_write32(bk_vout_s1, VOUT_CTL1_S1M);
  1223. soc_udelay(30);/*delay for vdd*/
  1224. /*enable avdd, for pll*/
  1225. sys_write32(((sys_read32(AVDDLDO_CTL)
  1226. & (~(1<<AVDDLDO_CTL_AVDD_PD_EN)))
  1227. | (1<<AVDDLDO_CTL_AVDD_EN)), AVDDLDO_CTL);
  1228. soc_udelay(1);
  1229. /* resume psram */
  1230. psram_power_control(false);
  1231. for(i = 0; i < 300; i++){
  1232. if(sys_read32(HOSC_CTL) & HOSC_CTL_READY_MASK)
  1233. break;
  1234. soc_udelay(5);
  1235. }
  1236. if (!soc_dvfs_opt())
  1237. soc_udelay(200);
  1238. sys_write32(corepll_backup, COREPLL_CTL);
  1239. /*spi0 clk switch to hosc*/
  1240. sys_write32(sys_read32(CMU_SPI0CLK) & ~(0x3 << 8) & ~(0xf << 0), CMU_SPI0CLK);
  1241. for(i = 0; i < 300; i++){
  1242. if(sys_read32(COREPLL_CTL) & (1<<8))
  1243. break;
  1244. soc_udelay(5);
  1245. }
  1246. check_cnt = i;
  1247. /*first switch clk ahb div*/
  1248. sys_write32((sys_read32(CMU_SYSCLK)&0x7) | (sysclk_bak & (~0x7)) , CMU_SYSCLK);
  1249. soc_udelay(1);
  1250. sys_write32(sysclk_bak, CMU_SYSCLK);
  1251. psram_self_refresh_control(false);
  1252. #if defined(CONFIG_BOARD_EMMCBOOT) || defined(CONFIG_BOARD_NANDBOOT)
  1253. sys_write32(spi1_clk, CMU_SPI1CLK); // restore spi1 clk for spsram
  1254. sys_write32(ddr_mode, SPI1_DDR_MODE_CTL);
  1255. #endif
  1256. #ifndef CONFIG_SOC_NO_PSRAM
  1257. /*spi1 rx fifo reset*/
  1258. sys_clear_bit(SPI1_CTL, 4);
  1259. sys_clear_bit(SPI1_CTL, 5);
  1260. soc_udelay(1);
  1261. sys_set_bit(SPI1_CTL, 4);
  1262. sys_set_bit(SPI1_CTL, 5);
  1263. /* invalid spi1 cache */
  1264. spi1_cache_ops(SPI_CACHE_INVALID_ALL, (void *)SPI1_BASE_ADDR, 0x1000);
  1265. #ifdef CONFIG_SLEEP_MEMORY_CHECK_INTEGRITY
  1266. resume_check();
  1267. #endif
  1268. #endif
  1269. sys_pwrctrl_assigned_sram(false);
  1270. current_sleep->g_num_cb = num_cb;
  1271. memcpy(current_sleep->g_syste_cb, cb,
  1272. num_cb * sizeof(struct sleep_wk_cb));
  1273. }
  1274. #ifdef CONFIG_SLEEP_PRINTK_DEBUG
  1275. #define soc_enter_sleep_switch_uart(x) NULL
  1276. #else
  1277. #ifdef CONFIG_SLEEP_UART_SILENT
  1278. #define soc_enter_sleep_switch_uart(x) NULL
  1279. #else
  1280. /* bt pwrgat, tx isolated to 0, should not set PULLUP to save pwr */
  1281. static const struct acts_pin_config bt_pin_cfg_uart[] = {
  1282. {28, 23 | GPIO_CTL_PADDRV_LEVEL(1)},
  1283. {29, 23 | GPIO_CTL_PADDRV_LEVEL(1) | GPIO_CTL_PULLUP},
  1284. };
  1285. static const struct acts_pin_config leopard_pin_cfg_uart[] = {
  1286. {28, 5 | GPIO_CTL_PADDRV_LEVEL(1) | GPIO_CTL_PULLUP},
  1287. {29, 5 | GPIO_CTL_PADDRV_LEVEL(1) | GPIO_CTL_PULLUP},
  1288. };
  1289. static void soc_enter_sleep_switch_uart(bool bt_uart)
  1290. {
  1291. if(current_sleep->sleep_mode)
  1292. return;
  1293. if (bt_uart) {
  1294. acts_pinmux_setup_pins(bt_pin_cfg_uart, ARRAY_SIZE(bt_pin_cfg_uart));
  1295. } else {
  1296. acts_pinmux_setup_pins(leopard_pin_cfg_uart, ARRAY_SIZE(leopard_pin_cfg_uart));
  1297. }
  1298. }
  1299. #endif /* CONFIG_SLEEP_UART_SILENT */
  1300. #endif /* CONFIG_SLEEP_TIMER_DEBUG */
  1301. void dump_reg(const char *promt)
  1302. {
  1303. #ifdef CONFIG_SLEEP_DUMP_INFO
  1304. printk("%s LOSC_CTL=0x%x,sleep_mode=%d\n", promt, sys_read32(LOSC_CTL), current_sleep->sleep_mode);
  1305. int i;
  1306. for (i = 0; i < 77 ; i++)// nor pin
  1307. printk("gpio%d=0x%x\n", i, sys_read32(GPION_CTL(i)));
  1308. printk("RMU_MRCR0=0x%x\n", sys_read32(RMU_MRCR0));
  1309. printk("RMU_MRCR1=0x%x\n", sys_read32(RMU_MRCR1));
  1310. printk("CMU_DEVCLKEN0=0x%x\n", sys_read32(CMU_DEVCLKEN0));
  1311. printk("CMU_DEVCLKEN1=0x%x\n", sys_read32(CMU_DEVCLKEN1));
  1312. printk("PMU_DET=0x%x\n", sys_read32(PMU_DET));
  1313. printk("NVIC_ISPR0=0x%x\n", sys_read32(NVIC_ISPR0));
  1314. printk("NVIC_ISPR1=0x%x\n", sys_read32(NVIC_ISPR1));
  1315. printk("CMU_MEMCLKEN0=0x%x\n", sys_read32(CMU_MEMCLKEN0));
  1316. printk("CMU_MEMCLKEN1=0x%x\n", sys_read32(CMU_MEMCLKEN1));
  1317. printk("CMU_MEMCLKSRC0=0x%x\n", sys_read32(CMU_MEMCLKSRC0));
  1318. printk("CMU_MEMCLKSRC1=0x%x\n", sys_read32(CMU_MEMCLKSRC1));
  1319. printk("PWRGATE_DIG=0x%x\n", sys_read32(PWRGATE_DIG));
  1320. printk("VOUT_CTL1_S2=0x%x\n", sys_read32(VOUT_CTL1_S2));
  1321. printk("VOUT_CTL1_S3=0x%x\n", sys_read32(VOUT_CTL1_S3));
  1322. printk("PWRGATE_RAM=0x%x\n", sys_read32(PWRGATE_RAM));
  1323. printk("WIO0_CTL=0x%x\n", sys_read32(WIO0_CTL));
  1324. printk("WIO1_CTL=0x%x\n", sys_read32(WIO1_CTL));
  1325. printk("WIO2_CTL=0x%x\n", sys_read32(WIO2_CTL));
  1326. printk("WIO3_CTL=0x%x\n", sys_read32(WIO3_CTL));
  1327. #endif
  1328. }
  1329. unsigned int soc_sleep_cycle(void)
  1330. {
  1331. return current_sleep->g_sleep_cycle;
  1332. }
  1333. __sleepfunc int64_t soc_sys_uptime_get(void)
  1334. {
  1335. if(g_sleep_st){
  1336. unsigned int ms;
  1337. while(g_sleep_t2cnt == sys_read32(T2_CNT)); // wait for clock sync
  1338. ms = rc32k_cyc_to_ms(sys_read32(T2_CNT)-g_sleep_st);
  1339. return (g_sleep_update_time+g_sleep_ms) + ms;
  1340. }else{
  1341. return (k_uptime_get()+g_sleep_ms);
  1342. }
  1343. }
  1344. static void soc_timer_sleep_prepare(void)
  1345. {
  1346. g_sleep_update_time = k_uptime_get();
  1347. acts_clock_peripheral_disable(CLOCK_ID_TIMER2); // for switch clk source
  1348. sys_write32(0x4, CMU_TIMER2CLK); /* select rc32k before enter S3 */
  1349. g_sleep_st = sys_read32(T2_CNT);
  1350. acts_clock_peripheral_enable(CLOCK_ID_TIMER2);
  1351. }
  1352. static void soc_timer_exit_sleep(void)
  1353. {
  1354. unsigned int end;
  1355. acts_clock_peripheral_disable(CLOCK_ID_TIMER2); // for switch clk source
  1356. end = sys_read32(T2_CNT);
  1357. sys_write32(0x0, CMU_TIMER2CLK); /* select hosc for k_cycle_get */
  1358. acts_clock_peripheral_enable(CLOCK_ID_TIMER2);
  1359. dump_stage_timer(g_sleep_st, end);
  1360. end = end - g_sleep_st;
  1361. current_sleep->g_sleep_cycle += soc_rc32K_mutiple_hosc()*end;
  1362. g_sleep_ms += rc32k_cyc_to_ms(end);
  1363. g_sleep_st = 0;
  1364. }
  1365. static uint8_t g_lcd_aod_mode;
  1366. void soc_set_aod_mode(int is_aod)
  1367. {
  1368. g_lcd_aod_mode = is_aod ? 1 : 0;
  1369. if (is_aod) {
  1370. sys_s3_wksrc_set(SLEEP_WK_SRC_RTC);
  1371. } else {
  1372. sys_s3_wksrc_clr(SLEEP_WK_SRC_RTC);
  1373. }
  1374. }
  1375. int soc_get_aod_mode(void)
  1376. {
  1377. return g_lcd_aod_mode;
  1378. }
  1379. void soc_set_sleep_mode(uint8_t mode)
  1380. {
  1381. if(mode > SLEEP_MODE_LOWPOWER)
  1382. mode = SLEEP_MODE_LOWPOWER;
  1383. current_sleep->sleep_mode = mode;
  1384. printk("sleep mode=%d\n", current_sleep->sleep_mode);
  1385. }
  1386. int soc_in_sleep_mode(void)
  1387. {
  1388. return (g_sleep_st > 0);
  1389. }
  1390. /*AIRCR SCR CCR SHPR SHCSR*/
  1391. static unsigned int scb_aircr_bak, scb_scr_bak, scb_ccr_bak, scb_shcsr_bak;
  1392. static unsigned char scb_shpr_bak[12];
  1393. static void soc_arm_reg_backup(void)
  1394. {
  1395. memcpy(scb_shpr_bak, (void*)SCB->SHPR, 12);
  1396. scb_aircr_bak = SCB->AIRCR;
  1397. scb_scr_bak = SCB->SCR;
  1398. scb_ccr_bak = SCB->CCR;
  1399. scb_shcsr_bak = SCB->SHCSR;
  1400. }
  1401. static void soc_arm_reg_restore(void)
  1402. {
  1403. #ifdef CONFIG_ACTIONS_ARM_MPU
  1404. arm_mpu_protect_init();
  1405. #endif
  1406. memcpy((void*) SCB->SHPR, scb_shpr_bak, 12);
  1407. SCB->AIRCR = scb_aircr_bak;
  1408. SCB->SCR = scb_scr_bak;
  1409. SCB->CCR = scb_ccr_bak;
  1410. SCB->SHCSR = scb_shcsr_bak;
  1411. }
  1412. void soc_enter_deep_sleep(void)
  1413. {
  1414. dump_reg("before");
  1415. soc_pmu_check_onoff_reset_func();
  1416. #ifdef CONFIG_SLEEP_DISABLE_BT
  1417. soc_cmu_sleep_prepare(0);
  1418. #else
  1419. soc_cmu_sleep_prepare(!current_sleep->sleep_mode);
  1420. #endif
  1421. soc_timer_sleep_prepare();
  1422. if (sleep_policy_is_pwrgat()) {
  1423. soc_arm_reg_backup();
  1424. }
  1425. soc_enter_sleep_switch_uart(true);
  1426. cpu_enter_sleep();/* wfi,enter to sleep */
  1427. soc_enter_sleep_switch_uart(false);
  1428. if (sleep_policy_is_pwrgat()) {
  1429. soc_arm_reg_restore();
  1430. }
  1431. soc_timer_exit_sleep();
  1432. soc_cmu_sleep_exit();
  1433. dump_reg("BT after");
  1434. #ifdef CONFIG_SLEEP_STAGE_DEBUG
  1435. soc_pstore_set(SOC_PSTORE_TAG_SLEEP_DBG_STAGE, 0);
  1436. #endif
  1437. }
  1438. void soc_enter_light_sleep(void)
  1439. {
  1440. dump_reg("before");
  1441. soc_cmu_sleep_prepare(0);
  1442. cpu_enter_sleep();/* wfi,enter to sleep */
  1443. soc_cmu_sleep_exit();
  1444. dump_reg("after");
  1445. }
  1446. static int soc_sleep_init(const struct device *dev)
  1447. {
  1448. int i;
  1449. ARG_UNUSED(dev);
  1450. #ifdef CONFIG_SLEEP_STAGE_DEBUG
  1451. uint32_t val;
  1452. soc_pstore_get(SOC_PSTORE_TAG_SLEEP_DBG_STAGE, &val);
  1453. printk("SLEEP_DBG_STAGE=0x%x\n", val);
  1454. #endif
  1455. g_lcd_aod_mode = 0;
  1456. current_sleep->g_sleep_cycle = 0;
  1457. current_sleep->sleep_mode = 0;
  1458. g_sleep_ms = 0;
  1459. g_sleep_st = 0;
  1460. g_sleep_wksrc_en = 0;
  1461. g_cyc2ms_mul = (1000<<16) / soc_rc32K_freq();
  1462. for(i = 0; i < SLEEP_WKSRC_NUM; i++)
  1463. g_wk_fun[i] = NULL;
  1464. #ifdef CONFIG_JTAG_DEBUG
  1465. extern void jtag_set(void);
  1466. jtag_set();
  1467. #endif
  1468. #ifdef CONFIG_SLEEP_MEMORY_CHECK_INTEGRITY
  1469. current_sleep->check_start_addr = PSRAM_START_ADDR;
  1470. current_sleep->check_len = MEMORY_CHECK_INTEGRITY_SIZE;
  1471. #endif
  1472. return 0;
  1473. }
  1474. SYS_INIT(soc_sleep_init, PRE_KERNEL_1, 80);