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 #include <linux/module.h> 19 20 #include "mt76x0.h" 21 #include "mcu.h" 22 #include "../mt76x02_usb.h" 23 24 #define MCU_FW_URB_MAX_PAYLOAD 0x38f8 25 #define MCU_FW_URB_SIZE (MCU_FW_URB_MAX_PAYLOAD + 12) 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 mt76x0_get_firmware(struct mt76x02_dev *dev, 79 const struct firmware **fw) 80 { 81 int err; 82 83 /* try to load mt7610e fw if available 84 * otherwise fall back to mt7610u one 85 */ 86 err = firmware_request_nowarn(fw, MT7610E_FIRMWARE, dev->mt76.dev); 87 if (err) { 88 dev_info(dev->mt76.dev, "%s not found, switching to %s", 89 MT7610E_FIRMWARE, MT7610U_FIRMWARE); 90 return request_firmware(fw, MT7610U_FIRMWARE, 91 dev->mt76.dev); 92 } 93 return 0; 94 } 95 96 static int mt76x0u_load_firmware(struct mt76x02_dev *dev) 97 { 98 const struct firmware *fw; 99 const struct mt76x02_fw_header *hdr; 100 int len, ret; 101 u32 val; 102 103 mt76_wr(dev, MT_USB_DMA_CFG, (MT_USB_DMA_CFG_RX_BULK_EN | 104 MT_USB_DMA_CFG_TX_BULK_EN)); 105 106 if (mt76x0_firmware_running(dev)) 107 return 0; 108 109 ret = mt76x0_get_firmware(dev, &fw); 110 if (ret) 111 return ret; 112 113 if (!fw || !fw->data || fw->size < sizeof(*hdr)) 114 goto err_inv_fw; 115 116 hdr = (const struct mt76x02_fw_header *)fw->data; 117 118 if (le32_to_cpu(hdr->ilm_len) <= MT_MCU_IVB_SIZE) 119 goto err_inv_fw; 120 121 len = sizeof(*hdr); 122 len += le32_to_cpu(hdr->ilm_len); 123 len += le32_to_cpu(hdr->dlm_len); 124 125 if (fw->size != len) 126 goto err_inv_fw; 127 128 val = le16_to_cpu(hdr->fw_ver); 129 dev_dbg(dev->mt76.dev, 130 "Firmware Version: %d.%d.%02d Build: %x Build time: %.16s\n", 131 (val >> 12) & 0xf, (val >> 8) & 0xf, val & 0xf, 132 le16_to_cpu(hdr->build_ver), hdr->build_time); 133 134 len = le32_to_cpu(hdr->ilm_len); 135 136 mt76_wr(dev, 0x1004, 0x2c); 137 138 mt76_set(dev, MT_USB_DMA_CFG, 139 (MT_USB_DMA_CFG_RX_BULK_EN | MT_USB_DMA_CFG_TX_BULK_EN) | 140 FIELD_PREP(MT_USB_DMA_CFG_RX_BULK_AGG_TOUT, 0x20)); 141 mt76x02u_mcu_fw_reset(dev); 142 usleep_range(5000, 6000); 143 144 mt76_wr(dev, MT_FCE_PSE_CTRL, 1); 145 146 /* FCE tx_fs_base_ptr */ 147 mt76_wr(dev, MT_TX_CPU_FROM_FCE_BASE_PTR, 0x400230); 148 /* FCE tx_fs_max_cnt */ 149 mt76_wr(dev, MT_TX_CPU_FROM_FCE_MAX_COUNT, 1); 150 /* FCE pdma enable */ 151 mt76_wr(dev, MT_FCE_PDMA_GLOBAL_CONF, 0x44); 152 /* FCE skip_fs_en */ 153 mt76_wr(dev, MT_FCE_SKIP_FS, 3); 154 155 val = mt76_rr(dev, MT_USB_DMA_CFG); 156 val |= MT_USB_DMA_CFG_UDMA_TX_WL_DROP; 157 mt76_wr(dev, MT_USB_DMA_CFG, val); 158 val &= ~MT_USB_DMA_CFG_UDMA_TX_WL_DROP; 159 mt76_wr(dev, MT_USB_DMA_CFG, val); 160 161 ret = mt76x0u_upload_firmware(dev, hdr); 162 release_firmware(fw); 163 164 mt76_wr(dev, MT_FCE_PSE_CTRL, 1); 165 166 return ret; 167 168 err_inv_fw: 169 dev_err(dev->mt76.dev, "Invalid firmware image\n"); 170 release_firmware(fw); 171 return -ENOENT; 172 } 173 174 int mt76x0u_mcu_init(struct mt76x02_dev *dev) 175 { 176 int ret; 177 178 ret = mt76x0u_load_firmware(dev); 179 if (ret < 0) 180 return ret; 181 182 set_bit(MT76_STATE_MCU_RUNNING, &dev->mt76.state); 183 184 return 0; 185 } 186