summaryrefslogtreecommitdiffstats
path: root/src/usr/i2c/test/i2ctest.H
blob: 8e56c2f0bf2f6a0a2770a0e4150a518fc68abde4 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
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
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/i2c/test/i2ctest.H $                                  */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2011,2018                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
#ifndef __I2CTEST_H
#define __I2CTEST_H

/**
 *  @file i2ctest.H
 *
 *  @brief Test cases for I2C code
 */
#include <sys/time.h>

#include <cxxtest/TestSuite.H>
#include <errl/errlmanager.H>
#include <errl/errlentry.H>
#include <devicefw/driverif.H>
#include <i2c/i2creasoncodes.H>
#include <i2c/i2cif.H>
#include <targeting/common/predicates/predicates.H>
#include <targeting/common/attributes.H>
#include <targeting/common/utilFilter.H>
#include "../i2c.H"

extern trace_desc_t* g_trac_i2c;

// Easy macro replace for unit testing
//#define TRACUCOMP(args...)  TRACFCOMP(args)
#define TRACUCOMP(args...)

using namespace TARGETING;


// Used to ignore comparing data on reads
#define I2C_TEST_IGNORE_DATA_COMPARE 0xFFFFFFFFFFFFFFFF


class I2CTest: public CxxTest::TestSuite
{
    public:

