1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Bluetooth HCI UART driver for marvell devices
6 * Copyright (C) 2016 Marvell International Ltd.
7 * Copyright (C) 2016 Intel Corporation
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/skbuff.h>
13 #include <linux/firmware.h>
14 #include <linux/module.h>
15 #include <linux/tty.h>
17 #include <linux/serdev.h>
19 #include <net/bluetooth/bluetooth.h>
20 #include <net/bluetooth/hci_core.h>
24 #define HCI_FW_REQ_PKT 0xA5
25 #define HCI_CHIP_VER_PKT 0xAA
29 #define MRVL_RAW_DATA 0x1F
30 #define MRVL_SET_BAUDRATE 0xFC09
33 STATE_CHIP_VER_PENDING
,
39 struct sk_buff
*rx_skb
;
40 struct sk_buff_head txq
;
41 struct sk_buff_head rawq
;
55 #define HCI_MRVL_PKT_SIZE 4
57 static int mrvl_open(struct hci_uart
*hu
)
59 struct mrvl_data
*mrvl
;
64 if (!hci_uart_has_flow_control(hu
))
67 mrvl
= kzalloc(sizeof(*mrvl
), GFP_KERNEL
);
71 skb_queue_head_init(&mrvl
->txq
);
72 skb_queue_head_init(&mrvl
->rawq
);
74 set_bit(STATE_CHIP_VER_PENDING
, &mrvl
->flags
);
79 ret
= serdev_device_open(hu
->serdev
);
91 static int mrvl_close(struct hci_uart
*hu
)
93 struct mrvl_data
*mrvl
= hu
->priv
;
98 serdev_device_close(hu
->serdev
);
100 skb_queue_purge(&mrvl
->txq
);
101 skb_queue_purge(&mrvl
->rawq
);
102 kfree_skb(mrvl
->rx_skb
);
109 static int mrvl_flush(struct hci_uart
*hu
)
111 struct mrvl_data
*mrvl
= hu
->priv
;
115 skb_queue_purge(&mrvl
->txq
);
116 skb_queue_purge(&mrvl
->rawq
);
121 static struct sk_buff
*mrvl_dequeue(struct hci_uart
*hu
)
123 struct mrvl_data
*mrvl
= hu
->priv
;
126 skb
= skb_dequeue(&mrvl
->txq
);
129 skb
= skb_dequeue(&mrvl
->rawq
);
131 /* Prepend skb with frame type */
132 memcpy(skb_push(skb
, 1), &bt_cb(skb
)->pkt_type
, 1);
138 static int mrvl_enqueue(struct hci_uart
*hu
, struct sk_buff
*skb
)
140 struct mrvl_data
*mrvl
= hu
->priv
;
142 skb_queue_tail(&mrvl
->txq
, skb
);
146 static void mrvl_send_ack(struct hci_uart
*hu
, unsigned char type
)
148 struct mrvl_data
*mrvl
= hu
->priv
;
151 /* No H4 payload, only 1 byte header */
152 skb
= bt_skb_alloc(0, GFP_ATOMIC
);
154 bt_dev_err(hu
->hdev
, "Unable to alloc ack/nak packet");
157 hci_skb_pkt_type(skb
) = type
;
159 skb_queue_tail(&mrvl
->txq
, skb
);
160 hci_uart_tx_wakeup(hu
);
163 static int mrvl_recv_fw_req(struct hci_dev
*hdev
, struct sk_buff
*skb
)
165 struct hci_mrvl_pkt
*pkt
= (void *)skb
->data
;
166 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
167 struct mrvl_data
*mrvl
= hu
->priv
;
170 if ((pkt
->lhs
^ pkt
->rhs
) != 0xffff) {
171 bt_dev_err(hdev
, "Corrupted mrvl header");
172 mrvl_send_ack(hu
, MRVL_NAK
);
176 mrvl_send_ack(hu
, MRVL_ACK
);
178 if (!test_bit(STATE_FW_REQ_PENDING
, &mrvl
->flags
)) {
179 bt_dev_err(hdev
, "Received unexpected firmware request");
184 mrvl
->tx_len
= le16_to_cpu(pkt
->lhs
);
186 clear_bit(STATE_FW_REQ_PENDING
, &mrvl
->flags
);
187 smp_mb__after_atomic();
188 wake_up_bit(&mrvl
->flags
, STATE_FW_REQ_PENDING
);
195 static int mrvl_recv_chip_ver(struct hci_dev
*hdev
, struct sk_buff
*skb
)
197 struct hci_mrvl_pkt
*pkt
= (void *)skb
->data
;
198 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
199 struct mrvl_data
*mrvl
= hu
->priv
;
200 u16 version
= le16_to_cpu(pkt
->lhs
);
203 if ((pkt
->lhs
^ pkt
->rhs
) != 0xffff) {
204 bt_dev_err(hdev
, "Corrupted mrvl header");
205 mrvl_send_ack(hu
, MRVL_NAK
);
209 mrvl_send_ack(hu
, MRVL_ACK
);
211 if (!test_bit(STATE_CHIP_VER_PENDING
, &mrvl
->flags
)) {
212 bt_dev_err(hdev
, "Received unexpected chip version");
217 mrvl
->rev
= version
>> 8;
219 bt_dev_info(hdev
, "Controller id = %x, rev = %x", mrvl
->id
, mrvl
->rev
);
221 clear_bit(STATE_CHIP_VER_PENDING
, &mrvl
->flags
);
222 smp_mb__after_atomic();
223 wake_up_bit(&mrvl
->flags
, STATE_CHIP_VER_PENDING
);
230 #define HCI_RECV_CHIP_VER \
231 .type = HCI_CHIP_VER_PKT, \
232 .hlen = HCI_MRVL_PKT_SIZE, \
235 .maxlen = HCI_MRVL_PKT_SIZE
237 #define HCI_RECV_FW_REQ \
238 .type = HCI_FW_REQ_PKT, \
239 .hlen = HCI_MRVL_PKT_SIZE, \
242 .maxlen = HCI_MRVL_PKT_SIZE
244 static const struct h4_recv_pkt mrvl_recv_pkts
[] = {
245 { H4_RECV_ACL
, .recv
= hci_recv_frame
},
246 { H4_RECV_SCO
, .recv
= hci_recv_frame
},
247 { H4_RECV_EVENT
, .recv
= hci_recv_frame
},
248 { HCI_RECV_FW_REQ
, .recv
= mrvl_recv_fw_req
},
249 { HCI_RECV_CHIP_VER
, .recv
= mrvl_recv_chip_ver
},
252 static int mrvl_recv(struct hci_uart
*hu
, const void *data
, int count
)
254 struct mrvl_data
*mrvl
= hu
->priv
;
256 if (!test_bit(HCI_UART_REGISTERED
, &hu
->flags
))
259 /* We might receive some noise when there is no firmware loaded. Therefore,
260 * we drop data if the firmware is not loaded yet and if there is no fw load
263 if (!test_bit(STATE_FW_REQ_PENDING
, &mrvl
->flags
) &&
264 !test_bit(STATE_FW_LOADED
, &mrvl
->flags
))
267 mrvl
->rx_skb
= h4_recv_buf(hu
->hdev
, mrvl
->rx_skb
, data
, count
,
269 ARRAY_SIZE(mrvl_recv_pkts
));
270 if (IS_ERR(mrvl
->rx_skb
)) {
271 int err
= PTR_ERR(mrvl
->rx_skb
);
272 bt_dev_err(hu
->hdev
, "Frame reassembly failed (%d)", err
);
280 static int mrvl_load_firmware(struct hci_dev
*hdev
, const char *name
)
282 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
283 struct mrvl_data
*mrvl
= hu
->priv
;
284 const struct firmware
*fw
= NULL
;
285 const u8
*fw_ptr
, *fw_max
;
288 err
= request_firmware(&fw
, name
, &hdev
->dev
);
290 bt_dev_err(hdev
, "Failed to load firmware file %s", name
);
295 fw_max
= fw
->data
+ fw
->size
;
297 bt_dev_info(hdev
, "Loading %s", name
);
299 set_bit(STATE_FW_REQ_PENDING
, &mrvl
->flags
);
301 while (fw_ptr
<= fw_max
) {
304 /* Controller drives the firmware load by sending firmware
305 * request packets containing the expected fragment size.
307 err
= wait_on_bit_timeout(&mrvl
->flags
, STATE_FW_REQ_PENDING
,
309 msecs_to_jiffies(2000));
311 bt_dev_err(hdev
, "Firmware load interrupted");
315 bt_dev_err(hdev
, "Firmware request timeout");
320 bt_dev_dbg(hdev
, "Firmware request, expecting %d bytes",
323 if (fw_ptr
== fw_max
) {
324 /* Controller requests a null size once firmware is
325 * fully loaded. If controller expects more data, there
329 bt_dev_info(hdev
, "Firmware loading complete");
331 bt_dev_err(hdev
, "Firmware loading failure");
337 if (fw_ptr
+ mrvl
->tx_len
> fw_max
) {
338 mrvl
->tx_len
= fw_max
- fw_ptr
;
339 bt_dev_dbg(hdev
, "Adjusting tx_len to %d",
343 skb
= bt_skb_alloc(mrvl
->tx_len
, GFP_KERNEL
);
345 bt_dev_err(hdev
, "Failed to alloc mem for FW packet");
349 bt_cb(skb
)->pkt_type
= MRVL_RAW_DATA
;
351 skb_put_data(skb
, fw_ptr
, mrvl
->tx_len
);
352 fw_ptr
+= mrvl
->tx_len
;
354 set_bit(STATE_FW_REQ_PENDING
, &mrvl
->flags
);
356 skb_queue_tail(&mrvl
->rawq
, skb
);
357 hci_uart_tx_wakeup(hu
);
360 release_firmware(fw
);
364 static int mrvl_setup(struct hci_uart
*hu
)
367 struct mrvl_data
*mrvl
= hu
->priv
;
369 hci_uart_set_flow_control(hu
, true);
371 err
= mrvl_load_firmware(hu
->hdev
, "mrvl/helper_uart_3000000.bin");
373 bt_dev_err(hu
->hdev
, "Unable to download firmware helper");
377 /* Let the final ack go out before switching the baudrate */
378 hci_uart_wait_until_sent(hu
);
381 serdev_device_set_baudrate(hu
->serdev
, hu
->oper_speed
);
383 hci_uart_set_baudrate(hu
, hu
->oper_speed
);
385 hci_uart_set_flow_control(hu
, false);
387 err
= mrvl_load_firmware(hu
->hdev
, "mrvl/uart8897_bt.bin");
391 set_bit(STATE_FW_LOADED
, &mrvl
->flags
);
396 static int mrvl_set_baudrate(struct hci_uart
*hu
, unsigned int speed
)
399 struct mrvl_data
*mrvl
= hu
->priv
;
400 __le32 speed_le
= cpu_to_le32(speed
);
402 /* The firmware might be loaded by the Wifi driver over SDIO. We wait
403 * up to 10s for the CTS to go up. Afterward, we know that the firmware
406 err
= serdev_device_wait_for_cts(hu
->serdev
, true, 10000);
408 bt_dev_err(hu
->hdev
, "Wait for CTS failed with %d\n", err
);
412 set_bit(STATE_FW_LOADED
, &mrvl
->flags
);
414 err
= __hci_cmd_sync_status(hu
->hdev
, MRVL_SET_BAUDRATE
,
415 sizeof(speed_le
), &speed_le
,
418 bt_dev_err(hu
->hdev
, "send command failed: %d", err
);
422 serdev_device_set_baudrate(hu
->serdev
, speed
);
424 /* We forcefully have to send a command to the bluetooth module so that
425 * the driver detects it after a baudrate change. This is foreseen by
426 * hci_serdev by setting HCI_UART_VND_DETECT which then causes a dummy
427 * local version read.
429 set_bit(HCI_UART_VND_DETECT
, &hu
->hdev_flags
);
434 static const struct hci_uart_proto mrvl_proto_8897
= {
437 .init_speed
= 115200,
438 .oper_speed
= 3000000,
444 .enqueue
= mrvl_enqueue
,
445 .dequeue
= mrvl_dequeue
,
448 static const struct hci_uart_proto mrvl_proto_8997
= {
450 .name
= "Marvell 8997",
451 .init_speed
= 115200,
452 .oper_speed
= 3000000,
456 .set_baudrate
= mrvl_set_baudrate
,
458 .enqueue
= mrvl_enqueue
,
459 .dequeue
= mrvl_dequeue
,
462 static int mrvl_serdev_probe(struct serdev_device
*serdev
)
464 struct mrvl_serdev
*mrvldev
;
465 const struct hci_uart_proto
*mrvl_proto
= device_get_match_data(&serdev
->dev
);
467 mrvldev
= devm_kzalloc(&serdev
->dev
, sizeof(*mrvldev
), GFP_KERNEL
);
471 mrvldev
->hu
.oper_speed
= mrvl_proto
->oper_speed
;
472 if (mrvl_proto
->set_baudrate
)
473 of_property_read_u32(serdev
->dev
.of_node
, "max-speed", &mrvldev
->hu
.oper_speed
);
475 mrvldev
->hu
.serdev
= serdev
;
476 serdev_device_set_drvdata(serdev
, mrvldev
);
478 return hci_uart_register_device(&mrvldev
->hu
, mrvl_proto
);
481 static void mrvl_serdev_remove(struct serdev_device
*serdev
)
483 struct mrvl_serdev
*mrvldev
= serdev_device_get_drvdata(serdev
);
485 hci_uart_unregister_device(&mrvldev
->hu
);
488 static const struct of_device_id __maybe_unused mrvl_bluetooth_of_match
[] = {
489 { .compatible
= "mrvl,88w8897", .data
= &mrvl_proto_8897
},
490 { .compatible
= "mrvl,88w8997", .data
= &mrvl_proto_8997
},
493 MODULE_DEVICE_TABLE(of
, mrvl_bluetooth_of_match
);
495 static struct serdev_device_driver mrvl_serdev_driver
= {
496 .probe
= mrvl_serdev_probe
,
497 .remove
= mrvl_serdev_remove
,
499 .name
= "hci_uart_mrvl",
500 .of_match_table
= of_match_ptr(mrvl_bluetooth_of_match
),
504 int __init
mrvl_init(void)
506 serdev_device_driver_register(&mrvl_serdev_driver
);
508 return hci_uart_register_proto(&mrvl_proto_8897
);
511 int __exit
mrvl_deinit(void)
513 serdev_device_driver_unregister(&mrvl_serdev_driver
);
515 return hci_uart_unregister_proto(&mrvl_proto_8897
);