Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved. |
| 3 | * |
| 4 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 5 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 6 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 7 | * more details. |
| 8 | * |
| 9 | * You should have received a copy of the GNU General Public License along with |
| 10 | * this program; if not, write to the Free Software Foundation, Inc., |
| 11 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA |
| 12 | * |
| 13 | * The full GNU General Public License is included in this distribution in the |
| 14 | * file called LICENSE. |
| 15 | * |
| 16 | * Contact Information: |
| 17 | * wlanfae <wlanfae@realtek.com> |
| 18 | ******************************************************************************/ |
| 19 | |
Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 20 | #include "rtl_core.h" |
| 21 | #include "r8192E_hw.h" |
Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 22 | #include "r8192E_hwimg.h" |
Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 23 | #include "r8192E_firmware.h" |
Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 24 | #include <linux/firmware.h> |
Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 25 | |
| 26 | extern void firmware_init_param(struct net_device *dev) |
| 27 | { |
Larry Finger | e3e3762 | 2011-07-19 00:26:41 -0500 | [diff] [blame^] | 28 | struct r8192_priv *priv = rtllib_priv(dev); |
Larry Finger | 5aca114 | 2011-07-18 23:10:50 -0500 | [diff] [blame] | 29 | struct rt_firmware *pfirmware = priv->pFirmware; |
Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 30 | |
| 31 | pfirmware->cmdpacket_frag_thresold = GET_COMMAND_PACKET_FRAG_THRESHOLD(MAX_TRANSMIT_BUFFER_SIZE); |
| 32 | } |
| 33 | |
| 34 | bool fw_download_code(struct net_device *dev, u8 *code_virtual_address, u32 buffer_len) |
| 35 | { |
Larry Finger | e3e3762 | 2011-07-19 00:26:41 -0500 | [diff] [blame^] | 36 | struct r8192_priv *priv = rtllib_priv(dev); |
Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 37 | bool rt_status = true; |
| 38 | u16 frag_threshold; |
| 39 | u16 frag_length, frag_offset = 0; |
| 40 | int i; |
| 41 | |
Larry Finger | 5aca114 | 2011-07-18 23:10:50 -0500 | [diff] [blame] | 42 | struct rt_firmware *pfirmware = priv->pFirmware; |
Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 43 | struct sk_buff *skb; |
| 44 | unsigned char *seg_ptr; |
Larry Finger | 3b83db4 | 2011-07-19 00:01:29 -0500 | [diff] [blame] | 45 | struct cb_desc *tcb_desc; |
Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 46 | u8 bLastIniPkt; |
| 47 | |
| 48 | firmware_init_param(dev); |
| 49 | frag_threshold = pfirmware->cmdpacket_frag_thresold; |
| 50 | do { |
| 51 | if ((buffer_len - frag_offset) > frag_threshold) { |
| 52 | frag_length = frag_threshold ; |
| 53 | bLastIniPkt = 0; |
| 54 | |
| 55 | } else { |
| 56 | frag_length = buffer_len - frag_offset; |
| 57 | bLastIniPkt = 1; |
| 58 | |
| 59 | } |
| 60 | |
| 61 | skb = dev_alloc_skb(frag_length + 4); |
| 62 | memcpy((unsigned char *)(skb->cb),&dev,sizeof(dev)); |
Larry Finger | 3b83db4 | 2011-07-19 00:01:29 -0500 | [diff] [blame] | 63 | tcb_desc = (struct cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE); |
Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 64 | tcb_desc->queue_index = TXCMD_QUEUE; |
| 65 | tcb_desc->bCmdOrInit = DESC_PACKET_TYPE_INIT; |
| 66 | tcb_desc->bLastIniPkt = bLastIniPkt; |
| 67 | |
| 68 | seg_ptr = skb->data; |
| 69 | for (i=0 ; i < frag_length; i+=4) { |
| 70 | *seg_ptr++ = ((i+0)<frag_length)?code_virtual_address[i+3]:0; |
| 71 | *seg_ptr++ = ((i+1)<frag_length)?code_virtual_address[i+2]:0; |
| 72 | *seg_ptr++ = ((i+2)<frag_length)?code_virtual_address[i+1]:0; |
| 73 | *seg_ptr++ = ((i+3)<frag_length)?code_virtual_address[i+0]:0; |
| 74 | } |
| 75 | tcb_desc->txbuf_size= (u16)i; |
| 76 | skb_put(skb, i); |
| 77 | |
| 78 | if (!priv->rtllib->check_nic_enough_desc(dev,tcb_desc->queue_index)|| |
| 79 | (!skb_queue_empty(&priv->rtllib->skb_waitQ[tcb_desc->queue_index]))||\ |
| 80 | (priv->rtllib->queue_stop) ) { |
| 81 | RT_TRACE(COMP_FIRMWARE, "===================> tx full!\n"); |
| 82 | skb_queue_tail(&priv->rtllib->skb_waitQ[tcb_desc->queue_index], skb); |
| 83 | } else { |
| 84 | priv->rtllib->softmac_hard_start_xmit(skb,dev); |
| 85 | } |
| 86 | |
| 87 | code_virtual_address += frag_length; |
| 88 | frag_offset += frag_length; |
| 89 | |
| 90 | }while(frag_offset < buffer_len); |
| 91 | |
| 92 | write_nic_byte(dev, TPPoll, TPPoll_CQ); |
| 93 | |
| 94 | return rt_status; |
| 95 | } |
| 96 | |
| 97 | bool |
| 98 | fwSendNullPacket( |
| 99 | struct net_device *dev, |
| 100 | u32 Length |
| 101 | ) |
| 102 | { |
| 103 | bool rtStatus = true; |
Larry Finger | e3e3762 | 2011-07-19 00:26:41 -0500 | [diff] [blame^] | 104 | struct r8192_priv *priv = rtllib_priv(dev); |
Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 105 | struct sk_buff *skb; |
Larry Finger | 3b83db4 | 2011-07-19 00:01:29 -0500 | [diff] [blame] | 106 | struct cb_desc *tcb_desc; |
Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 107 | unsigned char *ptr_buf; |
| 108 | bool bLastInitPacket = false; |
| 109 | |
| 110 | |
| 111 | skb = dev_alloc_skb(Length+ 4); |
| 112 | memcpy((unsigned char *)(skb->cb),&dev,sizeof(dev)); |
Larry Finger | 3b83db4 | 2011-07-19 00:01:29 -0500 | [diff] [blame] | 113 | tcb_desc = (struct cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE); |
Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 114 | tcb_desc->queue_index = TXCMD_QUEUE; |
| 115 | tcb_desc->bCmdOrInit = DESC_PACKET_TYPE_INIT; |
| 116 | tcb_desc->bLastIniPkt = bLastInitPacket; |
| 117 | ptr_buf = skb_put(skb, Length); |
| 118 | memset(ptr_buf,0,Length); |
| 119 | tcb_desc->txbuf_size= (u16)Length; |
| 120 | |
| 121 | if (!priv->rtllib->check_nic_enough_desc(dev,tcb_desc->queue_index)|| |
| 122 | (!skb_queue_empty(&priv->rtllib->skb_waitQ[tcb_desc->queue_index]))||\ |
| 123 | (priv->rtllib->queue_stop) ) { |
| 124 | RT_TRACE(COMP_FIRMWARE,"===================NULL packet================> tx full!\n"); |
| 125 | skb_queue_tail(&priv->rtllib->skb_waitQ[tcb_desc->queue_index], skb); |
| 126 | } else { |
| 127 | priv->rtllib->softmac_hard_start_xmit(skb,dev); |
| 128 | } |
| 129 | |
| 130 | write_nic_byte(dev, TPPoll, TPPoll_CQ); |
| 131 | return rtStatus; |
| 132 | } |
| 133 | |
| 134 | bool CPUcheck_maincodeok_turnonCPU(struct net_device *dev) |
| 135 | { |
| 136 | bool rt_status = true; |
| 137 | u32 CPU_status = 0; |
| 138 | unsigned long timeout; |
| 139 | |
| 140 | timeout = jiffies + MSECS(200); |
| 141 | while (time_before(jiffies, timeout)) { |
| 142 | CPU_status = read_nic_dword(dev, CPU_GEN); |
| 143 | if (CPU_status & CPU_GEN_PUT_CODE_OK) |
| 144 | break; |
| 145 | msleep(2); |
| 146 | } |
| 147 | |
| 148 | if (!(CPU_status&CPU_GEN_PUT_CODE_OK)) { |
| 149 | RT_TRACE(COMP_ERR, "Download Firmware: Put code fail!\n"); |
| 150 | goto CPUCheckMainCodeOKAndTurnOnCPU_Fail; |
| 151 | } else { |
| 152 | RT_TRACE(COMP_FIRMWARE, "Download Firmware: Put code ok!\n"); |
| 153 | } |
| 154 | |
| 155 | CPU_status = read_nic_dword(dev, CPU_GEN); |
| 156 | write_nic_byte(dev, CPU_GEN, (u8)((CPU_status|CPU_GEN_PWR_STB_CPU)&0xff)); |
| 157 | mdelay(1); |
| 158 | |
| 159 | timeout = jiffies + MSECS(200); |
| 160 | while (time_before(jiffies, timeout)) { |
| 161 | CPU_status = read_nic_dword(dev, CPU_GEN); |
| 162 | if (CPU_status&CPU_GEN_BOOT_RDY) |
| 163 | break; |
| 164 | msleep(2); |
| 165 | } |
| 166 | |
| 167 | if (!(CPU_status&CPU_GEN_BOOT_RDY)) { |
| 168 | goto CPUCheckMainCodeOKAndTurnOnCPU_Fail; |
| 169 | } else { |
| 170 | RT_TRACE(COMP_FIRMWARE, "Download Firmware: Boot ready!\n"); |
| 171 | } |
| 172 | |
| 173 | return rt_status; |
| 174 | |
| 175 | CPUCheckMainCodeOKAndTurnOnCPU_Fail: |
| 176 | RT_TRACE(COMP_ERR, "ERR in %s()\n", __func__); |
| 177 | rt_status = false; |
| 178 | return rt_status; |
| 179 | } |
| 180 | |
| 181 | bool CPUcheck_firmware_ready(struct net_device *dev) |
| 182 | { |
| 183 | |
| 184 | bool rt_status = true; |
| 185 | u32 CPU_status = 0; |
| 186 | unsigned long timeout; |
| 187 | |
| 188 | timeout = jiffies + MSECS(20); |
| 189 | while (time_before(jiffies, timeout)) { |
| 190 | CPU_status = read_nic_dword(dev, CPU_GEN); |
| 191 | if (CPU_status&CPU_GEN_FIRM_RDY) |
| 192 | break; |
| 193 | msleep(2); |
| 194 | } |
| 195 | |
| 196 | if (!(CPU_status&CPU_GEN_FIRM_RDY)) |
| 197 | goto CPUCheckFirmwareReady_Fail; |
| 198 | else |
| 199 | RT_TRACE(COMP_FIRMWARE, "Download Firmware: Firmware ready!\n"); |
| 200 | |
| 201 | return rt_status; |
| 202 | |
| 203 | CPUCheckFirmwareReady_Fail: |
| 204 | RT_TRACE(COMP_ERR, "ERR in %s()\n", __func__); |
| 205 | rt_status = false; |
| 206 | return rt_status; |
| 207 | |
| 208 | } |
| 209 | |
| 210 | inline static bool firmware_check_ready(struct net_device *dev, u8 load_fw_status) |
| 211 | { |
Larry Finger | e3e3762 | 2011-07-19 00:26:41 -0500 | [diff] [blame^] | 212 | struct r8192_priv *priv = rtllib_priv(dev); |
Larry Finger | 5aca114 | 2011-07-18 23:10:50 -0500 | [diff] [blame] | 213 | struct rt_firmware *pfirmware = priv->pFirmware; |
Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 214 | bool rt_status = true; |
| 215 | |
| 216 | switch (load_fw_status) { |
| 217 | case FW_INIT_STEP0_BOOT: |
| 218 | pfirmware->firmware_status = FW_STATUS_1_MOVE_BOOT_CODE; |
Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 219 | break; |
| 220 | |
| 221 | case FW_INIT_STEP1_MAIN: |
| 222 | pfirmware->firmware_status = FW_STATUS_2_MOVE_MAIN_CODE; |
| 223 | |
| 224 | rt_status = CPUcheck_maincodeok_turnonCPU(dev); |
| 225 | if (rt_status) { |
| 226 | pfirmware->firmware_status = FW_STATUS_3_TURNON_CPU; |
| 227 | } else { |
| 228 | RT_TRACE(COMP_FIRMWARE, "CPUcheck_maincodeok_turnonCPU fail!\n"); |
| 229 | } |
| 230 | |
| 231 | break; |
| 232 | |
| 233 | case FW_INIT_STEP2_DATA: |
| 234 | pfirmware->firmware_status = FW_STATUS_4_MOVE_DATA_CODE; |
| 235 | mdelay(1); |
| 236 | |
| 237 | rt_status = CPUcheck_firmware_ready(dev); |
| 238 | if (rt_status) { |
| 239 | pfirmware->firmware_status = FW_STATUS_5_READY; |
| 240 | } else { |
| 241 | RT_TRACE(COMP_FIRMWARE, "CPUcheck_firmware_ready fail(%d)!\n",rt_status); |
| 242 | } |
| 243 | |
| 244 | break; |
| 245 | default: |
| 246 | rt_status = false; |
| 247 | RT_TRACE(COMP_FIRMWARE, "Unknown firware status"); |
| 248 | break; |
| 249 | } |
| 250 | |
| 251 | return rt_status; |
| 252 | } |
| 253 | |
| 254 | bool init_firmware(struct net_device *dev) |
| 255 | { |
Larry Finger | e3e3762 | 2011-07-19 00:26:41 -0500 | [diff] [blame^] | 256 | struct r8192_priv *priv = rtllib_priv(dev); |
Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 257 | bool rt_status = true; |
| 258 | |
Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 259 | u8 *firmware_img_buf[3] = { &Rtl8192PciEFwBootArray[0], |
| 260 | &Rtl8192PciEFwMainArray[0], |
| 261 | &Rtl8192PciEFwDataArray[0]}; |
| 262 | |
| 263 | u32 firmware_img_len[3] = { sizeof(Rtl8192PciEFwBootArray), |
| 264 | sizeof(Rtl8192PciEFwMainArray), |
| 265 | sizeof(Rtl8192PciEFwDataArray)}; |
Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 266 | u32 file_length = 0; |
| 267 | u8 *mapped_file = NULL; |
| 268 | u8 init_step = 0; |
| 269 | opt_rst_type_e rst_opt = OPT_SYSTEM_RESET; |
| 270 | firmware_init_step_e starting_state = FW_INIT_STEP0_BOOT; |
| 271 | |
Larry Finger | 5aca114 | 2011-07-18 23:10:50 -0500 | [diff] [blame] | 272 | struct rt_firmware *pfirmware = priv->pFirmware; |
Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 273 | |
| 274 | RT_TRACE(COMP_FIRMWARE, " PlatformInitFirmware()==>\n"); |
| 275 | |
| 276 | if (pfirmware->firmware_status == FW_STATUS_0_INIT ) { |
| 277 | rst_opt = OPT_SYSTEM_RESET; |
| 278 | starting_state = FW_INIT_STEP0_BOOT; |
| 279 | |
| 280 | }else if (pfirmware->firmware_status == FW_STATUS_5_READY) { |
| 281 | rst_opt = OPT_FIRMWARE_RESET; |
| 282 | starting_state = FW_INIT_STEP2_DATA; |
| 283 | }else { |
| 284 | RT_TRACE(COMP_FIRMWARE, "PlatformInitFirmware: undefined firmware state\n"); |
| 285 | } |
| 286 | |
Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 287 | priv->firmware_source = FW_SOURCE_IMG_FILE; |
Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 288 | for (init_step = starting_state; init_step <= FW_INIT_STEP2_DATA; init_step++) { |
| 289 | if (rst_opt == OPT_SYSTEM_RESET) { |
| 290 | switch (priv->firmware_source) { |
| 291 | case FW_SOURCE_IMG_FILE: |
| 292 | { |
Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 293 | if (pfirmware->firmware_buf_size[init_step] == 0) { |
| 294 | const char *fw_name[3] = { "RTL8192E/boot.img", |
| 295 | "RTL8192E/main.img", |
| 296 | "RTL8192E/data.img" |
| 297 | }; |
| 298 | const struct firmware *fw_entry; |
| 299 | int rc; |
| 300 | rc = request_firmware(&fw_entry, fw_name[init_step],&priv->pdev->dev); |
| 301 | if (rc < 0 ) { |
| 302 | RT_TRACE(COMP_FIRMWARE, "request firmware fail!\n"); |
| 303 | goto download_firmware_fail; |
| 304 | } |
| 305 | if (fw_entry->size > sizeof(pfirmware->firmware_buf[init_step])) { |
| 306 | RT_TRACE(COMP_FIRMWARE, "img file size exceed the container buffer fail!\n"); |
| 307 | goto download_firmware_fail; |
| 308 | } |
| 309 | |
| 310 | if (init_step != FW_INIT_STEP1_MAIN) { |
| 311 | memcpy(pfirmware->firmware_buf[init_step],fw_entry->data,fw_entry->size); |
| 312 | pfirmware->firmware_buf_size[init_step] = fw_entry->size; |
| 313 | |
| 314 | } else { |
| 315 | memset(pfirmware->firmware_buf[init_step],0,128); |
| 316 | memcpy(&pfirmware->firmware_buf[init_step][128],fw_entry->data,fw_entry->size); |
| 317 | pfirmware->firmware_buf_size[init_step] = fw_entry->size+128; |
| 318 | } |
| 319 | |
| 320 | if (rst_opt == OPT_SYSTEM_RESET) { |
| 321 | release_firmware(fw_entry); |
| 322 | } |
| 323 | } |
| 324 | mapped_file = pfirmware->firmware_buf[init_step]; |
| 325 | file_length = pfirmware->firmware_buf_size[init_step]; |
Larry Finger | 94a7994 | 2011-08-23 19:00:42 -0500 | [diff] [blame] | 326 | break; |
| 327 | } |
| 328 | case FW_SOURCE_HEADER_FILE: |
| 329 | mapped_file = firmware_img_buf[init_step]; |
| 330 | file_length = firmware_img_len[init_step]; |
| 331 | if (init_step == FW_INIT_STEP2_DATA) { |
| 332 | memcpy(pfirmware->firmware_buf[init_step], mapped_file, file_length); |
| 333 | pfirmware->firmware_buf_size[init_step] = file_length; |
| 334 | } |
| 335 | break; |
| 336 | |
| 337 | default: |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | |
| 342 | } else if (rst_opt == OPT_FIRMWARE_RESET) { |
| 343 | mapped_file = pfirmware->firmware_buf[init_step]; |
| 344 | file_length = pfirmware->firmware_buf_size[init_step]; |
| 345 | } |
| 346 | |
| 347 | rt_status = fw_download_code(dev,mapped_file,file_length); |
| 348 | if (rt_status != true) { |
| 349 | goto download_firmware_fail; |
| 350 | } |
| 351 | |
| 352 | if (!firmware_check_ready(dev, init_step)) { |
| 353 | goto download_firmware_fail; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | RT_TRACE(COMP_FIRMWARE, "Firmware Download Success\n"); |
| 358 | return rt_status; |
| 359 | |
| 360 | download_firmware_fail: |
| 361 | RT_TRACE(COMP_ERR, "ERR in %s()\n", __func__); |
| 362 | rt_status = false; |
| 363 | return rt_status; |
| 364 | |
| 365 | } |