xref: /linux/drivers/net/wireless/mediatek/mt76/mt7921/init.c (revision c48a7c44a1d02516309015b6134c9bb982e17008)
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 static void
114 mt7921_regd_notifier(struct wiphy *wiphy,
115 		     struct regulatory_request *request)
116 {
117 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
118 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
119 
120 	memcpy(dev->mt76.alpha2, request->alpha2, sizeof(dev->mt76.alpha2));
121 	dev->mt76.region = request->dfs_region;
122 	dev->country_ie_env = request->country_ie_env;
123 
124 	mt792x_mutex_acquire(dev);
125 	mt7921_mcu_set_clc(dev, request->alpha2, request->country_ie_env);
126 	mt76_connac_mcu_set_channel_domain(hw->priv);
127 	mt7921_set_tx_sar_pwr(hw, NULL);
128 	mt792x_mutex_release(dev);
129 
130 	mt7921_regd_channel_update(wiphy, dev);
131 }
132 
133 int mt7921_mac_init(struct mt792x_dev *dev)
134 {
135 	int i;
136 
137 	mt76_rmw_field(dev, MT_MDP_DCR1, MT_MDP_DCR1_MAX_RX_LEN, 1536);
138 	/* enable hardware de-agg */
139 	mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_DAMSDU_EN);
140 	/* enable hardware rx header translation */
141 	mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_RX_HDR_TRANS_EN);
142 
143 	for (i = 0; i < MT792x_WTBL_SIZE; i++)
144 		mt7921_mac_wtbl_update(dev, i,
145 				       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
146 	for (i = 0; i < 2; i++)
147 		mt792x_mac_init_band(dev, i);
148 
149 	return mt76_connac_mcu_set_rts_thresh(&dev->mt76, 0x92b, 0);
150 }
151 EXPORT_SYMBOL_GPL(mt7921_mac_init);
152 
153 static int __mt7921_init_hardware(struct mt792x_dev *dev)
154 {
155 	int ret;
156 
157 	/* force firmware operation mode into normal state,
158 	 * which should be set before firmware download stage.
159 	 */
160 	mt76_wr(dev, MT_SWDEF_MODE, MT_SWDEF_NORMAL_MODE);
161 	ret = mt792x_mcu_init(dev);
162 	if (ret)
163 		goto out;
164 
165 	mt76_eeprom_override(&dev->mphy);
166 
167 	ret = mt7921_mcu_set_eeprom(dev);
168 	if (ret)
169 		goto out;
170 
171 	ret = mt7921_mac_init(dev);
172 out:
173 	return ret;
174 }
175 
176 static int mt7921_init_hardware(struct mt792x_dev *dev)
177 {
178 	int ret, i;
179 
180 	set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
181 
182 	for (i = 0; i < MT792x_MCU_INIT_RETRY_COUNT; i++) {
183 		ret = __mt7921_init_hardware(dev);
184 		if (!ret)
185 			break;
186 
187 		mt792x_init_reset(dev);
188 	}
189 
190 	if (i == MT792x_MCU_INIT_RETRY_COUNT) {
191 		dev_err(dev->mt76.dev, "hardware init failed\n");
192 		return ret;
193 	}
194 
195 	return 0;
196 }
197 
198 static void mt7921_init_work(struct work_struct *work)
199 {
200 	struct mt792x_dev *dev = container_of(work, struct mt792x_dev,
201 					      init_work);
202 	int ret;
203 
204 	ret = mt7921_init_hardware(dev);
205 	if (ret)
206 		return;
207 
208 	mt76_set_stream_caps(&dev->mphy, true);
209 	mt7921_set_stream_he_caps(&dev->phy);
210 
211 	ret = mt76_register_device(&dev->mt76, true, mt76_rates,
212 				   ARRAY_SIZE(mt76_rates));
213 	if (ret) {
214 		dev_err(dev->mt76.dev, "register device failed\n");
215 		return;
216 	}
217 
218 	ret = mt7921_init_debugfs(dev);
219 	if (ret) {
220 		dev_err(dev->mt76.dev, "register debugfs failed\n");
221 		return;
222 	}
223 
224 	ret = mt7921_thermal_init(&dev->phy);
225 	if (ret) {
226 		dev_err(dev->mt76.dev, "thermal init failed\n");
227 		return;
228 	}
229 
230 	/* we support chip reset now */
231 	dev->hw_init_done = true;
232 
233 	mt76_connac_mcu_set_deep_sleep(&dev->mt76, dev->pm.ds_enable);
234 }
235 
236 int mt7921_register_device(struct mt792x_dev *dev)
237 {
238 	struct ieee80211_hw *hw = mt76_hw(dev);
239 	int ret;
240 
241 	dev->phy.dev = dev;
242 	dev->phy.mt76 = &dev->mt76.phy;
243 	dev->mt76.phy.priv = &dev->phy;
244 	dev->mt76.tx_worker.fn = mt792x_tx_worker;
245 
246 	INIT_DELAYED_WORK(&dev->pm.ps_work, mt792x_pm_power_save_work);
247 	INIT_WORK(&dev->pm.wake_work, mt792x_pm_wake_work);
248 	spin_lock_init(&dev->pm.wake.lock);
249 	mutex_init(&dev->pm.mutex);
250 	init_waitqueue_head(&dev->pm.wait);
251 	if (mt76_is_sdio(&dev->mt76))
252 		init_waitqueue_head(&dev->mt76.sdio.wait);
253 	spin_lock_init(&dev->pm.txq_lock);
254 	INIT_DELAYED_WORK(&dev->mphy.mac_work, mt792x_mac_work);
255 	INIT_DELAYED_WORK(&dev->phy.scan_work, mt7921_scan_work);
256 	INIT_DELAYED_WORK(&dev->coredump.work, mt7921_coredump_work);
257 #if IS_ENABLED(CONFIG_IPV6)
258 	INIT_WORK(&dev->ipv6_ns_work, mt7921_set_ipv6_ns_work);
259 	skb_queue_head_init(&dev->ipv6_ns_list);
260 #endif
261 	skb_queue_head_init(&dev->phy.scan_event_list);
262 	skb_queue_head_init(&dev->coredump.msg_list);
263 
264 	INIT_WORK(&dev->reset_work, mt7921_mac_reset_work);
265 	INIT_WORK(&dev->init_work, mt7921_init_work);
266 
267 	INIT_WORK(&dev->phy.roc_work, mt7921_roc_work);
268 	timer_setup(&dev->phy.roc_timer, mt792x_roc_timer, 0);
269 	init_waitqueue_head(&dev->phy.roc_wait);
270 
271 	dev->pm.idle_timeout = MT792x_PM_TIMEOUT;
272 	dev->pm.stats.last_wake_event = jiffies;
273 	dev->pm.stats.last_doze_event = jiffies;
274 	if (!mt76_is_usb(&dev->mt76)) {
275 		dev->pm.enable_user = true;
276 		dev->pm.enable = true;
277 		dev->pm.ds_enable_user = true;
278 		dev->pm.ds_enable = true;
279 	}
280 
281 	if (!mt76_is_mmio(&dev->mt76))
282 		hw->extra_tx_headroom += MT_SDIO_TXD_SIZE + MT_SDIO_HDR_SIZE;
283 
284 	mt792x_init_acpi_sar(dev);
285 
286 	ret = mt792x_init_wcid(dev);
287 	if (ret)
288 		return ret;
289 
290 	ret = mt792x_init_wiphy(hw);
291 	if (ret)
292 		return ret;
293 
294 	hw->wiphy->reg_notifier = mt7921_regd_notifier;
295 	dev->mphy.sband_2g.sband.ht_cap.cap |=
296 			IEEE80211_HT_CAP_LDPC_CODING |
297 			IEEE80211_HT_CAP_MAX_AMSDU;
298 	dev->mphy.sband_5g.sband.ht_cap.cap |=
299 			IEEE80211_HT_CAP_LDPC_CODING |
300 			IEEE80211_HT_CAP_MAX_AMSDU;
301 	dev->mphy.sband_5g.sband.vht_cap.cap |=
302 			IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
303 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
304 			IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
305 			IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
306 			(3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT);
307 	if (is_mt7922(&dev->mt76))
308 		dev->mphy.sband_5g.sband.vht_cap.cap |=
309 			IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
310 			IEEE80211_VHT_CAP_SHORT_GI_160;
311 
312 	dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask;
313 	dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask;
314 
315 	queue_work(system_wq, &dev->init_work);
316 
317 	return 0;
318 }
319 EXPORT_SYMBOL_GPL(mt7921_register_device);
320