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 -------- ---------- ----------------------------------------------
39 #include "../rt_config.h"
43 #define EFUSE_USAGE_MAP_START 0x2d0
44 #define EFUSE_USAGE_MAP_END 0x2fc
45 #define EFUSE_USAGE_MAP_SIZE 45
49 #define EFUSE_EEPROM_DEFULT_FILE "RT30xxEEPROM.bin"
50 #define MAX_EEPROM_BIN_FILE_SIZE 1024
54 #define EFUSE_TAG 0x2fe
56 typedef union _EFUSE_CTRL_STRUC {
60 UINT32 EFSROM_LDO_OFF_TIME:6;
61 UINT32 EFSROM_LDO_ON_TIME:2;
68 } EFUSE_CTRL_STRUC, *PEFUSE_CTRL_STRUC;
70 static UCHAR eFuseReadRegisters(
76 static VOID eFuseReadPhysical(
78 IN PUSHORT lpInBuffer,
79 IN ULONG nInBufferSize,
80 OUT PUSHORT lpOutBuffer,
81 IN ULONG nOutBufferSize);
83 static VOID eFusePhysicalWriteRegisters(
89 static NTSTATUS eFuseWriteRegisters(
95 static VOID eFuseWritePhysical(
100 ULONG nOutBufferSize);
103 static NTSTATUS eFuseWriteRegistersFromBin(
104 IN PRTMP_ADAPTER pAd,
111 ========================================================================
121 ========================================================================
123 UCHAR eFuseReadRegisters(
124 IN PRTMP_ADAPTER pAd,
129 EFUSE_CTRL_STRUC eFuseCtrlStruc;
131 USHORT efuseDataOffset;
134 RTMP_IO_READ32(pAd, EFUSE_CTRL, &eFuseCtrlStruc.word);
136 //Step0. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
137 //Use the eeprom logical address and covert to address to block number
138 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
140 //Step1. Write EFSROM_MODE (0x580, bit7:bit6) to 0.
141 eFuseCtrlStruc.field.EFSROM_MODE = 0;
143 //Step2. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical read procedure.
144 eFuseCtrlStruc.field.EFSROM_KICK = 1;
146 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
147 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
149 //Step3. Polling EFSROM_KICK(0x580, bit30) until it become 0 again.
153 //rtmp.HwMemoryReadDword(EFUSE_CTRL, (DWORD *) &eFuseCtrlStruc, 4);
154 RTMP_IO_READ32(pAd, EFUSE_CTRL, &eFuseCtrlStruc.word);
155 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
163 //if EFSROM_AOUT is not found in physical address, write 0xffff
164 if (eFuseCtrlStruc.field.EFSROM_AOUT == 0x3f)
166 for(i=0; i<Length/2; i++)
167 *(pData+2*i) = 0xffff;
171 //Step4. Read 16-byte of data from EFUSE_DATA0-3 (0x590-0x59C)
172 efuseDataOffset = EFUSE_DATA3 - (Offset & 0xC);
173 //data hold 4 bytes data.
174 //In RTMP_IO_READ32 will automatically execute 32-bytes swapping
175 RTMP_IO_READ32(pAd, efuseDataOffset, &data);
176 //Decide the upper 2 bytes or the bottom 2 bytes.
177 // Little-endian S | S Big-endian
178 // addr 3 2 1 0 | 0 1 2 3
179 // Ori-V D C B A | A B C D
183 //The return byte statrs from S. Therefore, the little-endian will return BA, the Big-endian will return DC.
184 //For returning the bottom 2 bytes, the Big-endian should shift right 2-bytes.
185 data = data >> (8*(Offset & 0x3));
187 NdisMoveMemory(pData, &data, Length);
190 return (UCHAR) eFuseCtrlStruc.field.EFSROM_AOUT;
195 ========================================================================
205 ========================================================================
207 VOID eFusePhysicalReadRegisters(
208 IN PRTMP_ADAPTER pAd,
213 EFUSE_CTRL_STRUC eFuseCtrlStruc;
215 USHORT efuseDataOffset;
218 RTMP_IO_READ32(pAd, EFUSE_CTRL, &eFuseCtrlStruc.word);
220 //Step0. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
221 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
223 //Step1. Write EFSROM_MODE (0x580, bit7:bit6) to 1.
224 //Read in physical view
225 eFuseCtrlStruc.field.EFSROM_MODE = 1;
227 //Step2. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical read procedure.
228 eFuseCtrlStruc.field.EFSROM_KICK = 1;
230 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
231 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
233 //Step3. Polling EFSROM_KICK(0x580, bit30) until it become 0 again.
237 RTMP_IO_READ32(pAd, EFUSE_CTRL, &eFuseCtrlStruc.word);
238 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
244 //Step4. Read 16-byte of data from EFUSE_DATA0-3 (0x59C-0x590)
245 //Because the size of each EFUSE_DATA is 4 Bytes, the size of address of each is 2 bits.
246 //The previous 2 bits is the EFUSE_DATA number, the last 2 bits is used to decide which bytes
247 //Decide which EFUSE_DATA to read
252 efuseDataOffset = EFUSE_DATA3 - (Offset & 0xC) ;
254 RTMP_IO_READ32(pAd, efuseDataOffset, &data);
256 data = data >> (8*(Offset & 0x3));
258 NdisMoveMemory(pData, &data, Length);
263 ========================================================================
273 ========================================================================
275 static VOID eFuseReadPhysical(
276 IN PRTMP_ADAPTER pAd,
277 IN PUSHORT lpInBuffer,
278 IN ULONG nInBufferSize,
279 OUT PUSHORT lpOutBuffer,
280 IN ULONG nOutBufferSize
283 USHORT* pInBuf = (USHORT*)lpInBuffer;
284 USHORT* pOutBuf = (USHORT*)lpOutBuffer;
286 USHORT Offset = pInBuf[0]; //addr
287 USHORT Length = pInBuf[1]; //length
290 for(i=0; i<Length; i+=2)
292 eFusePhysicalReadRegisters(pAd,Offset+i, 2, &pOutBuf[i/2]);
297 ========================================================================
307 ========================================================================
310 IN PRTMP_ADAPTER pAd,
315 USHORT* pOutBuf = (USHORT*)pData;
316 NTSTATUS Status = STATUS_SUCCESS;
320 for(i=0; i<Length; i+=2)
322 EFSROM_AOUT = eFuseReadRegisters(pAd, Offset+i, 2, &pOutBuf[i/2]);
328 ========================================================================
338 ========================================================================
340 static VOID eFusePhysicalWriteRegisters(
341 IN PRTMP_ADAPTER pAd,
346 EFUSE_CTRL_STRUC eFuseCtrlStruc;
348 USHORT efuseDataOffset;
349 UINT32 data, eFuseDataBuffer[4];
351 //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.
353 /////////////////////////////////////////////////////////////////
354 //read current values of 16-byte block
355 RTMP_IO_READ32(pAd, EFUSE_CTRL, &eFuseCtrlStruc.word);
357 //Step0. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
358 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
360 //Step1. Write EFSROM_MODE (0x580, bit7:bit6) to 1.
361 eFuseCtrlStruc.field.EFSROM_MODE = 1;
363 //Step2. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical read procedure.
364 eFuseCtrlStruc.field.EFSROM_KICK = 1;
366 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
367 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
369 //Step3. Polling EFSROM_KICK(0x580, bit30) until it become 0 again.
373 RTMP_IO_READ32(pAd, EFUSE_CTRL, &eFuseCtrlStruc.word);
375 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
381 //Step4. Read 16-byte of data from EFUSE_DATA0-3 (0x59C-0x590)
382 efuseDataOffset = EFUSE_DATA3;
385 RTMP_IO_READ32(pAd, efuseDataOffset, (PUINT32) &eFuseDataBuffer[i]);
386 efuseDataOffset -= 4;
389 //Update the value, the offset is multiple of 2, length is 2
390 efuseDataOffset = (Offset & 0xc) >> 2;
391 data = pData[0] & 0xffff;
392 //The offset should be 0x***10 or 0x***00
393 if((Offset % 4) != 0)
395 eFuseDataBuffer[efuseDataOffset] = (eFuseDataBuffer[efuseDataOffset] & 0xffff) | (data << 16);
399 eFuseDataBuffer[efuseDataOffset] = (eFuseDataBuffer[efuseDataOffset] & 0xffff0000) | data;
402 efuseDataOffset = EFUSE_DATA3;
405 RTMP_IO_WRITE32(pAd, efuseDataOffset, eFuseDataBuffer[i]);
406 efuseDataOffset -= 4;
408 /////////////////////////////////////////////////////////////////
410 //Step1. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
412 RTMP_IO_READ32(pAd, EFUSE_CTRL, &eFuseCtrlStruc.word);
414 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
416 //Step2. Write EFSROM_MODE (0x580, bit7:bit6) to 3.
417 eFuseCtrlStruc.field.EFSROM_MODE = 3;
419 //Step3. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical write procedure.
420 eFuseCtrlStruc.field.EFSROM_KICK = 1;
422 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
423 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
425 //Step4. Polling EFSROM_KICK(0x580, bit30) until it become 0 again. It¡¦s done.
430 RTMP_IO_READ32(pAd, EFUSE_CTRL, &eFuseCtrlStruc.word);
432 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
441 ========================================================================
451 ========================================================================
453 static NTSTATUS eFuseWriteRegisters(
454 IN PRTMP_ADAPTER pAd,
461 USHORT LogicalAddress, BlkNum = 0xffff;
464 USHORT addr,tmpaddr, InBuf[3], tmpOffset;
466 BOOLEAN bWriteSuccess = TRUE;
468 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters Offset=%x, pData=%x\n", Offset, *pData));
470 //Step 0. find the entry in the mapping table
471 //The address of EEPROM is 2-bytes alignment.
472 //The last bit is used for alignment, so it must be 0.
473 tmpOffset = Offset & 0xfffe;
474 EFSROM_AOUT = eFuseReadRegisters(pAd, tmpOffset, 2, &eFuseData);
476 if( EFSROM_AOUT == 0x3f)
477 { //find available logical address pointer
478 //the logical address does not exist, find an empty one
479 //from the first address of block 45=16*45=0x2d0 to the last address of block 47
480 //==>48*16-3(reserved)=2FC
481 for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
483 //Retrive the logical block nubmer form each logical address pointer
484 //It will access two logical address pointer each time.
485 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
486 if( (LogicalAddress & 0xff) == 0)
487 {//Not used logical address pointer
488 BlkNum = i-EFUSE_USAGE_MAP_START;
491 else if(( (LogicalAddress >> 8) & 0xff) == 0)
492 {//Not used logical address pointer
493 if (i != EFUSE_USAGE_MAP_END)
495 BlkNum = i-EFUSE_USAGE_MAP_START+1;
503 BlkNum = EFSROM_AOUT;
506 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters BlkNum = %d \n", BlkNum));
510 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters: out of free E-fuse space!!!\n"));
514 //Step 1. Save data of this block which is pointed by the avaible logical address pointer
515 // read and save the original block data
518 addr = BlkNum * 0x10 ;
524 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
526 buffer[i] = InBuf[2];
529 //Step 2. Update the data in buffer, and write the data to Efuse
530 buffer[ (Offset >> 1) % 8] = pData[0];
534 //Step 3. Write the data to Efuse
539 addr = BlkNum * 0x10 ;
543 InBuf[2] = buffer[i];
545 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 2);
550 addr = BlkNum * 0x10 ;
552 InBuf[0] = addr+(Offset % 16);
556 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 2);
559 //Step 4. Write mapping table
560 addr = EFUSE_USAGE_MAP_START+BlkNum;
569 //convert the address from 10 to 8 bit ( bit7, 6 = parity and bit5 ~ 0 = bit9~4), and write to logical map entry
572 tmpOffset |= ((~((tmpOffset & 0x01) ^ ( tmpOffset >> 1 & 0x01) ^ (tmpOffset >> 2 & 0x01) ^ (tmpOffset >> 3 & 0x01))) << 6) & 0x40;
573 tmpOffset |= ((~( (tmpOffset >> 2 & 0x01) ^ (tmpOffset >> 3 & 0x01) ^ (tmpOffset >> 4 & 0x01) ^ ( tmpOffset >> 5 & 0x01))) << 7) & 0x80;
575 // write the logical address
577 InBuf[2] = tmpOffset<<8;
579 InBuf[2] = tmpOffset;
581 eFuseWritePhysical(pAd,&InBuf[0], 6, NULL, 0);
583 //Step 5. Compare data if not the same, invalidate the mapping entry, then re-write the data until E-fuse is exhausted
584 bWriteSuccess = TRUE;
587 addr = BlkNum * 0x10 ;
593 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
595 if(buffer[i] != InBuf[2])
597 bWriteSuccess = FALSE;
602 //Step 6. invlidate mapping entry and find a free mapping entry if not succeed
605 DBGPRINT(RT_DEBUG_TRACE, ("Not bWriteSuccess BlkNum = %d\n", BlkNum));
607 // the offset of current mapping entry
608 addr = EFUSE_USAGE_MAP_START+BlkNum;
610 //find a new mapping entry
612 for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
614 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
615 if( (LogicalAddress & 0xff) == 0)
617 BlkNum = i-EFUSE_USAGE_MAP_START;
620 else if(( (LogicalAddress >> 8) & 0xff) == 0)
622 if (i != EFUSE_USAGE_MAP_END)
624 BlkNum = i+1-EFUSE_USAGE_MAP_START;
629 DBGPRINT(RT_DEBUG_TRACE, ("Not bWriteSuccess new BlkNum = %d\n", BlkNum));
632 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters: out of free E-fuse space!!!\n"));
636 //invalidate the original mapping entry if new entry is not found
644 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
646 // write the logical address
649 // Invalidate the high byte
652 if( ( (InBuf[2] >> i) & 0x01) == 0)
654 InBuf[2] |= (0x1 <<i);
661 // invalidate the low byte
664 if( ( (InBuf[2] >> i) & 0x01) == 0)
666 InBuf[2] |= (0x1 <<i);
671 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 0);
674 while (!bWriteSuccess&&Loop<2);
676 DBGPRINT(RT_DEBUG_ERROR,("Efsue Write Failed!!\n"));
682 ========================================================================
692 ========================================================================
694 static VOID eFuseWritePhysical(
695 IN PRTMP_ADAPTER pAd,
702 USHORT* pInBuf = (USHORT*)lpInBuffer;
704 //USHORT* pOutBuf = (USHORT*)ioBuffer;
705 USHORT Offset = pInBuf[0]; // addr
706 USHORT Length = pInBuf[1]; // length
707 USHORT* pValueX = &pInBuf[2]; // value ...
709 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWritePhysical Offset=0x%x, length=%d\n", Offset, Length));
712 // Little-endian S | S Big-endian
713 // addr 3 2 1 0 | 0 1 2 3
714 // Ori-V D C B A | A B C D
717 // Both the little and big-endian use the same sequence to write data.
718 // Therefore, we only need swap data when read the data.
719 for (i=0; i<Length; i+=2)
721 eFusePhysicalWriteRegisters(pAd, Offset+i, 2, &pValueX[i/2]);
728 ========================================================================
738 ========================================================================
741 IN PRTMP_ADAPTER pAd,
747 USHORT* pValueX = (PUSHORT) pData; //value ...
749 // The input value=3070 will be stored as following
750 // Little-endian S | S Big-endian
752 // Ori-V 30 70 | 30 70
757 // The swapping should be removed for big-endian
758 for(i=0; i<length; i+=2)
760 eFuseWriteRegisters(pAd, Offset+i, 2, &pValueX[i/2]);
770 ========================================================================
780 ========================================================================
782 INT set_eFuseGetFreeBlockCount_Proc(
783 IN PRTMP_ADAPTER pAd,
787 USHORT LogicalAddress;
788 USHORT efusefreenum=0;
791 for (i = EFUSE_USAGE_MAP_START; i <= EFUSE_USAGE_MAP_END; i+=2)
793 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
794 if( (LogicalAddress & 0xff) == 0)
796 efusefreenum= (UCHAR) (EFUSE_USAGE_MAP_END-i+1);
799 else if(( (LogicalAddress >> 8) & 0xff) == 0)
801 efusefreenum = (UCHAR) (EFUSE_USAGE_MAP_END-i);
805 if(i == EFUSE_USAGE_MAP_END)
808 printk("efuseFreeNumber is %d\n",efusefreenum);
813 INT set_eFusedump_Proc(
814 IN PRTMP_ADAPTER pAd,
821 for(i =0; i<EFUSE_USAGE_MAP_END/2; i++)
827 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
829 printk("\nBlock %x:",i/8);
830 printk("%04x ",InBuf[2]);
836 INT set_eFuseLoadFromBin_Proc(
837 IN PRTMP_ADAPTER pAd,
842 RTMP_OS_FS_INFO osfsInfo;
844 PSTRING buffer, memPtr;
849 memSize = 128 + MAX_EEPROM_BIN_FILE_SIZE + sizeof(USHORT) * 8;
850 memPtr = kmalloc(memSize, MEM_ALLOC_FLAG);
854 NdisZeroMemory(memPtr, memSize);
855 src = memPtr; // kmalloc(128, MEM_ALLOC_FLAG);
856 buffer = src + 128; // kmalloc(MAX_EEPROM_BIN_FILE_SIZE, MEM_ALLOC_FLAG);
857 PDATA = (USHORT*)(buffer + MAX_EEPROM_BIN_FILE_SIZE); // kmalloc(sizeof(USHORT)*8,MEM_ALLOC_FLAG);
860 NdisMoveMemory(src, arg, strlen(arg));
862 NdisMoveMemory(src, EFUSE_EEPROM_DEFULT_FILE, strlen(EFUSE_EEPROM_DEFULT_FILE));
863 DBGPRINT(RT_DEBUG_TRACE, ("FileName=%s\n",src));
865 RtmpOSFSInfoChange(&osfsInfo, TRUE);
867 srcf = RtmpOSFileOpen(src, O_RDONLY, 0);
868 if (IS_FILE_OPEN_ERR(srcf))
870 DBGPRINT(RT_DEBUG_ERROR, ("--> Error opening file %s\n", src));
876 // The object must have a read method
877 while(RtmpOSFileRead(srcf, &buffer[i], 1)==1)
880 if(i>MAX_EEPROM_BIN_FILE_SIZE)
882 DBGPRINT(RT_DEBUG_ERROR, ("--> Error reading file %s, file size too large[>%d]\n", src, MAX_EEPROM_BIN_FILE_SIZE));
888 retval = RtmpOSFileClose(srcf);
890 DBGPRINT(RT_DEBUG_TRACE, ("--> Error closing file %s\n", src));
894 RtmpOSFSInfoChange(&osfsInfo, FALSE);
898 DBGPRINT(RT_DEBUG_TRACE, ("%02X ",buffer[j]&0xff));
900 PDATA[j/2%8]=((buffer[j]<<8)&0xff00)|(buffer[j-1]&0xff);
910 DBGPRINT(RT_DEBUG_TRACE, (" result=%02X,blk=%02x\n",k,j/16));
912 eFuseWriteRegistersFromBin(pAd,(USHORT)j-15, 16, PDATA);
915 if(eFuseReadRegisters(pAd,j, 2,(PUSHORT)&DATA)!=0x3f)
916 eFuseWriteRegistersFromBin(pAd,(USHORT)j-15, 16, PDATA);
920 printk("%04x ",PDATA[l]);
923 NdisZeroMemory(PDATA,16);
932 RtmpOSFileClose(srcf);
935 RtmpOSFSInfoChange(&osfsInfo, FALSE);
945 static NTSTATUS eFuseWriteRegistersFromBin(
946 IN PRTMP_ADAPTER pAd,
953 USHORT LogicalAddress, BlkNum = 0xffff;
954 UCHAR EFSROM_AOUT,Loop=0;
955 EFUSE_CTRL_STRUC eFuseCtrlStruc;
956 USHORT efuseDataOffset;
957 UINT32 data,tempbuffer;
958 USHORT addr,tmpaddr, InBuf[3], tmpOffset;
960 BOOLEAN bWriteSuccess = TRUE;
961 BOOLEAN bNotWrite=TRUE;
962 BOOLEAN bAllocateNewBlk=TRUE;
964 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin Offset=%x, pData=%04x:%04x:%04x:%04x\n", Offset, *pData,*(pData+1),*(pData+2),*(pData+3)));
968 //Step 0. find the entry in the mapping table
969 //The address of EEPROM is 2-bytes alignment.
970 //The last bit is used for alignment, so it must be 0.
972 tmpOffset = Offset & 0xfffe;
973 EFSROM_AOUT = eFuseReadRegisters(pAd, tmpOffset, 2, &eFuseData);
975 if( EFSROM_AOUT == 0x3f)
976 { //find available logical address pointer
977 //the logical address does not exist, find an empty one
978 //from the first address of block 45=16*45=0x2d0 to the last address of block 47
979 //==>48*16-3(reserved)=2FC
980 bAllocateNewBlk=TRUE;
981 for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
983 //Retrive the logical block nubmer form each logical address pointer
984 //It will access two logical address pointer each time.
985 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
986 if( (LogicalAddress & 0xff) == 0)
987 {//Not used logical address pointer
988 BlkNum = i-EFUSE_USAGE_MAP_START;
991 else if(( (LogicalAddress >> 8) & 0xff) == 0)
992 {//Not used logical address pointer
993 if (i != EFUSE_USAGE_MAP_END)
995 BlkNum = i-EFUSE_USAGE_MAP_START+1;
1003 bAllocateNewBlk=FALSE;
1004 BlkNum = EFSROM_AOUT;
1007 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters BlkNum = %d \n", BlkNum));
1009 if(BlkNum == 0xffff)
1011 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters: out of free E-fuse space!!!\n"));
1015 //If the block is not existing in mapping table, create one
1016 //and write down the 16-bytes data to the new block
1019 DBGPRINT(RT_DEBUG_TRACE, ("Allocate New Blk\n"));
1020 efuseDataOffset = EFUSE_DATA3;
1023 DBGPRINT(RT_DEBUG_TRACE, ("Allocate New Blk, Data%d=%04x%04x\n",3-i,pData[2*i+1],pData[2*i]));
1024 tempbuffer=((pData[2*i+1]<<16)&0xffff0000)|pData[2*i];
1027 RTMP_IO_WRITE32(pAd, efuseDataOffset,tempbuffer);
1028 efuseDataOffset -= 4;
1031 /////////////////////////////////////////////////////////////////
1033 //Step1.1.1. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
1034 RTMP_IO_READ32(pAd, EFUSE_CTRL, &eFuseCtrlStruc.word);
1035 eFuseCtrlStruc.field.EFSROM_AIN = BlkNum* 0x10 ;
1037 //Step1.1.2. Write EFSROM_MODE (0x580, bit7:bit6) to 3.
1038 eFuseCtrlStruc.field.EFSROM_MODE = 3;
1040 //Step1.1.3. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical write procedure.
1041 eFuseCtrlStruc.field.EFSROM_KICK = 1;
1043 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
1045 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
1047 //Step1.1.4. Polling EFSROM_KICK(0x580, bit30) until it become 0 again. It¡¦s done.
1051 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
1053 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
1063 //If the same logical number is existing, check if the writting data and the data
1064 //saving in this block are the same.
1065 /////////////////////////////////////////////////////////////////
1066 //read current values of 16-byte block
1067 RTMP_IO_READ32(pAd, EFUSE_CTRL, &eFuseCtrlStruc.word);
1069 //Step1.2.0. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
1070 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
1072 //Step1.2.1. Write EFSROM_MODE (0x580, bit7:bit6) to 1.
1073 eFuseCtrlStruc.field.EFSROM_MODE = 0;
1075 //Step1.2.2. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical read procedure.
1076 eFuseCtrlStruc.field.EFSROM_KICK = 1;
1078 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
1079 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
1081 //Step1.2.3. Polling EFSROM_KICK(0x580, bit30) until it become 0 again.
1085 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
1087 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
1093 //Step1.2.4. Read 16-byte of data from EFUSE_DATA0-3 (0x59C-0x590)
1094 efuseDataOffset = EFUSE_DATA3;
1097 RTMP_IO_READ32(pAd, efuseDataOffset, (PUINT32) &buffer[i]);
1098 efuseDataOffset -= 4;
1100 //Step1.2.5. Check if the data of efuse and the writing data are the same.
1103 tempbuffer=((pData[2*i+1]<<16)&0xffff0000)|pData[2*i];
1104 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));
1106 if(((buffer[i]&0xffff0000)==(pData[2*i+1]<<16))&&((buffer[i]&0xffff)==pData[2*i]))
1116 printk("The data is not the same\n");
1120 addr = BlkNum * 0x10 ;
1122 InBuf[0] = addr+2*i;
1124 InBuf[2] = pData[i];
1126 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 2);
1136 //Step 2. Write mapping table
1137 addr = EFUSE_USAGE_MAP_START+BlkNum;
1146 //convert the address from 10 to 8 bit ( bit7, 6 = parity and bit5 ~ 0 = bit9~4), and write to logical map entry
1149 tmpOffset |= ((~((tmpOffset & 0x01) ^ ( tmpOffset >> 1 & 0x01) ^ (tmpOffset >> 2 & 0x01) ^ (tmpOffset >> 3 & 0x01))) << 6) & 0x40;
1150 tmpOffset |= ((~( (tmpOffset >> 2 & 0x01) ^ (tmpOffset >> 3 & 0x01) ^ (tmpOffset >> 4 & 0x01) ^ ( tmpOffset >> 5 & 0x01))) << 7) & 0x80;
1152 // write the logical address
1154 InBuf[2] = tmpOffset<<8;
1156 InBuf[2] = tmpOffset;
1158 eFuseWritePhysical(pAd,&InBuf[0], 6, NULL, 0);
1160 //Step 3. Compare data if not the same, invalidate the mapping entry, then re-write the data until E-fuse is exhausted
1161 bWriteSuccess = TRUE;
1164 addr = BlkNum * 0x10 ;
1166 InBuf[0] = addr+2*i;
1170 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
1171 DBGPRINT(RT_DEBUG_TRACE, ("addr=%x, buffer[i]=%x,InBuf[2]=%x\n",InBuf[0],pData[i],InBuf[2]));
1172 if(pData[i] != InBuf[2])
1174 bWriteSuccess = FALSE;
1179 //Step 4. invlidate mapping entry and find a free mapping entry if not succeed
1181 if (!bWriteSuccess&&Loop<2)
1183 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin::Not bWriteSuccess BlkNum = %d\n", BlkNum));
1185 // the offset of current mapping entry
1186 addr = EFUSE_USAGE_MAP_START+BlkNum;
1188 //find a new mapping entry
1190 for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
1192 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
1193 if( (LogicalAddress & 0xff) == 0)
1195 BlkNum = i-EFUSE_USAGE_MAP_START;
1198 else if(( (LogicalAddress >> 8) & 0xff) == 0)
1200 if (i != EFUSE_USAGE_MAP_END)
1202 BlkNum = i+1-EFUSE_USAGE_MAP_START;
1207 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin::Not bWriteSuccess new BlkNum = %d\n", BlkNum));
1208 if(BlkNum == 0xffff)
1210 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin: out of free E-fuse space!!!\n"));
1214 //invalidate the original mapping entry if new entry is not found
1222 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
1224 // write the logical address
1227 // Invalidate the high byte
1228 for (i=8; i<15; i++)
1230 if( ( (InBuf[2] >> i) & 0x01) == 0)
1232 InBuf[2] |= (0x1 <<i);
1239 // invalidate the low byte
1242 if( ( (InBuf[2] >> i) & 0x01) == 0)
1244 InBuf[2] |= (0x1 <<i);
1249 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 0);
1253 while(!bWriteSuccess&&Loop<2);
1259 int rtmp_ee_efuse_read16(
1260 IN RTMP_ADAPTER *pAd,
1264 if(pAd->bFroceEEPROMBuffer || pAd->bEEPROMFile)
1266 DBGPRINT(RT_DEBUG_TRACE, ("Read from EEPROM Buffer\n"));
1267 NdisMoveMemory(pValue, &(pAd->EEPROMImage[Offset]), 2);
1270 eFuseReadRegisters(pAd, Offset, 2, pValue);
1275 int rtmp_ee_efuse_write16(
1276 IN RTMP_ADAPTER *pAd,
1280 if(pAd->bFroceEEPROMBuffer||pAd->bEEPROMFile)
1282 DBGPRINT(RT_DEBUG_TRACE, ("Write to EEPROM Buffer\n"));
1283 NdisMoveMemory(&(pAd->EEPROMImage[Offset]), &data, 2);
1286 eFuseWriteRegisters(pAd, Offset, 2, &data);
1291 int RtmpEfuseSupportCheck(
1292 IN RTMP_ADAPTER *pAd)
1298 eFusePhysicalReadRegisters(pAd, EFUSE_TAG, 2, &value);
1299 pAd->EFuseTag = (value & 0xff);
1304 INT set_eFuseBufferModeWriteBack_Proc(
1305 IN PRTMP_ADAPTER pAd,
1313 Enable= simple_strtol(arg, 0, 16);
1319 DBGPRINT(RT_DEBUG_TRACE, ("set_eFuseBufferMode_Proc:: Call WRITEEEPROMBUF"));
1320 eFuseWriteEeeppromBuf(pAd);
1329 ========================================================================
1331 Routine Description:
1332 Load EEPROM from bin file for eFuse mode
1335 Adapter Pointer to our adapter
1338 NDIS_STATUS_SUCCESS firmware image load ok
1339 NDIS_STATUS_FAILURE image not found
1341 IRQL = PASSIVE_LEVEL
1343 ========================================================================
1345 INT eFuseLoadEEPROM(
1346 IN PRTMP_ADAPTER pAd)
1351 RTMP_OS_FS_INFO osFSInfo;
1354 src=EFUSE_BUFFER_PATH;
1355 DBGPRINT(RT_DEBUG_TRACE, ("FileName=%s\n",src));
1358 RtmpOSFSInfoChange(&osFSInfo, TRUE);
1362 srcf = RtmpOSFileOpen(src, O_RDONLY, 0);
1363 if (IS_FILE_OPEN_ERR(srcf))
1365 DBGPRINT(RT_DEBUG_ERROR, ("--> Error %ld opening %s\n", -PTR_ERR(srcf),src));
1371 memset(pAd->EEPROMImage, 0x00, MAX_EEPROM_BIN_FILE_SIZE);
1374 retval =RtmpOSFileRead(srcf, (PSTRING)pAd->EEPROMImage, MAX_EEPROM_BIN_FILE_SIZE);
1377 RTMPSetProfileParameters(pAd, (PSTRING)pAd->EEPROMImage);
1378 retval = NDIS_STATUS_SUCCESS;
1381 DBGPRINT(RT_DEBUG_ERROR, ("Read file \"%s\" failed(errCode=%d)!\n", src, retval));
1389 DBGPRINT(RT_DEBUG_ERROR, ("--> Error src or srcf is null\n"));
1394 retval=RtmpOSFileClose(srcf);
1398 DBGPRINT(RT_DEBUG_TRACE, ("--> Error %d closing %s\n", -retval, src));
1402 RtmpOSFSInfoChange(&osFSInfo, FALSE);
1407 INT eFuseWriteEeeppromBuf(
1408 IN PRTMP_ADAPTER pAd)
1414 RTMP_OS_FS_INFO osFSInfo;
1417 src=EFUSE_BUFFER_PATH;
1418 DBGPRINT(RT_DEBUG_TRACE, ("FileName=%s\n",src));
1420 RtmpOSFSInfoChange(&osFSInfo, TRUE);
1426 srcf = RtmpOSFileOpen(src, O_WRONLY|O_CREAT, 0);
1428 if (IS_FILE_OPEN_ERR(srcf))
1430 DBGPRINT(RT_DEBUG_ERROR, ("--> Error %ld opening %s\n", -PTR_ERR(srcf),src));
1436 // The object must have a read method
1437 if (srcf->f_op && srcf->f_op->write)
1439 // The object must have a read method
1440 srcf->f_op->write(srcf, pAd->EEPROMImage, 1024, &srcf->f_pos);
1445 DBGPRINT(RT_DEBUG_ERROR, ("--> Error!! System doest not support read function\n"));
1450 RtmpOSFileWrite(srcf, (PSTRING)pAd->EEPROMImage,MAX_EEPROM_BIN_FILE_SIZE);
1458 DBGPRINT(RT_DEBUG_ERROR, ("--> Error src or srcf is null\n"));
1463 retval=RtmpOSFileClose(srcf);
1467 DBGPRINT(RT_DEBUG_TRACE, ("--> Error %d closing %s\n", -retval, src));
1470 RtmpOSFSInfoChange(&osFSInfo, FALSE);
1475 VOID eFuseGetFreeBlockCount(IN PRTMP_ADAPTER pAd,
1476 PUINT EfuseFreeBlock)
1479 USHORT LogicalAddress;
1482 DBGPRINT(RT_DEBUG_TRACE,("eFuseGetFreeBlockCount Only supports efuse Mode\n"));
1485 for (i = EFUSE_USAGE_MAP_START; i <= EFUSE_USAGE_MAP_END; i+=2)
1487 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
1488 if( (LogicalAddress & 0xff) == 0)
1490 *EfuseFreeBlock= (UCHAR) (EFUSE_USAGE_MAP_END-i+1);
1493 else if(( (LogicalAddress >> 8) & 0xff) == 0)
1495 *EfuseFreeBlock = (UCHAR) (EFUSE_USAGE_MAP_END-i);
1499 if(i == EFUSE_USAGE_MAP_END)
1500 *EfuseFreeBlock = 0;
1502 DBGPRINT(RT_DEBUG_TRACE,("eFuseGetFreeBlockCount is 0x%x\n",*EfuseFreeBlock));
1506 IN PRTMP_ADAPTER pAd)
1508 UINT EfuseFreeBlock=0;
1509 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));
1510 eFuseGetFreeBlockCount(pAd, &EfuseFreeBlock);
1511 //If the used block of efuse is less than 5. We assume the default value
1512 // of this efuse is empty and change to the buffer mode in odrder to
1513 //bring up interfaces successfully.
1514 if(EfuseFreeBlock > (EFUSE_USAGE_MAP_END-5))
1516 DBGPRINT(RT_DEBUG_ERROR, ("NVM is Efuse and the information is too less to bring up interface. Force to use EEPROM Buffer Mode\n"));
1517 pAd->bFroceEEPROMBuffer = TRUE;
1518 eFuseLoadEEPROM(pAd);
1521 pAd->bFroceEEPROMBuffer = FALSE;
1522 DBGPRINT(RT_DEBUG_TRACE, ("NVM is Efuse and force to use EEPROM Buffer Mode=%x\n",pAd->bFroceEEPROMBuffer));