blob: 53deea2eb7b4df3cfa54fcce37a7cc252c25e125 [file] [log] [blame]
Thomas Gleixner45051532019-05-29 16:57:47 -07001// SPDX-License-Identifier: GPL-2.0-only
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07002/*
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 Godavarthifa9ad872018-08-03 17:46:32 +05309 * Copyright (c) 2010, 2012, 2018 The Linux Foundation. All rights reserved.
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070010 *
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 Kim0ff252c2015-08-10 14:24:17 -070016 */
17
18#include <linux/kernel.h>
Thierry Escande05ba5332018-03-29 21:15:24 +020019#include <linux/clk.h>
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -070020#include <linux/completion.h>
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070021#include <linux/debugfs.h>
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +053022#include <linux/delay.h>
Balakrishna Godavarthid8415022020-01-02 20:19:11 +053023#include <linux/devcoredump.h>
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +053024#include <linux/device.h>
Thierry Escande05ba5332018-03-29 21:15:24 +020025#include <linux/gpio/consumer.h>
26#include <linux/mod_devicetable.h>
27#include <linux/module.h>
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +053028#include <linux/of_device.h>
Rocky Liaoe5d64682020-03-25 10:26:37 +080029#include <linux/acpi.h>
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +053030#include <linux/platform_device.h>
31#include <linux/regulator/consumer.h>
Thierry Escande05ba5332018-03-29 21:15:24 +020032#include <linux/serdev.h>
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +053033#include <linux/mutex.h>
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +053034#include <asm/unaligned.h>
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070035
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 Holtmannf81b0012015-08-30 23:05:32 +020046#define HCI_MAX_IBS_SIZE 10
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070047
Marcel Holtmannf81b0012015-08-30 23:05:32 +020048#define IBS_WAKE_RETRANS_TIMEOUT_MS 100
Balakrishna Godavarthi2d684762020-06-12 17:51:31 +053049#define IBS_BTSOC_TX_IDLE_TIMEOUT_MS 200
Claire Chang41d5b252019-10-31 18:46:14 +080050#define IBS_HOST_TX_IDLE_TIMEOUT_MS 2000
Matthias Kaehlcke94d66712019-02-27 15:52:23 -080051#define CMD_TRANS_TIMEOUT_MS 100
Balakrishna Godavarthid8415022020-01-02 20:19:11 +053052#define MEMDUMP_TIMEOUT_MS 8000
Venkata Lakshmi Narayana Gubbaad3a9c02020-12-30 22:47:08 +053053#define IBS_DISABLE_SSR_TIMEOUT_MS \
54 (MEMDUMP_TIMEOUT_MS + FW_DOWNLOAD_TIMEOUT_MS)
Venkata Lakshmi Narayana Gubba2be43ab2020-10-06 20:50:21 +053055#define FW_DOWNLOAD_TIMEOUT_MS 3000
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070056
Thierry Escande05ba5332018-03-29 21:15:24 +020057/* susclk rate */
58#define SUSCLK_RATE_32KHZ 32768
59
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +053060/* Controller debug log header */
61#define QCA_DEBUG_HANDLE 0x2EDC
62
Rocky Liaobb2500ab2020-01-15 16:55:51 +080063/* max retry count when init fails */
64#define MAX_INIT_RETRIES 3
65
Balakrishna Godavarthid8415022020-01-02 20:19:11 +053066/* Controller dump header */
67#define QCA_SSR_DUMP_HANDLE 0x0108
68#define QCA_DUMP_PACKET_SIZE 255
69#define QCA_LAST_SEQUENCE_NUM 0xFFFF
70#define QCA_CRASHBYTE_PACKET_LEN 1096
71#define QCA_MEMDUMP_BYTE 0xFB
72
Matthias Kaehlcke62a91992019-04-29 16:21:30 -070073enum qca_flags {
Venkata Lakshmi Narayana Gubba2be43ab2020-10-06 20:50:21 +053074 QCA_IBS_DISABLED,
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -070075 QCA_DROP_VENDOR_EVENT,
Claire Chang41d5b252019-10-31 18:46:14 +080076 QCA_SUSPENDING,
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +053077 QCA_MEMDUMP_COLLECTION,
Venkata Lakshmi Narayana Gubba33445372020-07-11 17:01:12 +053078 QCA_HW_ERROR_EVENT,
Venkata Lakshmi Narayana Gubba2be43ab2020-10-06 20:50:21 +053079 QCA_SSR_TRIGGERED,
Venkata Lakshmi Narayana Gubba55c0bd72021-02-05 21:07:16 +053080 QCA_BT_OFF,
81 QCA_ROM_FW
Matthias Kaehlcke62a91992019-04-29 16:21:30 -070082};
83
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -070084enum qca_capabilities {
85 QCA_CAP_WIDEBAND_SPEECH = BIT(0),
Abhishek Pandit-Subedi54780132020-09-30 13:01:38 -070086 QCA_CAP_VALID_LE_STATES = BIT(1),
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -070087};
Balakrishna Godavarthid8415022020-01-02 20:19:11 +053088
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070089/* HCI_IBS transmit side sleep protocol states */
90enum tx_ibs_states {
91 HCI_IBS_TX_ASLEEP,
92 HCI_IBS_TX_WAKING,
93 HCI_IBS_TX_AWAKE,
94};
95
96/* HCI_IBS receive side sleep protocol states */
97enum rx_states {
98 HCI_IBS_RX_ASLEEP,
99 HCI_IBS_RX_AWAKE,
100};
101
102/* HCI_IBS transmit and receive side clock state vote */
103enum hci_ibs_clock_state_vote {
104 HCI_IBS_VOTE_STATS_UPDATE,
105 HCI_IBS_TX_VOTE_CLOCK_ON,
106 HCI_IBS_TX_VOTE_CLOCK_OFF,
107 HCI_IBS_RX_VOTE_CLOCK_ON,
108 HCI_IBS_RX_VOTE_CLOCK_OFF,
109};
110
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530111/* Controller memory dump states */
112enum qca_memdump_states {
113 QCA_MEMDUMP_IDLE,
114 QCA_MEMDUMP_COLLECTING,
115 QCA_MEMDUMP_COLLECTED,
116 QCA_MEMDUMP_TIMEOUT,
117};
118
119struct qca_memdump_data {
120 char *memdump_buf_head;
121 char *memdump_buf_tail;
122 u32 current_seq_no;
123 u32 received_dump;
Zijun Hue5aeebd2020-05-29 22:38:31 +0800124 u32 ram_dump_size;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530125};
126
127struct qca_memdump_event_hdr {
128 __u8 evt;
129 __u8 plen;
130 __u16 opcode;
131 __u16 seq_no;
132 __u8 reserved;
133} __packed;
134
135
136struct qca_dump_size {
137 u32 dump_size;
138} __packed;
139
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700140struct qca_data {
141 struct hci_uart *hu;
142 struct sk_buff *rx_skb;
143 struct sk_buff_head txq;
144 struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530145 struct sk_buff_head rx_memdump_q; /* Memdump wait queue */
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700146 spinlock_t hci_ibs_lock; /* HCI_IBS state lock */
147 u8 tx_ibs_state; /* HCI_IBS transmit side power state*/
148 u8 rx_ibs_state; /* HCI_IBS receive side power state */
Viresh Kumar621a5f72015-09-26 15:04:07 -0700149 bool tx_vote; /* Clock must be on for TX */
150 bool rx_vote; /* Clock must be on for RX */
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700151 struct timer_list tx_idle_timer;
152 u32 tx_idle_delay;
153 struct timer_list wake_retrans_timer;
154 u32 wake_retrans;
155 struct workqueue_struct *workqueue;
156 struct work_struct ws_awake_rx;
157 struct work_struct ws_awake_device;
158 struct work_struct ws_rx_vote_off;
159 struct work_struct ws_tx_vote_off;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530160 struct work_struct ctrl_memdump_evt;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530161 struct delayed_work ctrl_memdump_timeout;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530162 struct qca_memdump_data *qca_memdump;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700163 unsigned long flags;
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -0700164 struct completion drop_ev_comp;
Claire Chang41d5b252019-10-31 18:46:14 +0800165 wait_queue_head_t suspend_wait_q;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530166 enum qca_memdump_states memdump_state;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530167 struct mutex hci_memdump_lock;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700168
169 /* For debugging purpose */
170 u64 ibs_sent_wacks;
171 u64 ibs_sent_slps;
172 u64 ibs_sent_wakes;
173 u64 ibs_recv_wacks;
174 u64 ibs_recv_slps;
175 u64 ibs_recv_wakes;
176 u64 vote_last_jif;
177 u32 vote_on_ms;
178 u32 vote_off_ms;
179 u64 tx_votes_on;
180 u64 rx_votes_on;
181 u64 tx_votes_off;
182 u64 rx_votes_off;
183 u64 votes_on;
184 u64 votes_off;
185};
186
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +0530187enum qca_speed_type {
188 QCA_INIT_SPEED = 1,
189 QCA_OPER_SPEED
190};
191
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530192/*
193 * Voltage regulator information required for configuring the
194 * QCA Bluetooth chipset
195 */
196struct qca_vreg {
197 const char *name;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530198 unsigned int load_uA;
199};
200
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -0700201struct qca_device_data {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530202 enum qca_btsoc_type soc_type;
203 struct qca_vreg *vregs;
204 size_t num_vregs;
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -0700205 uint32_t capabilities;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530206};
207
208/*
209 * Platform data for the QCA Bluetooth power driver.
210 */
211struct qca_power {
212 struct device *dev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530213 struct regulator_bulk_data *vreg_bulk;
Bjorn Andersson163d42f2019-10-17 22:24:03 -0700214 int num_vregs;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530215 bool vregs_on;
216};
217
Thierry Escande05ba5332018-03-29 21:15:24 +0200218struct qca_serdev {
219 struct hci_uart serdev_hu;
220 struct gpio_desc *bt_en;
Venkata Lakshmi Narayana Gubbad8f97da2021-05-18 22:04:42 +0530221 struct gpio_desc *sw_ctrl;
Thierry Escande05ba5332018-03-29 21:15:24 +0200222 struct clk *susclk;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530223 enum qca_btsoc_type btsoc_type;
224 struct qca_power *bt_power;
225 u32 init_speed;
226 u32 oper_speed;
Rocky Liao99c905c2019-06-06 17:40:30 +0800227 const char *firmware_name;
Thierry Escande05ba5332018-03-29 21:15:24 +0200228};
229
Bjorn Anderssona9314e72019-10-17 22:24:04 -0700230static int qca_regulator_enable(struct qca_serdev *qcadev);
231static void qca_regulator_disable(struct qca_serdev *qcadev);
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +0530232static void qca_power_shutdown(struct hci_uart *hu);
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +0530233static int qca_power_off(struct hci_dev *hdev);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530234static void qca_controller_memdump(struct work_struct *work);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530235
Matthias Kaehlcke4fdd5a42019-03-11 11:38:31 -0700236static enum qca_btsoc_type qca_soc_type(struct hci_uart *hu)
237{
238 enum qca_btsoc_type soc_type;
239
240 if (hu->serdev) {
241 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
242
243 soc_type = qsd->btsoc_type;
244 } else {
245 soc_type = QCA_ROME;
246 }
247
248 return soc_type;
249}
250
Rocky Liao99c905c2019-06-06 17:40:30 +0800251static const char *qca_get_firmware_name(struct hci_uart *hu)
252{
253 if (hu->serdev) {
254 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
255
256 return qsd->firmware_name;
257 } else {
258 return NULL;
259 }
260}
261
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700262static void __serial_clock_on(struct tty_struct *tty)
263{
264 /* TODO: Some chipset requires to enable UART clock on client
265 * side to save power consumption or manual work is required.
266 * Please put your code to control UART clock here if needed
267 */
268}
269
270static void __serial_clock_off(struct tty_struct *tty)
271{
272 /* TODO: Some chipset requires to disable UART clock on client
273 * side to save power consumption or manual work is required.
274 * Please put your code to control UART clock off here if needed
275 */
276}
277
278/* serial_clock_vote needs to be called with the ibs lock held */
279static void serial_clock_vote(unsigned long vote, struct hci_uart *hu)
280{
281 struct qca_data *qca = hu->priv;
282 unsigned int diff;
283
284 bool old_vote = (qca->tx_vote | qca->rx_vote);
285 bool new_vote;
286
287 switch (vote) {
288 case HCI_IBS_VOTE_STATS_UPDATE:
289 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
290
291 if (old_vote)
292 qca->vote_off_ms += diff;
293 else
294 qca->vote_on_ms += diff;
295 return;
296
297 case HCI_IBS_TX_VOTE_CLOCK_ON:
298 qca->tx_vote = true;
299 qca->tx_votes_on++;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700300 break;
301
302 case HCI_IBS_RX_VOTE_CLOCK_ON:
303 qca->rx_vote = true;
304 qca->rx_votes_on++;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700305 break;
306
307 case HCI_IBS_TX_VOTE_CLOCK_OFF:
308 qca->tx_vote = false;
309 qca->tx_votes_off++;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700310 break;
311
312 case HCI_IBS_RX_VOTE_CLOCK_OFF:
313 qca->rx_vote = false;
314 qca->rx_votes_off++;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700315 break;
316
317 default:
318 BT_ERR("Voting irregularity");
319 return;
320 }
321
Matthias Kaehlcke7310dd32020-06-06 09:53:06 -0700322 new_vote = qca->rx_vote | qca->tx_vote;
323
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700324 if (new_vote != old_vote) {
325 if (new_vote)
326 __serial_clock_on(hu->tty);
327 else
328 __serial_clock_off(hu->tty);
329
Prasanna Karthikce26d812015-09-15 12:19:45 +0000330 BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false",
331 vote ? "true" : "false");
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700332
333 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
334
335 if (new_vote) {
336 qca->votes_on++;
337 qca->vote_off_ms += diff;
338 } else {
339 qca->votes_off++;
340 qca->vote_on_ms += diff;
341 }
342 qca->vote_last_jif = jiffies;
343 }
344}
345
346/* Builds and sends an HCI_IBS command packet.
347 * These are very simple packets with only 1 cmd byte.
348 */
349static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
350{
351 int err = 0;
352 struct sk_buff *skb = NULL;
353 struct qca_data *qca = hu->priv;
354
355 BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd);
356
357 skb = bt_skb_alloc(1, GFP_ATOMIC);
358 if (!skb) {
359 BT_ERR("Failed to allocate memory for HCI_IBS packet");
360 return -ENOMEM;
361 }
362
363 /* Assign HCI_IBS type */
Johannes Berg634fef62017-06-16 14:29:24 +0200364 skb_put_u8(skb, cmd);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700365
366 skb_queue_tail(&qca->txq, skb);
367
368 return err;
369}
370
371static void qca_wq_awake_device(struct work_struct *work)
372{
373 struct qca_data *qca = container_of(work, struct qca_data,
374 ws_awake_device);
375 struct hci_uart *hu = qca->hu;
376 unsigned long retrans_delay;
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530377 unsigned long flags;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700378
379 BT_DBG("hu %p wq awake device", hu);
380
381 /* Vote for serial clock */
382 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
383
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530384 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700385
386 /* Send wake indication to device */
387 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
388 BT_ERR("Failed to send WAKE to device");
389
390 qca->ibs_sent_wakes++;
391
392 /* Start retransmit timer */
393 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
394 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
395
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530396 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700397
398 /* Actually send the packets */
399 hci_uart_tx_wakeup(hu);
400}
401
402static void qca_wq_awake_rx(struct work_struct *work)
403{
404 struct qca_data *qca = container_of(work, struct qca_data,
405 ws_awake_rx);
406 struct hci_uart *hu = qca->hu;
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530407 unsigned long flags;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700408
409 BT_DBG("hu %p wq awake rx", hu);
410
411 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
412
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530413 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700414 qca->rx_ibs_state = HCI_IBS_RX_AWAKE;
415
416 /* Always acknowledge device wake up,
417 * sending IBS message doesn't count as TX ON.
418 */
419 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
420 BT_ERR("Failed to acknowledge device wake up");
421
422 qca->ibs_sent_wacks++;
423
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530424 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700425
426 /* Actually send the packets */
427 hci_uart_tx_wakeup(hu);
428}
429
430static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work)
431{
432 struct qca_data *qca = container_of(work, struct qca_data,
433 ws_rx_vote_off);
434 struct hci_uart *hu = qca->hu;
435
436 BT_DBG("hu %p rx clock vote off", hu);
437
438 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
439}
440
441static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work)
442{
443 struct qca_data *qca = container_of(work, struct qca_data,
444 ws_tx_vote_off);
445 struct hci_uart *hu = qca->hu;
446
447 BT_DBG("hu %p tx clock vote off", hu);
448
449 /* Run HCI tx handling unlocked */
450 hci_uart_tx_wakeup(hu);
451
452 /* Now that message queued to tty driver, vote for tty clocks off.
453 * It is up to the tty driver to pend the clocks off until tx done.
454 */
455 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
456}
457
Kees Cook04356052017-10-04 17:54:29 -0700458static void hci_ibs_tx_idle_timeout(struct timer_list *t)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700459{
Kees Cook04356052017-10-04 17:54:29 -0700460 struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
461 struct hci_uart *hu = qca->hu;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700462 unsigned long flags;
463
464 BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state);
465
466 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
467 flags, SINGLE_DEPTH_NESTING);
468
469 switch (qca->tx_ibs_state) {
470 case HCI_IBS_TX_AWAKE:
471 /* TX_IDLE, go to SLEEP */
472 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
473 BT_ERR("Failed to send SLEEP to device");
474 break;
475 }
476 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
477 qca->ibs_sent_slps++;
478 queue_work(qca->workqueue, &qca->ws_tx_vote_off);
479 break;
480
481 case HCI_IBS_TX_ASLEEP:
482 case HCI_IBS_TX_WAKING:
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700483 default:
Colin Ian Kinge059a462017-02-17 19:58:10 +0000484 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700485 break;
486 }
487
488 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
489}
490
Kees Cook04356052017-10-04 17:54:29 -0700491static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700492{
Kees Cook04356052017-10-04 17:54:29 -0700493 struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
494 struct hci_uart *hu = qca->hu;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700495 unsigned long flags, retrans_delay;
Prasanna Karthika9137182015-09-28 08:03:24 +0000496 bool retransmit = false;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700497
498 BT_DBG("hu %p wake retransmit timeout in %d state",
499 hu, qca->tx_ibs_state);
500
501 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
502 flags, SINGLE_DEPTH_NESTING);
503
Claire Chang41d5b252019-10-31 18:46:14 +0800504 /* Don't retransmit the HCI_IBS_WAKE_IND when suspending. */
505 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
506 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
507 return;
508 }
509
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700510 switch (qca->tx_ibs_state) {
511 case HCI_IBS_TX_WAKING:
512 /* No WAKE_ACK, retransmit WAKE */
Prasanna Karthika9137182015-09-28 08:03:24 +0000513 retransmit = true;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700514 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
515 BT_ERR("Failed to acknowledge device wake up");
516 break;
517 }
518 qca->ibs_sent_wakes++;
519 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
520 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
521 break;
522
523 case HCI_IBS_TX_ASLEEP:
524 case HCI_IBS_TX_AWAKE:
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700525 default:
Colin Ian Kinge059a462017-02-17 19:58:10 +0000526 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700527 break;
528 }
529
530 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
531
532 if (retransmit)
533 hci_uart_tx_wakeup(hu);
534}
535
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530536
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530537static void qca_controller_memdump_timeout(struct work_struct *work)
538{
539 struct qca_data *qca = container_of(work, struct qca_data,
540 ctrl_memdump_timeout.work);
541 struct hci_uart *hu = qca->hu;
542
543 mutex_lock(&qca->hci_memdump_lock);
544 if (test_bit(QCA_MEMDUMP_COLLECTION, &qca->flags)) {
545 qca->memdump_state = QCA_MEMDUMP_TIMEOUT;
546 if (!test_bit(QCA_HW_ERROR_EVENT, &qca->flags)) {
547 /* Inject hw error event to reset the device
548 * and driver.
549 */
550 hci_reset_dev(hu->hdev);
551 }
552 }
553
554 mutex_unlock(&qca->hci_memdump_lock);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530555}
556
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530557
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700558/* Initialize protocol */
559static int qca_open(struct hci_uart *hu)
560{
Thierry Escande05ba5332018-03-29 21:15:24 +0200561 struct qca_serdev *qcadev;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700562 struct qca_data *qca;
563
564 BT_DBG("hu %p qca_open", hu);
565
Vladis Dronovb36a1552019-07-30 11:33:45 +0200566 if (!hci_uart_has_flow_control(hu))
567 return -EOPNOTSUPP;
568
Jia-Ju Bai25a13e382018-07-23 11:56:51 +0800569 qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700570 if (!qca)
571 return -ENOMEM;
572
573 skb_queue_head_init(&qca->txq);
574 skb_queue_head_init(&qca->tx_wait_q);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530575 skb_queue_head_init(&qca->rx_memdump_q);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700576 spin_lock_init(&qca->hci_ibs_lock);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530577 mutex_init(&qca->hci_memdump_lock);
Bhaktipriya Shridharfac9a602016-08-30 22:42:53 +0530578 qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700579 if (!qca->workqueue) {
580 BT_ERR("QCA Workqueue not initialized properly");
581 kfree(qca);
582 return -ENOMEM;
583 }
584
585 INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
586 INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
587 INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
588 INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530589 INIT_WORK(&qca->ctrl_memdump_evt, qca_controller_memdump);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530590 INIT_DELAYED_WORK(&qca->ctrl_memdump_timeout,
591 qca_controller_memdump_timeout);
Claire Chang41d5b252019-10-31 18:46:14 +0800592 init_waitqueue_head(&qca->suspend_wait_q);
593
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700594 qca->hu = hu;
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -0700595 init_completion(&qca->drop_ev_comp);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700596
597 /* Assume we start with both sides asleep -- extra wakes OK */
598 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
599 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
600
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700601 qca->vote_last_jif = jiffies;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700602
603 hu->priv = qca;
604
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530605 if (hu->serdev) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530606 qcadev = serdev_device_get_drvdata(hu->serdev);
Christian Hewitt37aee132020-04-23 01:34:30 +0000607
Venkata Lakshmi Narayana Gubbad8f97da2021-05-18 22:04:42 +0530608 if (qca_is_wcn399x(qcadev->btsoc_type) ||
609 qca_is_wcn6750(qcadev->btsoc_type))
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530610 hu->init_speed = qcadev->init_speed;
Christian Hewitt37aee132020-04-23 01:34:30 +0000611
612 if (qcadev->oper_speed)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530613 hu->oper_speed = qcadev->oper_speed;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530614 }
615
Kees Cook04356052017-10-04 17:54:29 -0700616 timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700617 qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
618
Kees Cook04356052017-10-04 17:54:29 -0700619 timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
Claire Chang41d5b252019-10-31 18:46:14 +0800620 qca->tx_idle_delay = IBS_HOST_TX_IDLE_TIMEOUT_MS;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700621
622 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
623 qca->tx_idle_delay, qca->wake_retrans);
624
625 return 0;
626}
627
628static void qca_debugfs_init(struct hci_dev *hdev)
629{
630 struct hci_uart *hu = hci_get_drvdata(hdev);
631 struct qca_data *qca = hu->priv;
632 struct dentry *ibs_dir;
633 umode_t mode;
634
635 if (!hdev->debugfs)
636 return;
637
638 ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
639
640 /* read only */
Nigel Christian99719442020-11-10 23:55:38 -0500641 mode = 0444;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700642 debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
643 debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
644 debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
645 &qca->ibs_sent_slps);
646 debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
647 &qca->ibs_sent_wakes);
648 debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
649 &qca->ibs_sent_wacks);
650 debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
651 &qca->ibs_recv_slps);
652 debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
653 &qca->ibs_recv_wakes);
654 debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
655 &qca->ibs_recv_wacks);
Ben YoungTae Kim10be6c02015-08-13 22:09:42 -0700656 debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700657 debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
658 debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
Ben YoungTae Kim10be6c02015-08-13 22:09:42 -0700659 debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700660 debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
661 debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
662 debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
663 debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
664 debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
665 debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
666
667 /* read/write */
Nigel Christian99719442020-11-10 23:55:38 -0500668 mode = 0644;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700669 debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
670 debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
671 &qca->tx_idle_delay);
672}
673
674/* Flush protocol data */
675static int qca_flush(struct hci_uart *hu)
676{
677 struct qca_data *qca = hu->priv;
678
679 BT_DBG("hu %p qca flush", hu);
680
681 skb_queue_purge(&qca->tx_wait_q);
682 skb_queue_purge(&qca->txq);
683
684 return 0;
685}
686
687/* Close protocol */
688static int qca_close(struct hci_uart *hu)
689{
690 struct qca_data *qca = hu->priv;
691
692 BT_DBG("hu %p qca close", hu);
693
694 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
695
696 skb_queue_purge(&qca->tx_wait_q);
697 skb_queue_purge(&qca->txq);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530698 skb_queue_purge(&qca->rx_memdump_q);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700699 del_timer(&qca->tx_idle_timer);
700 del_timer(&qca->wake_retrans_timer);
701 destroy_workqueue(qca->workqueue);
702 qca->hu = NULL;
703
704 kfree_skb(qca->rx_skb);
705
706 hu->priv = NULL;
707
708 kfree(qca);
709
710 return 0;
711}
712
713/* Called upon a wake-up-indication from the device.
714 */
715static void device_want_to_wakeup(struct hci_uart *hu)
716{
717 unsigned long flags;
718 struct qca_data *qca = hu->priv;
719
720 BT_DBG("hu %p want to wake up", hu);
721
722 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
723
724 qca->ibs_recv_wakes++;
725
Claire Chang41d5b252019-10-31 18:46:14 +0800726 /* Don't wake the rx up when suspending. */
727 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
728 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
729 return;
730 }
731
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700732 switch (qca->rx_ibs_state) {
733 case HCI_IBS_RX_ASLEEP:
734 /* Make sure clock is on - we may have turned clock off since
735 * receiving the wake up indicator awake rx clock.
736 */
737 queue_work(qca->workqueue, &qca->ws_awake_rx);
738 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
739 return;
740
741 case HCI_IBS_RX_AWAKE:
742 /* Always acknowledge device wake up,
743 * sending IBS message doesn't count as TX ON.
744 */
745 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
746 BT_ERR("Failed to acknowledge device wake up");
747 break;
748 }
749 qca->ibs_sent_wacks++;
750 break;
751
752 default:
753 /* Any other state is illegal */
754 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
755 qca->rx_ibs_state);
756 break;
757 }
758
759 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
760
761 /* Actually send the packets */
762 hci_uart_tx_wakeup(hu);
763}
764
765/* Called upon a sleep-indication from the device.
766 */
767static void device_want_to_sleep(struct hci_uart *hu)
768{
769 unsigned long flags;
770 struct qca_data *qca = hu->priv;
771
Rocky Liao6600c082019-08-14 15:42:39 +0800772 BT_DBG("hu %p want to sleep in %d state", hu, qca->rx_ibs_state);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700773
774 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
775
776 qca->ibs_recv_slps++;
777
778 switch (qca->rx_ibs_state) {
779 case HCI_IBS_RX_AWAKE:
780 /* Update state */
781 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
782 /* Vote off rx clock under workqueue */
783 queue_work(qca->workqueue, &qca->ws_rx_vote_off);
784 break;
785
786 case HCI_IBS_RX_ASLEEP:
Rocky Liao6600c082019-08-14 15:42:39 +0800787 break;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700788
789 default:
790 /* Any other state is illegal */
791 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
792 qca->rx_ibs_state);
793 break;
794 }
795
Claire Chang41d5b252019-10-31 18:46:14 +0800796 wake_up_interruptible(&qca->suspend_wait_q);
797
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700798 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
799}
800
801/* Called upon wake-up-acknowledgement from the device
802 */
803static void device_woke_up(struct hci_uart *hu)
804{
805 unsigned long flags, idle_delay;
806 struct qca_data *qca = hu->priv;
807 struct sk_buff *skb = NULL;
808
809 BT_DBG("hu %p woke up", hu);
810
811 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
812
813 qca->ibs_recv_wacks++;
814
Claire Chang41d5b252019-10-31 18:46:14 +0800815 /* Don't react to the wake-up-acknowledgment when suspending. */
816 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
817 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
818 return;
819 }
820
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700821 switch (qca->tx_ibs_state) {
822 case HCI_IBS_TX_AWAKE:
823 /* Expect one if we send 2 WAKEs */
824 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
825 qca->tx_ibs_state);
826 break;
827
828 case HCI_IBS_TX_WAKING:
829 /* Send pending packets */
830 while ((skb = skb_dequeue(&qca->tx_wait_q)))
831 skb_queue_tail(&qca->txq, skb);
832
833 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
834 del_timer(&qca->wake_retrans_timer);
835 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
836 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
837 qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
838 break;
839
840 case HCI_IBS_TX_ASLEEP:
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700841 default:
842 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
843 qca->tx_ibs_state);
844 break;
845 }
846
847 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
848
849 /* Actually send the packets */
850 hci_uart_tx_wakeup(hu);
851}
852
853/* Enqueue frame for transmittion (padding, crc, etc) may be called from
854 * two simultaneous tasklets.
855 */
856static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
857{
858 unsigned long flags = 0, idle_delay;
859 struct qca_data *qca = hu->priv;
860
861 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
862 qca->tx_ibs_state);
863
Venkata Lakshmi Narayana Gubba33445372020-07-11 17:01:12 +0530864 if (test_bit(QCA_SSR_TRIGGERED, &qca->flags)) {
865 /* As SSR is in progress, ignore the packets */
866 bt_dev_dbg(hu->hdev, "SSR is in progress");
867 kfree_skb(skb);
868 return 0;
869 }
870
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700871 /* Prepend skb with frame type */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100872 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700873
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +0530874 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
875
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700876 /* Don't go to sleep in middle of patch download or
877 * Out-Of-Band(GPIOs control) sleep is selected.
Claire Chang41d5b252019-10-31 18:46:14 +0800878 * Don't wake the device up when suspending.
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700879 */
Venkata Lakshmi Narayana Gubba2be43ab2020-10-06 20:50:21 +0530880 if (test_bit(QCA_IBS_DISABLED, &qca->flags) ||
Claire Chang41d5b252019-10-31 18:46:14 +0800881 test_bit(QCA_SUSPENDING, &qca->flags)) {
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700882 skb_queue_tail(&qca->txq, skb);
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +0530883 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700884 return 0;
885 }
886
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700887 /* Act according to current state */
888 switch (qca->tx_ibs_state) {
889 case HCI_IBS_TX_AWAKE:
890 BT_DBG("Device awake, sending normally");
891 skb_queue_tail(&qca->txq, skb);
892 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
893 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
894 break;
895
896 case HCI_IBS_TX_ASLEEP:
897 BT_DBG("Device asleep, waking up and queueing packet");
898 /* Save packet for later */
899 skb_queue_tail(&qca->tx_wait_q, skb);
900
901 qca->tx_ibs_state = HCI_IBS_TX_WAKING;
902 /* Schedule a work queue to wake up device */
903 queue_work(qca->workqueue, &qca->ws_awake_device);
904 break;
905
906 case HCI_IBS_TX_WAKING:
907 BT_DBG("Device waking up, queueing packet");
908 /* Transient state; just keep packet for later */
909 skb_queue_tail(&qca->tx_wait_q, skb);
910 break;
911
912 default:
913 BT_ERR("Illegal tx state: %d (losing packet)",
914 qca->tx_ibs_state);
915 kfree_skb(skb);
916 break;
917 }
918
919 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
920
921 return 0;
922}
923
924static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
925{
926 struct hci_uart *hu = hci_get_drvdata(hdev);
927
928 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
929
930 device_want_to_sleep(hu);
931
932 kfree_skb(skb);
933 return 0;
934}
935
936static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
937{
938 struct hci_uart *hu = hci_get_drvdata(hdev);
939
940 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
941
942 device_want_to_wakeup(hu);
943
944 kfree_skb(skb);
945 return 0;
946}
947
948static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
949{
950 struct hci_uart *hu = hci_get_drvdata(hdev);
951
952 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
953
954 device_woke_up(hu);
955
956 kfree_skb(skb);
957 return 0;
958}
959
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +0530960static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb)
961{
962 /* We receive debug logs from chip as an ACL packets.
963 * Instead of sending the data to ACL to decode the
964 * received data, we are pushing them to the above layers
965 * as a diagnostic packet.
966 */
967 if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE)
968 return hci_recv_diag(hdev, skb);
969
970 return hci_recv_frame(hdev, skb);
971}
972
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530973static void qca_controller_memdump(struct work_struct *work)
974{
975 struct qca_data *qca = container_of(work, struct qca_data,
976 ctrl_memdump_evt);
977 struct hci_uart *hu = qca->hu;
978 struct sk_buff *skb;
979 struct qca_memdump_event_hdr *cmd_hdr;
980 struct qca_memdump_data *qca_memdump = qca->qca_memdump;
981 struct qca_dump_size *dump;
982 char *memdump_buf;
983 char nullBuff[QCA_DUMP_PACKET_SIZE] = { 0 };
YueHaibing56b084e2020-01-08 01:54:31 +0000984 u16 seq_no;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530985 u32 dump_size;
Zijun Hue5aeebd2020-05-29 22:38:31 +0800986 u32 rx_size;
987 enum qca_btsoc_type soc_type = qca_soc_type(hu);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530988
989 while ((skb = skb_dequeue(&qca->rx_memdump_q))) {
990
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530991 mutex_lock(&qca->hci_memdump_lock);
Venkata Lakshmi Narayana Gubbaf98aa802020-06-09 21:27:08 +0530992 /* Skip processing the received packets if timeout detected
993 * or memdump collection completed.
994 */
995 if (qca->memdump_state == QCA_MEMDUMP_TIMEOUT ||
996 qca->memdump_state == QCA_MEMDUMP_COLLECTED) {
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530997 mutex_unlock(&qca->hci_memdump_lock);
998 return;
999 }
1000
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301001 if (!qca_memdump) {
1002 qca_memdump = kzalloc(sizeof(struct qca_memdump_data),
1003 GFP_ATOMIC);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301004 if (!qca_memdump) {
1005 mutex_unlock(&qca->hci_memdump_lock);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301006 return;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301007 }
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301008
1009 qca->qca_memdump = qca_memdump;
1010 }
1011
1012 qca->memdump_state = QCA_MEMDUMP_COLLECTING;
1013 cmd_hdr = (void *) skb->data;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301014 seq_no = __le16_to_cpu(cmd_hdr->seq_no);
1015 skb_pull(skb, sizeof(struct qca_memdump_event_hdr));
1016
1017 if (!seq_no) {
1018
1019 /* This is the first frame of memdump packet from
1020 * the controller, Disable IBS to recevie dump
1021 * with out any interruption, ideally time required for
1022 * the controller to send the dump is 8 seconds. let us
1023 * start timer to handle this asynchronous activity.
1024 */
Venkata Lakshmi Narayana Gubba2be43ab2020-10-06 20:50:21 +05301025 set_bit(QCA_IBS_DISABLED, &qca->flags);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301026 set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1027 dump = (void *) skb->data;
1028 dump_size = __le32_to_cpu(dump->dump_size);
1029 if (!(dump_size)) {
1030 bt_dev_err(hu->hdev, "Rx invalid memdump size");
Dinghao Liu71f8e702021-01-02 13:47:55 +08001031 kfree(qca_memdump);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301032 kfree_skb(skb);
Dinghao Liu71f8e702021-01-02 13:47:55 +08001033 qca->qca_memdump = NULL;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301034 mutex_unlock(&qca->hci_memdump_lock);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301035 return;
1036 }
1037
1038 bt_dev_info(hu->hdev, "QCA collecting dump of size:%u",
1039 dump_size);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301040 queue_delayed_work(qca->workqueue,
1041 &qca->ctrl_memdump_timeout,
Zijun Hue5aeebd2020-05-29 22:38:31 +08001042 msecs_to_jiffies(MEMDUMP_TIMEOUT_MS)
1043 );
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301044
1045 skb_pull(skb, sizeof(dump_size));
1046 memdump_buf = vmalloc(dump_size);
Zijun Hue5aeebd2020-05-29 22:38:31 +08001047 qca_memdump->ram_dump_size = dump_size;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301048 qca_memdump->memdump_buf_head = memdump_buf;
1049 qca_memdump->memdump_buf_tail = memdump_buf;
1050 }
1051
1052 memdump_buf = qca_memdump->memdump_buf_tail;
1053
1054 /* If sequence no 0 is missed then there is no point in
1055 * accepting the other sequences.
1056 */
1057 if (!memdump_buf) {
1058 bt_dev_err(hu->hdev, "QCA: Discarding other packets");
1059 kfree(qca_memdump);
1060 kfree_skb(skb);
1061 qca->qca_memdump = NULL;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301062 mutex_unlock(&qca->hci_memdump_lock);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301063 return;
1064 }
1065
1066 /* There could be chance of missing some packets from
1067 * the controller. In such cases let us store the dummy
1068 * packets in the buffer.
1069 */
Zijun Hue5aeebd2020-05-29 22:38:31 +08001070 /* For QCA6390, controller does not lost packets but
Bhaskar Chowdhury07528782021-03-22 06:30:51 +05301071 * sequence number field of packet sometimes has error
Zijun Hue5aeebd2020-05-29 22:38:31 +08001072 * bits, so skip this checking for missing packet.
1073 */
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301074 while ((seq_no > qca_memdump->current_seq_no + 1) &&
Zijun Hue5aeebd2020-05-29 22:38:31 +08001075 (soc_type != QCA_QCA6390) &&
1076 seq_no != QCA_LAST_SEQUENCE_NUM) {
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301077 bt_dev_err(hu->hdev, "QCA controller missed packet:%d",
1078 qca_memdump->current_seq_no);
Zijun Hue5aeebd2020-05-29 22:38:31 +08001079 rx_size = qca_memdump->received_dump;
1080 rx_size += QCA_DUMP_PACKET_SIZE;
1081 if (rx_size > qca_memdump->ram_dump_size) {
1082 bt_dev_err(hu->hdev,
1083 "QCA memdump received %d, no space for missed packet",
1084 qca_memdump->received_dump);
1085 break;
1086 }
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301087 memcpy(memdump_buf, nullBuff, QCA_DUMP_PACKET_SIZE);
1088 memdump_buf = memdump_buf + QCA_DUMP_PACKET_SIZE;
1089 qca_memdump->received_dump += QCA_DUMP_PACKET_SIZE;
1090 qca_memdump->current_seq_no++;
1091 }
1092
Zijun Hue5aeebd2020-05-29 22:38:31 +08001093 rx_size = qca_memdump->received_dump + skb->len;
1094 if (rx_size <= qca_memdump->ram_dump_size) {
1095 if ((seq_no != QCA_LAST_SEQUENCE_NUM) &&
1096 (seq_no != qca_memdump->current_seq_no))
1097 bt_dev_err(hu->hdev,
1098 "QCA memdump unexpected packet %d",
1099 seq_no);
1100 bt_dev_dbg(hu->hdev,
1101 "QCA memdump packet %d with length %d",
1102 seq_no, skb->len);
1103 memcpy(memdump_buf, (unsigned char *)skb->data,
1104 skb->len);
1105 memdump_buf = memdump_buf + skb->len;
1106 qca_memdump->memdump_buf_tail = memdump_buf;
1107 qca_memdump->current_seq_no = seq_no + 1;
1108 qca_memdump->received_dump += skb->len;
1109 } else {
1110 bt_dev_err(hu->hdev,
1111 "QCA memdump received %d, no space for packet %d",
1112 qca_memdump->received_dump, seq_no);
1113 }
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301114 qca->qca_memdump = qca_memdump;
1115 kfree_skb(skb);
1116 if (seq_no == QCA_LAST_SEQUENCE_NUM) {
Zijun Hue5aeebd2020-05-29 22:38:31 +08001117 bt_dev_info(hu->hdev,
1118 "QCA memdump Done, received %d, total %d",
1119 qca_memdump->received_dump,
1120 qca_memdump->ram_dump_size);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301121 memdump_buf = qca_memdump->memdump_buf_head;
1122 dev_coredumpv(&hu->serdev->dev, memdump_buf,
1123 qca_memdump->received_dump, GFP_KERNEL);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301124 cancel_delayed_work(&qca->ctrl_memdump_timeout);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301125 kfree(qca->qca_memdump);
1126 qca->qca_memdump = NULL;
1127 qca->memdump_state = QCA_MEMDUMP_COLLECTED;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301128 clear_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301129 }
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301130
1131 mutex_unlock(&qca->hci_memdump_lock);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301132 }
1133
1134}
1135
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301136static int qca_controller_memdump_event(struct hci_dev *hdev,
1137 struct sk_buff *skb)
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301138{
1139 struct hci_uart *hu = hci_get_drvdata(hdev);
1140 struct qca_data *qca = hu->priv;
1141
Venkata Lakshmi Narayana Gubba33445372020-07-11 17:01:12 +05301142 set_bit(QCA_SSR_TRIGGERED, &qca->flags);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301143 skb_queue_tail(&qca->rx_memdump_q, skb);
1144 queue_work(qca->workqueue, &qca->ctrl_memdump_evt);
1145
1146 return 0;
1147}
1148
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001149static int qca_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
1150{
1151 struct hci_uart *hu = hci_get_drvdata(hdev);
1152 struct qca_data *qca = hu->priv;
1153
1154 if (test_bit(QCA_DROP_VENDOR_EVENT, &qca->flags)) {
1155 struct hci_event_hdr *hdr = (void *)skb->data;
1156
1157 /* For the WCN3990 the vendor command for a baudrate change
1158 * isn't sent as synchronous HCI command, because the
1159 * controller sends the corresponding vendor event with the
1160 * new baudrate. The event is received and properly decoded
1161 * after changing the baudrate of the host port. It needs to
1162 * be dropped, otherwise it can be misinterpreted as
1163 * response to a later firmware download command (also a
1164 * vendor command).
1165 */
1166
1167 if (hdr->evt == HCI_EV_VENDOR)
1168 complete(&qca->drop_ev_comp);
1169
Wei Yongjun4974c832019-07-09 01:35:30 +00001170 kfree_skb(skb);
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001171
1172 return 0;
1173 }
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301174 /* We receive chip memory dump as an event packet, With a dedicated
1175 * handler followed by a hardware error event. When this event is
1176 * received we store dump into a file before closing hci. This
1177 * dump will help in triaging the issues.
1178 */
1179 if ((skb->data[0] == HCI_VENDOR_PKT) &&
1180 (get_unaligned_be16(skb->data + 2) == QCA_SSR_DUMP_HANDLE))
1181 return qca_controller_memdump_event(hdev, skb);
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001182
1183 return hci_recv_frame(hdev, skb);
1184}
1185
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001186#define QCA_IBS_SLEEP_IND_EVENT \
1187 .type = HCI_IBS_SLEEP_IND, \
1188 .hlen = 0, \
1189 .loff = 0, \
1190 .lsize = 0, \
1191 .maxlen = HCI_MAX_IBS_SIZE
1192
1193#define QCA_IBS_WAKE_IND_EVENT \
1194 .type = HCI_IBS_WAKE_IND, \
1195 .hlen = 0, \
1196 .loff = 0, \
1197 .lsize = 0, \
1198 .maxlen = HCI_MAX_IBS_SIZE
1199
1200#define QCA_IBS_WAKE_ACK_EVENT \
1201 .type = HCI_IBS_WAKE_ACK, \
1202 .hlen = 0, \
1203 .loff = 0, \
1204 .lsize = 0, \
1205 .maxlen = HCI_MAX_IBS_SIZE
1206
1207static const struct h4_recv_pkt qca_recv_pkts[] = {
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +05301208 { H4_RECV_ACL, .recv = qca_recv_acl_data },
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001209 { H4_RECV_SCO, .recv = hci_recv_frame },
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001210 { H4_RECV_EVENT, .recv = qca_recv_event },
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001211 { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind },
1212 { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack },
1213 { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
1214};
1215
1216static int qca_recv(struct hci_uart *hu, const void *data, int count)
1217{
1218 struct qca_data *qca = hu->priv;
1219
1220 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
1221 return -EUNATCH;
1222
1223 qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
1224 qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
1225 if (IS_ERR(qca->rx_skb)) {
1226 int err = PTR_ERR(qca->rx_skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001227 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001228 qca->rx_skb = NULL;
1229 return err;
1230 }
1231
1232 return count;
1233}
1234
1235static struct sk_buff *qca_dequeue(struct hci_uart *hu)
1236{
1237 struct qca_data *qca = hu->priv;
1238
1239 return skb_dequeue(&qca->txq);
1240}
1241
1242static uint8_t qca_get_baudrate_value(int speed)
1243{
Prasanna Karthikce26d812015-09-15 12:19:45 +00001244 switch (speed) {
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001245 case 9600:
1246 return QCA_BAUDRATE_9600;
1247 case 19200:
1248 return QCA_BAUDRATE_19200;
1249 case 38400:
1250 return QCA_BAUDRATE_38400;
1251 case 57600:
1252 return QCA_BAUDRATE_57600;
1253 case 115200:
1254 return QCA_BAUDRATE_115200;
1255 case 230400:
1256 return QCA_BAUDRATE_230400;
1257 case 460800:
1258 return QCA_BAUDRATE_460800;
1259 case 500000:
1260 return QCA_BAUDRATE_500000;
1261 case 921600:
1262 return QCA_BAUDRATE_921600;
1263 case 1000000:
1264 return QCA_BAUDRATE_1000000;
1265 case 2000000:
1266 return QCA_BAUDRATE_2000000;
1267 case 3000000:
1268 return QCA_BAUDRATE_3000000;
Balakrishna Godavarthibe93a492018-08-03 17:46:30 +05301269 case 3200000:
1270 return QCA_BAUDRATE_3200000;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001271 case 3500000:
1272 return QCA_BAUDRATE_3500000;
1273 default:
1274 return QCA_BAUDRATE_115200;
1275 }
1276}
1277
1278static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
1279{
1280 struct hci_uart *hu = hci_get_drvdata(hdev);
1281 struct qca_data *qca = hu->priv;
1282 struct sk_buff *skb;
1283 u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
1284
Balakrishna Godavarthibe93a492018-08-03 17:46:30 +05301285 if (baudrate > QCA_BAUDRATE_3200000)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001286 return -EINVAL;
1287
1288 cmd[4] = baudrate;
1289
Jia-Ju Bai25a13e382018-07-23 11:56:51 +08001290 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001291 if (!skb) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001292 bt_dev_err(hdev, "Failed to allocate baudrate packet");
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001293 return -ENOMEM;
1294 }
1295
1296 /* Assign commands to change baudrate and packet type. */
Johannes Berg59ae1d12017-06-16 14:29:20 +02001297 skb_put_data(skb, cmd, sizeof(cmd));
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001298 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001299
1300 skb_queue_tail(&qca->txq, skb);
1301 hci_uart_tx_wakeup(hu);
1302
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001303 /* Wait for the baudrate change request to be sent */
1304
1305 while (!skb_queue_empty(&qca->txq))
1306 usleep_range(100, 200);
1307
Matthias Kaehlckeecf2b762019-04-23 11:16:52 -07001308 if (hu->serdev)
1309 serdev_device_wait_until_sent(hu->serdev,
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001310 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1311
1312 /* Give the controller time to process the request */
Venkata Lakshmi Narayana Gubbad8f97da2021-05-18 22:04:42 +05301313 if (qca_is_wcn399x(qca_soc_type(hu)) ||
1314 qca_is_wcn6750(qca_soc_type(hu)))
Nigel Christian99719442020-11-10 23:55:38 -05001315 usleep_range(1000, 10000);
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001316 else
1317 msleep(300);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001318
1319 return 0;
1320}
1321
Thierry Escande05ba5332018-03-29 21:15:24 +02001322static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
1323{
1324 if (hu->serdev)
1325 serdev_device_set_baudrate(hu->serdev, speed);
1326 else
1327 hci_uart_set_baudrate(hu, speed);
1328}
1329
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001330static int qca_send_power_pulse(struct hci_uart *hu, bool on)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301331{
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301332 int ret;
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001333 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001334 u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301335
1336 /* These power pulses are single byte command which are sent
1337 * at required baudrate to wcn3990. On wcn3990, we have an external
1338 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1339 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1340 * and also we use the same power inputs to turn on and off for
1341 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1342 * we send a power on pulse at 115200 bps. This algorithm will help to
1343 * save power. Disabling hardware flow control is mandatory while
1344 * sending power pulses to SoC.
1345 */
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301346 bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301347
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301348 serdev_device_write_flush(hu->serdev);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301349 hci_uart_set_flow_control(hu, true);
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301350 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1351 if (ret < 0) {
1352 bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd);
1353 return ret;
1354 }
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301355
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301356 serdev_device_wait_until_sent(hu->serdev, timeout);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301357 hci_uart_set_flow_control(hu, false);
1358
Matthias Kaehlcke0ebcddd2019-02-26 11:46:47 -08001359 /* Give to controller time to boot/shutdown */
Matthias Kaehlckead571d72019-02-26 11:46:46 -08001360 if (on)
1361 msleep(100);
Matthias Kaehlcke0ebcddd2019-02-26 11:46:47 -08001362 else
Nigel Christian99719442020-11-10 23:55:38 -05001363 usleep_range(1000, 10000);
Matthias Kaehlckead571d72019-02-26 11:46:46 -08001364
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301365 return 0;
1366}
1367
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301368static unsigned int qca_get_speed(struct hci_uart *hu,
1369 enum qca_speed_type speed_type)
1370{
1371 unsigned int speed = 0;
1372
1373 if (speed_type == QCA_INIT_SPEED) {
1374 if (hu->init_speed)
1375 speed = hu->init_speed;
1376 else if (hu->proto->init_speed)
1377 speed = hu->proto->init_speed;
1378 } else {
1379 if (hu->oper_speed)
1380 speed = hu->oper_speed;
1381 else if (hu->proto->oper_speed)
1382 speed = hu->proto->oper_speed;
1383 }
1384
1385 return speed;
1386}
1387
1388static int qca_check_speeds(struct hci_uart *hu)
1389{
Venkata Lakshmi Narayana Gubbad8f97da2021-05-18 22:04:42 +05301390 if (qca_is_wcn399x(qca_soc_type(hu)) ||
1391 qca_is_wcn6750(qca_soc_type(hu))) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301392 if (!qca_get_speed(hu, QCA_INIT_SPEED) &&
1393 !qca_get_speed(hu, QCA_OPER_SPEED))
1394 return -EINVAL;
1395 } else {
1396 if (!qca_get_speed(hu, QCA_INIT_SPEED) ||
1397 !qca_get_speed(hu, QCA_OPER_SPEED))
1398 return -EINVAL;
1399 }
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301400
1401 return 0;
1402}
1403
1404static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type)
1405{
1406 unsigned int speed, qca_baudrate;
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001407 struct qca_data *qca = hu->priv;
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301408 int ret = 0;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301409
1410 if (speed_type == QCA_INIT_SPEED) {
1411 speed = qca_get_speed(hu, QCA_INIT_SPEED);
1412 if (speed)
1413 host_set_baudrate(hu, speed);
1414 } else {
Matthias Kaehlcke4fdd5a42019-03-11 11:38:31 -07001415 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1416
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301417 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1418 if (!speed)
1419 return 0;
1420
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301421 /* Disable flow control for wcn3990 to deassert RTS while
1422 * changing the baudrate of chip and host.
1423 */
Venkata Lakshmi Narayana Gubbad8f97da2021-05-18 22:04:42 +05301424 if (qca_is_wcn399x(soc_type) ||
1425 qca_is_wcn6750(soc_type))
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301426 hci_uart_set_flow_control(hu, true);
1427
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001428 if (soc_type == QCA_WCN3990) {
1429 reinit_completion(&qca->drop_ev_comp);
1430 set_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1431 }
1432
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301433 qca_baudrate = qca_get_baudrate_value(speed);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301434 bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed);
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301435 ret = qca_set_baudrate(hu->hdev, qca_baudrate);
1436 if (ret)
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301437 goto error;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301438
1439 host_set_baudrate(hu, speed);
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301440
1441error:
Venkata Lakshmi Narayana Gubbad8f97da2021-05-18 22:04:42 +05301442 if (qca_is_wcn399x(soc_type) ||
1443 qca_is_wcn6750(soc_type))
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301444 hci_uart_set_flow_control(hu, false);
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001445
1446 if (soc_type == QCA_WCN3990) {
1447 /* Wait for the controller to send the vendor event
1448 * for the baudrate change command.
1449 */
1450 if (!wait_for_completion_timeout(&qca->drop_ev_comp,
1451 msecs_to_jiffies(100))) {
1452 bt_dev_err(hu->hdev,
1453 "Failed to change controller baudrate\n");
1454 ret = -ETIMEDOUT;
1455 }
1456
1457 clear_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1458 }
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301459 }
1460
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301461 return ret;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301462}
1463
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301464static int qca_send_crashbuffer(struct hci_uart *hu)
1465{
1466 struct qca_data *qca = hu->priv;
1467 struct sk_buff *skb;
1468
1469 skb = bt_skb_alloc(QCA_CRASHBYTE_PACKET_LEN, GFP_KERNEL);
1470 if (!skb) {
1471 bt_dev_err(hu->hdev, "Failed to allocate memory for skb packet");
1472 return -ENOMEM;
1473 }
1474
1475 /* We forcefully crash the controller, by sending 0xfb byte for
1476 * 1024 times. We also might have chance of losing data, To be
1477 * on safer side we send 1096 bytes to the SoC.
1478 */
1479 memset(skb_put(skb, QCA_CRASHBYTE_PACKET_LEN), QCA_MEMDUMP_BYTE,
1480 QCA_CRASHBYTE_PACKET_LEN);
1481 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1482 bt_dev_info(hu->hdev, "crash the soc to collect controller dump");
1483 skb_queue_tail(&qca->txq, skb);
1484 hci_uart_tx_wakeup(hu);
1485
1486 return 0;
1487}
1488
1489static void qca_wait_for_dump_collection(struct hci_dev *hdev)
1490{
1491 struct hci_uart *hu = hci_get_drvdata(hdev);
1492 struct qca_data *qca = hu->priv;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301493
1494 wait_on_bit_timeout(&qca->flags, QCA_MEMDUMP_COLLECTION,
1495 TASK_UNINTERRUPTIBLE, MEMDUMP_TIMEOUT_MS);
1496
1497 clear_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301498}
1499
1500static void qca_hw_error(struct hci_dev *hdev, u8 code)
1501{
1502 struct hci_uart *hu = hci_get_drvdata(hdev);
1503 struct qca_data *qca = hu->priv;
1504
Venkata Lakshmi Narayana Gubba33445372020-07-11 17:01:12 +05301505 set_bit(QCA_SSR_TRIGGERED, &qca->flags);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301506 set_bit(QCA_HW_ERROR_EVENT, &qca->flags);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301507 bt_dev_info(hdev, "mem_dump_status: %d", qca->memdump_state);
1508
1509 if (qca->memdump_state == QCA_MEMDUMP_IDLE) {
1510 /* If hardware error event received for other than QCA
1511 * soc memory dump event, then we need to crash the SOC
1512 * and wait here for 8 seconds to get the dump packets.
1513 * This will block main thread to be on hold until we
1514 * collect dump.
1515 */
1516 set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1517 qca_send_crashbuffer(hu);
1518 qca_wait_for_dump_collection(hdev);
1519 } else if (qca->memdump_state == QCA_MEMDUMP_COLLECTING) {
1520 /* Let us wait here until memory dump collected or
1521 * memory dump timer expired.
1522 */
1523 bt_dev_info(hdev, "waiting for dump to complete");
1524 qca_wait_for_dump_collection(hdev);
1525 }
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301526
Venkata Lakshmi Narayana Gubbaf98aa802020-06-09 21:27:08 +05301527 mutex_lock(&qca->hci_memdump_lock);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301528 if (qca->memdump_state != QCA_MEMDUMP_COLLECTED) {
1529 bt_dev_err(hu->hdev, "clearing allocated memory due to memdump timeout");
Venkata Lakshmi Narayana Gubbaf98aa802020-06-09 21:27:08 +05301530 if (qca->qca_memdump) {
1531 vfree(qca->qca_memdump->memdump_buf_head);
1532 kfree(qca->qca_memdump);
1533 qca->qca_memdump = NULL;
1534 }
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301535 qca->memdump_state = QCA_MEMDUMP_TIMEOUT;
1536 cancel_delayed_work(&qca->ctrl_memdump_timeout);
Venkata Lakshmi Narayana Gubbaf98aa802020-06-09 21:27:08 +05301537 }
1538 mutex_unlock(&qca->hci_memdump_lock);
1539
1540 if (qca->memdump_state == QCA_MEMDUMP_TIMEOUT ||
1541 qca->memdump_state == QCA_MEMDUMP_COLLECTED) {
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301542 cancel_work_sync(&qca->ctrl_memdump_evt);
Venkata Lakshmi Narayana Gubbaf98aa802020-06-09 21:27:08 +05301543 skb_queue_purge(&qca->rx_memdump_q);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301544 }
1545
1546 clear_bit(QCA_HW_ERROR_EVENT, &qca->flags);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301547}
1548
1549static void qca_cmd_timeout(struct hci_dev *hdev)
1550{
1551 struct hci_uart *hu = hci_get_drvdata(hdev);
1552 struct qca_data *qca = hu->priv;
1553
Venkata Lakshmi Narayana Gubba33445372020-07-11 17:01:12 +05301554 set_bit(QCA_SSR_TRIGGERED, &qca->flags);
1555 if (qca->memdump_state == QCA_MEMDUMP_IDLE) {
1556 set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301557 qca_send_crashbuffer(hu);
Venkata Lakshmi Narayana Gubba33445372020-07-11 17:01:12 +05301558 qca_wait_for_dump_collection(hdev);
1559 } else if (qca->memdump_state == QCA_MEMDUMP_COLLECTING) {
1560 /* Let us wait here until memory dump collected or
1561 * memory dump timer expired.
1562 */
1563 bt_dev_info(hdev, "waiting for dump to complete");
1564 qca_wait_for_dump_collection(hdev);
1565 }
1566
1567 mutex_lock(&qca->hci_memdump_lock);
1568 if (qca->memdump_state != QCA_MEMDUMP_COLLECTED) {
1569 qca->memdump_state = QCA_MEMDUMP_TIMEOUT;
1570 if (!test_bit(QCA_HW_ERROR_EVENT, &qca->flags)) {
1571 /* Inject hw error event to reset the device
1572 * and driver.
1573 */
1574 hci_reset_dev(hu->hdev);
1575 }
1576 }
1577 mutex_unlock(&qca->hci_memdump_lock);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301578}
1579
Venkata Lakshmi Narayana Gubbac1a74162021-03-11 12:33:40 +05301580static bool qca_prevent_wake(struct hci_dev *hdev)
1581{
1582 struct hci_uart *hu = hci_get_drvdata(hdev);
1583 bool wakeup;
1584
1585 /* UART driver handles the interrupt from BT SoC.So we need to use
1586 * device handle of UART driver to get the status of device may wakeup.
1587 */
1588 wakeup = device_may_wakeup(hu->serdev->ctrl->dev.parent);
1589 bt_dev_dbg(hu->hdev, "wakeup status : %d", wakeup);
1590
1591 return !wakeup;
1592}
1593
Venkata Lakshmi Narayana Gubbad8f97da2021-05-18 22:04:42 +05301594static int qca_regulator_init(struct hci_uart *hu)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301595{
Venkata Lakshmi Narayana Gubbad8f97da2021-05-18 22:04:42 +05301596 enum qca_btsoc_type soc_type = qca_soc_type(hu);
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301597 struct qca_serdev *qcadev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301598 int ret;
Venkata Lakshmi Narayana Gubbad8f97da2021-05-18 22:04:42 +05301599 bool sw_ctrl_state;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301600
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301601 /* Check for vregs status, may be hci down has turned
1602 * off the voltage regulator.
1603 */
1604 qcadev = serdev_device_get_drvdata(hu->serdev);
1605 if (!qcadev->bt_power->vregs_on) {
1606 serdev_device_close(hu->serdev);
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001607 ret = qca_regulator_enable(qcadev);
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301608 if (ret)
1609 return ret;
1610
1611 ret = serdev_device_open(hu->serdev);
1612 if (ret) {
1613 bt_dev_err(hu->hdev, "failed to open port");
1614 return ret;
1615 }
1616 }
1617
Venkata Lakshmi Narayana Gubbad8f97da2021-05-18 22:04:42 +05301618 if (qca_is_wcn399x(soc_type)) {
1619 /* Forcefully enable wcn399x to enter in to boot mode. */
1620 host_set_baudrate(hu, 2400);
1621 ret = qca_send_power_pulse(hu, false);
1622 if (ret)
1623 return ret;
1624 }
1625
1626 /* For wcn6750 need to enable gpio bt_en */
1627 if (qcadev->bt_en) {
1628 gpiod_set_value_cansleep(qcadev->bt_en, 0);
1629 msleep(50);
1630 gpiod_set_value_cansleep(qcadev->bt_en, 1);
1631 msleep(50);
1632 if (qcadev->sw_ctrl) {
1633 sw_ctrl_state = gpiod_get_value_cansleep(qcadev->sw_ctrl);
1634 bt_dev_dbg(hu->hdev, "SW_CTRL is %d", sw_ctrl_state);
1635 }
1636 }
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301637
1638 qca_set_speed(hu, QCA_INIT_SPEED);
Venkata Lakshmi Narayana Gubbad8f97da2021-05-18 22:04:42 +05301639
1640 if (qca_is_wcn399x(soc_type)) {
1641 ret = qca_send_power_pulse(hu, true);
1642 if (ret)
1643 return ret;
1644 }
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301645
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301646 /* Now the device is in ready state to communicate with host.
1647 * To sync host with device we need to reopen port.
1648 * Without this, we will have RTS and CTS synchronization
1649 * issues.
1650 */
1651 serdev_device_close(hu->serdev);
1652 ret = serdev_device_open(hu->serdev);
1653 if (ret) {
1654 bt_dev_err(hu->hdev, "failed to open port");
1655 return ret;
1656 }
1657
1658 hci_uart_set_flow_control(hu, false);
1659
1660 return 0;
1661}
1662
Rocky Liao5e6d8402020-01-13 12:30:20 +08001663static int qca_power_on(struct hci_dev *hdev)
1664{
1665 struct hci_uart *hu = hci_get_drvdata(hdev);
1666 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1667 struct qca_serdev *qcadev;
Venkata Lakshmi Narayana Gubba2be43ab2020-10-06 20:50:21 +05301668 struct qca_data *qca = hu->priv;
Rocky Liao5e6d8402020-01-13 12:30:20 +08001669 int ret = 0;
1670
1671 /* Non-serdev device usually is powered by external power
1672 * and don't need additional action in driver for power on
1673 */
1674 if (!hu->serdev)
1675 return 0;
1676
Venkata Lakshmi Narayana Gubbad8f97da2021-05-18 22:04:42 +05301677 if (qca_is_wcn399x(soc_type) ||
1678 qca_is_wcn6750(soc_type)) {
1679 ret = qca_regulator_init(hu);
Rocky Liao5e6d8402020-01-13 12:30:20 +08001680 } else {
1681 qcadev = serdev_device_get_drvdata(hu->serdev);
Rocky Liao77131df2020-03-04 21:16:45 +08001682 if (qcadev->bt_en) {
Rocky Liao8a208b22020-03-04 09:54:29 +08001683 gpiod_set_value_cansleep(qcadev->bt_en, 1);
1684 /* Controller needs time to bootup. */
1685 msleep(150);
1686 }
Rocky Liao5e6d8402020-01-13 12:30:20 +08001687 }
1688
Venkata Lakshmi Narayana Gubba2be43ab2020-10-06 20:50:21 +05301689 clear_bit(QCA_BT_OFF, &qca->flags);
Rocky Liao5e6d8402020-01-13 12:30:20 +08001690 return ret;
1691}
1692
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001693static int qca_setup(struct hci_uart *hu)
1694{
1695 struct hci_dev *hdev = hu->hdev;
1696 struct qca_data *qca = hu->priv;
1697 unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
Rocky Liaobb2500ab2020-01-15 16:55:51 +08001698 unsigned int retries = 0;
Matthias Kaehlcke4fdd5a42019-03-11 11:38:31 -07001699 enum qca_btsoc_type soc_type = qca_soc_type(hu);
Rocky Liao99c905c2019-06-06 17:40:30 +08001700 const char *firmware_name = qca_get_firmware_name(hu);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001701 int ret;
Venkata Lakshmi Narayana Gubba059924f2020-11-19 18:24:09 +05301702 struct qca_btsoc_version ver;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001703
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301704 ret = qca_check_speeds(hu);
1705 if (ret)
1706 return ret;
1707
Venkata Lakshmi Narayana Gubba55c0bd72021-02-05 21:07:16 +05301708 clear_bit(QCA_ROM_FW, &qca->flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001709 /* Patch downloading has to be done without IBS mode */
Venkata Lakshmi Narayana Gubba2be43ab2020-10-06 20:50:21 +05301710 set_bit(QCA_IBS_DISABLED, &qca->flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001711
Rocky Liaoe14c1672019-08-21 14:23:39 +08001712 /* Enable controller to do both LE scan and BR/EDR inquiry
1713 * simultaneously.
1714 */
1715 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1716
Rocky Liao5e6d8402020-01-13 12:30:20 +08001717 bt_dev_info(hdev, "setting up %s",
Venkata Lakshmi Narayana Gubbad8f97da2021-05-18 22:04:42 +05301718 qca_is_wcn399x(soc_type) ? "wcn399x" :
1719 (soc_type == QCA_WCN6750) ? "wcn6750" : "ROME/QCA6390");
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301720
Venkata Lakshmi Narayana Gubba58789a12020-07-28 21:23:00 +05301721 qca->memdump_state = QCA_MEMDUMP_IDLE;
1722
Rocky Liaobb2500ab2020-01-15 16:55:51 +08001723retry:
Rocky Liao5e6d8402020-01-13 12:30:20 +08001724 ret = qca_power_on(hdev);
1725 if (ret)
Balakrishna Godavarthi9e805872020-11-11 10:44:13 +05301726 goto out;
Rocky Liao5e6d8402020-01-13 12:30:20 +08001727
Venkata Lakshmi Narayana Gubba33445372020-07-11 17:01:12 +05301728 clear_bit(QCA_SSR_TRIGGERED, &qca->flags);
1729
Venkata Lakshmi Narayana Gubbad8f97da2021-05-18 22:04:42 +05301730 if (qca_is_wcn399x(soc_type) ||
1731 qca_is_wcn6750(soc_type)) {
Matthias Kaehlcke5971752d2019-02-19 12:05:59 -08001732 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301733
Venkata Lakshmi Narayana Gubba059924f2020-11-19 18:24:09 +05301734 ret = qca_read_soc_version(hdev, &ver, soc_type);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301735 if (ret)
Balakrishna Godavarthi9e805872020-11-11 10:44:13 +05301736 goto out;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301737 } else {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301738 qca_set_speed(hu, QCA_INIT_SPEED);
1739 }
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001740
1741 /* Setup user speed if needed */
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301742 speed = qca_get_speed(hu, QCA_OPER_SPEED);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001743 if (speed) {
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301744 ret = qca_set_speed(hu, QCA_OPER_SPEED);
1745 if (ret)
Balakrishna Godavarthi9e805872020-11-11 10:44:13 +05301746 goto out;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301747
1748 qca_baudrate = qca_get_baudrate_value(speed);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001749 }
1750
Venkata Lakshmi Narayana Gubbad8f97da2021-05-18 22:04:42 +05301751 if (!(qca_is_wcn399x(soc_type) ||
1752 qca_is_wcn6750(soc_type))) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301753 /* Get QCA version information */
Venkata Lakshmi Narayana Gubba059924f2020-11-19 18:24:09 +05301754 ret = qca_read_soc_version(hdev, &ver, soc_type);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301755 if (ret)
Balakrishna Godavarthi9e805872020-11-11 10:44:13 +05301756 goto out;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301757 }
Balakrishna Godavarthiaadebac2018-08-03 17:46:28 +05301758
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001759 /* Setup patch / NVM configurations */
Venkata Lakshmi Narayana Gubba059924f2020-11-19 18:24:09 +05301760 ret = qca_uart_setup(hdev, qca_baudrate, soc_type, ver,
Rocky Liao99c905c2019-06-06 17:40:30 +08001761 firmware_name);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001762 if (!ret) {
Venkata Lakshmi Narayana Gubba2be43ab2020-10-06 20:50:21 +05301763 clear_bit(QCA_IBS_DISABLED, &qca->flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001764 qca_debugfs_init(hdev);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301765 hu->hdev->hw_error = qca_hw_error;
1766 hu->hdev->cmd_timeout = qca_cmd_timeout;
Venkata Lakshmi Narayana Gubbac1a74162021-03-11 12:33:40 +05301767 hu->hdev->prevent_wake = qca_prevent_wake;
Loic Poulainba8f3592017-11-06 12:16:56 +01001768 } else if (ret == -ENOENT) {
1769 /* No patch/nvm-config found, run with original fw/config */
Venkata Lakshmi Narayana Gubba55c0bd72021-02-05 21:07:16 +05301770 set_bit(QCA_ROM_FW, &qca->flags);
Loic Poulainba8f3592017-11-06 12:16:56 +01001771 ret = 0;
Amit Pundir7dc5fe02018-04-16 12:10:24 +05301772 } else if (ret == -EAGAIN) {
1773 /*
1774 * Userspace firmware loader will return -EAGAIN in case no
1775 * patch/nvm-config is found, so run with original fw/config.
1776 */
Venkata Lakshmi Narayana Gubba55c0bd72021-02-05 21:07:16 +05301777 set_bit(QCA_ROM_FW, &qca->flags);
Amit Pundir7dc5fe02018-04-16 12:10:24 +05301778 ret = 0;
Balakrishna Godavarthi9e805872020-11-11 10:44:13 +05301779 }
1780
1781out:
1782 if (ret && retries < MAX_INIT_RETRIES) {
1783 bt_dev_warn(hdev, "Retry BT power ON:%d", retries);
1784 qca_power_shutdown(hu);
1785 if (hu->serdev) {
1786 serdev_device_close(hu->serdev);
1787 ret = serdev_device_open(hu->serdev);
1788 if (ret) {
1789 bt_dev_err(hdev, "failed to open port");
1790 return ret;
Rocky Liaobb2500ab2020-01-15 16:55:51 +08001791 }
Rocky Liaobb2500ab2020-01-15 16:55:51 +08001792 }
Balakrishna Godavarthi9e805872020-11-11 10:44:13 +05301793 retries++;
1794 goto retry;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001795 }
1796
1797 /* Setup bdaddr */
Rocky Liaoe5d64682020-03-25 10:26:37 +08001798 if (soc_type == QCA_ROME)
Balakrishna Godavarthi5c0a10012019-01-16 18:01:15 +05301799 hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
Rocky Liaoe5d64682020-03-25 10:26:37 +08001800 else
1801 hu->hdev->set_bdaddr = qca_set_bdaddr;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001802
1803 return ret;
1804}
1805
Nishka Dasgupta2edc9c52019-08-15 11:21:49 +05301806static const struct hci_uart_proto qca_proto = {
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001807 .id = HCI_UART_QCA,
1808 .name = "QCA",
Marcel Holtmannaee61f72015-10-20 21:30:45 +02001809 .manufacturer = 29,
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001810 .init_speed = 115200,
1811 .oper_speed = 3000000,
1812 .open = qca_open,
1813 .close = qca_close,
1814 .flush = qca_flush,
1815 .setup = qca_setup,
1816 .recv = qca_recv,
1817 .enqueue = qca_enqueue,
1818 .dequeue = qca_dequeue,
1819};
1820
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -07001821static const struct qca_device_data qca_soc_data_wcn3990 = {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301822 .soc_type = QCA_WCN3990,
1823 .vregs = (struct qca_vreg []) {
Bjorn Anderssonf2edd662019-10-17 22:24:02 -07001824 { "vddio", 15000 },
1825 { "vddxo", 80000 },
1826 { "vddrf", 300000 },
1827 { "vddch0", 450000 },
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301828 },
1829 .num_vregs = 4,
1830};
1831
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -07001832static const struct qca_device_data qca_soc_data_wcn3991 = {
Balakrishna Godavarthi7d250a02019-11-06 15:18:32 +05301833 .soc_type = QCA_WCN3991,
1834 .vregs = (struct qca_vreg []) {
1835 { "vddio", 15000 },
1836 { "vddxo", 80000 },
1837 { "vddrf", 300000 },
1838 { "vddch0", 450000 },
1839 },
1840 .num_vregs = 4,
Abhishek Pandit-Subedi54780132020-09-30 13:01:38 -07001841 .capabilities = QCA_CAP_WIDEBAND_SPEECH | QCA_CAP_VALID_LE_STATES,
Balakrishna Godavarthi7d250a02019-11-06 15:18:32 +05301842};
1843
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -07001844static const struct qca_device_data qca_soc_data_wcn3998 = {
Harish Bandi523760b2019-04-26 19:26:01 +05301845 .soc_type = QCA_WCN3998,
1846 .vregs = (struct qca_vreg []) {
Bjorn Anderssonf2edd662019-10-17 22:24:02 -07001847 { "vddio", 10000 },
1848 { "vddxo", 80000 },
1849 { "vddrf", 300000 },
1850 { "vddch0", 450000 },
Harish Bandi523760b2019-04-26 19:26:01 +05301851 },
1852 .num_vregs = 4,
1853};
1854
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -07001855static const struct qca_device_data qca_soc_data_qca6390 = {
Rocky Liaoe5d64682020-03-25 10:26:37 +08001856 .soc_type = QCA_QCA6390,
1857 .num_vregs = 0,
1858};
1859
Venkata Lakshmi Narayana Gubbad8f97da2021-05-18 22:04:42 +05301860static const struct qca_device_data qca_soc_data_wcn6750 = {
1861 .soc_type = QCA_WCN6750,
1862 .vregs = (struct qca_vreg []) {
1863 { "vddio", 5000 },
1864 { "vddaon", 26000 },
1865 { "vddbtcxmx", 126000 },
1866 { "vddrfacmn", 12500 },
1867 { "vddrfa0p8", 102000 },
1868 { "vddrfa1p7", 302000 },
1869 { "vddrfa1p2", 257000 },
1870 { "vddrfa2p2", 1700000 },
1871 { "vddasd", 200 },
1872 },
1873 .num_vregs = 9,
1874 .capabilities = QCA_CAP_WIDEBAND_SPEECH | QCA_CAP_VALID_LE_STATES,
1875};
1876
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +05301877static void qca_power_shutdown(struct hci_uart *hu)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301878{
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001879 struct qca_serdev *qcadev;
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +05301880 struct qca_data *qca = hu->priv;
1881 unsigned long flags;
Rocky Liao55599042020-01-15 16:55:50 +08001882 enum qca_btsoc_type soc_type = qca_soc_type(hu);
Venkata Lakshmi Narayana Gubbad8f97da2021-05-18 22:04:42 +05301883 bool sw_ctrl_state;
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +05301884
1885 /* From this point we go into power off state. But serial port is
1886 * still open, stop queueing the IBS data and flush all the buffered
1887 * data in skb's.
1888 */
1889 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
Venkata Lakshmi Narayana Gubba2be43ab2020-10-06 20:50:21 +05301890 set_bit(QCA_IBS_DISABLED, &qca->flags);
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +05301891 qca_flush(hu);
1892 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1893
Rocky Liao55599042020-01-15 16:55:50 +08001894 /* Non-serdev device usually is powered by external power
1895 * and don't need additional action in driver for power down
1896 */
1897 if (!hu->serdev)
1898 return;
1899
Pavel Skripkin59f90f12021-05-03 13:06:05 +03001900 qcadev = serdev_device_get_drvdata(hu->serdev);
1901
Rocky Liao55599042020-01-15 16:55:50 +08001902 if (qca_is_wcn399x(soc_type)) {
1903 host_set_baudrate(hu, 2400);
1904 qca_send_power_pulse(hu, false);
1905 qca_regulator_disable(qcadev);
Venkata Lakshmi Narayana Gubbad8f97da2021-05-18 22:04:42 +05301906 } else if (soc_type == QCA_WCN6750) {
1907 gpiod_set_value_cansleep(qcadev->bt_en, 0);
1908 msleep(100);
1909 qca_regulator_disable(qcadev);
1910 if (qcadev->sw_ctrl) {
1911 sw_ctrl_state = gpiod_get_value_cansleep(qcadev->sw_ctrl);
1912 bt_dev_dbg(hu->hdev, "SW_CTRL is %d", sw_ctrl_state);
1913 }
Rocky Liao77131df2020-03-04 21:16:45 +08001914 } else if (qcadev->bt_en) {
Rocky Liao55599042020-01-15 16:55:50 +08001915 gpiod_set_value_cansleep(qcadev->bt_en, 0);
1916 }
Venkata Lakshmi Narayana Gubba2be43ab2020-10-06 20:50:21 +05301917
1918 set_bit(QCA_BT_OFF, &qca->flags);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301919}
1920
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301921static int qca_power_off(struct hci_dev *hdev)
1922{
1923 struct hci_uart *hu = hci_get_drvdata(hdev);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301924 struct qca_data *qca = hu->priv;
Rocky Liao4f9ed5b2020-02-29 20:21:18 +08001925 enum qca_btsoc_type soc_type = qca_soc_type(hu);
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301926
Venkata Lakshmi Narayana Gubba58789a12020-07-28 21:23:00 +05301927 hu->hdev->hw_error = NULL;
1928 hu->hdev->cmd_timeout = NULL;
1929
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301930 /* Stop sending shutdown command if soc crashes. */
Rocky Liaoe5d64682020-03-25 10:26:37 +08001931 if (soc_type != QCA_ROME
Rocky Liao4f9ed5b2020-02-29 20:21:18 +08001932 && qca->memdump_state == QCA_MEMDUMP_IDLE) {
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301933 qca_send_pre_shutdown_cmd(hdev);
1934 usleep_range(8000, 10000);
1935 }
Harish Bandia2780882019-07-12 10:39:40 +05301936
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301937 qca_power_shutdown(hu);
1938 return 0;
1939}
1940
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001941static int qca_regulator_enable(struct qca_serdev *qcadev)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301942{
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001943 struct qca_power *power = qcadev->bt_power;
1944 int ret;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301945
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001946 /* Already enabled */
1947 if (power->vregs_on)
1948 return 0;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301949
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001950 BT_DBG("enabling %d regulators)", power->num_vregs);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301951
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001952 ret = regulator_bulk_enable(power->num_vregs, power->vreg_bulk);
1953 if (ret)
1954 return ret;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301955
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001956 power->vregs_on = true;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301957
Venkata Lakshmi Narayana Gubba66cb7052020-02-03 16:10:40 +05301958 ret = clk_prepare_enable(qcadev->susclk);
Venkata Lakshmi Narayana Gubbaf3d63f52020-02-05 16:21:43 +05301959 if (ret)
Venkata Lakshmi Narayana Gubba66cb7052020-02-03 16:10:40 +05301960 qca_regulator_disable(qcadev);
Venkata Lakshmi Narayana Gubba66cb7052020-02-03 16:10:40 +05301961
Venkata Lakshmi Narayana Gubbaf3d63f52020-02-05 16:21:43 +05301962 return ret;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301963}
1964
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001965static void qca_regulator_disable(struct qca_serdev *qcadev)
1966{
1967 struct qca_power *power;
1968
1969 if (!qcadev)
1970 return;
1971
1972 power = qcadev->bt_power;
1973
1974 /* Already disabled? */
1975 if (!power->vregs_on)
1976 return;
1977
1978 regulator_bulk_disable(power->num_vregs, power->vreg_bulk);
1979 power->vregs_on = false;
Venkata Lakshmi Narayana Gubba66cb7052020-02-03 16:10:40 +05301980
Venkata Lakshmi Narayana Gubbaf3d63f52020-02-05 16:21:43 +05301981 clk_disable_unprepare(qcadev->susclk);
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001982}
1983
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301984static int qca_init_regulators(struct qca_power *qca,
1985 const struct qca_vreg *vregs, size_t num_vregs)
1986{
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001987 struct regulator_bulk_data *bulk;
1988 int ret;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301989 int i;
1990
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001991 bulk = devm_kcalloc(qca->dev, num_vregs, sizeof(*bulk), GFP_KERNEL);
1992 if (!bulk)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301993 return -ENOMEM;
1994
1995 for (i = 0; i < num_vregs; i++)
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001996 bulk[i].supply = vregs[i].name;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301997
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001998 ret = devm_regulator_bulk_get(qca->dev, num_vregs, bulk);
1999 if (ret < 0)
2000 return ret;
2001
2002 for (i = 0; i < num_vregs; i++) {
2003 ret = regulator_set_load(bulk[i].consumer, vregs[i].load_uA);
2004 if (ret)
2005 return ret;
2006 }
2007
2008 qca->vreg_bulk = bulk;
Bjorn Andersson163d42f2019-10-17 22:24:03 -07002009 qca->num_vregs = num_vregs;
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07002010
2011 return 0;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05302012}
2013
Thierry Escande05ba5332018-03-29 21:15:24 +02002014static int qca_serdev_probe(struct serdev_device *serdev)
2015{
2016 struct qca_serdev *qcadev;
Rocky Liaoae563182020-01-16 11:22:54 +08002017 struct hci_dev *hdev;
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -07002018 const struct qca_device_data *data;
Thierry Escande05ba5332018-03-29 21:15:24 +02002019 int err;
Rocky Liao8a208b22020-03-04 09:54:29 +08002020 bool power_ctrl_enabled = true;
Thierry Escande05ba5332018-03-29 21:15:24 +02002021
2022 qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
2023 if (!qcadev)
2024 return -ENOMEM;
2025
2026 qcadev->serdev_hu.serdev = serdev;
Rocky Liao9f3565b2019-12-13 16:50:45 +08002027 data = device_get_match_data(&serdev->dev);
Thierry Escande05ba5332018-03-29 21:15:24 +02002028 serdev_device_set_drvdata(serdev, qcadev);
Rocky Liao99c905c2019-06-06 17:40:30 +08002029 device_property_read_string(&serdev->dev, "firmware-name",
2030 &qcadev->firmware_name);
Christian Hewitt37aee132020-04-23 01:34:30 +00002031 device_property_read_u32(&serdev->dev, "max-speed",
2032 &qcadev->oper_speed);
2033 if (!qcadev->oper_speed)
2034 BT_DBG("UART will pick default operating speed");
2035
Venkata Lakshmi Narayana Gubbad8f97da2021-05-18 22:04:42 +05302036 if (data &&
2037 (qca_is_wcn399x(data->soc_type) ||
2038 qca_is_wcn6750(data->soc_type))) {
Harish Bandi523760b2019-04-26 19:26:01 +05302039 qcadev->btsoc_type = data->soc_type;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05302040 qcadev->bt_power = devm_kzalloc(&serdev->dev,
2041 sizeof(struct qca_power),
2042 GFP_KERNEL);
2043 if (!qcadev->bt_power)
2044 return -ENOMEM;
Thierry Escande05ba5332018-03-29 21:15:24 +02002045
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05302046 qcadev->bt_power->dev = &serdev->dev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05302047 err = qca_init_regulators(qcadev->bt_power, data->vregs,
2048 data->num_vregs);
2049 if (err) {
2050 BT_ERR("Failed to init regulators:%d", err);
Rocky Liaoae563182020-01-16 11:22:54 +08002051 return err;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05302052 }
2053
2054 qcadev->bt_power->vregs_on = false;
2055
Venkata Lakshmi Narayana Gubbad8f97da2021-05-18 22:04:42 +05302056 qcadev->bt_en = devm_gpiod_get_optional(&serdev->dev, "enable",
2057 GPIOD_OUT_LOW);
2058 if (!qcadev->bt_en && data->soc_type == QCA_WCN6750) {
2059 dev_err(&serdev->dev, "failed to acquire BT_EN gpio\n");
2060 power_ctrl_enabled = false;
2061 }
2062
2063 qcadev->sw_ctrl = devm_gpiod_get_optional(&serdev->dev, "swctrl",
2064 GPIOD_IN);
2065 if (!qcadev->sw_ctrl && data->soc_type == QCA_WCN6750)
2066 dev_warn(&serdev->dev, "failed to acquire SW_CTRL gpio\n");
2067
Venkata Lakshmi Narayana Gubba66cb7052020-02-03 16:10:40 +05302068 qcadev->susclk = devm_clk_get_optional(&serdev->dev, NULL);
2069 if (IS_ERR(qcadev->susclk)) {
2070 dev_err(&serdev->dev, "failed to acquire clk\n");
2071 return PTR_ERR(qcadev->susclk);
2072 }
2073
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05302074 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
2075 if (err) {
2076 BT_ERR("wcn3990 serdev registration failed");
Rocky Liaoae563182020-01-16 11:22:54 +08002077 return err;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05302078 }
2079 } else {
Rocky Liaoe5d64682020-03-25 10:26:37 +08002080 if (data)
2081 qcadev->btsoc_type = data->soc_type;
2082 else
2083 qcadev->btsoc_type = QCA_ROME;
2084
Rocky Liao77131df2020-03-04 21:16:45 +08002085 qcadev->bt_en = devm_gpiod_get_optional(&serdev->dev, "enable",
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05302086 GPIOD_OUT_LOW);
Rocky Liao77131df2020-03-04 21:16:45 +08002087 if (!qcadev->bt_en) {
Rocky Liao8a208b22020-03-04 09:54:29 +08002088 dev_warn(&serdev->dev, "failed to acquire enable gpio\n");
2089 power_ctrl_enabled = false;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05302090 }
2091
Rocky Liao77131df2020-03-04 21:16:45 +08002092 qcadev->susclk = devm_clk_get_optional(&serdev->dev, NULL);
Dan Carpenter4c07a5d2020-05-29 12:59:48 +03002093 if (IS_ERR(qcadev->susclk)) {
Rocky Liao8a208b22020-03-04 09:54:29 +08002094 dev_warn(&serdev->dev, "failed to acquire clk\n");
Dan Carpenter4c07a5d2020-05-29 12:59:48 +03002095 return PTR_ERR(qcadev->susclk);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05302096 }
Dan Carpenter4c07a5d2020-05-29 12:59:48 +03002097 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
2098 if (err)
2099 return err;
2100
2101 err = clk_prepare_enable(qcadev->susclk);
2102 if (err)
2103 return err;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05302104
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05302105 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
Rocky Liaoae563182020-01-16 11:22:54 +08002106 if (err) {
2107 BT_ERR("Rome serdev registration failed");
Xu Wang65a24d42020-09-03 08:27:19 +00002108 clk_disable_unprepare(qcadev->susclk);
Rocky Liaoae563182020-01-16 11:22:54 +08002109 return err;
2110 }
Thierry Escande05ba5332018-03-29 21:15:24 +02002111 }
2112
Abhishek Pandit-Subedi85e90d92020-05-20 09:32:28 -07002113 hdev = qcadev->serdev_hu.hdev;
2114
Rocky Liao8a208b22020-03-04 09:54:29 +08002115 if (power_ctrl_enabled) {
Rocky Liao8a208b22020-03-04 09:54:29 +08002116 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
2117 hdev->shutdown = qca_power_off;
2118 }
Thierry Escande05ba5332018-03-29 21:15:24 +02002119
Abhishek Pandit-Subedi54780132020-09-30 13:01:38 -07002120 if (data) {
2121 /* Wideband speech support must be set per driver since it can't
2122 * be queried via hci. Same with the valid le states quirk.
2123 */
2124 if (data->capabilities & QCA_CAP_WIDEBAND_SPEECH)
2125 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
2126 &hdev->quirks);
2127
2128 if (data->capabilities & QCA_CAP_VALID_LE_STATES)
2129 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
2130 }
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -07002131
Rocky Liaoae563182020-01-16 11:22:54 +08002132 return 0;
Thierry Escande05ba5332018-03-29 21:15:24 +02002133}
2134
2135static void qca_serdev_remove(struct serdev_device *serdev)
2136{
2137 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
Venkata Lakshmi Narayana Gubba054ec5e2020-09-10 16:00:43 +05302138 struct qca_power *power = qcadev->bt_power;
Thierry Escande05ba5332018-03-29 21:15:24 +02002139
Venkata Lakshmi Narayana Gubbad8f97da2021-05-18 22:04:42 +05302140 if ((qca_is_wcn399x(qcadev->btsoc_type) ||
2141 qca_is_wcn6750(qcadev->btsoc_type)) &&
2142 power->vregs_on)
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +05302143 qca_power_shutdown(&qcadev->serdev_hu);
Rocky Liao77131df2020-03-04 21:16:45 +08002144 else if (qcadev->susclk)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05302145 clk_disable_unprepare(qcadev->susclk);
Thierry Escande05ba5332018-03-29 21:15:24 +02002146
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05302147 hci_uart_unregister_device(&qcadev->serdev_hu);
Thierry Escande05ba5332018-03-29 21:15:24 +02002148}
2149
Zijun Hu7e7bbdd2020-05-29 21:56:57 +08002150static void qca_serdev_shutdown(struct device *dev)
2151{
2152 int ret;
2153 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
2154 struct serdev_device *serdev = to_serdev_device(dev);
2155 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
2156 const u8 ibs_wake_cmd[] = { 0xFD };
2157 const u8 edl_reset_soc_cmd[] = { 0x01, 0x00, 0xFC, 0x01, 0x05 };
2158
2159 if (qcadev->btsoc_type == QCA_QCA6390) {
2160 serdev_device_write_flush(serdev);
2161 ret = serdev_device_write_buf(serdev, ibs_wake_cmd,
2162 sizeof(ibs_wake_cmd));
2163 if (ret < 0) {
2164 BT_ERR("QCA send IBS_WAKE_IND error: %d", ret);
2165 return;
2166 }
2167 serdev_device_wait_until_sent(serdev, timeout);
2168 usleep_range(8000, 10000);
2169
2170 serdev_device_write_flush(serdev);
2171 ret = serdev_device_write_buf(serdev, edl_reset_soc_cmd,
2172 sizeof(edl_reset_soc_cmd));
2173 if (ret < 0) {
2174 BT_ERR("QCA send EDL_RESET_REQ error: %d", ret);
2175 return;
2176 }
2177 serdev_device_wait_until_sent(serdev, timeout);
2178 usleep_range(8000, 10000);
2179 }
2180}
2181
Claire Chang41d5b252019-10-31 18:46:14 +08002182static int __maybe_unused qca_suspend(struct device *dev)
2183{
Zijun Hufeac90d2020-05-29 04:31:07 +08002184 struct serdev_device *serdev = to_serdev_device(dev);
2185 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
2186 struct hci_uart *hu = &qcadev->serdev_hu;
Claire Chang41d5b252019-10-31 18:46:14 +08002187 struct qca_data *qca = hu->priv;
2188 unsigned long flags;
Matthias Kaehlckee2a119c2020-06-05 11:46:10 -07002189 bool tx_pending = false;
Claire Chang41d5b252019-10-31 18:46:14 +08002190 int ret = 0;
2191 u8 cmd;
Venkata Lakshmi Narayana Gubba2be43ab2020-10-06 20:50:21 +05302192 u32 wait_timeout = 0;
Claire Chang41d5b252019-10-31 18:46:14 +08002193
2194 set_bit(QCA_SUSPENDING, &qca->flags);
2195
Venkata Lakshmi Narayana Gubba55c0bd72021-02-05 21:07:16 +05302196 /* if BT SoC is running with default firmware then it does not
2197 * support in-band sleep
2198 */
2199 if (test_bit(QCA_ROM_FW, &qca->flags))
2200 return 0;
2201
Venkata Lakshmi Narayana Gubbaad3a9c02020-12-30 22:47:08 +05302202 /* During SSR after memory dump collection, controller will be
2203 * powered off and then powered on.If controller is powered off
2204 * during SSR then we should wait until SSR is completed.
2205 */
2206 if (test_bit(QCA_BT_OFF, &qca->flags) &&
2207 !test_bit(QCA_SSR_TRIGGERED, &qca->flags))
Venkata Lakshmi Narayana Gubba2be43ab2020-10-06 20:50:21 +05302208 return 0;
2209
Venkata Lakshmi Narayana Gubba1bb0c662021-02-02 20:27:42 +05302210 if (test_bit(QCA_IBS_DISABLED, &qca->flags) ||
2211 test_bit(QCA_SSR_TRIGGERED, &qca->flags)) {
Venkata Lakshmi Narayana Gubba2be43ab2020-10-06 20:50:21 +05302212 wait_timeout = test_bit(QCA_SSR_TRIGGERED, &qca->flags) ?
2213 IBS_DISABLE_SSR_TIMEOUT_MS :
2214 FW_DOWNLOAD_TIMEOUT_MS;
2215
2216 /* QCA_IBS_DISABLED flag is set to true, During FW download
2217 * and during memory dump collection. It is reset to false,
Venkata Lakshmi Narayana Gubbaad3a9c02020-12-30 22:47:08 +05302218 * After FW download complete.
Venkata Lakshmi Narayana Gubba2be43ab2020-10-06 20:50:21 +05302219 */
2220 wait_on_bit_timeout(&qca->flags, QCA_IBS_DISABLED,
2221 TASK_UNINTERRUPTIBLE, msecs_to_jiffies(wait_timeout));
2222
2223 if (test_bit(QCA_IBS_DISABLED, &qca->flags)) {
2224 bt_dev_err(hu->hdev, "SSR or FW download time out");
2225 ret = -ETIMEDOUT;
2226 goto error;
2227 }
2228 }
2229
Claire Chang41d5b252019-10-31 18:46:14 +08002230 cancel_work_sync(&qca->ws_awake_device);
2231 cancel_work_sync(&qca->ws_awake_rx);
2232
2233 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
2234 flags, SINGLE_DEPTH_NESTING);
2235
2236 switch (qca->tx_ibs_state) {
2237 case HCI_IBS_TX_WAKING:
2238 del_timer(&qca->wake_retrans_timer);
Gustavo A. R. Silvaa3b4cbf2020-07-08 15:26:50 -05002239 fallthrough;
Claire Chang41d5b252019-10-31 18:46:14 +08002240 case HCI_IBS_TX_AWAKE:
2241 del_timer(&qca->tx_idle_timer);
2242
2243 serdev_device_write_flush(hu->serdev);
2244 cmd = HCI_IBS_SLEEP_IND;
2245 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
2246
2247 if (ret < 0) {
2248 BT_ERR("Failed to send SLEEP to device");
2249 break;
2250 }
2251
2252 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
2253 qca->ibs_sent_slps++;
Matthias Kaehlckee2a119c2020-06-05 11:46:10 -07002254 tx_pending = true;
Claire Chang41d5b252019-10-31 18:46:14 +08002255 break;
2256
2257 case HCI_IBS_TX_ASLEEP:
2258 break;
2259
2260 default:
2261 BT_ERR("Spurious tx state %d", qca->tx_ibs_state);
2262 ret = -EINVAL;
2263 break;
2264 }
2265
2266 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
2267
2268 if (ret < 0)
2269 goto error;
2270
Matthias Kaehlckee2a119c2020-06-05 11:46:10 -07002271 if (tx_pending) {
2272 serdev_device_wait_until_sent(hu->serdev,
2273 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
Balakrishna Godavarthi201a1122020-06-12 17:45:17 +05302274 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
Matthias Kaehlckee2a119c2020-06-05 11:46:10 -07002275 }
Claire Chang41d5b252019-10-31 18:46:14 +08002276
2277 /* Wait for HCI_IBS_SLEEP_IND sent by device to indicate its Tx is going
2278 * to sleep, so that the packet does not wake the system later.
2279 */
Claire Chang41d5b252019-10-31 18:46:14 +08002280 ret = wait_event_interruptible_timeout(qca->suspend_wait_q,
2281 qca->rx_ibs_state == HCI_IBS_RX_ASLEEP,
2282 msecs_to_jiffies(IBS_BTSOC_TX_IDLE_TIMEOUT_MS));
Matthias Kaehlcke4da385f2020-06-05 11:46:11 -07002283 if (ret == 0) {
2284 ret = -ETIMEDOUT;
2285 goto error;
Matthias Kaehlckeeff981f2020-06-05 11:46:09 -07002286 }
Claire Chang41d5b252019-10-31 18:46:14 +08002287
Matthias Kaehlcke4da385f2020-06-05 11:46:11 -07002288 return 0;
Claire Chang41d5b252019-10-31 18:46:14 +08002289
2290error:
2291 clear_bit(QCA_SUSPENDING, &qca->flags);
2292
2293 return ret;
2294}
2295
2296static int __maybe_unused qca_resume(struct device *dev)
2297{
Zijun Hufeac90d2020-05-29 04:31:07 +08002298 struct serdev_device *serdev = to_serdev_device(dev);
2299 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
2300 struct hci_uart *hu = &qcadev->serdev_hu;
Claire Chang41d5b252019-10-31 18:46:14 +08002301 struct qca_data *qca = hu->priv;
2302
2303 clear_bit(QCA_SUSPENDING, &qca->flags);
2304
2305 return 0;
2306}
2307
2308static SIMPLE_DEV_PM_OPS(qca_pm_ops, qca_suspend, qca_resume);
2309
Rocky Liaoe5d64682020-03-25 10:26:37 +08002310#ifdef CONFIG_OF
Thierry Escande05ba5332018-03-29 21:15:24 +02002311static const struct of_device_id qca_bluetooth_of_match[] = {
2312 { .compatible = "qcom,qca6174-bt" },
Rocky Liaoe5d64682020-03-25 10:26:37 +08002313 { .compatible = "qcom,qca6390-bt", .data = &qca_soc_data_qca6390},
Christian Hewitt31d4ab82020-04-23 01:34:29 +00002314 { .compatible = "qcom,qca9377-bt" },
Harish Bandi523760b2019-04-26 19:26:01 +05302315 { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990},
Balakrishna Godavarthi7d250a02019-11-06 15:18:32 +05302316 { .compatible = "qcom,wcn3991-bt", .data = &qca_soc_data_wcn3991},
Harish Bandi523760b2019-04-26 19:26:01 +05302317 { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998},
Venkata Lakshmi Narayana Gubbad8f97da2021-05-18 22:04:42 +05302318 { .compatible = "qcom,wcn6750-bt", .data = &qca_soc_data_wcn6750},
Thierry Escande05ba5332018-03-29 21:15:24 +02002319 { /* sentinel */ }
2320};
2321MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
Rocky Liaoe5d64682020-03-25 10:26:37 +08002322#endif
2323
2324#ifdef CONFIG_ACPI
2325static const struct acpi_device_id qca_bluetooth_acpi_match[] = {
2326 { "QCOM6390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2327 { "DLA16390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2328 { "DLB16390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2329 { "DLB26390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2330 { },
2331};
2332MODULE_DEVICE_TABLE(acpi, qca_bluetooth_acpi_match);
2333#endif
2334
Thierry Escande05ba5332018-03-29 21:15:24 +02002335
2336static struct serdev_device_driver qca_serdev_driver = {
2337 .probe = qca_serdev_probe,
2338 .remove = qca_serdev_remove,
2339 .driver = {
2340 .name = "hci_uart_qca",
Rocky Liaoe5d64682020-03-25 10:26:37 +08002341 .of_match_table = of_match_ptr(qca_bluetooth_of_match),
2342 .acpi_match_table = ACPI_PTR(qca_bluetooth_acpi_match),
Zijun Hu7e7bbdd2020-05-29 21:56:57 +08002343 .shutdown = qca_serdev_shutdown,
Claire Chang41d5b252019-10-31 18:46:14 +08002344 .pm = &qca_pm_ops,
Thierry Escande05ba5332018-03-29 21:15:24 +02002345 },
2346};
2347
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07002348int __init qca_init(void)
2349{
Thierry Escande05ba5332018-03-29 21:15:24 +02002350 serdev_device_driver_register(&qca_serdev_driver);
2351
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07002352 return hci_uart_register_proto(&qca_proto);
2353}
2354
2355int __exit qca_deinit(void)
2356{
Thierry Escande05ba5332018-03-29 21:15:24 +02002357 serdev_device_driver_unregister(&qca_serdev_driver);
2358
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07002359 return hci_uart_unregister_proto(&qca_proto);
2360}