summaryrefslogblamecommitdiffstats
path: root/private/ntos/ndis/madge/driver/hwi_pci2.c
blob: 40a5320eda95081d72de5eb64646205f5cd66032 (plain) (tree)
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































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                                 
/****************************************************************************
*                                                                          
* HWI_PCI2.C : Part of the FASTMAC TOOL-KIT (FTK)                      
* 
* HARDWARE INTERFACE MODULE FOR PCI CARDS
*                                                                         
* Copyright (c) Madge Networks Ltd. 1994                              
* 
* COMPANY CONFIDENTIAL                                                        
*                                                                          
*                                                                          
*****************************************************************************
*                                                                          
* The purpose of the Hardware Interface (HWI) is to supply an adapter card 
* independent interface to any driver.  It  performs  nearly  all  of  the 
* functions  that  involve  affecting  SIF registers on the adapter cards. 
* This includes downloading code to, initializing, and removing adapters.  
*                                                                          
* The HWI_PCIT.C module contains the routines specific to the Smart 16/4
* PCI(BM) based on the Madge PCI ASIC, this card supports Pseudo DMA, and
* bus master DMA.
*                                                                          
*****************************************************************************

/*---------------------------------------------------------------------------
|                                                                          
| DEFINITIONS                                         
|                                                                          
---------------------------------------------------------------------------*/

#define     PCI_PCI2_DEVICE_ID   2

#include    "ftk_defs.h"

/*---------------------------------------------------------------------------
|                                                                          
|  MODULE ENTRY POINTS
|                                                                          
---------------------------------------------------------------------------*/

#include "ftk_intr.h"   /* routines internal to FTK */
#include "ftk_extr.h"   /* routines provided or used by external FTK user */

BYTE  bEepromByteStore;
BYTE  bLastDataBit;

#ifndef FTK_NO_PCI2

/*---------------------------------------------------------------------------
|                                                                          
|  LOCAL PROCEDURES
|                                                                          
---------------------------------------------------------------------------*/

local WBOOLEAN
hwi_pci2_read_node_address(
    ADAPTER * adapter
    );

local WORD
hwi_at24_read_a_word(
    ADAPTER * adapter,
    WORD word_address
    );

#ifndef FTK_NO_PROBE
/****************************************************************************
*
*                         hwi_pci2_probe_card
*                         ==================
*
*
* PARAMETERS (passed by hwi_probe_adapter) :
* ==========================================
*
* PROBE * resources
*
* resources is an array structures used to identify and record specific 
* information about adapters found.
*
* UINT    length
*
* length is the number of structures pointed to by reources.
*
* WORD *  valid_locations
*
* valid_locations is normally an array of IO locations to examine for the
* presence of an adapter. However for PCI adapters the io location is read
* from the BIOS, so this array can remain empty.
*
* UINT    number_locations
*
* This is the number of IO locations in the above list.
*
* BODY :
* ======
* 
* The  hwi_pci2_probe_card routine is called by  hwi_probe_adapter.  It
* reads the id registers to find the type of card and also reads the IRQ. 
*
*
* RETURNS :
* =========
* 
* The routine returns the number of adapters found, or PROBE_FAILURE if
* there's a problem.
*
****************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_pci2_probe_card)
#endif

export UINT
hwi_pci2_probe_card(
    PROBE * Resources,
    UINT    NumberOfResources,
    WORD *  IOMask,
    UINT    NumberIO
    )
{
    WORD    i;
    WORD    Handle;

    if (!sys_pci_valid_machine())
    {
        return 0;
    }

    for (i=0;i<NumberOfResources;i++)
    {
        if (!sys_pci_find_card(&Handle, i,PCI_PCI2_DEVICE_ID))
        {
            break;
        }
        
        Resources[i].pci_handle = Handle;
        Resources[i].adapter_card_bus_type = ADAPTER_CARD_PCI2_BUS_TYPE;

        if (!sys_pci_get_io_base(Handle, &(Resources[i].io_location)))
        {
            return PROBE_FAILURE;
        }

        if (!sys_pci_get_irq(Handle, &(Resources[i].interrupt_number)))
        {
            return PROBE_FAILURE;
        }


        /*
         * We can't read the memory size from the serial EEPROM until the
         * hwi_pci2_install_card function is called.
         */

        Resources[i].adapter_ram_size  = 512;
    
        Resources[i].adapter_card_type = ADAPTER_CARD_TYPE_16_4_PCI2;

        Resources[i].transfer_mode     = PIO_DATA_TRANSFER_MODE;

    }

    return i;
}
#endif

/****************************************************************************
*                                                                          
*                      hwi_pci2_install_card                                
*                      ====================                                
*                                                                          
*                                                                          
* PARAMETERS (passed by hwi_install_adapter) :                             
* ============================================                             
*                                                                          
* ADAPTER * adapter                                                        
*                                                                          
* This structure is used to identify and record specific information about 
* the required adapter.                                                    
*                                                                          
* DOWNLOAD_IMAGE * download_image                                          
*                                                                          
* This  is  the code to be downloaded to the adapter. The image must be of 
* the correct type i.e.  must be downloadable into  the  adapter.  If  the 
* pointer is 0 downloading is not done.                                    
*                                                                          
*                                                                          
* BODY :                                                                   
* ======                                                                   
*                                                                          
* hwi_pci2_install_card is called by  hwi_install_adapter.  It  sets up     
* the adapter card and downloads the required  code  to  it.  Firstly,  it 
* checks there is a valid adapter at the required IO  address  by  reading 
* the node address from the BIA PROM. It then sets up  and  checks various 
* on-board registers for correct operation.                                
*                                                                          
* Then, it halts the EAGLE, downloads the code,  restarts  the  EAGLE  and 
* waits  up to 3 seconds  for  a valid  bring-up  code. If  interrupts are 
* required,  these  are  enabled  by  operating  system  specific   calls. 
* The adapter is set up for Eagle Pseudo-DMA, since real DMA is not used.  
*                                                                          
*                                                                          
* RETURNS :                                                                
* =========                                                                
*                                                                          
* The routine returns TRUE if it succeeds.  If this routine fails (returns 
* FALSE)  then a subsequent call to driver_explain_error, with the adapter 
* handle corresponding to the adapter parameter used here,  will  give  an 
* explanation.                                                             
*                                                                          
****************************************************************************/
                                                                          
