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 + sizeof(struct mt7921_mcu_txd),
129 		.tailroom = MT_USB_TAIL_SIZE,
130 		.mcu_skb_send_msg = mt7921u_mcu_send_message,
131 		.mcu_parse_response = mt7921_mcu_parse_response,
132 		.mcu_restart = mt76_connac_mcu_restart,
133 	};
134 	int ret;
135 
136 	dev->mt76.mcu_ops = &mcu_ops;
137 
138 	mt76_set(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN);
139 	ret = mt7921_run_firmware(dev);
140 	if (ret)
141 		return ret;
142 
143 	set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state);
144 	mt76_clear(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN);
145 
146 	return 0;
147 }
148 
149 static void mt7921u_stop(struct ieee80211_hw *hw)
150 {
151 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
152 
153 	mt76u_stop_tx(&dev->mt76);
154 	mt7921_stop(hw);
155 }
156 
157 static void mt7921u_cleanup(struct mt7921_dev *dev)
158 {
159 	clear_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
160 	mt7921u_wfsys_reset(dev);
161 	mt7921_mcu_exit(dev);
162 	mt76u_queues_deinit(&dev->mt76);
163 }
164 
165 static int mt7921u_probe(struct usb_interface *usb_intf,
166 			 const struct usb_device_id *id)
167 {
168 	static const struct mt76_driver_ops drv_ops = {
169 		.txwi_size = MT_SDIO_TXD_SIZE,
170 		.drv_flags = MT_DRV_RX_DMA_HDR | MT_DRV_HW_MGMT_TXQ,
171 		.survey_flags = SURVEY_INFO_TIME_TX |
172 				SURVEY_INFO_TIME_RX |
173 				SURVEY_INFO_TIME_BSS_RX,
174 		.tx_prepare_skb = mt7921_usb_sdio_tx_prepare_skb,
175 		.tx_complete_skb = mt7921_usb_sdio_tx_complete_skb,
176 		.tx_status_data = mt7921_usb_sdio_tx_status_data,
177 		.rx_skb = mt7921_queue_rx_skb,
178 		.sta_ps = mt7921_sta_ps,
179 		.sta_add = mt7921_mac_sta_add,
180 		.sta_assoc = mt7921_mac_sta_assoc,
181 		.sta_remove = mt7921_mac_sta_remove,
182 		.update_survey = mt7921_update_channel,
183 	};
184 	static const struct mt7921_hif_ops hif_ops = {
185 		.mcu_init = mt7921u_mcu_init,
186 		.init_reset = mt7921u_init_reset,
187 		.reset = mt7921u_mac_reset,
188 	};
189 	static struct mt76_bus_ops bus_ops = {
190 		.rr = mt7921u_rr,
191 		.wr = mt7921u_wr,
192 		.rmw = mt7921u_rmw,
193 		.read_copy = mt76u_read_copy,
194 		.write_copy = mt7921u_copy,
195 		.type = MT76_BUS_USB,
196 	};
197 	struct usb_device *udev = interface_to_usbdev(usb_intf);
198 	struct ieee80211_ops *ops;
199 	struct ieee80211_hw *hw;
200 	struct mt7921_dev *dev;
201 	struct mt76_dev *mdev;
202 	int ret;
203 
204 	ops = devm_kmemdup(&usb_intf->dev, &mt7921_ops, sizeof(mt7921_ops),
205 			   GFP_KERNEL);
206 	if (!ops)
207 		return -ENOMEM;
208 
209 	ops->stop = mt7921u_stop;
210 
211 	mdev = mt76_alloc_device(&usb_intf->dev, sizeof(*dev), ops, &drv_ops);
212 	if (!mdev)
213 		return -ENOMEM;
214 
215 	dev = container_of(mdev, struct mt7921_dev, mt76);
216 	dev->hif_ops = &hif_ops;
217 
218 	udev = usb_get_dev(udev);
219 	usb_reset_device(udev);
220 
221 	usb_set_intfdata(usb_intf, dev);
222 
223 	ret = __mt76u_init(mdev, usb_intf, &bus_ops);
224 	if (ret < 0)
225 		goto error;
226 
227 	mdev->rev = (mt76_rr(dev, MT_HW_CHIPID) << 16) |
228 		    (mt76_rr(dev, MT_HW_REV) & 0xff);
229 	dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev);
230 
231 	if (mt76_get_field(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY)) {
232 		ret = mt7921u_wfsys_reset(dev);
233 		if (ret)
234 			goto error;
235 	}
236 
237 	ret = mt7921u_mcu_power_on(dev);
238 	if (ret)
239 		goto error;
240 
241 	ret = mt76u_alloc_mcu_queue(&dev->mt76);
242 	if (ret)
243 		goto error;
244 
245 	ret = mt76u_alloc_queues(&dev->mt76);
246 	if (ret)
247 		goto error;
248 
249 	ret = mt7921u_dma_init(dev);
250 	if (ret)
251 		return ret;
252 
253 	hw = mt76_hw(dev);
254 	/* check hw sg support in order to enable AMSDU */
255 	hw->max_tx_fragments = mdev->usb.sg_en ? MT_HW_TXP_MAX_BUF_NUM : 1;
256 
257 	ret = mt7921_register_device(dev);
258 	if (ret)
259 		goto error;
260 
261 	return 0;
262 
263 error:
264 	mt76u_queues_deinit(&dev->mt76);
265 
266 	usb_set_intfdata(usb_intf, NULL);
267 	usb_put_dev(interface_to_usbdev(usb_intf));
268 
269 	mt76_free_device(&dev->mt76);
270 
271 	return ret;
272 }
273 
274 static void mt7921u_disconnect(struct usb_interface *usb_intf)
275 {
276 	struct mt7921_dev *dev = usb_get_intfdata(usb_intf);
277 
278 	cancel_work_sync(&dev->init_work);
279 	if (!test_bit(MT76_STATE_INITIALIZED, &dev->mphy.state))
280 		return;
281 
282 	mt76_unregister_device(&dev->mt76);
283 	mt7921u_cleanup(dev);
284 
285 	usb_set_intfdata(usb_intf, NULL);
286 	usb_put_dev(interface_to_usbdev(usb_intf));
287 
288 	mt76_free_device(&dev->mt76);
289 }
290 
291 MODULE_DEVICE_TABLE(usb, mt7921u_device_table);
292 MODULE_FIRMWARE(MT7921_FIRMWARE_WM);
293 MODULE_FIRMWARE(MT7921_ROM_PATCH);
294 
295 static struct usb_driver mt7921u_driver = {
296 	.name		= KBUILD_MODNAME,
297 	.id_table	= mt7921u_device_table,
298 	.probe		= mt7921u_probe,
299 	.disconnect	= mt7921u_disconnect,
300 	.soft_unbind	= 1,
301 	.disable_hub_initiated_lpm = 1,
302 };
303 module_usb_driver(mt7921u_driver);
304 
305 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>");
306 MODULE_LICENSE("Dual BSD/GPL");
307