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, bool wait_resp)
19 {
20 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
21 	int ret, seq, ep;
22 
23 	mutex_lock(&mdev->mcu.mutex);
24 
25 	mt7615_mcu_fill_msg(dev, skb, cmd, &seq);
26 	if (cmd != MCU_CMD_FW_SCATTER)
27 		ep = MT_EP_OUT_INBAND_CMD;
28 	else
29 		ep = MT_EP_OUT_AC_BE;
30 
31 	put_unaligned_le32(skb->len, skb_push(skb, sizeof(skb->len)));
32 	ret = mt76_skb_adjust_pad(skb);
33 	if (ret < 0)
34 		goto out;
35 
36 	ret = mt76u_bulk_msg(&dev->mt76, skb->data, skb->len, NULL,
37 			     1000, ep);
38 	if (ret < 0)
39 		goto out;
40 
41 	if (wait_resp)
42 		ret = mt7615_mcu_wait_response(dev, cmd, seq);
43 
44 out:
45 	mutex_unlock(&mdev->mcu.mutex);
46 	dev_kfree_skb(skb);
47 
48 	return ret;
49 }
50 
51 int mt7663u_mcu_init(struct mt7615_dev *dev)
52 {
53 	static const struct mt76_mcu_ops mt7663u_mcu_ops = {
54 		.headroom = MT_USB_HDR_SIZE + sizeof(struct mt7615_mcu_txd),
55 		.tailroom = MT_USB_TAIL_SIZE,
56 		.mcu_skb_send_msg = mt7663u_mcu_send_message,
57 		.mcu_send_msg = mt7615_mcu_msg_send,
58 		.mcu_restart = mt7615_mcu_restart,
59 	};
60 	int ret;
61 
62 	dev->mt76.mcu_ops = &mt7663u_mcu_ops,
63 
64 	/* usb does not support runtime-pm */
65 	clear_bit(MT76_STATE_PM, &dev->mphy.state);
66 	mt76_set(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN);
67 
68 	if (test_and_clear_bit(MT76_STATE_POWER_OFF, &dev->mphy.state)) {
69 		mt7615_mcu_restart(&dev->mt76);
70 		if (!mt76_poll_msec(dev, MT_CONN_ON_MISC,
71 				    MT_TOP_MISC2_FW_PWR_ON, 0, 500))
72 			return -EIO;
73 
74 		ret = mt76u_vendor_request(&dev->mt76, MT_VEND_POWER_ON,
75 					   USB_DIR_OUT | USB_TYPE_VENDOR,
76 					   0x0, 0x1, NULL, 0);
77 		if (ret)
78 			return ret;
79 
80 		if (!mt76_poll_msec(dev, MT_CONN_ON_MISC,
81 				    MT_TOP_MISC2_FW_PWR_ON,
82 				    FW_STATE_PWR_ON << 1, 500)) {
83 			dev_err(dev->mt76.dev, "Timeout for power on\n");
84 			return -EIO;
85 		}
86 	}
87 
88 	ret = __mt7663_load_firmware(dev);
89 	if (ret)
90 		return ret;
91 
92 	mt76_clear(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN);
93 	set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state);
94 
95 	return 0;
96 }
97