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/hci_driver.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 #define HCI_SPI_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(ambiq_bt_hci_spi)
26 #define SPI_DEV_NODE DT_BUS(HCI_SPI_NODE)
27 
28 #define HCI_CMD 0x01
29 #define HCI_ACL 0x02
30 #define HCI_SCO 0x03
31 #define HCI_EVT 0x04
32 
33 /* Offset of special item */
34 #define PACKET_TYPE         0
35 #define PACKET_TYPE_SIZE    1
36 #define EVT_HEADER_TYPE     0
37 #define EVT_HEADER_EVENT    1
38 #define EVT_HEADER_SIZE     2
39 #define EVT_VENDOR_CODE_LSB 3
40 #define EVT_VENDOR_CODE_MSB 4
41 #define CMD_OGF             1
42 #define CMD_OCF             2
43 
44 #define EVT_OK      0
45 #define EVT_DISCARD 1
46 #define EVT_NOP     2
47 
48 /* Max SPI buffer length for transceive operations.
49  * The maximum TX packet number is 512 bytes data + 12 bytes header.
50  * The maximum RX packet number is 255 bytes data + 3 header.
51  */
52 #define SPI_MAX_TX_MSG_LEN 524
53 #define SPI_MAX_RX_MSG_LEN 258
54 
55 static uint8_t __noinit rxmsg[SPI_MAX_RX_MSG_LEN];
56 static const struct device *spi_dev = DEVICE_DT_GET(SPI_DEV_NODE);
57 static struct spi_config spi_cfg = {
58 	.operation = SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB | SPI_MODE_CPOL | SPI_MODE_CPHA |
59 		     SPI_WORD_SET(8),
60 };
61 static K_KERNEL_STACK_DEFINE(spi_rx_stack, CONFIG_BT_DRV_RX_STACK_SIZE);
62 static struct k_thread spi_rx_thread_data;
63 
64 static struct spi_buf spi_tx_buf;
65 static struct spi_buf spi_rx_buf;
66 static const struct spi_buf_set spi_tx = {.buffers = &spi_tx_buf, .count = 1};
67 static const struct spi_buf_set spi_rx = {.buffers = &spi_rx_buf, .count = 1};
68 
69 static K_SEM_DEFINE(sem_irq, 0, 1);
70 static K_SEM_DEFINE(sem_spi_available, 1, 1);
71 
bt_packet_irq_isr(const struct device * unused1,struct gpio_callback * unused2,uint32_t unused3)72 void bt_packet_irq_isr(const struct device *unused1, struct gpio_callback *unused2,
73 		       uint32_t unused3)
74 {
75 	k_sem_give(&sem_irq);
76 }
77 
bt_spi_transceive(void * tx,uint32_t tx_len,void * rx,uint32_t rx_len)78 static inline int bt_spi_transceive(void *tx, uint32_t tx_len, void *rx, uint32_t rx_len)
79 {
80 	spi_tx_buf.buf = tx;
81 	spi_tx_buf.len = (size_t)tx_len;
82 	spi_rx_buf.buf = rx;
83 	spi_rx_buf.len = (size_t)rx_len;
84 	return spi_transceive(spi_dev, &spi_cfg, &spi_tx, &spi_rx);
85 }
86 
spi_send_packet(uint8_t * data,uint16_t len)87 static int spi_send_packet(uint8_t *data, uint16_t len)
88 {
89 	int ret;
90 
91 	/* Wait for SPI bus to be available */
92 	k_sem_take(&sem_spi_available, K_FOREVER);
93 
94 	/* Send the SPI packet to controller */
95 	ret = bt_apollo_spi_send(data, len, bt_spi_transceive);
96 
97 	/* Free the SPI bus */
98 	k_sem_give(&sem_spi_available);
99 
100 	return ret;
101 }
102 
spi_receive_packet(uint8_t * data,uint16_t * len)103 static int spi_receive_packet(uint8_t *data, uint16_t *len)
104 {
105 	int ret;
106 
107 	/* Wait for SPI bus to be available */
108 	k_sem_take(&sem_spi_available, K_FOREVER);
109 
110 	/* Receive the SPI packet from controller */
111 	ret = bt_apollo_spi_rcv(data, len, bt_spi_transceive);
112 
113 	/* Free the SPI bus */
114 	k_sem_give(&sem_spi_available);
115 
116 	return ret;
117 }
118 
hci_event_filter(const uint8_t * evt_data)119 static int hci_event_filter(const uint8_t *evt_data)
120 {
121 	uint8_t evt_type = evt_data[0];
122 
123 	switch (evt_type) {
124 	case BT_HCI_EVT_LE_META_EVENT: {
125 		uint8_t subevt_type = evt_data[sizeof(struct bt_hci_evt_hdr)];
126 
127 		switch (subevt_type) {
128 		case BT_HCI_EVT_LE_ADVERTISING_REPORT:
129 			return EVT_DISCARD;
130 		default:
131 			return EVT_OK;
132 		}
133 	}
134 	case BT_HCI_EVT_CMD_COMPLETE: {
135 		uint16_t opcode = (uint16_t)(evt_data[3] + (evt_data[4] << 8));
136 
137 		switch (opcode) {
138 		case BT_OP_NOP:
139 			return EVT_NOP;
140 		default:
141 			return EVT_OK;
142 		}
143 	}
144 	default:
145 		return EVT_OK;
146 	}
147 }
148 
bt_hci_evt_recv(uint8_t * data,size_t len)149 static struct net_buf *bt_hci_evt_recv(uint8_t *data, size_t len)
150 {
151 	int evt_filter;
152 	bool discardable = false;
153 	struct bt_hci_evt_hdr hdr = {0};
154 	struct net_buf *buf;
155 	size_t buf_tailroom;
156 
157 	if (len < sizeof(hdr)) {
158 		LOG_ERR("Not enough data for event header");
159 		return NULL;
160 	}
161 
162 	evt_filter = hci_event_filter(data);
163 	if (evt_filter == EVT_NOP) {
164 		/* The controller sends NOP event when wakes up based on
165 		 * hardware specific requirement, do not post this event to
166 		 * host stack.
167 		 */
168 		return NULL;
169 	} else if (evt_filter == EVT_DISCARD) {
170 		discardable = true;
171 	}
172 
173 	memcpy((void *)&hdr, data, sizeof(hdr));
174 	data += sizeof(hdr);
175 	len -= sizeof(hdr);
176 
177 	if (len != hdr.len) {
178 		LOG_ERR("Event payload length is not correct");
179 		return NULL;
180 	}
181 
182 	buf = bt_buf_get_evt(hdr.evt, discardable, K_NO_WAIT);
183 	if (!buf) {
184 		if (discardable) {
185 			LOG_DBG("Discardable buffer pool full, ignoring event");
186 		} else {
187 			LOG_ERR("No available event buffers!");
188 		}
189 		return buf;
190 	}
191 
192 	net_buf_add_mem(buf, &hdr, sizeof(hdr));
193 
194 	buf_tailroom = net_buf_tailroom(buf);
195 	if (buf_tailroom < len) {
196 		LOG_ERR("Not enough space in buffer %zu/%zu", len, buf_tailroom);
197 		net_buf_unref(buf);
198 		return NULL;
199 	}
200 
201 	net_buf_add_mem(buf, data, len);
202 
203 	return buf;
204 }
205 
bt_hci_acl_recv(uint8_t * data,size_t len)206 static struct net_buf *bt_hci_acl_recv(uint8_t *data, size_t len)
207 {
208 	struct bt_hci_acl_hdr hdr = {0};
209 	struct net_buf *buf;
210 	size_t buf_tailroom;
211 
212 	if (len < sizeof(hdr)) {
213 		LOG_ERR("Not enough data for ACL header");
214 		return NULL;
215 	}
216 
217 	buf = bt_buf_get_rx(BT_BUF_ACL_IN, K_NO_WAIT);
218 	if (buf) {
219 		memcpy((void *)&hdr, data, sizeof(hdr));
220 		data += sizeof(hdr);
221 		len -= sizeof(hdr);
222 	} else {
223 		LOG_ERR("No available ACL buffers!");
224 		return NULL;
225 	}
226 
227 	if (len != sys_le16_to_cpu(hdr.len)) {
228 		LOG_ERR("ACL payload length is not correct");
229 		net_buf_unref(buf);
230 		return NULL;
231 	}
232 
233 	net_buf_add_mem(buf, &hdr, sizeof(hdr));
234 	buf_tailroom = net_buf_tailroom(buf);
235 	if (buf_tailroom < len) {
236 		LOG_ERR("Not enough space in buffer %zu/%zu", len, buf_tailroom);
237 		net_buf_unref(buf);
238 		return NULL;
239 	}
240 
241 	net_buf_add_mem(buf, data, len);
242 
243 	return buf;
244 }
245 
bt_spi_rx_thread(void * p1,void * p2,void * p3)246 static void bt_spi_rx_thread(void *p1, void *p2, void *p3)
247 {
248 	ARG_UNUSED(p1);
249 	ARG_UNUSED(p2);
250 	ARG_UNUSED(p3);
251 
252 	struct net_buf *buf;
253 	int ret;
254 	uint16_t len = 0;
255 
256 	while (true) {
257 		/* Wait for controller interrupt */
258 		k_sem_take(&sem_irq, K_FOREVER);
259 
260 		do {
261 			/* Recevive the HCI packet via SPI */
262 			ret = spi_receive_packet(&rxmsg[0], &len);
263 			if (ret) {
264 				break;
265 			}
266 
267 			/* Check if needs to handle the vendor specific events which are
268 			 * incompatible with the standard Bluetooth HCI format.
269 			 */
270 			if (bt_apollo_vnd_rcv_ongoing(&rxmsg[0], len)) {
271 				break;
272 			}
273 
274 			switch (rxmsg[PACKET_TYPE]) {
275 			case HCI_EVT:
276 				buf = bt_hci_evt_recv(&rxmsg[PACKET_TYPE + PACKET_TYPE_SIZE],
277 						      (len - PACKET_TYPE_SIZE));
278 				break;
279 			case HCI_ACL:
280 				buf = bt_hci_acl_recv(&rxmsg[PACKET_TYPE + PACKET_TYPE_SIZE],
281 						      (len - PACKET_TYPE_SIZE));
282 				break;
283 			default:
284 				buf = NULL;
285 				LOG_WRN("Unknown BT buf type %d", rxmsg[PACKET_TYPE]);
286 				break;
287 			}
288 
289 			/* Post the RX message to host stack to process */
290 			if (buf) {
291 				bt_recv(buf);
292 			}
293 		} while (0);
294 	}
295 }
296 
bt_hci_send(struct net_buf * buf)297 static int bt_hci_send(struct net_buf *buf)
298 {
299 	int ret = 0;
300 
301 	/* Buffer needs an additional byte for type */
302 	if (buf->len >= SPI_MAX_TX_MSG_LEN) {
303 		LOG_ERR("Message too long");
304 		return -EINVAL;
305 	}
306 
307 	switch (bt_buf_get_type(buf)) {
308 	case BT_BUF_ACL_OUT:
309 		net_buf_push_u8(buf, HCI_ACL);
310 		break;
311 	case BT_BUF_CMD:
312 		net_buf_push_u8(buf, HCI_CMD);
313 		break;
314 	default:
315 		LOG_ERR("Unsupported type");
316 		net_buf_unref(buf);
317 		return -EINVAL;
318 	}
319 
320 	/* Send the SPI packet */
321 	ret = spi_send_packet(buf->data, buf->len);
322 
323 	net_buf_unref(buf);
324 
325 	return ret;
326 }
327 
bt_hci_open(void)328 static int bt_hci_open(void)
329 {
330 	int ret;
331 
332 	ret = bt_hci_transport_setup(spi_dev);
333 	if (ret) {
334 		return ret;
335 	}
336 
337 	/* Start RX thread */
338 	k_thread_create(&spi_rx_thread_data, spi_rx_stack, K_KERNEL_STACK_SIZEOF(spi_rx_stack),
339 			(k_thread_entry_t)bt_spi_rx_thread, NULL, NULL, NULL,
340 			K_PRIO_COOP(CONFIG_BT_DRIVER_RX_HIGH_PRIO), 0, K_NO_WAIT);
341 
342 	ret = bt_apollo_controller_init(spi_send_packet);
343 
344 	return ret;
345 }
346 
bt_spi_setup(const struct bt_hci_setup_params * params)347 static int bt_spi_setup(const struct bt_hci_setup_params *params)
348 {
349 	ARG_UNUSED(params);
350 
351 	int ret;
352 
353 	ret = bt_apollo_vnd_setup();
354 
355 	return ret;
356 }
357 
358 static const struct bt_hci_driver drv = {
359 	.name = "ambiq hci",
360 	.bus = BT_HCI_DRIVER_BUS_SPI,
361 	.open = bt_hci_open,
362 	.send = bt_hci_send,
363 	.setup = bt_spi_setup,
364 };
365 
bt_hci_init(void)366 static int bt_hci_init(void)
367 {
368 	int ret;
369 
370 	if (!device_is_ready(spi_dev)) {
371 		LOG_ERR("SPI device not ready");
372 		return -ENODEV;
373 	}
374 
375 	ret = bt_apollo_dev_init();
376 	if (ret) {
377 		return ret;
378 	}
379 
380 	bt_hci_driver_register(&drv);
381 
382 	LOG_DBG("BT HCI initialized");
383 
384 	return 0;
385 }
386 
387 SYS_INIT(bt_hci_init, POST_KERNEL, CONFIG_BT_HCI_INIT_PRIORITY);
388