Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
   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
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
/* Copyright (c) 2008-2009, Code Aurora Forum. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/mm.h>
#include <linux/fb.h>
#include <linux/init.h>
#include <linux/ioport.h>
#include <linux/device.h>
#include <linux/dma-mapping.h>

#include "msm_fb_panel.h"
#include "mddihost.h"
#include "mddihosti.h"

#define FEATURE_MDDI_UNDERRUN_RECOVERY
#ifndef FEATURE_MDDI_DISABLE_REVERSE
static void mddi_read_rev_packet(byte *data_ptr);
#endif

struct timer_list mddi_host_timer;

#define MDDI_DEFAULT_TIMER_LENGTH 5000	/* 5 seconds */
uint32 mddi_rtd_frequency = 60000;	/* send RTD every 60 seconds */
uint32 mddi_client_status_frequency = 60000;	/* get status pkt every 60 secs */

boolean mddi_vsync_detect_enabled = FALSE;
mddi_gpio_info_type mddi_gpio;

uint32 mddi_host_core_version;
boolean mddi_debug_log_statistics = FALSE;
/* #define FEATURE_MDDI_HOST_ENABLE_EARLY_HIBERNATION */
/* default to TRUE in case MDP does not vote */
static boolean mddi_host_mdp_active_flag = TRUE;
static uint32 mddi_log_stats_counter;
uint32 mddi_log_stats_frequency = 4000;

#define MDDI_DEFAULT_REV_PKT_SIZE            0x20

#ifndef FEATURE_MDDI_DISABLE_REVERSE
static boolean mddi_rev_ptr_workaround = TRUE;
static uint32 mddi_reg_read_retry;
static uint32 mddi_reg_read_retry_max = 20;
static boolean mddi_enable_reg_read_retry = TRUE;
static boolean mddi_enable_reg_read_retry_once = FALSE;

#define MDDI_MAX_REV_PKT_SIZE                0x60

#define MDDI_CLIENT_CAPABILITY_REV_PKT_SIZE  0x60

#define MDDI_VIDEO_REV_PKT_SIZE              0x40
#define MDDI_REV_BUFFER_SIZE  MDDI_MAX_REV_PKT_SIZE
static byte rev_packet_data[MDDI_MAX_REV_PKT_SIZE];
#endif /* FEATURE_MDDI_DISABLE_REVERSE */
/* leave these variables so graphics will compile */

#define MDDI_MAX_REV_DATA_SIZE  128
/*lint -d__align(x) */
boolean mddi_debug_clear_rev_data = TRUE;

uint32 *mddi_reg_read_value_ptr;

mddi_client_capability_type mddi_client_capability_pkt;
static boolean mddi_client_capability_request = FALSE;

#ifndef FEATURE_MDDI_DISABLE_REVERSE

#define MAX_MDDI_REV_HANDLERS 2
#define INVALID_PKT_TYPE 0xFFFF

typedef struct {
	mddi_rev_handler_type handler;	/* ISR to be executed */
	uint16 pkt_type;
} mddi_rev_pkt_handler_type;
static mddi_rev_pkt_handler_type mddi_rev_pkt_handler[MAX_MDDI_REV_HANDLERS] =
    { {NULL, INVALID_PKT_TYPE}, {NULL, INVALID_PKT_TYPE} };

static boolean mddi_rev_encap_user_request = FALSE;
static mddi_linked_list_notify_type mddi_rev_user;

spinlock_t mddi_host_spin_lock;
extern uint32 mdp_in_processing;
#endif

typedef enum {
	MDDI_REV_IDLE
#ifndef FEATURE_MDDI_DISABLE_REVERSE
	    , MDDI_REV_REG_READ_ISSUED,
	MDDI_REV_REG_READ_SENT,
	MDDI_REV_ENCAP_ISSUED,
	MDDI_REV_STATUS_REQ_ISSUED,
	MDDI_REV_CLIENT_CAP_ISSUED
#endif
} mddi_rev_link_state_type;

typedef enum {
	MDDI_LINK_DISABLED,
	MDDI_LINK_HIBERNATING,
	MDDI_LINK_ACTIVATING,
	MDDI_LINK_ACTIVE
} mddi_host_link_state_type;

typedef struct {
	uint32 count;
	uint32 in_count;
	uint32 disp_req_count;
	uint32 state_change_count;
	uint32 ll_done_count;
	uint32 rev_avail_count;
	uint32 error_count;
	uint32 rev_encap_count;
	uint32 llist_ptr_write_1;
	uint32 llist_ptr_write_2;
} mddi_host_int_type;

typedef struct {
	uint32 fwd_crc_count;
	uint32 rev_crc_count;
	uint32 pri_underflow;
	uint32 sec_underflow;
	uint32 rev_overflow;
	uint32 pri_overwrite;
	uint32 sec_overwrite;
	uint32 rev_overwrite;
	uint32 dma_failure;
	uint32 rtd_failure;
	uint32 reg_read_failure;
#ifdef FEATURE_MDDI_UNDERRUN_RECOVERY
	uint32 pri_underrun_detected;
#endif
} mddi_host_stat_type;

typedef struct {
	uint32 rtd_cnt;
	uint32 rev_enc_cnt;
	uint32 vid_cnt;
	uint32 reg_acc_cnt;
	uint32 cli_stat_cnt;
	uint32 cli_cap_cnt;
	uint32 reg_read_cnt;
	uint32 link_active_cnt;
	uint32 link_hibernate_cnt;
	uint32 vsync_response_cnt;
	uint32 fwd_crc_cnt;
	uint32 rev_crc_cnt;
} mddi_log_params_struct_type;

typedef struct {
	uint32 rtd_value;
	uint32 rtd_counter;
	uint32 client_status_cnt;
	boolean rev_ptr_written;
	uint8 *rev_ptr_start;
	uint8 *rev_ptr_curr;
	uint32 mddi_rev_ptr_write_val;
	dma_addr_t rev_data_dma_addr;
	uint16 rev_pkt_size;
	mddi_rev_link_state_type rev_state;
	mddi_host_link_state_type link_state;
	mddi_host_driver_state_type driver_state;
	boolean disable_hibernation;
	uint32 saved_int_reg;
	uint32 saved_int_en;
	mddi_linked_list_type *llist_ptr;
	dma_addr_t llist_dma_addr;
	mddi_linked_list_type *llist_dma_ptr;
	uint32 *rev_data_buf;
	struct completion mddi_llist_avail_comp;
	boolean mddi_waiting_for_llist_avail;
	mddi_host_int_type int_type;
	mddi_host_stat_type stats;
	mddi_log_params_struct_type log_parms;
	mddi_llist_info_type llist_info;
	mddi_linked_list_notify_type llist_notify[MDDI_MAX_NUM_LLIST_ITEMS];
} mddi_host_cntl_type;

static mddi_host_type mddi_curr_host = MDDI_HOST_PRIM;
static mddi_host_cntl_type mhctl[MDDI_NUM_HOST_CORES];
mddi_linked_list_type *llist_extern[MDDI_NUM_HOST_CORES];
mddi_linked_list_type *llist_dma_extern[MDDI_NUM_HOST_CORES];
mddi_linked_list_notify_type *llist_extern_notify[MDDI_NUM_HOST_CORES];
static mddi_log_params_struct_type prev_parms[MDDI_NUM_HOST_CORES];

extern uint32 mdp_total_vdopkts;

static boolean mddi_host_io_clock_on = FALSE;
static boolean mddi_host_hclk_on = FALSE;

int int_mddi_pri_flag = FALSE;
int int_mddi_ext_flag = FALSE;

static void mddi_report_errors(uint32 int_reg)
{
	mddi_host_type host_idx = mddi_curr_host;
	mddi_host_cntl_type *pmhctl = &(mhctl[host_idx]);

	if (int_reg & MDDI_INT_PRI_UNDERFLOW) {
		pmhctl->stats.pri_underflow++;
		MDDI_MSG_ERR("!!! MDDI Primary Underflow !!!\n");
	}
	if (int_reg & MDDI_INT_SEC_UNDERFLOW) {
		pmhctl->stats.sec_underflow++;
		MDDI_MSG_ERR("!!! MDDI Secondary Underflow !!!\n");
	}
#ifndef FEATURE_MDDI_DISABLE_REVERSE
	if (int_reg & MDDI_INT_REV_OVERFLOW) {
		pmhctl->stats.rev_overflow++;
		MDDI_MSG_ERR("!!! MDDI Reverse Overflow !!!\n");
		pmhctl->rev_ptr_curr = pmhctl->rev_ptr_start;
		mddi_host_reg_out(REV_PTR, pmhctl->mddi_rev_ptr_write_val);

	}
	if (int_reg & MDDI_INT_CRC_ERROR)
		MDDI_MSG_ERR("!!! MDDI Reverse CRC Error !!!\n");
#endif
	if (int_reg & MDDI_INT_PRI_OVERWRITE) {
		pmhctl->stats.pri_overwrite++;
		MDDI_MSG_ERR("!!! MDDI Primary Overwrite !!!\n");
	}
	if (int_reg & MDDI_INT_SEC_OVERWRITE) {
		pmhctl->stats.sec_overwrite++;
		MDDI_MSG_ERR("!!! MDDI Secondary Overwrite !!!\n");
	}
#ifndef FEATURE_MDDI_DISABLE_REVERSE
	if (int_reg & MDDI_INT_REV_OVERWRITE) {
		pmhctl->stats.rev_overwrite++;
		/* This will show up normally and is not a problem */
		MDDI_MSG_DEBUG("MDDI Reverse Overwrite!\n");
	}
	if (int_reg & MDDI_INT_RTD_FAILURE) {
		mddi_host_reg_outm(INTEN, MDDI_INT_RTD_FAILURE, 0);
		pmhctl->stats.rtd_failure++;
		MDDI_MSG_ERR("!!! MDDI RTD Failure !!!\n");
	}
#endif
	if (int_reg & MDDI_INT_DMA_FAILURE) {
		pmhctl->stats.dma_failure++;
		MDDI_MSG_ERR("!!! MDDI DMA Abort !!!\n");
	}
}

static void mddi_host_enable_io_clock(void)
{
	if (!MDDI_HOST_IS_IO_CLOCK_ON)
		MDDI_HOST_ENABLE_IO_CLOCK;
}

static void mddi_host_enable_hclk(void)
{

	if (!MDDI_HOST_IS_HCLK_ON)
		MDDI_HOST_ENABLE_HCLK;
}

static void mddi_host_disable_io_clock(void)
{
#ifndef FEATURE_MDDI_HOST_IO_CLOCK_CONTROL_DISABLE
	if (MDDI_HOST_IS_IO_CLOCK_ON)
		MDDI_HOST_DISABLE_IO_CLOCK;
#endif
}

static void mddi_host_disable_hclk(void)
{
#ifndef FEATURE_MDDI_HOST_HCLK_CONTROL_DISABLE
	if (MDDI_HOST_IS_HCLK_ON)
		MDDI_HOST_DISABLE_HCLK;
#endif
}

static void mddi_vote_to_sleep(mddi_host_type host_idx, boolean sleep)
{
	uint16 vote_mask;

	if (host_idx == MDDI_HOST_PRIM)
		vote_mask = 0x01;
	else
		vote_mask = 0x02;
}

