blob: bce290bbf22df25810e5b5550ba3a622e1b0e8b1 [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;
Zijun Hue5aeebd2020-05-29 22:38:31 +0800117 u32 ram_dump_size;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530118};
119
120struct qca_memdump_event_hdr {
121 __u8 evt;
122 __u8 plen;
123 __u16 opcode;
124 __u16 seq_no;
125 __u8 reserved;
126} __packed;
127
128
129struct qca_dump_size {
130 u32 dump_size;
131} __packed;
132
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700133struct qca_data {
134 struct hci_uart *hu;
135 struct sk_buff *rx_skb;
136 struct sk_buff_head txq;
137 struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530138 struct sk_buff_head rx_memdump_q; /* Memdump wait queue */
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700139 spinlock_t hci_ibs_lock; /* HCI_IBS state lock */
140 u8 tx_ibs_state; /* HCI_IBS transmit side power state*/
141 u8 rx_ibs_state; /* HCI_IBS receive side power state */
Viresh Kumar621a5f72015-09-26 15:04:07 -0700142 bool tx_vote; /* Clock must be on for TX */
143 bool rx_vote; /* Clock must be on for RX */
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700144 struct timer_list tx_idle_timer;
145 u32 tx_idle_delay;
146 struct timer_list wake_retrans_timer;
147 u32 wake_retrans;
148 struct workqueue_struct *workqueue;
149 struct work_struct ws_awake_rx;
150 struct work_struct ws_awake_device;
151 struct work_struct ws_rx_vote_off;
152 struct work_struct ws_tx_vote_off;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530153 struct work_struct ctrl_memdump_evt;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530154 struct delayed_work ctrl_memdump_timeout;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530155 struct qca_memdump_data *qca_memdump;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700156 unsigned long flags;
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -0700157 struct completion drop_ev_comp;
Claire Chang41d5b252019-10-31 18:46:14 +0800158 wait_queue_head_t suspend_wait_q;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530159 enum qca_memdump_states memdump_state;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530160 struct mutex hci_memdump_lock;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700161
162 /* For debugging purpose */
163 u64 ibs_sent_wacks;
164 u64 ibs_sent_slps;
165 u64 ibs_sent_wakes;
166 u64 ibs_recv_wacks;
167 u64 ibs_recv_slps;
168 u64 ibs_recv_wakes;
169 u64 vote_last_jif;
170 u32 vote_on_ms;
171 u32 vote_off_ms;
172 u64 tx_votes_on;
173 u64 rx_votes_on;
174 u64 tx_votes_off;
175 u64 rx_votes_off;
176 u64 votes_on;
177 u64 votes_off;
178};
179
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +0530180enum qca_speed_type {
181 QCA_INIT_SPEED = 1,
182 QCA_OPER_SPEED
183};
184
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530185/*
186 * Voltage regulator information required for configuring the
187 * QCA Bluetooth chipset
188 */
189struct qca_vreg {
190 const char *name;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530191 unsigned int load_uA;
192};
193
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -0700194struct qca_device_data {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530195 enum qca_btsoc_type soc_type;
196 struct qca_vreg *vregs;
197 size_t num_vregs;
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -0700198 uint32_t capabilities;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530199};
200
201/*
202 * Platform data for the QCA Bluetooth power driver.
203 */
204struct qca_power {
205 struct device *dev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530206 struct regulator_bulk_data *vreg_bulk;
Bjorn Andersson163d42f2019-10-17 22:24:03 -0700207 int num_vregs;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530208 bool vregs_on;
209};
210
Thierry Escande05ba5332018-03-29 21:15:24 +0200211struct qca_serdev {
212 struct hci_uart serdev_hu;
213 struct gpio_desc *bt_en;
214 struct clk *susclk;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530215 enum qca_btsoc_type btsoc_type;
216 struct qca_power *bt_power;
217 u32 init_speed;
218 u32 oper_speed;
Rocky Liao99c905c2019-06-06 17:40:30 +0800219 const char *firmware_name;
Thierry Escande05ba5332018-03-29 21:15:24 +0200220};
221
Bjorn Anderssona9314e72019-10-17 22:24:04 -0700222static int qca_regulator_enable(struct qca_serdev *qcadev);
223static void qca_regulator_disable(struct qca_serdev *qcadev);
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +0530224static void qca_power_shutdown(struct hci_uart *hu);
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +0530225static int qca_power_off(struct hci_dev *hdev);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530226static void qca_controller_memdump(struct work_struct *work);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530227
Matthias Kaehlcke4fdd5a42019-03-11 11:38:31 -0700228static enum qca_btsoc_type qca_soc_type(struct hci_uart *hu)
229{
230 enum qca_btsoc_type soc_type;
231
232 if (hu->serdev) {
233 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
234
235 soc_type = qsd->btsoc_type;
236 } else {
237 soc_type = QCA_ROME;
238 }
239
240 return soc_type;
241}
242
Rocky Liao99c905c2019-06-06 17:40:30 +0800243static const char *qca_get_firmware_name(struct hci_uart *hu)
244{
245 if (hu->serdev) {
246 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
247
248 return qsd->firmware_name;
249 } else {
250 return NULL;
251 }
252}
253
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700254static void __serial_clock_on(struct tty_struct *tty)
255{
256 /* TODO: Some chipset requires to enable UART clock on client
257 * side to save power consumption or manual work is required.
258 * Please put your code to control UART clock here if needed
259 */
260}
261
262static void __serial_clock_off(struct tty_struct *tty)
263{
264 /* TODO: Some chipset requires to disable UART clock on client
265 * side to save power consumption or manual work is required.
266 * Please put your code to control UART clock off here if needed
267 */
268}
269
270/* serial_clock_vote needs to be called with the ibs lock held */
271static void serial_clock_vote(unsigned long vote, struct hci_uart *hu)
272{
273 struct qca_data *qca = hu->priv;
274 unsigned int diff;
275
276 bool old_vote = (qca->tx_vote | qca->rx_vote);
277 bool new_vote;
278
279 switch (vote) {
280 case HCI_IBS_VOTE_STATS_UPDATE:
281 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
282
283 if (old_vote)
284 qca->vote_off_ms += diff;
285 else
286 qca->vote_on_ms += diff;
287 return;
288
289 case HCI_IBS_TX_VOTE_CLOCK_ON:
290 qca->tx_vote = true;
291 qca->tx_votes_on++;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700292 break;
293
294 case HCI_IBS_RX_VOTE_CLOCK_ON:
295 qca->rx_vote = true;
296 qca->rx_votes_on++;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700297 break;
298
299 case HCI_IBS_TX_VOTE_CLOCK_OFF:
300 qca->tx_vote = false;
301 qca->tx_votes_off++;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700302 break;
303
304 case HCI_IBS_RX_VOTE_CLOCK_OFF:
305 qca->rx_vote = false;
306 qca->rx_votes_off++;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700307 break;
308
309 default:
310 BT_ERR("Voting irregularity");
311 return;
312 }
313
Matthias Kaehlcke7310dd32020-06-06 09:53:06 -0700314 new_vote = qca->rx_vote | qca->tx_vote;
315
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700316 if (new_vote != old_vote) {
317 if (new_vote)
318 __serial_clock_on(hu->tty);
319 else
320 __serial_clock_off(hu->tty);
321
Prasanna Karthikce26d812015-09-15 12:19:45 +0000322 BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false",
323 vote ? "true" : "false");
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700324
325 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
326
327 if (new_vote) {
328 qca->votes_on++;
329 qca->vote_off_ms += diff;
330 } else {
331 qca->votes_off++;
332 qca->vote_on_ms += diff;
333 }
334 qca->vote_last_jif = jiffies;
335 }
336}
337
338/* Builds and sends an HCI_IBS command packet.
339 * These are very simple packets with only 1 cmd byte.
340 */
341static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
342{
343 int err = 0;
344 struct sk_buff *skb = NULL;
345 struct qca_data *qca = hu->priv;
346
347 BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd);
348
349 skb = bt_skb_alloc(1, GFP_ATOMIC);
350 if (!skb) {
351 BT_ERR("Failed to allocate memory for HCI_IBS packet");
352 return -ENOMEM;
353 }
354
355 /* Assign HCI_IBS type */
Johannes Berg634fef62017-06-16 14:29:24 +0200356 skb_put_u8(skb, cmd);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700357
358 skb_queue_tail(&qca->txq, skb);
359
360 return err;
361}
362
363static void qca_wq_awake_device(struct work_struct *work)
364{
365 struct qca_data *qca = container_of(work, struct qca_data,
366 ws_awake_device);
367 struct hci_uart *hu = qca->hu;
368 unsigned long retrans_delay;
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530369 unsigned long flags;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700370
371 BT_DBG("hu %p wq awake device", hu);
372
373 /* Vote for serial clock */
374 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
375
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530376 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700377
378 /* Send wake indication to device */
379 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
380 BT_ERR("Failed to send WAKE to device");
381
382 qca->ibs_sent_wakes++;
383
384 /* Start retransmit timer */
385 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
386 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
387
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530388 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700389
390 /* Actually send the packets */
391 hci_uart_tx_wakeup(hu);
392}
393
394static void qca_wq_awake_rx(struct work_struct *work)
395{
396 struct qca_data *qca = container_of(work, struct qca_data,
397 ws_awake_rx);
398 struct hci_uart *hu = qca->hu;
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530399 unsigned long flags;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700400
401 BT_DBG("hu %p wq awake rx", hu);
402
403 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
404
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530405 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700406 qca->rx_ibs_state = HCI_IBS_RX_AWAKE;
407
408 /* Always acknowledge device wake up,
409 * sending IBS message doesn't count as TX ON.
410 */
411 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
412 BT_ERR("Failed to acknowledge device wake up");
413
414 qca->ibs_sent_wacks++;
415
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530416 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700417
418 /* Actually send the packets */
419 hci_uart_tx_wakeup(hu);
420}
421
422static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work)
423{
424 struct qca_data *qca = container_of(work, struct qca_data,
425 ws_rx_vote_off);
426 struct hci_uart *hu = qca->hu;
427
428 BT_DBG("hu %p rx clock vote off", hu);
429
430 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
431}
432
433static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work)
434{
435 struct qca_data *qca = container_of(work, struct qca_data,
436 ws_tx_vote_off);
437 struct hci_uart *hu = qca->hu;
438
439 BT_DBG("hu %p tx clock vote off", hu);
440
441 /* Run HCI tx handling unlocked */
442 hci_uart_tx_wakeup(hu);
443
444 /* Now that message queued to tty driver, vote for tty clocks off.
445 * It is up to the tty driver to pend the clocks off until tx done.
446 */
447 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
448}
449
Kees Cook04356052017-10-04 17:54:29 -0700450static void hci_ibs_tx_idle_timeout(struct timer_list *t)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700451{
Kees Cook04356052017-10-04 17:54:29 -0700452 struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
453 struct hci_uart *hu = qca->hu;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700454 unsigned long flags;
455
456 BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state);
457
458 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
459 flags, SINGLE_DEPTH_NESTING);
460
461 switch (qca->tx_ibs_state) {
462 case HCI_IBS_TX_AWAKE:
463 /* TX_IDLE, go to SLEEP */
464 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
465 BT_ERR("Failed to send SLEEP to device");
466 break;
467 }
468 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
469 qca->ibs_sent_slps++;
470 queue_work(qca->workqueue, &qca->ws_tx_vote_off);
471 break;
472
473 case HCI_IBS_TX_ASLEEP:
474 case HCI_IBS_TX_WAKING:
475 /* Fall through */
476
477 default:
Colin Ian Kinge059a462017-02-17 19:58:10 +0000478 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700479 break;
480 }
481
482 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
483}
484
Kees Cook04356052017-10-04 17:54:29 -0700485static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700486{
Kees Cook04356052017-10-04 17:54:29 -0700487 struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
488 struct hci_uart *hu = qca->hu;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700489 unsigned long flags, retrans_delay;
Prasanna Karthika9137182015-09-28 08:03:24 +0000490 bool retransmit = false;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700491
492 BT_DBG("hu %p wake retransmit timeout in %d state",
493 hu, qca->tx_ibs_state);
494
495 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
496 flags, SINGLE_DEPTH_NESTING);
497
Claire Chang41d5b252019-10-31 18:46:14 +0800498 /* Don't retransmit the HCI_IBS_WAKE_IND when suspending. */
499 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
500 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
501 return;
502 }
503
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700504 switch (qca->tx_ibs_state) {
505 case HCI_IBS_TX_WAKING:
506 /* No WAKE_ACK, retransmit WAKE */
Prasanna Karthika9137182015-09-28 08:03:24 +0000507 retransmit = true;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700508 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
509 BT_ERR("Failed to acknowledge device wake up");
510 break;
511 }
512 qca->ibs_sent_wakes++;
513 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
514 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
515 break;
516
517 case HCI_IBS_TX_ASLEEP:
518 case HCI_IBS_TX_AWAKE:
519 /* Fall through */
520
521 default:
Colin Ian Kinge059a462017-02-17 19:58:10 +0000522 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700523 break;
524 }
525
526 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
527
528 if (retransmit)
529 hci_uart_tx_wakeup(hu);
530}
531
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530532
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530533static void qca_controller_memdump_timeout(struct work_struct *work)
534{
535 struct qca_data *qca = container_of(work, struct qca_data,
536 ctrl_memdump_timeout.work);
537 struct hci_uart *hu = qca->hu;
538
539 mutex_lock(&qca->hci_memdump_lock);
540 if (test_bit(QCA_MEMDUMP_COLLECTION, &qca->flags)) {
541 qca->memdump_state = QCA_MEMDUMP_TIMEOUT;
542 if (!test_bit(QCA_HW_ERROR_EVENT, &qca->flags)) {
543 /* Inject hw error event to reset the device
544 * and driver.
545 */
546 hci_reset_dev(hu->hdev);
547 }
548 }
549
550 mutex_unlock(&qca->hci_memdump_lock);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530551}
552
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530553
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700554/* Initialize protocol */
555static int qca_open(struct hci_uart *hu)
556{
Thierry Escande05ba5332018-03-29 21:15:24 +0200557 struct qca_serdev *qcadev;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700558 struct qca_data *qca;
559
560 BT_DBG("hu %p qca_open", hu);
561
Vladis Dronovb36a1552019-07-30 11:33:45 +0200562 if (!hci_uart_has_flow_control(hu))
563 return -EOPNOTSUPP;
564
Jia-Ju Bai25a13e382018-07-23 11:56:51 +0800565 qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700566 if (!qca)
567 return -ENOMEM;
568
569 skb_queue_head_init(&qca->txq);
570 skb_queue_head_init(&qca->tx_wait_q);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530571 skb_queue_head_init(&qca->rx_memdump_q);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700572 spin_lock_init(&qca->hci_ibs_lock);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530573 mutex_init(&qca->hci_memdump_lock);
Bhaktipriya Shridharfac9a602016-08-30 22:42:53 +0530574 qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700575 if (!qca->workqueue) {
576 BT_ERR("QCA Workqueue not initialized properly");
577 kfree(qca);
578 return -ENOMEM;
579 }
580
581 INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
582 INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
583 INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
584 INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530585 INIT_WORK(&qca->ctrl_memdump_evt, qca_controller_memdump);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530586 INIT_DELAYED_WORK(&qca->ctrl_memdump_timeout,
587 qca_controller_memdump_timeout);
Claire Chang41d5b252019-10-31 18:46:14 +0800588 init_waitqueue_head(&qca->suspend_wait_q);
589
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700590 qca->hu = hu;
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -0700591 init_completion(&qca->drop_ev_comp);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700592
593 /* Assume we start with both sides asleep -- extra wakes OK */
594 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
595 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
596
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700597 qca->vote_last_jif = jiffies;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700598
599 hu->priv = qca;
600
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530601 if (hu->serdev) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530602 qcadev = serdev_device_get_drvdata(hu->serdev);
Christian Hewitt37aee132020-04-23 01:34:30 +0000603
604 if (qca_is_wcn399x(qcadev->btsoc_type))
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530605 hu->init_speed = qcadev->init_speed;
Christian Hewitt37aee132020-04-23 01:34:30 +0000606
607 if (qcadev->oper_speed)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530608 hu->oper_speed = qcadev->oper_speed;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530609 }
610
Kees Cook04356052017-10-04 17:54:29 -0700611 timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700612 qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
613
Kees Cook04356052017-10-04 17:54:29 -0700614 timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
Claire Chang41d5b252019-10-31 18:46:14 +0800615 qca->tx_idle_delay = IBS_HOST_TX_IDLE_TIMEOUT_MS;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700616
617 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
618 qca->tx_idle_delay, qca->wake_retrans);
619
620 return 0;
621}
622
623static void qca_debugfs_init(struct hci_dev *hdev)
624{
625 struct hci_uart *hu = hci_get_drvdata(hdev);
626 struct qca_data *qca = hu->priv;
627 struct dentry *ibs_dir;
628 umode_t mode;
629
630 if (!hdev->debugfs)
631 return;
632
633 ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
634
635 /* read only */
636 mode = S_IRUGO;
637 debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
638 debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
639 debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
640 &qca->ibs_sent_slps);
641 debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
642 &qca->ibs_sent_wakes);
643 debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
644 &qca->ibs_sent_wacks);
645 debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
646 &qca->ibs_recv_slps);
647 debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
648 &qca->ibs_recv_wakes);
649 debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
650 &qca->ibs_recv_wacks);
Ben YoungTae Kim10be6c02015-08-13 22:09:42 -0700651 debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700652 debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
653 debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
Ben YoungTae Kim10be6c02015-08-13 22:09:42 -0700654 debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700655 debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
656 debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
657 debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
658 debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
659 debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
660 debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
661
662 /* read/write */
663 mode = S_IRUGO | S_IWUSR;
664 debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
665 debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
666 &qca->tx_idle_delay);
667}
668
669/* Flush protocol data */
670static int qca_flush(struct hci_uart *hu)
671{
672 struct qca_data *qca = hu->priv;
673
674 BT_DBG("hu %p qca flush", hu);
675
676 skb_queue_purge(&qca->tx_wait_q);
677 skb_queue_purge(&qca->txq);
678
679 return 0;
680}
681
682/* Close protocol */
683static int qca_close(struct hci_uart *hu)
684{
685 struct qca_data *qca = hu->priv;
686
687 BT_DBG("hu %p qca close", hu);
688
689 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
690
691 skb_queue_purge(&qca->tx_wait_q);
692 skb_queue_purge(&qca->txq);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530693 skb_queue_purge(&qca->rx_memdump_q);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700694 del_timer(&qca->tx_idle_timer);
695 del_timer(&qca->wake_retrans_timer);
696 destroy_workqueue(qca->workqueue);
697 qca->hu = NULL;
698
Rocky Liao55599042020-01-15 16:55:50 +0800699 qca_power_shutdown(hu);
Thierry Escande05ba5332018-03-29 21:15:24 +0200700
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700701 kfree_skb(qca->rx_skb);
702
703 hu->priv = NULL;
704
705 kfree(qca);
706
707 return 0;
708}
709
710/* Called upon a wake-up-indication from the device.
711 */
712static void device_want_to_wakeup(struct hci_uart *hu)
713{
714 unsigned long flags;
715 struct qca_data *qca = hu->priv;
716
717 BT_DBG("hu %p want to wake up", hu);
718
719 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
720
721 qca->ibs_recv_wakes++;
722
Claire Chang41d5b252019-10-31 18:46:14 +0800723 /* Don't wake the rx up when suspending. */
724 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
725 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
726 return;
727 }
728
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700729 switch (qca->rx_ibs_state) {
730 case HCI_IBS_RX_ASLEEP:
731 /* Make sure clock is on - we may have turned clock off since
732 * receiving the wake up indicator awake rx clock.
733 */
734 queue_work(qca->workqueue, &qca->ws_awake_rx);
735 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
736 return;
737
738 case HCI_IBS_RX_AWAKE:
739 /* Always acknowledge device wake up,
740 * sending IBS message doesn't count as TX ON.
741 */
742 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
743 BT_ERR("Failed to acknowledge device wake up");
744 break;
745 }
746 qca->ibs_sent_wacks++;
747 break;
748
749 default:
750 /* Any other state is illegal */
751 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
752 qca->rx_ibs_state);
753 break;
754 }
755
756 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
757
758 /* Actually send the packets */
759 hci_uart_tx_wakeup(hu);
760}
761
762/* Called upon a sleep-indication from the device.
763 */
764static void device_want_to_sleep(struct hci_uart *hu)
765{
766 unsigned long flags;
767 struct qca_data *qca = hu->priv;
768
Rocky Liao6600c082019-08-14 15:42:39 +0800769 BT_DBG("hu %p want to sleep in %d state", hu, qca->rx_ibs_state);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700770
771 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
772
773 qca->ibs_recv_slps++;
774
775 switch (qca->rx_ibs_state) {
776 case HCI_IBS_RX_AWAKE:
777 /* Update state */
778 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
779 /* Vote off rx clock under workqueue */
780 queue_work(qca->workqueue, &qca->ws_rx_vote_off);
781 break;
782
783 case HCI_IBS_RX_ASLEEP:
Rocky Liao6600c082019-08-14 15:42:39 +0800784 break;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700785
786 default:
787 /* Any other state is illegal */
788 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
789 qca->rx_ibs_state);
790 break;
791 }
792
Claire Chang41d5b252019-10-31 18:46:14 +0800793 wake_up_interruptible(&qca->suspend_wait_q);
794
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700795 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
796}
797
798/* Called upon wake-up-acknowledgement from the device
799 */
800static void device_woke_up(struct hci_uart *hu)
801{
802 unsigned long flags, idle_delay;
803 struct qca_data *qca = hu->priv;
804 struct sk_buff *skb = NULL;
805
806 BT_DBG("hu %p woke up", hu);
807
808 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
809
810 qca->ibs_recv_wacks++;
811
Claire Chang41d5b252019-10-31 18:46:14 +0800812 /* Don't react to the wake-up-acknowledgment when suspending. */
813 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
814 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
815 return;
816 }
817
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700818 switch (qca->tx_ibs_state) {
819 case HCI_IBS_TX_AWAKE:
820 /* Expect one if we send 2 WAKEs */
821 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
822 qca->tx_ibs_state);
823 break;
824
825 case HCI_IBS_TX_WAKING:
826 /* Send pending packets */
827 while ((skb = skb_dequeue(&qca->tx_wait_q)))
828 skb_queue_tail(&qca->txq, skb);
829
830 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
831 del_timer(&qca->wake_retrans_timer);
832 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
833 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
834 qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
835 break;
836
837 case HCI_IBS_TX_ASLEEP:
838 /* Fall through */
839
840 default:
841 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
842 qca->tx_ibs_state);
843 break;
844 }
845
846 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
847
848 /* Actually send the packets */
849 hci_uart_tx_wakeup(hu);
850}
851
852/* Enqueue frame for transmittion (padding, crc, etc) may be called from
853 * two simultaneous tasklets.
854 */
855static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
856{
857 unsigned long flags = 0, idle_delay;
858 struct qca_data *qca = hu->priv;
859
860 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
861 qca->tx_ibs_state);
862
863 /* Prepend skb with frame type */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100864 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700865
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +0530866 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
867
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700868 /* Don't go to sleep in middle of patch download or
869 * Out-Of-Band(GPIOs control) sleep is selected.
Claire Chang41d5b252019-10-31 18:46:14 +0800870 * Don't wake the device up when suspending.
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700871 */
Claire Chang41d5b252019-10-31 18:46:14 +0800872 if (!test_bit(QCA_IBS_ENABLED, &qca->flags) ||
873 test_bit(QCA_SUSPENDING, &qca->flags)) {
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700874 skb_queue_tail(&qca->txq, skb);
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +0530875 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700876 return 0;
877 }
878
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700879 /* Act according to current state */
880 switch (qca->tx_ibs_state) {
881 case HCI_IBS_TX_AWAKE:
882 BT_DBG("Device awake, sending normally");
883 skb_queue_tail(&qca->txq, skb);
884 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
885 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
886 break;
887
888 case HCI_IBS_TX_ASLEEP:
889 BT_DBG("Device asleep, waking up and queueing packet");
890 /* Save packet for later */
891 skb_queue_tail(&qca->tx_wait_q, skb);
892
893 qca->tx_ibs_state = HCI_IBS_TX_WAKING;
894 /* Schedule a work queue to wake up device */
895 queue_work(qca->workqueue, &qca->ws_awake_device);
896 break;
897
898 case HCI_IBS_TX_WAKING:
899 BT_DBG("Device waking up, queueing packet");
900 /* Transient state; just keep packet for later */
901 skb_queue_tail(&qca->tx_wait_q, skb);
902 break;
903
904 default:
905 BT_ERR("Illegal tx state: %d (losing packet)",
906 qca->tx_ibs_state);
907 kfree_skb(skb);
908 break;
909 }
910
911 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
912
913 return 0;
914}
915
916static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
917{
918 struct hci_uart *hu = hci_get_drvdata(hdev);
919
920 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
921
922 device_want_to_sleep(hu);
923
924 kfree_skb(skb);
925 return 0;
926}
927
928static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
929{
930 struct hci_uart *hu = hci_get_drvdata(hdev);
931
932 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
933
934 device_want_to_wakeup(hu);
935
936 kfree_skb(skb);
937 return 0;
938}
939
940static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
941{
942 struct hci_uart *hu = hci_get_drvdata(hdev);
943
944 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
945
946 device_woke_up(hu);
947
948 kfree_skb(skb);
949 return 0;
950}
951
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +0530952static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb)
953{
954 /* We receive debug logs from chip as an ACL packets.
955 * Instead of sending the data to ACL to decode the
956 * received data, we are pushing them to the above layers
957 * as a diagnostic packet.
958 */
959 if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE)
960 return hci_recv_diag(hdev, skb);
961
962 return hci_recv_frame(hdev, skb);
963}
964
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530965static void qca_controller_memdump(struct work_struct *work)
966{
967 struct qca_data *qca = container_of(work, struct qca_data,
968 ctrl_memdump_evt);
969 struct hci_uart *hu = qca->hu;
970 struct sk_buff *skb;
971 struct qca_memdump_event_hdr *cmd_hdr;
972 struct qca_memdump_data *qca_memdump = qca->qca_memdump;
973 struct qca_dump_size *dump;
974 char *memdump_buf;
975 char nullBuff[QCA_DUMP_PACKET_SIZE] = { 0 };
YueHaibing56b084e2020-01-08 01:54:31 +0000976 u16 seq_no;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530977 u32 dump_size;
Zijun Hue5aeebd2020-05-29 22:38:31 +0800978 u32 rx_size;
979 enum qca_btsoc_type soc_type = qca_soc_type(hu);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530980
981 while ((skb = skb_dequeue(&qca->rx_memdump_q))) {
982
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530983 mutex_lock(&qca->hci_memdump_lock);
984 /* Skip processing the received packets if timeout detected. */
985 if (qca->memdump_state == QCA_MEMDUMP_TIMEOUT) {
986 mutex_unlock(&qca->hci_memdump_lock);
987 return;
988 }
989
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530990 if (!qca_memdump) {
991 qca_memdump = kzalloc(sizeof(struct qca_memdump_data),
992 GFP_ATOMIC);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530993 if (!qca_memdump) {
994 mutex_unlock(&qca->hci_memdump_lock);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530995 return;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +0530996 }
Balakrishna Godavarthid8415022020-01-02 20:19:11 +0530997
998 qca->qca_memdump = qca_memdump;
999 }
1000
1001 qca->memdump_state = QCA_MEMDUMP_COLLECTING;
1002 cmd_hdr = (void *) skb->data;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301003 seq_no = __le16_to_cpu(cmd_hdr->seq_no);
1004 skb_pull(skb, sizeof(struct qca_memdump_event_hdr));
1005
1006 if (!seq_no) {
1007
1008 /* This is the first frame of memdump packet from
1009 * the controller, Disable IBS to recevie dump
1010 * with out any interruption, ideally time required for
1011 * the controller to send the dump is 8 seconds. let us
1012 * start timer to handle this asynchronous activity.
1013 */
1014 clear_bit(QCA_IBS_ENABLED, &qca->flags);
1015 set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1016 dump = (void *) skb->data;
1017 dump_size = __le32_to_cpu(dump->dump_size);
1018 if (!(dump_size)) {
1019 bt_dev_err(hu->hdev, "Rx invalid memdump size");
1020 kfree_skb(skb);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301021 mutex_unlock(&qca->hci_memdump_lock);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301022 return;
1023 }
1024
1025 bt_dev_info(hu->hdev, "QCA collecting dump of size:%u",
1026 dump_size);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301027 queue_delayed_work(qca->workqueue,
1028 &qca->ctrl_memdump_timeout,
Zijun Hue5aeebd2020-05-29 22:38:31 +08001029 msecs_to_jiffies(MEMDUMP_TIMEOUT_MS)
1030 );
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301031
1032 skb_pull(skb, sizeof(dump_size));
1033 memdump_buf = vmalloc(dump_size);
Zijun Hue5aeebd2020-05-29 22:38:31 +08001034 qca_memdump->ram_dump_size = dump_size;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301035 qca_memdump->memdump_buf_head = memdump_buf;
1036 qca_memdump->memdump_buf_tail = memdump_buf;
1037 }
1038
1039 memdump_buf = qca_memdump->memdump_buf_tail;
1040
1041 /* If sequence no 0 is missed then there is no point in
1042 * accepting the other sequences.
1043 */
1044 if (!memdump_buf) {
1045 bt_dev_err(hu->hdev, "QCA: Discarding other packets");
1046 kfree(qca_memdump);
1047 kfree_skb(skb);
1048 qca->qca_memdump = NULL;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301049 mutex_unlock(&qca->hci_memdump_lock);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301050 return;
1051 }
1052
1053 /* There could be chance of missing some packets from
1054 * the controller. In such cases let us store the dummy
1055 * packets in the buffer.
1056 */
Zijun Hue5aeebd2020-05-29 22:38:31 +08001057 /* For QCA6390, controller does not lost packets but
1058 * sequence number field of packat sometimes has error
1059 * bits, so skip this checking for missing packet.
1060 */
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301061 while ((seq_no > qca_memdump->current_seq_no + 1) &&
Zijun Hue5aeebd2020-05-29 22:38:31 +08001062 (soc_type != QCA_QCA6390) &&
1063 seq_no != QCA_LAST_SEQUENCE_NUM) {
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301064 bt_dev_err(hu->hdev, "QCA controller missed packet:%d",
1065 qca_memdump->current_seq_no);
Zijun Hue5aeebd2020-05-29 22:38:31 +08001066 rx_size = qca_memdump->received_dump;
1067 rx_size += QCA_DUMP_PACKET_SIZE;
1068 if (rx_size > qca_memdump->ram_dump_size) {
1069 bt_dev_err(hu->hdev,
1070 "QCA memdump received %d, no space for missed packet",
1071 qca_memdump->received_dump);
1072 break;
1073 }
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301074 memcpy(memdump_buf, nullBuff, QCA_DUMP_PACKET_SIZE);
1075 memdump_buf = memdump_buf + QCA_DUMP_PACKET_SIZE;
1076 qca_memdump->received_dump += QCA_DUMP_PACKET_SIZE;
1077 qca_memdump->current_seq_no++;
1078 }
1079
Zijun Hue5aeebd2020-05-29 22:38:31 +08001080 rx_size = qca_memdump->received_dump + skb->len;
1081 if (rx_size <= qca_memdump->ram_dump_size) {
1082 if ((seq_no != QCA_LAST_SEQUENCE_NUM) &&
1083 (seq_no != qca_memdump->current_seq_no))
1084 bt_dev_err(hu->hdev,
1085 "QCA memdump unexpected packet %d",
1086 seq_no);
1087 bt_dev_dbg(hu->hdev,
1088 "QCA memdump packet %d with length %d",
1089 seq_no, skb->len);
1090 memcpy(memdump_buf, (unsigned char *)skb->data,
1091 skb->len);
1092 memdump_buf = memdump_buf + skb->len;
1093 qca_memdump->memdump_buf_tail = memdump_buf;
1094 qca_memdump->current_seq_no = seq_no + 1;
1095 qca_memdump->received_dump += skb->len;
1096 } else {
1097 bt_dev_err(hu->hdev,
1098 "QCA memdump received %d, no space for packet %d",
1099 qca_memdump->received_dump, seq_no);
1100 }
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301101 qca->qca_memdump = qca_memdump;
1102 kfree_skb(skb);
1103 if (seq_no == QCA_LAST_SEQUENCE_NUM) {
Zijun Hue5aeebd2020-05-29 22:38:31 +08001104 bt_dev_info(hu->hdev,
1105 "QCA memdump Done, received %d, total %d",
1106 qca_memdump->received_dump,
1107 qca_memdump->ram_dump_size);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301108 memdump_buf = qca_memdump->memdump_buf_head;
1109 dev_coredumpv(&hu->serdev->dev, memdump_buf,
1110 qca_memdump->received_dump, GFP_KERNEL);
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301111 cancel_delayed_work(&qca->ctrl_memdump_timeout);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301112 kfree(qca->qca_memdump);
1113 qca->qca_memdump = NULL;
1114 qca->memdump_state = QCA_MEMDUMP_COLLECTED;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301115 clear_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301116 }
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301117
1118 mutex_unlock(&qca->hci_memdump_lock);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301119 }
1120
1121}
1122
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301123static int qca_controller_memdump_event(struct hci_dev *hdev,
1124 struct sk_buff *skb)
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301125{
1126 struct hci_uart *hu = hci_get_drvdata(hdev);
1127 struct qca_data *qca = hu->priv;
1128
1129 skb_queue_tail(&qca->rx_memdump_q, skb);
1130 queue_work(qca->workqueue, &qca->ctrl_memdump_evt);
1131
1132 return 0;
1133}
1134
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001135static int qca_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
1136{
1137 struct hci_uart *hu = hci_get_drvdata(hdev);
1138 struct qca_data *qca = hu->priv;
1139
1140 if (test_bit(QCA_DROP_VENDOR_EVENT, &qca->flags)) {
1141 struct hci_event_hdr *hdr = (void *)skb->data;
1142
1143 /* For the WCN3990 the vendor command for a baudrate change
1144 * isn't sent as synchronous HCI command, because the
1145 * controller sends the corresponding vendor event with the
1146 * new baudrate. The event is received and properly decoded
1147 * after changing the baudrate of the host port. It needs to
1148 * be dropped, otherwise it can be misinterpreted as
1149 * response to a later firmware download command (also a
1150 * vendor command).
1151 */
1152
1153 if (hdr->evt == HCI_EV_VENDOR)
1154 complete(&qca->drop_ev_comp);
1155
Wei Yongjun4974c832019-07-09 01:35:30 +00001156 kfree_skb(skb);
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001157
1158 return 0;
1159 }
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301160 /* We receive chip memory dump as an event packet, With a dedicated
1161 * handler followed by a hardware error event. When this event is
1162 * received we store dump into a file before closing hci. This
1163 * dump will help in triaging the issues.
1164 */
1165 if ((skb->data[0] == HCI_VENDOR_PKT) &&
1166 (get_unaligned_be16(skb->data + 2) == QCA_SSR_DUMP_HANDLE))
1167 return qca_controller_memdump_event(hdev, skb);
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001168
1169 return hci_recv_frame(hdev, skb);
1170}
1171
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001172#define QCA_IBS_SLEEP_IND_EVENT \
1173 .type = HCI_IBS_SLEEP_IND, \
1174 .hlen = 0, \
1175 .loff = 0, \
1176 .lsize = 0, \
1177 .maxlen = HCI_MAX_IBS_SIZE
1178
1179#define QCA_IBS_WAKE_IND_EVENT \
1180 .type = HCI_IBS_WAKE_IND, \
1181 .hlen = 0, \
1182 .loff = 0, \
1183 .lsize = 0, \
1184 .maxlen = HCI_MAX_IBS_SIZE
1185
1186#define QCA_IBS_WAKE_ACK_EVENT \
1187 .type = HCI_IBS_WAKE_ACK, \
1188 .hlen = 0, \
1189 .loff = 0, \
1190 .lsize = 0, \
1191 .maxlen = HCI_MAX_IBS_SIZE
1192
1193static const struct h4_recv_pkt qca_recv_pkts[] = {
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +05301194 { H4_RECV_ACL, .recv = qca_recv_acl_data },
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001195 { H4_RECV_SCO, .recv = hci_recv_frame },
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001196 { H4_RECV_EVENT, .recv = qca_recv_event },
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001197 { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind },
1198 { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack },
1199 { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
1200};
1201
1202static int qca_recv(struct hci_uart *hu, const void *data, int count)
1203{
1204 struct qca_data *qca = hu->priv;
1205
1206 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
1207 return -EUNATCH;
1208
1209 qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
1210 qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
1211 if (IS_ERR(qca->rx_skb)) {
1212 int err = PTR_ERR(qca->rx_skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001213 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001214 qca->rx_skb = NULL;
1215 return err;
1216 }
1217
1218 return count;
1219}
1220
1221static struct sk_buff *qca_dequeue(struct hci_uart *hu)
1222{
1223 struct qca_data *qca = hu->priv;
1224
1225 return skb_dequeue(&qca->txq);
1226}
1227
1228static uint8_t qca_get_baudrate_value(int speed)
1229{
Prasanna Karthikce26d812015-09-15 12:19:45 +00001230 switch (speed) {
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001231 case 9600:
1232 return QCA_BAUDRATE_9600;
1233 case 19200:
1234 return QCA_BAUDRATE_19200;
1235 case 38400:
1236 return QCA_BAUDRATE_38400;
1237 case 57600:
1238 return QCA_BAUDRATE_57600;
1239 case 115200:
1240 return QCA_BAUDRATE_115200;
1241 case 230400:
1242 return QCA_BAUDRATE_230400;
1243 case 460800:
1244 return QCA_BAUDRATE_460800;
1245 case 500000:
1246 return QCA_BAUDRATE_500000;
1247 case 921600:
1248 return QCA_BAUDRATE_921600;
1249 case 1000000:
1250 return QCA_BAUDRATE_1000000;
1251 case 2000000:
1252 return QCA_BAUDRATE_2000000;
1253 case 3000000:
1254 return QCA_BAUDRATE_3000000;
Balakrishna Godavarthibe93a492018-08-03 17:46:30 +05301255 case 3200000:
1256 return QCA_BAUDRATE_3200000;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001257 case 3500000:
1258 return QCA_BAUDRATE_3500000;
1259 default:
1260 return QCA_BAUDRATE_115200;
1261 }
1262}
1263
1264static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
1265{
1266 struct hci_uart *hu = hci_get_drvdata(hdev);
1267 struct qca_data *qca = hu->priv;
1268 struct sk_buff *skb;
1269 u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
1270
Balakrishna Godavarthibe93a492018-08-03 17:46:30 +05301271 if (baudrate > QCA_BAUDRATE_3200000)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001272 return -EINVAL;
1273
1274 cmd[4] = baudrate;
1275
Jia-Ju Bai25a13e382018-07-23 11:56:51 +08001276 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001277 if (!skb) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001278 bt_dev_err(hdev, "Failed to allocate baudrate packet");
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001279 return -ENOMEM;
1280 }
1281
1282 /* Assign commands to change baudrate and packet type. */
Johannes Berg59ae1d12017-06-16 14:29:20 +02001283 skb_put_data(skb, cmd, sizeof(cmd));
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001284 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001285
1286 skb_queue_tail(&qca->txq, skb);
1287 hci_uart_tx_wakeup(hu);
1288
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001289 /* Wait for the baudrate change request to be sent */
1290
1291 while (!skb_queue_empty(&qca->txq))
1292 usleep_range(100, 200);
1293
Matthias Kaehlckeecf2b762019-04-23 11:16:52 -07001294 if (hu->serdev)
1295 serdev_device_wait_until_sent(hu->serdev,
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001296 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1297
1298 /* Give the controller time to process the request */
Harish Bandi523760b2019-04-26 19:26:01 +05301299 if (qca_is_wcn399x(qca_soc_type(hu)))
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001300 msleep(10);
1301 else
1302 msleep(300);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001303
1304 return 0;
1305}
1306
Thierry Escande05ba5332018-03-29 21:15:24 +02001307static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
1308{
1309 if (hu->serdev)
1310 serdev_device_set_baudrate(hu->serdev, speed);
1311 else
1312 hci_uart_set_baudrate(hu, speed);
1313}
1314
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001315static int qca_send_power_pulse(struct hci_uart *hu, bool on)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301316{
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301317 int ret;
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001318 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001319 u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301320
1321 /* These power pulses are single byte command which are sent
1322 * at required baudrate to wcn3990. On wcn3990, we have an external
1323 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1324 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1325 * and also we use the same power inputs to turn on and off for
1326 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1327 * we send a power on pulse at 115200 bps. This algorithm will help to
1328 * save power. Disabling hardware flow control is mandatory while
1329 * sending power pulses to SoC.
1330 */
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301331 bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301332
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301333 serdev_device_write_flush(hu->serdev);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301334 hci_uart_set_flow_control(hu, true);
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301335 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1336 if (ret < 0) {
1337 bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd);
1338 return ret;
1339 }
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301340
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301341 serdev_device_wait_until_sent(hu->serdev, timeout);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301342 hci_uart_set_flow_control(hu, false);
1343
Matthias Kaehlcke0ebcddd2019-02-26 11:46:47 -08001344 /* Give to controller time to boot/shutdown */
Matthias Kaehlckead571d72019-02-26 11:46:46 -08001345 if (on)
1346 msleep(100);
Matthias Kaehlcke0ebcddd2019-02-26 11:46:47 -08001347 else
1348 msleep(10);
Matthias Kaehlckead571d72019-02-26 11:46:46 -08001349
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301350 return 0;
1351}
1352
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301353static unsigned int qca_get_speed(struct hci_uart *hu,
1354 enum qca_speed_type speed_type)
1355{
1356 unsigned int speed = 0;
1357
1358 if (speed_type == QCA_INIT_SPEED) {
1359 if (hu->init_speed)
1360 speed = hu->init_speed;
1361 else if (hu->proto->init_speed)
1362 speed = hu->proto->init_speed;
1363 } else {
1364 if (hu->oper_speed)
1365 speed = hu->oper_speed;
1366 else if (hu->proto->oper_speed)
1367 speed = hu->proto->oper_speed;
1368 }
1369
1370 return speed;
1371}
1372
1373static int qca_check_speeds(struct hci_uart *hu)
1374{
Harish Bandi523760b2019-04-26 19:26:01 +05301375 if (qca_is_wcn399x(qca_soc_type(hu))) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301376 if (!qca_get_speed(hu, QCA_INIT_SPEED) &&
1377 !qca_get_speed(hu, QCA_OPER_SPEED))
1378 return -EINVAL;
1379 } else {
1380 if (!qca_get_speed(hu, QCA_INIT_SPEED) ||
1381 !qca_get_speed(hu, QCA_OPER_SPEED))
1382 return -EINVAL;
1383 }
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301384
1385 return 0;
1386}
1387
1388static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type)
1389{
1390 unsigned int speed, qca_baudrate;
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001391 struct qca_data *qca = hu->priv;
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301392 int ret = 0;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301393
1394 if (speed_type == QCA_INIT_SPEED) {
1395 speed = qca_get_speed(hu, QCA_INIT_SPEED);
1396 if (speed)
1397 host_set_baudrate(hu, speed);
1398 } else {
Matthias Kaehlcke4fdd5a42019-03-11 11:38:31 -07001399 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1400
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301401 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1402 if (!speed)
1403 return 0;
1404
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301405 /* Disable flow control for wcn3990 to deassert RTS while
1406 * changing the baudrate of chip and host.
1407 */
Harish Bandi523760b2019-04-26 19:26:01 +05301408 if (qca_is_wcn399x(soc_type))
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301409 hci_uart_set_flow_control(hu, true);
1410
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001411 if (soc_type == QCA_WCN3990) {
1412 reinit_completion(&qca->drop_ev_comp);
1413 set_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1414 }
1415
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301416 qca_baudrate = qca_get_baudrate_value(speed);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301417 bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed);
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301418 ret = qca_set_baudrate(hu->hdev, qca_baudrate);
1419 if (ret)
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301420 goto error;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301421
1422 host_set_baudrate(hu, speed);
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301423
1424error:
Jeffrey Hugobba79fe2019-10-21 07:18:27 -07001425 if (qca_is_wcn399x(soc_type))
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301426 hci_uart_set_flow_control(hu, false);
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001427
1428 if (soc_type == QCA_WCN3990) {
1429 /* Wait for the controller to send the vendor event
1430 * for the baudrate change command.
1431 */
1432 if (!wait_for_completion_timeout(&qca->drop_ev_comp,
1433 msecs_to_jiffies(100))) {
1434 bt_dev_err(hu->hdev,
1435 "Failed to change controller baudrate\n");
1436 ret = -ETIMEDOUT;
1437 }
1438
1439 clear_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1440 }
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301441 }
1442
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301443 return ret;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301444}
1445
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301446static int qca_send_crashbuffer(struct hci_uart *hu)
1447{
1448 struct qca_data *qca = hu->priv;
1449 struct sk_buff *skb;
1450
1451 skb = bt_skb_alloc(QCA_CRASHBYTE_PACKET_LEN, GFP_KERNEL);
1452 if (!skb) {
1453 bt_dev_err(hu->hdev, "Failed to allocate memory for skb packet");
1454 return -ENOMEM;
1455 }
1456
1457 /* We forcefully crash the controller, by sending 0xfb byte for
1458 * 1024 times. We also might have chance of losing data, To be
1459 * on safer side we send 1096 bytes to the SoC.
1460 */
1461 memset(skb_put(skb, QCA_CRASHBYTE_PACKET_LEN), QCA_MEMDUMP_BYTE,
1462 QCA_CRASHBYTE_PACKET_LEN);
1463 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1464 bt_dev_info(hu->hdev, "crash the soc to collect controller dump");
1465 skb_queue_tail(&qca->txq, skb);
1466 hci_uart_tx_wakeup(hu);
1467
1468 return 0;
1469}
1470
1471static void qca_wait_for_dump_collection(struct hci_dev *hdev)
1472{
1473 struct hci_uart *hu = hci_get_drvdata(hdev);
1474 struct qca_data *qca = hu->priv;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301475
1476 wait_on_bit_timeout(&qca->flags, QCA_MEMDUMP_COLLECTION,
1477 TASK_UNINTERRUPTIBLE, MEMDUMP_TIMEOUT_MS);
1478
1479 clear_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301480}
1481
1482static void qca_hw_error(struct hci_dev *hdev, u8 code)
1483{
1484 struct hci_uart *hu = hci_get_drvdata(hdev);
1485 struct qca_data *qca = hu->priv;
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301486 struct qca_memdump_data *qca_memdump = qca->qca_memdump;
1487 char *memdump_buf = NULL;
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301488
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301489 set_bit(QCA_HW_ERROR_EVENT, &qca->flags);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301490 bt_dev_info(hdev, "mem_dump_status: %d", qca->memdump_state);
1491
1492 if (qca->memdump_state == QCA_MEMDUMP_IDLE) {
1493 /* If hardware error event received for other than QCA
1494 * soc memory dump event, then we need to crash the SOC
1495 * and wait here for 8 seconds to get the dump packets.
1496 * This will block main thread to be on hold until we
1497 * collect dump.
1498 */
1499 set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1500 qca_send_crashbuffer(hu);
1501 qca_wait_for_dump_collection(hdev);
1502 } else if (qca->memdump_state == QCA_MEMDUMP_COLLECTING) {
1503 /* Let us wait here until memory dump collected or
1504 * memory dump timer expired.
1505 */
1506 bt_dev_info(hdev, "waiting for dump to complete");
1507 qca_wait_for_dump_collection(hdev);
1508 }
Venkata Lakshmi Narayana Gubba7c2c3e62020-02-14 21:47:15 +05301509
1510 if (qca->memdump_state != QCA_MEMDUMP_COLLECTED) {
1511 bt_dev_err(hu->hdev, "clearing allocated memory due to memdump timeout");
1512 mutex_lock(&qca->hci_memdump_lock);
1513 if (qca_memdump)
1514 memdump_buf = qca_memdump->memdump_buf_head;
1515 vfree(memdump_buf);
1516 kfree(qca_memdump);
1517 qca->qca_memdump = NULL;
1518 qca->memdump_state = QCA_MEMDUMP_TIMEOUT;
1519 cancel_delayed_work(&qca->ctrl_memdump_timeout);
1520 skb_queue_purge(&qca->rx_memdump_q);
1521 mutex_unlock(&qca->hci_memdump_lock);
1522 cancel_work_sync(&qca->ctrl_memdump_evt);
1523 }
1524
1525 clear_bit(QCA_HW_ERROR_EVENT, &qca->flags);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301526}
1527
1528static void qca_cmd_timeout(struct hci_dev *hdev)
1529{
1530 struct hci_uart *hu = hci_get_drvdata(hdev);
1531 struct qca_data *qca = hu->priv;
1532
1533 if (qca->memdump_state == QCA_MEMDUMP_IDLE)
1534 qca_send_crashbuffer(hu);
1535 else
1536 bt_dev_info(hdev, "Dump collection is in process");
1537}
1538
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301539static int qca_wcn3990_init(struct hci_uart *hu)
1540{
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301541 struct qca_serdev *qcadev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301542 int ret;
1543
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301544 /* Check for vregs status, may be hci down has turned
1545 * off the voltage regulator.
1546 */
1547 qcadev = serdev_device_get_drvdata(hu->serdev);
1548 if (!qcadev->bt_power->vregs_on) {
1549 serdev_device_close(hu->serdev);
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001550 ret = qca_regulator_enable(qcadev);
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301551 if (ret)
1552 return ret;
1553
1554 ret = serdev_device_open(hu->serdev);
1555 if (ret) {
1556 bt_dev_err(hu->hdev, "failed to open port");
1557 return ret;
1558 }
1559 }
1560
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301561 /* Forcefully enable wcn3990 to enter in to boot mode. */
1562 host_set_baudrate(hu, 2400);
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001563 ret = qca_send_power_pulse(hu, false);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301564 if (ret)
1565 return ret;
1566
1567 qca_set_speed(hu, QCA_INIT_SPEED);
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001568 ret = qca_send_power_pulse(hu, true);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301569 if (ret)
1570 return ret;
1571
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301572 /* Now the device is in ready state to communicate with host.
1573 * To sync host with device we need to reopen port.
1574 * Without this, we will have RTS and CTS synchronization
1575 * issues.
1576 */
1577 serdev_device_close(hu->serdev);
1578 ret = serdev_device_open(hu->serdev);
1579 if (ret) {
1580 bt_dev_err(hu->hdev, "failed to open port");
1581 return ret;
1582 }
1583
1584 hci_uart_set_flow_control(hu, false);
1585
1586 return 0;
1587}
1588
Rocky Liao5e6d8402020-01-13 12:30:20 +08001589static int qca_power_on(struct hci_dev *hdev)
1590{
1591 struct hci_uart *hu = hci_get_drvdata(hdev);
1592 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1593 struct qca_serdev *qcadev;
1594 int ret = 0;
1595
1596 /* Non-serdev device usually is powered by external power
1597 * and don't need additional action in driver for power on
1598 */
1599 if (!hu->serdev)
1600 return 0;
1601
1602 if (qca_is_wcn399x(soc_type)) {
1603 ret = qca_wcn3990_init(hu);
1604 } else {
1605 qcadev = serdev_device_get_drvdata(hu->serdev);
Rocky Liao77131df2020-03-04 21:16:45 +08001606 if (qcadev->bt_en) {
Rocky Liao8a208b22020-03-04 09:54:29 +08001607 gpiod_set_value_cansleep(qcadev->bt_en, 1);
1608 /* Controller needs time to bootup. */
1609 msleep(150);
1610 }
Rocky Liao5e6d8402020-01-13 12:30:20 +08001611 }
1612
1613 return ret;
1614}
1615
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001616static int qca_setup(struct hci_uart *hu)
1617{
1618 struct hci_dev *hdev = hu->hdev;
1619 struct qca_data *qca = hu->priv;
1620 unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
Rocky Liaobb2500ab2020-01-15 16:55:51 +08001621 unsigned int retries = 0;
Matthias Kaehlcke4fdd5a42019-03-11 11:38:31 -07001622 enum qca_btsoc_type soc_type = qca_soc_type(hu);
Rocky Liao99c905c2019-06-06 17:40:30 +08001623 const char *firmware_name = qca_get_firmware_name(hu);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001624 int ret;
Balakrishna Godavarthiaadebac2018-08-03 17:46:28 +05301625 int soc_ver = 0;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001626
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301627 ret = qca_check_speeds(hu);
1628 if (ret)
1629 return ret;
1630
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001631 /* Patch downloading has to be done without IBS mode */
Matthias Kaehlcke62a91992019-04-29 16:21:30 -07001632 clear_bit(QCA_IBS_ENABLED, &qca->flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001633
Rocky Liaoe14c1672019-08-21 14:23:39 +08001634 /* Enable controller to do both LE scan and BR/EDR inquiry
1635 * simultaneously.
1636 */
1637 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1638
Rocky Liao5e6d8402020-01-13 12:30:20 +08001639 bt_dev_info(hdev, "setting up %s",
Rocky Liaoe5d64682020-03-25 10:26:37 +08001640 qca_is_wcn399x(soc_type) ? "wcn399x" : "ROME/QCA6390");
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301641
Rocky Liaobb2500ab2020-01-15 16:55:51 +08001642retry:
Rocky Liao5e6d8402020-01-13 12:30:20 +08001643 ret = qca_power_on(hdev);
1644 if (ret)
1645 return ret;
1646
1647 if (qca_is_wcn399x(soc_type)) {
Matthias Kaehlcke5971752d2019-02-19 12:05:59 -08001648 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301649
Balakrishna Godavarthi7d250a02019-11-06 15:18:32 +05301650 ret = qca_read_soc_version(hdev, &soc_ver, soc_type);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301651 if (ret)
1652 return ret;
1653 } else {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301654 qca_set_speed(hu, QCA_INIT_SPEED);
1655 }
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001656
1657 /* Setup user speed if needed */
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301658 speed = qca_get_speed(hu, QCA_OPER_SPEED);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001659 if (speed) {
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301660 ret = qca_set_speed(hu, QCA_OPER_SPEED);
1661 if (ret)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001662 return ret;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301663
1664 qca_baudrate = qca_get_baudrate_value(speed);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001665 }
1666
Harish Bandi523760b2019-04-26 19:26:01 +05301667 if (!qca_is_wcn399x(soc_type)) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301668 /* Get QCA version information */
Balakrishna Godavarthi7d250a02019-11-06 15:18:32 +05301669 ret = qca_read_soc_version(hdev, &soc_ver, soc_type);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301670 if (ret)
1671 return ret;
1672 }
Balakrishna Godavarthiaadebac2018-08-03 17:46:28 +05301673
1674 bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001675 /* Setup patch / NVM configurations */
Rocky Liao99c905c2019-06-06 17:40:30 +08001676 ret = qca_uart_setup(hdev, qca_baudrate, soc_type, soc_ver,
1677 firmware_name);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001678 if (!ret) {
Matthias Kaehlcke62a91992019-04-29 16:21:30 -07001679 set_bit(QCA_IBS_ENABLED, &qca->flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001680 qca_debugfs_init(hdev);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301681 hu->hdev->hw_error = qca_hw_error;
1682 hu->hdev->cmd_timeout = qca_cmd_timeout;
Loic Poulainba8f3592017-11-06 12:16:56 +01001683 } else if (ret == -ENOENT) {
1684 /* No patch/nvm-config found, run with original fw/config */
1685 ret = 0;
Amit Pundir7dc5fe02018-04-16 12:10:24 +05301686 } else if (ret == -EAGAIN) {
1687 /*
1688 * Userspace firmware loader will return -EAGAIN in case no
1689 * patch/nvm-config is found, so run with original fw/config.
1690 */
1691 ret = 0;
Rocky Liaobb2500ab2020-01-15 16:55:51 +08001692 } else {
1693 if (retries < MAX_INIT_RETRIES) {
1694 qca_power_shutdown(hu);
1695 if (hu->serdev) {
1696 serdev_device_close(hu->serdev);
1697 ret = serdev_device_open(hu->serdev);
1698 if (ret) {
1699 bt_dev_err(hdev, "failed to open port");
1700 return ret;
1701 }
1702 }
1703 retries++;
1704 goto retry;
1705 }
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001706 }
1707
1708 /* Setup bdaddr */
Rocky Liaoe5d64682020-03-25 10:26:37 +08001709 if (soc_type == QCA_ROME)
Balakrishna Godavarthi5c0a10012019-01-16 18:01:15 +05301710 hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
Rocky Liaoe5d64682020-03-25 10:26:37 +08001711 else
1712 hu->hdev->set_bdaddr = qca_set_bdaddr;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001713
1714 return ret;
1715}
1716
Nishka Dasgupta2edc9c52019-08-15 11:21:49 +05301717static const struct hci_uart_proto qca_proto = {
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001718 .id = HCI_UART_QCA,
1719 .name = "QCA",
Marcel Holtmannaee61f72015-10-20 21:30:45 +02001720 .manufacturer = 29,
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001721 .init_speed = 115200,
1722 .oper_speed = 3000000,
1723 .open = qca_open,
1724 .close = qca_close,
1725 .flush = qca_flush,
1726 .setup = qca_setup,
1727 .recv = qca_recv,
1728 .enqueue = qca_enqueue,
1729 .dequeue = qca_dequeue,
1730};
1731
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -07001732static const struct qca_device_data qca_soc_data_wcn3990 = {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301733 .soc_type = QCA_WCN3990,
1734 .vregs = (struct qca_vreg []) {
Bjorn Anderssonf2edd662019-10-17 22:24:02 -07001735 { "vddio", 15000 },
1736 { "vddxo", 80000 },
1737 { "vddrf", 300000 },
1738 { "vddch0", 450000 },
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301739 },
1740 .num_vregs = 4,
1741};
1742
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -07001743static const struct qca_device_data qca_soc_data_wcn3991 = {
Balakrishna Godavarthi7d250a02019-11-06 15:18:32 +05301744 .soc_type = QCA_WCN3991,
1745 .vregs = (struct qca_vreg []) {
1746 { "vddio", 15000 },
1747 { "vddxo", 80000 },
1748 { "vddrf", 300000 },
1749 { "vddch0", 450000 },
1750 },
1751 .num_vregs = 4,
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -07001752 .capabilities = QCA_CAP_WIDEBAND_SPEECH,
Balakrishna Godavarthi7d250a02019-11-06 15:18:32 +05301753};
1754
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -07001755static const struct qca_device_data qca_soc_data_wcn3998 = {
Harish Bandi523760b2019-04-26 19:26:01 +05301756 .soc_type = QCA_WCN3998,
1757 .vregs = (struct qca_vreg []) {
Bjorn Anderssonf2edd662019-10-17 22:24:02 -07001758 { "vddio", 10000 },
1759 { "vddxo", 80000 },
1760 { "vddrf", 300000 },
1761 { "vddch0", 450000 },
Harish Bandi523760b2019-04-26 19:26:01 +05301762 },
1763 .num_vregs = 4,
1764};
1765
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -07001766static const struct qca_device_data qca_soc_data_qca6390 = {
Rocky Liaoe5d64682020-03-25 10:26:37 +08001767 .soc_type = QCA_QCA6390,
1768 .num_vregs = 0,
1769};
1770
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +05301771static void qca_power_shutdown(struct hci_uart *hu)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301772{
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001773 struct qca_serdev *qcadev;
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +05301774 struct qca_data *qca = hu->priv;
1775 unsigned long flags;
Rocky Liao55599042020-01-15 16:55:50 +08001776 enum qca_btsoc_type soc_type = qca_soc_type(hu);
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +05301777
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001778 qcadev = serdev_device_get_drvdata(hu->serdev);
1779
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +05301780 /* From this point we go into power off state. But serial port is
1781 * still open, stop queueing the IBS data and flush all the buffered
1782 * data in skb's.
1783 */
1784 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
Matthias Kaehlcke62a91992019-04-29 16:21:30 -07001785 clear_bit(QCA_IBS_ENABLED, &qca->flags);
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +05301786 qca_flush(hu);
1787 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1788
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301789 hu->hdev->hw_error = NULL;
1790 hu->hdev->cmd_timeout = NULL;
Rocky Liao55599042020-01-15 16:55:50 +08001791
1792 /* Non-serdev device usually is powered by external power
1793 * and don't need additional action in driver for power down
1794 */
1795 if (!hu->serdev)
1796 return;
1797
1798 if (qca_is_wcn399x(soc_type)) {
1799 host_set_baudrate(hu, 2400);
1800 qca_send_power_pulse(hu, false);
1801 qca_regulator_disable(qcadev);
Rocky Liao77131df2020-03-04 21:16:45 +08001802 } else if (qcadev->bt_en) {
Rocky Liao55599042020-01-15 16:55:50 +08001803 gpiod_set_value_cansleep(qcadev->bt_en, 0);
1804 }
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301805}
1806
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301807static int qca_power_off(struct hci_dev *hdev)
1808{
1809 struct hci_uart *hu = hci_get_drvdata(hdev);
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301810 struct qca_data *qca = hu->priv;
Rocky Liao4f9ed5b2020-02-29 20:21:18 +08001811 enum qca_btsoc_type soc_type = qca_soc_type(hu);
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301812
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301813 /* Stop sending shutdown command if soc crashes. */
Rocky Liaoe5d64682020-03-25 10:26:37 +08001814 if (soc_type != QCA_ROME
Rocky Liao4f9ed5b2020-02-29 20:21:18 +08001815 && qca->memdump_state == QCA_MEMDUMP_IDLE) {
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301816 qca_send_pre_shutdown_cmd(hdev);
1817 usleep_range(8000, 10000);
1818 }
Harish Bandia2780882019-07-12 10:39:40 +05301819
Balakrishna Godavarthid8415022020-01-02 20:19:11 +05301820 qca->memdump_state = QCA_MEMDUMP_IDLE;
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301821 qca_power_shutdown(hu);
1822 return 0;
1823}
1824
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001825static int qca_regulator_enable(struct qca_serdev *qcadev)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301826{
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001827 struct qca_power *power = qcadev->bt_power;
1828 int ret;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301829
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001830 /* Already enabled */
1831 if (power->vregs_on)
1832 return 0;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301833
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001834 BT_DBG("enabling %d regulators)", power->num_vregs);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301835
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001836 ret = regulator_bulk_enable(power->num_vregs, power->vreg_bulk);
1837 if (ret)
1838 return ret;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301839
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001840 power->vregs_on = true;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301841
Venkata Lakshmi Narayana Gubba66cb7052020-02-03 16:10:40 +05301842 ret = clk_prepare_enable(qcadev->susclk);
Venkata Lakshmi Narayana Gubbaf3d63f52020-02-05 16:21:43 +05301843 if (ret)
Venkata Lakshmi Narayana Gubba66cb7052020-02-03 16:10:40 +05301844 qca_regulator_disable(qcadev);
Venkata Lakshmi Narayana Gubba66cb7052020-02-03 16:10:40 +05301845
Venkata Lakshmi Narayana Gubbaf3d63f52020-02-05 16:21:43 +05301846 return ret;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301847}
1848
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001849static void qca_regulator_disable(struct qca_serdev *qcadev)
1850{
1851 struct qca_power *power;
1852
1853 if (!qcadev)
1854 return;
1855
1856 power = qcadev->bt_power;
1857
1858 /* Already disabled? */
1859 if (!power->vregs_on)
1860 return;
1861
1862 regulator_bulk_disable(power->num_vregs, power->vreg_bulk);
1863 power->vregs_on = false;
Venkata Lakshmi Narayana Gubba66cb7052020-02-03 16:10:40 +05301864
Venkata Lakshmi Narayana Gubbaf3d63f52020-02-05 16:21:43 +05301865 clk_disable_unprepare(qcadev->susclk);
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001866}
1867
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301868static int qca_init_regulators(struct qca_power *qca,
1869 const struct qca_vreg *vregs, size_t num_vregs)
1870{
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001871 struct regulator_bulk_data *bulk;
1872 int ret;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301873 int i;
1874
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001875 bulk = devm_kcalloc(qca->dev, num_vregs, sizeof(*bulk), GFP_KERNEL);
1876 if (!bulk)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301877 return -ENOMEM;
1878
1879 for (i = 0; i < num_vregs; i++)
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001880 bulk[i].supply = vregs[i].name;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301881
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001882 ret = devm_regulator_bulk_get(qca->dev, num_vregs, bulk);
1883 if (ret < 0)
1884 return ret;
1885
1886 for (i = 0; i < num_vregs; i++) {
1887 ret = regulator_set_load(bulk[i].consumer, vregs[i].load_uA);
1888 if (ret)
1889 return ret;
1890 }
1891
1892 qca->vreg_bulk = bulk;
Bjorn Andersson163d42f2019-10-17 22:24:03 -07001893 qca->num_vregs = num_vregs;
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001894
1895 return 0;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301896}
1897
Thierry Escande05ba5332018-03-29 21:15:24 +02001898static int qca_serdev_probe(struct serdev_device *serdev)
1899{
1900 struct qca_serdev *qcadev;
Rocky Liaoae563182020-01-16 11:22:54 +08001901 struct hci_dev *hdev;
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -07001902 const struct qca_device_data *data;
Thierry Escande05ba5332018-03-29 21:15:24 +02001903 int err;
Rocky Liao8a208b22020-03-04 09:54:29 +08001904 bool power_ctrl_enabled = true;
Thierry Escande05ba5332018-03-29 21:15:24 +02001905
1906 qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
1907 if (!qcadev)
1908 return -ENOMEM;
1909
1910 qcadev->serdev_hu.serdev = serdev;
Rocky Liao9f3565b2019-12-13 16:50:45 +08001911 data = device_get_match_data(&serdev->dev);
Thierry Escande05ba5332018-03-29 21:15:24 +02001912 serdev_device_set_drvdata(serdev, qcadev);
Rocky Liao99c905c2019-06-06 17:40:30 +08001913 device_property_read_string(&serdev->dev, "firmware-name",
1914 &qcadev->firmware_name);
Christian Hewitt37aee132020-04-23 01:34:30 +00001915 device_property_read_u32(&serdev->dev, "max-speed",
1916 &qcadev->oper_speed);
1917 if (!qcadev->oper_speed)
1918 BT_DBG("UART will pick default operating speed");
1919
Harish Bandi523760b2019-04-26 19:26:01 +05301920 if (data && qca_is_wcn399x(data->soc_type)) {
1921 qcadev->btsoc_type = data->soc_type;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301922 qcadev->bt_power = devm_kzalloc(&serdev->dev,
1923 sizeof(struct qca_power),
1924 GFP_KERNEL);
1925 if (!qcadev->bt_power)
1926 return -ENOMEM;
Thierry Escande05ba5332018-03-29 21:15:24 +02001927
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301928 qcadev->bt_power->dev = &serdev->dev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301929 err = qca_init_regulators(qcadev->bt_power, data->vregs,
1930 data->num_vregs);
1931 if (err) {
1932 BT_ERR("Failed to init regulators:%d", err);
Rocky Liaoae563182020-01-16 11:22:54 +08001933 return err;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301934 }
1935
1936 qcadev->bt_power->vregs_on = false;
1937
Venkata Lakshmi Narayana Gubba66cb7052020-02-03 16:10:40 +05301938 qcadev->susclk = devm_clk_get_optional(&serdev->dev, NULL);
1939 if (IS_ERR(qcadev->susclk)) {
1940 dev_err(&serdev->dev, "failed to acquire clk\n");
1941 return PTR_ERR(qcadev->susclk);
1942 }
1943
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301944 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1945 if (err) {
1946 BT_ERR("wcn3990 serdev registration failed");
Rocky Liaoae563182020-01-16 11:22:54 +08001947 return err;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301948 }
1949 } else {
Rocky Liaoe5d64682020-03-25 10:26:37 +08001950 if (data)
1951 qcadev->btsoc_type = data->soc_type;
1952 else
1953 qcadev->btsoc_type = QCA_ROME;
1954
Rocky Liao77131df2020-03-04 21:16:45 +08001955 qcadev->bt_en = devm_gpiod_get_optional(&serdev->dev, "enable",
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301956 GPIOD_OUT_LOW);
Rocky Liao77131df2020-03-04 21:16:45 +08001957 if (!qcadev->bt_en) {
Rocky Liao8a208b22020-03-04 09:54:29 +08001958 dev_warn(&serdev->dev, "failed to acquire enable gpio\n");
1959 power_ctrl_enabled = false;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301960 }
1961
Rocky Liao77131df2020-03-04 21:16:45 +08001962 qcadev->susclk = devm_clk_get_optional(&serdev->dev, NULL);
Dan Carpenter4c07a5d2020-05-29 12:59:48 +03001963 if (IS_ERR(qcadev->susclk)) {
Rocky Liao8a208b22020-03-04 09:54:29 +08001964 dev_warn(&serdev->dev, "failed to acquire clk\n");
Dan Carpenter4c07a5d2020-05-29 12:59:48 +03001965 return PTR_ERR(qcadev->susclk);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301966 }
Dan Carpenter4c07a5d2020-05-29 12:59:48 +03001967 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
1968 if (err)
1969 return err;
1970
1971 err = clk_prepare_enable(qcadev->susclk);
1972 if (err)
1973 return err;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301974
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301975 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
Rocky Liaoae563182020-01-16 11:22:54 +08001976 if (err) {
1977 BT_ERR("Rome serdev registration failed");
Rocky Liao77131df2020-03-04 21:16:45 +08001978 if (qcadev->susclk)
Rocky Liao8a208b22020-03-04 09:54:29 +08001979 clk_disable_unprepare(qcadev->susclk);
Rocky Liaoae563182020-01-16 11:22:54 +08001980 return err;
1981 }
Thierry Escande05ba5332018-03-29 21:15:24 +02001982 }
1983
Abhishek Pandit-Subedi85e90d92020-05-20 09:32:28 -07001984 hdev = qcadev->serdev_hu.hdev;
1985
Rocky Liao8a208b22020-03-04 09:54:29 +08001986 if (power_ctrl_enabled) {
Rocky Liao8a208b22020-03-04 09:54:29 +08001987 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
1988 hdev->shutdown = qca_power_off;
1989 }
Thierry Escande05ba5332018-03-29 21:15:24 +02001990
Abhishek Pandit-Subedia228f7a2020-05-14 13:14:04 -07001991 /* Wideband speech support must be set per driver since it can't be
1992 * queried via hci.
1993 */
1994 if (data && (data->capabilities & QCA_CAP_WIDEBAND_SPEECH))
1995 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
1996
Rocky Liaoae563182020-01-16 11:22:54 +08001997 return 0;
Thierry Escande05ba5332018-03-29 21:15:24 +02001998}
1999
2000static void qca_serdev_remove(struct serdev_device *serdev)
2001{
2002 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
2003
Harish Bandi523760b2019-04-26 19:26:01 +05302004 if (qca_is_wcn399x(qcadev->btsoc_type))
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +05302005 qca_power_shutdown(&qcadev->serdev_hu);
Rocky Liao77131df2020-03-04 21:16:45 +08002006 else if (qcadev->susclk)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05302007 clk_disable_unprepare(qcadev->susclk);
Thierry Escande05ba5332018-03-29 21:15:24 +02002008
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05302009 hci_uart_unregister_device(&qcadev->serdev_hu);
Thierry Escande05ba5332018-03-29 21:15:24 +02002010}
2011
Zijun Hu7e7bbdd2020-05-29 21:56:57 +08002012static void qca_serdev_shutdown(struct device *dev)
2013{
2014 int ret;
2015 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
2016 struct serdev_device *serdev = to_serdev_device(dev);
2017 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
2018 const u8 ibs_wake_cmd[] = { 0xFD };
2019 const u8 edl_reset_soc_cmd[] = { 0x01, 0x00, 0xFC, 0x01, 0x05 };
2020
2021 if (qcadev->btsoc_type == QCA_QCA6390) {
2022 serdev_device_write_flush(serdev);
2023 ret = serdev_device_write_buf(serdev, ibs_wake_cmd,
2024 sizeof(ibs_wake_cmd));
2025 if (ret < 0) {
2026 BT_ERR("QCA send IBS_WAKE_IND error: %d", ret);
2027 return;
2028 }
2029 serdev_device_wait_until_sent(serdev, timeout);
2030 usleep_range(8000, 10000);
2031
2032 serdev_device_write_flush(serdev);
2033 ret = serdev_device_write_buf(serdev, edl_reset_soc_cmd,
2034 sizeof(edl_reset_soc_cmd));
2035 if (ret < 0) {
2036 BT_ERR("QCA send EDL_RESET_REQ error: %d", ret);
2037 return;
2038 }
2039 serdev_device_wait_until_sent(serdev, timeout);
2040 usleep_range(8000, 10000);
2041 }
2042}
2043
Claire Chang41d5b252019-10-31 18:46:14 +08002044static int __maybe_unused qca_suspend(struct device *dev)
2045{
Zijun Hufeac90d2020-05-29 04:31:07 +08002046 struct serdev_device *serdev = to_serdev_device(dev);
2047 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
2048 struct hci_uart *hu = &qcadev->serdev_hu;
Claire Chang41d5b252019-10-31 18:46:14 +08002049 struct qca_data *qca = hu->priv;
2050 unsigned long flags;
2051 int ret = 0;
2052 u8 cmd;
2053
2054 set_bit(QCA_SUSPENDING, &qca->flags);
2055
2056 /* Device is downloading patch or doesn't support in-band sleep. */
2057 if (!test_bit(QCA_IBS_ENABLED, &qca->flags))
2058 return 0;
2059
2060 cancel_work_sync(&qca->ws_awake_device);
2061 cancel_work_sync(&qca->ws_awake_rx);
2062
2063 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
2064 flags, SINGLE_DEPTH_NESTING);
2065
2066 switch (qca->tx_ibs_state) {
2067 case HCI_IBS_TX_WAKING:
2068 del_timer(&qca->wake_retrans_timer);
2069 /* Fall through */
2070 case HCI_IBS_TX_AWAKE:
2071 del_timer(&qca->tx_idle_timer);
2072
2073 serdev_device_write_flush(hu->serdev);
2074 cmd = HCI_IBS_SLEEP_IND;
2075 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
2076
2077 if (ret < 0) {
2078 BT_ERR("Failed to send SLEEP to device");
2079 break;
2080 }
2081
2082 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
2083 qca->ibs_sent_slps++;
Claire Chang41d5b252019-10-31 18:46:14 +08002084 break;
2085
2086 case HCI_IBS_TX_ASLEEP:
2087 break;
2088
2089 default:
2090 BT_ERR("Spurious tx state %d", qca->tx_ibs_state);
2091 ret = -EINVAL;
2092 break;
2093 }
2094
2095 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
2096
2097 if (ret < 0)
2098 goto error;
2099
2100 serdev_device_wait_until_sent(hu->serdev,
2101 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
2102
2103 /* Wait for HCI_IBS_SLEEP_IND sent by device to indicate its Tx is going
2104 * to sleep, so that the packet does not wake the system later.
2105 */
2106
2107 ret = wait_event_interruptible_timeout(qca->suspend_wait_q,
2108 qca->rx_ibs_state == HCI_IBS_RX_ASLEEP,
2109 msecs_to_jiffies(IBS_BTSOC_TX_IDLE_TIMEOUT_MS));
2110
Matthias Kaehlckeeff981f2020-06-05 11:46:09 -07002111 if (ret > 0) {
2112 qca_wq_serial_tx_clock_vote_off(&qca->ws_tx_vote_off);
Claire Chang41d5b252019-10-31 18:46:14 +08002113 return 0;
Matthias Kaehlckeeff981f2020-06-05 11:46:09 -07002114 }
Claire Chang41d5b252019-10-31 18:46:14 +08002115
2116 if (ret == 0)
2117 ret = -ETIMEDOUT;
2118
2119error:
2120 clear_bit(QCA_SUSPENDING, &qca->flags);
2121
2122 return ret;
2123}
2124
2125static int __maybe_unused qca_resume(struct device *dev)
2126{
Zijun Hufeac90d2020-05-29 04:31:07 +08002127 struct serdev_device *serdev = to_serdev_device(dev);
2128 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
2129 struct hci_uart *hu = &qcadev->serdev_hu;
Claire Chang41d5b252019-10-31 18:46:14 +08002130 struct qca_data *qca = hu->priv;
2131
2132 clear_bit(QCA_SUSPENDING, &qca->flags);
2133
2134 return 0;
2135}
2136
2137static SIMPLE_DEV_PM_OPS(qca_pm_ops, qca_suspend, qca_resume);
2138
Rocky Liaoe5d64682020-03-25 10:26:37 +08002139#ifdef CONFIG_OF
Thierry Escande05ba5332018-03-29 21:15:24 +02002140static const struct of_device_id qca_bluetooth_of_match[] = {
2141 { .compatible = "qcom,qca6174-bt" },
Rocky Liaoe5d64682020-03-25 10:26:37 +08002142 { .compatible = "qcom,qca6390-bt", .data = &qca_soc_data_qca6390},
Christian Hewitt31d4ab82020-04-23 01:34:29 +00002143 { .compatible = "qcom,qca9377-bt" },
Harish Bandi523760b2019-04-26 19:26:01 +05302144 { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990},
Balakrishna Godavarthi7d250a02019-11-06 15:18:32 +05302145 { .compatible = "qcom,wcn3991-bt", .data = &qca_soc_data_wcn3991},
Harish Bandi523760b2019-04-26 19:26:01 +05302146 { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998},
Thierry Escande05ba5332018-03-29 21:15:24 +02002147 { /* sentinel */ }
2148};
2149MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
Rocky Liaoe5d64682020-03-25 10:26:37 +08002150#endif
2151
2152#ifdef CONFIG_ACPI
2153static const struct acpi_device_id qca_bluetooth_acpi_match[] = {
2154 { "QCOM6390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2155 { "DLA16390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2156 { "DLB16390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2157 { "DLB26390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2158 { },
2159};
2160MODULE_DEVICE_TABLE(acpi, qca_bluetooth_acpi_match);
2161#endif
2162
Thierry Escande05ba5332018-03-29 21:15:24 +02002163
2164static struct serdev_device_driver qca_serdev_driver = {
2165 .probe = qca_serdev_probe,
2166 .remove = qca_serdev_remove,
2167 .driver = {
2168 .name = "hci_uart_qca",
Rocky Liaoe5d64682020-03-25 10:26:37 +08002169 .of_match_table = of_match_ptr(qca_bluetooth_of_match),
2170 .acpi_match_table = ACPI_PTR(qca_bluetooth_acpi_match),
Zijun Hu7e7bbdd2020-05-29 21:56:57 +08002171 .shutdown = qca_serdev_shutdown,
Claire Chang41d5b252019-10-31 18:46:14 +08002172 .pm = &qca_pm_ops,
Thierry Escande05ba5332018-03-29 21:15:24 +02002173 },
2174};
2175
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07002176int __init qca_init(void)
2177{
Thierry Escande05ba5332018-03-29 21:15:24 +02002178 serdev_device_driver_register(&qca_serdev_driver);
2179
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07002180 return hci_uart_register_proto(&qca_proto);
2181}
2182
2183int __exit qca_deinit(void)
2184{
Thierry Escande05ba5332018-03-29 21:15:24 +02002185 serdev_device_driver_unregister(&qca_serdev_driver);
2186
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07002187 return hci_uart_unregister_proto(&qca_proto);
2188}