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