        /**
         * @brief I2C Direct Test
         *      This test will test a variety of direct reads and writes
         *      with various lengths across slave devices.
         *
         *      Currently only Processor targets are supported in simics.
         *
         *      Add other targets to this testcase when their support is
         *      added.
         */
        void _testI2CDirect ( void ) //@fixme-RTC:123019
        {
            errlHndl_t err = NULL;
            int cmds = 0;
            int fails = 0;

            TRACFCOMP( g_trac_i2c,
                       "testI2CDirect - Start" );

            struct
            {
                uint64_t port;      // Master engine port
                uint64_t engine;    // Master engine
                uint64_t devAddr;   // Slave Device address

                uint64_t data;      // Data to write or compare to
                                    // if data = I2C_TEST_IGNORE_DATA_COMPARE
                                    //  than ignore data compare

                size_t   size;        // Number of Bytes to read/write
                bool     rnw;         // Read (true), Write (false)
                TARGETING::TYPE type; // Target Type
            } testData[] =
            {

                // PROCESSOR TESTS
                //  -- For Processor SEEPROM's, ONLY USE ENGINE 0

                // READ All 3 at address 0 for 8 bytes first
                // Ignore data compare as we're not sure what's
                //  been writted there

                // Read SBE Primary:  Murano-0, port-0
                { 0x00, 0x00, 0xAC, I2C_TEST_IGNORE_DATA_COMPARE,
                     8, true, TARGETING::TYPE_PROC }, // Read

                // Read SBE Backup:  Murano-0, port-0
                { 0x00, 0x00, 0xAE, I2C_TEST_IGNORE_DATA_COMPARE,
                     8, true, TARGETING::TYPE_PROC }, // Read

                // Read From MVPD: Murano-0, port 1
                { 0x01, 0x00, 0xA4, I2C_TEST_IGNORE_DATA_COMPARE,
                     8, true, TARGETING::TYPE_PROC }, // Read data back


                // Read/Write SBE Primary:  Murano-0, port-0
                // Safe to write to first 0x300: 0x0-0x2FF

                { 0x00, 0x00, 0xAC, 0x0020ababcdcdefef,
                     8, false, TARGETING::TYPE_PROC }, // Write data to 0x0020

                { 0x00, 0x00, 0xAC, 0x0020000000000000,
                     2, false, TARGETING::TYPE_PROC }, // Write addr for read

                { 0x00, 0x00, 0xAc, 0xababcdcdefef0000,
                     6, true, TARGETING::TYPE_PROC  }, // Read data back

                { 0x00, 0x00, 0xAC, 0x0023000000000000,
                     2, false, TARGETING::TYPE_PROC }, // Write addr for read

                { 0x00, 0x00, 0xAC, 0xcdefef0000000000,
                     3, true, TARGETING::TYPE_PROC  }, // Read data back

                { 0x00, 0x00, 0xAC, 0x0025831200000000,
                     4, false, TARGETING::TYPE_PROC }, // Write data to 0x0025

                { 0x00, 0x00, 0xAC, 0x0024000000000000,
                     2, false, TARGETING::TYPE_PROC }, // Write addr for read

                { 0x00, 0x00, 0xAC, 0xef83120000000000,
                     3, true, TARGETING::TYPE_PROC  }, // Read data back



                // Read/Write SBE Backup:  Murano-0, port-0
                // Safe to write to first 0x300: 0x0-0x2FF

                { 0x00, 0x00, 0xAE, 0x0030ababcdcdefef,
                     8, false, TARGETING::TYPE_PROC }, // Write data to 0x0030

                { 0x00, 0x00, 0xAE, 0x0030000000000000,
                     2, false, TARGETING::TYPE_PROC }, // Write addr for read

                { 0x00, 0x00, 0xAE, 0xababcdcdefef0000,
                     6, true, TARGETING::TYPE_PROC  }, // Read data back

                { 0x00, 0x00, 0xAE, 0x0033000000000000,
                     2, false, TARGETING::TYPE_PROC }, // Write addr for read

                { 0x00, 0x00, 0xAE, 0xcdefef0000000000,
                     3, true, TARGETING::TYPE_PROC  }, // Read data back

                { 0x00, 0x00, 0xAE, 0x0035971200000000,
                     4, false, TARGETING::TYPE_PROC }, // Write data to 0x0035

                { 0x00, 0x00, 0xAE, 0x0034000000000000,
                     2, false, TARGETING::TYPE_PROC }, // Write addr for read

                { 0x00, 0x00, 0xAE, 0xef97120000000000,
                     3, true, TARGETING::TYPE_PROC  }, // Read data back


                // MEMBUF TESTS
                // Use the following commands when Centaur devices are
                // supported in simics.  No target date.
                // Real Centaur Devices
//                { 0x00, 0x00, 0x51, 0x1111000000000000,
//                    2, false, TARGETING::TYPE_MEMBUF }, // Write addr 0x0000
//                { 0x00, 0x00, 0x51, 0x0000000000000000,
//                    8, true, TARGETING::TYPE_MEMBUF  }, // Read 8 bytes
//                { 0x00, 0x00, 0x53, 0x0000000000000000,
//                    2, false, TARGETING::TYPE_MEMBUF }, // Write addr 0x0000
//                { 0x00, 0x00, 0x53, 0x0000000000000000,
//                    8, true, TARGETING::TYPE_MEMBUF  }, // Read 8 bytes
            };

            const uint32_t NUM_CMDS = sizeof(testData)/sizeof(testData[0]);


            // Skipping I2C test altogether in VBU/VPO environment
            if( TARGETING::is_vpo() )
            {
                return;
            }

            do
            {
                // Get top level system target
                TARGETING::TargetService& tS = TARGETING::targetService();
                TARGETING::Target * sysTarget = NULL;
                TARGETING::Target * theTarget = NULL;
                tS.getTopLevelTarget( sysTarget );
                assert( sysTarget != NULL );

                // Get the Proc Target
                TARGETING::Target* procTarget = NULL;
                tS.masterProcChipTargetHandle( procTarget );

                // Get a Centaur Target
                TargetHandleList centList;
                TARGETING::PredicateCTM predCent( TARGETING::CLASS_CHIP,
                                                  TARGETING::TYPE_MEMBUF );
                tS.getAssociated( centList,
                                  sysTarget,
                                  TARGETING::TargetService::CHILD,
                                  TARGETING::TargetService::ALL,
                                  &predCent );

                for( uint32_t i = 0; i < NUM_CMDS; i++ )
                {

                    // Make sure size is less than or = to 8 bytes
                    //  to fit into data
                    uint64_t data;
                    if (testData[i].size > 8)
                    {
                        TRACFCOMP( g_trac_i2c,
                                   "testI2CDirect Size (%d) is greater than"
                                   " 8 bytes. Skipping test %d",
                                   testData[i].size, i );
                        continue;
                    }

                    // if a read, initialize data, else, set data to write
                    if( testData[i].rnw )
                    {
                        data = 0x0ull;
                    }
                    else
                    {
                        data = testData[i].data;
                    }

                    // Decide which target to use
                    switch( testData[i].type )
                    {
                        case TARGETING::TYPE_PROC:
                            if( NULL == procTarget )
                            {
                                TRACFCOMP( g_trac_i2c,
                                           ERR_MRK"Processor Target is NULL, go to next "
                                           "operation!" );
                                continue;
                            }

                            theTarget = procTarget;
                            break;

                        case TARGETING::TYPE_MEMBUF:
                            if( ( 0 == centList.size() ) ||
                                ( NULL == centList[0] ) )
                            {
                                TRACFCOMP( g_trac_i2c,
                                           ERR_MRK"Centaur List has %d entries.  Either "
                                           "empty or first target is NULL!",
                                           centList.size() );
                                continue;
                            }

                            theTarget = centList[0];
                            break;

                        default:
                            TS_FAIL( "testI2CDirect - Invalid Chip type "
                                     "specificed in testData!" );
                            fails++;
                            continue;
                            break;
                    };

                    // Check to see if the target is functional before we
                    // continue..
                    if
                        (!theTarget->getAttr<TARGETING::ATTR_HWAS_STATE>().functional)
                    {

                        TRACFCOMP( g_trac_i2c,
                            "testI2CDirect - target not functional");

                        continue;
                    }

                    // do the operation
                    cmds++;
                    err = deviceOp( (testData[i].rnw ? DeviceFW::READ : DeviceFW::WRITE),
                                    theTarget,
                                    &data,
                                    testData[i].size,
                                    DEVICE_I2C_ADDRESS( testData[i].port,
                                                        testData[i].engine,
                                                        testData[i].devAddr,
                                                        0xFF,
                                                        nullptr) );

                    if( err )
                    {
                        TS_FAIL( "testI2CDirect - fail on cmd %d out of %d",
                                  i, NUM_CMDS );
                        errlCommit( err,
                                    I2C_COMP_ID );
                        delete err;
                        fails++;
                        continue;
                    }

                    // If a write op, wait 5ms for EEPROM to write data
                    // to its internal memory
                    if (testData[i].rnw == false)
                    {
                        nanosleep(0,5 * NS_PER_MSEC);   // 5 msec
                    }

                    // compare data for the read, but ignore case where
                    //  data = I2C_TEST_IGNORE_DATA_COMPARE
                    if( ( testData[i].rnw ) &&
                        ( testData[i].data != I2C_TEST_IGNORE_DATA_COMPARE ))
                    {
                        if( data != testData[i].data )
                        {
                            TRACFCOMP( g_trac_i2c,
                                       "testI2CDirect - cmd: %d/%d, Data read:"
                                       " %016llx, expected: %016llx",
                                       i, NUM_CMDS, data, testData[i].data );
                            TS_FAIL( "testI2CDirect - Failure comparing read data!" );
                            fails++;
                            continue;
                        }
                    }
                }
            } while( 0 );

            TRACFCOMP( g_trac_i2c,
                       "testI2CDirect - End: %d/%d fails",
                       fails, cmds );
        }

