270 lines
		
	
	
		
			8.5 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			270 lines
		
	
	
		
			8.5 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
|  | /****************************************************************************
 | ||
|  | 
 | ||
|  | Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED. | ||
|  | 
 | ||
|  | This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT | ||
|  | be copied by any method or incorporated into another program without | ||
|  | the express written consent of Aerospace C.Power. This Information or any portion | ||
|  | thereof remains the property of Aerospace C.Power. The Information contained herein | ||
|  | is believed to be accurate and Aerospace C.Power assumes no responsibility or | ||
|  | liability for its use in any way and conveys no license or title under | ||
|  | any patent or copyright and makes no representation or warranty that this | ||
|  | Information is free from patent or copyright infringement. | ||
|  | 
 | ||
|  | ****************************************************************************/ | ||
|  | #include "chip_reg_base.h"
 | ||
|  | #include "hw_reg_api.h"
 | ||
|  | #include "iot_bitops.h"
 | ||
|  | #include "flash.h"
 | ||
|  | #include "os_lock.h"
 | ||
|  | #include "iot_config.h"
 | ||
|  | #include "ahb.h"
 | ||
|  | 
 | ||
|  | #include "sec_glb.h"
 | ||
|  | 
 | ||
|  | #if HW_PLATFORM > HW_PLATFORM_SIMU
 | ||
|  | #include "dbg_io.h"
 | ||
|  | #endif
 | ||
|  | #include "iot_io.h"
 | ||
|  | 
 | ||
|  | #define SFC_API_TEST   1
 | ||
|  | 
 | ||
|  | #if SFC_API_TEST == 1
 | ||
|  | #include "sfc.h"
 | ||
|  | #endif
 | ||
|  | 
 | ||
|  | #define FLASH_TEST_OFFSET   (0x100000)
 | ||
|  | uint8_t rdata[0x100] = {0}; | ||
|  | uint8_t wdata[0x100] = {1,2,3,4,5,6,7,8,9,0,255,254,253,252,251,250}; | ||
|  | 
 | ||
|  | void sfc_rst() { | ||
|  | 
 | ||
|  |     ahb_cache_disable(); | ||
|  | } | ||
|  | 
 | ||
|  | void get_id_test() | ||
|  | { | ||
|  |     uint8_t param[0x100] = {0}; | ||
|  |     iot_printf("[SFDP data]\n"); | ||
|  |     sfc_read_sfdp(param, 0x0, 0x4); | ||
|  |     iot_printf("0x0: %x %x %x %x\n", param[0], param[1], param[2], param[3]); | ||
|  |     sfc_read_sfdp(param, 0x10, 0x4); | ||
|  |     iot_printf("0x0: %x %x %x %x\n", param[0], param[1], param[2], param[3]); | ||
|  | 
 | ||
|  |     iot_printf("\n[SFC API TEST] start get id test\n"); | ||
|  |     //g_sfc_ctrl->get_dev_id(rdata);
 | ||
|  |     iot_printf("Manufacturer ID:%x%x\r\n",rdata[1], rdata[0]); | ||
|  |     sfc_qspi_get_id_mult(rdata, MOD_SFC_SERIAL); | ||
|  |     iot_printf("Manufacturer ID:%x%x\r\n",rdata[1], rdata[0]); | ||
|  |     sfc_qspi_get_id_mult(rdata, MOD_SFC_DUAL); | ||
|  |     iot_printf("Manufacturer ID dual: %x%x\r\n",rdata[1], rdata[0]); | ||
|  |     sfc_qspi_get_id_mult(rdata, MOD_SFC_QUAD); | ||
|  |     iot_printf("Manufacturer ID quad: %x%x\r\n",rdata[1], rdata[0]); | ||
|  |     iot_printf("[SFC API TEST] end get id test\n"); | ||
|  | } | ||
|  | 
 | ||
