platform.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2015. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. */
  31. #include <debug.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <video.h>
  35. #include <dev/uart.h>
  36. #include <arch/arm.h>
  37. #include <arch/arm/mmu.h>
  38. #include <arch/ops.h>
  39. #include <target/board.h>
  40. #include <platform/mt_reg_base.h>
  41. #include <platform/mt_disp_drv.h>
  42. #include <platform/disp_drv.h>
  43. #include <platform/boot_mode.h>
  44. #include <platform/mt_logo.h>
  45. #include <platform/partition.h>
  46. #include <env.h>
  47. #include <platform/mt_gpio.h>
  48. #include <platform/mt_pmic.h>
  49. #include <platform/mt_pmic_dlpt.h>
  50. #include <platform/mt_pmic_wrap_init.h>
  51. #include <platform/mt_i2c.h>
  52. #include <platform/mtk_key.h>
  53. #include <platform/mtk_smi.h>
  54. #include <platform/mt_rtc.h>
  55. #include <platform/mt_leds.h>
  56. #include <platform/upmu_common.h>
  57. #include <platform/mtk_wdt.h>
  58. #include <platform/disp_drv_platform.h>
  59. #include <platform/verified_boot.h>
  60. #include <platform/sec_devinfo.h>
  61. #include <plinfo.h> // for plinfo_get_brom_header_block_size()
  62. #include <libfdt.h>
  63. #include <mt_gic.h> // for platform_init_interrupts()
  64. #include <mt_boot.h> // for bldr_load_dtb()
  65. #include <mtk_dcm.h> // for mt_dcm_init()
  66. #include <fpga_boot_argument.h>
  67. #include <profiling.h>
  68. #include <assert.h>
  69. #include <platform/mt_gpt.h> // for get_timer()
  70. #include <sec_export.h>
  71. #include "memory_layout.h"
  72. #include "load_vfy_boot.h"
  73. #include <fdt_op.h>
  74. #include <verified_boot_common.h>
  75. #include <platform/mtk_charger_intf.h>
  76. #include <rsc.h>
  77. #ifdef MTK_SMC_ID_MGMT
  78. #include "mtk_secure_api.h"
  79. #endif
  80. #include "mtk_dcm.h" //for mt_dcm_init()
  81. #ifdef MTK_EMMC_SUPPORT
  82. #include "mmc_rpmb.h"
  83. #endif
  84. #define DEVAPC_TURN_ON 1
  85. #if defined(MTK_VPU_SUPPORT)
  86. #include <platform/mt_vpu.h>
  87. #endif
  88. #ifdef MTK_AB_OTA_UPDATER
  89. #include "bootctrl.h"
  90. #endif
  91. #if defined(MTK_SECURITY_SW_SUPPORT)
  92. #include "oemkey.h"
  93. #endif
  94. #ifdef MTK_UFS_SUPPORT
  95. #include "ufs_aio_interface.h"
  96. #endif
  97. #ifdef MTK_CHARGER_NEW_ARCH
  98. #include <mtk_charger.h>
  99. #include <mtk_battery.h>
  100. #endif
  101. #include "show_animation_common.h"
  102. #ifdef MTK_SECURITY_SW_SUPPORT
  103. extern u8 g_oemkey[OEM_PUBK_SZ];
  104. #endif
  105. #ifdef LK_DL_CHECK
  106. /*block if check dl fail*/
  107. #undef LK_DL_CHECK_BLOCK_LEVEL
  108. #endif
  109. extern void platform_early_init_timer();
  110. extern void jump_da(u32 addr, u32 arg1, u32 arg2);
  111. extern int i2c_hw_init(void);
  112. extern int mboot_common_load_logo(unsigned long logo_addr, char* filename);
  113. extern int sec_func_init(u64 pl_start_addr);
  114. extern int sec_usbdl_enabled (void);
  115. extern unsigned int crypto_hw_engine_disable(void);
  116. extern void mtk_wdt_disable(void);
  117. extern void platform_deinit_interrupts(void);
  118. extern int mmc_get_dl_info(void);
  119. extern int mmc_legacy_init(int);
  120. extern void platform_clear_all_on_mux(void);
  121. extern unsigned int crypto_hw_engine_disable(void);
  122. #ifdef MTK_AEE_PLATFORM_DEBUG_SUPPORT
  123. extern void latch_lastbus_init(void);
  124. #endif
  125. #ifdef MTK_BATLOWV_NO_PANEL_ON_EARLY
  126. extern kal_bool is_low_battery(kal_int32 val);
  127. extern int hw_charging_get_charger_type(void);
  128. #endif
  129. #ifdef MTK_LK_REGISTER_WDT
  130. extern void lk_register_wdt_callback(void);
  131. #endif
  132. void platform_uninit(void);
  133. void config_shared_SRAM_size(void);
  134. extern int dev_info_nr_cpu(void);
  135. extern void mt_pll_turn_off(void);
  136. struct mmu_initial_mapping mmu_initial_mappings[] = {
  137. {
  138. .phys = (uint64_t)0,
  139. .virt = (uint32_t)0,
  140. .size = 0x40000000,
  141. .flags = MMU_MEMORY_TYPE_STRONGLY_ORDERED | MMU_MEMORY_AP_P_RW_U_NA,
  142. .name = "mcusys"
  143. },
  144. {
  145. .phys = (uint64_t)MEMBASE,
  146. .virt = (uint32_t)MEMBASE,
  147. .size = ROUNDUP(MEMSIZE, SECTION_SIZE),
  148. .flags = MMU_MEMORY_TYPE_NORMAL_WRITE_BACK | MMU_MEMORY_AP_P_RW_U_NA,
  149. .name = "ram"
  150. },
  151. {
  152. .phys = (uint64_t)SCRATCH_ADDR,
  153. .virt = (uint32_t)SCRATCH_ADDR,
  154. .size = SCRATCH_SIZE,
  155. .flags = MMU_MEMORY_TYPE_NORMAL_WRITE_BACK | MMU_MEMORY_AP_P_RW_U_NA,
  156. .name = "download"
  157. },
  158. /* null entry to terminate the list */
  159. { 0 }
  160. };
  161. BOOT_ARGUMENT *g_boot_arg;
  162. BOOT_ARGUMENT boot_addr;
  163. int g_nr_bank;
  164. BI_DRAM bi_dram[MAX_NR_BANK];
  165. unsigned int g_fb_base;
  166. unsigned int g_fb_size;
  167. static int g_dram_init_ret;
  168. static unsigned int bootarg_addr;
  169. static unsigned int bootarg_size;
  170. extern unsigned int logo_lk_t;
  171. extern unsigned int boot_time;
  172. int dram_init(void)
  173. {
  174. unsigned int i;
  175. struct boot_tag *tags;
  176. /* Get parameters from pre-loader. Get as early as possible
  177. * The address of BOOT_ARGUMENT_LOCATION will be used by Linux later
  178. * So copy the parameters from BOOT_ARGUMENT_LOCATION to LK's memory region
  179. */
  180. g_boot_arg = &boot_addr;
  181. bootarg_addr = (unsigned int)((unsigned int *)BOOT_ARGUMENT_LOCATION);
  182. bootarg_size = 0;
  183. fpga_set_boot_argument((BOOT_ARGUMENT*)bootarg_addr);
  184. if (*(unsigned int *)BOOT_ARGUMENT_LOCATION == BOOT_ARGUMENT_MAGIC) {
  185. memcpy(g_boot_arg, (void*)BOOT_ARGUMENT_LOCATION, sizeof(BOOT_ARGUMENT));
  186. bootarg_size = sizeof(BOOT_ARGUMENT);
  187. } else {
  188. g_boot_arg->maggic_number = BOOT_ARGUMENT_MAGIC;
  189. for (tags = (void *)BOOT_ARGUMENT_LOCATION; tags->hdr.size; tags = boot_tag_next(tags)) {
  190. switch (tags->hdr.tag) {
  191. case BOOT_TAG_BOOT_REASON:
  192. g_boot_arg->boot_reason = tags->u.boot_reason.boot_reason;
  193. break;
  194. case BOOT_TAG_BOOT_MODE:
  195. g_boot_arg->boot_mode = tags->u.boot_mode.boot_mode;
  196. break;
  197. case BOOT_TAG_META_COM:
  198. g_boot_arg->meta_com_type = tags->u.meta_com.meta_com_type;
  199. g_boot_arg->meta_com_id = tags->u.meta_com.meta_com_id;
  200. g_boot_arg->meta_uart_port = tags->u.meta_com.meta_uart_port;
  201. g_boot_arg->meta_log_disable = tags->u.meta_com.meta_log_disable;
  202. g_boot_arg->fast_meta_gpio = tags->u.meta_com.fast_meta_gpio;
  203. break;
  204. case BOOT_TAG_LOG_COM:
  205. g_boot_arg->log_port = tags->u.log_com.log_port;
  206. g_boot_arg->log_baudrate = tags->u.log_com.log_baudrate;
  207. g_boot_arg->log_enable = tags->u.log_com.log_enable;
  208. g_boot_arg->log_dynamic_switch = tags->u.log_com.log_dynamic_switch;
  209. break;
  210. case BOOT_TAG_MEM:
  211. g_boot_arg->dram_rank_num = tags->u.mem.dram_rank_num;
  212. for (i = 0; i < tags->u.mem.dram_rank_num; i++) {
  213. g_boot_arg->dram_rank_size[i] = tags->u.mem.dram_rank_size[i];
  214. }
  215. g_boot_arg->mblock_info = tags->u.mem.mblock_info;
  216. g_boot_arg->orig_dram_info = tags->u.mem.orig_dram_info;
  217. g_boot_arg->lca_reserved_mem = tags->u.mem.lca_reserved_mem;
  218. g_boot_arg->tee_reserved_mem = tags->u.mem.tee_reserved_mem;
  219. break;
  220. case BOOT_TAG_MD_INFO:
  221. for (i = 0; i < 4; i++) {
  222. g_boot_arg->md_type[i] = tags->u.md_info.md_type[i];
  223. }
  224. break;
  225. case BOOT_TAG_BOOT_TIME:
  226. g_boot_arg->boot_time = tags->u.boot_time.boot_time;
  227. break;
  228. case BOOT_TAG_DA_INFO:
  229. memcpy(&g_boot_arg->da_info, &tags->u.da_info.da_info, sizeof(da_info_t));
  230. break;
  231. case BOOT_TAG_SEC_INFO:
  232. memcpy(&g_boot_arg->sec_limit, &tags->u.sec_info.sec_limit, sizeof(SEC_LIMIT));
  233. break;
  234. case BOOT_TAG_PART_NUM:
  235. g_boot_arg->part_num = tags->u.part_num.part_num;
  236. break;
  237. case BOOT_TAG_PART_INFO:
  238. g_boot_arg->part_info = tags->u.part_info.part_info; /* only copy the pointer but the contains*/
  239. break;
  240. case BOOT_TAG_EFLAG:
  241. g_boot_arg->e_flag = tags->u.eflag.e_flag;
  242. break;
  243. case BOOT_TAG_DDR_RESERVE:
  244. g_boot_arg->ddr_reserve_enable = tags->u.ddr_reserve.ddr_reserve_enable;
  245. g_boot_arg->ddr_reserve_success = tags->u.ddr_reserve.ddr_reserve_success;
  246. g_boot_arg->ddr_reserve_ready = tags->u.ddr_reserve.ddr_reserve_ready;
  247. break;
  248. case BOOT_TAG_DRAM_BUF:
  249. g_boot_arg->dram_buf_size = tags->u.dram_buf.dram_buf_size;
  250. break;
  251. case BOOT_TAG_SRAM_INFO:
  252. g_boot_arg->non_secure_sram_addr = tags->u.sram_info.non_secure_sram_addr;
  253. g_boot_arg->non_secure_sram_size = tags->u.sram_info.non_secure_sram_size;
  254. break;
  255. case BOOT_TAG_PLAT_DBG_INFO:
  256. g_boot_arg->plat_dbg_info_max = tags->u.plat_dbg_info.info_max;
  257. if (g_boot_arg->plat_dbg_info_max > INFO_TYPE_MAX)
  258. g_boot_arg->plat_dbg_info_max = INFO_TYPE_MAX;
  259. for (i = 0; i < g_boot_arg->plat_dbg_info_max; i++) {
  260. g_boot_arg->plat_dbg_info[i].key = tags->u.plat_dbg_info.info[i].key;
  261. g_boot_arg->plat_dbg_info[i].base = tags->u.plat_dbg_info.info[i].base;
  262. g_boot_arg->plat_dbg_info[i].size = tags->u.plat_dbg_info.info[i].size;
  263. }
  264. break;
  265. case BOOT_TAG_PTP:
  266. memcpy(&g_boot_arg->ptp_volt_info, &tags->u.ptp_volt.ptp_volt_info, sizeof(ptp_info_t));
  267. break;
  268. case BOOT_TAG_EMI_INFO:
  269. memcpy(&(g_boot_arg->emi_info), &(tags->u.emi_info), sizeof(emi_info_t));
  270. break;
  271. case BOOT_TAG_IMGVER_INFO:
  272. g_boot_arg->pl_imgver_status = tags->u.imgver_info.pl_imgver_status;
  273. break;
  274. case BOOT_TAG_MAX_CPUS:
  275. g_boot_arg->max_cpus = tags->u.max_cpus.max_cpus;
  276. break;
  277. case BOOT_TAG_BAT_INFO:
  278. g_boot_arg->boot_voltage = tags->u.bat_info.boot_voltage;
  279. g_boot_arg->shutdown_time= tags->u.bat_info.shutdown_time;
  280. break;
  281. case BOOT_TAG_RAM_CONSOLE_INFO:
  282. g_boot_arg->ram_console_sram_addr = tags->u.ram_console_info.sram_addr;
  283. g_boot_arg->ram_console_sram_size = tags->u.ram_console_info.sram_size;
  284. g_boot_arg->ram_console_def_type = tags->u.ram_console_info.def_type;
  285. g_boot_arg->ram_console_memory_info_offset = tags->u.ram_console_info.memory_info_offset;
  286. break;
  287. case BOOT_TAG_CHR_INFO:
  288. g_boot_arg->charger_type = tags->u.chr_info.charger_type;
  289. break;
  290. #if WITH_GZ_MD_SHAREMEM
  291. case BOOT_TAG_GZ_PARAM:
  292. g_boot_arg->gz_md_shm_pa = tags->u.gz_param.modemMteeShareMemPA;
  293. g_boot_arg->gz_md_shm_sz = tags->u.gz_param.modemMteeShareMemSize;
  294. break;
  295. #endif
  296. case BOOT_TAG_SOC_ID:
  297. memcpy(g_boot_arg->socid, tags->u.socid.id, SOC_ID_LEN);
  298. break;
  299. default:
  300. break;
  301. }
  302. bootarg_size += tags->hdr.size;
  303. }
  304. }
  305. fpga_copy_boot_argument(g_boot_arg);
  306. g_nr_bank = g_boot_arg->dram_rank_num;
  307. if (g_nr_bank == 0 || g_nr_bank > MAX_NR_BANK) {
  308. g_dram_init_ret = -1;
  309. //dprintf(CRITICAL, "[LK ERROR] DRAM bank number is not correct!!!");
  310. //while (1) ;
  311. return -1;
  312. }
  313. return 0;
  314. }
  315. unsigned int platform_get_bootarg_addr(void)
  316. {
  317. return bootarg_addr;
  318. }
  319. unsigned int platform_get_bootarg_size(void)
  320. {
  321. return bootarg_size;
  322. }
  323. /*******************************************************
  324. * Routine: memory_size
  325. * Description: return DRAM size to LCM driver
  326. ******************************************************/
  327. u32 ddr_enable_4gb(void)
  328. {
  329. u32 status;
  330. #define INFRA_MISC (INFRACFG_AO_BASE + 0x0F00)
  331. #define INFRA_4GB_EN (1 << 13)
  332. #define PERI_MISC (PERICFG_BASE + 0x0208)
  333. #define PERI_4GB_EN (1 << 15)
  334. status = ((DRV_Reg32(INFRA_MISC) & INFRA_4GB_EN) && (DRV_Reg32(PERI_MISC) & PERI_4GB_EN)) ? 1 : 0;
  335. dprintf(CRITICAL, "%s() status=%u\n", __func__, status);
  336. return status;
  337. }
  338. u64 physical_memory_size(void)
  339. {
  340. int i;
  341. unsigned long long size = 0;
  342. for (i = 0; i < (int)(g_boot_arg->orig_dram_info.rank_num); i++) {
  343. size += g_boot_arg->orig_dram_info.rank_info[i].size;
  344. }
  345. return size;
  346. }
  347. u32 memory_size(void)
  348. {
  349. unsigned long long size = physical_memory_size();
  350. while (((unsigned long long)DRAM_PHY_ADDR + size) > 0x100000000ULL) {
  351. size -= (unsigned long long)(1024*1024*1024);
  352. }
  353. return (unsigned int)size;
  354. }
  355. void sw_env()
  356. {
  357. #ifdef LK_DL_CHECK
  358. #if defined(MTK_EMMC_SUPPORT) || defined(MTK_UFS_SUPPORT)
  359. int dl_status = 0;
  360. dl_status = mmc_get_dl_info();
  361. dprintf(INFO, "mt65xx_sw_env--dl_status: %d\n", dl_status);
  362. if (dl_status != 0) {
  363. video_printf("=> TOOL DL image Fail!\n");
  364. dprintf(CRITICAL, "TOOL DL image Fail\n");
  365. #ifdef LK_DL_CHECK_BLOCK_LEVEL
  366. dprintf(CRITICAL, "uboot is blocking by dl info\n");
  367. while (1) ;
  368. #endif
  369. }
  370. #endif
  371. #endif
  372. #ifndef MACH_FPGA_NO_DISPLAY
  373. #ifndef USER_BUILD
  374. switch (g_boot_mode) {
  375. case META_BOOT:
  376. video_printf(" => META MODE\n");
  377. break;
  378. case FACTORY_BOOT:
  379. video_printf(" => FACTORY MODE\n");
  380. break;
  381. case RECOVERY_BOOT:
  382. video_printf(" => RECOVERY MODE\n");
  383. break;
  384. case SW_REBOOT:
  385. //video_printf(" => SW RESET\n");
  386. break;
  387. case NORMAL_BOOT:
  388. //if(g_boot_arg->boot_reason != BR_RTC && get_env("hibboot") != NULL && atoi(get_env("hibboot")) == 1)
  389. if (get_env("hibboot") != NULL && atoi(get_env("hibboot")) == 1)
  390. video_printf(" => HIBERNATION BOOT\n");
  391. else
  392. video_printf(" => NORMAL BOOT\n");
  393. break;
  394. case ADVMETA_BOOT:
  395. video_printf(" => ADVANCED META MODE\n");
  396. break;
  397. case ATE_FACTORY_BOOT:
  398. video_printf(" => ATE FACTORY MODE\n");
  399. break;
  400. #ifdef MTK_KERNEL_POWER_OFF_CHARGING
  401. case KERNEL_POWER_OFF_CHARGING_BOOT:
  402. video_printf(" => POWER OFF CHARGING MODE\n");
  403. break;
  404. case LOW_POWER_OFF_CHARGING_BOOT:
  405. video_printf(" => LOW POWER OFF CHARGING MODE\n");
  406. break;
  407. #endif
  408. case ALARM_BOOT:
  409. video_printf(" => ALARM BOOT\n");
  410. break;
  411. case FASTBOOT:
  412. video_printf(" => FASTBOOT mode...\n");
  413. break;
  414. default:
  415. video_printf(" => UNKNOWN BOOT\n");
  416. }
  417. return;
  418. #endif
  419. #ifdef USER_BUILD
  420. /* it's ok to display META MODE since it already verfied by preloader */
  421. if (g_boot_mode == META_BOOT)
  422. video_printf(" => META MODE\n");
  423. if (g_boot_mode == FASTBOOT)
  424. video_printf(" => FASTBOOT mode...\n");
  425. return;
  426. #endif
  427. #endif /* MACH_FPGA_NO_DISPLAY */
  428. }
  429. extern uint64_t ld_tt_l1[4];
  430. void platform_init_mmu(void)
  431. {
  432. /* configure available RAM banks */
  433. dram_init();
  434. /* Long-descriptor translation with lpae enable */
  435. arm_mmu_lpae_init();
  436. struct mmu_initial_mapping *m = mmu_initial_mappings;
  437. for (uint i = 0; i < countof(mmu_initial_mappings); i++, m++) {
  438. arch_mmu_map(m->phys, m->virt, m->flags, m->size);
  439. }
  440. arch_enable_mmu(); //enable mmu after setup page table to avoid cpu prefetch which may bring on emi violation
  441. }
  442. /******************************************************************************
  443. ******************************************************************************/
  444. void init_storage(void)
  445. {
  446. PROFILING_START("NAND/EMMC init");
  447. #if defined(MTK_EMMC_SUPPORT)
  448. mmc_legacy_init(1);
  449. #elif defined(MTK_UFS_SUPPORT)
  450. ufs_lk_init();
  451. #else
  452. #ifndef MACH_FPGA
  453. nand_init();
  454. nand_driver_test();
  455. #endif // MACH_FPGA
  456. #endif // MTK_EMMC_SUPPORT
  457. #if defined(MTK_EMMC_SUPPORT) || defined(MTK_UFS_SUPPORT)
  458. init_rpmb_sharemem();
  459. #endif
  460. PROFILING_END(); /* NAND/EMMC init */
  461. }
  462. /******************************************************************************
  463. ******************************************************************************/
  464. void platform_early_init(void)
  465. {
  466. PROFILING_START("platform_early_init");
  467. #ifdef MTK_LK_REGISTER_WDT
  468. lk_register_wdt_callback();
  469. #endif
  470. /* initialize the uart */
  471. uart_init_early();
  472. PROFILING_START("Devinfo Init");
  473. init_devinfo_data();
  474. PROFILING_END();
  475. platform_init_interrupts();
  476. platform_early_init_timer();
  477. #if !defined(USE_DTB_NO_DWS) && !defined(MACH_FPGA)
  478. mt_gpio_set_default();
  479. #endif // !defined(USE_DTB_NO_DWS) && !defined(MACH_FPGA)
  480. dprintf(SPEW, "bootarg_addr: 0x%x, bootarg_size: 0x%x\n", platform_get_bootarg_addr(), platform_get_bootarg_size());
  481. if (g_dram_init_ret < 0) {
  482. dprintf(CRITICAL, "[LK ERROR] DRAM bank number is not correct!!!\n");
  483. while (1) ;
  484. }
  485. //i2c_v1_init();
  486. PROFILING_START("WDT Init");
  487. mtk_wdt_init();
  488. PROFILING_END();
  489. //i2c init
  490. i2c_hw_init();
  491. #ifdef MACH_FPGA
  492. mtk_timer_init(); // GPT4 will be initialized at PL after
  493. mtk_wdt_disable(); // WDT will be triggered when uncompressing linux image on FPGA
  494. #endif
  495. pwrap_init_lk();
  496. #if !defined(MACH_FPGA) && !defined(NO_PMIC)
  497. PROFILING_START("pmic_init");
  498. pmic_init();
  499. PROFILING_END();
  500. #endif // !defined(MACH_FPGA) && !defined(NO_PMIC)
  501. PROFILING_END(); /* platform_early_init */
  502. }
  503. extern void mt65xx_bat_init(void);
  504. extern bool mtk_bat_allow_backlight_enable(void);
  505. #if defined (MTK_KERNEL_POWER_OFF_CHARGING)
  506. int kernel_charging_boot(void)
  507. {
  508. if ((g_boot_mode == KERNEL_POWER_OFF_CHARGING_BOOT || g_boot_mode == LOW_POWER_OFF_CHARGING_BOOT) && upmu_is_chr_det() == KAL_TRUE) {
  509. dprintf(INFO,"[%s] Kernel Power Off Charging with Charger/Usb \n", __func__);
  510. return 1;
  511. } else if ((g_boot_mode == KERNEL_POWER_OFF_CHARGING_BOOT || g_boot_mode == LOW_POWER_OFF_CHARGING_BOOT) && upmu_is_chr_det() == KAL_FALSE) {
  512. dprintf(INFO,"[%s] Kernel Power Off Charging without Charger/Usb \n", __func__);
  513. return -1;
  514. } else
  515. return 0;
  516. }
  517. #endif
  518. static void lk_vb_init(void)
  519. {
  520. #ifdef MTK_SECURITY_SW_SUPPORT
  521. u64 pl_start_addr = 0;
  522. #ifdef MTK_VER_SIMPLE_TEST
  523. u32 ret = 0;
  524. ret = sec_mtk_internal_ver_test(MTK_VER_SIMPLE_TEST);
  525. if (ret)
  526. assert(0);
  527. #endif
  528. #ifdef MTK_OTP_FRAMEWORK_V2
  529. enable_anti_rollback_v2_framework();
  530. #endif
  531. plinfo_get_brom_header_block_size(&pl_start_addr);
  532. PROFILING_START("Security init");
  533. /* initialize security library */
  534. sec_func_init(pl_start_addr);
  535. seclib_set_oemkey(g_oemkey, OEM_PUBK_SZ);
  536. PROFILING_END();
  537. #endif
  538. }
  539. static void lk_vb_vfy_logo(void)
  540. {
  541. #ifdef MTK_SECURITY_SW_SUPPORT
  542. /* bypass logo vfy in fast meta mode */
  543. if(g_boot_mode == META_BOOT)
  544. return;
  545. PROFILING_START("logo verify");
  546. /*Verify logo before use it*/
  547. if (0 != img_auth_stor("logo", "logo", 0x0))
  548. assert(0);
  549. PROFILING_END();
  550. #endif
  551. }
  552. void platform_init(void)
  553. {
  554. bool backlight_on = false;
  555. int logo_size;
  556. PROFILING_START("platform_init");
  557. dprintf(CRITICAL, "platform_init()\n");
  558. init_storage();
  559. lk_vb_init();
  560. extern void dummy_ap_entry(void)__attribute__((weak)); /* This is empty function for normal load */
  561. if (dummy_ap_entry)
  562. dummy_ap_entry();
  563. #ifdef MTK_AB_OTA_UPDATER
  564. /* get A/B system parameter before load dtb from boot image */
  565. get_AB_OTA_param();
  566. #endif
  567. /*
  568. * RSC initialize. It must be done before load device tree.
  569. * rsc_init will ectract DTBO index, which will be used in device
  570. * tree overlay, from PARA partition
  571. */
  572. rsc_init();
  573. /* The device tree should be loaded as early as possible. */
  574. load_device_tree();
  575. #if defined(USE_DTB_NO_DWS) && !defined(MACH_FPGA)
  576. mt_gpio_set_default();
  577. #endif // defined(USE_DTB_NO_DWS) && !defined(MACH_FPGA)
  578. #ifndef MACH_FPGA
  579. PROFILING_START("led init");
  580. leds_init();
  581. PROFILING_END();
  582. #endif // MACH_FPGA
  583. #ifdef MTK_KERNEL_POWER_OFF_CHARGING
  584. if ((g_boot_arg->boot_reason == BR_USB) && (upmu_is_chr_det() == KAL_FALSE)) {
  585. dprintf(INFO, "[%s] Unplugged Charger/Usb between Pre-loader and Uboot in Kernel Charging Mode, Power Off \n", __func__);
  586. mt_power_off();
  587. }
  588. #endif // MTK_KERNEL_POWER_OFF_CHARGING
  589. PROFILING_START("ENV init");
  590. env_init();
  591. print_env();
  592. PROFILING_END();
  593. /* initialize the frame buffet information */
  594. g_fb_size = mt_disp_get_vram_size();
  595. g_fb_base = mblock_reserve_ext(&g_boot_arg->mblock_info, g_fb_size, 0x10000, 0x80000000, 0, "framebuffer");
  596. if (!g_fb_base) {
  597. dprintf(CRITICAL, "reserve framebuffer failed\n");
  598. }
  599. dprintf(CRITICAL, "FB base = 0x%x, FB size = 0x%x (%d)\n", g_fb_base, g_fb_size, g_fb_size);
  600. #ifndef MACH_FPGA
  601. #ifdef MTK_SMI_SUPPORT
  602. /* write SMI non on-the-fly register before DISP init */
  603. smi_apply_register_setting();
  604. #endif
  605. #endif
  606. #ifndef MACH_FPGA_NO_DISPLAY
  607. PROFILING_START("disp init");
  608. mt_disp_init((void *)g_fb_base);
  609. PROFILING_END();
  610. PROFILING_START("vedio init");
  611. drv_video_init();
  612. PROFILING_END();
  613. #endif /* MACH_FPGA_NO_DISPLAY */
  614. /*for kpd pmic mode setting*/
  615. set_kpd_pmic_mode();
  616. #ifndef MACH_FPGA
  617. PROFILING_START("boot mode select");
  618. #if !defined(NO_BOOT_MODE_SEL)
  619. boot_mode_select();
  620. #endif
  621. #ifdef MTK_USB2JTAG_SUPPORT
  622. if (g_boot_mode != FASTBOOT) {
  623. extern void usb2jtag_init(void);
  624. usb2jtag_init();
  625. }
  626. #endif
  627. #ifdef MTK_AEE_PLATFORM_DEBUG_SUPPORT
  628. /* init lastbus: MUST call after kedump (in boot_mode_select) */
  629. latch_lastbus_init();
  630. #endif
  631. PROFILING_END(); /* boot mode select */
  632. #endif /*MACH_FPGA */
  633. lk_vb_vfy_logo();
  634. #ifndef MACH_FPGA_NO_DISPLAY
  635. /* fast meta mode */
  636. if(g_boot_mode != META_BOOT){
  637. PROFILING_START("load_logo");
  638. logo_size = mboot_common_load_logo((unsigned long)mt_get_logo_db_addr_pa(), "logo");
  639. assert(logo_size <= LK_LOGO_MAX_SIZE);
  640. PROFILING_END();
  641. } else
  642. /* Indicate logo lib that logo partition is not loaded */
  643. enable_logo(0);
  644. #endif // MACH_FPGA_NO_DISPLAY
  645. /*Show download logo & message on screen */
  646. if (g_boot_arg->boot_mode == DOWNLOAD_BOOT) {
  647. dprintf(CRITICAL, "[LK] boot mode is DOWNLOAD_BOOT\n");
  648. #ifndef MACH_FPGA_NO_DISPLAY
  649. PROFILING_START("show logo");
  650. mt_disp_show_boot_logo();
  651. PROFILING_END();
  652. #endif
  653. video_printf(" => Downloading...\n");
  654. dprintf(CRITICAL, "enable backlight after show bootlogo! \n");
  655. #ifndef MACH_FPGA
  656. PROFILING_START("backlight");
  657. mt65xx_backlight_on();
  658. PROFILING_END();
  659. #endif
  660. #ifndef MACH_FPGA_NO_DISPLAY
  661. /* pwm need display sof */
  662. PROFILING_START("disp update");
  663. mt_disp_update(0, 0, CFG_DISPLAY_WIDTH, CFG_DISPLAY_HEIGHT);
  664. PROFILING_END();
  665. #endif
  666. logo_lk_t = ((unsigned int)get_timer(boot_time));
  667. PROFILING_START("disable wdt, l2, cache, mmu");
  668. mtk_wdt_disable(); //Disable wdt before jump to DA
  669. platform_uninit();
  670. #ifdef HAVE_CACHE_PL310
  671. l2_disable();
  672. #endif
  673. arch_disable_cache(UCACHE);
  674. arch_disable_mmu();
  675. #ifdef ENABLE_L2_SHARING
  676. config_shared_SRAM_size();
  677. #endif
  678. PROFILING_END();
  679. jump_da(g_boot_arg->da_info.addr, g_boot_arg->da_info.arg1, g_boot_arg->da_info.arg2);
  680. }
  681. #ifdef MTK_KERNEL_POWER_OFF_CHARGING
  682. else if (g_boot_mode != META_BOOT &&
  683. g_boot_mode != ALARM_BOOT &&
  684. g_boot_mode != FASTBOOT &&
  685. g_boot_mode != KERNEL_POWER_OFF_CHARGING_BOOT &&
  686. g_boot_mode != LOW_POWER_OFF_CHARGING_BOOT &&
  687. mtk_bat_allow_backlight_enable()) {
  688. #ifndef MACH_FPGA_NO_DISPLAY
  689. PROFILING_START("show logo");
  690. mt_disp_show_boot_logo();
  691. PROFILING_END();
  692. #endif // MACH_FPGA_NO_DISPLAY
  693. #ifndef MACH_FPGA
  694. PROFILING_START("backlight");
  695. mt65xx_backlight_on();
  696. PROFILING_END(); /* backlight */
  697. #endif
  698. #ifndef MACH_FPGA_NO_DISPLAY
  699. /* pwm need display sof */
  700. PROFILING_START("disp update");
  701. mt_disp_update(0, 0, CFG_DISPLAY_WIDTH, CFG_DISPLAY_HEIGHT);
  702. PROFILING_END();
  703. #endif
  704. backlight_on = true;
  705. dprintf(INFO, "backlight enabled before battery init\n");
  706. logo_lk_t = ((unsigned int)get_timer(boot_time));
  707. }
  708. #endif // MTK_KERNEL_POWER_OFF_CHARGING
  709. PROFILING_START("battery init");
  710. #ifndef MACH_FPGA
  711. #if !defined(NO_PLL_TURN_OFF)
  712. mt_pll_turn_off();
  713. #endif // !defined(NO_PLL_TURN_OFF)
  714. #if !defined(NO_DCM)
  715. if (mt_dcm_init())
  716. dprintf(CRITICAL, "mt_dcm_init fail\n");
  717. #endif // !defined(NO_DCM)
  718. #if !defined(NO_BAT_INIT)
  719. #ifdef MTK_CHARGER_NEW_ARCH
  720. PROFILING_START("charger init");
  721. mtk_charger_init();
  722. PROFILING_END(); /* charger init */
  723. PROFILING_START("DLPT init");
  724. pmic_dlpt_init();
  725. PROFILING_END(); /* dlpt init */
  726. PROFILING_START("check sw_ocv");
  727. check_sw_ocv();
  728. PROFILING_END(); /* battery check sw_ocv */
  729. PROFILING_START("charger start");
  730. mtk_charger_start();
  731. PROFILING_END(); /* charger start */
  732. PROFILING_START(" DLPT get imix");
  733. get_dlpt_imix_r();
  734. PROFILING_END(); /* DLPT get imix */
  735. #else
  736. mt65xx_bat_init();
  737. #endif // MTK_CHARGER_NEW_ARCH
  738. #endif // !defined(NO_BAT_INIT)
  739. #endif // MACH_FPGA
  740. PROFILING_END(); /* battery init */
  741. #ifndef CFG_POWER_CHARGING
  742. PROFILING_START("RTC boot check Init");
  743. /* NOTE: if define CFG_POWER_CHARGING, will rtc_boot_check() in mt65xx_bat_init() */
  744. rtc_boot_check(false);
  745. PROFILING_END();
  746. #endif // CFG_POWER_CHARGING
  747. #ifdef MTK_KERNEL_POWER_OFF_CHARGING
  748. if (kernel_charging_boot() == 1) {
  749. PROFILING_START("show logo");
  750. #ifdef MTK_BATLOWV_NO_PANEL_ON_EARLY
  751. CHARGER_TYPE CHR_Type_num = CHARGER_UNKNOWN;
  752. CHR_Type_num = hw_charging_get_charger_type();
  753. if ((g_boot_mode != LOW_POWER_OFF_CHARGING_BOOT) ||
  754. ((CHR_Type_num != STANDARD_HOST) && (CHR_Type_num != NONSTANDARD_CHARGER))) {
  755. #endif // MTK_BATLOWV_NO_PANEL_ON_EARLY
  756. mt_disp_power(TRUE);
  757. #ifndef MACH_FPGA_NO_DISPLAY
  758. mt_disp_show_low_battery();
  759. #endif
  760. mt65xx_leds_brightness_set(6, 110);
  761. #ifndef MACH_FPGA_NO_DISPLAY
  762. /* pwm need display sof */
  763. PROFILING_START("display update");
  764. mt_disp_update(0, 0, CFG_DISPLAY_WIDTH, CFG_DISPLAY_HEIGHT);
  765. PROFILING_END();
  766. #endif
  767. backlight_on = true;
  768. #ifdef MTK_BATLOWV_NO_PANEL_ON_EARLY
  769. }
  770. #endif
  771. PROFILING_END();
  772. }
  773. #endif // MTK_KERNEL_POWER_OFF_CHARGING
  774. #ifdef MTK_BATLOWV_NO_PANEL_ON_EARLY
  775. if (!is_low_battery(0)) {
  776. #endif
  777. /*
  778. * Update LCM with framebuffer before
  779. * turning on backlight to avoid LCM noise if there is
  780. * no any logo showed on screen before.
  781. */
  782. if (!backlight_on) {
  783. logo_lk_t = ((unsigned int)get_timer(boot_time));
  784. #ifndef MACH_FPGA_NO_DISPLAY
  785. PROFILING_START("display update");
  786. mt_disp_update(0, 0, CFG_DISPLAY_WIDTH, CFG_DISPLAY_HEIGHT);
  787. PROFILING_END();
  788. #endif
  789. #ifndef MACH_FPGA
  790. PROFILING_START("backlight");
  791. mt65xx_backlight_on();
  792. PROFILING_END(); /* backlight */
  793. #endif
  794. }
  795. #ifdef MTK_BATLOWV_NO_PANEL_ON_EARLY
  796. }
  797. #endif
  798. #ifndef MACH_FPGA
  799. PROFILING_START("sw_env");
  800. sw_env();
  801. PROFILING_END();
  802. #endif
  803. PROFILING_END(); /* platform_init */
  804. }
  805. void platform_uninit(void)
  806. {
  807. #ifndef MACH_FPGA
  808. leds_deinit();
  809. platform_clear_all_on_mux();
  810. #endif
  811. platform_deinit_interrupts();
  812. return;
  813. }
  814. #ifdef ENABLE_L2_SHARING
  815. #define ADDR_CA7L_CACHE_CONFIG_MP(x) (CA7MCUCFG_BASE + 0x200 * x)
  816. #define L2C_SIZE_CFG_OFFSET 8
  817. #define L2C_SHARE_EN_OFFSET 12
  818. /* 4'b1111: 2048KB(not support)
  819. * 4'b0111: 1024KB(not support)
  820. * 4'b0011: 512KB
  821. * 4'b0001: 256KB
  822. * 4'b0000: 128KB (not support)
  823. */
  824. int is_l2_need_config(void)
  825. {
  826. volatile unsigned int cache_cfg, addr;
  827. addr = ADDR_CA7L_CACHE_CONFIG_MP(0);
  828. cache_cfg = DRV_Reg32(addr);
  829. cache_cfg = cache_cfg >> L2C_SIZE_CFG_OFFSET;
  830. /* only read 256KB need to be config.*/
  831. if ((cache_cfg &(0x7)) == 0x1) {
  832. return 1;
  833. }
  834. return 0;
  835. }
  836. void cluster_l2_share_enable(int cluster)
  837. {
  838. volatile unsigned int cache_cfg, addr;
  839. addr = ADDR_CA7L_CACHE_CONFIG_MP(cluster);
  840. /* set L2C size to 256KB */
  841. cache_cfg = DRV_Reg32(addr);
  842. cache_cfg &= (~0x7) << L2C_SIZE_CFG_OFFSET;
  843. cache_cfg |= 0x1 << L2C_SIZE_CFG_OFFSET;
  844. /* enable L2C_share_en. Sram only for other to use*/
  845. cache_cfg |= (0x1 << L2C_SHARE_EN_OFFSET);
  846. DRV_WriteReg32(addr, cache_cfg);
  847. }
  848. void cluster_l2_share_disable(int cluster)
  849. {
  850. volatile unsigned int cache_cfg, addr;
  851. addr = ADDR_CA7L_CACHE_CONFIG_MP(cluster);
  852. /* set L2C size to 512KB */
  853. cache_cfg = DRV_Reg32(addr);
  854. cache_cfg &= (~0x7) << L2C_SIZE_CFG_OFFSET;
  855. cache_cfg |= 0x3 << L2C_SIZE_CFG_OFFSET;
  856. DRV_WriteReg32(addr, cache_cfg);
  857. /* disable L2C_share_en. Sram only for cpu to use*/
  858. cache_cfg &= ~(0x1 << L2C_SHARE_EN_OFFSET);
  859. DRV_WriteReg32(addr, cache_cfg);
  860. }
  861. /* config L2 cache and sram to its size */
  862. void config_L2_size(void)
  863. {
  864. int cluster;
  865. if (is_l2_need_config()) {
  866. /*
  867. * Becuase mcu config is protected.
  868. * only can write in secutity mode
  869. */
  870. if (dev_info_nr_cpu() == 6) {
  871. cluster_l2_share_disable(0);
  872. cluster_l2_share_enable(1);
  873. }
  874. else {
  875. for (cluster = 0; cluster < 2; cluster++) {
  876. cluster_l2_share_disable(cluster);
  877. }
  878. }
  879. }
  880. }
  881. /* config SRAM back from L2 cache for DA relocation */
  882. void config_shared_SRAM_size(void)
  883. {
  884. int cluster;
  885. if (is_l2_need_config()) {
  886. /*
  887. * Becuase mcu config is protected.
  888. * only can write in secutity mode
  889. */
  890. for (cluster = 0; cluster < 2; cluster++) {
  891. cluster_l2_share_enable(cluster);
  892. }
  893. }
  894. }
  895. #endif
  896. void platform_sec_post_init(void)
  897. {
  898. unsigned int ret = 0;
  899. ret = crypto_hw_engine_disable();
  900. if (ret) {
  901. dprintf(CRITICAL, "[SEC] crypto engine HW disable fail\n");
  902. }
  903. /* Lock Device APC in LK */
  904. dprintf(CRITICAL, "[DEVAPC] sec_post_init\n");
  905. #if DEVAPC_TURN_ON
  906. dprintf(CRITICAL, "[DEVAPC] platform_sec_post_init - SMC call to ATF from LK\n");
  907. #ifdef MTK_SMC_ID_MGMT
  908. mt_secure_call(MTK_SIP_LK_DAPC_INIT, 0, 0, 0, 0);
  909. #else
  910. mt_secure_call(0x82000101, 0, 0, 0);
  911. #endif
  912. #endif
  913. }
  914. u32 get_devinfo_with_index(u32 index)
  915. {
  916. return internal_get_devinfo_with_index(index);
  917. }
  918. int platform_skip_hibernation(void)
  919. {
  920. switch (g_boot_arg->boot_reason) {
  921. #if 0 // let schedule power on to go hiberantion bootup process
  922. case BR_RTC:
  923. #endif
  924. case BR_WDT:
  925. case BR_WDT_BY_PASS_PWK:
  926. case BR_WDT_SW:
  927. case BR_WDT_HW:
  928. return 1;
  929. }
  930. return 0;
  931. }
  932. int is_meta_log_disable(void)
  933. {
  934. return g_boot_arg->meta_log_disable;
  935. }