xref: /linux/drivers/net/wireless/mediatek/mt76/mt7925/init.c (revision c7979c3917fa1326dae3607e1c6a04c12057b194)
1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2023 MediaTek Inc. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/hwmon.h>
6 #include <linux/hwmon-sysfs.h>
7 #include <linux/thermal.h>
8 #include <linux/firmware.h>
9 #include "mt7925.h"
10 #include "mac.h"
11 #include "mcu.h"
12 
mt7925_thermal_temp_show(struct device * dev,struct device_attribute * attr,char * buf)13 static ssize_t mt7925_thermal_temp_show(struct device *dev,
14 					struct device_attribute *attr,
15 					char *buf)
16 {
17 	switch (to_sensor_dev_attr(attr)->index) {
18 	case 0: {
19 		struct mt792x_phy *phy = dev_get_drvdata(dev);
20 		struct mt792x_dev *mdev = phy->dev;
21 		int temperature;
22 
23 		mt792x_mutex_acquire(mdev);
24 		temperature = mt7925_mcu_get_temperature(phy);
25 		mt792x_mutex_release(mdev);
26 
27 		if (temperature < 0)
28 			return temperature;
29 		/* display in millidegree Celsius */
30 		return sprintf(buf, "%u\n", temperature * 1000);
31 	}
32 	default:
33 		return -EINVAL;
34 	}
35 }
36 static SENSOR_DEVICE_ATTR_RO(temp1_input, mt7925_thermal_temp, 0);
37 
38 static struct attribute *mt7925_hwmon_attrs[] = {
39 	&sensor_dev_attr_temp1_input.dev_attr.attr,
40 	NULL,
41 };
42 ATTRIBUTE_GROUPS(mt7925_hwmon);
43 
mt7925_thermal_init(struct mt792x_phy * phy)44 static int mt7925_thermal_init(struct mt792x_phy *phy)
45 {
46 	struct wiphy *wiphy = phy->mt76->hw->wiphy;
47 	struct device *hwmon;
48 	const char *name;
49 
50 	if (!IS_REACHABLE(CONFIG_HWMON))
51 		return 0;
52 
53 	name = devm_kasprintf(&wiphy->dev, GFP_KERNEL, "mt7925_%s",
54 			      wiphy_name(wiphy));
55 	if (!name)
56 		return -ENOMEM;
57 
58 	hwmon = devm_hwmon_device_register_with_groups(&wiphy->dev, name, phy,
59 						       mt7925_hwmon_groups);
60 	return PTR_ERR_OR_ZERO(hwmon);
61 }
62 
mt7925_regd_be_ctrl(struct mt792x_dev * dev,u8 * alpha2)63 void mt7925_regd_be_ctrl(struct mt792x_dev *dev, u8 *alpha2)
64 {
65 	struct mt792x_phy *phy = &dev->phy;
66 	struct mt7925_clc_rule_v2 *rule;
67 	struct mt7925_clc *clc;
68 	bool old = dev->has_eht, new = true;
69 	u32 mtcl_conf = mt792x_acpi_get_mtcl_conf(&dev->phy, alpha2);
70 	u8 *pos;
71 
72 	if (mtcl_conf != MT792X_ACPI_MTCL_INVALID &&
73 	    (((mtcl_conf >> 4) & 0x3) == 0)) {
74 		new = false;
75 		goto out;
76 	}
77 
78 	if (!phy->clc[MT792x_CLC_BE_CTRL])
79 		goto out;
80 
81 	clc = (struct mt7925_clc *)phy->clc[MT792x_CLC_BE_CTRL];
82 	pos = clc->data;
83 
84 	while (1) {
85 		rule = (struct mt7925_clc_rule_v2 *)pos;
86 
87 		if (rule->alpha2[0] == alpha2[0] &&
88 		    rule->alpha2[1] == alpha2[1]) {
89 			new = false;
90 			break;
91 		}
92 
93 		/* Check the last one */
94 		if (rule->flag & BIT(0))
95 			break;
96 
97 		pos += sizeof(*rule);
98 	}
99 
100 out:
101 	if (old == new)
102 		return;
103 
104 	dev->has_eht = new;
105 	mt7925_set_stream_he_eht_caps(phy);
106 }
107 
108 static void
mt7925_regd_channel_update(struct wiphy * wiphy,struct mt792x_dev * dev)109 mt7925_regd_channel_update(struct wiphy *wiphy, struct mt792x_dev *dev)
110 {
111 #define IS_UNII_INVALID(idx, sfreq, efreq, cfreq) \
112 	(!(dev->phy.clc_chan_conf & BIT(idx)) && (cfreq) >= (sfreq) && (cfreq) <= (efreq))
113 #define MT7925_UNII_59G_IS_VALID	0x1
114 #define MT7925_UNII_6G_IS_VALID	0x1e
115 	struct ieee80211_supported_band *sband;
116 	struct mt76_dev *mdev = &dev->mt76;
117 	struct ieee80211_channel *ch;
118 	u32 mtcl_conf = mt792x_acpi_get_mtcl_conf(&dev->phy, mdev->alpha2);
119 	int i;
120 
121 	if (mtcl_conf != MT792X_ACPI_MTCL_INVALID) {
122 		if ((mtcl_conf & 0x3) == 0)
123 			dev->phy.clc_chan_conf &= ~MT7925_UNII_59G_IS_VALID;
124 		if (((mtcl_conf >> 2) & 0x3) == 0)
125 			dev->phy.clc_chan_conf &= ~MT7925_UNII_6G_IS_VALID;
126 	}
127 
128 	sband = wiphy->bands[NL80211_BAND_5GHZ];
129 	if (!sband)
130 		return;
131 
132 	for (i = 0; i < sband->n_channels; i++) {
133 		ch = &sband->channels[i];
134 
135 		/* UNII-4 */
136 		if (IS_UNII_INVALID(0, 5845, 5925, ch->center_freq))
137 			ch->flags |= IEEE80211_CHAN_DISABLED;
138 	}
139 
140 	sband = wiphy->bands[NL80211_BAND_6GHZ];
141 	if (!sband)
142 		return;
143 
144 	for (i = 0; i < sband->n_channels; i++) {
145 		ch = &sband->channels[i];
146 
147 		/* UNII-5/6/7/8 */
148 		if (IS_UNII_INVALID(1, 5925, 6425, ch->center_freq) ||
149 		    IS_UNII_INVALID(2, 6425, 6525, ch->center_freq) ||
150 		    IS_UNII_INVALID(3, 6525, 6875, ch->center_freq) ||
151 		    IS_UNII_INVALID(4, 6875, 7125, ch->center_freq))
152 			ch->flags |= IEEE80211_CHAN_DISABLED;
153 	}
154 }
155 
mt7925_regd_update(struct mt792x_dev * dev)156 void mt7925_regd_update(struct mt792x_dev *dev)
157 {
158 	struct mt76_dev *mdev = &dev->mt76;
159 	struct ieee80211_hw *hw = mdev->hw;
160 	struct wiphy *wiphy = hw->wiphy;
161 
162 	if (!dev->regd_change)
163 		return;
164 
165 	mt7925_mcu_set_clc(dev, mdev->alpha2, dev->country_ie_env);
166 	mt7925_regd_channel_update(wiphy, dev);
167 	mt7925_mcu_set_channel_domain(hw->priv);
168 	mt7925_set_tx_sar_pwr(hw, NULL);
169 	dev->regd_change = false;
170 }
171 EXPORT_SYMBOL_GPL(mt7925_regd_update);
172 
173 static void
mt7925_regd_notifier(struct wiphy * wiphy,struct regulatory_request * req)174 mt7925_regd_notifier(struct wiphy *wiphy,
175 		     struct regulatory_request *req)
176 {
177 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
178 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
179 	struct mt76_dev *mdev = &dev->mt76;
180 	struct mt76_connac_pm *pm = &dev->pm;
181 
182 	/* allow world regdom at the first boot only */
183 	if (!memcmp(req->alpha2, "00", 2) &&
184 	    mdev->alpha2[0] && mdev->alpha2[1])
185 		return;
186 
187 	/* do not need to update the same country twice */
188 	if (!memcmp(req->alpha2, mdev->alpha2, 2) &&
189 	    dev->country_ie_env == req->country_ie_env)
190 		return;
191 
192 	memcpy(mdev->alpha2, req->alpha2, 2);
193 	mdev->region = req->dfs_region;
194 	dev->country_ie_env = req->country_ie_env;
195 	dev->regd_change = true;
196 
197 	if (pm->suspended)
198 		return;
199 
200 	dev->regd_in_progress = true;
201 	mt792x_mutex_acquire(dev);
202 	mt7925_regd_update(dev);
203 	mt792x_mutex_release(dev);
204 	dev->regd_in_progress = false;
205 	wake_up(&dev->wait);
206 }
207 
mt7925_mac_init_basic_rates(struct mt792x_dev * dev)208 static void mt7925_mac_init_basic_rates(struct mt792x_dev *dev)
209 {
210 	int i;
211 
212 	for (i = 0; i < ARRAY_SIZE(mt76_rates); i++) {
213 		u16 rate = mt76_rates[i].hw_value;
214 		u16 idx = MT792x_BASIC_RATES_TBL + i;
215 
216 		rate = FIELD_PREP(MT_TX_RATE_MODE, rate >> 8) |
217 		       FIELD_PREP(MT_TX_RATE_IDX, rate & GENMASK(7, 0));
218 		mt7925_mac_set_fixed_rate_table(dev, idx, rate);
219 	}
220 }
221 
mt7925_mac_init(struct mt792x_dev * dev)222 int mt7925_mac_init(struct mt792x_dev *dev)
223 {
224 	int i;
225 
226 	mt76_rmw_field(dev, MT_MDP_DCR1, MT_MDP_DCR1_MAX_RX_LEN, 1536);
227 	/* enable hardware de-agg */
228 	mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_DAMSDU_EN);
229 
230 	for (i = 0; i < MT792x_WTBL_SIZE; i++)
231 		mt7925_mac_wtbl_update(dev, i,
232 				       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
233 	for (i = 0; i < 2; i++)
234 		mt792x_mac_init_band(dev, i);
235 
236 	mt7925_mac_init_basic_rates(dev);
237 
238 	memzero_explicit(&dev->mt76.alpha2, sizeof(dev->mt76.alpha2));
239 
240 	return 0;
241 }
242 EXPORT_SYMBOL_GPL(mt7925_mac_init);
243 
__mt7925_init_hardware(struct mt792x_dev * dev)244 static int __mt7925_init_hardware(struct mt792x_dev *dev)
245 {
246 	int ret;
247 
248 	ret = mt792x_mcu_init(dev);
249 	if (ret)
250 		goto out;
251 
252 	mt76_eeprom_override(&dev->mphy);
253 
254 	ret = mt7925_mcu_set_eeprom(dev);
255 	if (ret)
256 		goto out;
257 
258 	ret = mt7925_mac_init(dev);
259 	if (ret)
260 		goto out;
261 
262 out:
263 	return ret;
264 }
265 
mt7925_init_hardware(struct mt792x_dev * dev)266 static int mt7925_init_hardware(struct mt792x_dev *dev)
267 {
268 	int ret, i;
269 
270 	set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
271 
272 	for (i = 0; i < MT792x_MCU_INIT_RETRY_COUNT; i++) {
273 		ret = __mt7925_init_hardware(dev);
274 		if (!ret)
275 			break;
276 
277 		mt792x_init_reset(dev);
278 	}
279 
280 	if (i == MT792x_MCU_INIT_RETRY_COUNT) {
281 		dev_err(dev->mt76.dev, "hardware init failed\n");
282 		return ret;
283 	}
284 
285 	return 0;
286 }
287 
mt7925_init_work(struct work_struct * work)288 static void mt7925_init_work(struct work_struct *work)
289 {
290 	struct mt792x_dev *dev = container_of(work, struct mt792x_dev,
291 					      init_work);
292 	int ret;
293 
294 	ret = mt7925_init_hardware(dev);
295 	if (ret)
296 		return;
297 
298 	mt76_set_stream_caps(&dev->mphy, true);
299 	mt7925_set_stream_he_eht_caps(&dev->phy);
300 	mt792x_config_mac_addr_list(dev);
301 
302 	ret = mt7925_init_mlo_caps(&dev->phy);
303 	if (ret) {
304 		dev_err(dev->mt76.dev, "MLO init failed\n");
305 		return;
306 	}
307 
308 	ret = mt76_register_device(&dev->mt76, true, mt76_rates,
309 				   ARRAY_SIZE(mt76_rates));
310 	if (ret) {
311 		dev_err(dev->mt76.dev, "register device failed\n");
312 		return;
313 	}
314 
315 	ret = mt7925_init_debugfs(dev);
316 	if (ret) {
317 		dev_err(dev->mt76.dev, "register debugfs failed\n");
318 		return;
319 	}
320 
321 	ret = mt7925_thermal_init(&dev->phy);
322 	if (ret) {
323 		dev_err(dev->mt76.dev, "thermal init failed\n");
324 		return;
325 	}
326 
327 	ret = mt7925_mcu_set_thermal_protect(dev);
328 	if (ret) {
329 		dev_err(dev->mt76.dev, "thermal protection enable failed\n");
330 		return;
331 	}
332 
333 	/* we support chip reset now */
334 	dev->hw_init_done = true;
335 
336 	mt7925_mcu_set_deep_sleep(dev, dev->pm.ds_enable);
337 }
338 
mt7925_register_device(struct mt792x_dev * dev)339 int mt7925_register_device(struct mt792x_dev *dev)
340 {
341 	struct ieee80211_hw *hw = mt76_hw(dev);
342 	int ret;
343 
344 	dev->phy.dev = dev;
345 	dev->phy.mt76 = &dev->mt76.phy;
346 	dev->mt76.phy.priv = &dev->phy;
347 	dev->mt76.tx_worker.fn = mt792x_tx_worker;
348 
349 	INIT_DELAYED_WORK(&dev->pm.ps_work, mt792x_pm_power_save_work);
350 	INIT_DELAYED_WORK(&dev->mlo_pm_work, mt7925_mlo_pm_work);
351 	INIT_WORK(&dev->pm.wake_work, mt792x_pm_wake_work);
352 	spin_lock_init(&dev->pm.wake.lock);
353 	mutex_init(&dev->pm.mutex);
354 	init_waitqueue_head(&dev->pm.wait);
355 	init_waitqueue_head(&dev->wait);
356 	spin_lock_init(&dev->pm.txq_lock);
357 	INIT_DELAYED_WORK(&dev->mphy.mac_work, mt792x_mac_work);
358 	INIT_DELAYED_WORK(&dev->phy.scan_work, mt7925_scan_work);
359 	INIT_DELAYED_WORK(&dev->coredump.work, mt7925_coredump_work);
360 #if IS_ENABLED(CONFIG_IPV6)
361 	INIT_WORK(&dev->ipv6_ns_work, mt7925_set_ipv6_ns_work);
362 	skb_queue_head_init(&dev->ipv6_ns_list);
363 #endif
364 	skb_queue_head_init(&dev->phy.scan_event_list);
365 	skb_queue_head_init(&dev->coredump.msg_list);
366 
367 	INIT_WORK(&dev->reset_work, mt7925_mac_reset_work);
368 	INIT_WORK(&dev->init_work, mt7925_init_work);
369 
370 	INIT_WORK(&dev->phy.roc_work, mt7925_roc_work);
371 	timer_setup(&dev->phy.roc_timer, mt792x_roc_timer, 0);
372 	init_waitqueue_head(&dev->phy.roc_wait);
373 
374 	dev->pm.idle_timeout = MT792x_PM_TIMEOUT;
375 	dev->pm.stats.last_wake_event = jiffies;
376 	dev->pm.stats.last_doze_event = jiffies;
377 	if (!mt76_is_usb(&dev->mt76)) {
378 		dev->pm.enable_user = true;
379 		dev->pm.enable = true;
380 		dev->pm.ds_enable_user = true;
381 		dev->pm.ds_enable = true;
382 	}
383 
384 	if (!mt76_is_mmio(&dev->mt76))
385 		hw->extra_tx_headroom += MT_SDIO_TXD_SIZE + MT_SDIO_HDR_SIZE;
386 
387 	mt792x_init_acpi_sar(dev);
388 
389 	ret = mt792x_init_wcid(dev);
390 	if (ret)
391 		return ret;
392 
393 	ret = mt792x_init_wiphy(hw);
394 	if (ret)
395 		return ret;
396 
397 	hw->wiphy->reg_notifier = mt7925_regd_notifier;
398 	dev->mphy.sband_2g.sband.ht_cap.cap |=
399 			IEEE80211_HT_CAP_LDPC_CODING |
400 			IEEE80211_HT_CAP_MAX_AMSDU;
401 	dev->mphy.sband_2g.sband.ht_cap.ampdu_density =
402 			IEEE80211_HT_MPDU_DENSITY_2;
403 	dev->mphy.sband_5g.sband.ht_cap.cap |=
404 			IEEE80211_HT_CAP_LDPC_CODING |
405 			IEEE80211_HT_CAP_MAX_AMSDU;
406 	dev->mphy.sband_2g.sband.ht_cap.ampdu_density =
407 			IEEE80211_HT_MPDU_DENSITY_1;
408 	dev->mphy.sband_5g.sband.vht_cap.cap |=
409 			IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
410 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
411 			IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
412 			IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
413 			(3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT);
414 	dev->mphy.sband_5g.sband.vht_cap.cap |=
415 			IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
416 			IEEE80211_VHT_CAP_SHORT_GI_160;
417 
418 	dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask;
419 	dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask;
420 
421 	queue_work(system_wq, &dev->init_work);
422 
423 	return 0;
424 }
425 EXPORT_SYMBOL_GPL(mt7925_register_device);
426