2 * Bluetooth supports for Qualcomm Atheros chips
4 * Copyright (c) 2015 The Linux Foundation. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/module.h>
21 #include <linux/firmware.h>
23 #include <net/bluetooth/bluetooth.h>
24 #include <net/bluetooth/hci_core.h>
30 int qca_read_soc_version(struct hci_dev
*hdev
, u32
*soc_version
)
33 struct edl_event_hdr
*edl
;
34 struct rome_version
*ver
;
38 bt_dev_dbg(hdev
, "QCA Version Request");
40 cmd
= EDL_PATCH_VER_REQ_CMD
;
41 skb
= __hci_cmd_sync_ev(hdev
, EDL_PATCH_CMD_OPCODE
, EDL_PATCH_CMD_LEN
,
42 &cmd
, HCI_EV_VENDOR
, HCI_INIT_TIMEOUT
);
45 bt_dev_err(hdev
, "Reading QCA version information failed (%d)",
50 if (skb
->len
!= sizeof(*edl
) + sizeof(*ver
)) {
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
!= EDL_APP_VER_RES_EVT
) {
65 bt_dev_err(hdev
, "QCA Wrong packet received %d %d", edl
->cresp
,
71 ver
= (struct rome_version
*)(edl
->data
);
73 BT_DBG("%s: Product:0x%08x", hdev
->name
, le32_to_cpu(ver
->product_id
));
74 BT_DBG("%s: Patch :0x%08x", hdev
->name
, le16_to_cpu(ver
->patch_ver
));
75 BT_DBG("%s: ROM :0x%08x", hdev
->name
, le16_to_cpu(ver
->rome_ver
));
76 BT_DBG("%s: SOC :0x%08x", hdev
->name
, le32_to_cpu(ver
->soc_id
));
78 /* QCA chipset version can be decided by patch and SoC
79 * version, combination with upper 2 bytes from SoC
80 * and lower 2 bytes from patch will be used.
82 *soc_version
= (le32_to_cpu(ver
->soc_id
) << 16) |
83 (le16_to_cpu(ver
->rome_ver
) & 0x0000ffff);
84 if (*soc_version
== 0)
90 bt_dev_err(hdev
, "QCA Failed to get version (%d)", err
);
94 EXPORT_SYMBOL_GPL(qca_read_soc_version
);
96 static int qca_send_reset(struct hci_dev
*hdev
)
101 bt_dev_dbg(hdev
, "QCA HCI_RESET");
103 skb
= __hci_cmd_sync(hdev
, HCI_OP_RESET
, 0, NULL
, HCI_INIT_TIMEOUT
);
106 bt_dev_err(hdev
, "QCA Reset failed (%d)", err
);
115 static void qca_tlv_check_data(struct rome_config
*config
,
116 const struct firmware
*fw
)
122 struct tlv_type_hdr
*tlv
;
123 struct tlv_type_patch
*tlv_patch
;
124 struct tlv_type_nvm
*tlv_nvm
;
126 tlv
= (struct tlv_type_hdr
*)fw
->data
;
128 type_len
= le32_to_cpu(tlv
->type_len
);
129 length
= (type_len
>> 8) & 0x00ffffff;
131 BT_DBG("TLV Type\t\t : 0x%x", type_len
& 0x000000ff);
132 BT_DBG("Length\t\t : %d bytes", length
);
134 config
->dnld_mode
= ROME_SKIP_EVT_NONE
;
136 switch (config
->type
) {
138 tlv_patch
= (struct tlv_type_patch
*)tlv
->data
;
140 /* For Rome version 1.1 to 3.1, all segment commands
141 * are acked by a vendor specific event (VSE).
142 * For Rome >= 3.2, the download mode field indicates
143 * if VSE is skipped by the controller.
144 * In case VSE is skipped, only the last segment is acked.
146 config
->dnld_mode
= tlv_patch
->download_mode
;
148 BT_DBG("Total Length : %d bytes",
149 le32_to_cpu(tlv_patch
->total_size
));
150 BT_DBG("Patch Data Length : %d bytes",
151 le32_to_cpu(tlv_patch
->data_length
));
152 BT_DBG("Signing Format Version : 0x%x",
153 tlv_patch
->format_version
);
154 BT_DBG("Signature Algorithm : 0x%x",
155 tlv_patch
->signature
);
156 BT_DBG("Download mode : 0x%x",
157 tlv_patch
->download_mode
);
158 BT_DBG("Reserved : 0x%x",
159 tlv_patch
->reserved1
);
160 BT_DBG("Product ID : 0x%04x",
161 le16_to_cpu(tlv_patch
->product_id
));
162 BT_DBG("Rom Build Version : 0x%04x",
163 le16_to_cpu(tlv_patch
->rom_build
));
164 BT_DBG("Patch Version : 0x%04x",
165 le16_to_cpu(tlv_patch
->patch_version
));
166 BT_DBG("Reserved : 0x%x",
167 le16_to_cpu(tlv_patch
->reserved2
));
168 BT_DBG("Patch Entry Address : 0x%x",
169 le32_to_cpu(tlv_patch
->entry
));
175 while (idx
< length
) {
176 tlv_nvm
= (struct tlv_type_nvm
*)(data
+ idx
);
178 tag_id
= le16_to_cpu(tlv_nvm
->tag_id
);
179 tag_len
= le16_to_cpu(tlv_nvm
->tag_len
);
181 /* Update NVM tags as needed */
184 /* HCI transport layer parameters
185 * enabling software inband sleep
186 * onto controller side.
188 tlv_nvm
->data
[0] |= 0x80;
191 tlv_nvm
->data
[2] = config
->user_baud_rate
;
195 case EDL_TAG_ID_DEEP_SLEEP
:
197 * enabling deep sleep feature on controller.
199 tlv_nvm
->data
[0] |= 0x01;
204 idx
+= (sizeof(u16
) + sizeof(u16
) + 8 + tag_len
);
209 BT_ERR("Unknown TLV type %d", config
->type
);
214 static int qca_tlv_send_segment(struct hci_dev
*hdev
, int seg_size
,
215 const u8
*data
, enum rome_tlv_dnld_mode mode
)
218 struct edl_event_hdr
*edl
;
219 struct tlv_seg_resp
*tlv_resp
;
220 u8 cmd
[MAX_SIZE_PER_TLV_SEGMENT
+ 2];
223 cmd
[0] = EDL_PATCH_TLV_REQ_CMD
;
225 memcpy(cmd
+ 2, data
, seg_size
);
227 if (mode
== ROME_SKIP_EVT_VSE_CC
|| mode
== ROME_SKIP_EVT_VSE
)
228 return __hci_cmd_send(hdev
, EDL_PATCH_CMD_OPCODE
, seg_size
+ 2,
231 skb
= __hci_cmd_sync_ev(hdev
, EDL_PATCH_CMD_OPCODE
, seg_size
+ 2, cmd
,
232 HCI_EV_VENDOR
, HCI_INIT_TIMEOUT
);
235 bt_dev_err(hdev
, "QCA Failed to send TLV segment (%d)", err
);
239 if (skb
->len
!= sizeof(*edl
) + sizeof(*tlv_resp
)) {
240 bt_dev_err(hdev
, "QCA TLV response size mismatch");
245 edl
= (struct edl_event_hdr
*)(skb
->data
);
247 bt_dev_err(hdev
, "TLV with no header");
252 tlv_resp
= (struct tlv_seg_resp
*)(edl
->data
);
254 if (edl
->cresp
!= EDL_CMD_REQ_RES_EVT
||
255 edl
->rtype
!= EDL_TVL_DNLD_RES_EVT
|| tlv_resp
->result
!= 0x00) {
256 bt_dev_err(hdev
, "QCA TLV with error stat 0x%x rtype 0x%x (0x%x)",
257 edl
->cresp
, edl
->rtype
, tlv_resp
->result
);
267 static int qca_download_firmware(struct hci_dev
*hdev
,
268 struct rome_config
*config
)
270 const struct firmware
*fw
;
272 int ret
, remain
, i
= 0;
274 bt_dev_info(hdev
, "QCA Downloading %s", config
->fwname
);
276 ret
= request_firmware(&fw
, config
->fwname
, &hdev
->dev
);
278 bt_dev_err(hdev
, "QCA Failed to request file: %s (%d)",
279 config
->fwname
, ret
);
283 qca_tlv_check_data(config
, fw
);
288 int segsize
= min(MAX_SIZE_PER_TLV_SEGMENT
, remain
);
290 bt_dev_dbg(hdev
, "Send segment %d, size %d", i
++, segsize
);
293 /* The last segment is always acked regardless download mode */
294 if (!remain
|| segsize
< MAX_SIZE_PER_TLV_SEGMENT
)
295 config
->dnld_mode
= ROME_SKIP_EVT_NONE
;
297 ret
= qca_tlv_send_segment(hdev
, segsize
, segment
,
305 release_firmware(fw
);
310 int qca_set_bdaddr_rome(struct hci_dev
*hdev
, const bdaddr_t
*bdaddr
)
316 cmd
[0] = EDL_NVM_ACCESS_SET_REQ_CMD
;
317 cmd
[1] = 0x02; /* TAG ID */
318 cmd
[2] = sizeof(bdaddr_t
); /* size */
319 memcpy(cmd
+ 3, bdaddr
, sizeof(bdaddr_t
));
320 skb
= __hci_cmd_sync_ev(hdev
, EDL_NVM_ACCESS_OPCODE
, sizeof(cmd
), cmd
,
321 HCI_EV_VENDOR
, HCI_INIT_TIMEOUT
);
324 bt_dev_err(hdev
, "QCA Change address command failed (%d)", err
);
332 EXPORT_SYMBOL_GPL(qca_set_bdaddr_rome
);
334 int qca_uart_setup(struct hci_dev
*hdev
, uint8_t baudrate
,
335 enum qca_btsoc_type soc_type
, u32 soc_ver
)
337 struct rome_config config
;
341 bt_dev_dbg(hdev
, "QCA setup on UART");
343 config
.user_baud_rate
= baudrate
;
345 /* Download rampatch file */
346 config
.type
= TLV_TYPE_PATCH
;
347 if (soc_type
== QCA_WCN3990
) {
348 /* Firmware files to download are based on ROM version.
349 * ROM version is derived from last two bytes of soc_ver.
351 rom_ver
= ((soc_ver
& 0x00000f00) >> 0x04) |
352 (soc_ver
& 0x0000000f);
353 snprintf(config
.fwname
, sizeof(config
.fwname
),
354 "qca/crbtfw%02x.tlv", rom_ver
);
356 snprintf(config
.fwname
, sizeof(config
.fwname
),
357 "qca/rampatch_%08x.bin", soc_ver
);
360 err
= qca_download_firmware(hdev
, &config
);
362 bt_dev_err(hdev
, "QCA Failed to download patch (%d)", err
);
366 /* Give the controller some time to get ready to receive the NVM */
369 /* Download NVM configuration */
370 config
.type
= TLV_TYPE_NVM
;
371 if (soc_type
== QCA_WCN3990
)
372 snprintf(config
.fwname
, sizeof(config
.fwname
),
373 "qca/crnv%02x.bin", rom_ver
);
375 snprintf(config
.fwname
, sizeof(config
.fwname
),
376 "qca/nvm_%08x.bin", soc_ver
);
378 err
= qca_download_firmware(hdev
, &config
);
380 bt_dev_err(hdev
, "QCA Failed to download NVM (%d)", err
);
384 /* Perform HCI reset */
385 err
= qca_send_reset(hdev
);
387 bt_dev_err(hdev
, "QCA Failed to run HCI_RESET (%d)", err
);
391 bt_dev_info(hdev
, "QCA setup on UART is completed");
395 EXPORT_SYMBOL_GPL(qca_uart_setup
);
397 MODULE_AUTHOR("Ben Young Tae Kim <ytkim@qca.qualcomm.com>");
398 MODULE_DESCRIPTION("Bluetooth support for Qualcomm Atheros family ver " VERSION
);
399 MODULE_VERSION(VERSION
);
400 MODULE_LICENSE("GPL");