static void mddi_report_state_change(uint32 int_reg)
{
	mddi_host_type host_idx = mddi_curr_host;
	mddi_host_cntl_type *pmhctl = &(mhctl[host_idx]);

	if ((pmhctl->saved_int_reg & MDDI_INT_IN_HIBERNATION) &&
	    (pmhctl->saved_int_reg & MDDI_INT_LINK_ACTIVE)) {
		/* recover from condition where the io_clock was turned off by the
		   clock driver during a transition to hibernation. The io_clock
		   disable is to prevent MDP/MDDI underruns when changing ARM
		   clock speeds. In the process of halting the ARM, the hclk
		   divider needs to be set to 1. When it is set to 1, there is
		   a small time (usecs) when hclk is off or slow, and this can
		   cause an underrun. To prevent the underrun, clock driver turns
		   off the MDDI io_clock before making the change. */
		mddi_host_reg_out(CMD, MDDI_CMD_POWERUP);
	}

	if (int_reg & MDDI_INT_LINK_ACTIVE) {
		pmhctl->link_state = MDDI_LINK_ACTIVE;
		pmhctl->log_parms.link_active_cnt++;
		pmhctl->rtd_value = mddi_host_reg_in(RTD_VAL);
		MDDI_MSG_DEBUG("!!! MDDI Active RTD:0x%x!!!\n",
			       pmhctl->rtd_value);
		/* now interrupt on hibernation */
		mddi_host_reg_outm(INTEN,
				   (MDDI_INT_IN_HIBERNATION |
				    MDDI_INT_LINK_ACTIVE),
				   MDDI_INT_IN_HIBERNATION);

#ifdef DEBUG_MDDIHOSTI
		/* if gpio interrupt is enabled, start polling at fastest
		 * registered rate
		 */
		if (mddi_gpio.polling_enabled) {
			timer_reg(&mddi_gpio_poll_timer,
		mddi_gpio_poll_timer_cb, 0, mddi_gpio.polling_interval, 0);
		}
#endif
#ifndef FEATURE_MDDI_DISABLE_REVERSE
		if (mddi_rev_ptr_workaround) {
			/* HW CR: need to reset reverse register stuff */
			pmhctl->rev_ptr_written = FALSE;
			pmhctl->rev_ptr_curr = pmhctl->rev_ptr_start;
		}
#endif
		/* vote on sleep */
		mddi_vote_to_sleep(host_idx, FALSE);

		if (host_idx == MDDI_HOST_PRIM) {
			if (mddi_vsync_detect_enabled) {
				/*
				 * Indicate to client specific code that vsync
				 * was enabled, but we did not detect a client
				 * intiated wakeup. The client specific
				 * handler can either reassert vsync detection,
				 * or treat this as a valid vsync.
				 */
				mddi_client_lcd_vsync_detected(FALSE);
				pmhctl->log_parms.vsync_response_cnt++;
			}
		}
	}
	if (int_reg & MDDI_INT_IN_HIBERNATION) {
		pmhctl->link_state = MDDI_LINK_HIBERNATING;
		pmhctl->log_parms.link_hibernate_cnt++;
		MDDI_MSG_DEBUG("!!! MDDI Hibernating !!!\n");
		/* now interrupt on link_active */
#ifdef FEATURE_MDDI_DISABLE_REVERSE
		mddi_host_reg_outm(INTEN,
				   (MDDI_INT_MDDI_IN |
				    MDDI_INT_IN_HIBERNATION |
				    MDDI_INT_LINK_ACTIVE),
				   MDDI_INT_LINK_ACTIVE);
#else
		mddi_host_reg_outm(INTEN,
				   (MDDI_INT_MDDI_IN |
				    MDDI_INT_IN_HIBERNATION |
				    MDDI_INT_LINK_ACTIVE),
				   (MDDI_INT_MDDI_IN | MDDI_INT_LINK_ACTIVE));

		pmhctl->rtd_counter = mddi_rtd_frequency;

		if (pmhctl->rev_state != MDDI_REV_IDLE) {
			/* a rev_encap will not wake up the link, so we do that here */
			pmhctl->link_state = MDDI_LINK_ACTIVATING;
			mddi_host_reg_out(CMD, MDDI_CMD_LINK_ACTIVE);
		}
#endif

		if (pmhctl->disable_hibernation) {
			mddi_host_reg_out(CMD, MDDI_CMD_HIBERNATE);
			mddi_host_reg_out(CMD, MDDI_CMD_LINK_ACTIVE);
			pmhctl->link_state = MDDI_LINK_ACTIVATING;
		}
#ifdef FEATURE_MDDI_UNDERRUN_RECOVERY
		if ((pmhctl->llist_info.transmitting_start_idx !=
		     UNASSIGNED_INDEX)
		    &&
		    ((pmhctl->
		      saved_int_reg & (MDDI_INT_PRI_LINK_LIST_DONE |
				       MDDI_INT_PRI_PTR_READ)) ==
		     MDDI_INT_PRI_PTR_READ)) {
			mddi_linked_list_type *llist_dma;
			llist_dma = pmhctl->llist_dma_ptr;
			/*
			 * All indications are that we have not received a
			 * linked list done interrupt, due to an underrun
			 * condition. Recovery attempt is to send again.
			 */
			dma_coherent_pre_ops();
			/* Write to primary pointer register again */
			mddi_host_reg_out(PRI_PTR,
					  &llist_dma[pmhctl->llist_info.
						     transmitting_start_idx]);
			pmhctl->stats.pri_underrun_detected++;
		}
#endif

		/* vote on sleep */
		if (pmhctl->link_state == MDDI_LINK_HIBERNATING) {
			mddi_vote_to_sleep(host_idx, TRUE);
		}

#ifdef DEBUG_MDDIHOSTI
		/* need to stop polling timer */
		if (mddi_gpio.polling_enabled) {
			(void) timer_clr(&mddi_gpio_poll_timer, T_NONE);
		}
#endif
	}
}

void mddi_host_timer_service(unsigned long data)
{
#ifndef FEATURE_MDDI_DISABLE_REVERSE
	unsigned long flags;
#endif
	mddi_host_type host_idx;
	mddi_host_cntl_type *pmhctl;

	unsigned long time_ms = MDDI_DEFAULT_TIMER_LENGTH;
	init_timer(&mddi_host_timer);
	mddi_host_timer.function = mddi_host_timer_service;
	mddi_host_timer.data = 0;

	mddi_host_timer.expires = jiffies + ((time_ms * HZ) / 1000);
	add_timer(&mddi_host_timer);

	for (host_idx = MDDI_HOST_PRIM; host_idx < MDDI_NUM_HOST_CORES;
	     host_idx++) {
		pmhctl = &(mhctl[host_idx]);
		mddi_log_stats_counter += (uint32) time_ms;
#ifndef FEATURE_MDDI_DISABLE_REVERSE
		pmhctl->rtd_counter += (uint32) time_ms;
		pmhctl->client_status_cnt += (uint32) time_ms;

		if (host_idx == MDDI_HOST_PRIM) {
			if (pmhctl->client_status_cnt >=
			    mddi_client_status_frequency) {
				if ((pmhctl->link_state ==
				     MDDI_LINK_HIBERNATING)
				    && (pmhctl->client_status_cnt >
					mddi_client_status_frequency)) {
					/*
					 * special case where we are hibernating
					 * and mddi_host_isr is not firing, so
					 * kick the link so that the status can
					 * be retrieved
					 */

					/* need to wake up link before issuing
					 * rev encap command
					 */
					MDDI_MSG_INFO("wake up link!\n");
					spin_lock_irqsave(&mddi_host_spin_lock,
							  flags);
					mddi_host_enable_hclk();
					mddi_host_enable_io_clock();
					pmhctl->link_state =
					    MDDI_LINK_ACTIVATING;
					mddi_host_reg_out(CMD,
							  MDDI_CMD_LINK_ACTIVE);
					spin_unlock_irqrestore
					    (&mddi_host_spin_lock, flags);
				} else
				    if ((pmhctl->link_state == MDDI_LINK_ACTIVE)
					&& pmhctl->disable_hibernation) {
					/*
					 * special case where we have disabled
					 * hibernation and mddi_host_isr
					 * is not firing, so enable interrupt
					 * for no pkts pending, which will
					 * generate an interrupt
					 */
					MDDI_MSG_INFO("kick isr!\n");
					spin_lock_irqsave(&mddi_host_spin_lock,
							  flags);
					mddi_host_enable_hclk();
					mddi_host_reg_outm(INTEN,
							   MDDI_INT_NO_CMD_PKTS_PEND,
							   MDDI_INT_NO_CMD_PKTS_PEND);
					spin_unlock_irqrestore
					    (&mddi_host_spin_lock, flags);
				}
			}
		}
#endif /* #ifndef FEATURE_MDDI_DISABLE_REVERSE */
	}

	/* Check if logging is turned on */
	for (host_idx = MDDI_HOST_PRIM; host_idx < MDDI_NUM_HOST_CORES;
	     host_idx++) {
		mddi_log_params_struct_type *prev_ptr = &(prev_parms[host_idx]);
		pmhctl = &(mhctl[host_idx]);

		if (mddi_debug_log_statistics) {

			/* get video pkt count from MDP, since MDDI sw cannot know this */
			pmhctl->log_parms.vid_cnt = mdp_total_vdopkts;

			if (mddi_log_stats_counter >= mddi_log_stats_frequency) {
				/* mddi_log_stats_counter = 0; */
				if (mddi_debug_log_statistics) {
					MDDI_MSG_NOTICE
					    ("MDDI Statistics since last report:\n");
					MDDI_MSG_NOTICE("  Packets sent:\n");
					MDDI_MSG_NOTICE
					    ("    %d RTD packet(s)\n",
					     pmhctl->log_parms.rtd_cnt -
					     prev_ptr->rtd_cnt);
					if (prev_ptr->rtd_cnt !=
					    pmhctl->log_parms.rtd_cnt) {
						unsigned long flags;
						spin_lock_irqsave
						    (&mddi_host_spin_lock,
						     flags);
						mddi_host_enable_hclk();
						pmhctl->rtd_value =
						    mddi_host_reg_in(RTD_VAL);
						spin_unlock_irqrestore
						    (&mddi_host_spin_lock,
						     flags);
						MDDI_MSG_NOTICE
						    ("      RTD value=%d\n",
						     pmhctl->rtd_value);
					}
					MDDI_MSG_NOTICE
					    ("    %d VIDEO packets\n",
					     pmhctl->log_parms.vid_cnt -
					     prev_ptr->vid_cnt);
					MDDI_MSG_NOTICE
					    ("    %d Register Access packets\n",
					     pmhctl->log_parms.reg_acc_cnt -
					     prev_ptr->reg_acc_cnt);
					MDDI_MSG_NOTICE
					    ("    %d Reverse Encapsulation packet(s)\n",
					     pmhctl->log_parms.rev_enc_cnt -
					     prev_ptr->rev_enc_cnt);
					if (prev_ptr->rev_enc_cnt !=
					    pmhctl->log_parms.rev_enc_cnt) {
						/* report # of reverse CRC errors */
						MDDI_MSG_NOTICE
						    ("      %d reverse CRC errors detected\n",
						     pmhctl->log_parms.
						     rev_crc_cnt -
						     prev_ptr->rev_crc_cnt);
					}
					MDDI_MSG_NOTICE
					    ("  Packets received:\n");
					MDDI_MSG_NOTICE
					    ("    %d Client Status packets",
					     pmhctl->log_parms.cli_stat_cnt -
					     prev_ptr->cli_stat_cnt);
					if (prev_ptr->cli_stat_cnt !=
					    pmhctl->log_parms.cli_stat_cnt) {
						MDDI_MSG_NOTICE
						    ("      %d forward CRC errors reported\n",
						     pmhctl->log_parms.
						     fwd_crc_cnt -
						     prev_ptr->fwd_crc_cnt);
					}
					MDDI_MSG_NOTICE
					    ("    %d Register Access Read packets\n",
					     pmhctl->log_parms.reg_read_cnt -
					     prev_ptr->reg_read_cnt);

					if (pmhctl->link_state ==
					    MDDI_LINK_ACTIVE) {
						MDDI_MSG_NOTICE
						    ("  Current Link Status: Active\n");
					} else
					    if ((pmhctl->link_state ==
						 MDDI_LINK_HIBERNATING)
						|| (pmhctl->link_state ==
						    MDDI_LINK_ACTIVATING)) {
						MDDI_MSG_NOTICE
						    ("  Current Link Status: Hibernation\n");
					} else {
						MDDI_MSG_NOTICE
						    ("  Current Link Status: Inactive\n");
					}
					MDDI_MSG_NOTICE
					    ("    Active state entered %d times\n",
					     pmhctl->log_parms.link_active_cnt -
					     prev_ptr->link_active_cnt);
					MDDI_MSG_NOTICE
					    ("    Hibernation state entered %d times\n",
					     pmhctl->log_parms.
					     link_hibernate_cnt -
					     prev_ptr->link_hibernate_cnt);
				}
			}
			prev_parms[host_idx] = pmhctl->log_parms;
		}
	}
	if (mddi_log_stats_counter >= mddi_log_stats_frequency)
		mddi_log_stats_counter = 0;

	return;
}				/* mddi_host_timer_cb */

