Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   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
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
/*
 * Copyright (c) 2013 Qualcomm Atheros, Inc.
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */


#include "opt_ah.h"

#include "ah.h"
#include "ah_internal.h"

#include "ar9300/ar9300.h"
#include "ar9300/ar9300reg.h"
#include "ar9300/ar9300phy.h"

#if ATH_SUPPORT_MCI

#define AH_MCI_REMOTE_RESET_INTERVAL_US     500
#define AH_MCI_DEBUG_PRINT_SCHED    0

static void ar9300_mci_print_msg(struct ath_hal *ah, HAL_BOOL send,u_int8_t hdr,
                                 int len, u_int32_t *pl)
{
#if 0
    char s[128];
    char *p = s;
    int i;
    u_int8_t *p_data = (u_int8_t *) pl;
    
    if (send) {
        p += snprintf(s, 60,
                      "(MCI) >>>>> Hdr: %02X, Len: %d, Payload:", hdr, len);
    }
    else {
        p += snprintf(s, 60,
                      "(MCI) <<<<< Hdr: %02X, Len: %d, Payload:", hdr, len);
    }
    for ( i=0; i<len; i++)
    {
        p += snprintf(p, 60, " %02x", *(p_data + i));
    }
    HALDEBUG(ah, HAL_DEBUG_BT_COEX, "%s\n", s);
/*
    for ( i=0; i<(len + 3)/4; i++)
    {
        HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI)   0x%08x\n", *(pl + i));
    }
*/
#endif
}

static
void ar9300_mci_osla_setup(struct ath_hal *ah, HAL_BOOL enable)
{
//    struct ath_hal_9300 *ahp = AH9300(ah);
    u_int32_t thresh;

    if (enable) {
        OS_REG_RMW_FIELD(ah, AR_MCI_SCHD_TABLE_2, AR_MCI_SCHD_TABLE_2_HW_BASED, 1);
        OS_REG_RMW_FIELD(ah, AR_MCI_SCHD_TABLE_2, AR_MCI_SCHD_TABLE_2_MEM_BASED, 1);

        if (!(ah->ah_config.ath_hal_mci_config &
            ATH_MCI_CONFIG_DISABLE_AGGR_THRESH))
        {

            if (AR_SREV_APHRODITE(ah))
                OS_REG_RMW_FIELD(ah, AR_MCI_MISC, AR_MCI_MISC_HW_FIX_EN, 1);

            thresh = MS(ah->ah_config.ath_hal_mci_config,
                        ATH_MCI_CONFIG_AGGR_THRESH);
            OS_REG_RMW_FIELD(ah, AR_BTCOEX_CTRL,
                             AR_BTCOEX_CTRL_AGGR_THRESH, thresh);
            OS_REG_RMW_FIELD(ah, AR_BTCOEX_CTRL,
                             AR_BTCOEX_CTRL_TIME_TO_NEXT_BT_THRESH_EN, 1);
            HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                "(MCI) SCHED aggr thresh: on, thresh=%d (%d.%d%%)\n",
                thresh, (thresh + 1)*125/10, (thresh + 1)*125%10);

        }
        else {
            OS_REG_RMW_FIELD(ah, AR_BTCOEX_CTRL,
                             AR_BTCOEX_CTRL_TIME_TO_NEXT_BT_THRESH_EN, 0);
            HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) SCHED aggr thresh: off\n");
        }
        OS_REG_RMW_FIELD(ah, AR_BTCOEX_CTRL,
                         AR_BTCOEX_CTRL_ONE_STEP_LOOK_AHEAD_EN, 1);
        HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) SCHED one step look ahead: on\n");
    }
    else {
        OS_REG_CLR_BIT(ah, AR_BTCOEX_CTRL, 
            AR_BTCOEX_CTRL_ONE_STEP_LOOK_AHEAD_EN);
        HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) SCHED one step look ahead: off\n");
    }
}

static void ar9300_mci_reset_req_wakeup(struct ath_hal *ah)
{
    /* to be tested in emulation */
    if (AR_SREV_JUPITER_20_OR_LATER(ah) || AR_SREV_APHRODITE(ah)) {
        OS_REG_RMW_FIELD(ah, AR_MCI_COMMAND2,
            AR_MCI_COMMAND2_RESET_REQ_WAKEUP, 1);
        OS_DELAY(1);
        OS_REG_RMW_FIELD(ah, AR_MCI_COMMAND2,
            AR_MCI_COMMAND2_RESET_REQ_WAKEUP, 0);
    }
}

static int32_t ar9300_mci_wait_for_interrupt(struct ath_hal *ah,
                                             u_int32_t address, 
                                             u_int32_t bit_position,
                                             int32_t time_out)
{
    int data; //, loop;

    while (time_out) {
        data = OS_REG_READ(ah, address);

        if (data & bit_position) {
            OS_REG_WRITE(ah, address, bit_position);
            if (address == AR_MCI_INTERRUPT_RX_MSG_RAW) {
                if (bit_position & AR_MCI_INTERRUPT_RX_MSG_REQ_WAKE) {
                    ar9300_mci_reset_req_wakeup(ah);
                }
                if (bit_position & (AR_MCI_INTERRUPT_RX_MSG_SYS_SLEEPING |
                                    AR_MCI_INTERRUPT_RX_MSG_SYS_WAKING))
                {
                    OS_REG_WRITE(ah, AR_MCI_INTERRUPT_RAW,
                        AR_MCI_INTERRUPT_REMOTE_SLEEP_UPDATE);
                }
                OS_REG_WRITE(ah, AR_MCI_INTERRUPT_RAW, AR_MCI_INTERRUPT_RX_MSG);
            }
            break;
        }

        OS_DELAY(10);
        time_out -= 10;
        if (time_out < 0) {
            break;
        }
    }

    if (time_out <= 0) {
        HALDEBUG(ah, HAL_DEBUG_BT_COEX,
            "(MCI) %s: Wait for Reg0x%08x = 0x%08x timeout.\n",
            __func__, address, bit_position);
        HALDEBUG(ah, HAL_DEBUG_BT_COEX,
            "(MCI) INT_RAW = 0x%08x, RX_MSG_RAW = 0x%08x\n",
            OS_REG_READ(ah, AR_MCI_INTERRUPT_RAW),
            OS_REG_READ(ah, AR_MCI_INTERRUPT_RX_MSG_RAW));
        time_out = 0;
    }
    return time_out;
}

void ar9300_mci_remote_reset(struct ath_hal *ah, HAL_BOOL wait_done)
{
    u_int32_t payload[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffff00};

    ar9300_mci_send_message(ah, MCI_REMOTE_RESET, 0, payload, 16, 
        wait_done, AH_FALSE);

    OS_DELAY(5);
}

void ar9300_mci_send_lna_transfer(struct ath_hal *ah, HAL_BOOL wait_done)
{
    u_int32_t payload = 0x00000000;

    ar9300_mci_send_message(ah, MCI_LNA_TRANS, 0, &payload, 1, 
        wait_done, AH_FALSE);
}

static void ar9300_mci_send_req_wake(struct ath_hal *ah, HAL_BOOL wait_done)
{
    ar9300_mci_send_message(ah, MCI_REQ_WAKE, 
        HAL_MCI_FLAG_DISABLE_TIMESTAMP, AH_NULL, 0, wait_done, AH_FALSE);

    OS_DELAY(5);
}

void ar9300_mci_send_sys_waking(struct ath_hal *ah, HAL_BOOL wait_done)
{
    ar9300_mci_send_message(ah, MCI_SYS_WAKING, 
        HAL_MCI_FLAG_DISABLE_TIMESTAMP, AH_NULL, 0, wait_done, AH_FALSE);
}

static void ar9300_mci_send_lna_take(struct ath_hal *ah, HAL_BOOL wait_done)
{
    u_int32_t payload = 0x70000000;

    /* LNA gain index is set to 7. */
    ar9300_mci_send_message(ah, MCI_LNA_TAKE, 
        HAL_MCI_FLAG_DISABLE_TIMESTAMP, &payload, 1, wait_done, AH_FALSE);
}

static void ar9300_mci_send_sys_sleeping(struct ath_hal *ah, HAL_BOOL wait_done)
{
    ar9300_mci_send_message(ah, MCI_SYS_SLEEPING, 
        HAL_MCI_FLAG_DISABLE_TIMESTAMP, AH_NULL, 0, wait_done, AH_FALSE);
}

static void
ar9300_mci_send_coex_version_query(struct ath_hal *ah, HAL_BOOL wait_done)
{
    struct ath_hal_9300 *ahp = AH9300(ah);
    u_int32_t payload[4] = {0, 0, 0, 0};

    if ((ahp->ah_mci_coex_bt_version_known == AH_FALSE) &&
        (ahp->ah_mci_bt_state != MCI_BT_SLEEP)) {
        HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) Send Coex version query.\n");
        MCI_GPM_SET_TYPE_OPCODE(payload,
            MCI_GPM_COEX_AGENT, MCI_GPM_COEX_VERSION_QUERY);
        ar9300_mci_send_message(ah, MCI_GPM, 0, payload, 16, wait_done, AH_TRUE);
    }
}

static void
ar9300_mci_send_coex_version_response(struct ath_hal *ah, HAL_BOOL wait_done)
{
    struct ath_hal_9300 *ahp = AH9300(ah);
    u_int32_t payload[4] = {0, 0, 0, 0};

    HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) Send Coex version response.\n");
    MCI_GPM_SET_TYPE_OPCODE(payload,
        MCI_GPM_COEX_AGENT, MCI_GPM_COEX_VERSION_RESPONSE);
    *(((u_int8_t *)payload) + MCI_GPM_COEX_B_MAJOR_VERSION) =
        ahp->ah_mci_coex_major_version_wlan;
    *(((u_int8_t *)payload) + MCI_GPM_COEX_B_MINOR_VERSION) =
        ahp->ah_mci_coex_minor_version_wlan;
    ar9300_mci_send_message(ah, MCI_GPM, 0, payload, 16, wait_done, AH_TRUE);
}

static void
ar9300_mci_send_coex_wlan_channels(struct ath_hal *ah, HAL_BOOL wait_done)
{
    struct ath_hal_9300 *ahp = AH9300(ah);
    u_int32_t *payload = &ahp->ah_mci_coex_wlan_channels[0];

    if ((ahp->ah_mci_coex_wlan_channels_update == AH_TRUE) &&
        (ahp->ah_mci_bt_state != MCI_BT_SLEEP))
    {
        MCI_GPM_SET_TYPE_OPCODE(payload,
            MCI_GPM_COEX_AGENT, MCI_GPM_COEX_WLAN_CHANNELS);
        ar9300_mci_send_message(ah, MCI_GPM, 0, payload, 16, wait_done, AH_TRUE);
        MCI_GPM_SET_TYPE_OPCODE(payload, 0xff, 0xff);
    }
}

static void ar9300_mci_send_coex_bt_status_query(struct ath_hal *ah,
                                    HAL_BOOL wait_done, u_int8_t query_type)
{
    struct ath_hal_9300 *ahp = AH9300(ah);
    u_int32_t pld[4] = {0, 0, 0, 0};
    HAL_BOOL query_btinfo = query_type &
            (MCI_GPM_COEX_QUERY_BT_ALL_INFO | MCI_GPM_COEX_QUERY_BT_TOPOLOGY);

    if (ahp->ah_mci_bt_state != MCI_BT_SLEEP) {
        HALDEBUG(ah, HAL_DEBUG_BT_COEX,
            "(MCI) Send Coex BT Status Query 0x%02X\n", query_type);
        MCI_GPM_SET_TYPE_OPCODE(pld,
            MCI_GPM_COEX_AGENT, MCI_GPM_COEX_STATUS_QUERY);
        *(((u_int8_t *)pld) + MCI_GPM_COEX_B_BT_BITMAP) = query_type;
        /*
         * If bt_status_query message is thought not sent successfully,
         * then ah_mci_need_flush_btinfo should be set again.
         */
        if (!ar9300_mci_send_message(ah, MCI_GPM, 0, pld, 16, wait_done, AH_TRUE))
        {
            if (query_btinfo) {
                ahp->ah_mci_need_flush_btinfo = AH_TRUE;
                HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                    "(MCI) send bt_status_query fail, set flush flag again\n");
            }
        }
        if (query_btinfo) {
            ahp->ah_mci_query_bt = AH_FALSE;
        }
    }
}