#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_pci2_install_card)
#endif

export WBOOLEAN  
hwi_pci2_install_card(     
    ADAPTER *        adapter,
    DOWNLOAD_IMAGE * download_image
    )
{
    ADAPTER_HANDLE adapter_handle = adapter->adapter_handle;
    WORD           sif_base;    
    WORD           ring_speed;
    WORD           wHardFeatures;
    BYTE           bTemp;

    /*
     * These things can all be assumed for the PCI2 Card.
     */

    adapter->adapter_card_type     = ADAPTER_CARD_TYPE_16_4_PCI2;
    adapter->adapter_card_revision = ADAPTER_CARD_16_4_PCI2;
    adapter->edge_triggered_ints   = FALSE;
    adapter->mc32_config           = 0;

    /* 
     * Start off by assuming we will use pseudo DMA.
     */

    adapter->EaglePsDMA            = TRUE;

    /*
     * Save IO locations of SIF registers.
     */

    sif_base = adapter->io_location + PCI2_SIF_OFFSET;

    adapter->sif_dat     = sif_base + EAGLE_SIFDAT;
    adapter->sif_datinc  = sif_base + EAGLE_SIFDAT_INC;
    adapter->sif_adr     = sif_base + EAGLE_SIFADR;
    adapter->sif_int     = sif_base + EAGLE_SIFINT;
    adapter->sif_acl     = sif_base + EAGLE_SIFACL;
    adapter->sif_adx     = sif_base + EAGLE_SIFADX;
    adapter->sif_dmalen  = sif_base + EAGLE_DMALEN;
    adapter->sif_sdmadat = sif_base + EAGLE_SDMADAT;
    adapter->sif_sdmaadr = sif_base + EAGLE_SDMAADR;
    adapter->sif_sdmaadx = sif_base + EAGLE_SDMAADX;

    adapter->io_range    = PCI2_IO_RANGE;

    /*
     * RESET the Card
     */

#ifndef FTK_NO_IO_ENABLE
     macro_enable_io(adapter);
#endif
    
    /*
     *  Clear all the RESET bits, wait at least 14uS and then assert all three
     *  , turning on one at a time, in the following sequence
     *  - Set CHIP_NRES
     *  - Set SIF_NRES
     *  - Set FIFO_NRES
     */

    bTemp = sys_insb(
                adapter_handle, 
                (WORD) (adapter->io_location + PCI2_RESET));

    bTemp &= ~(PCI2_CHIP_NRES | PCI2_FIFO_NRES | PCI2_SIF_NRES);

    sys_outsb(
        adapter_handle, 
        (WORD) (adapter->io_location + PCI2_RESET), 
        bTemp);

    /*
     *  Wait 14 uS before taking it out of reset.
     */

    sys_wait_at_least_microseconds(14);

    bTemp |= PCI2_CHIP_NRES;

    sys_outsb(
        adapter_handle, 
        (WORD) (adapter->io_location + PCI2_RESET), 
        bTemp);

    bTemp |= PCI2_SIF_NRES;

    sys_outsb(
        adapter_handle, 
        (WORD) (adapter->io_location + PCI2_RESET), 
        bTemp);

    bTemp |= PCI2_FIFO_NRES;

    sys_outsb(adapter_handle, 
        (WORD) (adapter->io_location + PCI2_RESET), 
        bTemp);


    if (!hwi_pci2_read_node_address(adapter))
    {
#ifndef FTK_NO_IO_ENABLE
        macro_disable_io(adapter);
#endif
	adapter->error_record.type  = ERROR_TYPE_HWI;
	adapter->error_record.value = HWI_E_05_ADAPTER_NOT_FOUND;
	return  FALSE;
    }
    
    /* 
     * If this is 1 the card is @ 4Mbit/s 0 16MBit/s.
     */

    ring_speed = hwi_at24_read_a_word(adapter, PCI2_EEPROM_RING_SPEED);

    /*
     * Get the amount of RAM from the serial EEPROM (in units of 128k).
     */

    adapter->adapter_ram_size =  hwi_at24_read_a_word(
                                     adapter,
                                     PCI2_EEPROM_RAM_SIZE) * 128;


    /*
     * This flag tells us if the card supports DMA, and if it supports release
     * 4.31 software.
     */

    wHardFeatures = hwi_at24_read_a_word(adapter, PCI2_HWF2);

#define  RELEASE_431 1
#ifdef RELEASE_431

    if (!(wHardFeatures & PCI2_HW2_431_READY))
    {
        /*
         * This card does not support Release 4.31 software.
         */

        adapter->error_record.type  = ERROR_TYPE_HWI;
        adapter->error_record.value = HWI_E_05_ADAPTER_NOT_FOUND;
        return  FALSE;
    }

#endif

    if (adapter->transfer_mode == DMA_DATA_TRANSFER_MODE)
    {
        /*
         * Does this card support DMA ???
         */

        if (wHardFeatures & PCI2_BUS_MASTER_ONLY)
        {
            adapter->EaglePsDMA = FALSE;

            /*
             * Need to set the MASTER ENABLE bit in the PCI Command register
             * otherwise DMA will not work and will hang the machine.
             * The BIOS should do this, but some don't.
             */

            sys_pci_read_config_byte(
                adapter_handle, 
                PCI_CONFIG_COMMAND, 
                &bTemp);

            bTemp |= PCI_CONFIG_BUS_MASTER_ENABLE;

            sys_pci_write_config_byte(
                adapter_handle, 
                PCI_CONFIG_COMMAND, 
                bTemp);
        }
        else
        {
#ifndef FTK_NO_IO_ENABLE
            macro_disable_io(adapter);
#endif
	    adapter->error_record.type  = ERROR_TYPE_HWI;
	    adapter->error_record.value = HWI_E_06_CANNOT_USE_DMA;
	    return FALSE;
        }

    }

    /*
     * If we've using pseudo DMA then we need a software handshake.
     */

    if (adapter->EaglePsDMA)
    {
        adapter->mc32_config = MC_AND_ISACP_USE_PIO;
    }

    /*
     * Set the ring speed. If the user has specified a value then we will   
     * use that, otherwise we will use the value read from the EEPROM.      
     */

    /*
     * These NSEL bits are different to other cards, bit 0 must be ~bit1
     * There is a missing NOT gate in the ASIC.
     */

    if (adapter->set_ring_speed == 16)
    {
        adapter->nselout_bits = 0;
    }
    else if (adapter->set_ring_speed == 4)
    {
        adapter->nselout_bits = 2;
    }
    else if (ring_speed == PCI2_EEPROM_4MBITS)
    {
        adapter->nselout_bits = 2;
    }
    else
    {
        adapter->nselout_bits = 0;
    }

    /*
     * Set the interrupt control register to enable SIF interrupts and
     * PCI Error interrupts, although the ISR does nowt about the latter
     * at present.
     */

    sys_outsb( adapter->adapter_handle, (WORD) (adapter->io_location + PCI2_INTERRUPT_CONTROL), PCI2_SINTEN | PCI2_PCI_ERR_EN);  

    /*
     * Halt the Eagle prior to downloading the MAC code - this will also    
     * write the interrupt bits into the SIFACL register, where the MAC can 
     * find them.                                                           
     */

    hwi_halt_eagle(adapter);

    /*
     * Download code to adapter.                                             
     * View download image as a sequence of download records.                
     * Pass address of routine to set up DIO addresses on PCI cards.
     * If routine fails return failure (error record already filled in).
     */

    if (!hwi_download_code(
             adapter,
             (DOWNLOAD_RECORD *) download_image,
             hwi_pci2_set_dio_address))
    {
#ifndef FTK_NO_IO_ENABLE
        macro_disable_io(adapter);
#endif
        return FALSE;
    }

    /* 
     * Restart the Eagle to initiate bring up diagnostics.
     */

    hwi_start_eagle(adapter);

    /*
     * Wait for a valid bring up code, may wait 3 seconds.
     */
              
    if (!hwi_get_bring_up_code(adapter))
    {
#ifndef FTK_NO_IO_ENABLE
        macro_disable_io(adapter);
#endif
        return FALSE;
    }

    /*
     * Set DIO address to point to EAGLE DATA page 0x10000L.
     */

    hwi_pci2_set_dio_address(adapter, DIO_LOCATION_EAGLE_DATA_PAGE);

    /*
     * Get the ring speed, from the Eagle DIO space.
     */

    adapter->ring_speed = hwi_get_ring_speed(adapter);

    /*
     * Set maximum frame size from the ring speed.
     */

    adapter->max_frame_size = hwi_get_max_frame_size(adapter);

    /*
     * If not in polling mode then set up interrupts.                        
     * interrupts_on field is used when disabling interrupts for adapter.
     */

    if (adapter->interrupt_number != POLLING_INTERRUPTS_MODE)
    {
	adapter->interrupts_on = sys_enable_irq_channel(
                                     adapter_handle,
                                     adapter->interrupt_number);

	if (!adapter->interrupts_on)
	{
	    adapter->error_record.type  = ERROR_TYPE_HWI;
	    adapter->error_record.value = HWI_E_0B_FAIL_IRQ_ENABLE;
#ifndef FTK_NO_IO_ENABLE
            macro_disable_io(adapter);
#endif
            return  FALSE;
	}
    }
    else
    {
    	adapter->interrupts_on = TRUE;
    }

    /*
     * Return successfully.
     */

#ifndef FTK_NO_IO_ENABLE
    macro_disable_io(adapter);
#endif
    return TRUE;
}