|  | void get_sts_reg_test() | ||
|  | { | ||
|  |     uint8_t data; | ||
|  |     uint8_t reg = 0; | ||
|  |     iot_printf("\n[SFC API TEST] start get status reg test\n"); | ||
|  |     g_sfc_ctrl->get_sts_reg(&data, reg); | ||
|  |     iot_printf("get reg[%d]: %08x\n ", reg, data); | ||
|  |     reg = 1; | ||
|  |     g_sfc_ctrl->get_sts_reg(&data, reg); | ||
|  |     iot_printf("get reg[%d]: %08x\n ", reg, data); | ||
|  |     iot_printf("[SFC API TEST] end get status reg test\n"); | ||
|  | } | ||
|  | 
 | ||
|  | void erase_test() | ||
|  | { | ||
|  |     iot_printf("\n[SFC API TEST] start erase test\n"); | ||
|  | 
 | ||
|  |     g_sfc_ctrl->read(rdata, FLASH_TEST_OFFSET, sizeof(wdata), MOD_SFC_READ_SIG); | ||
|  |     iot_printf("0x%8x: %02x %02x\n", FLASH_TEST_OFFSET, rdata[0], rdata[1]); | ||
|  |     g_sfc_ctrl->erase_block(FLASH_TEST_OFFSET, MOD_SW_MODE_DIS); | ||
|  |     g_sfc_ctrl->read(rdata, FLASH_TEST_OFFSET, sizeof(wdata), MOD_SFC_READ_SIG); | ||
|  |     iot_printf("0x%8x: %02x %02x\n", FLASH_TEST_OFFSET, rdata[0], rdata[1]); | ||
|  | 
 | ||
|  |     iot_printf("[SFC API TEST] end erase test\n"); | ||
|  | } | ||
|  | 
 | ||
|  | void write_test() | ||
|  | { | ||
|  |     iot_printf("\n[SFC API TEST] start write test\n"); | ||
|  |     g_sfc_ctrl->erase_sector(FLASH_TEST_OFFSET, MOD_SW_MODE_DIS); | ||
|  |     wdata[10] = 0x5a; | ||
|  |     rdata[10] = 0x0; | ||
|  |     g_sfc_ctrl->write(wdata,FLASH_TEST_OFFSET, sizeof(wdata), | ||
|  |             MOD_SFC_PROG_STAND, MOD_SW_MODE_DIS); | ||
|  |     g_sfc_ctrl->read(rdata,FLASH_TEST_OFFSET,sizeof(wdata), MOD_SFC_READ_SIG); | ||
|  |     if (wdata[10] != rdata[10]) { | ||
|  |         iot_printf("qspi write, qspi read error\n"); | ||
|  |     } else { | ||
|  |         iot_printf("qspi write, qspi read success\n"); | ||
|  |     } | ||
|  | 
 | ||
|  |     iot_printf("quad write test\n"); | ||
|  |     g_sfc_ctrl->erase_sector(FLASH_TEST_OFFSET, MOD_SW_MODE_DIS); | ||
|  |     wdata[10] = 0xa5; | ||
|  |     rdata[10] = 0x0; | ||
|  |     g_sfc_ctrl->write(wdata,FLASH_TEST_OFFSET,sizeof(wdata), | ||
|  |                       MOD_SFC_PROG_QUAD, MOD_SW_MODE_DIS); | ||
|  |     g_sfc_ctrl->read(rdata,FLASH_TEST_OFFSET,sizeof(wdata), MOD_SFC_READ_SIG); | ||
|  |     if (wdata[10] != rdata[10]) { | ||
|  |         iot_printf("quad write, qspi read error\n"); | ||
|  |     } else { | ||
|  |         iot_printf("quad write, qspi read success\n"); | ||
|  |     } | ||
|  |     iot_printf("[SFC API TEST] end write test\n"); | ||
|  | } | ||
|  | 
 | ||