static void mddi_process_link_list_done(void)
{
	mddi_host_type host_idx = mddi_curr_host;
	mddi_host_cntl_type *pmhctl = &(mhctl[host_idx]);

	/* normal forward linked list packet(s) were sent */
	if (pmhctl->llist_info.transmitting_start_idx == UNASSIGNED_INDEX) {
		MDDI_MSG_ERR("**** getting LL done, but no list ****\n");
	} else {
		uint16 idx;

#ifndef FEATURE_MDDI_DISABLE_REVERSE
		if (pmhctl->rev_state == MDDI_REV_REG_READ_ISSUED) {
			/* special case where a register read packet was sent */
			pmhctl->rev_state = MDDI_REV_REG_READ_SENT;
			if (pmhctl->llist_info.reg_read_idx == UNASSIGNED_INDEX) {
				MDDI_MSG_ERR
				    ("**** getting LL done, but no list ****\n");
			}
		}
#endif
		for (idx = pmhctl->llist_info.transmitting_start_idx;;) {
			uint16 next_idx = pmhctl->llist_notify[idx].next_idx;
			/* with reg read we don't release the waiting tcb until after
			 * the reverse encapsulation has completed.
			 */
			if (idx != pmhctl->llist_info.reg_read_idx) {
				/* notify task that may be waiting on this completion */
				if (pmhctl->llist_notify[idx].waiting) {
					complete(&
						 (pmhctl->llist_notify[idx].
						  done_comp));
				}
				if (pmhctl->llist_notify[idx].done_cb != NULL) {
					(*(pmhctl->llist_notify[idx].done_cb))
					    ();
				}

				pmhctl->llist_notify[idx].in_use = FALSE;
				pmhctl->llist_notify[idx].waiting = FALSE;
				pmhctl->llist_notify[idx].done_cb = NULL;
				if (idx < MDDI_NUM_DYNAMIC_LLIST_ITEMS) {
					/* static LLIST items are configured only once */
					pmhctl->llist_notify[idx].next_idx =
					    UNASSIGNED_INDEX;
				}
				/*
				 * currently, all linked list packets are
				 * register access, so we can increment the
				 * counter for that packet type here.
				 */
				pmhctl->log_parms.reg_acc_cnt++;
			}
			if (idx == pmhctl->llist_info.transmitting_end_idx)
				break;
			idx = next_idx;
			if (idx == UNASSIGNED_INDEX)
				MDDI_MSG_CRIT("MDDI linked list corruption!\n");
		}

		pmhctl->llist_info.transmitting_start_idx = UNASSIGNED_INDEX;
		pmhctl->llist_info.transmitting_end_idx = UNASSIGNED_INDEX;

		if (pmhctl->mddi_waiting_for_llist_avail) {
			if (!
			    (pmhctl->
			     llist_notify[pmhctl->llist_info.next_free_idx].
			     in_use)) {
				pmhctl->mddi_waiting_for_llist_avail = FALSE;
				complete(&(pmhctl->mddi_llist_avail_comp));
			}
		}
	}

	/* Turn off MDDI_INT_PRI_LINK_LIST_DONE interrupt */
	mddi_host_reg_outm(INTEN, MDDI_INT_PRI_LINK_LIST_DONE, 0);

}

static void mddi_queue_forward_linked_list(void)
{
	uint16 first_pkt_index;
	mddi_linked_list_type *llist_dma;
	mddi_host_type host_idx = mddi_curr_host;
	mddi_host_cntl_type *pmhctl = &(mhctl[host_idx]);
	llist_dma = pmhctl->llist_dma_ptr;

	first_pkt_index = UNASSIGNED_INDEX;

	if (pmhctl->llist_info.transmitting_start_idx == UNASSIGNED_INDEX) {
#ifndef FEATURE_MDDI_DISABLE_REVERSE
		if (pmhctl->llist_info.reg_read_waiting) {
			if (pmhctl->rev_state == MDDI_REV_IDLE) {
				/*
				 * we have a register read to send and
				 * can send it now
				 */
				pmhctl->rev_state = MDDI_REV_REG_READ_ISSUED;
				mddi_reg_read_retry = 0;
				first_pkt_index =
				    pmhctl->llist_info.waiting_start_idx;
				pmhctl->llist_info.reg_read_waiting = FALSE;
			}
		} else
#endif
		{
			/*
			 * not register read to worry about, go ahead and write
			 * anything that may be on the waiting list.
			 */
			first_pkt_index = pmhctl->llist_info.waiting_start_idx;
		}
	}

	if (first_pkt_index != UNASSIGNED_INDEX) {
		pmhctl->llist_info.transmitting_start_idx =
		    pmhctl->llist_info.waiting_start_idx;
		pmhctl->llist_info.transmitting_end_idx =
		    pmhctl->llist_info.waiting_end_idx;
		pmhctl->llist_info.waiting_start_idx = UNASSIGNED_INDEX;
		pmhctl->llist_info.waiting_end_idx = UNASSIGNED_INDEX;

		/* write to the primary pointer register */
		MDDI_MSG_DEBUG("MDDI writing primary ptr with idx=%d\n",
			       first_pkt_index);

		pmhctl->int_type.llist_ptr_write_2++;

		dma_coherent_pre_ops();
		mddi_host_reg_out(PRI_PTR, &llist_dma[first_pkt_index]);

		/* enable interrupt when complete */
		mddi_host_reg_outm(INTEN, MDDI_INT_PRI_LINK_LIST_DONE,
				   MDDI_INT_PRI_LINK_LIST_DONE);

	}

}

#ifndef FEATURE_MDDI_DISABLE_REVERSE
static void mddi_read_rev_packet(byte *data_ptr)
{
	uint16 i, length;
	mddi_host_type host_idx = mddi_curr_host;
	mddi_host_cntl_type *pmhctl = &(mhctl[host_idx]);

	uint8 *rev_ptr_overflow =
	    (pmhctl->rev_ptr_start + MDDI_REV_BUFFER_SIZE);

	/* first determine the length and handle invalid lengths */
	length = *pmhctl->rev_ptr_curr++;
	if (pmhctl->rev_ptr_curr >= rev_ptr_overflow)
		pmhctl->rev_ptr_curr = pmhctl->rev_ptr_start;
	length |= ((*pmhctl->rev_ptr_curr++) << 8);
	if (pmhctl->rev_ptr_curr >= rev_ptr_overflow)
		pmhctl->rev_ptr_curr = pmhctl->rev_ptr_start;
	if (length > (pmhctl->rev_pkt_size - 2)) {
		MDDI_MSG_ERR("Invalid rev pkt length %d\n", length);
		/* rev_pkt_size should always be <= rev_ptr_size so limit to packet size */
		length = pmhctl->rev_pkt_size - 2;
	}

	/* If the data pointer is NULL, just increment the pmhctl->rev_ptr_curr.
	 * Loop around if necessary. Don't bother reading the data.
	 */
	if (data_ptr == NULL) {
		pmhctl->rev_ptr_curr += length;
		if (pmhctl->rev_ptr_curr >= rev_ptr_overflow)
			pmhctl->rev_ptr_curr -= MDDI_REV_BUFFER_SIZE;
		return;
	}

	data_ptr[0] = length & 0x0ff;
	data_ptr[1] = length >> 8;
	data_ptr += 2;
	/* copy the data to data_ptr byte-at-a-time */
	for (i = 0; (i < length) && (pmhctl->rev_ptr_curr < rev_ptr_overflow);
	     i++)
		*data_ptr++ = *pmhctl->rev_ptr_curr++;
	if (pmhctl->rev_ptr_curr >= rev_ptr_overflow)
		pmhctl->rev_ptr_curr = pmhctl->rev_ptr_start;
	for (; (i < length) && (pmhctl->rev_ptr_curr < rev_ptr_overflow); i++)
		*data_ptr++ = *pmhctl->rev_ptr_curr++;
}

