2016-09-28 11:20:34 -04:00
|
|
|
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed 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.
|
|
|
|
#include <esp_types.h>
|
|
|
|
#include "esp_intr.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/semphr.h"
|
|
|
|
#include "freertos/xtensa_api.h"
|
|
|
|
#include "soc/dport_reg.h"
|
|
|
|
#include "driver/periph_ctrl.h"
|
|
|
|
|
|
|
|
static portMUX_TYPE periph_spinlock = portMUX_INITIALIZER_UNLOCKED;
|
|
|
|
|
2017-10-02 02:48:16 -04:00
|
|
|
/* Static functions to return register address & mask for clk_en / rst of each peripheral */
|
|
|
|
static uint32_t get_clk_en_mask(periph_module_t periph);
|
2018-10-29 11:55:02 -04:00
|
|
|
static uint32_t get_rst_en_mask(periph_module_t periph, bool enable);
|
2017-10-02 02:48:16 -04:00
|
|
|
static uint32_t get_clk_en_reg(periph_module_t periph);
|
|
|
|
static uint32_t get_rst_en_reg(periph_module_t periph);
|
|
|
|
|
2016-09-28 11:20:34 -04:00
|
|
|
void periph_module_enable(periph_module_t periph)
|
|
|
|
{
|
2019-03-25 06:32:15 -04:00
|
|
|
portENTER_CRITICAL_SAFE(&periph_spinlock);
|
2017-10-02 02:48:16 -04:00
|
|
|
DPORT_SET_PERI_REG_MASK(get_clk_en_reg(periph), get_clk_en_mask(periph));
|
2018-10-29 11:55:02 -04:00
|
|
|
DPORT_CLEAR_PERI_REG_MASK(get_rst_en_reg(periph), get_rst_en_mask(periph, true));
|
2019-03-25 06:32:15 -04:00
|
|
|
portEXIT_CRITICAL_SAFE(&periph_spinlock);
|
2017-10-02 02:48:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void periph_module_disable(periph_module_t periph)
|
|
|
|
{
|
2019-03-25 06:32:15 -04:00
|
|
|
portENTER_CRITICAL_SAFE(&periph_spinlock);
|
2017-10-02 02:48:16 -04:00
|
|
|
DPORT_CLEAR_PERI_REG_MASK(get_clk_en_reg(periph), get_clk_en_mask(periph));
|
2018-10-29 11:55:02 -04:00
|
|
|
DPORT_SET_PERI_REG_MASK(get_rst_en_reg(periph), get_rst_en_mask(periph, false));
|
2019-03-25 06:32:15 -04:00
|
|
|
portEXIT_CRITICAL_SAFE(&periph_spinlock);
|
2017-10-02 02:48:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void periph_module_reset(periph_module_t periph)
|
|
|
|
{
|
2019-03-25 06:32:15 -04:00
|
|
|
portENTER_CRITICAL_SAFE(&periph_spinlock);
|
2018-10-29 11:55:02 -04:00
|
|
|
DPORT_SET_PERI_REG_MASK(get_rst_en_reg(periph), get_rst_en_mask(periph, false));
|
|
|
|
DPORT_CLEAR_PERI_REG_MASK(get_rst_en_reg(periph), get_rst_en_mask(periph, false));
|
2019-03-25 06:32:15 -04:00
|
|
|
portEXIT_CRITICAL_SAFE(&periph_spinlock);
|
2017-10-02 02:48:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t get_clk_en_mask(periph_module_t periph)
|
|
|
|
{
|
2016-09-28 11:20:34 -04:00
|
|
|
switch(periph) {
|
2016-11-09 22:23:40 -05:00
|
|
|
case PERIPH_RMT_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_RMT_CLK_EN;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_LEDC_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_LEDC_CLK_EN;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_UART0_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_UART_CLK_EN;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_UART1_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_UART1_CLK_EN;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_UART2_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_UART2_CLK_EN;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_I2C0_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_I2C_EXT0_CLK_EN;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_I2C1_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_I2C_EXT1_CLK_EN;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_I2S0_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_I2S0_CLK_EN;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_I2S1_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_I2S1_CLK_EN;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_TIMG0_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_TIMERGROUP_CLK_EN;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_TIMG1_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_TIMERGROUP1_CLK_EN;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_PWM0_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_PWM0_CLK_EN;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_PWM1_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_PWM1_CLK_EN;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_PWM2_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_PWM2_CLK_EN;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_PWM3_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_PWM3_CLK_EN;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_UHCI0_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_UHCI0_CLK_EN;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_UHCI1_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_UHCI1_CLK_EN;
|
2016-11-21 05:17:07 -05:00
|
|
|
case PERIPH_PCNT_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_PCNT_CLK_EN;
|
2017-01-06 01:20:32 -05:00
|
|
|
case PERIPH_SPI_MODULE:
|
2018-04-24 04:38:46 -04:00
|
|
|
return DPORT_SPI01_CLK_EN;
|
2017-01-06 01:20:32 -05:00
|
|
|
case PERIPH_HSPI_MODULE:
|
2018-04-24 04:38:46 -04:00
|
|
|
return DPORT_SPI2_CLK_EN;
|
2017-01-06 01:20:32 -05:00
|
|
|
case PERIPH_VSPI_MODULE:
|
2018-04-24 04:38:46 -04:00
|
|
|
return DPORT_SPI3_CLK_EN;
|
2017-08-31 07:59:30 -04:00
|
|
|
case PERIPH_SPI_DMA_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_SPI_DMA_CLK_EN;
|
2017-09-04 06:12:15 -04:00
|
|
|
case PERIPH_SDMMC_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_WIFI_CLK_SDIO_HOST_EN;
|
2017-09-04 06:12:15 -04:00
|
|
|
case PERIPH_SDIO_SLAVE_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_WIFI_CLK_SDIOSLAVE_EN;
|
2017-09-04 06:12:15 -04:00
|
|
|
case PERIPH_CAN_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_CAN_CLK_EN;
|
2017-09-04 06:12:15 -04:00
|
|
|
case PERIPH_EMAC_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_WIFI_CLK_EMAC_EN;
|
2017-10-27 22:19:49 -04:00
|
|
|
case PERIPH_RNG_MODULE:
|
|
|
|
return DPORT_WIFI_CLK_RNG_EN;
|
|
|
|
case PERIPH_WIFI_MODULE:
|
|
|
|
return DPORT_WIFI_CLK_WIFI_EN_M;
|
2017-11-01 05:05:38 -04:00
|
|
|
case PERIPH_BT_MODULE:
|
|
|
|
return DPORT_WIFI_CLK_BT_EN_M;
|
|
|
|
case PERIPH_WIFI_BT_COMMON_MODULE:
|
|
|
|
return DPORT_WIFI_CLK_WIFI_BT_COMMON_M;
|
2018-04-08 07:19:47 -04:00
|
|
|
case PERIPH_BT_BASEBAND_MODULE:
|
|
|
|
return DPORT_BT_BASEBAND_EN;
|
|
|
|
case PERIPH_BT_LC_MODULE:
|
|
|
|
return DPORT_BT_LC_EN;
|
2018-10-29 11:55:02 -04:00
|
|
|
case PERIPH_AES_MODULE:
|
|
|
|
return DPORT_PERI_EN_AES;
|
|
|
|
case PERIPH_SHA_MODULE:
|
|
|
|
return DPORT_PERI_EN_SHA;
|
|
|
|
case PERIPH_RSA_MODULE:
|
|
|
|
return DPORT_PERI_EN_RSA;
|
2016-09-28 11:20:34 -04:00
|
|
|
default:
|
2017-10-02 02:48:16 -04:00
|
|
|
return 0;
|
2016-09-28 11:20:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-29 11:55:02 -04:00
|
|
|
static uint32_t get_rst_en_mask(periph_module_t periph, bool enable)
|
2016-09-28 11:20:34 -04:00
|
|
|
{
|
|
|
|
switch(periph) {
|
2016-11-23 06:07:30 -05:00
|
|
|
case PERIPH_RMT_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_RMT_RST;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_LEDC_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_LEDC_RST;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_UART0_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_UART_RST;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_UART1_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_UART1_RST;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_UART2_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_UART2_RST;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_I2C0_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_I2C_EXT0_RST;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_I2C1_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_I2C_EXT1_RST;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_I2S0_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_I2S0_RST;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_I2S1_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_I2S1_RST;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_TIMG0_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_TIMERGROUP_RST;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_TIMG1_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_TIMERGROUP1_RST;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_PWM0_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_PWM0_RST;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_PWM1_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_PWM1_RST;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_PWM2_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_PWM2_RST;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_PWM3_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_PWM3_RST;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_UHCI0_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_UHCI0_RST;
|
2016-09-28 11:20:34 -04:00
|
|
|
case PERIPH_UHCI1_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_UHCI1_RST;
|
2016-11-23 06:07:30 -05:00
|
|
|
case PERIPH_PCNT_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_PCNT_RST;
|
2017-01-06 01:20:32 -05:00
|
|
|
case PERIPH_SPI_MODULE:
|
2018-04-24 04:38:46 -04:00
|
|
|
return DPORT_SPI01_RST;
|
2017-01-06 01:20:32 -05:00
|
|
|
case PERIPH_HSPI_MODULE:
|
2018-04-24 04:38:46 -04:00
|
|
|
return DPORT_SPI2_RST;
|
2017-01-06 01:20:32 -05:00
|
|
|
case PERIPH_VSPI_MODULE:
|
2018-04-24 04:38:46 -04:00
|
|
|
return DPORT_SPI3_RST;
|
2017-08-31 07:59:30 -04:00
|
|
|
case PERIPH_SPI_DMA_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_SPI_DMA_RST;
|
2017-09-04 06:12:15 -04:00
|
|
|
case PERIPH_SDMMC_MODULE:
|
2017-10-27 22:19:49 -04:00
|
|
|
return DPORT_SDIO_HOST_RST;
|
2017-09-04 06:12:15 -04:00
|
|
|
case PERIPH_SDIO_SLAVE_MODULE:
|
2017-10-27 22:19:49 -04:00
|
|
|
return DPORT_SDIO_RST;
|
2017-09-04 06:12:15 -04:00
|
|
|
case PERIPH_CAN_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return DPORT_CAN_RST;
|
2017-09-04 06:12:15 -04:00
|
|
|
case PERIPH_EMAC_MODULE:
|
2017-10-27 22:19:49 -04:00
|
|
|
return DPORT_EMAC_RST;
|
2018-10-29 11:55:02 -04:00
|
|
|
case PERIPH_AES_MODULE:
|
|
|
|
if (enable == true) {
|
|
|
|
// Clear reset on digital signature & secure boot units, otherwise AES unit is held in reset also.
|
|
|
|
return (DPORT_PERI_EN_AES | DPORT_PERI_EN_DIGITAL_SIGNATURE | DPORT_PERI_EN_SECUREBOOT);
|
|
|
|
} else {
|
|
|
|
//Don't return other units to reset, as this pulls reset on RSA & SHA units, respectively.
|
|
|
|
return DPORT_PERI_EN_AES;
|
|
|
|
}
|
|
|
|
case PERIPH_SHA_MODULE:
|
|
|
|
if (enable == true) {
|
|
|
|
// Clear reset on secure boot, otherwise SHA is held in reset
|
|
|
|
return (DPORT_PERI_EN_SHA | DPORT_PERI_EN_SECUREBOOT);
|
|
|
|
} else {
|
|
|
|
// Don't assert reset on secure boot, otherwise AES is held in reset
|
|
|
|
return DPORT_PERI_EN_SHA;
|
|
|
|
}
|
|
|
|
case PERIPH_RSA_MODULE:
|
|
|
|
if (enable == true) {
|
|
|
|
// Also clear reset on digital signature, otherwise RSA is held in reset
|
|
|
|
return (DPORT_PERI_EN_RSA | DPORT_PERI_EN_DIGITAL_SIGNATURE);
|
|
|
|
} else {
|
|
|
|
// Don't reset digital signature unit, as this resets AES also
|
|
|
|
return DPORT_PERI_EN_RSA;
|
|
|
|
}
|
2017-10-27 22:19:49 -04:00
|
|
|
case PERIPH_WIFI_MODULE:
|
2017-11-01 05:05:38 -04:00
|
|
|
case PERIPH_BT_MODULE:
|
|
|
|
case PERIPH_WIFI_BT_COMMON_MODULE:
|
2018-04-08 07:19:47 -04:00
|
|
|
case PERIPH_BT_BASEBAND_MODULE:
|
|
|
|
case PERIPH_BT_LC_MODULE:
|
2017-11-01 05:05:38 -04:00
|
|
|
return 0;
|
2016-09-28 11:20:34 -04:00
|
|
|
default:
|
2017-10-02 02:48:16 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_wifi_clk_peripheral(periph_module_t periph)
|
|
|
|
{
|
|
|
|
/* A small subset of peripherals use WIFI_CLK_EN_REG and
|
|
|
|
CORE_RST_EN_REG for their clock & reset registers */
|
|
|
|
switch(periph) {
|
|
|
|
case PERIPH_SDMMC_MODULE:
|
|
|
|
case PERIPH_SDIO_SLAVE_MODULE:
|
|
|
|
case PERIPH_EMAC_MODULE:
|
2017-10-27 22:19:49 -04:00
|
|
|
case PERIPH_RNG_MODULE:
|
|
|
|
case PERIPH_WIFI_MODULE:
|
2017-11-01 05:05:38 -04:00
|
|
|
case PERIPH_BT_MODULE:
|
|
|
|
case PERIPH_WIFI_BT_COMMON_MODULE:
|
2018-04-08 07:19:47 -04:00
|
|
|
case PERIPH_BT_BASEBAND_MODULE:
|
|
|
|
case PERIPH_BT_LC_MODULE:
|
2017-10-02 02:48:16 -04:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
2016-09-28 11:20:34 -04:00
|
|
|
}
|
|
|
|
}
|
2017-10-02 02:48:16 -04:00
|
|
|
|
|
|
|
static uint32_t get_clk_en_reg(periph_module_t periph)
|
|
|
|
{
|
2018-10-29 11:55:02 -04:00
|
|
|
if (periph == PERIPH_AES_MODULE || periph == PERIPH_SHA_MODULE || periph == PERIPH_RSA_MODULE) {
|
|
|
|
return DPORT_PERI_CLK_EN_REG;
|
|
|
|
} else {
|
|
|
|
return is_wifi_clk_peripheral(periph) ? DPORT_WIFI_CLK_EN_REG : DPORT_PERIP_CLK_EN_REG;
|
|
|
|
}
|
2017-10-02 02:48:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t get_rst_en_reg(periph_module_t periph)
|
|
|
|
{
|
2018-10-29 11:55:02 -04:00
|
|
|
if (periph == PERIPH_AES_MODULE || periph == PERIPH_SHA_MODULE || periph == PERIPH_RSA_MODULE) {
|
|
|
|
return DPORT_PERI_RST_EN_REG;
|
|
|
|
} else {
|
|
|
|
return is_wifi_clk_peripheral(periph) ? DPORT_CORE_RST_EN_REG : DPORT_PERIP_RST_EN_REG;
|
|
|
|
}
|
2017-10-02 02:48:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|