boot_mode.c 8.2 KB

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