1 // SPDX-License-Identifier: BSD-3-Clause-Clear
3 * Copyright (c) 2020 The Linux Foundation. All rights reserved.
4 * Copyright (c) 2021-2024 Qualcomm Innovation Center, Inc. All rights reserved.
9 #include <linux/firmware.h>
11 #include <linux/of_address.h>
12 #include <linux/ioport.h>
20 #define MHI_TIMEOUT_DEFAULT_MS 20000
21 #define RDDM_DUMP_SIZE 0x420000
23 static const struct mhi_channel_config ath11k_mhi_channels_qca6390[] = {
32 .doorbell = MHI_DB_BRST_DISABLE,
34 .offload_channel = false,
35 .doorbell_mode_switch = false,
43 .dir = DMA_FROM_DEVICE,
46 .doorbell = MHI_DB_BRST_DISABLE,
48 .offload_channel = false,
49 .doorbell_mode_switch = false,
54 static struct mhi_event_config ath11k_mhi_events_qca6390[] = {
57 .irq_moderation_ms = 0,
59 .mode = MHI_DB_BRST_DISABLE,
60 .data_type = MHI_ER_CTRL,
61 .hardware_event = false,
62 .client_managed = false,
63 .offload_channel = false,
67 .irq_moderation_ms = 1,
69 .mode = MHI_DB_BRST_DISABLE,
71 .hardware_event = false,
72 .client_managed = false,
73 .offload_channel = false,
77 static const struct mhi_controller_config ath11k_mhi_config_qca6390 = {
80 .use_bounce_buf = false,
82 .num_channels = ARRAY_SIZE(ath11k_mhi_channels_qca6390),
83 .ch_cfg = ath11k_mhi_channels_qca6390,
84 .num_events = ARRAY_SIZE(ath11k_mhi_events_qca6390),
85 .event_cfg = ath11k_mhi_events_qca6390,
88 static const struct mhi_channel_config ath11k_mhi_channels_qcn9074[] = {
97 .doorbell = MHI_DB_BRST_DISABLE,
99 .offload_channel = false,
100 .doorbell_mode_switch = false,
108 .dir = DMA_FROM_DEVICE,
111 .doorbell = MHI_DB_BRST_DISABLE,
113 .offload_channel = false,
114 .doorbell_mode_switch = false,
119 static struct mhi_event_config ath11k_mhi_events_qcn9074[] = {
122 .irq_moderation_ms = 0,
124 .data_type = MHI_ER_CTRL,
125 .mode = MHI_DB_BRST_DISABLE,
126 .hardware_event = false,
127 .client_managed = false,
128 .offload_channel = false,
132 .irq_moderation_ms = 1,
134 .mode = MHI_DB_BRST_DISABLE,
136 .hardware_event = false,
137 .client_managed = false,
138 .offload_channel = false,
142 static const struct mhi_controller_config ath11k_mhi_config_qcn9074 = {
145 .use_bounce_buf = false,
147 .num_channels = ARRAY_SIZE(ath11k_mhi_channels_qcn9074),
148 .ch_cfg = ath11k_mhi_channels_qcn9074,
149 .num_events = ARRAY_SIZE(ath11k_mhi_events_qcn9074),
150 .event_cfg = ath11k_mhi_events_qcn9074,
153 void ath11k_mhi_set_mhictrl_reset(struct ath11k_base *ab)
157 val = ath11k_pcic_read32(ab, MHISTATUS);
159 ath11k_dbg(ab, ATH11K_DBG_PCI, "mhistatus 0x%x\n", val);
161 /* Observed on QCA6390 that after SOC_GLOBAL_RESET, MHISTATUS
162 * has SYSERR bit set and thus need to set MHICTRL_RESET
165 ath11k_pcic_write32(ab, MHICTRL, MHICTRL_RESET_MASK);
170 static void ath11k_mhi_reset_txvecdb(struct ath11k_base *ab)
172 ath11k_pcic_write32(ab, PCIE_TXVECDB, 0);
175 static void ath11k_mhi_reset_txvecstatus(struct ath11k_base *ab)
177 ath11k_pcic_write32(ab, PCIE_TXVECSTATUS, 0);
180 static void ath11k_mhi_reset_rxvecdb(struct ath11k_base *ab)
182 ath11k_pcic_write32(ab, PCIE_RXVECDB, 0);
185 static void ath11k_mhi_reset_rxvecstatus(struct ath11k_base *ab)
187 ath11k_pcic_write32(ab, PCIE_RXVECSTATUS, 0);
190 void ath11k_mhi_clear_vector(struct ath11k_base *ab)
192 ath11k_mhi_reset_txvecdb(ab);
193 ath11k_mhi_reset_txvecstatus(ab);
194 ath11k_mhi_reset_rxvecdb(ab);
195 ath11k_mhi_reset_rxvecstatus(ab);
198 static int ath11k_mhi_get_msi(struct ath11k_pci *ab_pci)
200 struct ath11k_base *ab = ab_pci->ab;
201 u32 user_base_data, base_vector;
202 int ret, num_vectors, i;
204 unsigned int msi_data;
206 ret = ath11k_pcic_get_user_msi_assignment(ab, "MHI", &num_vectors,
207 &user_base_data, &base_vector);
211 ath11k_dbg(ab, ATH11K_DBG_PCI, "num_vectors %d base_vector %d\n",
212 num_vectors, base_vector);
214 irq = kcalloc(num_vectors, sizeof(int), GFP_KERNEL);
218 for (i = 0; i < num_vectors; i++) {
219 msi_data = base_vector;
221 if (test_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, &ab->dev_flags))
224 irq[i] = ath11k_pci_get_msi_irq(ab, msi_data);
227 ab_pci->mhi_ctrl->irq = irq;
228 ab_pci->mhi_ctrl->nr_irqs = num_vectors;
233 static int ath11k_mhi_op_runtime_get(struct mhi_controller *mhi_cntrl)
238 static void ath11k_mhi_op_runtime_put(struct mhi_controller *mhi_cntrl)
242 static char *ath11k_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 ath11k_mhi_op_status_cb(struct mhi_controller *mhi_cntrl,
269 enum mhi_callback cb)
271 struct ath11k_base *ab = dev_get_drvdata(mhi_cntrl->cntrl_dev);
273 ath11k_dbg(ab, ATH11K_DBG_BOOT, "notify status reason %s\n",
274 ath11k_mhi_op_callback_to_str(cb));
277 case MHI_CB_SYS_ERROR:
278 ath11k_warn(ab, "firmware crashed: MHI_CB_SYS_ERROR\n");
281 ath11k_warn(ab, "firmware crashed: MHI_CB_EE_RDDM\n");
282 if (!(test_bit(ATH11K_FLAG_UNREGISTERING, &ab->dev_flags)))
283 queue_work(ab->workqueue_aux, &ab->reset_work);
290 static int ath11k_mhi_op_read_reg(struct mhi_controller *mhi_cntrl,
299 static void ath11k_mhi_op_write_reg(struct mhi_controller *mhi_cntrl,
306 static int ath11k_mhi_read_addr_from_dt(struct mhi_controller *mhi_ctrl)
308 struct device_node *np;
312 np = of_find_node_by_type(NULL, "memory");
316 ret = of_address_to_resource(np, 0, &res);
321 mhi_ctrl->iova_start = res.start + 0x1000000;
322 mhi_ctrl->iova_stop = res.end;
327 int ath11k_mhi_register(struct ath11k_pci *ab_pci)
329 struct ath11k_base *ab = ab_pci->ab;
330 struct mhi_controller *mhi_ctrl;
331 const struct mhi_controller_config *ath11k_mhi_config;
334 mhi_ctrl = mhi_alloc_controller();
338 ab_pci->mhi_ctrl = mhi_ctrl;
339 mhi_ctrl->cntrl_dev = ab->dev;
340 mhi_ctrl->regs = ab->mem;
341 mhi_ctrl->reg_len = ab->mem_len;
343 if (ab->fw.amss_data && ab->fw.amss_len > 0) {
344 /* use MHI firmware file from firmware-N.bin */
345 mhi_ctrl->fw_data = ab->fw.amss_data;
346 mhi_ctrl->fw_sz = ab->fw.amss_len;
348 /* use the old separate mhi.bin MHI firmware file */
349 ath11k_core_create_firmware_path(ab, ATH11K_AMSS_FILE,
351 sizeof(ab_pci->amss_path));
352 mhi_ctrl->fw_image = ab_pci->amss_path;
355 ret = ath11k_mhi_get_msi(ab_pci);
357 ath11k_err(ab, "failed to get msi for mhi\n");
358 goto free_controller;
361 if (!test_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, &ab->dev_flags))
362 mhi_ctrl->irq_flags = IRQF_SHARED | IRQF_NOBALANCING;
364 if (test_bit(ATH11K_FLAG_FIXED_MEM_RGN, &ab->dev_flags)) {
365 ret = ath11k_mhi_read_addr_from_dt(mhi_ctrl);
367 goto free_controller;
369 mhi_ctrl->iova_start = 0;
370 mhi_ctrl->iova_stop = ab_pci->dma_mask;
373 mhi_ctrl->rddm_size = RDDM_DUMP_SIZE;
374 mhi_ctrl->sbl_size = SZ_512K;
375 mhi_ctrl->seg_len = SZ_512K;
376 mhi_ctrl->fbc_download = true;
377 mhi_ctrl->runtime_get = ath11k_mhi_op_runtime_get;
378 mhi_ctrl->runtime_put = ath11k_mhi_op_runtime_put;
379 mhi_ctrl->status_cb = ath11k_mhi_op_status_cb;
380 mhi_ctrl->read_reg = ath11k_mhi_op_read_reg;
381 mhi_ctrl->write_reg = ath11k_mhi_op_write_reg;
383 switch (ab->hw_rev) {
384 case ATH11K_HW_QCN9074_HW10:
385 ath11k_mhi_config = &ath11k_mhi_config_qcn9074;
387 case ATH11K_HW_QCA6390_HW20:
388 case ATH11K_HW_WCN6855_HW20:
389 case ATH11K_HW_WCN6855_HW21:
390 case ATH11K_HW_QCA2066_HW21:
391 ath11k_mhi_config = &ath11k_mhi_config_qca6390;
394 ath11k_err(ab, "failed assign mhi_config for unknown hw rev %d\n",
397 goto free_controller;
400 ret = mhi_register_controller(mhi_ctrl, ath11k_mhi_config);
402 ath11k_err(ab, "failed to register to mhi bus, err = %d\n", ret);
403 goto free_controller;
409 mhi_free_controller(mhi_ctrl);
410 ab_pci->mhi_ctrl = NULL;
414 void ath11k_mhi_unregister(struct ath11k_pci *ab_pci)
416 struct mhi_controller *mhi_ctrl = ab_pci->mhi_ctrl;
418 mhi_unregister_controller(mhi_ctrl);
419 kfree(mhi_ctrl->irq);
420 mhi_free_controller(mhi_ctrl);
423 int ath11k_mhi_start(struct ath11k_pci *ab_pci)
425 struct ath11k_base *ab = ab_pci->ab;
428 ab_pci->mhi_ctrl->timeout_ms = MHI_TIMEOUT_DEFAULT_MS;
430 ret = mhi_prepare_for_power_up(ab_pci->mhi_ctrl);
432 ath11k_warn(ab, "failed to prepare mhi: %d", ret);
436 ret = mhi_sync_power_up(ab_pci->mhi_ctrl);
438 ath11k_warn(ab, "failed to power up mhi: %d", ret);
445 void ath11k_mhi_stop(struct ath11k_pci *ab_pci)
447 mhi_power_down(ab_pci->mhi_ctrl, true);
448 mhi_unprepare_after_power_down(ab_pci->mhi_ctrl);
451 int ath11k_mhi_suspend(struct ath11k_pci *ab_pci)
453 struct ath11k_base *ab = ab_pci->ab;
456 ret = mhi_pm_suspend(ab_pci->mhi_ctrl);
458 ath11k_warn(ab, "failed to suspend mhi: %d", ret);
465 int ath11k_mhi_resume(struct ath11k_pci *ab_pci)
467 struct ath11k_base *ab = ab_pci->ab;
470 /* Do force MHI resume as some devices like QCA6390, WCN6855
471 * are not in M3 state but they are functional. So just ignore
472 * the MHI state while resuming.
474 ret = mhi_pm_resume_force(ab_pci->mhi_ctrl);
476 ath11k_warn(ab, "failed to resume mhi: %d", ret);