efuse.c 33.2 KB
Newer Older
L
Larry Finger 已提交
1 2
/******************************************************************************
 *
L
Larry Finger 已提交
3
 * Copyright(c) 2009-2012  Realtek Corporation.
L
Larry Finger 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * Tmis program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * Tmis program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * Tme full GNU General Public License is included in this distribution in the
 * file called LICENSE.
 *
 * Contact Information:
 * wlanfae <wlanfae@realtek.com>
 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
 * Hsinchu 300, Taiwan.
 *
 * Larry Finger <Larry.Finger@lwfinger.net>
 *
 *****************************************************************************/
#include "wifi.h"
#include "efuse.h"
27
#include <linux/export.h>
L
Larry Finger 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

static const u8 MAX_PGPKT_SIZE = 9;
static const u8 PGPKT_DATA_SIZE = 8;
static const int EFUSE_MAX_SIZE = 512;

static const struct efuse_map RTL8712_SDIO_EFUSE_TABLE[] = {
	{0, 0, 0, 2},
	{0, 1, 0, 2},
	{0, 2, 0, 2},
	{1, 0, 0, 1},
	{1, 0, 1, 1},
	{1, 1, 0, 1},
	{1, 1, 1, 3},
	{1, 3, 0, 17},
	{3, 3, 1, 48},
	{10, 0, 0, 6},
	{10, 3, 0, 1},
	{10, 3, 1, 1},
	{11, 0, 0, 28}
};

static void efuse_shadow_read_1byte(struct ieee80211_hw *hw, u16 offset,
				    u8 *value);
static void efuse_shadow_read_2byte(struct ieee80211_hw *hw, u16 offset,
				    u16 *value);
static void efuse_shadow_read_4byte(struct ieee80211_hw *hw, u16 offset,
				    u32 *value);
static void efuse_shadow_write_1byte(struct ieee80211_hw *hw, u16 offset,
				     u8 value);
static void efuse_shadow_write_2byte(struct ieee80211_hw *hw, u16 offset,
				     u16 value);
static void efuse_shadow_write_4byte(struct ieee80211_hw *hw, u16 offset,
				     u32 value);
static int efuse_one_byte_write(struct ieee80211_hw *hw, u16 addr,
62
				u8 data);
L
Larry Finger 已提交
63 64
static void efuse_read_all_map(struct ieee80211_hw *hw, u8 *efuse);
static int efuse_pg_packet_read(struct ieee80211_hw *hw, u8 offset,
65
				u8 *data);
L
Larry Finger 已提交
66 67 68 69
static int efuse_pg_packet_write(struct ieee80211_hw *hw, u8 offset,
				 u8 word_en, u8 *data);
static void efuse_word_enable_data_read(u8 word_en, u8 *sourdata,
					u8 *targetdata);
70 71
static u8 enable_efuse_data_write(struct ieee80211_hw *hw,
				  u16 efuse_addr, u8 word_en, u8 *data);
72
static void efuse_power_switch(struct ieee80211_hw *hw, u8 write,
73
			       u8 pwrstate);
L
Larry Finger 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
static u16 efuse_get_current_size(struct ieee80211_hw *hw);
static u8 efuse_calculate_word_cnts(u8 word_en);

void efuse_initialize(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u8 bytetemp;
	u8 temp;

	bytetemp = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[SYS_FUNC_EN] + 1);
	temp = bytetemp | 0x20;
	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[SYS_FUNC_EN] + 1, temp);

	bytetemp = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[SYS_ISO_CTRL] + 1);
	temp = bytetemp & 0xFE;
	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[SYS_ISO_CTRL] + 1, temp);

	bytetemp = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_TEST] + 3);
	temp = bytetemp | 0x80;
	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_TEST] + 3, temp);

	rtl_write_byte(rtlpriv, 0x2F8, 0x3);

	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3, 0x72);

}

u8 efuse_read_1byte(struct ieee80211_hw *hw, u16 address)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u8 data;
	u8 bytetemp;
	u8 temp;
	u32 k = 0;
108 109
	const u32 efuse_len =
		rtlpriv->cfg->maps[EFUSE_REAL_CONTENT_SIZE];
L
Larry Finger 已提交
110

111
	if (address < efuse_len) {
L
Larry Finger 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
		temp = address & 0xFF;
		rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1,
			       temp);
		bytetemp = rtl_read_byte(rtlpriv,
					 rtlpriv->cfg->maps[EFUSE_CTRL] + 2);
		temp = ((address >> 8) & 0x03) | (bytetemp & 0xFC);
		rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2,
			       temp);

		bytetemp = rtl_read_byte(rtlpriv,
					 rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
		temp = bytetemp & 0x7F;
		rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3,
			       temp);

		bytetemp = rtl_read_byte(rtlpriv,
					 rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
		while (!(bytetemp & 0x80)) {
			bytetemp = rtl_read_byte(rtlpriv,
						 rtlpriv->cfg->
						 maps[EFUSE_CTRL] + 3);
			k++;
			if (k == 1000) {
				k = 0;
				break;
			}
		}
		data = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL]);
		return data;
	} else
		return 0xFF;

}
EXPORT_SYMBOL(efuse_read_1byte);

void efuse_write_1byte(struct ieee80211_hw *hw, u16 address, u8 value)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u8 bytetemp;
	u8 temp;
	u32 k = 0;
153 154
	const u32 efuse_len =
		rtlpriv->cfg->maps[EFUSE_REAL_CONTENT_SIZE];
L
Larry Finger 已提交
155

156 157
	RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD, "Addr=%x Data =%x\n",
		 address, value);
L
Larry Finger 已提交
158

159
	if (address < efuse_len) {
L
Larry Finger 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
		rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL], value);

		temp = address & 0xFF;
		rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1,
			       temp);
		bytetemp = rtl_read_byte(rtlpriv,
					 rtlpriv->cfg->maps[EFUSE_CTRL] + 2);

		temp = ((address >> 8) & 0x03) | (bytetemp & 0xFC);
		rtl_write_byte(rtlpriv,
			       rtlpriv->cfg->maps[EFUSE_CTRL] + 2, temp);

		bytetemp = rtl_read_byte(rtlpriv,
					 rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
		temp = bytetemp | 0x80;
		rtl_write_byte(rtlpriv,
			       rtlpriv->cfg->maps[EFUSE_CTRL] + 3, temp);

		bytetemp = rtl_read_byte(rtlpriv,
					 rtlpriv->cfg->maps[EFUSE_CTRL] + 3);

		while (bytetemp & 0x80) {
			bytetemp = rtl_read_byte(rtlpriv,
						 rtlpriv->cfg->
						 maps[EFUSE_CTRL] + 3);
			k++;
			if (k == 100) {
				k = 0;
				break;
			}
		}
	}

}

