113e920d9SHaijun Liu // SPDX-License-Identifier: GPL-2.0-only
213e920d9SHaijun Liu /*
313e920d9SHaijun Liu  * Copyright (c) 2021, MediaTek Inc.
413e920d9SHaijun Liu  * Copyright (c) 2021-2022, Intel Corporation.
513e920d9SHaijun Liu  *
613e920d9SHaijun Liu  * Authors:
713e920d9SHaijun Liu  *  Haijun Liu <haijun.liu@mediatek.com>
813e920d9SHaijun Liu  *  Sreehari Kancharla <sreehari.kancharla@intel.com>
913e920d9SHaijun Liu  *
1013e920d9SHaijun Liu  * Contributors:
1113e920d9SHaijun Liu  *  Amir Hanania <amir.hanania@intel.com>
1213e920d9SHaijun Liu  *  Ricardo Martinez <ricardo.martinez@linux.intel.com>
1313e920d9SHaijun Liu  */
1413e920d9SHaijun Liu 
1513e920d9SHaijun Liu #include <linux/bits.h>
1613e920d9SHaijun Liu #include <linux/completion.h>
1713e920d9SHaijun Liu #include <linux/dev_printk.h>
1813e920d9SHaijun Liu #include <linux/io.h>
1913e920d9SHaijun Liu #include <linux/irqreturn.h>
2013e920d9SHaijun Liu 
2113e920d9SHaijun Liu #include "t7xx_mhccif.h"
2213e920d9SHaijun Liu #include "t7xx_modem_ops.h"
2313e920d9SHaijun Liu #include "t7xx_pci.h"
2413e920d9SHaijun Liu #include "t7xx_pcie_mac.h"
2513e920d9SHaijun Liu #include "t7xx_reg.h"
2613e920d9SHaijun Liu 
2746e8f49eSHaijun Liu #define D2H_INT_SR_ACK		(D2H_INT_SUSPEND_ACK |		\
2846e8f49eSHaijun Liu 				 D2H_INT_RESUME_ACK |		\
2946e8f49eSHaijun Liu 				 D2H_INT_SUSPEND_ACK_AP |	\
3046e8f49eSHaijun Liu 				 D2H_INT_RESUME_ACK_AP)
3146e8f49eSHaijun Liu 
t7xx_mhccif_clear_interrupts(struct t7xx_pci_dev * t7xx_dev,u32 mask)3213e920d9SHaijun Liu static void t7xx_mhccif_clear_interrupts(struct t7xx_pci_dev *t7xx_dev, u32 mask)
3313e920d9SHaijun Liu {
3413e920d9SHaijun Liu 	void __iomem *mhccif_pbase = t7xx_dev->base_addr.mhccif_rc_base;
3513e920d9SHaijun Liu 
3613e920d9SHaijun Liu 	/* Clear level 2 interrupt */
3713e920d9SHaijun Liu 	iowrite32(mask, mhccif_pbase + REG_EP2RC_SW_INT_ACK);
3813e920d9SHaijun Liu 	/* Ensure write is complete */
3913e920d9SHaijun Liu 	t7xx_mhccif_read_sw_int_sts(t7xx_dev);
4013e920d9SHaijun Liu 	/* Clear level 1 interrupt */
4113e920d9SHaijun Liu 	t7xx_pcie_mac_clear_int_status(t7xx_dev, MHCCIF_INT);
4213e920d9SHaijun Liu }
4313e920d9SHaijun Liu 
t7xx_mhccif_isr_thread(int irq,void * data)4413e920d9SHaijun Liu static irqreturn_t t7xx_mhccif_isr_thread(int irq, void *data)
4513e920d9SHaijun Liu {
4613e920d9SHaijun Liu 	struct t7xx_pci_dev *t7xx_dev = data;
4713e920d9SHaijun Liu 	u32 int_status, val;
4813e920d9SHaijun Liu 
4913e920d9SHaijun Liu 	val = T7XX_L1_1_BIT(1) | T7XX_L1_2_BIT(1);
5013e920d9SHaijun Liu 	iowrite32(val, IREG_BASE(t7xx_dev) + DISABLE_ASPM_LOWPWR);
5113e920d9SHaijun Liu 
5213e920d9SHaijun Liu 	int_status = t7xx_mhccif_read_sw_int_sts(t7xx_dev);
5313e920d9SHaijun Liu 	if (int_status & D2H_SW_INT_MASK) {
5413e920d9SHaijun Liu 		int ret = t7xx_pci_mhccif_isr(t7xx_dev);
5513e920d9SHaijun Liu 
5613e920d9SHaijun Liu 		if (ret)
5713e920d9SHaijun Liu 			dev_err(&t7xx_dev->pdev->dev, "PCI MHCCIF ISR failure: %d", ret);
5813e920d9SHaijun Liu 	}
5913e920d9SHaijun Liu 
6013e920d9SHaijun Liu 	t7xx_mhccif_clear_interrupts(t7xx_dev, int_status);
6146e8f49eSHaijun Liu 
62*de49ea38SHaijun Liu 	if (int_status & D2H_INT_DS_LOCK_ACK)
63*de49ea38SHaijun Liu 		complete_all(&t7xx_dev->sleep_lock_acquire);
64*de49ea38SHaijun Liu 
6546e8f49eSHaijun Liu 	if (int_status & D2H_INT_SR_ACK)
6646e8f49eSHaijun Liu 		complete(&t7xx_dev->pm_sr_ack);
6746e8f49eSHaijun Liu 
6846e8f49eSHaijun Liu 	iowrite32(T7XX_L1_BIT(1), IREG_BASE(t7xx_dev) + ENABLE_ASPM_LOWPWR);
6946e8f49eSHaijun Liu 
7046e8f49eSHaijun Liu 	int_status = t7xx_mhccif_read_sw_int_sts(t7xx_dev);
7146e8f49eSHaijun Liu 	if (!int_status) {
7246e8f49eSHaijun Liu 		val = T7XX_L1_1_BIT(1) | T7XX_L1_2_BIT(1);
7346e8f49eSHaijun Liu 		iowrite32(val, IREG_BASE(t7xx_dev) + ENABLE_ASPM_LOWPWR);
7446e8f49eSHaijun Liu 	}
7546e8f49eSHaijun Liu 
7613e920d9SHaijun Liu 	t7xx_pcie_mac_set_int(t7xx_dev, MHCCIF_INT);
7713e920d9SHaijun Liu 	return IRQ_HANDLED;
7813e920d9SHaijun Liu }
7913e920d9SHaijun Liu 
t7xx_mhccif_read_sw_int_sts(struct t7xx_pci_dev * t7xx_dev)8013e920d9SHaijun Liu u32 t7xx_mhccif_read_sw_int_sts(struct t7xx_pci_dev *t7xx_dev)
8113e920d9SHaijun Liu {
8213e920d9SHaijun Liu 	return ioread32(t7xx_dev->base_addr.mhccif_rc_base + REG_EP2RC_SW_INT_STS);
8313e920d9SHaijun Liu }
8413e920d9SHaijun Liu 
t7xx_mhccif_mask_set(struct t7xx_pci_dev * t7xx_dev,u32 val)8513e920d9SHaijun Liu void t7xx_mhccif_mask_set(struct t7xx_pci_dev *t7xx_dev, u32 val)
8613e920d9SHaijun Liu {
8713e920d9SHaijun Liu 	iowrite32(val, t7xx_dev->base_addr.mhccif_rc_base + REG_EP2RC_SW_INT_EAP_MASK_SET);
8813e920d9SHaijun Liu }
8913e920d9SHaijun Liu 
t7xx_mhccif_mask_clr(struct t7xx_pci_dev * t7xx_dev,u32 val)9013e920d9SHaijun Liu void t7xx_mhccif_mask_clr(struct t7xx_pci_dev *t7xx_dev, u32 val)
9113e920d9SHaijun Liu {
9213e920d9SHaijun Liu 	iowrite32(val, t7xx_dev->base_addr.mhccif_rc_base + REG_EP2RC_SW_INT_EAP_MASK_CLR);
9313e920d9SHaijun Liu }
9413e920d9SHaijun Liu 
t7xx_mhccif_mask_get(struct t7xx_pci_dev * t7xx_dev)9513e920d9SHaijun Liu u32 t7xx_mhccif_mask_get(struct t7xx_pci_dev *t7xx_dev)
9613e920d9SHaijun Liu {
9713e920d9SHaijun Liu 	return ioread32(t7xx_dev->base_addr.mhccif_rc_base + REG_EP2RC_SW_INT_EAP_MASK);
9813e920d9SHaijun Liu }
9913e920d9SHaijun Liu 
t7xx_mhccif_isr_handler(int irq,void * data)10013e920d9SHaijun Liu static irqreturn_t t7xx_mhccif_isr_handler(int irq, void *data)
10113e920d9SHaijun Liu {
10213e920d9SHaijun Liu 	return IRQ_WAKE_THREAD;
10313e920d9SHaijun Liu }
10413e920d9SHaijun Liu 
t7xx_mhccif_init(struct t7xx_pci_dev * t7xx_dev)10513e920d9SHaijun Liu void t7xx_mhccif_init(struct t7xx_pci_dev *t7xx_dev)
10613e920d9SHaijun Liu {
10713e920d9SHaijun Liu 	t7xx_dev->base_addr.mhccif_rc_base = t7xx_dev->base_addr.pcie_ext_reg_base +
10813e920d9SHaijun Liu 					    MHCCIF_RC_DEV_BASE -
10913e920d9SHaijun Liu 					    t7xx_dev->base_addr.pcie_dev_reg_trsl_addr;
11013e920d9SHaijun Liu 
11113e920d9SHaijun Liu 	t7xx_dev->intr_handler[MHCCIF_INT] = t7xx_mhccif_isr_handler;
11213e920d9SHaijun Liu 	t7xx_dev->intr_thread[MHCCIF_INT] = t7xx_mhccif_isr_thread;
11313e920d9SHaijun Liu 	t7xx_dev->callback_param[MHCCIF_INT] = t7xx_dev;
11413e920d9SHaijun Liu }
11513e920d9SHaijun Liu 
t7xx_mhccif_h2d_swint_trigger(struct t7xx_pci_dev * t7xx_dev,u32 channel)11613e920d9SHaijun Liu void t7xx_mhccif_h2d_swint_trigger(struct t7xx_pci_dev *t7xx_dev, u32 channel)
11713e920d9SHaijun Liu {
11813e920d9SHaijun Liu 	void __iomem *mhccif_pbase = t7xx_dev->base_addr.mhccif_rc_base;
11913e920d9SHaijun Liu 
12013e920d9SHaijun Liu 	iowrite32(BIT(channel), mhccif_pbase + REG_RC2EP_SW_BSY);
12113e920d9SHaijun Liu 	iowrite32(channel, mhccif_pbase + REG_RC2EP_SW_TCHNUM);
12213e920d9SHaijun Liu }
123