static void mddi_process_rev_packets(void)
{
	uint32 rev_packet_count;
	word i;
	uint32 crc_errors;
	boolean mddi_reg_read_successful = FALSE;
	mddi_host_type host_idx = mddi_curr_host;
	mddi_host_cntl_type *pmhctl = &(mhctl[host_idx]);

	pmhctl->log_parms.rev_enc_cnt++;
	if ((pmhctl->rev_state != MDDI_REV_ENCAP_ISSUED) &&
	    (pmhctl->rev_state != MDDI_REV_STATUS_REQ_ISSUED) &&
	    (pmhctl->rev_state != MDDI_REV_CLIENT_CAP_ISSUED)) {
		MDDI_MSG_ERR("Wrong state %d for reverse int\n",
			     pmhctl->rev_state);
	}
	/* Turn off MDDI_INT_REV_AVAIL interrupt */
	mddi_host_reg_outm(INTEN, MDDI_INT_REV_DATA_AVAIL, 0);

	/* Clear rev data avail int */
	mddi_host_reg_out(INT, MDDI_INT_REV_DATA_AVAIL);

	/* Get Number of packets */
	rev_packet_count = mddi_host_reg_in(REV_PKT_CNT);

#ifndef T_MSM7500
	/* Clear out rev packet counter */
	mddi_host_reg_out(REV_PKT_CNT, 0x0000);
#endif

#if defined(CONFIG_FB_MSM_MDP31) || defined(CONFIG_FB_MSM_MDP40)
	if ((pmhctl->rev_state == MDDI_REV_CLIENT_CAP_ISSUED) &&
	    (rev_packet_count > 0) &&
	    (mddi_host_core_version == 0x28 ||
	     mddi_host_core_version == 0x30)) {

		uint32 int_reg;
		uint32 max_count = 0;

		mddi_host_reg_out(REV_PTR, pmhctl->mddi_rev_ptr_write_val);
		int_reg = mddi_host_reg_in(INT);
		while ((int_reg & 0x100000) == 0) {
			udelay(3);
			int_reg = mddi_host_reg_in(INT);
			if (++max_count > 100)
				break;
		}
	}
#endif

	/* Get CRC error count */
	crc_errors = mddi_host_reg_in(REV_CRC_ERR);
	if (crc_errors != 0) {
		pmhctl->log_parms.rev_crc_cnt += crc_errors;
		pmhctl->stats.rev_crc_count += crc_errors;
		MDDI_MSG_ERR("!!! MDDI %d Reverse CRC Error(s) !!!\n",
			     crc_errors);
#ifndef T_MSM7500
		/* Clear CRC error count */
		mddi_host_reg_out(REV_CRC_ERR, 0x0000);
#endif
		/* also issue an RTD to attempt recovery */
		pmhctl->rtd_counter = mddi_rtd_frequency;
	}

	pmhctl->rtd_value = mddi_host_reg_in(RTD_VAL);

	MDDI_MSG_DEBUG("MDDI rev pkt cnt=%d, ptr=0x%x, RTD:0x%x\n",
		       rev_packet_count,
		       pmhctl->rev_ptr_curr - pmhctl->rev_ptr_start,
		       pmhctl->rtd_value);

	if (rev_packet_count >= 1) {
		mddi_invalidate_cache_lines((uint32 *) pmhctl->rev_ptr_start,
					    MDDI_REV_BUFFER_SIZE);
	}
	/* order the reads */
	dma_coherent_post_ops();
	for (i = 0; i < rev_packet_count; i++) {
		mddi_rev_packet_type *rev_pkt_ptr;

		mddi_read_rev_packet(rev_packet_data);

		rev_pkt_ptr = (mddi_rev_packet_type *) rev_packet_data;

		if (rev_pkt_ptr->packet_length > pmhctl->rev_pkt_size) {
			MDDI_MSG_ERR("!!!invalid packet size: %d\n",
				     rev_pkt_ptr->packet_length);
		}

		MDDI_MSG_DEBUG("MDDI rev pkt 0x%x size 0x%x\n",
			       rev_pkt_ptr->packet_type,
			       rev_pkt_ptr->packet_length);

		/* Do whatever you want to do with the data based on the packet type */
		switch (rev_pkt_ptr->packet_type) {
		case 66:	/* Client Capability */
			{
				mddi_client_capability_type
				    *client_capability_pkt_ptr;

				client_capability_pkt_ptr =
				    (mddi_client_capability_type *)
				    rev_packet_data;
				MDDI_MSG_NOTICE
				    ("Client Capability: Week=%d, Year=%d\n",
				     client_capability_pkt_ptr->
				     Week_of_Manufacture,
				     client_capability_pkt_ptr->
				     Year_of_Manufacture);
				memcpy((void *)&mddi_client_capability_pkt,
				       (void *)rev_packet_data,
				       sizeof(mddi_client_capability_type));
				pmhctl->log_parms.cli_cap_cnt++;
			}
			break;

		case 70:	/* Display Status */
			{
				mddi_client_status_type *client_status_pkt_ptr;

				client_status_pkt_ptr =
				    (mddi_client_status_type *) rev_packet_data;
				if ((client_status_pkt_ptr->crc_error_count !=
				     0)
				    || (client_status_pkt_ptr->
					reverse_link_request != 0)) {
					MDDI_MSG_ERR
					    ("Client Status: RevReq=%d, CrcErr=%d\n",
					     client_status_pkt_ptr->
					     reverse_link_request,
					     client_status_pkt_ptr->
					     crc_error_count);
				} else {
					MDDI_MSG_DEBUG
					    ("Client Status: RevReq=%d, CrcErr=%d\n",
					     client_status_pkt_ptr->
					     reverse_link_request,
					     client_status_pkt_ptr->
					     crc_error_count);
				}
				pmhctl->log_parms.fwd_crc_cnt +=
				    client_status_pkt_ptr->crc_error_count;
				pmhctl->stats.fwd_crc_count +=
				    client_status_pkt_ptr->crc_error_count;
				pmhctl->log_parms.cli_stat_cnt++;
			}
			break;

		case 146:	/* register access packet */
			{
				mddi_register_access_packet_type
				    * regacc_pkt_ptr;

				regacc_pkt_ptr =
				    (mddi_register_access_packet_type *)
				    rev_packet_data;

				MDDI_MSG_DEBUG
				    ("Reg Acc parse reg=0x%x, value=0x%x\n",
				     regacc_pkt_ptr->register_address,
				     regacc_pkt_ptr->register_data_list);

				/* Copy register value to location passed in */
				if (mddi_reg_read_value_ptr) {
#if defined(T_MSM6280) && !defined(T_MSM7200)
					/* only least significant 16 bits are valid with 6280 */
					*mddi_reg_read_value_ptr =
					    regacc_pkt_ptr->
					    register_data_list & 0x0000ffff;
#else
					*mddi_reg_read_value_ptr =
					    regacc_pkt_ptr->register_data_list;
#endif
					mddi_reg_read_successful = TRUE;
					mddi_reg_read_value_ptr = NULL;
				}

#ifdef DEBUG_MDDIHOSTI
				if ((mddi_gpio.polling_enabled) &&
				    (regacc_pkt_ptr->register_address ==
				     mddi_gpio.polling_reg)) {
					/*
					 * ToDo: need to call Linux GPIO call
					 * here...
					 */
					 mddi_client_lcd_gpio_poll(
					 regacc_pkt_ptr->register_data_list);
				}
#endif
				pmhctl->log_parms.reg_read_cnt++;
			}
			break;

		default:	/* any other packet */
			{
				uint16 hdlr;

				for (hdlr = 0; hdlr < MAX_MDDI_REV_HANDLERS;
				     hdlr++) {
					if (mddi_rev_pkt_handler[hdlr].
					    pkt_type ==
					    rev_pkt_ptr->packet_type) {
						(*
						 (mddi_rev_pkt_handler[hdlr].
						  handler)) (rev_pkt_ptr);
					/* pmhctl->rev_state = MDDI_REV_IDLE; */
						break;
					}
				}
				if (hdlr >= MAX_MDDI_REV_HANDLERS)
					MDDI_MSG_ERR("MDDI unknown rev pkt\n");
			}
			break;
		}
	}
	if ((pmhctl->rev_ptr_curr + pmhctl->rev_pkt_size) >=
	    (pmhctl->rev_ptr_start + MDDI_REV_BUFFER_SIZE)) {
		pmhctl->rev_ptr_written = FALSE;
	}

	if (pmhctl->rev_state == MDDI_REV_ENCAP_ISSUED) {
		pmhctl->rev_state = MDDI_REV_IDLE;
		if (mddi_rev_user.waiting) {
			mddi_rev_user.waiting = FALSE;
			complete(&(mddi_rev_user.done_comp));
		} else if (pmhctl->llist_info.reg_read_idx == UNASSIGNED_INDEX) {
			MDDI_MSG_ERR
			    ("Reverse Encap state, but no reg read in progress\n");
		} else {
			if ((!mddi_reg_read_successful) &&
			    (mddi_reg_read_retry < mddi_reg_read_retry_max) &&
			    (mddi_enable_reg_read_retry)) {
				/*
				 * There is a race condition that can happen
				 * where the reverse encapsulation message is
				 * sent out by the MDDI host before the register
				 * read packet is sent. As a work-around for
				 * that problem we issue the reverse
				 * encapsulation one more time before giving up.
				 */
				if (mddi_enable_reg_read_retry_once)
					mddi_reg_read_retry =
					    mddi_reg_read_retry_max;
				pmhctl->rev_state = MDDI_REV_REG_READ_SENT;
				pmhctl->stats.reg_read_failure++;
			} else {
				uint16 reg_read_idx =
				    pmhctl->llist_info.reg_read_idx;

				mddi_reg_read_retry = 0;
				if (pmhctl->llist_notify[reg_read_idx].waiting) {
					complete(&
						 (pmhctl->
						  llist_notify[reg_read_idx].
						  done_comp));
				}
				pmhctl->llist_info.reg_read_idx =
				    UNASSIGNED_INDEX;
				if (pmhctl->llist_notify[reg_read_idx].
				    done_cb != NULL) {
					(*
					 (pmhctl->llist_notify[reg_read_idx].
					  done_cb)) ();
				}
				pmhctl->llist_notify[reg_read_idx].next_idx =
				    UNASSIGNED_INDEX;
				pmhctl->llist_notify[reg_read_idx].in_use =
				    FALSE;
				pmhctl->llist_notify[reg_read_idx].waiting =
				    FALSE;
				pmhctl->llist_notify[reg_read_idx].done_cb =
				    NULL;
				if (!mddi_reg_read_successful)
					pmhctl->stats.reg_read_failure++;
			}
		}
	} else if (pmhctl->rev_state == MDDI_REV_CLIENT_CAP_ISSUED) {
#if defined(CONFIG_FB_MSM_MDP31) || defined(CONFIG_FB_MSM_MDP40)
		if (mddi_host_core_version == 0x28 ||
		    mddi_host_core_version == 0x30) {
			mddi_host_reg_out(FIFO_ALLOC, 0x00);
			pmhctl->rev_ptr_written = TRUE;
			mddi_host_reg_out(REV_PTR,
				pmhctl->mddi_rev_ptr_write_val);
			pmhctl->rev_ptr_curr = pmhctl->rev_ptr_start;
			mddi_host_reg_out(CMD, 0xC00);
		}
#endif

		if (mddi_rev_user.waiting) {
			mddi_rev_user.waiting = FALSE;
			complete(&(mddi_rev_user.done_comp));
		}
		pmhctl->rev_state = MDDI_REV_IDLE;
	} else {
		pmhctl->rev_state = MDDI_REV_IDLE;
	}

	/* pmhctl->rev_state = MDDI_REV_IDLE; */

	/* Re-enable interrupt */
	mddi_host_reg_outm(INTEN, MDDI_INT_REV_DATA_AVAIL,
			   MDDI_INT_REV_DATA_AVAIL);

}

