summaryrefslogtreecommitdiff
path: root/src/amd_gx_driver.c
blob: 4691a58aafbeaebbcd6c5b7203a4faae61108f92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
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
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
/* Copyright (c) 2003-2005 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Neither the name of the Advanced Micro Devices, Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 * */

/*
 * File Contents:   This is the main module configures the interfacing with
 *                  the X server. The individual modules will be loaded based
 *                  upon the options selected from the XF86Config. This file
 *                  also has modules for finding supported modes, turning on
 *                  the modes based on options.
 *
 * Project:         Geode Xfree Frame buffer device driver.
 * */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

/* Includes that are used by all drivers */
#include "xf86.h"
#include "xf86_OSproc.h"
#include "xf86_ansic.h"
#include "xf86_libc.h"
#include "xf86Resources.h"

/* We may want inb() and outb() */
#include "compiler.h"

/* We may want to access the PCI config space */
#include "xf86PciInfo.h"
#include "xf86Pci.h"

/* Colormap handling stuff */
#include "xf86cmap.h"

#define RC_MAX_DEPTH 24

#include "amd.h"
#include "gfx_defs.h"
#include "gfx_regs.h"
#include "panel.h"

/* Frame buffer stuff */
#if CFB
/*
 * If using cfb, cfb.h is required.  Select the others for the bpp values
 * the driver supports.
 */
#define PSZ 8                          /* needed for cfb.h */
#include "cfb.h"
#undef PSZ
#include "cfb16.h"
#include "cfb24.h"
#include "cfb32.h"
#else
#include "fb.h"
#endif

#include "shadowfb.h"

/* Machine independent stuff */
#include "mipointer.h"
#include "mibank.h"
#include "micmap.h"
/* All drivers implementing backing store need this */
#include "mibstore.h"
#include "vgaHW.h"
#include "vbe.h"

/* Check for some extensions */
#ifdef XFreeXDGA
#define _XF86_DGA_SERVER_
#include <X11/extensions/xf86dgastr.h>
#endif /* XFreeXDGA */

#ifdef DPMSExtension
#include "globals.h"
#include "opaque.h"
#define DPMS_SERVER
#include <X11/extensions/dpms.h>
#endif /* DPMSExtension */

#include "amd_gx_vga.c"

extern SymTabRec GeodeChipsets[];
extern OptionInfoRec GX_GeodeOptions[];

/* Forward definitions */
static Bool GXPreInit(ScrnInfoPtr, int);
static Bool GXScreenInit(int, ScreenPtr, int, char **);
static Bool GXEnterVT(int, int);
static void GXLeaveVT(int, int);
static void GXFreeScreen(int, int);
void GXAdjustFrame(int, int, int, int);
Bool GXSwitchMode(int, DisplayModePtr, int);
static int GXValidMode(int, DisplayModePtr, Bool, int);
static void GXLoadPalette(ScrnInfoPtr pScrni,
    int numColors, int *indizes, LOCO * colors, VisualPtr pVisual);
static Bool GXMapMem(ScrnInfoPtr);
static Bool GXUnmapMem(ScrnInfoPtr);
static void gx_set_DvLineSize(unsigned int pitch);

extern Bool GXAccelInit(ScreenPtr pScrn);
extern Bool GXHWCursorInit(ScreenPtr pScrn);
extern void GXHideCursor(ScrnInfoPtr pScrni);
extern void GXShowCursor(ScrnInfoPtr pScrni);
extern void GXPointerMoved(int index, int x, int y);
extern void GXRotationInit(ScrnInfoPtr pScrni);
extern void GXShadowFBInit(ScreenPtr pScrn, GeodeRec *pGeode, int bytpp);
extern void GXInitVideo(ScreenPtr pScrn);
extern Bool GXDGAInit(ScreenPtr pScrn);
extern void GXLoadCursorImage(ScrnInfoPtr pScrni, unsigned char *src);

unsigned char *XpressROMPtr;
unsigned long fb;

/* Existing Processor Models */
#define GX1 0x1
#define GX 0x2
#define GX_CRT 0x6
#define GX_TFT 0xA

/* List of symbols from other modules that this module references.The purpose
 * is that to avoid unresolved symbol warnings
 */
extern const char *amdVgahwSymbols[];
extern const char *amdVbeSymbols[];
extern const char *amdInt10Symbols[];

#if CFB
extern const char *amdCfbSymbols[];
#else
extern const char *amdFbSymbols[];
#endif
extern const char *amdXaaSymbols[];
extern const char *amdExaSymbols[];
extern const char *amdRamdacSymbols[];
extern const char *amdShadowSymbols[];

void GXSetupChipsetFPtr(ScrnInfoPtr pScrn);
GeodeRec *GXGetRec(ScrnInfoPtr pScrni);
void get_flatpanel_info(const char *options, int *W, int *H,
    int *D, int *C, int *T);
void gx_clear_screen(ScrnInfoPtr pScrni, int width, int height, int bpp);
void gx_enable_dac_power(ScrnInfoPtr pScrni);
void gx_disable_dac_power(ScrnInfoPtr pScrni);

#if DEBUGLVL>0
FILE *zdfp = NULL;
#endif

void
GXSetupChipsetFPtr(ScrnInfoPtr pScrn)
{
#if DEBUGLVL>0
    if (zdfp == NULL) {
        zdfp = fopen("/tmp/xwin.log", "w");
#if DEBUGTIM==0
        setbuf(zdfp, NULL);
#endif
    }
#endif
    DEBUGMSG(1, (0, X_INFO, "GXSetupChipsetFPtr!\n"));
    pScrn->PreInit = GXPreInit;
    pScrn->ScreenInit = GXScreenInit;
    pScrn->SwitchMode = GXSwitchMode;
    pScrn->AdjustFrame = GXAdjustFrame;
    pScrn->EnterVT = GXEnterVT;
    pScrn->LeaveVT = GXLeaveVT;
    pScrn->FreeScreen = GXFreeScreen;
    pScrn->ValidMode = GXValidMode;
}

/*----------------------------------------------------------------------------
 * GXGetRec.
 *
 * Description	:This function allocates a GeodeRec and hooks into pScrni
 *				struct's driverPrivate member of ScreeenInfo structure.
 *
 * Parameters.
 *		pScrni	:Pointer handle to the screenonfo structure.
 *
 * Returns		:allocated pScrninfo structure.
 *
 * Comments     :none
 *
 *----------------------------------------------------------------------------
 */
GeodeRec *
GXGetRec(ScrnInfoPtr pScrni)
{
    if (!pScrni->driverPrivate) {
        GeodeRec *pGeode;

        pGeode = pScrni->driverPrivate = xnfcalloc(sizeof(GeodeRec), 1);
#if INT10_SUPPORT
        pGeode->vesa = xcalloc(sizeof(VESARec), 1);
#endif
    }
    return GEODEPTR(pScrni);
}

/*----------------------------------------------------------------------------
 * GXFreeRec.
 *
 * Description	:This function deallocates and disconnect the GeodeRec from
 *               the pScrni struct's driverPrivate member.
 *               
 * Parameters.
 *		pScrni	:Pointer handle to the screenonfo structure.
 *
 * Returns		:none
 *
 * Comments     :none
 *
 *----------------------------------------------------------------------------
 */
static void
GXFreeRec(ScrnInfoPtr pScrni)
{
    if (pScrni->driverPrivate == NULL) {
        return;
    }
    xfree(pScrni->driverPrivate);
    pScrni->driverPrivate = NULL;
}

/*----------------------------------------------------------------------------
 * GXSaveScreen.
 *
 * Description	:This is todo the screen blanking
 *
 * Parameters.
 *     pScrn	:Handle to ScreenPtr structure.
 *     mode		:mode is used by vgaHWSaveScren to check blnak os on.
 * 												
 * Returns		:TRUE on success and FALSE on failure.
 *
 * Comments     :none
 *----------------------------------------------------------------------------
 */
static Bool
GXSaveScreen(ScreenPtr pScrn, int mode)
{
    ScrnInfoPtr pScrni = xf86Screens[pScrn->myNum];
   GeodePtr pGeode = GEODEPTR(pScrni);

   if (pGeode->useVGA && !pScrni->vtSema)
        return vgaHWSaveScreen(pScrn, mode);
    return TRUE;
}

/*----------------------------------------------------------------------------
 * get_flatpanel_info.
 *
 * Description:		This gets the parameter values of the flatpanel attached.
 *
 * Parameters:
 *		options:	Pointer to the display options.
 *		W:			Pointer to the width of the panel
 *		H:			Pointer to the height of the panel
 * 	    D: 			Pointer to the depth of the panel.
 *      C: 			Pointer to the color of the panel. 
 *      T: 			Pointer to the type of the panel. 
 *
 * Returns: 		none.
 *
 * Comments:		none
 *------------------------------------------------------------------------
 */
void
get_flatpanel_info(const char *options, int *W, int *H,
    int *D, int *C, int *T)
{
    char *pnl_opt;

    pnl_opt = strtok((char *)options, ":");
    *W = strtoul(pnl_opt, NULL, 0);
    pnl_opt = strtok(NULL, ":");
    *H = strtoul(pnl_opt, NULL, 0);
    pnl_opt = strtok(NULL, ":");
    *D = strtoul(pnl_opt, NULL, 0);
    pnl_opt = strtok(NULL, ":");
    *C = strtoul(pnl_opt, NULL, 0);
    pnl_opt = strtok(NULL, ":");
    *T = strtoul(pnl_opt, NULL, 0);

    *C = (*C) ? PNL_COLOR_PANEL : PNL_MONO_PANEL;

    switch (*T) {
    case 0:
        *T = PNL_SSTN;
        break;
    case 1:
        *T = PNL_DSTN;
        break;
    case 2:
    default:
        *T = PNL_TFT;
        break;
    }

    if ((*W != 640) && (*W != 800) && (*W != 1024))
        *W = 640;

    if ((*H != 480) && (*H != 600) && (*H != 768))
        *H = 480;
}

static xf86MonPtr
GXProbeDDC(ScrnInfoPtr pScrni, int index)
{
    vbeInfoPtr pVbe;
    xf86MonPtr ddc = NULL;

    if (xf86LoadSubModule(pScrni, "vbe")) {
        pVbe = VBEInit(NULL, index);
        ddc = vbeDoEDID(pVbe, NULL);
        vbeFree(pVbe);
    }
    return ddc;
}

