0fb4726ea52413b5f0e656fa94c99cf246498407
[firefly-linux-kernel-4.4.55.git] / drivers / staging / rtl8723au / core / 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  ******************************************************************************/
15 #define _RTW_EFUSE_C_
16
17 #include <osdep_service.h>
18 #include <drv_types.h>
19
20 #include <rtw_efuse.h>
21 #include <rtl8723a_hal.h>
22 #include <usb_ops_linux.h>
23
24 #define REG_EFUSE_CTRL          0x0030
25 #define EFUSE_CTRL              REG_EFUSE_CTRL  /* E-Fuse Control */
26
27 #define VOLTAGE_V25             0x03
28 #define LDOE25_SHIFT            28
29
30 /*
31  * When we want to enable write operation, we should change to
32  * pwr on state. When we stop write, we should switch to 500k mode
33  * and disable LDO 2.5V.
34  */
35 static void Efuse_PowerSwitch(struct rtw_adapter *padapter,
36                               u8 bWrite, u8 PwrState)
37 {
38         u8 tempval;
39         u16 tmpV16;
40
41         if (PwrState == true) {
42                 rtl8723au_write8(padapter, REG_EFUSE_ACCESS, EFUSE_ACCESS_ON);
43
44                 /*
45                  * 1.2V Power: From VDDON with Power
46                  * Cut(0x0000h[15]), default valid
47                  */
48                 tmpV16 = rtl8723au_read16(padapter, REG_SYS_ISO_CTRL);
49                 if (!(tmpV16 & PWC_EV12V)) {
50                         tmpV16 |= PWC_EV12V;
51                         rtl8723au_write16(padapter, REG_SYS_ISO_CTRL, tmpV16);
52                 }
53                 /* Reset: 0x0000h[28], default valid */
54                 tmpV16 = rtl8723au_read16(padapter, REG_SYS_FUNC_EN);
55                 if (!(tmpV16 & FEN_ELDR)) {
56                         tmpV16 |= FEN_ELDR;
57                         rtl8723au_write16(padapter, REG_SYS_FUNC_EN, tmpV16);
58                 }
59
60                 /*
61                  * Clock: Gated(0x0008h[5]) 8M(0x0008h[1])
62                  * clock from ANA, default valid
63                  */
64                 tmpV16 = rtl8723au_read16(padapter, REG_SYS_CLKR);
65                 if ((!(tmpV16 & LOADER_CLK_EN)) || (!(tmpV16 & ANA8M))) {
66                         tmpV16 |= (LOADER_CLK_EN | ANA8M);
67                         rtl8723au_write16(padapter, REG_SYS_CLKR, tmpV16);
68                 }
69
70                 if (bWrite == true) {
71                         /*  Enable LDO 2.5V before read/write action */
72                         tempval = rtl8723au_read8(padapter, EFUSE_TEST + 3);
73                         tempval &= 0x0F;
74                         tempval |= (VOLTAGE_V25 << 4);
75                         rtl8723au_write8(padapter, EFUSE_TEST + 3,
76                                          tempval | 0x80);
77                 }
78         } else {
79                 rtl8723au_write8(padapter, REG_EFUSE_ACCESS, EFUSE_ACCESS_OFF);
80
81                 if (bWrite == true) {
82                         /*  Disable LDO 2.5V after read/write action */
83                         tempval = rtl8723au_read8(padapter, EFUSE_TEST + 3);
84                         rtl8723au_write8(padapter, EFUSE_TEST + 3,
85                                          tempval & 0x7F);
86                 }
87         }
88 }
89
90 u16
91 Efuse_GetCurrentSize23a(struct rtw_adapter *pAdapter, u8 efuseType)
92 {
93         u16 ret = 0;
94
95         if (efuseType == EFUSE_WIFI)
96                 ret = rtl8723a_EfuseGetCurrentSize_WiFi(pAdapter);
97         else
98                 ret = rtl8723a_EfuseGetCurrentSize_BT(pAdapter);
99
100         return ret;
101 }
102
103 /* Get current efuse area enabled word */
104 u8
105 Efuse_CalculateWordCnts23a(u8 word_en)
106 {
107         return hweight8((~word_en) & 0xf);
108 }
109
110 /*
111  * Description: Execute E-Fuse read byte operation.
112  *
113  * Assumptions: 1. Boot from E-Fuse and successfully auto-load.
114  *              2. PASSIVE_LEVEL (USB interface)
115  */
116 void
117 ReadEFuseByte23a(struct rtw_adapter *Adapter, u16 _offset, u8 *pbuf)
118 {
119         u32     value32;
120         u8      readbyte;
121         u16     retry;
122
123         /* Write Address */
124         rtl8723au_write8(Adapter, EFUSE_CTRL+1, (_offset & 0xff));
125         readbyte = rtl8723au_read8(Adapter, EFUSE_CTRL+2);
126         rtl8723au_write8(Adapter, EFUSE_CTRL+2,
127                          ((_offset >> 8) & 0x03) | (readbyte & 0xfc));
128
129         /* Write bit 32 0 */
130         readbyte = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
131         rtl8723au_write8(Adapter, EFUSE_CTRL+3, readbyte & 0x7f);
132
133         /* Check bit 32 read-ready */
134         retry = 0;
135         value32 = rtl8723au_read32(Adapter, EFUSE_CTRL);
136         while (!((value32 >> 24) & 0x80) && retry < 10000) {
137                 value32 = rtl8723au_read32(Adapter, EFUSE_CTRL);
138                 retry++;
139         }
140
141         /*
142          * Added suggested delay. This fixes the problem that
143          * Efuse read error in high temperature condition.
144          * Designer says that there shall be some delay after
145          * ready bit is set, or the result will always stay
146          * on last data we read.
147          */
148         udelay(50);
149         value32 = rtl8723au_read32(Adapter, EFUSE_CTRL);
150
151         *pbuf = (u8)(value32 & 0xff);
152 }
153
154 void
155 EFUSE_GetEfuseDefinition23a(struct rtw_adapter *pAdapter, u8 efuseType,
156                             u8 type, void *pOut)
157 {
158         u8 *pu1Tmp;
159         u16 *pu2Tmp;
160         u8 *pMax_section;
161
162         switch (type) {
163         case TYPE_EFUSE_MAX_SECTION:
164                 pMax_section = pOut;
165
166                 if (efuseType == EFUSE_WIFI)
167                         *pMax_section = EFUSE_MAX_SECTION_8723A;
168                 else
169                         *pMax_section = EFUSE_BT_MAX_SECTION;
170                 break;
171
172         case TYPE_EFUSE_REAL_CONTENT_LEN:
173                 pu2Tmp = pOut;
174
175                 if (efuseType == EFUSE_WIFI)
176                         *pu2Tmp = EFUSE_REAL_CONTENT_LEN_8723A;
177                 else
178                         *pu2Tmp = EFUSE_BT_REAL_CONTENT_LEN;
179                 break;
180
181         case TYPE_AVAILABLE_EFUSE_BYTES_BANK:
182                 pu2Tmp = pOut;
183
184                 if (efuseType == EFUSE_WIFI)
185                         *pu2Tmp = (EFUSE_REAL_CONTENT_LEN_8723A -
186                                    EFUSE_OOB_PROTECT_BYTES);
187                 else
188                         *pu2Tmp = (EFUSE_BT_REAL_BANK_CONTENT_LEN -
189                                    EFUSE_PROTECT_BYTES_BANK);
190                 break;
191
192         case TYPE_AVAILABLE_EFUSE_BYTES_TOTAL:
193                 pu2Tmp = pOut;
194
195                 if (efuseType == EFUSE_WIFI)
196                         *pu2Tmp = (EFUSE_REAL_CONTENT_LEN_8723A -
197                                    EFUSE_OOB_PROTECT_BYTES);
198                 else
199                         *pu2Tmp = (EFUSE_BT_REAL_CONTENT_LEN -
200                                    (EFUSE_PROTECT_BYTES_BANK * 3));
201                 break;
202
203         case TYPE_EFUSE_MAP_LEN:
204                 pu2Tmp = pOut;
205
206                 if (efuseType == EFUSE_WIFI)
207                         *pu2Tmp = EFUSE_MAP_LEN_8723A;
208                 else
209                         *pu2Tmp = EFUSE_BT_MAP_LEN;
210                 break;
211
212         case TYPE_EFUSE_PROTECT_BYTES_BANK:
213                 pu1Tmp = pOut;
214
215                 if (efuseType == EFUSE_WIFI)
216                         *pu1Tmp = EFUSE_OOB_PROTECT_BYTES;
217                 else
218                         *pu1Tmp = EFUSE_PROTECT_BYTES_BANK;
219                 break;
220
221         case TYPE_EFUSE_CONTENT_LEN_BANK:
222                 pu2Tmp = pOut;
223
224                 if (efuseType == EFUSE_WIFI)
225                         *pu2Tmp = EFUSE_REAL_CONTENT_LEN_8723A;
226                 else
227                         *pu2Tmp = EFUSE_BT_REAL_BANK_CONTENT_LEN;
228                 break;
229
230         default:
231                 pu1Tmp = pOut;
232                 *pu1Tmp = 0;
233                 break;
234         }
235 }
236
237 /* Copy from WMAC for EFUSE read 1 byte. */
238 u8
239 EFUSE_Read1Byte23a(struct rtw_adapter *Adapter, u16 Address)
240 {
241         u8      data;
242         u8      Bytetemp = {0x00};
243         u8      temp = {0x00};
244         u32     k = 0;
245         u16     contentLen = 0;
246
247         EFUSE_GetEfuseDefinition23a(Adapter, EFUSE_WIFI,
248                                  TYPE_EFUSE_REAL_CONTENT_LEN,
249                                  (void *)&contentLen);
250
251         if (Address < contentLen) { /* E-fuse 512Byte */
252                 /* Write E-fuse Register address bit0~7 */
253                 temp = Address & 0xFF;
254                 rtl8723au_write8(Adapter, EFUSE_CTRL+1, temp);
255                 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+2);
256                 /* Write E-fuse Register address bit8~9 */
257                 temp = ((Address >> 8) & 0x03) | (Bytetemp & 0xFC);
258                 rtl8723au_write8(Adapter, EFUSE_CTRL+2, temp);
259
260                 /* Write 0x30[31]= 0 */
261                 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
262                 temp = Bytetemp & 0x7F;
263                 rtl8723au_write8(Adapter, EFUSE_CTRL+3, temp);
264
265                 /* Wait Write-ready (0x30[31]= 1) */
266                 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
267                 while (!(Bytetemp & 0x80)) {
268                         Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
269                         k++;
270                         if (k == 1000) {
271                                 k = 0;
272                                 break;
273                         }
274                 }
275                 data = rtl8723au_read8(Adapter, EFUSE_CTRL);
276                 return data;
277         } else
278                 return 0xFF;
279 }
280
281 /* Copy from WMAC fot EFUSE write 1 byte. */
282 void
283 EFUSE_Write1Byte(struct rtw_adapter *Adapter, u16 Address, u8 Value);
284 void
285 EFUSE_Write1Byte(struct rtw_adapter *Adapter, u16 Address, u8 Value)
286 {
287         u8      Bytetemp = {0x00};
288         u8      temp = {0x00};
289         u32     k = 0;
290         u16     contentLen = 0;
291
292         EFUSE_GetEfuseDefinition23a(Adapter, EFUSE_WIFI,
293                                  TYPE_EFUSE_REAL_CONTENT_LEN,
294                                  (void *)&contentLen);
295
296         if (Address < contentLen) { /* E-fuse 512Byte */
297                 rtl8723au_write8(Adapter, EFUSE_CTRL, Value);
298
299                 /* Write E-fuse Register address bit0~7 */
300                 temp = Address & 0xFF;
301                 rtl8723au_write8(Adapter, EFUSE_CTRL+1, temp);
302                 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+2);
303
304                 /* Write E-fuse Register address bit8~9 */
305                 temp = ((Address >> 8) & 0x03) | (Bytetemp & 0xFC);
306                 rtl8723au_write8(Adapter, EFUSE_CTRL+2, temp);
307
308                 /* Write 0x30[31]= 1 */
309                 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
310                 temp = Bytetemp | 0x80;
311                 rtl8723au_write8(Adapter, EFUSE_CTRL+3, temp);
312
313                 /* Wait Write-ready (0x30[31]= 0) */
314                 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
315                 while (Bytetemp & 0x80) {
316                         Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
317                         k++;
318                         if (k == 100) {
319                                 k = 0;
320                                 break;
321                         }
322                 }
323         }
324 }
325
326 /* Read one byte from real Efuse. */
327 int
328 efuse_OneByteRead23a(struct rtw_adapter *pAdapter, u16 addr, u8 *data)
329 {
330         u8      tmpidx = 0;
331         int     bResult;
332
333         /*  -----------------e-fuse reg ctrl ---------------------------- */
334         /* address */
335         rtl8723au_write8(pAdapter, EFUSE_CTRL + 1, (u8)(addr & 0xff));
336         rtl8723au_write8(pAdapter, EFUSE_CTRL + 2,
337                          ((u8)((addr >> 8) & 0x03)) |
338                          (rtl8723au_read8(pAdapter, EFUSE_CTRL + 2) & 0xFC));
339
340         rtl8723au_write8(pAdapter, EFUSE_CTRL + 3, 0x72); /* read cmd */
341
342         while (!(0x80 & rtl8723au_read8(pAdapter, EFUSE_CTRL + 3)) &&
343                (tmpidx < 100))
344                 tmpidx++;
345         if (tmpidx < 100) {
346                 *data = rtl8723au_read8(pAdapter, EFUSE_CTRL);
347                 bResult = _SUCCESS;
348         } else {
349                 *data = 0xff;
350                 bResult = _FAIL;
351         }
352         return bResult;
353 }
354
355 /* Write one byte to reald Efuse. */
356 int
357 efuse_OneByteWrite23a(struct rtw_adapter *pAdapter, u16 addr, u8 data)
358 {
359         u8      tmpidx = 0;
360         int     bResult;
361
362         /* return       0; */
363
364         /*  -----------------e-fuse reg ctrl ------------------------- */
365         /* address */
366         rtl8723au_write8(pAdapter, EFUSE_CTRL + 1, (u8)(addr & 0xff));
367         rtl8723au_write8(pAdapter, EFUSE_CTRL + 2,
368                          (rtl8723au_read8(pAdapter, EFUSE_CTRL + 2) & 0xFC) |
369                          (u8)((addr >> 8) & 0x03));
370         rtl8723au_write8(pAdapter, EFUSE_CTRL, data); /* data */
371
372         rtl8723au_write8(pAdapter, EFUSE_CTRL + 3, 0xF2); /* write cmd */
373
374         while ((0x80 & rtl8723au_read8(pAdapter, EFUSE_CTRL + 3)) &&
375                (tmpidx < 100)) {
376                 tmpidx++;
377         }
378
379         if (tmpidx < 100)
380                 bResult = _SUCCESS;
381         else
382                 bResult = _FAIL;
383
384         return bResult;
385 }
386
387 /* Read allowed word in current efuse section data. */
388 void
389 efuse_WordEnableDataRead23a(u8  word_en,
390                          u8     *sourdata,
391                          u8     *targetdata)
392 {
393         if (!(word_en&BIT(0))) {
394                 targetdata[0] = sourdata[0];
395                 targetdata[1] = sourdata[1];
396         }
397         if (!(word_en&BIT(1))) {
398                 targetdata[2] = sourdata[2];
399                 targetdata[3] = sourdata[3];
400         }
401         if (!(word_en&BIT(2))) {
402                 targetdata[4] = sourdata[4];
403                 targetdata[5] = sourdata[5];
404         }
405         if (!(word_en&BIT(3))) {
406                 targetdata[6] = sourdata[6];
407                 targetdata[7] = sourdata[7];
408         }
409 }
410
411 static int efuse_read8(struct rtw_adapter *padapter, u16 address, u8 *value)
412 {
413         return efuse_OneByteRead23a(padapter, address, value);
414 }
415
416 static int efuse_write8(struct rtw_adapter *padapter, u16 address, u8 *value)
417 {
418         return efuse_OneByteWrite23a(padapter, address, *value);
419 }
420
421 /* read/write raw efuse data */
422 int rtw_efuse_access23a(struct rtw_adapter *padapter, u8 bWrite, u16 start_addr,
423                         u16 cnts, u8 *data)
424 {
425         int i = 0;
426         u16 real_content_len = 0, max_available_size = 0;
427         int res = _FAIL;
428         int (*rw8)(struct rtw_adapter *, u16, u8*);
429
430         EFUSE_GetEfuseDefinition23a(padapter, EFUSE_WIFI,
431                                  TYPE_EFUSE_REAL_CONTENT_LEN,
432                                  (void *)&real_content_len);
433         EFUSE_GetEfuseDefinition23a(padapter, EFUSE_WIFI,
434                                  TYPE_AVAILABLE_EFUSE_BYTES_TOTAL,
435                                  (void *)&max_available_size);
436
437         if (start_addr > real_content_len)
438                 return _FAIL;
439
440         if (true == bWrite) {
441                 if ((start_addr + cnts) > max_available_size)
442                         return _FAIL;
443                 rw8 = &efuse_write8;
444         } else
445                 rw8 = &efuse_read8;
446
447         Efuse_PowerSwitch(padapter, bWrite, true);
448
449         /* e-fuse one byte read/write */
450         for (i = 0; i < cnts; i++) {
451                 if (start_addr >= real_content_len) {
452                         res = _FAIL;
453                         break;
454                 }
455
456                 res = rw8(padapter, start_addr++, data++);
457                 if (res == _FAIL)
458                         break;
459         }
460
461         Efuse_PowerSwitch(padapter, bWrite, false);
462
463         return res;
464 }
465
466 u16 efuse_GetMaxSize23a(struct rtw_adapter *padapter)
467 {
468         u16 max_size;
469
470         EFUSE_GetEfuseDefinition23a(padapter, EFUSE_WIFI,
471                                  TYPE_AVAILABLE_EFUSE_BYTES_TOTAL,
472                                  (void *)&max_size);
473         return max_size;
474 }
475
476 int rtw_efuse_map_read23a(struct rtw_adapter *padapter,
477                           u16 addr, u16 cnts, u8 *data)
478 {
479         u16 mapLen = 0;
480
481         EFUSE_GetEfuseDefinition23a(padapter, EFUSE_WIFI,
482                                  TYPE_EFUSE_MAP_LEN, (void *)&mapLen);
483
484         if ((addr + cnts) > mapLen)
485                 return _FAIL;
486
487         Efuse_PowerSwitch(padapter, false, true);
488
489         rtl8723a_readefuse(padapter, EFUSE_WIFI, addr, cnts, data);
490
491         Efuse_PowerSwitch(padapter, false, false);
492
493         return _SUCCESS;
494 }
495
496 int rtw_BT_efuse_map_read23a(struct rtw_adapter *padapter,
497                              u16 addr, u16 cnts, u8 *data)
498 {
499         u16 mapLen = 0;
500
501         EFUSE_GetEfuseDefinition23a(padapter, EFUSE_BT,
502                                  TYPE_EFUSE_MAP_LEN, (void *)&mapLen);
503
504         if ((addr + cnts) > mapLen)
505                 return _FAIL;
506
507         Efuse_PowerSwitch(padapter, false, true);
508
509         rtl8723a_readefuse(padapter, EFUSE_BT, addr, cnts, data);
510
511         Efuse_PowerSwitch(padapter, false, false);
512
513         return _SUCCESS;
514 }
515
516 /* Read All Efuse content */
517 void
518 Efuse_ReadAllMap(struct rtw_adapter *pAdapter, u8 efuseType, u8 *Efuse);
519 void
520 Efuse_ReadAllMap(struct rtw_adapter *pAdapter, u8 efuseType, u8 *Efuse)
521 {
522         u16     mapLen = 0;
523
524         Efuse_PowerSwitch(pAdapter, false, true);
525
526         EFUSE_GetEfuseDefinition23a(pAdapter, efuseType, TYPE_EFUSE_MAP_LEN,
527                                  (void *)&mapLen);
528
529         rtl8723a_readefuse(pAdapter, efuseType, 0, mapLen, Efuse);
530
531         Efuse_PowerSwitch(pAdapter, false, false);
532 }
533
534 /*
535  * Functions:   efuse_ShadowRead1Byte
536  *              efuse_ShadowRead2Byte
537  *              efuse_ShadowRead4Byte
538  *
539  * Read from efuse init map by one/two/four bytes
540  */
541 static void
542 efuse_ShadowRead1Byte(struct rtw_adapter *pAdapter, u16 Offset, u8 *Value)
543 {
544         struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
545
546         *Value = pEEPROM->efuse_eeprom_data[Offset];
547 }
548
549 static void
550 efuse_ShadowRead2Byte(struct rtw_adapter *pAdapter, u16 Offset, u16 *Value)
551 {
552         struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
553
554         *Value = pEEPROM->efuse_eeprom_data[Offset];
555         *Value |= pEEPROM->efuse_eeprom_data[Offset+1]<<8;
556 }
557
558 static void
559 efuse_ShadowRead4Byte(struct rtw_adapter *pAdapter, u16 Offset, u32 *Value)
560 {
561         struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
562
563         *Value = pEEPROM->efuse_eeprom_data[Offset];
564         *Value |= pEEPROM->efuse_eeprom_data[Offset+1]<<8;
565         *Value |= pEEPROM->efuse_eeprom_data[Offset+2]<<16;
566         *Value |= pEEPROM->efuse_eeprom_data[Offset+3]<<24;
567 }
568
569 /* Transfer current EFUSE content to shadow init and modify map. */
570
571 void EFUSE_ShadowMapUpdate23a(struct rtw_adapter *pAdapter, u8 efuseType)
572 {
573         struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
574         u16     mapLen = 0;
575
576         EFUSE_GetEfuseDefinition23a(pAdapter, efuseType,
577                                  TYPE_EFUSE_MAP_LEN, (void *)&mapLen);
578
579         if (pEEPROM->bautoload_fail_flag == true)
580                 memset(pEEPROM->efuse_eeprom_data, 0xFF, mapLen);
581         else
582                 Efuse_ReadAllMap(pAdapter, efuseType,
583                                  pEEPROM->efuse_eeprom_data);
584 }
585
586 /* Read from efuse init map */
587 void
588 EFUSE_ShadowRead23a(struct rtw_adapter *pAdapter,
589                     u8 Type, u16 Offset, u32 *Value)
590 {
591         if (Type == 1)
592                 efuse_ShadowRead1Byte(pAdapter, Offset, (u8 *)Value);
593         else if (Type == 2)
594                 efuse_ShadowRead2Byte(pAdapter, Offset, (u16 *)Value);
595         else if (Type == 4)
596                 efuse_ShadowRead4Byte(pAdapter, Offset, (u32 *)Value);
597 }