-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patheep_5211.c
1574 lines (1347 loc) · 47.4 KB
/
eep_5211.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
/*
* Copyright (c) 2013,2016-2020 Sergey Ryazanov <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "atheepmgr.h"
#include "utils.h"
#include "eep_common.h"
#include "eep_5211.h"
struct eep_5211_priv {
struct eep_5211_param { /* Cached EEPROM parameters */
int eepmap; /* EEPROM map type */
int pdcal_off; /* PD calibration info offset */
int tgtpwr_off; /* Target power info offset */
struct eep_5211_pdcal_param {
const uint8_t *piers;
int npiers;
int8_t gains[AR5211_MAX_PDCAL_GAINS];/* dB */
int ngains;
int nicepts[AR5211_MAX_PDCAL_GAINS];
} pdcal_a, pdcal_b, pdcal_g; /* PD calibration params */
int ctls_num; /* Max number of CTLs */
} param;
struct ar5211_init_eep_data ini;
struct ar5211_eeprom eep;
};
#define EEP_WORD(__off) le16toh(aem->eep_buf[__off])
/* EEPROM as a bitstream processing context */
struct eep_bit_stream {
int eep_off; /* EEPROM offset, words */
int havebits; /* Ammount of bits in the buffer */
uint32_t buf; /* Buffer */
};
/**
* EEPROM parameter fields can occupy an arbitrary number of bits. To simplify
* the code, we use a function, which reads EEPROM in a word-by-word manner,
* stores data which was read in the buffer, and returns an arbitrary number
* of bits in each call.
*
* Some EEPROM fields begin in one word, cross the word boundary and continue
* in the next one. There are two ways to store bits of such fields:
* 1. the LSB(s) of the field are stored in the MSB(s) of the current word, and
* the MSB(s) of the field are stored in the LSB(s) of the next word of
* EEPROM;
* 2. the MSB(s) of the field are stored in the LSB(s) of the current word, and
* the LSB(s) of the field are stored in the MSB(s) of the next word of
* EEPROM.
*
* Since both approaches are actively used, we need two types of the bits
* fetching functions:
* 1. function of the first type appends the next read EEPROM word to the buffer
* to the right (places the new word as LSB of the buffer) and returns
* requested number of MSBs of the buffer;
* 2. function of the second type appends the next read EEPROM word to the buffer
* to the left (places the new word as MSB of the buffer) and returns
* requested number of MSBs of the buffer.
*/
static uint8_t ebs_get_hi_bits(struct atheepmgr *aem,
struct eep_bit_stream *ebs, int bnum)
{
uint8_t res;
if (ebs->havebits < bnum) {
ebs->buf = (ebs->buf << 16) | EEP_WORD(ebs->eep_off++);
ebs->havebits += 16;
}
ebs->havebits -= bnum;
res = (ebs->buf >> ebs->havebits) & ~(~0 << bnum);
ebs->buf &= ~(~0 << ebs->havebits);
return res;
}
static uint8_t ebs_get_lo_bits(struct atheepmgr *aem,
struct eep_bit_stream *ebs, int bnum)
{
uint8_t res;
if (ebs->havebits < bnum) {
ebs->buf |= EEP_WORD(ebs->eep_off++) << ebs->havebits;
ebs->havebits += 16;
}
res = ebs->buf & ~(~0 << bnum);
ebs->buf >>= bnum;
ebs->havebits -= bnum;
return res;
}
#define EEP_GET_MSB(__bnum) ebs_get_hi_bits(aem, ebs, __bnum)
#define EEP_GET_LSB(__bnum) ebs_get_lo_bits(aem, ebs, __bnum)
static void eep_5211_fill_init_data(struct atheepmgr *aem)
{
struct eep_5211_priv *emp = aem->eepmap_priv;
struct ar5211_init_eep_data *ini = &emp->ini;
struct ar5211_pci_eep_data *pci = &ini->pci;
struct ar5211_eeprom *eep = &emp->eep;
uint16_t word;
/* Copy data from buffer to PCI initialization structure */
memcpy(pci, &aem->eep_buf[AR5211_EEP_PCI_DATA], sizeof(*pci));
word = EEP_WORD(AR5211_EEP_ENDLOC_UP);
ini->eepsz = 2 << (MS(word, AR5211_EEP_ENDLOC_SIZE) + 9);
if (word)
ini->eeplen = MS(word, AR5211_EEP_ENDLOC_LOC) << 16 |
EEP_WORD(AR5211_EEP_ENDLOC_LO);
else
ini->eeplen = 0;
ini->magic = EEP_WORD(AR5211_EEP_MAGIC);
ini->prot = EEP_WORD(AR5211_EEP_PROT);
/* Copy customer data */
memcpy(eep->cust_data, &aem->eep_buf[AR5211_EEP_CUST_DATA],
AR5211_EEP_CUST_DATA_SZ * sizeof(uint16_t));
}
static void eep_5211_fill_headers_30(struct atheepmgr *aem)
{
struct eep_5211_priv *emp = aem->eepmap_priv;
struct ar5211_eeprom *eep = &emp->eep;
struct ar5211_base_eep_hdr *base = &eep->base;
uint16_t word;
emp->param.pdcal_off = AR5211_EEP_PDCAL_BASE_30;
emp->param.tgtpwr_off = AR5211_EEP_TGTPWR_BASE_30;
word = EEP_WORD(AR5211_EEP_ANTGAIN_30);
base->antgain_2g = MS(word, AR5211_EEP_ANTGAIN_2G);
base->antgain_5g = MS(word, AR5211_EEP_ANTGAIN_5G);
}
static void eep_5211_fill_headers_33(struct atheepmgr *aem)
{
struct eep_5211_priv *emp = aem->eepmap_priv;
struct ar5211_eeprom *eep = &emp->eep;
struct ar5211_base_eep_hdr *base = &eep->base;
uint16_t word;
emp->param.pdcal_off = AR5211_EEP_PDCAL_BASE_33;
emp->param.tgtpwr_off = AR5211_EEP_TGTPWR_BASE_33;
word = EEP_WORD(AR5211_EEP_ANTGAIN_33);
base->antgain_2g = MS(word, AR5211_EEP_ANTGAIN_2G);
base->antgain_5g = MS(word, AR5211_EEP_ANTGAIN_5G);
if (base->version >= AR5211_EEP_VER_4_0) {
word = EEP_WORD(AR5211_EEP_MISC0);
base->ear_off = MS(word, AR5211_EEP_EAR_OFF);
base->xr2_dis = !!(word & AR5211_EEP_XR2_DIS);
base->xr5_dis = !!(word & AR5211_EEP_XR5_DIS);
base->eepmap = MS(word, AR5211_EEP_EEPMAP);
emp->param.eepmap = base->eepmap;
word = EEP_WORD(AR5211_EEP_MISC1);
base->tgtpwr_off = MS(word, AR5211_EEP_TGTPWR_OFF);
base->exists_32khz = !!(word & AR5211_EEP_32KHZ);
emp->param.tgtpwr_off = base->tgtpwr_off;
word = EEP_WORD(AR5211_EEP_SRC_INFO0);
base->ear_file_ver = MS(word, AR5211_EEP_EAR_FILE_VER);
base->eep_file_ver = MS(word, AR5211_EEP_EEP_FILE_VER);
word = EEP_WORD(AR5211_EEP_SRC_INFO1);
base->ear_file_id = MS(word, AR5211_EEP_EAR_FILE_ID);
base->art_build_num = MS(word, AR5211_EEP_ART_BUILD);
}
if (base->version >= AR5211_EEP_VER_5_0) {
word = EEP_WORD(AR5211_EEP_MISC4);
base->cal_off = MS(word, AR5211_EEP_CAL_OFF);
emp->param.pdcal_off = base->cal_off;
word = EEP_WORD(AR5211_EEP_CAPABILITIES);
base->comp_dis = !!(word & AR5211_EEP_COMP_DIS);
base->aes_dis = !!(word & AR5211_EEP_AES_DIS);
base->ff_dis = !!(word & AR5211_EEP_FF_DIS);
base->burst_dis = !!(word & AR5211_EEP_BURST_DIS);
base->max_qcu = MS(word, AR5211_EEP_MAX_QCU);
base->clip_en = !(word & AR5211_EEP_CLIP_EN);
word = EEP_WORD(AR5211_EEP_REGCAP);
base->rd_flags = MS(word, AR5211_EEP_RD_FLAGS);
}
}
static void eep_5211_parse_modal_cmn1(struct atheepmgr *aem,
struct eep_bit_stream *ebs,
struct ar5211_modal_eep_hdr *modal)
{
int i;
EEP_GET_MSB(1); /* Take away unused bit */
modal->sw_settle_time = EEP_GET_MSB(7);
modal->txrx_atten = EEP_GET_MSB(6);
for (i = 0; i < ARRAY_SIZE(modal->ant_ctrl); ++i)
modal->ant_ctrl[i] = EEP_GET_MSB(6);
modal->adc_desired_size = EEP_GET_MSB(8);
}
static void eep_5211_parse_modal_cmn2(struct atheepmgr *aem,
struct eep_bit_stream *ebs,
struct ar5211_modal_eep_hdr *modal)
{
modal->tx_end_to_xlna_on = EEP_GET_MSB(8);
modal->thresh62 = EEP_GET_MSB(8);
modal->tx_end_to_xpa_off = EEP_GET_MSB(8);
modal->tx_frame_to_xpa_on = EEP_GET_MSB(8);
modal->pga_desired_size = EEP_GET_MSB(8);
modal->nfthresh = EEP_GET_MSB(8);
EEP_GET_MSB(2); /* Skip unused bits */
modal->fixed_bias = EEP_GET_MSB(1); /* A & G only */
modal->xlna_gain = EEP_GET_MSB(8);
modal->xpd_gain = EEP_GET_MSB(4);
modal->xpd = EEP_GET_MSB(1);
}
static void eep_5211_parse_modal_a(struct atheepmgr *aem, int off)
{
struct eep_5211_priv *emp = aem->eepmap_priv;
struct ar5211_eeprom *eep = &emp->eep;
struct ar5211_modal_eep_hdr *modal = &eep->modal_a;
struct eep_bit_stream __ebs = {.eep_off = off, .havebits = 0};
struct eep_bit_stream *ebs = &__ebs;
int i;
eep_5211_parse_modal_cmn1(aem, ebs, modal);
for (i = 0; i < 4; ++i) {
modal->pa_ob[3 - i] = EEP_GET_MSB(3);
modal->pa_db[3 - i] = EEP_GET_MSB(3);
}
eep_5211_parse_modal_cmn2(aem, ebs, modal);
if (eep->base.version < AR5211_EEP_VER_3_3)
return;
/* Here we change the bits fetching direction */
modal->xr_tgt_pwr = EEP_GET_LSB(6);
modal->false_detect_backoff = EEP_GET_LSB(7);
if (eep->base.version < AR5211_EEP_VER_3_4)
return;
modal->pd_gain_init = EEP_GET_LSB(6);
if (eep->base.version < AR5211_EEP_VER_4_0)
return;
modal->iq_cal_q = EEP_GET_LSB(5);
modal->iq_cal_i = EEP_GET_LSB(6);
EEP_GET_LSB(2); /* Skip unused bits */
if (eep->base.version < AR5211_EEP_VER_4_1)
return;
modal->rxtx_margin = EEP_GET_LSB(6);
if (eep->base.version < AR5211_EEP_VER_5_0)
return;
modal->turbo_sw_settle_time = EEP_GET_LSB(7);
modal->turbo_txrx_atten = EEP_GET_LSB(6);
modal->turbo_rxtx_margin = EEP_GET_LSB(6);
modal->turbo_adc_desired_size = EEP_GET_LSB(8);
modal->turbo_pga_desired_size = EEP_GET_LSB(8);
}
static void eep_5211_parse_modal_b(struct atheepmgr *aem, int off)
{
struct eep_5211_priv *emp = aem->eepmap_priv;
struct ar5211_eeprom *eep = &emp->eep;
struct ar5211_modal_eep_hdr *modal = &eep->modal_b;
struct eep_bit_stream __ebs = {.eep_off = off, .havebits = 0};
struct eep_bit_stream *ebs = &__ebs;
eep_5211_parse_modal_cmn1(aem, ebs, modal);
modal->pa_ob[0] = EEP_GET_MSB(4) & 0x07;
modal->pa_db[0] = EEP_GET_MSB(4) & 0x07;
eep_5211_parse_modal_cmn2(aem, ebs, modal);
if (eep->base.version < AR5211_EEP_VER_3_3)
return;
/* Here we change the bits fetching direction */
modal->pa_ob_2ghz = EEP_GET_LSB(3);
modal->pa_db_2ghz = EEP_GET_LSB(3);
modal->false_detect_backoff = EEP_GET_LSB(7);
if (eep->base.version < AR5211_EEP_VER_3_4)
return;
modal->pd_gain_init = EEP_GET_LSB(6);
EEP_GET_LSB(13); /* Skip unused bits */
if (eep->base.version < AR5211_EEP_VER_4_0)
return;
modal->cal_piers[0] = EEP_GET_LSB(8);
modal->cal_piers[1] = EEP_GET_LSB(8);
modal->cal_piers[2] = EEP_GET_LSB(8);
if (eep->base.version < AR5211_EEP_VER_4_1)
return;
modal->rxtx_margin = EEP_GET_LSB(6);
}
static void eep_5211_parse_modal_g(struct atheepmgr *aem, int off)
{
struct eep_5211_priv *emp = aem->eepmap_priv;
struct ar5211_eeprom *eep = &emp->eep;
struct ar5211_modal_eep_hdr *modal = &eep->modal_g;
struct eep_bit_stream __ebs = {.eep_off = off, .havebits = 0};
struct eep_bit_stream *ebs = &__ebs;
uint8_t ch14_filter_cck_delta, rxtx_margin;
eep_5211_parse_modal_cmn1(aem, ebs, modal);
modal->pa_ob[0] = EEP_GET_MSB(4) & 0x07;
modal->pa_db[0] = EEP_GET_MSB(4) & 0x07;
eep_5211_parse_modal_cmn2(aem, ebs, modal);
if (eep->base.version < AR5211_EEP_VER_3_3)
return;
/* Here we change the bits fetching direction */
modal->pa_ob_2ghz = EEP_GET_LSB(3);
modal->pa_db_2ghz = EEP_GET_LSB(3);
modal->false_detect_backoff = EEP_GET_LSB(7);
if (eep->base.version < AR5211_EEP_VER_3_4)
return;
modal->pd_gain_init = EEP_GET_LSB(6);
modal->cck_ofdm_pwr_delta = EEP_GET_LSB(8);
if (eep->base.version < AR5211_EEP_VER_4_0)
return;
ch14_filter_cck_delta = EEP_GET_LSB(5);
modal->cal_piers[0] = EEP_GET_LSB(8);
modal->cal_piers[1] = EEP_GET_LSB(8);
modal->turbo_maxtxpwr_2w = EEP_GET_LSB(7);
modal->xr_tgt_pwr = EEP_GET_LSB(6);
EEP_GET_LSB(3); /* Skip unused bits */
modal->cal_piers[2] = EEP_GET_LSB(8);
rxtx_margin = EEP_GET_LSB(6); /* Preserve bits for a while */
EEP_GET_LSB(2); /* Skip unused bits */
modal->iq_cal_q = EEP_GET_LSB(5);
modal->iq_cal_i = EEP_GET_LSB(6);
EEP_GET_LSB(5); /* Skip unused bits */
if (eep->base.version < AR5211_EEP_VER_4_1)
return;
modal->rxtx_margin = rxtx_margin;
if (eep->base.version < AR5211_EEP_VER_4_2)
return;
modal->cck_ofdm_gain_delta = EEP_GET_LSB(8);
if (eep->base.version < AR5211_EEP_VER_4_6)
return;
modal->ch14_filter_cck_delta = ch14_filter_cck_delta;
if (eep->base.version < AR5211_EEP_VER_5_0)
return;
modal->turbo_sw_settle_time = EEP_GET_LSB(7);
modal->turbo_txrx_atten = EEP_GET_LSB(6);
modal->turbo_rxtx_margin = EEP_GET_LSB(6);
modal->turbo_adc_desired_size = EEP_GET_LSB(8);
modal->turbo_pga_desired_size = EEP_GET_LSB(8);
}
/**
* This data is stored after the CTL index information and it acomplishes data
* fetched from the B & G modal headers.
*/
static void eep_5211_parse_modal_ext_31(struct atheepmgr *aem)
{
struct eep_5211_priv *emp = aem->eepmap_priv;
struct ar5211_eeprom *eep = &emp->eep;
struct ar5211_modal_eep_hdr *modal_b = &eep->modal_b;
struct ar5211_modal_eep_hdr *modal_g = &eep->modal_g;
int off = AR5211_EEP_MODAL_EXT_31;
uint16_t word;
word = EEP_WORD(off++);
modal_b->pa_ob_2ghz = (word & 0x07) >> 0;
modal_b->pa_db_2ghz = (word & 0x38) >> 3;
word = EEP_WORD(off++);
modal_g->pa_ob_2ghz = (word & 0x07) >> 0;
modal_g->pa_db_2ghz = (word & 0x38) >> 3;
}
static void eep_5211_fill_headers(struct atheepmgr *aem)
{
struct eep_5211_priv *emp = aem->eepmap_priv;
struct ar5211_eeprom *eep = &emp->eep;
struct ar5211_base_eep_hdr *base = &eep->base;
struct ar5211_modal_eep_hdr *modal_a = &eep->modal_a;
uint16_t word;
word = EEP_WORD(AR5211_EEP_MAC + 0);
base->mac[4] = (word & 0xff00) >> 8;
base->mac[5] = (word & 0x00ff) >> 0;
word = EEP_WORD(AR5211_EEP_MAC + 1);
base->mac[2] = (word & 0xff00) >> 8;
base->mac[3] = (word & 0x00ff) >> 0;
word = EEP_WORD(AR5211_EEP_MAC + 2);
base->mac[0] = (word & 0xff00) >> 8;
base->mac[1] = (word & 0x00ff) >> 0;
base->regdomain = EEP_WORD(AR5211_EEP_REGDOMAIN);
base->checksum = EEP_WORD(AR5211_EEP_CSUM);
base->version = EEP_WORD(AR5211_EEP_VER);
word = EEP_WORD(AR5211_EEP_OPFLAGS);
base->amode_en = !!(word & AR5211_EEP_AMODE);
base->bmode_en = !!(word & AR5211_EEP_BMODE);
base->gmode_en = !!(word & AR5211_EEP_GMODE);
base->turbo2_dis = !!(word & AR5211_EEP_TURBO2_DIS);
modal_a->turbo_maxtxpwr_2w = MS(word, AR5211_EEP_TURBO5_MAXPWR);
base->devtype = MS(word, AR5211_EEP_DEVTYPE);
base->rfkill_en = !!(word & AR5211_EEP_RFKILL_EN);
base->turbo5_dis = !!(word & AR5211_EEP_TURBO5_DIS);
if (base->version >= AR5211_EEP_VER_3_3) {
eep_5211_fill_headers_33(aem);
eep_5211_parse_modal_a(aem, AR5211_EEP_MODAL_A_33);
eep_5211_parse_modal_b(aem, AR5211_EEP_MODAL_B_33);
eep_5211_parse_modal_g(aem, AR5211_EEP_MODAL_G_33);
} else if (base->version >= AR5211_EEP_VER_3_0) {
eep_5211_fill_headers_30(aem);
eep_5211_parse_modal_a(aem, AR5211_EEP_MODAL_A_30);
eep_5211_parse_modal_b(aem, AR5211_EEP_MODAL_B_30);
eep_5211_parse_modal_g(aem, AR5211_EEP_MODAL_G_30);
if (base->version >= AR5211_EEP_VER_3_1)
eep_5211_parse_modal_ext_31(aem);
}
}
/* Used only for map0 PD calibration data */
static void eep_5211_decode_xpd_gain(uint8_t eep_val,
struct eep_5211_pdcal_param *pdcp)
{
static const int8_t gains[] = {-1, -1, -1, -1, -1, -1, -1, 18, -1, -1,
-1, 12, -1, 6, 0, -1, -1, -1, -1, -1};
if (eep_val < ARRAY_SIZE(gains) && gains[eep_val] != -1) {
pdcp->gains[0] = gains[eep_val];
} else {
printf("Unknown xPD gain code 0x%02x, use 6 dB\n", eep_val);
pdcp->gains[0] = 6;
}
pdcp->ngains = 1;
}
/* Use for map1 & map2 PD calibration data */
static void eep_5211_parse_xpd_gain(uint8_t eep_val, const int8_t *map,
struct eep_5211_pdcal_param *pdcp)
{
int i, j;
for (i = 0, j = 0; i < AR5211_MAX_PDCAL_GAINS; ++i) {
if (!(eep_val & (1 << i)))
continue;
pdcp->gains[j++] = map[i];
}
pdcp->ngains = j;
}
static void eep_5211_count_pdcal_piers(const uint8_t *piers, int *npiers,
int maxpiers)
{
int i = 0;
while (piers[i] && ++i < maxpiers);
*npiers = i;
}
static void eep_5211_parse_pdcal_piers_30(struct atheepmgr *aem,
struct eep_bit_stream *ebs,
uint8_t *piers, int *npiers,
int maxpiers)
{
int i = 0;
do {
piers[i] = EEP_GET_MSB(7);
piers[i] = FBIN_30_TO_33(piers[i], 0);
} while (piers[i] && ++i < maxpiers);
*npiers = i;
for (++i; i < maxpiers; ++i)
EEP_GET_LSB(8); /* Read leftover */
EEP_GET_MSB(10); /* Skip unused bits */
}
static void eep_5211_parse_pdcal_piers_33(struct atheepmgr *aem,
struct eep_bit_stream *ebs,
uint8_t *piers, int *npiers,
int maxpiers)
{
int i = 0;
do {
piers[i] = EEP_GET_MSB(8);
} while (piers[i] && ++i < maxpiers);
*npiers = i;
for (++i; i < maxpiers; ++i)
EEP_GET_LSB(8); /* Read leftover */
}
static void eep_5211_parse_pdcal_piers_40(struct atheepmgr *aem,
struct eep_bit_stream *ebs,
uint8_t *piers, int *npiers,
int maxpiers)
{
int i = 0;
do {
piers[i] = EEP_GET_LSB(8);
} while (piers[i] && ++i < maxpiers);
*npiers = i;
for (++i; i < maxpiers; ++i)
EEP_GET_LSB(8); /* Read leftover */
}
/**
* Map 0 format does not store VPD value of each point, instead it stores
* min and max VPD values and output tx power levels measured at predefined
* VPD values, which are expressed as percents. E.g.:
* VPD0 = VPDmin + 0.00 * (VPDmax - VPDmin) // 0%
* VPD1 = VPDmin + 0.10 * (VPDmax - VPDmin) // 10%
* VPD2 = VPDmin + 0.20 * (VPDmax - VPDmin) // 20%
* ...
* VPD11 = VPDmin + 1.00 * (VPDmax - VPDmin) // 100%
*/
static void eep_5211_parse_pdcal_data_map0(struct atheepmgr *aem,
struct eep_bit_stream *ebs,
struct eep_5211_pdcal_param *pdcp,
struct ar5211_pier_pdcal *pdcal)
{
#define ICEPTS_NUM 11
static const unsigned int icepts_vpd_percent[2][ICEPTS_NUM] = {
{0, 5, 10, 20, 30, 50, 70, 85, 90, 95, 100}, /* < v3.0 */
{0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100} /* >= v3.2 */
};
struct eep_5211_priv *emp = aem->eepmap_priv;
struct ar5211_eeprom *eep = &emp->eep;
const unsigned int *vp;
unsigned int vpd_min, vpd_max;
int16_t *pwr;
uint8_t *vpd;
int i, j;
vp = eep->base.version < AR5211_EEP_VER_3_2 ? icepts_vpd_percent[0] :
icepts_vpd_percent[1];
for (i = 0; i < pdcp->npiers; ++i) {
pwr = pdcal[i].pwr[0];
vpd = pdcal[i].vpd[0];
vpd_max = EEP_GET_MSB(6);
vpd_min = EEP_GET_MSB(6);
for (j = 0; j < ICEPTS_NUM; ++j) {
pwr[j] = EEP_GET_MSB(6) * 2; /* 0.5 dB -> 0.25 dB */
vpd[j] = vpd_min + vp[j] * (vpd_max - vpd_min) / 100;
}
pdcp->nicepts[0] = j;
EEP_GET_MSB(2); /* Skip unused bits */
}
#undef ICEPTS_NUM
}
static void eep_5211_parse_pdcal_map0(struct atheepmgr *aem,
struct eep_bit_stream *ebs)
{
#define F2B(__freq) FREQ2FBIN(__freq, 1)
static const uint8_t piers_b[] = {F2B(2412), F2B(2447), F2B(2484)};
static const uint8_t piers_g[] = {F2B(2312), F2B(2412), F2B(2484)};
#undef F2B
struct eep_5211_priv *emp = aem->eepmap_priv;
struct ar5211_eeprom *eep = &emp->eep;
struct eep_5211_pdcal_param *pdcp;
pdcp = &emp->param.pdcal_a;
if (eep->base.version >= AR5211_EEP_VER_3_3)
eep_5211_parse_pdcal_piers_33(aem, ebs, eep->pdcal_piers_a,
&pdcp->npiers,
AR5211_NUM_PDCAL_PIERS_A);
else
eep_5211_parse_pdcal_piers_30(aem, ebs, eep->pdcal_piers_a,
&pdcp->npiers,
AR5211_NUM_PDCAL_PIERS_A);
pdcp->piers = eep->pdcal_piers_a;
eep_5211_decode_xpd_gain(eep->modal_a.xpd_gain, pdcp);
eep_5211_parse_pdcal_data_map0(aem, ebs, pdcp, eep->pdcal_data_a);
pdcp = &emp->param.pdcal_b;
pdcp->piers = piers_b; /* Fixed piers */
pdcp->npiers = ARRAY_SIZE(piers_b);
eep_5211_decode_xpd_gain(eep->modal_b.xpd_gain, pdcp);
eep_5211_parse_pdcal_data_map0(aem, ebs, pdcp, eep->pdcal_data_b);
pdcp = &emp->param.pdcal_g;
pdcp->piers = piers_g; /* Fixed piers */
pdcp->npiers = ARRAY_SIZE(piers_g);
eep_5211_decode_xpd_gain(eep->modal_g.xpd_gain, pdcp);
eep_5211_parse_pdcal_data_map0(aem, ebs, pdcp, eep->pdcal_data_g);
}
static void eep_5211_parse_pdcal_data_map1(struct atheepmgr *aem,
struct eep_bit_stream *ebs,
struct eep_5211_pdcal_param *pdcp,
struct ar5211_pier_pdcal *pdcal)
{
static const uint8_t hi_xpd_gain_vpd[3] = {20, 35, 63}; /* Fixed */
struct eep_5211_priv *emp = aem->eepmap_priv;
struct ar5211_eeprom *eep = &emp->eep;
int i, j;
uint8_t vpd_d[4]; /* VPD deltas */
/**
* NB: storage format always contains data for 2 xPD gains, even if
* only one is in use. So parse data for both gains and dumping code
* will ignore data for higher gain if only one gain is in use.
*/
for (i = 0; i < pdcp->npiers; ++i) {
pdcp->nicepts[0] = 4;
for (j = 0; j < pdcp->nicepts[0]; ++j)
pdcal[i].pwr[0][j] = (int8_t)EEP_GET_LSB(8);
for (j = 1; j < 4; ++j)
vpd_d[j] = EEP_GET_LSB(5);
EEP_GET_LSB(1); /* Skip unused bit */
pdcp->nicepts[1] = 3;
for (j = 0; j < pdcp->nicepts[1]; ++j)
pdcal[i].pwr[1][j] = (int8_t)EEP_GET_LSB(8);
if (eep->base.version < AR5211_EEP_VER_4_3) {
pdcal[i].vpd[0][0] = 1; /* Fixed VPD value */
EEP_GET_LSB(8); /* Skip max power value */
} else {
pdcal[i].vpd[0][0] = EEP_GET_LSB(6);
EEP_GET_LSB(2); /* Skip unused bits */
}
for (j = 1; j < 4; ++j)
pdcal[i].vpd[0][j] = pdcal[i].vpd[0][j - 1] + vpd_d[j];
for (j = 0; j < ARRAY_SIZE(hi_xpd_gain_vpd); ++j)
pdcal[i].vpd[1][j] = hi_xpd_gain_vpd[j];
}
}
static void eep_5211_parse_pdcal_map1(struct atheepmgr *aem,
struct eep_bit_stream *ebs)
{
static const int8_t gains_map[] = {0, 6, 12, 18};
struct eep_5211_priv *emp = aem->eepmap_priv;
struct ar5211_eeprom *eep = &emp->eep;
struct eep_5211_pdcal_param *pdcp;
if (eep->base.amode_en) {
pdcp = &emp->param.pdcal_a;
eep_5211_parse_pdcal_piers_40(aem, ebs, eep->pdcal_piers_a,
&pdcp->npiers,
AR5211_NUM_PDCAL_PIERS_A);
pdcp->piers = eep->pdcal_piers_a;
eep_5211_parse_xpd_gain(eep->modal_a.xpd_gain, gains_map, pdcp);
eep_5211_parse_pdcal_data_map1(aem, ebs, pdcp,
eep->pdcal_data_a);
}
if (eep->base.bmode_en) {
pdcp = &emp->param.pdcal_b;
eep_5211_count_pdcal_piers(eep->modal_b.cal_piers,
&pdcp->npiers,
ARRAY_SIZE(eep->modal_b.cal_piers));
pdcp->piers = eep->modal_b.cal_piers;
eep_5211_parse_xpd_gain(eep->modal_b.xpd_gain, gains_map, pdcp);
eep_5211_parse_pdcal_data_map1(aem, ebs, pdcp,
eep->pdcal_data_b);
}
if (eep->base.gmode_en) {
pdcp = &emp->param.pdcal_g;
eep_5211_count_pdcal_piers(eep->modal_g.cal_piers,
&pdcp->npiers,
ARRAY_SIZE(eep->modal_g.cal_piers));
pdcp->piers = eep->modal_g.cal_piers;
eep_5211_parse_xpd_gain(eep->modal_g.xpd_gain, gains_map, pdcp);
eep_5211_parse_pdcal_data_map1(aem, ebs, pdcp,
eep->pdcal_data_g);
}
}
static void eep_5211_parse_pdcal_data_map2(struct atheepmgr *aem,
struct eep_bit_stream *ebs,
struct eep_5211_pdcal_param *pdcp,
struct ar5211_pier_pdcal *pdcal)
{
int maxk;
int i, j, k;
int16_t *pwr;
uint8_t *vpd;
for (i = 0; i < pdcp->npiers; ++i) {
for (j = pdcp->ngains - 1; j >= 0; --j) {
maxk = j == 0 ? 5 : 4;
pwr = pdcal[i].pwr[j];
vpd = pdcal[i].vpd[j];
pwr[0] = EEP_GET_LSB(5) * 4; /* dB -> 0.25 dB */
vpd[0] = EEP_GET_LSB(7);
for (k = 1; k < maxk; ++k) {
pwr[k] = pwr[k - 1] + EEP_GET_LSB(4) * 2;
vpd[k] = vpd[k - 1] + EEP_GET_LSB(6);
}
pdcp->nicepts[j] = maxk;
}
EEP_GET_LSB(ebs->havebits); /* Skip till word boundary */
}
}
static void eep_5211_parse_pdcal_map2(struct atheepmgr *aem,
struct eep_bit_stream *ebs)
{
/* Following array is approximation of {x0.5, x1, x2, x4} gains */
static const int8_t gains_map[] = {-6, 0, 6, 12};
struct eep_5211_priv *emp = aem->eepmap_priv;
struct ar5211_eeprom *eep = &emp->eep;
struct eep_5211_pdcal_param *pdcp;
if (eep->base.amode_en) {
pdcp = &emp->param.pdcal_a;
eep_5211_parse_pdcal_piers_40(aem, ebs, eep->pdcal_piers_a,
&pdcp->npiers,
AR5211_NUM_PDCAL_PIERS_A);
pdcp->piers = eep->pdcal_piers_a;
eep_5211_parse_xpd_gain(eep->modal_a.xpd_gain, gains_map, pdcp);
eep_5211_parse_pdcal_data_map2(aem, ebs, pdcp,
eep->pdcal_data_a);
}
if (eep->base.bmode_en) {
pdcp = &emp->param.pdcal_b;
eep_5211_parse_pdcal_piers_40(aem, ebs, eep->pdcal_piers_b,
&pdcp->npiers,
AR5211_NUM_PDCAL_PIERS_B);
pdcp->piers = eep->pdcal_piers_b;
eep_5211_parse_xpd_gain(eep->modal_b.xpd_gain, gains_map, pdcp);
eep_5211_parse_pdcal_data_map2(aem, ebs, pdcp,
eep->pdcal_data_b);
}
if (eep->base.gmode_en) {
pdcp = &emp->param.pdcal_g;
eep_5211_parse_pdcal_piers_40(aem, ebs, eep->pdcal_piers_g,
&pdcp->npiers,
AR5211_NUM_PDCAL_PIERS_G);
pdcp->piers = eep->pdcal_piers_g;
eep_5211_parse_xpd_gain(eep->modal_g.xpd_gain, gains_map, pdcp);
eep_5211_parse_pdcal_data_map2(aem, ebs, pdcp,
eep->pdcal_data_g);
}
}
static void eep_5211_parse_pdcal(struct atheepmgr *aem)
{
struct eep_5211_priv *emp = aem->eepmap_priv;
struct eep_bit_stream __ebs, *ebs = &__ebs;
memset(ebs, 0x00, sizeof(*ebs));
ebs->eep_off = emp->param.pdcal_off;
if (emp->param.eepmap == 2)
eep_5211_parse_pdcal_map2(aem, ebs);
else if (emp->param.eepmap == 1)
eep_5211_parse_pdcal_map1(aem, ebs);
else if (emp->param.eepmap == 0)
eep_5211_parse_pdcal_map0(aem, ebs);
}
static void eep_5211_parse_tgtpwr_set(struct atheepmgr *aem,
struct eep_bit_stream *ebs,
struct ar5211_chan_tgtpwr *tgtpwr,
int maxchans, int is_2g)
{
struct eep_5211_priv *emp = aem->eepmap_priv;
struct ar5211_eeprom *eep = &emp->eep;
int i, j;
for (i = 0; i < maxchans; ++i) {
if (eep->base.version < AR5211_EEP_VER_3_3) {
tgtpwr[i].chan = EEP_GET_MSB(7);
tgtpwr[i].chan = FBIN_30_TO_33(tgtpwr[i].chan, is_2g);
} else {
tgtpwr[i].chan = EEP_GET_MSB(8);
}
for (j = 0; j < AR5211_NUM_TGTPWR_RATES; ++j)
tgtpwr[i].pwr[j] = EEP_GET_MSB(6);
if (eep->base.version < AR5211_EEP_VER_3_3)
EEP_GET_MSB(1); /* Skip unused bit */
}
}
static void eep_5211_parse_tgtpwr(struct atheepmgr *aem)
{
struct eep_5211_priv *emp = aem->eepmap_priv;
struct ar5211_eeprom *eep = &emp->eep;
struct eep_bit_stream __ebs, *ebs = &__ebs;
memset(ebs, 0x00, sizeof(*ebs));
ebs->eep_off = emp->param.tgtpwr_off;
eep_5211_parse_tgtpwr_set(aem, ebs, eep->tgtpwr_a,
ARRAY_SIZE(eep->tgtpwr_a), 0);
eep_5211_parse_tgtpwr_set(aem, ebs, eep->tgtpwr_b,
ARRAY_SIZE(eep->tgtpwr_b), 1);
eep_5211_parse_tgtpwr_set(aem, ebs, eep->tgtpwr_g,
ARRAY_SIZE(eep->tgtpwr_g), 1);
}
static void eep_5211_fill_ctl_index(struct atheepmgr *aem,
int off)
{
struct eep_5211_priv *emp = aem->eepmap_priv;
struct ar5211_eeprom *eep = &emp->eep;
uint16_t word;
int i;
for (i = 0; i < emp->param.ctls_num; i += 2) {
word = EEP_WORD(off + i / 2);
eep->ctl_index[i + 0] = word >> 8;
eep->ctl_index[i + 1] = word & 0xff;
}
}
static void eep_5211_fill_ctl_data_30(struct atheepmgr *aem)
{
struct eep_5211_priv *emp = aem->eepmap_priv;
struct ar5211_eeprom *eep = &emp->eep;
int off = emp->param.tgtpwr_off + AR5211_EEP_CTL_DATA;
struct eep_bit_stream __ebs = {.eep_off = off}, *ebs = &__ebs;
int i, j, is_2g;
for (i = 0; i < emp->param.ctls_num; ++i) {
ebs->havebits = 0; /* Reset buffer contents */
for (j = 0; j < AR5211_NUM_BAND_EDGES; ++j)
eep->ctl_data[i][j].fbin = EEP_GET_MSB(7);
for (j = 0; j < AR5211_NUM_BAND_EDGES; ++j)
eep->ctl_data[i][j].pwr = EEP_GET_MSB(6);
/* Convert edge frequency codes to modern binary format */
is_2g = eep_ctlmodes[eep->ctl_index[i] & 0xf][0] == '2';
for (j = 0; j < AR5211_NUM_BAND_EDGES; ++j)
eep->ctl_data[i][j].fbin =
FBIN_30_TO_33(eep->ctl_data[i][j].fbin, is_2g);
}
}
static void eep_5211_fill_ctl_data_33(struct atheepmgr *aem)
{
struct eep_5211_priv *emp = aem->eepmap_priv;
struct ar5211_eeprom *eep = &emp->eep;
int off = emp->param.tgtpwr_off + AR5211_EEP_CTL_DATA;
uint16_t word;
int i, j;
for (i = 0; i < emp->param.ctls_num; ++i) {
for (j = 0; j < AR5211_NUM_BAND_EDGES; j += 2) {
word = EEP_WORD(off++);
eep->ctl_data[i][j + 0].fbin = word >> 8;
eep->ctl_data[i][j + 1].fbin = word & 0xff;
}
for (j = 0; j < AR5211_NUM_BAND_EDGES; j += 2) {
word = EEP_WORD(off++);
eep->ctl_data[i][j + 0].pwr = word >> 8;
eep->ctl_data[i][j + 1].pwr = word & 0xff;
}
}
}
static bool eep_5211_load_eeprom(struct atheepmgr *aem, bool raw)
{
struct eep_5211_priv *emp = aem->eepmap_priv;
struct ar5211_eeprom *eep = &emp->eep;
struct ar5211_base_eep_hdr *base = &eep->base;
uint16_t endloc_up, endloc_lo;
uint16_t magic;
int len = 0, addr;
uint16_t *buf = aem->eep_buf;
if (raw) { /* Use max size for RAW loading */
len = aem->eepmap->eep_buf_sz;
goto data_read;
}
/* RAW magic reading with subsequent swaping requirement check */
if (!EEP_READ(AR5211_EEP_MAGIC, &magic)) {
fprintf(stderr, "EEPROM magic read failed\n");
return false;
}
if (bswap_16(magic) == htole16(AR5211_EEPROM_MAGIC_VAL))
aem->eep_io_swap = !aem->eep_io_swap;
if (!EEP_READ(AR5211_EEP_ENDLOC_UP, &endloc_up) ||
!EEP_READ(AR5211_EEP_ENDLOC_LO, &endloc_lo)) {
fprintf(stderr, "Unable to read EEPROM size\n");
return false;
}
if (endloc_up) {
endloc_up = le16toh(endloc_up);
endloc_lo = le16toh(endloc_lo);
len = ((uint32_t)MS(endloc_up, AR5211_EEP_ENDLOC_LOC) << 16) |
endloc_lo;
if (len > aem->eepmap->eep_buf_sz) {
fprintf(stderr, "EEPROM stored length is too big (%d) use maximal lenght (%zd)\n",
len, aem->eepmap->eep_buf_sz);
len = aem->eepmap->eep_buf_sz;
}
}
if (!len) {
if (aem->verbose)
printf("EEPROM length not configured, use default (%d words, %d bytes)\n",
AR5211_SIZE_DEF, AR5211_SIZE_DEF * 2);
len = AR5211_SIZE_DEF;
}
data_read:
/* Read to intermediated buffer */
for (addr = 0; addr < len; ++addr, ++buf) {
if (!EEP_READ(addr, buf)) {
fprintf(stderr, "Unable to read EEPROM to buffer\n");
return false;
}
}
aem->eep_len = addr;
if (raw) /* Earlier exit on RAW contents loading */
return true;
memset(&emp->param, 0x00, sizeof(emp->param));
eep_5211_fill_init_data(aem);
eep_5211_fill_headers(aem);
eep_5211_parse_pdcal(aem);
eep_5211_parse_tgtpwr(aem);
if (base->version >= AR5211_EEP_VER_3_3) {
emp->param.ctls_num = AR5211_NUM_CTLS_33;
eep_5211_fill_ctl_index(aem, AR5211_EEP_CTL_INDEX_33);
eep_5211_fill_ctl_data_33(aem);
} else if (base->version >= AR5211_EEP_VER_3_0) {