1 // SPDX-License-Identifier: GPL-2.0-only
3 * Bluetooth supports for Qualcomm Atheros chips
5 * Copyright (c) 2015 The Linux Foundation. All rights reserved.
7 #include <linux/module.h>
8 #include <linux/firmware.h>
10 #include <net/bluetooth/bluetooth.h>
11 #include <net/bluetooth/hci_core.h>
17 int qca_read_soc_version(struct hci_dev
*hdev
, struct qca_btsoc_version
*ver
,
18 enum qca_btsoc_type soc_type
)
21 struct edl_event_hdr
*edl
;
24 u8 event_type
= HCI_EV_VENDOR
;
25 u8 rlen
= sizeof(*edl
) + sizeof(*ver
);
26 u8 rtype
= EDL_APP_VER_RES_EVT
;
28 bt_dev_dbg(hdev
, "QCA Version Request");
30 /* Unlike other SoC's sending version command response as payload to
31 * VSE event. WCN3991 sends version command response as a payload to
32 * command complete event.
34 if (soc_type
>= QCA_WCN3991
) {
37 rtype
= EDL_PATCH_VER_REQ_CMD
;
40 cmd
= EDL_PATCH_VER_REQ_CMD
;
41 skb
= __hci_cmd_sync_ev(hdev
, EDL_PATCH_CMD_OPCODE
, EDL_PATCH_CMD_LEN
,
42 &cmd
, event_type
, HCI_INIT_TIMEOUT
);
45 bt_dev_err(hdev
, "Reading QCA version information failed (%d)",
50 if (skb
->len
!= rlen
) {
51 bt_dev_err(hdev
, "QCA Version size mismatch len %d", skb
->len
);
56 edl
= (struct edl_event_hdr
*)(skb
->data
);
58 bt_dev_err(hdev
, "QCA TLV with no header");
63 if (edl
->cresp
!= EDL_CMD_REQ_RES_EVT
||
64 edl
->rtype
!= rtype
) {
65 bt_dev_err(hdev
, "QCA Wrong packet received %d %d", edl
->cresp
,
71 if (soc_type
>= QCA_WCN3991
)
72 memcpy(ver
, edl
->data
+ 1, sizeof(*ver
));
74 memcpy(ver
, &edl
->data
, sizeof(*ver
));
76 bt_dev_info(hdev
, "QCA Product ID :0x%08x",
77 le32_to_cpu(ver
->product_id
));
78 bt_dev_info(hdev
, "QCA SOC Version :0x%08x",
79 le32_to_cpu(ver
->soc_id
));
80 bt_dev_info(hdev
, "QCA ROM Version :0x%08x",
81 le16_to_cpu(ver
->rom_ver
));
82 bt_dev_info(hdev
, "QCA Patch Version:0x%08x",
83 le16_to_cpu(ver
->patch_ver
));
85 if (ver
->soc_id
== 0 || ver
->rom_ver
== 0)
91 bt_dev_err(hdev
, "QCA Failed to get version (%d)", err
);
95 EXPORT_SYMBOL_GPL(qca_read_soc_version
);
97 static int qca_send_reset(struct hci_dev
*hdev
)
102 bt_dev_dbg(hdev
, "QCA HCI_RESET");
104 skb
= __hci_cmd_sync(hdev
, HCI_OP_RESET
, 0, NULL
, HCI_INIT_TIMEOUT
);
107 bt_dev_err(hdev
, "QCA Reset failed (%d)", err
);
116 int qca_send_pre_shutdown_cmd(struct hci_dev
*hdev
)
121 bt_dev_dbg(hdev
, "QCA pre shutdown cmd");
123 skb
= __hci_cmd_sync_ev(hdev
, QCA_PRE_SHUTDOWN_CMD
, 0,
124 NULL
, HCI_EV_CMD_COMPLETE
, HCI_INIT_TIMEOUT
);
128 bt_dev_err(hdev
, "QCA preshutdown_cmd failed (%d)", err
);
136 EXPORT_SYMBOL_GPL(qca_send_pre_shutdown_cmd
);
138 static void qca_tlv_check_data(struct qca_fw_config
*config
,
139 const struct firmware
*fw
, enum qca_btsoc_type soc_type
)
145 struct tlv_type_hdr
*tlv
;
146 struct tlv_type_patch
*tlv_patch
;
147 struct tlv_type_nvm
*tlv_nvm
;
148 uint8_t nvm_baud_rate
= config
->user_baud_rate
;
150 tlv
= (struct tlv_type_hdr
*)fw
->data
;
152 type_len
= le32_to_cpu(tlv
->type_len
);
153 length
= (type_len
>> 8) & 0x00ffffff;
155 BT_DBG("TLV Type\t\t : 0x%x", type_len
& 0x000000ff);
156 BT_DBG("Length\t\t : %d bytes", length
);
158 config
->dnld_mode
= QCA_SKIP_EVT_NONE
;
159 config
->dnld_type
= QCA_SKIP_EVT_NONE
;
161 switch (config
->type
) {
163 tlv_patch
= (struct tlv_type_patch
*)tlv
->data
;
165 /* For Rome version 1.1 to 3.1, all segment commands
166 * are acked by a vendor specific event (VSE).
167 * For Rome >= 3.2, the download mode field indicates
168 * if VSE is skipped by the controller.
169 * In case VSE is skipped, only the last segment is acked.
171 config
->dnld_mode
= tlv_patch
->download_mode
;
172 config
->dnld_type
= config
->dnld_mode
;
174 BT_DBG("Total Length : %d bytes",
175 le32_to_cpu(tlv_patch
->total_size
));
176 BT_DBG("Patch Data Length : %d bytes",
177 le32_to_cpu(tlv_patch
->data_length
));
178 BT_DBG("Signing Format Version : 0x%x",
179 tlv_patch
->format_version
);
180 BT_DBG("Signature Algorithm : 0x%x",
181 tlv_patch
->signature
);
182 BT_DBG("Download mode : 0x%x",
183 tlv_patch
->download_mode
);
184 BT_DBG("Reserved : 0x%x",
185 tlv_patch
->reserved1
);
186 BT_DBG("Product ID : 0x%04x",
187 le16_to_cpu(tlv_patch
->product_id
));
188 BT_DBG("Rom Build Version : 0x%04x",
189 le16_to_cpu(tlv_patch
->rom_build
));
190 BT_DBG("Patch Version : 0x%04x",
191 le16_to_cpu(tlv_patch
->patch_version
));
192 BT_DBG("Reserved : 0x%x",
193 le16_to_cpu(tlv_patch
->reserved2
));
194 BT_DBG("Patch Entry Address : 0x%x",
195 le32_to_cpu(tlv_patch
->entry
));
201 while (idx
< length
) {
202 tlv_nvm
= (struct tlv_type_nvm
*)(data
+ idx
);
204 tag_id
= le16_to_cpu(tlv_nvm
->tag_id
);
205 tag_len
= le16_to_cpu(tlv_nvm
->tag_len
);
207 /* Update NVM tags as needed */
210 /* HCI transport layer parameters
211 * enabling software inband sleep
212 * onto controller side.
214 tlv_nvm
->data
[0] |= 0x80;
217 if (soc_type
>= QCA_WCN3991
)
218 tlv_nvm
->data
[1] = nvm_baud_rate
;
220 tlv_nvm
->data
[2] = nvm_baud_rate
;
224 case EDL_TAG_ID_DEEP_SLEEP
:
226 * enabling deep sleep feature on controller.
228 tlv_nvm
->data
[0] |= 0x01;
233 idx
+= (sizeof(u16
) + sizeof(u16
) + 8 + tag_len
);
238 BT_ERR("Unknown TLV type %d", config
->type
);
243 static int qca_tlv_send_segment(struct hci_dev
*hdev
, int seg_size
,
244 const u8
*data
, enum qca_tlv_dnld_mode mode
,
245 enum qca_btsoc_type soc_type
)
248 struct edl_event_hdr
*edl
;
249 struct tlv_seg_resp
*tlv_resp
;
250 u8 cmd
[MAX_SIZE_PER_TLV_SEGMENT
+ 2];
252 u8 event_type
= HCI_EV_VENDOR
;
253 u8 rlen
= (sizeof(*edl
) + sizeof(*tlv_resp
));
254 u8 rtype
= EDL_TVL_DNLD_RES_EVT
;
256 cmd
[0] = EDL_PATCH_TLV_REQ_CMD
;
258 memcpy(cmd
+ 2, data
, seg_size
);
260 if (mode
== QCA_SKIP_EVT_VSE_CC
|| mode
== QCA_SKIP_EVT_VSE
)
261 return __hci_cmd_send(hdev
, EDL_PATCH_CMD_OPCODE
, seg_size
+ 2,
264 /* Unlike other SoC's sending version command response as payload to
265 * VSE event. WCN3991 sends version command response as a payload to
266 * command complete event.
268 if (soc_type
>= QCA_WCN3991
) {
271 rtype
= EDL_PATCH_TLV_REQ_CMD
;
274 skb
= __hci_cmd_sync_ev(hdev
, EDL_PATCH_CMD_OPCODE
, seg_size
+ 2, cmd
,
275 event_type
, HCI_INIT_TIMEOUT
);
278 bt_dev_err(hdev
, "QCA Failed to send TLV segment (%d)", err
);
282 if (skb
->len
!= rlen
) {
283 bt_dev_err(hdev
, "QCA TLV response size mismatch");
288 edl
= (struct edl_event_hdr
*)(skb
->data
);
290 bt_dev_err(hdev
, "TLV with no header");
295 if (edl
->cresp
!= EDL_CMD_REQ_RES_EVT
|| edl
->rtype
!= rtype
) {
296 bt_dev_err(hdev
, "QCA TLV with error stat 0x%x rtype 0x%x",
297 edl
->cresp
, edl
->rtype
);
301 if (soc_type
>= QCA_WCN3991
)
304 tlv_resp
= (struct tlv_seg_resp
*)(edl
->data
);
305 if (tlv_resp
->result
) {
306 bt_dev_err(hdev
, "QCA TLV with error stat 0x%x rtype 0x%x (0x%x)",
307 edl
->cresp
, edl
->rtype
, tlv_resp
->result
);
316 static int qca_inject_cmd_complete_event(struct hci_dev
*hdev
)
318 struct hci_event_hdr
*hdr
;
319 struct hci_ev_cmd_complete
*evt
;
322 skb
= bt_skb_alloc(sizeof(*hdr
) + sizeof(*evt
) + 1, GFP_KERNEL
);
326 hdr
= skb_put(skb
, sizeof(*hdr
));
327 hdr
->evt
= HCI_EV_CMD_COMPLETE
;
328 hdr
->plen
= sizeof(*evt
) + 1;
330 evt
= skb_put(skb
, sizeof(*evt
));
332 evt
->opcode
= cpu_to_le16(QCA_HCI_CC_OPCODE
);
334 skb_put_u8(skb
, QCA_HCI_CC_SUCCESS
);
336 hci_skb_pkt_type(skb
) = HCI_EVENT_PKT
;
338 return hci_recv_frame(hdev
, skb
);
341 static int qca_download_firmware(struct hci_dev
*hdev
,
342 struct qca_fw_config
*config
,
343 enum qca_btsoc_type soc_type
)
345 const struct firmware
*fw
;
347 int ret
, remain
, i
= 0;
349 bt_dev_info(hdev
, "QCA Downloading %s", config
->fwname
);
351 ret
= request_firmware(&fw
, config
->fwname
, &hdev
->dev
);
353 bt_dev_err(hdev
, "QCA Failed to request file: %s (%d)",
354 config
->fwname
, ret
);
358 qca_tlv_check_data(config
, fw
, soc_type
);
363 int segsize
= min(MAX_SIZE_PER_TLV_SEGMENT
, remain
);
365 bt_dev_dbg(hdev
, "Send segment %d, size %d", i
++, segsize
);
368 /* The last segment is always acked regardless download mode */
369 if (!remain
|| segsize
< MAX_SIZE_PER_TLV_SEGMENT
)
370 config
->dnld_mode
= QCA_SKIP_EVT_NONE
;
372 ret
= qca_tlv_send_segment(hdev
, segsize
, segment
,
373 config
->dnld_mode
, soc_type
);
380 /* Latest qualcomm chipsets are not sending a command complete event
381 * for every fw packet sent. They only respond with a vendor specific
382 * event for the last packet. This optimization in the chip will
383 * decrease the BT in initialization time. Here we will inject a command
384 * complete event to avoid a command timeout error message.
386 if (config
->dnld_type
== QCA_SKIP_EVT_VSE_CC
||
387 config
->dnld_type
== QCA_SKIP_EVT_VSE
)
388 ret
= qca_inject_cmd_complete_event(hdev
);
391 release_firmware(fw
);
396 static int qca_disable_soc_logging(struct hci_dev
*hdev
)
402 cmd
[0] = QCA_DISABLE_LOGGING_SUB_OP
;
404 skb
= __hci_cmd_sync_ev(hdev
, QCA_DISABLE_LOGGING
, sizeof(cmd
), cmd
,
405 HCI_EV_CMD_COMPLETE
, HCI_INIT_TIMEOUT
);
408 bt_dev_err(hdev
, "QCA Failed to disable soc logging(%d)", err
);
417 int qca_set_bdaddr_rome(struct hci_dev
*hdev
, const bdaddr_t
*bdaddr
)
423 cmd
[0] = EDL_NVM_ACCESS_SET_REQ_CMD
;
424 cmd
[1] = 0x02; /* TAG ID */
425 cmd
[2] = sizeof(bdaddr_t
); /* size */
426 memcpy(cmd
+ 3, bdaddr
, sizeof(bdaddr_t
));
427 skb
= __hci_cmd_sync_ev(hdev
, EDL_NVM_ACCESS_OPCODE
, sizeof(cmd
), cmd
,
428 HCI_EV_VENDOR
, HCI_INIT_TIMEOUT
);
431 bt_dev_err(hdev
, "QCA Change address command failed (%d)", err
);
439 EXPORT_SYMBOL_GPL(qca_set_bdaddr_rome
);
441 int qca_uart_setup(struct hci_dev
*hdev
, uint8_t baudrate
,
442 enum qca_btsoc_type soc_type
, struct qca_btsoc_version ver
,
443 const char *firmware_name
)
445 struct qca_fw_config config
;
450 bt_dev_dbg(hdev
, "QCA setup on UART");
452 soc_ver
= get_soc_ver(ver
.soc_id
, ver
.rom_ver
);
454 bt_dev_info(hdev
, "QCA controller version 0x%08x", soc_ver
);
456 config
.user_baud_rate
= baudrate
;
458 /* Download rampatch file */
459 config
.type
= TLV_TYPE_PATCH
;
460 if (qca_is_wcn399x(soc_type
)) {
461 /* Firmware files to download are based on ROM version.
462 * ROM version is derived from last two bytes of soc_ver.
464 rom_ver
= ((soc_ver
& 0x00000f00) >> 0x04) |
465 (soc_ver
& 0x0000000f);
466 snprintf(config
.fwname
, sizeof(config
.fwname
),
467 "qca/crbtfw%02x.tlv", rom_ver
);
468 } else if (soc_type
== QCA_QCA6390
) {
469 rom_ver
= ((soc_ver
& 0x00000f00) >> 0x04) |
470 (soc_ver
& 0x0000000f);
471 snprintf(config
.fwname
, sizeof(config
.fwname
),
472 "qca/htbtfw%02x.tlv", rom_ver
);
474 snprintf(config
.fwname
, sizeof(config
.fwname
),
475 "qca/rampatch_%08x.bin", soc_ver
);
478 err
= qca_download_firmware(hdev
, &config
, soc_type
);
480 bt_dev_err(hdev
, "QCA Failed to download patch (%d)", err
);
484 /* Give the controller some time to get ready to receive the NVM */
487 /* Download NVM configuration */
488 config
.type
= TLV_TYPE_NVM
;
490 snprintf(config
.fwname
, sizeof(config
.fwname
),
491 "qca/%s", firmware_name
);
492 else if (qca_is_wcn399x(soc_type
)) {
493 if (ver
.soc_id
== QCA_WCN3991_SOC_ID
) {
494 snprintf(config
.fwname
, sizeof(config
.fwname
),
495 "qca/crnv%02xu.bin", rom_ver
);
497 snprintf(config
.fwname
, sizeof(config
.fwname
),
498 "qca/crnv%02x.bin", rom_ver
);
501 else if (soc_type
== QCA_QCA6390
)
502 snprintf(config
.fwname
, sizeof(config
.fwname
),
503 "qca/htnv%02x.bin", rom_ver
);
505 snprintf(config
.fwname
, sizeof(config
.fwname
),
506 "qca/nvm_%08x.bin", soc_ver
);
508 err
= qca_download_firmware(hdev
, &config
, soc_type
);
510 bt_dev_err(hdev
, "QCA Failed to download NVM (%d)", err
);
514 if (soc_type
>= QCA_WCN3991
) {
515 err
= qca_disable_soc_logging(hdev
);
520 /* Perform HCI reset */
521 err
= qca_send_reset(hdev
);
523 bt_dev_err(hdev
, "QCA Failed to run HCI_RESET (%d)", err
);
527 bt_dev_info(hdev
, "QCA setup on UART is completed");
531 EXPORT_SYMBOL_GPL(qca_uart_setup
);
533 int qca_set_bdaddr(struct hci_dev
*hdev
, const bdaddr_t
*bdaddr
)
538 skb
= __hci_cmd_sync_ev(hdev
, EDL_WRITE_BD_ADDR_OPCODE
, 6, bdaddr
,
539 HCI_EV_VENDOR
, HCI_INIT_TIMEOUT
);
542 bt_dev_err(hdev
, "QCA Change address cmd failed (%d)", err
);
550 EXPORT_SYMBOL_GPL(qca_set_bdaddr
);
553 MODULE_AUTHOR("Ben Young Tae Kim <ytkim@qca.qualcomm.com>");
554 MODULE_DESCRIPTION("Bluetooth support for Qualcomm Atheros family ver " VERSION
);
555 MODULE_VERSION(VERSION
);
556 MODULE_LICENSE("GPL");