summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/nvrm/io/ap20/ap20rm_i2c.c
blob: 272abadb4e75a621f35b776484c7b7fdb0779de7 (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
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
/*
 * Copyright (c) 2008-2009 NVIDIA Corporation.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the NVIDIA Corporation nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */

/** @file
 * @brief <b>NVIDIA Driver Development Kit: I2C API</b>
 *
 * @b Description: Contains the NvRM I2C implementation. for Ap20 
 */

#include "nvrm_i2c.h"
#include "nvrm_i2c_private.h"
#include "nvrm_drf.h"
#include "nvos.h"
#include "nvrm_module.h"
#include "ap20/ari2c.h"
#include "nvrm_hardware_access.h"
#include "nvrm_power.h" 
#include "nvrm_interrupt.h"
#include "nvassert.h"
#include "ap20/ardvc.h"
#include "nvodm_query_pinmux.h"
#include "nvrm_pinmux.h"
#include "nvrm_chiplib.h"
#include "nvrm_hwintf.h"

/* Register access Macros */
#define I2C_REGR(c, reg) NV_REGR((c)->hRmDevice, (c)->ModuleId, (c)->Instance,  \
        (c)->I2cRegisterOffset + (((c)->ModuleId == NvRmModuleID_Dvc) ? DVC_##reg##_0 : \
                                                I2C_##reg##_0)) 

#define I2C_REGW(c, reg, val) \
        do { \
            NV_REGW((c)->hRmDevice, (c)->ModuleId, (c)->Instance, \
                ((c)->I2cRegisterOffset + (((c)->ModuleId == NvRmModuleID_Dvc) ? DVC_##reg##_0 : \
                                            I2C_##reg##_0)), (val)); \
        } while(0)

#define DVC_REGR(c, reg)    NV_REGR((c)->hRmDevice, NvRmModuleID_Dvc, (c)->Instance, \
                    DVC_##reg##_0)
#define DVC_REGW(c, reg, val) \
        do {    \
            NV_REGW((c)->hRmDevice, NvRmModuleID_Dvc, (c)->Instance, \
                    DVC_##reg##_0, val); \
        } while(0)


/* Register access Macros */
#define I2C2_REGR(c, reg) NV_REGR((c)->hRmDevice, (c)->ModuleId, (c)->Instance,  \
                    (c)->I2cRegisterOffset + I2C_##reg##_0 )

#define I2C2_REGW(c, reg, val) \
        do {    \
                NV_REGW((c)->hRmDevice, (c)->ModuleId, (c)->Instance, \
                    ((c)->I2cRegisterOffset + I2C_##reg##_0), (val) ); \
        } while(0);
                    

#define DEBUG_SEND_PROCESS 0
#define DEBUG_READ_PROCESS 0
#define DEBUG_TRACE_PROCESS 0

#if DEBUG_SEND_PROCESS
#define DEBUG_I2C_SEND(Expr, Format) \
                    do { \
                        if (Expr) \
                        {   \
                            NvOsDebugPrintf Format; \
                        }   \
                    } while(0)
#else
#define DEBUG_I2C_SEND(Expr, Format)
#endif

#if DEBUG_READ_PROCESS
#define DEBUG_I2C_READ(Expr, Format) \
                    do { \
                        if (Expr) \
                        {   \
                            NvOsDebugPrintf Format; \
                        }   \
                    } while(0)
#else
#define DEBUG_I2C_READ(Expr, Format)
#endif

#if DEBUG_TRACE_PROCESS
#define DEBUG_I2C_TRACE(Expr, Format) \
                    do { \
                        if (Expr) \
                        {   \
                            NvOsDebugPrintf Format; \
                        } \
                    } while(0)
#else
#define DEBUG_I2C_TRACE(Expr, Format)
#endif

// The maximum transfer size by one transaction.
enum {MAX_I2C_ONE_TRANSACTION_SIZE = 0x1000}; // 4KB

// The maximum request size for one transaction using the dma.
// + 64 bytes for the packet header.
enum {DEFAULT_I2C_DMA_BUFFER_SIZE = (MAX_I2C_ONE_TRANSACTION_SIZE + 0x40)}; // 4KB

// The default request size for one transaction using the nondma mode.
enum {DEFAULT_I2C_CPU_BUFFER_SIZE = MAX_I2C_ONE_TRANSACTION_SIZE};

// Wait time to poll the status for completion.
enum { I2C_POLLING_TIMEOUT_STEP_USEC = 1000};

// I2C fifo depth.
enum { I2C_FIFO_DEPTH = 8};

// I2C Dma/CPU based seletion thresold.
enum { I2C_MAX_WORD_TO_USE_CPU = 16};

// Holding the apb dma for the continuous non dma transaction count
enum {HOLDING_DMA_TRANSACTION_COUNT = 15};
#define I2C_TRANSACTION_STATUS_ERRORS \
    (NV_DRF_DEF(I2C, INTERRUPT_STATUS_REGISTER, TFIFO_OVF, SET) | \
     NV_DRF_DEF(I2C, INTERRUPT_STATUS_REGISTER, RFIFO_UNF, SET) | \
     NV_DRF_DEF(I2C, INTERRUPT_STATUS_REGISTER, ARB_LOST, SET))

#define I2C_ARBITRATION_LOST_ERRORS \
         (NV_DRF_DEF(I2C, INTERRUPT_STATUS_REGISTER, ARB_LOST, SET))

#define I2C_ERRORS_INTERRUPT_MASK \
    (NV_DRF_DEF(I2C, INTERRUPT_MASK_REGISTER, TFIFO_OVF_INT_EN, ENABLE) | \
     NV_DRF_DEF(I2C, INTERRUPT_MASK_REGISTER, RFIFO_UNF_INT_EN, ENABLE) | \
     NV_DRF_DEF(I2C, INTERRUPT_MASK_REGISTER, ARB_LOST_INT_EN, ENABLE))

#if NV_OAL
#define USE_POLLING_METHOD 1
#else
#define USE_POLLING_METHOD 0
#endif

#if USE_POLLING_METHOD
#define RESET_SEMA_COUNT(hSema) 
#else
#define RESET_SEMA_COUNT(hSema) \
    while(NvOsSemaphoreWaitTimeout(hSema, 0) != NvError_Timeout)
#endif

// Convert the number of bytes to word.
#define BYTES_TO_WORD(ReqSize) (((ReqSize) + 3) >> 2)

/**
 * Create the dma buffer memory handle.
 */
static NvError
CreateDmaBufferMemoryHandle(
    NvRmDeviceHandle hDevice,
    NvRmMemHandle *phNewMemHandle,
    NvRmPhysAddr *pNewMemAddr,
    NvU32 BufferSize)
{
    NvError Error = NvSuccess;
    NvRmMemHandle hNewMemHandle = NULL;

    // Initialize the memory handle with NULL
    *phNewMemHandle = NULL;

    /// Create memory handle
    Error = NvRmMemHandleCreate(hDevice, &hNewMemHandle, BufferSize);

    // Allocates the memory from the sdram
    if (!Error)
        Error = NvRmMemAlloc(hNewMemHandle, NULL,
                        0, 4, NvOsMemAttribute_Uncached);

    // Pin the memory allocation so that it should not move by memory manager.
    if (!Error)
        *pNewMemAddr = NvRmMemPin(hNewMemHandle);

    // If error then free the memory allocation and memory handle.
    if (Error)
    {
        NvRmMemHandleFree(hNewMemHandle);
        hNewMemHandle = NULL;
    }

    *phNewMemHandle = hNewMemHandle;
    return Error;
}

 /**
  * Destroy the dma buffer memory handle.
  */
static void DestroyDmaBufferMemoryHandle(NvRmMemHandle hMemHandle)
{
    // Can accept the null parameter. If it is not null then only destroy.
    if (hMemHandle)
    {
        // Unpin the memory allocation.
        NvRmMemUnpin(hMemHandle);

        // Free the memory handle.
        NvRmMemHandleFree(hMemHandle);
    }
}

/**
 * Create the dma transfer buffer for the given handles.
 */
static NvError
CreateDmaTransferBuffer(
    NvRmDeviceHandle hRmDevice,
    NvRmMemHandle *phRmMemory,
    NvRmPhysAddr *pBuffPhysAddr,
    void **pBuffPtr,
    NvU32 BufferSize)
{
    NvError Error = NvSuccess;
    NvRmMemHandle hRmMemory = NULL;
    NvRmPhysAddr BuffPhysAddr;

    // Reset all the members realted to the dma buffer.
    BuffPhysAddr = 0;

    *phRmMemory = NULL;
    *pBuffPtr = (void *)NULL;
    *pBuffPhysAddr = 0;

    // Create the dma buffer memory for receive and transmit.
    // It will be double of the OneBufferSize
    Error = CreateDmaBufferMemoryHandle(hRmDevice, &hRmMemory, 
                                                &BuffPhysAddr, BufferSize);
    if (!Error)
    {
        // 0 to OneBufferSize-1 is buffer 1 and OneBufferSize to 2*OneBufferSize
        // is second buffer.
        Error = NvRmMemMap(hRmMemory, 0, BufferSize, 
                                                NVOS_MEM_READ_WRITE, pBuffPtr);
        // If error then free the allocation and reset all changed value.
        if (Error)
        {
            DestroyDmaBufferMemoryHandle(hRmMemory);
            hRmMemory = NULL;
            *pBuffPtr = (void *)NULL;
            return Error;
        }
        *phRmMemory = hRmMemory;
        *pBuffPhysAddr = BuffPhysAddr;
    }
    return Error;
}

/**
 * Destroy the dma transfer buffer.
 */
static void
DestroyDmaTransferBuffer(
    NvRmMemHandle hRmMemory,
    void *pBuffPtr,
    NvU32 BufferSize)
{
    if (hRmMemory)
    {
        if (pBuffPtr)
            NvRmMemUnmap(hRmMemory, pBuffPtr, BufferSize);
        DestroyDmaBufferMemoryHandle(hRmMemory);
    }
}

static void SetTxFifoTriggerLevel(NvRmI2cControllerHandle hRmI2cCont, NvU32 TrigLevel)
{
    NvU32 FifoControlReg;
    NvU32 ActualTriggerLevel = NV_MIN(I2C_FIFO_DEPTH, TrigLevel);

    if (!ActualTriggerLevel)
        return;
    
    FifoControlReg = I2C_REGR (hRmI2cCont, FIFO_CONTROL);
    FifoControlReg = NV_FLD_SET_DRF_NUM(I2C, FIFO_CONTROL, TX_FIFO_TRIG, 
                                                ActualTriggerLevel - 1, FifoControlReg);
    DEBUG_I2C_SEND(1, ("Tx Fifo Control  0x%08x\n", FifoControlReg));
    I2C_REGW (hRmI2cCont, FIFO_CONTROL, FifoControlReg);
}

static void SetRxFifoTriggerLevel(NvRmI2cControllerHandle hRmI2cCont, NvU32 TrigLevel)
{
    NvU32 FifoControlReg;
    NvU32 ActualTriggerLevel = NV_MIN(I2C_FIFO_DEPTH, TrigLevel);

    if (!ActualTriggerLevel)
        return;
        
    FifoControlReg = I2C_REGR (hRmI2cCont, FIFO_CONTROL);
    FifoControlReg = NV_FLD_SET_DRF_NUM(I2C, FIFO_CONTROL, RX_FIFO_TRIG, 
                                                ActualTriggerLevel - 1, FifoControlReg);
    DEBUG_I2C_READ(1, ("Rx Fifo Control  0x%08x\n", FifoControlReg));
    I2C_REGW (hRmI2cCont, FIFO_CONTROL, FifoControlReg);
}


static void ResetTxFifo(NvRmI2cControllerHandle hRmI2cCont)
{
    NvU32 FifoControlReg;
    
    FifoControlReg = I2C_REGR (hRmI2cCont, FIFO_CONTROL);
    FifoControlReg = NV_FLD_SET_DRF_DEF(I2C, FIFO_CONTROL, TX_FIFO_FLUSH, 
                                                SET, FifoControlReg);
    I2C_REGW (hRmI2cCont, FIFO_CONTROL, FifoControlReg);
    do
    {
        NvOsWaitUS(10);
        FifoControlReg = I2C_REGR (hRmI2cCont, FIFO_CONTROL);
    }while(FifoControlReg & NV_DRF_DEF(I2C, FIFO_CONTROL, TX_FIFO_FLUSH, SET));
}

static void DoDvcI2cControlInitialization(NvRmI2cControllerHandle hRmI2cCont)
{
    NvU32 RegVal = 0;
    
    RegVal = DVC_REGR(hRmI2cCont, CTRL_REG3);
    RegVal = NV_FLD_SET_DRF_DEF(DVC, CTRL_REG3, I2C_HW_SW_PROG, SW, RegVal);
    RegVal = NV_FLD_SET_DRF_DEF(DVC, CTRL_REG3, I2C_DONE_INTR_EN, ENABLE, RegVal);
    DVC_REGW(hRmI2cCont, CTRL_REG3, RegVal);
    
    RegVal = DVC_REGR(hRmI2cCont, CTRL_REG1);
    RegVal = NV_FLD_SET_DRF_DEF(DVC, CTRL_REG1, INTR_EN, ENABLE, RegVal);
    DVC_REGW(hRmI2cCont, CTRL_REG1, RegVal);
}

static void UseDvcI2cNewSlave(NvRmI2cControllerHandle hRmI2cCont)
{
    NvU32 RegVal = 0;
    RegVal = NV_DRF_DEF(I2C, I2C_SL_CNFG, NEWSL, ENABLE);
    I2C2_REGW(hRmI2cCont, I2C_SL_CNFG, RegVal);
    
    RegVal = NV_DRF_NUM(I2C, I2C_SL_ADDR1, SL_ADDR0, 0xF);
    I2C2_REGW(hRmI2cCont, I2C_SL_ADDR1, RegVal);
}

static void 
GetPacketHeaders(
    NvRmI2cControllerHandle hRmI2cCont,
    NvRmI2cTransactionInfo *pTransaction,
    NvU32 PacketId,
    NvU32 *pPacketHeader1,
    NvU32 *pPacketHeader2,
    NvU32 *pPacketHeader3)
{
    NvU32 PacketHeader1;
    NvU32 PacketHeader2;
    NvU32 PacketHeader3;

    // prepare Generic header1
    // Header size = 0 Protocol  = I2C,pktType = 0
    PacketHeader1 = NV_DRF_DEF(I2C, IO_PACKET_HEADER, HDRSZ, ONE) | 
                              NV_DRF_DEF(I2C, IO_PACKET_HEADER, PROTOCOL, I2C);
    
    // Set pkt id as 1
    PacketHeader1 = NV_FLD_SET_DRF_NUM(I2C, IO_PACKET_HEADER, PKTID, PacketId, PacketHeader1);
    
    // Controller id is according to the instance of the i2c/dvc
    PacketHeader1 = NV_FLD_SET_DRF_NUM(I2C, IO_PACKET_HEADER, 
                               CONTROLLER_ID, hRmI2cCont->ControllerId, PacketHeader1);
    
    PacketHeader2 = NV_FLD_SET_DRF_NUM(I2C, IO_PACKET_HEADER, 
                                    PAYLOADSIZE, (pTransaction->NumBytes - 1), 0);
    
    // prepare IO specific header
    // Configure the slave address
    PacketHeader3 = pTransaction->Address;
    
    // 10 bit address mode: Set address mode to 10 bit
    if (hRmI2cCont->Is10BitAddress)
        PacketHeader3 = NV_FLD_SET_DRF_DEF(I2C, IO_PACKET_HEADER, ADDR_MODE, 
                                                        TEN_BIT, PacketHeader3);
    
    hRmI2cCont->IsCurrentTransferNoAck = NV_FALSE;
    // Enable mode to handle devices that do not generate ACK 
    if (pTransaction->Flags & NVRM_I2C_NOACK)
    {
        PacketHeader3 = NV_FLD_SET_DRF_DEF(I2C, IO_PACKET_HEADER, CONTUNE_ON_NACK, 
                                                        ENABLE, PacketHeader3);
        hRmI2cCont->IsCurrentTransferNoAck = NV_TRUE;
    }
    
    hRmI2cCont->IsCurrentTransferNoStop = NV_FALSE;

    // Enable mode to repeat start if it is configured
    if (pTransaction->Flags & NVRM_I2C_NOSTOP)
    {
        PacketHeader3 = NV_FLD_SET_DRF_DEF(I2C, IO_PACKET_HEADER,REPEAT_START, 
                                                REPEAT_START, PacketHeader3);
        hRmI2cCont->IsCurrentTransferNoStop = NV_TRUE;
    }                                           

    hRmI2cCont->IsCurrentTransferRead = NV_FALSE;
    // Enable Read if it is required
    if (!(pTransaction->Flags & NVRM_I2C_WRITE))
    {
        PacketHeader3 = NV_FLD_SET_DRF_DEF(I2C, IO_PACKET_HEADER, READ, READ, 
                                                PacketHeader3);
        hRmI2cCont->IsCurrentTransferRead = NV_TRUE;
    }

    *pPacketHeader1 = PacketHeader1;
    *pPacketHeader2 = PacketHeader2;
    *pPacketHeader3 = PacketHeader3;
}

static void StartI2cPacketMode(NvRmI2cControllerHandle hRmI2cCont)
{
    NvU32 I2cConfig;
    // PACKET_MODE_TRANSFER_EN field of I2C Controller configuration Register
    I2cConfig = NV_DRF_DEF(I2C, I2C_CNFG, NEW_MASTER_FSM, ENABLE);
    I2cConfig = NV_FLD_SET_DRF_DEF(I2C, I2C_CNFG, PACKET_MODE_EN, GO, I2cConfig);
    I2C_REGW(hRmI2cCont, I2C_CNFG, I2cConfig);
}

static void 
DoTxFifoEmpty(
    NvRmI2cControllerHandle hRmI2cCont, 
    NvU32 *pFifoEmptyCount)
{
    NvU32 TFifoEmptyCount = 0;
    NvU32 FifoStatus;
    
    // Tx Fifo should be empty. If not force to make it empty
    FifoStatus = I2C_REGR(hRmI2cCont, FIFO_STATUS);
    TFifoEmptyCount = NV_DRF_VAL(I2C, FIFO_STATUS, TX_FIFO_EMPTY_CNT, FifoStatus);
    if (TFifoEmptyCount < I2C_FIFO_DEPTH)
        ResetTxFifo(hRmI2cCont);

    *pFifoEmptyCount = TFifoEmptyCount;
}

static void WriteIntMaksReg(NvRmI2cControllerHandle hRmI2cCont)
{
#if !USE_POLLING_METHOD
    I2C_REGW (hRmI2cCont, INTERRUPT_MASK_REGISTER, hRmI2cCont->IntMaskReg);
#endif
}
static void I2cIsr(void* args)
{
    NvRmI2cControllerHandle hRmI2cCont = (NvRmI2cControllerHandle)args;
    NvU32 FifoStatus;
    NvU32 WordCount;
    NvU32 FilledSlots;
    NvU32 MaxWordToRead;
    NvBool IsFinalIntGot = NV_FALSE;
    NvU32 FreeSlots;
    NvU32 MaxWordToWrite;
    // Read the Interrupt status register & PKT_STATUS
    hRmI2cCont->ControllerStatus = I2C_REGR(hRmI2cCont, INTERRUPT_STATUS_REGISTER);

    // Write one to clear in the interrupt status register
    I2C_REGW(hRmI2cCont, INTERRUPT_STATUS_REGISTER, hRmI2cCont->ControllerStatus);
    FifoStatus = I2C_REGR(hRmI2cCont, FIFO_STATUS);

    DEBUG_I2C_READ(1, ("ISR ContStatus 0x%08x FifoStatus 0x%08x\n", 
                                    hRmI2cCont->ControllerStatus, FifoStatus));

    if (hRmI2cCont->ControllerStatus & hRmI2cCont->FinalInterrupt)
        IsFinalIntGot = NV_TRUE;

    // If any error then stop transfer.
    if (hRmI2cCont->ControllerStatus & I2C_TRANSACTION_STATUS_ERRORS)
    {
        NvOsDebugPrintf("Err in I2c transfer: Controller Status 0x%08x \n",
                                hRmI2cCont->ControllerStatus);
        IsFinalIntGot = NV_TRUE;
        goto FinalIntDone;
    }

    if (hRmI2cCont->IsCurrentTransferRead)
    {
        // If there is remianing word to read then read here from fifo.
        if ((hRmI2cCont->WordRemaining) && (!hRmI2cCont->IsUsingApbDma))
        {
            DEBUG_I2C_READ(1, ("Reading RxFifo From Int\n"));

            // Get RFifo full count
            FilledSlots = NV_DRF_VAL(I2C, FIFO_STATUS, RX_FIFO_FULL_CNT, FifoStatus);
        
            MaxWordToRead = NV_MIN(hRmI2cCont->WordRemaining, FilledSlots);
            for (WordCount = 0; WordCount < MaxWordToRead; ++WordCount)
            {
                // Read data from the I2C RX pkt FIFO Register
                hRmI2cCont->pDataBuffer[hRmI2cCont->WordTransferred] = 
                                        I2C_REGR(hRmI2cCont, I2C_RX_FIFO);
                hRmI2cCont->WordTransferred++;
            }
            hRmI2cCont->WordRemaining -= MaxWordToRead;
        
            if ((IsFinalIntGot) || (hRmI2cCont->WordRemaining == 0))
                goto FinalIntDone;

            // If still want to receive more than the fifo depth then continue 
            // the int   
            if (hRmI2cCont->WordRemaining > I2C_FIFO_DEPTH)
                goto IntDone;

            if(hRmI2cCont->IsCurrentTransferNoStop)
            {
                // If remaining required read is less than fifo depth then enable the
                // all tranfer interrupt only and disable the fifo trigger level interrupt
                
                if (hRmI2cCont->WordRemaining < I2C_FIFO_DEPTH)
                    SetRxFifoTriggerLevel(hRmI2cCont, hRmI2cCont->WordRemaining);
                    
                hRmI2cCont->FinalInterrupt = NV_FLD_SET_DRF_DEF(I2C, INTERRUPT_MASK_REGISTER, 
                                RFIFO_DATA_REQ_INT_EN, ENABLE, hRmI2cCont->FinalInterrupt);
            }
            else
            {
                hRmI2cCont->IntMaskReg = NV_FLD_SET_DRF_DEF(I2C, INTERRUPT_MASK_REGISTER, 
                                          RFIFO_DATA_REQ_INT_EN, DISABLE, hRmI2cCont->IntMaskReg);

                hRmI2cCont->IntMaskReg = NV_FLD_SET_DRF_DEF(I2C, INTERRUPT_MASK_REGISTER, 
                                ALL_PACKETS_XFER_COMPLETE_INT_EN, ENABLE, hRmI2cCont->IntMaskReg);
                                
                
                hRmI2cCont->FinalInterrupt = NV_FLD_SET_DRF_DEF(I2C, INTERRUPT_MASK_REGISTER, 
                                ALL_PACKETS_XFER_COMPLETE_INT_EN, ENABLE, hRmI2cCont->FinalInterrupt);
                WriteIntMaksReg(hRmI2cCont);
            }
            goto IntDone;
        }
    }
    else
    {
        if (IsFinalIntGot)
            goto FinalIntDone;

        // If there is remaining word to write then keep writing it.
        if (hRmI2cCont->WordRemaining)
        {
            DEBUG_I2C_SEND(1, ("Writing Tx from int\n"));
            
            // Get TFifo empty count
            FreeSlots = NV_DRF_VAL(I2C, FIFO_STATUS, TX_FIFO_EMPTY_CNT, FifoStatus);
            MaxWordToWrite = NV_MIN(hRmI2cCont->WordRemaining, FreeSlots);
            for (WordCount = 0; WordCount < MaxWordToWrite; ++WordCount)
            {
                // Write data into the I2C TX pkt FIFO Register
                I2C_REGW(hRmI2cCont, I2C_TX_PACKET_FIFO, 
                        hRmI2cCont->pDataBuffer[hRmI2cCont->WordTransferred]);
                hRmI2cCont->WordTransferred++;
            }
            hRmI2cCont->WordRemaining -= MaxWordToWrite;

            if (hRmI2cCont->WordRemaining == 0)
            {
                if(hRmI2cCont->IsCurrentTransferNoStop)
                {
                    hRmI2cCont->FinalInterrupt = NV_FLD_SET_DRF_DEF(I2C, INTERRUPT_MASK_REGISTER, 
                                    TFIFO_DATA_REQ_INT_EN, ENABLE, hRmI2cCont->FinalInterrupt);
                }
                else
                {
                    hRmI2cCont->IntMaskReg = NV_FLD_SET_DRF_DEF(I2C, INTERRUPT_MASK_REGISTER, 
                                              TFIFO_DATA_REQ_INT_EN, DISABLE, hRmI2cCont->IntMaskReg);
                
                    hRmI2cCont->IntMaskReg = NV_FLD_SET_DRF_DEF(I2C, INTERRUPT_MASK_REGISTER, 
                                    ALL_PACKETS_XFER_COMPLETE_INT_EN, ENABLE, hRmI2cCont->IntMaskReg);
                                    
                    
                    hRmI2cCont->FinalInterrupt = NV_FLD_SET_DRF_DEF(I2C, INTERRUPT_MASK_REGISTER, 
                                    ALL_PACKETS_XFER_COMPLETE_INT_EN, ENABLE, hRmI2cCont->FinalInterrupt);
                    WriteIntMaksReg(hRmI2cCont);
                }                
            }
        }
        goto IntDone;
    }
FinalIntDone:    
    if(IsFinalIntGot)
    {
        // Clear interrupt mask register, and DVC interrupt status for DVC I2C.
        // Note that h/w clean up and use of hRmI2cCont (shared with transaction
        // API thread) must be completed in ISR before semaphore is signaled.
        I2C_REGW (hRmI2cCont, INTERRUPT_MASK_REGISTER, 0);
        hRmI2cCont->IsTransferCompleted = NV_TRUE;
        if (hRmI2cCont->ModuleId == NvRmModuleID_Dvc)
            DVC_REGW(hRmI2cCont, STATUS_REG,
                     NV_DRF_NUM(DVC, STATUS_REG, I2C_DONE_INTR, 1));
        NvOsSemaphoreSignal(hRmI2cCont->I2cSyncSemaphore);
        goto Done;
    }

    NvOsDebugPrintf("AP20 I2c Isr got unwanted interrupt IntStatus 0x%08x\n", 
                                                    hRmI2cCont->ControllerStatus);
    NV_ASSERT(0);
    
IntDone:    
    if (hRmI2cCont->ModuleId == NvRmModuleID_Dvc)
        DVC_REGW(hRmI2cCont, STATUS_REG, NV_DRF_NUM(DVC, STATUS_REG, I2C_DONE_INTR, 1));
Done:        
    NvRmInterruptDone(hRmI2cCont->I2CInterruptHandle);
}

#if USE_POLLING_METHOD
static NvError WaitForTransactionCompletesPolling(
    NvRmI2cControllerHandle hRmI2cCont, 
    NvU32 Timeout)
{
    NvU32 RemainingTime;
    RemainingTime = (Timeout == NV_WAIT_INFINITE)?Timeout:Timeout*1000;
    do {
        NvOsWaitUS(I2C_POLLING_TIMEOUT_STEP_USEC);

        // Read the Interrupt status register & PKT_STATUS
        hRmI2cCont->ControllerStatus = I2C_REGR(hRmI2cCont, INTERRUPT_STATUS_REGISTER);
        if (hRmI2cCont->ControllerStatus & hRmI2cCont->IntMaskReg)
        {
            I2cIsr(hRmI2cCont);
            if (hRmI2cCont->IsTransferCompleted)
                break;
        }

        if (Timeout != NV_WAIT_INFINITE)
            RemainingTime = (RemainingTime > I2C_POLLING_TIMEOUT_STEP_USEC)? 
                                    (RemainingTime - I2C_POLLING_TIMEOUT_STEP_USEC): 0;
    } while(RemainingTime);

    if (!RemainingTime)
        return NvError_Timeout;

    return NvSuccess;
}
#endif

static NvError WaitForTransactionCompletes(
    NvRmI2cControllerHandle hRmI2cCont, 
    NvU32 Timeout)
{
    NvError Error;

#if USE_POLLING_METHOD
    hRmI2cCont->IsTransferCompleted = NV_FALSE;
    Error = WaitForTransactionCompletesPolling(hRmI2cCont, hRmI2cCont->timeout);
#else
    // Wait for the  Transfer completes
    Error = NvOsSemaphoreWaitTimeout(hRmI2cCont->I2cSyncSemaphore, Timeout);
#endif
    return Error;
}


static NvError 
DoOneReceiveTransaction(
    NvRmI2cControllerHandle hRmI2cCont,
    NvU32 PacketId,
    NvU8*  pBuffer,
    NvRmI2cTransactionInfo *pTransaction,
    NvU32* pBytesTransferred)
{
    NvU32 WordsToRead = 0;
    NvU32 TFifoEmptyCount = 0;
    NvError Error = NvSuccess;
    NvU32 PacketHeader1;
    NvU32 PacketHeader2;
    NvU32 PacketHeader3;
    NvU32 IntMaskReg;
    NvRmDmaModuleID DmaModuleId = NvRmDmaModuleID_I2c;
    NvU32 BytesRead;


    hRmI2cCont->WordTransferred = 0;
    hRmI2cCont->WordRemaining = 0;
    hRmI2cCont->TransCountFromLastDmaUsage++;

    GetPacketHeaders(hRmI2cCont, pTransaction, PacketId, &PacketHeader1, 
                                            &PacketHeader2, &PacketHeader3);
    
    DoTxFifoEmpty(hRmI2cCont, &TFifoEmptyCount);
    
    // Enable all possible i2c controller error.
    IntMaskReg  = I2C_ERRORS_INTERRUPT_MASK;

    if (!hRmI2cCont->IsCurrentTransferNoAck)
        IntMaskReg = NV_FLD_SET_DRF_DEF(I2C, INTERRUPT_MASK_REGISTER, 
                                    NOACK_INT_EN, ENABLE, IntMaskReg);

    hRmI2cCont->FinalInterrupt = IntMaskReg;

    // Words to read 
    WordsToRead = BYTES_TO_WORD(pTransaction->NumBytes);
    hRmI2cCont->WordTransferred = 0;
    hRmI2cCont->WordRemaining = WordsToRead;

    hRmI2cCont->IsTransferCompleted = NV_FALSE;

    // If requested size is more than cpu transaction thresold then use dma.
    if ((hRmI2cCont->DmaBufferSize) && 
            (hRmI2cCont->WordRemaining > I2C_MAX_WORD_TO_USE_CPU))
    {
        if (!hRmI2cCont->IsApbDmaAllocated)
        {
            if (hRmI2cCont->ModuleId == NvRmModuleID_Dvc)
                DmaModuleId =NvRmDmaModuleID_Dvc;
                
            Error = NvRmDmaAllocate(hRmI2cCont->hRmDevice, &hRmI2cCont->hRmDma,
                             NV_FALSE, NvRmDmaPriority_High, DmaModuleId,
                             hRmI2cCont->Instance);
            if (!Error)
                hRmI2cCont->IsApbDmaAllocated = NV_TRUE;
            Error = NvSuccess;
        }
        if (!hRmI2cCont->IsApbDmaAllocated)
            goto CpuBasedReading;
        
        hRmI2cCont->IsUsingApbDma = NV_TRUE;
        hRmI2cCont->TransCountFromLastDmaUsage = 0;
        hRmI2cCont->RxDmaReq.TransferSize = hRmI2cCont->WordRemaining << 2;
        SetRxFifoTriggerLevel(hRmI2cCont, 1);
        if (hRmI2cCont->IsCurrentTransferNoStop)
        {
#if USE_POLLING_METHOD
            goto CpuBasedReading;
#else
            Error = NvRmDmaStartDmaTransfer(hRmI2cCont->hRmDma, &hRmI2cCont->RxDmaReq,
                    NvRmDmaDirection_Forward, 0, hRmI2cCont->I2cSyncSemaphore);
#endif                    
        }
        else
        {
            Error = NvRmDmaStartDmaTransfer(hRmI2cCont->hRmDma, &hRmI2cCont->RxDmaReq,
                    NvRmDmaDirection_Forward, 0, NULL);
        }
        if (!Error)
        {
            hRmI2cCont->ControllerStatus = 0;
            I2C_REGW(hRmI2cCont, I2C_TX_PACKET_FIFO, PacketHeader1);
            I2C_REGW(hRmI2cCont, I2C_TX_PACKET_FIFO, PacketHeader2);
            
            // Write I2C specific header
            I2C_REGW(hRmI2cCont, I2C_TX_PACKET_FIFO, PacketHeader3);
        
            if (!hRmI2cCont->IsCurrentTransferNoStop)
            {
                IntMaskReg = NV_FLD_SET_DRF_DEF(I2C, INTERRUPT_MASK_REGISTER, 
                                ALL_PACKETS_XFER_COMPLETE_INT_EN, ENABLE, IntMaskReg);
                hRmI2cCont->FinalInterrupt = IntMaskReg;
            }
            hRmI2cCont->IntMaskReg = IntMaskReg;
            WriteIntMaksReg(hRmI2cCont);
            goto WaitForCompletion;
        }
        Error = NvSuccess;
    }

CpuBasedReading:    
    hRmI2cCont->IsUsingApbDma = NV_FALSE;

    // Enable the Rx trigger level interrupt if the word to read is more than
    // fifo depth or no stop transfer is selected
    if ((hRmI2cCont->WordRemaining > I2C_FIFO_DEPTH) ||
                                hRmI2cCont->IsCurrentTransferNoStop)
    {
        SetRxFifoTriggerLevel(hRmI2cCont, hRmI2cCont->WordRemaining);
        IntMaskReg = NV_FLD_SET_DRF_DEF(I2C, INTERRUPT_MASK_REGISTER, 
                                    RFIFO_DATA_REQ_INT_EN, ENABLE, IntMaskReg);
    }
    else
    {
        IntMaskReg = NV_FLD_SET_DRF_DEF(I2C, INTERRUPT_MASK_REGISTER, 
                                    ALL_PACKETS_XFER_COMPLETE_INT_EN, ENABLE, IntMaskReg);
        hRmI2cCont->FinalInterrupt = IntMaskReg;
    }
    
    hRmI2cCont->IntMaskReg = IntMaskReg;

    WriteIntMaksReg(hRmI2cCont);

    //Write Generic Header1 & 2
    I2C_REGW(hRmI2cCont, I2C_TX_PACKET_FIFO, PacketHeader1);
    I2C_REGW(hRmI2cCont, I2C_TX_PACKET_FIFO, PacketHeader2);
    
    // Write I2C specific header
    I2C_REGW(hRmI2cCont, I2C_TX_PACKET_FIFO, PacketHeader3);

WaitForCompletion:
    Error = WaitForTransactionCompletes(hRmI2cCont, hRmI2cCont->timeout);
    if (Error == NvSuccess)
    {
        hRmI2cCont->I2cTransferStatus = NvError_I2cReadFailed;
        if (hRmI2cCont->ControllerStatus & I2C_TRANSACTION_STATUS_ERRORS)
        {
            if (hRmI2cCont->ControllerStatus & I2C_ARBITRATION_LOST_ERRORS)
                hRmI2cCont->I2cTransferStatus = NvError_I2cArbitrationFailed;
            else
                hRmI2cCont->I2cTransferStatus = NvError_I2cInternalError;

            goto ReadExitWithReset;
        }
        else
        {
            if (!hRmI2cCont->IsCurrentTransferNoAck)
            {
                if(hRmI2cCont->ControllerStatus & 
                    NV_DRF_DEF(I2C, INTERRUPT_MASK_REGISTER, NOACK_INT_EN, ENABLE))
                {    
                    hRmI2cCont->I2cTransferStatus = NvError_I2cDeviceNotFound;
                    goto ReadExitWithReset;
                }
            }
            // Memcopy fifo back to actual buffer given by client
            if (hRmI2cCont->IsUsingApbDma)
            {
                BytesRead = NV_MIN(pTransaction->NumBytes, 
                                        hRmI2cCont->RxDmaReq.TransferSize);
                NvOsMemcpy(pBuffer, (NvU8* )hRmI2cCont->pDmaBuffer, BytesRead);
            }
            else
            {
                BytesRead = NV_MIN(pTransaction->NumBytes, 
                                        (4*hRmI2cCont->WordTransferred));
                if (BytesRead != pTransaction->NumBytes)
                {
                    hRmI2cCont->I2cTransferStatus = NvError_I2cReadFailed;
                    goto ReadExitWithReset;
                }
                NvOsMemcpy(pBuffer, (NvU8* )hRmI2cCont->pDataBuffer, BytesRead);
            }

            if (pBytesTransferred != NULL)
                *pBytesTransferred = BytesRead;

            
            hRmI2cCont->I2cTransferStatus = NvSuccess;
            goto ReadExit;
        }
    }
    else if (Error == NvError_Timeout)
    {
        DEBUG_I2C_READ(1, ("Read Timeout Error \n"));
        hRmI2cCont->I2cTransferStatus = NvError_Timeout;
    }

ReadExitWithReset:
    // If we reach here then there is something wrong in transfer, reset the module.
    if (hRmI2cCont->IsUsingApbDma)
        NvRmDmaAbort(hRmI2cCont->hRmDma);

    // If there is NACK error, then there is possibilty that i2c controller is 
    // still busy to send the stop signal.
    // Wait for 2x of i2c clock period is recommended, waiting for 1 ms to use 
    // the NvOsMsSleep api.
    NvOsSleepMS(1);
        
    NvRmModuleReset(hRmI2cCont->hRmDevice, 
                NVRM_MODULE_ID(hRmI2cCont->ModuleId, hRmI2cCont->Instance));
    RESET_SEMA_COUNT(hRmI2cCont->I2cSyncSemaphore);
ReadExit:    
    DEBUG_I2C_READ(1, ("Read Transfer Status 0x%08x \n", hRmI2cCont->I2cTransferStatus));
    
    // Time to free dma??
    if ((hRmI2cCont->IsApbDmaAllocated) && 
        (hRmI2cCont->TransCountFromLastDmaUsage > HOLDING_DMA_TRANSACTION_COUNT))
    {
        NvRmDmaFree(hRmI2cCont->hRmDma);
        hRmI2cCont->hRmDma = NULL;
        hRmI2cCont->IsApbDmaAllocated = NV_FALSE;
    }
    return hRmI2cCont->I2cTransferStatus;
}


static NvError 
DoOneSendTransaction(
    NvRmI2cControllerHandle hRmI2cCont,
    NvU32 PacketId,
    NvU8*  pBuffer,
    NvRmI2cTransactionInfo *pTransaction,
    NvU32* pBytesTransferred)
{
    NvU32 WordsToSend = 0;
    NvU32 TFifoEmptyCount = 0;
    NvError Error = NvSuccess;
    NvU32 PacketHeader1;
    NvU32 PacketHeader2;
    NvU32 PacketHeader3;
    NvU32 IntMaskReg;
    NvU32 WordCount;
    NvRmDmaModuleID DmaModuleId = NvRmDmaModuleID_I2c;

    hRmI2cCont->WordTransferred = 0;
    hRmI2cCont->WordRemaining = 0;
    hRmI2cCont->TransCountFromLastDmaUsage++;

    GetPacketHeaders(hRmI2cCont, pTransaction, PacketId, &PacketHeader1, 
                                            &PacketHeader2, &PacketHeader3);
    DoTxFifoEmpty(hRmI2cCont, &TFifoEmptyCount);
    
    // Enable all possible i2c controller error.
    IntMaskReg  = I2C_ERRORS_INTERRUPT_MASK;

    if (!hRmI2cCont->IsCurrentTransferNoAck)
        IntMaskReg = NV_FLD_SET_DRF_DEF(I2C, INTERRUPT_MASK_REGISTER, 
                                    NOACK_INT_EN, ENABLE, IntMaskReg);

    hRmI2cCont->FinalInterrupt = IntMaskReg;

    // Words to write 
    WordsToSend = BYTES_TO_WORD(pTransaction->NumBytes);
    hRmI2cCont->WordTransferred = 0;
    hRmI2cCont->WordRemaining = WordsToSend;
    hRmI2cCont->IsTransferCompleted = NV_FALSE;

    if ((hRmI2cCont->DmaBufferSize) && 
            (hRmI2cCont->WordRemaining > I2C_MAX_WORD_TO_USE_CPU))
    {
        if (!hRmI2cCont->IsApbDmaAllocated)
        {
            if (hRmI2cCont->ModuleId == NvRmModuleID_Dvc)
                DmaModuleId =NvRmDmaModuleID_Dvc;
                
            Error = NvRmDmaAllocate(hRmI2cCont->hRmDevice, &hRmI2cCont->hRmDma,
                             NV_FALSE, NvRmDmaPriority_High, DmaModuleId,
                             hRmI2cCont->Instance);
            if (!Error)
                hRmI2cCont->IsApbDmaAllocated = NV_TRUE;
            Error = NvSuccess;
        }
        if (!hRmI2cCont->IsApbDmaAllocated)
            goto CpuBasedWriting;
    
        hRmI2cCont->IsUsingApbDma = NV_TRUE;
        hRmI2cCont->TransCountFromLastDmaUsage = 0;
        hRmI2cCont->pDmaBuffer[0] = PacketHeader1;
        hRmI2cCont->pDmaBuffer[1] = PacketHeader2;
        hRmI2cCont->pDmaBuffer[2] = PacketHeader3;
        hRmI2cCont->TxDmaReq.TransferSize = (hRmI2cCont->WordRemaining + 3) << 2;
        NvOsMemcpy(hRmI2cCont->pDmaBuffer + 3, (void *)pBuffer, pTransaction->NumBytes);
        SetTxFifoTriggerLevel(hRmI2cCont, 8);
        Error = NvRmDmaStartDmaTransfer(hRmI2cCont->hRmDma, &hRmI2cCont->TxDmaReq,
                NvRmDmaDirection_Forward, 0, NULL);
        if (!Error)
        {
            hRmI2cCont->WordRemaining = 0;
            if (hRmI2cCont->IsCurrentTransferNoStop)
            {
                IntMaskReg = NV_FLD_SET_DRF_DEF(I2C, INTERRUPT_MASK_REGISTER, 
                                        TFIFO_DATA_REQ_INT_EN, ENABLE, IntMaskReg);
                hRmI2cCont->FinalInterrupt = IntMaskReg;                        
            }
            else
            {
                IntMaskReg = NV_FLD_SET_DRF_DEF(I2C, INTERRUPT_MASK_REGISTER, 
                                ALL_PACKETS_XFER_COMPLETE_INT_EN, ENABLE, IntMaskReg);
                hRmI2cCont->FinalInterrupt = IntMaskReg;
            }
            goto WaitForCompletion;
        }
        
//        NvOsDebugPrintf("Send Using Dma\n");
        Error = NvSuccess;
    }

CpuBasedWriting:
    hRmI2cCont->IsUsingApbDma = NV_FALSE;

    //Write Generic Header1 & 2
    I2C_REGW(hRmI2cCont, I2C_TX_PACKET_FIFO, PacketHeader1);
    I2C_REGW(hRmI2cCont, I2C_TX_PACKET_FIFO, PacketHeader2);
    
    // Write I2C specific header
    I2C_REGW(hRmI2cCont, I2C_TX_PACKET_FIFO, PacketHeader3);
    
    TFifoEmptyCount -= 3;
    
    if (hRmI2cCont->WordRemaining)
    {
        NvOsMemcpy(hRmI2cCont->pDataBuffer, (void *)pBuffer, pTransaction->NumBytes);
        
        WordsToSend = NV_MIN(hRmI2cCont->WordRemaining, TFifoEmptyCount);
        for (WordCount = 0; WordCount < WordsToSend; WordCount++)
        {
            // Write data into the I2C TX pkt FIFO Register
            I2C_REGW(hRmI2cCont, I2C_TX_PACKET_FIFO, 
                        hRmI2cCont->pDataBuffer[hRmI2cCont->WordTransferred]);
            hRmI2cCont->WordTransferred++;
        }
        hRmI2cCont->WordRemaining -= WordsToSend;

        if (hRmI2cCont->WordRemaining == 0)
        {
            if (hRmI2cCont->IsCurrentTransferNoStop)
            {
                SetTxFifoTriggerLevel(hRmI2cCont, 8);
                IntMaskReg = NV_FLD_SET_DRF_DEF(I2C, INTERRUPT_MASK_REGISTER, 
                                        TFIFO_DATA_REQ_INT_EN, ENABLE, IntMaskReg);
                hRmI2cCont->FinalInterrupt = IntMaskReg;                        
            }
            else
            {
                IntMaskReg = NV_FLD_SET_DRF_DEF(I2C, INTERRUPT_MASK_REGISTER, 
                                ALL_PACKETS_XFER_COMPLETE_INT_EN, ENABLE, IntMaskReg);
                hRmI2cCont->FinalInterrupt = IntMaskReg;
            }
        }
        else
        {
            SetTxFifoTriggerLevel(hRmI2cCont, 8);
            IntMaskReg = NV_FLD_SET_DRF_DEF(I2C, INTERRUPT_MASK_REGISTER, 
                                    TFIFO_DATA_REQ_INT_EN, ENABLE, IntMaskReg);
        }
    }

WaitForCompletion:
    hRmI2cCont->IntMaskReg = IntMaskReg;
    WriteIntMaksReg(hRmI2cCont);
    Error = WaitForTransactionCompletes(hRmI2cCont, hRmI2cCont->timeout);
    if (Error == NvSuccess)
    {
        hRmI2cCont->I2cTransferStatus = NvError_I2cWriteFailed;
        if (hRmI2cCont->ControllerStatus & I2C_TRANSACTION_STATUS_ERRORS)
        {
            if (hRmI2cCont->ControllerStatus & I2C_ARBITRATION_LOST_ERRORS)
                hRmI2cCont->I2cTransferStatus = NvError_I2cArbitrationFailed;
            else
                hRmI2cCont->I2cTransferStatus = NvError_I2cInternalError;
            goto WriteExitWithReset;
        }   
        else
        {
            if (!hRmI2cCont->IsCurrentTransferNoAck)
            {
                if(hRmI2cCont->ControllerStatus & 
                    NV_DRF_DEF(I2C, INTERRUPT_MASK_REGISTER, NOACK_INT_EN, ENABLE))
                {    
                    hRmI2cCont->I2cTransferStatus = NvError_I2cDeviceNotFound;
                    goto WriteExitWithReset;
                }
            }   
            if (pBytesTransferred != NULL)
                *pBytesTransferred = pTransaction->NumBytes;
            hRmI2cCont->I2cTransferStatus = NvSuccess;
            goto WriteExit;
        }
    }
    else if (Error == NvError_Timeout)
    {
        DEBUG_I2C_SEND(1, ("SEND Timeout Error \n"));
        hRmI2cCont->I2cTransferStatus = NvError_Timeout;
    }
    
WriteExitWithReset:
    if (hRmI2cCont->IsUsingApbDma)
        NvRmDmaAbort(hRmI2cCont->hRmDma);

    // If there is NACK error, then there is possibilty that i2c controller is 
    // still busy to send the stop signal.
    // Wait for 2x of i2c clock period is recommended, waiting for 1 ms to use 
    // the NvOsMsSleep api.
    NvOsSleepMS(1);
    
    // If we reach here then there is something wrong in transfer, reset the module.
    NvRmModuleReset(hRmI2cCont->hRmDevice, 
                NVRM_MODULE_ID(hRmI2cCont->ModuleId, hRmI2cCont->Instance));
    RESET_SEMA_COUNT(hRmI2cCont->I2cSyncSemaphore);
WriteExit:    
    DEBUG_I2C_SEND(1, ("Send Transfer Status 0x%08x \n", hRmI2cCont->I2cTransferStatus));

    // Time to free dma??
    if ((hRmI2cCont->IsApbDmaAllocated) && 
        (hRmI2cCont->TransCountFromLastDmaUsage > HOLDING_DMA_TRANSACTION_COUNT))
    {
        NvRmDmaFree(hRmI2cCont->hRmDma);
        hRmI2cCont->hRmDma = NULL;
        hRmI2cCont->IsApbDmaAllocated = NV_FALSE;
    }
    
    return hRmI2cCont->I2cTransferStatus;
}


static NvError 
DoMultiReceiveTransaction(
    NvRmI2cControllerHandle hRmI2cCont,
    NvU32 *pPacketId,
    NvU8*  pBuffer,
    const NvRmI2cTransactionInfo *pTransaction,
    NvU32* pBytesTransferred)
{
    NvRmI2cTransactionInfo Transaction;
    NvU32 BytesTransferredYet = 0;
    NvU32 TotalBytesRequested;
    NvU8 *pReadBuffer = pBuffer;
    NvError Error = NvSuccess;
    NvU32 CurrBytesRequested;
    NvU32 PacketId;
    NvU32 BytesTransferred = 0;

    Transaction.Is10BitAddress = pTransaction->Is10BitAddress;
    Transaction.Address = pTransaction->Address;
    TotalBytesRequested = pTransaction->NumBytes;
    PacketId = *pPacketId;
    while (TotalBytesRequested)
    {
        Transaction.Flags = pTransaction->Flags;
        CurrBytesRequested = TotalBytesRequested;
        if (TotalBytesRequested > MAX_I2C_ONE_TRANSACTION_SIZE)
        {
            Transaction.Flags |= NVRM_I2C_NOSTOP;
            CurrBytesRequested = MAX_I2C_ONE_TRANSACTION_SIZE;
        }
        Transaction.NumBytes = CurrBytesRequested;
        Error = DoOneReceiveTransaction(hRmI2cCont, PacketId, pReadBuffer, 
                                &Transaction, &BytesTransferred);
        if (Error)
            break;
        BytesTransferredYet += CurrBytesRequested;  
        pReadBuffer += CurrBytesRequested;
        TotalBytesRequested -= CurrBytesRequested;
        PacketId++;
        I2C_REGW(hRmI2cCont, INTERRUPT_MASK_REGISTER, 0);
    }
    *pPacketId = PacketId;
    *pBytesTransferred = BytesTransferredYet;
    return Error;
}

static NvError
DoMultiSendTransaction(
    NvRmI2cControllerHandle hRmI2cCont,
    NvU32 *pPacketId,
    NvU8*  pBuffer,
    const NvRmI2cTransactionInfo *pTransaction,
    NvU32* pBytesTransferred)

{
    NvRmI2cTransactionInfo Transaction;
    NvU32 BytesTransferredYet = 0;
    NvU32 TotalBytesRequested;
    NvU8 *pReadBuffer = pBuffer;
    NvError Error = NvSuccess;
    NvU32 CurrBytesRequested;
    NvU32 PacketId;
    NvU32 BytesTransferred = 0;
    
    Transaction.Is10BitAddress = pTransaction->Is10BitAddress;
    Transaction.Address = pTransaction->Address;
    TotalBytesRequested = pTransaction->NumBytes;
    PacketId = *pPacketId;
    while (TotalBytesRequested)
    {
        Transaction.Flags = pTransaction->Flags;
        CurrBytesRequested = TotalBytesRequested;
        if (TotalBytesRequested > MAX_I2C_ONE_TRANSACTION_SIZE)
        {
            Transaction.Flags |= NVRM_I2C_NOSTOP;
            CurrBytesRequested = MAX_I2C_ONE_TRANSACTION_SIZE;
        }
        Transaction.NumBytes = CurrBytesRequested;
        Error = DoOneSendTransaction(hRmI2cCont, PacketId, pReadBuffer, 
                                &Transaction, &BytesTransferred);
        if (Error)
            break;
        BytesTransferredYet += CurrBytesRequested;  
        pReadBuffer += CurrBytesRequested;
        TotalBytesRequested -= CurrBytesRequested;
        PacketId++;
        I2C_REGW(hRmI2cCont, INTERRUPT_MASK_REGISTER, 0);
    }
    *pPacketId = PacketId;
    *pBytesTransferred = BytesTransferred;
    return Error;
}


static NvError 
AP20RmI2cReceive(
    NvRmI2cControllerHandle hRmI2cCont,
    NvU8*  pBuffer,
    const NvRmI2cTransactionInfo *pTransaction,
    NvU32* pBytesTransferred)
{
    NvError Error = NvSuccess;
    NvU32 PacketId = 1;
    
    NV_ASSERT(pBuffer);
    NV_ASSERT(pTransaction->NumBytes > 0);

    DEBUG_I2C_TRACE(1, ("AP20RmI2cReceive()++ 0x%08x and add 0x%02x\n", pTransaction->NumBytes, Transaction.Address));

    if (hRmI2cCont->ModuleId == NvRmModuleID_Dvc)
        DoDvcI2cControlInitialization(hRmI2cCont);
        
    // Clear interrupt mask register to avoid any false interrupts.
    I2C_REGW(hRmI2cCont, INTERRUPT_MASK_REGISTER, 0);
    
    // Start the packet mode
    StartI2cPacketMode(hRmI2cCont);

    Error = DoMultiReceiveTransaction(hRmI2cCont, &PacketId, pBuffer, 
                                      pTransaction, pBytesTransferred);
    DEBUG_I2C_TRACE(1, ("AP20RmI2cReceive()-- 0x%08x\n", Error));
    return Error;
}


static NvError
AP20RmI2cSend(
    NvRmI2cControllerHandle hRmI2cCont,
    NvU8*  pBuffer,
    const NvRmI2cTransactionInfo *pTransaction,
    NvU32* pBytesTransferred)
{
    NvError Error = NvSuccess;
    NvU32 PacketId = 1;

    NV_ASSERT(pBuffer);
    NV_ASSERT(pTransaction->NumBytes > 0);

    DEBUG_I2C_TRACE(1, ("AP20RmI2cSend()++ 0x%08x and 0x%02x\n", pTransaction->NumBytes, Transaction.Address));

    if (hRmI2cCont->ModuleId == NvRmModuleID_Dvc)
        DoDvcI2cControlInitialization(hRmI2cCont);

    // Clear interrupt mask register to avoid any false interrupts.
    I2C_REGW(hRmI2cCont, INTERRUPT_MASK_REGISTER, 0);
    
    // Start the packet mode
    StartI2cPacketMode(hRmI2cCont);

    Error = DoMultiSendTransaction(hRmI2cCont, &PacketId, pBuffer, 
                                   pTransaction, pBytesTransferred);
    DEBUG_I2C_TRACE(1, ("AP20RmI2cSend()-- 0x%08x\n", Error));
    return Error;
}

static NvError
AP20RmI2cRepeatStartTransaction(
    NvRmI2cControllerHandle hRmI2cCont,
    NvU8* pBuffer,
    NvRmI2cTransactionInfo *pTransactions,
    NvU32 NoOfTransations)
{
    NvError Error = NvSuccess;
    NvU8 *pReqBuffer = pBuffer;
    NvU32 BytesSend;
    NvU32 BytesRecvd;
    NvU32 PacketId;
    NvU32 TransCount;
    
    NV_ASSERT(pBuffer);
    NV_ASSERT(pTransactions);
    NV_ASSERT(pBuffer);
    
    DEBUG_I2C_TRACE(1, ("AP20RmI2cRepeatStartTransaction()++ 0x%08x and 0x%02x\n", NoOfTransations, pTransactions[0].Address));

    if (hRmI2cCont->ModuleId == NvRmModuleID_Dvc)
    {    
        DoDvcI2cControlInitialization(hRmI2cCont);
        UseDvcI2cNewSlave(hRmI2cCont);
    }

    // Clear interrupt mask register to avoid any false interrupts.
    I2C_REGW(hRmI2cCont, INTERRUPT_MASK_REGISTER, 0);
    
    // Start the packet mode
    StartI2cPacketMode(hRmI2cCont);

    PacketId = 1;
    for (TransCount = 0; TransCount < NoOfTransations; TransCount++)
    {
        if (pTransactions[TransCount].Flags & NVRM_I2C_WRITE) 
        {
            Error = DoMultiSendTransaction(hRmI2cCont, &PacketId, 
                        pReqBuffer, &pTransactions[TransCount], &BytesSend);
        }
        else
        {
            Error = DoMultiReceiveTransaction(hRmI2cCont, &PacketId, 
                        pReqBuffer, &pTransactions[TransCount], &BytesRecvd);
        }
        if (Error)
        {
            DEBUG_I2C_TRACE(1, ("AP20RmI2cRepeatStartTransaction()-- 0x%08x at Transaction %d \n", Error, TransCount));
            break;
        }    
        pReqBuffer += pTransactions[TransCount].NumBytes;
        
        I2C_REGW(hRmI2cCont, INTERRUPT_MASK_REGISTER, 0);
        PacketId++;
    }
    DEBUG_I2C_TRACE(1, ("AP20RmI2cRepeatStartTransaction()-- 0x%08x at Transaction %d \n", Error, TransCount));
    return Error;
}

static NvBool AP20RmI2cGetGpioPins(
    NvRmI2cControllerHandle hRmI2cCont,
    NvU32 I2cPinMap,
    NvU32 *pScl,
    NvU32 *pSda)
{
    NvU32 SclPin = 0;
    NvU32 SdaPin = 0;
    NvU32 SclPort = 0;
    NvU32 SdaPort = 0;
    NvBool Result = NV_TRUE;

    NV_ASSERT((pScl!=NULL) && (pSda!=NULL));

    if (hRmI2cCont->ModuleId == NvRmModuleID_I2c)
    {
        switch ((hRmI2cCont->Instance<<4) | I2cPinMap)
        {
            case ((0<<4) | 1):
                SclPort = 'c' - 'a';
                SdaPort = 'c' - 'a';
                SclPin = 4;
                SdaPin = 5;
                break;
            case ((0<<4) | 2):
                SclPort = 'k' - 'a';
                SdaPort = 'k' - 'a';
                SclPin = 5;
                SdaPin = 6;
                break;
            case ((0<<4) | 3):
                SclPort = 'w' - 'a';
                SdaPort = 'w' - 'a';
                SclPin = 2;
                SdaPin = 3;
                break;
                /*  NOTE:  The pins used by pin map 1 for instance 1 are not
                 *  connected to a GPIO controller (DDC pins), so the software
                 *  fallback is not supported for them.  */
            case ((1<<4) | 2):
                SclPort = 't' - 'a';
                SdaPort = 't' - 'a';
                SclPin = 5;
                SdaPin = 6;
                break;
            case ((2<<4) | 1):
                //  Port 'BB'
                SclPort = 'z' - 'a' + 2;
                SdaPort = 'z' - 'a' + 2;
                SclPin = 2;
                SdaPin = 3;
                break;
            default:
                Result = NV_FALSE;
        }
    }
    else if ((hRmI2cCont->ModuleId == NvRmModuleID_Dvc) &&
                 (hRmI2cCont->Instance == 0) &&
                     (I2cPinMap == NvOdmI2cPmuPinMap_Config1))
    {
        SclPin = 6;
        SdaPin = 7;
        SclPort = 'z' - 'a';
        SdaPort = 'z' - 'a';
    }
    else
        Result = NV_FALSE;

    *pScl = (SclPin | (SclPort<<16));
    *pSda = (SdaPin | (SdaPort<<16));
    return Result;
}

static void AP20RmI2cClose(NvRmI2cControllerHandle hRmI2cCont)
{

    if (hRmI2cCont->I2cSyncSemaphore)
    {
#if !USE_POLLING_METHOD
        NvRmInterruptUnregister(hRmI2cCont->hRmDevice, hRmI2cCont->I2CInterruptHandle);
#endif
        NvOsSemaphoreDestroy(hRmI2cCont->I2cSyncSemaphore);
        hRmI2cCont->I2cSyncSemaphore = NULL;
        hRmI2cCont->I2CInterruptHandle = NULL;
    }

    if (hRmI2cCont->pCpuBuffer)
    {
        NvOsFree(hRmI2cCont->pCpuBuffer);
        hRmI2cCont->pCpuBuffer = NULL;
        hRmI2cCont->pDataBuffer = NULL;
    }

    if (hRmI2cCont->hRmDma)
    {
        NvRmDmaAbort(hRmI2cCont->hRmDma);
        NvRmDmaFree(hRmI2cCont->hRmDma);
    }
    
    DestroyDmaTransferBuffer(hRmI2cCont->hRmMemory, hRmI2cCont->pDmaBuffer,
                hRmI2cCont->DmaBufferSize);

    hRmI2cCont->hRmDma = NULL;
    hRmI2cCont->hRmMemory = NULL;
    hRmI2cCont->DmaBuffPhysAdd = 0;
    hRmI2cCont->pDmaBuffer = NULL;

    hRmI2cCont->receive = 0;
    hRmI2cCont->send = 0;
    hRmI2cCont->repeatStart = 0;
    hRmI2cCont->close = 0;
    hRmI2cCont->GetGpioPins = 0;
}

NvError AP20RmI2cOpen(NvRmI2cControllerHandle hRmI2cCont)
{
    NvError Error = NvSuccess;
#if !USE_POLLING_METHOD
    NvU32 IrqList;
    NvOsInterruptHandler IntHandlers = I2cIsr;
#endif
    NvU32 RxFifoPhyAddress;
    NvU32 TxFifoPhyAddress;
    
    NV_ASSERT(hRmI2cCont);
    DEBUG_I2C_TRACE(1, ("AP20RmI2cOpen\n"));

    // Polulate the structures 
    hRmI2cCont->receive = AP20RmI2cReceive;
    hRmI2cCont->send = AP20RmI2cSend;
    hRmI2cCont->repeatStart = AP20RmI2cRepeatStartTransaction;
    hRmI2cCont->close = AP20RmI2cClose;
    hRmI2cCont->GetGpioPins =  AP20RmI2cGetGpioPins;
    hRmI2cCont->I2cRegisterOffset = I2C_I2C_CNFG_0;
    hRmI2cCont->ControllerId = hRmI2cCont->Instance;

    hRmI2cCont->hRmDma = NULL;
    hRmI2cCont->hRmMemory = NULL;
    hRmI2cCont->DmaBuffPhysAdd = 0;
    hRmI2cCont->pDmaBuffer = NULL;

    hRmI2cCont->pCpuBuffer = NULL;
    hRmI2cCont->pDataBuffer = NULL;
    hRmI2cCont->I2cSyncSemaphore = NULL;
    hRmI2cCont->I2CInterruptHandle = NULL;
    hRmI2cCont->TransCountFromLastDmaUsage = 0;

    TxFifoPhyAddress = hRmI2cCont->I2cRegisterOffset + I2C_I2C_TX_PACKET_FIFO_0;
    RxFifoPhyAddress = hRmI2cCont->I2cRegisterOffset + I2C_I2C_RX_FIFO_0;
    
    if (hRmI2cCont->ModuleId == NvRmModuleID_Dvc)
    {
        hRmI2cCont->I2cRegisterOffset = 0;
        hRmI2cCont->ControllerId = 3;
        RxFifoPhyAddress = DVC_I2C_RX_FIFO_0;
        TxFifoPhyAddress = DVC_I2C_TX_PACKET_FIFO_0;
    }    

    hRmI2cCont->IsApbDmaAllocated = NV_FALSE;
    hRmI2cCont->hRmDma = NULL;

    // Allocate the dma buffer 
    hRmI2cCont->DmaBufferSize = 0;
    Error = CreateDmaTransferBuffer(hRmI2cCont->hRmDevice, &hRmI2cCont->hRmMemory, 
            &hRmI2cCont->DmaBuffPhysAdd, (void **)&hRmI2cCont->pDmaBuffer,
            DEFAULT_I2C_DMA_BUFFER_SIZE);
    if (Error)
    {
        hRmI2cCont->hRmMemory = NULL;
        hRmI2cCont->DmaBuffPhysAdd = 0;
        hRmI2cCont->pDmaBuffer = NULL;
        Error = NvSuccess;
    }
    else
    {
        hRmI2cCont->DmaBufferSize = DEFAULT_I2C_DMA_BUFFER_SIZE;
        
        hRmI2cCont->RxDmaReq.SourceBufferPhyAddress= RxFifoPhyAddress + hRmI2cCont->ControllerAdd;
        hRmI2cCont->RxDmaReq.DestinationBufferPhyAddress = hRmI2cCont->DmaBuffPhysAdd;
        hRmI2cCont->RxDmaReq.SourceAddressWrapSize = 4;
        hRmI2cCont->RxDmaReq.DestinationAddressWrapSize = 0;
        
        hRmI2cCont->TxDmaReq.SourceBufferPhyAddress= hRmI2cCont->DmaBuffPhysAdd;
        hRmI2cCont->TxDmaReq.DestinationBufferPhyAddress = TxFifoPhyAddress + hRmI2cCont->ControllerAdd;
        hRmI2cCont->TxDmaReq.SourceAddressWrapSize = 0;
        hRmI2cCont->TxDmaReq.DestinationAddressWrapSize = 4;
    }

    if (!Error)
    {
        hRmI2cCont->pCpuBuffer = NvOsAlloc(DEFAULT_I2C_CPU_BUFFER_SIZE);
        if (!hRmI2cCont->pCpuBuffer)
            Error = NvError_InsufficientMemory;
    }            
    
    if (!Error)
        hRmI2cCont->pDataBuffer = hRmI2cCont->pCpuBuffer;
    
    // Create the sync semaphore for the interrupt synchrnoisation
    if (!Error)
        Error = NvOsSemaphoreCreate( &hRmI2cCont->I2cSyncSemaphore, 0);

#if !USE_POLLING_METHOD
    if (!Error)
    {
        IrqList = NvRmGetIrqForLogicalInterrupt(
                hRmI2cCont->hRmDevice, NVRM_MODULE_ID(hRmI2cCont->ModuleId, hRmI2cCont->Instance), 0);

        Error = NvRmInterruptRegister(hRmI2cCont->hRmDevice, 1, &IrqList, &IntHandlers, 
                hRmI2cCont, &hRmI2cCont->I2CInterruptHandle, NV_TRUE);
    }
#endif
    // Packet mode initialization
    hRmI2cCont->RsTransfer =  NV_FALSE;

    // If error then destroy all the allocation done here.
    if (Error)
        AP20RmI2cClose(hRmI2cCont);
    
    return Error;
}