1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2021 MediaTek Inc. */
3 
4 #include "mt7921.h"
5 #include "mcu.h"
6 
7 int mt7921e_driver_own(struct mt7921_dev *dev)
8 {
9 	u32 reg = mt7921_reg_map_l1(dev, MT_TOP_LPCR_HOST_BAND0);
10 
11 	mt76_wr(dev, reg, MT_TOP_LPCR_HOST_DRV_OWN);
12 	if (!mt76_poll_msec(dev, reg, MT_TOP_LPCR_HOST_FW_OWN,
13 			    0, 500)) {
14 		dev_err(dev->mt76.dev, "Timeout for driver own\n");
15 		return -EIO;
16 	}
17 
18 	return 0;
19 }
20 
21 static int
22 mt7921_mcu_send_message(struct mt76_dev *mdev, struct sk_buff *skb,
23 			int cmd, int *seq)
24 {
25 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
26 	enum mt76_mcuq_id txq = MT_MCUQ_WM;
27 	int ret;
28 
29 	ret = mt76_connac2_mcu_fill_message(mdev, skb, cmd, seq);
30 	if (ret)
31 		return ret;
32 
33 	if (cmd == MCU_UNI_CMD(HIF_CTRL) ||
34 	    cmd == MCU_UNI_CMD(SUSPEND) ||
35 	    cmd == MCU_UNI_CMD(OFFLOAD))
36 		mdev->mcu.timeout = HZ;
37 	else
38 		mdev->mcu.timeout = 3 * HZ;
39 
40 	if (cmd == MCU_CMD(FW_SCATTER))
41 		txq = MT_MCUQ_FWDL;
42 
43 	return mt76_tx_queue_skb_raw(dev, mdev->q_mcu[txq], skb, 0);
44 }
45 
46 int mt7921e_mcu_init(struct mt7921_dev *dev)
47 {
48 	static const struct mt76_mcu_ops mt7921_mcu_ops = {
49 		.headroom = sizeof(struct mt76_connac2_mcu_txd),
50 		.mcu_skb_send_msg = mt7921_mcu_send_message,
51 		.mcu_parse_response = mt7921_mcu_parse_response,
52 		.mcu_restart = mt76_connac_mcu_restart,
53 	};
54 	int err;
55 
56 	dev->mt76.mcu_ops = &mt7921_mcu_ops;
57 
58 	err = mt7921e_driver_own(dev);
59 	if (err)
60 		return err;
61 
62 	err = mt7921_run_firmware(dev);
63 
64 	mt76_queue_tx_cleanup(dev, dev->mt76.q_mcu[MT_MCUQ_FWDL], false);
65 
66 	return err;
67 }
68 
69 int __mt7921e_mcu_drv_pmctrl(struct mt7921_dev *dev)
70 {
71 	int i, err = 0;
72 
73 	for (i = 0; i < MT7921_DRV_OWN_RETRY_COUNT; i++) {
74 		mt76_wr(dev, MT_CONN_ON_LPCTL, PCIE_LPCR_HOST_CLR_OWN);
75 		if (mt76_poll_msec(dev, MT_CONN_ON_LPCTL,
76 				   PCIE_LPCR_HOST_OWN_SYNC, 0, 50))
77 			break;
78 	}
79 
80 	if (i == MT7921_DRV_OWN_RETRY_COUNT) {
81 		dev_err(dev->mt76.dev, "driver own failed\n");
82 		err = -EIO;
83 	}
84 
85 	return err;
86 }
87 
88 int mt7921e_mcu_drv_pmctrl(struct mt7921_dev *dev)
89 {
90 	struct mt76_phy *mphy = &dev->mt76.phy;
91 	struct mt76_connac_pm *pm = &dev->pm;
92 	int err;
93 
94 	err = __mt7921e_mcu_drv_pmctrl(dev);
95 	if (err < 0)
96 		goto out;
97 
98 	mt7921_wpdma_reinit_cond(dev);
99 	clear_bit(MT76_STATE_PM, &mphy->state);
100 
101 	pm->stats.last_wake_event = jiffies;
102 	pm->stats.doze_time += pm->stats.last_wake_event -
103 			       pm->stats.last_doze_event;
104 out:
105 	return err;
106 }
107 
108 int mt7921e_mcu_fw_pmctrl(struct mt7921_dev *dev)
109 {
110 	struct mt76_phy *mphy = &dev->mt76.phy;
111 	struct mt76_connac_pm *pm = &dev->pm;
112 	int i;
113 
114 	for (i = 0; i < MT7921_DRV_OWN_RETRY_COUNT; i++) {
115 		mt76_wr(dev, MT_CONN_ON_LPCTL, PCIE_LPCR_HOST_SET_OWN);
116 		if (mt76_poll_msec(dev, MT_CONN_ON_LPCTL,
117 				   PCIE_LPCR_HOST_OWN_SYNC, 4, 50))
118 			break;
119 	}
120 
121 	if (i == MT7921_DRV_OWN_RETRY_COUNT) {
122 		dev_err(dev->mt76.dev, "firmware own failed\n");
123 		clear_bit(MT76_STATE_PM, &mphy->state);
124 		return -EIO;
125 	}
126 
127 	pm->stats.last_doze_event = jiffies;
128 	pm->stats.awake_time += pm->stats.last_doze_event -
129 				pm->stats.last_wake_event;
130 
131 	return 0;
132 }
133