boot_mode.c 8.1 KB

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