boot_mode.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 <string.h>
  29. #include <platform/mt_typedefs.h>
  30. #include <platform/boot_mode.h>
  31. #include <platform/mt_reg_base.h>
  32. #include <target/cust_key.h>
  33. #include <meta.h>
  34. #include <dev/mrdump.h>
  35. #include <platform/mt_rtc.h>
  36. #include <platform/mtk_key.h>
  37. #include <platform/mt_gpt.h>
  38. #ifdef CFG_MTK_WDT_COMMON
  39. #include <mtk_wdt.h>
  40. #else
  41. #include <platform/mtk_wdt.h>
  42. #endif
  43. #include <platform/mt_leds.h> // for mt65xx_backlight_on()
  44. #include <env.h>
  45. #include <boot_menu.h>
  46. #include <recovery.h>
  47. // global variable for specifying boot mode (default = NORMAL)
  48. BOOTMODE g_boot_mode = NORMAL_BOOT;
  49. #ifdef MTK_KERNEL_POWER_OFF_CHARGING
  50. extern BOOL kernel_power_off_charging_detection(void);
  51. #endif
  52. BOOL meta_mode_check(void)
  53. {
  54. if (g_boot_mode == META_BOOT || g_boot_mode == ADVMETA_BOOT || g_boot_mode ==ATE_FACTORY_BOOT || g_boot_mode == FACTORY_BOOT) {
  55. return TRUE;
  56. } else {
  57. return FALSE;
  58. }
  59. }
  60. extern BOOT_ARGUMENT *g_boot_arg;
  61. #define MODULE_NAME "[FACTORY]"
  62. // check the boot mode : (1) meta mode or (2) recovery mode ...
  63. static int mrdump_check(void)
  64. {
  65. if (mrdump_detection()) {
  66. mt65xx_backlight_on();
  67. if (g_boot_mode == FASTBOOT)
  68. return 1;
  69. mrdump_run2();
  70. return 1;
  71. } else {
  72. return 0;
  73. }
  74. }
  75. /* Add for reboot from normal mode to meta mode by dialer */
  76. #define REBOOT_META_FLAG "reboot_meta_flag"
  77. #define REBOOT_META_FLAG_VALUE "meta"
  78. void boot_mode_select(void)
  79. {
  80. char *meta_value = NULL;
  81. int factory_forbidden = 0;
  82. // int forbid_mode;
  83. /*We put conditions here to filer some cases that can not do key detection*/
  84. extern int kedump_mini(void) __attribute__((weak));
  85. if (kedump_mini) {
  86. if (kedump_mini()) {
  87. mrdump_check();
  88. if (g_boot_mode == FASTBOOT)
  89. return;
  90. #ifdef MTK_PMIC_FULL_RESET
  91. dprintf(CRITICAL, "kedump:full pmic reset!\n");
  92. mtk_arch_full_reset();
  93. #else
  94. dprintf(CRITICAL, "kedump:sw reset!\n");
  95. mtk_arch_reset(1);
  96. #endif
  97. return;
  98. }
  99. }
  100. if (meta_detection()) {
  101. return;
  102. }
  103. meta_value = get_env(REBOOT_META_FLAG);
  104. if (meta_value != NULL) {
  105. if (strncmp(meta_value, REBOOT_META_FLAG_VALUE, strlen(REBOOT_META_FLAG_VALUE)+1) == 0) {
  106. g_boot_mode = META_BOOT;
  107. if (set_env(REBOOT_META_FLAG, "0") != 0)
  108. dprintf(INFO, "REBOOT_META_FLAG set failed!\n");
  109. return;
  110. }
  111. }
  112. #if defined(HAVE_LK_TEXT_MENU) || defined(MTK_FASTBOOT_SUPPORT)
  113. /*Check RTC to know if system want to reboot to Fastboot*/
  114. if (Check_RTC_PDN1_bit13()) {
  115. g_boot_mode = FASTBOOT;
  116. Set_Clr_RTC_PDN1_bit13(false);
  117. }
  118. /*Check RGU to know if system want to reboot to Fastboot*/
  119. #ifdef MTK_ENTER_FASTBOOT_VIA_RGU
  120. if (mtk_wdt_fastboot_check()) {
  121. g_boot_mode = FASTBOOT;
  122. mtk_wdt_fastboot_set(0); // clear RGU fastboot mode bit
  123. }
  124. #endif
  125. if (g_boot_mode == FASTBOOT) {
  126. dprintf(CRITICAL, "[FASTBOOT] reboot to boot loader\n");
  127. return;
  128. }
  129. #endif
  130. #if defined (HAVE_LK_TEXT_MENU)
  131. /*If forbidden mode is factory, cacel the factory key detection*/
  132. if (g_boot_arg->sec_limit.magic_num == 0x4C4C4C4C) {
  133. if (g_boot_arg->sec_limit.forbid_mode == F_FACTORY_MODE) {
  134. //Forbid to enter factory mode
  135. dprintf(CRITICAL, "%s Forbidden\n",MODULE_NAME);
  136. factory_forbidden=1;
  137. }
  138. }
  139. // forbid_mode = g_boot_arg->boot_mode &= 0x000000FF;
  140. /*If boot reason is power key + volumn down, then
  141. disable factory mode dectection*/
  142. if (mtk_detect_pmic_just_rst()) {
  143. factory_forbidden=1;
  144. }
  145. /*Check RTC to know if system want to reboot to Recovery*/
  146. #ifndef CFG_MTK_WDT_COMMON
  147. if (Check_RTC_Recovery_Mode()) {
  148. #else
  149. if (Check_RTC_Recovery_Mode() ||
  150. (mtk_wdt_restart_reason() == MTK_WDT_NONRST2_BOOT_RECOVERY)) {
  151. #endif
  152. g_boot_mode = RECOVERY_BOOT;
  153. return;
  154. }
  155. /*If MISC Write has not completed in recovery mode
  156. before system reboot, go to recovery mode to
  157. finish remain tasks*/
  158. if (recovery_check_command_trigger()) {
  159. return;
  160. }
  161. ulong begin = get_timer(0);
  162. /*we put key dectection here to detect key which is pressed*/
  163. dprintf(INFO, "eng build\n");
  164. while (get_timer(begin)<50) {
  165. if (!factory_forbidden) {
  166. if (mtk_detect_key(MT65XX_FACTORY_KEY)) {
  167. dprintf(CRITICAL, "%s Detect key\n",MODULE_NAME);
  168. dprintf(CRITICAL, "%s Enable factory mode\n",MODULE_NAME);
  169. g_boot_mode = FACTORY_BOOT;
  170. //video_printf("%s : detect factory mode !\n",MODULE_NAME);
  171. return;
  172. }
  173. }
  174. if (mtk_detect_key(MT65XX_BOOT_MENU_KEY)) {
  175. dprintf(CRITICAL, "\n%s Check boot menu\n",MODULE_NAME);
  176. dprintf(CRITICAL, "%s Wait 50ms for special keys\n",MODULE_NAME);
  177. mtk_wdt_disable();
  178. /*************************/
  179. mt65xx_backlight_on();
  180. /*************************/
  181. boot_menu_select();
  182. mtk_wdt_init();
  183. return;
  184. }
  185. #ifdef MT65XX_RECOVERY_KEY
  186. if (mtk_detect_key(MT65XX_RECOVERY_KEY)) {
  187. dprintf(CRITICAL, "%s Detect cal key\n",MODULE_NAME);
  188. dprintf(CRITICAL, "%s Enable recovery mode\n",MODULE_NAME);
  189. g_boot_mode = RECOVERY_BOOT;
  190. //video_printf("%s : detect recovery mode !\n",MODULE_NAME);
  191. return;
  192. }
  193. #endif
  194. }
  195. #else
  196. /*We put conditions here to filer some cases that can not do key detection*/
  197. /*If forbidden mode is factory, cacel the factory key detection*/
  198. if (g_boot_arg->sec_limit.magic_num == 0x4C4C4C4C) {
  199. if (g_boot_arg->sec_limit.forbid_mode == F_FACTORY_MODE) {
  200. //Forbid to enter factory mode
  201. dprintf(INFO, "%s Forbidden\n",MODULE_NAME);
  202. factory_forbidden=1;
  203. }
  204. }
  205. // forbid_mode = g_boot_arg->boot_mode &= 0x000000FF;
  206. /*If boot reason is power key + volumn down, then
  207. disable factory mode dectection*/
  208. if (mtk_detect_pmic_just_rst()) {
  209. factory_forbidden=1;
  210. }
  211. /*Check RTC to know if system want to reboot to Recovery*/
  212. #ifndef CFG_MTK_WDT_COMMON
  213. if (Check_RTC_Recovery_Mode()) {
  214. #else
  215. if (Check_RTC_Recovery_Mode() ||
  216. (mtk_wdt_restart_reason() == MTK_WDT_NONRST2_BOOT_RECOVERY)) {
  217. #endif
  218. g_boot_mode = RECOVERY_BOOT;
  219. return TRUE;
  220. }
  221. /*If MISC Write has not completed in recovery mode
  222. and interrupted by system reboot, go to recovery mode to
  223. finish remain tasks*/
  224. if (recovery_check_command_trigger()) {
  225. return TRUE;
  226. }
  227. ulong begin = get_timer(0);
  228. /*we put key dectection here to detect key which is pressed*/
  229. while (get_timer(begin)<50) {
  230. #ifdef MTK_FASTBOOT_SUPPORT
  231. if (mtk_detect_key(MT_CAMERA_KEY)) {
  232. dprintf(INFO,"[FASTBOOT]Key Detect\n");
  233. g_boot_mode = FASTBOOT;
  234. return TRUE;
  235. }
  236. #endif
  237. if (!factory_forbidden) {
  238. if (mtk_detect_key(MT65XX_FACTORY_KEY)) {
  239. dprintf(INFO, "%s Detect key\n",MODULE_NAME);
  240. dprintf(INFO, "%s Enable factory mode\n",MODULE_NAME);
  241. g_boot_mode = FACTORY_BOOT;
  242. //video_printf("%s : detect factory mode !\n",MODULE_NAME);
  243. return TRUE;
  244. }
  245. }
  246. #ifdef MT65XX_RECOVERY_KEY
  247. if (mtk_detect_key(MT65XX_RECOVERY_KEY)) {
  248. dprintf(INFO, "%s Detect cal key\n",MODULE_NAME);
  249. dprintf(INFO, "%s Enable recovery mode\n",MODULE_NAME);
  250. g_boot_mode = RECOVERY_BOOT;
  251. //video_printf("%s : detect recovery mode !\n",MODULE_NAME);
  252. return TRUE;
  253. }
  254. #endif
  255. }
  256. #endif
  257. #ifdef MTK_KERNEL_POWER_OFF_CHARGING
  258. if (kernel_power_off_charging_detection()) {
  259. dprintf(CRITICAL, " < Kernel Power Off Charging Detection Ok> \n");
  260. return;
  261. } else {
  262. dprintf(CRITICAL, "< Kernel Enter Normal Boot > \n");
  263. }
  264. #endif
  265. }
  266. #if 1
  267. unsigned int get_chip_sw_ver_code(void)
  268. {
  269. return DRV_Reg32(APSW_VER);
  270. }
  271. unsigned int mt_get_chip_sw_ver(void)
  272. {
  273. return (unsigned int)get_chip_sw_ver_code();
  274. }
  275. #endif
  276. unsigned int mt_get_chip_hw_ver(void)
  277. {
  278. return DRV_Reg32(APHW_VER);
  279. }