2 *************************************************************************
4 * 5F., No.36, Taiyuan St., Jhubei City,
8 * (c) Copyright 2002-2007, Ralink Technology, Inc.
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 *************************************************************************
34 -------- ---------- ----------------------------------------------
35 Name Date Modification logs
37 #include "../rt_config.h"
39 // IRQL = PASSIVE_LEVEL
45 RTMP_IO_WRITE32(pAd, E2PROM_CSR, *x);
46 RTMPusecDelay(1); // Max frequency = 1MHz in Spec. definition
49 // IRQL = PASSIVE_LEVEL
55 RTMP_IO_WRITE32(pAd, E2PROM_CSR, *x);
59 // IRQL = PASSIVE_LEVEL
66 RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
75 RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
77 LowerClock(pAd, &x); /* prevent read failed */
87 // IRQL = PASSIVE_LEVEL
95 mask = 0x01 << (count - 1);
96 RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
103 if(data & mask) x |= EEDI;
105 RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
114 RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
117 // IRQL = PASSIVE_LEVEL
119 IN PRTMP_ADAPTER pAd)
123 RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
126 RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
133 IN PRTMP_ADAPTER pAd)
137 // reset bits and set EECS
138 RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
139 x &= ~(EEDI | EEDO | EESK);
141 RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
147 // output the read_opcode and six pulse in that order
148 ShiftOutBits(pAd, EEPROM_EWEN_OPCODE, 5);
149 ShiftOutBits(pAd, 0, 6);
155 IN PRTMP_ADAPTER pAd)
159 // reset bits and set EECS
160 RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
161 x &= ~(EEDI | EEDO | EESK);
163 RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
169 // output the read_opcode and six pulse in that order
170 ShiftOutBits(pAd, EEPROM_EWDS_OPCODE, 5);
171 ShiftOutBits(pAd, 0, 6);
176 // IRQL = PASSIVE_LEVEL
177 USHORT RTMP_EEPROM_READ16(
178 IN PRTMP_ADAPTER pAd,
185 if (pAd->NicConfig2.field.AntDiversity)
187 pAd->EepromAccess = TRUE;
189 //2008/09/11:KH add to support efuse<--
190 //2008/09/11:KH add to support efuse-->
194 // reset bits and set EECS
195 RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
196 x &= ~(EEDI | EEDO | EESK);
198 RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
200 // patch can not access e-Fuse issue
208 // output the read_opcode and register number in that order
209 ShiftOutBits(pAd, EEPROM_READ_OPCODE, 3);
210 ShiftOutBits(pAd, Offset, pAd->EEPROMAddressNum);
212 // Now read the data (16 bits) in from the selected EEPROM word
213 data = ShiftInBits(pAd);
218 // Antenna and EEPROM access are both using EESK pin,
219 // Therefor we should avoid accessing EESK at the same time
220 // Then restore antenna after EEPROM access
221 if ((pAd->NicConfig2.field.AntDiversity) || (pAd->RfIcType == RFIC_3020))
223 pAd->EepromAccess = FALSE;
224 AsicSetRxAnt(pAd, pAd->RxAnt.Pair1PrimaryRxAnt);
231 VOID RTMP_EEPROM_WRITE16(
232 IN PRTMP_ADAPTER pAd,
239 if (pAd->NicConfig2.field.AntDiversity)
241 pAd->EepromAccess = TRUE;
243 //2008/09/11:KH add to support efuse<--
244 //2008/09/11:KH add to support efuse-->
251 // reset bits and set EECS
252 RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
253 x &= ~(EEDI | EEDO | EESK);
255 RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
257 // patch can not access e-Fuse issue
265 // output the read_opcode ,register number and data in that order
266 ShiftOutBits(pAd, EEPROM_WRITE_OPCODE, 3);
267 ShiftOutBits(pAd, Offset, pAd->EEPROMAddressNum);
268 ShiftOutBits(pAd, Data, 16); // 16-bit access
271 RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
275 RTMPusecDelay(10000); //delay for twp(MAX)=10ms
282 // Antenna and EEPROM access are both using EESK pin,
283 // Therefor we should avoid accessing EESK at the same time
284 // Then restore antenna after EEPROM access
285 if ((pAd->NicConfig2.field.AntDiversity) || (pAd->RfIcType == RFIC_3020))
287 pAd->EepromAccess = FALSE;
288 AsicSetRxAnt(pAd, pAd->RxAnt.Pair1PrimaryRxAnt);
296 ========================================================================
308 ========================================================================
310 UCHAR eFuseReadRegisters(
311 IN PRTMP_ADAPTER pAd,
316 EFUSE_CTRL_STRUC eFuseCtrlStruc;
318 USHORT efuseDataOffset;
321 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
323 //Step0. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
324 //Use the eeprom logical address and covert to address to block number
325 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
327 //Step1. Write EFSROM_MODE (0x580, bit7:bit6) to 0.
328 eFuseCtrlStruc.field.EFSROM_MODE = 0;
330 //Step2. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical read procedure.
331 eFuseCtrlStruc.field.EFSROM_KICK = 1;
333 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
334 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
336 //Step3. Polling EFSROM_KICK(0x580, bit30) until it become 0 again.
340 //rtmp.HwMemoryReadDword(EFUSE_CTRL, (DWORD *) &eFuseCtrlStruc, 4);
341 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
342 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
350 //if EFSROM_AOUT is not found in physical address, write 0xffff
351 if (eFuseCtrlStruc.field.EFSROM_AOUT == 0x3f)
353 for(i=0; i<Length/2; i++)
354 *(pData+2*i) = 0xffff;
358 //Step4. Read 16-byte of data from EFUSE_DATA0-3 (0x590-0x59C)
359 efuseDataOffset = EFUSE_DATA3 - (Offset & 0xC) ;
360 //data hold 4 bytes data.
361 //In RTMP_IO_READ32 will automatically execute 32-bytes swapping
362 RTMP_IO_READ32(pAd, efuseDataOffset, &data);
363 //Decide the upper 2 bytes or the bottom 2 bytes.
364 // Little-endian S | S Big-endian
365 // addr 3 2 1 0 | 0 1 2 3
366 // Ori-V D C B A | A B C D
370 //The return byte statrs from S. Therefore, the little-endian will return BA, the Big-endian will return DC.
371 //For returning the bottom 2 bytes, the Big-endian should shift right 2-bytes.
372 data = data >> (8*(Offset & 0x3));
374 NdisMoveMemory(pData, &data, Length);
377 return (UCHAR) eFuseCtrlStruc.field.EFSROM_AOUT;
382 ========================================================================
394 ========================================================================
396 VOID eFusePhysicalReadRegisters(
397 IN PRTMP_ADAPTER pAd,
402 EFUSE_CTRL_STRUC eFuseCtrlStruc;
404 USHORT efuseDataOffset;
407 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
409 //Step0. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
410 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
412 //Step1. Write EFSROM_MODE (0x580, bit7:bit6) to 1.
413 //Read in physical view
414 eFuseCtrlStruc.field.EFSROM_MODE = 1;
416 //Step2. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical read procedure.
417 eFuseCtrlStruc.field.EFSROM_KICK = 1;
419 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
420 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
422 //Step3. Polling EFSROM_KICK(0x580, bit30) until it become 0 again.
426 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
427 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
433 //Step4. Read 16-byte of data from EFUSE_DATA0-3 (0x59C-0x590)
434 //Because the size of each EFUSE_DATA is 4 Bytes, the size of address of each is 2 bits.
435 //The previous 2 bits is the EFUSE_DATA number, the last 2 bits is used to decide which bytes
436 //Decide which EFUSE_DATA to read
441 efuseDataOffset = EFUSE_DATA3 - (Offset & 0xC) ;
443 RTMP_IO_READ32(pAd, efuseDataOffset, &data);
445 data = data >> (8*(Offset & 0x3));
447 NdisMoveMemory(pData, &data, Length);
452 ========================================================================
464 ========================================================================
466 VOID eFuseReadPhysical(
467 IN PRTMP_ADAPTER pAd,
468 IN PUSHORT lpInBuffer,
469 IN ULONG nInBufferSize,
470 OUT PUSHORT lpOutBuffer,
471 IN ULONG nOutBufferSize
474 USHORT* pInBuf = (USHORT*)lpInBuffer;
475 USHORT* pOutBuf = (USHORT*)lpOutBuffer;
477 USHORT Offset = pInBuf[0]; //addr
478 USHORT Length = pInBuf[1]; //length
481 for(i=0; i<Length; i+=2)
483 eFusePhysicalReadRegisters(pAd,Offset+i, 2, &pOutBuf[i/2]);
488 ========================================================================
500 ========================================================================
503 IN PRTMP_ADAPTER pAd,
508 USHORT* pOutBuf = (USHORT*)pData;
509 NTSTATUS Status = STATUS_SUCCESS;
513 for(i=0; i<Length; i+=2)
515 EFSROM_AOUT = eFuseReadRegisters(pAd, Offset+i, 2, &pOutBuf[i/2]);
521 ========================================================================
533 ========================================================================
535 VOID eFusePhysicalWriteRegisters(
536 IN PRTMP_ADAPTER pAd,
541 EFUSE_CTRL_STRUC eFuseCtrlStruc;
543 USHORT efuseDataOffset;
544 UINT32 data, eFuseDataBuffer[4];
546 //Step0. Write 16-byte of data to EFUSE_DATA0-3 (0x590-0x59C), where EFUSE_DATA0 is the LSB DW, EFUSE_DATA3 is the MSB DW.
548 /////////////////////////////////////////////////////////////////
549 //read current values of 16-byte block
550 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
552 //Step0. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
553 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
555 //Step1. Write EFSROM_MODE (0x580, bit7:bit6) to 1.
556 eFuseCtrlStruc.field.EFSROM_MODE = 1;
558 //Step2. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical read procedure.
559 eFuseCtrlStruc.field.EFSROM_KICK = 1;
561 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
562 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
564 //Step3. Polling EFSROM_KICK(0x580, bit30) until it become 0 again.
568 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
570 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
576 //Step4. Read 16-byte of data from EFUSE_DATA0-3 (0x59C-0x590)
577 efuseDataOffset = EFUSE_DATA3;
580 RTMP_IO_READ32(pAd, efuseDataOffset, (PUINT32) &eFuseDataBuffer[i]);
581 efuseDataOffset -= 4;
584 //Update the value, the offset is multiple of 2, length is 2
585 efuseDataOffset = (Offset & 0xc) >> 2;
586 data = pData[0] & 0xffff;
587 //The offset should be 0x***10 or 0x***00
588 if((Offset % 4) != 0)
590 eFuseDataBuffer[efuseDataOffset] = (eFuseDataBuffer[efuseDataOffset] & 0xffff) | (data << 16);
594 eFuseDataBuffer[efuseDataOffset] = (eFuseDataBuffer[efuseDataOffset] & 0xffff0000) | data;
597 efuseDataOffset = EFUSE_DATA3;
600 RTMP_IO_WRITE32(pAd, efuseDataOffset, eFuseDataBuffer[i]);
601 efuseDataOffset -= 4;
603 /////////////////////////////////////////////////////////////////
605 //Step1. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
606 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
608 //Step2. Write EFSROM_MODE (0x580, bit7:bit6) to 3.
609 eFuseCtrlStruc.field.EFSROM_MODE = 3;
611 //Step3. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical write procedure.
612 eFuseCtrlStruc.field.EFSROM_KICK = 1;
614 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
615 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
617 //Step4. Polling EFSROM_KICK(0x580, bit30) until it become 0 again. It��s done.
621 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
623 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
632 ========================================================================
644 ========================================================================
646 NTSTATUS eFuseWriteRegisters(
647 IN PRTMP_ADAPTER pAd,
654 USHORT LogicalAddress, BlkNum = 0xffff;
657 USHORT addr,tmpaddr, InBuf[3], tmpOffset;
659 BOOLEAN bWriteSuccess = TRUE;
661 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters Offset=%x, pData=%x\n", Offset, *pData));
663 //Step 0. find the entry in the mapping table
664 //The address of EEPROM is 2-bytes alignment.
665 //The last bit is used for alignment, so it must be 0.
666 tmpOffset = Offset & 0xfffe;
667 EFSROM_AOUT = eFuseReadRegisters(pAd, tmpOffset, 2, &eFuseData);
669 if( EFSROM_AOUT == 0x3f)
670 { //find available logical address pointer
671 //the logical address does not exist, find an empty one
672 //from the first address of block 45=16*45=0x2d0 to the last address of block 47
673 //==>48*16-3(reserved)=2FC
674 for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
676 //Retrive the logical block nubmer form each logical address pointer
677 //It will access two logical address pointer each time.
678 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
679 if( (LogicalAddress & 0xff) == 0)
680 {//Not used logical address pointer
681 BlkNum = i-EFUSE_USAGE_MAP_START;
684 else if(( (LogicalAddress >> 8) & 0xff) == 0)
685 {//Not used logical address pointer
686 if (i != EFUSE_USAGE_MAP_END)
688 BlkNum = i-EFUSE_USAGE_MAP_START+1;
696 BlkNum = EFSROM_AOUT;
699 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters BlkNum = %d \n", BlkNum));
703 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters: out of free E-fuse space!!!\n"));
707 //Step 1. Save data of this block which is pointed by the avaible logical address pointer
708 // read and save the original block data
711 addr = BlkNum * 0x10 ;
717 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
719 buffer[i] = InBuf[2];
722 //Step 2. Update the data in buffer, and write the data to Efuse
723 buffer[ (Offset >> 1) % 8] = pData[0];
727 //Step 3. Write the data to Efuse
732 addr = BlkNum * 0x10 ;
736 InBuf[2] = buffer[i];
738 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 2);
743 addr = BlkNum * 0x10 ;
745 InBuf[0] = addr+(Offset % 16);
749 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 2);
752 //Step 4. Write mapping table
753 addr = EFUSE_USAGE_MAP_START+BlkNum;
762 //convert the address from 10 to 8 bit ( bit7, 6 = parity and bit5 ~ 0 = bit9~4), and write to logical map entry
765 tmpOffset |= ((~((tmpOffset & 0x01) ^ ( tmpOffset >> 1 & 0x01) ^ (tmpOffset >> 2 & 0x01) ^ (tmpOffset >> 3 & 0x01))) << 6) & 0x40;
766 tmpOffset |= ((~( (tmpOffset >> 2 & 0x01) ^ (tmpOffset >> 3 & 0x01) ^ (tmpOffset >> 4 & 0x01) ^ ( tmpOffset >> 5 & 0x01))) << 7) & 0x80;
768 // write the logical address
770 InBuf[2] = tmpOffset<<8;
772 InBuf[2] = tmpOffset;
774 eFuseWritePhysical(pAd,&InBuf[0], 6, NULL, 0);
776 //Step 5. Compare data if not the same, invalidate the mapping entry, then re-write the data until E-fuse is exhausted
777 bWriteSuccess = TRUE;
780 addr = BlkNum * 0x10 ;
786 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
788 if(buffer[i] != InBuf[2])
790 bWriteSuccess = FALSE;
795 //Step 6. invlidate mapping entry and find a free mapping entry if not succeed
798 DBGPRINT(RT_DEBUG_TRACE, ("Not bWriteSuccess BlkNum = %d\n", BlkNum));
800 // the offset of current mapping entry
801 addr = EFUSE_USAGE_MAP_START+BlkNum;
803 //find a new mapping entry
805 for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
807 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
808 if( (LogicalAddress & 0xff) == 0)
810 BlkNum = i-EFUSE_USAGE_MAP_START;
813 else if(( (LogicalAddress >> 8) & 0xff) == 0)
815 if (i != EFUSE_USAGE_MAP_END)
817 BlkNum = i+1-EFUSE_USAGE_MAP_START;
822 DBGPRINT(RT_DEBUG_TRACE, ("Not bWriteSuccess new BlkNum = %d\n", BlkNum));
825 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters: out of free E-fuse space!!!\n"));
829 //invalidate the original mapping entry if new entry is not found
837 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
839 // write the logical address
842 // Invalidate the high byte
845 if( ( (InBuf[2] >> i) & 0x01) == 0)
847 InBuf[2] |= (0x1 <<i);
854 // invalidate the low byte
857 if( ( (InBuf[2] >> i) & 0x01) == 0)
859 InBuf[2] |= (0x1 <<i);
864 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 0);
867 while(!bWriteSuccess);
873 ========================================================================
885 ========================================================================
887 VOID eFuseWritePhysical(
888 IN PRTMP_ADAPTER pAd,
895 USHORT* pInBuf = (USHORT*)lpInBuffer;
897 //USHORT* pOutBuf = (USHORT*)ioBuffer;
899 USHORT Offset = pInBuf[0]; //addr
900 USHORT Length = pInBuf[1]; //length
901 USHORT* pValueX = &pInBuf[2]; //value ...
902 // Little-endian S | S Big-endian
903 // addr 3 2 1 0 | 0 1 2 3
904 // Ori-V D C B A | A B C D
907 //Both the little and big-endian use the same sequence to write data.
908 //Therefore, we only need swap data when read the data.
909 for(i=0; i<Length; i+=2)
911 eFusePhysicalWriteRegisters(pAd, Offset+i, 2, &pValueX[i/2]);
917 ========================================================================
929 ========================================================================
932 IN PRTMP_ADAPTER pAd,
939 USHORT* pValueX = (PUSHORT) pData; //value ...
940 //The input value=3070 will be stored as following
941 // Little-endian S | S Big-endian
943 // Ori-V 30 70 | 30 70
948 //The swapping should be removed for big-endian
949 for(i=0; i<length; i+=2)
951 eFuseWriteRegisters(pAd, Offset+i, 2, &pValueX[i/2]);
958 ========================================================================
970 ========================================================================
972 INT set_eFuseGetFreeBlockCount_Proc(
973 IN PRTMP_ADAPTER pAd,
977 USHORT LogicalAddress;
978 USHORT efusefreenum=0;
981 for (i = EFUSE_USAGE_MAP_START; i <= EFUSE_USAGE_MAP_END; i+=2)
983 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
984 if( (LogicalAddress & 0xff) == 0)
986 efusefreenum= (UCHAR) (EFUSE_USAGE_MAP_END-i+1);
989 else if(( (LogicalAddress >> 8) & 0xff) == 0)
991 efusefreenum = (UCHAR) (EFUSE_USAGE_MAP_END-i);
995 if(i == EFUSE_USAGE_MAP_END)
998 printk("efuseFreeNumber is %d\n",efusefreenum);
1001 INT set_eFusedump_Proc(
1002 IN PRTMP_ADAPTER pAd,
1009 for(i =0; i<EFUSE_USAGE_MAP_END/2; i++)
1015 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
1017 printk("\nBlock %x:",i/8);
1018 printk("%04x ",InBuf[2]);
1022 INT set_eFuseLoadFromBin_Proc(
1023 IN PRTMP_ADAPTER pAd,
1031 UCHAR BinFileSize=0;
1035 BinFileSize=strlen("RT30xxEEPROM.bin");
1036 src = kmalloc(128, MEM_ALLOC_FLAG);
1037 NdisZeroMemory(src, 128);
1042 NdisMoveMemory(src, arg, strlen(arg));
1048 NdisMoveMemory(src, "RT30xxEEPROM.bin", BinFileSize);
1051 DBGPRINT(RT_DEBUG_TRACE, ("FileName=%s\n",src));
1052 buffer = kmalloc(MAX_EEPROM_BIN_FILE_SIZE, MEM_ALLOC_FLAG);
1059 PDATA=kmalloc(sizeof(USHORT)*8,MEM_ALLOC_FLAG);
1074 srcf = filp_open(src, O_RDONLY, 0);
1077 DBGPRINT(RT_DEBUG_ERROR, ("--> Error %ld opening %s\n", -PTR_ERR(srcf),src));
1082 // The object must have a read method
1083 if (srcf->f_op && srcf->f_op->read)
1085 memset(buffer, 0x00, MAX_EEPROM_BIN_FILE_SIZE);
1086 while(srcf->f_op->read(srcf, &buffer[i], 1, &srcf->f_pos)==1)
1088 DBGPRINT(RT_DEBUG_TRACE, ("%02X ",buffer[i]));
1090 DBGPRINT(RT_DEBUG_TRACE, ("\n"));
1092 if(i>=MAX_EEPROM_BIN_FILE_SIZE)
1094 DBGPRINT(RT_DEBUG_ERROR, ("--> Error %ld reading %s, The file is too large[1024]\n", -PTR_ERR(srcf),src));
1104 DBGPRINT(RT_DEBUG_ERROR, ("--> Error!! System doest not support read function\n"));
1116 DBGPRINT(RT_DEBUG_ERROR, ("--> Error src or srcf is null\n"));
1124 retval=filp_close(srcf,NULL);
1128 DBGPRINT(RT_DEBUG_TRACE, ("--> Error %d closing %s\n", -retval, src));
1134 DBGPRINT(RT_DEBUG_TRACE, ("%02X ",buffer[j]));
1136 PDATA[j/2%8]=((buffer[j]<<8)&0xff00)|(buffer[j-1]&0xff);
1147 DBGPRINT(RT_DEBUG_TRACE, (" result=%02X,blk=%02x\n",k,j/16));
1150 eFuseWriteRegistersFromBin(pAd,(USHORT)j-15, 16, PDATA);
1153 if(eFuseReadRegisters(pAd,j, 2,(PUSHORT)&DATA)!=0x3f)
1154 eFuseWriteRegistersFromBin(pAd,(USHORT)j-15, 16, PDATA);
1158 printk("%04x ",PDATA[l]);
1161 NdisZeroMemory(PDATA,16);
1176 NTSTATUS eFuseWriteRegistersFromBin(
1177 IN PRTMP_ADAPTER pAd,
1184 USHORT LogicalAddress, BlkNum = 0xffff;
1185 UCHAR EFSROM_AOUT,Loop=0;
1186 EFUSE_CTRL_STRUC eFuseCtrlStruc;
1187 USHORT efuseDataOffset;
1188 UINT32 data,tempbuffer;
1189 USHORT addr,tmpaddr, InBuf[3], tmpOffset;
1191 BOOLEAN bWriteSuccess = TRUE;
1192 BOOLEAN bNotWrite=TRUE;
1193 BOOLEAN bAllocateNewBlk=TRUE;
1195 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin Offset=%x, pData=%04x:%04x:%04x:%04x\n", Offset, *pData,*(pData+1),*(pData+2),*(pData+3)));
1199 //Step 0. find the entry in the mapping table
1200 //The address of EEPROM is 2-bytes alignment.
1201 //The last bit is used for alignment, so it must be 0.
1203 tmpOffset = Offset & 0xfffe;
1204 EFSROM_AOUT = eFuseReadRegisters(pAd, tmpOffset, 2, &eFuseData);
1206 if( EFSROM_AOUT == 0x3f)
1207 { //find available logical address pointer
1208 //the logical address does not exist, find an empty one
1209 //from the first address of block 45=16*45=0x2d0 to the last address of block 47
1210 //==>48*16-3(reserved)=2FC
1211 bAllocateNewBlk=TRUE;
1212 for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
1214 //Retrive the logical block nubmer form each logical address pointer
1215 //It will access two logical address pointer each time.
1216 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
1217 if( (LogicalAddress & 0xff) == 0)
1218 {//Not used logical address pointer
1219 BlkNum = i-EFUSE_USAGE_MAP_START;
1222 else if(( (LogicalAddress >> 8) & 0xff) == 0)
1223 {//Not used logical address pointer
1224 if (i != EFUSE_USAGE_MAP_END)
1226 BlkNum = i-EFUSE_USAGE_MAP_START+1;
1234 bAllocateNewBlk=FALSE;
1235 BlkNum = EFSROM_AOUT;
1238 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters BlkNum = %d \n", BlkNum));
1240 if(BlkNum == 0xffff)
1242 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters: out of free E-fuse space!!!\n"));
1246 //If the block is not existing in mapping table, create one
1247 //and write down the 16-bytes data to the new block
1250 DBGPRINT(RT_DEBUG_TRACE, ("Allocate New Blk\n"));
1251 efuseDataOffset = EFUSE_DATA3;
1254 DBGPRINT(RT_DEBUG_TRACE, ("Allocate New Blk, Data%d=%04x%04x\n",3-i,pData[2*i+1],pData[2*i]));
1255 tempbuffer=((pData[2*i+1]<<16)&0xffff0000)|pData[2*i];
1258 RTMP_IO_WRITE32(pAd, efuseDataOffset,tempbuffer);
1259 efuseDataOffset -= 4;
1262 /////////////////////////////////////////////////////////////////
1264 //Step1.1.1. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
1265 eFuseCtrlStruc.field.EFSROM_AIN = BlkNum* 0x10 ;
1267 //Step1.1.2. Write EFSROM_MODE (0x580, bit7:bit6) to 3.
1268 eFuseCtrlStruc.field.EFSROM_MODE = 3;
1270 //Step1.1.3. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical write procedure.
1271 eFuseCtrlStruc.field.EFSROM_KICK = 1;
1273 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
1275 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
1277 //Step1.1.4. Polling EFSROM_KICK(0x580, bit30) until it become 0 again. It��s done.
1281 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
1283 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
1293 //If the same logical number is existing, check if the writting data and the data
1294 //saving in this block are the same.
1295 /////////////////////////////////////////////////////////////////
1296 //read current values of 16-byte block
1297 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
1299 //Step1.2.0. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
1300 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
1302 //Step1.2.1. Write EFSROM_MODE (0x580, bit7:bit6) to 1.
1303 eFuseCtrlStruc.field.EFSROM_MODE = 0;
1305 //Step1.2.2. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical read procedure.
1306 eFuseCtrlStruc.field.EFSROM_KICK = 1;
1308 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
1309 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
1311 //Step1.2.3. Polling EFSROM_KICK(0x580, bit30) until it become 0 again.
1315 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
1317 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
1323 //Step1.2.4. Read 16-byte of data from EFUSE_DATA0-3 (0x59C-0x590)
1324 efuseDataOffset = EFUSE_DATA3;
1327 RTMP_IO_READ32(pAd, efuseDataOffset, (PUINT32) &buffer[i]);
1328 efuseDataOffset -= 4;
1330 //Step1.2.5. Check if the data of efuse and the writing data are the same.
1333 tempbuffer=((pData[2*i+1]<<16)&0xffff0000)|pData[2*i];
1334 DBGPRINT(RT_DEBUG_TRACE, ("buffer[%d]=%x,pData[%d]=%x,pData[%d]=%x,tempbuffer=%x\n",i,buffer[i],2*i,pData[2*i],2*i+1,pData[2*i+1],tempbuffer));
1336 if(((buffer[i]&0xffff0000)==(pData[2*i+1]<<16))&&((buffer[i]&0xffff)==pData[2*i]))
1346 printk("The data is not the same\n");
1350 addr = BlkNum * 0x10 ;
1352 InBuf[0] = addr+2*i;
1354 InBuf[2] = pData[i];
1356 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 2);
1366 //Step 2. Write mapping table
1367 addr = EFUSE_USAGE_MAP_START+BlkNum;
1376 //convert the address from 10 to 8 bit ( bit7, 6 = parity and bit5 ~ 0 = bit9~4), and write to logical map entry
1379 tmpOffset |= ((~((tmpOffset & 0x01) ^ ( tmpOffset >> 1 & 0x01) ^ (tmpOffset >> 2 & 0x01) ^ (tmpOffset >> 3 & 0x01))) << 6) & 0x40;
1380 tmpOffset |= ((~( (tmpOffset >> 2 & 0x01) ^ (tmpOffset >> 3 & 0x01) ^ (tmpOffset >> 4 & 0x01) ^ ( tmpOffset >> 5 & 0x01))) << 7) & 0x80;
1382 // write the logical address
1384 InBuf[2] = tmpOffset<<8;
1386 InBuf[2] = tmpOffset;
1388 eFuseWritePhysical(pAd,&InBuf[0], 6, NULL, 0);
1390 //Step 3. Compare data if not the same, invalidate the mapping entry, then re-write the data until E-fuse is exhausted
1391 bWriteSuccess = TRUE;
1394 addr = BlkNum * 0x10 ;
1396 InBuf[0] = addr+2*i;
1400 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
1401 DBGPRINT(RT_DEBUG_TRACE, ("addr=%x, buffer[i]=%x,InBuf[2]=%x\n",InBuf[0],pData[i],InBuf[2]));
1402 if(pData[i] != InBuf[2])
1404 bWriteSuccess = FALSE;
1409 //Step 4. invlidate mapping entry and find a free mapping entry if not succeed
1411 if (!bWriteSuccess&&Loop<2)
1413 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin::Not bWriteSuccess BlkNum = %d\n", BlkNum));
1415 // the offset of current mapping entry
1416 addr = EFUSE_USAGE_MAP_START+BlkNum;
1418 //find a new mapping entry
1420 for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
1422 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
1423 if( (LogicalAddress & 0xff) == 0)
1425 BlkNum = i-EFUSE_USAGE_MAP_START;
1428 else if(( (LogicalAddress >> 8) & 0xff) == 0)
1430 if (i != EFUSE_USAGE_MAP_END)
1432 BlkNum = i+1-EFUSE_USAGE_MAP_START;
1437 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin::Not bWriteSuccess new BlkNum = %d\n", BlkNum));
1438 if(BlkNum == 0xffff)
1440 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin: out of free E-fuse space!!!\n"));
1444 //invalidate the original mapping entry if new entry is not found
1452 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
1454 // write the logical address
1457 // Invalidate the high byte
1458 for (i=8; i<15; i++)
1460 if( ( (InBuf[2] >> i) & 0x01) == 0)
1462 InBuf[2] |= (0x1 <<i);
1469 // invalidate the low byte
1472 if( ( (InBuf[2] >> i) & 0x01) == 0)
1474 InBuf[2] |= (0x1 <<i);
1479 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 0);
1483 while(!bWriteSuccess&&Loop<2);