boot_mode.c 8.8 KB

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