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