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