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);
294 //2008/09/11:KH add to support efuse<--
297 ========================================================================
309 ========================================================================
311 UCHAR eFuseReadRegisters(
312 IN PRTMP_ADAPTER pAd,
317 EFUSE_CTRL_STRUC eFuseCtrlStruc;
319 USHORT efuseDataOffset;
322 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
324 //Step0. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
325 //Use the eeprom logical address and covert to address to block number
326 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
328 //Step1. Write EFSROM_MODE (0x580, bit7:bit6) to 0.
329 eFuseCtrlStruc.field.EFSROM_MODE = 0;
331 //Step2. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical read procedure.
332 eFuseCtrlStruc.field.EFSROM_KICK = 1;
334 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
335 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
337 //Step3. Polling EFSROM_KICK(0x580, bit30) until it become 0 again.
341 //rtmp.HwMemoryReadDword(EFUSE_CTRL, (DWORD *) &eFuseCtrlStruc, 4);
342 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
343 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
351 //if EFSROM_AOUT is not found in physical address, write 0xffff
352 if (eFuseCtrlStruc.field.EFSROM_AOUT == 0x3f)
354 for(i=0; i<Length/2; i++)
355 *(pData+2*i) = 0xffff;
359 //Step4. Read 16-byte of data from EFUSE_DATA0-3 (0x590-0x59C)
360 efuseDataOffset = EFUSE_DATA3 - (Offset & 0xC) ;
361 //data hold 4 bytes data.
362 //In RTMP_IO_READ32 will automatically execute 32-bytes swapping
363 RTMP_IO_READ32(pAd, efuseDataOffset, &data);
364 //Decide the upper 2 bytes or the bottom 2 bytes.
365 // Little-endian S | S Big-endian
366 // addr 3 2 1 0 | 0 1 2 3
367 // Ori-V D C B A | A B C D
371 //The return byte statrs from S. Therefore, the little-endian will return BA, the Big-endian will return DC.
372 //For returning the bottom 2 bytes, the Big-endian should shift right 2-bytes.
373 data = data >> (8*(Offset & 0x3));
375 NdisMoveMemory(pData, &data, Length);
378 return (UCHAR) eFuseCtrlStruc.field.EFSROM_AOUT;
383 ========================================================================
395 ========================================================================
397 VOID eFusePhysicalReadRegisters(
398 IN PRTMP_ADAPTER pAd,
403 EFUSE_CTRL_STRUC eFuseCtrlStruc;
405 USHORT efuseDataOffset;
408 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
410 //Step0. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
411 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
413 //Step1. Write EFSROM_MODE (0x580, bit7:bit6) to 1.
414 //Read in physical view
415 eFuseCtrlStruc.field.EFSROM_MODE = 1;
417 //Step2. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical read procedure.
418 eFuseCtrlStruc.field.EFSROM_KICK = 1;
420 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
421 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
423 //Step3. Polling EFSROM_KICK(0x580, bit30) until it become 0 again.
427 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
428 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
434 //Step4. Read 16-byte of data from EFUSE_DATA0-3 (0x59C-0x590)
435 //Because the size of each EFUSE_DATA is 4 Bytes, the size of address of each is 2 bits.
436 //The previous 2 bits is the EFUSE_DATA number, the last 2 bits is used to decide which bytes
437 //Decide which EFUSE_DATA to read
442 efuseDataOffset = EFUSE_DATA3 - (Offset & 0xC) ;
444 RTMP_IO_READ32(pAd, efuseDataOffset, &data);
446 data = data >> (8*(Offset & 0x3));
448 NdisMoveMemory(pData, &data, Length);
453 ========================================================================
465 ========================================================================
467 VOID eFuseReadPhysical(
468 IN PRTMP_ADAPTER pAd,
469 IN PUSHORT lpInBuffer,
470 IN ULONG nInBufferSize,
471 OUT PUSHORT lpOutBuffer,
472 IN ULONG nOutBufferSize
475 USHORT* pInBuf = (USHORT*)lpInBuffer;
476 USHORT* pOutBuf = (USHORT*)lpOutBuffer;
478 USHORT Offset = pInBuf[0]; //addr
479 USHORT Length = pInBuf[1]; //length
482 for(i=0; i<Length; i+=2)
484 eFusePhysicalReadRegisters(pAd,Offset+i, 2, &pOutBuf[i/2]);
489 ========================================================================
501 ========================================================================
504 IN PRTMP_ADAPTER pAd,
509 USHORT* pOutBuf = (USHORT*)pData;
510 NTSTATUS Status = STATUS_SUCCESS;
514 for(i=0; i<Length; i+=2)
516 EFSROM_AOUT = eFuseReadRegisters(pAd, Offset+i, 2, &pOutBuf[i/2]);
522 ========================================================================
534 ========================================================================
536 VOID eFusePhysicalWriteRegisters(
537 IN PRTMP_ADAPTER pAd,
542 EFUSE_CTRL_STRUC eFuseCtrlStruc;
544 USHORT efuseDataOffset;
545 UINT32 data, eFuseDataBuffer[4];
547 //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.
549 /////////////////////////////////////////////////////////////////
550 //read current values of 16-byte block
551 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
553 //Step0. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
554 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
556 //Step1. Write EFSROM_MODE (0x580, bit7:bit6) to 1.
557 eFuseCtrlStruc.field.EFSROM_MODE = 1;
559 //Step2. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical read procedure.
560 eFuseCtrlStruc.field.EFSROM_KICK = 1;
562 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
563 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
565 //Step3. Polling EFSROM_KICK(0x580, bit30) until it become 0 again.
569 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
571 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
577 //Step4. Read 16-byte of data from EFUSE_DATA0-3 (0x59C-0x590)
578 efuseDataOffset = EFUSE_DATA3;
581 RTMP_IO_READ32(pAd, efuseDataOffset, (PUINT32) &eFuseDataBuffer[i]);
582 efuseDataOffset -= 4;
585 //Update the value, the offset is multiple of 2, length is 2
586 efuseDataOffset = (Offset & 0xc) >> 2;
587 data = pData[0] & 0xffff;
588 //The offset should be 0x***10 or 0x***00
589 if((Offset % 4) != 0)
591 eFuseDataBuffer[efuseDataOffset] = (eFuseDataBuffer[efuseDataOffset] & 0xffff) | (data << 16);
595 eFuseDataBuffer[efuseDataOffset] = (eFuseDataBuffer[efuseDataOffset] & 0xffff0000) | data;
598 efuseDataOffset = EFUSE_DATA3;
601 RTMP_IO_WRITE32(pAd, efuseDataOffset, eFuseDataBuffer[i]);
602 efuseDataOffset -= 4;
604 /////////////////////////////////////////////////////////////////
606 //Step1. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
607 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
609 //Step2. Write EFSROM_MODE (0x580, bit7:bit6) to 3.
610 eFuseCtrlStruc.field.EFSROM_MODE = 3;
612 //Step3. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical write procedure.
613 eFuseCtrlStruc.field.EFSROM_KICK = 1;
615 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
616 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
618 //Step4. Polling EFSROM_KICK(0x580, bit30) until it become 0 again. It��s done.
622 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
624 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
633 ========================================================================
645 ========================================================================
647 NTSTATUS eFuseWriteRegisters(
648 IN PRTMP_ADAPTER pAd,
655 USHORT LogicalAddress, BlkNum = 0xffff;
658 USHORT addr,tmpaddr, InBuf[3], tmpOffset;
660 BOOLEAN bWriteSuccess = TRUE;
662 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters Offset=%x, pData=%x\n", Offset, *pData));
664 //Step 0. find the entry in the mapping table
665 //The address of EEPROM is 2-bytes alignment.
666 //The last bit is used for alignment, so it must be 0.
667 tmpOffset = Offset & 0xfffe;
668 EFSROM_AOUT = eFuseReadRegisters(pAd, tmpOffset, 2, &eFuseData);
670 if( EFSROM_AOUT == 0x3f)
671 { //find available logical address pointer
672 //the logical address does not exist, find an empty one
673 //from the first address of block 45=16*45=0x2d0 to the last address of block 47
674 //==>48*16-3(reserved)=2FC
675 for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
677 //Retrive the logical block nubmer form each logical address pointer
678 //It will access two logical address pointer each time.
679 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
680 if( (LogicalAddress & 0xff) == 0)
681 {//Not used logical address pointer
682 BlkNum = i-EFUSE_USAGE_MAP_START;
685 else if(( (LogicalAddress >> 8) & 0xff) == 0)
686 {//Not used logical address pointer
687 if (i != EFUSE_USAGE_MAP_END)
689 BlkNum = i-EFUSE_USAGE_MAP_START+1;
697 BlkNum = EFSROM_AOUT;
700 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters BlkNum = %d \n", BlkNum));
704 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters: out of free E-fuse space!!!\n"));
708 //Step 1. Save data of this block which is pointed by the avaible logical address pointer
709 // read and save the original block data
712 addr = BlkNum * 0x10 ;
718 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
720 buffer[i] = InBuf[2];
723 //Step 2. Update the data in buffer, and write the data to Efuse
724 buffer[ (Offset >> 1) % 8] = pData[0];
728 //Step 3. Write the data to Efuse
733 addr = BlkNum * 0x10 ;
737 InBuf[2] = buffer[i];
739 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 2);
744 addr = BlkNum * 0x10 ;
746 InBuf[0] = addr+(Offset % 16);
750 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 2);
753 //Step 4. Write mapping table
754 addr = EFUSE_USAGE_MAP_START+BlkNum;
763 //convert the address from 10 to 8 bit ( bit7, 6 = parity and bit5 ~ 0 = bit9~4), and write to logical map entry
766 tmpOffset |= ((~((tmpOffset & 0x01) ^ ( tmpOffset >> 1 & 0x01) ^ (tmpOffset >> 2 & 0x01) ^ (tmpOffset >> 3 & 0x01))) << 6) & 0x40;
767 tmpOffset |= ((~( (tmpOffset >> 2 & 0x01) ^ (tmpOffset >> 3 & 0x01) ^ (tmpOffset >> 4 & 0x01) ^ ( tmpOffset >> 5 & 0x01))) << 7) & 0x80;
769 // write the logical address
771 InBuf[2] = tmpOffset<<8;
773 InBuf[2] = tmpOffset;
775 eFuseWritePhysical(pAd,&InBuf[0], 6, NULL, 0);
777 //Step 5. Compare data if not the same, invalidate the mapping entry, then re-write the data until E-fuse is exhausted
778 bWriteSuccess = TRUE;
781 addr = BlkNum * 0x10 ;
787 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
789 if(buffer[i] != InBuf[2])
791 bWriteSuccess = FALSE;
796 //Step 6. invlidate mapping entry and find a free mapping entry if not succeed
799 DBGPRINT(RT_DEBUG_TRACE, ("Not bWriteSuccess BlkNum = %d\n", BlkNum));
801 // the offset of current mapping entry
802 addr = EFUSE_USAGE_MAP_START+BlkNum;
804 //find a new mapping entry
806 for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
808 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
809 if( (LogicalAddress & 0xff) == 0)
811 BlkNum = i-EFUSE_USAGE_MAP_START;
814 else if(( (LogicalAddress >> 8) & 0xff) == 0)
816 if (i != EFUSE_USAGE_MAP_END)
818 BlkNum = i+1-EFUSE_USAGE_MAP_START;
823 DBGPRINT(RT_DEBUG_TRACE, ("Not bWriteSuccess new BlkNum = %d\n", BlkNum));
826 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters: out of free E-fuse space!!!\n"));
830 //invalidate the original mapping entry if new entry is not found
838 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
840 // write the logical address
843 // Invalidate the high byte
846 if( ( (InBuf[2] >> i) & 0x01) == 0)
848 InBuf[2] |= (0x1 <<i);
855 // invalidate the low byte
858 if( ( (InBuf[2] >> i) & 0x01) == 0)
860 InBuf[2] |= (0x1 <<i);
865 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 0);
868 while(!bWriteSuccess);
874 ========================================================================
886 ========================================================================
888 VOID eFuseWritePhysical(
889 IN PRTMP_ADAPTER pAd,
896 USHORT* pInBuf = (USHORT*)lpInBuffer;
898 //USHORT* pOutBuf = (USHORT*)ioBuffer;
900 USHORT Offset = pInBuf[0]; //addr
901 USHORT Length = pInBuf[1]; //length
902 USHORT* pValueX = &pInBuf[2]; //value ...
903 // Little-endian S | S Big-endian
904 // addr 3 2 1 0 | 0 1 2 3
905 // Ori-V D C B A | A B C D
908 //Both the little and big-endian use the same sequence to write data.
909 //Therefore, we only need swap data when read the data.
910 for(i=0; i<Length; i+=2)
912 eFusePhysicalWriteRegisters(pAd, Offset+i, 2, &pValueX[i/2]);
918 ========================================================================
930 ========================================================================
933 IN PRTMP_ADAPTER pAd,
940 USHORT* pValueX = (PUSHORT) pData; //value ...
941 //The input value=3070 will be stored as following
942 // Little-endian S | S Big-endian
944 // Ori-V 30 70 | 30 70
949 //The swapping should be removed for big-endian
950 for(i=0; i<length; i+=2)
952 eFuseWriteRegisters(pAd, Offset+i, 2, &pValueX[i/2]);
959 ========================================================================
971 ========================================================================
973 INT set_eFuseGetFreeBlockCount_Proc(
974 IN PRTMP_ADAPTER pAd,
978 USHORT LogicalAddress;
979 USHORT efusefreenum=0;
982 for (i = EFUSE_USAGE_MAP_START; i <= EFUSE_USAGE_MAP_END; i+=2)
984 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
985 if( (LogicalAddress & 0xff) == 0)
987 efusefreenum= (UCHAR) (EFUSE_USAGE_MAP_END-i+1);
990 else if(( (LogicalAddress >> 8) & 0xff) == 0)
992 efusefreenum = (UCHAR) (EFUSE_USAGE_MAP_END-i);
996 if(i == EFUSE_USAGE_MAP_END)
999 printk("efuseFreeNumber is %d\n",efusefreenum);
1002 INT set_eFusedump_Proc(
1003 IN PRTMP_ADAPTER pAd,
1010 for(i =0; i<EFUSE_USAGE_MAP_END/2; i++)
1016 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
1018 printk("\nBlock %x:",i/8);
1019 printk("%04x ",InBuf[2]);
1023 INT set_eFuseLoadFromBin_Proc(
1024 IN PRTMP_ADAPTER pAd,
1032 UCHAR BinFileSize=0;
1036 BinFileSize=strlen("RT30xxEEPROM.bin");
1037 src = kmalloc(128, MEM_ALLOC_FLAG);
1038 NdisZeroMemory(src, 128);
1043 NdisMoveMemory(src, arg, strlen(arg));
1049 NdisMoveMemory(src, "RT30xxEEPROM.bin", BinFileSize);
1052 DBGPRINT(RT_DEBUG_TRACE, ("FileName=%s\n",src));
1053 buffer = kmalloc(MAX_EEPROM_BIN_FILE_SIZE, MEM_ALLOC_FLAG);
1060 PDATA=kmalloc(sizeof(USHORT)*8,MEM_ALLOC_FLAG);
1075 srcf = filp_open(src, O_RDONLY, 0);
1078 DBGPRINT(RT_DEBUG_ERROR, ("--> Error %ld opening %s\n", -PTR_ERR(srcf),src));
1083 // The object must have a read method
1084 if (srcf->f_op && srcf->f_op->read)
1086 memset(buffer, 0x00, MAX_EEPROM_BIN_FILE_SIZE);
1087 while(srcf->f_op->read(srcf, &buffer[i], 1, &srcf->f_pos)==1)
1089 DBGPRINT(RT_DEBUG_TRACE, ("%02X ",buffer[i]));
1091 DBGPRINT(RT_DEBUG_TRACE, ("\n"));
1093 if(i>=MAX_EEPROM_BIN_FILE_SIZE)
1095 DBGPRINT(RT_DEBUG_ERROR, ("--> Error %ld reading %s, The file is too large[1024]\n", -PTR_ERR(srcf),src));
1105 DBGPRINT(RT_DEBUG_ERROR, ("--> Error!! System doest not support read function\n"));
1117 DBGPRINT(RT_DEBUG_ERROR, ("--> Error src or srcf is null\n"));
1125 retval=filp_close(srcf,NULL);
1129 DBGPRINT(RT_DEBUG_TRACE, ("--> Error %d closing %s\n", -retval, src));
1135 DBGPRINT(RT_DEBUG_TRACE, ("%02X ",buffer[j]));
1137 PDATA[j/2%8]=((buffer[j]<<8)&0xff00)|(buffer[j-1]&0xff);
1148 DBGPRINT(RT_DEBUG_TRACE, (" result=%02X,blk=%02x\n",k,j/16));
1151 eFuseWriteRegistersFromBin(pAd,(USHORT)j-15, 16, PDATA);
1154 if(eFuseReadRegisters(pAd,j, 2,(PUSHORT)&DATA)!=0x3f)
1155 eFuseWriteRegistersFromBin(pAd,(USHORT)j-15, 16, PDATA);
1159 printk("%04x ",PDATA[l]);
1162 NdisZeroMemory(PDATA,16);
1177 NTSTATUS eFuseWriteRegistersFromBin(
1178 IN PRTMP_ADAPTER pAd,
1185 USHORT LogicalAddress, BlkNum = 0xffff;
1186 UCHAR EFSROM_AOUT,Loop=0;
1187 EFUSE_CTRL_STRUC eFuseCtrlStruc;
1188 USHORT efuseDataOffset;
1189 UINT32 data,tempbuffer;
1190 USHORT addr,tmpaddr, InBuf[3], tmpOffset;
1192 BOOLEAN bWriteSuccess = TRUE;
1193 BOOLEAN bNotWrite=TRUE;
1194 BOOLEAN bAllocateNewBlk=TRUE;
1196 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin Offset=%x, pData=%04x:%04x:%04x:%04x\n", Offset, *pData,*(pData+1),*(pData+2),*(pData+3)));
1200 //Step 0. find the entry in the mapping table
1201 //The address of EEPROM is 2-bytes alignment.
1202 //The last bit is used for alignment, so it must be 0.
1204 tmpOffset = Offset & 0xfffe;
1205 EFSROM_AOUT = eFuseReadRegisters(pAd, tmpOffset, 2, &eFuseData);
1207 if( EFSROM_AOUT == 0x3f)
1208 { //find available logical address pointer
1209 //the logical address does not exist, find an empty one
1210 //from the first address of block 45=16*45=0x2d0 to the last address of block 47
1211 //==>48*16-3(reserved)=2FC
1212 bAllocateNewBlk=TRUE;
1213 for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
1215 //Retrive the logical block nubmer form each logical address pointer
1216 //It will access two logical address pointer each time.
1217 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
1218 if( (LogicalAddress & 0xff) == 0)
1219 {//Not used logical address pointer
1220 BlkNum = i-EFUSE_USAGE_MAP_START;
1223 else if(( (LogicalAddress >> 8) & 0xff) == 0)
1224 {//Not used logical address pointer
1225 if (i != EFUSE_USAGE_MAP_END)
1227 BlkNum = i-EFUSE_USAGE_MAP_START+1;
1235 bAllocateNewBlk=FALSE;
1236 BlkNum = EFSROM_AOUT;
1239 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters BlkNum = %d \n", BlkNum));
1241 if(BlkNum == 0xffff)
1243 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters: out of free E-fuse space!!!\n"));
1247 //If the block is not existing in mapping table, create one
1248 //and write down the 16-bytes data to the new block
1251 DBGPRINT(RT_DEBUG_TRACE, ("Allocate New Blk\n"));
1252 efuseDataOffset = EFUSE_DATA3;
1255 DBGPRINT(RT_DEBUG_TRACE, ("Allocate New Blk, Data%d=%04x%04x\n",3-i,pData[2*i+1],pData[2*i]));
1256 tempbuffer=((pData[2*i+1]<<16)&0xffff0000)|pData[2*i];
1259 RTMP_IO_WRITE32(pAd, efuseDataOffset,tempbuffer);
1260 efuseDataOffset -= 4;
1263 /////////////////////////////////////////////////////////////////
1265 //Step1.1.1. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
1266 eFuseCtrlStruc.field.EFSROM_AIN = BlkNum* 0x10 ;
1268 //Step1.1.2. Write EFSROM_MODE (0x580, bit7:bit6) to 3.
1269 eFuseCtrlStruc.field.EFSROM_MODE = 3;
1271 //Step1.1.3. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical write procedure.
1272 eFuseCtrlStruc.field.EFSROM_KICK = 1;
1274 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
1276 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
1278 //Step1.1.4. Polling EFSROM_KICK(0x580, bit30) until it become 0 again. It��s done.
1282 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
1284 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
1294 //If the same logical number is existing, check if the writting data and the data
1295 //saving in this block are the same.
1296 /////////////////////////////////////////////////////////////////
1297 //read current values of 16-byte block
1298 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
1300 //Step1.2.0. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
1301 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
1303 //Step1.2.1. Write EFSROM_MODE (0x580, bit7:bit6) to 1.
1304 eFuseCtrlStruc.field.EFSROM_MODE = 0;
1306 //Step1.2.2. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical read procedure.
1307 eFuseCtrlStruc.field.EFSROM_KICK = 1;
1309 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
1310 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
1312 //Step1.2.3. Polling EFSROM_KICK(0x580, bit30) until it become 0 again.
1316 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
1318 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
1324 //Step1.2.4. Read 16-byte of data from EFUSE_DATA0-3 (0x59C-0x590)
1325 efuseDataOffset = EFUSE_DATA3;
1328 RTMP_IO_READ32(pAd, efuseDataOffset, (PUINT32) &buffer[i]);
1329 efuseDataOffset -= 4;
1331 //Step1.2.5. Check if the data of efuse and the writing data are the same.
1334 tempbuffer=((pData[2*i+1]<<16)&0xffff0000)|pData[2*i];
1335 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));
1337 if(((buffer[i]&0xffff0000)==(pData[2*i+1]<<16))&&((buffer[i]&0xffff)==pData[2*i]))
1347 printk("The data is not the same\n");
1351 addr = BlkNum * 0x10 ;
1353 InBuf[0] = addr+2*i;
1355 InBuf[2] = pData[i];
1357 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 2);
1367 //Step 2. Write mapping table
1368 addr = EFUSE_USAGE_MAP_START+BlkNum;
1377 //convert the address from 10 to 8 bit ( bit7, 6 = parity and bit5 ~ 0 = bit9~4), and write to logical map entry
1380 tmpOffset |= ((~((tmpOffset & 0x01) ^ ( tmpOffset >> 1 & 0x01) ^ (tmpOffset >> 2 & 0x01) ^ (tmpOffset >> 3 & 0x01))) << 6) & 0x40;
1381 tmpOffset |= ((~( (tmpOffset >> 2 & 0x01) ^ (tmpOffset >> 3 & 0x01) ^ (tmpOffset >> 4 & 0x01) ^ ( tmpOffset >> 5 & 0x01))) << 7) & 0x80;
1383 // write the logical address
1385 InBuf[2] = tmpOffset<<8;
1387 InBuf[2] = tmpOffset;
1389 eFuseWritePhysical(pAd,&InBuf[0], 6, NULL, 0);
1391 //Step 3. Compare data if not the same, invalidate the mapping entry, then re-write the data until E-fuse is exhausted
1392 bWriteSuccess = TRUE;
1395 addr = BlkNum * 0x10 ;
1397 InBuf[0] = addr+2*i;
1401 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
1402 DBGPRINT(RT_DEBUG_TRACE, ("addr=%x, buffer[i]=%x,InBuf[2]=%x\n",InBuf[0],pData[i],InBuf[2]));
1403 if(pData[i] != InBuf[2])
1405 bWriteSuccess = FALSE;
1410 //Step 4. invlidate mapping entry and find a free mapping entry if not succeed
1412 if (!bWriteSuccess&&Loop<2)
1414 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin::Not bWriteSuccess BlkNum = %d\n", BlkNum));
1416 // the offset of current mapping entry
1417 addr = EFUSE_USAGE_MAP_START+BlkNum;
1419 //find a new mapping entry
1421 for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
1423 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
1424 if( (LogicalAddress & 0xff) == 0)
1426 BlkNum = i-EFUSE_USAGE_MAP_START;
1429 else if(( (LogicalAddress >> 8) & 0xff) == 0)
1431 if (i != EFUSE_USAGE_MAP_END)
1433 BlkNum = i+1-EFUSE_USAGE_MAP_START;
1438 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin::Not bWriteSuccess new BlkNum = %d\n", BlkNum));
1439 if(BlkNum == 0xffff)
1441 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin: out of free E-fuse space!!!\n"));
1445 //invalidate the original mapping entry if new entry is not found
1453 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
1455 // write the logical address
1458 // Invalidate the high byte
1459 for (i=8; i<15; i++)
1461 if( ( (InBuf[2] >> i) & 0x01) == 0)
1463 InBuf[2] |= (0x1 <<i);
1470 // invalidate the low byte
1473 if( ( (InBuf[2] >> i) & 0x01) == 0)
1475 InBuf[2] |= (0x1 <<i);
1480 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 0);
1484 while(!bWriteSuccess&&Loop<2);
1490 //2008/09/11:KH add to support efuse-->