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>
30 unsigned int ath10k_debug_mask;
31 static bool uart_print;
32 static unsigned int ath10k_p2p;
33 module_param_named(debug_mask, ath10k_debug_mask, uint, 0644);
34 module_param(uart_print, bool, 0644);
35 module_param_named(p2p, ath10k_p2p, uint, 0644);
36 MODULE_PARM_DESC(debug_mask, "Debugging mask");
37 MODULE_PARM_DESC(uart_print, "Uart target debugging");
38 MODULE_PARM_DESC(p2p, "Enable ath10k P2P support");
40 static const struct ath10k_hw_params ath10k_hw_params_list[] = {
42 .id = QCA988X_HW_1_0_VERSION,
43 .name = "qca988x hw1.0",
44 .patch_load_addr = QCA988X_HW_1_0_PATCH_LOAD_ADDR,
46 .dir = QCA988X_HW_1_0_FW_DIR,
47 .fw = QCA988X_HW_1_0_FW_FILE,
48 .otp = QCA988X_HW_1_0_OTP_FILE,
49 .board = QCA988X_HW_1_0_BOARD_DATA_FILE,
53 .id = QCA988X_HW_2_0_VERSION,
54 .name = "qca988x hw2.0",
55 .patch_load_addr = QCA988X_HW_2_0_PATCH_LOAD_ADDR,
57 .dir = QCA988X_HW_2_0_FW_DIR,
58 .fw = QCA988X_HW_2_0_FW_FILE,
59 .otp = QCA988X_HW_2_0_OTP_FILE,
60 .board = QCA988X_HW_2_0_BOARD_DATA_FILE,
65 static void ath10k_send_suspend_complete(struct ath10k *ar)
67 ath10k_dbg(ATH10K_DBG_CORE, "%s\n", __func__);
69 ar->is_target_paused = true;
70 wake_up(&ar->event_queue);
73 static int ath10k_check_fw_version(struct ath10k *ar)
77 if (ar->fw_version_major >= SUPPORTED_FW_MAJOR &&
78 ar->fw_version_minor >= SUPPORTED_FW_MINOR &&
79 ar->fw_version_release >= SUPPORTED_FW_RELEASE &&
80 ar->fw_version_build >= SUPPORTED_FW_BUILD)
83 snprintf(version, sizeof(version), "%u.%u.%u.%u",
84 SUPPORTED_FW_MAJOR, SUPPORTED_FW_MINOR,
85 SUPPORTED_FW_RELEASE, SUPPORTED_FW_BUILD);
87 ath10k_warn("WARNING: Firmware version %s is not officially supported.\n",
88 ar->hw->wiphy->fw_version);
89 ath10k_warn("Please upgrade to version %s (or newer)\n", version);
94 static int ath10k_init_connect_htc(struct ath10k *ar)
98 status = ath10k_wmi_connect_htc_service(ar);
103 status = ath10k_htc_start(&ar->htc);
107 /* Wait for WMI event to be ready */
108 status = ath10k_wmi_wait_for_service_ready(ar);
110 ath10k_warn("wmi service ready event not received");
115 ath10k_dbg(ATH10K_DBG_CORE, "core wmi ready\n");
119 ath10k_htc_stop(&ar->htc);
124 static int ath10k_init_configure_target(struct ath10k *ar)
129 /* tell target which HTC version it is used*/
130 ret = ath10k_bmi_write32(ar, hi_app_host_interest,
131 HTC_PROTOCOL_VERSION);
133 ath10k_err("settings HTC version failed\n");
137 /* set the firmware mode to STA/IBSS/AP */
138 ret = ath10k_bmi_read32(ar, hi_option_flag, ¶m_host);
140 ath10k_err("setting firmware mode (1/2) failed\n");
144 /* TODO following parameters need to be re-visited. */
146 param_host |= (1 << HI_OPTION_NUM_DEV_SHIFT);
148 /* FIXME: Why FW_MODE_AP ??.*/
149 param_host |= (HI_OPTION_FW_MODE_AP << HI_OPTION_FW_MODE_SHIFT);
150 /* mac_addr_method */
151 param_host |= (1 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
152 /* firmware_bridge */
153 param_host |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
155 param_host |= (0 << HI_OPTION_FW_SUBMODE_SHIFT);
157 ret = ath10k_bmi_write32(ar, hi_option_flag, param_host);
159 ath10k_err("setting firmware mode (2/2) failed\n");
163 /* We do all byte-swapping on the host */
164 ret = ath10k_bmi_write32(ar, hi_be, 0);
166 ath10k_err("setting host CPU BE mode failed\n");
170 /* FW descriptor/Data swap flags */
171 ret = ath10k_bmi_write32(ar, hi_fw_swap, 0);
174 ath10k_err("setting FW data/desc swap flags failed\n");
181 static const struct firmware *ath10k_fetch_fw_file(struct ath10k *ar,
186 const struct firmware *fw;
190 return ERR_PTR(-ENOENT);
195 snprintf(filename, sizeof(filename), "%s/%s", dir, file);
196 ret = request_firmware(&fw, filename, ar->dev);
203 static int ath10k_push_board_ext_data(struct ath10k *ar,
204 const struct firmware *fw)
206 u32 board_data_size = QCA988X_BOARD_DATA_SZ;
207 u32 board_ext_data_size = QCA988X_BOARD_EXT_DATA_SZ;
208 u32 board_ext_data_addr;
211 ret = ath10k_bmi_read32(ar, hi_board_ext_data, &board_ext_data_addr);
213 ath10k_err("could not read board ext data addr (%d)\n", ret);
217 ath10k_dbg(ATH10K_DBG_CORE,
218 "ath10k: Board extended Data download addr: 0x%x\n",
219 board_ext_data_addr);
221 if (board_ext_data_addr == 0)
224 if (fw->size != (board_data_size + board_ext_data_size)) {
225 ath10k_err("invalid board (ext) data sizes %zu != %d+%d\n",
226 fw->size, board_data_size, board_ext_data_size);
230 ret = ath10k_bmi_write_memory(ar, board_ext_data_addr,
231 fw->data + board_data_size,
232 board_ext_data_size);
234 ath10k_err("could not write board ext data (%d)\n", ret);
238 ret = ath10k_bmi_write32(ar, hi_board_ext_data_config,
239 (board_ext_data_size << 16) | 1);
241 ath10k_err("could not write board ext data bit (%d)\n", ret);
248 static int ath10k_download_board_data(struct ath10k *ar)
250 const struct firmware *fw = ar->board_data;
251 u32 board_data_size = QCA988X_BOARD_DATA_SZ;
255 ret = ath10k_push_board_ext_data(ar, fw);
257 ath10k_err("could not push board ext data (%d)\n", ret);
261 ret = ath10k_bmi_read32(ar, hi_board_data, &address);
263 ath10k_err("could not read board data addr (%d)\n", ret);
267 ret = ath10k_bmi_write_memory(ar, address, fw->data,
268 min_t(u32, board_data_size, fw->size));
270 ath10k_err("could not write board data (%d)\n", ret);
274 ret = ath10k_bmi_write32(ar, hi_board_data_initialized, 1);
276 ath10k_err("could not write board data bit (%d)\n", ret);
284 static int ath10k_download_and_run_otp(struct ath10k *ar)
286 const struct firmware *fw = ar->otp;
287 u32 address = ar->hw_params.patch_load_addr;
291 /* OTP is optional */
296 ret = ath10k_bmi_fast_download(ar, address, fw->data, fw->size);
298 ath10k_err("could not write otp (%d)\n", ret);
303 ret = ath10k_bmi_execute(ar, address, &exec_param);
305 ath10k_err("could not execute otp (%d)\n", ret);
313 static int ath10k_download_fw(struct ath10k *ar)
315 const struct firmware *fw = ar->firmware;
319 address = ar->hw_params.patch_load_addr;
321 ret = ath10k_bmi_fast_download(ar, address, fw->data, fw->size);
323 ath10k_err("could not write fw (%d)\n", ret);
331 static void ath10k_core_free_firmware_files(struct ath10k *ar)
333 if (ar->board_data && !IS_ERR(ar->board_data))
334 release_firmware(ar->board_data);
336 if (ar->otp && !IS_ERR(ar->otp))
337 release_firmware(ar->otp);
339 if (ar->firmware && !IS_ERR(ar->firmware))
340 release_firmware(ar->firmware);
342 ar->board_data = NULL;
347 static int ath10k_core_fetch_firmware_files(struct ath10k *ar)
351 if (ar->hw_params.fw.fw == NULL) {
352 ath10k_err("firmware file not defined\n");
356 if (ar->hw_params.fw.board == NULL) {
357 ath10k_err("board data file not defined");
361 ar->board_data = ath10k_fetch_fw_file(ar,
362 ar->hw_params.fw.dir,
363 ar->hw_params.fw.board);
364 if (IS_ERR(ar->board_data)) {
365 ret = PTR_ERR(ar->board_data);
366 ath10k_err("could not fetch board data (%d)\n", ret);
370 ar->firmware = ath10k_fetch_fw_file(ar,
371 ar->hw_params.fw.dir,
372 ar->hw_params.fw.fw);
373 if (IS_ERR(ar->firmware)) {
374 ret = PTR_ERR(ar->firmware);
375 ath10k_err("could not fetch firmware (%d)\n", ret);
379 /* OTP may be undefined. If so, don't fetch it at all */
380 if (ar->hw_params.fw.otp == NULL)
383 ar->otp = ath10k_fetch_fw_file(ar,
384 ar->hw_params.fw.dir,
385 ar->hw_params.fw.otp);
386 if (IS_ERR(ar->otp)) {
387 ret = PTR_ERR(ar->otp);
388 ath10k_err("could not fetch otp (%d)\n", ret);
395 ath10k_core_free_firmware_files(ar);
399 static int ath10k_init_download_firmware(struct ath10k *ar)
403 ret = ath10k_download_board_data(ar);
407 ret = ath10k_download_and_run_otp(ar);
411 ret = ath10k_download_fw(ar);
418 static int ath10k_init_uart(struct ath10k *ar)
423 * Explicitly setting UART prints to zero as target turns it on
424 * based on scratch registers.
426 ret = ath10k_bmi_write32(ar, hi_serial_enable, 0);
428 ath10k_warn("could not disable UART prints (%d)\n", ret);
433 ath10k_info("UART prints disabled\n");
437 ret = ath10k_bmi_write32(ar, hi_dbg_uart_txpin, 7);
439 ath10k_warn("could not enable UART prints (%d)\n", ret);
443 ret = ath10k_bmi_write32(ar, hi_serial_enable, 1);
445 ath10k_warn("could not enable UART prints (%d)\n", ret);
449 ath10k_info("UART prints enabled\n");
453 static int ath10k_init_hw_params(struct ath10k *ar)
455 const struct ath10k_hw_params *uninitialized_var(hw_params);
458 for (i = 0; i < ARRAY_SIZE(ath10k_hw_params_list); i++) {
459 hw_params = &ath10k_hw_params_list[i];
461 if (hw_params->id == ar->target_version)
465 if (i == ARRAY_SIZE(ath10k_hw_params_list)) {
466 ath10k_err("Unsupported hardware version: 0x%x\n",
471 ar->hw_params = *hw_params;
473 ath10k_info("Hardware name %s version 0x%x\n",
474 ar->hw_params.name, ar->target_version);
479 static void ath10k_core_restart(struct work_struct *work)
481 struct ath10k *ar = container_of(work, struct ath10k, restart_work);
483 mutex_lock(&ar->conf_mutex);
486 case ATH10K_STATE_ON:
488 ar->state = ATH10K_STATE_RESTARTING;
489 ieee80211_restart_hw(ar->hw);
491 case ATH10K_STATE_OFF:
492 /* this can happen if driver is being unloaded */
493 ath10k_warn("cannot restart a device that hasn't been started\n");
495 case ATH10K_STATE_RESTARTING:
496 case ATH10K_STATE_RESTARTED:
497 ar->state = ATH10K_STATE_WEDGED;
499 case ATH10K_STATE_WEDGED:
500 ath10k_warn("device is wedged, will not restart\n");
504 mutex_unlock(&ar->conf_mutex);
507 struct ath10k *ath10k_core_create(void *hif_priv, struct device *dev,
508 const struct ath10k_hif_ops *hif_ops)
512 ar = ath10k_mac_create();
516 ar->ath_common.priv = ar;
517 ar->ath_common.hw = ar->hw;
519 ar->p2p = !!ath10k_p2p;
522 ar->hif.priv = hif_priv;
523 ar->hif.ops = hif_ops;
525 init_completion(&ar->scan.started);
526 init_completion(&ar->scan.completed);
527 init_completion(&ar->scan.on_channel);
529 init_completion(&ar->install_key_done);
530 init_completion(&ar->vdev_setup_done);
532 setup_timer(&ar->scan.timeout, ath10k_reset_scan, (unsigned long)ar);
534 ar->workqueue = create_singlethread_workqueue("ath10k_wq");
538 mutex_init(&ar->conf_mutex);
539 spin_lock_init(&ar->data_lock);
541 INIT_LIST_HEAD(&ar->peers);
542 init_waitqueue_head(&ar->peer_mapping_wq);
544 init_completion(&ar->offchan_tx_completed);
545 INIT_WORK(&ar->offchan_tx_work, ath10k_offchan_tx_work);
546 skb_queue_head_init(&ar->offchan_tx_queue);
548 init_waitqueue_head(&ar->event_queue);
550 INIT_WORK(&ar->restart_work, ath10k_core_restart);
555 ath10k_mac_destroy(ar);
558 EXPORT_SYMBOL(ath10k_core_create);
560 void ath10k_core_destroy(struct ath10k *ar)
562 flush_workqueue(ar->workqueue);
563 destroy_workqueue(ar->workqueue);
565 ath10k_mac_destroy(ar);
567 EXPORT_SYMBOL(ath10k_core_destroy);
569 int ath10k_core_start(struct ath10k *ar)
573 ath10k_bmi_start(ar);
575 if (ath10k_init_configure_target(ar)) {
580 status = ath10k_init_download_firmware(ar);
584 status = ath10k_init_uart(ar);
588 ar->htc.htc_ops.target_send_suspend_complete =
589 ath10k_send_suspend_complete;
591 status = ath10k_htc_init(ar);
593 ath10k_err("could not init HTC (%d)\n", status);
597 status = ath10k_bmi_done(ar);
601 status = ath10k_wmi_attach(ar);
603 ath10k_err("WMI attach failed: %d\n", status);
607 status = ath10k_htc_wait_target(&ar->htc);
611 status = ath10k_htt_attach(ar);
613 ath10k_err("could not attach htt (%d)\n", status);
617 status = ath10k_init_connect_htc(ar);
621 ath10k_info("firmware %s booted\n", ar->hw->wiphy->fw_version);
623 status = ath10k_check_fw_version(ar);
625 goto err_disconnect_htc;
627 status = ath10k_wmi_cmd_init(ar);
629 ath10k_err("could not send WMI init command (%d)\n", status);
630 goto err_disconnect_htc;
633 status = ath10k_wmi_wait_for_unified_ready(ar);
635 ath10k_err("wmi unified ready event not received\n");
637 goto err_disconnect_htc;
640 status = ath10k_htt_attach_target(&ar->htt);
642 goto err_disconnect_htc;
644 ar->free_vdev_map = (1 << TARGET_NUM_VDEVS) - 1;
649 ath10k_htc_stop(&ar->htc);
651 ath10k_htt_detach(&ar->htt);
653 ath10k_wmi_detach(ar);
657 EXPORT_SYMBOL(ath10k_core_start);
659 void ath10k_core_stop(struct ath10k *ar)
661 ath10k_htc_stop(&ar->htc);
662 ath10k_htt_detach(&ar->htt);
663 ath10k_wmi_detach(ar);
665 EXPORT_SYMBOL(ath10k_core_stop);
667 /* mac80211 manages fw/hw initialization through start/stop hooks. However in
668 * order to know what hw capabilities should be advertised to mac80211 it is
669 * necessary to load the firmware (and tear it down immediately since start
670 * hook will try to init it again) before registering */
671 static int ath10k_core_probe_fw(struct ath10k *ar)
673 struct bmi_target_info target_info;
676 ret = ath10k_hif_power_up(ar);
678 ath10k_err("could not start pci hif (%d)\n", ret);
682 memset(&target_info, 0, sizeof(target_info));
683 ret = ath10k_bmi_get_target_info(ar, &target_info);
685 ath10k_err("could not get target info (%d)\n", ret);
686 ath10k_hif_power_down(ar);
690 ar->target_version = target_info.version;
691 ar->hw->wiphy->hw_version = target_info.version;
693 ret = ath10k_init_hw_params(ar);
695 ath10k_err("could not get hw params (%d)\n", ret);
696 ath10k_hif_power_down(ar);
700 ret = ath10k_core_fetch_firmware_files(ar);
702 ath10k_err("could not fetch firmware files (%d)\n", ret);
703 ath10k_hif_power_down(ar);
707 ret = ath10k_core_start(ar);
709 ath10k_err("could not init core (%d)\n", ret);
710 ath10k_core_free_firmware_files(ar);
711 ath10k_hif_power_down(ar);
715 ath10k_core_stop(ar);
716 ath10k_hif_power_down(ar);
720 int ath10k_core_register(struct ath10k *ar)
724 status = ath10k_core_probe_fw(ar);
726 ath10k_err("could not probe fw (%d)\n", status);
730 status = ath10k_mac_register(ar);
732 ath10k_err("could not register to mac80211 (%d)\n", status);
736 status = ath10k_debug_create(ar);
738 ath10k_err("unable to initialize debugfs\n");
739 goto err_unregister_mac;
745 ath10k_mac_unregister(ar);
747 ath10k_core_free_firmware_files(ar);
750 EXPORT_SYMBOL(ath10k_core_register);
752 void ath10k_core_unregister(struct ath10k *ar)
754 /* We must unregister from mac80211 before we stop HTC and HIF.
755 * Otherwise we will fail to submit commands to FW and mac80211 will be
756 * unhappy about callback failures. */
757 ath10k_mac_unregister(ar);
758 ath10k_core_free_firmware_files(ar);
760 EXPORT_SYMBOL(ath10k_core_unregister);
762 MODULE_AUTHOR("Qualcomm Atheros");
763 MODULE_DESCRIPTION("Core module for QCA988X PCIe devices.");
764 MODULE_LICENSE("Dual BSD/GPL");