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);
76 LowerClock(pAd, &x); //prevent read failed
86 // IRQL = PASSIVE_LEVEL
94 mask = 0x01 << (count - 1);
95 RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
102 if(data & mask) x |= EEDI;
104 RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
113 RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
116 // IRQL = PASSIVE_LEVEL
118 IN PRTMP_ADAPTER pAd)
122 RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
125 RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
132 IN PRTMP_ADAPTER pAd)
136 // reset bits and set EECS
137 RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
138 x &= ~(EEDI | EEDO | EESK);
140 RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
146 // output the read_opcode and six pulse in that order
147 ShiftOutBits(pAd, EEPROM_EWEN_OPCODE, 5);
148 ShiftOutBits(pAd, 0, 6);
154 IN PRTMP_ADAPTER pAd)
158 // reset bits and set EECS
159 RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
160 x &= ~(EEDI | EEDO | EESK);
162 RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
168 // output the read_opcode and six pulse in that order
169 ShiftOutBits(pAd, EEPROM_EWDS_OPCODE, 5);
170 ShiftOutBits(pAd, 0, 6);
175 // IRQL = PASSIVE_LEVEL
176 USHORT RTMP_EEPROM_READ16(
177 IN PRTMP_ADAPTER pAd,
183 if (pAd->NicConfig2.field.AntDiversity)
185 pAd->EepromAccess = TRUE;
187 //2008/09/11:KH add to support efuse<--
188 //2008/09/11:KH add to support efuse-->
191 // reset bits and set EECS
192 RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
193 x &= ~(EEDI | EEDO | EESK);
195 RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
197 // patch can not access e-Fuse issue
205 // output the read_opcode and register number in that order
206 ShiftOutBits(pAd, EEPROM_READ_OPCODE, 3);
207 ShiftOutBits(pAd, Offset, pAd->EEPROMAddressNum);
209 // Now read the data (16 bits) in from the selected EEPROM word
210 data = ShiftInBits(pAd);
214 // Antenna and EEPROM access are both using EESK pin,
215 // Therefor we should avoid accessing EESK at the same time
216 // Then restore antenna after EEPROM access
217 if ((pAd->NicConfig2.field.AntDiversity) || (pAd->RfIcType == RFIC_3020))
219 pAd->EepromAccess = FALSE;
220 AsicSetRxAnt(pAd, pAd->RxAnt.Pair1PrimaryRxAnt);
226 VOID RTMP_EEPROM_WRITE16(
227 IN PRTMP_ADAPTER pAd,
233 if (pAd->NicConfig2.field.AntDiversity)
235 pAd->EepromAccess = TRUE;
237 //2008/09/11:KH add to support efuse<--
238 //2008/09/11:KH add to support efuse-->
244 // reset bits and set EECS
245 RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
246 x &= ~(EEDI | EEDO | EESK);
248 RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
250 // patch can not access e-Fuse issue
258 // output the read_opcode ,register number and data in that order
259 ShiftOutBits(pAd, EEPROM_WRITE_OPCODE, 3);
260 ShiftOutBits(pAd, Offset, pAd->EEPROMAddressNum);
261 ShiftOutBits(pAd, Data, 16); // 16-bit access
264 RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
268 RTMPusecDelay(10000); //delay for twp(MAX)=10ms
274 // Antenna and EEPROM access are both using EESK pin,
275 // Therefor we should avoid accessing EESK at the same time
276 // Then restore antenna after EEPROM access
277 if ((pAd->NicConfig2.field.AntDiversity) || (pAd->RfIcType == RFIC_3020))
279 pAd->EepromAccess = FALSE;
280 AsicSetRxAnt(pAd, pAd->RxAnt.Pair1PrimaryRxAnt);
285 //2008/09/11:KH add to support efuse<--
288 ========================================================================
300 ========================================================================
302 UCHAR eFuseReadRegisters(
303 IN PRTMP_ADAPTER pAd,
308 EFUSE_CTRL_STRUC eFuseCtrlStruc;
310 USHORT efuseDataOffset;
313 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
315 //Step0. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
316 //Use the eeprom logical address and covert to address to block number
317 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
319 //Step1. Write EFSROM_MODE (0x580, bit7:bit6) to 0.
320 eFuseCtrlStruc.field.EFSROM_MODE = 0;
322 //Step2. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical read procedure.
323 eFuseCtrlStruc.field.EFSROM_KICK = 1;
325 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
326 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
328 //Step3. Polling EFSROM_KICK(0x580, bit30) until it become 0 again.
332 //rtmp.HwMemoryReadDword(EFUSE_CTRL, (DWORD *) &eFuseCtrlStruc, 4);
333 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
334 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
342 //if EFSROM_AOUT is not found in physical address, write 0xffff
343 if (eFuseCtrlStruc.field.EFSROM_AOUT == 0x3f)
345 for(i=0; i<Length/2; i++)
346 *(pData+2*i) = 0xffff;
350 //Step4. Read 16-byte of data from EFUSE_DATA0-3 (0x590-0x59C)
351 efuseDataOffset = EFUSE_DATA3 - (Offset & 0xC) ;
352 //data hold 4 bytes data.
353 //In RTMP_IO_READ32 will automatically execute 32-bytes swapping
354 RTMP_IO_READ32(pAd, efuseDataOffset, &data);
355 //Decide the upper 2 bytes or the bottom 2 bytes.
356 // Little-endian S | S Big-endian
357 // addr 3 2 1 0 | 0 1 2 3
358 // Ori-V D C B A | A B C D
362 //The return byte statrs from S. Therefore, the little-endian will return BA, the Big-endian will return DC.
363 //For returning the bottom 2 bytes, the Big-endian should shift right 2-bytes.
365 data = data << (8*((Offset & 0x3)^0x2));
367 data = data >> (8*(Offset & 0x3));
370 NdisMoveMemory(pData, &data, Length);
373 return (UCHAR) eFuseCtrlStruc.field.EFSROM_AOUT;
378 ========================================================================
390 ========================================================================
392 VOID eFusePhysicalReadRegisters(
393 IN PRTMP_ADAPTER pAd,
398 EFUSE_CTRL_STRUC eFuseCtrlStruc;
400 USHORT efuseDataOffset;
403 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
405 //Step0. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
406 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
408 //Step1. Write EFSROM_MODE (0x580, bit7:bit6) to 1.
409 //Read in physical view
410 eFuseCtrlStruc.field.EFSROM_MODE = 1;
412 //Step2. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical read procedure.
413 eFuseCtrlStruc.field.EFSROM_KICK = 1;
415 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
416 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
418 //Step3. Polling EFSROM_KICK(0x580, bit30) until it become 0 again.
422 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
423 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
429 //Step4. Read 16-byte of data from EFUSE_DATA0-3 (0x59C-0x590)
430 //Because the size of each EFUSE_DATA is 4 Bytes, the size of address of each is 2 bits.
431 //The previous 2 bits is the EFUSE_DATA number, the last 2 bits is used to decide which bytes
432 //Decide which EFUSE_DATA to read
437 efuseDataOffset = EFUSE_DATA3 - (Offset & 0xC) ;
439 RTMP_IO_READ32(pAd, efuseDataOffset, &data);
442 data = data << (8*((Offset & 0x3)^0x2));
444 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,
1028 INT retval, orgfsuid, orgfsgid;
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);
1068 /* Don't change to uid 0, let the file be opened as the "normal" user */
1070 orgfsuid = current->fsuid;
1071 orgfsgid = current->fsgid;
1072 current->fsuid=current->fsgid = 0;
1079 srcf = filp_open(src, O_RDONLY, 0);
1082 DBGPRINT(RT_DEBUG_ERROR, ("--> Error %ld opening %s\n", -PTR_ERR(srcf),src));
1087 // The object must have a read method
1088 if (srcf->f_op && srcf->f_op->read)
1090 memset(buffer, 0x00, MAX_EEPROM_BIN_FILE_SIZE);
1091 while(srcf->f_op->read(srcf, &buffer[i], 1, &srcf->f_pos)==1)
1093 DBGPRINT(RT_DEBUG_TRACE, ("%02X ",buffer[i]));
1095 DBGPRINT(RT_DEBUG_TRACE, ("\n"));
1097 if(i>=MAX_EEPROM_BIN_FILE_SIZE)
1099 DBGPRINT(RT_DEBUG_ERROR, ("--> Error %ld reading %s, The file is too large[1024]\n", -PTR_ERR(srcf),src));
1109 DBGPRINT(RT_DEBUG_ERROR, ("--> Error!! System doest not support read function\n"));
1121 DBGPRINT(RT_DEBUG_ERROR, ("--> Error src or srcf is null\n"));
1129 retval=filp_close(srcf,NULL);
1133 DBGPRINT(RT_DEBUG_TRACE, ("--> Error %d closing %s\n", -retval, src));
1137 current->fsuid = orgfsuid;
1138 current->fsgid = orgfsgid;
1142 DBGPRINT(RT_DEBUG_TRACE, ("%02X ",buffer[j]));
1144 PDATA[j/2%8]=((buffer[j]<<8)&0xff00)|(buffer[j-1]&0xff);
1155 DBGPRINT(RT_DEBUG_TRACE, (" result=%02X,blk=%02x\n",k,j/16));
1158 eFuseWriteRegistersFromBin(pAd,(USHORT)j-15, 16, PDATA);
1161 if(eFuseReadRegisters(pAd,j, 2,(PUSHORT)&DATA)!=0x3f)
1162 eFuseWriteRegistersFromBin(pAd,(USHORT)j-15, 16, PDATA);
1166 printk("%04x ",PDATA[l]);
1169 NdisZeroMemory(PDATA,16);
1184 NTSTATUS eFuseWriteRegistersFromBin(
1185 IN PRTMP_ADAPTER pAd,
1192 USHORT LogicalAddress, BlkNum = 0xffff;
1193 UCHAR EFSROM_AOUT,Loop=0;
1194 EFUSE_CTRL_STRUC eFuseCtrlStruc;
1195 USHORT efuseDataOffset;
1196 UINT32 data,tempbuffer;
1197 USHORT addr,tmpaddr, InBuf[3], tmpOffset;
1199 BOOLEAN bWriteSuccess = TRUE;
1200 BOOLEAN bNotWrite=TRUE;
1201 BOOLEAN bAllocateNewBlk=TRUE;
1203 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin Offset=%x, pData=%04x:%04x:%04x:%04x\n", Offset, *pData,*(pData+1),*(pData+2),*(pData+3)));
1207 //Step 0. find the entry in the mapping table
1208 //The address of EEPROM is 2-bytes alignment.
1209 //The last bit is used for alignment, so it must be 0.
1211 tmpOffset = Offset & 0xfffe;
1212 EFSROM_AOUT = eFuseReadRegisters(pAd, tmpOffset, 2, &eFuseData);
1214 if( EFSROM_AOUT == 0x3f)
1215 { //find available logical address pointer
1216 //the logical address does not exist, find an empty one
1217 //from the first address of block 45=16*45=0x2d0 to the last address of block 47
1218 //==>48*16-3(reserved)=2FC
1219 bAllocateNewBlk=TRUE;
1220 for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
1222 //Retrive the logical block nubmer form each logical address pointer
1223 //It will access two logical address pointer each time.
1224 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
1225 if( (LogicalAddress & 0xff) == 0)
1226 {//Not used logical address pointer
1227 BlkNum = i-EFUSE_USAGE_MAP_START;
1230 else if(( (LogicalAddress >> 8) & 0xff) == 0)
1231 {//Not used logical address pointer
1232 if (i != EFUSE_USAGE_MAP_END)
1234 BlkNum = i-EFUSE_USAGE_MAP_START+1;
1242 bAllocateNewBlk=FALSE;
1243 BlkNum = EFSROM_AOUT;
1246 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters BlkNum = %d \n", BlkNum));
1248 if(BlkNum == 0xffff)
1250 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters: out of free E-fuse space!!!\n"));
1254 //If the block is not existing in mapping table, create one
1255 //and write down the 16-bytes data to the new block
1258 DBGPRINT(RT_DEBUG_TRACE, ("Allocate New Blk\n"));
1259 efuseDataOffset = EFUSE_DATA3;
1262 DBGPRINT(RT_DEBUG_TRACE, ("Allocate New Blk, Data%d=%04x%04x\n",3-i,pData[2*i+1],pData[2*i]));
1263 tempbuffer=((pData[2*i+1]<<16)&0xffff0000)|pData[2*i];
1266 RTMP_IO_WRITE32(pAd, efuseDataOffset,tempbuffer);
1267 efuseDataOffset -= 4;
1270 /////////////////////////////////////////////////////////////////
1272 //Step1.1.1. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
1273 eFuseCtrlStruc.field.EFSROM_AIN = BlkNum* 0x10 ;
1275 //Step1.1.2. Write EFSROM_MODE (0x580, bit7:bit6) to 3.
1276 eFuseCtrlStruc.field.EFSROM_MODE = 3;
1278 //Step1.1.3. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical write procedure.
1279 eFuseCtrlStruc.field.EFSROM_KICK = 1;
1281 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
1283 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
1285 //Step1.1.4. Polling EFSROM_KICK(0x580, bit30) until it become 0 again. It¡¦s done.
1289 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
1291 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
1301 //If the same logical number is existing, check if the writting data and the data
1302 //saving in this block are the same.
1303 /////////////////////////////////////////////////////////////////
1304 //read current values of 16-byte block
1305 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
1307 //Step1.2.0. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
1308 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
1310 //Step1.2.1. Write EFSROM_MODE (0x580, bit7:bit6) to 1.
1311 eFuseCtrlStruc.field.EFSROM_MODE = 0;
1313 //Step1.2.2. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical read procedure.
1314 eFuseCtrlStruc.field.EFSROM_KICK = 1;
1316 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
1317 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
1319 //Step1.2.3. Polling EFSROM_KICK(0x580, bit30) until it become 0 again.
1323 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
1325 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
1331 //Step1.2.4. Read 16-byte of data from EFUSE_DATA0-3 (0x59C-0x590)
1332 efuseDataOffset = EFUSE_DATA3;
1335 RTMP_IO_READ32(pAd, efuseDataOffset, (PUINT32) &buffer[i]);
1336 efuseDataOffset -= 4;
1338 //Step1.2.5. Check if the data of efuse and the writing data are the same.
1341 tempbuffer=((pData[2*i+1]<<16)&0xffff0000)|pData[2*i];
1342 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));
1344 if(((buffer[i]&0xffff0000)==(pData[2*i+1]<<16))&&((buffer[i]&0xffff)==pData[2*i]))
1354 printk("The data is not the same\n");
1358 addr = BlkNum * 0x10 ;
1360 InBuf[0] = addr+2*i;
1362 InBuf[2] = pData[i];
1364 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 2);
1374 //Step 2. Write mapping table
1375 addr = EFUSE_USAGE_MAP_START+BlkNum;
1384 //convert the address from 10 to 8 bit ( bit7, 6 = parity and bit5 ~ 0 = bit9~4), and write to logical map entry
1387 tmpOffset |= ((~((tmpOffset & 0x01) ^ ( tmpOffset >> 1 & 0x01) ^ (tmpOffset >> 2 & 0x01) ^ (tmpOffset >> 3 & 0x01))) << 6) & 0x40;
1388 tmpOffset |= ((~( (tmpOffset >> 2 & 0x01) ^ (tmpOffset >> 3 & 0x01) ^ (tmpOffset >> 4 & 0x01) ^ ( tmpOffset >> 5 & 0x01))) << 7) & 0x80;
1390 // write the logical address
1392 InBuf[2] = tmpOffset<<8;
1394 InBuf[2] = tmpOffset;
1396 eFuseWritePhysical(pAd,&InBuf[0], 6, NULL, 0);
1398 //Step 3. Compare data if not the same, invalidate the mapping entry, then re-write the data until E-fuse is exhausted
1399 bWriteSuccess = TRUE;
1402 addr = BlkNum * 0x10 ;
1404 InBuf[0] = addr+2*i;
1408 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
1409 DBGPRINT(RT_DEBUG_TRACE, ("addr=%x, buffer[i]=%x,InBuf[2]=%x\n",InBuf[0],pData[i],InBuf[2]));
1410 if(pData[i] != InBuf[2])
1412 bWriteSuccess = FALSE;
1417 //Step 4. invlidate mapping entry and find a free mapping entry if not succeed
1419 if (!bWriteSuccess&&Loop<2)
1421 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin::Not bWriteSuccess BlkNum = %d\n", BlkNum));
1423 // the offset of current mapping entry
1424 addr = EFUSE_USAGE_MAP_START+BlkNum;
1426 //find a new mapping entry
1428 for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
1430 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
1431 if( (LogicalAddress & 0xff) == 0)
1433 BlkNum = i-EFUSE_USAGE_MAP_START;
1436 else if(( (LogicalAddress >> 8) & 0xff) == 0)
1438 if (i != EFUSE_USAGE_MAP_END)
1440 BlkNum = i+1-EFUSE_USAGE_MAP_START;
1445 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin::Not bWriteSuccess new BlkNum = %d\n", BlkNum));
1446 if(BlkNum == 0xffff)
1448 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin: out of free E-fuse space!!!\n"));
1452 //invalidate the original mapping entry if new entry is not found
1460 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
1462 // write the logical address
1465 // Invalidate the high byte
1466 for (i=8; i<15; i++)
1468 if( ( (InBuf[2] >> i) & 0x01) == 0)
1470 InBuf[2] |= (0x1 <<i);
1477 // invalidate the low byte
1480 if( ( (InBuf[2] >> i) & 0x01) == 0)
1482 InBuf[2] |= (0x1 <<i);
1487 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 0);
1491 while(!bWriteSuccess&&Loop<2);
1497 //2008/09/11:KH add to support efuse-->