void ar9300_mci_send_coex_halt_bt_gpm(struct ath_hal *ah,
                                      HAL_BOOL halt, HAL_BOOL wait_done)
{
    struct ath_hal_9300 *ahp = AH9300(ah);
    u_int32_t payload[4] = {0, 0, 0, 0};

    HALDEBUG(ah, HAL_DEBUG_BT_COEX,
        "(MCI) Send Coex %s BT GPM.\n", (halt == AH_TRUE)?"HALT":"UNHALT");

    MCI_GPM_SET_TYPE_OPCODE(payload,
        MCI_GPM_COEX_AGENT, MCI_GPM_COEX_HALT_BT_GPM);
    if (halt == AH_TRUE) {
        ahp->ah_mci_query_bt = AH_TRUE;
        /* Send next UNHALT no matter HALT sent or not */
        ahp->ah_mci_unhalt_bt_gpm = AH_TRUE;
        ahp->ah_mci_need_flush_btinfo = AH_TRUE;
        *(((u_int8_t *)payload) + MCI_GPM_COEX_B_HALT_STATE) =
            MCI_GPM_COEX_BT_GPM_HALT;
    }
    else {
        *(((u_int8_t *)payload) + MCI_GPM_COEX_B_HALT_STATE) =
            MCI_GPM_COEX_BT_GPM_UNHALT;
    }
    ar9300_mci_send_message(ah, MCI_GPM, 0, payload, 16, wait_done, AH_TRUE);
}

static HAL_BOOL ar9300_mci_send_coex_bt_flags(struct ath_hal *ah, HAL_BOOL wait_done,
                                          u_int8_t opcode, u_int32_t bt_flags)
{
//    struct ath_hal_9300 *ahp = AH9300(ah);
    u_int32_t pld[4] = {0, 0, 0, 0};

    MCI_GPM_SET_TYPE_OPCODE(pld,
        MCI_GPM_COEX_AGENT, MCI_GPM_COEX_BT_UPDATE_FLAGS);

    *(((u_int8_t *)pld) + MCI_GPM_COEX_B_BT_FLAGS_OP)  = opcode;
    *(((u_int8_t *)pld) + MCI_GPM_COEX_W_BT_FLAGS + 0) = bt_flags & 0xFF;
    *(((u_int8_t *)pld) + MCI_GPM_COEX_W_BT_FLAGS + 1) =
        (bt_flags >> 8) & 0xFF;
    *(((u_int8_t *)pld) + MCI_GPM_COEX_W_BT_FLAGS + 2) =
        (bt_flags >> 16) & 0xFF;
    *(((u_int8_t *)pld) + MCI_GPM_COEX_W_BT_FLAGS + 3) =
        (bt_flags >> 24) & 0xFF;

    HALDEBUG(ah, HAL_DEBUG_BT_COEX,
        "(MCI) BT_MCI_FLAGS: Send Coex BT Update Flags %s 0x%08x\n",
            (opcode == MCI_GPM_COEX_BT_FLAGS_READ)?"READ":
            ((opcode == MCI_GPM_COEX_BT_FLAGS_SET)?"SET":"CLEAR"),
            bt_flags);

    return ar9300_mci_send_message(ah, MCI_GPM, 0, pld, 16, wait_done, AH_TRUE);
}

void ar9300_mci_2g5g_changed(struct ath_hal *ah, HAL_BOOL is_2g)
{
    struct ath_hal_9300 *ahp = AH9300(ah);

    if (ahp->ah_mci_coex_2g5g_update == AH_FALSE) {
        if (ahp->ah_mci_coex_is_2g == is_2g) {
            //HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) BT_MCI_FLAGS: not changed\n");
        } else {
            ahp->ah_mci_coex_2g5g_update = AH_TRUE;
            HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) BT_MCI_FLAGS: changed\n");
        }
    } else {
        HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) BT_MCI_FLAGS: force send\n");
    }
    ahp->ah_mci_coex_is_2g = is_2g;
}

static void ar9300_mci_send_2g5g_status(struct ath_hal *ah, HAL_BOOL wait_done)
{
    struct ath_hal_9300 *ahp = AH9300(ah);
    u_int32_t new_flags, to_set, to_clear;

    if ((AR_SREV_JUPITER_20_OR_LATER(ah) || AR_SREV_APHRODITE(ah)) &&
        (ahp->ah_mci_coex_2g5g_update == AH_TRUE) &&
        (ahp->ah_mci_bt_state != MCI_BT_SLEEP))
    {
        if (ahp->ah_mci_coex_is_2g) {
            new_flags = HAL_MCI_2G_FLAGS;
            to_clear = HAL_MCI_2G_FLAGS_CLEAR_MASK;
            to_set = HAL_MCI_2G_FLAGS_SET_MASK;
        } else {
            new_flags = HAL_MCI_5G_FLAGS;
            to_clear = HAL_MCI_5G_FLAGS_CLEAR_MASK;
            to_set = HAL_MCI_5G_FLAGS_SET_MASK;
        }
        HALDEBUG(ah, HAL_DEBUG_BT_COEX,
            "(MCI) BT_MCI_FLAGS: %s (0x%08x) clr=0x%08x, set=0x%08x\n",
            ahp->ah_mci_coex_is_2g?"2G":"5G", new_flags, to_clear, to_set);
        if (to_clear) {
            ar9300_mci_send_coex_bt_flags(ah, wait_done,
                MCI_GPM_COEX_BT_FLAGS_CLEAR, to_clear);
        }
        if (to_set) {
            ar9300_mci_send_coex_bt_flags(ah, wait_done,
                MCI_GPM_COEX_BT_FLAGS_SET, to_set);
        }
    }
    if (AR_SREV_JUPITER_10(ah) && (ahp->ah_mci_bt_state != MCI_BT_SLEEP)) {
        ahp->ah_mci_coex_2g5g_update = AH_FALSE;
    }
}

void ar9300_mci_2g5g_switch(struct ath_hal *ah, HAL_BOOL wait_done)
{
    struct ath_hal_9300 *ahp = AH9300(ah);

    if (ahp->ah_mci_coex_2g5g_update)
    {
        if (ahp->ah_mci_coex_is_2g) {
            ar9300_mci_send_2g5g_status(ah, AH_TRUE);

            HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) Send LNA trans\n");
            ar9300_mci_send_lna_transfer(ah, AH_TRUE);
            OS_DELAY(5);

            OS_REG_CLR_BIT(ah, AR_MCI_TX_CTRL,
                AR_MCI_TX_CTRL_DISABLE_LNA_UPDATE);
            if (AR_SREV_JUPITER_20_OR_LATER(ah) || AR_SREV_APHRODITE(ah)) {
                OS_REG_CLR_BIT(ah, AR_GLB_CONTROL,
                    AR_BTCOEX_CTRL_BT_OWN_SPDT_CTRL);
                if (!(ah->ah_config.ath_hal_mci_config &
                    ATH_MCI_CONFIG_DISABLE_OSLA))
                {
                    ar9300_mci_osla_setup(ah, AH_TRUE);
                }
            }
        } else {
            HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) Send LNA take\n");
            ar9300_mci_send_lna_take(ah, AH_TRUE);
            OS_DELAY(5);

            OS_REG_SET_BIT(ah, AR_MCI_TX_CTRL,
                AR_MCI_TX_CTRL_DISABLE_LNA_UPDATE);
            if (AR_SREV_JUPITER_20_OR_LATER(ah) || AR_SREV_APHRODITE(ah)) {
                OS_REG_SET_BIT(ah, AR_GLB_CONTROL,
                    AR_BTCOEX_CTRL_BT_OWN_SPDT_CTRL);
                ar9300_mci_osla_setup(ah, AH_FALSE);
            }

            ar9300_mci_send_2g5g_status(ah, AH_TRUE);
        }
    }

    /*
     * Update self gen chain mask. Also set basic set for
     * txbf.
     */
    if (AR_SREV_JUPITER(ah)) {
        if (ahp->ah_mci_coex_is_2g) {
            ahp->ah_reduced_self_gen_mask = AH_TRUE;
            OS_REG_WRITE(ah, AR_SELFGEN_MASK, 0x02);
            ar9300_txbf_set_basic_set(ah);
        }
        else {
            ahp->ah_reduced_self_gen_mask = AH_FALSE;
            ar9300_txbf_set_basic_set(ah);
        }
    }
}

void ar9300_mci_mute_bt(struct ath_hal *ah)
{

    HALDEBUG(ah, HAL_DEBUG_BT_COEX, "%s: called\n", __func__);

    /* disable all MCI messages */ 
    OS_REG_WRITE(ah, AR_MCI_MSG_ATTRIBUTES_TABLE, 0xFFFF0000);
    OS_REG_WRITE(ah, AR_BTCOEX_WL_WEIGHTS0, 0xFFFFFFFF);
    OS_REG_WRITE(ah, AR_BTCOEX_WL_WEIGHTS1, 0xFFFFFFFF);
    OS_REG_WRITE(ah, AR_BTCOEX_WL_WEIGHTS2, 0xFFFFFFFF);
    OS_REG_WRITE(ah, AR_BTCOEX_WL_WEIGHTS3, 0xFFFFFFFF);
    OS_REG_SET_BIT(ah, AR_MCI_TX_CTRL, AR_MCI_TX_CTRL_DISABLE_LNA_UPDATE);
    /* wait pending HW messages to flush out */
    OS_DELAY(10);

    /*
     * Send LNA_TAKE and SYS_SLEEPING when
     * 1. reset not after resuming from full sleep
     * 2. before reset MCI RX, to quiet BT and avoid MCI RX misalignment
     */
    if (MCI_ANT_ARCH_PA_LNA_SHARED(ah->ah_config.ath_hal_mci_config)) {
        HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) Send LNA take\n");
        ar9300_mci_send_lna_take(ah, AH_TRUE);
        OS_DELAY(5);
    }
    HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) Send sys sleeping\n");
    ar9300_mci_send_sys_sleeping(ah, AH_TRUE);
}

