1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2021, MediaTek Inc.
4 * Copyright (c) 2021-2022, Intel Corporation.
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 FASTBOOT_RESET_DELAY_MS 2000
57 #define EX_HS_TIMEOUT_MS 5000
58 #define EX_HS_POLL_DELAY_MS 10
60 enum mtk_feature_support_type {
61 MTK_FEATURE_DOES_NOT_EXIST,
62 MTK_FEATURE_NOT_SUPPORTED,
63 MTK_FEATURE_MUST_BE_SUPPORTED,
66 static unsigned int t7xx_get_interrupt_status(struct t7xx_pci_dev *t7xx_dev)
68 return t7xx_mhccif_read_sw_int_sts(t7xx_dev) & D2H_SW_INT_MASK;
72 * t7xx_pci_mhccif_isr() - Process MHCCIF interrupts.
73 * @t7xx_dev: MTK device.
75 * Check the interrupt status and queue commands accordingly.
79 ** -EINVAL - Failure to get FSM control.
81 int t7xx_pci_mhccif_isr(struct t7xx_pci_dev *t7xx_dev)
83 struct t7xx_modem *md = t7xx_dev->md;
84 struct t7xx_fsm_ctl *ctl;
91 dev_err_ratelimited(&t7xx_dev->pdev->dev,
92 "MHCCIF interrupt received before initializing MD monitor\n");
96 spin_lock_bh(&md->exp_lock);
97 int_sta = t7xx_get_interrupt_status(t7xx_dev);
98 md->exp_id |= int_sta;
99 if (md->exp_id & D2H_INT_EXCEPTION_INIT) {
100 if (ctl->md_state == MD_STATE_INVALID ||
101 ctl->md_state == MD_STATE_WAITING_FOR_HS1 ||
102 ctl->md_state == MD_STATE_WAITING_FOR_HS2 ||
103 ctl->md_state == MD_STATE_READY) {
104 md->exp_id &= ~D2H_INT_EXCEPTION_INIT;
105 ret = t7xx_fsm_recv_md_intr(ctl, MD_IRQ_CCIF_EX);
107 } else if (md->exp_id & D2H_INT_PORT_ENUM) {
108 md->exp_id &= ~D2H_INT_PORT_ENUM;
110 if (ctl->curr_state == FSM_STATE_INIT || ctl->curr_state == FSM_STATE_PRE_START ||
111 ctl->curr_state == FSM_STATE_STOPPED)
112 ret = t7xx_fsm_recv_md_intr(ctl, MD_IRQ_PORT_ENUM);
113 } else if (ctl->md_state == MD_STATE_WAITING_FOR_HS1) {
114 mask = t7xx_mhccif_mask_get(t7xx_dev);
115 if ((md->exp_id & D2H_INT_ASYNC_MD_HK) && !(mask & D2H_INT_ASYNC_MD_HK)) {
116 md->exp_id &= ~D2H_INT_ASYNC_MD_HK;
117 queue_work(md->handshake_wq, &md->handshake_work);
120 spin_unlock_bh(&md->exp_lock);
125 static void t7xx_clr_device_irq_via_pcie(struct t7xx_pci_dev *t7xx_dev)
127 struct t7xx_addr_base *pbase_addr = &t7xx_dev->base_addr;
128 void __iomem *reset_pcie_reg;
131 reset_pcie_reg = pbase_addr->pcie_ext_reg_base + TOPRGU_CH_PCIE_IRQ_STA -
132 pbase_addr->pcie_dev_reg_trsl_addr;
133 val = ioread32(reset_pcie_reg);
134 iowrite32(val, reset_pcie_reg);
137 void t7xx_clear_rgu_irq(struct t7xx_pci_dev *t7xx_dev)
140 t7xx_clr_device_irq_via_pcie(t7xx_dev);
142 t7xx_pcie_mac_clear_int_status(t7xx_dev, SAP_RGU_INT);
145 static int t7xx_acpi_reset(struct t7xx_pci_dev *t7xx_dev, char *fn_name)
148 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
149 struct device *dev = &t7xx_dev->pdev->dev;
150 acpi_status acpi_ret;
153 handle = ACPI_HANDLE(dev);
155 dev_err(dev, "ACPI handle not found\n");
159 if (!acpi_has_method(handle, fn_name)) {
160 dev_err(dev, "%s method not found\n", fn_name);
164 acpi_ret = acpi_evaluate_object(handle, fn_name, NULL, &buffer);
165 if (ACPI_FAILURE(acpi_ret)) {
166 dev_err(dev, "%s method fail: %s\n", fn_name, acpi_format_exception(acpi_ret));
170 kfree(buffer.pointer);
172 struct device *dev = &t7xx_dev->pdev->dev;
175 ret = pci_reset_function(t7xx_dev->pdev);
177 dev_err(dev, "Failed to reset device, error:%d\n", ret);
184 static void t7xx_host_event_notify(struct t7xx_pci_dev *t7xx_dev, unsigned int event_id)
188 value = ioread32(IREG_BASE(t7xx_dev) + T7XX_PCIE_MISC_DEV_STATUS);
189 value &= ~HOST_EVENT_MASK;
190 value |= FIELD_PREP(HOST_EVENT_MASK, event_id);
191 iowrite32(value, IREG_BASE(t7xx_dev) + T7XX_PCIE_MISC_DEV_STATUS);
194 int t7xx_reset_device(struct t7xx_pci_dev *t7xx_dev, enum reset_type type)
198 pci_save_state(t7xx_dev->pdev);
199 t7xx_pci_reprobe_early(t7xx_dev);
200 t7xx_mode_update(t7xx_dev, T7XX_RESET);
201 WRITE_ONCE(t7xx_dev->debug_ports_show, false);
204 ret = t7xx_acpi_reset(t7xx_dev, "_RST");
205 } else if (type == PLDR) {
206 ret = t7xx_acpi_reset(t7xx_dev, "MRST._RST");
207 } else if (type == FASTBOOT) {
208 t7xx_host_event_notify(t7xx_dev, FASTBOOT_DL_NOTIFY);
209 t7xx_mhccif_h2d_swint_trigger(t7xx_dev, H2D_CH_DEVICE_RESET);
210 msleep(FASTBOOT_RESET_DELAY_MS);
213 pci_restore_state(t7xx_dev->pdev);
217 return t7xx_pci_reprobe(t7xx_dev, true);
220 static void t7xx_reset_device_via_pmic(struct t7xx_pci_dev *t7xx_dev)
224 val = ioread32(IREG_BASE(t7xx_dev) + T7XX_PCIE_MISC_DEV_STATUS);
225 if (val & MISC_RESET_TYPE_PLDR)
226 t7xx_reset_device(t7xx_dev, PLDR);
227 else if (val & MISC_RESET_TYPE_FLDR)
228 t7xx_reset_device(t7xx_dev, FLDR);
231 static irqreturn_t t7xx_rgu_isr_thread(int irq, void *data)
233 struct t7xx_pci_dev *t7xx_dev = data;
235 msleep(RGU_RESET_DELAY_MS);
236 t7xx_reset_device_via_pmic(t7xx_dev);
240 static irqreturn_t t7xx_rgu_isr_handler(int irq, void *data)
242 struct t7xx_pci_dev *t7xx_dev = data;
243 struct t7xx_modem *modem;
245 t7xx_clear_rgu_irq(t7xx_dev);
246 if (!t7xx_dev->rgu_pci_irq_en)
249 modem = t7xx_dev->md;
250 modem->rgu_irq_asserted = true;
251 t7xx_pcie_mac_clear_int(t7xx_dev, SAP_RGU_INT);
252 return IRQ_WAKE_THREAD;
255 static void t7xx_pcie_register_rgu_isr(struct t7xx_pci_dev *t7xx_dev)
257 /* Registers RGU callback ISR with PCIe driver */
258 t7xx_pcie_mac_clear_int(t7xx_dev, SAP_RGU_INT);
259 t7xx_pcie_mac_clear_int_status(t7xx_dev, SAP_RGU_INT);
261 t7xx_dev->intr_handler[SAP_RGU_INT] = t7xx_rgu_isr_handler;
262 t7xx_dev->intr_thread[SAP_RGU_INT] = t7xx_rgu_isr_thread;
263 t7xx_dev->callback_param[SAP_RGU_INT] = t7xx_dev;
264 t7xx_pcie_mac_set_int(t7xx_dev, SAP_RGU_INT);
268 * t7xx_cldma_exception() - CLDMA exception handler.
269 * @md_ctrl: modem control struct.
270 * @stage: exception stage.
272 * Part of the modem exception recovery.
273 * Stages are one after the other as describe below:
274 * HIF_EX_INIT: Disable and clear TXQ.
275 * HIF_EX_CLEARQ_DONE: Disable RX, flush TX/RX workqueues and clear RX.
276 * HIF_EX_ALLQ_RESET: HW is back in safe mode for re-initialization and restart.
279 /* Modem Exception Handshake Flow
281 * Modem HW Exception interrupt received
284 * +---------v--------+
285 * | HIF_EX_INIT | : Disable and clear TXQ
286 * +------------------+
288 * +---------v--------+
289 * | HIF_EX_INIT_DONE | : Wait for the init to be done
290 * +------------------+
292 * +---------v--------+
293 * |HIF_EX_CLEARQ_DONE| : Disable and clear RXQ
294 * +------------------+ : Flush TX/RX workqueues
296 * +---------v--------+
297 * |HIF_EX_ALLQ_RESET | : Restart HW and CLDMA
298 * +------------------+
300 static void t7xx_cldma_exception(struct cldma_ctrl *md_ctrl, enum hif_ex_stage stage)
304 t7xx_cldma_stop_all_qs(md_ctrl, MTK_TX);
305 t7xx_cldma_clear_all_qs(md_ctrl, MTK_TX);
308 case HIF_EX_CLEARQ_DONE:
309 /* We do not want to get CLDMA IRQ when MD is
310 * resetting CLDMA after it got clearq_ack.
312 t7xx_cldma_stop_all_qs(md_ctrl, MTK_RX);
313 t7xx_cldma_stop(md_ctrl);
315 if (md_ctrl->hif_id == CLDMA_ID_MD)
316 t7xx_cldma_hw_reset(md_ctrl->t7xx_dev->base_addr.infracfg_ao_base);
318 t7xx_cldma_clear_all_qs(md_ctrl, MTK_RX);
321 case HIF_EX_ALLQ_RESET:
322 t7xx_cldma_hw_init(&md_ctrl->hw_info);
323 t7xx_cldma_start(md_ctrl);
331 static void t7xx_md_exception(struct t7xx_modem *md, enum hif_ex_stage stage)
333 struct t7xx_pci_dev *t7xx_dev = md->t7xx_dev;
335 if (stage == HIF_EX_CLEARQ_DONE) {
336 /* Give DHL time to flush data */
337 msleep(PORT_RESET_DELAY_MS);
338 t7xx_port_proxy_reset(md->port_prox);
341 t7xx_cldma_exception(md->md_ctrl[CLDMA_ID_MD], stage);
342 t7xx_cldma_exception(md->md_ctrl[CLDMA_ID_AP], stage);
344 if (stage == HIF_EX_INIT)
345 t7xx_mhccif_h2d_swint_trigger(t7xx_dev, H2D_CH_EXCEPTION_ACK);
346 else if (stage == HIF_EX_CLEARQ_DONE)
347 t7xx_mhccif_h2d_swint_trigger(t7xx_dev, H2D_CH_EXCEPTION_CLEARQ_ACK);
350 static int t7xx_wait_hif_ex_hk_event(struct t7xx_modem *md, int event_id)
352 unsigned int waited_time_ms = 0;
355 if (md->exp_id & event_id)
358 waited_time_ms += EX_HS_POLL_DELAY_MS;
359 msleep(EX_HS_POLL_DELAY_MS);
360 } while (waited_time_ms < EX_HS_TIMEOUT_MS);
365 static void t7xx_md_sys_sw_init(struct t7xx_pci_dev *t7xx_dev)
367 /* Register the MHCCIF ISR for MD exception, port enum and
368 * async handshake notifications.
370 t7xx_mhccif_mask_set(t7xx_dev, D2H_SW_INT_MASK);
371 t7xx_mhccif_mask_clr(t7xx_dev, D2H_INT_PORT_ENUM);
373 /* Register RGU IRQ handler for sAP exception notification */
374 t7xx_dev->rgu_pci_irq_en = true;
375 t7xx_pcie_register_rgu_isr(t7xx_dev);
378 struct feature_query {
380 u8 feature_set[FEATURE_COUNT];
384 static void t7xx_prepare_host_rt_data_query(struct t7xx_sys_info *core)
386 struct feature_query *ft_query;
389 skb = t7xx_ctrl_alloc_skb(sizeof(*ft_query));
393 ft_query = skb_put(skb, sizeof(*ft_query));
394 ft_query->head_pattern = cpu_to_le32(MD_FEATURE_QUERY_ID);
395 memcpy(ft_query->feature_set, core->feature_set, FEATURE_COUNT);
396 ft_query->tail_pattern = cpu_to_le32(MD_FEATURE_QUERY_ID);
398 /* Send HS1 message to device */
399 t7xx_port_send_ctl_skb(core->ctl_port, skb, CTL_ID_HS1_MSG, 0);
402 static int t7xx_prepare_device_rt_data(struct t7xx_sys_info *core, struct device *dev,
405 struct feature_query *md_feature = data;
406 struct mtk_runtime_feature *rt_feature;
407 unsigned int i, rt_data_len = 0;
410 /* Parse MD runtime data query */
411 if (le32_to_cpu(md_feature->head_pattern) != MD_FEATURE_QUERY_ID ||
412 le32_to_cpu(md_feature->tail_pattern) != MD_FEATURE_QUERY_ID) {
413 dev_err(dev, "Invalid feature pattern: head 0x%x, tail 0x%x\n",
414 le32_to_cpu(md_feature->head_pattern),
415 le32_to_cpu(md_feature->tail_pattern));
419 for (i = 0; i < FEATURE_COUNT; i++) {
420 if (FIELD_GET(FEATURE_MSK, md_feature->feature_set[i]) !=
421 MTK_FEATURE_MUST_BE_SUPPORTED)
422 rt_data_len += sizeof(*rt_feature);
425 skb = t7xx_ctrl_alloc_skb(rt_data_len);
429 rt_feature = skb_put(skb, rt_data_len);
430 memset(rt_feature, 0, rt_data_len);
432 /* Fill runtime feature */
433 for (i = 0; i < FEATURE_COUNT; i++) {
434 u8 md_feature_mask = FIELD_GET(FEATURE_MSK, md_feature->feature_set[i]);
436 if (md_feature_mask == MTK_FEATURE_MUST_BE_SUPPORTED)
439 rt_feature->feature_id = i;
440 if (md_feature_mask == MTK_FEATURE_DOES_NOT_EXIST)
441 rt_feature->support_info = md_feature->feature_set[i];
446 /* Send HS3 message to device */
447 t7xx_port_send_ctl_skb(core->ctl_port, skb, CTL_ID_HS3_MSG, 0);
451 static int t7xx_parse_host_rt_data(struct t7xx_fsm_ctl *ctl, struct t7xx_sys_info *core,
452 struct device *dev, void *data, int data_length)
454 enum mtk_feature_support_type ft_spt_st, ft_spt_cfg;
455 struct mtk_runtime_feature *rt_feature;
458 offset = sizeof(struct feature_query);
459 for (i = 0; i < FEATURE_COUNT && offset < data_length; i++) {
460 rt_feature = data + offset;
461 offset += sizeof(*rt_feature) + le32_to_cpu(rt_feature->data_len);
463 ft_spt_cfg = FIELD_GET(FEATURE_MSK, core->feature_set[i]);
464 if (ft_spt_cfg != MTK_FEATURE_MUST_BE_SUPPORTED)
467 ft_spt_st = FIELD_GET(FEATURE_MSK, rt_feature->support_info);
468 if (ft_spt_st != MTK_FEATURE_MUST_BE_SUPPORTED)
471 if (i == RT_ID_MD_PORT_ENUM || i == RT_ID_AP_PORT_ENUM)
472 t7xx_port_enum_msg_handler(ctl->md, rt_feature->data);
478 static int t7xx_core_reset(struct t7xx_modem *md)
480 struct device *dev = &md->t7xx_dev->pdev->dev;
481 struct t7xx_fsm_ctl *ctl = md->fsm_ctl;
483 md->core_md.ready = false;
486 dev_err(dev, "FSM is not initialized\n");
490 if (md->core_md.handshake_ongoing) {
491 int ret = t7xx_fsm_append_event(ctl, FSM_EVENT_MD_HS2_EXIT, NULL, 0);
497 md->core_md.handshake_ongoing = false;
501 static void t7xx_core_hk_handler(struct t7xx_modem *md, struct t7xx_sys_info *core_info,
502 struct t7xx_fsm_ctl *ctl,
503 enum t7xx_fsm_event_state event_id,
504 enum t7xx_fsm_event_state err_detect)
506 struct t7xx_fsm_event *event = NULL, *event_next;
507 struct device *dev = &md->t7xx_dev->pdev->dev;
511 t7xx_prepare_host_rt_data_query(core_info);
513 while (!kthread_should_stop()) {
514 bool event_received = false;
516 spin_lock_irqsave(&ctl->event_lock, flags);
517 list_for_each_entry_safe(event, event_next, &ctl->event_queue, entry) {
518 if (event->event_id == err_detect) {
519 list_del(&event->entry);
520 spin_unlock_irqrestore(&ctl->event_lock, flags);
521 dev_err(dev, "Core handshake error event received\n");
523 } else if (event->event_id == event_id) {
524 list_del(&event->entry);
525 event_received = true;
529 spin_unlock_irqrestore(&ctl->event_lock, flags);
534 wait_event_interruptible(ctl->event_wq, !list_empty(&ctl->event_queue) ||
535 kthread_should_stop());
536 if (kthread_should_stop())
540 if (!event || ctl->exp_flg)
543 ret = t7xx_parse_host_rt_data(ctl, core_info, dev, event->data, event->length);
545 dev_err(dev, "Host failure parsing runtime data: %d\n", ret);
552 ret = t7xx_prepare_device_rt_data(core_info, dev, event->data);
554 dev_err(dev, "Device failure parsing runtime data: %d", ret);
558 core_info->ready = true;
559 core_info->handshake_ongoing = false;
560 wake_up(&ctl->async_hk_wq);
565 static void t7xx_md_hk_wq(struct work_struct *work)
567 struct t7xx_modem *md = container_of(work, struct t7xx_modem, handshake_work);
568 struct t7xx_fsm_ctl *ctl = md->fsm_ctl;
570 /* Clear the HS2 EXIT event appended in core_reset() */
571 t7xx_fsm_clr_event(ctl, FSM_EVENT_MD_HS2_EXIT);
572 t7xx_cldma_switch_cfg(md->md_ctrl[CLDMA_ID_MD], CLDMA_SHARED_Q_CFG);
573 t7xx_cldma_start(md->md_ctrl[CLDMA_ID_MD]);
574 t7xx_fsm_broadcast_state(ctl, MD_STATE_WAITING_FOR_HS2);
575 md->core_md.handshake_ongoing = true;
576 t7xx_core_hk_handler(md, &md->core_md, ctl, FSM_EVENT_MD_HS2, FSM_EVENT_MD_HS2_EXIT);
579 static void t7xx_ap_hk_wq(struct work_struct *work)
581 struct t7xx_modem *md = container_of(work, struct t7xx_modem, ap_handshake_work);
582 struct t7xx_fsm_ctl *ctl = md->fsm_ctl;
584 /* Clear the HS2 EXIT event appended in t7xx_core_reset(). */
585 t7xx_fsm_clr_event(ctl, FSM_EVENT_AP_HS2_EXIT);
586 t7xx_cldma_stop(md->md_ctrl[CLDMA_ID_AP]);
587 t7xx_cldma_switch_cfg(md->md_ctrl[CLDMA_ID_AP], CLDMA_SHARED_Q_CFG);
588 t7xx_cldma_start(md->md_ctrl[CLDMA_ID_AP]);
589 md->core_ap.handshake_ongoing = true;
590 t7xx_core_hk_handler(md, &md->core_ap, ctl, FSM_EVENT_AP_HS2, FSM_EVENT_AP_HS2_EXIT);
593 void t7xx_md_event_notify(struct t7xx_modem *md, enum md_event_id evt_id)
595 struct t7xx_fsm_ctl *ctl = md->fsm_ctl;
596 unsigned int int_sta;
601 t7xx_mhccif_mask_clr(md->t7xx_dev, D2H_INT_PORT_ENUM | D2H_INT_ASYNC_MD_HK |
602 D2H_INT_ASYNC_AP_HK);
606 t7xx_mhccif_mask_set(md->t7xx_dev, D2H_INT_PORT_ENUM);
608 spin_lock_irqsave(&md->exp_lock, flags);
609 int_sta = t7xx_get_interrupt_status(md->t7xx_dev);
610 md->exp_id |= int_sta;
611 if (md->exp_id & D2H_INT_EXCEPTION_INIT) {
613 md->exp_id &= ~D2H_INT_EXCEPTION_INIT;
614 md->exp_id &= ~D2H_INT_ASYNC_MD_HK;
615 md->exp_id &= ~D2H_INT_ASYNC_AP_HK;
616 } else if (ctl->exp_flg) {
617 md->exp_id &= ~D2H_INT_ASYNC_MD_HK;
618 md->exp_id &= ~D2H_INT_ASYNC_AP_HK;
620 void __iomem *mhccif_base = md->t7xx_dev->base_addr.mhccif_rc_base;
622 if (md->exp_id & D2H_INT_ASYNC_MD_HK) {
623 queue_work(md->handshake_wq, &md->handshake_work);
624 md->exp_id &= ~D2H_INT_ASYNC_MD_HK;
625 iowrite32(D2H_INT_ASYNC_MD_HK, mhccif_base + REG_EP2RC_SW_INT_ACK);
626 t7xx_mhccif_mask_set(md->t7xx_dev, D2H_INT_ASYNC_MD_HK);
629 if (md->exp_id & D2H_INT_ASYNC_AP_HK) {
630 queue_work(md->handshake_wq, &md->ap_handshake_work);
631 md->exp_id &= ~D2H_INT_ASYNC_AP_HK;
632 iowrite32(D2H_INT_ASYNC_AP_HK, mhccif_base + REG_EP2RC_SW_INT_ACK);
633 t7xx_mhccif_mask_set(md->t7xx_dev, D2H_INT_ASYNC_AP_HK);
636 spin_unlock_irqrestore(&md->exp_lock, flags);
638 t7xx_mhccif_mask_clr(md->t7xx_dev,
639 D2H_INT_EXCEPTION_INIT |
640 D2H_INT_EXCEPTION_INIT_DONE |
641 D2H_INT_EXCEPTION_CLEARQ_DONE |
642 D2H_INT_EXCEPTION_ALLQ_RESET);
646 t7xx_mhccif_mask_set(md->t7xx_dev, D2H_INT_ASYNC_MD_HK);
647 t7xx_mhccif_mask_set(md->t7xx_dev, D2H_INT_ASYNC_AP_HK);
655 void t7xx_md_exception_handshake(struct t7xx_modem *md)
657 struct device *dev = &md->t7xx_dev->pdev->dev;
660 t7xx_md_exception(md, HIF_EX_INIT);
661 ret = t7xx_wait_hif_ex_hk_event(md, D2H_INT_EXCEPTION_INIT_DONE);
663 dev_err(dev, "EX CCIF HS timeout, RCH 0x%lx\n", D2H_INT_EXCEPTION_INIT_DONE);
665 t7xx_md_exception(md, HIF_EX_INIT_DONE);
666 ret = t7xx_wait_hif_ex_hk_event(md, D2H_INT_EXCEPTION_CLEARQ_DONE);
668 dev_err(dev, "EX CCIF HS timeout, RCH 0x%lx\n", D2H_INT_EXCEPTION_CLEARQ_DONE);
670 t7xx_md_exception(md, HIF_EX_CLEARQ_DONE);
671 ret = t7xx_wait_hif_ex_hk_event(md, D2H_INT_EXCEPTION_ALLQ_RESET);
673 dev_err(dev, "EX CCIF HS timeout, RCH 0x%lx\n", D2H_INT_EXCEPTION_ALLQ_RESET);
675 t7xx_md_exception(md, HIF_EX_ALLQ_RESET);
678 static struct t7xx_modem *t7xx_md_alloc(struct t7xx_pci_dev *t7xx_dev)
680 struct device *dev = &t7xx_dev->pdev->dev;
681 struct t7xx_modem *md;
683 md = devm_kzalloc(dev, sizeof(*md), GFP_KERNEL);
687 md->t7xx_dev = t7xx_dev;
689 spin_lock_init(&md->exp_lock);
690 md->handshake_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_HIGHPRI,
692 if (!md->handshake_wq)
695 INIT_WORK(&md->handshake_work, t7xx_md_hk_wq);
696 md->core_md.feature_set[RT_ID_MD_PORT_ENUM] &= ~FEATURE_MSK;
697 md->core_md.feature_set[RT_ID_MD_PORT_ENUM] |=
698 FIELD_PREP(FEATURE_MSK, MTK_FEATURE_MUST_BE_SUPPORTED);
700 INIT_WORK(&md->ap_handshake_work, t7xx_ap_hk_wq);
701 md->core_ap.feature_set[RT_ID_AP_PORT_ENUM] &= ~FEATURE_MSK;
702 md->core_ap.feature_set[RT_ID_AP_PORT_ENUM] |=
703 FIELD_PREP(FEATURE_MSK, MTK_FEATURE_MUST_BE_SUPPORTED);
708 int t7xx_md_reset(struct t7xx_pci_dev *t7xx_dev)
710 struct t7xx_modem *md = t7xx_dev->md;
712 md->md_init_finish = false;
715 t7xx_cldma_reset(md->md_ctrl[CLDMA_ID_MD]);
716 t7xx_cldma_reset(md->md_ctrl[CLDMA_ID_AP]);
717 t7xx_port_proxy_reset(md->port_prox);
718 md->md_init_finish = true;
719 return t7xx_core_reset(md);
723 * t7xx_md_init() - Initialize modem.
724 * @t7xx_dev: MTK device.
726 * Allocate and initialize MD control block, and initialize data path.
727 * Register MHCCIF ISR and RGU ISR, and start the state machine.
731 ** -ENOMEM - Allocation failure.
733 int t7xx_md_init(struct t7xx_pci_dev *t7xx_dev)
735 struct t7xx_modem *md;
738 md = t7xx_md_alloc(t7xx_dev);
742 ret = t7xx_cldma_alloc(CLDMA_ID_MD, t7xx_dev);
744 goto err_destroy_hswq;
746 ret = t7xx_cldma_alloc(CLDMA_ID_AP, t7xx_dev);
748 goto err_destroy_hswq;
750 ret = t7xx_fsm_init(md);
752 goto err_destroy_hswq;
754 ret = t7xx_ccmni_init(t7xx_dev);
758 ret = t7xx_cldma_init(md->md_ctrl[CLDMA_ID_MD]);
760 goto err_uninit_ccmni;
762 ret = t7xx_cldma_init(md->md_ctrl[CLDMA_ID_AP]);
764 goto err_uninit_md_cldma;
766 ret = t7xx_port_proxy_init(md);
768 goto err_uninit_ap_cldma;
770 ret = t7xx_fsm_append_cmd(md->fsm_ctl, FSM_CMD_START, 0);
771 if (ret) /* t7xx_fsm_uninit() flushes cmd queue */
772 goto err_uninit_proxy;
774 t7xx_md_sys_sw_init(t7xx_dev);
775 md->md_init_finish = true;
779 t7xx_port_proxy_uninit(md->port_prox);
782 t7xx_cldma_exit(md->md_ctrl[CLDMA_ID_AP]);
785 t7xx_cldma_exit(md->md_ctrl[CLDMA_ID_MD]);
788 t7xx_ccmni_exit(t7xx_dev);
794 destroy_workqueue(md->handshake_wq);
795 dev_err(&t7xx_dev->pdev->dev, "Modem init failed\n");
799 void t7xx_md_exit(struct t7xx_pci_dev *t7xx_dev)
801 enum t7xx_mode mode = READ_ONCE(t7xx_dev->mode);
802 struct t7xx_modem *md = t7xx_dev->md;
804 t7xx_pcie_mac_clear_int(t7xx_dev, SAP_RGU_INT);
806 if (!md->md_init_finish)
809 if (mode != T7XX_RESET && mode != T7XX_UNKNOWN)
810 t7xx_fsm_append_cmd(md->fsm_ctl, FSM_CMD_PRE_STOP, FSM_CMD_FLAG_WAIT_FOR_COMPLETION);
811 t7xx_port_proxy_uninit(md->port_prox);
812 t7xx_cldma_exit(md->md_ctrl[CLDMA_ID_AP]);
813 t7xx_cldma_exit(md->md_ctrl[CLDMA_ID_MD]);
814 t7xx_ccmni_exit(t7xx_dev);
816 destroy_workqueue(md->handshake_wq);