blob: 325baa046c3ab95a6c02d84728c54aa76833ccb9 [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>
29#include <linux/platform_device.h>
30#include <linux/regulator/consumer.h>
Thierry Escande05ba5332018-03-29 21:15:24 +020031#include <linux/serdev.h>
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +053032#include <linux/mutex.h>
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +053033#include <asm/unaligned.h>
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070034
35#include <net/bluetooth/bluetooth.h>
36#include <net/bluetooth/hci_core.h>
37
38#include "hci_uart.h"
39#include "btqca.h"
40
41/* HCI_IBS protocol messages */
42#define HCI_IBS_SLEEP_IND 0xFE
43#define HCI_IBS_WAKE_IND 0xFD
44#define HCI_IBS_WAKE_ACK 0xFC
Marcel Holtmannf81b0012015-08-30 23:05:32 +020045#define HCI_MAX_IBS_SIZE 10
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070046
Marcel Holtmannf81b0012015-08-30 23:05:32 +020047#define IBS_WAKE_RETRANS_TIMEOUT_MS 100
Claire Chang41d5b252019-10-31 18:46:14 +080048#define IBS_BTSOC_TX_IDLE_TIMEOUT_MS 40
49#define IBS_HOST_TX_IDLE_TIMEOUT_MS 2000
Matthias Kaehlcke94d66712019-02-27 15:52:23 -080050#define CMD_TRANS_TIMEOUT_MS 100
Balakrishna Godavarthid8415022020-01-02 20:19:11 +053051#define MEMDUMP_TIMEOUT_MS 8000
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070052
Thierry Escande05ba5332018-03-29 21:15:24 +020053/* susclk rate */
54#define SUSCLK_RATE_32KHZ 32768
55
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +053056/* Controller debug log header */
57#define QCA_DEBUG_HANDLE 0x2EDC
58
Rocky Liaobb2500ab2020-01-15 16:55:51 +080059/* max retry count when init fails */
60#define MAX_INIT_RETRIES 3
61
Balakrishna Godavarthid8415022020-01-02 20:19:11 +053062/* Controller dump header */
63#define QCA_SSR_DUMP_HANDLE 0x0108
64#define QCA_DUMP_PACKET_SIZE 255
65#define QCA_LAST_SEQUENCE_NUM 0xFFFF
66#define QCA_CRASHBYTE_PACKET_LEN 1096
67#define QCA_MEMDUMP_BYTE 0xFB
68
Matthias Kaehlcke62a91992019-04-29 16:21:30 -070069enum qca_flags {
70 QCA_IBS_ENABLED,
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -070071 QCA_DROP_VENDOR_EVENT,
Claire Chang41d5b252019-10-31 18:46:14 +080072 QCA_SUSPENDING,
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +053073 QCA_MEMDUMP_COLLECTION,
74 QCA_HW_ERROR_EVENT
Matthias Kaehlcke62a91992019-04-29 16:21:30 -070075};
76
Balakrishna Godavarthid8415022020-01-02 20:19:11 +053077
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070078/* HCI_IBS transmit side sleep protocol states */
79enum tx_ibs_states {
80 HCI_IBS_TX_ASLEEP,
81 HCI_IBS_TX_WAKING,
82 HCI_IBS_TX_AWAKE,
83};
84
85/* HCI_IBS receive side sleep protocol states */
86enum rx_states {
87 HCI_IBS_RX_ASLEEP,
88 HCI_IBS_RX_AWAKE,
89};
90
91/* HCI_IBS transmit and receive side clock state vote */
92enum hci_ibs_clock_state_vote {
93 HCI_IBS_VOTE_STATS_UPDATE,
94 HCI_IBS_TX_VOTE_CLOCK_ON,
95 HCI_IBS_TX_VOTE_CLOCK_OFF,
96 HCI_IBS_RX_VOTE_CLOCK_ON,
97 HCI_IBS_RX_VOTE_CLOCK_OFF,
98};
99
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530100/* Controller memory dump states */
101enum qca_memdump_states {
102 QCA_MEMDUMP_IDLE,
103 QCA_MEMDUMP_COLLECTING,
104 QCA_MEMDUMP_COLLECTED,
105 QCA_MEMDUMP_TIMEOUT,
106};
107
108struct qca_memdump_data {
109 char *memdump_buf_head;
110 char *memdump_buf_tail;
111 u32 current_seq_no;
112 u32 received_dump;
113};
114
115struct qca_memdump_event_hdr {
116 __u8 evt;
117 __u8 plen;
118 __u16 opcode;
119 __u16 seq_no;
120 __u8 reserved;
121} __packed;
122
123
124struct qca_dump_size {
125 u32 dump_size;
126} __packed;
127
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700128struct qca_data {
129 struct hci_uart *hu;
130 struct sk_buff *rx_skb;
131 struct sk_buff_head txq;
132 struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530133 struct sk_buff_head rx_memdump_q; /* Memdump wait queue */
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700134 spinlock_t hci_ibs_lock; /* HCI_IBS state lock */
135 u8 tx_ibs_state; /* HCI_IBS transmit side power state*/
136 u8 rx_ibs_state; /* HCI_IBS receive side power state */
Viresh Kumar621a5f72015-09-26 15:04:07 -0700137 bool tx_vote; /* Clock must be on for TX */
138 bool rx_vote; /* Clock must be on for RX */
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700139 struct timer_list tx_idle_timer;
140 u32 tx_idle_delay;
141 struct timer_list wake_retrans_timer;
142 u32 wake_retrans;
143 struct workqueue_struct *workqueue;
144 struct work_struct ws_awake_rx;
145 struct work_struct ws_awake_device;
146 struct work_struct ws_rx_vote_off;
147 struct work_struct ws_tx_vote_off;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530148 struct work_struct ctrl_memdump_evt;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530149 struct delayed_work ctrl_memdump_timeout;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530150 struct qca_memdump_data *qca_memdump;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700151 unsigned long flags;
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -0700152 struct completion drop_ev_comp;
Claire Chang41d5b252019-10-31 18:46:14 +0800153 wait_queue_head_t suspend_wait_q;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530154 enum qca_memdump_states memdump_state;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530155 struct mutex hci_memdump_lock;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700156
157 /* For debugging purpose */
158 u64 ibs_sent_wacks;
159 u64 ibs_sent_slps;
160 u64 ibs_sent_wakes;
161 u64 ibs_recv_wacks;
162 u64 ibs_recv_slps;
163 u64 ibs_recv_wakes;
164 u64 vote_last_jif;
165 u32 vote_on_ms;
166 u32 vote_off_ms;
167 u64 tx_votes_on;
168 u64 rx_votes_on;
169 u64 tx_votes_off;
170 u64 rx_votes_off;
171 u64 votes_on;
172 u64 votes_off;
173};
174
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +0530175enum qca_speed_type {
176 QCA_INIT_SPEED = 1,
177 QCA_OPER_SPEED
178};
179
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530180/*
181 * Voltage regulator information required for configuring the
182 * QCA Bluetooth chipset
183 */
184struct qca_vreg {
185 const char *name;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530186 unsigned int load_uA;
187};
188
189struct qca_vreg_data {
190 enum qca_btsoc_type soc_type;
191 struct qca_vreg *vregs;
192 size_t num_vregs;
193};
194
195/*
196 * Platform data for the QCA Bluetooth power driver.
197 */
198struct qca_power {
199 struct device *dev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530200 struct regulator_bulk_data *vreg_bulk;
Bjorn Andersson163d42f2019-10-17 22:24:03 -0700201 int num_vregs;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530202 bool vregs_on;
203};
204
Thierry Escande05ba5332018-03-29 21:15:24 +0200205struct qca_serdev {
206 struct hci_uart serdev_hu;
207 struct gpio_desc *bt_en;
208 struct clk *susclk;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530209 enum qca_btsoc_type btsoc_type;
210 struct qca_power *bt_power;
211 u32 init_speed;
212 u32 oper_speed;
Rocky Liao99c905c2019-06-06 17:40:30 +0800213 const char *firmware_name;
Thierry Escande05ba5332018-03-29 21:15:24 +0200214};
215
Bjorn Anderssona9314e72019-10-17 22:24:04 -0700216static int qca_regulator_enable(struct qca_serdev *qcadev);
217static void qca_regulator_disable(struct qca_serdev *qcadev);
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +0530218static void qca_power_shutdown(struct hci_uart *hu);
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +0530219static int qca_power_off(struct hci_dev *hdev);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530220static void qca_controller_memdump(struct work_struct *work);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530221
Matthias Kaehlcke4fdd5a42019-03-11 11:38:31 -0700222static enum qca_btsoc_type qca_soc_type(struct hci_uart *hu)
223{
224 enum qca_btsoc_type soc_type;
225
226 if (hu->serdev) {
227 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
228
229 soc_type = qsd->btsoc_type;
230 } else {
231 soc_type = QCA_ROME;
232 }
233
234 return soc_type;
235}
236
Rocky Liao99c905c2019-06-06 17:40:30 +0800237static const char *qca_get_firmware_name(struct hci_uart *hu)
238{
239 if (hu->serdev) {
240 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
241
242 return qsd->firmware_name;
243 } else {
244 return NULL;
245 }
246}
247
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700248static void __serial_clock_on(struct tty_struct *tty)
249{
250 /* TODO: Some chipset requires to enable UART clock on client
251 * side to save power consumption or manual work is required.
252 * Please put your code to control UART clock here if needed
253 */
254}
255
256static void __serial_clock_off(struct tty_struct *tty)
257{
258 /* TODO: Some chipset requires to disable UART clock on client
259 * side to save power consumption or manual work is required.
260 * Please put your code to control UART clock off here if needed
261 */
262}
263
264/* serial_clock_vote needs to be called with the ibs lock held */
265static void serial_clock_vote(unsigned long vote, struct hci_uart *hu)
266{
267 struct qca_data *qca = hu->priv;
268 unsigned int diff;
269
270 bool old_vote = (qca->tx_vote | qca->rx_vote);
271 bool new_vote;
272
273 switch (vote) {
274 case HCI_IBS_VOTE_STATS_UPDATE:
275 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
276
277 if (old_vote)
278 qca->vote_off_ms += diff;
279 else
280 qca->vote_on_ms += diff;
281 return;
282
283 case HCI_IBS_TX_VOTE_CLOCK_ON:
284 qca->tx_vote = true;
285 qca->tx_votes_on++;
286 new_vote = true;
287 break;
288
289 case HCI_IBS_RX_VOTE_CLOCK_ON:
290 qca->rx_vote = true;
291 qca->rx_votes_on++;
292 new_vote = true;
293 break;
294
295 case HCI_IBS_TX_VOTE_CLOCK_OFF:
296 qca->tx_vote = false;
297 qca->tx_votes_off++;
298 new_vote = qca->rx_vote | qca->tx_vote;
299 break;
300
301 case HCI_IBS_RX_VOTE_CLOCK_OFF:
302 qca->rx_vote = false;
303 qca->rx_votes_off++;
304 new_vote = qca->rx_vote | qca->tx_vote;
305 break;
306
307 default:
308 BT_ERR("Voting irregularity");
309 return;
310 }
311
312 if (new_vote != old_vote) {
313 if (new_vote)
314 __serial_clock_on(hu->tty);
315 else
316 __serial_clock_off(hu->tty);
317
Prasanna Karthikce26d812015-09-15 12:19:45 +0000318 BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false",
319 vote ? "true" : "false");
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700320
321 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
322
323 if (new_vote) {
324 qca->votes_on++;
325 qca->vote_off_ms += diff;
326 } else {
327 qca->votes_off++;
328 qca->vote_on_ms += diff;
329 }
330 qca->vote_last_jif = jiffies;
331 }
332}
333
334/* Builds and sends an HCI_IBS command packet.
335 * These are very simple packets with only 1 cmd byte.
336 */
337static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
338{
339 int err = 0;
340 struct sk_buff *skb = NULL;
341 struct qca_data *qca = hu->priv;
342
343 BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd);
344
345 skb = bt_skb_alloc(1, GFP_ATOMIC);
346 if (!skb) {
347 BT_ERR("Failed to allocate memory for HCI_IBS packet");
348 return -ENOMEM;
349 }
350
351 /* Assign HCI_IBS type */
Johannes Berg634fef62017-06-16 14:29:24 +0200352 skb_put_u8(skb, cmd);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700353
354 skb_queue_tail(&qca->txq, skb);
355
356 return err;
357}
358
359static void qca_wq_awake_device(struct work_struct *work)
360{
361 struct qca_data *qca = container_of(work, struct qca_data,
362 ws_awake_device);
363 struct hci_uart *hu = qca->hu;
364 unsigned long retrans_delay;
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530365 unsigned long flags;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700366
367 BT_DBG("hu %p wq awake device", hu);
368
369 /* Vote for serial clock */
370 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
371
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530372 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700373
374 /* Send wake indication to device */
375 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
376 BT_ERR("Failed to send WAKE to device");
377
378 qca->ibs_sent_wakes++;
379
380 /* Start retransmit timer */
381 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
382 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
383
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530384 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700385
386 /* Actually send the packets */
387 hci_uart_tx_wakeup(hu);
388}
389
390static void qca_wq_awake_rx(struct work_struct *work)
391{
392 struct qca_data *qca = container_of(work, struct qca_data,
393 ws_awake_rx);
394 struct hci_uart *hu = qca->hu;
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530395 unsigned long flags;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700396
397 BT_DBG("hu %p wq awake rx", hu);
398
399 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
400
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530401 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700402 qca->rx_ibs_state = HCI_IBS_RX_AWAKE;
403
404 /* Always acknowledge device wake up,
405 * sending IBS message doesn't count as TX ON.
406 */
407 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
408 BT_ERR("Failed to acknowledge device wake up");
409
410 qca->ibs_sent_wacks++;
411
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530412 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700413
414 /* Actually send the packets */
415 hci_uart_tx_wakeup(hu);
416}
417
418static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work)
419{
420 struct qca_data *qca = container_of(work, struct qca_data,
421 ws_rx_vote_off);
422 struct hci_uart *hu = qca->hu;
423
424 BT_DBG("hu %p rx clock vote off", hu);
425
426 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
427}
428
429static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work)
430{
431 struct qca_data *qca = container_of(work, struct qca_data,
432 ws_tx_vote_off);
433 struct hci_uart *hu = qca->hu;
434
435 BT_DBG("hu %p tx clock vote off", hu);
436
437 /* Run HCI tx handling unlocked */
438 hci_uart_tx_wakeup(hu);
439
440 /* Now that message queued to tty driver, vote for tty clocks off.
441 * It is up to the tty driver to pend the clocks off until tx done.
442 */
443 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
444}
445
Kees Cook04356052017-10-04 17:54:29 -0700446static void hci_ibs_tx_idle_timeout(struct timer_list *t)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700447{
Kees Cook04356052017-10-04 17:54:29 -0700448 struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
449 struct hci_uart *hu = qca->hu;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700450 unsigned long flags;
451
452 BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state);
453
454 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
455 flags, SINGLE_DEPTH_NESTING);
456
457 switch (qca->tx_ibs_state) {
458 case HCI_IBS_TX_AWAKE:
459 /* TX_IDLE, go to SLEEP */
460 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
461 BT_ERR("Failed to send SLEEP to device");
462 break;
463 }
464 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
465 qca->ibs_sent_slps++;
466 queue_work(qca->workqueue, &qca->ws_tx_vote_off);
467 break;
468
469 case HCI_IBS_TX_ASLEEP:
470 case HCI_IBS_TX_WAKING:
471 /* Fall through */
472
473 default:
Colin Ian Kinge059a462017-02-17 19:58:10 +0000474 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700475 break;
476 }
477
478 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
479}
480
Kees Cook04356052017-10-04 17:54:29 -0700481static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700482{
Kees Cook04356052017-10-04 17:54:29 -0700483 struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
484 struct hci_uart *hu = qca->hu;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700485 unsigned long flags, retrans_delay;
Prasanna Karthika9137182015-09-28 08:03:24 +0000486 bool retransmit = false;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700487
488 BT_DBG("hu %p wake retransmit timeout in %d state",
489 hu, qca->tx_ibs_state);
490
491 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
492 flags, SINGLE_DEPTH_NESTING);
493
Claire Chang41d5b252019-10-31 18:46:14 +0800494 /* Don't retransmit the HCI_IBS_WAKE_IND when suspending. */
495 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
496 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
497 return;
498 }
499
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700500 switch (qca->tx_ibs_state) {
501 case HCI_IBS_TX_WAKING:
502 /* No WAKE_ACK, retransmit WAKE */
Prasanna Karthika9137182015-09-28 08:03:24 +0000503 retransmit = true;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700504 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
505 BT_ERR("Failed to acknowledge device wake up");
506 break;
507 }
508 qca->ibs_sent_wakes++;
509 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
510 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
511 break;
512
513 case HCI_IBS_TX_ASLEEP:
514 case HCI_IBS_TX_AWAKE:
515 /* Fall through */
516
517 default:
Colin Ian Kinge059a462017-02-17 19:58:10 +0000518 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700519 break;
520 }
521
522 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
523
524 if (retransmit)
525 hci_uart_tx_wakeup(hu);
526}
527
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530528
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530529static void qca_controller_memdump_timeout(struct work_struct *work)
530{
531 struct qca_data *qca = container_of(work, struct qca_data,
532 ctrl_memdump_timeout.work);
533 struct hci_uart *hu = qca->hu;
534
535 mutex_lock(&qca->hci_memdump_lock);
536 if (test_bit(QCA_MEMDUMP_COLLECTION, &qca->flags)) {
537 qca->memdump_state = QCA_MEMDUMP_TIMEOUT;
538 if (!test_bit(QCA_HW_ERROR_EVENT, &qca->flags)) {
539 /* Inject hw error event to reset the device
540 * and driver.
541 */
542 hci_reset_dev(hu->hdev);
543 }
544 }
545
546 mutex_unlock(&qca->hci_memdump_lock);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530547}
548
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530549
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700550/* Initialize protocol */
551static int qca_open(struct hci_uart *hu)
552{
Thierry Escande05ba5332018-03-29 21:15:24 +0200553 struct qca_serdev *qcadev;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700554 struct qca_data *qca;
555
556 BT_DBG("hu %p qca_open", hu);
557
Vladis Dronovb36a1552019-07-30 11:33:45 +0200558 if (!hci_uart_has_flow_control(hu))
559 return -EOPNOTSUPP;
560
Jia-Ju Bai25a13e382018-07-23 11:56:51 +0800561 qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700562 if (!qca)
563 return -ENOMEM;
564
565 skb_queue_head_init(&qca->txq);
566 skb_queue_head_init(&qca->tx_wait_q);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530567 skb_queue_head_init(&qca->rx_memdump_q);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700568 spin_lock_init(&qca->hci_ibs_lock);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530569 mutex_init(&qca->hci_memdump_lock);
Bhaktipriya Shridharfac9a602016-08-30 22:42:53 +0530570 qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700571 if (!qca->workqueue) {
572 BT_ERR("QCA Workqueue not initialized properly");
573 kfree(qca);
574 return -ENOMEM;
575 }
576
577 INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
578 INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
579 INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
580 INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530581 INIT_WORK(&qca->ctrl_memdump_evt, qca_controller_memdump);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530582 INIT_DELAYED_WORK(&qca->ctrl_memdump_timeout,
583 qca_controller_memdump_timeout);
Claire Chang41d5b252019-10-31 18:46:14 +0800584 init_waitqueue_head(&qca->suspend_wait_q);
585
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700586 qca->hu = hu;
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -0700587 init_completion(&qca->drop_ev_comp);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700588
589 /* Assume we start with both sides asleep -- extra wakes OK */
590 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
591 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
592
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700593 qca->vote_last_jif = jiffies;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700594
595 hu->priv = qca;
596
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530597 if (hu->serdev) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530598 qcadev = serdev_device_get_drvdata(hu->serdev);
Rocky Liao5e6d8402020-01-13 12:30:20 +0800599 if (qca_is_wcn399x(qcadev->btsoc_type)) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530600 hu->init_speed = qcadev->init_speed;
601 hu->oper_speed = qcadev->oper_speed;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530602 }
603 }
604
Kees Cook04356052017-10-04 17:54:29 -0700605 timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700606 qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
607
Kees Cook04356052017-10-04 17:54:29 -0700608 timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
Claire Chang41d5b252019-10-31 18:46:14 +0800609 qca->tx_idle_delay = IBS_HOST_TX_IDLE_TIMEOUT_MS;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700610
611 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
612 qca->tx_idle_delay, qca->wake_retrans);
613
614 return 0;
615}
616
617static void qca_debugfs_init(struct hci_dev *hdev)
618{
619 struct hci_uart *hu = hci_get_drvdata(hdev);
620 struct qca_data *qca = hu->priv;
621 struct dentry *ibs_dir;
622 umode_t mode;
623
624 if (!hdev->debugfs)
625 return;
626
627 ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
628
629 /* read only */
630 mode = S_IRUGO;
631 debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
632 debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
633 debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
634 &qca->ibs_sent_slps);
635 debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
636 &qca->ibs_sent_wakes);
637 debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
638 &qca->ibs_sent_wacks);
639 debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
640 &qca->ibs_recv_slps);
641 debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
642 &qca->ibs_recv_wakes);
643 debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
644 &qca->ibs_recv_wacks);
Ben YoungTae Kim10be6c02015-08-13 22:09:42 -0700645 debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700646 debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
647 debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
Ben YoungTae Kim10be6c02015-08-13 22:09:42 -0700648 debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700649 debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
650 debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
651 debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
652 debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
653 debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
654 debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
655
656 /* read/write */
657 mode = S_IRUGO | S_IWUSR;
658 debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
659 debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
660 &qca->tx_idle_delay);
661}
662
663/* Flush protocol data */
664static int qca_flush(struct hci_uart *hu)
665{
666 struct qca_data *qca = hu->priv;
667
668 BT_DBG("hu %p qca flush", hu);
669
670 skb_queue_purge(&qca->tx_wait_q);
671 skb_queue_purge(&qca->txq);
672
673 return 0;
674}
675
676/* Close protocol */
677static int qca_close(struct hci_uart *hu)
678{
679 struct qca_data *qca = hu->priv;
680
681 BT_DBG("hu %p qca close", hu);
682
683 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
684
685 skb_queue_purge(&qca->tx_wait_q);
686 skb_queue_purge(&qca->txq);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530687 skb_queue_purge(&qca->rx_memdump_q);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700688 del_timer(&qca->tx_idle_timer);
689 del_timer(&qca->wake_retrans_timer);
690 destroy_workqueue(qca->workqueue);
691 qca->hu = NULL;
692
Rocky Liao55599042020-01-15 16:55:50 +0800693 qca_power_shutdown(hu);
Thierry Escande05ba5332018-03-29 21:15:24 +0200694
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700695 kfree_skb(qca->rx_skb);
696
697 hu->priv = NULL;
698
699 kfree(qca);
700
701 return 0;
702}
703
704/* Called upon a wake-up-indication from the device.
705 */
706static void device_want_to_wakeup(struct hci_uart *hu)
707{
708 unsigned long flags;
709 struct qca_data *qca = hu->priv;
710
711 BT_DBG("hu %p want to wake up", hu);
712
713 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
714
715 qca->ibs_recv_wakes++;
716
Claire Chang41d5b252019-10-31 18:46:14 +0800717 /* Don't wake the rx up when suspending. */
718 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
719 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
720 return;
721 }
722
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700723 switch (qca->rx_ibs_state) {
724 case HCI_IBS_RX_ASLEEP:
725 /* Make sure clock is on - we may have turned clock off since
726 * receiving the wake up indicator awake rx clock.
727 */
728 queue_work(qca->workqueue, &qca->ws_awake_rx);
729 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
730 return;
731
732 case HCI_IBS_RX_AWAKE:
733 /* Always acknowledge device wake up,
734 * sending IBS message doesn't count as TX ON.
735 */
736 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
737 BT_ERR("Failed to acknowledge device wake up");
738 break;
739 }
740 qca->ibs_sent_wacks++;
741 break;
742
743 default:
744 /* Any other state is illegal */
745 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
746 qca->rx_ibs_state);
747 break;
748 }
749
750 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
751
752 /* Actually send the packets */
753 hci_uart_tx_wakeup(hu);
754}
755
756/* Called upon a sleep-indication from the device.
757 */
758static void device_want_to_sleep(struct hci_uart *hu)
759{
760 unsigned long flags;
761 struct qca_data *qca = hu->priv;
762
Rocky Liao6600c082019-08-14 15:42:39 +0800763 BT_DBG("hu %p want to sleep in %d state", hu, qca->rx_ibs_state);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700764
765 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
766
767 qca->ibs_recv_slps++;
768
769 switch (qca->rx_ibs_state) {
770 case HCI_IBS_RX_AWAKE:
771 /* Update state */
772 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
773 /* Vote off rx clock under workqueue */
774 queue_work(qca->workqueue, &qca->ws_rx_vote_off);
775 break;
776
777 case HCI_IBS_RX_ASLEEP:
Rocky Liao6600c082019-08-14 15:42:39 +0800778 break;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700779
780 default:
781 /* Any other state is illegal */
782 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
783 qca->rx_ibs_state);
784 break;
785 }
786
Claire Chang41d5b252019-10-31 18:46:14 +0800787 wake_up_interruptible(&qca->suspend_wait_q);
788
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700789 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
790}
791
792/* Called upon wake-up-acknowledgement from the device
793 */
794static void device_woke_up(struct hci_uart *hu)
795{
796 unsigned long flags, idle_delay;
797 struct qca_data *qca = hu->priv;
798 struct sk_buff *skb = NULL;
799
800 BT_DBG("hu %p woke up", hu);
801
802 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
803
804 qca->ibs_recv_wacks++;
805
Claire Chang41d5b252019-10-31 18:46:14 +0800806 /* Don't react to the wake-up-acknowledgment when suspending. */
807 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
808 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
809 return;
810 }
811
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700812 switch (qca->tx_ibs_state) {
813 case HCI_IBS_TX_AWAKE:
814 /* Expect one if we send 2 WAKEs */
815 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
816 qca->tx_ibs_state);
817 break;
818
819 case HCI_IBS_TX_WAKING:
820 /* Send pending packets */
821 while ((skb = skb_dequeue(&qca->tx_wait_q)))
822 skb_queue_tail(&qca->txq, skb);
823
824 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
825 del_timer(&qca->wake_retrans_timer);
826 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
827 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
828 qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
829 break;
830
831 case HCI_IBS_TX_ASLEEP:
832 /* Fall through */
833
834 default:
835 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
836 qca->tx_ibs_state);
837 break;
838 }
839
840 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
841
842 /* Actually send the packets */
843 hci_uart_tx_wakeup(hu);
844}
845
846/* Enqueue frame for transmittion (padding, crc, etc) may be called from
847 * two simultaneous tasklets.
848 */
849static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
850{
851 unsigned long flags = 0, idle_delay;
852 struct qca_data *qca = hu->priv;
853
854 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
855 qca->tx_ibs_state);
856
857 /* Prepend skb with frame type */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100858 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700859
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +0530860 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
861
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700862 /* Don't go to sleep in middle of patch download or
863 * Out-Of-Band(GPIOs control) sleep is selected.
Claire Chang41d5b252019-10-31 18:46:14 +0800864 * Don't wake the device up when suspending.
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700865 */
Claire Chang41d5b252019-10-31 18:46:14 +0800866 if (!test_bit(QCA_IBS_ENABLED, &qca->flags) ||
867 test_bit(QCA_SUSPENDING, &qca->flags)) {
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700868 skb_queue_tail(&qca->txq, skb);
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +0530869 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700870 return 0;
871 }
872
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700873 /* Act according to current state */
874 switch (qca->tx_ibs_state) {
875 case HCI_IBS_TX_AWAKE:
876 BT_DBG("Device awake, sending normally");
877 skb_queue_tail(&qca->txq, skb);
878 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
879 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
880 break;
881
882 case HCI_IBS_TX_ASLEEP:
883 BT_DBG("Device asleep, waking up and queueing packet");
884 /* Save packet for later */
885 skb_queue_tail(&qca->tx_wait_q, skb);
886
887 qca->tx_ibs_state = HCI_IBS_TX_WAKING;
888 /* Schedule a work queue to wake up device */
889 queue_work(qca->workqueue, &qca->ws_awake_device);
890 break;
891
892 case HCI_IBS_TX_WAKING:
893 BT_DBG("Device waking up, queueing packet");
894 /* Transient state; just keep packet for later */
895 skb_queue_tail(&qca->tx_wait_q, skb);
896 break;
897
898 default:
899 BT_ERR("Illegal tx state: %d (losing packet)",
900 qca->tx_ibs_state);
901 kfree_skb(skb);
902 break;
903 }
904
905 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
906
907 return 0;
908}
909
910static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
911{
912 struct hci_uart *hu = hci_get_drvdata(hdev);
913
914 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
915
916 device_want_to_sleep(hu);
917
918 kfree_skb(skb);
919 return 0;
920}
921
922static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
923{
924 struct hci_uart *hu = hci_get_drvdata(hdev);
925
926 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
927
928 device_want_to_wakeup(hu);
929
930 kfree_skb(skb);
931 return 0;
932}
933
934static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
935{
936 struct hci_uart *hu = hci_get_drvdata(hdev);
937
938 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
939
940 device_woke_up(hu);
941
942 kfree_skb(skb);
943 return 0;
944}
945
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +0530946static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb)
947{
948 /* We receive debug logs from chip as an ACL packets.
949 * Instead of sending the data to ACL to decode the
950 * received data, we are pushing them to the above layers
951 * as a diagnostic packet.
952 */
953 if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE)
954 return hci_recv_diag(hdev, skb);
955
956 return hci_recv_frame(hdev, skb);
957}
958
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530959static void qca_controller_memdump(struct work_struct *work)
960{
961 struct qca_data *qca = container_of(work, struct qca_data,
962 ctrl_memdump_evt);
963 struct hci_uart *hu = qca->hu;
964 struct sk_buff *skb;
965 struct qca_memdump_event_hdr *cmd_hdr;
966 struct qca_memdump_data *qca_memdump = qca->qca_memdump;
967 struct qca_dump_size *dump;
968 char *memdump_buf;
969 char nullBuff[QCA_DUMP_PACKET_SIZE] = { 0 };
YueHaibing56b084e2020-01-08 01:54:31 +0000970 u16 seq_no;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530971 u32 dump_size;
972
973 while ((skb = skb_dequeue(&qca->rx_memdump_q))) {
974
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530975 mutex_lock(&qca->hci_memdump_lock);
976 /* Skip processing the received packets if timeout detected. */
977 if (qca->memdump_state == QCA_MEMDUMP_TIMEOUT) {
978 mutex_unlock(&qca->hci_memdump_lock);
979 return;
980 }
981
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530982 if (!qca_memdump) {
983 qca_memdump = kzalloc(sizeof(struct qca_memdump_data),
984 GFP_ATOMIC);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530985 if (!qca_memdump) {
986 mutex_unlock(&qca->hci_memdump_lock);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530987 return;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530988 }
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530989
990 qca->qca_memdump = qca_memdump;
991 }
992
993 qca->memdump_state = QCA_MEMDUMP_COLLECTING;
994 cmd_hdr = (void *) skb->data;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530995 seq_no = __le16_to_cpu(cmd_hdr->seq_no);
996 skb_pull(skb, sizeof(struct qca_memdump_event_hdr));
997
998 if (!seq_no) {
999
1000 /* This is the first frame of memdump packet from
1001 * the controller, Disable IBS to recevie dump
1002 * with out any interruption, ideally time required for
1003 * the controller to send the dump is 8 seconds. let us
1004 * start timer to handle this asynchronous activity.
1005 */
1006 clear_bit(QCA_IBS_ENABLED, &qca->flags);
1007 set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1008 dump = (void *) skb->data;
1009 dump_size = __le32_to_cpu(dump->dump_size);
1010 if (!(dump_size)) {
1011 bt_dev_err(hu->hdev, "Rx invalid memdump size");
1012 kfree_skb(skb);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301013 mutex_unlock(&qca->hci_memdump_lock);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301014 return;
1015 }
1016
1017 bt_dev_info(hu->hdev, "QCA collecting dump of size:%u",
1018 dump_size);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301019 queue_delayed_work(qca->workqueue,
1020 &qca->ctrl_memdump_timeout,
1021 msecs_to_jiffies(MEMDUMP_TIMEOUT_MS));
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301022
1023 skb_pull(skb, sizeof(dump_size));
1024 memdump_buf = vmalloc(dump_size);
1025 qca_memdump->memdump_buf_head = memdump_buf;
1026 qca_memdump->memdump_buf_tail = memdump_buf;
1027 }
1028
1029 memdump_buf = qca_memdump->memdump_buf_tail;
1030
1031 /* If sequence no 0 is missed then there is no point in
1032 * accepting the other sequences.
1033 */
1034 if (!memdump_buf) {
1035 bt_dev_err(hu->hdev, "QCA: Discarding other packets");
1036 kfree(qca_memdump);
1037 kfree_skb(skb);
1038 qca->qca_memdump = NULL;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301039 mutex_unlock(&qca->hci_memdump_lock);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301040 return;
1041 }
1042
1043 /* There could be chance of missing some packets from
1044 * the controller. In such cases let us store the dummy
1045 * packets in the buffer.
1046 */
1047 while ((seq_no > qca_memdump->current_seq_no + 1) &&
1048 seq_no != QCA_LAST_SEQUENCE_NUM) {
1049 bt_dev_err(hu->hdev, "QCA controller missed packet:%d",
1050 qca_memdump->current_seq_no);
1051 memcpy(memdump_buf, nullBuff, QCA_DUMP_PACKET_SIZE);
1052 memdump_buf = memdump_buf + QCA_DUMP_PACKET_SIZE;
1053 qca_memdump->received_dump += QCA_DUMP_PACKET_SIZE;
1054 qca_memdump->current_seq_no++;
1055 }
1056
1057 memcpy(memdump_buf, (unsigned char *) skb->data, skb->len);
1058 memdump_buf = memdump_buf + skb->len;
1059 qca_memdump->memdump_buf_tail = memdump_buf;
1060 qca_memdump->current_seq_no = seq_no + 1;
1061 qca_memdump->received_dump += skb->len;
1062 qca->qca_memdump = qca_memdump;
1063 kfree_skb(skb);
1064 if (seq_no == QCA_LAST_SEQUENCE_NUM) {
1065 bt_dev_info(hu->hdev, "QCA writing crash dump of size %d bytes",
1066 qca_memdump->received_dump);
1067 memdump_buf = qca_memdump->memdump_buf_head;
1068 dev_coredumpv(&hu->serdev->dev, memdump_buf,
1069 qca_memdump->received_dump, GFP_KERNEL);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301070 cancel_delayed_work(&qca->ctrl_memdump_timeout);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301071 kfree(qca->qca_memdump);
1072 qca->qca_memdump = NULL;
1073 qca->memdump_state = QCA_MEMDUMP_COLLECTED;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301074 clear_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301075 }
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301076
1077 mutex_unlock(&qca->hci_memdump_lock);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301078 }
1079
1080}
1081
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301082static int qca_controller_memdump_event(struct hci_dev *hdev,
1083 struct sk_buff *skb)
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301084{
1085 struct hci_uart *hu = hci_get_drvdata(hdev);
1086 struct qca_data *qca = hu->priv;
1087
1088 skb_queue_tail(&qca->rx_memdump_q, skb);
1089 queue_work(qca->workqueue, &qca->ctrl_memdump_evt);
1090
1091 return 0;
1092}
1093
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001094static int qca_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
1095{
1096 struct hci_uart *hu = hci_get_drvdata(hdev);
1097 struct qca_data *qca = hu->priv;
1098
1099 if (test_bit(QCA_DROP_VENDOR_EVENT, &qca->flags)) {
1100 struct hci_event_hdr *hdr = (void *)skb->data;
1101
1102 /* For the WCN3990 the vendor command for a baudrate change
1103 * isn't sent as synchronous HCI command, because the
1104 * controller sends the corresponding vendor event with the
1105 * new baudrate. The event is received and properly decoded
1106 * after changing the baudrate of the host port. It needs to
1107 * be dropped, otherwise it can be misinterpreted as
1108 * response to a later firmware download command (also a
1109 * vendor command).
1110 */
1111
1112 if (hdr->evt == HCI_EV_VENDOR)
1113 complete(&qca->drop_ev_comp);
1114
Wei Yongjun4974c832019-07-09 01:35:30 +00001115 kfree_skb(skb);
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001116
1117 return 0;
1118 }
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301119 /* We receive chip memory dump as an event packet, With a dedicated
1120 * handler followed by a hardware error event. When this event is
1121 * received we store dump into a file before closing hci. This
1122 * dump will help in triaging the issues.
1123 */
1124 if ((skb->data[0] == HCI_VENDOR_PKT) &&
1125 (get_unaligned_be16(skb->data + 2) == QCA_SSR_DUMP_HANDLE))
1126 return qca_controller_memdump_event(hdev, skb);
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001127
1128 return hci_recv_frame(hdev, skb);
1129}
1130
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001131#define QCA_IBS_SLEEP_IND_EVENT \
1132 .type = HCI_IBS_SLEEP_IND, \
1133 .hlen = 0, \
1134 .loff = 0, \
1135 .lsize = 0, \
1136 .maxlen = HCI_MAX_IBS_SIZE
1137
1138#define QCA_IBS_WAKE_IND_EVENT \
1139 .type = HCI_IBS_WAKE_IND, \
1140 .hlen = 0, \
1141 .loff = 0, \
1142 .lsize = 0, \
1143 .maxlen = HCI_MAX_IBS_SIZE
1144
1145#define QCA_IBS_WAKE_ACK_EVENT \
1146 .type = HCI_IBS_WAKE_ACK, \
1147 .hlen = 0, \
1148 .loff = 0, \
1149 .lsize = 0, \
1150 .maxlen = HCI_MAX_IBS_SIZE
1151
1152static const struct h4_recv_pkt qca_recv_pkts[] = {
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +05301153 { H4_RECV_ACL, .recv = qca_recv_acl_data },
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001154 { H4_RECV_SCO, .recv = hci_recv_frame },
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001155 { H4_RECV_EVENT, .recv = qca_recv_event },
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001156 { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind },
1157 { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack },
1158 { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
1159};
1160
1161static int qca_recv(struct hci_uart *hu, const void *data, int count)
1162{
1163 struct qca_data *qca = hu->priv;
1164
1165 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
1166 return -EUNATCH;
1167
1168 qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
1169 qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
1170 if (IS_ERR(qca->rx_skb)) {
1171 int err = PTR_ERR(qca->rx_skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001172 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001173 qca->rx_skb = NULL;
1174 return err;
1175 }
1176
1177 return count;
1178}
1179
1180static struct sk_buff *qca_dequeue(struct hci_uart *hu)
1181{
1182 struct qca_data *qca = hu->priv;
1183
1184 return skb_dequeue(&qca->txq);
1185}
1186
1187static uint8_t qca_get_baudrate_value(int speed)
1188{
Prasanna Karthikce26d812015-09-15 12:19:45 +00001189 switch (speed) {
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001190 case 9600:
1191 return QCA_BAUDRATE_9600;
1192 case 19200:
1193 return QCA_BAUDRATE_19200;
1194 case 38400:
1195 return QCA_BAUDRATE_38400;
1196 case 57600:
1197 return QCA_BAUDRATE_57600;
1198 case 115200:
1199 return QCA_BAUDRATE_115200;
1200 case 230400:
1201 return QCA_BAUDRATE_230400;
1202 case 460800:
1203 return QCA_BAUDRATE_460800;
1204 case 500000:
1205 return QCA_BAUDRATE_500000;
1206 case 921600:
1207 return QCA_BAUDRATE_921600;
1208 case 1000000:
1209 return QCA_BAUDRATE_1000000;
1210 case 2000000:
1211 return QCA_BAUDRATE_2000000;
1212 case 3000000:
1213 return QCA_BAUDRATE_3000000;
Balakrishna Godavarthibe93a492018-08-03 17:46:30 +05301214 case 3200000:
1215 return QCA_BAUDRATE_3200000;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001216 case 3500000:
1217 return QCA_BAUDRATE_3500000;
1218 default:
1219 return QCA_BAUDRATE_115200;
1220 }
1221}
1222
1223static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
1224{
1225 struct hci_uart *hu = hci_get_drvdata(hdev);
1226 struct qca_data *qca = hu->priv;
1227 struct sk_buff *skb;
1228 u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
1229
Balakrishna Godavarthibe93a492018-08-03 17:46:30 +05301230 if (baudrate > QCA_BAUDRATE_3200000)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001231 return -EINVAL;
1232
1233 cmd[4] = baudrate;
1234
Jia-Ju Bai25a13e382018-07-23 11:56:51 +08001235 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001236 if (!skb) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001237 bt_dev_err(hdev, "Failed to allocate baudrate packet");
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001238 return -ENOMEM;
1239 }
1240
1241 /* Assign commands to change baudrate and packet type. */
Johannes Berg59ae1d12017-06-16 14:29:20 +02001242 skb_put_data(skb, cmd, sizeof(cmd));
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001243 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001244
1245 skb_queue_tail(&qca->txq, skb);
1246 hci_uart_tx_wakeup(hu);
1247
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001248 /* Wait for the baudrate change request to be sent */
1249
1250 while (!skb_queue_empty(&qca->txq))
1251 usleep_range(100, 200);
1252
Matthias Kaehlckeecf2b762019-04-23 11:16:52 -07001253 if (hu->serdev)
1254 serdev_device_wait_until_sent(hu->serdev,
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001255 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1256
1257 /* Give the controller time to process the request */
Harish Bandi523760b2019-04-26 19:26:01 +05301258 if (qca_is_wcn399x(qca_soc_type(hu)))
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001259 msleep(10);
1260 else
1261 msleep(300);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001262
1263 return 0;
1264}
1265
Thierry Escande05ba5332018-03-29 21:15:24 +02001266static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
1267{
1268 if (hu->serdev)
1269 serdev_device_set_baudrate(hu->serdev, speed);
1270 else
1271 hci_uart_set_baudrate(hu, speed);
1272}
1273
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001274static int qca_send_power_pulse(struct hci_uart *hu, bool on)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301275{
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301276 int ret;
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001277 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001278 u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301279
1280 /* These power pulses are single byte command which are sent
1281 * at required baudrate to wcn3990. On wcn3990, we have an external
1282 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1283 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1284 * and also we use the same power inputs to turn on and off for
1285 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1286 * we send a power on pulse at 115200 bps. This algorithm will help to
1287 * save power. Disabling hardware flow control is mandatory while
1288 * sending power pulses to SoC.
1289 */
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301290 bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301291
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301292 serdev_device_write_flush(hu->serdev);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301293 hci_uart_set_flow_control(hu, true);
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301294 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1295 if (ret < 0) {
1296 bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd);
1297 return ret;
1298 }
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301299
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301300 serdev_device_wait_until_sent(hu->serdev, timeout);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301301 hci_uart_set_flow_control(hu, false);
1302
Matthias Kaehlcke0ebcddd2019-02-26 11:46:47 -08001303 /* Give to controller time to boot/shutdown */
Matthias Kaehlckead571d72019-02-26 11:46:46 -08001304 if (on)
1305 msleep(100);
Matthias Kaehlcke0ebcddd2019-02-26 11:46:47 -08001306 else
1307 msleep(10);
Matthias Kaehlckead571d72019-02-26 11:46:46 -08001308
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301309 return 0;
1310}
1311
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301312static unsigned int qca_get_speed(struct hci_uart *hu,
1313 enum qca_speed_type speed_type)
1314{
1315 unsigned int speed = 0;
1316
1317 if (speed_type == QCA_INIT_SPEED) {
1318 if (hu->init_speed)
1319 speed = hu->init_speed;
1320 else if (hu->proto->init_speed)
1321 speed = hu->proto->init_speed;
1322 } else {
1323 if (hu->oper_speed)
1324 speed = hu->oper_speed;
1325 else if (hu->proto->oper_speed)
1326 speed = hu->proto->oper_speed;
1327 }
1328
1329 return speed;
1330}
1331
1332static int qca_check_speeds(struct hci_uart *hu)
1333{
Harish Bandi523760b2019-04-26 19:26:01 +05301334 if (qca_is_wcn399x(qca_soc_type(hu))) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301335 if (!qca_get_speed(hu, QCA_INIT_SPEED) &&
1336 !qca_get_speed(hu, QCA_OPER_SPEED))
1337 return -EINVAL;
1338 } else {
1339 if (!qca_get_speed(hu, QCA_INIT_SPEED) ||
1340 !qca_get_speed(hu, QCA_OPER_SPEED))
1341 return -EINVAL;
1342 }
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301343
1344 return 0;
1345}
1346
1347static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type)
1348{
1349 unsigned int speed, qca_baudrate;
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001350 struct qca_data *qca = hu->priv;
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301351 int ret = 0;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301352
1353 if (speed_type == QCA_INIT_SPEED) {
1354 speed = qca_get_speed(hu, QCA_INIT_SPEED);
1355 if (speed)
1356 host_set_baudrate(hu, speed);
1357 } else {
Matthias Kaehlcke4fdd5a42019-03-11 11:38:31 -07001358 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1359
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301360 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1361 if (!speed)
1362 return 0;
1363
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301364 /* Disable flow control for wcn3990 to deassert RTS while
1365 * changing the baudrate of chip and host.
1366 */
Harish Bandi523760b2019-04-26 19:26:01 +05301367 if (qca_is_wcn399x(soc_type))
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301368 hci_uart_set_flow_control(hu, true);
1369
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001370 if (soc_type == QCA_WCN3990) {
1371 reinit_completion(&qca->drop_ev_comp);
1372 set_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1373 }
1374
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301375 qca_baudrate = qca_get_baudrate_value(speed);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301376 bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed);
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301377 ret = qca_set_baudrate(hu->hdev, qca_baudrate);
1378 if (ret)
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301379 goto error;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301380
1381 host_set_baudrate(hu, speed);
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301382
1383error:
Jeffrey Hugobba79fe2019-10-21 07:18:27 -07001384 if (qca_is_wcn399x(soc_type))
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301385 hci_uart_set_flow_control(hu, false);
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001386
1387 if (soc_type == QCA_WCN3990) {
1388 /* Wait for the controller to send the vendor event
1389 * for the baudrate change command.
1390 */
1391 if (!wait_for_completion_timeout(&qca->drop_ev_comp,
1392 msecs_to_jiffies(100))) {
1393 bt_dev_err(hu->hdev,
1394 "Failed to change controller baudrate\n");
1395 ret = -ETIMEDOUT;
1396 }
1397
1398 clear_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1399 }
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301400 }
1401
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301402 return ret;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301403}
1404
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301405static int qca_send_crashbuffer(struct hci_uart *hu)
1406{
1407 struct qca_data *qca = hu->priv;
1408 struct sk_buff *skb;
1409
1410 skb = bt_skb_alloc(QCA_CRASHBYTE_PACKET_LEN, GFP_KERNEL);
1411 if (!skb) {
1412 bt_dev_err(hu->hdev, "Failed to allocate memory for skb packet");
1413 return -ENOMEM;
1414 }
1415
1416 /* We forcefully crash the controller, by sending 0xfb byte for
1417 * 1024 times. We also might have chance of losing data, To be
1418 * on safer side we send 1096 bytes to the SoC.
1419 */
1420 memset(skb_put(skb, QCA_CRASHBYTE_PACKET_LEN), QCA_MEMDUMP_BYTE,
1421 QCA_CRASHBYTE_PACKET_LEN);
1422 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1423 bt_dev_info(hu->hdev, "crash the soc to collect controller dump");
1424 skb_queue_tail(&qca->txq, skb);
1425 hci_uart_tx_wakeup(hu);
1426
1427 return 0;
1428}
1429
1430static void qca_wait_for_dump_collection(struct hci_dev *hdev)
1431{
1432 struct hci_uart *hu = hci_get_drvdata(hdev);
1433 struct qca_data *qca = hu->priv;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301434
1435 wait_on_bit_timeout(&qca->flags, QCA_MEMDUMP_COLLECTION,
1436 TASK_UNINTERRUPTIBLE, MEMDUMP_TIMEOUT_MS);
1437
1438 clear_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301439}
1440
1441static void qca_hw_error(struct hci_dev *hdev, u8 code)
1442{
1443 struct hci_uart *hu = hci_get_drvdata(hdev);
1444 struct qca_data *qca = hu->priv;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301445 struct qca_memdump_data *qca_memdump = qca->qca_memdump;
1446 char *memdump_buf = NULL;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301447
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301448 set_bit(QCA_HW_ERROR_EVENT, &qca->flags);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301449 bt_dev_info(hdev, "mem_dump_status: %d", qca->memdump_state);
1450
1451 if (qca->memdump_state == QCA_MEMDUMP_IDLE) {
1452 /* If hardware error event received for other than QCA
1453 * soc memory dump event, then we need to crash the SOC
1454 * and wait here for 8 seconds to get the dump packets.
1455 * This will block main thread to be on hold until we
1456 * collect dump.
1457 */
1458 set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1459 qca_send_crashbuffer(hu);
1460 qca_wait_for_dump_collection(hdev);
1461 } else if (qca->memdump_state == QCA_MEMDUMP_COLLECTING) {
1462 /* Let us wait here until memory dump collected or
1463 * memory dump timer expired.
1464 */
1465 bt_dev_info(hdev, "waiting for dump to complete");
1466 qca_wait_for_dump_collection(hdev);
1467 }
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301468
1469 if (qca->memdump_state != QCA_MEMDUMP_COLLECTED) {
1470 bt_dev_err(hu->hdev, "clearing allocated memory due to memdump timeout");
1471 mutex_lock(&qca->hci_memdump_lock);
1472 if (qca_memdump)
1473 memdump_buf = qca_memdump->memdump_buf_head;
1474 vfree(memdump_buf);
1475 kfree(qca_memdump);
1476 qca->qca_memdump = NULL;
1477 qca->memdump_state = QCA_MEMDUMP_TIMEOUT;
1478 cancel_delayed_work(&qca->ctrl_memdump_timeout);
1479 skb_queue_purge(&qca->rx_memdump_q);
1480 mutex_unlock(&qca->hci_memdump_lock);
1481 cancel_work_sync(&qca->ctrl_memdump_evt);
1482 }
1483
1484 clear_bit(QCA_HW_ERROR_EVENT, &qca->flags);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301485}
1486
1487static void qca_cmd_timeout(struct hci_dev *hdev)
1488{
1489 struct hci_uart *hu = hci_get_drvdata(hdev);
1490 struct qca_data *qca = hu->priv;
1491
1492 if (qca->memdump_state == QCA_MEMDUMP_IDLE)
1493 qca_send_crashbuffer(hu);
1494 else
1495 bt_dev_info(hdev, "Dump collection is in process");
1496}
1497
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301498static int qca_wcn3990_init(struct hci_uart *hu)
1499{
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301500 struct qca_serdev *qcadev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301501 int ret;
1502
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301503 /* Check for vregs status, may be hci down has turned
1504 * off the voltage regulator.
1505 */
1506 qcadev = serdev_device_get_drvdata(hu->serdev);
1507 if (!qcadev->bt_power->vregs_on) {
1508 serdev_device_close(hu->serdev);
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001509 ret = qca_regulator_enable(qcadev);
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301510 if (ret)
1511 return ret;
1512
1513 ret = serdev_device_open(hu->serdev);
1514 if (ret) {
1515 bt_dev_err(hu->hdev, "failed to open port");
1516 return ret;
1517 }
1518 }
1519
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301520 /* Forcefully enable wcn3990 to enter in to boot mode. */
1521 host_set_baudrate(hu, 2400);
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001522 ret = qca_send_power_pulse(hu, false);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301523 if (ret)
1524 return ret;
1525
1526 qca_set_speed(hu, QCA_INIT_SPEED);
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001527 ret = qca_send_power_pulse(hu, true);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301528 if (ret)
1529 return ret;
1530
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301531 /* Now the device is in ready state to communicate with host.
1532 * To sync host with device we need to reopen port.
1533 * Without this, we will have RTS and CTS synchronization
1534 * issues.
1535 */
1536 serdev_device_close(hu->serdev);
1537 ret = serdev_device_open(hu->serdev);
1538 if (ret) {
1539 bt_dev_err(hu->hdev, "failed to open port");
1540 return ret;
1541 }
1542
1543 hci_uart_set_flow_control(hu, false);
1544
1545 return 0;
1546}
1547
Rocky Liao5e6d8402020-01-13 12:30:20 +08001548static int qca_power_on(struct hci_dev *hdev)
1549{
1550 struct hci_uart *hu = hci_get_drvdata(hdev);
1551 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1552 struct qca_serdev *qcadev;
1553 int ret = 0;
1554
1555 /* Non-serdev device usually is powered by external power
1556 * and don't need additional action in driver for power on
1557 */
1558 if (!hu->serdev)
1559 return 0;
1560
1561 if (qca_is_wcn399x(soc_type)) {
1562 ret = qca_wcn3990_init(hu);
1563 } else {
1564 qcadev = serdev_device_get_drvdata(hu->serdev);
Rocky Liao8a208b22020-03-04 09:54:29 +08001565 if (!IS_ERR(qcadev->bt_en)) {
1566 gpiod_set_value_cansleep(qcadev->bt_en, 1);
1567 /* Controller needs time to bootup. */
1568 msleep(150);
1569 }
Rocky Liao5e6d8402020-01-13 12:30:20 +08001570 }
1571
1572 return ret;
1573}
1574
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001575static int qca_setup(struct hci_uart *hu)
1576{
1577 struct hci_dev *hdev = hu->hdev;
1578 struct qca_data *qca = hu->priv;
1579 unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
Rocky Liaobb2500ab2020-01-15 16:55:51 +08001580 unsigned int retries = 0;
Matthias Kaehlcke4fdd5a42019-03-11 11:38:31 -07001581 enum qca_btsoc_type soc_type = qca_soc_type(hu);
Rocky Liao99c905c2019-06-06 17:40:30 +08001582 const char *firmware_name = qca_get_firmware_name(hu);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001583 int ret;
Balakrishna Godavarthiaadebac2018-08-03 17:46:28 +05301584 int soc_ver = 0;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001585
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301586 ret = qca_check_speeds(hu);
1587 if (ret)
1588 return ret;
1589
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001590 /* Patch downloading has to be done without IBS mode */
Matthias Kaehlcke62a91992019-04-29 16:21:30 -07001591 clear_bit(QCA_IBS_ENABLED, &qca->flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001592
Rocky Liaoe14c1672019-08-21 14:23:39 +08001593 /* Enable controller to do both LE scan and BR/EDR inquiry
1594 * simultaneously.
1595 */
1596 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1597
Rocky Liao5e6d8402020-01-13 12:30:20 +08001598 bt_dev_info(hdev, "setting up %s",
1599 qca_is_wcn399x(soc_type) ? "wcn399x" : "ROME");
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301600
Rocky Liaobb2500ab2020-01-15 16:55:51 +08001601retry:
Rocky Liao5e6d8402020-01-13 12:30:20 +08001602 ret = qca_power_on(hdev);
1603 if (ret)
1604 return ret;
1605
1606 if (qca_is_wcn399x(soc_type)) {
Matthias Kaehlcke5971752d2019-02-19 12:05:59 -08001607 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301608
Balakrishna Godavarthi7d250a02019-11-06 15:18:32 +05301609 ret = qca_read_soc_version(hdev, &soc_ver, soc_type);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301610 if (ret)
1611 return ret;
1612 } else {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301613 qca_set_speed(hu, QCA_INIT_SPEED);
1614 }
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001615
1616 /* Setup user speed if needed */
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301617 speed = qca_get_speed(hu, QCA_OPER_SPEED);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001618 if (speed) {
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301619 ret = qca_set_speed(hu, QCA_OPER_SPEED);
1620 if (ret)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001621 return ret;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301622
1623 qca_baudrate = qca_get_baudrate_value(speed);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001624 }
1625
Harish Bandi523760b2019-04-26 19:26:01 +05301626 if (!qca_is_wcn399x(soc_type)) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301627 /* Get QCA version information */
Balakrishna Godavarthi7d250a02019-11-06 15:18:32 +05301628 ret = qca_read_soc_version(hdev, &soc_ver, soc_type);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301629 if (ret)
1630 return ret;
1631 }
Balakrishna Godavarthiaadebac2018-08-03 17:46:28 +05301632
1633 bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001634 /* Setup patch / NVM configurations */
Rocky Liao99c905c2019-06-06 17:40:30 +08001635 ret = qca_uart_setup(hdev, qca_baudrate, soc_type, soc_ver,
1636 firmware_name);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001637 if (!ret) {
Matthias Kaehlcke62a91992019-04-29 16:21:30 -07001638 set_bit(QCA_IBS_ENABLED, &qca->flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001639 qca_debugfs_init(hdev);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301640 hu->hdev->hw_error = qca_hw_error;
1641 hu->hdev->cmd_timeout = qca_cmd_timeout;
Loic Poulainba8f3592017-11-06 12:16:56 +01001642 } else if (ret == -ENOENT) {
1643 /* No patch/nvm-config found, run with original fw/config */
1644 ret = 0;
Amit Pundir7dc5fe02018-04-16 12:10:24 +05301645 } else if (ret == -EAGAIN) {
1646 /*
1647 * Userspace firmware loader will return -EAGAIN in case no
1648 * patch/nvm-config is found, so run with original fw/config.
1649 */
1650 ret = 0;
Rocky Liaobb2500ab2020-01-15 16:55:51 +08001651 } else {
1652 if (retries < MAX_INIT_RETRIES) {
1653 qca_power_shutdown(hu);
1654 if (hu->serdev) {
1655 serdev_device_close(hu->serdev);
1656 ret = serdev_device_open(hu->serdev);
1657 if (ret) {
1658 bt_dev_err(hdev, "failed to open port");
1659 return ret;
1660 }
1661 }
1662 retries++;
1663 goto retry;
1664 }
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001665 }
1666
1667 /* Setup bdaddr */
Harish Bandi523760b2019-04-26 19:26:01 +05301668 if (qca_is_wcn399x(soc_type))
Balakrishna Godavarthi5c0a10012019-01-16 18:01:15 +05301669 hu->hdev->set_bdaddr = qca_set_bdaddr;
1670 else
1671 hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001672
1673 return ret;
1674}
1675
Nishka Dasgupta2edc9c52019-08-15 11:21:49 +05301676static const struct hci_uart_proto qca_proto = {
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001677 .id = HCI_UART_QCA,
1678 .name = "QCA",
Marcel Holtmannaee61f72015-10-20 21:30:45 +02001679 .manufacturer = 29,
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001680 .init_speed = 115200,
1681 .oper_speed = 3000000,
1682 .open = qca_open,
1683 .close = qca_close,
1684 .flush = qca_flush,
1685 .setup = qca_setup,
1686 .recv = qca_recv,
1687 .enqueue = qca_enqueue,
1688 .dequeue = qca_dequeue,
1689};
1690
Harish Bandi523760b2019-04-26 19:26:01 +05301691static const struct qca_vreg_data qca_soc_data_wcn3990 = {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301692 .soc_type = QCA_WCN3990,
1693 .vregs = (struct qca_vreg []) {
Bjorn Anderssonf2edd662019-10-17 22:24:02 -07001694 { "vddio", 15000 },
1695 { "vddxo", 80000 },
1696 { "vddrf", 300000 },
1697 { "vddch0", 450000 },
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301698 },
1699 .num_vregs = 4,
1700};
1701
Balakrishna Godavarthi7d250a02019-11-06 15:18:32 +05301702static const struct qca_vreg_data qca_soc_data_wcn3991 = {
1703 .soc_type = QCA_WCN3991,
1704 .vregs = (struct qca_vreg []) {
1705 { "vddio", 15000 },
1706 { "vddxo", 80000 },
1707 { "vddrf", 300000 },
1708 { "vddch0", 450000 },
1709 },
1710 .num_vregs = 4,
1711};
1712
Harish Bandi523760b2019-04-26 19:26:01 +05301713static const struct qca_vreg_data qca_soc_data_wcn3998 = {
1714 .soc_type = QCA_WCN3998,
1715 .vregs = (struct qca_vreg []) {
Bjorn Anderssonf2edd662019-10-17 22:24:02 -07001716 { "vddio", 10000 },
1717 { "vddxo", 80000 },
1718 { "vddrf", 300000 },
1719 { "vddch0", 450000 },
Harish Bandi523760b2019-04-26 19:26:01 +05301720 },
1721 .num_vregs = 4,
1722};
1723
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +05301724static void qca_power_shutdown(struct hci_uart *hu)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301725{
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001726 struct qca_serdev *qcadev;
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +05301727 struct qca_data *qca = hu->priv;
1728 unsigned long flags;
Rocky Liao55599042020-01-15 16:55:50 +08001729 enum qca_btsoc_type soc_type = qca_soc_type(hu);
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +05301730
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001731 qcadev = serdev_device_get_drvdata(hu->serdev);
1732
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +05301733 /* From this point we go into power off state. But serial port is
1734 * still open, stop queueing the IBS data and flush all the buffered
1735 * data in skb's.
1736 */
1737 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
Matthias Kaehlcke62a91992019-04-29 16:21:30 -07001738 clear_bit(QCA_IBS_ENABLED, &qca->flags);
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +05301739 qca_flush(hu);
1740 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1741
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301742 hu->hdev->hw_error = NULL;
1743 hu->hdev->cmd_timeout = NULL;
Rocky Liao55599042020-01-15 16:55:50 +08001744
1745 /* Non-serdev device usually is powered by external power
1746 * and don't need additional action in driver for power down
1747 */
1748 if (!hu->serdev)
1749 return;
1750
1751 if (qca_is_wcn399x(soc_type)) {
1752 host_set_baudrate(hu, 2400);
1753 qca_send_power_pulse(hu, false);
1754 qca_regulator_disable(qcadev);
Rocky Liao8a208b22020-03-04 09:54:29 +08001755 } else if (!IS_ERR(qcadev->bt_en)) {
Rocky Liao55599042020-01-15 16:55:50 +08001756 gpiod_set_value_cansleep(qcadev->bt_en, 0);
1757 }
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301758}
1759
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301760static int qca_power_off(struct hci_dev *hdev)
1761{
1762 struct hci_uart *hu = hci_get_drvdata(hdev);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301763 struct qca_data *qca = hu->priv;
Rocky Liao4f9ed5b2020-02-29 20:21:18 +08001764 enum qca_btsoc_type soc_type = qca_soc_type(hu);
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301765
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301766 /* Stop sending shutdown command if soc crashes. */
Rocky Liao4f9ed5b2020-02-29 20:21:18 +08001767 if (qca_is_wcn399x(soc_type)
1768 && qca->memdump_state == QCA_MEMDUMP_IDLE) {
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301769 qca_send_pre_shutdown_cmd(hdev);
1770 usleep_range(8000, 10000);
1771 }
Harish Bandia2780882019-07-12 10:39:40 +05301772
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301773 qca->memdump_state = QCA_MEMDUMP_IDLE;
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301774 qca_power_shutdown(hu);
1775 return 0;
1776}
1777
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001778static int qca_regulator_enable(struct qca_serdev *qcadev)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301779{
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001780 struct qca_power *power = qcadev->bt_power;
1781 int ret;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301782
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001783 /* Already enabled */
1784 if (power->vregs_on)
1785 return 0;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301786
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001787 BT_DBG("enabling %d regulators)", power->num_vregs);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301788
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001789 ret = regulator_bulk_enable(power->num_vregs, power->vreg_bulk);
1790 if (ret)
1791 return ret;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301792
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001793 power->vregs_on = true;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301794
Venkata Lakshmi Narayana Gubba66cb7052020-02-03 16:10:40 +05301795 ret = clk_prepare_enable(qcadev->susclk);
Venkata Lakshmi Narayana Gubbaf3d63f52020-02-05 16:21:43 +05301796 if (ret)
Venkata Lakshmi Narayana Gubba66cb7052020-02-03 16:10:40 +05301797 qca_regulator_disable(qcadev);
Venkata Lakshmi Narayana Gubba66cb7052020-02-03 16:10:40 +05301798
Venkata Lakshmi Narayana Gubbaf3d63f52020-02-05 16:21:43 +05301799 return ret;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301800}
1801
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001802static void qca_regulator_disable(struct qca_serdev *qcadev)
1803{
1804 struct qca_power *power;
1805
1806 if (!qcadev)
1807 return;
1808
1809 power = qcadev->bt_power;
1810
1811 /* Already disabled? */
1812 if (!power->vregs_on)
1813 return;
1814
1815 regulator_bulk_disable(power->num_vregs, power->vreg_bulk);
1816 power->vregs_on = false;
Venkata Lakshmi Narayana Gubba66cb7052020-02-03 16:10:40 +05301817
Venkata Lakshmi Narayana Gubbaf3d63f52020-02-05 16:21:43 +05301818 clk_disable_unprepare(qcadev->susclk);
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001819}
1820
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301821static int qca_init_regulators(struct qca_power *qca,
1822 const struct qca_vreg *vregs, size_t num_vregs)
1823{
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001824 struct regulator_bulk_data *bulk;
1825 int ret;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301826 int i;
1827
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001828 bulk = devm_kcalloc(qca->dev, num_vregs, sizeof(*bulk), GFP_KERNEL);
1829 if (!bulk)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301830 return -ENOMEM;
1831
1832 for (i = 0; i < num_vregs; i++)
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001833 bulk[i].supply = vregs[i].name;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301834
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001835 ret = devm_regulator_bulk_get(qca->dev, num_vregs, bulk);
1836 if (ret < 0)
1837 return ret;
1838
1839 for (i = 0; i < num_vregs; i++) {
1840 ret = regulator_set_load(bulk[i].consumer, vregs[i].load_uA);
1841 if (ret)
1842 return ret;
1843 }
1844
1845 qca->vreg_bulk = bulk;
Bjorn Andersson163d42f2019-10-17 22:24:03 -07001846 qca->num_vregs = num_vregs;
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001847
1848 return 0;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301849}
1850
Thierry Escande05ba5332018-03-29 21:15:24 +02001851static int qca_serdev_probe(struct serdev_device *serdev)
1852{
1853 struct qca_serdev *qcadev;
Rocky Liaoae563182020-01-16 11:22:54 +08001854 struct hci_dev *hdev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301855 const struct qca_vreg_data *data;
Thierry Escande05ba5332018-03-29 21:15:24 +02001856 int err;
Rocky Liao8a208b22020-03-04 09:54:29 +08001857 bool power_ctrl_enabled = true;
Thierry Escande05ba5332018-03-29 21:15:24 +02001858
1859 qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
1860 if (!qcadev)
1861 return -ENOMEM;
1862
1863 qcadev->serdev_hu.serdev = serdev;
Rocky Liao9f3565b2019-12-13 16:50:45 +08001864 data = device_get_match_data(&serdev->dev);
Thierry Escande05ba5332018-03-29 21:15:24 +02001865 serdev_device_set_drvdata(serdev, qcadev);
Rocky Liao99c905c2019-06-06 17:40:30 +08001866 device_property_read_string(&serdev->dev, "firmware-name",
1867 &qcadev->firmware_name);
Harish Bandi523760b2019-04-26 19:26:01 +05301868 if (data && qca_is_wcn399x(data->soc_type)) {
1869 qcadev->btsoc_type = data->soc_type;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301870 qcadev->bt_power = devm_kzalloc(&serdev->dev,
1871 sizeof(struct qca_power),
1872 GFP_KERNEL);
1873 if (!qcadev->bt_power)
1874 return -ENOMEM;
Thierry Escande05ba5332018-03-29 21:15:24 +02001875
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301876 qcadev->bt_power->dev = &serdev->dev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301877 err = qca_init_regulators(qcadev->bt_power, data->vregs,
1878 data->num_vregs);
1879 if (err) {
1880 BT_ERR("Failed to init regulators:%d", err);
Rocky Liaoae563182020-01-16 11:22:54 +08001881 return err;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301882 }
1883
1884 qcadev->bt_power->vregs_on = false;
1885
Venkata Lakshmi Narayana Gubba66cb7052020-02-03 16:10:40 +05301886 qcadev->susclk = devm_clk_get_optional(&serdev->dev, NULL);
1887 if (IS_ERR(qcadev->susclk)) {
1888 dev_err(&serdev->dev, "failed to acquire clk\n");
1889 return PTR_ERR(qcadev->susclk);
1890 }
1891
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301892 device_property_read_u32(&serdev->dev, "max-speed",
1893 &qcadev->oper_speed);
1894 if (!qcadev->oper_speed)
1895 BT_DBG("UART will pick default operating speed");
1896
1897 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1898 if (err) {
1899 BT_ERR("wcn3990 serdev registration failed");
Rocky Liaoae563182020-01-16 11:22:54 +08001900 return err;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301901 }
1902 } else {
1903 qcadev->btsoc_type = QCA_ROME;
1904 qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable",
1905 GPIOD_OUT_LOW);
1906 if (IS_ERR(qcadev->bt_en)) {
Rocky Liao8a208b22020-03-04 09:54:29 +08001907 dev_warn(&serdev->dev, "failed to acquire enable gpio\n");
1908 power_ctrl_enabled = false;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301909 }
1910
1911 qcadev->susclk = devm_clk_get(&serdev->dev, NULL);
1912 if (IS_ERR(qcadev->susclk)) {
Rocky Liao8a208b22020-03-04 09:54:29 +08001913 dev_warn(&serdev->dev, "failed to acquire clk\n");
1914 } else {
1915 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
1916 if (err)
1917 return err;
1918
1919 err = clk_prepare_enable(qcadev->susclk);
1920 if (err)
1921 return err;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301922 }
1923
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301924 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
Rocky Liaoae563182020-01-16 11:22:54 +08001925 if (err) {
1926 BT_ERR("Rome serdev registration failed");
Rocky Liao8a208b22020-03-04 09:54:29 +08001927 if (!IS_ERR(qcadev->susclk))
1928 clk_disable_unprepare(qcadev->susclk);
Rocky Liaoae563182020-01-16 11:22:54 +08001929 return err;
1930 }
Thierry Escande05ba5332018-03-29 21:15:24 +02001931 }
1932
Rocky Liao8a208b22020-03-04 09:54:29 +08001933 if (power_ctrl_enabled) {
1934 hdev = qcadev->serdev_hu.hdev;
1935 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
1936 hdev->shutdown = qca_power_off;
1937 }
Thierry Escande05ba5332018-03-29 21:15:24 +02001938
Rocky Liaoae563182020-01-16 11:22:54 +08001939 return 0;
Thierry Escande05ba5332018-03-29 21:15:24 +02001940}
1941
1942static void qca_serdev_remove(struct serdev_device *serdev)
1943{
1944 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
1945
Harish Bandi523760b2019-04-26 19:26:01 +05301946 if (qca_is_wcn399x(qcadev->btsoc_type))
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +05301947 qca_power_shutdown(&qcadev->serdev_hu);
Rocky Liao8a208b22020-03-04 09:54:29 +08001948 else if (!IS_ERR(qcadev->susclk))
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301949 clk_disable_unprepare(qcadev->susclk);
Thierry Escande05ba5332018-03-29 21:15:24 +02001950
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301951 hci_uart_unregister_device(&qcadev->serdev_hu);
Thierry Escande05ba5332018-03-29 21:15:24 +02001952}
1953
Claire Chang41d5b252019-10-31 18:46:14 +08001954static int __maybe_unused qca_suspend(struct device *dev)
1955{
1956 struct hci_dev *hdev = container_of(dev, struct hci_dev, dev);
1957 struct hci_uart *hu = hci_get_drvdata(hdev);
1958 struct qca_data *qca = hu->priv;
1959 unsigned long flags;
1960 int ret = 0;
1961 u8 cmd;
1962
1963 set_bit(QCA_SUSPENDING, &qca->flags);
1964
1965 /* Device is downloading patch or doesn't support in-band sleep. */
1966 if (!test_bit(QCA_IBS_ENABLED, &qca->flags))
1967 return 0;
1968
1969 cancel_work_sync(&qca->ws_awake_device);
1970 cancel_work_sync(&qca->ws_awake_rx);
1971
1972 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
1973 flags, SINGLE_DEPTH_NESTING);
1974
1975 switch (qca->tx_ibs_state) {
1976 case HCI_IBS_TX_WAKING:
1977 del_timer(&qca->wake_retrans_timer);
1978 /* Fall through */
1979 case HCI_IBS_TX_AWAKE:
1980 del_timer(&qca->tx_idle_timer);
1981
1982 serdev_device_write_flush(hu->serdev);
1983 cmd = HCI_IBS_SLEEP_IND;
1984 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1985
1986 if (ret < 0) {
1987 BT_ERR("Failed to send SLEEP to device");
1988 break;
1989 }
1990
1991 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
1992 qca->ibs_sent_slps++;
1993
1994 qca_wq_serial_tx_clock_vote_off(&qca->ws_tx_vote_off);
1995 break;
1996
1997 case HCI_IBS_TX_ASLEEP:
1998 break;
1999
2000 default:
2001 BT_ERR("Spurious tx state %d", qca->tx_ibs_state);
2002 ret = -EINVAL;
2003 break;
2004 }
2005
2006 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
2007
2008 if (ret < 0)
2009 goto error;
2010
2011 serdev_device_wait_until_sent(hu->serdev,
2012 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
2013
2014 /* Wait for HCI_IBS_SLEEP_IND sent by device to indicate its Tx is going
2015 * to sleep, so that the packet does not wake the system later.
2016 */
2017
2018 ret = wait_event_interruptible_timeout(qca->suspend_wait_q,
2019 qca->rx_ibs_state == HCI_IBS_RX_ASLEEP,
2020 msecs_to_jiffies(IBS_BTSOC_TX_IDLE_TIMEOUT_MS));
2021
2022 if (ret > 0)
2023 return 0;
2024
2025 if (ret == 0)
2026 ret = -ETIMEDOUT;
2027
2028error:
2029 clear_bit(QCA_SUSPENDING, &qca->flags);
2030
2031 return ret;
2032}
2033
2034static int __maybe_unused qca_resume(struct device *dev)
2035{
2036 struct hci_dev *hdev = container_of(dev, struct hci_dev, dev);
2037 struct hci_uart *hu = hci_get_drvdata(hdev);
2038 struct qca_data *qca = hu->priv;
2039
2040 clear_bit(QCA_SUSPENDING, &qca->flags);
2041
2042 return 0;
2043}
2044
2045static SIMPLE_DEV_PM_OPS(qca_pm_ops, qca_suspend, qca_resume);
2046
Thierry Escande05ba5332018-03-29 21:15:24 +02002047static const struct of_device_id qca_bluetooth_of_match[] = {
2048 { .compatible = "qcom,qca6174-bt" },
Harish Bandi523760b2019-04-26 19:26:01 +05302049 { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990},
Balakrishna Godavarthi7d250a02019-11-06 15:18:32 +05302050 { .compatible = "qcom,wcn3991-bt", .data = &qca_soc_data_wcn3991},
Harish Bandi523760b2019-04-26 19:26:01 +05302051 { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998},
Thierry Escande05ba5332018-03-29 21:15:24 +02002052 { /* sentinel */ }
2053};
2054MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
2055
2056static struct serdev_device_driver qca_serdev_driver = {
2057 .probe = qca_serdev_probe,
2058 .remove = qca_serdev_remove,
2059 .driver = {
2060 .name = "hci_uart_qca",
2061 .of_match_table = qca_bluetooth_of_match,
Claire Chang41d5b252019-10-31 18:46:14 +08002062 .pm = &qca_pm_ops,
Thierry Escande05ba5332018-03-29 21:15:24 +02002063 },
2064};
2065
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07002066int __init qca_init(void)
2067{
Thierry Escande05ba5332018-03-29 21:15:24 +02002068 serdev_device_driver_register(&qca_serdev_driver);
2069
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07002070 return hci_uart_register_proto(&qca_proto);
2071}
2072
2073int __exit qca_deinit(void)
2074{
Thierry Escande05ba5332018-03-29 21:15:24 +02002075 serdev_device_driver_unregister(&qca_serdev_driver);
2076
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07002077 return hci_uart_unregister_proto(&qca_proto);
2078}