Files
kunlun/dtest/ddr_training_test/ddr_training_test.c
2024-09-28 14:24:04 +08:00

255 lines
7.2 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.
****************************************************************************/
/* os shim includes */
#include "os_types.h"
#include "os_utils.h"
#include "os_mem.h"
#include "dbg_io.h"
#include "iot_diag.h"
#include "iot_io.h"
#include "ddr_reg_rf.h"
#include "dmc_reg_rf.h"
#include "hw_reg_api.h"
#include "ahb.h"
#include "k3d_reg.h"
#include "ai_glb_reg.h"
#include "mac_sys_reg.h"
#include "cpu.h"
#include "ddrc.h"
#include "flash.h"
#include "pmu_hw.h"
#include "ana.h"
#include "clk.h"
#include "efuse.h"
#include "efuse_mapping.h"
// ############################################################################
// common define
#define REG32(a) (*((volatile uint32_t *)(a)))
#define EFUSE_MAC_TEST_ERR (1 << 0)
#define DDR_MEM_TEST_ERR (1 << 1)
#define FLASH_TEST_ERR (1 << 2)
uint32_t err_id = 0;
void efuse_mac_read()
{
uint32_t tmp;
uint8_t mac[6] = {0};
tmp = efuse_read(CFG_EFUSE_BITS32_2_ADDR);
mac[0] = REG_FIELD_GET(MAC_ADDR_B0, tmp);
tmp = efuse_read(CFG_EFUSE_BITS32_3_ADDR);
mac[1] = REG_FIELD_GET(MAC_ADDR_B1, tmp);
mac[2] = REG_FIELD_GET(MAC_ADDR_B2, tmp);
mac[3] = REG_FIELD_GET(MAC_ADDR_B3, tmp);
mac[4] = REG_FIELD_GET(MAC_ADDR_B4, tmp);
tmp = efuse_read(CFG_EFUSE_BITS32_4_ADDR);
mac[5] = REG_FIELD_GET(MAC_ADDR_B5, tmp);
iot_printf("[MAC_ID_TEST]efuse read MAC:%02X:%02X:%02X:%02X:%02X:%02X\n",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
if (mac[0] != 0x48 || mac[1] != 0x55 || mac[2] != 0x5c) {
err_id |= EFUSE_MAC_TEST_ERR;
iot_printf("####MAC ID TEST FAILED.####\n");
} else {
iot_printf("####MAC ID TEST OK.####\n");
}
}
#define DDR_MEM_START_ADDR (0)
#define DDR_MEM_ICACHE_ADDR (0x18000000)
#define DDR_MEM_DCACHE_ADDR (0x10000000)
#define DDR_MEM_SIZE_32MB (8*1024*1024) // unit is word, total size: 32MB
#define DDR_MEM_SIZE_8MB (2*1024*1024) // unit is word, total size: 8MB
#define DDR_MEM_TEST_ADDR (DDR_MEM_ICACHE_ADDR + DDR_MEM_START_ADDR)
void dump_dmc_reg()
{
volatile uint32_t *reg = (volatile uint32_t *) DDR_RF_BASEADDR;
iot_printf("dump ddr reg(%p):\n", reg);
for(uint32_t i = 0; i < 0x300/4; i++) {
if ((i % 4) == 0) {
iot_printf("\n%p: ", (reg+i+1));
}
iot_printf("0x%08x ", *(reg+i));
}
iot_printf("\n");
reg = (volatile uint32_t *) DMC_RF_BASEADDR;
iot_printf("dump dmc reg(%p):\n", reg);
for(uint32_t i = 0; i < 0x300/4; i++) {
if ((i % 4) == 0) {
iot_printf("\n%p: ", (reg+i+1));
}
iot_printf("0x%08x ", *(reg+i));
}
iot_printf("\n");
}
void ddr_mem_dtest(uint32_t mode, uint32_t cnt)
{
iot_printf("\n-------DDR MEMORY TEST---------\n");
volatile uint32_t *src = (volatile uint32_t *) DDR_MEM_TEST_ADDR;
uint32_t test_size = 0;
if (mode == DEV_X16_MODE) {
test_size = DDR_MEM_SIZE_32MB;
} else {
test_size = DDR_MEM_SIZE_8MB;
}
/* write */
for(uint32_t i = 0; i < test_size; i++) {
*(src + i) = i;
}
/* read */
uint8_t err = 0;
uint32_t tmp;
for(uint32_t i = 0; i < test_size; i++) {
tmp = *(src + i);
if (tmp != i) {
iot_printf("read error....addr-0x%08x: 0x%08x-0x%08x\n",
i*4, i, tmp);
err_id |= DDR_MEM_TEST_ERR;
err = 1;
}
}
if (err == 1) {
dump_dmc_reg();
iot_printf("failed cnt: %d\n", cnt);
iot_printf("####DDR MEMORY TEST FAILED.####\n");
while(1);
} else {
iot_printf("####DDR MEMORY TEST OK.####\n");
}
}
static uint8_t rdata[0x100] = {0};
static uint8_t wdata[0x100] = {0};
#define FLASH_TEST_OFFSET (0x0)
#define FLASH_VENDOR_ID_GD 0xc8
#define FLASH_VENDOR_ID_PUYA 0x85
#define FLASH_VENDOR_ID_ZBIT 0x5e
#define FLASH_VENDOR_ID_WINBOND 0xef
void flash_test(uint32_t mode)
{
iot_printf("\n-------FLASH TEST---------\n");
uint8_t flag = 0;
if (mode == DEV_X8_MODE) {
uint8_t data;
g_sfc_ctrl->get_dev_id(&data);
iot_printf("[FLASH_TEST]get id: %02x\n", data);
if (data != FLASH_VENDOR_ID_GD && data != FLASH_VENDOR_ID_PUYA\
&& data != FLASH_VENDOR_ID_ZBIT && data != FLASH_VENDOR_ID_WINBOND){
iot_printf("[FLASH_TEST]id is not match.\n");
flag = 1;
goto err;
}
for(uint32_t i = 0; i < 0x100; i++) {
wdata[i] = i;
}
g_sfc_ctrl->erase_sector(FLASH_TEST_OFFSET, MOD_SW_MODE_DIS);
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);
for(uint32_t i = 0; i < 0x100; i++) {
if (wdata[i] != rdata[i]) {
flag = 1;
iot_printf("[FLASH_TEST]not match, index: %d, \
write:%02x, read: %02x\n", wdata[i], rdata[i]);
break;
}
}
} else {
flag = 2;
}
err:
if (flag == 2) {
iot_printf("####FLASH TEST TEST BYPASS.####\n");
} else if (flag == 1) {
err_id |= FLASH_TEST_ERR;
iot_printf("####FLASH TEST TEST FAILED.####\n");
} else {
iot_printf("####FLASH TEST TEST OK.####\n");
}
}
int main(void)
{
dbg_uart_init();
iot_printf("\n-------EFUSE MAC TEST---------\n");
efuse_mac_read();
iot_printf("####EFUSE MAC TEST FINISHED.####\n");
iot_printf("\n-------DDR TRAINING TEST---------\n");
//disable auto-baud;
REG32(0x44001020) &= ~0x1;
//change CPU core to 150M.
clk_core_freq_set(CPU_FREQ_150M);
//flash init;
flash_init(1);
// disable cache
ahb_cache_disable();
ddr_cache_init();
// enable ahb cache
ahb_cache_enable();
//ahb_cache_reset();
//enable dmc cache;
ahb_dmc_cache_enable();
//cache space init;
ahb_cache_fill_valid_space();
iot_printf("####DDR TRAINING TEST FINISHED.####\n");
uint32_t mode;
uint32_t pkt_type = ahb_get_pkg_type();
if (pkt_type == AHB_PKG_AI) {
mode = DEV_X16_MODE;
} else {
mode = DEV_X8_MODE;
}
ddr_mem_dtest(mode, 0);
flash_test(mode);
iot_printf("\n[TEST]result: %08x\n", err_id);
if (err_id) {
iot_printf("[TEST]test failed.\n");
} else {
iot_printf("[TEST]test ok.\n");
}
iot_printf("\n****TEST FINISHED.****\n");
return 0;
}