1 /*
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <hi_boot_rom.h>
17 #include <hi_loaderboot_flash.h>
18 #include <cmd_loop.h>
19 #include "main.h"
20
boot_io_init(hi_void)21 hi_void boot_io_init(hi_void)
22 {
23 hi_io_set_func(HI_IO_NAME_GPIO_3, HI_IO_FUNC_GPIO_3_UART0_TXD); /* uart0 tx */
24 hi_io_set_func(HI_IO_NAME_GPIO_4, HI_IO_FUNC_GPIO_4_UART0_RXD); /* uart0 rx */
25 hi_io_set_func(HI_IO_NAME_GPIO_13, HI_IO_FUNC_GPIO_13_SSI_DATA);
26 hi_io_set_func(HI_IO_NAME_GPIO_14, HI_IO_FUNC_GPIO_14_SSI_CLK);
27 hi_io_set_func(HI_IO_NAME_SFC_CLK, HI_IO_FUNC_SFC_CLK_SFC_CLK);
28 hi_io_set_func(HI_IO_NAME_SFC_IO3, HI_IO_FUNC_SFC_IO_3_SFC_HOLDN);
29 }
30
boot_flash_init(hi_void)31 hi_void boot_flash_init(hi_void)
32 {
33 hi_flash_cmd_func flash_funcs = {0};
34 flash_funcs.init = hi_flash_init;
35 flash_funcs.read = hi_flash_read;
36 flash_funcs.write = hi_flash_write;
37 flash_funcs.erase = hi_flash_erase;
38 hi_cmd_regist_flash_cmd(&flash_funcs);
39 (hi_void) hi_flash_init();
40 }
41
42 /* the entry of C. */
start_loaderboot(hi_void)43 hi_void start_loaderboot(hi_void)
44 {
45 uart_ctx *cmd_ctx = HI_NULL;
46 hi_malloc_func malloc_funcs = {0, };
47
48 /* io config */
49 boot_io_init();
50
51 /* Heap registration and initialization */
52 malloc_funcs.init = rom_boot_malloc_init;
53 malloc_funcs.boot_malloc = rom_boot_malloc;
54 malloc_funcs.boot_free = rom_boot_free;
55
56 hi_register_malloc((uintptr_t)&__heap_begin__, &malloc_funcs);
57 hi_u32 check_sum = (uintptr_t)(&__heap_begin__) ^ (uintptr_t)(&__heap_end__);
58 boot_malloc_init((uintptr_t)&__heap_begin__, (uintptr_t)&__heap_end__, check_sum);
59
60 /* Flash initialization */
61 boot_flash_init();
62
63 cmd_ctx = cmd_loop_init();
64 if (cmd_ctx == HI_NULL) {
65 boot_msg0("cmd init fail");
66 reset();
67 while (1) {}
68 }
69
70 /* Enter the waiting command cycle and disable the watchdog. */
71 hi_watchdog_disable();
72 loader_ack(ACK_SUCCESS);
73 boot_msg0("Entry loader");
74
75 hi_u32 ret = flash_protect_set_protect(0, HI_FALSE);
76 if (ret != HI_ERR_SUCCESS) {
77 boot_msg0("Unlock Fail!");
78 }
79
80 boot_msg0("============================================\n");
81 cmd_loop(cmd_ctx);
82 reset();
83 while (1) {}
84 }
85