ipmsg_btc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /*
  2. * Copyright (c) 2020 Actions Technology Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <errno.h>
  7. #include <string.h>
  8. #include <device.h>
  9. #include <soc.h>
  10. #include "soc_memctrl.h"
  11. #include <drivers/ipmsg.h>
  12. #include <sdfs.h>
  13. #include <board_cfg.h>
  14. #include <soc_atp.h>
  15. #include <sys/byteorder.h>
  16. #define DEBUGSEL (0x40068410)
  17. #define DEBUGOE0 (0x40068430)
  18. #define HOSC_CTL_HOSC_CAP(x) ((x) << HOSC_CTL_HOSC_CAP_SHIFT)
  19. /* LOSC_CTL */
  20. #define LOSC_CTL_LOSC_RDY BIT(15)
  21. /* CMU_MEMCLKEN1 */
  22. #define BT_ROM_RAM_CLK_EN BIT(16)
  23. /* CMU_MEMCLKSRC1 */
  24. #define BT_RAM_CLK_SRC BIT(16)
  25. /* CMU_S1CLKCTL */
  26. #define RC64M_S1EN BIT(2)
  27. #define HOSC_S1EN BIT(1)
  28. #define RC4M_S1EN BIT(0)
  29. /* CMU_S1BTCLKCTL */
  30. #define RC64M_S1BTEN BIT(2)
  31. #define HOSC_S1BTEN BIT(1)
  32. #define RC4M_S1BTEN BIT(0)
  33. /* CMU_DEVCLKEN1 */
  34. #define BTHUB_RC32KEN BIT(28)
  35. #define BTHUB_RC64MEN BIT(27)
  36. #define BTHUB_HOSCEN BIT(26)
  37. #define BTHUB_RC4MEN BIT(25)
  38. #define BTHUB_LOSCEN BIT(24)
  39. #define JTAG_CTL_BTSWEN BIT(20)
  40. /* DEBUGSEL */
  41. #define DBGSE_SHIFT 0
  42. #define DBGSE(x) ((x) << DBGSE_SHIFT)
  43. #define DBGSE_BT_CONTROLLER DBGSE(0x29)
  44. #if defined(CONFIG_BTC_FW_IN_NAND)
  45. #define BT_RF_FILE "/NAND:K/bt_rf.bin"
  46. #define BT_TABLE_FILE "/NAND:K/bttbl.bin"
  47. #define BT_PATCH_FILE "/NAND:K/bt_pth.bin"
  48. #define BT_FCC_FILE "/NAND:K/fcc.bin"
  49. #define BT_ROM_FILE "/NAND:K/bt_rom.bin"
  50. #elif defined(CONFIG_BTC_FW_IN_SD)
  51. #define BT_RF_FILE "/SD:K/bt_rf.bin"
  52. #define BT_TABLE_FILE "/SD:K/bttbl.bin"
  53. #define BT_PATCH_FILE "/SD:K/bt_pth.bin"
  54. #define BT_FCC_FILE "/SD:K/fcc.bin"
  55. #define BT_ROM_FILE "/SD:K/bt_rom.bin"
  56. #elif defined(CONFIG_BTC_FW_IN_NOR_EXT)
  57. #define BT_RF_FILE "/NOR:K/bt_rf.bin"
  58. #define BT_TABLE_FILE "/NOR:K/bttbl.bin"
  59. #define BT_PATCH_FILE "/NOR:K/bt_pth.bin"
  60. #define BT_FCC_FILE "/NOR:K/fcc.bin"
  61. #define BT_ROM_FILE "/NOR:K/bt_rom.bin"
  62. #else
  63. #define BT_RF_FILE "bt_rf.bin"
  64. #define BT_TABLE_FILE "bttbl.bin"
  65. #define BT_PATCH_FILE "bt_pth.bin"
  66. #define BT_FCC_FILE "fcc.bin"
  67. #define BT_ROM_FILE "bt_rom.bin"
  68. #endif
  69. //#define BT_RAM1_ADDR (0x01208000)
  70. //#define BT_RAM_TABLE_ADDR (0x01200000)
  71. //#define BT_RAM_PATCH_ADDR (0x01201310)
  72. #define BT_RAM1_ADDR (0x2FF26000)
  73. #define BT_RAM_TABLE_ADDR (0x2FF20000)
  74. #define BT_RAM_PATCH_ADDR (0x2FF21400)
  75. #define BT_ROM_ADDR (0x2FE80000)
  76. /* Config pos from bt_table_config.txt */
  77. #define BT_RF_FUNC_ADDR_POS 0x1c
  78. #define BT_MAX_RF_POWER_POS_1 0x22 /* tx_max_pwr_lvl */
  79. #define BT_DEFAULT_TX_POWER 0x23
  80. #define BT_MAX_RF_POWER_POS_2 0x25 /* tws_max_pwr_lvl */
  81. #define BT_CFG_WAKEUP_ADVANCE_POS 0x26
  82. #define BT_BLE_RF_POWER_POS 0x29 /* le_tx_pwr_lvl */
  83. #define BT_DUT_MODE 0x2b
  84. #define BT_CFG_FIX_MAX_PWR_POS 0x40
  85. #define BT_CFG_FIX_MAX_PWR_BIT 0x3
  86. #define BT_CFG_DISABLE_3M_POS 0x41
  87. #define BT_CFG_DISABLE_3M_BIT 0x1
  88. #define BT_CFG_SEL_32768_POS 0x42
  89. #define BT_CFG_SEL_32768_BIT 0x0 /* Set to 1 */
  90. #define BT_CFG_INIT_K192_BY_HOST_POS 0x42
  91. #define BT_CFG_INIT_K192_BY_HOST_BIT 0x1
  92. #define BT_CFG_FORCE_LIGHT_SLEEP_POS 0x42
  93. #define BT_CFG_FORCE_LIGHT_SLEEP_BIT 0x5 /* Set to 0 */
  94. #define BT_CFG_FORCE_UART_PRINT_POS 0x42
  95. #define BT_CFG_FORCE_UART_PRINT_BIT 0x7 /* Set to 0 */
  96. #define BT_CFG_LE_USING_US_TIMER_POS 0x43
  97. #define BT_CFG_LE_USING_US_TIMER_BIT 0x1 /* Power test set to 0, normal run set to 1, default value 1 */
  98. #define BT_CFG_BT_FAST_BOOT_POS 0x43
  99. #define BT_CFG_BT_FAST_BOOT_BIT 0x2
  100. #define BT_CFG_EFUSE_SET_POS 0x43
  101. #define BT_CFG_EFUSE_SET_BIT 0x4
  102. #define BT_CFG_TRANS_EN_POS 0x43
  103. #define BT_CFG_TRANS_EN_BIT 0x5 /* Set to 1 */
  104. #define BT_CFG_SEL_RC32K_POS 0x44
  105. #define BT_CFG_SEL_RC32K_BIT 0x2 /* Set to 0 */
  106. #define BT_CFG_BQB_DEFAULT_MODE_POS 0x44
  107. #define BT_CFG_BQB_DEFAULT_MODE_BIT 0x4
  108. #define BT_CFG_UART_BAUD_RATE_POS 0x4C /* 0x4c, 0x4d,0x4e,0x4f Little-endian */
  109. #define BT_CFG_EFUSE_AVDD_IF_POS 0x50
  110. #define BT_CFG_EFUSE_POWER_VER_POS 0x54
  111. #define BT_CFG_EFUSE_RF_POS 0x58
  112. #define BT_CFG_IC_PKG_POS 0xB6
  113. #define BT_CFG_UART_BAUD_RATE 2000000 /* 2M */
  114. #define BT_CFG_VD12_SET_S1BT 0xb5
  115. #define EFUSE_POWER_VER 0
  116. #define EFUSE_RF 1
  117. #define EFUSE_AVDD_IF 2
  118. struct ipmsg_btc_config {
  119. void *mem_base;
  120. uint32_t mem_size;
  121. };
  122. struct ipmsg_btc_data {
  123. ipmsg_callback_t btc_cb;
  124. ipmsg_callback_t tws0_cb;
  125. ipmsg_callback_t tws1_cb;
  126. ipmsg_pm_ctrl_callback_t pm_ctrl_cb;
  127. };
  128. static ipmsg_btc_init_param_t btc_set_param;
  129. DEVICE_DECLARE(btc);
  130. #ifdef CONFIG_BT_CTRL_DEBUG
  131. #ifndef CONFIG_BT_CTRL_LOG
  132. #define BT_UART_MFP_SEL 23
  133. static const struct acts_pin_config btc_pin_cfg_uart[] = {
  134. {14, BT_UART_MFP_SEL | GPIO_CTL_PADDRV_LEVEL(1) | GPIO_CTL_PULLUP},
  135. {15, BT_UART_MFP_SEL | GPIO_CTL_PADDRV_LEVEL(1) | GPIO_CTL_PULLUP},
  136. };
  137. #endif
  138. #endif
  139. static void ipmsg_btc_isr(void *arg)
  140. {
  141. struct device *dev = (struct device *)arg;
  142. struct ipmsg_btc_data *data = dev->data;
  143. //clear irq pending
  144. sys_write32(1, PENDING_FROM_BT_CPU);
  145. if (data->btc_cb) {
  146. data->btc_cb(NULL, NULL);
  147. }
  148. }
  149. static void ipmsg_tws0_isr(void *arg)
  150. {
  151. struct device *dev = (struct device *)arg;
  152. struct ipmsg_btc_data *data = dev->data;
  153. if (data->tws0_cb) {
  154. data->tws0_cb(NULL, NULL);
  155. }
  156. }
  157. static void ipmsg_tws1_isr(void *arg)
  158. {
  159. struct device *dev = (struct device *)arg;
  160. struct ipmsg_btc_data *data = dev->data;
  161. if (data->tws1_cb) {
  162. data->tws1_cb(NULL, NULL);
  163. }
  164. }
  165. static void ipmsg_btc_init_param(struct device *dev, void *param)
  166. {
  167. memcpy(&btc_set_param, param, sizeof(btc_set_param));
  168. }
  169. static int sd_load(const char *filename, void *dst)
  170. {
  171. struct sd_file *sdf;
  172. int ret;
  173. sdf = sd_fopen(filename);
  174. if (!sdf) {
  175. return -EINVAL;
  176. }
  177. ret = sd_fread(sdf, dst, sdf->size);
  178. printk("%s size: %d, load: %d\n", filename, sdf->size, ret);
  179. if (ret == sdf->size) {
  180. ret = 0;
  181. } else {
  182. printk("load %s failed!\n", filename);
  183. ret = -EINVAL;
  184. }
  185. sd_fclose(sdf);
  186. return ret;
  187. }
  188. #ifdef CONFIG_SD_FS
  189. extern bool bt_bqb_is_in_test(void);
  190. static inline void bttbl_set_bit(uint8_t *start, uint16_t pos, uint8_t bit)
  191. {
  192. start[pos] |= (0x1 << bit);
  193. }
  194. static inline void bttbl_clear_bit(uint8_t *start, uint16_t pos, uint8_t bit)
  195. {
  196. start[pos] &= (~(0x1 << bit));
  197. }
  198. static inline void bttbl_set_value_u8t(uint8_t *start, uint16_t pos, uint8_t value)
  199. {
  200. start[pos] = value;
  201. }
  202. static inline void bttbl_set_value_u32t(uint8_t *start, uint16_t pos, uint32_t value)
  203. {
  204. sys_put_le32(value ,&start[pos]);
  205. }
  206. static void ipmsg_btc_update_bt_table(void *table_addr)
  207. {
  208. int err;
  209. uint32_t calib_val = 0;
  210. uint8_t *start = table_addr;
  211. int opt = soc_dvfs_opt();
  212. int valid_number = 0;
  213. printk("opt:%d Befor set 0x42 = 0x%x 0x43 = 0x%x 0x44 = 0x%x\n", opt,start[0x42], start[0x43], start[0x44]);
  214. err = soc_atp_get_rf_calib(EFUSE_AVDD_IF, &calib_val);
  215. printk("get efuse avdd ret:%d value:%x default %x\n", err,calib_val,start[BT_CFG_EFUSE_AVDD_IF_POS]);
  216. if (err == 0) {
  217. bttbl_set_value_u32t(start, BT_CFG_EFUSE_AVDD_IF_POS, calib_val);
  218. valid_number ++;
  219. }
  220. err = soc_atp_get_rf_calib(EFUSE_POWER_VER, &calib_val);
  221. printk("get efuse power version ret:%d value:%x default %x\n", err,calib_val,start[BT_CFG_EFUSE_POWER_VER_POS]);
  222. if (err == 0) {
  223. bttbl_set_value_u32t(start, BT_CFG_EFUSE_POWER_VER_POS, calib_val);
  224. valid_number ++;
  225. }
  226. err = soc_atp_get_rf_calib(EFUSE_RF, &calib_val);
  227. printk("get efuse rf ret:%d value:%x default %x\n", err,calib_val,start[BT_CFG_EFUSE_RF_POS]);
  228. if (err == 0) {
  229. bttbl_set_value_u32t(start, BT_CFG_EFUSE_RF_POS, calib_val);
  230. valid_number ++;
  231. }
  232. if (valid_number) {
  233. bttbl_set_bit(start, BT_CFG_EFUSE_SET_POS, BT_CFG_EFUSE_SET_BIT);
  234. }
  235. #ifdef CONFIG_BT_CTRL_RF_DEBUG
  236. /* 0x106000, bt_rf start address */
  237. bttbl_set_value_u32t(start, BT_RF_FUNC_ADDR_POS, 0x106000);
  238. #endif
  239. #ifdef CONFIG_IPMSG_BTC_SEL_32K /*ext 32 losc*/
  240. bttbl_set_bit(start, BT_CFG_SEL_32768_POS, BT_CFG_SEL_32768_BIT);
  241. #else
  242. #if 1 /* Use RC32K */
  243. bttbl_set_bit(start, BT_CFG_SEL_32768_POS, BT_CFG_SEL_32768_BIT);
  244. bttbl_set_bit(start, BT_CFG_SEL_RC32K_POS, BT_CFG_SEL_RC32K_BIT);
  245. #else /*hcl */
  246. bttbl_clear_bit(start, BT_CFG_SEL_32768_POS, BT_CFG_SEL_32768_BIT);
  247. bttbl_clear_bit(start, BT_CFG_SEL_RC32K_POS, BT_CFG_SEL_RC32K_BIT);
  248. #endif
  249. #endif
  250. #ifdef CONFIG_BT_ECC_ACTS
  251. bttbl_set_bit(start, BT_CFG_INIT_K192_BY_HOST_POS, BT_CFG_INIT_K192_BY_HOST_BIT);
  252. #else
  253. bttbl_clear_bit(start, BT_CFG_INIT_K192_BY_HOST_POS, BT_CFG_INIT_K192_BY_HOST_BIT);
  254. #endif
  255. bttbl_clear_bit(start, BT_CFG_FORCE_LIGHT_SLEEP_POS, BT_CFG_FORCE_LIGHT_SLEEP_BIT);
  256. #ifdef CONFIG_BT_CTRL_LOG
  257. bttbl_clear_bit(start, BT_CFG_FORCE_UART_PRINT_POS, BT_CFG_FORCE_UART_PRINT_BIT);
  258. #else
  259. bttbl_set_bit(start, BT_CFG_FORCE_UART_PRINT_POS, BT_CFG_FORCE_UART_PRINT_BIT);
  260. #endif
  261. #ifdef CONFIG_BT_TRANSMIT
  262. bttbl_set_bit(start, BT_CFG_TRANS_EN_POS, BT_CFG_TRANS_EN_BIT);
  263. #endif
  264. #ifndef CONFIG_BOARD_LARK_DVB_EARPHONE
  265. bttbl_set_value_u32t(start, BT_CFG_UART_BAUD_RATE_POS, BT_CFG_UART_BAUD_RATE);
  266. printk("Bt uart rate %d\n", BT_CFG_UART_BAUD_RATE);
  267. #endif
  268. if (btc_set_param.set_max_rf_power) {
  269. bttbl_set_value_u8t(start, BT_MAX_RF_POWER_POS_1, btc_set_param.bt_max_rf_tx_power);
  270. bttbl_set_value_u8t(start, BT_MAX_RF_POWER_POS_2, btc_set_param.bt_max_rf_tx_power);
  271. printk("max rf power %d\n", btc_set_param.bt_max_rf_tx_power);
  272. #if 0
  273. /* Fixed tx power */
  274. #define BT_FIX_POWER_LEVEL 38 /* 8db */
  275. bttbl_set_value_u8t(start, BT_MAX_RF_POWER_POS_1, BT_FIX_POWER_LEVEL);
  276. bttbl_set_value_u8t(start, BT_MAX_RF_POWER_POS_2, BT_FIX_POWER_LEVEL);
  277. bttbl_set_value_u8t(start, BT_DEFAULT_TX_POWER, BT_FIX_POWER_LEVEL);
  278. bttbl_set_bit(start, BT_CFG_FIX_MAX_PWR_POS, BT_CFG_FIX_MAX_PWR_BIT);
  279. printk("After set 0x22 = %d 0x23 = %d 0x25 = %d\n", start[0x22], start[0x23], start[0x25]);
  280. printk("After set 0x40 = 0x%x\n", start[BT_CFG_FIX_MAX_PWR_POS]);
  281. #endif
  282. }
  283. if (btc_set_param.set_ble_rf_power) {
  284. bttbl_set_value_u8t(start, BT_BLE_RF_POWER_POS, btc_set_param.ble_rf_tx_power);
  285. printk("ble rf tx power %d\n", start[BT_BLE_RF_POWER_POS]);
  286. }
  287. #ifdef CONFIG_BT_CTRL_BQB
  288. if (bt_bqb_is_in_test()) {
  289. bttbl_clear_bit(start, BT_CFG_DISABLE_3M_POS, BT_CFG_DISABLE_3M_BIT);
  290. bttbl_set_bit(start, BT_CFG_BQB_DEFAULT_MODE_POS, BT_CFG_BQB_DEFAULT_MODE_BIT);
  291. bttbl_set_value_u8t(start, BT_DUT_MODE, 0x01);
  292. bttbl_set_value_u8t(start, BT_CFG_VD12_SET_S1BT, 0x0E); // VD12 set 1.3V to bt core.
  293. }
  294. #endif
  295. #ifdef CONFIG_BT_FAST_BOOT
  296. if (soc_dvfs_opt())
  297. bttbl_set_bit(start, BT_CFG_BT_FAST_BOOT_POS, BT_CFG_BT_FAST_BOOT_BIT);
  298. #endif
  299. bttbl_set_value_u8t(start, BT_CFG_IC_PKG_POS, ipmsg_btc_get_ic_pkt());
  300. printk("After set 0x42 = 0x%x 0x43 = 0x%x 0x44 = 0x%x\n", start[0x42], start[0x43], start[0x44]);
  301. }
  302. #endif
  303. static int ipmsg_btc_load(struct device *dev, void *data, uint32_t size)
  304. {
  305. uint32_t val;
  306. int err = 0;
  307. const struct ipmsg_btc_config *config = dev->config;
  308. if (size > config->mem_size) {
  309. return -EINVAL;
  310. }
  311. val = sys_read32(CMU_MEMCLKSRC1) & ~BT_RAM_CLK_SRC;
  312. sys_write32(val, CMU_MEMCLKSRC1);
  313. #ifdef CONFIG_SD_FS
  314. #ifdef CONFIG_BT_FCC_TEST
  315. #define BT_RAM_FCC_ADDR BT_RAM_TABLE_ADDR
  316. sys_write32((sys_read32(MEMORYCTL) | (0x1<<4)), MEMORYCTL); // bt cpu boot from btram0
  317. k_sleep(K_MSEC(1));
  318. sd_load(CONFIG_IPMSG_BTC_FCC_NAME, (void *)BT_RAM_FCC_ADDR); /* load bt config table */
  319. k_sleep(K_MSEC(1));
  320. return err;
  321. #endif
  322. #ifdef CONFIG_BT_CTRL_RF_DEBUG
  323. err = sd_load(BT_RF_FILE, (void *)BT_RAM1_ADDR);
  324. if (err) {
  325. return -EINVAL;
  326. }
  327. #endif
  328. err = sd_load(BT_TABLE_FILE, (void *)BT_RAM_TABLE_ADDR); /* load bt config table */
  329. ipmsg_btc_update_bt_table((void *)BT_RAM_TABLE_ADDR);
  330. err |= sd_load(BT_PATCH_FILE, (void *)BT_RAM_PATCH_ADDR); /* load bt patch */
  331. #else
  332. memcpy(config->mem_base, data, size);
  333. #endif
  334. return err;
  335. }
  336. static void ipmsg_btc_soc_set_hoscldo_ctl(void)
  337. {
  338. int val, i, j, cnt;
  339. sys_write32(0x80100204, HOSCLDO_CTL);
  340. k_sleep(K_MSEC(10));
  341. val = sys_read32(HOSCLDO_CTL) | (0x1 << HOSCLDO_CTL_OSC32M_EN);
  342. sys_write32(val, HOSCLDO_CTL);
  343. k_sleep(K_MSEC(10));
  344. val = sys_read32(HOSCLDO_CTL) | (0x1 << HOSCLDO_CTL_OSC32M_CALEN) | (0x1 << HOSCLDO_CTL_OSC32M_CALMODE);
  345. sys_write32(val, HOSCLDO_CTL);
  346. i = 0;
  347. while ((sys_read32(HOSCLDO_CTL) & (0x1 << HOSCLDO_CTL_OSC32M_CALDONE))) {
  348. if (i++ > 10) {
  349. printk("HOSCLDO_CTL OSC32M wait 0 timeout\n");
  350. break;
  351. }
  352. k_sleep(K_MSEC(1));
  353. }
  354. i = 0;
  355. while (1) {
  356. cnt = 0;
  357. for (j=0; j<3; j++) {
  358. if (sys_read32(HOSCLDO_CTL) & (0x1 << HOSCLDO_CTL_OSC32M_CALDONE)) {
  359. cnt++;
  360. }
  361. k_busy_wait(10);
  362. }
  363. if (cnt == 3) {
  364. break;
  365. }
  366. if (i++ > 50) {
  367. printk("HOSCLDO_CTL OSC32M wait 1 timeout\n");
  368. break;
  369. }
  370. k_sleep(K_MSEC(1));
  371. }
  372. val = sys_read32(HOSCLDO_CTL) & HOSCLDO_CTL_OSC32M_FRQCAL_MASK;
  373. val >>= HOSCLDO_CTL_OSC32M_FRQCAL_SHIFT;
  374. val <<= HOSCLDO_CTL_OSC32M_FRQMSET_SHIFT;
  375. val |= 0x80000204;
  376. sys_write32(val, HOSCLDO_CTL);
  377. }
  378. void ipmsg_btc_soc_set_hosc_cap(int cap)
  379. {
  380. int val;
  381. printk("Set hosc cap %d.%d pf\n", cap / 10, cap % 10);
  382. val = sys_read32(HOSC_CTL);
  383. val &= (~HOSC_CTL_HOSC_CAP_MASK);
  384. val |= HOSC_CTL_HOSC_CAP(cap);
  385. sys_write32(val, HOSC_CTL);
  386. ipmsg_btc_soc_set_hoscldo_ctl();
  387. printk("HOSC_CTL = 0x%x HOSCLDO_CTL = 0x%x\n", sys_read32(HOSC_CTL), sys_read32(HOSCLDO_CTL));
  388. }
  389. /* Start BT CPU */
  390. static inline int ipmsg_btc_start(struct device *dev, uint32_t *send_id, uint32_t *recv_id)
  391. {
  392. uint32_t val;
  393. ARG_UNUSED(dev);
  394. /* System runing, some bit can't change in HOSC_CTL, init in leopard_init */
  395. //sys_write32(0xa01f9b2, HOSC_CTL);
  396. if (btc_set_param.set_hosc_cap) {
  397. ipmsg_btc_soc_set_hosc_cap((int)btc_set_param.hosc_capacity);
  398. } else {
  399. /* Default cap 0x55 */
  400. val = sys_read32(HOSC_CTL) | HOSC_CTL_HOSC_CAP(0x55);
  401. sys_write32(val, HOSC_CTL);
  402. ipmsg_btc_soc_set_hoscldo_ctl();
  403. printk("HOSC_CTL = 0x%x HOSCLDO_CTL = 0x%x\n", sys_read32(HOSC_CTL), sys_read32(HOSCLDO_CTL));
  404. }
  405. #ifdef CONFIG_IPMSG_BTC_SEL_32K
  406. sys_write32(0x5515FB, LOSC_CTL);
  407. int i = 0;
  408. while (!(sys_read32(LOSC_CTL) & LOSC_CTL_LOSC_RDY)) {
  409. if (i++ > 500) {
  410. printk("LOSC_CTL not ready!\n");
  411. break;
  412. }
  413. k_sleep(K_MSEC(1));
  414. }
  415. #else
  416. sys_write32(0x951, HOSCOK_CTL);
  417. #endif
  418. val = sys_read32(CMU_MEMCLKSRC1) | BT_RAM_CLK_SRC;
  419. sys_write32(val, CMU_MEMCLKSRC1);
  420. val = sys_read32(CMU_MEMCLKEN1) | BT_ROM_RAM_CLK_EN;
  421. sys_write32(val, CMU_MEMCLKEN1);
  422. val = sys_read32(CMU_DEVCLKEN1) | BTHUB_RC32KEN | BTHUB_RC64MEN | BTHUB_HOSCEN |
  423. BTHUB_RC4MEN | BTHUB_LOSCEN;
  424. sys_write32(val, CMU_DEVCLKEN1);
  425. #ifdef CONFIG_BT_FCC_TEST
  426. val = sys_read32(CMU_S1CLKCTL) | RC64M_S1EN | HOSC_S1EN | RC4M_S1EN;
  427. sys_write32(val, CMU_S1CLKCTL);
  428. val = sys_read32(CMU_S1BTCLKCTL) | RC64M_S1BTEN | HOSC_S1BTEN | RC4M_S1BTEN;
  429. sys_write32(val, CMU_S1BTCLKCTL);
  430. #else
  431. val = sys_read32(CMU_S1CLKCTL) | HOSC_S1EN | RC4M_S1EN;
  432. sys_write32(val, CMU_S1CLKCTL);
  433. val = sys_read32(CMU_S1BTCLKCTL) | HOSC_S1BTEN | RC4M_S1BTEN;
  434. sys_write32(val, CMU_S1BTCLKCTL);
  435. #endif
  436. #ifdef CONFIG_BT_CTRL_DEBUG
  437. #ifdef CONFIG_BT_CTRL_BQB
  438. extern bool bt_bqb_is_in_test(void);
  439. if (!bt_bqb_is_in_test())
  440. #endif
  441. {
  442. sys_write32(DBGSE_BT_CONTROLLER, DEBUGSEL);
  443. sys_write32(0x3C000, DEBUGOE0);
  444. //sys_write32(0xF03C000, DEBUGOE0);
  445. }
  446. #ifndef CONFIG_BT_CTRL_LOG
  447. acts_pinmux_setup_pins(btc_pin_cfg_uart, ARRAY_SIZE(btc_pin_cfg_uart));
  448. #endif
  449. #endif
  450. /* BT sleep clock RC4M/4, RAM can deepsleep */
  451. //sys_clear_bit(PWRGATE_DIG, 29); // RAM_BT_FORCE=0
  452. //sys_clear_bit(PWRGATE_DIG, 27); // BT_FORCE=0
  453. #ifdef CONFIG_BT_FAST_BOOT
  454. if (soc_dvfs_opt())
  455. soc_powergate_set(POWERGATE_BT_PG_DEV, false);
  456. else
  457. soc_powergate_set(POWERGATE_BT_PG_DEV, true);
  458. #else
  459. soc_powergate_set(POWERGATE_BT_PG_DEV, true);
  460. #endif
  461. /* BT RAM0/1 deepsleep */
  462. sys_write32(sys_read32(RAM_DEEPSLEEP) | 0x03000000, RAM_DEEPSLEEP);
  463. acts_reset_peripheral(RESET_ID_BT);
  464. irq_enable(IRQ_ID_BT);
  465. return 0;
  466. }
  467. static int ipmsg_btc_stop(struct device *dev)
  468. {
  469. uint32_t val;
  470. ARG_UNUSED(dev);
  471. // disable irq
  472. irq_disable(IRQ_ID_BT);
  473. // reset btc
  474. acts_reset_peripheral_assert(RESET_ID_BT);
  475. // reset BTRAM clock src
  476. val = sys_read32(CMU_MEMCLKSRC1) & ~BT_RAM_CLK_SRC;
  477. sys_write32(val, CMU_MEMCLKSRC1);
  478. return 0;
  479. }
  480. static int ipmsg_btc_notify(struct device *dev)
  481. {
  482. ARG_UNUSED(dev);
  483. volatile uint8_t i = 5;
  484. // send interrupt to btc
  485. sys_write32(0, INT_TO_BT_CPU);
  486. while(i--);
  487. sys_write32(1, INT_TO_BT_CPU);
  488. return 0;
  489. }
  490. static void ipmsg_btc_register_callback(struct device *dev,
  491. ipmsg_callback_t cb, void *context)
  492. {
  493. struct ipmsg_btc_data *data = dev->data;
  494. uint8_t irq = *((uint8_t *)context);
  495. switch (irq) {
  496. case IPMSG_BTC_IRQ:
  497. data->btc_cb = cb;
  498. break;
  499. case IPMSG_TWS0_IRQ:
  500. data->tws0_cb = cb;
  501. break;
  502. case IPMSG_TWS1_IRQ:
  503. data->tws1_cb = cb;
  504. break;
  505. case IPMSG_REG_PW_CTRL:
  506. data->pm_ctrl_cb = (ipmsg_pm_ctrl_callback_t)cb;
  507. break;
  508. default:
  509. printk("Unknown irq %d\n", irq);
  510. break;
  511. }
  512. }
  513. static void ipmsg_btc_clear_irq_pending(int32_t irq)
  514. {
  515. if (irq < 32) {
  516. sys_write32((0x1 << irq), NVIC_ICPR0);
  517. } else {
  518. sys_write32((0x1 << (irq - 32)), NVIC_ICPR1);
  519. }
  520. }
  521. static void ipmsg_btc_irq_enable(struct device *dev, uint8_t irq)
  522. {
  523. switch (irq) {
  524. case IPMSG_BTC_IRQ:
  525. irq_enable(IRQ_ID_BT);
  526. break;
  527. case IPMSG_TWS0_IRQ:
  528. ipmsg_btc_clear_irq_pending(IRQ_ID_TWS);
  529. irq_enable(IRQ_ID_TWS);
  530. break;
  531. case IPMSG_TWS1_IRQ:
  532. ipmsg_btc_clear_irq_pending(IRQ_ID_TWS1);
  533. irq_enable(IRQ_ID_TWS1);
  534. break;
  535. }
  536. }
  537. static void ipmsg_btc_irq_disable(struct device *dev, uint8_t irq)
  538. {
  539. switch (irq) {
  540. case IPMSG_BTC_IRQ:
  541. irq_disable(IRQ_ID_BT);
  542. break;
  543. case IPMSG_TWS0_IRQ:
  544. irq_disable(IRQ_ID_TWS);
  545. break;
  546. case IPMSG_TWS1_IRQ:
  547. irq_disable(IRQ_ID_TWS1);
  548. break;
  549. }
  550. }
  551. static int ipmsg_btc_init(const struct device *dev)
  552. {
  553. ARG_UNUSED(dev);
  554. /* btc irq init */
  555. IRQ_CONNECT(IRQ_ID_BT, CONFIG_BTC_IRQ_PRI,
  556. ipmsg_btc_isr, DEVICE_GET(btc), 0);
  557. /* tws irq init */
  558. IRQ_CONNECT(IRQ_ID_TWS, CONFIG_TWS_IRQ_PRI,
  559. ipmsg_tws0_isr, DEVICE_GET(btc), 0);
  560. IRQ_CONNECT(IRQ_ID_TWS1, CONFIG_TWS_IRQ_PRI,
  561. ipmsg_tws1_isr, DEVICE_GET(btc), 0);
  562. return 0;
  563. }
  564. #ifdef CONFIG_PM_DEVICE
  565. int ipmsg_btc_pm_control(const struct device *device, enum pm_device_action action)
  566. {
  567. int ret;
  568. struct ipmsg_btc_data *data = device->data;
  569. if (data && data->pm_ctrl_cb) {
  570. data->pm_ctrl_cb(0, action);
  571. }
  572. switch (action) {
  573. case PM_DEVICE_ACTION_RESUME:
  574. break;
  575. case PM_DEVICE_ACTION_SUSPEND:
  576. break;
  577. case PM_DEVICE_ACTION_EARLY_SUSPEND:
  578. break;
  579. case PM_DEVICE_ACTION_LATE_RESUME:
  580. break;
  581. default:
  582. ret = -EINVAL;
  583. }
  584. return 0;
  585. }
  586. #else
  587. #define ipmsg_btc_pm_control NULL
  588. #endif
  589. static const struct ipmsg_driver_api ipmsg_btc_driver_api = {
  590. .init_param = ipmsg_btc_init_param,
  591. .load = ipmsg_btc_load,
  592. .start = ipmsg_btc_start,
  593. .stop = ipmsg_btc_stop,
  594. .notify = ipmsg_btc_notify,
  595. .register_callback = ipmsg_btc_register_callback,
  596. .enable = ipmsg_btc_irq_enable,
  597. .disable = ipmsg_btc_irq_disable,
  598. };
  599. static const struct ipmsg_btc_config ipmsg_btc_cfg = {
  600. .mem_base = (void *)BTC_REG_BASE,
  601. .mem_size = 0x40000,
  602. };
  603. static struct ipmsg_btc_data ipmsg_btc_dat;
  604. DEVICE_DEFINE(btc, CONFIG_BTC_NAME,
  605. &ipmsg_btc_init, ipmsg_btc_pm_control,
  606. &ipmsg_btc_dat, &ipmsg_btc_cfg,
  607. POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
  608. &ipmsg_btc_driver_api);