static void ar9300_mci_observation_set_up(struct ath_hal *ah)
{
    /*
     * Set up the observation bus in order to monitor MCI bus
     * through GPIOs (0, 1, 2, and 3).
     */
    /*
    OS_REG_WRITE(ah, AR_GPIO_INTR_POL, 0x00420000);
    OS_REG_WRITE(ah, AR_GPIO_OE_OUT, 0x000000ff); // 4050
    OS_REG_WRITE(ah, AR_GPIO_OUTPUT_MUX1, 0x000bdab4); // 4068
    OS_REG_WRITE(ah, AR_OBS, 0x0000004b); // 4088
    OS_REG_WRITE(ah, AR_DIAG_SW, 0x080c0000);
    OS_REG_WRITE(ah, AR_MACMISC, 0x0001a000);
    OS_REG_WRITE(ah, AR_PHY_TEST, 0x00080000); // a360
    OS_REG_WRITE(ah, AR_PHY_TEST_CTL_STATUS, 0xe0000000); // a364
    */
    HALDEBUG(ah, HAL_DEBUG_BT_COEX, "%s: called; config=0x%08x\n",
        __func__, ah->ah_config.ath_hal_mci_config);

    if (ah->ah_config.ath_hal_mci_config &
        ATH_MCI_CONFIG_MCI_OBS_MCI)
    {
        HALDEBUG(ah, HAL_DEBUG_BT_COEX, "%s: CONFIG_MCI_OBS_MCI\n", __func__);
        ar9300_gpio_cfg_output(ah, 3, HAL_GPIO_OUTPUT_MUX_AS_MCI_WLAN_DATA);
        ar9300_gpio_cfg_output(ah, 2, HAL_GPIO_OUTPUT_MUX_AS_MCI_WLAN_CLK);
        ar9300_gpio_cfg_output(ah, 1, HAL_GPIO_OUTPUT_MUX_AS_MCI_BT_DATA);
        ar9300_gpio_cfg_output(ah, 0, HAL_GPIO_OUTPUT_MUX_AS_MCI_BT_CLK);
    }
    else if (ah->ah_config.ath_hal_mci_config & 
        ATH_MCI_CONFIG_MCI_OBS_TXRX)
    {
        HALDEBUG(ah, HAL_DEBUG_BT_COEX, "%s: CONFIG_MCI_OBS_TXRX\n", __func__);
        ar9300_gpio_cfg_output(ah, 3, HAL_GPIO_OUTPUT_MUX_AS_WL_IN_TX);
        ar9300_gpio_cfg_output(ah, 2, HAL_GPIO_OUTPUT_MUX_AS_WL_IN_RX);
        ar9300_gpio_cfg_output(ah, 1, HAL_GPIO_OUTPUT_MUX_AS_BT_IN_TX);
        ar9300_gpio_cfg_output(ah, 0, HAL_GPIO_OUTPUT_MUX_AS_BT_IN_RX);
        ar9300_gpio_cfg_output(ah, 5, HAL_GPIO_OUTPUT_MUX_AS_OUTPUT);
    }
    else if (ah->ah_config.ath_hal_mci_config & 
        ATH_MCI_CONFIG_MCI_OBS_BT)
    {
        HALDEBUG(ah, HAL_DEBUG_BT_COEX, "%s: CONFIG_MCI_OBS_BT\n", __func__);
        ar9300_gpio_cfg_output(ah, 3, HAL_GPIO_OUTPUT_MUX_AS_BT_IN_TX);
        ar9300_gpio_cfg_output(ah, 2, HAL_GPIO_OUTPUT_MUX_AS_BT_IN_RX);
        ar9300_gpio_cfg_output(ah, 1, HAL_GPIO_OUTPUT_MUX_AS_MCI_BT_DATA);
        ar9300_gpio_cfg_output(ah, 0, HAL_GPIO_OUTPUT_MUX_AS_MCI_BT_CLK);
    }
    else {
        return;
    }

    OS_REG_SET_BIT(ah,
        AR_HOSTIF_REG(ah, AR_GPIO_INPUT_EN_VAL), AR_GPIO_JTAG_DISABLE);

    if (AR_SREV_JUPITER_20_OR_LATER(ah) || AR_SREV_APHRODITE(ah)) {
        OS_REG_RMW_FIELD(ah, AR_GLB_CONTROL, AR_GLB_DS_JTAG_DISABLE, 1);
        OS_REG_RMW_FIELD(ah, AR_GLB_CONTROL, AR_GLB_WLAN_UART_INTF_EN, 0);
        OS_REG_WRITE(ah, AR_GLB_GPIO_CONTROL, 
                     (OS_REG_READ(ah, AR_GLB_GPIO_CONTROL) | 
                      ATH_MCI_CONFIG_MCI_OBS_GPIO));
    }

    OS_REG_RMW_FIELD(ah, AR_BTCOEX_CTRL2, AR_BTCOEX_CTRL2_GPIO_OBS_SEL, 0);
    OS_REG_RMW_FIELD(ah, AR_BTCOEX_CTRL2, AR_BTCOEX_CTRL2_MAC_BB_OBS_SEL, 1);
    OS_REG_WRITE(ah, AR_HOSTIF_REG(ah, AR_OBS), 0x4b);
    OS_REG_RMW_FIELD(ah, AR_DIAG_SW, AR_DIAG_OBS_PT_SEL1, 0x03);
    OS_REG_RMW_FIELD(ah, AR_DIAG_SW, AR_DIAG_OBS_PT_SEL2, 0x01);    
    OS_REG_RMW_FIELD(ah, AR_MACMISC, AR_MACMISC_MISC_OBS_BUS_LSB, 0x02);
    OS_REG_RMW_FIELD(ah, AR_MACMISC, AR_MACMISC_MISC_OBS_BUS_MSB, 0x03);
    //OS_REG_RMW_FIELD(ah, AR_PHY_TEST, AR_PHY_TEST_BBB_OBS_SEL, 0x01);
    OS_REG_RMW_FIELD(ah, AR_PHY_TEST_CTL_STATUS, 
        AR_PHY_TEST_CTL_DEBUGPORT_SEL, 0x07);
}

static void ar9300_mci_process_gpm_extra(struct ath_hal *ah,
                    u_int8_t gpm_type, u_int8_t gpm_opcode, u_int32_t *p_gpm)
{
    struct ath_hal_9300 *ahp = AH9300(ah);
    u_int8_t *p_data = (u_int8_t *) p_gpm;

    switch (gpm_type)
    {
        case MCI_GPM_COEX_AGENT:
            switch (gpm_opcode)
            {
                case MCI_GPM_COEX_VERSION_QUERY:
                    HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                        "(MCI) Recv GPM COEX Version Query.\n");
                    ar9300_mci_send_coex_version_response(ah, AH_TRUE);
                    break;

                case MCI_GPM_COEX_VERSION_RESPONSE:
                    HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                        "(MCI) Recv GPM COEX Version Response.\n");
                    ahp->ah_mci_coex_major_version_bt =
                        *(p_data + MCI_GPM_COEX_B_MAJOR_VERSION);
                    ahp->ah_mci_coex_minor_version_bt =
                        *(p_data + MCI_GPM_COEX_B_MINOR_VERSION);
                    ahp->ah_mci_coex_bt_version_known = AH_TRUE;
                    HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                        "(MCI) BT Coex version: %d.%d\n",
                        ahp->ah_mci_coex_major_version_bt,
                        ahp->ah_mci_coex_minor_version_bt);
                    break;

                case MCI_GPM_COEX_STATUS_QUERY:
                    HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                        "(MCI) Recv GPM COEX Status Query = 0x%02X.\n",
                        *(p_data + MCI_GPM_COEX_B_WLAN_BITMAP));
                    //if ((*(p_data + MCI_GPM_COEX_B_WLAN_BITMAP)) &
                    //    MCI_GPM_COEX_QUERY_WLAN_ALL_INFO)
                    {
                        ahp->ah_mci_coex_wlan_channels_update = AH_TRUE;
                        ar9300_mci_send_coex_wlan_channels(ah, AH_TRUE);
                    }
                    break;

                case MCI_GPM_COEX_BT_PROFILE_INFO:
                    ahp->ah_mci_query_bt = AH_TRUE;
                    HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                        "(MCI) Recv GPM COEX BT_Profile_Info (drop&query)\n");
                    break;

                case MCI_GPM_COEX_BT_STATUS_UPDATE:
                    ahp->ah_mci_query_bt = AH_TRUE;
                    HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                        "(MCI) Recv GPM COEX BT_Status_Update "
                        "SEQ=%d (drop&query)\n",
                        *(p_gpm + 3));
                    break;

                default:
                    break;
            }
        default:
            break;
    }
}

u_int32_t ar9300_mci_wait_for_gpm(struct ath_hal *ah, u_int8_t gpm_type, 
                                  u_int8_t gpm_opcode, int32_t time_out)
{
    u_int32_t *p_gpm = NULL, mismatch = 0, more_data = HAL_MCI_GPM_NOMORE;
    struct ath_hal_9300 *ahp = AH9300(ah);
    HAL_BOOL b_is_bt_cal_done = (gpm_type == MCI_GPM_BT_CAL_DONE);
    u_int32_t offset;
    u_int8_t recv_type = 0, recv_opcode = 0;

    if (time_out == 0) {
        more_data = HAL_MCI_GPM_MORE;
    }

    while (time_out > 0)
    {
        if (p_gpm != NULL) {
            MCI_GPM_RECYCLE(p_gpm);
            p_gpm = NULL;
        }

        if (more_data != HAL_MCI_GPM_MORE) {
            time_out = ar9300_mci_wait_for_interrupt(ah, 
                AR_MCI_INTERRUPT_RX_MSG_RAW, 
                AR_MCI_INTERRUPT_RX_MSG_GPM,
                time_out);
        }

        if (time_out) {
            offset = ar9300_mci_state(ah,
                HAL_MCI_STATE_NEXT_GPM_OFFSET, &more_data);

            if (offset == HAL_MCI_GPM_INVALID) {
                continue;
            }
            p_gpm = (u_int32_t *) (ahp->ah_mci_gpm_buf + offset);
            ar9300_mci_print_msg(ah, AH_FALSE, MCI_GPM, 16, p_gpm);

            recv_type = MCI_GPM_TYPE(p_gpm);
            recv_opcode = MCI_GPM_OPCODE(p_gpm);

            if (MCI_GPM_IS_CAL_TYPE(recv_type)) {
                if (recv_type == gpm_type) {
                    if ((gpm_type == MCI_GPM_BT_CAL_DONE) && !b_is_bt_cal_done)
                    {
                        gpm_type = MCI_GPM_BT_CAL_GRANT;
                        HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                            "(MCI) Rcv BT_CAL_DONE. Now Wait BT_CAL_GRANT\n");
                        continue;
                    }
                    if (gpm_type == MCI_GPM_BT_CAL_GRANT) {
                        HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                            "(MCI) BT_CAL_GRANT seq=%d, req_count=%d\n",
                            *(p_gpm + 2), *(p_gpm + 3));
                    }
                    break;
                }
            }
            else {
                if ((recv_type == gpm_type) && (recv_opcode == gpm_opcode)) {
                    break;
                }
            }
            
            /* not expected message */
            
            /*
             * Check if it's cal_grant
             *
             * When we're waiting for cal_grant in reset routine, it's
             * possible that BT sends out cal_request at the same time.
             * Since BT's calibration doesn't happen that often, we'll
             * let BT completes calibration then we continue to wait 
             * for cal_grant from BT.
             * Orginal: Wait BT_CAL_GRANT.
             * New: Receive BT_CAL_REQ -> send WLAN_CAL_GRANT -> wait
             * BT_CAL_DONE -> Wait BT_CAL_GRANT.
             */
            if ((gpm_type == MCI_GPM_BT_CAL_GRANT) &&
                (recv_type == MCI_GPM_BT_CAL_REQ))
            {
                u_int32_t payload[4] = {0, 0, 0, 0};

                gpm_type = MCI_GPM_BT_CAL_DONE;
                HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                    "(MCI) Rcv BT_CAL_REQ. Send WLAN_CAL_GRANT.\n");

                MCI_GPM_SET_CAL_TYPE(payload, MCI_GPM_WLAN_CAL_GRANT);
                ar9300_mci_send_message(ah, MCI_GPM, 0, payload, 16, 
                    AH_FALSE, AH_FALSE);

                HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                    "(MCI) Now wait for BT_CAL_DONE.\n");
                continue;
            }
            else {
                HALDEBUG(ah, HAL_DEBUG_BT_COEX, 
                    "(MCI) GPM subtype not match 0x%x\n", *(p_gpm + 1));
                mismatch++;
                ar9300_mci_process_gpm_extra(ah, recv_type, recv_opcode, p_gpm);
            }
        }
    }
    if (p_gpm != NULL) {
        MCI_GPM_RECYCLE(p_gpm);
        p_gpm = NULL;
    }

    if (time_out <= 0) {
        time_out = 0;
        HALDEBUG(ah, HAL_DEBUG_BT_COEX,
            "(MCI) GPM receiving timeout, mismatch = %d\n", mismatch);
    } else {
        HALDEBUG(ah, HAL_DEBUG_BT_COEX,
            "(MCI) Receive GPM type=0x%x, code=0x%x\n", gpm_type, gpm_opcode);
    }

    while (more_data == HAL_MCI_GPM_MORE) {
        HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) discard remaining GPM\n");
        offset = ar9300_mci_state(ah,
            HAL_MCI_STATE_NEXT_GPM_OFFSET, &more_data);

        if (offset == HAL_MCI_GPM_INVALID) {
            break;
        }
        p_gpm = (u_int32_t *) (ahp->ah_mci_gpm_buf + offset);
        ar9300_mci_print_msg(ah, AH_FALSE, MCI_GPM, 16, p_gpm);
        recv_type = MCI_GPM_TYPE(p_gpm);
        recv_opcode = MCI_GPM_OPCODE(p_gpm);
        if (!MCI_GPM_IS_CAL_TYPE(recv_type)) {
            ar9300_mci_process_gpm_extra(ah, recv_type, recv_opcode, p_gpm);
        }
        MCI_GPM_RECYCLE(p_gpm);
    }

    return time_out;
}

