mtk_wdt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. //#include <common.h>
  2. //#include <asm/io.h>
  3. #include <platform/mt_reg_base.h>
  4. #include <platform/mt_typedefs.h>
  5. #include <platform/mtk_wdt.h>
  6. #include <platform/boot_mode.h>
  7. #include <platform/mt_gpt.h>
  8. #include <debug.h>
  9. #define WDTTAG "[WDT] "
  10. #define WDTLOG(fmt, arg...) dprintf(INFO,WDTTAG fmt, ##arg)
  11. #define WDTMSG(fmt, arg...) dprintf(INFO,fmt, ##arg)
  12. #define WDTERR(fmt, arg...) dprintf(INFO,WDTTAG "%5d: "fmt, __LINE__, ##arg)
  13. //CRITICAL
  14. #if ENABLE_WDT_MODULE
  15. static unsigned int timeout;
  16. static unsigned int is_rgu_trigger_rst = 0;
  17. void mtk_wdt_disable(void)
  18. {
  19. u32 tmp;
  20. tmp = DRV_Reg32(MTK_WDT_MODE);
  21. tmp &= ~MTK_WDT_MODE_ENABLE; /* disable watchdog */
  22. tmp |= (MTK_WDT_MODE_KEY); /* need key then write is allowed */
  23. DRV_WriteReg32(MTK_WDT_MODE,tmp);
  24. }
  25. static void mtk_wdt_reset(char mode)
  26. {
  27. /* Watchdog Rest */
  28. unsigned int wdt_mode_val;
  29. DRV_WriteReg32(MTK_WDT_RESTART, MTK_WDT_RESTART_KEY);
  30. wdt_mode_val = DRV_Reg32(MTK_WDT_MODE);
  31. /* clear autorestart bit: autoretart: 1, bypass power key, 0: not bypass power key */
  32. wdt_mode_val &=(~MTK_WDT_MODE_AUTO_RESTART);
  33. /* make sure WDT mode is hw reboot mode, can not config isr mode */
  34. wdt_mode_val &=(~(MTK_WDT_MODE_IRQ|MTK_WDT_MODE_ENABLE | MTK_WDT_MODE_DUAL_MODE));
  35. if(mode){ /* mode != 0 means by pass power key reboot, We using auto_restart bit as by pass power key flag */
  36. wdt_mode_val = wdt_mode_val | (MTK_WDT_MODE_KEY|MTK_WDT_MODE_EXTEN|MTK_WDT_MODE_AUTO_RESTART);
  37. DRV_WriteReg32(MTK_WDT_MODE, wdt_mode_val);
  38. }else{
  39. wdt_mode_val = wdt_mode_val | (MTK_WDT_MODE_KEY|MTK_WDT_MODE_EXTEN);
  40. DRV_WriteReg32(MTK_WDT_MODE,wdt_mode_val);
  41. }
  42. gpt_busy_wait_us(100);
  43. DRV_WriteReg32(MTK_WDT_SWRST, MTK_WDT_SWRST_KEY);
  44. }
  45. unsigned int mtk_wdt_check_status(void)
  46. {
  47. unsigned int status;
  48. status = DRV_Reg32(MTK_WDT_STATUS);
  49. return status;
  50. }
  51. static void mtk_wdt_mode_config( BOOL dual_mode_en,
  52. BOOL irq,
  53. BOOL ext_en,
  54. BOOL ext_pol,
  55. BOOL wdt_en )
  56. {
  57. unsigned int tmp;
  58. WDTLOG(" mtk_wdt_mode LK config mode value=%x\n",DRV_Reg32(MTK_WDT_MODE));
  59. tmp = DRV_Reg32(MTK_WDT_MODE);
  60. tmp |= MTK_WDT_MODE_KEY;
  61. // Bit 0 : Whether enable watchdog or not
  62. if(wdt_en == TRUE)
  63. tmp |= MTK_WDT_MODE_ENABLE;
  64. else
  65. tmp &= ~MTK_WDT_MODE_ENABLE;
  66. // Bit 1 : Configure extern reset signal polarity.
  67. if(ext_pol == TRUE)
  68. tmp |= MTK_WDT_MODE_EXT_POL;
  69. else
  70. tmp &= ~MTK_WDT_MODE_EXT_POL;
  71. // Bit 2 : Whether enable external reset signal
  72. if(ext_en == TRUE)
  73. tmp |= MTK_WDT_MODE_EXTEN;
  74. else
  75. tmp &= ~MTK_WDT_MODE_EXTEN;
  76. // Bit 3 : Whether generating interrupt instead of reset signal
  77. if(irq == TRUE)
  78. tmp |= MTK_WDT_MODE_IRQ;
  79. else
  80. tmp &= ~MTK_WDT_MODE_IRQ;
  81. // Bit 6 : Whether enable debug module reset
  82. if(dual_mode_en == TRUE)
  83. tmp |= MTK_WDT_MODE_DUAL_MODE;
  84. else
  85. tmp &= ~MTK_WDT_MODE_DUAL_MODE;
  86. // Bit 4: WDT_Auto_restart, this is a reserved bit, we use it as bypass powerkey flag.
  87. // Because HW reboot always need reboot to kernel, we set it always.
  88. tmp |= MTK_WDT_MODE_AUTO_RESTART;
  89. DRV_WriteReg32(MTK_WDT_MODE,tmp);
  90. //dual_mode(1); //always dual mode
  91. //mdelay(100);
  92. WDTLOG(" mtk_wdt_mode_config LK mode value=%x, tmp:%x\n",DRV_Reg32(MTK_WDT_MODE), tmp);
  93. }
  94. #if 0
  95. static void mtk_wdt_mode_config(BOOL debug_en,
  96. BOOL irq,
  97. BOOL ext_en,
  98. BOOL ext_pol,
  99. BOOL wdt_en )
  100. {
  101. unsigned short tmp;
  102. tmp = DRV_Reg32(MTK_WDT_MODE);
  103. tmp |= MTK_WDT_MODE_KEY;
  104. // Bit 0 : Whether enable watchdog or not
  105. if(wdt_en == TRUE)
  106. tmp |= MTK_WDT_MODE_ENABLE;
  107. else
  108. tmp &= ~MTK_WDT_MODE_ENABLE;
  109. // Bit 1 : Configure extern reset signal polarity.
  110. if(ext_pol == TRUE)
  111. tmp |= MTK_WDT_MODE_EXT_POL;
  112. else
  113. tmp &= ~MTK_WDT_MODE_EXT_POL;
  114. // Bit 2 : Whether enable external reset signal
  115. if(ext_en == TRUE)
  116. tmp |= MTK_WDT_MODE_EXTEN;
  117. else
  118. tmp &= ~MTK_WDT_MODE_EXTEN;
  119. // Bit 3 : Whether generating interrupt instead of reset signal
  120. if(irq == TRUE)
  121. tmp |= MTK_WDT_MODE_IRQ;
  122. else
  123. tmp &= ~MTK_WDT_MODE_IRQ;
  124. // Bit 6 : Whether enable debug module reset
  125. if(debug_en == TRUE)
  126. tmp |= MTK_WDT_MODE_DEBUG_EN;
  127. else
  128. tmp &= ~MTK_WDT_MODE_DEBUG_EN;
  129. DRV_WriteReg32(MTK_WDT_MODE,tmp);
  130. }
  131. #endif
  132. void mtk_wdt_set_time_out_value(UINT32 value)
  133. {
  134. /*
  135. * TimeOut = BitField 15:5
  136. * Key = BitField 4:0 = 0x08
  137. */
  138. // sec * 32768 / 512 = sec * 64 = sec * 1 << 6
  139. timeout = (unsigned int)(value * ( 1 << 6) );
  140. timeout = timeout << 5;
  141. DRV_WriteReg32(MTK_WDT_LENGTH, (timeout | MTK_WDT_LENGTH_KEY) );
  142. }
  143. void mtk_wdt_restart(void)
  144. {
  145. // Reset WatchDogTimer's counting value to time out value
  146. // ie., keepalive()
  147. DRV_WriteReg32(MTK_WDT_RESTART, MTK_WDT_RESTART_KEY);
  148. }
  149. static void mtk_wdt_sw_reset(void)
  150. {
  151. WDTLOG ("UB WDT SW RESET\n");
  152. //DRV_WriteReg32 (0x70025000, 0x2201);
  153. //DRV_WriteReg32 (0x70025008, 0x1971);
  154. //DRV_WriteReg32 (0x7002501C, 0x1209);
  155. mtk_wdt_reset(1);/* NOTE here, this reset will cause by pass power key */
  156. // system will reset
  157. while (1)
  158. {
  159. WDTLOG ("UB SW reset fail ... \n");
  160. };
  161. }
  162. static void mtk_wdt_hw_reset(void)
  163. {
  164. WDTLOG("UB WDT_HW_Reset\n");
  165. // 1. set WDT timeout 1 secs, 1*64*512/32768 = 1sec
  166. mtk_wdt_set_time_out_value(1);
  167. // 2. enable WDT debug reset enable, generating irq disable, ext reset disable
  168. // ext reset signal low, wdt enalbe
  169. mtk_wdt_mode_config(TRUE, FALSE, FALSE, FALSE, TRUE);
  170. // 3. reset the watch dog timer to the value set in WDT_LENGTH register
  171. mtk_wdt_restart();
  172. // 4. system will reset
  173. while(1);
  174. }
  175. /**
  176. * For Power off and power on reset, the INTERVAL default value is 0x7FF.
  177. * We set Interval[1:0] to different value to distinguish different stage.
  178. * Enter pre-loader, we will set it to 0x0
  179. * Enter u-boot, we will set it to 0x1
  180. * Enter kernel, we will set it to 0x2
  181. * And the default value is 0x3 which means reset from a power off and power on reset
  182. */
  183. #define POWER_OFF_ON_MAGIC (0x3)
  184. #define PRE_LOADER_MAGIC (0x0)
  185. #define U_BOOT_MAGIC (0x1)
  186. #define KERNEL_MAGIC (0x2)
  187. #define MAGIC_NUM_MASK (0x3)
  188. /**
  189. * If the reset is trigger by RGU(Time out or SW trigger), we hope the system can boot up directly;
  190. * we DO NOT hope we must press power key to reboot system after reset.
  191. * This message should tell pre-loader and u-boot, and we use Interval[2] to store this information.
  192. * And this information will be cleared when enter kernel.
  193. */
  194. #define IS_POWER_ON_RESET (0x1<<2)
  195. #define RGU_TRIGGER_RESET_MASK (0x1<<2)
  196. void mtk_wdt_init(void)
  197. {
  198. unsigned int nonrst;
  199. unsigned int interval_val = DRV_Reg32(MTK_WDT_INTERVAL);
  200. mtk_wdt_mode_config(FALSE, FALSE, FALSE, FALSE, FALSE);
  201. WDTLOG("UB wdt init\n");
  202. /* Update interval register value and check reboot flag */
  203. if( (interval_val & RGU_TRIGGER_RESET_MASK) == IS_POWER_ON_RESET )
  204. is_rgu_trigger_rst = 0; // Power off and power on reset
  205. else
  206. is_rgu_trigger_rst = 1; // RGU trigger reset
  207. interval_val &= ~(RGU_TRIGGER_RESET_MASK|MAGIC_NUM_MASK);
  208. interval_val |= (U_BOOT_MAGIC|IS_POWER_ON_RESET);
  209. /* Write back INTERVAL REG */
  210. DRV_WriteReg32(MTK_WDT_INTERVAL, interval_val);
  211. /* Setting timeout 10s */
  212. mtk_wdt_set_time_out_value(10);
  213. #if (!LK_WDT_DISABLE)
  214. mtk_wdt_mode_config(TRUE, TRUE, TRUE, FALSE, TRUE);
  215. mtk_wdt_restart();
  216. #endif
  217. /*
  218. * E3 ECO
  219. * reset will delay 2ms after set SW_WDT in register
  220. */
  221. nonrst = DRV_Reg32(MTK_WDT_NONRST_REG);
  222. nonrst = (nonrst | (1<<29));
  223. DRV_WriteReg32(MTK_WDT_NONRST_REG, nonrst);
  224. dprintf(CRITICAL,"WDT NONRST=0x%x \n\r", DRV_Reg32(MTK_WDT_NONRST_REG));
  225. #if 0
  226. int i=0;
  227. //lk wdt test
  228. while(1)
  229. {
  230. i++;
  231. mdelay(1000);
  232. WDTLOG("UB wdt test %d\n" ,i);
  233. /* Dump RGU regisers */
  234. WDTLOG("==== fwq Dump RGU Reg ========\n");
  235. WDTLOG("RGU MODE: %x\n", DRV_Reg32(MTK_WDT_MODE));
  236. WDTLOG("RGU LENGTH: %x\n", DRV_Reg32(MTK_WDT_LENGTH));
  237. WDTLOG("RGU STA: %x\n", DRV_Reg32(MTK_WDT_STATUS));
  238. WDTLOG("RGU INTERVAL: %x\n", DRV_Reg32(MTK_WDT_INTERVAL));
  239. WDTLOG("RGU SWSYSRST: %x\n", DRV_Reg32(MTK_WDT_SWSYSRST));
  240. WDTLOG("==== Dump RGU Reg End ====\n");
  241. if(i<60)
  242. {
  243. WDTLOG("kick lk ext\n" );
  244. mtk_wdt_restart();
  245. }
  246. }
  247. #endif
  248. }
  249. BOOL mtk_is_rgu_trigger_reset(void)
  250. {
  251. if(is_rgu_trigger_rst)
  252. return TRUE;
  253. return FALSE;
  254. }
  255. extern BOOT_ARGUMENT *g_boot_arg;
  256. int mtk_wdt_boot_check(void)
  257. {
  258. int boot_reason;
  259. if (g_boot_arg->maggic_number == BOOT_ARGUMENT_MAGIC) {
  260. boot_reason = g_boot_arg->boot_reason;
  261. WDTLOG("WDT get boot reason is %d from pre-loader\n", boot_reason);
  262. if (boot_reason == BR_WDT) {
  263. return WDT_NORMAL_REBOOT;
  264. } else if (boot_reason == BR_WDT_BY_PASS_PWK || BR_KERNEL_PANIC == boot_reason || BR_WDT_SW ==boot_reason || BR_WDT_HW ==boot_reason) {
  265. return WDT_BY_PASS_PWK_REBOOT;
  266. } else {
  267. return WDT_NOT_WDT_REBOOT;
  268. }
  269. }
  270. return WDT_NOT_WDT_REBOOT;
  271. }
  272. void mtk_arch_reset(char mode)
  273. {
  274. WDTLOG("UB mtk_arch_reset\n");
  275. mtk_wdt_reset(mode);
  276. while (1);
  277. }
  278. void rgu_swsys_reset(WD_SYS_RST_TYPE reset_type)
  279. {
  280. if(WD_MD_RST == reset_type)
  281. {
  282. unsigned int wdt_dbg_ctrl;
  283. wdt_dbg_ctrl = DRV_Reg32(MTK_WDT_SWSYSRST);
  284. wdt_dbg_ctrl |= MTK_WDT_SWSYS_RST_KEY;
  285. wdt_dbg_ctrl |= 0x80;// 1<<7
  286. DRV_WriteReg32(MTK_WDT_SWSYSRST,wdt_dbg_ctrl);
  287. udelay(1000);
  288. wdt_dbg_ctrl = DRV_Reg32(MTK_WDT_SWSYSRST);
  289. wdt_dbg_ctrl |= MTK_WDT_SWSYS_RST_KEY;
  290. wdt_dbg_ctrl &= (~0x80);// ~(1<<7)
  291. DRV_WriteReg32(MTK_WDT_SWSYSRST,wdt_dbg_ctrl);
  292. printf("fwq rgu lk md reset\n");
  293. }
  294. }
  295. #else
  296. void mtk_wdt_init(void)
  297. {
  298. WDTLOG("UB WDT Dummy init called\n");
  299. }
  300. BOOL mtk_is_rgu_trigger_reset()
  301. {
  302. WDTLOG("UB Dummy mtk_is_rgu_trigger_reset called\n");
  303. return FALSE;
  304. }
  305. void mtk_arch_reset(char mode)
  306. {
  307. WDTLOG("UB WDT Dummy arch reset called\n");
  308. }
  309. int mtk_wdt_boot_check(void)
  310. {
  311. WDTLOG("UB WDT Dummy mtk_wdt_boot_check called\n");
  312. return WDT_NOT_WDT_REBOOT;
  313. }
  314. void mtk_wdt_disable(void)
  315. {
  316. WDTLOG("UB WDT Dummy mtk_wdt_disable called\n");
  317. }
  318. void mtk_wdt_restart(void)
  319. {
  320. WDTLOG("UB WDT Dummy mtk_wdt_restart called\n");
  321. }
  322. static void mtk_wdt_sw_reset(void)
  323. {
  324. WDTLOG("UB WDT Dummy mtk_wdt_sw_reset called\n");
  325. }
  326. static void mtk_wdt_hw_reset(void)
  327. {
  328. WDTLOG("UB WDT Dummy mtk_wdt_hw_reset called\n");
  329. }
  330. void rgu_swsys_reset(WD_SYS_RST_TYPE reset_type)
  331. {
  332. WDTLOG("UB WDT Dummy rgu_swsys_reset called\n");
  333. }
  334. #endif