xref: /linux/drivers/net/wireless/mediatek/mt76/mt7925/init.c (revision be239684b18e1cdcafcf8c7face4a2f562c745ad)
1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2023 MediaTek Inc. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/firmware.h>
6 #include "mt7925.h"
7 #include "mac.h"
8 #include "mcu.h"
9 
10 static void
11 mt7925_regd_notifier(struct wiphy *wiphy,
12 		     struct regulatory_request *req)
13 {
14 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
15 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
16 	struct mt76_dev *mdev = &dev->mt76;
17 
18 	/* allow world regdom at the first boot only */
19 	if (!memcmp(req->alpha2, "00", 2) &&
20 	    mdev->alpha2[0] && mdev->alpha2[1])
21 		return;
22 
23 	/* do not need to update the same country twice */
24 	if (!memcmp(req->alpha2, mdev->alpha2, 2) &&
25 	    dev->country_ie_env == req->country_ie_env)
26 		return;
27 
28 	memcpy(mdev->alpha2, req->alpha2, 2);
29 	mdev->region = req->dfs_region;
30 	dev->country_ie_env = req->country_ie_env;
31 
32 	mt792x_mutex_acquire(dev);
33 	mt7925_mcu_set_clc(dev, req->alpha2, req->country_ie_env);
34 	mt7925_mcu_set_channel_domain(hw->priv);
35 	mt7925_set_tx_sar_pwr(hw, NULL);
36 	mt792x_mutex_release(dev);
37 }
38 
39 static void mt7925_mac_init_basic_rates(struct mt792x_dev *dev)
40 {
41 	int i;
42 
43 	for (i = 0; i < ARRAY_SIZE(mt76_rates); i++) {
44 		u16 rate = mt76_rates[i].hw_value;
45 		u16 idx = MT792x_BASIC_RATES_TBL + i;
46 
47 		rate = FIELD_PREP(MT_TX_RATE_MODE, rate >> 8) |
48 		       FIELD_PREP(MT_TX_RATE_IDX, rate & GENMASK(7, 0));
49 		mt7925_mac_set_fixed_rate_table(dev, idx, rate);
50 	}
51 }
52 
53 int mt7925_mac_init(struct mt792x_dev *dev)
54 {
55 	int i;
56 
57 	mt76_rmw_field(dev, MT_MDP_DCR1, MT_MDP_DCR1_MAX_RX_LEN, 1536);
58 	/* enable hardware de-agg */
59 	mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_DAMSDU_EN);
60 
61 	for (i = 0; i < MT792x_WTBL_SIZE; i++)
62 		mt7925_mac_wtbl_update(dev, i,
63 				       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
64 	for (i = 0; i < 2; i++)
65 		mt792x_mac_init_band(dev, i);
66 
67 	mt7925_mac_init_basic_rates(dev);
68 
69 	memzero_explicit(&dev->mt76.alpha2, sizeof(dev->mt76.alpha2));
70 
71 	return 0;
72 }
73 EXPORT_SYMBOL_GPL(mt7925_mac_init);
74 
75 static int __mt7925_init_hardware(struct mt792x_dev *dev)
76 {
77 	int ret;
78 
79 	ret = mt792x_mcu_init(dev);
80 	if (ret)
81 		goto out;
82 
83 	mt76_eeprom_override(&dev->mphy);
84 
85 	ret = mt7925_mcu_set_eeprom(dev);
86 	if (ret)
87 		goto out;
88 
89 	ret = mt7925_mac_init(dev);
90 	if (ret)
91 		goto out;
92 
93 out:
94 	return ret;
95 }
96 
97 static int mt7925_init_hardware(struct mt792x_dev *dev)
98 {
99 	int ret, i;
100 
101 	set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
102 
103 	for (i = 0; i < MT792x_MCU_INIT_RETRY_COUNT; i++) {
104 		ret = __mt7925_init_hardware(dev);
105 		if (!ret)
106 			break;
107 
108 		mt792x_init_reset(dev);
109 	}
110 
111 	if (i == MT792x_MCU_INIT_RETRY_COUNT) {
112 		dev_err(dev->mt76.dev, "hardware init failed\n");
113 		return ret;
114 	}
115 
116 	return 0;
117 }
118 
119 static void mt7925_init_work(struct work_struct *work)
120 {
121 	struct mt792x_dev *dev = container_of(work, struct mt792x_dev,
122 					      init_work);
123 	int ret;
124 
125 	ret = mt7925_init_hardware(dev);
126 	if (ret)
127 		return;
128 
129 	mt76_set_stream_caps(&dev->mphy, true);
130 	mt7925_set_stream_he_eht_caps(&dev->phy);
131 
132 	ret = mt76_register_device(&dev->mt76, true, mt76_rates,
133 				   ARRAY_SIZE(mt76_rates));
134 	if (ret) {
135 		dev_err(dev->mt76.dev, "register device failed\n");
136 		return;
137 	}
138 
139 	ret = mt7925_init_debugfs(dev);
140 	if (ret) {
141 		dev_err(dev->mt76.dev, "register debugfs failed\n");
142 		return;
143 	}
144 
145 	/* we support chip reset now */
146 	dev->hw_init_done = true;
147 
148 	mt7925_mcu_set_deep_sleep(dev, dev->pm.ds_enable);
149 }
150 
151 int mt7925_register_device(struct mt792x_dev *dev)
152 {
153 	struct ieee80211_hw *hw = mt76_hw(dev);
154 	int ret;
155 
156 	dev->phy.dev = dev;
157 	dev->phy.mt76 = &dev->mt76.phy;
158 	dev->mt76.phy.priv = &dev->phy;
159 	dev->mt76.tx_worker.fn = mt792x_tx_worker;
160 
161 	INIT_DELAYED_WORK(&dev->pm.ps_work, mt792x_pm_power_save_work);
162 	INIT_WORK(&dev->pm.wake_work, mt792x_pm_wake_work);
163 	spin_lock_init(&dev->pm.wake.lock);
164 	mutex_init(&dev->pm.mutex);
165 	init_waitqueue_head(&dev->pm.wait);
166 	spin_lock_init(&dev->pm.txq_lock);
167 	INIT_DELAYED_WORK(&dev->mphy.mac_work, mt792x_mac_work);
168 	INIT_DELAYED_WORK(&dev->phy.scan_work, mt7925_scan_work);
169 	INIT_DELAYED_WORK(&dev->coredump.work, mt7925_coredump_work);
170 #if IS_ENABLED(CONFIG_IPV6)
171 	INIT_WORK(&dev->ipv6_ns_work, mt7925_set_ipv6_ns_work);
172 	skb_queue_head_init(&dev->ipv6_ns_list);
173 #endif
174 	skb_queue_head_init(&dev->phy.scan_event_list);
175 	skb_queue_head_init(&dev->coredump.msg_list);
176 
177 	INIT_WORK(&dev->reset_work, mt7925_mac_reset_work);
178 	INIT_WORK(&dev->init_work, mt7925_init_work);
179 
180 	INIT_WORK(&dev->phy.roc_work, mt7925_roc_work);
181 	timer_setup(&dev->phy.roc_timer, mt792x_roc_timer, 0);
182 	init_waitqueue_head(&dev->phy.roc_wait);
183 
184 	dev->pm.idle_timeout = MT792x_PM_TIMEOUT;
185 	dev->pm.stats.last_wake_event = jiffies;
186 	dev->pm.stats.last_doze_event = jiffies;
187 	if (!mt76_is_usb(&dev->mt76)) {
188 		dev->pm.enable_user = true;
189 		dev->pm.enable = true;
190 		dev->pm.ds_enable_user = true;
191 		dev->pm.ds_enable = true;
192 	}
193 
194 	if (!mt76_is_mmio(&dev->mt76))
195 		hw->extra_tx_headroom += MT_SDIO_TXD_SIZE + MT_SDIO_HDR_SIZE;
196 
197 	mt792x_init_acpi_sar(dev);
198 
199 	ret = mt792x_init_wcid(dev);
200 	if (ret)
201 		return ret;
202 
203 	ret = mt792x_init_wiphy(hw);
204 	if (ret)
205 		return ret;
206 
207 	hw->wiphy->reg_notifier = mt7925_regd_notifier;
208 	dev->mphy.sband_2g.sband.ht_cap.cap |=
209 			IEEE80211_HT_CAP_LDPC_CODING |
210 			IEEE80211_HT_CAP_MAX_AMSDU;
211 	dev->mphy.sband_2g.sband.ht_cap.ampdu_density =
212 			IEEE80211_HT_MPDU_DENSITY_2;
213 	dev->mphy.sband_5g.sband.ht_cap.cap |=
214 			IEEE80211_HT_CAP_LDPC_CODING |
215 			IEEE80211_HT_CAP_MAX_AMSDU;
216 	dev->mphy.sband_2g.sband.ht_cap.ampdu_density =
217 			IEEE80211_HT_MPDU_DENSITY_1;
218 	dev->mphy.sband_5g.sband.vht_cap.cap |=
219 			IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
220 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
221 			IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
222 			IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
223 			(3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT);
224 	dev->mphy.sband_5g.sband.vht_cap.cap |=
225 			IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
226 			IEEE80211_VHT_CAP_SHORT_GI_160;
227 
228 	dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask;
229 	dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask;
230 
231 	queue_work(system_wq, &dev->init_work);
232 
233 	return 0;
234 }
235 EXPORT_SYMBOL_GPL(mt7925_register_device);
236