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 * You should have received a copy of the GNU General Public License along with
15 * tmis program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 * Tme full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
21 * Contact Information:
22 * wlanfae <wlanfae@realtek.com>
23 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
24 * Hsinchu 300, Taiwan.
26 * Larry Finger <Larry.Finger@lwfinger.net>
28 *****************************************************************************/
30 #include <linux/export.h>
34 static const u8 MAX_PGPKT_SIZE
= 9;
35 static const u8 PGPKT_DATA_SIZE
= 8;
36 static const int EFUSE_MAX_SIZE
= 512;
38 static const struct efuse_map RTL8712_SDIO_EFUSE_TABLE
[] = {
54 static void efuse_shadow_read_1byte(struct ieee80211_hw
*hw
, u16 offset
,
56 static void efuse_shadow_read_2byte(struct ieee80211_hw
*hw
, u16 offset
,
58 static void efuse_shadow_read_4byte(struct ieee80211_hw
*hw
, u16 offset
,
60 static void efuse_shadow_write_1byte(struct ieee80211_hw
*hw
, u16 offset
,
62 static void efuse_shadow_write_2byte(struct ieee80211_hw
*hw
, u16 offset
,
64 static void efuse_shadow_write_4byte(struct ieee80211_hw
*hw
, u16 offset
,
66 static int efuse_one_byte_read(struct ieee80211_hw
*hw
, u16 addr
,
68 static int efuse_one_byte_write(struct ieee80211_hw
*hw
, u16 addr
,
70 static void efuse_read_all_map(struct ieee80211_hw
*hw
, u8
*efuse
);
71 static int efuse_pg_packet_read(struct ieee80211_hw
*hw
, u8 offset
,
73 static int efuse_pg_packet_write(struct ieee80211_hw
*hw
, u8 offset
,
74 u8 word_en
, u8
*data
);
75 static void efuse_word_enable_data_read(u8 word_en
, u8
*sourdata
,
77 static u8
efuse_word_enable_data_write(struct ieee80211_hw
*hw
,
78 u16 efuse_addr
, u8 word_en
, u8
*data
);
79 static void efuse_power_switch(struct ieee80211_hw
*hw
, u8 write
,
81 static u16
efuse_get_current_size(struct ieee80211_hw
*hw
);
82 static u8
efuse_calculate_word_cnts(u8 word_en
);
84 void efuse_initialize(struct ieee80211_hw
*hw
)
86 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
90 bytetemp
= rtl_read_byte(rtlpriv
, rtlpriv
->cfg
->maps
[SYS_FUNC_EN
] + 1);
91 temp
= bytetemp
| 0x20;
92 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[SYS_FUNC_EN
] + 1, temp
);
94 bytetemp
= rtl_read_byte(rtlpriv
, rtlpriv
->cfg
->maps
[SYS_ISO_CTRL
] + 1);
95 temp
= bytetemp
& 0xFE;
96 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[SYS_ISO_CTRL
] + 1, temp
);
98 bytetemp
= rtl_read_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_TEST
] + 3);
99 temp
= bytetemp
| 0x80;
100 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_TEST
] + 3, temp
);
102 rtl_write_byte(rtlpriv
, 0x2F8, 0x3);
104 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3, 0x72);
108 u8
efuse_read_1byte(struct ieee80211_hw
*hw
, u16 address
)
110 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
115 const u32 efuse_len
=
116 rtlpriv
->cfg
->maps
[EFUSE_REAL_CONTENT_SIZE
];
118 if (address
< efuse_len
) {
119 temp
= address
& 0xFF;
120 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 1,
122 bytetemp
= rtl_read_byte(rtlpriv
,
123 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 2);
124 temp
= ((address
>> 8) & 0x03) | (bytetemp
& 0xFC);
125 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 2,
128 bytetemp
= rtl_read_byte(rtlpriv
,
129 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3);
130 temp
= bytetemp
& 0x7F;
131 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3,
134 bytetemp
= rtl_read_byte(rtlpriv
,
135 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3);
136 while (!(bytetemp
& 0x80)) {
137 bytetemp
= rtl_read_byte(rtlpriv
,
139 maps
[EFUSE_CTRL
] + 3);
146 data
= rtl_read_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
]);
152 EXPORT_SYMBOL(efuse_read_1byte
);
154 void efuse_write_1byte(struct ieee80211_hw
*hw
, u16 address
, u8 value
)
156 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
160 const u32 efuse_len
=
161 rtlpriv
->cfg
->maps
[EFUSE_REAL_CONTENT_SIZE
];
163 RT_TRACE(rtlpriv
, COMP_EFUSE
, DBG_LOUD
, "Addr=%x Data =%x\n",
166 if (address
< efuse_len
) {
167 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
], value
);
169 temp
= address
& 0xFF;
170 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 1,
172 bytetemp
= rtl_read_byte(rtlpriv
,
173 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 2);
175 temp
= ((address
>> 8) & 0x03) | (bytetemp
& 0xFC);
176 rtl_write_byte(rtlpriv
,
177 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 2, temp
);
179 bytetemp
= rtl_read_byte(rtlpriv
,
180 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3);
181 temp
= bytetemp
| 0x80;
182 rtl_write_byte(rtlpriv
,
183 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3, temp
);
185 bytetemp
= rtl_read_byte(rtlpriv
,
186 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3);
188 while (bytetemp
& 0x80) {
189 bytetemp
= rtl_read_byte(rtlpriv
,
191 maps
[EFUSE_CTRL
] + 3);
202 void read_efuse_byte(struct ieee80211_hw
*hw
, u16 _offset
, u8
*pbuf
)
204 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
209 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 1,
211 readbyte
= rtl_read_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 2);
212 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 2,
213 ((_offset
>> 8) & 0x03) | (readbyte
& 0xfc));
215 readbyte
= rtl_read_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3);
216 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3,
220 value32
= rtl_read_dword(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
]);
221 while (!(((value32
>> 24) & 0xff) & 0x80) && (retry
< 10000)) {
222 value32
= rtl_read_dword(rtlpriv
,
223 rtlpriv
->cfg
->maps
[EFUSE_CTRL
]);
228 value32
= rtl_read_dword(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
]);
230 *pbuf
= (u8
) (value32
& 0xff);
232 EXPORT_SYMBOL_GPL(read_efuse_byte
);
234 void read_efuse(struct ieee80211_hw
*hw
, u16 _offset
, u16 _size_byte
, u8
*pbuf
)
236 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
237 struct rtl_efuse
*rtlefuse
= rtl_efuse(rtl_priv(hw
));
245 const u16 efuse_max_section
=
246 rtlpriv
->cfg
->maps
[EFUSE_MAX_SECTION_MAP
];
247 const u32 efuse_len
=
248 rtlpriv
->cfg
->maps
[EFUSE_REAL_CONTENT_SIZE
];
250 u16 efuse_utilized
= 0;
253 if ((_offset
+ _size_byte
) > rtlpriv
->cfg
->maps
[EFUSE_HWSET_MAX_SIZE
]) {
254 RT_TRACE(rtlpriv
, COMP_EFUSE
, DBG_LOUD
,
255 "read_efuse(): Invalid offset(%#x) with read bytes(%#x)!!\n",
256 _offset
, _size_byte
);
260 /* allocate memory for efuse_tbl and efuse_word */
261 efuse_tbl
= kmalloc(rtlpriv
->cfg
->maps
[EFUSE_HWSET_MAX_SIZE
] *
262 sizeof(u8
), GFP_ATOMIC
);
265 efuse_word
= kzalloc(EFUSE_MAX_WORD_UNIT
* sizeof(u16
*), GFP_ATOMIC
);
268 for (i
= 0; i
< EFUSE_MAX_WORD_UNIT
; i
++) {
269 efuse_word
[i
] = kmalloc(efuse_max_section
* sizeof(u16
),
275 for (i
= 0; i
< efuse_max_section
; i
++)
276 for (j
= 0; j
< EFUSE_MAX_WORD_UNIT
; j
++)
277 efuse_word
[j
][i
] = 0xFFFF;
279 read_efuse_byte(hw
, efuse_addr
, rtemp8
);
280 if (*rtemp8
!= 0xFF) {
282 RTPRINT(rtlpriv
, FEEPROM
, EFUSE_READ_ALL
,
283 "Addr=%d\n", efuse_addr
);
287 while ((*rtemp8
!= 0xFF) && (efuse_addr
< efuse_len
)) {
288 /* Check PG header for section num. */
289 if ((*rtemp8
& 0x1F) == 0x0F) {/* extended header */
290 u1temp
= ((*rtemp8
& 0xE0) >> 5);
291 read_efuse_byte(hw
, efuse_addr
, rtemp8
);
293 if ((*rtemp8
& 0x0F) == 0x0F) {
295 read_efuse_byte(hw
, efuse_addr
, rtemp8
);
297 if (*rtemp8
!= 0xFF &&
298 (efuse_addr
< efuse_len
)) {
303 offset
= ((*rtemp8
& 0xF0) >> 1) | u1temp
;
304 wren
= (*rtemp8
& 0x0F);
308 offset
= ((*rtemp8
>> 4) & 0x0f);
309 wren
= (*rtemp8
& 0x0f);
312 if (offset
< efuse_max_section
) {
313 RTPRINT(rtlpriv
, FEEPROM
, EFUSE_READ_ALL
,
314 "offset-%d Worden=%x\n", offset
, wren
);
316 for (i
= 0; i
< EFUSE_MAX_WORD_UNIT
; i
++) {
317 if (!(wren
& 0x01)) {
318 RTPRINT(rtlpriv
, FEEPROM
,
320 "Addr=%d\n", efuse_addr
);
322 read_efuse_byte(hw
, efuse_addr
, rtemp8
);
325 efuse_word
[i
][offset
] =
328 if (efuse_addr
>= efuse_len
)
331 RTPRINT(rtlpriv
, FEEPROM
,
333 "Addr=%d\n", efuse_addr
);
335 read_efuse_byte(hw
, efuse_addr
, rtemp8
);
338 efuse_word
[i
][offset
] |=
339 (((u16
)*rtemp8
<< 8) & 0xff00);
341 if (efuse_addr
>= efuse_len
)
349 RTPRINT(rtlpriv
, FEEPROM
, EFUSE_READ_ALL
,
350 "Addr=%d\n", efuse_addr
);
351 read_efuse_byte(hw
, efuse_addr
, rtemp8
);
352 if (*rtemp8
!= 0xFF && (efuse_addr
< efuse_len
)) {
358 for (i
= 0; i
< efuse_max_section
; i
++) {
359 for (j
= 0; j
< EFUSE_MAX_WORD_UNIT
; j
++) {
360 efuse_tbl
[(i
* 8) + (j
* 2)] =
361 (efuse_word
[j
][i
] & 0xff);
362 efuse_tbl
[(i
* 8) + ((j
* 2) + 1)] =
363 ((efuse_word
[j
][i
] >> 8) & 0xff);
367 for (i
= 0; i
< _size_byte
; i
++)
368 pbuf
[i
] = efuse_tbl
[_offset
+ i
];
370 rtlefuse
->efuse_usedbytes
= efuse_utilized
;
371 efuse_usage
= (u8
) ((efuse_utilized
* 100) / efuse_len
);
372 rtlefuse
->efuse_usedpercentage
= efuse_usage
;
373 rtlpriv
->cfg
->ops
->set_hw_reg(hw
, HW_VAR_EFUSE_BYTES
,
374 (u8
*)&efuse_utilized
);
375 rtlpriv
->cfg
->ops
->set_hw_reg(hw
, HW_VAR_EFUSE_USAGE
,
378 for (i
= 0; i
< EFUSE_MAX_WORD_UNIT
; i
++)
379 kfree(efuse_word
[i
]);
385 bool efuse_shadow_update_chk(struct ieee80211_hw
*hw
)
387 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
388 struct rtl_efuse
*rtlefuse
= rtl_efuse(rtl_priv(hw
));
389 u8 section_idx
, i
, Base
;
390 u16 words_need
= 0, hdr_num
= 0, totalbytes
, efuse_used
;
391 bool wordchanged
, result
= true;
393 for (section_idx
= 0; section_idx
< 16; section_idx
++) {
394 Base
= section_idx
* 8;
397 for (i
= 0; i
< 8; i
= i
+ 2) {
398 if ((rtlefuse
->efuse_map
[EFUSE_INIT_MAP
][Base
+ i
] !=
399 rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][Base
+ i
]) ||
400 (rtlefuse
->efuse_map
[EFUSE_INIT_MAP
][Base
+ i
+ 1] !=
401 rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][Base
+ i
+
412 totalbytes
= hdr_num
+ words_need
* 2;
413 efuse_used
= rtlefuse
->efuse_usedbytes
;
415 if ((totalbytes
+ efuse_used
) >=
417 rtlpriv
->cfg
->maps
[EFUSE_OOB_PROTECT_BYTES_LEN
]))
420 RT_TRACE(rtlpriv
, COMP_EFUSE
, DBG_LOUD
,
421 "efuse_shadow_update_chk(): totalbytes(%#x), hdr_num(%#x), words_need(%#x), efuse_used(%d)\n",
422 totalbytes
, hdr_num
, words_need
, efuse_used
);
427 void efuse_shadow_read(struct ieee80211_hw
*hw
, u8 type
,
428 u16 offset
, u32
*value
)
431 efuse_shadow_read_1byte(hw
, offset
, (u8
*) value
);
433 efuse_shadow_read_2byte(hw
, offset
, (u16
*) value
);
435 efuse_shadow_read_4byte(hw
, offset
, value
);
439 void efuse_shadow_write(struct ieee80211_hw
*hw
, u8 type
, u16 offset
,
443 efuse_shadow_write_1byte(hw
, offset
, (u8
) value
);
445 efuse_shadow_write_2byte(hw
, offset
, (u16
) value
);
447 efuse_shadow_write_4byte(hw
, offset
, value
);
451 bool efuse_shadow_update(struct ieee80211_hw
*hw
)
453 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
454 struct rtl_efuse
*rtlefuse
= rtl_efuse(rtl_priv(hw
));
459 RT_TRACE(rtlpriv
, COMP_EFUSE
, DBG_LOUD
, "--->\n");
461 if (!efuse_shadow_update_chk(hw
)) {
462 efuse_read_all_map(hw
, &rtlefuse
->efuse_map
[EFUSE_INIT_MAP
][0]);
463 memcpy(&rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][0],
464 &rtlefuse
->efuse_map
[EFUSE_INIT_MAP
][0],
465 rtlpriv
->cfg
->maps
[EFUSE_HWSET_MAX_SIZE
]);
467 RT_TRACE(rtlpriv
, COMP_EFUSE
, DBG_LOUD
,
468 "<---efuse out of capacity!!\n");
471 efuse_power_switch(hw
, true, true);
473 for (offset
= 0; offset
< 16; offset
++) {
478 for (i
= 0; i
< 8; 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
];
487 if (rtlefuse
->efuse_map
[EFUSE_INIT_MAP
][base
+ i
] !=
488 rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][base
+ i
]) {
489 word_en
&= ~(BIT(i
/ 2));
491 rtlefuse
->efuse_map
[EFUSE_INIT_MAP
][base
+ i
] =
492 rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][base
+ i
];
497 if (word_en
!= 0x0F) {
500 &rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][base
],
502 RT_PRINT_DATA(rtlpriv
, COMP_INIT
, DBG_LOUD
,
503 "U-efuse", tmpdata
, 8);
505 if (!efuse_pg_packet_write(hw
, (u8
) offset
, word_en
,
507 RT_TRACE(rtlpriv
, COMP_ERR
, DBG_WARNING
,
508 "PG section(%#x) fail!!\n", offset
);
515 efuse_power_switch(hw
, true, false);
516 efuse_read_all_map(hw
, &rtlefuse
->efuse_map
[EFUSE_INIT_MAP
][0]);
518 memcpy(&rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][0],
519 &rtlefuse
->efuse_map
[EFUSE_INIT_MAP
][0],
520 rtlpriv
->cfg
->maps
[EFUSE_HWSET_MAX_SIZE
]);
522 RT_TRACE(rtlpriv
, COMP_EFUSE
, DBG_LOUD
, "<---\n");
526 void rtl_efuse_shadow_map_update(struct ieee80211_hw
*hw
)
528 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
529 struct rtl_efuse
*rtlefuse
= rtl_efuse(rtl_priv(hw
));
531 if (rtlefuse
->autoload_failflag
)
532 memset(&rtlefuse
->efuse_map
[EFUSE_INIT_MAP
][0], 0xFF,
533 rtlpriv
->cfg
->maps
[EFUSE_HWSET_MAX_SIZE
]);
535 efuse_read_all_map(hw
, &rtlefuse
->efuse_map
[EFUSE_INIT_MAP
][0]);
537 memcpy(&rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][0],
538 &rtlefuse
->efuse_map
[EFUSE_INIT_MAP
][0],
539 rtlpriv
->cfg
->maps
[EFUSE_HWSET_MAX_SIZE
]);
542 EXPORT_SYMBOL(rtl_efuse_shadow_map_update
);
544 void efuse_force_write_vendor_Id(struct ieee80211_hw
*hw
)
546 u8 tmpdata
[8] = { 0xFF, 0xFF, 0xEC, 0x10, 0xFF, 0xFF, 0xFF, 0xFF };
548 efuse_power_switch(hw
, true, true);
550 efuse_pg_packet_write(hw
, 1, 0xD, tmpdata
);
552 efuse_power_switch(hw
, true, false);
556 void efuse_re_pg_section(struct ieee80211_hw
*hw
, u8 section_idx
)
560 static void efuse_shadow_read_1byte(struct ieee80211_hw
*hw
,
561 u16 offset
, u8
*value
)
563 struct rtl_efuse
*rtlefuse
= rtl_efuse(rtl_priv(hw
));
564 *value
= rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
];
567 static void efuse_shadow_read_2byte(struct ieee80211_hw
*hw
,
568 u16 offset
, u16
*value
)
570 struct rtl_efuse
*rtlefuse
= rtl_efuse(rtl_priv(hw
));
572 *value
= rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
];
573 *value
|= rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
+ 1] << 8;
577 static void efuse_shadow_read_4byte(struct ieee80211_hw
*hw
,
578 u16 offset
, u32
*value
)
580 struct rtl_efuse
*rtlefuse
= rtl_efuse(rtl_priv(hw
));
582 *value
= rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
];
583 *value
|= rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
+ 1] << 8;
584 *value
|= rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
+ 2] << 16;
585 *value
|= rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
+ 3] << 24;
588 static void efuse_shadow_write_1byte(struct ieee80211_hw
*hw
,
589 u16 offset
, u8 value
)
591 struct rtl_efuse
*rtlefuse
= rtl_efuse(rtl_priv(hw
));
593 rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
] = value
;
596 static void efuse_shadow_write_2byte(struct ieee80211_hw
*hw
,
597 u16 offset
, u16 value
)
599 struct rtl_efuse
*rtlefuse
= rtl_efuse(rtl_priv(hw
));
601 rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
] = value
& 0x00FF;
602 rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
+ 1] = value
>> 8;
606 static void efuse_shadow_write_4byte(struct ieee80211_hw
*hw
,
607 u16 offset
, u32 value
)
609 struct rtl_efuse
*rtlefuse
= rtl_efuse(rtl_priv(hw
));
611 rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
] =
612 (u8
) (value
& 0x000000FF);
613 rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
+ 1] =
614 (u8
) ((value
>> 8) & 0x0000FF);
615 rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
+ 2] =
616 (u8
) ((value
>> 16) & 0x00FF);
617 rtlefuse
->efuse_map
[EFUSE_MODIFY_MAP
][offset
+ 3] =
618 (u8
) ((value
>> 24) & 0xFF);
622 static int efuse_one_byte_read(struct ieee80211_hw
*hw
, u16 addr
, u8
*data
)
624 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
628 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 1,
630 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 2,
631 ((u8
) ((addr
>> 8) & 0x03)) |
632 (rtl_read_byte(rtlpriv
,
633 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 2) &
636 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3, 0x72);
638 while (!(0x80 & rtl_read_byte(rtlpriv
,
639 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3))
645 *data
= rtl_read_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
]);
654 static int efuse_one_byte_write(struct ieee80211_hw
*hw
, u16 addr
, u8 data
)
656 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
659 RT_TRACE(rtlpriv
, COMP_EFUSE
, DBG_LOUD
, "Addr = %x Data=%x\n",
662 rtl_write_byte(rtlpriv
,
663 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 1, (u8
) (addr
& 0xff));
664 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 2,
665 (rtl_read_byte(rtlpriv
,
666 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] +
667 2) & 0xFC) | (u8
) ((addr
>> 8) & 0x03));
669 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
], data
);
670 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3, 0xF2);
672 while ((0x80 & rtl_read_byte(rtlpriv
,
673 rtlpriv
->cfg
->maps
[EFUSE_CTRL
] + 3))
684 static void efuse_read_all_map(struct ieee80211_hw
*hw
, u8
* efuse
)
686 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
687 efuse_power_switch(hw
, false, true);
688 read_efuse(hw
, 0, rtlpriv
->cfg
->maps
[EFUSE_HWSET_MAX_SIZE
], efuse
);
689 efuse_power_switch(hw
, false, false);
692 static void efuse_read_data_case1(struct ieee80211_hw
*hw
, u16
*efuse_addr
,
693 u8 efuse_data
, u8 offset
, u8
*tmpdata
,
696 bool dataempty
= true;
702 hoffset
= (efuse_data
>> 4) & 0x0F;
703 hworden
= efuse_data
& 0x0F;
704 word_cnts
= efuse_calculate_word_cnts(hworden
);
706 if (hoffset
== offset
) {
707 for (tmpidx
= 0; tmpidx
< word_cnts
* 2; tmpidx
++) {
708 if (efuse_one_byte_read(hw
, *efuse_addr
+ 1 + tmpidx
,
710 tmpdata
[tmpidx
] = efuse_data
;
711 if (efuse_data
!= 0xff)
717 *readstate
= PG_STATE_DATA
;
719 *efuse_addr
= *efuse_addr
+ (word_cnts
* 2) + 1;
720 *readstate
= PG_STATE_HEADER
;
724 *efuse_addr
= *efuse_addr
+ (word_cnts
* 2) + 1;
725 *readstate
= PG_STATE_HEADER
;
729 static int efuse_pg_packet_read(struct ieee80211_hw
*hw
, u8 offset
, u8
*data
)
731 u8 readstate
= PG_STATE_HEADER
;
732 bool continual
= true;
733 u8 efuse_data
, word_cnts
= 0;
742 memset(data
, 0xff, PGPKT_DATA_SIZE
* sizeof(u8
));
743 memset(tmpdata
, 0xff, PGPKT_DATA_SIZE
* sizeof(u8
));
745 while (continual
&& (efuse_addr
< EFUSE_MAX_SIZE
)) {
746 if (readstate
& PG_STATE_HEADER
) {
747 if (efuse_one_byte_read(hw
, efuse_addr
, &efuse_data
)
748 && (efuse_data
!= 0xFF))
749 efuse_read_data_case1(hw
, &efuse_addr
,
755 } else if (readstate
& PG_STATE_DATA
) {
756 efuse_word_enable_data_read(0, tmpdata
, data
);
757 efuse_addr
= efuse_addr
+ (word_cnts
* 2) + 1;
758 readstate
= PG_STATE_HEADER
;
763 if ((data
[0] == 0xff) && (data
[1] == 0xff) &&
764 (data
[2] == 0xff) && (data
[3] == 0xff) &&
765 (data
[4] == 0xff) && (data
[5] == 0xff) &&
766 (data
[6] == 0xff) && (data
[7] == 0xff))
773 static void efuse_write_data_case1(struct ieee80211_hw
*hw
, u16
*efuse_addr
,
774 u8 efuse_data
, u8 offset
, int *continual
,
775 u8
*write_state
, struct pgpkt_struct
*target_pkt
,
776 int *repeat_times
, int *result
, u8 word_en
)
778 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
779 struct pgpkt_struct tmp_pkt
;
780 bool dataempty
= true;
781 u8 originaldata
[8 * sizeof(u8
)];
783 u8 match_word_en
, tmp_word_en
;
785 u8 tmp_header
= efuse_data
;
788 tmp_pkt
.offset
= (tmp_header
>> 4) & 0x0F;
789 tmp_pkt
.word_en
= tmp_header
& 0x0F;
790 tmp_word_cnts
= efuse_calculate_word_cnts(tmp_pkt
.word_en
);
792 if (tmp_pkt
.offset
!= target_pkt
->offset
) {
793 *efuse_addr
= *efuse_addr
+ (tmp_word_cnts
* 2) + 1;
794 *write_state
= PG_STATE_HEADER
;
796 for (tmpindex
= 0; tmpindex
< (tmp_word_cnts
* 2); tmpindex
++) {
797 u16 address
= *efuse_addr
+ 1 + tmpindex
;
798 if (efuse_one_byte_read(hw
, address
,
799 &efuse_data
) && (efuse_data
!= 0xFF))
804 *efuse_addr
= *efuse_addr
+ (tmp_word_cnts
* 2) + 1;
805 *write_state
= PG_STATE_HEADER
;
807 match_word_en
= 0x0F;
808 if (!((target_pkt
->word_en
& BIT(0)) |
809 (tmp_pkt
.word_en
& BIT(0))))
810 match_word_en
&= (~BIT(0));
812 if (!((target_pkt
->word_en
& BIT(1)) |
813 (tmp_pkt
.word_en
& BIT(1))))
814 match_word_en
&= (~BIT(1));
816 if (!((target_pkt
->word_en
& BIT(2)) |
817 (tmp_pkt
.word_en
& BIT(2))))
818 match_word_en
&= (~BIT(2));
820 if (!((target_pkt
->word_en
& BIT(3)) |
821 (tmp_pkt
.word_en
& BIT(3))))
822 match_word_en
&= (~BIT(3));
824 if ((match_word_en
& 0x0F) != 0x0F) {
825 badworden
= efuse_word_enable_data_write(
830 if (0x0F != (badworden
& 0x0F)) {
831 u8 reorg_offset
= offset
;
832 u8 reorg_worden
= badworden
;
833 efuse_pg_packet_write(hw
, reorg_offset
,
839 if ((target_pkt
->word_en
& BIT(0)) ^
840 (match_word_en
& BIT(0)))
841 tmp_word_en
&= (~BIT(0));
843 if ((target_pkt
->word_en
& BIT(1)) ^
844 (match_word_en
& BIT(1)))
845 tmp_word_en
&= (~BIT(1));
847 if ((target_pkt
->word_en
& BIT(2)) ^
848 (match_word_en
& BIT(2)))
849 tmp_word_en
&= (~BIT(2));
851 if ((target_pkt
->word_en
& BIT(3)) ^
852 (match_word_en
& BIT(3)))
853 tmp_word_en
&= (~BIT(3));
855 if ((tmp_word_en
& 0x0F) != 0x0F) {
856 *efuse_addr
= efuse_get_current_size(hw
);
857 target_pkt
->offset
= offset
;
858 target_pkt
->word_en
= tmp_word_en
;
862 *write_state
= PG_STATE_HEADER
;
864 if (*repeat_times
> EFUSE_REPEAT_THRESHOLD_
) {
869 *efuse_addr
+= (2 * tmp_word_cnts
) + 1;
870 target_pkt
->offset
= offset
;
871 target_pkt
->word_en
= word_en
;
872 *write_state
= PG_STATE_HEADER
;
876 RTPRINT(rtlpriv
, FEEPROM
, EFUSE_PG
, "efuse PG_STATE_HEADER-1\n");
879 static void efuse_write_data_case2(struct ieee80211_hw
*hw
, u16
*efuse_addr
,
880 int *continual
, u8
*write_state
,
881 struct pgpkt_struct target_pkt
,
882 int *repeat_times
, int *result
)
884 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
885 struct pgpkt_struct tmp_pkt
;
888 u8 originaldata
[8 * sizeof(u8
)];
892 pg_header
= ((target_pkt
.offset
<< 4) & 0xf0) | target_pkt
.word_en
;
893 efuse_one_byte_write(hw
, *efuse_addr
, pg_header
);
894 efuse_one_byte_read(hw
, *efuse_addr
, &tmp_header
);
896 if (tmp_header
== pg_header
) {
897 *write_state
= PG_STATE_DATA
;
898 } else if (tmp_header
== 0xFF) {
899 *write_state
= PG_STATE_HEADER
;
901 if (*repeat_times
> EFUSE_REPEAT_THRESHOLD_
) {
906 tmp_pkt
.offset
= (tmp_header
>> 4) & 0x0F;
907 tmp_pkt
.word_en
= tmp_header
& 0x0F;
909 tmp_word_cnts
= efuse_calculate_word_cnts(tmp_pkt
.word_en
);
911 memset(originaldata
, 0xff, 8 * sizeof(u8
));
913 if (efuse_pg_packet_read(hw
, tmp_pkt
.offset
, originaldata
)) {
914 badworden
= efuse_word_enable_data_write(hw
,
915 *efuse_addr
+ 1, tmp_pkt
.word_en
,
918 if (0x0F != (badworden
& 0x0F)) {
919 u8 reorg_offset
= tmp_pkt
.offset
;
920 u8 reorg_worden
= badworden
;
921 efuse_pg_packet_write(hw
, reorg_offset
,
924 *efuse_addr
= efuse_get_current_size(hw
);
926 *efuse_addr
= *efuse_addr
+ (tmp_word_cnts
* 2)
930 *efuse_addr
= *efuse_addr
+ (tmp_word_cnts
* 2) + 1;
933 *write_state
= PG_STATE_HEADER
;
935 if (*repeat_times
> EFUSE_REPEAT_THRESHOLD_
) {
940 RTPRINT(rtlpriv
, FEEPROM
, EFUSE_PG
,
941 "efuse PG_STATE_HEADER-2\n");
945 static int efuse_pg_packet_write(struct ieee80211_hw
*hw
,
946 u8 offset
, u8 word_en
, u8
*data
)
948 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
949 struct pgpkt_struct target_pkt
;
950 u8 write_state
= PG_STATE_HEADER
;
951 int continual
= true, result
= true;
954 u8 target_word_cnts
= 0;
956 static int repeat_times
;
958 if (efuse_get_current_size(hw
) >= (EFUSE_MAX_SIZE
-
959 rtlpriv
->cfg
->maps
[EFUSE_OOB_PROTECT_BYTES_LEN
])) {
960 RTPRINT(rtlpriv
, FEEPROM
, EFUSE_PG
,
961 "efuse_pg_packet_write error\n");
965 target_pkt
.offset
= offset
;
966 target_pkt
.word_en
= word_en
;
968 memset(target_pkt
.data
, 0xFF, 8 * sizeof(u8
));
970 efuse_word_enable_data_read(word_en
, data
, target_pkt
.data
);
971 target_word_cnts
= efuse_calculate_word_cnts(target_pkt
.word_en
);
973 RTPRINT(rtlpriv
, FEEPROM
, EFUSE_PG
, "efuse Power ON\n");
975 while (continual
&& (efuse_addr
< (EFUSE_MAX_SIZE
-
976 rtlpriv
->cfg
->maps
[EFUSE_OOB_PROTECT_BYTES_LEN
]))) {
978 if (write_state
== PG_STATE_HEADER
) {
980 RTPRINT(rtlpriv
, FEEPROM
, EFUSE_PG
,
981 "efuse PG_STATE_HEADER\n");
983 if (efuse_one_byte_read(hw
, efuse_addr
, &efuse_data
) &&
984 (efuse_data
!= 0xFF))
985 efuse_write_data_case1(hw
, &efuse_addr
,
988 &write_state
, &target_pkt
,
989 &repeat_times
, &result
,
992 efuse_write_data_case2(hw
, &efuse_addr
,
999 } else if (write_state
== PG_STATE_DATA
) {
1000 RTPRINT(rtlpriv
, FEEPROM
, EFUSE_PG
,
1001 "efuse PG_STATE_DATA\n");
1003 efuse_word_enable_data_write(hw
, efuse_addr
+ 1,
1007 if ((badworden
& 0x0F) == 0x0F) {
1010 efuse_addr
+= (2 * target_word_cnts
) + 1;
1012 target_pkt
.offset
= offset
;
1013 target_pkt
.word_en
= badworden
;
1015 efuse_calculate_word_cnts(target_pkt
.
1017 write_state
= PG_STATE_HEADER
;
1019 if (repeat_times
> EFUSE_REPEAT_THRESHOLD_
) {
1023 RTPRINT(rtlpriv
, FEEPROM
, EFUSE_PG
,
1024 "efuse PG_STATE_HEADER-3\n");
1029 if (efuse_addr
>= (EFUSE_MAX_SIZE
-
1030 rtlpriv
->cfg
->maps
[EFUSE_OOB_PROTECT_BYTES_LEN
])) {
1031 RT_TRACE(rtlpriv
, COMP_EFUSE
, DBG_LOUD
,
1032 "efuse_addr(%#x) Out of size!!\n", efuse_addr
);
1038 static void efuse_word_enable_data_read(u8 word_en
,
1039 u8
*sourdata
, u8
*targetdata
)
1041 if (!(word_en
& BIT(0))) {
1042 targetdata
[0] = sourdata
[0];
1043 targetdata
[1] = sourdata
[1];
1046 if (!(word_en
& BIT(1))) {
1047 targetdata
[2] = sourdata
[2];
1048 targetdata
[3] = sourdata
[3];
1051 if (!(word_en
& BIT(2))) {
1052 targetdata
[4] = sourdata
[4];
1053 targetdata
[5] = sourdata
[5];
1056 if (!(word_en
& BIT(3))) {
1057 targetdata
[6] = sourdata
[6];
1058 targetdata
[7] = sourdata
[7];
1062 static u8
efuse_word_enable_data_write(struct ieee80211_hw
*hw
,
1063 u16 efuse_addr
, u8 word_en
, u8
*data
)
1065 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
1067 u16 start_addr
= efuse_addr
;
1068 u8 badworden
= 0x0F;
1071 memset(tmpdata
, 0xff, PGPKT_DATA_SIZE
);
1072 RT_TRACE(rtlpriv
, COMP_EFUSE
, DBG_LOUD
, "word_en = %x efuse_addr=%x\n",
1073 word_en
, efuse_addr
);
1075 if (!(word_en
& BIT(0))) {
1076 tmpaddr
= start_addr
;
1077 efuse_one_byte_write(hw
, start_addr
++, data
[0]);
1078 efuse_one_byte_write(hw
, start_addr
++, data
[1]);
1080 efuse_one_byte_read(hw
, tmpaddr
, &tmpdata
[0]);
1081 efuse_one_byte_read(hw
, tmpaddr
+ 1, &tmpdata
[1]);
1082 if ((data
[0] != tmpdata
[0]) || (data
[1] != tmpdata
[1]))
1083 badworden
&= (~BIT(0));
1086 if (!(word_en
& BIT(1))) {
1087 tmpaddr
= start_addr
;
1088 efuse_one_byte_write(hw
, start_addr
++, data
[2]);
1089 efuse_one_byte_write(hw
, start_addr
++, data
[3]);
1091 efuse_one_byte_read(hw
, tmpaddr
, &tmpdata
[2]);
1092 efuse_one_byte_read(hw
, tmpaddr
+ 1, &tmpdata
[3]);
1093 if ((data
[2] != tmpdata
[2]) || (data
[3] != tmpdata
[3]))
1094 badworden
&= (~BIT(1));
1097 if (!(word_en
& BIT(2))) {
1098 tmpaddr
= start_addr
;
1099 efuse_one_byte_write(hw
, start_addr
++, data
[4]);
1100 efuse_one_byte_write(hw
, start_addr
++, data
[5]);
1102 efuse_one_byte_read(hw
, tmpaddr
, &tmpdata
[4]);
1103 efuse_one_byte_read(hw
, tmpaddr
+ 1, &tmpdata
[5]);
1104 if ((data
[4] != tmpdata
[4]) || (data
[5] != tmpdata
[5]))
1105 badworden
&= (~BIT(2));
1108 if (!(word_en
& BIT(3))) {
1109 tmpaddr
= start_addr
;
1110 efuse_one_byte_write(hw
, start_addr
++, data
[6]);
1111 efuse_one_byte_write(hw
, start_addr
++, data
[7]);
1113 efuse_one_byte_read(hw
, tmpaddr
, &tmpdata
[6]);
1114 efuse_one_byte_read(hw
, tmpaddr
+ 1, &tmpdata
[7]);
1115 if ((data
[6] != tmpdata
[6]) || (data
[7] != tmpdata
[7]))
1116 badworden
&= (~BIT(3));
1122 static void efuse_power_switch(struct ieee80211_hw
*hw
, u8 write
, u8 pwrstate
)
1124 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
1125 struct rtl_hal
*rtlhal
= rtl_hal(rtl_priv(hw
));
1129 if (pwrstate
&& (rtlhal
->hw_type
!= HARDWARE_TYPE_RTL8192SE
)) {
1130 if (rtlhal
->hw_type
== HARDWARE_TYPE_RTL8188EE
)
1131 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_ACCESS
],
1134 tmpV16
= rtl_read_word(rtlpriv
,
1135 rtlpriv
->cfg
->maps
[SYS_ISO_CTRL
]);
1136 if (!(tmpV16
& rtlpriv
->cfg
->maps
[EFUSE_PWC_EV12V
])) {
1137 tmpV16
|= rtlpriv
->cfg
->maps
[EFUSE_PWC_EV12V
];
1138 rtl_write_word(rtlpriv
,
1139 rtlpriv
->cfg
->maps
[SYS_ISO_CTRL
],
1143 tmpV16
= rtl_read_word(rtlpriv
,
1144 rtlpriv
->cfg
->maps
[SYS_FUNC_EN
]);
1145 if (!(tmpV16
& rtlpriv
->cfg
->maps
[EFUSE_FEN_ELDR
])) {
1146 tmpV16
|= rtlpriv
->cfg
->maps
[EFUSE_FEN_ELDR
];
1147 rtl_write_word(rtlpriv
,
1148 rtlpriv
->cfg
->maps
[SYS_FUNC_EN
], tmpV16
);
1151 tmpV16
= rtl_read_word(rtlpriv
, rtlpriv
->cfg
->maps
[SYS_CLK
]);
1152 if ((!(tmpV16
& rtlpriv
->cfg
->maps
[EFUSE_LOADER_CLK_EN
])) ||
1153 (!(tmpV16
& rtlpriv
->cfg
->maps
[EFUSE_ANA8M
]))) {
1154 tmpV16
|= (rtlpriv
->cfg
->maps
[EFUSE_LOADER_CLK_EN
] |
1155 rtlpriv
->cfg
->maps
[EFUSE_ANA8M
]);
1156 rtl_write_word(rtlpriv
,
1157 rtlpriv
->cfg
->maps
[SYS_CLK
], tmpV16
);
1163 tempval
= rtl_read_byte(rtlpriv
,
1164 rtlpriv
->cfg
->maps
[EFUSE_TEST
] +
1167 if (rtlhal
->hw_type
!= HARDWARE_TYPE_RTL8192SE
) {
1169 tempval
|= (VOLTAGE_V25
<< 4);
1172 rtl_write_byte(rtlpriv
,
1173 rtlpriv
->cfg
->maps
[EFUSE_TEST
] + 3,
1177 if (rtlhal
->hw_type
== HARDWARE_TYPE_RTL8192SE
) {
1178 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CLK
],
1183 if (rtlhal
->hw_type
== HARDWARE_TYPE_RTL8188EE
)
1184 rtl_write_byte(rtlpriv
,
1185 rtlpriv
->cfg
->maps
[EFUSE_ACCESS
], 0);
1188 tempval
= rtl_read_byte(rtlpriv
,
1189 rtlpriv
->cfg
->maps
[EFUSE_TEST
] +
1191 rtl_write_byte(rtlpriv
,
1192 rtlpriv
->cfg
->maps
[EFUSE_TEST
] + 3,
1196 if (rtlhal
->hw_type
== HARDWARE_TYPE_RTL8192SE
) {
1197 rtl_write_byte(rtlpriv
, rtlpriv
->cfg
->maps
[EFUSE_CLK
],
1205 static u16
efuse_get_current_size(struct ieee80211_hw
*hw
)
1209 u8 efuse_data
, word_cnts
;
1211 while (efuse_one_byte_read(hw
, efuse_addr
, &efuse_data
) &&
1212 efuse_addr
< EFUSE_MAX_SIZE
) {
1213 if (efuse_data
== 0xFF)
1216 hworden
= efuse_data
& 0x0F;
1217 word_cnts
= efuse_calculate_word_cnts(hworden
);
1218 efuse_addr
= efuse_addr
+ (word_cnts
* 2) + 1;
1224 static u8
efuse_calculate_word_cnts(u8 word_en
)
1227 if (!(word_en
& BIT(0)))
1229 if (!(word_en
& BIT(1)))
1231 if (!(word_en
& BIT(2)))
1233 if (!(word_en
& BIT(3)))