static void
GXDecodeDDC(ScrnInfoPtr pScrni, xf86MonPtr ddc)
{
    int i;

    DEBUGMSG(1, (0, X_INFO,
            "Detected monitor DDC (%4s) id %d serno %d week %d year %d "
            "nsects %d\n",
            &ddc->vendor.name[0], ddc->vendor.prod_id, ddc->vendor.serial,
            ddc->vendor.week, ddc->vendor.year, ddc->no_sections));
    for (i = 0; i < DET_TIMINGS; ++i) {
        struct detailed_monitor_section *dp = &ddc->det_mon[i];

        switch (dp->type) {
        case DT:
            {
                struct detailed_timings *r = &dp->section.d_timings;

                DEBUGMSG(1, (0, X_INFO, "  mon det timing %0.3f  %dx%d\n",
                        r->clock / 1000000.0, r->h_active, r->v_active));
                DEBUGMSG(1, (0, X_INFO,
                        "  mon border %d,%d laced %d stereo %d sync %d, misc"
                        " %d\n",
                        r->h_border, r->v_border, r->interlaced, r->stereo,
                        r->sync, r->misc));
            }
            break;
        case DS_SERIAL:
            {
                char *serial_no = (char *)&dp->section.serial[0];

                DEBUGMSG(1, (0, X_INFO, "  mon serial %13s\n", serial_no));
            }
            break;
        case DS_ASCII_STR:
            {
                char *ascii = (char *)&dp->section.ascii_data[0];

                DEBUGMSG(1, (0, X_INFO, "  mon ascii_str %13s\n", ascii));
            }
            break;
        case DS_NAME:
            {
                char *name = (char *)&dp->section.name[0];

                DEBUGMSG(1, (0, X_INFO, "  mon name %13s\n", name));
            }
            break;
        case DS_RANGES:
            {
                struct monitor_ranges *r = &dp->section.ranges;

                DEBUGMSG(1, (0, X_INFO,
                        "  mon ranges v %d-%d h %d-%d clk %d\n", r->min_v,
                        r->max_v, r->min_h, r->max_h, r->max_clock));
            }
            break;
        case DS_WHITE_P:
            {
                struct whitePoints *wp = &dp->section.wp[0];

                DEBUGMSG(1, (0, X_INFO,
                        "  mon whitept %f,%f %f,%f idx %d,%d gamma %f,%f\n",
                        wp[1].white_x, wp[1].white_y, wp[2].white_x,
                        wp[2].white_y, wp[1].index, wp[2].index,
                        wp[1].white_gamma, wp[2].white_gamma));
            }
            break;
        case DS_STD_TIMINGS:
            {
                struct std_timings *t = &dp->section.std_t[0];

                DEBUGMSG(1, (0, X_INFO,
                        "  mon std_timing no   size   @rate, id\n"));
                for (i = 0; i < 5; ++i)
                    DEBUGMSG(1, (0, X_INFO,
                            "                 %d %5dx%-5d @%-4d %d\n", i,
                            t[i].hsize, t[i].vsize, t[i].refresh, t[i].id));
            }
            break;
        }
    }
}

/*----------------------------------------------------------------------------
 * GXPreInit.
 *
 * Description	:This function is called only once ate teh server startup
 *
 * Parameters.
 *  	pScrni 	:Handle to ScreenPtr structure.
 *  flags       :flags may be used to check the probeed one with config.
 * 												
 * Returns		:TRUE on success and FALSE on failure.
 *
 * Comments     :none.
 *----------------------------------------------------------------------------
 */
