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
32 STATE_CHIP_VER_PENDING,
37 struct sk_buff *rx_skb;
38 struct sk_buff_head txq;
39 struct sk_buff_head rawq;
53 #define HCI_MRVL_PKT_SIZE 4
55 static int mrvl_open(struct hci_uart *hu)
57 struct mrvl_data *mrvl;
62 mrvl = kzalloc(sizeof(*mrvl), GFP_KERNEL);
66 skb_queue_head_init(&mrvl->txq);
67 skb_queue_head_init(&mrvl->rawq);
69 set_bit(STATE_CHIP_VER_PENDING, &mrvl->flags);
74 ret = serdev_device_open(hu->serdev);
86 static int mrvl_close(struct hci_uart *hu)
88 struct mrvl_data *mrvl = hu->priv;
93 serdev_device_close(hu->serdev);
95 skb_queue_purge(&mrvl->txq);
96 skb_queue_purge(&mrvl->rawq);
97 kfree_skb(mrvl->rx_skb);
104 static int mrvl_flush(struct hci_uart *hu)
106 struct mrvl_data *mrvl = hu->priv;
110 skb_queue_purge(&mrvl->txq);
111 skb_queue_purge(&mrvl->rawq);
116 static struct sk_buff *mrvl_dequeue(struct hci_uart *hu)
118 struct mrvl_data *mrvl = hu->priv;
121 skb = skb_dequeue(&mrvl->txq);
124 skb = skb_dequeue(&mrvl->rawq);
126 /* Prepend skb with frame type */
127 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
133 static int mrvl_enqueue(struct hci_uart *hu, struct sk_buff *skb)
135 struct mrvl_data *mrvl = hu->priv;
137 skb_queue_tail(&mrvl->txq, skb);
141 static void mrvl_send_ack(struct hci_uart *hu, unsigned char type)
143 struct mrvl_data *mrvl = hu->priv;
146 /* No H4 payload, only 1 byte header */
147 skb = bt_skb_alloc(0, GFP_ATOMIC);
149 bt_dev_err(hu->hdev, "Unable to alloc ack/nak packet");
152 hci_skb_pkt_type(skb) = type;
154 skb_queue_tail(&mrvl->txq, skb);
155 hci_uart_tx_wakeup(hu);
158 static int mrvl_recv_fw_req(struct hci_dev *hdev, struct sk_buff *skb)
160 struct hci_mrvl_pkt *pkt = (void *)skb->data;
161 struct hci_uart *hu = hci_get_drvdata(hdev);
162 struct mrvl_data *mrvl = hu->priv;
165 if ((pkt->lhs ^ pkt->rhs) != 0xffff) {
166 bt_dev_err(hdev, "Corrupted mrvl header");
167 mrvl_send_ack(hu, MRVL_NAK);
171 mrvl_send_ack(hu, MRVL_ACK);
173 if (!test_bit(STATE_FW_REQ_PENDING, &mrvl->flags)) {
174 bt_dev_err(hdev, "Received unexpected firmware request");
179 mrvl->tx_len = le16_to_cpu(pkt->lhs);
181 clear_bit(STATE_FW_REQ_PENDING, &mrvl->flags);
182 smp_mb__after_atomic();
183 wake_up_bit(&mrvl->flags, STATE_FW_REQ_PENDING);
190 static int mrvl_recv_chip_ver(struct hci_dev *hdev, struct sk_buff *skb)
192 struct hci_mrvl_pkt *pkt = (void *)skb->data;
193 struct hci_uart *hu = hci_get_drvdata(hdev);
194 struct mrvl_data *mrvl = hu->priv;
195 u16 version = le16_to_cpu(pkt->lhs);
198 if ((pkt->lhs ^ pkt->rhs) != 0xffff) {
199 bt_dev_err(hdev, "Corrupted mrvl header");
200 mrvl_send_ack(hu, MRVL_NAK);
204 mrvl_send_ack(hu, MRVL_ACK);
206 if (!test_bit(STATE_CHIP_VER_PENDING, &mrvl->flags)) {
207 bt_dev_err(hdev, "Received unexpected chip version");
212 mrvl->rev = version >> 8;
214 bt_dev_info(hdev, "Controller id = %x, rev = %x", mrvl->id, mrvl->rev);
216 clear_bit(STATE_CHIP_VER_PENDING, &mrvl->flags);
217 smp_mb__after_atomic();
218 wake_up_bit(&mrvl->flags, STATE_CHIP_VER_PENDING);
225 #define HCI_RECV_CHIP_VER \
226 .type = HCI_CHIP_VER_PKT, \
227 .hlen = HCI_MRVL_PKT_SIZE, \
230 .maxlen = HCI_MRVL_PKT_SIZE
232 #define HCI_RECV_FW_REQ \
233 .type = HCI_FW_REQ_PKT, \
234 .hlen = HCI_MRVL_PKT_SIZE, \
237 .maxlen = HCI_MRVL_PKT_SIZE
239 static const struct h4_recv_pkt mrvl_recv_pkts[] = {
240 { H4_RECV_ACL, .recv = hci_recv_frame },
241 { H4_RECV_SCO, .recv = hci_recv_frame },
242 { H4_RECV_EVENT, .recv = hci_recv_frame },
243 { HCI_RECV_FW_REQ, .recv = mrvl_recv_fw_req },
244 { HCI_RECV_CHIP_VER, .recv = mrvl_recv_chip_ver },
247 static int mrvl_recv(struct hci_uart *hu, const void *data, int count)
249 struct mrvl_data *mrvl = hu->priv;
251 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
254 mrvl->rx_skb = h4_recv_buf(hu->hdev, mrvl->rx_skb, data, count,
256 ARRAY_SIZE(mrvl_recv_pkts));
257 if (IS_ERR(mrvl->rx_skb)) {
258 int err = PTR_ERR(mrvl->rx_skb);
259 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
267 static int mrvl_load_firmware(struct hci_dev *hdev, const char *name)
269 struct hci_uart *hu = hci_get_drvdata(hdev);
270 struct mrvl_data *mrvl = hu->priv;
271 const struct firmware *fw = NULL;
272 const u8 *fw_ptr, *fw_max;
275 err = request_firmware(&fw, name, &hdev->dev);
277 bt_dev_err(hdev, "Failed to load firmware file %s", name);
282 fw_max = fw->data + fw->size;
284 bt_dev_info(hdev, "Loading %s", name);
286 set_bit(STATE_FW_REQ_PENDING, &mrvl->flags);
288 while (fw_ptr <= fw_max) {
291 /* Controller drives the firmware load by sending firmware
292 * request packets containing the expected fragment size.
294 err = wait_on_bit_timeout(&mrvl->flags, STATE_FW_REQ_PENDING,
296 msecs_to_jiffies(2000));
298 bt_dev_err(hdev, "Firmware load interrupted");
302 bt_dev_err(hdev, "Firmware request timeout");
307 bt_dev_dbg(hdev, "Firmware request, expecting %d bytes",
310 if (fw_ptr == fw_max) {
311 /* Controller requests a null size once firmware is
312 * fully loaded. If controller expects more data, there
316 bt_dev_info(hdev, "Firmware loading complete");
318 bt_dev_err(hdev, "Firmware loading failure");
324 if (fw_ptr + mrvl->tx_len > fw_max) {
325 mrvl->tx_len = fw_max - fw_ptr;
326 bt_dev_dbg(hdev, "Adjusting tx_len to %d",
330 skb = bt_skb_alloc(mrvl->tx_len, GFP_KERNEL);
332 bt_dev_err(hdev, "Failed to alloc mem for FW packet");
336 bt_cb(skb)->pkt_type = MRVL_RAW_DATA;
338 skb_put_data(skb, fw_ptr, mrvl->tx_len);
339 fw_ptr += mrvl->tx_len;
341 set_bit(STATE_FW_REQ_PENDING, &mrvl->flags);
343 skb_queue_tail(&mrvl->rawq, skb);
344 hci_uart_tx_wakeup(hu);
347 release_firmware(fw);
351 static int mrvl_setup(struct hci_uart *hu)
355 hci_uart_set_flow_control(hu, true);
357 err = mrvl_load_firmware(hu->hdev, "mrvl/helper_uart_3000000.bin");
359 bt_dev_err(hu->hdev, "Unable to download firmware helper");
363 /* Let the final ack go out before switching the baudrate */
364 hci_uart_wait_until_sent(hu);
367 serdev_device_set_baudrate(hu->serdev, 3000000);
369 hci_uart_set_baudrate(hu, 3000000);
371 hci_uart_set_flow_control(hu, false);
373 err = mrvl_load_firmware(hu->hdev, "mrvl/uart8897_bt.bin");
380 static const struct hci_uart_proto mrvl_proto = {
383 .init_speed = 115200,
389 .enqueue = mrvl_enqueue,
390 .dequeue = mrvl_dequeue,
393 static int mrvl_serdev_probe(struct serdev_device *serdev)
395 struct mrvl_serdev *mrvldev;
397 mrvldev = devm_kzalloc(&serdev->dev, sizeof(*mrvldev), GFP_KERNEL);
401 mrvldev->hu.serdev = serdev;
402 serdev_device_set_drvdata(serdev, mrvldev);
404 return hci_uart_register_device(&mrvldev->hu, &mrvl_proto);
407 static void mrvl_serdev_remove(struct serdev_device *serdev)
409 struct mrvl_serdev *mrvldev = serdev_device_get_drvdata(serdev);
411 hci_uart_unregister_device(&mrvldev->hu);
415 static const struct of_device_id mrvl_bluetooth_of_match[] = {
416 { .compatible = "mrvl,88w8897" },
419 MODULE_DEVICE_TABLE(of, mrvl_bluetooth_of_match);
422 static struct serdev_device_driver mrvl_serdev_driver = {
423 .probe = mrvl_serdev_probe,
424 .remove = mrvl_serdev_remove,
426 .name = "hci_uart_mrvl",
427 .of_match_table = of_match_ptr(mrvl_bluetooth_of_match),
431 int __init mrvl_init(void)
433 serdev_device_driver_register(&mrvl_serdev_driver);
435 return hci_uart_register_proto(&mrvl_proto);
438 int __exit mrvl_deinit(void)
440 serdev_device_driver_unregister(&mrvl_serdev_driver);
442 return hci_uart_unregister_proto(&mrvl_proto);