boot_mode.c 7.7 KB

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