11bd4fe43Sopenharmony_ci/*
21bd4fe43Sopenharmony_ci * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
31bd4fe43Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
41bd4fe43Sopenharmony_ci * you may not use this file except in compliance with the License.
51bd4fe43Sopenharmony_ci * You may obtain a copy of the License at
61bd4fe43Sopenharmony_ci *
71bd4fe43Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
81bd4fe43Sopenharmony_ci *
91bd4fe43Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
101bd4fe43Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
111bd4fe43Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
121bd4fe43Sopenharmony_ci * See the License for the specific language governing permissions and
131bd4fe43Sopenharmony_ci * limitations under the License.
141bd4fe43Sopenharmony_ci */
151bd4fe43Sopenharmony_ci
161bd4fe43Sopenharmony_ci#include <hi_flashboot.h>
171bd4fe43Sopenharmony_ci#include <boot_start.h>
181bd4fe43Sopenharmony_ci#include <hi_boot_rom.h>
191bd4fe43Sopenharmony_ci#include "main.h"
201bd4fe43Sopenharmony_ci#ifdef CONFIG_FLASH_ENCRYPT_SUPPORT
211bd4fe43Sopenharmony_ci#include <crypto.h>
221bd4fe43Sopenharmony_ci#endif
231bd4fe43Sopenharmony_ci
241bd4fe43Sopenharmony_ci#define KERNEL_START_ADDR   0x40D3C0
251bd4fe43Sopenharmony_ci
261bd4fe43Sopenharmony_cihi_u32 g_uart_auth;
271bd4fe43Sopenharmony_ci#define FLASHBOOT_UART_DEFAULT_PARAM    {115200, 8, 1, 0, 0, 0, 2, 1, 4}
281bd4fe43Sopenharmony_ci
291bd4fe43Sopenharmony_cihi_void boot_kernel(uintptr_t kaddr)
301bd4fe43Sopenharmony_ci{
311bd4fe43Sopenharmony_ci    __asm__ __volatile__("ecall");  /* switch U-MODE -> M-MODE */
321bd4fe43Sopenharmony_ci    hi_void (*entry)(hi_void) = (hi_void*)(kaddr);
331bd4fe43Sopenharmony_ci    entry();
341bd4fe43Sopenharmony_ci}
351bd4fe43Sopenharmony_ci
361bd4fe43Sopenharmony_cihi_void boot_io_init(hi_void)
371bd4fe43Sopenharmony_ci{
381bd4fe43Sopenharmony_ci    hi_io_set_func(HI_IO_NAME_GPIO_3, HI_IO_FUNC_GPIO_3_UART0_TXD); /* uart0 tx */
391bd4fe43Sopenharmony_ci    hi_io_set_func(HI_IO_NAME_GPIO_4, HI_IO_FUNC_GPIO_4_UART0_RXD); /* uart0 rx */
401bd4fe43Sopenharmony_ci}
411bd4fe43Sopenharmony_ci
421bd4fe43Sopenharmony_cihi_void boot_flash_init(hi_void)
431bd4fe43Sopenharmony_ci{
441bd4fe43Sopenharmony_ci    hi_flash_cmd_func flash_funcs = {0};
451bd4fe43Sopenharmony_ci    flash_funcs.init = hi_flash_init;
461bd4fe43Sopenharmony_ci    flash_funcs.read = hi_flash_read;
471bd4fe43Sopenharmony_ci    flash_funcs.write = hi_flash_write;
481bd4fe43Sopenharmony_ci    flash_funcs.erase = hi_flash_erase;
491bd4fe43Sopenharmony_ci    hi_cmd_regist_flash_cmd(&flash_funcs);
501bd4fe43Sopenharmony_ci    (hi_void) hi_flash_init();
511bd4fe43Sopenharmony_ci}
521bd4fe43Sopenharmony_ci#define XTAL_DS     0x7
531bd4fe43Sopenharmony_ci#define OSC_DRV_CTL 0x2
541bd4fe43Sopenharmony_ci
551bd4fe43Sopenharmony_cihi_void boot_extern_32k(hi_void)
561bd4fe43Sopenharmony_ci{
571bd4fe43Sopenharmony_ci    hi_u16 chip_id, chip_id_bk;
581bd4fe43Sopenharmony_ci    hi_u32 ret;
591bd4fe43Sopenharmony_ci    ret = hi_efuse_read(HI_EFUSE_CHIP_RW_ID, (hi_u8 *)&chip_id, (hi_u8)sizeof(hi_u8));
601bd4fe43Sopenharmony_ci    if (ret != HI_ERR_SUCCESS) {
611bd4fe43Sopenharmony_ci        return;
621bd4fe43Sopenharmony_ci    }
631bd4fe43Sopenharmony_ci    ret = hi_efuse_read(HI_EFUSE_CHIP_BK_RW_ID, (hi_u8 *)&chip_id_bk, (hi_u8)sizeof(hi_u8));
641bd4fe43Sopenharmony_ci    if (ret != HI_ERR_SUCCESS) {
651bd4fe43Sopenharmony_ci        return;
661bd4fe43Sopenharmony_ci    }
671bd4fe43Sopenharmony_ci    hi_u8 chip_ver = (chip_id >> OFFSET_4_B) & MSK_3_B; /* chip_id bit[4:7] is chip_ver. */
681bd4fe43Sopenharmony_ci    hi_u8 chip_ver_bk = (chip_id_bk >> OFFSET_4_B) & MSK_3_B; /* chip_id bit[4:7] is chip_ver. */
691bd4fe43Sopenharmony_ci    if (chip_ver != HI_CHIP_VER_HI3861L) {
701bd4fe43Sopenharmony_ci        if (chip_ver_bk != HI_CHIP_VER_HI3861L) {
711bd4fe43Sopenharmony_ci            return;
721bd4fe43Sopenharmony_ci        }
731bd4fe43Sopenharmony_ci    }
741bd4fe43Sopenharmony_ci    hi_u32 reg_val;
751bd4fe43Sopenharmony_ci    hi_reg_read(HI_IOCFG_REG_BASE + IO_CTRL_REG_BASE_ADDR, reg_val);
761bd4fe43Sopenharmony_ci    reg_val &= ~(MSK_2_B << OFFSET_4_B); /* Maximum drive capability */
771bd4fe43Sopenharmony_ci    reg_val |= (MSK_2_B << OFFSET_22_B); /* external xtal, osc enable */
781bd4fe43Sopenharmony_ci    reg_val &= ~(MSK_3_B << OFFSET_25_B);
791bd4fe43Sopenharmony_ci    reg_val |= XTAL_DS << OFFSET_25_B;   /* 1.6ua */
801bd4fe43Sopenharmony_ci    reg_val &= ~(MSK_2_B << OFFSET_28_B);
811bd4fe43Sopenharmony_ci    reg_val |= OSC_DRV_CTL << OFFSET_28_B;   /* 4Mohm */
821bd4fe43Sopenharmony_ci    hi_reg_write(HI_IOCFG_REG_BASE + IO_CTRL_REG_BASE_ADDR, reg_val);
831bd4fe43Sopenharmony_ci}
841bd4fe43Sopenharmony_ci
851bd4fe43Sopenharmony_ci/* the entry of C. */
861bd4fe43Sopenharmony_cihi_void start_fastboot(hi_void)
871bd4fe43Sopenharmony_ci{
881bd4fe43Sopenharmony_ci#ifndef CONFIG_QUICK_SEND_MODE
891bd4fe43Sopenharmony_ci    hi_u32 ret;
901bd4fe43Sopenharmony_ci    hi_malloc_func malloc_funcs = {0, };
911bd4fe43Sopenharmony_ci    uart_param_stru default_uart_param = FLASHBOOT_UART_DEFAULT_PARAM;
921bd4fe43Sopenharmony_ci    hi_watchdog_disable();
931bd4fe43Sopenharmony_ci    hi_watchdog_enable(WDG_TIME_US);
941bd4fe43Sopenharmony_ci
951bd4fe43Sopenharmony_ci    /* io config */
961bd4fe43Sopenharmony_ci    boot_io_init();
971bd4fe43Sopenharmony_ci
981bd4fe43Sopenharmony_ci    /* Registering and Initializing the Heap Area */
991bd4fe43Sopenharmony_ci    malloc_funcs.init = rom_boot_malloc_init;
1001bd4fe43Sopenharmony_ci    malloc_funcs.boot_malloc = rom_boot_malloc;
1011bd4fe43Sopenharmony_ci    malloc_funcs.boot_free = rom_boot_free;
1021bd4fe43Sopenharmony_ci
1031bd4fe43Sopenharmony_ci    hi_register_malloc((uintptr_t)&__heap_begin__, &malloc_funcs);
1041bd4fe43Sopenharmony_ci    hi_u32 check_sum = ((uintptr_t)&__heap_begin__) ^ ((uintptr_t)&__heap_end__);
1051bd4fe43Sopenharmony_ci    boot_malloc_init((uintptr_t)&__heap_begin__, (uintptr_t)&__heap_end__, check_sum);
1061bd4fe43Sopenharmony_ci
1071bd4fe43Sopenharmony_ci    /* Initializing the Debugging Serial Port */
1081bd4fe43Sopenharmony_ci    ret = serial_init(UART0, default_uart_param);
1091bd4fe43Sopenharmony_ci    if (ret != HI_ERR_SUCCESS) {
1101bd4fe43Sopenharmony_ci        boot_msg0("uart err"); /* Use the serial port of the romboot to configure the printing. */
1111bd4fe43Sopenharmony_ci    }
1121bd4fe43Sopenharmony_ci
1131bd4fe43Sopenharmony_ci    boot_extern_32k();
1141bd4fe43Sopenharmony_ci    /* Initializing the Flash Driver */
1151bd4fe43Sopenharmony_ci    boot_flash_init();
1161bd4fe43Sopenharmony_ci    /* NV initialization */
1171bd4fe43Sopenharmony_ci    ret = hi_factory_nv_init(HI_FNV_DEFAULT_ADDR, HI_NV_DEFAULT_TOTAL_SIZE, HI_NV_DEFAULT_BLOCK_SIZE);
1181bd4fe43Sopenharmony_ci    if (ret != HI_ERR_SUCCESS) {
1191bd4fe43Sopenharmony_ci        boot_msg0("fnv err");
1201bd4fe43Sopenharmony_ci    }
1211bd4fe43Sopenharmony_ci
1221bd4fe43Sopenharmony_ci    ret = hi_flash_partition_init();
1231bd4fe43Sopenharmony_ci    if (ret != HI_ERR_SUCCESS) {
1241bd4fe43Sopenharmony_ci        boot_msg0("parti err");
1251bd4fe43Sopenharmony_ci    }
1261bd4fe43Sopenharmony_ci
1271bd4fe43Sopenharmony_ci    execute_upg_boot();
1281bd4fe43Sopenharmony_ci#else
1291bd4fe43Sopenharmony_ci
1301bd4fe43Sopenharmony_ci#ifdef CHIP_HI3861L
1311bd4fe43Sopenharmony_ci    boot_extern_32k();
1321bd4fe43Sopenharmony_ci#endif
1331bd4fe43Sopenharmony_ci    hi_flash_init();
1341bd4fe43Sopenharmony_ci    boot_kernel(KERNEL_START_ADDR);
1351bd4fe43Sopenharmony_ci#endif
1361bd4fe43Sopenharmony_ci    mdelay(RESET_DELAY_MS);
1371bd4fe43Sopenharmony_ci    global_reset();
1381bd4fe43Sopenharmony_ci}
139