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 *************************************************************************
31 Miniport generic portion header file
35 -------- ---------- ----------------------------------------------
38 #include "../rt_config.h"
41 #define EFUSE_USAGE_MAP_START 0x2d0
42 #define EFUSE_USAGE_MAP_END 0x2fc
43 #define EFUSE_USAGE_MAP_SIZE 45
47 #define EFUSE_EEPROM_DEFULT_FILE "RT30xxEEPROM.bin"
48 #define MAX_EEPROM_BIN_FILE_SIZE 1024
52 #define EFUSE_TAG 0x2fe
56 typedef union _EFUSE_CTRL_STRUC {
62 UINT32 EFSROM_LDO_ON_TIME:2;
63 UINT32 EFSROM_LDO_OFF_TIME:6;
68 } EFUSE_CTRL_STRUC, *PEFUSE_CTRL_STRUC;
70 typedef union _EFUSE_CTRL_STRUC {
74 UINT32 EFSROM_LDO_OFF_TIME:6;
75 UINT32 EFSROM_LDO_ON_TIME:2;
82 } EFUSE_CTRL_STRUC, *PEFUSE_CTRL_STRUC;
83 #endif // RT_BIG_ENDIAN //
85 static UCHAR eFuseReadRegisters(
91 static VOID eFuseReadPhysical(
93 IN PUSHORT lpInBuffer,
94 IN ULONG nInBufferSize,
95 OUT PUSHORT lpOutBuffer,
96 IN ULONG nOutBufferSize);
98 static VOID eFusePhysicalWriteRegisters(
104 static NTSTATUS eFuseWriteRegisters(
105 IN PRTMP_ADAPTER pAd,
110 static VOID eFuseWritePhysical(
111 IN PRTMP_ADAPTER pAd,
115 ULONG nOutBufferSize);
118 static NTSTATUS eFuseWriteRegistersFromBin(
119 IN PRTMP_ADAPTER pAd,
126 ========================================================================
136 ========================================================================
138 UCHAR eFuseReadRegisters(
139 IN PRTMP_ADAPTER pAd,
144 EFUSE_CTRL_STRUC eFuseCtrlStruc;
146 USHORT efuseDataOffset;
149 RTMP_IO_READ32(pAd, EFUSE_CTRL, &eFuseCtrlStruc.word);
151 //Step0. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
152 //Use the eeprom logical address and covert to address to block number
153 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
155 //Step1. Write EFSROM_MODE (0x580, bit7:bit6) to 0.
156 eFuseCtrlStruc.field.EFSROM_MODE = 0;
158 //Step2. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical read procedure.
159 eFuseCtrlStruc.field.EFSROM_KICK = 1;
161 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
162 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
164 //Step3. Polling EFSROM_KICK(0x580, bit30) until it become 0 again.
168 //rtmp.HwMemoryReadDword(EFUSE_CTRL, (DWORD *) &eFuseCtrlStruc, 4);
169 RTMP_IO_READ32(pAd, EFUSE_CTRL, &eFuseCtrlStruc.word);
170 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
178 //if EFSROM_AOUT is not found in physical address, write 0xffff
179 if (eFuseCtrlStruc.field.EFSROM_AOUT == 0x3f)
181 for(i=0; i<Length/2; i++)
182 *(pData+2*i) = 0xffff;
186 //Step4. Read 16-byte of data from EFUSE_DATA0-3 (0x590-0x59C)
187 efuseDataOffset = EFUSE_DATA3 - (Offset & 0xC);
188 //data hold 4 bytes data.
189 //In RTMP_IO_READ32 will automatically execute 32-bytes swapping
190 RTMP_IO_READ32(pAd, efuseDataOffset, &data);
191 //Decide the upper 2 bytes or the bottom 2 bytes.
192 // Little-endian S | S Big-endian
193 // addr 3 2 1 0 | 0 1 2 3
194 // Ori-V D C B A | A B C D
198 //The return byte statrs from S. Therefore, the little-endian will return BA, the Big-endian will return DC.
199 //For returning the bottom 2 bytes, the Big-endian should shift right 2-bytes.
201 data = data << (8*((Offset & 0x3)^0x2));
203 data = data >> (8*(Offset & 0x3));
204 #endif // RT_BIG_ENDIAN //
206 NdisMoveMemory(pData, &data, Length);
209 return (UCHAR) eFuseCtrlStruc.field.EFSROM_AOUT;
214 ========================================================================
224 ========================================================================
226 VOID eFusePhysicalReadRegisters(
227 IN PRTMP_ADAPTER pAd,
232 EFUSE_CTRL_STRUC eFuseCtrlStruc;
234 USHORT efuseDataOffset;
237 RTMP_IO_READ32(pAd, EFUSE_CTRL, &eFuseCtrlStruc.word);
239 //Step0. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
240 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
242 //Step1. Write EFSROM_MODE (0x580, bit7:bit6) to 1.
243 //Read in physical view
244 eFuseCtrlStruc.field.EFSROM_MODE = 1;
246 //Step2. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical read procedure.
247 eFuseCtrlStruc.field.EFSROM_KICK = 1;
249 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
250 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
252 //Step3. Polling EFSROM_KICK(0x580, bit30) until it become 0 again.
256 RTMP_IO_READ32(pAd, EFUSE_CTRL, &eFuseCtrlStruc.word);
257 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
263 //Step4. Read 16-byte of data from EFUSE_DATA0-3 (0x59C-0x590)
264 //Because the size of each EFUSE_DATA is 4 Bytes, the size of address of each is 2 bits.
265 //The previous 2 bits is the EFUSE_DATA number, the last 2 bits is used to decide which bytes
266 //Decide which EFUSE_DATA to read
271 efuseDataOffset = EFUSE_DATA3 - (Offset & 0xC) ;
273 RTMP_IO_READ32(pAd, efuseDataOffset, &data);
276 data = data << (8*((Offset & 0x3)^0x2));
278 data = data >> (8*(Offset & 0x3));
279 #endif // RT_BIG_ENDIAN //
281 NdisMoveMemory(pData, &data, Length);
286 ========================================================================
296 ========================================================================
298 static VOID eFuseReadPhysical(
299 IN PRTMP_ADAPTER pAd,
300 IN PUSHORT lpInBuffer,
301 IN ULONG nInBufferSize,
302 OUT PUSHORT lpOutBuffer,
303 IN ULONG nOutBufferSize
306 USHORT* pInBuf = (USHORT*)lpInBuffer;
307 USHORT* pOutBuf = (USHORT*)lpOutBuffer;
309 USHORT Offset = pInBuf[0]; //addr
310 USHORT Length = pInBuf[1]; //length
313 for(i=0; i<Length; i+=2)
315 eFusePhysicalReadRegisters(pAd,Offset+i, 2, &pOutBuf[i/2]);
320 ========================================================================
330 ========================================================================
333 IN PRTMP_ADAPTER pAd,
338 USHORT* pOutBuf = (USHORT*)pData;
339 NTSTATUS Status = STATUS_SUCCESS;
343 for(i=0; i<Length; i+=2)
345 EFSROM_AOUT = eFuseReadRegisters(pAd, Offset+i, 2, &pOutBuf[i/2]);
351 ========================================================================
361 ========================================================================
363 static VOID eFusePhysicalWriteRegisters(
364 IN PRTMP_ADAPTER pAd,
369 EFUSE_CTRL_STRUC eFuseCtrlStruc;
371 USHORT efuseDataOffset;
372 UINT32 data, eFuseDataBuffer[4];
374 //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.
376 /////////////////////////////////////////////////////////////////
377 //read current values of 16-byte block
378 RTMP_IO_READ32(pAd, EFUSE_CTRL, &eFuseCtrlStruc.word);
380 //Step0. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
381 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
383 //Step1. Write EFSROM_MODE (0x580, bit7:bit6) to 1.
384 eFuseCtrlStruc.field.EFSROM_MODE = 1;
386 //Step2. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical read procedure.
387 eFuseCtrlStruc.field.EFSROM_KICK = 1;
389 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
390 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
392 //Step3. Polling EFSROM_KICK(0x580, bit30) until it become 0 again.
396 RTMP_IO_READ32(pAd, EFUSE_CTRL, &eFuseCtrlStruc.word);
398 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
404 //Step4. Read 16-byte of data from EFUSE_DATA0-3 (0x59C-0x590)
405 efuseDataOffset = EFUSE_DATA3;
408 RTMP_IO_READ32(pAd, efuseDataOffset, (PUINT32) &eFuseDataBuffer[i]);
409 efuseDataOffset -= 4;
412 //Update the value, the offset is multiple of 2, length is 2
413 efuseDataOffset = (Offset & 0xc) >> 2;
414 data = pData[0] & 0xffff;
415 //The offset should be 0x***10 or 0x***00
416 if((Offset % 4) != 0)
418 eFuseDataBuffer[efuseDataOffset] = (eFuseDataBuffer[efuseDataOffset] & 0xffff) | (data << 16);
422 eFuseDataBuffer[efuseDataOffset] = (eFuseDataBuffer[efuseDataOffset] & 0xffff0000) | data;
425 efuseDataOffset = EFUSE_DATA3;
428 RTMP_IO_WRITE32(pAd, efuseDataOffset, eFuseDataBuffer[i]);
429 efuseDataOffset -= 4;
431 /////////////////////////////////////////////////////////////////
433 //Step1. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
435 RTMP_IO_READ32(pAd, EFUSE_CTRL, &eFuseCtrlStruc.word);
437 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
439 //Step2. Write EFSROM_MODE (0x580, bit7:bit6) to 3.
440 eFuseCtrlStruc.field.EFSROM_MODE = 3;
442 //Step3. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical write procedure.
443 eFuseCtrlStruc.field.EFSROM_KICK = 1;
445 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
446 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
448 //Step4. Polling EFSROM_KICK(0x580, bit30) until it become 0 again. It��s done.
453 RTMP_IO_READ32(pAd, EFUSE_CTRL, &eFuseCtrlStruc.word);
455 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
464 ========================================================================
474 ========================================================================
476 static NTSTATUS eFuseWriteRegisters(
477 IN PRTMP_ADAPTER pAd,
484 USHORT LogicalAddress, BlkNum = 0xffff;
487 USHORT addr,tmpaddr, InBuf[3], tmpOffset;
489 BOOLEAN bWriteSuccess = TRUE;
491 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters Offset=%x, pData=%x\n", Offset, *pData));
493 //Step 0. find the entry in the mapping table
494 //The address of EEPROM is 2-bytes alignment.
495 //The last bit is used for alignment, so it must be 0.
496 tmpOffset = Offset & 0xfffe;
497 EFSROM_AOUT = eFuseReadRegisters(pAd, tmpOffset, 2, &eFuseData);
499 if( EFSROM_AOUT == 0x3f)
500 { //find available logical address pointer
501 //the logical address does not exist, find an empty one
502 //from the first address of block 45=16*45=0x2d0 to the last address of block 47
503 //==>48*16-3(reserved)=2FC
504 for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
506 //Retrive the logical block nubmer form each logical address pointer
507 //It will access two logical address pointer each time.
508 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
509 if( (LogicalAddress & 0xff) == 0)
510 {//Not used logical address pointer
511 BlkNum = i-EFUSE_USAGE_MAP_START;
514 else if(( (LogicalAddress >> 8) & 0xff) == 0)
515 {//Not used logical address pointer
516 if (i != EFUSE_USAGE_MAP_END)
518 BlkNum = i-EFUSE_USAGE_MAP_START+1;
526 BlkNum = EFSROM_AOUT;
529 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters BlkNum = %d \n", BlkNum));
533 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters: out of free E-fuse space!!!\n"));
537 //Step 1. Save data of this block which is pointed by the avaible logical address pointer
538 // read and save the original block data
541 addr = BlkNum * 0x10 ;
547 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
549 buffer[i] = InBuf[2];
552 //Step 2. Update the data in buffer, and write the data to Efuse
553 buffer[ (Offset >> 1) % 8] = pData[0];
557 //Step 3. Write the data to Efuse
562 addr = BlkNum * 0x10 ;
566 InBuf[2] = buffer[i];
568 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 2);
573 addr = BlkNum * 0x10 ;
575 InBuf[0] = addr+(Offset % 16);
579 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 2);
582 //Step 4. Write mapping table
583 addr = EFUSE_USAGE_MAP_START+BlkNum;
592 //convert the address from 10 to 8 bit ( bit7, 6 = parity and bit5 ~ 0 = bit9~4), and write to logical map entry
595 tmpOffset |= ((~((tmpOffset & 0x01) ^ ( tmpOffset >> 1 & 0x01) ^ (tmpOffset >> 2 & 0x01) ^ (tmpOffset >> 3 & 0x01))) << 6) & 0x40;
596 tmpOffset |= ((~( (tmpOffset >> 2 & 0x01) ^ (tmpOffset >> 3 & 0x01) ^ (tmpOffset >> 4 & 0x01) ^ ( tmpOffset >> 5 & 0x01))) << 7) & 0x80;
598 // write the logical address
600 InBuf[2] = tmpOffset<<8;
602 InBuf[2] = tmpOffset;
604 eFuseWritePhysical(pAd,&InBuf[0], 6, NULL, 0);
606 //Step 5. Compare data if not the same, invalidate the mapping entry, then re-write the data until E-fuse is exhausted
607 bWriteSuccess = TRUE;
610 addr = BlkNum * 0x10 ;
616 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
618 if(buffer[i] != InBuf[2])
620 bWriteSuccess = FALSE;
625 //Step 6. invlidate mapping entry and find a free mapping entry if not succeed
628 DBGPRINT(RT_DEBUG_TRACE, ("Not bWriteSuccess BlkNum = %d\n", BlkNum));
630 // the offset of current mapping entry
631 addr = EFUSE_USAGE_MAP_START+BlkNum;
633 //find a new mapping entry
635 for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
637 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
638 if( (LogicalAddress & 0xff) == 0)
640 BlkNum = i-EFUSE_USAGE_MAP_START;
643 else if(( (LogicalAddress >> 8) & 0xff) == 0)
645 if (i != EFUSE_USAGE_MAP_END)
647 BlkNum = i+1-EFUSE_USAGE_MAP_START;
652 DBGPRINT(RT_DEBUG_TRACE, ("Not bWriteSuccess new BlkNum = %d\n", BlkNum));
655 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters: out of free E-fuse space!!!\n"));
659 //invalidate the original mapping entry if new entry is not found
667 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
669 // write the logical address
672 // Invalidate the high byte
675 if( ( (InBuf[2] >> i) & 0x01) == 0)
677 InBuf[2] |= (0x1 <<i);
684 // invalidate the low byte
687 if( ( (InBuf[2] >> i) & 0x01) == 0)
689 InBuf[2] |= (0x1 <<i);
694 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 0);
697 while (!bWriteSuccess&&Loop<2);
699 DBGPRINT(RT_DEBUG_ERROR,("Efsue Write Failed!!\n"));
705 ========================================================================
715 ========================================================================
717 static VOID eFuseWritePhysical(
718 IN PRTMP_ADAPTER pAd,
725 USHORT* pInBuf = (USHORT*)lpInBuffer;
727 //USHORT* pOutBuf = (USHORT*)ioBuffer;
728 USHORT Offset = pInBuf[0]; // addr
729 USHORT Length = pInBuf[1]; // length
730 USHORT* pValueX = &pInBuf[2]; // value ...
732 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWritePhysical Offset=0x%x, length=%d\n", Offset, Length));
735 // Little-endian S | S Big-endian
736 // addr 3 2 1 0 | 0 1 2 3
737 // Ori-V D C B A | A B C D
740 // Both the little and big-endian use the same sequence to write data.
741 // Therefore, we only need swap data when read the data.
742 for (i=0; i<Length; i+=2)
744 eFusePhysicalWriteRegisters(pAd, Offset+i, 2, &pValueX[i/2]);
751 ========================================================================
761 ========================================================================
764 IN PRTMP_ADAPTER pAd,
770 USHORT* pValueX = (PUSHORT) pData; //value ...
772 // The input value=3070 will be stored as following
773 // Little-endian S | S Big-endian
775 // Ori-V 30 70 | 30 70
780 // The swapping should be removed for big-endian
781 for(i=0; i<length; i+=2)
783 eFuseWriteRegisters(pAd, Offset+i, 2, &pValueX[i/2]);
793 ========================================================================
803 ========================================================================
805 INT set_eFuseGetFreeBlockCount_Proc(
806 IN PRTMP_ADAPTER pAd,
810 USHORT LogicalAddress;
811 USHORT efusefreenum=0;
814 for (i = EFUSE_USAGE_MAP_START; i <= EFUSE_USAGE_MAP_END; i+=2)
816 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
817 if( (LogicalAddress & 0xff) == 0)
819 efusefreenum= (UCHAR) (EFUSE_USAGE_MAP_END-i+1);
822 else if(( (LogicalAddress >> 8) & 0xff) == 0)
824 efusefreenum = (UCHAR) (EFUSE_USAGE_MAP_END-i);
828 if(i == EFUSE_USAGE_MAP_END)
831 printk("efuseFreeNumber is %d\n",efusefreenum);
836 INT set_eFusedump_Proc(
837 IN PRTMP_ADAPTER pAd,
844 for(i =0; i<EFUSE_USAGE_MAP_END/2; i++)
850 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
852 printk("\nBlock %x:",i/8);
853 printk("%04x ",InBuf[2]);
859 INT set_eFuseLoadFromBin_Proc(
860 IN PRTMP_ADAPTER pAd,
865 RTMP_OS_FS_INFO osfsInfo;
867 PSTRING buffer, memPtr;
872 memSize = 128 + MAX_EEPROM_BIN_FILE_SIZE + sizeof(USHORT) * 8;
873 memPtr = kmalloc(memSize, MEM_ALLOC_FLAG);
877 NdisZeroMemory(memPtr, memSize);
878 src = memPtr; // kmalloc(128, MEM_ALLOC_FLAG);
879 buffer = src + 128; // kmalloc(MAX_EEPROM_BIN_FILE_SIZE, MEM_ALLOC_FLAG);
880 PDATA = (USHORT*)(buffer + MAX_EEPROM_BIN_FILE_SIZE); // kmalloc(sizeof(USHORT)*8,MEM_ALLOC_FLAG);
883 NdisMoveMemory(src, arg, strlen(arg));
885 NdisMoveMemory(src, EFUSE_EEPROM_DEFULT_FILE, strlen(EFUSE_EEPROM_DEFULT_FILE));
886 DBGPRINT(RT_DEBUG_TRACE, ("FileName=%s\n",src));
888 RtmpOSFSInfoChange(&osfsInfo, TRUE);
890 srcf = RtmpOSFileOpen(src, O_RDONLY, 0);
891 if (IS_FILE_OPEN_ERR(srcf))
893 DBGPRINT(RT_DEBUG_ERROR, ("--> Error opening file %s\n", src));
899 // The object must have a read method
900 while(RtmpOSFileRead(srcf, &buffer[i], 1)==1)
903 if(i>MAX_EEPROM_BIN_FILE_SIZE)
905 DBGPRINT(RT_DEBUG_ERROR, ("--> Error reading file %s, file size too large[>%d]\n", src, MAX_EEPROM_BIN_FILE_SIZE));
911 retval = RtmpOSFileClose(srcf);
913 DBGPRINT(RT_DEBUG_TRACE, ("--> Error closing file %s\n", src));
917 RtmpOSFSInfoChange(&osfsInfo, FALSE);
921 DBGPRINT(RT_DEBUG_TRACE, ("%02X ",buffer[j]&0xff));
923 PDATA[j/2%8]=((buffer[j]<<8)&0xff00)|(buffer[j-1]&0xff);
933 DBGPRINT(RT_DEBUG_TRACE, (" result=%02X,blk=%02x\n",k,j/16));
935 eFuseWriteRegistersFromBin(pAd,(USHORT)j-15, 16, PDATA);
938 if(eFuseReadRegisters(pAd,j, 2,(PUSHORT)&DATA)!=0x3f)
939 eFuseWriteRegistersFromBin(pAd,(USHORT)j-15, 16, PDATA);
943 printk("%04x ",PDATA[l]);
946 NdisZeroMemory(PDATA,16);
955 RtmpOSFileClose(srcf);
958 RtmpOSFSInfoChange(&osfsInfo, FALSE);
968 static NTSTATUS eFuseWriteRegistersFromBin(
969 IN PRTMP_ADAPTER pAd,
976 USHORT LogicalAddress, BlkNum = 0xffff;
977 UCHAR EFSROM_AOUT,Loop=0;
978 EFUSE_CTRL_STRUC eFuseCtrlStruc;
979 USHORT efuseDataOffset;
980 UINT32 data,tempbuffer;
981 USHORT addr,tmpaddr, InBuf[3], tmpOffset;
983 BOOLEAN bWriteSuccess = TRUE;
984 BOOLEAN bNotWrite=TRUE;
985 BOOLEAN bAllocateNewBlk=TRUE;
987 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin Offset=%x, pData=%04x:%04x:%04x:%04x\n", Offset, *pData,*(pData+1),*(pData+2),*(pData+3)));
991 //Step 0. find the entry in the mapping table
992 //The address of EEPROM is 2-bytes alignment.
993 //The last bit is used for alignment, so it must be 0.
995 tmpOffset = Offset & 0xfffe;
996 EFSROM_AOUT = eFuseReadRegisters(pAd, tmpOffset, 2, &eFuseData);
998 if( EFSROM_AOUT == 0x3f)
999 { //find available logical address pointer
1000 //the logical address does not exist, find an empty one
1001 //from the first address of block 45=16*45=0x2d0 to the last address of block 47
1002 //==>48*16-3(reserved)=2FC
1003 bAllocateNewBlk=TRUE;
1004 for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
1006 //Retrive the logical block nubmer form each logical address pointer
1007 //It will access two logical address pointer each time.
1008 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
1009 if( (LogicalAddress & 0xff) == 0)
1010 {//Not used logical address pointer
1011 BlkNum = i-EFUSE_USAGE_MAP_START;
1014 else if(( (LogicalAddress >> 8) & 0xff) == 0)
1015 {//Not used logical address pointer
1016 if (i != EFUSE_USAGE_MAP_END)
1018 BlkNum = i-EFUSE_USAGE_MAP_START+1;
1026 bAllocateNewBlk=FALSE;
1027 BlkNum = EFSROM_AOUT;
1030 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters BlkNum = %d \n", BlkNum));
1032 if(BlkNum == 0xffff)
1034 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters: out of free E-fuse space!!!\n"));
1038 //If the block is not existing in mapping table, create one
1039 //and write down the 16-bytes data to the new block
1042 DBGPRINT(RT_DEBUG_TRACE, ("Allocate New Blk\n"));
1043 efuseDataOffset = EFUSE_DATA3;
1046 DBGPRINT(RT_DEBUG_TRACE, ("Allocate New Blk, Data%d=%04x%04x\n",3-i,pData[2*i+1],pData[2*i]));
1047 tempbuffer=((pData[2*i+1]<<16)&0xffff0000)|pData[2*i];
1050 RTMP_IO_WRITE32(pAd, efuseDataOffset,tempbuffer);
1051 efuseDataOffset -= 4;
1054 /////////////////////////////////////////////////////////////////
1056 //Step1.1.1. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
1057 RTMP_IO_READ32(pAd, EFUSE_CTRL, &eFuseCtrlStruc.word);
1058 eFuseCtrlStruc.field.EFSROM_AIN = BlkNum* 0x10 ;
1060 //Step1.1.2. Write EFSROM_MODE (0x580, bit7:bit6) to 3.
1061 eFuseCtrlStruc.field.EFSROM_MODE = 3;
1063 //Step1.1.3. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical write procedure.
1064 eFuseCtrlStruc.field.EFSROM_KICK = 1;
1066 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
1068 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
1070 //Step1.1.4. Polling EFSROM_KICK(0x580, bit30) until it become 0 again. It��s done.
1074 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
1076 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
1086 //If the same logical number is existing, check if the writting data and the data
1087 //saving in this block are the same.
1088 /////////////////////////////////////////////////////////////////
1089 //read current values of 16-byte block
1090 RTMP_IO_READ32(pAd, EFUSE_CTRL, &eFuseCtrlStruc.word);
1092 //Step1.2.0. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
1093 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
1095 //Step1.2.1. Write EFSROM_MODE (0x580, bit7:bit6) to 1.
1096 eFuseCtrlStruc.field.EFSROM_MODE = 0;
1098 //Step1.2.2. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical read procedure.
1099 eFuseCtrlStruc.field.EFSROM_KICK = 1;
1101 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
1102 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
1104 //Step1.2.3. Polling EFSROM_KICK(0x580, bit30) until it become 0 again.
1108 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
1110 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
1116 //Step1.2.4. Read 16-byte of data from EFUSE_DATA0-3 (0x59C-0x590)
1117 efuseDataOffset = EFUSE_DATA3;
1120 RTMP_IO_READ32(pAd, efuseDataOffset, (PUINT32) &buffer[i]);
1121 efuseDataOffset -= 4;
1123 //Step1.2.5. Check if the data of efuse and the writing data are the same.
1126 tempbuffer=((pData[2*i+1]<<16)&0xffff0000)|pData[2*i];
1127 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));
1129 if(((buffer[i]&0xffff0000)==(pData[2*i+1]<<16))&&((buffer[i]&0xffff)==pData[2*i]))
1139 printk("The data is not the same\n");
1143 addr = BlkNum * 0x10 ;
1145 InBuf[0] = addr+2*i;
1147 InBuf[2] = pData[i];
1149 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 2);
1159 //Step 2. Write mapping table
1160 addr = EFUSE_USAGE_MAP_START+BlkNum;
1169 //convert the address from 10 to 8 bit ( bit7, 6 = parity and bit5 ~ 0 = bit9~4), and write to logical map entry
1172 tmpOffset |= ((~((tmpOffset & 0x01) ^ ( tmpOffset >> 1 & 0x01) ^ (tmpOffset >> 2 & 0x01) ^ (tmpOffset >> 3 & 0x01))) << 6) & 0x40;
1173 tmpOffset |= ((~( (tmpOffset >> 2 & 0x01) ^ (tmpOffset >> 3 & 0x01) ^ (tmpOffset >> 4 & 0x01) ^ ( tmpOffset >> 5 & 0x01))) << 7) & 0x80;
1175 // write the logical address
1177 InBuf[2] = tmpOffset<<8;
1179 InBuf[2] = tmpOffset;
1181 eFuseWritePhysical(pAd,&InBuf[0], 6, NULL, 0);
1183 //Step 3. Compare data if not the same, invalidate the mapping entry, then re-write the data until E-fuse is exhausted
1184 bWriteSuccess = TRUE;
1187 addr = BlkNum * 0x10 ;
1189 InBuf[0] = addr+2*i;
1193 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
1194 DBGPRINT(RT_DEBUG_TRACE, ("addr=%x, buffer[i]=%x,InBuf[2]=%x\n",InBuf[0],pData[i],InBuf[2]));
1195 if(pData[i] != InBuf[2])
1197 bWriteSuccess = FALSE;
1202 //Step 4. invlidate mapping entry and find a free mapping entry if not succeed
1204 if (!bWriteSuccess&&Loop<2)
1206 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin::Not bWriteSuccess BlkNum = %d\n", BlkNum));
1208 // the offset of current mapping entry
1209 addr = EFUSE_USAGE_MAP_START+BlkNum;
1211 //find a new mapping entry
1213 for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
1215 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
1216 if( (LogicalAddress & 0xff) == 0)
1218 BlkNum = i-EFUSE_USAGE_MAP_START;
1221 else if(( (LogicalAddress >> 8) & 0xff) == 0)
1223 if (i != EFUSE_USAGE_MAP_END)
1225 BlkNum = i+1-EFUSE_USAGE_MAP_START;
1230 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin::Not bWriteSuccess new BlkNum = %d\n", BlkNum));
1231 if(BlkNum == 0xffff)
1233 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin: out of free E-fuse space!!!\n"));
1237 //invalidate the original mapping entry if new entry is not found
1245 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
1247 // write the logical address
1250 // Invalidate the high byte
1251 for (i=8; i<15; i++)
1253 if( ( (InBuf[2] >> i) & 0x01) == 0)
1255 InBuf[2] |= (0x1 <<i);
1262 // invalidate the low byte
1265 if( ( (InBuf[2] >> i) & 0x01) == 0)
1267 InBuf[2] |= (0x1 <<i);
1272 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 0);
1276 while(!bWriteSuccess&&Loop<2);
1282 int rtmp_ee_efuse_read16(
1283 IN RTMP_ADAPTER *pAd,
1287 if(pAd->bFroceEEPROMBuffer || pAd->bEEPROMFile)
1289 DBGPRINT(RT_DEBUG_TRACE, ("Read from EEPROM Buffer\n"));
1290 NdisMoveMemory(pValue, &(pAd->EEPROMImage[Offset]), 2);
1293 eFuseReadRegisters(pAd, Offset, 2, pValue);
1298 int rtmp_ee_efuse_write16(
1299 IN RTMP_ADAPTER *pAd,
1303 if(pAd->bFroceEEPROMBuffer||pAd->bEEPROMFile)
1305 DBGPRINT(RT_DEBUG_TRACE, ("Write to EEPROM Buffer\n"));
1306 NdisMoveMemory(&(pAd->EEPROMImage[Offset]), &data, 2);
1309 eFuseWriteRegisters(pAd, Offset, 2, &data);
1314 int RtmpEfuseSupportCheck(
1315 IN RTMP_ADAPTER *pAd)
1321 eFusePhysicalReadRegisters(pAd, EFUSE_TAG, 2, &value);
1322 pAd->EFuseTag = (value & 0xff);
1327 INT set_eFuseBufferModeWriteBack_Proc(
1328 IN PRTMP_ADAPTER pAd,
1336 Enable= simple_strtol(arg, 0, 16);
1342 DBGPRINT(RT_DEBUG_TRACE, ("set_eFuseBufferMode_Proc:: Call WRITEEEPROMBUF"));
1343 eFuseWriteEeeppromBuf(pAd);
1352 ========================================================================
1354 Routine Description:
1355 Load EEPROM from bin file for eFuse mode
1358 Adapter Pointer to our adapter
1361 NDIS_STATUS_SUCCESS firmware image load ok
1362 NDIS_STATUS_FAILURE image not found
1364 IRQL = PASSIVE_LEVEL
1366 ========================================================================
1368 INT eFuseLoadEEPROM(
1369 IN PRTMP_ADAPTER pAd)
1374 RTMP_OS_FS_INFO osFSInfo;
1377 src=EFUSE_BUFFER_PATH;
1378 DBGPRINT(RT_DEBUG_TRACE, ("FileName=%s\n",src));
1381 RtmpOSFSInfoChange(&osFSInfo, TRUE);
1385 srcf = RtmpOSFileOpen(src, O_RDONLY, 0);
1386 if (IS_FILE_OPEN_ERR(srcf))
1388 DBGPRINT(RT_DEBUG_ERROR, ("--> Error %ld opening %s\n", -PTR_ERR(srcf),src));
1394 memset(pAd->EEPROMImage, 0x00, MAX_EEPROM_BIN_FILE_SIZE);
1397 retval =RtmpOSFileRead(srcf, (PSTRING)pAd->EEPROMImage, MAX_EEPROM_BIN_FILE_SIZE);
1400 RTMPSetProfileParameters(pAd, (PSTRING)pAd->EEPROMImage);
1401 retval = NDIS_STATUS_SUCCESS;
1404 DBGPRINT(RT_DEBUG_ERROR, ("Read file \"%s\" failed(errCode=%d)!\n", src, retval));
1412 DBGPRINT(RT_DEBUG_ERROR, ("--> Error src or srcf is null\n"));
1417 retval=RtmpOSFileClose(srcf);
1421 DBGPRINT(RT_DEBUG_TRACE, ("--> Error %d closing %s\n", -retval, src));
1425 RtmpOSFSInfoChange(&osFSInfo, FALSE);
1430 INT eFuseWriteEeeppromBuf(
1431 IN PRTMP_ADAPTER pAd)
1437 RTMP_OS_FS_INFO osFSInfo;
1440 src=EFUSE_BUFFER_PATH;
1441 DBGPRINT(RT_DEBUG_TRACE, ("FileName=%s\n",src));
1443 RtmpOSFSInfoChange(&osFSInfo, TRUE);
1449 srcf = RtmpOSFileOpen(src, O_WRONLY|O_CREAT, 0);
1451 if (IS_FILE_OPEN_ERR(srcf))
1453 DBGPRINT(RT_DEBUG_ERROR, ("--> Error %ld opening %s\n", -PTR_ERR(srcf),src));
1459 // The object must have a read method
1460 if (srcf->f_op && srcf->f_op->write)
1462 // The object must have a read method
1463 srcf->f_op->write(srcf, pAd->EEPROMImage, 1024, &srcf->f_pos);
1468 DBGPRINT(RT_DEBUG_ERROR, ("--> Error!! System doest not support read function\n"));
1473 RtmpOSFileWrite(srcf, (PSTRING)pAd->EEPROMImage,MAX_EEPROM_BIN_FILE_SIZE);
1481 DBGPRINT(RT_DEBUG_ERROR, ("--> Error src or srcf is null\n"));
1486 retval=RtmpOSFileClose(srcf);
1490 DBGPRINT(RT_DEBUG_TRACE, ("--> Error %d closing %s\n", -retval, src));
1493 RtmpOSFSInfoChange(&osFSInfo, FALSE);
1498 VOID eFuseGetFreeBlockCount(IN PRTMP_ADAPTER pAd,
1499 PUINT EfuseFreeBlock)
1502 USHORT LogicalAddress;
1505 DBGPRINT(RT_DEBUG_TRACE,("eFuseGetFreeBlockCount Only supports efuse Mode\n"));
1508 for (i = EFUSE_USAGE_MAP_START; i <= EFUSE_USAGE_MAP_END; i+=2)
1510 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
1511 if( (LogicalAddress & 0xff) == 0)
1513 *EfuseFreeBlock= (UCHAR) (EFUSE_USAGE_MAP_END-i+1);
1516 else if(( (LogicalAddress >> 8) & 0xff) == 0)
1518 *EfuseFreeBlock = (UCHAR) (EFUSE_USAGE_MAP_END-i);
1522 if(i == EFUSE_USAGE_MAP_END)
1523 *EfuseFreeBlock = 0;
1525 DBGPRINT(RT_DEBUG_TRACE,("eFuseGetFreeBlockCount is 0x%x\n",*EfuseFreeBlock));
1529 IN PRTMP_ADAPTER pAd)
1531 UINT EfuseFreeBlock=0;
1532 DBGPRINT(RT_DEBUG_ERROR, ("NVM is Efuse and its size =%x[%x-%x] \n",EFUSE_USAGE_MAP_SIZE,EFUSE_USAGE_MAP_START,EFUSE_USAGE_MAP_END));
1533 eFuseGetFreeBlockCount(pAd, &EfuseFreeBlock);
1534 //If the used block of efuse is less than 5. We assume the default value
1535 // of this efuse is empty and change to the buffer mode in odrder to
1536 //bring up interfaces successfully.
1537 if(EfuseFreeBlock > (EFUSE_USAGE_MAP_END-5))
1539 DBGPRINT(RT_DEBUG_ERROR, ("NVM is Efuse and the information is too less to bring up interface. Force to use EEPROM Buffer Mode\n"));
1540 pAd->bFroceEEPROMBuffer = TRUE;
1541 eFuseLoadEEPROM(pAd);
1544 pAd->bFroceEEPROMBuffer = FALSE;
1545 DBGPRINT(RT_DEBUG_TRACE, ("NVM is Efuse and force to use EEPROM Buffer Mode=%x\n",pAd->bFroceEEPROMBuffer));