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