static Bool
GXPreInit(ScrnInfoPtr pScrni, int flags)
{
    ClockRangePtr GeodeClockRange;
    MessageType from;
    int i = 0;
    GeodeRec *pGeode;
    char *mod = NULL;
    xf86MonPtr ddc = NULL;
    OptionInfoRec *GeodeOptions = &GX_GeodeOptions[0];

#if CFB
    char *reqSymbol = NULL;
#endif /* CFB */
#if INT10_SUPPORT
    VESARec *pVesa;
#endif
    unsigned int PitchInc = 0, minPitch = 0, maxPitch = 0;
    unsigned int minHeight = 0, maxHeight = 0, maxWidth = 0;
    unsigned int SupportFlags;
    const char *s;
    char **modes;

    /*
     * Setup the ClockRanges, which describe what clock ranges are
     * available, and what sort of modes they can be used for.
     */
    GeodeClockRange = (ClockRangePtr) xnfcalloc(sizeof(ClockRange), 1);
    GeodeClockRange->next = NULL;
    GeodeClockRange->minClock = 25175;
    GeodeClockRange->maxClock = 229500;
    GeodeClockRange->clockIndex = -1;  /* programmable */
    GeodeClockRange->interlaceAllowed = TRUE;
    GeodeClockRange->doubleScanAllowed = FALSE; /* XXX check this */

    DEBUGMSG(1, (0, X_INFO, "GXPreInit!\n"));
    /* Allocate driver private structure */
    if (!(pGeode = GXGetRec(pScrni)))
        return FALSE;

   if (pScrni->numEntities != 1) 
     return FALSE;

        pGeode->pEnt = xf86GetEntityInfo(pScrni->entityList[i]);
        if (pGeode->pEnt->resources)
            return FALSE;
        pGeode->Chipset = pGeode->pEnt->chipset;
        pScrni->chipset = (char *)xf86TokenToString(GeodeChipsets,
            pGeode->pEnt->chipset);


   /* DEBUGMSG(1, (pScrni->scrnIndex, X_ERROR, "PROBEDDC\n")); */

   /* Note that we can't do this without VGA */

   if (flags & PROBE_DETECT != 0) {
	ConfiguredMonitor = GXProbeDDC(pScrni, pGeode->pEnt->index);
        return TRUE;
    }

   /* Hardware detection */

    pGeode->cpu_version = gfx_detect_cpu();

   if ((pGeode->cpu_version & 0xFF) == GFX_CPU_REDCLOUD) {
	   int ret;
        Q_WORD msrValue;
	   pGeode->DetectedChipSet = GX;

	   /* See if this a CRT or TFT part */

        ret = gfx_msr_read(RC_ID_DF, MBD_MSR_CONFIG, &msrValue);

	/* We depend heavily on the MSR working, so if it doesn't, there
	 * isn't much we can do but complain and move on
	 */

	if (ret != 0) {
		DEBUGMSG(1, (pScrni->scrnIndex, X_ERROR,
					"MSR read failed (ret=%d)\n", ret));
	}

        pGeode->DetectedChipSet =
            ((msrValue.low & RCDF_CONFIG_FMT_MASK) ==
            RCDF_CONFIG_FMT_FP) ? GX_TFT : GX_CRT;

    }

    pGeode->vid_version = gfx_detect_video();
    pGeode->FBLinearAddr = gfx_get_frame_buffer_base();

    /* update the max clock from the one system suports  */
    GeodeClockRange->maxClock = gfx_get_max_supported_pixel_clock();


   /* Set Durango register pointers */

    if (pGeode->DetectedChipSet & GX) {
        pGeode->cpu_reg_size = 0x4000;
        pGeode->gp_reg_size = 0x4000;
        pGeode->vid_reg_size = 0x4000;
    } else {
        pGeode->cpu_reg_size = 0x9000;
        pGeode->vid_reg_size = 0x1000;
    }


   pGeode->FBVGAActive = 0;

    /* Fill in the monitor field */
    pScrni->monitor = pScrni->confScreen->monitor;

    SupportFlags = Support24bppFb | Support32bppFb;

    if (!xf86SetDepthBpp(pScrni, 8, 8, 8, SupportFlags)) {
        return FALSE;
    } else {
        if (!((pScrni->depth == 8) ||
                (pScrni->depth == 16) ||
                (pScrni->depth == 24) || (pScrni->depth == 32))) {
            DEBUGMSG(1, (pScrni->scrnIndex, X_ERROR,
                    "Given depth (%d bpp) is not supported by this driver\n",
                    pScrni->depth));
            return FALSE;
        }
    }

    /*This must happen after pScrni->display has been set
     * * because xf86SetWeight references it.
     */
    if (pScrni->depth > 8) {

        rgb BitsPerComponent = { 0, 0, 0 };
        rgb BitMask = { 0, 0, 0 };

        if (pScrni->depth > 16) {

            BitsPerComponent.red = 8;
            BitsPerComponent.green = 8;
            BitsPerComponent.blue = 8;

            BitMask.red = 0xFF0000;
            BitMask.green = 0x00FF00;
            BitMask.blue = 0x0000FF;
        }
        if (!xf86SetWeight(pScrni, BitsPerComponent, BitMask)) {
            return FALSE;
        } else {
            /* XXX Check if the returned weight is supported */
        }
    }

    xf86PrintDepthBpp(pScrni);

    if (!xf86SetDefaultVisual(pScrni, -1))
        return FALSE;

    if (pScrni->depth > 1) {
        Gamma zeros = { 0.0, 0.0, 0.0 };

        if (!xf86SetGamma(pScrni, zeros)) {
            return FALSE;
        }
    }

    /* We use a programmable clock */
    pScrni->progClock = TRUE;

    xf86CollectOptions(pScrni, NULL);

   xf86ProcessOptions(pScrni->scrnIndex, pScrni->options,
		      GeodeOptions);

   pGeode->useVGA = TRUE;

   if (xf86ReturnOptValBool(GeodeOptions, GX_OPTION_NOVGA, FALSE))
	pGeode->useVGA = FALSE;

    xf86DrvMsg(pScrni->scrnIndex, X_DEFAULT,
        "useVGA=%d\n", pGeode->useVGA);


   if (pGeode->useVGA) {
#if INT10_SUPPORT
     if (!xf86LoadSubModule(pScrni, "int10"))
       return FALSE;
     xf86LoaderReqSymLists(amdInt10Symbols, NULL);

    pVesa = pGeode->vesa;

     if ((pVesa->pInt = xf86InitInt10(pGeode->pEnt->index)) == NULL) {
         DEBUGMSG(1, (pScrni->scrnIndex, X_ERROR,
                  "Unable to initialize INT10 support\n"));
         return FALSE;
     }
#endif

     if (!xf86LoadSubModule(pScrni, "vgahw"))
         return FALSE;

     xf86LoaderReqSymLists(amdVgahwSymbols, NULL);

     pGeode->FBAvail = gfx_get_frame_buffer_size();
   }
   else {
     if (!xf86GetOptValInteger(GeodeOptions, GX_OPTION_FBSIZE,
			       &(pGeode->FBAvail)))
       pGeode->FBAvail = 0x800000;
   }

   if (!GXMapMem(pScrni))
      return FALSE;

   pGeode->FBVGAActive = FALSE;

   if (pGeode->useVGA)
       pGeode->FBVGAActive = gu2_get_vga_active();

    /*Set the bits per RGB for 8bpp mode */

   if (pScrni->depth == 8)
        pScrni->rgbBits = 8;

    from = X_DEFAULT;

    /*
     * *The preferred method is to use the "hw cursor" option as a tri-state
     * *option, with the default set above.
     */
    pGeode->HWCursor = TRUE;

   if (xf86GetOptValBool(GeodeOptions, GX_OPTION_HW_CURSOR, &pGeode->HWCursor)) {
        from = X_CONFIG;
    }
    /* For compatibility, accept this too (as an override) */
    if (xf86ReturnOptValBool(GeodeOptions, GX_OPTION_SW_CURSOR, FALSE)) {
        from = X_CONFIG;
        pGeode->HWCursor = FALSE;
    }

    pGeode->Compression = TRUE;
    if (xf86ReturnOptValBool(GeodeOptions, GX_OPTION_NOCOMPRESSION, FALSE)) {
        pGeode->Compression = FALSE;
    }

    pGeode->NoAccel = FALSE;
    if (xf86ReturnOptValBool(GeodeOptions, GX_OPTION_NOACCEL, FALSE)) {
        pGeode->NoAccel = TRUE;
    }

    if (!xf86GetOptValInteger(GeodeOptions, GX_OPTION_OSM_IMG_BUFS,
            &(pGeode->NoOfImgBuffers)))
        pGeode->NoOfImgBuffers = DEFAULT_IMG_LINE_BUFS;

    if (pGeode->NoOfImgBuffers <= 0)
        pGeode->NoOfImgBuffers = 0;

    if (!xf86GetOptValInteger(GeodeOptions, GX_OPTION_OSM_CLR_BUFS,
            &(pGeode->NoOfColorExpandLines)))
        pGeode->NoOfColorExpandLines = DEFAULT_CLR_LINE_BUFS;

    if (pGeode->NoOfColorExpandLines <= 0)
        pGeode->NoOfColorExpandLines = 0;


#ifdef XF86EXA
    if (!xf86GetOptValInteger(GeodeOptions, GX_OPTION_EXA_SCRATCH_BFRSZ,
            &(pGeode->exaBfrSz)))
        pGeode->exaBfrSz = DEFAULT_EXA_SCRATCH_BFRSZ;

    if (pGeode->exaBfrSz <= 0)
        pGeode->exaBfrSz = 0;
#endif

  

    pGeode->CustomMode = FALSE;
    if (xf86ReturnOptValBool(GeodeOptions, GX_OPTION_CUSTOM_MODE, FALSE)) {
        pGeode->CustomMode = TRUE;
    }

   pGeode->Panel = FALSE;

   if (xf86ReturnOptValBool(GeodeOptions, GX_OPTION_FLATPANEL, FALSE)) {
     pGeode->Panel = TRUE;
   }

    if (pGeode->DetectedChipSet == GX_TFT) {
        pGeode->Panel = TRUE;
    }

   pGeode->dconPanel = FALSE;

   if (xf86ReturnOptValBool(GeodeOptions, GX_OPTION_DCONPANEL, FALSE)) {
	   pGeode->dconPanel = TRUE;
   }

   /* Force the Panel on if on a GX TFT part, no CRT support anyway */

   if (pGeode->DetectedChipSet == GX_TFT) {
        pGeode->Panel = TRUE;
   }

   /* If on a CRT and Panel flag set, disable Panel */
   if ((pGeode->DetectedChipSet == GX_CRT) && (pGeode->Panel)) {
       pGeode->dconPanel = FALSE;
       pGeode->Panel = FALSE;
    }

#if defined(PNL_SUP)
    /* If dconPanel is selected - then hard wire the settings */
    /* Otherwise, suck the timings out of the BIOS */

    if (pGeode->dconPanel) {
	    pGeode->FPBX = DCON_DEFAULT_XRES;
	    pGeode->FPBY = DCON_DEFAULT_YRES;
	    pGeode->FPBB = DCON_DEFAULT_BPP;
	    pGeode->FPBF = DCON_DEFAULT_REFRESH;
            DEBUGMSG(1, (pScrni->scrnIndex, X_ERROR, "DCON!\n"));
    } else if (pGeode->Panel) {
	if ((pGeode->Panel = Pnl_IsPanelEnabledInBIOS()))
		Pnl_GetPanelInfoFromBIOS(&pGeode->FPBX, &pGeode->FPBY,
		&pGeode->FPBB, &pGeode->FPBF);
    }

    if (pGeode->Panel)
       Pnl_PowerUp();
     else
       Pnl_PowerDown();
#endif

    pGeode->useEXA = FALSE;
    from = X_DEFAULT;
    if ((s = xf86GetOptValString(GeodeOptions, GX_OPTION_ACCEL_METHOD))) {
        if (!xf86NameCmp(s, "XAA")) {
            from = X_CONFIG;
            pGeode->useEXA = FALSE;
#if XF86EXA
        } else if (!xf86NameCmp(s, "EXA")) {
            from = X_CONFIG;
            pGeode->useEXA = TRUE;
#endif
        } else
            xf86DrvMsg(pScrni->scrnIndex, X_ERROR,
                "Unknown acceleration architecture - %s\n", s);
    }

    xf86DrvMsg(pScrni->scrnIndex, from,
        "Using %s acceleration architecture\n",
        pGeode->useEXA ? "EXA" : "XAA");

    pGeode->ShadowFB = FALSE;
    if (xf86ReturnOptValBool(GeodeOptions, GX_OPTION_SHADOW_FB, FALSE)) {
        pGeode->ShadowFB = TRUE;
        pGeode->NoAccel = TRUE;
    }

    pGeode->Rotate = 0;
    if ((s = xf86GetOptValString(GeodeOptions, GX_OPTION_ROTATE))) {

     if (!xf86NameCmp(s, "CW")) 
            pGeode->Rotate = 1;
      else if (!xf86NameCmp(s, "INVERT")) 
            pGeode->Rotate = 2;
      else if (!xf86NameCmp(s, "CCW")) 
            pGeode->Rotate = 3;


        if (pGeode->Rotate == 0) {
            DEBUGMSG(1, (pScrni->scrnIndex, X_CONFIG,
                    "\"%s\" is not a valid value for Option \"Rotate\"\n",
                    s));
            DEBUGMSG(1, (pScrni->scrnIndex, X_INFO,
                    "Valid options are \"CW\", \"INVERT\" or \"CCW\"\n"));
        }
    }

   if (pGeode->Rotate == 1) 
     xf86DrvMsg(pScrni->scrnIndex, X_CONFIG, "Rotating screen clockwise\n");
   else if (pGeode->Rotate == 2)
      xf86DrvMsg(pScrni->scrnIndex, X_CONFIG, "Rotating screen 180 degrees\n");
   else if (pGeode->Rotate == 3)
     xf86DrvMsg(pScrni->scrnIndex, X_CONFIG, "Rotating screen 180 counter clockwise\n");

    if (pGeode->Rotate != 0) {
     xf86DrvMsg(pScrni->scrnIndex, X_CONFIG, "Option 'Rotate' will disable acceleration and enable shadow\n");
        pGeode->NoAccel = TRUE;
        pGeode->ShadowFB = TRUE;
    }

    /* XXX Init further private data here */

    /*
     * * This shouldn't happen because such problems should be caught in
     * * GeodeProbe(), but check it just in case.
     */
    if (pScrni->chipset == NULL) {
     xf86DrvMsg(pScrni->scrnIndex, X_ERROR, "ChipID 0x%04X is not recognised\n", pGeode->Chipset);
        return FALSE;
    }

    if (pGeode->Chipset < 0) {
     xf86DrvMsg(pScrni->scrnIndex, X_ERROR, "Chipset %s is not recognised\n", pScrni->chipset);
        return FALSE;
    }

    if (pScrni->memPhysBase == 0) {
        from = X_PROBED;
        pScrni->memPhysBase = gfx_get_frame_buffer_base();
    }

    pScrni->fbOffset = 0;

    if (pGeode->pEnt->device->videoRam == 0) {
        from = X_PROBED;
        pScrni->videoRam = pGeode->FBAvail / 1024;
    } else {
        pScrni->videoRam = pGeode->pEnt->device->videoRam;
        from = X_CONFIG;
    }

    /*
     * * xf86ValidateModes will check that the mode HTotal and VTotal values
     * * don't exceed the chipset's limit if pScrni->maxHValue adn
     * * pScrni->maxVValue are set. Since our GXValidMode()
     * * already takes care of this, we don't worry about setting them here.
     */
    /* Select valid modes from those available */

    /*
     * * min pitch 1024, max 2048 (Pixel count)
     * * min height 480, max 1024 (Pixel count)
     */

    minPitch = 1024;
    maxPitch = 8192;                   /* Can support upto 1600x1200 32Bpp */
    maxWidth = 1600;
    minHeight = 400;
    maxHeight = 1200;                  /* Can support upto 1600x1200 32Bpp */


    if (pGeode->Compression) {
        if (pScrni->depth > 16) {
            PitchInc = 4096;
        } else if (pScrni->depth == 16) {
            PitchInc = 2048;
        } else {
            PitchInc = 1024;
        }
    }
    else {
	/* When compression is off - use a linear pitch */
	if (pScrni->depth < 16)
		PitchInc = 1600;
	else if (pScrni->depth == 16)
		PitchInc = 3200;
	else
		PitchInc = 6400;
    }

    PitchInc <<= 3;                    /* in bits */

    pGeode->maxWidth = maxWidth;
    pGeode->maxHeight = maxHeight;

    /* by default use what user sets in the XF86Config file */
    modes = pScrni->display->modes;

    if (ddc != NULL && pScrni->monitor != NULL
        && pScrni->monitor->DDC == NULL) {
        pScrni->monitor->DDC = ddc;
        GXDecodeDDC(pScrni, ddc);
    }

    i = xf86ValidateModes(pScrni,
        pScrni->monitor->Modes,
        modes, GeodeClockRange,
        NULL, minPitch, maxPitch,
        PitchInc, minHeight, maxHeight,
        pScrni->display->virtualX,
        pScrni->display->virtualY, pGeode->FBAvail, LOOKUP_BEST_REFRESH);

    if (i == -1) {
     xf86DrvMsg(pScrni->scrnIndex, X_ERROR, "No valid modes were found\n");
        GXFreeRec(pScrni);
        return FALSE;
    }

    /* Prune the modes marked as invalid */
    xf86PruneDriverModes(pScrni);

    if (i == 0 || pScrni->modes == NULL) {
     xf86DrvMsg(pScrni->scrnIndex, X_ERROR, "No valid modes were found\n");
        GXFreeRec(pScrni);
        return FALSE;
    }

    xf86SetCrtcForModes(pScrni, 0);

    /* Set the current mode to the first in the list */
    pScrni->currentMode = pScrni->modes;
    DEBUGMSG(1, (0, X_INFO, "GXPreInit(12)!\n"));

    /* Print the list of modes being used */
    xf86PrintModes(pScrni);

    /* Set the display resolution */
    xf86SetDpi(pScrni, 0, 0);

    /* Load bpp-specific modules */
    mod = NULL;

#if CFB
    /* Load bpp-specific modules */
    switch (pScrni->bitsPerPixel) {
    case 8:
        mod = "cfb";
        reqSymbol = "cfbScreenInit";
        break;
    case 16:
        mod = "cfb16";
        reqSymbol = "cfb16ScreenInit";
        break;
    case 24:
        mod = "cfb24";
        reqSymbol = "cfb24ScreenInit";
        break;
    case 32:
        mod = "cfb32";
        reqSymbol = "cfb32ScreenInit";
        break;
    default:
        return FALSE;
    }

    if (mod && xf86LoadSubModule(pScrni, mod) == NULL) {
        GXFreeRec(pScrni);
        return FALSE;
    }

    xf86LoaderReqSymbols(reqSymbol, NULL);
#else
    if (xf86LoadSubModule(pScrni, "fb") == NULL) {
        GXFreeRec(pScrni);
        return FALSE;
    }

    xf86LoaderReqSymLists(amdFbSymbols, NULL);
#endif

    if (pGeode->NoAccel == FALSE) {
        const char *module = "xaa";
        const char **symbols = &amdXaaSymbols[0];

#if XF86EXA
        if (pGeode->useEXA) {
            module = "exa";
            symbols = &amdExaSymbols[0];
        }
#endif
        if (!xf86LoadSubModule(pScrni, module)) {
            GXFreeRec(pScrni);
            return FALSE;
        }

        xf86LoaderReqSymLists(symbols, NULL);
    }

    if (pGeode->HWCursor == TRUE) {
        if (!xf86LoadSubModule(pScrni, "ramdac")) {
            GXFreeRec(pScrni);
            return FALSE;
        }

        xf86LoaderReqSymLists(amdRamdacSymbols, NULL);
    }

    /* Load shadowfb if needed */
    if (pGeode->ShadowFB) {
        if (!xf86LoadSubModule(pScrni, "shadowfb")) {
            GXFreeRec(pScrni);
            return FALSE;
        }

        xf86LoaderReqSymLists(amdShadowSymbols, NULL);
    }

    if (xf86RegisterResources(pGeode->pEnt->index, NULL, ResExclusive)) {
        DEBUGMSG(1, (pScrni->scrnIndex, X_ERROR,
                "xf86RegisterResources() found resource conflicts\n"));
        GXFreeRec(pScrni);
        return FALSE;
    }

    GXUnmapMem(pScrni);
    return TRUE;
}

/*----------------------------------------------------------------------------
 * GXRestore.
 *
 * Description	:This function restores the mode that was saved on server
                 entry
 * Parameters.
 * 	pScrni 		:Handle to ScreenPtr structure.
 *  Pmode       :poits to screen mode
 * 												
 * Returns		:none.
 *
 * Comments     :none.
 *----------------------------------------------------------------------------
 */