static void mddi_issue_reverse_encapsulation(void)
{
	mddi_host_type host_idx = mddi_curr_host;
	mddi_host_cntl_type *pmhctl = &(mhctl[host_idx]);
	/* Only issue a reverse encapsulation packet if:
	 * 1) another reverse is not in progress (MDDI_REV_IDLE).
	 * 2) a register read has been sent (MDDI_REV_REG_READ_SENT).
	 * 3) forward is not in progress, because of a hw bug in client that
	 *    causes forward crc errors on packet immediately after rev encap.
	 */
	if (((pmhctl->rev_state == MDDI_REV_IDLE) ||
	     (pmhctl->rev_state == MDDI_REV_REG_READ_SENT)) &&
	    (pmhctl->llist_info.transmitting_start_idx == UNASSIGNED_INDEX) &&
	    (!mdp_in_processing)) {
		uint32 mddi_command = MDDI_CMD_SEND_REV_ENCAP;

		if ((pmhctl->rev_state == MDDI_REV_REG_READ_SENT) ||
		    (mddi_rev_encap_user_request == TRUE)) {
			mddi_host_enable_io_clock();
			if (pmhctl->link_state == MDDI_LINK_HIBERNATING) {
				/* need to wake up link before issuing rev encap command */
				MDDI_MSG_DEBUG("wake up link!\n");
				pmhctl->link_state = MDDI_LINK_ACTIVATING;
				mddi_host_reg_out(CMD, MDDI_CMD_LINK_ACTIVE);
			} else {
				if (pmhctl->rtd_counter >= mddi_rtd_frequency) {
					MDDI_MSG_DEBUG
					    ("mddi sending RTD command!\n");
					mddi_host_reg_out(CMD,
							  MDDI_CMD_SEND_RTD);
					pmhctl->rtd_counter = 0;
					pmhctl->log_parms.rtd_cnt++;
				}
				if (pmhctl->rev_state != MDDI_REV_REG_READ_SENT) {
					/* this is generic reverse request by user, so
					 * reset the waiting flag. */
					mddi_rev_encap_user_request = FALSE;
				}
				/* link is active so send reverse encap to get register read results */
				pmhctl->rev_state = MDDI_REV_ENCAP_ISSUED;
				mddi_command = MDDI_CMD_SEND_REV_ENCAP;
				MDDI_MSG_DEBUG("sending rev encap!\n");
			}
		} else
		    if ((pmhctl->client_status_cnt >=
			 mddi_client_status_frequency)
			|| mddi_client_capability_request) {
			mddi_host_enable_io_clock();
			if (pmhctl->link_state == MDDI_LINK_HIBERNATING) {
				/* only wake up the link if it client status is overdue */
				if ((pmhctl->client_status_cnt >=
				     (mddi_client_status_frequency * 2))
				    || mddi_client_capability_request) {
					/* need to wake up link before issuing rev encap command */
					MDDI_MSG_DEBUG("wake up link!\n");
					pmhctl->link_state =
					    MDDI_LINK_ACTIVATING;
					mddi_host_reg_out(CMD,
							  MDDI_CMD_LINK_ACTIVE);
				}
			} else {
				if (pmhctl->rtd_counter >= mddi_rtd_frequency) {
					MDDI_MSG_DEBUG
					    ("mddi sending RTD command!\n");
					mddi_host_reg_out(CMD,
							  MDDI_CMD_SEND_RTD);
					pmhctl->rtd_counter = 0;
					pmhctl->log_parms.rtd_cnt++;
				}
				/* periodically get client status */
				MDDI_MSG_DEBUG
				    ("mddi sending rev enc! (get status)\n");
				if (mddi_client_capability_request) {
					pmhctl->rev_state =
					    MDDI_REV_CLIENT_CAP_ISSUED;
					mddi_command = MDDI_CMD_GET_CLIENT_CAP;
					mddi_client_capability_request = FALSE;
				} else {
					pmhctl->rev_state =
					    MDDI_REV_STATUS_REQ_ISSUED;
					pmhctl->client_status_cnt = 0;
					mddi_command =
					    MDDI_CMD_GET_CLIENT_STATUS;
				}
			}
		}
		if ((pmhctl->rev_state == MDDI_REV_ENCAP_ISSUED) ||
		    (pmhctl->rev_state == MDDI_REV_STATUS_REQ_ISSUED) ||
		    (pmhctl->rev_state == MDDI_REV_CLIENT_CAP_ISSUED)) {
			pmhctl->int_type.rev_encap_count++;
#if defined(T_MSM6280) && !defined(T_MSM7200)
			mddi_rev_pointer_written = TRUE;
			mddi_host_reg_out(REV_PTR, mddi_rev_ptr_write_val);
			mddi_rev_ptr_curr = mddi_rev_ptr_start;
			/* force new rev ptr command */
			mddi_host_reg_out(CMD, 0xC00);
#else
			if (!pmhctl->rev_ptr_written) {
				MDDI_MSG_DEBUG("writing reverse pointer!\n");
				pmhctl->rev_ptr_written = TRUE;
#if defined(CONFIG_FB_MSM_MDP31) || defined(CONFIG_FB_MSM_MDP40)
				if ((pmhctl->rev_state ==
				     MDDI_REV_CLIENT_CAP_ISSUED) &&
				    (mddi_host_core_version == 0x28 ||
				     mddi_host_core_version == 0x30)) {
					pmhctl->rev_ptr_written = FALSE;
					mddi_host_reg_out(FIFO_ALLOC, 0x02);
				} else
					mddi_host_reg_out(REV_PTR,
						  pmhctl->
						  mddi_rev_ptr_write_val);
#else
				mddi_host_reg_out(REV_PTR,
						  pmhctl->
						  mddi_rev_ptr_write_val);
#endif
			}
#endif
			if (mddi_debug_clear_rev_data) {
				uint16 i;
				for (i = 0; i < MDDI_MAX_REV_DATA_SIZE / 4; i++)
					pmhctl->rev_data_buf[i] = 0xdddddddd;
				/* clean cache */
				mddi_flush_cache_lines(pmhctl->rev_data_buf,
						       MDDI_MAX_REV_DATA_SIZE);
			}

			/* send reverse encapsulation to get needed data */
			mddi_host_reg_out(CMD, mddi_command);
		}
	}

}

static void mddi_process_client_initiated_wakeup(void)
{
	mddi_host_type host_idx = mddi_curr_host;
	mddi_host_cntl_type *pmhctl = &(mhctl[host_idx]);

	/* Disable MDDI_INT Interrupt, we detect client initiated wakeup one
	 * time for each entry into hibernation */
	mddi_host_reg_outm(INTEN, MDDI_INT_MDDI_IN, 0);

	if (host_idx == MDDI_HOST_PRIM) {
		if (mddi_vsync_detect_enabled) {
			mddi_host_enable_io_clock();
#ifndef MDDI_HOST_DISP_LISTEN
			/* issue command to bring up link */
			/* need to do this to clear the vsync condition */
			if (pmhctl->link_state == MDDI_LINK_HIBERNATING) {
				pmhctl->link_state = MDDI_LINK_ACTIVATING;
				mddi_host_reg_out(CMD, MDDI_CMD_LINK_ACTIVE);
			}
#endif
			/*
			 * Indicate to client specific code that vsync was
			 * enabled, and we did not detect a client initiated
			 * wakeup. The client specific handler can clear the
			 * condition if necessary to prevent subsequent
			 * client initiated wakeups.
			 */
			mddi_client_lcd_vsync_detected(TRUE);
			pmhctl->log_parms.vsync_response_cnt++;
			MDDI_MSG_NOTICE("MDDI_INT_IN condition\n");

		}
	}

	if (mddi_gpio.polling_enabled) {
		mddi_host_enable_io_clock();
		/* check interrupt status now */
		(void)mddi_queue_register_read_int(mddi_gpio.polling_reg,
						   &mddi_gpio.polling_val);
	}
}
#endif /* FEATURE_MDDI_DISABLE_REVERSE */

static void mddi_host_isr(void)
{
	uint32 int_reg, int_en;
#ifndef FEATURE_MDDI_DISABLE_REVERSE
	uint32 status_reg;
#endif
	mddi_host_type host_idx = mddi_curr_host;
	mddi_host_cntl_type *pmhctl = &(mhctl[host_idx]);

	if (!MDDI_HOST_IS_HCLK_ON) {
		MDDI_HOST_ENABLE_HCLK;
		MDDI_MSG_DEBUG("HCLK disabled, but isr is firing\n");
	}
	int_reg = mddi_host_reg_in(INT);
	int_en = mddi_host_reg_in(INTEN);
	pmhctl->saved_int_reg = int_reg;
	pmhctl->saved_int_en = int_en;
	int_reg = int_reg & int_en;
	pmhctl->int_type.count++;


#ifndef FEATURE_MDDI_DISABLE_REVERSE
	status_reg = mddi_host_reg_in(STAT);

	if ((int_reg & MDDI_INT_MDDI_IN) ||
	    ((int_en & MDDI_INT_MDDI_IN) &&
	     ((int_reg == 0) || (status_reg & MDDI_STAT_CLIENT_WAKEUP_REQ)))) {
		/*
		 * The MDDI_IN condition will clear itself, and so it is
		 * possible that MDDI_IN was the reason for the isr firing,
		 * even though the interrupt register does not have the
		 * MDDI_IN bit set. To check if this was the case we need to
		 * look at the status register bit that signifies a client
		 * initiated wakeup. If the status register bit is set, as well
		 * as the MDDI_IN interrupt enabled, then we treat this as a
		 * client initiated wakeup.
		 */
		if (int_reg & MDDI_INT_MDDI_IN)
			pmhctl->int_type.in_count++;
		mddi_process_client_initiated_wakeup();
	}
#endif

	if (int_reg & MDDI_INT_LINK_STATE_CHANGES) {
		pmhctl->int_type.state_change_count++;
		mddi_report_state_change(int_reg);
	}

	if (int_reg & MDDI_INT_PRI_LINK_LIST_DONE) {
		pmhctl->int_type.ll_done_count++;
		mddi_process_link_list_done();
	}
#ifndef FEATURE_MDDI_DISABLE_REVERSE
	if (int_reg & MDDI_INT_REV_DATA_AVAIL) {
		pmhctl->int_type.rev_avail_count++;
		mddi_process_rev_packets();
	}
#endif

	if (int_reg & MDDI_INT_ERROR_CONDITIONS) {
		pmhctl->int_type.error_count++;
		mddi_report_errors(int_reg);

		mddi_host_reg_out(INT, int_reg & MDDI_INT_ERROR_CONDITIONS);
	}
#ifndef FEATURE_MDDI_DISABLE_REVERSE
	mddi_issue_reverse_encapsulation();

	if ((pmhctl->rev_state != MDDI_REV_ENCAP_ISSUED) &&
	    (pmhctl->rev_state != MDDI_REV_STATUS_REQ_ISSUED))
#endif
		/* don't want simultaneous reverse and forward with Eagle */
		mddi_queue_forward_linked_list();

	if (int_reg & MDDI_INT_NO_CMD_PKTS_PEND) {
		/* this interrupt is used to kick the isr when hibernation is disabled */
		mddi_host_reg_outm(INTEN, MDDI_INT_NO_CMD_PKTS_PEND, 0);
	}

	if ((!mddi_host_mdp_active_flag) &&
	    (!mddi_vsync_detect_enabled) &&
	    (pmhctl->llist_info.transmitting_start_idx == UNASSIGNED_INDEX) &&
	    (pmhctl->llist_info.waiting_start_idx == UNASSIGNED_INDEX) &&
	    (pmhctl->rev_state == MDDI_REV_IDLE)) {
		if (pmhctl->link_state == MDDI_LINK_HIBERNATING) {
			mddi_host_disable_io_clock();
			mddi_host_disable_hclk();
		}
#ifdef FEATURE_MDDI_HOST_ENABLE_EARLY_HIBERNATION
		else if ((pmhctl->link_state == MDDI_LINK_ACTIVE) &&
			 (!pmhctl->disable_hibernation)) {
			mddi_host_reg_out(CMD, MDDI_CMD_POWERDOWN);
		}
#endif
	}
}