195
void read_efuse_byte(struct ieee80211_hw *hw, u16 _offset, u8 *pbuf)
L
Larry Finger 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u32 value32;
	u8 readbyte;
	u16 retry;

	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1,
		       (_offset & 0xff));
	readbyte = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2);
	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2,
		       ((_offset >> 8) & 0x03) | (readbyte & 0xfc));

	readbyte = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3,
		       (readbyte & 0x7f));

	retry = 0;
	value32 = rtl_read_dword(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL]);
	while (!(((value32 >> 24) & 0xff) & 0x80) && (retry < 10000)) {
		value32 = rtl_read_dword(rtlpriv,
					 rtlpriv->cfg->maps[EFUSE_CTRL]);
		retry++;
	}

	udelay(50);
	value32 = rtl_read_dword(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL]);

	*pbuf = (u8) (value32 & 0xff);
}
225
EXPORT_SYMBOL_GPL(read_efuse_byte);
L
Larry Finger 已提交
226 227 228 229 230

void read_efuse(struct ieee80211_hw *hw, u16 _offset, u16 _size_byte, u8 *pbuf)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
231
	u8 *efuse_tbl;
L
Larry Finger 已提交
232 233 234
	u8 rtemp8[1];
	u16 efuse_addr = 0;
	u8 offset, wren;
235
	u8 u1temp = 0;
L
Larry Finger 已提交
236 237
	u16 i;
	u16 j;
238 239 240 241
	const u16 efuse_max_section =
		rtlpriv->cfg->maps[EFUSE_MAX_SECTION_MAP];
	const u32 efuse_len =
		rtlpriv->cfg->maps[EFUSE_REAL_CONTENT_SIZE];
242
	u16 **efuse_word;
L
Larry Finger 已提交
243 244 245
	u16 efuse_utilized = 0;
	u8 efuse_usage;

246
	if ((_offset + _size_byte) > rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]) {
L
Larry Finger 已提交
247
		RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
248 249
			 "read_efuse(): Invalid offset(%#x) with read bytes(%#x)!!\n",
			 _offset, _size_byte);
L
Larry Finger 已提交
250 251 252
		return;
	}

253 254 255 256 257
	/* allocate memory for efuse_tbl and efuse_word */
	efuse_tbl = kmalloc(rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE] *
			    sizeof(u8), GFP_ATOMIC);
	if (!efuse_tbl)
		return;
258
	efuse_word = kzalloc(EFUSE_MAX_WORD_UNIT * sizeof(u16 *), GFP_ATOMIC);
259
	if (!efuse_word)
260
		goto out;
261 262 263 264 265 266 267
	for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) {
		efuse_word[i] = kmalloc(efuse_max_section * sizeof(u16),
					GFP_ATOMIC);
		if (!efuse_word[i])
			goto done;
	}

268
	for (i = 0; i < efuse_max_section; i++)
L
Larry Finger 已提交
269
		for (j = 0; j < EFUSE_MAX_WORD_UNIT; j++)
270
			efuse_word[j][i] = 0xFFFF;
L
Larry Finger 已提交
271 272 273 274 275

	read_efuse_byte(hw, efuse_addr, rtemp8);
	if (*rtemp8 != 0xFF) {
		efuse_utilized++;
		RTPRINT(rtlpriv, FEEPROM, EFUSE_READ_ALL,
276
			"Addr=%d\n", efuse_addr);
L
Larry Finger 已提交
277 278 279
		efuse_addr++;
	}

280
	while ((*rtemp8 != 0xFF) && (efuse_addr < efuse_len)) {
281 282 283 284
		/*  Check PG header for section num.  */
		if ((*rtemp8 & 0x1F) == 0x0F) {/* extended header */
			u1temp = ((*rtemp8 & 0xE0) >> 5);
			read_efuse_byte(hw, efuse_addr, rtemp8);
L
Larry Finger 已提交
285

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
			if ((*rtemp8 & 0x0F) == 0x0F) {
				efuse_addr++;
				read_efuse_byte(hw, efuse_addr, rtemp8);

				if (*rtemp8 != 0xFF &&
				    (efuse_addr < efuse_len)) {
					efuse_addr++;
				}
				continue;
			} else {
				offset = ((*rtemp8 & 0xF0) >> 1) | u1temp;
				wren = (*rtemp8 & 0x0F);
				efuse_addr++;
			}
		} else {
			offset = ((*rtemp8 >> 4) & 0x0f);
L
Larry Finger 已提交
302
			wren = (*rtemp8 & 0x0f);
303 304 305
		}

		if (offset < efuse_max_section) {
L
Larry Finger 已提交
306
			RTPRINT(rtlpriv, FEEPROM, EFUSE_READ_ALL,
307
				"offset-%d Worden=%x\n", offset, wren);
L
Larry Finger 已提交
308 309 310 311

			for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) {
				if (!(wren & 0x01)) {
					RTPRINT(rtlpriv, FEEPROM,
312 313
						EFUSE_READ_ALL,
						"Addr=%d\n", efuse_addr);
L
Larry Finger 已提交
314 315 316 317

					read_efuse_byte(hw, efuse_addr, rtemp8);
					efuse_addr++;
					efuse_utilized++;
318 319
					efuse_word[i][offset] =
							 (*rtemp8 & 0xff);
L
Larry Finger 已提交
320

321
					if (efuse_addr >= efuse_len)
L
Larry Finger 已提交
322 323 324
						break;

					RTPRINT(rtlpriv, FEEPROM,
325 326
						EFUSE_READ_ALL,
						"Addr=%d\n", efuse_addr);
L
Larry Finger 已提交
327 328 329 330

					read_efuse_byte(hw, efuse_addr, rtemp8);
					efuse_addr++;
					efuse_utilized++;
331
					efuse_word[i][offset] |=
L
Larry Finger 已提交
332 333
					    (((u16)*rtemp8 << 8) & 0xff00);

334
					if (efuse_addr >= efuse_len)
L
Larry Finger 已提交
335 336 337 338 339 340 341 342
						break;
				}

				wren >>= 1;
			}
		}

		RTPRINT(rtlpriv, FEEPROM, EFUSE_READ_ALL,
343
			"Addr=%d\n", efuse_addr);
