root/drivers/staging/rtl8188eu/core/rtw_efuse.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. efuse_power_switch
  2. efuse_phymap_to_logical
  3. efuse_read_phymap_from_txpktbuf
  4. iol_read_efuse
  5. efuse_ReadEFuse
  6. Efuse_WordEnableDataWrite
  7. Efuse_GetCurrentSize
  8. Efuse_PgPacketRead
  9. hal_EfuseFixHeaderProcess
  10. hal_EfusePgPacketWrite2ByteHeader
  11. hal_EfusePgPacketWrite1ByteHeader
  12. hal_EfusePgPacketWriteData
  13. hal_EfusePgPacketWriteHeader
  14. wordEnMatched
  15. hal_EfuseCheckIfDatafollowed
  16. hal_EfusePartialWriteCheck
  17. hal_EfuseConstructPGPkt
  18. Efuse_PgPacketWrite
  19. Efuse_CalculateWordCnts
  20. efuse_OneByteRead
  21. efuse_OneByteWrite
  22. efuse_WordEnableDataRead
  23. Efuse_ReadAllMap
  24. EFUSE_ShadowMapUpdate

   1 // SPDX-License-Identifier: GPL-2.0
   2 /******************************************************************************
   3  *
   4  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
   5  *
   6  ******************************************************************************/
   7 #define _RTW_EFUSE_C_
   8 
   9 #include <osdep_service.h>
  10 #include <drv_types.h>
  11 #include <rtw_efuse.h>
  12 #include <usb_ops_linux.h>
  13 #include <rtl8188e_hal.h>
  14 #include <rtw_iol.h>
  15 
  16 #define REG_EFUSE_CTRL          0x0030
  17 #define EFUSE_CTRL                      REG_EFUSE_CTRL          /*  E-Fuse Control. */
  18 
  19 enum{
  20                 VOLTAGE_V25                                             = 0x03,
  21                 LDOE25_SHIFT                                            = 28,
  22         };
  23 
  24 /*
  25  * When we want to enable write operation, we should change to pwr on state.
  26  * When we stop write, we should switch to 500k mode and disable LDO 2.5V.
  27  */
  28 void efuse_power_switch(struct adapter *pAdapter, u8 write, u8 pwrstate)
  29 {
  30         u8 tempval;
  31         u16 tmpv16;
  32 
  33         if (pwrstate) {
  34                 usb_write8(pAdapter, REG_EFUSE_ACCESS, EFUSE_ACCESS_ON);
  35 
  36                 /*  1.2V Power: From VDDON with Power Cut(0x0000h[15]), default valid */
  37                 tmpv16 = usb_read16(pAdapter, REG_SYS_ISO_CTRL);
  38                 if (!(tmpv16 & PWC_EV12V)) {
  39                         tmpv16 |= PWC_EV12V;
  40                         usb_write16(pAdapter, REG_SYS_ISO_CTRL, tmpv16);
  41                 }
  42                 /*  Reset: 0x0000h[28], default valid */
  43                 tmpv16 =  usb_read16(pAdapter, REG_SYS_FUNC_EN);
  44                 if (!(tmpv16 & FEN_ELDR)) {
  45                         tmpv16 |= FEN_ELDR;
  46                         usb_write16(pAdapter, REG_SYS_FUNC_EN, tmpv16);
  47                 }
  48 
  49                 /*  Clock: Gated(0x0008h[5]) 8M(0x0008h[1]) clock from ANA, default valid */
  50                 tmpv16 = usb_read16(pAdapter, REG_SYS_CLKR);
  51                 if ((!(tmpv16 & LOADER_CLK_EN))  || (!(tmpv16 & ANA8M))) {
  52                         tmpv16 |= (LOADER_CLK_EN | ANA8M);
  53                         usb_write16(pAdapter, REG_SYS_CLKR, tmpv16);
  54                 }
  55 
  56                 if (write) {
  57                         /*  Enable LDO 2.5V before read/write action */
  58                         tempval = usb_read8(pAdapter, EFUSE_TEST + 3);
  59                         tempval &= 0x0F;
  60                         tempval |= (VOLTAGE_V25 << 4);
  61                         usb_write8(pAdapter, EFUSE_TEST + 3, (tempval | 0x80));
  62                 }
  63         } else {
  64                 usb_write8(pAdapter, REG_EFUSE_ACCESS, EFUSE_ACCESS_OFF);
  65 
  66                 if (write) {
  67                         /*  Disable LDO 2.5V after read/write action */
  68                         tempval = usb_read8(pAdapter, EFUSE_TEST + 3);
  69                         usb_write8(pAdapter, EFUSE_TEST + 3, (tempval & 0x7F));
  70                 }
  71         }
  72 }
  73 
  74 static void
  75 efuse_phymap_to_logical(u8 *phymap, u16 _offset, u16 _size_byte, u8  *pbuf)
  76 {
  77         u8 *efuseTbl = NULL;
  78         u8 rtemp8;
  79         u16     eFuse_Addr = 0;
  80         u8 offset, wren;
  81         u16     i, j;
  82         u16     **eFuseWord = NULL;
  83         u16     efuse_utilized = 0;
  84         u8 u1temp = 0;
  85         void **tmp = NULL;
  86 
  87         efuseTbl = kzalloc(EFUSE_MAP_LEN_88E, GFP_KERNEL);
  88         if (!efuseTbl)
  89                 return;
  90 
  91         tmp = kcalloc(EFUSE_MAX_SECTION_88E,
  92                       sizeof(void *) + EFUSE_MAX_WORD_UNIT * sizeof(u16),
  93                       GFP_KERNEL);
  94         if (!tmp) {
  95                 DBG_88E("%s: alloc eFuseWord fail!\n", __func__);
  96                 goto eFuseWord_failed;
  97         }
  98         for (i = 0; i < EFUSE_MAX_SECTION_88E; i++)
  99                 tmp[i] = ((char *)(tmp + EFUSE_MAX_SECTION_88E)) + i * EFUSE_MAX_WORD_UNIT * sizeof(u16);
 100         eFuseWord = (u16 **)tmp;
 101 
 102         /*  0. Refresh efuse init map as all oxFF. */
 103         for (i = 0; i < EFUSE_MAX_SECTION_88E; i++)
 104                 for (j = 0; j < EFUSE_MAX_WORD_UNIT; j++)
 105                         eFuseWord[i][j] = 0xFFFF;
 106 
 107         /*  */
 108         /*  1. Read the first byte to check if efuse is empty!!! */
 109         /*  */
 110         /*  */
 111         rtemp8 = *(phymap + eFuse_Addr);
 112         if (rtemp8 != 0xFF) {
 113                 efuse_utilized++;
 114                 eFuse_Addr++;
 115         } else {
 116                 DBG_88E("EFUSE is empty efuse_Addr-%d efuse_data =%x\n", eFuse_Addr, rtemp8);
 117                 goto exit;
 118         }
 119 
 120         /*  */
 121         /*  2. Read real efuse content. Filter PG header and every section data. */
 122         /*  */
 123         while ((rtemp8 != 0xFF) && (eFuse_Addr < EFUSE_REAL_CONTENT_LEN_88E)) {
 124                 /*  Check PG header for section num. */
 125                 if ((rtemp8 & 0x1F) == 0x0F) {          /* extended header */
 126                         u1temp = (rtemp8 & 0xE0) >> 5;
 127                         rtemp8 = *(phymap + eFuse_Addr);
 128                         if ((rtemp8 & 0x0F) == 0x0F) {
 129                                 eFuse_Addr++;
 130                                 rtemp8 = *(phymap + eFuse_Addr);
 131 
 132                                 if (rtemp8 != 0xFF && (eFuse_Addr < EFUSE_REAL_CONTENT_LEN_88E))
 133                                         eFuse_Addr++;
 134                                 continue;
 135                         } else {
 136                                 offset = ((rtemp8 & 0xF0) >> 1) | u1temp;
 137                                 wren = rtemp8 & 0x0F;
 138                                 eFuse_Addr++;
 139                         }
 140                 } else {
 141                         offset = (rtemp8 >> 4) & 0x0f;
 142                         wren = rtemp8 & 0x0f;
 143                 }
 144 
 145                 if (offset < EFUSE_MAX_SECTION_88E) {
 146                         /*  Get word enable value from PG header */
 147                         for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) {
 148                                 /*  Check word enable condition in the section */
 149                                 if (!(wren & 0x01)) {
 150                                         rtemp8 = *(phymap + eFuse_Addr);
 151                                         eFuse_Addr++;
 152                                         efuse_utilized++;
 153                                         eFuseWord[offset][i] = (rtemp8 & 0xff);
 154                                         if (eFuse_Addr >= EFUSE_REAL_CONTENT_LEN_88E)
 155                                                 break;
 156                                         rtemp8 = *(phymap + eFuse_Addr);
 157                                         eFuse_Addr++;
 158                                         efuse_utilized++;
 159                                         eFuseWord[offset][i] |= (((u16)rtemp8 << 8) & 0xff00);
 160 
 161                                         if (eFuse_Addr >= EFUSE_REAL_CONTENT_LEN_88E)
 162                                                 break;
 163                                 }
 164                                 wren >>= 1;
 165                         }
 166                 }
 167                 /*  Read next PG header */
 168                 rtemp8 = *(phymap + eFuse_Addr);
 169 
 170                 if (rtemp8 != 0xFF && (eFuse_Addr < EFUSE_REAL_CONTENT_LEN_88E)) {
 171                         efuse_utilized++;
 172                         eFuse_Addr++;
 173                 }
 174         }
 175 
 176         /*  */
 177         /*  3. Collect 16 sections and 4 word unit into Efuse map. */
 178         /*  */
 179         for (i = 0; i < EFUSE_MAX_SECTION_88E; i++) {
 180                 for (j = 0; j < EFUSE_MAX_WORD_UNIT; j++) {
 181                         efuseTbl[(i * 8) + (j * 2)] = (eFuseWord[i][j] & 0xff);
 182                         efuseTbl[(i * 8) + ((j * 2) + 1)] = ((eFuseWord[i][j] >> 8) & 0xff);
 183                 }
 184         }
 185 
 186         /*  */
 187         /*  4. Copy from Efuse map to output pointer memory!!! */
 188         /*  */
 189         for (i = 0; i < _size_byte; i++)
 190                 pbuf[i] = efuseTbl[_offset + i];
 191 
 192         /*  */
 193         /*  5. Calculate Efuse utilization. */
 194         /*  */
 195 
 196 exit:
 197         kfree(eFuseWord);
 198 
 199 eFuseWord_failed:
 200         kfree(efuseTbl);
 201 }
 202 
 203 static void efuse_read_phymap_from_txpktbuf(
 204         struct adapter  *adapter,
 205         int bcnhead,    /* beacon head, where FW store len(2-byte) and efuse physical map. */
 206         u8 *content,    /* buffer to store efuse physical map */
 207         u16 *size       /* for efuse content: the max byte to read. will update to byte read */
 208         )
 209 {
 210         u16 dbg_addr = 0;
 211         unsigned long start = 0;
 212         u8 reg_0x143 = 0;
 213         u32 lo32 = 0, hi32 = 0;
 214         u16 len = 0, count = 0;
 215         int i = 0;
 216         u16 limit = *size;
 217 
 218         u8 *pos = content;
 219 
 220         if (bcnhead < 0) /* if not valid */
 221                 bcnhead = usb_read8(adapter, REG_TDECTRL + 1);
 222 
 223         DBG_88E("%s bcnhead:%d\n", __func__, bcnhead);
 224 
 225         usb_write8(adapter, REG_PKT_BUFF_ACCESS_CTRL, TXPKT_BUF_SELECT);
 226 
 227         dbg_addr = bcnhead * 128 / 8; /* 8-bytes addressing */
 228 
 229         while (1) {
 230                 usb_write16(adapter, REG_PKTBUF_DBG_ADDR, dbg_addr + i);
 231 
 232                 usb_write8(adapter, REG_TXPKTBUF_DBG, 0);
 233                 start = jiffies;
 234                 while (!(reg_0x143 = usb_read8(adapter, REG_TXPKTBUF_DBG)) &&
 235                        jiffies_to_msecs(jiffies - start) < 1000) {
 236                         DBG_88E("%s polling reg_0x143:0x%02x, reg_0x106:0x%02x\n", __func__, reg_0x143, usb_read8(adapter, 0x106));
 237                         usleep_range(1000, 2000);
 238                 }
 239 
 240                 lo32 = usb_read32(adapter, REG_PKTBUF_DBG_DATA_L);
 241                 hi32 = usb_read32(adapter, REG_PKTBUF_DBG_DATA_H);
 242 
 243                 if (i == 0) {
 244                         u8 lenc[2];
 245                         u16 lenbak, aaabak;
 246                         u16 aaa;
 247 
 248                         lenc[0] = usb_read8(adapter, REG_PKTBUF_DBG_DATA_L);
 249                         lenc[1] = usb_read8(adapter, REG_PKTBUF_DBG_DATA_L + 1);
 250 
 251                         aaabak = le16_to_cpup((__le16 *)lenc);
 252                         lenbak = le16_to_cpu(*((__le16 *)lenc));
 253                         aaa = le16_to_cpup((__le16 *)&lo32);
 254                         len = le16_to_cpu(*((__le16 *)&lo32));
 255 
 256                         limit = min_t(u16, len - 2, limit);
 257 
 258                         DBG_88E("%s len:%u, lenbak:%u, aaa:%u, aaabak:%u\n", __func__, len, lenbak, aaa, aaabak);
 259 
 260                         memcpy(pos, ((u8 *)&lo32) + 2, (limit >= count + 2) ? 2 : limit - count);
 261                         count += (limit >= count + 2) ? 2 : limit - count;
 262                         pos = content + count;
 263 
 264                 } else {
 265                         memcpy(pos, ((u8 *)&lo32), (limit >= count + 4) ? 4 : limit - count);
 266                         count += (limit >= count + 4) ? 4 : limit - count;
 267                         pos = content + count;
 268                 }
 269 
 270                 if (limit > count && len - 2 > count) {
 271                         memcpy(pos, (u8 *)&hi32, (limit >= count + 4) ? 4 : limit - count);
 272                         count += (limit >= count + 4) ? 4 : limit - count;
 273                         pos = content + count;
 274                 }
 275 
 276                 if (limit <= count || len - 2 <= count)
 277                         break;
 278                 i++;
 279         }
 280         usb_write8(adapter, REG_PKT_BUFF_ACCESS_CTRL, DISABLE_TRXPKT_BUF_ACCESS);
 281         DBG_88E("%s read count:%u\n", __func__, count);
 282         *size = count;
 283 }
 284 
 285 static s32 iol_read_efuse(struct adapter *padapter, u8 txpktbuf_bndy, u16 offset, u16 size_byte, u8 *logical_map)
 286 {
 287         s32 status = _FAIL;
 288         u8 physical_map[512];
 289         u16 size = 512;
 290 
 291         usb_write8(padapter, REG_TDECTRL + 1, txpktbuf_bndy);
 292         memset(physical_map, 0xFF, 512);
 293         usb_write8(padapter, REG_PKT_BUFF_ACCESS_CTRL, TXPKT_BUF_SELECT);
 294         status = iol_execute(padapter, CMD_READ_EFUSE_MAP);
 295         if (status == _SUCCESS)
 296                 efuse_read_phymap_from_txpktbuf(padapter, txpktbuf_bndy, physical_map, &size);
 297         efuse_phymap_to_logical(physical_map, offset, size_byte, logical_map);
 298         return status;
 299 }
 300 
 301 void efuse_ReadEFuse(struct adapter *Adapter, u8 efuseType, u16 _offset, u16 _size_byte, u8 *pbuf)
 302 {
 303         if (rtw_iol_applied(Adapter)) {
 304                 rtw_hal_power_on(Adapter);
 305                 iol_mode_enable(Adapter, 1);
 306                 iol_read_efuse(Adapter, 0, _offset, _size_byte, pbuf);
 307                 iol_mode_enable(Adapter, 0);
 308         }
 309 }
 310 
 311 u8 Efuse_WordEnableDataWrite(struct adapter *pAdapter, u16 efuse_addr, u8 word_en, u8 *data)
 312 {
 313         u16     tmpaddr = 0;
 314         u16     start_addr = efuse_addr;
 315         u8 badworden = 0x0F;
 316         u8 tmpdata[8];
 317 
 318         memset(tmpdata, 0xff, PGPKT_DATA_SIZE);
 319 
 320         if (!(word_en & BIT(0))) {
 321                 tmpaddr = start_addr;
 322                 efuse_OneByteWrite(pAdapter, start_addr++, data[0]);
 323                 efuse_OneByteWrite(pAdapter, start_addr++, data[1]);
 324 
 325                 efuse_OneByteRead(pAdapter, tmpaddr, &tmpdata[0]);
 326                 efuse_OneByteRead(pAdapter, tmpaddr + 1, &tmpdata[1]);
 327                 if ((data[0] != tmpdata[0]) || (data[1] != tmpdata[1]))
 328                         badworden &= (~BIT(0));
 329         }
 330         if (!(word_en & BIT(1))) {
 331                 tmpaddr = start_addr;
 332                 efuse_OneByteWrite(pAdapter, start_addr++, data[2]);
 333                 efuse_OneByteWrite(pAdapter, start_addr++, data[3]);
 334 
 335                 efuse_OneByteRead(pAdapter, tmpaddr, &tmpdata[2]);
 336                 efuse_OneByteRead(pAdapter, tmpaddr + 1, &tmpdata[3]);
 337                 if ((data[2] != tmpdata[2]) || (data[3] != tmpdata[3]))
 338                         badworden &= (~BIT(1));
 339         }
 340         if (!(word_en & BIT(2))) {
 341                 tmpaddr = start_addr;
 342                 efuse_OneByteWrite(pAdapter, start_addr++, data[4]);
 343                 efuse_OneByteWrite(pAdapter, start_addr++, data[5]);
 344 
 345                 efuse_OneByteRead(pAdapter, tmpaddr, &tmpdata[4]);
 346                 efuse_OneByteRead(pAdapter, tmpaddr + 1, &tmpdata[5]);
 347                 if ((data[4] != tmpdata[4]) || (data[5] != tmpdata[5]))
 348                         badworden &= (~BIT(2));
 349         }
 350         if (!(word_en & BIT(3))) {
 351                 tmpaddr = start_addr;
 352                 efuse_OneByteWrite(pAdapter, start_addr++, data[6]);
 353                 efuse_OneByteWrite(pAdapter, start_addr++, data[7]);
 354 
 355                 efuse_OneByteRead(pAdapter, tmpaddr, &tmpdata[6]);
 356                 efuse_OneByteRead(pAdapter, tmpaddr + 1, &tmpdata[7]);
 357                 if ((data[6] != tmpdata[6]) || (data[7] != tmpdata[7]))
 358                         badworden &= (~BIT(3));
 359         }
 360         return badworden;
 361 }
 362 
 363 static u16 Efuse_GetCurrentSize(struct adapter *pAdapter)
 364 {
 365         u16     efuse_addr = 0;
 366         u8 hoffset = 0, hworden = 0;
 367         u8 efuse_data, word_cnts = 0;
 368 
 369         rtw_hal_get_hwreg(pAdapter, HW_VAR_EFUSE_BYTES, (u8 *)&efuse_addr);
 370 
 371         while (efuse_OneByteRead(pAdapter, efuse_addr, &efuse_data) &&
 372                AVAILABLE_EFUSE_ADDR(efuse_addr)) {
 373                 if (efuse_data != 0xFF) {
 374                         if ((efuse_data & 0x1F) == 0x0F) {              /* extended header */
 375                                 hoffset = efuse_data;
 376                                 efuse_addr++;
 377                                 efuse_OneByteRead(pAdapter, efuse_addr, &efuse_data);
 378                                 if ((efuse_data & 0x0F) == 0x0F) {
 379                                         efuse_addr++;
 380                                         continue;
 381                                 } else {
 382                                         hoffset = ((hoffset & 0xE0) >> 5) | ((efuse_data & 0xF0) >> 1);
 383                                         hworden = efuse_data & 0x0F;
 384                                 }
 385                         } else {
 386                                 hoffset = (efuse_data >> 4) & 0x0F;
 387                                 hworden =  efuse_data & 0x0F;
 388                         }
 389                         word_cnts = Efuse_CalculateWordCnts(hworden);
 390                         /* read next header */
 391                         efuse_addr = efuse_addr + (word_cnts * 2) + 1;
 392                 } else {
 393                         break;
 394                 }
 395         }
 396 
 397         rtw_hal_set_hwreg(pAdapter, HW_VAR_EFUSE_BYTES, (u8 *)&efuse_addr);
 398 
 399         return efuse_addr;
 400 }
 401 
 402 int Efuse_PgPacketRead(struct adapter *pAdapter, u8 offset, u8 *data)
 403 {
 404         u8 ReadState = PG_STATE_HEADER;
 405         int     bContinual = true;
 406         int     bDataEmpty = true;
 407         u8 efuse_data, word_cnts = 0;
 408         u16     efuse_addr = 0;
 409         u8 hoffset = 0, hworden = 0;
 410         u8 tmpidx = 0;
 411         u8 tmpdata[8];
 412         u8 tmp_header = 0;
 413 
 414         if (!data)
 415                 return false;
 416         if (offset > EFUSE_MAX_SECTION_88E)
 417                 return false;
 418 
 419         memset(data, 0xff, sizeof(u8) * PGPKT_DATA_SIZE);
 420         memset(tmpdata, 0xff, sizeof(u8) * PGPKT_DATA_SIZE);
 421 
 422         /*  <Roger_TODO> Efuse has been pre-programmed dummy 5Bytes at the end of Efuse by CP. */
 423         /*  Skip dummy parts to prevent unexpected data read from Efuse. */
 424         /*  By pass right now. 2009.02.19. */
 425         while (bContinual && AVAILABLE_EFUSE_ADDR(efuse_addr)) {
 426                 /*   Header Read ------------- */
 427                 if (ReadState & PG_STATE_HEADER) {
 428                         if (efuse_OneByteRead(pAdapter, efuse_addr, &efuse_data) && (efuse_data != 0xFF)) {
 429                                 if (EXT_HEADER(efuse_data)) {
 430                                         tmp_header = efuse_data;
 431                                         efuse_addr++;
 432                                         efuse_OneByteRead(pAdapter, efuse_addr, &efuse_data);
 433                                         if (!ALL_WORDS_DISABLED(efuse_data)) {
 434                                                 hoffset = ((tmp_header & 0xE0) >> 5) | ((efuse_data & 0xF0) >> 1);
 435                                                 hworden = efuse_data & 0x0F;
 436                                         } else {
 437                                                 DBG_88E("Error, All words disabled\n");
 438                                                 efuse_addr++;
 439                                                 continue;
 440                                         }
 441                                 } else {
 442                                         hoffset = (efuse_data >> 4) & 0x0F;
 443                                         hworden =  efuse_data & 0x0F;
 444                                 }
 445                                 word_cnts = Efuse_CalculateWordCnts(hworden);
 446                                 bDataEmpty = true;
 447 
 448                                 if (hoffset == offset) {
 449                                         for (tmpidx = 0; tmpidx < word_cnts * 2; tmpidx++) {
 450                                                 if (efuse_OneByteRead(pAdapter, efuse_addr + 1 + tmpidx, &efuse_data)) {
 451                                                         tmpdata[tmpidx] = efuse_data;
 452                                                         if (efuse_data != 0xff)
 453                                                                 bDataEmpty = false;
 454                                                 }
 455                                         }
 456                                         if (!bDataEmpty) {
 457                                                 ReadState = PG_STATE_DATA;
 458                                         } else {/* read next header */
 459                                                 efuse_addr = efuse_addr + (word_cnts * 2) + 1;
 460                                                 ReadState = PG_STATE_HEADER;
 461                                         }
 462                                 } else {/* read next header */
 463                                         efuse_addr = efuse_addr + (word_cnts * 2) + 1;
 464                                         ReadState = PG_STATE_HEADER;
 465                                 }
 466                         } else {
 467                                 bContinual = false;
 468                         }
 469                 } else if (ReadState & PG_STATE_DATA) {
 470                         /*   Data section Read ------------- */
 471                         efuse_WordEnableDataRead(hworden, tmpdata, data);
 472                         efuse_addr = efuse_addr + (word_cnts * 2) + 1;
 473                         ReadState = PG_STATE_HEADER;
 474                 }
 475         }
 476 
 477         if ((data[0] == 0xff) && (data[1] == 0xff) && (data[2] == 0xff)  && (data[3] == 0xff) &&
 478             (data[4] == 0xff) && (data[5] == 0xff) && (data[6] == 0xff)  && (data[7] == 0xff))
 479                 return false;
 480         else
 481                 return true;
 482 }
 483 
 484 static bool hal_EfuseFixHeaderProcess(struct adapter *pAdapter, u8 efuseType, struct pgpkt *pFixPkt, u16 *pAddr)
 485 {
 486         u8 originaldata[8], badworden = 0;
 487         u16     efuse_addr = *pAddr;
 488         u32     PgWriteSuccess = 0;
 489 
 490         memset(originaldata, 0xff, 8);
 491 
 492         if (Efuse_PgPacketRead(pAdapter, pFixPkt->offset, originaldata)) {
 493                 /* check if data exist */
 494                 badworden = Efuse_WordEnableDataWrite(pAdapter, efuse_addr + 1, pFixPkt->word_en, originaldata);
 495 
 496                 if (badworden != 0xf) { /*  write fail */
 497                         PgWriteSuccess = Efuse_PgPacketWrite(pAdapter, pFixPkt->offset, badworden, originaldata);
 498 
 499                         if (!PgWriteSuccess)
 500                                 return false;
 501                         else
 502                                 efuse_addr = Efuse_GetCurrentSize(pAdapter);
 503                 } else {
 504                         efuse_addr = efuse_addr + (pFixPkt->word_cnts * 2) + 1;
 505                 }
 506         } else {
 507                 efuse_addr = efuse_addr + (pFixPkt->word_cnts * 2) + 1;
 508         }
 509         *pAddr = efuse_addr;
 510         return true;
 511 }
 512 
 513 static bool hal_EfusePgPacketWrite2ByteHeader(struct adapter *pAdapter, u8 efuseType, u16 *pAddr, struct pgpkt *pTargetPkt)
 514 {
 515         bool ret = false;
 516         u16 efuse_addr = *pAddr;
 517         u16 efuse_max_available_len =
 518                 EFUSE_REAL_CONTENT_LEN_88E - EFUSE_OOB_PROTECT_BYTES_88E;
 519         u8 pg_header = 0, tmp_header = 0, pg_header_temp = 0;
 520         u8 repeatcnt = 0;
 521 
 522         while (efuse_addr < efuse_max_available_len) {
 523                 pg_header = ((pTargetPkt->offset & 0x07) << 5) | 0x0F;
 524                 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
 525                 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
 526 
 527                 while (tmp_header == 0xFF) {
 528                         if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_)
 529                                 return false;
 530 
 531                         efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
 532                         efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
 533                 }
 534 
 535                 /* to write ext_header */
 536                 if (tmp_header == pg_header) {
 537                         efuse_addr++;
 538                         pg_header_temp = pg_header;
 539                         pg_header = ((pTargetPkt->offset & 0x78) << 1) | pTargetPkt->word_en;
 540 
 541                         efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
 542                         efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
 543 
 544                         while (tmp_header == 0xFF) {
 545                                 if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_)
 546                                         return false;
 547 
 548                                 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
 549                                 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
 550                         }
 551 
 552                         if ((tmp_header & 0x0F) == 0x0F) {      /* word_en PG fail */
 553                                 if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_)
 554                                         return false;
 555 
 556                                 efuse_addr++;
 557                                 continue;
 558                         } else if (pg_header != tmp_header) {   /* offset PG fail */
 559                                 struct pgpkt    fixPkt;
 560 
 561                                 fixPkt.offset = ((pg_header_temp & 0xE0) >> 5) | ((tmp_header & 0xF0) >> 1);
 562                                 fixPkt.word_en = tmp_header & 0x0F;
 563                                 fixPkt.word_cnts = Efuse_CalculateWordCnts(fixPkt.word_en);
 564                                 if (!hal_EfuseFixHeaderProcess(pAdapter, efuseType, &fixPkt, &efuse_addr))
 565                                         return false;
 566                         } else {
 567                                 ret = true;
 568                                 break;
 569                         }
 570                 } else if ((tmp_header & 0x1F) == 0x0F) {               /* wrong extended header */
 571                         efuse_addr += 2;
 572                         continue;
 573                 }
 574         }
 575 
 576         *pAddr = efuse_addr;
 577         return ret;
 578 }
 579 
 580 static bool hal_EfusePgPacketWrite1ByteHeader(struct adapter *pAdapter, u8 efuseType, u16 *pAddr, struct pgpkt *pTargetPkt)
 581 {
 582         bool ret = false;
 583         u8 pg_header = 0, tmp_header = 0;
 584         u16     efuse_addr = *pAddr;
 585         u8 repeatcnt = 0;
 586 
 587         pg_header = ((pTargetPkt->offset << 4) & 0xf0) | pTargetPkt->word_en;
 588 
 589         efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
 590         efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
 591 
 592         while (tmp_header == 0xFF) {
 593                 if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_)
 594                         return false;
 595                 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
 596                 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
 597         }
 598 
 599         if (pg_header == tmp_header) {
 600                 ret = true;
 601         } else {
 602                 struct pgpkt    fixPkt;
 603 
 604                 fixPkt.offset = (tmp_header >> 4) & 0x0F;
 605                 fixPkt.word_en = tmp_header & 0x0F;
 606                 fixPkt.word_cnts = Efuse_CalculateWordCnts(fixPkt.word_en);
 607                 if (!hal_EfuseFixHeaderProcess(pAdapter, efuseType, &fixPkt, &efuse_addr))
 608                         return false;
 609         }
 610 
 611         *pAddr = efuse_addr;
 612         return ret;
 613 }
 614 
 615 static bool hal_EfusePgPacketWriteData(struct adapter *pAdapter, u8 efuseType, u16 *pAddr, struct pgpkt *pTargetPkt)
 616 {
 617         u16     efuse_addr = *pAddr;
 618         u8 badworden = 0;
 619         u32     PgWriteSuccess = 0;
 620 
 621         badworden = 0x0f;
 622         badworden = Efuse_WordEnableDataWrite(pAdapter, efuse_addr + 1, pTargetPkt->word_en, pTargetPkt->data);
 623         if (badworden == 0x0F) {
 624                 /*  write ok */
 625                 return true;
 626         }
 627         /* reorganize other pg packet */
 628         PgWriteSuccess = Efuse_PgPacketWrite(pAdapter, pTargetPkt->offset, badworden, pTargetPkt->data);
 629         if (!PgWriteSuccess)
 630                 return false;
 631         else
 632                 return true;
 633 }
 634 
 635 static bool
 636 hal_EfusePgPacketWriteHeader(
 637                                 struct adapter *pAdapter,
 638                                 u8 efuseType,
 639                                 u16                             *pAddr,
 640                                 struct pgpkt *pTargetPkt)
 641 {
 642         bool ret = false;
 643 
 644         if (pTargetPkt->offset >= EFUSE_MAX_SECTION_BASE)
 645                 ret = hal_EfusePgPacketWrite2ByteHeader(pAdapter, efuseType, pAddr, pTargetPkt);
 646         else
 647                 ret = hal_EfusePgPacketWrite1ByteHeader(pAdapter, efuseType, pAddr, pTargetPkt);
 648 
 649         return ret;
 650 }
 651 
 652 static bool wordEnMatched(struct pgpkt *pTargetPkt, struct pgpkt *pCurPkt,
 653                           u8 *pWden)
 654 {
 655         u8 match_word_en = 0x0F;        /*  default all words are disabled */
 656 
 657         /*  check if the same words are enabled both target and current PG packet */
 658         if (((pTargetPkt->word_en & BIT(0)) == 0) &&
 659             ((pCurPkt->word_en & BIT(0)) == 0))
 660                 match_word_en &= ~BIT(0);                               /*  enable word 0 */
 661         if (((pTargetPkt->word_en & BIT(1)) == 0) &&
 662             ((pCurPkt->word_en & BIT(1)) == 0))
 663                 match_word_en &= ~BIT(1);                               /*  enable word 1 */
 664         if (((pTargetPkt->word_en & BIT(2)) == 0) &&
 665             ((pCurPkt->word_en & BIT(2)) == 0))
 666                 match_word_en &= ~BIT(2);                               /*  enable word 2 */
 667         if (((pTargetPkt->word_en & BIT(3)) == 0) &&
 668             ((pCurPkt->word_en & BIT(3)) == 0))
 669                 match_word_en &= ~BIT(3);                               /*  enable word 3 */
 670 
 671         *pWden = match_word_en;
 672 
 673         if (match_word_en != 0xf)
 674                 return true;
 675         else
 676                 return false;
 677 }
 678 
 679 static bool hal_EfuseCheckIfDatafollowed(struct adapter *pAdapter, u8 word_cnts, u16 startAddr)
 680 {
 681         bool ret = false;
 682         u8 i, efuse_data;
 683 
 684         for (i = 0; i < (word_cnts * 2); i++) {
 685                 if (efuse_OneByteRead(pAdapter, (startAddr + i), &efuse_data) && (efuse_data != 0xFF))
 686                         ret = true;
 687         }
 688         return ret;
 689 }
 690 
 691 static bool hal_EfusePartialWriteCheck(struct adapter *pAdapter, u8 efuseType, u16 *pAddr, struct pgpkt *pTargetPkt)
 692 {
 693         bool ret = false;
 694         u8 i, efuse_data = 0, cur_header = 0;
 695         u8 matched_wden = 0, badworden = 0;
 696         u16 startAddr = 0;
 697         u16 efuse_max_available_len =
 698                 EFUSE_REAL_CONTENT_LEN_88E - EFUSE_OOB_PROTECT_BYTES_88E;
 699         struct pgpkt curPkt;
 700 
 701         rtw_hal_get_hwreg(pAdapter, HW_VAR_EFUSE_BYTES, (u8 *)&startAddr);
 702         startAddr %= EFUSE_REAL_CONTENT_LEN;
 703 
 704         while (1) {
 705                 if (startAddr >= efuse_max_available_len) {
 706                         ret = false;
 707                         break;
 708                 }
 709 
 710                 if (efuse_OneByteRead(pAdapter, startAddr, &efuse_data) && (efuse_data != 0xFF)) {
 711                         if (EXT_HEADER(efuse_data)) {
 712                                 cur_header = efuse_data;
 713                                 startAddr++;
 714                                 efuse_OneByteRead(pAdapter, startAddr, &efuse_data);
 715                                 if (ALL_WORDS_DISABLED(efuse_data)) {
 716                                         ret = false;
 717                                         break;
 718                                 } else {
 719                                         curPkt.offset = ((cur_header & 0xE0) >> 5) | ((efuse_data & 0xF0) >> 1);
 720                                         curPkt.word_en = efuse_data & 0x0F;
 721                                 }
 722                         } else {
 723                                 cur_header  =  efuse_data;
 724                                 curPkt.offset = (cur_header >> 4) & 0x0F;
 725                                 curPkt.word_en = cur_header & 0x0F;
 726                         }
 727 
 728                         curPkt.word_cnts = Efuse_CalculateWordCnts(curPkt.word_en);
 729                         /*  if same header is found but no data followed */
 730                         /*  write some part of data followed by the header. */
 731                         if ((curPkt.offset == pTargetPkt->offset) &&
 732                             (!hal_EfuseCheckIfDatafollowed(pAdapter, curPkt.word_cnts, startAddr + 1)) &&
 733                             wordEnMatched(pTargetPkt, &curPkt, &matched_wden)) {
 734                                 /*  Here to write partial data */
 735                                 badworden = Efuse_WordEnableDataWrite(pAdapter, startAddr + 1, matched_wden, pTargetPkt->data);
 736                                 if (badworden != 0x0F) {
 737                                         u32     PgWriteSuccess = 0;
 738                                         /*  if write fail on some words, write these bad words again */
 739 
 740                                         PgWriteSuccess = Efuse_PgPacketWrite(pAdapter, pTargetPkt->offset, badworden, pTargetPkt->data);
 741 
 742                                         if (!PgWriteSuccess) {
 743                                                 ret = false;    /*  write fail, return */
 744                                                 break;
 745                                         }
 746                                 }
 747                                 /*  partial write ok, update the target packet for later use */
 748                                 for (i = 0; i < 4; i++) {
 749                                         if ((matched_wden & (0x1 << i)) == 0)   /*  this word has been written */
 750                                                 pTargetPkt->word_en |= (0x1 << i);      /*  disable the word */
 751                                 }
 752                                 pTargetPkt->word_cnts = Efuse_CalculateWordCnts(pTargetPkt->word_en);
 753                         }
 754                         /*  read from next header */
 755                         startAddr = startAddr + (curPkt.word_cnts * 2) + 1;
 756                 } else {
 757                         /*  not used header, 0xff */
 758                         *pAddr = startAddr;
 759                         ret = true;
 760                         break;
 761                 }
 762         }
 763         return ret;
 764 }
 765 
 766 static void hal_EfuseConstructPGPkt(u8 offset, u8 word_en, u8 *pData, struct pgpkt *pTargetPkt)
 767 {
 768         memset((void *)pTargetPkt->data, 0xFF, sizeof(u8) * 8);
 769         pTargetPkt->offset = offset;
 770         pTargetPkt->word_en = word_en;
 771         efuse_WordEnableDataRead(word_en, pData, pTargetPkt->data);
 772         pTargetPkt->word_cnts = Efuse_CalculateWordCnts(pTargetPkt->word_en);
 773 }
 774 
 775 bool Efuse_PgPacketWrite(struct adapter *pAdapter, u8 offset, u8 word_en, u8 *pData)
 776 {
 777         struct pgpkt    targetPkt;
 778         u16                     startAddr = 0;
 779         u8 efuseType = EFUSE_WIFI;
 780 
 781         if (Efuse_GetCurrentSize(pAdapter) >= EFUSE_MAP_LEN_88E)
 782                 return false;
 783 
 784         hal_EfuseConstructPGPkt(offset, word_en, pData, &targetPkt);
 785 
 786         if (!hal_EfusePartialWriteCheck(pAdapter, efuseType, &startAddr, &targetPkt))
 787                 return false;
 788 
 789         if (!hal_EfusePgPacketWriteHeader(pAdapter, efuseType, &startAddr, &targetPkt))
 790                 return false;
 791 
 792         if (!hal_EfusePgPacketWriteData(pAdapter, efuseType, &startAddr, &targetPkt))
 793                 return false;
 794 
 795         return true;
 796 }
 797 
 798 u8 Efuse_CalculateWordCnts(u8 word_en)
 799 {
 800         u8 word_cnts = 0;
 801 
 802         if (!(word_en & BIT(0)))
 803                 word_cnts++; /*  0 : write enable */
 804         if (!(word_en & BIT(1)))
 805                 word_cnts++;
 806         if (!(word_en & BIT(2)))
 807                 word_cnts++;
 808         if (!(word_en & BIT(3)))
 809                 word_cnts++;
 810         return word_cnts;
 811 }
 812 
 813 u8 efuse_OneByteRead(struct adapter *pAdapter, u16 addr, u8 *data)
 814 {
 815         u8 tmpidx = 0;
 816         u8 result;
 817 
 818         usb_write8(pAdapter, EFUSE_CTRL + 1, (u8)(addr & 0xff));
 819         usb_write8(pAdapter, EFUSE_CTRL + 2, ((u8)((addr >> 8) & 0x03)) |
 820                    (usb_read8(pAdapter, EFUSE_CTRL + 2) & 0xFC));
 821 
 822         usb_write8(pAdapter, EFUSE_CTRL + 3,  0x72);/* read cmd */
 823 
 824         while (!(0x80 & usb_read8(pAdapter, EFUSE_CTRL + 3)) && (tmpidx < 100))
 825                 tmpidx++;
 826         if (tmpidx < 100) {
 827                 *data = usb_read8(pAdapter, EFUSE_CTRL);
 828                 result = true;
 829         } else {
 830                 *data = 0xff;
 831                 result = false;
 832         }
 833         return result;
 834 }
 835 
 836 u8 efuse_OneByteWrite(struct adapter *pAdapter, u16 addr, u8 data)
 837 {
 838         u8 tmpidx = 0;
 839         u8 result;
 840 
 841         usb_write8(pAdapter, EFUSE_CTRL + 1, (u8)(addr & 0xff));
 842         usb_write8(pAdapter, EFUSE_CTRL + 2,
 843                    (usb_read8(pAdapter, EFUSE_CTRL + 2) & 0xFC) |
 844                    (u8)((addr >> 8) & 0x03));
 845         usb_write8(pAdapter, EFUSE_CTRL, data);/* data */
 846 
 847         usb_write8(pAdapter, EFUSE_CTRL + 3, 0xF2);/* write cmd */
 848 
 849         while ((0x80 &  usb_read8(pAdapter, EFUSE_CTRL + 3)) && (tmpidx < 100))
 850                 tmpidx++;
 851 
 852         if (tmpidx < 100)
 853                 result = true;
 854         else
 855                 result = false;
 856 
 857         return result;
 858 }
 859 
 860 /* Read allowed word in current efuse section data. */
 861 void efuse_WordEnableDataRead(u8 word_en, u8 *sourdata, u8 *targetdata)
 862 {
 863         if (!(word_en & BIT(0))) {
 864                 targetdata[0] = sourdata[0];
 865                 targetdata[1] = sourdata[1];
 866         }
 867         if (!(word_en & BIT(1))) {
 868                 targetdata[2] = sourdata[2];
 869                 targetdata[3] = sourdata[3];
 870         }
 871         if (!(word_en & BIT(2))) {
 872                 targetdata[4] = sourdata[4];
 873                 targetdata[5] = sourdata[5];
 874         }
 875         if (!(word_en & BIT(3))) {
 876                 targetdata[6] = sourdata[6];
 877                 targetdata[7] = sourdata[7];
 878         }
 879 }
 880 
 881 /* Read All Efuse content */
 882 static void Efuse_ReadAllMap(struct adapter *pAdapter, u8 efuseType, u8 *Efuse)
 883 {
 884         efuse_power_switch(pAdapter, false, true);
 885 
 886         efuse_ReadEFuse(pAdapter, efuseType, 0, EFUSE_MAP_LEN_88E, Efuse);
 887 
 888         efuse_power_switch(pAdapter, false, false);
 889 }
 890 
 891 /* Transfer current EFUSE content to shadow init and modify map. */
 892 void EFUSE_ShadowMapUpdate(struct adapter *pAdapter, u8 efuseType)
 893 {
 894         struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
 895 
 896         if (pEEPROM->bautoload_fail_flag)
 897                 memset(pEEPROM->efuse_eeprom_data, 0xFF, EFUSE_MAP_LEN_88E);
 898         else
 899                 Efuse_ReadAllMap(pAdapter, efuseType, pEEPROM->efuse_eeprom_data);
 900 }

/* [<][>][^][v][top][bottom][index][help] */