diff --git a/arch/risc-v/src/bl602/Make.defs b/arch/risc-v/src/bl602/Make.defs index 6b7bc8c66e..a34d1830ed 100644 --- a/arch/risc-v/src/bl602/Make.defs +++ b/arch/risc-v/src/bl602/Make.defs @@ -50,7 +50,7 @@ endif CHIP_CSRCS = bl602_allocateheap.c CHIP_CSRCS += bl602_idle.c bl602_irq.c bl602_irq_dispatch.c CHIP_CSRCS += bl602_serial.c bl602_lowputc.c bl602_tim.c -CHIP_CSRCS += bl602_start.c bl602_timerisr.c +CHIP_CSRCS += bl602_start.c bl602_timerisr.c bl602_efuse.c ifeq ($(CONFIG_I2C),y) CHIP_CSRCS += bl602_i2c.c diff --git a/arch/risc-v/src/bl602/bl602_efuse.c b/arch/risc-v/src/bl602/bl602_efuse.c new file mode 100644 index 0000000000..2afcb96919 --- /dev/null +++ b/arch/risc-v/src/bl602/bl602_efuse.c @@ -0,0 +1,132 @@ +/**************************************************************************** + * arch/risc-v/src/bl602/bl602_efuse.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#include +#include +#include + +#include "chip.h" +#include "riscv_arch.h" + +#include "hardware/bl602_ef.h" +#include "bl602_romapi.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define bl602_romapi_efuse_ctrl_load_r0 \ + ((void (*)(void))BL602_ROMAPI_EFUSE_CTRL_LOAD_R0) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static inline uint32_t count_zero_bits_in_byte(uint8_t val) +{ + uint32_t cnt = 0; + uint32_t i = 0; + for (i = 0; i < 8; i++) + { + if ((val & (1 << i)) == 0) + { + cnt += 1; + } + } + + return cnt; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: bl602_efuse_read_mac_address + * + * Description: + * Read MAC address from efuse. + * + * Input Parameters: + * mac: the buffer to hold mac address + * + * Returned Value: + * 0: OK + * ENODATA: Failed + * + ****************************************************************************/ + +int bl602_efuse_read_mac_address(uint8_t mac[6]) +{ + DEBUGASSERT(mac != NULL); + + uint8_t *maclow = (uint8_t *)mac; + uint8_t *machigh = (uint8_t *)(mac + 4); + uint32_t tmp_val; + uint32_t i = 0; + uint32_t cnt = 0; + + /* Trigger read data from efuse */ + + bl602_romapi_efuse_ctrl_load_r0(); + + tmp_val = getreg32(BL602_EF_WIFI_MAC_LOW); + maclow[0] = tmp_val & 0xff; + maclow[1] = (tmp_val >> 8) & 0xff; + maclow[2] = (tmp_val >> 16) & 0xff; + maclow[3] = (tmp_val >> 24) & 0xff; + + tmp_val = getreg32(BL602_EF_WIFI_MAC_HIGH); + machigh[0] = tmp_val & 0xff; + machigh[1] = (tmp_val >> 8) & 0xff; + + /* Check parity */ + + for (i = 0; i < 6; i++) + { + cnt += count_zero_bits_in_byte(mac[i]); + } + + if ((cnt & 0x3f) == ((tmp_val >> 16) & 0x3f)) + { + /* Change to network order */ + + for (i = 0; i < 3; i++) + { + tmp_val = mac[i]; + mac[i] = mac[5 - i]; + mac[5 - i] = tmp_val; + } + + return 0; + } + else + { + return -ENODATA; + } +} + diff --git a/arch/risc-v/src/bl602/bl602_efuse.h b/arch/risc-v/src/bl602/bl602_efuse.h new file mode 100644 index 0000000000..fa1266c4af --- /dev/null +++ b/arch/risc-v/src/bl602/bl602_efuse.h @@ -0,0 +1,73 @@ +/**************************************************************************** + * arch/risc-v/src/bl602/bl602_efuse.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_RISCV_SRC_BL602_BL602_EFUSE_H +#define __ARCH_RISCV_SRC_BL602_BL602_EFUSE_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifndef __ASSEMBLY__ + +#undef EXTERN +#if defined(__cplusplus) +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: bl602_efuse_read_mac_address + * + * Description: + * Read MAC address from efuse. + * + * Input Parameters: + * mac: the buffer to hold mac address + * + * Returned Value: + * 0: OK + * ENODATA: Failed + * + ****************************************************************************/ + +int bl602_efuse_read_mac_address(uint8_t mac[6]); + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* __ARCH_RISCV_SRC_BL602_BL602_EFUSE_H */ diff --git a/arch/risc-v/src/bl602/bl602_flash.c b/arch/risc-v/src/bl602/bl602_flash.c index 91a8c0e254..9a124d1410 100644 --- a/arch/risc-v/src/bl602/bl602_flash.c +++ b/arch/risc-v/src/bl602/bl602_flash.c @@ -29,37 +29,37 @@ #include #include +#include "bl602_romapi.h" #ifdef CONFIG_BL602_SPIFLASH +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define bl602_romapi_sflash_erase \ + ((void (*)(uint8_t *, uint32_t, int))BL602_ROMAPI_SFLASH_EREASE_NEEDLOCK) + +#define bl602_romapi_sflash_write \ + ((void (*)(uint8_t *, uint32_t, const uint8_t *, int)) \ + BL602_ROMAPI_SFLASH_WRITE_NEEDLOCK) + +#define bl602_romapi_sflash_read \ + ((void (*)( \ + uint8_t *, uint32_t, uint8_t *, int))BL602_ROMAPI_SFLASH_READ_NEEDLOCK) + /**************************************************************************** * Private Data ****************************************************************************/ +static struct bl602_romflash_cfg_desc g_bl602_romflash_cfg; + struct bl602_romflash_cfg_desc { uint32_t magic; uint8_t cfg[84]; }; -#define ROMAPI_BASE (0x21010800) -#define ROMAPI_SFLASH_EREASE_NEEDLOCK (ROMAPI_BASE + 163 * 4) -#define ROMAPI_SFLASH_WRITE_NEEDLOCK (ROMAPI_BASE + 164 * 4) -#define ROMAPI_SFLASH_READ_NEEDLOCK (ROMAPI_BASE + 165 * 4) -#define ROMAPI_SFLASH_GET_JEDECID_NOLOCK (ROMAPI_BASE + 166 * 4) -#define ROMAPI_SFLASH_READ_WITHLOCK (ROMAPI_BASE + 170 * 4) -#define ROMAPI_SFLASH_WRITE_WITHLOCK (ROMAPI_BASE + 171 * 4) -#define ROMAPI_SFLASH_EREASE_WITHLOCK (ROMAPI_BASE + 172 * 4) - -static struct bl602_romflash_cfg_desc g_bl602_romflash_cfg; - -typedef void (*bl602_romdrv_erase_fn) (uint8_t *cfg, - uint32_t addr, int len); -typedef void (*bl602_romdrv_write_fn) (uint8_t *cfg, - uint32_t addr, const uint8_t *dst, int len); -typedef void (*bl602_romdrv_read_fn) (uint8_t *cfg, - uint32_t addr, uint8_t *dst, int len); - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -71,8 +71,7 @@ int bl602_flash_erase(uint32_t addr, int len) finfo("addr = %08lx, len = %d\n", addr, len); flags = up_irq_save(); - ((bl602_romdrv_erase_fn)(*((uint32_t *)(ROMAPI_SFLASH_EREASE_NEEDLOCK)))) - (g_bl602_romflash_cfg.cfg, addr, addr + len - 1); + bl602_romapi_sflash_erase(g_bl602_romflash_cfg.cfg, addr, addr + len - 1); up_irq_restore(flags); return 0; @@ -85,8 +84,7 @@ int bl602_flash_write(uint32_t addr, const uint8_t *src, int len) finfo("addr = %08lx, len = %d\n", addr, len); flags = up_irq_save(); - ((bl602_romdrv_write_fn)(*((uint32_t *)(ROMAPI_SFLASH_WRITE_NEEDLOCK)))) - (g_bl602_romflash_cfg.cfg, addr, src, len); + bl602_romapi_sflash_write(g_bl602_romflash_cfg.cfg, addr, src, len); up_irq_restore(flags); return 0; @@ -99,8 +97,7 @@ int bl602_flash_read(uint32_t addr, uint8_t *dst, int len) finfo("addr = %08lx, len = %d\n", addr, len); flags = up_irq_save(); - ((bl602_romdrv_read_fn)(*((uint32_t *)(ROMAPI_SFLASH_READ_NEEDLOCK)))) - (g_bl602_romflash_cfg.cfg, addr, dst, len); + bl602_romapi_sflash_read(g_bl602_romflash_cfg.cfg, addr, dst, len); up_irq_restore(flags); return 0; diff --git a/arch/risc-v/src/bl602/bl602_netdev.c b/arch/risc-v/src/bl602/bl602_netdev.c index 4186f80fb0..9e1522bb09 100644 --- a/arch/risc-v/src/bl602/bl602_netdev.c +++ b/arch/risc-v/src/bl602/bl602_netdev.c @@ -62,6 +62,7 @@ #include "wifi_manager/include/wifi_mgmr_ext.h" #include "wifi_driver/os_hal.h" #include "bl602_netdev.h" +#include "bl602_efuse.h" #ifdef CONFIG_BL602_WIRELESS @@ -2177,7 +2178,7 @@ int bl602_net_initialize(void) * Applies only if the Wireless MAC has its own internal address. */ - bl602_ef_ctrl_read_mac_address(mac); + bl602_efuse_read_mac_address(mac); wlinfo(":::MAC:%x %x %x %x %x %x\n", mac[0], mac[1], diff --git a/arch/risc-v/src/bl602/bl602_romapi.h b/arch/risc-v/src/bl602/bl602_romapi.h new file mode 100644 index 0000000000..c2696e3977 --- /dev/null +++ b/arch/risc-v/src/bl602/bl602_romapi.h @@ -0,0 +1,46 @@ +/**************************************************************************** + * arch/risc-v/src/bl602/bl602_romapi.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_RISCV_SRC_BL602_BL602_ROMAPI_H +#define __ARCH_RISCV_SRC_BL602_BL602_ROMAPI_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#define BL602_ROMAPI_BASE (0x21010800) +#define BL602_ROMAPI_FUNC(idx) (*(uintptr_t *)(BL602_ROMAPI_BASE + (idx)*4)) + +#define BL602_ROMAPI_ASM_DELAY_US BL602_ROMAPI_FUNC(20) +#define BL602_ROMAPI_EFUSE_CTRL_LOAD_R0 BL602_ROMAPI_FUNC(31) +#define BL602_ROMAPI_RST_SYSTEM BL602_ROMAPI_FUNC(47) +#define BL602_ROMAPI_RST_CPU_SW BL602_ROMAPI_FUNC(48) +#define BL602_ROMAPI_RST_POR BL602_ROMAPI_FUNC(49) +#define BL602_ROMAPI_SFLASH_EREASE_NEEDLOCK BL602_ROMAPI_FUNC(163) +#define BL602_ROMAPI_SFLASH_WRITE_NEEDLOCK BL602_ROMAPI_FUNC(164) +#define BL602_ROMAPI_SFLASH_READ_NEEDLOCK BL602_ROMAPI_FUNC(165) +#define BL602_ROMAPI_SFLASH_GET_JEDECID_NOLOCK BL602_ROMAPI_FUNC(166) +#define BL602_ROMAPI_SFLASH_READ_WITHLOCK BL602_ROMAPI_FUNC(170) +#define BL602_ROMAPI_SFLASH_WRITE_WITHLOCK BL602_ROMAPI_FUNC(171) +#define BL602_ROMAPI_SFLASH_EREASE_WITHLOCK BL602_ROMAPI_FUNC(172) + +#endif diff --git a/arch/risc-v/src/bl602/bl602_systemreset.c b/arch/risc-v/src/bl602/bl602_systemreset.c index 172ae632d9..c639fc4a52 100644 --- a/arch/risc-v/src/bl602/bl602_systemreset.c +++ b/arch/risc-v/src/bl602/bl602_systemreset.c @@ -29,6 +29,11 @@ #include "hardware/bl602_glb.h" #include "hardware/bl602_hbn.h" +#include "bl602_romapi.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ /* We choose to use ROM driver here. * @@ -36,11 +41,9 @@ * reset, this part of the code cannot be placed on the XIP Flash. */ -typedef void (*bl602_romdrv_reset_system) (void); -typedef void (*bl602_romdrv_reset_sw_cpu) (void); -typedef void (*bl602_romdrv_reset_por) (void); - -#define ROM_APITABLE ((uint32_t *)0x21010800) +#define bl602_romapi_reset_system ((void (*)(void))BL602_ROMAPI_RST_SYSTEM) +#define bl602_romapi_reset_cpu_sw ((void (*)(void))BL602_ROMAPI_RST_CPU_SW) +#define bl602_romapi_reset_por ((void (*)(void))BL602_ROMAPI_RST_POR) /**************************************************************************** * Private Functions @@ -64,7 +67,7 @@ void up_systemreset(void) asm volatile("csrci mstatus, 8"); - ((bl602_romdrv_reset_system)(*(ROM_APITABLE + 47)))(); + bl602_romapi_reset_system(); } /**************************************************************************** @@ -81,7 +84,7 @@ void bl602_cpu_reset(void) asm volatile("csrci mstatus, 8"); - ((bl602_romdrv_reset_sw_cpu)(*(ROM_APITABLE + 48)))(); + bl602_romapi_reset_cpu_sw(); } /**************************************************************************** @@ -98,5 +101,5 @@ void bl602_por_reset(void) asm volatile("csrci mstatus, 8"); - ((bl602_romdrv_reset_por)(*(ROM_APITABLE + 49)))(); + bl602_romapi_reset_por(); }