static void
GXRestore(ScrnInfoPtr pScrni)
{
    GeodeRec *pGeode = GEODEPTR(pScrni);

   if (pGeode->useVGA && pGeode->FBVGAActive) {
        vgaHWPtr pvgaHW = VGAHWPTR(pScrni);

        vgaHWProtect(pScrni, TRUE);
        vgaHWRestore(pScrni, &pvgaHW->SavedReg, VGA_SR_ALL);
        vgaHWProtect(pScrni, FALSE);
    }
}

/*----------------------------------------------------------------------------
 * GXCalculatePitchBytes.
 *
 * Description	:This function restores the mode that was saved on server
 *
 * Parameters.
 * 		pScrni 	:Handle to ScreenPtr structure.
 *   	Pmode   :Points to screenmode
 * 									
 * Returns		:none.
 *
 * Comments     :none.
 *----------------------------------------------------------------------------
 */
static int
GXCalculatePitchBytes(unsigned int width, unsigned int bpp)
{
    int lineDelta = width * (bpp >> 3);

    if (width < 640) {
        /* low resolutions have both pixel and line doubling */
        DEBUGMSG(1, (0, X_PROBED, "lower resolution %d %d\n",
                width, lineDelta));
        lineDelta <<= 1;
    }
    /* needed in Rotate mode when in accel is turned off */
    if (1) {                           /*!pGeode->NoAccel */
        if (lineDelta > 4096)
            lineDelta = 8192;
        else if (lineDelta > 2048)
            lineDelta = 4096;
        else if (lineDelta > 1024)
            lineDelta = 2048;
        else
            lineDelta = 1024;
    }

    DEBUGMSG(1, (0, X_PROBED, "pitch %d %d\n", width, lineDelta));

    return lineDelta;
}

/*----------------------------------------------------------------------------
 * GXGetRefreshRate.
 *
 * Description	:This function restores the mode that saved on server
 *
 * Parameters.
 *     Pmode    :Pointer to the screen modes
 * 												
 * Returns		:It returns the selected refresh rate.
 *
 * Comments     :none.
 *----------------------------------------------------------------------------
 */
static int
GXGetRefreshRate(DisplayModePtr pMode)
{
#define THRESHOLD 2
    unsigned int i;
    static int validRates[] = { 56, 60, 70, 72, 75, 85, 90, 100 };      /* Hz */
    unsigned long dotClock;
    int refreshRate;
    int selectedRate;

    dotClock = pMode->SynthClock * 1000;
    refreshRate = dotClock / pMode->CrtcHTotal / pMode->CrtcVTotal;

    if ((pMode->CrtcHTotal < 640) && (pMode->CrtcVTotal < 480))
        refreshRate >>= 2;             /* double pixel and double scan */

    DEBUGMSG(1, (0, X_PROBED, "dotclock %lu %d\n", dotClock, refreshRate));

    selectedRate = validRates[0];

    for (i = 0; i < (sizeof(validRates) / sizeof(validRates[0])); i++) {
        if (validRates[i] < (refreshRate + THRESHOLD)) {
            selectedRate = validRates[i];
        }
    }

    return selectedRate;
}

void
gx_clear_screen(ScrnInfoPtr pScrni, int width, int height, int bpp)
{
    /* no accels, mode is not yet set */
    GeodeRec *pGeode = GEODEPTR(pScrni);
    unsigned long offset = gfx_get_display_offset();
    unsigned long pitch = gfx_get_display_pitch();
    unsigned long n = width * ((bpp + 7) >> 3);

    DEBUGMSG(1, (0, X_PROBED, "clear screen %lx %d %d %d %ld %ld\n", offset,
            width, height, bpp, pitch, n));
    while (height > 0) {
        memset(pGeode->FBBase + offset, 0, n);
        offset += pitch;
        --height;
    }
}

void
gx_clear_fb(ScrnInfoPtr pScrni)
{
    GeodeRec *pGeode = GEODEPTR(pScrni);
    unsigned char *fb = pGeode->FBBase + pGeode->FBOffset;

    memset(fb, 0, pGeode->FBSize);
    if (pGeode->ShadowPtr != NULL && pGeode->ShadowPtr != fb)
        memset(pGeode->ShadowPtr, 0, pGeode->ShadowSize);
}

void
gx_set_DvLineSize(unsigned int pitch)
{
    unsigned long temp, dv_size = MDC_DV_LINE_SIZE_1024;

    if (pitch > 1024) {
        dv_size = MDC_DV_LINE_SIZE_2048;
    }
    if (pitch > 2048) {
        dv_size = MDC_DV_LINE_SIZE_4096;
    }
    if (pitch > 4096) {
        dv_size = MDC_DV_LINE_SIZE_8192;
    }

    /* WRITE DIRTY/VALID CONTROL WITH LINE LENGTH */

    temp = READ_REG32(MDC_DV_CTL);
    WRITE_REG32(MDC_DV_CTL, (temp & ~MDC_DV_LINE_SIZE_MASK) | dv_size);
}

/*----------------------------------------------------------------------------
 * GXSetMode.
 *
 * Description	:This function sets parametrs for screen mode
 *
 * Parameters.
 * 		pScrni 	:Pointer to the screenInfo structure.
 *	 	Pmode   :Pointer to the screen modes
 * 												
 * Returns		:TRUE on success and FALSE on Failure.
 *
 * Comments     :none.
 *----------------------------------------------------------------------------
 */

static Bool
GXSetMode(ScrnInfoPtr pScrni, DisplayModePtr pMode)
{
    int flags;
    static char *quals[4] = {
        " +hsync +vsync", " -hsync +vsync", " +hsync -vsync", " -hsync -vsync"
    };
    GeodeRec *pGeode = GEODEPTR(pScrni);

    gfx_wait_until_idle();
    /* disable video */
    gx_disable_dac_power(pScrni);

    /* unsigned int compOffset, compPitch, compSize; */
    DEBUGMSG(1, (0, X_INFO, "GXSetMode %p %p %p %p %p\n",
            gfx_virt_regptr, gfx_virt_gpptr,
            gfx_virt_spptr, gfx_virt_vidptr, gfx_virt_fbptr));

    /* Set the VT semaphore */
    pScrni->vtSema = TRUE;

    /* The timing will be adjusted later */
    flags = 0;
    if ((pMode->Flags & V_NHSYNC) != 0)
        flags |= 1;

    if ((pMode->Flags & V_NVSYNC) != 0)
        flags |= 2;

    DEBUGMSG(1, (0, X_PROBED,
            "Setting mode %dx%d %0.3f  %d %d %d %d  %d %d %d %d%s\n",
            pMode->CrtcHDisplay, pMode->CrtcVDisplay,
            pMode->SynthClock / 1000.0, pMode->CrtcHDisplay,
            pMode->CrtcHSyncStart, pMode->CrtcHSyncEnd, pMode->CrtcHTotal,
            pMode->CrtcVDisplay, pMode->CrtcVSyncStart, pMode->CrtcVSyncEnd,
            pMode->CrtcVTotal, quals[flags]));
    DEBUGMSG(1, (0, X_INFO, "Set display mode: %dHz Pitch %d\n",
            GXGetRefreshRate(pMode), pGeode->Pitch));

    /* TV not selected */
    DEBUGMSG(1, (0, X_PROBED, "Setting Display for CRT or TFT\n"));
    if (pGeode->CustomMode != 0) {
        DEBUGMSG(1, (0, X_PROBED, "Setting Custom mode\n"));
        GFX(set_display_timings(pScrni->bitsPerPixel, flags,
                pMode->CrtcHDisplay, pMode->CrtcHBlankStart,
                pMode->CrtcHSyncStart, pMode->CrtcHSyncEnd,
                pMode->CrtcHBlankEnd, pMode->CrtcHTotal,
                pMode->CrtcVDisplay, pMode->CrtcVBlankStart,
                pMode->CrtcVSyncStart, pMode->CrtcVSyncEnd,
                pMode->CrtcVBlankEnd, pMode->CrtcVTotal,
                (int)((pMode->SynthClock / 1000.0) * 0x10000)));
    } else if (pGeode->Panel != 0) {
        DEBUGMSG(0, (0, X_PROBED, "Setting Display for TFT\n"));
        DEBUGMSG(1, (0, X_PROBED, "Restore Panel %d %d %d %d %d\n",
                pGeode->FPBX, pGeode->FPBY,
                pMode->CrtcHDisplay,
                pMode->CrtcVDisplay, pScrni->bitsPerPixel));

        DEBUGMSG(1, (pScrni->scrnIndex, X_CONFIG, "FP Bios %d\n",
                pGeode->Panel));
        GFX(set_fixed_timings(pGeode->FPBX, pGeode->FPBY,
                pMode->CrtcHDisplay,
                pMode->CrtcVDisplay, pScrni->bitsPerPixel));
    } else {
        /* display is crt */
        DEBUGMSG(1, (0, X_PROBED,
                "Setting Display for CRT %dx%d-%d@%d\n",
                pMode->CrtcHDisplay, pMode->CrtcVDisplay,
                pScrni->bitsPerPixel, GXGetRefreshRate(pMode)));
        GFX(set_display_mode(pMode->CrtcHDisplay, pMode->CrtcVDisplay,
                pScrni->bitsPerPixel, GXGetRefreshRate(pMode)));

        /* adjust the pitch */
    }

    /* enable crt */
    GFX(set_crt_enable(CRT_ENABLE));
    GFX(set_display_pitch(pGeode->Pitch));
    GFX(set_display_offset(0L));
    GFX(wait_vertical_blank());

    DEBUGMSG(1, (0, X_PROBED, "Display mode set\n"));
    /* enable compression if option selected */
    if (pGeode->Compression) {
        DEBUGMSG(1, (0, X_PROBED, "Compression mode set %d\n",
                pGeode->Compression));
        /* set the compression parameters,and it will be turned on later. */
        gx_set_DvLineSize(pGeode->Pitch);

        gfx_set_compression_offset(pGeode->CBOffset);
        gfx_set_compression_pitch(pGeode->CBPitch);
        gfx_set_compression_size(pGeode->CBSize);

        /* set the compression buffer, all parameters already set */
        gfx_set_compression_enable(1);
    }

    if (pGeode->HWCursor) {
        /* Load blank cursor */
        GXLoadCursorImage(pScrni, NULL);
        GFX(set_cursor_position(pGeode->CursorStartOffset, 0, 0, 0, 0));
        GFX(set_cursor_enable(1));
   } else {
      DEBUGMSG(1,(0, X_INFO, "GXRestore ... "));
      GXRestore(pScrni);
      DEBUGMSG(1,(0, X_INFO, "done.\n"));
   }

    DEBUGMSG(1, (0, X_INFO, "done.\n"));
    /* Reenable the hardware cursor after the mode switch */
    if (pGeode->HWCursor == TRUE) {
        DEBUGMSG(1, (0, X_INFO, "GXShowCursor ... "));
        GXShowCursor(pScrni);
        DEBUGMSG(1, (0, X_INFO, "done.\n"));
    }

    if (!pGeode->Panel)
        GFX(set_display_offset(pGeode->PrevDisplayOffset));

    /* Restore the contents in the screen info */
    DEBUGMSG(1, (0, X_INFO, "After setting the mode\n"));
    switch (pGeode->Rotate) {
    case 1:
    case 3:
        pGeode->HDisplay = pMode->VDisplay;
        pGeode->VDisplay = pMode->HDisplay;
        break;
    default:
        pGeode->HDisplay = pMode->HDisplay;
        pGeode->VDisplay = pMode->VDisplay;
        break;
    }
    gx_enable_dac_power(pScrni);
    return TRUE;
}