        /**
         * @brief I2C Offset
         *      This test will use the I2C interface where an offset for
         *      the device is provided before the reads or write.
         *
         *      Currently only Processor targets are supported in simics.
         *
         *      Add other targets to this testcase when their support is
         *      added.
         */
        void _testI2COffset ( void ) //@fixme-RTC:123019
        {
            errlHndl_t err = NULL;
            int cmds = 0;
            int fails = 0;
            uint64_t original_data = 0;
            uint64_t data = 0;

            TRACFCOMP( g_trac_i2c,
                       "testI2COffset - Start" );

            struct
            {
                uint64_t port;         // Master engine port
                uint64_t engine;       // Master engine
                uint64_t devAddr;      // Slave Device address
                uint16_t offset;       // Slave Device offset - 2 bytes
                uint64_t data;         // Data to write
                size_t   size;         // Number of Bytes to read/write
                TARGETING::TYPE type;  // Target Type
            } testData[] =
            {

                // PROCESSOR TESTS

                // Read/Write SBE Backup:  Murano-0, port-0
                // Safe to write to first 0x300: 0x0-0x2FF
                { 0x00, 0x00, 0xAE, 0x0123,
                  0xFEFEDADA57579191, 8, TARGETING::TYPE_PROC },

                // Read/Write SBE Primary:  Murano-0, port-0
                // Safe to write to first 0x300: 0x0-0x2FF
                { 0x00, 0x00, 0xAC, 0x02FC,
                  0x5ABC310000000000, 3, TARGETING::TYPE_PROC },
            };

            const uint32_t NUM_CMDS = sizeof(testData)/sizeof(testData[0]);


            // Skipping I2C test altogether in VBU/VPO environment
            if( TARGETING::is_vpo() )
            {
                return;
            }

            do
            {

                // Get top level system target
                TARGETING::TargetService& tS = TARGETING::targetService();
                TARGETING::Target * sysTarget = NULL;
                TARGETING::Target * theTarget = NULL;
                tS.getTopLevelTarget( sysTarget );
                assert( sysTarget != NULL );

                // Get the Proc Target
                TARGETING::Target* procTarget = NULL;
                tS.masterProcChipTargetHandle( procTarget );

                // Get a Centaur Target
                TargetHandleList centList;
                TARGETING::PredicateCTM predCent( TARGETING::CLASS_CHIP,
                                                  TARGETING::TYPE_MEMBUF );
                tS.getAssociated( centList,
                                  sysTarget,
                                  TARGETING::TargetService::CHILD,
                                  TARGETING::TargetService::ALL,
                                  &predCent );

                for ( uint32_t i = 0; i < NUM_CMDS; i++ )
                {
                    TRACUCOMP( g_trac_i2c,"testI2COffset: Outer Loop i= "
                               "%d, NUM_CMDS = %d", i, NUM_CMDS);

                    // Make sure size is less than or = to 8 bytes
                    //  to fit into data
                    if (testData[i].size > 8)
                    {
                        TRACFCOMP( g_trac_i2c,
                                   "testI2COffset: Size (%d) is greater than"
                                   " 8 bytes. Skipping test %d",
                                   testData[i].size, i );
                        continue;
                    }

                    // Decide which target to use
                    switch( testData[i].type )
                    {
                        case TARGETING::TYPE_PROC:
                            if( NULL == procTarget )
                            {
                                TRACFCOMP( g_trac_i2c,
                                           ERR_MRK"Processor Target is NULL, "
                                           "go to next operation!" );
                                continue;
                            }

                            theTarget = procTarget;
                            break;

                        case TARGETING::TYPE_MEMBUF:
                            if( ( 0 == centList.size() ) ||
                                ( NULL == centList[0] ) )
                            {
                                TRACFCOMP( g_trac_i2c,
                                           ERR_MRK"Centaur List has %d entries."
                                           "  Either empty or first target is "
                                           "NULL!",
                                           centList.size() );
                                continue;
                            }

                            theTarget = centList[0];
                            break;

                        default:
                            TS_FAIL( "testI2COffset - Invalid Chip type "
                                     "specificed in testData!" );
                            fails++;
                            continue;
                            break;
                    };


                    // Check to see if the target is functional before we
                    // continue..
                    if
                        (!theTarget->getAttr<TARGETING::ATTR_HWAS_STATE>().functional)
                    {

                        TRACFCOMP( g_trac_i2c,
                            "testI2COffset - target not functional");

                        continue;
                    }


                    // For Each Set of Data, 5 operations:
                    // 1) Read Original Data and Save It
                    // 2) Write New Data
                    // 3) Read New Data and Compare
                    // 4) Write Back Original Data
                    // 5) Read Back Original Data and Compare

                    // Before starting, clear original data buffer
                    original_data = 0x0ull;

                    for (uint8_t j = 1; j <= 5; j++)
                    {

                        // Clear data variable
                        data = 0x0ull;

                        // For Loop 2: set data to new data
                        if ( j == 2 )
                            data = testData[i].data;

                        // For Loop 4: set data to original_data
                        if ( j == 4 )
                            data = original_data;

                        // increment cmd op counter
                        cmds++;

                        err = deviceOp(
                                (j%2) ? DeviceFW::READ : DeviceFW::WRITE,
                                theTarget,
                                &data,
                                testData[i].size,
                                DEVICE_I2C_ADDRESS_OFFSET(
                                                    testData[i].port,
                                                    testData[i].engine,
                                                    testData[i].devAddr,
                                                    sizeof(testData[i].offset),
                                                    reinterpret_cast<uint8_t*>(
                                                    &(testData[i].offset)),
                                                    0xFF,
                                                    nullptr));

                        if( err )
                        {
                            TS_FAIL( "testI2COffset - OP %d FAILED "
                                     "- cmd %d out of %d",
                                     j, i, NUM_CMDS );
                            errlCommit( err,
                                        I2C_COMP_ID );
                            delete err;
                            fails++;
                            continue;
                        }


                        // If a write op, wait 5ms for EEPROM to write data
                        // to its internal memory
                        if ( (j%2) == 0 )
                        {
                            nanosleep(0,5 * NS_PER_MSEC);   // 5 msec
                        }

                        // Handle loop-specific results

                        // For Loop 1: save original data
                        if ( j == 1 )
                        {
                            original_data = data;

                            // Always trace original data - just in case
                            TRACFCOMP(g_trac_i2c,"testI2COffset: "
                                      "original_data=0x%x", original_data);
                        }


                        // For Loop 3: compare new data
                        if ( j == 3 )
                        {

                            TRACUCOMP( g_trac_i2c,"testI2COffset: "
                               "New Data Compare: "
                               "written=0x%016llx, read_back=0x%016llx",
                               testData[i].data, data);

                            if( data != testData[i].data )
                            {
                                TRACFCOMP(g_trac_i2c,"testI2COffset: New "
                                    "Data Compare Fail: wrote=%016llx, "
                                    "read back=%016llx. cmd: %d/%d (%d)",
                                    testData[i].data, data, i, NUM_CMDS, j);
                                TS_FAIL( "testI2COffset - Failure comparing "
                                         "new data!" );
                                fails++;

                                // Don't break - try to write back original data
                                continue;
                            }

                        }

                        // For Loop 5: compare writing-back original data
                        if ( j == 5 )
                        {
                            TRACUCOMP( g_trac_i2c,"testI2COffset: "
                               "Original Data Compare: "
                               "original=0x%016llx, read_back=0x%016llx",
                               original_data, data);

                            if( data != original_data )
                            {
                                TRACFCOMP(g_trac_i2c,"testI2COffset: New "
                                    "Data Compare Fail: original=%016llx, "
                                    "read back=%016llx. cmd: %d/%d (%d)",
                                    original_data, data, i, NUM_CMDS, j);
                                TS_FAIL( "testI2COffset - Failure comparing "
                                         "original data!" );
                                fails++;

                                // Break: stop testing if we can't write back
                                // original data successfully
                                break;
                            }
                        }

                    }  // end of 'j' loop: 5 ops per testData[i]

                }  // end of 'i' loop: unique testData[i] sets

            } while( 0 );

            TRACFCOMP( g_trac_i2c,
                       "testI2COffset - End: %d/%d fails",
                       fails, cmds );
        }


