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/platform_device.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/serdev.h>
32 #include <linux/mutex.h>
33 #include <asm/unaligned.h>
35 #include <net/bluetooth/bluetooth.h>
36 #include <net/bluetooth/hci_core.h>
41 /* HCI_IBS protocol messages */
42 #define HCI_IBS_SLEEP_IND 0xFE
43 #define HCI_IBS_WAKE_IND 0xFD
44 #define HCI_IBS_WAKE_ACK 0xFC
45 #define HCI_MAX_IBS_SIZE 10
47 #define IBS_WAKE_RETRANS_TIMEOUT_MS 100
48 #define IBS_BTSOC_TX_IDLE_TIMEOUT_MS 40
49 #define IBS_HOST_TX_IDLE_TIMEOUT_MS 2000
50 #define CMD_TRANS_TIMEOUT_MS 100
51 #define MEMDUMP_TIMEOUT_MS 8000
54 #define SUSCLK_RATE_32KHZ 32768
56 /* Controller debug log header */
57 #define QCA_DEBUG_HANDLE 0x2EDC
59 /* max retry count when init fails */
60 #define MAX_INIT_RETRIES 3
62 /* Controller dump header */
63 #define QCA_SSR_DUMP_HANDLE 0x0108
64 #define QCA_DUMP_PACKET_SIZE 255
65 #define QCA_LAST_SEQUENCE_NUM 0xFFFF
66 #define QCA_CRASHBYTE_PACKET_LEN 1096
67 #define QCA_MEMDUMP_BYTE 0xFB
71 QCA_DROP_VENDOR_EVENT
,
73 QCA_MEMDUMP_COLLECTION
,
78 /* HCI_IBS transmit side sleep protocol states */
85 /* HCI_IBS receive side sleep protocol states */
91 /* HCI_IBS transmit and receive side clock state vote */
92 enum hci_ibs_clock_state_vote
{
93 HCI_IBS_VOTE_STATS_UPDATE
,
94 HCI_IBS_TX_VOTE_CLOCK_ON
,
95 HCI_IBS_TX_VOTE_CLOCK_OFF
,
96 HCI_IBS_RX_VOTE_CLOCK_ON
,
97 HCI_IBS_RX_VOTE_CLOCK_OFF
,
100 /* Controller memory dump states */
101 enum qca_memdump_states
{
103 QCA_MEMDUMP_COLLECTING
,
104 QCA_MEMDUMP_COLLECTED
,
108 struct qca_memdump_data
{
109 char *memdump_buf_head
;
110 char *memdump_buf_tail
;
115 struct qca_memdump_event_hdr
{
124 struct qca_dump_size
{
130 struct sk_buff
*rx_skb
;
131 struct sk_buff_head txq
;
132 struct sk_buff_head tx_wait_q
; /* HCI_IBS wait queue */
133 struct sk_buff_head rx_memdump_q
; /* Memdump wait queue */
134 spinlock_t hci_ibs_lock
; /* HCI_IBS state lock */
135 u8 tx_ibs_state
; /* HCI_IBS transmit side power state*/
136 u8 rx_ibs_state
; /* HCI_IBS receive side power state */
137 bool tx_vote
; /* Clock must be on for TX */
138 bool rx_vote
; /* Clock must be on for RX */
139 struct timer_list tx_idle_timer
;
141 struct timer_list wake_retrans_timer
;
143 struct workqueue_struct
*workqueue
;
144 struct work_struct ws_awake_rx
;
145 struct work_struct ws_awake_device
;
146 struct work_struct ws_rx_vote_off
;
147 struct work_struct ws_tx_vote_off
;
148 struct work_struct ctrl_memdump_evt
;
149 struct delayed_work ctrl_memdump_timeout
;
150 struct qca_memdump_data
*qca_memdump
;
152 struct completion drop_ev_comp
;
153 wait_queue_head_t suspend_wait_q
;
154 enum qca_memdump_states memdump_state
;
155 struct mutex hci_memdump_lock
;
157 /* For debugging purpose */
175 enum qca_speed_type
{
181 * Voltage regulator information required for configuring the
182 * QCA Bluetooth chipset
186 unsigned int load_uA
;
189 struct qca_vreg_data
{
190 enum qca_btsoc_type soc_type
;
191 struct qca_vreg
*vregs
;
196 * Platform data for the QCA Bluetooth power driver.
200 struct regulator_bulk_data
*vreg_bulk
;
206 struct hci_uart serdev_hu
;
207 struct gpio_desc
*bt_en
;
209 enum qca_btsoc_type btsoc_type
;
210 struct qca_power
*bt_power
;
213 const char *firmware_name
;
216 static int qca_regulator_enable(struct qca_serdev
*qcadev
);
217 static void qca_regulator_disable(struct qca_serdev
*qcadev
);
218 static void qca_power_shutdown(struct hci_uart
*hu
);
219 static int qca_power_off(struct hci_dev
*hdev
);
220 static void qca_controller_memdump(struct work_struct
*work
);
222 static enum qca_btsoc_type
qca_soc_type(struct hci_uart
*hu
)
224 enum qca_btsoc_type soc_type
;
227 struct qca_serdev
*qsd
= serdev_device_get_drvdata(hu
->serdev
);
229 soc_type
= qsd
->btsoc_type
;
237 static const char *qca_get_firmware_name(struct hci_uart
*hu
)
240 struct qca_serdev
*qsd
= serdev_device_get_drvdata(hu
->serdev
);
242 return qsd
->firmware_name
;
248 static void __serial_clock_on(struct tty_struct
*tty
)
250 /* TODO: Some chipset requires to enable UART clock on client
251 * side to save power consumption or manual work is required.
252 * Please put your code to control UART clock here if needed
256 static void __serial_clock_off(struct tty_struct
*tty
)
258 /* TODO: Some chipset requires to disable UART clock on client
259 * side to save power consumption or manual work is required.
260 * Please put your code to control UART clock off here if needed
264 /* serial_clock_vote needs to be called with the ibs lock held */
265 static void serial_clock_vote(unsigned long vote
, struct hci_uart
*hu
)
267 struct qca_data
*qca
= hu
->priv
;
270 bool old_vote
= (qca
->tx_vote
| qca
->rx_vote
);
274 case HCI_IBS_VOTE_STATS_UPDATE
:
275 diff
= jiffies_to_msecs(jiffies
- qca
->vote_last_jif
);
278 qca
->vote_off_ms
+= diff
;
280 qca
->vote_on_ms
+= diff
;
283 case HCI_IBS_TX_VOTE_CLOCK_ON
:
289 case HCI_IBS_RX_VOTE_CLOCK_ON
:
295 case HCI_IBS_TX_VOTE_CLOCK_OFF
:
296 qca
->tx_vote
= false;
298 new_vote
= qca
->rx_vote
| qca
->tx_vote
;
301 case HCI_IBS_RX_VOTE_CLOCK_OFF
:
302 qca
->rx_vote
= false;
304 new_vote
= qca
->rx_vote
| qca
->tx_vote
;
308 BT_ERR("Voting irregularity");
312 if (new_vote
!= old_vote
) {
314 __serial_clock_on(hu
->tty
);
316 __serial_clock_off(hu
->tty
);
318 BT_DBG("Vote serial clock %s(%s)", new_vote
? "true" : "false",
319 vote
? "true" : "false");
321 diff
= jiffies_to_msecs(jiffies
- qca
->vote_last_jif
);
325 qca
->vote_off_ms
+= diff
;
328 qca
->vote_on_ms
+= diff
;
330 qca
->vote_last_jif
= jiffies
;
334 /* Builds and sends an HCI_IBS command packet.
335 * These are very simple packets with only 1 cmd byte.
337 static int send_hci_ibs_cmd(u8 cmd
, struct hci_uart
*hu
)
340 struct sk_buff
*skb
= NULL
;
341 struct qca_data
*qca
= hu
->priv
;
343 BT_DBG("hu %p send hci ibs cmd 0x%x", hu
, cmd
);
345 skb
= bt_skb_alloc(1, GFP_ATOMIC
);
347 BT_ERR("Failed to allocate memory for HCI_IBS packet");
351 /* Assign HCI_IBS type */
352 skb_put_u8(skb
, cmd
);
354 skb_queue_tail(&qca
->txq
, skb
);
359 static void qca_wq_awake_device(struct work_struct
*work
)
361 struct qca_data
*qca
= container_of(work
, struct qca_data
,
363 struct hci_uart
*hu
= qca
->hu
;
364 unsigned long retrans_delay
;
367 BT_DBG("hu %p wq awake device", hu
);
369 /* Vote for serial clock */
370 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON
, hu
);
372 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
374 /* Send wake indication to device */
375 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND
, hu
) < 0)
376 BT_ERR("Failed to send WAKE to device");
378 qca
->ibs_sent_wakes
++;
380 /* Start retransmit timer */
381 retrans_delay
= msecs_to_jiffies(qca
->wake_retrans
);
382 mod_timer(&qca
->wake_retrans_timer
, jiffies
+ retrans_delay
);
384 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
386 /* Actually send the packets */
387 hci_uart_tx_wakeup(hu
);
390 static void qca_wq_awake_rx(struct work_struct
*work
)
392 struct qca_data
*qca
= container_of(work
, struct qca_data
,
394 struct hci_uart
*hu
= qca
->hu
;
397 BT_DBG("hu %p wq awake rx", hu
);
399 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON
, hu
);
401 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
402 qca
->rx_ibs_state
= HCI_IBS_RX_AWAKE
;
404 /* Always acknowledge device wake up,
405 * sending IBS message doesn't count as TX ON.
407 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK
, hu
) < 0)
408 BT_ERR("Failed to acknowledge device wake up");
410 qca
->ibs_sent_wacks
++;
412 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
414 /* Actually send the packets */
415 hci_uart_tx_wakeup(hu
);
418 static void qca_wq_serial_rx_clock_vote_off(struct work_struct
*work
)
420 struct qca_data
*qca
= container_of(work
, struct qca_data
,
422 struct hci_uart
*hu
= qca
->hu
;
424 BT_DBG("hu %p rx clock vote off", hu
);
426 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF
, hu
);
429 static void qca_wq_serial_tx_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 tx clock vote off", hu
);
437 /* Run HCI tx handling unlocked */
438 hci_uart_tx_wakeup(hu
);
440 /* Now that message queued to tty driver, vote for tty clocks off.
441 * It is up to the tty driver to pend the clocks off until tx done.
443 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF
, hu
);
446 static void hci_ibs_tx_idle_timeout(struct timer_list
*t
)
448 struct qca_data
*qca
= from_timer(qca
, t
, tx_idle_timer
);
449 struct hci_uart
*hu
= qca
->hu
;
452 BT_DBG("hu %p idle timeout in %d state", hu
, qca
->tx_ibs_state
);
454 spin_lock_irqsave_nested(&qca
->hci_ibs_lock
,
455 flags
, SINGLE_DEPTH_NESTING
);
457 switch (qca
->tx_ibs_state
) {
458 case HCI_IBS_TX_AWAKE
:
459 /* TX_IDLE, go to SLEEP */
460 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND
, hu
) < 0) {
461 BT_ERR("Failed to send SLEEP to device");
464 qca
->tx_ibs_state
= HCI_IBS_TX_ASLEEP
;
465 qca
->ibs_sent_slps
++;
466 queue_work(qca
->workqueue
, &qca
->ws_tx_vote_off
);
469 case HCI_IBS_TX_ASLEEP
:
470 case HCI_IBS_TX_WAKING
:
474 BT_ERR("Spurious timeout tx state %d", qca
->tx_ibs_state
);
478 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
481 static void hci_ibs_wake_retrans_timeout(struct timer_list
*t
)
483 struct qca_data
*qca
= from_timer(qca
, t
, wake_retrans_timer
);
484 struct hci_uart
*hu
= qca
->hu
;
485 unsigned long flags
, retrans_delay
;
486 bool retransmit
= false;
488 BT_DBG("hu %p wake retransmit timeout in %d state",
489 hu
, qca
->tx_ibs_state
);
491 spin_lock_irqsave_nested(&qca
->hci_ibs_lock
,
492 flags
, SINGLE_DEPTH_NESTING
);
494 /* Don't retransmit the HCI_IBS_WAKE_IND when suspending. */
495 if (test_bit(QCA_SUSPENDING
, &qca
->flags
)) {
496 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
500 switch (qca
->tx_ibs_state
) {
501 case HCI_IBS_TX_WAKING
:
502 /* No WAKE_ACK, retransmit WAKE */
504 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND
, hu
) < 0) {
505 BT_ERR("Failed to acknowledge device wake up");
508 qca
->ibs_sent_wakes
++;
509 retrans_delay
= msecs_to_jiffies(qca
->wake_retrans
);
510 mod_timer(&qca
->wake_retrans_timer
, jiffies
+ retrans_delay
);
513 case HCI_IBS_TX_ASLEEP
:
514 case HCI_IBS_TX_AWAKE
:
518 BT_ERR("Spurious timeout tx state %d", qca
->tx_ibs_state
);
522 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
525 hci_uart_tx_wakeup(hu
);
529 static void qca_controller_memdump_timeout(struct work_struct
*work
)
531 struct qca_data
*qca
= container_of(work
, struct qca_data
,
532 ctrl_memdump_timeout
.work
);
533 struct hci_uart
*hu
= qca
->hu
;
535 mutex_lock(&qca
->hci_memdump_lock
);
536 if (test_bit(QCA_MEMDUMP_COLLECTION
, &qca
->flags
)) {
537 qca
->memdump_state
= QCA_MEMDUMP_TIMEOUT
;
538 if (!test_bit(QCA_HW_ERROR_EVENT
, &qca
->flags
)) {
539 /* Inject hw error event to reset the device
542 hci_reset_dev(hu
->hdev
);
546 mutex_unlock(&qca
->hci_memdump_lock
);
550 /* Initialize protocol */
551 static int qca_open(struct hci_uart
*hu
)
553 struct qca_serdev
*qcadev
;
554 struct qca_data
*qca
;
556 BT_DBG("hu %p qca_open", hu
);
558 if (!hci_uart_has_flow_control(hu
))
561 qca
= kzalloc(sizeof(struct qca_data
), GFP_KERNEL
);
565 skb_queue_head_init(&qca
->txq
);
566 skb_queue_head_init(&qca
->tx_wait_q
);
567 skb_queue_head_init(&qca
->rx_memdump_q
);
568 spin_lock_init(&qca
->hci_ibs_lock
);
569 mutex_init(&qca
->hci_memdump_lock
);
570 qca
->workqueue
= alloc_ordered_workqueue("qca_wq", 0);
571 if (!qca
->workqueue
) {
572 BT_ERR("QCA Workqueue not initialized properly");
577 INIT_WORK(&qca
->ws_awake_rx
, qca_wq_awake_rx
);
578 INIT_WORK(&qca
->ws_awake_device
, qca_wq_awake_device
);
579 INIT_WORK(&qca
->ws_rx_vote_off
, qca_wq_serial_rx_clock_vote_off
);
580 INIT_WORK(&qca
->ws_tx_vote_off
, qca_wq_serial_tx_clock_vote_off
);
581 INIT_WORK(&qca
->ctrl_memdump_evt
, qca_controller_memdump
);
582 INIT_DELAYED_WORK(&qca
->ctrl_memdump_timeout
,
583 qca_controller_memdump_timeout
);
584 init_waitqueue_head(&qca
->suspend_wait_q
);
587 init_completion(&qca
->drop_ev_comp
);
589 /* Assume we start with both sides asleep -- extra wakes OK */
590 qca
->tx_ibs_state
= HCI_IBS_TX_ASLEEP
;
591 qca
->rx_ibs_state
= HCI_IBS_RX_ASLEEP
;
593 qca
->vote_last_jif
= jiffies
;
598 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
599 if (qca_is_wcn399x(qcadev
->btsoc_type
)) {
600 hu
->init_speed
= qcadev
->init_speed
;
601 hu
->oper_speed
= qcadev
->oper_speed
;
605 timer_setup(&qca
->wake_retrans_timer
, hci_ibs_wake_retrans_timeout
, 0);
606 qca
->wake_retrans
= IBS_WAKE_RETRANS_TIMEOUT_MS
;
608 timer_setup(&qca
->tx_idle_timer
, hci_ibs_tx_idle_timeout
, 0);
609 qca
->tx_idle_delay
= IBS_HOST_TX_IDLE_TIMEOUT_MS
;
611 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
612 qca
->tx_idle_delay
, qca
->wake_retrans
);
617 static void qca_debugfs_init(struct hci_dev
*hdev
)
619 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
620 struct qca_data
*qca
= hu
->priv
;
621 struct dentry
*ibs_dir
;
627 ibs_dir
= debugfs_create_dir("ibs", hdev
->debugfs
);
631 debugfs_create_u8("tx_ibs_state", mode
, ibs_dir
, &qca
->tx_ibs_state
);
632 debugfs_create_u8("rx_ibs_state", mode
, ibs_dir
, &qca
->rx_ibs_state
);
633 debugfs_create_u64("ibs_sent_sleeps", mode
, ibs_dir
,
634 &qca
->ibs_sent_slps
);
635 debugfs_create_u64("ibs_sent_wakes", mode
, ibs_dir
,
636 &qca
->ibs_sent_wakes
);
637 debugfs_create_u64("ibs_sent_wake_acks", mode
, ibs_dir
,
638 &qca
->ibs_sent_wacks
);
639 debugfs_create_u64("ibs_recv_sleeps", mode
, ibs_dir
,
640 &qca
->ibs_recv_slps
);
641 debugfs_create_u64("ibs_recv_wakes", mode
, ibs_dir
,
642 &qca
->ibs_recv_wakes
);
643 debugfs_create_u64("ibs_recv_wake_acks", mode
, ibs_dir
,
644 &qca
->ibs_recv_wacks
);
645 debugfs_create_bool("tx_vote", mode
, ibs_dir
, &qca
->tx_vote
);
646 debugfs_create_u64("tx_votes_on", mode
, ibs_dir
, &qca
->tx_votes_on
);
647 debugfs_create_u64("tx_votes_off", mode
, ibs_dir
, &qca
->tx_votes_off
);
648 debugfs_create_bool("rx_vote", mode
, ibs_dir
, &qca
->rx_vote
);
649 debugfs_create_u64("rx_votes_on", mode
, ibs_dir
, &qca
->rx_votes_on
);
650 debugfs_create_u64("rx_votes_off", mode
, ibs_dir
, &qca
->rx_votes_off
);
651 debugfs_create_u64("votes_on", mode
, ibs_dir
, &qca
->votes_on
);
652 debugfs_create_u64("votes_off", mode
, ibs_dir
, &qca
->votes_off
);
653 debugfs_create_u32("vote_on_ms", mode
, ibs_dir
, &qca
->vote_on_ms
);
654 debugfs_create_u32("vote_off_ms", mode
, ibs_dir
, &qca
->vote_off_ms
);
657 mode
= S_IRUGO
| S_IWUSR
;
658 debugfs_create_u32("wake_retrans", mode
, ibs_dir
, &qca
->wake_retrans
);
659 debugfs_create_u32("tx_idle_delay", mode
, ibs_dir
,
660 &qca
->tx_idle_delay
);
663 /* Flush protocol data */
664 static int qca_flush(struct hci_uart
*hu
)
666 struct qca_data
*qca
= hu
->priv
;
668 BT_DBG("hu %p qca flush", hu
);
670 skb_queue_purge(&qca
->tx_wait_q
);
671 skb_queue_purge(&qca
->txq
);
677 static int qca_close(struct hci_uart
*hu
)
679 struct qca_data
*qca
= hu
->priv
;
681 BT_DBG("hu %p qca close", hu
);
683 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE
, hu
);
685 skb_queue_purge(&qca
->tx_wait_q
);
686 skb_queue_purge(&qca
->txq
);
687 skb_queue_purge(&qca
->rx_memdump_q
);
688 del_timer(&qca
->tx_idle_timer
);
689 del_timer(&qca
->wake_retrans_timer
);
690 destroy_workqueue(qca
->workqueue
);
693 qca_power_shutdown(hu
);
695 kfree_skb(qca
->rx_skb
);
704 /* Called upon a wake-up-indication from the device.
706 static void device_want_to_wakeup(struct hci_uart
*hu
)
709 struct qca_data
*qca
= hu
->priv
;
711 BT_DBG("hu %p want to wake up", hu
);
713 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
715 qca
->ibs_recv_wakes
++;
717 /* Don't wake the rx up when suspending. */
718 if (test_bit(QCA_SUSPENDING
, &qca
->flags
)) {
719 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
723 switch (qca
->rx_ibs_state
) {
724 case HCI_IBS_RX_ASLEEP
:
725 /* Make sure clock is on - we may have turned clock off since
726 * receiving the wake up indicator awake rx clock.
728 queue_work(qca
->workqueue
, &qca
->ws_awake_rx
);
729 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
732 case HCI_IBS_RX_AWAKE
:
733 /* Always acknowledge device wake up,
734 * sending IBS message doesn't count as TX ON.
736 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK
, hu
) < 0) {
737 BT_ERR("Failed to acknowledge device wake up");
740 qca
->ibs_sent_wacks
++;
744 /* Any other state is illegal */
745 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
750 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
752 /* Actually send the packets */
753 hci_uart_tx_wakeup(hu
);
756 /* Called upon a sleep-indication from the device.
758 static void device_want_to_sleep(struct hci_uart
*hu
)
761 struct qca_data
*qca
= hu
->priv
;
763 BT_DBG("hu %p want to sleep in %d state", hu
, qca
->rx_ibs_state
);
765 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
767 qca
->ibs_recv_slps
++;
769 switch (qca
->rx_ibs_state
) {
770 case HCI_IBS_RX_AWAKE
:
772 qca
->rx_ibs_state
= HCI_IBS_RX_ASLEEP
;
773 /* Vote off rx clock under workqueue */
774 queue_work(qca
->workqueue
, &qca
->ws_rx_vote_off
);
777 case HCI_IBS_RX_ASLEEP
:
781 /* Any other state is illegal */
782 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
787 wake_up_interruptible(&qca
->suspend_wait_q
);
789 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
792 /* Called upon wake-up-acknowledgement from the device
794 static void device_woke_up(struct hci_uart
*hu
)
796 unsigned long flags
, idle_delay
;
797 struct qca_data
*qca
= hu
->priv
;
798 struct sk_buff
*skb
= NULL
;
800 BT_DBG("hu %p woke up", hu
);
802 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
804 qca
->ibs_recv_wacks
++;
806 /* Don't react to the wake-up-acknowledgment when suspending. */
807 if (test_bit(QCA_SUSPENDING
, &qca
->flags
)) {
808 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
812 switch (qca
->tx_ibs_state
) {
813 case HCI_IBS_TX_AWAKE
:
814 /* Expect one if we send 2 WAKEs */
815 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
819 case HCI_IBS_TX_WAKING
:
820 /* Send pending packets */
821 while ((skb
= skb_dequeue(&qca
->tx_wait_q
)))
822 skb_queue_tail(&qca
->txq
, skb
);
824 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
825 del_timer(&qca
->wake_retrans_timer
);
826 idle_delay
= msecs_to_jiffies(qca
->tx_idle_delay
);
827 mod_timer(&qca
->tx_idle_timer
, jiffies
+ idle_delay
);
828 qca
->tx_ibs_state
= HCI_IBS_TX_AWAKE
;
831 case HCI_IBS_TX_ASLEEP
:
835 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
840 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
842 /* Actually send the packets */
843 hci_uart_tx_wakeup(hu
);
846 /* Enqueue frame for transmittion (padding, crc, etc) may be called from
847 * two simultaneous tasklets.
849 static int qca_enqueue(struct hci_uart
*hu
, struct sk_buff
*skb
)
851 unsigned long flags
= 0, idle_delay
;
852 struct qca_data
*qca
= hu
->priv
;
854 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu
, skb
,
857 /* Prepend skb with frame type */
858 memcpy(skb_push(skb
, 1), &hci_skb_pkt_type(skb
), 1);
860 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
862 /* Don't go to sleep in middle of patch download or
863 * Out-Of-Band(GPIOs control) sleep is selected.
864 * Don't wake the device up when suspending.
866 if (!test_bit(QCA_IBS_ENABLED
, &qca
->flags
) ||
867 test_bit(QCA_SUSPENDING
, &qca
->flags
)) {
868 skb_queue_tail(&qca
->txq
, skb
);
869 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
873 /* Act according to current state */
874 switch (qca
->tx_ibs_state
) {
875 case HCI_IBS_TX_AWAKE
:
876 BT_DBG("Device awake, sending normally");
877 skb_queue_tail(&qca
->txq
, skb
);
878 idle_delay
= msecs_to_jiffies(qca
->tx_idle_delay
);
879 mod_timer(&qca
->tx_idle_timer
, jiffies
+ idle_delay
);
882 case HCI_IBS_TX_ASLEEP
:
883 BT_DBG("Device asleep, waking up and queueing packet");
884 /* Save packet for later */
885 skb_queue_tail(&qca
->tx_wait_q
, skb
);
887 qca
->tx_ibs_state
= HCI_IBS_TX_WAKING
;
888 /* Schedule a work queue to wake up device */
889 queue_work(qca
->workqueue
, &qca
->ws_awake_device
);
892 case HCI_IBS_TX_WAKING
:
893 BT_DBG("Device waking up, queueing packet");
894 /* Transient state; just keep packet for later */
895 skb_queue_tail(&qca
->tx_wait_q
, skb
);
899 BT_ERR("Illegal tx state: %d (losing packet)",
905 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
910 static int qca_ibs_sleep_ind(struct hci_dev
*hdev
, struct sk_buff
*skb
)
912 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
914 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu
, HCI_IBS_SLEEP_IND
);
916 device_want_to_sleep(hu
);
922 static int qca_ibs_wake_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_WAKE_IND
);
928 device_want_to_wakeup(hu
);
934 static int qca_ibs_wake_ack(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_ACK
);
946 static int qca_recv_acl_data(struct hci_dev
*hdev
, struct sk_buff
*skb
)
948 /* We receive debug logs from chip as an ACL packets.
949 * Instead of sending the data to ACL to decode the
950 * received data, we are pushing them to the above layers
951 * as a diagnostic packet.
953 if (get_unaligned_le16(skb
->data
) == QCA_DEBUG_HANDLE
)
954 return hci_recv_diag(hdev
, skb
);
956 return hci_recv_frame(hdev
, skb
);
959 static void qca_controller_memdump(struct work_struct
*work
)
961 struct qca_data
*qca
= container_of(work
, struct qca_data
,
963 struct hci_uart
*hu
= qca
->hu
;
965 struct qca_memdump_event_hdr
*cmd_hdr
;
966 struct qca_memdump_data
*qca_memdump
= qca
->qca_memdump
;
967 struct qca_dump_size
*dump
;
969 char nullBuff
[QCA_DUMP_PACKET_SIZE
] = { 0 };
973 while ((skb
= skb_dequeue(&qca
->rx_memdump_q
))) {
975 mutex_lock(&qca
->hci_memdump_lock
);
976 /* Skip processing the received packets if timeout detected. */
977 if (qca
->memdump_state
== QCA_MEMDUMP_TIMEOUT
) {
978 mutex_unlock(&qca
->hci_memdump_lock
);
983 qca_memdump
= kzalloc(sizeof(struct qca_memdump_data
),
986 mutex_unlock(&qca
->hci_memdump_lock
);
990 qca
->qca_memdump
= qca_memdump
;
993 qca
->memdump_state
= QCA_MEMDUMP_COLLECTING
;
994 cmd_hdr
= (void *) skb
->data
;
995 seq_no
= __le16_to_cpu(cmd_hdr
->seq_no
);
996 skb_pull(skb
, sizeof(struct qca_memdump_event_hdr
));
1000 /* This is the first frame of memdump packet from
1001 * the controller, Disable IBS to recevie dump
1002 * with out any interruption, ideally time required for
1003 * the controller to send the dump is 8 seconds. let us
1004 * start timer to handle this asynchronous activity.
1006 clear_bit(QCA_IBS_ENABLED
, &qca
->flags
);
1007 set_bit(QCA_MEMDUMP_COLLECTION
, &qca
->flags
);
1008 dump
= (void *) skb
->data
;
1009 dump_size
= __le32_to_cpu(dump
->dump_size
);
1011 bt_dev_err(hu
->hdev
, "Rx invalid memdump size");
1013 mutex_unlock(&qca
->hci_memdump_lock
);
1017 bt_dev_info(hu
->hdev
, "QCA collecting dump of size:%u",
1019 queue_delayed_work(qca
->workqueue
,
1020 &qca
->ctrl_memdump_timeout
,
1021 msecs_to_jiffies(MEMDUMP_TIMEOUT_MS
));
1023 skb_pull(skb
, sizeof(dump_size
));
1024 memdump_buf
= vmalloc(dump_size
);
1025 qca_memdump
->memdump_buf_head
= memdump_buf
;
1026 qca_memdump
->memdump_buf_tail
= memdump_buf
;
1029 memdump_buf
= qca_memdump
->memdump_buf_tail
;
1031 /* If sequence no 0 is missed then there is no point in
1032 * accepting the other sequences.
1035 bt_dev_err(hu
->hdev
, "QCA: Discarding other packets");
1038 qca
->qca_memdump
= NULL
;
1039 mutex_unlock(&qca
->hci_memdump_lock
);
1043 /* There could be chance of missing some packets from
1044 * the controller. In such cases let us store the dummy
1045 * packets in the buffer.
1047 while ((seq_no
> qca_memdump
->current_seq_no
+ 1) &&
1048 seq_no
!= QCA_LAST_SEQUENCE_NUM
) {
1049 bt_dev_err(hu
->hdev
, "QCA controller missed packet:%d",
1050 qca_memdump
->current_seq_no
);
1051 memcpy(memdump_buf
, nullBuff
, QCA_DUMP_PACKET_SIZE
);
1052 memdump_buf
= memdump_buf
+ QCA_DUMP_PACKET_SIZE
;
1053 qca_memdump
->received_dump
+= QCA_DUMP_PACKET_SIZE
;
1054 qca_memdump
->current_seq_no
++;
1057 memcpy(memdump_buf
, (unsigned char *) skb
->data
, skb
->len
);
1058 memdump_buf
= memdump_buf
+ skb
->len
;
1059 qca_memdump
->memdump_buf_tail
= memdump_buf
;
1060 qca_memdump
->current_seq_no
= seq_no
+ 1;
1061 qca_memdump
->received_dump
+= skb
->len
;
1062 qca
->qca_memdump
= qca_memdump
;
1064 if (seq_no
== QCA_LAST_SEQUENCE_NUM
) {
1065 bt_dev_info(hu
->hdev
, "QCA writing crash dump of size %d bytes",
1066 qca_memdump
->received_dump
);
1067 memdump_buf
= qca_memdump
->memdump_buf_head
;
1068 dev_coredumpv(&hu
->serdev
->dev
, memdump_buf
,
1069 qca_memdump
->received_dump
, GFP_KERNEL
);
1070 cancel_delayed_work(&qca
->ctrl_memdump_timeout
);
1071 kfree(qca
->qca_memdump
);
1072 qca
->qca_memdump
= NULL
;
1073 qca
->memdump_state
= QCA_MEMDUMP_COLLECTED
;
1074 clear_bit(QCA_MEMDUMP_COLLECTION
, &qca
->flags
);
1077 mutex_unlock(&qca
->hci_memdump_lock
);
1082 static int qca_controller_memdump_event(struct hci_dev
*hdev
,
1083 struct sk_buff
*skb
)
1085 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
1086 struct qca_data
*qca
= hu
->priv
;
1088 skb_queue_tail(&qca
->rx_memdump_q
, skb
);
1089 queue_work(qca
->workqueue
, &qca
->ctrl_memdump_evt
);
1094 static int qca_recv_event(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1096 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
1097 struct qca_data
*qca
= hu
->priv
;
1099 if (test_bit(QCA_DROP_VENDOR_EVENT
, &qca
->flags
)) {
1100 struct hci_event_hdr
*hdr
= (void *)skb
->data
;
1102 /* For the WCN3990 the vendor command for a baudrate change
1103 * isn't sent as synchronous HCI command, because the
1104 * controller sends the corresponding vendor event with the
1105 * new baudrate. The event is received and properly decoded
1106 * after changing the baudrate of the host port. It needs to
1107 * be dropped, otherwise it can be misinterpreted as
1108 * response to a later firmware download command (also a
1112 if (hdr
->evt
== HCI_EV_VENDOR
)
1113 complete(&qca
->drop_ev_comp
);
1119 /* We receive chip memory dump as an event packet, With a dedicated
1120 * handler followed by a hardware error event. When this event is
1121 * received we store dump into a file before closing hci. This
1122 * dump will help in triaging the issues.
1124 if ((skb
->data
[0] == HCI_VENDOR_PKT
) &&
1125 (get_unaligned_be16(skb
->data
+ 2) == QCA_SSR_DUMP_HANDLE
))
1126 return qca_controller_memdump_event(hdev
, skb
);
1128 return hci_recv_frame(hdev
, skb
);
1131 #define QCA_IBS_SLEEP_IND_EVENT \
1132 .type = HCI_IBS_SLEEP_IND, \
1136 .maxlen = HCI_MAX_IBS_SIZE
1138 #define QCA_IBS_WAKE_IND_EVENT \
1139 .type = HCI_IBS_WAKE_IND, \
1143 .maxlen = HCI_MAX_IBS_SIZE
1145 #define QCA_IBS_WAKE_ACK_EVENT \
1146 .type = HCI_IBS_WAKE_ACK, \
1150 .maxlen = HCI_MAX_IBS_SIZE
1152 static const struct h4_recv_pkt qca_recv_pkts
[] = {
1153 { H4_RECV_ACL
, .recv
= qca_recv_acl_data
},
1154 { H4_RECV_SCO
, .recv
= hci_recv_frame
},
1155 { H4_RECV_EVENT
, .recv
= qca_recv_event
},
1156 { QCA_IBS_WAKE_IND_EVENT
, .recv
= qca_ibs_wake_ind
},
1157 { QCA_IBS_WAKE_ACK_EVENT
, .recv
= qca_ibs_wake_ack
},
1158 { QCA_IBS_SLEEP_IND_EVENT
, .recv
= qca_ibs_sleep_ind
},
1161 static int qca_recv(struct hci_uart
*hu
, const void *data
, int count
)
1163 struct qca_data
*qca
= hu
->priv
;
1165 if (!test_bit(HCI_UART_REGISTERED
, &hu
->flags
))
1168 qca
->rx_skb
= h4_recv_buf(hu
->hdev
, qca
->rx_skb
, data
, count
,
1169 qca_recv_pkts
, ARRAY_SIZE(qca_recv_pkts
));
1170 if (IS_ERR(qca
->rx_skb
)) {
1171 int err
= PTR_ERR(qca
->rx_skb
);
1172 bt_dev_err(hu
->hdev
, "Frame reassembly failed (%d)", err
);
1180 static struct sk_buff
*qca_dequeue(struct hci_uart
*hu
)
1182 struct qca_data
*qca
= hu
->priv
;
1184 return skb_dequeue(&qca
->txq
);
1187 static uint8_t qca_get_baudrate_value(int speed
)
1191 return QCA_BAUDRATE_9600
;
1193 return QCA_BAUDRATE_19200
;
1195 return QCA_BAUDRATE_38400
;
1197 return QCA_BAUDRATE_57600
;
1199 return QCA_BAUDRATE_115200
;
1201 return QCA_BAUDRATE_230400
;
1203 return QCA_BAUDRATE_460800
;
1205 return QCA_BAUDRATE_500000
;
1207 return QCA_BAUDRATE_921600
;
1209 return QCA_BAUDRATE_1000000
;
1211 return QCA_BAUDRATE_2000000
;
1213 return QCA_BAUDRATE_3000000
;
1215 return QCA_BAUDRATE_3200000
;
1217 return QCA_BAUDRATE_3500000
;
1219 return QCA_BAUDRATE_115200
;
1223 static int qca_set_baudrate(struct hci_dev
*hdev
, uint8_t baudrate
)
1225 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
1226 struct qca_data
*qca
= hu
->priv
;
1227 struct sk_buff
*skb
;
1228 u8 cmd
[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
1230 if (baudrate
> QCA_BAUDRATE_3200000
)
1235 skb
= bt_skb_alloc(sizeof(cmd
), GFP_KERNEL
);
1237 bt_dev_err(hdev
, "Failed to allocate baudrate packet");
1241 /* Assign commands to change baudrate and packet type. */
1242 skb_put_data(skb
, cmd
, sizeof(cmd
));
1243 hci_skb_pkt_type(skb
) = HCI_COMMAND_PKT
;
1245 skb_queue_tail(&qca
->txq
, skb
);
1246 hci_uart_tx_wakeup(hu
);
1248 /* Wait for the baudrate change request to be sent */
1250 while (!skb_queue_empty(&qca
->txq
))
1251 usleep_range(100, 200);
1254 serdev_device_wait_until_sent(hu
->serdev
,
1255 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS
));
1257 /* Give the controller time to process the request */
1258 if (qca_is_wcn399x(qca_soc_type(hu
)))
1266 static inline void host_set_baudrate(struct hci_uart
*hu
, unsigned int speed
)
1269 serdev_device_set_baudrate(hu
->serdev
, speed
);
1271 hci_uart_set_baudrate(hu
, speed
);
1274 static int qca_send_power_pulse(struct hci_uart
*hu
, bool on
)
1277 int timeout
= msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS
);
1278 u8 cmd
= on
? QCA_WCN3990_POWERON_PULSE
: QCA_WCN3990_POWEROFF_PULSE
;
1280 /* These power pulses are single byte command which are sent
1281 * at required baudrate to wcn3990. On wcn3990, we have an external
1282 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1283 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1284 * and also we use the same power inputs to turn on and off for
1285 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1286 * we send a power on pulse at 115200 bps. This algorithm will help to
1287 * save power. Disabling hardware flow control is mandatory while
1288 * sending power pulses to SoC.
1290 bt_dev_dbg(hu
->hdev
, "sending power pulse %02x to controller", cmd
);
1292 serdev_device_write_flush(hu
->serdev
);
1293 hci_uart_set_flow_control(hu
, true);
1294 ret
= serdev_device_write_buf(hu
->serdev
, &cmd
, sizeof(cmd
));
1296 bt_dev_err(hu
->hdev
, "failed to send power pulse %02x", cmd
);
1300 serdev_device_wait_until_sent(hu
->serdev
, timeout
);
1301 hci_uart_set_flow_control(hu
, false);
1303 /* Give to controller time to boot/shutdown */
1312 static unsigned int qca_get_speed(struct hci_uart
*hu
,
1313 enum qca_speed_type speed_type
)
1315 unsigned int speed
= 0;
1317 if (speed_type
== QCA_INIT_SPEED
) {
1319 speed
= hu
->init_speed
;
1320 else if (hu
->proto
->init_speed
)
1321 speed
= hu
->proto
->init_speed
;
1324 speed
= hu
->oper_speed
;
1325 else if (hu
->proto
->oper_speed
)
1326 speed
= hu
->proto
->oper_speed
;
1332 static int qca_check_speeds(struct hci_uart
*hu
)
1334 if (qca_is_wcn399x(qca_soc_type(hu
))) {
1335 if (!qca_get_speed(hu
, QCA_INIT_SPEED
) &&
1336 !qca_get_speed(hu
, QCA_OPER_SPEED
))
1339 if (!qca_get_speed(hu
, QCA_INIT_SPEED
) ||
1340 !qca_get_speed(hu
, QCA_OPER_SPEED
))
1347 static int qca_set_speed(struct hci_uart
*hu
, enum qca_speed_type speed_type
)
1349 unsigned int speed
, qca_baudrate
;
1350 struct qca_data
*qca
= hu
->priv
;
1353 if (speed_type
== QCA_INIT_SPEED
) {
1354 speed
= qca_get_speed(hu
, QCA_INIT_SPEED
);
1356 host_set_baudrate(hu
, speed
);
1358 enum qca_btsoc_type soc_type
= qca_soc_type(hu
);
1360 speed
= qca_get_speed(hu
, QCA_OPER_SPEED
);
1364 /* Disable flow control for wcn3990 to deassert RTS while
1365 * changing the baudrate of chip and host.
1367 if (qca_is_wcn399x(soc_type
))
1368 hci_uart_set_flow_control(hu
, true);
1370 if (soc_type
== QCA_WCN3990
) {
1371 reinit_completion(&qca
->drop_ev_comp
);
1372 set_bit(QCA_DROP_VENDOR_EVENT
, &qca
->flags
);
1375 qca_baudrate
= qca_get_baudrate_value(speed
);
1376 bt_dev_dbg(hu
->hdev
, "Set UART speed to %d", speed
);
1377 ret
= qca_set_baudrate(hu
->hdev
, qca_baudrate
);
1381 host_set_baudrate(hu
, speed
);
1384 if (qca_is_wcn399x(soc_type
))
1385 hci_uart_set_flow_control(hu
, false);
1387 if (soc_type
== QCA_WCN3990
) {
1388 /* Wait for the controller to send the vendor event
1389 * for the baudrate change command.
1391 if (!wait_for_completion_timeout(&qca
->drop_ev_comp
,
1392 msecs_to_jiffies(100))) {
1393 bt_dev_err(hu
->hdev
,
1394 "Failed to change controller baudrate\n");
1398 clear_bit(QCA_DROP_VENDOR_EVENT
, &qca
->flags
);
1405 static int qca_send_crashbuffer(struct hci_uart
*hu
)
1407 struct qca_data
*qca
= hu
->priv
;
1408 struct sk_buff
*skb
;
1410 skb
= bt_skb_alloc(QCA_CRASHBYTE_PACKET_LEN
, GFP_KERNEL
);
1412 bt_dev_err(hu
->hdev
, "Failed to allocate memory for skb packet");
1416 /* We forcefully crash the controller, by sending 0xfb byte for
1417 * 1024 times. We also might have chance of losing data, To be
1418 * on safer side we send 1096 bytes to the SoC.
1420 memset(skb_put(skb
, QCA_CRASHBYTE_PACKET_LEN
), QCA_MEMDUMP_BYTE
,
1421 QCA_CRASHBYTE_PACKET_LEN
);
1422 hci_skb_pkt_type(skb
) = HCI_COMMAND_PKT
;
1423 bt_dev_info(hu
->hdev
, "crash the soc to collect controller dump");
1424 skb_queue_tail(&qca
->txq
, skb
);
1425 hci_uart_tx_wakeup(hu
);
1430 static void qca_wait_for_dump_collection(struct hci_dev
*hdev
)
1432 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
1433 struct qca_data
*qca
= hu
->priv
;
1435 wait_on_bit_timeout(&qca
->flags
, QCA_MEMDUMP_COLLECTION
,
1436 TASK_UNINTERRUPTIBLE
, MEMDUMP_TIMEOUT_MS
);
1438 clear_bit(QCA_MEMDUMP_COLLECTION
, &qca
->flags
);
1441 static void qca_hw_error(struct hci_dev
*hdev
, u8 code
)
1443 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
1444 struct qca_data
*qca
= hu
->priv
;
1445 struct qca_memdump_data
*qca_memdump
= qca
->qca_memdump
;
1446 char *memdump_buf
= NULL
;
1448 set_bit(QCA_HW_ERROR_EVENT
, &qca
->flags
);
1449 bt_dev_info(hdev
, "mem_dump_status: %d", qca
->memdump_state
);
1451 if (qca
->memdump_state
== QCA_MEMDUMP_IDLE
) {
1452 /* If hardware error event received for other than QCA
1453 * soc memory dump event, then we need to crash the SOC
1454 * and wait here for 8 seconds to get the dump packets.
1455 * This will block main thread to be on hold until we
1458 set_bit(QCA_MEMDUMP_COLLECTION
, &qca
->flags
);
1459 qca_send_crashbuffer(hu
);
1460 qca_wait_for_dump_collection(hdev
);
1461 } else if (qca
->memdump_state
== QCA_MEMDUMP_COLLECTING
) {
1462 /* Let us wait here until memory dump collected or
1463 * memory dump timer expired.
1465 bt_dev_info(hdev
, "waiting for dump to complete");
1466 qca_wait_for_dump_collection(hdev
);
1469 if (qca
->memdump_state
!= QCA_MEMDUMP_COLLECTED
) {
1470 bt_dev_err(hu
->hdev
, "clearing allocated memory due to memdump timeout");
1471 mutex_lock(&qca
->hci_memdump_lock
);
1473 memdump_buf
= qca_memdump
->memdump_buf_head
;
1476 qca
->qca_memdump
= NULL
;
1477 qca
->memdump_state
= QCA_MEMDUMP_TIMEOUT
;
1478 cancel_delayed_work(&qca
->ctrl_memdump_timeout
);
1479 skb_queue_purge(&qca
->rx_memdump_q
);
1480 mutex_unlock(&qca
->hci_memdump_lock
);
1481 cancel_work_sync(&qca
->ctrl_memdump_evt
);
1484 clear_bit(QCA_HW_ERROR_EVENT
, &qca
->flags
);
1487 static void qca_cmd_timeout(struct hci_dev
*hdev
)
1489 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
1490 struct qca_data
*qca
= hu
->priv
;
1492 if (qca
->memdump_state
== QCA_MEMDUMP_IDLE
)
1493 qca_send_crashbuffer(hu
);
1495 bt_dev_info(hdev
, "Dump collection is in process");
1498 static int qca_wcn3990_init(struct hci_uart
*hu
)
1500 struct qca_serdev
*qcadev
;
1503 /* Check for vregs status, may be hci down has turned
1504 * off the voltage regulator.
1506 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
1507 if (!qcadev
->bt_power
->vregs_on
) {
1508 serdev_device_close(hu
->serdev
);
1509 ret
= qca_regulator_enable(qcadev
);
1513 ret
= serdev_device_open(hu
->serdev
);
1515 bt_dev_err(hu
->hdev
, "failed to open port");
1520 /* Forcefully enable wcn3990 to enter in to boot mode. */
1521 host_set_baudrate(hu
, 2400);
1522 ret
= qca_send_power_pulse(hu
, false);
1526 qca_set_speed(hu
, QCA_INIT_SPEED
);
1527 ret
= qca_send_power_pulse(hu
, true);
1531 /* Now the device is in ready state to communicate with host.
1532 * To sync host with device we need to reopen port.
1533 * Without this, we will have RTS and CTS synchronization
1536 serdev_device_close(hu
->serdev
);
1537 ret
= serdev_device_open(hu
->serdev
);
1539 bt_dev_err(hu
->hdev
, "failed to open port");
1543 hci_uart_set_flow_control(hu
, false);
1548 static int qca_power_on(struct hci_dev
*hdev
)
1550 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
1551 enum qca_btsoc_type soc_type
= qca_soc_type(hu
);
1552 struct qca_serdev
*qcadev
;
1555 /* Non-serdev device usually is powered by external power
1556 * and don't need additional action in driver for power on
1561 if (qca_is_wcn399x(soc_type
)) {
1562 ret
= qca_wcn3990_init(hu
);
1564 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
1565 if (qcadev
->bt_en
) {
1566 gpiod_set_value_cansleep(qcadev
->bt_en
, 1);
1567 /* Controller needs time to bootup. */
1575 static int qca_setup(struct hci_uart
*hu
)
1577 struct hci_dev
*hdev
= hu
->hdev
;
1578 struct qca_data
*qca
= hu
->priv
;
1579 unsigned int speed
, qca_baudrate
= QCA_BAUDRATE_115200
;
1580 unsigned int retries
= 0;
1581 enum qca_btsoc_type soc_type
= qca_soc_type(hu
);
1582 const char *firmware_name
= qca_get_firmware_name(hu
);
1586 ret
= qca_check_speeds(hu
);
1590 /* Patch downloading has to be done without IBS mode */
1591 clear_bit(QCA_IBS_ENABLED
, &qca
->flags
);
1593 /* Enable controller to do both LE scan and BR/EDR inquiry
1596 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY
, &hdev
->quirks
);
1598 bt_dev_info(hdev
, "setting up %s",
1599 qca_is_wcn399x(soc_type
) ? "wcn399x" : "ROME");
1602 ret
= qca_power_on(hdev
);
1606 if (qca_is_wcn399x(soc_type
)) {
1607 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY
, &hdev
->quirks
);
1609 ret
= qca_read_soc_version(hdev
, &soc_ver
, soc_type
);
1613 qca_set_speed(hu
, QCA_INIT_SPEED
);
1616 /* Setup user speed if needed */
1617 speed
= qca_get_speed(hu
, QCA_OPER_SPEED
);
1619 ret
= qca_set_speed(hu
, QCA_OPER_SPEED
);
1623 qca_baudrate
= qca_get_baudrate_value(speed
);
1626 if (!qca_is_wcn399x(soc_type
)) {
1627 /* Get QCA version information */
1628 ret
= qca_read_soc_version(hdev
, &soc_ver
, soc_type
);
1633 bt_dev_info(hdev
, "QCA controller version 0x%08x", soc_ver
);
1634 /* Setup patch / NVM configurations */
1635 ret
= qca_uart_setup(hdev
, qca_baudrate
, soc_type
, soc_ver
,
1638 set_bit(QCA_IBS_ENABLED
, &qca
->flags
);
1639 qca_debugfs_init(hdev
);
1640 hu
->hdev
->hw_error
= qca_hw_error
;
1641 hu
->hdev
->cmd_timeout
= qca_cmd_timeout
;
1642 } else if (ret
== -ENOENT
) {
1643 /* No patch/nvm-config found, run with original fw/config */
1645 } else if (ret
== -EAGAIN
) {
1647 * Userspace firmware loader will return -EAGAIN in case no
1648 * patch/nvm-config is found, so run with original fw/config.
1652 if (retries
< MAX_INIT_RETRIES
) {
1653 qca_power_shutdown(hu
);
1655 serdev_device_close(hu
->serdev
);
1656 ret
= serdev_device_open(hu
->serdev
);
1658 bt_dev_err(hdev
, "failed to open port");
1668 if (qca_is_wcn399x(soc_type
))
1669 hu
->hdev
->set_bdaddr
= qca_set_bdaddr
;
1671 hu
->hdev
->set_bdaddr
= qca_set_bdaddr_rome
;
1676 static const struct hci_uart_proto qca_proto
= {
1680 .init_speed
= 115200,
1681 .oper_speed
= 3000000,
1687 .enqueue
= qca_enqueue
,
1688 .dequeue
= qca_dequeue
,
1691 static const struct qca_vreg_data qca_soc_data_wcn3990
= {
1692 .soc_type
= QCA_WCN3990
,
1693 .vregs
= (struct qca_vreg
[]) {
1696 { "vddrf", 300000 },
1697 { "vddch0", 450000 },
1702 static const struct qca_vreg_data qca_soc_data_wcn3991
= {
1703 .soc_type
= QCA_WCN3991
,
1704 .vregs
= (struct qca_vreg
[]) {
1707 { "vddrf", 300000 },
1708 { "vddch0", 450000 },
1713 static const struct qca_vreg_data qca_soc_data_wcn3998
= {
1714 .soc_type
= QCA_WCN3998
,
1715 .vregs
= (struct qca_vreg
[]) {
1718 { "vddrf", 300000 },
1719 { "vddch0", 450000 },
1724 static void qca_power_shutdown(struct hci_uart
*hu
)
1726 struct qca_serdev
*qcadev
;
1727 struct qca_data
*qca
= hu
->priv
;
1728 unsigned long flags
;
1729 enum qca_btsoc_type soc_type
= qca_soc_type(hu
);
1731 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
1733 /* From this point we go into power off state. But serial port is
1734 * still open, stop queueing the IBS data and flush all the buffered
1737 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
1738 clear_bit(QCA_IBS_ENABLED
, &qca
->flags
);
1740 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
1742 hu
->hdev
->hw_error
= NULL
;
1743 hu
->hdev
->cmd_timeout
= NULL
;
1745 /* Non-serdev device usually is powered by external power
1746 * and don't need additional action in driver for power down
1751 if (qca_is_wcn399x(soc_type
)) {
1752 host_set_baudrate(hu
, 2400);
1753 qca_send_power_pulse(hu
, false);
1754 qca_regulator_disable(qcadev
);
1755 } else if (qcadev
->bt_en
) {
1756 gpiod_set_value_cansleep(qcadev
->bt_en
, 0);
1760 static int qca_power_off(struct hci_dev
*hdev
)
1762 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
1763 struct qca_data
*qca
= hu
->priv
;
1764 enum qca_btsoc_type soc_type
= qca_soc_type(hu
);
1766 /* Stop sending shutdown command if soc crashes. */
1767 if (qca_is_wcn399x(soc_type
)
1768 && qca
->memdump_state
== QCA_MEMDUMP_IDLE
) {
1769 qca_send_pre_shutdown_cmd(hdev
);
1770 usleep_range(8000, 10000);
1773 qca
->memdump_state
= QCA_MEMDUMP_IDLE
;
1774 qca_power_shutdown(hu
);
1778 static int qca_regulator_enable(struct qca_serdev
*qcadev
)
1780 struct qca_power
*power
= qcadev
->bt_power
;
1783 /* Already enabled */
1784 if (power
->vregs_on
)
1787 BT_DBG("enabling %d regulators)", power
->num_vregs
);
1789 ret
= regulator_bulk_enable(power
->num_vregs
, power
->vreg_bulk
);
1793 power
->vregs_on
= true;
1795 ret
= clk_prepare_enable(qcadev
->susclk
);
1797 qca_regulator_disable(qcadev
);
1802 static void qca_regulator_disable(struct qca_serdev
*qcadev
)
1804 struct qca_power
*power
;
1809 power
= qcadev
->bt_power
;
1811 /* Already disabled? */
1812 if (!power
->vregs_on
)
1815 regulator_bulk_disable(power
->num_vregs
, power
->vreg_bulk
);
1816 power
->vregs_on
= false;
1818 clk_disable_unprepare(qcadev
->susclk
);
1821 static int qca_init_regulators(struct qca_power
*qca
,
1822 const struct qca_vreg
*vregs
, size_t num_vregs
)
1824 struct regulator_bulk_data
*bulk
;
1828 bulk
= devm_kcalloc(qca
->dev
, num_vregs
, sizeof(*bulk
), GFP_KERNEL
);
1832 for (i
= 0; i
< num_vregs
; i
++)
1833 bulk
[i
].supply
= vregs
[i
].name
;
1835 ret
= devm_regulator_bulk_get(qca
->dev
, num_vregs
, bulk
);
1839 for (i
= 0; i
< num_vregs
; i
++) {
1840 ret
= regulator_set_load(bulk
[i
].consumer
, vregs
[i
].load_uA
);
1845 qca
->vreg_bulk
= bulk
;
1846 qca
->num_vregs
= num_vregs
;
1851 static int qca_serdev_probe(struct serdev_device
*serdev
)
1853 struct qca_serdev
*qcadev
;
1854 struct hci_dev
*hdev
;
1855 const struct qca_vreg_data
*data
;
1857 bool power_ctrl_enabled
= true;
1859 qcadev
= devm_kzalloc(&serdev
->dev
, sizeof(*qcadev
), GFP_KERNEL
);
1863 qcadev
->serdev_hu
.serdev
= serdev
;
1864 data
= device_get_match_data(&serdev
->dev
);
1865 serdev_device_set_drvdata(serdev
, qcadev
);
1866 device_property_read_string(&serdev
->dev
, "firmware-name",
1867 &qcadev
->firmware_name
);
1868 if (data
&& qca_is_wcn399x(data
->soc_type
)) {
1869 qcadev
->btsoc_type
= data
->soc_type
;
1870 qcadev
->bt_power
= devm_kzalloc(&serdev
->dev
,
1871 sizeof(struct qca_power
),
1873 if (!qcadev
->bt_power
)
1876 qcadev
->bt_power
->dev
= &serdev
->dev
;
1877 err
= qca_init_regulators(qcadev
->bt_power
, data
->vregs
,
1880 BT_ERR("Failed to init regulators:%d", err
);
1884 qcadev
->bt_power
->vregs_on
= false;
1886 qcadev
->susclk
= devm_clk_get_optional(&serdev
->dev
, NULL
);
1887 if (IS_ERR(qcadev
->susclk
)) {
1888 dev_err(&serdev
->dev
, "failed to acquire clk\n");
1889 return PTR_ERR(qcadev
->susclk
);
1892 device_property_read_u32(&serdev
->dev
, "max-speed",
1893 &qcadev
->oper_speed
);
1894 if (!qcadev
->oper_speed
)
1895 BT_DBG("UART will pick default operating speed");
1897 err
= hci_uart_register_device(&qcadev
->serdev_hu
, &qca_proto
);
1899 BT_ERR("wcn3990 serdev registration failed");
1903 qcadev
->btsoc_type
= QCA_ROME
;
1904 qcadev
->bt_en
= devm_gpiod_get_optional(&serdev
->dev
, "enable",
1906 if (!qcadev
->bt_en
) {
1907 dev_warn(&serdev
->dev
, "failed to acquire enable gpio\n");
1908 power_ctrl_enabled
= false;
1911 qcadev
->susclk
= devm_clk_get_optional(&serdev
->dev
, NULL
);
1912 if (!qcadev
->susclk
) {
1913 dev_warn(&serdev
->dev
, "failed to acquire clk\n");
1915 err
= clk_set_rate(qcadev
->susclk
, SUSCLK_RATE_32KHZ
);
1919 err
= clk_prepare_enable(qcadev
->susclk
);
1924 err
= hci_uart_register_device(&qcadev
->serdev_hu
, &qca_proto
);
1926 BT_ERR("Rome serdev registration failed");
1928 clk_disable_unprepare(qcadev
->susclk
);
1933 if (power_ctrl_enabled
) {
1934 hdev
= qcadev
->serdev_hu
.hdev
;
1935 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP
, &hdev
->quirks
);
1936 hdev
->shutdown
= qca_power_off
;
1942 static void qca_serdev_remove(struct serdev_device
*serdev
)
1944 struct qca_serdev
*qcadev
= serdev_device_get_drvdata(serdev
);
1946 if (qca_is_wcn399x(qcadev
->btsoc_type
))
1947 qca_power_shutdown(&qcadev
->serdev_hu
);
1948 else if (qcadev
->susclk
)
1949 clk_disable_unprepare(qcadev
->susclk
);
1951 hci_uart_unregister_device(&qcadev
->serdev_hu
);
1954 static int __maybe_unused
qca_suspend(struct device
*dev
)
1956 struct serdev_device
*serdev
= to_serdev_device(dev
);
1957 struct qca_serdev
*qcadev
= serdev_device_get_drvdata(serdev
);
1958 struct hci_uart
*hu
= &qcadev
->serdev_hu
;
1959 struct qca_data
*qca
= hu
->priv
;
1960 unsigned long flags
;
1964 set_bit(QCA_SUSPENDING
, &qca
->flags
);
1966 /* Device is downloading patch or doesn't support in-band sleep. */
1967 if (!test_bit(QCA_IBS_ENABLED
, &qca
->flags
))
1970 cancel_work_sync(&qca
->ws_awake_device
);
1971 cancel_work_sync(&qca
->ws_awake_rx
);
1973 spin_lock_irqsave_nested(&qca
->hci_ibs_lock
,
1974 flags
, SINGLE_DEPTH_NESTING
);
1976 switch (qca
->tx_ibs_state
) {
1977 case HCI_IBS_TX_WAKING
:
1978 del_timer(&qca
->wake_retrans_timer
);
1980 case HCI_IBS_TX_AWAKE
:
1981 del_timer(&qca
->tx_idle_timer
);
1983 serdev_device_write_flush(hu
->serdev
);
1984 cmd
= HCI_IBS_SLEEP_IND
;
1985 ret
= serdev_device_write_buf(hu
->serdev
, &cmd
, sizeof(cmd
));
1988 BT_ERR("Failed to send SLEEP to device");
1992 qca
->tx_ibs_state
= HCI_IBS_TX_ASLEEP
;
1993 qca
->ibs_sent_slps
++;
1995 qca_wq_serial_tx_clock_vote_off(&qca
->ws_tx_vote_off
);
1998 case HCI_IBS_TX_ASLEEP
:
2002 BT_ERR("Spurious tx state %d", qca
->tx_ibs_state
);
2007 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
2012 serdev_device_wait_until_sent(hu
->serdev
,
2013 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS
));
2015 /* Wait for HCI_IBS_SLEEP_IND sent by device to indicate its Tx is going
2016 * to sleep, so that the packet does not wake the system later.
2019 ret
= wait_event_interruptible_timeout(qca
->suspend_wait_q
,
2020 qca
->rx_ibs_state
== HCI_IBS_RX_ASLEEP
,
2021 msecs_to_jiffies(IBS_BTSOC_TX_IDLE_TIMEOUT_MS
));
2030 clear_bit(QCA_SUSPENDING
, &qca
->flags
);
2035 static int __maybe_unused
qca_resume(struct device
*dev
)
2037 struct serdev_device
*serdev
= to_serdev_device(dev
);
2038 struct qca_serdev
*qcadev
= serdev_device_get_drvdata(serdev
);
2039 struct hci_uart
*hu
= &qcadev
->serdev_hu
;
2040 struct qca_data
*qca
= hu
->priv
;
2042 clear_bit(QCA_SUSPENDING
, &qca
->flags
);
2047 static SIMPLE_DEV_PM_OPS(qca_pm_ops
, qca_suspend
, qca_resume
);
2049 static const struct of_device_id qca_bluetooth_of_match
[] = {
2050 { .compatible
= "qcom,qca6174-bt" },
2051 { .compatible
= "qcom,wcn3990-bt", .data
= &qca_soc_data_wcn3990
},
2052 { .compatible
= "qcom,wcn3991-bt", .data
= &qca_soc_data_wcn3991
},
2053 { .compatible
= "qcom,wcn3998-bt", .data
= &qca_soc_data_wcn3998
},
2056 MODULE_DEVICE_TABLE(of
, qca_bluetooth_of_match
);
2058 static struct serdev_device_driver qca_serdev_driver
= {
2059 .probe
= qca_serdev_probe
,
2060 .remove
= qca_serdev_remove
,
2062 .name
= "hci_uart_qca",
2063 .of_match_table
= qca_bluetooth_of_match
,
2068 int __init
qca_init(void)
2070 serdev_device_driver_register(&qca_serdev_driver
);
2072 return hci_uart_register_proto(&qca_proto
);
2075 int __exit
qca_deinit(void)
2077 serdev_device_driver_unregister(&qca_serdev_driver
);
2079 return hci_uart_unregister_proto(&qca_proto
);