cm-kernel /drivers/net/wireless/tiwlan1251/common/src/TNETW_Driver/TNETWIF/BusTxn/wspi.c

Language C Lines 1434
MD5 Hash cac3d746a186542a49eb549504ed99f4 Estimated Cost $19,275 (why?)
Repository git://github.com/CyanogenMod/cm-kernel.git View Raw File View Project SPDX
   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
/****************************************************************************
**+-----------------------------------------------------------------------+**
**|                                                                       |**
**| Copyright(c) 1998 - 2008 Texas Instruments. All rights reserved.      |**
**| 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 Texas Instruments 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  |**
**| OWNER 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.  |**
**|                                                                       |**
**+-----------------------------------------------------------------------+**
****************************************************************************/


/****************************************************************************
 *
 *   MODULE:  wspi.c
 *   PURPOSE: 
 *
 ****************************************************************************/


#include "osTIType.h"
#include "osApi.h"
#include "whalCommon.h"
#include "spi_api.h"
#include "wspi.h"

/* WSPI INIT CMD 
-----------------------------------------------------------------
| start |  tx   |                command                        |
-----------------------------------------------------------------
    47      46      45                                      40

-----------------------------------------------------------------
|                           reserve                             |
-----------------------------------------------------------------
    39                                                      32

-----------------------------------------------------------------
|                           reserve                             |
-----------------------------------------------------------------
    31                                                      24

-----------------------------------------------------------------
|           reserve              |  1250 |   fixed busy length  | 
-----------------------------------------------------------------
    23                                                      16
                                                                                    
-----------------------------------------------------------------
| fixed |       |       |       |       |       |       |       |
| busy  | iod   |   ip  |   cs  |   ws  |   bs  |  de   | wspi  |
| enable|       |       |       |       |       |       |       |
-----------------------------------------------------------------
    15      14      13      12      11      10      9       8

-----------------------------------------------------------------
|                         CRC7                            | end |
-----------------------------------------------------------------
    7                                               1        0
*/

#define WSPI_SIZEOF_UINT32          sizeof (UINT32)

#define WSPI_INIT_CMD_CRC_LEN       5

#define WSPI_INIT_CMD_START         0x00
#define WSPI_INIT_CMD_TX            0x40
#define WSPI_INIT_CMD_BYPASS_BIT    0x80 /* the extra bypass bit is sampled by the TNET as '1' */
#define WSPI_INIT_CMD_FIXEDBUSY_LEN 0x07
#define WSPI_INIT_CMD_EN_FIXEDBUSY  0x80
#define WSPI_INIT_CMD_DIS_FIXEDBUSY 0x00
#define WSPI_INIT_CMD_IOD           0x40
#define WSPI_INIT_CMD_IP            0x20
#define WSPI_INIT_CMD_CS            0x10
#define WSPI_INIT_CMD_WS            0x08
#define WSPI_INIT_CMD_WSPI          0x01
#define WSPI_INIT_CMD_END           0x01  
#define WSPI_INIT_CMD_CRC_INPUT_LEN 40

#define PRINT_TEST_REGION_SIZE      100
#define PRINT_TEST_REGION_START     0x305674 /* the device ID register */
#define PRINT_TEST_SIZE_REG         0x1FFC0
#define PRINT_TEST_ADDR_REG         0x1FFC4


/* WSPI CMD                                                                         
-----------------------------------------------------------------
| start |  tx   | fixed |         byte length                   |
-----------------------------------------------------------------
    31      30      29      28                              24

-----------------------------------------------------------------
|                           byte length                 |   a16 |
-----------------------------------------------------------------
    23                                              17      16

-----------------------------------------------------------------
|                           byte address                        |
-----------------------------------------------------------------
    15                                                      8

-----------------------------------------------------------------
|                           byte address                        | 
-----------------------------------------------------------------
    7                                                       0
*/


/* The 31's bit in the WSPI cmd is read bit */
#define WSPI_CMD_READ                 0x40000000 
#define WSPI_CMD_WRITE                0x00000000
#define WSPI_CMD_FIXED                0x20000000
#define WSPI_CMD_BYTE_LENGTH          0x1FFE0000 
#define WSPI_CMD_BYTE_LENGTH_OFFSET   17
#define WSPI_CMD_BYTE_ADDR            0x0001FFFF 


#define WSPI_FIXED_BUSY_TIMEOUT       100
#define WSPI_SYNC_OVER_ASYNC_TIMEOUT  100


/* For FixedBusy Handle */
#define NOT_IN_USE					  0

static UINT8 WSPI_GenerateCRC7 (UINT8* bits, UINT32 len);
#ifndef USE_WRITE_READ_API
static void  WSPI_WriteCmdCb      (TI_HANDLE hWSPI, int status);
#endif
static void  WSPI_ReadDataCb      (TI_HANDLE hWSPI, int status);
#ifdef USE_SYNC_API
#ifdef USE_SYNC_OVER_ASYNC
static void  WSPI_SyncOverAsyncCb (TI_HANDLE hWSPI, int status);
#endif
#endif 
static void  WSPI_ReadAsyncCb      (TI_HANDLE hWSPI, int status);
static void  WSPI_ConfigureResetCb (TI_HANDLE hWSPI, int status);

#ifdef TI_DBG
	int DebugFixedBusy[10];
#endif


/*
 * ----------------------------------------------------------------------------
 * Function :   WSPI_Open
 *
 * Input    :   none
 *
 * Output   :   void**  phWSPI - a pointer to the handle of the WSPI
 *
 * ReturnVal:   one of the error codes (zero is success)
 *
 * Note(s)  :   allocates the WSPI driver context and creates the SPI sublayer
 * -----------------------------------------------------------------------------
 */
TI_HANDLE WSPI_Open (TI_HANDLE hOs)
{   
    WSPI_t* pWSPI;

    /* Allocate WSPI memory */
    pWSPI = (WSPI_t *) os_memoryAlloc (hOs, sizeof(WSPI_t));
    if (pWSPI == NULL)
    {
        return NULL;
    }
   
    pWSPI->hOs = hOs;
    pWSPI->pTempBuf = 0;
	pWSPI->pExtraFixedBusyBuf = 0;
        
    /* Call to SPI_Open */
    pWSPI->hSPI = SPI_Open ();   

    return (TI_HANDLE)pWSPI;
}
    

/*
 * ----------------------------------------------------------------------------
 * Function :   WSPI_Close
 *
 * Input    :   void* hWSPI - the WSPI handle
 *
 * Output   :   none
 *
 * ReturnVal:   one of the error codes (zero is success)
 *
 * Note(s)  :   frees the WSPI driver context and closes the SPI sublayer
 * -----------------------------------------------------------------------------
 */