        /**
         * @brief Call I2C Setup Masters Function
         *      This function is part of the shutdown path and is
         *      needed by PHYP to ensure that the I2C masters are
         *      setup correctly on the system.
         */
        void _testI2CSetupActiveMasters ( void ) //@fixme-RTC:123019
        {
            errlHndl_t err = NULL;
            int fails = 0;
            const int NUM_CMDS = 1;

            TRACFCOMP( g_trac_i2c,
                       "testI2CSetupActiveMasters - Start" );

            err = I2C::i2cSetupActiveMasters(I2C::I2C_PROC_ALL);

            if( err )
            {
                TS_FAIL( "testI2CSetupActiveMasters - Error returned from "
                         "i2cSetupActiveMasters(I2C_PROC_ALL)");
                fails++;
                delete err;
            }

            TRACFCOMP( g_trac_i2c,
                       "testI2CSetupActiveMasters - End: %d/%d fails",
                       fails, NUM_CMDS );
        }


        /**
         * @brief Call I2C Set Bus Variables and related functions
         *
         *      Test that i2cSetBusVariables() and the functions it uses
         *      are returning expected values.
         */
        void _testI2cSetBusVariables ( void ) //@fixme-RTC:123019
        {
            errlHndl_t err = NULL;
            int fails = 0;
            int cmds = 0;
            I2C::misc_args_t io_args;

            TRACFCOMP( g_trac_i2c,
                       "testI2cSetBusVariables - Start" );

            // Get top level system target
            TARGETING::TargetService& tS = TARGETING::targetService();
            TARGETING::Target * sysTarget = NULL;
            tS.getTopLevelTarget( sysTarget );
            assert( sysTarget != NULL );

            // Get the Proc Target
            TARGETING::Target* procTarget = NULL;
            tS.masterProcChipTargetHandle( procTarget );

            /****************************************************/
            /* Test i2cSetBusVariables() with different i_speed */
            /****************************************************/
            struct
            {
                uint64_t i_speed;
                uint64_t o_bus_speed;
            } testData[] =
            {
                // 1MHz
                { I2C_BUS_SPEED_1MHZ, 1000 },

                // 400KHz
                { I2C_BUS_SPEED_400KHZ, 400 },

                // I2C_BUS_SPEED_FROM_MRW (==0) should default to 1MHz
                // with a processor target using Host I2C
                { I2C_BUS_SPEED_FROM_MRW, 1000 },
            };

            uint32_t NUM_CMDS = sizeof(testData)/sizeof(testData[0]);

            // set Processor Target to Host I2C mode and default engine/port
            io_args.switches.useHostI2C = 1;
            io_args.switches.useFsiI2C = 0;
            io_args.engine = 0;
            io_args.port = 0;

            for ( uint32_t i = 0; i < NUM_CMDS; i++ )
            {
                cmds++;

                err = I2C::i2cSetBusVariables(procTarget,
                                              testData[i].i_speed,
                                              io_args);

                if( err )
                {
                    TS_FAIL( "testI2cSetBusVariables - Error returned from "
                             "i2cSetBusVariables: i_speed=%d, i=%d",
                             testData[i].i_speed, i);
                    fails++;
                    delete err;
                }
                else if ( io_args.bus_speed != testData[i].o_bus_speed )
                {
                    TS_FAIL( "testI2cSetBusVariables - i2cSetBusVariables "
                             "returned bad data: i_speed=%d, i=%d "
                             "o_bus_speed=%d, io_args.bus_speed=%d",
                             testData[i].i_speed, i,
                             testData[i].o_bus_speed, io_args.bus_speed);
                    fails++;
                }
            }

            /****************************************************/
            /* Test functions that set other variables          */
            /****************************************************/
            struct
            {
                uint64_t i_nest_freq_mhz;
                uint64_t i_bus_speed;
                uint16_t o_bit_rate_divisor;
                uint64_t o_polling_interval_ns;
                uint64_t o_timeout_count;
            } testData_2[] =
            {
                // Most likely settings

                // NEST_FREQ_MHz=2000, bus_speed=1MHz
                { 2000, 1000,
                  31, 800, 6250 },

                // NEST_FREQ_MHz=2000, bus_speed=400KHz
                { 2000, 400,
                  77, 2000, 2500 },

                // NEST_FREQ_MHz=2400, bus_speed=1MHz
                { 2400, 1000,
                  37, 800, 6250 },

                // NEST_FREQ_MHz=2400, bus_speed=400KHz
                { 2400, 400,
                  93, 2000, 2500 },
            };

            NUM_CMDS = sizeof(testData_2)/sizeof(testData_2[0]);

            uint16_t l_brd = 0;
            uint64_t l_pi =0;
            uint64_t l_tc=0;

            for ( uint32_t i = 0; i < NUM_CMDS; i++ )
            {
                I2C::g_I2C_NEST_FREQ_MHZ = testData_2[i].i_nest_freq_mhz;

                cmds++;
                l_brd = I2C::i2cGetBitRateDivisor(
                                             testData_2[i].i_bus_speed,
                                             testData_2[i].i_nest_freq_mhz);
                l_pi = I2C::i2cGetPollingInterval(testData_2[i].i_bus_speed);
                l_tc = I2C_TIMEOUT_COUNT(l_pi);

                if ((l_brd != testData_2[i].o_bit_rate_divisor ) ||
                    (l_pi != testData_2[i].o_polling_interval_ns ) ||
                    (l_tc != testData_2[i].o_timeout_count )
                   )
                {
                    TS_FAIL( "testI2cSetBusVariables - Bad Calculations i=%d "
                             "nest_freq_mhz=%d, bus_speed=%d, "
                             "l_brd=%d, testData_2[i].o_bit_rate_divisor=%d, "
                             "l_pi=%d, testData_2[i].o_polling_interval_ns=%d, "
                             "l_tc=%d, testData_2[i].o_timeout_count=%d",
                             i, testData_2[i].i_nest_freq_mhz,
                             testData_2[i].i_bus_speed,
                             l_brd, testData_2[i].o_bit_rate_divisor,
                             l_pi, testData_2[i].o_polling_interval_ns,
                             l_tc, testData_2[i].o_timeout_count );
                    fails++;
                }
            }

            // reset global variable
            I2C::g_I2C_NEST_FREQ_MHZ = I2C::i2cGetNestFreq();

            TRACFCOMP( g_trac_i2c,
                       "testI2cSetBusVariables - End: %d/%d fails",
                       fails, cmds );
        }



