boot_mode.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 <platform/mt_leds.h> // for mt65xx_backlight_on()
  39. #include <env.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. * To avoid backlight enabled with non-zero framebuffer,
  157. * which causes noise screen.
  158. */
  159. video_clean_screen();
  160. /*************************/
  161. mt65xx_backlight_on();
  162. /*************************/
  163. boot_mode_menu_select();
  164. mtk_wdt_init();
  165. return;
  166. }
  167. #ifdef MT65XX_RECOVERY_KEY
  168. if (mtk_detect_key(MT65XX_RECOVERY_KEY)) {
  169. dprintf(CRITICAL, "%s Detect cal key\n",MODULE_NAME);
  170. dprintf(CRITICAL, "%s Enable recovery mode\n",MODULE_NAME);
  171. g_boot_mode = RECOVERY_BOOT;
  172. //video_printf("%s : detect recovery mode !\n",MODULE_NAME);
  173. return;
  174. }
  175. #endif
  176. }
  177. #else
  178. /*We put conditions here to filer some cases that can not do key detection*/
  179. /*Check RTC to know if system want to reboot to Fastboot*/
  180. #ifdef MTK_FASTBOOT_SUPPORT
  181. if (Check_RTC_PDN1_bit13()) {
  182. dprintf(INFO,"[FASTBOOT] reboot to boot loader\n");
  183. g_boot_mode = FASTBOOT;
  184. Set_Clr_RTC_PDN1_bit13(false);
  185. return TRUE;
  186. }
  187. #endif
  188. /*If forbidden mode is factory, cacel the factory key detection*/
  189. if (g_boot_arg->sec_limit.magic_num == 0x4C4C4C4C) {
  190. if (g_boot_arg->sec_limit.forbid_mode == F_FACTORY_MODE) {
  191. //Forbid to enter factory mode
  192. dprintf(INFO, "%s Forbidden\n",MODULE_NAME);
  193. factory_forbidden=1;
  194. }
  195. }
  196. // forbid_mode = g_boot_arg->boot_mode &= 0x000000FF;
  197. /*If boot reason is power key + volumn down, then
  198. disable factory mode dectection*/
  199. if (mtk_detect_pmic_just_rst()) {
  200. factory_forbidden=1;
  201. }
  202. /*Check RTC to know if system want to reboot to Recovery*/
  203. if (Check_RTC_Recovery_Mode()) {
  204. g_boot_mode = RECOVERY_BOOT;
  205. return TRUE;
  206. }
  207. /*If MISC Write has not completed in recovery mode
  208. and interrupted by system reboot, go to recovery mode to
  209. finish remain tasks*/
  210. if (unshield_recovery_detection()) {
  211. return TRUE;
  212. }
  213. ulong begin = get_timer(0);
  214. /*we put key dectection here to detect key which is pressed*/
  215. while (get_timer(begin)<50) {
  216. #ifdef MTK_FASTBOOT_SUPPORT
  217. if (mtk_detect_key(MT_CAMERA_KEY)) {
  218. dprintf(INFO,"[FASTBOOT]Key Detect\n");
  219. g_boot_mode = FASTBOOT;
  220. return TRUE;
  221. }
  222. #endif
  223. if (!factory_forbidden) {
  224. if (mtk_detect_key(MT65XX_FACTORY_KEY)) {
  225. dprintf(INFO, "%s Detect key\n",MODULE_NAME);
  226. dprintf(INFO, "%s Enable factory mode\n",MODULE_NAME);
  227. g_boot_mode = FACTORY_BOOT;
  228. //video_printf("%s : detect factory mode !\n",MODULE_NAME);
  229. return TRUE;
  230. }
  231. }
  232. #ifdef MT65XX_RECOVERY_KEY
  233. if (mtk_detect_key(MT65XX_RECOVERY_KEY)) {
  234. dprintf(INFO, "%s Detect cal key\n",MODULE_NAME);
  235. dprintf(INFO, "%s Enable recovery mode\n",MODULE_NAME);
  236. g_boot_mode = RECOVERY_BOOT;
  237. //video_printf("%s : detect recovery mode !\n",MODULE_NAME);
  238. return TRUE;
  239. }
  240. #endif
  241. }
  242. #endif
  243. #ifdef MTK_KERNEL_POWER_OFF_CHARGING
  244. if (kernel_power_off_charging_detection()) {
  245. dprintf(CRITICAL, " < Kernel Power Off Charging Detection Ok> \n");
  246. return;
  247. } else {
  248. dprintf(CRITICAL, "< Kernel Enter Normal Boot > \n");
  249. }
  250. #endif
  251. }
  252. #endif // NO_BOOT_MODE_SEL
  253. #if 0
  254. unsigned int get_chip_sw_ver_code(void)
  255. {
  256. return DRV_Reg32(APSW_VER);
  257. }
  258. CHIP_SW_VER mt_get_chip_sw_ver(void)
  259. {
  260. return (CHIP_SW_VER)get_chip_sw_ver_code();
  261. }
  262. #endif
  263. CHIP_HW_VER mt_get_chip_hw_ver(void)
  264. {
  265. return DRV_Reg32(APHW_VER);
  266. }