-
Notifications
You must be signed in to change notification settings - Fork 6
/
rtbth_core_init.c
1756 lines (1405 loc) · 42.2 KB
/
rtbth_core_init.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
62
63
64
65
66
67
68
69
70
71
72
73
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
108
109
110
111
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
153
154
155
156
157
158
159
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
195
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*************************************************************************
* Ralink Technology Corporation
* 5F., No. 5, Taiyuan 1st St., Jhubei City,
* Hsinchu County 302,
* Taiwan, R.O.C.
*
* (c) Copyright 2012, Ralink Technology Corporation
*
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This 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. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
*************************************************************************/
#include "rtbt_hal.h"
#include "hps_bluez.h"
#include "rtbth_us.h"
#define PKT_HV3_MAX_DATA_LEN 30UL
extern RTBTH_ADAPTER *gpAd;
static struct rtbt_hps_ops rtbt_3298_hps_ops;
int BthInitializeAdapter(IN RTBTH_ADAPTER *pAd)
{
DebugPrint(TRACE, DBG_INIT, "--> BthInitializeAdapter\n");
// Initialze ASIC
BthInitializeAsic(pAd);
_rtbth_us_event_notification(pAd, INIT_COREINIT_EVENT);
DebugPrint(TRACE, DBG_INIT, "<-- BthInitializeAdapter\n");
return STATUS_SUCCESS;
}
UINT32 RT3298_REG_ADDR[]= {
0x0,
0x4,
0x8,
0xc,
0x10,
0x14,
0x18,
0x1c,
0x20,
0x180,
0x184,
0x188,
0x18c,
0x200,
0x204,
0x208,
0x20c,
0x210,
0x220,
0x228,
0x300,
0x304,
0x308,
0x30c,
0x320,
0x324,
0x328,
0x32c,
0x330,
0x334,
0x338,
0x340,
0x344,
0x348,
0x350,
0x354,
0x358,
0x35c,
0x360,
0x380,
0x384,
0x3c0,
0x3c4,
0x400,
0x404,
0x408,
0x40c,
0x410,
0x414,
0x418,
0x41c,
0x420,
0x424,
0x428,
0x42c,
0x434,
0x440,
0x444,
0x448,
0x600,
0x604,
0x608,
0x60c,
0x610
};
VOID eFusePhysicalReadRegisters(
IN PRTBTH_ADAPTER pAd,
IN UINT16 Offset,
IN UINT16 Length,
OUT UINT16 *pData)
{
EFUSE_CTRL_STRUC eFuseCtrlStruc;
int i;
UINT16 efuseDataOffset;
ULONG data;
RT_IO_READ32(pAd, EFUSE_CTRL, (UINT32 *) &eFuseCtrlStruc.word);
//Step0. Write 10-bit of address to EFSROM_AIN (0x324, bit25:bit16). The address must be 16-byte alignment.
eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
//Step1. Write EFSROM_MODE (0x324, bit7:bit6) to 1.
eFuseCtrlStruc.field.EFSROM_MODE = 1;
//Step2. Write EFSROM_KICK (0x324, bit30) to 1 to kick-off physical read procedure.
eFuseCtrlStruc.field.EFSROM_KICK = 1;
RtlCopyMemory(&data, &eFuseCtrlStruc.word, 4);
RT_IO_WRITE32(pAd, EFUSE_CTRL, data);
//Step3. Polling EFSROM_KICK(0x324, bit30) until it become 0 again.
i = 0;
//TODO
while ((i < 100) && (!RT_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)))
{
RT_IO_READ32(pAd, EFUSE_CTRL, (UINT32 *) &eFuseCtrlStruc.word);
if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
break;
rtbt_usec_delay(100);
i++;
}
//Step4. Read 16-byte of data from EFUSE_DATA0-3 (0x59C-0x590)
//efuseDataOffset = EFUSE_DATA3 - (Offset & 0xC) ;
efuseDataOffset = EFUSE_DATA0 + (Offset & 0xC);
RT_IO_READ32(pAd, efuseDataOffset, &data);
data = data >> (8*(Offset & 0x3));
RtlCopyMemory(pData, &data, Length);
}
UCHAR eFuseReadRegisters(
IN RTBTH_ADAPTER *pAd,
IN UINT16 Offset,
IN UINT16 Length,
OUT UINT16 *pData)
{
EFUSE_CTRL_STRUC eFuseCtrlStruc;
int i;
UINT16 efuseDataOffset;
ULONG data;
RT_IO_READ32(pAd, EFUSE_CTRL, (UINT32 *) &eFuseCtrlStruc.word);
//Step0. Write 10-bit of address to EFSROM_AIN (0x324, bit25:bit16). The address must be 16-byte alignment.
// The address must be 16-byte alignment. (This is to say, the last 4 bits must be 0)
eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
//Step1. Write EFSROM_MODE (0x324, bit7:bit6) to 0.
eFuseCtrlStruc.field.EFSROM_MODE = 0;
//Step2. Write EFSROM_KICK (0x324, bit30) to 1 to kick-off physical read procedure.
eFuseCtrlStruc.field.EFSROM_KICK = 1;
RtlCopyMemory(&data, &eFuseCtrlStruc.word, 4);
RT_IO_WRITE32(pAd, EFUSE_CTRL, data);
//Step3. Polling EFSROM_KICK(0x324, bit30) until it become 0 again.
i = 0;
//TODO: NIC card existence checking
while ((i < 100) && (!RT_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)))
{
RT_IO_READ32(pAd, EFUSE_CTRL, (UINT32 *) &eFuseCtrlStruc.word);
if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
{
break;
}
rtbt_usec_delay(100);
i++;
}
//if EFSROM_AOUT is not found in physical address, write 0xffff
if (eFuseCtrlStruc.field.EFSROM_AOUT == 0x3f)
{
for(i=0; i<Length/2; i++)
*(pData+2*i) = 0xffff;
}
else
{
//Step4. Read 16-byte of data from EFUSE_DATA0-3 (0x590-0x59C)
//efuseDataOffset = EFUSE_DATA3 - (Offset & 0xC) ;
efuseDataOffset = EFUSE_DATA0 + (Offset & 0xC) ;
RT_IO_READ32(pAd, efuseDataOffset, &data);
data = data >> (8*(Offset & 0x3));
RtlCopyMemory(pData, &data, Length);
}
return (UCHAR) eFuseCtrlStruc.field.EFSROM_AOUT;
}
VOID eFuseReadPhysical(
IN RTBTH_ADAPTER *pAd,
IN UINT16 *lpInBuffer,
IN ULONG nInBufferSize,
OUT UINT16 *lpOutBuffer,
IN ULONG nOutBufferSize)
{
UINT16 *pInBuf = (UINT16 *)lpInBuffer;
UINT16 *pOutBuf = (UINT16 *)lpOutBuffer;
UINT16 Offset = pInBuf[0]; //addr
UINT16 Length = pInBuf[1]; //length
UINT16 i;
for(i=0; i<Length; i+=2)
{
eFusePhysicalReadRegisters(pAd,Offset+i, 2, &pOutBuf[i/2]);
}
}
NTSTATUS eFuseRead(
IN RTBTH_ADAPTER *pAd,
IN UINT16 Offset,
OUT UINT8 *pData,
IN UINT16 Length)
{
USHORT* pOutBuf = (USHORT*)pData;
NTSTATUS Status = STATUS_SUCCESS;
UCHAR EFSROM_AOUT;
//int i;
USHORT i;
for(i=0; i<Length; i+=2)
{
EFSROM_AOUT = eFuseReadRegisters(pAd, Offset+i, 2, &pOutBuf[i/2]);
}
return Status;
}
VOID eFusePhysicalWriteRegisters(
IN RTBTH_ADAPTER *pAd,
IN USHORT Offset,
IN USHORT Length,
OUT USHORT *pData)
{
EFUSE_CTRL_STRUC eFuseCtrlStruc;
int i;
USHORT efuseDataOffset;
ULONG data, eFuseDataBuffer[4];
DebugPrint(TRACE, DBG_HW_ACCESS,
"eFusePhysicalWriteRegisters() Offset=0x%X, pData=0x%X\n", Offset, *pData);
bt_memset(eFuseDataBuffer, 0, sizeof(ULONG)*4);
if (RT_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST))
return;
//Step0. Write 16-byte of data to EFUSE_DATA0-3 (0x590-0x59C), where EFUSE_DATA0 is the LSB DW, EFUSE_DATA3 is the MSB DW.
/////////////////////////////////////////////////////////////////
//read current values of 16-byte block
RT_IO_READ32(pAd, EFUSE_CTRL, (PULONG) &eFuseCtrlStruc.word);
//Step0. Write 10-bit of address to EFSROM_AIN (0x324, bit25:bit16). The address must be 16-byte alignment.
eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
//Step1. Write EFSROM_MODE (0x324, bit7:bit6) to 1.
eFuseCtrlStruc.field.EFSROM_MODE = 1;
//Step2. Write EFSROM_KICK (0x324, bit30) to 1 to kick-off physical read procedure.
eFuseCtrlStruc.field.EFSROM_KICK = 1;
RtlCopyMemory(&data, &eFuseCtrlStruc.word, 4);
RT_IO_WRITE32(pAd, EFUSE_CTRL, data);
//Step3. Polling EFSROM_KICK(0x324, bit30) until it become 0 again.
i = 0;
while ((i < 100) && (!RT_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)))
{
RT_IO_READ32(pAd, EFUSE_CTRL, (PULONG) &eFuseCtrlStruc.word);
if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
break;
rtbt_usec_delay(100);
i++;
}
//Step4. Read 16-byte of data from EFUSE_DATA0-3 (0x59C-0x590)
//efuseDataOffset = EFUSE_DATA3;
efuseDataOffset = EFUSE_DATA0;
for(i=0; i< 4; i++)
{
RT_IO_READ32(pAd, efuseDataOffset, (PULONG) &eFuseDataBuffer[i]);
//efuseDataOffset -= 4;
efuseDataOffset += 4;
}
//Update the value, the offset is multiple of 2, length is 2
efuseDataOffset = (Offset & 0xc) >> 2;
data = pData[0] & 0xffff;
if((Offset % 4) != 0)
{
eFuseDataBuffer[efuseDataOffset] = (eFuseDataBuffer[efuseDataOffset] & 0xffff) | (data << 16);
}
else
{
eFuseDataBuffer[efuseDataOffset] = (eFuseDataBuffer[efuseDataOffset] & 0xffff0000) | data;
}
//efuseDataOffset = EFUSE_DATA3;
efuseDataOffset = EFUSE_DATA0;
for(i=0; i< 4; i++)
{
RT_IO_WRITE32(pAd, efuseDataOffset, eFuseDataBuffer[i]);
//efuseDataOffset -= 4;
efuseDataOffset += 4;
}
/////////////////////////////////////////////////////////////////
//Step1. Write 10-bit of address to EFSROM_AIN (0x324, bit25:bit16). The address must be 16-byte alignment.
eFuseCtrlStruc.field.EFSROM_AIN = Offset & 0xfff0;
//Step2. Write EFSROM_MODE (0x324, bit7:bit6) to 3.
eFuseCtrlStruc.field.EFSROM_MODE = 3;
//Step3. Write EFSROM_KICK (0x324, bit30) to 1 to kick-off physical write procedure.
eFuseCtrlStruc.field.EFSROM_KICK = 1;
RtlCopyMemory(&data, &eFuseCtrlStruc.word, 4);
RT_IO_WRITE32(pAd, EFUSE_CTRL, data);
//Step4. Polling EFSROM_KICK(0x324, bit30) until it become 0 again. It’s done.
i = 0;
while ((i < 100) && (!RT_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)))
{
RT_IO_READ32(pAd, EFUSE_CTRL, (UINT32 *) &eFuseCtrlStruc.word);
if(eFuseCtrlStruc.field.EFSROM_KICK == 0)
break;
rtbt_usec_delay(100);
i++;
}
}
VOID eFuseWritePhysical(
IN RTBTH_ADAPTER *pAd,
IN USHORT *lpInBuffer,
IN ULONG nInBufferSize,
OUT UCHAR *lpOutBuffer,
OUT ULONG nOutBufferSize)
{
USHORT* pInBuf = (USHORT*)lpInBuffer;
//int i;
USHORT i;
//USHORT* pOutBuf = (USHORT*)ioBuffer;
USHORT Offset = pInBuf[0]; //addr
USHORT Length = pInBuf[1]; //length
USHORT* pValueX = &pInBuf[2]; //value ...
for(i=0; i<Length; i+=2)
{
eFusePhysicalWriteRegisters(pAd, Offset+i, 2, &pValueX[i/2]);
}
}
NTSTATUS eFuseWriteRegisters(
IN PRTBTH_ADAPTER pAd,
IN USHORT Offset,
IN USHORT Length,
IN USHORT* pData)
{
UINT16 i;
UINT16 eFuseData;
UINT16 LogicalAddress, BlkNum = 0xffff;
UINT8 EFSROM_AOUT;
//BOOLEAN bUpdateBlock = FALSE;
UINT16 addr,tmpaddr, InBuf[3], tmpOffset;
UINT16 buffer[8];
BOOLEAN bWriteSuccess = TRUE;
ULONG LoopCount = 0;
DebugPrint(TRACE, DBG_HW_ACCESS, "eFuseWriteRegisters Offset=0x%X, Length=%d, pData=0x%X\n", Offset, Length,*pData);
//Step 0. find the entry in the mapping table
tmpOffset = Offset & 0xfffe;
EFSROM_AOUT = eFuseReadRegisters(pAd, tmpOffset, 2, &eFuseData);
if( EFSROM_AOUT == 0x3f)
{
//the logical address does not exist, find an empty one
for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
{
eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
DebugPrint(TRACE, DBG_HW_ACCESS, "eFusePhysicalReadRegisters() read pool addr=0x%04X, value=0x%X\n", i, LogicalAddress);
/* Choose Not Used Row Index */
if( (LogicalAddress & 0xff) == 0)
{
BlkNum = i-EFUSE_USAGE_MAP_START;
break;
}
else if(( (LogicalAddress >> 8) & 0xff) == 0)
{
if (i != EFUSE_USAGE_MAP_END)
{
BlkNum = i+1-EFUSE_USAGE_MAP_START;
}
break;
}
}
}
else
{
BlkNum = EFSROM_AOUT;
}
DebugPrint(TRACE, DBG_HW_ACCESS, "eFuseWriteRegisters BlkNum = 0x%02X, EFSROM_AOUT= 0x%02X \n", BlkNum, EFSROM_AOUT);
if(BlkNum == 0xffff)
{
DebugPrint(ERROR, DBG_HW_ACCESS, "eFuseWriteRegisters: out of free E-fuse space!!!\n");
return STATUS_UNSUCCESSFUL;
}
//Step 1. Save data of this block
// read and save the original block data (16 bytes)
for(i =0; i<8; i++)
{
addr = BlkNum * 0x10 ;
InBuf[0] = addr+2*i;
InBuf[1] = 2;
InBuf[2] = 0x0;
eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
buffer[i] = InBuf[2];
}
//Step 2. Update the data in buffer, and write the data to Efuse
//Need new block but no free eFuse space
if ((buffer[ (Offset >> 1) % 8] | pData[0]) != pData[0])
{
//
// This means we need to find a new block for saving this value
// Check if we can find a new one, EFUSE_USAGE_MAP_END is the lastest block point to Block #29 (EFUSE_USAGE_MAP_START ~ EFUSE_USAGE_MAP_END)
//
eFusePhysicalReadRegisters(pAd, EFUSE_USAGE_MAP_END, 2, &LogicalAddress);
if( (LogicalAddress & 0xff) != 0)
{
// There is no empty one for update EFuse
return STATUS_UNSUCCESSFUL;
}
}
//Update to Target word (two bytes)
buffer[ (Offset >> 1) % 8] = pData[0];
do
{
//Step 3. Write the data to Efuse
DebugPrint(TRACE, DBG_HW_ACCESS, "Write the data to Efuse:\n");
if(!bWriteSuccess)
{
for(i =0; i<8; i++)
{
addr = BlkNum * 0x10 ;
InBuf[0] = addr+2*i;
InBuf[1] = 2;
InBuf[2] = buffer[i];
eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 2);
}
}
else
{
DebugPrint(TRACE, DBG_HW_ACCESS, "!!!Check it, Write Remainder bytes at address 0x%X, pData=0x%X \n", InBuf[0], InBuf[2]);
addr = BlkNum * 0x10 ;
InBuf[0] = addr+(Offset % 16);
InBuf[1] = 2;
InBuf[2] = pData[0];
eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 2);
}
//Step 4. Write mapping table
addr = EFUSE_USAGE_MAP_START+BlkNum;
tmpaddr = addr;
if(addr % 2 != 0)
addr = addr -1;
InBuf[0] = addr;
InBuf[1] = 2;
//convert the address from 10 to 8 bit ( bit7, 6 = parity and bit5 ~ 0 = bit9~4), and write to logical map entry
tmpOffset = Offset;
/* Convert to Row address */
tmpOffset >>= 4;
tmpOffset |= ((~((tmpOffset & 0x01) ^ ( tmpOffset >> 1 & 0x01) ^ (tmpOffset >> 2 & 0x01) ^ (tmpOffset >> 3 & 0x01))) << 6) & 0x40;
tmpOffset |= ((~( (tmpOffset >> 2 & 0x01) ^ (tmpOffset >> 3 & 0x01) ^ (tmpOffset >> 4 & 0x01) ^ ( tmpOffset >> 5 & 0x01))) << 7) & 0x80;
// write the logical address
if(tmpaddr%2 != 0)
InBuf[2] = tmpOffset<<8;
else
InBuf[2] = tmpOffset;
DebugPrint(TRACE, DBG_HW_ACCESS, "Write mapping table:\n");
eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 0);
//Step 5. Compare data if not the same, invalidate the mapping entry, then re-write the data until E-fuse is exhausted
bWriteSuccess = TRUE;
for(i =0; i<8; i++)
{
addr = BlkNum * 0x10 ;
InBuf[0] = addr+2*i;
InBuf[1] = 2;
InBuf[2] = 0x0;
eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
if(buffer[i] != InBuf[2])
{
bWriteSuccess = FALSE;
break;
}
}
//Step 6. invlidate mapping entry and find a free mapping entry if not succeed
if (!bWriteSuccess)
{
DebugPrint(TRACE, DBG_HW_ACCESS, "Not bWriteSuccess BlkNum = %d\n", BlkNum);
// the offset of current mapping entry
addr = EFUSE_USAGE_MAP_START+BlkNum;
//find a new mapping entry
BlkNum = 0xffff;
for (i=EFUSE_USAGE_MAP_START; i<=EFUSE_USAGE_MAP_END; i+=2)
{
eFusePhysicalReadRegisters(pAd, i, 2, &LogicalAddress);
if( (LogicalAddress & 0xff) == 0)
{
BlkNum = i-EFUSE_USAGE_MAP_START;
break;
}
else if(( (LogicalAddress >> 8) & 0xff) == 0)
{
if (i != EFUSE_USAGE_MAP_END)
{
BlkNum = i+1-EFUSE_USAGE_MAP_START;
}
break;
}
}
DebugPrint(TRACE, DBG_HW_ACCESS, "Not bWriteSuccess new BlkNum = %d\n", BlkNum);
if(BlkNum == 0xffff)
{
DebugPrint(TRACE, DBG_HW_ACCESS, "eFuseWriteRegisters: out of free E-fuse space!!!\n");
return STATUS_UNSUCCESSFUL;
}
//invalidate the original mapping entry if new entry is not found
tmpaddr = addr;
if(addr % 2 != 0)
addr = addr -1;
InBuf[0] = addr;
InBuf[1] = 2;
eFuseReadPhysical(pAd, &InBuf[0], 4, &InBuf[2], 2);
// write the logical address
if(tmpaddr%2 != 0)
{
// Invalidate the high byte
for (i=8; i<15; i++)
{
if( ( (InBuf[2] >> i) & 0x01) == 0)
{
InBuf[2] |= (0x1 <<i);
break;
}
}
}
else
{
// invalidate the low byte
for (i=0; i<8; i++)
{
if( ( (InBuf[2] >> i) & 0x01) == 0)
{
InBuf[2] |= (0x1 <<i);
break;
}
}
}
eFuseWritePhysical(pAd, &InBuf[0], 6, NULL, 0);
}
LoopCount++;
} while ((!bWriteSuccess) && (LoopCount < 200) && (!RT_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)));
return STATUS_SUCCESS;
}
NTSTATUS eFuseWrite(
IN RTBTH_ADAPTER *pAd,
IN USHORT Offset,
IN UCHAR *pData,
IN USHORT length)
{
//int i;
USHORT i;
NTSTATUS Status = STATUS_SUCCESS;
USHORT *pValueX = (USHORT *) pData; //value ...
for(i=0; i<length; i+=2)
{
eFuseWriteRegisters(pAd, Offset+i, 2, &pValueX[i/2]);
}
return Status;
}
VOID RaiseClock(
IN PRTBTH_ADAPTER pAd,
IN ULONG *x)
{
*x = *x | EESK;
RT_IO_WRITE32(pAd, E2PROM_CSR, *x);
rtbt_usec_delay(1); // Max frequency = 1MHz in Spec. definition
}
VOID LowerClock(
IN PRTBTH_ADAPTER pAd,
IN ULONG *x)
{
*x = *x & ~EESK;
RT_IO_WRITE32(pAd, E2PROM_CSR, *x);
rtbt_usec_delay(1);
}
USHORT ShiftInBits(
IN PRTBTH_ADAPTER pAd)
{
ULONG x,i;
USHORT data=0;
RT_IO_READ32(pAd, E2PROM_CSR, &x);
x &= ~( EEDO | EEDI);
for(i=0; i<16; i++)
{
data = data << 1;
RaiseClock(pAd, &x);
RT_IO_READ32(pAd, E2PROM_CSR, &x);
LowerClock(pAd, &x);
x &= ~(EEDI);
if(x & EEDO)
data |= 1;
}
return data;
}
VOID ShiftOutBits(
IN PRTBTH_ADAPTER pAd,
IN USHORT data,
IN USHORT count)
{
ULONG x,mask;
mask = 0x01 << (count - 1);
RT_IO_READ32(pAd, E2PROM_CSR, &x);
x &= ~(EEDO | EEDI);
do
{
x &= ~EEDI;
if(data & mask)
x |= EEDI;
RT_IO_WRITE32(pAd, E2PROM_CSR, x);
RaiseClock(pAd, &x);
LowerClock(pAd, &x);
mask = mask >> 1;
} while(mask);
x &= ~EEDI;
RT_IO_WRITE32(pAd, E2PROM_CSR, x);
}
VOID EEpromCleanup(
IN PRTBTH_ADAPTER pAd)
{
ULONG x;
RT_IO_READ32(pAd, E2PROM_CSR, &x);
x &= ~(EECS | EEDI);
RT_IO_WRITE32(pAd, E2PROM_CSR, x);
RaiseClock(pAd, &x);
LowerClock(pAd, &x);
}
VOID EWEN(
IN PRTBTH_ADAPTER pAd)
{
ULONG x;
// reset bits and set EECS
RT_IO_READ32(pAd, E2PROM_CSR, &x);
x &= ~(EEDI | EEDO | EESK);
x |= EECS;
RT_IO_WRITE32(pAd, E2PROM_CSR, x);
// kick a pulse
RaiseClock(pAd, &x);
LowerClock(pAd, &x);
// output the read_opcode and six pulse in that order
ShiftOutBits(pAd, EEPROM_EWEN_OPCODE, 5);
ShiftOutBits(pAd, 0, 6);
EEpromCleanup(pAd);
}
VOID EWDS(
IN PRTBTH_ADAPTER pAd)
{
ULONG x;
// reset bits and set EECS
RT_IO_READ32(pAd, E2PROM_CSR, &x);
x &= ~(EEDI | EEDO | EESK);
x |= EECS;
RT_IO_WRITE32(pAd, E2PROM_CSR, x);
// kick a pulse
RaiseClock(pAd, &x);
LowerClock(pAd, &x);
// output the read_opcode and six pulse in that order
ShiftOutBits(pAd, EEPROM_EWDS_OPCODE, 5);
ShiftOutBits(pAd, 0, 6);
EEpromCleanup(pAd);
}
int rtbt_prom_read16(
IN PRTBTH_ADAPTER pAd,
IN USHORT Offset,
OUT USHORT *pData)
{
ULONG x;
Offset /= 2;
// reset bits and set EECS
RT_IO_READ32(pAd, E2PROM_CSR, &x);
x &= ~(EEDI | EEDO | EESK);
x |= EECS;
RT_IO_WRITE32(pAd, E2PROM_CSR, x);
// kick a pulse
RaiseClock(pAd, &x);
LowerClock(pAd, &x);
// output the read_opcode and register number in that order
ShiftOutBits(pAd, EEPROM_READ_OPCODE, 3);
ShiftOutBits(pAd, Offset, EEPROM_ADDRESS_NUM);
// Now read the data (16 bits) in from the selected EEPROM word
*pData = ShiftInBits(pAd);
EEpromCleanup(pAd);
return 0;
}
int rtbt_prom_write16(
IN PRTBTH_ADAPTER pAd,
IN USHORT Offset,
IN USHORT Data)
{
ULONG x;
Offset /= 2;
EWEN(pAd);
// reset bits and set EECS
RT_IO_READ32(pAd, E2PROM_CSR, &x);
x &= ~(EEDI | EEDO | EESK);
x |= EECS;
RT_IO_WRITE32(pAd, E2PROM_CSR, x);
// kick a pulse
RaiseClock(pAd, &x);
LowerClock(pAd, &x);
// output the read_opcode ,register number and data in that order
ShiftOutBits(pAd, EEPROM_WRITE_OPCODE, 3);
ShiftOutBits(pAd, Offset, EEPROM_ADDRESS_NUM);
ShiftOutBits(pAd, Data, 16); // 16-bit access
// read DO status
RT_IO_READ32(pAd, E2PROM_CSR, &x);
EEpromCleanup(pAd);
rtbt_usec_delay(10000); //delay for twp(MAX)=10ms
EWDS(pAd);
EEpromCleanup(pAd);
return 0;
}
USHORT Bth_EEPROM_READ16(
IN RTBTH_ADAPTER *pAd,
IN USHORT Offset)
{
ULONG value = 0;
USHORT data;
LONG count=0;
RT_IO_READ32(pAd, BT_FUNC_INFO, &value);
while((value & 0x80000000) && (count <=100))
{
count++;
rtbt_usec_delay(100000);
RT_IO_READ32(pAd, BT_FUNC_INFO, &value);
if(value == 0xffffffff)
{
DebugPrint(TRACE, DBG_HW_ACCESS,
"Bth_EEPROM_READ16: Card maybe not exist or bt_func not enable bt_fun= %x\n",pAd->btFunCtrl.word);
break;
}
}
RT_IO_READ32(pAd, WLAN_FUN_INFO, &value);
RT_IO_WRITE32(pAd, WLAN_FUN_INFO, value | 0x80000000);
if (pAd->bUseEfuse)
{
eFuseRead(pAd, Offset, (UINT8 *)&data, 2);
}
else
{
rtbt_prom_read16(pAd, Offset, &data);
}
value &= (~0x80000000);
RT_IO_WRITE32(pAd, WLAN_FUN_INFO, value);
return data;
}
VOID Bth_EEPROM_WRITE16(
IN PRTBTH_ADAPTER pAd,
IN USHORT Offset,
IN USHORT Data)
{
ULONG value = 0;
LONG count=0;
RT_IO_READ32(pAd, BT_FUNC_INFO, &value);
while((value & 0x80000000) && (count<=100))
{
count++;
rtbt_usec_delay(100000);
RT_IO_READ32(pAd, BT_FUNC_INFO, &value);
if(value == 0xffffffff)
{
DebugPrint(TRACE, DBG_HW_ACCESS,
"%s():Card maybe not exist or bt_func not enable bt_fun= %x\n",
__FUNCTION__, pAd->btFunCtrl.word);
break;
}
}
RT_IO_READ32(pAd, WLAN_FUN_INFO, &value);
RT_IO_WRITE32(pAd, WLAN_FUN_INFO, value | 0x80000000);
if(pAd->bUseEfuse)
{
eFuseWrite(pAd, Offset, (PUCHAR) &Data, 2);
}
else
{
rtbt_prom_write16(pAd, Offset, Data);
}
value &= (~0x80000000);
RT_IO_WRITE32(pAd, WLAN_FUN_INFO, value);
}
VOID dump_mac_reg(IN RTBTH_ADAPTER *pAd)
{
UINT32 mac_val;
// UINT32 mac_reg; //sean wang linux: warning: unused variable ‘mac_reg’
int cnt;
DebugPrint(TRACE, DBG_MISC, "Dump Mac Registers:\n");
for (cnt = 0; cnt < sizeof(RT3298_REG_ADDR)/sizeof(UINT32);)
{
RT_IO_FORCE_READ32(pAd, RT3298_REG_ADDR[cnt], &mac_val);
DebugPrint(TRACE, DBG_MISC, "0x%x=0x%x\t", RT3298_REG_ADDR[cnt], mac_val);
if (((++cnt) % 4) == 0)
DebugPrint(TRACE, DBG_MISC, "\n");
}
DebugPrint(TRACE, DBG_MISC, "\n-------\n");
}
void BthInitializePrerequire(IN RTBTH_ADAPTER *pAd)
{
ULONG Index, Value;
USHORT usValue;
UCHAR ucValue;
EFUSE_CTRL_STRUC eFuseCtrl;
OSCCTL_STRUC osCtrl;
DebugPrint(TRACE, DBG_INIT, " -->%s()\n", __FUNCTION__);
DebugPrint(TRACE, DBG_INIT, " Dump MAC registers before call BthEnableBtFunc\n");
dump_mac_reg(pAd);
// read BT_FUN_CTRL as default value
RT_IO_FORCE_READ32(pAd, BT_FUN_CTRL, &pAd->btFunCtrl.word);
DebugPrint(TRACE, DBG_INIT, "btFunCtrl.work = 0x08%x\n", pAd->btFunCtrl.word);
BthEnableBtFunc(pAd);
DebugPrint(TRACE, DBG_INIT, " Dump MAC registers After call BthEnableBtFunc\n");
dump_mac_reg(pAd);
RT_IO_READ32(pAd, BT_FUN_CTRL, &Value);
Value |= 0x8;
RT_IO_WRITE32(pAd, BT_FUN_CTRL, Value);
rtbt_usec_delay(10);
Value &= ~0x8;
RT_IO_WRITE32(pAd, BT_FUN_CTRL, Value);
//Enable ROSC_EN first then CAL_REQ