Commit | Line | Data |
---|---|---|
45051539 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
83e81961 BYTK |
2 | /* |
3 | * Bluetooth supports for Qualcomm Atheros chips | |
4 | * | |
5 | * Copyright (c) 2015 The Linux Foundation. All rights reserved. | |
83e81961 BYTK |
6 | */ |
7 | #include <linux/module.h> | |
8 | #include <linux/firmware.h> | |
9 | ||
10 | #include <net/bluetooth/bluetooth.h> | |
11 | #include <net/bluetooth/hci_core.h> | |
12 | ||
13 | #include "btqca.h" | |
14 | ||
15 | #define VERSION "0.1" | |
16 | ||
ba493d4f | 17 | int qca_read_soc_version(struct hci_dev *hdev, u32 *soc_version) |
83e81961 BYTK |
18 | { |
19 | struct sk_buff *skb; | |
20 | struct edl_event_hdr *edl; | |
21 | struct rome_version *ver; | |
22 | char cmd; | |
23 | int err = 0; | |
24 | ||
ba493d4f | 25 | bt_dev_dbg(hdev, "QCA Version Request"); |
83e81961 BYTK |
26 | |
27 | cmd = EDL_PATCH_VER_REQ_CMD; | |
28 | skb = __hci_cmd_sync_ev(hdev, EDL_PATCH_CMD_OPCODE, EDL_PATCH_CMD_LEN, | |
e4cc5a18 | 29 | &cmd, HCI_EV_VENDOR, HCI_INIT_TIMEOUT); |
83e81961 BYTK |
30 | if (IS_ERR(skb)) { |
31 | err = PTR_ERR(skb); | |
ba493d4f BG |
32 | bt_dev_err(hdev, "Reading QCA version information failed (%d)", |
33 | err); | |
83e81961 BYTK |
34 | return err; |
35 | } | |
36 | ||
37 | if (skb->len != sizeof(*edl) + sizeof(*ver)) { | |
ba493d4f | 38 | bt_dev_err(hdev, "QCA Version size mismatch len %d", skb->len); |
83e81961 BYTK |
39 | err = -EILSEQ; |
40 | goto out; | |
41 | } | |
42 | ||
43 | edl = (struct edl_event_hdr *)(skb->data); | |
0676cab4 | 44 | if (!edl) { |
ba493d4f | 45 | bt_dev_err(hdev, "QCA TLV with no header"); |
83e81961 BYTK |
46 | err = -EILSEQ; |
47 | goto out; | |
48 | } | |
49 | ||
50 | if (edl->cresp != EDL_CMD_REQ_RES_EVT || | |
51 | edl->rtype != EDL_APP_VER_RES_EVT) { | |
ba493d4f BG |
52 | bt_dev_err(hdev, "QCA Wrong packet received %d %d", edl->cresp, |
53 | edl->rtype); | |
83e81961 BYTK |
54 | err = -EIO; |
55 | goto out; | |
56 | } | |
57 | ||
58 | ver = (struct rome_version *)(edl->data); | |
59 | ||
60 | BT_DBG("%s: Product:0x%08x", hdev->name, le32_to_cpu(ver->product_id)); | |
61 | BT_DBG("%s: Patch :0x%08x", hdev->name, le16_to_cpu(ver->patch_ver)); | |
62 | BT_DBG("%s: ROM :0x%08x", hdev->name, le16_to_cpu(ver->rome_ver)); | |
63 | BT_DBG("%s: SOC :0x%08x", hdev->name, le32_to_cpu(ver->soc_id)); | |
64 | ||
ba493d4f | 65 | /* QCA chipset version can be decided by patch and SoC |
83e81961 BYTK |
66 | * version, combination with upper 2 bytes from SoC |
67 | * and lower 2 bytes from patch will be used. | |
68 | */ | |
ba493d4f | 69 | *soc_version = (le32_to_cpu(ver->soc_id) << 16) | |
2193a980 | 70 | (le16_to_cpu(ver->rome_ver) & 0x0000ffff); |
aadebac4 BG |
71 | if (*soc_version == 0) |
72 | err = -EILSEQ; | |
83e81961 BYTK |
73 | |
74 | out: | |
75 | kfree_skb(skb); | |
aadebac4 BG |
76 | if (err) |
77 | bt_dev_err(hdev, "QCA Failed to get version (%d)", err); | |
83e81961 BYTK |
78 | |
79 | return err; | |
80 | } | |
ba493d4f | 81 | EXPORT_SYMBOL_GPL(qca_read_soc_version); |
83e81961 | 82 | |
ba493d4f | 83 | static int qca_send_reset(struct hci_dev *hdev) |
83e81961 BYTK |
84 | { |
85 | struct sk_buff *skb; | |
86 | int err; | |
87 | ||
ba493d4f | 88 | bt_dev_dbg(hdev, "QCA HCI_RESET"); |
83e81961 BYTK |
89 | |
90 | skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT); | |
91 | if (IS_ERR(skb)) { | |
92 | err = PTR_ERR(skb); | |
ba493d4f | 93 | bt_dev_err(hdev, "QCA Reset failed (%d)", err); |
83e81961 BYTK |
94 | return err; |
95 | } | |
96 | ||
97 | kfree_skb(skb); | |
98 | ||
99 | return 0; | |
100 | } | |
101 | ||
ba493d4f | 102 | static void qca_tlv_check_data(struct rome_config *config, |
83e81961 BYTK |
103 | const struct firmware *fw) |
104 | { | |
105 | const u8 *data; | |
106 | u32 type_len; | |
107 | u16 tag_id, tag_len; | |
108 | int idx, length; | |
109 | struct tlv_type_hdr *tlv; | |
110 | struct tlv_type_patch *tlv_patch; | |
111 | struct tlv_type_nvm *tlv_nvm; | |
112 | ||
113 | tlv = (struct tlv_type_hdr *)fw->data; | |
114 | ||
115 | type_len = le32_to_cpu(tlv->type_len); | |
116 | length = (type_len >> 8) & 0x00ffffff; | |
117 | ||
118 | BT_DBG("TLV Type\t\t : 0x%x", type_len & 0x000000ff); | |
119 | BT_DBG("Length\t\t : %d bytes", length); | |
120 | ||
6e03126a LP |
121 | config->dnld_mode = ROME_SKIP_EVT_NONE; |
122 | ||
83e81961 BYTK |
123 | switch (config->type) { |
124 | case TLV_TYPE_PATCH: | |
125 | tlv_patch = (struct tlv_type_patch *)tlv->data; | |
6e03126a LP |
126 | |
127 | /* For Rome version 1.1 to 3.1, all segment commands | |
128 | * are acked by a vendor specific event (VSE). | |
129 | * For Rome >= 3.2, the download mode field indicates | |
130 | * if VSE is skipped by the controller. | |
131 | * In case VSE is skipped, only the last segment is acked. | |
132 | */ | |
133 | config->dnld_mode = tlv_patch->download_mode; | |
32646db8 | 134 | config->dnld_type = config->dnld_mode; |
6e03126a LP |
135 | |
136 | BT_DBG("Total Length : %d bytes", | |
83e81961 | 137 | le32_to_cpu(tlv_patch->total_size)); |
6e03126a | 138 | BT_DBG("Patch Data Length : %d bytes", |
83e81961 BYTK |
139 | le32_to_cpu(tlv_patch->data_length)); |
140 | BT_DBG("Signing Format Version : 0x%x", | |
141 | tlv_patch->format_version); | |
6e03126a | 142 | BT_DBG("Signature Algorithm : 0x%x", |
83e81961 | 143 | tlv_patch->signature); |
6e03126a LP |
144 | BT_DBG("Download mode : 0x%x", |
145 | tlv_patch->download_mode); | |
146 | BT_DBG("Reserved : 0x%x", | |
147 | tlv_patch->reserved1); | |
148 | BT_DBG("Product ID : 0x%04x", | |
83e81961 | 149 | le16_to_cpu(tlv_patch->product_id)); |
6e03126a | 150 | BT_DBG("Rom Build Version : 0x%04x", |
83e81961 | 151 | le16_to_cpu(tlv_patch->rom_build)); |
6e03126a | 152 | BT_DBG("Patch Version : 0x%04x", |
83e81961 | 153 | le16_to_cpu(tlv_patch->patch_version)); |
6e03126a | 154 | BT_DBG("Reserved : 0x%x", |
83e81961 | 155 | le16_to_cpu(tlv_patch->reserved2)); |
6e03126a | 156 | BT_DBG("Patch Entry Address : 0x%x", |
83e81961 BYTK |
157 | le32_to_cpu(tlv_patch->entry)); |
158 | break; | |
159 | ||
160 | case TLV_TYPE_NVM: | |
161 | idx = 0; | |
162 | data = tlv->data; | |
163 | while (idx < length) { | |
164 | tlv_nvm = (struct tlv_type_nvm *)(data + idx); | |
165 | ||
166 | tag_id = le16_to_cpu(tlv_nvm->tag_id); | |
167 | tag_len = le16_to_cpu(tlv_nvm->tag_len); | |
168 | ||
169 | /* Update NVM tags as needed */ | |
170 | switch (tag_id) { | |
171 | case EDL_TAG_ID_HCI: | |
172 | /* HCI transport layer parameters | |
173 | * enabling software inband sleep | |
174 | * onto controller side. | |
175 | */ | |
176 | tlv_nvm->data[0] |= 0x80; | |
177 | ||
178 | /* UART Baud Rate */ | |
179 | tlv_nvm->data[2] = config->user_baud_rate; | |
180 | ||
181 | break; | |
182 | ||
183 | case EDL_TAG_ID_DEEP_SLEEP: | |
184 | /* Sleep enable mask | |
185 | * enabling deep sleep feature on controller. | |
186 | */ | |
187 | tlv_nvm->data[0] |= 0x01; | |
188 | ||
189 | break; | |
190 | } | |
191 | ||
192 | idx += (sizeof(u16) + sizeof(u16) + 8 + tag_len); | |
193 | } | |
194 | break; | |
195 | ||
196 | default: | |
197 | BT_ERR("Unknown TLV type %d", config->type); | |
198 | break; | |
199 | } | |
200 | } | |
201 | ||
ba493d4f | 202 | static int qca_tlv_send_segment(struct hci_dev *hdev, int seg_size, |
6e03126a | 203 | const u8 *data, enum rome_tlv_dnld_mode mode) |
83e81961 BYTK |
204 | { |
205 | struct sk_buff *skb; | |
206 | struct edl_event_hdr *edl; | |
207 | struct tlv_seg_resp *tlv_resp; | |
208 | u8 cmd[MAX_SIZE_PER_TLV_SEGMENT + 2]; | |
209 | int err = 0; | |
210 | ||
83e81961 BYTK |
211 | cmd[0] = EDL_PATCH_TLV_REQ_CMD; |
212 | cmd[1] = seg_size; | |
213 | memcpy(cmd + 2, data, seg_size); | |
214 | ||
6e03126a LP |
215 | if (mode == ROME_SKIP_EVT_VSE_CC || mode == ROME_SKIP_EVT_VSE) |
216 | return __hci_cmd_send(hdev, EDL_PATCH_CMD_OPCODE, seg_size + 2, | |
217 | cmd); | |
218 | ||
83e81961 | 219 | skb = __hci_cmd_sync_ev(hdev, EDL_PATCH_CMD_OPCODE, seg_size + 2, cmd, |
e4cc5a18 | 220 | HCI_EV_VENDOR, HCI_INIT_TIMEOUT); |
83e81961 BYTK |
221 | if (IS_ERR(skb)) { |
222 | err = PTR_ERR(skb); | |
ba493d4f | 223 | bt_dev_err(hdev, "QCA Failed to send TLV segment (%d)", err); |
83e81961 BYTK |
224 | return err; |
225 | } | |
226 | ||
227 | if (skb->len != sizeof(*edl) + sizeof(*tlv_resp)) { | |
ba493d4f | 228 | bt_dev_err(hdev, "QCA TLV response size mismatch"); |
83e81961 BYTK |
229 | err = -EILSEQ; |
230 | goto out; | |
231 | } | |
232 | ||
233 | edl = (struct edl_event_hdr *)(skb->data); | |
0676cab4 | 234 | if (!edl) { |
ba493d4f | 235 | bt_dev_err(hdev, "TLV with no header"); |
83e81961 BYTK |
236 | err = -EILSEQ; |
237 | goto out; | |
238 | } | |
239 | ||
240 | tlv_resp = (struct tlv_seg_resp *)(edl->data); | |
241 | ||
242 | if (edl->cresp != EDL_CMD_REQ_RES_EVT || | |
243 | edl->rtype != EDL_TVL_DNLD_RES_EVT || tlv_resp->result != 0x00) { | |
ba493d4f BG |
244 | bt_dev_err(hdev, "QCA TLV with error stat 0x%x rtype 0x%x (0x%x)", |
245 | edl->cresp, edl->rtype, tlv_resp->result); | |
83e81961 BYTK |
246 | err = -EIO; |
247 | } | |
248 | ||
249 | out: | |
250 | kfree_skb(skb); | |
251 | ||
252 | return err; | |
253 | } | |
254 | ||
32646db8 BG |
255 | static int qca_inject_cmd_complete_event(struct hci_dev *hdev) |
256 | { | |
257 | struct hci_event_hdr *hdr; | |
258 | struct hci_ev_cmd_complete *evt; | |
259 | struct sk_buff *skb; | |
260 | ||
261 | skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL); | |
262 | if (!skb) | |
263 | return -ENOMEM; | |
264 | ||
265 | hdr = skb_put(skb, sizeof(*hdr)); | |
266 | hdr->evt = HCI_EV_CMD_COMPLETE; | |
267 | hdr->plen = sizeof(*evt) + 1; | |
268 | ||
269 | evt = skb_put(skb, sizeof(*evt)); | |
270 | evt->ncmd = 1; | |
2fde6afb | 271 | evt->opcode = cpu_to_le16(QCA_HCI_CC_OPCODE); |
32646db8 BG |
272 | |
273 | skb_put_u8(skb, QCA_HCI_CC_SUCCESS); | |
274 | ||
275 | hci_skb_pkt_type(skb) = HCI_EVENT_PKT; | |
276 | ||
277 | return hci_recv_frame(hdev, skb); | |
278 | } | |
279 | ||
ba493d4f | 280 | static int qca_download_firmware(struct hci_dev *hdev, |
83e81961 BYTK |
281 | struct rome_config *config) |
282 | { | |
283 | const struct firmware *fw; | |
6e03126a LP |
284 | const u8 *segment; |
285 | int ret, remain, i = 0; | |
83e81961 | 286 | |
ba493d4f | 287 | bt_dev_info(hdev, "QCA Downloading %s", config->fwname); |
83e81961 BYTK |
288 | |
289 | ret = request_firmware(&fw, config->fwname, &hdev->dev); | |
290 | if (ret) { | |
ba493d4f BG |
291 | bt_dev_err(hdev, "QCA Failed to request file: %s (%d)", |
292 | config->fwname, ret); | |
83e81961 BYTK |
293 | return ret; |
294 | } | |
295 | ||
ba493d4f | 296 | qca_tlv_check_data(config, fw); |
83e81961 | 297 | |
6e03126a LP |
298 | segment = fw->data; |
299 | remain = fw->size; | |
300 | while (remain > 0) { | |
301 | int segsize = min(MAX_SIZE_PER_TLV_SEGMENT, remain); | |
302 | ||
303 | bt_dev_dbg(hdev, "Send segment %d, size %d", i++, segsize); | |
304 | ||
305 | remain -= segsize; | |
306 | /* The last segment is always acked regardless download mode */ | |
307 | if (!remain || segsize < MAX_SIZE_PER_TLV_SEGMENT) | |
308 | config->dnld_mode = ROME_SKIP_EVT_NONE; | |
309 | ||
ba493d4f | 310 | ret = qca_tlv_send_segment(hdev, segsize, segment, |
6e03126a LP |
311 | config->dnld_mode); |
312 | if (ret) | |
32646db8 | 313 | goto out; |
6e03126a LP |
314 | |
315 | segment += segsize; | |
83e81961 BYTK |
316 | } |
317 | ||
32646db8 BG |
318 | /* Latest qualcomm chipsets are not sending a command complete event |
319 | * for every fw packet sent. They only respond with a vendor specific | |
320 | * event for the last packet. This optimization in the chip will | |
321 | * decrease the BT in initialization time. Here we will inject a command | |
322 | * complete event to avoid a command timeout error message. | |
323 | */ | |
324 | if (config->dnld_type == ROME_SKIP_EVT_VSE_CC || | |
325 | config->dnld_type == ROME_SKIP_EVT_VSE) | |
326 | return qca_inject_cmd_complete_event(hdev); | |
327 | ||
328 | out: | |
83e81961 BYTK |
329 | release_firmware(fw); |
330 | ||
331 | return ret; | |
332 | } | |
333 | ||
334 | int qca_set_bdaddr_rome(struct hci_dev *hdev, const bdaddr_t *bdaddr) | |
335 | { | |
336 | struct sk_buff *skb; | |
337 | u8 cmd[9]; | |
338 | int err; | |
339 | ||
340 | cmd[0] = EDL_NVM_ACCESS_SET_REQ_CMD; | |
341 | cmd[1] = 0x02; /* TAG ID */ | |
342 | cmd[2] = sizeof(bdaddr_t); /* size */ | |
343 | memcpy(cmd + 3, bdaddr, sizeof(bdaddr_t)); | |
344 | skb = __hci_cmd_sync_ev(hdev, EDL_NVM_ACCESS_OPCODE, sizeof(cmd), cmd, | |
e4cc5a18 | 345 | HCI_EV_VENDOR, HCI_INIT_TIMEOUT); |
83e81961 BYTK |
346 | if (IS_ERR(skb)) { |
347 | err = PTR_ERR(skb); | |
ba493d4f | 348 | bt_dev_err(hdev, "QCA Change address command failed (%d)", err); |
83e81961 BYTK |
349 | return err; |
350 | } | |
351 | ||
352 | kfree_skb(skb); | |
353 | ||
354 | return 0; | |
355 | } | |
356 | EXPORT_SYMBOL_GPL(qca_set_bdaddr_rome); | |
357 | ||
aadebac4 | 358 | int qca_uart_setup(struct hci_dev *hdev, uint8_t baudrate, |
99c905c6 RL |
359 | enum qca_btsoc_type soc_type, u32 soc_ver, |
360 | const char *firmware_name) | |
83e81961 | 361 | { |
83e81961 BYTK |
362 | struct rome_config config; |
363 | int err; | |
523760b7 | 364 | u8 rom_ver = 0; |
83e81961 | 365 | |
ba493d4f | 366 | bt_dev_dbg(hdev, "QCA setup on UART"); |
83e81961 BYTK |
367 | |
368 | config.user_baud_rate = baudrate; | |
369 | ||
83e81961 BYTK |
370 | /* Download rampatch file */ |
371 | config.type = TLV_TYPE_PATCH; | |
523760b7 | 372 | if (qca_is_wcn399x(soc_type)) { |
4219d468 BG |
373 | /* Firmware files to download are based on ROM version. |
374 | * ROM version is derived from last two bytes of soc_ver. | |
375 | */ | |
376 | rom_ver = ((soc_ver & 0x00000f00) >> 0x04) | | |
377 | (soc_ver & 0x0000000f); | |
378 | snprintf(config.fwname, sizeof(config.fwname), | |
379 | "qca/crbtfw%02x.tlv", rom_ver); | |
380 | } else { | |
381 | snprintf(config.fwname, sizeof(config.fwname), | |
382 | "qca/rampatch_%08x.bin", soc_ver); | |
383 | } | |
384 | ||
ba493d4f | 385 | err = qca_download_firmware(hdev, &config); |
83e81961 | 386 | if (err < 0) { |
ba493d4f | 387 | bt_dev_err(hdev, "QCA Failed to download patch (%d)", err); |
83e81961 BYTK |
388 | return err; |
389 | } | |
390 | ||
8059ba0b MK |
391 | /* Give the controller some time to get ready to receive the NVM */ |
392 | msleep(10); | |
393 | ||
83e81961 BYTK |
394 | /* Download NVM configuration */ |
395 | config.type = TLV_TYPE_NVM; | |
99c905c6 RL |
396 | if (firmware_name) |
397 | snprintf(config.fwname, sizeof(config.fwname), | |
398 | "qca/%s", firmware_name); | |
399 | else if (qca_is_wcn399x(soc_type)) | |
4219d468 BG |
400 | snprintf(config.fwname, sizeof(config.fwname), |
401 | "qca/crnv%02x.bin", rom_ver); | |
402 | else | |
403 | snprintf(config.fwname, sizeof(config.fwname), | |
404 | "qca/nvm_%08x.bin", soc_ver); | |
405 | ||
ba493d4f | 406 | err = qca_download_firmware(hdev, &config); |
83e81961 | 407 | if (err < 0) { |
ba493d4f | 408 | bt_dev_err(hdev, "QCA Failed to download NVM (%d)", err); |
83e81961 BYTK |
409 | return err; |
410 | } | |
411 | ||
412 | /* Perform HCI reset */ | |
ba493d4f | 413 | err = qca_send_reset(hdev); |
83e81961 | 414 | if (err < 0) { |
ba493d4f | 415 | bt_dev_err(hdev, "QCA Failed to run HCI_RESET (%d)", err); |
83e81961 BYTK |
416 | return err; |
417 | } | |
418 | ||
ba493d4f | 419 | bt_dev_info(hdev, "QCA setup on UART is completed"); |
83e81961 BYTK |
420 | |
421 | return 0; | |
422 | } | |
ba493d4f | 423 | EXPORT_SYMBOL_GPL(qca_uart_setup); |
83e81961 | 424 | |
5c0a1001 BG |
425 | int qca_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr) |
426 | { | |
427 | struct sk_buff *skb; | |
428 | int err; | |
429 | ||
430 | skb = __hci_cmd_sync_ev(hdev, EDL_WRITE_BD_ADDR_OPCODE, 6, bdaddr, | |
431 | HCI_EV_VENDOR, HCI_INIT_TIMEOUT); | |
432 | if (IS_ERR(skb)) { | |
433 | err = PTR_ERR(skb); | |
434 | bt_dev_err(hdev, "QCA Change address cmd failed (%d)", err); | |
435 | return err; | |
436 | } | |
437 | ||
438 | kfree_skb(skb); | |
439 | ||
440 | return 0; | |
441 | } | |
442 | EXPORT_SYMBOL_GPL(qca_set_bdaddr); | |
443 | ||
523760b7 | 444 | |
83e81961 BYTK |
445 | MODULE_AUTHOR("Ben Young Tae Kim <ytkim@qca.qualcomm.com>"); |
446 | MODULE_DESCRIPTION("Bluetooth support for Qualcomm Atheros family ver " VERSION); | |
447 | MODULE_VERSION(VERSION); | |
448 | MODULE_LICENSE("GPL"); |