/****************************************************************************
*                                                                          
*                      hwi_pci2_interrupt_handler                           
*                      =========================                           
*                                                                          
*                                                                          
* PARAMETERS (passed by hwi_interrupt_entry) :                             
* ==========================================                               
*                                                                          
* ADAPTER * adapter                                                        
*                                                                          
* This structure is used to identify and record specific information about 
* the required adapter.                                                    
*                                                                          
*                                                                          
* BODY :                                                                   
* ======                                                                   
*                                                                          
* The hwi_pci2_interrupt_handler routine  is  called, when an interrupt              
* occurs, by hwi_interrupt_entry.  It checks to see if a  particular  card 
* has interrupted. The interrupt could be generated by  the SIF for either 
* a PIO data transfer or a normal condition (received frame, SRB complete, 
* ARB indication etc). Note it could in fact be the case that no interrupt 
* has  occured  on the particular adapter being checked.                   
*                                                                          
* On normal SIF interrupts, the interrupt is acknowledged and cleared. The 
* value in the SIF interrupt register is recorded in order to pass  it  to 
* the driver_interrupt_entry routine (along with the adapter details).     
*                                                                          
* On PseudoDMA interrupts, the length, direction and physical  address  of 
* the  transfer  is  determined. A system provided routine is called to do 
* the data transfer itself.                                                
*                                                                          
*                                                                          
* RETURNS :                                                                
* =========                                                                
*                                                                          
* The routine always successfully completes.                               
*                                                                          
****************************************************************************/
                                                                         
