xref: /linux/drivers/net/wireless/mediatek/mt76/mt7925/init.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2023 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 "mt7925.h"
10 #include "mac.h"
11 #include "mcu.h"
12 
13 static ssize_t mt7925_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 = mt7925_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, mt7925_thermal_temp, 0);
37 
38 static struct attribute *mt7925_hwmon_attrs[] = {
39 	&sensor_dev_attr_temp1_input.dev_attr.attr,
40 	NULL,
41 };
42 ATTRIBUTE_GROUPS(mt7925_hwmon);
43 
44 static int mt7925_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, "mt7925_%s",
54 			      wiphy_name(wiphy));
55 
56 	hwmon = devm_hwmon_device_register_with_groups(&wiphy->dev, name, phy,
57 						       mt7925_hwmon_groups);
58 	return PTR_ERR_OR_ZERO(hwmon);
59 }
60 static void
61 mt7925_regd_notifier(struct wiphy *wiphy,
62 		     struct regulatory_request *req)
63 {
64 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
65 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
66 	struct mt76_dev *mdev = &dev->mt76;
67 
68 	/* allow world regdom at the first boot only */
69 	if (!memcmp(req->alpha2, "00", 2) &&
70 	    mdev->alpha2[0] && mdev->alpha2[1])
71 		return;
72 
73 	/* do not need to update the same country twice */
74 	if (!memcmp(req->alpha2, mdev->alpha2, 2) &&
75 	    dev->country_ie_env == req->country_ie_env)
76 		return;
77 
78 	memcpy(mdev->alpha2, req->alpha2, 2);
79 	mdev->region = req->dfs_region;
80 	dev->country_ie_env = req->country_ie_env;
81 
82 	mt792x_mutex_acquire(dev);
83 	mt7925_mcu_set_clc(dev, req->alpha2, req->country_ie_env);
84 	mt7925_mcu_set_channel_domain(hw->priv);
85 	mt7925_set_tx_sar_pwr(hw, NULL);
86 	mt792x_mutex_release(dev);
87 }
88 
89 static void mt7925_mac_init_basic_rates(struct mt792x_dev *dev)
90 {
91 	int i;
92 
93 	for (i = 0; i < ARRAY_SIZE(mt76_rates); i++) {
94 		u16 rate = mt76_rates[i].hw_value;
95 		u16 idx = MT792x_BASIC_RATES_TBL + i;
96 
97 		rate = FIELD_PREP(MT_TX_RATE_MODE, rate >> 8) |
98 		       FIELD_PREP(MT_TX_RATE_IDX, rate & GENMASK(7, 0));
99 		mt7925_mac_set_fixed_rate_table(dev, idx, rate);
100 	}
101 }
102 
103 int mt7925_mac_init(struct mt792x_dev *dev)
104 {
105 	int i;
106 
107 	mt76_rmw_field(dev, MT_MDP_DCR1, MT_MDP_DCR1_MAX_RX_LEN, 1536);
108 	/* enable hardware de-agg */
109 	mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_DAMSDU_EN);
110 
111 	for (i = 0; i < MT792x_WTBL_SIZE; i++)
112 		mt7925_mac_wtbl_update(dev, i,
113 				       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
114 	for (i = 0; i < 2; i++)
115 		mt792x_mac_init_band(dev, i);
116 
117 	mt7925_mac_init_basic_rates(dev);
118 
119 	memzero_explicit(&dev->mt76.alpha2, sizeof(dev->mt76.alpha2));
120 
121 	return 0;
122 }
123 EXPORT_SYMBOL_GPL(mt7925_mac_init);
124 
125 static int __mt7925_init_hardware(struct mt792x_dev *dev)
126 {
127 	int ret;
128 
129 	ret = mt792x_mcu_init(dev);
130 	if (ret)
131 		goto out;
132 
133 	mt76_eeprom_override(&dev->mphy);
134 
135 	ret = mt7925_mcu_set_eeprom(dev);
136 	if (ret)
137 		goto out;
138 
139 	ret = mt7925_mac_init(dev);
140 	if (ret)
141 		goto out;
142 
143 out:
144 	return ret;
145 }
146 
147 static int mt7925_init_hardware(struct mt792x_dev *dev)
148 {
149 	int ret, i;
150 
151 	set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
152 
153 	for (i = 0; i < MT792x_MCU_INIT_RETRY_COUNT; i++) {
154 		ret = __mt7925_init_hardware(dev);
155 		if (!ret)
156 			break;
157 
158 		mt792x_init_reset(dev);
159 	}
160 
161 	if (i == MT792x_MCU_INIT_RETRY_COUNT) {
162 		dev_err(dev->mt76.dev, "hardware init failed\n");
163 		return ret;
164 	}
165 
166 	return 0;
167 }
168 
169 static void mt7925_init_work(struct work_struct *work)
170 {
171 	struct mt792x_dev *dev = container_of(work, struct mt792x_dev,
172 					      init_work);
173 	int ret;
174 
175 	ret = mt7925_init_hardware(dev);
176 	if (ret)
177 		return;
178 
179 	mt76_set_stream_caps(&dev->mphy, true);
180 	mt7925_set_stream_he_eht_caps(&dev->phy);
181 
182 	ret = mt7925_init_mlo_caps(&dev->phy);
183 	if (ret) {
184 		dev_err(dev->mt76.dev, "MLO init failed\n");
185 		return;
186 	}
187 
188 	ret = mt76_register_device(&dev->mt76, true, mt76_rates,
189 				   ARRAY_SIZE(mt76_rates));
190 	if (ret) {
191 		dev_err(dev->mt76.dev, "register device failed\n");
192 		return;
193 	}
194 
195 	ret = mt7925_init_debugfs(dev);
196 	if (ret) {
197 		dev_err(dev->mt76.dev, "register debugfs failed\n");
198 		return;
199 	}
200 
201 	ret = mt7925_thermal_init(&dev->phy);
202 	if (ret) {
203 		dev_err(dev->mt76.dev, "thermal init failed\n");
204 		return;
205 	}
206 
207 	/* we support chip reset now */
208 	dev->hw_init_done = true;
209 
210 	mt7925_mcu_set_deep_sleep(dev, dev->pm.ds_enable);
211 }
212 
213 int mt7925_register_device(struct mt792x_dev *dev)
214 {
215 	struct ieee80211_hw *hw = mt76_hw(dev);
216 	int ret;
217 
218 	dev->phy.dev = dev;
219 	dev->phy.mt76 = &dev->mt76.phy;
220 	dev->mt76.phy.priv = &dev->phy;
221 	dev->mt76.tx_worker.fn = mt792x_tx_worker;
222 
223 	INIT_DELAYED_WORK(&dev->pm.ps_work, mt792x_pm_power_save_work);
224 	INIT_WORK(&dev->pm.wake_work, mt792x_pm_wake_work);
225 	spin_lock_init(&dev->pm.wake.lock);
226 	mutex_init(&dev->pm.mutex);
227 	init_waitqueue_head(&dev->pm.wait);
228 	spin_lock_init(&dev->pm.txq_lock);
229 	INIT_DELAYED_WORK(&dev->mphy.mac_work, mt792x_mac_work);
230 	INIT_DELAYED_WORK(&dev->phy.scan_work, mt7925_scan_work);
231 	INIT_DELAYED_WORK(&dev->coredump.work, mt7925_coredump_work);
232 #if IS_ENABLED(CONFIG_IPV6)
233 	INIT_WORK(&dev->ipv6_ns_work, mt7925_set_ipv6_ns_work);
234 	skb_queue_head_init(&dev->ipv6_ns_list);
235 #endif
236 	skb_queue_head_init(&dev->phy.scan_event_list);
237 	skb_queue_head_init(&dev->coredump.msg_list);
238 
239 	INIT_WORK(&dev->reset_work, mt7925_mac_reset_work);
240 	INIT_WORK(&dev->init_work, mt7925_init_work);
241 
242 	INIT_WORK(&dev->phy.roc_work, mt7925_roc_work);
243 	timer_setup(&dev->phy.roc_timer, mt792x_roc_timer, 0);
244 	init_waitqueue_head(&dev->phy.roc_wait);
245 
246 	dev->pm.idle_timeout = MT792x_PM_TIMEOUT;
247 	dev->pm.stats.last_wake_event = jiffies;
248 	dev->pm.stats.last_doze_event = jiffies;
249 	if (!mt76_is_usb(&dev->mt76)) {
250 		dev->pm.enable_user = true;
251 		dev->pm.enable = true;
252 		dev->pm.ds_enable_user = true;
253 		dev->pm.ds_enable = true;
254 	}
255 
256 	if (!mt76_is_mmio(&dev->mt76))
257 		hw->extra_tx_headroom += MT_SDIO_TXD_SIZE + MT_SDIO_HDR_SIZE;
258 
259 	mt792x_init_acpi_sar(dev);
260 
261 	ret = mt792x_init_wcid(dev);
262 	if (ret)
263 		return ret;
264 
265 	ret = mt792x_init_wiphy(hw);
266 	if (ret)
267 		return ret;
268 
269 	hw->wiphy->reg_notifier = mt7925_regd_notifier;
270 	dev->mphy.sband_2g.sband.ht_cap.cap |=
271 			IEEE80211_HT_CAP_LDPC_CODING |
272 			IEEE80211_HT_CAP_MAX_AMSDU;
273 	dev->mphy.sband_2g.sband.ht_cap.ampdu_density =
274 			IEEE80211_HT_MPDU_DENSITY_2;
275 	dev->mphy.sband_5g.sband.ht_cap.cap |=
276 			IEEE80211_HT_CAP_LDPC_CODING |
277 			IEEE80211_HT_CAP_MAX_AMSDU;
278 	dev->mphy.sband_2g.sband.ht_cap.ampdu_density =
279 			IEEE80211_HT_MPDU_DENSITY_1;
280 	dev->mphy.sband_5g.sband.vht_cap.cap |=
281 			IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
282 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
283 			IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
284 			IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
285 			(3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT);
286 	dev->mphy.sband_5g.sband.vht_cap.cap |=
287 			IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
288 			IEEE80211_VHT_CAP_SHORT_GI_160;
289 
290 	dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask;
291 	dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask;
292 
293 	queue_work(system_wq, &dev->init_work);
294 
295 	return 0;
296 }
297 EXPORT_SYMBOL_GPL(mt7925_register_device);
298