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