|  | void read_test() | ||
|  | { | ||
|  |     iot_printf("\n[SFC API TEST] start read test\n"); | ||
|  |     iot_printf("read test\n"); | ||
|  |     g_sfc_ctrl->read(rdata,FLASH_TEST_OFFSET, | ||
|  |                 sizeof(wdata), MOD_SFC_READ_SIG); | ||
|  |     iot_printf("sig: rdata[10] = %02x\n", rdata[10]); | ||
|  |     g_sfc_ctrl->read(rdata,FLASH_TEST_OFFSET, | ||
|  |                 sizeof(wdata), MOD_SFC_READ_HIGH_SPD); | ||
|  |     iot_printf("high speed: rdata[10] = %02x\n", rdata[10]); | ||
|  |     g_sfc_ctrl->read(rdata,FLASH_TEST_OFFSET, | ||
|  |                 sizeof(wdata), MOD_SFC_READ_DUAL_FAST); | ||
|  |     iot_printf("dual fast: rdata[10] = %02x\n", rdata[10]); | ||
|  |     g_sfc_ctrl->read(rdata,FLASH_TEST_OFFSET, | ||
|  |                 sizeof(wdata), MOD_SFC_READ_QUAD_FAST); | ||
|  |     iot_printf("quad fast: rdata[10] = %02x\n", rdata[10]); | ||
|  |     g_sfc_ctrl->read(rdata,FLASH_TEST_OFFSET, | ||
|  |                 sizeof(wdata), MOD_SFC_READ_DUAL_IO_FAST); | ||
|  |     iot_printf("dual fast: rdata[10] = %02x\n", rdata[10]); | ||
|  |     g_sfc_ctrl->read(rdata,FLASH_TEST_OFFSET, | ||
|  |                 sizeof(wdata), MOD_SFC_READ_QUAD_IO_FAST); | ||
|  |     iot_printf("quad io fast: rdata[10] = %02x\n", rdata[10]); | ||
|  |     g_sfc_ctrl->read(rdata,FLASH_TEST_OFFSET, | ||
|  |                 sizeof(wdata), MOD_SFC_READ_QUAD_IO_WORD_FAST); | ||
|  |     iot_printf("quad io word fast: rdata[10] = %02x\n", rdata[10]); | ||
|  | 
 | ||
|  |     iot_printf("[SFC API TEST] end read test\n"); | ||
|  | } | ||
|  | 
 | ||
|  | uint8_t g_dump_data[0x1000] = {0}; | ||
|  | void static flash_dump_fw_data(uint32_t addr, uint32_t size) | ||
|  | { | ||
|  |     uint32_t i = 0; | ||
|  |     uint32_t j = 0; | ||
|  |     uint32_t n = 0; | ||
|  |     n = size / 0x1000; | ||
|  |     iot_printf("addr: %08x, size: %08x, n: %08x\n", addr, size, n); | ||
|  |     for(i = 0; i < n; i++) { | ||
|  |         flash_read(g_dump_data, addr, 0x1000, MOD_SFC_READ_QUAD_IO_FAST); | ||
|  |         iot_printf("\naddr: 0x%08x:\n", addr); | ||
|  |         for(j = 0; j < 0x1000; j++) { | ||
|  |             if (j%4 == 0) { | ||
|  |                 iot_printf(" "); | ||
|  |                 if (j%16 == 0) { | ||
|  |                     iot_printf("\n"); | ||
|  |                 } | ||
|  |             } | ||
|  |             iot_printf("%02x", g_dump_data[j]); | ||
|  | 
 | ||
|  |         } | ||
|  |         addr += 0x1000; | ||
|  |         iot_printf("\n"); | ||
|  |     } | ||
|  |     iot_printf("end...........\n"); | ||
|  | } | ||
|  | 
 | ||
