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/iopoll.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/serdev.h>
21 #include <linux/skbuff.h>
23 #include <net/bluetooth/bluetooth.h>
24 #include <net/bluetooth/hci_core.h>
30 #define FIRMWARE_MT7622 "mediatek/mt7622pr2h.bin"
32 #define MTK_STP_TLR_SIZE 2
34 #define BTMTKUART_TX_STATE_ACTIVE 1
35 #define BTMTKUART_TX_STATE_WAKEUP 2
36 #define BTMTKUART_TX_WAIT_VND_EVT 3
39 MTK_WMT_PATCH_DWNLD = 0x1,
40 MTK_WMT_FUNC_CTRL = 0x6,
42 MTK_WMT_SEMAPHORE = 0x17,
47 BTMTK_WMT_PATCH_UNDONE,
51 BTMTK_WMT_ON_PROGRESS,
67 struct mtk_hci_wmt_cmd {
68 struct mtk_wmt_hdr hdr;
72 struct btmtk_hci_wmt_evt {
73 struct hci_event_hdr hhdr;
74 struct mtk_wmt_hdr whdr;
77 struct btmtk_hci_wmt_evt_funcc {
78 struct btmtk_hci_wmt_evt hwhdr;
82 struct btmtk_tci_sleep {
90 struct btmtk_hci_wmt_params {
98 struct btmtkuart_dev {
100 struct serdev_device *serdev;
103 struct work_struct tx_work;
104 unsigned long tx_state;
105 struct sk_buff_head txq;
107 struct sk_buff *rx_skb;
108 struct sk_buff *evt_skb;
115 static int mtk_hci_wmt_sync(struct hci_dev *hdev,
116 struct btmtk_hci_wmt_params *wmt_params)
118 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
119 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
120 u32 hlen, status = BTMTK_WMT_INVALID;
121 struct btmtk_hci_wmt_evt *wmt_evt;
122 struct mtk_hci_wmt_cmd wc;
123 struct mtk_wmt_hdr *hdr;
126 hlen = sizeof(*hdr) + wmt_params->dlen;
130 hdr = (struct mtk_wmt_hdr *)&wc;
132 hdr->op = wmt_params->op;
133 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
134 hdr->flag = wmt_params->flag;
135 memcpy(wc.data, wmt_params->data, wmt_params->dlen);
137 set_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
139 err = __hci_cmd_send(hdev, 0xfc6f, hlen, &wc);
141 clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
145 /* The vendor specific WMT commands are all answered by a vendor
146 * specific event and will not have the Command Status or Command
147 * Complete as with usual HCI command flow control.
149 * After sending the command, wait for BTMTKUART_TX_WAIT_VND_EVT
150 * state to be cleared. The driver specific event receive routine
151 * will clear that state and with that indicate completion of the
154 err = wait_on_bit_timeout(&bdev->tx_state, BTMTKUART_TX_WAIT_VND_EVT,
155 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
157 bt_dev_err(hdev, "Execution of wmt command interrupted");
158 clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
163 bt_dev_err(hdev, "Execution of wmt command timed out");
164 clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
168 /* Parse and handle the return WMT event */
169 wmt_evt = (struct btmtk_hci_wmt_evt *)bdev->evt_skb->data;
170 if (wmt_evt->whdr.op != hdr->op) {
171 bt_dev_err(hdev, "Wrong op received %d expected %d",
172 wmt_evt->whdr.op, hdr->op);
177 switch (wmt_evt->whdr.op) {
178 case MTK_WMT_SEMAPHORE:
179 if (wmt_evt->whdr.flag == 2)
180 status = BTMTK_WMT_PATCH_UNDONE;
182 status = BTMTK_WMT_PATCH_DONE;
184 case MTK_WMT_FUNC_CTRL:
185 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
186 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
187 status = BTMTK_WMT_ON_DONE;
188 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
189 status = BTMTK_WMT_ON_PROGRESS;
191 status = BTMTK_WMT_ON_UNDONE;
195 if (wmt_params->status)
196 *wmt_params->status = status;
199 kfree_skb(bdev->evt_skb);
200 bdev->evt_skb = NULL;
205 static int mtk_setup_fw(struct hci_dev *hdev)
207 struct btmtk_hci_wmt_params wmt_params;
208 const struct firmware *fw;
214 err = request_firmware(&fw, FIRMWARE_MT7622, &hdev->dev);
216 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
223 /* The size of patch header is 30 bytes, should be skip */
233 wmt_params.op = MTK_WMT_PATCH_DWNLD;
234 wmt_params.status = NULL;
236 while (fw_size > 0) {
237 dlen = min_t(int, 250, fw_size);
239 /* Tell device the position in sequence */
240 if (fw_size - dlen <= 0)
242 else if (fw_size < fw->size - 30)
245 wmt_params.flag = flag;
246 wmt_params.dlen = dlen;
247 wmt_params.data = fw_ptr;
249 err = mtk_hci_wmt_sync(hdev, &wmt_params);
251 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
260 wmt_params.op = MTK_WMT_RST;
263 wmt_params.data = NULL;
264 wmt_params.status = NULL;
266 /* Activate funciton the firmware providing to */
267 err = mtk_hci_wmt_sync(hdev, &wmt_params);
269 bt_dev_err(hdev, "Failed to send wmt rst (%d)", err);
273 /* Wait a few moments for firmware activation done */
274 usleep_range(10000, 12000);
277 release_firmware(fw);
281 static int btmtkuart_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
283 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
284 struct hci_event_hdr *hdr = (void *)skb->data;
287 /* Fix up the vendor event id with 0xff for vendor specific instead
288 * of 0xe4 so that event send via monitoring socket can be parsed
291 if (hdr->evt == 0xe4)
292 hdr->evt = HCI_EV_VENDOR;
294 /* When someone waits for the WMT event, the skb is being cloned
295 * and being processed the events from there then.
297 if (test_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state)) {
298 bdev->evt_skb = skb_clone(skb, GFP_KERNEL);
299 if (!bdev->evt_skb) {
305 err = hci_recv_frame(hdev, skb);
309 if (hdr->evt == HCI_EV_VENDOR) {
310 if (test_and_clear_bit(BTMTKUART_TX_WAIT_VND_EVT,
312 /* Barrier to sync with other CPUs */
313 smp_mb__after_atomic();
314 wake_up_bit(&bdev->tx_state, BTMTKUART_TX_WAIT_VND_EVT);
321 kfree_skb(bdev->evt_skb);
322 bdev->evt_skb = NULL;
328 static const struct h4_recv_pkt mtk_recv_pkts[] = {
329 { H4_RECV_ACL, .recv = hci_recv_frame },
330 { H4_RECV_SCO, .recv = hci_recv_frame },
331 { H4_RECV_EVENT, .recv = btmtkuart_recv_event },
334 static void btmtkuart_tx_work(struct work_struct *work)
336 struct btmtkuart_dev *bdev = container_of(work, struct btmtkuart_dev,
338 struct serdev_device *serdev = bdev->serdev;
339 struct hci_dev *hdev = bdev->hdev;
342 clear_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state);
345 struct sk_buff *skb = skb_dequeue(&bdev->txq);
351 len = serdev_device_write_buf(serdev, skb->data,
353 hdev->stat.byte_tx += len;
357 skb_queue_head(&bdev->txq, skb);
361 switch (hci_skb_pkt_type(skb)) {
362 case HCI_COMMAND_PKT:
365 case HCI_ACLDATA_PKT:
368 case HCI_SCODATA_PKT:
376 if (!test_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state))
380 clear_bit(BTMTKUART_TX_STATE_ACTIVE, &bdev->tx_state);
383 static void btmtkuart_tx_wakeup(struct btmtkuart_dev *bdev)
385 if (test_and_set_bit(BTMTKUART_TX_STATE_ACTIVE, &bdev->tx_state))
386 set_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state);
388 schedule_work(&bdev->tx_work);
391 static const unsigned char *
392 mtk_stp_split(struct btmtkuart_dev *bdev, const unsigned char *data, int count,
395 struct mtk_stp_hdr *shdr;
397 /* The cursor is reset when all the data of STP is consumed out */
398 if (!bdev->stp_dlen && bdev->stp_cursor >= 6)
399 bdev->stp_cursor = 0;
401 /* Filling pad until all STP info is obtained */
402 while (bdev->stp_cursor < 6 && count > 0) {
403 bdev->stp_pad[bdev->stp_cursor] = *data;
409 /* Retrieve STP info and have a sanity check */
410 if (!bdev->stp_dlen && bdev->stp_cursor >= 6) {
411 shdr = (struct mtk_stp_hdr *)&bdev->stp_pad[2];
412 bdev->stp_dlen = be16_to_cpu(shdr->dlen) & 0x0fff;
414 /* Resync STP when unexpected data is being read */
415 if (shdr->prefix != 0x80 || bdev->stp_dlen > 2048) {
416 bt_dev_err(bdev->hdev, "stp format unexpect (%d, %d)",
417 shdr->prefix, bdev->stp_dlen);
418 bdev->stp_cursor = 2;
423 /* Directly quit when there's no data found for H4 can process */
427 /* Tranlate to how much the size of data H4 can handle so far */
428 *sz_h4 = min_t(int, count, bdev->stp_dlen);
430 /* Update the remaining size of STP packet */
431 bdev->stp_dlen -= *sz_h4;
433 /* Data points to STP payload which can be handled by H4 */
437 static int btmtkuart_recv(struct hci_dev *hdev, const u8 *data, size_t count)
439 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
440 const unsigned char *p_left = data, *p_h4;
441 int sz_left = count, sz_h4, adv;
444 while (sz_left > 0) {
445 /* The serial data received from MT7622 BT controller is
446 * at all time padded around with the STP header and tailer.
448 * A full STP packet is looking like
449 * -----------------------------------
450 * | STP header | H:4 | STP tailer |
451 * -----------------------------------
452 * but it doesn't guarantee to contain a full H:4 packet which
453 * means that it's possible for multiple STP packets forms a
454 * full H:4 packet that means extra STP header + length doesn't
455 * indicate a full H:4 frame, things can fragment. Whose length
456 * recorded in STP header just shows up the most length the
457 * H:4 engine can handle currently.
460 p_h4 = mtk_stp_split(bdev, p_left, sz_left, &sz_h4);
468 bdev->rx_skb = h4_recv_buf(bdev->hdev, bdev->rx_skb, p_h4,
469 sz_h4, mtk_recv_pkts,
470 ARRAY_SIZE(mtk_recv_pkts));
471 if (IS_ERR(bdev->rx_skb)) {
472 err = PTR_ERR(bdev->rx_skb);
473 bt_dev_err(bdev->hdev,
474 "Frame reassembly failed (%d)", err);
486 static int btmtkuart_receive_buf(struct serdev_device *serdev, const u8 *data,
489 struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
492 err = btmtkuart_recv(bdev->hdev, data, count);
496 bdev->hdev->stat.byte_rx += count;
501 static void btmtkuart_write_wakeup(struct serdev_device *serdev)
503 struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
505 btmtkuart_tx_wakeup(bdev);
508 static const struct serdev_device_ops btmtkuart_client_ops = {
509 .receive_buf = btmtkuart_receive_buf,
510 .write_wakeup = btmtkuart_write_wakeup,
513 static int btmtkuart_open(struct hci_dev *hdev)
515 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
519 err = serdev_device_open(bdev->serdev);
521 bt_dev_err(hdev, "Unable to open UART device %s",
522 dev_name(&bdev->serdev->dev));
526 bdev->stp_cursor = 2;
529 dev = &bdev->serdev->dev;
531 /* Enable the power domain and clock the device requires */
532 pm_runtime_enable(dev);
533 err = pm_runtime_get_sync(dev);
535 pm_runtime_put_noidle(dev);
536 goto err_disable_rpm;
539 err = clk_prepare_enable(bdev->clk);
546 pm_runtime_put_sync(dev);
548 pm_runtime_disable(dev);
553 static int btmtkuart_close(struct hci_dev *hdev)
555 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
556 struct device *dev = &bdev->serdev->dev;
558 /* Shutdown the clock and power domain the device requires */
559 clk_disable_unprepare(bdev->clk);
560 pm_runtime_put_sync(dev);
561 pm_runtime_disable(dev);
563 serdev_device_close(bdev->serdev);
568 static int btmtkuart_flush(struct hci_dev *hdev)
570 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
572 /* Flush any pending characters */
573 serdev_device_write_flush(bdev->serdev);
574 skb_queue_purge(&bdev->txq);
576 cancel_work_sync(&bdev->tx_work);
578 kfree_skb(bdev->rx_skb);
581 bdev->stp_cursor = 2;
587 static int btmtkuart_func_query(struct hci_dev *hdev)
589 struct btmtk_hci_wmt_params wmt_params;
593 /* Query whether the function is enabled */
594 wmt_params.op = MTK_WMT_FUNC_CTRL;
596 wmt_params.dlen = sizeof(param);
597 wmt_params.data = ¶m;
598 wmt_params.status = &status;
600 err = mtk_hci_wmt_sync(hdev, &wmt_params);
602 bt_dev_err(hdev, "Failed to query function status (%d)", err);
609 static int btmtkuart_setup(struct hci_dev *hdev)
611 struct btmtk_hci_wmt_params wmt_params;
612 ktime_t calltime, delta, rettime;
613 struct btmtk_tci_sleep tci_sleep;
614 unsigned long long duration;
619 calltime = ktime_get();
621 /* Query whether the firmware is already download */
622 wmt_params.op = MTK_WMT_SEMAPHORE;
625 wmt_params.data = NULL;
626 wmt_params.status = &status;
628 err = mtk_hci_wmt_sync(hdev, &wmt_params);
630 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
634 if (status == BTMTK_WMT_PATCH_DONE) {
635 bt_dev_info(hdev, "Firmware already downloaded");
636 goto ignore_setup_fw;
639 /* Setup a firmware which the device definitely requires */
640 err = mtk_setup_fw(hdev);
645 /* Query whether the device is already enabled */
646 err = readx_poll_timeout(btmtkuart_func_query, hdev, status,
647 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
649 /* -ETIMEDOUT happens */
653 /* The other errors happen in btusb_mtk_func_query */
657 if (status == BTMTK_WMT_ON_DONE) {
658 bt_dev_info(hdev, "function already on");
662 /* Enable Bluetooth protocol */
663 wmt_params.op = MTK_WMT_FUNC_CTRL;
665 wmt_params.dlen = sizeof(param);
666 wmt_params.data = ¶m;
667 wmt_params.status = NULL;
669 err = mtk_hci_wmt_sync(hdev, &wmt_params);
671 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
676 /* Apply the low power environment setup */
677 tci_sleep.mode = 0x5;
678 tci_sleep.duration = cpu_to_le16(0x640);
679 tci_sleep.host_duration = cpu_to_le16(0x640);
680 tci_sleep.host_wakeup_pin = 0;
681 tci_sleep.time_compensation = 0;
683 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
687 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
692 rettime = ktime_get();
693 delta = ktime_sub(rettime, calltime);
694 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
696 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
701 static int btmtkuart_shutdown(struct hci_dev *hdev)
703 struct btmtk_hci_wmt_params wmt_params;
707 /* Disable the device */
708 wmt_params.op = MTK_WMT_FUNC_CTRL;
710 wmt_params.dlen = sizeof(param);
711 wmt_params.data = ¶m;
712 wmt_params.status = NULL;
714 err = mtk_hci_wmt_sync(hdev, &wmt_params);
716 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
723 static int btmtkuart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
725 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
726 struct mtk_stp_hdr *shdr;
727 int err, dlen, type = 0;
729 /* Prepend skb with frame type */
730 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
732 /* Make sure that there is enough rooms for STP header and trailer */
733 if (unlikely(skb_headroom(skb) < sizeof(*shdr)) ||
734 (skb_tailroom(skb) < MTK_STP_TLR_SIZE)) {
735 err = pskb_expand_head(skb, sizeof(*shdr), MTK_STP_TLR_SIZE,
741 /* Add the STP header */
743 shdr = skb_push(skb, sizeof(*shdr));
745 shdr->dlen = cpu_to_be16((dlen & 0x0fff) | (type << 12));
746 shdr->cs = 0; /* MT7622 doesn't care about checksum value */
748 /* Add the STP trailer */
749 skb_put_zero(skb, MTK_STP_TLR_SIZE);
751 skb_queue_tail(&bdev->txq, skb);
753 btmtkuart_tx_wakeup(bdev);
757 static int btmtkuart_probe(struct serdev_device *serdev)
759 struct btmtkuart_dev *bdev;
760 struct hci_dev *hdev;
762 bdev = devm_kzalloc(&serdev->dev, sizeof(*bdev), GFP_KERNEL);
766 bdev->clk = devm_clk_get(&serdev->dev, "ref");
767 if (IS_ERR(bdev->clk))
768 return PTR_ERR(bdev->clk);
770 bdev->serdev = serdev;
771 serdev_device_set_drvdata(serdev, bdev);
773 serdev_device_set_client_ops(serdev, &btmtkuart_client_ops);
775 INIT_WORK(&bdev->tx_work, btmtkuart_tx_work);
776 skb_queue_head_init(&bdev->txq);
778 /* Initialize and register HCI device */
779 hdev = hci_alloc_dev();
781 dev_err(&serdev->dev, "Can't allocate HCI device\n");
787 hdev->bus = HCI_UART;
788 hci_set_drvdata(hdev, bdev);
790 hdev->open = btmtkuart_open;
791 hdev->close = btmtkuart_close;
792 hdev->flush = btmtkuart_flush;
793 hdev->setup = btmtkuart_setup;
794 hdev->shutdown = btmtkuart_shutdown;
795 hdev->send = btmtkuart_send_frame;
796 SET_HCIDEV_DEV(hdev, &serdev->dev);
798 hdev->manufacturer = 70;
799 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
801 if (hci_register_dev(hdev) < 0) {
802 dev_err(&serdev->dev, "Can't register HCI device\n");
810 static void btmtkuart_remove(struct serdev_device *serdev)
812 struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
813 struct hci_dev *hdev = bdev->hdev;
815 hci_unregister_dev(hdev);
820 static const struct of_device_id mtk_of_match_table[] = {
821 { .compatible = "mediatek,mt7622-bluetooth"},
824 MODULE_DEVICE_TABLE(of, mtk_of_match_table);
827 static struct serdev_device_driver btmtkuart_driver = {
828 .probe = btmtkuart_probe,
829 .remove = btmtkuart_remove,
832 .of_match_table = of_match_ptr(mtk_of_match_table),
836 module_serdev_device_driver(btmtkuart_driver);
838 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
839 MODULE_DESCRIPTION("MediaTek Bluetooth Serial driver ver " VERSION);
840 MODULE_VERSION(VERSION);
841 MODULE_LICENSE("GPL");
842 MODULE_FIRMWARE(FIRMWARE_MT7622);