1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018 MediaTek Inc.
5 * Bluetooth support for MediaTek serial devices
7 * Author: Sean Wang <sean.wang@mediatek.com>
11 #include <asm/unaligned.h>
12 #include <linux/atomic.h>
13 #include <linux/clk.h>
14 #include <linux/firmware.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/serdev.h>
20 #include <linux/skbuff.h>
22 #include <net/bluetooth/bluetooth.h>
23 #include <net/bluetooth/hci_core.h>
29 #define FIRMWARE_MT7622 "mediatek/mt7622pr2h.bin"
31 #define MTK_STP_TLR_SIZE 2
33 #define BTMTKUART_TX_STATE_ACTIVE 1
34 #define BTMTKUART_TX_STATE_WAKEUP 2
35 #define BTMTKUART_TX_WAIT_VND_EVT 3
38 MTK_WMT_PATCH_DWNLD
= 0x1,
39 MTK_WMT_FUNC_CTRL
= 0x6,
56 struct mtk_hci_wmt_cmd
{
57 struct mtk_wmt_hdr hdr
;
61 struct btmtkuart_dev
{
63 struct serdev_device
*serdev
;
66 struct work_struct tx_work
;
67 unsigned long tx_state
;
68 struct sk_buff_head txq
;
70 struct sk_buff
*rx_skb
;
77 static int mtk_hci_wmt_sync(struct hci_dev
*hdev
, u8 op
, u8 flag
, u16 plen
,
80 struct btmtkuart_dev
*bdev
= hci_get_drvdata(hdev
);
81 struct mtk_hci_wmt_cmd wc
;
82 struct mtk_wmt_hdr
*hdr
;
86 hlen
= sizeof(*hdr
) + plen
;
90 hdr
= (struct mtk_wmt_hdr
*)&wc
;
93 hdr
->dlen
= cpu_to_le16(plen
+ 1);
95 memcpy(wc
.data
, param
, plen
);
97 set_bit(BTMTKUART_TX_WAIT_VND_EVT
, &bdev
->tx_state
);
99 err
= __hci_cmd_send(hdev
, 0xfc6f, hlen
, &wc
);
101 clear_bit(BTMTKUART_TX_WAIT_VND_EVT
, &bdev
->tx_state
);
105 /* The vendor specific WMT commands are all answered by a vendor
106 * specific event and will not have the Command Status or Command
107 * Complete as with usual HCI command flow control.
109 * After sending the command, wait for BTMTKUART_TX_WAIT_VND_EVT
110 * state to be cleared. The driver speicfic event receive routine
111 * will clear that state and with that indicate completion of the
114 err
= wait_on_bit_timeout(&bdev
->tx_state
, BTMTKUART_TX_WAIT_VND_EVT
,
115 TASK_INTERRUPTIBLE
, HCI_INIT_TIMEOUT
);
117 bt_dev_err(hdev
, "Execution of wmt command interrupted");
118 clear_bit(BTMTKUART_TX_WAIT_VND_EVT
, &bdev
->tx_state
);
123 bt_dev_err(hdev
, "Execution of wmt command timed out");
124 clear_bit(BTMTKUART_TX_WAIT_VND_EVT
, &bdev
->tx_state
);
131 static int mtk_setup_fw(struct hci_dev
*hdev
)
133 const struct firmware
*fw
;
139 err
= request_firmware(&fw
, FIRMWARE_MT7622
, &hdev
->dev
);
141 bt_dev_err(hdev
, "Failed to load firmware file (%d)", err
);
148 /* The size of patch header is 30 bytes, should be skip */
158 while (fw_size
> 0) {
159 dlen
= min_t(int, 250, fw_size
);
161 /* Tell device the position in sequence */
162 if (fw_size
- dlen
<= 0)
164 else if (fw_size
< fw
->size
- 30)
167 err
= mtk_hci_wmt_sync(hdev
, MTK_WMT_PATCH_DWNLD
, flag
, dlen
,
170 bt_dev_err(hdev
, "Failed to send wmt patch dwnld (%d)",
180 release_firmware(fw
);
184 static int btmtkuart_recv_event(struct hci_dev
*hdev
, struct sk_buff
*skb
)
186 struct btmtkuart_dev
*bdev
= hci_get_drvdata(hdev
);
187 struct hci_event_hdr
*hdr
= (void *)skb
->data
;
190 /* Fix up the vendor event id with 0xff for vendor specific instead
191 * of 0xe4 so that event send via monitoring socket can be parsed
194 if (hdr
->evt
== 0xe4)
195 hdr
->evt
= HCI_EV_VENDOR
;
197 err
= hci_recv_frame(hdev
, skb
);
199 if (hdr
->evt
== HCI_EV_VENDOR
) {
200 if (test_and_clear_bit(BTMTKUART_TX_WAIT_VND_EVT
,
202 /* Barrier to sync with other CPUs */
203 smp_mb__after_atomic();
204 wake_up_bit(&bdev
->tx_state
, BTMTKUART_TX_WAIT_VND_EVT
);
211 static const struct h4_recv_pkt mtk_recv_pkts
[] = {
212 { H4_RECV_ACL
, .recv
= hci_recv_frame
},
213 { H4_RECV_SCO
, .recv
= hci_recv_frame
},
214 { H4_RECV_EVENT
, .recv
= btmtkuart_recv_event
},
217 static void btmtkuart_tx_work(struct work_struct
*work
)
219 struct btmtkuart_dev
*bdev
= container_of(work
, struct btmtkuart_dev
,
221 struct serdev_device
*serdev
= bdev
->serdev
;
222 struct hci_dev
*hdev
= bdev
->hdev
;
225 clear_bit(BTMTKUART_TX_STATE_WAKEUP
, &bdev
->tx_state
);
228 struct sk_buff
*skb
= skb_dequeue(&bdev
->txq
);
234 len
= serdev_device_write_buf(serdev
, skb
->data
,
236 hdev
->stat
.byte_tx
+= len
;
240 skb_queue_head(&bdev
->txq
, skb
);
244 switch (hci_skb_pkt_type(skb
)) {
245 case HCI_COMMAND_PKT
:
248 case HCI_ACLDATA_PKT
:
251 case HCI_SCODATA_PKT
:
259 if (!test_bit(BTMTKUART_TX_STATE_WAKEUP
, &bdev
->tx_state
))
263 clear_bit(BTMTKUART_TX_STATE_ACTIVE
, &bdev
->tx_state
);
266 static void btmtkuart_tx_wakeup(struct btmtkuart_dev
*bdev
)
268 if (test_and_set_bit(BTMTKUART_TX_STATE_ACTIVE
, &bdev
->tx_state
))
269 set_bit(BTMTKUART_TX_STATE_WAKEUP
, &bdev
->tx_state
);
271 schedule_work(&bdev
->tx_work
);
274 static const unsigned char *
275 mtk_stp_split(struct btmtkuart_dev
*bdev
, const unsigned char *data
, int count
,
278 struct mtk_stp_hdr
*shdr
;
280 /* The cursor is reset when all the data of STP is consumed out */
281 if (!bdev
->stp_dlen
&& bdev
->stp_cursor
>= 6)
282 bdev
->stp_cursor
= 0;
284 /* Filling pad until all STP info is obtained */
285 while (bdev
->stp_cursor
< 6 && count
> 0) {
286 bdev
->stp_pad
[bdev
->stp_cursor
] = *data
;
292 /* Retrieve STP info and have a sanity check */
293 if (!bdev
->stp_dlen
&& bdev
->stp_cursor
>= 6) {
294 shdr
= (struct mtk_stp_hdr
*)&bdev
->stp_pad
[2];
295 bdev
->stp_dlen
= be16_to_cpu(shdr
->dlen
) & 0x0fff;
297 /* Resync STP when unexpected data is being read */
298 if (shdr
->prefix
!= 0x80 || bdev
->stp_dlen
> 2048) {
299 bt_dev_err(bdev
->hdev
, "stp format unexpect (%d, %d)",
300 shdr
->prefix
, bdev
->stp_dlen
);
301 bdev
->stp_cursor
= 2;
306 /* Directly quit when there's no data found for H4 can process */
310 /* Tranlate to how much the size of data H4 can handle so far */
311 *sz_h4
= min_t(int, count
, bdev
->stp_dlen
);
313 /* Update the remaining size of STP packet */
314 bdev
->stp_dlen
-= *sz_h4
;
316 /* Data points to STP payload which can be handled by H4 */
320 static int btmtkuart_recv(struct hci_dev
*hdev
, const u8
*data
, size_t count
)
322 struct btmtkuart_dev
*bdev
= hci_get_drvdata(hdev
);
323 const unsigned char *p_left
= data
, *p_h4
;
324 int sz_left
= count
, sz_h4
, adv
;
327 while (sz_left
> 0) {
328 /* The serial data received from MT7622 BT controller is
329 * at all time padded around with the STP header and tailer.
331 * A full STP packet is looking like
332 * -----------------------------------
333 * | STP header | H:4 | STP tailer |
334 * -----------------------------------
335 * but it doesn't guarantee to contain a full H:4 packet which
336 * means that it's possible for multiple STP packets forms a
337 * full H:4 packet that means extra STP header + length doesn't
338 * indicate a full H:4 frame, things can fragment. Whose length
339 * recorded in STP header just shows up the most length the
340 * H:4 engine can handle currently.
343 p_h4
= mtk_stp_split(bdev
, p_left
, sz_left
, &sz_h4
);
351 bdev
->rx_skb
= h4_recv_buf(bdev
->hdev
, bdev
->rx_skb
, p_h4
,
352 sz_h4
, mtk_recv_pkts
,
353 ARRAY_SIZE(mtk_recv_pkts
));
354 if (IS_ERR(bdev
->rx_skb
)) {
355 err
= PTR_ERR(bdev
->rx_skb
);
356 bt_dev_err(bdev
->hdev
,
357 "Frame reassembly failed (%d)", err
);
369 static int btmtkuart_receive_buf(struct serdev_device
*serdev
, const u8
*data
,
372 struct btmtkuart_dev
*bdev
= serdev_device_get_drvdata(serdev
);
375 err
= btmtkuart_recv(bdev
->hdev
, data
, count
);
379 bdev
->hdev
->stat
.byte_rx
+= count
;
384 static void btmtkuart_write_wakeup(struct serdev_device
*serdev
)
386 struct btmtkuart_dev
*bdev
= serdev_device_get_drvdata(serdev
);
388 btmtkuart_tx_wakeup(bdev
);
391 static const struct serdev_device_ops btmtkuart_client_ops
= {
392 .receive_buf
= btmtkuart_receive_buf
,
393 .write_wakeup
= btmtkuart_write_wakeup
,
396 static int btmtkuart_open(struct hci_dev
*hdev
)
398 struct btmtkuart_dev
*bdev
= hci_get_drvdata(hdev
);
402 err
= serdev_device_open(bdev
->serdev
);
404 bt_dev_err(hdev
, "Unable to open UART device %s",
405 dev_name(&bdev
->serdev
->dev
));
409 bdev
->stp_cursor
= 2;
412 dev
= &bdev
->serdev
->dev
;
414 /* Enable the power domain and clock the device requires */
415 pm_runtime_enable(dev
);
416 err
= pm_runtime_get_sync(dev
);
418 pm_runtime_put_noidle(dev
);
419 goto err_disable_rpm
;
422 err
= clk_prepare_enable(bdev
->clk
);
429 pm_runtime_put_sync(dev
);
431 pm_runtime_disable(dev
);
436 static int btmtkuart_close(struct hci_dev
*hdev
)
438 struct btmtkuart_dev
*bdev
= hci_get_drvdata(hdev
);
439 struct device
*dev
= &bdev
->serdev
->dev
;
441 /* Shutdown the clock and power domain the device requires */
442 clk_disable_unprepare(bdev
->clk
);
443 pm_runtime_put_sync(dev
);
444 pm_runtime_disable(dev
);
446 serdev_device_close(bdev
->serdev
);
451 static int btmtkuart_flush(struct hci_dev
*hdev
)
453 struct btmtkuart_dev
*bdev
= hci_get_drvdata(hdev
);
455 /* Flush any pending characters */
456 serdev_device_write_flush(bdev
->serdev
);
457 skb_queue_purge(&bdev
->txq
);
459 cancel_work_sync(&bdev
->tx_work
);
461 kfree_skb(bdev
->rx_skb
);
464 bdev
->stp_cursor
= 2;
470 static int btmtkuart_setup(struct hci_dev
*hdev
)
475 /* Setup a firmware which the device definitely requires */
476 err
= mtk_setup_fw(hdev
);
480 /* Activate function the firmware providing to */
481 err
= mtk_hci_wmt_sync(hdev
, MTK_WMT_RST
, 0x4, 0, 0);
483 bt_dev_err(hdev
, "Failed to send wmt rst (%d)", err
);
487 /* Enable Bluetooth protocol */
488 err
= mtk_hci_wmt_sync(hdev
, MTK_WMT_FUNC_CTRL
, 0x0, sizeof(param
),
491 bt_dev_err(hdev
, "Failed to send wmt func ctrl (%d)", err
);
498 static int btmtkuart_shutdown(struct hci_dev
*hdev
)
503 /* Disable the device */
504 err
= mtk_hci_wmt_sync(hdev
, MTK_WMT_FUNC_CTRL
, 0x0, sizeof(param
),
507 bt_dev_err(hdev
, "Failed to send wmt func ctrl (%d)", err
);
514 static int btmtkuart_send_frame(struct hci_dev
*hdev
, struct sk_buff
*skb
)
516 struct btmtkuart_dev
*bdev
= hci_get_drvdata(hdev
);
517 struct mtk_stp_hdr
*shdr
;
518 int err
, dlen
, type
= 0;
520 /* Prepend skb with frame type */
521 memcpy(skb_push(skb
, 1), &hci_skb_pkt_type(skb
), 1);
523 /* Make sure that there is enough rooms for STP header and trailer */
524 if (unlikely(skb_headroom(skb
) < sizeof(*shdr
)) ||
525 (skb_tailroom(skb
) < MTK_STP_TLR_SIZE
)) {
526 err
= pskb_expand_head(skb
, sizeof(*shdr
), MTK_STP_TLR_SIZE
,
532 /* Add the STP header */
534 shdr
= skb_push(skb
, sizeof(*shdr
));
536 shdr
->dlen
= cpu_to_be16((dlen
& 0x0fff) | (type
<< 12));
537 shdr
->cs
= 0; /* MT7622 doesn't care about checksum value */
539 /* Add the STP trailer */
540 skb_put_zero(skb
, MTK_STP_TLR_SIZE
);
542 skb_queue_tail(&bdev
->txq
, skb
);
544 btmtkuart_tx_wakeup(bdev
);
548 static int btmtkuart_probe(struct serdev_device
*serdev
)
550 struct btmtkuart_dev
*bdev
;
551 struct hci_dev
*hdev
;
553 bdev
= devm_kzalloc(&serdev
->dev
, sizeof(*bdev
), GFP_KERNEL
);
557 bdev
->clk
= devm_clk_get(&serdev
->dev
, "ref");
558 if (IS_ERR(bdev
->clk
))
559 return PTR_ERR(bdev
->clk
);
561 bdev
->serdev
= serdev
;
562 serdev_device_set_drvdata(serdev
, bdev
);
564 serdev_device_set_client_ops(serdev
, &btmtkuart_client_ops
);
566 INIT_WORK(&bdev
->tx_work
, btmtkuart_tx_work
);
567 skb_queue_head_init(&bdev
->txq
);
569 /* Initialize and register HCI device */
570 hdev
= hci_alloc_dev();
572 dev_err(&serdev
->dev
, "Can't allocate HCI device\n");
578 hdev
->bus
= HCI_UART
;
579 hci_set_drvdata(hdev
, bdev
);
581 hdev
->open
= btmtkuart_open
;
582 hdev
->close
= btmtkuart_close
;
583 hdev
->flush
= btmtkuart_flush
;
584 hdev
->setup
= btmtkuart_setup
;
585 hdev
->shutdown
= btmtkuart_shutdown
;
586 hdev
->send
= btmtkuart_send_frame
;
587 SET_HCIDEV_DEV(hdev
, &serdev
->dev
);
589 hdev
->manufacturer
= 70;
590 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP
, &hdev
->quirks
);
592 if (hci_register_dev(hdev
) < 0) {
593 dev_err(&serdev
->dev
, "Can't register HCI device\n");
601 static void btmtkuart_remove(struct serdev_device
*serdev
)
603 struct btmtkuart_dev
*bdev
= serdev_device_get_drvdata(serdev
);
604 struct hci_dev
*hdev
= bdev
->hdev
;
606 hci_unregister_dev(hdev
);
611 static const struct of_device_id mtk_of_match_table
[] = {
612 { .compatible
= "mediatek,mt7622-bluetooth"},
615 MODULE_DEVICE_TABLE(of
, mtk_of_match_table
);
618 static struct serdev_device_driver btmtkuart_driver
= {
619 .probe
= btmtkuart_probe
,
620 .remove
= btmtkuart_remove
,
623 .of_match_table
= of_match_ptr(mtk_of_match_table
),
627 module_serdev_device_driver(btmtkuart_driver
);
629 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
630 MODULE_DESCRIPTION("MediaTek Bluetooth Serial driver ver " VERSION
);
631 MODULE_VERSION(VERSION
);
632 MODULE_LICENSE("GPL");
633 MODULE_FIRMWARE(FIRMWARE_MT7622
);