boot_mode.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright (c) 2008-2009 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. /*This file implements MTK boot mode.*/
  24. #include <sys/types.h>
  25. #include <debug.h>
  26. #include <err.h>
  27. #include <reg.h>
  28. #include <platform/mt_typedefs.h>
  29. #include <platform/boot_mode.h>
  30. #include <platform/mt_reg_base.h>
  31. #include <target/cust_key.h>
  32. #include <meta.h>
  33. #include <dev/mrdump.h>
  34. #include <platform/mt_rtc.h>
  35. #include <platform/mtk_key.h>
  36. #include <platform/mt_gpt.h>
  37. #ifdef CFG_MTK_WDT_COMMON
  38. #include <mtk_wdt.h>
  39. #else
  40. #include <platform/mtk_wdt.h>
  41. #endif
  42. #include <env.h>
  43. #include <boot_menu.h>
  44. #include <recovery.h>
  45. // global variable for specifying boot mode (default = NORMAL)
  46. extern void mtk_wdt_disable(void);
  47. BOOTMODE g_boot_mode = NORMAL_BOOT;
  48. #ifdef MTK_KERNEL_POWER_OFF_CHARGING
  49. extern BOOL kernel_power_off_charging_detection(void);
  50. #endif
  51. BOOL meta_mode_check(void)
  52. {
  53. if (g_boot_mode == META_BOOT || g_boot_mode == ADVMETA_BOOT || g_boot_mode ==ATE_FACTORY_BOOT || g_boot_mode == FACTORY_BOOT) {
  54. return TRUE;
  55. } else {
  56. return FALSE;
  57. }
  58. }
  59. extern BOOT_ARGUMENT *g_boot_arg;
  60. #define MODULE_NAME "[FACTORY]"
  61. // check the boot mode : (1) meta mode or (2) recovery mode ...
  62. static int mrdump_check(void)
  63. {
  64. if (mrdump_detection()) {
  65. mt65xx_backlight_on();
  66. if (g_boot_mode == FASTBOOT)
  67. return 1;
  68. mrdump_run2();
  69. return 1;
  70. } else {
  71. return 0;
  72. }
  73. }
  74. #ifdef NO_BOOT_MODE_SEL
  75. /******************************************************************************
  76. ******************************************************************************/
  77. void boot_mode_select(void)
  78. {
  79. dprintf(CRITICAL, "Warning: %s is turned off!\n", __func__);
  80. }
  81. #else
  82. /******************************************************************************
  83. ******************************************************************************/
  84. void boot_mode_select(void)
  85. {
  86. int factory_forbidden = 0;
  87. // int forbid_mode;
  88. /*We put conditions here to filer some cases that can not do key detection*/
  89. extern int kedump_mini(void) __attribute__((weak));
  90. if (kedump_mini) {
  91. if (kedump_mini()) {
  92. mrdump_check();
  93. if (g_boot_mode == FASTBOOT)
  94. return;
  95. #ifdef MTK_PMIC_FULL_RESET
  96. dprintf(CRITICAL, "kedump:full pmic reset!\n");
  97. mtk_arch_full_reset();
  98. #else
  99. dprintf(CRITICAL, "kedump:sw reset!\n");
  100. mtk_arch_reset(1);
  101. #endif
  102. return;
  103. }
  104. }
  105. if (meta_detection()) {
  106. return;
  107. }
  108. #if defined(HAVE_LK_TEXT_MENU) || defined(MTK_FASTBOOT_SUPPORT)
  109. /*Check RTC to know if system want to reboot to Fastboot*/
  110. if (Check_RTC_PDN1_bit13()) {
  111. g_boot_mode = FASTBOOT;
  112. Set_Clr_RTC_PDN1_bit13(false);
  113. }
  114. /*Check RGU to know if system want to reboot to Fastboot*/
  115. #ifdef MTK_ENTER_FASTBOOT_VIA_RGU
  116. if (mtk_wdt_fastboot_check()) {
  117. g_boot_mode = FASTBOOT;
  118. mtk_wdt_fastboot_set(0); // clear RGU fastboot mode bit
  119. }
  120. #endif
  121. if (g_boot_mode == FASTBOOT) {
  122. dprintf(CRITICAL, "[FASTBOOT] reboot to boot loader\n");
  123. return;
  124. }
  125. #endif
  126. #if defined (HAVE_LK_TEXT_MENU)
  127. /*If forbidden mode is factory, cacel the factory key detection*/
  128. if (g_boot_arg->sec_limit.magic_num == 0x4C4C4C4C) {
  129. if (g_boot_arg->sec_limit.forbid_mode == F_FACTORY_MODE) {
  130. //Forbid to enter factory mode
  131. dprintf(CRITICAL, "%s Forbidden\n",MODULE_NAME);
  132. factory_forbidden=1;
  133. }
  134. }
  135. // forbid_mode = g_boot_arg->boot_mode &= 0x000000FF;
  136. /*If boot reason is power key + volumn down, then
  137. disable factory mode dectection*/
  138. if (mtk_detect_pmic_just_rst()) {
  139. factory_forbidden=1;
  140. }
  141. /*Check RTC to know if system want to reboot to Recovery*/
  142. #ifndef CFG_MTK_WDT_COMMON
  143. if (Check_RTC_Recovery_Mode()) {
  144. #else
  145. if (Check_RTC_Recovery_Mode() ||
  146. (mtk_wdt_restart_reason() == MTK_WDT_NONRST2_BOOT_RECOVERY)) {
  147. #endif
  148. g_boot_mode = RECOVERY_BOOT;
  149. return;
  150. }
  151. /*If MISC Write has not completed in recovery mode
  152. before system reboot, go to recovery mode to
  153. finish remain tasks*/
  154. if (recovery_check_command_trigger()) {
  155. return;
  156. }
  157. ulong begin = get_timer(0);
  158. /*we put key dectection here to detect key which is pressed*/
  159. dprintf(INFO, "eng build\n");
  160. while (get_timer(begin)<50) {
  161. if (!factory_forbidden) {
  162. if (mtk_detect_key(MT65XX_FACTORY_KEY)) {
  163. dprintf(CRITICAL, "%s Detect key\n",MODULE_NAME);
  164. dprintf(CRITICAL, "%s Enable factory mode\n",MODULE_NAME);
  165. g_boot_mode = FACTORY_BOOT;
  166. //video_printf("%s : detect factory mode !\n",MODULE_NAME);
  167. return;
  168. }
  169. }
  170. if (mtk_detect_key(MT65XX_BOOT_MENU_KEY)) {
  171. dprintf(CRITICAL, "\n%s Check boot menu\n",MODULE_NAME);
  172. dprintf(CRITICAL, "%s Wait 50ms for special keys\n",MODULE_NAME);
  173. mtk_wdt_disable();
  174. /*************************/
  175. mt65xx_backlight_on();
  176. /*************************/
  177. boot_menu_select();
  178. mtk_wdt_init();
  179. return;
  180. }
  181. #ifdef MT65XX_RECOVERY_KEY
  182. if (mtk_detect_key(MT65XX_RECOVERY_KEY)) {
  183. dprintf(CRITICAL, "%s Detect cal key\n",MODULE_NAME);
  184. dprintf(CRITICAL, "%s Enable recovery mode\n",MODULE_NAME);
  185. g_boot_mode = RECOVERY_BOOT;
  186. //video_printf("%s : detect recovery mode !\n",MODULE_NAME);
  187. return;
  188. }
  189. #endif
  190. }
  191. #else
  192. /*We put conditions here to filer some cases that can not do key detection*/
  193. /*If forbidden mode is factory, cacel the factory key detection*/
  194. if (g_boot_arg->sec_limit.magic_num == 0x4C4C4C4C) {
  195. if (g_boot_arg->sec_limit.forbid_mode == F_FACTORY_MODE) {
  196. //Forbid to enter factory mode
  197. dprintf(INFO, "%s Forbidden\n",MODULE_NAME);
  198. factory_forbidden=1;
  199. }
  200. }
  201. // forbid_mode = g_boot_arg->boot_mode &= 0x000000FF;
  202. /*If boot reason is power key + volumn down, then
  203. disable factory mode dectection*/
  204. if (mtk_detect_pmic_just_rst()) {
  205. factory_forbidden=1;
  206. }
  207. /*Check RTC to know if system want to reboot to Recovery*/
  208. #ifndef CFG_MTK_WDT_COMMON
  209. if (Check_RTC_Recovery_Mode()) {
  210. #else
  211. if (Check_RTC_Recovery_Mode() ||
  212. (mtk_wdt_restart_reason() == MTK_WDT_NONRST2_BOOT_RECOVERY)) {
  213. #endif
  214. g_boot_mode = RECOVERY_BOOT;
  215. return TRUE;
  216. }
  217. /*If MISC Write has not completed in recovery mode
  218. and interrupted by system reboot, go to recovery mode to
  219. finish remain tasks*/
  220. if (recovery_check_command_trigger()) {
  221. return TRUE;
  222. }
  223. ulong begin = get_timer(0);
  224. /*we put key dectection here to detect key which is pressed*/
  225. while (get_timer(begin)<50) {
  226. #ifdef MTK_FASTBOOT_SUPPORT
  227. if (mtk_detect_key(MT_CAMERA_KEY)) {
  228. dprintf(INFO,"[FASTBOOT]Key Detect\n");
  229. g_boot_mode = FASTBOOT;
  230. return TRUE;
  231. }
  232. #endif
  233. if (!factory_forbidden) {
  234. if (mtk_detect_key(MT65XX_FACTORY_KEY)) {
  235. dprintf(INFO, "%s Detect key\n",MODULE_NAME);
  236. dprintf(INFO, "%s Enable factory mode\n",MODULE_NAME);
  237. g_boot_mode = FACTORY_BOOT;
  238. //video_printf("%s : detect factory mode !\n",MODULE_NAME);
  239. return TRUE;
  240. }
  241. }
  242. #ifdef MT65XX_RECOVERY_KEY
  243. if (mtk_detect_key(MT65XX_RECOVERY_KEY)) {
  244. dprintf(INFO, "%s Detect cal key\n",MODULE_NAME);
  245. dprintf(INFO, "%s Enable recovery mode\n",MODULE_NAME);
  246. g_boot_mode = RECOVERY_BOOT;
  247. //video_printf("%s : detect recovery mode !\n",MODULE_NAME);
  248. return TRUE;
  249. }
  250. #endif
  251. }
  252. #endif
  253. #ifdef MTK_KERNEL_POWER_OFF_CHARGING
  254. if (kernel_power_off_charging_detection()) {
  255. dprintf(CRITICAL, " < Kernel Power Off Charging Detection Ok> \n");
  256. return;
  257. } else {
  258. dprintf(CRITICAL, "< Kernel Enter Normal Boot > \n");
  259. }
  260. #endif
  261. }
  262. #endif // NO_BOOT_MODE_SEL
  263. #if 0
  264. unsigned int get_chip_sw_ver_code(void)
  265. {
  266. return DRV_Reg32(APSW_VER);
  267. }
  268. unsigned int mt_get_chip_sw_ver(void)
  269. {
  270. return (unsigned int)get_chip_sw_ver_code();
  271. }
  272. #endif
  273. unsigned int mt_get_chip_hw_ver(void)
  274. {
  275. return DRV_Reg32(APHW_VER);
  276. }