L
Larry Finger 已提交
344
		read_efuse_byte(hw, efuse_addr, rtemp8);
345
		if (*rtemp8 != 0xFF && (efuse_addr < efuse_len)) {
L
Larry Finger 已提交
346 347 348 349 350
			efuse_utilized++;
			efuse_addr++;
		}
	}

351
	for (i = 0; i < efuse_max_section; i++) {
L
Larry Finger 已提交
352 353
		for (j = 0; j < EFUSE_MAX_WORD_UNIT; j++) {
			efuse_tbl[(i * 8) + (j * 2)] =
354
			    (efuse_word[j][i] & 0xff);
L
Larry Finger 已提交
355
			efuse_tbl[(i * 8) + ((j * 2) + 1)] =
356
			    ((efuse_word[j][i] >> 8) & 0xff);
L
Larry Finger 已提交
357 358 359 360 361 362 363
		}
	}

	for (i = 0; i < _size_byte; i++)
		pbuf[i] = efuse_tbl[_offset + i];

	rtlefuse->efuse_usedbytes = efuse_utilized;
364
	efuse_usage = (u8) ((efuse_utilized * 100) / efuse_len);
L
Larry Finger 已提交
365 366 367 368
	rtlefuse->efuse_usedpercentage = efuse_usage;
	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_EFUSE_BYTES,
				      (u8 *)&efuse_utilized);
	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_EFUSE_USAGE,
369
				      &efuse_usage);
370 371 372 373
done:
	for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++)
		kfree(efuse_word[i]);
	kfree(efuse_word);
374
out:
375
	kfree(efuse_tbl);
L
Larry Finger 已提交
376 377 378 379 380 381 382 383
}

bool efuse_shadow_update_chk(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
	u8 section_idx, i, Base;
	u16 words_need = 0, hdr_num = 0, totalbytes, efuse_used;
384
	bool wordchanged, result = true;
L
Larry Finger 已提交
385 386 387

	for (section_idx = 0; section_idx < 16; section_idx++) {
		Base = section_idx * 8;
388
		wordchanged = false;
L
Larry Finger 已提交
389 390 391 392 393 394 395 396

		for (i = 0; i < 8; i = i + 2) {
			if ((rtlefuse->efuse_map[EFUSE_INIT_MAP][Base + i] !=
			     rtlefuse->efuse_map[EFUSE_MODIFY_MAP][Base + i]) ||
			    (rtlefuse->efuse_map[EFUSE_INIT_MAP][Base + i + 1] !=
			     rtlefuse->efuse_map[EFUSE_MODIFY_MAP][Base + i +
								   1])) {
				words_need++;
397
				wordchanged = true;
L
Larry Finger 已提交
398 399 400
			}
		}

401
		if (wordchanged)
L
Larry Finger 已提交
402 403 404 405 406 407 408
			hdr_num++;
	}

	totalbytes = hdr_num + words_need * 2;
	efuse_used = rtlefuse->efuse_usedbytes;

	if ((totalbytes + efuse_used) >=
409
	    (EFUSE_MAX_SIZE - rtlpriv->cfg->maps[EFUSE_OOB_PROTECT_BYTES_LEN]))
410
		result = false;
L
Larry Finger 已提交
411 412

	RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
413 414
		 "efuse_shadow_update_chk(): totalbytes(%#x), hdr_num(%#x), words_need(%#x), efuse_used(%d)\n",
		 totalbytes, hdr_num, words_need, efuse_used);
L
Larry Finger 已提交
415

416
	return result;
L
Larry Finger 已提交
417 418 419 420 421 422
}

void efuse_shadow_read(struct ieee80211_hw *hw, u8 type,
		       u16 offset, u32 *value)
{
	if (type == 1)
423
		efuse_shadow_read_1byte(hw, offset, (u8 *)value);
L
Larry Finger 已提交
424
	else if (type == 2)
425
		efuse_shadow_read_2byte(hw, offset, (u16 *)value);
L
Larry Finger 已提交
426
	else if (type == 4)
427
		efuse_shadow_read_4byte(hw, offset, value);
L
Larry Finger 已提交
428 429

}
430
EXPORT_SYMBOL(efuse_shadow_read);
L
Larry Finger 已提交
431 432 433 434 435 436 437 438 439

void efuse_shadow_write(struct ieee80211_hw *hw, u8 type, u16 offset,
				u32 value)
{
	if (type == 1)
		efuse_shadow_write_1byte(hw, offset, (u8) value);
	else if (type == 2)
		efuse_shadow_write_2byte(hw, offset, (u16) value);
	else if (type == 4)
440
		efuse_shadow_write_4byte(hw, offset, value);
L
Larry Finger 已提交
441 442 443 444 445 446 447 448 449 450 451

}

bool efuse_shadow_update(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
	u16 i, offset, base;
	u8 word_en = 0x0F;
	u8 first_pg = false;

452
	RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD, "\n");
L
Larry Finger 已提交
453 454 455

	if (!efuse_shadow_update_chk(hw)) {
		efuse_read_all_map(hw, &rtlefuse->efuse_map[EFUSE_INIT_MAP][0]);
I
Ilia Mirkin 已提交
456 457
		memcpy(&rtlefuse->efuse_map[EFUSE_MODIFY_MAP][0],
		       &rtlefuse->efuse_map[EFUSE_INIT_MAP][0],
L
Larry Finger 已提交
458 459 460
		       rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]);

		RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
461
			 "efuse out of capacity!!\n");
L
Larry Finger 已提交
462 463 464 465 466 467 468 469 470 471
		return false;
	}
	efuse_power_switch(hw, true, true);

	for (offset = 0; offset < 16; offset++) {

		word_en = 0x0F;
		base = offset * 8;

		for (i = 0; i < 8; i++) {
472
			if (first_pg) {
L
Larry Finger 已提交
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
				word_en &= ~(BIT(i / 2));

				rtlefuse->efuse_map[EFUSE_INIT_MAP][base + i] =
				    rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base + i];
			} else {

				if (rtlefuse->efuse_map[EFUSE_INIT_MAP][base + i] !=
				    rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base + i]) {
					word_en &= ~(BIT(i / 2));

					rtlefuse->efuse_map[EFUSE_INIT_MAP][base + i] =
					    rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base + i];
				}
			}
		}

		if (word_en != 0x0F) {
			u8 tmpdata[8];
I
Ilia Mirkin 已提交
491 492 493
			memcpy(tmpdata,
			       &rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base],
			       8);