static void ar9300_mci_prep_interface(struct ath_hal *ah)
{
    struct ath_hal_9300 *ahp = AH9300(ah);
    u_int32_t saved_mci_int_en;
    u_int32_t mci_timeout = 150;

    ahp->ah_mci_bt_state = MCI_BT_SLEEP;

    saved_mci_int_en = OS_REG_READ(ah, AR_MCI_INTERRUPT_EN);
    OS_REG_WRITE(ah, AR_MCI_INTERRUPT_EN, 0);

    OS_REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
        OS_REG_READ(ah, AR_MCI_INTERRUPT_RX_MSG_RAW));
    OS_REG_WRITE(ah, AR_MCI_INTERRUPT_RAW,
        OS_REG_READ(ah, AR_MCI_INTERRUPT_RAW));

    /* Remote Reset */
    HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) %s: Reset sequence start\n", __func__);
    HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) send REMOTE_RESET\n");
    ar9300_mci_remote_reset(ah, AH_TRUE);

    /*
     * This delay is required for the reset delay worst case value 255 in 
     * MCI_COMMAND2 register 
     */
    if (AR_SREV_JUPITER_10(ah)) {
        OS_DELAY(252);
    }

    /* Send REQ_WAKE to BT */
    HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) %s: Send REQ_WAKE to remote(BT)\n",
        __func__);

    ar9300_mci_send_req_wake(ah, AH_TRUE);

    if (ar9300_mci_wait_for_interrupt(ah, AR_MCI_INTERRUPT_RX_MSG_RAW, 
        AR_MCI_INTERRUPT_RX_MSG_SYS_WAKING, 500))
    {
        HALDEBUG(ah, HAL_DEBUG_BT_COEX, 
            "(MCI) %s: Saw SYS_WAKING from remote(BT)\n", __func__);
        ahp->ah_mci_bt_state = MCI_BT_AWAKE;

        if (AR_SREV_JUPITER_10(ah)) {
            OS_DELAY(10);
        }
        /*
         * We don't need to send more remote_reset at this moment.
         *
         * If BT receive first remote_reset, then BT HW will be cleaned up and
         * will be able to receive req_wake and BT HW will respond sys_waking.
         * In this case, WLAN will receive BT's HW sys_waking.
         *
         * Otherwise, if BT SW missed initial remote_reset, that remote_reset
         * will still clean up BT MCI RX, and the req_wake will wake BT up,
         * and BT SW will respond this req_wake with a remote_reset and
         * sys_waking. In this case, WLAN will receive BT's SW sys_waking.
         *
         * In either case, BT's RX is cleaned up. So we don't need to reply
         * BT's remote_reset now, if any.
         *
         * Similarly, if in any case, WLAN can receive BT's sys_waking, that
         * means WLAN's RX is also fine.
         */

        /* Send SYS_WAKING to BT */
        HALDEBUG(ah, HAL_DEBUG_BT_COEX, 
            "(MCI) %s: Send SW SYS_WAKING to remot(BT)\n", __func__);
        ar9300_mci_send_sys_waking(ah, AH_TRUE);

        OS_DELAY(10);

        /*
         * Set BT priority interrupt value to be 0xff to
         * avoid having too many BT PRIORITY interrupts.
         */

        OS_REG_WRITE(ah, AR_MCI_BT_PRI0, 0xFFFFFFFF);
        OS_REG_WRITE(ah, AR_MCI_BT_PRI1, 0xFFFFFFFF);
        OS_REG_WRITE(ah, AR_MCI_BT_PRI2, 0xFFFFFFFF);
        OS_REG_WRITE(ah, AR_MCI_BT_PRI3, 0xFFFFFFFF);
        OS_REG_WRITE(ah, AR_MCI_BT_PRI, 0X000000FF);

        /*
         * A contention reset will be received after send out sys_waking.
         * Also BT priority interrupt bits will be set. Clear those bits
         * before the next step.
         */
        OS_REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_RAW, 
            AR_MCI_INTERRUPT_RX_MSG_CONT_RST);
        OS_REG_WRITE(ah, AR_MCI_INTERRUPT_RAW, AR_MCI_INTERRUPT_BT_PRI);

        if (AR_SREV_JUPITER_10(ah) ||
           (ahp->ah_mci_coex_is_2g &&
            MCI_ANT_ARCH_PA_LNA_SHARED(ah->ah_config.ath_hal_mci_config))) {
            /* Send LNA_TRANS */
            HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) %s: Send LNA_TRANS to BT\n", 
                __func__);
            ar9300_mci_send_lna_transfer(ah, AH_TRUE);
    
            OS_DELAY(5);
        }

        if (AR_SREV_JUPITER_10(ah) ||
            (ahp->ah_mci_coex_is_2g && !ahp->ah_mci_coex_2g5g_update &&
            MCI_ANT_ARCH_PA_LNA_SHARED(ah->ah_config.ath_hal_mci_config))) {
            if (ar9300_mci_wait_for_interrupt(ah, AR_MCI_INTERRUPT_RX_MSG_RAW, 
                AR_MCI_INTERRUPT_RX_MSG_LNA_INFO, mci_timeout)) {
                HALDEBUG(ah, HAL_DEBUG_BT_COEX, 
                    "(MCI) %s: WLAN has control over the LNA & BT obeys it\n", 
                    __func__);
            } else {
                HALDEBUG(ah, HAL_DEBUG_BT_COEX, 
                    "(MCI) %s: BT did not respond to LNA_TRANS!\n", __func__);
                //ahp->ah_mci_bt_state = MCI_BT_SLEEP;
            }
        }

        if (AR_SREV_JUPITER_10(ah)) {
            /* Send another remote_reset to deassert BT clk_req. */
            HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                "(MCI) %s: Another remote_reset to deassert clk_req.\n", 
                __func__);
            ar9300_mci_remote_reset(ah, AH_TRUE);
            OS_DELAY(252);
        }
    }

    /* Clear the extra redundant SYS_WAKING from BT */
    if ((ahp->ah_mci_bt_state == MCI_BT_AWAKE) &&
        (OS_REG_READ_FIELD(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
            AR_MCI_INTERRUPT_RX_MSG_SYS_WAKING)) &&
        (OS_REG_READ_FIELD(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
            AR_MCI_INTERRUPT_RX_MSG_SYS_SLEEPING) == 0))
    {
        OS_REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
            AR_MCI_INTERRUPT_RX_MSG_SYS_WAKING);
        OS_REG_WRITE(ah, AR_MCI_INTERRUPT_RAW,
            AR_MCI_INTERRUPT_REMOTE_SLEEP_UPDATE);
    }

    OS_REG_WRITE(ah, AR_MCI_INTERRUPT_EN, saved_mci_int_en);
}

void ar9300_mci_setup(struct ath_hal *ah, u_int32_t gpm_addr, 
                      void *gpm_buf, u_int16_t len,
                      u_int32_t sched_addr)
{
    struct ath_hal_9300 *ahp = AH9300(ah);
    void *sched_buf = (void *)((char *) gpm_buf + (sched_addr - gpm_addr));

    ahp->ah_mci_gpm_addr = gpm_addr;
    ahp->ah_mci_gpm_buf = gpm_buf;
    ahp->ah_mci_gpm_len = len;
    ahp->ah_mci_sched_addr = sched_addr;
    ahp->ah_mci_sched_buf = sched_buf;

    ar9300_mci_reset(ah, AH_TRUE, AH_TRUE, AH_TRUE);
}

void ar9300_mci_disable_interrupt(struct ath_hal *ah)
{
    OS_REG_WRITE(ah, AR_MCI_INTERRUPT_EN, 0);
    OS_REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_EN, 0);
}

void ar9300_mci_enable_interrupt(struct ath_hal *ah)
{
    OS_REG_WRITE(ah, AR_MCI_INTERRUPT_EN, AR_MCI_INTERRUPT_DEFAULT);
    OS_REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_EN, 
        AR_MCI_INTERRUPT_RX_MSG_DEFAULT);
}

static void ar9300_mci_set_btcoex_ctrl_9565_1ANT(struct ath_hal *ah)
{
    uint32_t regval;

    HALDEBUG(ah, HAL_DEBUG_BT_COEX, "%s: called\n", __func__);
    regval = SM(1, AR_BTCOEX_CTRL_JUPITER_MODE) |
      SM(1, AR_BTCOEX_CTRL_WBTIMER_EN) |
      SM(1, AR_BTCOEX_CTRL_PA_SHARED) |
      SM(1, AR_BTCOEX_CTRL_LNA_SHARED) |
      SM(1, AR_BTCOEX_CTRL_NUM_ANTENNAS) |
      SM(1, AR_BTCOEX_CTRL_RX_CHAIN_MASK) |
      SM(0, AR_BTCOEX_CTRL_1_CHAIN_ACK) |
      SM(0, AR_BTCOEX_CTRL_1_CHAIN_BCN) |
      SM(0, AR_BTCOEX_CTRL_ONE_STEP_LOOK_AHEAD_EN);

    OS_REG_RMW_FIELD(ah, AR_BTCOEX_CTRL2,
      AR_BTCOEX_CTRL2_TX_CHAIN_MASK, 0x1);
    OS_REG_WRITE(ah, AR_BTCOEX_CTRL, regval);
}

static void ar9300_mci_set_btcoex_ctrl_9565_2ANT(struct ath_hal *ah)
{
    uint32_t regval;

    HALDEBUG(ah, HAL_DEBUG_BT_COEX, "%s: called\n", __func__);
    regval = SM(1, AR_BTCOEX_CTRL_JUPITER_MODE) |
      SM(1, AR_BTCOEX_CTRL_WBTIMER_EN) |
      SM(0, AR_BTCOEX_CTRL_PA_SHARED) |
      SM(0, AR_BTCOEX_CTRL_LNA_SHARED) |
      SM(2, AR_BTCOEX_CTRL_NUM_ANTENNAS) |
      SM(1, AR_BTCOEX_CTRL_RX_CHAIN_MASK) |
      SM(0, AR_BTCOEX_CTRL_1_CHAIN_ACK) |
      SM(0, AR_BTCOEX_CTRL_1_CHAIN_BCN) |
      SM(0, AR_BTCOEX_CTRL_ONE_STEP_LOOK_AHEAD_EN);

    OS_REG_RMW_FIELD(ah, AR_BTCOEX_CTRL2,
      AR_BTCOEX_CTRL2_TX_CHAIN_MASK, 0x0);
    OS_REG_WRITE(ah, AR_BTCOEX_CTRL, regval);
}

static void ar9300_mci_set_btcoex_ctrl_9462(struct ath_hal *ah)
{
    uint32_t regval;

    HALDEBUG(ah, HAL_DEBUG_BT_COEX, "%s: called\n", __func__);
    regval = SM(1, AR_BTCOEX_CTRL_JUPITER_MODE) |
      SM(1, AR_BTCOEX_CTRL_WBTIMER_EN) |
      SM(1, AR_BTCOEX_CTRL_PA_SHARED) |
      SM(1, AR_BTCOEX_CTRL_LNA_SHARED) |
      SM(2, AR_BTCOEX_CTRL_NUM_ANTENNAS) |
      SM(3, AR_BTCOEX_CTRL_RX_CHAIN_MASK) |
      SM(0, AR_BTCOEX_CTRL_1_CHAIN_ACK) |
      SM(0, AR_BTCOEX_CTRL_1_CHAIN_BCN) |
      SM(0, AR_BTCOEX_CTRL_ONE_STEP_LOOK_AHEAD_EN);

    if (AR_SREV_JUPITER_10(ah)) {
        regval |= SM(1, AR_BTCOEX_CTRL_SPDT_ENABLE_10);
    }

    OS_REG_WRITE(ah, AR_BTCOEX_CTRL, regval);
}

