stm32h7: add clock enable/disable callbacks

Change-Id: I4c135ba9cbed2c8e607c5f191bb9d82638f9b6b3
This commit is contained in:
Aidan MacDonald 2025-12-26 21:28:40 +00:00 committed by Solomon Peachy
parent 23448a13d1
commit ddb3bb354c
4 changed files with 103 additions and 1 deletions

View file

@ -2019,6 +2019,7 @@ target/arm/rk27xx/ihifi2/audio-ihifi800.c
target/arm/stm32/crt0-stm32h7.S
target/arm/stm32/vectors-stm32h7.S
target/arm/stm32/adc-stm32h7.c
target/arm/stm32/clock-stm32h7.c
target/arm/stm32/debug-stm32h7.c
target/arm/stm32/gpio-stm32h7.c
target/arm/stm32/i2c-stm32h7.c

View file

@ -0,0 +1,65 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2025 Aidan MacDonald
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "clock-stm32h7.h"
#include "mutex.h"
#include "panic.h"
#include <stdint.h>
struct stm_clock_state
{
struct mutex mutex;
uint8_t refcount[STM_NUM_CLOCKS];
};
static struct stm_clock_state stm_clocks;
void stm_clock_init(void)
{
mutex_init(&stm_clocks.mutex);
stm_target_clock_init();
}
void stm_clock_enable(enum stm_clock clock)
{
mutex_lock(&stm_clocks.mutex);
if (stm_clocks.refcount[clock] == UINT8_MAX)
panicf("%s: clock %d overflow", __func__, (int)clock);
if (stm_clocks.refcount[clock]++ == 0)
stm_target_clock_enable(clock, true);
mutex_unlock(&stm_clocks.mutex);
}
void stm_clock_disable(enum stm_clock clock)
{
mutex_lock(&stm_clocks.mutex);
if (stm_clocks.refcount[clock] == 0)
panicf("%s: clock %d underflow", __func__, (int)clock);
if (--stm_clocks.refcount[clock] == 0)
stm_target_clock_enable(clock, false);
mutex_unlock(&stm_clocks.mutex);
}

View file

@ -22,6 +22,18 @@
#define __CLOCK_STM32H7_H__
#include "system.h"
#include <stdbool.h>
enum stm_clock
{
STM_CLOCK_SPI1_KER,
STM_CLOCK_SPI2_KER,
STM_CLOCK_SPI3_KER,
STM_CLOCK_SPI4_KER,
STM_CLOCK_SPI5_KER,
STM_CLOCK_SPI6_KER,
STM_NUM_CLOCKS,
};
/*
* Implemented by the target to initialize all oscillators,
@ -30,4 +42,28 @@
*/
void stm_target_clock_init(void) INIT_ATTR;
/*
* Callback to be implemented by the target when the hardware
* clock needs to be turned on or off. Clocks are internally
* reference counted so only the first / last user will change
* the hardware state.
*
* Only clocks that are actually used need to be implemented,
* and unless otherwise noted it is allowed for enable/disable
* to be a no-op if the clock is always enabled.
*/
void stm_target_clock_enable(enum stm_clock clock, bool enable);
/*
* Called from system_init(). Sets up internal book-keeping
* and then calls stm_target_clock_init().
*/
void stm_clock_init(void) INIT_ATTR;
/*
* Enable or disable a clock. Not safe to call from an IRQ handler.
*/
void stm_clock_enable(enum stm_clock clock);
void stm_clock_disable(enum stm_clock clock);
#endif /* __CLOCK_STM32H7_H__ */

View file

@ -69,7 +69,7 @@ void system_init(void)
stm_enable_caches();
/* Initialize system clocks */
stm_target_clock_init();
stm_clock_init();
/* TODO: move this */
systick_init(1000/HZ);