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_boot_rom.h>
171bd4fe43Sopenharmony_ci#include <hi_loaderboot_flash.h>
181bd4fe43Sopenharmony_ci#include <cmd_loop.h>
191bd4fe43Sopenharmony_ci#include "main.h"
201bd4fe43Sopenharmony_ci
211bd4fe43Sopenharmony_cihi_void boot_io_init(hi_void)
221bd4fe43Sopenharmony_ci{
231bd4fe43Sopenharmony_ci    hi_io_set_func(HI_IO_NAME_GPIO_3, HI_IO_FUNC_GPIO_3_UART0_TXD); /* uart0 tx */
241bd4fe43Sopenharmony_ci    hi_io_set_func(HI_IO_NAME_GPIO_4, HI_IO_FUNC_GPIO_4_UART0_RXD); /* uart0 rx */
251bd4fe43Sopenharmony_ci    hi_io_set_func(HI_IO_NAME_GPIO_13, HI_IO_FUNC_GPIO_13_SSI_DATA);
261bd4fe43Sopenharmony_ci    hi_io_set_func(HI_IO_NAME_GPIO_14, HI_IO_FUNC_GPIO_14_SSI_CLK);
271bd4fe43Sopenharmony_ci    hi_io_set_func(HI_IO_NAME_SFC_CLK, HI_IO_FUNC_SFC_CLK_SFC_CLK);
281bd4fe43Sopenharmony_ci    hi_io_set_func(HI_IO_NAME_SFC_IO3, HI_IO_FUNC_SFC_IO_3_SFC_HOLDN);
291bd4fe43Sopenharmony_ci}
301bd4fe43Sopenharmony_ci
311bd4fe43Sopenharmony_cihi_void boot_flash_init(hi_void)
321bd4fe43Sopenharmony_ci{
331bd4fe43Sopenharmony_ci    hi_flash_cmd_func flash_funcs = {0};
341bd4fe43Sopenharmony_ci    flash_funcs.init = hi_flash_init;
351bd4fe43Sopenharmony_ci    flash_funcs.read = hi_flash_read;
361bd4fe43Sopenharmony_ci    flash_funcs.write = hi_flash_write;
371bd4fe43Sopenharmony_ci    flash_funcs.erase = hi_flash_erase;
381bd4fe43Sopenharmony_ci    hi_cmd_regist_flash_cmd(&flash_funcs);
391bd4fe43Sopenharmony_ci    (hi_void) hi_flash_init();
401bd4fe43Sopenharmony_ci}
411bd4fe43Sopenharmony_ci
421bd4fe43Sopenharmony_ci/* the entry of C. */
431bd4fe43Sopenharmony_cihi_void start_loaderboot(hi_void)
441bd4fe43Sopenharmony_ci{
451bd4fe43Sopenharmony_ci    uart_ctx *cmd_ctx = HI_NULL;
461bd4fe43Sopenharmony_ci    hi_malloc_func malloc_funcs = {0, };
471bd4fe43Sopenharmony_ci
481bd4fe43Sopenharmony_ci    /* io config */
491bd4fe43Sopenharmony_ci    boot_io_init();
501bd4fe43Sopenharmony_ci
511bd4fe43Sopenharmony_ci    /* Heap registration and initialization */
521bd4fe43Sopenharmony_ci    malloc_funcs.init = rom_boot_malloc_init;
531bd4fe43Sopenharmony_ci    malloc_funcs.boot_malloc = rom_boot_malloc;
541bd4fe43Sopenharmony_ci    malloc_funcs.boot_free = rom_boot_free;
551bd4fe43Sopenharmony_ci
561bd4fe43Sopenharmony_ci    hi_register_malloc((uintptr_t)&__heap_begin__, &malloc_funcs);
571bd4fe43Sopenharmony_ci    hi_u32 check_sum = (uintptr_t)(&__heap_begin__) ^ (uintptr_t)(&__heap_end__);
581bd4fe43Sopenharmony_ci    boot_malloc_init((uintptr_t)&__heap_begin__, (uintptr_t)&__heap_end__, check_sum);
591bd4fe43Sopenharmony_ci
601bd4fe43Sopenharmony_ci    /* Flash initialization */
611bd4fe43Sopenharmony_ci    boot_flash_init();
621bd4fe43Sopenharmony_ci
631bd4fe43Sopenharmony_ci    cmd_ctx = cmd_loop_init();
641bd4fe43Sopenharmony_ci    if (cmd_ctx == HI_NULL) {
651bd4fe43Sopenharmony_ci        boot_msg0("cmd init fail");
661bd4fe43Sopenharmony_ci        reset();
671bd4fe43Sopenharmony_ci        while (1) {}
681bd4fe43Sopenharmony_ci    }
691bd4fe43Sopenharmony_ci
701bd4fe43Sopenharmony_ci    /* Enter the waiting command cycle and disable the watchdog. */
711bd4fe43Sopenharmony_ci    hi_watchdog_disable();
721bd4fe43Sopenharmony_ci    loader_ack(ACK_SUCCESS);
731bd4fe43Sopenharmony_ci    boot_msg0("Entry loader");
741bd4fe43Sopenharmony_ci
751bd4fe43Sopenharmony_ci    hi_u32 ret = flash_protect_set_protect(0, HI_FALSE);
761bd4fe43Sopenharmony_ci    if (ret != HI_ERR_SUCCESS) {
771bd4fe43Sopenharmony_ci        boot_msg0("Unlock Fail!");
781bd4fe43Sopenharmony_ci    }
791bd4fe43Sopenharmony_ci
801bd4fe43Sopenharmony_ci    boot_msg0("============================================\n");
811bd4fe43Sopenharmony_ci    cmd_loop(cmd_ctx);
821bd4fe43Sopenharmony_ci    reset();
831bd4fe43Sopenharmony_ci    while (1) {}
841bd4fe43Sopenharmony_ci}
85