L
Larry Finger 已提交
494
			RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_LOUD,
495
				      "U-efuse\n", tmpdata, 8);
L
Larry Finger 已提交
496 497 498 499

			if (!efuse_pg_packet_write(hw, (u8) offset, word_en,
						   tmpdata)) {
				RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
500
					 "PG section(%#x) fail!!\n", offset);
L
Larry Finger 已提交
501 502 503 504 505 506 507 508 509
				break;
			}
		}

	}

	efuse_power_switch(hw, true, false);
	efuse_read_all_map(hw, &rtlefuse->efuse_map[EFUSE_INIT_MAP][0]);

I
Ilia Mirkin 已提交
510 511
	memcpy(&rtlefuse->efuse_map[EFUSE_MODIFY_MAP][0],
	       &rtlefuse->efuse_map[EFUSE_INIT_MAP][0],
L
Larry Finger 已提交
512 513
	       rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]);

514
	RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD, "\n");
L
Larry Finger 已提交
515 516 517 518 519 520 521 522
	return true;
}

void rtl_efuse_shadow_map_update(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));

523
	if (rtlefuse->autoload_failflag)
524 525
		memset((&rtlefuse->efuse_map[EFUSE_INIT_MAP][0]),
		       0xFF, rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]);
526
	else
L
Larry Finger 已提交
527 528
		efuse_read_all_map(hw, &rtlefuse->efuse_map[EFUSE_INIT_MAP][0]);

I
Ilia Mirkin 已提交
529
	memcpy(&rtlefuse->efuse_map[EFUSE_MODIFY_MAP][0],
530 531
			&rtlefuse->efuse_map[EFUSE_INIT_MAP][0],
			rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]);
L
Larry Finger 已提交
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613

}
EXPORT_SYMBOL(rtl_efuse_shadow_map_update);

void efuse_force_write_vendor_Id(struct ieee80211_hw *hw)
{
	u8 tmpdata[8] = { 0xFF, 0xFF, 0xEC, 0x10, 0xFF, 0xFF, 0xFF, 0xFF };

	efuse_power_switch(hw, true, true);

	efuse_pg_packet_write(hw, 1, 0xD, tmpdata);

	efuse_power_switch(hw, true, false);

}

void efuse_re_pg_section(struct ieee80211_hw *hw, u8 section_idx)
{
}

static void efuse_shadow_read_1byte(struct ieee80211_hw *hw,
				    u16 offset, u8 *value)
{
	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
	*value = rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset];
}

static void efuse_shadow_read_2byte(struct ieee80211_hw *hw,
				    u16 offset, u16 *value)
{
	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));

	*value = rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset];
	*value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] << 8;

}

static void efuse_shadow_read_4byte(struct ieee80211_hw *hw,
				    u16 offset, u32 *value)
{
	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));

	*value = rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset];
	*value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] << 8;
	*value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 2] << 16;
	*value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 3] << 24;
}

static void efuse_shadow_write_1byte(struct ieee80211_hw *hw,
				     u16 offset, u8 value)
{
	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));

	rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset] = value;
}

static void efuse_shadow_write_2byte(struct ieee80211_hw *hw,
				     u16 offset, u16 value)
{
	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));

	rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset] = value & 0x00FF;
	rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] = value >> 8;

}

static void efuse_shadow_write_4byte(struct ieee80211_hw *hw,
				     u16 offset, u32 value)
{
	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));

	rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset] =
	    (u8) (value & 0x000000FF);
	rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] =
	    (u8) ((value >> 8) & 0x0000FF);
	rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 2] =
	    (u8) ((value >> 16) & 0x00FF);
	rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 3] =
	    (u8) ((value >> 24) & 0xFF);

}

614
int efuse_one_byte_read(struct ieee80211_hw *hw, u16 addr, u8 *data)
L
Larry Finger 已提交
615 616 617
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u8 tmpidx = 0;
618
	int result;
L
Larry Finger 已提交
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637

	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1,
		       (u8) (addr & 0xff));
	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2,
		       ((u8) ((addr >> 8) & 0x03)) |
		       (rtl_read_byte(rtlpriv,
				      rtlpriv->cfg->maps[EFUSE_CTRL] + 2) &
			0xFC));

	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3, 0x72);

	while (!(0x80 & rtl_read_byte(rtlpriv,
				      rtlpriv->cfg->maps[EFUSE_CTRL] + 3))
	       && (tmpidx < 100)) {
		tmpidx++;
	}

	if (tmpidx < 100) {
		*data = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL]);
638
		result = true;
L
Larry Finger 已提交
639 640
	} else {
		*data = 0xff;
641
		result = false;
L
Larry Finger 已提交
642
	}
643
	return result;
L
Larry Finger 已提交
644
}
645
EXPORT_SYMBOL(efuse_one_byte_read);
L
Larry Finger 已提交
646 647 648 649 650 651

static int efuse_one_byte_write(struct ieee80211_hw *hw, u16 addr, u8 data)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u8 tmpidx = 0;

652 653
	RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
		 "Addr = %x Data=%x\n", addr, data);
L
Larry Finger 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671

	rtl_write_byte(rtlpriv,
		       rtlpriv->cfg->maps[EFUSE_CTRL] + 1, (u8) (addr & 0xff));
	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2,
		       (rtl_read_byte(rtlpriv,
			 rtlpriv->cfg->maps[EFUSE_CTRL] +
			 2) & 0xFC) | (u8) ((addr >> 8) & 0x03));

	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL], data);
	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3, 0xF2);

	while ((0x80 & rtl_read_byte(rtlpriv,
				     rtlpriv->cfg->maps[EFUSE_CTRL] + 3))
	       && (tmpidx < 100)) {
		tmpidx++;
	}

	if (tmpidx < 100)
672 673
		return true;
	return false;
L
Larry Finger 已提交
674 675
}

676
static void efuse_read_all_map(struct ieee80211_hw *hw, u8 *efuse)
L
Larry Finger 已提交
677
{
678
	struct rtl_priv *rtlpriv = rtl_priv(hw);
L
Larry Finger 已提交
679
	efuse_power_switch(hw, false, true);
680
	read_efuse(hw, 0, rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE], efuse);
