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 | |
| 20 | #if (defined(RTL8192E) || defined(RTL8190P)) |
| 21 | |
| 22 | #include "rtl_core.h" |
| 23 | #include "r8192E_hw.h" |
| 24 | #ifdef RTL8190P |
| 25 | #include "r8190P_hwimg.h" |
| 26 | #elif defined RTL8192E |
| 27 | #include "r8192E_hwimg.h" |
| 28 | #endif |
| 29 | #include "r8192E_firmware.h" |
| 30 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0) |
| 31 | #include <linux/firmware.h> |
| 32 | #endif |
| 33 | |
| 34 | extern void firmware_init_param(struct net_device *dev) |
| 35 | { |
| 36 | struct r8192_priv *priv = rtllib_priv(dev); |
| 37 | rt_firmware *pfirmware = priv->pFirmware; |
| 38 | |
| 39 | pfirmware->cmdpacket_frag_thresold = GET_COMMAND_PACKET_FRAG_THRESHOLD(MAX_TRANSMIT_BUFFER_SIZE); |
| 40 | } |
| 41 | |
| 42 | bool fw_download_code(struct net_device *dev, u8 *code_virtual_address, u32 buffer_len) |
| 43 | { |
| 44 | struct r8192_priv *priv = rtllib_priv(dev); |
| 45 | bool rt_status = true; |
| 46 | u16 frag_threshold; |
| 47 | u16 frag_length, frag_offset = 0; |
| 48 | int i; |
| 49 | |
| 50 | rt_firmware *pfirmware = priv->pFirmware; |
| 51 | struct sk_buff *skb; |
| 52 | unsigned char *seg_ptr; |
| 53 | cb_desc *tcb_desc; |
| 54 | u8 bLastIniPkt; |
| 55 | |
| 56 | firmware_init_param(dev); |
| 57 | frag_threshold = pfirmware->cmdpacket_frag_thresold; |
| 58 | do { |
| 59 | if ((buffer_len - frag_offset) > frag_threshold) { |
| 60 | frag_length = frag_threshold ; |
| 61 | bLastIniPkt = 0; |
| 62 | |
| 63 | } else { |
| 64 | frag_length = buffer_len - frag_offset; |
| 65 | bLastIniPkt = 1; |
| 66 | |
| 67 | } |
| 68 | |
| 69 | skb = dev_alloc_skb(frag_length + 4); |
| 70 | memcpy((unsigned char *)(skb->cb),&dev,sizeof(dev)); |
| 71 | tcb_desc = (cb_desc*)(skb->cb + MAX_DEV_ADDR_SIZE); |
| 72 | tcb_desc->queue_index = TXCMD_QUEUE; |
| 73 | tcb_desc->bCmdOrInit = DESC_PACKET_TYPE_INIT; |
| 74 | tcb_desc->bLastIniPkt = bLastIniPkt; |
| 75 | |
| 76 | seg_ptr = skb->data; |
| 77 | for (i=0 ; i < frag_length; i+=4) { |
| 78 | *seg_ptr++ = ((i+0)<frag_length)?code_virtual_address[i+3]:0; |
| 79 | *seg_ptr++ = ((i+1)<frag_length)?code_virtual_address[i+2]:0; |
| 80 | *seg_ptr++ = ((i+2)<frag_length)?code_virtual_address[i+1]:0; |
| 81 | *seg_ptr++ = ((i+3)<frag_length)?code_virtual_address[i+0]:0; |
| 82 | } |
| 83 | tcb_desc->txbuf_size= (u16)i; |
| 84 | skb_put(skb, i); |
| 85 | |
| 86 | if (!priv->rtllib->check_nic_enough_desc(dev,tcb_desc->queue_index)|| |
| 87 | (!skb_queue_empty(&priv->rtllib->skb_waitQ[tcb_desc->queue_index]))||\ |
| 88 | (priv->rtllib->queue_stop) ) { |
| 89 | RT_TRACE(COMP_FIRMWARE, "===================> tx full!\n"); |
| 90 | skb_queue_tail(&priv->rtllib->skb_waitQ[tcb_desc->queue_index], skb); |
| 91 | } else { |
| 92 | priv->rtllib->softmac_hard_start_xmit(skb,dev); |
| 93 | } |
| 94 | |
| 95 | code_virtual_address += frag_length; |
| 96 | frag_offset += frag_length; |
| 97 | |
| 98 | }while(frag_offset < buffer_len); |
| 99 | |
| 100 | write_nic_byte(dev, TPPoll, TPPoll_CQ); |
| 101 | |
| 102 | return rt_status; |
| 103 | } |
| 104 | |
| 105 | bool |
| 106 | fwSendNullPacket( |
| 107 | struct net_device *dev, |
| 108 | u32 Length |
| 109 | ) |
| 110 | { |
| 111 | bool rtStatus = true; |
| 112 | struct r8192_priv *priv = rtllib_priv(dev); |
| 113 | struct sk_buff *skb; |
| 114 | cb_desc *tcb_desc; |
| 115 | unsigned char *ptr_buf; |
| 116 | bool bLastInitPacket = false; |
| 117 | |
| 118 | |
| 119 | skb = dev_alloc_skb(Length+ 4); |
| 120 | memcpy((unsigned char *)(skb->cb),&dev,sizeof(dev)); |
| 121 | tcb_desc = (cb_desc*)(skb->cb + MAX_DEV_ADDR_SIZE); |
| 122 | tcb_desc->queue_index = TXCMD_QUEUE; |
| 123 | tcb_desc->bCmdOrInit = DESC_PACKET_TYPE_INIT; |
| 124 | tcb_desc->bLastIniPkt = bLastInitPacket; |
| 125 | ptr_buf = skb_put(skb, Length); |
| 126 | memset(ptr_buf,0,Length); |
| 127 | tcb_desc->txbuf_size= (u16)Length; |
| 128 | |
| 129 | if (!priv->rtllib->check_nic_enough_desc(dev,tcb_desc->queue_index)|| |
| 130 | (!skb_queue_empty(&priv->rtllib->skb_waitQ[tcb_desc->queue_index]))||\ |
| 131 | (priv->rtllib->queue_stop) ) { |
| 132 | RT_TRACE(COMP_FIRMWARE,"===================NULL packet================> tx full!\n"); |
| 133 | skb_queue_tail(&priv->rtllib->skb_waitQ[tcb_desc->queue_index], skb); |
| 134 | } else { |
| 135 | priv->rtllib->softmac_hard_start_xmit(skb,dev); |
| 136 | } |
| 137 | |
| 138 | write_nic_byte(dev, TPPoll, TPPoll_CQ); |
| 139 | return rtStatus; |
| 140 | } |
| 141 | |
| 142 | bool CPUcheck_maincodeok_turnonCPU(struct net_device *dev) |
| 143 | { |
| 144 | bool rt_status = true; |
| 145 | u32 CPU_status = 0; |
| 146 | unsigned long timeout; |
| 147 | |
| 148 | timeout = jiffies + MSECS(200); |
| 149 | while (time_before(jiffies, timeout)) { |
| 150 | CPU_status = read_nic_dword(dev, CPU_GEN); |
| 151 | if (CPU_status & CPU_GEN_PUT_CODE_OK) |
| 152 | break; |
| 153 | msleep(2); |
| 154 | } |
| 155 | |
| 156 | if (!(CPU_status&CPU_GEN_PUT_CODE_OK)) { |
| 157 | RT_TRACE(COMP_ERR, "Download Firmware: Put code fail!\n"); |
| 158 | goto CPUCheckMainCodeOKAndTurnOnCPU_Fail; |
| 159 | } else { |
| 160 | RT_TRACE(COMP_FIRMWARE, "Download Firmware: Put code ok!\n"); |
| 161 | } |
| 162 | |
| 163 | CPU_status = read_nic_dword(dev, CPU_GEN); |
| 164 | write_nic_byte(dev, CPU_GEN, (u8)((CPU_status|CPU_GEN_PWR_STB_CPU)&0xff)); |
| 165 | mdelay(1); |
| 166 | |
| 167 | timeout = jiffies + MSECS(200); |
| 168 | while (time_before(jiffies, timeout)) { |
| 169 | CPU_status = read_nic_dword(dev, CPU_GEN); |
| 170 | if (CPU_status&CPU_GEN_BOOT_RDY) |
| 171 | break; |
| 172 | msleep(2); |
| 173 | } |
| 174 | |
| 175 | if (!(CPU_status&CPU_GEN_BOOT_RDY)) { |
| 176 | goto CPUCheckMainCodeOKAndTurnOnCPU_Fail; |
| 177 | } else { |
| 178 | RT_TRACE(COMP_FIRMWARE, "Download Firmware: Boot ready!\n"); |
| 179 | } |
| 180 | |
| 181 | return rt_status; |
| 182 | |
| 183 | CPUCheckMainCodeOKAndTurnOnCPU_Fail: |
| 184 | RT_TRACE(COMP_ERR, "ERR in %s()\n", __func__); |
| 185 | rt_status = false; |
| 186 | return rt_status; |
| 187 | } |
| 188 | |
| 189 | bool CPUcheck_firmware_ready(struct net_device *dev) |
| 190 | { |
| 191 | |
| 192 | bool rt_status = true; |
| 193 | u32 CPU_status = 0; |
| 194 | unsigned long timeout; |
| 195 | |
| 196 | timeout = jiffies + MSECS(20); |
| 197 | while (time_before(jiffies, timeout)) { |
| 198 | CPU_status = read_nic_dword(dev, CPU_GEN); |
| 199 | if (CPU_status&CPU_GEN_FIRM_RDY) |
| 200 | break; |
| 201 | msleep(2); |
| 202 | } |
| 203 | |
| 204 | if (!(CPU_status&CPU_GEN_FIRM_RDY)) |
| 205 | goto CPUCheckFirmwareReady_Fail; |
| 206 | else |
| 207 | RT_TRACE(COMP_FIRMWARE, "Download Firmware: Firmware ready!\n"); |
| 208 | |
| 209 | return rt_status; |
| 210 | |
| 211 | CPUCheckFirmwareReady_Fail: |
| 212 | RT_TRACE(COMP_ERR, "ERR in %s()\n", __func__); |
| 213 | rt_status = false; |
| 214 | return rt_status; |
| 215 | |
| 216 | } |
| 217 | |
| 218 | inline static bool firmware_check_ready(struct net_device *dev, u8 load_fw_status) |
| 219 | { |
| 220 | struct r8192_priv *priv = rtllib_priv(dev); |
| 221 | rt_firmware *pfirmware = priv->pFirmware; |
| 222 | bool rt_status = true; |
| 223 | |
| 224 | switch (load_fw_status) { |
| 225 | case FW_INIT_STEP0_BOOT: |
| 226 | pfirmware->firmware_status = FW_STATUS_1_MOVE_BOOT_CODE; |
| 227 | #ifdef RTL8190P |
| 228 | rt_status = fwSendNullPacket(dev, RTL8190_CPU_START_OFFSET); |
| 229 | if (!rt_status) { |
| 230 | RT_TRACE(COMP_INIT, "fwSendNullPacket() fail ! \n"); |
| 231 | } |
| 232 | #endif |
| 233 | break; |
| 234 | |
| 235 | case FW_INIT_STEP1_MAIN: |
| 236 | pfirmware->firmware_status = FW_STATUS_2_MOVE_MAIN_CODE; |
| 237 | |
| 238 | rt_status = CPUcheck_maincodeok_turnonCPU(dev); |
| 239 | if (rt_status) { |
| 240 | pfirmware->firmware_status = FW_STATUS_3_TURNON_CPU; |
| 241 | } else { |
| 242 | RT_TRACE(COMP_FIRMWARE, "CPUcheck_maincodeok_turnonCPU fail!\n"); |
| 243 | } |
| 244 | |
| 245 | break; |
| 246 | |
| 247 | case FW_INIT_STEP2_DATA: |
| 248 | pfirmware->firmware_status = FW_STATUS_4_MOVE_DATA_CODE; |
| 249 | mdelay(1); |
| 250 | |
| 251 | rt_status = CPUcheck_firmware_ready(dev); |
| 252 | if (rt_status) { |
| 253 | pfirmware->firmware_status = FW_STATUS_5_READY; |
| 254 | } else { |
| 255 | RT_TRACE(COMP_FIRMWARE, "CPUcheck_firmware_ready fail(%d)!\n",rt_status); |
| 256 | } |
| 257 | |
| 258 | break; |
| 259 | default: |
| 260 | rt_status = false; |
| 261 | RT_TRACE(COMP_FIRMWARE, "Unknown firware status"); |
| 262 | break; |
| 263 | } |
| 264 | |
| 265 | return rt_status; |
| 266 | } |
| 267 | |
| 268 | bool init_firmware(struct net_device *dev) |
| 269 | { |
| 270 | struct r8192_priv *priv = rtllib_priv(dev); |
| 271 | bool rt_status = true; |
| 272 | |
| 273 | #ifdef RTL8190P |
| 274 | u8 *firmware_img_buf[3] = { &Rtl8190PciFwBootArray[0], |
| 275 | &Rtl8190PciFwMainArray[0], |
| 276 | &Rtl8190PciFwDataArray[0]}; |
| 277 | |
| 278 | u32 firmware_img_len[3] = { sizeof(Rtl8190PciFwBootArray), |
| 279 | sizeof(Rtl8190PciFwMainArray), |
| 280 | sizeof(Rtl8190PciFwDataArray)}; |
| 281 | #else |
| 282 | u8 *firmware_img_buf[3] = { &Rtl8192PciEFwBootArray[0], |
| 283 | &Rtl8192PciEFwMainArray[0], |
| 284 | &Rtl8192PciEFwDataArray[0]}; |
| 285 | |
| 286 | u32 firmware_img_len[3] = { sizeof(Rtl8192PciEFwBootArray), |
| 287 | sizeof(Rtl8192PciEFwMainArray), |
| 288 | sizeof(Rtl8192PciEFwDataArray)}; |
| 289 | #endif |
| 290 | u32 file_length = 0; |
| 291 | u8 *mapped_file = NULL; |
| 292 | u8 init_step = 0; |
| 293 | opt_rst_type_e rst_opt = OPT_SYSTEM_RESET; |
| 294 | firmware_init_step_e starting_state = FW_INIT_STEP0_BOOT; |
| 295 | |
| 296 | rt_firmware *pfirmware = priv->pFirmware; |
| 297 | |
| 298 | RT_TRACE(COMP_FIRMWARE, " PlatformInitFirmware()==>\n"); |
| 299 | |
| 300 | if (pfirmware->firmware_status == FW_STATUS_0_INIT ) { |
| 301 | rst_opt = OPT_SYSTEM_RESET; |
| 302 | starting_state = FW_INIT_STEP0_BOOT; |
| 303 | |
| 304 | }else if (pfirmware->firmware_status == FW_STATUS_5_READY) { |
| 305 | rst_opt = OPT_FIRMWARE_RESET; |
| 306 | starting_state = FW_INIT_STEP2_DATA; |
| 307 | }else { |
| 308 | RT_TRACE(COMP_FIRMWARE, "PlatformInitFirmware: undefined firmware state\n"); |
| 309 | } |
| 310 | |
| 311 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0) && defined(USE_FW_SOURCE_IMG_FILE) |
| 312 | priv->firmware_source = FW_SOURCE_IMG_FILE; |
| 313 | #else |
| 314 | priv->firmware_source = FW_SOURCE_HEADER_FILE; |
| 315 | #endif |
| 316 | for (init_step = starting_state; init_step <= FW_INIT_STEP2_DATA; init_step++) { |
| 317 | if (rst_opt == OPT_SYSTEM_RESET) { |
| 318 | switch (priv->firmware_source) { |
| 319 | case FW_SOURCE_IMG_FILE: |
| 320 | { |
| 321 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0) && defined(USE_FW_SOURCE_IMG_FILE) |
| 322 | if (pfirmware->firmware_buf_size[init_step] == 0) { |
| 323 | const char *fw_name[3] = { "RTL8192E/boot.img", |
| 324 | "RTL8192E/main.img", |
| 325 | "RTL8192E/data.img" |
| 326 | }; |
| 327 | const struct firmware *fw_entry; |
| 328 | int rc; |
| 329 | rc = request_firmware(&fw_entry, fw_name[init_step],&priv->pdev->dev); |
| 330 | if (rc < 0 ) { |
| 331 | RT_TRACE(COMP_FIRMWARE, "request firmware fail!\n"); |
| 332 | goto download_firmware_fail; |
| 333 | } |
| 334 | if (fw_entry->size > sizeof(pfirmware->firmware_buf[init_step])) { |
| 335 | RT_TRACE(COMP_FIRMWARE, "img file size exceed the container buffer fail!\n"); |
| 336 | goto download_firmware_fail; |
| 337 | } |
| 338 | |
| 339 | if (init_step != FW_INIT_STEP1_MAIN) { |
| 340 | memcpy(pfirmware->firmware_buf[init_step],fw_entry->data,fw_entry->size); |
| 341 | pfirmware->firmware_buf_size[init_step] = fw_entry->size; |
| 342 | |
| 343 | } else { |
| 344 | memset(pfirmware->firmware_buf[init_step],0,128); |
| 345 | memcpy(&pfirmware->firmware_buf[init_step][128],fw_entry->data,fw_entry->size); |
| 346 | pfirmware->firmware_buf_size[init_step] = fw_entry->size+128; |
| 347 | } |
| 348 | |
| 349 | if (rst_opt == OPT_SYSTEM_RESET) { |
| 350 | release_firmware(fw_entry); |
| 351 | } |
| 352 | } |
| 353 | mapped_file = pfirmware->firmware_buf[init_step]; |
| 354 | file_length = pfirmware->firmware_buf_size[init_step]; |
| 355 | #endif |
| 356 | break; |
| 357 | } |
| 358 | case FW_SOURCE_HEADER_FILE: |
| 359 | mapped_file = firmware_img_buf[init_step]; |
| 360 | file_length = firmware_img_len[init_step]; |
| 361 | if (init_step == FW_INIT_STEP2_DATA) { |
| 362 | memcpy(pfirmware->firmware_buf[init_step], mapped_file, file_length); |
| 363 | pfirmware->firmware_buf_size[init_step] = file_length; |
| 364 | } |
| 365 | break; |
| 366 | |
| 367 | default: |
| 368 | break; |
| 369 | } |
| 370 | |
| 371 | |
| 372 | } else if (rst_opt == OPT_FIRMWARE_RESET) { |
| 373 | mapped_file = pfirmware->firmware_buf[init_step]; |
| 374 | file_length = pfirmware->firmware_buf_size[init_step]; |
| 375 | } |
| 376 | |
| 377 | rt_status = fw_download_code(dev,mapped_file,file_length); |
| 378 | if (rt_status != true) { |
| 379 | goto download_firmware_fail; |
| 380 | } |
| 381 | |
| 382 | if (!firmware_check_ready(dev, init_step)) { |
| 383 | goto download_firmware_fail; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | RT_TRACE(COMP_FIRMWARE, "Firmware Download Success\n"); |
| 388 | return rt_status; |
| 389 | |
| 390 | download_firmware_fail: |
| 391 | RT_TRACE(COMP_ERR, "ERR in %s()\n", __func__); |
| 392 | rt_status = false; |
| 393 | return rt_status; |
| 394 | |
| 395 | } |
| 396 | #endif |