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 	/* usb does not support runtime-pm */
59 	clear_bit(MT76_STATE_PM, &dev->mphy.state);
60 	mt76_set(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN);
61 
62 	if (test_and_clear_bit(MT76_STATE_POWER_OFF, &dev->mphy.state)) {
63 		mt7615_mcu_restart(&dev->mt76);
64 		if (!mt76_poll_msec(dev, MT_CONN_ON_MISC,
65 				    MT_TOP_MISC2_FW_PWR_ON, 0, 500))
66 			return -EIO;
67 
68 		ret = mt76u_vendor_request(&dev->mt76, MT_VEND_POWER_ON,
69 					   USB_DIR_OUT | USB_TYPE_VENDOR,
70 					   0x0, 0x1, NULL, 0);
71 		if (ret)
72 			return ret;
73 
74 		if (!mt76_poll_msec(dev, MT_CONN_ON_MISC,
75 				    MT_TOP_MISC2_FW_PWR_ON,
76 				    FW_STATE_PWR_ON << 1, 500)) {
77 			dev_err(dev->mt76.dev, "Timeout for power on\n");
78 			return -EIO;
79 		}
80 	}
81 
82 	ret = __mt7663_load_firmware(dev);
83 	if (ret)
84 		return ret;
85 
86 	mt76_clear(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN);
87 	set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state);
88 
89 	return 0;
90 }
91