L
Larry Finger 已提交
681 682 683 684 685 686 687
	efuse_power_switch(hw, false, false);
}

static void efuse_read_data_case1(struct ieee80211_hw *hw, u16 *efuse_addr,
				u8 efuse_data, u8 offset, u8 *tmpdata,
				u8 *readstate)
{
688
	bool dataempty = true;
L
Larry Finger 已提交
689 690 691 692 693 694 695 696 697 698 699 700
	u8 hoffset;
	u8 tmpidx;
	u8 hworden;
	u8 word_cnts;

	hoffset = (efuse_data >> 4) & 0x0F;
	hworden = efuse_data & 0x0F;
	word_cnts = efuse_calculate_word_cnts(hworden);

	if (hoffset == offset) {
		for (tmpidx = 0; tmpidx < word_cnts * 2; tmpidx++) {
			if (efuse_one_byte_read(hw, *efuse_addr + 1 + tmpidx,
701
						&efuse_data)) {
L
Larry Finger 已提交
702 703
				tmpdata[tmpidx] = efuse_data;
				if (efuse_data != 0xff)
704
					dataempty = false;
L
Larry Finger 已提交
705 706 707
			}
		}

708
		if (!dataempty) {
L
Larry Finger 已提交
709
			*readstate = PG_STATE_DATA;
710
		} else {
L
Larry Finger 已提交
711 712 713 714 715 716 717 718 719 720 721 722 723
			*efuse_addr = *efuse_addr + (word_cnts * 2) + 1;
			*readstate = PG_STATE_HEADER;
		}

	} else {
		*efuse_addr = *efuse_addr + (word_cnts * 2) + 1;
		*readstate = PG_STATE_HEADER;
	}
}

static int efuse_pg_packet_read(struct ieee80211_hw *hw, u8 offset, u8 *data)
{
	u8 readstate = PG_STATE_HEADER;
724

725
	bool continual = true;
726

L
Larry Finger 已提交
727 728 729 730 731 732 733 734 735
	u8 efuse_data, word_cnts = 0;
	u16 efuse_addr = 0;
	u8 tmpdata[8];

	if (data == NULL)
		return false;
	if (offset > 15)
		return false;

I
Ilia Mirkin 已提交
736 737
	memset(data, 0xff, PGPKT_DATA_SIZE * sizeof(u8));
	memset(tmpdata, 0xff, PGPKT_DATA_SIZE * sizeof(u8));
L
Larry Finger 已提交
738

739
	while (continual && (efuse_addr < EFUSE_MAX_SIZE)) {
L
Larry Finger 已提交
740 741 742 743
		if (readstate & PG_STATE_HEADER) {
			if (efuse_one_byte_read(hw, efuse_addr, &efuse_data)
			    && (efuse_data != 0xFF))
				efuse_read_data_case1(hw, &efuse_addr,
744 745
						      efuse_data, offset,
						      tmpdata, &readstate);
L
Larry Finger 已提交
746
			else
747
				continual = false;
L
Larry Finger 已提交
748
		} else if (readstate & PG_STATE_DATA) {
749
			efuse_word_enable_data_read(0, tmpdata, data);
L
Larry Finger 已提交
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
			efuse_addr = efuse_addr + (word_cnts * 2) + 1;
			readstate = PG_STATE_HEADER;
		}

	}

	if ((data[0] == 0xff) && (data[1] == 0xff) &&
	    (data[2] == 0xff) && (data[3] == 0xff) &&
	    (data[4] == 0xff) && (data[5] == 0xff) &&
	    (data[6] == 0xff) && (data[7] == 0xff))
		return false;
	else
		return true;

}

static void efuse_write_data_case1(struct ieee80211_hw *hw, u16 *efuse_addr,
767 768 769 770
				   u8 efuse_data, u8 offset,
				   int *continual, u8 *write_state,
				   struct pgpkt_struct *target_pkt,
				   int *repeat_times, int *result, u8 word_en)
L
Larry Finger 已提交
771 772 773
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct pgpkt_struct tmp_pkt;
774
	int dataempty = true;
L
Larry Finger 已提交
775 776 777 778 779 780 781 782 783 784 785
	u8 originaldata[8 * sizeof(u8)];
	u8 badworden = 0x0F;
	u8 match_word_en, tmp_word_en;
	u8 tmpindex;
	u8 tmp_header = efuse_data;
	u8 tmp_word_cnts;

	tmp_pkt.offset = (tmp_header >> 4) & 0x0F;
	tmp_pkt.word_en = tmp_header & 0x0F;
	tmp_word_cnts = efuse_calculate_word_cnts(tmp_pkt.word_en);

