1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2021, MediaTek Inc.
4 * Copyright (c) 2021-2022, Intel Corporation.
7 * Haijun Liu <haijun.liu@mediatek.com>
8 * Eliot Lee <eliot.lee@intel.com>
9 * Moises Veleta <moises.veleta@intel.com>
10 * Ricardo Martinez <ricardo.martinez@linux.intel.com>
13 * Amir Hanania <amir.hanania@intel.com>
14 * Chiranjeevi Rapolu <chiranjeevi.rapolu@intel.com>
15 * Sreehari Kancharla <sreehari.kancharla@intel.com>
18 #include <linux/acpi.h>
19 #include <linux/bits.h>
20 #include <linux/bitfield.h>
21 #include <linux/device.h>
22 #include <linux/delay.h>
23 #include <linux/gfp.h>
25 #include <linux/irqreturn.h>
26 #include <linux/kthread.h>
27 #include <linux/skbuff.h>
28 #include <linux/spinlock.h>
29 #include <linux/string.h>
30 #include <linux/types.h>
31 #include <linux/wait.h>
32 #include <linux/workqueue.h>
34 #include "t7xx_cldma.h"
35 #include "t7xx_hif_cldma.h"
36 #include "t7xx_mhccif.h"
37 #include "t7xx_modem_ops.h"
38 #include "t7xx_netdev.h"
40 #include "t7xx_pcie_mac.h"
41 #include "t7xx_port.h"
42 #include "t7xx_port_proxy.h"
44 #include "t7xx_state_monitor.h"
46 #define RT_ID_MD_PORT_ENUM 0
47 #define RT_ID_AP_PORT_ENUM 1
48 /* Modem feature query identification code - "ICCC" */
49 #define MD_FEATURE_QUERY_ID 0x49434343
51 #define FEATURE_VER GENMASK(7, 4)
52 #define FEATURE_MSK GENMASK(3, 0)
54 #define RGU_RESET_DELAY_MS 10
55 #define PORT_RESET_DELAY_MS 2000
56 #define EX_HS_TIMEOUT_MS 5000
57 #define EX_HS_POLL_DELAY_MS 10
59 enum mtk_feature_support_type {
60 MTK_FEATURE_DOES_NOT_EXIST,
61 MTK_FEATURE_NOT_SUPPORTED,
62 MTK_FEATURE_MUST_BE_SUPPORTED,
65 static unsigned int t7xx_get_interrupt_status(struct t7xx_pci_dev *t7xx_dev)
67 return t7xx_mhccif_read_sw_int_sts(t7xx_dev) & D2H_SW_INT_MASK;
71 * t7xx_pci_mhccif_isr() - Process MHCCIF interrupts.
72 * @t7xx_dev: MTK device.
74 * Check the interrupt status and queue commands accordingly.
78 ** -EINVAL - Failure to get FSM control.
80 int t7xx_pci_mhccif_isr(struct t7xx_pci_dev *t7xx_dev)
82 struct t7xx_modem *md = t7xx_dev->md;
83 struct t7xx_fsm_ctl *ctl;
90 dev_err_ratelimited(&t7xx_dev->pdev->dev,
91 "MHCCIF interrupt received before initializing MD monitor\n");
95 spin_lock_bh(&md->exp_lock);
96 int_sta = t7xx_get_interrupt_status(t7xx_dev);
97 md->exp_id |= int_sta;
98 if (md->exp_id & D2H_INT_EXCEPTION_INIT) {
99 if (ctl->md_state == MD_STATE_INVALID ||
100 ctl->md_state == MD_STATE_WAITING_FOR_HS1 ||
101 ctl->md_state == MD_STATE_WAITING_FOR_HS2 ||
102 ctl->md_state == MD_STATE_READY) {
103 md->exp_id &= ~D2H_INT_EXCEPTION_INIT;
104 ret = t7xx_fsm_recv_md_intr(ctl, MD_IRQ_CCIF_EX);
106 } else if (md->exp_id & D2H_INT_PORT_ENUM) {
107 md->exp_id &= ~D2H_INT_PORT_ENUM;
109 if (ctl->curr_state == FSM_STATE_INIT || ctl->curr_state == FSM_STATE_PRE_START ||
110 ctl->curr_state == FSM_STATE_STOPPED)
111 ret = t7xx_fsm_recv_md_intr(ctl, MD_IRQ_PORT_ENUM);
112 } else if (ctl->md_state == MD_STATE_WAITING_FOR_HS1) {
113 mask = t7xx_mhccif_mask_get(t7xx_dev);
114 if ((md->exp_id & D2H_INT_ASYNC_MD_HK) && !(mask & D2H_INT_ASYNC_MD_HK)) {
115 md->exp_id &= ~D2H_INT_ASYNC_MD_HK;
116 queue_work(md->handshake_wq, &md->handshake_work);
119 spin_unlock_bh(&md->exp_lock);
124 static void t7xx_clr_device_irq_via_pcie(struct t7xx_pci_dev *t7xx_dev)
126 struct t7xx_addr_base *pbase_addr = &t7xx_dev->base_addr;
127 void __iomem *reset_pcie_reg;
130 reset_pcie_reg = pbase_addr->pcie_ext_reg_base + TOPRGU_CH_PCIE_IRQ_STA -
131 pbase_addr->pcie_dev_reg_trsl_addr;
132 val = ioread32(reset_pcie_reg);
133 iowrite32(val, reset_pcie_reg);
136 void t7xx_clear_rgu_irq(struct t7xx_pci_dev *t7xx_dev)
139 t7xx_clr_device_irq_via_pcie(t7xx_dev);
141 t7xx_pcie_mac_clear_int_status(t7xx_dev, SAP_RGU_INT);
144 static int t7xx_acpi_reset(struct t7xx_pci_dev *t7xx_dev, char *fn_name)
147 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
148 struct device *dev = &t7xx_dev->pdev->dev;
149 acpi_status acpi_ret;
152 handle = ACPI_HANDLE(dev);
154 dev_err(dev, "ACPI handle not found\n");
158 if (!acpi_has_method(handle, fn_name)) {
159 dev_err(dev, "%s method not found\n", fn_name);
163 acpi_ret = acpi_evaluate_object(handle, fn_name, NULL, &buffer);
164 if (ACPI_FAILURE(acpi_ret)) {
165 dev_err(dev, "%s method fail: %s\n", fn_name, acpi_format_exception(acpi_ret));
169 kfree(buffer.pointer);
175 int t7xx_acpi_fldr_func(struct t7xx_pci_dev *t7xx_dev)
177 return t7xx_acpi_reset(t7xx_dev, "_RST");
180 static void t7xx_reset_device_via_pmic(struct t7xx_pci_dev *t7xx_dev)
184 val = ioread32(IREG_BASE(t7xx_dev) + T7XX_PCIE_MISC_DEV_STATUS);
185 if (val & MISC_RESET_TYPE_PLDR)
186 t7xx_acpi_reset(t7xx_dev, "MRST._RST");
187 else if (val & MISC_RESET_TYPE_FLDR)
188 t7xx_acpi_fldr_func(t7xx_dev);
191 static irqreturn_t t7xx_rgu_isr_thread(int irq, void *data)
193 struct t7xx_pci_dev *t7xx_dev = data;
195 msleep(RGU_RESET_DELAY_MS);
196 t7xx_reset_device_via_pmic(t7xx_dev);
200 static irqreturn_t t7xx_rgu_isr_handler(int irq, void *data)
202 struct t7xx_pci_dev *t7xx_dev = data;
203 struct t7xx_modem *modem;
205 t7xx_clear_rgu_irq(t7xx_dev);
206 if (!t7xx_dev->rgu_pci_irq_en)
209 modem = t7xx_dev->md;
210 modem->rgu_irq_asserted = true;
211 t7xx_pcie_mac_clear_int(t7xx_dev, SAP_RGU_INT);
212 return IRQ_WAKE_THREAD;
215 static void t7xx_pcie_register_rgu_isr(struct t7xx_pci_dev *t7xx_dev)
217 /* Registers RGU callback ISR with PCIe driver */
218 t7xx_pcie_mac_clear_int(t7xx_dev, SAP_RGU_INT);
219 t7xx_pcie_mac_clear_int_status(t7xx_dev, SAP_RGU_INT);
221 t7xx_dev->intr_handler[SAP_RGU_INT] = t7xx_rgu_isr_handler;
222 t7xx_dev->intr_thread[SAP_RGU_INT] = t7xx_rgu_isr_thread;
223 t7xx_dev->callback_param[SAP_RGU_INT] = t7xx_dev;
224 t7xx_pcie_mac_set_int(t7xx_dev, SAP_RGU_INT);
228 * t7xx_cldma_exception() - CLDMA exception handler.
229 * @md_ctrl: modem control struct.
230 * @stage: exception stage.
232 * Part of the modem exception recovery.
233 * Stages are one after the other as describe below:
234 * HIF_EX_INIT: Disable and clear TXQ.
235 * HIF_EX_CLEARQ_DONE: Disable RX, flush TX/RX workqueues and clear RX.
236 * HIF_EX_ALLQ_RESET: HW is back in safe mode for re-initialization and restart.
239 /* Modem Exception Handshake Flow
241 * Modem HW Exception interrupt received
244 * +---------v--------+
245 * | HIF_EX_INIT | : Disable and clear TXQ
246 * +------------------+
248 * +---------v--------+
249 * | HIF_EX_INIT_DONE | : Wait for the init to be done
250 * +------------------+
252 * +---------v--------+
253 * |HIF_EX_CLEARQ_DONE| : Disable and clear RXQ
254 * +------------------+ : Flush TX/RX workqueues
256 * +---------v--------+
257 * |HIF_EX_ALLQ_RESET | : Restart HW and CLDMA
258 * +------------------+
260 static void t7xx_cldma_exception(struct cldma_ctrl *md_ctrl, enum hif_ex_stage stage)
264 t7xx_cldma_stop_all_qs(md_ctrl, MTK_TX);
265 t7xx_cldma_clear_all_qs(md_ctrl, MTK_TX);
268 case HIF_EX_CLEARQ_DONE:
269 /* We do not want to get CLDMA IRQ when MD is
270 * resetting CLDMA after it got clearq_ack.
272 t7xx_cldma_stop_all_qs(md_ctrl, MTK_RX);
273 t7xx_cldma_stop(md_ctrl);
275 if (md_ctrl->hif_id == CLDMA_ID_MD)
276 t7xx_cldma_hw_reset(md_ctrl->t7xx_dev->base_addr.infracfg_ao_base);
278 t7xx_cldma_clear_all_qs(md_ctrl, MTK_RX);
281 case HIF_EX_ALLQ_RESET:
282 t7xx_cldma_hw_init(&md_ctrl->hw_info);
283 t7xx_cldma_start(md_ctrl);
291 static void t7xx_md_exception(struct t7xx_modem *md, enum hif_ex_stage stage)
293 struct t7xx_pci_dev *t7xx_dev = md->t7xx_dev;
295 if (stage == HIF_EX_CLEARQ_DONE) {
296 /* Give DHL time to flush data */
297 msleep(PORT_RESET_DELAY_MS);
298 t7xx_port_proxy_reset(md->port_prox);
301 t7xx_cldma_exception(md->md_ctrl[CLDMA_ID_MD], stage);
302 t7xx_cldma_exception(md->md_ctrl[CLDMA_ID_AP], stage);
304 if (stage == HIF_EX_INIT)
305 t7xx_mhccif_h2d_swint_trigger(t7xx_dev, H2D_CH_EXCEPTION_ACK);
306 else if (stage == HIF_EX_CLEARQ_DONE)
307 t7xx_mhccif_h2d_swint_trigger(t7xx_dev, H2D_CH_EXCEPTION_CLEARQ_ACK);
310 static int t7xx_wait_hif_ex_hk_event(struct t7xx_modem *md, int event_id)
312 unsigned int waited_time_ms = 0;
315 if (md->exp_id & event_id)
318 waited_time_ms += EX_HS_POLL_DELAY_MS;
319 msleep(EX_HS_POLL_DELAY_MS);
320 } while (waited_time_ms < EX_HS_TIMEOUT_MS);
325 static void t7xx_md_sys_sw_init(struct t7xx_pci_dev *t7xx_dev)
327 /* Register the MHCCIF ISR for MD exception, port enum and
328 * async handshake notifications.
330 t7xx_mhccif_mask_set(t7xx_dev, D2H_SW_INT_MASK);
331 t7xx_mhccif_mask_clr(t7xx_dev, D2H_INT_PORT_ENUM);
333 /* Register RGU IRQ handler for sAP exception notification */
334 t7xx_dev->rgu_pci_irq_en = true;
335 t7xx_pcie_register_rgu_isr(t7xx_dev);
338 struct feature_query {
340 u8 feature_set[FEATURE_COUNT];
344 static void t7xx_prepare_host_rt_data_query(struct t7xx_sys_info *core)
346 struct feature_query *ft_query;
349 skb = t7xx_ctrl_alloc_skb(sizeof(*ft_query));
353 ft_query = skb_put(skb, sizeof(*ft_query));
354 ft_query->head_pattern = cpu_to_le32(MD_FEATURE_QUERY_ID);
355 memcpy(ft_query->feature_set, core->feature_set, FEATURE_COUNT);
356 ft_query->tail_pattern = cpu_to_le32(MD_FEATURE_QUERY_ID);
358 /* Send HS1 message to device */
359 t7xx_port_send_ctl_skb(core->ctl_port, skb, CTL_ID_HS1_MSG, 0);
362 static int t7xx_prepare_device_rt_data(struct t7xx_sys_info *core, struct device *dev,
365 struct feature_query *md_feature = data;
366 struct mtk_runtime_feature *rt_feature;
367 unsigned int i, rt_data_len = 0;
370 /* Parse MD runtime data query */
371 if (le32_to_cpu(md_feature->head_pattern) != MD_FEATURE_QUERY_ID ||
372 le32_to_cpu(md_feature->tail_pattern) != MD_FEATURE_QUERY_ID) {
373 dev_err(dev, "Invalid feature pattern: head 0x%x, tail 0x%x\n",
374 le32_to_cpu(md_feature->head_pattern),
375 le32_to_cpu(md_feature->tail_pattern));
379 for (i = 0; i < FEATURE_COUNT; i++) {
380 if (FIELD_GET(FEATURE_MSK, md_feature->feature_set[i]) !=
381 MTK_FEATURE_MUST_BE_SUPPORTED)
382 rt_data_len += sizeof(*rt_feature);
385 skb = t7xx_ctrl_alloc_skb(rt_data_len);
389 rt_feature = skb_put(skb, rt_data_len);
390 memset(rt_feature, 0, rt_data_len);
392 /* Fill runtime feature */
393 for (i = 0; i < FEATURE_COUNT; i++) {
394 u8 md_feature_mask = FIELD_GET(FEATURE_MSK, md_feature->feature_set[i]);
396 if (md_feature_mask == MTK_FEATURE_MUST_BE_SUPPORTED)
399 rt_feature->feature_id = i;
400 if (md_feature_mask == MTK_FEATURE_DOES_NOT_EXIST)
401 rt_feature->support_info = md_feature->feature_set[i];
406 /* Send HS3 message to device */
407 t7xx_port_send_ctl_skb(core->ctl_port, skb, CTL_ID_HS3_MSG, 0);
411 static int t7xx_parse_host_rt_data(struct t7xx_fsm_ctl *ctl, struct t7xx_sys_info *core,
412 struct device *dev, void *data, int data_length)
414 enum mtk_feature_support_type ft_spt_st, ft_spt_cfg;
415 struct mtk_runtime_feature *rt_feature;
418 offset = sizeof(struct feature_query);
419 for (i = 0; i < FEATURE_COUNT && offset < data_length; i++) {
420 rt_feature = data + offset;
421 offset += sizeof(*rt_feature) + le32_to_cpu(rt_feature->data_len);
423 ft_spt_cfg = FIELD_GET(FEATURE_MSK, core->feature_set[i]);
424 if (ft_spt_cfg != MTK_FEATURE_MUST_BE_SUPPORTED)
427 ft_spt_st = FIELD_GET(FEATURE_MSK, rt_feature->support_info);
428 if (ft_spt_st != MTK_FEATURE_MUST_BE_SUPPORTED)
431 if (i == RT_ID_MD_PORT_ENUM || i == RT_ID_AP_PORT_ENUM)
432 t7xx_port_enum_msg_handler(ctl->md, rt_feature->data);
438 static int t7xx_core_reset(struct t7xx_modem *md)
440 struct device *dev = &md->t7xx_dev->pdev->dev;
441 struct t7xx_fsm_ctl *ctl = md->fsm_ctl;
443 md->core_md.ready = false;
446 dev_err(dev, "FSM is not initialized\n");
450 if (md->core_md.handshake_ongoing) {
451 int ret = t7xx_fsm_append_event(ctl, FSM_EVENT_MD_HS2_EXIT, NULL, 0);
457 md->core_md.handshake_ongoing = false;
461 static void t7xx_core_hk_handler(struct t7xx_modem *md, struct t7xx_sys_info *core_info,
462 struct t7xx_fsm_ctl *ctl,
463 enum t7xx_fsm_event_state event_id,
464 enum t7xx_fsm_event_state err_detect)
466 struct t7xx_fsm_event *event = NULL, *event_next;
467 struct device *dev = &md->t7xx_dev->pdev->dev;
471 t7xx_prepare_host_rt_data_query(core_info);
473 while (!kthread_should_stop()) {
474 bool event_received = false;
476 spin_lock_irqsave(&ctl->event_lock, flags);
477 list_for_each_entry_safe(event, event_next, &ctl->event_queue, entry) {
478 if (event->event_id == err_detect) {
479 list_del(&event->entry);
480 spin_unlock_irqrestore(&ctl->event_lock, flags);
481 dev_err(dev, "Core handshake error event received\n");
483 } else if (event->event_id == event_id) {
484 list_del(&event->entry);
485 event_received = true;
489 spin_unlock_irqrestore(&ctl->event_lock, flags);
494 wait_event_interruptible(ctl->event_wq, !list_empty(&ctl->event_queue) ||
495 kthread_should_stop());
496 if (kthread_should_stop())
500 if (!event || ctl->exp_flg)
503 ret = t7xx_parse_host_rt_data(ctl, core_info, dev, event->data, event->length);
505 dev_err(dev, "Host failure parsing runtime data: %d\n", ret);
512 ret = t7xx_prepare_device_rt_data(core_info, dev, event->data);
514 dev_err(dev, "Device failure parsing runtime data: %d", ret);
518 core_info->ready = true;
519 core_info->handshake_ongoing = false;
520 wake_up(&ctl->async_hk_wq);
525 static void t7xx_md_hk_wq(struct work_struct *work)
527 struct t7xx_modem *md = container_of(work, struct t7xx_modem, handshake_work);
528 struct t7xx_fsm_ctl *ctl = md->fsm_ctl;
530 /* Clear the HS2 EXIT event appended in core_reset() */
531 t7xx_fsm_clr_event(ctl, FSM_EVENT_MD_HS2_EXIT);
532 t7xx_cldma_switch_cfg(md->md_ctrl[CLDMA_ID_MD]);
533 t7xx_cldma_start(md->md_ctrl[CLDMA_ID_MD]);
534 t7xx_fsm_broadcast_state(ctl, MD_STATE_WAITING_FOR_HS2);
535 md->core_md.handshake_ongoing = true;
536 t7xx_core_hk_handler(md, &md->core_md, ctl, FSM_EVENT_MD_HS2, FSM_EVENT_MD_HS2_EXIT);
539 static void t7xx_ap_hk_wq(struct work_struct *work)
541 struct t7xx_modem *md = container_of(work, struct t7xx_modem, ap_handshake_work);
542 struct t7xx_fsm_ctl *ctl = md->fsm_ctl;
544 /* Clear the HS2 EXIT event appended in t7xx_core_reset(). */
545 t7xx_fsm_clr_event(ctl, FSM_EVENT_AP_HS2_EXIT);
546 t7xx_cldma_stop(md->md_ctrl[CLDMA_ID_AP]);
547 t7xx_cldma_switch_cfg(md->md_ctrl[CLDMA_ID_AP]);
548 t7xx_cldma_start(md->md_ctrl[CLDMA_ID_AP]);
549 md->core_ap.handshake_ongoing = true;
550 t7xx_core_hk_handler(md, &md->core_ap, ctl, FSM_EVENT_AP_HS2, FSM_EVENT_AP_HS2_EXIT);
553 void t7xx_md_event_notify(struct t7xx_modem *md, enum md_event_id evt_id)
555 struct t7xx_fsm_ctl *ctl = md->fsm_ctl;
556 unsigned int int_sta;
561 t7xx_mhccif_mask_clr(md->t7xx_dev, D2H_INT_PORT_ENUM | D2H_INT_ASYNC_MD_HK |
562 D2H_INT_ASYNC_AP_HK);
566 t7xx_mhccif_mask_set(md->t7xx_dev, D2H_INT_PORT_ENUM);
568 spin_lock_irqsave(&md->exp_lock, flags);
569 int_sta = t7xx_get_interrupt_status(md->t7xx_dev);
570 md->exp_id |= int_sta;
571 if (md->exp_id & D2H_INT_EXCEPTION_INIT) {
573 md->exp_id &= ~D2H_INT_EXCEPTION_INIT;
574 md->exp_id &= ~D2H_INT_ASYNC_MD_HK;
575 md->exp_id &= ~D2H_INT_ASYNC_AP_HK;
576 } else if (ctl->exp_flg) {
577 md->exp_id &= ~D2H_INT_ASYNC_MD_HK;
578 md->exp_id &= ~D2H_INT_ASYNC_AP_HK;
580 void __iomem *mhccif_base = md->t7xx_dev->base_addr.mhccif_rc_base;
582 if (md->exp_id & D2H_INT_ASYNC_MD_HK) {
583 queue_work(md->handshake_wq, &md->handshake_work);
584 md->exp_id &= ~D2H_INT_ASYNC_MD_HK;
585 iowrite32(D2H_INT_ASYNC_MD_HK, mhccif_base + REG_EP2RC_SW_INT_ACK);
586 t7xx_mhccif_mask_set(md->t7xx_dev, D2H_INT_ASYNC_MD_HK);
589 if (md->exp_id & D2H_INT_ASYNC_AP_HK) {
590 queue_work(md->handshake_wq, &md->ap_handshake_work);
591 md->exp_id &= ~D2H_INT_ASYNC_AP_HK;
592 iowrite32(D2H_INT_ASYNC_AP_HK, mhccif_base + REG_EP2RC_SW_INT_ACK);
593 t7xx_mhccif_mask_set(md->t7xx_dev, D2H_INT_ASYNC_AP_HK);
596 spin_unlock_irqrestore(&md->exp_lock, flags);
598 t7xx_mhccif_mask_clr(md->t7xx_dev,
599 D2H_INT_EXCEPTION_INIT |
600 D2H_INT_EXCEPTION_INIT_DONE |
601 D2H_INT_EXCEPTION_CLEARQ_DONE |
602 D2H_INT_EXCEPTION_ALLQ_RESET);
606 t7xx_mhccif_mask_set(md->t7xx_dev, D2H_INT_ASYNC_MD_HK);
607 t7xx_mhccif_mask_set(md->t7xx_dev, D2H_INT_ASYNC_AP_HK);
615 void t7xx_md_exception_handshake(struct t7xx_modem *md)
617 struct device *dev = &md->t7xx_dev->pdev->dev;
620 t7xx_md_exception(md, HIF_EX_INIT);
621 ret = t7xx_wait_hif_ex_hk_event(md, D2H_INT_EXCEPTION_INIT_DONE);
623 dev_err(dev, "EX CCIF HS timeout, RCH 0x%lx\n", D2H_INT_EXCEPTION_INIT_DONE);
625 t7xx_md_exception(md, HIF_EX_INIT_DONE);
626 ret = t7xx_wait_hif_ex_hk_event(md, D2H_INT_EXCEPTION_CLEARQ_DONE);
628 dev_err(dev, "EX CCIF HS timeout, RCH 0x%lx\n", D2H_INT_EXCEPTION_CLEARQ_DONE);
630 t7xx_md_exception(md, HIF_EX_CLEARQ_DONE);
631 ret = t7xx_wait_hif_ex_hk_event(md, D2H_INT_EXCEPTION_ALLQ_RESET);
633 dev_err(dev, "EX CCIF HS timeout, RCH 0x%lx\n", D2H_INT_EXCEPTION_ALLQ_RESET);
635 t7xx_md_exception(md, HIF_EX_ALLQ_RESET);
638 static struct t7xx_modem *t7xx_md_alloc(struct t7xx_pci_dev *t7xx_dev)
640 struct device *dev = &t7xx_dev->pdev->dev;
641 struct t7xx_modem *md;
643 md = devm_kzalloc(dev, sizeof(*md), GFP_KERNEL);
647 md->t7xx_dev = t7xx_dev;
649 spin_lock_init(&md->exp_lock);
650 md->handshake_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_HIGHPRI,
652 if (!md->handshake_wq)
655 INIT_WORK(&md->handshake_work, t7xx_md_hk_wq);
656 md->core_md.feature_set[RT_ID_MD_PORT_ENUM] &= ~FEATURE_MSK;
657 md->core_md.feature_set[RT_ID_MD_PORT_ENUM] |=
658 FIELD_PREP(FEATURE_MSK, MTK_FEATURE_MUST_BE_SUPPORTED);
660 INIT_WORK(&md->ap_handshake_work, t7xx_ap_hk_wq);
661 md->core_ap.feature_set[RT_ID_AP_PORT_ENUM] &= ~FEATURE_MSK;
662 md->core_ap.feature_set[RT_ID_AP_PORT_ENUM] |=
663 FIELD_PREP(FEATURE_MSK, MTK_FEATURE_MUST_BE_SUPPORTED);
668 int t7xx_md_reset(struct t7xx_pci_dev *t7xx_dev)
670 struct t7xx_modem *md = t7xx_dev->md;
672 md->md_init_finish = false;
675 t7xx_cldma_reset(md->md_ctrl[CLDMA_ID_MD]);
676 t7xx_cldma_reset(md->md_ctrl[CLDMA_ID_AP]);
677 t7xx_port_proxy_reset(md->port_prox);
678 md->md_init_finish = true;
679 return t7xx_core_reset(md);
683 * t7xx_md_init() - Initialize modem.
684 * @t7xx_dev: MTK device.
686 * Allocate and initialize MD control block, and initialize data path.
687 * Register MHCCIF ISR and RGU ISR, and start the state machine.
691 ** -ENOMEM - Allocation failure.
693 int t7xx_md_init(struct t7xx_pci_dev *t7xx_dev)
695 struct t7xx_modem *md;
698 md = t7xx_md_alloc(t7xx_dev);
702 ret = t7xx_cldma_alloc(CLDMA_ID_MD, t7xx_dev);
704 goto err_destroy_hswq;
706 ret = t7xx_cldma_alloc(CLDMA_ID_AP, t7xx_dev);
708 goto err_destroy_hswq;
710 ret = t7xx_fsm_init(md);
712 goto err_destroy_hswq;
714 ret = t7xx_ccmni_init(t7xx_dev);
718 ret = t7xx_cldma_init(md->md_ctrl[CLDMA_ID_MD]);
720 goto err_uninit_ccmni;
722 ret = t7xx_cldma_init(md->md_ctrl[CLDMA_ID_AP]);
724 goto err_uninit_md_cldma;
726 ret = t7xx_port_proxy_init(md);
728 goto err_uninit_ap_cldma;
730 ret = t7xx_fsm_append_cmd(md->fsm_ctl, FSM_CMD_START, 0);
731 if (ret) /* t7xx_fsm_uninit() flushes cmd queue */
732 goto err_uninit_proxy;
734 t7xx_md_sys_sw_init(t7xx_dev);
735 md->md_init_finish = true;
739 t7xx_port_proxy_uninit(md->port_prox);
742 t7xx_cldma_exit(md->md_ctrl[CLDMA_ID_AP]);
745 t7xx_cldma_exit(md->md_ctrl[CLDMA_ID_MD]);
748 t7xx_ccmni_exit(t7xx_dev);
754 destroy_workqueue(md->handshake_wq);
755 dev_err(&t7xx_dev->pdev->dev, "Modem init failed\n");
759 void t7xx_md_exit(struct t7xx_pci_dev *t7xx_dev)
761 struct t7xx_modem *md = t7xx_dev->md;
763 t7xx_pcie_mac_clear_int(t7xx_dev, SAP_RGU_INT);
765 if (!md->md_init_finish)
768 t7xx_fsm_append_cmd(md->fsm_ctl, FSM_CMD_PRE_STOP, FSM_CMD_FLAG_WAIT_FOR_COMPLETION);
769 t7xx_port_proxy_uninit(md->port_prox);
770 t7xx_cldma_exit(md->md_ctrl[CLDMA_ID_AP]);
771 t7xx_cldma_exit(md->md_ctrl[CLDMA_ID_MD]);
772 t7xx_ccmni_exit(t7xx_dev);
774 destroy_workqueue(md->handshake_wq);