1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018 MediaTek Inc.
5 * Bluetooth support for MediaTek serial devices
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");
122 bt_dev_err(hdev, "Execution of wmt command timed out");
129 static int mtk_setup_fw(struct hci_dev *hdev)
131 const struct firmware *fw;
137 err = request_firmware(&fw, FIRMWARE_MT7622, &hdev->dev);
139 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
146 /* The size of patch header is 30 bytes, should be skip */
156 while (fw_size > 0) {
157 dlen = min_t(int, 250, fw_size);
159 /* Tell device the position in sequence */
160 if (fw_size - dlen <= 0)
162 else if (fw_size < fw->size - 30)
165 err = mtk_hci_wmt_sync(hdev, MTK_WMT_PATCH_DWNLD, flag, dlen,
168 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
178 release_firmware(fw);
182 static int btmtkuart_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
184 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
185 struct hci_event_hdr *hdr = (void *)skb->data;
188 /* Fix up the vendor event id with 0xff for vendor specific instead
189 * of 0xe4 so that event send via monitoring socket can be parsed
192 if (hdr->evt == 0xe4)
193 hdr->evt = HCI_EV_VENDOR;
195 err = hci_recv_frame(hdev, skb);
197 if (hdr->evt == HCI_EV_VENDOR) {
198 if (test_and_clear_bit(BTMTKUART_TX_WAIT_VND_EVT,
200 /* Barrier to sync with other CPUs */
201 smp_mb__after_atomic();
202 wake_up_bit(&bdev->tx_state, BTMTKUART_TX_WAIT_VND_EVT);
209 static const struct h4_recv_pkt mtk_recv_pkts[] = {
210 { H4_RECV_ACL, .recv = hci_recv_frame },
211 { H4_RECV_SCO, .recv = hci_recv_frame },
212 { H4_RECV_EVENT, .recv = btmtkuart_recv_event },
215 static void btmtkuart_tx_work(struct work_struct *work)
217 struct btmtkuart_dev *bdev = container_of(work, struct btmtkuart_dev,
219 struct serdev_device *serdev = bdev->serdev;
220 struct hci_dev *hdev = bdev->hdev;
223 clear_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state);
226 struct sk_buff *skb = skb_dequeue(&bdev->txq);
232 len = serdev_device_write_buf(serdev, skb->data,
234 hdev->stat.byte_tx += len;
238 skb_queue_head(&bdev->txq, skb);
242 switch (hci_skb_pkt_type(skb)) {
243 case HCI_COMMAND_PKT:
246 case HCI_ACLDATA_PKT:
249 case HCI_SCODATA_PKT:
257 if (!test_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state))
261 clear_bit(BTMTKUART_TX_STATE_ACTIVE, &bdev->tx_state);
264 static void btmtkuart_tx_wakeup(struct btmtkuart_dev *bdev)
266 if (test_and_set_bit(BTMTKUART_TX_STATE_ACTIVE, &bdev->tx_state))
267 set_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state);
269 schedule_work(&bdev->tx_work);
272 static const unsigned char *
273 mtk_stp_split(struct btmtkuart_dev *bdev, const unsigned char *data, int count,
276 struct mtk_stp_hdr *shdr;
278 /* The cursor is reset when all the data of STP is consumed out */
279 if (!bdev->stp_dlen && bdev->stp_cursor >= 6)
280 bdev->stp_cursor = 0;
282 /* Filling pad until all STP info is obtained */
283 while (bdev->stp_cursor < 6 && count > 0) {
284 bdev->stp_pad[bdev->stp_cursor] = *data;
290 /* Retrieve STP info and have a sanity check */
291 if (!bdev->stp_dlen && bdev->stp_cursor >= 6) {
292 shdr = (struct mtk_stp_hdr *)&bdev->stp_pad[2];
293 bdev->stp_dlen = be16_to_cpu(shdr->dlen) & 0x0fff;
295 /* Resync STP when unexpected data is being read */
296 if (shdr->prefix != 0x80 || bdev->stp_dlen > 2048) {
297 bt_dev_err(bdev->hdev, "stp format unexpect (%d, %d)",
298 shdr->prefix, bdev->stp_dlen);
299 bdev->stp_cursor = 2;
304 /* Directly quit when there's no data found for H4 can process */
308 /* Tranlate to how much the size of data H4 can handle so far */
309 *sz_h4 = min_t(int, count, bdev->stp_dlen);
311 /* Update the remaining size of STP packet */
312 bdev->stp_dlen -= *sz_h4;
314 /* Data points to STP payload which can be handled by H4 */
318 static int btmtkuart_recv(struct hci_dev *hdev, const u8 *data, size_t count)
320 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
321 const unsigned char *p_left = data, *p_h4;
322 int sz_left = count, sz_h4, adv;
325 while (sz_left > 0) {
326 /* The serial data received from MT7622 BT controller is
327 * at all time padded around with the STP header and tailer.
329 * A full STP packet is looking like
330 * -----------------------------------
331 * | STP header | H:4 | STP tailer |
332 * -----------------------------------
333 * but it doesn't guarantee to contain a full H:4 packet which
334 * means that it's possible for multiple STP packets forms a
335 * full H:4 packet that means extra STP header + length doesn't
336 * indicate a full H:4 frame, things can fragment. Whose length
337 * recorded in STP header just shows up the most length the
338 * H:4 engine can handle currently.
341 p_h4 = mtk_stp_split(bdev, p_left, sz_left, &sz_h4);
349 bdev->rx_skb = h4_recv_buf(bdev->hdev, bdev->rx_skb, p_h4,
350 sz_h4, mtk_recv_pkts,
351 ARRAY_SIZE(mtk_recv_pkts));
352 if (IS_ERR(bdev->rx_skb)) {
353 err = PTR_ERR(bdev->rx_skb);
354 bt_dev_err(bdev->hdev,
355 "Frame reassembly failed (%d)", err);
367 static int btmtkuart_receive_buf(struct serdev_device *serdev, const u8 *data,
370 struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
373 err = btmtkuart_recv(bdev->hdev, data, count);
377 bdev->hdev->stat.byte_rx += count;
382 static void btmtkuart_write_wakeup(struct serdev_device *serdev)
384 struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
386 btmtkuart_tx_wakeup(bdev);
389 static const struct serdev_device_ops btmtkuart_client_ops = {
390 .receive_buf = btmtkuart_receive_buf,
391 .write_wakeup = btmtkuart_write_wakeup,
394 static int btmtkuart_open(struct hci_dev *hdev)
396 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
400 err = serdev_device_open(bdev->serdev);
402 bt_dev_err(hdev, "Unable to open UART device %s",
403 dev_name(&bdev->serdev->dev));
407 bdev->stp_cursor = 2;
410 dev = &bdev->serdev->dev;
412 /* Enable the power domain and clock the device requires */
413 pm_runtime_enable(dev);
414 err = pm_runtime_get_sync(dev);
416 pm_runtime_put_noidle(dev);
417 goto err_disable_rpm;
420 err = clk_prepare_enable(bdev->clk);
427 pm_runtime_put_sync(dev);
429 pm_runtime_disable(dev);
434 static int btmtkuart_close(struct hci_dev *hdev)
436 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
437 struct device *dev = &bdev->serdev->dev;
439 /* Shutdown the clock and power domain the device requires */
440 clk_disable_unprepare(bdev->clk);
441 pm_runtime_put_sync(dev);
442 pm_runtime_disable(dev);
444 serdev_device_close(bdev->serdev);
449 static int btmtkuart_flush(struct hci_dev *hdev)
451 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
453 /* Flush any pending characters */
454 serdev_device_write_flush(bdev->serdev);
455 skb_queue_purge(&bdev->txq);
457 cancel_work_sync(&bdev->tx_work);
459 kfree_skb(bdev->rx_skb);
462 bdev->stp_cursor = 2;
468 static int btmtkuart_setup(struct hci_dev *hdev)
473 /* Setup a firmware which the device definitely requires */
474 err = mtk_setup_fw(hdev);
478 /* Activate function the firmware providing to */
479 err = mtk_hci_wmt_sync(hdev, MTK_WMT_RST, 0x4, 0, 0);
481 bt_dev_err(hdev, "Failed to send wmt rst (%d)", err);
485 /* Enable Bluetooth protocol */
486 err = mtk_hci_wmt_sync(hdev, MTK_WMT_FUNC_CTRL, 0x0, sizeof(param),
489 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
496 static int btmtkuart_shutdown(struct hci_dev *hdev)
501 /* Disable the device */
502 err = mtk_hci_wmt_sync(hdev, MTK_WMT_FUNC_CTRL, 0x0, sizeof(param),
505 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
512 static int btmtkuart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
514 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
515 struct mtk_stp_hdr *shdr;
516 int err, dlen, type = 0;
518 /* Prepend skb with frame type */
519 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
521 /* Make sure that there is enough rooms for STP header and trailer */
522 if (unlikely(skb_headroom(skb) < sizeof(*shdr)) ||
523 (skb_tailroom(skb) < MTK_STP_TLR_SIZE)) {
524 err = pskb_expand_head(skb, sizeof(*shdr), MTK_STP_TLR_SIZE,
530 /* Add the STP header */
532 shdr = skb_push(skb, sizeof(*shdr));
534 shdr->dlen = cpu_to_be16((dlen & 0x0fff) | (type << 12));
535 shdr->cs = 0; /* MT7622 doesn't care about checksum value */
537 /* Add the STP trailer */
538 skb_put_zero(skb, MTK_STP_TLR_SIZE);
540 skb_queue_tail(&bdev->txq, skb);
542 btmtkuart_tx_wakeup(bdev);
546 static int btmtkuart_probe(struct serdev_device *serdev)
548 struct btmtkuart_dev *bdev;
549 struct hci_dev *hdev;
551 bdev = devm_kzalloc(&serdev->dev, sizeof(*bdev), GFP_KERNEL);
555 bdev->clk = devm_clk_get(&serdev->dev, "ref");
556 if (IS_ERR(bdev->clk))
557 return PTR_ERR(bdev->clk);
559 bdev->serdev = serdev;
560 serdev_device_set_drvdata(serdev, bdev);
562 serdev_device_set_client_ops(serdev, &btmtkuart_client_ops);
564 INIT_WORK(&bdev->tx_work, btmtkuart_tx_work);
565 skb_queue_head_init(&bdev->txq);
567 /* Initialize and register HCI device */
568 hdev = hci_alloc_dev();
570 dev_err(&serdev->dev, "Can't allocate HCI device\n");
576 hdev->bus = HCI_UART;
577 hci_set_drvdata(hdev, bdev);
579 hdev->open = btmtkuart_open;
580 hdev->close = btmtkuart_close;
581 hdev->flush = btmtkuart_flush;
582 hdev->setup = btmtkuart_setup;
583 hdev->shutdown = btmtkuart_shutdown;
584 hdev->send = btmtkuart_send_frame;
585 SET_HCIDEV_DEV(hdev, &serdev->dev);
587 hdev->manufacturer = 70;
588 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
590 if (hci_register_dev(hdev) < 0) {
591 dev_err(&serdev->dev, "Can't register HCI device\n");
599 static void btmtkuart_remove(struct serdev_device *serdev)
601 struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
602 struct hci_dev *hdev = bdev->hdev;
604 hci_unregister_dev(hdev);
609 static const struct of_device_id mtk_of_match_table[] = {
610 { .compatible = "mediatek,mt7622-bluetooth"},
613 MODULE_DEVICE_TABLE(of, mtk_of_match_table);
616 static struct serdev_device_driver btmtkuart_driver = {
617 .probe = btmtkuart_probe,
618 .remove = btmtkuart_remove,
621 .of_match_table = of_match_ptr(mtk_of_match_table),
625 module_serdev_device_driver(btmtkuart_driver);
628 MODULE_DESCRIPTION("MediaTek Bluetooth Serial driver ver " VERSION);
629 MODULE_VERSION(VERSION);
630 MODULE_LICENSE("GPL");
631 MODULE_FIRMWARE(FIRMWARE_MT7622);