|  | void flash_kunlun_dump() | ||
|  | { | ||
|  | #define PIB_ADDR_1 0x00012000
 | ||
|  | #define PIB_SIZE_1 0x00003000
 | ||
|  | #define PIB_ADDR_2 0x00019000
 | ||
|  | #define PIB_SIZE_2 0x00003000
 | ||
|  | #define FW_ADDR_1 0x00020000
 | ||
|  | #define FW_SIZE_1 0x00068000
 | ||
|  | #define FW_ADDR_2 0x00090000
 | ||
|  | #define FW_SIZE_2 0x00068000
 | ||
|  |     iot_printf("####start to dump flash data####\n"); | ||
|  | 
 | ||
|  |     iot_printf("\npib1 addr 0x%08x, size 0x%08\n", PIB_ADDR_1, PIB_SIZE_1); | ||
|  |     flash_dump_fw_data(PIB_ADDR_1, PIB_SIZE_1); | ||
|  | 
 | ||
|  |     iot_printf("\npib2 addr 0x%08x, size 0x%08\n", PIB_ADDR_2, PIB_SIZE_2); | ||
|  |     flash_dump_fw_data(PIB_ADDR_2, PIB_SIZE_2); | ||
|  | 
 | ||
|  |     iot_printf("\nfw1 addr 0x%08x, size 0x%08\n", FW_ADDR_1, FW_SIZE_1); | ||
|  |     flash_dump_fw_data(FW_ADDR_1, FW_SIZE_1); | ||
|  | 
 | ||
|  |     iot_printf("\nfw2 addr 0x%08x, size 0x%08\n", FW_ADDR_2, FW_SIZE_2); | ||
|  |     flash_dump_fw_data(FW_ADDR_2, FW_SIZE_2); | ||
|  | 
 | ||
|  |     iot_printf("####end to dump flash data####\n"); | ||
|  |     while(1); | ||
|  | } | ||
|  | 
 | ||
|  | void sfc_main() { | ||
|  |     uint8_t wip_sts; | ||
|  |     uint32_t loop = 0x10000; | ||
|  | 
 | ||
|  |     dbg_uart_init(); | ||
|  |     //dbg_uart_init_port(0, 1);  // enable 3000000 baud
 | ||
|  |     iot_printf("\r\n>>>>flash read&write test begin<<<<\r\n"); | ||
|  | 
 | ||
|  |     /* rst sfc */ | ||
|  |     //sfc_rst();
 | ||
|  | 
 | ||
|  |     /* init sfc and flash */ | ||
|  |     flash_init(1); | ||
|  | 
 | ||
|  |     do { | ||
|  |         iot_printf("quad: %d\n", g_sfc_ctrl->is_quad); | ||
|  | 
 | ||
|  |         get_id_test(); | ||
|  | 
 | ||
|  |         get_sts_reg_test(); | ||
|  | 
 | ||
|  |         //erase_test();
 | ||
|  | 
 | ||
|  |         g_sfc_ctrl->query_wip_sts(&wip_sts); | ||
|  | 
 | ||
|  |         if(wip_sts == 1) { | ||
|  |             iot_printf("FLASH is WORK IN STATUS\r\n"); | ||
|  |         } else{ | ||
|  |             iot_printf("erase sector test\n"); | ||
|  |             g_sfc_ctrl->erase_sector(FLASH_TEST_OFFSET, MOD_SW_MODE_DIS); | ||
|  |             g_sfc_ctrl->read(rdata,FLASH_TEST_OFFSET, | ||
|  |                     sizeof(wdata), MOD_SFC_READ_SIG); | ||
|  |             if (rdata[0] != 0xff) { | ||
|  |                 iot_printf("erase sector failed\n"); | ||
|  |             } else { | ||
|  |                 iot_printf("erase sector success\n\n"); | ||
|  |             } | ||
|  | 
 | ||
|  |             write_test(); | ||
|  |             g_sfc_ctrl->read(rdata,FLASH_TEST_OFFSET, | ||
|  |                     sizeof(wdata), MOD_SFC_READ_SIG); | ||
|  |             read_test(); | ||
|  | 
 | ||
|  |             g_sfc_ctrl->erase_sector(FLASH_TEST_OFFSET, MOD_SW_MODE_DIS); | ||
|  |             g_sfc_ctrl->read(rdata,FLASH_TEST_OFFSET, | ||
|  |                     sizeof(wdata), MOD_SFC_READ_SIG); | ||
|  |             read_test(); | ||
|  |         } | ||
|  | 
 | ||
|  |         loop = 0x10000; | ||
|  |         while(loop--); | ||
|  |     } while (0); | ||
|  | 
 | ||
|  |     return; | ||
|  | } | ||
|  | 
 | ||
|  | #ifdef __GNUC__
 | ||
|  | 
 | ||
|  | int main(void) { | ||
|  |     sfc_main(); | ||
|  |     return 0; | ||
|  | } | ||
|  | #endif // __GCC__
 | ||
|  | 
 |