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
19 #define MHI_CB_INVALID 0xff
21 static const struct mhi_channel_config ath12k_mhi_channels_qcn9274[] = {
30 .doorbell = MHI_DB_BRST_DISABLE,
32 .offload_channel = false,
33 .doorbell_mode_switch = false,
41 .dir = DMA_FROM_DEVICE,
44 .doorbell = MHI_DB_BRST_DISABLE,
46 .offload_channel = false,
47 .doorbell_mode_switch = false,
52 static struct mhi_event_config ath12k_mhi_events_qcn9274[] = {
55 .irq_moderation_ms = 0,
57 .data_type = MHI_ER_CTRL,
58 .mode = MHI_DB_BRST_DISABLE,
59 .hardware_event = false,
60 .client_managed = false,
61 .offload_channel = false,
65 .irq_moderation_ms = 1,
67 .mode = MHI_DB_BRST_DISABLE,
69 .hardware_event = false,
70 .client_managed = false,
71 .offload_channel = false,
75 const struct mhi_controller_config ath12k_mhi_config_qcn9274 = {
78 .use_bounce_buf = false,
80 .num_channels = ARRAY_SIZE(ath12k_mhi_channels_qcn9274),
81 .ch_cfg = ath12k_mhi_channels_qcn9274,
82 .num_events = ARRAY_SIZE(ath12k_mhi_events_qcn9274),
83 .event_cfg = ath12k_mhi_events_qcn9274,
86 static const struct mhi_channel_config ath12k_mhi_channels_wcn7850[] = {
95 .doorbell = MHI_DB_BRST_DISABLE,
97 .offload_channel = false,
98 .doorbell_mode_switch = false,
106 .dir = DMA_FROM_DEVICE,
109 .doorbell = MHI_DB_BRST_DISABLE,
111 .offload_channel = false,
112 .doorbell_mode_switch = false,
117 static struct mhi_event_config ath12k_mhi_events_wcn7850[] = {
120 .irq_moderation_ms = 0,
122 .mode = MHI_DB_BRST_DISABLE,
123 .data_type = MHI_ER_CTRL,
124 .hardware_event = false,
125 .client_managed = false,
126 .offload_channel = false,
130 .irq_moderation_ms = 1,
132 .mode = MHI_DB_BRST_DISABLE,
134 .hardware_event = false,
135 .client_managed = false,
136 .offload_channel = false,
140 const struct mhi_controller_config ath12k_mhi_config_wcn7850 = {
143 .use_bounce_buf = false,
145 .num_channels = ARRAY_SIZE(ath12k_mhi_channels_wcn7850),
146 .ch_cfg = ath12k_mhi_channels_wcn7850,
147 .num_events = ARRAY_SIZE(ath12k_mhi_events_wcn7850),
148 .event_cfg = ath12k_mhi_events_wcn7850,
151 void ath12k_mhi_set_mhictrl_reset(struct ath12k_base *ab)
155 val = ath12k_pci_read32(ab, MHISTATUS);
157 ath12k_dbg(ab, ATH12K_DBG_PCI, "MHISTATUS 0x%x\n", val);
159 /* Observed on some targets that after SOC_GLOBAL_RESET, MHISTATUS
160 * has SYSERR bit set and thus need to set MHICTRL_RESET
163 ath12k_pci_write32(ab, MHICTRL, MHICTRL_RESET_MASK);
168 static void ath12k_mhi_reset_txvecdb(struct ath12k_base *ab)
170 ath12k_pci_write32(ab, PCIE_TXVECDB, 0);
173 static void ath12k_mhi_reset_txvecstatus(struct ath12k_base *ab)
175 ath12k_pci_write32(ab, PCIE_TXVECSTATUS, 0);
178 static void ath12k_mhi_reset_rxvecdb(struct ath12k_base *ab)
180 ath12k_pci_write32(ab, PCIE_RXVECDB, 0);
183 static void ath12k_mhi_reset_rxvecstatus(struct ath12k_base *ab)
185 ath12k_pci_write32(ab, PCIE_RXVECSTATUS, 0);
188 void ath12k_mhi_clear_vector(struct ath12k_base *ab)
190 ath12k_mhi_reset_txvecdb(ab);
191 ath12k_mhi_reset_txvecstatus(ab);
192 ath12k_mhi_reset_rxvecdb(ab);
193 ath12k_mhi_reset_rxvecstatus(ab);
196 static int ath12k_mhi_get_msi(struct ath12k_pci *ab_pci)
198 struct ath12k_base *ab = ab_pci->ab;
199 u32 user_base_data, base_vector;
200 int ret, num_vectors, i;
202 unsigned int msi_data;
204 ret = ath12k_pci_get_user_msi_assignment(ab,
206 &user_base_data, &base_vector);
210 ath12k_dbg(ab, ATH12K_DBG_PCI, "Number of assigned MSI for MHI is %d, base vector is %d\n",
211 num_vectors, base_vector);
213 irq = kcalloc(num_vectors, sizeof(*irq), GFP_KERNEL);
217 msi_data = base_vector;
218 for (i = 0; i < num_vectors; i++) {
219 if (test_bit(ATH12K_PCI_FLAG_MULTI_MSI_VECTORS, &ab_pci->flags))
220 irq[i] = ath12k_pci_get_msi_irq(ab->dev,
223 irq[i] = ath12k_pci_get_msi_irq(ab->dev,
227 ab_pci->mhi_ctrl->irq = irq;
228 ab_pci->mhi_ctrl->nr_irqs = num_vectors;
233 static int ath12k_mhi_op_runtime_get(struct mhi_controller *mhi_cntrl)
238 static void ath12k_mhi_op_runtime_put(struct mhi_controller *mhi_cntrl)
242 static char *ath12k_mhi_op_callback_to_str(enum mhi_callback reason)
246 return "MHI_CB_IDLE";
247 case MHI_CB_PENDING_DATA:
248 return "MHI_CB_PENDING_DATA";
249 case MHI_CB_LPM_ENTER:
250 return "MHI_CB_LPM_ENTER";
251 case MHI_CB_LPM_EXIT:
252 return "MHI_CB_LPM_EXIT";
254 return "MHI_CB_EE_RDDM";
255 case MHI_CB_EE_MISSION_MODE:
256 return "MHI_CB_EE_MISSION_MODE";
257 case MHI_CB_SYS_ERROR:
258 return "MHI_CB_SYS_ERROR";
259 case MHI_CB_FATAL_ERROR:
260 return "MHI_CB_FATAL_ERROR";
262 return "MHI_CB_BW_REQ";
268 static void ath12k_mhi_op_status_cb(struct mhi_controller *mhi_cntrl,
269 enum mhi_callback cb)
271 struct ath12k_base *ab = dev_get_drvdata(mhi_cntrl->cntrl_dev);
272 struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
274 ath12k_dbg(ab, ATH12K_DBG_BOOT, "mhi notify status reason %s\n",
275 ath12k_mhi_op_callback_to_str(cb));
278 case MHI_CB_SYS_ERROR:
279 ath12k_warn(ab, "firmware crashed: MHI_CB_SYS_ERROR\n");
282 if (ab_pci->mhi_pre_cb == MHI_CB_EE_RDDM) {
283 ath12k_dbg(ab, ATH12K_DBG_BOOT,
284 "do not queue again for consecutive RDDM event\n");
288 if (!(test_bit(ATH12K_FLAG_UNREGISTERING, &ab->dev_flags)))
289 queue_work(ab->workqueue_aux, &ab->reset_work);
295 ab_pci->mhi_pre_cb = cb;
298 static int ath12k_mhi_op_read_reg(struct mhi_controller *mhi_cntrl,
307 static void ath12k_mhi_op_write_reg(struct mhi_controller *mhi_cntrl,
314 int ath12k_mhi_register(struct ath12k_pci *ab_pci)
316 struct ath12k_base *ab = ab_pci->ab;
317 struct mhi_controller *mhi_ctrl;
318 unsigned int board_id;
320 bool dualmac = false;
322 mhi_ctrl = mhi_alloc_controller();
326 ab_pci->mhi_pre_cb = MHI_CB_INVALID;
327 ab_pci->mhi_ctrl = mhi_ctrl;
328 mhi_ctrl->cntrl_dev = ab->dev;
329 mhi_ctrl->regs = ab->mem;
330 mhi_ctrl->reg_len = ab->mem_len;
331 mhi_ctrl->rddm_size = ab->hw_params->rddm_size;
333 if (ab->hw_params->otp_board_id_register) {
335 ath12k_pci_read32(ab, ab->hw_params->otp_board_id_register);
336 board_id = u32_get_bits(board_id, OTP_BOARD_ID_MASK);
338 if (!board_id || (board_id == OTP_INVALID_BOARD_ID)) {
339 ath12k_dbg(ab, ATH12K_DBG_BOOT,
340 "failed to read board id\n");
341 } else if (board_id & OTP_VALID_DUALMAC_BOARD_ID_MASK) {
343 ath12k_dbg(ab, ATH12K_DBG_BOOT,
344 "dualmac fw selected for board id: %x\n", board_id);
349 if (ab->fw.amss_dualmac_data && ab->fw.amss_dualmac_len > 0) {
350 /* use MHI firmware file from firmware-N.bin */
351 mhi_ctrl->fw_data = ab->fw.amss_dualmac_data;
352 mhi_ctrl->fw_sz = ab->fw.amss_dualmac_len;
354 ath12k_warn(ab, "dualmac firmware IE not present in firmware-N.bin\n");
356 goto free_controller;
359 if (ab->fw.amss_data && ab->fw.amss_len > 0) {
360 /* use MHI firmware file from firmware-N.bin */
361 mhi_ctrl->fw_data = ab->fw.amss_data;
362 mhi_ctrl->fw_sz = ab->fw.amss_len;
364 /* use the old separate mhi.bin MHI firmware file */
365 ath12k_core_create_firmware_path(ab, ATH12K_AMSS_FILE,
367 sizeof(ab_pci->amss_path));
368 mhi_ctrl->fw_image = ab_pci->amss_path;
372 ret = ath12k_mhi_get_msi(ab_pci);
374 ath12k_err(ab, "failed to get msi for mhi\n");
375 goto free_controller;
378 if (!test_bit(ATH12K_PCI_FLAG_MULTI_MSI_VECTORS, &ab_pci->flags))
379 mhi_ctrl->irq_flags = IRQF_SHARED | IRQF_NOBALANCING;
381 mhi_ctrl->iova_start = 0;
382 mhi_ctrl->iova_stop = 0xffffffff;
383 mhi_ctrl->sbl_size = SZ_512K;
384 mhi_ctrl->seg_len = SZ_512K;
385 mhi_ctrl->fbc_download = true;
386 mhi_ctrl->runtime_get = ath12k_mhi_op_runtime_get;
387 mhi_ctrl->runtime_put = ath12k_mhi_op_runtime_put;
388 mhi_ctrl->status_cb = ath12k_mhi_op_status_cb;
389 mhi_ctrl->read_reg = ath12k_mhi_op_read_reg;
390 mhi_ctrl->write_reg = ath12k_mhi_op_write_reg;
392 ret = mhi_register_controller(mhi_ctrl, ab->hw_params->mhi_config);
394 ath12k_err(ab, "failed to register to mhi bus, err = %d\n", ret);
395 goto free_controller;
401 mhi_free_controller(mhi_ctrl);
402 ab_pci->mhi_ctrl = NULL;
406 void ath12k_mhi_unregister(struct ath12k_pci *ab_pci)
408 struct mhi_controller *mhi_ctrl = ab_pci->mhi_ctrl;
410 mhi_unregister_controller(mhi_ctrl);
411 kfree(mhi_ctrl->irq);
412 mhi_free_controller(mhi_ctrl);
413 ab_pci->mhi_ctrl = NULL;
416 static char *ath12k_mhi_state_to_str(enum ath12k_mhi_state mhi_state)
419 case ATH12K_MHI_INIT:
421 case ATH12K_MHI_DEINIT:
423 case ATH12K_MHI_POWER_ON:
425 case ATH12K_MHI_POWER_OFF:
427 case ATH12K_MHI_POWER_OFF_KEEP_DEV:
428 return "POWER_OFF_KEEP_DEV";
429 case ATH12K_MHI_FORCE_POWER_OFF:
430 return "FORCE_POWER_OFF";
431 case ATH12K_MHI_SUSPEND:
433 case ATH12K_MHI_RESUME:
435 case ATH12K_MHI_TRIGGER_RDDM:
436 return "TRIGGER_RDDM";
437 case ATH12K_MHI_RDDM_DONE:
444 static void ath12k_mhi_set_state_bit(struct ath12k_pci *ab_pci,
445 enum ath12k_mhi_state mhi_state)
447 struct ath12k_base *ab = ab_pci->ab;
450 case ATH12K_MHI_INIT:
451 set_bit(ATH12K_MHI_INIT, &ab_pci->mhi_state);
453 case ATH12K_MHI_DEINIT:
454 clear_bit(ATH12K_MHI_INIT, &ab_pci->mhi_state);
456 case ATH12K_MHI_POWER_ON:
457 set_bit(ATH12K_MHI_POWER_ON, &ab_pci->mhi_state);
459 case ATH12K_MHI_POWER_OFF:
460 case ATH12K_MHI_POWER_OFF_KEEP_DEV:
461 case ATH12K_MHI_FORCE_POWER_OFF:
462 clear_bit(ATH12K_MHI_POWER_ON, &ab_pci->mhi_state);
463 clear_bit(ATH12K_MHI_TRIGGER_RDDM, &ab_pci->mhi_state);
464 clear_bit(ATH12K_MHI_RDDM_DONE, &ab_pci->mhi_state);
466 case ATH12K_MHI_SUSPEND:
467 set_bit(ATH12K_MHI_SUSPEND, &ab_pci->mhi_state);
469 case ATH12K_MHI_RESUME:
470 clear_bit(ATH12K_MHI_SUSPEND, &ab_pci->mhi_state);
472 case ATH12K_MHI_TRIGGER_RDDM:
473 set_bit(ATH12K_MHI_TRIGGER_RDDM, &ab_pci->mhi_state);
475 case ATH12K_MHI_RDDM_DONE:
476 set_bit(ATH12K_MHI_RDDM_DONE, &ab_pci->mhi_state);
479 ath12k_err(ab, "unhandled mhi state (%d)\n", mhi_state);
483 static int ath12k_mhi_check_state_bit(struct ath12k_pci *ab_pci,
484 enum ath12k_mhi_state mhi_state)
486 struct ath12k_base *ab = ab_pci->ab;
489 case ATH12K_MHI_INIT:
490 if (!test_bit(ATH12K_MHI_INIT, &ab_pci->mhi_state))
493 case ATH12K_MHI_DEINIT:
494 case ATH12K_MHI_POWER_ON:
495 if (test_bit(ATH12K_MHI_INIT, &ab_pci->mhi_state) &&
496 !test_bit(ATH12K_MHI_POWER_ON, &ab_pci->mhi_state))
499 case ATH12K_MHI_FORCE_POWER_OFF:
500 if (test_bit(ATH12K_MHI_POWER_ON, &ab_pci->mhi_state))
503 case ATH12K_MHI_POWER_OFF:
504 case ATH12K_MHI_POWER_OFF_KEEP_DEV:
505 case ATH12K_MHI_SUSPEND:
506 if (test_bit(ATH12K_MHI_POWER_ON, &ab_pci->mhi_state) &&
507 !test_bit(ATH12K_MHI_SUSPEND, &ab_pci->mhi_state))
510 case ATH12K_MHI_RESUME:
511 if (test_bit(ATH12K_MHI_SUSPEND, &ab_pci->mhi_state))
514 case ATH12K_MHI_TRIGGER_RDDM:
515 if (test_bit(ATH12K_MHI_POWER_ON, &ab_pci->mhi_state) &&
516 !test_bit(ATH12K_MHI_TRIGGER_RDDM, &ab_pci->mhi_state))
519 case ATH12K_MHI_RDDM_DONE:
522 ath12k_err(ab, "unhandled mhi state: %s(%d)\n",
523 ath12k_mhi_state_to_str(mhi_state), mhi_state);
526 ath12k_err(ab, "failed to set mhi state %s(%d) in current mhi state (0x%lx)\n",
527 ath12k_mhi_state_to_str(mhi_state), mhi_state,
533 static int ath12k_mhi_set_state(struct ath12k_pci *ab_pci,
534 enum ath12k_mhi_state mhi_state)
536 struct ath12k_base *ab = ab_pci->ab;
539 ret = ath12k_mhi_check_state_bit(ab_pci, mhi_state);
543 ath12k_dbg(ab, ATH12K_DBG_PCI, "setting mhi state: %s(%d)\n",
544 ath12k_mhi_state_to_str(mhi_state), mhi_state);
547 case ATH12K_MHI_INIT:
548 ret = mhi_prepare_for_power_up(ab_pci->mhi_ctrl);
550 case ATH12K_MHI_DEINIT:
551 mhi_unprepare_after_power_down(ab_pci->mhi_ctrl);
554 case ATH12K_MHI_POWER_ON:
555 /* In case of resume, QRTR's resume_early() is called
556 * right after ath12k' resume_early(). Since QRTR requires
557 * MHI mission mode state when preparing IPCR channels
558 * (see ee_mask of that channel), we need to use the 'sync'
559 * version here to make sure MHI is in that state when we
560 * return. Or QRTR might resume before that state comes,
561 * and as a result it fails.
563 * The 'sync' version works for non-resume (normal power on)
566 ret = mhi_sync_power_up(ab_pci->mhi_ctrl);
568 case ATH12K_MHI_POWER_OFF:
569 mhi_power_down(ab_pci->mhi_ctrl, true);
572 case ATH12K_MHI_POWER_OFF_KEEP_DEV:
573 mhi_power_down_keep_dev(ab_pci->mhi_ctrl, true);
576 case ATH12K_MHI_FORCE_POWER_OFF:
577 mhi_power_down(ab_pci->mhi_ctrl, false);
580 case ATH12K_MHI_SUSPEND:
581 ret = mhi_pm_suspend(ab_pci->mhi_ctrl);
583 case ATH12K_MHI_RESUME:
584 ret = mhi_pm_resume(ab_pci->mhi_ctrl);
586 case ATH12K_MHI_TRIGGER_RDDM:
587 ret = mhi_force_rddm_mode(ab_pci->mhi_ctrl);
589 case ATH12K_MHI_RDDM_DONE:
592 ath12k_err(ab, "unhandled MHI state (%d)\n", mhi_state);
599 ath12k_mhi_set_state_bit(ab_pci, mhi_state);
604 ath12k_err(ab, "failed to set mhi state: %s(%d)\n",
605 ath12k_mhi_state_to_str(mhi_state), mhi_state);
609 int ath12k_mhi_start(struct ath12k_pci *ab_pci)
613 ab_pci->mhi_ctrl->timeout_ms = MHI_TIMEOUT_DEFAULT_MS;
615 ret = ath12k_mhi_set_state(ab_pci, ATH12K_MHI_INIT);
619 ret = ath12k_mhi_set_state(ab_pci, ATH12K_MHI_POWER_ON);
629 void ath12k_mhi_stop(struct ath12k_pci *ab_pci, bool is_suspend)
631 /* During suspend we need to use mhi_power_down_keep_dev()
632 * workaround, otherwise ath12k_core_resume() will timeout
636 ath12k_mhi_set_state(ab_pci, ATH12K_MHI_POWER_OFF_KEEP_DEV);
638 ath12k_mhi_set_state(ab_pci, ATH12K_MHI_POWER_OFF);
640 ath12k_mhi_set_state(ab_pci, ATH12K_MHI_DEINIT);
643 void ath12k_mhi_suspend(struct ath12k_pci *ab_pci)
645 ath12k_mhi_set_state(ab_pci, ATH12K_MHI_SUSPEND);
648 void ath12k_mhi_resume(struct ath12k_pci *ab_pci)
650 ath12k_mhi_set_state(ab_pci, ATH12K_MHI_RESUME);
653 void ath12k_mhi_coredump(struct mhi_controller *mhi_ctrl, bool in_panic)
655 mhi_download_rddm_image(mhi_ctrl, in_panic);