static void mddi_host_isr_primary(void)
{
	mddi_curr_host = MDDI_HOST_PRIM;
	mddi_host_isr();
}

irqreturn_t mddi_pmdh_isr_proxy(int irq, void *ptr)
{
	mddi_host_isr_primary();
	return IRQ_HANDLED;
}

static void mddi_host_isr_external(void)
{
	mddi_curr_host = MDDI_HOST_EXT;
	mddi_host_isr();
	mddi_curr_host = MDDI_HOST_PRIM;
}

irqreturn_t mddi_emdh_isr_proxy(int irq, void *ptr)
{
	mddi_host_isr_external();
	return IRQ_HANDLED;
}

static void mddi_host_initialize_registers(mddi_host_type host_idx)
{
	uint32 pad_reg_val;
	mddi_host_cntl_type *pmhctl = &(mhctl[host_idx]);

	if (pmhctl->driver_state == MDDI_DRIVER_ENABLED)
		return;

	/* turn on HCLK to MDDI host core */
	mddi_host_enable_hclk();

	/* MDDI Reset command */
	mddi_host_reg_out(CMD, MDDI_CMD_RESET);

	/* Version register (= 0x01) */
	mddi_host_reg_out(VERSION, 0x0001);

	/* Bytes per subframe register */
	mddi_host_reg_out(BPS, MDDI_HOST_BYTES_PER_SUBFRAME);

	/* Subframes per media frames register (= 0x03) */
	mddi_host_reg_out(SPM, 0x0003);

	/* Turn Around 1 register (= 0x05) */
	mddi_host_reg_out(TA1_LEN, 0x0005);

	/* Turn Around 2 register (= 0x0C) */
	mddi_host_reg_out(TA2_LEN, MDDI_HOST_TA2_LEN);

	/* Drive hi register (= 0x96) */
	mddi_host_reg_out(DRIVE_HI, 0x0096);

	/* Drive lo register (= 0x32) */
	mddi_host_reg_out(DRIVE_LO, 0x0032);

	/* Display wakeup count register (= 0x3c) */
	mddi_host_reg_out(DISP_WAKE, 0x003c);

	/* Reverse Rate Divisor register (= 0x2) */
	mddi_host_reg_out(REV_RATE_DIV, MDDI_HOST_REV_RATE_DIV);

#ifndef FEATURE_MDDI_DISABLE_REVERSE
	/* Reverse Pointer Size */
	mddi_host_reg_out(REV_SIZE, MDDI_REV_BUFFER_SIZE);

	/* Rev Encap Size */
	mddi_host_reg_out(REV_ENCAP_SZ, pmhctl->rev_pkt_size);
#endif

	/* Periodic Rev Encap */
	/* don't send periodically */
	mddi_host_reg_out(CMD, MDDI_CMD_PERIODIC_REV_ENCAP);

	pad_reg_val = mddi_host_reg_in(PAD_CTL);
	if (pad_reg_val == 0) {
		/* If we are turning on band gap, need to wait 5us before turning
		 * on the rest of the PAD */
		mddi_host_reg_out(PAD_CTL, 0x08000);
		udelay(5);
	}
#ifdef T_MSM7200
	/* Recommendation from PAD hw team */
	mddi_host_reg_out(PAD_CTL, 0xa850a);
#else
	/* Recommendation from PAD hw team */
	mddi_host_reg_out(PAD_CTL, 0xa850f);
#endif

#if defined(CONFIG_FB_MSM_MDP31) || defined(CONFIG_FB_MSM_MDP40)
	mddi_host_reg_out(PAD_IO_CTL, 0x00320000);
	mddi_host_reg_out(PAD_CAL, 0x00220020);
#endif

	mddi_host_core_version = mddi_host_reg_inm(CORE_VER, 0xffff);

#ifndef FEATURE_MDDI_DISABLE_REVERSE
	if (mddi_host_core_version >= 8)
		mddi_rev_ptr_workaround = FALSE;
	pmhctl->rev_ptr_curr = pmhctl->rev_ptr_start;
#endif

	if ((mddi_host_core_version > 8) && (mddi_host_core_version < 0x19))
		mddi_host_reg_out(TEST, 0x2);

	/* Need an even number for counts */
	mddi_host_reg_out(DRIVER_START_CNT, 0x60006);

#ifndef T_MSM7500
	/* Setup defaults for MDP related register */
	mddi_host_reg_out(MDP_VID_FMT_DES, 0x5666);
	mddi_host_reg_out(MDP_VID_PIX_ATTR, 0x00C3);
	mddi_host_reg_out(MDP_VID_CLIENTID, 0);
#endif

	/* automatically hibernate after 1 empty subframe */
	if (pmhctl->disable_hibernation)
		mddi_host_reg_out(CMD, MDDI_CMD_HIBERNATE);
	else
		mddi_host_reg_out(CMD, MDDI_CMD_HIBERNATE | 1);

	/* Bring up link if display (client) requests it */
#ifdef MDDI_HOST_DISP_LISTEN
	mddi_host_reg_out(CMD, MDDI_CMD_DISP_LISTEN);
#else
	mddi_host_reg_out(CMD, MDDI_CMD_DISP_IGNORE);
#endif

}

void mddi_host_configure_interrupts(mddi_host_type host_idx, boolean enable)
{
	unsigned long flags;
	mddi_host_cntl_type *pmhctl = &(mhctl[host_idx]);

	spin_lock_irqsave(&mddi_host_spin_lock, flags);

	/* turn on HCLK to MDDI host core if it has been disabled */
	mddi_host_enable_hclk();
	/* Clear MDDI Interrupt enable reg */
	mddi_host_reg_out(INTEN, 0);

	spin_unlock_irqrestore(&mddi_host_spin_lock, flags);

	if (enable) {
		pmhctl->driver_state = MDDI_DRIVER_ENABLED;

		if (host_idx == MDDI_HOST_PRIM) {
			if (request_irq
			    (INT_MDDI_PRI, mddi_pmdh_isr_proxy, IRQF_DISABLED,
			     "PMDH", 0) != 0)
				printk(KERN_ERR
				       "a mddi: unable to request_irq\n");
			else
				int_mddi_pri_flag = TRUE;
		} else {
			if (request_irq
			    (INT_MDDI_EXT, mddi_emdh_isr_proxy, IRQF_DISABLED,
			     "EMDH", 0) != 0)
				printk(KERN_ERR
				       "b mddi: unable to request_irq\n");
			else
				int_mddi_ext_flag = TRUE;
		}

		/* Set MDDI Interrupt enable reg -- Enable Reverse data avail */
#ifdef FEATURE_MDDI_DISABLE_REVERSE
		mddi_host_reg_out(INTEN,
				  MDDI_INT_ERROR_CONDITIONS |
				  MDDI_INT_LINK_STATE_CHANGES);
#else
		/* Reverse Pointer register */
		pmhctl->rev_ptr_written = FALSE;

		mddi_host_reg_out(INTEN,
				  MDDI_INT_REV_DATA_AVAIL |
				  MDDI_INT_ERROR_CONDITIONS |
				  MDDI_INT_LINK_STATE_CHANGES);
		pmhctl->rtd_counter = mddi_rtd_frequency;
		pmhctl->client_status_cnt = 0;
#endif
	} else {
		if (pmhctl->driver_state == MDDI_DRIVER_ENABLED)
			pmhctl->driver_state = MDDI_DRIVER_DISABLED;
	}

}

static void mddi_host_powerup(mddi_host_type host_idx)
{
	mddi_host_cntl_type *pmhctl = &(mhctl[host_idx]);

	if (pmhctl->link_state != MDDI_LINK_DISABLED)
		return;

	/* enable IO_CLK and hclk to MDDI host core */
	mddi_host_enable_io_clock();

	mddi_host_initialize_registers(host_idx);
	mddi_host_configure_interrupts(host_idx, TRUE);

	pmhctl->link_state = MDDI_LINK_ACTIVATING;

	/* Link activate command */
	mddi_host_reg_out(CMD, MDDI_CMD_LINK_ACTIVE);

#ifdef CLKRGM_MDDI_IO_CLOCK_IN_MHZ
	MDDI_MSG_NOTICE("MDDI Host: Activating Link %d Mbps\n",
			CLKRGM_MDDI_IO_CLOCK_IN_MHZ * 2);
#else
	MDDI_MSG_NOTICE("MDDI Host: Activating Link\n");
#endif

	/* Initialize the timer */
	if (host_idx == MDDI_HOST_PRIM)
		mddi_host_timer_service(0);
}