/*----------------------------------------------------------------------------
 * GXEnterGraphics.
 *
 * Description	:This function will intiallize the displaytiming
				 structure for nextmode and switch to VGA mode.
 *
 * Parameters.
 *   	pScrn  	:Screen information will be stored in this structure.
 * 		pScrni	:Pointer to the screenInfo structure.
 *													
 * Returns		:TRUE on success and FALSE on Failure.
 *
 * Comments     :gfx_vga_mode_switch() will start and end the
 *				 switching based on the arguments 0 or 1.soft_vga
 *				 is disabled in this function.
 *----------------------------------------------------------------------------
 */
static Bool
GXEnterGraphics(ScreenPtr pScrn, ScrnInfoPtr pScrni)
{
    GeodeRec *pGeode = GEODEPTR(pScrni);

    gfx_wait_until_idle();

    DEBUGMSG(1, (0, X_INFO, "GXEnterGraphics(1)!\n"));

    /* Save CRT State */
    pGeode->FBgfxdisplaytiming.dwDotClock = gfx_get_clock_frequency();
    pGeode->FBgfxdisplaytiming.wPitch = gfx_get_display_pitch();
    pGeode->FBgfxdisplaytiming.wBpp = gfx_get_display_bpp();
    pGeode->FBgfxdisplaytiming.wHTotal = gfx_get_htotal();
    pGeode->FBgfxdisplaytiming.wHActive = gfx_get_hactive();
    pGeode->FBgfxdisplaytiming.wHSyncStart = gfx_get_hsync_start();
    pGeode->FBgfxdisplaytiming.wHSyncEnd = gfx_get_hsync_end();
    pGeode->FBgfxdisplaytiming.wHBlankStart = gfx_get_hblank_start();
    pGeode->FBgfxdisplaytiming.wHBlankEnd = gfx_get_hblank_end();
    pGeode->FBgfxdisplaytiming.wVTotal = gfx_get_vtotal();
    pGeode->FBgfxdisplaytiming.wVActive = gfx_get_vactive();
    pGeode->FBgfxdisplaytiming.wVSyncStart = gfx_get_vsync_start();
    pGeode->FBgfxdisplaytiming.wVSyncEnd = gfx_get_vsync_end();
    pGeode->FBgfxdisplaytiming.wVBlankStart = gfx_get_vblank_start();
    pGeode->FBgfxdisplaytiming.wVBlankEnd = gfx_get_vblank_end();
    pGeode->FBgfxdisplaytiming.wPolarity = gfx_get_sync_polarities();

    /* Save Display offset */
    pGeode->FBDisplayOffset = gfx_get_display_offset();

    DEBUGMSG(1, (0, X_INFO, "GXEnterGraphics(2)!\n"));

    if (pGeode->useVGA) {
      vgaHWPtr pvgaHW = VGAHWPTR(pScrni);
      pGeode->FBBIOSMode = pvgaHW->readCrtc(pvgaHW, 0x040);
    }

    /* Save the current Compression state */
    pGeode->FBCompressionEnable = gfx_get_compression_enable();
    pGeode->FBCompressionOffset = gfx_get_compression_offset();
    pGeode->FBCompressionPitch = gfx_get_compression_pitch();
    pGeode->FBCompressionSize = gfx_get_compression_size();

    /* Save Cursor offset */
    pGeode->FBCursorOffset = gfx_get_cursor_offset();

#if defined(PNL_SUP)
    /* Save the Panel state */
    DEBUGMSG(1, (0, X_INFO, "GXEnterGraphics(3)!\n"));
    Pnl_SavePanelState();
#endif

    /* only if comming from VGA */
   if (pGeode->useVGA && pGeode->FBVGAActive) {
        unsigned short sequencer;
        vgaHWPtr pvgaHW = VGAHWPTR(pScrni);

        /* Map VGA aperture */
        if (!vgaHWMapMem(pScrni))
            return FALSE;

        /* Unlock VGA registers */
        vgaHWUnlock(pvgaHW);

        /* Save the current state and setup the current mode */
        vgaHWSave(pScrni, &VGAHWPTR(pScrni)->SavedReg, VGA_SR_ALL);

        /* DISABLE VGA SEQUENCER */
        /* This allows the VGA state machine to terminate. We must delay */
        /* such that there are no pending MBUS requests.  */

        gfx_outb(MDC_SEQUENCER_INDEX, MDC_SEQUENCER_CLK_MODE);
        sequencer = gfx_inb(MDC_SEQUENCER_DATA);
        sequencer |= MDC_CLK_MODE_SCREEN_OFF;
        gfx_outb(MDC_SEQUENCER_DATA, sequencer);

        gfx_delay_milliseconds(1);

        /* BLANK THE VGA DISPLAY */
        gfx_outw(MDC_SEQUENCER_INDEX, MDC_SEQUENCER_RESET);
        sequencer = gfx_inb(MDC_SEQUENCER_DATA);
        sequencer &= ~MDC_RESET_VGA_DISP_ENABLE;
        gfx_outb(MDC_SEQUENCER_DATA, sequencer);

        gfx_delay_milliseconds(1);
    }

    gx_clear_fb(pScrni);

    if (!GXSetMode(pScrni, pScrni->currentMode)) {
        return FALSE;
    }

    gx_enable_dac_power(pScrni);
    DEBUGMSG(1, (0, X_INFO, "GXEnterGraphics(4)!\n"));
    return TRUE;
}

void
gx_enable_dac_power(ScrnInfoPtr pScrni)
{
    /* enable the DAC POWER */
    gfx_write_vid32(RCDF_VID_MISC,
        gfx_read_vid32(RCDF_VID_MISC) & RCDF_GAMMA_BYPASS_BOTH);
}

void
gx_disable_dac_power(ScrnInfoPtr pScrni)
{
    /* disable the DAC POWER */
    gfx_write_vid32(RCDF_VID_MISC,
        RCDF_DAC_POWER_DOWN | RCDF_ANALOG_POWER_DOWN |
        (gfx_read_vid32(RCDF_VID_MISC) & RCDF_GAMMA_BYPASS_BOTH));
}

/*----------------------------------------------------------------------------
 * GXLeaveGraphics:
 *
 * Description	:This function will restore the displaymode parameters
 * 				 and switches the VGA mode
 *
 * Parameters.
 *    	pScrn 	:Screen information will be stored in this structure.
 * 		pScrni 	:Pointer to the screenInfo structure.
 * 												
 * Returns		:none.
 *
 * Comments		: gfx_vga_mode_switch() will start and end the switching
 *			     based on the arguments 0 or 1.soft_vga is disabled in
 *               this function.
 *----------------------------------------------------------------------------
 */
static void
GXLeaveGraphics(ScrnInfoPtr pScrni)
{
    GeodeRec *pGeode = GEODEPTR(pScrni);

    gfx_wait_until_idle();

    /* Restore VG registers */
    gfx_set_display_timings(pGeode->FBgfxdisplaytiming.wBpp,
        pGeode->FBgfxdisplaytiming.wPolarity,
        pGeode->FBgfxdisplaytiming.wHActive,
        pGeode->FBgfxdisplaytiming.wHBlankStart,
        pGeode->FBgfxdisplaytiming.wHSyncStart,
        pGeode->FBgfxdisplaytiming.wHSyncEnd,
        pGeode->FBgfxdisplaytiming.wHBlankEnd,
        pGeode->FBgfxdisplaytiming.wHTotal,
        pGeode->FBgfxdisplaytiming.wVActive,
        pGeode->FBgfxdisplaytiming.wVBlankStart,
        pGeode->FBgfxdisplaytiming.wVSyncStart,
        pGeode->FBgfxdisplaytiming.wVSyncEnd,
        pGeode->FBgfxdisplaytiming.wVBlankEnd,
        pGeode->FBgfxdisplaytiming.wVTotal,
        pGeode->FBgfxdisplaytiming.dwDotClock);

    gfx_set_compression_enable(0);

    /* Restore the previous Compression state */
    if (pGeode->FBCompressionEnable) {
        gfx_set_compression_offset(pGeode->FBCompressionOffset);
        gfx_set_compression_pitch(pGeode->FBCompressionPitch);
        gfx_set_compression_size(pGeode->FBCompressionSize);
        gfx_set_compression_enable(1);
    }

    gfx_set_display_pitch(pGeode->FBgfxdisplaytiming.wPitch);

    gfx_set_display_offset(pGeode->FBDisplayOffset);

    /* Restore Cursor */
    gfx_set_cursor_position(pGeode->FBCursorOffset, 0, 0, 0, 0);

   if (pGeode->useVGA) {
    pGeode->vesa->pInt->num = 0x10;
    pGeode->vesa->pInt->ax = 0x0 | pGeode->FBBIOSMode;
    pGeode->vesa->pInt->bx = 0;
    xf86ExecX86int10(pGeode->vesa->pInt);
    gfx_delay_milliseconds(3);
   }

    GXRestore(pScrni);

    gx_enable_dac_power(pScrni);
}

/*----------------------------------------------------------------------------
 * GXCloseScreen.
 *
 * Description		:This function will restore the original mode
 *					 and also it unmap video memory
 *
 * Parameters.
 *  	ScrnIndex	:Screen index value of the screen will be closed.
 * 		pScrn    	:Pointer to the screen structure.
 *	
 * 												
 * Returns			:TRUE on success and FALSE on Failure.
 *
 * Comments			:none.
 *----------------------------------------------------------------------------
 */
static Bool
GXCloseScreen(int scrnIndex, ScreenPtr pScrn)
{
    ScrnInfoPtr pScrni = xf86Screens[scrnIndex];
    GeodeRec *pGeode = GEODEPTR(pScrni);

    if (pGeode->ShadowPtr)
        xfree(pGeode->ShadowPtr);

    DEBUGMSG(0, (scrnIndex, X_PROBED, "GXCloseScreen %d\n", pScrni->vtSema));
    if (pScrni->vtSema)
        GXLeaveGraphics(pScrni);

    if (pGeode->AccelInfoRec)
        XAADestroyInfoRec(pGeode->AccelInfoRec);

    if (pGeode->AccelImageWriteBuffers) {
#if GX_USE_OFFSCRN_MEM
        xfree(pGeode->AccelImageWriteBuffers[0]);
#endif
        xfree(pGeode->AccelImageWriteBuffers);
        pGeode->AccelImageWriteBuffers = NULL;
    }

    if (pGeode->AccelColorExpandBuffers) {
        xfree(pGeode->AccelColorExpandBuffers);
        pGeode->AccelColorExpandBuffers = NULL;
    }

#ifdef XF86EXA
    if (pGeode->pExa) {
        exaDriverFini(pScrn);
        xfree(pGeode->pExa);
        pGeode->pExa = NULL;
    }
#endif

    pScrni->vtSema = FALSE;

    GXUnmapMem(pScrni);

    if (pGeode && (pScrn->CloseScreen = pGeode->CloseScreen)) {
        pGeode->CloseScreen = NULL;
        return ((*pScrn->CloseScreen) (scrnIndex, pScrn));
    }

    return TRUE;
}

#ifdef DPMSExtension
static void
GXPanelPower(int enable)
{
    unsigned long power = READ_VID32(RCDF_POWER_MANAGEMENT);

    if (enable != 0)
        power |= RCDF_PM_PANEL_POWER_ON;
    else
        power &= ~RCDF_PM_PANEL_POWER_ON;

    WRITE_VID32(RCDF_POWER_MANAGEMENT, power);
}

/*----------------------------------------------------------------------------
 * GXDPMSSet.
 *
 * Description	:This function sets geode into Power Management
 *               Signalling mode.				
 *
 * Parameters.
 * 	pScrni	 	:Pointer to screen info strucrure.
 * 	mode        :Specifies the power management mode.
 *	 												
 * Returns		:none.
 *
 * Comments     :none.
 *----------------------------------------------------------------------------
 */