int WSPI_Close (TI_HANDLE hWSPI)
{
    WSPI_t* pWSPI = (WSPI_t*)hWSPI;

    /* Call SPI_Close */
    SPI_Close (pWSPI->hSPI);

    /* Free temporary buffer */
    if (pWSPI->pTempBuf)
    {
        os_memoryFree (pWSPI->hOs, pWSPI->pTempBuf, WSPI_NO_EXTRA_ALLOC_SIZE + pWSPI->uFixedBusyBytes);
    }


	if (pWSPI->pExtraFixedBusyBuf)
    {
        os_memoryFree (pWSPI->hOs, pWSPI->pExtraFixedBusyBuf, WSPI_EXTRA_BUFFER_ALLOC_SIZE);
    }

    /* Free allocated memory */
    os_memoryFree (pWSPI->hOs, hWSPI, sizeof(WSPI_t));

    return WSPI_OK;
}


/*
 * ----------------------------------------------------------------------------
 * Function :   WSPI_Configure
 *
 * Input    :   void* hWSPI - the WSPI handle
 *              const WSPIConfig_t* aConfig -  a structure that holds the configuration data 
                WSPI_CB_T* cb - callback 
 *
 * Output   :   
 *
 * ReturnVal:   one of the error codes (zero is success)
 *
 * Note(s)  :   1. configures the SPI sublayer
 *              2. sends the WSPI init word
 * -----------------------------------------------------------------------------
 */
int WSPI_Configure (TI_HANDLE hWSPI, TI_HANDLE hReport, const WSPIConfig_t* aConfig, WSPI_CB_T* cb)
{
    WSPI_t *pWSPI = (WSPI_t*)hWSPI;
    int i;
    
	#ifdef TI_DBG
		for (i=0;i<10;i++) 
		{
			DebugFixedBusy[i] = 0;
		}
	#endif
    
    /* 
     * This function is called during initialization and recovery. In recovery,
     * The old values are used, so there's no need to re-configure everything again.
     */
    if (NULL != aConfig)
    {
    /* Set WSPI_t parameters */
    pWSPI->uConfigMask = aConfig->mask;
    pWSPI->bFixedAddr = aConfig->isFixedAddress; 
    pWSPI->uFixedBusyLen = aConfig->fixedBusyLength;   
    pWSPI->uFixedBusyBytes = pWSPI->uFixedBusyLen * 4 + 4;
    /* Will be set to TRUE only when no extra bytes were allocated */
    pWSPI->bUseTempBuf = FALSE; 
    pWSPI->fErr = 0;  
    pWSPI->hReport = hReport; 
    /* Save CB for the WSPI_ConfigureResetCb() */
    pWSPI->fCb = cb->CBFunc;
    pWSPI->pCb = cb->CBArg;

    /* Allocate the temporary buffer that will hold buffers with no extra room */
    if (pWSPI->uFixedBusyLen != 0)  
    {
        if ((pWSPI->pTempBuf = os_memoryAlloc (pWSPI->hOs, WSPI_NO_EXTRA_ALLOC_SIZE + pWSPI->uFixedBusyBytes)) == NULL)
        {
            return WSPI_ERR_ALLOC_MEM;
        }
    }
    }
    
	/* Allocate the temporary buffer that will hold buffers with no extra room */
	if ((pWSPI->pExtraFixedBusyBuf = os_memoryAlloc (pWSPI->hOs, WSPI_EXTRA_BUFFER_ALLOC_SIZE)) == NULL)
    {
        return WSPI_ERR_ALLOC_MEM;
    }
	pWSPI->ExtraBufLength = 0;

    /* This CMD is used to reset the HW SM */
    for (i = 0; i < WSPI_INIT_CMD_LEN; i++)
    {
        pWSPI->auInitCmd[i] = 0xff;
    }
    
    /* Write the reset CMD */
    pWSPI->returnStatus =  SPI_Write (pWSPI->hSPI, pWSPI->auInitCmd, WSPI_INIT_CMD_LEN, (request_callback_t)WSPI_ConfigureResetCb, hWSPI, 1);
    if (pWSPI->returnStatus == SPI_TXN_COMPLETE)
    {
		/* Note that in the next function call, pWSPI->returnStatus is going to be changed */
        WSPI_ConfigureResetCb(hWSPI, OK);
    }

    return (pWSPI->returnStatus == SPI_TXN_COMPLETE ? WSPI_TXN_COMPLETE : 
			(pWSPI->returnStatus == SPI_TXN_PENDING ? WSPI_TXN_PENDING : WSPI_ERR_UNKNOWN));
}

static void WSPI_ConfigureResetCb (TI_HANDLE hWSPI, int status)
{ 
    WSPI_t *pWSPI = (WSPI_t*)hWSPI;
    UINT8   auCRCBuffer [WSPI_INIT_CMD_CRC_LEN];

    /*
     * Set WSPI_INIT_COMMAND       
     * the data is being send from the MSB to LSB
     */
    pWSPI->auInitCmd[2] = 0xff;
    pWSPI->auInitCmd[3] = 0xff;
    pWSPI->auInitCmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
    pWSPI->auInitCmd[0] = 0;
    pWSPI->auInitCmd[7] = 0;
    pWSPI->auInitCmd[6] = 
        (pWSPI->uConfigMask << 3) | (pWSPI->uFixedBusyLen & WSPI_INIT_CMD_FIXEDBUSY_LEN);
    pWSPI->auInitCmd[5] = 
        ((pWSPI->uFixedBusyLen == 0) ? 
            WSPI_INIT_CMD_DIS_FIXEDBUSY : 
            WSPI_INIT_CMD_EN_FIXEDBUSY) 
        | WSPI_INIT_CMD_IOD 
        | WSPI_INIT_CMD_IP 
        | WSPI_INIT_CMD_CS 
       #ifndef SPI_16_BIT
        | WSPI_INIT_CMD_WS
       #endif
        | WSPI_INIT_CMD_WSPI;

    auCRCBuffer[0] = pWSPI->auInitCmd[1];
    auCRCBuffer[1] = pWSPI->auInitCmd[0];
    auCRCBuffer[2] = pWSPI->auInitCmd[7];
    auCRCBuffer[3] = pWSPI->auInitCmd[6];
    auCRCBuffer[4] = pWSPI->auInitCmd[5]; 

    pWSPI->auInitCmd[4] = 
        (WSPI_GenerateCRC7 (auCRCBuffer, WSPI_INIT_CMD_CRC_INPUT_LEN) << 1) 
        | WSPI_INIT_CMD_END;   
   
    pWSPI->returnStatus = SPI_Write (pWSPI->hSPI, pWSPI->auInitCmd, WSPI_INIT_CMD_LEN, pWSPI->fCb, pWSPI->pCb, 1);
}