void ar9300_mci_reset(struct ath_hal *ah, HAL_BOOL en_int, HAL_BOOL is_2g,
                      HAL_BOOL is_full_sleep)
{
    struct ath_hal_9300 *ahp = AH9300(ah);
//    struct ath_hal_private *ahpriv = AH_PRIVATE(ah);
    u_int32_t regval;

    HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) %s: full_sleep = %d, is_2g = %d\n",
        __func__, is_full_sleep, is_2g);

    if (!ahp->ah_mci_gpm_addr && !ahp->ah_mci_sched_addr) {
        /* GPM buffer and scheduling message buffer are not allocated */
        HALDEBUG(ah, HAL_DEBUG_BT_COEX,
            "(MCI) GPM and SCHEDULE buffers not allocated\n");
        return;
    }

    if (OS_REG_READ(ah, AR_BTCOEX_CTRL) == 0xdeadbeef) {
        HALDEBUG(ah, HAL_DEBUG_BT_COEX, 
            "(MCI) %s: ### It's deadbeef, quit mcireset()\n", __func__);
        return;
    }

    /* Program MCI DMA related registers */
    OS_REG_WRITE(ah, AR_MCI_GPM_0, ahp->ah_mci_gpm_addr);
    OS_REG_WRITE(ah, AR_MCI_GPM_1, ahp->ah_mci_gpm_len);
    OS_REG_WRITE(ah, AR_MCI_SCHD_TABLE_0, ahp->ah_mci_sched_addr);

    /*
     * To avoid MCI state machine be affected by incoming remote MCI messages,
     * MCI mode will be enabled later, right before reset the MCI TX and RX.
     */
    if (AR_SREV_APHRODITE(ah)) {
        uint8_t ant = MS(ah->ah_config.ath_hal_mci_config,
          ATH_MCI_CONFIG_ANT_ARCH);
        if (ant == ATH_MCI_ANT_ARCH_1_ANT_PA_LNA_SHARED)
            ar9300_mci_set_btcoex_ctrl_9565_1ANT(ah);
        else
            ar9300_mci_set_btcoex_ctrl_9565_2ANT(ah);
    } else {
            ar9300_mci_set_btcoex_ctrl_9462(ah);
    }


    if (is_2g && (AR_SREV_JUPITER_20_OR_LATER(ah) || AR_SREV_APHRODITE(ah)) &&
         !(ah->ah_config.ath_hal_mci_config &
           ATH_MCI_CONFIG_DISABLE_OSLA))
    {
        ar9300_mci_osla_setup(ah, AH_TRUE);
    }
    else {
        ar9300_mci_osla_setup(ah, AH_FALSE);
    }

    if (AR_SREV_JUPITER_20_OR_LATER(ah) || AR_SREV_APHRODITE(ah)) {
        OS_REG_SET_BIT(ah, AR_GLB_CONTROL, AR_BTCOEX_CTRL_SPDT_ENABLE);

        OS_REG_RMW_FIELD(ah, AR_BTCOEX_CTRL3,
                         AR_BTCOEX_CTRL3_CONT_INFO_TIMEOUT, 20);
    }

    OS_REG_RMW_FIELD(ah, AR_BTCOEX_CTRL2, AR_BTCOEX_CTRL2_RX_DEWEIGHT, 0);

    OS_REG_RMW_FIELD(ah, AR_PCU_MISC, AR_PCU_BT_ANT_PREVENT_RX, 0);

    /* Set the time out to 3.125ms (5 BT slots) */
    OS_REG_RMW_FIELD(ah, AR_BTCOEX_WL_LNA, AR_BTCOEX_WL_LNA_TIMEOUT, 0x3D090);

    if (ah->ah_config.ath_hal_mci_config & ATH_MCI_CONFIG_CONCUR_TX) {
        u_int8_t i;
        u_int32_t const *pmax_tx_pwr;

        if ((ah->ah_config.ath_hal_mci_config & 
             ATH_MCI_CONFIG_CONCUR_TX) == ATH_MCI_CONCUR_TX_SHARED_CHN)
        {
            ahp->ah_mci_concur_tx_en = (ahp->ah_bt_coex_flag & 
                HAL_BT_COEX_FLAG_MCI_MAX_TX_PWR) ? AH_TRUE : AH_FALSE;

            HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) concur_tx_en = %d\n", 
                     ahp->ah_mci_concur_tx_en);
            /*
             * We're not relying on HW to reduce WLAN tx power.
             * Set the max tx power table to 0x7f for all.
             */
#if 0
            if (AH_PRIVATE(ah)->ah_curchan) {
                chan_flags = AH_PRIVATE(ah)->ah_curchan->channel_flags;
            }
            if (chan_flags == CHANNEL_G_HT20) {
                pmax_tx_pwr = &mci_concur_tx_max_pwr[2][0];
            }
            else if (chan_flags == CHANNEL_G) {
                pmax_tx_pwr = &mci_concur_tx_max_pwr[1][0];
            }
            else if ((chan_flags == CHANNEL_G_HT40PLUS) || 
                     (chan_flags == CHANNEL_G_HT40MINUS))
            {
                pmax_tx_pwr = &mci_concur_tx_max_pwr[3][0];
            }
            else {
                pmax_tx_pwr = &mci_concur_tx_max_pwr[0][0];
            }

            if (ahp->ah_mci_concur_tx_en) {
                HALDEBUG(ah, HAL_DEBUG_BT_COEX, 
                        "(MCI) chan flags = 0x%x, max_tx_pwr = %d dBm\n", 
                        chan_flags, 
                        (MS(pmax_tx_pwr[2],
                         ATH_MCI_CONCUR_TX_LOWEST_PWR_MASK) >> 1));
            }
#else
            pmax_tx_pwr = &mci_concur_tx_max_pwr[0][0];
#endif
        }
        else if ((ah->ah_config.ath_hal_mci_config &
                  ATH_MCI_CONFIG_CONCUR_TX) == ATH_MCI_CONCUR_TX_UNSHARED_CHN)
        {
            pmax_tx_pwr = &mci_concur_tx_max_pwr[0][0];
            ahp->ah_mci_concur_tx_en = AH_TRUE;
        }
        else {
            pmax_tx_pwr = &mci_concur_tx_max_pwr[0][0];
            ahp->ah_mci_concur_tx_en = AH_TRUE;
        }

    	/* Default is using rate based TPC. */
        OS_REG_RMW_FIELD(ah, AR_BTCOEX_CTRL2, 
                         AR_BTCOEX_CTRL2_DESC_BASED_TXPWR_ENABLE, 0);
        OS_REG_RMW_FIELD(ah, AR_BTCOEX_CTRL2,
                         AR_BTCOEX_CTRL2_TXPWR_THRESH, 0x7f);
        OS_REG_RMW_FIELD(ah, AR_BTCOEX_CTRL, 
                         AR_BTCOEX_CTRL_REDUCE_TXPWR, 0);
        for (i = 0; i < 8; i++) {
            OS_REG_WRITE(ah, AR_BTCOEX_MAX_TXPWR(i), pmax_tx_pwr[i]);
        }
    }

    regval = MS(ah->ah_config.ath_hal_mci_config,
                ATH_MCI_CONFIG_CLK_DIV);
    OS_REG_RMW_FIELD(ah, AR_MCI_TX_CTRL, AR_MCI_TX_CTRL_CLK_DIV, regval);

    OS_REG_SET_BIT(ah, AR_BTCOEX_CTRL, AR_BTCOEX_CTRL_MCI_MODE_EN);

    /* Resetting the Rx and Tx paths of MCI */
    regval = OS_REG_READ(ah, AR_MCI_COMMAND2);
    regval |= SM(1, AR_MCI_COMMAND2_RESET_TX);
    OS_REG_WRITE(ah, AR_MCI_COMMAND2, regval);
    OS_DELAY(1);
    regval &= ~SM(1, AR_MCI_COMMAND2_RESET_TX);
    OS_REG_WRITE(ah, AR_MCI_COMMAND2, regval);

    if (is_full_sleep) {
        ar9300_mci_mute_bt(ah);
        OS_DELAY(100);
    }

    regval |= SM(1, AR_MCI_COMMAND2_RESET_RX);
    OS_REG_WRITE(ah, AR_MCI_COMMAND2, regval);
    OS_DELAY(1);
    regval &= ~SM(1, AR_MCI_COMMAND2_RESET_RX);
    OS_REG_WRITE(ah, AR_MCI_COMMAND2, regval);

    ar9300_mci_state(ah, HAL_MCI_STATE_INIT_GPM_OFFSET, NULL);
    OS_REG_WRITE(ah, AR_MCI_MSG_ATTRIBUTES_TABLE,
             (SM(0xe801, AR_MCI_MSG_ATTRIBUTES_TABLE_INVALID_HDR) |
              SM(0x0000, AR_MCI_MSG_ATTRIBUTES_TABLE_CHECKSUM)));
    if (MCI_ANT_ARCH_PA_LNA_SHARED(ah->ah_config.ath_hal_mci_config)) {
        OS_REG_CLR_BIT(ah, AR_MCI_TX_CTRL, AR_MCI_TX_CTRL_DISABLE_LNA_UPDATE);
    } else {
        OS_REG_SET_BIT(ah, AR_MCI_TX_CTRL, AR_MCI_TX_CTRL_DISABLE_LNA_UPDATE);
    }

    if (AR_SREV_JUPITER_20_OR_LATER(ah) || AR_SREV_APHRODITE(ah)) {
        ar9300_mci_observation_set_up(ah);
    }
    
    ahp->ah_mci_ready = AH_TRUE;
    ar9300_mci_prep_interface(ah);

    if (en_int) {
        ar9300_mci_enable_interrupt(ah);
    }

#if ATH_SUPPORT_AIC
    if (ahp->ah_aic_enabled) {
        ar9300_aic_start_normal(ah);
    }
#endif
}

static void ar9300_mci_queue_unsent_gpm(struct ath_hal *ah, u_int8_t header,
                                        u_int32_t *payload, HAL_BOOL queue)
{
    struct ath_hal_9300 *ahp = AH9300(ah);
    u_int8_t type, opcode;

    if (queue == AH_TRUE) {
        if (payload != NULL) {
            HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                "(MCI) ERROR: Send fail: %02x: %02x %02x %02x\n",
                header,
                *(((u_int8_t *)payload) + 4),
                *(((u_int8_t *)payload) + 5),
                *(((u_int8_t *)payload) + 6));
        } else {
            HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                "(MCI) ERROR: Send fail: %02x\n", header);
        }
    }
    /* check if the message is to be queued */
    if (header == MCI_GPM) {
        type = MCI_GPM_TYPE(payload);
        opcode = MCI_GPM_OPCODE(payload);

        if (type == MCI_GPM_COEX_AGENT) {
            switch (opcode)
            {
                case MCI_GPM_COEX_BT_UPDATE_FLAGS:
                    if (AR_SREV_JUPITER_10(ah)) {
                        break;
                    }
                    if (*(((u_int8_t *)payload) + MCI_GPM_COEX_B_BT_FLAGS_OP) ==
                        MCI_GPM_COEX_BT_FLAGS_READ)
                    {
                        break;
                    }
                    ahp->ah_mci_coex_2g5g_update = queue;
                    if (queue == AH_TRUE) {
                        HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                            "(MCI) BT_MCI_FLAGS: 2G5G status <queued> %s.\n",
                            ahp->ah_mci_coex_is_2g?"2G":"5G");
                    }
                    else {
                        HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                            "(MCI) BT_MCI_FLAGS: 2G5G status <sent> %s.\n",
                            ahp->ah_mci_coex_is_2g?"2G":"5G");
                    }
                    break;

                case MCI_GPM_COEX_WLAN_CHANNELS:
                    ahp->ah_mci_coex_wlan_channels_update = queue;
                    if (queue == AH_TRUE) {
                        HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                            "(MCI) WLAN channel map <queued>.\n");
                    }
                    else {
                        HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                            "(MCI) WLAN channel map <sent>.\n");
                    }
                    break;

                case MCI_GPM_COEX_HALT_BT_GPM:
                    if (*(((u_int8_t *)payload) + MCI_GPM_COEX_B_HALT_STATE) ==
                        MCI_GPM_COEX_BT_GPM_UNHALT)
                    {
                        ahp->ah_mci_unhalt_bt_gpm = queue;
                        if (queue == AH_TRUE) {
                            HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                                "(MCI) UNHALT BT GPM <queued>.\n");
                        }
                        else {
                            ahp->ah_mci_halted_bt_gpm = AH_FALSE;
                            HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                                "(MCI) UNHALT BT GPM <sent>.\n");
                        }
                    }
                    if (*(((u_int8_t *)payload) + MCI_GPM_COEX_B_HALT_STATE) ==
                        MCI_GPM_COEX_BT_GPM_HALT)
                    {
                        ahp->ah_mci_halted_bt_gpm = !queue;
                        if (queue == AH_TRUE) {
                            HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                                "(MCI) HALT BT GPM <not sent>.\n");
                        }
                        else {
                            HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                                "(MCI) HALT BT GPM <sent>.\n");
                        }
                    }
                    break;

                default:
                    break;
            }
        }
    }
}

