xref: /linux/drivers/net/wireless/mediatek/mt76/mt7925/init.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
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 "regd.h"
11 #include "mac.h"
12 #include "mcu.h"
13 
mt7925_thermal_temp_show(struct device * dev,struct device_attribute * attr,char * buf)14 static ssize_t mt7925_thermal_temp_show(struct device *dev,
15 					struct device_attribute *attr,
16 					char *buf)
17 {
18 	switch (to_sensor_dev_attr(attr)->index) {
19 	case 0: {
20 		struct mt792x_phy *phy = dev_get_drvdata(dev);
21 		struct mt792x_dev *mdev = phy->dev;
22 		int temperature;
23 
24 		mt792x_mutex_acquire(mdev);
25 		temperature = mt7925_mcu_get_temperature(phy);
26 		mt792x_mutex_release(mdev);
27 
28 		if (temperature < 0)
29 			return temperature;
30 		/* display in millidegree Celsius */
31 		return sprintf(buf, "%u\n", temperature * 1000);
32 	}
33 	default:
34 		return -EINVAL;
35 	}
36 }
37 static SENSOR_DEVICE_ATTR_RO(temp1_input, mt7925_thermal_temp, 0);
38 
39 static struct attribute *mt7925_hwmon_attrs[] = {
40 	&sensor_dev_attr_temp1_input.dev_attr.attr,
41 	NULL,
42 };
43 ATTRIBUTE_GROUPS(mt7925_hwmon);
44 
mt7925_thermal_init(struct mt792x_phy * phy)45 static int mt7925_thermal_init(struct mt792x_phy *phy)
46 {
47 	struct wiphy *wiphy = phy->mt76->hw->wiphy;
48 	struct device *hwmon;
49 	const char *name;
50 
51 	if (!IS_REACHABLE(CONFIG_HWMON))
52 		return 0;
53 
54 	name = devm_kasprintf(&wiphy->dev, GFP_KERNEL, "mt7925_%s",
55 			      wiphy_name(wiphy));
56 	if (!name)
57 		return -ENOMEM;
58 
59 	hwmon = devm_hwmon_device_register_with_groups(&wiphy->dev, name, phy,
60 						       mt7925_hwmon_groups);
61 	return PTR_ERR_OR_ZERO(hwmon);
62 }
63 
mt7925_mac_init_basic_rates(struct mt792x_dev * dev)64 static void mt7925_mac_init_basic_rates(struct mt792x_dev *dev)
65 {
66 	int i;
67 
68 	for (i = 0; i < ARRAY_SIZE(mt76_rates); i++) {
69 		u16 rate = mt76_rates[i].hw_value;
70 		u16 idx = MT792x_BASIC_RATES_TBL + i;
71 
72 		rate = FIELD_PREP(MT_TX_RATE_MODE, rate >> 8) |
73 		       FIELD_PREP(MT_TX_RATE_IDX, rate & GENMASK(7, 0));
74 		mt7925_mac_set_fixed_rate_table(dev, idx, rate);
75 	}
76 }
77 
mt7925_mac_init(struct mt792x_dev * dev)78 int mt7925_mac_init(struct mt792x_dev *dev)
79 {
80 	int i;
81 
82 	mt76_rmw_field(dev, MT_MDP_DCR1, MT_MDP_DCR1_MAX_RX_LEN, 1536);
83 	/* enable hardware de-agg */
84 	mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_DAMSDU_EN);
85 
86 	for (i = 0; i < MT792x_WTBL_SIZE; i++)
87 		mt7925_mac_wtbl_update(dev, i,
88 				       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
89 	for (i = 0; i < 2; i++)
90 		mt792x_mac_init_band(dev, i);
91 
92 	mt7925_mac_init_basic_rates(dev);
93 
94 	memzero_explicit(&dev->mt76.alpha2, sizeof(dev->mt76.alpha2));
95 
96 	return 0;
97 }
98 EXPORT_SYMBOL_GPL(mt7925_mac_init);
99 
__mt7925_init_hardware(struct mt792x_dev * dev)100 static int __mt7925_init_hardware(struct mt792x_dev *dev)
101 {
102 	int ret;
103 
104 	ret = mt792x_mcu_init(dev);
105 	if (ret)
106 		goto out;
107 
108 	ret = mt76_eeprom_override(&dev->mphy);
109 	if (ret)
110 		goto out;
111 
112 	ret = mt7925_mcu_set_eeprom(dev);
113 	if (ret)
114 		goto out;
115 
116 	ret = mt7925_mac_init(dev);
117 	if (ret)
118 		goto out;
119 
120 	if (is_mt7927(&dev->mt76)) {
121 		ret = mt7925_mcu_set_dbdc(&dev->mphy, true);
122 		if (ret) {
123 			dev_warn(dev->mt76.dev,
124 				 "MT7927 DBDC enable failed: %d\n", ret);
125 			ret = 0;
126 		}
127 	}
128 
129 out:
130 	return ret;
131 }
132 
mt7925_init_hardware(struct mt792x_dev * dev)133 static int mt7925_init_hardware(struct mt792x_dev *dev)
134 {
135 	int ret, i;
136 
137 	set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
138 
139 	for (i = 0; i < MT792x_MCU_INIT_RETRY_COUNT; i++) {
140 		if (atomic_read(&dev->mt76.bus_hung)) {
141 			ret = -EIO;
142 			break;
143 		}
144 
145 		ret = __mt7925_init_hardware(dev);
146 		if (!ret)
147 			break;
148 
149 		if (atomic_read(&dev->mt76.bus_hung))
150 			break;
151 
152 		mt792x_init_reset(dev);
153 	}
154 
155 	if (i == MT792x_MCU_INIT_RETRY_COUNT) {
156 		dev_err(dev->mt76.dev, "hardware init failed\n");
157 		return ret;
158 	}
159 
160 	return 0;
161 }
162 
mt7925_init_nan_cap(struct mt76_dev * mdev)163 static int mt7925_init_nan_cap(struct mt76_dev *mdev)
164 {
165 	struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
166 	const struct ieee80211_sta_he_cap *he_cap;
167 	struct ieee80211_supported_band *sband;
168 	struct wiphy *wiphy = mdev->hw->wiphy;
169 
170 	if (!(dev->fw_features & MT792x_FW_CAP_NAN))
171 		return 0;
172 
173 	sband = wiphy->bands[NL80211_BAND_2GHZ];
174 	if (sband)
175 		wiphy->nan_capa.phy.ht = sband->ht_cap;
176 
177 	sband = wiphy->bands[NL80211_BAND_5GHZ];
178 	if (sband)
179 		wiphy->nan_capa.phy.vht = sband->vht_cap;
180 
181 	sband = wiphy->bands[NL80211_BAND_2GHZ];
182 	he_cap = sband ? ieee80211_get_he_iftype_cap(sband, NL80211_IFTYPE_NAN)
183 		       : NULL;
184 	if (he_cap)
185 		wiphy->nan_capa.phy.he = *he_cap;
186 
187 	return 0;
188 }
189 
mt7925_init_work(struct work_struct * work)190 static void mt7925_init_work(struct work_struct *work)
191 {
192 	struct mt792x_dev *dev = container_of(work, struct mt792x_dev,
193 					      init_work);
194 	int ret;
195 
196 	ret = mt7925_init_hardware(dev);
197 	if (ret)
198 		return;
199 
200 	mt76_set_stream_caps(&dev->mphy, true);
201 	mt7925_set_stream_he_eht_caps(&dev->phy);
202 	mt792x_config_mac_addr_list(dev);
203 
204 	ret = mt7925_init_mlo_caps(&dev->phy);
205 	if (ret) {
206 		dev_err(dev->mt76.dev, "MLO init failed\n");
207 		return;
208 	}
209 
210 	dev->mt76.init_wiphy = mt7925_init_nan_cap;
211 
212 	ret = mt76_register_device(&dev->mt76, true, mt76_rates,
213 				   ARRAY_SIZE(mt76_rates));
214 	if (ret) {
215 		dev_err(dev->mt76.dev, "register device failed\n");
216 		return;
217 	}
218 
219 	ret = mt7925_init_debugfs(dev);
220 	if (ret) {
221 		dev_err(dev->mt76.dev, "register debugfs failed\n");
222 		return;
223 	}
224 
225 	ret = mt7925_thermal_init(&dev->phy);
226 	if (ret) {
227 		dev_err(dev->mt76.dev, "thermal init failed\n");
228 		return;
229 	}
230 
231 	ret = mt7925_mcu_set_thermal_protect(dev);
232 	if (ret) {
233 		dev_err(dev->mt76.dev, "thermal protection enable failed\n");
234 		return;
235 	}
236 
237 	/* we support chip reset now */
238 	dev->hw_init_done = true;
239 
240 	mt7925_mcu_set_deep_sleep(dev, dev->pm.ds_enable);
241 }
242 
mt7925_register_device(struct mt792x_dev * dev)243 int mt7925_register_device(struct mt792x_dev *dev)
244 {
245 	struct ieee80211_hw *hw = mt76_hw(dev);
246 	int ret;
247 
248 	dev->phy.dev = dev;
249 	dev->phy.mt76 = &dev->mt76.phy;
250 	dev->mt76.phy.priv = &dev->phy;
251 	dev->mt76.tx_worker.fn = mt792x_tx_worker;
252 
253 	INIT_DELAYED_WORK(&dev->pm.ps_work, mt792x_pm_power_save_work);
254 	INIT_DELAYED_WORK(&dev->mlo_pm_work, mt7925_mlo_pm_work);
255 	INIT_WORK(&dev->pm.wake_work, mt792x_pm_wake_work);
256 	spin_lock_init(&dev->pm.wake.lock);
257 	mutex_init(&dev->pm.mutex);
258 	init_waitqueue_head(&dev->pm.wait);
259 	init_waitqueue_head(&dev->wait);
260 	spin_lock_init(&dev->pm.txq_lock);
261 	INIT_DELAYED_WORK(&dev->mphy.mac_work, mt792x_mac_work);
262 	INIT_DELAYED_WORK(&dev->phy.scan_work, mt7925_scan_work);
263 	INIT_DELAYED_WORK(&dev->coredump.work, mt7925_coredump_work);
264 #if IS_ENABLED(CONFIG_IPV6)
265 	INIT_WORK(&dev->ipv6_ns_work, mt7925_set_ipv6_ns_work);
266 	skb_queue_head_init(&dev->ipv6_ns_list);
267 #endif
268 	skb_queue_head_init(&dev->phy.scan_event_list);
269 	skb_queue_head_init(&dev->coredump.msg_list);
270 
271 	INIT_WORK(&dev->reset_work, mt7925_mac_reset_work);
272 	INIT_WORK(&dev->init_work, mt7925_init_work);
273 
274 	INIT_WORK(&dev->phy.roc_work, mt7925_roc_work);
275 	timer_setup(&dev->phy.roc_timer, mt792x_roc_timer, 0);
276 	init_waitqueue_head(&dev->phy.roc_wait);
277 
278 	dev->pm.idle_timeout = MT792x_PM_TIMEOUT;
279 	dev->pm.stats.last_wake_event = jiffies;
280 	dev->pm.stats.last_doze_event = jiffies;
281 	/* MT7927: runtime PM crashes BT firmware on the shared CONNINFRA domain */
282 	if (!mt76_is_usb(&dev->mt76) && !is_mt7927(&dev->mt76)) {
283 		dev->pm.enable_user = true;
284 		dev->pm.enable = true;
285 		dev->pm.ds_enable_user = true;
286 		dev->pm.ds_enable = true;
287 	}
288 
289 	if (!mt76_is_mmio(&dev->mt76))
290 		hw->extra_tx_headroom += MT_SDIO_TXD_SIZE + MT_SDIO_HDR_SIZE;
291 
292 	mt792x_init_acpi_sar(dev);
293 
294 	ret = mt792x_init_wcid(dev);
295 	if (ret)
296 		return ret;
297 
298 	ret = mt792x_init_wiphy(hw);
299 	if (ret)
300 		return ret;
301 
302 	hw->wiphy->reg_notifier = mt7925_regd_notifier;
303 	dev->mphy.sband_2g.sband.ht_cap.cap |=
304 			IEEE80211_HT_CAP_LDPC_CODING |
305 			IEEE80211_HT_CAP_MAX_AMSDU;
306 	dev->mphy.sband_2g.sband.ht_cap.ampdu_density =
307 			IEEE80211_HT_MPDU_DENSITY_2;
308 	dev->mphy.sband_5g.sband.ht_cap.cap |=
309 			IEEE80211_HT_CAP_LDPC_CODING |
310 			IEEE80211_HT_CAP_MAX_AMSDU;
311 	dev->mphy.sband_2g.sband.ht_cap.ampdu_density =
312 			IEEE80211_HT_MPDU_DENSITY_1;
313 	dev->mphy.sband_5g.sband.vht_cap.cap |=
314 			IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
315 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
316 			IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
317 			IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
318 			(3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT);
319 	dev->mphy.sband_5g.sband.vht_cap.cap |=
320 			IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
321 			IEEE80211_VHT_CAP_SHORT_GI_160;
322 
323 	dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask;
324 	dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask;
325 
326 	queue_work(system_percpu_wq, &dev->init_work);
327 
328 	return 0;
329 }
330 EXPORT_SYMBOL_GPL(mt7925_register_device);
331