786 787
	if (tmp_pkt.offset != target_pkt->offset) {
		*efuse_addr = *efuse_addr + (tmp_word_cnts * 2) + 1;
L
Larry Finger 已提交
788 789 790
		*write_state = PG_STATE_HEADER;
	} else {
		for (tmpindex = 0; tmpindex < (tmp_word_cnts * 2); tmpindex++) {
791 792 793 794
			if (efuse_one_byte_read(hw,
						(*efuse_addr + 1 + tmpindex),
						&efuse_data) &&
			    (efuse_data != 0xFF))
795
				dataempty = false;
L
Larry Finger 已提交
796 797
		}

798
		if (!dataempty) {
799
			*efuse_addr = *efuse_addr + (tmp_word_cnts * 2) + 1;
L
Larry Finger 已提交
800 801 802
			*write_state = PG_STATE_HEADER;
		} else {
			match_word_en = 0x0F;
803
			if (!((target_pkt->word_en & BIT(0)) |
804
			    (tmp_pkt.word_en & BIT(0))))
L
Larry Finger 已提交
805 806
				match_word_en &= (~BIT(0));

807
			if (!((target_pkt->word_en & BIT(1)) |
808
			    (tmp_pkt.word_en & BIT(1))))
L
Larry Finger 已提交
809 810
				match_word_en &= (~BIT(1));

811
			if (!((target_pkt->word_en & BIT(2)) |
812
			    (tmp_pkt.word_en & BIT(2))))
L
Larry Finger 已提交
813 814
				match_word_en &= (~BIT(2));

815
			if (!((target_pkt->word_en & BIT(3)) |
816
			    (tmp_pkt.word_en & BIT(3))))
L
Larry Finger 已提交
817 818 819
				match_word_en &= (~BIT(3));

			if ((match_word_en & 0x0F) != 0x0F) {
820 821 822 823 824
				badworden =
				  enable_efuse_data_write(hw,
							  *efuse_addr + 1,
							  tmp_pkt.word_en,
							  target_pkt->data);
L
Larry Finger 已提交
825

826
				if (0x0F != (badworden & 0x0F))	{
L
Larry Finger 已提交
827 828 829
					u8 reorg_offset = offset;
					u8 reorg_worden = badworden;
					efuse_pg_packet_write(hw, reorg_offset,
830 831
							      reorg_worden,
							      originaldata);
L
Larry Finger 已提交
832 833 834
				}

				tmp_word_en = 0x0F;
835
				if ((target_pkt->word_en & BIT(0)) ^
L
Larry Finger 已提交
836 837 838
				    (match_word_en & BIT(0)))
					tmp_word_en &= (~BIT(0));

839
				if ((target_pkt->word_en & BIT(1)) ^
L
Larry Finger 已提交
840 841 842
				    (match_word_en & BIT(1)))
					tmp_word_en &= (~BIT(1));

843
				if ((target_pkt->word_en & BIT(2)) ^
844
				    (match_word_en & BIT(2)))
L
Larry Finger 已提交
845 846
					tmp_word_en &= (~BIT(2));

847
				if ((target_pkt->word_en & BIT(3)) ^
848
				    (match_word_en & BIT(3)))
L
Larry Finger 已提交
849 850 851 852
					tmp_word_en &= (~BIT(3));

				if ((tmp_word_en & 0x0F) != 0x0F) {
					*efuse_addr = efuse_get_current_size(hw);
853 854
					target_pkt->offset = offset;
					target_pkt->word_en = tmp_word_en;
855
				} else {
856
					*continual = false;
857
				}
L
Larry Finger 已提交
858 859 860
				*write_state = PG_STATE_HEADER;
				*repeat_times += 1;
				if (*repeat_times > EFUSE_REPEAT_THRESHOLD_) {
861 862
					*continual = false;
					*result = false;
L
Larry Finger 已提交
863 864 865
				}
			} else {
				*efuse_addr += (2 * tmp_word_cnts) + 1;
866 867
				target_pkt->offset = offset;
				target_pkt->word_en = word_en;
L
Larry Finger 已提交
868 869 870 871
				*write_state = PG_STATE_HEADER;
			}
		}
	}
872
	RTPRINT(rtlpriv, FEEPROM, EFUSE_PG, "efuse PG_STATE_HEADER-1\n");
L
Larry Finger 已提交
873 874 875
}

static void efuse_write_data_case2(struct ieee80211_hw *hw, u16 *efuse_addr,
876
				   int *continual, u8 *write_state,
L
Larry Finger 已提交
877
				   struct pgpkt_struct target_pkt,
878
				   int *repeat_times, int *result)
L
Larry Finger 已提交
879 880 881 882 883 884 885 886 887 888 889 890 891
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct pgpkt_struct tmp_pkt;
	u8 pg_header;
	u8 tmp_header;
	u8 originaldata[8 * sizeof(u8)];
	u8 tmp_word_cnts;
	u8 badworden = 0x0F;

	pg_header = ((target_pkt.offset << 4) & 0xf0) | target_pkt.word_en;
	efuse_one_byte_write(hw, *efuse_addr, pg_header);
	efuse_one_byte_read(hw, *efuse_addr, &tmp_header);

892
	if (tmp_header == pg_header) {
L
Larry Finger 已提交
893
		*write_state = PG_STATE_DATA;
894
	} else if (tmp_header == 0xFF) {
L
Larry Finger 已提交
895 896 897
		*write_state = PG_STATE_HEADER;
		*repeat_times += 1;
		if (*repeat_times > EFUSE_REPEAT_THRESHOLD_) {
898 899
			*continual = false;
			*result = false;
L
Larry Finger 已提交
900 901 902 903 904 905 906
		}
	} else {
		tmp_pkt.offset = (tmp_header >> 4) & 0x0F;
		tmp_pkt.word_en = tmp_header & 0x0F;

		tmp_word_cnts = efuse_calculate_word_cnts(tmp_pkt.word_en);

907
		memset(originaldata, 0xff,  8 * sizeof(u8));
L
Larry Finger 已提交
908 909

		if (efuse_pg_packet_read(hw, tmp_pkt.offset, originaldata)) {
910 911 912 913
			badworden = enable_efuse_data_write(hw,
							    *efuse_addr + 1,
							    tmp_pkt.word_en,
							    originaldata);
L
Larry Finger 已提交
914 915 916 917 918 919 920 921

			if (0x0F != (badworden & 0x0F)) {
				u8 reorg_offset = tmp_pkt.offset;
				u8 reorg_worden = badworden;
				efuse_pg_packet_write(hw, reorg_offset,
						      reorg_worden,
						      originaldata);
				*efuse_addr = efuse_get_current_size(hw);
922
			} else {
923 924
				*efuse_addr = *efuse_addr +
					      (tmp_word_cnts * 2) + 1;
925 926
			}
		} else {
L
Larry Finger 已提交
927
			*efuse_addr = *efuse_addr + (tmp_word_cnts * 2) + 1;
928
		}
L
Larry Finger 已提交
929 930 931 932

		*write_state = PG_STATE_HEADER;
		*repeat_times += 1;
		if (*repeat_times > EFUSE_REPEAT_THRESHOLD_) {
933 934
			*continual = false;
			*result = false;
L
Larry Finger 已提交
935 936 937
		}

		RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
938
			"efuse PG_STATE_HEADER-2\n");
L
Larry Finger 已提交
939 940 941 942 943 944 945 946 947
	}
}

static int efuse_pg_packet_write(struct ieee80211_hw *hw,
				 u8 offset, u8 word_en, u8 *data)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct pgpkt_struct target_pkt;
	u8 write_state = PG_STATE_HEADER;
948
	int continual = true, dataempty = true, result = true;
L
Larry Finger 已提交
949 950 951 952 953 954
	u16 efuse_addr = 0;
	u8 efuse_data;
	u8 target_word_cnts = 0;
	u8 badworden = 0x0F;
	static int repeat_times;

