boot_mode.c 7.9 KB

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