#ifdef FTK_IRQ_FUNCTION
#pragma FTK_IRQ_FUNCTION(hwi_pci2_interrupt_handler)
#endif

export void     
hwi_pci2_interrupt_handler(   
    ADAPTER *      adapter
    )
{
    ADAPTER_HANDLE adapter_handle    = adapter->adapter_handle;
    WORD           sifacl;
    WORD           sifint_value;
    WORD           sifint_tmp;
    WORD           pio_addr_lo;
    WBOOLEAN       sifint_occurred = FALSE;
    WBOOLEAN       pioint_occurred = FALSE;
    DWORD          pio_addr_hi;
    WORD           pio_len_bytes;
    WBOOLEAN       pio_from_adapter;
    BYTE           bInt;
    BYTE     FAR * pio_address;
    WORD           saved_sifadr;

#ifndef FTK_NO_IO_ENABLE
    macro_enable_io(adapter);
#endif

    /*
     * Check for SIF interrupt or PIO interrupt.
     */

    /*
     * Read SIFINT, and then re-read to make sure value is stable.
     */

    sifint_value = sys_insw(adapter_handle, adapter->sif_int);
    do
    {
        sifint_tmp   = sifint_value;
        sifint_value = sys_insw(adapter_handle, adapter->sif_int);
    } 
    while (sifint_tmp != sifint_value);

    /*
     * Given the SIFINT value, we can check one of the bits in it to see
     * if that is what caused the interrupt.
     */

    if ((sifint_value & EAGLE_SIFINT_SYSTEM) != 0)
    {
        /*
         * A SIF interrupt has occurred.
	 * This could be an SRB free, an adapter check or a received frame
         * interrupt.
	 * Note that we do not process it yet - we wait until we have EOI'd 
	 * the interrupt controller.                                        
         */

        sifint_occurred = TRUE;

        /*
	 * Clear EAGLE_SIFINT_HOST_IRQ to acknowledge interrupt at SIF.      
         */

	sys_outsw(adapter_handle, adapter->sif_int, 0);

        /*
         * Call driver with details of SIF interrupt.
         */

        driver_interrupt_entry(adapter_handle, adapter, sifint_value);
    }

    
   /*
    * Now read the SIFACL register to check for a PseudoDMA interrupt.
    */

    if (adapter->transfer_mode != DMA_DATA_TRANSFER_MODE)
    {
        sifacl = sys_insw(adapter_handle, adapter->sif_acl);

        if ((sifacl & EAGLE_SIFACL_SWHRQ) != 0)
        {
             /*
              * PIO interrupt has occurred. Transfer data to/from adapter.
              */

             pioint_occurred = TRUE;

             /*
              * Preserve SIFADR.
              */

	     saved_sifadr = sys_insw(adapter_handle, adapter->sif_adr);

             /*
              * Start the software handshake.
              */

	     sys_outsw(adapter_handle, adapter->sif_adr, DIO_LOCATION_DMA_CONTROL);
	     sys_outsw(adapter_handle, adapter->sif_dat, 0);

             /*
              * By writing the SWHLDA bit, we "start" the transfer,
              * causing the SDMA registers to mapped in.
              */

             macro_setw_bit(
                 adapter_handle,
                 adapter->sif_acl,
                 EAGLE_SIFACL_SWHLDA);
           
	     /*
              * Determine what direction the data transfer is to take place in.
              */

	     pio_from_adapter = sys_insw(
                                   adapter_handle,
                                   adapter->sif_acl) & EAGLE_SIFACL_SWDDIR;
                                
             pio_len_bytes    = sys_insw(
                                    adapter_handle,
                                    adapter->sif_dmalen);

             pio_addr_lo      = sys_insw(
                                    adapter_handle,
                                    adapter->sif_sdmaadr);

             pio_addr_hi      = (DWORD) sys_insw(
                                            adapter_handle,
                                            adapter->sif_sdmaadx);

             pio_address      = (BYTE FAR *) ((pio_addr_hi << 16) |
                                            ((DWORD) pio_addr_lo));


	     /*
              * Do the actual data transfer.
              */

	     /*
              * Note that Fastmac only copies whole WORDs to DWORD boundaries.
	      * FastmacPlus, however, can transfer any length to any address.
              */

             if (pio_from_adapter)
             {
                 /*
                  * Transfer into host memory from adapter.
                  */

		 /*
                  * First, check if host address is on an odd byte boundary.
                  */

		 if ((card_t)pio_address % 2)
		 {
		     pio_len_bytes--;
		     *(pio_address++) = sys_insb(
                                            adapter_handle,
                                            (WORD) (adapter->sif_sdmadat + 1));
		 }

		 /*
                  * Now transfer the bulk of the data.
                  */

		 sys_rep_insw(
                     adapter_handle,
                     adapter->sif_sdmadat,
                     pio_address,
                     (WORD) (pio_len_bytes >> 1));
                    
		 /*
                  * Finally transfer any trailing byte.
                  */

		 if (pio_len_bytes % 2)
                 {
		     *(pio_address+pio_len_bytes - 1) = 
                          sys_insb(adapter_handle, adapter->sif_sdmadat);
                 }
             }
	     else
	     {
                  /*
                   * Transfer into adapter memory from the host.
                   */

		  if ((card_t)pio_address % 2)
		  {
		      pio_len_bytes--;
		          sys_outsb(
                          adapter_handle,
                          (WORD) (adapter->sif_sdmadat + 1),
                          *(pio_address++));
		  }

		  sys_rep_outsw(
                      adapter_handle,
                      adapter->sif_sdmadat,
                      pio_address,
                      (WORD) (pio_len_bytes >> 1));

		  if (pio_len_bytes % 2)
                  {
		         sys_outsb(
                          adapter_handle,
                          adapter->sif_sdmadat,
                          *(pio_address+pio_len_bytes-1));
                  }
	     }

             /*
              * Wait for SWHLDA to go low, it is not safe to access 
              * normal SIF registers until this is the case.
              */

             do
             {
                 sifacl = sys_insw(adapter_handle, adapter->sif_acl);
             }
             while (sifacl & EAGLE_SIFACL_SWHLDA);

             /*
              * Finish the software handshake. We need a dummy ready so that
              * the SIF can stabalise.
              */

	     sys_outsw(adapter_handle, adapter->sif_adr, DIO_LOCATION_DMA_CONTROL);
	     sys_insw(adapter_handle, adapter->sif_dat);

	     sys_outsw(adapter_handle, adapter->sif_adr, DIO_LOCATION_DMA_CONTROL);
	     sys_outsw(adapter_handle, adapter->sif_dat, 0xFFFF);
	
             /*
              * Restore SIFDR.
              */

	     sys_outsw(adapter_handle, adapter->sif_adr, saved_sifadr);
         }
    }

    if (pioint_occurred || sifint_occurred)
    {
#ifndef FTK_NO_CLEAR_IRQ

        /*
         * Acknowledge/clear interrupt at interrupt controller.
         */

        sys_clear_controller_interrupt(
            adapter_handle,
            adapter->interrupt_number);

#endif
    }
    else
    {
        bInt = sys_insb(
                   adapter->adapter_handle, 
                   (WORD) (adapter->io_location + PCI2_INTERRUPT_STATUS));

        if (bInt & PCI2_PCI_INT)
        {
#ifndef FTK_NO_CLEAR_IRQ

	     sys_clear_controller_interrupt(
                 adapter_handle,
                 adapter->interrupt_number);

#endif
        }
    }

    /*
     * Let system know we have finished accessing the IO ports.
     */

#ifndef FTK_NO_IO_ENABLE
    macro_disable_io(adapter);
#endif

}


