1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018 MediaTek Inc.
3
4 /*
5 * Bluetooth support for MediaTek serial devices
6 *
7 * Author: Sean Wang <sean.wang@mediatek.com>
8 *
9 */
10
11 #include <asm/unaligned.h>
12 #include <linux/atomic.h>
13 #include <linux/clk.h>
14 #include <linux/firmware.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/iopoll.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/of_device.h>
21 #include <linux/pinctrl/consumer.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/serdev.h>
25 #include <linux/skbuff.h>
26
27 #include <net/bluetooth/bluetooth.h>
28 #include <net/bluetooth/hci_core.h>
29
30 #include "h4_recv.h"
31
32 #define VERSION "0.2"
33
34 #define FIRMWARE_MT7622 "mediatek/mt7622pr2h.bin"
35 #define FIRMWARE_MT7663 "mediatek/mt7663pr2h.bin"
36 #define FIRMWARE_MT7668 "mediatek/mt7668pr2h.bin"
37
38 #define MTK_STP_TLR_SIZE 2
39
40 #define BTMTKUART_TX_STATE_ACTIVE 1
41 #define BTMTKUART_TX_STATE_WAKEUP 2
42 #define BTMTKUART_TX_WAIT_VND_EVT 3
43 #define BTMTKUART_REQUIRED_WAKEUP 4
44
45 #define BTMTKUART_FLAG_STANDALONE_HW BIT(0)
46
47 enum {
48 MTK_WMT_PATCH_DWNLD = 0x1,
49 MTK_WMT_TEST = 0x2,
50 MTK_WMT_WAKEUP = 0x3,
51 MTK_WMT_HIF = 0x4,
52 MTK_WMT_FUNC_CTRL = 0x6,
53 MTK_WMT_RST = 0x7,
54 MTK_WMT_SEMAPHORE = 0x17,
55 };
56
57 enum {
58 BTMTK_WMT_INVALID,
59 BTMTK_WMT_PATCH_UNDONE,
60 BTMTK_WMT_PATCH_DONE,
61 BTMTK_WMT_ON_UNDONE,
62 BTMTK_WMT_ON_DONE,
63 BTMTK_WMT_ON_PROGRESS,
64 };
65
66 struct mtk_stp_hdr {
67 u8 prefix;
68 __be16 dlen;
69 u8 cs;
70 } __packed;
71
72 struct btmtkuart_data {
73 unsigned int flags;
74 const char *fwname;
75 };
76
77 struct mtk_wmt_hdr {
78 u8 dir;
79 u8 op;
80 __le16 dlen;
81 u8 flag;
82 } __packed;
83
84 struct mtk_hci_wmt_cmd {
85 struct mtk_wmt_hdr hdr;
86 u8 data[256];
87 } __packed;
88
89 struct btmtk_hci_wmt_evt {
90 struct hci_event_hdr hhdr;
91 struct mtk_wmt_hdr whdr;
92 } __packed;
93
94 struct btmtk_hci_wmt_evt_funcc {
95 struct btmtk_hci_wmt_evt hwhdr;
96 __be16 status;
97 } __packed;
98
99 struct btmtk_tci_sleep {
100 u8 mode;
101 __le16 duration;
102 __le16 host_duration;
103 u8 host_wakeup_pin;
104 u8 time_compensation;
105 } __packed;
106
107 struct btmtk_hci_wmt_params {
108 u8 op;
109 u8 flag;
110 u16 dlen;
111 const void *data;
112 u32 *status;
113 };
114
115 struct btmtkuart_dev {
116 struct hci_dev *hdev;
117 struct serdev_device *serdev;
118
119 struct clk *clk;
120 struct clk *osc;
121 struct regulator *vcc;
122 struct gpio_desc *reset;
123 struct gpio_desc *boot;
124 struct pinctrl *pinctrl;
125 struct pinctrl_state *pins_runtime;
126 struct pinctrl_state *pins_boot;
127 speed_t desired_speed;
128 speed_t curr_speed;
129
130 struct work_struct tx_work;
131 unsigned long tx_state;
132 struct sk_buff_head txq;
133
134 struct sk_buff *rx_skb;
135 struct sk_buff *evt_skb;
136
137 u8 stp_pad[6];
138 u8 stp_cursor;
139 u16 stp_dlen;
140
141 const struct btmtkuart_data *data;
142 };
143
144 #define btmtkuart_is_standalone(bdev) \
145 ((bdev)->data->flags & BTMTKUART_FLAG_STANDALONE_HW)
146 #define btmtkuart_is_builtin_soc(bdev) \
147 !((bdev)->data->flags & BTMTKUART_FLAG_STANDALONE_HW)
148
mtk_hci_wmt_sync(struct hci_dev * hdev,struct btmtk_hci_wmt_params * wmt_params)149 static int mtk_hci_wmt_sync(struct hci_dev *hdev,
150 struct btmtk_hci_wmt_params *wmt_params)
151 {
152 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
153 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
154 u32 hlen, status = BTMTK_WMT_INVALID;
155 struct btmtk_hci_wmt_evt *wmt_evt;
156 struct mtk_hci_wmt_cmd wc;
157 struct mtk_wmt_hdr *hdr;
158 int err;
159
160 hlen = sizeof(*hdr) + wmt_params->dlen;
161 if (hlen > 255) {
162 err = -EINVAL;
163 goto err_free_skb;
164 }
165
166 hdr = (struct mtk_wmt_hdr *)&wc;
167 hdr->dir = 1;
168 hdr->op = wmt_params->op;
169 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
170 hdr->flag = wmt_params->flag;
171 memcpy(wc.data, wmt_params->data, wmt_params->dlen);
172
173 set_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
174
175 err = __hci_cmd_send(hdev, 0xfc6f, hlen, &wc);
176 if (err < 0) {
177 clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
178 goto err_free_skb;
179 }
180
181 /* The vendor specific WMT commands are all answered by a vendor
182 * specific event and will not have the Command Status or Command
183 * Complete as with usual HCI command flow control.
184 *
185 * After sending the command, wait for BTMTKUART_TX_WAIT_VND_EVT
186 * state to be cleared. The driver specific event receive routine
187 * will clear that state and with that indicate completion of the
188 * WMT command.
189 */
190 err = wait_on_bit_timeout(&bdev->tx_state, BTMTKUART_TX_WAIT_VND_EVT,
191 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
192 if (err == -EINTR) {
193 bt_dev_err(hdev, "Execution of wmt command interrupted");
194 clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
195 goto err_free_skb;
196 }
197
198 if (err) {
199 bt_dev_err(hdev, "Execution of wmt command timed out");
200 clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
201 err = -ETIMEDOUT;
202 goto err_free_skb;
203 }
204
205 /* Parse and handle the return WMT event */
206 wmt_evt = (struct btmtk_hci_wmt_evt *)bdev->evt_skb->data;
207 if (wmt_evt->whdr.op != hdr->op) {
208 bt_dev_err(hdev, "Wrong op received %d expected %d",
209 wmt_evt->whdr.op, hdr->op);
210 err = -EIO;
211 goto err_free_skb;
212 }
213
214 switch (wmt_evt->whdr.op) {
215 case MTK_WMT_SEMAPHORE:
216 if (wmt_evt->whdr.flag == 2)
217 status = BTMTK_WMT_PATCH_UNDONE;
218 else
219 status = BTMTK_WMT_PATCH_DONE;
220 break;
221 case MTK_WMT_FUNC_CTRL:
222 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
223 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
224 status = BTMTK_WMT_ON_DONE;
225 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
226 status = BTMTK_WMT_ON_PROGRESS;
227 else
228 status = BTMTK_WMT_ON_UNDONE;
229 break;
230 }
231
232 if (wmt_params->status)
233 *wmt_params->status = status;
234
235 err_free_skb:
236 kfree_skb(bdev->evt_skb);
237 bdev->evt_skb = NULL;
238
239 return err;
240 }
241
mtk_setup_firmware(struct hci_dev * hdev,const char * fwname)242 static int mtk_setup_firmware(struct hci_dev *hdev, const char *fwname)
243 {
244 struct btmtk_hci_wmt_params wmt_params;
245 const struct firmware *fw;
246 const u8 *fw_ptr;
247 size_t fw_size;
248 int err, dlen;
249 u8 flag;
250
251 err = request_firmware(&fw, fwname, &hdev->dev);
252 if (err < 0) {
253 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
254 return err;
255 }
256
257 fw_ptr = fw->data;
258 fw_size = fw->size;
259
260 /* The size of patch header is 30 bytes, should be skip */
261 if (fw_size < 30) {
262 err = -EINVAL;
263 goto free_fw;
264 }
265
266 fw_size -= 30;
267 fw_ptr += 30;
268 flag = 1;
269
270 wmt_params.op = MTK_WMT_PATCH_DWNLD;
271 wmt_params.status = NULL;
272
273 while (fw_size > 0) {
274 dlen = min_t(int, 250, fw_size);
275
276 /* Tell device the position in sequence */
277 if (fw_size - dlen <= 0)
278 flag = 3;
279 else if (fw_size < fw->size - 30)
280 flag = 2;
281
282 wmt_params.flag = flag;
283 wmt_params.dlen = dlen;
284 wmt_params.data = fw_ptr;
285
286 err = mtk_hci_wmt_sync(hdev, &wmt_params);
287 if (err < 0) {
288 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
289 err);
290 goto free_fw;
291 }
292
293 fw_size -= dlen;
294 fw_ptr += dlen;
295 }
296
297 wmt_params.op = MTK_WMT_RST;
298 wmt_params.flag = 4;
299 wmt_params.dlen = 0;
300 wmt_params.data = NULL;
301 wmt_params.status = NULL;
302
303 /* Activate funciton the firmware providing to */
304 err = mtk_hci_wmt_sync(hdev, &wmt_params);
305 if (err < 0) {
306 bt_dev_err(hdev, "Failed to send wmt rst (%d)", err);
307 goto free_fw;
308 }
309
310 /* Wait a few moments for firmware activation done */
311 usleep_range(10000, 12000);
312
313 free_fw:
314 release_firmware(fw);
315 return err;
316 }
317
btmtkuart_recv_event(struct hci_dev * hdev,struct sk_buff * skb)318 static int btmtkuart_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
319 {
320 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
321 struct hci_event_hdr *hdr = (void *)skb->data;
322 int err;
323
324 /* Fix up the vendor event id with 0xff for vendor specific instead
325 * of 0xe4 so that event send via monitoring socket can be parsed
326 * properly.
327 */
328 if (hdr->evt == 0xe4)
329 hdr->evt = HCI_EV_VENDOR;
330
331 /* When someone waits for the WMT event, the skb is being cloned
332 * and being processed the events from there then.
333 */
334 if (test_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state)) {
335 bdev->evt_skb = skb_clone(skb, GFP_KERNEL);
336 if (!bdev->evt_skb) {
337 err = -ENOMEM;
338 goto err_out;
339 }
340 }
341
342 err = hci_recv_frame(hdev, skb);
343 if (err < 0)
344 goto err_free_skb;
345
346 if (hdr->evt == HCI_EV_VENDOR) {
347 if (test_and_clear_bit(BTMTKUART_TX_WAIT_VND_EVT,
348 &bdev->tx_state)) {
349 /* Barrier to sync with other CPUs */
350 smp_mb__after_atomic();
351 wake_up_bit(&bdev->tx_state, BTMTKUART_TX_WAIT_VND_EVT);
352 }
353 }
354
355 return 0;
356
357 err_free_skb:
358 kfree_skb(bdev->evt_skb);
359 bdev->evt_skb = NULL;
360
361 err_out:
362 return err;
363 }
364
365 static const struct h4_recv_pkt mtk_recv_pkts[] = {
366 { H4_RECV_ACL, .recv = hci_recv_frame },
367 { H4_RECV_SCO, .recv = hci_recv_frame },
368 { H4_RECV_EVENT, .recv = btmtkuart_recv_event },
369 };
370
btmtkuart_tx_work(struct work_struct * work)371 static void btmtkuart_tx_work(struct work_struct *work)
372 {
373 struct btmtkuart_dev *bdev = container_of(work, struct btmtkuart_dev,
374 tx_work);
375 struct serdev_device *serdev = bdev->serdev;
376 struct hci_dev *hdev = bdev->hdev;
377
378 while (1) {
379 clear_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state);
380
381 while (1) {
382 struct sk_buff *skb = skb_dequeue(&bdev->txq);
383 int len;
384
385 if (!skb)
386 break;
387
388 len = serdev_device_write_buf(serdev, skb->data,
389 skb->len);
390 hdev->stat.byte_tx += len;
391
392 skb_pull(skb, len);
393 if (skb->len > 0) {
394 skb_queue_head(&bdev->txq, skb);
395 break;
396 }
397
398 switch (hci_skb_pkt_type(skb)) {
399 case HCI_COMMAND_PKT:
400 hdev->stat.cmd_tx++;
401 break;
402 case HCI_ACLDATA_PKT:
403 hdev->stat.acl_tx++;
404 break;
405 case HCI_SCODATA_PKT:
406 hdev->stat.sco_tx++;
407 break;
408 }
409
410 kfree_skb(skb);
411 }
412
413 if (!test_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state))
414 break;
415 }
416
417 clear_bit(BTMTKUART_TX_STATE_ACTIVE, &bdev->tx_state);
418 }
419
btmtkuart_tx_wakeup(struct btmtkuart_dev * bdev)420 static void btmtkuart_tx_wakeup(struct btmtkuart_dev *bdev)
421 {
422 if (test_and_set_bit(BTMTKUART_TX_STATE_ACTIVE, &bdev->tx_state))
423 set_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state);
424
425 schedule_work(&bdev->tx_work);
426 }
427
428 static const unsigned char *
mtk_stp_split(struct btmtkuart_dev * bdev,const unsigned char * data,int count,int * sz_h4)429 mtk_stp_split(struct btmtkuart_dev *bdev, const unsigned char *data, int count,
430 int *sz_h4)
431 {
432 struct mtk_stp_hdr *shdr;
433
434 /* The cursor is reset when all the data of STP is consumed out */
435 if (!bdev->stp_dlen && bdev->stp_cursor >= 6)
436 bdev->stp_cursor = 0;
437
438 /* Filling pad until all STP info is obtained */
439 while (bdev->stp_cursor < 6 && count > 0) {
440 bdev->stp_pad[bdev->stp_cursor] = *data;
441 bdev->stp_cursor++;
442 data++;
443 count--;
444 }
445
446 /* Retrieve STP info and have a sanity check */
447 if (!bdev->stp_dlen && bdev->stp_cursor >= 6) {
448 shdr = (struct mtk_stp_hdr *)&bdev->stp_pad[2];
449 bdev->stp_dlen = be16_to_cpu(shdr->dlen) & 0x0fff;
450
451 /* Resync STP when unexpected data is being read */
452 if (shdr->prefix != 0x80 || bdev->stp_dlen > 2048) {
453 bt_dev_err(bdev->hdev, "stp format unexpect (%d, %d)",
454 shdr->prefix, bdev->stp_dlen);
455 bdev->stp_cursor = 2;
456 bdev->stp_dlen = 0;
457 }
458 }
459
460 /* Directly quit when there's no data found for H4 can process */
461 if (count <= 0)
462 return NULL;
463
464 /* Tranlate to how much the size of data H4 can handle so far */
465 *sz_h4 = min_t(int, count, bdev->stp_dlen);
466
467 /* Update the remaining size of STP packet */
468 bdev->stp_dlen -= *sz_h4;
469
470 /* Data points to STP payload which can be handled by H4 */
471 return data;
472 }
473
btmtkuart_recv(struct hci_dev * hdev,const u8 * data,size_t count)474 static void btmtkuart_recv(struct hci_dev *hdev, const u8 *data, size_t count)
475 {
476 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
477 const unsigned char *p_left = data, *p_h4;
478 int sz_left = count, sz_h4, adv;
479 int err;
480
481 while (sz_left > 0) {
482 /* The serial data received from MT7622 BT controller is
483 * at all time padded around with the STP header and tailer.
484 *
485 * A full STP packet is looking like
486 * -----------------------------------
487 * | STP header | H:4 | STP tailer |
488 * -----------------------------------
489 * but it doesn't guarantee to contain a full H:4 packet which
490 * means that it's possible for multiple STP packets forms a
491 * full H:4 packet that means extra STP header + length doesn't
492 * indicate a full H:4 frame, things can fragment. Whose length
493 * recorded in STP header just shows up the most length the
494 * H:4 engine can handle currently.
495 */
496
497 p_h4 = mtk_stp_split(bdev, p_left, sz_left, &sz_h4);
498 if (!p_h4)
499 break;
500
501 adv = p_h4 - p_left;
502 sz_left -= adv;
503 p_left += adv;
504
505 bdev->rx_skb = h4_recv_buf(bdev->hdev, bdev->rx_skb, p_h4,
506 sz_h4, mtk_recv_pkts,
507 ARRAY_SIZE(mtk_recv_pkts));
508 if (IS_ERR(bdev->rx_skb)) {
509 err = PTR_ERR(bdev->rx_skb);
510 bt_dev_err(bdev->hdev,
511 "Frame reassembly failed (%d)", err);
512 bdev->rx_skb = NULL;
513 return;
514 }
515
516 sz_left -= sz_h4;
517 p_left += sz_h4;
518 }
519 }
520
btmtkuart_receive_buf(struct serdev_device * serdev,const u8 * data,size_t count)521 static int btmtkuart_receive_buf(struct serdev_device *serdev, const u8 *data,
522 size_t count)
523 {
524 struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
525
526 btmtkuart_recv(bdev->hdev, data, count);
527
528 bdev->hdev->stat.byte_rx += count;
529
530 return count;
531 }
532
btmtkuart_write_wakeup(struct serdev_device * serdev)533 static void btmtkuart_write_wakeup(struct serdev_device *serdev)
534 {
535 struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
536
537 btmtkuart_tx_wakeup(bdev);
538 }
539
540 static const struct serdev_device_ops btmtkuart_client_ops = {
541 .receive_buf = btmtkuart_receive_buf,
542 .write_wakeup = btmtkuart_write_wakeup,
543 };
544
btmtkuart_open(struct hci_dev * hdev)545 static int btmtkuart_open(struct hci_dev *hdev)
546 {
547 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
548 struct device *dev;
549 int err;
550
551 err = serdev_device_open(bdev->serdev);
552 if (err) {
553 bt_dev_err(hdev, "Unable to open UART device %s",
554 dev_name(&bdev->serdev->dev));
555 goto err_open;
556 }
557
558 if (btmtkuart_is_standalone(bdev)) {
559 if (bdev->curr_speed != bdev->desired_speed)
560 err = serdev_device_set_baudrate(bdev->serdev,
561 115200);
562 else
563 err = serdev_device_set_baudrate(bdev->serdev,
564 bdev->desired_speed);
565
566 if (err < 0) {
567 bt_dev_err(hdev, "Unable to set baudrate UART device %s",
568 dev_name(&bdev->serdev->dev));
569 goto err_serdev_close;
570 }
571
572 serdev_device_set_flow_control(bdev->serdev, false);
573 }
574
575 bdev->stp_cursor = 2;
576 bdev->stp_dlen = 0;
577
578 dev = &bdev->serdev->dev;
579
580 /* Enable the power domain and clock the device requires */
581 pm_runtime_enable(dev);
582 err = pm_runtime_resume_and_get(dev);
583 if (err < 0)
584 goto err_disable_rpm;
585
586 err = clk_prepare_enable(bdev->clk);
587 if (err < 0)
588 goto err_put_rpm;
589
590 return 0;
591
592 err_put_rpm:
593 pm_runtime_put_sync(dev);
594 err_disable_rpm:
595 pm_runtime_disable(dev);
596 err_serdev_close:
597 serdev_device_close(bdev->serdev);
598 err_open:
599 return err;
600 }
601
btmtkuart_close(struct hci_dev * hdev)602 static int btmtkuart_close(struct hci_dev *hdev)
603 {
604 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
605 struct device *dev = &bdev->serdev->dev;
606
607 /* Shutdown the clock and power domain the device requires */
608 clk_disable_unprepare(bdev->clk);
609 pm_runtime_put_sync(dev);
610 pm_runtime_disable(dev);
611
612 serdev_device_close(bdev->serdev);
613
614 return 0;
615 }
616
btmtkuart_flush(struct hci_dev * hdev)617 static int btmtkuart_flush(struct hci_dev *hdev)
618 {
619 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
620
621 /* Flush any pending characters */
622 serdev_device_write_flush(bdev->serdev);
623 skb_queue_purge(&bdev->txq);
624
625 cancel_work_sync(&bdev->tx_work);
626
627 kfree_skb(bdev->rx_skb);
628 bdev->rx_skb = NULL;
629
630 bdev->stp_cursor = 2;
631 bdev->stp_dlen = 0;
632
633 return 0;
634 }
635
btmtkuart_func_query(struct hci_dev * hdev)636 static int btmtkuart_func_query(struct hci_dev *hdev)
637 {
638 struct btmtk_hci_wmt_params wmt_params;
639 int status, err;
640 u8 param = 0;
641
642 /* Query whether the function is enabled */
643 wmt_params.op = MTK_WMT_FUNC_CTRL;
644 wmt_params.flag = 4;
645 wmt_params.dlen = sizeof(param);
646 wmt_params.data = ¶m;
647 wmt_params.status = &status;
648
649 err = mtk_hci_wmt_sync(hdev, &wmt_params);
650 if (err < 0) {
651 bt_dev_err(hdev, "Failed to query function status (%d)", err);
652 return err;
653 }
654
655 return status;
656 }
657
btmtkuart_change_baudrate(struct hci_dev * hdev)658 static int btmtkuart_change_baudrate(struct hci_dev *hdev)
659 {
660 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
661 struct btmtk_hci_wmt_params wmt_params;
662 __le32 baudrate;
663 u8 param;
664 int err;
665
666 /* Indicate the device to enter the probe state the host is
667 * ready to change a new baudrate.
668 */
669 baudrate = cpu_to_le32(bdev->desired_speed);
670 wmt_params.op = MTK_WMT_HIF;
671 wmt_params.flag = 1;
672 wmt_params.dlen = 4;
673 wmt_params.data = &baudrate;
674 wmt_params.status = NULL;
675
676 err = mtk_hci_wmt_sync(hdev, &wmt_params);
677 if (err < 0) {
678 bt_dev_err(hdev, "Failed to device baudrate (%d)", err);
679 return err;
680 }
681
682 err = serdev_device_set_baudrate(bdev->serdev,
683 bdev->desired_speed);
684 if (err < 0) {
685 bt_dev_err(hdev, "Failed to set up host baudrate (%d)",
686 err);
687 return err;
688 }
689
690 serdev_device_set_flow_control(bdev->serdev, false);
691
692 /* Send a dummy byte 0xff to activate the new baudrate */
693 param = 0xff;
694 err = serdev_device_write_buf(bdev->serdev, ¶m, sizeof(param));
695 if (err < 0 || err < sizeof(param))
696 return err;
697
698 serdev_device_wait_until_sent(bdev->serdev, 0);
699
700 /* Wait some time for the device changing baudrate done */
701 usleep_range(20000, 22000);
702
703 /* Test the new baudrate */
704 wmt_params.op = MTK_WMT_TEST;
705 wmt_params.flag = 7;
706 wmt_params.dlen = 0;
707 wmt_params.data = NULL;
708 wmt_params.status = NULL;
709
710 err = mtk_hci_wmt_sync(hdev, &wmt_params);
711 if (err < 0) {
712 bt_dev_err(hdev, "Failed to test new baudrate (%d)",
713 err);
714 return err;
715 }
716
717 bdev->curr_speed = bdev->desired_speed;
718
719 return 0;
720 }
721
btmtkuart_setup(struct hci_dev * hdev)722 static int btmtkuart_setup(struct hci_dev *hdev)
723 {
724 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
725 struct btmtk_hci_wmt_params wmt_params;
726 ktime_t calltime, delta, rettime;
727 struct btmtk_tci_sleep tci_sleep;
728 unsigned long long duration;
729 struct sk_buff *skb;
730 int err, status;
731 u8 param = 0x1;
732
733 calltime = ktime_get();
734
735 /* Wakeup MCUSYS is required for certain devices before we start to
736 * do any setups.
737 */
738 if (test_bit(BTMTKUART_REQUIRED_WAKEUP, &bdev->tx_state)) {
739 wmt_params.op = MTK_WMT_WAKEUP;
740 wmt_params.flag = 3;
741 wmt_params.dlen = 0;
742 wmt_params.data = NULL;
743 wmt_params.status = NULL;
744
745 err = mtk_hci_wmt_sync(hdev, &wmt_params);
746 if (err < 0) {
747 bt_dev_err(hdev, "Failed to wakeup the chip (%d)", err);
748 return err;
749 }
750
751 clear_bit(BTMTKUART_REQUIRED_WAKEUP, &bdev->tx_state);
752 }
753
754 if (btmtkuart_is_standalone(bdev))
755 btmtkuart_change_baudrate(hdev);
756
757 /* Query whether the firmware is already download */
758 wmt_params.op = MTK_WMT_SEMAPHORE;
759 wmt_params.flag = 1;
760 wmt_params.dlen = 0;
761 wmt_params.data = NULL;
762 wmt_params.status = &status;
763
764 err = mtk_hci_wmt_sync(hdev, &wmt_params);
765 if (err < 0) {
766 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
767 return err;
768 }
769
770 if (status == BTMTK_WMT_PATCH_DONE) {
771 bt_dev_info(hdev, "Firmware already downloaded");
772 goto ignore_setup_fw;
773 }
774
775 /* Setup a firmware which the device definitely requires */
776 err = mtk_setup_firmware(hdev, bdev->data->fwname);
777 if (err < 0)
778 return err;
779
780 ignore_setup_fw:
781 /* Query whether the device is already enabled */
782 err = readx_poll_timeout(btmtkuart_func_query, hdev, status,
783 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
784 2000, 5000000);
785 /* -ETIMEDOUT happens */
786 if (err < 0)
787 return err;
788
789 /* The other errors happen in btusb_mtk_func_query */
790 if (status < 0)
791 return status;
792
793 if (status == BTMTK_WMT_ON_DONE) {
794 bt_dev_info(hdev, "function already on");
795 goto ignore_func_on;
796 }
797
798 /* Enable Bluetooth protocol */
799 wmt_params.op = MTK_WMT_FUNC_CTRL;
800 wmt_params.flag = 0;
801 wmt_params.dlen = sizeof(param);
802 wmt_params.data = ¶m;
803 wmt_params.status = NULL;
804
805 err = mtk_hci_wmt_sync(hdev, &wmt_params);
806 if (err < 0) {
807 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
808 return err;
809 }
810
811 ignore_func_on:
812 /* Apply the low power environment setup */
813 tci_sleep.mode = 0x5;
814 tci_sleep.duration = cpu_to_le16(0x640);
815 tci_sleep.host_duration = cpu_to_le16(0x640);
816 tci_sleep.host_wakeup_pin = 0;
817 tci_sleep.time_compensation = 0;
818
819 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
820 HCI_INIT_TIMEOUT);
821 if (IS_ERR(skb)) {
822 err = PTR_ERR(skb);
823 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
824 return err;
825 }
826 kfree_skb(skb);
827
828 rettime = ktime_get();
829 delta = ktime_sub(rettime, calltime);
830 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
831
832 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
833
834 return 0;
835 }
836
btmtkuart_shutdown(struct hci_dev * hdev)837 static int btmtkuart_shutdown(struct hci_dev *hdev)
838 {
839 struct btmtk_hci_wmt_params wmt_params;
840 u8 param = 0x0;
841 int err;
842
843 /* Disable the device */
844 wmt_params.op = MTK_WMT_FUNC_CTRL;
845 wmt_params.flag = 0;
846 wmt_params.dlen = sizeof(param);
847 wmt_params.data = ¶m;
848 wmt_params.status = NULL;
849
850 err = mtk_hci_wmt_sync(hdev, &wmt_params);
851 if (err < 0) {
852 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
853 return err;
854 }
855
856 return 0;
857 }
858
btmtkuart_send_frame(struct hci_dev * hdev,struct sk_buff * skb)859 static int btmtkuart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
860 {
861 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
862 struct mtk_stp_hdr *shdr;
863 int err, dlen, type = 0;
864
865 /* Prepend skb with frame type */
866 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
867
868 /* Make sure that there is enough rooms for STP header and trailer */
869 if (unlikely(skb_headroom(skb) < sizeof(*shdr)) ||
870 (skb_tailroom(skb) < MTK_STP_TLR_SIZE)) {
871 err = pskb_expand_head(skb, sizeof(*shdr), MTK_STP_TLR_SIZE,
872 GFP_ATOMIC);
873 if (err < 0)
874 return err;
875 }
876
877 /* Add the STP header */
878 dlen = skb->len;
879 shdr = skb_push(skb, sizeof(*shdr));
880 shdr->prefix = 0x80;
881 shdr->dlen = cpu_to_be16((dlen & 0x0fff) | (type << 12));
882 shdr->cs = 0; /* MT7622 doesn't care about checksum value */
883
884 /* Add the STP trailer */
885 skb_put_zero(skb, MTK_STP_TLR_SIZE);
886
887 skb_queue_tail(&bdev->txq, skb);
888
889 btmtkuart_tx_wakeup(bdev);
890 return 0;
891 }
892
btmtkuart_parse_dt(struct serdev_device * serdev)893 static int btmtkuart_parse_dt(struct serdev_device *serdev)
894 {
895 struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
896 struct device_node *node = serdev->dev.of_node;
897 u32 speed = 921600;
898 int err;
899
900 if (btmtkuart_is_standalone(bdev)) {
901 of_property_read_u32(node, "current-speed", &speed);
902
903 bdev->desired_speed = speed;
904
905 bdev->vcc = devm_regulator_get(&serdev->dev, "vcc");
906 if (IS_ERR(bdev->vcc)) {
907 err = PTR_ERR(bdev->vcc);
908 return err;
909 }
910
911 bdev->osc = devm_clk_get_optional(&serdev->dev, "osc");
912 if (IS_ERR(bdev->osc)) {
913 err = PTR_ERR(bdev->osc);
914 return err;
915 }
916
917 bdev->boot = devm_gpiod_get_optional(&serdev->dev, "boot",
918 GPIOD_OUT_LOW);
919 if (IS_ERR(bdev->boot)) {
920 err = PTR_ERR(bdev->boot);
921 return err;
922 }
923
924 bdev->pinctrl = devm_pinctrl_get(&serdev->dev);
925 if (IS_ERR(bdev->pinctrl)) {
926 err = PTR_ERR(bdev->pinctrl);
927 return err;
928 }
929
930 bdev->pins_boot = pinctrl_lookup_state(bdev->pinctrl,
931 "default");
932 if (IS_ERR(bdev->pins_boot) && !bdev->boot) {
933 err = PTR_ERR(bdev->pins_boot);
934 dev_err(&serdev->dev,
935 "Should assign RXD to LOW at boot stage\n");
936 return err;
937 }
938
939 bdev->pins_runtime = pinctrl_lookup_state(bdev->pinctrl,
940 "runtime");
941 if (IS_ERR(bdev->pins_runtime)) {
942 err = PTR_ERR(bdev->pins_runtime);
943 return err;
944 }
945
946 bdev->reset = devm_gpiod_get_optional(&serdev->dev, "reset",
947 GPIOD_OUT_LOW);
948 if (IS_ERR(bdev->reset)) {
949 err = PTR_ERR(bdev->reset);
950 return err;
951 }
952 } else if (btmtkuart_is_builtin_soc(bdev)) {
953 bdev->clk = devm_clk_get(&serdev->dev, "ref");
954 if (IS_ERR(bdev->clk))
955 return PTR_ERR(bdev->clk);
956 }
957
958 return 0;
959 }
960
btmtkuart_probe(struct serdev_device * serdev)961 static int btmtkuart_probe(struct serdev_device *serdev)
962 {
963 struct btmtkuart_dev *bdev;
964 struct hci_dev *hdev;
965 int err;
966
967 bdev = devm_kzalloc(&serdev->dev, sizeof(*bdev), GFP_KERNEL);
968 if (!bdev)
969 return -ENOMEM;
970
971 bdev->data = of_device_get_match_data(&serdev->dev);
972 if (!bdev->data)
973 return -ENODEV;
974
975 bdev->serdev = serdev;
976 serdev_device_set_drvdata(serdev, bdev);
977
978 serdev_device_set_client_ops(serdev, &btmtkuart_client_ops);
979
980 err = btmtkuart_parse_dt(serdev);
981 if (err < 0)
982 return err;
983
984 INIT_WORK(&bdev->tx_work, btmtkuart_tx_work);
985 skb_queue_head_init(&bdev->txq);
986
987 /* Initialize and register HCI device */
988 hdev = hci_alloc_dev();
989 if (!hdev) {
990 dev_err(&serdev->dev, "Can't allocate HCI device\n");
991 return -ENOMEM;
992 }
993
994 bdev->hdev = hdev;
995
996 hdev->bus = HCI_UART;
997 hci_set_drvdata(hdev, bdev);
998
999 hdev->open = btmtkuart_open;
1000 hdev->close = btmtkuart_close;
1001 hdev->flush = btmtkuart_flush;
1002 hdev->setup = btmtkuart_setup;
1003 hdev->shutdown = btmtkuart_shutdown;
1004 hdev->send = btmtkuart_send_frame;
1005 SET_HCIDEV_DEV(hdev, &serdev->dev);
1006
1007 hdev->manufacturer = 70;
1008 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
1009
1010 if (btmtkuart_is_standalone(bdev)) {
1011 err = clk_prepare_enable(bdev->osc);
1012 if (err < 0)
1013 goto err_hci_free_dev;
1014
1015 if (bdev->boot) {
1016 gpiod_set_value_cansleep(bdev->boot, 1);
1017 } else {
1018 /* Switch to the specific pin state for the booting
1019 * requires.
1020 */
1021 pinctrl_select_state(bdev->pinctrl, bdev->pins_boot);
1022 }
1023
1024 /* Power on */
1025 err = regulator_enable(bdev->vcc);
1026 if (err < 0)
1027 goto err_clk_disable_unprepare;
1028
1029 /* Reset if the reset-gpios is available otherwise the board
1030 * -level design should be guaranteed.
1031 */
1032 if (bdev->reset) {
1033 gpiod_set_value_cansleep(bdev->reset, 1);
1034 usleep_range(1000, 2000);
1035 gpiod_set_value_cansleep(bdev->reset, 0);
1036 }
1037
1038 /* Wait some time until device got ready and switch to the pin
1039 * mode the device requires for UART transfers.
1040 */
1041 msleep(50);
1042
1043 if (bdev->boot)
1044 devm_gpiod_put(&serdev->dev, bdev->boot);
1045
1046 pinctrl_select_state(bdev->pinctrl, bdev->pins_runtime);
1047
1048 /* A standalone device doesn't depends on power domain on SoC,
1049 * so mark it as no callbacks.
1050 */
1051 pm_runtime_no_callbacks(&serdev->dev);
1052
1053 set_bit(BTMTKUART_REQUIRED_WAKEUP, &bdev->tx_state);
1054 }
1055
1056 err = hci_register_dev(hdev);
1057 if (err < 0) {
1058 dev_err(&serdev->dev, "Can't register HCI device\n");
1059 goto err_regulator_disable;
1060 }
1061
1062 return 0;
1063
1064 err_regulator_disable:
1065 if (btmtkuart_is_standalone(bdev))
1066 regulator_disable(bdev->vcc);
1067 err_clk_disable_unprepare:
1068 if (btmtkuart_is_standalone(bdev))
1069 clk_disable_unprepare(bdev->osc);
1070 err_hci_free_dev:
1071 hci_free_dev(hdev);
1072
1073 return err;
1074 }
1075
btmtkuart_remove(struct serdev_device * serdev)1076 static void btmtkuart_remove(struct serdev_device *serdev)
1077 {
1078 struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
1079 struct hci_dev *hdev = bdev->hdev;
1080
1081 if (btmtkuart_is_standalone(bdev)) {
1082 regulator_disable(bdev->vcc);
1083 clk_disable_unprepare(bdev->osc);
1084 }
1085
1086 hci_unregister_dev(hdev);
1087 hci_free_dev(hdev);
1088 }
1089
1090 static const struct btmtkuart_data mt7622_data = {
1091 .fwname = FIRMWARE_MT7622,
1092 };
1093
1094 static const struct btmtkuart_data mt7663_data = {
1095 .flags = BTMTKUART_FLAG_STANDALONE_HW,
1096 .fwname = FIRMWARE_MT7663,
1097 };
1098
1099 static const struct btmtkuart_data mt7668_data = {
1100 .flags = BTMTKUART_FLAG_STANDALONE_HW,
1101 .fwname = FIRMWARE_MT7668,
1102 };
1103
1104 #ifdef CONFIG_OF
1105 static const struct of_device_id mtk_of_match_table[] = {
1106 { .compatible = "mediatek,mt7622-bluetooth", .data = &mt7622_data},
1107 { .compatible = "mediatek,mt7663u-bluetooth", .data = &mt7663_data},
1108 { .compatible = "mediatek,mt7668u-bluetooth", .data = &mt7668_data},
1109 { }
1110 };
1111 MODULE_DEVICE_TABLE(of, mtk_of_match_table);
1112 #endif
1113
1114 static struct serdev_device_driver btmtkuart_driver = {
1115 .probe = btmtkuart_probe,
1116 .remove = btmtkuart_remove,
1117 .driver = {
1118 .name = "btmtkuart",
1119 .of_match_table = of_match_ptr(mtk_of_match_table),
1120 },
1121 };
1122
1123 module_serdev_device_driver(btmtkuart_driver);
1124
1125 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1126 MODULE_DESCRIPTION("MediaTek Bluetooth Serial driver ver " VERSION);
1127 MODULE_VERSION(VERSION);
1128 MODULE_LICENSE("GPL");
1129 MODULE_FIRMWARE(FIRMWARE_MT7622);
1130 MODULE_FIRMWARE(FIRMWARE_MT7663);
1131 MODULE_FIRMWARE(FIRMWARE_MT7668);
1132