xref: /linux/drivers/net/wireless/mediatek/mt76/mt7921/init.c (revision 4877fc92142f635be418d8c915eb48ef87681108)
1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 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 "mt7921.h"
10 #include "../mt76_connac2_mac.h"
11 #include "mcu.h"
12 
13 static ssize_t mt7921_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 = mt7921_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, mt7921_thermal_temp, 0);
37 
38 static struct attribute *mt7921_hwmon_attrs[] = {
39 	&sensor_dev_attr_temp1_input.dev_attr.attr,
40 	NULL,
41 };
42 ATTRIBUTE_GROUPS(mt7921_hwmon);
43 
44 static int mt7921_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, "mt7921_%s",
54 			      wiphy_name(wiphy));
55 
56 	hwmon = devm_hwmon_device_register_with_groups(&wiphy->dev, name, phy,
57 						       mt7921_hwmon_groups);
58 	return PTR_ERR_OR_ZERO(hwmon);
59 }
60 
61 static void
62 mt7921_regd_channel_update(struct wiphy *wiphy, struct mt792x_dev *dev)
63 {
64 #define IS_UNII_INVALID(idx, sfreq, efreq) \
65 	(!(dev->phy.clc_chan_conf & BIT(idx)) && (cfreq) >= (sfreq) && (cfreq) <= (efreq))
66 	struct ieee80211_supported_band *sband;
67 	struct mt76_dev *mdev = &dev->mt76;
68 	struct device_node *np, *band_np;
69 	struct ieee80211_channel *ch;
70 	int i, cfreq;
71 
72 	np = mt76_find_power_limits_node(mdev);
73 
74 	sband = wiphy->bands[NL80211_BAND_5GHZ];
75 	band_np = np ? of_get_child_by_name(np, "txpower-5g") : NULL;
76 	for (i = 0; i < sband->n_channels; i++) {
77 		ch = &sband->channels[i];
78 		cfreq = ch->center_freq;
79 
80 		if (np && (!band_np || !mt76_find_channel_node(band_np, ch))) {
81 			ch->flags |= IEEE80211_CHAN_DISABLED;
82 			continue;
83 		}
84 
85 		/* UNII-4 */
86 		if (IS_UNII_INVALID(0, 5850, 5925))
87 			ch->flags |= IEEE80211_CHAN_DISABLED;
88 	}
89 
90 	sband = wiphy->bands[NL80211_BAND_6GHZ];
91 	if (!sband)
92 		return;
93 
94 	band_np = np ? of_get_child_by_name(np, "txpower-6g") : NULL;
95 	for (i = 0; i < sband->n_channels; i++) {
96 		ch = &sband->channels[i];
97 		cfreq = ch->center_freq;
98 
99 		if (np && (!band_np || !mt76_find_channel_node(band_np, ch))) {
100 			ch->flags |= IEEE80211_CHAN_DISABLED;
101 			continue;
102 		}
103 
104 		/* UNII-5/6/7/8 */
105 		if (IS_UNII_INVALID(1, 5925, 6425) ||
106 		    IS_UNII_INVALID(2, 6425, 6525) ||
107 		    IS_UNII_INVALID(3, 6525, 6875) ||
108 		    IS_UNII_INVALID(4, 6875, 7125))
109 			ch->flags |= IEEE80211_CHAN_DISABLED;
110 	}
111 }
112 
113 void mt7921_regd_update(struct mt792x_dev *dev)
114 {
115 	struct mt76_dev *mdev = &dev->mt76;
116 	struct ieee80211_hw *hw = mdev->hw;
117 	struct wiphy *wiphy = hw->wiphy;
118 
119 	mt7921_mcu_set_clc(dev, mdev->alpha2, dev->country_ie_env);
120 	mt7921_regd_channel_update(wiphy, dev);
121 	mt76_connac_mcu_set_channel_domain(hw->priv);
122 	mt7921_set_tx_sar_pwr(hw, NULL);
123 }
124 EXPORT_SYMBOL_GPL(mt7921_regd_update);
125 
126 static void
127 mt7921_regd_notifier(struct wiphy *wiphy,
128 		     struct regulatory_request *request)
129 {
130 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
131 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
132 	struct mt76_connac_pm *pm = &dev->pm;
133 
134 	memcpy(dev->mt76.alpha2, request->alpha2, sizeof(dev->mt76.alpha2));
135 	dev->mt76.region = request->dfs_region;
136 	dev->country_ie_env = request->country_ie_env;
137 
138 	if (pm->suspended)
139 		return;
140 
141 	mt792x_mutex_acquire(dev);
142 	mt7921_regd_update(dev);
143 	mt792x_mutex_release(dev);
144 }
145 
146 int mt7921_mac_init(struct mt792x_dev *dev)
147 {
148 	int i;
149 
150 	mt76_rmw_field(dev, MT_MDP_DCR1, MT_MDP_DCR1_MAX_RX_LEN, 1536);
151 	/* enable hardware de-agg */
152 	mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_DAMSDU_EN);
153 	/* enable hardware rx header translation */
154 	mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_RX_HDR_TRANS_EN);
155 
156 	for (i = 0; i < MT792x_WTBL_SIZE; i++)
157 		mt7921_mac_wtbl_update(dev, i,
158 				       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
159 	for (i = 0; i < 2; i++)
160 		mt792x_mac_init_band(dev, i);
161 
162 	return mt76_connac_mcu_set_rts_thresh(&dev->mt76, 0x92b, 0);
163 }
164 EXPORT_SYMBOL_GPL(mt7921_mac_init);
165 
166 static int __mt7921_init_hardware(struct mt792x_dev *dev)
167 {
168 	int ret;
169 
170 	/* force firmware operation mode into normal state,
171 	 * which should be set before firmware download stage.
172 	 */
173 	mt76_wr(dev, MT_SWDEF_MODE, MT_SWDEF_NORMAL_MODE);
174 	ret = mt792x_mcu_init(dev);
175 	if (ret)
176 		goto out;
177 
178 	mt76_eeprom_override(&dev->mphy);
179 
180 	ret = mt7921_mcu_set_eeprom(dev);
181 	if (ret)
182 		goto out;
183 
184 	ret = mt7921_mac_init(dev);
185 out:
186 	return ret;
187 }
188 
189 static int mt7921_init_hardware(struct mt792x_dev *dev)
190 {
191 	int ret, i;
192 
193 	set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
194 
195 	for (i = 0; i < MT792x_MCU_INIT_RETRY_COUNT; i++) {
196 		ret = __mt7921_init_hardware(dev);
197 		if (!ret)
198 			break;
199 
200 		mt792x_init_reset(dev);
201 	}
202 
203 	if (i == MT792x_MCU_INIT_RETRY_COUNT) {
204 		dev_err(dev->mt76.dev, "hardware init failed\n");
205 		return ret;
206 	}
207 
208 	return 0;
209 }
210 
211 static void mt7921_init_work(struct work_struct *work)
212 {
213 	struct mt792x_dev *dev = container_of(work, struct mt792x_dev,
214 					      init_work);
215 	int ret;
216 
217 	ret = mt7921_init_hardware(dev);
218 	if (ret)
219 		return;
220 
221 	mt76_set_stream_caps(&dev->mphy, true);
222 	mt7921_set_stream_he_caps(&dev->phy);
223 
224 	ret = mt76_register_device(&dev->mt76, true, mt76_rates,
225 				   ARRAY_SIZE(mt76_rates));
226 	if (ret) {
227 		dev_err(dev->mt76.dev, "register device failed\n");
228 		return;
229 	}
230 
231 	ret = mt7921_init_debugfs(dev);
232 	if (ret) {
233 		dev_err(dev->mt76.dev, "register debugfs failed\n");
234 		return;
235 	}
236 
237 	ret = mt7921_thermal_init(&dev->phy);
238 	if (ret) {
239 		dev_err(dev->mt76.dev, "thermal init failed\n");
240 		return;
241 	}
242 
243 	/* we support chip reset now */
244 	dev->hw_init_done = true;
245 
246 	mt76_connac_mcu_set_deep_sleep(&dev->mt76, dev->pm.ds_enable);
247 }
248 
249 int mt7921_register_device(struct mt792x_dev *dev)
250 {
251 	struct ieee80211_hw *hw = mt76_hw(dev);
252 	int ret;
253 
254 	dev->phy.dev = dev;
255 	dev->phy.mt76 = &dev->mt76.phy;
256 	dev->mt76.phy.priv = &dev->phy;
257 	dev->mt76.tx_worker.fn = mt792x_tx_worker;
258 
259 	INIT_DELAYED_WORK(&dev->pm.ps_work, mt792x_pm_power_save_work);
260 	INIT_WORK(&dev->pm.wake_work, mt792x_pm_wake_work);
261 	spin_lock_init(&dev->pm.wake.lock);
262 	mutex_init(&dev->pm.mutex);
263 	init_waitqueue_head(&dev->pm.wait);
264 	if (mt76_is_sdio(&dev->mt76))
265 		init_waitqueue_head(&dev->mt76.sdio.wait);
266 	spin_lock_init(&dev->pm.txq_lock);
267 	INIT_DELAYED_WORK(&dev->mphy.mac_work, mt792x_mac_work);
268 	INIT_DELAYED_WORK(&dev->phy.scan_work, mt7921_scan_work);
269 	INIT_DELAYED_WORK(&dev->coredump.work, mt7921_coredump_work);
270 #if IS_ENABLED(CONFIG_IPV6)
271 	INIT_WORK(&dev->ipv6_ns_work, mt7921_set_ipv6_ns_work);
272 	skb_queue_head_init(&dev->ipv6_ns_list);
273 #endif
274 	skb_queue_head_init(&dev->phy.scan_event_list);
275 	skb_queue_head_init(&dev->coredump.msg_list);
276 
277 	INIT_WORK(&dev->reset_work, mt7921_mac_reset_work);
278 	INIT_WORK(&dev->init_work, mt7921_init_work);
279 
280 	INIT_WORK(&dev->phy.roc_work, mt7921_roc_work);
281 	timer_setup(&dev->phy.roc_timer, mt792x_roc_timer, 0);
282 	init_waitqueue_head(&dev->phy.roc_wait);
283 
284 	dev->pm.idle_timeout = MT792x_PM_TIMEOUT;
285 	dev->pm.stats.last_wake_event = jiffies;
286 	dev->pm.stats.last_doze_event = jiffies;
287 	if (!mt76_is_usb(&dev->mt76)) {
288 		dev->pm.enable_user = true;
289 		dev->pm.enable = true;
290 		dev->pm.ds_enable_user = true;
291 		dev->pm.ds_enable = true;
292 	}
293 
294 	if (!mt76_is_mmio(&dev->mt76))
295 		hw->extra_tx_headroom += MT_SDIO_TXD_SIZE + MT_SDIO_HDR_SIZE;
296 
297 	mt792x_init_acpi_sar(dev);
298 
299 	ret = mt792x_init_wcid(dev);
300 	if (ret)
301 		return ret;
302 
303 	ret = mt792x_init_wiphy(hw);
304 	if (ret)
305 		return ret;
306 
307 	hw->wiphy->reg_notifier = mt7921_regd_notifier;
308 	dev->mphy.sband_2g.sband.ht_cap.cap |=
309 			IEEE80211_HT_CAP_LDPC_CODING |
310 			IEEE80211_HT_CAP_MAX_AMSDU;
311 	dev->mphy.sband_5g.sband.ht_cap.cap |=
312 			IEEE80211_HT_CAP_LDPC_CODING |
313 			IEEE80211_HT_CAP_MAX_AMSDU;
314 	dev->mphy.sband_5g.sband.vht_cap.cap |=
315 			IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
316 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
317 			IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
318 			IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
319 			(3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT);
320 	if (is_mt7922(&dev->mt76))
321 		dev->mphy.sband_5g.sband.vht_cap.cap |=
322 			IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
323 			IEEE80211_VHT_CAP_SHORT_GI_160;
324 
325 	dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask;
326 	dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask;
327 
328 	queue_work(system_wq, &dev->init_work);
329 
330 	return 0;
331 }
332 EXPORT_SYMBOL_GPL(mt7921_register_device);
333