/****************************************************************************
*                                                                          
*                      hwi_pci2_remove_card                                 
*                      ===================                                 
*                                                                          
*                                                                          
* PARAMETERS (passed by hwi_remove_card)                                   
* ======================================                                   
*                                                                          
* ADAPTER * adapter                                                        
*                                                                          
* This structure is used to identify and record specific information about 
* the required adapter.                                                    
*                                                                          
*                                                                          
* BODY :                                                                   
* ======                                                                   
*                                                                          
* The hwi_smart16_remove_card routine is called by hwi_remove_adapter. It  
* disables interrupts if they are being used. It also resets the adapter.  
*                                                                          
*                                                                          
* RETURNS :                                                                
* =========                                                                
*                                                                          
* The routine always successfully completes.                               
*                                                                          
****************************************************************************/

#ifdef FTK_RES_FUNCTION
#pragma FTK_RES_FUNCTION(hwi_pci2_remove_card)
#endif
                                                                          
export void
hwi_pci2_remove_card(
    ADAPTER *      adapter
    )
{
    ADAPTER_HANDLE adapter_handle = adapter->adapter_handle;
    WORD           wGenConAddr    = adapter->io_location +
                                        PCI_GENERAL_CONTROL_REG;
    WORD           sifacl;
    BYTE           bTemp;

    /*
     * Interrupt must be disabled at adapter before unpatching interrupt.    
     * Even in polling mode we must turn off interrupts at adapter.
     */

#ifndef FTK_NO_IO_ENABLE
    macro_enable_io(adapter);
#endif

    sifacl = sys_insw(adapter_handle, adapter->sif_acl);
    sifacl = (sifacl & ~(EAGLE_SIFACL_PSDMAEN | EAGLE_SIFACL_SINTEN));
    sys_outsw(adapter_handle, adapter->sif_acl, sifacl);

    if (adapter->interrupts_on)
    {
        if (adapter->interrupt_number != POLLING_INTERRUPTS_MODE)
        {
	    sys_disable_irq_channel(
                adapter_handle,
                adapter->interrupt_number);
	}

	      adapter->interrupts_on = FALSE;
    }

    /*
     * RESET the Eagle
     */

    bTemp = sys_insb(
                adapter_handle, 
                (WORD) (adapter->io_location + PCI2_RESET));

    bTemp &= ~(PCI2_CHIP_NRES | PCI2_FIFO_NRES | PCI2_SIF_NRES);

    sys_outsb(
        adapter_handle, 
        (WORD) (adapter->io_location + PCI2_RESET), 
        bTemp);
   
    /*
     *  Wait 14 uS before taking it out of reset.
     */

    sys_wait_at_least_microseconds(14);

    bTemp |= (PCI2_CHIP_NRES | PCI2_FIFO_NRES | PCI2_SIF_NRES);  

    sys_outsb(
        adapter_handle, 
        (WORD) (adapter->io_location + PCI2_RESET), 
        bTemp);

#ifndef FTK_NO_IO_ENABLE
    macro_disable_io(adapter);
#endif
}


