Ben Young Tae Kim | 83e8196 | 2015-08-10 14:24:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Bluetooth supports for Qualcomm Atheros chips |
| 3 | * |
Balvinder Singh | c6f0381 | 2017-11-06 11:55:30 +0530 | [diff] [blame] | 4 | * Copyright (c) 2017 The Linux Foundation. All rights reserved. |
Ben Young Tae Kim | 83e8196 | 2015-08-10 14:24:12 -0700 | [diff] [blame] | 5 | * |
| 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 |
| 9 | * |
| 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. |
| 14 | * |
| 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 |
| 18 | * |
| 19 | */ |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/firmware.h> |
| 22 | |
| 23 | #include <net/bluetooth/bluetooth.h> |
| 24 | #include <net/bluetooth/hci_core.h> |
| 25 | |
| 26 | #include "btqca.h" |
| 27 | |
| 28 | #define VERSION "0.1" |
| 29 | |
Balvinder Singh | c6f0381 | 2017-11-06 11:55:30 +0530 | [diff] [blame] | 30 | #define MAX_PATCH_FILE_SIZE (100*1024) |
| 31 | #define MAX_NVM_FILE_SIZE (10*1024) |
| 32 | |
Ben Young Tae Kim | 83e8196 | 2015-08-10 14:24:12 -0700 | [diff] [blame] | 33 | static int rome_patch_ver_req(struct hci_dev *hdev, u32 *rome_version) |
| 34 | { |
| 35 | struct sk_buff *skb; |
| 36 | struct edl_event_hdr *edl; |
| 37 | struct rome_version *ver; |
| 38 | char cmd; |
| 39 | int err = 0; |
| 40 | |
| 41 | BT_DBG("%s: ROME Patch Version Request", hdev->name); |
| 42 | |
| 43 | cmd = EDL_PATCH_VER_REQ_CMD; |
| 44 | skb = __hci_cmd_sync_ev(hdev, EDL_PATCH_CMD_OPCODE, EDL_PATCH_CMD_LEN, |
| 45 | &cmd, HCI_VENDOR_PKT, HCI_INIT_TIMEOUT); |
| 46 | if (IS_ERR(skb)) { |
| 47 | err = PTR_ERR(skb); |
| 48 | BT_ERR("%s: Failed to read version of ROME (%d)", hdev->name, |
| 49 | err); |
| 50 | return err; |
| 51 | } |
| 52 | |
| 53 | if (skb->len != sizeof(*edl) + sizeof(*ver)) { |
| 54 | BT_ERR("%s: Version size mismatch len %d", hdev->name, |
| 55 | skb->len); |
| 56 | err = -EILSEQ; |
| 57 | goto out; |
| 58 | } |
| 59 | |
| 60 | edl = (struct edl_event_hdr *)(skb->data); |
Colin Ian King | 0676cab | 2016-09-06 13:15:49 +0100 | [diff] [blame] | 61 | if (!edl) { |
| 62 | BT_ERR("%s: TLV with no header", hdev->name); |
Ben Young Tae Kim | 83e8196 | 2015-08-10 14:24:12 -0700 | [diff] [blame] | 63 | err = -EILSEQ; |
| 64 | goto out; |
| 65 | } |
| 66 | |
| 67 | if (edl->cresp != EDL_CMD_REQ_RES_EVT || |
| 68 | edl->rtype != EDL_APP_VER_RES_EVT) { |
| 69 | BT_ERR("%s: Wrong packet received %d %d", hdev->name, |
| 70 | edl->cresp, edl->rtype); |
| 71 | err = -EIO; |
| 72 | goto out; |
| 73 | } |
| 74 | |
| 75 | ver = (struct rome_version *)(edl->data); |
| 76 | |
| 77 | BT_DBG("%s: Product:0x%08x", hdev->name, le32_to_cpu(ver->product_id)); |
| 78 | BT_DBG("%s: Patch :0x%08x", hdev->name, le16_to_cpu(ver->patch_ver)); |
| 79 | BT_DBG("%s: ROM :0x%08x", hdev->name, le16_to_cpu(ver->rome_ver)); |
| 80 | BT_DBG("%s: SOC :0x%08x", hdev->name, le32_to_cpu(ver->soc_id)); |
| 81 | |
| 82 | /* ROME chipset version can be decided by patch and SoC |
| 83 | * version, combination with upper 2 bytes from SoC |
| 84 | * and lower 2 bytes from patch will be used. |
| 85 | */ |
| 86 | *rome_version = (le32_to_cpu(ver->soc_id) << 16) | |
| 87 | (le16_to_cpu(ver->rome_ver) & 0x0000ffff); |
| 88 | |
| 89 | out: |
| 90 | kfree_skb(skb); |
| 91 | |
| 92 | return err; |
| 93 | } |
| 94 | |
| 95 | static int rome_reset(struct hci_dev *hdev) |
| 96 | { |
| 97 | struct sk_buff *skb; |
| 98 | int err; |
| 99 | |
| 100 | BT_DBG("%s: ROME HCI_RESET", hdev->name); |
| 101 | |
| 102 | skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT); |
| 103 | if (IS_ERR(skb)) { |
| 104 | err = PTR_ERR(skb); |
| 105 | BT_ERR("%s: Reset failed (%d)", hdev->name, err); |
| 106 | return err; |
| 107 | } |
| 108 | |
| 109 | kfree_skb(skb); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static void rome_tlv_check_data(struct rome_config *config, |
| 115 | const struct firmware *fw) |
| 116 | { |
| 117 | const u8 *data; |
| 118 | u32 type_len; |
| 119 | u16 tag_id, tag_len; |
| 120 | int idx, length; |
| 121 | struct tlv_type_hdr *tlv; |
| 122 | struct tlv_type_patch *tlv_patch; |
| 123 | struct tlv_type_nvm *tlv_nvm; |
| 124 | |
| 125 | tlv = (struct tlv_type_hdr *)fw->data; |
| 126 | |
| 127 | type_len = le32_to_cpu(tlv->type_len); |
| 128 | length = (type_len >> 8) & 0x00ffffff; |
| 129 | |
| 130 | BT_DBG("TLV Type\t\t : 0x%x", type_len & 0x000000ff); |
| 131 | BT_DBG("Length\t\t : %d bytes", length); |
| 132 | |
| 133 | switch (config->type) { |
| 134 | case TLV_TYPE_PATCH: |
| 135 | tlv_patch = (struct tlv_type_patch *)tlv->data; |
| 136 | BT_DBG("Total Length\t\t : %d bytes", |
| 137 | le32_to_cpu(tlv_patch->total_size)); |
| 138 | BT_DBG("Patch Data Length\t : %d bytes", |
| 139 | le32_to_cpu(tlv_patch->data_length)); |
| 140 | BT_DBG("Signing Format Version : 0x%x", |
| 141 | tlv_patch->format_version); |
| 142 | BT_DBG("Signature Algorithm\t : 0x%x", |
| 143 | tlv_patch->signature); |
| 144 | BT_DBG("Reserved\t\t : 0x%x", |
| 145 | le16_to_cpu(tlv_patch->reserved1)); |
| 146 | BT_DBG("Product ID\t\t : 0x%04x", |
| 147 | le16_to_cpu(tlv_patch->product_id)); |
| 148 | BT_DBG("Rom Build Version\t : 0x%04x", |
| 149 | le16_to_cpu(tlv_patch->rom_build)); |
| 150 | BT_DBG("Patch Version\t\t : 0x%04x", |
| 151 | le16_to_cpu(tlv_patch->patch_version)); |
| 152 | BT_DBG("Reserved\t\t : 0x%x", |
| 153 | le16_to_cpu(tlv_patch->reserved2)); |
| 154 | BT_DBG("Patch Entry Address\t : 0x%x", |
| 155 | le32_to_cpu(tlv_patch->entry)); |
| 156 | break; |
| 157 | |
| 158 | case TLV_TYPE_NVM: |
| 159 | idx = 0; |
| 160 | data = tlv->data; |
| 161 | while (idx < length) { |
| 162 | tlv_nvm = (struct tlv_type_nvm *)(data + idx); |
| 163 | |
| 164 | tag_id = le16_to_cpu(tlv_nvm->tag_id); |
| 165 | tag_len = le16_to_cpu(tlv_nvm->tag_len); |
| 166 | |
| 167 | /* Update NVM tags as needed */ |
| 168 | switch (tag_id) { |
| 169 | case EDL_TAG_ID_HCI: |
| 170 | /* HCI transport layer parameters |
| 171 | * enabling software inband sleep |
| 172 | * onto controller side. |
| 173 | */ |
| 174 | tlv_nvm->data[0] |= 0x80; |
| 175 | |
| 176 | /* UART Baud Rate */ |
| 177 | tlv_nvm->data[2] = config->user_baud_rate; |
| 178 | |
| 179 | break; |
| 180 | |
| 181 | case EDL_TAG_ID_DEEP_SLEEP: |
| 182 | /* Sleep enable mask |
| 183 | * enabling deep sleep feature on controller. |
| 184 | */ |
| 185 | tlv_nvm->data[0] |= 0x01; |
| 186 | |
| 187 | break; |
| 188 | } |
| 189 | |
| 190 | idx += (sizeof(u16) + sizeof(u16) + 8 + tag_len); |
| 191 | } |
| 192 | break; |
| 193 | |
| 194 | default: |
| 195 | BT_ERR("Unknown TLV type %d", config->type); |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | static int rome_tlv_send_segment(struct hci_dev *hdev, int idx, int seg_size, |
| 201 | const u8 *data) |
| 202 | { |
| 203 | struct sk_buff *skb; |
| 204 | struct edl_event_hdr *edl; |
| 205 | struct tlv_seg_resp *tlv_resp; |
| 206 | u8 cmd[MAX_SIZE_PER_TLV_SEGMENT + 2]; |
| 207 | int err = 0; |
| 208 | |
| 209 | BT_DBG("%s: Download segment #%d size %d", hdev->name, idx, seg_size); |
| 210 | |
| 211 | cmd[0] = EDL_PATCH_TLV_REQ_CMD; |
| 212 | cmd[1] = seg_size; |
| 213 | memcpy(cmd + 2, data, seg_size); |
| 214 | |
| 215 | skb = __hci_cmd_sync_ev(hdev, EDL_PATCH_CMD_OPCODE, seg_size + 2, cmd, |
| 216 | HCI_VENDOR_PKT, HCI_INIT_TIMEOUT); |
| 217 | if (IS_ERR(skb)) { |
| 218 | err = PTR_ERR(skb); |
| 219 | BT_ERR("%s: Failed to send TLV segment (%d)", hdev->name, err); |
| 220 | return err; |
| 221 | } |
| 222 | |
| 223 | if (skb->len != sizeof(*edl) + sizeof(*tlv_resp)) { |
| 224 | BT_ERR("%s: TLV response size mismatch", hdev->name); |
| 225 | err = -EILSEQ; |
| 226 | goto out; |
| 227 | } |
| 228 | |
| 229 | edl = (struct edl_event_hdr *)(skb->data); |
Colin Ian King | 0676cab | 2016-09-06 13:15:49 +0100 | [diff] [blame] | 230 | if (!edl) { |
| 231 | BT_ERR("%s: TLV with no header", hdev->name); |
Ben Young Tae Kim | 83e8196 | 2015-08-10 14:24:12 -0700 | [diff] [blame] | 232 | err = -EILSEQ; |
| 233 | goto out; |
| 234 | } |
| 235 | |
| 236 | tlv_resp = (struct tlv_seg_resp *)(edl->data); |
| 237 | |
| 238 | if (edl->cresp != EDL_CMD_REQ_RES_EVT || |
| 239 | edl->rtype != EDL_TVL_DNLD_RES_EVT || tlv_resp->result != 0x00) { |
| 240 | BT_ERR("%s: TLV with error stat 0x%x rtype 0x%x (0x%x)", |
| 241 | hdev->name, edl->cresp, edl->rtype, tlv_resp->result); |
| 242 | err = -EIO; |
| 243 | } |
| 244 | |
| 245 | out: |
| 246 | kfree_skb(skb); |
| 247 | |
| 248 | return err; |
| 249 | } |
| 250 | |
| 251 | static int rome_tlv_download_request(struct hci_dev *hdev, |
| 252 | const struct firmware *fw) |
| 253 | { |
| 254 | const u8 *buffer, *data; |
| 255 | int total_segment, remain_size; |
| 256 | int ret, i; |
| 257 | |
| 258 | if (!fw || !fw->data) |
| 259 | return -EINVAL; |
| 260 | |
| 261 | total_segment = fw->size / MAX_SIZE_PER_TLV_SEGMENT; |
| 262 | remain_size = fw->size % MAX_SIZE_PER_TLV_SEGMENT; |
| 263 | |
| 264 | BT_DBG("%s: Total segment num %d remain size %d total size %zu", |
| 265 | hdev->name, total_segment, remain_size, fw->size); |
| 266 | |
| 267 | data = fw->data; |
| 268 | for (i = 0; i < total_segment; i++) { |
| 269 | buffer = data + i * MAX_SIZE_PER_TLV_SEGMENT; |
| 270 | ret = rome_tlv_send_segment(hdev, i, MAX_SIZE_PER_TLV_SEGMENT, |
| 271 | buffer); |
| 272 | if (ret < 0) |
| 273 | return -EIO; |
| 274 | } |
| 275 | |
| 276 | if (remain_size) { |
| 277 | buffer = data + total_segment * MAX_SIZE_PER_TLV_SEGMENT; |
| 278 | ret = rome_tlv_send_segment(hdev, total_segment, remain_size, |
| 279 | buffer); |
| 280 | if (ret < 0) |
| 281 | return -EIO; |
| 282 | } |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static int rome_download_firmware(struct hci_dev *hdev, |
| 288 | struct rome_config *config) |
| 289 | { |
| 290 | const struct firmware *fw; |
Balvinder Singh | c6f0381 | 2017-11-06 11:55:30 +0530 | [diff] [blame] | 291 | u32 type_len, length; |
| 292 | struct tlv_type_hdr *tlv; |
Ben Young Tae Kim | 83e8196 | 2015-08-10 14:24:12 -0700 | [diff] [blame] | 293 | int ret; |
| 294 | |
Balvinder Singh | c6f0381 | 2017-11-06 11:55:30 +0530 | [diff] [blame] | 295 | BT_INFO("%s: ROME Downloading file: %s", hdev->name, config->fwname); |
Ben Young Tae Kim | 83e8196 | 2015-08-10 14:24:12 -0700 | [diff] [blame] | 296 | ret = request_firmware(&fw, config->fwname, &hdev->dev); |
Balvinder Singh | c6f0381 | 2017-11-06 11:55:30 +0530 | [diff] [blame] | 297 | |
| 298 | if (ret || !fw || !fw->data || fw->size <= 0) { |
| 299 | BT_ERR("Failed to request file: err = (%d)", ret); |
| 300 | ret = ret ? ret : -EINVAL; |
Ben Young Tae Kim | 83e8196 | 2015-08-10 14:24:12 -0700 | [diff] [blame] | 301 | return ret; |
| 302 | } |
| 303 | |
Balvinder Singh | c6f0381 | 2017-11-06 11:55:30 +0530 | [diff] [blame] | 304 | if (config->type != TLV_TYPE_NVM && |
| 305 | config->type != TLV_TYPE_PATCH) { |
| 306 | ret = -EINVAL; |
| 307 | BT_ERR("TLV_NVM dload: wrong config type selected"); |
| 308 | goto exit; |
Ben Young Tae Kim | 83e8196 | 2015-08-10 14:24:12 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Balvinder Singh | c6f0381 | 2017-11-06 11:55:30 +0530 | [diff] [blame] | 311 | if (config->type == TLV_TYPE_PATCH && |
| 312 | (fw->size > MAX_PATCH_FILE_SIZE)) { |
| 313 | ret = -EINVAL; |
| 314 | BT_ERR("TLV_PATCH dload: wrong patch file sizes"); |
| 315 | goto exit; |
| 316 | } else if (config->type == TLV_TYPE_NVM && |
| 317 | (fw->size > MAX_NVM_FILE_SIZE)) { |
| 318 | ret = -EINVAL; |
| 319 | BT_ERR("TLV_NVM dload: wrong NVM file sizes"); |
| 320 | goto exit; |
| 321 | } |
Ben Young Tae Kim | 83e8196 | 2015-08-10 14:24:12 -0700 | [diff] [blame] | 322 | |
Balvinder Singh | c6f0381 | 2017-11-06 11:55:30 +0530 | [diff] [blame] | 323 | if (fw->size < sizeof(struct tlv_type_hdr)) { |
| 324 | ret = -EINVAL; |
| 325 | BT_ERR("Firware size smaller to fit minimum value"); |
| 326 | goto exit; |
| 327 | } |
| 328 | |
| 329 | tlv = (struct tlv_type_hdr *)fw->data; |
| 330 | type_len = le32_to_cpu(tlv->type_len); |
| 331 | length = (type_len >> 8) & 0x00ffffff; |
| 332 | |
| 333 | if (fw->size - 4 != length) { |
| 334 | ret = -EINVAL; |
| 335 | BT_ERR("Requested size not matching size in header"); |
| 336 | goto exit; |
| 337 | } |
| 338 | |
| 339 | rome_tlv_check_data(config, fw); |
| 340 | ret = rome_tlv_download_request(hdev, fw); |
| 341 | |
| 342 | if (ret) { |
| 343 | BT_ERR("Failed to download FW: error = (%d)", ret); |
| 344 | } |
| 345 | |
| 346 | exit: |
| 347 | release_firmware(fw); |
Ben Young Tae Kim | 83e8196 | 2015-08-10 14:24:12 -0700 | [diff] [blame] | 348 | return ret; |
| 349 | } |
| 350 | |
| 351 | int qca_set_bdaddr_rome(struct hci_dev *hdev, const bdaddr_t *bdaddr) |
| 352 | { |
| 353 | struct sk_buff *skb; |
| 354 | u8 cmd[9]; |
| 355 | int err; |
| 356 | |
| 357 | cmd[0] = EDL_NVM_ACCESS_SET_REQ_CMD; |
Balvinder Singh | c6f0381 | 2017-11-06 11:55:30 +0530 | [diff] [blame] | 358 | /* Set the TAG ID of 0x02 for NVM set and size of tag */ |
| 359 | cmd[1] = 0x02; |
| 360 | cmd[2] = sizeof(bdaddr_t); |
Ben Young Tae Kim | 83e8196 | 2015-08-10 14:24:12 -0700 | [diff] [blame] | 361 | memcpy(cmd + 3, bdaddr, sizeof(bdaddr_t)); |
| 362 | skb = __hci_cmd_sync_ev(hdev, EDL_NVM_ACCESS_OPCODE, sizeof(cmd), cmd, |
| 363 | HCI_VENDOR_PKT, HCI_INIT_TIMEOUT); |
| 364 | if (IS_ERR(skb)) { |
| 365 | err = PTR_ERR(skb); |
| 366 | BT_ERR("%s: Change address command failed (%d)", |
| 367 | hdev->name, err); |
| 368 | return err; |
| 369 | } |
| 370 | |
| 371 | kfree_skb(skb); |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | EXPORT_SYMBOL_GPL(qca_set_bdaddr_rome); |
| 376 | |
| 377 | int qca_uart_setup_rome(struct hci_dev *hdev, uint8_t baudrate) |
| 378 | { |
| 379 | u32 rome_ver = 0; |
| 380 | struct rome_config config; |
| 381 | int err; |
| 382 | |
| 383 | BT_DBG("%s: ROME setup on UART", hdev->name); |
| 384 | |
| 385 | config.user_baud_rate = baudrate; |
| 386 | |
| 387 | /* Get ROME version information */ |
| 388 | err = rome_patch_ver_req(hdev, &rome_ver); |
| 389 | if (err < 0 || rome_ver == 0) { |
| 390 | BT_ERR("%s: Failed to get version 0x%x", hdev->name, err); |
| 391 | return err; |
| 392 | } |
| 393 | |
| 394 | BT_INFO("%s: ROME controller version 0x%08x", hdev->name, rome_ver); |
| 395 | |
| 396 | /* Download rampatch file */ |
| 397 | config.type = TLV_TYPE_PATCH; |
| 398 | snprintf(config.fwname, sizeof(config.fwname), "qca/rampatch_%08x.bin", |
| 399 | rome_ver); |
| 400 | err = rome_download_firmware(hdev, &config); |
| 401 | if (err < 0) { |
| 402 | BT_ERR("%s: Failed to download patch (%d)", hdev->name, err); |
| 403 | return err; |
| 404 | } |
| 405 | |
| 406 | /* Download NVM configuration */ |
| 407 | config.type = TLV_TYPE_NVM; |
| 408 | snprintf(config.fwname, sizeof(config.fwname), "qca/nvm_%08x.bin", |
| 409 | rome_ver); |
| 410 | err = rome_download_firmware(hdev, &config); |
| 411 | if (err < 0) { |
| 412 | BT_ERR("%s: Failed to download NVM (%d)", hdev->name, err); |
| 413 | return err; |
| 414 | } |
| 415 | |
| 416 | /* Perform HCI reset */ |
| 417 | err = rome_reset(hdev); |
| 418 | if (err < 0) { |
| 419 | BT_ERR("%s: Failed to run HCI_RESET (%d)", hdev->name, err); |
| 420 | return err; |
| 421 | } |
| 422 | |
| 423 | BT_INFO("%s: ROME setup on UART is completed", hdev->name); |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | EXPORT_SYMBOL_GPL(qca_uart_setup_rome); |
| 428 | |
| 429 | MODULE_AUTHOR("Ben Young Tae Kim <ytkim@qca.qualcomm.com>"); |
| 430 | MODULE_DESCRIPTION("Bluetooth support for Qualcomm Atheros family ver " VERSION); |
| 431 | MODULE_VERSION(VERSION); |
| 432 | MODULE_LICENSE("GPL"); |