boot_mode.c 7.9 KB

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