File Index
Files and directories
Sign in
FILE

init.c

File preview with syntax highlighting.

linux-6.19.14/drivers/net/wireless/mediatek/mt76/mt7921/init.c C
  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
 13static 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}
 36static SENSOR_DEVICE_ATTR_RO(temp1_input, mt7921_thermal_temp, 0);
 37
 38static struct attribute *mt7921_hwmon_attrs[] = {
 39	&sensor_dev_attr_temp1_input.dev_attr.attr,
 40	NULL,
 41};
 42ATTRIBUTE_GROUPS(mt7921_hwmon);
 43
 44static 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
 63static void
 64mt7921_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
115void 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}
126EXPORT_SYMBOL_GPL(mt7921_regd_update);
127
128static void
129mt7921_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 (request->initiator == NL80211_REGDOM_SET_BY_USER) {
141		if (dev->mt76.alpha2[0] == '0' && dev->mt76.alpha2[1] == '0')
142			wiphy->regulatory_flags &= ~REGULATORY_COUNTRY_IE_IGNORE;
143		else
144			wiphy->regulatory_flags |= REGULATORY_COUNTRY_IE_IGNORE;
145	}
146
147	if (pm->suspended)
148		return;
149
150	dev->regd_in_progress = true;
151
152	mt792x_mutex_acquire(dev);
153	mt7921_regd_update(dev);
154	mt792x_mutex_release(dev);
155
156	dev->regd_in_progress = false;
157	wake_up(&dev->wait);
158}
159
160int mt7921_mac_init(struct mt792x_dev *dev)
161{
162	int i;
163
164	mt76_rmw_field(dev, MT_MDP_DCR1, MT_MDP_DCR1_MAX_RX_LEN, 1536);
165	/* enable hardware de-agg */
166	mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_DAMSDU_EN);
167	/* enable hardware rx header translation */
168	mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_RX_HDR_TRANS_EN);
169
170	for (i = 0; i < MT792x_WTBL_SIZE; i++)
171		mt7921_mac_wtbl_update(dev, i,
172				       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
173	for (i = 0; i < 2; i++)
174		mt792x_mac_init_band(dev, i);
175
176	return mt76_connac_mcu_set_rts_thresh(&dev->mt76, 0x92b, 0);
177}
178EXPORT_SYMBOL_GPL(mt7921_mac_init);
179
180static int __mt7921_init_hardware(struct mt792x_dev *dev)
181{
182	int ret;
183
184	/* force firmware operation mode into normal state,
185	 * which should be set before firmware download stage.
186	 */
187	mt76_wr(dev, MT_SWDEF_MODE, MT_SWDEF_NORMAL_MODE);
188	ret = mt792x_mcu_init(dev);
189	if (ret)
190		goto out;
191
192	ret = mt76_eeprom_override(&dev->mphy);
193	if (ret)
194		goto out;
195
196	ret = mt7921_mcu_set_eeprom(dev);
197	if (ret)
198		goto out;
199
200	ret = mt7921_mac_init(dev);
201out:
202	return ret;
203}
204
205static int mt7921_init_hardware(struct mt792x_dev *dev)
206{
207	int ret, i;
208
209	set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
210
211	for (i = 0; i < MT792x_MCU_INIT_RETRY_COUNT; i++) {
212		ret = __mt7921_init_hardware(dev);
213		if (!ret)
214			break;
215
216		mt792x_init_reset(dev);
217	}
218
219	if (i == MT792x_MCU_INIT_RETRY_COUNT) {
220		dev_err(dev->mt76.dev, "hardware init failed\n");
221		return ret;
222	}
223
224	return 0;
225}
226
227static void mt7921_init_work(struct work_struct *work)
228{
229	struct mt792x_dev *dev = container_of(work, struct mt792x_dev,
230					      init_work);
231	int ret;
232
233	ret = mt7921_init_hardware(dev);
234	if (ret)
235		return;
236
237	mt76_set_stream_caps(&dev->mphy, true);
238	mt7921_set_stream_he_caps(&dev->phy);
239	mt792x_config_mac_addr_list(dev);
240
241	ret = mt76_register_device(&dev->mt76, true, mt76_rates,
242				   ARRAY_SIZE(mt76_rates));
243	if (ret) {
244		dev_err(dev->mt76.dev, "register device failed\n");
245		return;
246	}
247
248	ret = mt7921_init_debugfs(dev);
249	if (ret) {
250		dev_err(dev->mt76.dev, "register debugfs failed\n");
251		return;
252	}
253
254	ret = mt7921_thermal_init(&dev->phy);
255	if (ret) {
256		dev_err(dev->mt76.dev, "thermal init failed\n");
257		return;
258	}
259
260	/* we support chip reset now */
261	dev->hw_init_done = true;
262
263	mt76_connac_mcu_set_deep_sleep(&dev->mt76, dev->pm.ds_enable);
264}
265
266int mt7921_register_device(struct mt792x_dev *dev)
267{
268	struct ieee80211_hw *hw = mt76_hw(dev);
269	int ret;
270
271	dev->phy.dev = dev;
272	dev->phy.mt76 = &dev->mt76.phy;
273	dev->mt76.phy.priv = &dev->phy;
274	dev->mt76.tx_worker.fn = mt792x_tx_worker;
275
276	INIT_DELAYED_WORK(&dev->pm.ps_work, mt792x_pm_power_save_work);
277	INIT_WORK(&dev->pm.wake_work, mt792x_pm_wake_work);
278	spin_lock_init(&dev->pm.wake.lock);
279	mutex_init(&dev->pm.mutex);
280	init_waitqueue_head(&dev->pm.wait);
281	init_waitqueue_head(&dev->wait);
282	if (mt76_is_sdio(&dev->mt76))
283		init_waitqueue_head(&dev->mt76.sdio.wait);
284	spin_lock_init(&dev->pm.txq_lock);
285	INIT_DELAYED_WORK(&dev->mphy.mac_work, mt792x_mac_work);
286	INIT_DELAYED_WORK(&dev->phy.scan_work, mt7921_scan_work);
287	INIT_DELAYED_WORK(&dev->coredump.work, mt7921_coredump_work);
288#if IS_ENABLED(CONFIG_IPV6)
289	INIT_WORK(&dev->ipv6_ns_work, mt7921_set_ipv6_ns_work);
290	skb_queue_head_init(&dev->ipv6_ns_list);
291#endif
292	skb_queue_head_init(&dev->phy.scan_event_list);
293	skb_queue_head_init(&dev->coredump.msg_list);
294
295	INIT_WORK(&dev->reset_work, mt7921_mac_reset_work);
296	INIT_WORK(&dev->init_work, mt7921_init_work);
297
298	INIT_WORK(&dev->phy.roc_work, mt7921_roc_work);
299	timer_setup(&dev->phy.roc_timer, mt792x_roc_timer, 0);
300	init_waitqueue_head(&dev->phy.roc_wait);
301
302	dev->pm.idle_timeout = MT792x_PM_TIMEOUT;
303	dev->pm.stats.last_wake_event = jiffies;
304	dev->pm.stats.last_doze_event = jiffies;
305	if (!mt76_is_usb(&dev->mt76)) {
306		dev->pm.enable_user = true;
307		dev->pm.enable = true;
308		dev->pm.ds_enable_user = true;
309		dev->pm.ds_enable = true;
310	}
311
312	if (!mt76_is_mmio(&dev->mt76))
313		hw->extra_tx_headroom += MT_SDIO_TXD_SIZE + MT_SDIO_HDR_SIZE;
314
315	mt792x_init_acpi_sar(dev);
316
317	ret = mt792x_init_wcid(dev);
318	if (ret)
319		return ret;
320
321	ret = mt792x_init_wiphy(hw);
322	if (ret)
323		return ret;
324
325	hw->wiphy->reg_notifier = mt7921_regd_notifier;
326	dev->mphy.sband_2g.sband.ht_cap.cap |=
327			IEEE80211_HT_CAP_LDPC_CODING |
328			IEEE80211_HT_CAP_MAX_AMSDU;
329	dev->mphy.sband_5g.sband.ht_cap.cap |=
330			IEEE80211_HT_CAP_LDPC_CODING |
331			IEEE80211_HT_CAP_MAX_AMSDU;
332	dev->mphy.sband_5g.sband.vht_cap.cap |=
333			IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
334			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
335			IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
336			IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
337			(3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT);
338	if (is_mt7922(&dev->mt76))
339		dev->mphy.sband_5g.sband.vht_cap.cap |=
340			IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
341			IEEE80211_VHT_CAP_SHORT_GI_160;
342
343	dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask;
344	dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask;
345
346	queue_work(system_percpu_wq, &dev->init_work);
347
348	return 0;
349}
350EXPORT_SYMBOL_GPL(mt7921_register_device);
~/ Read-only file preview.