8723BU: Update 8723BU wifi driver to version v4.3.16_14189.20150519_BTCOEX2015119...
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rockchip_wlan / rtl8723bu / core / efuse / rtw_efuse.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
4  *                                        
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  *
19  ******************************************************************************/
20 #define _RTW_EFUSE_C_
21
22 #include <drv_types.h>
23 #include <hal_data.h>
24
25 #include "../hal/efuse/efuse_mask.h"
26
27 /*------------------------Define local variable------------------------------*/
28 u8      fakeEfuseBank=0;
29 u32     fakeEfuseUsedBytes=0;
30 u8      fakeEfuseContent[EFUSE_MAX_HW_SIZE]={0};
31 u8      fakeEfuseInitMap[EFUSE_MAX_MAP_LEN]={0};
32 u8      fakeEfuseModifiedMap[EFUSE_MAX_MAP_LEN]={0};
33
34 u32     BTEfuseUsedBytes=0;
35 u8      BTEfuseContent[EFUSE_MAX_BT_BANK][EFUSE_MAX_HW_SIZE];
36 u8      BTEfuseInitMap[EFUSE_BT_MAX_MAP_LEN]={0};
37 u8      BTEfuseModifiedMap[EFUSE_BT_MAX_MAP_LEN]={0};
38
39 u32     fakeBTEfuseUsedBytes=0;
40 u8      fakeBTEfuseContent[EFUSE_MAX_BT_BANK][EFUSE_MAX_HW_SIZE];
41 u8      fakeBTEfuseInitMap[EFUSE_BT_MAX_MAP_LEN]={0};
42 u8      fakeBTEfuseModifiedMap[EFUSE_BT_MAX_MAP_LEN]={0};
43
44 u8      maskfileBuffer[32];
45 /*------------------------Define local variable------------------------------*/
46
47 //------------------------------------------------------------------------------
48 #define REG_EFUSE_CTRL          0x0030
49 #define EFUSE_CTRL                      REG_EFUSE_CTRL          // E-Fuse Control.
50 //------------------------------------------------------------------------------
51
52 BOOLEAN
53 Efuse_Read1ByteFromFakeContent(
54         IN              PADAPTER        pAdapter,
55         IN              u16             Offset,
56         IN OUT  u8              *Value  );
57 BOOLEAN
58 Efuse_Read1ByteFromFakeContent(
59         IN              PADAPTER        pAdapter,
60         IN              u16             Offset,
61         IN OUT  u8              *Value  )
62 {
63         if(Offset >= EFUSE_MAX_HW_SIZE)
64         {
65                 return _FALSE;
66         }
67         //DbgPrint("Read fake content, offset = %d\n", Offset);
68         if(fakeEfuseBank == 0)
69                 *Value = fakeEfuseContent[Offset];
70         else
71                 *Value = fakeBTEfuseContent[fakeEfuseBank-1][Offset];
72         return _TRUE;
73 }
74
75 BOOLEAN
76 Efuse_Write1ByteToFakeContent(
77         IN              PADAPTER        pAdapter,
78         IN              u16             Offset,
79         IN              u8              Value   );
80 BOOLEAN
81 Efuse_Write1ByteToFakeContent(
82         IN              PADAPTER        pAdapter,
83         IN              u16             Offset,
84         IN              u8              Value   )
85 {
86         if(Offset >= EFUSE_MAX_HW_SIZE)
87         {
88                 return _FALSE;
89         }
90         if(fakeEfuseBank == 0)
91                 fakeEfuseContent[Offset] = Value;
92         else
93         {
94                 fakeBTEfuseContent[fakeEfuseBank-1][Offset] = Value;
95         }
96         return _TRUE;
97 }
98
99 /*-----------------------------------------------------------------------------
100  * Function:    Efuse_PowerSwitch
101  *
102  * Overview:    When we want to enable write operation, we should change to 
103  *                              pwr on state. When we stop write, we should switch to 500k mode
104  *                              and disable LDO 2.5V.
105  *
106  * Input:       NONE
107  *
108  * Output:      NONE
109  *
110  * Return:      NONE
111  *
112  * Revised History:
113  * When                 Who             Remark
114  * 11/17/2008   MHC             Create Version 0.
115  *
116  *---------------------------------------------------------------------------*/
117 VOID
118 Efuse_PowerSwitch(
119         IN      PADAPTER        pAdapter,
120         IN      u8              bWrite,
121         IN      u8              PwrState)
122 {
123         pAdapter->HalFunc.EfusePowerSwitch(pAdapter, bWrite, PwrState);
124 }
125
126 VOID
127 BTEfuse_PowerSwitch(
128         IN      PADAPTER        pAdapter,
129         IN      u8              bWrite,
130         IN      u8              PwrState)
131 {
132         if(pAdapter->HalFunc.BTEfusePowerSwitch)
133                 pAdapter->HalFunc.BTEfusePowerSwitch(pAdapter, bWrite, PwrState);
134 }
135
136 /*-----------------------------------------------------------------------------
137  * Function:    efuse_GetCurrentSize
138  *
139  * Overview:    Get current efuse size!!!
140  *
141  * Input:       NONE
142  *
143  * Output:      NONE
144  *
145  * Return:      NONE
146  *
147  * Revised History:
148  * When                 Who             Remark
149  * 11/16/2008   MHC             Create Version 0.
150  *
151  *---------------------------------------------------------------------------*/
152 u16
153 Efuse_GetCurrentSize(
154         IN PADAPTER             pAdapter,
155         IN u8                   efuseType,
156         IN BOOLEAN              bPseudoTest)
157 {
158         u16 ret=0;
159
160         ret = pAdapter->HalFunc.EfuseGetCurrentSize(pAdapter, efuseType, bPseudoTest);
161
162         return ret;
163 }
164
165 /*  11/16/2008 MH Add description. Get current efuse area enabled word!!. */
166 u8
167 Efuse_CalculateWordCnts(IN u8   word_en)
168 {
169         u8 word_cnts = 0;
170         if(!(word_en & BIT(0))) word_cnts++; // 0 : write enable
171         if(!(word_en & BIT(1))) word_cnts++;
172         if(!(word_en & BIT(2))) word_cnts++;
173         if(!(word_en & BIT(3))) word_cnts++;
174         return word_cnts;
175 }
176
177 //
178 //      Description:
179 //              Execute E-Fuse read byte operation.
180 //              Refered from SD1 Richard.
181 //
182 //      Assumption:
183 //              1. Boot from E-Fuse and successfully auto-load.
184 //              2. PASSIVE_LEVEL (USB interface)
185 //
186 //      Created by Roger, 2008.10.21.
187 //
188 VOID
189 ReadEFuseByte(
190                 PADAPTER        Adapter,
191                 u16                     _offset, 
192                 u8                      *pbuf, 
193                 IN BOOLEAN      bPseudoTest) 
194 {
195         u32     value32;
196         u8      readbyte;
197         u16     retry;
198         //u32 start=rtw_get_current_time();
199
200         if(bPseudoTest)
201         {
202                 Efuse_Read1ByteFromFakeContent(Adapter, _offset, pbuf);
203                 return;
204         }
205         if (IS_HARDWARE_TYPE_8723B(Adapter))
206         {
207                 // <20130121, Kordan> For SMIC S55 EFUSE specificatoin.
208                 //0x34[11]: SW force PGMEN input of efuse to high. (for the bank selected by 0x34[9:8])
209                 PHY_SetMacReg(Adapter, EFUSE_TEST, BIT11, 0);
210         }
211         //Write Address
212         rtw_write8(Adapter, EFUSE_CTRL+1, (_offset & 0xff));            
213         readbyte = rtw_read8(Adapter, EFUSE_CTRL+2);
214         rtw_write8(Adapter, EFUSE_CTRL+2, ((_offset >> 8) & 0x03) | (readbyte & 0xfc));                 
215
216         //Write bit 32 0
217         readbyte = rtw_read8(Adapter, EFUSE_CTRL+3);            
218         rtw_write8(Adapter, EFUSE_CTRL+3, (readbyte & 0x7f));   
219         
220         //Check bit 32 read-ready
221         retry = 0;
222         value32 = rtw_read32(Adapter, EFUSE_CTRL);
223         //while(!(((value32 >> 24) & 0xff) & 0x80)  && (retry<10))
224         while(!(((value32 >> 24) & 0xff) & 0x80)  && (retry<10000))
225         {
226                 value32 = rtw_read32(Adapter, EFUSE_CTRL);
227                 retry++;
228         }
229
230         // 20100205 Joseph: Add delay suggested by SD1 Victor.
231         // This fix the problem that Efuse read error in high temperature condition.
232         // Designer says that there shall be some delay after ready bit is set, or the
233         // result will always stay on last data we read.
234         rtw_udelay_os(50);
235         value32 = rtw_read32(Adapter, EFUSE_CTRL);
236         
237         *pbuf = (u8)(value32 & 0xff);
238         //DBG_871X("ReadEFuseByte _offset:%08u, in %d ms\n",_offset ,rtw_get_passing_time_ms(start));
239         
240 }
241
242 //
243 //      Description:
244 //              1. Execute E-Fuse read byte operation according as map offset and 
245 //                  save to E-Fuse table.
246 //              2. Refered from SD1 Richard.
247 //
248 //      Assumption:
249 //              1. Boot from E-Fuse and successfully auto-load.
250 //              2. PASSIVE_LEVEL (USB interface)
251 //
252 //      Created by Roger, 2008.10.21.
253 //
254 //      2008/12/12 MH   1. Reorganize code flow and reserve bytes. and add description.
255 //                                      2. Add efuse utilization collect.
256 //      2008/12/22 MH   Read Efuse must check if we write section 1 data again!!! Sec1
257 //                                      write addr must be after sec5.
258 //
259
260 VOID
261 efuse_ReadEFuse(
262         PADAPTER        Adapter,
263         u8              efuseType,
264         u16             _offset,
265         u16             _size_byte,
266         u8              *pbuf,
267         IN      BOOLEAN bPseudoTest
268         );
269 VOID
270 efuse_ReadEFuse(
271         PADAPTER        Adapter,
272         u8              efuseType,
273         u16             _offset,
274         u16             _size_byte,
275         u8              *pbuf,
276         IN      BOOLEAN bPseudoTest
277         )
278 {
279         Adapter->HalFunc.ReadEFuse(Adapter, efuseType, _offset, _size_byte, pbuf, bPseudoTest);
280 }
281
282 VOID
283 EFUSE_GetEfuseDefinition(
284         IN              PADAPTER        pAdapter,
285         IN              u8              efuseType,
286         IN              u8              type,
287         OUT             void            *pOut,
288         IN              BOOLEAN         bPseudoTest
289         )
290 {
291         pAdapter->HalFunc.EFUSEGetEfuseDefinition(pAdapter, efuseType, type, pOut, bPseudoTest);
292 }
293
294 /*-----------------------------------------------------------------------------
295  * Function:    EFUSE_Read1Byte
296  *
297  * Overview:    Copy from WMAC fot EFUSE read 1 byte.
298  *
299  * Input:       NONE
300  *
301  * Output:      NONE
302  *
303  * Return:      NONE
304  *
305  * Revised History:
306  * When                 Who             Remark
307  * 09/23/2008   MHC             Copy from WMAC.
308  *
309  *---------------------------------------------------------------------------*/
310 u8
311 EFUSE_Read1Byte(        
312         IN      PADAPTER        Adapter, 
313         IN      u16             Address)
314 {
315         u8      data;
316         u8      Bytetemp = {0x00};
317         u8      temp = {0x00};
318         u32     k=0;
319         u16     contentLen=0;
320
321         EFUSE_GetEfuseDefinition(Adapter, EFUSE_WIFI , TYPE_EFUSE_REAL_CONTENT_LEN, (PVOID)&contentLen, _FALSE);
322
323         if (Address < contentLen)       //E-fuse 512Byte
324         {
325                 //Write E-fuse Register address bit0~7
326                 temp = Address & 0xFF;  
327                 rtw_write8(Adapter, EFUSE_CTRL+1, temp);        
328                 Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+2);    
329                 //Write E-fuse Register address bit8~9
330                 temp = ((Address >> 8) & 0x03) | (Bytetemp & 0xFC);     
331                 rtw_write8(Adapter, EFUSE_CTRL+2, temp);        
332
333                 //Write 0x30[31]=0
334                 Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+3);
335                 temp = Bytetemp & 0x7F;
336                 rtw_write8(Adapter, EFUSE_CTRL+3, temp);
337
338                 //Wait Write-ready (0x30[31]=1)
339                 Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+3);
340                 while(!(Bytetemp & 0x80))
341                 {                               
342                         Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+3);
343                         k++;
344                         if(k==1000)
345                         {
346                                 k=0;
347                                 break;
348                         }
349                 }
350                 data=rtw_read8(Adapter, EFUSE_CTRL);
351                 return data;
352         }
353         else
354                 return 0xFF;
355         
356 }/* EFUSE_Read1Byte */
357
358 /*-----------------------------------------------------------------------------
359  * Function:    EFUSE_Write1Byte
360  *
361  * Overview:    Copy from WMAC fot EFUSE write 1 byte.
362  *
363  * Input:       NONE
364  *
365  * Output:      NONE
366  *
367  * Return:      NONE
368  *
369  * Revised History:
370  * When                 Who             Remark
371  * 09/23/2008   MHC             Copy from WMAC.
372  *
373  *---------------------------------------------------------------------------*/
374
375 void    
376 EFUSE_Write1Byte(       
377         IN      PADAPTER        Adapter, 
378         IN      u16             Address,
379         IN      u8              Value);
380 void    
381 EFUSE_Write1Byte(       
382         IN      PADAPTER        Adapter, 
383         IN      u16             Address,
384         IN      u8              Value)
385 {
386         u8      Bytetemp = {0x00};
387         u8      temp = {0x00};
388         u32     k=0;
389         u16     contentLen=0;
390
391         //RT_TRACE(COMP_EFUSE, DBG_LOUD, ("Addr=%x Data =%x\n", Address, Value));
392         EFUSE_GetEfuseDefinition(Adapter, EFUSE_WIFI , TYPE_EFUSE_REAL_CONTENT_LEN, (PVOID)&contentLen, _FALSE);
393
394         if( Address < contentLen)       //E-fuse 512Byte
395         {
396                 rtw_write8(Adapter, EFUSE_CTRL, Value);
397
398                 //Write E-fuse Register address bit0~7
399                 temp = Address & 0xFF;  
400                 rtw_write8(Adapter, EFUSE_CTRL+1, temp);        
401                 Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+2);    
402                 
403                 //Write E-fuse Register address bit8~9
404                 temp = ((Address >> 8) & 0x03) | (Bytetemp & 0xFC);     
405                 rtw_write8(Adapter, EFUSE_CTRL+2, temp);        
406
407                 //Write 0x30[31]=1
408                 Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+3);
409                 temp = Bytetemp | 0x80;
410                 rtw_write8(Adapter, EFUSE_CTRL+3, temp);
411
412                 //Wait Write-ready (0x30[31]=0)
413                 Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+3);
414                 while(Bytetemp & 0x80)
415                 {
416                         Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+3);                    
417                         k++;
418                         if(k==100)
419                         {
420                                 k=0;
421                                 break;
422                         }
423                 }
424         }
425 }/* EFUSE_Write1Byte */
426
427
428 /*  11/16/2008 MH Read one byte from real Efuse. */
429 u8
430 efuse_OneByteRead(
431         IN      PADAPTER        pAdapter, 
432         IN      u16                     addr,
433         IN      u8                      *data,
434         IN      BOOLEAN         bPseudoTest)
435 {
436         u32     tmpidx = 0;
437         u8      bResult;
438         u8      readbyte;
439         HAL_DATA_TYPE   *pHalData = GET_HAL_DATA(pAdapter);
440         
441         //DBG_871X("===> EFUSE_OneByteRead(), addr = %x\n", addr);
442         //DBG_871X("===> EFUSE_OneByteRead() start, 0x34 = 0x%X\n", rtw_read32(pAdapter, EFUSE_TEST));
443
444         if(bPseudoTest)
445         {
446                 bResult = Efuse_Read1ByteFromFakeContent(pAdapter, addr, data);
447                 return bResult;
448         }
449         
450         if(     IS_HARDWARE_TYPE_8723B(pAdapter) ||
451                 (IS_HARDWARE_TYPE_8192E(pAdapter) && (!IS_A_CUT(pHalData->VersionID))) ||
452                 //(IS_HARDWARE_TYPE_8188E(pAdapter) && ((IS_I_CUT(pHalData->VersionID)) || (IS_J_CUT(pHalData->VersionID))))
453                 (IS_VENDOR_8188E_I_CUT_SERIES(pAdapter))
454           )
455         {
456                 // <20130121, Kordan> For SMIC EFUSE specificatoin.
457                 //0x34[11]: SW force PGMEN input of efuse to high. (for the bank selected by 0x34[9:8]) 
458                 //PHY_SetMacReg(pAdapter, 0x34, BIT11, 0);
459                 rtw_write16(pAdapter, 0x34, rtw_read16(pAdapter,0x34)& (~BIT11) ); 
460         }
461
462         // -----------------e-fuse reg ctrl ---------------------------------
463         //address                       
464         rtw_write8(pAdapter, EFUSE_CTRL+1, (u8)(addr&0xff));            
465         rtw_write8(pAdapter, EFUSE_CTRL+2, ((u8)((addr>>8) &0x03) ) |
466         (rtw_read8(pAdapter, EFUSE_CTRL+2)&0xFC ));     
467
468         //rtw_write8(pAdapter, EFUSE_CTRL+3,  0x72);//read cmd  
469         //Write bit 32 0
470         readbyte = rtw_read8(pAdapter, EFUSE_CTRL+3);           
471         rtw_write8(pAdapter, EFUSE_CTRL+3, (readbyte & 0x7f));
472
473         while(!(0x80 &rtw_read8(pAdapter, EFUSE_CTRL+3))&&(tmpidx<1000))
474         {
475                 rtw_mdelay_os(1);
476                 tmpidx++;
477         }
478         if(tmpidx<100)
479         {                       
480                 *data=rtw_read8(pAdapter, EFUSE_CTRL);          
481                 bResult = _TRUE;
482         }
483         else
484         {
485                 *data = 0xff;   
486                 bResult = _FALSE;
487                 DBG_871X("%s: [ERROR] addr=0x%x bResult=%d time out 1s !!!\n", __FUNCTION__, addr, bResult);
488                 DBG_871X("%s: [ERROR] EFUSE_CTRL =0x%08x !!!\n", __FUNCTION__, rtw_read32(pAdapter, EFUSE_CTRL));
489         }
490
491         return bResult;
492 }
493                 
494 /*  11/16/2008 MH Write one byte to reald Efuse. */
495 u8
496 efuse_OneByteWrite(
497         IN      PADAPTER        pAdapter,  
498         IN      u16                     addr, 
499         IN      u8                      data,
500         IN      BOOLEAN         bPseudoTest)
501 {
502         u8      tmpidx = 0;
503         u8      bResult=_FALSE;
504         u32 efuseValue = 0;
505         HAL_DATA_TYPE   *pHalData = GET_HAL_DATA(pAdapter);
506         
507         //DBG_871X("===> EFUSE_OneByteWrite(), addr = %x data=%x\n", addr, data);
508         //DBG_871X("===> EFUSE_OneByteWrite() start, 0x34 = 0x%X\n", rtw_read32(pAdapter, EFUSE_TEST));
509
510         if(bPseudoTest)
511         {
512                 bResult = Efuse_Write1ByteToFakeContent(pAdapter, addr, data);
513                 return bResult;
514         }
515
516
517         // -----------------e-fuse reg ctrl ---------------------------------   
518         //address                       
519
520         
521         efuseValue = rtw_read32(pAdapter, EFUSE_CTRL);
522         efuseValue |= (BIT21|BIT31);
523         efuseValue &= ~(0x3FFFF);
524         efuseValue |= ((addr<<8 | data) & 0x3FFFF);
525
526
527         // <20130227, Kordan> 8192E MP chip A-cut had better not set 0x34[11] until B-Cut.
528         if (    IS_HARDWARE_TYPE_8723B(pAdapter) ||
529                 (IS_HARDWARE_TYPE_8192E(pAdapter) && (!IS_A_CUT(pHalData->VersionID))) ||
530                 //(IS_HARDWARE_TYPE_8188E(pAdapter) && ((IS_I_CUT(pHalData->VersionID)) || (IS_J_CUT(pHalData->VersionID))))
531                 (IS_VENDOR_8188E_I_CUT_SERIES(pAdapter))
532           )
533         {
534                 // <20130121, Kordan> For SMIC EFUSE specificatoin.
535                 //0x34[11]: SW force PGMEN input of efuse to high. (for the bank selected by 0x34[9:8])
536                 //PHY_SetMacReg(pAdapter, 0x34, BIT11, 1);
537                 rtw_write16(pAdapter, 0x34, rtw_read16(pAdapter,0x34)| (BIT11) );
538                 rtw_write32(pAdapter, EFUSE_CTRL, 0x90600000|((addr<<8 | data)) );
539         }
540         else
541         {
542                 rtw_write32(pAdapter, EFUSE_CTRL, efuseValue);
543         }
544
545         while((0x80 &  rtw_read8(pAdapter, EFUSE_CTRL+3)) && (tmpidx<100) ){
546                 rtw_mdelay_os(1);
547                 tmpidx++;
548         }
549
550         if(tmpidx<100)
551         {
552                 bResult = _TRUE;
553         }
554         else
555         {
556                 bResult = _FALSE;
557                 DBG_871X("%s: [ERROR] addr=0x%x ,efuseValue=0x%x ,bResult=%d time out 1s !!! \n",
558                                         __FUNCTION__, addr, efuseValue, bResult);
559                 DBG_871X("%s: [ERROR] EFUSE_CTRL =0x%08x !!!\n", __FUNCTION__, rtw_read32(pAdapter, EFUSE_CTRL));
560         }
561
562         // disable Efuse program enable
563         if (    IS_HARDWARE_TYPE_8723B(pAdapter) ||
564                 (IS_HARDWARE_TYPE_8192E(pAdapter) && (!IS_A_CUT(pHalData->VersionID))) ||
565                 //(IS_HARDWARE_TYPE_8188E(pAdapter) && ((IS_I_CUT(pHalData->VersionID)) || (IS_J_CUT(pHalData->VersionID))))
566                 (IS_VENDOR_8188E_I_CUT_SERIES(pAdapter))
567           )
568         {
569                 PHY_SetMacReg(pAdapter, EFUSE_TEST, BIT(11), 0);
570         }
571
572         return bResult;
573 }
574
575 int
576 Efuse_PgPacketRead(     IN      PADAPTER        pAdapter,
577                                         IN      u8                      offset,
578                                         IN      u8                      *data,
579                                         IN      BOOLEAN         bPseudoTest)
580 {
581         int     ret=0;
582
583         ret =  pAdapter->HalFunc.Efuse_PgPacketRead(pAdapter, offset, data, bPseudoTest);
584
585         return ret;
586 }
587
588 int 
589 Efuse_PgPacketWrite(IN  PADAPTER        pAdapter, 
590                                         IN      u8                      offset,
591                                         IN      u8                      word_en,
592                                         IN      u8                      *data,
593                                         IN      BOOLEAN         bPseudoTest)
594 {
595         int ret;
596
597         ret =  pAdapter->HalFunc.Efuse_PgPacketWrite(pAdapter, offset, word_en, data, bPseudoTest);
598
599         return ret;
600 }
601
602
603 int 
604 Efuse_PgPacketWrite_BT(IN       PADAPTER        pAdapter, 
605                                         IN      u8                      offset,
606                                         IN      u8                      word_en,
607                                         IN      u8                      *data,
608                                         IN      BOOLEAN         bPseudoTest)
609 {
610         int ret;
611
612         ret =  pAdapter->HalFunc.Efuse_PgPacketWrite_BT(pAdapter, offset, word_en, data, bPseudoTest);
613
614         return ret;
615 }
616
617 /*-----------------------------------------------------------------------------
618  * Function:    efuse_WordEnableDataRead
619  *
620  * Overview:    Read allowed word in current efuse section data.
621  *
622  * Input:       NONE
623  *
624  * Output:      NONE
625  *
626  * Return:      NONE
627  *
628  * Revised History:
629  * When                 Who             Remark
630  * 11/16/2008   MHC             Create Version 0.
631  * 11/21/2008   MHC             Fix Write bug when we only enable late word.
632  *
633  *---------------------------------------------------------------------------*/
634 void
635 efuse_WordEnableDataRead(IN     u8      word_en,
636                                                         IN      u8      *sourdata,
637                                                         IN      u8      *targetdata)
638 {       
639         if (!(word_en&BIT(0)))
640         {
641                 targetdata[0] = sourdata[0];
642                 targetdata[1] = sourdata[1];
643         }
644         if (!(word_en&BIT(1)))
645         {
646                 targetdata[2] = sourdata[2];
647                 targetdata[3] = sourdata[3];
648         }
649         if (!(word_en&BIT(2)))
650         {
651                 targetdata[4] = sourdata[4];
652                 targetdata[5] = sourdata[5];
653         }
654         if (!(word_en&BIT(3)))
655         {
656                 targetdata[6] = sourdata[6];
657                 targetdata[7] = sourdata[7];
658         }
659 }
660
661
662 u8
663 Efuse_WordEnableDataWrite(      IN      PADAPTER        pAdapter,
664                                                         IN      u16             efuse_addr,
665                                                         IN      u8              word_en, 
666                                                         IN      u8              *data,
667                                                         IN      BOOLEAN         bPseudoTest)
668 {
669         u8      ret=0;
670
671         ret =  pAdapter->HalFunc.Efuse_WordEnableDataWrite(pAdapter, efuse_addr, word_en, data, bPseudoTest);
672         
673         return ret;
674 }
675
676 static u8 efuse_read8(PADAPTER padapter, u16 address, u8 *value)
677 {
678         return efuse_OneByteRead(padapter,address, value, _FALSE);
679 }
680
681 static u8 efuse_write8(PADAPTER padapter, u16 address, u8 *value)
682 {
683         return efuse_OneByteWrite(padapter,address, *value, _FALSE);
684 }
685
686 /*
687  * read/wirte raw efuse data
688  */
689 u8 rtw_efuse_access(PADAPTER padapter, u8 bWrite, u16 start_addr, u16 cnts, u8 *data)
690 {
691         int i = 0;
692         u16     real_content_len = 0, max_available_size = 0;
693         u8 res = _FAIL ;
694         u8 (*rw8)(PADAPTER, u16, u8*);
695
696         EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_REAL_CONTENT_LEN, (PVOID)&real_content_len, _FALSE);
697         EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_AVAILABLE_EFUSE_BYTES_TOTAL, (PVOID)&max_available_size, _FALSE);
698
699         if (start_addr > real_content_len)
700                 return _FAIL;
701
702         if (_TRUE == bWrite) {
703                 if ((start_addr + cnts) > max_available_size)
704                         return _FAIL;
705                 rw8 = &efuse_write8;
706         } else
707                 rw8 = &efuse_read8;
708
709         Efuse_PowerSwitch(padapter, bWrite, _TRUE);
710
711         // e-fuse one byte read / write
712         for (i = 0; i < cnts; i++) {
713                 if (start_addr >= real_content_len) {
714                         res = _FAIL;
715                         break;
716                 }
717
718                 res = rw8(padapter, start_addr++, data++);
719                 if (_FAIL == res) break;
720         }
721
722         Efuse_PowerSwitch(padapter, bWrite, _FALSE);
723
724         return res;
725 }
726 //------------------------------------------------------------------------------
727 u16 efuse_GetMaxSize(PADAPTER padapter)
728 {
729         u16     max_size;
730
731         max_size = 0;
732         EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI , TYPE_AVAILABLE_EFUSE_BYTES_TOTAL, (PVOID)&max_size, _FALSE);
733         return max_size;
734 }
735 //------------------------------------------------------------------------------
736 u8 efuse_GetCurrentSize(PADAPTER padapter, u16 *size)
737 {
738         Efuse_PowerSwitch(padapter, _FALSE, _TRUE);
739         *size = Efuse_GetCurrentSize(padapter, EFUSE_WIFI, _FALSE);
740         Efuse_PowerSwitch(padapter, _FALSE, _FALSE);
741
742         return _SUCCESS;
743 }
744 //------------------------------------------------------------------------------
745 u8 rtw_efuse_map_read(PADAPTER padapter, u16 addr, u16 cnts, u8 *data)
746 {
747         u16     mapLen=0;
748
749         EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN, (PVOID)&mapLen, _FALSE);
750
751         if ((addr + cnts) > mapLen)
752                 return _FAIL;
753
754         Efuse_PowerSwitch(padapter, _FALSE, _TRUE);
755
756         efuse_ReadEFuse(padapter, EFUSE_WIFI, addr, cnts, data, _FALSE);
757
758         Efuse_PowerSwitch(padapter, _FALSE, _FALSE);
759
760         return _SUCCESS;
761 }
762
763 u8 rtw_BT_efuse_map_read(PADAPTER padapter, u16 addr, u16 cnts, u8 *data)
764 {
765         u16     mapLen=0;
766
767         EFUSE_GetEfuseDefinition(padapter, EFUSE_BT, TYPE_EFUSE_MAP_LEN, (PVOID)&mapLen, _FALSE);
768
769         if ((addr + cnts) > mapLen)
770                 return _FAIL;
771
772         Efuse_PowerSwitch(padapter, _FALSE, _TRUE);
773
774         efuse_ReadEFuse(padapter, EFUSE_BT, addr, cnts, data, _FALSE);
775
776         Efuse_PowerSwitch(padapter, _FALSE, _FALSE);
777
778         return _SUCCESS;
779 }
780
781 BOOLEAN rtw_file_efuse_IsMasked(
782         PADAPTER        pAdapter,
783         u16             Offset
784         )
785 {
786         int r = Offset/16;
787         int c = (Offset%16) / 2;
788         int result = 0;
789         
790         if(pAdapter->registrypriv.boffefusemask)
791                 return FALSE;
792
793         //DBG_871X(" %s ,Offset=%x r= %d , c=%d , maskfileBuffer[r]= %x \n",__func__,Offset,r,c,maskfileBuffer[r]);
794         if (c < 4) // Upper double word
795             result = (maskfileBuffer[r] & (0x10 << c));
796         else
797             result = (maskfileBuffer[r] & (0x01 << (c-4)));
798         
799         return (result > 0) ? 0 : 1;
800
801 }
802
803
804 u8 rtw_efuse_file_read(PADAPTER padapter,u8 *filepatch,u8 *buf,u32 len)
805 {
806         char *ptmp;
807         char *ptmpbuf=NULL;
808         u32 rtStatus;
809         HAL_DATA_TYPE   *pHalData = GET_HAL_DATA(padapter);
810
811         ptmpbuf = rtw_zmalloc(2048);
812
813         if (ptmpbuf == NULL)
814                 return _FALSE;
815
816         _rtw_memset(ptmpbuf,'\0',2048);
817         
818         rtStatus = rtw_retrive_from_file(filepatch, ptmpbuf, 2048);
819
820         if( rtStatus > 100 )
821         {
822                 u32 i,j;
823                 for(i=0,j=0;j<len;i+=2,j++)
824                 {
825                         if (( ptmpbuf[i] == ' ' ) && (ptmpbuf[i+1] != '\n' && ptmpbuf[i+1] != '\0')) {
826                                 i++;
827                         }
828                         if( (ptmpbuf[i+1] != '\n' && ptmpbuf[i+1] != '\0'))
829                         {
830                                         buf[j] = simple_strtoul(&ptmpbuf[i],&ptmp, 16);
831                                         DBG_871X(" i=%d,j=%d, %x \n",i,j,buf[j]);
832
833                         } else {
834                                 j--;
835                         }
836                         
837                 }
838
839         } else {
840                 DBG_871X(" %s ,filepatch %s , FAIL %d\n", __func__, filepatch, rtStatus);
841                 return _FALSE;
842         }
843         rtw_mfree(ptmpbuf, 2048);
844         DBG_871X(" %s ,filepatch %s , done %d\n", __func__, filepatch, rtStatus);
845         return _TRUE;
846 }
847
848
849 BOOLEAN 
850 efuse_IsMasked(
851         PADAPTER        pAdapter,
852         u16             Offset
853         )
854 {
855         HAL_DATA_TYPE   *pHalData = GET_HAL_DATA(pAdapter);
856         
857
858         //if (bEfuseMaskOFF(pAdapter))
859         if(pAdapter->registrypriv.boffefusemask)
860                 return FALSE;
861                 
862 #if DEV_BUS_TYPE == RT_USB_INTERFACE
863 #if defined(CONFIG_RTL8188E)
864         if (IS_HARDWARE_TYPE_8188E(pAdapter))  
865                 return (IS_MASKED(8188E,_MUSB,Offset)) ? TRUE : FALSE;
866 #endif
867 #if defined(CONFIG_RTL8812A)
868         if (IS_HARDWARE_TYPE_8812(pAdapter))  
869                 return (IS_MASKED(8812A,_MUSB,Offset)) ? TRUE : FALSE;
870 #endif
871 #if defined(CONFIG_RTL8821A)
872         //if (IS_HARDWARE_TYPE_8811AU(pAdapter))  
873         //      return (IS_MASKED(8811A,_MUSB,Offset)) ? TRUE : FALSE;
874         if (IS_HARDWARE_TYPE_8821(pAdapter))  
875                 return (IS_MASKED(8821A,_MUSB,Offset)) ? TRUE : FALSE;          
876 #endif          
877 #if defined(CONFIG_RTL8192E)
878         if (IS_HARDWARE_TYPE_8192E(pAdapter))  
879                 return (IS_MASKED(8192E,_MUSB,Offset)) ? TRUE : FALSE;
880 #endif
881 #if defined(CONFIG_RTL8723B)
882         if (IS_HARDWARE_TYPE_8723B(pAdapter))  
883                 return (IS_MASKED(8723B,_MUSB,Offset)) ? TRUE : FALSE;
884 #endif
885 #if defined(CONFIG_RTL8814A)
886         if (IS_HARDWARE_TYPE_8814A(pAdapter))
887                 return (IS_MASKED(8814A, _MUSB, Offset)) ? TRUE : FALSE;
888 #endif  
889 #elif DEV_BUS_TYPE == RT_PCI_INTERFACE
890 #if defined(CONFIG_RTL8188E)
891         if (IS_HARDWARE_TYPE_8188E(pAdapter))  
892                 return (IS_MASKED(8188E,_MPCIE,Offset)) ? TRUE : FALSE;
893 #endif
894 #if defined(CONFIG_RTL8192E)
895         if (IS_HARDWARE_TYPE_8192E(pAdapter))   
896                 return (IS_MASKED(8192E,_MPCIE,Offset)) ? TRUE : FALSE;
897 #endif  
898 #if defined(CONFIG_RTL8812A)
899         if (IS_HARDWARE_TYPE_8812(pAdapter))  
900                 return (IS_MASKED(8812A,_MPCIE,Offset)) ? TRUE : FALSE;
901 #endif  
902 #if defined(CONFIG_RTL8821A)
903         if (IS_HARDWARE_TYPE_8821(pAdapter))  
904                 return (IS_MASKED(8821A,_MPCIE,Offset)) ? TRUE : FALSE;
905 #endif
906 #if defined(CONFIG_RTL8723B)
907         if (IS_HARDWARE_TYPE_8723B(pAdapter))  
908                 return (IS_MASKED(8723B,_MPCIE,Offset)) ? TRUE : FALSE; 
909 #endif
910 #if defined(CONFIG_RTL8814A)
911         if (IS_HARDWARE_TYPE_8814A(pAdapter))
912                 return (IS_MASKED(8814A, _MPCIE, Offset)) ? TRUE : FALSE;
913 #endif
914         //else if (IS_HARDWARE_TYPE_8821B(pAdapter))  
915         //      return (IS_MASKED(8821B,_MPCIE,Offset)) ? TRUE : FALSE; 
916
917 #elif DEV_BUS_TYPE == RT_SDIO_INTERFACE
918 #ifdef CONFIG_RTL8188E_SDIO
919         if (IS_HARDWARE_TYPE_8188E(pAdapter))  
920                 return (IS_MASKED(8188E,_MSDIO,Offset)) ? TRUE : FALSE;
921 #endif          
922 #endif
923
924         return FALSE;   
925 }
926
927 //------------------------------------------------------------------------------
928 u8 rtw_efuse_map_write(PADAPTER padapter, u16 addr, u16 cnts, u8 *data)
929 {
930 #define RT_ASSERT_RET(expr)                                                                                             \
931         if(!(expr)) {                                                                                                                   \
932                 printk( "Assertion failed! %s at ......\n", #expr);                                                     \
933                 printk( "      ......%s,%s,line=%d\n",__FILE__,__FUNCTION__,__LINE__);  \
934                 return _FAIL;   \
935         }
936
937         u8      offset, word_en;
938         u8      *map;
939         u8      newdata[PGPKT_DATA_SIZE];
940         s32     i, j, idx;
941         u8      ret = _SUCCESS;
942         u16     mapLen=0;
943         HAL_DATA_TYPE   *pHalData = GET_HAL_DATA(padapter);
944
945         EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN, (PVOID)&mapLen, _FALSE);
946
947         if ((addr + cnts) > mapLen)
948                 return _FAIL;
949
950         RT_ASSERT_RET(PGPKT_DATA_SIZE == 8); // have to be 8 byte alignment
951         RT_ASSERT_RET((mapLen & 0x7) == 0); // have to be PGPKT_DATA_SIZE alignment for memcpy
952
953         map = rtw_zmalloc(mapLen);
954         if(map == NULL){
955                 return _FAIL;
956         }
957         
958         _rtw_memset(map, 0xFF, mapLen);
959         
960         ret = rtw_efuse_map_read(padapter, 0, mapLen, map);
961         if (ret == _FAIL) goto exit;
962
963         if(padapter->registrypriv.boffefusemask==0)
964         {
965                 for (i =0; i < cnts; i++)
966                 { 
967                         if(padapter->registrypriv.bFileMaskEfuse==_TRUE)
968                         {
969                                 if (rtw_file_efuse_IsMasked(padapter, addr+i))  /*use file efuse mask. */
970                                                 data[i] = map[addr+i];
971                         }
972                         else
973                         {
974                                 if ( efuse_IsMasked(padapter, addr+i ))
975                                                 data[i] = map[addr+i];
976                         }
977                         DBG_8192C("%s , data[%d] = %x, map[addr+i]= %x\n", __func__, i, data[i], map[addr+i]);
978                 }
979         }
980         Efuse_PowerSwitch(padapter, _TRUE, _TRUE);
981
982         idx = 0;
983         offset = (addr >> 3);
984         while (idx < cnts)
985         {
986                 word_en = 0xF;
987                 j = (addr + idx) & 0x7;
988                 _rtw_memcpy(newdata, &map[offset << 3], PGPKT_DATA_SIZE);
989                 for (i = j; i<PGPKT_DATA_SIZE && idx < cnts; i++, idx++)
990                 {
991                         if (data[idx] != map[addr + idx])
992                         {
993                                 word_en &= ~BIT(i >> 1);
994                                 newdata[i] = data[idx];
995 #ifdef CONFIG_RTL8723B                                  
996                                  if( addr + idx == 0x8)
997                                  {      
998                                         if (IS_C_CUT(pHalData->VersionID) || IS_B_CUT(pHalData->VersionID))
999                                         {
1000                                                 if(pHalData->adjuseVoltageVal == 6)
1001                                                 {
1002                                                                 newdata[i] = map[addr + idx];
1003                                                                 DBG_8192C(" %s ,\n adjuseVoltageVal = %d ,newdata[%d] = %x \n",__func__,pHalData->adjuseVoltageVal,i,newdata[i]);        
1004                                                 }
1005                                         }
1006                                   }
1007 #endif
1008                         }
1009                 }
1010
1011                 if (word_en != 0xF) {
1012                         ret = Efuse_PgPacketWrite(padapter, offset, word_en, newdata, _FALSE);
1013                         DBG_871X("offset=%x \n",offset);
1014                         DBG_871X("word_en=%x \n",word_en);
1015
1016                         for(i=0;i<PGPKT_DATA_SIZE;i++)
1017                         {
1018                                 DBG_871X("data=%x \t",newdata[i]);
1019                         }
1020                         if (ret == _FAIL) break;
1021                 }
1022
1023                 offset++;
1024         }
1025
1026         Efuse_PowerSwitch(padapter, _TRUE, _FALSE);
1027
1028 exit:
1029
1030         rtw_mfree(map, mapLen);
1031
1032         return ret;
1033 }
1034
1035 u8 rtw_efuse_mask_map_read(PADAPTER padapter, u16 addr, u16 cnts, u8 *data)
1036 {
1037         u8      ret = _SUCCESS;
1038         u16     mapLen = 0, i = 0;
1039         
1040         EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN, (PVOID)&mapLen, _FALSE);
1041         
1042         ret = rtw_efuse_map_read(padapter, 0, mapLen, data);
1043
1044         if (padapter->registrypriv.boffefusemask == 0) {
1045
1046                         for (i = 0; i < cnts; i++) { 
1047                                 if (padapter->registrypriv.bFileMaskEfuse == _TRUE) {
1048                                         if (rtw_file_efuse_IsMasked(padapter, addr+i)) /*use file efuse mask.*/ 
1049                                                         data[i] = 0xff;
1050                                 } else {
1051                                         /*DBG_8192C(" %s , data[%d] = %x\n", __func__, i, data[i]);*/
1052                                         if (efuse_IsMasked(padapter, addr+i)) {
1053                                                 data[i] = 0xff;
1054                                                 /*DBG_8192C(" %s ,mask data[%d] = %x\n", __func__, i, data[i]);*/
1055                                         }
1056                                 }
1057                         }
1058         
1059         }
1060         return ret;
1061
1062 }
1063
1064 u8 rtw_BT_efuse_map_write(PADAPTER padapter, u16 addr, u16 cnts, u8 *data)
1065 {
1066 #define RT_ASSERT_RET(expr)                                                                                             \
1067         if(!(expr)) {                                                                                                                   \
1068                 printk( "Assertion failed! %s at ......\n", #expr);                                                     \
1069                 printk( "      ......%s,%s,line=%d\n",__FILE__,__FUNCTION__,__LINE__);  \
1070                 return _FAIL;   \
1071         }
1072
1073         u8      offset, word_en;
1074         u8      *map;
1075         u8      newdata[PGPKT_DATA_SIZE];
1076         s32     i=0, j=0, idx;
1077         u8      ret = _SUCCESS;
1078         u16     mapLen=0;
1079
1080         EFUSE_GetEfuseDefinition(padapter, EFUSE_BT, TYPE_EFUSE_MAP_LEN, (PVOID)&mapLen, _FALSE);
1081
1082         if ((addr + cnts) > mapLen)
1083                 return _FAIL;
1084
1085         RT_ASSERT_RET(PGPKT_DATA_SIZE == 8); // have to be 8 byte alignment
1086         RT_ASSERT_RET((mapLen & 0x7) == 0); // have to be PGPKT_DATA_SIZE alignment for memcpy
1087
1088         map = rtw_zmalloc(mapLen);
1089         if(map == NULL){
1090                 return _FAIL;
1091         }
1092
1093         ret = rtw_BT_efuse_map_read(padapter, 0, mapLen, map);
1094         if (ret == _FAIL) goto exit;
1095         DBG_871X("OFFSET\tVALUE(hex)\n");
1096         for (i=0; i<1024; i+=16) // set 512 because the iwpriv's extra size have limit 0x7FF
1097         {
1098                         DBG_871X("0x%03x\t", i);
1099                         for (j=0; j<8; j++) {
1100                                 DBG_871X("%02X ", map[i+j]);
1101                         }
1102                         DBG_871X("\t");
1103                         for (; j<16; j++) {
1104                                 DBG_871X("%02X ", map[i+j]);
1105                         }
1106                         DBG_871X("\n");
1107         }
1108         DBG_871X("\n");
1109         Efuse_PowerSwitch(padapter, _TRUE, _TRUE);
1110
1111         idx = 0;
1112         offset = (addr >> 3);
1113         while (idx < cnts)
1114         {
1115                 word_en = 0xF;
1116                 j = (addr + idx) & 0x7;
1117                 _rtw_memcpy(newdata, &map[offset << 3], PGPKT_DATA_SIZE);
1118                 for (i = j; i<PGPKT_DATA_SIZE && idx < cnts; i++, idx++)
1119                 {
1120                         if (data[idx] != map[addr + idx])
1121                         {
1122                                 word_en &= ~BIT(i >> 1);
1123                                 newdata[i] = data[idx];
1124                         }
1125                 }
1126
1127                 if (word_en != 0xF) {
1128                         DBG_871X("offset=%x \n",offset);
1129                         DBG_871X("word_en=%x \n",word_en);
1130                         DBG_871X("%s: data=", __FUNCTION__);
1131                         for(i=0;i<PGPKT_DATA_SIZE;i++)
1132                         {
1133                                 DBG_871X("0x%02X ", newdata[i]);
1134                         }
1135                         DBG_871X("\n");
1136                         ret = Efuse_PgPacketWrite_BT(padapter, offset, word_en, newdata, _FALSE);
1137                         if (ret == _FAIL) break;
1138                 }
1139
1140                 offset++;
1141         }
1142
1143         Efuse_PowerSwitch(padapter, _TRUE, _FALSE);
1144
1145 exit:
1146
1147         rtw_mfree(map, mapLen);
1148
1149         return ret;
1150 }
1151
1152 /*-----------------------------------------------------------------------------
1153  * Function:    Efuse_ReadAllMap
1154  *
1155  * Overview:    Read All Efuse content
1156  *
1157  * Input:       NONE
1158  *
1159  * Output:      NONE
1160  *
1161  * Return:      NONE
1162  *
1163  * Revised History:
1164  * When                 Who             Remark
1165  * 11/11/2008   MHC             Create Version 0.
1166  *
1167  *---------------------------------------------------------------------------*/
1168 VOID 
1169 Efuse_ReadAllMap(
1170         IN              PADAPTER        pAdapter, 
1171         IN              u8              efuseType,
1172         IN OUT  u8              *Efuse,
1173         IN              BOOLEAN         bPseudoTest);
1174 VOID 
1175 Efuse_ReadAllMap(
1176         IN              PADAPTER        pAdapter, 
1177         IN              u8              efuseType,
1178         IN OUT  u8              *Efuse,
1179         IN              BOOLEAN         bPseudoTest)
1180 {
1181         u16     mapLen=0;
1182
1183         Efuse_PowerSwitch(pAdapter,_FALSE, _TRUE);
1184
1185         EFUSE_GetEfuseDefinition(pAdapter, efuseType, TYPE_EFUSE_MAP_LEN, (PVOID)&mapLen, bPseudoTest);
1186
1187         efuse_ReadEFuse(pAdapter, efuseType, 0, mapLen, Efuse, bPseudoTest);
1188
1189         Efuse_PowerSwitch(pAdapter,_FALSE, _FALSE);
1190 }
1191
1192 /*-----------------------------------------------------------------------------
1193  * Function:    efuse_ShadowRead1Byte
1194  *                      efuse_ShadowRead2Byte
1195  *                      efuse_ShadowRead4Byte
1196  *
1197  * Overview:    Read from efuse init map by one/two/four bytes !!!!!
1198  *
1199  * Input:       NONE
1200  *
1201  * Output:      NONE
1202  *
1203  * Return:      NONE
1204  *
1205  * Revised History:
1206  * When                 Who             Remark
1207  * 11/12/2008   MHC             Create Version 0.
1208  *
1209  *---------------------------------------------------------------------------*/
1210 static VOID
1211 efuse_ShadowRead1Byte(
1212         IN      PADAPTER        pAdapter,
1213         IN      u16             Offset,
1214         IN OUT  u8              *Value)
1215 {
1216         PHAL_DATA_TYPE pHalData = GET_HAL_DATA(pAdapter);
1217
1218         *Value = pHalData->efuse_eeprom_data[Offset];
1219
1220 }       // EFUSE_ShadowRead1Byte
1221
1222 //---------------Read Two Bytes
1223 static VOID
1224 efuse_ShadowRead2Byte(
1225         IN      PADAPTER        pAdapter,
1226         IN      u16             Offset,
1227         IN OUT  u16             *Value)
1228 {
1229         PHAL_DATA_TYPE pHalData = GET_HAL_DATA(pAdapter);
1230
1231         *Value = pHalData->efuse_eeprom_data[Offset];
1232         *Value |= pHalData->efuse_eeprom_data[Offset+1]<<8;
1233
1234 }       // EFUSE_ShadowRead2Byte
1235
1236 //---------------Read Four Bytes
1237 static VOID
1238 efuse_ShadowRead4Byte(
1239         IN      PADAPTER        pAdapter,
1240         IN      u16             Offset,
1241         IN OUT  u32             *Value)
1242 {
1243         PHAL_DATA_TYPE pHalData = GET_HAL_DATA(pAdapter);
1244
1245         *Value = pHalData->efuse_eeprom_data[Offset];
1246         *Value |= pHalData->efuse_eeprom_data[Offset+1]<<8;
1247         *Value |= pHalData->efuse_eeprom_data[Offset+2]<<16;
1248         *Value |= pHalData->efuse_eeprom_data[Offset+3]<<24;
1249
1250 }       // efuse_ShadowRead4Byte
1251
1252
1253 /*-----------------------------------------------------------------------------
1254  * Function:    efuse_ShadowWrite1Byte
1255  *                      efuse_ShadowWrite2Byte
1256  *                      efuse_ShadowWrite4Byte
1257  *
1258  * Overview:    Write efuse modify map by one/two/four byte.
1259  *
1260  * Input:       NONE
1261  *
1262  * Output:      NONE
1263  *
1264  * Return:      NONE
1265  *
1266  * Revised History:
1267  * When                 Who             Remark
1268  * 11/12/2008   MHC             Create Version 0.
1269  *
1270  *---------------------------------------------------------------------------*/
1271 #ifdef PLATFORM
1272 static VOID
1273 efuse_ShadowWrite1Byte(
1274         IN      PADAPTER        pAdapter,
1275         IN      u16             Offset,
1276         IN      u8              Value);
1277 #endif //PLATFORM
1278 static VOID
1279 efuse_ShadowWrite1Byte(
1280         IN      PADAPTER        pAdapter,
1281         IN      u16             Offset,
1282         IN      u8              Value)
1283 {
1284         PHAL_DATA_TYPE pHalData = GET_HAL_DATA(pAdapter);
1285
1286         pHalData->efuse_eeprom_data[Offset] = Value;
1287
1288 }       // efuse_ShadowWrite1Byte
1289
1290 //---------------Write Two Bytes
1291 static VOID
1292 efuse_ShadowWrite2Byte(
1293         IN      PADAPTER        pAdapter,
1294         IN      u16             Offset,
1295         IN      u16             Value)
1296 {
1297         
1298         PHAL_DATA_TYPE pHalData = GET_HAL_DATA(pAdapter);
1299         
1300
1301         pHalData->efuse_eeprom_data[Offset] = Value&0x00FF;
1302         pHalData->efuse_eeprom_data[Offset+1] = Value>>8;
1303
1304 }       // efuse_ShadowWrite1Byte
1305
1306 //---------------Write Four Bytes
1307 static VOID
1308 efuse_ShadowWrite4Byte(
1309         IN      PADAPTER        pAdapter,
1310         IN      u16             Offset,
1311         IN      u32             Value)
1312 {
1313         PHAL_DATA_TYPE pHalData = GET_HAL_DATA(pAdapter);
1314
1315         pHalData->efuse_eeprom_data[Offset] = (u8)(Value&0x000000FF);
1316         pHalData->efuse_eeprom_data[Offset+1] = (u8)((Value>>8)&0x0000FF);
1317         pHalData->efuse_eeprom_data[Offset+2] = (u8)((Value>>16)&0x00FF);
1318         pHalData->efuse_eeprom_data[Offset+3] = (u8)((Value>>24)&0xFF);
1319
1320 }       // efuse_ShadowWrite1Byte
1321
1322 /*-----------------------------------------------------------------------------
1323  * Function:    EFUSE_ShadowMapUpdate
1324  *
1325  * Overview:    Transfer current EFUSE content to shadow init and modify map.
1326  *
1327  * Input:       NONE
1328  *
1329  * Output:      NONE
1330  *
1331  * Return:      NONE
1332  *
1333  * Revised History:
1334  * When                 Who             Remark
1335  * 11/13/2008   MHC             Create Version 0.
1336  *
1337  *---------------------------------------------------------------------------*/
1338 void EFUSE_ShadowMapUpdate(
1339         IN PADAPTER     pAdapter,
1340         IN u8           efuseType,
1341         IN BOOLEAN      bPseudoTest)
1342 {
1343         PHAL_DATA_TYPE pHalData = GET_HAL_DATA(pAdapter);
1344         u16     mapLen=0;
1345
1346         EFUSE_GetEfuseDefinition(pAdapter, efuseType, TYPE_EFUSE_MAP_LEN, (PVOID)&mapLen, bPseudoTest);
1347
1348         if (pHalData->bautoload_fail_flag == _TRUE)
1349         {
1350                 _rtw_memset(pHalData->efuse_eeprom_data, 0xFF, mapLen);
1351         }
1352         else
1353         {
1354                 #ifdef CONFIG_ADAPTOR_INFO_CACHING_FILE                 
1355                 if(_SUCCESS != retriveAdaptorInfoFile(pAdapter->registrypriv.adaptor_info_caching_file_path, pHalData->efuse_eeprom_data)) {
1356                 #endif
1357                 
1358                 Efuse_ReadAllMap(pAdapter, efuseType, pHalData->efuse_eeprom_data, bPseudoTest);
1359                 
1360                 #ifdef CONFIG_ADAPTOR_INFO_CACHING_FILE
1361                         storeAdaptorInfoFile(pAdapter->registrypriv.adaptor_info_caching_file_path, pHalData->efuse_eeprom_data);
1362                 }
1363                 #endif
1364         }
1365
1366         //PlatformMoveMemory((PVOID)&pHalData->EfuseMap[EFUSE_MODIFY_MAP][0], 
1367         //(PVOID)&pHalData->EfuseMap[EFUSE_INIT_MAP][0], mapLen);
1368 }// EFUSE_ShadowMapUpdate
1369
1370
1371 /*-----------------------------------------------------------------------------
1372  * Function:    EFUSE_ShadowRead
1373  *
1374  * Overview:    Read from efuse init map !!!!!
1375  *
1376  * Input:       NONE
1377  *
1378  * Output:      NONE
1379  *
1380  * Return:      NONE
1381  *
1382  * Revised History:
1383  * When                 Who             Remark
1384  * 11/12/2008   MHC             Create Version 0.
1385  *
1386  *---------------------------------------------------------------------------*/
1387 void
1388 EFUSE_ShadowRead(
1389         IN              PADAPTER        pAdapter,
1390         IN              u8              Type,
1391         IN              u16             Offset,
1392         IN OUT  u32             *Value  )
1393 {
1394         if (Type == 1)
1395                 efuse_ShadowRead1Byte(pAdapter, Offset, (u8 *)Value);
1396         else if (Type == 2)
1397                 efuse_ShadowRead2Byte(pAdapter, Offset, (u16 *)Value);
1398         else if (Type == 4)
1399                 efuse_ShadowRead4Byte(pAdapter, Offset, (u32 *)Value);
1400         
1401 }       // EFUSE_ShadowRead
1402
1403 /*-----------------------------------------------------------------------------
1404  * Function:    EFUSE_ShadowWrite
1405  *
1406  * Overview:    Write efuse modify map for later update operation to use!!!!!
1407  *
1408  * Input:       NONE
1409  *
1410  * Output:      NONE
1411  *
1412  * Return:      NONE
1413  *
1414  * Revised History:
1415  * When                 Who             Remark
1416  * 11/12/2008   MHC             Create Version 0.
1417  *
1418  *---------------------------------------------------------------------------*/
1419 VOID
1420 EFUSE_ShadowWrite(
1421         IN      PADAPTER        pAdapter,
1422         IN      u8              Type,
1423         IN      u16             Offset,
1424         IN OUT  u32             Value);
1425 VOID
1426 EFUSE_ShadowWrite(
1427         IN      PADAPTER        pAdapter,
1428         IN      u8              Type,
1429         IN      u16             Offset,
1430         IN OUT  u32             Value)
1431 {
1432 #if (MP_DRIVER == 0)
1433         return;
1434 #endif
1435         if ( pAdapter->registrypriv.mp_mode == 0)
1436                 return;
1437
1438
1439         if (Type == 1)
1440                 efuse_ShadowWrite1Byte(pAdapter, Offset, (u8)Value);
1441         else if (Type == 2)
1442                 efuse_ShadowWrite2Byte(pAdapter, Offset, (u16)Value);
1443         else if (Type == 4)
1444                 efuse_ShadowWrite4Byte(pAdapter, Offset, (u32)Value);
1445
1446 }       // EFUSE_ShadowWrite
1447
1448 VOID
1449 Efuse_InitSomeVar(
1450         IN              PADAPTER        pAdapter
1451         );
1452 VOID
1453 Efuse_InitSomeVar(
1454         IN              PADAPTER        pAdapter
1455         )
1456 {
1457         u8 i;
1458         
1459         _rtw_memset((PVOID)&fakeEfuseContent[0], 0xff, EFUSE_MAX_HW_SIZE);
1460         _rtw_memset((PVOID)&fakeEfuseInitMap[0], 0xff, EFUSE_MAX_MAP_LEN);
1461         _rtw_memset((PVOID)&fakeEfuseModifiedMap[0], 0xff, EFUSE_MAX_MAP_LEN);
1462
1463         for(i=0; i<EFUSE_MAX_BT_BANK; i++)
1464         {
1465                 _rtw_memset((PVOID)&BTEfuseContent[i][0], EFUSE_MAX_HW_SIZE, 0xff);
1466         }
1467         _rtw_memset((PVOID)&BTEfuseInitMap[0], 0xff, EFUSE_BT_MAX_MAP_LEN);
1468         _rtw_memset((PVOID)&BTEfuseModifiedMap[0], 0xff, EFUSE_BT_MAX_MAP_LEN);
1469
1470         for(i=0; i<EFUSE_MAX_BT_BANK; i++)
1471         {
1472                 _rtw_memset((PVOID)&fakeBTEfuseContent[i][0], 0xff, EFUSE_MAX_HW_SIZE);
1473         }
1474         _rtw_memset((PVOID)&fakeBTEfuseInitMap[0], 0xff, EFUSE_BT_MAX_MAP_LEN);
1475         _rtw_memset((PVOID)&fakeBTEfuseModifiedMap[0], 0xff, EFUSE_BT_MAX_MAP_LEN);
1476 }
1477
1478 #ifdef PLATFORM_LINUX
1479 #ifdef CONFIG_ADAPTOR_INFO_CACHING_FILE
1480 //#include <rtw_eeprom.h>
1481
1482  int isAdaptorInfoFileValid(void)
1483 {
1484         return _TRUE;
1485 }
1486
1487 int storeAdaptorInfoFile(char *path, u8* efuse_data)
1488 {
1489         int ret =_SUCCESS;
1490
1491         if(path && efuse_data) {
1492                 ret = rtw_store_to_file(path, efuse_data, EEPROM_MAX_SIZE_512);
1493                 if(ret == EEPROM_MAX_SIZE)
1494                         ret = _SUCCESS;
1495                 else
1496                         ret = _FAIL;
1497         } else {
1498                 DBG_871X("%s NULL pointer\n",__FUNCTION__);
1499                 ret =  _FAIL;
1500         }
1501         return ret;
1502 }
1503
1504 int retriveAdaptorInfoFile(char *path, u8* efuse_data)
1505 {
1506         int ret = _SUCCESS;
1507         mm_segment_t oldfs;
1508         struct file *fp;
1509         
1510         if(path && efuse_data) {
1511
1512                 ret = rtw_retrive_from_file(path, efuse_data, EEPROM_MAX_SIZE);
1513                 
1514                 if(ret == EEPROM_MAX_SIZE)
1515                         ret = _SUCCESS;
1516                 else
1517                         ret = _FAIL;
1518
1519                 #if 0
1520                 if(isAdaptorInfoFileValid()) {  
1521                         return 0;
1522                 } else {
1523                         return _FAIL;
1524                 }
1525                 #endif
1526                 
1527         } else {
1528                 DBG_871X("%s NULL pointer\n",__FUNCTION__);
1529                 ret = _FAIL;
1530         }
1531         return ret;
1532 }
1533 #endif //CONFIG_ADAPTOR_INFO_CACHING_FILE
1534 #endif //PLATFORM_LINUX
1535
1536 #ifdef CONFIG_EFUSE_CONFIG_FILE
1537
1538 void Rtw_Hal_ReadMACAddrFromFile(PADAPTER padapter)
1539 {
1540         u32 i;
1541         struct file *fp;
1542         mm_segment_t fs;
1543         u8 source_addr[18];
1544         loff_t pos = 0;
1545         u32 curtime = rtw_get_current_time();
1546         PHAL_DATA_TYPE pHalData = GET_HAL_DATA(padapter);
1547         u8 *head, *end;
1548
1549         u8 null_mac_addr[ETH_ALEN] = {0, 0, 0,0, 0, 0};
1550         u8 multi_mac_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1551         
1552         _rtw_memset(source_addr, 0, 18);
1553         _rtw_memset(pHalData->EEPROMMACAddr, 0, ETH_ALEN);
1554
1555         fp = filp_open("/data/wifimac.txt", O_RDWR,  0644);
1556         if (IS_ERR(fp)) {
1557                 pHalData->bloadmac_fail_flag = _TRUE;
1558                 DBG_871X("Error, wifi mac address file doesn't exist.\n");
1559         } else {
1560                 fs = get_fs();
1561                 set_fs(KERNEL_DS);
1562
1563                 DBG_871X("wifi mac address:\n");
1564                 vfs_read(fp, source_addr, 18, &pos);
1565                 source_addr[17] = ':';
1566
1567                 head = end = source_addr;
1568                 for (i=0; i<ETH_ALEN; i++) {
1569                         while (end && (*end != ':') )
1570                                 end++;
1571
1572                         if (end && (*end == ':') )
1573                                 *end = '\0';
1574
1575                         pHalData->EEPROMMACAddr[i] = simple_strtoul(head, NULL, 16 );
1576
1577                         if (end) {
1578                                 end++;
1579                                 head = end;
1580                         }
1581                         DBG_871X("%02x \n", pHalData->EEPROMMACAddr[i]);
1582                 }
1583                 DBG_871X("\n");
1584                 set_fs(fs);
1585                 pHalData->bloadmac_fail_flag = _FALSE;
1586                 filp_close(fp, NULL);
1587         }
1588
1589         if ( (_rtw_memcmp(pHalData->EEPROMMACAddr, null_mac_addr, ETH_ALEN)) ||
1590                 (_rtw_memcmp(pHalData->EEPROMMACAddr, multi_mac_addr, ETH_ALEN)) ) {
1591                 pHalData->EEPROMMACAddr[0] = 0x00;
1592                 pHalData->EEPROMMACAddr[1] = 0xe0;
1593                 pHalData->EEPROMMACAddr[2] = 0x4c;
1594                 pHalData->EEPROMMACAddr[3] = (u8)(curtime & 0xff) ;
1595                 pHalData->EEPROMMACAddr[4] = (u8)((curtime>>8) & 0xff) ;
1596                 pHalData->EEPROMMACAddr[5] = (u8)((curtime>>16) & 0xff) ;
1597         }
1598
1599         DBG_871X("Hal_ReadMACAddrFromFile: Permanent Address = %02x-%02x-%02x-%02x-%02x-%02x !!!\n",
1600                   pHalData->EEPROMMACAddr[0], pHalData->EEPROMMACAddr[1],
1601                   pHalData->EEPROMMACAddr[2], pHalData->EEPROMMACAddr[3],
1602                   pHalData->EEPROMMACAddr[4], pHalData->EEPROMMACAddr[5]);
1603 }
1604
1605
1606 u32 Rtw_Hal_readPGDataFromConfigFile(PADAPTER   padapter)
1607 {
1608         u32 i;
1609         struct file *fp;
1610         mm_segment_t fs;
1611         u8 temp[3];
1612         loff_t pos = 0;
1613         PHAL_DATA_TYPE pHalData = GET_HAL_DATA(padapter);
1614         u8      *PROMContent = pHalData->efuse_eeprom_data;
1615
1616
1617         temp[2] = 0; // add end of string '\0'
1618
1619         fp = filp_open("/system/etc/wifi/wifi_efuse.map", O_RDWR,  0644);
1620         if (IS_ERR(fp)) {
1621                 pHalData->bloadfile_fail_flag = _TRUE;
1622                 DBG_871X("Error, Efuse configure file doesn't exist.\n");
1623                 return _FAIL;
1624         }
1625
1626         fs = get_fs();
1627         set_fs(KERNEL_DS);
1628
1629         DBG_871X("Efuse configure file:\n");
1630         for (i=0; i< EFUSE_MAP_SIZE  ; i++) {
1631                 vfs_read(fp, temp, 2, &pos);
1632                 PROMContent[i] = simple_strtoul(temp, NULL, 16 );
1633                 pos += 1; // Filter the space character
1634                 DBG_871X("%02X \n", PROMContent[i]);
1635         }
1636         DBG_871X("\n");
1637         set_fs(fs);
1638
1639         filp_close(fp, NULL);
1640         
1641         pHalData->bloadfile_fail_flag = _FALSE;
1642         
1643         return _SUCCESS;
1644 }
1645
1646 #endif //#CONFIG_EFUSE_CONFIG_FILE