955
	if (efuse_get_current_size(hw) >= (EFUSE_MAX_SIZE -
956
		rtlpriv->cfg->maps[EFUSE_OOB_PROTECT_BYTES_LEN])) {
L
Larry Finger 已提交
957
		RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
958
			"efuse_pg_packet_write error\n");
L
Larry Finger 已提交
959 960 961 962 963 964
		return false;
	}

	target_pkt.offset = offset;
	target_pkt.word_en = word_en;

965
	memset(target_pkt.data, 0xFF,  8 * sizeof(u8));
L
Larry Finger 已提交
966 967 968 969

	efuse_word_enable_data_read(word_en, data, target_pkt.data);
	target_word_cnts = efuse_calculate_word_cnts(target_pkt.word_en);

970
	RTPRINT(rtlpriv, FEEPROM, EFUSE_PG, "efuse Power ON\n");
L
Larry Finger 已提交
971

972
	while (continual && (efuse_addr < (EFUSE_MAX_SIZE -
973
		rtlpriv->cfg->maps[EFUSE_OOB_PROTECT_BYTES_LEN]))) {
L
Larry Finger 已提交
974 975

		if (write_state == PG_STATE_HEADER) {
976
			dataempty = true;
L
Larry Finger 已提交
977 978
			badworden = 0x0F;
			RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
979
				"efuse PG_STATE_HEADER\n");
L
Larry Finger 已提交
980 981 982 983 984

			if (efuse_one_byte_read(hw, efuse_addr, &efuse_data) &&
			    (efuse_data != 0xFF))
				efuse_write_data_case1(hw, &efuse_addr,
						       efuse_data, offset,
985
						       &continual,
986 987
						       &write_state,
						       &target_pkt,
988
						       &repeat_times, &result,
L
Larry Finger 已提交
989 990 991
						       word_en);
			else
				efuse_write_data_case2(hw, &efuse_addr,
992
						       &continual,
L
Larry Finger 已提交
993 994 995
						       &write_state,
						       target_pkt,
						       &repeat_times,
996
						       &result);
L
Larry Finger 已提交
997 998 999

		} else if (write_state == PG_STATE_DATA) {
			RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
1000
				"efuse PG_STATE_DATA\n");
1001
			badworden = 0x0f;
L
Larry Finger 已提交
1002
			badworden =
1003 1004 1005
			    enable_efuse_data_write(hw, efuse_addr + 1,
						    target_pkt.word_en,
						    target_pkt.data);
L
Larry Finger 已提交
1006 1007

			if ((badworden & 0x0F) == 0x0F) {
1008
				continual = false;
L
Larry Finger 已提交
1009
			} else {
1010 1011
				efuse_addr =
				    efuse_addr + (2 * target_word_cnts) + 1;
L
Larry Finger 已提交
1012 1013 1014 1015 1016 1017 1018 1019 1020

				target_pkt.offset = offset;
				target_pkt.word_en = badworden;
				target_word_cnts =
				    efuse_calculate_word_cnts(target_pkt.
							      word_en);
				write_state = PG_STATE_HEADER;
				repeat_times++;
				if (repeat_times > EFUSE_REPEAT_THRESHOLD_) {
1021 1022
					continual = false;
					result = false;
L
Larry Finger 已提交
1023 1024
				}
				RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
1025
					"efuse PG_STATE_HEADER-3\n");
L
Larry Finger 已提交
1026 1027 1028 1029
			}
		}
	}

1030
	if (efuse_addr >= (EFUSE_MAX_SIZE -
1031
		rtlpriv->cfg->maps[EFUSE_OOB_PROTECT_BYTES_LEN])) {
L
Larry Finger 已提交
1032
		RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
1033
			 "efuse_addr(%#x) Out of size!!\n", efuse_addr);
L
Larry Finger 已提交
1034 1035 1036 1037 1038
	}

	return true;
}

1039 1040
static void efuse_word_enable_data_read(u8 word_en, u8 *sourdata,
					u8 *targetdata)
L
Larry Finger 已提交
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
{
	if (!(word_en & BIT(0))) {
		targetdata[0] = sourdata[0];
		targetdata[1] = sourdata[1];
	}

	if (!(word_en & BIT(1))) {
		targetdata[2] = sourdata[2];
		targetdata[3] = sourdata[3];
	}

	if (!(word_en & BIT(2))) {
		targetdata[4] = sourdata[4];
		targetdata[5] = sourdata[5];
	}

	if (!(word_en & BIT(3))) {
		targetdata[6] = sourdata[6];
		targetdata[7] = sourdata[7];
	}
}

1063 1064
static u8 enable_efuse_data_write(struct ieee80211_hw *hw,
				  u16 efuse_addr, u8 word_en, u8 *data)
L
Larry Finger 已提交
1065 1066 1067 1068 1069 1070 1071
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u16 tmpaddr;
	u16 start_addr = efuse_addr;
	u8 badworden = 0x0F;
	u8 tmpdata[8];

I
Ilia Mirkin 已提交
1072
	memset(tmpdata, 0xff, PGPKT_DATA_SIZE);
1073 1074
	RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
		 "word_en = %x efuse_addr=%x\n", word_en, efuse_addr);
L
Larry Finger 已提交
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122

	if (!(word_en & BIT(0))) {
		tmpaddr = start_addr;
		efuse_one_byte_write(hw, start_addr++, data[0]);
		efuse_one_byte_write(hw, start_addr++, data[1]);

		efuse_one_byte_read(hw, tmpaddr, &tmpdata[0]);
		efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[1]);
		if ((data[0] != tmpdata[0]) || (data[1] != tmpdata[1]))
			badworden &= (~BIT(0));
	}

	if (!(word_en & BIT(1))) {
		tmpaddr = start_addr;
		efuse_one_byte_write(hw, start_addr++, data[2]);
		efuse_one_byte_write(hw, start_addr++, data[3]);

		efuse_one_byte_read(hw, tmpaddr, &tmpdata[2]);
		efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[3]);
		if ((data[2] != tmpdata[2]) || (data[3] != tmpdata[3]))
			badworden &= (~BIT(1));
	}

	if (!(word_en & BIT(2))) {
		tmpaddr = start_addr;
		efuse_one_byte_write(hw, start_addr++, data[4]);
		efuse_one_byte_write(hw, start_addr++, data[5]);

		efuse_one_byte_read(hw, tmpaddr, &tmpdata[4]);
		efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[5]);
		if ((data[4] != tmpdata[4]) || (data[5] != tmpdata[5]))
			badworden &= (~BIT(2));
	}

	if (!(word_en & BIT(3))) {
		tmpaddr = start_addr;
		efuse_one_byte_write(hw, start_addr++, data[6]);
		efuse_one_byte_write(hw, start_addr++, data[7]);

		efuse_one_byte_read(hw, tmpaddr, &tmpdata[6]);
		efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[7]);
		if ((data[6] != tmpdata[6]) || (data[7] != tmpdata[7]))
			badworden &= (~BIT(3));
	}

	return badworden;
}

