1 /**************************************************************************************************
2 * Procedure: Init boot code/firmware code/data session
4 * Description: This routine will intialize firmware. If any error occurs during the initialization
5 * process, the routine shall terminate immediately and return fail.
6 * NIC driver should call NdisOpenFile only from MiniportInitialize.
8 * Arguments: The pointer of the adapter
11 * NDIS_STATUS_FAILURE - the following initialization process should be terminated
12 * NDIS_STATUS_SUCCESS - if firmware initialization process success
13 **************************************************************************************************/
14 #if defined(RTL8192SE)||defined(RTL8192SU)
16 #include "r8192S_firmware.h"
17 #include <linux/unistd.h>
20 #include "r8192S_hw.h"
21 #include "r8192SU_HWImg.h"
22 //#include "r8192S_FwImgDTM.h"
24 //#include "r8192U_hw.h"
25 #include "r8192xU_firmware_img.h"
28 #include <linux/firmware.h>
30 #define byte(x,n) ( (x >> (8 * n)) & 0xff )
33 // Description: This routine will intialize firmware. If any error occurs during the initialization
34 // process, the routine shall terminate immediately and return fail.
36 // Arguments: The pointer of the adapter
37 // Code address (Virtual address, should fill descriptor with physical address)
39 // Created by Roger, 2008.04.10.
41 bool FirmwareDownloadCode(struct net_device *dev, u8 * code_virtual_address,u32 buffer_len)
43 struct r8192_priv *priv = ieee80211_priv(dev);
44 bool rt_status = true;
45 u16 frag_threshold = MAX_FIRMWARE_CODE_SIZE; //Fragmentation might be required in 90/92 but not in 92S
46 u16 frag_length, frag_offset = 0;
48 unsigned char *seg_ptr;
51 u16 ExtraDescOffset = 0;
54 fw_SetRQPN(dev); // For 92SE only
57 RT_TRACE(COMP_FIRMWARE, "--->FirmwareDownloadCode()\n" );
59 //MAX_TRANSMIT_BUFFER_SIZE
60 if(buffer_len >= MAX_FIRMWARE_CODE_SIZE-USB_HWDESC_HEADER_LEN)
62 RT_TRACE(COMP_ERR, "Size over MAX_FIRMWARE_CODE_SIZE! \n");
63 goto cmdsend_downloadcode_fail;
66 ExtraDescOffset = USB_HWDESC_HEADER_LEN;
69 if((buffer_len-frag_offset) > frag_threshold)
71 frag_length = frag_threshold + ExtraDescOffset;
75 frag_length = (u16)(buffer_len - frag_offset + ExtraDescOffset);
79 /* Allocate skb buffer to contain firmware info and tx descriptor info. */
80 skb = dev_alloc_skb(frag_length);
81 memcpy((unsigned char *)(skb->cb),&dev,sizeof(dev));
83 tcb_desc = (cb_desc*)(skb->cb + MAX_DEV_ADDR_SIZE);
84 tcb_desc->queue_index = TXCMD_QUEUE;
85 tcb_desc->bCmdOrInit = DESC_PACKET_TYPE_INIT;
86 tcb_desc->bLastIniPkt = bLastIniPkt;
88 skb_reserve(skb, ExtraDescOffset);
89 seg_ptr = (u8 *)skb_put(skb, (u32)(frag_length-ExtraDescOffset));
90 memcpy(seg_ptr, code_virtual_address+frag_offset, (u32)(frag_length-ExtraDescOffset));
92 tcb_desc->txbuf_size= frag_length;
94 if(!priv->ieee80211->check_nic_enough_desc(dev,tcb_desc->queue_index)||
95 (!skb_queue_empty(&priv->ieee80211->skb_waitQ[tcb_desc->queue_index]))||\
96 (priv->ieee80211->queue_stop) )
98 RT_TRACE(COMP_FIRMWARE,"=====================================================> tx full!\n");
99 skb_queue_tail(&priv->ieee80211->skb_waitQ[tcb_desc->queue_index], skb);
103 priv->ieee80211->softmac_hard_start_xmit(skb,dev);
106 frag_offset += (frag_length - ExtraDescOffset);
108 }while(frag_offset < buffer_len);
113 cmdsend_downloadcode_fail:
115 RT_TRACE(COMP_ERR, "CmdSendDownloadCode fail !!\n");
121 static void fw_SetRQPN(struct net_device *dev)
123 // Only for 92SE HW bug, we have to set RAPN before every FW download
124 // We can remove the code later.
125 write_nic_dword(dev, RQPN, 0xffffffff);
126 write_nic_dword(dev, RQPN+4, 0xffffffff);
127 write_nic_byte(dev, RQPN+8, 0xff);
128 write_nic_byte(dev, RQPN+0xB, 0x80);
129 //#if ((HAL_CODE_BASE == RTL8192_S) && (PLATFORM != PLATFORM_WINDOWS_USB))
135 FirmwareEnableCPU(struct net_device *dev)
138 RT_STATUS rtStatus = RT_STATUS_SUCCESS;
139 u8 tmpU1b, CPUStatus = 0;
141 u32 iCheckTime = 200;
143 RT_TRACE(COMP_FIRMWARE, "-->FirmwareEnableCPU()\n" );
145 fw_SetRQPN(dev); // For 92SE only
148 tmpU1b = read_nic_byte(dev, SYS_CLKR);
149 write_nic_byte(dev, SYS_CLKR, (tmpU1b|SYS_CPU_CLKSEL)); //AFE source
151 tmpU2b = read_nic_word(dev, SYS_FUNC_EN);
152 write_nic_word(dev, SYS_FUNC_EN, (tmpU2b|FEN_CPUEN));
154 //Polling IMEM Ready after CPU has refilled.
157 CPUStatus = read_nic_byte(dev, TCR);
158 if(CPUStatus& IMEM_RDY)
160 RT_TRACE(COMP_FIRMWARE, "IMEM Ready after CPU has refilled.\n");
166 }while(iCheckTime--);
168 if(!(CPUStatus & IMEM_RDY))
169 return RT_STATUS_FAILURE;
171 RT_TRACE(COMP_FIRMWARE, "<--FirmwareEnableCPU(): rtStatus(%#x)\n", rtStatus);
175 FIRMWARE_8192S_STATUS
176 FirmwareGetNextStatus(FIRMWARE_8192S_STATUS FWCurrentStatus)
178 FIRMWARE_8192S_STATUS NextFWStatus = 0;
180 switch(FWCurrentStatus)
183 NextFWStatus = FW_STATUS_LOAD_IMEM;
186 case FW_STATUS_LOAD_IMEM:
187 NextFWStatus = FW_STATUS_LOAD_EMEM;
190 case FW_STATUS_LOAD_EMEM:
191 NextFWStatus = FW_STATUS_LOAD_DMEM;
194 case FW_STATUS_LOAD_DMEM:
195 NextFWStatus = FW_STATUS_READY;
199 RT_TRACE(COMP_ERR,"Invalid FW Status(%#x)!!\n", FWCurrentStatus);
206 FirmwareCheckReady(struct net_device *dev, u8 LoadFWStatus)
208 struct r8192_priv *priv = ieee80211_priv(dev);
209 RT_STATUS rtStatus = RT_STATUS_SUCCESS;
210 rt_firmware *pFirmware = priv->pFirmware;
211 int PollingCnt = 1000;
212 //u8 tmpU1b, CPUStatus = 0;
215 //bool bOrgIMREnable;
217 RT_TRACE(COMP_FIRMWARE, "--->FirmwareCheckReady(): LoadStaus(%d),", LoadFWStatus);
219 pFirmware->FWStatus = (FIRMWARE_8192S_STATUS)LoadFWStatus;
220 if( LoadFWStatus == FW_STATUS_LOAD_IMEM)
223 {//Polling IMEM code done.
224 CPUStatus = read_nic_byte(dev, TCR);
225 if(CPUStatus& IMEM_CODE_DONE)
229 }while(PollingCnt--);
230 if(!(CPUStatus & IMEM_CHK_RPT) || PollingCnt <= 0)
232 RT_TRACE(COMP_ERR, "FW_STATUS_LOAD_IMEM FAIL CPU, Status=%x\r\n", CPUStatus);
236 else if( LoadFWStatus == FW_STATUS_LOAD_EMEM)
237 {//Check Put Code OK and Turn On CPU
239 {//Polling EMEM code done.
240 CPUStatus = read_nic_byte(dev, TCR);
241 if(CPUStatus& EMEM_CODE_DONE)
245 }while(PollingCnt--);
246 if(!(CPUStatus & EMEM_CHK_RPT))
248 RT_TRACE(COMP_ERR, "FW_STATUS_LOAD_EMEM FAIL CPU, Status=%x\r\n", CPUStatus);
253 rtStatus = FirmwareEnableCPU(dev);
254 if(rtStatus != RT_STATUS_SUCCESS)
256 RT_TRACE(COMP_ERR, "Enable CPU fail ! \n" );
260 else if( LoadFWStatus == FW_STATUS_LOAD_DMEM)
263 {//Polling DMEM code done
264 CPUStatus = read_nic_byte(dev, TCR);
265 if(CPUStatus& DMEM_CODE_DONE)
269 }while(PollingCnt--);
271 if(!(CPUStatus & DMEM_CODE_DONE))
273 RT_TRACE(COMP_ERR, "Polling DMEM code done fail ! CPUStatus(%#x)\n", CPUStatus);
277 RT_TRACE(COMP_FIRMWARE, "DMEM code download success, CPUStatus(%#x)\n", CPUStatus);
279 // PollingCnt = 100; // Set polling cycle to 10ms.
280 PollingCnt = 10000; // Set polling cycle to 10ms.
283 {//Polling Load Firmware ready
284 CPUStatus = read_nic_byte(dev, TCR);
285 if(CPUStatus & FWRDY)
289 }while(PollingCnt--);
291 RT_TRACE(COMP_FIRMWARE, "Polling Load Firmware ready, CPUStatus(%x)\n", CPUStatus);
293 //if(!(CPUStatus & LOAD_FW_READY))
294 //if((CPUStatus & LOAD_FW_READY) != 0xff)
295 if((CPUStatus & LOAD_FW_READY) != LOAD_FW_READY)
297 RT_TRACE(COMP_ERR, "Polling Load Firmware ready fail ! CPUStatus(%x)\n", CPUStatus);
302 // <Roger_Notes> USB interface will update reserved followings parameters later!!
306 //write_nic_dword(dev, RQPN, 0x10101010);
307 //write_nic_byte(dev, 0xAB, 0x80);
311 // <Roger_Notes> If right here, we can set TCR/RCR to desired value
312 // and config MAC lookback mode to normal mode. 2008.08.28.
314 tmpU4b = read_nic_dword(dev,TCR);
315 write_nic_dword(dev, TCR, (tmpU4b&(~TCR_ICV)));
317 tmpU4b = read_nic_dword(dev, RCR);
318 write_nic_dword(dev, RCR,
319 (tmpU4b|RCR_APPFCS|RCR_APP_ICV|RCR_APP_MIC));
321 RT_TRACE(COMP_FIRMWARE, "FirmwareCheckReady(): Current RCR settings(%#x)\n", tmpU4b);
323 #if defined (RTL8192SU_ASIC_VERIFICATION)
324 #ifdef NOT_YET //YJ,TMP
325 priv->TransmitConfig = read_nic_dword(dev, TCR);
326 RT_TRACE(COMP_FIRMWARE, "FirmwareCheckReady(): Current TCR settings(%x)\n", priv->TransmitConfig);
327 pHalData->FwRsvdTxPageCfg = read_nic_byte(dev, FW_RSVD_PG_CRTL);
331 // Set to normal mode.
332 write_nic_byte(dev, LBKMD_SEL, LBK_NORMAL);
336 RT_TRACE(COMP_FIRMWARE, "<---FirmwareCheckReady(): LoadFWStatus(%d), rtStatus(%x)\n", LoadFWStatus, rtStatus);
337 return (rtStatus == RT_STATUS_SUCCESS) ? true:false;
341 // Description: This routine is to update the RF types in FW header partially.
343 // Created by Roger, 2008.12.24.
345 u8 FirmwareHeaderMapRfType(struct net_device *dev)
347 struct r8192_priv *priv = ieee80211_priv(dev);
348 switch(priv->rf_type)
350 case RF_1T1R: return 0x11;
351 case RF_1T2R: return 0x12;
352 case RF_2T2R: return 0x22;
353 case RF_2T2R_GREEN: return 0x92;
355 RT_TRACE(COMP_INIT, "Unknown RF type(%x)\n",priv->rf_type);
363 // Description: This routine is to update the private parts in FW header partially.
365 // Created by Roger, 2008.12.18.
367 void FirmwareHeaderPriveUpdate(struct net_device *dev, PRT_8192S_FIRMWARE_PRIV pFwPriv)
369 struct r8192_priv *priv = ieee80211_priv(dev);
371 // Update USB endpoint number for RQPN settings.
372 pFwPriv->usb_ep_num = priv->EEPROMUsbEndPointNumber; // endpoint number: 4, 6 and 11.
373 RT_TRACE(COMP_INIT, "FirmwarePriveUpdate(): usb_ep_num(%#x)\n", pFwPriv->usb_ep_num);
376 // Update RF types for RATR settings.
377 pFwPriv->rf_config = FirmwareHeaderMapRfType(dev);
382 bool FirmwareDownload92S(struct net_device *dev)
384 struct r8192_priv *priv = ieee80211_priv(dev);
385 bool rtStatus = true;
386 const char *pFwImageFileName[1] = {"RTL8192SU/rtl8192sfw.bin"};
387 u8 *pucMappedFile = NULL;
388 u32 ulFileLength, ulInitStep = 0;
389 u8 FwHdrSize = RT_8192S_FIRMWARE_HDR_SIZE;
390 rt_firmware *pFirmware = priv->pFirmware;
391 u8 FwStatus = FW_STATUS_INIT;
392 PRT_8192S_FIRMWARE_HDR pFwHdr = NULL;
393 PRT_8192S_FIRMWARE_PRIV pFwPriv = NULL;
395 const struct firmware *fw_entry;
398 pFirmware->FWStatus = FW_STATUS_INIT;
400 RT_TRACE(COMP_FIRMWARE, " --->FirmwareDownload92S()\n");
403 //3 //<1> Open Image file, and map file to contineous memory if open file success.
404 //3 // or read image file from array. Default load from BIN file
406 priv->firmware_source = FW_SOURCE_IMG_FILE;// We should decided by Reg.
408 switch( priv->firmware_source )
410 case FW_SOURCE_IMG_FILE:
411 if(pFirmware->szFwTmpBufferLen == 0)
414 rc = request_firmware(&fw_entry, pFwImageFileName[ulInitStep],&priv->udev->dev);//===>1
416 RT_TRACE(COMP_ERR, "request firmware fail!\n");
417 goto DownloadFirmware_Fail;
420 if(fw_entry->size > sizeof(pFirmware->szFwTmpBuffer))
422 RT_TRACE(COMP_ERR, "img file size exceed the container buffer fail!\n");
423 release_firmware(fw_entry);
424 goto DownloadFirmware_Fail;
427 memcpy(pFirmware->szFwTmpBuffer,fw_entry->data,fw_entry->size);
428 pFirmware->szFwTmpBufferLen = fw_entry->size;
429 release_firmware(fw_entry);
431 pucMappedFile = pFirmware->szFwTmpBuffer;
432 file_length = pFirmware->szFwTmpBufferLen;
434 //Retrieve FW header.
435 pFirmware->pFwHeader = (PRT_8192S_FIRMWARE_HDR) pucMappedFile;
436 pFwHdr = pFirmware->pFwHeader;
437 RT_TRACE(COMP_FIRMWARE,"signature:%x, version:%x, size:%x, imemsize:%x, sram size:%x\n", \
438 pFwHdr->Signature, pFwHdr->Version, pFwHdr->DMEMSize, \
439 pFwHdr->IMG_IMEM_SIZE, pFwHdr->IMG_SRAM_SIZE);
440 pFirmware->FirmwareVersion = byte(pFwHdr->Version ,0);
441 if ((pFwHdr->IMG_IMEM_SIZE==0) || (pFwHdr->IMG_IMEM_SIZE > sizeof(pFirmware->FwIMEM)))
443 RT_TRACE(COMP_ERR, "%s: memory for data image is less than IMEM required\n",\
445 goto DownloadFirmware_Fail;
447 pucMappedFile+=FwHdrSize;
449 //Retrieve IMEM image.
450 memcpy(pFirmware->FwIMEM, pucMappedFile, pFwHdr->IMG_IMEM_SIZE);
451 pFirmware->FwIMEMLen = pFwHdr->IMG_IMEM_SIZE;
454 if (pFwHdr->IMG_SRAM_SIZE > sizeof(pFirmware->FwEMEM))
456 RT_TRACE(COMP_ERR, "%s: memory for data image is less than EMEM required\n",\
458 goto DownloadFirmware_Fail;
460 pucMappedFile += pFirmware->FwIMEMLen;
462 /* Retriecve EMEM image */
463 memcpy(pFirmware->FwEMEM, pucMappedFile, pFwHdr->IMG_SRAM_SIZE);//===>6
464 pFirmware->FwEMEMLen = pFwHdr->IMG_SRAM_SIZE;
471 case FW_SOURCE_HEADER_FILE:
473 #define Rtl819XFwImageArray Rtl8192SUFwImgArray
474 //2008.11.10 Add by tynli.
475 pucMappedFile = Rtl819XFwImageArray;
476 ulFileLength = ImgArrayLength;
478 RT_TRACE(COMP_INIT,"Fw download from header.\n");
479 /* Retrieve FW header*/
480 pFirmware->pFwHeader = (PRT_8192S_FIRMWARE_HDR) pucMappedFile;
481 pFwHdr = pFirmware->pFwHeader;
482 RT_TRACE(COMP_FIRMWARE,"signature:%x, version:%x, size:%x, imemsize:%x, sram size:%x\n", \
483 pFwHdr->Signature, pFwHdr->Version, pFwHdr->DMEMSize, \
484 pFwHdr->IMG_IMEM_SIZE, pFwHdr->IMG_SRAM_SIZE);
485 pFirmware->FirmwareVersion = byte(pFwHdr->Version ,0);
487 if ((pFwHdr->IMG_IMEM_SIZE==0) || (pFwHdr->IMG_IMEM_SIZE > sizeof(pFirmware->FwIMEM)))
489 printk("FirmwareDownload92S(): memory for data image is less than IMEM required\n");
490 goto DownloadFirmware_Fail;
492 pucMappedFile+=FwHdrSize;
493 //Retrieve IMEM image.
494 memcpy(pFirmware->FwIMEM, pucMappedFile, pFwHdr->IMG_IMEM_SIZE);
495 pFirmware->FwIMEMLen = pFwHdr->IMG_IMEM_SIZE;
498 if (pFwHdr->IMG_SRAM_SIZE > sizeof(pFirmware->FwEMEM))
500 printk(" FirmwareDownload92S(): memory for data image is less than EMEM required\n");
501 goto DownloadFirmware_Fail;
503 pucMappedFile+= pFirmware->FwIMEMLen;
505 //Retriecve EMEM image.
506 memcpy(pFirmware->FwEMEM, pucMappedFile, pFwHdr->IMG_SRAM_SIZE);
507 pFirmware->FwEMEMLen = pFwHdr->IMG_SRAM_SIZE;
515 FwStatus = FirmwareGetNextStatus(pFirmware->FWStatus);
516 while(FwStatus!= FW_STATUS_READY)
518 // Image buffer redirection.
521 case FW_STATUS_LOAD_IMEM:
522 pucMappedFile = pFirmware->FwIMEM;
523 ulFileLength = pFirmware->FwIMEMLen;
526 case FW_STATUS_LOAD_EMEM:
527 pucMappedFile = pFirmware->FwEMEM;
528 ulFileLength = pFirmware->FwEMEMLen;
531 case FW_STATUS_LOAD_DMEM:
532 /* <Roger_Notes> Partial update the content of header private. 2008.12.18 */
533 pFwHdr = pFirmware->pFwHeader;
534 pFwPriv = (PRT_8192S_FIRMWARE_PRIV)&pFwHdr->FWPriv;
535 FirmwareHeaderPriveUpdate(dev, pFwPriv);
536 pucMappedFile = (u8*)(pFirmware->pFwHeader)+RT_8192S_FIRMWARE_HDR_EXCLUDE_PRI_SIZE;
537 ulFileLength = FwHdrSize-RT_8192S_FIRMWARE_HDR_EXCLUDE_PRI_SIZE;
541 RT_TRACE(COMP_ERR, "Unexpected Download step!!\n");
542 goto DownloadFirmware_Fail;
547 //3// <2> Download image file
549 rtStatus = FirmwareDownloadCode(dev, pucMappedFile, ulFileLength);
553 RT_TRACE(COMP_ERR, "FirmwareDownloadCode() fail ! \n" );
554 goto DownloadFirmware_Fail;
558 //3// <3> Check whether load FW process is ready
560 rtStatus = FirmwareCheckReady(dev, FwStatus);
564 RT_TRACE(COMP_ERR, "FirmwareDownloadCode() fail ! \n");
565 goto DownloadFirmware_Fail;
568 FwStatus = FirmwareGetNextStatus(pFirmware->FWStatus);
571 RT_TRACE(COMP_FIRMWARE, "Firmware Download Success!!\n");
574 DownloadFirmware_Fail:
575 RT_TRACE(COMP_ERR, "Firmware Download Fail!!%x\n",read_nic_word(dev, TCR));
580 void firmware_init_param(struct net_device *dev)
582 struct r8192_priv *priv = ieee80211_priv(dev);
583 rt_firmware *pfirmware = priv->pFirmware;
585 pfirmware->cmdpacket_frag_thresold = GET_COMMAND_PACKET_FRAG_THRESHOLD(MAX_TRANSMIT_BUFFER_SIZE);
589 * segment the img and use the ptr and length to remember info on each segment
592 bool fw_download_code(struct net_device *dev, u8 *code_virtual_address, u32 buffer_len)
594 struct r8192_priv *priv = ieee80211_priv(dev);
595 bool rt_status = true;
596 //u16 frag_threshold;
597 u16 frag_length, frag_offset = 0;
601 //rt_firmware *pfirmware = priv->pFirmware;
603 unsigned char *seg_ptr;
607 fw_SetRQPN(dev); // For 92SE only
611 if(buffer_len >= 64000-USB_HWDESC_HEADER_LEN)
615 firmware_init_param(dev);
616 //Fragmentation might be required
617 frag_threshold = pfirmware->cmdpacket_frag_thresold;
622 if((buffer_len - frag_offset) > frag_threshold) {
623 frag_length = frag_threshold ;
629 frag_length = buffer_len - frag_offset;
634 /* Allocate skb buffer to contain firmware info and tx descriptor info
635 * add 4 to avoid packet appending overflow.
638 skb = dev_alloc_skb(USB_HWDESC_HEADER_LEN + frag_length + 4);
640 skb = dev_alloc_skb(frag_length + 4);
642 memcpy((unsigned char *)(skb->cb),&dev,sizeof(dev));
643 tcb_desc = (cb_desc*)(skb->cb + MAX_DEV_ADDR_SIZE);
644 tcb_desc->queue_index = TXCMD_QUEUE;
645 tcb_desc->bCmdOrInit = DESC_PACKET_TYPE_INIT;
646 tcb_desc->bLastIniPkt = bLastIniPkt;
649 skb_reserve(skb, USB_HWDESC_HEADER_LEN);
653 * Transform from little endian to big endian
656 for(i=0 ; i < frag_length; i+=4) {
657 *seg_ptr++ = ((i+0)<frag_length)?code_virtual_address[i+3]:0;
658 *seg_ptr++ = ((i+1)<frag_length)?code_virtual_address[i+2]:0;
659 *seg_ptr++ = ((i+2)<frag_length)?code_virtual_address[i+1]:0;
660 *seg_ptr++ = ((i+3)<frag_length)?code_virtual_address[i+0]:0;
662 tcb_desc->txbuf_size= (u16)i;
665 if(!priv->ieee80211->check_nic_enough_desc(dev,tcb_desc->queue_index)||
666 (!skb_queue_empty(&priv->ieee80211->skb_waitQ[tcb_desc->queue_index]))||\
667 (priv->ieee80211->queue_stop) ) {
668 RT_TRACE(COMP_FIRMWARE,"=====================================================> tx full!\n");
669 skb_queue_tail(&priv->ieee80211->skb_waitQ[tcb_desc->queue_index], skb);
671 priv->ieee80211->softmac_hard_start_xmit(skb,dev);
674 code_virtual_address += frag_length;
675 frag_offset += frag_length;
677 }while(frag_offset < buffer_len);
682 cmdsend_downloadcode_fail:
684 RT_TRACE(COMP_ERR, "CmdSendDownloadCode fail !!\n");
691 struct net_device *dev,
695 bool rtStatus = true;
696 struct r8192_priv *priv = ieee80211_priv(dev);
699 unsigned char *ptr_buf;
700 bool bLastInitPacket = false;
702 //PlatformAcquireSpinLock(dev, RT_TX_SPINLOCK);
704 //Get TCB and local buffer from common pool. (It is shared by CmdQ, MgntQ, and USB coalesce DataQ)
705 skb = dev_alloc_skb(Length+ 4);
706 memcpy((unsigned char *)(skb->cb),&dev,sizeof(dev));
707 tcb_desc = (cb_desc*)(skb->cb + MAX_DEV_ADDR_SIZE);
708 tcb_desc->queue_index = TXCMD_QUEUE;
709 tcb_desc->bCmdOrInit = DESC_PACKET_TYPE_INIT;
710 tcb_desc->bLastIniPkt = bLastInitPacket;
711 ptr_buf = skb_put(skb, Length);
712 memset(ptr_buf,0,Length);
713 tcb_desc->txbuf_size= (u16)Length;
715 if(!priv->ieee80211->check_nic_enough_desc(dev,tcb_desc->queue_index)||
716 (!skb_queue_empty(&priv->ieee80211->skb_waitQ[tcb_desc->queue_index]))||\
717 (priv->ieee80211->queue_stop) ) {
718 RT_TRACE(COMP_FIRMWARE,"===================NULL packet==================================> tx full!\n");
719 skb_queue_tail(&priv->ieee80211->skb_waitQ[tcb_desc->queue_index], skb);
721 priv->ieee80211->softmac_hard_start_xmit(skb,dev);
724 //PlatformReleaseSpinLock(dev, RT_TX_SPINLOCK);
728 //-----------------------------------------------------------------------------
729 // Procedure: Check whether main code is download OK. If OK, turn on CPU
731 // Description: CPU register locates in different page against general register.
732 // Switch to CPU register in the begin and switch back before return
735 // Arguments: The pointer of the dev
738 // NDIS_STATUS_FAILURE - the following initialization process should be terminated
739 // NDIS_STATUS_SUCCESS - if firmware initialization process success
740 //-----------------------------------------------------------------------------
741 bool CPUcheck_maincodeok_turnonCPU(struct net_device *dev)
743 bool rt_status = true;
744 int check_putcodeOK_time = 200000, check_bootOk_time = 200000;
747 /* Check whether put code OK */
749 CPU_status = read_nic_dword(dev, CPU_GEN);
751 if(CPU_status&CPU_GEN_PUT_CODE_OK)
754 }while(check_putcodeOK_time--);
756 if(!(CPU_status&CPU_GEN_PUT_CODE_OK)) {
757 RT_TRACE(COMP_ERR, "Download Firmware: Put code fail!\n");
758 goto CPUCheckMainCodeOKAndTurnOnCPU_Fail;
760 RT_TRACE(COMP_FIRMWARE, "Download Firmware: Put code ok!\n");
764 CPU_status = read_nic_dword(dev, CPU_GEN);
765 write_nic_byte(dev, CPU_GEN, (u8)((CPU_status|CPU_GEN_PWR_STB_CPU)&0xff));
768 /* Check whether CPU boot OK */
770 CPU_status = read_nic_dword(dev, CPU_GEN);
772 if(CPU_status&CPU_GEN_BOOT_RDY)
774 }while(check_bootOk_time--);
776 if(!(CPU_status&CPU_GEN_BOOT_RDY)) {
777 goto CPUCheckMainCodeOKAndTurnOnCPU_Fail;
779 RT_TRACE(COMP_FIRMWARE, "Download Firmware: Boot ready!\n");
784 CPUCheckMainCodeOKAndTurnOnCPU_Fail:
785 RT_TRACE(COMP_ERR, "ERR in %s()\n", __FUNCTION__);
790 bool CPUcheck_firmware_ready(struct net_device *dev)
793 bool rt_status = true;
794 int check_time = 200000;
797 /* Check Firmware Ready */
799 CPU_status = read_nic_dword(dev, CPU_GEN);
801 if(CPU_status&CPU_GEN_FIRM_RDY)
804 }while(check_time--);
806 if(!(CPU_status&CPU_GEN_FIRM_RDY))
807 goto CPUCheckFirmwareReady_Fail;
809 RT_TRACE(COMP_FIRMWARE, "Download Firmware: Firmware ready!\n");
813 CPUCheckFirmwareReady_Fail:
814 RT_TRACE(COMP_ERR, "ERR in %s()\n", __FUNCTION__);
820 bool init_firmware(struct net_device *dev)
822 struct r8192_priv *priv = ieee80211_priv(dev);
823 bool rt_status = TRUE;
825 u8 *firmware_img_buf[3] = { &rtl8190_fwboot_array[0],
826 &rtl8190_fwmain_array[0],
827 &rtl8190_fwdata_array[0]};
829 u32 firmware_img_len[3] = { sizeof(rtl8190_fwboot_array),
830 sizeof(rtl8190_fwmain_array),
831 sizeof(rtl8190_fwdata_array)};
833 u8 *mapped_file = NULL;
835 opt_rst_type_e rst_opt = OPT_SYSTEM_RESET;
836 firmware_init_step_e starting_state = FW_INIT_STEP0_BOOT;
838 rt_firmware *pfirmware = priv->pFirmware;
839 const struct firmware *fw_entry;
840 const char *fw_name[3] = { "RTL8192U/boot.img",
842 "RTL8192U/data.img"};
845 RT_TRACE(COMP_FIRMWARE, " PlatformInitFirmware()==>\n");
847 if (pfirmware->firmware_status == FW_STATUS_0_INIT ) {
848 /* it is called by reset */
849 rst_opt = OPT_SYSTEM_RESET;
850 starting_state = FW_INIT_STEP0_BOOT;
851 // TODO: system reset
853 }else if(pfirmware->firmware_status == FW_STATUS_5_READY) {
854 /* it is called by Initialize */
855 rst_opt = OPT_FIRMWARE_RESET;
856 starting_state = FW_INIT_STEP2_DATA;
858 RT_TRACE(COMP_FIRMWARE, "PlatformInitFirmware: undefined firmware state\n");
862 * Download boot, main, and data image for System reset.
863 * Download data image for firmware reseta
865 priv->firmware_source = FW_SOURCE_IMG_FILE;
867 for(init_step = starting_state; init_step <= FW_INIT_STEP2_DATA; init_step++) {
869 * Open Image file, and map file to contineous memory if open file success.
870 * or read image file from array. Default load from IMG file
872 if(rst_opt == OPT_SYSTEM_RESET) {
873 switch(priv->firmware_source) {
874 case FW_SOURCE_IMG_FILE:
875 rc = request_firmware(&fw_entry, fw_name[init_step],&priv->udev->dev);
877 RT_TRACE(COMP_ERR, "request firmware fail!\n");
878 goto download_firmware_fail;
881 if(fw_entry->size > sizeof(pfirmware->firmware_buf)) {
882 RT_TRACE(COMP_ERR, "img file size exceed the container buffer fail!\n");
883 goto download_firmware_fail;
886 if(init_step != FW_INIT_STEP1_MAIN) {
887 memcpy(pfirmware->firmware_buf,fw_entry->data,fw_entry->size);
888 mapped_file = pfirmware->firmware_buf;
889 file_length = fw_entry->size;
892 memcpy(pfirmware->firmware_buf,fw_entry->data,fw_entry->size);
893 mapped_file = pfirmware->firmware_buf;
894 file_length = fw_entry->size;
896 memset(pfirmware->firmware_buf,0,128);
897 memcpy(&pfirmware->firmware_buf[128],fw_entry->data,fw_entry->size);
898 mapped_file = pfirmware->firmware_buf;
899 file_length = fw_entry->size + 128;
902 pfirmware->firmware_buf_size = file_length;
905 case FW_SOURCE_HEADER_FILE:
906 mapped_file = firmware_img_buf[init_step];
907 file_length = firmware_img_len[init_step];
908 if(init_step == FW_INIT_STEP2_DATA) {
909 memcpy(pfirmware->firmware_buf, mapped_file, file_length);
910 pfirmware->firmware_buf_size = file_length;
919 }else if(rst_opt == OPT_FIRMWARE_RESET ) {
920 /* we only need to download data.img here */
921 mapped_file = pfirmware->firmware_buf;
922 file_length = pfirmware->firmware_buf_size;
925 /* Download image file */
926 /* The firmware download process is just as following,
927 * 1. that is each packet will be segmented and inserted to the wait queue.
928 * 2. each packet segment will be put in the skb_buff packet.
929 * 3. each skb_buff packet data content will already include the firmware info
930 * and Tx descriptor info
932 rt_status = fw_download_code(dev,mapped_file,file_length);
934 if(rst_opt == OPT_SYSTEM_RESET) {
935 release_firmware(fw_entry);
938 if(rt_status != TRUE) {
939 goto download_firmware_fail;
943 case FW_INIT_STEP0_BOOT:
945 * initialize command descriptor.
946 * will set polling bit when firmware code is also configured
948 pfirmware->firmware_status = FW_STATUS_1_MOVE_BOOT_CODE;
950 // To initialize IMEM, CPU move code from 0x80000080, hence, we send 0x80 byte packet
951 rt_status = fwSendNullPacket(dev, RTL8190_CPU_START_OFFSET);
952 if(rt_status != true)
954 RT_TRACE(COMP_INIT, "fwSendNullPacket() fail ! \n");
955 goto download_firmware_fail;
960 * To initialize IMEM, CPU move code from 0x80000080,
961 * hence, we send 0x80 byte packet
965 case FW_INIT_STEP1_MAIN:
966 /* Download firmware code. Wait until Boot Ready and Turn on CPU */
967 pfirmware->firmware_status = FW_STATUS_2_MOVE_MAIN_CODE;
969 /* Check Put Code OK and Turn On CPU */
970 rt_status = CPUcheck_maincodeok_turnonCPU(dev);
971 if(rt_status != TRUE) {
972 RT_TRACE(COMP_ERR, "CPUcheck_maincodeok_turnonCPU fail!\n");
973 goto download_firmware_fail;
976 pfirmware->firmware_status = FW_STATUS_3_TURNON_CPU;
979 case FW_INIT_STEP2_DATA:
980 /* download initial data code */
981 pfirmware->firmware_status = FW_STATUS_4_MOVE_DATA_CODE;
984 rt_status = CPUcheck_firmware_ready(dev);
985 if(rt_status != TRUE) {
986 RT_TRACE(COMP_ERR, "CPUcheck_firmware_ready fail(%d)!\n",rt_status);
987 goto download_firmware_fail;
990 /* wait until data code is initialized ready.*/
991 pfirmware->firmware_status = FW_STATUS_5_READY;
996 RT_TRACE(COMP_FIRMWARE, "Firmware Download Success\n");
997 //assert(pfirmware->firmware_status == FW_STATUS_5_READY, ("Firmware Download Fail\n"));
1001 download_firmware_fail:
1002 RT_TRACE(COMP_ERR, "ERR in %s()\n", __FUNCTION__);