1 /******************************************************************************
3 * Copyright(c) 2009-2012 Realtek Corporation.
5 * Tmis 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.
9 * Tmis 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
14 * Tme full GNU General Public License is included in this distribution in the
15 * file called LICENSE.
17 * Contact Information:
18 * wlanfae <wlanfae@realtek.com>
19 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
20 * Hsinchu 300, Taiwan.
22 * Larry Finger <Larry.Finger@lwfinger.net>
24 *****************************************************************************/
27 #include <linux/export.h>
29 static const u8 MAX_PGPKT_SIZE
= 9;
30 static const u8 PGPKT_DATA_SIZE
= 8;
31 static const int EFUSE_MAX_SIZE
= 512;
33 static const struct efuse_map RTL8712_SDIO_EFUSE_TABLE
[] = {
49 static void efuse_shadow_read_1byte(struct ieee80211_hw
*hw
, u16 offset
,
51 static void efuse_shadow_read_2byte(struct ieee80211_hw
*hw
, u16 offset
,
53 static void efuse_shadow_read_4byte(struct ieee80211_hw
*hw
, u16 offset
,
55 static void efuse_shadow_write_1byte(struct ieee80211_hw
*hw
, u16 offset
,
57 static void efuse_shadow_write_2byte(struct ieee80211_hw
*hw
, u16 offset
,
59 static void efuse_shadow_write_4byte(struct ieee80211_hw
*hw
, u16 offset
,
61 static int efuse_one_byte_write(struct ieee80211_hw
*hw
, u16 addr
,
63 static void efuse_read_all_map(struct ieee80211_hw
*hw
, u8
*efuse
);
64 static int efuse_pg_packet_read(struct ieee80211_hw
*hw
, u8 offset
,
66 static int efuse_pg_packet_write(struct ieee80211_hw
*hw
, u8 offset
,
67 u8 word_en
, u8
*data
);
68 static void efuse_word_enable_data_read(u8 word_en
, u8
*sourdata
,
70 static u8
enable_efuse_data_write(struct ieee80211_hw
*hw
,
71 u16 efuse_addr
, u8 word_en
, u8
*data
);
72 static void efuse_power_switch(struct ieee80211_hw
*hw
, u8 write
,
74 static u16
efuse_get_current_size(struct ieee80211_hw
*hw
);
75 static u8
efuse_calculate_word_cnts(u8 word_en
);
77 void efuse_initialize(struct ieee80211_hw
*hw
)
79 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
83 bytetemp
= rtl_read_byte(rtlpriv
, rtlpriv
->cfg
->maps
[SYS_FUNC_EN
] + 1);
84 temp
= bytetemp
| 0x20;
85 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[SYS_FUNC_EN
] + 1, temp
);
87 bytetemp
= rtl_read_byte(rtlpriv
, rtlpriv
->cfg
->maps
[SYS_ISO_CTRL
] + 1);
88 temp
= bytetemp
& 0xFE;
89 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[SYS_ISO_CTRL
] + 1, temp
);
91 bytetemp
= rtl_read_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_TEST
] + 3);
92 temp
= bytetemp
| 0x80;
93 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_TEST
] + 3, temp
);
95 rtl_write_byte(rtlpriv
, 0x2F8, 0x3);
97 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3, 0x72);
101 u8
efuse_read_1byte(struct ieee80211_hw
*hw
, u16 address
)
103 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
108 const u32 efuse_len
=
109 rtlpriv
->cfg
->maps
[EFUSE_REAL_CONTENT_SIZE
];
111 if (address
< efuse_len
) {
112 temp
= address
& 0xFF;
113 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 1,
115 bytetemp
= rtl_read_byte(rtlpriv
,
116 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 2);
117 temp
= ((address
>> 8) & 0x03) | (bytetemp
& 0xFC);
118 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 2,
121 bytetemp
= rtl_read_byte(rtlpriv
,
122 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3);
123 temp
= bytetemp
& 0x7F;
124 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3,
127 bytetemp
= rtl_read_byte(rtlpriv
,
128 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3);
129 while (!(bytetemp
& 0x80)) {
130 bytetemp
= rtl_read_byte(rtlpriv
,
132 maps
[EFUSE_CTRL
] + 3);
139 data
= rtl_read_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
]);
145 EXPORT_SYMBOL(efuse_read_1byte
);
147 void efuse_write_1byte(struct ieee80211_hw
*hw
, u16 address
, u8 value
)
149 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
153 const u32 efuse_len
=
154 rtlpriv
->cfg
->maps
[EFUSE_REAL_CONTENT_SIZE
];
156 RT_TRACE(rtlpriv
, COMP_EFUSE
, DBG_LOUD
, "Addr=%x Data =%x\n",
159 if (address
< efuse_len
) {
160 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
], value
);
162 temp
= address
& 0xFF;
163 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 1,
165 bytetemp
= rtl_read_byte(rtlpriv
,
166 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 2);
168 temp
= ((address
>> 8) & 0x03) | (bytetemp
& 0xFC);
169 rtl_write_byte(rtlpriv
,
170 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 2, temp
);
172 bytetemp
= rtl_read_byte(rtlpriv
,
173 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3);
174 temp
= bytetemp
| 0x80;
175 rtl_write_byte(rtlpriv
,
176 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3, temp
);
178 bytetemp
= rtl_read_byte(rtlpriv
,
179 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3);
181 while (bytetemp
& 0x80) {
182 bytetemp
= rtl_read_byte(rtlpriv
,
184 maps
[EFUSE_CTRL
] + 3);
195 void read_efuse_byte(struct ieee80211_hw
*hw
, u16 _offset
, u8
*pbuf
)
197 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
202 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 1,
204 readbyte
= rtl_read_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 2);
205 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 2,
206 ((_offset
>> 8) & 0x03) | (readbyte
& 0xfc));
208 readbyte
= rtl_read_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3);
209 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3,
213 value32
= rtl_read_dword(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
]);
214 while (!(((value32
>> 24) & 0xff) & 0x80) && (retry
< 10000)) {
215 value32
= rtl_read_dword(rtlpriv
,
216 rtlpriv
->cfg
->maps
[EFUSE_CTRL
]);
221 value32
= rtl_read_dword(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
]);
223 *pbuf
= (u8
) (value32
& 0xff);
225 EXPORT_SYMBOL_GPL(read_efuse_byte
);
227 void read_efuse(struct ieee80211_hw
*hw
, u16 _offset
, u16 _size_byte
, u8
*pbuf
)
229 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
230 struct rtl_efuse
*rtlefuse
= rtl_efuse(rtl_priv(hw
));
238 const u16 efuse_max_section
=
239 rtlpriv
->cfg
->maps
[EFUSE_MAX_SECTION_MAP
];
240 const u32 efuse_len
=
241 rtlpriv
->cfg
->maps
[EFUSE_REAL_CONTENT_SIZE
];
243 u16 efuse_utilized
= 0;
246 if ((_offset
+ _size_byte
) > rtlpriv
->cfg
->maps
[EFUSE_HWSET_MAX_SIZE
]) {
247 RT_TRACE(rtlpriv
, COMP_EFUSE
, DBG_LOUD
,
248 "read_efuse(): Invalid offset(%#x) with read bytes(%#x)!!\n",
249 _offset
, _size_byte
);
253 /* allocate memory for efuse_tbl and efuse_word */
254 efuse_tbl
= kzalloc(rtlpriv
->cfg
->maps
[EFUSE_HWSET_MAX_SIZE
] *
255 sizeof(u8
), GFP_ATOMIC
);
258 efuse_word
= kzalloc(EFUSE_MAX_WORD_UNIT
* sizeof(u16
*), GFP_ATOMIC
);
261 for (i
= 0; i
< EFUSE_MAX_WORD_UNIT
; i
++) {
262 efuse_word
[i
] = kzalloc(efuse_max_section
* sizeof(u16
),
268 for (i
= 0; i
< efuse_max_section
; i
++)
269 for (j
= 0; j
< EFUSE_MAX_WORD_UNIT
; j
++)
270 efuse_word
[j
][i
] = 0xFFFF;
272 read_efuse_byte(hw
, efuse_addr
, rtemp8
);
273 if (*rtemp8
!= 0xFF) {
275 RTPRINT(rtlpriv
, FEEPROM
, EFUSE_READ_ALL
,
276 "Addr=%d\n", efuse_addr
);
280 while ((*rtemp8
!= 0xFF) && (efuse_addr
< efuse_len
)) {
281 /* Check PG header for section num. */
282 if ((*rtemp8
& 0x1F) == 0x0F) {/* extended header */
283 u1temp
= ((*rtemp8
& 0xE0) >> 5);
284 read_efuse_byte(hw
, efuse_addr
, rtemp8
);
286 if ((*rtemp8
& 0x0F) == 0x0F) {
288 read_efuse_byte(hw
, efuse_addr
, rtemp8
);
290 if (*rtemp8
!= 0xFF &&
291 (efuse_addr
< efuse_len
)) {
296 offset
= ((*rtemp8
& 0xF0) >> 1) | u1temp
;
297 wren
= (*rtemp8
& 0x0F);
301 offset
= ((*rtemp8
>> 4) & 0x0f);
302 wren
= (*rtemp8
& 0x0f);
305 if (offset
< efuse_max_section
) {
306 RTPRINT(rtlpriv
, FEEPROM
, EFUSE_READ_ALL
,
307 "offset-%d Worden=%x\n", offset
, wren
);
309 for (i
= 0; i
< EFUSE_MAX_WORD_UNIT
; i
++) {
310 if (!(wren
& 0x01)) {
311 RTPRINT(rtlpriv
, FEEPROM
,
313 "Addr=%d\n", efuse_addr
);
315 read_efuse_byte(hw
, efuse_addr
, rtemp8
);
318 efuse_word
[i
][offset
] =
321 if (efuse_addr
>= efuse_len
)
324 RTPRINT(rtlpriv
, FEEPROM
,
326 "Addr=%d\n", efuse_addr
);
328 read_efuse_byte(hw
, efuse_addr
, rtemp8
);
331 efuse_word
[i
][offset
] |=
332 (((u16
)*rtemp8
<< 8) & 0xff00);
334 if (efuse_addr
>= efuse_len
)
342 RTPRINT(rtlpriv
, FEEPROM
, EFUSE_READ_ALL
,
343 "Addr=%d\n", efuse_addr
);
344 read_efuse_byte(hw
, efuse_addr
, rtemp8
);
345 if (*rtemp8
!= 0xFF && (efuse_addr
< efuse_len
)) {
351 for (i
= 0; i
< efuse_max_section
; i
++) {
352 for (j
= 0; j
< EFUSE_MAX_WORD_UNIT
; j
++) {
353 efuse_tbl
[(i
* 8) + (j
* 2)] =
354 (efuse_word
[j
][i
] & 0xff);
355 efuse_tbl
[(i
* 8) + ((j
* 2) + 1)] =
356 ((efuse_word
[j
][i
] >> 8) & 0xff);
360 for (i
= 0; i
< _size_byte
; i
++)
361 pbuf
[i
] = efuse_tbl
[_offset
+ i
];
363 rtlefuse
->efuse_usedbytes
= efuse_utilized
;
364 efuse_usage
= (u8
) ((efuse_utilized
* 100) / efuse_len
);
365 rtlefuse
->efuse_usedpercentage
= efuse_usage
;
366 rtlpriv
->cfg
->ops
->set_hw_reg(hw
, HW_VAR_EFUSE_BYTES
,
367 (u8
*)&efuse_utilized
);
368 rtlpriv
->cfg
->ops
->set_hw_reg(hw
, HW_VAR_EFUSE_USAGE
,
371 for (i
= 0; i
< EFUSE_MAX_WORD_UNIT
; i
++)
372 kfree(efuse_word
[i
]);
378 bool efuse_shadow_update_chk(struct ieee80211_hw
*hw
)
380 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
381 struct rtl_efuse
*rtlefuse
= rtl_efuse(rtl_priv(hw
));
382 u8 section_idx
, i
, Base
;
383 u16 words_need
= 0, hdr_num
= 0, totalbytes
, efuse_used
;
384 bool wordchanged
, result
= true;
386 for (section_idx
= 0; section_idx
< 16; section_idx
++) {
387 Base
= section_idx
* 8;
390 for (i
= 0; i
< 8; i
= i
+ 2) {
391 if ((rtlefuse
->efuse_map
[EFUSE_INIT_MAP
][Base
+ i
] !=
392 rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][Base
+ i
]) ||
393 (rtlefuse
->efuse_map
[EFUSE_INIT_MAP
][Base
+ i
+ 1] !=
394 rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][Base
+ i
+
405 totalbytes
= hdr_num
+ words_need
* 2;
406 efuse_used
= rtlefuse
->efuse_usedbytes
;
408 if ((totalbytes
+ efuse_used
) >=
409 (EFUSE_MAX_SIZE
- rtlpriv
->cfg
->maps
[EFUSE_OOB_PROTECT_BYTES_LEN
]))
412 RT_TRACE(rtlpriv
, COMP_EFUSE
, DBG_LOUD
,
413 "efuse_shadow_update_chk(): totalbytes(%#x), hdr_num(%#x), words_need(%#x), efuse_used(%d)\n",
414 totalbytes
, hdr_num
, words_need
, efuse_used
);
419 void efuse_shadow_read(struct ieee80211_hw
*hw
, u8 type
,
420 u16 offset
, u32
*value
)
423 efuse_shadow_read_1byte(hw
, offset
, (u8
*)value
);
425 efuse_shadow_read_2byte(hw
, offset
, (u16
*)value
);
427 efuse_shadow_read_4byte(hw
, offset
, value
);
430 EXPORT_SYMBOL(efuse_shadow_read
);
432 void efuse_shadow_write(struct ieee80211_hw
*hw
, u8 type
, u16 offset
,
436 efuse_shadow_write_1byte(hw
, offset
, (u8
) value
);
438 efuse_shadow_write_2byte(hw
, offset
, (u16
) value
);
440 efuse_shadow_write_4byte(hw
, offset
, value
);
444 bool efuse_shadow_update(struct ieee80211_hw
*hw
)
446 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
447 struct rtl_efuse
*rtlefuse
= rtl_efuse(rtl_priv(hw
));
452 RT_TRACE(rtlpriv
, COMP_EFUSE
, DBG_LOUD
, "\n");
454 if (!efuse_shadow_update_chk(hw
)) {
455 efuse_read_all_map(hw
, &rtlefuse
->efuse_map
[EFUSE_INIT_MAP
][0]);
456 memcpy(&rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][0],
457 &rtlefuse
->efuse_map
[EFUSE_INIT_MAP
][0],
458 rtlpriv
->cfg
->maps
[EFUSE_HWSET_MAX_SIZE
]);
460 RT_TRACE(rtlpriv
, COMP_EFUSE
, DBG_LOUD
,
461 "efuse out of capacity!!\n");
464 efuse_power_switch(hw
, true, true);
466 for (offset
= 0; offset
< 16; offset
++) {
471 for (i
= 0; i
< 8; i
++) {
473 word_en
&= ~(BIT(i
/ 2));
475 rtlefuse
->efuse_map
[EFUSE_INIT_MAP
][base
+ i
] =
476 rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][base
+ i
];
479 if (rtlefuse
->efuse_map
[EFUSE_INIT_MAP
][base
+ i
] !=
480 rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][base
+ i
]) {
481 word_en
&= ~(BIT(i
/ 2));
483 rtlefuse
->efuse_map
[EFUSE_INIT_MAP
][base
+ i
] =
484 rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][base
+ i
];
489 if (word_en
!= 0x0F) {
492 &rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][base
],
494 RT_PRINT_DATA(rtlpriv
, COMP_INIT
, DBG_LOUD
,
495 "U-efuse\n", tmpdata
, 8);
497 if (!efuse_pg_packet_write(hw
, (u8
) offset
, word_en
,
499 RT_TRACE(rtlpriv
, COMP_ERR
, DBG_WARNING
,
500 "PG section(%#x) fail!!\n", offset
);
507 efuse_power_switch(hw
, true, false);
508 efuse_read_all_map(hw
, &rtlefuse
->efuse_map
[EFUSE_INIT_MAP
][0]);
510 memcpy(&rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][0],
511 &rtlefuse
->efuse_map
[EFUSE_INIT_MAP
][0],
512 rtlpriv
->cfg
->maps
[EFUSE_HWSET_MAX_SIZE
]);
514 RT_TRACE(rtlpriv
, COMP_EFUSE
, DBG_LOUD
, "\n");
518 void rtl_efuse_shadow_map_update(struct ieee80211_hw
*hw
)
520 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
521 struct rtl_efuse
*rtlefuse
= rtl_efuse(rtl_priv(hw
));
523 if (rtlefuse
->autoload_failflag
)
524 memset((&rtlefuse
->efuse_map
[EFUSE_INIT_MAP
][0]),
525 0xFF, rtlpriv
->cfg
->maps
[EFUSE_HWSET_MAX_SIZE
]);
527 efuse_read_all_map(hw
, &rtlefuse
->efuse_map
[EFUSE_INIT_MAP
][0]);
529 memcpy(&rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][0],
530 &rtlefuse
->efuse_map
[EFUSE_INIT_MAP
][0],
531 rtlpriv
->cfg
->maps
[EFUSE_HWSET_MAX_SIZE
]);
534 EXPORT_SYMBOL(rtl_efuse_shadow_map_update
);
536 void efuse_force_write_vendor_Id(struct ieee80211_hw
*hw
)
538 u8 tmpdata
[8] = { 0xFF, 0xFF, 0xEC, 0x10, 0xFF, 0xFF, 0xFF, 0xFF };
540 efuse_power_switch(hw
, true, true);
542 efuse_pg_packet_write(hw
, 1, 0xD, tmpdata
);
544 efuse_power_switch(hw
, true, false);
548 void efuse_re_pg_section(struct ieee80211_hw
*hw
, u8 section_idx
)
552 static void efuse_shadow_read_1byte(struct ieee80211_hw
*hw
,
553 u16 offset
, u8
*value
)
555 struct rtl_efuse
*rtlefuse
= rtl_efuse(rtl_priv(hw
));
556 *value
= rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
];
559 static void efuse_shadow_read_2byte(struct ieee80211_hw
*hw
,
560 u16 offset
, u16
*value
)
562 struct rtl_efuse
*rtlefuse
= rtl_efuse(rtl_priv(hw
));
564 *value
= rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
];
565 *value
|= rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
+ 1] << 8;
569 static void efuse_shadow_read_4byte(struct ieee80211_hw
*hw
,
570 u16 offset
, u32
*value
)
572 struct rtl_efuse
*rtlefuse
= rtl_efuse(rtl_priv(hw
));
574 *value
= rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
];
575 *value
|= rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
+ 1] << 8;
576 *value
|= rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
+ 2] << 16;
577 *value
|= rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
+ 3] << 24;
580 static void efuse_shadow_write_1byte(struct ieee80211_hw
*hw
,
581 u16 offset
, u8 value
)
583 struct rtl_efuse
*rtlefuse
= rtl_efuse(rtl_priv(hw
));
585 rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
] = value
;
588 static void efuse_shadow_write_2byte(struct ieee80211_hw
*hw
,
589 u16 offset
, u16 value
)
591 struct rtl_efuse
*rtlefuse
= rtl_efuse(rtl_priv(hw
));
593 rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
] = value
& 0x00FF;
594 rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
+ 1] = value
>> 8;
598 static void efuse_shadow_write_4byte(struct ieee80211_hw
*hw
,
599 u16 offset
, u32 value
)
601 struct rtl_efuse
*rtlefuse
= rtl_efuse(rtl_priv(hw
));
603 rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
] =
604 (u8
) (value
& 0x000000FF);
605 rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
+ 1] =
606 (u8
) ((value
>> 8) & 0x0000FF);
607 rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
+ 2] =
608 (u8
) ((value
>> 16) & 0x00FF);
609 rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
+ 3] =
610 (u8
) ((value
>> 24) & 0xFF);
614 int efuse_one_byte_read(struct ieee80211_hw
*hw
, u16 addr
, u8
*data
)
616 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
620 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 1,
622 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 2,
623 ((u8
) ((addr
>> 8) & 0x03)) |
624 (rtl_read_byte(rtlpriv
,
625 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 2) &
628 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3, 0x72);
630 while (!(0x80 & rtl_read_byte(rtlpriv
,
631 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3))
637 *data
= rtl_read_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
]);
645 EXPORT_SYMBOL(efuse_one_byte_read
);
647 static int efuse_one_byte_write(struct ieee80211_hw
*hw
, u16 addr
, u8 data
)
649 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
652 RT_TRACE(rtlpriv
, COMP_EFUSE
, DBG_LOUD
,
653 "Addr = %x Data=%x\n", addr
, data
);
655 rtl_write_byte(rtlpriv
,
656 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 1, (u8
) (addr
& 0xff));
657 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 2,
658 (rtl_read_byte(rtlpriv
,
659 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] +
660 2) & 0xFC) | (u8
) ((addr
>> 8) & 0x03));
662 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
], data
);
663 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3, 0xF2);
665 while ((0x80 & rtl_read_byte(rtlpriv
,
666 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3))
676 static void efuse_read_all_map(struct ieee80211_hw
*hw
, u8
*efuse
)
678 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
679 efuse_power_switch(hw
, false, true);
680 read_efuse(hw
, 0, rtlpriv
->cfg
->maps
[EFUSE_HWSET_MAX_SIZE
], efuse
);
681 efuse_power_switch(hw
, false, false);
684 static void efuse_read_data_case1(struct ieee80211_hw
*hw
, u16
*efuse_addr
,
685 u8 efuse_data
, u8 offset
, u8
*tmpdata
,
688 bool dataempty
= true;
694 hoffset
= (efuse_data
>> 4) & 0x0F;
695 hworden
= efuse_data
& 0x0F;
696 word_cnts
= efuse_calculate_word_cnts(hworden
);
698 if (hoffset
== offset
) {
699 for (tmpidx
= 0; tmpidx
< word_cnts
* 2; tmpidx
++) {
700 if (efuse_one_byte_read(hw
, *efuse_addr
+ 1 + tmpidx
,
702 tmpdata
[tmpidx
] = efuse_data
;
703 if (efuse_data
!= 0xff)
709 *readstate
= PG_STATE_DATA
;
711 *efuse_addr
= *efuse_addr
+ (word_cnts
* 2) + 1;
712 *readstate
= PG_STATE_HEADER
;
716 *efuse_addr
= *efuse_addr
+ (word_cnts
* 2) + 1;
717 *readstate
= PG_STATE_HEADER
;
721 static int efuse_pg_packet_read(struct ieee80211_hw
*hw
, u8 offset
, u8
*data
)
723 u8 readstate
= PG_STATE_HEADER
;
725 bool continual
= true;
727 u8 efuse_data
, word_cnts
= 0;
736 memset(data
, 0xff, PGPKT_DATA_SIZE
* sizeof(u8
));
737 memset(tmpdata
, 0xff, PGPKT_DATA_SIZE
* sizeof(u8
));
739 while (continual
&& (efuse_addr
< EFUSE_MAX_SIZE
)) {
740 if (readstate
& PG_STATE_HEADER
) {
741 if (efuse_one_byte_read(hw
, efuse_addr
, &efuse_data
)
742 && (efuse_data
!= 0xFF))
743 efuse_read_data_case1(hw
, &efuse_addr
,
745 tmpdata
, &readstate
);
748 } else if (readstate
& PG_STATE_DATA
) {
749 efuse_word_enable_data_read(0, tmpdata
, data
);
750 efuse_addr
= efuse_addr
+ (word_cnts
* 2) + 1;
751 readstate
= PG_STATE_HEADER
;
756 if ((data
[0] == 0xff) && (data
[1] == 0xff) &&
757 (data
[2] == 0xff) && (data
[3] == 0xff) &&
758 (data
[4] == 0xff) && (data
[5] == 0xff) &&
759 (data
[6] == 0xff) && (data
[7] == 0xff))
766 static void efuse_write_data_case1(struct ieee80211_hw
*hw
, u16
*efuse_addr
,
767 u8 efuse_data
, u8 offset
,
768 int *continual
, u8
*write_state
,
769 struct pgpkt_struct
*target_pkt
,
770 int *repeat_times
, int *result
, u8 word_en
)
772 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
773 struct pgpkt_struct tmp_pkt
;
774 int dataempty
= true;
775 u8 originaldata
[8 * sizeof(u8
)];
777 u8 match_word_en
, tmp_word_en
;
779 u8 tmp_header
= efuse_data
;
782 tmp_pkt
.offset
= (tmp_header
>> 4) & 0x0F;
783 tmp_pkt
.word_en
= tmp_header
& 0x0F;
784 tmp_word_cnts
= efuse_calculate_word_cnts(tmp_pkt
.word_en
);
786 if (tmp_pkt
.offset
!= target_pkt
->offset
) {
787 *efuse_addr
= *efuse_addr
+ (tmp_word_cnts
* 2) + 1;
788 *write_state
= PG_STATE_HEADER
;
790 for (tmpindex
= 0; tmpindex
< (tmp_word_cnts
* 2); tmpindex
++) {
791 if (efuse_one_byte_read(hw
,
792 (*efuse_addr
+ 1 + tmpindex
),
794 (efuse_data
!= 0xFF))
799 *efuse_addr
= *efuse_addr
+ (tmp_word_cnts
* 2) + 1;
800 *write_state
= PG_STATE_HEADER
;
802 match_word_en
= 0x0F;
803 if (!((target_pkt
->word_en
& BIT(0)) |
804 (tmp_pkt
.word_en
& BIT(0))))
805 match_word_en
&= (~BIT(0));
807 if (!((target_pkt
->word_en
& BIT(1)) |
808 (tmp_pkt
.word_en
& BIT(1))))
809 match_word_en
&= (~BIT(1));
811 if (!((target_pkt
->word_en
& BIT(2)) |
812 (tmp_pkt
.word_en
& BIT(2))))
813 match_word_en
&= (~BIT(2));
815 if (!((target_pkt
->word_en
& BIT(3)) |
816 (tmp_pkt
.word_en
& BIT(3))))
817 match_word_en
&= (~BIT(3));
819 if ((match_word_en
& 0x0F) != 0x0F) {
821 enable_efuse_data_write(hw
,
826 if (0x0F != (badworden
& 0x0F)) {
827 u8 reorg_offset
= offset
;
828 u8 reorg_worden
= badworden
;
829 efuse_pg_packet_write(hw
, reorg_offset
,
835 if ((target_pkt
->word_en
& BIT(0)) ^
836 (match_word_en
& BIT(0)))
837 tmp_word_en
&= (~BIT(0));
839 if ((target_pkt
->word_en
& BIT(1)) ^
840 (match_word_en
& BIT(1)))
841 tmp_word_en
&= (~BIT(1));
843 if ((target_pkt
->word_en
& BIT(2)) ^
844 (match_word_en
& BIT(2)))
845 tmp_word_en
&= (~BIT(2));
847 if ((target_pkt
->word_en
& BIT(3)) ^
848 (match_word_en
& BIT(3)))
849 tmp_word_en
&= (~BIT(3));
851 if ((tmp_word_en
& 0x0F) != 0x0F) {
852 *efuse_addr
= efuse_get_current_size(hw
);
853 target_pkt
->offset
= offset
;
854 target_pkt
->word_en
= tmp_word_en
;
858 *write_state
= PG_STATE_HEADER
;
860 if (*repeat_times
> EFUSE_REPEAT_THRESHOLD_
) {
865 *efuse_addr
+= (2 * tmp_word_cnts
) + 1;
866 target_pkt
->offset
= offset
;
867 target_pkt
->word_en
= word_en
;
868 *write_state
= PG_STATE_HEADER
;
872 RTPRINT(rtlpriv
, FEEPROM
, EFUSE_PG
, "efuse PG_STATE_HEADER-1\n");
875 static void efuse_write_data_case2(struct ieee80211_hw
*hw
, u16
*efuse_addr
,
876 int *continual
, u8
*write_state
,
877 struct pgpkt_struct target_pkt
,
878 int *repeat_times
, int *result
)
880 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
881 struct pgpkt_struct tmp_pkt
;
884 u8 originaldata
[8 * sizeof(u8
)];
888 pg_header
= ((target_pkt
.offset
<< 4) & 0xf0) | target_pkt
.word_en
;
889 efuse_one_byte_write(hw
, *efuse_addr
, pg_header
);
890 efuse_one_byte_read(hw
, *efuse_addr
, &tmp_header
);
892 if (tmp_header
== pg_header
) {
893 *write_state
= PG_STATE_DATA
;
894 } else if (tmp_header
== 0xFF) {
895 *write_state
= PG_STATE_HEADER
;
897 if (*repeat_times
> EFUSE_REPEAT_THRESHOLD_
) {
902 tmp_pkt
.offset
= (tmp_header
>> 4) & 0x0F;
903 tmp_pkt
.word_en
= tmp_header
& 0x0F;
905 tmp_word_cnts
= efuse_calculate_word_cnts(tmp_pkt
.word_en
);
907 memset(originaldata
, 0xff, 8 * sizeof(u8
));
909 if (efuse_pg_packet_read(hw
, tmp_pkt
.offset
, originaldata
)) {
910 badworden
= enable_efuse_data_write(hw
,
915 if (0x0F != (badworden
& 0x0F)) {
916 u8 reorg_offset
= tmp_pkt
.offset
;
917 u8 reorg_worden
= badworden
;
918 efuse_pg_packet_write(hw
, reorg_offset
,
921 *efuse_addr
= efuse_get_current_size(hw
);
923 *efuse_addr
= *efuse_addr
+
924 (tmp_word_cnts
* 2) + 1;
927 *efuse_addr
= *efuse_addr
+ (tmp_word_cnts
* 2) + 1;
930 *write_state
= PG_STATE_HEADER
;
932 if (*repeat_times
> EFUSE_REPEAT_THRESHOLD_
) {
937 RTPRINT(rtlpriv
, FEEPROM
, EFUSE_PG
,
938 "efuse PG_STATE_HEADER-2\n");
942 static int efuse_pg_packet_write(struct ieee80211_hw
*hw
,
943 u8 offset
, u8 word_en
, u8
*data
)
945 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
946 struct pgpkt_struct target_pkt
;
947 u8 write_state
= PG_STATE_HEADER
;
948 int continual
= true, dataempty
= true, result
= true;
951 u8 target_word_cnts
= 0;
953 static int repeat_times
;
955 if (efuse_get_current_size(hw
) >= (EFUSE_MAX_SIZE
-
956 rtlpriv
->cfg
->maps
[EFUSE_OOB_PROTECT_BYTES_LEN
])) {
957 RTPRINT(rtlpriv
, FEEPROM
, EFUSE_PG
,
958 "efuse_pg_packet_write error\n");
962 target_pkt
.offset
= offset
;
963 target_pkt
.word_en
= word_en
;
965 memset(target_pkt
.data
, 0xFF, 8 * sizeof(u8
));
967 efuse_word_enable_data_read(word_en
, data
, target_pkt
.data
);
968 target_word_cnts
= efuse_calculate_word_cnts(target_pkt
.word_en
);
970 RTPRINT(rtlpriv
, FEEPROM
, EFUSE_PG
, "efuse Power ON\n");
972 while (continual
&& (efuse_addr
< (EFUSE_MAX_SIZE
-
973 rtlpriv
->cfg
->maps
[EFUSE_OOB_PROTECT_BYTES_LEN
]))) {
975 if (write_state
== PG_STATE_HEADER
) {
978 RTPRINT(rtlpriv
, FEEPROM
, EFUSE_PG
,
979 "efuse PG_STATE_HEADER\n");
981 if (efuse_one_byte_read(hw
, efuse_addr
, &efuse_data
) &&
982 (efuse_data
!= 0xFF))
983 efuse_write_data_case1(hw
, &efuse_addr
,
988 &repeat_times
, &result
,
991 efuse_write_data_case2(hw
, &efuse_addr
,
998 } else if (write_state
== PG_STATE_DATA
) {
999 RTPRINT(rtlpriv
, FEEPROM
, EFUSE_PG
,
1000 "efuse PG_STATE_DATA\n");
1003 enable_efuse_data_write(hw
, efuse_addr
+ 1,
1007 if ((badworden
& 0x0F) == 0x0F) {
1011 efuse_addr
+ (2 * target_word_cnts
) + 1;
1013 target_pkt
.offset
= offset
;
1014 target_pkt
.word_en
= badworden
;
1016 efuse_calculate_word_cnts(target_pkt
.
1018 write_state
= PG_STATE_HEADER
;
1020 if (repeat_times
> EFUSE_REPEAT_THRESHOLD_
) {
1024 RTPRINT(rtlpriv
, FEEPROM
, EFUSE_PG
,
1025 "efuse PG_STATE_HEADER-3\n");
1030 if (efuse_addr
>= (EFUSE_MAX_SIZE
-
1031 rtlpriv
->cfg
->maps
[EFUSE_OOB_PROTECT_BYTES_LEN
])) {
1032 RT_TRACE(rtlpriv
, COMP_EFUSE
, DBG_LOUD
,
1033 "efuse_addr(%#x) Out of size!!\n", efuse_addr
);
1039 static void efuse_word_enable_data_read(u8 word_en
, u8
*sourdata
,
1042 if (!(word_en
& BIT(0))) {
1043 targetdata
[0] = sourdata
[0];
1044 targetdata
[1] = sourdata
[1];
1047 if (!(word_en
& BIT(1))) {
1048 targetdata
[2] = sourdata
[2];
1049 targetdata
[3] = sourdata
[3];
1052 if (!(word_en
& BIT(2))) {
1053 targetdata
[4] = sourdata
[4];
1054 targetdata
[5] = sourdata
[5];
1057 if (!(word_en
& BIT(3))) {
1058 targetdata
[6] = sourdata
[6];
1059 targetdata
[7] = sourdata
[7];
1063 static u8
enable_efuse_data_write(struct ieee80211_hw
*hw
,
1064 u16 efuse_addr
, u8 word_en
, u8
*data
)
1066 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
1068 u16 start_addr
= efuse_addr
;
1069 u8 badworden
= 0x0F;
1072 memset(tmpdata
, 0xff, PGPKT_DATA_SIZE
);
1073 RT_TRACE(rtlpriv
, COMP_EFUSE
, DBG_LOUD
,
1074 "word_en = %x efuse_addr=%x\n", word_en
, efuse_addr
);
1076 if (!(word_en
& BIT(0))) {
1077 tmpaddr
= start_addr
;
1078 efuse_one_byte_write(hw
, start_addr
++, data
[0]);
1079 efuse_one_byte_write(hw
, start_addr
++, data
[1]);
1081 efuse_one_byte_read(hw
, tmpaddr
, &tmpdata
[0]);
1082 efuse_one_byte_read(hw
, tmpaddr
+ 1, &tmpdata
[1]);
1083 if ((data
[0] != tmpdata
[0]) || (data
[1] != tmpdata
[1]))
1084 badworden
&= (~BIT(0));
1087 if (!(word_en
& BIT(1))) {
1088 tmpaddr
= start_addr
;
1089 efuse_one_byte_write(hw
, start_addr
++, data
[2]);
1090 efuse_one_byte_write(hw
, start_addr
++, data
[3]);
1092 efuse_one_byte_read(hw
, tmpaddr
, &tmpdata
[2]);
1093 efuse_one_byte_read(hw
, tmpaddr
+ 1, &tmpdata
[3]);
1094 if ((data
[2] != tmpdata
[2]) || (data
[3] != tmpdata
[3]))
1095 badworden
&= (~BIT(1));
1098 if (!(word_en
& BIT(2))) {
1099 tmpaddr
= start_addr
;
1100 efuse_one_byte_write(hw
, start_addr
++, data
[4]);
1101 efuse_one_byte_write(hw
, start_addr
++, data
[5]);
1103 efuse_one_byte_read(hw
, tmpaddr
, &tmpdata
[4]);
1104 efuse_one_byte_read(hw
, tmpaddr
+ 1, &tmpdata
[5]);
1105 if ((data
[4] != tmpdata
[4]) || (data
[5] != tmpdata
[5]))
1106 badworden
&= (~BIT(2));
1109 if (!(word_en
& BIT(3))) {
1110 tmpaddr
= start_addr
;
1111 efuse_one_byte_write(hw
, start_addr
++, data
[6]);
1112 efuse_one_byte_write(hw
, start_addr
++, data
[7]);
1114 efuse_one_byte_read(hw
, tmpaddr
, &tmpdata
[6]);
1115 efuse_one_byte_read(hw
, tmpaddr
+ 1, &tmpdata
[7]);
1116 if ((data
[6] != tmpdata
[6]) || (data
[7] != tmpdata
[7]))
1117 badworden
&= (~BIT(3));
1123 static void efuse_power_switch(struct ieee80211_hw
*hw
, u8 write
, u8 pwrstate
)
1125 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
1126 struct rtl_hal
*rtlhal
= rtl_hal(rtl_priv(hw
));
1130 if (pwrstate
&& (rtlhal
->hw_type
!= HARDWARE_TYPE_RTL8192SE
)) {
1132 if (rtlhal
->hw_type
!= HARDWARE_TYPE_RTL8192CE
&&
1133 rtlhal
->hw_type
!= HARDWARE_TYPE_RTL8192DE
) {
1134 rtl_write_byte(rtlpriv
,
1135 rtlpriv
->cfg
->maps
[EFUSE_ACCESS
], 0x69);
1138 rtl_read_word(rtlpriv
,
1139 rtlpriv
->cfg
->maps
[SYS_ISO_CTRL
]);
1140 if (!(tmpV16
& rtlpriv
->cfg
->maps
[EFUSE_PWC_EV12V
])) {
1141 tmpV16
|= rtlpriv
->cfg
->maps
[EFUSE_PWC_EV12V
];
1142 rtl_write_word(rtlpriv
,
1143 rtlpriv
->cfg
->maps
[SYS_ISO_CTRL
],
1147 tmpV16
= rtl_read_word(rtlpriv
,
1148 rtlpriv
->cfg
->maps
[SYS_FUNC_EN
]);
1149 if (!(tmpV16
& rtlpriv
->cfg
->maps
[EFUSE_FEN_ELDR
])) {
1150 tmpV16
|= rtlpriv
->cfg
->maps
[EFUSE_FEN_ELDR
];
1151 rtl_write_word(rtlpriv
,
1152 rtlpriv
->cfg
->maps
[SYS_FUNC_EN
], tmpV16
);
1155 tmpV16
= rtl_read_word(rtlpriv
, rtlpriv
->cfg
->maps
[SYS_CLK
]);
1156 if ((!(tmpV16
& rtlpriv
->cfg
->maps
[EFUSE_LOADER_CLK_EN
])) ||
1157 (!(tmpV16
& rtlpriv
->cfg
->maps
[EFUSE_ANA8M
]))) {
1158 tmpV16
|= (rtlpriv
->cfg
->maps
[EFUSE_LOADER_CLK_EN
] |
1159 rtlpriv
->cfg
->maps
[EFUSE_ANA8M
]);
1160 rtl_write_word(rtlpriv
,
1161 rtlpriv
->cfg
->maps
[SYS_CLK
], tmpV16
);
1167 tempval
= rtl_read_byte(rtlpriv
,
1168 rtlpriv
->cfg
->maps
[EFUSE_TEST
] +
1171 if (rtlhal
->hw_type
== HARDWARE_TYPE_RTL8812AE
) {
1172 tempval
&= ~(BIT(3) | BIT(4) | BIT(5) | BIT(6));
1173 tempval
|= (VOLTAGE_V25
<< 3);
1174 } else if (rtlhal
->hw_type
!= HARDWARE_TYPE_RTL8192SE
) {
1176 tempval
|= (VOLTAGE_V25
<< 4);
1179 rtl_write_byte(rtlpriv
,
1180 rtlpriv
->cfg
->maps
[EFUSE_TEST
] + 3,
1184 if (rtlhal
->hw_type
== HARDWARE_TYPE_RTL8192SE
) {
1185 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CLK
],
1189 if (rtlhal
->hw_type
!= HARDWARE_TYPE_RTL8192CE
&&
1190 rtlhal
->hw_type
!= HARDWARE_TYPE_RTL8192DE
)
1191 rtl_write_byte(rtlpriv
,
1192 rtlpriv
->cfg
->maps
[EFUSE_ACCESS
], 0);
1195 tempval
= rtl_read_byte(rtlpriv
,
1196 rtlpriv
->cfg
->maps
[EFUSE_TEST
] +
1198 rtl_write_byte(rtlpriv
,
1199 rtlpriv
->cfg
->maps
[EFUSE_TEST
] + 3,
1203 if (rtlhal
->hw_type
== HARDWARE_TYPE_RTL8192SE
) {
1204 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CLK
],
1210 static u16
efuse_get_current_size(struct ieee80211_hw
*hw
)
1212 int continual
= true;
1214 u8 hoffset
, hworden
;
1215 u8 efuse_data
, word_cnts
;
1217 while (continual
&& efuse_one_byte_read(hw
, efuse_addr
, &efuse_data
) &&
1218 (efuse_addr
< EFUSE_MAX_SIZE
)) {
1219 if (efuse_data
!= 0xFF) {
1220 hoffset
= (efuse_data
>> 4) & 0x0F;
1221 hworden
= efuse_data
& 0x0F;
1222 word_cnts
= efuse_calculate_word_cnts(hworden
);
1223 efuse_addr
= efuse_addr
+ (word_cnts
* 2) + 1;
1232 static u8
efuse_calculate_word_cnts(u8 word_en
)
1235 if (!(word_en
& BIT(0)))
1237 if (!(word_en
& BIT(1)))
1239 if (!(word_en
& BIT(2)))
1241 if (!(word_en
& BIT(3)))