Debian Codegraph @

src/boot/bootmain.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /**
3 ******************************************************************************
4 * @file bootmain.c
5 * @author StarFive Technology
6 * @version V1.0
7 * @date 07/24/2020
8 * @brief
9 ******************************************************************************
10 * @copy
11 *
12 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
13 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
14 * TIME. AS A RESULT, STARFIVE SHALL NOT BE HELD LIABLE FOR ANY
15 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
16 * FROM THE CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
17 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
18 *
19 * COPYRIGHT 2020 Shanghai StarFive Technology Co., Ltd.
20 */
21 #include <sys.h>
22 #include <uart.h>
23 #include <spi.h>
24 #include <spi_flash.h>
25 #include <clkgen_ctrl_macro.h>
26 #include <syscon_sysmain_ctrl_macro.h>
27 #include <ezGPIO_fullMux_ctrl_macro.h>
28 #include <rstgen_ctrl_macro.h>
29 #include <syscon_iopad_ctrl_macro.h>
30
31 #define PRINT(fmt, ...) serial_printf(fmt, ##__VA_ARGS__)
32 #define ERROR(fmt, ...) serial_printf("ERROR %s() ln %d:" fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)
33
34 extern unsigned int receive_count;
35 int spi_flash_erases(struct spi_flash *spi_flash_local, u32 start_addr, int num_block)
36 {
37 int ret;
38 int l = 0;;
39 int k, j, page_par_block = 0;
40 u32 len = 0, offset_new = start_addr;
41 u32 i, block_size,page_size;
42
43 block_size = spi_flash_local->block_size;
44 page_size = spi_flash_local->page_size;
45 len = page_size;
46 page_par_block = block_size/page_size;
47 for(l = 0; l < num_block; l++)
48 {
49 ret = spi_flash_local->erase(spi_flash_local, offset_new, block_size, 64);
50 if(ret != 0)
51 {
52 return ret;
53 }
54 offset_new += block_size;
55
56 }
57 return ret;
58
59 }
60
61 int spi_flash_writes(struct spi_flash* spi_flash_local, u32 start_addr,u8 *data_buff, u32 page_num)
62 {
63 int k = 0,i = 0,j = 0,ret = 0;
64 u32 offset_new = start_addr;
65 unsigned char *data = (unsigned char *)data_buff;
66 int spi_write = 1; // or 4
67
68 int len = spi_flash_local->page_size;
69
70 for(k = 0; k < page_num; k++)
71 {
72 ret = spi_flash_local->write(spi_flash_local, offset_new, len, (void*)data, spi_write);
73 if(ret != 0)
74 {
75 return -1;
76 }
77 offset_new = offset_new + len;
78 data = data + len;
79
80 }
81 return ret;
82 }
83 int updata_flash(u32 flash_addr)
84 {
85 int ret = 0;
86 struct spi_flash* flash;
87 u32 len = 0;
88 int erase_block = 0;
89 unsigned int page_count;
90 unsigned int file_size = 0;
91 // receive_count = 0;
92 rlSendString("send a file by xmodem\r\n");
93 // replace xmodem_recv_file, which may have too many errors to receive the file
94 ret = xmodemReceive((unsigned char *)DEFAULT_BOOT_LOAD_ADDR, 192*1024);
95 if(ret <= 0)
96 return -1;
97 file_size = ret;
98
99 cadence_qspi_init(0, 1);
100 flash = spi_flash_probe(0, 0, 10000000, 0, (u32)SPI_DATAMODE_8);
101 if (!flash) {
102 rlSendString("spi_flash_probe fail\r\n");
103 return -1;
104 }
105 erase_block = (file_size + flash->block_size - 1) / flash->block_size;
106 page_count = (file_size + flash->page_size - 1) / flash->page_size;
107
108 ret = spi_flash_erases(flash, flash_addr, erase_block);
109 if(ret < 0)
110 {
111 rlSendString("spi_flash_erases fail\r\n");
112 return -1;
113 }
114
115 ret = spi_flash_writes(flash, flash_addr, (u8 *)DEFAULT_BOOT_LOAD_ADDR, page_count);
116 if(ret < 0)
117 {
118 rlSendString("spi_flash_writes fail\r\n");
119 return -1;
120 }
121 else
122 {
123 rlSendString("updata flash ok\r\n");
124 }
125 return 0;
126 }
127
128 static int updata_flash_code(unsigned int updata_num)
129 {
130 int ret = 0;
131 switch (updata_num){
132 case 0:
133 ret = updata_flash(FLASH_SECONDBOOT_START_ADDR);
134 break;
135 case 1:
136 ret = updata_flash(FLASH_DDRINIT_START_ADDR);
137 break;
138 default:
139 break;
140
141 }
142
143 return ret;
144
145 }
146 static void chip_clk_init()
147 {
148 _SWITCH_CLOCK_clk_cpundbus_root_SOURCE_clk_pll0_out_;
149 _SWITCH_CLOCK_clk_dla_root_SOURCE_clk_pll1_out_;
150 _SWITCH_CLOCK_clk_dsp_root_SOURCE_clk_pll2_out_;
151 _SWITCH_CLOCK_clk_perh0_root_SOURCE_clk_pll0_out_;
152 }
153 void BootMain(void)
154 {
155 int boot_mode = 0;
156 s32 usel;
157 int i;
158 char str[128];
159 int ret = 0;
160 int bootdelay = SECONDBOOTDELAY;
161 unsigned long ts;
162 int abort = 0;
163
164 chip_clk_init();
165
166 _SET_SYSCON_REG_register50_SCFG_funcshare_pad_ctrl_18(0x00c000c0);
167
168 _CLEAR_RESET_rstgen_rstn_usbnoc_axi_;
169 _CLEAR_RESET_rstgen_rstn_hifi4noc_axi_;
170
171 _ENABLE_CLOCK_clk_x2c_axi_;
172 _CLEAR_RESET_rstgen_rstn_x2c_axi_;
173
174 _CLEAR_RESET_rstgen_rstn_dspx2c_axi_;
175 _CLEAR_RESET_rstgen_rstn_dma1p_axi_;
176
177 _ENABLE_CLOCK_clk_msi_apb_;
178 _CLEAR_RESET_rstgen_rstn_msi_apb_;
179
180 _ASSERT_RESET_rstgen_rstn_x2c_axi_;
181 _CLEAR_RESET_rstgen_rstn_x2c_axi_;
182
183 uart_init(3);
184
185 PRINT("\r\nVIC second boot, version:%s %s\n", VERSION, CONFIGURATION);
186 while(1)
187 {
188 rlSendString("***************************************************\r\n");
189 rlSendString("***************JH7100 recovery boot ***************\r\n");
190 rlSendString("***************************************************\r\n");
191
192 rlSendString("0:updata bootloader\r\n");
193 rlSendString("1:updata ddr init\r\n");
194 again:
195 rlSendString("Select the function to test : ");
196 serial_gets(str);
197
198 if(str[0] == 0)
199 goto again;
200
201 usel = atoi(str);
202 if(usel > 2)
203 {
204 rlSendString("error select,try again\r\n");
205 goto again;
206 }
207 PRINT("\r\nselect %d\n", usel);
208 ret = updata_flash_code(usel);
209 if(ret < 0)
210 rlSendString("updata fail\r\n");
211 else
212 {
213 rlSendString("updata success\r\n");
214 }
215
216 }
217 }