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 	ret = mt76u_skb_dma_info(skb, skb->len);
32 	if (ret < 0)
33 		goto out;
34 
35 	ret = mt76u_bulk_msg(&dev->mt76, skb->data, skb->len, NULL,
36 			     1000, ep);
37 	dev_kfree_skb(skb);
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 
47 	return ret;
48 }
49 
50 int mt7663u_mcu_init(struct mt7615_dev *dev)
51 {
52 	static const struct mt76_mcu_ops mt7663u_mcu_ops = {
53 		.headroom = MT_USB_HDR_SIZE + sizeof(struct mt7615_mcu_txd),
54 		.tailroom = MT_USB_TAIL_SIZE,
55 		.mcu_skb_send_msg = mt7663u_mcu_send_message,
56 		.mcu_send_msg = mt7615_mcu_msg_send,
57 		.mcu_restart = mt7615_mcu_restart,
58 	};
59 	int ret;
60 
61 	dev->mt76.mcu_ops = &mt7663u_mcu_ops,
62 
63 	mt76_set(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN);
64 
65 	if (test_and_clear_bit(MT76_STATE_POWER_OFF, &dev->mphy.state)) {
66 		mt7615_mcu_restart(&dev->mt76);
67 		if (!mt76_poll_msec(dev, MT_CONN_ON_MISC,
68 				    MT_TOP_MISC2_FW_PWR_ON, 0, 500))
69 			return -EIO;
70 
71 		ret = mt76u_vendor_request(&dev->mt76, MT_VEND_POWER_ON,
72 					   USB_DIR_OUT | USB_TYPE_VENDOR,
73 					   0x0, 0x1, NULL, 0);
74 		if (ret)
75 			return ret;
76 
77 		if (!mt76_poll_msec(dev, MT_CONN_ON_MISC,
78 				    MT_TOP_MISC2_FW_PWR_ON,
79 				    FW_STATE_PWR_ON << 1, 500)) {
80 			dev_err(dev->mt76.dev, "Timeout for power on\n");
81 			return -EIO;
82 		}
83 	}
84 
85 	ret = __mt7663_load_firmware(dev);
86 	if (ret)
87 		return ret;
88 
89 	mt76_clear(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN);
90 	set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state);
91 
92 	return 0;
93 }
94