HAL_BOOL ar9300_mci_send_message(struct ath_hal *ah, u_int8_t header,
                              u_int32_t flag, u_int32_t *payload, 
                              u_int8_t len, HAL_BOOL wait_done, HAL_BOOL check_bt)
{
    int i;
    struct ath_hal_9300 *ahp = AH9300(ah);
    HAL_BOOL msg_sent = AH_FALSE;
    u_int32_t regval;
    u_int32_t saved_mci_int_en = OS_REG_READ(ah, AR_MCI_INTERRUPT_EN);

    regval = OS_REG_READ(ah, AR_BTCOEX_CTRL);
    if ((regval == 0xdeadbeef) || !(regval & AR_BTCOEX_CTRL_MCI_MODE_EN)) {
        HALDEBUG(ah, HAL_DEBUG_BT_COEX,
            "(MCI) %s: Not send 0x%x. MCI is not enabled. full_sleep = %d\n", 
            __func__, header, ahp->ah_chip_full_sleep);
        ar9300_mci_queue_unsent_gpm(ah, header, payload, AH_TRUE);
        return AH_FALSE;
    }
    else if (check_bt && (ahp->ah_mci_bt_state == MCI_BT_SLEEP)) {
        HALDEBUG(ah, HAL_DEBUG_BT_COEX, 
            "(MCI) %s: Don't send message(0x%x). BT is in sleep state\n", 
            __func__, header);
        ar9300_mci_queue_unsent_gpm(ah, header, payload, AH_TRUE);
        return AH_FALSE;
    }

    if (wait_done) {
        OS_REG_WRITE(ah, AR_MCI_INTERRUPT_EN, 0);
    }

    /* Need to clear SW_MSG_DONE raw bit before wait */
    OS_REG_WRITE(ah, AR_MCI_INTERRUPT_RAW,
        AR_MCI_INTERRUPT_SW_MSG_DONE | AR_MCI_INTERRUPT_MSG_FAIL_MASK);

    if (payload != AH_NULL) {
        for (i = 0; (i*4) < len; i++) {
            OS_REG_WRITE(ah, (AR_MCI_TX_PAYLOAD0 + i*4), *(payload + i));
        }
    }
    ar9300_mci_print_msg(ah, AH_TRUE, header, len, payload);

    OS_REG_WRITE(ah, AR_MCI_COMMAND0,
                (SM((flag & HAL_MCI_FLAG_DISABLE_TIMESTAMP), 
                 AR_MCI_COMMAND0_DISABLE_TIMESTAMP) |
                 SM(len, AR_MCI_COMMAND0_LEN) |
                 SM(header, AR_MCI_COMMAND0_HEADER)));

    if (wait_done &&
        ar9300_mci_wait_for_interrupt(ah, AR_MCI_INTERRUPT_RAW, 
                                    AR_MCI_INTERRUPT_SW_MSG_DONE, 500) == 0)
    {
        ar9300_mci_queue_unsent_gpm(ah, header, payload, AH_TRUE);
    }
    else {
        ar9300_mci_queue_unsent_gpm(ah, header, payload, AH_FALSE);
        msg_sent = AH_TRUE;
    }
    
    if (wait_done) {
        OS_REG_WRITE(ah, AR_MCI_INTERRUPT_EN, saved_mci_int_en);
    }

    return msg_sent;
}

u_int32_t ar9300_mci_get_interrupt(struct ath_hal *ah, u_int32_t *mci_int, 
                                   u_int32_t *mci_int_rx_msg)
{
    struct ath_hal_9300 *ahp = AH9300(ah);

    *mci_int = ahp->ah_mci_int_raw;
    *mci_int_rx_msg = ahp->ah_mci_int_rx_msg;

    /* Clean int bits after the values are read. */
    ahp->ah_mci_int_raw = 0;
    ahp->ah_mci_int_rx_msg = 0;

    return 0;
}

u_int32_t ar9300_mci_check_int(struct ath_hal *ah, u_int32_t ints)
{
    u_int32_t reg;

    reg = OS_REG_READ(ah, AR_MCI_INTERRUPT_RX_MSG_RAW);
    return ((reg & ints) == ints);
}

void ar9300_mci_sync_bt_state(struct ath_hal *ah)
{
    struct ath_hal_9300 *ahp = AH9300(ah);
    u_int32_t cur_bt_state;

    cur_bt_state = ar9300_mci_state(ah, HAL_MCI_STATE_REMOTE_SLEEP, NULL);
    if (ahp->ah_mci_bt_state != cur_bt_state) {
        HALDEBUG(ah, HAL_DEBUG_BT_COEX,
            "(MCI) %s: BT state mismatches. old: %d, new: %d\n",
            __func__, ahp->ah_mci_bt_state, cur_bt_state);
        ahp->ah_mci_bt_state = cur_bt_state;
    }
    if (ahp->ah_mci_bt_state != MCI_BT_SLEEP) {
#if MCI_QUERY_BT_VERSION_VERBOSE
        ar9300_mci_send_coex_version_query(ah, AH_TRUE);
#endif
        ar9300_mci_send_coex_wlan_channels(ah, AH_TRUE);
        if (ahp->ah_mci_unhalt_bt_gpm == AH_TRUE) {
            HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) %s: UNHALT BT GPM\n", __func__);
            ar9300_mci_send_coex_halt_bt_gpm(ah, AH_FALSE, AH_TRUE);
        }
    }
}

static HAL_BOOL ar9300_mci_is_gpm_valid(struct ath_hal *ah, u_int32_t msg_index)
{
    struct ath_hal_9300 *ahp = AH9300(ah);
    u_int32_t *payload;
    u_int32_t recv_type, offset = msg_index << 4;

    if (msg_index == HAL_MCI_GPM_INVALID) {
        return AH_FALSE;
    }

    payload = (u_int32_t *) (ahp->ah_mci_gpm_buf + offset);
    recv_type = MCI_GPM_TYPE(payload);

    if (recv_type == MCI_GPM_RSVD_PATTERN) {
        HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) Skip RSVD GPM\n");
        return AH_FALSE;
    }

    return AH_TRUE;
}

u_int32_t
ar9300_mci_state(struct ath_hal *ah, u_int32_t state_type, u_int32_t *p_data)
{
    u_int32_t   value = 0, more_gpm = 0, gpm_ptr;
    struct ath_hal_9300 *ahp = AH9300(ah);

    switch (state_type) {
        case HAL_MCI_STATE_ENABLE:
            if (AH_PRIVATE(ah)->ah_caps.halMciSupport && ahp->ah_mci_ready) {
                value = OS_REG_READ(ah, AR_BTCOEX_CTRL);
                if ((value == 0xdeadbeef) || (value == 0xffffffff)) {
                        HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                            "(MCI) BTCOEX_CTRL = 0xdeadbeef\n");
                    value = 0;
                }
            }
            value &= AR_BTCOEX_CTRL_MCI_MODE_EN;
            break;

        case HAL_MCI_STATE_INIT_GPM_OFFSET:
            value = MS(OS_REG_READ(ah, AR_MCI_GPM_1), AR_MCI_GPM_WRITE_PTR);
            HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                    "(MCI) %s: GPM initial WRITE_PTR=%d.\n", __func__, value);
            ahp->ah_mci_gpm_idx = value;
            break;

        case HAL_MCI_STATE_NEXT_GPM_OFFSET:
        case HAL_MCI_STATE_LAST_GPM_OFFSET:
            /*
             * This could be useful to avoid new GPM message interrupt which
             * may lead to spurious interrupt after power sleep, or multiple
             * entry of ath_coex_mci_intr().
             * Adding empty GPM check by returning HAL_MCI_GPM_INVALID can
             * alleviate this effect, but clearing GPM RX interrupt bit is
             * safe, because whether this is called from HAL or LMAC, there
             * must be an interrupt bit set/triggered initially.
             */
            OS_REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
                         AR_MCI_INTERRUPT_RX_MSG_GPM);

            gpm_ptr = MS(OS_REG_READ(ah, AR_MCI_GPM_1), AR_MCI_GPM_WRITE_PTR);
            value = gpm_ptr;

            if (value == 0) {
                value = ahp->ah_mci_gpm_len - 1;
            }
            else if (value >= ahp->ah_mci_gpm_len) {
                if (value != 0xFFFF) {
                    value = 0;
                    HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                        "(MCI) %s: GPM offset out of range.\n", __func__);
                }
            }
            else {
                value--;
            }

            if (value == 0xFFFF) {
                value = HAL_MCI_GPM_INVALID;
                more_gpm = HAL_MCI_GPM_NOMORE;
                HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                        "(MCI) %s: GPM ptr invalid "
                        "@ptr=%d, @offset=%d, more=NOMORE.\n",
                        __func__, gpm_ptr, value);
            }
            else if (state_type == HAL_MCI_STATE_NEXT_GPM_OFFSET) {
                if (gpm_ptr == ahp->ah_mci_gpm_idx) {
                    value = HAL_MCI_GPM_INVALID;
                    more_gpm = HAL_MCI_GPM_NOMORE;
                    HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                            "(MCI) %s: GPM message not available "
                            "@ptr=%d, @offset=%d, more=NOMORE.\n",
                            __func__, gpm_ptr, value);
                }
                else {
                    while (1) {
                        u_int32_t temp_index;

                        /* skip reserved GPM if any */
                        if (value != ahp->ah_mci_gpm_idx) {
                            more_gpm = HAL_MCI_GPM_MORE;
                        }
                        else {
                            more_gpm = HAL_MCI_GPM_NOMORE;
                        }
                        temp_index = ahp->ah_mci_gpm_idx;
                        ahp->ah_mci_gpm_idx++;
                        if (ahp->ah_mci_gpm_idx >= ahp->ah_mci_gpm_len) {
                            ahp->ah_mci_gpm_idx = 0;
                        }
                        HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                                "(MCI) %s: GPM message got "
                                "@ptr=%d, @offset=%d, more=%s.\n",
                                __func__, gpm_ptr, temp_index,
                                (more_gpm == HAL_MCI_GPM_MORE)?"MORE":"NOMORE");
                        if (ar9300_mci_is_gpm_valid(ah, temp_index)) {
                            value = temp_index;
                            break;
                        }
                        if (more_gpm == HAL_MCI_GPM_NOMORE) {
                            value = HAL_MCI_GPM_INVALID;
                            break;
                        }
                    }
                }
                if (p_data != NULL) {
                    *p_data = more_gpm;
                }
            }
            if (value != HAL_MCI_GPM_INVALID) {
                value <<= 4;
            }
            break;

    case HAL_MCI_STATE_LAST_SCHD_MSG_OFFSET:
        value = MS(OS_REG_READ(ah, AR_MCI_RX_STATUS), 
            AR_MCI_RX_LAST_SCHD_MSG_INDEX);

