boot_mode.c 8.4 KB

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