periph_clk_gating: add reference counter

This commit is contained in:
morris 2020-04-10 16:09:07 +08:00
parent 99f0dc78c8
commit e0b9f7be6d
4 changed files with 23 additions and 2 deletions

View file

@ -28,6 +28,10 @@ extern "C" {
*
* Clock for the module will be ungated, and reset de-asserted.
*
* @note If periph_module_enable is called a number of times,
* periph_module_disable has to be called the same number of times
* in order to put the peripheral into disabled state.
*
* @return NULL
*
*/
@ -40,6 +44,10 @@ void periph_module_enable(periph_module_t periph);
*
* Clock for the module will be gated, reset asserted.
*
* @note If periph_module_enable is called a number of times,
* periph_module_disable has to be called the same number of times
* in order to put the peripheral into disabled state.
*
* @return NULL
*
*/

View file

@ -17,22 +17,33 @@
static portMUX_TYPE periph_spinlock = portMUX_INITIALIZER_UNLOCKED;
static uint8_t ref_counts[PERIPH_MODULE_MAX] = {0};
void periph_module_enable(periph_module_t periph)
{
assert(periph < PERIPH_MODULE_MAX);
portENTER_CRITICAL_SAFE(&periph_spinlock);
periph_ll_enable_clk_clear_rst(periph);
if (ref_counts[periph] == 0) {
periph_ll_enable_clk_clear_rst(periph);
}
ref_counts[periph]++;
portEXIT_CRITICAL_SAFE(&periph_spinlock);
}
void periph_module_disable(periph_module_t periph)
{
assert(periph < PERIPH_MODULE_MAX);
portENTER_CRITICAL_SAFE(&periph_spinlock);
periph_ll_disable_clk_set_rst(periph);
ref_counts[periph]--;
if (ref_counts[periph] == 0) {
periph_ll_disable_clk_set_rst(periph);
}
portEXIT_CRITICAL_SAFE(&periph_spinlock);
}
void periph_module_reset(periph_module_t periph)
{
assert(periph < PERIPH_MODULE_MAX);
portENTER_CRITICAL_SAFE(&periph_spinlock);
periph_ll_reset(periph);
portEXIT_CRITICAL_SAFE(&periph_spinlock);

View file

@ -55,6 +55,7 @@ typedef enum {
PERIPH_AES_MODULE,
PERIPH_SHA_MODULE,
PERIPH_RSA_MODULE,
PERIPH_MODULE_MAX
} periph_module_t;
#ifdef __cplusplus

View file

@ -54,6 +54,7 @@ typedef enum {
PERIPH_CRYPTO_DMA_MODULE, //this DMA is shared between AES and SHA
PERIPH_AES_DMA_MODULE,
PERIPH_SHA_DMA_MODULE,
PERIPH_MODULE_MAX
} periph_module_t;
typedef enum {