zhangyu-duck commented on code in PR #21546: URL: https://github.com/apache/nuttx/pull/21546#discussion_r4563648611
########## arch/tricore/src/tc3xx/tc3xx_start.c: ########## @@ -0,0 +1,159 @@ +/**************************************************************************** + * arch/tricore/src/tc3xx/tc3xx_start.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 <nuttx/config.h> + +#include <stdbool.h> +#include <stdint.h> + +#include <arch/arch.h> + +#include "tricore_internal.h" +#include "tricore_start.h" +#include "hardware/tc3xx_cpu.h" +#include "hardware/tc3xx_scu.h" +#include "tc3xx_wdt.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* The BIV of the single interrupt vector at INTTAB + 0x1fe0 */ + +#define TC3XX_BIV_VECTOR 0x1fe0 + +/**************************************************************************** + * External Function Prototypes + ****************************************************************************/ + +void core0_main(void); + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/* Provided by the linker script */ + +extern uintptr_t __CSA0[]; +extern uintptr_t __CSA0_END[]; Review Comment: common code ########## arch/tricore/src/common/tricore_main.c: ########## @@ -48,15 +93,9 @@ static void core_main(void) /* Wait for CPU sync event */ - IfxCpu_emitEvent(&g_sync_event); - -#if defined(CONFIG_ARCH_CHIP_TC3XX) - IfxCpu_waitEvent(&g_sync_event, 1); -#else - IfxCpu_waitEvent(&g_sync_event, UINT32_MAX, 1); -#endif + tricore_core_sync(&g_sync_event); - if (IfxCpu_getCoreIndex() == 0) + if (tricore_core_index() == 0) Review Comment: use common function ########## arch/tricore/src/tc3xx/tc3xx_wdt.c: ########## @@ -66,23 +66,45 @@ static void tc3xx_wdt_endinit(uintptr_t con0, uint32_t pw, bool set) } /**************************************************************************** - * Name: tc3xx_wdt_disable + * Name: tc3xx_wdt_password + ****************************************************************************/ + +static uint32_t tc3xx_wdt_password(uintptr_t con0) +{ + return ((getreg32(con0) & TC3XX_WDT_CON0_PW_MASK) >> + TC3XX_WDT_CON0_PW_SHIFT) ^ TC3XX_WDT_PW_INVERTED; +} + +/**************************************************************************** + * Name: tc3xx_wdt_request * * Description: - * Request a watchdog to be disabled. + * Request a watchdog to be disabled or enabled again. * ****************************************************************************/ -static void tc3xx_wdt_disable(uintptr_t con0, uintptr_t con1) +static void tc3xx_wdt_request(uintptr_t con0, uintptr_t con1, bool disable) { - uint32_t pw = ((getreg32(con0) & TC3XX_WDT_CON0_PW_MASK) >> - TC3XX_WDT_CON0_PW_SHIFT) ^ TC3XX_WDT_PW_INVERTED; + uint32_t pw = tc3xx_wdt_password(con0); + uint32_t regval; tc3xx_wdt_endinit(con0, pw, false); - putreg32(getreg32(con1) | TC3XX_WDT_CON1_DR, con1); + regval = getreg32(con1) & ~TC3XX_WDT_CON1_DR; + putreg32(regval | (disable ? TC3XX_WDT_CON1_DR : 0), con1); tc3xx_wdt_endinit(con0, pw, true); } +/**************************************************************************** + * Name: tc3xx_wdt_cpu + ****************************************************************************/ + +static uint32_t tc3xx_wdt_cpu(void) +{ + uint32_t cpu = tricore_mfcr(TRICORE_CPU_CORE_ID); + + return cpu > 5 ? 5 : cpu; Review Comment: remove ########## arch/tricore/src/common/tasking/tricore_head.asm: ########## @@ -0,0 +1,163 @@ +/**************************************************************************** + * arch/tricore/src/common/tasking/tricore_head.asm + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 <nuttx/config.h> +#include <arch/csfr.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define TRICORE_PSW_RESET 0x980 /* Supervisor, call depth counting on */ + +#ifdef CONFIG_ARCH_CHIP_TC4XX +# define TC4XX_BOOT_SP 0x7010eb00 Review Comment: The implementation here is odd; the SP being launched should have a consistent name. ########## arch/tricore/src/tc4xx/tc4xx_start.c: ########## @@ -0,0 +1,323 @@ +/**************************************************************************** + * arch/tricore/src/tc4xx/tc4xx_start.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 <nuttx/config.h> + +#include <stdbool.h> +#include <stdint.h> + +#include <arch/arch.h> + +#include "tricore_internal.h" +#include "tricore_start.h" +#include "hardware/tc4xx_clock.h" +#include "hardware/tc4xx_cpu.h" +#include "hardware/tc4xx_pms.h" +#include "hardware/tc4xx_smm.h" +#include "hardware/tc4xx_vmt.h" +#include "tc4xx_wdt.h" +#include "tricore_stm.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Watchdog reload values of the startup and of the application */ + +#define TC4XX_WDT_RELOAD_STARTUP 0xffc2 +#define TC4XX_WDT_RELOAD_APP 0xfffc + +/* STM of CPU0 */ + +#define TC4XX_STM0_ABS (TRICORE_STM0_BASE + TRICORE_STM_ABS_OFFSET) + +/* The BIV of the single interrupt vector at INTTAB + 0x1fe0 */ + +#define TC4XX_BIV_VECTOR 0x1fe0 + +/**************************************************************************** + * External Function Prototypes + ****************************************************************************/ + +void core0_main(void); + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/* Provided by the linker script */ + +extern uintptr_t __CSA0[]; +extern uintptr_t __CSA0_END[]; +extern uintptr_t __INTTAB_CPU0[]; Review Comment: common code ########## arch/tricore/src/tc3xx/tc3xx_wdt.c: ########## @@ -66,23 +66,45 @@ static void tc3xx_wdt_endinit(uintptr_t con0, uint32_t pw, bool set) } /**************************************************************************** - * Name: tc3xx_wdt_disable + * Name: tc3xx_wdt_password + ****************************************************************************/ + +static uint32_t tc3xx_wdt_password(uintptr_t con0) +{ + return ((getreg32(con0) & TC3XX_WDT_CON0_PW_MASK) >> + TC3XX_WDT_CON0_PW_SHIFT) ^ TC3XX_WDT_PW_INVERTED; +} + +/**************************************************************************** + * Name: tc3xx_wdt_request * * Description: - * Request a watchdog to be disabled. + * Request a watchdog to be disabled or enabled again. * ****************************************************************************/ -static void tc3xx_wdt_disable(uintptr_t con0, uintptr_t con1) +static void tc3xx_wdt_request(uintptr_t con0, uintptr_t con1, bool disable) { - uint32_t pw = ((getreg32(con0) & TC3XX_WDT_CON0_PW_MASK) >> - TC3XX_WDT_CON0_PW_SHIFT) ^ TC3XX_WDT_PW_INVERTED; + uint32_t pw = tc3xx_wdt_password(con0); + uint32_t regval; tc3xx_wdt_endinit(con0, pw, false); - putreg32(getreg32(con1) | TC3XX_WDT_CON1_DR, con1); + regval = getreg32(con1) & ~TC3XX_WDT_CON1_DR; + putreg32(regval | (disable ? TC3XX_WDT_CON1_DR : 0), con1); tc3xx_wdt_endinit(con0, pw, true); } +/**************************************************************************** + * Name: tc3xx_wdt_cpu + ****************************************************************************/ + +static uint32_t tc3xx_wdt_cpu(void) +{ + uint32_t cpu = tricore_mfcr(TRICORE_CPU_CORE_ID); + + return cpu > 5 ? 5 : cpu; Review Comment: Why do you keep adding so many unnecessary and redundant interfaces? ########## arch/tricore/src/tc3xx/tc3xx_wdt.c: ########## @@ -66,23 +66,45 @@ static void tc3xx_wdt_endinit(uintptr_t con0, uint32_t pw, bool set) } /**************************************************************************** - * Name: tc3xx_wdt_disable + * Name: tc3xx_wdt_password + ****************************************************************************/ + +static uint32_t tc3xx_wdt_password(uintptr_t con0) +{ + return ((getreg32(con0) & TC3XX_WDT_CON0_PW_MASK) >> + TC3XX_WDT_CON0_PW_SHIFT) ^ TC3XX_WDT_PW_INVERTED; +} + +/**************************************************************************** + * Name: tc3xx_wdt_request * * Description: - * Request a watchdog to be disabled. + * Request a watchdog to be disabled or enabled again. * ****************************************************************************/ -static void tc3xx_wdt_disable(uintptr_t con0, uintptr_t con1) +static void tc3xx_wdt_request(uintptr_t con0, uintptr_t con1, bool disable) { - uint32_t pw = ((getreg32(con0) & TC3XX_WDT_CON0_PW_MASK) >> - TC3XX_WDT_CON0_PW_SHIFT) ^ TC3XX_WDT_PW_INVERTED; + uint32_t pw = tc3xx_wdt_password(con0); + uint32_t regval; tc3xx_wdt_endinit(con0, pw, false); - putreg32(getreg32(con1) | TC3XX_WDT_CON1_DR, con1); + regval = getreg32(con1) & ~TC3XX_WDT_CON1_DR; + putreg32(regval | (disable ? TC3XX_WDT_CON1_DR : 0), con1); tc3xx_wdt_endinit(con0, pw, true); } +/**************************************************************************** + * Name: tc3xx_wdt_cpu + ****************************************************************************/ + +static uint32_t tc3xx_wdt_cpu(void) +{ + uint32_t cpu = tricore_mfcr(TRICORE_CPU_CORE_ID); + + return cpu > 5 ? 5 : cpu; Review Comment: up_cpu_index, use it ########## arch/tricore/src/tc3xx/tc3xx_start.c: ########## @@ -0,0 +1,277 @@ +/**************************************************************************** + * arch/tricore/src/tc3xx/tc3xx_start.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 <nuttx/config.h> + +#include <stdbool.h> +#include <stdint.h> + +#include <arch/arch.h> + +#include "tricore_internal.h" +#include "hardware/tc3xx_cpu.h" +#include "hardware/tc3xx_scu.h" +#include "tc3xx_wdt.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* The BIV of the single interrupt vector at INTTAB + 0x1fe0 */ + +#define TC3XX_BIV_VECTOR 0x1fe0 + +/**************************************************************************** + * External Function Prototypes + ****************************************************************************/ + +void core0_main(void); + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/* Provided by the linker script */ + +extern uintptr_t __CSA0[]; +extern uintptr_t __CSA0_END[]; +extern uintptr_t __TRAPTAB_CPU0[]; +extern uintptr_t __INTTAB_CPU0[]; +extern uintptr_t __ISTACK0[]; +extern uintptr_t __START1[]; + +#ifndef CONFIG_TRICORE_TOOLCHAIN_TASKING +extern const uintptr_t __clear_table[]; +extern const uintptr_t __copy_table[]; +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tc3xx_csa_init + * + * Description: + * Link the CSAs from start to end into the free context list, with LCX + * at the third last. It runs before any function call is possible, so + * it must not call. + * + ****************************************************************************/ + +static inline_function void tc3xx_csa_init(uintptr_t *start, uintptr_t *end) +{ + uint32_t num = ((uintptr_t)end - (uintptr_t)start) / TC_CONTEXT_SIZE; + uintptr_t *prev = NULL; + uintptr_t *csa = start; + uint32_t link; + uint32_t k; + + for (k = 0; k < num; k++) + { + link = tricore_addr2csa(csa); + if (k == 0) + { + tricore_mtcr(TRICORE_CPU_FCX, link); + } + else + { + *prev = link; + } + + if (k == num - 3) + { + tricore_mtcr(TRICORE_CPU_LCX, link); + } + + prev = csa; + csa += TC_CONTEXT_REGS; + } + + *prev = 0; + UP_DSB(); +} + +/**************************************************************************** + * Name: tc3xx_application_reset + ****************************************************************************/ + +static bool tc3xx_application_reset(void) +{ + uint32_t rststat = getreg32(TC3XX_SCU_RSTSTAT); + uint32_t bit; + + if ((rststat & TC3XX_RSTSTAT_POWERON) != 0) + { + return false; + } + + if ((rststat & TC3XX_RSTSTAT_RESET) != 0) + { + rststat &= TC3XX_RSTSTAT_RESET; + for (bit = 31; (rststat & (1u << bit)) == 0; bit--); + + return ((getreg32(TC3XX_SCU_RSTCON) >> (bit << 1)) & 3) == + TC3XX_RSTCON_APPLICATION; + } + + if ((rststat & TC3XX_RSTSTAT_CB3) != 0) + { + return true; + } + + return (getreg32(TC3XX_CPU0_KRST0) & TC3XX_KRST0_RSTSTAT_MASK) != 0; +} + +/**************************************************************************** + * Name: tc3xx_c_init + * + * Description: + * Clear .bss and copy .data, from the tables of the linker script on + * GCC, with the TASKING runtime otherwise. + * + ****************************************************************************/ + +static void tc3xx_c_init(void) +{ +#ifdef CONFIG_TRICORE_TOOLCHAIN_TASKING + extern void _c_init(void); + + _c_init(); +#else + const uintptr_t *table; + uint8_t *src; + uint8_t *dst; + uint32_t len; + + for (table = __clear_table; ; table += 2) + { + dst = (uint8_t *)table[0]; + len = table[1]; + if (len == 0xffffffff) + { + break; + } + + while (len >= 8) + { + *(uint64_t *)dst = 0; + dst += 8; + len -= 8; + } + + while (len-- > 0) + { + *dst++ = 0; + } + } + + for (table = __copy_table; ; table += 3) + { + src = (uint8_t *)table[0]; + dst = (uint8_t *)table[1]; + len = table[2]; + if (len == 0xffffffff) + { + break; + } + + while (len >= 8) + { + *(uint64_t *)dst = *(uint64_t *)src; + dst += 8; + src += 8; + len -= 8; + } + + while (len-- > 0) + { + *dst++ = *src++; + } + } +#endif +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tricore_start + * + * Description: + * C entry of the startup, jumped to from _START in tricore_head with the + * stack and the small data pointers set. It does what the iLLD startup + * code did for CPU0: link the CSAs, set up the clocks after a power-on + * reset, initialize the C runtime with the watchdogs disabled, start + * CPU1, enable the caches, set BTV, BIV and ISP and enter core0_main. + * + ****************************************************************************/ + +void tricore_start(void) +{ + tc3xx_csa_init(__CSA0, __CSA0_END); + UP_ISB(); + + if (!tc3xx_application_reset()) + { + tc3xx_wdt_service(); + if (tricore_clockconfig() != 0) + { + __asm__ volatile ("debug"); + } + } + + tc3xx_wdt_set_disabled(true); + tc3xx_c_init(); + tc3xx_wdt_set_disabled(false); + +#ifndef CONFIG_TRICORE_BL + putreg32((getreg32(TC3XX_CPU1_PC) & 1) | ((uintptr_t)__START1 & ~1), + TC3XX_CPU1_PC); + if ((getreg32(TC3XX_CPU1_SYSCON) & TC3XX_SYSCON_BHALT) != 0) + { + putreg32(getreg32(TC3XX_CPU1_SYSCON) & ~TC3XX_SYSCON_BHALT, + TC3XX_CPU1_SYSCON); + } +#endif + + /* Caches on, trap and interrupt vectors, interrupt stack */ + + tc3xx_wdt_cpu_endinit(false); + tricore_mtcr(TRICORE_CPU_PCON0, 0); + UP_ISB(); + tricore_mtcr(TRICORE_CPU_DCON0, 0); + UP_ISB(); + tricore_mtcr(TRICORE_CPU_BTV, (uintptr_t)__TRAPTAB_CPU0); Review Comment: Why not move all the generic code into the assembly file? It seems cleaner that way. I’ve moved such code into an assembly file without handling `endinit`, and I haven’t encountered any issues so far—could you explain why that might be? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
