1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2022 MediaTek Inc.
3  *
4  * Author: Lorenzo Bianconi <lorenzo@kernel.org>
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/usb.h>
10 
11 #include "mt7921.h"
12 #include "mcu.h"
13 #include "mac.h"
14 
15 static const struct usb_device_id mt7921u_device_table[] = {
16 	{ USB_DEVICE_AND_INTERFACE_INFO(0x0e8d, 0x7961, 0xff, 0xff, 0xff) },
17 	{ },
18 };
19 
20 static u32 mt7921u_rr(struct mt76_dev *dev, u32 addr)
21 {
22 	u32 ret;
23 
24 	mutex_lock(&dev->usb.usb_ctrl_mtx);
25 	ret = ___mt76u_rr(dev, MT_VEND_READ_EXT,
26 			  USB_DIR_IN | MT_USB_TYPE_VENDOR, addr);
27 	mutex_unlock(&dev->usb.usb_ctrl_mtx);
28 
29 	return ret;
30 }
31 
32 static void mt7921u_wr(struct mt76_dev *dev, u32 addr, u32 val)
33 {
34 	mutex_lock(&dev->usb.usb_ctrl_mtx);
35 	___mt76u_wr(dev, MT_VEND_WRITE_EXT,
36 		    USB_DIR_OUT | MT_USB_TYPE_VENDOR, addr, val);
37 	mutex_unlock(&dev->usb.usb_ctrl_mtx);
38 }
39 
40 static u32 mt7921u_rmw(struct mt76_dev *dev, u32 addr,
41 		       u32 mask, u32 val)
42 {
43 	mutex_lock(&dev->usb.usb_ctrl_mtx);
44 	val |= ___mt76u_rr(dev, MT_VEND_READ_EXT,
45 			   USB_DIR_IN | MT_USB_TYPE_VENDOR, addr) & ~mask;
46 	___mt76u_wr(dev, MT_VEND_WRITE_EXT,
47 		    USB_DIR_OUT | MT_USB_TYPE_VENDOR, addr, val);
48 	mutex_unlock(&dev->usb.usb_ctrl_mtx);
49 
50 	return val;
51 }
52 
53 static void mt7921u_copy(struct mt76_dev *dev, u32 offset,
54 			 const void *data, int len)
55 {
56 	struct mt76_usb *usb = &dev->usb;
57 	int ret, i = 0, batch_len;
58 	const u8 *val = data;
59 
60 	len = round_up(len, 4);
61 
62 	mutex_lock(&usb->usb_ctrl_mtx);
63 	while (i < len) {
64 		batch_len = min_t(int, usb->data_len, len - i);
65 		memcpy(usb->data, val + i, batch_len);
66 		ret = __mt76u_vendor_request(dev, MT_VEND_WRITE_EXT,
67 					     USB_DIR_OUT | MT_USB_TYPE_VENDOR,
68 					     (offset + i) >> 16, offset + i,
69 					     usb->data, batch_len);
70 		if (ret < 0)
71 			break;
72 
73 		i += batch_len;
74 	}
75 	mutex_unlock(&usb->usb_ctrl_mtx);
76 }
77 
78 int mt7921u_mcu_power_on(struct mt7921_dev *dev)
79 {
80 	int ret;
81 
82 	ret = mt76u_vendor_request(&dev->mt76, MT_VEND_POWER_ON,
83 				   USB_DIR_OUT | MT_USB_TYPE_VENDOR,
84 				   0x0, 0x1, NULL, 0);
85 	if (ret)
86 		return ret;
87 
88 	if (!mt76_poll_msec(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_PWR_ON,
89 			    MT_TOP_MISC2_FW_PWR_ON, 500)) {
90 		dev_err(dev->mt76.dev, "Timeout for power on\n");
91 		ret = -EIO;
92 	}
93 
94 	return ret;
95 }
96 
97 static int
98 mt7921u_mcu_send_message(struct mt76_dev *mdev, struct sk_buff *skb,
99 			 int cmd, int *seq)
100 {
101 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
102 	u32 pad, ep;
103 	int ret;
104 
105 	ret = mt76_connac2_mcu_fill_message(mdev, skb, cmd, seq);
106 	if (ret)
107 		return ret;
108 
109 	mdev->mcu.timeout = 3 * HZ;
110 
111 	if (cmd != MCU_CMD(FW_SCATTER))
112 		ep = MT_EP_OUT_INBAND_CMD;
113 	else
114 		ep = MT_EP_OUT_AC_BE;
115 
116 	mt7921_skb_add_usb_sdio_hdr(dev, skb, 0);
117 	pad = round_up(skb->len, 4) + 4 - skb->len;
118 	__skb_put_zero(skb, pad);
119 
120 	ret = mt76u_bulk_msg(&dev->mt76, skb->data, skb->len, NULL,
121 			     1000, ep);
122 	dev_kfree_skb(skb);
123 
124 	return ret;
125 }
126 
127 static int mt7921u_mcu_init(struct mt7921_dev *dev)
128 {
129 	static const struct mt76_mcu_ops mcu_ops = {
130 		.headroom = MT_SDIO_HDR_SIZE +
131 			    sizeof(struct mt76_connac2_mcu_txd),
132 		.tailroom = MT_USB_TAIL_SIZE,
133 		.mcu_skb_send_msg = mt7921u_mcu_send_message,
134 		.mcu_parse_response = mt7921_mcu_parse_response,
135 		.mcu_restart = mt76_connac_mcu_restart,
136 	};
137 	int ret;
138 
139 	dev->mt76.mcu_ops = &mcu_ops;
140 
141 	mt76_set(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN);
142 	ret = mt7921_run_firmware(dev);
143 	if (ret)
144 		return ret;
145 
146 	set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state);
147 	mt76_clear(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN);
148 
149 	return 0;
150 }
151 
152 static void mt7921u_stop(struct ieee80211_hw *hw)
153 {
154 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
155 
156 	mt76u_stop_tx(&dev->mt76);
157 	mt7921_stop(hw);
158 }
159 
160 static void mt7921u_cleanup(struct mt7921_dev *dev)
161 {
162 	clear_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
163 	mt7921u_wfsys_reset(dev);
164 	skb_queue_purge(&dev->mt76.mcu.res_q);
165 	mt76u_queues_deinit(&dev->mt76);
166 }
167 
168 static int mt7921u_probe(struct usb_interface *usb_intf,
169 			 const struct usb_device_id *id)
170 {
171 	static const struct mt76_driver_ops drv_ops = {
172 		.txwi_size = MT_SDIO_TXD_SIZE,
173 		.drv_flags = MT_DRV_RX_DMA_HDR | MT_DRV_HW_MGMT_TXQ,
174 		.survey_flags = SURVEY_INFO_TIME_TX |
175 				SURVEY_INFO_TIME_RX |
176 				SURVEY_INFO_TIME_BSS_RX,
177 		.tx_prepare_skb = mt7921_usb_sdio_tx_prepare_skb,
178 		.tx_complete_skb = mt7921_usb_sdio_tx_complete_skb,
179 		.tx_status_data = mt7921_usb_sdio_tx_status_data,
180 		.rx_skb = mt7921_queue_rx_skb,
181 		.rx_check = mt7921_rx_check,
182 		.sta_ps = mt7921_sta_ps,
183 		.sta_add = mt7921_mac_sta_add,
184 		.sta_assoc = mt7921_mac_sta_assoc,
185 		.sta_remove = mt7921_mac_sta_remove,
186 		.update_survey = mt7921_update_channel,
187 	};
188 	static const struct mt7921_hif_ops hif_ops = {
189 		.mcu_init = mt7921u_mcu_init,
190 		.init_reset = mt7921u_init_reset,
191 		.reset = mt7921u_mac_reset,
192 	};
193 	static struct mt76_bus_ops bus_ops = {
194 		.rr = mt7921u_rr,
195 		.wr = mt7921u_wr,
196 		.rmw = mt7921u_rmw,
197 		.read_copy = mt76u_read_copy,
198 		.write_copy = mt7921u_copy,
199 		.type = MT76_BUS_USB,
200 	};
201 	struct usb_device *udev = interface_to_usbdev(usb_intf);
202 	struct ieee80211_ops *ops;
203 	struct ieee80211_hw *hw;
204 	struct mt7921_dev *dev;
205 	struct mt76_dev *mdev;
206 	int ret;
207 
208 	ops = devm_kmemdup(&usb_intf->dev, &mt7921_ops, sizeof(mt7921_ops),
209 			   GFP_KERNEL);
210 	if (!ops)
211 		return -ENOMEM;
212 
213 	ops->stop = mt7921u_stop;
214 
215 	mdev = mt76_alloc_device(&usb_intf->dev, sizeof(*dev), ops, &drv_ops);
216 	if (!mdev)
217 		return -ENOMEM;
218 
219 	dev = container_of(mdev, struct mt7921_dev, mt76);
220 	dev->hif_ops = &hif_ops;
221 
222 	udev = usb_get_dev(udev);
223 	usb_reset_device(udev);
224 
225 	usb_set_intfdata(usb_intf, dev);
226 
227 	ret = __mt76u_init(mdev, usb_intf, &bus_ops);
228 	if (ret < 0)
229 		goto error;
230 
231 	mdev->rev = (mt76_rr(dev, MT_HW_CHIPID) << 16) |
232 		    (mt76_rr(dev, MT_HW_REV) & 0xff);
233 	dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev);
234 
235 	if (mt76_get_field(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY)) {
236 		ret = mt7921u_wfsys_reset(dev);
237 		if (ret)
238 			goto error;
239 	}
240 
241 	ret = mt7921u_mcu_power_on(dev);
242 	if (ret)
243 		goto error;
244 
245 	ret = mt76u_alloc_mcu_queue(&dev->mt76);
246 	if (ret)
247 		goto error;
248 
249 	ret = mt76u_alloc_queues(&dev->mt76);
250 	if (ret)
251 		goto error;
252 
253 	ret = mt7921u_dma_init(dev, false);
254 	if (ret)
255 		return ret;
256 
257 	hw = mt76_hw(dev);
258 	/* check hw sg support in order to enable AMSDU */
259 	hw->max_tx_fragments = mdev->usb.sg_en ? MT_HW_TXP_MAX_BUF_NUM : 1;
260 
261 	ret = mt7921_register_device(dev);
262 	if (ret)
263 		goto error;
264 
265 	return 0;
266 
267 error:
268 	mt76u_queues_deinit(&dev->mt76);
269 
270 	usb_set_intfdata(usb_intf, NULL);
271 	usb_put_dev(interface_to_usbdev(usb_intf));
272 
273 	mt76_free_device(&dev->mt76);
274 
275 	return ret;
276 }
277 
278 static void mt7921u_disconnect(struct usb_interface *usb_intf)
279 {
280 	struct mt7921_dev *dev = usb_get_intfdata(usb_intf);
281 
282 	cancel_work_sync(&dev->init_work);
283 	if (!test_bit(MT76_STATE_INITIALIZED, &dev->mphy.state))
284 		return;
285 
286 	mt76_unregister_device(&dev->mt76);
287 	mt7921u_cleanup(dev);
288 
289 	usb_set_intfdata(usb_intf, NULL);
290 	usb_put_dev(interface_to_usbdev(usb_intf));
291 
292 	mt76_free_device(&dev->mt76);
293 }
294 
295 #ifdef CONFIG_PM
296 static int mt7921u_suspend(struct usb_interface *intf, pm_message_t state)
297 {
298 	struct mt7921_dev *dev = usb_get_intfdata(intf);
299 	struct mt76_connac_pm *pm = &dev->pm;
300 	int err;
301 
302 	pm->suspended = true;
303 	flush_work(&dev->reset_work);
304 
305 	err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, true);
306 	if (err)
307 		goto failed;
308 
309 	mt76u_stop_rx(&dev->mt76);
310 	mt76u_stop_tx(&dev->mt76);
311 
312 	return 0;
313 
314 failed:
315 	pm->suspended = false;
316 
317 	if (err < 0)
318 		mt7921_reset(&dev->mt76);
319 
320 	return err;
321 }
322 
323 static int mt7921u_resume(struct usb_interface *intf)
324 {
325 	struct mt7921_dev *dev = usb_get_intfdata(intf);
326 	struct mt76_connac_pm *pm = &dev->pm;
327 	bool reinit = true;
328 	int err, i;
329 
330 	for (i = 0; i < 10; i++) {
331 		u32 val = mt76_rr(dev, MT_WF_SW_DEF_CR_USB_MCU_EVENT);
332 
333 		if (!(val & MT_WF_SW_SER_TRIGGER_SUSPEND)) {
334 			reinit = false;
335 			break;
336 		}
337 		if (val & MT_WF_SW_SER_DONE_SUSPEND) {
338 			mt76_wr(dev, MT_WF_SW_DEF_CR_USB_MCU_EVENT, 0);
339 			break;
340 		}
341 
342 		msleep(20);
343 	}
344 
345 	if (reinit || mt7921_dma_need_reinit(dev)) {
346 		err = mt7921u_dma_init(dev, true);
347 		if (err)
348 			goto failed;
349 	}
350 
351 	err = mt76u_resume_rx(&dev->mt76);
352 	if (err < 0)
353 		goto failed;
354 
355 	err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, false);
356 failed:
357 	pm->suspended = false;
358 
359 	if (err < 0)
360 		mt7921_reset(&dev->mt76);
361 
362 	return err;
363 }
364 #endif /* CONFIG_PM */
365 
366 MODULE_DEVICE_TABLE(usb, mt7921u_device_table);
367 MODULE_FIRMWARE(MT7921_FIRMWARE_WM);
368 MODULE_FIRMWARE(MT7921_ROM_PATCH);
369 
370 static struct usb_driver mt7921u_driver = {
371 	.name		= KBUILD_MODNAME,
372 	.id_table	= mt7921u_device_table,
373 	.probe		= mt7921u_probe,
374 	.disconnect	= mt7921u_disconnect,
375 #ifdef CONFIG_PM
376 	.suspend	= mt7921u_suspend,
377 	.resume		= mt7921u_resume,
378 	.reset_resume	= mt7921u_resume,
379 #endif /* CONFIG_PM */
380 	.soft_unbind	= 1,
381 	.disable_hub_initiated_lpm = 1,
382 };
383 module_usb_driver(mt7921u_driver);
384 
385 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>");
386 MODULE_LICENSE("Dual BSD/GPL");
387