xref: /linux/drivers/net/wireless/mediatek/mt76/mt7615/usb_mcu.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2019 MediaTek Inc.
3  *
4  * Author: Felix Fietkau <nbd@nbd.name>
5  *	   Lorenzo Bianconi <lorenzo@kernel.org>
6  *	   Sean Wang <sean.wang@mediatek.com>
7  */
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 
11 #include "mt7615.h"
12 #include "mac.h"
13 #include "mcu.h"
14 #include "regs.h"
15 
16 static int
17 mt7663u_mcu_send_message(struct mt76_dev *mdev, struct sk_buff *skb,
18 			 int cmd, int *seq)
19 {
20 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
21 	int ret, ep, len, pad;
22 
23 	mt7615_mcu_fill_msg(dev, skb, cmd, seq);
24 	if (cmd != MCU_CMD(FW_SCATTER))
25 		ep = MT_EP_OUT_INBAND_CMD;
26 	else
27 		ep = MT_EP_OUT_AC_BE;
28 
29 	len = skb->len;
30 	put_unaligned_le32(len, skb_push(skb, sizeof(len)));
31 	pad = round_up(skb->len, 4) + 4 - skb->len;
32 	ret = mt76_skb_adjust_pad(skb, pad);
33 	if (ret < 0)
34 		goto out;
35 
36 	ret = mt76u_bulk_msg(&dev->mt76, skb->data, skb->len, NULL,
37 			     1000, ep);
38 
39 out:
40 	dev_kfree_skb(skb);
41 
42 	return ret;
43 }
44 
45 int mt7663u_mcu_power_on(struct mt7615_dev *dev)
46 {
47 	int ret;
48 
49 	ret = mt76u_vendor_request(&dev->mt76, MT_VEND_POWER_ON,
50 				   USB_DIR_OUT | USB_TYPE_VENDOR,
51 				   0x0, 0x1, NULL, 0);
52 	if (ret)
53 		return ret;
54 
55 	if (!mt76_poll_msec(dev, MT_CONN_ON_MISC,
56 			    MT_TOP_MISC2_FW_PWR_ON,
57 			    FW_STATE_PWR_ON << 1, 500)) {
58 		dev_err(dev->mt76.dev, "Timeout for power on\n");
59 		ret = -EIO;
60 	}
61 
62 	return 0;
63 }
64 
65 int mt7663u_mcu_init(struct mt7615_dev *dev)
66 {
67 	static const struct mt76_mcu_ops mt7663u_mcu_ops = {
68 		.headroom = MT_USB_HDR_SIZE + sizeof(struct mt7615_mcu_txd),
69 		.tailroom = MT_USB_TAIL_SIZE,
70 		.mcu_skb_send_msg = mt7663u_mcu_send_message,
71 		.mcu_parse_response = mt7615_mcu_parse_response,
72 	};
73 	int ret;
74 
75 	dev->mt76.mcu_ops = &mt7663u_mcu_ops,
76 
77 	mt76_set(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN);
78 	if (test_and_clear_bit(MT76_STATE_POWER_OFF, &dev->mphy.state)) {
79 		ret = mt7615_mcu_restart(&dev->mt76);
80 		if (ret)
81 			return ret;
82 
83 		if (!mt76_poll_msec(dev, MT_CONN_ON_MISC,
84 				    MT_TOP_MISC2_FW_PWR_ON, 0, 500))
85 			return -EIO;
86 
87 		ret = mt7663u_mcu_power_on(dev);
88 		if (ret)
89 			return ret;
90 	}
91 
92 	ret = __mt7663_load_firmware(dev);
93 	if (ret)
94 		return ret;
95 
96 	mt76_clear(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN);
97 	set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state);
98 
99 	return 0;
100 }
101