#if AH_MCI_DEBUG_PRINT_SCHED
        {
            u_int32_t index = value;
            u_int32_t prev_index, sched_idx;
            u_int32_t *pld;
            u_int8_t  *pld8;
            u_int32_t wbtimer = OS_REG_READ(ah, AR_BTCOEX_WBTIMER);
            u_int32_t schd_ctl = OS_REG_READ(ah, AR_MCI_HW_SCHD_TBL_CTL);

            if (index > 0) {
                prev_index = index - 1;
            } else {
                prev_index = index;
            }

            HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) SCHED\n");
            HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                "(MCI) SCHED SCHD_TBL_CTRL=0x%08x, WBTIMER=0x%08x (%d)\n",
                schd_ctl, wbtimer, wbtimer);
            for (sched_idx = prev_index; sched_idx <= index; sched_idx++) {
                pld = (u_int32_t *) (ahp->ah_mci_sched_buf + (sched_idx << 4));
                pld8 = (u_int8_t *) pld;

                ar9300_mci_print_msg(ah, AH_FALSE, MCI_SCHD_INFO, 16, pld);
                HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                    "(MCI) SCHED    idx=%d, T1=0x%08x (%d), T2=0x%08x (%d)\n",
                    sched_idx,
                    pld[0], pld[0], pld[1], pld[1]);
                HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                    "(MCI) SCHED    addr=%d %s pwr=%d prio=%d %s link=%d\n",
                    pld8[11] >> 4,
                    (pld8[11] & 0x08)?"TX":"RX",
                    (int8_t) (((pld8[11] & 0x07) << 5) | (pld8[10] >> 3)),
                    (((pld8[10] & 0x07) << 5) | (pld8[9] >> 3)),
                    (pld8[9] & 0x04)?"LE":"BR/EDR",
                    (((pld8[9] & 0x03) << 2) | (pld8[8] >> 6)));
            }
        }
#endif /* AH_MCI_DEBUG_PRINT_SCHED */

        /* Make it in bytes */
        value <<= 4;
        break;

    case HAL_MCI_STATE_REMOTE_SLEEP:
        value = MS(OS_REG_READ(ah, AR_MCI_RX_STATUS), 
            AR_MCI_RX_REMOTE_SLEEP) ? MCI_BT_SLEEP : MCI_BT_AWAKE;
        break;

        case HAL_MCI_STATE_CONT_RSSI_POWER:
            value = MS(ahp->ah_mci_cont_status,
                AR_MCI_CONT_RSSI_POWER);
            break;

        case HAL_MCI_STATE_CONT_PRIORITY:
            value = MS(ahp->ah_mci_cont_status,
                AR_MCI_CONT_RRIORITY);
            break;

        case HAL_MCI_STATE_CONT_TXRX:
            value = MS(ahp->ah_mci_cont_status,
                AR_MCI_CONT_TXRX);
            break;

        case HAL_MCI_STATE_BT:
            value = ahp->ah_mci_bt_state;
            break;

        case HAL_MCI_STATE_SET_BT_SLEEP:
            ahp->ah_mci_bt_state = MCI_BT_SLEEP;
            break;

        case HAL_MCI_STATE_SET_BT_AWAKE:
            ahp->ah_mci_bt_state = MCI_BT_AWAKE;
            ar9300_mci_send_coex_version_query(ah, AH_TRUE);
            ar9300_mci_send_coex_wlan_channels(ah, AH_TRUE);
            if (ahp->ah_mci_unhalt_bt_gpm == AH_TRUE) {
                HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                    "(MCI) %s: UNHALT BT GPM\n", __func__);
                ar9300_mci_send_coex_halt_bt_gpm(ah, AH_FALSE, AH_TRUE);
            }
            ar9300_mci_2g5g_switch(ah, AH_TRUE);
            break;

        case HAL_MCI_STATE_SET_BT_CAL_START:
            ahp->ah_mci_bt_state = MCI_BT_CAL_START;
            break;

        case HAL_MCI_STATE_SET_BT_CAL:
            ahp->ah_mci_bt_state = MCI_BT_CAL;
            break;

        case HAL_MCI_STATE_RESET_REQ_WAKE:
            ar9300_mci_reset_req_wakeup(ah);
            ahp->ah_mci_coex_2g5g_update = AH_TRUE;

            if ((AR_SREV_JUPITER_20_OR_LATER(ah) || AR_SREV_APHRODITE(ah)) && 
                (ah->ah_config.ath_hal_mci_config & 
                 ATH_MCI_CONFIG_MCI_OBS_MASK))
            {
                /* Check if we still have control of the GPIOs */
                if ((OS_REG_READ(ah, AR_GLB_GPIO_CONTROL) & 
                     ATH_MCI_CONFIG_MCI_OBS_GPIO) != 
                     ATH_MCI_CONFIG_MCI_OBS_GPIO)
                 {
                    HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                        "(MCI) Reconfigure observation\n");
                    ar9300_mci_observation_set_up(ah);
                 }
            }

            break;
            
        case HAL_MCI_STATE_SEND_WLAN_COEX_VERSION:
            ar9300_mci_send_coex_version_response(ah, AH_TRUE);
            break;

        case HAL_MCI_STATE_SET_BT_COEX_VERSION:
            if (p_data == NULL) {
                HALDEBUG(ah, HAL_DEBUG_BT_COEX,
                    "(MCI) Error: Set BT Coex version with NULL data !!!\n");
            }
            else {
                ahp->ah_mci_coex_major_version_bt = (*p_data >> 8) & 0xff;
                ahp->ah_mci_coex_minor_version_bt = (*p_data) & 0xff;
                ahp->ah_mci_coex_bt_version_known = AH_TRUE;
                HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) BT version set: %d.%d\n",
                        ahp->ah_mci_coex_major_version_bt,
                        ahp->ah_mci_coex_minor_version_bt);
            }
            break;

        case HAL_MCI_STATE_SEND_WLAN_CHANNELS:
            if (p_data != NULL)
            {
                if (((ahp->ah_mci_coex_wlan_channels[1] & 0xffff0000) ==
                    (*(p_data + 1) & 0xffff0000)) &&
                    (ahp->ah_mci_coex_wlan_channels[2] == *(p_data + 2)) &&
                    (ahp->ah_mci_coex_wlan_channels[3] == *(p_data + 3)))
                {
                    break;
                }
                ahp->ah_mci_coex_wlan_channels[0] = *p_data++;
                ahp->ah_mci_coex_wlan_channels[1] = *p_data++;
                ahp->ah_mci_coex_wlan_channels[2] = *p_data++;
                ahp->ah_mci_coex_wlan_channels[3] = *p_data++;
            }
            ahp->ah_mci_coex_wlan_channels_update = AH_TRUE;
            ar9300_mci_send_coex_wlan_channels(ah, AH_TRUE);
            break;

        case HAL_MCI_STATE_SEND_VERSION_QUERY:
            ar9300_mci_send_coex_version_query(ah, AH_TRUE);
            break;

        case HAL_MCI_STATE_SEND_STATUS_QUERY:
            if (AR_SREV_JUPITER_10(ah)) {
                ar9300_mci_send_coex_bt_status_query(ah, AH_TRUE,
                        MCI_GPM_COEX_QUERY_BT_ALL_INFO);
            } else {
                ar9300_mci_send_coex_bt_status_query(ah, AH_TRUE,
                        MCI_GPM_COEX_QUERY_BT_TOPOLOGY);
            }
            break;

        case HAL_MCI_STATE_NEED_FLUSH_BT_INFO:
            /*
             * ah_mci_unhalt_bt_gpm means whether it's needed to send
             * UNHALT message. It's set whenever there's a request to send HALT
             * message. ah_mci_halted_bt_gpm means whether HALT message is sent
             * out successfully.
             *
             * Checking (ah_mci_unhalt_bt_gpm == AH_FALSE) instead of checking
             * (ahp->ah_mci_halted_bt_gpm == AH_FALSE) will make sure currently is
             * in UNHALT-ed mode and BT can respond to status query.
             */
            if ((ahp->ah_mci_unhalt_bt_gpm == AH_FALSE) &&
                (ahp->ah_mci_need_flush_btinfo == AH_TRUE))
            {
                value = 1;
            }
            else {
                value = 0;
            }
            if (p_data != NULL) {
                ahp->ah_mci_need_flush_btinfo = (*p_data != 0)? AH_TRUE : AH_FALSE;
            }
            break;

        case HAL_MCI_STATE_SET_CONCUR_TX_PRI:
            if (p_data) {
                ahp->ah_mci_stomp_none_tx_pri = *p_data & 0xff;
                ahp->ah_mci_stomp_low_tx_pri = (*p_data >> 8) & 0xff;
                ahp->ah_mci_stomp_all_tx_pri = (*p_data >> 16) & 0xff;
            }
            break;

        case HAL_MCI_STATE_RECOVER_RX:
            HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) hal RECOVER_RX\n");
            ar9300_mci_prep_interface(ah);
            ahp->ah_mci_query_bt = AH_TRUE;
            ahp->ah_mci_need_flush_btinfo = AH_TRUE;
            ar9300_mci_send_coex_wlan_channels(ah, AH_TRUE);
            ar9300_mci_2g5g_switch(ah, AH_TRUE);
            break;
            
        case HAL_MCI_STATE_DEBUG:
            if (p_data != NULL) {
                if (*p_data == HAL_MCI_STATE_DEBUG_REQ_BT_DEBUG) {
                    HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) QUERY_BT_DEBUG\n");
                    ar9300_mci_send_coex_bt_status_query(ah, AH_TRUE,
                        MCI_GPM_COEX_QUERY_BT_DEBUG);
                    OS_DELAY(10);
                    if (AR_SREV_JUPITER_20_OR_LATER(ah) || AR_SREV_APHRODITE(ah)) {
                        ar9300_mci_send_coex_bt_flags(ah, AH_TRUE,
                            MCI_GPM_COEX_BT_FLAGS_READ, 0);
                    }
                }
            }
            break;

        case HAL_MCI_STATE_NEED_FTP_STOMP:
            value = (ah->ah_config.ath_hal_mci_config &
                     ATH_MCI_CONFIG_DISABLE_FTP_STOMP) ? 0 : 1;
            break;

        case HAL_MCI_STATE_NEED_TUNING:
            value = (ah->ah_config.ath_hal_mci_config &
                     ATH_MCI_CONFIG_DISABLE_TUNING) ? 0 : 1;
            break;

        case HAL_MCI_STATE_SHARED_CHAIN_CONCUR_TX:
            value = ((ah->ah_config.ath_hal_mci_config &
                     ATH_MCI_CONFIG_CONCUR_TX) == 
                     ATH_MCI_CONCUR_TX_SHARED_CHN)? 1 : 0;
            break;

        default:
            break;
    }
    return value;
}

void ar9300_mci_detach(struct ath_hal *ah)
{
    /* Turn off MCI and Jupiter mode. */
    OS_REG_WRITE(ah, AR_BTCOEX_CTRL, 0x00);
    HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) ar9300_mci_detach\n");
    ar9300_mci_disable_interrupt(ah);
}

/*
 * Low priority BT: 0 - 59(0x3b)
 * High priority BT: 60 - 125(0x7d)
 * Critical BT: 126 - 255

    BTCOEX_WL_WEIGHTS0_VALUE0 ; // wl_idle                       
    BTCOEX_WL_WEIGHTS0_VALUE1 ; // sw_ctrl[3] - all_stomp        
    BTCOEX_WL_WEIGHTS0_VALUE2 ; // sw_ctrl[2] - all_not_stomp    
    BTCOEX_WL_WEIGHTS0_VALUE3 ; // sw_ctrl[1] - pa_pre_distortion
    BTCOEX_WL_WEIGHTS1_VALUE0 ; // sw_ctrl[0] - general purpose  
    BTCOEX_WL_WEIGHTS1_VALUE1 ; // tm_wl_wait_beacon             
    BTCOEX_WL_WEIGHTS1_VALUE2 ; // ts_state_wait_ack_cts         
    BTCOEX_WL_WEIGHTS1_VALUE3 ; // self_gen                      
    BTCOEX_WL_WEIGHTS2_VALUE0 ; // idle                          
    BTCOEX_WL_WEIGHTS2_VALUE1 ; // rx                            
    BTCOEX_WL_WEIGHTS2_VALUE2 ; // tx                            
    BTCOEX_WL_WEIGHTS2_VALUE3 ; // rx + tx                       
    BTCOEX_WL_WEIGHTS3_VALUE0 ; // tx                            
    BTCOEX_WL_WEIGHTS3_VALUE1 ; // rx                            
    BTCOEX_WL_WEIGHTS3_VALUE2 ; // tx                            
    BTCOEX_WL_WEIGHTS3_VALUE3 ; // rx + tx                       

    Stomp all:
    ah_bt_coex_wlan_weight[0] = 0x00007d00
    ah_bt_coex_wlan_weight[1] = 0x7d7d7d00
    ah_bt_coex_wlan_weight[2] = 0x7d7d7d00
    ah_bt_coex_wlan_weight[3] = 0x7d7d7d7d
    Stomp low:
    ah_bt_coex_wlan_weight[0] = 0x00007d00
    ah_bt_coex_wlan_weight[1] = 0x7d3b3b00
    ah_bt_coex_wlan_weight[2] = 0x3b3b3b00
    ah_bt_coex_wlan_weight[3] = 0x3b3b3b3b
    Stomp none:
    ah_bt_coex_wlan_weight[0] = 0x00007d00
    ah_bt_coex_wlan_weight[1] = 0x7d000000
    ah_bt_coex_wlan_weight[2] = 0x00000000
    ah_bt_coex_wlan_weight[3] = 0x00000000
*/