        /**
         * @brief I2C Invalid Target test
         *      This test will pass in the Master Sentinel chip in as a target
         *      to be sure that an error is returned, and that the error returned
         *      is the correct error.
         */
        void _testI2CInvalidTarget ( void ) //@fixme-RTC:123019
        {
            errlHndl_t err = NULL;
            int fails = 0;
            const int NUM_CMDS = 1;

            TRACFCOMP( g_trac_i2c,
                       "testI2CInvalidTarget - Start" );

            // Set processor chip to the master
            TARGETING::Target* testTarget = MASTER_PROCESSOR_CHIP_TARGET_SENTINEL;
            uint64_t data = 0x0ull;
            size_t size = sizeof(uint64_t);

            err = deviceOp( DeviceFW::READ,
                            testTarget,
                            &data,
                            size,
                            DEVICE_I2C_ADDRESS( 0x0,
                                                0x0,
                                                0x50,
                                                0xFF,
                                                nullptr) );

            if( !err )
            {
                TS_FAIL( "testI2CInvalidTarget - Failure to return error "
                         "using Master Sentinel Chip!" );
                fails++;
            }
            else
            {
                delete err;
                err = NULL;
            }

            TRACFCOMP( g_trac_i2c,
                       "testI2CInvalidTarget - End: %d/%d fails",
                       fails, NUM_CMDS );
        }