1123
static void efuse_power_switch(struct ieee80211_hw *hw, u8 write, u8 pwrstate)
L
Larry Finger 已提交
1124 1125
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
1126
	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
L
Larry Finger 已提交
1127 1128 1129
	u8 tempval;
	u16 tmpV16;

1130 1131
	if (pwrstate && (rtlhal->hw_type != HARDWARE_TYPE_RTL8192SE)) {

1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
		if (rtlhal->hw_type != HARDWARE_TYPE_RTL8192CE &&
		    rtlhal->hw_type != HARDWARE_TYPE_RTL8192DE) {
			rtl_write_byte(rtlpriv,
				       rtlpriv->cfg->maps[EFUSE_ACCESS], 0x69);
		} else {
			tmpV16 =
			  rtl_read_word(rtlpriv,
					rtlpriv->cfg->maps[SYS_ISO_CTRL]);
			if (!(tmpV16 & rtlpriv->cfg->maps[EFUSE_PWC_EV12V])) {
				tmpV16 |= rtlpriv->cfg->maps[EFUSE_PWC_EV12V];
				rtl_write_word(rtlpriv,
					       rtlpriv->cfg->maps[SYS_ISO_CTRL],
					       tmpV16);
			}
L
Larry Finger 已提交
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
		}
		tmpV16 = rtl_read_word(rtlpriv,
				       rtlpriv->cfg->maps[SYS_FUNC_EN]);
		if (!(tmpV16 & rtlpriv->cfg->maps[EFUSE_FEN_ELDR])) {
			tmpV16 |= rtlpriv->cfg->maps[EFUSE_FEN_ELDR];
			rtl_write_word(rtlpriv,
				       rtlpriv->cfg->maps[SYS_FUNC_EN], tmpV16);
		}

		tmpV16 = rtl_read_word(rtlpriv, rtlpriv->cfg->maps[SYS_CLK]);
		if ((!(tmpV16 & rtlpriv->cfg->maps[EFUSE_LOADER_CLK_EN])) ||
		    (!(tmpV16 & rtlpriv->cfg->maps[EFUSE_ANA8M]))) {
			tmpV16 |= (rtlpriv->cfg->maps[EFUSE_LOADER_CLK_EN] |
				   rtlpriv->cfg->maps[EFUSE_ANA8M]);
			rtl_write_word(rtlpriv,
				       rtlpriv->cfg->maps[SYS_CLK], tmpV16);
		}
	}

1165 1166
	if (pwrstate) {
		if (write) {
L
Larry Finger 已提交
1167 1168 1169
			tempval = rtl_read_byte(rtlpriv,
						rtlpriv->cfg->maps[EFUSE_TEST] +
						3);
1170

1171 1172 1173 1174
			if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE) {
				tempval &= ~(BIT(3) | BIT(4) | BIT(5) | BIT(6));
				tempval |= (VOLTAGE_V25 << 3);
			} else if (rtlhal->hw_type != HARDWARE_TYPE_RTL8192SE) {
1175 1176 1177 1178
				tempval &= 0x0F;
				tempval |= (VOLTAGE_V25 << 4);
			}

L
Larry Finger 已提交
1179 1180 1181 1182 1183
			rtl_write_byte(rtlpriv,
				       rtlpriv->cfg->maps[EFUSE_TEST] + 3,
				       (tempval | 0x80));
		}

1184 1185
		if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192SE) {
			rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CLK],
1186
				       0x03);
1187
		}
L
Larry Finger 已提交
1188
	} else {
1189 1190
		if (rtlhal->hw_type != HARDWARE_TYPE_RTL8192CE &&
		    rtlhal->hw_type != HARDWARE_TYPE_RTL8192DE)
1191 1192 1193
			rtl_write_byte(rtlpriv,
				       rtlpriv->cfg->maps[EFUSE_ACCESS], 0);

1194
		if (write) {
L
Larry Finger 已提交
1195 1196 1197 1198 1199 1200 1201 1202
			tempval = rtl_read_byte(rtlpriv,
						rtlpriv->cfg->maps[EFUSE_TEST] +
						3);
			rtl_write_byte(rtlpriv,
				       rtlpriv->cfg->maps[EFUSE_TEST] + 3,
				       (tempval & 0x7F));
		}

1203 1204
		if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192SE) {
			rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CLK],
1205
				       0x02);
1206
		}
L
Larry Finger 已提交
1207 1208 1209 1210 1211
	}
}

static u16 efuse_get_current_size(struct ieee80211_hw *hw)
{
1212
	int continual = true;
L
Larry Finger 已提交
1213
	u16 efuse_addr = 0;
1214
	u8 hoffset, hworden;
L
Larry Finger 已提交
1215 1216
	u8 efuse_data, word_cnts;

1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
	while (continual && efuse_one_byte_read(hw, efuse_addr, &efuse_data) &&
	       (efuse_addr < EFUSE_MAX_SIZE)) {
		if (efuse_data != 0xFF) {
			hoffset = (efuse_data >> 4) & 0x0F;
			hworden = efuse_data & 0x0F;
			word_cnts = efuse_calculate_word_cnts(hworden);
			efuse_addr = efuse_addr + (word_cnts * 2) + 1;
		} else {
			continual = false;
		}
L
Larry Finger 已提交
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
	}

	return efuse_addr;
}

static u8 efuse_calculate_word_cnts(u8 word_en)
{
	u8 word_cnts = 0;
	if (!(word_en & BIT(0)))
		word_cnts++;
	if (!(word_en & BIT(1)))
		word_cnts++;
	if (!(word_en & BIT(2)))
		word_cnts++;
	if (!(word_en & BIT(3)))
		word_cnts++;
	return word_cnts;
}