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