xref: /linux/drivers/net/wireless/mediatek/mt76/mt7921/usb.c (revision fc6ee71a2a8f2d183724e3f97762e93c5102425c)
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 = mt7921_mcu_fill_message(mdev, skb, cmd, seq);
106 	if (ret)
107 		return ret;
108 
109 	if (cmd != MCU_CMD(FW_SCATTER))
110 		ep = MT_EP_OUT_INBAND_CMD;
111 	else
112 		ep = MT_EP_OUT_AC_BE;
113 
114 	mt7921_skb_add_usb_sdio_hdr(dev, skb, 0);
115 	pad = round_up(skb->len, 4) + 4 - skb->len;
116 	__skb_put_zero(skb, pad);
117 
118 	ret = mt76u_bulk_msg(&dev->mt76, skb->data, skb->len, NULL,
119 			     1000, ep);
120 	dev_kfree_skb(skb);
121 
122 	return ret;
123 }
124 
125 static int mt7921u_mcu_init(struct mt7921_dev *dev)
126 {
127 	static const struct mt76_mcu_ops mcu_ops = {
128 		.headroom = MT_SDIO_HDR_SIZE +
129 			    sizeof(struct mt76_connac2_mcu_txd),
130 		.tailroom = MT_USB_TAIL_SIZE,
131 		.mcu_skb_send_msg = mt7921u_mcu_send_message,
132 		.mcu_parse_response = mt7921_mcu_parse_response,
133 		.mcu_restart = mt76_connac_mcu_restart,
134 	};
135 	int ret;
136 
137 	dev->mt76.mcu_ops = &mcu_ops;
138 
139 	mt76_set(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN);
140 	ret = mt7921_run_firmware(dev);
141 	if (ret)
142 		return ret;
143 
144 	set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state);
145 	mt76_clear(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN);
146 
147 	return 0;
148 }
149 
150 static void mt7921u_stop(struct ieee80211_hw *hw)
151 {
152 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
153 
154 	mt76u_stop_tx(&dev->mt76);
155 	mt7921_stop(hw);
156 }
157 
158 static void mt7921u_cleanup(struct mt7921_dev *dev)
159 {
160 	clear_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
161 	mt7921u_wfsys_reset(dev);
162 	skb_queue_purge(&dev->mt76.mcu.res_q);
163 	mt76u_queues_deinit(&dev->mt76);
164 }
165 
166 static int mt7921u_probe(struct usb_interface *usb_intf,
167 			 const struct usb_device_id *id)
168 {
169 	static const struct mt76_driver_ops drv_ops = {
170 		.txwi_size = MT_SDIO_TXD_SIZE,
171 		.drv_flags = MT_DRV_RX_DMA_HDR | MT_DRV_HW_MGMT_TXQ,
172 		.survey_flags = SURVEY_INFO_TIME_TX |
173 				SURVEY_INFO_TIME_RX |
174 				SURVEY_INFO_TIME_BSS_RX,
175 		.tx_prepare_skb = mt7921_usb_sdio_tx_prepare_skb,
176 		.tx_complete_skb = mt7921_usb_sdio_tx_complete_skb,
177 		.tx_status_data = mt7921_usb_sdio_tx_status_data,
178 		.rx_skb = mt7921_queue_rx_skb,
179 		.sta_ps = mt7921_sta_ps,
180 		.sta_add = mt7921_mac_sta_add,
181 		.sta_assoc = mt7921_mac_sta_assoc,
182 		.sta_remove = mt7921_mac_sta_remove,
183 		.update_survey = mt7921_update_channel,
184 	};
185 	static const struct mt7921_hif_ops hif_ops = {
186 		.mcu_init = mt7921u_mcu_init,
187 		.init_reset = mt7921u_init_reset,
188 		.reset = mt7921u_mac_reset,
189 	};
190 	static struct mt76_bus_ops bus_ops = {
191 		.rr = mt7921u_rr,
192 		.wr = mt7921u_wr,
193 		.rmw = mt7921u_rmw,
194 		.read_copy = mt76u_read_copy,
195 		.write_copy = mt7921u_copy,
196 		.type = MT76_BUS_USB,
197 	};
198 	struct usb_device *udev = interface_to_usbdev(usb_intf);
199 	struct ieee80211_ops *ops;
200 	struct ieee80211_hw *hw;
201 	struct mt7921_dev *dev;
202 	struct mt76_dev *mdev;
203 	int ret;
204 
205 	ops = devm_kmemdup(&usb_intf->dev, &mt7921_ops, sizeof(mt7921_ops),
206 			   GFP_KERNEL);
207 	if (!ops)
208 		return -ENOMEM;
209 
210 	ops->stop = mt7921u_stop;
211 
212 	mdev = mt76_alloc_device(&usb_intf->dev, sizeof(*dev), ops, &drv_ops);
213 	if (!mdev)
214 		return -ENOMEM;
215 
216 	dev = container_of(mdev, struct mt7921_dev, mt76);
217 	dev->hif_ops = &hif_ops;
218 
219 	udev = usb_get_dev(udev);
220 	usb_reset_device(udev);
221 
222 	usb_set_intfdata(usb_intf, dev);
223 
224 	ret = __mt76u_init(mdev, usb_intf, &bus_ops);
225 	if (ret < 0)
226 		goto error;
227 
228 	mdev->rev = (mt76_rr(dev, MT_HW_CHIPID) << 16) |
229 		    (mt76_rr(dev, MT_HW_REV) & 0xff);
230 	dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev);
231 
232 	if (mt76_get_field(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY)) {
233 		ret = mt7921u_wfsys_reset(dev);
234 		if (ret)
235 			goto error;
236 	}
237 
238 	ret = mt7921u_mcu_power_on(dev);
239 	if (ret)
240 		goto error;
241 
242 	ret = mt76u_alloc_mcu_queue(&dev->mt76);
243 	if (ret)
244 		goto error;
245 
246 	ret = mt76u_alloc_queues(&dev->mt76);
247 	if (ret)
248 		goto error;
249 
250 	ret = mt7921u_dma_init(dev, false);
251 	if (ret)
252 		return ret;
253 
254 	hw = mt76_hw(dev);
255 	/* check hw sg support in order to enable AMSDU */
256 	hw->max_tx_fragments = mdev->usb.sg_en ? MT_HW_TXP_MAX_BUF_NUM : 1;
257 
258 	ret = mt7921_register_device(dev);
259 	if (ret)
260 		goto error;
261 
262 	return 0;
263 
264 error:
265 	mt76u_queues_deinit(&dev->mt76);
266 
267 	usb_set_intfdata(usb_intf, NULL);
268 	usb_put_dev(interface_to_usbdev(usb_intf));
269 
270 	mt76_free_device(&dev->mt76);
271 
272 	return ret;
273 }
274 
275 static void mt7921u_disconnect(struct usb_interface *usb_intf)
276 {
277 	struct mt7921_dev *dev = usb_get_intfdata(usb_intf);
278 
279 	cancel_work_sync(&dev->init_work);
280 	if (!test_bit(MT76_STATE_INITIALIZED, &dev->mphy.state))
281 		return;
282 
283 	mt76_unregister_device(&dev->mt76);
284 	mt7921u_cleanup(dev);
285 
286 	usb_set_intfdata(usb_intf, NULL);
287 	usb_put_dev(interface_to_usbdev(usb_intf));
288 
289 	mt76_free_device(&dev->mt76);
290 }
291 
292 #ifdef CONFIG_PM
293 static int mt7921u_suspend(struct usb_interface *intf, pm_message_t state)
294 {
295 	struct mt7921_dev *dev = usb_get_intfdata(intf);
296 	int err;
297 
298 	err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, true);
299 	if (err)
300 		return err;
301 
302 	mt76u_stop_rx(&dev->mt76);
303 	mt76u_stop_tx(&dev->mt76);
304 
305 	set_bit(MT76_STATE_SUSPEND, &dev->mphy.state);
306 
307 	return 0;
308 }
309 
310 static int mt7921u_resume(struct usb_interface *intf)
311 {
312 	struct mt7921_dev *dev = usb_get_intfdata(intf);
313 	bool reinit = true;
314 	int err, i;
315 
316 	for (i = 0; i < 10; i++) {
317 		u32 val = mt76_rr(dev, MT_WF_SW_DEF_CR_USB_MCU_EVENT);
318 
319 		if (!(val & MT_WF_SW_SER_TRIGGER_SUSPEND)) {
320 			reinit = false;
321 			break;
322 		}
323 		if (val & MT_WF_SW_SER_DONE_SUSPEND) {
324 			mt76_wr(dev, MT_WF_SW_DEF_CR_USB_MCU_EVENT, 0);
325 			break;
326 		}
327 
328 		msleep(20);
329 	}
330 
331 	if (reinit || mt7921_dma_need_reinit(dev)) {
332 		err = mt7921u_dma_init(dev, true);
333 		if (err)
334 			return err;
335 	}
336 
337 	clear_bit(MT76_STATE_SUSPEND, &dev->mphy.state);
338 
339 	err = mt76u_resume_rx(&dev->mt76);
340 	if (err < 0)
341 		return err;
342 
343 	return mt76_connac_mcu_set_hif_suspend(&dev->mt76, false);
344 }
345 #endif /* CONFIG_PM */
346 
347 MODULE_DEVICE_TABLE(usb, mt7921u_device_table);
348 MODULE_FIRMWARE(MT7921_FIRMWARE_WM);
349 MODULE_FIRMWARE(MT7921_ROM_PATCH);
350 
351 static struct usb_driver mt7921u_driver = {
352 	.name		= KBUILD_MODNAME,
353 	.id_table	= mt7921u_device_table,
354 	.probe		= mt7921u_probe,
355 	.disconnect	= mt7921u_disconnect,
356 #ifdef CONFIG_PM
357 	.suspend	= mt7921u_suspend,
358 	.resume		= mt7921u_resume,
359 	.reset_resume	= mt7921u_resume,
360 #endif /* CONFIG_PM */
361 	.soft_unbind	= 1,
362 	.disable_hub_initiated_lpm = 1,
363 };
364 module_usb_driver(mt7921u_driver);
365 
366 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>");
367 MODULE_LICENSE("Dual BSD/GPL");
368