1 // SPDX-License-Identifier: BSD-3-Clause-Clear
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 ret = mt76_eeprom_override(&dev->mphy);
203 if (ret)
204 goto out;
205
206 ret = mt7921_mcu_set_eeprom(dev);
207 if (ret)
208 goto out;
209
210 ret = mt7921_mac_init(dev);
211 out:
212 return ret;
213 }
214
mt7921_init_hardware(struct mt792x_dev * dev)215 static int mt7921_init_hardware(struct mt792x_dev *dev)
216 {
217 int ret, i;
218
219 set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
220
221 for (i = 0; i < MT792x_MCU_INIT_RETRY_COUNT; i++) {
222 ret = __mt7921_init_hardware(dev);
223 if (!ret)
224 break;
225
226 mt792x_init_reset(dev);
227 }
228
229 if (i == MT792x_MCU_INIT_RETRY_COUNT) {
230 dev_err(dev->mt76.dev, "hardware init failed\n");
231 return ret;
232 }
233
234 return 0;
235 }
236
mt7921_init_work(struct work_struct * work)237 static void mt7921_init_work(struct work_struct *work)
238 {
239 struct mt792x_dev *dev = container_of(work, struct mt792x_dev,
240 init_work);
241 int ret;
242
243 ret = mt7921_init_hardware(dev);
244 if (ret)
245 return;
246
247 mt76_set_stream_caps(&dev->mphy, true);
248 mt7921_set_stream_he_caps(&dev->phy);
249 mt792x_config_mac_addr_list(dev);
250
251 ret = mt76_register_device(&dev->mt76, true, mt76_rates,
252 ARRAY_SIZE(mt76_rates));
253 if (ret) {
254 dev_err(dev->mt76.dev, "register device failed\n");
255 return;
256 }
257
258 #if !defined(__FreeBSD__) || defined(CONFIG_MT7921_DEBUGFS)
259 ret = mt7921_init_debugfs(dev);
260 if (ret) {
261 dev_err(dev->mt76.dev, "register debugfs failed\n");
262 return;
263 }
264 #endif
265
266 #if defined(__linux__)
267 ret = mt7921_thermal_init(&dev->phy);
268 if (ret) {
269 dev_err(dev->mt76.dev, "thermal init failed\n");
270 return;
271 }
272 #endif
273
274 /* we support chip reset now */
275 dev->hw_init_done = true;
276
277 mt76_connac_mcu_set_deep_sleep(&dev->mt76, dev->pm.ds_enable);
278 }
279
mt7921_register_device(struct mt792x_dev * dev)280 int mt7921_register_device(struct mt792x_dev *dev)
281 {
282 struct ieee80211_hw *hw = mt76_hw(dev);
283 int ret;
284
285 dev->phy.dev = dev;
286 dev->phy.mt76 = &dev->mt76.phy;
287 dev->mt76.phy.priv = &dev->phy;
288 dev->mt76.tx_worker.fn = mt792x_tx_worker;
289
290 INIT_DELAYED_WORK(&dev->pm.ps_work, mt792x_pm_power_save_work);
291 INIT_WORK(&dev->pm.wake_work, mt792x_pm_wake_work);
292 spin_lock_init(&dev->pm.wake.lock);
293 mutex_init(&dev->pm.mutex);
294 init_waitqueue_head(&dev->pm.wait);
295 init_waitqueue_head(&dev->wait);
296 if (mt76_is_sdio(&dev->mt76))
297 init_waitqueue_head(&dev->mt76.sdio.wait);
298 spin_lock_init(&dev->pm.txq_lock);
299 INIT_DELAYED_WORK(&dev->mphy.mac_work, mt792x_mac_work);
300 INIT_DELAYED_WORK(&dev->phy.scan_work, mt7921_scan_work);
301 INIT_DELAYED_WORK(&dev->coredump.work, mt7921_coredump_work);
302 #if IS_ENABLED(CONFIG_IPV6)
303 INIT_WORK(&dev->ipv6_ns_work, mt7921_set_ipv6_ns_work);
304 skb_queue_head_init(&dev->ipv6_ns_list);
305 #endif
306 skb_queue_head_init(&dev->phy.scan_event_list);
307 skb_queue_head_init(&dev->coredump.msg_list);
308
309 INIT_WORK(&dev->reset_work, mt7921_mac_reset_work);
310 INIT_WORK(&dev->init_work, mt7921_init_work);
311
312 INIT_WORK(&dev->phy.roc_work, mt7921_roc_work);
313 timer_setup(&dev->phy.roc_timer, mt792x_roc_timer, 0);
314 init_waitqueue_head(&dev->phy.roc_wait);
315
316 dev->pm.idle_timeout = MT792x_PM_TIMEOUT;
317 dev->pm.stats.last_wake_event = jiffies;
318 dev->pm.stats.last_doze_event = jiffies;
319 if (!mt76_is_usb(&dev->mt76)) {
320 dev->pm.enable_user = true;
321 dev->pm.enable = true;
322 dev->pm.ds_enable_user = true;
323 dev->pm.ds_enable = true;
324 }
325
326 if (!mt76_is_mmio(&dev->mt76))
327 hw->extra_tx_headroom += MT_SDIO_TXD_SIZE + MT_SDIO_HDR_SIZE;
328
329 mt792x_init_acpi_sar(dev);
330
331 ret = mt792x_init_wcid(dev);
332 if (ret)
333 return ret;
334
335 ret = mt792x_init_wiphy(hw);
336 if (ret)
337 return ret;
338
339 hw->wiphy->reg_notifier = mt7921_regd_notifier;
340 dev->mphy.sband_2g.sband.ht_cap.cap |=
341 IEEE80211_HT_CAP_LDPC_CODING |
342 IEEE80211_HT_CAP_MAX_AMSDU;
343 dev->mphy.sband_5g.sband.ht_cap.cap |=
344 IEEE80211_HT_CAP_LDPC_CODING |
345 IEEE80211_HT_CAP_MAX_AMSDU;
346 dev->mphy.sband_5g.sband.vht_cap.cap |=
347 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
348 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
349 IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
350 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
351 (3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT);
352 if (is_mt7922(&dev->mt76))
353 dev->mphy.sband_5g.sband.vht_cap.cap |=
354 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
355 IEEE80211_VHT_CAP_SHORT_GI_160;
356
357 dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask;
358 dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask;
359
360 queue_work(system_percpu_wq, &dev->init_work);
361
362 return 0;
363 }
364 EXPORT_SYMBOL_GPL(mt7921_register_device);
365