void mddi_host_init(mddi_host_type host_idx)
/* Write out the MDDI configuration registers */
{
	static boolean initialized = FALSE;
	mddi_host_cntl_type *pmhctl;

	if (host_idx >= MDDI_NUM_HOST_CORES) {
		MDDI_MSG_ERR("Invalid host core index\n");
		return;
	}

	if (!initialized) {
		uint16 idx;
		mddi_host_type host;
		for (host = MDDI_HOST_PRIM; host < MDDI_NUM_HOST_CORES; host++) {
			pmhctl = &(mhctl[host]);
			initialized = TRUE;

			pmhctl->llist_ptr =
			    dma_alloc_coherent(NULL, MDDI_LLIST_POOL_SIZE,
					       &(pmhctl->llist_dma_addr),
					       GFP_KERNEL);
			pmhctl->llist_dma_ptr =
			    (mddi_linked_list_type *) (void *)pmhctl->
			    llist_dma_addr;
#ifdef FEATURE_MDDI_DISABLE_REVERSE
			pmhctl->rev_data_buf = NULL;
			if (pmhctl->llist_ptr == NULL)
#else
			mddi_rev_user.waiting = FALSE;
			init_completion(&(mddi_rev_user.done_comp));
			pmhctl->rev_data_buf =
			    dma_alloc_coherent(NULL, MDDI_MAX_REV_DATA_SIZE,
					       &(pmhctl->rev_data_dma_addr),
					       GFP_KERNEL);
			if ((pmhctl->llist_ptr == NULL)
			    || (pmhctl->rev_data_buf == NULL))
#endif
			{
				MDDI_MSG_CRIT
				    ("unable to alloc non-cached memory\n");
			}
			llist_extern[host] = pmhctl->llist_ptr;
			llist_dma_extern[host] = pmhctl->llist_dma_ptr;
			llist_extern_notify[host] = pmhctl->llist_notify;

			for (idx = 0; idx < UNASSIGNED_INDEX; idx++) {
				init_completion(&
						(pmhctl->llist_notify[idx].
						 done_comp));
			}
			init_completion(&(pmhctl->mddi_llist_avail_comp));
			spin_lock_init(&mddi_host_spin_lock);
			pmhctl->mddi_waiting_for_llist_avail = FALSE;
			pmhctl->mddi_rev_ptr_write_val =
			    (uint32) (void *)(pmhctl->rev_data_dma_addr);
			pmhctl->rev_ptr_start = (void *)pmhctl->rev_data_buf;

			pmhctl->rev_pkt_size = MDDI_DEFAULT_REV_PKT_SIZE;
			pmhctl->rev_state = MDDI_REV_IDLE;
#ifdef IMAGE_MODEM_PROC
			/* assume hibernation state is last state from APPS proc, so that
			 * we don't reinitialize the host core */
			pmhctl->link_state = MDDI_LINK_HIBERNATING;
#else
			pmhctl->link_state = MDDI_LINK_DISABLED;
#endif
			pmhctl->driver_state = MDDI_DRIVER_DISABLED;
			pmhctl->disable_hibernation = FALSE;

			/* initialize llist variables */
			pmhctl->llist_info.transmitting_start_idx =
			    UNASSIGNED_INDEX;
			pmhctl->llist_info.transmitting_end_idx =
			    UNASSIGNED_INDEX;
			pmhctl->llist_info.waiting_start_idx = UNASSIGNED_INDEX;
			pmhctl->llist_info.waiting_end_idx = UNASSIGNED_INDEX;
			pmhctl->llist_info.reg_read_idx = UNASSIGNED_INDEX;
			pmhctl->llist_info.next_free_idx =
			    MDDI_FIRST_DYNAMIC_LLIST_IDX;
			pmhctl->llist_info.reg_read_waiting = FALSE;

			mddi_vsync_detect_enabled = FALSE;
			mddi_gpio.polling_enabled = FALSE;

			pmhctl->int_type.count = 0;
			pmhctl->int_type.in_count = 0;
			pmhctl->int_type.disp_req_count = 0;
			pmhctl->int_type.state_change_count = 0;
			pmhctl->int_type.ll_done_count = 0;
			pmhctl->int_type.rev_avail_count = 0;
			pmhctl->int_type.error_count = 0;
			pmhctl->int_type.rev_encap_count = 0;
			pmhctl->int_type.llist_ptr_write_1 = 0;
			pmhctl->int_type.llist_ptr_write_2 = 0;

			pmhctl->stats.fwd_crc_count = 0;
			pmhctl->stats.rev_crc_count = 0;
			pmhctl->stats.pri_underflow = 0;
			pmhctl->stats.sec_underflow = 0;
			pmhctl->stats.rev_overflow = 0;
			pmhctl->stats.pri_overwrite = 0;
			pmhctl->stats.sec_overwrite = 0;
			pmhctl->stats.rev_overwrite = 0;
			pmhctl->stats.dma_failure = 0;
			pmhctl->stats.rtd_failure = 0;
			pmhctl->stats.reg_read_failure = 0;
#ifdef FEATURE_MDDI_UNDERRUN_RECOVERY
			pmhctl->stats.pri_underrun_detected = 0;
#endif

			pmhctl->log_parms.rtd_cnt = 0;
			pmhctl->log_parms.rev_enc_cnt = 0;
			pmhctl->log_parms.vid_cnt = 0;
			pmhctl->log_parms.reg_acc_cnt = 0;
			pmhctl->log_parms.cli_stat_cnt = 0;
			pmhctl->log_parms.cli_cap_cnt = 0;
			pmhctl->log_parms.reg_read_cnt = 0;
			pmhctl->log_parms.link_active_cnt = 0;
			pmhctl->log_parms.link_hibernate_cnt = 0;
			pmhctl->log_parms.fwd_crc_cnt = 0;
			pmhctl->log_parms.rev_crc_cnt = 0;
			pmhctl->log_parms.vsync_response_cnt = 0;

			prev_parms[host_idx] = pmhctl->log_parms;
			mddi_client_capability_pkt.packet_length = 0;
		}

#ifndef T_MSM7500
		/* tell clock driver we are user of this PLL */
		MDDI_HOST_ENABLE_IO_CLOCK;
#endif
	}

	mddi_host_powerup(host_idx);
	pmhctl = &(mhctl[host_idx]);
}

#ifdef CONFIG_FB_MSM_MDDI_AUTO_DETECT
static uint32 mddi_client_id;

uint32 mddi_get_client_id(void)
{

#ifndef FEATURE_MDDI_DISABLE_REVERSE
	mddi_host_type host_idx = MDDI_HOST_PRIM;
	static boolean client_detection_try = FALSE;
	mddi_host_cntl_type *pmhctl;
	unsigned long flags;
	uint16 saved_rev_pkt_size;

	if (!client_detection_try) {
		/* Toshiba display requires larger drive_lo value */
		mddi_host_reg_out(DRIVE_LO, 0x0050);

		pmhctl = &(mhctl[MDDI_HOST_PRIM]);

		saved_rev_pkt_size = pmhctl->rev_pkt_size;

		/* Increase Rev Encap Size */
		pmhctl->rev_pkt_size = MDDI_CLIENT_CAPABILITY_REV_PKT_SIZE;
		mddi_host_reg_out(REV_ENCAP_SZ, pmhctl->rev_pkt_size);

		/* disable hibernation temporarily */
		if (!pmhctl->disable_hibernation)
			mddi_host_reg_out(CMD, MDDI_CMD_HIBERNATE);

		mddi_rev_user.waiting = TRUE;
		INIT_COMPLETION(mddi_rev_user.done_comp);

		spin_lock_irqsave(&mddi_host_spin_lock, flags);

		/* turn on clock(s), if they have been disabled */
		mddi_host_enable_hclk();
		mddi_host_enable_io_clock();

		mddi_client_capability_request = TRUE;

		if (pmhctl->rev_state == MDDI_REV_IDLE) {
			/* attempt to send the reverse encapsulation now */
			mddi_issue_reverse_encapsulation();
		}
		spin_unlock_irqrestore(&mddi_host_spin_lock, flags);

		wait_for_completion_killable(&(mddi_rev_user.done_comp));

		/* Set Rev Encap Size back to its original value */
		pmhctl->rev_pkt_size = saved_rev_pkt_size;
		mddi_host_reg_out(REV_ENCAP_SZ, pmhctl->rev_pkt_size);

		/* reenable auto-hibernate */
		if (!pmhctl->disable_hibernation)
			mddi_host_reg_out(CMD, MDDI_CMD_HIBERNATE | 1);

		mddi_host_reg_out(DRIVE_LO, 0x0032);
		client_detection_try = TRUE;

		mddi_client_id = (mddi_client_capability_pkt.Mfr_Name<<16) |
				mddi_client_capability_pkt.Product_Code;

		if (!mddi_client_id)
			mddi_disable(1);
	}

#if 0
	switch (mddi_client_capability_pkt.Mfr_Name) {
	case 0x4474:
		if ((mddi_client_capability_pkt.Product_Code != 0x8960) &&
		    (target == DISPLAY_1)) {
			ret = PRISM_WVGA;
		}
		break;

	case 0xD263:
		if (target == DISPLAY_1)
			ret = TOSHIBA_VGA_PRIM;
		else if (target == DISPLAY_2)
			ret = TOSHIBA_QCIF_SECD;
		break;

	case 0:
		if (mddi_client_capability_pkt.Product_Code == 0x8835) {
			if (target == DISPLAY_1)
				ret = SHARP_QVGA_PRIM;
			else if (target == DISPLAY_2)
				ret = SHARP_128x128_SECD;
		}
		break;

	default:
		break;
	}

	if ((!client_detection_try) && (ret != TOSHIBA_VGA_PRIM)
	    && (ret != TOSHIBA_QCIF_SECD)) {
		/* Not a Toshiba display, so change drive_lo back to default value */
		mddi_host_reg_out(DRIVE_LO, 0x0032);
	}
#endif

#endif

	return mddi_client_id;
}
#endif

void mddi_host_powerdown(mddi_host_type host_idx)
{
	mddi_host_cntl_type *pmhctl = &(mhctl[host_idx]);

	if (host_idx >= MDDI_NUM_HOST_CORES) {
		MDDI_MSG_ERR("Invalid host core index\n");
		return;
	}

	if (pmhctl->driver_state == MDDI_DRIVER_RESET) {
		return;
	}

	if (host_idx == MDDI_HOST_PRIM) {
		/* disable timer */
		del_timer(&mddi_host_timer);
	}

	mddi_host_configure_interrupts(host_idx, FALSE);

	/* turn on HCLK to MDDI host core if it has been disabled */
	mddi_host_enable_hclk();

	/* MDDI Reset command */
	mddi_host_reg_out(CMD, MDDI_CMD_RESET);

	/* Pad Control Register */
	mddi_host_reg_out(PAD_CTL, 0x0);

	/* disable IO_CLK and hclk to MDDI host core */
	mddi_host_disable_io_clock();
	mddi_host_disable_hclk();

	pmhctl->link_state = MDDI_LINK_DISABLED;
	pmhctl->driver_state = MDDI_DRIVER_RESET;

	MDDI_MSG_NOTICE("MDDI Host: Disabling Link\n");

}

uint16 mddi_get_next_free_llist_item(mddi_host_type host_idx, boolean wait)
{
	unsigned long flags;
	uint16 ret_idx;
	boolean forced_wait = FALSE;
	mddi_host_cntl_type *pmhctl = &(mhctl[host_idx]);

	ret_idx = pmhctl->llist_info.next_free_idx;

	pmhctl->llist_info.next_free_idx++;
	if (pmhctl->llist_info.next_free_idx >= MDDI_NUM_DYNAMIC_LLIST_ITEMS)
		pmhctl->llist_info.next_free_idx = MDDI_FIRST_DYNAMIC_LLIST_IDX;
	spin_lock_irqsave(&mddi_host_spin_lock, flags);
	if (pmhctl->llist_notify[ret_idx].in_use) {
		if (!wait) {
			pmhctl->llist_info.next_free_idx = ret_idx;
			ret_idx = UNASSIGNED_INDEX;
		} else {
			forced_wait = TRUE;
			INIT_COMPLETION(pmhctl->mddi_llist_avail_comp);
		}
	}
	spin_unlock_irqrestore(&mddi_host_spin_lock, flags);

	if (forced_wait) {
		wait_for_completion_killable(&
						  (pmhctl->
						   mddi_llist_avail_comp));
		MDDI_MSG_ERR("task waiting on mddi llist item\n");
	}

	if (ret_idx != UNASSIGNED_INDEX) {
		pmhctl->llist_notify[ret_idx].waiting = FALSE;
		pmhctl->llist_notify[ret_idx].done_cb = NULL;
		pmhctl->llist_notify[ret_idx].in_use = TRUE;
		pmhctl->llist_notify[ret_idx].next_idx = UNASSIGNED_INDEX;
	}

	return ret_idx;
}

uint16 mddi_get_reg_read_llist_item(mddi_host_type host_idx, boolean wait)
{
#ifdef FEATURE_MDDI_DISABLE_REVERSE
	MDDI_MSG_CRIT("No reverse link available\n");
	(void)wait;
	return FALSE;
#else
	unsigned long flags;
	uint16 ret_idx;
	boolean error = FALSE;
	mddi_host_cntl_type *pmhctl = &(mhctl[host_idx]);

	spin_lock_irqsave(&mddi_host_spin_lock, flags);
	if (pmhctl->llist_info.reg_read_idx != UNASSIGNED_INDEX) {
		/* need to block here or is this an error condition? */
		error = TRUE;
		ret_idx = UNASSIGNED_INDEX;
	}
	spin_unlock_irqrestore(&mddi_host_spin_lock, flags);

	if (!error) {
		ret_idx = pmhctl->llist_info.reg_read_idx =
		    mddi_get_next_free_llist_item(host_idx, wait);
		/* clear the reg_read_waiting flag */
		pmhctl->llist_info.reg_read_waiting = FALSE;
	}

	if (error)
		MDDI_MSG_ERR("***** Reg read still in progress! ****\n");
	return ret_idx;
#endif

}