/****************************************************************************
*                                                                          
*                      hwi_pci2_set_dio_address                             
*                      =======================                             
*                                                                          
* The hwi_pci2_set_dio_address routine is used, with PCI cards, for         
* putting  a  32 bit DIO address into the SIF DIO address and extended DIO 
* address registers. Note that the extended  address  register  should  be 
* loaded first.                                                            
*                                                                          
****************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_pci2_set_dio_address)
#endif

export void
hwi_pci2_set_dio_address(
    ADAPTER *      adapter,
    DWORD          dio_address )
{
    ADAPTER_HANDLE adapter_handle = adapter->adapter_handle;
    WORD           sif_dio_adr    = adapter->sif_adr;
    WORD           sif_dio_adrx   = adapter->sif_adx;

    /*
     * Load extended DIO address register with top 16 bits of address.
     * Always load extended address register first.
     */

    sys_outsw(
        adapter_handle,
        sif_dio_adrx,
        (WORD)(dio_address >> 16));

    /* 
     * Load DIO address register with low 16 bits of address.
     */

    sys_outsw(
        adapter_handle,
        sif_dio_adr,
        (WORD)(dio_address & 0x0000FFFF));
}


/*---------------------------------------------------------------------------
|
| LOCAL PROCEDURES     
|
---------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------
|                                                                          
|                      hwi_pci2_read_node_address                           
|                      ==========================                           
|                                                                          
| The hwi_pci2_read_node_address routine reads in the node address from     
| the SEEPROM, and checks that it is a valid Madge node address.           
|                                                                          
---------------------------------------------------------------------------*/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_pci2_read_node_address)
#endif

local WBOOLEAN
hwi_pci2_read_node_address(
    ADAPTER * adapter
    )
{
    WORD      temp;

    temp = hwi_at24_read_a_word(adapter, PCIT_EEPROM_BIA_WORD0);
    adapter->permanent_address.byte[0] = (BYTE) ((temp     ) & 0x00ff);
    adapter->permanent_address.byte[1] = (BYTE) ((temp >> 8) & 0x00ff);

    temp = hwi_at24_read_a_word(adapter, PCIT_EEPROM_BIA_WORD1);
    adapter->permanent_address.byte[2] = (BYTE) ((temp     ) & 0x00ff);
    adapter->permanent_address.byte[3] = (BYTE) ((temp >> 8) & 0x00ff);
    
    temp = hwi_at24_read_a_word(adapter, PCIT_EEPROM_BIA_WORD2);
    adapter->permanent_address.byte[4] = (BYTE) ((temp     ) & 0x00ff);
    adapter->permanent_address.byte[5] = (BYTE) ((temp >> 8) & 0x00ff);

    return TRUE;
}

/***************************************************************************
*                                                                          *
* Local routines for accessing the AT93AT24 Serial EEPROM, this is the same*
* EEPROM fitted to the PNP card and the PCI 2 card.                        *
*                                                                          *
* The routines are 'nicked' from the PCI-T card with just write_bits and   *
* input having been changed to reflect I/O through I/O space not PCI config*
* space.                                                                   *
*                                                                          *
***************************************************************************/

local   void     hwi_at24_delay( ADAPTER * adapter );
local   void     hwi_at24_set_clk( ADAPTER * adapter );
local   void     hwi_at24_clr_clk( ADAPTER * adapter );
local   void     hwi_at24_twitch_clk( ADAPTER * adapter );
local   void     hwi_at24_start_bit( ADAPTER * adapter );
local   void     hwi_at24_stop_bit( ADAPTER * adapter );
local   WBOOLEAN hwi_at24_wait_ack( ADAPTER * adapter );
local   WBOOLEAN hwi_at24_dummy_wait_ack( ADAPTER * adapter );


/************************************************************************
 * Read the 3 EEPROM bits
 *
 * Inputs  : Adapter structure.
 *
 * Outputs : Value read from control register.
 ***********************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_at24_input)
#endif

local BYTE hwi_at24_input(ADAPTER * adapter)
{
   BYTE  bInput;

   bInput = sys_insb(
                adapter->adapter_handle, 
                (WORD) (adapter->io_location + PCI2_SEEPROM_CONTROL));

   /*
    * Store the 5 bits which don't interrest us
    */

   bEepromByteStore = bInput & (BYTE)0xF8;

   return bInput;
}


/************************************************************************
 * Write to the 3 EEPROM bits
 *
 * Inputs  : Adapter structure.
 *           The data to be written.
 *
 * Outputs : None.
 ***********************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_at24_write_bits)
#endif

local void hwi_at24_write_bits(ADAPTER * adapter, BYTE bValue)
{
   BYTE bTemp;

   /*
    * Restore the 5 bits we were not interested in from the previous read
    */

   bTemp |= bEepromByteStore;

   sys_outsb(
       adapter->adapter_handle, 
       (WORD) (adapter->io_location + PCI2_SEEPROM_CONTROL), 
       bValue);
}


/************************************************************************
 * 
 * Write to the three EEPROM bits.
 *
 * We have to store the DATA bit as we cannot definately read it back.
 *
 * Inputs  : Adapter structure.
 *           The data to be written.
 *
 * Outputs : None.
 ***********************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_at24_output)
#endif

local void hwi_at24_output(ADAPTER * adapter, BYTE bValue)
{
   bLastDataBit = bValue & (BYTE)AT24_IO_DATA;

   hwi_at24_write_bits(adapter, bValue);
}


/************************************************************************
 * 
 * Write to the three EEPROM bits.
 *
 * Set the DATA bit to the most recent written bit.
 *
 * Inputs  : Adapter structure.
 *           The data to be written.
 *
 * Outputs : None.
 ***********************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_at24_output_preserve_data)
#endif

local void hwi_at24_output_preserve_data(ADAPTER * adapter, BYTE bValue)
{
   bValue &= ~AT24_IO_DATA;

   bValue |= bLastDataBit;

   hwi_at24_write_bits(adapter, bValue);
}


/************************************************************************
 * Delay to allow for serial device timing issues
 *
 * Inputs  : Adapter structure
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_at24_delay)
#endif

local void hwi_at24_delay(ADAPTER * adapter)
{
    UINT i;

    for (i = 0; i < 100; i++)
    {
        sys_insb(adapter->adapter_handle, adapter->io_location);
    }
}


/************************************************************************
 * Set the serial device clock bit
 *
 * Inputs  : Adapter structure
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_at24_set_clk)
#endif

local void hwi_at24_set_clk(ADAPTER * adapter)
{
   BYTE temp;

   temp  = hwi_at24_input(adapter);
   temp |= AT24_IO_CLOCK;

   hwi_at24_output_preserve_data(adapter, temp);
}


/************************************************************************
 *
 * Clears the serial device clock bit
 *
 * Inputs  : Adapter structure
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_at24_clr_clk)
#endif

local void hwi_at24_clr_clk(ADAPTER * adapter) 
{
   BYTE temp;

   temp  = hwi_at24_input(adapter);
   temp &= ~AT24_IO_CLOCK;

   hwi_at24_output_preserve_data(adapter, temp);

   return;
}

/************************************************************************
*
* hwi_at24_read_data
* Read a data bit from the serial EEPROM. It is assumed that the clock is low
* on entry to this routine. The data bit is forced high to allow access to
* the data from the EEPROM, then the clock is toggled, with a read of the
* data in the middle.
*
* Beware! The latched data bit will be set on exit.
*
************************************************************************/
#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_at24_read_data)
#endif

