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_2_0_VERSION,
43 .name = "qca988x hw2.0",
44 .patch_load_addr = QCA988X_HW_2_0_PATCH_LOAD_ADDR,
46 .dir = QCA988X_HW_2_0_FW_DIR,
47 .fw = QCA988X_HW_2_0_FW_FILE,
48 .otp = QCA988X_HW_2_0_OTP_FILE,
49 .board = QCA988X_HW_2_0_BOARD_DATA_FILE,
54 static void ath10k_send_suspend_complete(struct ath10k *ar)
56 ath10k_dbg(ATH10K_DBG_BOOT, "boot suspend complete\n");
58 ar->is_target_paused = true;
59 wake_up(&ar->event_queue);
62 static int ath10k_init_connect_htc(struct ath10k *ar)
66 status = ath10k_wmi_connect_htc_service(ar);
71 status = ath10k_htc_start(&ar->htc);
75 /* Wait for WMI event to be ready */
76 status = ath10k_wmi_wait_for_service_ready(ar);
78 ath10k_warn("wmi service ready event not received");
83 ath10k_dbg(ATH10K_DBG_BOOT, "boot wmi ready\n");
87 ath10k_htc_stop(&ar->htc);
92 static int ath10k_init_configure_target(struct ath10k *ar)
97 /* tell target which HTC version it is used*/
98 ret = ath10k_bmi_write32(ar, hi_app_host_interest,
99 HTC_PROTOCOL_VERSION);
101 ath10k_err("settings HTC version failed\n");
105 /* set the firmware mode to STA/IBSS/AP */
106 ret = ath10k_bmi_read32(ar, hi_option_flag, ¶m_host);
108 ath10k_err("setting firmware mode (1/2) failed\n");
112 /* TODO following parameters need to be re-visited. */
114 param_host |= (1 << HI_OPTION_NUM_DEV_SHIFT);
116 /* FIXME: Why FW_MODE_AP ??.*/
117 param_host |= (HI_OPTION_FW_MODE_AP << HI_OPTION_FW_MODE_SHIFT);
118 /* mac_addr_method */
119 param_host |= (1 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
120 /* firmware_bridge */
121 param_host |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
123 param_host |= (0 << HI_OPTION_FW_SUBMODE_SHIFT);
125 ret = ath10k_bmi_write32(ar, hi_option_flag, param_host);
127 ath10k_err("setting firmware mode (2/2) failed\n");
131 /* We do all byte-swapping on the host */
132 ret = ath10k_bmi_write32(ar, hi_be, 0);
134 ath10k_err("setting host CPU BE mode failed\n");
138 /* FW descriptor/Data swap flags */
139 ret = ath10k_bmi_write32(ar, hi_fw_swap, 0);
142 ath10k_err("setting FW data/desc swap flags failed\n");
149 static const struct firmware *ath10k_fetch_fw_file(struct ath10k *ar,
154 const struct firmware *fw;
158 return ERR_PTR(-ENOENT);
163 snprintf(filename, sizeof(filename), "%s/%s", dir, file);
164 ret = request_firmware(&fw, filename, ar->dev);
171 static int ath10k_push_board_ext_data(struct ath10k *ar,
172 const struct firmware *fw)
174 u32 board_data_size = QCA988X_BOARD_DATA_SZ;
175 u32 board_ext_data_size = QCA988X_BOARD_EXT_DATA_SZ;
176 u32 board_ext_data_addr;
179 ret = ath10k_bmi_read32(ar, hi_board_ext_data, &board_ext_data_addr);
181 ath10k_err("could not read board ext data addr (%d)\n", ret);
185 ath10k_dbg(ATH10K_DBG_BOOT,
186 "boot push board extended data addr 0x%x\n",
187 board_ext_data_addr);
189 if (board_ext_data_addr == 0)
192 if (fw->size != (board_data_size + board_ext_data_size)) {
193 ath10k_err("invalid board (ext) data sizes %zu != %d+%d\n",
194 fw->size, board_data_size, board_ext_data_size);
198 ret = ath10k_bmi_write_memory(ar, board_ext_data_addr,
199 fw->data + board_data_size,
200 board_ext_data_size);
202 ath10k_err("could not write board ext data (%d)\n", ret);
206 ret = ath10k_bmi_write32(ar, hi_board_ext_data_config,
207 (board_ext_data_size << 16) | 1);
209 ath10k_err("could not write board ext data bit (%d)\n", ret);
216 static int ath10k_download_board_data(struct ath10k *ar)
218 const struct firmware *fw = ar->board;
219 u32 board_data_size = QCA988X_BOARD_DATA_SZ;
223 ret = ath10k_push_board_ext_data(ar, fw);
225 ath10k_err("could not push board ext data (%d)\n", ret);
229 ret = ath10k_bmi_read32(ar, hi_board_data, &address);
231 ath10k_err("could not read board data addr (%d)\n", ret);
235 ret = ath10k_bmi_write_memory(ar, address, fw->data,
236 min_t(u32, board_data_size, fw->size));
238 ath10k_err("could not write board data (%d)\n", ret);
242 ret = ath10k_bmi_write32(ar, hi_board_data_initialized, 1);
244 ath10k_err("could not write board data bit (%d)\n", ret);
252 static int ath10k_download_and_run_otp(struct ath10k *ar)
254 const struct firmware *fw = ar->otp;
255 u32 address = ar->hw_params.patch_load_addr;
259 /* OTP is optional */
264 ret = ath10k_bmi_fast_download(ar, address, fw->data, fw->size);
266 ath10k_err("could not write otp (%d)\n", ret);
271 ret = ath10k_bmi_execute(ar, address, &exec_param);
273 ath10k_err("could not execute otp (%d)\n", ret);
281 static int ath10k_download_fw(struct ath10k *ar)
283 const struct firmware *fw = ar->firmware;
287 address = ar->hw_params.patch_load_addr;
289 ret = ath10k_bmi_fast_download(ar, address, fw->data, fw->size);
291 ath10k_err("could not write fw (%d)\n", ret);
299 static void ath10k_core_free_firmware_files(struct ath10k *ar)
301 if (ar->board && !IS_ERR(ar->board))
302 release_firmware(ar->board);
304 if (ar->otp && !IS_ERR(ar->otp))
305 release_firmware(ar->otp);
307 if (ar->firmware && !IS_ERR(ar->firmware))
308 release_firmware(ar->firmware);
315 static int ath10k_core_fetch_firmware_files(struct ath10k *ar)
319 if (ar->hw_params.fw.fw == NULL) {
320 ath10k_err("firmware file not defined\n");
324 if (ar->hw_params.fw.board == NULL) {
325 ath10k_err("board data file not defined");
329 ar->board = ath10k_fetch_fw_file(ar,
330 ar->hw_params.fw.dir,
331 ar->hw_params.fw.board);
332 if (IS_ERR(ar->board)) {
333 ret = PTR_ERR(ar->board);
334 ath10k_err("could not fetch board data (%d)\n", ret);
338 ar->firmware = ath10k_fetch_fw_file(ar,
339 ar->hw_params.fw.dir,
340 ar->hw_params.fw.fw);
341 if (IS_ERR(ar->firmware)) {
342 ret = PTR_ERR(ar->firmware);
343 ath10k_err("could not fetch firmware (%d)\n", ret);
347 /* OTP may be undefined. If so, don't fetch it at all */
348 if (ar->hw_params.fw.otp == NULL)
351 ar->otp = ath10k_fetch_fw_file(ar,
352 ar->hw_params.fw.dir,
353 ar->hw_params.fw.otp);
354 if (IS_ERR(ar->otp)) {
355 ret = PTR_ERR(ar->otp);
356 ath10k_err("could not fetch otp (%d)\n", ret);
363 ath10k_core_free_firmware_files(ar);
367 static int ath10k_init_download_firmware(struct ath10k *ar)
371 ret = ath10k_download_board_data(ar);
375 ret = ath10k_download_and_run_otp(ar);
379 ret = ath10k_download_fw(ar);
386 static int ath10k_init_uart(struct ath10k *ar)
391 * Explicitly setting UART prints to zero as target turns it on
392 * based on scratch registers.
394 ret = ath10k_bmi_write32(ar, hi_serial_enable, 0);
396 ath10k_warn("could not disable UART prints (%d)\n", ret);
401 ath10k_info("UART prints disabled\n");
405 ret = ath10k_bmi_write32(ar, hi_dbg_uart_txpin, 7);
407 ath10k_warn("could not enable UART prints (%d)\n", ret);
411 ret = ath10k_bmi_write32(ar, hi_serial_enable, 1);
413 ath10k_warn("could not enable UART prints (%d)\n", ret);
417 /* Set the UART baud rate to 19200. */
418 ret = ath10k_bmi_write32(ar, hi_desired_baud_rate, 19200);
420 ath10k_warn("could not set the baud rate (%d)\n", ret);
424 ath10k_info("UART prints enabled\n");
428 static int ath10k_init_hw_params(struct ath10k *ar)
430 const struct ath10k_hw_params *uninitialized_var(hw_params);
433 for (i = 0; i < ARRAY_SIZE(ath10k_hw_params_list); i++) {
434 hw_params = &ath10k_hw_params_list[i];
436 if (hw_params->id == ar->target_version)
440 if (i == ARRAY_SIZE(ath10k_hw_params_list)) {
441 ath10k_err("Unsupported hardware version: 0x%x\n",
446 ar->hw_params = *hw_params;
448 ath10k_info("Hardware name %s version 0x%x\n",
449 ar->hw_params.name, ar->target_version);
454 static void ath10k_core_restart(struct work_struct *work)
456 struct ath10k *ar = container_of(work, struct ath10k, restart_work);
458 mutex_lock(&ar->conf_mutex);
461 case ATH10K_STATE_ON:
463 ar->state = ATH10K_STATE_RESTARTING;
464 ieee80211_restart_hw(ar->hw);
466 case ATH10K_STATE_OFF:
467 /* this can happen if driver is being unloaded */
468 ath10k_warn("cannot restart a device that hasn't been started\n");
470 case ATH10K_STATE_RESTARTING:
471 case ATH10K_STATE_RESTARTED:
472 ar->state = ATH10K_STATE_WEDGED;
474 case ATH10K_STATE_WEDGED:
475 ath10k_warn("device is wedged, will not restart\n");
479 mutex_unlock(&ar->conf_mutex);
482 struct ath10k *ath10k_core_create(void *hif_priv, struct device *dev,
483 const struct ath10k_hif_ops *hif_ops)
487 ar = ath10k_mac_create();
491 ar->ath_common.priv = ar;
492 ar->ath_common.hw = ar->hw;
494 ar->p2p = !!ath10k_p2p;
497 ar->hif.priv = hif_priv;
498 ar->hif.ops = hif_ops;
500 init_completion(&ar->scan.started);
501 init_completion(&ar->scan.completed);
502 init_completion(&ar->scan.on_channel);
504 init_completion(&ar->install_key_done);
505 init_completion(&ar->vdev_setup_done);
507 setup_timer(&ar->scan.timeout, ath10k_reset_scan, (unsigned long)ar);
509 ar->workqueue = create_singlethread_workqueue("ath10k_wq");
513 mutex_init(&ar->conf_mutex);
514 spin_lock_init(&ar->data_lock);
516 INIT_LIST_HEAD(&ar->peers);
517 init_waitqueue_head(&ar->peer_mapping_wq);
519 init_completion(&ar->offchan_tx_completed);
520 INIT_WORK(&ar->offchan_tx_work, ath10k_offchan_tx_work);
521 skb_queue_head_init(&ar->offchan_tx_queue);
523 INIT_WORK(&ar->wmi_mgmt_tx_work, ath10k_mgmt_over_wmi_tx_work);
524 skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
526 init_waitqueue_head(&ar->event_queue);
528 INIT_WORK(&ar->restart_work, ath10k_core_restart);
533 ath10k_mac_destroy(ar);
536 EXPORT_SYMBOL(ath10k_core_create);
538 void ath10k_core_destroy(struct ath10k *ar)
540 flush_workqueue(ar->workqueue);
541 destroy_workqueue(ar->workqueue);
543 ath10k_mac_destroy(ar);
545 EXPORT_SYMBOL(ath10k_core_destroy);
547 int ath10k_core_start(struct ath10k *ar)
551 ath10k_bmi_start(ar);
553 if (ath10k_init_configure_target(ar)) {
558 status = ath10k_init_download_firmware(ar);
562 status = ath10k_init_uart(ar);
566 ar->htc.htc_ops.target_send_suspend_complete =
567 ath10k_send_suspend_complete;
569 status = ath10k_htc_init(ar);
571 ath10k_err("could not init HTC (%d)\n", status);
575 status = ath10k_bmi_done(ar);
579 status = ath10k_wmi_attach(ar);
581 ath10k_err("WMI attach failed: %d\n", status);
585 status = ath10k_htc_wait_target(&ar->htc);
589 status = ath10k_htt_attach(ar);
591 ath10k_err("could not attach htt (%d)\n", status);
595 status = ath10k_init_connect_htc(ar);
599 ath10k_info("firmware %s booted\n", ar->hw->wiphy->fw_version);
601 status = ath10k_wmi_cmd_init(ar);
603 ath10k_err("could not send WMI init command (%d)\n", status);
604 goto err_disconnect_htc;
607 status = ath10k_wmi_wait_for_unified_ready(ar);
609 ath10k_err("wmi unified ready event not received\n");
611 goto err_disconnect_htc;
614 status = ath10k_htt_attach_target(&ar->htt);
616 goto err_disconnect_htc;
618 status = ath10k_debug_start(ar);
620 goto err_disconnect_htc;
622 ar->free_vdev_map = (1 << TARGET_NUM_VDEVS) - 1;
627 ath10k_htc_stop(&ar->htc);
629 ath10k_htt_detach(&ar->htt);
631 ath10k_wmi_detach(ar);
635 EXPORT_SYMBOL(ath10k_core_start);
637 void ath10k_core_stop(struct ath10k *ar)
639 ath10k_debug_stop(ar);
640 ath10k_htc_stop(&ar->htc);
641 ath10k_htt_detach(&ar->htt);
642 ath10k_wmi_detach(ar);
644 EXPORT_SYMBOL(ath10k_core_stop);
646 /* mac80211 manages fw/hw initialization through start/stop hooks. However in
647 * order to know what hw capabilities should be advertised to mac80211 it is
648 * necessary to load the firmware (and tear it down immediately since start
649 * hook will try to init it again) before registering */
650 static int ath10k_core_probe_fw(struct ath10k *ar)
652 struct bmi_target_info target_info;
655 ret = ath10k_hif_power_up(ar);
657 ath10k_err("could not start pci hif (%d)\n", ret);
661 memset(&target_info, 0, sizeof(target_info));
662 ret = ath10k_bmi_get_target_info(ar, &target_info);
664 ath10k_err("could not get target info (%d)\n", ret);
665 ath10k_hif_power_down(ar);
669 ar->target_version = target_info.version;
670 ar->hw->wiphy->hw_version = target_info.version;
672 ret = ath10k_init_hw_params(ar);
674 ath10k_err("could not get hw params (%d)\n", ret);
675 ath10k_hif_power_down(ar);
679 ret = ath10k_core_fetch_firmware_files(ar);
681 ath10k_err("could not fetch firmware files (%d)\n", ret);
682 ath10k_hif_power_down(ar);
686 ret = ath10k_core_start(ar);
688 ath10k_err("could not init core (%d)\n", ret);
689 ath10k_core_free_firmware_files(ar);
690 ath10k_hif_power_down(ar);
694 ath10k_core_stop(ar);
695 ath10k_hif_power_down(ar);
699 static int ath10k_core_check_chip_id(struct ath10k *ar)
701 u32 hw_revision = MS(ar->chip_id, SOC_CHIP_ID_REV);
703 ath10k_dbg(ATH10K_DBG_BOOT, "boot chip_id 0x%08x hw_revision 0x%x\n",
704 ar->chip_id, hw_revision);
706 /* Check that we are not using hw1.0 (some of them have same pci id
707 * as hw2.0) before doing anything else as ath10k crashes horribly
708 * due to missing hw1.0 workarounds. */
709 switch (hw_revision) {
710 case QCA988X_HW_1_0_CHIP_ID_REV:
711 ath10k_err("ERROR: qca988x hw1.0 is not supported\n");
714 case QCA988X_HW_2_0_CHIP_ID_REV:
715 /* known hardware revision, continue normally */
719 ath10k_warn("Warning: hardware revision unknown (0x%x), expect problems\n",
727 int ath10k_core_register(struct ath10k *ar, u32 chip_id)
731 ar->chip_id = chip_id;
733 status = ath10k_core_check_chip_id(ar);
735 ath10k_err("Unsupported chip id 0x%08x\n", ar->chip_id);
739 status = ath10k_core_probe_fw(ar);
741 ath10k_err("could not probe fw (%d)\n", status);
745 status = ath10k_mac_register(ar);
747 ath10k_err("could not register to mac80211 (%d)\n", status);
751 status = ath10k_debug_create(ar);
753 ath10k_err("unable to initialize debugfs\n");
754 goto err_unregister_mac;
760 ath10k_mac_unregister(ar);
762 ath10k_core_free_firmware_files(ar);
765 EXPORT_SYMBOL(ath10k_core_register);
767 void ath10k_core_unregister(struct ath10k *ar)
769 /* We must unregister from mac80211 before we stop HTC and HIF.
770 * Otherwise we will fail to submit commands to FW and mac80211 will be
771 * unhappy about callback failures. */
772 ath10k_mac_unregister(ar);
774 ath10k_core_free_firmware_files(ar);
776 EXPORT_SYMBOL(ath10k_core_unregister);
778 MODULE_AUTHOR("Qualcomm Atheros");
779 MODULE_DESCRIPTION("Core module for QCA988X PCIe devices.");
780 MODULE_LICENSE("Dual BSD/GPL");