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 * arg)31 static esp_err_t btbb_sleep_retention_init(void *arg)
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_BT_MAC_BB, SLEEP_RETENTION_MODULE_BT_BB);
39     ESP_RETURN_ON_ERROR(err, TAG, "failed to allocate memory for btbb retention");
40     ESP_LOGD(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     esp_err_t err = sleep_retention_module_free(SLEEP_RETENTION_MODULE_BT_BB);
47     if (err != ESP_OK) {
48         ESP_LOGW(TAG, "failed to destroy sleep retention linked list for btbb retention");
49     }
50     err = sleep_retention_module_deinit(SLEEP_RETENTION_MODULE_BT_BB);
51     if (err != ESP_OK) {
52         ESP_LOGW(TAG, "Modem BT BB retention callback unregister failed");
53     }
54 }
55 #endif // SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE
56 
57 
esp_btbb_enable(void)58 void esp_btbb_enable(void)
59 {
60     _lock_acquire(&s_btbb_access_lock);
61     if (s_btbb_access_ref == 0) {
62         bt_bb_v2_init_cmplx(BTBB_ENABLE_VERSION_PRINT);
63 #if SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE
64         sleep_retention_module_init_param_t init_param = {
65             .cbs     = { .create = { .handle = btbb_sleep_retention_init, .arg = NULL } },
66             .depends = BIT(SLEEP_RETENTION_MODULE_CLOCK_MODEM)
67         };
68         esp_err_t err = sleep_retention_module_init(SLEEP_RETENTION_MODULE_BT_BB, &init_param);
69         if (err == ESP_OK) {
70             err = sleep_retention_module_allocate(SLEEP_RETENTION_MODULE_BT_BB);
71             if (err != ESP_OK) {
72                 ESP_LOGW(TAG, "failed to allocate sleep retention linked list for btbb retention");
73             }
74         } else {
75             ESP_LOGW(TAG, "Modem BT BB retention callback register failed");
76         }
77 #endif // SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE
78     }
79     s_btbb_access_ref++;
80     _lock_release(&s_btbb_access_lock);
81 }
82 
esp_btbb_disable(void)83 void esp_btbb_disable(void)
84 {
85     _lock_acquire(&s_btbb_access_lock);
86     if (s_btbb_access_ref && (--s_btbb_access_ref == 0)) {
87 #if SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE
88         btbb_sleep_retention_deinit();
89 #endif // SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE
90     }
91     _lock_release(&s_btbb_access_lock);
92 }
93