2 * Copyright (c) 2005-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <linux/module.h>
19 #include <linux/firmware.h>
33 unsigned int ath10k_debug_mask;
34 static bool uart_print;
37 module_param_named(debug_mask, ath10k_debug_mask, uint, 0644);
38 module_param(uart_print, bool, 0644);
39 module_param(skip_otp, bool, 0644);
41 MODULE_PARM_DESC(debug_mask, "Debugging mask");
42 MODULE_PARM_DESC(uart_print, "Uart target debugging");
43 MODULE_PARM_DESC(skip_otp, "Skip otp failure for calibration in testmode");
45 static const struct ath10k_hw_params ath10k_hw_params_list[] = {
47 .id = QCA988X_HW_2_0_VERSION,
48 .name = "qca988x hw2.0",
49 .patch_load_addr = QCA988X_HW_2_0_PATCH_LOAD_ADDR,
51 .has_shifted_cc_wraparound = true,
53 .dir = QCA988X_HW_2_0_FW_DIR,
54 .fw = QCA988X_HW_2_0_FW_FILE,
55 .otp = QCA988X_HW_2_0_OTP_FILE,
56 .board = QCA988X_HW_2_0_BOARD_DATA_FILE,
57 .board_size = QCA988X_BOARD_DATA_SZ,
58 .board_ext_size = QCA988X_BOARD_EXT_DATA_SZ,
62 .id = QCA6174_HW_2_1_VERSION,
63 .name = "qca6174 hw2.1",
64 .patch_load_addr = QCA6174_HW_2_1_PATCH_LOAD_ADDR,
67 .dir = QCA6174_HW_2_1_FW_DIR,
68 .fw = QCA6174_HW_2_1_FW_FILE,
69 .otp = QCA6174_HW_2_1_OTP_FILE,
70 .board = QCA6174_HW_2_1_BOARD_DATA_FILE,
71 .board_size = QCA6174_BOARD_DATA_SZ,
72 .board_ext_size = QCA6174_BOARD_EXT_DATA_SZ,
76 .id = QCA6174_HW_3_0_VERSION,
77 .name = "qca6174 hw3.0",
78 .patch_load_addr = QCA6174_HW_3_0_PATCH_LOAD_ADDR,
81 .dir = QCA6174_HW_3_0_FW_DIR,
82 .fw = QCA6174_HW_3_0_FW_FILE,
83 .otp = QCA6174_HW_3_0_OTP_FILE,
84 .board = QCA6174_HW_3_0_BOARD_DATA_FILE,
85 .board_size = QCA6174_BOARD_DATA_SZ,
86 .board_ext_size = QCA6174_BOARD_EXT_DATA_SZ,
90 .id = QCA6174_HW_3_2_VERSION,
91 .name = "qca6174 hw3.2",
92 .patch_load_addr = QCA6174_HW_3_0_PATCH_LOAD_ADDR,
95 /* uses same binaries as hw3.0 */
96 .dir = QCA6174_HW_3_0_FW_DIR,
97 .fw = QCA6174_HW_3_0_FW_FILE,
98 .otp = QCA6174_HW_3_0_OTP_FILE,
99 .board = QCA6174_HW_3_0_BOARD_DATA_FILE,
100 .board_size = QCA6174_BOARD_DATA_SZ,
101 .board_ext_size = QCA6174_BOARD_EXT_DATA_SZ,
106 static void ath10k_send_suspend_complete(struct ath10k *ar)
108 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot suspend complete\n");
110 complete(&ar->target_suspend);
113 static int ath10k_init_configure_target(struct ath10k *ar)
118 /* tell target which HTC version it is used*/
119 ret = ath10k_bmi_write32(ar, hi_app_host_interest,
120 HTC_PROTOCOL_VERSION);
122 ath10k_err(ar, "settings HTC version failed\n");
126 /* set the firmware mode to STA/IBSS/AP */
127 ret = ath10k_bmi_read32(ar, hi_option_flag, ¶m_host);
129 ath10k_err(ar, "setting firmware mode (1/2) failed\n");
133 /* TODO following parameters need to be re-visited. */
135 param_host |= (1 << HI_OPTION_NUM_DEV_SHIFT);
137 /* FIXME: Why FW_MODE_AP ??.*/
138 param_host |= (HI_OPTION_FW_MODE_AP << HI_OPTION_FW_MODE_SHIFT);
139 /* mac_addr_method */
140 param_host |= (1 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
141 /* firmware_bridge */
142 param_host |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
144 param_host |= (0 << HI_OPTION_FW_SUBMODE_SHIFT);
146 ret = ath10k_bmi_write32(ar, hi_option_flag, param_host);
148 ath10k_err(ar, "setting firmware mode (2/2) failed\n");
152 /* We do all byte-swapping on the host */
153 ret = ath10k_bmi_write32(ar, hi_be, 0);
155 ath10k_err(ar, "setting host CPU BE mode failed\n");
159 /* FW descriptor/Data swap flags */
160 ret = ath10k_bmi_write32(ar, hi_fw_swap, 0);
163 ath10k_err(ar, "setting FW data/desc swap flags failed\n");
170 static const struct firmware *ath10k_fetch_fw_file(struct ath10k *ar,
175 const struct firmware *fw;
179 return ERR_PTR(-ENOENT);
184 snprintf(filename, sizeof(filename), "%s/%s", dir, file);
185 ret = request_firmware(&fw, filename, ar->dev);
192 static int ath10k_push_board_ext_data(struct ath10k *ar, const void *data,
195 u32 board_data_size = ar->hw_params.fw.board_size;
196 u32 board_ext_data_size = ar->hw_params.fw.board_ext_size;
197 u32 board_ext_data_addr;
200 ret = ath10k_bmi_read32(ar, hi_board_ext_data, &board_ext_data_addr);
202 ath10k_err(ar, "could not read board ext data addr (%d)\n",
207 ath10k_dbg(ar, ATH10K_DBG_BOOT,
208 "boot push board extended data addr 0x%x\n",
209 board_ext_data_addr);
211 if (board_ext_data_addr == 0)
214 if (data_len != (board_data_size + board_ext_data_size)) {
215 ath10k_err(ar, "invalid board (ext) data sizes %zu != %d+%d\n",
216 data_len, board_data_size, board_ext_data_size);
220 ret = ath10k_bmi_write_memory(ar, board_ext_data_addr,
221 data + board_data_size,
222 board_ext_data_size);
224 ath10k_err(ar, "could not write board ext data (%d)\n", ret);
228 ret = ath10k_bmi_write32(ar, hi_board_ext_data_config,
229 (board_ext_data_size << 16) | 1);
231 ath10k_err(ar, "could not write board ext data bit (%d)\n",
239 static int ath10k_download_board_data(struct ath10k *ar, const void *data,
242 u32 board_data_size = ar->hw_params.fw.board_size;
246 ret = ath10k_push_board_ext_data(ar, data, data_len);
248 ath10k_err(ar, "could not push board ext data (%d)\n", ret);
252 ret = ath10k_bmi_read32(ar, hi_board_data, &address);
254 ath10k_err(ar, "could not read board data addr (%d)\n", ret);
258 ret = ath10k_bmi_write_memory(ar, address, data,
259 min_t(u32, board_data_size,
262 ath10k_err(ar, "could not write board data (%d)\n", ret);
266 ret = ath10k_bmi_write32(ar, hi_board_data_initialized, 1);
268 ath10k_err(ar, "could not write board data bit (%d)\n", ret);
276 static int ath10k_download_cal_file(struct ath10k *ar)
283 if (IS_ERR(ar->cal_file))
284 return PTR_ERR(ar->cal_file);
286 ret = ath10k_download_board_data(ar, ar->cal_file->data,
289 ath10k_err(ar, "failed to download cal_file data: %d\n", ret);
293 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cal file downloaded\n");
298 static int ath10k_download_cal_dt(struct ath10k *ar)
300 struct device_node *node;
305 node = ar->dev->of_node;
307 /* Device Tree is optional, don't print any warnings if
308 * there's no node for ath10k.
312 if (!of_get_property(node, "qcom,ath10k-calibration-data",
314 /* The calibration data node is optional */
318 if (data_len != QCA988X_CAL_DATA_LEN) {
319 ath10k_warn(ar, "invalid calibration data length in DT: %d\n",
325 data = kmalloc(data_len, GFP_KERNEL);
331 ret = of_property_read_u8_array(node, "qcom,ath10k-calibration-data",
334 ath10k_warn(ar, "failed to read calibration data from DT: %d\n",
339 ret = ath10k_download_board_data(ar, data, data_len);
341 ath10k_warn(ar, "failed to download calibration data from Device Tree: %d\n",
355 static int ath10k_download_and_run_otp(struct ath10k *ar)
357 u32 result, address = ar->hw_params.patch_load_addr;
360 ret = ath10k_download_board_data(ar, ar->board_data, ar->board_len);
362 ath10k_err(ar, "failed to download board data: %d\n", ret);
366 /* OTP is optional */
368 if (!ar->otp_data || !ar->otp_len) {
369 ath10k_warn(ar, "Not running otp, calibration will be incorrect (otp-data %p otp_len %zd)!\n",
370 ar->otp_data, ar->otp_len);
374 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot upload otp to 0x%x len %zd\n",
375 address, ar->otp_len);
377 ret = ath10k_bmi_fast_download(ar, address, ar->otp_data, ar->otp_len);
379 ath10k_err(ar, "could not write otp (%d)\n", ret);
383 ret = ath10k_bmi_execute(ar, address, 0, &result);
385 ath10k_err(ar, "could not execute otp (%d)\n", ret);
389 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot otp execute result %d\n", result);
391 if (!(skip_otp || test_bit(ATH10K_FW_FEATURE_IGNORE_OTP_RESULT,
394 ath10k_err(ar, "otp calibration failed: %d", result);
401 static int ath10k_download_fw(struct ath10k *ar, enum ath10k_firmware_mode mode)
403 u32 address, data_len;
404 const char *mode_name;
408 address = ar->hw_params.patch_load_addr;
411 case ATH10K_FIRMWARE_MODE_NORMAL:
412 data = ar->firmware_data;
413 data_len = ar->firmware_len;
414 mode_name = "normal";
416 case ATH10K_FIRMWARE_MODE_UTF:
417 data = ar->testmode.utf->data;
418 data_len = ar->testmode.utf->size;
422 ath10k_err(ar, "unknown firmware mode: %d\n", mode);
426 ath10k_dbg(ar, ATH10K_DBG_BOOT,
427 "boot uploading firmware image %p len %d mode %s\n",
428 data, data_len, mode_name);
430 ret = ath10k_bmi_fast_download(ar, address, data, data_len);
432 ath10k_err(ar, "failed to download %s firmware: %d\n",
440 static void ath10k_core_free_firmware_files(struct ath10k *ar)
442 if (!IS_ERR(ar->board))
443 release_firmware(ar->board);
445 if (!IS_ERR(ar->otp))
446 release_firmware(ar->otp);
448 if (!IS_ERR(ar->firmware))
449 release_firmware(ar->firmware);
451 if (!IS_ERR(ar->cal_file))
452 release_firmware(ar->cal_file);
455 ar->board_data = NULL;
463 ar->firmware_data = NULL;
464 ar->firmware_len = 0;
469 static int ath10k_fetch_cal_file(struct ath10k *ar)
473 /* cal-<bus>-<id>.bin */
474 scnprintf(filename, sizeof(filename), "cal-%s-%s.bin",
475 ath10k_bus_str(ar->hif.bus), dev_name(ar->dev));
477 ar->cal_file = ath10k_fetch_fw_file(ar, ATH10K_FW_DIR, filename);
478 if (IS_ERR(ar->cal_file))
479 /* calibration file is optional, don't print any warnings */
480 return PTR_ERR(ar->cal_file);
482 ath10k_dbg(ar, ATH10K_DBG_BOOT, "found calibration file %s/%s\n",
483 ATH10K_FW_DIR, filename);
488 static int ath10k_core_fetch_spec_board_file(struct ath10k *ar)
492 scnprintf(filename, sizeof(filename), "board-%s-%s.bin",
493 ath10k_bus_str(ar->hif.bus), ar->spec_board_id);
495 ar->board = ath10k_fetch_fw_file(ar, ar->hw_params.fw.dir, filename);
496 if (IS_ERR(ar->board))
497 return PTR_ERR(ar->board);
499 ar->board_data = ar->board->data;
500 ar->board_len = ar->board->size;
501 ar->spec_board_loaded = true;
506 static int ath10k_core_fetch_generic_board_file(struct ath10k *ar)
508 if (!ar->hw_params.fw.board) {
509 ath10k_err(ar, "failed to find board file fw entry\n");
513 ar->board = ath10k_fetch_fw_file(ar,
514 ar->hw_params.fw.dir,
515 ar->hw_params.fw.board);
516 if (IS_ERR(ar->board))
517 return PTR_ERR(ar->board);
519 ar->board_data = ar->board->data;
520 ar->board_len = ar->board->size;
521 ar->spec_board_loaded = false;
526 static int ath10k_core_fetch_board_file(struct ath10k *ar)
530 if (strlen(ar->spec_board_id) > 0) {
531 ret = ath10k_core_fetch_spec_board_file(ar);
533 ath10k_info(ar, "failed to load spec board file, falling back to generic: %d\n",
538 ath10k_dbg(ar, ATH10K_DBG_BOOT, "found specific board file for %s\n",
544 ret = ath10k_core_fetch_generic_board_file(ar);
546 ath10k_err(ar, "failed to fetch generic board data: %d\n", ret);
553 static int ath10k_core_fetch_firmware_api_1(struct ath10k *ar)
557 if (ar->hw_params.fw.fw == NULL) {
558 ath10k_err(ar, "firmware file not defined\n");
562 ar->firmware = ath10k_fetch_fw_file(ar,
563 ar->hw_params.fw.dir,
564 ar->hw_params.fw.fw);
565 if (IS_ERR(ar->firmware)) {
566 ret = PTR_ERR(ar->firmware);
567 ath10k_err(ar, "could not fetch firmware (%d)\n", ret);
571 ar->firmware_data = ar->firmware->data;
572 ar->firmware_len = ar->firmware->size;
574 /* OTP may be undefined. If so, don't fetch it at all */
575 if (ar->hw_params.fw.otp == NULL)
578 ar->otp = ath10k_fetch_fw_file(ar,
579 ar->hw_params.fw.dir,
580 ar->hw_params.fw.otp);
581 if (IS_ERR(ar->otp)) {
582 ret = PTR_ERR(ar->otp);
583 ath10k_err(ar, "could not fetch otp (%d)\n", ret);
587 ar->otp_data = ar->otp->data;
588 ar->otp_len = ar->otp->size;
593 ath10k_core_free_firmware_files(ar);
597 static int ath10k_core_fetch_firmware_api_n(struct ath10k *ar, const char *name)
599 size_t magic_len, len, ie_len;
600 int ie_id, i, index, bit, ret;
601 struct ath10k_fw_ie *hdr;
603 __le32 *timestamp, *version;
605 /* first fetch the firmware file (firmware-*.bin) */
606 ar->firmware = ath10k_fetch_fw_file(ar, ar->hw_params.fw.dir, name);
607 if (IS_ERR(ar->firmware)) {
608 ath10k_err(ar, "could not fetch firmware file '%s/%s': %ld\n",
609 ar->hw_params.fw.dir, name, PTR_ERR(ar->firmware));
610 return PTR_ERR(ar->firmware);
613 data = ar->firmware->data;
614 len = ar->firmware->size;
616 /* magic also includes the null byte, check that as well */
617 magic_len = strlen(ATH10K_FIRMWARE_MAGIC) + 1;
619 if (len < magic_len) {
620 ath10k_err(ar, "firmware file '%s/%s' too small to contain magic: %zu\n",
621 ar->hw_params.fw.dir, name, len);
626 if (memcmp(data, ATH10K_FIRMWARE_MAGIC, magic_len) != 0) {
627 ath10k_err(ar, "invalid firmware magic\n");
632 /* jump over the padding */
633 magic_len = ALIGN(magic_len, 4);
639 while (len > sizeof(struct ath10k_fw_ie)) {
640 hdr = (struct ath10k_fw_ie *)data;
642 ie_id = le32_to_cpu(hdr->id);
643 ie_len = le32_to_cpu(hdr->len);
646 data += sizeof(*hdr);
649 ath10k_err(ar, "invalid length for FW IE %d (%zu < %zu)\n",
656 case ATH10K_FW_IE_FW_VERSION:
657 if (ie_len > sizeof(ar->hw->wiphy->fw_version) - 1)
660 memcpy(ar->hw->wiphy->fw_version, data, ie_len);
661 ar->hw->wiphy->fw_version[ie_len] = '\0';
663 ath10k_dbg(ar, ATH10K_DBG_BOOT,
664 "found fw version %s\n",
665 ar->hw->wiphy->fw_version);
667 case ATH10K_FW_IE_TIMESTAMP:
668 if (ie_len != sizeof(u32))
671 timestamp = (__le32 *)data;
673 ath10k_dbg(ar, ATH10K_DBG_BOOT, "found fw timestamp %d\n",
674 le32_to_cpup(timestamp));
676 case ATH10K_FW_IE_FEATURES:
677 ath10k_dbg(ar, ATH10K_DBG_BOOT,
678 "found firmware features ie (%zd B)\n",
681 for (i = 0; i < ATH10K_FW_FEATURE_COUNT; i++) {
688 if (data[index] & (1 << bit)) {
689 ath10k_dbg(ar, ATH10K_DBG_BOOT,
690 "Enabling feature bit: %i\n",
692 __set_bit(i, ar->fw_features);
696 ath10k_dbg_dump(ar, ATH10K_DBG_BOOT, "features", "",
698 sizeof(ar->fw_features));
700 case ATH10K_FW_IE_FW_IMAGE:
701 ath10k_dbg(ar, ATH10K_DBG_BOOT,
702 "found fw image ie (%zd B)\n",
705 ar->firmware_data = data;
706 ar->firmware_len = ie_len;
709 case ATH10K_FW_IE_OTP_IMAGE:
710 ath10k_dbg(ar, ATH10K_DBG_BOOT,
711 "found otp image ie (%zd B)\n",
715 ar->otp_len = ie_len;
718 case ATH10K_FW_IE_WMI_OP_VERSION:
719 if (ie_len != sizeof(u32))
722 version = (__le32 *)data;
724 ar->wmi.op_version = le32_to_cpup(version);
726 ath10k_dbg(ar, ATH10K_DBG_BOOT, "found fw ie wmi op version %d\n",
729 case ATH10K_FW_IE_HTT_OP_VERSION:
730 if (ie_len != sizeof(u32))
733 version = (__le32 *)data;
735 ar->htt.op_version = le32_to_cpup(version);
737 ath10k_dbg(ar, ATH10K_DBG_BOOT, "found fw ie htt op version %d\n",
741 ath10k_warn(ar, "Unknown FW IE: %u\n",
742 le32_to_cpu(hdr->id));
746 /* jump over the padding */
747 ie_len = ALIGN(ie_len, 4);
753 if (!ar->firmware_data || !ar->firmware_len) {
754 ath10k_warn(ar, "No ATH10K_FW_IE_FW_IMAGE found from '%s/%s', skipping\n",
755 ar->hw_params.fw.dir, name);
763 ath10k_core_free_firmware_files(ar);
767 static int ath10k_core_fetch_firmware_files(struct ath10k *ar)
771 /* calibration file is optional, don't check for any errors */
772 ath10k_fetch_cal_file(ar);
774 ret = ath10k_core_fetch_board_file(ar);
776 ath10k_err(ar, "failed to fetch board file: %d\n", ret);
781 ath10k_dbg(ar, ATH10K_DBG_BOOT, "trying fw api %d\n", ar->fw_api);
783 ret = ath10k_core_fetch_firmware_api_n(ar, ATH10K_FW_API5_FILE);
788 ath10k_dbg(ar, ATH10K_DBG_BOOT, "trying fw api %d\n", ar->fw_api);
790 ret = ath10k_core_fetch_firmware_api_n(ar, ATH10K_FW_API4_FILE);
795 ath10k_dbg(ar, ATH10K_DBG_BOOT, "trying fw api %d\n", ar->fw_api);
797 ret = ath10k_core_fetch_firmware_api_n(ar, ATH10K_FW_API3_FILE);
802 ath10k_dbg(ar, ATH10K_DBG_BOOT, "trying fw api %d\n", ar->fw_api);
804 ret = ath10k_core_fetch_firmware_api_n(ar, ATH10K_FW_API2_FILE);
809 ath10k_dbg(ar, ATH10K_DBG_BOOT, "trying fw api %d\n", ar->fw_api);
811 ret = ath10k_core_fetch_firmware_api_1(ar);
816 ath10k_dbg(ar, ATH10K_DBG_BOOT, "using fw api %d\n", ar->fw_api);
821 static int ath10k_download_cal_data(struct ath10k *ar)
825 ret = ath10k_download_cal_file(ar);
827 ar->cal_mode = ATH10K_CAL_MODE_FILE;
831 ath10k_dbg(ar, ATH10K_DBG_BOOT,
832 "boot did not find a calibration file, try DT next: %d\n",
835 ret = ath10k_download_cal_dt(ar);
837 ar->cal_mode = ATH10K_CAL_MODE_DT;
841 ath10k_dbg(ar, ATH10K_DBG_BOOT,
842 "boot did not find DT entry, try OTP next: %d\n",
845 ret = ath10k_download_and_run_otp(ar);
847 ath10k_err(ar, "failed to run otp: %d\n", ret);
851 ar->cal_mode = ATH10K_CAL_MODE_OTP;
854 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot using calibration mode %s\n",
855 ath10k_cal_mode_str(ar->cal_mode));
859 static int ath10k_init_uart(struct ath10k *ar)
864 * Explicitly setting UART prints to zero as target turns it on
865 * based on scratch registers.
867 ret = ath10k_bmi_write32(ar, hi_serial_enable, 0);
869 ath10k_warn(ar, "could not disable UART prints (%d)\n", ret);
876 ret = ath10k_bmi_write32(ar, hi_dbg_uart_txpin, ar->hw_params.uart_pin);
878 ath10k_warn(ar, "could not enable UART prints (%d)\n", ret);
882 ret = ath10k_bmi_write32(ar, hi_serial_enable, 1);
884 ath10k_warn(ar, "could not enable UART prints (%d)\n", ret);
888 /* Set the UART baud rate to 19200. */
889 ret = ath10k_bmi_write32(ar, hi_desired_baud_rate, 19200);
891 ath10k_warn(ar, "could not set the baud rate (%d)\n", ret);
895 ath10k_info(ar, "UART prints enabled\n");
899 static int ath10k_init_hw_params(struct ath10k *ar)
901 const struct ath10k_hw_params *uninitialized_var(hw_params);
904 for (i = 0; i < ARRAY_SIZE(ath10k_hw_params_list); i++) {
905 hw_params = &ath10k_hw_params_list[i];
907 if (hw_params->id == ar->target_version)
911 if (i == ARRAY_SIZE(ath10k_hw_params_list)) {
912 ath10k_err(ar, "Unsupported hardware version: 0x%x\n",
917 ar->hw_params = *hw_params;
919 ath10k_dbg(ar, ATH10K_DBG_BOOT, "Hardware name %s version 0x%x\n",
920 ar->hw_params.name, ar->target_version);
925 static void ath10k_core_restart(struct work_struct *work)
927 struct ath10k *ar = container_of(work, struct ath10k, restart_work);
929 set_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags);
931 /* Place a barrier to make sure the compiler doesn't reorder
932 * CRASH_FLUSH and calling other functions.
936 ieee80211_stop_queues(ar->hw);
938 complete_all(&ar->scan.started);
939 complete_all(&ar->scan.completed);
940 complete_all(&ar->scan.on_channel);
941 complete_all(&ar->offchan_tx_completed);
942 complete_all(&ar->install_key_done);
943 complete_all(&ar->vdev_setup_done);
944 complete_all(&ar->thermal.wmi_sync);
945 wake_up(&ar->htt.empty_tx_wq);
946 wake_up(&ar->wmi.tx_credits_wq);
947 wake_up(&ar->peer_mapping_wq);
949 mutex_lock(&ar->conf_mutex);
952 case ATH10K_STATE_ON:
953 ar->state = ATH10K_STATE_RESTARTING;
955 ath10k_scan_finish(ar);
956 ieee80211_restart_hw(ar->hw);
958 case ATH10K_STATE_OFF:
959 /* this can happen if driver is being unloaded
960 * or if the crash happens during FW probing */
961 ath10k_warn(ar, "cannot restart a device that hasn't been started\n");
963 case ATH10K_STATE_RESTARTING:
964 /* hw restart might be requested from multiple places */
966 case ATH10K_STATE_RESTARTED:
967 ar->state = ATH10K_STATE_WEDGED;
969 case ATH10K_STATE_WEDGED:
970 ath10k_warn(ar, "device is wedged, will not restart\n");
972 case ATH10K_STATE_UTF:
973 ath10k_warn(ar, "firmware restart in UTF mode not supported\n");
977 mutex_unlock(&ar->conf_mutex);
980 static int ath10k_core_init_firmware_features(struct ath10k *ar)
982 if (test_bit(ATH10K_FW_FEATURE_WMI_10_2, ar->fw_features) &&
983 !test_bit(ATH10K_FW_FEATURE_WMI_10X, ar->fw_features)) {
984 ath10k_err(ar, "feature bits corrupted: 10.2 feature requires 10.x feature to be set as well");
988 if (ar->wmi.op_version >= ATH10K_FW_WMI_OP_VERSION_MAX) {
989 ath10k_err(ar, "unsupported WMI OP version (max %d): %d\n",
990 ATH10K_FW_WMI_OP_VERSION_MAX, ar->wmi.op_version);
994 /* Backwards compatibility for firmwares without
995 * ATH10K_FW_IE_WMI_OP_VERSION.
997 if (ar->wmi.op_version == ATH10K_FW_WMI_OP_VERSION_UNSET) {
998 if (test_bit(ATH10K_FW_FEATURE_WMI_10X, ar->fw_features)) {
999 if (test_bit(ATH10K_FW_FEATURE_WMI_10_2,
1001 ar->wmi.op_version = ATH10K_FW_WMI_OP_VERSION_10_2;
1003 ar->wmi.op_version = ATH10K_FW_WMI_OP_VERSION_10_1;
1005 ar->wmi.op_version = ATH10K_FW_WMI_OP_VERSION_MAIN;
1009 switch (ar->wmi.op_version) {
1010 case ATH10K_FW_WMI_OP_VERSION_MAIN:
1011 ar->max_num_peers = TARGET_NUM_PEERS;
1012 ar->max_num_stations = TARGET_NUM_STATIONS;
1013 ar->max_num_vdevs = TARGET_NUM_VDEVS;
1014 ar->htt.max_num_pending_tx = TARGET_NUM_MSDU_DESC;
1015 ar->fw_stats_req_mask = WMI_STAT_PDEV | WMI_STAT_VDEV |
1018 case ATH10K_FW_WMI_OP_VERSION_10_1:
1019 case ATH10K_FW_WMI_OP_VERSION_10_2:
1020 case ATH10K_FW_WMI_OP_VERSION_10_2_4:
1021 ar->max_num_peers = TARGET_10X_NUM_PEERS;
1022 ar->max_num_stations = TARGET_10X_NUM_STATIONS;
1023 ar->max_num_vdevs = TARGET_10X_NUM_VDEVS;
1024 ar->htt.max_num_pending_tx = TARGET_10X_NUM_MSDU_DESC;
1025 ar->fw_stats_req_mask = WMI_STAT_PEER;
1027 case ATH10K_FW_WMI_OP_VERSION_TLV:
1028 ar->max_num_peers = TARGET_TLV_NUM_PEERS;
1029 ar->max_num_stations = TARGET_TLV_NUM_STATIONS;
1030 ar->max_num_vdevs = TARGET_TLV_NUM_VDEVS;
1031 ar->max_num_tdls_vdevs = TARGET_TLV_NUM_TDLS_VDEVS;
1032 ar->htt.max_num_pending_tx = TARGET_TLV_NUM_MSDU_DESC;
1033 ar->wow.max_num_patterns = TARGET_TLV_NUM_WOW_PATTERNS;
1034 ar->fw_stats_req_mask = WMI_STAT_PDEV | WMI_STAT_VDEV |
1037 case ATH10K_FW_WMI_OP_VERSION_UNSET:
1038 case ATH10K_FW_WMI_OP_VERSION_MAX:
1043 /* Backwards compatibility for firmwares without
1044 * ATH10K_FW_IE_HTT_OP_VERSION.
1046 if (ar->htt.op_version == ATH10K_FW_HTT_OP_VERSION_UNSET) {
1047 switch (ar->wmi.op_version) {
1048 case ATH10K_FW_WMI_OP_VERSION_MAIN:
1049 ar->htt.op_version = ATH10K_FW_HTT_OP_VERSION_MAIN;
1051 case ATH10K_FW_WMI_OP_VERSION_10_1:
1052 case ATH10K_FW_WMI_OP_VERSION_10_2:
1053 case ATH10K_FW_WMI_OP_VERSION_10_2_4:
1054 ar->htt.op_version = ATH10K_FW_HTT_OP_VERSION_10_1;
1056 case ATH10K_FW_WMI_OP_VERSION_TLV:
1057 ar->htt.op_version = ATH10K_FW_HTT_OP_VERSION_TLV;
1059 case ATH10K_FW_WMI_OP_VERSION_UNSET:
1060 case ATH10K_FW_WMI_OP_VERSION_MAX:
1069 int ath10k_core_start(struct ath10k *ar, enum ath10k_firmware_mode mode)
1073 lockdep_assert_held(&ar->conf_mutex);
1075 clear_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags);
1077 ath10k_bmi_start(ar);
1079 if (ath10k_init_configure_target(ar)) {
1084 status = ath10k_download_cal_data(ar);
1088 /* Some of of qca988x solutions are having global reset issue
1089 * during target initialization. Bypassing PLL setting before
1090 * downloading firmware and letting the SoC run on REF_CLK is
1091 * fixing the problem. Corresponding firmware change is also needed
1092 * to set the clock source once the target is initialized.
1094 if (test_bit(ATH10K_FW_FEATURE_SUPPORTS_SKIP_CLOCK_INIT,
1096 status = ath10k_bmi_write32(ar, hi_skip_clock_init, 1);
1098 ath10k_err(ar, "could not write to skip_clock_init: %d\n",
1104 status = ath10k_download_fw(ar, mode);
1108 status = ath10k_init_uart(ar);
1112 ar->htc.htc_ops.target_send_suspend_complete =
1113 ath10k_send_suspend_complete;
1115 status = ath10k_htc_init(ar);
1117 ath10k_err(ar, "could not init HTC (%d)\n", status);
1121 status = ath10k_bmi_done(ar);
1125 status = ath10k_wmi_attach(ar);
1127 ath10k_err(ar, "WMI attach failed: %d\n", status);
1131 status = ath10k_htt_init(ar);
1133 ath10k_err(ar, "failed to init htt: %d\n", status);
1134 goto err_wmi_detach;
1137 status = ath10k_htt_tx_alloc(&ar->htt);
1139 ath10k_err(ar, "failed to alloc htt tx: %d\n", status);
1140 goto err_wmi_detach;
1143 status = ath10k_htt_rx_alloc(&ar->htt);
1145 ath10k_err(ar, "failed to alloc htt rx: %d\n", status);
1146 goto err_htt_tx_detach;
1149 status = ath10k_hif_start(ar);
1151 ath10k_err(ar, "could not start HIF: %d\n", status);
1152 goto err_htt_rx_detach;
1155 status = ath10k_htc_wait_target(&ar->htc);
1157 ath10k_err(ar, "failed to connect to HTC: %d\n", status);
1161 if (mode == ATH10K_FIRMWARE_MODE_NORMAL) {
1162 status = ath10k_htt_connect(&ar->htt);
1164 ath10k_err(ar, "failed to connect htt (%d)\n", status);
1169 status = ath10k_wmi_connect(ar);
1171 ath10k_err(ar, "could not connect wmi: %d\n", status);
1175 status = ath10k_htc_start(&ar->htc);
1177 ath10k_err(ar, "failed to start htc: %d\n", status);
1181 if (mode == ATH10K_FIRMWARE_MODE_NORMAL) {
1182 status = ath10k_wmi_wait_for_service_ready(ar);
1184 ath10k_warn(ar, "wmi service ready event not received");
1189 ath10k_dbg(ar, ATH10K_DBG_BOOT, "firmware %s booted\n",
1190 ar->hw->wiphy->fw_version);
1192 status = ath10k_wmi_cmd_init(ar);
1194 ath10k_err(ar, "could not send WMI init command (%d)\n",
1199 status = ath10k_wmi_wait_for_unified_ready(ar);
1201 ath10k_err(ar, "wmi unified ready event not received\n");
1205 /* If firmware indicates Full Rx Reorder support it must be used in a
1206 * slightly different manner. Let HTT code know.
1208 ar->htt.rx_ring.in_ord_rx = !!(test_bit(WMI_SERVICE_RX_FULL_REORDER,
1211 status = ath10k_htt_rx_ring_refill(ar);
1213 ath10k_err(ar, "failed to refill htt rx ring: %d\n", status);
1217 /* we don't care about HTT in UTF mode */
1218 if (mode == ATH10K_FIRMWARE_MODE_NORMAL) {
1219 status = ath10k_htt_setup(&ar->htt);
1221 ath10k_err(ar, "failed to setup htt: %d\n", status);
1226 status = ath10k_debug_start(ar);
1230 ar->free_vdev_map = (1LL << ar->max_num_vdevs) - 1;
1232 INIT_LIST_HEAD(&ar->arvifs);
1237 ath10k_hif_stop(ar);
1239 ath10k_htt_rx_free(&ar->htt);
1241 ath10k_htt_tx_free(&ar->htt);
1243 ath10k_wmi_detach(ar);
1247 EXPORT_SYMBOL(ath10k_core_start);
1249 int ath10k_wait_for_suspend(struct ath10k *ar, u32 suspend_opt)
1252 unsigned long time_left;
1254 reinit_completion(&ar->target_suspend);
1256 ret = ath10k_wmi_pdev_suspend_target(ar, suspend_opt);
1258 ath10k_warn(ar, "could not suspend target (%d)\n", ret);
1262 time_left = wait_for_completion_timeout(&ar->target_suspend, 1 * HZ);
1265 ath10k_warn(ar, "suspend timed out - target pause event never came\n");
1272 void ath10k_core_stop(struct ath10k *ar)
1274 lockdep_assert_held(&ar->conf_mutex);
1276 /* try to suspend target */
1277 if (ar->state != ATH10K_STATE_RESTARTING &&
1278 ar->state != ATH10K_STATE_UTF)
1279 ath10k_wait_for_suspend(ar, WMI_PDEV_SUSPEND_AND_DISABLE_INTR);
1281 ath10k_debug_stop(ar);
1282 ath10k_hif_stop(ar);
1283 ath10k_htt_tx_free(&ar->htt);
1284 ath10k_htt_rx_free(&ar->htt);
1285 ath10k_wmi_detach(ar);
1287 EXPORT_SYMBOL(ath10k_core_stop);
1289 /* mac80211 manages fw/hw initialization through start/stop hooks. However in
1290 * order to know what hw capabilities should be advertised to mac80211 it is
1291 * necessary to load the firmware (and tear it down immediately since start
1292 * hook will try to init it again) before registering */
1293 static int ath10k_core_probe_fw(struct ath10k *ar)
1295 struct bmi_target_info target_info;
1298 ret = ath10k_hif_power_up(ar);
1300 ath10k_err(ar, "could not start pci hif (%d)\n", ret);
1304 memset(&target_info, 0, sizeof(target_info));
1305 ret = ath10k_bmi_get_target_info(ar, &target_info);
1307 ath10k_err(ar, "could not get target info (%d)\n", ret);
1308 goto err_power_down;
1311 ar->target_version = target_info.version;
1312 ar->hw->wiphy->hw_version = target_info.version;
1314 ret = ath10k_init_hw_params(ar);
1316 ath10k_err(ar, "could not get hw params (%d)\n", ret);
1317 goto err_power_down;
1320 ret = ath10k_core_fetch_firmware_files(ar);
1322 ath10k_err(ar, "could not fetch firmware files (%d)\n", ret);
1323 goto err_power_down;
1326 ret = ath10k_core_init_firmware_features(ar);
1328 ath10k_err(ar, "fatal problem with firmware features: %d\n",
1330 goto err_free_firmware_files;
1333 mutex_lock(&ar->conf_mutex);
1335 ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL);
1337 ath10k_err(ar, "could not init core (%d)\n", ret);
1341 ath10k_print_driver_info(ar);
1342 ath10k_core_stop(ar);
1344 mutex_unlock(&ar->conf_mutex);
1346 ath10k_hif_power_down(ar);
1350 mutex_unlock(&ar->conf_mutex);
1352 err_free_firmware_files:
1353 ath10k_core_free_firmware_files(ar);
1356 ath10k_hif_power_down(ar);
1361 static void ath10k_core_register_work(struct work_struct *work)
1363 struct ath10k *ar = container_of(work, struct ath10k, register_work);
1366 status = ath10k_core_probe_fw(ar);
1368 ath10k_err(ar, "could not probe fw (%d)\n", status);
1372 status = ath10k_mac_register(ar);
1374 ath10k_err(ar, "could not register to mac80211 (%d)\n", status);
1375 goto err_release_fw;
1378 status = ath10k_debug_register(ar);
1380 ath10k_err(ar, "unable to initialize debugfs\n");
1381 goto err_unregister_mac;
1384 status = ath10k_spectral_create(ar);
1386 ath10k_err(ar, "failed to initialize spectral\n");
1387 goto err_debug_destroy;
1390 status = ath10k_thermal_register(ar);
1392 ath10k_err(ar, "could not register thermal device: %d\n",
1394 goto err_spectral_destroy;
1397 set_bit(ATH10K_FLAG_CORE_REGISTERED, &ar->dev_flags);
1400 err_spectral_destroy:
1401 ath10k_spectral_destroy(ar);
1403 ath10k_debug_destroy(ar);
1405 ath10k_mac_unregister(ar);
1407 ath10k_core_free_firmware_files(ar);
1409 /* TODO: It's probably a good idea to release device from the driver
1410 * but calling device_release_driver() here will cause a deadlock.
1415 int ath10k_core_register(struct ath10k *ar, u32 chip_id)
1417 ar->chip_id = chip_id;
1418 queue_work(ar->workqueue, &ar->register_work);
1422 EXPORT_SYMBOL(ath10k_core_register);
1424 void ath10k_core_unregister(struct ath10k *ar)
1426 cancel_work_sync(&ar->register_work);
1428 if (!test_bit(ATH10K_FLAG_CORE_REGISTERED, &ar->dev_flags))
1431 ath10k_thermal_unregister(ar);
1432 /* Stop spectral before unregistering from mac80211 to remove the
1433 * relayfs debugfs file cleanly. Otherwise the parent debugfs tree
1434 * would be already be free'd recursively, leading to a double free.
1436 ath10k_spectral_destroy(ar);
1438 /* We must unregister from mac80211 before we stop HTC and HIF.
1439 * Otherwise we will fail to submit commands to FW and mac80211 will be
1440 * unhappy about callback failures. */
1441 ath10k_mac_unregister(ar);
1443 ath10k_testmode_destroy(ar);
1445 ath10k_core_free_firmware_files(ar);
1447 ath10k_debug_unregister(ar);
1449 EXPORT_SYMBOL(ath10k_core_unregister);
1451 struct ath10k *ath10k_core_create(size_t priv_size, struct device *dev,
1452 enum ath10k_bus bus,
1453 enum ath10k_hw_rev hw_rev,
1454 const struct ath10k_hif_ops *hif_ops)
1459 ar = ath10k_mac_create(priv_size);
1463 ar->ath_common.priv = ar;
1464 ar->ath_common.hw = ar->hw;
1466 ar->hw_rev = hw_rev;
1467 ar->hif.ops = hif_ops;
1471 case ATH10K_HW_QCA988X:
1472 ar->regs = &qca988x_regs;
1474 case ATH10K_HW_QCA6174:
1475 ar->regs = &qca6174_regs;
1478 ath10k_err(ar, "unsupported core hardware revision %d\n",
1484 init_completion(&ar->scan.started);
1485 init_completion(&ar->scan.completed);
1486 init_completion(&ar->scan.on_channel);
1487 init_completion(&ar->target_suspend);
1488 init_completion(&ar->wow.wakeup_completed);
1490 init_completion(&ar->install_key_done);
1491 init_completion(&ar->vdev_setup_done);
1492 init_completion(&ar->thermal.wmi_sync);
1494 INIT_DELAYED_WORK(&ar->scan.timeout, ath10k_scan_timeout_work);
1496 ar->workqueue = create_singlethread_workqueue("ath10k_wq");
1500 mutex_init(&ar->conf_mutex);
1501 spin_lock_init(&ar->data_lock);
1503 INIT_LIST_HEAD(&ar->peers);
1504 init_waitqueue_head(&ar->peer_mapping_wq);
1505 init_waitqueue_head(&ar->htt.empty_tx_wq);
1506 init_waitqueue_head(&ar->wmi.tx_credits_wq);
1508 init_completion(&ar->offchan_tx_completed);
1509 INIT_WORK(&ar->offchan_tx_work, ath10k_offchan_tx_work);
1510 skb_queue_head_init(&ar->offchan_tx_queue);
1512 INIT_WORK(&ar->wmi_mgmt_tx_work, ath10k_mgmt_over_wmi_tx_work);
1513 skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
1515 INIT_WORK(&ar->register_work, ath10k_core_register_work);
1516 INIT_WORK(&ar->restart_work, ath10k_core_restart);
1518 ret = ath10k_debug_create(ar);
1525 destroy_workqueue(ar->workqueue);
1528 ath10k_mac_destroy(ar);
1532 EXPORT_SYMBOL(ath10k_core_create);
1534 void ath10k_core_destroy(struct ath10k *ar)
1536 flush_workqueue(ar->workqueue);
1537 destroy_workqueue(ar->workqueue);
1539 ath10k_debug_destroy(ar);
1540 ath10k_mac_destroy(ar);
1542 EXPORT_SYMBOL(ath10k_core_destroy);
1544 MODULE_AUTHOR("Qualcomm Atheros");
1545 MODULE_DESCRIPTION("Core module for QCA988X PCIe devices.");
1546 MODULE_LICENSE("Dual BSD/GPL");