platform.c 28 KB

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