Staging: rt2870: prepare for rt{28,30}70/common/*.[ch] merge
[linux-2.6] / drivers / staging / rt2870 / common / eeprom.c
1 /*
2  *************************************************************************
3  * Ralink Tech Inc.
4  * 5F., No.36, Taiyuan St., Jhubei City,
5  * Hsinchu County 302,
6  * Taiwan, R.O.C.
7  *
8  * (c) Copyright 2002-2007, Ralink Technology, Inc.
9  *
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.                                   *
14  *                                                                       *
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.                          *
19  *                                                                       *
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.             *
24  *                                                                       *
25  *************************************************************************
26
27         Module Name:
28         eeprom.c
29
30         Abstract:
31
32         Revision History:
33         Who                     When                    What
34         --------        ----------              ----------------------------------------------
35         Name            Date                    Modification logs
36 */
37 #include "../rt_config.h"
38
39 // IRQL = PASSIVE_LEVEL
40 VOID RaiseClock(
41     IN  PRTMP_ADAPTER   pAd,
42     IN  UINT32 *x)
43 {
44     *x = *x | EESK;
45     RTMP_IO_WRITE32(pAd, E2PROM_CSR, *x);
46     RTMPusecDelay(1);                           // Max frequency = 1MHz in Spec. definition
47 }
48
49 // IRQL = PASSIVE_LEVEL
50 VOID LowerClock(
51     IN  PRTMP_ADAPTER   pAd,
52     IN  UINT32 *x)
53 {
54     *x = *x & ~EESK;
55     RTMP_IO_WRITE32(pAd, E2PROM_CSR, *x);
56     RTMPusecDelay(1);
57 }
58
59 // IRQL = PASSIVE_LEVEL
60 USHORT ShiftInBits(
61     IN  PRTMP_ADAPTER   pAd)
62 {
63     UINT32              x,i;
64         USHORT      data=0;
65
66     RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
67
68     x &= ~( EEDO | EEDI);
69
70     for(i=0; i<16; i++)
71     {
72         data = data << 1;
73         RaiseClock(pAd, &x);
74
75         RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
76 #ifdef RT30xx
77                 LowerClock(pAd, &x); //prevent read failed
78 #endif
79         x &= ~(EEDI);
80         if(x & EEDO)
81             data |= 1;
82
83 #ifndef RT30xx
84         LowerClock(pAd, &x);
85 #endif
86     }
87
88     return data;
89 }
90
91 // IRQL = PASSIVE_LEVEL
92 VOID ShiftOutBits(
93     IN  PRTMP_ADAPTER   pAd,
94     IN  USHORT data,
95     IN  USHORT count)
96 {
97     UINT32       x,mask;
98
99     mask = 0x01 << (count - 1);
100     RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
101
102     x &= ~(EEDO | EEDI);
103
104     do
105     {
106         x &= ~EEDI;
107         if(data & mask)         x |= EEDI;
108
109         RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
110
111         RaiseClock(pAd, &x);
112         LowerClock(pAd, &x);
113
114         mask = mask >> 1;
115     } while(mask);
116
117     x &= ~EEDI;
118     RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
119 }
120
121 // IRQL = PASSIVE_LEVEL
122 VOID EEpromCleanup(
123     IN  PRTMP_ADAPTER   pAd)
124 {
125     UINT32 x;
126
127     RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
128
129     x &= ~(EECS | EEDI);
130     RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
131
132     RaiseClock(pAd, &x);
133     LowerClock(pAd, &x);
134 }
135
136 VOID EWEN(
137         IN      PRTMP_ADAPTER   pAd)
138 {
139     UINT32      x;
140
141     // reset bits and set EECS
142     RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
143     x &= ~(EEDI | EEDO | EESK);
144     x |= EECS;
145     RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
146
147         // kick a pulse
148         RaiseClock(pAd, &x);
149         LowerClock(pAd, &x);
150
151     // output the read_opcode and six pulse in that order
152     ShiftOutBits(pAd, EEPROM_EWEN_OPCODE, 5);
153     ShiftOutBits(pAd, 0, 6);
154
155     EEpromCleanup(pAd);
156 }
157
158 VOID EWDS(
159         IN      PRTMP_ADAPTER   pAd)
160 {
161     UINT32      x;
162
163     // reset bits and set EECS
164     RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
165     x &= ~(EEDI | EEDO | EESK);
166     x |= EECS;
167     RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
168
169         // kick a pulse
170         RaiseClock(pAd, &x);
171         LowerClock(pAd, &x);
172
173     // output the read_opcode and six pulse in that order
174     ShiftOutBits(pAd, EEPROM_EWDS_OPCODE, 5);
175     ShiftOutBits(pAd, 0, 6);
176
177     EEpromCleanup(pAd);
178 }
179
180 // IRQL = PASSIVE_LEVEL
181 USHORT RTMP_EEPROM_READ16(
182     IN  PRTMP_ADAPTER   pAd,
183     IN  USHORT Offset)
184 {
185     UINT32              x;
186     USHORT              data;
187
188 #ifdef RT30xx
189         if (pAd->NicConfig2.field.AntDiversity)
190     {
191         pAd->EepromAccess = TRUE;
192     }
193 //2008/09/11:KH add to support efuse<--
194 //2008/09/11:KH add to support efuse-->
195 {
196 #endif
197     Offset /= 2;
198     // reset bits and set EECS
199     RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
200     x &= ~(EEDI | EEDO | EESK);
201     x |= EECS;
202     RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
203
204 #ifdef RT30xx
205         // patch can not access e-Fuse issue
206     if (!IS_RT3090(pAd))
207     {
208 #endif
209         // kick a pulse
210         RaiseClock(pAd, &x);
211         LowerClock(pAd, &x);
212 #ifdef RT30xx
213     }
214 #endif
215
216     // output the read_opcode and register number in that order
217     ShiftOutBits(pAd, EEPROM_READ_OPCODE, 3);
218     ShiftOutBits(pAd, Offset, pAd->EEPROMAddressNum);
219
220     // Now read the data (16 bits) in from the selected EEPROM word
221     data = ShiftInBits(pAd);
222
223     EEpromCleanup(pAd);
224
225 #ifdef RT30xx
226         // Antenna and EEPROM access are both using EESK pin,
227     // Therefor we should avoid accessing EESK at the same time
228     // Then restore antenna after EEPROM access
229         if ((pAd->NicConfig2.field.AntDiversity) || (pAd->RfIcType == RFIC_3020))
230     {
231             pAd->EepromAccess = FALSE;
232             AsicSetRxAnt(pAd, pAd->RxAnt.Pair1PrimaryRxAnt);
233     }
234 }
235 #endif
236     return data;
237 }       //ReadEEprom
238
239 VOID RTMP_EEPROM_WRITE16(
240     IN  PRTMP_ADAPTER   pAd,
241     IN  USHORT Offset,
242     IN  USHORT Data)
243 {
244     UINT32 x;
245
246 #ifdef RT30xx
247         if (pAd->NicConfig2.field.AntDiversity)
248     {
249         pAd->EepromAccess = TRUE;
250     }
251         //2008/09/11:KH add to support efuse<--
252 //2008/09/11:KH add to support efuse-->
253         {
254 #endif
255         Offset /= 2;
256
257         EWEN(pAd);
258
259     // reset bits and set EECS
260     RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
261     x &= ~(EEDI | EEDO | EESK);
262     x |= EECS;
263     RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
264
265 #ifdef RT30xx
266         // patch can not access e-Fuse issue
267     if (!IS_RT3090(pAd))
268     {
269 #endif
270         // kick a pulse
271         RaiseClock(pAd, &x);
272         LowerClock(pAd, &x);
273 #ifdef RT30xx
274     }
275 #endif
276
277     // output the read_opcode ,register number and data in that order
278     ShiftOutBits(pAd, EEPROM_WRITE_OPCODE, 3);
279     ShiftOutBits(pAd, Offset, pAd->EEPROMAddressNum);
280         ShiftOutBits(pAd, Data, 16);            // 16-bit access
281
282     // read DO status
283     RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
284
285         EEpromCleanup(pAd);
286
287         RTMPusecDelay(10000);   //delay for twp(MAX)=10ms
288
289         EWDS(pAd);
290
291     EEpromCleanup(pAd);
292
293 #ifdef RT30xx
294         // Antenna and EEPROM access are both using EESK pin,
295     // Therefor we should avoid accessing EESK at the same time
296     // Then restore antenna after EEPROM access
297         if ((pAd->NicConfig2.field.AntDiversity) || (pAd->RfIcType == RFIC_3020))
298     {
299             pAd->EepromAccess = FALSE;
300             AsicSetRxAnt(pAd, pAd->RxAnt.Pair1PrimaryRxAnt);
301     }
302 }
303 #endif
304 }
305
306 //2008/09/11:KH add to support efuse<--
307 #ifdef RT30xx
308 /*
309         ========================================================================
310
311         Routine Description:
312
313         Arguments:
314
315         Return Value:
316
317         IRQL =
318
319         Note:
320
321         ========================================================================
322 */
323 UCHAR eFuseReadRegisters(
324         IN      PRTMP_ADAPTER   pAd,
325         IN      USHORT Offset,
326         IN      USHORT Length,
327         OUT     USHORT* pData)
328 {
329         EFUSE_CTRL_STRUC                eFuseCtrlStruc;
330         int     i;
331         USHORT  efuseDataOffset;
332         UINT32  data;
333
334         RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
335
336         //Step0. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
337         //Use the eeprom logical address and covert to address to block number
338         eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
339
340         //Step1. Write EFSROM_MODE (0x580, bit7:bit6) to 0.
341         eFuseCtrlStruc.field.EFSROM_MODE = 0;
342
343         //Step2. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical read procedure.
344         eFuseCtrlStruc.field.EFSROM_KICK = 1;
345
346         NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
347         RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
348
349         //Step3. Polling EFSROM_KICK(0x580, bit30) until it become 0 again.
350         i = 0;
351         while(i < 100)
352         {
353                 //rtmp.HwMemoryReadDword(EFUSE_CTRL, (DWORD *) &eFuseCtrlStruc, 4);
354                 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
355                 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
356                 {
357                         break;
358                 }
359                 RTMPusecDelay(2);
360                 i++;
361         }
362
363         //if EFSROM_AOUT is not found in physical address, write 0xffff
364         if (eFuseCtrlStruc.field.EFSROM_AOUT == 0x3f)
365         {
366                 for(i=0; i<Length/2; i++)
367                         *(pData+2*i) = 0xffff;
368         }
369         else
370         {
371                 //Step4. Read 16-byte of data from EFUSE_DATA0-3 (0x590-0x59C)
372                 efuseDataOffset =  EFUSE_DATA3 - (Offset & 0xC)  ;
373                 //data hold 4 bytes data.
374                 //In RTMP_IO_READ32 will automatically execute 32-bytes swapping
375                 RTMP_IO_READ32(pAd, efuseDataOffset, &data);
376                 //Decide the upper 2 bytes or the bottom 2 bytes.
377                 // Little-endian                S       |       S       Big-endian
378                 // addr 3       2       1       0       |       0       1       2       3
379                 // Ori-V        D       C       B       A       |       A       B       C       D
380                 //After swapping
381                 //              D       C       B       A       |       D       C       B       A
382                 //Return 2-bytes
383                 //The return byte statrs from S. Therefore, the little-endian will return BA, the Big-endian will return DC.
384                 //For returning the bottom 2 bytes, the Big-endian should shift right 2-bytes.
385                 data = data >> (8*(Offset & 0x3));
386
387                 NdisMoveMemory(pData, &data, Length);
388         }
389
390         return (UCHAR) eFuseCtrlStruc.field.EFSROM_AOUT;
391
392 }
393
394 /*
395         ========================================================================
396
397         Routine Description:
398
399         Arguments:
400
401         Return Value:
402
403         IRQL =
404
405         Note:
406
407         ========================================================================
408 */
409 VOID eFusePhysicalReadRegisters(
410         IN      PRTMP_ADAPTER   pAd,
411         IN      USHORT Offset,
412         IN      USHORT Length,
413         OUT     USHORT* pData)
414 {
415         EFUSE_CTRL_STRUC                eFuseCtrlStruc;
416         int     i;
417         USHORT  efuseDataOffset;
418         UINT32  data;
419
420         RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
421
422         //Step0. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
423         eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
424
425         //Step1. Write EFSROM_MODE (0x580, bit7:bit6) to 1.
426         //Read in physical view
427         eFuseCtrlStruc.field.EFSROM_MODE = 1;
428
429         //Step2. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical read procedure.
430         eFuseCtrlStruc.field.EFSROM_KICK = 1;
431
432         NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
433         RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
434
435         //Step3. Polling EFSROM_KICK(0x580, bit30) until it become 0 again.
436         i = 0;
437         while(i < 100)
438         {
439                 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
440                 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
441                         break;
442                 RTMPusecDelay(2);
443                 i++;
444         }
445
446         //Step4. Read 16-byte of data from EFUSE_DATA0-3 (0x59C-0x590)
447         //Because the size of each EFUSE_DATA is 4 Bytes, the size of address of each is 2 bits.
448         //The previous 2 bits is the EFUSE_DATA number, the last 2 bits is used to decide which bytes
449         //Decide which EFUSE_DATA to read
450         //590:F E D C
451         //594:B A 9 8
452         //598:7 6 5 4
453         //59C:3 2 1 0
454         efuseDataOffset =  EFUSE_DATA3 - (Offset & 0xC)  ;
455
456         RTMP_IO_READ32(pAd, efuseDataOffset, &data);
457
458         data = data >> (8*(Offset & 0x3));
459
460         NdisMoveMemory(pData, &data, Length);
461
462 }
463
464 /*
465         ========================================================================
466
467         Routine Description:
468
469         Arguments:
470
471         Return Value:
472
473         IRQL =
474
475         Note:
476
477         ========================================================================
478 */
479 VOID eFuseReadPhysical(
480         IN      PRTMP_ADAPTER   pAd,
481         IN      PUSHORT lpInBuffer,
482         IN      ULONG nInBufferSize,
483         OUT     PUSHORT lpOutBuffer,
484         IN      ULONG nOutBufferSize
485 )
486 {
487         USHORT* pInBuf = (USHORT*)lpInBuffer;
488         USHORT* pOutBuf = (USHORT*)lpOutBuffer;
489
490         USHORT Offset = pInBuf[0];                                      //addr
491         USHORT Length = pInBuf[1];                                      //length
492         int             i;
493
494         for(i=0; i<Length; i+=2)
495         {
496                 eFusePhysicalReadRegisters(pAd,Offset+i, 2, &pOutBuf[i/2]);
497         }
498 }
499
500 /*
501         ========================================================================
502
503         Routine Description:
504
505         Arguments:
506
507         Return Value:
508
509         IRQL =
510
511         Note:
512
513         ========================================================================
514 */
515 NTSTATUS eFuseRead(
516         IN      PRTMP_ADAPTER   pAd,
517         IN      USHORT                  Offset,
518         OUT     PUCHAR                  pData,
519         IN      USHORT                  Length)
520 {
521         USHORT* pOutBuf = (USHORT*)pData;
522         NTSTATUS        Status = STATUS_SUCCESS;
523         UCHAR   EFSROM_AOUT;
524         int     i;
525
526         for(i=0; i<Length; i+=2)
527         {
528                 EFSROM_AOUT = eFuseReadRegisters(pAd, Offset+i, 2, &pOutBuf[i/2]);
529         }
530         return Status;
531 }
532
533 /*
534         ========================================================================
535
536         Routine Description:
537
538         Arguments:
539
540         Return Value:
541
542         IRQL =
543
544         Note:
545
546         ========================================================================
547 */
548 VOID eFusePhysicalWriteRegisters(
549         IN      PRTMP_ADAPTER   pAd,
550         IN      USHORT Offset,
551         IN      USHORT Length,
552         OUT     USHORT* pData)
553 {
554         EFUSE_CTRL_STRUC                eFuseCtrlStruc;
555         int     i;
556         USHORT  efuseDataOffset;
557         UINT32  data, eFuseDataBuffer[4];
558
559         //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.
560
561         /////////////////////////////////////////////////////////////////
562         //read current values of 16-byte block
563         RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
564
565         //Step0. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
566         eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
567
568         //Step1. Write EFSROM_MODE (0x580, bit7:bit6) to 1.
569         eFuseCtrlStruc.field.EFSROM_MODE = 1;
570
571         //Step2. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical read procedure.
572         eFuseCtrlStruc.field.EFSROM_KICK = 1;
573
574         NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
575         RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
576
577         //Step3. Polling EFSROM_KICK(0x580, bit30) until it become 0 again.
578         i = 0;
579         while(i < 100)
580         {
581                 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
582
583                 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
584                         break;
585                 RTMPusecDelay(2);
586                 i++;
587         }
588
589         //Step4. Read 16-byte of data from EFUSE_DATA0-3 (0x59C-0x590)
590         efuseDataOffset =  EFUSE_DATA3;
591         for(i=0; i< 4; i++)
592         {
593                 RTMP_IO_READ32(pAd, efuseDataOffset, (PUINT32) &eFuseDataBuffer[i]);
594                 efuseDataOffset -=  4;
595         }
596
597         //Update the value, the offset is multiple of 2, length is 2
598         efuseDataOffset = (Offset & 0xc) >> 2;
599         data = pData[0] & 0xffff;
600         //The offset should be 0x***10 or 0x***00
601         if((Offset % 4) != 0)
602         {
603                 eFuseDataBuffer[efuseDataOffset] = (eFuseDataBuffer[efuseDataOffset] & 0xffff) | (data << 16);
604         }
605         else
606         {
607                 eFuseDataBuffer[efuseDataOffset] = (eFuseDataBuffer[efuseDataOffset] & 0xffff0000) | data;
608         }
609
610         efuseDataOffset =  EFUSE_DATA3;
611         for(i=0; i< 4; i++)
612         {
613                 RTMP_IO_WRITE32(pAd, efuseDataOffset, eFuseDataBuffer[i]);
614                 efuseDataOffset -= 4;
615         }
616         /////////////////////////////////////////////////////////////////
617
618         //Step1. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
619         eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
620
621         //Step2. Write EFSROM_MODE (0x580, bit7:bit6) to 3.
622         eFuseCtrlStruc.field.EFSROM_MODE = 3;
623
624         //Step3. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical write procedure.
625         eFuseCtrlStruc.field.EFSROM_KICK = 1;
626
627         NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
628         RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
629
630         //Step4. Polling EFSROM_KICK(0x580, bit30) until it become 0 again. It¡¦s done.
631         i = 0;
632         while(i < 100)
633         {
634                 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
635
636                 if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
637                         break;
638
639                 RTMPusecDelay(2);
640                 i++;
641         }
642 }
643
644 /*
645         ========================================================================
646
647         Routine Description:
648
649         Arguments:
650
651         Return Value:
652
653         IRQL =
654
655         Note:
656
657         ========================================================================
658 */
659 NTSTATUS eFuseWriteRegisters(
660         IN      PRTMP_ADAPTER   pAd,
661         IN      USHORT Offset,
662         IN      USHORT Length,
663         IN      USHORT* pData)
664 {
665         USHORT  i;
666         USHORT  eFuseData;
667         USHORT  LogicalAddress, BlkNum = 0xffff;
668         UCHAR   EFSROM_AOUT;
669
670         USHORT addr,tmpaddr, InBuf[3], tmpOffset;
671         USHORT buffer[8];
672         BOOLEAN         bWriteSuccess = TRUE;
673
674         DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters Offset=%x, pData=%x\n", Offset, *pData));
675
676         //Step 0. find the entry in the mapping table
677         //The address of EEPROM is 2-bytes alignment.
678         //The last bit is used for alignment, so it must be 0.
679         tmpOffset = Offset & 0xfffe;
680         EFSROM_AOUT = eFuseReadRegisters(pAd, tmpOffset, 2, &eFuseData);
681
682         if( EFSROM_AOUT == 0x3f)
683         {       //find available logical address pointer
684                 //the logical address does not exist, find an empty one
685                 //from the first address of block 45=16*45=0x2d0 to the last address of block 47
686                 //==>48*16-3(reserved)=2FC
687                 for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
688                 {
689                         //Retrive the logical block nubmer form each logical address pointer
690                         //It will access two logical address pointer each time.
691                         eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
692                         if( (LogicalAddress & 0xff) == 0)
693                         {//Not used logical address pointer
694                                 BlkNum = i-EFUSE_USAGE_MAP_START;
695                                 break;
696                         }
697                         else if(( (LogicalAddress >> 8) & 0xff) == 0)
698                         {//Not used logical address pointer
699                                 if (i != EFUSE_USAGE_MAP_END)
700                                 {
701                                         BlkNum = i-EFUSE_USAGE_MAP_START+1;
702                                 }
703                                 break;
704                         }
705                 }
706         }
707         else
708         {
709                 BlkNum = EFSROM_AOUT;
710         }
711
712         DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters BlkNum = %d \n", BlkNum));
713
714         if(BlkNum == 0xffff)
715         {
716                 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters: out of free E-fuse space!!!\n"));
717                 return FALSE;
718         }
719
720         //Step 1. Save data of this block       which is pointed by the avaible logical address pointer
721         // read and save the original block data
722         for(i =0; i<8; i++)
723         {
724                 addr = BlkNum * 0x10 ;
725
726                 InBuf[0] = addr+2*i;
727                 InBuf[1] = 2;
728                 InBuf[2] = 0x0;
729
730                 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
731
732                 buffer[i] = InBuf[2];
733         }
734
735         //Step 2. Update the data in buffer, and write the data to Efuse
736         buffer[ (Offset >> 1) % 8] = pData[0];
737
738         do
739         {
740                 //Step 3. Write the data to Efuse
741                 if(!bWriteSuccess)
742                 {
743                         for(i =0; i<8; i++)
744                         {
745                                 addr = BlkNum * 0x10 ;
746
747                                 InBuf[0] = addr+2*i;
748                                 InBuf[1] = 2;
749                                 InBuf[2] = buffer[i];
750
751                                 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 2);
752                         }
753                 }
754                 else
755                 {
756                                 addr = BlkNum * 0x10 ;
757
758                                 InBuf[0] = addr+(Offset % 16);
759                                 InBuf[1] = 2;
760                                 InBuf[2] = pData[0];
761
762                                 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 2);
763                 }
764
765                 //Step 4. Write mapping table
766                 addr = EFUSE_USAGE_MAP_START+BlkNum;
767
768                 tmpaddr = addr;
769
770                 if(addr % 2 != 0)
771                         addr = addr -1;
772                 InBuf[0] = addr;
773                 InBuf[1] = 2;
774
775                 //convert the address from 10 to 8 bit ( bit7, 6 = parity and bit5 ~ 0 = bit9~4), and write to logical map entry
776                 tmpOffset = Offset;
777                 tmpOffset >>= 4;
778                 tmpOffset |= ((~((tmpOffset & 0x01) ^ ( tmpOffset >> 1 & 0x01) ^  (tmpOffset >> 2 & 0x01) ^  (tmpOffset >> 3 & 0x01))) << 6) & 0x40;
779                 tmpOffset |= ((~( (tmpOffset >> 2 & 0x01) ^ (tmpOffset >> 3 & 0x01) ^ (tmpOffset >> 4 & 0x01) ^ ( tmpOffset >> 5 & 0x01))) << 7) & 0x80;
780
781                 // write the logical address
782                 if(tmpaddr%2 != 0)
783                         InBuf[2] = tmpOffset<<8;
784                 else
785                         InBuf[2] = tmpOffset;
786
787                 eFuseWritePhysical(pAd,&InBuf[0], 6, NULL, 0);
788
789                 //Step 5. Compare data if not the same, invalidate the mapping entry, then re-write the data until E-fuse is exhausted
790                 bWriteSuccess = TRUE;
791                 for(i =0; i<8; i++)
792                 {
793                         addr = BlkNum * 0x10 ;
794
795                         InBuf[0] = addr+2*i;
796                         InBuf[1] = 2;
797                         InBuf[2] = 0x0;
798
799                         eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
800
801                         if(buffer[i] != InBuf[2])
802                         {
803                                 bWriteSuccess = FALSE;
804                                 break;
805                         }
806                 }
807
808                 //Step 6. invlidate mapping entry and find a free mapping entry if not succeed
809                 if (!bWriteSuccess)
810                 {
811                         DBGPRINT(RT_DEBUG_TRACE, ("Not bWriteSuccess BlkNum = %d\n", BlkNum));
812
813                         // the offset of current mapping entry
814                         addr = EFUSE_USAGE_MAP_START+BlkNum;
815
816                         //find a new mapping entry
817                         BlkNum = 0xffff;
818                         for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
819                         {
820                                 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
821                                 if( (LogicalAddress & 0xff) == 0)
822                                 {
823                                         BlkNum = i-EFUSE_USAGE_MAP_START;
824                                         break;
825                                 }
826                                 else if(( (LogicalAddress >> 8) & 0xff) == 0)
827                                 {
828                                         if (i != EFUSE_USAGE_MAP_END)
829                                         {
830                                                 BlkNum = i+1-EFUSE_USAGE_MAP_START;
831                                         }
832                                         break;
833                                 }
834                         }
835                         DBGPRINT(RT_DEBUG_TRACE, ("Not bWriteSuccess new BlkNum = %d\n", BlkNum));
836                         if(BlkNum == 0xffff)
837                         {
838                                 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters: out of free E-fuse space!!!\n"));
839                                 return FALSE;
840                         }
841
842                         //invalidate the original mapping entry if new entry is not found
843                         tmpaddr = addr;
844
845                         if(addr % 2 != 0)
846                                 addr = addr -1;
847                         InBuf[0] = addr;
848                         InBuf[1] = 2;
849
850                         eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
851
852                         // write the logical address
853                         if(tmpaddr%2 != 0)
854                         {
855                                 // Invalidate the high byte
856                                 for (i=8; i<15; i++)
857                                 {
858                                         if( ( (InBuf[2] >> i) & 0x01) == 0)
859                                         {
860                                                 InBuf[2] |= (0x1 <<i);
861                                                 break;
862                                         }
863                                 }
864                         }
865                         else
866                         {
867                                 // invalidate the low byte
868                                 for (i=0; i<8; i++)
869                                 {
870                                         if( ( (InBuf[2] >> i) & 0x01) == 0)
871                                         {
872                                                 InBuf[2] |= (0x1 <<i);
873                                                 break;
874                                         }
875                                 }
876                         }
877                         eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 0);
878                 }
879         }
880         while(!bWriteSuccess);
881
882         return TRUE;
883 }
884
885 /*
886         ========================================================================
887
888         Routine Description:
889
890         Arguments:
891
892         Return Value:
893
894         IRQL =
895
896         Note:
897
898         ========================================================================
899 */
900 VOID eFuseWritePhysical(
901         IN      PRTMP_ADAPTER   pAd,
902         PUSHORT lpInBuffer,
903         ULONG nInBufferSize,
904         PUCHAR lpOutBuffer,
905         ULONG nOutBufferSize
906 )
907 {
908         USHORT* pInBuf = (USHORT*)lpInBuffer;
909         int             i;
910         //USHORT* pOutBuf = (USHORT*)ioBuffer;
911
912         USHORT Offset = pInBuf[0];                                      //addr
913         USHORT Length = pInBuf[1];                                      //length
914         USHORT* pValueX = &pInBuf[2];                           //value ...
915                 // Little-endian                S       |       S       Big-endian
916                 // addr 3       2       1       0       |       0       1       2       3
917                 // Ori-V        D       C       B       A       |       A       B       C       D
918                 //After swapping
919                 //              D       C       B       A       |       D       C       B       A
920                 //Both the little and big-endian use the same sequence to write  data.
921                 //Therefore, we only need swap data when read the data.
922         for(i=0; i<Length; i+=2)
923         {
924                 eFusePhysicalWriteRegisters(pAd, Offset+i, 2, &pValueX[i/2]);
925         }
926 }
927
928
929 /*
930         ========================================================================
931
932         Routine Description:
933
934         Arguments:
935
936         Return Value:
937
938         IRQL =
939
940         Note:
941
942         ========================================================================
943 */
944 NTSTATUS eFuseWrite(
945         IN      PRTMP_ADAPTER   pAd,
946         IN      USHORT                  Offset,
947         IN      PUCHAR                  pData,
948         IN      USHORT                  length)
949 {
950         int i;
951
952         USHORT* pValueX = (PUSHORT) pData;                              //value ...
953                 //The input value=3070 will be stored as following
954                 // Little-endian                S       |       S       Big-endian
955                 // addr                 1       0       |       0       1
956                 // Ori-V                        30      70      |       30      70
957                 //After swapping
958                 //                              30      70      |       70      30
959                 //Casting
960                 //                              3070    |       7030 (x)
961                 //The swapping should be removed for big-endian
962         for(i=0; i<length; i+=2)
963         {
964                 eFuseWriteRegisters(pAd, Offset+i, 2, &pValueX[i/2]);
965         }
966
967         return TRUE;
968 }
969
970 /*
971         ========================================================================
972
973         Routine Description:
974
975         Arguments:
976
977         Return Value:
978
979         IRQL =
980
981         Note:
982
983         ========================================================================
984 */
985 INT set_eFuseGetFreeBlockCount_Proc(
986         IN      PRTMP_ADAPTER   pAd,
987         IN      PUCHAR                  arg)
988 {
989         USHORT i;
990         USHORT  LogicalAddress;
991         USHORT efusefreenum=0;
992         if(!pAd->bUseEfuse)
993                 return FALSE;
994         for (i = EFUSE_USAGE_MAP_START; i <= EFUSE_USAGE_MAP_END; i+=2)
995         {
996                 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
997                 if( (LogicalAddress & 0xff) == 0)
998                 {
999                         efusefreenum= (UCHAR) (EFUSE_USAGE_MAP_END-i+1);
1000                         break;
1001                 }
1002                 else if(( (LogicalAddress >> 8) & 0xff) == 0)
1003                 {
1004                         efusefreenum = (UCHAR) (EFUSE_USAGE_MAP_END-i);
1005                         break;
1006                 }
1007
1008                 if(i == EFUSE_USAGE_MAP_END)
1009                         efusefreenum = 0;
1010         }
1011         printk("efuseFreeNumber is %d\n",efusefreenum);
1012         return TRUE;
1013 }
1014 INT set_eFusedump_Proc(
1015         IN      PRTMP_ADAPTER   pAd,
1016         IN      PUCHAR                  arg)
1017 {
1018 USHORT InBuf[3];
1019         INT i=0;
1020         if(!pAd->bUseEfuse)
1021                 return FALSE;
1022         for(i =0; i<EFUSE_USAGE_MAP_END/2; i++)
1023         {
1024                 InBuf[0] = 2*i;
1025                 InBuf[1] = 2;
1026                 InBuf[2] = 0x0;
1027
1028                 eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
1029                 if(i%4==0)
1030                 printk("\nBlock %x:",i/8);
1031                 printk("%04x ",InBuf[2]);
1032         }
1033         return TRUE;
1034 }
1035 INT     set_eFuseLoadFromBin_Proc(
1036         IN      PRTMP_ADAPTER   pAd,
1037         IN      PUCHAR                  arg)
1038 {
1039         CHAR                                    *src;
1040         struct file                             *srcf;
1041         INT                                     retval, orgfsuid, orgfsgid;
1042         mm_segment_t                    orgfs;
1043         UCHAR                                   *buffer;
1044         UCHAR                                   BinFileSize=0;
1045         INT                                             i = 0,j=0,k=1;
1046         USHORT                                  *PDATA;
1047         USHORT                                  DATA;
1048         BinFileSize=strlen("RT30xxEEPROM.bin");
1049         src = kmalloc(128, MEM_ALLOC_FLAG);
1050         NdisZeroMemory(src, 128);
1051
1052         if(strlen(arg)>0)
1053         {
1054
1055                 NdisMoveMemory(src, arg, strlen(arg));
1056         }
1057
1058         else
1059         {
1060
1061                 NdisMoveMemory(src, "RT30xxEEPROM.bin", BinFileSize);
1062         }
1063
1064         DBGPRINT(RT_DEBUG_TRACE, ("FileName=%s\n",src));
1065         buffer = kmalloc(MAX_EEPROM_BIN_FILE_SIZE, MEM_ALLOC_FLAG);
1066
1067         if(buffer == NULL)
1068         {
1069                 kfree(src);
1070                  return FALSE;
1071 }
1072         PDATA=kmalloc(sizeof(USHORT)*8,MEM_ALLOC_FLAG);
1073
1074         if(PDATA==NULL)
1075         {
1076                 kfree(src);
1077
1078                 kfree(buffer);
1079                 return FALSE;
1080         }
1081         /* Don't change to uid 0, let the file be opened as the "normal" user */
1082 #if 0
1083         orgfsuid = current->fsuid;
1084         orgfsgid = current->fsgid;
1085         current->fsuid=current->fsgid = 0;
1086 #endif
1087         orgfs = get_fs();
1088          set_fs(KERNEL_DS);
1089
1090         if (src && *src)
1091         {
1092                 srcf = filp_open(src, O_RDONLY, 0);
1093                 if (IS_ERR(srcf))
1094                 {
1095                         DBGPRINT(RT_DEBUG_ERROR, ("--> Error %ld opening %s\n", -PTR_ERR(srcf),src));
1096                         return FALSE;
1097                 }
1098                 else
1099                 {
1100                         // The object must have a read method
1101                         if (srcf->f_op && srcf->f_op->read)
1102                         {
1103                                 memset(buffer, 0x00, MAX_EEPROM_BIN_FILE_SIZE);
1104                                 while(srcf->f_op->read(srcf, &buffer[i], 1, &srcf->f_pos)==1)
1105                                 {
1106                                         DBGPRINT(RT_DEBUG_TRACE, ("%02X ",buffer[i]));
1107                                         if((i+1)%8==0)
1108                                                 DBGPRINT(RT_DEBUG_TRACE, ("\n"));
1109                                 i++;
1110                                                 if(i>=MAX_EEPROM_BIN_FILE_SIZE)
1111                                                         {
1112                                                                 DBGPRINT(RT_DEBUG_ERROR, ("--> Error %ld reading %s, The file is too large[1024]\n", -PTR_ERR(srcf),src));
1113                                                                 kfree(PDATA);
1114                                                                 kfree(buffer);
1115                                                                 kfree(src);
1116                                                                 return FALSE;
1117                                                         }
1118                                }
1119                         }
1120                         else
1121                         {
1122                                                 DBGPRINT(RT_DEBUG_ERROR, ("--> Error!! System doest not support read function\n"));
1123                                                 kfree(PDATA);
1124                                                 kfree(buffer);
1125                                                 kfree(src);
1126                                                 return FALSE;
1127                         }
1128                 }
1129
1130
1131         }
1132         else
1133                 {
1134                                         DBGPRINT(RT_DEBUG_ERROR, ("--> Error src  or srcf is null\n"));
1135                                         kfree(PDATA);
1136                                         kfree(buffer);
1137                                         return FALSE;
1138
1139                 }
1140
1141
1142         retval=filp_close(srcf,NULL);
1143
1144         if (retval)
1145         {
1146                 DBGPRINT(RT_DEBUG_TRACE, ("--> Error %d closing %s\n", -retval, src));
1147         }
1148         set_fs(orgfs);
1149 #if 0
1150         current->fsuid = orgfsuid;
1151         current->fsgid = orgfsgid;
1152 #endif
1153         for(j=0;j<i;j++)
1154         {
1155                 DBGPRINT(RT_DEBUG_TRACE, ("%02X ",buffer[j]));
1156                 if((j+1)%2==0)
1157                         PDATA[j/2%8]=((buffer[j]<<8)&0xff00)|(buffer[j-1]&0xff);
1158                 if(j%16==0)
1159                 {
1160                         k=buffer[j];
1161                 }
1162                 else
1163                 {
1164                         k&=buffer[j];
1165                         if((j+1)%16==0)
1166                         {
1167
1168                                 DBGPRINT(RT_DEBUG_TRACE, (" result=%02X,blk=%02x\n",k,j/16));
1169
1170                                 if(k!=0xff)
1171                                         eFuseWriteRegistersFromBin(pAd,(USHORT)j-15, 16, PDATA);
1172                                 else
1173                                         {
1174                                                 if(eFuseReadRegisters(pAd,j, 2,(PUSHORT)&DATA)!=0x3f)
1175                                                         eFuseWriteRegistersFromBin(pAd,(USHORT)j-15, 16, PDATA);
1176                                         }
1177                                 /*
1178                                 for(l=0;l<8;l++)
1179                                         printk("%04x ",PDATA[l]);
1180                                 printk("\n");
1181                                 */
1182                                 NdisZeroMemory(PDATA,16);
1183
1184
1185                         }
1186                 }
1187
1188
1189         }
1190
1191
1192         kfree(PDATA);
1193         kfree(buffer);
1194         kfree(src);
1195         return TRUE;
1196 }
1197 NTSTATUS eFuseWriteRegistersFromBin(
1198         IN      PRTMP_ADAPTER   pAd,
1199         IN      USHORT Offset,
1200         IN      USHORT Length,
1201         IN      USHORT* pData)
1202 {
1203         USHORT  i;
1204         USHORT  eFuseData;
1205         USHORT  LogicalAddress, BlkNum = 0xffff;
1206         UCHAR   EFSROM_AOUT,Loop=0;
1207         EFUSE_CTRL_STRUC                eFuseCtrlStruc;
1208         USHORT  efuseDataOffset;
1209         UINT32  data,tempbuffer;
1210         USHORT addr,tmpaddr, InBuf[3], tmpOffset;
1211         UINT32 buffer[4];
1212         BOOLEAN         bWriteSuccess = TRUE;
1213         BOOLEAN         bNotWrite=TRUE;
1214         BOOLEAN         bAllocateNewBlk=TRUE;
1215
1216         DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin Offset=%x, pData=%04x:%04x:%04x:%04x\n", Offset, *pData,*(pData+1),*(pData+2),*(pData+3)));
1217
1218         do
1219         {
1220         //Step 0. find the entry in the mapping table
1221         //The address of EEPROM is 2-bytes alignment.
1222         //The last bit is used for alignment, so it must be 0.
1223         Loop++;
1224         tmpOffset = Offset & 0xfffe;
1225         EFSROM_AOUT = eFuseReadRegisters(pAd, tmpOffset, 2, &eFuseData);
1226
1227         if( EFSROM_AOUT == 0x3f)
1228         {       //find available logical address pointer
1229                 //the logical address does not exist, find an empty one
1230                 //from the first address of block 45=16*45=0x2d0 to the last address of block 47
1231                 //==>48*16-3(reserved)=2FC
1232                 bAllocateNewBlk=TRUE;
1233                 for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
1234                 {
1235                         //Retrive the logical block nubmer form each logical address pointer
1236                         //It will access two logical address pointer each time.
1237                         eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
1238                         if( (LogicalAddress & 0xff) == 0)
1239                         {//Not used logical address pointer
1240                                 BlkNum = i-EFUSE_USAGE_MAP_START;
1241                                 break;
1242                         }
1243                         else if(( (LogicalAddress >> 8) & 0xff) == 0)
1244                         {//Not used logical address pointer
1245                                 if (i != EFUSE_USAGE_MAP_END)
1246                                 {
1247                                         BlkNum = i-EFUSE_USAGE_MAP_START+1;
1248                                 }
1249                                 break;
1250                         }
1251                 }
1252         }
1253         else
1254         {
1255                 bAllocateNewBlk=FALSE;
1256                 BlkNum = EFSROM_AOUT;
1257         }
1258
1259         DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters BlkNum = %d \n", BlkNum));
1260
1261         if(BlkNum == 0xffff)
1262         {
1263                 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegisters: out of free E-fuse space!!!\n"));
1264                 return FALSE;
1265         }
1266         //Step 1.1.0
1267         //If the block is not existing in mapping table, create one
1268         //and write down the 16-bytes data to the new block
1269         if(bAllocateNewBlk)
1270         {
1271                 DBGPRINT(RT_DEBUG_TRACE, ("Allocate New Blk\n"));
1272                 efuseDataOffset =  EFUSE_DATA3;
1273                 for(i=0; i< 4; i++)
1274                 {
1275                         DBGPRINT(RT_DEBUG_TRACE, ("Allocate New Blk, Data%d=%04x%04x\n",3-i,pData[2*i+1],pData[2*i]));
1276                         tempbuffer=((pData[2*i+1]<<16)&0xffff0000)|pData[2*i];
1277
1278
1279                         RTMP_IO_WRITE32(pAd, efuseDataOffset,tempbuffer);
1280                         efuseDataOffset -= 4;
1281
1282                 }
1283                 /////////////////////////////////////////////////////////////////
1284
1285                 //Step1.1.1. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
1286                 eFuseCtrlStruc.field.EFSROM_AIN = BlkNum* 0x10 ;
1287
1288                 //Step1.1.2. Write EFSROM_MODE (0x580, bit7:bit6) to 3.
1289                 eFuseCtrlStruc.field.EFSROM_MODE = 3;
1290
1291                 //Step1.1.3. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical write procedure.
1292                 eFuseCtrlStruc.field.EFSROM_KICK = 1;
1293
1294                 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
1295
1296                 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
1297
1298                 //Step1.1.4. Polling EFSROM_KICK(0x580, bit30) until it become 0 again. It¡¦s done.
1299                 i = 0;
1300                 while(i < 100)
1301                 {
1302                         RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
1303
1304                         if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
1305                                 break;
1306
1307                         RTMPusecDelay(2);
1308                         i++;
1309                 }
1310
1311         }
1312         else
1313         {       //Step1.2.
1314                 //If the same logical number is existing, check if the writting data and the data
1315                 //saving in this block are the same.
1316                 /////////////////////////////////////////////////////////////////
1317                 //read current values of 16-byte block
1318                 RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
1319
1320                 //Step1.2.0. Write 10-bit of address to EFSROM_AIN (0x580, bit25:bit16). The address must be 16-byte alignment.
1321                 eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
1322
1323                 //Step1.2.1. Write EFSROM_MODE (0x580, bit7:bit6) to 1.
1324                 eFuseCtrlStruc.field.EFSROM_MODE = 0;
1325
1326                 //Step1.2.2. Write EFSROM_KICK (0x580, bit30) to 1 to kick-off physical read procedure.
1327                 eFuseCtrlStruc.field.EFSROM_KICK = 1;
1328
1329                 NdisMoveMemory(&data, &eFuseCtrlStruc, 4);
1330                 RTMP_IO_WRITE32(pAd, EFUSE_CTRL, data);
1331
1332                 //Step1.2.3. Polling EFSROM_KICK(0x580, bit30) until it become 0 again.
1333                 i = 0;
1334                 while(i < 100)
1335                 {
1336                         RTMP_IO_READ32(pAd, EFUSE_CTRL, (PUINT32) &eFuseCtrlStruc);
1337
1338                         if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
1339                                 break;
1340                         RTMPusecDelay(2);
1341                         i++;
1342                 }
1343
1344                 //Step1.2.4. Read 16-byte of data from EFUSE_DATA0-3 (0x59C-0x590)
1345                 efuseDataOffset =  EFUSE_DATA3;
1346                 for(i=0; i< 4; i++)
1347                 {
1348                         RTMP_IO_READ32(pAd, efuseDataOffset, (PUINT32) &buffer[i]);
1349                         efuseDataOffset -=  4;
1350                 }
1351                 //Step1.2.5. Check if the data of efuse and the writing data are the same.
1352                 for(i =0; i<4; i++)
1353                 {
1354                         tempbuffer=((pData[2*i+1]<<16)&0xffff0000)|pData[2*i];
1355                         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));
1356
1357                         if(((buffer[i]&0xffff0000)==(pData[2*i+1]<<16))&&((buffer[i]&0xffff)==pData[2*i]))
1358                                 bNotWrite&=TRUE;
1359                         else
1360                         {
1361                                 bNotWrite&=FALSE;
1362                                 break;
1363                         }
1364                 }
1365                 if(!bNotWrite)
1366                 {
1367                 printk("The data is not the same\n");
1368
1369                         for(i =0; i<8; i++)
1370                         {
1371                                 addr = BlkNum * 0x10 ;
1372
1373                                 InBuf[0] = addr+2*i;
1374                                 InBuf[1] = 2;
1375                                 InBuf[2] = pData[i];
1376
1377                                 eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 2);
1378                         }
1379
1380                 }
1381                 else
1382                         return TRUE;
1383              }
1384
1385
1386
1387                 //Step 2. Write mapping table
1388                 addr = EFUSE_USAGE_MAP_START+BlkNum;
1389
1390                 tmpaddr = addr;
1391
1392                 if(addr % 2 != 0)
1393                         addr = addr -1;
1394                 InBuf[0] = addr;
1395                 InBuf[1] = 2;
1396
1397                 //convert the address from 10 to 8 bit ( bit7, 6 = parity and bit5 ~ 0 = bit9~4), and write to logical map entry
1398                 tmpOffset = Offset;
1399                 tmpOffset >>= 4;
1400                 tmpOffset |= ((~((tmpOffset & 0x01) ^ ( tmpOffset >> 1 & 0x01) ^  (tmpOffset >> 2 & 0x01) ^  (tmpOffset >> 3 & 0x01))) << 6) & 0x40;
1401                 tmpOffset |= ((~( (tmpOffset >> 2 & 0x01) ^ (tmpOffset >> 3 & 0x01) ^ (tmpOffset >> 4 & 0x01) ^ ( tmpOffset >> 5 & 0x01))) << 7) & 0x80;
1402
1403                 // write the logical address
1404                 if(tmpaddr%2 != 0)
1405                         InBuf[2] = tmpOffset<<8;
1406                 else
1407                         InBuf[2] = tmpOffset;
1408
1409                 eFuseWritePhysical(pAd,&InBuf[0], 6, NULL, 0);
1410
1411                 //Step 3. Compare data if not the same, invalidate the mapping entry, then re-write the data until E-fuse is exhausted
1412                 bWriteSuccess = TRUE;
1413                 for(i =0; i<8; i++)
1414                 {
1415                         addr = BlkNum * 0x10 ;
1416
1417                         InBuf[0] = addr+2*i;
1418                         InBuf[1] = 2;
1419                         InBuf[2] = 0x0;
1420
1421                         eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
1422                         DBGPRINT(RT_DEBUG_TRACE, ("addr=%x, buffer[i]=%x,InBuf[2]=%x\n",InBuf[0],pData[i],InBuf[2]));
1423                         if(pData[i] != InBuf[2])
1424                         {
1425                                 bWriteSuccess = FALSE;
1426                                 break;
1427                         }
1428                 }
1429
1430                 //Step 4. invlidate mapping entry and find a free mapping entry if not succeed
1431
1432                 if (!bWriteSuccess&&Loop<2)
1433                 {
1434                         DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin::Not bWriteSuccess BlkNum = %d\n", BlkNum));
1435
1436                         // the offset of current mapping entry
1437                         addr = EFUSE_USAGE_MAP_START+BlkNum;
1438
1439                         //find a new mapping entry
1440                         BlkNum = 0xffff;
1441                         for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
1442                         {
1443                                 eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
1444                                 if( (LogicalAddress & 0xff) == 0)
1445                                 {
1446                                         BlkNum = i-EFUSE_USAGE_MAP_START;
1447                                         break;
1448                                 }
1449                                 else if(( (LogicalAddress >> 8) & 0xff) == 0)
1450                                 {
1451                                         if (i != EFUSE_USAGE_MAP_END)
1452                                         {
1453                                                 BlkNum = i+1-EFUSE_USAGE_MAP_START;
1454                                         }
1455                                         break;
1456                                 }
1457                         }
1458                         DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin::Not bWriteSuccess new BlkNum = %d\n", BlkNum));
1459                         if(BlkNum == 0xffff)
1460                         {
1461                                 DBGPRINT(RT_DEBUG_TRACE, ("eFuseWriteRegistersFromBin: out of free E-fuse space!!!\n"));
1462                                 return FALSE;
1463                         }
1464
1465                         //invalidate the original mapping entry if new entry is not found
1466                         tmpaddr = addr;
1467
1468                         if(addr % 2 != 0)
1469                                 addr = addr -1;
1470                         InBuf[0] = addr;
1471                         InBuf[1] = 2;
1472
1473                         eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
1474
1475                         // write the logical address
1476                         if(tmpaddr%2 != 0)
1477                         {
1478                                 // Invalidate the high byte
1479                                 for (i=8; i<15; i++)
1480                                 {
1481                                         if( ( (InBuf[2] >> i) & 0x01) == 0)
1482                                         {
1483                                                 InBuf[2] |= (0x1 <<i);
1484                                                 break;
1485                                         }
1486                                 }
1487                         }
1488                         else
1489                         {
1490                                 // invalidate the low byte
1491                                 for (i=0; i<8; i++)
1492                                 {
1493                                         if( ( (InBuf[2] >> i) & 0x01) == 0)
1494                                         {
1495                                                 InBuf[2] |= (0x1 <<i);
1496                                                 break;
1497                                         }
1498                                 }
1499                         }
1500                         eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 0);
1501                 }
1502
1503         }
1504         while(!bWriteSuccess&&Loop<2);
1505
1506         return TRUE;
1507 }
1508
1509 #endif // RT30xx //
1510 //2008/09/11:KH add to support efuse-->