1 /* 2 * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com> 3 * 4 * Permission to use, copy, modify, and/or distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 #include <linux/kernel.h> 17 #include <linux/firmware.h> 18 19 #include "mt76x0.h" 20 #include "mcu.h" 21 #include "../mt76x02_usb.h" 22 23 #define MCU_FW_URB_MAX_PAYLOAD 0x38f8 24 #define MCU_FW_URB_SIZE (MCU_FW_URB_MAX_PAYLOAD + 12) 25 #define MT7610U_FIRMWARE "mediatek/mt7610u.bin" 26 27 static int 28 mt76x0u_upload_firmware(struct mt76x02_dev *dev, 29 const struct mt76x02_fw_header *hdr) 30 { 31 u8 *fw_payload = (u8 *)(hdr + 1); 32 u32 ilm_len, dlm_len; 33 void *ivb; 34 int err; 35 36 ivb = kmemdup(fw_payload, MT_MCU_IVB_SIZE, GFP_KERNEL); 37 if (!ivb) 38 return -ENOMEM; 39 40 ilm_len = le32_to_cpu(hdr->ilm_len) - MT_MCU_IVB_SIZE; 41 dev_dbg(dev->mt76.dev, "loading FW - ILM %u + IVB %u\n", 42 ilm_len, MT_MCU_IVB_SIZE); 43 err = mt76x02u_mcu_fw_send_data(dev, fw_payload + MT_MCU_IVB_SIZE, 44 ilm_len, MCU_FW_URB_MAX_PAYLOAD, 45 MT_MCU_IVB_SIZE); 46 if (err) 47 goto out; 48 49 dlm_len = le32_to_cpu(hdr->dlm_len); 50 dev_dbg(dev->mt76.dev, "loading FW - DLM %u\n", dlm_len); 51 err = mt76x02u_mcu_fw_send_data(dev, 52 fw_payload + le32_to_cpu(hdr->ilm_len), 53 dlm_len, MCU_FW_URB_MAX_PAYLOAD, 54 MT_MCU_DLM_OFFSET); 55 if (err) 56 goto out; 57 58 err = mt76u_vendor_request(&dev->mt76, MT_VEND_DEV_MODE, 59 USB_DIR_OUT | USB_TYPE_VENDOR, 60 0x12, 0, ivb, MT_MCU_IVB_SIZE); 61 if (err < 0) 62 goto out; 63 64 if (!mt76_poll_msec(dev, MT_MCU_COM_REG0, 1, 1, 1000)) { 65 dev_err(dev->mt76.dev, "Firmware failed to start\n"); 66 err = -ETIMEDOUT; 67 goto out; 68 } 69 70 dev_dbg(dev->mt76.dev, "Firmware running!\n"); 71 72 out: 73 kfree(ivb); 74 75 return err; 76 } 77 78 static int mt76x0u_load_firmware(struct mt76x02_dev *dev) 79 { 80 const struct firmware *fw; 81 const struct mt76x02_fw_header *hdr; 82 int len, ret; 83 u32 val; 84 85 mt76_wr(dev, MT_USB_DMA_CFG, (MT_USB_DMA_CFG_RX_BULK_EN | 86 MT_USB_DMA_CFG_TX_BULK_EN)); 87 88 if (mt76x0_firmware_running(dev)) 89 return 0; 90 91 ret = request_firmware(&fw, MT7610U_FIRMWARE, dev->mt76.dev); 92 if (ret) 93 return ret; 94 95 if (!fw || !fw->data || fw->size < sizeof(*hdr)) 96 goto err_inv_fw; 97 98 hdr = (const struct mt76x02_fw_header *)fw->data; 99 100 if (le32_to_cpu(hdr->ilm_len) <= MT_MCU_IVB_SIZE) 101 goto err_inv_fw; 102 103 len = sizeof(*hdr); 104 len += le32_to_cpu(hdr->ilm_len); 105 len += le32_to_cpu(hdr->dlm_len); 106 107 if (fw->size != len) 108 goto err_inv_fw; 109 110 val = le16_to_cpu(hdr->fw_ver); 111 dev_dbg(dev->mt76.dev, 112 "Firmware Version: %d.%d.%02d Build: %x Build time: %.16s\n", 113 (val >> 12) & 0xf, (val >> 8) & 0xf, val & 0xf, 114 le16_to_cpu(hdr->build_ver), hdr->build_time); 115 116 len = le32_to_cpu(hdr->ilm_len); 117 118 mt76_wr(dev, 0x1004, 0x2c); 119 120 mt76_set(dev, MT_USB_DMA_CFG, 121 (MT_USB_DMA_CFG_RX_BULK_EN | MT_USB_DMA_CFG_TX_BULK_EN) | 122 FIELD_PREP(MT_USB_DMA_CFG_RX_BULK_AGG_TOUT, 0x20)); 123 mt76x02u_mcu_fw_reset(dev); 124 usleep_range(5000, 6000); 125 /* 126 mt76x0_rmw(dev, MT_PBF_CFG, 0, (MT_PBF_CFG_TX0Q_EN | 127 MT_PBF_CFG_TX1Q_EN | 128 MT_PBF_CFG_TX2Q_EN | 129 MT_PBF_CFG_TX3Q_EN)); 130 */ 131 132 mt76_wr(dev, MT_FCE_PSE_CTRL, 1); 133 134 /* FCE tx_fs_base_ptr */ 135 mt76_wr(dev, MT_TX_CPU_FROM_FCE_BASE_PTR, 0x400230); 136 /* FCE tx_fs_max_cnt */ 137 mt76_wr(dev, MT_TX_CPU_FROM_FCE_MAX_COUNT, 1); 138 /* FCE pdma enable */ 139 mt76_wr(dev, MT_FCE_PDMA_GLOBAL_CONF, 0x44); 140 /* FCE skip_fs_en */ 141 mt76_wr(dev, MT_FCE_SKIP_FS, 3); 142 143 val = mt76_rr(dev, MT_USB_DMA_CFG); 144 val |= MT_USB_DMA_CFG_UDMA_TX_WL_DROP; 145 mt76_wr(dev, MT_USB_DMA_CFG, val); 146 val &= ~MT_USB_DMA_CFG_UDMA_TX_WL_DROP; 147 mt76_wr(dev, MT_USB_DMA_CFG, val); 148 149 ret = mt76x0u_upload_firmware(dev, hdr); 150 release_firmware(fw); 151 152 mt76_wr(dev, MT_FCE_PSE_CTRL, 1); 153 154 return ret; 155 156 err_inv_fw: 157 dev_err(dev->mt76.dev, "Invalid firmware image\n"); 158 release_firmware(fw); 159 return -ENOENT; 160 } 161 162 int mt76x0u_mcu_init(struct mt76x02_dev *dev) 163 { 164 int ret; 165 166 ret = mt76x0u_load_firmware(dev); 167 if (ret < 0) 168 return ret; 169 170 set_bit(MT76_STATE_MCU_RUNNING, &dev->mt76.state); 171 172 return 0; 173 } 174 175 MODULE_FIRMWARE(MT7610U_FIRMWARE); 176