1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /* Copyright (c) 2020 The Linux Foundation. All rights reserved. */
11 #define MHI_TIMEOUT_DEFAULT_MS 90000
13 static struct mhi_channel_config ath11k_mhi_channels
[] = {
22 .doorbell
= MHI_DB_BRST_DISABLE
,
24 .offload_channel
= false,
25 .doorbell_mode_switch
= false,
33 .dir
= DMA_FROM_DEVICE
,
36 .doorbell
= MHI_DB_BRST_DISABLE
,
38 .offload_channel
= false,
39 .doorbell_mode_switch
= false,
50 .doorbell
= MHI_DB_BRST_DISABLE
,
52 .offload_channel
= false,
53 .doorbell_mode_switch
= false,
61 .dir
= DMA_FROM_DEVICE
,
64 .doorbell
= MHI_DB_BRST_DISABLE
,
66 .offload_channel
= false,
67 .doorbell_mode_switch
= false,
72 static struct mhi_event_config ath11k_mhi_events
[] = {
75 .irq_moderation_ms
= 0,
77 .mode
= MHI_DB_BRST_DISABLE
,
78 .data_type
= MHI_ER_CTRL
,
79 .hardware_event
= false,
80 .client_managed
= false,
81 .offload_channel
= false,
85 .irq_moderation_ms
= 1,
87 .mode
= MHI_DB_BRST_DISABLE
,
89 .hardware_event
= false,
90 .client_managed
= false,
91 .offload_channel
= false,
95 static struct mhi_controller_config ath11k_mhi_config
= {
98 .use_bounce_buf
= false,
100 .num_channels
= ARRAY_SIZE(ath11k_mhi_channels
),
101 .ch_cfg
= ath11k_mhi_channels
,
102 .num_events
= ARRAY_SIZE(ath11k_mhi_events
),
103 .event_cfg
= ath11k_mhi_events
,
106 void ath11k_mhi_set_mhictrl_reset(struct ath11k_base
*ab
)
110 val
= ath11k_pci_read32(ab
, MHISTATUS
);
112 ath11k_dbg(ab
, ATH11K_DBG_PCI
, "MHISTATUS 0x%x\n", val
);
114 /* Observed on QCA6390 that after SOC_GLOBAL_RESET, MHISTATUS
115 * has SYSERR bit set and thus need to set MHICTRL_RESET
118 ath11k_pci_write32(ab
, MHICTRL
, MHICTRL_RESET_MASK
);
123 static void ath11k_mhi_reset_txvecdb(struct ath11k_base
*ab
)
125 ath11k_pci_write32(ab
, PCIE_TXVECDB
, 0);
128 static void ath11k_mhi_reset_txvecstatus(struct ath11k_base
*ab
)
130 ath11k_pci_write32(ab
, PCIE_TXVECSTATUS
, 0);
133 static void ath11k_mhi_reset_rxvecdb(struct ath11k_base
*ab
)
135 ath11k_pci_write32(ab
, PCIE_RXVECDB
, 0);
138 static void ath11k_mhi_reset_rxvecstatus(struct ath11k_base
*ab
)
140 ath11k_pci_write32(ab
, PCIE_RXVECSTATUS
, 0);
143 void ath11k_mhi_clear_vector(struct ath11k_base
*ab
)
145 ath11k_mhi_reset_txvecdb(ab
);
146 ath11k_mhi_reset_txvecstatus(ab
);
147 ath11k_mhi_reset_rxvecdb(ab
);
148 ath11k_mhi_reset_rxvecstatus(ab
);
151 static int ath11k_mhi_get_msi(struct ath11k_pci
*ab_pci
)
153 struct ath11k_base
*ab
= ab_pci
->ab
;
154 u32 user_base_data
, base_vector
;
155 int ret
, num_vectors
, i
;
158 ret
= ath11k_pci_get_user_msi_assignment(ab_pci
,
160 &user_base_data
, &base_vector
);
164 ath11k_dbg(ab
, ATH11K_DBG_PCI
, "Number of assigned MSI for MHI is %d, base vector is %d\n",
165 num_vectors
, base_vector
);
167 irq
= kcalloc(num_vectors
, sizeof(int), GFP_KERNEL
);
171 for (i
= 0; i
< num_vectors
; i
++)
172 irq
[i
] = ath11k_pci_get_msi_irq(ab
->dev
,
175 ab_pci
->mhi_ctrl
->irq
= irq
;
176 ab_pci
->mhi_ctrl
->nr_irqs
= num_vectors
;
181 static int ath11k_mhi_op_runtime_get(struct mhi_controller
*mhi_cntrl
)
186 static void ath11k_mhi_op_runtime_put(struct mhi_controller
*mhi_cntrl
)
190 static void ath11k_mhi_op_status_cb(struct mhi_controller
*mhi_cntrl
,
191 enum mhi_callback cb
)
193 struct ath11k_base
*ab
= dev_get_drvdata(mhi_cntrl
->cntrl_dev
);
196 case MHI_CB_SYS_ERROR
:
197 ath11k_warn(ab
, "firmware crashed: MHI_CB_SYS_ERROR\n");
204 static int ath11k_mhi_op_read_reg(struct mhi_controller
*mhi_cntrl
,
213 static void ath11k_mhi_op_write_reg(struct mhi_controller
*mhi_cntrl
,
220 int ath11k_mhi_register(struct ath11k_pci
*ab_pci
)
222 struct ath11k_base
*ab
= ab_pci
->ab
;
223 struct mhi_controller
*mhi_ctrl
;
226 mhi_ctrl
= mhi_alloc_controller();
230 ath11k_core_create_firmware_path(ab
, ATH11K_AMSS_FILE
,
232 sizeof(ab_pci
->amss_path
));
234 ab_pci
->mhi_ctrl
= mhi_ctrl
;
235 mhi_ctrl
->cntrl_dev
= ab
->dev
;
236 mhi_ctrl
->fw_image
= ab_pci
->amss_path
;
237 mhi_ctrl
->regs
= ab
->mem
;
239 ret
= ath11k_mhi_get_msi(ab_pci
);
241 ath11k_err(ab
, "failed to get msi for mhi\n");
242 mhi_free_controller(mhi_ctrl
);
246 mhi_ctrl
->iova_start
= 0;
247 mhi_ctrl
->iova_stop
= 0xffffffff;
248 mhi_ctrl
->sbl_size
= SZ_512K
;
249 mhi_ctrl
->seg_len
= SZ_512K
;
250 mhi_ctrl
->fbc_download
= true;
251 mhi_ctrl
->runtime_get
= ath11k_mhi_op_runtime_get
;
252 mhi_ctrl
->runtime_put
= ath11k_mhi_op_runtime_put
;
253 mhi_ctrl
->status_cb
= ath11k_mhi_op_status_cb
;
254 mhi_ctrl
->read_reg
= ath11k_mhi_op_read_reg
;
255 mhi_ctrl
->write_reg
= ath11k_mhi_op_write_reg
;
257 ret
= mhi_register_controller(mhi_ctrl
, &ath11k_mhi_config
);
259 ath11k_err(ab
, "failed to register to mhi bus, err = %d\n", ret
);
260 mhi_free_controller(mhi_ctrl
);
267 void ath11k_mhi_unregister(struct ath11k_pci
*ab_pci
)
269 struct mhi_controller
*mhi_ctrl
= ab_pci
->mhi_ctrl
;
271 mhi_unregister_controller(mhi_ctrl
);
272 kfree(mhi_ctrl
->irq
);
273 mhi_free_controller(mhi_ctrl
);
276 static char *ath11k_mhi_state_to_str(enum ath11k_mhi_state mhi_state
)
279 case ATH11K_MHI_INIT
:
281 case ATH11K_MHI_DEINIT
:
283 case ATH11K_MHI_POWER_ON
:
285 case ATH11K_MHI_POWER_OFF
:
287 case ATH11K_MHI_FORCE_POWER_OFF
:
288 return "FORCE_POWER_OFF";
289 case ATH11K_MHI_SUSPEND
:
291 case ATH11K_MHI_RESUME
:
293 case ATH11K_MHI_TRIGGER_RDDM
:
294 return "TRIGGER_RDDM";
295 case ATH11K_MHI_RDDM_DONE
:
302 static void ath11k_mhi_set_state_bit(struct ath11k_pci
*ab_pci
,
303 enum ath11k_mhi_state mhi_state
)
305 struct ath11k_base
*ab
= ab_pci
->ab
;
308 case ATH11K_MHI_INIT
:
309 set_bit(ATH11K_MHI_INIT
, &ab_pci
->mhi_state
);
311 case ATH11K_MHI_DEINIT
:
312 clear_bit(ATH11K_MHI_INIT
, &ab_pci
->mhi_state
);
314 case ATH11K_MHI_POWER_ON
:
315 set_bit(ATH11K_MHI_POWER_ON
, &ab_pci
->mhi_state
);
317 case ATH11K_MHI_POWER_OFF
:
318 case ATH11K_MHI_FORCE_POWER_OFF
:
319 clear_bit(ATH11K_MHI_POWER_ON
, &ab_pci
->mhi_state
);
320 clear_bit(ATH11K_MHI_TRIGGER_RDDM
, &ab_pci
->mhi_state
);
321 clear_bit(ATH11K_MHI_RDDM_DONE
, &ab_pci
->mhi_state
);
323 case ATH11K_MHI_SUSPEND
:
324 set_bit(ATH11K_MHI_SUSPEND
, &ab_pci
->mhi_state
);
326 case ATH11K_MHI_RESUME
:
327 clear_bit(ATH11K_MHI_SUSPEND
, &ab_pci
->mhi_state
);
329 case ATH11K_MHI_TRIGGER_RDDM
:
330 set_bit(ATH11K_MHI_TRIGGER_RDDM
, &ab_pci
->mhi_state
);
332 case ATH11K_MHI_RDDM_DONE
:
333 set_bit(ATH11K_MHI_RDDM_DONE
, &ab_pci
->mhi_state
);
336 ath11k_err(ab
, "unhandled mhi state (%d)\n", mhi_state
);
340 static int ath11k_mhi_check_state_bit(struct ath11k_pci
*ab_pci
,
341 enum ath11k_mhi_state mhi_state
)
343 struct ath11k_base
*ab
= ab_pci
->ab
;
346 case ATH11K_MHI_INIT
:
347 if (!test_bit(ATH11K_MHI_INIT
, &ab_pci
->mhi_state
))
350 case ATH11K_MHI_DEINIT
:
351 case ATH11K_MHI_POWER_ON
:
352 if (test_bit(ATH11K_MHI_INIT
, &ab_pci
->mhi_state
) &&
353 !test_bit(ATH11K_MHI_POWER_ON
, &ab_pci
->mhi_state
))
356 case ATH11K_MHI_FORCE_POWER_OFF
:
357 if (test_bit(ATH11K_MHI_POWER_ON
, &ab_pci
->mhi_state
))
360 case ATH11K_MHI_POWER_OFF
:
361 case ATH11K_MHI_SUSPEND
:
362 if (test_bit(ATH11K_MHI_POWER_ON
, &ab_pci
->mhi_state
) &&
363 !test_bit(ATH11K_MHI_SUSPEND
, &ab_pci
->mhi_state
))
366 case ATH11K_MHI_RESUME
:
367 if (test_bit(ATH11K_MHI_SUSPEND
, &ab_pci
->mhi_state
))
370 case ATH11K_MHI_TRIGGER_RDDM
:
371 if (test_bit(ATH11K_MHI_POWER_ON
, &ab_pci
->mhi_state
) &&
372 !test_bit(ATH11K_MHI_TRIGGER_RDDM
, &ab_pci
->mhi_state
))
375 case ATH11K_MHI_RDDM_DONE
:
378 ath11k_err(ab
, "unhandled mhi state: %s(%d)\n",
379 ath11k_mhi_state_to_str(mhi_state
), mhi_state
);
382 ath11k_err(ab
, "failed to set mhi state %s(%d) in current mhi state (0x%lx)\n",
383 ath11k_mhi_state_to_str(mhi_state
), mhi_state
,
389 static int ath11k_mhi_set_state(struct ath11k_pci
*ab_pci
,
390 enum ath11k_mhi_state mhi_state
)
392 struct ath11k_base
*ab
= ab_pci
->ab
;
395 ret
= ath11k_mhi_check_state_bit(ab_pci
, mhi_state
);
399 ath11k_dbg(ab
, ATH11K_DBG_PCI
, "setting mhi state: %s(%d)\n",
400 ath11k_mhi_state_to_str(mhi_state
), mhi_state
);
403 case ATH11K_MHI_INIT
:
404 ret
= mhi_prepare_for_power_up(ab_pci
->mhi_ctrl
);
406 case ATH11K_MHI_DEINIT
:
407 mhi_unprepare_after_power_down(ab_pci
->mhi_ctrl
);
410 case ATH11K_MHI_POWER_ON
:
411 ret
= mhi_async_power_up(ab_pci
->mhi_ctrl
);
413 case ATH11K_MHI_POWER_OFF
:
414 mhi_power_down(ab_pci
->mhi_ctrl
, true);
417 case ATH11K_MHI_FORCE_POWER_OFF
:
418 mhi_power_down(ab_pci
->mhi_ctrl
, false);
421 case ATH11K_MHI_SUSPEND
:
422 ret
= mhi_pm_suspend(ab_pci
->mhi_ctrl
);
424 case ATH11K_MHI_RESUME
:
425 ret
= mhi_pm_resume(ab_pci
->mhi_ctrl
);
427 case ATH11K_MHI_TRIGGER_RDDM
:
428 ret
= mhi_force_rddm_mode(ab_pci
->mhi_ctrl
);
430 case ATH11K_MHI_RDDM_DONE
:
433 ath11k_err(ab
, "unhandled MHI state (%d)\n", mhi_state
);
440 ath11k_mhi_set_state_bit(ab_pci
, mhi_state
);
445 ath11k_err(ab
, "failed to set mhi state: %s(%d)\n",
446 ath11k_mhi_state_to_str(mhi_state
), mhi_state
);
450 int ath11k_mhi_start(struct ath11k_pci
*ab_pci
)
454 ab_pci
->mhi_ctrl
->timeout_ms
= MHI_TIMEOUT_DEFAULT_MS
;
456 ret
= ath11k_mhi_set_state(ab_pci
, ATH11K_MHI_INIT
);
460 ret
= ath11k_mhi_set_state(ab_pci
, ATH11K_MHI_POWER_ON
);
470 void ath11k_mhi_stop(struct ath11k_pci
*ab_pci
)
472 ath11k_mhi_set_state(ab_pci
, ATH11K_MHI_POWER_OFF
);
473 ath11k_mhi_set_state(ab_pci
, ATH11K_MHI_DEINIT
);
476 void ath11k_mhi_suspend(struct ath11k_pci
*ab_pci
)
478 ath11k_mhi_set_state(ab_pci
, ATH11K_MHI_SUSPEND
);
481 void ath11k_mhi_resume(struct ath11k_pci
*ab_pci
)
483 ath11k_mhi_set_state(ab_pci
, ATH11K_MHI_RESUME
);