void mddi_queue_forward_packets(uint16 first_llist_idx,
				uint16 last_llist_idx,
				boolean wait,
				mddi_llist_done_cb_type llist_done_cb,
				mddi_host_type host_idx)
{
	unsigned long flags;
	mddi_linked_list_type *llist;
	mddi_linked_list_type *llist_dma;
	mddi_host_cntl_type *pmhctl = &(mhctl[host_idx]);

	if ((first_llist_idx >= UNASSIGNED_INDEX) ||
	    (last_llist_idx >= UNASSIGNED_INDEX)) {
		MDDI_MSG_ERR("MDDI queueing invalid linked list\n");
		return;
	}

	if (pmhctl->link_state == MDDI_LINK_DISABLED)
		MDDI_MSG_CRIT("MDDI host powered down!\n");

	llist = pmhctl->llist_ptr;
	llist_dma = pmhctl->llist_dma_ptr;

	/* clean cache so MDDI host can read data */
	memory_barrier();

	pmhctl->llist_notify[last_llist_idx].waiting = wait;
	if (wait)
		INIT_COMPLETION(pmhctl->llist_notify[last_llist_idx].done_comp);
	pmhctl->llist_notify[last_llist_idx].done_cb = llist_done_cb;

	spin_lock_irqsave(&mddi_host_spin_lock, flags);

	if ((pmhctl->llist_info.transmitting_start_idx == UNASSIGNED_INDEX) &&
	    (pmhctl->llist_info.waiting_start_idx == UNASSIGNED_INDEX) &&
	    (pmhctl->rev_state == MDDI_REV_IDLE)) {
		/* no packets are currently transmitting */
#ifndef FEATURE_MDDI_DISABLE_REVERSE
		if (first_llist_idx == pmhctl->llist_info.reg_read_idx) {
			/* This is the special case where the packet is a register read. */
			pmhctl->rev_state = MDDI_REV_REG_READ_ISSUED;
			mddi_reg_read_retry = 0;
			/* mddi_rev_reg_read_attempt = 1; */
		}
#endif
		/* assign transmitting index values */
		pmhctl->llist_info.transmitting_start_idx = first_llist_idx;
		pmhctl->llist_info.transmitting_end_idx = last_llist_idx;

		/* turn on clock(s), if they have been disabled */
		mddi_host_enable_hclk();
		mddi_host_enable_io_clock();
		pmhctl->int_type.llist_ptr_write_1++;
		/* Write to primary pointer register */
		dma_coherent_pre_ops();
		mddi_host_reg_out(PRI_PTR, &llist_dma[first_llist_idx]);

		/* enable interrupt when complete */
		mddi_host_reg_outm(INTEN, MDDI_INT_PRI_LINK_LIST_DONE,
				   MDDI_INT_PRI_LINK_LIST_DONE);

	} else if (pmhctl->llist_info.waiting_start_idx == UNASSIGNED_INDEX) {
#ifndef FEATURE_MDDI_DISABLE_REVERSE
		if (first_llist_idx == pmhctl->llist_info.reg_read_idx) {
			/*
			 * we have a register read to send but need to wait
			 * for current reverse activity to end or there are
			 * packets currently transmitting
			 */
			/* mddi_rev_reg_read_attempt = 0; */
			pmhctl->llist_info.reg_read_waiting = TRUE;
		}
#endif

		/* assign waiting index values */
		pmhctl->llist_info.waiting_start_idx = first_llist_idx;
		pmhctl->llist_info.waiting_end_idx = last_llist_idx;
	} else {
		uint16 prev_end_idx = pmhctl->llist_info.waiting_end_idx;
#ifndef FEATURE_MDDI_DISABLE_REVERSE
		if (first_llist_idx == pmhctl->llist_info.reg_read_idx) {
			/*
			 * we have a register read to send but need to wait
			 * for current reverse activity to end or there are
			 * packets currently transmitting
			 */
			/* mddi_rev_reg_read_attempt = 0; */
			pmhctl->llist_info.reg_read_waiting = TRUE;
		}
#endif

		llist = pmhctl->llist_ptr;

		/* clear end flag in previous last packet */
		llist[prev_end_idx].link_controller_flags = 0;
		pmhctl->llist_notify[prev_end_idx].next_idx = first_llist_idx;

		/* set the next_packet_pointer of the previous last packet */
		llist[prev_end_idx].next_packet_pointer =
		    (void *)(&llist_dma[first_llist_idx]);

		/* clean cache so MDDI host can read data */
		memory_barrier();

		/* assign new waiting last index value */
		pmhctl->llist_info.waiting_end_idx = last_llist_idx;
	}

	spin_unlock_irqrestore(&mddi_host_spin_lock, flags);

}

void mddi_host_write_pix_attr_reg(uint32 value)
{
	(void)value;
}

void mddi_queue_reverse_encapsulation(boolean wait)
{
#ifdef FEATURE_MDDI_DISABLE_REVERSE
	MDDI_MSG_CRIT("No reverse link available\n");
	(void)wait;
#else
	unsigned long flags;
	boolean error = FALSE;
	mddi_host_type host_idx = MDDI_HOST_PRIM;
	mddi_host_cntl_type *pmhctl = &(mhctl[MDDI_HOST_PRIM]);

	spin_lock_irqsave(&mddi_host_spin_lock, flags);

	/* turn on clock(s), if they have been disabled */
	mddi_host_enable_hclk();
	mddi_host_enable_io_clock();

	if (wait) {
		if (!mddi_rev_user.waiting) {
			mddi_rev_user.waiting = TRUE;
			INIT_COMPLETION(mddi_rev_user.done_comp);
		} else
			error = TRUE;
	}
	mddi_rev_encap_user_request = TRUE;

	if (pmhctl->rev_state == MDDI_REV_IDLE) {
		/* attempt to send the reverse encapsulation now */
		mddi_host_type orig_host_idx = mddi_curr_host;
		mddi_curr_host = host_idx;
		mddi_issue_reverse_encapsulation();
		mddi_curr_host = orig_host_idx;
	}
	spin_unlock_irqrestore(&mddi_host_spin_lock, flags);

	if (error) {
		MDDI_MSG_ERR("Reverse Encap request already in progress\n");
	} else if (wait)
		wait_for_completion_killable(&(mddi_rev_user.done_comp));
#endif
}

/* ISR to be executed */
boolean mddi_set_rev_handler(mddi_rev_handler_type handler, uint16 pkt_type)
{
#ifdef FEATURE_MDDI_DISABLE_REVERSE
	MDDI_MSG_CRIT("No reverse link available\n");
	(void)handler;
	(void)pkt_type;
	return (FALSE);
#else
	unsigned long flags;
	uint16 hdlr;
	boolean handler_set = FALSE;
	boolean overwrite = FALSE;
	mddi_host_type host_idx = MDDI_HOST_PRIM;
	mddi_host_cntl_type *pmhctl = &(mhctl[MDDI_HOST_PRIM]);

	/* Disable interrupts */
	spin_lock_irqsave(&mddi_host_spin_lock, flags);

	for (hdlr = 0; hdlr < MAX_MDDI_REV_HANDLERS; hdlr++) {
		if (mddi_rev_pkt_handler[hdlr].pkt_type == pkt_type) {
			mddi_rev_pkt_handler[hdlr].handler = handler;
			if (handler == NULL) {
				/* clearing handler from table */
				mddi_rev_pkt_handler[hdlr].pkt_type =
				    INVALID_PKT_TYPE;
				handler_set = TRUE;
				if (pkt_type == 0x10) {	/* video stream packet */
					/* ensure HCLK on to MDDI host core before register write */
					mddi_host_enable_hclk();
					/* No longer getting video, so reset rev encap size to default */
					pmhctl->rev_pkt_size =
					    MDDI_DEFAULT_REV_PKT_SIZE;
					mddi_host_reg_out(REV_ENCAP_SZ,
							  pmhctl->rev_pkt_size);
				}
			} else {
				/* already a handler for this packet */
				overwrite = TRUE;
			}
			break;
		}
	}
	if ((hdlr >= MAX_MDDI_REV_HANDLERS) && (handler != NULL)) {
		/* assigning new handler */
		for (hdlr = 0; hdlr < MAX_MDDI_REV_HANDLERS; hdlr++) {
			if (mddi_rev_pkt_handler[hdlr].pkt_type ==
			    INVALID_PKT_TYPE) {
				if ((pkt_type == 0x10) &&	/* video stream packet */
				    (pmhctl->rev_pkt_size <
				     MDDI_VIDEO_REV_PKT_SIZE)) {
					/* ensure HCLK on to MDDI host core before register write */
					mddi_host_enable_hclk();
					/* Increase Rev Encap Size */
					pmhctl->rev_pkt_size =
					    MDDI_VIDEO_REV_PKT_SIZE;
					mddi_host_reg_out(REV_ENCAP_SZ,
							  pmhctl->rev_pkt_size);
				}
				mddi_rev_pkt_handler[hdlr].handler = handler;
				mddi_rev_pkt_handler[hdlr].pkt_type = pkt_type;
				handler_set = TRUE;
				break;
			}
		}
	}

	/* Restore interrupts */
	spin_unlock_irqrestore(&mddi_host_spin_lock, flags);

	if (overwrite)
		MDDI_MSG_ERR("Overwriting previous rev packet handler\n");

	return handler_set;

#endif
}				/* mddi_set_rev_handler */

void mddi_host_disable_hibernation(boolean disable)
{
	mddi_host_type host_idx = MDDI_HOST_PRIM;
	mddi_host_cntl_type *pmhctl = &(mhctl[MDDI_HOST_PRIM]);

	if (disable) {
		pmhctl->disable_hibernation = TRUE;
		/* hibernation will be turned off by isr next time it is entered */
	} else {
		if (pmhctl->disable_hibernation) {
			unsigned long flags;
			spin_lock_irqsave(&mddi_host_spin_lock, flags);
			if (!MDDI_HOST_IS_HCLK_ON)
				MDDI_HOST_ENABLE_HCLK;
			mddi_host_reg_out(CMD, MDDI_CMD_HIBERNATE | 1);
			spin_unlock_irqrestore(&mddi_host_spin_lock, flags);
			pmhctl->disable_hibernation = FALSE;
		}
	}
}

void mddi_mhctl_remove(mddi_host_type host_idx)
{
	mddi_host_cntl_type *pmhctl;

	pmhctl = &(mhctl[host_idx]);

	dma_free_coherent(NULL, MDDI_LLIST_POOL_SIZE, (void *)pmhctl->llist_ptr,
			  pmhctl->llist_dma_addr);

	dma_free_coherent(NULL, MDDI_MAX_REV_DATA_SIZE,
			  (void *)pmhctl->rev_data_buf,
			  pmhctl->rev_data_dma_addr);
}