1 /*
2 * Copyright (c) 2023 Ambiq Micro Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /**
8 * @brief Ambiq SPI based Bluetooth HCI driver.
9 */
10
11 #define DT_DRV_COMPAT ambiq_bt_hci_spi
12
13 #include <zephyr/init.h>
14 #include <zephyr/sys/byteorder.h>
15 #include <zephyr/drivers/spi.h>
16 #include <zephyr/drivers/bluetooth.h>
17 #include <zephyr/bluetooth/hci.h>
18
19 #define LOG_LEVEL CONFIG_BT_HCI_DRIVER_LOG_LEVEL
20 #include <zephyr/logging/log.h>
21 LOG_MODULE_REGISTER(bt_hci_driver);
22
23 #include "apollox_blue.h"
24
25 /* Offset of special item */
26 #define PACKET_TYPE 0
27 #define PACKET_TYPE_SIZE 1
28 #define EVT_HEADER_TYPE 0
29 #define EVT_CMD_COMP_OP_LSB 3
30 #define EVT_CMD_COMP_OP_MSB 4
31 #define EVT_CMD_COMP_DATA 5
32
33 #define EVT_OK 0
34 #define EVT_DISCARD 1
35 #define EVT_NOP 2
36
37 #define BT_FEAT_SET_BIT(feat, octet, bit) (feat[octet] |= BIT(bit))
38 #define BT_FEAT_SET_NO_BREDR(feat) BT_FEAT_SET_BIT(feat, 4, 5)
39 #define BT_FEAT_SET_LE(feat) BT_FEAT_SET_BIT(feat, 4, 6)
40
41 /* Max SPI buffer length for transceive operations.
42 * The maximum TX packet number is 512 bytes data + 12 bytes header.
43 * The maximum RX packet number is 255 bytes data + 3 header.
44 */
45 #define SPI_MAX_TX_MSG_LEN 524
46 #define SPI_MAX_RX_MSG_LEN 258
47
48 /* The controller may be unavailable to receive packets because it is busy
49 * on processing something or have packets to send to host. Need to free the
50 * SPI bus and wait some moment to try again.
51 */
52 #define SPI_BUSY_WAIT_INTERVAL_MS 25
53 #define SPI_BUSY_TX_ATTEMPTS 200
54
55 static uint8_t __noinit rxmsg[SPI_MAX_RX_MSG_LEN];
56
57 static struct spi_dt_spec spi_bus =
58 SPI_DT_SPEC_INST_GET(0,
59 SPI_OP_MODE_MASTER | SPI_HALF_DUPLEX | SPI_TRANSFER_MSB |
60 SPI_MODE_CPOL | SPI_MODE_CPHA | SPI_WORD_SET(8),
61 0);
62
63 static K_KERNEL_STACK_DEFINE(spi_rx_stack, CONFIG_BT_DRV_RX_STACK_SIZE);
64 static struct k_thread spi_rx_thread_data;
65
66 static struct spi_buf spi_tx_buf;
67 static struct spi_buf spi_rx_buf;
68 static const struct spi_buf_set spi_tx = {.buffers = &spi_tx_buf, .count = 1};
69 static const struct spi_buf_set spi_rx = {.buffers = &spi_rx_buf, .count = 1};
70
71 static K_SEM_DEFINE(sem_irq, 0, 1);
72 static K_SEM_DEFINE(sem_spi_available, 1, 1);
73
74 struct bt_apollo_data {
75 bt_hci_recv_t recv;
76 };
77
bt_packet_irq_isr(const struct device * unused1,struct gpio_callback * unused2,uint32_t unused3)78 void bt_packet_irq_isr(const struct device *unused1, struct gpio_callback *unused2,
79 uint32_t unused3)
80 {
81 bt_apollo_rcv_isr_preprocess();
82 k_sem_give(&sem_irq);
83 }
84
bt_spi_transceive(void * tx,uint32_t tx_len,void * rx,uint32_t rx_len)85 static inline int bt_spi_transceive(void *tx, uint32_t tx_len, void *rx, uint32_t rx_len)
86 {
87 spi_tx_buf.buf = tx;
88 spi_tx_buf.len = (size_t)tx_len;
89 spi_rx_buf.buf = rx;
90 spi_rx_buf.len = (size_t)rx_len;
91
92 /* Before sending packet to controller the host needs to poll the status of
93 * controller to know it's ready, or before reading packets from controller
94 * the host needs to get the payload size of coming packets by sending specific
95 * command and putting the status or size to the rx buffer, the CS should be
96 * held at this moment to continue to send or receive packets.
97 */
98 if (tx_len && rx_len) {
99 spi_bus.config.operation |= SPI_HOLD_ON_CS;
100 } else {
101 spi_bus.config.operation &= ~SPI_HOLD_ON_CS;
102 }
103 return spi_transceive_dt(&spi_bus, &spi_tx, &spi_rx);
104 }
105
spi_send_packet(uint8_t * data,uint16_t len)106 static int spi_send_packet(uint8_t *data, uint16_t len)
107 {
108 int ret;
109 uint16_t fail_count = 0;
110
111 do {
112 /* Wait for SPI bus to be available */
113 k_sem_take(&sem_spi_available, K_FOREVER);
114
115 /* Send the SPI packet to controller */
116 ret = bt_apollo_spi_send(data, len, bt_spi_transceive);
117
118 /* Free the SPI bus */
119 k_sem_give(&sem_spi_available);
120
121 if (ret) {
122 /* Give some chance to controller to complete the processing or
123 * packets sending.
124 */
125 k_sleep(K_MSEC(SPI_BUSY_WAIT_INTERVAL_MS));
126 } else {
127 break;
128 }
129 } while (fail_count++ < SPI_BUSY_TX_ATTEMPTS);
130
131 return ret;
132 }
133
spi_receive_packet(uint8_t * data,uint16_t * len)134 static int spi_receive_packet(uint8_t *data, uint16_t *len)
135 {
136 int ret;
137
138 /* Wait for SPI bus to be available */
139 k_sem_take(&sem_spi_available, K_FOREVER);
140
141 /* Receive the SPI packet from controller */
142 ret = bt_apollo_spi_rcv(data, len, bt_spi_transceive);
143
144 /* Free the SPI bus */
145 k_sem_give(&sem_spi_available);
146
147 return ret;
148 }
149
hci_event_filter(const uint8_t * evt_data)150 static int hci_event_filter(const uint8_t *evt_data)
151 {
152 uint8_t evt_type = evt_data[EVT_HEADER_TYPE];
153
154 switch (evt_type) {
155 case BT_HCI_EVT_LE_META_EVENT: {
156 uint8_t subevt_type = evt_data[sizeof(struct bt_hci_evt_hdr)];
157
158 switch (subevt_type) {
159 case BT_HCI_EVT_LE_ADVERTISING_REPORT:
160 return EVT_DISCARD;
161 default:
162 return EVT_OK;
163 }
164 }
165 case BT_HCI_EVT_CMD_COMPLETE: {
166 uint16_t opcode = (uint16_t)(evt_data[EVT_CMD_COMP_OP_LSB] +
167 (evt_data[EVT_CMD_COMP_OP_MSB] << 8));
168
169 switch (opcode) {
170 case BT_OP_NOP:
171 return EVT_NOP;
172 case BT_HCI_OP_READ_LOCAL_FEATURES: {
173 /* The BLE controller of some Ambiq Apollox Blue SOC may have issue to
174 * report the expected supported features bitmask successfully, thought the
175 * features are actually supportive. Need to correct them before going to
176 * the host stack.
177 */
178 struct bt_hci_rp_read_local_features *rp =
179 (void *)&evt_data[EVT_CMD_COMP_DATA];
180 if (rp->status == 0) {
181 BT_FEAT_SET_NO_BREDR(rp->features);
182 BT_FEAT_SET_LE(rp->features);
183 }
184 return EVT_OK;
185 }
186 default:
187 return EVT_OK;
188 }
189 }
190 default:
191 return EVT_OK;
192 }
193 }
194
bt_hci_evt_recv(uint8_t * data,size_t len)195 static struct net_buf *bt_hci_evt_recv(uint8_t *data, size_t len)
196 {
197 int evt_filter;
198 bool discardable = false;
199 struct bt_hci_evt_hdr hdr = {0};
200 struct net_buf *buf;
201 size_t buf_tailroom;
202
203 if (len < sizeof(hdr)) {
204 LOG_ERR("Not enough data for event header");
205 return NULL;
206 }
207
208 evt_filter = hci_event_filter(data);
209 if (evt_filter == EVT_NOP) {
210 /* The controller sends NOP event when wakes up based on
211 * hardware specific requirement, do not post this event to
212 * host stack.
213 */
214 return NULL;
215 } else if (evt_filter == EVT_DISCARD) {
216 discardable = true;
217 }
218
219 memcpy((void *)&hdr, data, sizeof(hdr));
220 data += sizeof(hdr);
221 len -= sizeof(hdr);
222
223 if (len != hdr.len) {
224 LOG_ERR("Event payload length is not correct");
225 return NULL;
226 }
227
228 buf = bt_buf_get_evt(hdr.evt, discardable, K_NO_WAIT);
229 if (!buf) {
230 if (discardable) {
231 LOG_DBG("Discardable buffer pool full, ignoring event");
232 } else {
233 LOG_ERR("No available event buffers!");
234 }
235 return buf;
236 }
237
238 net_buf_add_mem(buf, &hdr, sizeof(hdr));
239
240 buf_tailroom = net_buf_tailroom(buf);
241 if (buf_tailroom < len) {
242 LOG_ERR("Not enough space in buffer %zu/%zu", len, buf_tailroom);
243 net_buf_unref(buf);
244 return NULL;
245 }
246
247 net_buf_add_mem(buf, data, len);
248
249 return buf;
250 }
251
bt_hci_acl_recv(uint8_t * data,size_t len)252 static struct net_buf *bt_hci_acl_recv(uint8_t *data, size_t len)
253 {
254 struct bt_hci_acl_hdr hdr = {0};
255 struct net_buf *buf;
256 size_t buf_tailroom;
257
258 if (len < sizeof(hdr)) {
259 LOG_ERR("Not enough data for ACL header");
260 return NULL;
261 }
262
263 buf = bt_buf_get_rx(BT_BUF_ACL_IN, K_NO_WAIT);
264 if (buf) {
265 memcpy((void *)&hdr, data, sizeof(hdr));
266 data += sizeof(hdr);
267 len -= sizeof(hdr);
268 } else {
269 LOG_ERR("No available ACL buffers!");
270 return NULL;
271 }
272
273 if (len != sys_le16_to_cpu(hdr.len)) {
274 LOG_ERR("ACL payload length is not correct");
275 net_buf_unref(buf);
276 return NULL;
277 }
278
279 net_buf_add_mem(buf, &hdr, sizeof(hdr));
280 buf_tailroom = net_buf_tailroom(buf);
281 if (buf_tailroom < len) {
282 LOG_ERR("Not enough space in buffer %zu/%zu", len, buf_tailroom);
283 net_buf_unref(buf);
284 return NULL;
285 }
286
287 net_buf_add_mem(buf, data, len);
288
289 return buf;
290 }
291
bt_spi_rx_thread(void * p1,void * p2,void * p3)292 static void bt_spi_rx_thread(void *p1, void *p2, void *p3)
293 {
294 const struct device *dev = p1;
295 struct bt_apollo_data *hci = dev->data;
296
297 ARG_UNUSED(p2);
298 ARG_UNUSED(p3);
299
300 struct net_buf *buf;
301 int ret;
302 uint16_t len = 0;
303
304 while (true) {
305 /* Wait for controller interrupt */
306 k_sem_take(&sem_irq, K_FOREVER);
307
308 do {
309 /* Recevive the HCI packet via SPI */
310 ret = spi_receive_packet(&rxmsg[0], &len);
311 if (ret) {
312 break;
313 }
314
315 /* Check if needs to handle the vendor specific events which are
316 * incompatible with the standard Bluetooth HCI format.
317 */
318 if (bt_apollo_vnd_rcv_ongoing(&rxmsg[0], len)) {
319 break;
320 }
321
322 switch (rxmsg[PACKET_TYPE]) {
323 case BT_HCI_H4_EVT:
324 buf = bt_hci_evt_recv(&rxmsg[PACKET_TYPE + PACKET_TYPE_SIZE],
325 (len - PACKET_TYPE_SIZE));
326 break;
327 case BT_HCI_H4_ACL:
328 buf = bt_hci_acl_recv(&rxmsg[PACKET_TYPE + PACKET_TYPE_SIZE],
329 (len - PACKET_TYPE_SIZE));
330 break;
331 default:
332 buf = NULL;
333 LOG_WRN("Unknown BT buf type %d", rxmsg[PACKET_TYPE]);
334 break;
335 }
336
337 /* Post the RX message to host stack to process */
338 if (buf) {
339 hci->recv(dev, buf);
340 }
341 } while (0);
342 }
343 }
344
bt_apollo_send(const struct device * dev,struct net_buf * buf)345 static int bt_apollo_send(const struct device *dev, struct net_buf *buf)
346 {
347 int ret = 0;
348
349 /* Buffer needs an additional byte for type */
350 if (buf->len >= SPI_MAX_TX_MSG_LEN) {
351 LOG_ERR("Message too long");
352 return -EINVAL;
353 }
354
355 switch (bt_buf_get_type(buf)) {
356 case BT_BUF_ACL_OUT:
357 net_buf_push_u8(buf, BT_HCI_H4_ACL);
358 break;
359 case BT_BUF_CMD:
360 net_buf_push_u8(buf, BT_HCI_H4_CMD);
361 break;
362 default:
363 LOG_ERR("Unsupported type");
364 net_buf_unref(buf);
365 return -EINVAL;
366 }
367
368 /* Send the SPI packet */
369 ret = spi_send_packet(buf->data, buf->len);
370
371 net_buf_unref(buf);
372
373 return ret;
374 }
375
bt_apollo_open(const struct device * dev,bt_hci_recv_t recv)376 static int bt_apollo_open(const struct device *dev, bt_hci_recv_t recv)
377 {
378 struct bt_apollo_data *hci = dev->data;
379 int ret;
380
381 ret = bt_hci_transport_setup(spi_bus.bus);
382 if (ret) {
383 return ret;
384 }
385
386 /* Start RX thread */
387 k_thread_create(&spi_rx_thread_data, spi_rx_stack, K_KERNEL_STACK_SIZEOF(spi_rx_stack),
388 (k_thread_entry_t)bt_spi_rx_thread, (void *)dev, NULL, NULL,
389 K_PRIO_COOP(CONFIG_BT_DRIVER_RX_HIGH_PRIO), 0, K_NO_WAIT);
390
391 ret = bt_apollo_controller_init(spi_send_packet);
392 if (ret == 0) {
393 hci->recv = recv;
394 }
395
396 return ret;
397 }
398
bt_apollo_close(const struct device * dev)399 static int bt_apollo_close(const struct device *dev)
400 {
401 int ret;
402 struct bt_apollo_data *hci = dev->data;
403
404 ret = bt_apollo_controller_deinit();
405 if (ret) {
406 return ret;
407 }
408
409 hci->recv = NULL;
410
411 return ret;
412 }
413
bt_apollo_setup(const struct device * dev,const struct bt_hci_setup_params * params)414 static int bt_apollo_setup(const struct device *dev, const struct bt_hci_setup_params *params)
415 {
416 ARG_UNUSED(params);
417
418 int ret;
419
420 ret = bt_apollo_vnd_setup();
421
422 return ret;
423 }
424
425 static DEVICE_API(bt_hci, drv) = {
426 .open = bt_apollo_open,
427 .close = bt_apollo_close,
428 .send = bt_apollo_send,
429 .setup = bt_apollo_setup,
430 };
431
bt_apollo_init(const struct device * dev)432 static int bt_apollo_init(const struct device *dev)
433 {
434 int ret;
435
436 ARG_UNUSED(dev);
437
438 if (!device_is_ready(spi_bus.bus)) {
439 LOG_ERR("SPI device not ready");
440 return -ENODEV;
441 }
442
443 ret = bt_apollo_dev_init();
444 if (ret) {
445 return ret;
446 }
447
448 LOG_DBG("BT HCI initialized");
449
450 return 0;
451 }
452
453 #define HCI_DEVICE_INIT(inst) \
454 static struct bt_apollo_data hci_data_##inst = {}; \
455 DEVICE_DT_INST_DEFINE(inst, bt_apollo_init, NULL, &hci_data_##inst, NULL, POST_KERNEL, \
456 CONFIG_BT_HCI_INIT_PRIORITY, &drv)
457
458 /* Only one instance supported right now */
459 HCI_DEVICE_INIT(0)
460