1 // SPDX-License-Identifier: BSD-3-Clause-Clear
3 * Copyright (c) 2020-2021 The Linux Foundation. All rights reserved.
4 * Copyright (c) 2021-2024 Qualcomm Innovation Center, Inc. All rights reserved.
9 #include <linux/firmware.h>
16 #define MHI_TIMEOUT_DEFAULT_MS 90000
17 #define OTP_INVALID_BOARD_ID 0xFFFF
18 #define OTP_VALID_DUALMAC_BOARD_ID_MASK 0x1000
20 static const struct mhi_channel_config ath12k_mhi_channels_qcn9274[] = {
29 .doorbell = MHI_DB_BRST_DISABLE,
31 .offload_channel = false,
32 .doorbell_mode_switch = false,
40 .dir = DMA_FROM_DEVICE,
43 .doorbell = MHI_DB_BRST_DISABLE,
45 .offload_channel = false,
46 .doorbell_mode_switch = false,
57 .doorbell = MHI_DB_BRST_DISABLE,
59 .offload_channel = false,
60 .doorbell_mode_switch = false,
68 .dir = DMA_FROM_DEVICE,
71 .doorbell = MHI_DB_BRST_DISABLE,
73 .offload_channel = false,
74 .doorbell_mode_switch = false,
79 static struct mhi_event_config ath12k_mhi_events_qcn9274[] = {
82 .irq_moderation_ms = 0,
84 .data_type = MHI_ER_CTRL,
85 .mode = MHI_DB_BRST_DISABLE,
86 .hardware_event = false,
87 .client_managed = false,
88 .offload_channel = false,
92 .irq_moderation_ms = 1,
94 .mode = MHI_DB_BRST_DISABLE,
96 .hardware_event = false,
97 .client_managed = false,
98 .offload_channel = false,
102 const struct mhi_controller_config ath12k_mhi_config_qcn9274 = {
105 .use_bounce_buf = false,
107 .num_channels = ARRAY_SIZE(ath12k_mhi_channels_qcn9274),
108 .ch_cfg = ath12k_mhi_channels_qcn9274,
109 .num_events = ARRAY_SIZE(ath12k_mhi_events_qcn9274),
110 .event_cfg = ath12k_mhi_events_qcn9274,
113 static const struct mhi_channel_config ath12k_mhi_channels_wcn7850[] = {
119 .dir = DMA_TO_DEVICE,
122 .doorbell = MHI_DB_BRST_DISABLE,
124 .offload_channel = false,
125 .doorbell_mode_switch = false,
133 .dir = DMA_FROM_DEVICE,
136 .doorbell = MHI_DB_BRST_DISABLE,
138 .offload_channel = false,
139 .doorbell_mode_switch = false,
147 .dir = DMA_TO_DEVICE,
150 .doorbell = MHI_DB_BRST_DISABLE,
152 .offload_channel = false,
153 .doorbell_mode_switch = false,
161 .dir = DMA_FROM_DEVICE,
164 .doorbell = MHI_DB_BRST_DISABLE,
166 .offload_channel = false,
167 .doorbell_mode_switch = false,
172 static struct mhi_event_config ath12k_mhi_events_wcn7850[] = {
175 .irq_moderation_ms = 0,
177 .mode = MHI_DB_BRST_DISABLE,
178 .data_type = MHI_ER_CTRL,
179 .hardware_event = false,
180 .client_managed = false,
181 .offload_channel = false,
185 .irq_moderation_ms = 1,
187 .mode = MHI_DB_BRST_DISABLE,
189 .hardware_event = false,
190 .client_managed = false,
191 .offload_channel = false,
195 const struct mhi_controller_config ath12k_mhi_config_wcn7850 = {
198 .use_bounce_buf = false,
200 .num_channels = ARRAY_SIZE(ath12k_mhi_channels_wcn7850),
201 .ch_cfg = ath12k_mhi_channels_wcn7850,
202 .num_events = ARRAY_SIZE(ath12k_mhi_events_wcn7850),
203 .event_cfg = ath12k_mhi_events_wcn7850,
206 void ath12k_mhi_set_mhictrl_reset(struct ath12k_base *ab)
210 val = ath12k_pci_read32(ab, MHISTATUS);
212 ath12k_dbg(ab, ATH12K_DBG_PCI, "MHISTATUS 0x%x\n", val);
214 /* Observed on some targets that after SOC_GLOBAL_RESET, MHISTATUS
215 * has SYSERR bit set and thus need to set MHICTRL_RESET
218 ath12k_pci_write32(ab, MHICTRL, MHICTRL_RESET_MASK);
223 static void ath12k_mhi_reset_txvecdb(struct ath12k_base *ab)
225 ath12k_pci_write32(ab, PCIE_TXVECDB, 0);
228 static void ath12k_mhi_reset_txvecstatus(struct ath12k_base *ab)
230 ath12k_pci_write32(ab, PCIE_TXVECSTATUS, 0);
233 static void ath12k_mhi_reset_rxvecdb(struct ath12k_base *ab)
235 ath12k_pci_write32(ab, PCIE_RXVECDB, 0);
238 static void ath12k_mhi_reset_rxvecstatus(struct ath12k_base *ab)
240 ath12k_pci_write32(ab, PCIE_RXVECSTATUS, 0);
243 void ath12k_mhi_clear_vector(struct ath12k_base *ab)
245 ath12k_mhi_reset_txvecdb(ab);
246 ath12k_mhi_reset_txvecstatus(ab);
247 ath12k_mhi_reset_rxvecdb(ab);
248 ath12k_mhi_reset_rxvecstatus(ab);
251 static int ath12k_mhi_get_msi(struct ath12k_pci *ab_pci)
253 struct ath12k_base *ab = ab_pci->ab;
254 u32 user_base_data, base_vector;
255 int ret, num_vectors, i;
257 unsigned int msi_data;
259 ret = ath12k_pci_get_user_msi_assignment(ab,
261 &user_base_data, &base_vector);
265 ath12k_dbg(ab, ATH12K_DBG_PCI, "Number of assigned MSI for MHI is %d, base vector is %d\n",
266 num_vectors, base_vector);
268 irq = kcalloc(num_vectors, sizeof(*irq), GFP_KERNEL);
272 msi_data = base_vector;
273 for (i = 0; i < num_vectors; i++) {
274 if (test_bit(ATH12K_PCI_FLAG_MULTI_MSI_VECTORS, &ab_pci->flags))
275 irq[i] = ath12k_pci_get_msi_irq(ab->dev,
278 irq[i] = ath12k_pci_get_msi_irq(ab->dev,
282 ab_pci->mhi_ctrl->irq = irq;
283 ab_pci->mhi_ctrl->nr_irqs = num_vectors;
288 static int ath12k_mhi_op_runtime_get(struct mhi_controller *mhi_cntrl)
293 static void ath12k_mhi_op_runtime_put(struct mhi_controller *mhi_cntrl)
297 static char *ath12k_mhi_op_callback_to_str(enum mhi_callback reason)
301 return "MHI_CB_IDLE";
302 case MHI_CB_PENDING_DATA:
303 return "MHI_CB_PENDING_DATA";
304 case MHI_CB_LPM_ENTER:
305 return "MHI_CB_LPM_ENTER";
306 case MHI_CB_LPM_EXIT:
307 return "MHI_CB_LPM_EXIT";
309 return "MHI_CB_EE_RDDM";
310 case MHI_CB_EE_MISSION_MODE:
311 return "MHI_CB_EE_MISSION_MODE";
312 case MHI_CB_SYS_ERROR:
313 return "MHI_CB_SYS_ERROR";
314 case MHI_CB_FATAL_ERROR:
315 return "MHI_CB_FATAL_ERROR";
317 return "MHI_CB_BW_REQ";
323 static void ath12k_mhi_op_status_cb(struct mhi_controller *mhi_cntrl,
324 enum mhi_callback cb)
326 struct ath12k_base *ab = dev_get_drvdata(mhi_cntrl->cntrl_dev);
328 ath12k_dbg(ab, ATH12K_DBG_BOOT, "mhi notify status reason %s\n",
329 ath12k_mhi_op_callback_to_str(cb));
332 case MHI_CB_SYS_ERROR:
333 ath12k_warn(ab, "firmware crashed: MHI_CB_SYS_ERROR\n");
336 if (!(test_bit(ATH12K_FLAG_UNREGISTERING, &ab->dev_flags)))
337 queue_work(ab->workqueue_aux, &ab->reset_work);
344 static int ath12k_mhi_op_read_reg(struct mhi_controller *mhi_cntrl,
353 static void ath12k_mhi_op_write_reg(struct mhi_controller *mhi_cntrl,
360 int ath12k_mhi_register(struct ath12k_pci *ab_pci)
362 struct ath12k_base *ab = ab_pci->ab;
363 struct mhi_controller *mhi_ctrl;
364 unsigned int board_id;
366 bool dualmac = false;
368 mhi_ctrl = mhi_alloc_controller();
372 ab_pci->mhi_ctrl = mhi_ctrl;
373 mhi_ctrl->cntrl_dev = ab->dev;
374 mhi_ctrl->regs = ab->mem;
375 mhi_ctrl->reg_len = ab->mem_len;
376 mhi_ctrl->rddm_size = ab->hw_params->rddm_size;
378 if (ab->hw_params->otp_board_id_register) {
380 ath12k_pci_read32(ab, ab->hw_params->otp_board_id_register);
381 board_id = u32_get_bits(board_id, OTP_BOARD_ID_MASK);
383 if (!board_id || (board_id == OTP_INVALID_BOARD_ID)) {
384 ath12k_dbg(ab, ATH12K_DBG_BOOT,
385 "failed to read board id\n");
386 } else if (board_id & OTP_VALID_DUALMAC_BOARD_ID_MASK) {
388 ab->slo_capable = false;
389 ath12k_dbg(ab, ATH12K_DBG_BOOT,
390 "dualmac fw selected for board id: %x\n", board_id);
395 if (ab->fw.amss_dualmac_data && ab->fw.amss_dualmac_len > 0) {
396 /* use MHI firmware file from firmware-N.bin */
397 mhi_ctrl->fw_data = ab->fw.amss_dualmac_data;
398 mhi_ctrl->fw_sz = ab->fw.amss_dualmac_len;
400 ath12k_warn(ab, "dualmac firmware IE not present in firmware-N.bin\n");
402 goto free_controller;
405 if (ab->fw.amss_data && ab->fw.amss_len > 0) {
406 /* use MHI firmware file from firmware-N.bin */
407 mhi_ctrl->fw_data = ab->fw.amss_data;
408 mhi_ctrl->fw_sz = ab->fw.amss_len;
410 /* use the old separate mhi.bin MHI firmware file */
411 ath12k_core_create_firmware_path(ab, ATH12K_AMSS_FILE,
413 sizeof(ab_pci->amss_path));
414 mhi_ctrl->fw_image = ab_pci->amss_path;
418 ret = ath12k_mhi_get_msi(ab_pci);
420 ath12k_err(ab, "failed to get msi for mhi\n");
421 goto free_controller;
424 if (!test_bit(ATH12K_PCI_FLAG_MULTI_MSI_VECTORS, &ab_pci->flags))
425 mhi_ctrl->irq_flags = IRQF_SHARED | IRQF_NOBALANCING;
427 mhi_ctrl->iova_start = 0;
428 mhi_ctrl->iova_stop = 0xffffffff;
429 mhi_ctrl->sbl_size = SZ_512K;
430 mhi_ctrl->seg_len = SZ_512K;
431 mhi_ctrl->fbc_download = true;
432 mhi_ctrl->runtime_get = ath12k_mhi_op_runtime_get;
433 mhi_ctrl->runtime_put = ath12k_mhi_op_runtime_put;
434 mhi_ctrl->status_cb = ath12k_mhi_op_status_cb;
435 mhi_ctrl->read_reg = ath12k_mhi_op_read_reg;
436 mhi_ctrl->write_reg = ath12k_mhi_op_write_reg;
438 ret = mhi_register_controller(mhi_ctrl, ab->hw_params->mhi_config);
440 ath12k_err(ab, "failed to register to mhi bus, err = %d\n", ret);
441 goto free_controller;
447 mhi_free_controller(mhi_ctrl);
448 ab_pci->mhi_ctrl = NULL;
452 void ath12k_mhi_unregister(struct ath12k_pci *ab_pci)
454 struct mhi_controller *mhi_ctrl = ab_pci->mhi_ctrl;
456 mhi_unregister_controller(mhi_ctrl);
457 kfree(mhi_ctrl->irq);
458 mhi_free_controller(mhi_ctrl);
459 ab_pci->mhi_ctrl = NULL;
462 static char *ath12k_mhi_state_to_str(enum ath12k_mhi_state mhi_state)
465 case ATH12K_MHI_INIT:
467 case ATH12K_MHI_DEINIT:
469 case ATH12K_MHI_POWER_ON:
471 case ATH12K_MHI_POWER_OFF:
473 case ATH12K_MHI_FORCE_POWER_OFF:
474 return "FORCE_POWER_OFF";
475 case ATH12K_MHI_SUSPEND:
477 case ATH12K_MHI_RESUME:
479 case ATH12K_MHI_TRIGGER_RDDM:
480 return "TRIGGER_RDDM";
481 case ATH12K_MHI_RDDM_DONE:
488 static void ath12k_mhi_set_state_bit(struct ath12k_pci *ab_pci,
489 enum ath12k_mhi_state mhi_state)
491 struct ath12k_base *ab = ab_pci->ab;
494 case ATH12K_MHI_INIT:
495 set_bit(ATH12K_MHI_INIT, &ab_pci->mhi_state);
497 case ATH12K_MHI_DEINIT:
498 clear_bit(ATH12K_MHI_INIT, &ab_pci->mhi_state);
500 case ATH12K_MHI_POWER_ON:
501 set_bit(ATH12K_MHI_POWER_ON, &ab_pci->mhi_state);
503 case ATH12K_MHI_POWER_OFF:
504 case ATH12K_MHI_FORCE_POWER_OFF:
505 clear_bit(ATH12K_MHI_POWER_ON, &ab_pci->mhi_state);
506 clear_bit(ATH12K_MHI_TRIGGER_RDDM, &ab_pci->mhi_state);
507 clear_bit(ATH12K_MHI_RDDM_DONE, &ab_pci->mhi_state);
509 case ATH12K_MHI_SUSPEND:
510 set_bit(ATH12K_MHI_SUSPEND, &ab_pci->mhi_state);
512 case ATH12K_MHI_RESUME:
513 clear_bit(ATH12K_MHI_SUSPEND, &ab_pci->mhi_state);
515 case ATH12K_MHI_TRIGGER_RDDM:
516 set_bit(ATH12K_MHI_TRIGGER_RDDM, &ab_pci->mhi_state);
518 case ATH12K_MHI_RDDM_DONE:
519 set_bit(ATH12K_MHI_RDDM_DONE, &ab_pci->mhi_state);
522 ath12k_err(ab, "unhandled mhi state (%d)\n", mhi_state);
526 static int ath12k_mhi_check_state_bit(struct ath12k_pci *ab_pci,
527 enum ath12k_mhi_state mhi_state)
529 struct ath12k_base *ab = ab_pci->ab;
532 case ATH12K_MHI_INIT:
533 if (!test_bit(ATH12K_MHI_INIT, &ab_pci->mhi_state))
536 case ATH12K_MHI_DEINIT:
537 case ATH12K_MHI_POWER_ON:
538 if (test_bit(ATH12K_MHI_INIT, &ab_pci->mhi_state) &&
539 !test_bit(ATH12K_MHI_POWER_ON, &ab_pci->mhi_state))
542 case ATH12K_MHI_FORCE_POWER_OFF:
543 if (test_bit(ATH12K_MHI_POWER_ON, &ab_pci->mhi_state))
546 case ATH12K_MHI_POWER_OFF:
547 case ATH12K_MHI_SUSPEND:
548 if (test_bit(ATH12K_MHI_POWER_ON, &ab_pci->mhi_state) &&
549 !test_bit(ATH12K_MHI_SUSPEND, &ab_pci->mhi_state))
552 case ATH12K_MHI_RESUME:
553 if (test_bit(ATH12K_MHI_SUSPEND, &ab_pci->mhi_state))
556 case ATH12K_MHI_TRIGGER_RDDM:
557 if (test_bit(ATH12K_MHI_POWER_ON, &ab_pci->mhi_state) &&
558 !test_bit(ATH12K_MHI_TRIGGER_RDDM, &ab_pci->mhi_state))
561 case ATH12K_MHI_RDDM_DONE:
564 ath12k_err(ab, "unhandled mhi state: %s(%d)\n",
565 ath12k_mhi_state_to_str(mhi_state), mhi_state);
568 ath12k_err(ab, "failed to set mhi state %s(%d) in current mhi state (0x%lx)\n",
569 ath12k_mhi_state_to_str(mhi_state), mhi_state,
575 static int ath12k_mhi_set_state(struct ath12k_pci *ab_pci,
576 enum ath12k_mhi_state mhi_state)
578 struct ath12k_base *ab = ab_pci->ab;
581 ret = ath12k_mhi_check_state_bit(ab_pci, mhi_state);
585 ath12k_dbg(ab, ATH12K_DBG_PCI, "setting mhi state: %s(%d)\n",
586 ath12k_mhi_state_to_str(mhi_state), mhi_state);
589 case ATH12K_MHI_INIT:
590 ret = mhi_prepare_for_power_up(ab_pci->mhi_ctrl);
592 case ATH12K_MHI_DEINIT:
593 mhi_unprepare_after_power_down(ab_pci->mhi_ctrl);
596 case ATH12K_MHI_POWER_ON:
597 ret = mhi_async_power_up(ab_pci->mhi_ctrl);
599 case ATH12K_MHI_POWER_OFF:
600 mhi_power_down(ab_pci->mhi_ctrl, true);
603 case ATH12K_MHI_FORCE_POWER_OFF:
604 mhi_power_down(ab_pci->mhi_ctrl, false);
607 case ATH12K_MHI_SUSPEND:
608 ret = mhi_pm_suspend(ab_pci->mhi_ctrl);
610 case ATH12K_MHI_RESUME:
611 ret = mhi_pm_resume(ab_pci->mhi_ctrl);
613 case ATH12K_MHI_TRIGGER_RDDM:
614 ret = mhi_force_rddm_mode(ab_pci->mhi_ctrl);
616 case ATH12K_MHI_RDDM_DONE:
619 ath12k_err(ab, "unhandled MHI state (%d)\n", mhi_state);
626 ath12k_mhi_set_state_bit(ab_pci, mhi_state);
631 ath12k_err(ab, "failed to set mhi state: %s(%d)\n",
632 ath12k_mhi_state_to_str(mhi_state), mhi_state);
636 int ath12k_mhi_start(struct ath12k_pci *ab_pci)
640 ab_pci->mhi_ctrl->timeout_ms = MHI_TIMEOUT_DEFAULT_MS;
642 ret = ath12k_mhi_set_state(ab_pci, ATH12K_MHI_INIT);
646 ret = ath12k_mhi_set_state(ab_pci, ATH12K_MHI_POWER_ON);
656 void ath12k_mhi_stop(struct ath12k_pci *ab_pci)
658 ath12k_mhi_set_state(ab_pci, ATH12K_MHI_POWER_OFF);
659 ath12k_mhi_set_state(ab_pci, ATH12K_MHI_DEINIT);
662 void ath12k_mhi_suspend(struct ath12k_pci *ab_pci)
664 ath12k_mhi_set_state(ab_pci, ATH12K_MHI_SUSPEND);
667 void ath12k_mhi_resume(struct ath12k_pci *ab_pci)
669 ath12k_mhi_set_state(ab_pci, ATH12K_MHI_RESUME);