bootmain.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #define PRINT(fmt, ...) serial_printf(fmt, ##__VA_ARGS__)
  31. #define ERROR(fmt, ...) serial_printf("ERROR %s() ln %d:" fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)
  32. extern unsigned int receive_count;
  33. int spi_flash_erases(struct spi_flash *spi_flash_local, u32 start_addr, int num_block)
  34. {
  35. int ret;
  36. int l = 0;;
  37. int k, j, page_par_block = 0;
  38. u32 len = 0, offset_new = start_addr;
  39. u32 i, block_size,page_size;
  40. block_size = spi_flash_local->block_size;
  41. page_size = spi_flash_local->page_size;
  42. len = page_size;
  43. page_par_block = block_size/page_size;
  44. for(l = 0; l < num_block; l++)
  45. {
  46. ret = spi_flash_local->erase(spi_flash_local, offset_new, block_size, 64);
  47. if(ret != 0)
  48. {
  49. return ret;
  50. }
  51. offset_new += block_size;
  52. }
  53. return ret;
  54. }
  55. int spi_flash_writes(struct spi_flash* spi_flash_local, u32 start_addr,u8 *data_buff, u32 page_num)
  56. {
  57. int k = 0,i = 0,j = 0,ret = 0;
  58. u32 offset_new = start_addr;
  59. unsigned char *data = (unsigned char *)data_buff;
  60. int spi_write = 1; // or 4
  61. int len = spi_flash_local->page_size;
  62. for(k = 0; k < page_num; k++)
  63. {
  64. ret = spi_flash_local->write(spi_flash_local, offset_new, len, (void*)data, spi_write);
  65. if(ret != 0)
  66. {
  67. return -1;
  68. }
  69. offset_new = offset_new + len;
  70. data = data + len;
  71. }
  72. return ret;
  73. }
  74. int updata_flash(u32 flash_addr)
  75. {
  76. int ret = 0;
  77. struct spi_flash* flash;
  78. u32 len = 0;
  79. int erase_block = 0;
  80. unsigned int page_count;
  81. unsigned int file_size = 0;
  82. // receive_count = 0;
  83. rlSendString("send a file by xmodem\r\n");
  84. // replace xmodem_recv_file, which may have too many errors to receive the file
  85. ret = xmodemReceive((unsigned char *)DEFAULT_BOOT_LOAD_ADDR, 192*1024);
  86. if(ret <= 0)
  87. return -1;
  88. file_size = ret;
  89. cadence_qspi_init(0, 1);
  90. flash = spi_flash_probe(0, 0, 10000000, 0, (u32)SPI_DATAMODE_8);
  91. if (!flash) {
  92. rlSendString("spi_flash_probe fail\r\n");
  93. return -1;
  94. }
  95. erase_block = (file_size + flash->block_size - 1) / flash->block_size;
  96. page_count = (file_size + flash->page_size - 1) / flash->page_size;
  97. ret = spi_flash_erases(flash, flash_addr, erase_block);
  98. if(ret < 0)
  99. {
  100. rlSendString("spi_flash_erases fail\r\n");
  101. return -1;
  102. }
  103. ret = spi_flash_writes(flash, flash_addr, (u8 *)DEFAULT_BOOT_LOAD_ADDR, page_count);
  104. if(ret < 0)
  105. {
  106. rlSendString("spi_flash_writes fail\r\n");
  107. return -1;
  108. }
  109. else
  110. {
  111. rlSendString("updata flash ok\r\n");
  112. }
  113. return 0;
  114. }
  115. static int updata_flash_code(unsigned int updata_num)
  116. {
  117. int ret = 0;
  118. switch (updata_num){
  119. case 0:
  120. ret = updata_flash(FLASH_SECONDBOOT_START_ADDR);
  121. break;
  122. case 1:
  123. ret = updata_flash(FLASH_DDRINIT_START_ADDR);
  124. break;
  125. default:
  126. break;
  127. }
  128. return ret;
  129. }
  130. static void chip_clk_init()
  131. {
  132. _SWITCH_CLOCK_clk_cpundbus_root_SOURCE_clk_pll0_out_;
  133. _SWITCH_CLOCK_clk_dla_root_SOURCE_clk_pll1_out_;
  134. _SWITCH_CLOCK_clk_dsp_root_SOURCE_clk_pll2_out_;
  135. _SWITCH_CLOCK_clk_perh0_root_SOURCE_clk_pll0_out_;
  136. }
  137. void BootMain(void)
  138. {
  139. int boot_mode = 0;
  140. s32 usel;
  141. int i;
  142. char str[128];
  143. int ret = 0;
  144. int bootdelay = SECONDBOOTDELAY;
  145. unsigned long ts;
  146. int abort = 0;
  147. chip_clk_init();
  148. _SET_SYSCON_REG_register50_SCFG_funcshare_pad_ctrl_18(0x00c000c0);
  149. _CLEAR_RESET_rstgen_rstn_usbnoc_axi_;
  150. _CLEAR_RESET_rstgen_rstn_hifi4noc_axi_;
  151. _ENABLE_CLOCK_clk_x2c_axi_;
  152. _CLEAR_RESET_rstgen_rstn_x2c_axi_;
  153. _CLEAR_RESET_rstgen_rstn_dspx2c_axi_;
  154. _CLEAR_RESET_rstgen_rstn_dma1p_axi_;
  155. _ENABLE_CLOCK_clk_msi_apb_;
  156. _CLEAR_RESET_rstgen_rstn_msi_apb_;
  157. _ASSERT_RESET_rstgen_rstn_x2c_axi_;
  158. _CLEAR_RESET_rstgen_rstn_x2c_axi_;
  159. uart_init(3);
  160. PRINT("\r\nVIC second boot, version:%s %s\n", VERSION, CONFIGURATION);
  161. while(1)
  162. {
  163. rlSendString("***************************************************\r\n");
  164. rlSendString("***************JH7100 recovery boot ***************\r\n");
  165. rlSendString("***************************************************\r\n");
  166. rlSendString("0:updata bootloader\r\n");
  167. rlSendString("1:updata ddr init\r\n");
  168. again:
  169. rlSendString("Select the function to test : ");
  170. serial_gets(str);
  171. if(str[0] == 0)
  172. goto again;
  173. usel = atoi(str);
  174. if(usel > 2)
  175. {
  176. rlSendString("error select,try again\r\n");
  177. goto again;
  178. }
  179. PRINT("\r\nselect %d\n", usel);
  180. ret = updata_flash_code(usel);
  181. if(ret < 0)
  182. rlSendString("updata fail\r\n");
  183. else
  184. {
  185. rlSendString("updata success\r\n");
  186. }
  187. }
  188. }