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