10e3d6777SRyder Lee // SPDX-License-Identifier: ISC
2f16f700dSLorenzo Bianconi /*
3f16f700dSLorenzo Bianconi * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
4f16f700dSLorenzo Bianconi */
5f16f700dSLorenzo Bianconi #include <linux/kernel.h>
6f16f700dSLorenzo Bianconi #include <linux/firmware.h>
785b7e2acSHauke Mehrtens #include <linux/module.h>
8f16f700dSLorenzo Bianconi
9f16f700dSLorenzo Bianconi #include "mt76x0.h"
10f16f700dSLorenzo Bianconi #include "mcu.h"
11f16f700dSLorenzo Bianconi #include "../mt76x02_usb.h"
12f16f700dSLorenzo Bianconi
13f16f700dSLorenzo Bianconi #define MCU_FW_URB_MAX_PAYLOAD 0x38f8
14f16f700dSLorenzo Bianconi #define MCU_FW_URB_SIZE (MCU_FW_URB_MAX_PAYLOAD + 12)
15f16f700dSLorenzo Bianconi
16f16f700dSLorenzo Bianconi static int
mt76x0u_upload_firmware(struct mt76x02_dev * dev,const struct mt76x02_fw_header * hdr)17b2d871c0SLorenzo Bianconi mt76x0u_upload_firmware(struct mt76x02_dev *dev,
18f16f700dSLorenzo Bianconi const struct mt76x02_fw_header *hdr)
19f16f700dSLorenzo Bianconi {
20f16f700dSLorenzo Bianconi u8 *fw_payload = (u8 *)(hdr + 1);
21f16f700dSLorenzo Bianconi u32 ilm_len, dlm_len;
22f16f700dSLorenzo Bianconi void *ivb;
23f16f700dSLorenzo Bianconi int err;
24f16f700dSLorenzo Bianconi
25f16f700dSLorenzo Bianconi ivb = kmemdup(fw_payload, MT_MCU_IVB_SIZE, GFP_KERNEL);
26f16f700dSLorenzo Bianconi if (!ivb)
27f16f700dSLorenzo Bianconi return -ENOMEM;
28f16f700dSLorenzo Bianconi
29f16f700dSLorenzo Bianconi ilm_len = le32_to_cpu(hdr->ilm_len) - MT_MCU_IVB_SIZE;
30f16f700dSLorenzo Bianconi dev_dbg(dev->mt76.dev, "loading FW - ILM %u + IVB %u\n",
31f16f700dSLorenzo Bianconi ilm_len, MT_MCU_IVB_SIZE);
3295c3e451SLorenzo Bianconi err = mt76x02u_mcu_fw_send_data(dev, fw_payload + MT_MCU_IVB_SIZE,
33f16f700dSLorenzo Bianconi ilm_len, MCU_FW_URB_MAX_PAYLOAD,
34f16f700dSLorenzo Bianconi MT_MCU_IVB_SIZE);
35f16f700dSLorenzo Bianconi if (err)
36f16f700dSLorenzo Bianconi goto out;
37f16f700dSLorenzo Bianconi
38f16f700dSLorenzo Bianconi dlm_len = le32_to_cpu(hdr->dlm_len);
39f16f700dSLorenzo Bianconi dev_dbg(dev->mt76.dev, "loading FW - DLM %u\n", dlm_len);
4095c3e451SLorenzo Bianconi err = mt76x02u_mcu_fw_send_data(dev,
41f16f700dSLorenzo Bianconi fw_payload + le32_to_cpu(hdr->ilm_len),
42f16f700dSLorenzo Bianconi dlm_len, MCU_FW_URB_MAX_PAYLOAD,
43f16f700dSLorenzo Bianconi MT_MCU_DLM_OFFSET);
44f16f700dSLorenzo Bianconi if (err)
45f16f700dSLorenzo Bianconi goto out;
46f16f700dSLorenzo Bianconi
47f16f700dSLorenzo Bianconi err = mt76u_vendor_request(&dev->mt76, MT_VEND_DEV_MODE,
48f16f700dSLorenzo Bianconi USB_DIR_OUT | USB_TYPE_VENDOR,
49f16f700dSLorenzo Bianconi 0x12, 0, ivb, MT_MCU_IVB_SIZE);
50f16f700dSLorenzo Bianconi if (err < 0)
51f16f700dSLorenzo Bianconi goto out;
52f16f700dSLorenzo Bianconi
53f16f700dSLorenzo Bianconi if (!mt76_poll_msec(dev, MT_MCU_COM_REG0, 1, 1, 1000)) {
54f16f700dSLorenzo Bianconi dev_err(dev->mt76.dev, "Firmware failed to start\n");
55f16f700dSLorenzo Bianconi err = -ETIMEDOUT;
56f16f700dSLorenzo Bianconi goto out;
57f16f700dSLorenzo Bianconi }
58f16f700dSLorenzo Bianconi
59f16f700dSLorenzo Bianconi dev_dbg(dev->mt76.dev, "Firmware running!\n");
60f16f700dSLorenzo Bianconi
61f16f700dSLorenzo Bianconi out:
62f16f700dSLorenzo Bianconi kfree(ivb);
63f16f700dSLorenzo Bianconi
64f16f700dSLorenzo Bianconi return err;
65f16f700dSLorenzo Bianconi }
66f16f700dSLorenzo Bianconi
mt76x0_get_firmware(struct mt76x02_dev * dev,const struct firmware ** fw)67f4730140SLorenzo Bianconi static int mt76x0_get_firmware(struct mt76x02_dev *dev,
68f4730140SLorenzo Bianconi const struct firmware **fw)
69f4730140SLorenzo Bianconi {
70f4730140SLorenzo Bianconi int err;
71f4730140SLorenzo Bianconi
72f4730140SLorenzo Bianconi /* try to load mt7610e fw if available
73f4730140SLorenzo Bianconi * otherwise fall back to mt7610u one
74f4730140SLorenzo Bianconi */
75f4730140SLorenzo Bianconi err = firmware_request_nowarn(fw, MT7610E_FIRMWARE, dev->mt76.dev);
76f4730140SLorenzo Bianconi if (err) {
77f4730140SLorenzo Bianconi dev_info(dev->mt76.dev, "%s not found, switching to %s",
78f4730140SLorenzo Bianconi MT7610E_FIRMWARE, MT7610U_FIRMWARE);
79f4730140SLorenzo Bianconi return request_firmware(fw, MT7610U_FIRMWARE,
80f4730140SLorenzo Bianconi dev->mt76.dev);
81f4730140SLorenzo Bianconi }
82f4730140SLorenzo Bianconi return 0;
83f4730140SLorenzo Bianconi }
84f4730140SLorenzo Bianconi
mt76x0u_load_firmware(struct mt76x02_dev * dev)85b2d871c0SLorenzo Bianconi static int mt76x0u_load_firmware(struct mt76x02_dev *dev)
86f16f700dSLorenzo Bianconi {
87f16f700dSLorenzo Bianconi const struct firmware *fw;
88f16f700dSLorenzo Bianconi const struct mt76x02_fw_header *hdr;
89f16f700dSLorenzo Bianconi int len, ret;
90f16f700dSLorenzo Bianconi u32 val;
91f16f700dSLorenzo Bianconi
92f16f700dSLorenzo Bianconi mt76_wr(dev, MT_USB_DMA_CFG, (MT_USB_DMA_CFG_RX_BULK_EN |
93f16f700dSLorenzo Bianconi MT_USB_DMA_CFG_TX_BULK_EN));
94f16f700dSLorenzo Bianconi
95f16f700dSLorenzo Bianconi if (mt76x0_firmware_running(dev))
96f16f700dSLorenzo Bianconi return 0;
97f16f700dSLorenzo Bianconi
98f4730140SLorenzo Bianconi ret = mt76x0_get_firmware(dev, &fw);
99f16f700dSLorenzo Bianconi if (ret)
100f16f700dSLorenzo Bianconi return ret;
101f16f700dSLorenzo Bianconi
102f16f700dSLorenzo Bianconi if (!fw || !fw->data || fw->size < sizeof(*hdr))
103f16f700dSLorenzo Bianconi goto err_inv_fw;
104f16f700dSLorenzo Bianconi
105f16f700dSLorenzo Bianconi hdr = (const struct mt76x02_fw_header *)fw->data;
106f16f700dSLorenzo Bianconi
107f16f700dSLorenzo Bianconi if (le32_to_cpu(hdr->ilm_len) <= MT_MCU_IVB_SIZE)
108f16f700dSLorenzo Bianconi goto err_inv_fw;
109f16f700dSLorenzo Bianconi
110f16f700dSLorenzo Bianconi len = sizeof(*hdr);
111f16f700dSLorenzo Bianconi len += le32_to_cpu(hdr->ilm_len);
112f16f700dSLorenzo Bianconi len += le32_to_cpu(hdr->dlm_len);
113f16f700dSLorenzo Bianconi
114f16f700dSLorenzo Bianconi if (fw->size != len)
115f16f700dSLorenzo Bianconi goto err_inv_fw;
116f16f700dSLorenzo Bianconi
117f16f700dSLorenzo Bianconi val = le16_to_cpu(hdr->fw_ver);
118f16f700dSLorenzo Bianconi dev_dbg(dev->mt76.dev,
119f16f700dSLorenzo Bianconi "Firmware Version: %d.%d.%02d Build: %x Build time: %.16s\n",
120f16f700dSLorenzo Bianconi (val >> 12) & 0xf, (val >> 8) & 0xf, val & 0xf,
121f16f700dSLorenzo Bianconi le16_to_cpu(hdr->build_ver), hdr->build_time);
122f16f700dSLorenzo Bianconi
123f16f700dSLorenzo Bianconi len = le32_to_cpu(hdr->ilm_len);
124f16f700dSLorenzo Bianconi
125f16f700dSLorenzo Bianconi mt76_wr(dev, 0x1004, 0x2c);
126f16f700dSLorenzo Bianconi
127f16f700dSLorenzo Bianconi mt76_set(dev, MT_USB_DMA_CFG,
128f16f700dSLorenzo Bianconi (MT_USB_DMA_CFG_RX_BULK_EN | MT_USB_DMA_CFG_TX_BULK_EN) |
129f16f700dSLorenzo Bianconi FIELD_PREP(MT_USB_DMA_CFG_RX_BULK_AGG_TOUT, 0x20));
13095c3e451SLorenzo Bianconi mt76x02u_mcu_fw_reset(dev);
131f16f700dSLorenzo Bianconi usleep_range(5000, 6000);
132f16f700dSLorenzo Bianconi
133f16f700dSLorenzo Bianconi mt76_wr(dev, MT_FCE_PSE_CTRL, 1);
134f16f700dSLorenzo Bianconi
135f16f700dSLorenzo Bianconi /* FCE tx_fs_base_ptr */
136f16f700dSLorenzo Bianconi mt76_wr(dev, MT_TX_CPU_FROM_FCE_BASE_PTR, 0x400230);
137f16f700dSLorenzo Bianconi /* FCE tx_fs_max_cnt */
138f16f700dSLorenzo Bianconi mt76_wr(dev, MT_TX_CPU_FROM_FCE_MAX_COUNT, 1);
139f16f700dSLorenzo Bianconi /* FCE pdma enable */
140f16f700dSLorenzo Bianconi mt76_wr(dev, MT_FCE_PDMA_GLOBAL_CONF, 0x44);
141f16f700dSLorenzo Bianconi /* FCE skip_fs_en */
142f16f700dSLorenzo Bianconi mt76_wr(dev, MT_FCE_SKIP_FS, 3);
143f16f700dSLorenzo Bianconi
144f16f700dSLorenzo Bianconi val = mt76_rr(dev, MT_USB_DMA_CFG);
145f16f700dSLorenzo Bianconi val |= MT_USB_DMA_CFG_UDMA_TX_WL_DROP;
146f16f700dSLorenzo Bianconi mt76_wr(dev, MT_USB_DMA_CFG, val);
147f16f700dSLorenzo Bianconi val &= ~MT_USB_DMA_CFG_UDMA_TX_WL_DROP;
148f16f700dSLorenzo Bianconi mt76_wr(dev, MT_USB_DMA_CFG, val);
149f16f700dSLorenzo Bianconi
150f16f700dSLorenzo Bianconi ret = mt76x0u_upload_firmware(dev, hdr);
151*c625b2c8SLorenzo Bianconi mt76x02_set_ethtool_fwver(dev, hdr);
152f16f700dSLorenzo Bianconi release_firmware(fw);
153f16f700dSLorenzo Bianconi
154f16f700dSLorenzo Bianconi mt76_wr(dev, MT_FCE_PSE_CTRL, 1);
155f16f700dSLorenzo Bianconi
156f16f700dSLorenzo Bianconi return ret;
157f16f700dSLorenzo Bianconi
158f16f700dSLorenzo Bianconi err_inv_fw:
159f16f700dSLorenzo Bianconi dev_err(dev->mt76.dev, "Invalid firmware image\n");
160f16f700dSLorenzo Bianconi release_firmware(fw);
161f16f700dSLorenzo Bianconi return -ENOENT;
162f16f700dSLorenzo Bianconi }
163f16f700dSLorenzo Bianconi
mt76x0u_mcu_init(struct mt76x02_dev * dev)164b2d871c0SLorenzo Bianconi int mt76x0u_mcu_init(struct mt76x02_dev *dev)
165f16f700dSLorenzo Bianconi {
166f16f700dSLorenzo Bianconi int ret;
167f16f700dSLorenzo Bianconi
168f16f700dSLorenzo Bianconi ret = mt76x0u_load_firmware(dev);
169f16f700dSLorenzo Bianconi if (ret < 0)
170f16f700dSLorenzo Bianconi return ret;
171f16f700dSLorenzo Bianconi
172011849e0SFelix Fietkau set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state);
173f16f700dSLorenzo Bianconi
174f16f700dSLorenzo Bianconi return 0;
175f16f700dSLorenzo Bianconi }
176