        /**
         * @brief I2C Invalid Operation Test
         *      This test will pass in an invalid Operation type.  It
         *      is expected that an error log is to be returned.
         */
        void _testI2CInvalidOperation ( void ) //@fixme-RTC:123019
        {
            errlHndl_t err = NULL;
            int64_t fails = 0, num_ops = 0;
            uint64_t data = 0x0ull;
            size_t dataSize = 8;

            TRACFCOMP( g_trac_i2c,
                       "testI2CInvalidOperation - Start" );

            do
            {
                // Get a processor Target
                TARGETING::TargetService& tS = TARGETING::targetService();
                TARGETING::Target* testTarget = NULL;
                tS.masterProcChipTargetHandle( testTarget );
                assert(testTarget != NULL);


                // check to see if the target is functional before we
                // continue..
                if
                    (!testTarget->getAttr<TARGETING::ATTR_HWAS_STATE>().functional)
                {
                    TRACFCOMP( g_trac_i2c,
                               "testI2CInvalidOperation - not functional" );


                    continue;
                }

                num_ops++;
                err = deviceOp( DeviceFW::LAST_OP_TYPE,
                                testTarget,
                                &data,
                                dataSize,
                                DEVICE_I2C_ADDRESS( 0x0,
                                                    0x0,
                                                    0x50,
                                                    0xFF,
                                                    nullptr) );

                if( NULL == err )
                {
                    fails++;
                    TS_FAIL( "testI2CInvalidOperation - Error should've "
                             "resulted in Operation type of LAST_OP_TYPE!" );
                }
                else
                {
                    delete err;
                    err = NULL;
                }
            } while( 0 );
            TRACFCOMP( g_trac_i2c,
                       "testI2CInvalidOperation - End: %d/%d fails",
                       fails, num_ops );
        }

