From e1e78a5f08a9219e59fda115d1fb30984ce8b877 Mon Sep 17 00:00:00 2001 From: Richard Wheatley Date: Thu, 9 May 2024 13:38:10 -0500 Subject: [PATCH] drivers: hwinfo: Adds Ambiq hwinfo support This commit adds support for the hwinfo for Apollo4P SoCs Signed-off-by: Richard Wheatley --- boards/ambiq/apollo4p_evb/apollo4p_evb.yaml | 1 + drivers/hwinfo/CMakeLists.txt | 1 + drivers/hwinfo/Kconfig | 9 ++ drivers/hwinfo/hwinfo_ambiq.c | 137 ++++++++++++++++++++ modules/hal_ambiq/Kconfig | 5 + west.yml | 2 +- 6 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 drivers/hwinfo/hwinfo_ambiq.c diff --git a/boards/ambiq/apollo4p_evb/apollo4p_evb.yaml b/boards/ambiq/apollo4p_evb/apollo4p_evb.yaml index 1aa0fbf75d4..205da184346 100644 --- a/boards/ambiq/apollo4p_evb/apollo4p_evb.yaml +++ b/boards/ambiq/apollo4p_evb/apollo4p_evb.yaml @@ -14,6 +14,7 @@ supported: - gpio - spi - i2c + - hwinfo testing: ignore_tags: - net diff --git a/drivers/hwinfo/CMakeLists.txt b/drivers/hwinfo/CMakeLists.txt index 2e7d91141be..60707d59a61 100644 --- a/drivers/hwinfo/CMakeLists.txt +++ b/drivers/hwinfo/CMakeLists.txt @@ -29,3 +29,4 @@ zephyr_library_sources_ifdef(CONFIG_HWINFO_SMARTBOND hwinfo_smartbond.c) zephyr_library_sources_ifdef(CONFIG_HWINFO_STM32 hwinfo_stm32.c) zephyr_library_sources_ifdef(CONFIG_HWINFO_ANDES hwinfo_andes.c) zephyr_library_sources_ifdef(CONFIG_HWINFO_RW61X hwinfo_rw61x.c) +zephyr_library_sources_ifdef(CONFIG_HWINFO_AMBIQ hwinfo_ambiq.c) diff --git a/drivers/hwinfo/Kconfig b/drivers/hwinfo/Kconfig index 703201410de..b3bab9c82af 100644 --- a/drivers/hwinfo/Kconfig +++ b/drivers/hwinfo/Kconfig @@ -200,4 +200,13 @@ config HWINFO_RW61X help Enable RW61X hwinfo driver +config HWINFO_AMBIQ + bool "AMBIQ hwinfo" + default y + depends on SOC_SERIES_APOLLO4X + select AMBIQ_HAL + select AMBIQ_HAL_USE_HWINFO + help + Enable AMBIQ hwinfo driver + endif diff --git a/drivers/hwinfo/hwinfo_ambiq.c b/drivers/hwinfo/hwinfo_ambiq.c new file mode 100644 index 00000000000..80bcf62763a --- /dev/null +++ b/drivers/hwinfo/hwinfo_ambiq.c @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2024 Ambiq + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include + +ssize_t z_impl_hwinfo_get_device_id(uint8_t *buffer, size_t length) +{ + + struct ambiq_hwinfo { + /* Ambiq Chip ID0 */ + uint32_t chip_id_0; + /* Ambiq Chip ID1 */ + uint32_t chip_id_1; + /* Ambiq Factory Trim Revision */ + /* Can be used in Ambiq HAL for additional code support */ + uint32_t factory_trim_version; + }; + + struct ambiq_hwinfo dev_hw_info = {0}; + + /* Contains the HAL hardware information about the device. */ + am_hal_mcuctrl_device_t mcu_ctrl_device; + + am_hal_mram_info_read(1, AM_REG_INFO1_TRIM_REV_O / 4, 1, &dev_hw_info.factory_trim_version); + am_hal_mcuctrl_info_get(AM_HAL_MCUCTRL_INFO_DEVICEID, &mcu_ctrl_device); + + dev_hw_info.chip_id_0 = mcu_ctrl_device.ui32ChipID0; + dev_hw_info.chip_id_1 = mcu_ctrl_device.ui32ChipID1; + + if (length > sizeof(dev_hw_info)) { + length = sizeof(dev_hw_info); + } + + dev_hw_info.chip_id_0 = BSWAP_32(dev_hw_info.chip_id_0); + dev_hw_info.chip_id_1 = BSWAP_32(dev_hw_info.chip_id_1); + dev_hw_info.factory_trim_version = BSWAP_32(dev_hw_info.factory_trim_version); + memcpy(buffer, &dev_hw_info, length); + + return length; +} + +int z_impl_hwinfo_get_reset_cause(uint32_t *cause) +{ + uint32_t flags = 0; + uint32_t reset_status = 0; + am_hal_reset_status_t status = {0}; + + /* Print out reset status register upon entry */ + am_hal_reset_status_get(&status); + reset_status = status.eStatus; + + /* EXTERNAL PIN */ + if (reset_status & AM_HAL_RESET_STATUS_EXTERNAL) { + flags |= RESET_PIN; + } + + /* POWER CYCLE */ + if (reset_status & AM_HAL_RESET_STATUS_POR) { + flags |= RESET_POR; + } + + /* BROWNOUT DETECTOR */ + if (reset_status & AM_HAL_RESET_STATUS_BOD) { + flags |= RESET_BROWNOUT; + } + + /* SOFTWARE POR */ + if (reset_status & AM_HAL_RESET_STATUS_SWPOR) { + flags |= RESET_SOFTWARE; + } + + /* SOFTWARE POI */ + if (reset_status & AM_HAL_RESET_STATUS_SWPOI) { + flags |= RESET_SOFTWARE; + } + + /* DEBUGGER */ + if (reset_status & AM_HAL_RESET_STATUS_DEBUGGER) { + flags |= RESET_DEBUG; + } + + /* WATCHDOG */ + if (reset_status & AM_HAL_RESET_STATUS_WDT) { + flags |= RESET_WATCHDOG; + } + + /* BOUNREG */ + if (reset_status & AM_HAL_RESET_STATUS_BOUNREG) { + flags |= RESET_HARDWARE; + } + + /* BOCORE */ + if (reset_status & AM_HAL_RESET_STATUS_BOCORE) { + flags |= RESET_HARDWARE; + } + + /* BOMEM */ + if (reset_status & AM_HAL_RESET_STATUS_BOMEM) { + flags |= RESET_HARDWARE; + } + + /* BOHPMEM */ + if (reset_status & AM_HAL_RESET_STATUS_BOHPMEM) { + flags |= RESET_HARDWARE; + } + + *cause = flags; + return 0; +} + +int z_impl_hwinfo_clear_reset_cause(void) +{ + /* SBL maintains the RSTGEN->STAT register in + * INFO1 space even upon clearing RSTGEN->STAT + * register. + * - INFO1_RESETSTATUS + */ + + return -ENOSYS; +} + +int z_impl_hwinfo_get_supported_reset_cause(uint32_t *supported) +{ + *supported = RESET_PIN + | RESET_SOFTWARE + | RESET_POR + | RESET_WATCHDOG + | RESET_HARDWARE + | RESET_BROWNOUT; + return 0; +} diff --git a/modules/hal_ambiq/Kconfig b/modules/hal_ambiq/Kconfig index 24aaf1189cd..b329d117c89 100644 --- a/modules/hal_ambiq/Kconfig +++ b/modules/hal_ambiq/Kconfig @@ -45,4 +45,9 @@ config AMBIQ_HAL_USE_MSPI help Use the MSPI driver from Ambiq HAL +config AMBIQ_HAL_USE_HWINFO + bool + help + Use the HWINFO driver from Ambiq HAL + endif # AMBIQ_HAL diff --git a/west.yml b/west.yml index 94d555e9f16..55bfac7ae33 100644 --- a/west.yml +++ b/west.yml @@ -142,7 +142,7 @@ manifest: groups: - hal - name: hal_ambiq - revision: 705f1cbb1ccbdaac7613eb7b27c208c8f592e0c4 + revision: f6858cb3fe3d44945a23d114b7feeeac22ce71cf path: modules/hal/ambiq groups: - hal