bqb_acts.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <errno.h>
  4. #include <stdbool.h>
  5. #include <kernel.h>
  6. #include <device.h>
  7. #include <init.h>
  8. #include <soc.h>
  9. #include <drivers/ipmsg.h>
  10. #include <sys/printk.h>
  11. #include <sys/byteorder.h>
  12. #include <drivers/uart.h>
  13. #include <drivers/uart_dma.h>
  14. #include <drivers/bluetooth/bt_drv.h>
  15. #define BQB_USE_UART_0 1
  16. #define BT_HCI_OP_VS_WRITE_BB_REG 0xfc8a
  17. #define BT_HCI_OP_VS_READ_BB_REG 0xfc8b
  18. #define BT_HCI_OP_VS_WRITE_RF_REG 0xfc8c
  19. #define BT_HCI_OP_VS_READ_RF_REG 0xfc8d
  20. #define BT_HCI_EVT_VS_READ_BB_REG_REPORT 0x80
  21. #define BT_HCI_EVT_VS_READ_RF_REG_REPORT 0x81
  22. struct bqb_bb_reg_report {
  23. uint32_t base_addr;
  24. uint32_t size;
  25. uint32_t result[0];
  26. } __packed;
  27. struct bqb_rf_reg_report {
  28. uint16_t base_addr;
  29. uint16_t size;
  30. uint16_t result[0];
  31. } __packed;
  32. #if BQB_USE_UART_0
  33. #define BQB_UART_STA UART0_STA
  34. #define BQB_UART_SET_BR 115200
  35. #define BLE_BQB_UART "UART_0"
  36. static struct device *uart_dev;
  37. #else
  38. #define BQB_UART_STA UART1_STA
  39. #define BLE_BQB_UART "UART_1"
  40. static struct device *uart_dev;
  41. #endif
  42. struct acts_bqb_data {
  43. uint8_t tx_data[128];
  44. uint8_t rx_data[128];
  45. uint8_t rx_type;
  46. uint8_t ble_bqb_mode;
  47. uint8_t bqb_in_test;
  48. };
  49. static struct acts_bqb_data *bqb_info;
  50. static void bqb_vs_evt_handle(uint8_t *buf)
  51. {
  52. int i;
  53. struct bqb_bb_reg_report *bb_evt;
  54. struct bqb_rf_reg_report *rf_evt;
  55. uint8_t subevt = buf[0];
  56. buf++;
  57. if (subevt == BT_HCI_EVT_VS_READ_BB_REG_REPORT) {
  58. bb_evt = (void *)buf;
  59. for (i=0; i<bb_evt->size; i++) {
  60. printk("0x%08x: 0x%08x\n", bb_evt->base_addr + i*4, bb_evt->result[i]);
  61. }
  62. } else if (subevt == BT_HCI_EVT_VS_READ_RF_REG_REPORT) {
  63. rf_evt = (void *)buf;
  64. for (i=0; i<rf_evt->size; i++) {
  65. printk("0x%04x: 0x%04x\n", rf_evt->base_addr + i, rf_evt->result[i]);
  66. }
  67. } else {
  68. printk("[BQB] unknown vendor event\n");
  69. }
  70. }
  71. static void bqb_evt_handle(uint8_t *buf)
  72. {
  73. uint8_t evt = buf[0];
  74. uint8_t len = buf[1];
  75. if (len <= 0) {
  76. printk("[BQB] buf empty\n");
  77. }
  78. if (evt == 0xff) { //HCI Vendor Event
  79. bqb_vs_evt_handle(buf + HCI_EVT_HDR_SIZE);
  80. }
  81. }
  82. int bt_bqb_vs_write_bb_reg(uint32_t addr, uint32_t val)
  83. {
  84. if (!bqb_info) {
  85. return -ENOMEM;
  86. }
  87. bqb_info->tx_data[0] = (uint8_t)BT_HCI_OP_VS_WRITE_BB_REG;
  88. bqb_info->tx_data[1] = (uint8_t)(BT_HCI_OP_VS_WRITE_BB_REG >> 8);
  89. bqb_info->tx_data[2] = 0x08;
  90. sys_put_le32(addr, &bqb_info->tx_data[3]);
  91. sys_put_le32(val, &bqb_info->tx_data[7]);
  92. return btdrv_send(HCI_CMD, bqb_info->tx_data, 11);
  93. }
  94. int bt_bqb_vs_read_bb_reg(uint32_t addr, uint8_t size)
  95. {
  96. if (!bqb_info) {
  97. return -ENOMEM;
  98. }
  99. bqb_info->tx_data[0] = (uint8_t)BT_HCI_OP_VS_READ_BB_REG;
  100. bqb_info->tx_data[1] = (uint8_t)(BT_HCI_OP_VS_READ_BB_REG >> 8);
  101. bqb_info->tx_data[2] = 0x05;
  102. sys_put_le32(addr, &bqb_info->tx_data[3]);
  103. bqb_info->tx_data[7] = size;
  104. return btdrv_send(HCI_CMD, bqb_info->tx_data, 8);
  105. }
  106. int bt_bqb_vs_write_rf_reg(uint16_t addr, uint16_t val)
  107. {
  108. if (!bqb_info) {
  109. return -ENOMEM;
  110. }
  111. bqb_info->tx_data[0] = (uint8_t)BT_HCI_OP_VS_WRITE_RF_REG;
  112. bqb_info->tx_data[1] = (uint8_t)(BT_HCI_OP_VS_WRITE_RF_REG >> 8);
  113. bqb_info->tx_data[2] = 0x04;
  114. sys_put_le16(addr, &bqb_info->tx_data[3]);
  115. sys_put_le16(val, &bqb_info->tx_data[5]);
  116. return btdrv_send(HCI_CMD, bqb_info->tx_data, 7);
  117. }
  118. int bt_bqb_vs_read_rf_reg(uint16_t addr, uint8_t size)
  119. {
  120. if (!bqb_info) {
  121. return -ENOMEM;
  122. }
  123. bqb_info->tx_data[0] = (uint8_t)BT_HCI_OP_VS_READ_RF_REG;
  124. bqb_info->tx_data[1] = (uint8_t)(BT_HCI_OP_VS_READ_RF_REG >> 8);
  125. bqb_info->tx_data[2] = 0x03;
  126. sys_put_le16(addr, &bqb_info->tx_data[3]);
  127. bqb_info->tx_data[5] = size;
  128. return btdrv_send(HCI_CMD, bqb_info->tx_data, 6);
  129. }
  130. void bt_dut_mode(void)
  131. {
  132. if (!bqb_info) {
  133. return;
  134. }
  135. bqb_info->tx_data[0] = 0x03;
  136. bqb_info->tx_data[1] = 0x0c; /* ogf: baseband */
  137. bqb_info->tx_data[2] = 0x00; /* len */
  138. btdrv_send(HCI_CMD, bqb_info->tx_data, 3); /* HCI reset */
  139. k_sleep(K_MSEC(50));
  140. #if 0
  141. bqb_info->tx_data[0] = 0x52;
  142. bqb_info->tx_data[1] = 0x0c;
  143. bqb_info->tx_data[2] = 0x0d;
  144. bqb_info->tx_data[3] = 0x00;
  145. bqb_info->tx_data[4] = 0x0b;
  146. bqb_info->tx_data[5] = 0x09;
  147. bqb_info->tx_data[6] = 'L';
  148. bqb_info->tx_data[7] = 'a';
  149. bqb_info->tx_data[8] = 'r';
  150. bqb_info->tx_data[9] = 'k';
  151. bqb_info->tx_data[10] = '_';
  152. bqb_info->tx_data[11] = 'B';
  153. bqb_info->tx_data[12] = 'Q';
  154. bqb_info->tx_data[13] = 'B';
  155. bqb_info->tx_data[14] = 0x00;
  156. bqb_info->tx_data[15] = 0x00;
  157. btdrv_send(HCI_CMD, bqb_info->tx_data, 16); /* HCI set name */
  158. k_sleep(K_MSEC(20));
  159. #endif
  160. bqb_info->tx_data[0] = 0x1e; /* write inquiry scan activity */
  161. bqb_info->tx_data[1] = 0x0c;
  162. bqb_info->tx_data[2] = 0x04;
  163. bqb_info->tx_data[3] = 0x00;
  164. bqb_info->tx_data[4] = 0x02;
  165. bqb_info->tx_data[5] = 0x60;
  166. bqb_info->tx_data[6] = 0x00;
  167. btdrv_send(HCI_CMD, bqb_info->tx_data, 7);
  168. k_sleep(K_MSEC(20));
  169. bqb_info->tx_data[0] = 0x1c; /* write page scan activity */
  170. bqb_info->tx_data[1] = 0x0c;
  171. bqb_info->tx_data[2] = 0x04;
  172. bqb_info->tx_data[3] = 0x00;
  173. bqb_info->tx_data[4] = 0x04;
  174. bqb_info->tx_data[5] = 0x80;
  175. bqb_info->tx_data[6] = 0x00;
  176. btdrv_send(HCI_CMD, bqb_info->tx_data, 7);
  177. k_sleep(K_MSEC(20));
  178. bqb_info->tx_data[0] = 0x43; /* write inquiry scan type */
  179. bqb_info->tx_data[1] = 0x0c;
  180. bqb_info->tx_data[2] = 0x01;
  181. bqb_info->tx_data[3] = 0x01;
  182. btdrv_send(HCI_CMD, bqb_info->tx_data, 4);
  183. k_sleep(K_MSEC(20));
  184. bqb_info->tx_data[0] = 0x47; /* write page scan type */
  185. bqb_info->tx_data[1] = 0x0c;
  186. bqb_info->tx_data[2] = 0x01;
  187. bqb_info->tx_data[3] = 0x01;
  188. btdrv_send(HCI_CMD, bqb_info->tx_data, 4);
  189. k_sleep(K_MSEC(20));
  190. bqb_info->tx_data[0] = 0x1a;
  191. bqb_info->tx_data[1] = 0x0c;
  192. bqb_info->tx_data[2] = 0x01;
  193. bqb_info->tx_data[3] = 0x03;
  194. btdrv_send(HCI_CMD, bqb_info->tx_data, 4); /* HCI write scan */
  195. k_sleep(K_MSEC(20));
  196. bqb_info->tx_data[0] = 0x05;
  197. bqb_info->tx_data[1] = 0x0c;
  198. bqb_info->tx_data[2] = 0x03;
  199. bqb_info->tx_data[3] = 0x02;
  200. bqb_info->tx_data[4] = 0x00;
  201. bqb_info->tx_data[5] = 0x02;
  202. btdrv_send(HCI_CMD, bqb_info->tx_data, 6); /* HCI set event filter */
  203. k_sleep(K_MSEC(20));
  204. bqb_info->tx_data[0] = 0x03;
  205. bqb_info->tx_data[1] = 0x18; /* ogf: test */
  206. bqb_info->tx_data[2] = 0x00;
  207. btdrv_send(HCI_CMD, bqb_info->tx_data, 3); /* HCI dut mode */
  208. k_sleep(K_MSEC(20));
  209. }
  210. static void bqb_uart_send(uint8_t *tx_data, uint16_t len)
  211. {
  212. int i;
  213. if (!bqb_info) {
  214. return;
  215. }
  216. for (i = 0; i < len; i++) {
  217. uart_poll_out(uart_dev, tx_data[i]);
  218. }
  219. }
  220. static int bqb_uart_tx_hci(uint8_t type, uint8_t *data)
  221. {
  222. uint16_t len;
  223. switch (type) {
  224. case HCI_ACL:
  225. len = (data[3]<<8 | data[2]) + HCI_ACL_HDR_SIZE;
  226. break;
  227. case HCI_SCO:
  228. len = data[2] + HCI_SCO_HDR_SIZE;
  229. break;
  230. case HCI_EVT:
  231. len = data[1] + HCI_EVT_HDR_SIZE;
  232. break;
  233. case HCI_ISO:
  234. len = (data[3]<<8 | data[2]) + HCI_ISO_HDR_SIZE;
  235. break;
  236. default:
  237. return -EINVAL;
  238. }
  239. uart_poll_out(uart_dev, type);
  240. bqb_uart_send(data, len);
  241. return 0;
  242. }
  243. static uint16_t bqb_uart_read(uint8_t *rx_data, uint16_t size)
  244. {
  245. uint16_t i;
  246. if (!bqb_info) {
  247. return 0;
  248. }
  249. for (i = 0; i < size; i++) {
  250. uart_poll_in(uart_dev, &rx_data[i]);
  251. }
  252. return i;
  253. }
  254. static void bqb_uart_rx_hci(uint8_t *buf)
  255. {
  256. uint8_t type = 0;
  257. bool has_hdr = false;
  258. uint16_t len, rd_len, total = 0;
  259. uart_poll_in(uart_dev, &type);
  260. //printk("read type: %d\n", type);
  261. switch (type) {
  262. case HCI_CMD:
  263. rd_len = bqb_uart_read(buf, HCI_CMD_HDR_SIZE);
  264. if (rd_len == HCI_CMD_HDR_SIZE) {
  265. has_hdr = true;
  266. len = buf[2];
  267. }
  268. break;
  269. case HCI_ACL:
  270. rd_len = bqb_uart_read(buf, HCI_ACL_HDR_SIZE);
  271. if (rd_len == HCI_ACL_HDR_SIZE) {
  272. has_hdr = true;
  273. len = buf[3]<<8 | buf[2];
  274. }
  275. break;
  276. case HCI_SCO:
  277. rd_len = bqb_uart_read(buf, HCI_SCO_HDR_SIZE);
  278. if (rd_len == HCI_SCO_HDR_SIZE) {
  279. has_hdr = true;
  280. len = buf[2];
  281. }
  282. break;
  283. case HCI_ISO:
  284. rd_len = bqb_uart_read(buf, HCI_ISO_HDR_SIZE);
  285. if (rd_len == HCI_ISO_HDR_SIZE) {
  286. has_hdr = true;
  287. len = buf[3]<<8 | buf[2];
  288. }
  289. break;
  290. default:
  291. printk("unknown type: %d\n", type);
  292. break;
  293. }
  294. //printk("read hdr: %d %d\n", rd_len, len);
  295. if (!has_hdr) {
  296. printk("uart rx incomplete\n");
  297. return;
  298. }
  299. total += rd_len;
  300. rd_len = bqb_uart_read(buf+rd_len, len);
  301. if (rd_len != len) {
  302. printk("uart rx incomplete\n");
  303. return;
  304. }
  305. total += rd_len;
  306. btdrv_send(type, buf, total);
  307. }
  308. static void bqb_uart_isr(const struct device *dev, void *user_data)
  309. {
  310. ARG_UNUSED(dev);
  311. ARG_UNUSED(user_data);
  312. if (uart_irq_rx_ready(uart_dev)) {
  313. if (bqb_info) {
  314. bqb_uart_rx_hci(bqb_info->tx_data);
  315. }
  316. }
  317. /* Clear the interrupt */
  318. sys_write32(0x1, BQB_UART_STA);
  319. }
  320. #if BQB_USE_UART_0
  321. static int _stdout_hook_null(int c)
  322. {
  323. (void)(c);
  324. return EOF;
  325. }
  326. static int bqb_uart_init(void)
  327. {
  328. struct uart_config cfg;
  329. uart_dev = (struct device *)device_get_binding(BLE_BQB_UART);
  330. if (uart_dev == NULL) {
  331. printk("cannot get device %s\n", BLE_BQB_UART);
  332. return -ENODEV;
  333. }
  334. extern void __printk_hook_install(int (*fn)(int));
  335. extern void __stdout_hook_install(int (*fn)(int));
  336. __printk_hook_install(_stdout_hook_null);
  337. __stdout_hook_install(_stdout_hook_null);
  338. uart_config_get(uart_dev, &cfg);
  339. cfg.baudrate = BQB_UART_SET_BR;
  340. uart_configure(uart_dev, &cfg);
  341. uart_dma_send_stop(uart_dev);
  342. uart_fifo_switch(uart_dev, 1, UART_FIFO_TYPE_CPU);
  343. uart_dma_receive_stop(uart_dev);
  344. uart_fifo_switch(uart_dev, 0, UART_FIFO_TYPE_CPU);
  345. uart_irq_rx_disable(uart_dev);
  346. uart_irq_callback_set(uart_dev, bqb_uart_isr);
  347. uart_irq_rx_enable(uart_dev);
  348. return 0;
  349. }
  350. #else
  351. #ifdef CONFIG_BOARD_LARK_DVB_EARPHONE
  352. #define BT_UART_MFP_SEL 6
  353. static const struct acts_pin_config bqb_pin_cfg[] = {
  354. {12, BT_UART_MFP_SEL | GPIO_CTL_PADDRV_LEVEL(1) | GPIO_CTL_PULLUP},
  355. {13, BT_UART_MFP_SEL | GPIO_CTL_PADDRV_LEVEL(1) | GPIO_CTL_PULLUP},
  356. };
  357. #else
  358. #define BT_UART_MFP_SEL 6
  359. static const struct acts_pin_config bqb_pin_cfg[] = {
  360. {16, BT_UART_MFP_SEL | GPIO_CTL_PADDRV_LEVEL(1) | GPIO_CTL_PULLUP},
  361. {17, BT_UART_MFP_SEL | GPIO_CTL_PADDRV_LEVEL(1) | GPIO_CTL_PULLUP},
  362. };
  363. #endif
  364. static int bqb_uart_init(void)
  365. {
  366. acts_pinmux_setup_pins(bqb_pin_cfg, ARRAY_SIZE(bqb_pin_cfg));
  367. uart_dev = (struct device *)device_get_binding(BLE_BQB_UART);
  368. if (uart_dev == NULL) {
  369. printk("cannot get device %s\n", BLE_BQB_UART);
  370. return -ENODEV;
  371. }
  372. uart_irq_callback_set(uart_dev, bqb_uart_isr);
  373. uart_irq_rx_enable(uart_dev);
  374. return 0;
  375. }
  376. #endif
  377. static void *bqb_init_info(void)
  378. {
  379. uint32_t id;
  380. struct acts_bqb_data *bqb_info;
  381. id = ipmsg_create(RBUF_RAW, sizeof(struct acts_bqb_data));
  382. if (id == 0) {
  383. return NULL;
  384. }
  385. rbuf_t *rbuf = RBUF_FR_OF(id);
  386. bqb_info = (void *)RBUF_FR_OF(rbuf->buf_off);
  387. memset(bqb_info, 0, sizeof(struct acts_bqb_data));
  388. return bqb_info;
  389. }
  390. static uint8_t *bqb_get_buf(uint8_t type, uint8_t evt, uint16_t exp_len)
  391. {
  392. if (bqb_info) {
  393. bqb_info->rx_type = type;
  394. return bqb_info->rx_data;
  395. } else {
  396. return NULL;
  397. }
  398. }
  399. static int bqb_recv(uint16_t len)
  400. {
  401. if (bqb_info) {
  402. if (bqb_info->ble_bqb_mode) {
  403. bqb_uart_tx_hci(bqb_info->rx_type, bqb_info->rx_data);
  404. }
  405. if (bqb_info->rx_type == HCI_EVT) {
  406. bqb_evt_handle(bqb_info->rx_data);
  407. }
  408. }
  409. return 0;
  410. }
  411. static const btdrv_hci_cb_t cb = {
  412. .get_buf = bqb_get_buf,
  413. .recv = bqb_recv,
  414. };
  415. int bqb_init(bool ble_bqb)
  416. {
  417. int err;
  418. bqb_info = bqb_init_info();
  419. if (!bqb_info) {
  420. printk("Error bqb_init_info failed!\n");
  421. return -ENOMEM;
  422. }
  423. bqb_info->bqb_in_test = 1;
  424. bqb_info->ble_bqb_mode = ble_bqb ? 1 : 0;
  425. err = btdrv_init((btdrv_hci_cb_t *)&cb);
  426. if (err) {
  427. return -EINVAL;
  428. }
  429. if (bqb_info->ble_bqb_mode) {
  430. err = bqb_uart_init();
  431. if (err) {
  432. return -EINVAL;
  433. }
  434. return 0;
  435. }
  436. bt_dut_mode();
  437. return 0;
  438. }
  439. bool bt_bqb_is_in_test(void)
  440. {
  441. if (bqb_info) {
  442. return bqb_info->bqb_in_test ? true : false;
  443. } else {
  444. return false;
  445. }
  446. }