local BYTE hwi_at24_read_data(ADAPTER * adapter)
{
   BYTE  bData;

   /*
    * Set the latched data bit to disconnect us from the data line.
    */

   bData = AT24_IO_ENABLE | AT24_IO_DATA;

   hwi_at24_output(adapter, bData);

   /*
    * Set the clk bit to enable the data line.
    */

   hwi_at24_set_clk(adapter);

   /*
    * Read the data bit.
    */

   bData = hwi_at24_input(adapter);

   /*
    * Get the Data bit into bit 0.
    */

   bData &= AT24_IO_DATA;
   bData >>= 1;

   /*
    * Clear clock again.
    */

   hwi_at24_clr_clk(adapter);

   return bData;
}

/************************************************************************
*
* hwi_at24_write_data
*
* Write a data bit to the serial EEPROM. No clock toggle is performed.
*
************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_at24_write_data)
#endif

local void hwi_at24_write_data(ADAPTER * adapter, BYTE bData)
{
   BYTE bTemp;

   /*
    *  The bit value is in position 0, get it into position 1 
    */

   bData <<= 1;

   /*
    *  Not strictly neccessary, but I'm paranoid.
    */

   bData &= AT24_IO_DATA;

   bTemp = hwi_at24_input(adapter);
   bTemp &= ~AT24_IO_DATA;

   bTemp |= bData;
   hwi_at24_output(adapter, bTemp);
}


/************************************************************************
 *
 * hwi_at24_enable_eeprom
 *
 * Must be called at the start of eeprom access to ensure we can pull low
 * the data and clock pins on the EEPROM. Forces the clock signal low, as part
 * of the strategy of routines assuming the clock is low on entry to them.
 *
 * Inputs  : Adapter structure
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_at24_enable_eeprom)
#endif

local void hwi_at24_enable_eeprom(ADAPTER * adapter)
{
   BYTE temp;

   temp  = hwi_at24_input(adapter);
   temp |= AT24_IO_ENABLE;                      
   
   hwi_at24_output(adapter, temp);
}


/************************************************************************
 *
 * hwi_at24_start_bit
 *
 * Send a "START bit" to the serial EEPROM. This involves toggling the
 * clock bit low to high, with data set on the rising edge and cleared on the
 * falling edge. Assumes clock is low and EEPROM enabled on entry.
 *
 * Inputs  : Adapter structure
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_at24_start_bit)
#endif

local void hwi_at24_start_bit(ADAPTER * adapter)
{
   BYTE  bData;

   bData = AT24_IO_ENABLE | AT24_IO_DATA;

   /*
    * Set the Data bit
    */

   hwi_at24_output(adapter, bData);
   hwi_at24_set_clk(adapter);

   /*
    * Clear the Data bit.
    */

   bData = AT24_IO_ENABLE | AT24_IO_CLOCK;
   hwi_at24_output(adapter, bData);
   hwi_at24_clr_clk(adapter);
}


/************************************************************************
 *
 * hwi_at24_stop_bit
 *
 * Send a "STOP bit" to the serial EEPROM. This involves toggling the
 * clock bit low to high, with data clear on the rising edge and set on the
 * falling edge. Assumes clock is low and EEPROM enabled on entry.
 * Inputs  : Adapter structure
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_at24_stop_bit)
#endif

local void hwi_at24_stop_bit(ADAPTER * adapter)
{
   BYTE bData;

   bData = AT24_IO_ENABLE;

   /*
    *  Clear the Data Bit
    */

   hwi_at24_output(adapter, bData);
   hwi_at24_set_clk(adapter);

   /* 
    * Set the Data Bit.
    */

   bData |= (AT24_IO_DATA | AT24_IO_CLOCK);
   hwi_at24_output(adapter, bData);
   hwi_at24_clr_clk(adapter);
}