/*
 * ----------------------------------------------------------------------------
 * Function :   WSPI_ReadSync
 *
 * Input    :   void* hWSPI    - the WSPI handle
 *              UINT32 address - the address (in bytes) in the firmware to read the data from
 *              UINT32 length  - the number of bytes to read
 *              WSPI_CB_T cb   - callback parameters
 *              BOOL bMore     - more read/write transaction will follow   
 *
 * Output   :   UINT8* data - the buffer to put the read data
 *
 * ReturnVal:   one of the error codes (zero is success)
 *
 * Note(s)  :   1. set the WSPI cammand+address word
 *              2. send the WSPIcammand+address word
 *              3. read the fixed busy
 *              4. read the data
 *              5. check that the fixed busy was OK
 *
 *              because of the TNET state machine the data is always sent and only after 
 *              we read it we validate that it's OK
 * -----------------------------------------------------------------------------
 */
int WSPI_ReadSync (TI_HANDLE hWSPI, UINT32 address, UINT8* data, UINT32 length)
{
  #ifdef USE_SYNC_OVER_ASYNC
    
    WSPI_t    *pWSPI = (WSPI_t*)hWSPI;
    WSPI_CB_T  cb = { (request_callback_t)WSPI_SyncOverAsyncCb, hWSPI };
    UINT32        i, timeout;

    if (length == 0) 
    {
        return WSPI_ERR_WRONG_LENGTH;
    }
      
    /* Set the sync flag */
    pWSPI->bSyncFlag = 0;
        
    WSPI_ReadAsync (hWSPI, address, data, length, &cb, 1, 0);

    /* Wait to end of the asynchronous read */
    timeout = WSPI_SYNC_OVER_ASYNC_TIMEOUT * length;
    for (i = 0; i < timeout; i++)
    {
        if (pWSPI->bSyncFlag)
        {
            break;
        }
    }
    if (i == timeout)
    {
        /* Reached the timeout criteria without ending the asynchronous read */
        WLAN_REPORT_ERROR (pWSPI->hReport, HAL_HW_CTRL_MODULE_LOG, ("WSPI ASYNC READ DIDN'T FINISH\n")); 
                       
        return  WSPI_ERR_ASYNC_TIMEOUT;
    }

    return WSPI_OK;

  #else /* USE_SYNC_OVER_ASYNC */

    WSPI_t* pWSPI = (WSPI_t*)hWSPI;
    UINT32     i;
    UINT32  uFixedBusy;
    int     ret = WSPI_OK;

    if (length == 0) 
    {
        return WSPI_ERR_WRONG_LENGTH;
    }
         
    /*****************************/
    /* Write the command+address */
    /*****************************/
    pWSPI->uCmd = WSPI_CMD_READ;

    /* Set bFixedAddr */
    if (pWSPI->bFixedAddr)
    {
        pWSPI->uCmd |= WSPI_CMD_FIXED;   
    }
    
    /* Set length */
    pWSPI->uCmd |= (length << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
    /* Set address */
    pWSPI->uCmd |= address & WSPI_CMD_BYTE_ADDR;        
    
    /* Write command+address */
    ret |= SPI_WriteSync (pWSPI->hSPI, (UINT8*)&pWSPI->uCmd, WSPI_SIZEOF_UINT32);   

    if (pWSPI->uFixedBusyLen == 0)
    {
        /* 1150 */
        /* For 1150 read until we get the not-busy word */
        for (i = 0; i < WSPI_FIXED_BUSY_TIMEOUT; i++)  
        {
            ret |= SPI_ReadSync (pWSPI->hSPI, (UINT8*)&uFixedBusy, WSPI_SIZEOF_UINT32); 
            if ((uFixedBusy & 0x1) != 0)
            {
                break;
            }
        }
        
        if (i == WSPI_FIXED_BUSY_TIMEOUT)
        {
            WLAN_REPORT_ERROR (pWSPI->hReport, HAL_HW_CTRL_MODULE_LOG, ("WSPI FIXED BUSY (SYNC)\n")); 
                               
            return WSPI_ERR_BUS_BUSY;
        }
    }
    else
    {
        /* 1251 */
        /* For 1251 read the predefined number of busy words and at the end the not-busy word */  
        for (i = 0; i < pWSPI->uFixedBusyLen; i++) 
            ret |= SPI_ReadSync (pWSPI->hSPI, (UINT8*)&uFixedBusy, WSPI_SIZEOF_UINT32);
        ret |= SPI_ReadSync (pWSPI->hSPI, (UINT8*)&uFixedBusy, WSPI_SIZEOF_UINT32);
    }

    /* Read data */
    ret |= SPI_ReadSync (pWSPI->hSPI, data, length);

    if (pWSPI->uFixedBusyLen)
    { 
        /* 1251 */
        /* Check the fixed busy */
        if (!(uFixedBusy & 0x1))
        {
            WLAN_REPORT_ERROR (pWSPI->hReport, HAL_HW_CTRL_MODULE_LOG, ("WSPI FIXED BUSY (SYNC)\n")); 
                               
            ret = WSPI_ERR_BUS_BUSY;        
        }
    }

    return ret; 
  #endif
}


/*
 * ----------------------------------------------------------------------------
 * Function :   WSPI_ReadAsync
 *
 * Input    :   void* hWSPI    - the WSPI handle
 *              UINT32 address - the address (in bytes) in the firmware to read the data from
 *              UINT32 length  - the number of bytes to read
 *              WSPI_CB_T cb   - callback parameters
 *              BOOL bMore     - more read/write transaction will follow   
 *              BOOL bSpaceReserved 
 *                             - extra space padding has been reserved by an upper layer
 *
 * Output   :   UINT8* data    - the buffer to put the read data
 *
 * ReturnVal:   one of the error codes (zero is success)
 *
 * -----------------------------------------------------------------------------
 */
int WSPI_ReadAsyncOld (TI_HANDLE hWSPI, UINT32 address, UINT8* data, UINT32 length, WSPI_CB_T* cb, BOOL bMore, BOOL bSpaceReserved)
{
    WSPI_t       *pWSPI = (WSPI_t*)hWSPI;
    int           ret = WSPI_OK;

    if (length == 0) 
    {
        return WSPI_ERR_WRONG_LENGTH;
    }

    /*
     * Pass to the CB function of the read data also the fixed busy response so
     * to put all the required data in subsequent places in the buffer of the WSPI context 
     * first the fixed busy, second the CBFunc, third the CBArg
     * here we guarantee that the data will be sequential
     */  

    /*****************************/
    /* Write the command+address */
    /*****************************/
    pWSPI->uCmd = WSPI_CMD_READ;

    /* Set bFixedAddr */
    if (pWSPI->bFixedAddr)
    {
        pWSPI->uCmd |= WSPI_CMD_FIXED;   
    }
    
    /* Set the length */
    pWSPI->uCmd |= (length << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
    /* Set the address */
    pWSPI->uCmd |= address & WSPI_CMD_BYTE_ADDR;        
    
    /* Write command+address */
    ret |= SPI_WriteSync (pWSPI->hSPI, (UINT8*)&pWSPI->uCmd, WSPI_SIZEOF_UINT32);   

    if (pWSPI->uFixedBusyLen == 0)
    {
        UINT32 uFixedBusy, i;
        /* 1150 */
        /* For 1150 read until we get the not-busy word */
        for (i = 0; i < WSPI_FIXED_BUSY_TIMEOUT; i++)  
        {
            ret |= SPI_ReadSync (pWSPI->hSPI, (UINT8*)&uFixedBusy, WSPI_SIZEOF_UINT32);     
            if ((uFixedBusy & 0x1) != 0)
            {
                break;
            }
        }
        
        if (i == WSPI_FIXED_BUSY_TIMEOUT)
        {
            WLAN_REPORT_ERROR (pWSPI->hReport, HAL_HW_CTRL_MODULE_LOG, ("WSPI FIXED BUSY (ASYNC)\n")); 

            if (pWSPI->fErr) 
            { 
                (*pWSPI->fErr) (); 
            }

            return WSPI_ERR_BUS_BUSY;
        }
    }
    else
    {
        UINT32 uFixedBusy, i;
        /* 1251 */
        /* For 1251 read the predefined number of busy words and at the end the not-busy word */  
        for (i = 0; i < pWSPI->uFixedBusyLen; i++) 
            ret |= SPI_ReadSync (pWSPI->hSPI, (UINT8*)&uFixedBusy, WSPI_SIZEOF_UINT32);
        ret |= SPI_ReadSync (pWSPI->hSPI, (UINT8*)&pWSPI->uFixedBusy, WSPI_SIZEOF_UINT32);
    }  

    /* Read the data */
    pWSPI->fCb = cb->CBFunc;
    pWSPI->pCb = cb->CBArg;

    /* Tell the lower machine to start executing after this submission */  
    ret |= SPI_Read (pWSPI->hSPI, 
                     data + bSpaceReserved * pWSPI->uFixedBusyBytes, 
                     length, 
                     WSPI_ReadAsyncCb, 
                     pWSPI, 
                     1);
   
    return ret; 
}


/*
 * ----------------------------------------------------------------------------
 * Function :   WSPI_WriteSync
 *
 * Input    :   void* hWSPI - the WSPI handle
 *              UINT32 address - the address (in bytes) in the firmware to read the data from
 *              UINT8* data - the buffer that holds the data to write
 *              UINT32 length - the number of bytes to read
 *
 * Output   :   none
 *
 * ReturnVal:   one of the error codes (zero is success)
 *
 * Note(s)  :   1. set the WSPI cammand+address word
 *              2. send the WSPIcammand+address word
 *              3. send the data 
 *
 * -----------------------------------------------------------------------------
 */
int WSPI_WriteSync (TI_HANDLE hWSPI, UINT32 address, UINT8* data, UINT32 length)
{
  #ifdef USE_SYNC_OVER_ASYNC
    
    WSPI_t*   pWSPI = (WSPI_t*)hWSPI;
    WSPI_CB_T cb = { (request_callback_t)WSPI_SyncOverAsyncCb, hWSPI };
    int       i, timeout;

    if (length == 0) 
    {
        return WSPI_ERR_WRONG_LENGTH;
    }
        
    /* Set the sync flag */
    pWSPI->bSyncFlag = 0;
        
    WSPI_WriteAsync (hWSPI, address, data, length, &cb, 1, 0);

    /* Wait to end of the asynchronous write */
    timeout = WSPI_SYNC_OVER_ASYNC_TIMEOUT * length;
    for (i = 0; i < timeout; i++)
    {
        if (pWSPI->bSyncFlag)
        {
            break;
        }
    }
    if (i == timeout)
    {
        /* Reached the timeout criteria without ending the asynchronous write */
        WLAN_REPORT_ERROR (pWSPI->hReport, HAL_HW_CTRL_MODULE_LOG, ("WSPI ASYNC WRITE DIDN'T END\n")); 
                           
        return  WSPI_ERR_ASYNC_TIMEOUT;
    }

    return WSPI_OK;

  #else

    WSPI_t* pWSPI = (WSPI_t*)hWSPI;
    int     ret = WSPI_OK;
      
    if (length == 0) 
    {
        return WSPI_ERR_WRONG_LENGTH;
    }

    /*****************************/
    /* Write the command+address */
    /*****************************/
    pWSPI->uCmd = WSPI_CMD_WRITE;

    /* Set bFixedAddr */
    if (pWSPI->bFixedAddr)
    {
        pWSPI->uCmd |= WSPI_CMD_FIXED;
    }
        
    /* Set length */
    pWSPI->uCmd |= ((UINT32)length << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
    /* Set address */
    pWSPI->uCmd |= address & WSPI_CMD_BYTE_ADDR;        
    
    /* Write */
    ret |= SPI_WriteSync (pWSPI->hSPI, (UINT8*)&pWSPI->uCmd, WSPI_SIZEOF_UINT32);  

    /**************/
    /* Write data */
    /**************/
    ret |= SPI_WriteSync (pWSPI->hSPI, data, length);   
    
    return ret; 

  #endif
}


/*
 * ----------------------------------------------------------------------------
 * Function :   WSPI_ReadAsync
 *
 * Input    :   void* hWSPI    - the WSPI handle
 *              UINT32 address - the address (in bytes) in the firmware to read the data from
 *              UINT32 length  - the number of bytes to read
 *              WSPI_CB_T cb   - callback parameters
 *              BOOL bMore     - more read/write transaction will follow   
 *              BOOL bSpaceReserved 
 *                             - extra space padding has been reserved by an upper layer
 *
 * Output   :   UINT8* data    - the buffer to put the read data
 *
 * ReturnVal:   one of the error codes (zero is success)
 *
 * Note(s)  :   1. set the WSPI cammand+address word
 *              2. set the command+address request struct
 *              3. submit the request
 *              4. set the read fixed busy request struct
 *              5. submit the request
 *              6. set the read data request struct
 *              7. submit the request with execute flag
 *
 *              the fixed response is read into the buffer of the WSPI context
 * -----------------------------------------------------------------------------
 */
int WSPI_ReadAsync (TI_HANDLE hWSPI, UINT32 address, UINT8* data, UINT32 length, WSPI_CB_T* cb, BOOL bMore, BOOL bSpaceReserved)                   
{
    WSPI_t       *pWSPI = (WSPI_t*)hWSPI;
    int           status;
        
    if (length == 0) 
    {
        return WSPI_ERR_WRONG_LENGTH;
    }
    
    /* Save parameters */
    pWSPI->length = (UINT32)length; 
    pWSPI->data = data;
    pWSPI->bMore = bMore; 
    pWSPI->bSpaceReserved = bSpaceReserved;
    pWSPI->fCb = cb->CBFunc;
    pWSPI->pCb = cb->CBArg;

    /**********************************/
    /* Prepare the CMD for the SM use */
    /**********************************/
    pWSPI->uCmd = WSPI_CMD_READ;

    /* Set bFixedAddr */
    if (pWSPI->bFixedAddr)
    {
        pWSPI->uCmd |= WSPI_CMD_FIXED;   
    }
    
    /* Set length */
    pWSPI->uCmd |= ((UINT32)length << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
    /* Set address */
    pWSPI->uCmd |= address & WSPI_CMD_BYTE_ADDR;        
    
  #ifdef USE_WRITE_READ_API

    if (bSpaceReserved)
    {
        /* Indicate that that the temporary buffer is NOT used */
        pWSPI->bUseTempBuf = 0;
    }

    else
    {
        if (pWSPI->length <= WSPI_NO_EXTRA_ALLOC_SIZE)
        {   
            /* Indicate that that the temporary buffer is used */
            data = pWSPI->pTempBuf;
            pWSPI->bUseTempBuf = 1;
        }
        else
        {
            WLAN_REPORT_ERROR (pWSPI->hReport, HAL_HW_CTRL_MODULE_LOG, ("No space reserved for command\n"));
                               
            return WSPI_ERR_UNKNOWN;
        }
    }

    status = SPI_WriteRead (pWSPI->hSPI,
                            (UINT8*)&pWSPI->uCmd,
                            WSPI_SIZEOF_CMD,
                            data,
                            length + pWSPI->uFixedBusyBytes,
                            WSPI_ReadDataCb,
                            pWSPI,
                            bMore);

    /* In case of synchronous transaction completion call read callback explicitly */
    if (SPI_TXN_COMPLETE == status)
    {
        pWSPI->fCb = NULL;
        WSPI_ReadDataCb (hWSPI, status);
    }

    return status;

  #else

    /* Write command and indicate that more is ON */
    status = SPI_Write (pWSPI->hSPI, 
                        (UINT8*)&pWSPI->uCmd,
                        WSPI_SIZEOF_CMD,
                        WSPI_WriteCmdCb, 
                        hWSPI, 
                        1);  

    /* Don't pend, call directly to the callback */
    if (SPI_TXN_COMPLETE == status) 
    {
        WSPI_WriteCmdCb (hWSPI, WSPI_OK);
        /* WSPI_WriteCmdCb updates the pWSPI->status */
        return pWSPI->status;
    }

    return status;

  #endif
}


#ifndef USE_WRITE_READ_API
/*
 * ----------------------------------------------------------------------------
 * Function :   WSPI_WriteCmdCb
 *
 * Input    :   void* hWSPI - the WSPI handle
 *              int status  - SPI request status 
 *
 * Note(s)  :   CB from writing CMD in WSPI_ReadAsync().
 *              Read the data according to bSpaceReserved (indicates whether extra bytes were allocated
 *              in the buffer for the FixedBusyWord).
 *              if (!bSpaceReserved) but the length is short (WSPI_NO_EXTRA_ALLOC_SIZE) than copy it to 
 *              temporary file and read the data
 *              else - ERROR !
 * -----------------------------------------------------------------------------
 */
void WSPI_WriteCmdCb (TI_HANDLE hWSPI, int status)
{
    WSPI_t *pWSPI = (WSPI_t*)hWSPI;

    /* Extra room was saved - set the pointer as is */
    if (pWSPI->bSpaceReserved)
    {
        /* Indicate that that the temporary buffer is NOT used */
        pWSPI->bUseTempBuf = 0;

        /* Execute read with extra fixed busy bytes */  
        pWSPI->status = SPI_Read (pWSPI->hSPI, 
                                  pWSPI->data, 
                                  pWSPI->length + pWSPI->uFixedBusyBytes, 
                                  WSPI_ReadDataCb, 
                                  hWSPI,
                                  pWSPI->bMore); 
    }

    /* 
     * This case is used for handling buffers which have no extra room for the fixed busy words.
     * Use a temporary buffer and than copy the results            
     */
    else if (pWSPI->length <= WSPI_NO_EXTRA_ALLOC_SIZE)
    {   
        /* Indicate that that the temporary buffer is used */
        pWSPI->bUseTempBuf = 1;
        
        /* Read fixed busy words and a data in one transaction */ 
        pWSPI->status = SPI_Read (pWSPI->hSPI, 
                                  pWSPI->pTempBuf, 
                                  pWSPI->length + pWSPI->uFixedBusyBytes, 
                                  WSPI_ReadDataCb, 
                                  hWSPI,
                                  pWSPI->bMore); 
    }
    else    
    {
        WLAN_REPORT_ERROR (pWSPI->hReport, HAL_HW_CTRL_MODULE_LOG, ("No space reserved command\n"));                          
    }
    
    /* Check the return status; if not pending - call directly */
    if (SPI_TXN_COMPLETE == pWSPI->status)
    {   
        WSPI_ReadDataCb (hWSPI, WSPI_OK);
    }
}
#endif


/*
 * ----------------------------------------------------------------------------
 * Function :   WSPI_ReadDataCb
 *
 * Input    :   void* hWSPI - the WSPI handle
 *              int status  - SPI request status 
 *
 * Note(s)  :   CB from WSPI_WriteCmdCb().
 *              Copy the data if needed and call original CB
 * -----------------------------------------------------------------------------
 */
void WSPI_ReadDataCb (TI_HANDLE hWSPI, int status)
{
    WSPI_t *pWSPI = (WSPI_t*)hWSPI;
    UINT32 *pRbuf;

    /* If we are using the temp buffer and its fixedBusyWord is OK */
    pRbuf = (pWSPI->bUseTempBuf) ? (UINT32 *)pWSPI->pTempBuf 
                                 : (UINT32 *)pWSPI->data;

    /* No fixed busy */
    if ((pRbuf[pWSPI->uFixedBusyLen] & 0x1) != 0)
    {
        /* Copy data to the original buffer */
        if (pWSPI->bUseTempBuf)
        {
          #if WSPI_NO_EXTRA_ALLOC_SIZE == 4 /* used for optimization */
            /* In case the data size is 4 bytes copy it directly from the temp buffer */
            *((UINT32*)pWSPI->data) = pRbuf[pWSPI->uFixedBusyLen + 1];
          #else
            os_memoryCopy (pWSPI->hOs, pWSPI->data, &pRbuf[pWSPI->uFixedBusyLen + 1], pWSPI->length);            
          #endif 
        }

        /* Call user callback */
        if (pWSPI->fCb != NULL)
        {
            pWSPI->fCb (pWSPI->pCb, 0);
        } 
    }
    /* Handle fixed busy */
    else
    {
		WLAN_REPORT_WARNING (pWSPI->hReport, HAL_HW_CTRL_MODULE_LOG, 
			("WSPI FIXED BUSY (READ CALLBACK) Cmd = 0x%x, data pointer = %p data = 0x%x length = %d\n",
			pWSPI->uCmd, pRbuf, pRbuf[pWSPI->uFixedBusyLen + 1], pWSPI->length));

		WSPI_HandleFixedBusy(hWSPI, pRbuf);
    }       
}

/*
* ----------------------------------------------------------------------------
* Function :   WSPI_ReadDataAfterNotBusyCb
*
* Input    :   void* hWSPI - the WSPI handle
*              int status  - Not in use.
*
* Note(s)  :   Called from WSPI_HandleFixedBusy after the last read operation had the ~busy
*				word, but some of the data is missing
* -----------------------------------------------------------------------------
*/
void WSPI_ReadDataAfterNotBusyCb (TI_HANDLE hWSPI, int status)
{
	WSPI_t  *pWSPI = (WSPI_t*)hWSPI;
	UINT32 *pRbuf;
	UINT32 iExtraBufByteLength = pWSPI->ExtraBufLength*WSPI_SIZEOF_UINT32;
	UINT32 iTotalLength		   = pWSPI->length + pWSPI->uFixedBusyBytes;

	/* If we are using the temp buffer and its fixedBusyWord is OK */
	pRbuf = (pWSPI->bUseTempBuf) ? (UINT32 *)pWSPI->pTempBuf : (UINT32 *)pWSPI->data;

	if (pWSPI->bUseTempBuf)
	{
	   #if WSPI_NO_EXTRA_ALLOC_SIZE == 4 /* used for optimization */
		/* In case the data size is 4 bytes copy it directly from the temp buffer */
		*((UINT32*)pWSPI->data) = *((UINT32*)pWSPI->pExtraFixedBusyBuf);
	   #else
		os_memoryCopy (pWSPI->hOs, pWSPI->data, &(pRbuf[pWSPI->uFixedBusyLen + 1 + pWSPI->ExtraBufLength]), pWSPI->length - iExtraBufByteLength);            
		os_memoryCopy (pWSPI->hOs, &(pWSPI->data[pWSPI->length - iExtraBufByteLength]), pWSPI->pExtraFixedBusyBuf, iExtraBufByteLength);            
	   #endif 
	}
	else
	{
		os_memoryCopy(pWSPI->hOs, pRbuf,  &(pRbuf[pWSPI->ExtraBufLength]),iTotalLength - iExtraBufByteLength);
		pWSPI->data = (UINT8*)pRbuf;
		os_memoryCopy (pWSPI->hOs, &(pWSPI->data[iTotalLength - iExtraBufByteLength]), pWSPI->pExtraFixedBusyBuf, iExtraBufByteLength);            
	}

	/* Call user callback */
	if (pWSPI->fCb != NULL)
	{
		pWSPI->fCb (pWSPI->pCb, 0);
	}
}

/*
* ----------------------------------------------------------------------------
* Function :   WSPI_ReadNotBusyAndDataCb
*
* Input    :   void* hWSPI - the WSPI handle
*              int status  - Not in use.
*
* Note(s)  :   Called from WSPI_HandleFixedBusy after the last read operation had no response
*			    from the CHIP. i.e. the ~busy word is missing too
* -----------------------------------------------------------------------------
*/
void WSPI_ReadNotBusyAndDataCb (TI_HANDLE hWSPI, int status)
{
	WSPI_t  *pWSPI = (WSPI_t*)hWSPI;
	int i = 0;

	if ( pWSPI->bUseTempBuf )
	{
		while (i < (WSPI_EXTRA_READ_AFTER_NO_RESPONSE) )
		{
			/* Check if this is the ~busy word */
			if ( (*(UINT32*)(pWSPI->pExtraFixedBusyBuf + i)) & 0x1 )
			{	
				/* we have reached the fixed busy word */
			   #if WSPI_NO_EXTRA_ALLOC_SIZE == 4 /* used for optimization */
				/* In case the data size is 4 bytes copy it directly from the temp buffer */
				*((UINT32*)pWSPI->data) = *((UINT32*)(pWSPI->pExtraFixedBusyBuf+i+4));
			   #else
				os_memoryCopy (pWSPI->hOs, pWSPI->data, &(pWSPI->pTempBuf[pWSPI->uFixedBusyLen + 1 + pWSPI->ExtraBufLength]), pWSPI->length - iExtraBufByteLength);            
				os_memoryCopy (pWSPI->hOs, &(pWSPI->data[pWSPI->length - iExtraBufByteLength]), pWSPI->pExtraFixedBusyBuf, iExtraBufByteLength);            
			   #endif 

				if (pWSPI->fCb != NULL)
				{
					pWSPI->fCb (pWSPI->pCb, 0);
				}
				return;
			}
			i+=4;
		}
	}
	/* Oh boy, We couldn't find the ~Busy word */
	WLAN_REPORT_ERROR (pWSPI->hReport, HAL_HW_CTRL_MODULE_LOG, 
		("WSPI FIXED BUSY (READ CALLBACK) Cmd = 0x%x, length = %d\n",
		pWSPI->uCmd,pWSPI->length));

	if (pWSPI->fErr) 
	{
		(*pWSPI->fErr) ();
	}
	if (pWSPI->fCb != NULL)
	{
		pWSPI->fCb (pWSPI->pCb, WSPI_ERR_BUS_BUSY);
	}
}

/*
 * ----------------------------------------------------------------------------
 * Function :   WSPI_HandleFixedBusy
 *
 * Input    :   void* hWSPI - the WSPI handle
 *              UINT32 *pRbuf  - The temp read buffer 
 *
 * Note(s)  :   Called from WSPI_ReadDataCb() when a Fixed_Busy error accurse.
 *              Shifting the data to the expected place and reading the missing data
 * -----------------------------------------------------------------------------
 */
void WSPI_HandleFixedBusy(TI_HANDLE hWSPI, UINT32 *pRbuf)
{
	WSPI_t  *pWSPI = (WSPI_t*)hWSPI;
    UINT8   iTotalLength = pWSPI->length + pWSPI->uFixedBusyBytes;
	UINT8   iExtraBufByteLength = 0;
	int		status;

	pWSPI->ExtraBufLength = 0;

	/* Find the Data beginning */
	while (((pRbuf[pWSPI->uFixedBusyLen + pWSPI->ExtraBufLength] & 0x1) == 0) &&
		   ((pWSPI->uFixedBusyBytes + (pWSPI->ExtraBufLength * WSPI_SIZEOF_UINT32))  < iTotalLength) &&
		   ((pWSPI->ExtraBufLength * WSPI_SIZEOF_UINT32 ) < WSPI_EXTRA_BUFFER_ALLOC_SIZE )) 
	{
		pWSPI->ExtraBufLength++;
	}

	#ifdef TI_DBG
		if (pWSPI->ExtraBufLength > 0) 
		{
			DebugFixedBusy[pWSPI->ExtraBufLength - 1]++;
		}
	#endif
    
	/* if we had less then WSPI_EXTRA_BUFFER_ALLOC_SIZE FixedBusy words */
	if ((pRbuf[pWSPI->uFixedBusyLen + pWSPI->ExtraBufLength] & 0x1) != 0)
	{
		iExtraBufByteLength = pWSPI->ExtraBufLength*WSPI_SIZEOF_UINT32;

		/* Execute read with extra fixed busy bytes and without CMD*/  
		status = SPI_WriteRead(pWSPI->hSPI,
								NULL,
								0, 
								pWSPI->pExtraFixedBusyBuf, 
								iExtraBufByteLength, 
								WSPI_ReadDataAfterNotBusyCb,
								pWSPI,
								1);
        /* In case of synchronous transaction call the handling function implicitly */
		if (SPI_TXN_COMPLETE == status)
		{
			WSPI_ReadDataAfterNotBusyCb(hWSPI, OK);
		}
	}
	/* In the next case we try to read again even though the SPI line doesn't indicate any response  */
	else if ( pWSPI->bUseTempBuf )
	{	/* This case is handled for TempBuf only */ 
		/* Execute read without CMD */  
		status = SPI_WriteRead(pWSPI->hSPI,
								NULL,
								0, 
								pWSPI->pExtraFixedBusyBuf, 
								WSPI_EXTRA_READ_AFTER_NO_RESPONSE, 
								WSPI_ReadNotBusyAndDataCb,
								pWSPI,
								1);
		/* In case of synchronous transaction call the handling function implicitly */
		if (SPI_TXN_COMPLETE == status)
		{
			WSPI_ReadNotBusyAndDataCb(hWSPI, OK);
		}
	}
	else	/* Recovery from this one can be done by adjusting the length */
	{
		WLAN_REPORT_ERROR (pWSPI->hReport, HAL_HW_CTRL_MODULE_LOG, 
			("WSPI FIXED BUSY (READ CALLBACK) Cmd = 0x%x, data pointer = %p data = 0x%x length = %d\n",
			pWSPI->uCmd, pRbuf, pRbuf[pWSPI->uFixedBusyLen + 1],pWSPI->length));

        if (pWSPI->fErr) 
        {
            (*pWSPI->fErr) ();
        }

        if (pWSPI->fCb != NULL)
        {
            pWSPI->fCb (pWSPI->pCb, WSPI_ERR_BUS_BUSY);
        }
    }       
}


/*
 * ----------------------------------------------------------------------------
 * Function :   WSPI_WriteAsync
 *
 * Input    :   void* hWSPI    - the WSPI handle
 *              UINT32 address - the address (in bytes) in the firmware to read the data from
 *              UINT8* data    - the buffer that holds the data to write
 *              UINT32 length  - the number of bytes to read
 *              WSPI_CB_T cb   - callback parameters
 *              BOOL bMore     - more read/write transaction will follow   
 *              BOOL bSpaceReserved 
                               - extra space padding has been reserved by an upper layer
 *
 * Output   :   none
 *
 * ReturnVal:   WSPI_Status_e
 *
 * Note(s)  :   1. set the WSPI cammand+address word
 *              2. set the command+address request struct
 *              3. submit the request
 *              4. set the write data request struct
 *              5. submit the request with execute flag
 *
 * -----------------------------------------------------------------------------
 */
int WSPI_WriteAsyncOld (TI_HANDLE hWSPI, UINT32 address, UINT8* data, UINT32 length, WSPI_CB_T* cb, BOOL bMore, BOOL bSpaceReserved)
{
    WSPI_t       *pWSPI = (WSPI_t*) hWSPI;
    int           ret = WSPI_OK;

    if (length == 0) 
    {
        return WSPI_ERR_WRONG_LENGTH;
    }
        
    /*****************************/
    /* Write the command+address */
    /*****************************/
    pWSPI->uCmd = WSPI_CMD_WRITE;

    /* Set bFixedAddr */
    if (pWSPI->bFixedAddr)
    {
        pWSPI->uCmd |= WSPI_CMD_FIXED;
    } 
        
    /* Set the length */
    pWSPI->uCmd |= ((UINT32)length << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
    /* Set the address */
    pWSPI->uCmd |= address & WSPI_CMD_BYTE_ADDR;        
    
    /* Write */
    ret |= SPI_WriteSync (pWSPI->hSPI, (UINT8*)&pWSPI->uCmd, WSPI_SIZEOF_UINT32);  

    /******************/
    /* Write the data */
    /******************/

    /* Tell the lower machine to start executing after this submission */   
    ret |= SPI_Write (pWSPI->hSPI, 
                      data + bSpaceReserved * WSPI_SIZEOF_CMD, 
                      length, 
                      cb->CBFunc, 
                      cb->CBArg, 
                      1); 

    return ret; 
}


/*
 * ----------------------------------------------------------------------------
 * Function :   WSPI_WriteAsync
 *
 * Input    :   void* hWSPI    - the WSPI handle
 *              UINT32 address - the address (in bytes) in the firmware to read the data from
 *              UINT8* data    - the buffer that holds the data to write
 *              UINT32 length  - the number of bytes to read
 *              WSPI_CB_T cb   - callback parameters
 *              BOOL bMore     - more read/write transaction will follow   
 *              BOOL bSpaceReserved 
                               - extra space padding has been reserved by an upper layer
 *
 * Output   :   none
 *
 * ReturnVal:   WSPI_Status_e
 *
 * Note(s)  :   3 options are checked in this function:
 *              1) (bSpaceReserved == TRUE)
 *                  write the command and the data in one chunk
 *              2) else (length <= WSPI_NO_EXTRA_ALLOC_SIZE)
 *                  copy Command + data to tempBuffer and write it in one chunk
 *              3) else - error !!!
 *
 * -----------------------------------------------------------------------------
 */
int WSPI_WriteAsync (TI_HANDLE hWSPI, UINT32 address, UINT8* data, UINT32 length, WSPI_CB_T* cb, BOOL bMore, BOOL bSpaceReserved)                    
{
    WSPI_t       *pWSPI = (WSPI_t*) hWSPI;
    int           status;

    if (length == 0) 
    {
        return WSPI_ERR_WRONG_LENGTH;
    }

    /*****************************/
    /* Write the command+address */
    /*****************************/
    pWSPI->uCmd = WSPI_CMD_WRITE;

    /* Set bFixedAddr */
    if (pWSPI->bFixedAddr)
    {
        pWSPI->uCmd |= WSPI_CMD_FIXED;
    }
        
    /* Set the length */
    pWSPI->uCmd |= (length << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
    /* Set the address */
    pWSPI->uCmd |= address & WSPI_CMD_BYTE_ADDR;        

    if (bSpaceReserved)
    {
        /* If extra place was saved, transfer command and data in one transaction */
        *(UINT32*)data = pWSPI->uCmd;
        
         status = SPI_Write (pWSPI->hSPI, 
                             data, 
                             length + WSPI_SIZEOF_CMD, 
                             cb->CBFunc, 
                             cb->CBArg, 
                             bMore); 
    }

    /* If extra room was not saved for the command use temporary buffer */       
    else if (length <= WSPI_NO_EXTRA_ALLOC_SIZE)
    {
      #if WSPI_NO_EXTRA_ALLOC_SIZE == 4 /* used for optimization */
        /* Copy data to the temporary buffer */
        *((UINT32*)(pWSPI->pTempBuf + WSPI_SIZEOF_CMD)) = *((UINT32*)data);
      #else
        os_memoryCopy (pWSPI->hOs, pWSPI->pTempBuf + WSPI_SIZEOF_CMD, data, length);            
      #endif 
    
        /* Put the command at the beginning of the temporary buffer */
        *((UINT32*)pWSPI->pTempBuf) = pWSPI->uCmd;

        status = SPI_Write (pWSPI->hSPI, 
                            pWSPI->pTempBuf, 
                            length + WSPI_SIZEOF_CMD, 
                            cb->CBFunc, 
                            cb->CBArg, 
                            bMore); 
    }
    else 
    {   
        WLAN_REPORT_ERROR (pWSPI->hReport, HAL_HW_CTRL_MODULE_LOG, ("No space reserved for command\n"));
                           
        return WSPI_ERR_UNKNOWN;
    }

    return status; 
}


/*
 * ----------------------------------------------------------------------------
 * Function :   WSPI_GenerateCRC7
 *
 * Input    :   UINT8* bits - a string that holds the bits, in the order from the first to last
 *              UINT32 len - the number of bits
 *
 * Output   :   none
 *
 * ReturnVal:   UINT8 - the CRC word
 *
 * Note(s)  :   calculates the CRC7 for the WSPI init command
 * -----------------------------------------------------------------------------
 */
static UINT8 WSPI_GenerateCRC7 (UINT8* bits, UINT32 len)
{
    UINT8 CRC, CRC0, CRC1, CRC2, CRC3, CRC4, CRC5, CRC6;
    UINT8 bit, temp;
    UINT32   i;

    CRC0 = CRC1 = CRC2 = CRC3 = CRC4 = CRC5 = CRC6 = 0;

    /* Calculate the CRC with the formula : G(x) = x^7 + x^3 + 1 */
    for (i = 0; i < len; i++)
    {
        bit = (bits[i / 8] & (1 << (7 - (i % 8)))) >> (7 - (i % 8));
        temp = CRC6;        
        CRC6 = CRC5;
        CRC5 = CRC4;
        CRC4 = CRC3;
        CRC3 = CRC2;
        CRC2 = CRC1;
        CRC1 = CRC0;
        CRC0 = temp ^ bit;
        CRC3 = CRC0 ^ CRC3;
    }

    CRC = CRC0;
    CRC |= CRC1 << 1;
    CRC |= CRC2 << 2;
    CRC |= CRC3 << 3;
    CRC |= CRC4 << 4;
    CRC |= CRC5 << 5;
    CRC |= CRC6 << 6; 

    return CRC;
}


/*
 * ----------------------------------------------------------------------------
 * Function :   WSPI_ReadAsyncCb
 *
 * Input    :   void* handle - a handle that was passed to the asynchronous request (usually the WSPI handle)
 *              int status - the status of the SPI request
 *
 * Output   :   none
 *
 * ReturnVal:   none
 *
 * Note(s)  :   this is a temp function for passing as a CB function to the SPI layer for checking the async mechanism
 * -----------------------------------------------------------------------------
 */
static void WSPI_ReadAsyncCb (void* hWSPI, int status)
{
    WSPI_t *pWSPI = (WSPI_t*) hWSPI;
    /* Check the fixed busy word */
    if ((pWSPI->uFixedBusy & 0x1) != 0) 
    {
        pWSPI->fCb (pWSPI->pCb, 0);
    }
    else
    {
        WLAN_REPORT_ERROR (pWSPI->hReport, HAL_HW_CTRL_MODULE_LOG, ("WSPI FIXED BUSY (CALLBACK)\n"));

        if (pWSPI->fErr) 
        {
            (*pWSPI->fErr) ();
        }

        pWSPI->fCb (pWSPI->pCb, -1);
    }               
}


/*
 * ----------------------------------------------------------------------------
 * Function :   WSPI_SyncOverAsyncCb
 *
 * Input    :   void* handle - a handle that was passed to the asynchronous request
 *              int status - the status that the SPI request ended
 *
 * Output   :   none
 *
 * ReturnVal:   none
 *
 * Note(s)  :   this function just sets a flag so the sync SPI read/write function 
 *              can return
 * -----------------------------------------------------------------------------
 */
#ifdef USE_SYNC_OVER_ASYNC
static void WSPI_SyncOverAsyncCb (void* hWSPI, int status)
{
    WSPI_t* pWSPI = (WSPI_t*) hWSPI;

  #ifdef USE_SPI_DEBUG  
    {
        SPI_t *pSPI = (SPI_t*) pWSPI->hSPI;
        pSPI->stat.uNumOfSyncOverAsync ++;
    }
  #endif

    pWSPI->bSyncFlag = 1;
}
#endif


void WSPI_SetErrLog (void* hWSPI, void (*fErr)(void))
{
    WSPI_t* pWSPI = (WSPI_t*) hWSPI;

    pWSPI->fErr = fErr;
}
Back to Top