platform.c 28 KB

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