1 /******************************************************************************
2  *
3  * Copyright(c) 2009-2012  Realtek Corporation.
4  *
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.
8  *
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
12  * more details.
13  *
14  * Tme full GNU General Public License is included in this distribution in the
15  * file called LICENSE.
16  *
17  * Contact Information:
18  * wlanfae <wlanfae@realtek.com>
19  * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
20  * Hsinchu 300, Taiwan.
21  *
22  * Larry Finger <Larry.Finger@lwfinger.net>
23  *
24  *****************************************************************************/
25 #include "wifi.h"
26 #include "efuse.h"
27 #include "pci.h"
28 #include <linux/export.h>
29 
30 static const u8 MAX_PGPKT_SIZE = 9;
31 static const u8 PGPKT_DATA_SIZE = 8;
32 static const int EFUSE_MAX_SIZE = 512;
33 
34 #define START_ADDRESS		0x1000
35 #define REG_MCUFWDL		0x0080
36 
37 static const struct efuse_map RTL8712_SDIO_EFUSE_TABLE[] = {
38 	{0, 0, 0, 2},
39 	{0, 1, 0, 2},
40 	{0, 2, 0, 2},
41 	{1, 0, 0, 1},
42 	{1, 0, 1, 1},
43 	{1, 1, 0, 1},
44 	{1, 1, 1, 3},
45 	{1, 3, 0, 17},
46 	{3, 3, 1, 48},
47 	{10, 0, 0, 6},
48 	{10, 3, 0, 1},
49 	{10, 3, 1, 1},
50 	{11, 0, 0, 28}
51 };
52 
53 static const struct rtl_efuse_ops efuse_ops = {
54 	.efuse_onebyte_read = efuse_one_byte_read,
55 	.efuse_logical_map_read = efuse_shadow_read,
56 };
57 
58 static void efuse_shadow_read_1byte(struct ieee80211_hw *hw, u16 offset,
59 				    u8 *value);
60 static void efuse_shadow_read_2byte(struct ieee80211_hw *hw, u16 offset,
61 				    u16 *value);
62 static void efuse_shadow_read_4byte(struct ieee80211_hw *hw, u16 offset,
63 				    u32 *value);
64 static void efuse_shadow_write_1byte(struct ieee80211_hw *hw, u16 offset,
65 				     u8 value);
66 static void efuse_shadow_write_2byte(struct ieee80211_hw *hw, u16 offset,
67 				     u16 value);
68 static void efuse_shadow_write_4byte(struct ieee80211_hw *hw, u16 offset,
69 				     u32 value);
70 static int efuse_one_byte_write(struct ieee80211_hw *hw, u16 addr,
71 				u8 data);
72 static void efuse_read_all_map(struct ieee80211_hw *hw, u8 *efuse);
73 static int efuse_pg_packet_read(struct ieee80211_hw *hw, u8 offset,
74 				u8 *data);
75 static int efuse_pg_packet_write(struct ieee80211_hw *hw, u8 offset,
76 				 u8 word_en, u8 *data);
77 static void efuse_word_enable_data_read(u8 word_en, u8 *sourdata,
78 					u8 *targetdata);
79 static u8 enable_efuse_data_write(struct ieee80211_hw *hw,
80 				  u16 efuse_addr, u8 word_en, u8 *data);
81 static u16 efuse_get_current_size(struct ieee80211_hw *hw);
82 static u8 efuse_calculate_word_cnts(u8 word_en);
83 
84 void efuse_initialize(struct ieee80211_hw *hw)
85 {
86 	struct rtl_priv *rtlpriv = rtl_priv(hw);
87 	u8 bytetemp;
88 	u8 temp;
89 
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);
93 
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);
97 
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);
101 
102 	rtl_write_byte(rtlpriv, 0x2F8, 0x3);
103 
104 	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3, 0x72);
105 
106 }
107 
108 u8 efuse_read_1byte(struct ieee80211_hw *hw, u16 address)
109 {
110 	struct rtl_priv *rtlpriv = rtl_priv(hw);
111 	u8 data;
112 	u8 bytetemp;
113 	u8 temp;
114 	u32 k = 0;
115 	const u32 efuse_len =
116 		rtlpriv->cfg->maps[EFUSE_REAL_CONTENT_SIZE];
117 
118 	if (address < efuse_len) {
119 		temp = address & 0xFF;
120 		rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1,
121 			       temp);
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,
126 			       temp);
127 
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,
132 			       temp);
133 
134 		bytetemp = rtl_read_byte(rtlpriv,
135 					 rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
136 		while (!(bytetemp & 0x80)) {
137 			bytetemp = rtl_read_byte(rtlpriv,
138 						 rtlpriv->cfg->
139 						 maps[EFUSE_CTRL] + 3);
140 			k++;
141 			if (k == 1000) {
142 				k = 0;
143 				break;
144 			}
145 		}
146 		data = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL]);
147 		return data;
148 	} else
149 		return 0xFF;
150 
151 }
152 EXPORT_SYMBOL(efuse_read_1byte);
153 
154 void efuse_write_1byte(struct ieee80211_hw *hw, u16 address, u8 value)
155 {
156 	struct rtl_priv *rtlpriv = rtl_priv(hw);
157 	u8 bytetemp;
158 	u8 temp;
159 	u32 k = 0;
160 	const u32 efuse_len =
161 		rtlpriv->cfg->maps[EFUSE_REAL_CONTENT_SIZE];
162 
163 	RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD, "Addr=%x Data =%x\n",
164 		 address, value);
165 
166 	if (address < efuse_len) {
167 		rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL], value);
168 
169 		temp = address & 0xFF;
170 		rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1,
171 			       temp);
172 		bytetemp = rtl_read_byte(rtlpriv,
173 					 rtlpriv->cfg->maps[EFUSE_CTRL] + 2);
174 
175 		temp = ((address >> 8) & 0x03) | (bytetemp & 0xFC);
176 		rtl_write_byte(rtlpriv,
177 			       rtlpriv->cfg->maps[EFUSE_CTRL] + 2, temp);
178 
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);
184 
185 		bytetemp = rtl_read_byte(rtlpriv,
186 					 rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
187 
188 		while (bytetemp & 0x80) {
189 			bytetemp = rtl_read_byte(rtlpriv,
190 						 rtlpriv->cfg->
191 						 maps[EFUSE_CTRL] + 3);
192 			k++;
193 			if (k == 100) {
194 				k = 0;
195 				break;
196 			}
197 		}
198 	}
199 
200 }
201 
202 void read_efuse_byte(struct ieee80211_hw *hw, u16 _offset, u8 *pbuf)
203 {
204 	struct rtl_priv *rtlpriv = rtl_priv(hw);
205 	u32 value32;
206 	u8 readbyte;
207 	u16 retry;
208 
209 	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1,
210 		       (_offset & 0xff));
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));
214 
215 	readbyte = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
216 	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3,
217 		       (readbyte & 0x7f));
218 
219 	retry = 0;
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]);
224 		retry++;
225 	}
226 
227 	udelay(50);
228 	value32 = rtl_read_dword(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL]);
229 
230 	*pbuf = (u8) (value32 & 0xff);
231 }
232 EXPORT_SYMBOL_GPL(read_efuse_byte);
233 
234 void read_efuse(struct ieee80211_hw *hw, u16 _offset, u16 _size_byte, u8 *pbuf)
235 {
236 	struct rtl_priv *rtlpriv = rtl_priv(hw);
237 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
238 	u8 *efuse_tbl;
239 	u8 rtemp8[1];
240 	u16 efuse_addr = 0;
241 	u8 offset, wren;
242 	u8 u1temp = 0;
243 	u16 i;
244 	u16 j;
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];
249 	u16 **efuse_word;
250 	u16 efuse_utilized = 0;
251 	u8 efuse_usage;
252 
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);
257 		return;
258 	}
259 
260 	/* allocate memory for efuse_tbl and efuse_word */
261 	efuse_tbl = kzalloc(rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE],
262 			    GFP_ATOMIC);
263 	if (!efuse_tbl)
264 		return;
265 	efuse_word = kcalloc(EFUSE_MAX_WORD_UNIT, sizeof(u16 *), GFP_ATOMIC);
266 	if (!efuse_word)
267 		goto out;
268 	for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) {
269 		efuse_word[i] = kcalloc(efuse_max_section, sizeof(u16),
270 					GFP_ATOMIC);
271 		if (!efuse_word[i])
272 			goto done;
273 	}
274 
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;
278 
279 	read_efuse_byte(hw, efuse_addr, rtemp8);
280 	if (*rtemp8 != 0xFF) {
281 		efuse_utilized++;
282 		RTPRINT(rtlpriv, FEEPROM, EFUSE_READ_ALL,
283 			"Addr=%d\n", efuse_addr);
284 		efuse_addr++;
285 	}
286 
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);
292 
293 			if ((*rtemp8 & 0x0F) == 0x0F) {
294 				efuse_addr++;
295 				read_efuse_byte(hw, efuse_addr, rtemp8);
296 
297 				if (*rtemp8 != 0xFF &&
298 				    (efuse_addr < efuse_len)) {
299 					efuse_addr++;
300 				}
301 				continue;
302 			} else {
303 				offset = ((*rtemp8 & 0xF0) >> 1) | u1temp;
304 				wren = (*rtemp8 & 0x0F);
305 				efuse_addr++;
306 			}
307 		} else {
308 			offset = ((*rtemp8 >> 4) & 0x0f);
309 			wren = (*rtemp8 & 0x0f);
310 		}
311 
312 		if (offset < efuse_max_section) {
313 			RTPRINT(rtlpriv, FEEPROM, EFUSE_READ_ALL,
314 				"offset-%d Worden=%x\n", offset, wren);
315 
316 			for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) {
317 				if (!(wren & 0x01)) {
318 					RTPRINT(rtlpriv, FEEPROM,
319 						EFUSE_READ_ALL,
320 						"Addr=%d\n", efuse_addr);
321 
322 					read_efuse_byte(hw, efuse_addr, rtemp8);
323 					efuse_addr++;
324 					efuse_utilized++;
325 					efuse_word[i][offset] =
326 							 (*rtemp8 & 0xff);
327 
328 					if (efuse_addr >= efuse_len)
329 						break;
330 
331 					RTPRINT(rtlpriv, FEEPROM,
332 						EFUSE_READ_ALL,
333 						"Addr=%d\n", efuse_addr);
334 
335 					read_efuse_byte(hw, efuse_addr, rtemp8);
336 					efuse_addr++;
337 					efuse_utilized++;
338 					efuse_word[i][offset] |=
339 					    (((u16)*rtemp8 << 8) & 0xff00);
340 
341 					if (efuse_addr >= efuse_len)
342 						break;
343 				}
344 
345 				wren >>= 1;
346 			}
347 		}
348 
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)) {
353 			efuse_utilized++;
354 			efuse_addr++;
355 		}
356 	}
357 
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);
364 		}
365 	}
366 
367 	for (i = 0; i < _size_byte; i++)
368 		pbuf[i] = efuse_tbl[_offset + i];
369 
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,
376 				      &efuse_usage);
377 done:
378 	for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++)
379 		kfree(efuse_word[i]);
380 	kfree(efuse_word);
381 out:
382 	kfree(efuse_tbl);
383 }
384 
385 bool efuse_shadow_update_chk(struct ieee80211_hw *hw)
386 {
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;
392 
393 	for (section_idx = 0; section_idx < 16; section_idx++) {
394 		Base = section_idx * 8;
395 		wordchanged = false;
396 
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 +
402 								   1])) {
403 				words_need++;
404 				wordchanged = true;
405 			}
406 		}
407 
408 		if (wordchanged)
409 			hdr_num++;
410 	}
411 
412 	totalbytes = hdr_num + words_need * 2;
413 	efuse_used = rtlefuse->efuse_usedbytes;
414 
415 	if ((totalbytes + efuse_used) >=
416 	    (EFUSE_MAX_SIZE - rtlpriv->cfg->maps[EFUSE_OOB_PROTECT_BYTES_LEN]))
417 		result = false;
418 
419 	RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
420 		 "efuse_shadow_update_chk(): totalbytes(%#x), hdr_num(%#x), words_need(%#x), efuse_used(%d)\n",
421 		 totalbytes, hdr_num, words_need, efuse_used);
422 
423 	return result;
424 }
425 
426 void efuse_shadow_read(struct ieee80211_hw *hw, u8 type,
427 		       u16 offset, u32 *value)
428 {
429 	if (type == 1)
430 		efuse_shadow_read_1byte(hw, offset, (u8 *)value);
431 	else if (type == 2)
432 		efuse_shadow_read_2byte(hw, offset, (u16 *)value);
433 	else if (type == 4)
434 		efuse_shadow_read_4byte(hw, offset, value);
435 
436 }
437 EXPORT_SYMBOL(efuse_shadow_read);
438 
439 void efuse_shadow_write(struct ieee80211_hw *hw, u8 type, u16 offset,
440 				u32 value)
441 {
442 	if (type == 1)
443 		efuse_shadow_write_1byte(hw, offset, (u8) value);
444 	else if (type == 2)
445 		efuse_shadow_write_2byte(hw, offset, (u16) value);
446 	else if (type == 4)
447 		efuse_shadow_write_4byte(hw, offset, value);
448 
449 }
450 
451 bool efuse_shadow_update(struct ieee80211_hw *hw)
452 {
453 	struct rtl_priv *rtlpriv = rtl_priv(hw);
454 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
455 	u16 i, offset, base;
456 	u8 word_en = 0x0F;
457 	u8 first_pg = false;
458 
459 	RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD, "\n");
460 
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]);
466 
467 		RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
468 			 "efuse out of capacity!!\n");
469 		return false;
470 	}
471 	efuse_power_switch(hw, true, true);
472 
473 	for (offset = 0; offset < 16; offset++) {
474 
475 		word_en = 0x0F;
476 		base = offset * 8;
477 
478 		for (i = 0; i < 8; i++) {
479 			if (first_pg) {
480 				word_en &= ~(BIT(i / 2));
481 
482 				rtlefuse->efuse_map[EFUSE_INIT_MAP][base + i] =
483 				    rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base + i];
484 			} else {
485 
486 				if (rtlefuse->efuse_map[EFUSE_INIT_MAP][base + i] !=
487 				    rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base + i]) {
488 					word_en &= ~(BIT(i / 2));
489 
490 					rtlefuse->efuse_map[EFUSE_INIT_MAP][base + i] =
491 					    rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base + i];
492 				}
493 			}
494 		}
495 
496 		if (word_en != 0x0F) {
497 			u8 tmpdata[8];
498 			memcpy(tmpdata,
499 			       &rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base],
500 			       8);
501 			RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_LOUD,
502 				      "U-efuse\n", tmpdata, 8);
503 
504 			if (!efuse_pg_packet_write(hw, (u8) offset, word_en,
505 						   tmpdata)) {
506 				RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
507 					 "PG section(%#x) fail!!\n", offset);
508 				break;
509 			}
510 		}
511 
512 	}
513 
514 	efuse_power_switch(hw, true, false);
515 	efuse_read_all_map(hw, &rtlefuse->efuse_map[EFUSE_INIT_MAP][0]);
516 
517 	memcpy(&rtlefuse->efuse_map[EFUSE_MODIFY_MAP][0],
518 	       &rtlefuse->efuse_map[EFUSE_INIT_MAP][0],
519 	       rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]);
520 
521 	RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD, "\n");
522 	return true;
523 }
524 
525 void rtl_efuse_shadow_map_update(struct ieee80211_hw *hw)
526 {
527 	struct rtl_priv *rtlpriv = rtl_priv(hw);
528 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
529 
530 	if (rtlefuse->autoload_failflag)
531 		memset((&rtlefuse->efuse_map[EFUSE_INIT_MAP][0]),
532 		       0xFF, rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]);
533 	else
534 		efuse_read_all_map(hw, &rtlefuse->efuse_map[EFUSE_INIT_MAP][0]);
535 
536 	memcpy(&rtlefuse->efuse_map[EFUSE_MODIFY_MAP][0],
537 			&rtlefuse->efuse_map[EFUSE_INIT_MAP][0],
538 			rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]);
539 
540 }
541 EXPORT_SYMBOL(rtl_efuse_shadow_map_update);
542 
543 void efuse_force_write_vendor_Id(struct ieee80211_hw *hw)
544 {
545 	u8 tmpdata[8] = { 0xFF, 0xFF, 0xEC, 0x10, 0xFF, 0xFF, 0xFF, 0xFF };
546 
547 	efuse_power_switch(hw, true, true);
548 
549 	efuse_pg_packet_write(hw, 1, 0xD, tmpdata);
550 
551 	efuse_power_switch(hw, true, false);
552 
553 }
554 
555 void efuse_re_pg_section(struct ieee80211_hw *hw, u8 section_idx)
556 {
557 }
558 
559 static void efuse_shadow_read_1byte(struct ieee80211_hw *hw,
560 				    u16 offset, u8 *value)
561 {
562 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
563 	*value = rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset];
564 }
565 
566 static void efuse_shadow_read_2byte(struct ieee80211_hw *hw,
567 				    u16 offset, u16 *value)
568 {
569 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
570 
571 	*value = rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset];
572 	*value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] << 8;
573 
574 }
575 
576 static void efuse_shadow_read_4byte(struct ieee80211_hw *hw,
577 				    u16 offset, u32 *value)
578 {
579 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
580 
581 	*value = rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset];
582 	*value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] << 8;
583 	*value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 2] << 16;
584 	*value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 3] << 24;
585 }
586 
587 static void efuse_shadow_write_1byte(struct ieee80211_hw *hw,
588 				     u16 offset, u8 value)
589 {
590 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
591 
592 	rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset] = value;
593 }
594 
595 static void efuse_shadow_write_2byte(struct ieee80211_hw *hw,
596 				     u16 offset, u16 value)
597 {
598 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
599 
600 	rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset] = value & 0x00FF;
601 	rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] = value >> 8;
602 
603 }
604 
605 static void efuse_shadow_write_4byte(struct ieee80211_hw *hw,
606 				     u16 offset, u32 value)
607 {
608 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
609 
610 	rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset] =
611 	    (u8) (value & 0x000000FF);
612 	rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] =
613 	    (u8) ((value >> 8) & 0x0000FF);
614 	rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 2] =
615 	    (u8) ((value >> 16) & 0x00FF);
616 	rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 3] =
617 	    (u8) ((value >> 24) & 0xFF);
618 
619 }
620 
621 int efuse_one_byte_read(struct ieee80211_hw *hw, u16 addr, u8 *data)
622 {
623 	struct rtl_priv *rtlpriv = rtl_priv(hw);
624 	u8 tmpidx = 0;
625 	int result;
626 
627 	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1,
628 		       (u8) (addr & 0xff));
629 	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2,
630 		       ((u8) ((addr >> 8) & 0x03)) |
631 		       (rtl_read_byte(rtlpriv,
632 				      rtlpriv->cfg->maps[EFUSE_CTRL] + 2) &
633 			0xFC));
634 
635 	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3, 0x72);
636 
637 	while (!(0x80 & rtl_read_byte(rtlpriv,
638 				      rtlpriv->cfg->maps[EFUSE_CTRL] + 3))
639 	       && (tmpidx < 100)) {
640 		tmpidx++;
641 	}
642 
643 	if (tmpidx < 100) {
644 		*data = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL]);
645 		result = true;
646 	} else {
647 		*data = 0xff;
648 		result = false;
649 	}
650 	return result;
651 }
652 EXPORT_SYMBOL(efuse_one_byte_read);
653 
654 static int efuse_one_byte_write(struct ieee80211_hw *hw, u16 addr, u8 data)
655 {
656 	struct rtl_priv *rtlpriv = rtl_priv(hw);
657 	u8 tmpidx = 0;
658 
659 	RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
660 		 "Addr = %x Data=%x\n", addr, data);
661 
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));
668 
669 	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL], data);
670 	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3, 0xF2);
671 
672 	while ((0x80 & rtl_read_byte(rtlpriv,
673 				     rtlpriv->cfg->maps[EFUSE_CTRL] + 3))
674 	       && (tmpidx < 100)) {
675 		tmpidx++;
676 	}
677 
678 	if (tmpidx < 100)
679 		return true;
680 	return false;
681 }
682 
683 static void efuse_read_all_map(struct ieee80211_hw *hw, u8 *efuse)
684 {
685 	struct rtl_priv *rtlpriv = rtl_priv(hw);
686 	efuse_power_switch(hw, false, true);
687 	read_efuse(hw, 0, rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE], efuse);
688 	efuse_power_switch(hw, false, false);
689 }
690 
691 static void efuse_read_data_case1(struct ieee80211_hw *hw, u16 *efuse_addr,
692 				u8 efuse_data, u8 offset, u8 *tmpdata,
693 				u8 *readstate)
694 {
695 	bool dataempty = true;
696 	u8 hoffset;
697 	u8 tmpidx;
698 	u8 hworden;
699 	u8 word_cnts;
700 
701 	hoffset = (efuse_data >> 4) & 0x0F;
702 	hworden = efuse_data & 0x0F;
703 	word_cnts = efuse_calculate_word_cnts(hworden);
704 
705 	if (hoffset == offset) {
706 		for (tmpidx = 0; tmpidx < word_cnts * 2; tmpidx++) {
707 			if (efuse_one_byte_read(hw, *efuse_addr + 1 + tmpidx,
708 						&efuse_data)) {
709 				tmpdata[tmpidx] = efuse_data;
710 				if (efuse_data != 0xff)
711 					dataempty = false;
712 			}
713 		}
714 
715 		if (!dataempty) {
716 			*readstate = PG_STATE_DATA;
717 		} else {
718 			*efuse_addr = *efuse_addr + (word_cnts * 2) + 1;
719 			*readstate = PG_STATE_HEADER;
720 		}
721 
722 	} else {
723 		*efuse_addr = *efuse_addr + (word_cnts * 2) + 1;
724 		*readstate = PG_STATE_HEADER;
725 	}
726 }
727 
728 static int efuse_pg_packet_read(struct ieee80211_hw *hw, u8 offset, u8 *data)
729 {
730 	u8 readstate = PG_STATE_HEADER;
731 
732 	bool continual = true;
733 
734 	u8 efuse_data, word_cnts = 0;
735 	u16 efuse_addr = 0;
736 	u8 tmpdata[8];
737 
738 	if (data == NULL)
739 		return false;
740 	if (offset > 15)
741 		return false;
742 
743 	memset(data, 0xff, PGPKT_DATA_SIZE * sizeof(u8));
744 	memset(tmpdata, 0xff, PGPKT_DATA_SIZE * sizeof(u8));
745 
746 	while (continual && (efuse_addr < EFUSE_MAX_SIZE)) {
747 		if (readstate & PG_STATE_HEADER) {
748 			if (efuse_one_byte_read(hw, efuse_addr, &efuse_data)
749 			    && (efuse_data != 0xFF))
750 				efuse_read_data_case1(hw, &efuse_addr,
751 						      efuse_data, offset,
752 						      tmpdata, &readstate);
753 			else
754 				continual = false;
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;
759 		}
760 
761 	}
762 
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))
767 		return false;
768 	else
769 		return true;
770 
771 }
772 
773 static void efuse_write_data_case1(struct ieee80211_hw *hw, u16 *efuse_addr,
774 				   u8 efuse_data, u8 offset,
775 				   int *continual, u8 *write_state,
776 				   struct pgpkt_struct *target_pkt,
777 				   int *repeat_times, int *result, u8 word_en)
778 {
779 	struct rtl_priv *rtlpriv = rtl_priv(hw);
780 	struct pgpkt_struct tmp_pkt;
781 	int dataempty = true;
782 	u8 originaldata[8 * sizeof(u8)];
783 	u8 badworden = 0x0F;
784 	u8 match_word_en, tmp_word_en;
785 	u8 tmpindex;
786 	u8 tmp_header = efuse_data;
787 	u8 tmp_word_cnts;
788 
789 	tmp_pkt.offset = (tmp_header >> 4) & 0x0F;
790 	tmp_pkt.word_en = tmp_header & 0x0F;
791 	tmp_word_cnts = efuse_calculate_word_cnts(tmp_pkt.word_en);
792 
793 	if (tmp_pkt.offset != target_pkt->offset) {
794 		*efuse_addr = *efuse_addr + (tmp_word_cnts * 2) + 1;
795 		*write_state = PG_STATE_HEADER;
796 	} else {
797 		for (tmpindex = 0; tmpindex < (tmp_word_cnts * 2); tmpindex++) {
798 			if (efuse_one_byte_read(hw,
799 						(*efuse_addr + 1 + tmpindex),
800 						&efuse_data) &&
801 			    (efuse_data != 0xFF))
802 				dataempty = false;
803 		}
804 
805 		if (!dataempty) {
806 			*efuse_addr = *efuse_addr + (tmp_word_cnts * 2) + 1;
807 			*write_state = PG_STATE_HEADER;
808 		} else {
809 			match_word_en = 0x0F;
810 			if (!((target_pkt->word_en & BIT(0)) |
811 			    (tmp_pkt.word_en & BIT(0))))
812 				match_word_en &= (~BIT(0));
813 
814 			if (!((target_pkt->word_en & BIT(1)) |
815 			    (tmp_pkt.word_en & BIT(1))))
816 				match_word_en &= (~BIT(1));
817 
818 			if (!((target_pkt->word_en & BIT(2)) |
819 			    (tmp_pkt.word_en & BIT(2))))
820 				match_word_en &= (~BIT(2));
821 
822 			if (!((target_pkt->word_en & BIT(3)) |
823 			    (tmp_pkt.word_en & BIT(3))))
824 				match_word_en &= (~BIT(3));
825 
826 			if ((match_word_en & 0x0F) != 0x0F) {
827 				badworden =
828 				  enable_efuse_data_write(hw,
829 							  *efuse_addr + 1,
830 							  tmp_pkt.word_en,
831 							  target_pkt->data);
832 
833 				if (0x0F != (badworden & 0x0F))	{
834 					u8 reorg_offset = offset;
835 					u8 reorg_worden = badworden;
836 					efuse_pg_packet_write(hw, reorg_offset,
837 							      reorg_worden,
838 							      originaldata);
839 				}
840 
841 				tmp_word_en = 0x0F;
842 				if ((target_pkt->word_en & BIT(0)) ^
843 				    (match_word_en & BIT(0)))
844 					tmp_word_en &= (~BIT(0));
845 
846 				if ((target_pkt->word_en & BIT(1)) ^
847 				    (match_word_en & BIT(1)))
848 					tmp_word_en &= (~BIT(1));
849 
850 				if ((target_pkt->word_en & BIT(2)) ^
851 				    (match_word_en & BIT(2)))
852 					tmp_word_en &= (~BIT(2));
853 
854 				if ((target_pkt->word_en & BIT(3)) ^
855 				    (match_word_en & BIT(3)))
856 					tmp_word_en &= (~BIT(3));
857 
858 				if ((tmp_word_en & 0x0F) != 0x0F) {
859 					*efuse_addr = efuse_get_current_size(hw);
860 					target_pkt->offset = offset;
861 					target_pkt->word_en = tmp_word_en;
862 				} else {
863 					*continual = false;
864 				}
865 				*write_state = PG_STATE_HEADER;
866 				*repeat_times += 1;
867 				if (*repeat_times > EFUSE_REPEAT_THRESHOLD_) {
868 					*continual = false;
869 					*result = false;
870 				}
871 			} else {
872 				*efuse_addr += (2 * tmp_word_cnts) + 1;
873 				target_pkt->offset = offset;
874 				target_pkt->word_en = word_en;
875 				*write_state = PG_STATE_HEADER;
876 			}
877 		}
878 	}
879 	RTPRINT(rtlpriv, FEEPROM, EFUSE_PG, "efuse PG_STATE_HEADER-1\n");
880 }
881 
882 static void efuse_write_data_case2(struct ieee80211_hw *hw, u16 *efuse_addr,
883 				   int *continual, u8 *write_state,
884 				   struct pgpkt_struct target_pkt,
885 				   int *repeat_times, int *result)
886 {
887 	struct rtl_priv *rtlpriv = rtl_priv(hw);
888 	struct pgpkt_struct tmp_pkt;
889 	u8 pg_header;
890 	u8 tmp_header;
891 	u8 originaldata[8 * sizeof(u8)];
892 	u8 tmp_word_cnts;
893 	u8 badworden = 0x0F;
894 
895 	pg_header = ((target_pkt.offset << 4) & 0xf0) | target_pkt.word_en;
896 	efuse_one_byte_write(hw, *efuse_addr, pg_header);
897 	efuse_one_byte_read(hw, *efuse_addr, &tmp_header);
898 
899 	if (tmp_header == pg_header) {
900 		*write_state = PG_STATE_DATA;
901 	} else if (tmp_header == 0xFF) {
902 		*write_state = PG_STATE_HEADER;
903 		*repeat_times += 1;
904 		if (*repeat_times > EFUSE_REPEAT_THRESHOLD_) {
905 			*continual = false;
906 			*result = false;
907 		}
908 	} else {
909 		tmp_pkt.offset = (tmp_header >> 4) & 0x0F;
910 		tmp_pkt.word_en = tmp_header & 0x0F;
911 
912 		tmp_word_cnts = efuse_calculate_word_cnts(tmp_pkt.word_en);
913 
914 		memset(originaldata, 0xff,  8 * sizeof(u8));
915 
916 		if (efuse_pg_packet_read(hw, tmp_pkt.offset, originaldata)) {
917 			badworden = enable_efuse_data_write(hw,
918 							    *efuse_addr + 1,
919 							    tmp_pkt.word_en,
920 							    originaldata);
921 
922 			if (0x0F != (badworden & 0x0F)) {
923 				u8 reorg_offset = tmp_pkt.offset;
924 				u8 reorg_worden = badworden;
925 				efuse_pg_packet_write(hw, reorg_offset,
926 						      reorg_worden,
927 						      originaldata);
928 				*efuse_addr = efuse_get_current_size(hw);
929 			} else {
930 				*efuse_addr = *efuse_addr +
931 					      (tmp_word_cnts * 2) + 1;
932 			}
933 		} else {
934 			*efuse_addr = *efuse_addr + (tmp_word_cnts * 2) + 1;
935 		}
936 
937 		*write_state = PG_STATE_HEADER;
938 		*repeat_times += 1;
939 		if (*repeat_times > EFUSE_REPEAT_THRESHOLD_) {
940 			*continual = false;
941 			*result = false;
942 		}
943 
944 		RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
945 			"efuse PG_STATE_HEADER-2\n");
946 	}
947 }
948 
949 static int efuse_pg_packet_write(struct ieee80211_hw *hw,
950 				 u8 offset, u8 word_en, u8 *data)
951 {
952 	struct rtl_priv *rtlpriv = rtl_priv(hw);
953 	struct pgpkt_struct target_pkt;
954 	u8 write_state = PG_STATE_HEADER;
955 	int continual = true, dataempty = true, result = true;
956 	u16 efuse_addr = 0;
957 	u8 efuse_data;
958 	u8 target_word_cnts = 0;
959 	u8 badworden = 0x0F;
960 	static int repeat_times;
961 
962 	if (efuse_get_current_size(hw) >= (EFUSE_MAX_SIZE -
963 		rtlpriv->cfg->maps[EFUSE_OOB_PROTECT_BYTES_LEN])) {
964 		RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
965 			"efuse_pg_packet_write error\n");
966 		return false;
967 	}
968 
969 	target_pkt.offset = offset;
970 	target_pkt.word_en = word_en;
971 
972 	memset(target_pkt.data, 0xFF,  8 * sizeof(u8));
973 
974 	efuse_word_enable_data_read(word_en, data, target_pkt.data);
975 	target_word_cnts = efuse_calculate_word_cnts(target_pkt.word_en);
976 
977 	RTPRINT(rtlpriv, FEEPROM, EFUSE_PG, "efuse Power ON\n");
978 
979 	while (continual && (efuse_addr < (EFUSE_MAX_SIZE -
980 		rtlpriv->cfg->maps[EFUSE_OOB_PROTECT_BYTES_LEN]))) {
981 
982 		if (write_state == PG_STATE_HEADER) {
983 			dataempty = true;
984 			badworden = 0x0F;
985 			RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
986 				"efuse PG_STATE_HEADER\n");
987 
988 			if (efuse_one_byte_read(hw, efuse_addr, &efuse_data) &&
989 			    (efuse_data != 0xFF))
990 				efuse_write_data_case1(hw, &efuse_addr,
991 						       efuse_data, offset,
992 						       &continual,
993 						       &write_state,
994 						       &target_pkt,
995 						       &repeat_times, &result,
996 						       word_en);
997 			else
998 				efuse_write_data_case2(hw, &efuse_addr,
999 						       &continual,
1000 						       &write_state,
1001 						       target_pkt,
1002 						       &repeat_times,
1003 						       &result);
1004 
1005 		} else if (write_state == PG_STATE_DATA) {
1006 			RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
1007 				"efuse PG_STATE_DATA\n");
1008 			badworden = 0x0f;
1009 			badworden =
1010 			    enable_efuse_data_write(hw, efuse_addr + 1,
1011 						    target_pkt.word_en,
1012 						    target_pkt.data);
1013 
1014 			if ((badworden & 0x0F) == 0x0F) {
1015 				continual = false;
1016 			} else {
1017 				efuse_addr =
1018 				    efuse_addr + (2 * target_word_cnts) + 1;
1019 
1020 				target_pkt.offset = offset;
1021 				target_pkt.word_en = badworden;
1022 				target_word_cnts =
1023 				    efuse_calculate_word_cnts(target_pkt.
1024 							      word_en);
1025 				write_state = PG_STATE_HEADER;
1026 				repeat_times++;
1027 				if (repeat_times > EFUSE_REPEAT_THRESHOLD_) {
1028 					continual = false;
1029 					result = false;
1030 				}
1031 				RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
1032 					"efuse PG_STATE_HEADER-3\n");
1033 			}
1034 		}
1035 	}
1036 
1037 	if (efuse_addr >= (EFUSE_MAX_SIZE -
1038 		rtlpriv->cfg->maps[EFUSE_OOB_PROTECT_BYTES_LEN])) {
1039 		RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
1040 			 "efuse_addr(%#x) Out of size!!\n", efuse_addr);
1041 	}
1042 
1043 	return true;
1044 }
1045 
1046 static void efuse_word_enable_data_read(u8 word_en, u8 *sourdata,
1047 					u8 *targetdata)
1048 {
1049 	if (!(word_en & BIT(0))) {
1050 		targetdata[0] = sourdata[0];
1051 		targetdata[1] = sourdata[1];
1052 	}
1053 
1054 	if (!(word_en & BIT(1))) {
1055 		targetdata[2] = sourdata[2];
1056 		targetdata[3] = sourdata[3];
1057 	}
1058 
1059 	if (!(word_en & BIT(2))) {
1060 		targetdata[4] = sourdata[4];
1061 		targetdata[5] = sourdata[5];
1062 	}
1063 
1064 	if (!(word_en & BIT(3))) {
1065 		targetdata[6] = sourdata[6];
1066 		targetdata[7] = sourdata[7];
1067 	}
1068 }
1069 
1070 static u8 enable_efuse_data_write(struct ieee80211_hw *hw,
1071 				  u16 efuse_addr, u8 word_en, u8 *data)
1072 {
1073 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1074 	u16 tmpaddr;
1075 	u16 start_addr = efuse_addr;
1076 	u8 badworden = 0x0F;
1077 	u8 tmpdata[8];
1078 
1079 	memset(tmpdata, 0xff, PGPKT_DATA_SIZE);
1080 	RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
1081 		 "word_en = %x efuse_addr=%x\n", word_en, efuse_addr);
1082 
1083 	if (!(word_en & BIT(0))) {
1084 		tmpaddr = start_addr;
1085 		efuse_one_byte_write(hw, start_addr++, data[0]);
1086 		efuse_one_byte_write(hw, start_addr++, data[1]);
1087 
1088 		efuse_one_byte_read(hw, tmpaddr, &tmpdata[0]);
1089 		efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[1]);
1090 		if ((data[0] != tmpdata[0]) || (data[1] != tmpdata[1]))
1091 			badworden &= (~BIT(0));
1092 	}
1093 
1094 	if (!(word_en & BIT(1))) {
1095 		tmpaddr = start_addr;
1096 		efuse_one_byte_write(hw, start_addr++, data[2]);
1097 		efuse_one_byte_write(hw, start_addr++, data[3]);
1098 
1099 		efuse_one_byte_read(hw, tmpaddr, &tmpdata[2]);
1100 		efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[3]);
1101 		if ((data[2] != tmpdata[2]) || (data[3] != tmpdata[3]))
1102 			badworden &= (~BIT(1));
1103 	}
1104 
1105 	if (!(word_en & BIT(2))) {
1106 		tmpaddr = start_addr;
1107 		efuse_one_byte_write(hw, start_addr++, data[4]);
1108 		efuse_one_byte_write(hw, start_addr++, data[5]);
1109 
1110 		efuse_one_byte_read(hw, tmpaddr, &tmpdata[4]);
1111 		efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[5]);
1112 		if ((data[4] != tmpdata[4]) || (data[5] != tmpdata[5]))
1113 			badworden &= (~BIT(2));
1114 	}
1115 
1116 	if (!(word_en & BIT(3))) {
1117 		tmpaddr = start_addr;
1118 		efuse_one_byte_write(hw, start_addr++, data[6]);
1119 		efuse_one_byte_write(hw, start_addr++, data[7]);
1120 
1121 		efuse_one_byte_read(hw, tmpaddr, &tmpdata[6]);
1122 		efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[7]);
1123 		if ((data[6] != tmpdata[6]) || (data[7] != tmpdata[7]))
1124 			badworden &= (~BIT(3));
1125 	}
1126 
1127 	return badworden;
1128 }
1129 
1130 void efuse_power_switch(struct ieee80211_hw *hw, u8 write, u8 pwrstate)
1131 {
1132 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1133 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1134 	u8 tempval;
1135 	u16 tmpV16;
1136 
1137 	if (pwrstate && (rtlhal->hw_type != HARDWARE_TYPE_RTL8192SE)) {
1138 
1139 		if (rtlhal->hw_type != HARDWARE_TYPE_RTL8192CE &&
1140 		    rtlhal->hw_type != HARDWARE_TYPE_RTL8192DE) {
1141 			rtl_write_byte(rtlpriv,
1142 				       rtlpriv->cfg->maps[EFUSE_ACCESS], 0x69);
1143 		} else {
1144 			tmpV16 =
1145 			  rtl_read_word(rtlpriv,
1146 					rtlpriv->cfg->maps[SYS_ISO_CTRL]);
1147 			if (!(tmpV16 & rtlpriv->cfg->maps[EFUSE_PWC_EV12V])) {
1148 				tmpV16 |= rtlpriv->cfg->maps[EFUSE_PWC_EV12V];
1149 				rtl_write_word(rtlpriv,
1150 					       rtlpriv->cfg->maps[SYS_ISO_CTRL],
1151 					       tmpV16);
1152 			}
1153 		}
1154 		tmpV16 = rtl_read_word(rtlpriv,
1155 				       rtlpriv->cfg->maps[SYS_FUNC_EN]);
1156 		if (!(tmpV16 & rtlpriv->cfg->maps[EFUSE_FEN_ELDR])) {
1157 			tmpV16 |= rtlpriv->cfg->maps[EFUSE_FEN_ELDR];
1158 			rtl_write_word(rtlpriv,
1159 				       rtlpriv->cfg->maps[SYS_FUNC_EN], tmpV16);
1160 		}
1161 
1162 		tmpV16 = rtl_read_word(rtlpriv, rtlpriv->cfg->maps[SYS_CLK]);
1163 		if ((!(tmpV16 & rtlpriv->cfg->maps[EFUSE_LOADER_CLK_EN])) ||
1164 		    (!(tmpV16 & rtlpriv->cfg->maps[EFUSE_ANA8M]))) {
1165 			tmpV16 |= (rtlpriv->cfg->maps[EFUSE_LOADER_CLK_EN] |
1166 				   rtlpriv->cfg->maps[EFUSE_ANA8M]);
1167 			rtl_write_word(rtlpriv,
1168 				       rtlpriv->cfg->maps[SYS_CLK], tmpV16);
1169 		}
1170 	}
1171 
1172 	if (pwrstate) {
1173 		if (write) {
1174 			tempval = rtl_read_byte(rtlpriv,
1175 						rtlpriv->cfg->maps[EFUSE_TEST] +
1176 						3);
1177 
1178 			if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE) {
1179 				tempval &= ~(BIT(3) | BIT(4) | BIT(5) | BIT(6));
1180 				tempval |= (VOLTAGE_V25 << 3);
1181 			} else if (rtlhal->hw_type != HARDWARE_TYPE_RTL8192SE) {
1182 				tempval &= 0x0F;
1183 				tempval |= (VOLTAGE_V25 << 4);
1184 			}
1185 
1186 			rtl_write_byte(rtlpriv,
1187 				       rtlpriv->cfg->maps[EFUSE_TEST] + 3,
1188 				       (tempval | 0x80));
1189 		}
1190 
1191 		if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192SE) {
1192 			rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CLK],
1193 				       0x03);
1194 		}
1195 	} else {
1196 		if (rtlhal->hw_type != HARDWARE_TYPE_RTL8192CE &&
1197 		    rtlhal->hw_type != HARDWARE_TYPE_RTL8192DE)
1198 			rtl_write_byte(rtlpriv,
1199 				       rtlpriv->cfg->maps[EFUSE_ACCESS], 0);
1200 
1201 		if (write) {
1202 			tempval = rtl_read_byte(rtlpriv,
1203 						rtlpriv->cfg->maps[EFUSE_TEST] +
1204 						3);
1205 			rtl_write_byte(rtlpriv,
1206 				       rtlpriv->cfg->maps[EFUSE_TEST] + 3,
1207 				       (tempval & 0x7F));
1208 		}
1209 
1210 		if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192SE) {
1211 			rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CLK],
1212 				       0x02);
1213 		}
1214 	}
1215 }
1216 EXPORT_SYMBOL(efuse_power_switch);
1217 
1218 static u16 efuse_get_current_size(struct ieee80211_hw *hw)
1219 {
1220 	int continual = true;
1221 	u16 efuse_addr = 0;
1222 	u8 hoffset, hworden;
1223 	u8 efuse_data, word_cnts;
1224 
1225 	while (continual && efuse_one_byte_read(hw, efuse_addr, &efuse_data) &&
1226 	       (efuse_addr < EFUSE_MAX_SIZE)) {
1227 		if (efuse_data != 0xFF) {
1228 			hoffset = (efuse_data >> 4) & 0x0F;
1229 			hworden = efuse_data & 0x0F;
1230 			word_cnts = efuse_calculate_word_cnts(hworden);
1231 			efuse_addr = efuse_addr + (word_cnts * 2) + 1;
1232 		} else {
1233 			continual = false;
1234 		}
1235 	}
1236 
1237 	return efuse_addr;
1238 }
1239 
1240 static u8 efuse_calculate_word_cnts(u8 word_en)
1241 {
1242 	u8 word_cnts = 0;
1243 	if (!(word_en & BIT(0)))
1244 		word_cnts++;
1245 	if (!(word_en & BIT(1)))
1246 		word_cnts++;
1247 	if (!(word_en & BIT(2)))
1248 		word_cnts++;
1249 	if (!(word_en & BIT(3)))
1250 		word_cnts++;
1251 	return word_cnts;
1252 }
1253 
1254 int rtl_get_hwinfo(struct ieee80211_hw *hw, struct rtl_priv *rtlpriv,
1255 		   int max_size, u8 *hwinfo, int *params)
1256 {
1257 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1258 	struct rtl_pci_priv *rtlpcipriv = rtl_pcipriv(hw);
1259 	struct device *dev = &rtlpcipriv->dev.pdev->dev;
1260 	u16 eeprom_id;
1261 	u16 i, usvalue;
1262 
1263 	switch (rtlefuse->epromtype) {
1264 	case EEPROM_BOOT_EFUSE:
1265 		rtl_efuse_shadow_map_update(hw);
1266 		break;
1267 
1268 	case EEPROM_93C46:
1269 		pr_err("RTL8XXX did not boot from eeprom, check it !!\n");
1270 		return 1;
1271 
1272 	default:
1273 		dev_warn(dev, "no efuse data\n");
1274 		return 1;
1275 	}
1276 
1277 	memcpy(hwinfo, &rtlefuse->efuse_map[EFUSE_INIT_MAP][0], max_size);
1278 
1279 	RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG, "MAP",
1280 		      hwinfo, max_size);
1281 
1282 	eeprom_id = *((u16 *)&hwinfo[0]);
1283 	if (eeprom_id != params[0]) {
1284 		RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
1285 			 "EEPROM ID(%#x) is invalid!!\n", eeprom_id);
1286 		rtlefuse->autoload_failflag = true;
1287 	} else {
1288 		RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "Autoload OK\n");
1289 		rtlefuse->autoload_failflag = false;
1290 	}
1291 
1292 	if (rtlefuse->autoload_failflag)
1293 		return 1;
1294 
1295 	rtlefuse->eeprom_vid = *(u16 *)&hwinfo[params[1]];
1296 	rtlefuse->eeprom_did = *(u16 *)&hwinfo[params[2]];
1297 	rtlefuse->eeprom_svid = *(u16 *)&hwinfo[params[3]];
1298 	rtlefuse->eeprom_smid = *(u16 *)&hwinfo[params[4]];
1299 	RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1300 		 "EEPROMId = 0x%4x\n", eeprom_id);
1301 	RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1302 		 "EEPROM VID = 0x%4x\n", rtlefuse->eeprom_vid);
1303 	RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1304 		 "EEPROM DID = 0x%4x\n", rtlefuse->eeprom_did);
1305 	RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1306 		 "EEPROM SVID = 0x%4x\n", rtlefuse->eeprom_svid);
1307 	RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1308 		 "EEPROM SMID = 0x%4x\n", rtlefuse->eeprom_smid);
1309 
1310 	for (i = 0; i < 6; i += 2) {
1311 		usvalue = *(u16 *)&hwinfo[params[5] + i];
1312 		*((u16 *)(&rtlefuse->dev_addr[i])) = usvalue;
1313 	}
1314 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "%pM\n", rtlefuse->dev_addr);
1315 
1316 	rtlefuse->eeprom_channelplan = *&hwinfo[params[6]];
1317 	rtlefuse->eeprom_version = *(u16 *)&hwinfo[params[7]];
1318 	rtlefuse->txpwr_fromeprom = true;
1319 	rtlefuse->eeprom_oemid = *&hwinfo[params[8]];
1320 
1321 	RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1322 		 "EEPROM Customer ID: 0x%2x\n", rtlefuse->eeprom_oemid);
1323 
1324 	/* set channel plan to world wide 13 */
1325 	rtlefuse->channel_plan = params[9];
1326 
1327 	return 0;
1328 }
1329 EXPORT_SYMBOL_GPL(rtl_get_hwinfo);
1330 
1331 void rtl_fw_block_write(struct ieee80211_hw *hw, const u8 *buffer, u32 size)
1332 {
1333 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1334 	u8 *pu4byteptr = (u8 *)buffer;
1335 	u32 i;
1336 
1337 	for (i = 0; i < size; i++)
1338 		rtl_write_byte(rtlpriv, (START_ADDRESS + i), *(pu4byteptr + i));
1339 }
1340 EXPORT_SYMBOL_GPL(rtl_fw_block_write);
1341 
1342 void rtl_fw_page_write(struct ieee80211_hw *hw, u32 page, const u8 *buffer,
1343 		       u32 size)
1344 {
1345 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1346 	u8 value8;
1347 	u8 u8page = (u8)(page & 0x07);
1348 
1349 	value8 = (rtl_read_byte(rtlpriv, REG_MCUFWDL + 2) & 0xF8) | u8page;
1350 
1351 	rtl_write_byte(rtlpriv, (REG_MCUFWDL + 2), value8);
1352 	rtl_fw_block_write(hw, buffer, size);
1353 }
1354 EXPORT_SYMBOL_GPL(rtl_fw_page_write);
1355 
1356 void rtl_fill_dummy(u8 *pfwbuf, u32 *pfwlen)
1357 {
1358 	u32 fwlen = *pfwlen;
1359 	u8 remain = (u8)(fwlen % 4);
1360 
1361 	remain = (remain == 0) ? 0 : (4 - remain);
1362 
1363 	while (remain > 0) {
1364 		pfwbuf[fwlen] = 0;
1365 		fwlen++;
1366 		remain--;
1367 	}
1368 
1369 	*pfwlen = fwlen;
1370 }
1371 EXPORT_SYMBOL_GPL(rtl_fill_dummy);
1372 
1373 void rtl_efuse_ops_init(struct ieee80211_hw *hw)
1374 {
1375 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1376 
1377 	rtlpriv->efuse.efuse_ops = &efuse_ops;
1378 }
1379 EXPORT_SYMBOL_GPL(rtl_efuse_ops_init);
1380