static void
GXDPMSSet(ScrnInfoPtr pScrni, int mode, int flags)
{
    GeodeRec *pGeode;

    pGeode = GEODEPTR(pScrni);

    DEBUGMSG(1, (0, X_INFO, "GXDPMSSet! %d %d\n", mode, flags));

    /* Check if we are actively controlling the display */
    if (!pScrni->vtSema) {
        ErrorF("GXDPMSSet called when we not controlling the VT!\n");
        return;
    }

    switch (mode) {
    case DPMSModeOn:
        /* Screen: On; HSync: On; VSync: On */
        GFX(set_crt_enable(CRT_ENABLE));
#if defined(PNL_SUP)
        if (pGeode->Panel) {
            Pnl_PowerUp();
            GXPanelPower(1);
        }
#endif
        break;

    case DPMSModeStandby:
        /* Screen: Off; HSync: Off; VSync: On */
        GFX(set_crt_enable(CRT_STANDBY));
#if defined(PNL_SUP)
        if (pGeode->Panel) {
            Pnl_PowerDown();
            GXPanelPower(0);
        }
#endif
        break;

    case DPMSModeSuspend:
        /* Screen: Off; HSync: On; VSync: Off */
        GFX(set_crt_enable(CRT_SUSPEND));
#if defined(PNL_SUP)
        if (pGeode->Panel) {
            Pnl_PowerDown();
            GXPanelPower(0);
        }
#endif
        break;

    case DPMSModeOff:
        /* Screen: Off; HSync: Off; VSync: Off */
        GFX(set_crt_enable(CRT_DISABLE));
#if defined(PNL_SUP)
        if (pGeode->Panel) {
            Pnl_PowerDown();
            GXPanelPower(0);
        }
#endif
        break;
    }
}
#endif

/*----------------------------------------------------------------------------
 * GXScreenInit.
 *
 * Description	:This function will be called at the each ofserver
 *   			 generation.				
 *
 * Parameters.
 *   scrnIndex  :Specfies the screenindex value during generation.
 *   pScrn	 	:Pointer to screen info strucrure.
 * 	 argc       :parameters for command line arguments count
 *	 argv       :command line arguments if any it is not used.  												
 *
 * Returns		:none.
 *
 * Comments     :none.
 *----------------------------------------------------------------------------
 */
