staging: rtl8188eu: Remove function rtw_BT_efuse_map_read()
[deliverable/linux.git] / drivers / staging / rtl8188eu / core / rtw_efuse.c
1 /******************************************************************************
2 *
3 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
4 *
5 * This 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 * This 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 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 *
19 ******************************************************************************/
20 #define _RTW_EFUSE_C_
21
22 #include <osdep_service.h>
23 #include <drv_types.h>
24 #include <rtw_efuse.h>
25 #include <usb_ops_linux.h>
26
27 /*------------------------Define local variable------------------------------*/
28 u8 fakeEfuseBank;
29 u32 fakeEfuseUsedBytes;
30 u8 fakeEfuseContent[EFUSE_MAX_HW_SIZE] = {0};
31 u8 fakeEfuseInitMap[EFUSE_MAX_MAP_LEN] = {0};
32 u8 fakeEfuseModifiedMap[EFUSE_MAX_MAP_LEN] = {0};
33
34 u32 BTEfuseUsedBytes;
35 u8 BTEfuseContent[EFUSE_MAX_BT_BANK][EFUSE_MAX_HW_SIZE];
36 u8 BTEfuseInitMap[EFUSE_BT_MAX_MAP_LEN] = {0};
37 u8 BTEfuseModifiedMap[EFUSE_BT_MAX_MAP_LEN] = {0};
38
39 u32 fakeBTEfuseUsedBytes;
40 u8 fakeBTEfuseContent[EFUSE_MAX_BT_BANK][EFUSE_MAX_HW_SIZE];
41 u8 fakeBTEfuseInitMap[EFUSE_BT_MAX_MAP_LEN] = {0};
42 u8 fakeBTEfuseModifiedMap[EFUSE_BT_MAX_MAP_LEN] = {0};
43 /*------------------------Define local variable------------------------------*/
44
45 /* */
46 #define REG_EFUSE_CTRL 0x0030
47 #define EFUSE_CTRL REG_EFUSE_CTRL /* E-Fuse Control. */
48 /* */
49
50 /* 11/16/2008 MH Add description. Get current efuse area enabled word!!. */
51 u8
52 Efuse_CalculateWordCnts(u8 word_en)
53 {
54 u8 word_cnts = 0;
55 if (!(word_en & BIT(0)))
56 word_cnts++; /* 0 : write enable */
57 if (!(word_en & BIT(1)))
58 word_cnts++;
59 if (!(word_en & BIT(2)))
60 word_cnts++;
61 if (!(word_en & BIT(3)))
62 word_cnts++;
63 return word_cnts;
64 }
65
66 /*
67 * Description:
68 * Execute E-Fuse read byte operation.
69 * Referred from SD1 Richard.
70 * Assumption:
71 * 1. Boot from E-Fuse and successfully auto-load.
72 * 2. PASSIVE_LEVEL (USB interface)
73 * Created by Roger, 2008.10.21.
74 */
75 void
76 ReadEFuseByte(
77 struct adapter *Adapter,
78 u16 _offset,
79 u8 *pbuf,
80 bool pseudo)
81 {
82 u32 value32;
83 u8 readbyte;
84 u16 retry;
85
86 /* Write Address */
87 usb_write8(Adapter, EFUSE_CTRL+1, (_offset & 0xff));
88 readbyte = usb_read8(Adapter, EFUSE_CTRL+2);
89 usb_write8(Adapter, EFUSE_CTRL+2, ((_offset >> 8) & 0x03) | (readbyte & 0xfc));
90
91 /* Write bit 32 0 */
92 readbyte = usb_read8(Adapter, EFUSE_CTRL+3);
93 usb_write8(Adapter, EFUSE_CTRL+3, (readbyte & 0x7f));
94
95 /* Check bit 32 read-ready */
96 retry = 0;
97 value32 = usb_read32(Adapter, EFUSE_CTRL);
98 while (!(((value32 >> 24) & 0xff) & 0x80) && (retry < 10000)) {
99 value32 = usb_read32(Adapter, EFUSE_CTRL);
100 retry++;
101 }
102
103 /* 20100205 Joseph: Add delay suggested by SD1 Victor. */
104 /* This fix the problem that Efuse read error in high temperature condition. */
105 /* Designer says that there shall be some delay after ready bit is set, or the */
106 /* result will always stay on last data we read. */
107 udelay(50);
108 value32 = usb_read32(Adapter, EFUSE_CTRL);
109
110 *pbuf = (u8)(value32 & 0xff);
111 }
112
113 /*-----------------------------------------------------------------------------
114 * Function: EFUSE_Read1Byte
115 *
116 * Overview: Copy from WMAC fot EFUSE read 1 byte.
117 *
118 * Input: NONE
119 *
120 * Output: NONE
121 *
122 * Return: NONE
123 *
124 * Revised History:
125 * When Who Remark
126 * 09/23/2008 MHC Copy from WMAC.
127 *
128 *---------------------------------------------------------------------------*/
129 u8 EFUSE_Read1Byte(struct adapter *Adapter, u16 Address)
130 {
131 u8 data;
132 u8 Bytetemp = {0x00};
133 u8 temp = {0x00};
134 u32 k = 0;
135 u16 contentLen = 0;
136
137 EFUSE_GetEfuseDefinition(Adapter, EFUSE_WIFI , TYPE_EFUSE_REAL_CONTENT_LEN, (void *)&contentLen);
138
139 if (Address < contentLen) { /* E-fuse 512Byte */
140 /* Write E-fuse Register address bit0~7 */
141 temp = Address & 0xFF;
142 usb_write8(Adapter, EFUSE_CTRL+1, temp);
143 Bytetemp = usb_read8(Adapter, EFUSE_CTRL+2);
144 /* Write E-fuse Register address bit8~9 */
145 temp = ((Address >> 8) & 0x03) | (Bytetemp & 0xFC);
146 usb_write8(Adapter, EFUSE_CTRL+2, temp);
147
148 /* Write 0x30[31]= 0 */
149 Bytetemp = usb_read8(Adapter, EFUSE_CTRL+3);
150 temp = Bytetemp & 0x7F;
151 usb_write8(Adapter, EFUSE_CTRL+3, temp);
152
153 /* Wait Write-ready (0x30[31]= 1) */
154 Bytetemp = usb_read8(Adapter, EFUSE_CTRL+3);
155 while (!(Bytetemp & 0x80)) {
156 Bytetemp = usb_read8(Adapter, EFUSE_CTRL+3);
157 k++;
158 if (k == 1000) {
159 k = 0;
160 break;
161 }
162 }
163 data = usb_read8(Adapter, EFUSE_CTRL);
164 return data;
165 } else {
166 return 0xFF;
167 }
168
169 } /* EFUSE_Read1Byte */
170
171 /* 11/16/2008 MH Read one byte from real Efuse. */
172 u8 efuse_OneByteRead(struct adapter *pAdapter, u16 addr, u8 *data, bool pseudo)
173 {
174 u8 tmpidx = 0;
175 u8 result;
176
177 /* -----------------e-fuse reg ctrl --------------------------------- */
178 /* address */
179 usb_write8(pAdapter, EFUSE_CTRL+1, (u8)(addr & 0xff));
180 usb_write8(pAdapter, EFUSE_CTRL+2, ((u8)((addr>>8) & 0x03)) |
181 (usb_read8(pAdapter, EFUSE_CTRL+2) & 0xFC));
182
183 usb_write8(pAdapter, EFUSE_CTRL+3, 0x72);/* read cmd */
184
185 while (!(0x80 & usb_read8(pAdapter, EFUSE_CTRL+3)) && (tmpidx < 100))
186 tmpidx++;
187 if (tmpidx < 100) {
188 *data = usb_read8(pAdapter, EFUSE_CTRL);
189 result = true;
190 } else {
191 *data = 0xff;
192 result = false;
193 }
194 return result;
195 }
196
197 /* 11/16/2008 MH Write one byte to reald Efuse. */
198 u8 efuse_OneByteWrite(struct adapter *pAdapter, u16 addr, u8 data, bool pseudo)
199 {
200 u8 tmpidx = 0;
201 u8 result;
202
203 /* -----------------e-fuse reg ctrl --------------------------------- */
204 /* address */
205 usb_write8(pAdapter, EFUSE_CTRL+1, (u8)(addr&0xff));
206 usb_write8(pAdapter, EFUSE_CTRL+2,
207 (usb_read8(pAdapter, EFUSE_CTRL+2) & 0xFC) |
208 (u8)((addr>>8) & 0x03));
209 usb_write8(pAdapter, EFUSE_CTRL, data);/* data */
210
211 usb_write8(pAdapter, EFUSE_CTRL+3, 0xF2);/* write cmd */
212
213 while ((0x80 & usb_read8(pAdapter, EFUSE_CTRL+3)) && (tmpidx < 100))
214 tmpidx++;
215
216 if (tmpidx < 100)
217 result = true;
218 else
219 result = false;
220
221 return result;
222 }
223
224 /*-----------------------------------------------------------------------------
225 * Function: efuse_WordEnableDataRead
226 *
227 * Overview: Read allowed word in current efuse section data.
228 *
229 * Input: NONE
230 *
231 * Output: NONE
232 *
233 * Return: NONE
234 *
235 * Revised History:
236 * When Who Remark
237 * 11/16/2008 MHC Create Version 0.
238 * 11/21/2008 MHC Fix Write bug when we only enable late word.
239 *
240 *---------------------------------------------------------------------------*/
241 void efuse_WordEnableDataRead(u8 word_en, u8 *sourdata, u8 *targetdata)
242 {
243 if (!(word_en&BIT(0))) {
244 targetdata[0] = sourdata[0];
245 targetdata[1] = sourdata[1];
246 }
247 if (!(word_en&BIT(1))) {
248 targetdata[2] = sourdata[2];
249 targetdata[3] = sourdata[3];
250 }
251 if (!(word_en&BIT(2))) {
252 targetdata[4] = sourdata[4];
253 targetdata[5] = sourdata[5];
254 }
255 if (!(word_en&BIT(3))) {
256 targetdata[6] = sourdata[6];
257 targetdata[7] = sourdata[7];
258 }
259 }
260
261 static u8 efuse_read8(struct adapter *padapter, u16 address, u8 *value)
262 {
263 return efuse_OneByteRead(padapter, address, value, false);
264 }
265
266 static u8 efuse_write8(struct adapter *padapter, u16 address, u8 *value)
267 {
268 return efuse_OneByteWrite(padapter, address, *value, false);
269 }
270
271 /*
272 * read/wirte raw efuse data
273 */
274 u8 rtw_efuse_access(struct adapter *padapter, u8 write, u16 start_addr, u16 cnts, u8 *data)
275 {
276 int i = 0;
277 u16 real_content_len = 0, max_available_size = 0;
278 u8 res = _FAIL;
279 u8 (*rw8)(struct adapter *, u16, u8*);
280
281 EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_REAL_CONTENT_LEN, (void *)&real_content_len);
282 EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_AVAILABLE_EFUSE_BYTES_TOTAL, (void *)&max_available_size);
283
284 if (start_addr > real_content_len)
285 return _FAIL;
286
287 if (write) {
288 if ((start_addr + cnts) > max_available_size)
289 return _FAIL;
290 rw8 = &efuse_write8;
291 } else {
292 rw8 = &efuse_read8;
293 }
294
295 Efuse_PowerSwitch(padapter, write, true);
296
297 /* e-fuse one byte read / write */
298 for (i = 0; i < cnts; i++) {
299 if (start_addr >= real_content_len) {
300 res = _FAIL;
301 break;
302 }
303
304 res = rw8(padapter, start_addr++, data++);
305 if (_FAIL == res)
306 break;
307 }
308
309 Efuse_PowerSwitch(padapter, write, false);
310
311 return res;
312 }
313 /* */
314 u16 efuse_GetMaxSize(struct adapter *padapter)
315 {
316 u16 max_size;
317 EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI , TYPE_AVAILABLE_EFUSE_BYTES_TOTAL, (void *)&max_size);
318 return max_size;
319 }
320 /* */
321 u8 efuse_GetCurrentSize(struct adapter *padapter, u16 *size)
322 {
323 Efuse_PowerSwitch(padapter, false, true);
324 *size = Efuse_GetCurrentSize(padapter, false);
325 Efuse_PowerSwitch(padapter, false, false);
326
327 return _SUCCESS;
328 }
329 /* */
330 u8 rtw_efuse_map_read(struct adapter *padapter, u16 addr, u16 cnts, u8 *data)
331 {
332 u16 mapLen = 0;
333
334 EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN, (void *)&mapLen);
335
336 if ((addr + cnts) > mapLen)
337 return _FAIL;
338
339 Efuse_PowerSwitch(padapter, false, true);
340
341 efuse_ReadEFuse(padapter, EFUSE_WIFI, addr, cnts, data, false);
342
343 Efuse_PowerSwitch(padapter, false, false);
344
345 return _SUCCESS;
346 }
347
348 /* */
349 u8 rtw_efuse_map_write(struct adapter *padapter, u16 addr, u16 cnts, u8 *data)
350 {
351 u8 offset, word_en;
352 u8 *map;
353 u8 newdata[PGPKT_DATA_SIZE + 1];
354 s32 i, idx;
355 u8 ret = _SUCCESS;
356 u16 mapLen = 0;
357
358 EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN, (void *)&mapLen);
359
360 if ((addr + cnts) > mapLen)
361 return _FAIL;
362
363 map = rtw_zmalloc(mapLen);
364 if (map == NULL)
365 return _FAIL;
366
367 ret = rtw_efuse_map_read(padapter, 0, mapLen, map);
368 if (ret == _FAIL)
369 goto exit;
370
371 Efuse_PowerSwitch(padapter, true, true);
372
373 offset = (addr >> 3);
374 word_en = 0xF;
375 _rtw_memset(newdata, 0xFF, PGPKT_DATA_SIZE + 1);
376 i = addr & 0x7; /* index of one package */
377 idx = 0; /* data index */
378
379 if (i & 0x1) {
380 /* odd start */
381 if (data[idx] != map[addr+idx]) {
382 word_en &= ~BIT(i >> 1);
383 newdata[i-1] = map[addr+idx-1];
384 newdata[i] = data[idx];
385 }
386 i++;
387 idx++;
388 }
389 do {
390 for (; i < PGPKT_DATA_SIZE; i += 2) {
391 if (cnts == idx)
392 break;
393 if ((cnts - idx) == 1) {
394 if (data[idx] != map[addr+idx]) {
395 word_en &= ~BIT(i >> 1);
396 newdata[i] = data[idx];
397 newdata[i+1] = map[addr+idx+1];
398 }
399 idx++;
400 break;
401 } else {
402 if ((data[idx] != map[addr+idx]) ||
403 (data[idx+1] != map[addr+idx+1])) {
404 word_en &= ~BIT(i >> 1);
405 newdata[i] = data[idx];
406 newdata[i+1] = data[idx + 1];
407 }
408 idx += 2;
409 }
410 if (idx == cnts)
411 break;
412 }
413
414 if (word_en != 0xF) {
415 ret = Efuse_PgPacketWrite(padapter, offset, word_en, newdata, false);
416 DBG_88E("offset=%x\n", offset);
417 DBG_88E("word_en=%x\n", word_en);
418
419 for (i = 0; i < PGPKT_DATA_SIZE; i++)
420 DBG_88E("data=%x \t", newdata[i]);
421 if (ret == _FAIL)
422 break;
423 }
424
425 if (idx == cnts)
426 break;
427
428 offset++;
429 i = 0;
430 word_en = 0xF;
431 _rtw_memset(newdata, 0xFF, PGPKT_DATA_SIZE);
432 } while (1);
433
434 Efuse_PowerSwitch(padapter, true, false);
435 exit:
436 kfree(map);
437 return ret;
438 }
439
440 /*-----------------------------------------------------------------------------
441 * Function: efuse_ShadowRead1Byte
442 * efuse_ShadowRead2Byte
443 * efuse_ShadowRead4Byte
444 *
445 * Overview: Read from efuse init map by one/two/four bytes !!!!!
446 *
447 * Input: NONE
448 *
449 * Output: NONE
450 *
451 * Return: NONE
452 *
453 * Revised History:
454 * When Who Remark
455 * 11/12/2008 MHC Create Version 0.
456 *
457 *---------------------------------------------------------------------------*/
458 static void
459 efuse_ShadowRead1Byte(
460 struct adapter *pAdapter,
461 u16 Offset,
462 u8 *Value)
463 {
464 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
465
466 *Value = pEEPROM->efuse_eeprom_data[Offset];
467
468 } /* EFUSE_ShadowRead1Byte */
469
470 /* Read Two Bytes */
471 static void
472 efuse_ShadowRead2Byte(
473 struct adapter *pAdapter,
474 u16 Offset,
475 u16 *Value)
476 {
477 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
478
479 *Value = pEEPROM->efuse_eeprom_data[Offset];
480 *Value |= pEEPROM->efuse_eeprom_data[Offset+1]<<8;
481
482 } /* EFUSE_ShadowRead2Byte */
483
484 /* Read Four Bytes */
485 static void
486 efuse_ShadowRead4Byte(
487 struct adapter *pAdapter,
488 u16 Offset,
489 u32 *Value)
490 {
491 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
492
493 *Value = pEEPROM->efuse_eeprom_data[Offset];
494 *Value |= pEEPROM->efuse_eeprom_data[Offset+1]<<8;
495 *Value |= pEEPROM->efuse_eeprom_data[Offset+2]<<16;
496 *Value |= pEEPROM->efuse_eeprom_data[Offset+3]<<24;
497
498 } /* efuse_ShadowRead4Byte */
499
500 /*-----------------------------------------------------------------------------
501 * Function: Efuse_ReadAllMap
502 *
503 * Overview: Read All Efuse content
504 *
505 * Input: NONE
506 *
507 * Output: NONE
508 *
509 * Return: NONE
510 *
511 * Revised History:
512 * When Who Remark
513 * 11/11/2008 MHC Create Version 0.
514 *
515 *---------------------------------------------------------------------------*/
516 static void Efuse_ReadAllMap(struct adapter *pAdapter, u8 efuseType, u8 *Efuse, bool pseudo)
517 {
518 u16 mapLen = 0;
519
520 Efuse_PowerSwitch(pAdapter, false, true);
521
522 EFUSE_GetEfuseDefinition(pAdapter, efuseType, TYPE_EFUSE_MAP_LEN, (void *)&mapLen);
523
524 efuse_ReadEFuse(pAdapter, efuseType, 0, mapLen, Efuse, pseudo);
525
526 Efuse_PowerSwitch(pAdapter, false, false);
527 }
528
529 /*-----------------------------------------------------------------------------
530 * Function: EFUSE_ShadowMapUpdate
531 *
532 * Overview: Transfer current EFUSE content to shadow init and modify map.
533 *
534 * Input: NONE
535 *
536 * Output: NONE
537 *
538 * Return: NONE
539 *
540 * Revised History:
541 * When Who Remark
542 * 11/13/2008 MHC Create Version 0.
543 *
544 *---------------------------------------------------------------------------*/
545 void EFUSE_ShadowMapUpdate(
546 struct adapter *pAdapter,
547 u8 efuseType,
548 bool pseudo)
549 {
550 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
551 u16 mapLen = 0;
552
553 EFUSE_GetEfuseDefinition(pAdapter, efuseType, TYPE_EFUSE_MAP_LEN, (void *)&mapLen);
554
555 if (pEEPROM->bautoload_fail_flag)
556 _rtw_memset(pEEPROM->efuse_eeprom_data, 0xFF, mapLen);
557 else
558 Efuse_ReadAllMap(pAdapter, efuseType, pEEPROM->efuse_eeprom_data, pseudo);
559 } /* EFUSE_ShadowMapUpdate */
560
561 /*-----------------------------------------------------------------------------
562 * Function: EFUSE_ShadowRead
563 *
564 * Overview: Read from efuse init map !!!!!
565 *
566 * Input: NONE
567 *
568 * Output: NONE
569 *
570 * Return: NONE
571 *
572 * Revised History:
573 * When Who Remark
574 * 11/12/2008 MHC Create Version 0.
575 *
576 *---------------------------------------------------------------------------*/
577 void EFUSE_ShadowRead(struct adapter *pAdapter, u8 Type, u16 Offset, u32 *Value)
578 {
579 if (Type == 1)
580 efuse_ShadowRead1Byte(pAdapter, Offset, (u8 *)Value);
581 else if (Type == 2)
582 efuse_ShadowRead2Byte(pAdapter, Offset, (u16 *)Value);
583 else if (Type == 4)
584 efuse_ShadowRead4Byte(pAdapter, Offset, (u32 *)Value);
585
586 } /* EFUSE_ShadowRead */
This page took 0.043842 seconds and 6 git commands to generate.