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