/************************************************************************
 *
 * hwi_at24_wait_ack
 *
 * Wait for an ack from the EEPROM.
 * Outputs : TRUE or FALSE
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_at24_wait_ack)
#endif

local WBOOLEAN hwi_at24_wait_ack(ADAPTER * adapter)
{
   WBOOLEAN Acked = FALSE;
   UINT     i;
   BYTE     bData;

   for (i = 0; i < 10; i++)
   {
      bData  = hwi_at24_read_data(adapter);
      bData &= 1;

      if (!bData)
      {
         Acked = TRUE;
         break;
      }
   }

   return Acked;
}


/************************************************************************
 *
 * hwi_at24_dummy_wait_ack
 *
 * Wait for a negative ack from the EEPROM.
 *
 * Outputs : TRUE or FALSE
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_at24_dummy_wait_ack)
#endif

local WBOOLEAN hwi_at24_dummy_wait_ack(ADAPTER * adapter)
{
   WBOOLEAN Acked = FALSE;
   UINT     i;
   BYTE     bData;

   for (i = 0; i < 10; i++)
   {
      bData = hwi_at24_read_data(adapter);

      if (bData & 1)
      {
         Acked = TRUE;
         break;
      }
   }

   return Acked;
}


/************************************************************************
 *
 * hwi_at24_serial_read_bits
 *
 * Read a Byte from the serial EEPROM.
 *
 * NB This routine gets 8 bits from the EEPROM, but relies upon commands
 * having been sent to the EEPROM 1st. In order to read a byte use
 * hwi_at24_receive_data.
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_at24_serial_read_bits)
#endif

local BYTE hwi_at24_serial_read_bits(ADAPTER * adapter)
{
   BYTE  bData = 0;
   BYTE  bBit;
   UINT  i;

   for (i = 0; i < 8; i++)
   {
      /* 
       * The EEPROM clocks data out MSB first.
       */

      bBit    = hwi_at24_read_data(adapter);
      bData <<= 1;
      bData  |= bBit;
   }

   return bData;
}

/************************************************************************
 *
 * hwi_at24_serial_write_bits
 *
 * Send 8 bits to the serial EEPROM.
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_at24_serial_write_bits)
#endif

local void hwi_at24_serial_write_bits(ADAPTER * adapter, BYTE bData)
{
   BYTE  bBit;
   UINT  i;

   for (i = 0; i < 8; i++)
   {
      bBit  = (BYTE)(bData >> (7-i));
      bBit &= 1;
      hwi_at24_write_data(adapter, bBit);

      /*
       * Toggle the clock line to pass the data to the device.
       */ 

      hwi_at24_set_clk(adapter);
      hwi_at24_clr_clk(adapter);
   }
}


/************************************************************************
 *
 * hwi_at24_serial_send_cmd
 *
 * Send a command to the serial EEPROM.
 *
 * Outputs : TRUE if sent OK
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_at24_serial_send_cmd)
#endif

local WBOOLEAN hwi_at24_serial_send_cmd(ADAPTER * adapter, BYTE bCommand)
{
   UINT     i    = 0;
   WBOOLEAN Sent = FALSE;

   while ((i < 40) && (Sent == FALSE))
   {
      i++;

      /*
       *  Wake the device up.
       */

      hwi_at24_start_bit(adapter);
      hwi_at24_serial_write_bits(adapter, bCommand);

      Sent = hwi_at24_wait_ack(adapter);
   }

   return Sent;
}


/************************************************************************
 *
 * hwi_at24_serial_send_cmd_addr
 *
 * Send a command and address to the serial EEPROM.
 *
 * Outputs : TRUE if sent OK
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_at24_serial_send_cmd_addr)
#endif

local WBOOLEAN 
hwi_at24_serial_send_cmd_addr(ADAPTER * adapter, BYTE bCommand, BYTE bAddr)
{
   WBOOLEAN RetCode;

   RetCode = hwi_at24_serial_send_cmd(adapter, bCommand);

   if (RetCode)
   {
      hwi_at24_serial_write_bits(adapter, bAddr);

      RetCode = hwi_at24_wait_ack(adapter);
   }

   return RetCode;
}


/************************************************************************
 *
 * hwi_at24_serial_receive_data
 *
 * Having set up the address we want to read from, read the data.
 *
 * Outputs : Data read back.
 *
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_at24_serial_receive_data)
#endif

local BYTE hwi_at24_serial_receive_data(ADAPTER * adapter)
{
   BYTE     bData;
   WBOOLEAN Acked;

   bData = hwi_at24_serial_read_bits(adapter);

   Acked = hwi_at24_dummy_wait_ack(adapter);

   if (!Acked)
   {
      bData = 0xFF;
   }
    
   return bData;

}

/************************************************************************
 *
 * hwi_at24_serial_read_byte
 *
 * Read a byte of data from the specified ROM address
 *
 * Outputs : Data read back.
 *
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_at24_serial_read_byte)
#endif

local BYTE hwi_at24_serial_read_byte(ADAPTER * adapter, BYTE bAddr)
{
   BYTE  bData;

   hwi_at24_enable_eeprom(adapter);

   /*
    * Send the serial device a dummy WRITE command 
    * that contains the address of the byte we want to
    * read !
    */

   hwi_at24_serial_send_cmd_addr(adapter, AT24_WRITE_CMD, bAddr);

   /*
    * Send the read command
    */

   hwi_at24_serial_send_cmd(adapter, AT24_READ_CMD);

   /*
    * Read the data
    */

   bData = hwi_at24_serial_receive_data(adapter);

   /*
    * Deselect the EEPROM
    */

   hwi_at24_stop_bit(adapter);

   return bData;

}


/************************************************************************
 *
 * hwi_at24_read_a_word
 *
 * Read a word of data from the specified ROM address
 *
 * Outputs : Data read back.
 *
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_at24_read_a_word)
#endif

local WORD hwi_at24_read_a_word(ADAPTER * adapter, WORD word_address)
{
   WORD wData;
   BYTE bLoByte;
   BYTE bByteAddress = (BYTE)((word_address * 2)&0xFF);

   bLoByte = hwi_at24_serial_read_byte(adapter, bByteAddress);

   wData = (WORD) hwi_at24_serial_read_byte(
                      adapter, 
                      (BYTE)(bByteAddress + 1));

   wData <<= 8;
   wData |= bLoByte;

   return wData;
}

#endif


/******* End of HWI_PCI2.C **************************************************/