        void _testI2cForceReset(void) //@fixme-RTC:123019
        {


            errlHndl_t err = NULL;

            do
            {
                I2C::misc_args_t io_args;

                uint64_t data = 0x0;

                // Get the Proc Target
                TARGETING::TargetService& tS = TARGETING::targetService();
                TARGETING::Target* procTarget = NULL;
                tS.masterProcChipTargetHandle( procTarget );
                assert( procTarget != NULL );

                // reset the target and then try a read
                if(procTarget->getAttr<TARGETING::ATTR_HWAS_STATE>().functional)
                {
                    TRACFCOMP( g_trac_i2c,
                               " testI2cForceReset- functional good path" );
                    io_args.engine = 0x0;
                    io_args.port = 0x0;
                    //get engine lock attr for engine 0
                    //prevents sbe update test fails

                    mutex_t * engineLock = NULL;
                    engineLock =
                    procTarget->getHbMutexAttr<TARGETING::ATTR_I2C_ENGINE_MUTEX_0>();

                    (void)mutex_lock( engineLock );

                    //force reset
                    err = I2C::i2cReset(procTarget,
                                        io_args,
                                        I2C::FORCE_UNLOCK_RESET);

                    (void) mutex_unlock( engineLock );

                    //if there was an error for a reason commit it
                    if(err)
                    {
                        errlCommit( err,I2C_COMP_ID );
                        delete err; err = NULL;
                    }

                    nanosleep( 0, 100);

                    //read
                    size_t data_size = 8;
                    err = deviceOp( DeviceFW::READ,
                                    procTarget,
                                    &data,
                                    data_size,
                                    DEVICE_I2C_ADDRESS( 0,
                                                        0,
                                                        0xAC,
                                                        0xFF,
                                                        nullptr) );

                }




                if(err)
                {
                    TS_FAIL("testI2cForceReset failed err detected");
                    errlCommit( err,I2C_COMP_ID );
                    delete err; err = NULL;
                    break;
                }

                if(data == 0)
                {
                    //there should be some data
                    TS_FAIL("testI2cForceReset failed data read is 0");
                    break;
                }


            }while(0);


        }

