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
|
/* $NetBSD: voodoofb.c,v 1.56 2022/09/25 17:52:25 thorpej Exp $ */
/*
* Copyright (c) 2005, 2006, 2012 Michael Lorenz
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* A console driver for 3Dfx Voodoo3 graphics boards
* Thanks to Andreas Drewke (andreas_dr@gmx.de) for his Voodoo3 driver for BeOS
* which I used as reference / documentation
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: voodoofb.c,v 1.56 2022/09/25 17:52:25 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/callout.h>
#include <sys/kauth.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcidevs.h>
#include <dev/pci/pciio.h>
#include <dev/pci/voodoofbreg.h>
#include <dev/wscons/wsdisplayvar.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wsfont/wsfont.h>
#include <dev/rasops/rasops.h>
#include <dev/wscons/wsdisplay_vconsvar.h>
#include <dev/pci/wsdisplay_pci.h>
#include <dev/i2c/i2cvar.h>
#include <dev/i2c/i2c_bitbang.h>
#include <dev/i2c/ddcvar.h>
#include <dev/videomode/videomode.h>
#include <dev/videomode/edidvar.h>
#include <dev/videomode/edidreg.h>
#include "opt_wsemul.h"
struct voodoofb_softc {
device_t sc_dev;
pci_chipset_tag_t sc_pc;
pcitag_t sc_pcitag;
struct pci_attach_args sc_pa;
bus_space_tag_t sc_memt;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_memh;
bus_space_tag_t sc_regt;
bus_space_tag_t sc_ioregt;
bus_space_handle_t sc_regh;
bus_space_handle_t sc_ioregh;
bus_addr_t sc_regs, sc_fb, sc_ioreg;
bus_size_t sc_regsize, sc_fbsize, sc_ioregsize;
void *sc_ih;
#define MAX_CLOCK_VB 270000 /* Voodoo Banshee */
#define MAX_CLOCK_V3 300000 /* Voodoo3 */
#define MAX_CLOCK_V45 350000 /* Voodoo4/5 (not yet) */
uint32_t sc_max_clock;
size_t sc_memsize;
int sc_memtype;
int sc_bits_per_pixel;
int sc_width, sc_height, sc_linebytes;
const struct videomode *sc_videomode;
int sc_is_mapped;
/* glyph cache */
long sc_defattr, sc_kernattr;
uint32_t sc_glyphs_defattr[256];
uint32_t sc_glyphs_kernattr[256];
int sc_numglyphs, sc_usedglyphs;
uint32_t sc_cache; /* offset where cache starts */
uint32_t sc_fmt; /* *fmt register */
void *sc_font;
/* i2c stuff */
struct i2c_controller sc_i2c;
uint8_t sc_edid_data[128];
struct edid_info sc_edid_info;
uint32_t sc_i2creg;
int sc_mode;
uint32_t sc_bg;
u_char sc_cmap_red[256];
u_char sc_cmap_green[256];
u_char sc_cmap_blue[256];
int sc_dacw;
struct vcons_data vd;
};
struct voodoo_regs {
uint8_t vr_crtc[31];
uint8_t vr_graph[9];
uint8_t vr_attr[21];
uint8_t vr_seq[5];
};
static struct vcons_screen voodoofb_console_screen;
static int voodoofb_match(device_t, cfdata_t, void *);
static void voodoofb_attach(device_t, device_t, void *);
static int voodoofb_drm_print(void *, const char *);
static int voodoofb_drm_unmap(struct voodoofb_softc *);
static int voodoofb_drm_map(struct voodoofb_softc *);
CFATTACH_DECL_NEW(voodoofb, sizeof(struct voodoofb_softc), voodoofb_match,
voodoofb_attach, NULL, NULL);
static bool voodoofb_is_console(struct voodoofb_softc *);
static void voodoofb_init(struct voodoofb_softc *);
static void voodoofb_cursor(void *, int, int, int);
static void voodoofb_putchar(void *, int, int, u_int, long);
static void voodoofb_putchar_aa(void *, int, int, u_int, long);
static void voodoofb_copycols(void *, int, int, int, int);
static void voodoofb_erasecols(void *, int, int, int, long);
static void voodoofb_copyrows(void *, int, int, int);
static void voodoofb_eraserows(void *, int, int, long);
#if 0
static int voodoofb_allocattr(void *, int, int, int, long *);
static void voodoofb_scroll(void *, void *, int);
static int voodoofb_load_font(void *, void *, struct wsdisplay_font *);
#endif
static int voodoofb_putcmap(struct voodoofb_softc *,
struct wsdisplay_cmap *);
static int voodoofb_getcmap(struct voodoofb_softc *,
struct wsdisplay_cmap *);
static int voodoofb_putpalreg(struct voodoofb_softc *, uint8_t, uint8_t,
uint8_t, uint8_t);
static void voodoofb_bitblt(struct voodoofb_softc *, int, int, int, int,
int, int);
static void voodoofb_rectfill(struct voodoofb_softc *, int, int, int, int,
int);
static void voodoofb_rectinvert(struct voodoofb_softc *, int, int, int,
int);
static void voodoofb_setup_mono(struct voodoofb_softc *, int, int, int,
int, uint32_t, uint32_t);
static void voodoofb_feed_line(struct voodoofb_softc *, int, uint8_t *);
static void voodoofb_wait_idle(struct voodoofb_softc *);
static void voodoofb_init_glyphcache(struct voodoofb_softc *,
struct rasops_info *);
#ifdef VOODOOFB_ENABLE_INTR
static int voodoofb_intr(void *);
#endif
static void voodoofb_set_videomode(struct voodoofb_softc *,
const struct videomode *);
struct wsscreen_descr voodoofb_defaultscreen = {
"default",
0, 0,
NULL,
8, 16,
WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
NULL,
};
const struct wsscreen_descr *_voodoofb_scrlist[] = {
&voodoofb_defaultscreen,
/* XXX other formats, graphics screen? */
};
struct wsscreen_list voodoofb_screenlist = {
sizeof(_voodoofb_scrlist) / sizeof(struct wsscreen_descr *), _voodoofb_scrlist
};
static int voodoofb_ioctl(void *, void *, u_long, void *, int,
struct lwp *);
static paddr_t voodoofb_mmap(void *, void *, off_t, int);
static void voodoofb_clearscreen(struct voodoofb_softc *);
static void voodoofb_init_screen(void *, struct vcons_screen *, int,
long *);
struct wsdisplay_accessops voodoofb_accessops = {
voodoofb_ioctl,
voodoofb_mmap,
NULL,
NULL,
NULL,
NULL, /* load_font */
NULL, /* polls */
NULL, /* scroll */
};
/* I2C glue */
static int voodoofb_i2c_send_start(void *, int);
static int voodoofb_i2c_send_stop(void *, int);
static int voodoofb_i2c_initiate_xfer(void *, i2c_addr_t, int);
static int voodoofb_i2c_read_byte(void *, uint8_t *, int);
static int voodoofb_i2c_write_byte(void *, uint8_t, int);
/* I2C bitbang glue */
static void voodoofb_i2cbb_set_bits(void *, uint32_t);
static void voodoofb_i2cbb_set_dir(void *, uint32_t);
static uint32_t voodoofb_i2cbb_read(void *);
static void voodoofb_setup_i2c(struct voodoofb_softc *);
static const struct i2c_bitbang_ops voodoofb_i2cbb_ops = {
voodoofb_i2cbb_set_bits,
voodoofb_i2cbb_set_dir,
voodoofb_i2cbb_read,
{
VSP_SDA0_IN,
VSP_SCL0_IN,
0,
0
}
};
extern const u_char rasops_cmap[768];
/*
* Inline functions for getting access to register aperture.
*/
static inline void
voodoo3_write32(struct voodoofb_softc *sc, uint32_t reg, uint32_t val)
{
bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, val);
}
static inline void
voodoo3_write32s(struct voodoofb_softc *sc, uint32_t reg, uint32_t val)
{
bus_space_write_stream_4(sc->sc_regt, sc->sc_regh, reg, val);
}
static inline uint32_t
voodoo3_read32(struct voodoofb_softc *sc, uint32_t reg)
{
return bus_space_read_4(sc->sc_regt, sc->sc_regh, reg);
}
static inline void
voodoo3_write_crtc(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
{
bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, CRTC_INDEX - 0x300, reg);
bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, CRTC_DATA - 0x300, val);
}
static inline void
voodoo3_write_seq(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
{
bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, SEQ_INDEX - 0x300, reg);
bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, SEQ_DATA - 0x300, val);
}
static inline void
voodoo3_write_gra(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
{
bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, GRA_INDEX - 0x300, reg);
bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, GRA_DATA - 0x300, val);
}
static inline void
voodoo3_write_attr(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
{
uint8_t index;
(void)bus_space_read_1(sc->sc_ioregt, sc->sc_ioregh, IS1_R - 0x300);
index = bus_space_read_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300);
bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300, reg);
bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300, val);
bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300, index);
}
static inline void
vga_outb(struct voodoofb_softc *sc, uint32_t reg, uint8_t val)
{
bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, reg - 0x300, val);
}
/* wait until there's room for len bytes in the FIFO */
static inline void
voodoo3_make_room(struct voodoofb_softc *sc, int len)
{
while ((voodoo3_read32(sc, STATUS) & 0x1f) < len);
}
static void
voodoofb_wait_idle(struct voodoofb_softc *sc)
{
int i = 0;
voodoo3_make_room(sc, 1);
voodoo3_write32(sc, COMMAND_3D, COMMAND_3D_NOP);
while (1) {
i = (voodoo3_read32(sc, STATUS) & STATUS_BUSY) ? 0 : i + 1;
if(i == 3) break;
}
}
static int
voodoofb_match(device_t parent, cfdata_t match, void *aux)
{
struct pci_attach_args *pa = (struct pci_attach_args *)aux;
if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
return 0;
if ((PCI_VENDOR(pa->pa_id)==PCI_VENDOR_3DFX) &&
(PCI_PRODUCT(pa->pa_id)>=PCI_PRODUCT_3DFX_VOODOO3))
return 100;
if ((PCI_VENDOR(pa->pa_id)==PCI_VENDOR_3DFX) &&
(PCI_PRODUCT(pa->pa_id)>=PCI_PRODUCT_3DFX_BANSHEE))
return 100;
return 0;
}
static void
voodoofb_attach(device_t parent, device_t self, void *aux)
{
struct voodoofb_softc *sc = device_private(self);
struct pci_attach_args *pa = aux;
struct wsemuldisplaydev_attach_args aa;
struct rasops_info *ri;
#ifdef VOODOOFB_ENABLE_INTR
pci_intr_handle_t ih;
const char *intrstr;
#endif
ulong defattr;
int console, width, height, i;
prop_dictionary_t dict;
int linebytes, depth, flags;
uint32_t bg, fg, ul;
sc->sc_dev = self;
sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
sc->sc_pc = pa->pa_pc;
sc->sc_pcitag = pa->pa_tag;
sc->sc_dacw = -1;
pci_aprint_devinfo(pa, NULL);
sc->sc_memt = pa->pa_memt;
sc->sc_iot = pa->pa_iot;
sc->sc_pa = *pa;
if (PCI_PRODUCT(pa->pa_id)>=PCI_PRODUCT_3DFX_BANSHEE)
sc->sc_max_clock = MAX_CLOCK_VB;
else
sc->sc_max_clock = MAX_CLOCK_V3;
/* the framebuffer */
if (pci_mapreg_info(sc->sc_pc, sc->sc_pcitag, 0x14,
PCI_MAPREG_TYPE_MEM, &sc->sc_fb, &sc->sc_fbsize, &flags)) {
aprint_error_dev(self, "failed to map the frame buffer.\n");
}
sc->sc_memsize = sc->sc_fbsize >> 1; /* VRAM aperture is 2x VRAM */
/* memory-mapped registers */
if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
&sc->sc_regt, &sc->sc_regh, &sc->sc_regs, &sc->sc_regsize)) {
aprint_error_dev(self,
"failed to map memory-mapped registers.\n");
}
/* IO-mapped registers */
if (pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_IO, 0,
&sc->sc_ioregt, &sc->sc_ioregh, &sc->sc_ioreg,
&sc->sc_ioregsize)) {
aprint_error_dev(self, "failed to map IO-mapped registers.\n");
}
sc->sc_is_mapped = TRUE;
voodoofb_init(sc);
/* we should read these from the chip instead of depending on OF */
width = height = -1;
dict = device_properties(self);
if (!prop_dictionary_get_uint32(dict, "width", &width)) {
aprint_error_dev(self, "no width property\n");
return;
}
if (!prop_dictionary_get_uint32(dict, "height", &height)) {
aprint_error_dev(self, "no height property\n");
return;
}
if (!prop_dictionary_get_uint32(dict, "depth", &depth)) {
aprint_error_dev(self, "no depth property\n");
return;
}
linebytes = width; /* XXX */
if (width == -1 || height == -1)
return;
sc->sc_width = width;
sc->sc_height = height;
sc->sc_bits_per_pixel = depth;
sc->sc_linebytes = linebytes;
printf("%s: initial resolution %dx%d, %d bit\n", device_xname(self),
sc->sc_width, sc->sc_height, sc->sc_bits_per_pixel);
sc->sc_videomode = NULL;
voodoofb_setup_i2c(sc);
/* XXX this should at least be configurable via kernel config */
if (sc->sc_videomode == NULL) {
sc->sc_videomode = pick_mode_by_ref(width, height, 60);
}
voodoofb_set_videomode(sc, sc->sc_videomode);
sc->sc_font = NULL;
vcons_init(&sc->vd, sc, &voodoofb_defaultscreen, &voodoofb_accessops);
sc->vd.init_screen = voodoofb_init_screen;
console = voodoofb_is_console(sc);
ri = &voodoofb_console_screen.scr_ri;
if (console) {
vcons_init_screen(&sc->vd, &voodoofb_console_screen, 1,
&defattr);
voodoofb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
voodoofb_defaultscreen.textops = &ri->ri_ops;
voodoofb_defaultscreen.capabilities = ri->ri_caps;
voodoofb_defaultscreen.nrows = ri->ri_rows;
voodoofb_defaultscreen.ncols = ri->ri_cols;
wsdisplay_cnattach(&voodoofb_defaultscreen, ri, 0, 0, defattr);
} else {
if (voodoofb_console_screen.scr_ri.ri_rows == 0) {
/* do some minimal setup to avoid weirdnesses later */
vcons_init_screen(&sc->vd, &voodoofb_console_screen,
1, &defattr);
} else
(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
}
printf("%s: %d MB aperture at 0x%08x, %d MB registers at 0x%08x\n",
device_xname(self), (u_int)(sc->sc_fbsize >> 20),
(u_int)sc->sc_fb, (u_int)(sc->sc_regsize >> 20),
(u_int)sc->sc_regs);
#ifdef VOODOOFB_DEBUG
printf("fb: %08lx\n", (ulong)ri->ri_bits);
#endif
if (sc->sc_bits_per_pixel == 8) {
uint8_t tmp;
for (i = 0; i < 256; i++) {
tmp = i & 0xe0;
/*
* replicate bits so 0xe0 maps to a red value of 0xff
* in order to make white look actually white
*/
tmp |= (tmp >> 3) | (tmp >> 6);
sc->sc_cmap_red[i] = tmp;
tmp = (i & 0x1c) << 3;
tmp |= (tmp >> 3) | (tmp >> 6);
sc->sc_cmap_green[i] = tmp;
tmp = (i & 0x03) << 6;
tmp |= tmp >> 2;
tmp |= tmp >> 4;
sc->sc_cmap_blue[i] = tmp;
voodoofb_putpalreg(sc, i, sc->sc_cmap_red[i],
sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
}
} else {
/* linear ramp */
for (i = 0; i < 256; i++) {
sc->sc_cmap_red[i] = i;
sc->sc_cmap_green[i] = i;
sc->sc_cmap_blue[i] = i;
voodoofb_putpalreg(sc, i, sc->sc_cmap_red[i],
sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
}
}
#ifdef VOODOOFB_ENABLE_INTR
char intrbuf[PCI_INTRSTR_LEN];
/* Interrupt. We don't use it for anything yet */
if (pci_intr_map(pa, &ih)) {
aprint_error_dev(self, "failed to map interrupt\n");
return;
}
intrstr = pci_intr_string(sc->sc_pc, ih, intrbuf, sizeof(intrbuf));
sc->sc_ih = pci_intr_establish_xname(sc->sc_pc, ih, IPL_NET,
voodoofb_intr, sc, device_xname(self));
if (sc->sc_ih == NULL) {
aprint_error_dev(self, "failed to establish interrupt");
if (intrstr != NULL)
aprint_error(" at %s", intrstr);
aprint_error("\n");
return;
}
aprint_normal_dev(self, "interrupting at %s\n", intrstr);
#endif
rasops_unpack_attr(defattr, &fg, &bg, &ul);
sc->sc_bg = ri->ri_devcmap[bg];
voodoofb_clearscreen(sc);
if (console)
vcons_replay_msgbuf(&voodoofb_console_screen);
aa.console = console;
aa.scrdata = &voodoofb_screenlist;
aa.accessops = &voodoofb_accessops;
aa.accesscookie = &sc->vd;
config_found(self, &aa, wsemuldisplaydevprint,
CFARGS(.iattr = "wsemuldisplaydev"));
config_found(self, aux, voodoofb_drm_print,
CFARGS(.iattr = "drm"));
}
static int
voodoofb_drm_print(void *opaque, const char *pnp)
{
if (pnp)
aprint_normal("drm at %s", pnp);
return UNCONF;
}
static int
voodoofb_drm_unmap(struct voodoofb_softc *sc)
{
if (!sc->sc_is_mapped)
return 0;
printf("%s: releasing bus resources\n", device_xname(sc->sc_dev));
bus_space_unmap(sc->sc_ioregt, sc->sc_ioregh, sc->sc_ioregsize);
bus_space_unmap(sc->sc_regt, sc->sc_regh, sc->sc_regsize);
sc->sc_is_mapped = FALSE;
return 0;
}
static int
voodoofb_drm_map(struct voodoofb_softc *sc)
{
if (sc->sc_is_mapped)
return 0;
/* memory-mapped registers */
if (pci_mapreg_map(&sc->sc_pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
&sc->sc_regt, &sc->sc_regh, &sc->sc_regs, &sc->sc_regsize)) {
aprint_error_dev(sc->sc_dev,
"failed to map memory-mapped registers.\n");
}
/* IO-mapped registers */
if (pci_mapreg_map(&sc->sc_pa, 0x18, PCI_MAPREG_TYPE_IO, 0,
&sc->sc_ioregt, &sc->sc_ioregh, &sc->sc_ioreg,
&sc->sc_ioregsize)) {
aprint_error_dev(sc->sc_dev,
"failed to map IO-mapped registers.\n");
}
sc->sc_is_mapped = TRUE;
voodoofb_init(sc);
/* XXX this should at least be configurable via kernel config */
voodoofb_set_videomode(sc, sc->sc_videomode);
return 0;
}
static int
voodoofb_putpalreg(struct voodoofb_softc *sc, uint8_t index, uint8_t r,
uint8_t g, uint8_t b)
{
uint32_t color;
sc->sc_cmap_red[index] = r;
sc->sc_cmap_green[index] = g;
sc->sc_cmap_blue[index] = b;
color = (r << 16) | (g << 8) | b;
voodoo3_make_room(sc, 2);
voodoo3_write32(sc, DACADDR, index);
voodoo3_write32(sc, DACDATA, color);
return 0;
}
static int
voodoofb_putcmap(struct voodoofb_softc *sc, struct wsdisplay_cmap *cm)
{
u_char *r, *g, *b;
u_int index = cm->index;
u_int count = cm->count;
int i, error;
u_char rbuf[256], gbuf[256], bbuf[256];
#ifdef VOODOOFB_DEBUG
printf("putcmap: %d %d\n",index, count);
#endif
if (cm->index >= 256 || cm->count > 256 ||
(cm->index + cm->count) > 256)
return EINVAL;
error = copyin(cm->red, &rbuf[index], count);
if (error)
return error;
error = copyin(cm->green, &gbuf[index], count);
if (error)
return error;
error = copyin(cm->blue, &bbuf[index], count);
if (error)
return error;
memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
r = &sc->sc_cmap_red[index];
g = &sc->sc_cmap_green[index];
b = &sc->sc_cmap_blue[index];
for (i = 0; i < count; i++) {
voodoofb_putpalreg(sc, index, *r, *g, *b);
index++;
r++, g++, b++;
}
return 0;
}
static int
voodoofb_getcmap(struct voodoofb_softc *sc, struct wsdisplay_cmap *cm)
{
u_int index = cm->index;
u_int count = cm->count;
int error;
if (index >= 255 || count > 256 || index + count > 256)
return EINVAL;
error = copyout(&sc->sc_cmap_red[index], cm->red, count);
if (error)
return error;
error = copyout(&sc->sc_cmap_green[index], cm->green, count);
if (error)
return error;
error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
if (error)
return error;
return 0;
}
static bool
voodoofb_is_console(struct voodoofb_softc *sc)
{
prop_dictionary_t dict;
bool console = FALSE;
dict = device_properties(sc->sc_dev);
prop_dictionary_get_bool(dict, "is_console", &console);
return console;
}
static void
voodoofb_clearscreen(struct voodoofb_softc *sc)
{
voodoofb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height, sc->sc_bg);
}
/*
* wsdisplay_emulops
*/
static void
voodoofb_cursor(void *cookie, int on, int row, int col)
{
struct rasops_info *ri = cookie;
struct vcons_screen *scr = ri->ri_hw;
struct voodoofb_softc *sc = scr->scr_cookie;
int x, y, wi, he;
wi = ri->ri_font->fontwidth;
he = ri->ri_font->fontheight;
if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
x = ri->ri_ccol * wi + ri->ri_xorigin;
y = ri->ri_crow * he + ri->ri_yorigin;
if (ri->ri_flg & RI_CURSOR) {
voodoofb_rectinvert(sc, x, y, wi, he);
ri->ri_flg &= ~RI_CURSOR;
}
ri->ri_crow = row;
ri->ri_ccol = col;
if (on)
{
x = ri->ri_ccol * wi + ri->ri_xorigin;
y = ri->ri_crow * he + ri->ri_yorigin;
voodoofb_rectinvert(sc, x, y, wi, he);
ri->ri_flg |= RI_CURSOR;
}
} else {
ri->ri_flg &= ~RI_CURSOR;
ri->ri_crow = row;
ri->ri_ccol = col;
}
}
#if 0
int
voodoofb_mapchar(void *cookie, int uni, u_int *index)
{
return 0;
}
#endif
static void
voodoofb_putchar(void *cookie, int row, int col, u_int c, long attr)
{
struct rasops_info *ri = cookie;
struct wsdisplay_font *font = PICK_FONT(ri, c);
struct vcons_screen *scr = ri->ri_hw;
struct voodoofb_softc *sc = scr->scr_cookie;
if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
uint8_t *data;
int fg, bg, uc, i;
int x, y, wi, he;
wi = font->fontwidth;
he = font->fontheight;
if (!CHAR_IN_FONT(c, font))
return;
bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xf];
fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0xf];
x = ri->ri_xorigin + col * wi;
y = ri->ri_yorigin + row * he;
if (c == 0x20) {
voodoofb_rectfill(sc, x, y, wi, he, bg);
} else {
uc = c - font->firstchar;
data = (uint8_t *)font->data + uc *
ri->ri_fontscale;
voodoofb_setup_mono(sc, x, y, wi, he, fg, bg);
for (i = 0; i < he; i++) {
voodoofb_feed_line(sc, font->stride, data);
data += font->stride;
}
}
}
}
static void
voodoofb_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
{
struct rasops_info *ri = cookie;
struct wsdisplay_font *font = PICK_FONT(ri, c);
struct vcons_screen *scr = ri->ri_hw;
struct voodoofb_softc *sc = scr->scr_cookie;
uint8_t *data;
uint32_t bg, latch = 0, bg8, fg8, pixel, save_offset = 0;
int i, x, y, wi, he, r, g, b, aval;
int r1, g1, b1, r0, g0, b0, fgo, bgo, j;
if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
return;
if (!CHAR_IN_FONT(c, font))
return;
wi = font->fontwidth;
he = font->fontheight;
bg = ri->ri_devcmap[(attr >> 16) & 0xf];
x = ri->ri_xorigin + col * wi;
y = ri->ri_yorigin + row * he;
if (c == 0x20) {
voodoofb_rectfill(sc, x, y, wi, he, bg);
return;
}
/* first, see if it's in the cache */
if ((attr == sc->sc_defattr) && (c < 256)) {
uint32_t offset = sc->sc_glyphs_defattr[c];
if (offset != 0) {
voodoo3_make_room(sc, 8);
voodoo3_write32(sc, SRCBASE, offset);
voodoo3_write32(sc, DSTBASE, 0);
voodoo3_write32(sc, SRCFORMAT, sc->sc_fmt);
voodoo3_write32(sc, DSTFORMAT,
sc->sc_linebytes | FMT_8BIT);
voodoo3_write32(sc, DSTSIZE, wi | (he << 16));
voodoo3_write32(sc, DSTXY, x | (y << 16));
voodoo3_write32(sc, SRCXY, 0);
voodoo3_write32(sc, COMMAND_2D, COMMAND_2D_S2S_BITBLT
| (ROP_COPY << 24) | SST_2D_GO);
return;
} else {
int slot = sc->sc_usedglyphs;
sc->sc_usedglyphs++;
save_offset = sc->sc_cache + (slot * ri->ri_fontscale);
sc->sc_glyphs_defattr[c] = save_offset;
}
}
data = WSFONT_GLYPH(c, font);
voodoo3_make_room(sc, 7);
voodoo3_write32(sc, DSTBASE, 0);
voodoo3_write32(sc, SRCFORMAT, FMT_8BIT | FMT_PAD_BYTE);
voodoo3_write32(sc, DSTFORMAT, sc->sc_linebytes | FMT_8BIT);
voodoo3_write32(sc, DSTSIZE, wi | (he << 16));
voodoo3_write32(sc, DSTXY, x | (y << 16));
voodoo3_write32(sc, SRCXY, 0);
voodoo3_write32(sc, COMMAND_2D, COMMAND_2D_H2S_BITBLT |
(ROP_COPY << 24) | SST_2D_GO);
/*
* we need the RGB colours here, so get offsets into rasops_cmap
*/
fgo = ((attr >> 24) & 0xf) * 3;
bgo = ((attr >> 16) & 0xf) * 3;
r0 = rasops_cmap[bgo];
r1 = rasops_cmap[fgo];
g0 = rasops_cmap[bgo + 1];
g1 = rasops_cmap[fgo + 1];
b0 = rasops_cmap[bgo + 2];
b1 = rasops_cmap[fgo + 2];
#define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
bg8 = R3G3B2(r0, g0, b0);
fg8 = R3G3B2(r1, g1, b1);
voodoo3_make_room(sc, 15);
j = 15;
for (i = 0; i < ri->ri_fontscale; i++) {
aval = *data;
if (aval == 0) {
pixel = bg8;
} else if (aval == 255) {
pixel = fg8;
} else {
r = aval * r1 + (255 - aval) * r0;
g = aval * g1 + (255 - aval) * g0;
b = aval * b1 + (255 - aval) * b0;
pixel = ((r & 0xe000) >> 8) |
((g & 0xe000) >> 11) |
((b & 0xc000) >> 14);
}
latch = (latch << 8) | pixel;
/* write in 32bit chunks */
if ((i & 3) == 3) {
voodoo3_write32s(sc, LAUNCH_2D, latch);
/*
* not strictly necessary, old data should be shifted
* out
*/
latch = 0;
/*
* check for pipeline space every now and then
* I don't think we can feed a Voodoo3 faster than it
* can process simple pixel data but I have been wrong
* about that before
*/
j--;
if (j == 0) {
voodoo3_make_room(sc, 15);
j = 15;
}
}
data++;
}
/* if we have pixels left in latch write them out */
if ((i & 3) != 0) {
latch = latch << ((4 - (i & 3)) << 3);
voodoo3_write32s(sc, LAUNCH_2D, latch);
}
if (save_offset != 0) {
/* save the glyph we just drew */
voodoo3_make_room(sc, 8);
voodoo3_write32(sc, SRCBASE, 0);
voodoo3_write32(sc, DSTBASE, save_offset);
voodoo3_write32(sc, SRCFORMAT, sc->sc_linebytes | FMT_8BIT);
voodoo3_write32(sc, DSTFORMAT, sc->sc_fmt);
voodoo3_write32(sc, DSTSIZE, wi | (he << 16));
voodoo3_write32(sc, DSTXY, 0);
voodoo3_write32(sc, SRCXY, x | (y << 16));
voodoo3_write32(sc, COMMAND_2D, COMMAND_2D_S2S_BITBLT |
(ROP_COPY << 24) | SST_2D_GO);
}
}
static void
voodoofb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
{
struct rasops_info *ri = cookie;
struct vcons_screen *scr = ri->ri_hw;
struct voodoofb_softc *sc = scr->scr_cookie;
int32_t xs, xd, y, width, height;
if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
y = ri->ri_yorigin + ri->ri_font->fontheight * row;
width = ri->ri_font->fontwidth * ncols;
height = ri->ri_font->fontheight;
voodoofb_bitblt(sc, xs, y, xd, y, width, height);
}
}
static void
voodoofb_erasecols(void *cookie, int row, int startcol, int ncols,
long fillattr)
{
struct rasops_info *ri = cookie;
struct vcons_screen *scr = ri->ri_hw;
struct voodoofb_softc *sc = scr->scr_cookie;
int32_t x, y, width, height, fg, bg, ul;
if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
y = ri->ri_yorigin + ri->ri_font->fontheight * row;
width = ri->ri_font->fontwidth * ncols;
height = ri->ri_font->fontheight;
rasops_unpack_attr(fillattr, &fg, &bg, &ul);
voodoofb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
}
}
static void
voodoofb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
{
struct rasops_info *ri = cookie;
struct vcons_screen *scr = ri->ri_hw;
struct voodoofb_softc *sc = scr->scr_cookie;
int32_t x, ys, yd, width, height;
if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
x = ri->ri_xorigin;
ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
width = ri->ri_emuwidth;
height = ri->ri_font->fontheight * nrows;
voodoofb_bitblt(sc, x, ys, x, yd, width, height);
}
}
static void
voodoofb_eraserows(void *cookie, int row, int nrows, long fillattr)
{
struct rasops_info *ri = cookie;
struct vcons_screen *scr = ri->ri_hw;
struct voodoofb_softc *sc = scr->scr_cookie;
int32_t x, y, width, height, fg, bg, ul;
if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
rasops_unpack_attr(fillattr, &fg, &bg, &ul);
if ((row == 0) && (nrows == ri->ri_rows)) {
/* clear the whole screen */
voodoofb_rectfill(sc, 0, 0, ri->ri_width,
ri->ri_height, ri->ri_devcmap[bg]);
} else {
x = ri->ri_xorigin;
y = ri->ri_yorigin + ri->ri_font->fontheight * row;
width = ri->ri_emuwidth;
height = ri->ri_font->fontheight * nrows;
voodoofb_rectfill(sc, x, y, width, height,
ri->ri_devcmap[bg]);
}
}
}
static void
voodoofb_bitblt(struct voodoofb_softc *sc, int xs, int ys, int xd, int yd,
int width, int height)
{
uint32_t fmt, blitcmd;
fmt = sc->sc_linebytes | ((sc->sc_bits_per_pixel +
((sc->sc_bits_per_pixel == 8) ? 0 : 8)) << 13);
blitcmd = COMMAND_2D_S2S_BITBLT | (ROP_COPY << 24);
if (xs <= xd) {
blitcmd |= BIT(14);
xs += (width - 1);
xd += (width - 1);
}
if (ys <= yd) {
blitcmd |= BIT(15);
ys += (height - 1);
yd += (height - 1);
}
voodoo3_make_room(sc, 8);
voodoo3_write32(sc, SRCBASE, 0);
voodoo3_write32(sc, DSTBASE, 0);
voodoo3_write32(sc, SRCFORMAT, fmt);
voodoo3_write32(sc, DSTFORMAT, fmt);
voodoo3_write32(sc, DSTSIZE, width | (height << 16));
voodoo3_write32(sc, DSTXY, xd | (yd << 16));
voodoo3_write32(sc, SRCXY, xs | (ys << 16));
voodoo3_write32(sc, COMMAND_2D, blitcmd | SST_2D_GO);
}
static void
voodoofb_rectfill(struct voodoofb_softc *sc, int x, int y, int width,
int height, int colour)
{
uint32_t fmt;
fmt = sc->sc_linebytes | ((sc->sc_bits_per_pixel +
((sc->sc_bits_per_pixel == 8) ? 0 : 8)) << 13);
voodoo3_make_room(sc, 7);
voodoo3_write32(sc, DSTFORMAT, fmt);
voodoo3_write32(sc, DSTBASE, 0);
voodoo3_write32(sc, COLORFORE, colour);
voodoo3_write32(sc, COLORBACK, colour);
voodoo3_write32(sc, COMMAND_2D, COMMAND_2D_FILLRECT | (ROP_COPY << 24));
voodoo3_write32(sc, DSTSIZE, width | (height << 16));
voodoo3_write32(sc, LAUNCH_2D, x | (y << 16));
}
static void
voodoofb_rectinvert(struct voodoofb_softc *sc, int x, int y, int width,
int height)
{
uint32_t fmt;
fmt = sc->sc_linebytes | ((sc->sc_bits_per_pixel +
((sc->sc_bits_per_pixel == 8) ? 0 : 8)) << 13);
voodoo3_make_room(sc, 8);
voodoo3_write32(sc, SRCBASE, 0);
voodoo3_write32(sc, DSTBASE, 0);
voodoo3_write32(sc, DSTFORMAT, fmt);
voodoo3_write32(sc, COMMAND_2D, COMMAND_2D_FILLRECT |
(ROP_INVERT << 24));
voodoo3_write32(sc, DSTSIZE, width | (height << 16));
voodoo3_write32(sc, DSTXY, x | (y << 16));
voodoo3_write32(sc, LAUNCH_2D, x | (y << 16));
}
static void
voodoofb_setup_mono(struct voodoofb_softc *sc, int xd, int yd, int width,
int height, uint32_t fg, uint32_t bg)
{
uint32_t dfmt, sfmt = sc->sc_linebytes;
dfmt = sc->sc_linebytes | ((sc->sc_bits_per_pixel +
((sc->sc_bits_per_pixel == 8) ? 0 : 8)) << 13);
voodoo3_make_room(sc, 10);
voodoo3_write32(sc, SRCFORMAT, sfmt);
voodoo3_write32(sc, DSTFORMAT, dfmt);
voodoo3_write32(sc, DSTBASE, 0);
voodoo3_write32(sc, COLORFORE, fg);
voodoo3_write32(sc, COLORBACK, bg);
voodoo3_write32(sc, DSTSIZE, width | (height << 16));
voodoo3_write32(sc, DSTXY, xd | (yd << 16));
voodoo3_write32(sc, SRCXY, 0);
voodoo3_write32(sc, COMMAND_2D, COMMAND_2D_H2S_BITBLT |
(ROP_COPY << 24) | SST_2D_GO);
/* now feed the data into the chip */
}
static void
voodoofb_feed_line(struct voodoofb_softc *sc, int count, uint8_t *data)
{
int i;
uint32_t latch = 0, bork;
int shift = 0;
voodoo3_make_room(sc, count);
for (i = 0; i < count; i++) {
bork = data[i];
latch |= (bork << shift);
if (shift == 24) {
voodoo3_write32(sc, LAUNCH_2D, latch);
latch = 0;
shift = 0;
} else
shift += 8;
}
if (shift != 24)
voodoo3_write32(sc, LAUNCH_2D, latch);
}
#if 0
static int
voodoofb_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
{
return 0;
}
#endif
/*
* wsdisplay_accessops
*/
static int
voodoofb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
struct lwp *l)
{
struct vcons_data *vd = v;
struct voodoofb_softc *sc = vd->cookie;
struct wsdisplay_fbinfo *wdf;
struct vcons_screen *ms = vd->active;
switch (cmd) {
case WSDISPLAYIO_GTYPE:
*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
return 0;
case WSDISPLAYIO_GINFO:
wdf = (void *)data;
wdf->height = ms->scr_ri.ri_height;
wdf->width = ms->scr_ri.ri_width;
wdf->depth = ms->scr_ri.ri_depth;
wdf->cmsize = 256;
return 0;
case WSDISPLAYIO_GETCMAP:
return voodoofb_getcmap(sc,
(struct wsdisplay_cmap *)data);
case WSDISPLAYIO_PUTCMAP:
return voodoofb_putcmap(sc,
(struct wsdisplay_cmap *)data);
/* PCI config read/write passthrough. */
case PCI_IOC_CFGREAD:
case PCI_IOC_CFGWRITE:
return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
cmd, data, flag, l);
case WSDISPLAYIO_GET_BUSID:
return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
sc->sc_pcitag, data);
case WSDISPLAYIO_SMODE: {
int new_mode = *(int*)data;
if (new_mode != sc->sc_mode) {
sc->sc_mode = new_mode;
if (new_mode == WSDISPLAYIO_MODE_EMUL) {
voodoofb_drm_map(sc);
int i;
/* restore the palette */
for (i = 0; i < 256; i++) {
voodoofb_putpalreg(sc,
i,
sc->sc_cmap_red[i],
sc->sc_cmap_green[i],
sc->sc_cmap_blue[i]);
}
/* zap the glyph cache */
for (i = 0; i < 256; i++) {
sc->sc_glyphs_defattr[i] = 0;
sc->sc_glyphs_kernattr[i] = 0;
}
sc->sc_usedglyphs = 0;
voodoofb_clearscreen(sc);
vcons_redraw_screen(ms);
} else {
voodoofb_drm_unmap(sc);
}
}
}
return 0;
/* XXX WSDISPLAYIO_GET_EDID */
case WSDISPLAYIO_GET_FBINFO: {
struct wsdisplayio_fbinfo *fbi = data;
return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
}
}
return EPASSTHROUGH;
}
static paddr_t
voodoofb_mmap(void *v, void *vs, off_t offset, int prot)
{
struct vcons_data *vd = v;
struct voodoofb_softc *sc = vd->cookie;
paddr_t pa;
/* 'regular' framebuffer mmap()ing */
if (sc->sc_mode == WSDISPLAYIO_MODE_DUMBFB) {
if (offset < sc->sc_fbsize) {
pa = bus_space_mmap(sc->sc_memt, sc->sc_fb, offset,
prot,
BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
return pa;
}
} else if (sc->sc_mode == WSDISPLAYIO_MODE_MAPPED) {
if (kauth_authorize_machdep(kauth_cred_get(),
KAUTH_MACHDEP_UNMANAGEDMEM, NULL, NULL, NULL, NULL) != 0) {
aprint_error_dev(sc->sc_dev, "mmap() rejected.\n");
return -1;
}
if ((offset >= 0xa0000) &&
(offset < 0xb0000)) {
pa = bus_space_mmap(sc->sc_memt,
sc->sc_fb, offset - 0xa0000,
prot, BUS_SPACE_MAP_LINEAR);
return pa;
}
if ((offset >= sc->sc_fb) &&
(offset < (sc->sc_fb + sc->sc_fbsize))) {
pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
return pa;
}
if ((offset >= sc->sc_regs) && (offset < (sc->sc_regs +
sc->sc_regsize))) {
pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
BUS_SPACE_MAP_LINEAR);
return pa;
}
#ifdef PCI_MAGIC_IO_RANGE
/* allow mapping of IO space */
if ((offset >= PCI_MAGIC_IO_RANGE) &&
(offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
pa = bus_space_mmap(sc->sc_iot,
offset - PCI_MAGIC_IO_RANGE, 0, prot, 0);
return pa;
}
#endif
#ifdef OFB_ALLOW_OTHERS
if (offset >= 0x80000000) {
pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
BUS_SPACE_MAP_LINEAR);
return pa;
}
#endif
}
return -1;
}
static void
voodoofb_init_screen(void *cookie, struct vcons_screen *scr,
int existing, long *defattr)
{
struct voodoofb_softc *sc = cookie;
struct rasops_info *ri = &scr->scr_ri;
ri->ri_depth = sc->sc_bits_per_pixel;
ri->ri_width = sc->sc_width;
ri->ri_height = sc->sc_height;
ri->ri_stride = sc->sc_linebytes;
ri->ri_flg = RI_CENTER | RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
rasops_init(ri, 0, 0);
ri->ri_caps = WSSCREEN_WSCOLORS;
rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
sc->sc_width / ri->ri_font->fontwidth);
ri->ri_hw = scr;
ri->ri_ops.copyrows = voodoofb_copyrows;
ri->ri_ops.copycols = voodoofb_copycols;
ri->ri_ops.eraserows = voodoofb_eraserows;
ri->ri_ops.erasecols = voodoofb_erasecols;
ri->ri_ops.cursor = voodoofb_cursor;
if (FONT_IS_ALPHA(ri->ri_font)) {
ri->ri_ops.putchar = voodoofb_putchar_aa;
ri->ri_ops.allocattr(ri, WS_DEFAULT_FG, WS_DEFAULT_BG,
0, &sc->sc_defattr);
sc->sc_kernattr = (WS_KERNEL_FG << 24) | (WS_KERNEL_BG << 16);
voodoofb_init_glyphcache(sc, ri);
} else {
ri->ri_ops.putchar = voodoofb_putchar;
}
}
#if 0
int
voodoofb_load_font(void *v, void *cookie, struct wsdisplay_font *data)
{
return 0;
}
#endif
#ifdef VOODOOFB_ENABLE_INTR
static int
voodoofb_intr(void *arg)
{
struct voodoofb_softc *sc = arg;
voodoo3_write32(sc, V3_STATUS, 0); /* clear interrupts */
return 1;
}
#endif
/* video mode stuff */
#define REFFREQ 14318 /* .18 */
#define ABS(a) ((a < 0) ? -a : a)
static int
voodoofb_calc_pll(int freq, int *f_out, int isBanshee)
{
int m, n, k, best_m, best_n, best_k, f_cur, best_error;
int minm, maxm;
best_error = freq;
best_n = best_m = best_k = 0;
if (isBanshee) {
minm = 24;
maxm = 24;
} else {
minm = 1;
maxm = 57;
/* This used to be 64, alas it seems the last 8 (funny that ?)
* values cause jittering at lower resolutions. I've not done
* any calculations to what the adjustment affects clock ranges,
* but I can still run at 1600x1200@75Hz */
}
for (n = 1; n < 256; n++) {
f_cur = REFFREQ * (n + 2);
if (f_cur < freq) {
f_cur = f_cur / 3;
if (freq - f_cur < best_error) {
best_error = freq - f_cur;
best_n = n;
best_m = 1;
best_k = 0;
continue;
}
}
for (m = minm; m < maxm; m++) {
for (k = 0; k < 4; k++) {
f_cur = REFFREQ * (n + 2) / (m + 2) / (1 << k);
if (ABS(f_cur - freq) < best_error) {
best_error = ABS(f_cur - freq);
best_n = n;
best_m = m;
best_k = k;
}
}
}
}
n = best_n;
m = best_m;
k = best_k;
*f_out = REFFREQ * (n + 2) / (m + 2) / (1 << k);
return ( n << 8) | (m << 2) | k;
}
static void
voodoofb_setup_monitor(struct voodoofb_softc *sc, const struct videomode *vm)
{
struct voodoo_regs mod;
struct voodoo_regs *mode;
uint32_t horizontal_display_end, horizontal_sync_start,
horizontal_sync_end, horizontal_total,
horizontal_blanking_start, horizontal_blanking_end;
uint32_t vertical_display_enable_end, vertical_sync_start,
vertical_sync_end, vertical_total, vertical_blanking_start,
vertical_blanking_end;
uint32_t wd; // CRTC offset
int i;
uint8_t misc;
memset(&mod, 0, sizeof(mod));
mode = &mod;
wd = (vm->hdisplay >> 3) - 1;
horizontal_display_end = (vm->hdisplay >> 3) - 1;
horizontal_sync_start = (vm->hsync_start >> 3) - 1;
horizontal_sync_end = (vm->hsync_end >> 3) - 1;
horizontal_total = (vm->htotal >> 3) - 1;
horizontal_blanking_start = horizontal_display_end;
horizontal_blanking_end = horizontal_total;
vertical_display_enable_end = vm->vdisplay - 1;
vertical_sync_start = vm->vsync_start; // - 1;
vertical_sync_end = vm->vsync_end; // - 1;
vertical_total = vm->vtotal - 2;
vertical_blanking_start = vertical_display_enable_end;
vertical_blanking_end = vertical_total;
#if 0
misc = 0x0f |
(vm->hdisplay < 400 ? 0xa0 :
vm->hdisplay < 480 ? 0x60 :
vm->hdisplay < 768 ? 0xe0 : 0x20);
#else
misc = 0x2f;
if (vm->flags & VID_NHSYNC)
misc |= HSYNC_NEG;
if (vm->flags & VID_NVSYNC)
misc |= VSYNC_NEG;
#ifdef VOODOOFB_DEBUG
printf("misc: %02x\n", misc);
#endif
#endif
mode->vr_seq[0] = 3;
mode->vr_seq[1] = 1;
mode->vr_seq[2] = 8;
mode->vr_seq[3] = 0;
mode->vr_seq[4] = 6;
/* crtc regs start */
mode->vr_crtc[0] = horizontal_total - 4;
mode->vr_crtc[1] = horizontal_display_end;
mode->vr_crtc[2] = horizontal_blanking_start;
mode->vr_crtc[3] = 0x80 | (horizontal_blanking_end & 0x1f);
mode->vr_crtc[4] = horizontal_sync_start;
mode->vr_crtc[5] = ((horizontal_blanking_end & 0x20) << 2) |
(horizontal_sync_end & 0x1f);
mode->vr_crtc[6] = vertical_total;
mode->vr_crtc[7] = ((vertical_sync_start & 0x200) >> 2) |
((vertical_display_enable_end & 0x200) >> 3) |
((vertical_total & 0x200) >> 4) |
0x10 |
((vertical_blanking_start & 0x100) >> 5) |
((vertical_sync_start & 0x100) >> 6) |
((vertical_display_enable_end & 0x100) >> 7) |
((vertical_total & 0x100) >> 8);
mode->vr_crtc[8] = 0;
mode->vr_crtc[9] = 0x40 |
((vertical_blanking_start & 0x200) >> 4);
mode->vr_crtc[10] = 0;
mode->vr_crtc[11] = 0;
mode->vr_crtc[12] = 0;
mode->vr_crtc[13] = 0;
mode->vr_crtc[14] = 0;
mode->vr_crtc[15] = 0;
mode->vr_crtc[16] = vertical_sync_start;
mode->vr_crtc[17] = (vertical_sync_end & 0x0f) | 0x20;
mode->vr_crtc[18] = vertical_display_enable_end;
mode->vr_crtc[19] = wd; // CRTC offset
mode->vr_crtc[20] = 0;
mode->vr_crtc[21] = vertical_blanking_start;
mode->vr_crtc[22] = vertical_blanking_end + 1;
mode->vr_crtc[23] = 128;
mode->vr_crtc[24] = 255;
/* overflow registers */
mode->vr_crtc[CRTC_HDISP_EXT] =
(horizontal_total&0x100) >> 8 |
(horizontal_display_end & 0x100) >> 6 |
(horizontal_blanking_start & 0x100) >> 4 |
(horizontal_blanking_end & 0x40) >> 1 |
(horizontal_sync_start & 0x100) >> 2 |
(horizontal_sync_end & 0x20) << 2;
mode->vr_crtc[CRTC_VDISP_EXT] =
(vertical_total & 0x400) >> 10 |
(vertical_display_enable_end & 0x400) >> 8 | /* the manual is contradictory here */
(vertical_blanking_start & 0x400) >> 6 |
(vertical_blanking_end & 0x400) >> 4;
/* attr regs start */
mode->vr_attr[0] = 0;
mode->vr_attr[1] = 0;
mode->vr_attr[2] = 0;
mode->vr_attr[3] = 0;
mode->vr_attr[4] = 0;
mode->vr_attr[5] = 0;
mode->vr_attr[6] = 0;
mode->vr_attr[7] = 0;
mode->vr_attr[8] = 0;
mode->vr_attr[9] = 0;
mode->vr_attr[10] = 0;
mode->vr_attr[11] = 0;
mode->vr_attr[12] = 0;
mode->vr_attr[13] = 0;
mode->vr_attr[14] = 0;
mode->vr_attr[15] = 0;
mode->vr_attr[16] = 1;
mode->vr_attr[17] = 0;
mode->vr_attr[18] = 15;
mode->vr_attr[19] = 0;
/* attr regs end */
/* graph regs start */
mode->vr_graph[0] = 159;
mode->vr_graph[1] = 127;
mode->vr_graph[2] = 127;
mode->vr_graph[3] = 131;
mode->vr_graph[4] = 130;
mode->vr_graph[5] = 142;
mode->vr_graph[6] = 30;
mode->vr_graph[7] = 245;
mode->vr_graph[8] = 0;
vga_outb(sc, MISC_W, misc | 0x01);
for(i = 0; i < 5; i++)
voodoo3_write_seq(sc, i, mode->vr_seq[i]);
for (i = 0; i < CRTC_PCI_READBACK; i ++)
voodoo3_write_crtc(sc, i, mode->vr_crtc[i]);
for (i = 0; i < 0x14; i ++)
voodoo3_write_attr(sc, i, mode->vr_attr[i]);
for (i = 0; i < 0x09; i ++)
voodoo3_write_gra(sc, i, mode->vr_graph[i]);
}
static void
voodoofb_set_videomode(struct voodoofb_softc *sc,
const struct videomode *vm)
{
uint32_t miscinit0 = 0;
int vidpll, fout;
uint32_t vidproc = VIDPROCDEFAULT;
uint32_t bpp = 1; /* for now */
uint32_t bytes_per_row = vm->hdisplay * bpp;
sc->sc_bits_per_pixel = bpp << 3;
sc->sc_width = vm->hdisplay;
sc->sc_height = vm->vdisplay;
sc->sc_linebytes = bytes_per_row;
voodoofb_setup_monitor(sc, vm);
vidproc &= ~(0x1c0000); /* clear bits 18 to 20, bpp in vidproccfg */
/* enable bits 18 to 20 to the required bpp */
vidproc |= ((bpp - 1) << VIDCFG_PIXFMT_SHIFT);
vidpll = voodoofb_calc_pll(vm->dot_clock, &fout, 0);
#ifdef VOODOOFB_DEBUG
uint32_t vp;
vp = voodoo3_read32(sc, VIDPROCCFG);
printf("old vidproc: %08x\n", vp);
printf("pll: %08x %d\n", vidpll, fout);
#endif
/* bit 10 of vidproccfg, is enabled or disabled as needed */
switch (bpp) {
case 1:
/*
* bit 10 off for palettized modes only, off means
* palette is used
*/
vidproc &= ~(1 << 10);
break;
#if 0
case 2:
#if __POWERPC__
miscinit0 = 0xc0000000;
#endif
/* bypass palette for 16bit modes */
vidproc |= (1 << 10);
break;
case 4:
#if __POWERPC__
miscinit0 = 0x40000000;
#endif
vidproc |= (1 << 10); /* Same for 32bit modes */
break;
#endif
default:
printf("We support only 8 bit for now\n");
return;
}
voodoofb_wait_idle(sc);
voodoo3_write32(sc, MISCINIT1, voodoo3_read32(sc, MISCINIT1) | 0x01);
voodoo3_make_room(sc, 4);
voodoo3_write32(sc, VGAINIT0, 4928);
voodoo3_write32(sc, DACMODE, 0);
voodoo3_write32(sc, VIDDESKSTRIDE, bytes_per_row);
voodoo3_write32(sc, PLLCTRL0, vidpll);
voodoo3_make_room(sc, 5);
voodoo3_write32(sc, VIDSCREENSIZE, sc->sc_width |
(sc->sc_height << 12));
voodoo3_write32(sc, VIDDESKSTART, 0);
vidproc &= ~VIDCFG_HWCURSOR_ENABLE;
voodoo3_write32(sc, VIDPROCCFG, vidproc);
voodoo3_write32(sc, VGAINIT1, 0);
voodoo3_write32(sc, MISCINIT0, miscinit0);
#ifdef VOODOOFB_DEBUG
printf("vidproc: %08x\n", vidproc);
#endif
voodoo3_make_room(sc, 8);
voodoo3_write32(sc, SRCBASE, 0);
voodoo3_write32(sc, DSTBASE, 0);
voodoo3_write32(sc, COMMANDEXTRA_2D, 0);
voodoo3_write32(sc, CLIP0MIN, 0);
voodoo3_write32(sc, CLIP0MAX, 0x0fff0fff);
voodoo3_write32(sc, CLIP1MIN, 0);
voodoo3_write32(sc, CLIP1MAX, 0x0fff0fff);
voodoo3_write32(sc, SRCXY, 0);
voodoofb_wait_idle(sc);
printf("%s: switched to %dx%d, %d bit\n", device_xname(sc->sc_dev),
sc->sc_width, sc->sc_height, sc->sc_bits_per_pixel);
sc->sc_numglyphs = 0; /* invalidate the glyph cache */
}
static void
voodoofb_init(struct voodoofb_softc *sc)
{
/* XXX */
uint32_t vgainit0 = 0;
uint32_t vidcfg = 0;
#ifdef VOODOOFB_DEBUG
printf("initializing engine...");
#endif
vgainit0 = voodoo3_read32(sc, VGAINIT0);
#ifdef VOODOOFB_DEBUG
printf("vga: %08x", vgainit0);
#endif
vgainit0 |=
VGAINIT0_8BIT_DAC |
VGAINIT0_EXT_ENABLE |
VGAINIT0_WAKEUP_3C3 |
VGAINIT0_ALT_READBACK |
VGAINIT0_EXTSHIFTOUT;
vidcfg = voodoo3_read32(sc, VIDPROCCFG);
#ifdef VOODOOFB_DEBUG
printf(" vidcfg: %08x\n", vidcfg);
#endif
vidcfg |=
VIDCFG_VIDPROC_ENABLE |
VIDCFG_DESK_ENABLE;
vidcfg &= ~VIDCFG_HWCURSOR_ENABLE;
voodoo3_make_room(sc, 2);
voodoo3_write32(sc, VGAINIT0, vgainit0);
voodoo3_write32(sc, VIDPROCCFG, vidcfg);
voodoo3_make_room(sc, 8);
voodoo3_write32(sc, SRCBASE, 0);
voodoo3_write32(sc, DSTBASE, 0);
voodoo3_write32(sc, COMMANDEXTRA_2D, 0);
voodoo3_write32(sc, CLIP0MIN, 0);
voodoo3_write32(sc, CLIP0MAX, 0x1fff1fff);
voodoo3_write32(sc, CLIP1MIN, 0);
voodoo3_write32(sc, CLIP1MAX, 0x1fff1fff);
voodoo3_write32(sc, SRCXY, 0);
voodoofb_wait_idle(sc);
}
#define MAX_HRES 1700 /*
* XXX in theory we can go higher but I
* couldn't get anything above 1680 x 1200
* to work, so until I find out why, it's
* disabled so people won't end up with a
* blank screen
*/
#define MODE_IS_VALID(m, mclk) (((m)->dot_clock <= (mclk)) && \
((m)->hdisplay < MAX_HRES))
static void
voodoofb_setup_i2c(struct voodoofb_softc *sc)
{
int i;
/* Fill in the i2c tag */
iic_tag_init(&sc->sc_i2c);
sc->sc_i2c.ic_cookie = sc;
sc->sc_i2c.ic_send_start = voodoofb_i2c_send_start;
sc->sc_i2c.ic_send_stop = voodoofb_i2c_send_stop;
sc->sc_i2c.ic_initiate_xfer = voodoofb_i2c_initiate_xfer;
sc->sc_i2c.ic_read_byte = voodoofb_i2c_read_byte;
sc->sc_i2c.ic_write_byte = voodoofb_i2c_write_byte;
sc->sc_i2creg = voodoo3_read32(sc, VIDSERPARPORT);
#ifdef VOODOOFB_DEBUG
printf("data: %08x\n", sc->sc_i2creg);
#endif
sc->sc_i2creg |= VSP_ENABLE_IIC0;
sc->sc_i2creg &= ~(VSP_SDA0_OUT | VSP_SCL0_OUT);
voodoo3_write32(sc, VIDSERPARPORT, sc->sc_i2creg);
/* zero out the EDID buffer */
memset(sc->sc_edid_data, 0, 128);
/* Some monitors don't respond first time */
i = 0;
while (sc->sc_edid_data[1] == 0 && i++ < 3)
ddc_read_edid(&sc->sc_i2c, sc->sc_edid_data, 128);
if (i < 3) {
if (edid_parse(sc->sc_edid_data, &sc->sc_edid_info) != -1) {
#ifdef VOODOOFB_DEBUG
edid_print(&sc->sc_edid_info);
#endif
/*
* Now pick a mode.
* How do we know our max. pixel clock?
* All Voodoo3 should support at least 250MHz.
* All Voodoo3 I've seen so far have at least 8MB
* which we're not going to exhaust either in 8bit.
*/
if ((sc->sc_edid_info.edid_preferred_mode != NULL)) {
struct videomode *m =
sc->sc_edid_info.edid_preferred_mode;
if (MODE_IS_VALID(m, sc->sc_max_clock)) {
sc->sc_videomode = m;
} else {
aprint_error_dev(sc->sc_dev,
"unable to use preferred mode\n");
}
}
/*
* if we can't use the preferred mode go look for the
* best one we can support
*/
if (sc->sc_videomode == NULL) {
int n = 0;
struct videomode *m =
sc->sc_edid_info.edid_modes;
sort_modes(sc->sc_edid_info.edid_modes,
&sc->sc_edid_info.edid_preferred_mode,
sc->sc_edid_info.edid_nmodes);
while ((sc->sc_videomode == NULL) &&
(n < sc->sc_edid_info.edid_nmodes)) {
if (MODE_IS_VALID(&m[n],
sc->sc_max_clock)) {
sc->sc_videomode = &m[n];
}
n++;
}
}
}
}
}
/* I2C bitbanging */
static void voodoofb_i2cbb_set_bits(void *cookie, uint32_t bits)
{
struct voodoofb_softc *sc = cookie;
uint32_t out;
out = bits >> 2; /* bitmasks match the IN bits */
voodoo3_write32(sc, VIDSERPARPORT, sc->sc_i2creg | out);
}
static void voodoofb_i2cbb_set_dir(void *cookie, uint32_t dir)
{
/* Nothing to do */
}
static uint32_t voodoofb_i2cbb_read(void *cookie)
{
struct voodoofb_softc *sc = cookie;
uint32_t bits;
bits = voodoo3_read32(sc, VIDSERPARPORT);
return bits;
}
/* higher level I2C stuff */
static int
voodoofb_i2c_send_start(void *cookie, int flags)
{
return (i2c_bitbang_send_start(cookie, flags, &voodoofb_i2cbb_ops));
}
static int
voodoofb_i2c_send_stop(void *cookie, int flags)
{
return (i2c_bitbang_send_stop(cookie, flags, &voodoofb_i2cbb_ops));
}
static int
voodoofb_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
{
return (i2c_bitbang_initiate_xfer(cookie, addr, flags,
&voodoofb_i2cbb_ops));
}
static int
voodoofb_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
{
return (i2c_bitbang_read_byte(cookie, valp, flags, &voodoofb_i2cbb_ops));
}
static int
voodoofb_i2c_write_byte(void *cookie, uint8_t val, int flags)
{
return (i2c_bitbang_write_byte(cookie, val, flags, &voodoofb_i2cbb_ops));
}
/* glyph cache */
static void
voodoofb_init_glyphcache(struct voodoofb_softc *sc, struct rasops_info *ri)
{
int bufsize, i;
if (sc->sc_numglyphs != 0) {
/* re-initialize */
if (ri->ri_font == sc->sc_font) {
/* nothing to do, keep using the same cache */
return;
}
}
sc->sc_cache = (ri->ri_width * ri->ri_stride + 3) & 0xfffffffc;
bufsize = sc->sc_memsize - sc->sc_cache;
sc->sc_numglyphs = bufsize / ri->ri_fontscale;
sc->sc_usedglyphs = 0;
for (i = 0; i < 256; i++) {
sc->sc_glyphs_kernattr[i] = 0;
sc->sc_glyphs_defattr[i] = 0;
}
sc->sc_fmt = ri->ri_font->fontwidth | FMT_8BIT;
}
|