blob: aa957d749d6f5fc659f005a17128796d3fd5963f [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
Claire Chang41d5b252019-10-31 18:46:14 +080049#define IBS_BTSOC_TX_IDLE_TIMEOUT_MS 40
50#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
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070053
Thierry Escande05ba5332018-03-29 21:15:24 +020054/* susclk rate */
55#define SUSCLK_RATE_32KHZ 32768
56
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +053057/* Controller debug log header */
58#define QCA_DEBUG_HANDLE 0x2EDC
59
Rocky Liaobb2500ab2020-01-15 16:55:51 +080060/* max retry count when init fails */
61#define MAX_INIT_RETRIES 3
62
Balakrishna Godavarthid8415022020-01-02 20:19:11 +053063/* Controller dump header */
64#define QCA_SSR_DUMP_HANDLE 0x0108
65#define QCA_DUMP_PACKET_SIZE 255
66#define QCA_LAST_SEQUENCE_NUM 0xFFFF
67#define QCA_CRASHBYTE_PACKET_LEN 1096
68#define QCA_MEMDUMP_BYTE 0xFB
69
Matthias Kaehlcke62a91992019-04-29 16:21:30 -070070enum qca_flags {
71 QCA_IBS_ENABLED,
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -070072 QCA_DROP_VENDOR_EVENT,
Claire Chang41d5b252019-10-31 18:46:14 +080073 QCA_SUSPENDING,
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +053074 QCA_MEMDUMP_COLLECTION,
75 QCA_HW_ERROR_EVENT
Matthias Kaehlcke62a91992019-04-29 16:21:30 -070076};
77
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -070078enum qca_capabilities {
79 QCA_CAP_WIDEBAND_SPEECH = BIT(0),
80};
Balakrishna Godavarthid8415022020-01-02 20:19:11 +053081
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070082/* HCI_IBS transmit side sleep protocol states */
83enum tx_ibs_states {
84 HCI_IBS_TX_ASLEEP,
85 HCI_IBS_TX_WAKING,
86 HCI_IBS_TX_AWAKE,
87};
88
89/* HCI_IBS receive side sleep protocol states */
90enum rx_states {
91 HCI_IBS_RX_ASLEEP,
92 HCI_IBS_RX_AWAKE,
93};
94
95/* HCI_IBS transmit and receive side clock state vote */
96enum hci_ibs_clock_state_vote {
97 HCI_IBS_VOTE_STATS_UPDATE,
98 HCI_IBS_TX_VOTE_CLOCK_ON,
99 HCI_IBS_TX_VOTE_CLOCK_OFF,
100 HCI_IBS_RX_VOTE_CLOCK_ON,
101 HCI_IBS_RX_VOTE_CLOCK_OFF,
102};
103
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530104/* Controller memory dump states */
105enum qca_memdump_states {
106 QCA_MEMDUMP_IDLE,
107 QCA_MEMDUMP_COLLECTING,
108 QCA_MEMDUMP_COLLECTED,
109 QCA_MEMDUMP_TIMEOUT,
110};
111
112struct qca_memdump_data {
113 char *memdump_buf_head;
114 char *memdump_buf_tail;
115 u32 current_seq_no;
116 u32 received_dump;
117};
118
119struct qca_memdump_event_hdr {
120 __u8 evt;
121 __u8 plen;
122 __u16 opcode;
123 __u16 seq_no;
124 __u8 reserved;
125} __packed;
126
127
128struct qca_dump_size {
129 u32 dump_size;
130} __packed;
131
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700132struct qca_data {
133 struct hci_uart *hu;
134 struct sk_buff *rx_skb;
135 struct sk_buff_head txq;
136 struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530137 struct sk_buff_head rx_memdump_q; /* Memdump wait queue */
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700138 spinlock_t hci_ibs_lock; /* HCI_IBS state lock */
139 u8 tx_ibs_state; /* HCI_IBS transmit side power state*/
140 u8 rx_ibs_state; /* HCI_IBS receive side power state */
Viresh Kumar621a5f72015-09-26 15:04:07 -0700141 bool tx_vote; /* Clock must be on for TX */
142 bool rx_vote; /* Clock must be on for RX */
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700143 struct timer_list tx_idle_timer;
144 u32 tx_idle_delay;
145 struct timer_list wake_retrans_timer;
146 u32 wake_retrans;
147 struct workqueue_struct *workqueue;
148 struct work_struct ws_awake_rx;
149 struct work_struct ws_awake_device;
150 struct work_struct ws_rx_vote_off;
151 struct work_struct ws_tx_vote_off;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530152 struct work_struct ctrl_memdump_evt;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530153 struct delayed_work ctrl_memdump_timeout;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530154 struct qca_memdump_data *qca_memdump;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700155 unsigned long flags;
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -0700156 struct completion drop_ev_comp;
Claire Chang41d5b252019-10-31 18:46:14 +0800157 wait_queue_head_t suspend_wait_q;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530158 enum qca_memdump_states memdump_state;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530159 struct mutex hci_memdump_lock;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700160
161 /* For debugging purpose */
162 u64 ibs_sent_wacks;
163 u64 ibs_sent_slps;
164 u64 ibs_sent_wakes;
165 u64 ibs_recv_wacks;
166 u64 ibs_recv_slps;
167 u64 ibs_recv_wakes;
168 u64 vote_last_jif;
169 u32 vote_on_ms;
170 u32 vote_off_ms;
171 u64 tx_votes_on;
172 u64 rx_votes_on;
173 u64 tx_votes_off;
174 u64 rx_votes_off;
175 u64 votes_on;
176 u64 votes_off;
177};
178
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +0530179enum qca_speed_type {
180 QCA_INIT_SPEED = 1,
181 QCA_OPER_SPEED
182};
183
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530184/*
185 * Voltage regulator information required for configuring the
186 * QCA Bluetooth chipset
187 */
188struct qca_vreg {
189 const char *name;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530190 unsigned int load_uA;
191};
192
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -0700193struct qca_device_data {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530194 enum qca_btsoc_type soc_type;
195 struct qca_vreg *vregs;
196 size_t num_vregs;
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -0700197 uint32_t capabilities;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530198};
199
200/*
201 * Platform data for the QCA Bluetooth power driver.
202 */
203struct qca_power {
204 struct device *dev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530205 struct regulator_bulk_data *vreg_bulk;
Bjorn Andersson163d42f2019-10-17 22:24:03 -0700206 int num_vregs;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530207 bool vregs_on;
208};
209
Thierry Escande05ba5332018-03-29 21:15:24 +0200210struct qca_serdev {
211 struct hci_uart serdev_hu;
212 struct gpio_desc *bt_en;
213 struct clk *susclk;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530214 enum qca_btsoc_type btsoc_type;
215 struct qca_power *bt_power;
216 u32 init_speed;
217 u32 oper_speed;
Rocky Liao99c905c2019-06-06 17:40:30 +0800218 const char *firmware_name;
Thierry Escande05ba5332018-03-29 21:15:24 +0200219};
220
Bjorn Anderssona9314e72019-10-17 22:24:04 -0700221static int qca_regulator_enable(struct qca_serdev *qcadev);
222static void qca_regulator_disable(struct qca_serdev *qcadev);
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +0530223static void qca_power_shutdown(struct hci_uart *hu);
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +0530224static int qca_power_off(struct hci_dev *hdev);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530225static void qca_controller_memdump(struct work_struct *work);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530226
Matthias Kaehlcke4fdd5a42019-03-11 11:38:31 -0700227static enum qca_btsoc_type qca_soc_type(struct hci_uart *hu)
228{
229 enum qca_btsoc_type soc_type;
230
231 if (hu->serdev) {
232 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
233
234 soc_type = qsd->btsoc_type;
235 } else {
236 soc_type = QCA_ROME;
237 }
238
239 return soc_type;
240}
241
Rocky Liao99c905c2019-06-06 17:40:30 +0800242static const char *qca_get_firmware_name(struct hci_uart *hu)
243{
244 if (hu->serdev) {
245 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
246
247 return qsd->firmware_name;
248 } else {
249 return NULL;
250 }
251}
252
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700253static void __serial_clock_on(struct tty_struct *tty)
254{
255 /* TODO: Some chipset requires to enable UART clock on client
256 * side to save power consumption or manual work is required.
257 * Please put your code to control UART clock here if needed
258 */
259}
260
261static void __serial_clock_off(struct tty_struct *tty)
262{
263 /* TODO: Some chipset requires to disable UART clock on client
264 * side to save power consumption or manual work is required.
265 * Please put your code to control UART clock off here if needed
266 */
267}
268
269/* serial_clock_vote needs to be called with the ibs lock held */
270static void serial_clock_vote(unsigned long vote, struct hci_uart *hu)
271{
272 struct qca_data *qca = hu->priv;
273 unsigned int diff;
274
275 bool old_vote = (qca->tx_vote | qca->rx_vote);
276 bool new_vote;
277
278 switch (vote) {
279 case HCI_IBS_VOTE_STATS_UPDATE:
280 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
281
282 if (old_vote)
283 qca->vote_off_ms += diff;
284 else
285 qca->vote_on_ms += diff;
286 return;
287
288 case HCI_IBS_TX_VOTE_CLOCK_ON:
289 qca->tx_vote = true;
290 qca->tx_votes_on++;
291 new_vote = true;
292 break;
293
294 case HCI_IBS_RX_VOTE_CLOCK_ON:
295 qca->rx_vote = true;
296 qca->rx_votes_on++;
297 new_vote = true;
298 break;
299
300 case HCI_IBS_TX_VOTE_CLOCK_OFF:
301 qca->tx_vote = false;
302 qca->tx_votes_off++;
303 new_vote = qca->rx_vote | qca->tx_vote;
304 break;
305
306 case HCI_IBS_RX_VOTE_CLOCK_OFF:
307 qca->rx_vote = false;
308 qca->rx_votes_off++;
309 new_vote = qca->rx_vote | qca->tx_vote;
310 break;
311
312 default:
313 BT_ERR("Voting irregularity");
314 return;
315 }
316
317 if (new_vote != old_vote) {
318 if (new_vote)
319 __serial_clock_on(hu->tty);
320 else
321 __serial_clock_off(hu->tty);
322
Prasanna Karthikce26d812015-09-15 12:19:45 +0000323 BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false",
324 vote ? "true" : "false");
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700325
326 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
327
328 if (new_vote) {
329 qca->votes_on++;
330 qca->vote_off_ms += diff;
331 } else {
332 qca->votes_off++;
333 qca->vote_on_ms += diff;
334 }
335 qca->vote_last_jif = jiffies;
336 }
337}
338
339/* Builds and sends an HCI_IBS command packet.
340 * These are very simple packets with only 1 cmd byte.
341 */
342static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
343{
344 int err = 0;
345 struct sk_buff *skb = NULL;
346 struct qca_data *qca = hu->priv;
347
348 BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd);
349
350 skb = bt_skb_alloc(1, GFP_ATOMIC);
351 if (!skb) {
352 BT_ERR("Failed to allocate memory for HCI_IBS packet");
353 return -ENOMEM;
354 }
355
356 /* Assign HCI_IBS type */
Johannes Berg634fef62017-06-16 14:29:24 +0200357 skb_put_u8(skb, cmd);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700358
359 skb_queue_tail(&qca->txq, skb);
360
361 return err;
362}
363
364static void qca_wq_awake_device(struct work_struct *work)
365{
366 struct qca_data *qca = container_of(work, struct qca_data,
367 ws_awake_device);
368 struct hci_uart *hu = qca->hu;
369 unsigned long retrans_delay;
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530370 unsigned long flags;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700371
372 BT_DBG("hu %p wq awake device", hu);
373
374 /* Vote for serial clock */
375 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
376
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530377 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700378
379 /* Send wake indication to device */
380 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
381 BT_ERR("Failed to send WAKE to device");
382
383 qca->ibs_sent_wakes++;
384
385 /* Start retransmit timer */
386 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
387 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
388
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530389 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700390
391 /* Actually send the packets */
392 hci_uart_tx_wakeup(hu);
393}
394
395static void qca_wq_awake_rx(struct work_struct *work)
396{
397 struct qca_data *qca = container_of(work, struct qca_data,
398 ws_awake_rx);
399 struct hci_uart *hu = qca->hu;
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530400 unsigned long flags;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700401
402 BT_DBG("hu %p wq awake rx", hu);
403
404 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
405
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530406 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700407 qca->rx_ibs_state = HCI_IBS_RX_AWAKE;
408
409 /* Always acknowledge device wake up,
410 * sending IBS message doesn't count as TX ON.
411 */
412 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
413 BT_ERR("Failed to acknowledge device wake up");
414
415 qca->ibs_sent_wacks++;
416
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530417 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700418
419 /* Actually send the packets */
420 hci_uart_tx_wakeup(hu);
421}
422
423static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work)
424{
425 struct qca_data *qca = container_of(work, struct qca_data,
426 ws_rx_vote_off);
427 struct hci_uart *hu = qca->hu;
428
429 BT_DBG("hu %p rx clock vote off", hu);
430
431 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
432}
433
434static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work)
435{
436 struct qca_data *qca = container_of(work, struct qca_data,
437 ws_tx_vote_off);
438 struct hci_uart *hu = qca->hu;
439
440 BT_DBG("hu %p tx clock vote off", hu);
441
442 /* Run HCI tx handling unlocked */
443 hci_uart_tx_wakeup(hu);
444
445 /* Now that message queued to tty driver, vote for tty clocks off.
446 * It is up to the tty driver to pend the clocks off until tx done.
447 */
448 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
449}
450
Kees Cook04356052017-10-04 17:54:29 -0700451static void hci_ibs_tx_idle_timeout(struct timer_list *t)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700452{
Kees Cook04356052017-10-04 17:54:29 -0700453 struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
454 struct hci_uart *hu = qca->hu;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700455 unsigned long flags;
456
457 BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state);
458
459 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
460 flags, SINGLE_DEPTH_NESTING);
461
462 switch (qca->tx_ibs_state) {
463 case HCI_IBS_TX_AWAKE:
464 /* TX_IDLE, go to SLEEP */
465 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
466 BT_ERR("Failed to send SLEEP to device");
467 break;
468 }
469 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
470 qca->ibs_sent_slps++;
471 queue_work(qca->workqueue, &qca->ws_tx_vote_off);
472 break;
473
474 case HCI_IBS_TX_ASLEEP:
475 case HCI_IBS_TX_WAKING:
476 /* Fall through */
477
478 default:
Colin Ian Kinge059a462017-02-17 19:58:10 +0000479 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700480 break;
481 }
482
483 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
484}
485
Kees Cook04356052017-10-04 17:54:29 -0700486static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700487{
Kees Cook04356052017-10-04 17:54:29 -0700488 struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
489 struct hci_uart *hu = qca->hu;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700490 unsigned long flags, retrans_delay;
Prasanna Karthika9137182015-09-28 08:03:24 +0000491 bool retransmit = false;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700492
493 BT_DBG("hu %p wake retransmit timeout in %d state",
494 hu, qca->tx_ibs_state);
495
496 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
497 flags, SINGLE_DEPTH_NESTING);
498
Claire Chang41d5b252019-10-31 18:46:14 +0800499 /* Don't retransmit the HCI_IBS_WAKE_IND when suspending. */
500 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
501 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
502 return;
503 }
504
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700505 switch (qca->tx_ibs_state) {
506 case HCI_IBS_TX_WAKING:
507 /* No WAKE_ACK, retransmit WAKE */
Prasanna Karthika9137182015-09-28 08:03:24 +0000508 retransmit = true;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700509 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
510 BT_ERR("Failed to acknowledge device wake up");
511 break;
512 }
513 qca->ibs_sent_wakes++;
514 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
515 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
516 break;
517
518 case HCI_IBS_TX_ASLEEP:
519 case HCI_IBS_TX_AWAKE:
520 /* Fall through */
521
522 default:
Colin Ian Kinge059a462017-02-17 19:58:10 +0000523 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700524 break;
525 }
526
527 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
528
529 if (retransmit)
530 hci_uart_tx_wakeup(hu);
531}
532
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530533
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530534static void qca_controller_memdump_timeout(struct work_struct *work)
535{
536 struct qca_data *qca = container_of(work, struct qca_data,
537 ctrl_memdump_timeout.work);
538 struct hci_uart *hu = qca->hu;
539
540 mutex_lock(&qca->hci_memdump_lock);
541 if (test_bit(QCA_MEMDUMP_COLLECTION, &qca->flags)) {
542 qca->memdump_state = QCA_MEMDUMP_TIMEOUT;
543 if (!test_bit(QCA_HW_ERROR_EVENT, &qca->flags)) {
544 /* Inject hw error event to reset the device
545 * and driver.
546 */
547 hci_reset_dev(hu->hdev);
548 }
549 }
550
551 mutex_unlock(&qca->hci_memdump_lock);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530552}
553
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530554
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700555/* Initialize protocol */
556static int qca_open(struct hci_uart *hu)
557{
Thierry Escande05ba5332018-03-29 21:15:24 +0200558 struct qca_serdev *qcadev;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700559 struct qca_data *qca;
560
561 BT_DBG("hu %p qca_open", hu);
562
Vladis Dronovb36a1552019-07-30 11:33:45 +0200563 if (!hci_uart_has_flow_control(hu))
564 return -EOPNOTSUPP;
565
Jia-Ju Bai25a13e382018-07-23 11:56:51 +0800566 qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700567 if (!qca)
568 return -ENOMEM;
569
570 skb_queue_head_init(&qca->txq);
571 skb_queue_head_init(&qca->tx_wait_q);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530572 skb_queue_head_init(&qca->rx_memdump_q);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700573 spin_lock_init(&qca->hci_ibs_lock);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530574 mutex_init(&qca->hci_memdump_lock);
Bhaktipriya Shridharfac9a602016-08-30 22:42:53 +0530575 qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700576 if (!qca->workqueue) {
577 BT_ERR("QCA Workqueue not initialized properly");
578 kfree(qca);
579 return -ENOMEM;
580 }
581
582 INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
583 INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
584 INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
585 INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530586 INIT_WORK(&qca->ctrl_memdump_evt, qca_controller_memdump);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530587 INIT_DELAYED_WORK(&qca->ctrl_memdump_timeout,
588 qca_controller_memdump_timeout);
Claire Chang41d5b252019-10-31 18:46:14 +0800589 init_waitqueue_head(&qca->suspend_wait_q);
590
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700591 qca->hu = hu;
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -0700592 init_completion(&qca->drop_ev_comp);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700593
594 /* Assume we start with both sides asleep -- extra wakes OK */
595 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
596 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
597
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700598 qca->vote_last_jif = jiffies;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700599
600 hu->priv = qca;
601
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530602 if (hu->serdev) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530603 qcadev = serdev_device_get_drvdata(hu->serdev);
Christian Hewitt37aee132020-04-23 01:34:30 +0000604
605 if (qca_is_wcn399x(qcadev->btsoc_type))
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530606 hu->init_speed = qcadev->init_speed;
Christian Hewitt37aee132020-04-23 01:34:30 +0000607
608 if (qcadev->oper_speed)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530609 hu->oper_speed = qcadev->oper_speed;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530610 }
611
Kees Cook04356052017-10-04 17:54:29 -0700612 timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700613 qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
614
Kees Cook04356052017-10-04 17:54:29 -0700615 timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
Claire Chang41d5b252019-10-31 18:46:14 +0800616 qca->tx_idle_delay = IBS_HOST_TX_IDLE_TIMEOUT_MS;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700617
618 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
619 qca->tx_idle_delay, qca->wake_retrans);
620
621 return 0;
622}
623
624static void qca_debugfs_init(struct hci_dev *hdev)
625{
626 struct hci_uart *hu = hci_get_drvdata(hdev);
627 struct qca_data *qca = hu->priv;
628 struct dentry *ibs_dir;
629 umode_t mode;
630
631 if (!hdev->debugfs)
632 return;
633
634 ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
635
636 /* read only */
637 mode = S_IRUGO;
638 debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
639 debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
640 debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
641 &qca->ibs_sent_slps);
642 debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
643 &qca->ibs_sent_wakes);
644 debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
645 &qca->ibs_sent_wacks);
646 debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
647 &qca->ibs_recv_slps);
648 debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
649 &qca->ibs_recv_wakes);
650 debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
651 &qca->ibs_recv_wacks);
Ben YoungTae Kim10be6c02015-08-13 22:09:42 -0700652 debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700653 debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
654 debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
Ben YoungTae Kim10be6c02015-08-13 22:09:42 -0700655 debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700656 debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
657 debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
658 debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
659 debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
660 debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
661 debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
662
663 /* read/write */
664 mode = S_IRUGO | S_IWUSR;
665 debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
666 debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
667 &qca->tx_idle_delay);
668}
669
670/* Flush protocol data */
671static int qca_flush(struct hci_uart *hu)
672{
673 struct qca_data *qca = hu->priv;
674
675 BT_DBG("hu %p qca flush", hu);
676
677 skb_queue_purge(&qca->tx_wait_q);
678 skb_queue_purge(&qca->txq);
679
680 return 0;
681}
682
683/* Close protocol */
684static int qca_close(struct hci_uart *hu)
685{
686 struct qca_data *qca = hu->priv;
687
688 BT_DBG("hu %p qca close", hu);
689
690 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
691
692 skb_queue_purge(&qca->tx_wait_q);
693 skb_queue_purge(&qca->txq);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530694 skb_queue_purge(&qca->rx_memdump_q);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700695 del_timer(&qca->tx_idle_timer);
696 del_timer(&qca->wake_retrans_timer);
697 destroy_workqueue(qca->workqueue);
698 qca->hu = NULL;
699
Rocky Liao55599042020-01-15 16:55:50 +0800700 qca_power_shutdown(hu);
Thierry Escande05ba5332018-03-29 21:15:24 +0200701
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700702 kfree_skb(qca->rx_skb);
703
704 hu->priv = NULL;
705
706 kfree(qca);
707
708 return 0;
709}
710
711/* Called upon a wake-up-indication from the device.
712 */
713static void device_want_to_wakeup(struct hci_uart *hu)
714{
715 unsigned long flags;
716 struct qca_data *qca = hu->priv;
717
718 BT_DBG("hu %p want to wake up", hu);
719
720 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
721
722 qca->ibs_recv_wakes++;
723
Claire Chang41d5b252019-10-31 18:46:14 +0800724 /* Don't wake the rx up when suspending. */
725 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
726 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
727 return;
728 }
729
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700730 switch (qca->rx_ibs_state) {
731 case HCI_IBS_RX_ASLEEP:
732 /* Make sure clock is on - we may have turned clock off since
733 * receiving the wake up indicator awake rx clock.
734 */
735 queue_work(qca->workqueue, &qca->ws_awake_rx);
736 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
737 return;
738
739 case HCI_IBS_RX_AWAKE:
740 /* Always acknowledge device wake up,
741 * sending IBS message doesn't count as TX ON.
742 */
743 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
744 BT_ERR("Failed to acknowledge device wake up");
745 break;
746 }
747 qca->ibs_sent_wacks++;
748 break;
749
750 default:
751 /* Any other state is illegal */
752 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
753 qca->rx_ibs_state);
754 break;
755 }
756
757 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
758
759 /* Actually send the packets */
760 hci_uart_tx_wakeup(hu);
761}
762
763/* Called upon a sleep-indication from the device.
764 */
765static void device_want_to_sleep(struct hci_uart *hu)
766{
767 unsigned long flags;
768 struct qca_data *qca = hu->priv;
769
Rocky Liao6600c082019-08-14 15:42:39 +0800770 BT_DBG("hu %p want to sleep in %d state", hu, qca->rx_ibs_state);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700771
772 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
773
774 qca->ibs_recv_slps++;
775
776 switch (qca->rx_ibs_state) {
777 case HCI_IBS_RX_AWAKE:
778 /* Update state */
779 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
780 /* Vote off rx clock under workqueue */
781 queue_work(qca->workqueue, &qca->ws_rx_vote_off);
782 break;
783
784 case HCI_IBS_RX_ASLEEP:
Rocky Liao6600c082019-08-14 15:42:39 +0800785 break;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700786
787 default:
788 /* Any other state is illegal */
789 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
790 qca->rx_ibs_state);
791 break;
792 }
793
Claire Chang41d5b252019-10-31 18:46:14 +0800794 wake_up_interruptible(&qca->suspend_wait_q);
795
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700796 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
797}
798
799/* Called upon wake-up-acknowledgement from the device
800 */
801static void device_woke_up(struct hci_uart *hu)
802{
803 unsigned long flags, idle_delay;
804 struct qca_data *qca = hu->priv;
805 struct sk_buff *skb = NULL;
806
807 BT_DBG("hu %p woke up", hu);
808
809 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
810
811 qca->ibs_recv_wacks++;
812
Claire Chang41d5b252019-10-31 18:46:14 +0800813 /* Don't react to the wake-up-acknowledgment when suspending. */
814 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
815 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
816 return;
817 }
818
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700819 switch (qca->tx_ibs_state) {
820 case HCI_IBS_TX_AWAKE:
821 /* Expect one if we send 2 WAKEs */
822 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
823 qca->tx_ibs_state);
824 break;
825
826 case HCI_IBS_TX_WAKING:
827 /* Send pending packets */
828 while ((skb = skb_dequeue(&qca->tx_wait_q)))
829 skb_queue_tail(&qca->txq, skb);
830
831 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
832 del_timer(&qca->wake_retrans_timer);
833 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
834 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
835 qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
836 break;
837
838 case HCI_IBS_TX_ASLEEP:
839 /* Fall through */
840
841 default:
842 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
843 qca->tx_ibs_state);
844 break;
845 }
846
847 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
848
849 /* Actually send the packets */
850 hci_uart_tx_wakeup(hu);
851}
852
853/* Enqueue frame for transmittion (padding, crc, etc) may be called from
854 * two simultaneous tasklets.
855 */
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
864 /* Prepend skb with frame type */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100865 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700866
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +0530867 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
868
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700869 /* Don't go to sleep in middle of patch download or
870 * Out-Of-Band(GPIOs control) sleep is selected.
Claire Chang41d5b252019-10-31 18:46:14 +0800871 * Don't wake the device up when suspending.
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700872 */
Claire Chang41d5b252019-10-31 18:46:14 +0800873 if (!test_bit(QCA_IBS_ENABLED, &qca->flags) ||
874 test_bit(QCA_SUSPENDING, &qca->flags)) {
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700875 skb_queue_tail(&qca->txq, skb);
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +0530876 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700877 return 0;
878 }
879
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700880 /* Act according to current state */
881 switch (qca->tx_ibs_state) {
882 case HCI_IBS_TX_AWAKE:
883 BT_DBG("Device awake, sending normally");
884 skb_queue_tail(&qca->txq, skb);
885 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
886 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
887 break;
888
889 case HCI_IBS_TX_ASLEEP:
890 BT_DBG("Device asleep, waking up and queueing packet");
891 /* Save packet for later */
892 skb_queue_tail(&qca->tx_wait_q, skb);
893
894 qca->tx_ibs_state = HCI_IBS_TX_WAKING;
895 /* Schedule a work queue to wake up device */
896 queue_work(qca->workqueue, &qca->ws_awake_device);
897 break;
898
899 case HCI_IBS_TX_WAKING:
900 BT_DBG("Device waking up, queueing packet");
901 /* Transient state; just keep packet for later */
902 skb_queue_tail(&qca->tx_wait_q, skb);
903 break;
904
905 default:
906 BT_ERR("Illegal tx state: %d (losing packet)",
907 qca->tx_ibs_state);
908 kfree_skb(skb);
909 break;
910 }
911
912 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
913
914 return 0;
915}
916
917static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
918{
919 struct hci_uart *hu = hci_get_drvdata(hdev);
920
921 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
922
923 device_want_to_sleep(hu);
924
925 kfree_skb(skb);
926 return 0;
927}
928
929static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
930{
931 struct hci_uart *hu = hci_get_drvdata(hdev);
932
933 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
934
935 device_want_to_wakeup(hu);
936
937 kfree_skb(skb);
938 return 0;
939}
940
941static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
942{
943 struct hci_uart *hu = hci_get_drvdata(hdev);
944
945 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
946
947 device_woke_up(hu);
948
949 kfree_skb(skb);
950 return 0;
951}
952
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +0530953static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb)
954{
955 /* We receive debug logs from chip as an ACL packets.
956 * Instead of sending the data to ACL to decode the
957 * received data, we are pushing them to the above layers
958 * as a diagnostic packet.
959 */
960 if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE)
961 return hci_recv_diag(hdev, skb);
962
963 return hci_recv_frame(hdev, skb);
964}
965
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530966static void qca_controller_memdump(struct work_struct *work)
967{
968 struct qca_data *qca = container_of(work, struct qca_data,
969 ctrl_memdump_evt);
970 struct hci_uart *hu = qca->hu;
971 struct sk_buff *skb;
972 struct qca_memdump_event_hdr *cmd_hdr;
973 struct qca_memdump_data *qca_memdump = qca->qca_memdump;
974 struct qca_dump_size *dump;
975 char *memdump_buf;
976 char nullBuff[QCA_DUMP_PACKET_SIZE] = { 0 };
YueHaibing56b084e2020-01-08 01:54:31 +0000977 u16 seq_no;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530978 u32 dump_size;
979
980 while ((skb = skb_dequeue(&qca->rx_memdump_q))) {
981
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530982 mutex_lock(&qca->hci_memdump_lock);
983 /* Skip processing the received packets if timeout detected. */
984 if (qca->memdump_state == QCA_MEMDUMP_TIMEOUT) {
985 mutex_unlock(&qca->hci_memdump_lock);
986 return;
987 }
988
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530989 if (!qca_memdump) {
990 qca_memdump = kzalloc(sizeof(struct qca_memdump_data),
991 GFP_ATOMIC);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530992 if (!qca_memdump) {
993 mutex_unlock(&qca->hci_memdump_lock);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530994 return;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530995 }
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530996
997 qca->qca_memdump = qca_memdump;
998 }
999
1000 qca->memdump_state = QCA_MEMDUMP_COLLECTING;
1001 cmd_hdr = (void *) skb->data;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301002 seq_no = __le16_to_cpu(cmd_hdr->seq_no);
1003 skb_pull(skb, sizeof(struct qca_memdump_event_hdr));
1004
1005 if (!seq_no) {
1006
1007 /* This is the first frame of memdump packet from
1008 * the controller, Disable IBS to recevie dump
1009 * with out any interruption, ideally time required for
1010 * the controller to send the dump is 8 seconds. let us
1011 * start timer to handle this asynchronous activity.
1012 */
1013 clear_bit(QCA_IBS_ENABLED, &qca->flags);
1014 set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1015 dump = (void *) skb->data;
1016 dump_size = __le32_to_cpu(dump->dump_size);
1017 if (!(dump_size)) {
1018 bt_dev_err(hu->hdev, "Rx invalid memdump size");
1019 kfree_skb(skb);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301020 mutex_unlock(&qca->hci_memdump_lock);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301021 return;
1022 }
1023
1024 bt_dev_info(hu->hdev, "QCA collecting dump of size:%u",
1025 dump_size);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301026 queue_delayed_work(qca->workqueue,
1027 &qca->ctrl_memdump_timeout,
1028 msecs_to_jiffies(MEMDUMP_TIMEOUT_MS));
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301029
1030 skb_pull(skb, sizeof(dump_size));
1031 memdump_buf = vmalloc(dump_size);
1032 qca_memdump->memdump_buf_head = memdump_buf;
1033 qca_memdump->memdump_buf_tail = memdump_buf;
1034 }
1035
1036 memdump_buf = qca_memdump->memdump_buf_tail;
1037
1038 /* If sequence no 0 is missed then there is no point in
1039 * accepting the other sequences.
1040 */
1041 if (!memdump_buf) {
1042 bt_dev_err(hu->hdev, "QCA: Discarding other packets");
1043 kfree(qca_memdump);
1044 kfree_skb(skb);
1045 qca->qca_memdump = NULL;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301046 mutex_unlock(&qca->hci_memdump_lock);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301047 return;
1048 }
1049
1050 /* There could be chance of missing some packets from
1051 * the controller. In such cases let us store the dummy
1052 * packets in the buffer.
1053 */
1054 while ((seq_no > qca_memdump->current_seq_no + 1) &&
1055 seq_no != QCA_LAST_SEQUENCE_NUM) {
1056 bt_dev_err(hu->hdev, "QCA controller missed packet:%d",
1057 qca_memdump->current_seq_no);
1058 memcpy(memdump_buf, nullBuff, QCA_DUMP_PACKET_SIZE);
1059 memdump_buf = memdump_buf + QCA_DUMP_PACKET_SIZE;
1060 qca_memdump->received_dump += QCA_DUMP_PACKET_SIZE;
1061 qca_memdump->current_seq_no++;
1062 }
1063
1064 memcpy(memdump_buf, (unsigned char *) skb->data, skb->len);
1065 memdump_buf = memdump_buf + skb->len;
1066 qca_memdump->memdump_buf_tail = memdump_buf;
1067 qca_memdump->current_seq_no = seq_no + 1;
1068 qca_memdump->received_dump += skb->len;
1069 qca->qca_memdump = qca_memdump;
1070 kfree_skb(skb);
1071 if (seq_no == QCA_LAST_SEQUENCE_NUM) {
1072 bt_dev_info(hu->hdev, "QCA writing crash dump of size %d bytes",
1073 qca_memdump->received_dump);
1074 memdump_buf = qca_memdump->memdump_buf_head;
1075 dev_coredumpv(&hu->serdev->dev, memdump_buf,
1076 qca_memdump->received_dump, GFP_KERNEL);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301077 cancel_delayed_work(&qca->ctrl_memdump_timeout);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301078 kfree(qca->qca_memdump);
1079 qca->qca_memdump = NULL;
1080 qca->memdump_state = QCA_MEMDUMP_COLLECTED;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301081 clear_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301082 }
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301083
1084 mutex_unlock(&qca->hci_memdump_lock);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301085 }
1086
1087}
1088
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301089static int qca_controller_memdump_event(struct hci_dev *hdev,
1090 struct sk_buff *skb)
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301091{
1092 struct hci_uart *hu = hci_get_drvdata(hdev);
1093 struct qca_data *qca = hu->priv;
1094
1095 skb_queue_tail(&qca->rx_memdump_q, skb);
1096 queue_work(qca->workqueue, &qca->ctrl_memdump_evt);
1097
1098 return 0;
1099}
1100
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001101static int qca_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
1102{
1103 struct hci_uart *hu = hci_get_drvdata(hdev);
1104 struct qca_data *qca = hu->priv;
1105
1106 if (test_bit(QCA_DROP_VENDOR_EVENT, &qca->flags)) {
1107 struct hci_event_hdr *hdr = (void *)skb->data;
1108
1109 /* For the WCN3990 the vendor command for a baudrate change
1110 * isn't sent as synchronous HCI command, because the
1111 * controller sends the corresponding vendor event with the
1112 * new baudrate. The event is received and properly decoded
1113 * after changing the baudrate of the host port. It needs to
1114 * be dropped, otherwise it can be misinterpreted as
1115 * response to a later firmware download command (also a
1116 * vendor command).
1117 */
1118
1119 if (hdr->evt == HCI_EV_VENDOR)
1120 complete(&qca->drop_ev_comp);
1121
Wei Yongjun4974c832019-07-09 01:35:30 +00001122 kfree_skb(skb);
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001123
1124 return 0;
1125 }
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301126 /* We receive chip memory dump as an event packet, With a dedicated
1127 * handler followed by a hardware error event. When this event is
1128 * received we store dump into a file before closing hci. This
1129 * dump will help in triaging the issues.
1130 */
1131 if ((skb->data[0] == HCI_VENDOR_PKT) &&
1132 (get_unaligned_be16(skb->data + 2) == QCA_SSR_DUMP_HANDLE))
1133 return qca_controller_memdump_event(hdev, skb);
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001134
1135 return hci_recv_frame(hdev, skb);
1136}
1137
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001138#define QCA_IBS_SLEEP_IND_EVENT \
1139 .type = HCI_IBS_SLEEP_IND, \
1140 .hlen = 0, \
1141 .loff = 0, \
1142 .lsize = 0, \
1143 .maxlen = HCI_MAX_IBS_SIZE
1144
1145#define QCA_IBS_WAKE_IND_EVENT \
1146 .type = HCI_IBS_WAKE_IND, \
1147 .hlen = 0, \
1148 .loff = 0, \
1149 .lsize = 0, \
1150 .maxlen = HCI_MAX_IBS_SIZE
1151
1152#define QCA_IBS_WAKE_ACK_EVENT \
1153 .type = HCI_IBS_WAKE_ACK, \
1154 .hlen = 0, \
1155 .loff = 0, \
1156 .lsize = 0, \
1157 .maxlen = HCI_MAX_IBS_SIZE
1158
1159static const struct h4_recv_pkt qca_recv_pkts[] = {
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +05301160 { H4_RECV_ACL, .recv = qca_recv_acl_data },
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001161 { H4_RECV_SCO, .recv = hci_recv_frame },
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001162 { H4_RECV_EVENT, .recv = qca_recv_event },
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001163 { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind },
1164 { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack },
1165 { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
1166};
1167
1168static int qca_recv(struct hci_uart *hu, const void *data, int count)
1169{
1170 struct qca_data *qca = hu->priv;
1171
1172 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
1173 return -EUNATCH;
1174
1175 qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
1176 qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
1177 if (IS_ERR(qca->rx_skb)) {
1178 int err = PTR_ERR(qca->rx_skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001179 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001180 qca->rx_skb = NULL;
1181 return err;
1182 }
1183
1184 return count;
1185}
1186
1187static struct sk_buff *qca_dequeue(struct hci_uart *hu)
1188{
1189 struct qca_data *qca = hu->priv;
1190
1191 return skb_dequeue(&qca->txq);
1192}
1193
1194static uint8_t qca_get_baudrate_value(int speed)
1195{
Prasanna Karthikce26d812015-09-15 12:19:45 +00001196 switch (speed) {
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001197 case 9600:
1198 return QCA_BAUDRATE_9600;
1199 case 19200:
1200 return QCA_BAUDRATE_19200;
1201 case 38400:
1202 return QCA_BAUDRATE_38400;
1203 case 57600:
1204 return QCA_BAUDRATE_57600;
1205 case 115200:
1206 return QCA_BAUDRATE_115200;
1207 case 230400:
1208 return QCA_BAUDRATE_230400;
1209 case 460800:
1210 return QCA_BAUDRATE_460800;
1211 case 500000:
1212 return QCA_BAUDRATE_500000;
1213 case 921600:
1214 return QCA_BAUDRATE_921600;
1215 case 1000000:
1216 return QCA_BAUDRATE_1000000;
1217 case 2000000:
1218 return QCA_BAUDRATE_2000000;
1219 case 3000000:
1220 return QCA_BAUDRATE_3000000;
Balakrishna Godavarthibe93a492018-08-03 17:46:30 +05301221 case 3200000:
1222 return QCA_BAUDRATE_3200000;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001223 case 3500000:
1224 return QCA_BAUDRATE_3500000;
1225 default:
1226 return QCA_BAUDRATE_115200;
1227 }
1228}
1229
1230static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
1231{
1232 struct hci_uart *hu = hci_get_drvdata(hdev);
1233 struct qca_data *qca = hu->priv;
1234 struct sk_buff *skb;
1235 u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
1236
Balakrishna Godavarthibe93a492018-08-03 17:46:30 +05301237 if (baudrate > QCA_BAUDRATE_3200000)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001238 return -EINVAL;
1239
1240 cmd[4] = baudrate;
1241
Jia-Ju Bai25a13e382018-07-23 11:56:51 +08001242 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001243 if (!skb) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001244 bt_dev_err(hdev, "Failed to allocate baudrate packet");
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001245 return -ENOMEM;
1246 }
1247
1248 /* Assign commands to change baudrate and packet type. */
Johannes Berg59ae1d12017-06-16 14:29:20 +02001249 skb_put_data(skb, cmd, sizeof(cmd));
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001250 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001251
1252 skb_queue_tail(&qca->txq, skb);
1253 hci_uart_tx_wakeup(hu);
1254
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001255 /* Wait for the baudrate change request to be sent */
1256
1257 while (!skb_queue_empty(&qca->txq))
1258 usleep_range(100, 200);
1259
Matthias Kaehlckeecf2b762019-04-23 11:16:52 -07001260 if (hu->serdev)
1261 serdev_device_wait_until_sent(hu->serdev,
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001262 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1263
1264 /* Give the controller time to process the request */
Harish Bandi523760b2019-04-26 19:26:01 +05301265 if (qca_is_wcn399x(qca_soc_type(hu)))
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001266 msleep(10);
1267 else
1268 msleep(300);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001269
1270 return 0;
1271}
1272
Thierry Escande05ba5332018-03-29 21:15:24 +02001273static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
1274{
1275 if (hu->serdev)
1276 serdev_device_set_baudrate(hu->serdev, speed);
1277 else
1278 hci_uart_set_baudrate(hu, speed);
1279}
1280
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001281static int qca_send_power_pulse(struct hci_uart *hu, bool on)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301282{
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301283 int ret;
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001284 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001285 u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301286
1287 /* These power pulses are single byte command which are sent
1288 * at required baudrate to wcn3990. On wcn3990, we have an external
1289 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1290 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1291 * and also we use the same power inputs to turn on and off for
1292 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1293 * we send a power on pulse at 115200 bps. This algorithm will help to
1294 * save power. Disabling hardware flow control is mandatory while
1295 * sending power pulses to SoC.
1296 */
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301297 bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301298
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301299 serdev_device_write_flush(hu->serdev);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301300 hci_uart_set_flow_control(hu, true);
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301301 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1302 if (ret < 0) {
1303 bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd);
1304 return ret;
1305 }
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301306
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301307 serdev_device_wait_until_sent(hu->serdev, timeout);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301308 hci_uart_set_flow_control(hu, false);
1309
Matthias Kaehlcke0ebcddd2019-02-26 11:46:47 -08001310 /* Give to controller time to boot/shutdown */
Matthias Kaehlckead571d72019-02-26 11:46:46 -08001311 if (on)
1312 msleep(100);
Matthias Kaehlcke0ebcddd2019-02-26 11:46:47 -08001313 else
1314 msleep(10);
Matthias Kaehlckead571d72019-02-26 11:46:46 -08001315
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301316 return 0;
1317}
1318
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301319static unsigned int qca_get_speed(struct hci_uart *hu,
1320 enum qca_speed_type speed_type)
1321{
1322 unsigned int speed = 0;
1323
1324 if (speed_type == QCA_INIT_SPEED) {
1325 if (hu->init_speed)
1326 speed = hu->init_speed;
1327 else if (hu->proto->init_speed)
1328 speed = hu->proto->init_speed;
1329 } else {
1330 if (hu->oper_speed)
1331 speed = hu->oper_speed;
1332 else if (hu->proto->oper_speed)
1333 speed = hu->proto->oper_speed;
1334 }
1335
1336 return speed;
1337}
1338
1339static int qca_check_speeds(struct hci_uart *hu)
1340{
Harish Bandi523760b2019-04-26 19:26:01 +05301341 if (qca_is_wcn399x(qca_soc_type(hu))) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301342 if (!qca_get_speed(hu, QCA_INIT_SPEED) &&
1343 !qca_get_speed(hu, QCA_OPER_SPEED))
1344 return -EINVAL;
1345 } else {
1346 if (!qca_get_speed(hu, QCA_INIT_SPEED) ||
1347 !qca_get_speed(hu, QCA_OPER_SPEED))
1348 return -EINVAL;
1349 }
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301350
1351 return 0;
1352}
1353
1354static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type)
1355{
1356 unsigned int speed, qca_baudrate;
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001357 struct qca_data *qca = hu->priv;
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301358 int ret = 0;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301359
1360 if (speed_type == QCA_INIT_SPEED) {
1361 speed = qca_get_speed(hu, QCA_INIT_SPEED);
1362 if (speed)
1363 host_set_baudrate(hu, speed);
1364 } else {
Matthias Kaehlcke4fdd5a42019-03-11 11:38:31 -07001365 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1366
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301367 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1368 if (!speed)
1369 return 0;
1370
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301371 /* Disable flow control for wcn3990 to deassert RTS while
1372 * changing the baudrate of chip and host.
1373 */
Harish Bandi523760b2019-04-26 19:26:01 +05301374 if (qca_is_wcn399x(soc_type))
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301375 hci_uart_set_flow_control(hu, true);
1376
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001377 if (soc_type == QCA_WCN3990) {
1378 reinit_completion(&qca->drop_ev_comp);
1379 set_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1380 }
1381
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301382 qca_baudrate = qca_get_baudrate_value(speed);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301383 bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed);
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301384 ret = qca_set_baudrate(hu->hdev, qca_baudrate);
1385 if (ret)
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301386 goto error;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301387
1388 host_set_baudrate(hu, speed);
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301389
1390error:
Jeffrey Hugobba79fe2019-10-21 07:18:27 -07001391 if (qca_is_wcn399x(soc_type))
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301392 hci_uart_set_flow_control(hu, false);
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001393
1394 if (soc_type == QCA_WCN3990) {
1395 /* Wait for the controller to send the vendor event
1396 * for the baudrate change command.
1397 */
1398 if (!wait_for_completion_timeout(&qca->drop_ev_comp,
1399 msecs_to_jiffies(100))) {
1400 bt_dev_err(hu->hdev,
1401 "Failed to change controller baudrate\n");
1402 ret = -ETIMEDOUT;
1403 }
1404
1405 clear_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1406 }
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301407 }
1408
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301409 return ret;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301410}
1411
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301412static int qca_send_crashbuffer(struct hci_uart *hu)
1413{
1414 struct qca_data *qca = hu->priv;
1415 struct sk_buff *skb;
1416
1417 skb = bt_skb_alloc(QCA_CRASHBYTE_PACKET_LEN, GFP_KERNEL);
1418 if (!skb) {
1419 bt_dev_err(hu->hdev, "Failed to allocate memory for skb packet");
1420 return -ENOMEM;
1421 }
1422
1423 /* We forcefully crash the controller, by sending 0xfb byte for
1424 * 1024 times. We also might have chance of losing data, To be
1425 * on safer side we send 1096 bytes to the SoC.
1426 */
1427 memset(skb_put(skb, QCA_CRASHBYTE_PACKET_LEN), QCA_MEMDUMP_BYTE,
1428 QCA_CRASHBYTE_PACKET_LEN);
1429 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1430 bt_dev_info(hu->hdev, "crash the soc to collect controller dump");
1431 skb_queue_tail(&qca->txq, skb);
1432 hci_uart_tx_wakeup(hu);
1433
1434 return 0;
1435}
1436
1437static void qca_wait_for_dump_collection(struct hci_dev *hdev)
1438{
1439 struct hci_uart *hu = hci_get_drvdata(hdev);
1440 struct qca_data *qca = hu->priv;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301441
1442 wait_on_bit_timeout(&qca->flags, QCA_MEMDUMP_COLLECTION,
1443 TASK_UNINTERRUPTIBLE, MEMDUMP_TIMEOUT_MS);
1444
1445 clear_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301446}
1447
1448static void qca_hw_error(struct hci_dev *hdev, u8 code)
1449{
1450 struct hci_uart *hu = hci_get_drvdata(hdev);
1451 struct qca_data *qca = hu->priv;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301452 struct qca_memdump_data *qca_memdump = qca->qca_memdump;
1453 char *memdump_buf = NULL;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301454
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301455 set_bit(QCA_HW_ERROR_EVENT, &qca->flags);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301456 bt_dev_info(hdev, "mem_dump_status: %d", qca->memdump_state);
1457
1458 if (qca->memdump_state == QCA_MEMDUMP_IDLE) {
1459 /* If hardware error event received for other than QCA
1460 * soc memory dump event, then we need to crash the SOC
1461 * and wait here for 8 seconds to get the dump packets.
1462 * This will block main thread to be on hold until we
1463 * collect dump.
1464 */
1465 set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1466 qca_send_crashbuffer(hu);
1467 qca_wait_for_dump_collection(hdev);
1468 } else if (qca->memdump_state == QCA_MEMDUMP_COLLECTING) {
1469 /* Let us wait here until memory dump collected or
1470 * memory dump timer expired.
1471 */
1472 bt_dev_info(hdev, "waiting for dump to complete");
1473 qca_wait_for_dump_collection(hdev);
1474 }
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301475
1476 if (qca->memdump_state != QCA_MEMDUMP_COLLECTED) {
1477 bt_dev_err(hu->hdev, "clearing allocated memory due to memdump timeout");
1478 mutex_lock(&qca->hci_memdump_lock);
1479 if (qca_memdump)
1480 memdump_buf = qca_memdump->memdump_buf_head;
1481 vfree(memdump_buf);
1482 kfree(qca_memdump);
1483 qca->qca_memdump = NULL;
1484 qca->memdump_state = QCA_MEMDUMP_TIMEOUT;
1485 cancel_delayed_work(&qca->ctrl_memdump_timeout);
1486 skb_queue_purge(&qca->rx_memdump_q);
1487 mutex_unlock(&qca->hci_memdump_lock);
1488 cancel_work_sync(&qca->ctrl_memdump_evt);
1489 }
1490
1491 clear_bit(QCA_HW_ERROR_EVENT, &qca->flags);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301492}
1493
1494static void qca_cmd_timeout(struct hci_dev *hdev)
1495{
1496 struct hci_uart *hu = hci_get_drvdata(hdev);
1497 struct qca_data *qca = hu->priv;
1498
1499 if (qca->memdump_state == QCA_MEMDUMP_IDLE)
1500 qca_send_crashbuffer(hu);
1501 else
1502 bt_dev_info(hdev, "Dump collection is in process");
1503}
1504
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301505static int qca_wcn3990_init(struct hci_uart *hu)
1506{
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301507 struct qca_serdev *qcadev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301508 int ret;
1509
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301510 /* Check for vregs status, may be hci down has turned
1511 * off the voltage regulator.
1512 */
1513 qcadev = serdev_device_get_drvdata(hu->serdev);
1514 if (!qcadev->bt_power->vregs_on) {
1515 serdev_device_close(hu->serdev);
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001516 ret = qca_regulator_enable(qcadev);
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301517 if (ret)
1518 return ret;
1519
1520 ret = serdev_device_open(hu->serdev);
1521 if (ret) {
1522 bt_dev_err(hu->hdev, "failed to open port");
1523 return ret;
1524 }
1525 }
1526
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301527 /* Forcefully enable wcn3990 to enter in to boot mode. */
1528 host_set_baudrate(hu, 2400);
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001529 ret = qca_send_power_pulse(hu, false);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301530 if (ret)
1531 return ret;
1532
1533 qca_set_speed(hu, QCA_INIT_SPEED);
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001534 ret = qca_send_power_pulse(hu, true);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301535 if (ret)
1536 return ret;
1537
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301538 /* Now the device is in ready state to communicate with host.
1539 * To sync host with device we need to reopen port.
1540 * Without this, we will have RTS and CTS synchronization
1541 * issues.
1542 */
1543 serdev_device_close(hu->serdev);
1544 ret = serdev_device_open(hu->serdev);
1545 if (ret) {
1546 bt_dev_err(hu->hdev, "failed to open port");
1547 return ret;
1548 }
1549
1550 hci_uart_set_flow_control(hu, false);
1551
1552 return 0;
1553}
1554
Rocky Liao5e6d8402020-01-13 12:30:20 +08001555static int qca_power_on(struct hci_dev *hdev)
1556{
1557 struct hci_uart *hu = hci_get_drvdata(hdev);
1558 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1559 struct qca_serdev *qcadev;
1560 int ret = 0;
1561
1562 /* Non-serdev device usually is powered by external power
1563 * and don't need additional action in driver for power on
1564 */
1565 if (!hu->serdev)
1566 return 0;
1567
1568 if (qca_is_wcn399x(soc_type)) {
1569 ret = qca_wcn3990_init(hu);
1570 } else {
1571 qcadev = serdev_device_get_drvdata(hu->serdev);
Rocky Liao77131df2020-03-04 21:16:45 +08001572 if (qcadev->bt_en) {
Rocky Liao8a208b22020-03-04 09:54:29 +08001573 gpiod_set_value_cansleep(qcadev->bt_en, 1);
1574 /* Controller needs time to bootup. */
1575 msleep(150);
1576 }
Rocky Liao5e6d8402020-01-13 12:30:20 +08001577 }
1578
1579 return ret;
1580}
1581
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001582static int qca_setup(struct hci_uart *hu)
1583{
1584 struct hci_dev *hdev = hu->hdev;
1585 struct qca_data *qca = hu->priv;
1586 unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
Rocky Liaobb2500ab2020-01-15 16:55:51 +08001587 unsigned int retries = 0;
Matthias Kaehlcke4fdd5a42019-03-11 11:38:31 -07001588 enum qca_btsoc_type soc_type = qca_soc_type(hu);
Rocky Liao99c905c2019-06-06 17:40:30 +08001589 const char *firmware_name = qca_get_firmware_name(hu);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001590 int ret;
Balakrishna Godavarthiaadebac2018-08-03 17:46:28 +05301591 int soc_ver = 0;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001592
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301593 ret = qca_check_speeds(hu);
1594 if (ret)
1595 return ret;
1596
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001597 /* Patch downloading has to be done without IBS mode */
Matthias Kaehlcke62a91992019-04-29 16:21:30 -07001598 clear_bit(QCA_IBS_ENABLED, &qca->flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001599
Rocky Liaoe14c1672019-08-21 14:23:39 +08001600 /* Enable controller to do both LE scan and BR/EDR inquiry
1601 * simultaneously.
1602 */
1603 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1604
Rocky Liao5e6d8402020-01-13 12:30:20 +08001605 bt_dev_info(hdev, "setting up %s",
Rocky Liaoe5d64682020-03-25 10:26:37 +08001606 qca_is_wcn399x(soc_type) ? "wcn399x" : "ROME/QCA6390");
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301607
Rocky Liaobb2500ab2020-01-15 16:55:51 +08001608retry:
Rocky Liao5e6d8402020-01-13 12:30:20 +08001609 ret = qca_power_on(hdev);
1610 if (ret)
1611 return ret;
1612
1613 if (qca_is_wcn399x(soc_type)) {
Matthias Kaehlcke5971752d2019-02-19 12:05:59 -08001614 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301615
Balakrishna Godavarthi7d250a02019-11-06 15:18:32 +05301616 ret = qca_read_soc_version(hdev, &soc_ver, soc_type);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301617 if (ret)
1618 return ret;
1619 } else {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301620 qca_set_speed(hu, QCA_INIT_SPEED);
1621 }
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001622
1623 /* Setup user speed if needed */
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301624 speed = qca_get_speed(hu, QCA_OPER_SPEED);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001625 if (speed) {
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301626 ret = qca_set_speed(hu, QCA_OPER_SPEED);
1627 if (ret)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001628 return ret;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301629
1630 qca_baudrate = qca_get_baudrate_value(speed);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001631 }
1632
Harish Bandi523760b2019-04-26 19:26:01 +05301633 if (!qca_is_wcn399x(soc_type)) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301634 /* Get QCA version information */
Balakrishna Godavarthi7d250a02019-11-06 15:18:32 +05301635 ret = qca_read_soc_version(hdev, &soc_ver, soc_type);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301636 if (ret)
1637 return ret;
1638 }
Balakrishna Godavarthiaadebac2018-08-03 17:46:28 +05301639
1640 bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001641 /* Setup patch / NVM configurations */
Rocky Liao99c905c2019-06-06 17:40:30 +08001642 ret = qca_uart_setup(hdev, qca_baudrate, soc_type, soc_ver,
1643 firmware_name);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001644 if (!ret) {
Matthias Kaehlcke62a91992019-04-29 16:21:30 -07001645 set_bit(QCA_IBS_ENABLED, &qca->flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001646 qca_debugfs_init(hdev);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301647 hu->hdev->hw_error = qca_hw_error;
1648 hu->hdev->cmd_timeout = qca_cmd_timeout;
Loic Poulainba8f3592017-11-06 12:16:56 +01001649 } else if (ret == -ENOENT) {
1650 /* No patch/nvm-config found, run with original fw/config */
1651 ret = 0;
Amit Pundir7dc5fe02018-04-16 12:10:24 +05301652 } else if (ret == -EAGAIN) {
1653 /*
1654 * Userspace firmware loader will return -EAGAIN in case no
1655 * patch/nvm-config is found, so run with original fw/config.
1656 */
1657 ret = 0;
Rocky Liaobb2500ab2020-01-15 16:55:51 +08001658 } else {
1659 if (retries < MAX_INIT_RETRIES) {
1660 qca_power_shutdown(hu);
1661 if (hu->serdev) {
1662 serdev_device_close(hu->serdev);
1663 ret = serdev_device_open(hu->serdev);
1664 if (ret) {
1665 bt_dev_err(hdev, "failed to open port");
1666 return ret;
1667 }
1668 }
1669 retries++;
1670 goto retry;
1671 }
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001672 }
1673
1674 /* Setup bdaddr */
Rocky Liaoe5d64682020-03-25 10:26:37 +08001675 if (soc_type == QCA_ROME)
Balakrishna Godavarthi5c0a10012019-01-16 18:01:15 +05301676 hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
Rocky Liaoe5d64682020-03-25 10:26:37 +08001677 else
1678 hu->hdev->set_bdaddr = qca_set_bdaddr;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001679
1680 return ret;
1681}
1682
Nishka Dasgupta2edc9c52019-08-15 11:21:49 +05301683static const struct hci_uart_proto qca_proto = {
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001684 .id = HCI_UART_QCA,
1685 .name = "QCA",
Marcel Holtmannaee61f72015-10-20 21:30:45 +02001686 .manufacturer = 29,
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001687 .init_speed = 115200,
1688 .oper_speed = 3000000,
1689 .open = qca_open,
1690 .close = qca_close,
1691 .flush = qca_flush,
1692 .setup = qca_setup,
1693 .recv = qca_recv,
1694 .enqueue = qca_enqueue,
1695 .dequeue = qca_dequeue,
1696};
1697
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -07001698static const struct qca_device_data qca_soc_data_wcn3990 = {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301699 .soc_type = QCA_WCN3990,
1700 .vregs = (struct qca_vreg []) {
Bjorn Anderssonf2edd662019-10-17 22:24:02 -07001701 { "vddio", 15000 },
1702 { "vddxo", 80000 },
1703 { "vddrf", 300000 },
1704 { "vddch0", 450000 },
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301705 },
1706 .num_vregs = 4,
1707};
1708
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -07001709static const struct qca_device_data qca_soc_data_wcn3991 = {
Balakrishna Godavarthi7d250a02019-11-06 15:18:32 +05301710 .soc_type = QCA_WCN3991,
1711 .vregs = (struct qca_vreg []) {
1712 { "vddio", 15000 },
1713 { "vddxo", 80000 },
1714 { "vddrf", 300000 },
1715 { "vddch0", 450000 },
1716 },
1717 .num_vregs = 4,
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -07001718 .capabilities = QCA_CAP_WIDEBAND_SPEECH,
Balakrishna Godavarthi7d250a02019-11-06 15:18:32 +05301719};
1720
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -07001721static const struct qca_device_data qca_soc_data_wcn3998 = {
Harish Bandi523760b2019-04-26 19:26:01 +05301722 .soc_type = QCA_WCN3998,
1723 .vregs = (struct qca_vreg []) {
Bjorn Anderssonf2edd662019-10-17 22:24:02 -07001724 { "vddio", 10000 },
1725 { "vddxo", 80000 },
1726 { "vddrf", 300000 },
1727 { "vddch0", 450000 },
Harish Bandi523760b2019-04-26 19:26:01 +05301728 },
1729 .num_vregs = 4,
1730};
1731
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -07001732static const struct qca_device_data qca_soc_data_qca6390 = {
Rocky Liaoe5d64682020-03-25 10:26:37 +08001733 .soc_type = QCA_QCA6390,
1734 .num_vregs = 0,
1735};
1736
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +05301737static void qca_power_shutdown(struct hci_uart *hu)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301738{
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001739 struct qca_serdev *qcadev;
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +05301740 struct qca_data *qca = hu->priv;
1741 unsigned long flags;
Rocky Liao55599042020-01-15 16:55:50 +08001742 enum qca_btsoc_type soc_type = qca_soc_type(hu);
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +05301743
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001744 qcadev = serdev_device_get_drvdata(hu->serdev);
1745
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +05301746 /* From this point we go into power off state. But serial port is
1747 * still open, stop queueing the IBS data and flush all the buffered
1748 * data in skb's.
1749 */
1750 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
Matthias Kaehlcke62a91992019-04-29 16:21:30 -07001751 clear_bit(QCA_IBS_ENABLED, &qca->flags);
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +05301752 qca_flush(hu);
1753 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1754
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301755 hu->hdev->hw_error = NULL;
1756 hu->hdev->cmd_timeout = NULL;
Rocky Liao55599042020-01-15 16:55:50 +08001757
1758 /* Non-serdev device usually is powered by external power
1759 * and don't need additional action in driver for power down
1760 */
1761 if (!hu->serdev)
1762 return;
1763
1764 if (qca_is_wcn399x(soc_type)) {
1765 host_set_baudrate(hu, 2400);
1766 qca_send_power_pulse(hu, false);
1767 qca_regulator_disable(qcadev);
Rocky Liao77131df2020-03-04 21:16:45 +08001768 } else if (qcadev->bt_en) {
Rocky Liao55599042020-01-15 16:55:50 +08001769 gpiod_set_value_cansleep(qcadev->bt_en, 0);
1770 }
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301771}
1772
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301773static int qca_power_off(struct hci_dev *hdev)
1774{
1775 struct hci_uart *hu = hci_get_drvdata(hdev);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301776 struct qca_data *qca = hu->priv;
Rocky Liao4f9ed5b2020-02-29 20:21:18 +08001777 enum qca_btsoc_type soc_type = qca_soc_type(hu);
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301778
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301779 /* Stop sending shutdown command if soc crashes. */
Rocky Liaoe5d64682020-03-25 10:26:37 +08001780 if (soc_type != QCA_ROME
Rocky Liao4f9ed5b2020-02-29 20:21:18 +08001781 && qca->memdump_state == QCA_MEMDUMP_IDLE) {
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301782 qca_send_pre_shutdown_cmd(hdev);
1783 usleep_range(8000, 10000);
1784 }
Harish Bandia2780882019-07-12 10:39:40 +05301785
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301786 qca->memdump_state = QCA_MEMDUMP_IDLE;
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301787 qca_power_shutdown(hu);
1788 return 0;
1789}
1790
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001791static int qca_regulator_enable(struct qca_serdev *qcadev)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301792{
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001793 struct qca_power *power = qcadev->bt_power;
1794 int ret;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301795
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001796 /* Already enabled */
1797 if (power->vregs_on)
1798 return 0;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301799
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001800 BT_DBG("enabling %d regulators)", power->num_vregs);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301801
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001802 ret = regulator_bulk_enable(power->num_vregs, power->vreg_bulk);
1803 if (ret)
1804 return ret;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301805
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001806 power->vregs_on = true;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301807
Venkata Lakshmi Narayana Gubba66cb7052020-02-03 16:10:40 +05301808 ret = clk_prepare_enable(qcadev->susclk);
Venkata Lakshmi Narayana Gubbaf3d63f52020-02-05 16:21:43 +05301809 if (ret)
Venkata Lakshmi Narayana Gubba66cb7052020-02-03 16:10:40 +05301810 qca_regulator_disable(qcadev);
Venkata Lakshmi Narayana Gubba66cb7052020-02-03 16:10:40 +05301811
Venkata Lakshmi Narayana Gubbaf3d63f52020-02-05 16:21:43 +05301812 return ret;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301813}
1814
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001815static void qca_regulator_disable(struct qca_serdev *qcadev)
1816{
1817 struct qca_power *power;
1818
1819 if (!qcadev)
1820 return;
1821
1822 power = qcadev->bt_power;
1823
1824 /* Already disabled? */
1825 if (!power->vregs_on)
1826 return;
1827
1828 regulator_bulk_disable(power->num_vregs, power->vreg_bulk);
1829 power->vregs_on = false;
Venkata Lakshmi Narayana Gubba66cb7052020-02-03 16:10:40 +05301830
Venkata Lakshmi Narayana Gubbaf3d63f52020-02-05 16:21:43 +05301831 clk_disable_unprepare(qcadev->susclk);
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001832}
1833
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301834static int qca_init_regulators(struct qca_power *qca,
1835 const struct qca_vreg *vregs, size_t num_vregs)
1836{
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001837 struct regulator_bulk_data *bulk;
1838 int ret;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301839 int i;
1840
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001841 bulk = devm_kcalloc(qca->dev, num_vregs, sizeof(*bulk), GFP_KERNEL);
1842 if (!bulk)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301843 return -ENOMEM;
1844
1845 for (i = 0; i < num_vregs; i++)
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001846 bulk[i].supply = vregs[i].name;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301847
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001848 ret = devm_regulator_bulk_get(qca->dev, num_vregs, bulk);
1849 if (ret < 0)
1850 return ret;
1851
1852 for (i = 0; i < num_vregs; i++) {
1853 ret = regulator_set_load(bulk[i].consumer, vregs[i].load_uA);
1854 if (ret)
1855 return ret;
1856 }
1857
1858 qca->vreg_bulk = bulk;
Bjorn Andersson163d42f2019-10-17 22:24:03 -07001859 qca->num_vregs = num_vregs;
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001860
1861 return 0;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301862}
1863
Thierry Escande05ba5332018-03-29 21:15:24 +02001864static int qca_serdev_probe(struct serdev_device *serdev)
1865{
1866 struct qca_serdev *qcadev;
Rocky Liaoae563182020-01-16 11:22:54 +08001867 struct hci_dev *hdev;
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -07001868 const struct qca_device_data *data;
Thierry Escande05ba5332018-03-29 21:15:24 +02001869 int err;
Rocky Liao8a208b22020-03-04 09:54:29 +08001870 bool power_ctrl_enabled = true;
Thierry Escande05ba5332018-03-29 21:15:24 +02001871
1872 qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
1873 if (!qcadev)
1874 return -ENOMEM;
1875
1876 qcadev->serdev_hu.serdev = serdev;
Rocky Liao9f3565b2019-12-13 16:50:45 +08001877 data = device_get_match_data(&serdev->dev);
Thierry Escande05ba5332018-03-29 21:15:24 +02001878 serdev_device_set_drvdata(serdev, qcadev);
Rocky Liao99c905c2019-06-06 17:40:30 +08001879 device_property_read_string(&serdev->dev, "firmware-name",
1880 &qcadev->firmware_name);
Christian Hewitt37aee132020-04-23 01:34:30 +00001881 device_property_read_u32(&serdev->dev, "max-speed",
1882 &qcadev->oper_speed);
1883 if (!qcadev->oper_speed)
1884 BT_DBG("UART will pick default operating speed");
1885
Harish Bandi523760b2019-04-26 19:26:01 +05301886 if (data && qca_is_wcn399x(data->soc_type)) {
1887 qcadev->btsoc_type = data->soc_type;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301888 qcadev->bt_power = devm_kzalloc(&serdev->dev,
1889 sizeof(struct qca_power),
1890 GFP_KERNEL);
1891 if (!qcadev->bt_power)
1892 return -ENOMEM;
Thierry Escande05ba5332018-03-29 21:15:24 +02001893
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301894 qcadev->bt_power->dev = &serdev->dev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301895 err = qca_init_regulators(qcadev->bt_power, data->vregs,
1896 data->num_vregs);
1897 if (err) {
1898 BT_ERR("Failed to init regulators:%d", err);
Rocky Liaoae563182020-01-16 11:22:54 +08001899 return err;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301900 }
1901
1902 qcadev->bt_power->vregs_on = false;
1903
Venkata Lakshmi Narayana Gubba66cb7052020-02-03 16:10:40 +05301904 qcadev->susclk = devm_clk_get_optional(&serdev->dev, NULL);
1905 if (IS_ERR(qcadev->susclk)) {
1906 dev_err(&serdev->dev, "failed to acquire clk\n");
1907 return PTR_ERR(qcadev->susclk);
1908 }
1909
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301910 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1911 if (err) {
1912 BT_ERR("wcn3990 serdev registration failed");
Rocky Liaoae563182020-01-16 11:22:54 +08001913 return err;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301914 }
1915 } else {
Rocky Liaoe5d64682020-03-25 10:26:37 +08001916 if (data)
1917 qcadev->btsoc_type = data->soc_type;
1918 else
1919 qcadev->btsoc_type = QCA_ROME;
1920
Rocky Liao77131df2020-03-04 21:16:45 +08001921 qcadev->bt_en = devm_gpiod_get_optional(&serdev->dev, "enable",
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301922 GPIOD_OUT_LOW);
Rocky Liao77131df2020-03-04 21:16:45 +08001923 if (!qcadev->bt_en) {
Rocky Liao8a208b22020-03-04 09:54:29 +08001924 dev_warn(&serdev->dev, "failed to acquire enable gpio\n");
1925 power_ctrl_enabled = false;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301926 }
1927
Rocky Liao77131df2020-03-04 21:16:45 +08001928 qcadev->susclk = devm_clk_get_optional(&serdev->dev, NULL);
1929 if (!qcadev->susclk) {
Rocky Liao8a208b22020-03-04 09:54:29 +08001930 dev_warn(&serdev->dev, "failed to acquire clk\n");
1931 } else {
1932 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
1933 if (err)
1934 return err;
1935
1936 err = clk_prepare_enable(qcadev->susclk);
1937 if (err)
1938 return err;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301939 }
1940
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301941 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
Rocky Liaoae563182020-01-16 11:22:54 +08001942 if (err) {
1943 BT_ERR("Rome serdev registration failed");
Rocky Liao77131df2020-03-04 21:16:45 +08001944 if (qcadev->susclk)
Rocky Liao8a208b22020-03-04 09:54:29 +08001945 clk_disable_unprepare(qcadev->susclk);
Rocky Liaoae563182020-01-16 11:22:54 +08001946 return err;
1947 }
Thierry Escande05ba5332018-03-29 21:15:24 +02001948 }
1949
Abhishek Pandit-Subedi85e90d92020-05-20 09:32:28 -07001950 hdev = qcadev->serdev_hu.hdev;
1951
Rocky Liao8a208b22020-03-04 09:54:29 +08001952 if (power_ctrl_enabled) {
Rocky Liao8a208b22020-03-04 09:54:29 +08001953 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
1954 hdev->shutdown = qca_power_off;
1955 }
Thierry Escande05ba5332018-03-29 21:15:24 +02001956
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -07001957 /* Wideband speech support must be set per driver since it can't be
1958 * queried via hci.
1959 */
1960 if (data && (data->capabilities & QCA_CAP_WIDEBAND_SPEECH))
1961 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
1962
Rocky Liaoae563182020-01-16 11:22:54 +08001963 return 0;
Thierry Escande05ba5332018-03-29 21:15:24 +02001964}
1965
1966static void qca_serdev_remove(struct serdev_device *serdev)
1967{
1968 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
1969
Harish Bandi523760b2019-04-26 19:26:01 +05301970 if (qca_is_wcn399x(qcadev->btsoc_type))
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +05301971 qca_power_shutdown(&qcadev->serdev_hu);
Rocky Liao77131df2020-03-04 21:16:45 +08001972 else if (qcadev->susclk)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301973 clk_disable_unprepare(qcadev->susclk);
Thierry Escande05ba5332018-03-29 21:15:24 +02001974
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301975 hci_uart_unregister_device(&qcadev->serdev_hu);
Thierry Escande05ba5332018-03-29 21:15:24 +02001976}
1977
Zijun Hu7e7bbdd2020-05-29 21:56:57 +08001978static void qca_serdev_shutdown(struct device *dev)
1979{
1980 int ret;
1981 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
1982 struct serdev_device *serdev = to_serdev_device(dev);
1983 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
1984 const u8 ibs_wake_cmd[] = { 0xFD };
1985 const u8 edl_reset_soc_cmd[] = { 0x01, 0x00, 0xFC, 0x01, 0x05 };
1986
1987 if (qcadev->btsoc_type == QCA_QCA6390) {
1988 serdev_device_write_flush(serdev);
1989 ret = serdev_device_write_buf(serdev, ibs_wake_cmd,
1990 sizeof(ibs_wake_cmd));
1991 if (ret < 0) {
1992 BT_ERR("QCA send IBS_WAKE_IND error: %d", ret);
1993 return;
1994 }
1995 serdev_device_wait_until_sent(serdev, timeout);
1996 usleep_range(8000, 10000);
1997
1998 serdev_device_write_flush(serdev);
1999 ret = serdev_device_write_buf(serdev, edl_reset_soc_cmd,
2000 sizeof(edl_reset_soc_cmd));
2001 if (ret < 0) {
2002 BT_ERR("QCA send EDL_RESET_REQ error: %d", ret);
2003 return;
2004 }
2005 serdev_device_wait_until_sent(serdev, timeout);
2006 usleep_range(8000, 10000);
2007 }
2008}
2009
Claire Chang41d5b252019-10-31 18:46:14 +08002010static int __maybe_unused qca_suspend(struct device *dev)
2011{
Zijun Hufeac90d2020-05-29 04:31:07 +08002012 struct serdev_device *serdev = to_serdev_device(dev);
2013 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
2014 struct hci_uart *hu = &qcadev->serdev_hu;
Claire Chang41d5b252019-10-31 18:46:14 +08002015 struct qca_data *qca = hu->priv;
2016 unsigned long flags;
2017 int ret = 0;
2018 u8 cmd;
2019
2020 set_bit(QCA_SUSPENDING, &qca->flags);
2021
2022 /* Device is downloading patch or doesn't support in-band sleep. */
2023 if (!test_bit(QCA_IBS_ENABLED, &qca->flags))
2024 return 0;
2025
2026 cancel_work_sync(&qca->ws_awake_device);
2027 cancel_work_sync(&qca->ws_awake_rx);
2028
2029 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
2030 flags, SINGLE_DEPTH_NESTING);
2031
2032 switch (qca->tx_ibs_state) {
2033 case HCI_IBS_TX_WAKING:
2034 del_timer(&qca->wake_retrans_timer);
2035 /* Fall through */
2036 case HCI_IBS_TX_AWAKE:
2037 del_timer(&qca->tx_idle_timer);
2038
2039 serdev_device_write_flush(hu->serdev);
2040 cmd = HCI_IBS_SLEEP_IND;
2041 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
2042
2043 if (ret < 0) {
2044 BT_ERR("Failed to send SLEEP to device");
2045 break;
2046 }
2047
2048 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
2049 qca->ibs_sent_slps++;
2050
2051 qca_wq_serial_tx_clock_vote_off(&qca->ws_tx_vote_off);
2052 break;
2053
2054 case HCI_IBS_TX_ASLEEP:
2055 break;
2056
2057 default:
2058 BT_ERR("Spurious tx state %d", qca->tx_ibs_state);
2059 ret = -EINVAL;
2060 break;
2061 }
2062
2063 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
2064
2065 if (ret < 0)
2066 goto error;
2067
2068 serdev_device_wait_until_sent(hu->serdev,
2069 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
2070
2071 /* Wait for HCI_IBS_SLEEP_IND sent by device to indicate its Tx is going
2072 * to sleep, so that the packet does not wake the system later.
2073 */
2074
2075 ret = wait_event_interruptible_timeout(qca->suspend_wait_q,
2076 qca->rx_ibs_state == HCI_IBS_RX_ASLEEP,
2077 msecs_to_jiffies(IBS_BTSOC_TX_IDLE_TIMEOUT_MS));
2078
2079 if (ret > 0)
2080 return 0;
2081
2082 if (ret == 0)
2083 ret = -ETIMEDOUT;
2084
2085error:
2086 clear_bit(QCA_SUSPENDING, &qca->flags);
2087
2088 return ret;
2089}
2090
2091static int __maybe_unused qca_resume(struct device *dev)
2092{
Zijun Hufeac90d2020-05-29 04:31:07 +08002093 struct serdev_device *serdev = to_serdev_device(dev);
2094 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
2095 struct hci_uart *hu = &qcadev->serdev_hu;
Claire Chang41d5b252019-10-31 18:46:14 +08002096 struct qca_data *qca = hu->priv;
2097
2098 clear_bit(QCA_SUSPENDING, &qca->flags);
2099
2100 return 0;
2101}
2102
2103static SIMPLE_DEV_PM_OPS(qca_pm_ops, qca_suspend, qca_resume);
2104
Rocky Liaoe5d64682020-03-25 10:26:37 +08002105#ifdef CONFIG_OF
Thierry Escande05ba5332018-03-29 21:15:24 +02002106static const struct of_device_id qca_bluetooth_of_match[] = {
2107 { .compatible = "qcom,qca6174-bt" },
Rocky Liaoe5d64682020-03-25 10:26:37 +08002108 { .compatible = "qcom,qca6390-bt", .data = &qca_soc_data_qca6390},
Christian Hewitt31d4ab82020-04-23 01:34:29 +00002109 { .compatible = "qcom,qca9377-bt" },
Harish Bandi523760b2019-04-26 19:26:01 +05302110 { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990},
Balakrishna Godavarthi7d250a02019-11-06 15:18:32 +05302111 { .compatible = "qcom,wcn3991-bt", .data = &qca_soc_data_wcn3991},
Harish Bandi523760b2019-04-26 19:26:01 +05302112 { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998},
Thierry Escande05ba5332018-03-29 21:15:24 +02002113 { /* sentinel */ }
2114};
2115MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
Rocky Liaoe5d64682020-03-25 10:26:37 +08002116#endif
2117
2118#ifdef CONFIG_ACPI
2119static const struct acpi_device_id qca_bluetooth_acpi_match[] = {
2120 { "QCOM6390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2121 { "DLA16390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2122 { "DLB16390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2123 { "DLB26390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2124 { },
2125};
2126MODULE_DEVICE_TABLE(acpi, qca_bluetooth_acpi_match);
2127#endif
2128
Thierry Escande05ba5332018-03-29 21:15:24 +02002129
2130static struct serdev_device_driver qca_serdev_driver = {
2131 .probe = qca_serdev_probe,
2132 .remove = qca_serdev_remove,
2133 .driver = {
2134 .name = "hci_uart_qca",
Rocky Liaoe5d64682020-03-25 10:26:37 +08002135 .of_match_table = of_match_ptr(qca_bluetooth_of_match),
2136 .acpi_match_table = ACPI_PTR(qca_bluetooth_acpi_match),
Zijun Hu7e7bbdd2020-05-29 21:56:57 +08002137 .shutdown = qca_serdev_shutdown,
Claire Chang41d5b252019-10-31 18:46:14 +08002138 .pm = &qca_pm_ops,
Thierry Escande05ba5332018-03-29 21:15:24 +02002139 },
2140};
2141
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07002142int __init qca_init(void)
2143{
Thierry Escande05ba5332018-03-29 21:15:24 +02002144 serdev_device_driver_register(&qca_serdev_driver);
2145
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07002146 return hci_uart_register_proto(&qca_proto);
2147}
2148
2149int __exit qca_deinit(void)
2150{
Thierry Escande05ba5332018-03-29 21:15:24 +02002151 serdev_device_driver_unregister(&qca_serdev_driver);
2152
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07002153 return hci_uart_unregister_proto(&qca_proto);
2154}