1 /*
2  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdint.h>
8 #include "esp_check.h"
9 #include "esp_log.h"
10 #include "freertos/FreeRTOS.h"
11 #include "esp_private/btbb.h"
12 
13 #define BTBB_ENABLE_VERSION_PRINT 1
14 
15 static _lock_t s_btbb_access_lock;
16 /* Reference count of enabling BT BB */
17 static uint8_t s_btbb_access_ref = 0;
18 
19 
20 #if SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE
21 #include "esp_private/sleep_retention.h"
22 #include "btbb_retention_reg.h"
23 static const char* TAG = "btbb_init";
24 
25 #if SOC_PM_RETENTION_HAS_CLOCK_BUG
26 #define BTBB_LINK_OWNER  ENTRY(3)
27 #else
28 #define BTBB_LINK_OWNER  ENTRY(0) | ENTRY(2)
29 #endif // SOC_PM_RETENTION_HAS_CLOCK_BUG
30 
btbb_sleep_retention_init(void)31 static esp_err_t btbb_sleep_retention_init(void)
32 {
33     const static sleep_retention_entries_config_t btbb_regs_retention[] = {
34         [0] = { .config = REGDMA_LINK_CONTINUOUS_INIT(REGDMA_MODEM_BT_BB_LINK(0x00), BB_PART_0_ADDR, BB_PART_0_ADDR, BB_PART_0_SIZE, 0, 0), .owner = BTBB_LINK_OWNER },
35         [1] = { .config = REGDMA_LINK_CONTINUOUS_INIT(REGDMA_MODEM_BT_BB_LINK(0x01), BB_PART_1_ADDR, BB_PART_1_ADDR, BB_PART_1_SIZE, 0, 0), .owner = BTBB_LINK_OWNER },
36         [2] = { .config = REGDMA_LINK_CONTINUOUS_INIT(REGDMA_MODEM_BT_BB_LINK(0x02), BB_PART_2_ADDR, BB_PART_2_ADDR, BB_PART_2_SIZE, 0, 0), .owner = BTBB_LINK_OWNER }
37     };
38     esp_err_t err = sleep_retention_entries_create(btbb_regs_retention, ARRAY_SIZE(btbb_regs_retention), REGDMA_LINK_PRI_5, SLEEP_RETENTION_MODULE_BT_BB);
39     ESP_RETURN_ON_ERROR(err, TAG, "failed to allocate memory for btbb retention");
40     ESP_LOGI(TAG, "btbb sleep retention initialization");
41     return ESP_OK;
42 }
43 
btbb_sleep_retention_deinit(void)44 static void btbb_sleep_retention_deinit(void)
45 {
46     sleep_retention_entries_destroy(SLEEP_RETENTION_MODULE_BT_BB);
47 }
48 #endif // SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE
49 
50 
esp_btbb_enable(void)51 void esp_btbb_enable(void)
52 {
53     _lock_acquire(&s_btbb_access_lock);
54     if (s_btbb_access_ref == 0) {
55         bt_bb_v2_init_cmplx(BTBB_ENABLE_VERSION_PRINT);
56 #if SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE
57         btbb_sleep_retention_init();
58 #endif // SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE
59     }
60     s_btbb_access_ref++;
61     _lock_release(&s_btbb_access_lock);
62 }
63 
esp_btbb_disable(void)64 void esp_btbb_disable(void)
65 {
66     _lock_acquire(&s_btbb_access_lock);
67     if (s_btbb_access_ref && (--s_btbb_access_ref == 0)) {
68 #if SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE
69         btbb_sleep_retention_deinit();
70 #endif // SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE
71     }
72     _lock_release(&s_btbb_access_lock);
73 }
74