static Bool
GXScreenInit(int scrnIndex, ScreenPtr pScrn, int argc, char **argv)
{
    int i, l, bytpp, size, fbsize, fboffset, fbavail;
    int pitch, displayWidth, virtualX, virtualY;
    int HDisplay, VDisplay, maxHDisplay, maxVDisplay, maxX, maxY;
    unsigned char *FBStart;
    unsigned char **ap, *bp;
    DisplayModePtr p;
    GeodeRec *pGeode;
    VisualPtr visual;
    BoxRec AvailBox;
    RegionRec OffscreenRegion;
    ScrnInfoPtr pScrni = xf86Screens[pScrn->myNum];
    Bool Inited = FALSE;

    pGeode = GXGetRec(pScrni);

   if (pGeode->useVGA) {
    if (!vgaHWGetHWRec(pScrni))
        return FALSE;
    if (!vgaHWMapMem(pScrni))
        return FALSE;

    vgaHWGetIOBase(VGAHWPTR(pScrni));
   }

    if (!GXMapMem(pScrni))
        return FALSE;

    /* If compression is not turned on - adjust the pitch to be linear */
    if (pGeode->Compression)
      pGeode->Pitch = GXCalculatePitchBytes(pScrni->virtualX, pScrni->bitsPerPixel);
    else
      pGeode->Pitch = ((pScrni->virtualX + 3) & ~3) * (pScrni->bitsPerPixel >> 3);

    pGeode->AccelPitch = pGeode->Pitch;
    bytpp = (pScrni->bitsPerPixel + 7) / 8;

    /* start of framebuffer for accels */
    fboffset = 0;
    /* 0x4000 for gfx_gu2_scratch_buffer */
    fbavail = pGeode->FBAvail - 0x4000;

#ifdef V4L2_VIDEO_BFR_SZ
    fbavail -= V4L2_VIDEO_BFR_SZ;
#endif

    /* allocate display frame buffer at zero offset */
    fbsize = pScrni->virtualY * pGeode->Pitch;
    pGeode->FBSize = fbsize;

    pGeode->CursorSize = 16 * 64;      /* 64x64 */
    pGeode->CursorStartOffset = 0;

    DEBUGMSG(1, (scrnIndex, X_PROBED, "%d %d %d\n",
            pScrni->virtualX, pScrni->bitsPerPixel, pGeode->Pitch));

    HDisplay = pScrni->currentMode->HDisplay;
    VDisplay = pScrni->currentMode->VDisplay;
    pGeode->orig_virtX = pScrni->virtualX;
    pGeode->orig_virtY = pScrni->virtualY;

    p = pScrni->modes;
    maxHDisplay = p->HDisplay;
    maxVDisplay = p->VDisplay;
    while ((p = p->next) != pScrni->modes) {
        if (maxHDisplay < p->HDisplay)
            maxHDisplay = p->HDisplay;

        if (maxVDisplay < p->VDisplay)
            maxVDisplay = p->VDisplay;
    }

    DEBUGMSG(1, (scrnIndex, X_PROBED, "maxHDisplay %d maxVDisplay %d\n",
            maxHDisplay, maxVDisplay));

    switch (pGeode->Rotate) {
    case 1:
    case 3:
        pGeode->HDisplay = VDisplay;
        pGeode->VDisplay = HDisplay;
        virtualX = pScrni->virtualY;
        virtualY = pScrni->virtualX;
        maxX = maxVDisplay;
        maxY = maxHDisplay;
        break;
    default:
        pGeode->HDisplay = HDisplay;
        pGeode->VDisplay = VDisplay;
        virtualX = pScrni->virtualX;
        virtualY = pScrni->virtualY;
        maxX = maxHDisplay;
        maxY = maxVDisplay;
        break;
    }

    /* shadow may be first in FB, since accels render there */

    pGeode->ShadowPtr = NULL;
    if (pGeode->ShadowFB) {
        if (!pGeode->PointerMoved) {
            pGeode->PointerMoved = pScrni->PointerMoved;
            pScrni->PointerMoved = GXPointerMoved;
        }

        if (pGeode->ShadowPtr == NULL) {
            pGeode->ShadowPitch =
                BitmapBytePad(pScrni->bitsPerPixel * virtualX);
            size = pGeode->ShadowPitch * virtualY;
            pGeode->ShadowPtr = xalloc(size);
            if (pGeode->ShadowPtr != NULL) {
                pGeode->ShadowSize = size;
                if (!pGeode->NoAccel) {
                    pGeode->NoAccel = TRUE;
                    pGeode->HWCursor = FALSE;
                    xf86DrvMsg(scrnIndex, X_ERROR,
                        "Shadow FB offscreen, All Accels disabled\n");
                }

            } else {
                xf86DrvMsg(scrnIndex, X_ERROR,
                    "Shadow FB, No offscreen Memory, disabled\n");
                pGeode->ShadowFB = FALSE;
                pGeode->Rotate = 0;
                pGeode->HDisplay = HDisplay;
                pGeode->VDisplay = VDisplay;
                virtualX = pScrni->virtualX;
                virtualY = pScrni->virtualY;
            }
        }
    }

    if (pGeode->ShadowPtr != NULL) {
        displayWidth = pGeode->ShadowPitch / bytpp;
        FBStart = pGeode->ShadowPtr;
        DEBUGMSG(1, (0, X_PROBED, "Shadow %p \n", FBStart));
    } else {
        displayWidth = pGeode->Pitch / bytpp;
        FBStart = pGeode->FBBase;
        DEBUGMSG(1, (0, X_PROBED, "FBStart %p \n", FBStart));
    }

    DEBUGMSG(1, (0, X_PROBED, "FB display %X size %X \n", fboffset, fbsize));
    pGeode->FBOffset = fboffset;       /* offset of display framebuffer */
    pScrni->fbOffset = fboffset;
    fboffset += fbsize;
    fbavail -= fbsize;

    if (pGeode->Compression) {         /* Compression enabled */
        pGeode->CBPitch = 512 + 32;
        pGeode->CBSize = 512 + 32;
        size = maxY * pGeode->CBPitch;
        DEBUGMSG(1, (0, X_PROBED, "CB %#x size %#x (%d*%d)\n", fboffset, size,
                maxY, pGeode->CBPitch));
        if (size <= fbavail) {
            pGeode->CBOffset = fboffset;
            fboffset += size;
            fbavail -= size;
        } else {
            xf86DrvMsg(scrnIndex, X_ERROR,
                "Compression, No FB Memory, disabled\n");
            pGeode->Compression = FALSE;
        }
    }

    if (pGeode->HWCursor) {            /* HWCursor enabled */
        size = pGeode->CursorSize;
        if (size <= fbavail) {
            pGeode->CursorStartOffset = fboffset;
            fboffset += size;
            fbavail -= size;
        } else {
            xf86DrvMsg(scrnIndex, X_ERROR,
                "HWCursor, No FB Memory, disabled\n");
            pGeode->HWCursor = FALSE;
        }
    }
#if XF86EXA
    if (!pGeode->NoAccel && pGeode->useEXA) {   /* exa acceleration enabled */
        if (!(pGeode->pExa = xnfcalloc(sizeof(ExaDriverRec), 1))) {
            xf86DrvMsg(scrnIndex, X_ERROR,
                "No ExaDriverRec Memory, disabled\n");
            pGeode->NoAccel = TRUE;
        } else {
            if ((size = pGeode->exaBfrSz) > 0 && size <= fbavail) {
                pGeode->exaBfrOffset = fboffset;
                fboffset += size;
                fbavail -= size;
            }
        }
    }
#endif

    if (!pGeode->NoAccel && !pGeode->useEXA) {  /* xaa acceleration enabled */
        if (pGeode->NoOfImgBuffers > 0) {
            pGeode->AccelImageWriteBuffers = NULL;
            pitch = pGeode->AccelPitch;
            size = pitch * pGeode->NoOfImgBuffers;
#if !GX_USE_OFFSCRN_MEM
            if (size <= fbavail) {
                bp = (unsigned char *)pGeode->FBBase + fboffset;
                l = sizeof(pGeode->AccelImageWriteBuffers[0]) *
                    pGeode->NoOfImgBuffers;
                ap = (unsigned char **)xalloc(l);
                if (ap != NULL) {
                    for (i = 0; i < pGeode->NoOfImgBuffers; ++i) {
                        ap[i] = bp;
                        DEBUGMSG(1, (scrnIndex, X_PROBED, "img line  %d %p\n",
                                i, ap[i]));
                        bp += pitch;
                    }
                    pGeode->AccelImageWriteBuffers = ap;
                    fboffset += size;
                    fbavail -= size;
                } else {
                    xf86DrvMsg(scrnIndex, X_ERROR,
                        "Image Write, No Memory\n");
                }
            } else {
                xf86DrvMsg(scrnIndex, X_ERROR, "Image Write, No FB Memory\n");
            }
#else
            if ((bp = (unsigned char *)xalloc(size)) != NULL) {
                ap = xalloc(sizeof(pGeode->AccelImageWriteBuffers[0]) *
                    pGeode->NoOfImgBuffers);
                if (ap != NULL) {
                    for (i = 0; i < pGeode->NoOfImgBuffers; ++i) {
                        ap[i] = bp;
                        DEBUGMSG(1, (scrnIndex, X_PROBED, "img line  %d %x\n",
                                i, ap[i]));
                        bp += pitch;
                    }
                    pGeode->AccelImageWriteBuffers = ap;
                } else {
                    xf86DrvMsg(scrnIndex, X_ERROR,
                        "Image Write, No Memory\n");
                }
            } else {
                xf86DrvMsg(scrnIndex, X_ERROR,
                    "Image Write, No offscreen Memory\n");
            }
#endif
            if (pGeode->AccelImageWriteBuffers == NULL) {
                xf86DrvMsg(scrnIndex, X_ERROR,
                    "Accel Image Write disabled\n");
                pGeode->NoOfImgBuffers = 0;
            }
        }

        if (pGeode->NoOfColorExpandLines > 0) {
            pGeode->AccelColorExpandBuffers = NULL;
            pitch = ((pGeode->AccelPitch + 31) >> 5) << 2;
            size = pitch * pGeode->NoOfColorExpandLines;
            if (size <= fbavail) {
                bp = (unsigned char *)pGeode->FBBase + fboffset;
                l = sizeof(pGeode->AccelColorExpandBuffers[0]) *
                    pGeode->NoOfColorExpandLines;
                ap = (unsigned char **)xalloc(l);
                if (ap != NULL) {
                    for (i = 0; i < pGeode->NoOfColorExpandLines; ++i) {
                        ap[i] = bp;
                        DEBUGMSG(1, (scrnIndex, X_PROBED, "clr line  %d %p\n",
                                i, ap[i]));
                        bp += pitch;
                    }
                    pGeode->AccelColorExpandBuffers = ap;
                    fboffset += size;
                    fbavail -= size;
                } else {
                    xf86DrvMsg(scrnIndex, X_ERROR,
                        "Color Expansion, No Memory\n");
                }
            } else {
                xf86DrvMsg(scrnIndex, X_ERROR,
                    "Color Expansion, No offscreen Memory\n");
            }
            if (pGeode->AccelColorExpandBuffers == NULL) {
                xf86DrvMsg(scrnIndex, X_ERROR,
                    "Accel Color Expansion disabled\n");
                pGeode->NoOfColorExpandLines = 0;
            }
        }
    } else {
        pGeode->NoOfImgBuffers = 0;
        pGeode->AccelImageWriteBuffers = NULL;
        pGeode->NoOfColorExpandLines = 0;
        pGeode->AccelColorExpandBuffers = NULL;
    }

    /* Initialise graphics mode */
    if (!GXEnterGraphics(pScrn, pScrni))
        return FALSE;

    pScrni->virtualX = virtualX;
    pScrni->virtualY = virtualY;

    DEBUGMSG(1, (0, X_INFO, "GXScreenInit(1)!\n"));

    /* Reset visual list */
    miClearVisualTypes();
    DEBUGMSG(1, (0, X_INFO, "GXScreenInit(2)!\n"));

    /* Setup the visual we support */
    if (pScrni->bitsPerPixel > 8) {
        DEBUGMSG(1, (scrnIndex, X_PROBED,
                "miSetVisualTypes %d %X %X %X\n",
                pScrni->depth, TrueColorMask,
                pScrni->rgbBits, pScrni->defaultVisual));

        if (!miSetVisualTypes(pScrni->depth,
                TrueColorMask, pScrni->rgbBits, pScrni->defaultVisual)) {
            return FALSE;
        }
    } else {
        if (!miSetVisualTypes(pScrni->depth,
                miGetDefaultVisualMask(pScrni->depth),
                pScrni->rgbBits, pScrni->defaultVisual)) {
            return FALSE;
        }
    }
    DEBUGMSG(1, (0, X_INFO, "GXScreenInit(3)!\n"));

    /* Set for RENDER extensions */
    miSetPixmapDepths();

    /* Call the framebuffer layer's ScreenInit function, and fill in other
     * * pScrn fields.
     */
    switch (pScrni->bitsPerPixel) {
#if CFB
    case 8:
        Inited = cfbScreenInit(pScrn, FBStart, virtualX, virtualY,
            pScrni->xDpi, pScrni->yDpi, displayWidth);
        break;
    case 16:
        Inited = cfb16ScreenInit(pScrn, FBStart, virtualX, virtualY,
            pScrni->xDpi, pScrni->yDpi, displayWidth);
        break;
    case 24:
    case 32:
        Inited = cfb32ScreenInit(pScrn, FBStart, virtualX, virtualY,
            pScrni->xDpi, pScrni->yDpi, displayWidth);
        break;
#else
    case 8:
    case 16:
    case 24:
    case 32:
        Inited = fbScreenInit(pScrn, FBStart, virtualX, virtualY,
            pScrni->xDpi, pScrni->yDpi, displayWidth, pScrni->bitsPerPixel);
        break;
#endif
    default:
        xf86DrvMsg(scrnIndex, X_ERROR,
            "Internal error: invalid bpp (%d) in ScreenInit\n",
            pScrni->bitsPerPixel);
        Inited = FALSE;
        break;
    }

    if (!Inited)
        return FALSE;

    GXRotationInit(pScrni);
    GXAdjustFrame(scrnIndex, pScrni->frameX0, pScrni->frameY0, 0);

    if (!pGeode->NoAccel) {
        if (!pGeode->useEXA) {
            AvailBox.x1 = 0;           /* SET UP GRAPHICS MEMORY AVAILABLE FOR PIXMAP CACHE */
            AvailBox.y1 =
                (fboffset + pGeode->AccelPitch - 1) / pGeode->AccelPitch;
            AvailBox.x2 = displayWidth;
            AvailBox.y2 = (fboffset + fbavail) / pGeode->AccelPitch;

            DEBUGMSG(1, (scrnIndex, X_PROBED,
                    "Memory manager initialized to (%d,%d) (%d,%d)\n",
                    AvailBox.x1, AvailBox.y1, AvailBox.x2, AvailBox.y2));

            if (AvailBox.y1 < AvailBox.y2) {
                xf86DrvMsg(scrnIndex, X_INFO,
                    "Initializing Memory manager to (%d,%d) (%d,%d)\n",
                    AvailBox.x1, AvailBox.y1, AvailBox.x2, AvailBox.y2);
                REGION_INIT(pScrn, &OffscreenRegion, &AvailBox, 2);
                if (!xf86InitFBManagerRegion(pScrn, &OffscreenRegion)) {
                    xf86DrvMsg(scrnIndex, X_ERROR,
                        "Memory manager initialization failed, Cache Diabled\n");
                }
                REGION_UNINIT(pScrn, &OffscreenRegion);
            } else {
                xf86DrvMsg(scrnIndex, X_INFO,
                    "No Off Screen Memory, Cache Disabled (%d,%d) (%d,%d)\n",
                    AvailBox.x1, AvailBox.y1, AvailBox.x2, AvailBox.y2);
            }
        }
#if XF86EXA
        else if (pGeode->pExa) {
            ExaDriverPtr pExa = pGeode->pExa;

            pExa->memoryBase = pGeode->FBBase;
            pExa->offScreenBase = fboffset;
            pExa->memorySize = fboffset + fbavail;
            pExa->pixmapOffsetAlign = 32;
            pExa->pixmapPitchAlign = 32;
            pExa->flags = EXA_OFFSCREEN_PIXMAPS;
            pExa->maxX = pGeode->maxWidth - 1;
            pExa->maxY = pGeode->maxHeight - 1;
        }
#endif
    }

    DEBUGMSG(1, (0, X_INFO, "GXScreenInit(4)!\n"));
    xf86SetBlackWhitePixels(pScrn);

    if (!pGeode->ShadowFB) {
        GXDGAInit(pScrn);
    }

    DEBUGMSG(1, (0, X_INFO, "GXScreenInit(5)!\n"));
    if (pScrni->bitsPerPixel > 8) {
        /* Fixup RGB ordering */
        visual = pScrn->visuals + pScrn->numVisuals;
        while (--visual >= pScrn->visuals) {
            if ((visual->class | DynamicClass) == DirectColor) {
                visual->offsetRed = pScrni->offset.red;
                visual->offsetGreen = pScrni->offset.green;
                visual->offsetBlue = pScrni->offset.blue;
                visual->redMask = pScrni->mask.red;
                visual->greenMask = pScrni->mask.green;
                visual->blueMask = pScrni->mask.blue;
            }
        }
    }
#if CFB
#else
    /* must be after RGB ordering fixed */
    fbPictureInit(pScrn, 0, 0);
#endif

    DEBUGMSG(1, (0, X_INFO, "GXScreenInit(6)!\n"));
    if (!pGeode->NoAccel) {
        GXAccelInit(pScrn);
    }

    DEBUGMSG(1, (0, X_INFO, "GXScreenInit(7)!\n"));
    miInitializeBackingStore(pScrn);
    xf86SetBackingStore(pScrn);
    DEBUGMSG(1, (0, X_INFO, "GXScreenInit(8)!\n"));
    /* Initialise software cursor */
    miDCInitialize(pScrn, xf86GetPointerScreenFuncs());
    /* Initialize HW cursor layer.
     * * Must follow software cursor initialization
     */
    if (pGeode->HWCursor) {
        if (!GXHWCursorInit(pScrn))
            xf86DrvMsg(pScrni->scrnIndex, X_ERROR,
                "Hardware cursor initialization failed\n");
    }

    DEBUGMSG(1, (0, X_INFO, "GXScreenInit(9)!\n"));
    /* Setup default colourmap */
    if (!miCreateDefColormap(pScrn)) {
        return FALSE;
    }

    DEBUGMSG(1, (0, X_INFO, "GXScreenInit(10)!\n"));
    if (pScrni->bitsPerPixel == 8) {
        /* Initialize colormap layer.
         * * Must follow initialization of the default colormap
         */
        if (!xf86HandleColormaps(pScrn, 256, 8,
                GXLoadPalette, NULL,
                CMAP_PALETTED_TRUECOLOR | CMAP_RELOAD_ON_MODE_SWITCH)) {
            return FALSE;
        }
    }

    DEBUGMSG(1, (0, X_INFO, "GXScreenInit(11)!\n"));

    if (pGeode->ShadowFB) {
        DEBUGMSG(1, (0, X_INFO, "Shadowed, Rotate=%d, NoAccel=%d\n",
                pGeode->Rotate, pGeode->NoAccel));
        GXShadowFBInit(pScrn, pGeode, bytpp);
    }
#ifdef DPMSExtension
    xf86DPMSInit(pScrn, GXDPMSSet, 0);
#endif
    DEBUGMSG(1, (0, X_INFO, "GXScreenInit(12)!\n"));

    DEBUGMSG(1, (0, X_INFO, "GXScreenInit(13)!\n"));
    GXInitVideo(pScrn);                /* needed for video */
    /* Wrap the screen's CloseScreen vector and set its
     * SaveScreen vector 
     */
    pGeode->CloseScreen = pScrn->CloseScreen;
    pScrn->CloseScreen = GXCloseScreen;

    pScrn->SaveScreen = GXSaveScreen;
    DEBUGMSG(1, (0, X_INFO, "GXScreenInit(14)!\n"));

    /* Report any unused options */
    if (serverGeneration == 1) {
        xf86ShowUnusedOptions(pScrni->scrnIndex, pScrni->options);
    }

    DEBUGMSG(1, (0, X_INFO, "GXScreenInit(15)!\n"));
    return TRUE;
}

/*----------------------------------------------------------------------------
 * GXSwitchMode.
 *
 * Description	:This function will switches the screen mode
 *   			    				
 * Parameters:
 *    scrnIndex	:Specfies the screen index value.
 *    pMode		:pointer to the mode structure.
 * 	  flags     :may be used for status check?.
 *	  												
 * Returns		:Returns TRUE on success and FALSE on failure.
 *
 * Comments     :none.
 *----------------------------------------------------------------------------
 */
