1 /******************************************************************************
2  *
3  * Copyright(c) 2009-2010  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  * 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
17  *
18  * Tme full GNU General Public License is included in this distribution in the
19  * file called LICENSE.
20  *
21  * Contact Information:
22  * wlanfae <wlanfae@realtek.com>
23  * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
24  * Hsinchu 300, Taiwan.
25  *
26  * Larry Finger <Larry.Finger@lwfinger.net>
27  *
28  *****************************************************************************/
29 
30 #include "wifi.h"
31 #include "efuse.h"
32 
33 static const u8 MAX_PGPKT_SIZE = 9;
34 static const u8 PGPKT_DATA_SIZE = 8;
35 static const int EFUSE_MAX_SIZE = 512;
36 
37 static const u8 EFUSE_OOB_PROTECT_BYTES = 15;
38 
39 static const struct efuse_map RTL8712_SDIO_EFUSE_TABLE[] = {
40 	{0, 0, 0, 2},
41 	{0, 1, 0, 2},
42 	{0, 2, 0, 2},
43 	{1, 0, 0, 1},
44 	{1, 0, 1, 1},
45 	{1, 1, 0, 1},
46 	{1, 1, 1, 3},
47 	{1, 3, 0, 17},
48 	{3, 3, 1, 48},
49 	{10, 0, 0, 6},
50 	{10, 3, 0, 1},
51 	{10, 3, 1, 1},
52 	{11, 0, 0, 28}
53 };
54 
55 static void read_efuse_byte(struct ieee80211_hw *hw, u16 _offset,
56 					u8 *pbuf);
57 static void efuse_shadow_read_1byte(struct ieee80211_hw *hw, u16 offset,
58 				    u8 *value);
59 static void efuse_shadow_read_2byte(struct ieee80211_hw *hw, u16 offset,
60 				    u16 *value);
61 static void efuse_shadow_read_4byte(struct ieee80211_hw *hw, u16 offset,
62 				    u32 *value);
63 static void efuse_shadow_write_1byte(struct ieee80211_hw *hw, u16 offset,
64 				     u8 value);
65 static void efuse_shadow_write_2byte(struct ieee80211_hw *hw, u16 offset,
66 				     u16 value);
67 static void efuse_shadow_write_4byte(struct ieee80211_hw *hw, u16 offset,
68 				     u32 value);
69 static int efuse_one_byte_read(struct ieee80211_hw *hw, u16 addr,
70 					u8 *data);
71 static int efuse_one_byte_write(struct ieee80211_hw *hw, u16 addr,
72 					u8 data);
73 static void efuse_read_all_map(struct ieee80211_hw *hw, u8 *efuse);
74 static int efuse_pg_packet_read(struct ieee80211_hw *hw, u8 offset,
75 					u8 *data);
76 static int efuse_pg_packet_write(struct ieee80211_hw *hw, u8 offset,
77 				 u8 word_en, u8 *data);
78 static void efuse_word_enable_data_read(u8 word_en, u8 *sourdata,
79 					u8 *targetdata);
80 static u8 efuse_word_enable_data_write(struct ieee80211_hw *hw,
81 				       u16 efuse_addr, u8 word_en, u8 *data);
82 static void efuse_power_switch(struct ieee80211_hw *hw, u8 bwrite,
83 					u8 pwrstate);
84 static u16 efuse_get_current_size(struct ieee80211_hw *hw);
85 static u8 efuse_calculate_word_cnts(u8 word_en);
86 
efuse_initialize(struct ieee80211_hw * hw)87 void efuse_initialize(struct ieee80211_hw *hw)
88 {
89 	struct rtl_priv *rtlpriv = rtl_priv(hw);
90 	u8 bytetemp;
91 	u8 temp;
92 
93 	bytetemp = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[SYS_FUNC_EN] + 1);
94 	temp = bytetemp | 0x20;
95 	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[SYS_FUNC_EN] + 1, temp);
96 
97 	bytetemp = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[SYS_ISO_CTRL] + 1);
98 	temp = bytetemp & 0xFE;
99 	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[SYS_ISO_CTRL] + 1, temp);
100 
101 	bytetemp = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_TEST] + 3);
102 	temp = bytetemp | 0x80;
103 	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_TEST] + 3, temp);
104 
105 	rtl_write_byte(rtlpriv, 0x2F8, 0x3);
106 
107 	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3, 0x72);
108 
109 }
110 
efuse_read_1byte(struct ieee80211_hw * hw,u16 address)111 u8 efuse_read_1byte(struct ieee80211_hw *hw, u16 address)
112 {
113 	struct rtl_priv *rtlpriv = rtl_priv(hw);
114 	u8 data;
115 	u8 bytetemp;
116 	u8 temp;
117 	u32 k = 0;
118 
119 	if (address < EFUSE_REAL_CONTENT_LEN) {
120 		temp = address & 0xFF;
121 		rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1,
122 			       temp);
123 		bytetemp = rtl_read_byte(rtlpriv,
124 					 rtlpriv->cfg->maps[EFUSE_CTRL] + 2);
125 		temp = ((address >> 8) & 0x03) | (bytetemp & 0xFC);
126 		rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2,
127 			       temp);
128 
129 		bytetemp = rtl_read_byte(rtlpriv,
130 					 rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
131 		temp = bytetemp & 0x7F;
132 		rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3,
133 			       temp);
134 
135 		bytetemp = rtl_read_byte(rtlpriv,
136 					 rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
137 		while (!(bytetemp & 0x80)) {
138 			bytetemp = rtl_read_byte(rtlpriv,
139 						 rtlpriv->cfg->
140 						 maps[EFUSE_CTRL] + 3);
141 			k++;
142 			if (k == 1000) {
143 				k = 0;
144 				break;
145 			}
146 		}
147 		data = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL]);
148 		return data;
149 	} else
150 		return 0xFF;
151 
152 }
153 EXPORT_SYMBOL(efuse_read_1byte);
154 
efuse_write_1byte(struct ieee80211_hw * hw,u16 address,u8 value)155 void efuse_write_1byte(struct ieee80211_hw *hw, u16 address, u8 value)
156 {
157 	struct rtl_priv *rtlpriv = rtl_priv(hw);
158 	u8 bytetemp;
159 	u8 temp;
160 	u32 k = 0;
161 
162 	RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
163 		 ("Addr=%x Data =%x\n", address, value));
164 
165 	if (address < EFUSE_REAL_CONTENT_LEN) {
166 		rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL], value);
167 
168 		temp = address & 0xFF;
169 		rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1,
170 			       temp);
171 		bytetemp = rtl_read_byte(rtlpriv,
172 					 rtlpriv->cfg->maps[EFUSE_CTRL] + 2);
173 
174 		temp = ((address >> 8) & 0x03) | (bytetemp & 0xFC);
175 		rtl_write_byte(rtlpriv,
176 			       rtlpriv->cfg->maps[EFUSE_CTRL] + 2, temp);
177 
178 		bytetemp = rtl_read_byte(rtlpriv,
179 					 rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
180 		temp = bytetemp | 0x80;
181 		rtl_write_byte(rtlpriv,
182 			       rtlpriv->cfg->maps[EFUSE_CTRL] + 3, temp);
183 
184 		bytetemp = rtl_read_byte(rtlpriv,
185 					 rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
186 
187 		while (bytetemp & 0x80) {
188 			bytetemp = rtl_read_byte(rtlpriv,
189 						 rtlpriv->cfg->
190 						 maps[EFUSE_CTRL] + 3);
191 			k++;
192 			if (k == 100) {
193 				k = 0;
194 				break;
195 			}
196 		}
197 	}
198 
199 }
200 
read_efuse_byte(struct ieee80211_hw * hw,u16 _offset,u8 * pbuf)201 static void read_efuse_byte(struct ieee80211_hw *hw, u16 _offset, u8 *pbuf)
202 {
203 	struct rtl_priv *rtlpriv = rtl_priv(hw);
204 	u32 value32;
205 	u8 readbyte;
206 	u16 retry;
207 
208 	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1,
209 		       (_offset & 0xff));
210 	readbyte = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2);
211 	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2,
212 		       ((_offset >> 8) & 0x03) | (readbyte & 0xfc));
213 
214 	readbyte = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
215 	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3,
216 		       (readbyte & 0x7f));
217 
218 	retry = 0;
219 	value32 = rtl_read_dword(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL]);
220 	while (!(((value32 >> 24) & 0xff) & 0x80) && (retry < 10000)) {
221 		value32 = rtl_read_dword(rtlpriv,
222 					 rtlpriv->cfg->maps[EFUSE_CTRL]);
223 		retry++;
224 	}
225 
226 	udelay(50);
227 	value32 = rtl_read_dword(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL]);
228 
229 	*pbuf = (u8) (value32 & 0xff);
230 }
231 
read_efuse(struct ieee80211_hw * hw,u16 _offset,u16 _size_byte,u8 * pbuf)232 void read_efuse(struct ieee80211_hw *hw, u16 _offset, u16 _size_byte, u8 *pbuf)
233 {
234 	struct rtl_priv *rtlpriv = rtl_priv(hw);
235 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
236 	u8 efuse_tbl[EFUSE_MAP_LEN];
237 	u8 rtemp8[1];
238 	u16 efuse_addr = 0;
239 	u8 offset, wren;
240 	u16 i;
241 	u16 j;
242 	u16 efuse_word[EFUSE_MAX_SECTION][EFUSE_MAX_WORD_UNIT];
243 	u16 efuse_utilized = 0;
244 	u8 efuse_usage;
245 
246 	if ((_offset + _size_byte) > EFUSE_MAP_LEN) {
247 		RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
248 			 ("read_efuse(): Invalid offset(%#x) with read "
249 			  "bytes(%#x)!!\n", _offset, _size_byte));
250 		return;
251 	}
252 
253 	for (i = 0; i < EFUSE_MAX_SECTION; i++)
254 		for (j = 0; j < EFUSE_MAX_WORD_UNIT; j++)
255 			efuse_word[i][j] = 0xFFFF;
256 
257 	read_efuse_byte(hw, efuse_addr, rtemp8);
258 	if (*rtemp8 != 0xFF) {
259 		efuse_utilized++;
260 		RTPRINT(rtlpriv, FEEPROM, EFUSE_READ_ALL,
261 			("Addr=%d\n", efuse_addr));
262 		efuse_addr++;
263 	}
264 
265 	while ((*rtemp8 != 0xFF) && (efuse_addr < EFUSE_REAL_CONTENT_LEN)) {
266 		offset = ((*rtemp8 >> 4) & 0x0f);
267 
268 		if (offset < EFUSE_MAX_SECTION) {
269 			wren = (*rtemp8 & 0x0f);
270 			RTPRINT(rtlpriv, FEEPROM, EFUSE_READ_ALL,
271 				("offset-%d Worden=%x\n", offset, wren));
272 
273 			for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) {
274 				if (!(wren & 0x01)) {
275 					RTPRINT(rtlpriv, FEEPROM,
276 						EFUSE_READ_ALL, ("Addr=%d\n",
277 								 efuse_addr));
278 
279 					read_efuse_byte(hw, efuse_addr, rtemp8);
280 					efuse_addr++;
281 					efuse_utilized++;
282 					efuse_word[offset][i] = (*rtemp8 & 0xff);
283 
284 					if (efuse_addr >= EFUSE_REAL_CONTENT_LEN)
285 						break;
286 
287 					RTPRINT(rtlpriv, FEEPROM,
288 						EFUSE_READ_ALL, ("Addr=%d\n",
289 								 efuse_addr));
290 
291 					read_efuse_byte(hw, efuse_addr, rtemp8);
292 					efuse_addr++;
293 					efuse_utilized++;
294 					efuse_word[offset][i] |=
295 					    (((u16)*rtemp8 << 8) & 0xff00);
296 
297 					if (efuse_addr >= EFUSE_REAL_CONTENT_LEN)
298 						break;
299 				}
300 
301 				wren >>= 1;
302 			}
303 		}
304 
305 		RTPRINT(rtlpriv, FEEPROM, EFUSE_READ_ALL,
306 			("Addr=%d\n", efuse_addr));
307 		read_efuse_byte(hw, efuse_addr, rtemp8);
308 		if (*rtemp8 != 0xFF && (efuse_addr < 512)) {
309 			efuse_utilized++;
310 			efuse_addr++;
311 		}
312 	}
313 
314 	for (i = 0; i < EFUSE_MAX_SECTION; i++) {
315 		for (j = 0; j < EFUSE_MAX_WORD_UNIT; j++) {
316 			efuse_tbl[(i * 8) + (j * 2)] =
317 			    (efuse_word[i][j] & 0xff);
318 			efuse_tbl[(i * 8) + ((j * 2) + 1)] =
319 			    ((efuse_word[i][j] >> 8) & 0xff);
320 		}
321 	}
322 
323 	for (i = 0; i < _size_byte; i++)
324 		pbuf[i] = efuse_tbl[_offset + i];
325 
326 	rtlefuse->efuse_usedbytes = efuse_utilized;
327 	efuse_usage = (u8)((efuse_utilized * 100) / EFUSE_REAL_CONTENT_LEN);
328 	rtlefuse->efuse_usedpercentage = efuse_usage;
329 	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_EFUSE_BYTES,
330 				      (u8 *)&efuse_utilized);
331 	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_EFUSE_USAGE,
332 				      (u8 *)&efuse_usage);
333 }
334 
efuse_shadow_update_chk(struct ieee80211_hw * hw)335 bool efuse_shadow_update_chk(struct ieee80211_hw *hw)
336 {
337 	struct rtl_priv *rtlpriv = rtl_priv(hw);
338 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
339 	u8 section_idx, i, Base;
340 	u16 words_need = 0, hdr_num = 0, totalbytes, efuse_used;
341 	bool bwordchanged, bresult = true;
342 
343 	for (section_idx = 0; section_idx < 16; section_idx++) {
344 		Base = section_idx * 8;
345 		bwordchanged = false;
346 
347 		for (i = 0; i < 8; i = i + 2) {
348 			if ((rtlefuse->efuse_map[EFUSE_INIT_MAP][Base + i] !=
349 			     rtlefuse->efuse_map[EFUSE_MODIFY_MAP][Base + i]) ||
350 			    (rtlefuse->efuse_map[EFUSE_INIT_MAP][Base + i + 1] !=
351 			     rtlefuse->efuse_map[EFUSE_MODIFY_MAP][Base + i +
352 								   1])) {
353 				words_need++;
354 				bwordchanged = true;
355 			}
356 		}
357 
358 		if (bwordchanged == true)
359 			hdr_num++;
360 	}
361 
362 	totalbytes = hdr_num + words_need * 2;
363 	efuse_used = rtlefuse->efuse_usedbytes;
364 
365 	if ((totalbytes + efuse_used) >=
366 	    (EFUSE_MAX_SIZE - EFUSE_OOB_PROTECT_BYTES))
367 		bresult = false;
368 
369 	RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
370 		 ("efuse_shadow_update_chk(): totalbytes(%#x), "
371 		  "hdr_num(%#x), words_need(%#x), efuse_used(%d)\n",
372 		  totalbytes, hdr_num, words_need, efuse_used));
373 
374 	return bresult;
375 }
376 
efuse_shadow_read(struct ieee80211_hw * hw,u8 type,u16 offset,u32 * value)377 void efuse_shadow_read(struct ieee80211_hw *hw, u8 type,
378 		       u16 offset, u32 *value)
379 {
380 	if (type == 1)
381 		efuse_shadow_read_1byte(hw, offset, (u8 *) value);
382 	else if (type == 2)
383 		efuse_shadow_read_2byte(hw, offset, (u16 *) value);
384 	else if (type == 4)
385 		efuse_shadow_read_4byte(hw, offset, (u32 *) value);
386 
387 }
388 
efuse_shadow_write(struct ieee80211_hw * hw,u8 type,u16 offset,u32 value)389 void efuse_shadow_write(struct ieee80211_hw *hw, u8 type, u16 offset,
390 				u32 value)
391 {
392 	if (type == 1)
393 		efuse_shadow_write_1byte(hw, offset, (u8) value);
394 	else if (type == 2)
395 		efuse_shadow_write_2byte(hw, offset, (u16) value);
396 	else if (type == 4)
397 		efuse_shadow_write_4byte(hw, offset, (u32) value);
398 
399 }
400 
efuse_shadow_update(struct ieee80211_hw * hw)401 bool efuse_shadow_update(struct ieee80211_hw *hw)
402 {
403 	struct rtl_priv *rtlpriv = rtl_priv(hw);
404 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
405 	u16 i, offset, base;
406 	u8 word_en = 0x0F;
407 	u8 first_pg = false;
408 
409 	RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD, ("--->\n"));
410 
411 	if (!efuse_shadow_update_chk(hw)) {
412 		efuse_read_all_map(hw, &rtlefuse->efuse_map[EFUSE_INIT_MAP][0]);
413 		memcpy(&rtlefuse->efuse_map[EFUSE_MODIFY_MAP][0],
414 		       &rtlefuse->efuse_map[EFUSE_INIT_MAP][0],
415 		       rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]);
416 
417 		RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
418 			 ("<---efuse out of capacity!!\n"));
419 		return false;
420 	}
421 	efuse_power_switch(hw, true, true);
422 
423 	for (offset = 0; offset < 16; offset++) {
424 
425 		word_en = 0x0F;
426 		base = offset * 8;
427 
428 		for (i = 0; i < 8; i++) {
429 			if (first_pg == true) {
430 
431 				word_en &= ~(BIT(i / 2));
432 
433 				rtlefuse->efuse_map[EFUSE_INIT_MAP][base + i] =
434 				    rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base + i];
435 			} else {
436 
437 				if (rtlefuse->efuse_map[EFUSE_INIT_MAP][base + i] !=
438 				    rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base + i]) {
439 					word_en &= ~(BIT(i / 2));
440 
441 					rtlefuse->efuse_map[EFUSE_INIT_MAP][base + i] =
442 					    rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base + i];
443 				}
444 			}
445 		}
446 
447 		if (word_en != 0x0F) {
448 			u8 tmpdata[8];
449 			memcpy(tmpdata,
450 			       &rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base],
451 			       8);
452 			RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_LOUD,
453 				      ("U-efuse\n"), tmpdata, 8);
454 
455 			if (!efuse_pg_packet_write(hw, (u8) offset, word_en,
456 						   tmpdata)) {
457 				RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
458 					 ("PG section(%#x) fail!!\n", offset));
459 				break;
460 			}
461 		}
462 
463 	}
464 
465 	efuse_power_switch(hw, true, false);
466 	efuse_read_all_map(hw, &rtlefuse->efuse_map[EFUSE_INIT_MAP][0]);
467 
468 	memcpy(&rtlefuse->efuse_map[EFUSE_MODIFY_MAP][0],
469 	       &rtlefuse->efuse_map[EFUSE_INIT_MAP][0],
470 	       rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]);
471 
472 	RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD, ("<---\n"));
473 	return true;
474 }
475 
rtl_efuse_shadow_map_update(struct ieee80211_hw * hw)476 void rtl_efuse_shadow_map_update(struct ieee80211_hw *hw)
477 {
478 	struct rtl_priv *rtlpriv = rtl_priv(hw);
479 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
480 
481 	if (rtlefuse->autoload_failflag == true) {
482 		memset(&rtlefuse->efuse_map[EFUSE_INIT_MAP][0], 0xFF, 128);
483 	} else
484 		efuse_read_all_map(hw, &rtlefuse->efuse_map[EFUSE_INIT_MAP][0]);
485 
486 	memcpy(&rtlefuse->efuse_map[EFUSE_MODIFY_MAP][0],
487 	       &rtlefuse->efuse_map[EFUSE_INIT_MAP][0],
488 	       rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]);
489 
490 }
491 EXPORT_SYMBOL(rtl_efuse_shadow_map_update);
492 
efuse_force_write_vendor_Id(struct ieee80211_hw * hw)493 void efuse_force_write_vendor_Id(struct ieee80211_hw *hw)
494 {
495 	u8 tmpdata[8] = { 0xFF, 0xFF, 0xEC, 0x10, 0xFF, 0xFF, 0xFF, 0xFF };
496 
497 	efuse_power_switch(hw, true, true);
498 
499 	efuse_pg_packet_write(hw, 1, 0xD, tmpdata);
500 
501 	efuse_power_switch(hw, true, false);
502 
503 }
504 
efuse_re_pg_section(struct ieee80211_hw * hw,u8 section_idx)505 void efuse_re_pg_section(struct ieee80211_hw *hw, u8 section_idx)
506 {
507 }
508 
efuse_shadow_read_1byte(struct ieee80211_hw * hw,u16 offset,u8 * value)509 static void efuse_shadow_read_1byte(struct ieee80211_hw *hw,
510 				    u16 offset, u8 *value)
511 {
512 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
513 	*value = rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset];
514 }
515 
efuse_shadow_read_2byte(struct ieee80211_hw * hw,u16 offset,u16 * value)516 static void efuse_shadow_read_2byte(struct ieee80211_hw *hw,
517 				    u16 offset, u16 *value)
518 {
519 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
520 
521 	*value = rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset];
522 	*value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] << 8;
523 
524 }
525 
efuse_shadow_read_4byte(struct ieee80211_hw * hw,u16 offset,u32 * value)526 static void efuse_shadow_read_4byte(struct ieee80211_hw *hw,
527 				    u16 offset, u32 *value)
528 {
529 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
530 
531 	*value = rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset];
532 	*value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] << 8;
533 	*value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 2] << 16;
534 	*value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 3] << 24;
535 }
536 
efuse_shadow_write_1byte(struct ieee80211_hw * hw,u16 offset,u8 value)537 static void efuse_shadow_write_1byte(struct ieee80211_hw *hw,
538 				     u16 offset, u8 value)
539 {
540 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
541 
542 	rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset] = value;
543 }
544 
efuse_shadow_write_2byte(struct ieee80211_hw * hw,u16 offset,u16 value)545 static void efuse_shadow_write_2byte(struct ieee80211_hw *hw,
546 				     u16 offset, u16 value)
547 {
548 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
549 
550 	rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset] = value & 0x00FF;
551 	rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] = value >> 8;
552 
553 }
554 
efuse_shadow_write_4byte(struct ieee80211_hw * hw,u16 offset,u32 value)555 static void efuse_shadow_write_4byte(struct ieee80211_hw *hw,
556 				     u16 offset, u32 value)
557 {
558 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
559 
560 	rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset] =
561 	    (u8) (value & 0x000000FF);
562 	rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] =
563 	    (u8) ((value >> 8) & 0x0000FF);
564 	rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 2] =
565 	    (u8) ((value >> 16) & 0x00FF);
566 	rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 3] =
567 	    (u8) ((value >> 24) & 0xFF);
568 
569 }
570 
efuse_one_byte_read(struct ieee80211_hw * hw,u16 addr,u8 * data)571 static int efuse_one_byte_read(struct ieee80211_hw *hw, u16 addr, u8 *data)
572 {
573 	struct rtl_priv *rtlpriv = rtl_priv(hw);
574 	u8 tmpidx = 0;
575 	int bresult;
576 
577 	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1,
578 		       (u8) (addr & 0xff));
579 	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2,
580 		       ((u8) ((addr >> 8) & 0x03)) |
581 		       (rtl_read_byte(rtlpriv,
582 				      rtlpriv->cfg->maps[EFUSE_CTRL] + 2) &
583 			0xFC));
584 
585 	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3, 0x72);
586 
587 	while (!(0x80 & rtl_read_byte(rtlpriv,
588 				      rtlpriv->cfg->maps[EFUSE_CTRL] + 3))
589 	       && (tmpidx < 100)) {
590 		tmpidx++;
591 	}
592 
593 	if (tmpidx < 100) {
594 		*data = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL]);
595 		bresult = true;
596 	} else {
597 		*data = 0xff;
598 		bresult = false;
599 	}
600 	return bresult;
601 }
602 
efuse_one_byte_write(struct ieee80211_hw * hw,u16 addr,u8 data)603 static int efuse_one_byte_write(struct ieee80211_hw *hw, u16 addr, u8 data)
604 {
605 	struct rtl_priv *rtlpriv = rtl_priv(hw);
606 	u8 tmpidx = 0;
607 	bool bresult;
608 
609 	RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
610 		 ("Addr = %x Data=%x\n", addr, data));
611 
612 	rtl_write_byte(rtlpriv,
613 		       rtlpriv->cfg->maps[EFUSE_CTRL] + 1, (u8) (addr & 0xff));
614 	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2,
615 		       (rtl_read_byte(rtlpriv,
616 			 rtlpriv->cfg->maps[EFUSE_CTRL] +
617 			 2) & 0xFC) | (u8) ((addr >> 8) & 0x03));
618 
619 	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL], data);
620 	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3, 0xF2);
621 
622 	while ((0x80 & rtl_read_byte(rtlpriv,
623 				     rtlpriv->cfg->maps[EFUSE_CTRL] + 3))
624 	       && (tmpidx < 100)) {
625 		tmpidx++;
626 	}
627 
628 	if (tmpidx < 100)
629 		bresult = true;
630 	else
631 		bresult = false;
632 
633 	return bresult;
634 }
635 
efuse_read_all_map(struct ieee80211_hw * hw,u8 * efuse)636 static void efuse_read_all_map(struct ieee80211_hw *hw, u8 * efuse)
637 {
638 	efuse_power_switch(hw, false, true);
639 	read_efuse(hw, 0, 128, efuse);
640 	efuse_power_switch(hw, false, false);
641 }
642 
efuse_read_data_case1(struct ieee80211_hw * hw,u16 * efuse_addr,u8 efuse_data,u8 offset,u8 * tmpdata,u8 * readstate)643 static void efuse_read_data_case1(struct ieee80211_hw *hw, u16 *efuse_addr,
644 				u8 efuse_data, u8 offset, u8 *tmpdata,
645 				u8 *readstate)
646 {
647 	bool bdataempty = true;
648 	u8 hoffset;
649 	u8 tmpidx;
650 	u8 hworden;
651 	u8 word_cnts;
652 
653 	hoffset = (efuse_data >> 4) & 0x0F;
654 	hworden = efuse_data & 0x0F;
655 	word_cnts = efuse_calculate_word_cnts(hworden);
656 
657 	if (hoffset == offset) {
658 		for (tmpidx = 0; tmpidx < word_cnts * 2; tmpidx++) {
659 			if (efuse_one_byte_read(hw, *efuse_addr + 1 + tmpidx,
660 			    &efuse_data)) {
661 				tmpdata[tmpidx] = efuse_data;
662 				if (efuse_data != 0xff)
663 					bdataempty = true;
664 			}
665 		}
666 
667 		if (bdataempty == true)
668 			*readstate = PG_STATE_DATA;
669 		else {
670 			*efuse_addr = *efuse_addr + (word_cnts * 2) + 1;
671 			*readstate = PG_STATE_HEADER;
672 		}
673 
674 	} else {
675 		*efuse_addr = *efuse_addr + (word_cnts * 2) + 1;
676 		*readstate = PG_STATE_HEADER;
677 	}
678 }
679 
efuse_pg_packet_read(struct ieee80211_hw * hw,u8 offset,u8 * data)680 static int efuse_pg_packet_read(struct ieee80211_hw *hw, u8 offset, u8 *data)
681 {
682 	u8 readstate = PG_STATE_HEADER;
683 
684 	bool bcontinual = true;
685 
686 	u8 efuse_data, word_cnts = 0;
687 	u16 efuse_addr = 0;
688 	u8 hworden = 0;
689 	u8 tmpdata[8];
690 
691 	if (data == NULL)
692 		return false;
693 	if (offset > 15)
694 		return false;
695 
696 	memset(data, 0xff, PGPKT_DATA_SIZE * sizeof(u8));
697 	memset(tmpdata, 0xff, PGPKT_DATA_SIZE * sizeof(u8));
698 
699 	while (bcontinual && (efuse_addr < EFUSE_MAX_SIZE)) {
700 		if (readstate & PG_STATE_HEADER) {
701 			if (efuse_one_byte_read(hw, efuse_addr, &efuse_data)
702 			    && (efuse_data != 0xFF))
703 				efuse_read_data_case1(hw, &efuse_addr,
704 						      efuse_data,
705 						      offset, tmpdata,
706 						      &readstate);
707 			else
708 				bcontinual = false;
709 		} else if (readstate & PG_STATE_DATA) {
710 			efuse_word_enable_data_read(hworden, tmpdata, data);
711 			efuse_addr = efuse_addr + (word_cnts * 2) + 1;
712 			readstate = PG_STATE_HEADER;
713 		}
714 
715 	}
716 
717 	if ((data[0] == 0xff) && (data[1] == 0xff) &&
718 	    (data[2] == 0xff) && (data[3] == 0xff) &&
719 	    (data[4] == 0xff) && (data[5] == 0xff) &&
720 	    (data[6] == 0xff) && (data[7] == 0xff))
721 		return false;
722 	else
723 		return true;
724 
725 }
726 
efuse_write_data_case1(struct ieee80211_hw * hw,u16 * efuse_addr,u8 efuse_data,u8 offset,int * bcontinual,u8 * write_state,struct pgpkt_struct * target_pkt,int * repeat_times,int * bresult,u8 word_en)727 static void efuse_write_data_case1(struct ieee80211_hw *hw, u16 *efuse_addr,
728 			u8 efuse_data, u8 offset, int *bcontinual,
729 			u8 *write_state, struct pgpkt_struct *target_pkt,
730 			int *repeat_times, int *bresult, u8 word_en)
731 {
732 	struct rtl_priv *rtlpriv = rtl_priv(hw);
733 	struct pgpkt_struct tmp_pkt;
734 	int bdataempty = true;
735 	u8 originaldata[8 * sizeof(u8)];
736 	u8 badworden = 0x0F;
737 	u8 match_word_en, tmp_word_en;
738 	u8 tmpindex;
739 	u8 tmp_header = efuse_data;
740 	u8 tmp_word_cnts;
741 
742 	tmp_pkt.offset = (tmp_header >> 4) & 0x0F;
743 	tmp_pkt.word_en = tmp_header & 0x0F;
744 	tmp_word_cnts = efuse_calculate_word_cnts(tmp_pkt.word_en);
745 
746 	if (tmp_pkt.offset != target_pkt->offset) {
747 		*efuse_addr = *efuse_addr + (tmp_word_cnts * 2) + 1;
748 		*write_state = PG_STATE_HEADER;
749 	} else {
750 		for (tmpindex = 0; tmpindex < (tmp_word_cnts * 2); tmpindex++) {
751 			u16 address = *efuse_addr + 1 + tmpindex;
752 			if (efuse_one_byte_read(hw, address,
753 			     &efuse_data) && (efuse_data != 0xFF))
754 				bdataempty = false;
755 		}
756 
757 		if (bdataempty == false) {
758 			*efuse_addr = *efuse_addr + (tmp_word_cnts * 2) + 1;
759 			*write_state = PG_STATE_HEADER;
760 		} else {
761 			match_word_en = 0x0F;
762 			if (!((target_pkt->word_en & BIT(0)) |
763 			     (tmp_pkt.word_en & BIT(0))))
764 				match_word_en &= (~BIT(0));
765 
766 			if (!((target_pkt->word_en & BIT(1)) |
767 			     (tmp_pkt.word_en & BIT(1))))
768 				match_word_en &= (~BIT(1));
769 
770 			if (!((target_pkt->word_en & BIT(2)) |
771 			     (tmp_pkt.word_en & BIT(2))))
772 				match_word_en &= (~BIT(2));
773 
774 			if (!((target_pkt->word_en & BIT(3)) |
775 			     (tmp_pkt.word_en & BIT(3))))
776 				match_word_en &= (~BIT(3));
777 
778 			if ((match_word_en & 0x0F) != 0x0F) {
779 				badworden = efuse_word_enable_data_write(
780 							    hw, *efuse_addr + 1,
781 							    tmp_pkt.word_en,
782 							    target_pkt->data);
783 
784 				if (0x0F != (badworden & 0x0F)) {
785 					u8 reorg_offset = offset;
786 					u8 reorg_worden = badworden;
787 					efuse_pg_packet_write(hw, reorg_offset,
788 							       reorg_worden,
789 							       originaldata);
790 				}
791 
792 				tmp_word_en = 0x0F;
793 				if ((target_pkt->word_en & BIT(0)) ^
794 				    (match_word_en & BIT(0)))
795 					tmp_word_en &= (~BIT(0));
796 
797 				if ((target_pkt->word_en & BIT(1)) ^
798 				    (match_word_en & BIT(1)))
799 					tmp_word_en &= (~BIT(1));
800 
801 				if ((target_pkt->word_en & BIT(2)) ^
802 					(match_word_en & BIT(2)))
803 					tmp_word_en &= (~BIT(2));
804 
805 				if ((target_pkt->word_en & BIT(3)) ^
806 				    (match_word_en & BIT(3)))
807 					tmp_word_en &= (~BIT(3));
808 
809 				if ((tmp_word_en & 0x0F) != 0x0F) {
810 					*efuse_addr = efuse_get_current_size(hw);
811 					target_pkt->offset = offset;
812 					target_pkt->word_en = tmp_word_en;
813 				} else
814 					*bcontinual = false;
815 				*write_state = PG_STATE_HEADER;
816 				*repeat_times += 1;
817 				if (*repeat_times > EFUSE_REPEAT_THRESHOLD_) {
818 					*bcontinual = false;
819 					*bresult = false;
820 				}
821 			} else {
822 				*efuse_addr += (2 * tmp_word_cnts) + 1;
823 				target_pkt->offset = offset;
824 				target_pkt->word_en = word_en;
825 				*write_state = PG_STATE_HEADER;
826 			}
827 		}
828 	}
829 	RTPRINT(rtlpriv, FEEPROM, EFUSE_PG, ("efuse PG_STATE_HEADER-1\n"));
830 }
831 
efuse_write_data_case2(struct ieee80211_hw * hw,u16 * efuse_addr,int * bcontinual,u8 * write_state,struct pgpkt_struct target_pkt,int * repeat_times,int * bresult)832 static void efuse_write_data_case2(struct ieee80211_hw *hw, u16 *efuse_addr,
833 				   int *bcontinual, u8 *write_state,
834 				   struct pgpkt_struct target_pkt,
835 				   int *repeat_times, int *bresult)
836 {
837 	struct rtl_priv *rtlpriv = rtl_priv(hw);
838 	struct pgpkt_struct tmp_pkt;
839 	u8 pg_header;
840 	u8 tmp_header;
841 	u8 originaldata[8 * sizeof(u8)];
842 	u8 tmp_word_cnts;
843 	u8 badworden = 0x0F;
844 
845 	pg_header = ((target_pkt.offset << 4) & 0xf0) | target_pkt.word_en;
846 	efuse_one_byte_write(hw, *efuse_addr, pg_header);
847 	efuse_one_byte_read(hw, *efuse_addr, &tmp_header);
848 
849 	if (tmp_header == pg_header)
850 		*write_state = PG_STATE_DATA;
851 	else if (tmp_header == 0xFF) {
852 		*write_state = PG_STATE_HEADER;
853 		*repeat_times += 1;
854 		if (*repeat_times > EFUSE_REPEAT_THRESHOLD_) {
855 			*bcontinual = false;
856 			*bresult = false;
857 		}
858 	} else {
859 		tmp_pkt.offset = (tmp_header >> 4) & 0x0F;
860 		tmp_pkt.word_en = tmp_header & 0x0F;
861 
862 		tmp_word_cnts = efuse_calculate_word_cnts(tmp_pkt.word_en);
863 
864 		memset(originaldata, 0xff, 8 * sizeof(u8));
865 
866 		if (efuse_pg_packet_read(hw, tmp_pkt.offset, originaldata)) {
867 			badworden = efuse_word_enable_data_write(hw,
868 				    *efuse_addr + 1, tmp_pkt.word_en,
869 				    originaldata);
870 
871 			if (0x0F != (badworden & 0x0F)) {
872 				u8 reorg_offset = tmp_pkt.offset;
873 				u8 reorg_worden = badworden;
874 				efuse_pg_packet_write(hw, reorg_offset,
875 						      reorg_worden,
876 						      originaldata);
877 				*efuse_addr = efuse_get_current_size(hw);
878 			 } else
879 				*efuse_addr = *efuse_addr + (tmp_word_cnts * 2)
880 					      + 1;
881 		} else
882 			*efuse_addr = *efuse_addr + (tmp_word_cnts * 2) + 1;
883 
884 		*write_state = PG_STATE_HEADER;
885 		*repeat_times += 1;
886 		if (*repeat_times > EFUSE_REPEAT_THRESHOLD_) {
887 			*bcontinual = false;
888 			*bresult = false;
889 		}
890 
891 		RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
892 			("efuse PG_STATE_HEADER-2\n"));
893 	}
894 }
895 
efuse_pg_packet_write(struct ieee80211_hw * hw,u8 offset,u8 word_en,u8 * data)896 static int efuse_pg_packet_write(struct ieee80211_hw *hw,
897 				 u8 offset, u8 word_en, u8 *data)
898 {
899 	struct rtl_priv *rtlpriv = rtl_priv(hw);
900 	struct pgpkt_struct target_pkt;
901 	u8 write_state = PG_STATE_HEADER;
902 	int bcontinual = true, bdataempty = true, bresult = true;
903 	u16 efuse_addr = 0;
904 	u8 efuse_data;
905 	u8 target_word_cnts = 0;
906 	u8 badworden = 0x0F;
907 	static int repeat_times;
908 
909 	if (efuse_get_current_size(hw) >=
910 	    (EFUSE_MAX_SIZE - EFUSE_OOB_PROTECT_BYTES)) {
911 		RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
912 			("efuse_pg_packet_write error\n"));
913 		return false;
914 	}
915 
916 	target_pkt.offset = offset;
917 	target_pkt.word_en = word_en;
918 
919 	memset(target_pkt.data, 0xFF, 8 * sizeof(u8));
920 
921 	efuse_word_enable_data_read(word_en, data, target_pkt.data);
922 	target_word_cnts = efuse_calculate_word_cnts(target_pkt.word_en);
923 
924 	RTPRINT(rtlpriv, FEEPROM, EFUSE_PG, ("efuse Power ON\n"));
925 
926 	while (bcontinual && (efuse_addr <
927 	       (EFUSE_MAX_SIZE - EFUSE_OOB_PROTECT_BYTES))) {
928 
929 		if (write_state == PG_STATE_HEADER) {
930 			bdataempty = true;
931 			badworden = 0x0F;
932 			RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
933 				("efuse PG_STATE_HEADER\n"));
934 
935 			if (efuse_one_byte_read(hw, efuse_addr, &efuse_data) &&
936 			    (efuse_data != 0xFF))
937 				efuse_write_data_case1(hw, &efuse_addr,
938 						       efuse_data, offset,
939 						       &bcontinual,
940 						       &write_state, &target_pkt,
941 						       &repeat_times, &bresult,
942 						       word_en);
943 			else
944 				efuse_write_data_case2(hw, &efuse_addr,
945 						       &bcontinual,
946 						       &write_state,
947 						       target_pkt,
948 						       &repeat_times,
949 						       &bresult);
950 
951 		} else if (write_state == PG_STATE_DATA) {
952 			RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
953 				("efuse PG_STATE_DATA\n"));
954 			badworden = 0x0f;
955 			badworden =
956 			    efuse_word_enable_data_write(hw, efuse_addr + 1,
957 							 target_pkt.word_en,
958 							 target_pkt.data);
959 
960 			if ((badworden & 0x0F) == 0x0F) {
961 				bcontinual = false;
962 			} else {
963 				efuse_addr =
964 				    efuse_addr + (2 * target_word_cnts) + 1;
965 
966 				target_pkt.offset = offset;
967 				target_pkt.word_en = badworden;
968 				target_word_cnts =
969 				    efuse_calculate_word_cnts(target_pkt.
970 							      word_en);
971 				write_state = PG_STATE_HEADER;
972 				repeat_times++;
973 				if (repeat_times > EFUSE_REPEAT_THRESHOLD_) {
974 					bcontinual = false;
975 					bresult = false;
976 				}
977 				RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
978 					("efuse PG_STATE_HEADER-3\n"));
979 			}
980 		}
981 	}
982 
983 	if (efuse_addr >= (EFUSE_MAX_SIZE - EFUSE_OOB_PROTECT_BYTES)) {
984 		RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
985 			 ("efuse_addr(%#x) Out of size!!\n", efuse_addr));
986 	}
987 
988 	return true;
989 }
990 
efuse_word_enable_data_read(u8 word_en,u8 * sourdata,u8 * targetdata)991 static void efuse_word_enable_data_read(u8 word_en,
992 					u8 *sourdata, u8 *targetdata)
993 {
994 	if (!(word_en & BIT(0))) {
995 		targetdata[0] = sourdata[0];
996 		targetdata[1] = sourdata[1];
997 	}
998 
999 	if (!(word_en & BIT(1))) {
1000 		targetdata[2] = sourdata[2];
1001 		targetdata[3] = sourdata[3];
1002 	}
1003 
1004 	if (!(word_en & BIT(2))) {
1005 		targetdata[4] = sourdata[4];
1006 		targetdata[5] = sourdata[5];
1007 	}
1008 
1009 	if (!(word_en & BIT(3))) {
1010 		targetdata[6] = sourdata[6];
1011 		targetdata[7] = sourdata[7];
1012 	}
1013 }
1014 
efuse_word_enable_data_write(struct ieee80211_hw * hw,u16 efuse_addr,u8 word_en,u8 * data)1015 static u8 efuse_word_enable_data_write(struct ieee80211_hw *hw,
1016 				       u16 efuse_addr, u8 word_en, u8 *data)
1017 {
1018 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1019 	u16 tmpaddr;
1020 	u16 start_addr = efuse_addr;
1021 	u8 badworden = 0x0F;
1022 	u8 tmpdata[8];
1023 
1024 	memset(tmpdata, 0xff, PGPKT_DATA_SIZE);
1025 	RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
1026 		 ("word_en = %x efuse_addr=%x\n", word_en, efuse_addr));
1027 
1028 	if (!(word_en & BIT(0))) {
1029 		tmpaddr = start_addr;
1030 		efuse_one_byte_write(hw, start_addr++, data[0]);
1031 		efuse_one_byte_write(hw, start_addr++, data[1]);
1032 
1033 		efuse_one_byte_read(hw, tmpaddr, &tmpdata[0]);
1034 		efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[1]);
1035 		if ((data[0] != tmpdata[0]) || (data[1] != tmpdata[1]))
1036 			badworden &= (~BIT(0));
1037 	}
1038 
1039 	if (!(word_en & BIT(1))) {
1040 		tmpaddr = start_addr;
1041 		efuse_one_byte_write(hw, start_addr++, data[2]);
1042 		efuse_one_byte_write(hw, start_addr++, data[3]);
1043 
1044 		efuse_one_byte_read(hw, tmpaddr, &tmpdata[2]);
1045 		efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[3]);
1046 		if ((data[2] != tmpdata[2]) || (data[3] != tmpdata[3]))
1047 			badworden &= (~BIT(1));
1048 	}
1049 
1050 	if (!(word_en & BIT(2))) {
1051 		tmpaddr = start_addr;
1052 		efuse_one_byte_write(hw, start_addr++, data[4]);
1053 		efuse_one_byte_write(hw, start_addr++, data[5]);
1054 
1055 		efuse_one_byte_read(hw, tmpaddr, &tmpdata[4]);
1056 		efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[5]);
1057 		if ((data[4] != tmpdata[4]) || (data[5] != tmpdata[5]))
1058 			badworden &= (~BIT(2));
1059 	}
1060 
1061 	if (!(word_en & BIT(3))) {
1062 		tmpaddr = start_addr;
1063 		efuse_one_byte_write(hw, start_addr++, data[6]);
1064 		efuse_one_byte_write(hw, start_addr++, data[7]);
1065 
1066 		efuse_one_byte_read(hw, tmpaddr, &tmpdata[6]);
1067 		efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[7]);
1068 		if ((data[6] != tmpdata[6]) || (data[7] != tmpdata[7]))
1069 			badworden &= (~BIT(3));
1070 	}
1071 
1072 	return badworden;
1073 }
1074 
efuse_power_switch(struct ieee80211_hw * hw,u8 bwrite,u8 pwrstate)1075 static void efuse_power_switch(struct ieee80211_hw *hw, u8 bwrite, u8 pwrstate)
1076 {
1077 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1078 	u8 tempval;
1079 	u16 tmpV16;
1080 
1081 	if (pwrstate == true) {
1082 		tmpV16 = rtl_read_word(rtlpriv,
1083 				       rtlpriv->cfg->maps[SYS_ISO_CTRL]);
1084 		if (!(tmpV16 & rtlpriv->cfg->maps[EFUSE_PWC_EV12V])) {
1085 			tmpV16 |= rtlpriv->cfg->maps[EFUSE_PWC_EV12V];
1086 			rtl_write_word(rtlpriv,
1087 				       rtlpriv->cfg->maps[SYS_ISO_CTRL],
1088 				       tmpV16);
1089 		}
1090 
1091 		tmpV16 = rtl_read_word(rtlpriv,
1092 				       rtlpriv->cfg->maps[SYS_FUNC_EN]);
1093 		if (!(tmpV16 & rtlpriv->cfg->maps[EFUSE_FEN_ELDR])) {
1094 			tmpV16 |= rtlpriv->cfg->maps[EFUSE_FEN_ELDR];
1095 			rtl_write_word(rtlpriv,
1096 				       rtlpriv->cfg->maps[SYS_FUNC_EN], tmpV16);
1097 		}
1098 
1099 		tmpV16 = rtl_read_word(rtlpriv, rtlpriv->cfg->maps[SYS_CLK]);
1100 		if ((!(tmpV16 & rtlpriv->cfg->maps[EFUSE_LOADER_CLK_EN])) ||
1101 		    (!(tmpV16 & rtlpriv->cfg->maps[EFUSE_ANA8M]))) {
1102 			tmpV16 |= (rtlpriv->cfg->maps[EFUSE_LOADER_CLK_EN] |
1103 				   rtlpriv->cfg->maps[EFUSE_ANA8M]);
1104 			rtl_write_word(rtlpriv,
1105 				       rtlpriv->cfg->maps[SYS_CLK], tmpV16);
1106 		}
1107 	}
1108 
1109 	if (pwrstate == true) {
1110 		if (bwrite == true) {
1111 			tempval = rtl_read_byte(rtlpriv,
1112 						rtlpriv->cfg->maps[EFUSE_TEST] +
1113 						3);
1114 			tempval &= 0x0F;
1115 			tempval |= (VOLTAGE_V25 << 4);
1116 			rtl_write_byte(rtlpriv,
1117 				       rtlpriv->cfg->maps[EFUSE_TEST] + 3,
1118 				       (tempval | 0x80));
1119 		}
1120 
1121 	} else {
1122 		if (bwrite == true) {
1123 			tempval = rtl_read_byte(rtlpriv,
1124 						rtlpriv->cfg->maps[EFUSE_TEST] +
1125 						3);
1126 			rtl_write_byte(rtlpriv,
1127 				       rtlpriv->cfg->maps[EFUSE_TEST] + 3,
1128 				       (tempval & 0x7F));
1129 		}
1130 
1131 	}
1132 
1133 }
1134 
efuse_get_current_size(struct ieee80211_hw * hw)1135 static u16 efuse_get_current_size(struct ieee80211_hw *hw)
1136 {
1137 	int bcontinual = true;
1138 	u16 efuse_addr = 0;
1139 	u8 hoffset, hworden;
1140 	u8 efuse_data, word_cnts;
1141 
1142 	while (bcontinual && efuse_one_byte_read(hw, efuse_addr, &efuse_data)
1143 	       && (efuse_addr < EFUSE_MAX_SIZE)) {
1144 		if (efuse_data != 0xFF) {
1145 			hoffset = (efuse_data >> 4) & 0x0F;
1146 			hworden = efuse_data & 0x0F;
1147 			word_cnts = efuse_calculate_word_cnts(hworden);
1148 			efuse_addr = efuse_addr + (word_cnts * 2) + 1;
1149 		} else {
1150 			bcontinual = false;
1151 		}
1152 	}
1153 
1154 	return efuse_addr;
1155 }
1156 
efuse_calculate_word_cnts(u8 word_en)1157 static u8 efuse_calculate_word_cnts(u8 word_en)
1158 {
1159 	u8 word_cnts = 0;
1160 	if (!(word_en & BIT(0)))
1161 		word_cnts++;
1162 	if (!(word_en & BIT(1)))
1163 		word_cnts++;
1164 	if (!(word_en & BIT(2)))
1165 		word_cnts++;
1166 	if (!(word_en & BIT(3)))
1167 		word_cnts++;
1168 	return word_cnts;
1169 }
1170 
1171