        /**
         * @brief Verify we retrieve all of the EEPROMs we can think of
         */
        void test_getMasterInfo( void )
        {
            std::list<I2C::MasterInfo_t> info;
            TARGETING::TargetHandleList allchips;
            TARGETING::getAllChips( allchips, TARGETING::TYPE_NA );
            for( TARGETING::TargetHandleList::iterator tgt = allchips.begin();
                 tgt != allchips.end();
                 ++tgt )
            {
                getMasterInfo( *tgt, info );
                for( std::list<I2C::MasterInfo_t>::iterator i2cm = info.begin();
                     i2cm != info.end();
                     ++i2cm )
                {
                    TRACFCOMP( g_trac_i2c, "Found I2C Master: Master=%.8X :: Scom=%.8X, Eng=%d, Freq=%d", TARGETING::get_huid(*tgt), i2cm->scomAddr, i2cm->engine, i2cm->freq );
                }
            }
        }

        void test_getDeviceInfo( void )
        {
            TRACFCOMP(g_trac_i2c, ENTER_MRK"test_getDeviceInfo");

            std::vector<I2C::DeviceInfo_t> l_deviceInfo;

            // Loop through every chip
            TARGETING::TargetHandleList allchips;
            TARGETING::getAllChips( allchips, TARGETING::TYPE_NA );
            for( auto &tgt : allchips )
            {
                getDeviceInfo( tgt, l_deviceInfo);
            }

            std::vector<I2C::DeviceInfo_t>::const_iterator devInfo_iter;
            for(devInfo_iter = l_deviceInfo.begin();
                devInfo_iter != l_deviceInfo.end();
                devInfo_iter++)
            {
                TRACFCOMP(g_trac_i2c, "Found Device Info: Engine=%d, Port=%d",
                          devInfo_iter->engine,
                          devInfo_iter->masterPort);
                TRACFCOMP(g_trac_i2c, "Type=%.2X, addr=%X, slavePort=%d",
                          devInfo_iter->deviceType,
                          devInfo_iter->addr,
                          devInfo_iter->slavePort);
                TRACFCOMP(g_trac_i2c, "busFreqKhz=%d, purpose=%X",
                          devInfo_iter->busFreqKhz,
                          devInfo_iter->devicePurpose)
            }

            TRACFCOMP(g_trac_i2c, EXIT_MRK"test_getDeviceInfo");
        }

        /**
         * @brief Verify the functions that create the userdata values
         */
        void test_setI2CUserData( void )
        {

            int64_t fails = 0, num_ops = 0;

            TRACFCOMP( g_trac_i2c,
                       "test_setI2CUserData - Start" );


            // Test userdata_1
            uint64_t userdata_1 = 0x0;
            uint64_t userdata_1_result = 0xABCDEFABFFFFFFFF;

            I2C::status_reg_t status_reg;
            status_reg.value = 0xABCDEFAB55555555;

            userdata_1 = I2C::I2C_SET_USER_DATA_1 (
                             status_reg,
                             MASTER_PROCESSOR_CHIP_TARGET_SENTINEL);

            num_ops++;
            if ( userdata_1 != userdata_1_result )
            {
                TS_FAIL( "testI2CUserData - Fail in generating userdata)1: "
                         "0x%X - should be 0x%X",
                         userdata_1, userdata_1_result );
                fails++;
            }

            // Test userdata_2
            uint64_t userdata_2 = 0x0;
            uint64_t userdata_2_result = 0x34679A5D1234FEDC;

            I2C::misc_args_t args;
            args.engine = 0x34;
            args.port = 0x67;
            args.devAddr = 0x1234123412349A5D;
            args.bus_speed = 0xFFFFFFFFFFFF1234;
            args.bit_rate_divisor = 0xFEDC;

            userdata_2 = I2C::I2C_SET_USER_DATA_2 ( args );

            num_ops++;
            if ( userdata_2 != userdata_2_result )
            {
                TS_FAIL( "testI2CUserData - Fail in generating userdata_2: "
                         "0x%X - should be 0x%X",
                         userdata_2, userdata_2_result );
                fails++;
            }

           TRACFCOMP( g_trac_i2c,
                       "testI2CUserData - End: %d/%d fails",
                       fails, num_ops );

        }


};

#endif
OpenPOWER on IntegriCloud