Thomas Gleixner | 4505153 | 2019-05-29 16:57:47 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Bluetooth Software UART Qualcomm protocol |
| 4 | * |
| 5 | * HCI_IBS (HCI In-Band Sleep) is Qualcomm's power management |
| 6 | * protocol extension to H4. |
| 7 | * |
| 8 | * Copyright (C) 2007 Texas Instruments, Inc. |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 9 | * Copyright (c) 2010, 2012, 2018 The Linux Foundation. All rights reserved. |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 10 | * |
| 11 | * Acknowledgements: |
| 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. |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <linux/kernel.h> |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 19 | #include <linux/clk.h> |
Matthias Kaehlcke | 2faa3f1 | 2019-05-21 12:53:07 -0700 | [diff] [blame] | 20 | #include <linux/completion.h> |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 21 | #include <linux/debugfs.h> |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 22 | #include <linux/delay.h> |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 23 | #include <linux/devcoredump.h> |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 24 | #include <linux/device.h> |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 25 | #include <linux/gpio/consumer.h> |
| 26 | #include <linux/mod_devicetable.h> |
| 27 | #include <linux/module.h> |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 28 | #include <linux/of_device.h> |
Rocky Liao | e5d6468 | 2020-03-25 10:26:37 +0800 | [diff] [blame] | 29 | #include <linux/acpi.h> |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 30 | #include <linux/platform_device.h> |
| 31 | #include <linux/regulator/consumer.h> |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 32 | #include <linux/serdev.h> |
Venkata Lakshmi Narayana Gubba | 7c2c3e6 | 2020-02-14 21:47:15 +0530 | [diff] [blame] | 33 | #include <linux/mutex.h> |
Balakrishna Godavarthi | c614ca3 | 2018-10-16 19:51:35 +0530 | [diff] [blame] | 34 | #include <asm/unaligned.h> |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 35 | |
| 36 | #include <net/bluetooth/bluetooth.h> |
| 37 | #include <net/bluetooth/hci_core.h> |
| 38 | |
| 39 | #include "hci_uart.h" |
| 40 | #include "btqca.h" |
| 41 | |
| 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 |
Marcel Holtmann | f81b001 | 2015-08-30 23:05:32 +0200 | [diff] [blame] | 46 | #define HCI_MAX_IBS_SIZE 10 |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 47 | |
Marcel Holtmann | f81b001 | 2015-08-30 23:05:32 +0200 | [diff] [blame] | 48 | #define IBS_WAKE_RETRANS_TIMEOUT_MS 100 |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 49 | #define IBS_BTSOC_TX_IDLE_TIMEOUT_MS 40 |
| 50 | #define IBS_HOST_TX_IDLE_TIMEOUT_MS 2000 |
Matthias Kaehlcke | 94d6671 | 2019-02-27 15:52:23 -0800 | [diff] [blame] | 51 | #define CMD_TRANS_TIMEOUT_MS 100 |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 52 | #define MEMDUMP_TIMEOUT_MS 8000 |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 53 | |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 54 | /* susclk rate */ |
| 55 | #define SUSCLK_RATE_32KHZ 32768 |
| 56 | |
Balakrishna Godavarthi | c614ca3 | 2018-10-16 19:51:35 +0530 | [diff] [blame] | 57 | /* Controller debug log header */ |
| 58 | #define QCA_DEBUG_HANDLE 0x2EDC |
| 59 | |
Rocky Liao | bb2500ab | 2020-01-15 16:55:51 +0800 | [diff] [blame] | 60 | /* max retry count when init fails */ |
| 61 | #define MAX_INIT_RETRIES 3 |
| 62 | |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 63 | /* Controller dump header */ |
| 64 | #define QCA_SSR_DUMP_HANDLE 0x0108 |
| 65 | #define QCA_DUMP_PACKET_SIZE 255 |
| 66 | #define QCA_LAST_SEQUENCE_NUM 0xFFFF |
| 67 | #define QCA_CRASHBYTE_PACKET_LEN 1096 |
| 68 | #define QCA_MEMDUMP_BYTE 0xFB |
| 69 | |
Matthias Kaehlcke | 62a9199 | 2019-04-29 16:21:30 -0700 | [diff] [blame] | 70 | enum qca_flags { |
| 71 | QCA_IBS_ENABLED, |
Matthias Kaehlcke | 2faa3f1 | 2019-05-21 12:53:07 -0700 | [diff] [blame] | 72 | QCA_DROP_VENDOR_EVENT, |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 73 | QCA_SUSPENDING, |
Venkata Lakshmi Narayana Gubba | 7c2c3e6 | 2020-02-14 21:47:15 +0530 | [diff] [blame] | 74 | QCA_MEMDUMP_COLLECTION, |
| 75 | QCA_HW_ERROR_EVENT |
Matthias Kaehlcke | 62a9199 | 2019-04-29 16:21:30 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
Abhishek Pandit-Subedi | a228f7a | 2020-05-14 13:14:04 -0700 | [diff] [blame] | 78 | enum qca_capabilities { |
| 79 | QCA_CAP_WIDEBAND_SPEECH = BIT(0), |
| 80 | }; |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 81 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 82 | /* HCI_IBS transmit side sleep protocol states */ |
| 83 | enum tx_ibs_states { |
| 84 | HCI_IBS_TX_ASLEEP, |
| 85 | HCI_IBS_TX_WAKING, |
| 86 | HCI_IBS_TX_AWAKE, |
| 87 | }; |
| 88 | |
| 89 | /* HCI_IBS receive side sleep protocol states */ |
| 90 | enum rx_states { |
| 91 | HCI_IBS_RX_ASLEEP, |
| 92 | HCI_IBS_RX_AWAKE, |
| 93 | }; |
| 94 | |
| 95 | /* HCI_IBS transmit and receive side clock state vote */ |
| 96 | enum hci_ibs_clock_state_vote { |
| 97 | HCI_IBS_VOTE_STATS_UPDATE, |
| 98 | HCI_IBS_TX_VOTE_CLOCK_ON, |
| 99 | HCI_IBS_TX_VOTE_CLOCK_OFF, |
| 100 | HCI_IBS_RX_VOTE_CLOCK_ON, |
| 101 | HCI_IBS_RX_VOTE_CLOCK_OFF, |
| 102 | }; |
| 103 | |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 104 | /* Controller memory dump states */ |
| 105 | enum qca_memdump_states { |
| 106 | QCA_MEMDUMP_IDLE, |
| 107 | QCA_MEMDUMP_COLLECTING, |
| 108 | QCA_MEMDUMP_COLLECTED, |
| 109 | QCA_MEMDUMP_TIMEOUT, |
| 110 | }; |
| 111 | |
| 112 | struct qca_memdump_data { |
| 113 | char *memdump_buf_head; |
| 114 | char *memdump_buf_tail; |
| 115 | u32 current_seq_no; |
| 116 | u32 received_dump; |
| 117 | }; |
| 118 | |
| 119 | struct qca_memdump_event_hdr { |
| 120 | __u8 evt; |
| 121 | __u8 plen; |
| 122 | __u16 opcode; |
| 123 | __u16 seq_no; |
| 124 | __u8 reserved; |
| 125 | } __packed; |
| 126 | |
| 127 | |
| 128 | struct qca_dump_size { |
| 129 | u32 dump_size; |
| 130 | } __packed; |
| 131 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 132 | struct qca_data { |
| 133 | struct hci_uart *hu; |
| 134 | struct sk_buff *rx_skb; |
| 135 | struct sk_buff_head txq; |
| 136 | struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */ |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 137 | struct sk_buff_head rx_memdump_q; /* Memdump wait queue */ |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 138 | spinlock_t hci_ibs_lock; /* HCI_IBS state lock */ |
| 139 | u8 tx_ibs_state; /* HCI_IBS transmit side power state*/ |
| 140 | u8 rx_ibs_state; /* HCI_IBS receive side power state */ |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 141 | bool tx_vote; /* Clock must be on for TX */ |
| 142 | bool rx_vote; /* Clock must be on for RX */ |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 143 | struct timer_list tx_idle_timer; |
| 144 | u32 tx_idle_delay; |
| 145 | struct timer_list wake_retrans_timer; |
| 146 | u32 wake_retrans; |
| 147 | struct workqueue_struct *workqueue; |
| 148 | struct work_struct ws_awake_rx; |
| 149 | struct work_struct ws_awake_device; |
| 150 | struct work_struct ws_rx_vote_off; |
| 151 | struct work_struct ws_tx_vote_off; |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 152 | struct work_struct ctrl_memdump_evt; |
Venkata Lakshmi Narayana Gubba | 7c2c3e6 | 2020-02-14 21:47:15 +0530 | [diff] [blame] | 153 | struct delayed_work ctrl_memdump_timeout; |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 154 | struct qca_memdump_data *qca_memdump; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 155 | unsigned long flags; |
Matthias Kaehlcke | 2faa3f1 | 2019-05-21 12:53:07 -0700 | [diff] [blame] | 156 | struct completion drop_ev_comp; |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 157 | wait_queue_head_t suspend_wait_q; |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 158 | enum qca_memdump_states memdump_state; |
Venkata Lakshmi Narayana Gubba | 7c2c3e6 | 2020-02-14 21:47:15 +0530 | [diff] [blame] | 159 | struct mutex hci_memdump_lock; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 160 | |
| 161 | /* For debugging purpose */ |
| 162 | u64 ibs_sent_wacks; |
| 163 | u64 ibs_sent_slps; |
| 164 | u64 ibs_sent_wakes; |
| 165 | u64 ibs_recv_wacks; |
| 166 | u64 ibs_recv_slps; |
| 167 | u64 ibs_recv_wakes; |
| 168 | u64 vote_last_jif; |
| 169 | u32 vote_on_ms; |
| 170 | u32 vote_off_ms; |
| 171 | u64 tx_votes_on; |
| 172 | u64 rx_votes_on; |
| 173 | u64 tx_votes_off; |
| 174 | u64 rx_votes_off; |
| 175 | u64 votes_on; |
| 176 | u64 votes_off; |
| 177 | }; |
| 178 | |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 179 | enum qca_speed_type { |
| 180 | QCA_INIT_SPEED = 1, |
| 181 | QCA_OPER_SPEED |
| 182 | }; |
| 183 | |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 184 | /* |
| 185 | * Voltage regulator information required for configuring the |
| 186 | * QCA Bluetooth chipset |
| 187 | */ |
| 188 | struct qca_vreg { |
| 189 | const char *name; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 190 | unsigned int load_uA; |
| 191 | }; |
| 192 | |
Abhishek Pandit-Subedi | a228f7a | 2020-05-14 13:14:04 -0700 | [diff] [blame] | 193 | struct qca_device_data { |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 194 | enum qca_btsoc_type soc_type; |
| 195 | struct qca_vreg *vregs; |
| 196 | size_t num_vregs; |
Abhishek Pandit-Subedi | a228f7a | 2020-05-14 13:14:04 -0700 | [diff] [blame] | 197 | uint32_t capabilities; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | /* |
| 201 | * Platform data for the QCA Bluetooth power driver. |
| 202 | */ |
| 203 | struct qca_power { |
| 204 | struct device *dev; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 205 | struct regulator_bulk_data *vreg_bulk; |
Bjorn Andersson | 163d42f | 2019-10-17 22:24:03 -0700 | [diff] [blame] | 206 | int num_vregs; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 207 | bool vregs_on; |
| 208 | }; |
| 209 | |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 210 | struct qca_serdev { |
| 211 | struct hci_uart serdev_hu; |
| 212 | struct gpio_desc *bt_en; |
| 213 | struct clk *susclk; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 214 | enum qca_btsoc_type btsoc_type; |
| 215 | struct qca_power *bt_power; |
| 216 | u32 init_speed; |
| 217 | u32 oper_speed; |
Rocky Liao | 99c905c | 2019-06-06 17:40:30 +0800 | [diff] [blame] | 218 | const char *firmware_name; |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 219 | }; |
| 220 | |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 221 | static int qca_regulator_enable(struct qca_serdev *qcadev); |
| 222 | static void qca_regulator_disable(struct qca_serdev *qcadev); |
Balakrishna Godavarthi | c2d7827 | 2018-08-22 17:50:05 +0530 | [diff] [blame] | 223 | static void qca_power_shutdown(struct hci_uart *hu); |
Balakrishna Godavarthi | 3e4be65 | 2018-09-24 20:14:45 +0530 | [diff] [blame] | 224 | static int qca_power_off(struct hci_dev *hdev); |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 225 | static void qca_controller_memdump(struct work_struct *work); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 226 | |
Matthias Kaehlcke | 4fdd5a4 | 2019-03-11 11:38:31 -0700 | [diff] [blame] | 227 | static enum qca_btsoc_type qca_soc_type(struct hci_uart *hu) |
| 228 | { |
| 229 | enum qca_btsoc_type soc_type; |
| 230 | |
| 231 | if (hu->serdev) { |
| 232 | struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev); |
| 233 | |
| 234 | soc_type = qsd->btsoc_type; |
| 235 | } else { |
| 236 | soc_type = QCA_ROME; |
| 237 | } |
| 238 | |
| 239 | return soc_type; |
| 240 | } |
| 241 | |
Rocky Liao | 99c905c | 2019-06-06 17:40:30 +0800 | [diff] [blame] | 242 | static const char *qca_get_firmware_name(struct hci_uart *hu) |
| 243 | { |
| 244 | if (hu->serdev) { |
| 245 | struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev); |
| 246 | |
| 247 | return qsd->firmware_name; |
| 248 | } else { |
| 249 | return NULL; |
| 250 | } |
| 251 | } |
| 252 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 253 | static void __serial_clock_on(struct tty_struct *tty) |
| 254 | { |
| 255 | /* TODO: Some chipset requires to enable UART clock on client |
| 256 | * side to save power consumption or manual work is required. |
| 257 | * Please put your code to control UART clock here if needed |
| 258 | */ |
| 259 | } |
| 260 | |
| 261 | static void __serial_clock_off(struct tty_struct *tty) |
| 262 | { |
| 263 | /* TODO: Some chipset requires to disable UART clock on client |
| 264 | * side to save power consumption or manual work is required. |
| 265 | * Please put your code to control UART clock off here if needed |
| 266 | */ |
| 267 | } |
| 268 | |
| 269 | /* serial_clock_vote needs to be called with the ibs lock held */ |
| 270 | static void serial_clock_vote(unsigned long vote, struct hci_uart *hu) |
| 271 | { |
| 272 | struct qca_data *qca = hu->priv; |
| 273 | unsigned int diff; |
| 274 | |
| 275 | bool old_vote = (qca->tx_vote | qca->rx_vote); |
| 276 | bool new_vote; |
| 277 | |
| 278 | switch (vote) { |
| 279 | case HCI_IBS_VOTE_STATS_UPDATE: |
| 280 | diff = jiffies_to_msecs(jiffies - qca->vote_last_jif); |
| 281 | |
| 282 | if (old_vote) |
| 283 | qca->vote_off_ms += diff; |
| 284 | else |
| 285 | qca->vote_on_ms += diff; |
| 286 | return; |
| 287 | |
| 288 | case HCI_IBS_TX_VOTE_CLOCK_ON: |
| 289 | qca->tx_vote = true; |
| 290 | qca->tx_votes_on++; |
| 291 | new_vote = true; |
| 292 | break; |
| 293 | |
| 294 | case HCI_IBS_RX_VOTE_CLOCK_ON: |
| 295 | qca->rx_vote = true; |
| 296 | qca->rx_votes_on++; |
| 297 | new_vote = true; |
| 298 | break; |
| 299 | |
| 300 | case HCI_IBS_TX_VOTE_CLOCK_OFF: |
| 301 | qca->tx_vote = false; |
| 302 | qca->tx_votes_off++; |
| 303 | new_vote = qca->rx_vote | qca->tx_vote; |
| 304 | break; |
| 305 | |
| 306 | case HCI_IBS_RX_VOTE_CLOCK_OFF: |
| 307 | qca->rx_vote = false; |
| 308 | qca->rx_votes_off++; |
| 309 | new_vote = qca->rx_vote | qca->tx_vote; |
| 310 | break; |
| 311 | |
| 312 | default: |
| 313 | BT_ERR("Voting irregularity"); |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | if (new_vote != old_vote) { |
| 318 | if (new_vote) |
| 319 | __serial_clock_on(hu->tty); |
| 320 | else |
| 321 | __serial_clock_off(hu->tty); |
| 322 | |
Prasanna Karthik | ce26d81 | 2015-09-15 12:19:45 +0000 | [diff] [blame] | 323 | BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false", |
| 324 | vote ? "true" : "false"); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 325 | |
| 326 | diff = jiffies_to_msecs(jiffies - qca->vote_last_jif); |
| 327 | |
| 328 | if (new_vote) { |
| 329 | qca->votes_on++; |
| 330 | qca->vote_off_ms += diff; |
| 331 | } else { |
| 332 | qca->votes_off++; |
| 333 | qca->vote_on_ms += diff; |
| 334 | } |
| 335 | qca->vote_last_jif = jiffies; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /* Builds and sends an HCI_IBS command packet. |
| 340 | * These are very simple packets with only 1 cmd byte. |
| 341 | */ |
| 342 | static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu) |
| 343 | { |
| 344 | int err = 0; |
| 345 | struct sk_buff *skb = NULL; |
| 346 | struct qca_data *qca = hu->priv; |
| 347 | |
| 348 | BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd); |
| 349 | |
| 350 | skb = bt_skb_alloc(1, GFP_ATOMIC); |
| 351 | if (!skb) { |
| 352 | BT_ERR("Failed to allocate memory for HCI_IBS packet"); |
| 353 | return -ENOMEM; |
| 354 | } |
| 355 | |
| 356 | /* Assign HCI_IBS type */ |
Johannes Berg | 634fef6 | 2017-06-16 14:29:24 +0200 | [diff] [blame] | 357 | skb_put_u8(skb, cmd); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 358 | |
| 359 | skb_queue_tail(&qca->txq, skb); |
| 360 | |
| 361 | return err; |
| 362 | } |
| 363 | |
| 364 | static void qca_wq_awake_device(struct work_struct *work) |
| 365 | { |
| 366 | struct qca_data *qca = container_of(work, struct qca_data, |
| 367 | ws_awake_device); |
| 368 | struct hci_uart *hu = qca->hu; |
| 369 | unsigned long retrans_delay; |
Harish Bandi | 31fb1bb | 2019-09-04 10:04:16 +0530 | [diff] [blame] | 370 | unsigned long flags; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 371 | |
| 372 | BT_DBG("hu %p wq awake device", hu); |
| 373 | |
| 374 | /* Vote for serial clock */ |
| 375 | serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu); |
| 376 | |
Harish Bandi | 31fb1bb | 2019-09-04 10:04:16 +0530 | [diff] [blame] | 377 | spin_lock_irqsave(&qca->hci_ibs_lock, flags); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 378 | |
| 379 | /* Send wake indication to device */ |
| 380 | if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) |
| 381 | BT_ERR("Failed to send WAKE to device"); |
| 382 | |
| 383 | qca->ibs_sent_wakes++; |
| 384 | |
| 385 | /* Start retransmit timer */ |
| 386 | retrans_delay = msecs_to_jiffies(qca->wake_retrans); |
| 387 | mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay); |
| 388 | |
Harish Bandi | 31fb1bb | 2019-09-04 10:04:16 +0530 | [diff] [blame] | 389 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 390 | |
| 391 | /* Actually send the packets */ |
| 392 | hci_uart_tx_wakeup(hu); |
| 393 | } |
| 394 | |
| 395 | static void qca_wq_awake_rx(struct work_struct *work) |
| 396 | { |
| 397 | struct qca_data *qca = container_of(work, struct qca_data, |
| 398 | ws_awake_rx); |
| 399 | struct hci_uart *hu = qca->hu; |
Harish Bandi | 31fb1bb | 2019-09-04 10:04:16 +0530 | [diff] [blame] | 400 | unsigned long flags; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 401 | |
| 402 | BT_DBG("hu %p wq awake rx", hu); |
| 403 | |
| 404 | serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu); |
| 405 | |
Harish Bandi | 31fb1bb | 2019-09-04 10:04:16 +0530 | [diff] [blame] | 406 | spin_lock_irqsave(&qca->hci_ibs_lock, flags); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 407 | qca->rx_ibs_state = HCI_IBS_RX_AWAKE; |
| 408 | |
| 409 | /* Always acknowledge device wake up, |
| 410 | * sending IBS message doesn't count as TX ON. |
| 411 | */ |
| 412 | if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) |
| 413 | BT_ERR("Failed to acknowledge device wake up"); |
| 414 | |
| 415 | qca->ibs_sent_wacks++; |
| 416 | |
Harish Bandi | 31fb1bb | 2019-09-04 10:04:16 +0530 | [diff] [blame] | 417 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 418 | |
| 419 | /* Actually send the packets */ |
| 420 | hci_uart_tx_wakeup(hu); |
| 421 | } |
| 422 | |
| 423 | static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work) |
| 424 | { |
| 425 | struct qca_data *qca = container_of(work, struct qca_data, |
| 426 | ws_rx_vote_off); |
| 427 | struct hci_uart *hu = qca->hu; |
| 428 | |
| 429 | BT_DBG("hu %p rx clock vote off", hu); |
| 430 | |
| 431 | serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu); |
| 432 | } |
| 433 | |
| 434 | static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work) |
| 435 | { |
| 436 | struct qca_data *qca = container_of(work, struct qca_data, |
| 437 | ws_tx_vote_off); |
| 438 | struct hci_uart *hu = qca->hu; |
| 439 | |
| 440 | BT_DBG("hu %p tx clock vote off", hu); |
| 441 | |
| 442 | /* Run HCI tx handling unlocked */ |
| 443 | hci_uart_tx_wakeup(hu); |
| 444 | |
| 445 | /* Now that message queued to tty driver, vote for tty clocks off. |
| 446 | * It is up to the tty driver to pend the clocks off until tx done. |
| 447 | */ |
| 448 | serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu); |
| 449 | } |
| 450 | |
Kees Cook | 0435605 | 2017-10-04 17:54:29 -0700 | [diff] [blame] | 451 | static void hci_ibs_tx_idle_timeout(struct timer_list *t) |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 452 | { |
Kees Cook | 0435605 | 2017-10-04 17:54:29 -0700 | [diff] [blame] | 453 | struct qca_data *qca = from_timer(qca, t, tx_idle_timer); |
| 454 | struct hci_uart *hu = qca->hu; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 455 | unsigned long flags; |
| 456 | |
| 457 | BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state); |
| 458 | |
| 459 | spin_lock_irqsave_nested(&qca->hci_ibs_lock, |
| 460 | flags, SINGLE_DEPTH_NESTING); |
| 461 | |
| 462 | switch (qca->tx_ibs_state) { |
| 463 | case HCI_IBS_TX_AWAKE: |
| 464 | /* TX_IDLE, go to SLEEP */ |
| 465 | if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) { |
| 466 | BT_ERR("Failed to send SLEEP to device"); |
| 467 | break; |
| 468 | } |
| 469 | qca->tx_ibs_state = HCI_IBS_TX_ASLEEP; |
| 470 | qca->ibs_sent_slps++; |
| 471 | queue_work(qca->workqueue, &qca->ws_tx_vote_off); |
| 472 | break; |
| 473 | |
| 474 | case HCI_IBS_TX_ASLEEP: |
| 475 | case HCI_IBS_TX_WAKING: |
| 476 | /* Fall through */ |
| 477 | |
| 478 | default: |
Colin Ian King | e059a46 | 2017-02-17 19:58:10 +0000 | [diff] [blame] | 479 | BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 480 | break; |
| 481 | } |
| 482 | |
| 483 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
| 484 | } |
| 485 | |
Kees Cook | 0435605 | 2017-10-04 17:54:29 -0700 | [diff] [blame] | 486 | static void hci_ibs_wake_retrans_timeout(struct timer_list *t) |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 487 | { |
Kees Cook | 0435605 | 2017-10-04 17:54:29 -0700 | [diff] [blame] | 488 | struct qca_data *qca = from_timer(qca, t, wake_retrans_timer); |
| 489 | struct hci_uart *hu = qca->hu; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 490 | unsigned long flags, retrans_delay; |
Prasanna Karthik | a913718 | 2015-09-28 08:03:24 +0000 | [diff] [blame] | 491 | bool retransmit = false; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 492 | |
| 493 | BT_DBG("hu %p wake retransmit timeout in %d state", |
| 494 | hu, qca->tx_ibs_state); |
| 495 | |
| 496 | spin_lock_irqsave_nested(&qca->hci_ibs_lock, |
| 497 | flags, SINGLE_DEPTH_NESTING); |
| 498 | |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 499 | /* Don't retransmit the HCI_IBS_WAKE_IND when suspending. */ |
| 500 | if (test_bit(QCA_SUSPENDING, &qca->flags)) { |
| 501 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
| 502 | return; |
| 503 | } |
| 504 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 505 | switch (qca->tx_ibs_state) { |
| 506 | case HCI_IBS_TX_WAKING: |
| 507 | /* No WAKE_ACK, retransmit WAKE */ |
Prasanna Karthik | a913718 | 2015-09-28 08:03:24 +0000 | [diff] [blame] | 508 | retransmit = true; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 509 | if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) { |
| 510 | BT_ERR("Failed to acknowledge device wake up"); |
| 511 | break; |
| 512 | } |
| 513 | qca->ibs_sent_wakes++; |
| 514 | retrans_delay = msecs_to_jiffies(qca->wake_retrans); |
| 515 | mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay); |
| 516 | break; |
| 517 | |
| 518 | case HCI_IBS_TX_ASLEEP: |
| 519 | case HCI_IBS_TX_AWAKE: |
| 520 | /* Fall through */ |
| 521 | |
| 522 | default: |
Colin Ian King | e059a46 | 2017-02-17 19:58:10 +0000 | [diff] [blame] | 523 | BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 524 | break; |
| 525 | } |
| 526 | |
| 527 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
| 528 | |
| 529 | if (retransmit) |
| 530 | hci_uart_tx_wakeup(hu); |
| 531 | } |
| 532 | |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 533 | |
Venkata Lakshmi Narayana Gubba | 7c2c3e6 | 2020-02-14 21:47:15 +0530 | [diff] [blame] | 534 | static void qca_controller_memdump_timeout(struct work_struct *work) |
| 535 | { |
| 536 | struct qca_data *qca = container_of(work, struct qca_data, |
| 537 | ctrl_memdump_timeout.work); |
| 538 | struct hci_uart *hu = qca->hu; |
| 539 | |
| 540 | mutex_lock(&qca->hci_memdump_lock); |
| 541 | if (test_bit(QCA_MEMDUMP_COLLECTION, &qca->flags)) { |
| 542 | qca->memdump_state = QCA_MEMDUMP_TIMEOUT; |
| 543 | if (!test_bit(QCA_HW_ERROR_EVENT, &qca->flags)) { |
| 544 | /* Inject hw error event to reset the device |
| 545 | * and driver. |
| 546 | */ |
| 547 | hci_reset_dev(hu->hdev); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | mutex_unlock(&qca->hci_memdump_lock); |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 552 | } |
| 553 | |
Venkata Lakshmi Narayana Gubba | 7c2c3e6 | 2020-02-14 21:47:15 +0530 | [diff] [blame] | 554 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 555 | /* Initialize protocol */ |
| 556 | static int qca_open(struct hci_uart *hu) |
| 557 | { |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 558 | struct qca_serdev *qcadev; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 559 | struct qca_data *qca; |
| 560 | |
| 561 | BT_DBG("hu %p qca_open", hu); |
| 562 | |
Vladis Dronov | b36a155 | 2019-07-30 11:33:45 +0200 | [diff] [blame] | 563 | if (!hci_uart_has_flow_control(hu)) |
| 564 | return -EOPNOTSUPP; |
| 565 | |
Jia-Ju Bai | 25a13e38 | 2018-07-23 11:56:51 +0800 | [diff] [blame] | 566 | qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 567 | if (!qca) |
| 568 | return -ENOMEM; |
| 569 | |
| 570 | skb_queue_head_init(&qca->txq); |
| 571 | skb_queue_head_init(&qca->tx_wait_q); |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 572 | skb_queue_head_init(&qca->rx_memdump_q); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 573 | spin_lock_init(&qca->hci_ibs_lock); |
Venkata Lakshmi Narayana Gubba | 7c2c3e6 | 2020-02-14 21:47:15 +0530 | [diff] [blame] | 574 | mutex_init(&qca->hci_memdump_lock); |
Bhaktipriya Shridhar | fac9a60 | 2016-08-30 22:42:53 +0530 | [diff] [blame] | 575 | qca->workqueue = alloc_ordered_workqueue("qca_wq", 0); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 576 | if (!qca->workqueue) { |
| 577 | BT_ERR("QCA Workqueue not initialized properly"); |
| 578 | kfree(qca); |
| 579 | return -ENOMEM; |
| 580 | } |
| 581 | |
| 582 | INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx); |
| 583 | INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device); |
| 584 | INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off); |
| 585 | INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off); |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 586 | INIT_WORK(&qca->ctrl_memdump_evt, qca_controller_memdump); |
Venkata Lakshmi Narayana Gubba | 7c2c3e6 | 2020-02-14 21:47:15 +0530 | [diff] [blame] | 587 | INIT_DELAYED_WORK(&qca->ctrl_memdump_timeout, |
| 588 | qca_controller_memdump_timeout); |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 589 | init_waitqueue_head(&qca->suspend_wait_q); |
| 590 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 591 | qca->hu = hu; |
Matthias Kaehlcke | 2faa3f1 | 2019-05-21 12:53:07 -0700 | [diff] [blame] | 592 | init_completion(&qca->drop_ev_comp); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 593 | |
| 594 | /* Assume we start with both sides asleep -- extra wakes OK */ |
| 595 | qca->tx_ibs_state = HCI_IBS_TX_ASLEEP; |
| 596 | qca->rx_ibs_state = HCI_IBS_RX_ASLEEP; |
| 597 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 598 | qca->vote_last_jif = jiffies; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 599 | |
| 600 | hu->priv = qca; |
| 601 | |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 602 | if (hu->serdev) { |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 603 | qcadev = serdev_device_get_drvdata(hu->serdev); |
Christian Hewitt | 37aee13 | 2020-04-23 01:34:30 +0000 | [diff] [blame] | 604 | |
| 605 | if (qca_is_wcn399x(qcadev->btsoc_type)) |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 606 | hu->init_speed = qcadev->init_speed; |
Christian Hewitt | 37aee13 | 2020-04-23 01:34:30 +0000 | [diff] [blame] | 607 | |
| 608 | if (qcadev->oper_speed) |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 609 | hu->oper_speed = qcadev->oper_speed; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 610 | } |
| 611 | |
Kees Cook | 0435605 | 2017-10-04 17:54:29 -0700 | [diff] [blame] | 612 | timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 613 | qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS; |
| 614 | |
Kees Cook | 0435605 | 2017-10-04 17:54:29 -0700 | [diff] [blame] | 615 | timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0); |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 616 | qca->tx_idle_delay = IBS_HOST_TX_IDLE_TIMEOUT_MS; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 617 | |
| 618 | BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u", |
| 619 | qca->tx_idle_delay, qca->wake_retrans); |
| 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | static void qca_debugfs_init(struct hci_dev *hdev) |
| 625 | { |
| 626 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 627 | struct qca_data *qca = hu->priv; |
| 628 | struct dentry *ibs_dir; |
| 629 | umode_t mode; |
| 630 | |
| 631 | if (!hdev->debugfs) |
| 632 | return; |
| 633 | |
| 634 | ibs_dir = debugfs_create_dir("ibs", hdev->debugfs); |
| 635 | |
| 636 | /* read only */ |
| 637 | mode = S_IRUGO; |
| 638 | debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state); |
| 639 | debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state); |
| 640 | debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir, |
| 641 | &qca->ibs_sent_slps); |
| 642 | debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir, |
| 643 | &qca->ibs_sent_wakes); |
| 644 | debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir, |
| 645 | &qca->ibs_sent_wacks); |
| 646 | debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir, |
| 647 | &qca->ibs_recv_slps); |
| 648 | debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir, |
| 649 | &qca->ibs_recv_wakes); |
| 650 | debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir, |
| 651 | &qca->ibs_recv_wacks); |
Ben YoungTae Kim | 10be6c0 | 2015-08-13 22:09:42 -0700 | [diff] [blame] | 652 | debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 653 | debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on); |
| 654 | debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off); |
Ben YoungTae Kim | 10be6c0 | 2015-08-13 22:09:42 -0700 | [diff] [blame] | 655 | debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 656 | debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on); |
| 657 | debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off); |
| 658 | debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on); |
| 659 | debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off); |
| 660 | debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms); |
| 661 | debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms); |
| 662 | |
| 663 | /* read/write */ |
| 664 | mode = S_IRUGO | S_IWUSR; |
| 665 | debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans); |
| 666 | debugfs_create_u32("tx_idle_delay", mode, ibs_dir, |
| 667 | &qca->tx_idle_delay); |
| 668 | } |
| 669 | |
| 670 | /* Flush protocol data */ |
| 671 | static int qca_flush(struct hci_uart *hu) |
| 672 | { |
| 673 | struct qca_data *qca = hu->priv; |
| 674 | |
| 675 | BT_DBG("hu %p qca flush", hu); |
| 676 | |
| 677 | skb_queue_purge(&qca->tx_wait_q); |
| 678 | skb_queue_purge(&qca->txq); |
| 679 | |
| 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | /* Close protocol */ |
| 684 | static int qca_close(struct hci_uart *hu) |
| 685 | { |
| 686 | struct qca_data *qca = hu->priv; |
| 687 | |
| 688 | BT_DBG("hu %p qca close", hu); |
| 689 | |
| 690 | serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu); |
| 691 | |
| 692 | skb_queue_purge(&qca->tx_wait_q); |
| 693 | skb_queue_purge(&qca->txq); |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 694 | skb_queue_purge(&qca->rx_memdump_q); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 695 | del_timer(&qca->tx_idle_timer); |
| 696 | del_timer(&qca->wake_retrans_timer); |
| 697 | destroy_workqueue(qca->workqueue); |
| 698 | qca->hu = NULL; |
| 699 | |
Rocky Liao | 5559904 | 2020-01-15 16:55:50 +0800 | [diff] [blame] | 700 | qca_power_shutdown(hu); |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 701 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 702 | kfree_skb(qca->rx_skb); |
| 703 | |
| 704 | hu->priv = NULL; |
| 705 | |
| 706 | kfree(qca); |
| 707 | |
| 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | /* Called upon a wake-up-indication from the device. |
| 712 | */ |
| 713 | static void device_want_to_wakeup(struct hci_uart *hu) |
| 714 | { |
| 715 | unsigned long flags; |
| 716 | struct qca_data *qca = hu->priv; |
| 717 | |
| 718 | BT_DBG("hu %p want to wake up", hu); |
| 719 | |
| 720 | spin_lock_irqsave(&qca->hci_ibs_lock, flags); |
| 721 | |
| 722 | qca->ibs_recv_wakes++; |
| 723 | |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 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); |
| 727 | return; |
| 728 | } |
| 729 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 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. |
| 734 | */ |
| 735 | queue_work(qca->workqueue, &qca->ws_awake_rx); |
| 736 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
| 737 | return; |
| 738 | |
| 739 | case HCI_IBS_RX_AWAKE: |
| 740 | /* Always acknowledge device wake up, |
| 741 | * sending IBS message doesn't count as TX ON. |
| 742 | */ |
| 743 | if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) { |
| 744 | BT_ERR("Failed to acknowledge device wake up"); |
| 745 | break; |
| 746 | } |
| 747 | qca->ibs_sent_wacks++; |
| 748 | break; |
| 749 | |
| 750 | default: |
| 751 | /* Any other state is illegal */ |
| 752 | BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d", |
| 753 | qca->rx_ibs_state); |
| 754 | break; |
| 755 | } |
| 756 | |
| 757 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
| 758 | |
| 759 | /* Actually send the packets */ |
| 760 | hci_uart_tx_wakeup(hu); |
| 761 | } |
| 762 | |
| 763 | /* Called upon a sleep-indication from the device. |
| 764 | */ |
| 765 | static void device_want_to_sleep(struct hci_uart *hu) |
| 766 | { |
| 767 | unsigned long flags; |
| 768 | struct qca_data *qca = hu->priv; |
| 769 | |
Rocky Liao | 6600c08 | 2019-08-14 15:42:39 +0800 | [diff] [blame] | 770 | BT_DBG("hu %p want to sleep in %d state", hu, qca->rx_ibs_state); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 771 | |
| 772 | spin_lock_irqsave(&qca->hci_ibs_lock, flags); |
| 773 | |
| 774 | qca->ibs_recv_slps++; |
| 775 | |
| 776 | switch (qca->rx_ibs_state) { |
| 777 | case HCI_IBS_RX_AWAKE: |
| 778 | /* Update state */ |
| 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); |
| 782 | break; |
| 783 | |
| 784 | case HCI_IBS_RX_ASLEEP: |
Rocky Liao | 6600c08 | 2019-08-14 15:42:39 +0800 | [diff] [blame] | 785 | break; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 786 | |
| 787 | default: |
| 788 | /* Any other state is illegal */ |
| 789 | BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d", |
| 790 | qca->rx_ibs_state); |
| 791 | break; |
| 792 | } |
| 793 | |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 794 | wake_up_interruptible(&qca->suspend_wait_q); |
| 795 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 796 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
| 797 | } |
| 798 | |
| 799 | /* Called upon wake-up-acknowledgement from the device |
| 800 | */ |
| 801 | static void device_woke_up(struct hci_uart *hu) |
| 802 | { |
| 803 | unsigned long flags, idle_delay; |
| 804 | struct qca_data *qca = hu->priv; |
| 805 | struct sk_buff *skb = NULL; |
| 806 | |
| 807 | BT_DBG("hu %p woke up", hu); |
| 808 | |
| 809 | spin_lock_irqsave(&qca->hci_ibs_lock, flags); |
| 810 | |
| 811 | qca->ibs_recv_wacks++; |
| 812 | |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 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); |
| 816 | return; |
| 817 | } |
| 818 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 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", |
| 823 | qca->tx_ibs_state); |
| 824 | break; |
| 825 | |
| 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); |
| 830 | |
| 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; |
| 836 | break; |
| 837 | |
| 838 | case HCI_IBS_TX_ASLEEP: |
| 839 | /* Fall through */ |
| 840 | |
| 841 | default: |
| 842 | BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d", |
| 843 | qca->tx_ibs_state); |
| 844 | break; |
| 845 | } |
| 846 | |
| 847 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
| 848 | |
| 849 | /* Actually send the packets */ |
| 850 | hci_uart_tx_wakeup(hu); |
| 851 | } |
| 852 | |
| 853 | /* Enqueue frame for transmittion (padding, crc, etc) may be called from |
| 854 | * two simultaneous tasklets. |
| 855 | */ |
| 856 | static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb) |
| 857 | { |
| 858 | unsigned long flags = 0, idle_delay; |
| 859 | struct qca_data *qca = hu->priv; |
| 860 | |
| 861 | BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb, |
| 862 | qca->tx_ibs_state); |
| 863 | |
| 864 | /* Prepend skb with frame type */ |
Marcel Holtmann | 618e8bc | 2015-11-05 07:33:56 +0100 | [diff] [blame] | 865 | memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 866 | |
Balakrishna Godavarthi | 035a960 | 2019-02-04 20:36:43 +0530 | [diff] [blame] | 867 | spin_lock_irqsave(&qca->hci_ibs_lock, flags); |
| 868 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 869 | /* Don't go to sleep in middle of patch download or |
| 870 | * Out-Of-Band(GPIOs control) sleep is selected. |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 871 | * Don't wake the device up when suspending. |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 872 | */ |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 873 | if (!test_bit(QCA_IBS_ENABLED, &qca->flags) || |
| 874 | test_bit(QCA_SUSPENDING, &qca->flags)) { |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 875 | skb_queue_tail(&qca->txq, skb); |
Balakrishna Godavarthi | 035a960 | 2019-02-04 20:36:43 +0530 | [diff] [blame] | 876 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 877 | return 0; |
| 878 | } |
| 879 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 880 | /* Act according to current state */ |
| 881 | switch (qca->tx_ibs_state) { |
| 882 | case HCI_IBS_TX_AWAKE: |
| 883 | BT_DBG("Device awake, sending normally"); |
| 884 | skb_queue_tail(&qca->txq, skb); |
| 885 | idle_delay = msecs_to_jiffies(qca->tx_idle_delay); |
| 886 | mod_timer(&qca->tx_idle_timer, jiffies + idle_delay); |
| 887 | break; |
| 888 | |
| 889 | case HCI_IBS_TX_ASLEEP: |
| 890 | BT_DBG("Device asleep, waking up and queueing packet"); |
| 891 | /* Save packet for later */ |
| 892 | skb_queue_tail(&qca->tx_wait_q, skb); |
| 893 | |
| 894 | qca->tx_ibs_state = HCI_IBS_TX_WAKING; |
| 895 | /* Schedule a work queue to wake up device */ |
| 896 | queue_work(qca->workqueue, &qca->ws_awake_device); |
| 897 | break; |
| 898 | |
| 899 | case HCI_IBS_TX_WAKING: |
| 900 | BT_DBG("Device waking up, queueing packet"); |
| 901 | /* Transient state; just keep packet for later */ |
| 902 | skb_queue_tail(&qca->tx_wait_q, skb); |
| 903 | break; |
| 904 | |
| 905 | default: |
| 906 | BT_ERR("Illegal tx state: %d (losing packet)", |
| 907 | qca->tx_ibs_state); |
| 908 | kfree_skb(skb); |
| 909 | break; |
| 910 | } |
| 911 | |
| 912 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
| 913 | |
| 914 | return 0; |
| 915 | } |
| 916 | |
| 917 | static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb) |
| 918 | { |
| 919 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 920 | |
| 921 | BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND); |
| 922 | |
| 923 | device_want_to_sleep(hu); |
| 924 | |
| 925 | kfree_skb(skb); |
| 926 | return 0; |
| 927 | } |
| 928 | |
| 929 | static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb) |
| 930 | { |
| 931 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 932 | |
| 933 | BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND); |
| 934 | |
| 935 | device_want_to_wakeup(hu); |
| 936 | |
| 937 | kfree_skb(skb); |
| 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb) |
| 942 | { |
| 943 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 944 | |
| 945 | BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK); |
| 946 | |
| 947 | device_woke_up(hu); |
| 948 | |
| 949 | kfree_skb(skb); |
| 950 | return 0; |
| 951 | } |
| 952 | |
Balakrishna Godavarthi | c614ca3 | 2018-10-16 19:51:35 +0530 | [diff] [blame] | 953 | static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb) |
| 954 | { |
| 955 | /* We receive debug logs from chip as an ACL packets. |
| 956 | * Instead of sending the data to ACL to decode the |
| 957 | * received data, we are pushing them to the above layers |
| 958 | * as a diagnostic packet. |
| 959 | */ |
| 960 | if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE) |
| 961 | return hci_recv_diag(hdev, skb); |
| 962 | |
| 963 | return hci_recv_frame(hdev, skb); |
| 964 | } |
| 965 | |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 966 | static void qca_controller_memdump(struct work_struct *work) |
| 967 | { |
| 968 | struct qca_data *qca = container_of(work, struct qca_data, |
| 969 | ctrl_memdump_evt); |
| 970 | struct hci_uart *hu = qca->hu; |
| 971 | struct sk_buff *skb; |
| 972 | struct qca_memdump_event_hdr *cmd_hdr; |
| 973 | struct qca_memdump_data *qca_memdump = qca->qca_memdump; |
| 974 | struct qca_dump_size *dump; |
| 975 | char *memdump_buf; |
| 976 | char nullBuff[QCA_DUMP_PACKET_SIZE] = { 0 }; |
YueHaibing | 56b084e | 2020-01-08 01:54:31 +0000 | [diff] [blame] | 977 | u16 seq_no; |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 978 | u32 dump_size; |
| 979 | |
| 980 | while ((skb = skb_dequeue(&qca->rx_memdump_q))) { |
| 981 | |
Venkata Lakshmi Narayana Gubba | 7c2c3e6 | 2020-02-14 21:47:15 +0530 | [diff] [blame] | 982 | mutex_lock(&qca->hci_memdump_lock); |
| 983 | /* Skip processing the received packets if timeout detected. */ |
| 984 | if (qca->memdump_state == QCA_MEMDUMP_TIMEOUT) { |
| 985 | mutex_unlock(&qca->hci_memdump_lock); |
| 986 | return; |
| 987 | } |
| 988 | |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 989 | if (!qca_memdump) { |
| 990 | qca_memdump = kzalloc(sizeof(struct qca_memdump_data), |
| 991 | GFP_ATOMIC); |
Venkata Lakshmi Narayana Gubba | 7c2c3e6 | 2020-02-14 21:47:15 +0530 | [diff] [blame] | 992 | if (!qca_memdump) { |
| 993 | mutex_unlock(&qca->hci_memdump_lock); |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 994 | return; |
Venkata Lakshmi Narayana Gubba | 7c2c3e6 | 2020-02-14 21:47:15 +0530 | [diff] [blame] | 995 | } |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 996 | |
| 997 | qca->qca_memdump = qca_memdump; |
| 998 | } |
| 999 | |
| 1000 | qca->memdump_state = QCA_MEMDUMP_COLLECTING; |
| 1001 | cmd_hdr = (void *) skb->data; |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 1002 | seq_no = __le16_to_cpu(cmd_hdr->seq_no); |
| 1003 | skb_pull(skb, sizeof(struct qca_memdump_event_hdr)); |
| 1004 | |
| 1005 | if (!seq_no) { |
| 1006 | |
| 1007 | /* This is the first frame of memdump packet from |
| 1008 | * the controller, Disable IBS to recevie dump |
| 1009 | * with out any interruption, ideally time required for |
| 1010 | * the controller to send the dump is 8 seconds. let us |
| 1011 | * start timer to handle this asynchronous activity. |
| 1012 | */ |
| 1013 | clear_bit(QCA_IBS_ENABLED, &qca->flags); |
| 1014 | set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags); |
| 1015 | dump = (void *) skb->data; |
| 1016 | dump_size = __le32_to_cpu(dump->dump_size); |
| 1017 | if (!(dump_size)) { |
| 1018 | bt_dev_err(hu->hdev, "Rx invalid memdump size"); |
| 1019 | kfree_skb(skb); |
Venkata Lakshmi Narayana Gubba | 7c2c3e6 | 2020-02-14 21:47:15 +0530 | [diff] [blame] | 1020 | mutex_unlock(&qca->hci_memdump_lock); |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 1021 | return; |
| 1022 | } |
| 1023 | |
| 1024 | bt_dev_info(hu->hdev, "QCA collecting dump of size:%u", |
| 1025 | dump_size); |
Venkata Lakshmi Narayana Gubba | 7c2c3e6 | 2020-02-14 21:47:15 +0530 | [diff] [blame] | 1026 | queue_delayed_work(qca->workqueue, |
| 1027 | &qca->ctrl_memdump_timeout, |
| 1028 | msecs_to_jiffies(MEMDUMP_TIMEOUT_MS)); |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 1029 | |
| 1030 | skb_pull(skb, sizeof(dump_size)); |
| 1031 | memdump_buf = vmalloc(dump_size); |
| 1032 | qca_memdump->memdump_buf_head = memdump_buf; |
| 1033 | qca_memdump->memdump_buf_tail = memdump_buf; |
| 1034 | } |
| 1035 | |
| 1036 | memdump_buf = qca_memdump->memdump_buf_tail; |
| 1037 | |
| 1038 | /* If sequence no 0 is missed then there is no point in |
| 1039 | * accepting the other sequences. |
| 1040 | */ |
| 1041 | if (!memdump_buf) { |
| 1042 | bt_dev_err(hu->hdev, "QCA: Discarding other packets"); |
| 1043 | kfree(qca_memdump); |
| 1044 | kfree_skb(skb); |
| 1045 | qca->qca_memdump = NULL; |
Venkata Lakshmi Narayana Gubba | 7c2c3e6 | 2020-02-14 21:47:15 +0530 | [diff] [blame] | 1046 | mutex_unlock(&qca->hci_memdump_lock); |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 1047 | return; |
| 1048 | } |
| 1049 | |
| 1050 | /* There could be chance of missing some packets from |
| 1051 | * the controller. In such cases let us store the dummy |
| 1052 | * packets in the buffer. |
| 1053 | */ |
| 1054 | while ((seq_no > qca_memdump->current_seq_no + 1) && |
| 1055 | seq_no != QCA_LAST_SEQUENCE_NUM) { |
| 1056 | bt_dev_err(hu->hdev, "QCA controller missed packet:%d", |
| 1057 | qca_memdump->current_seq_no); |
| 1058 | memcpy(memdump_buf, nullBuff, QCA_DUMP_PACKET_SIZE); |
| 1059 | memdump_buf = memdump_buf + QCA_DUMP_PACKET_SIZE; |
| 1060 | qca_memdump->received_dump += QCA_DUMP_PACKET_SIZE; |
| 1061 | qca_memdump->current_seq_no++; |
| 1062 | } |
| 1063 | |
| 1064 | memcpy(memdump_buf, (unsigned char *) skb->data, skb->len); |
| 1065 | memdump_buf = memdump_buf + skb->len; |
| 1066 | qca_memdump->memdump_buf_tail = memdump_buf; |
| 1067 | qca_memdump->current_seq_no = seq_no + 1; |
| 1068 | qca_memdump->received_dump += skb->len; |
| 1069 | qca->qca_memdump = qca_memdump; |
| 1070 | kfree_skb(skb); |
| 1071 | if (seq_no == QCA_LAST_SEQUENCE_NUM) { |
| 1072 | bt_dev_info(hu->hdev, "QCA writing crash dump of size %d bytes", |
| 1073 | qca_memdump->received_dump); |
| 1074 | memdump_buf = qca_memdump->memdump_buf_head; |
| 1075 | dev_coredumpv(&hu->serdev->dev, memdump_buf, |
| 1076 | qca_memdump->received_dump, GFP_KERNEL); |
Venkata Lakshmi Narayana Gubba | 7c2c3e6 | 2020-02-14 21:47:15 +0530 | [diff] [blame] | 1077 | cancel_delayed_work(&qca->ctrl_memdump_timeout); |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 1078 | kfree(qca->qca_memdump); |
| 1079 | qca->qca_memdump = NULL; |
| 1080 | qca->memdump_state = QCA_MEMDUMP_COLLECTED; |
Venkata Lakshmi Narayana Gubba | 7c2c3e6 | 2020-02-14 21:47:15 +0530 | [diff] [blame] | 1081 | clear_bit(QCA_MEMDUMP_COLLECTION, &qca->flags); |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 1082 | } |
Venkata Lakshmi Narayana Gubba | 7c2c3e6 | 2020-02-14 21:47:15 +0530 | [diff] [blame] | 1083 | |
| 1084 | mutex_unlock(&qca->hci_memdump_lock); |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | } |
| 1088 | |
Venkata Lakshmi Narayana Gubba | 7c2c3e6 | 2020-02-14 21:47:15 +0530 | [diff] [blame] | 1089 | static int qca_controller_memdump_event(struct hci_dev *hdev, |
| 1090 | struct sk_buff *skb) |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 1091 | { |
| 1092 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 1093 | struct qca_data *qca = hu->priv; |
| 1094 | |
| 1095 | skb_queue_tail(&qca->rx_memdump_q, skb); |
| 1096 | queue_work(qca->workqueue, &qca->ctrl_memdump_evt); |
| 1097 | |
| 1098 | return 0; |
| 1099 | } |
| 1100 | |
Matthias Kaehlcke | 2faa3f1 | 2019-05-21 12:53:07 -0700 | [diff] [blame] | 1101 | static int qca_recv_event(struct hci_dev *hdev, struct sk_buff *skb) |
| 1102 | { |
| 1103 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 1104 | struct qca_data *qca = hu->priv; |
| 1105 | |
| 1106 | if (test_bit(QCA_DROP_VENDOR_EVENT, &qca->flags)) { |
| 1107 | struct hci_event_hdr *hdr = (void *)skb->data; |
| 1108 | |
| 1109 | /* For the WCN3990 the vendor command for a baudrate change |
| 1110 | * isn't sent as synchronous HCI command, because the |
| 1111 | * controller sends the corresponding vendor event with the |
| 1112 | * new baudrate. The event is received and properly decoded |
| 1113 | * after changing the baudrate of the host port. It needs to |
| 1114 | * be dropped, otherwise it can be misinterpreted as |
| 1115 | * response to a later firmware download command (also a |
| 1116 | * vendor command). |
| 1117 | */ |
| 1118 | |
| 1119 | if (hdr->evt == HCI_EV_VENDOR) |
| 1120 | complete(&qca->drop_ev_comp); |
| 1121 | |
Wei Yongjun | 4974c83 | 2019-07-09 01:35:30 +0000 | [diff] [blame] | 1122 | kfree_skb(skb); |
Matthias Kaehlcke | 2faa3f1 | 2019-05-21 12:53:07 -0700 | [diff] [blame] | 1123 | |
| 1124 | return 0; |
| 1125 | } |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 1126 | /* We receive chip memory dump as an event packet, With a dedicated |
| 1127 | * handler followed by a hardware error event. When this event is |
| 1128 | * received we store dump into a file before closing hci. This |
| 1129 | * dump will help in triaging the issues. |
| 1130 | */ |
| 1131 | if ((skb->data[0] == HCI_VENDOR_PKT) && |
| 1132 | (get_unaligned_be16(skb->data + 2) == QCA_SSR_DUMP_HANDLE)) |
| 1133 | return qca_controller_memdump_event(hdev, skb); |
Matthias Kaehlcke | 2faa3f1 | 2019-05-21 12:53:07 -0700 | [diff] [blame] | 1134 | |
| 1135 | return hci_recv_frame(hdev, skb); |
| 1136 | } |
| 1137 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1138 | #define QCA_IBS_SLEEP_IND_EVENT \ |
| 1139 | .type = HCI_IBS_SLEEP_IND, \ |
| 1140 | .hlen = 0, \ |
| 1141 | .loff = 0, \ |
| 1142 | .lsize = 0, \ |
| 1143 | .maxlen = HCI_MAX_IBS_SIZE |
| 1144 | |
| 1145 | #define QCA_IBS_WAKE_IND_EVENT \ |
| 1146 | .type = HCI_IBS_WAKE_IND, \ |
| 1147 | .hlen = 0, \ |
| 1148 | .loff = 0, \ |
| 1149 | .lsize = 0, \ |
| 1150 | .maxlen = HCI_MAX_IBS_SIZE |
| 1151 | |
| 1152 | #define QCA_IBS_WAKE_ACK_EVENT \ |
| 1153 | .type = HCI_IBS_WAKE_ACK, \ |
| 1154 | .hlen = 0, \ |
| 1155 | .loff = 0, \ |
| 1156 | .lsize = 0, \ |
| 1157 | .maxlen = HCI_MAX_IBS_SIZE |
| 1158 | |
| 1159 | static const struct h4_recv_pkt qca_recv_pkts[] = { |
Balakrishna Godavarthi | c614ca3 | 2018-10-16 19:51:35 +0530 | [diff] [blame] | 1160 | { H4_RECV_ACL, .recv = qca_recv_acl_data }, |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1161 | { H4_RECV_SCO, .recv = hci_recv_frame }, |
Matthias Kaehlcke | 2faa3f1 | 2019-05-21 12:53:07 -0700 | [diff] [blame] | 1162 | { H4_RECV_EVENT, .recv = qca_recv_event }, |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1163 | { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind }, |
| 1164 | { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack }, |
| 1165 | { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind }, |
| 1166 | }; |
| 1167 | |
| 1168 | static int qca_recv(struct hci_uart *hu, const void *data, int count) |
| 1169 | { |
| 1170 | struct qca_data *qca = hu->priv; |
| 1171 | |
| 1172 | if (!test_bit(HCI_UART_REGISTERED, &hu->flags)) |
| 1173 | return -EUNATCH; |
| 1174 | |
| 1175 | qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count, |
| 1176 | qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts)); |
| 1177 | if (IS_ERR(qca->rx_skb)) { |
| 1178 | int err = PTR_ERR(qca->rx_skb); |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 1179 | bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1180 | qca->rx_skb = NULL; |
| 1181 | return err; |
| 1182 | } |
| 1183 | |
| 1184 | return count; |
| 1185 | } |
| 1186 | |
| 1187 | static struct sk_buff *qca_dequeue(struct hci_uart *hu) |
| 1188 | { |
| 1189 | struct qca_data *qca = hu->priv; |
| 1190 | |
| 1191 | return skb_dequeue(&qca->txq); |
| 1192 | } |
| 1193 | |
| 1194 | static uint8_t qca_get_baudrate_value(int speed) |
| 1195 | { |
Prasanna Karthik | ce26d81 | 2015-09-15 12:19:45 +0000 | [diff] [blame] | 1196 | switch (speed) { |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1197 | case 9600: |
| 1198 | return QCA_BAUDRATE_9600; |
| 1199 | case 19200: |
| 1200 | return QCA_BAUDRATE_19200; |
| 1201 | case 38400: |
| 1202 | return QCA_BAUDRATE_38400; |
| 1203 | case 57600: |
| 1204 | return QCA_BAUDRATE_57600; |
| 1205 | case 115200: |
| 1206 | return QCA_BAUDRATE_115200; |
| 1207 | case 230400: |
| 1208 | return QCA_BAUDRATE_230400; |
| 1209 | case 460800: |
| 1210 | return QCA_BAUDRATE_460800; |
| 1211 | case 500000: |
| 1212 | return QCA_BAUDRATE_500000; |
| 1213 | case 921600: |
| 1214 | return QCA_BAUDRATE_921600; |
| 1215 | case 1000000: |
| 1216 | return QCA_BAUDRATE_1000000; |
| 1217 | case 2000000: |
| 1218 | return QCA_BAUDRATE_2000000; |
| 1219 | case 3000000: |
| 1220 | return QCA_BAUDRATE_3000000; |
Balakrishna Godavarthi | be93a49 | 2018-08-03 17:46:30 +0530 | [diff] [blame] | 1221 | case 3200000: |
| 1222 | return QCA_BAUDRATE_3200000; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1223 | case 3500000: |
| 1224 | return QCA_BAUDRATE_3500000; |
| 1225 | default: |
| 1226 | return QCA_BAUDRATE_115200; |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate) |
| 1231 | { |
| 1232 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 1233 | struct qca_data *qca = hu->priv; |
| 1234 | struct sk_buff *skb; |
| 1235 | u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 }; |
| 1236 | |
Balakrishna Godavarthi | be93a49 | 2018-08-03 17:46:30 +0530 | [diff] [blame] | 1237 | if (baudrate > QCA_BAUDRATE_3200000) |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1238 | return -EINVAL; |
| 1239 | |
| 1240 | cmd[4] = baudrate; |
| 1241 | |
Jia-Ju Bai | 25a13e38 | 2018-07-23 11:56:51 +0800 | [diff] [blame] | 1242 | skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1243 | if (!skb) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 1244 | bt_dev_err(hdev, "Failed to allocate baudrate packet"); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1245 | return -ENOMEM; |
| 1246 | } |
| 1247 | |
| 1248 | /* Assign commands to change baudrate and packet type. */ |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 1249 | skb_put_data(skb, cmd, sizeof(cmd)); |
Marcel Holtmann | 618e8bc | 2015-11-05 07:33:56 +0100 | [diff] [blame] | 1250 | hci_skb_pkt_type(skb) = HCI_COMMAND_PKT; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1251 | |
| 1252 | skb_queue_tail(&qca->txq, skb); |
| 1253 | hci_uart_tx_wakeup(hu); |
| 1254 | |
Matthias Kaehlcke | 94d6671 | 2019-02-27 15:52:23 -0800 | [diff] [blame] | 1255 | /* Wait for the baudrate change request to be sent */ |
| 1256 | |
| 1257 | while (!skb_queue_empty(&qca->txq)) |
| 1258 | usleep_range(100, 200); |
| 1259 | |
Matthias Kaehlcke | ecf2b76 | 2019-04-23 11:16:52 -0700 | [diff] [blame] | 1260 | if (hu->serdev) |
| 1261 | serdev_device_wait_until_sent(hu->serdev, |
Matthias Kaehlcke | 94d6671 | 2019-02-27 15:52:23 -0800 | [diff] [blame] | 1262 | msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS)); |
| 1263 | |
| 1264 | /* Give the controller time to process the request */ |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 1265 | if (qca_is_wcn399x(qca_soc_type(hu))) |
Matthias Kaehlcke | 94d6671 | 2019-02-27 15:52:23 -0800 | [diff] [blame] | 1266 | msleep(10); |
| 1267 | else |
| 1268 | msleep(300); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1269 | |
| 1270 | return 0; |
| 1271 | } |
| 1272 | |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1273 | static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed) |
| 1274 | { |
| 1275 | if (hu->serdev) |
| 1276 | serdev_device_set_baudrate(hu->serdev, speed); |
| 1277 | else |
| 1278 | hci_uart_set_baudrate(hu, speed); |
| 1279 | } |
| 1280 | |
Matthias Kaehlcke | 9836b80 | 2019-02-26 11:46:45 -0800 | [diff] [blame] | 1281 | static int qca_send_power_pulse(struct hci_uart *hu, bool on) |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1282 | { |
Balakrishna Godavarthi | f955827 | 2019-02-04 20:36:41 +0530 | [diff] [blame] | 1283 | int ret; |
Matthias Kaehlcke | 94d6671 | 2019-02-27 15:52:23 -0800 | [diff] [blame] | 1284 | int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS); |
Matthias Kaehlcke | 9836b80 | 2019-02-26 11:46:45 -0800 | [diff] [blame] | 1285 | u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1286 | |
| 1287 | /* These power pulses are single byte command which are sent |
| 1288 | * at required baudrate to wcn3990. On wcn3990, we have an external |
| 1289 | * circuit at Tx pin which decodes the pulse sent at specific baudrate. |
| 1290 | * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT |
| 1291 | * and also we use the same power inputs to turn on and off for |
| 1292 | * Wi-Fi/BT. Powering up the power sources will not enable BT, until |
| 1293 | * we send a power on pulse at 115200 bps. This algorithm will help to |
| 1294 | * save power. Disabling hardware flow control is mandatory while |
| 1295 | * sending power pulses to SoC. |
| 1296 | */ |
Balakrishna Godavarthi | f955827 | 2019-02-04 20:36:41 +0530 | [diff] [blame] | 1297 | bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1298 | |
Balakrishna Godavarthi | f955827 | 2019-02-04 20:36:41 +0530 | [diff] [blame] | 1299 | serdev_device_write_flush(hu->serdev); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1300 | hci_uart_set_flow_control(hu, true); |
Balakrishna Godavarthi | f955827 | 2019-02-04 20:36:41 +0530 | [diff] [blame] | 1301 | ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd)); |
| 1302 | if (ret < 0) { |
| 1303 | bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd); |
| 1304 | return ret; |
| 1305 | } |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1306 | |
Balakrishna Godavarthi | f955827 | 2019-02-04 20:36:41 +0530 | [diff] [blame] | 1307 | serdev_device_wait_until_sent(hu->serdev, timeout); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1308 | hci_uart_set_flow_control(hu, false); |
| 1309 | |
Matthias Kaehlcke | 0ebcddd | 2019-02-26 11:46:47 -0800 | [diff] [blame] | 1310 | /* Give to controller time to boot/shutdown */ |
Matthias Kaehlcke | ad571d7 | 2019-02-26 11:46:46 -0800 | [diff] [blame] | 1311 | if (on) |
| 1312 | msleep(100); |
Matthias Kaehlcke | 0ebcddd | 2019-02-26 11:46:47 -0800 | [diff] [blame] | 1313 | else |
| 1314 | msleep(10); |
Matthias Kaehlcke | ad571d7 | 2019-02-26 11:46:46 -0800 | [diff] [blame] | 1315 | |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1316 | return 0; |
| 1317 | } |
| 1318 | |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1319 | static unsigned int qca_get_speed(struct hci_uart *hu, |
| 1320 | enum qca_speed_type speed_type) |
| 1321 | { |
| 1322 | unsigned int speed = 0; |
| 1323 | |
| 1324 | if (speed_type == QCA_INIT_SPEED) { |
| 1325 | if (hu->init_speed) |
| 1326 | speed = hu->init_speed; |
| 1327 | else if (hu->proto->init_speed) |
| 1328 | speed = hu->proto->init_speed; |
| 1329 | } else { |
| 1330 | if (hu->oper_speed) |
| 1331 | speed = hu->oper_speed; |
| 1332 | else if (hu->proto->oper_speed) |
| 1333 | speed = hu->proto->oper_speed; |
| 1334 | } |
| 1335 | |
| 1336 | return speed; |
| 1337 | } |
| 1338 | |
| 1339 | static int qca_check_speeds(struct hci_uart *hu) |
| 1340 | { |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 1341 | if (qca_is_wcn399x(qca_soc_type(hu))) { |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1342 | if (!qca_get_speed(hu, QCA_INIT_SPEED) && |
| 1343 | !qca_get_speed(hu, QCA_OPER_SPEED)) |
| 1344 | return -EINVAL; |
| 1345 | } else { |
| 1346 | if (!qca_get_speed(hu, QCA_INIT_SPEED) || |
| 1347 | !qca_get_speed(hu, QCA_OPER_SPEED)) |
| 1348 | return -EINVAL; |
| 1349 | } |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1350 | |
| 1351 | return 0; |
| 1352 | } |
| 1353 | |
| 1354 | static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type) |
| 1355 | { |
| 1356 | unsigned int speed, qca_baudrate; |
Matthias Kaehlcke | 2faa3f1 | 2019-05-21 12:53:07 -0700 | [diff] [blame] | 1357 | struct qca_data *qca = hu->priv; |
Balakrishna Godavarthi | 78e8fa2 | 2019-02-04 20:36:42 +0530 | [diff] [blame] | 1358 | int ret = 0; |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1359 | |
| 1360 | if (speed_type == QCA_INIT_SPEED) { |
| 1361 | speed = qca_get_speed(hu, QCA_INIT_SPEED); |
| 1362 | if (speed) |
| 1363 | host_set_baudrate(hu, speed); |
| 1364 | } else { |
Matthias Kaehlcke | 4fdd5a4 | 2019-03-11 11:38:31 -0700 | [diff] [blame] | 1365 | enum qca_btsoc_type soc_type = qca_soc_type(hu); |
| 1366 | |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1367 | speed = qca_get_speed(hu, QCA_OPER_SPEED); |
| 1368 | if (!speed) |
| 1369 | return 0; |
| 1370 | |
Balakrishna Godavarthi | 78e8fa2 | 2019-02-04 20:36:42 +0530 | [diff] [blame] | 1371 | /* Disable flow control for wcn3990 to deassert RTS while |
| 1372 | * changing the baudrate of chip and host. |
| 1373 | */ |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 1374 | if (qca_is_wcn399x(soc_type)) |
Balakrishna Godavarthi | 78e8fa2 | 2019-02-04 20:36:42 +0530 | [diff] [blame] | 1375 | hci_uart_set_flow_control(hu, true); |
| 1376 | |
Matthias Kaehlcke | 2faa3f1 | 2019-05-21 12:53:07 -0700 | [diff] [blame] | 1377 | if (soc_type == QCA_WCN3990) { |
| 1378 | reinit_completion(&qca->drop_ev_comp); |
| 1379 | set_bit(QCA_DROP_VENDOR_EVENT, &qca->flags); |
| 1380 | } |
| 1381 | |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1382 | qca_baudrate = qca_get_baudrate_value(speed); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1383 | bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed); |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1384 | ret = qca_set_baudrate(hu->hdev, qca_baudrate); |
| 1385 | if (ret) |
Balakrishna Godavarthi | 78e8fa2 | 2019-02-04 20:36:42 +0530 | [diff] [blame] | 1386 | goto error; |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1387 | |
| 1388 | host_set_baudrate(hu, speed); |
Balakrishna Godavarthi | 78e8fa2 | 2019-02-04 20:36:42 +0530 | [diff] [blame] | 1389 | |
| 1390 | error: |
Jeffrey Hugo | bba79fe | 2019-10-21 07:18:27 -0700 | [diff] [blame] | 1391 | if (qca_is_wcn399x(soc_type)) |
Balakrishna Godavarthi | 78e8fa2 | 2019-02-04 20:36:42 +0530 | [diff] [blame] | 1392 | hci_uart_set_flow_control(hu, false); |
Matthias Kaehlcke | 2faa3f1 | 2019-05-21 12:53:07 -0700 | [diff] [blame] | 1393 | |
| 1394 | if (soc_type == QCA_WCN3990) { |
| 1395 | /* Wait for the controller to send the vendor event |
| 1396 | * for the baudrate change command. |
| 1397 | */ |
| 1398 | if (!wait_for_completion_timeout(&qca->drop_ev_comp, |
| 1399 | msecs_to_jiffies(100))) { |
| 1400 | bt_dev_err(hu->hdev, |
| 1401 | "Failed to change controller baudrate\n"); |
| 1402 | ret = -ETIMEDOUT; |
| 1403 | } |
| 1404 | |
| 1405 | clear_bit(QCA_DROP_VENDOR_EVENT, &qca->flags); |
| 1406 | } |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1407 | } |
| 1408 | |
Balakrishna Godavarthi | 78e8fa2 | 2019-02-04 20:36:42 +0530 | [diff] [blame] | 1409 | return ret; |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1410 | } |
| 1411 | |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 1412 | static int qca_send_crashbuffer(struct hci_uart *hu) |
| 1413 | { |
| 1414 | struct qca_data *qca = hu->priv; |
| 1415 | struct sk_buff *skb; |
| 1416 | |
| 1417 | skb = bt_skb_alloc(QCA_CRASHBYTE_PACKET_LEN, GFP_KERNEL); |
| 1418 | if (!skb) { |
| 1419 | bt_dev_err(hu->hdev, "Failed to allocate memory for skb packet"); |
| 1420 | return -ENOMEM; |
| 1421 | } |
| 1422 | |
| 1423 | /* We forcefully crash the controller, by sending 0xfb byte for |
| 1424 | * 1024 times. We also might have chance of losing data, To be |
| 1425 | * on safer side we send 1096 bytes to the SoC. |
| 1426 | */ |
| 1427 | memset(skb_put(skb, QCA_CRASHBYTE_PACKET_LEN), QCA_MEMDUMP_BYTE, |
| 1428 | QCA_CRASHBYTE_PACKET_LEN); |
| 1429 | hci_skb_pkt_type(skb) = HCI_COMMAND_PKT; |
| 1430 | bt_dev_info(hu->hdev, "crash the soc to collect controller dump"); |
| 1431 | skb_queue_tail(&qca->txq, skb); |
| 1432 | hci_uart_tx_wakeup(hu); |
| 1433 | |
| 1434 | return 0; |
| 1435 | } |
| 1436 | |
| 1437 | static void qca_wait_for_dump_collection(struct hci_dev *hdev) |
| 1438 | { |
| 1439 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 1440 | struct qca_data *qca = hu->priv; |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 1441 | |
| 1442 | wait_on_bit_timeout(&qca->flags, QCA_MEMDUMP_COLLECTION, |
| 1443 | TASK_UNINTERRUPTIBLE, MEMDUMP_TIMEOUT_MS); |
| 1444 | |
| 1445 | clear_bit(QCA_MEMDUMP_COLLECTION, &qca->flags); |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | static void qca_hw_error(struct hci_dev *hdev, u8 code) |
| 1449 | { |
| 1450 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 1451 | struct qca_data *qca = hu->priv; |
Venkata Lakshmi Narayana Gubba | 7c2c3e6 | 2020-02-14 21:47:15 +0530 | [diff] [blame] | 1452 | struct qca_memdump_data *qca_memdump = qca->qca_memdump; |
| 1453 | char *memdump_buf = NULL; |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 1454 | |
Venkata Lakshmi Narayana Gubba | 7c2c3e6 | 2020-02-14 21:47:15 +0530 | [diff] [blame] | 1455 | set_bit(QCA_HW_ERROR_EVENT, &qca->flags); |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 1456 | bt_dev_info(hdev, "mem_dump_status: %d", qca->memdump_state); |
| 1457 | |
| 1458 | if (qca->memdump_state == QCA_MEMDUMP_IDLE) { |
| 1459 | /* If hardware error event received for other than QCA |
| 1460 | * soc memory dump event, then we need to crash the SOC |
| 1461 | * and wait here for 8 seconds to get the dump packets. |
| 1462 | * This will block main thread to be on hold until we |
| 1463 | * collect dump. |
| 1464 | */ |
| 1465 | set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags); |
| 1466 | qca_send_crashbuffer(hu); |
| 1467 | qca_wait_for_dump_collection(hdev); |
| 1468 | } else if (qca->memdump_state == QCA_MEMDUMP_COLLECTING) { |
| 1469 | /* Let us wait here until memory dump collected or |
| 1470 | * memory dump timer expired. |
| 1471 | */ |
| 1472 | bt_dev_info(hdev, "waiting for dump to complete"); |
| 1473 | qca_wait_for_dump_collection(hdev); |
| 1474 | } |
Venkata Lakshmi Narayana Gubba | 7c2c3e6 | 2020-02-14 21:47:15 +0530 | [diff] [blame] | 1475 | |
| 1476 | if (qca->memdump_state != QCA_MEMDUMP_COLLECTED) { |
| 1477 | bt_dev_err(hu->hdev, "clearing allocated memory due to memdump timeout"); |
| 1478 | mutex_lock(&qca->hci_memdump_lock); |
| 1479 | if (qca_memdump) |
| 1480 | memdump_buf = qca_memdump->memdump_buf_head; |
| 1481 | vfree(memdump_buf); |
| 1482 | kfree(qca_memdump); |
| 1483 | qca->qca_memdump = NULL; |
| 1484 | qca->memdump_state = QCA_MEMDUMP_TIMEOUT; |
| 1485 | cancel_delayed_work(&qca->ctrl_memdump_timeout); |
| 1486 | skb_queue_purge(&qca->rx_memdump_q); |
| 1487 | mutex_unlock(&qca->hci_memdump_lock); |
| 1488 | cancel_work_sync(&qca->ctrl_memdump_evt); |
| 1489 | } |
| 1490 | |
| 1491 | clear_bit(QCA_HW_ERROR_EVENT, &qca->flags); |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | static void qca_cmd_timeout(struct hci_dev *hdev) |
| 1495 | { |
| 1496 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 1497 | struct qca_data *qca = hu->priv; |
| 1498 | |
| 1499 | if (qca->memdump_state == QCA_MEMDUMP_IDLE) |
| 1500 | qca_send_crashbuffer(hu); |
| 1501 | else |
| 1502 | bt_dev_info(hdev, "Dump collection is in process"); |
| 1503 | } |
| 1504 | |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1505 | static int qca_wcn3990_init(struct hci_uart *hu) |
| 1506 | { |
Balakrishna Godavarthi | 3e4be65 | 2018-09-24 20:14:45 +0530 | [diff] [blame] | 1507 | struct qca_serdev *qcadev; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1508 | int ret; |
| 1509 | |
Balakrishna Godavarthi | 3e4be65 | 2018-09-24 20:14:45 +0530 | [diff] [blame] | 1510 | /* Check for vregs status, may be hci down has turned |
| 1511 | * off the voltage regulator. |
| 1512 | */ |
| 1513 | qcadev = serdev_device_get_drvdata(hu->serdev); |
| 1514 | if (!qcadev->bt_power->vregs_on) { |
| 1515 | serdev_device_close(hu->serdev); |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 1516 | ret = qca_regulator_enable(qcadev); |
Balakrishna Godavarthi | 3e4be65 | 2018-09-24 20:14:45 +0530 | [diff] [blame] | 1517 | if (ret) |
| 1518 | return ret; |
| 1519 | |
| 1520 | ret = serdev_device_open(hu->serdev); |
| 1521 | if (ret) { |
| 1522 | bt_dev_err(hu->hdev, "failed to open port"); |
| 1523 | return ret; |
| 1524 | } |
| 1525 | } |
| 1526 | |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1527 | /* Forcefully enable wcn3990 to enter in to boot mode. */ |
| 1528 | host_set_baudrate(hu, 2400); |
Matthias Kaehlcke | 9836b80 | 2019-02-26 11:46:45 -0800 | [diff] [blame] | 1529 | ret = qca_send_power_pulse(hu, false); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1530 | if (ret) |
| 1531 | return ret; |
| 1532 | |
| 1533 | qca_set_speed(hu, QCA_INIT_SPEED); |
Matthias Kaehlcke | 9836b80 | 2019-02-26 11:46:45 -0800 | [diff] [blame] | 1534 | ret = qca_send_power_pulse(hu, true); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1535 | if (ret) |
| 1536 | return ret; |
| 1537 | |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1538 | /* Now the device is in ready state to communicate with host. |
| 1539 | * To sync host with device we need to reopen port. |
| 1540 | * Without this, we will have RTS and CTS synchronization |
| 1541 | * issues. |
| 1542 | */ |
| 1543 | serdev_device_close(hu->serdev); |
| 1544 | ret = serdev_device_open(hu->serdev); |
| 1545 | if (ret) { |
| 1546 | bt_dev_err(hu->hdev, "failed to open port"); |
| 1547 | return ret; |
| 1548 | } |
| 1549 | |
| 1550 | hci_uart_set_flow_control(hu, false); |
| 1551 | |
| 1552 | return 0; |
| 1553 | } |
| 1554 | |
Rocky Liao | 5e6d840 | 2020-01-13 12:30:20 +0800 | [diff] [blame] | 1555 | static int qca_power_on(struct hci_dev *hdev) |
| 1556 | { |
| 1557 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 1558 | enum qca_btsoc_type soc_type = qca_soc_type(hu); |
| 1559 | struct qca_serdev *qcadev; |
| 1560 | int ret = 0; |
| 1561 | |
| 1562 | /* Non-serdev device usually is powered by external power |
| 1563 | * and don't need additional action in driver for power on |
| 1564 | */ |
| 1565 | if (!hu->serdev) |
| 1566 | return 0; |
| 1567 | |
| 1568 | if (qca_is_wcn399x(soc_type)) { |
| 1569 | ret = qca_wcn3990_init(hu); |
| 1570 | } else { |
| 1571 | qcadev = serdev_device_get_drvdata(hu->serdev); |
Rocky Liao | 77131df | 2020-03-04 21:16:45 +0800 | [diff] [blame] | 1572 | if (qcadev->bt_en) { |
Rocky Liao | 8a208b2 | 2020-03-04 09:54:29 +0800 | [diff] [blame] | 1573 | gpiod_set_value_cansleep(qcadev->bt_en, 1); |
| 1574 | /* Controller needs time to bootup. */ |
| 1575 | msleep(150); |
| 1576 | } |
Rocky Liao | 5e6d840 | 2020-01-13 12:30:20 +0800 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | return ret; |
| 1580 | } |
| 1581 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1582 | static int qca_setup(struct hci_uart *hu) |
| 1583 | { |
| 1584 | struct hci_dev *hdev = hu->hdev; |
| 1585 | struct qca_data *qca = hu->priv; |
| 1586 | unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200; |
Rocky Liao | bb2500ab | 2020-01-15 16:55:51 +0800 | [diff] [blame] | 1587 | unsigned int retries = 0; |
Matthias Kaehlcke | 4fdd5a4 | 2019-03-11 11:38:31 -0700 | [diff] [blame] | 1588 | enum qca_btsoc_type soc_type = qca_soc_type(hu); |
Rocky Liao | 99c905c | 2019-06-06 17:40:30 +0800 | [diff] [blame] | 1589 | const char *firmware_name = qca_get_firmware_name(hu); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1590 | int ret; |
Balakrishna Godavarthi | aadebac | 2018-08-03 17:46:28 +0530 | [diff] [blame] | 1591 | int soc_ver = 0; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1592 | |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1593 | ret = qca_check_speeds(hu); |
| 1594 | if (ret) |
| 1595 | return ret; |
| 1596 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1597 | /* Patch downloading has to be done without IBS mode */ |
Matthias Kaehlcke | 62a9199 | 2019-04-29 16:21:30 -0700 | [diff] [blame] | 1598 | clear_bit(QCA_IBS_ENABLED, &qca->flags); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1599 | |
Rocky Liao | e14c167 | 2019-08-21 14:23:39 +0800 | [diff] [blame] | 1600 | /* Enable controller to do both LE scan and BR/EDR inquiry |
| 1601 | * simultaneously. |
| 1602 | */ |
| 1603 | set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks); |
| 1604 | |
Rocky Liao | 5e6d840 | 2020-01-13 12:30:20 +0800 | [diff] [blame] | 1605 | bt_dev_info(hdev, "setting up %s", |
Rocky Liao | e5d6468 | 2020-03-25 10:26:37 +0800 | [diff] [blame] | 1606 | qca_is_wcn399x(soc_type) ? "wcn399x" : "ROME/QCA6390"); |
Balakrishna Godavarthi | 3e4be65 | 2018-09-24 20:14:45 +0530 | [diff] [blame] | 1607 | |
Rocky Liao | bb2500ab | 2020-01-15 16:55:51 +0800 | [diff] [blame] | 1608 | retry: |
Rocky Liao | 5e6d840 | 2020-01-13 12:30:20 +0800 | [diff] [blame] | 1609 | ret = qca_power_on(hdev); |
| 1610 | if (ret) |
| 1611 | return ret; |
| 1612 | |
| 1613 | if (qca_is_wcn399x(soc_type)) { |
Matthias Kaehlcke | 5971752d | 2019-02-19 12:05:59 -0800 | [diff] [blame] | 1614 | set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1615 | |
Balakrishna Godavarthi | 7d250a0 | 2019-11-06 15:18:32 +0530 | [diff] [blame] | 1616 | ret = qca_read_soc_version(hdev, &soc_ver, soc_type); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1617 | if (ret) |
| 1618 | return ret; |
| 1619 | } else { |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1620 | qca_set_speed(hu, QCA_INIT_SPEED); |
| 1621 | } |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1622 | |
| 1623 | /* Setup user speed if needed */ |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1624 | speed = qca_get_speed(hu, QCA_OPER_SPEED); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1625 | if (speed) { |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1626 | ret = qca_set_speed(hu, QCA_OPER_SPEED); |
| 1627 | if (ret) |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1628 | return ret; |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1629 | |
| 1630 | qca_baudrate = qca_get_baudrate_value(speed); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1631 | } |
| 1632 | |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 1633 | if (!qca_is_wcn399x(soc_type)) { |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1634 | /* Get QCA version information */ |
Balakrishna Godavarthi | 7d250a0 | 2019-11-06 15:18:32 +0530 | [diff] [blame] | 1635 | ret = qca_read_soc_version(hdev, &soc_ver, soc_type); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1636 | if (ret) |
| 1637 | return ret; |
| 1638 | } |
Balakrishna Godavarthi | aadebac | 2018-08-03 17:46:28 +0530 | [diff] [blame] | 1639 | |
| 1640 | bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1641 | /* Setup patch / NVM configurations */ |
Rocky Liao | 99c905c | 2019-06-06 17:40:30 +0800 | [diff] [blame] | 1642 | ret = qca_uart_setup(hdev, qca_baudrate, soc_type, soc_ver, |
| 1643 | firmware_name); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1644 | if (!ret) { |
Matthias Kaehlcke | 62a9199 | 2019-04-29 16:21:30 -0700 | [diff] [blame] | 1645 | set_bit(QCA_IBS_ENABLED, &qca->flags); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1646 | qca_debugfs_init(hdev); |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 1647 | hu->hdev->hw_error = qca_hw_error; |
| 1648 | hu->hdev->cmd_timeout = qca_cmd_timeout; |
Loic Poulain | ba8f359 | 2017-11-06 12:16:56 +0100 | [diff] [blame] | 1649 | } else if (ret == -ENOENT) { |
| 1650 | /* No patch/nvm-config found, run with original fw/config */ |
| 1651 | ret = 0; |
Amit Pundir | 7dc5fe0 | 2018-04-16 12:10:24 +0530 | [diff] [blame] | 1652 | } else if (ret == -EAGAIN) { |
| 1653 | /* |
| 1654 | * Userspace firmware loader will return -EAGAIN in case no |
| 1655 | * patch/nvm-config is found, so run with original fw/config. |
| 1656 | */ |
| 1657 | ret = 0; |
Rocky Liao | bb2500ab | 2020-01-15 16:55:51 +0800 | [diff] [blame] | 1658 | } else { |
| 1659 | if (retries < MAX_INIT_RETRIES) { |
| 1660 | qca_power_shutdown(hu); |
| 1661 | if (hu->serdev) { |
| 1662 | serdev_device_close(hu->serdev); |
| 1663 | ret = serdev_device_open(hu->serdev); |
| 1664 | if (ret) { |
| 1665 | bt_dev_err(hdev, "failed to open port"); |
| 1666 | return ret; |
| 1667 | } |
| 1668 | } |
| 1669 | retries++; |
| 1670 | goto retry; |
| 1671 | } |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | /* Setup bdaddr */ |
Rocky Liao | e5d6468 | 2020-03-25 10:26:37 +0800 | [diff] [blame] | 1675 | if (soc_type == QCA_ROME) |
Balakrishna Godavarthi | 5c0a1001 | 2019-01-16 18:01:15 +0530 | [diff] [blame] | 1676 | hu->hdev->set_bdaddr = qca_set_bdaddr_rome; |
Rocky Liao | e5d6468 | 2020-03-25 10:26:37 +0800 | [diff] [blame] | 1677 | else |
| 1678 | hu->hdev->set_bdaddr = qca_set_bdaddr; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1679 | |
| 1680 | return ret; |
| 1681 | } |
| 1682 | |
Nishka Dasgupta | 2edc9c5 | 2019-08-15 11:21:49 +0530 | [diff] [blame] | 1683 | static const struct hci_uart_proto qca_proto = { |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1684 | .id = HCI_UART_QCA, |
| 1685 | .name = "QCA", |
Marcel Holtmann | aee61f7 | 2015-10-20 21:30:45 +0200 | [diff] [blame] | 1686 | .manufacturer = 29, |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1687 | .init_speed = 115200, |
| 1688 | .oper_speed = 3000000, |
| 1689 | .open = qca_open, |
| 1690 | .close = qca_close, |
| 1691 | .flush = qca_flush, |
| 1692 | .setup = qca_setup, |
| 1693 | .recv = qca_recv, |
| 1694 | .enqueue = qca_enqueue, |
| 1695 | .dequeue = qca_dequeue, |
| 1696 | }; |
| 1697 | |
Abhishek Pandit-Subedi | a228f7a | 2020-05-14 13:14:04 -0700 | [diff] [blame] | 1698 | static const struct qca_device_data qca_soc_data_wcn3990 = { |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1699 | .soc_type = QCA_WCN3990, |
| 1700 | .vregs = (struct qca_vreg []) { |
Bjorn Andersson | f2edd66 | 2019-10-17 22:24:02 -0700 | [diff] [blame] | 1701 | { "vddio", 15000 }, |
| 1702 | { "vddxo", 80000 }, |
| 1703 | { "vddrf", 300000 }, |
| 1704 | { "vddch0", 450000 }, |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1705 | }, |
| 1706 | .num_vregs = 4, |
| 1707 | }; |
| 1708 | |
Abhishek Pandit-Subedi | a228f7a | 2020-05-14 13:14:04 -0700 | [diff] [blame] | 1709 | static const struct qca_device_data qca_soc_data_wcn3991 = { |
Balakrishna Godavarthi | 7d250a0 | 2019-11-06 15:18:32 +0530 | [diff] [blame] | 1710 | .soc_type = QCA_WCN3991, |
| 1711 | .vregs = (struct qca_vreg []) { |
| 1712 | { "vddio", 15000 }, |
| 1713 | { "vddxo", 80000 }, |
| 1714 | { "vddrf", 300000 }, |
| 1715 | { "vddch0", 450000 }, |
| 1716 | }, |
| 1717 | .num_vregs = 4, |
Abhishek Pandit-Subedi | a228f7a | 2020-05-14 13:14:04 -0700 | [diff] [blame] | 1718 | .capabilities = QCA_CAP_WIDEBAND_SPEECH, |
Balakrishna Godavarthi | 7d250a0 | 2019-11-06 15:18:32 +0530 | [diff] [blame] | 1719 | }; |
| 1720 | |
Abhishek Pandit-Subedi | a228f7a | 2020-05-14 13:14:04 -0700 | [diff] [blame] | 1721 | static const struct qca_device_data qca_soc_data_wcn3998 = { |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 1722 | .soc_type = QCA_WCN3998, |
| 1723 | .vregs = (struct qca_vreg []) { |
Bjorn Andersson | f2edd66 | 2019-10-17 22:24:02 -0700 | [diff] [blame] | 1724 | { "vddio", 10000 }, |
| 1725 | { "vddxo", 80000 }, |
| 1726 | { "vddrf", 300000 }, |
| 1727 | { "vddch0", 450000 }, |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 1728 | }, |
| 1729 | .num_vregs = 4, |
| 1730 | }; |
| 1731 | |
Abhishek Pandit-Subedi | a228f7a | 2020-05-14 13:14:04 -0700 | [diff] [blame] | 1732 | static const struct qca_device_data qca_soc_data_qca6390 = { |
Rocky Liao | e5d6468 | 2020-03-25 10:26:37 +0800 | [diff] [blame] | 1733 | .soc_type = QCA_QCA6390, |
| 1734 | .num_vregs = 0, |
| 1735 | }; |
| 1736 | |
Balakrishna Godavarthi | c2d7827 | 2018-08-22 17:50:05 +0530 | [diff] [blame] | 1737 | static void qca_power_shutdown(struct hci_uart *hu) |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1738 | { |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 1739 | struct qca_serdev *qcadev; |
Balakrishna Godavarthi | 035a960 | 2019-02-04 20:36:43 +0530 | [diff] [blame] | 1740 | struct qca_data *qca = hu->priv; |
| 1741 | unsigned long flags; |
Rocky Liao | 5559904 | 2020-01-15 16:55:50 +0800 | [diff] [blame] | 1742 | enum qca_btsoc_type soc_type = qca_soc_type(hu); |
Balakrishna Godavarthi | 035a960 | 2019-02-04 20:36:43 +0530 | [diff] [blame] | 1743 | |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 1744 | qcadev = serdev_device_get_drvdata(hu->serdev); |
| 1745 | |
Balakrishna Godavarthi | 035a960 | 2019-02-04 20:36:43 +0530 | [diff] [blame] | 1746 | /* From this point we go into power off state. But serial port is |
| 1747 | * still open, stop queueing the IBS data and flush all the buffered |
| 1748 | * data in skb's. |
| 1749 | */ |
| 1750 | spin_lock_irqsave(&qca->hci_ibs_lock, flags); |
Matthias Kaehlcke | 62a9199 | 2019-04-29 16:21:30 -0700 | [diff] [blame] | 1751 | clear_bit(QCA_IBS_ENABLED, &qca->flags); |
Balakrishna Godavarthi | 035a960 | 2019-02-04 20:36:43 +0530 | [diff] [blame] | 1752 | qca_flush(hu); |
| 1753 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
| 1754 | |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 1755 | hu->hdev->hw_error = NULL; |
| 1756 | hu->hdev->cmd_timeout = NULL; |
Rocky Liao | 5559904 | 2020-01-15 16:55:50 +0800 | [diff] [blame] | 1757 | |
| 1758 | /* Non-serdev device usually is powered by external power |
| 1759 | * and don't need additional action in driver for power down |
| 1760 | */ |
| 1761 | if (!hu->serdev) |
| 1762 | return; |
| 1763 | |
| 1764 | if (qca_is_wcn399x(soc_type)) { |
| 1765 | host_set_baudrate(hu, 2400); |
| 1766 | qca_send_power_pulse(hu, false); |
| 1767 | qca_regulator_disable(qcadev); |
Rocky Liao | 77131df | 2020-03-04 21:16:45 +0800 | [diff] [blame] | 1768 | } else if (qcadev->bt_en) { |
Rocky Liao | 5559904 | 2020-01-15 16:55:50 +0800 | [diff] [blame] | 1769 | gpiod_set_value_cansleep(qcadev->bt_en, 0); |
| 1770 | } |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1771 | } |
| 1772 | |
Balakrishna Godavarthi | 3e4be65 | 2018-09-24 20:14:45 +0530 | [diff] [blame] | 1773 | static int qca_power_off(struct hci_dev *hdev) |
| 1774 | { |
| 1775 | struct hci_uart *hu = hci_get_drvdata(hdev); |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 1776 | struct qca_data *qca = hu->priv; |
Rocky Liao | 4f9ed5b | 2020-02-29 20:21:18 +0800 | [diff] [blame] | 1777 | enum qca_btsoc_type soc_type = qca_soc_type(hu); |
Balakrishna Godavarthi | 3e4be65 | 2018-09-24 20:14:45 +0530 | [diff] [blame] | 1778 | |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 1779 | /* Stop sending shutdown command if soc crashes. */ |
Rocky Liao | e5d6468 | 2020-03-25 10:26:37 +0800 | [diff] [blame] | 1780 | if (soc_type != QCA_ROME |
Rocky Liao | 4f9ed5b | 2020-02-29 20:21:18 +0800 | [diff] [blame] | 1781 | && qca->memdump_state == QCA_MEMDUMP_IDLE) { |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 1782 | qca_send_pre_shutdown_cmd(hdev); |
| 1783 | usleep_range(8000, 10000); |
| 1784 | } |
Harish Bandi | a278088 | 2019-07-12 10:39:40 +0530 | [diff] [blame] | 1785 | |
Balakrishna Godavarthi | d841502 | 2020-01-02 20:19:11 +0530 | [diff] [blame] | 1786 | qca->memdump_state = QCA_MEMDUMP_IDLE; |
Balakrishna Godavarthi | 3e4be65 | 2018-09-24 20:14:45 +0530 | [diff] [blame] | 1787 | qca_power_shutdown(hu); |
| 1788 | return 0; |
| 1789 | } |
| 1790 | |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 1791 | static int qca_regulator_enable(struct qca_serdev *qcadev) |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1792 | { |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 1793 | struct qca_power *power = qcadev->bt_power; |
| 1794 | int ret; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1795 | |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 1796 | /* Already enabled */ |
| 1797 | if (power->vregs_on) |
| 1798 | return 0; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1799 | |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 1800 | BT_DBG("enabling %d regulators)", power->num_vregs); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1801 | |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 1802 | ret = regulator_bulk_enable(power->num_vregs, power->vreg_bulk); |
| 1803 | if (ret) |
| 1804 | return ret; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1805 | |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 1806 | power->vregs_on = true; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1807 | |
Venkata Lakshmi Narayana Gubba | 66cb705 | 2020-02-03 16:10:40 +0530 | [diff] [blame] | 1808 | ret = clk_prepare_enable(qcadev->susclk); |
Venkata Lakshmi Narayana Gubba | f3d63f5 | 2020-02-05 16:21:43 +0530 | [diff] [blame] | 1809 | if (ret) |
Venkata Lakshmi Narayana Gubba | 66cb705 | 2020-02-03 16:10:40 +0530 | [diff] [blame] | 1810 | qca_regulator_disable(qcadev); |
Venkata Lakshmi Narayana Gubba | 66cb705 | 2020-02-03 16:10:40 +0530 | [diff] [blame] | 1811 | |
Venkata Lakshmi Narayana Gubba | f3d63f5 | 2020-02-05 16:21:43 +0530 | [diff] [blame] | 1812 | return ret; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1813 | } |
| 1814 | |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 1815 | static void qca_regulator_disable(struct qca_serdev *qcadev) |
| 1816 | { |
| 1817 | struct qca_power *power; |
| 1818 | |
| 1819 | if (!qcadev) |
| 1820 | return; |
| 1821 | |
| 1822 | power = qcadev->bt_power; |
| 1823 | |
| 1824 | /* Already disabled? */ |
| 1825 | if (!power->vregs_on) |
| 1826 | return; |
| 1827 | |
| 1828 | regulator_bulk_disable(power->num_vregs, power->vreg_bulk); |
| 1829 | power->vregs_on = false; |
Venkata Lakshmi Narayana Gubba | 66cb705 | 2020-02-03 16:10:40 +0530 | [diff] [blame] | 1830 | |
Venkata Lakshmi Narayana Gubba | f3d63f5 | 2020-02-05 16:21:43 +0530 | [diff] [blame] | 1831 | clk_disable_unprepare(qcadev->susclk); |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 1832 | } |
| 1833 | |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1834 | static int qca_init_regulators(struct qca_power *qca, |
| 1835 | const struct qca_vreg *vregs, size_t num_vregs) |
| 1836 | { |
Bjorn Andersson | c29ff10 | 2019-10-17 22:24:01 -0700 | [diff] [blame] | 1837 | struct regulator_bulk_data *bulk; |
| 1838 | int ret; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1839 | int i; |
| 1840 | |
Bjorn Andersson | c29ff10 | 2019-10-17 22:24:01 -0700 | [diff] [blame] | 1841 | bulk = devm_kcalloc(qca->dev, num_vregs, sizeof(*bulk), GFP_KERNEL); |
| 1842 | if (!bulk) |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1843 | return -ENOMEM; |
| 1844 | |
| 1845 | for (i = 0; i < num_vregs; i++) |
Bjorn Andersson | c29ff10 | 2019-10-17 22:24:01 -0700 | [diff] [blame] | 1846 | bulk[i].supply = vregs[i].name; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1847 | |
Bjorn Andersson | c29ff10 | 2019-10-17 22:24:01 -0700 | [diff] [blame] | 1848 | ret = devm_regulator_bulk_get(qca->dev, num_vregs, bulk); |
| 1849 | if (ret < 0) |
| 1850 | return ret; |
| 1851 | |
| 1852 | for (i = 0; i < num_vregs; i++) { |
| 1853 | ret = regulator_set_load(bulk[i].consumer, vregs[i].load_uA); |
| 1854 | if (ret) |
| 1855 | return ret; |
| 1856 | } |
| 1857 | |
| 1858 | qca->vreg_bulk = bulk; |
Bjorn Andersson | 163d42f | 2019-10-17 22:24:03 -0700 | [diff] [blame] | 1859 | qca->num_vregs = num_vregs; |
Bjorn Andersson | c29ff10 | 2019-10-17 22:24:01 -0700 | [diff] [blame] | 1860 | |
| 1861 | return 0; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1862 | } |
| 1863 | |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1864 | static int qca_serdev_probe(struct serdev_device *serdev) |
| 1865 | { |
| 1866 | struct qca_serdev *qcadev; |
Rocky Liao | ae56318 | 2020-01-16 11:22:54 +0800 | [diff] [blame] | 1867 | struct hci_dev *hdev; |
Abhishek Pandit-Subedi | a228f7a | 2020-05-14 13:14:04 -0700 | [diff] [blame] | 1868 | const struct qca_device_data *data; |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1869 | int err; |
Rocky Liao | 8a208b2 | 2020-03-04 09:54:29 +0800 | [diff] [blame] | 1870 | bool power_ctrl_enabled = true; |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1871 | |
| 1872 | qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL); |
| 1873 | if (!qcadev) |
| 1874 | return -ENOMEM; |
| 1875 | |
| 1876 | qcadev->serdev_hu.serdev = serdev; |
Rocky Liao | 9f3565b | 2019-12-13 16:50:45 +0800 | [diff] [blame] | 1877 | data = device_get_match_data(&serdev->dev); |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1878 | serdev_device_set_drvdata(serdev, qcadev); |
Rocky Liao | 99c905c | 2019-06-06 17:40:30 +0800 | [diff] [blame] | 1879 | device_property_read_string(&serdev->dev, "firmware-name", |
| 1880 | &qcadev->firmware_name); |
Christian Hewitt | 37aee13 | 2020-04-23 01:34:30 +0000 | [diff] [blame] | 1881 | device_property_read_u32(&serdev->dev, "max-speed", |
| 1882 | &qcadev->oper_speed); |
| 1883 | if (!qcadev->oper_speed) |
| 1884 | BT_DBG("UART will pick default operating speed"); |
| 1885 | |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 1886 | if (data && qca_is_wcn399x(data->soc_type)) { |
| 1887 | qcadev->btsoc_type = data->soc_type; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1888 | qcadev->bt_power = devm_kzalloc(&serdev->dev, |
| 1889 | sizeof(struct qca_power), |
| 1890 | GFP_KERNEL); |
| 1891 | if (!qcadev->bt_power) |
| 1892 | return -ENOMEM; |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1893 | |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1894 | qcadev->bt_power->dev = &serdev->dev; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1895 | err = qca_init_regulators(qcadev->bt_power, data->vregs, |
| 1896 | data->num_vregs); |
| 1897 | if (err) { |
| 1898 | BT_ERR("Failed to init regulators:%d", err); |
Rocky Liao | ae56318 | 2020-01-16 11:22:54 +0800 | [diff] [blame] | 1899 | return err; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | qcadev->bt_power->vregs_on = false; |
| 1903 | |
Venkata Lakshmi Narayana Gubba | 66cb705 | 2020-02-03 16:10:40 +0530 | [diff] [blame] | 1904 | qcadev->susclk = devm_clk_get_optional(&serdev->dev, NULL); |
| 1905 | if (IS_ERR(qcadev->susclk)) { |
| 1906 | dev_err(&serdev->dev, "failed to acquire clk\n"); |
| 1907 | return PTR_ERR(qcadev->susclk); |
| 1908 | } |
| 1909 | |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1910 | err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto); |
| 1911 | if (err) { |
| 1912 | BT_ERR("wcn3990 serdev registration failed"); |
Rocky Liao | ae56318 | 2020-01-16 11:22:54 +0800 | [diff] [blame] | 1913 | return err; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1914 | } |
| 1915 | } else { |
Rocky Liao | e5d6468 | 2020-03-25 10:26:37 +0800 | [diff] [blame] | 1916 | if (data) |
| 1917 | qcadev->btsoc_type = data->soc_type; |
| 1918 | else |
| 1919 | qcadev->btsoc_type = QCA_ROME; |
| 1920 | |
Rocky Liao | 77131df | 2020-03-04 21:16:45 +0800 | [diff] [blame] | 1921 | qcadev->bt_en = devm_gpiod_get_optional(&serdev->dev, "enable", |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1922 | GPIOD_OUT_LOW); |
Rocky Liao | 77131df | 2020-03-04 21:16:45 +0800 | [diff] [blame] | 1923 | if (!qcadev->bt_en) { |
Rocky Liao | 8a208b2 | 2020-03-04 09:54:29 +0800 | [diff] [blame] | 1924 | dev_warn(&serdev->dev, "failed to acquire enable gpio\n"); |
| 1925 | power_ctrl_enabled = false; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1926 | } |
| 1927 | |
Rocky Liao | 77131df | 2020-03-04 21:16:45 +0800 | [diff] [blame] | 1928 | qcadev->susclk = devm_clk_get_optional(&serdev->dev, NULL); |
| 1929 | if (!qcadev->susclk) { |
Rocky Liao | 8a208b2 | 2020-03-04 09:54:29 +0800 | [diff] [blame] | 1930 | dev_warn(&serdev->dev, "failed to acquire clk\n"); |
| 1931 | } else { |
| 1932 | err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ); |
| 1933 | if (err) |
| 1934 | return err; |
| 1935 | |
| 1936 | err = clk_prepare_enable(qcadev->susclk); |
| 1937 | if (err) |
| 1938 | return err; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1939 | } |
| 1940 | |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1941 | err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto); |
Rocky Liao | ae56318 | 2020-01-16 11:22:54 +0800 | [diff] [blame] | 1942 | if (err) { |
| 1943 | BT_ERR("Rome serdev registration failed"); |
Rocky Liao | 77131df | 2020-03-04 21:16:45 +0800 | [diff] [blame] | 1944 | if (qcadev->susclk) |
Rocky Liao | 8a208b2 | 2020-03-04 09:54:29 +0800 | [diff] [blame] | 1945 | clk_disable_unprepare(qcadev->susclk); |
Rocky Liao | ae56318 | 2020-01-16 11:22:54 +0800 | [diff] [blame] | 1946 | return err; |
| 1947 | } |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1948 | } |
| 1949 | |
Abhishek Pandit-Subedi | 85e90d9 | 2020-05-20 09:32:28 -0700 | [diff] [blame] | 1950 | hdev = qcadev->serdev_hu.hdev; |
| 1951 | |
Rocky Liao | 8a208b2 | 2020-03-04 09:54:29 +0800 | [diff] [blame] | 1952 | if (power_ctrl_enabled) { |
Rocky Liao | 8a208b2 | 2020-03-04 09:54:29 +0800 | [diff] [blame] | 1953 | set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks); |
| 1954 | hdev->shutdown = qca_power_off; |
| 1955 | } |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1956 | |
Abhishek Pandit-Subedi | a228f7a | 2020-05-14 13:14:04 -0700 | [diff] [blame] | 1957 | /* Wideband speech support must be set per driver since it can't be |
| 1958 | * queried via hci. |
| 1959 | */ |
| 1960 | if (data && (data->capabilities & QCA_CAP_WIDEBAND_SPEECH)) |
| 1961 | set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks); |
| 1962 | |
Rocky Liao | ae56318 | 2020-01-16 11:22:54 +0800 | [diff] [blame] | 1963 | return 0; |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1964 | } |
| 1965 | |
| 1966 | static void qca_serdev_remove(struct serdev_device *serdev) |
| 1967 | { |
| 1968 | struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev); |
| 1969 | |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 1970 | if (qca_is_wcn399x(qcadev->btsoc_type)) |
Balakrishna Godavarthi | c2d7827 | 2018-08-22 17:50:05 +0530 | [diff] [blame] | 1971 | qca_power_shutdown(&qcadev->serdev_hu); |
Rocky Liao | 77131df | 2020-03-04 21:16:45 +0800 | [diff] [blame] | 1972 | else if (qcadev->susclk) |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1973 | clk_disable_unprepare(qcadev->susclk); |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1974 | |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1975 | hci_uart_unregister_device(&qcadev->serdev_hu); |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1976 | } |
| 1977 | |
Zijun Hu | 7e7bbdd | 2020-05-29 21:56:57 +0800 | [diff] [blame^] | 1978 | static void qca_serdev_shutdown(struct device *dev) |
| 1979 | { |
| 1980 | int ret; |
| 1981 | int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS); |
| 1982 | struct serdev_device *serdev = to_serdev_device(dev); |
| 1983 | struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev); |
| 1984 | const u8 ibs_wake_cmd[] = { 0xFD }; |
| 1985 | const u8 edl_reset_soc_cmd[] = { 0x01, 0x00, 0xFC, 0x01, 0x05 }; |
| 1986 | |
| 1987 | if (qcadev->btsoc_type == QCA_QCA6390) { |
| 1988 | serdev_device_write_flush(serdev); |
| 1989 | ret = serdev_device_write_buf(serdev, ibs_wake_cmd, |
| 1990 | sizeof(ibs_wake_cmd)); |
| 1991 | if (ret < 0) { |
| 1992 | BT_ERR("QCA send IBS_WAKE_IND error: %d", ret); |
| 1993 | return; |
| 1994 | } |
| 1995 | serdev_device_wait_until_sent(serdev, timeout); |
| 1996 | usleep_range(8000, 10000); |
| 1997 | |
| 1998 | serdev_device_write_flush(serdev); |
| 1999 | ret = serdev_device_write_buf(serdev, edl_reset_soc_cmd, |
| 2000 | sizeof(edl_reset_soc_cmd)); |
| 2001 | if (ret < 0) { |
| 2002 | BT_ERR("QCA send EDL_RESET_REQ error: %d", ret); |
| 2003 | return; |
| 2004 | } |
| 2005 | serdev_device_wait_until_sent(serdev, timeout); |
| 2006 | usleep_range(8000, 10000); |
| 2007 | } |
| 2008 | } |
| 2009 | |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 2010 | static int __maybe_unused qca_suspend(struct device *dev) |
| 2011 | { |
Zijun Hu | feac90d | 2020-05-29 04:31:07 +0800 | [diff] [blame] | 2012 | struct serdev_device *serdev = to_serdev_device(dev); |
| 2013 | struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev); |
| 2014 | struct hci_uart *hu = &qcadev->serdev_hu; |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 2015 | struct qca_data *qca = hu->priv; |
| 2016 | unsigned long flags; |
| 2017 | int ret = 0; |
| 2018 | u8 cmd; |
| 2019 | |
| 2020 | set_bit(QCA_SUSPENDING, &qca->flags); |
| 2021 | |
| 2022 | /* Device is downloading patch or doesn't support in-band sleep. */ |
| 2023 | if (!test_bit(QCA_IBS_ENABLED, &qca->flags)) |
| 2024 | return 0; |
| 2025 | |
| 2026 | cancel_work_sync(&qca->ws_awake_device); |
| 2027 | cancel_work_sync(&qca->ws_awake_rx); |
| 2028 | |
| 2029 | spin_lock_irqsave_nested(&qca->hci_ibs_lock, |
| 2030 | flags, SINGLE_DEPTH_NESTING); |
| 2031 | |
| 2032 | switch (qca->tx_ibs_state) { |
| 2033 | case HCI_IBS_TX_WAKING: |
| 2034 | del_timer(&qca->wake_retrans_timer); |
| 2035 | /* Fall through */ |
| 2036 | case HCI_IBS_TX_AWAKE: |
| 2037 | del_timer(&qca->tx_idle_timer); |
| 2038 | |
| 2039 | serdev_device_write_flush(hu->serdev); |
| 2040 | cmd = HCI_IBS_SLEEP_IND; |
| 2041 | ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd)); |
| 2042 | |
| 2043 | if (ret < 0) { |
| 2044 | BT_ERR("Failed to send SLEEP to device"); |
| 2045 | break; |
| 2046 | } |
| 2047 | |
| 2048 | qca->tx_ibs_state = HCI_IBS_TX_ASLEEP; |
| 2049 | qca->ibs_sent_slps++; |
| 2050 | |
| 2051 | qca_wq_serial_tx_clock_vote_off(&qca->ws_tx_vote_off); |
| 2052 | break; |
| 2053 | |
| 2054 | case HCI_IBS_TX_ASLEEP: |
| 2055 | break; |
| 2056 | |
| 2057 | default: |
| 2058 | BT_ERR("Spurious tx state %d", qca->tx_ibs_state); |
| 2059 | ret = -EINVAL; |
| 2060 | break; |
| 2061 | } |
| 2062 | |
| 2063 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
| 2064 | |
| 2065 | if (ret < 0) |
| 2066 | goto error; |
| 2067 | |
| 2068 | serdev_device_wait_until_sent(hu->serdev, |
| 2069 | msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS)); |
| 2070 | |
| 2071 | /* Wait for HCI_IBS_SLEEP_IND sent by device to indicate its Tx is going |
| 2072 | * to sleep, so that the packet does not wake the system later. |
| 2073 | */ |
| 2074 | |
| 2075 | ret = wait_event_interruptible_timeout(qca->suspend_wait_q, |
| 2076 | qca->rx_ibs_state == HCI_IBS_RX_ASLEEP, |
| 2077 | msecs_to_jiffies(IBS_BTSOC_TX_IDLE_TIMEOUT_MS)); |
| 2078 | |
| 2079 | if (ret > 0) |
| 2080 | return 0; |
| 2081 | |
| 2082 | if (ret == 0) |
| 2083 | ret = -ETIMEDOUT; |
| 2084 | |
| 2085 | error: |
| 2086 | clear_bit(QCA_SUSPENDING, &qca->flags); |
| 2087 | |
| 2088 | return ret; |
| 2089 | } |
| 2090 | |
| 2091 | static int __maybe_unused qca_resume(struct device *dev) |
| 2092 | { |
Zijun Hu | feac90d | 2020-05-29 04:31:07 +0800 | [diff] [blame] | 2093 | struct serdev_device *serdev = to_serdev_device(dev); |
| 2094 | struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev); |
| 2095 | struct hci_uart *hu = &qcadev->serdev_hu; |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 2096 | struct qca_data *qca = hu->priv; |
| 2097 | |
| 2098 | clear_bit(QCA_SUSPENDING, &qca->flags); |
| 2099 | |
| 2100 | return 0; |
| 2101 | } |
| 2102 | |
| 2103 | static SIMPLE_DEV_PM_OPS(qca_pm_ops, qca_suspend, qca_resume); |
| 2104 | |
Rocky Liao | e5d6468 | 2020-03-25 10:26:37 +0800 | [diff] [blame] | 2105 | #ifdef CONFIG_OF |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 2106 | static const struct of_device_id qca_bluetooth_of_match[] = { |
| 2107 | { .compatible = "qcom,qca6174-bt" }, |
Rocky Liao | e5d6468 | 2020-03-25 10:26:37 +0800 | [diff] [blame] | 2108 | { .compatible = "qcom,qca6390-bt", .data = &qca_soc_data_qca6390}, |
Christian Hewitt | 31d4ab8 | 2020-04-23 01:34:29 +0000 | [diff] [blame] | 2109 | { .compatible = "qcom,qca9377-bt" }, |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 2110 | { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990}, |
Balakrishna Godavarthi | 7d250a0 | 2019-11-06 15:18:32 +0530 | [diff] [blame] | 2111 | { .compatible = "qcom,wcn3991-bt", .data = &qca_soc_data_wcn3991}, |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 2112 | { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998}, |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 2113 | { /* sentinel */ } |
| 2114 | }; |
| 2115 | MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match); |
Rocky Liao | e5d6468 | 2020-03-25 10:26:37 +0800 | [diff] [blame] | 2116 | #endif |
| 2117 | |
| 2118 | #ifdef CONFIG_ACPI |
| 2119 | static const struct acpi_device_id qca_bluetooth_acpi_match[] = { |
| 2120 | { "QCOM6390", (kernel_ulong_t)&qca_soc_data_qca6390 }, |
| 2121 | { "DLA16390", (kernel_ulong_t)&qca_soc_data_qca6390 }, |
| 2122 | { "DLB16390", (kernel_ulong_t)&qca_soc_data_qca6390 }, |
| 2123 | { "DLB26390", (kernel_ulong_t)&qca_soc_data_qca6390 }, |
| 2124 | { }, |
| 2125 | }; |
| 2126 | MODULE_DEVICE_TABLE(acpi, qca_bluetooth_acpi_match); |
| 2127 | #endif |
| 2128 | |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 2129 | |
| 2130 | static struct serdev_device_driver qca_serdev_driver = { |
| 2131 | .probe = qca_serdev_probe, |
| 2132 | .remove = qca_serdev_remove, |
| 2133 | .driver = { |
| 2134 | .name = "hci_uart_qca", |
Rocky Liao | e5d6468 | 2020-03-25 10:26:37 +0800 | [diff] [blame] | 2135 | .of_match_table = of_match_ptr(qca_bluetooth_of_match), |
| 2136 | .acpi_match_table = ACPI_PTR(qca_bluetooth_acpi_match), |
Zijun Hu | 7e7bbdd | 2020-05-29 21:56:57 +0800 | [diff] [blame^] | 2137 | .shutdown = qca_serdev_shutdown, |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 2138 | .pm = &qca_pm_ops, |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 2139 | }, |
| 2140 | }; |
| 2141 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 2142 | int __init qca_init(void) |
| 2143 | { |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 2144 | serdev_device_driver_register(&qca_serdev_driver); |
| 2145 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 2146 | return hci_uart_register_proto(&qca_proto); |
| 2147 | } |
| 2148 | |
| 2149 | int __exit qca_deinit(void) |
| 2150 | { |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 2151 | serdev_device_driver_unregister(&qca_serdev_driver); |
| 2152 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 2153 | return hci_uart_unregister_proto(&qca_proto); |
| 2154 | } |