This is an automated email from the ASF dual-hosted git repository. GUIDINGLI pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 2c7259faacbdbbdc2e25fded4bedbc508269fc7b Author: Saravanan Sekar <[email protected]> AuthorDate: Fri Nov 6 16:57:54 2026 +0530 arch/tricore: Set up the TC4xx clocks without the iLLD. The startup code sets up the TC4xx flash wait states, clock dividers, oscillators and PLLs with the iLLD IfxClock_init and its TC4Dx default configuration. Add tricore_clockconfig for TC4xx, which runs the same sequence with the clock, SMU and DMU registers: set the wait states and dividers, power up the 25 MHz crystal and the ramp oscillator, set up both PLLs, ramp the system clock up and switch both clocks to the PLLs. The long waits service the watchdogs, as IfxClock does. The clock registers end up with the same values as with IfxClock_init. Co-authored-by: Parthiban Nallathambi <[email protected]> Signed-off-by: Saravanan Sekar <[email protected]> --- .../src/illd/tc4xx/Configurations/Ifx_Cfg_Ssw.c | 2 +- .../src/illd/tc4xx/Configurations/Ifx_Cfg_Ssw.h | 8 + arch/tricore/src/tc4xx/CMakeLists.txt | 2 +- arch/tricore/src/tc4xx/Make.defs | 1 + arch/tricore/src/tc4xx/hardware/tc4xx_clock.h | 99 ++++ arch/tricore/src/tc4xx/hardware/tc4xx_dmu.h | 37 ++ arch/tricore/src/tc4xx/hardware/tc4xx_smu.h | 40 ++ arch/tricore/src/tc4xx/tc4xx_clockconfig.c | 497 +++++++++++++++++++++ arch/tricore/src/tc4xx/tc4xx_wdt.c | 60 +++ arch/tricore/src/tc4xx/tc4xx_wdt.h | 32 ++ 10 files changed, 776 insertions(+), 2 deletions(-) diff --git a/arch/tricore/src/illd/tc4xx/Configurations/Ifx_Cfg_Ssw.c b/arch/tricore/src/illd/tc4xx/Configurations/Ifx_Cfg_Ssw.c index 42f8cff1384..bdf0595a919 100644 --- a/arch/tricore/src/illd/tc4xx/Configurations/Ifx_Cfg_Ssw.c +++ b/arch/tricore/src/illd/tc4xx/Configurations/Ifx_Cfg_Ssw.c @@ -119,7 +119,7 @@ void Ifx_Ssw_PowerOnCrystalOsc(void) void Ifx_Ssw_PllInit(void) { - if (IfxClock_init(&IfxClock_defaultClockConfig) != 0U) + if (tricore_clockconfig() != 0) { __debug(); } diff --git a/arch/tricore/src/illd/tc4xx/Configurations/Ifx_Cfg_Ssw.h b/arch/tricore/src/illd/tc4xx/Configurations/Ifx_Cfg_Ssw.h index e26e3f85b88..807fa7a8404 100644 --- a/arch/tricore/src/illd/tc4xx/Configurations/Ifx_Cfg_Ssw.h +++ b/arch/tricore/src/illd/tc4xx/Configurations/Ifx_Cfg_Ssw.h @@ -78,4 +78,12 @@ #define IFX_CFG_SSW_ENABLE_KEYOFF_MBIST_DSPRS_DMARAM (0U) #endif +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#if IFX_CFG_SSW_ENABLE_PLL_INIT == 1U +int tricore_clockconfig(void); +#endif + #endif /* __ARCH_TRICORE_SRC_ILLD_TC4XX_CONFIGURATIONS_IFX_CFG_SSW_H */ diff --git a/arch/tricore/src/tc4xx/CMakeLists.txt b/arch/tricore/src/tc4xx/CMakeLists.txt index 3ea36ddea2c..204224f0811 100644 --- a/arch/tricore/src/tc4xx/CMakeLists.txt +++ b/arch/tricore/src/tc4xx/CMakeLists.txt @@ -20,7 +20,7 @@ # # ############################################################################## -set(SRCS tc4xx_timerisr.c tc4xx_serial.c tc4xx_wdt.c) +set(SRCS tc4xx_timerisr.c tc4xx_serial.c tc4xx_wdt.c tc4xx_clockconfig.c) target_sources(arch PRIVATE ${SRCS}) diff --git a/arch/tricore/src/tc4xx/Make.defs b/arch/tricore/src/tc4xx/Make.defs index 7ca2ce0a8ac..ae07146faf4 100644 --- a/arch/tricore/src/tc4xx/Make.defs +++ b/arch/tricore/src/tc4xx/Make.defs @@ -23,6 +23,7 @@ CHIP_CSRCS += tc4xx_timerisr.c CHIP_CSRCS += tc4xx_serial.c CHIP_CSRCS += tc4xx_wdt.c +CHIP_CSRCS += tc4xx_clockconfig.c VPATH += tc4xx diff --git a/arch/tricore/src/tc4xx/hardware/tc4xx_clock.h b/arch/tricore/src/tc4xx/hardware/tc4xx_clock.h new file mode 100644 index 00000000000..ae75ed52aea --- /dev/null +++ b/arch/tricore/src/tc4xx/hardware/tc4xx_clock.h @@ -0,0 +1,99 @@ +/**************************************************************************** + * arch/tricore/src/tc4xx/hardware/tc4xx_clock.h + * + * 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. + * + ****************************************************************************/ + +#ifndef __ARCH_TRICORE_SRC_TC4XX_HARDWARE_TC4XX_CLOCK_H +#define __ARCH_TRICORE_SRC_TC4XX_HARDWARE_TC4XX_CLOCK_H + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Clock module registers */ + +#define TC4XX_CLOCK_OSCCON 0xf0064100 +#define TC4XX_CLOCK_RAMPCON0 0xf0064200 +#define TC4XX_CLOCK_RAMPSTAT 0xf006420c +#define TC4XX_CLOCK_SYSPLLCON0 0xf0064300 +#define TC4XX_CLOCK_SYSPLLCON1 0xf0064304 +#define TC4XX_CLOCK_SYSPLLSTAT 0xf006430c +#define TC4XX_CLOCK_PERPLLCON0 0xf0064380 +#define TC4XX_CLOCK_PERPLLCON1 0xf0064384 +#define TC4XX_CLOCK_PERPLLSTAT 0xf006438c +#define TC4XX_CLOCK_CCUCON 0xf0064400 +#define TC4XX_CLOCK_CCUSTAT 0xf0064404 +#define TC4XX_CLOCK_SYSCCUCON0 0xf0064410 +#define TC4XX_CLOCK_SYSCCUCON1 0xf0064414 +#define TC4XX_CLOCK_PERCCUCON0 0xf0064420 +#define TC4XX_CLOCK_PERCCUCON1 0xf0064424 +#define TC4XX_CLOCK_OSCMON1 0xf0064508 +#define TC4XX_CLOCK_OSCSTAT 0xf006450c +#define TC4XX_CLOCK_RAMPMON 0xf0064510 + +#define TC4XX_OSCCON_MODE_SHIFT 8 +#define TC4XX_OSCCON_MODE_MASK (0x7 << TC4XX_OSCCON_MODE_SHIFT) +#define TC4XX_OSCCON_INSEL_SHIFT 24 +#define TC4XX_OSCCON_INSEL_MASK (0x3 << TC4XX_OSCCON_INSEL_SHIFT) + +#define TC4XX_OSCMON1_OSCVAL_MASK (0x7f << 0) +#define TC4XX_OSCMON1_SMONEN (1 << 8) +#define TC4XX_OSCMON1_PMONEN (1 << 24) +#define TC4XX_OSCSTAT_WDS_MASK ((0x3 << 0) | (0x3 << 16)) + +#define TC4XX_RAMPCON0_UFL_MASK (0x3ff << 0) +#define TC4XX_RAMPCON0_CMD_SHIFT 16 +#define TC4XX_RAMPCON0_CMD_MASK (0x7 << TC4XX_RAMPCON0_CMD_SHIFT) +#define TC4XX_RAMPCON0_PWR (1 << 19) +#define TC4XX_RAMPSTAT_FSTAT_SHIFT 16 +#define TC4XX_RAMPSTAT_FSTAT_MASK (0x3 << TC4XX_RAMPSTAT_FSTAT_SHIFT) +#define TC4XX_RAMPSTAT_ACTIVE (1 << 23) +#define TC4XX_RAMPSTAT_FLLLOCK (1 << 24) +#define TC4XX_RAMPMON_FLLLDEN (1 << 28) + +#define TC4XX_PLLCON0_PLLPWR (1 << 0) +#define TC4XX_PLLCON0_NDIV_SHIFT 8 +#define TC4XX_PLLCON0_NDIV_MASK (0x7f << TC4XX_PLLCON0_NDIV_SHIFT) +#define TC4XX_PLLCON0_PDIV_SHIFT 16 +#define TC4XX_PLLCON0_PDIV_MASK (0x7 << TC4XX_PLLCON0_PDIV_SHIFT) +#define TC4XX_PLLSTAT_PWRSTAT (1 << 0) +#define TC4XX_PLLSTAT_PLLLOCK (1 << 1) + +#define TC4XX_PLLCON1_K2DIV_MASK (0xf << 0) +#define TC4XX_PLLCON1_K2PRE_SHIFT 4 +#define TC4XX_PLLCON1_K2PRE_MASK (0x3 << TC4XX_PLLCON1_K2PRE_SHIFT) +#define TC4XX_PLLCON1_K3DIV_SHIFT 8 +#define TC4XX_PLLCON1_K3DIV_MASK (0x7 << TC4XX_PLLCON1_K3DIV_SHIFT) +#define TC4XX_PLLCON1_K3PRE_SHIFT 12 +#define TC4XX_PLLCON1_K3PRE_MASK (0xf << TC4XX_PLLCON1_K3PRE_SHIFT) +#define TC4XX_PLLCON1_K4DIV_SHIFT 16 /* PERPLLCON1 only */ +#define TC4XX_PLLCON1_K4DIV_MASK (0x7 << TC4XX_PLLCON1_K4DIV_SHIFT) +#define TC4XX_PLLCON1_K4PRE_SHIFT 20 /* PERPLLCON1 only */ +#define TC4XX_PLLCON1_K4PRE_MASK (0x3 << TC4XX_PLLCON1_K4PRE_SHIFT) + +#define TC4XX_CCUSTAT_LCK (1 << 31) +#define TC4XX_CCU_CLKSELS_MASK (0x3 << 0) /* CCUCON and CCUSTAT */ +#define TC4XX_CCU_CLKSELS_PLL 0 +#define TC4XX_CCU_CLKSELS_BACK 1 +#define TC4XX_CCU_CLKSELS_RAMP 2 +#define TC4XX_CCU_CLKSELP (1 << 16) /* Set: backup clock */ +#define TC4XX_CCUCON_UP (1 << 30) /* SYSCCUCON0 and 1 */ + +#endif /* __ARCH_TRICORE_SRC_TC4XX_HARDWARE_TC4XX_CLOCK_H */ diff --git a/arch/tricore/src/tc4xx/hardware/tc4xx_dmu.h b/arch/tricore/src/tc4xx/hardware/tc4xx_dmu.h new file mode 100644 index 00000000000..565ffcc3afc --- /dev/null +++ b/arch/tricore/src/tc4xx/hardware/tc4xx_dmu.h @@ -0,0 +1,37 @@ +/**************************************************************************** + * arch/tricore/src/tc4xx/hardware/tc4xx_dmu.h + * + * 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. + * + ****************************************************************************/ + +#ifndef __ARCH_TRICORE_SRC_TC4XX_HARDWARE_TC4XX_DMU_H +#define __ARCH_TRICORE_SRC_TC4XX_HARDWARE_TC4XX_DMU_H + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Data memory unit (DMU) flash wait state registers */ + +#define TC4XX_DMU_HOST_DFWAIT 0xf8040100 +#define TC4XX_DMU_HOST_PFWAIT 0xf804011c + +#define TC4XX_DMU_RFLASH_MASK 0x1f + +#endif /* __ARCH_TRICORE_SRC_TC4XX_HARDWARE_TC4XX_DMU_H */ diff --git a/arch/tricore/src/tc4xx/hardware/tc4xx_smu.h b/arch/tricore/src/tc4xx/hardware/tc4xx_smu.h new file mode 100644 index 00000000000..5dbd50df3d9 --- /dev/null +++ b/arch/tricore/src/tc4xx/hardware/tc4xx_smu.h @@ -0,0 +1,40 @@ +/**************************************************************************** + * arch/tricore/src/tc4xx/hardware/tc4xx_smu.h + * + * 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. + * + ****************************************************************************/ + +#ifndef __ARCH_TRICORE_SRC_TC4XX_HARDWARE_TC4XX_SMU_H +#define __ARCH_TRICORE_SRC_TC4XX_HARDWARE_TC4XX_SMU_H + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Safety management unit (SMU_SAFE0) registers */ + +#define TC4XX_SMU_SAFE0_CMD 0xf0029000 +#define TC4XX_SMU_SAFE0_KEYS 0xf002901c +#define TC4XX_SMU_SAFE0_AGSF8_CON(n) (0xf00291a0 + (n) * 4) +#define TC4XX_SMU_SAFE0_AGSF8_STS 0xf00291b8 + +#define TC4XX_SMU_KEYS_CFG 0xbc +#define TC4XX_SMU_CMD_ALARM_CLR 5 + +#endif /* __ARCH_TRICORE_SRC_TC4XX_HARDWARE_TC4XX_SMU_H */ diff --git a/arch/tricore/src/tc4xx/tc4xx_clockconfig.c b/arch/tricore/src/tc4xx/tc4xx_clockconfig.c new file mode 100644 index 00000000000..1d153134e37 --- /dev/null +++ b/arch/tricore/src/tc4xx/tc4xx_clockconfig.c @@ -0,0 +1,497 @@ +/**************************************************************************** + * arch/tricore/src/tc4xx/tc4xx_clockconfig.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 "tricore_internal.h" +#include "hardware/tc4xx_clock.h" +#include "hardware/tc4xx_dmu.h" +#include "hardware/tc4xx_smu.h" +#include "tc4xx_wdt.h" +#include "tricore_stm.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* SMU alarms 272 and 273: system and peripheral PLL crystal clock out of + * range, in alarm group 8 of SMU_SAFE0 + */ + +#define TC4XX_SMU_PLL_ALARMS ((1 << 16) | (1 << 17)) + +/* Flash wait states for the final clocks */ + +#define TC4XX_DFLASH_WAIT 5 +#define TC4XX_PFLASH_WAIT 8 + +/* CPU0 STM timer, 100 MHz while the clocks are set up */ + +#define TC4XX_STM0_ABS (TRICORE_STM0_BASE + TRICORE_STM_ABS_OFFSET) +#define TC4XX_STM_TICKS_PER_US 100 + +/* Loop counts before a wait gives up */ + +#define TC4XX_CCU_LCK_TIMEOUT 0x1000 +#define TC4XX_PLL_LCK_TIMEOUT 0x2710 +#define TC4XX_PLL_LOCK_TIMEOUT 0xf000 +#define TC4XX_RAMP_TIMEOUT 0x1fffff + +/* Clock setup: 25 MHz crystal, system PLL 500 MHz, peripheral PLL 160 and + * 200 MHz, as the iLLD IfxClock default configuration for TC4Dx. + */ + +#define TC4XX_XTAL_MHZ 25 +#define TC4XX_PLL_INSEL_OSC0 1 +#define TC4XX_OSCCON_MASK (0xff020) /* Bits 5 and 12 to 19 */ +#define TC4XX_OSCCON_VALUE (1 << 5) +#define TC4XX_RAMP_UFL 400 + +#define TC4XX_SYSPLL_PDIV (1 - 1) +#define TC4XX_SYSPLL_NDIV (40 - 1) +#define TC4XX_SYSPLL_K2 (2 - 1) +#define TC4XX_SYSPLL_K2PRE (1 - 1) +#define TC4XX_SYSPLL_K3 (2 - 1) +#define TC4XX_SYSPLL_K3PRE (2 - 1) +#define TC4XX_SYSPLLCON1_INITIAL 0x0000a111 + +#define TC4XX_PERPLL_PDIV (1 - 1) +#define TC4XX_PERPLL_NDIV (32 - 1) +#define TC4XX_PERPLL_K2 (5 - 1) +#define TC4XX_PERPLL_K2PRE 0 +#define TC4XX_PERPLL_K3 (2 - 1) +#define TC4XX_PERPLL_K3PRE 10 +#define TC4XX_PERPLL_K4 (2 - 1) +#define TC4XX_PERPLL_K4PRE 1 +#define TC4XX_PERPLLCON1_PREDIV 0x0010a010 +#define TC4XX_PERPLLCON1_DIV 0x00010101 + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct tc4xx_ccucon_s +{ + uintptr_t reg; + uint32_t value; + uint32_t mask; + uint32_t set; +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Clock distribution, written before the PLLs are set up */ + +static const struct tc4xx_ccucon_s g_tc4xx_ccucon[] = +{ + { TC4XX_CLOCK_SYSCCUCON0, 0x21051225, 0x2f0fffff, TC4XX_CCUCON_UP }, + { TC4XX_CLOCK_SYSCCUCON1, 0x02222002, 0x0ffff00f, TC4XX_CCUCON_UP }, + { TC4XX_CLOCK_PERCCUCON0, 0x12212111, 0xff3f3f3f, 0 }, + { TC4XX_CLOCK_PERCCUCON1, 0x1f115111, 0x1ff1ffff, 0 }, +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tc4xx_wait + * + * Description: + * Wait until all bits of mask in the register equal the bits in value. + * Return 1 if the wait gave up after count loops, otherwise 0. + * + ****************************************************************************/ + +static int tc4xx_wait(uintptr_t reg, uint32_t mask, uint32_t value, + uint32_t count) +{ + while ((getreg32(reg) & mask) != value) + { + if (--count == 0) + { + return 1; + } + } + + return 0; +} + +static int tc4xx_wait_unlocked(uint32_t count) +{ + return tc4xx_wait(TC4XX_CLOCK_CCUSTAT, TC4XX_CCUSTAT_LCK, 0, count); +} + +static void tc4xx_modify(uintptr_t reg, uint32_t clear, uint32_t set) +{ + putreg32((getreg32(reg) & ~clear) | set, reg); +} + +/**************************************************************************** + * Name: tc4xx_delay + * + * Description: + * Wait for us microseconds on the STM, servicing the watchdogs. + * + ****************************************************************************/ + +static void tc4xx_delay(uint32_t us) +{ + uint32_t start = getreg32(TC4XX_STM0_ABS); + + while (getreg32(TC4XX_STM0_ABS) - start < us * TC4XX_STM_TICKS_PER_US) + { + tc4xx_wdt_service(); + } +} + +static void tc4xx_smu_pll_alarms(bool enable) +{ + int i; + + if (enable) + { + putreg32(TC4XX_SMU_CMD_ALARM_CLR, TC4XX_SMU_SAFE0_CMD); + putreg32(1 << 16, TC4XX_SMU_SAFE0_AGSF8_STS); + putreg32(TC4XX_SMU_CMD_ALARM_CLR, TC4XX_SMU_SAFE0_CMD); + putreg32(1 << 17, TC4XX_SMU_SAFE0_AGSF8_STS); + return; + } + + /* Internal alarm action disabled */ + + putreg32(TC4XX_SMU_KEYS_CFG, TC4XX_SMU_SAFE0_KEYS); + for (i = 0; i < 4; i++) + { + tc4xx_modify(TC4XX_SMU_SAFE0_AGSF8_CON(i), TC4XX_SMU_PLL_ALARMS, 0); + } + + putreg32(0, TC4XX_SMU_SAFE0_KEYS); +} + +/**************************************************************************** + * Name: tc4xx_clock_distribute + ****************************************************************************/ + +static int tc4xx_clock_distribute(void) +{ + const struct tc4xx_ccucon_s *cfg; + int ret = 0; + int i; + + tc4xx_modify(TC4XX_DMU_HOST_DFWAIT, TC4XX_DMU_RFLASH_MASK, + TC4XX_DFLASH_WAIT); + tc4xx_modify(TC4XX_DMU_HOST_PFWAIT, TC4XX_DMU_RFLASH_MASK, + TC4XX_PFLASH_WAIT); + + for (i = 0; i < sizeof(g_tc4xx_ccucon) / sizeof(g_tc4xx_ccucon[0]); i++) + { + uint32_t regval; + + cfg = &g_tc4xx_ccucon[i]; + regval = (getreg32(cfg->reg) & ~cfg->mask) | + (cfg->value & cfg->mask) | cfg->set; + ret |= tc4xx_wait_unlocked(TC4XX_CCU_LCK_TIMEOUT); + putreg32(regval, cfg->reg); + } + + return ret; +} + +/**************************************************************************** + * Name: tc4xx_osc_init + * + * Description: + * Power up the crystal and the ramp oscillator, select the crystal as + * PLL input and wait for it to be stable. + * + ****************************************************************************/ + +static int tc4xx_osc_init(void) +{ + uint32_t mode; + uint32_t osccon; + uint32_t count; + int ret; + + ret = tc4xx_wait_unlocked(TC4XX_CCU_LCK_TIMEOUT); + + tc4xx_smu_pll_alarms(false); + + mode = (getreg32(TC4XX_CLOCK_OSCCON) & TC4XX_OSCCON_MODE_MASK) >> + TC4XX_OSCCON_MODE_SHIFT; + if (mode == 7) + { + tc4xx_modify(TC4XX_CLOCK_OSCCON, TC4XX_OSCCON_MODE_MASK, + 3 << TC4XX_OSCCON_MODE_SHIFT); + tc4xx_modify(TC4XX_CLOCK_OSCCON, TC4XX_OSCCON_MODE_MASK, 0); + } + else if (mode != 2 && mode != 0) + { + osccon = getreg32(TC4XX_CLOCK_OSCCON) & ~TC4XX_OSCCON_MODE_MASK; + tc4xx_wait_unlocked(TC4XX_CCU_LCK_TIMEOUT); + putreg32(osccon, TC4XX_CLOCK_OSCCON); + } + + if ((getreg32(TC4XX_CLOCK_RAMPSTAT) & TC4XX_RAMPSTAT_ACTIVE) == 0) + { + tc4xx_wait_unlocked(TC4XX_CCU_LCK_TIMEOUT); + tc4xx_modify(TC4XX_CLOCK_RAMPCON0, 0, TC4XX_RAMPCON0_PWR); + + count = TC4XX_CCU_LCK_TIMEOUT; + while ((getreg32(TC4XX_CLOCK_RAMPSTAT) & TC4XX_RAMPSTAT_ACTIVE) == 0 && + --count != 0); + } + + tc4xx_modify(TC4XX_CLOCK_RAMPMON, 0, TC4XX_RAMPMON_FLLLDEN); + + osccon = getreg32(TC4XX_CLOCK_OSCCON) & ~TC4XX_OSCCON_MASK; + tc4xx_modify(TC4XX_CLOCK_OSCMON1, TC4XX_OSCMON1_OSCVAL_MASK, + (TC4XX_XTAL_MHZ - 15) | TC4XX_OSCMON1_SMONEN | + TC4XX_OSCMON1_PMONEN); + + ret |= tc4xx_wait_unlocked(TC4XX_CCU_LCK_TIMEOUT); + putreg32(osccon | TC4XX_OSCCON_VALUE, TC4XX_CLOCK_OSCCON); + ret |= tc4xx_wait_unlocked(TC4XX_CCU_LCK_TIMEOUT); + tc4xx_modify(TC4XX_CLOCK_OSCCON, TC4XX_OSCCON_INSEL_MASK, + TC4XX_PLL_INSEL_OSC0 << TC4XX_OSCCON_INSEL_SHIFT); + + /* The crystal may need up to 3 ms to give a stable clock */ + + tc4xx_delay(3000); + if ((getreg32(TC4XX_CLOCK_OSCSTAT) & TC4XX_OSCSTAT_WDS_MASK) != 0) + { + ret = 1; + } + + return ret; +} + +/**************************************************************************** + * Name: tc4xx_pll_init + * + * Description: + * Set up one PLL: power it down, set the initial K dividers, power it up + * with P and N, wait for the lock and set the final K dividers. + * + * Returned Value: + * 0 on success, 1 if a wait gave up, -1 if the PLL cannot be powered + * down because the clock runs from it. + * + ****************************************************************************/ + +static int tc4xx_pll_init(uintptr_t con0, uintptr_t con1, uintptr_t stat, + uint32_t pdiv, uint32_t ndiv, uint32_t prediv, + uint32_t premask, uint32_t div, uint32_t divmask, + bool sys) +{ + uint32_t regval; + int ret = 0; + + if ((getreg32(stat) & TC4XX_PLLSTAT_PWRSTAT) != 0) + { + regval = getreg32(TC4XX_CLOCK_CCUSTAT); + if (sys ? ((regval & TC4XX_CCU_CLKSELS_MASK) == + TC4XX_CCU_CLKSELS_PLL || + (regval & TC4XX_CCU_CLKSELS_MASK) == 3) : + (regval & TC4XX_CCU_CLKSELP) == 0) + { + return -1; + } + + tc4xx_modify(con0, TC4XX_PLLCON0_PLLPWR, 0); + } + + ret |= tc4xx_wait_unlocked(TC4XX_PLL_LCK_TIMEOUT); + if (sys) + { + putreg32(TC4XX_SYSPLLCON1_INITIAL, con1); + } + else + { + putreg32(TC4XX_PERPLLCON1_PREDIV, con1); + ret |= tc4xx_wait_unlocked(TC4XX_PLL_LCK_TIMEOUT); + tc4xx_modify(con1, 0, TC4XX_PERPLLCON1_DIV); + ret |= tc4xx_wait_unlocked(TC4XX_PLL_LCK_TIMEOUT); + } + + /* The peripheral PLL K pre-dividers are set from the value written + * above, as IfxClock does + */ + + regval = getreg32(con1); + + tc4xx_modify(con0, TC4XX_PLLCON0_PDIV_MASK | TC4XX_PLLCON0_NDIV_MASK, + (pdiv << TC4XX_PLLCON0_PDIV_SHIFT) | + (ndiv << TC4XX_PLLCON0_NDIV_SHIFT) | TC4XX_PLLCON0_PLLPWR); + ret |= tc4xx_wait_unlocked(TC4XX_PLL_LCK_TIMEOUT); + ret |= tc4xx_wait(stat, TC4XX_PLLSTAT_PWRSTAT, TC4XX_PLLSTAT_PWRSTAT, + TC4XX_PLL_LCK_TIMEOUT); + + /* Wait 1 ms to avoid jitter */ + + tc4xx_delay(1000); + ret |= tc4xx_wait(stat, TC4XX_PLLSTAT_PLLLOCK, TC4XX_PLLSTAT_PLLLOCK, + TC4XX_PLL_LCK_TIMEOUT); + + if (sys) + { + regval = getreg32(con1); + } + + putreg32((regval & ~premask) | prediv, con1); + tc4xx_delay(10); + ret |= tc4xx_wait_unlocked(TC4XX_PLL_LCK_TIMEOUT); + + tc4xx_modify(con1, divmask, div); + ret |= tc4xx_wait_unlocked(TC4XX_PLL_LCK_TIMEOUT); + + return ret; +} + +/**************************************************************************** + * Name: tc4xx_clock_switch + * + * Description: + * Run the system clock from the ramp oscillator, ramp it up to the PLL + * frequency and switch both clocks to the PLLs. + * + ****************************************************************************/ + +static int tc4xx_clock_switch(void) +{ + uint32_t regval; + int ret; + + tc4xx_smu_pll_alarms(true); + + regval = getreg32(TC4XX_CLOCK_CCUCON); + regval &= ~TC4XX_CCU_CLKSELS_MASK; + regval |= TC4XX_CCU_CLKSELS_RAMP | TC4XX_CCU_CLKSELP; + ret = tc4xx_wait_unlocked(TC4XX_CCU_LCK_TIMEOUT); + putreg32(regval, TC4XX_CLOCK_CCUCON); + + ret |= tc4xx_wait(TC4XX_CLOCK_RAMPSTAT, TC4XX_RAMPSTAT_FLLLOCK, + TC4XX_RAMPSTAT_FLLLOCK, TC4XX_CCU_LCK_TIMEOUT); + + regval = getreg32(TC4XX_CLOCK_RAMPCON0); + regval &= ~(TC4XX_RAMPCON0_UFL_MASK | TC4XX_RAMPCON0_CMD_MASK); + regval |= TC4XX_RAMP_UFL | (1 << TC4XX_RAMPCON0_CMD_SHIFT); + ret |= tc4xx_wait_unlocked(TC4XX_CCU_LCK_TIMEOUT); + putreg32(regval, TC4XX_CLOCK_RAMPCON0); + + /* Ramp frequency at the top */ + + ret |= tc4xx_wait(TC4XX_CLOCK_RAMPSTAT, TC4XX_RAMPSTAT_FSTAT_MASK, + 2 << TC4XX_RAMPSTAT_FSTAT_SHIFT, TC4XX_RAMP_TIMEOUT); + ret |= tc4xx_wait(TC4XX_CLOCK_SYSPLLSTAT, TC4XX_PLLSTAT_PLLLOCK, + TC4XX_PLLSTAT_PLLLOCK, TC4XX_PLL_LOCK_TIMEOUT); + ret |= tc4xx_wait(TC4XX_CLOCK_PERPLLSTAT, TC4XX_PLLSTAT_PLLLOCK, + TC4XX_PLLSTAT_PLLLOCK, TC4XX_PLL_LOCK_TIMEOUT); + + regval = getreg32(TC4XX_CLOCK_CCUCON); + regval &= ~(TC4XX_CCU_CLKSELS_MASK | TC4XX_CCU_CLKSELP); + regval |= TC4XX_CCU_CLKSELS_PLL; + ret |= tc4xx_wait_unlocked(TC4XX_CCU_LCK_TIMEOUT); + putreg32(regval, TC4XX_CLOCK_CCUCON); + + return ret; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tricore_clockconfig + * + * Description: + * Set up the flash wait states, the clock distribution, the oscillators + * and the PLLs. Called by the startup code before the C runtime is + * initialized, so it may only use constant data. + * + * Returned Value: + * 0 on success, 1 if a wait for the hardware gave up. + * + ****************************************************************************/ + +int tricore_clockconfig(void) +{ + int ret; + int pll; + + ret = tc4xx_clock_distribute(); + if (ret != 0) + { + return ret; + } + + ret |= tc4xx_osc_init(); + + pll = tc4xx_pll_init(TC4XX_CLOCK_SYSPLLCON0, TC4XX_CLOCK_SYSPLLCON1, + TC4XX_CLOCK_SYSPLLSTAT, TC4XX_SYSPLL_PDIV, + TC4XX_SYSPLL_NDIV, + (TC4XX_SYSPLL_K2PRE << TC4XX_PLLCON1_K2PRE_SHIFT) | + (TC4XX_SYSPLL_K3PRE << TC4XX_PLLCON1_K3PRE_SHIFT), + TC4XX_PLLCON1_K2PRE_MASK | TC4XX_PLLCON1_K3PRE_MASK, + TC4XX_SYSPLL_K2 | + (TC4XX_SYSPLL_K3 << TC4XX_PLLCON1_K3DIV_SHIFT), + TC4XX_PLLCON1_K2DIV_MASK | TC4XX_PLLCON1_K3DIV_MASK, + true); + if (pll < 0) + { + return 1; + } + + ret |= pll; + pll = tc4xx_pll_init(TC4XX_CLOCK_PERPLLCON0, TC4XX_CLOCK_PERPLLCON1, + TC4XX_CLOCK_PERPLLSTAT, TC4XX_PERPLL_PDIV, + TC4XX_PERPLL_NDIV, + (TC4XX_PERPLL_K2PRE << TC4XX_PLLCON1_K2PRE_SHIFT) | + (TC4XX_PERPLL_K3PRE << TC4XX_PLLCON1_K3PRE_SHIFT) | + (TC4XX_PERPLL_K4PRE << TC4XX_PLLCON1_K4PRE_SHIFT), + TC4XX_PLLCON1_K2PRE_MASK | TC4XX_PLLCON1_K3PRE_MASK | + TC4XX_PLLCON1_K4PRE_MASK, + TC4XX_PERPLL_K2 | + (TC4XX_PERPLL_K3 << TC4XX_PLLCON1_K3DIV_SHIFT) | + (TC4XX_PERPLL_K4 << TC4XX_PLLCON1_K4DIV_SHIFT), + TC4XX_PLLCON1_K2DIV_MASK | TC4XX_PLLCON1_K3DIV_MASK | + TC4XX_PLLCON1_K4DIV_MASK, false); + if (pll < 0) + { + return 1; + } + + ret |= pll; + ret |= tc4xx_clock_switch(); + + return ret; +} diff --git a/arch/tricore/src/tc4xx/tc4xx_wdt.c b/arch/tricore/src/tc4xx/tc4xx_wdt.c index 87e1cefa534..1127c6cef4d 100644 --- a/arch/tricore/src/tc4xx/tc4xx_wdt.c +++ b/arch/tricore/src/tc4xx/tc4xx_wdt.c @@ -32,6 +32,7 @@ #include "tricore_internal.h" #include "hardware/tc4xx_wdt.h" +#include "tc4xx_wdt.h" /**************************************************************************** * Pre-processor Definitions @@ -72,10 +73,69 @@ static void tc4xx_wdt_disable(uintptr_t ctrla, uintptr_t ctrlb) putreg32(a | TC4XX_WTU_CTRLA_LCK, ctrla); } +/**************************************************************************** + * Name: tc4xx_wdt_password + ****************************************************************************/ + +static uint32_t tc4xx_wdt_password(uintptr_t ctrla) +{ + return ((getreg32(ctrla) & TC4XX_WTU_CTRLA_PW_MASK) >> + TC4XX_WTU_CTRLA_PW_SHIFT) ^ TC4XX_WTU_PW_INVERTED; +} + +/**************************************************************************** + * Name: tc4xx_wdt_service_one + * + * Description: + * Service a watchdog: unlock CTRLA with the password, with the inverted + * timer value if the timer check is enabled, then lock it again. + * + ****************************************************************************/ + +static void tc4xx_wdt_service_one(uintptr_t ctrla, uint32_t pw) +{ + uint32_t a = getreg32(ctrla); + + if ((a & TC4XX_WTU_CTRLA_LCK) != 0) + { + uint32_t stat = getreg32(ctrla + TC4XX_WTU_STAT_OFFSET); + + a &= ~(TC4XX_WTU_CTRLA_LCK | TC4XX_WTU_CTRLA_PW_MASK); + a |= pw << TC4XX_WTU_CTRLA_PW_SHIFT; + if ((stat & TC4XX_WTU_STAT_TC) != 0) + { + a &= ~(0xffff << TC4XX_WTU_CTRLA_TCVI_SHIFT); + a |= (~stat >> TC4XX_WTU_STAT_TIM_SHIFT) << + TC4XX_WTU_CTRLA_TCVI_SHIFT; + } + + putreg32(a, ctrla); + } + + putreg32(a | TC4XX_WTU_CTRLA_LCK, ctrla); +} + /**************************************************************************** * Public Functions ****************************************************************************/ +/**************************************************************************** + * Name: tc4xx_wdt_service + * + * Description: + * Service the CPU0 and the system watchdog, for the long waits of the + * clock setup, which runs before the watchdogs are disabled. + * + ****************************************************************************/ + +void tc4xx_wdt_service(void) +{ + tc4xx_wdt_service_one(TC4XX_WTU_WDTCPU_CTRLA(0), + tc4xx_wdt_password(TC4XX_WTU_WDTCPU_CTRLA(0))); + tc4xx_wdt_service_one(TC4XX_WTU_WDTSYS_CTRLA, + tc4xx_wdt_password(TC4XX_WTU_WDTSYS_CTRLA)); +} + /**************************************************************************** * Name: tricore_wdt_disable * diff --git a/arch/tricore/src/tc4xx/tc4xx_wdt.h b/arch/tricore/src/tc4xx/tc4xx_wdt.h new file mode 100644 index 00000000000..b258ee162e5 --- /dev/null +++ b/arch/tricore/src/tc4xx/tc4xx_wdt.h @@ -0,0 +1,32 @@ +/**************************************************************************** + * arch/tricore/src/tc4xx/tc4xx_wdt.h + * + * 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. + * + ****************************************************************************/ + +#ifndef __ARCH_TRICORE_SRC_TC4XX_TC4XX_WDT_H +#define __ARCH_TRICORE_SRC_TC4XX_TC4XX_WDT_H + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +void tc4xx_wdt_service(void); + +#endif /* __ARCH_TRICORE_SRC_TC4XX_TC4XX_WDT_H */