void ar9300_mci_bt_coex_set_weights(struct ath_hal *ah, u_int32_t stomp_type)
{
    struct ath_hal_9300 *ahp = AH9300(ah);
//    struct ath_hal_private *ahpriv = AH_PRIVATE(ah);
    u_int32_t tx_priority = 0;

    HALDEBUG(ah, HAL_DEBUG_BT_COEX, "%s: stomp_type=%d\n", __func__, stomp_type);

    switch (stomp_type) {
    case HAL_BT_COEX_STOMP_ALL:
        ahp->ah_bt_coex_wlan_weight[0] = JUPITER_STOMP_ALL_WLAN_WGHT0;
        ahp->ah_bt_coex_wlan_weight[1] = JUPITER_STOMP_ALL_WLAN_WGHT1;
        ahp->ah_bt_coex_wlan_weight[2] = JUPITER_STOMP_ALL_WLAN_WGHT2;
        ahp->ah_bt_coex_wlan_weight[3] = JUPITER_STOMP_ALL_WLAN_WGHT3;
        if (ahp->ah_mci_concur_tx_en && ahp->ah_mci_stomp_all_tx_pri) {
            tx_priority = ahp->ah_mci_stomp_all_tx_pri;
        }
        break;
    case HAL_BT_COEX_STOMP_LOW:
        if (ahp->ah_bt_coex_flag & HAL_BT_COEX_FLAG_MCI_FTP_STOMP_RX) {
            ahp->ah_bt_coex_wlan_weight[0] = JUPITER_STOMP_LOW_FTP_WLAN_WGHT0;
            ahp->ah_bt_coex_wlan_weight[1] = JUPITER_STOMP_LOW_FTP_WLAN_WGHT1;
            ahp->ah_bt_coex_wlan_weight[2] = JUPITER_STOMP_LOW_FTP_WLAN_WGHT2;
            ahp->ah_bt_coex_wlan_weight[3] = JUPITER_STOMP_LOW_FTP_WLAN_WGHT3;
        }
        else {
            ahp->ah_bt_coex_wlan_weight[0] = JUPITER_STOMP_LOW_WLAN_WGHT0;
            ahp->ah_bt_coex_wlan_weight[1] = JUPITER_STOMP_LOW_WLAN_WGHT1;
            ahp->ah_bt_coex_wlan_weight[2] = JUPITER_STOMP_LOW_WLAN_WGHT2;
            ahp->ah_bt_coex_wlan_weight[3] = JUPITER_STOMP_LOW_WLAN_WGHT3;
        }
        if (ahp->ah_mci_concur_tx_en && ahp->ah_mci_stomp_low_tx_pri) {
            tx_priority = ahp->ah_mci_stomp_low_tx_pri;
        }
        if (ah->ah_config.ath_hal_mci_config & 
            ATH_MCI_CONFIG_MCI_OBS_TXRX)
        {
            ar9300_gpio_set(ah, 5, 1);
        }
        break;
    case HAL_BT_COEX_STOMP_ALL_FORCE:
        ahp->ah_bt_coex_wlan_weight[0] = JUPITER_STOMP_ALL_FORCE_WLAN_WGHT0;
        ahp->ah_bt_coex_wlan_weight[1] = JUPITER_STOMP_ALL_FORCE_WLAN_WGHT1;
        ahp->ah_bt_coex_wlan_weight[2] = JUPITER_STOMP_ALL_FORCE_WLAN_WGHT2;
        ahp->ah_bt_coex_wlan_weight[3] = JUPITER_STOMP_ALL_FORCE_WLAN_WGHT3;
        break;
    case HAL_BT_COEX_STOMP_LOW_FORCE:
        ahp->ah_bt_coex_wlan_weight[0] = JUPITER_STOMP_LOW_FORCE_WLAN_WGHT0;
        ahp->ah_bt_coex_wlan_weight[1] = JUPITER_STOMP_LOW_FORCE_WLAN_WGHT1;
        ahp->ah_bt_coex_wlan_weight[2] = JUPITER_STOMP_LOW_FORCE_WLAN_WGHT2;
        ahp->ah_bt_coex_wlan_weight[3] = JUPITER_STOMP_LOW_FORCE_WLAN_WGHT3;
        if (ahp->ah_mci_concur_tx_en && ahp->ah_mci_stomp_low_tx_pri) {
            tx_priority = ahp->ah_mci_stomp_low_tx_pri;
        }
        break;
    case HAL_BT_COEX_STOMP_NONE:
    case HAL_BT_COEX_NO_STOMP:
        ahp->ah_bt_coex_wlan_weight[0] = JUPITER_STOMP_NONE_WLAN_WGHT0;
        ahp->ah_bt_coex_wlan_weight[1] = JUPITER_STOMP_NONE_WLAN_WGHT1;
        ahp->ah_bt_coex_wlan_weight[2] = JUPITER_STOMP_NONE_WLAN_WGHT2;
        ahp->ah_bt_coex_wlan_weight[3] = JUPITER_STOMP_NONE_WLAN_WGHT3;
        if (ahp->ah_mci_concur_tx_en && ahp->ah_mci_stomp_none_tx_pri) {
            tx_priority = ahp->ah_mci_stomp_none_tx_pri;
        }
        if (ah->ah_config.ath_hal_mci_config & 
            ATH_MCI_CONFIG_MCI_OBS_TXRX)
        {
            ar9300_gpio_set(ah, 5, 0);
        }
        break;
    case HAL_BT_COEX_STOMP_AUDIO:
        ahp->ah_bt_coex_wlan_weight[0] = 0xffffff01;
        ahp->ah_bt_coex_wlan_weight[1] = 0xffffffff;
        ahp->ah_bt_coex_wlan_weight[2] = 0xffffff01;
        ahp->ah_bt_coex_wlan_weight[3] = 0xffffffff;
        break;
    default:
        /* There is a forceWeight from registry */
        ahp->ah_bt_coex_wlan_weight[0] = stomp_type;
        ahp->ah_bt_coex_wlan_weight[1] = stomp_type;
        break;
    }

    if (ahp->ah_mci_concur_tx_en && tx_priority) {
        ahp->ah_bt_coex_wlan_weight[1] &= ~MCI_CONCUR_TX_WLAN_WGHT1_MASK;
        ahp->ah_bt_coex_wlan_weight[1] |= 
            SM(tx_priority, MCI_CONCUR_TX_WLAN_WGHT1_MASK);
        ahp->ah_bt_coex_wlan_weight[2] &= ~MCI_CONCUR_TX_WLAN_WGHT2_MASK;
        ahp->ah_bt_coex_wlan_weight[2] |= 
            SM(tx_priority, MCI_CONCUR_TX_WLAN_WGHT2_MASK);
        ahp->ah_bt_coex_wlan_weight[3] &= ~MCI_CONCUR_TX_WLAN_WGHT3_MASK;
        ahp->ah_bt_coex_wlan_weight[3] |= 
            SM(tx_priority, MCI_CONCUR_TX_WLAN_WGHT3_MASK);
        ahp->ah_bt_coex_wlan_weight[3] &= ~MCI_CONCUR_TX_WLAN_WGHT3_MASK2;
        ahp->ah_bt_coex_wlan_weight[3] |= 
            SM(tx_priority, MCI_CONCUR_TX_WLAN_WGHT3_MASK2);
    }
//    if (ah->ah_config.ath_hal_mci_config & 
//        ATH_MCI_CONFIG_MCI_WEIGHT_DBG)
//    {
        HALDEBUG(ah, HAL_DEBUG_BT_COEX, 
                "(MCI) Set weights: 0x%08x 0x%08x 0x%08x 0x%08x\n",
                ahp->ah_bt_coex_wlan_weight[0],
                ahp->ah_bt_coex_wlan_weight[1],
                ahp->ah_bt_coex_wlan_weight[2],
                ahp->ah_bt_coex_wlan_weight[3]);
//    }
}

void ar9300_mci_bt_coex_disable(struct ath_hal *ah)
{
    struct ath_hal_9300 *ahp = AH9300(ah);

    HALDEBUG(ah, HAL_DEBUG_BT_COEX, 
        "(MCI) %s: Set weight to stomp none.\n", __func__);

    ar9300_mci_bt_coex_set_weights(ah, HAL_BT_COEX_STOMP_NONE);

    /* 
     * In Jupiter, when coex is disabled, we just set weight
     * table to be in favor of WLAN.
     */
    OS_REG_WRITE(ah, AR_BTCOEX_WL_WEIGHTS0, ahp->ah_bt_coex_wlan_weight[0]);
    OS_REG_WRITE(ah, AR_BTCOEX_WL_WEIGHTS1, ahp->ah_bt_coex_wlan_weight[1]);
    OS_REG_WRITE(ah, AR_BTCOEX_WL_WEIGHTS2, ahp->ah_bt_coex_wlan_weight[2]);
    OS_REG_WRITE(ah, AR_BTCOEX_WL_WEIGHTS3, ahp->ah_bt_coex_wlan_weight[3]);

    ahp->ah_bt_coex_enabled = AH_FALSE;
}

int ar9300_mci_bt_coex_enable(struct ath_hal *ah)
{
    struct ath_hal_9300 *ahp = AH9300(ah);

    HALDEBUG(ah, HAL_DEBUG_BT_COEX, "(MCI) %s: called\n", __func__);

    HALDEBUG(ah, HAL_DEBUG_BT_COEX, 
        "(MCI) Write weights: 0x%08x 0x%08x 0x%08x 0x%08x\n",
       ahp->ah_bt_coex_wlan_weight[0],
       ahp->ah_bt_coex_wlan_weight[1],
       ahp->ah_bt_coex_wlan_weight[2],
       ahp->ah_bt_coex_wlan_weight[3]);


    /* Mainly change the WLAN weight table */
    OS_REG_WRITE(ah, AR_BTCOEX_WL_WEIGHTS0, ahp->ah_bt_coex_wlan_weight[0]);
    OS_REG_WRITE(ah, AR_BTCOEX_WL_WEIGHTS1, ahp->ah_bt_coex_wlan_weight[1]);
    OS_REG_WRITE(ah, AR_BTCOEX_WL_WEIGHTS2, ahp->ah_bt_coex_wlan_weight[2]);
    OS_REG_WRITE(ah, AR_BTCOEX_WL_WEIGHTS3, ahp->ah_bt_coex_wlan_weight[3]);

    /* Send ACK even when BT has higher priority. */
    OS_REG_RMW_FIELD(ah, AR_QUIET1, AR_QUIET1_QUIET_ACK_CTS_ENABLE, 1);

    if (ahp->ah_bt_coex_flag & HAL_BT_COEX_FLAG_LOW_ACK_PWR) {
        OS_REG_WRITE(ah, AR_TPC, HAL_BT_COEX_LOW_ACK_POWER);
    }
    else {
        OS_REG_WRITE(ah, AR_TPC, HAL_BT_COEX_HIGH_ACK_POWER);
    }

    ahp->ah_bt_coex_enabled = AH_TRUE;

    return 0;
}

#endif /* ATH_SUPPORT_MCI */