Bool
GXSwitchMode(int scrnIndex, DisplayModePtr pMode, int flags)
{
    DEBUGMSG(1, (0, X_INFO, "GXSwitchMode!\n"));
    return GXSetMode(xf86Screens[scrnIndex], pMode);
}

/*----------------------------------------------------------------------------
 * GXAdjustFrame.
 *
 * Description	:This function is used to intiallize the start
 *				 address of the memory.
 * Parameters.
 *    scrnIndex	:Specfies the screen index value.
 *     x     	:x co-ordinate value interms of pixels.
 * 	   y        :y co-ordinate value interms of pixels.
 *	  												
 * Returns		:none.
 *
 * Comments    	:none.
 *----------------------------------------------------------------------------
 */
void
GXAdjustFrame(int scrnIndex, int x, int y, int flags)
{
    ScrnInfoPtr pScrni = xf86Screens[scrnIndex];

    GeodeRec *pGeode = GEODEPTR(pScrni);
    int newX, newY;
    unsigned long offset;

    if (x + pGeode->HDisplay >= pScrni->virtualX)
        x = pScrni->virtualX - pGeode->HDisplay;

    if (x < 0)
        x = 0;

    if (y + pGeode->VDisplay >= pScrni->virtualY)
        y = pScrni->virtualY - pGeode->VDisplay;

    if (y < 0)
        y = 0;

    pScrni->frameX0 = x;
    pScrni->frameY0 = y;
    pScrni->frameX1 = x + pGeode->HDisplay - 1;
    pScrni->frameY1 = y + pGeode->VDisplay - 1;
    (*pGeode->Rotation) (x, y, pScrni->virtualX, pScrni->virtualY, &newX,
        &newY);
    (*pGeode->RBltXlat) (newX, newY, pGeode->HDisplay, pGeode->VDisplay,
        &newX, &newY);
    offset =
        pGeode->FBOffset + newY * pGeode->Pitch +
        newX * (pScrni->bitsPerPixel >> 3);
    gfx_set_display_offset(offset);
}

/*----------------------------------------------------------------------------
 * GXEnterVT.
 *
 * Description	:This is called when VT switching back to the X server
 *			
 * Parameters.
 *    scrnIndex	:Specfies the screen index value.
 *     flags   	:Not used inside the function.
 * 	 						
 * Returns		:none.
 *
 * Comments     :none.
 *----------------------------------------------------------------------------
 */
static Bool
GXEnterVT(int scrnIndex, int flags)
{
    DEBUGMSG(1, (0, X_INFO, "GXEnterVT!\n"));
    return GXEnterGraphics(NULL, xf86Screens[scrnIndex]);
}

/*----------------------------------------------------------------------------
 * GXLeaveVT.
 *
 * Description	:This is called when VT switching  X server text mode.
 *			
 * Parameters.
 *    scrnIndex	:Specfies the screen index value.
 *     flags    :Not used inside the function.
 * 	 						
 * Returns		:none.
 *
 * Comments     :none.
 *----------------------------------------------------------------------------
 */
static void
GXLeaveVT(int scrnIndex, int flags)
{
    ScrnInfoPtr pScrni = xf86Screens[scrnIndex];
    GeodeRec *pGeode = GEODEPTR(pScrni);

    pGeode->PrevDisplayOffset = gfx_get_display_offset();
    DEBUGMSG(1, (0, X_INFO, "GXLeaveVT!\n"));
    GXLeaveGraphics(xf86Screens[scrnIndex]);
}

/*----------------------------------------------------------------------------
 * GXFreeScreen.
 *
 * Description	:This is called to free any persistent data structures.
 *			
 * Parameters.
 *    scrnIndex :Specfies the screen index value.
 *     flags   	:Not used inside the function.
 * 	 						
 * Returns		:none.
 *
 * Comments     :This will be called only when screen being deleted..
 *----------------------------------------------------------------------------
 */
static void
GXFreeScreen(int scrnIndex, int flags)
{
    DEBUGMSG(1, (0, X_INFO, "GXFreeScreen!\n"));
    if (xf86LoaderCheckSymbol("vgaHWFreeHWRec"))
        vgaHWFreeHWRec(xf86Screens[scrnIndex]);

    GXFreeRec(xf86Screens[scrnIndex]);
}

/*----------------------------------------------------------------------------
 * GXValidMode.
 *
 * Description	:This function checks if a mode is suitable for selected
 *               chipset.
 * Parameters.
 *    scrnIndex :Specfies the screen index value.
 *     pMode	:Pointer to the screen mode structure..
 * 	 verbose    :not used for implementation.						
 *     flags    :not used for implementation
 *
 * Returns		:MODE_OK if the specified mode is supported or
 *               MODE_NO_INTERLACE.
 * Comments     :none.
 *----------------------------------------------------------------------------
 */
static int
GXValidMode(int scrnIndex, DisplayModePtr pMode, Bool Verbose, int flags)
{
    unsigned int total_memory_required;
    ScrnInfoPtr pScrni = xf86Screens[scrnIndex];
    int ret = -1;
    unsigned int ptch;

    GeodeRec *pGeode = GEODEPTR(pScrni);

    DEBUGMSG(1, (0, X_NONE, "GeodeValidateMode: %dx%d %d %d\n",
            pMode->CrtcHDisplay, pMode->CrtcVDisplay,
            pScrni->bitsPerPixel, GXGetRefreshRate(pMode)));
    if (pGeode->CustomMode == 0) {

#if defined(PNL_SUP)
        if (pGeode->Panel != 0) {
	    DEBUGMSG(1, (0, X_NONE, "crtcH = %d, FPBX=%d, CrtcV=%d FPBY=%d\n",
				    pMode->CrtcHDisplay, pGeode->FPBX,
				    pMode->CrtcVDisplay, pGeode->FPBY));
            if (pMode->CrtcHDisplay > pGeode->FPBX ||
                pMode->CrtcVDisplay > pGeode->FPBY ||
                gfx_is_panel_mode_supported(pGeode->FPBX, pGeode->FPBY,
                    pMode->CrtcHDisplay, pMode->CrtcVDisplay,
                    pScrni->bitsPerPixel) < 0)
                return MODE_NOMODE;
        }
#endif /* PNL_SUP */

        DEBUGMSG(1, (0, X_NONE, "CRT mode\n"));
        if (pMode->Flags & V_INTERLACE)
            return MODE_NO_INTERLACE;

        ret = gfx_is_display_mode_supported(pMode->CrtcHDisplay,
            pMode->CrtcVDisplay,
            pScrni->bitsPerPixel, GXGetRefreshRate(pMode));
        if (ret < 0)
            return MODE_NOMODE;
    }

    if (pGeode->Compression)
	ptch = GXCalculatePitchBytes(pMode->CrtcHDisplay, pScrni->bitsPerPixel);
    else
	ptch = ((pMode->CrtcHDisplay + 3) & ~3) * (pScrni->bitsPerPixel >> 3);

    total_memory_required = ptch * pMode->CrtcVDisplay;

    DEBUGMSG(1, (0, X_NONE, "Total Mem %X %lX\n",
            total_memory_required, pGeode->FBAvail));

    if (total_memory_required > pGeode->FBAvail)
        return MODE_MEM;

    return MODE_OK;
}

/*----------------------------------------------------------------------------
 * GXLoadPalette.
 *
 * Description:		This function sets the  palette entry used for graphics 
 * 					data
 *
 * Parameters.
 *   	pScrni:		Points the screeninfo structure.
 *     	numColors:	Specifies the no of colors it supported.
 * 	 	indizes:	This is used get index value .						
 *     	LOCO:		to be added.
 *     	pVisual:	to be added.
 *
 * Returns:			MODE_OK if the specified mode is supported or
 *          	 	MODE_NO_INTERLACE.
 *
 * Comments:		none.
 *----------------------------------------------------------------------------
 */
static void
GXLoadPalette(ScrnInfoPtr pScrni,
    int numColors, int *indizes, LOCO * colors, VisualPtr pVisual)
{
    int i, index, color;

    for (i = 0; i < numColors; i++) {
        index = indizes[i] & 0xFF;
        color = (((unsigned long)(colors[index].red & 0xFF)) << 16) |
            (((unsigned long)(colors[index].green & 0xFF)) << 8) |
            ((unsigned long)(colors[index].blue & 0xFF));
        DEBUGMSG(0, (0, X_NONE, "GXLoadPalette: %d %d %X\n",
                numColors, index, color));

        GFX(set_display_palette_entry(index, color));
    }
}

static Bool
GXMapMem(ScrnInfoPtr pScrni)
{
    GeodeRec *pGeode = GEODEPTR(pScrni);

    gfx_virt_regptr = (unsigned char *)xf86MapVidMem(pScrni->scrnIndex,
        VIDMEM_MMIO, (unsigned int)
        gfx_get_cpu_register_base(), pGeode->cpu_reg_size);

    if (pGeode->DetectedChipSet & GX) {
        gfx_virt_gpptr = (unsigned char *)xf86MapVidMem(pScrni->scrnIndex,
            VIDMEM_MMIO, (unsigned int)
            gfx_get_graphics_register_base(), pGeode->gp_reg_size);
    } else {
        gfx_virt_spptr = gfx_virt_regptr;
    }

    gfx_virt_vidptr = (unsigned char *)xf86MapVidMem(pScrni->scrnIndex,
        VIDMEM_MMIO, (unsigned int)
        gfx_get_vid_register_base(), pGeode->vid_reg_size);

    gfx_virt_fbptr = (unsigned char *)xf86MapVidMem(pScrni->scrnIndex,
        VIDMEM_FRAMEBUFFER, pGeode->FBLinearAddr, pGeode->FBAvail);

    XpressROMPtr = (unsigned char *)xf86MapVidMem(pScrni->scrnIndex,
        VIDMEM_FRAMEBUFFER, 0xF0000, 0x10000);

    pGeode->FBBase = gfx_virt_fbptr;

    DEBUGMSG(1, (0, X_NONE, "Set mode %p %p %p %p %p\n",
            gfx_virt_regptr,
            gfx_virt_gpptr, gfx_virt_spptr, gfx_virt_vidptr, gfx_virt_fbptr));

    /* CHECK IF REGISTERS WERE MAPPED SUCCESSFULLY */
    if ((!gfx_virt_regptr) ||
        (!gfx_virt_gpptr) || (!gfx_virt_vidptr) || (!gfx_virt_fbptr)) {
        DEBUGMSG(1, (0, X_NONE, "Could not map hardware registers.\n"));
        return (FALSE);
    }

    DEBUGMSG(1, (0, X_NONE, "adapter info %lx %lx %lx %p\n",
            pGeode->cpu_version,
            pGeode->vid_version,
            pGeode->FBAvail, pGeode->FBBase));

    return TRUE;
}

/*
 * Unmap the framebuffer and MMIO memory.
 */

static Bool
GXUnmapMem(ScrnInfoPtr pScrni)
{
    GeodeRec *pGeode = GEODEPTR(pScrni);

    /* unmap all the memory map's */
    xf86UnMapVidMem(pScrni->scrnIndex, gfx_virt_regptr, pGeode->cpu_reg_size);
    if (pGeode->DetectedChipSet & GX) {
        xf86UnMapVidMem(pScrni->scrnIndex,
            gfx_virt_gpptr, pGeode->gp_reg_size);
    }
    xf86UnMapVidMem(pScrni->scrnIndex, gfx_virt_vidptr, pGeode->vid_reg_size);
    xf86UnMapVidMem(pScrni->scrnIndex, gfx_virt_fbptr, pGeode->FBAvail);
    return TRUE;
}

/* End of file */