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