1 // SPDX-License-Identifier: GPL-2.0-only
3 * Bluetooth Software UART Qualcomm protocol
5 * HCI_IBS (HCI In-Band Sleep) is Qualcomm's power management
6 * protocol extension to H4.
8 * Copyright (C) 2007 Texas Instruments, Inc.
9 * Copyright (c) 2010, 2012, 2018 The Linux Foundation. All rights reserved.
12 * This file is based on hci_ll.c, which was...
13 * Written by Ohad Ben-Cohen <ohad@bencohen.org>
14 * which was in turn based on hci_h4.c, which was written
15 * by Maxim Krasnyansky and Marcel Holtmann.
18 #include <linux/kernel.h>
19 #include <linux/clk.h>
20 #include <linux/completion.h>
21 #include <linux/debugfs.h>
22 #include <linux/delay.h>
23 #include <linux/devcoredump.h>
24 #include <linux/device.h>
25 #include <linux/gpio/consumer.h>
26 #include <linux/mod_devicetable.h>
27 #include <linux/module.h>
28 #include <linux/of_device.h>
29 #include <linux/acpi.h>
30 #include <linux/platform_device.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/serdev.h>
33 #include <linux/mutex.h>
34 #include <asm/unaligned.h>
36 #include <net/bluetooth/bluetooth.h>
37 #include <net/bluetooth/hci_core.h>
42 /* HCI_IBS protocol messages */
43 #define HCI_IBS_SLEEP_IND 0xFE
44 #define HCI_IBS_WAKE_IND 0xFD
45 #define HCI_IBS_WAKE_ACK 0xFC
46 #define HCI_MAX_IBS_SIZE 10
48 #define IBS_WAKE_RETRANS_TIMEOUT_MS 100
49 #define IBS_BTSOC_TX_IDLE_TIMEOUT_MS 200
50 #define IBS_HOST_TX_IDLE_TIMEOUT_MS 2000
51 #define CMD_TRANS_TIMEOUT_MS 100
52 #define MEMDUMP_TIMEOUT_MS 8000
53 #define IBS_DISABLE_SSR_TIMEOUT_MS \
54 (MEMDUMP_TIMEOUT_MS + FW_DOWNLOAD_TIMEOUT_MS)
55 #define FW_DOWNLOAD_TIMEOUT_MS 3000
58 #define SUSCLK_RATE_32KHZ 32768
60 /* Controller debug log header */
61 #define QCA_DEBUG_HANDLE 0x2EDC
63 /* max retry count when init fails */
64 #define MAX_INIT_RETRIES 3
66 /* Controller dump header */
67 #define QCA_SSR_DUMP_HANDLE 0x0108
68 #define QCA_DUMP_PACKET_SIZE 255
69 #define QCA_LAST_SEQUENCE_NUM 0xFFFF
70 #define QCA_CRASHBYTE_PACKET_LEN 1096
71 #define QCA_MEMDUMP_BYTE 0xFB
75 QCA_DROP_VENDOR_EVENT,
77 QCA_MEMDUMP_COLLECTION,
84 enum qca_capabilities {
85 QCA_CAP_WIDEBAND_SPEECH = BIT(0),
86 QCA_CAP_VALID_LE_STATES = BIT(1),
89 /* HCI_IBS transmit side sleep protocol states */
96 /* HCI_IBS receive side sleep protocol states */
102 /* HCI_IBS transmit and receive side clock state vote */
103 enum hci_ibs_clock_state_vote {
104 HCI_IBS_VOTE_STATS_UPDATE,
105 HCI_IBS_TX_VOTE_CLOCK_ON,
106 HCI_IBS_TX_VOTE_CLOCK_OFF,
107 HCI_IBS_RX_VOTE_CLOCK_ON,
108 HCI_IBS_RX_VOTE_CLOCK_OFF,
111 /* Controller memory dump states */
112 enum qca_memdump_states {
114 QCA_MEMDUMP_COLLECTING,
115 QCA_MEMDUMP_COLLECTED,
119 struct qca_memdump_data {
120 char *memdump_buf_head;
121 char *memdump_buf_tail;
127 struct qca_memdump_event_hdr {
136 struct qca_dump_size {
142 struct sk_buff *rx_skb;
143 struct sk_buff_head txq;
144 struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */
145 struct sk_buff_head rx_memdump_q; /* Memdump wait queue */
146 spinlock_t hci_ibs_lock; /* HCI_IBS state lock */
147 u8 tx_ibs_state; /* HCI_IBS transmit side power state*/
148 u8 rx_ibs_state; /* HCI_IBS receive side power state */
149 bool tx_vote; /* Clock must be on for TX */
150 bool rx_vote; /* Clock must be on for RX */
151 struct timer_list tx_idle_timer;
153 struct timer_list wake_retrans_timer;
155 struct workqueue_struct *workqueue;
156 struct work_struct ws_awake_rx;
157 struct work_struct ws_awake_device;
158 struct work_struct ws_rx_vote_off;
159 struct work_struct ws_tx_vote_off;
160 struct work_struct ctrl_memdump_evt;
161 struct delayed_work ctrl_memdump_timeout;
162 struct qca_memdump_data *qca_memdump;
164 struct completion drop_ev_comp;
165 wait_queue_head_t suspend_wait_q;
166 enum qca_memdump_states memdump_state;
167 struct mutex hci_memdump_lock;
169 /* For debugging purpose */
187 enum qca_speed_type {
193 * Voltage regulator information required for configuring the
194 * QCA Bluetooth chipset
198 unsigned int load_uA;
201 struct qca_device_data {
202 enum qca_btsoc_type soc_type;
203 struct qca_vreg *vregs;
205 uint32_t capabilities;
209 * Platform data for the QCA Bluetooth power driver.
213 struct regulator_bulk_data *vreg_bulk;
219 struct hci_uart serdev_hu;
220 struct gpio_desc *bt_en;
222 enum qca_btsoc_type btsoc_type;
223 struct qca_power *bt_power;
226 const char *firmware_name;
229 static int qca_regulator_enable(struct qca_serdev *qcadev);
230 static void qca_regulator_disable(struct qca_serdev *qcadev);
231 static void qca_power_shutdown(struct hci_uart *hu);
232 static int qca_power_off(struct hci_dev *hdev);
233 static void qca_controller_memdump(struct work_struct *work);
235 static enum qca_btsoc_type qca_soc_type(struct hci_uart *hu)
237 enum qca_btsoc_type soc_type;
240 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
242 soc_type = qsd->btsoc_type;
250 static const char *qca_get_firmware_name(struct hci_uart *hu)
253 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
255 return qsd->firmware_name;
261 static void __serial_clock_on(struct tty_struct *tty)
263 /* TODO: Some chipset requires to enable UART clock on client
264 * side to save power consumption or manual work is required.
265 * Please put your code to control UART clock here if needed
269 static void __serial_clock_off(struct tty_struct *tty)
271 /* TODO: Some chipset requires to disable UART clock on client
272 * side to save power consumption or manual work is required.
273 * Please put your code to control UART clock off here if needed
277 /* serial_clock_vote needs to be called with the ibs lock held */
278 static void serial_clock_vote(unsigned long vote, struct hci_uart *hu)
280 struct qca_data *qca = hu->priv;
283 bool old_vote = (qca->tx_vote | qca->rx_vote);
287 case HCI_IBS_VOTE_STATS_UPDATE:
288 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
291 qca->vote_off_ms += diff;
293 qca->vote_on_ms += diff;
296 case HCI_IBS_TX_VOTE_CLOCK_ON:
301 case HCI_IBS_RX_VOTE_CLOCK_ON:
306 case HCI_IBS_TX_VOTE_CLOCK_OFF:
307 qca->tx_vote = false;
311 case HCI_IBS_RX_VOTE_CLOCK_OFF:
312 qca->rx_vote = false;
317 BT_ERR("Voting irregularity");
321 new_vote = qca->rx_vote | qca->tx_vote;
323 if (new_vote != old_vote) {
325 __serial_clock_on(hu->tty);
327 __serial_clock_off(hu->tty);
329 BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false",
330 vote ? "true" : "false");
332 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
336 qca->vote_off_ms += diff;
339 qca->vote_on_ms += diff;
341 qca->vote_last_jif = jiffies;
345 /* Builds and sends an HCI_IBS command packet.
346 * These are very simple packets with only 1 cmd byte.
348 static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
351 struct sk_buff *skb = NULL;
352 struct qca_data *qca = hu->priv;
354 BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd);
356 skb = bt_skb_alloc(1, GFP_ATOMIC);
358 BT_ERR("Failed to allocate memory for HCI_IBS packet");
362 /* Assign HCI_IBS type */
363 skb_put_u8(skb, cmd);
365 skb_queue_tail(&qca->txq, skb);
370 static void qca_wq_awake_device(struct work_struct *work)
372 struct qca_data *qca = container_of(work, struct qca_data,
374 struct hci_uart *hu = qca->hu;
375 unsigned long retrans_delay;
378 BT_DBG("hu %p wq awake device", hu);
380 /* Vote for serial clock */
381 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
383 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
385 /* Send wake indication to device */
386 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
387 BT_ERR("Failed to send WAKE to device");
389 qca->ibs_sent_wakes++;
391 /* Start retransmit timer */
392 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
393 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
395 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
397 /* Actually send the packets */
398 hci_uart_tx_wakeup(hu);
401 static void qca_wq_awake_rx(struct work_struct *work)
403 struct qca_data *qca = container_of(work, struct qca_data,
405 struct hci_uart *hu = qca->hu;
408 BT_DBG("hu %p wq awake rx", hu);
410 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
412 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
413 qca->rx_ibs_state = HCI_IBS_RX_AWAKE;
415 /* Always acknowledge device wake up,
416 * sending IBS message doesn't count as TX ON.
418 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
419 BT_ERR("Failed to acknowledge device wake up");
421 qca->ibs_sent_wacks++;
423 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
425 /* Actually send the packets */
426 hci_uart_tx_wakeup(hu);
429 static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work)
431 struct qca_data *qca = container_of(work, struct qca_data,
433 struct hci_uart *hu = qca->hu;
435 BT_DBG("hu %p rx clock vote off", hu);
437 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
440 static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work)
442 struct qca_data *qca = container_of(work, struct qca_data,
444 struct hci_uart *hu = qca->hu;
446 BT_DBG("hu %p tx clock vote off", hu);
448 /* Run HCI tx handling unlocked */
449 hci_uart_tx_wakeup(hu);
451 /* Now that message queued to tty driver, vote for tty clocks off.
452 * It is up to the tty driver to pend the clocks off until tx done.
454 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
457 static void hci_ibs_tx_idle_timeout(struct timer_list *t)
459 struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
460 struct hci_uart *hu = qca->hu;
463 BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state);
465 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
466 flags, SINGLE_DEPTH_NESTING);
468 switch (qca->tx_ibs_state) {
469 case HCI_IBS_TX_AWAKE:
470 /* TX_IDLE, go to SLEEP */
471 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
472 BT_ERR("Failed to send SLEEP to device");
475 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
476 qca->ibs_sent_slps++;
477 queue_work(qca->workqueue, &qca->ws_tx_vote_off);
480 case HCI_IBS_TX_ASLEEP:
481 case HCI_IBS_TX_WAKING:
483 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
487 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
490 static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
492 struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
493 struct hci_uart *hu = qca->hu;
494 unsigned long flags, retrans_delay;
495 bool retransmit = false;
497 BT_DBG("hu %p wake retransmit timeout in %d state",
498 hu, qca->tx_ibs_state);
500 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
501 flags, SINGLE_DEPTH_NESTING);
503 /* Don't retransmit the HCI_IBS_WAKE_IND when suspending. */
504 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
505 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
509 switch (qca->tx_ibs_state) {
510 case HCI_IBS_TX_WAKING:
511 /* No WAKE_ACK, retransmit WAKE */
513 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
514 BT_ERR("Failed to acknowledge device wake up");
517 qca->ibs_sent_wakes++;
518 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
519 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
522 case HCI_IBS_TX_ASLEEP:
523 case HCI_IBS_TX_AWAKE:
525 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
529 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
532 hci_uart_tx_wakeup(hu);
536 static void qca_controller_memdump_timeout(struct work_struct *work)
538 struct qca_data *qca = container_of(work, struct qca_data,
539 ctrl_memdump_timeout.work);
540 struct hci_uart *hu = qca->hu;
542 mutex_lock(&qca->hci_memdump_lock);
543 if (test_bit(QCA_MEMDUMP_COLLECTION, &qca->flags)) {
544 qca->memdump_state = QCA_MEMDUMP_TIMEOUT;
545 if (!test_bit(QCA_HW_ERROR_EVENT, &qca->flags)) {
546 /* Inject hw error event to reset the device
549 hci_reset_dev(hu->hdev);
553 mutex_unlock(&qca->hci_memdump_lock);
557 /* Initialize protocol */
558 static int qca_open(struct hci_uart *hu)
560 struct qca_serdev *qcadev;
561 struct qca_data *qca;
563 BT_DBG("hu %p qca_open", hu);
565 if (!hci_uart_has_flow_control(hu))
568 qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL);
572 skb_queue_head_init(&qca->txq);
573 skb_queue_head_init(&qca->tx_wait_q);
574 skb_queue_head_init(&qca->rx_memdump_q);
575 spin_lock_init(&qca->hci_ibs_lock);
576 mutex_init(&qca->hci_memdump_lock);
577 qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
578 if (!qca->workqueue) {
579 BT_ERR("QCA Workqueue not initialized properly");
584 INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
585 INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
586 INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
587 INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
588 INIT_WORK(&qca->ctrl_memdump_evt, qca_controller_memdump);
589 INIT_DELAYED_WORK(&qca->ctrl_memdump_timeout,
590 qca_controller_memdump_timeout);
591 init_waitqueue_head(&qca->suspend_wait_q);
594 init_completion(&qca->drop_ev_comp);
596 /* Assume we start with both sides asleep -- extra wakes OK */
597 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
598 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
600 qca->vote_last_jif = jiffies;
605 qcadev = serdev_device_get_drvdata(hu->serdev);
607 if (qca_is_wcn399x(qcadev->btsoc_type))
608 hu->init_speed = qcadev->init_speed;
610 if (qcadev->oper_speed)
611 hu->oper_speed = qcadev->oper_speed;
614 timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
615 qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
617 timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
618 qca->tx_idle_delay = IBS_HOST_TX_IDLE_TIMEOUT_MS;
620 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
621 qca->tx_idle_delay, qca->wake_retrans);
626 static void qca_debugfs_init(struct hci_dev *hdev)
628 struct hci_uart *hu = hci_get_drvdata(hdev);
629 struct qca_data *qca = hu->priv;
630 struct dentry *ibs_dir;
636 ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
640 debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
641 debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
642 debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
643 &qca->ibs_sent_slps);
644 debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
645 &qca->ibs_sent_wakes);
646 debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
647 &qca->ibs_sent_wacks);
648 debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
649 &qca->ibs_recv_slps);
650 debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
651 &qca->ibs_recv_wakes);
652 debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
653 &qca->ibs_recv_wacks);
654 debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
655 debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
656 debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
657 debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
658 debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
659 debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
660 debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
661 debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
662 debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
663 debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
667 debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
668 debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
669 &qca->tx_idle_delay);
672 /* Flush protocol data */
673 static int qca_flush(struct hci_uart *hu)
675 struct qca_data *qca = hu->priv;
677 BT_DBG("hu %p qca flush", hu);
679 skb_queue_purge(&qca->tx_wait_q);
680 skb_queue_purge(&qca->txq);
686 static int qca_close(struct hci_uart *hu)
688 struct qca_data *qca = hu->priv;
690 BT_DBG("hu %p qca close", hu);
692 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
694 skb_queue_purge(&qca->tx_wait_q);
695 skb_queue_purge(&qca->txq);
696 skb_queue_purge(&qca->rx_memdump_q);
697 del_timer(&qca->tx_idle_timer);
698 del_timer(&qca->wake_retrans_timer);
699 destroy_workqueue(qca->workqueue);
702 kfree_skb(qca->rx_skb);
711 /* Called upon a wake-up-indication from the device.
713 static void device_want_to_wakeup(struct hci_uart *hu)
716 struct qca_data *qca = hu->priv;
718 BT_DBG("hu %p want to wake up", hu);
720 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
722 qca->ibs_recv_wakes++;
724 /* Don't wake the rx up when suspending. */
725 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
726 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
730 switch (qca->rx_ibs_state) {
731 case HCI_IBS_RX_ASLEEP:
732 /* Make sure clock is on - we may have turned clock off since
733 * receiving the wake up indicator awake rx clock.
735 queue_work(qca->workqueue, &qca->ws_awake_rx);
736 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
739 case HCI_IBS_RX_AWAKE:
740 /* Always acknowledge device wake up,
741 * sending IBS message doesn't count as TX ON.
743 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
744 BT_ERR("Failed to acknowledge device wake up");
747 qca->ibs_sent_wacks++;
751 /* Any other state is illegal */
752 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
757 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
759 /* Actually send the packets */
760 hci_uart_tx_wakeup(hu);
763 /* Called upon a sleep-indication from the device.
765 static void device_want_to_sleep(struct hci_uart *hu)
768 struct qca_data *qca = hu->priv;
770 BT_DBG("hu %p want to sleep in %d state", hu, qca->rx_ibs_state);
772 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
774 qca->ibs_recv_slps++;
776 switch (qca->rx_ibs_state) {
777 case HCI_IBS_RX_AWAKE:
779 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
780 /* Vote off rx clock under workqueue */
781 queue_work(qca->workqueue, &qca->ws_rx_vote_off);
784 case HCI_IBS_RX_ASLEEP:
788 /* Any other state is illegal */
789 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
794 wake_up_interruptible(&qca->suspend_wait_q);
796 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
799 /* Called upon wake-up-acknowledgement from the device
801 static void device_woke_up(struct hci_uart *hu)
803 unsigned long flags, idle_delay;
804 struct qca_data *qca = hu->priv;
805 struct sk_buff *skb = NULL;
807 BT_DBG("hu %p woke up", hu);
809 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
811 qca->ibs_recv_wacks++;
813 /* Don't react to the wake-up-acknowledgment when suspending. */
814 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
815 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
819 switch (qca->tx_ibs_state) {
820 case HCI_IBS_TX_AWAKE:
821 /* Expect one if we send 2 WAKEs */
822 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
826 case HCI_IBS_TX_WAKING:
827 /* Send pending packets */
828 while ((skb = skb_dequeue(&qca->tx_wait_q)))
829 skb_queue_tail(&qca->txq, skb);
831 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
832 del_timer(&qca->wake_retrans_timer);
833 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
834 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
835 qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
838 case HCI_IBS_TX_ASLEEP:
840 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
845 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
847 /* Actually send the packets */
848 hci_uart_tx_wakeup(hu);
851 /* Enqueue frame for transmittion (padding, crc, etc) may be called from
852 * two simultaneous tasklets.
854 static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
856 unsigned long flags = 0, idle_delay;
857 struct qca_data *qca = hu->priv;
859 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
862 if (test_bit(QCA_SSR_TRIGGERED, &qca->flags)) {
863 /* As SSR is in progress, ignore the packets */
864 bt_dev_dbg(hu->hdev, "SSR is in progress");
869 /* Prepend skb with frame type */
870 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
872 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
874 /* Don't go to sleep in middle of patch download or
875 * Out-Of-Band(GPIOs control) sleep is selected.
876 * Don't wake the device up when suspending.
878 if (test_bit(QCA_IBS_DISABLED, &qca->flags) ||
879 test_bit(QCA_SUSPENDING, &qca->flags)) {
880 skb_queue_tail(&qca->txq, skb);
881 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
885 /* Act according to current state */
886 switch (qca->tx_ibs_state) {
887 case HCI_IBS_TX_AWAKE:
888 BT_DBG("Device awake, sending normally");
889 skb_queue_tail(&qca->txq, skb);
890 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
891 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
894 case HCI_IBS_TX_ASLEEP:
895 BT_DBG("Device asleep, waking up and queueing packet");
896 /* Save packet for later */
897 skb_queue_tail(&qca->tx_wait_q, skb);
899 qca->tx_ibs_state = HCI_IBS_TX_WAKING;
900 /* Schedule a work queue to wake up device */
901 queue_work(qca->workqueue, &qca->ws_awake_device);
904 case HCI_IBS_TX_WAKING:
905 BT_DBG("Device waking up, queueing packet");
906 /* Transient state; just keep packet for later */
907 skb_queue_tail(&qca->tx_wait_q, skb);
911 BT_ERR("Illegal tx state: %d (losing packet)",
917 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
922 static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
924 struct hci_uart *hu = hci_get_drvdata(hdev);
926 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
928 device_want_to_sleep(hu);
934 static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
936 struct hci_uart *hu = hci_get_drvdata(hdev);
938 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
940 device_want_to_wakeup(hu);
946 static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
948 struct hci_uart *hu = hci_get_drvdata(hdev);
950 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
958 static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb)
960 /* We receive debug logs from chip as an ACL packets.
961 * Instead of sending the data to ACL to decode the
962 * received data, we are pushing them to the above layers
963 * as a diagnostic packet.
965 if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE)
966 return hci_recv_diag(hdev, skb);
968 return hci_recv_frame(hdev, skb);
971 static void qca_controller_memdump(struct work_struct *work)
973 struct qca_data *qca = container_of(work, struct qca_data,
975 struct hci_uart *hu = qca->hu;
977 struct qca_memdump_event_hdr *cmd_hdr;
978 struct qca_memdump_data *qca_memdump = qca->qca_memdump;
979 struct qca_dump_size *dump;
981 char nullBuff[QCA_DUMP_PACKET_SIZE] = { 0 };
985 enum qca_btsoc_type soc_type = qca_soc_type(hu);
987 while ((skb = skb_dequeue(&qca->rx_memdump_q))) {
989 mutex_lock(&qca->hci_memdump_lock);
990 /* Skip processing the received packets if timeout detected
991 * or memdump collection completed.
993 if (qca->memdump_state == QCA_MEMDUMP_TIMEOUT ||
994 qca->memdump_state == QCA_MEMDUMP_COLLECTED) {
995 mutex_unlock(&qca->hci_memdump_lock);
1000 qca_memdump = kzalloc(sizeof(struct qca_memdump_data),
1003 mutex_unlock(&qca->hci_memdump_lock);
1007 qca->qca_memdump = qca_memdump;
1010 qca->memdump_state = QCA_MEMDUMP_COLLECTING;
1011 cmd_hdr = (void *) skb->data;
1012 seq_no = __le16_to_cpu(cmd_hdr->seq_no);
1013 skb_pull(skb, sizeof(struct qca_memdump_event_hdr));
1017 /* This is the first frame of memdump packet from
1018 * the controller, Disable IBS to recevie dump
1019 * with out any interruption, ideally time required for
1020 * the controller to send the dump is 8 seconds. let us
1021 * start timer to handle this asynchronous activity.
1023 set_bit(QCA_IBS_DISABLED, &qca->flags);
1024 set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1025 dump = (void *) skb->data;
1026 dump_size = __le32_to_cpu(dump->dump_size);
1028 bt_dev_err(hu->hdev, "Rx invalid memdump size");
1031 qca->qca_memdump = NULL;
1032 mutex_unlock(&qca->hci_memdump_lock);
1036 bt_dev_info(hu->hdev, "QCA collecting dump of size:%u",
1038 queue_delayed_work(qca->workqueue,
1039 &qca->ctrl_memdump_timeout,
1040 msecs_to_jiffies(MEMDUMP_TIMEOUT_MS)
1043 skb_pull(skb, sizeof(dump_size));
1044 memdump_buf = vmalloc(dump_size);
1045 qca_memdump->ram_dump_size = dump_size;
1046 qca_memdump->memdump_buf_head = memdump_buf;
1047 qca_memdump->memdump_buf_tail = memdump_buf;
1050 memdump_buf = qca_memdump->memdump_buf_tail;
1052 /* If sequence no 0 is missed then there is no point in
1053 * accepting the other sequences.
1056 bt_dev_err(hu->hdev, "QCA: Discarding other packets");
1059 qca->qca_memdump = NULL;
1060 mutex_unlock(&qca->hci_memdump_lock);
1064 /* There could be chance of missing some packets from
1065 * the controller. In such cases let us store the dummy
1066 * packets in the buffer.
1068 /* For QCA6390, controller does not lost packets but
1069 * sequence number field of packet sometimes has error
1070 * bits, so skip this checking for missing packet.
1072 while ((seq_no > qca_memdump->current_seq_no + 1) &&
1073 (soc_type != QCA_QCA6390) &&
1074 seq_no != QCA_LAST_SEQUENCE_NUM) {
1075 bt_dev_err(hu->hdev, "QCA controller missed packet:%d",
1076 qca_memdump->current_seq_no);
1077 rx_size = qca_memdump->received_dump;
1078 rx_size += QCA_DUMP_PACKET_SIZE;
1079 if (rx_size > qca_memdump->ram_dump_size) {
1080 bt_dev_err(hu->hdev,
1081 "QCA memdump received %d, no space for missed packet",
1082 qca_memdump->received_dump);
1085 memcpy(memdump_buf, nullBuff, QCA_DUMP_PACKET_SIZE);
1086 memdump_buf = memdump_buf + QCA_DUMP_PACKET_SIZE;
1087 qca_memdump->received_dump += QCA_DUMP_PACKET_SIZE;
1088 qca_memdump->current_seq_no++;
1091 rx_size = qca_memdump->received_dump + skb->len;
1092 if (rx_size <= qca_memdump->ram_dump_size) {
1093 if ((seq_no != QCA_LAST_SEQUENCE_NUM) &&
1094 (seq_no != qca_memdump->current_seq_no))
1095 bt_dev_err(hu->hdev,
1096 "QCA memdump unexpected packet %d",
1098 bt_dev_dbg(hu->hdev,
1099 "QCA memdump packet %d with length %d",
1101 memcpy(memdump_buf, (unsigned char *)skb->data,
1103 memdump_buf = memdump_buf + skb->len;
1104 qca_memdump->memdump_buf_tail = memdump_buf;
1105 qca_memdump->current_seq_no = seq_no + 1;
1106 qca_memdump->received_dump += skb->len;
1108 bt_dev_err(hu->hdev,
1109 "QCA memdump received %d, no space for packet %d",
1110 qca_memdump->received_dump, seq_no);
1112 qca->qca_memdump = qca_memdump;
1114 if (seq_no == QCA_LAST_SEQUENCE_NUM) {
1115 bt_dev_info(hu->hdev,
1116 "QCA memdump Done, received %d, total %d",
1117 qca_memdump->received_dump,
1118 qca_memdump->ram_dump_size);
1119 memdump_buf = qca_memdump->memdump_buf_head;
1120 dev_coredumpv(&hu->serdev->dev, memdump_buf,
1121 qca_memdump->received_dump, GFP_KERNEL);
1122 cancel_delayed_work(&qca->ctrl_memdump_timeout);
1123 kfree(qca->qca_memdump);
1124 qca->qca_memdump = NULL;
1125 qca->memdump_state = QCA_MEMDUMP_COLLECTED;
1126 clear_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1129 mutex_unlock(&qca->hci_memdump_lock);
1134 static int qca_controller_memdump_event(struct hci_dev *hdev,
1135 struct sk_buff *skb)
1137 struct hci_uart *hu = hci_get_drvdata(hdev);
1138 struct qca_data *qca = hu->priv;
1140 set_bit(QCA_SSR_TRIGGERED, &qca->flags);
1141 skb_queue_tail(&qca->rx_memdump_q, skb);
1142 queue_work(qca->workqueue, &qca->ctrl_memdump_evt);
1147 static int qca_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
1149 struct hci_uart *hu = hci_get_drvdata(hdev);
1150 struct qca_data *qca = hu->priv;
1152 if (test_bit(QCA_DROP_VENDOR_EVENT, &qca->flags)) {
1153 struct hci_event_hdr *hdr = (void *)skb->data;
1155 /* For the WCN3990 the vendor command for a baudrate change
1156 * isn't sent as synchronous HCI command, because the
1157 * controller sends the corresponding vendor event with the
1158 * new baudrate. The event is received and properly decoded
1159 * after changing the baudrate of the host port. It needs to
1160 * be dropped, otherwise it can be misinterpreted as
1161 * response to a later firmware download command (also a
1165 if (hdr->evt == HCI_EV_VENDOR)
1166 complete(&qca->drop_ev_comp);
1172 /* We receive chip memory dump as an event packet, With a dedicated
1173 * handler followed by a hardware error event. When this event is
1174 * received we store dump into a file before closing hci. This
1175 * dump will help in triaging the issues.
1177 if ((skb->data[0] == HCI_VENDOR_PKT) &&
1178 (get_unaligned_be16(skb->data + 2) == QCA_SSR_DUMP_HANDLE))
1179 return qca_controller_memdump_event(hdev, skb);
1181 return hci_recv_frame(hdev, skb);
1184 #define QCA_IBS_SLEEP_IND_EVENT \
1185 .type = HCI_IBS_SLEEP_IND, \
1189 .maxlen = HCI_MAX_IBS_SIZE
1191 #define QCA_IBS_WAKE_IND_EVENT \
1192 .type = HCI_IBS_WAKE_IND, \
1196 .maxlen = HCI_MAX_IBS_SIZE
1198 #define QCA_IBS_WAKE_ACK_EVENT \
1199 .type = HCI_IBS_WAKE_ACK, \
1203 .maxlen = HCI_MAX_IBS_SIZE
1205 static const struct h4_recv_pkt qca_recv_pkts[] = {
1206 { H4_RECV_ACL, .recv = qca_recv_acl_data },
1207 { H4_RECV_SCO, .recv = hci_recv_frame },
1208 { H4_RECV_EVENT, .recv = qca_recv_event },
1209 { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind },
1210 { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack },
1211 { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
1214 static int qca_recv(struct hci_uart *hu, const void *data, int count)
1216 struct qca_data *qca = hu->priv;
1218 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
1221 qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
1222 qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
1223 if (IS_ERR(qca->rx_skb)) {
1224 int err = PTR_ERR(qca->rx_skb);
1225 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
1233 static struct sk_buff *qca_dequeue(struct hci_uart *hu)
1235 struct qca_data *qca = hu->priv;
1237 return skb_dequeue(&qca->txq);
1240 static uint8_t qca_get_baudrate_value(int speed)
1244 return QCA_BAUDRATE_9600;
1246 return QCA_BAUDRATE_19200;
1248 return QCA_BAUDRATE_38400;
1250 return QCA_BAUDRATE_57600;
1252 return QCA_BAUDRATE_115200;
1254 return QCA_BAUDRATE_230400;
1256 return QCA_BAUDRATE_460800;
1258 return QCA_BAUDRATE_500000;
1260 return QCA_BAUDRATE_921600;
1262 return QCA_BAUDRATE_1000000;
1264 return QCA_BAUDRATE_2000000;
1266 return QCA_BAUDRATE_3000000;
1268 return QCA_BAUDRATE_3200000;
1270 return QCA_BAUDRATE_3500000;
1272 return QCA_BAUDRATE_115200;
1276 static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
1278 struct hci_uart *hu = hci_get_drvdata(hdev);
1279 struct qca_data *qca = hu->priv;
1280 struct sk_buff *skb;
1281 u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
1283 if (baudrate > QCA_BAUDRATE_3200000)
1288 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1290 bt_dev_err(hdev, "Failed to allocate baudrate packet");
1294 /* Assign commands to change baudrate and packet type. */
1295 skb_put_data(skb, cmd, sizeof(cmd));
1296 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1298 skb_queue_tail(&qca->txq, skb);
1299 hci_uart_tx_wakeup(hu);
1301 /* Wait for the baudrate change request to be sent */
1303 while (!skb_queue_empty(&qca->txq))
1304 usleep_range(100, 200);
1307 serdev_device_wait_until_sent(hu->serdev,
1308 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1310 /* Give the controller time to process the request */
1311 if (qca_is_wcn399x(qca_soc_type(hu)))
1312 usleep_range(1000, 10000);
1319 static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
1322 serdev_device_set_baudrate(hu->serdev, speed);
1324 hci_uart_set_baudrate(hu, speed);
1327 static int qca_send_power_pulse(struct hci_uart *hu, bool on)
1330 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
1331 u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE;
1333 /* These power pulses are single byte command which are sent
1334 * at required baudrate to wcn3990. On wcn3990, we have an external
1335 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1336 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1337 * and also we use the same power inputs to turn on and off for
1338 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1339 * we send a power on pulse at 115200 bps. This algorithm will help to
1340 * save power. Disabling hardware flow control is mandatory while
1341 * sending power pulses to SoC.
1343 bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd);
1345 serdev_device_write_flush(hu->serdev);
1346 hci_uart_set_flow_control(hu, true);
1347 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1349 bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd);
1353 serdev_device_wait_until_sent(hu->serdev, timeout);
1354 hci_uart_set_flow_control(hu, false);
1356 /* Give to controller time to boot/shutdown */
1360 usleep_range(1000, 10000);
1365 static unsigned int qca_get_speed(struct hci_uart *hu,
1366 enum qca_speed_type speed_type)
1368 unsigned int speed = 0;
1370 if (speed_type == QCA_INIT_SPEED) {
1372 speed = hu->init_speed;
1373 else if (hu->proto->init_speed)
1374 speed = hu->proto->init_speed;
1377 speed = hu->oper_speed;
1378 else if (hu->proto->oper_speed)
1379 speed = hu->proto->oper_speed;
1385 static int qca_check_speeds(struct hci_uart *hu)
1387 if (qca_is_wcn399x(qca_soc_type(hu))) {
1388 if (!qca_get_speed(hu, QCA_INIT_SPEED) &&
1389 !qca_get_speed(hu, QCA_OPER_SPEED))
1392 if (!qca_get_speed(hu, QCA_INIT_SPEED) ||
1393 !qca_get_speed(hu, QCA_OPER_SPEED))
1400 static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type)
1402 unsigned int speed, qca_baudrate;
1403 struct qca_data *qca = hu->priv;
1406 if (speed_type == QCA_INIT_SPEED) {
1407 speed = qca_get_speed(hu, QCA_INIT_SPEED);
1409 host_set_baudrate(hu, speed);
1411 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1413 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1417 /* Disable flow control for wcn3990 to deassert RTS while
1418 * changing the baudrate of chip and host.
1420 if (qca_is_wcn399x(soc_type))
1421 hci_uart_set_flow_control(hu, true);
1423 if (soc_type == QCA_WCN3990) {
1424 reinit_completion(&qca->drop_ev_comp);
1425 set_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1428 qca_baudrate = qca_get_baudrate_value(speed);
1429 bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed);
1430 ret = qca_set_baudrate(hu->hdev, qca_baudrate);
1434 host_set_baudrate(hu, speed);
1437 if (qca_is_wcn399x(soc_type))
1438 hci_uart_set_flow_control(hu, false);
1440 if (soc_type == QCA_WCN3990) {
1441 /* Wait for the controller to send the vendor event
1442 * for the baudrate change command.
1444 if (!wait_for_completion_timeout(&qca->drop_ev_comp,
1445 msecs_to_jiffies(100))) {
1446 bt_dev_err(hu->hdev,
1447 "Failed to change controller baudrate\n");
1451 clear_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1458 static int qca_send_crashbuffer(struct hci_uart *hu)
1460 struct qca_data *qca = hu->priv;
1461 struct sk_buff *skb;
1463 skb = bt_skb_alloc(QCA_CRASHBYTE_PACKET_LEN, GFP_KERNEL);
1465 bt_dev_err(hu->hdev, "Failed to allocate memory for skb packet");
1469 /* We forcefully crash the controller, by sending 0xfb byte for
1470 * 1024 times. We also might have chance of losing data, To be
1471 * on safer side we send 1096 bytes to the SoC.
1473 memset(skb_put(skb, QCA_CRASHBYTE_PACKET_LEN), QCA_MEMDUMP_BYTE,
1474 QCA_CRASHBYTE_PACKET_LEN);
1475 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1476 bt_dev_info(hu->hdev, "crash the soc to collect controller dump");
1477 skb_queue_tail(&qca->txq, skb);
1478 hci_uart_tx_wakeup(hu);
1483 static void qca_wait_for_dump_collection(struct hci_dev *hdev)
1485 struct hci_uart *hu = hci_get_drvdata(hdev);
1486 struct qca_data *qca = hu->priv;
1488 wait_on_bit_timeout(&qca->flags, QCA_MEMDUMP_COLLECTION,
1489 TASK_UNINTERRUPTIBLE, MEMDUMP_TIMEOUT_MS);
1491 clear_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1494 static void qca_hw_error(struct hci_dev *hdev, u8 code)
1496 struct hci_uart *hu = hci_get_drvdata(hdev);
1497 struct qca_data *qca = hu->priv;
1499 set_bit(QCA_SSR_TRIGGERED, &qca->flags);
1500 set_bit(QCA_HW_ERROR_EVENT, &qca->flags);
1501 bt_dev_info(hdev, "mem_dump_status: %d", qca->memdump_state);
1503 if (qca->memdump_state == QCA_MEMDUMP_IDLE) {
1504 /* If hardware error event received for other than QCA
1505 * soc memory dump event, then we need to crash the SOC
1506 * and wait here for 8 seconds to get the dump packets.
1507 * This will block main thread to be on hold until we
1510 set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1511 qca_send_crashbuffer(hu);
1512 qca_wait_for_dump_collection(hdev);
1513 } else if (qca->memdump_state == QCA_MEMDUMP_COLLECTING) {
1514 /* Let us wait here until memory dump collected or
1515 * memory dump timer expired.
1517 bt_dev_info(hdev, "waiting for dump to complete");
1518 qca_wait_for_dump_collection(hdev);
1521 mutex_lock(&qca->hci_memdump_lock);
1522 if (qca->memdump_state != QCA_MEMDUMP_COLLECTED) {
1523 bt_dev_err(hu->hdev, "clearing allocated memory due to memdump timeout");
1524 if (qca->qca_memdump) {
1525 vfree(qca->qca_memdump->memdump_buf_head);
1526 kfree(qca->qca_memdump);
1527 qca->qca_memdump = NULL;
1529 qca->memdump_state = QCA_MEMDUMP_TIMEOUT;
1530 cancel_delayed_work(&qca->ctrl_memdump_timeout);
1532 mutex_unlock(&qca->hci_memdump_lock);
1534 if (qca->memdump_state == QCA_MEMDUMP_TIMEOUT ||
1535 qca->memdump_state == QCA_MEMDUMP_COLLECTED) {
1536 cancel_work_sync(&qca->ctrl_memdump_evt);
1537 skb_queue_purge(&qca->rx_memdump_q);
1540 clear_bit(QCA_HW_ERROR_EVENT, &qca->flags);
1543 static void qca_cmd_timeout(struct hci_dev *hdev)
1545 struct hci_uart *hu = hci_get_drvdata(hdev);
1546 struct qca_data *qca = hu->priv;
1548 set_bit(QCA_SSR_TRIGGERED, &qca->flags);
1549 if (qca->memdump_state == QCA_MEMDUMP_IDLE) {
1550 set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1551 qca_send_crashbuffer(hu);
1552 qca_wait_for_dump_collection(hdev);
1553 } else if (qca->memdump_state == QCA_MEMDUMP_COLLECTING) {
1554 /* Let us wait here until memory dump collected or
1555 * memory dump timer expired.
1557 bt_dev_info(hdev, "waiting for dump to complete");
1558 qca_wait_for_dump_collection(hdev);
1561 mutex_lock(&qca->hci_memdump_lock);
1562 if (qca->memdump_state != QCA_MEMDUMP_COLLECTED) {
1563 qca->memdump_state = QCA_MEMDUMP_TIMEOUT;
1564 if (!test_bit(QCA_HW_ERROR_EVENT, &qca->flags)) {
1565 /* Inject hw error event to reset the device
1568 hci_reset_dev(hu->hdev);
1571 mutex_unlock(&qca->hci_memdump_lock);
1574 static bool qca_prevent_wake(struct hci_dev *hdev)
1576 struct hci_uart *hu = hci_get_drvdata(hdev);
1579 /* UART driver handles the interrupt from BT SoC.So we need to use
1580 * device handle of UART driver to get the status of device may wakeup.
1582 wakeup = device_may_wakeup(hu->serdev->ctrl->dev.parent);
1583 bt_dev_dbg(hu->hdev, "wakeup status : %d", wakeup);
1588 static int qca_wcn3990_init(struct hci_uart *hu)
1590 struct qca_serdev *qcadev;
1593 /* Check for vregs status, may be hci down has turned
1594 * off the voltage regulator.
1596 qcadev = serdev_device_get_drvdata(hu->serdev);
1597 if (!qcadev->bt_power->vregs_on) {
1598 serdev_device_close(hu->serdev);
1599 ret = qca_regulator_enable(qcadev);
1603 ret = serdev_device_open(hu->serdev);
1605 bt_dev_err(hu->hdev, "failed to open port");
1610 /* Forcefully enable wcn3990 to enter in to boot mode. */
1611 host_set_baudrate(hu, 2400);
1612 ret = qca_send_power_pulse(hu, false);
1616 qca_set_speed(hu, QCA_INIT_SPEED);
1617 ret = qca_send_power_pulse(hu, true);
1621 /* Now the device is in ready state to communicate with host.
1622 * To sync host with device we need to reopen port.
1623 * Without this, we will have RTS and CTS synchronization
1626 serdev_device_close(hu->serdev);
1627 ret = serdev_device_open(hu->serdev);
1629 bt_dev_err(hu->hdev, "failed to open port");
1633 hci_uart_set_flow_control(hu, false);
1638 static int qca_power_on(struct hci_dev *hdev)
1640 struct hci_uart *hu = hci_get_drvdata(hdev);
1641 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1642 struct qca_serdev *qcadev;
1643 struct qca_data *qca = hu->priv;
1646 /* Non-serdev device usually is powered by external power
1647 * and don't need additional action in driver for power on
1652 if (qca_is_wcn399x(soc_type)) {
1653 ret = qca_wcn3990_init(hu);
1655 qcadev = serdev_device_get_drvdata(hu->serdev);
1656 if (qcadev->bt_en) {
1657 gpiod_set_value_cansleep(qcadev->bt_en, 1);
1658 /* Controller needs time to bootup. */
1663 clear_bit(QCA_BT_OFF, &qca->flags);
1667 static int qca_setup(struct hci_uart *hu)
1669 struct hci_dev *hdev = hu->hdev;
1670 struct qca_data *qca = hu->priv;
1671 unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
1672 unsigned int retries = 0;
1673 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1674 const char *firmware_name = qca_get_firmware_name(hu);
1676 struct qca_btsoc_version ver;
1678 ret = qca_check_speeds(hu);
1682 clear_bit(QCA_ROM_FW, &qca->flags);
1683 /* Patch downloading has to be done without IBS mode */
1684 set_bit(QCA_IBS_DISABLED, &qca->flags);
1686 /* Enable controller to do both LE scan and BR/EDR inquiry
1689 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1691 bt_dev_info(hdev, "setting up %s",
1692 qca_is_wcn399x(soc_type) ? "wcn399x" : "ROME/QCA6390");
1694 qca->memdump_state = QCA_MEMDUMP_IDLE;
1697 ret = qca_power_on(hdev);
1701 clear_bit(QCA_SSR_TRIGGERED, &qca->flags);
1703 if (qca_is_wcn399x(soc_type)) {
1704 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
1706 ret = qca_read_soc_version(hdev, &ver, soc_type);
1710 qca_set_speed(hu, QCA_INIT_SPEED);
1713 /* Setup user speed if needed */
1714 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1716 ret = qca_set_speed(hu, QCA_OPER_SPEED);
1720 qca_baudrate = qca_get_baudrate_value(speed);
1723 if (!qca_is_wcn399x(soc_type)) {
1724 /* Get QCA version information */
1725 ret = qca_read_soc_version(hdev, &ver, soc_type);
1730 /* Setup patch / NVM configurations */
1731 ret = qca_uart_setup(hdev, qca_baudrate, soc_type, ver,
1734 clear_bit(QCA_IBS_DISABLED, &qca->flags);
1735 qca_debugfs_init(hdev);
1736 hu->hdev->hw_error = qca_hw_error;
1737 hu->hdev->cmd_timeout = qca_cmd_timeout;
1738 hu->hdev->prevent_wake = qca_prevent_wake;
1739 } else if (ret == -ENOENT) {
1740 /* No patch/nvm-config found, run with original fw/config */
1741 set_bit(QCA_ROM_FW, &qca->flags);
1743 } else if (ret == -EAGAIN) {
1745 * Userspace firmware loader will return -EAGAIN in case no
1746 * patch/nvm-config is found, so run with original fw/config.
1748 set_bit(QCA_ROM_FW, &qca->flags);
1753 if (ret && retries < MAX_INIT_RETRIES) {
1754 bt_dev_warn(hdev, "Retry BT power ON:%d", retries);
1755 qca_power_shutdown(hu);
1757 serdev_device_close(hu->serdev);
1758 ret = serdev_device_open(hu->serdev);
1760 bt_dev_err(hdev, "failed to open port");
1769 if (soc_type == QCA_ROME)
1770 hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
1772 hu->hdev->set_bdaddr = qca_set_bdaddr;
1777 static const struct hci_uart_proto qca_proto = {
1781 .init_speed = 115200,
1782 .oper_speed = 3000000,
1788 .enqueue = qca_enqueue,
1789 .dequeue = qca_dequeue,
1792 static const struct qca_device_data qca_soc_data_wcn3990 = {
1793 .soc_type = QCA_WCN3990,
1794 .vregs = (struct qca_vreg []) {
1797 { "vddrf", 300000 },
1798 { "vddch0", 450000 },
1803 static const struct qca_device_data qca_soc_data_wcn3991 = {
1804 .soc_type = QCA_WCN3991,
1805 .vregs = (struct qca_vreg []) {
1808 { "vddrf", 300000 },
1809 { "vddch0", 450000 },
1812 .capabilities = QCA_CAP_WIDEBAND_SPEECH | QCA_CAP_VALID_LE_STATES,
1815 static const struct qca_device_data qca_soc_data_wcn3998 = {
1816 .soc_type = QCA_WCN3998,
1817 .vregs = (struct qca_vreg []) {
1820 { "vddrf", 300000 },
1821 { "vddch0", 450000 },
1826 static const struct qca_device_data qca_soc_data_qca6390 = {
1827 .soc_type = QCA_QCA6390,
1831 static void qca_power_shutdown(struct hci_uart *hu)
1833 struct qca_serdev *qcadev;
1834 struct qca_data *qca = hu->priv;
1835 unsigned long flags;
1836 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1838 qcadev = serdev_device_get_drvdata(hu->serdev);
1840 /* From this point we go into power off state. But serial port is
1841 * still open, stop queueing the IBS data and flush all the buffered
1844 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
1845 set_bit(QCA_IBS_DISABLED, &qca->flags);
1847 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1849 /* Non-serdev device usually is powered by external power
1850 * and don't need additional action in driver for power down
1855 if (qca_is_wcn399x(soc_type)) {
1856 host_set_baudrate(hu, 2400);
1857 qca_send_power_pulse(hu, false);
1858 qca_regulator_disable(qcadev);
1859 } else if (qcadev->bt_en) {
1860 gpiod_set_value_cansleep(qcadev->bt_en, 0);
1863 set_bit(QCA_BT_OFF, &qca->flags);
1866 static int qca_power_off(struct hci_dev *hdev)
1868 struct hci_uart *hu = hci_get_drvdata(hdev);
1869 struct qca_data *qca = hu->priv;
1870 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1872 hu->hdev->hw_error = NULL;
1873 hu->hdev->cmd_timeout = NULL;
1875 /* Stop sending shutdown command if soc crashes. */
1876 if (soc_type != QCA_ROME
1877 && qca->memdump_state == QCA_MEMDUMP_IDLE) {
1878 qca_send_pre_shutdown_cmd(hdev);
1879 usleep_range(8000, 10000);
1882 qca_power_shutdown(hu);
1886 static int qca_regulator_enable(struct qca_serdev *qcadev)
1888 struct qca_power *power = qcadev->bt_power;
1891 /* Already enabled */
1892 if (power->vregs_on)
1895 BT_DBG("enabling %d regulators)", power->num_vregs);
1897 ret = regulator_bulk_enable(power->num_vregs, power->vreg_bulk);
1901 power->vregs_on = true;
1903 ret = clk_prepare_enable(qcadev->susclk);
1905 qca_regulator_disable(qcadev);
1910 static void qca_regulator_disable(struct qca_serdev *qcadev)
1912 struct qca_power *power;
1917 power = qcadev->bt_power;
1919 /* Already disabled? */
1920 if (!power->vregs_on)
1923 regulator_bulk_disable(power->num_vregs, power->vreg_bulk);
1924 power->vregs_on = false;
1926 clk_disable_unprepare(qcadev->susclk);
1929 static int qca_init_regulators(struct qca_power *qca,
1930 const struct qca_vreg *vregs, size_t num_vregs)
1932 struct regulator_bulk_data *bulk;
1936 bulk = devm_kcalloc(qca->dev, num_vregs, sizeof(*bulk), GFP_KERNEL);
1940 for (i = 0; i < num_vregs; i++)
1941 bulk[i].supply = vregs[i].name;
1943 ret = devm_regulator_bulk_get(qca->dev, num_vregs, bulk);
1947 for (i = 0; i < num_vregs; i++) {
1948 ret = regulator_set_load(bulk[i].consumer, vregs[i].load_uA);
1953 qca->vreg_bulk = bulk;
1954 qca->num_vregs = num_vregs;
1959 static int qca_serdev_probe(struct serdev_device *serdev)
1961 struct qca_serdev *qcadev;
1962 struct hci_dev *hdev;
1963 const struct qca_device_data *data;
1965 bool power_ctrl_enabled = true;
1967 qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
1971 qcadev->serdev_hu.serdev = serdev;
1972 data = device_get_match_data(&serdev->dev);
1973 serdev_device_set_drvdata(serdev, qcadev);
1974 device_property_read_string(&serdev->dev, "firmware-name",
1975 &qcadev->firmware_name);
1976 device_property_read_u32(&serdev->dev, "max-speed",
1977 &qcadev->oper_speed);
1978 if (!qcadev->oper_speed)
1979 BT_DBG("UART will pick default operating speed");
1981 if (data && qca_is_wcn399x(data->soc_type)) {
1982 qcadev->btsoc_type = data->soc_type;
1983 qcadev->bt_power = devm_kzalloc(&serdev->dev,
1984 sizeof(struct qca_power),
1986 if (!qcadev->bt_power)
1989 qcadev->bt_power->dev = &serdev->dev;
1990 err = qca_init_regulators(qcadev->bt_power, data->vregs,
1993 BT_ERR("Failed to init regulators:%d", err);
1997 qcadev->bt_power->vregs_on = false;
1999 qcadev->susclk = devm_clk_get_optional(&serdev->dev, NULL);
2000 if (IS_ERR(qcadev->susclk)) {
2001 dev_err(&serdev->dev, "failed to acquire clk\n");
2002 return PTR_ERR(qcadev->susclk);
2005 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
2007 BT_ERR("wcn3990 serdev registration failed");
2012 qcadev->btsoc_type = data->soc_type;
2014 qcadev->btsoc_type = QCA_ROME;
2016 qcadev->bt_en = devm_gpiod_get_optional(&serdev->dev, "enable",
2018 if (!qcadev->bt_en) {
2019 dev_warn(&serdev->dev, "failed to acquire enable gpio\n");
2020 power_ctrl_enabled = false;
2023 qcadev->susclk = devm_clk_get_optional(&serdev->dev, NULL);
2024 if (IS_ERR(qcadev->susclk)) {
2025 dev_warn(&serdev->dev, "failed to acquire clk\n");
2026 return PTR_ERR(qcadev->susclk);
2028 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
2032 err = clk_prepare_enable(qcadev->susclk);
2036 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
2038 BT_ERR("Rome serdev registration failed");
2039 clk_disable_unprepare(qcadev->susclk);
2044 hdev = qcadev->serdev_hu.hdev;
2046 if (power_ctrl_enabled) {
2047 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
2048 hdev->shutdown = qca_power_off;
2052 /* Wideband speech support must be set per driver since it can't
2053 * be queried via hci. Same with the valid le states quirk.
2055 if (data->capabilities & QCA_CAP_WIDEBAND_SPEECH)
2056 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
2059 if (data->capabilities & QCA_CAP_VALID_LE_STATES)
2060 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
2066 static void qca_serdev_remove(struct serdev_device *serdev)
2068 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
2069 struct qca_power *power = qcadev->bt_power;
2071 if (qca_is_wcn399x(qcadev->btsoc_type) && power->vregs_on)
2072 qca_power_shutdown(&qcadev->serdev_hu);
2073 else if (qcadev->susclk)
2074 clk_disable_unprepare(qcadev->susclk);
2076 hci_uart_unregister_device(&qcadev->serdev_hu);
2079 static void qca_serdev_shutdown(struct device *dev)
2082 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
2083 struct serdev_device *serdev = to_serdev_device(dev);
2084 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
2085 const u8 ibs_wake_cmd[] = { 0xFD };
2086 const u8 edl_reset_soc_cmd[] = { 0x01, 0x00, 0xFC, 0x01, 0x05 };
2088 if (qcadev->btsoc_type == QCA_QCA6390) {
2089 serdev_device_write_flush(serdev);
2090 ret = serdev_device_write_buf(serdev, ibs_wake_cmd,
2091 sizeof(ibs_wake_cmd));
2093 BT_ERR("QCA send IBS_WAKE_IND error: %d", ret);
2096 serdev_device_wait_until_sent(serdev, timeout);
2097 usleep_range(8000, 10000);
2099 serdev_device_write_flush(serdev);
2100 ret = serdev_device_write_buf(serdev, edl_reset_soc_cmd,
2101 sizeof(edl_reset_soc_cmd));
2103 BT_ERR("QCA send EDL_RESET_REQ error: %d", ret);
2106 serdev_device_wait_until_sent(serdev, timeout);
2107 usleep_range(8000, 10000);
2111 static int __maybe_unused qca_suspend(struct device *dev)
2113 struct serdev_device *serdev = to_serdev_device(dev);
2114 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
2115 struct hci_uart *hu = &qcadev->serdev_hu;
2116 struct qca_data *qca = hu->priv;
2117 unsigned long flags;
2118 bool tx_pending = false;
2121 u32 wait_timeout = 0;
2123 set_bit(QCA_SUSPENDING, &qca->flags);
2125 /* if BT SoC is running with default firmware then it does not
2126 * support in-band sleep
2128 if (test_bit(QCA_ROM_FW, &qca->flags))
2131 /* During SSR after memory dump collection, controller will be
2132 * powered off and then powered on.If controller is powered off
2133 * during SSR then we should wait until SSR is completed.
2135 if (test_bit(QCA_BT_OFF, &qca->flags) &&
2136 !test_bit(QCA_SSR_TRIGGERED, &qca->flags))
2139 if (test_bit(QCA_IBS_DISABLED, &qca->flags) ||
2140 test_bit(QCA_SSR_TRIGGERED, &qca->flags)) {
2141 wait_timeout = test_bit(QCA_SSR_TRIGGERED, &qca->flags) ?
2142 IBS_DISABLE_SSR_TIMEOUT_MS :
2143 FW_DOWNLOAD_TIMEOUT_MS;
2145 /* QCA_IBS_DISABLED flag is set to true, During FW download
2146 * and during memory dump collection. It is reset to false,
2147 * After FW download complete.
2149 wait_on_bit_timeout(&qca->flags, QCA_IBS_DISABLED,
2150 TASK_UNINTERRUPTIBLE, msecs_to_jiffies(wait_timeout));
2152 if (test_bit(QCA_IBS_DISABLED, &qca->flags)) {
2153 bt_dev_err(hu->hdev, "SSR or FW download time out");
2159 cancel_work_sync(&qca->ws_awake_device);
2160 cancel_work_sync(&qca->ws_awake_rx);
2162 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
2163 flags, SINGLE_DEPTH_NESTING);
2165 switch (qca->tx_ibs_state) {
2166 case HCI_IBS_TX_WAKING:
2167 del_timer(&qca->wake_retrans_timer);
2169 case HCI_IBS_TX_AWAKE:
2170 del_timer(&qca->tx_idle_timer);
2172 serdev_device_write_flush(hu->serdev);
2173 cmd = HCI_IBS_SLEEP_IND;
2174 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
2177 BT_ERR("Failed to send SLEEP to device");
2181 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
2182 qca->ibs_sent_slps++;
2186 case HCI_IBS_TX_ASLEEP:
2190 BT_ERR("Spurious tx state %d", qca->tx_ibs_state);
2195 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
2201 serdev_device_wait_until_sent(hu->serdev,
2202 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
2203 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
2206 /* Wait for HCI_IBS_SLEEP_IND sent by device to indicate its Tx is going
2207 * to sleep, so that the packet does not wake the system later.
2209 ret = wait_event_interruptible_timeout(qca->suspend_wait_q,
2210 qca->rx_ibs_state == HCI_IBS_RX_ASLEEP,
2211 msecs_to_jiffies(IBS_BTSOC_TX_IDLE_TIMEOUT_MS));
2220 clear_bit(QCA_SUSPENDING, &qca->flags);
2225 static int __maybe_unused qca_resume(struct device *dev)
2227 struct serdev_device *serdev = to_serdev_device(dev);
2228 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
2229 struct hci_uart *hu = &qcadev->serdev_hu;
2230 struct qca_data *qca = hu->priv;
2232 clear_bit(QCA_SUSPENDING, &qca->flags);
2237 static SIMPLE_DEV_PM_OPS(qca_pm_ops, qca_suspend, qca_resume);
2240 static const struct of_device_id qca_bluetooth_of_match[] = {
2241 { .compatible = "qcom,qca6174-bt" },
2242 { .compatible = "qcom,qca6390-bt", .data = &qca_soc_data_qca6390},
2243 { .compatible = "qcom,qca9377-bt" },
2244 { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990},
2245 { .compatible = "qcom,wcn3991-bt", .data = &qca_soc_data_wcn3991},
2246 { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998},
2249 MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
2253 static const struct acpi_device_id qca_bluetooth_acpi_match[] = {
2254 { "QCOM6390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2255 { "DLA16390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2256 { "DLB16390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2257 { "DLB26390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2260 MODULE_DEVICE_TABLE(acpi, qca_bluetooth_acpi_match);
2264 static struct serdev_device_driver qca_serdev_driver = {
2265 .probe = qca_serdev_probe,
2266 .remove = qca_serdev_remove,
2268 .name = "hci_uart_qca",
2269 .of_match_table = of_match_ptr(qca_bluetooth_of_match),
2270 .acpi_match_table = ACPI_PTR(qca_bluetooth_acpi_match),
2271 .shutdown = qca_serdev_shutdown,
2276 int __init qca_init(void)
2278 serdev_device_driver_register(&qca_serdev_driver);
2280 return hci_uart_register_proto(&qca_proto);
2283 int __exit qca_deinit(void)
2285 serdev_device_driver_unregister(&qca_serdev_driver);
2287 return hci_uart_unregister_proto(&qca_proto);