1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2021 MediaTek Inc. */
3 
4 #include "mt7921.h"
5 #include "mcu.h"
6 
mt7921e_driver_own(struct mt7921_dev * dev)7 static 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
mt7921_mcu_send_message(struct mt76_dev * mdev,struct sk_buff * skb,int cmd,int * seq)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 = mt7921_mcu_fill_message(mdev, skb, cmd, seq);
30 	if (ret)
31 		return ret;
32 
33 	if (cmd == MCU_CMD_FW_SCATTER)
34 		txq = MT_MCUQ_FWDL;
35 
36 	return mt76_tx_queue_skb_raw(dev, mdev->q_mcu[txq], skb, 0);
37 }
38 
mt7921e_mcu_init(struct mt7921_dev * dev)39 int mt7921e_mcu_init(struct mt7921_dev *dev)
40 {
41 	static const struct mt76_mcu_ops mt7921_mcu_ops = {
42 		.headroom = sizeof(struct mt7921_mcu_txd),
43 		.mcu_skb_send_msg = mt7921_mcu_send_message,
44 		.mcu_parse_response = mt7921_mcu_parse_response,
45 		.mcu_restart = mt7921_mcu_restart,
46 	};
47 	int err;
48 
49 	dev->mt76.mcu_ops = &mt7921_mcu_ops;
50 
51 	err = mt7921e_driver_own(dev);
52 	if (err)
53 		return err;
54 
55 	err = mt7921_run_firmware(dev);
56 
57 	mt76_queue_tx_cleanup(dev, dev->mt76.q_mcu[MT_MCUQ_FWDL], false);
58 
59 	return err;
60 }
61 
mt7921e_mcu_drv_pmctrl(struct mt7921_dev * dev)62 int mt7921e_mcu_drv_pmctrl(struct mt7921_dev *dev)
63 {
64 	struct mt76_phy *mphy = &dev->mt76.phy;
65 	struct mt76_connac_pm *pm = &dev->pm;
66 	int i, err = 0;
67 
68 	for (i = 0; i < MT7921_DRV_OWN_RETRY_COUNT; i++) {
69 		mt76_wr(dev, MT_CONN_ON_LPCTL, PCIE_LPCR_HOST_CLR_OWN);
70 		if (mt76_poll_msec(dev, MT_CONN_ON_LPCTL,
71 				   PCIE_LPCR_HOST_OWN_SYNC, 0, 50))
72 			break;
73 	}
74 
75 	if (i == MT7921_DRV_OWN_RETRY_COUNT) {
76 		dev_err(dev->mt76.dev, "driver own failed\n");
77 		err = -EIO;
78 		goto out;
79 	}
80 
81 	mt7921_wpdma_reinit_cond(dev);
82 	clear_bit(MT76_STATE_PM, &mphy->state);
83 
84 	pm->stats.last_wake_event = jiffies;
85 	pm->stats.doze_time += pm->stats.last_wake_event -
86 			       pm->stats.last_doze_event;
87 out:
88 	return err;
89 }
90 
mt7921e_mcu_fw_pmctrl(struct mt7921_dev * dev)91 int mt7921e_mcu_fw_pmctrl(struct mt7921_dev *dev)
92 {
93 	struct mt76_phy *mphy = &dev->mt76.phy;
94 	struct mt76_connac_pm *pm = &dev->pm;
95 	int i, err = 0;
96 
97 	for (i = 0; i < MT7921_DRV_OWN_RETRY_COUNT; i++) {
98 		mt76_wr(dev, MT_CONN_ON_LPCTL, PCIE_LPCR_HOST_SET_OWN);
99 		if (mt76_poll_msec(dev, MT_CONN_ON_LPCTL,
100 				   PCIE_LPCR_HOST_OWN_SYNC, 4, 50))
101 			break;
102 	}
103 
104 	if (i == MT7921_DRV_OWN_RETRY_COUNT) {
105 		dev_err(dev->mt76.dev, "firmware own failed\n");
106 		clear_bit(MT76_STATE_PM, &mphy->state);
107 		err = -EIO;
108 	}
109 
110 	pm->stats.last_doze_event = jiffies;
111 	pm->stats.awake_time += pm->stats.last_doze_event -
112 				pm->stats.last_wake_event;
113 
114 	return err;
115 }
116