OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [devs/] [eth/] [intel/] [i82544/] [v2_0/] [src/] [if_i82544.c] - Blame information for rev 27

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      if_i82544.c
4
//
5
//      Intel 82544 ethernet driver
6
//
7
//==========================================================================
8
//####ECOSGPLCOPYRIGHTBEGIN####
9
// -------------------------------------------
10
// This file is part of eCos, the Embedded Configurable Operating System.
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under
14
// the terms of the GNU General Public License as published by the Free
15
// Software Foundation; either version 2 or (at your option) any later version.
16
//
17
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
19
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20
// for more details.
21
//
22
// You should have received a copy of the GNU General Public License along
23
// with eCos; if not, write to the Free Software Foundation, Inc.,
24
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25
//
26
// As a special exception, if other files instantiate templates or use macros
27
// or inline functions from this file, or you compile this file and link it
28
// with other works to produce a work based on this file, this file does not
29
// by itself cause the resulting work to be covered by the GNU General Public
30
// License. However the source code for this file must still be made available
31
// in accordance with section (3) of the GNU General Public License.
32
//
33
// This exception does not invalidate any other reasons why a work based on
34
// this file might be covered by the GNU General Public License.
35
//
36
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37
// at http://sources.redhat.com/ecos/ecos-license/
38
// -------------------------------------------
39
//####ECOSGPLCOPYRIGHTEND####
40
//####BSDCOPYRIGHTBEGIN####
41
//
42
// -------------------------------------------
43
//
44
// Portions of this software may have been derived from OpenBSD or
45
// other sources, and are covered by the appropriate copyright
46
// disclaimers included herein.
47
//
48
// -------------------------------------------
49
//
50
//####BSDCOPYRIGHTEND####
51
//==========================================================================
52
//#####DESCRIPTIONBEGIN####
53
//
54
// Author(s):    hmt, gthomas
55
// Contributors: Ron Spence, Pacific Softworks, jskov
56
// Date:         2000-02-01
57
// Purpose:      
58
// Description:  hardware driver for 82544 Intel PRO/100+ ethernet
59
// Notes:        CU commands such as dump and config should, according
60
//               to the docs, set the CU active state while executing.
61
//               That does not seem to be the case though, and the
62
//               driver polls the completion bit in the packet status
63
//               word instead.
64
//
65
//               Platform code may provide this vector:
66
//               CYGNUM_DEVS_ETH_INTEL_I82544_SEPARATE_MUX_INTERRUPT if it
67
//               requires the interrupts to be handled via demuxers
68
//               attached to a distinct interrupt.
69
//
70
//               Platform code may alternatively define:
71
//               CYGHWR_DEVS_ETH_INTEL_I82544_DEMUX_ALL if it is necessary
72
//               to demux all interrupt sources - for example if they are
73
//               wire-or'd together on some hardware but distinct on
74
//               others.  In this circumstance it is permitted for
75
//               cyg_pci_translate_interrupt [HAL_PCI_TRANSLATE_INTERRUPT]
76
//               to return invalid for 2nd and subsequent devices.
77
//
78
//               Platform code can also define these three:
79
//               CYGPRI_DEVS_ETH_INTEL_I82544_MASK_INTERRUPTS(p_i82544,old)
80
//               CYGPRI_DEVS_ETH_INTEL_I82544_UNMASK_INTERRUPTS(p_i82544,old)
81
//               CYGPRI_DEVS_ETH_INTEL_I82544_ACK_INTERRUPTS(p_i82544)
82
//               which are particularly useful when nested interrupt
83
//               management is needed (which is always IMHO).
84
//
85
//               Platform code can define this:
86
//               CYGHWR_DEVS_ETH_INTEL_I82544_MISSED_INTERRUPT(p_i82544)
87
//               to detect a dropped interrupt and loop again or
88
//               direct-call the DSR to reschedule the delivery routine.
89
//               Only a problem on edge-triggered interrupt systems.
90
//
91
//               Platform code can also provide this macro:
92
//               CYGPRI_DEVS_ETH_INTEL_I82544_INTERRUPT_ACK_LOOP(p_i82544)
93
//               to handle delaying for acks to register on the interrupt
94
//               controller as necessary on the EBSA.
95
//
96
//               Platform can define CYGHWR_DEVS_ETH_INTEL_I82544_GET_ESA()
97
//               as an external means to get ESAs, possibly from RedBoot
98
//               configuration info that's stored in flash memory.
99
//
100
//               Platform def CYGHWR_DEVS_ETH_INTEL_I82544_HAS_NO_EEPROM
101
//               removes all code for dealing with the EEPROM for those
102
//               targets where there is none fitted.  Either an external
103
//               means to get ESAs should be used, or we must rely on
104
//               hard-wiring the ESA's into each executable by means of the
105
//               usual CDL configuration.
106
//
107
//               Platform def CYGHWR_DEVS_ETH_INTEL_I82544_HAS_ONE_EEPROM
108
//               is for hardware with multiple devices, but only one with a
109
//               serial EEPROM installed.  The 2nd device would get either
110
//               the same ESA - because they are certain to be on different
111
//               segment and internets - or the same ESA incremented by
112
//               CYGHWR_DEVS_ETH_INTEL_I82544_HAS_ONE_EEPROM_MAC_ADJUST.
113
//               CYGHWR_DEVS_ETH_INTEL_I82544_HAS_ONE_EEPROM should be the
114
//               number (0 or 1) of the device that does have the EEPROM.
115
//
116
//               CYGHWR_DEVS_ETH_INTEL_I82544_PCIMEM_DISCONTIGUOUS enables
117
//               checking code for breaks in the physical address of PCI
118
//               window memory.  This can happen on some boards where a
119
//               smaller SDRAM is fitted than the hardware allows, so some
120
//               higher-order address bits are ignored.  We make SDRAM
121
//               contiguous in mapped memory, but what the i82544 sees
122
//               might be discontiguous.  The checking code skips any
123
//               allocated chunk who appears to contain such a break, and
124
//               tries again.
125
//
126
//               CYGHWR_DEVS_ETH_INTEL_I82544_RESET_TIMEOUT( int32 )
127
//               CYGHWR_DEVS_ETH_INTEL_I82544_TIMEOUT_FIRED( int32 ) if
128
//               both defined give the driver a means to detect that we
129
//               have been fixated on the same transmit operation for too
130
//               long - we missed an interrupt or the device crashed.  The
131
//               int32 argument is used to hold a eg. the value of a
132
//               fast-running hardware timer.
133
//
134
//               Platform def CYGHWR_DEVS_ETH_INTEL_I82544_USE_ASD is
135
//               used to select Auto Speed Detection for setting up the
136
//               link parameters.
137
//
138
//        FIXME: replace -1/-2 return values with proper E-defines
139
//        FIXME: For 82557/8 compatibility i82544_configure() function
140
//               probably needs some tweaking - config bits differ
141
//               slightly but crucially.
142
//        FIXME: EEPROM code not tested on a BE system.
143
//
144
//####DESCRIPTIONEND####
145
//
146
//==========================================================================
147
 
148
#include <pkgconf/system.h>
149
#ifdef CYGPKG_IO_ETH_DRIVERS
150
#include <pkgconf/io_eth_drivers.h>
151
#endif
152
#include <pkgconf/devs_eth_intel_i82544.h>
153
 
154
#include <cyg/infra/cyg_type.h>
155
#include <cyg/infra/cyg_ass.h>
156
#include <cyg/hal/hal_arch.h>
157
#include <cyg/hal/hal_intr.h>
158
#include <cyg/infra/diag.h>
159
#include <cyg/hal/hal_if.h>
160
#include <cyg/hal/drv_api.h>
161
#include <cyg/io/eth/netdev.h>
162
#include <cyg/io/eth/eth_drv.h>
163
 
164
#ifdef CYGPKG_NET
165
#include <pkgconf/net.h>
166
#include <net/if.h>  /* Needed for struct ifnet */
167
#endif
168
 
169
#ifdef CYGPKG_IO_PCI
170
#include <cyg/io/pci.h>
171
// So we can check the validity of the PCI window against the MLTs opinion,
172
// and thereby what the malloc heap consumes willy-nilly:
173
#include CYGHWR_MEMORY_LAYOUT_H
174
#else
175
#error "Need PCI package here"
176
#endif
177
 
178
#include <cyg/devs/eth/i82544_info.h>
179
 
180
#include CYGDAT_DEVS_ETH_INTEL_I82544_INL
181
 
182
// ------------------------------------------------------------------------
183
 
184
#ifdef CYGDBG_DEVS_ETH_INTEL_I82544_CHATTER
185
#define DEBUG_82544 // This one prints stuff as packets come and go
186
#define DEBUG          // Startup printing mainly
187
#define noDEBUG_EE       // Some EEPROM specific retries &c
188
#endif
189
 
190
#ifdef CYGDBG_USE_ASSERTS
191
static struct {
192
    int can_send;
193
    int deliver;
194
    int stats;
195
    int waitcmd_timeouts;
196
    int waitcmd_timeouts_cu;
197
    int lockup_timeouts;
198
    int bad_cu_idles;
199
} missed_interrupt = { 0,0,0, 0,0, 0, 0 };
200
#endif
201
 
202
#ifndef CYGPKG_REDBOOT
203
 
204
#define os_printf diag_printf
205
#define db_printf diag_printf
206
 
207
#else
208
 
209
static void os_printf( char *fmt, ... )
210
{
211
    extern int start_console(void);
212
    extern void end_console(int);
213
    va_list a;
214
    int old_console;
215
    va_start( a, fmt );
216
    old_console = start_console();
217
    diag_vprintf( fmt, a );
218
    end_console(old_console);
219
    va_end( a );
220
}
221
 
222
#define db_printf os_printf
223
 
224
#endif
225
 
226
// ------------------------------------------------------------------------
227
//
228
//                             MEMORY ADDRESSING
229
// 
230
// ------------------------------------------------------------------------
231
// Macros for writing shared memory structures - no need for byte flipping
232
 
233
#define READMEM8(   _reg_, _val_ ) ((CYG_BYTE)(_val_) = *((volatile CYG_BYTE *)(_reg_)))
234
#define WRITEMEM8(  _reg_, _val_ ) (*((volatile CYG_BYTE *)(_reg_)) = (CYG_BYTE)(_val_))
235
#define READMEM16(  _reg_, _val_ ) ((CYG_WORD16)(_val_) = *((volatile CYG_WORD16 *)(_reg_)))
236
#define WRITEMEM16( _reg_, _val_ ) (*((volatile CYG_WORD16 *)(_reg_)) = (CYG_WORD16)(_val_))
237
#define READMEM32(  _reg_, _val_ ) ((CYG_WORD32)(_val_) = *((volatile CYG_WORD32 *)(_reg_)))
238
#define WRITEMEM32( _reg_, _val_ ) (*((volatile CYG_WORD32 *)(_reg_)) = (CYG_WORD32)(_val_))
239
#define READMEM64(  _reg_, _val_ ) ((CYG_WORD64)(_val_) = *((volatile CYG_WORD64 *)(_reg_)))
240
#define WRITEMEM64( _reg_, _val_ ) (*((volatile CYG_WORD64 *)(_reg_)) = (CYG_WORD64)(_val_))
241
 
242
#define OUTL( _v_, _a_ ) WRITEMEM32( _a_, _v_ )
243
 
244
static inline cyg_uint32 INL(cyg_uint32 io_address)
245
 {   cyg_uint32 _t_; READMEM32( io_address, _t_ ); return _t_;   }
246
 
247
 
248
// ------------------------------------------------------------------------
249
// Map from CPU-view addresses to PCI-bus master's view - however that is:
250
 
251
#ifdef CYGHWR_INTEL_I82544_PCI_VIRT_TO_BUS
252
 
253
#define VIRT_TO_BUS( _x_ ) CYGHWR_INTEL_I82544_PCI_VIRT_TO_BUS( _x_ )
254
#define BUS_TO_VIRT( _x_ ) CYGHWR_INTEL_I82544_PCI_BUS_TO_VIRT( _x_ )
255
 
256
#else // use default mappings: get a physical address to give to the device
257
 
258
#define VIRT_TO_BUS( _x_ ) virt_to_bus((cyg_uint32)(_x_))
259
static inline cyg_uint32 virt_to_bus(cyg_uint32 p_memory)
260
{ return CYGARC_PHYSICAL_ADDRESS(p_memory);    }
261
 
262
#define BUS_TO_VIRT( _x_ ) bus_to_virt((cyg_uint32)(_x_))
263
static inline cyg_uint32 bus_to_virt(cyg_uint32 p_bus)
264
{ return CYGARC_UNCACHED_ADDRESS(p_bus);    }
265
 
266
#endif // not defined CYGHWR_INTEL_I82544_PCI_VIRT_TO_BUS
267
 
268
// ------------------------------------------------------------------------
269
//                                                                      
270
//                   82544 REGISTER OFFSETS
271
//                                                                      
272
// ------------------------------------------------------------------------
273
 
274
// General registers
275
#define I82544_CTRL     0x00000
276
#define I82544_STATUS   0x00008
277
#define I82544_EECD     0x00010
278
#define I82544_CTRL_EXT 0x00018
279
#define I82544_MDIC     0x00020
280
#define I82544_FCAL     0x00028
281
#define I82544_FCAH     0x0002c
282
#define I82544_FCT      0x00030
283
#define I82544_VET      0x00038
284
#define I82544_FCTTV    0x00170
285
#define I82544_TXCW     0x00178
286
#define I82544_RXCW     0x00180
287
#define I82544_PBA      0x01000
288
 
289
// Interrupt control registers
290
#define I82544_ICR      0x000c0
291
#define I82544_ICS      0x000c8
292
#define I82544_IMS      0x000d0
293
#define I82544_IMC      0x000d8
294
 
295
// Receive registers
296
#define I82544_RCTL     0x00100
297
#define I82544_FCRTL    0x02160
298
#define I82544_FCRTH    0x02168
299
#define I82544_RDBAL    0x02800
300
#define I82544_RDBAH    0x02804
301
#define I82544_RDLEN    0x02808
302
#define I82544_RDH      0x02810
303
#define I82544_RDT      0x02818
304
#define I82544_RDTR     0x02820
305
#define I82544_RXDCTL   0x02828
306
#define I82544_RXCSUM   0x05000
307
#define I82544_MTA      0x05200
308
#define I82544_RAT      0x05400
309
#define I82544_VFTA     0x05600
310
 
311
#define I82544_RCTL_EN  (1<<1)
312
#define I82544_RCTL_BAM (1<<15)
313
 
314
// Transmit registers
315
#define I82544_TCTL     0x00400
316
#define I82544_TIPG     0x00410
317
#define I82544_TBT      0x00448
318
#define I82544_AIT      0x00458
319
#define I82544_TXDMAC   0x03000
320
#define I82544_TDBAL    0x03800
321
#define I82544_TDBAH    0x03804
322
#define I82544_TDLEN    0x03808
323
#define I82544_TDH      0x03810
324
#define I82544_TDT      0x03818
325
#define I82544_TIDV     0x03820
326
#define I82544_TXDCTL   0x03828
327
#define I82544_TSPMT    0x03830
328
 
329
 
330
#define I82544_TCTL_EN  (1<<1)
331
#define I82544_TCTL_PSP (1<<3)
332
 
333
 
334
// ------------------------------------------------------------------------
335
//
336
//               82544 DEVICE CONTROL WORD DEFNITIONS
337
//
338
// ------------------------------------------------------------------------
339
 
340
#define I82544_CTRL_FD          (1<<0)
341
#define I82544_CTRL_BEM         (1<<1)
342
#define I82544_CTRL_LRST        (1<<3)
343
#define I82544_CTRL_ASDE        (1<<5)
344
#define I82544_CTRL_SLU         (1<<6)
345
#define I82544_CTRL_ILOS        (1<<7)
346
#define I82544_CTRL_SPEED       (3<<8)
347
#define I82544_CTRL_FRCSPD      (1<<11)
348
#define I82544_CTRL_FRCDPLX     (1<<12)
349
#define I82544_CTRL_SWDPINSLO   (15<<18)
350
#define I82544_CTRL_SWDPINSIO   (15<<22)
351
#define I82544_CTRL_RST         (1<<26)
352
#define I82544_CTRL_RFCE        (1<<27)
353
#define I82544_CTRL_TFCE        (1<<28)
354
#define I82544_CTRL_VME         (1<<30)
355
#define I82544_CTRL_PHY_RST     (1<<31)
356
 
357
#define I82544_CTRL_PHY_RESET           (1<<18)
358
#define I82544_CTRL_PHY_RESET_DIR       (1<<22)
359
#define I82544_CTRL_MDIO                (1<<20)
360
#define I82544_CTRL_MDIO_DIR            (1<<24)
361
#define I82544_CTRL_MDC                 (1<<21)
362
#define I82544_CTRL_MDC_DIR             (1<<25)
363
 
364
#define I82544_CTRL_EXT_PHY_RESET4      (1<<4)
365
#define I82544_CTRL_EXT_PHY_RESET_DIR4  (1<<8)
366
 
367
#define PHY_ADDRESS 1
368
 
369
// ------------------------------------------------------------------------
370
//
371
//               82544 DEVICE STATUS WORD DEFNITIONS
372
//
373
// ------------------------------------------------------------------------
374
 
375
#define I82544_STATUS_FD        0x0001
376
#define I82544_STATUS_LU        0x0002
377
#define I82544_STATUS_TXOFF     0x0010
378
#define I82544_STATUS_TBIMODE   0x0020
379
#define I82544_STATUS_SPEED     0x00C0
380
#define I82544_STATUS_ASDV      0x0300
381
#define I82544_STATUS_PCI_SPD   0x0800
382
#define I82544_STATUS_BUS64     0x1000
383
#define I82544_STATUS_PCIX_MODE 0x2000
384
#define I82544_STATUS_PCIXSPD   0xC000
385
 
386
// ------------------------------------------------------------------------
387
//
388
//                   82544 EEPROM INTERFACE
389
//
390
// ------------------------------------------------------------------------
391
 
392
//  EEPROM_Ctrl bits.
393
#define EE_SHIFT_CLK    0x01            // EEPROM shift clock.
394
#define EE_CS           0x02            // EEPROM chip select.
395
#define EE_DATA_WRITE   0x04            // EEPROM chip data in.
396
#define EE_DATA_READ    0x08            // EEPROM chip data out.
397
#define EE_ENB          (0x10|EE_CS)
398
 
399
 
400
// ------------------------------------------------------------------------
401
//
402
//               RECEIVE DESCRIPTORS
403
//
404
// ------------------------------------------------------------------------
405
 
406
#define I82544_RD_BUFFER        0
407
#define I82544_RD_LENGTH        8
408
#define I82544_RD_CSUM          10
409
#define I82544_RD_STATUS        12
410
#define I82544_RD_ERRORS        13
411
#define I82544_RD_SPECIAL       14
412
#define I82544_RD_SIZE          16
413
 
414
#define I82544_RD_STATUS_DD     (1<<0)
415
#define I82544_RD_STATUS_EOP    (1<<1)
416
#define I82544_RD_STATUS_IXSM   (1<<2)
417
#define I82544_RD_STATUS_VP     (1<<3)
418
#define I82544_RD_STATUS_TCPCS  (1<<5)
419
#define I82544_RD_STATUS_IPCS   (1<<6)
420
#define I82544_RD_STATUS_PIF    (1<<7)
421
 
422
// ------------------------------------------------------------------------
423
//
424
//               TRANSMIT DESCRIPTORS
425
//
426
// ------------------------------------------------------------------------
427
 
428
// Currently we only use the legacy Tx descriptor
429
 
430
#define I82544_TD_BUFFER        0
431
#define I82544_TD_LENGTH        8
432
#define I82544_TD_CSO           10
433
#define I82544_TD_CMD           11
434
#define I82544_TD_STATUS        12
435
#define I82544_TD_CSS           13
436
#define I82544_TD_SPECIAL       14
437
#define I82544_TD_SIZE          16
438
 
439
#define I82544_TD_CMD_EOP       (1<<0)
440
#define I82544_TD_CMD_IFCS      (1<<1)
441
#define I82544_TD_CMD_IC        (1<<2)
442
#define I82544_TD_CMD_RS        (1<<3)
443
#define I82544_TD_CMD_RPS       (1<<4)
444
#define I82544_TD_CMD_DEXT      (1<<5)
445
#define I82544_TD_CMD_VLE       (1<<6)
446
#define I82544_TD_CMD_IDE       (1<<7)
447
 
448
#define I82544_TD_STATUS_DD     (1<<0)
449
#define I82544_TD_STATUS_EC     (1<<1)
450
#define I82544_TD_STATUS_LC     (1<<2)
451
#define I82544_TD_STATUS_TU     (1<<3)
452
 
453
// ------------------------------------------------------------------------
454
//
455
//                      DEVICES AND PACKET QUEUES
456
//
457
// ------------------------------------------------------------------------
458
 
459
#define MAX_RX_PACKET_SIZE  1536        // maximum Rx packet size
460
#define MAX_TX_PACKET_SIZE  1536        // maximum Tx packet size
461
 
462
 
463
// ------------------------------------------------------------------------
464
// Use arrays provided by platform header to verify pointers.
465
 
466
#ifdef CYGDBG_USE_ASSERTS
467
#define CHECK_NDP_SC_LINK()                                             \
468
    CYG_MACRO_START                                                     \
469
    int i, valid_netdev = 0, valid_sc = 0;                              \
470
    for(i = 0; i < CYGNUM_DEVS_ETH_INTEL_I82544_DEV_COUNT; i++) {       \
471
        if (i82544_netdev_array[i] == ndp) valid_netdev = 1;            \
472
        if (i82544_sc_array[i] == sc) valid_sc = 1;                     \
473
        if (valid_sc || valid_netdev) break;                            \
474
    }                                                                   \
475
    CYG_ASSERT( valid_netdev, "Bad ndp" );                              \
476
    CYG_ASSERT( valid_sc, "Bad sc" );                                   \
477
    CYG_ASSERT( (void *)p_i82544 == i82544_sc_array[i]->driver_private, \
478
                "sc pointer bad" );                                     \
479
    CYG_MACRO_END
480
#else
481
#define CHECK_NDP_SC_LINK()
482
#endif
483
 
484
#define IF_BAD_82544( _p_ )                                             \
485
if (({                                                                  \
486
    int i, valid_p = 0;                                                 \
487
    for(i = 0; i < CYGNUM_DEVS_ETH_INTEL_I82544_DEV_COUNT; i++) {       \
488
        if (i82544_priv_array[i] == (_p_)) {                            \
489
            valid_p = 1;                                                \
490
            break;                                                      \
491
        }                                                               \
492
    }                                                                   \
493
    CYG_ASSERT(valid_p, "Bad pointer-to-i82544");                       \
494
    (!valid_p);                                                         \
495
}))
496
 
497
// ------------------------------------------------------------------------
498
//
499
// Managing the memory that is windowed onto the PCI bus
500
//
501
// ------------------------------------------------------------------------
502
 
503
static cyg_uint32 i82544_heap_size;
504
static cyg_uint8 *i82544_heap_base;
505
static cyg_uint8 *i82544_heap_free;
506
 
507
static void *mem_reserved_ioctl = (void*)0;
508
// uncacheable memory reserved for ioctl calls
509
 
510
// ------------------------------------------------------------------------
511
//
512
//                       FUNCTION PROTOTYPES
513
//
514
// ------------------------------------------------------------------------
515
 
516
static int pci_init_find_82544s(void);
517
 
518
static void i82544_reset(struct i82544* p_i82544);
519
static void i82544_setup(struct i82544* p_i82544);
520
static int eth_set_mac_address(struct i82544* p_i82544, cyg_uint8 *addr, int eeprom );
521
 
522
static void InitRxRing(struct i82544* p_i82544);
523
static void InitTxRing(struct i82544* p_i82544);
524
 
525
static cyg_uint32
526
eth_isr(cyg_vector_t vector, cyg_addrword_t data);
527
 
528
static int i82544_configure(struct i82544* p_i82544, int promisc, int oversized);
529
 
530
// debugging/logging only:
531
//void dump_txcb(TxCB* p_txcb);
532
void DisplayStatistics(void);
533
void update_statistics(struct i82544* p_i82544);
534
//void dump_rfd(RFD* p_rfd, int anyway );
535
void dump_all_rfds( int intf );
536
void dump_packet(cyg_uint8 *p_buffer, int length);
537
 
538
static void i82544_stop( struct eth_drv_sc *sc );
539
 
540
// ------------------------------------------------------------------------
541
 
542
static void
543
udelay(int delay)
544
{
545
    CYGACC_CALL_IF_DELAY_US(delay);
546
}
547
 
548
// ------------------------------------------------------------------------
549
// If we are demuxing for all interrupt sources, we must mask and unmask
550
// *all* interrupt sources together.
551
 
552
static inline int
553
Mask82544Interrupt(struct i82544* p_i82544)
554
{
555
    cyg_drv_interrupt_mask(p_i82544->vector);
556
 
557
    return 1;
558
}
559
 
560
static inline void
561
UnMask82544Interrupt(struct i82544* p_i82544, int old)
562
{
563
    if (old & 1)
564
        cyg_drv_interrupt_unmask(p_i82544->vector);
565
}
566
 
567
 
568
static inline void
569
Acknowledge82544Interrupt(struct i82544* p_i82544)
570
{
571
    cyg_drv_interrupt_acknowledge(p_i82544->vector);
572
}
573
 
574
// ------------------------------------------------------------------------
575
// Memory management
576
//
577
// Simply carve off from the front of the PCI mapped window into real memory
578
 
579
static CYG_ADDRESS
580
pciwindow_mem_alloc(int size)
581
{
582
    CYG_ADDRESS p_memory;
583
    int _size = size;
584
 
585
#ifdef DEBUG
586
//    db_printf("pciwindow_mem_alloc %d\n",size);
587
#endif
588
 
589
    CYG_ASSERT(
590
        (CYGHWR_INTEL_I82544_PCI_MEM_MAP_BASE <= (int)i82544_heap_free)
591
        &&
592
        ((CYGHWR_INTEL_I82544_PCI_MEM_MAP_BASE +
593
          CYGHWR_INTEL_I82544_PCI_MEM_MAP_SIZE) > (int)i82544_heap_free)
594
        &&
595
        (0 < i82544_heap_size)
596
        &&
597
        (CYGHWR_INTEL_I82544_PCI_MEM_MAP_SIZE >= i82544_heap_size)
598
        &&
599
        (CYGHWR_INTEL_I82544_PCI_MEM_MAP_BASE == (int)i82544_heap_base),
600
        "Heap variables corrupted" );
601
 
602
    p_memory = 0;
603
    size = (size + 3) & ~3;
604
    if ( (i82544_heap_free+size) < (i82544_heap_base+i82544_heap_size) ) {
605
        cyg_uint32 *p;
606
        p_memory = (CYG_ADDRESS)i82544_heap_free;
607
        i82544_heap_free += size;
608
        for ( p = (cyg_uint32 *)p_memory; _size > 0; _size -= 4 )
609
            *p++ = 0;
610
    }
611
 
612
    CYG_ASSERT(
613
        NULL == p_memory ||
614
        VIRT_TO_BUS( p_memory ) + size == VIRT_TO_BUS( i82544_heap_free ),
615
        "Discontiguous PCI memory in real addresses" );
616
 
617
    return p_memory;
618
}
619
 
620
 
621
// ------------------------------------------------------------------------
622
//
623
//                     MDIO
624
//
625
// Device-specific bit-twiddling and line driving side-effects
626
 
627
// CYGACC_CALL_IF_DELAY_US() drags in huge amounts of scheduler locking and
628
// the like 'cos it's a VV call!  We only want a delay of 1uS tops, so:
629
 
630
#define MII_DELAY() do { int z; for ( z = 0; z < 100; z++ ) ; } while (0)
631
 
632
#if 0
633
# define MII_PRINTF diag_printf
634
# define MII_STUFF "%4s | %4s | %4s | %4s [%08x]\n",    \
635
    (*_ctrl & (1<<20)) ? "MDIO" : "---",                     \
636
    (*_ctrl & (1<<24)) ? "Wr" : "Rd",                      \
637
    (*_ctrl & (1<<21)) ? "CLK" : "clk",                      \
638
    *_ctrl
639
#else
640
# define MII_PRINTF( foo )
641
# define MII_STUFF
642
#endif
643
 
644
static inline cyg_uint32 mii_init( int ioaddr )
645
{
646
    cyg_uint32 ctrl;
647
    cyg_uint32 *_ctrl = &ctrl;
648
    *_ctrl = INL( ioaddr + I82544_CTRL );
649
    *_ctrl &=~ I82544_CTRL_MDC;
650
    *_ctrl |= I82544_CTRL_MDC_DIR;
651
    *_ctrl &= ~I82544_CTRL_MDIO_DIR;
652
    *_ctrl &= ~I82544_CTRL_MDIO;
653
    OUTL( *_ctrl, ioaddr + I82544_CTRL );
654
    MII_PRINTF( "mii_init  : " MII_STUFF  );
655
    MII_DELAY();
656
    return *_ctrl;
657
}
658
 
659
static inline void mii_clock_up( int ioaddr, cyg_uint32 *_ctrl )
660
{
661
    *_ctrl |= I82544_CTRL_MDC;
662
    OUTL( *_ctrl, ioaddr + I82544_CTRL );
663
    MII_PRINTF( "mii_clock_up  : " MII_STUFF  );
664
    MII_DELAY();
665
}
666
 
667
static inline void mii_clock_down( int ioaddr, cyg_uint32 *_ctrl )
668
{
669
    *_ctrl &=~ I82544_CTRL_MDC;
670
    OUTL( *_ctrl, ioaddr + I82544_CTRL );
671
    MII_PRINTF( "mii_clock_down: " MII_STUFF  );
672
    MII_DELAY();
673
}
674
 
675
static inline void mii_read_mode( int ioaddr, cyg_uint32 *_ctrl )
676
{
677
    *_ctrl &= ~I82544_CTRL_MDIO_DIR;
678
    *_ctrl &= ~I82544_CTRL_MDIO;
679
    OUTL( *_ctrl, ioaddr + I82544_CTRL );
680
    MII_PRINTF( "mii_read_mode : " MII_STUFF  );
681
    MII_DELAY();
682
}
683
 
684
static inline int mii_read_data_bit( int ioaddr, cyg_uint32 *_ctrl )
685
{
686
    *_ctrl = INL( ioaddr + I82544_CTRL );
687
    MII_PRINTF( "mii_read_data : " MII_STUFF  );
688
    return I82544_CTRL_MDIO == (I82544_CTRL_MDIO & *_ctrl);
689
}
690
 
691
static inline void mii_write_data_bit( int ioaddr, int databit, cyg_uint32 *_ctrl )
692
{
693
    if ( databit )
694
        *_ctrl |= I82544_CTRL_MDIO;
695
    else
696
        *_ctrl &= ~I82544_CTRL_MDIO;
697
    *_ctrl |= I82544_CTRL_MDIO_DIR; // drive the mdio line
698
    OUTL( *_ctrl, ioaddr + I82544_CTRL );
699
    MII_PRINTF( "mii_write_data: " MII_STUFF  );
700
    MII_DELAY();
701
}
702
 
703
// Pass ioaddr around "invisibly"
704
#define MII_INIT()                cyg_uint32 _ctrl_val = mii_init(ioaddr); \
705
                                  cyg_uint32 *_ctrl = &_ctrl_val;
706
 
707
#define MII_CLOCK_UP()            mii_clock_up(ioaddr, _ctrl)
708
#define MII_CLOCK_DOWN()          mii_clock_down(ioaddr, _ctrl)
709
#define MII_READ_MODE()           mii_read_mode(ioaddr, _ctrl)
710
#define MII_READ_DATA_BIT()       mii_read_data_bit(ioaddr, _ctrl)
711
#define MII_WRITE_DATA_BIT( _d_ ) mii_write_data_bit(ioaddr,(_d_),_ctrl)
712
 
713
// ------------------------------------------------------------------------
714
//
715
//                     MDIO
716
//
717
// Management data over the MII interface - nasty hand driven serial stuff
718
//
719
 
720
static void mii_write_bits( int ioaddr, int val, int bitcount, cyg_uint32 *_ctrl )
721
{
722
    int i;
723
    // These are deliberately signed ints so that we can send an overlong
724
    // preamble if we want by saying "send -1 of width 40 bits" and relying
725
    // on sign extension.
726
    for ( i = bitcount - 1; i >= 0; i-- ) {
727
        MII_WRITE_DATA_BIT( (val >> i) & 1 );
728
        MII_DELAY();
729
        MII_CLOCK_UP();
730
        MII_CLOCK_DOWN();
731
    }
732
}
733
 
734
static int mii_read_bits( int ioaddr, int bitcount, cyg_uint32 *_ctrl )
735
{
736
    int i;
737
    int val = 0;
738
    for ( i = bitcount - 1; i >= 0; i-- ) {
739
        MII_CLOCK_DOWN();
740
        val <<= 1;
741
        val |= MII_READ_DATA_BIT();
742
        MII_CLOCK_UP();
743
    }
744
    return val;
745
}
746
 
747
#define MII_WRITE_BITS( val, bitcount ) mii_write_bits( ioaddr, val, bitcount, _ctrl )
748
#define MII_READ_BITS( bitcount )       mii_read_bits( ioaddr, bitcount, _ctrl )
749
 
750
// Now define subsections of the protocol in terms of the above
751
 
752
#define MII_WRITE_PREAMBLE()    MII_WRITE_BITS( -1, 32 )  // >32 x 1s
753
#define MII_WRITE_START()       MII_WRITE_BITS( 1, 2 )    // 01
754
#define MII_WRITE_WRITE_CMD()   MII_WRITE_BITS( 1, 2 )    // 01
755
#define MII_WRITE_READ_CMD()    MII_WRITE_BITS( 2, 2 )    // 10
756
 
757
#define MII_WRITE_PHY_ADDR(_p_) MII_WRITE_BITS( _p_, 5 )
758
#define MII_WRITE_REGNUM( _r_ ) MII_WRITE_BITS( (_r_), 5 )
759
 
760
#define MII_WRITE_TURNAROUND()  MII_WRITE_BITS( 2, 2 )
761
 
762
#define MII_READ_TURNAROUND()   CYG_MACRO_START         \
763
  MII_READ_MODE(); /* to turn off driving the line */   \
764
  (void)(MII_READ_BITS( 2 )); /* discard TA "bits" */   \
765
CYG_MACRO_END
766
 
767
#define MII_IDLE() CYG_MACRO_START                              \
768
  MII_READ_MODE(); /* to turn off driving the line */           \
769
  ((void)MII_READ_BITS( 5 )); /* extra clocks in Hi-Z mode */   \
770
  MII_CLOCK_DOWN();                                             \
771
CYG_MACRO_END
772
 
773
#define MII_READ_REGVAL()       MII_READ_BITS( 16 )
774
#define MII_WRITE_REGVAL( _v_ ) MII_WRITE_BITS( (_v_), 16 )
775
 
776
static int mii_read_register( struct i82544 *p_i82544, int phy, int regnum )
777
{
778
    int value = 0;
779
    cyg_uint32 ioaddr = p_i82544->io_address;
780
 
781
    if( p_i82544->device == 0x1004 )
782
    {
783
        // An 82543, read MII register via software defined pins in
784
        // CTRL register.
785
 
786
        MII_INIT();
787
        MII_WRITE_PREAMBLE();
788
        MII_WRITE_START();
789
        MII_WRITE_READ_CMD();
790
        MII_WRITE_PHY_ADDR(phy);
791
        MII_WRITE_REGNUM( regnum );
792
        MII_READ_TURNAROUND();
793
        value = MII_READ_REGVAL();
794
        MII_IDLE();
795
    }
796
    else if( p_i82544->device == 0x1008 )
797
    {
798
        // An 82544, read MII register via MDIC register.
799
        // UNTESTED
800
 
801
        cyg_uint32 mdic = (2<<26) | (phy<<21) | (regnum<<16);
802
 
803
        OUTL( mdic, ioaddr + I82544_MDIC );
804
 
805
        // Wait for ready
806
        do
807
        {
808
            mdic = INL( ioaddr + I82544_MDIC );
809
        } while( (mdic & (1<<28)) == 0 );
810
 
811
        value = mdic & 0xFFFF;
812
    }
813
    return value;
814
}
815
 
816
static void mii_write_register( struct i82544 *p_i82544, int phy, int regnum, int value )
817
{
818
    cyg_uint32 ioaddr = p_i82544->io_address;
819
 
820
    if( p_i82544->device == 0x1004 )
821
    {
822
        // An 82543, write MII register via software defined pins in
823
        // CTRL register.
824
 
825
        MII_INIT();
826
        MII_WRITE_PREAMBLE();
827
        MII_WRITE_START();
828
        MII_WRITE_WRITE_CMD();
829
        MII_WRITE_PHY_ADDR(phy);
830
        MII_WRITE_REGNUM( regnum );
831
        MII_WRITE_TURNAROUND();
832
        MII_WRITE_REGVAL( value );
833
        MII_IDLE();
834
    }
835
    else if( p_i82544->device == 0x1008 )
836
    {
837
        // An 82544, write MII register via MDIC register.
838
        // UNTESTED
839
 
840
        cyg_uint32 mdic = (1<<26) | (phy<<21) | (regnum<<16) | (value&0xFFFF);
841
 
842
        OUTL( mdic, ioaddr + I82544_MDIC );
843
 
844
        // Wait for ready
845
        do
846
        {
847
            mdic = INL( ioaddr + I82544_MDIC );
848
        } while( (mdic & (1<<28)) == 0 );
849
    }
850
}
851
 
852
// dump out the PHY registers
853
static void show_phy( struct i82544 *p_i82544, int phy )
854
{
855
    int i;
856
 
857
    os_printf("PHY %d regs:",phy);
858
    for( i = 0; i < 32; i++ )
859
    {
860
        cyg_uint32 mdic;
861
 
862
        mdic = mii_read_register( p_i82544, phy, i );
863
 
864
        if( (i%8)==0 ) os_printf("\n");
865
 
866
        os_printf("%04x ",mdic);
867
    }
868
    os_printf("\n");
869
}
870
 
871
// ------------------------------------------------------------------------
872
//
873
//                       GET EEPROM SIZE
874
//
875
// ------------------------------------------------------------------------
876
 
877
// ------------------------------------------------------------------------
878
//
879
// Serial EEPROM access  - much like the other Intel ethernet
880
//
881
 
882
// CYGACC_CALL_IF_DELAY_US() drags in huge amounts of scheduler locking and
883
// the like 'cos it's a VV call!  Waste of time, mostly.
884
 
885
#define EE_DELAY() do { int z; for ( z = 0; z < 10000; z++ ) ; } while (0)
886
 
887
#if 0
888
# define EE_PRINTF diag_printf
889
# define EE_STUFF "%4s | %4s | %4s | %4s [%08x]\n",     \
890
    (l & EE_SHIFT_CLK) ? "CLK"  : "clk",                      \
891
    (l & EE_CS) ? "eeCS" : "--",                       \
892
    (l & EE_DATA_WRITE) ? "eeDW" : "---",                      \
893
    (l & EE_DATA_READ) ? "eeDR" : "---",                      \
894
    l & 0xfffff
895
#else
896
# define EE_PRINTF( foo )
897
# define EE_STUFF
898
#endif
899
 
900
 
901
static inline void ee_select( int ioaddr )
902
{
903
    cyg_uint32 l;
904
    l = EE_ENB;
905
    OUTL( l, ioaddr + I82544_EECD );
906
    EE_DELAY();
907
    l |= EE_CS;
908
    OUTL( l, ioaddr + I82544_EECD );
909
    l = INL( ioaddr + I82544_EECD );
910
    EE_PRINTF( "ee_select       : " EE_STUFF  );
911
    EE_DELAY();
912
}
913
 
914
static inline void ee_deselect( int ioaddr )
915
{
916
    cyg_uint32 l;
917
    l = EE_ENB;
918
    OUTL( l, ioaddr + I82544_EECD );
919
    EE_PRINTF( "ee_deselect 1   : " EE_STUFF  );
920
    EE_DELAY();
921
    EE_DELAY();
922
    EE_DELAY();
923
    l = EE_ENB & ~EE_CS;
924
    OUTL( l, ioaddr + I82544_EECD );
925
    l = INL( ioaddr + I82544_EECD );
926
    EE_PRINTF( "ee_deselect 2   : " EE_STUFF  );
927
    EE_DELAY();
928
    EE_DELAY();
929
    EE_DELAY();
930
}
931
 
932
static inline void ee_clock_up( int ioaddr )
933
{
934
    cyg_uint32 l;
935
    l = INL( ioaddr + I82544_EECD );
936
    l |= EE_SHIFT_CLK;
937
    OUTL( l, ioaddr + I82544_EECD );
938
    EE_DELAY();
939
    l = INL( ioaddr + I82544_EECD );
940
    EE_PRINTF( "ee_clock_up     : " EE_STUFF  );
941
    EE_DELAY();
942
}
943
 
944
static inline void ee_clock_down( int ioaddr )
945
{
946
    cyg_uint32 l;
947
    l = INL( ioaddr + I82544_EECD );
948
    l &=~ EE_SHIFT_CLK;
949
    OUTL( l, ioaddr + I82544_EECD );
950
    EE_DELAY();
951
    l = INL( ioaddr + I82544_EECD );
952
    EE_PRINTF( "ee_clock_down   : " EE_STUFF  );
953
    EE_DELAY();
954
}
955
 
956
static inline int ee_read_data_bit( int ioaddr )
957
{
958
    cyg_uint32 l;
959
    l = INL( ioaddr + I82544_EECD );
960
    EE_PRINTF( "ee_read_data    : " EE_STUFF  );
961
    return EE_DATA_READ == (EE_DATA_READ & l);
962
}
963
 
964
static inline void ee_write_data_bit( int ioaddr, int databit )
965
{
966
    cyg_uint32 l;
967
    l = INL( ioaddr + I82544_EECD );
968
    if ( databit )
969
        l |= EE_DATA_WRITE;
970
    else
971
        l &= ~EE_DATA_WRITE;
972
    OUTL( l, ioaddr + I82544_EECD );
973
    l = INL( ioaddr + I82544_EECD );
974
    EE_PRINTF( "ee_write_data   : " EE_STUFF  );
975
    EE_DELAY();
976
}
977
 
978
// Pass ioaddr around "invisibly"
979
#define EE_SELECT()              ee_select(ioaddr)
980
#define EE_DESELECT()            ee_deselect(ioaddr)
981
#define EE_CLOCK_UP()            ee_clock_up(ioaddr)
982
#define EE_CLOCK_DOWN()          ee_clock_down(ioaddr)
983
#define EE_READ_DATA_BIT()       ee_read_data_bit(ioaddr)
984
#define EE_WRITE_DATA_BIT( _d_ ) ee_write_data_bit(ioaddr,(_d_))
985
 
986
// ------------------------------------------------------------------------
987
 
988
static int
989
get_eeprom_size( struct i82544 *p_i82544 )
990
{
991
//    int i, addrbits, tmp;
992
//    cyg_uint32 ioaddr = p_i82544->io_address;
993
 
994
#ifdef DEBUG_EE
995
    diag_printf( "get_eeprom_size\n" );
996
#endif
997
 
998
#if 1
999
    return 6;
1000
#else    
1001
    // Should already be not-selected, but anyway:
1002
    EE_SELECT();
1003
 
1004
#ifdef DEBUG_EE
1005
    diag_printf( "send command\n" );
1006
#endif
1007
 
1008
    // Shift the read command bits out.
1009
    for (i = 3; i >= 0; i--) { // Doc says to shift out a zero then:
1010
        tmp = (6 & (1 << i)) ? 1 : 0; // "6" is the "read" command.
1011
        EE_WRITE_DATA_BIT(tmp);
1012
        EE_CLOCK_UP();
1013
        EE_CLOCK_DOWN();
1014
    }
1015
#ifdef DEBUG_EE
1016
    diag_printf( "send address zero\n" );
1017
#endif
1018
    // Now clock out address zero, looking for the dummy 0 data bit
1019
    for ( i = 1; i <= 12; i++ ) {
1020
        EE_WRITE_DATA_BIT(0);
1021
        EE_CLOCK_UP();
1022
        tmp = EE_READ_DATA_BIT();
1023
        EE_CLOCK_DOWN();
1024
        if ( !tmp )
1025
            break;
1026
    }
1027
 
1028
#ifdef DEBUG_EE
1029
    diag_printf( "eeprom data bits %d\n", i );
1030
#endif
1031
 
1032
    if ( 6 != i && 8 != i && 1 != i) {
1033
#ifdef DEBUG_EE
1034
        diag_printf( "*****EEPROM data bits not 6, 8 or 1*****\n" );
1035
#endif
1036
        addrbits = 1; // Flag no eeprom here.
1037
    }
1038
    else
1039
        addrbits = i;
1040
 
1041
    // read in the data regardless
1042
    tmp = 0;
1043
    for (i = 15; i >= 0; i--) {
1044
        EE_CLOCK_UP();
1045
        if ( EE_READ_DATA_BIT() )
1046
            tmp |= (1<<i);
1047
        EE_CLOCK_DOWN();
1048
    }
1049
 
1050
#ifdef DEBUG_EE
1051
    diag_printf( "eeprom first data word %x\n", tmp );
1052
#endif
1053
 
1054
    // Terminate the EEPROM access.
1055
    EE_DESELECT();
1056
 
1057
    return addrbits;
1058
#endif    
1059
}
1060
 
1061
static int
1062
read_eeprom_word( struct i82544 *p_i82544, int addrbits, int address )
1063
{
1064
    int i, tmp;
1065
    cyg_uint32 ioaddr = p_i82544->io_address;
1066
 
1067
    // Should already be not-selected, but anyway:
1068
    EE_SELECT();
1069
 
1070
    // Shift the read command bits out.
1071
    for (i = 3; i >= 0; i--) { // Doc says to shift out a zero then:
1072
        tmp = (6 & (1 << i)) ? 1 : 0; // "6" is the "read" command.
1073
        EE_WRITE_DATA_BIT(tmp);
1074
        EE_CLOCK_UP();
1075
        EE_CLOCK_DOWN();
1076
    }
1077
 
1078
    // Now clock out address
1079
    for ( i = addrbits - 1; i >= 0 ; i-- ) {
1080
        tmp = (address & (1<<i)) ? 1 : 0;
1081
        EE_WRITE_DATA_BIT(tmp);
1082
        EE_CLOCK_UP();
1083
        tmp = EE_READ_DATA_BIT();
1084
        EE_CLOCK_DOWN();
1085
 
1086
//        CYG_ASSERT( (0 == tmp) == (0 == i), "Looking for zero handshake bit" );
1087
    }
1088
 
1089
    // read in the data
1090
    tmp = 0;
1091
    for (i = 15; i >= 0; i--) {
1092
        EE_CLOCK_UP();
1093
        if ( EE_READ_DATA_BIT() )
1094
            tmp |= (1<<i);
1095
        EE_CLOCK_DOWN();
1096
    }
1097
 
1098
    // Terminate the EEPROM access.
1099
    EE_DESELECT();
1100
 
1101
#ifdef DEBUG_EE
1102
//    diag_printf( "eeprom address %4x: data %4x\n", address, tmp );
1103
#endif
1104
 
1105
    return tmp;
1106
}
1107
 
1108
// ------------------------------------------------------------------------
1109
 
1110
// ------------------------------------------------------------------------
1111
//
1112
//                NETWORK INTERFACE INITIALIZATION
1113
//
1114
//  Function : Init82544
1115
//
1116
//  Description :
1117
//       This routine resets, configures, and initializes the chip.
1118
//       It also clears the ethernet statistics structure, and selects
1119
//       which statistics are supported by this driver.
1120
//
1121
// ------------------------------------------------------------------------
1122
 
1123
static bool
1124
i82544_init(struct cyg_netdevtab_entry * ndp)
1125
{
1126
    static int initialized = 0; // only probe PCI et al *once*
1127
 
1128
    struct eth_drv_sc *sc;
1129
    cyg_uint32 ioaddr;
1130
    int count;
1131
    struct i82544 *p_i82544;
1132
    cyg_uint8 mac_address[ETHER_ADDR_LEN];
1133
 
1134
#ifdef DEBUG
1135
    db_printf("i82544_init\n");
1136
#endif
1137
 
1138
    sc = (struct eth_drv_sc *)(ndp->device_instance);
1139
    p_i82544 = (struct i82544 *)(sc->driver_private);
1140
 
1141
    IF_BAD_82544( p_i82544 ) {
1142
#ifdef DEBUG
1143
        os_printf( "Bad device private pointer %x\n", sc->driver_private );
1144
#endif
1145
        return 0;
1146
    }
1147
 
1148
    CHECK_NDP_SC_LINK();
1149
 
1150
    if ( 0 == initialized++ ) {
1151
        // then this is the first time ever:
1152
        if ( ! pci_init_find_82544s() ) {
1153
#ifdef DEBUG
1154
            os_printf( "pci_init_find_82544s failed\n" );
1155
#endif
1156
            return 0;
1157
        }
1158
    }
1159
 
1160
    // If this device is not present, exit
1161
    if (0 == p_i82544->found)
1162
        return 0;
1163
 
1164
    p_i82544->mac_addr_ok = 0;
1165
 
1166
    ioaddr = p_i82544->io_address; // get I/O address for 82544
1167
 
1168
#ifdef DEBUG
1169
    os_printf("Init82544 %d @ %x\n", p_i82544->index, (int)ndp);
1170
#endif
1171
 
1172
    // Reset device
1173
    i82544_reset(p_i82544);
1174
 
1175
    i82544_setup(p_i82544);
1176
 
1177
    InitRxRing(p_i82544);
1178
    InitTxRing(p_i82544);
1179
 
1180
 
1181
    if (p_i82544->hardwired_esa) {
1182
        // Hardwire the address without consulting the EEPROM.
1183
        // When this flag is set, the p_i82544 will already contain
1184
        // the ESA. Copy it to a mac_address for call to set_mac_addr
1185
        mac_address[0] = p_i82544->mac_address[0];
1186
        mac_address[1] = p_i82544->mac_address[1];
1187
        mac_address[2] = p_i82544->mac_address[2];
1188
        mac_address[3] = p_i82544->mac_address[3];
1189
        mac_address[4] = p_i82544->mac_address[4];
1190
        mac_address[5] = p_i82544->mac_address[5];
1191
 
1192
        eth_set_mac_address(p_i82544, mac_address, 0);
1193
 
1194
    } else {
1195
 
1196
        // Acquire the ESA either from extenal means (probably RedBoot
1197
        // variables) or from the attached EEPROM - if there is one.
1198
 
1199
#ifdef CYGHWR_DEVS_ETH_INTEL_I82544_GET_ESA
1200
        int ok = false;
1201
        CYGHWR_DEVS_ETH_INTEL_I82544_GET_ESA( p_i82544, mac_address, ok );
1202
        if ( ok )
1203
            eth_set_mac_address(p_i82544, mac_address, 0);
1204
 
1205
#else // ! CYGHWR_DEVS_ETH_INTEL_I82544_GET_ESA
1206
 
1207
#ifndef CYGHWR_DEVS_ETH_INTEL_I82544_HAS_NO_EEPROM
1208
        int addr_length, i;
1209
        cyg_uint16 checksum;
1210
 
1211
        // read eeprom and get 82544's mac address
1212
        addr_length = get_eeprom_size(p_i82544);
1213
        // (this is the length of the *EEPROM*s address, not MAC address)
1214
 
1215
        // If length is 1, it _probably_ means there's no EEPROM
1216
        // present.  Couldn't find an explicit mention of this in the
1217
        // docs, but length=1 appears to be the behaviour in that case.
1218
        if (1 == addr_length) {
1219
#ifdef DEBUG_EE
1220
            os_printf("Error: No EEPROM present for device %d\n",
1221
                      p_i82544->index);
1222
#endif
1223
        } else {
1224
            for (checksum = 0, i = 0, count = 0; count < 64; count++) {
1225
                cyg_uint16 value;
1226
                // read word from eeprom
1227
                value = read_eeprom_word(p_i82544, addr_length, count);
1228
#ifdef DEBUG_EE
1229
                os_printf( "%02x: %04x\n", count, value );
1230
#endif
1231
                checksum += value;
1232
                if (count < 3) {
1233
                    mac_address[i++] = value & 0xFF;
1234
                    mac_address[i++] = (value >> 8) & 0xFF;
1235
                }
1236
            }
1237
 
1238
#ifndef CYGHWR_DEVS_ETH_INTEL_I82544_HAS_ONE_EEPROM_WITHOUT_CRC
1239
            // If the EEPROM checksum is wrong, the MAC address read
1240
            // from the EEPROM is probably wrong as well. In that
1241
            // case, we don't set mac_addr_ok, but continue the
1242
            // initialization. If then somebody calls i82544_start
1243
            // without calling eth_set_mac_address() first, we refuse
1244
            // to bring up the interface, because running with an
1245
            // invalid MAC address is not a very brilliant idea.
1246
 
1247
            if ((checksum & 0xFFFF) != 0xBABA)  {
1248
                // selftest verified checksum, verify again
1249
#ifdef DEBUG_EE
1250
                os_printf("Warning: Invalid EEPROM checksum %04X for device %d\n",
1251
                          checksum, p_i82544->index);
1252
#endif
1253
            } else // trailing block
1254
#endif
1255
            {
1256
                p_i82544->mac_addr_ok = 1;
1257
#ifdef DEBUG_EE
1258
                os_printf("Valid EEPROM checksum\n");
1259
#endif
1260
                eth_set_mac_address(p_i82544, mac_address, 0);
1261
            }
1262
        }
1263
 
1264
        // record the MAC address in the device structure
1265
        p_i82544->mac_address[0] = mac_address[0];
1266
        p_i82544->mac_address[1] = mac_address[1];
1267
        p_i82544->mac_address[2] = mac_address[2];
1268
        p_i82544->mac_address[3] = mac_address[3];
1269
        p_i82544->mac_address[4] = mac_address[4];
1270
        p_i82544->mac_address[5] = mac_address[5];
1271
 
1272
#endif // ! CYGHWR_DEVS_ETH_INTEL_I82544_HAS_NO_EEPROM
1273
#endif // ! CYGHWR_DEVS_ETH_INTEL_I82544_GET_ESA
1274
    }
1275
 
1276
#ifdef DEBUG
1277
    os_printf("i82544_init: MAC Address = %02X %02X %02X %02X %02X %02X\n",
1278
              p_i82544->mac_address[0], p_i82544->mac_address[1],
1279
              p_i82544->mac_address[2], p_i82544->mac_address[3],
1280
              p_i82544->mac_address[4], p_i82544->mac_address[5]);
1281
#endif
1282
 
1283
    // and record the net dev pointer
1284
    p_i82544->ndp = (void *)ndp;
1285
 
1286
    p_i82544->within_send = 0; // init recursion level
1287
 
1288
    // Initialize upper level driver
1289
    if ( p_i82544->mac_addr_ok )
1290
        (sc->funs->eth_drv->init)(sc, &(p_i82544->mac_address[0]) );
1291
    else
1292
        (sc->funs->eth_drv->init)(sc, NULL );
1293
 
1294
 
1295
#ifdef DEBUG
1296
 
1297
    os_printf("CTRL %08x\n",INL( ioaddr + I82544_CTRL ));
1298
    os_printf("STATUS %08x\n",INL( ioaddr + I82544_STATUS ));
1299
 
1300
#endif    
1301
 
1302
    return (1);
1303
}
1304
 
1305
// ------------------------------------------------------------------------
1306
//
1307
//  Function : i82544_setup
1308
//
1309
// ------------------------------------------------------------------------
1310
 
1311
static void
1312
i82544_setup( struct i82544 *p_i82544 )
1313
{
1314
    cyg_uint32 ioaddr;
1315
    cyg_uint32 ctrl;
1316
    cyg_uint32 ctrl_ext;
1317
 
1318
    ioaddr = p_i82544->io_address; // get 82544's I/O address
1319
 
1320
 
1321
#ifdef CYGHWR_DEVS_ETH_INTEL_I82544_USE_ASD
1322
    // Use Auto-negotiation
1323
 
1324
    ctrl = INL( ioaddr + I82544_CTRL );
1325
 
1326
    // Set link up bit
1327
    ctrl |= I82544_CTRL_SLU | I82544_CTRL_ASDE;
1328
    ctrl &= ~(I82544_CTRL_ILOS | I82544_CTRL_FRCSPD | I82544_CTRL_FRCDPLX);
1329
    OUTL( ctrl, ioaddr + I82544_CTRL );
1330
    udelay(20);
1331
 
1332
    // wait up to 5 seconds for link to come up
1333
    {
1334
        int delay_cnt = 500;
1335
        while ((mii_read_register( p_i82544, PHY_ADDRESS, 1 ) & 0x4) == 0) {
1336
            udelay(10000);
1337
            if (--delay_cnt <= 0)
1338
                break;
1339
        }
1340
    }
1341
 
1342
#else
1343
    // The following sequence of resets and bit twiddling seem to be
1344
    // necessary to get the 82543 working. Not sure what is necessary
1345
    // for the 82544.
1346
 
1347
    ctrl = INL( ioaddr + I82544_CTRL );
1348
 
1349
    // Set link up bit
1350
    ctrl |= I82544_CTRL_SLU;
1351
    ctrl &= ~I82544_CTRL_ILOS;
1352
    OUTL( ctrl, ioaddr + I82544_CTRL );
1353
    udelay(20);
1354
 
1355
    // Force PHY physical reset
1356
    // We can only access the PHY after we have done this.
1357
 
1358
    ctrl_ext = INL( ioaddr + I82544_CTRL_EXT );
1359
    ctrl_ext |= I82544_CTRL_EXT_PHY_RESET_DIR4;
1360
    OUTL( ctrl_ext, ioaddr + I82544_CTRL_EXT );
1361
    udelay( 20000 );
1362
 
1363
    ctrl_ext = INL( ioaddr + I82544_CTRL_EXT );
1364
    ctrl_ext &= ~I82544_CTRL_EXT_PHY_RESET4;
1365
    OUTL( ctrl_ext, ioaddr + I82544_CTRL_EXT );
1366
    udelay( 20000 );
1367
 
1368
    ctrl_ext = INL( ioaddr + I82544_CTRL_EXT );
1369
    ctrl_ext |= I82544_CTRL_EXT_PHY_RESET4;
1370
    OUTL( ctrl_ext, ioaddr + I82544_CTRL_EXT );
1371
    udelay( 20000 );
1372
 
1373
#ifdef DEBUG    
1374
    show_phy( p_i82544, PHY_ADDRESS );
1375
#endif
1376
 
1377
#if 0
1378
    // Reset PHY
1379
    // Does not appear to be necessary.
1380
    {
1381
        cyg_uint16 phy_ctrl;
1382
        phy_ctrl = mii_read_register( p_i82544, PHY_ADDRESS, 0 );
1383
        phy_ctrl = 0x9000;
1384
//        os_printf("PHY ctrl %04x\n",phy_ctrl);
1385
        mii_write_register( p_i82544, PHY_ADDRESS, 0, phy_ctrl );
1386
        do {
1387
            phy_ctrl = mii_read_register( p_i82544, PHY_ADDRESS, 0 );
1388
//            os_printf("PHY ctrl %04x\n",phy_ctrl);
1389
        } while( phy_ctrl & 0x8000 );
1390
    }
1391
    show_phy( p_i82544, PHY_ADDRESS );
1392
#endif
1393
 
1394
#if 0
1395
    // Tinker with PHY configuration.
1396
    // Only on 82543? May not be necessary at all, since disabling
1397
    // this does not see to make any difference.
1398
    {
1399
        cyg_uint16 data;
1400
 
1401
        // Set CRS on Tx bit in PHY specific CR
1402
        data = mii_read_register( p_i82544, PHY_ADDRESS, 16 );
1403
        os_printf("PSCR %04x\n",data);
1404
        data |= 0x0800;
1405
        mii_write_register( p_i82544, PHY_ADDRESS, 16, data );
1406
 
1407
        // Set TX clock to 25MHz in PHY extended CR
1408
        data = mii_read_register( p_i82544, PHY_ADDRESS, 20 );
1409
        os_printf("PSECR %04x\n",data);
1410
        data |= 0x0070;
1411
        mii_write_register( p_i82544, PHY_ADDRESS, 20, data );
1412
    }
1413
    show_phy( p_i82544, PHY_ADDRESS );
1414
#endif
1415
 
1416
#if 1
1417
    // Force speed renegotiation.
1418
 
1419
    {
1420
        cyg_uint16 phy_ctrl;
1421
        cyg_uint16 phy_stat;
1422
 
1423
        phy_ctrl = mii_read_register( p_i82544, PHY_ADDRESS, 0 );
1424
        phy_ctrl |= 0x1200;
1425
//        os_printf("PHY ctrl %04x\n",phy_ctrl);
1426
        mii_write_register( p_i82544, PHY_ADDRESS, 0, phy_ctrl );
1427
        // Wait for it to complete
1428
        do {
1429
            udelay(100);
1430
            phy_stat = mii_read_register( p_i82544, PHY_ADDRESS, 1 );
1431
            phy_stat = mii_read_register( p_i82544, PHY_ADDRESS, 1 );
1432
        } while( (phy_stat & 0x0020) == 0 );
1433
    }
1434
 
1435
#ifdef DEBUG    
1436
    show_phy( p_i82544, PHY_ADDRESS );
1437
#endif    
1438
#endif
1439
 
1440
#if 0
1441
    // Reset link
1442
    OUTL( ctrl | I82544_CTRL_LRST, ioaddr + I82544_CTRL );
1443
    udelay(20);
1444
    OUTL( ctrl, ioaddr + I82544_CTRL );
1445
    udelay(20);
1446
    show_phy( p_i82544, PHY_ADDRESS );
1447
#endif
1448
 
1449
 
1450
 
1451
    // Transfer speed and duplicity settings from PHY to MAC
1452
 
1453
    // In theory the MAC is supposed to auto-configure from what the
1454
    // PHY has autonegotiated. In practice this does not seem to work
1455
    // (on the 82543 at least, it always thinks it is 1000MHz full
1456
    // duplex) and we have to transfer the settings from the PHY by
1457
    // hand. Additionally, the settings in the PHY ctrl register seem
1458
    // bogus, so we read the values out of the PHY specific status
1459
    // register instead.
1460
 
1461
    {
1462
        cyg_uint16 phy_pssr;
1463
 
1464
        phy_pssr = mii_read_register( p_i82544, PHY_ADDRESS, 17 );
1465
 
1466
        ctrl = INL( ioaddr + I82544_CTRL );
1467
//        os_printf("CTRL %08x\n",ctrl);
1468
        ctrl &= ~(I82544_CTRL_SPEED | I82544_CTRL_FD);
1469
        if( phy_pssr & (1<<13) )
1470
            ctrl |= I82544_CTRL_FD;
1471
 
1472
        // Transfer speed
1473
        ctrl |= ((phy_pssr>>14)&3)<<8;
1474
 
1475
        ctrl |= I82544_CTRL_FRCDPLX | I82544_CTRL_FRCSPD;
1476
 
1477
        OUTL( ctrl, ioaddr + I82544_CTRL );
1478
//        os_printf("CTRL %08x\n",ctrl);
1479
    }
1480
 
1481
 
1482
#if 0
1483
#ifdef DEBUG
1484
    {
1485
        int status = i82544_status( sc );
1486
        static int speed[4] = { 10, 100, 1000, 1000 };
1487
        os_printf("i82544_start %d flg %x Link = %s, %d Mbps, %s Duplex\n",
1488
                  p_i82544->index,
1489
                  *(int *)p_i82544,
1490
                  status & GEN_STATUS_LINK ? "Up" : "Down",
1491
                  speed[(status & GEN_STATUS_BPS)>>GEN_STATUS_BPS_SHIFT],
1492
                  status & GEN_STATUS_FDX ? "Full" : "Half");
1493
    }
1494
#endif
1495
#endif
1496
 
1497
    // Hang around here for a bit to let the device settle down. We
1498
    // don't seem to get away without this.
1499
 
1500
    udelay( 1000000 );
1501
 
1502
#if 0
1503
 
1504
    // Having done all that, the interface still does not work
1505
    // properly, UNLESS I now wait >= 45 seconds here. After that it
1506
    // seems happy. I cannot find any difference in the state of the
1507
    // PHY or the 82543 to explain this.
1508
 
1509
    show_phy( p_i82544, PHY_ADDRESS );
1510
    os_printf("CTRL %08x\n",INL( ioaddr + I82544_CTRL ));
1511
    os_printf("STATUS %08x\n",INL( ioaddr + I82544_STATUS ));
1512
    os_printf("ICR %08x\n",INL( ioaddr + I82544_ICR ));
1513
    os_printf("RCTL %08x\n",INL( ioaddr + I82544_RCTL ));
1514
    os_printf("TCTL %08x\n",INL( ioaddr + I82544_TCTL ));
1515
 
1516
    os_printf("Waiting 45 seconds\n");
1517
    {
1518
        int i;
1519
        cyg_uint32 status = INL( ioaddr + I82544_STATUS );
1520
//        for( i = 0; i < 60; i++ )       // works
1521
//        for( i = 0; i < 45; i++ )       // works
1522
//        for( i = 0; i < 40; i++ )       // fails
1523
//        for( i = 0; i < 35; i++ )       // fails
1524
//        for( i = 0; i < 30; i++ )       // fails
1525
        {
1526
            cyg_uint32 s;
1527
            PC_WRITE_SCREEN_32( 60, i );
1528
            udelay(1000000);
1529
            s = INL( ioaddr + I82544_STATUS );
1530
            if( s != status )
1531
            {
1532
                os_printf("%d STATUS change %08x\n",i,s);
1533
                status = s;
1534
            }
1535
        }
1536
    }
1537
 
1538
    show_phy( p_i82544, PHY_ADDRESS );
1539
    os_printf("CTRL %08x\n",INL( ioaddr + I82544_CTRL ));
1540
    os_printf("STATUS %08x\n",INL( ioaddr + I82544_STATUS ));
1541
    os_printf("ICR %08x\n",INL( ioaddr + I82544_ICR ));
1542
    os_printf("RCTL %08x\n",INL( ioaddr + I82544_RCTL ));
1543
    os_printf("TCTL %08x\n",INL( ioaddr + I82544_TCTL ));
1544
 
1545
#endif
1546
#endif // CYGHWR_DEVS_ETH_INTEL_I82544_USE_ASD
1547
 
1548
    // Set up interrupts
1549
 
1550
    // Clear any pending interrupts
1551
    ctrl = INL( ioaddr + I82544_ICR );
1552
 
1553
    // clear all mask bits
1554
    OUTL( 0xFFFFFFFF, ioaddr + I82544_IMC );
1555
 
1556
    // Set interrupt bits for:
1557
    // 1 = Transmit queue empty
1558
    // 7 = Receiver timeout interrupt
1559
    OUTL( (1<<1)|(1<<7), ioaddr + I82544_IMS );
1560
 
1561
}
1562
 
1563
// ------------------------------------------------------------------------
1564
//
1565
//  Function : i82544_start
1566
//
1567
// ------------------------------------------------------------------------
1568
 
1569
static void
1570
i82544_start( struct eth_drv_sc *sc, unsigned char *enaddr, int flags )
1571
{
1572
    struct i82544 *p_i82544;
1573
    cyg_uint32 ioaddr;
1574
    cyg_uint32 txctl, rxctl;
1575
 
1576
#ifdef CYGPKG_NET
1577
    struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1578
#endif
1579
#ifdef DEBUG
1580
    db_printf("i82544_start\n");
1581
#endif
1582
 
1583
    p_i82544 = (struct i82544 *)sc->driver_private;
1584
    ioaddr = p_i82544->io_address; // get 82544's I/O address
1585
 
1586
    IF_BAD_82544( p_i82544 ) {
1587
#ifdef DEBUG
1588
        os_printf( "i82544_start: Bad device pointer %x\n", p_i82544 );
1589
#endif
1590
        return;
1591
    }
1592
 
1593
    if ( ! p_i82544->mac_addr_ok ) {
1594
#ifdef DEBUG
1595
        os_printf("i82544_start %d: invalid MAC address, "
1596
                  "can't bring up interface\n",
1597
                  p_i82544->index );
1598
#endif
1599
        return;
1600
    }
1601
 
1602
    if ( p_i82544->active )
1603
        i82544_stop( sc );
1604
 
1605
    // Enable device
1606
    p_i82544->active = 1;
1607
 
1608
    /* Enable promiscuous mode if requested, reception of oversized frames always.
1609
     * The latter is needed for VLAN support and shouldn't hurt even if we're not
1610
     * using VLANs.
1611
     */
1612
    i82544_configure(p_i82544, 0
1613
#ifdef CYGPKG_NET
1614
                     || !!(ifp->if_flags & IFF_PROMISC)
1615
#endif
1616
#ifdef ETH_DRV_FLAGS_PROMISC_MODE
1617
                     || !!(flags & ETH_DRV_FLAGS_PROMISC_MODE)
1618
#endif
1619
                     , 1);
1620
 
1621
    // enable receiver
1622
    rxctl = INL( ioaddr + I82544_RCTL );
1623
    rxctl |= I82544_RCTL_EN;
1624
    OUTL( rxctl, ioaddr + I82544_RCTL );
1625
 
1626
    // Enable transmitter
1627
    txctl = INL( ioaddr + I82544_TCTL );
1628
    txctl |= I82544_TCTL_EN;
1629
    OUTL( txctl, ioaddr + I82544_TCTL );
1630
 
1631
}
1632
 
1633
// ------------------------------------------------------------------------
1634
//
1635
//  Function : i82544_status
1636
//
1637
// ------------------------------------------------------------------------
1638
int
1639
i82544_status( struct eth_drv_sc *sc )
1640
{
1641
    int status;
1642
    struct i82544 *p_i82544;
1643
    cyg_uint32 ioaddr;
1644
#ifdef DEBUG
1645
    db_printf("i82544_status\n");
1646
#endif
1647
 
1648
    p_i82544 = (struct i82544 *)sc->driver_private;
1649
 
1650
    IF_BAD_82544( p_i82544 ) {
1651
#ifdef DEBUG
1652
        os_printf( "i82544_status: Bad device pointer %x\n", p_i82544 );
1653
#endif
1654
        return 0;
1655
    }
1656
 
1657
    ioaddr = p_i82544->io_address; // get 82544's I/O address
1658
 
1659
    status = INL(ioaddr + I82544_STATUS);
1660
 
1661
    return status;
1662
}
1663
 
1664
// ------------------------------------------------------------------------
1665
//
1666
//  Function : BringDown82544
1667
//
1668
// ------------------------------------------------------------------------
1669
 
1670
static void
1671
i82544_stop( struct eth_drv_sc *sc )
1672
{
1673
    struct i82544 *p_i82544;
1674
    cyg_uint32 ioaddr;
1675
    cyg_uint32 txctl, rxctl;
1676
 
1677
#ifdef DEBUG
1678
    db_printf("i82544_stop\n");
1679
#endif
1680
 
1681
    p_i82544 = (struct i82544 *)sc->driver_private;
1682
 
1683
    IF_BAD_82544( p_i82544 ) {
1684
#ifdef DEBUG
1685
        os_printf( "i82544_stop: Bad device pointer %x\n", p_i82544 );
1686
#endif
1687
        return;
1688
    }
1689
 
1690
#ifdef DEBUG
1691
    os_printf("i82544_stop %d flg %x\n", p_i82544->index, *(int *)p_i82544 );
1692
#endif
1693
 
1694
    p_i82544->active = 0;               // stop people tormenting it
1695
 
1696
    ioaddr = p_i82544->io_address;
1697
 
1698
    // disable receiver
1699
    rxctl = INL( ioaddr + I82544_RCTL );
1700
    rxctl &= ~I82544_RCTL_EN;
1701
    OUTL( rxctl, ioaddr + I82544_RCTL );
1702
 
1703
    // Enable transmitter
1704
    txctl = INL( ioaddr + I82544_TCTL );
1705
    txctl &= ~I82544_TCTL_EN;
1706
    OUTL( txctl, ioaddr + I82544_TCTL );
1707
 
1708
}
1709
 
1710
 
1711
// ------------------------------------------------------------------------
1712
//
1713
//  Function : InitRxRing
1714
//
1715
// ------------------------------------------------------------------------
1716
 
1717
static void
1718
InitRxRing(struct i82544* p_i82544)
1719
{
1720
    int i;
1721
    CYG_ADDRESS rxring;
1722
    cyg_uint32 ioaddr = p_i82544->io_address;
1723
    cyg_uint32 rxctl;
1724
 
1725
#ifdef DEBUG_82544
1726
    os_printf("InitRxRing %d\n", p_i82544->index);
1727
#endif
1728
 
1729
    // Allocate array of Rx desriptors
1730
    rxring = pciwindow_mem_alloc(
1731
        MAX_RX_DESCRIPTORS * I82544_RD_SIZE + 32 );
1732
 
1733
    // assign ring structure, aligning it on a 16 byte boudary.
1734
    p_i82544->rx_ring = (rxring + 15) & ~15;
1735
 
1736
    // Allocate and fill in buffer pointers
1737
    for ( i = 0; i < MAX_RX_DESCRIPTORS; i++) {
1738
        CYG_ADDRESS rd = p_i82544->rx_ring + (i*I82544_RD_SIZE);
1739
        CYG_ADDRESS buf = pciwindow_mem_alloc(MAX_RX_PACKET_SIZE);
1740
        WRITEMEM64( rd + I82544_RD_BUFFER, VIRT_TO_BUS(buf) );
1741
    }
1742
 
1743
    // Set the receiver queue registers
1744
 
1745
    OUTL( VIRT_TO_BUS(p_i82544->rx_ring), ioaddr + I82544_RDBAL );
1746
    OUTL( 0, ioaddr + I82544_RDBAH );
1747
    OUTL( MAX_RX_DESCRIPTORS * I82544_RD_SIZE, ioaddr + I82544_RDLEN );
1748
    OUTL( 0, ioaddr + I82544_RDH );
1749
    OUTL( MAX_RX_DESCRIPTORS - 5, ioaddr + I82544_RDT );
1750
 
1751
    // zero out RAT
1752
 
1753
    for( i = 0; i < 32; i++ )
1754
        OUTL( 0, ioaddr + I82544_RAT +(i*4) );
1755
 
1756
    // Zero out MTA
1757
    for( i = 0; i < 128; i++ )
1758
        OUTL( 0, ioaddr + I82544_MTA +(i*4) );
1759
 
1760
    // Set up receiver to accept broadcasts
1761
    rxctl = INL( ioaddr + I82544_RCTL );
1762
    rxctl |= I82544_RCTL_BAM;
1763
    OUTL( rxctl, ioaddr + I82544_RCTL );
1764
 
1765
#ifdef DEBUG_82544
1766
    os_printf("RCTL %08x\n",rxctl);
1767
#endif
1768
 
1769
    p_i82544->next_rx_descriptor = 0;
1770
    p_i82544->rx_pointer = 0;
1771
}
1772
 
1773
// ------------------------------------------------------------------------
1774
//
1775
//  Function : PacketRxReady     (Called from delivery thread & foreground)
1776
//
1777
// ------------------------------------------------------------------------
1778
 
1779
static void
1780
PacketRxReady(struct i82544* p_i82544)
1781
{
1782
    struct cyg_netdevtab_entry *ndp;
1783
    struct eth_drv_sc *sc;
1784
    cyg_int32 rxp;
1785
    cyg_uint32 ioaddr;
1786
 
1787
#ifdef DEBUG_82544
1788
//    db_printf("PacketRxReady\n");
1789
#endif
1790
 
1791
    ndp = (struct cyg_netdevtab_entry *)(p_i82544->ndp);
1792
    sc = (struct eth_drv_sc *)(ndp->device_instance);
1793
 
1794
    CHECK_NDP_SC_LINK();
1795
 
1796
    ioaddr = p_i82544->io_address;
1797
 
1798
 
1799
    rxp = p_i82544->rx_pointer;
1800
 
1801
    for(;;)
1802
    {
1803
        cyg_int32 rxh, rxt;
1804
        CYG_ADDRESS dp;
1805
        cyg_uint8 status;
1806
 
1807
        rxh = INL( ioaddr + I82544_RDH );
1808
 
1809
#if 0 //def DEBUG_82544
1810
        os_printf("rxp %04d rxh %04x\n",rxp,rxh);
1811
#endif
1812
 
1813
        // If the head pointer has not advanced, there have been no
1814
        // packets received.
1815
        if( rxh == rxp )
1816
            break;
1817
 
1818
        // Form packet address
1819
        dp = p_i82544->rx_ring + (rxp * I82544_RD_SIZE);
1820
 
1821
        // Get status
1822
        READMEM8( dp + I82544_RD_STATUS, status );
1823
 
1824
#if 0 //def DEBUG_82544
1825
        {
1826
            cyg_uint16 length;
1827
            READMEM16( dp + I82544_RD_LENGTH, length );
1828
            os_printf("rxp %04d status %02x length %d\n",rxp,status,length);
1829
        }
1830
#endif
1831
 
1832
        if( status & I82544_RD_STATUS_EOP )
1833
        {
1834
            cyg_uint16 length;
1835
 
1836
            READMEM16( dp + I82544_RD_LENGTH, length );
1837
 
1838
#ifdef DEBUG_82544
1839
        os_printf("rxp %04d length %d\n",rxp,length);
1840
#endif
1841
            CYG_ASSERT( MAX_RX_PACKET_SIZE >= length, "Oversize Rx" );
1842
 
1843
            // tell the callback the right packet
1844
            p_i82544->next_rx_descriptor = rxp;
1845
 
1846
#ifdef CYGPKG_NET
1847
            if ( length > sizeof( struct ether_header ) )
1848
            // then it is acceptable; offer the data to the network stack
1849
#endif
1850
            (sc->funs->eth_drv->recv)( sc, length );
1851
 
1852
            // All done!
1853
        }
1854
 
1855
        // Advance rxp pointer
1856
        rxp = ( rxp + 1 ) % MAX_RX_DESCRIPTORS;
1857
 
1858
        // We can now also advance the tail pointer by 1
1859
        rxt = INL( ioaddr + I82544_RDT );
1860
        dp = p_i82544->rx_ring + (rxt * I82544_RD_SIZE);
1861
        WRITEMEM8( dp + I82544_RD_STATUS, status );
1862
        rxt = ( rxt + 1 ) % MAX_RX_DESCRIPTORS;
1863
        OUTL( rxt, ioaddr + I82544_RDT );
1864
 
1865
#ifdef CYGPKG_IO_ETH_DRIVERS_STAND_ALONE
1866
        // Can't deliver more than one packet in polled standalone mode
1867
        break;
1868
#endif
1869
    }
1870
 
1871
    // Save next rx pointer for next time.
1872
    p_i82544->rx_pointer = rxp;
1873
 
1874
}
1875
 
1876
// and the callback function
1877
 
1878
static void
1879
i82544_recv( struct eth_drv_sc *sc, struct eth_drv_sg *sg_list, int sg_len )
1880
{
1881
    struct i82544 *p_i82544;
1882
    cyg_int32 rxp;
1883
    CYG_ADDRESS dp;
1884
    CYG_ADDRESS from_p;
1885
    cyg_uint16 total_len;
1886
    struct eth_drv_sg *last_sg;
1887
 
1888
#ifdef DEBUG_82544
1889
    db_printf("i82544_recv\n");
1890
#endif
1891
 
1892
    p_i82544 = (struct i82544 *)sc->driver_private;
1893
 
1894
    IF_BAD_82544( p_i82544 ) {
1895
#ifdef DEBUG_82544
1896
        os_printf( "i82544_recv: Bad device pointer %x\n", p_i82544 );
1897
#endif
1898
        return;
1899
    }
1900
 
1901
    rxp = p_i82544->next_rx_descriptor;
1902
    // Form packet address
1903
    dp = p_i82544->rx_ring + (rxp * I82544_RD_SIZE);
1904
 
1905
#if 0 //def DEBUG_82544
1906
        {
1907
            int i;
1908
            os_printf("RxD %08x",dp);
1909
            for( i = 0; i < 16; i++ )
1910
            {
1911
                cyg_uint8 b;
1912
                if( (i%8) == 0 ) os_printf("\n");
1913
                READMEM8( dp + i, b );
1914
                os_printf("%02x ",b);
1915
            }
1916
            os_printf("\n");
1917
        }
1918
#endif        
1919
    // Copy the data to the network stack
1920
    READMEM64( dp + I82544_RD_BUFFER, from_p );
1921
    from_p = BUS_TO_VIRT(from_p);
1922
    READMEM16( dp + I82544_RD_LENGTH, total_len );
1923
 
1924
#ifdef DEBUG_82544
1925
    db_printf("RXP: %04x len %d\n",rxp,total_len);
1926
#endif
1927
 
1928
    // check we have memory to copy into; we would be called even if
1929
    // caller was out of memory in order to maintain our state.
1930
    if ( 0 == sg_len || 0 == sg_list )
1931
        return; // caller was out of mbufs
1932
 
1933
    CYG_ASSERT( 0 < sg_len, "sg_len underflow" );
1934
    CYG_ASSERT( MAX_ETH_DRV_SG >= sg_len, "sg_len overflow" );
1935
 
1936
    for ( last_sg = &sg_list[sg_len]; sg_list < last_sg; sg_list++ ) {
1937
        cyg_uint8 *to_p;
1938
        int l;
1939
 
1940
        to_p = (cyg_uint8 *)(sg_list->buf);
1941
        l = sg_list->len;
1942
 
1943
        CYG_ASSERT( 0 <= l, "sg length -ve" );
1944
 
1945
        if ( 0 >= l || 0 == to_p )
1946
            return; // caller was out of mbufs
1947
 
1948
        if ( l > total_len )
1949
            l = total_len;
1950
 
1951
#if 0 //def DEBUG_82544
1952
    {
1953
        int i,ll = l;
1954
        os_printf("Pkt len %d",l);
1955
        if( ll > 32 ) ll = 32;
1956
        for( i = 0; i < ll; i++ )
1957
        {
1958
            cyg_uint8 b;
1959
            if( (i%8) == 0 ) os_printf("\n %04x: ",i);
1960
            b = ((cyg_uint8 *)from_p)[i];
1961
            os_printf("%02x ",b);
1962
        }
1963
        os_printf("\n");
1964
 
1965
    }
1966
#endif
1967
        memcpy( to_p, (unsigned char *)from_p, l );
1968
        from_p += l;
1969
        total_len -= l;
1970
    }
1971
 
1972
    CYG_ASSERT( 0 == total_len, "total_len mismatch in rx" );
1973
    CYG_ASSERT( last_sg == sg_list, "sg count mismatch in rx" );
1974
 
1975
}
1976
 
1977
 
1978
// ------------------------------------------------------------------------
1979
//
1980
//  Function : InitTxRing
1981
//
1982
// ------------------------------------------------------------------------
1983
 
1984
static void
1985
InitTxRing(struct i82544* p_i82544)
1986
{
1987
    int i;
1988
    cyg_uint32 ioaddr = p_i82544->io_address;
1989
    CYG_ADDRESS txring;
1990
    cyg_uint32 txctl;
1991
 
1992
#ifdef DEBUG_82544
1993
    os_printf("InitTxRing %d\n", p_i82544->index);
1994
#endif
1995
 
1996
    // Allocate array of Tx desriptors
1997
    txring = pciwindow_mem_alloc(
1998
        MAX_TX_DESCRIPTORS * I82544_TD_SIZE + 32 );
1999
 
2000
    // assign ring structure, aligning it on a 16 byte boudary.
2001
    p_i82544->tx_ring = (txring + 15) & ~15;
2002
 
2003
    // Allocate and fill in buffer pointers
2004
    for ( i = 0; i < MAX_TX_DESCRIPTORS; i++) {
2005
        CYG_ADDRESS td = p_i82544->tx_ring + (i*I82544_TD_SIZE);
2006
        WRITEMEM64( td + I82544_TD_BUFFER, 0 );
2007
    }
2008
 
2009
    // Set the transmitter queue registers
2010
 
2011
    OUTL( VIRT_TO_BUS(p_i82544->tx_ring), ioaddr + I82544_TDBAL );
2012
    OUTL( 0, ioaddr + I82544_TDBAH );
2013
    OUTL( MAX_TX_DESCRIPTORS * I82544_TD_SIZE, ioaddr + I82544_TDLEN );
2014
    OUTL( 0, ioaddr + I82544_TDH );
2015
    OUTL( 0, ioaddr + I82544_TDT );
2016
 
2017
    // Set IPG values
2018
    OUTL( 8 | (8<<10) | (6<<20), ioaddr + I82544_TIPG );
2019
 
2020
    // Program tx ctrl register
2021
    txctl = INL( ioaddr + I82544_TCTL );
2022
    txctl |= (15<<4);   // collision threshold
2023
    txctl |= (64<<12);  // collision distance
2024
    txctl |= I82544_TCTL_PSP;
2025
    OUTL( txctl, ioaddr + I82544_TCTL );
2026
 
2027
    p_i82544->tx_in_progress = 0;
2028
    p_i82544->tx_pointer = 0;
2029
 
2030
}
2031
 
2032
// ------------------------------------------------------------------------
2033
//
2034
//  Function : TxDone          (Called from delivery thread)
2035
//
2036
// This returns Tx's from the Tx Machine to the stack (ie. reports
2037
// completion) - allowing for missed interrupts, and so on.
2038
// ------------------------------------------------------------------------
2039
 
2040
static void
2041
TxDone(struct i82544* p_i82544)
2042
{
2043
    struct cyg_netdevtab_entry *ndp;
2044
    struct eth_drv_sc *sc;
2045
    cyg_uint32 ioaddr;
2046
    cyg_int32 txp = p_i82544->tx_pointer;
2047
 
2048
#ifdef DEBUG_82544
2049
//    db_printf("TxDone\n");
2050
#endif
2051
 
2052
    ndp = (struct cyg_netdevtab_entry *)(p_i82544->ndp);
2053
    sc = (struct eth_drv_sc *)(ndp->device_instance);
2054
 
2055
    CHECK_NDP_SC_LINK();
2056
 
2057
    ioaddr = p_i82544->io_address;      // get device I/O address
2058
 
2059
    if( !p_i82544->active )
2060
        return;
2061
 
2062
    for(;;)
2063
    {
2064
        cyg_uint8 status;
2065
        cyg_uint8 cmd;
2066
        CYG_ADDRESS dp;
2067
        cyg_int32 txh;
2068
 
2069
        txh = INL( ioaddr + I82544_TDH );
2070
 
2071
        // If there has been no advance on the transmit header,
2072
        // nothing to do.
2073
        if( txh == txp )
2074
            break;
2075
 
2076
#ifdef DEBUG_82544
2077
        os_printf("TxDone: TxH %04d TxP %04d\n",txh,txp);
2078
#endif        
2079
 
2080
        // Get descriptor address
2081
        dp = p_i82544->tx_ring + (txp * I82544_TD_SIZE);
2082
 
2083
        READMEM8( dp + I82544_TD_CMD, cmd );
2084
        READMEM8( dp + I82544_TD_STATUS, status );
2085
#ifdef DEBUG_82544
2086
        os_printf("cmd %02x status %02x\n",cmd,status);
2087
#endif        
2088
 
2089
        // Zero out buffer and command
2090
        WRITEMEM64( dp + I82544_TD_BUFFER, 0 );
2091
        WRITEMEM8( dp + I82544_TD_CMD, 0 );
2092
 
2093
        if( cmd & I82544_TD_CMD_EOP )
2094
        {
2095
            // A done end of packet descrptor
2096
 
2097
            if( p_i82544->tx_keys[txp] != 0 )
2098
            {
2099
                // Call network stack with correct value of txp in structure.
2100
                // There may be recursive calls in here, so we need to make sure
2101
                // that txp is updated correctly.
2102
                p_i82544->tx_pointer = ( txp + 1 ) % MAX_TX_DESCRIPTORS;
2103
                (sc->funs->eth_drv->tx_done)( sc, p_i82544->tx_keys[txp], 0 );
2104
                txp = p_i82544->tx_pointer;
2105
                continue;
2106
            }
2107
        }
2108
 
2109
        // Advance tx pointer
2110
        txp = ( txp + 1 ) % MAX_TX_DESCRIPTORS;
2111
 
2112
    }
2113
 
2114
    // restore txp to data structure.
2115
    p_i82544->tx_pointer = txp;
2116
 
2117
}
2118
 
2119
 
2120
// ------------------------------------------------------------------------
2121
//
2122
//  Function : i82544_can_send
2123
//
2124
// ------------------------------------------------------------------------
2125
 
2126
static int
2127
i82544_can_send(struct eth_drv_sc *sc)
2128
{
2129
    struct i82544 *p_i82544;
2130
    cyg_uint32 ioaddr;
2131
 
2132
#ifdef DEBUG_82544
2133
//    db_printf("i82544_can_send\n");
2134
#endif
2135
 
2136
    p_i82544 = (struct i82544 *)sc->driver_private;
2137
 
2138
    IF_BAD_82544( p_i82544 ) {
2139
#ifdef DEBUG_82544
2140
        os_printf( "i82544_send: Bad device pointer %x\n", p_i82544 );
2141
#endif
2142
        return 0;
2143
    }
2144
 
2145
    ioaddr = p_i82544->io_address;      // get device I/O address
2146
 
2147
    if ( p_i82544->active )
2148
    {
2149
        cyg_int32 txh, txt, diff;
2150
 
2151
        // Poll for Tx completion
2152
        TxDone( p_i82544 );
2153
 
2154
#ifndef CYGPKG_IO_ETH_DRIVERS_STAND_ALONE
2155
        // We are not prepared to receive a packet now if we are in a polled
2156
        // standalone configuration.
2157
 
2158
        // Poll for receptions
2159
        PacketRxReady( p_i82544 );
2160
#endif
2161
 
2162
        // Now see whether the Tx queue has space for another
2163
        // transmit.  We look at the difference between the head and
2164
        // tail pointer, and if there is space for at least 5 more
2165
        // descriptors, we allow a new transmission to go ahead.
2166
        txh = INL( ioaddr + I82544_TDH );
2167
        txt = INL( ioaddr + I82544_TDT );
2168
 
2169
        diff = (txh-1) - txt;
2170
        if( diff < 0 ) diff += MAX_TX_DESCRIPTORS;
2171
#ifdef DEBUG_82544
2172
//        os_printf("TxH %04d TxT %04d diff %04d\n",txh,txt,diff);
2173
#endif
2174
        return diff > 5;
2175
    }
2176
 
2177
    return false;
2178
 
2179
}
2180
 
2181
// ------------------------------------------------------------------------
2182
//
2183
//  Function : i82544_send
2184
//
2185
// ------------------------------------------------------------------------
2186
 
2187
static void
2188
i82544_send(struct eth_drv_sc *sc,
2189
            struct eth_drv_sg *sg_list, int sg_len, int total_len,
2190
            unsigned long key)
2191
{
2192
    struct i82544 *p_i82544;
2193
    cyg_uint32 ioaddr;
2194
    struct eth_drv_sg *last_sg;
2195
    cyg_int32 txt;
2196
 
2197
 
2198
#ifdef DEBUG_82544
2199
    db_printf("i82544_send\n");
2200
#endif
2201
 
2202
    p_i82544 = (struct i82544 *)sc->driver_private;
2203
 
2204
    IF_BAD_82544( p_i82544 ) {
2205
#ifdef DEBUG_82544
2206
        os_printf( "i82544_send: Bad device pointer %x\n", p_i82544 );
2207
#endif
2208
        return;
2209
    }
2210
 
2211
#ifdef DEBUG_82544
2212
    os_printf("Tx %d %x: %d sg's, %d bytes, KEY %x\n",
2213
              p_i82544->index, (int)p_i82544, sg_len, total_len, key );
2214
#endif
2215
 
2216
    if ( ! p_i82544->active )
2217
        return;                         // device inactive, no return
2218
    ioaddr = p_i82544->io_address;      // get device I/O address
2219
 
2220
    // Get the tx tail pointer
2221
    txt = INL( ioaddr + I82544_TDT );
2222
 
2223
    for ( last_sg = &sg_list[sg_len]; sg_list < last_sg; sg_list++ )
2224
    {
2225
        cyg_uint8 *from_p;
2226
        cyg_uint8 cmd = 0;
2227
        CYG_ADDRESS dp;
2228
        int l;
2229
 
2230
        from_p = (cyg_uint8 *)(sg_list->buf); // normal cached address
2231
        l = sg_list->len;
2232
 
2233
        if ( l > total_len )
2234
            l = total_len;
2235
 
2236
        // Get the descriptor address from the tail pointer.
2237
        dp = p_i82544->tx_ring + (txt * I82544_TD_SIZE);
2238
 
2239
        total_len -= l;
2240
 
2241
#ifdef HAL_DCACHE_STORE
2242
        HAL_DCACHE_STORE( ((CYG_ADDRESS)from_p) &~(HAL_DCACHE_LINE_SIZE-1),
2243
                          l + (HAL_DCACHE_LINE_SIZE-1) );
2244
#endif
2245
 
2246
        WRITEMEM64( dp + I82544_TD_BUFFER, VIRT_TO_BUS(from_p) );
2247
        WRITEMEM16( dp + I82544_TD_LENGTH, l );
2248
 
2249
        // Set EOP bit on last packet
2250
        if( total_len <= 0 )
2251
            cmd |= I82544_TD_CMD_EOP;
2252
 
2253
        // Get status back
2254
//        cmd |= I82544_TD_CMD_RPS | I82544_TD_CMD_RS;
2255
 
2256
        cmd |= I82544_TD_CMD_IFCS;
2257
 
2258
        // Write command byte
2259
        WRITEMEM8( dp + I82544_TD_CMD, cmd );
2260
 
2261
        // Zero out rest of fields
2262
        WRITEMEM8( dp + I82544_TD_STATUS, 0 );
2263
        WRITEMEM8( dp + I82544_TD_CSO, 0 );
2264
        WRITEMEM8( dp + I82544_TD_CSS, 0 );
2265
        WRITEMEM16( dp + I82544_TD_SPECIAL, 0 );
2266
 
2267
        // Store the key for this transmission in the matching slot
2268
        // for the descriptor.
2269
        p_i82544->tx_keys[txt] = key;
2270
 
2271
        // Increment tx tail pointer
2272
        txt = (txt + 1) % MAX_TX_DESCRIPTORS;
2273
 
2274
    }
2275
 
2276
    // And finally, cause the transmission to happen by setting the
2277
    // tail pointer in the hardware.
2278
    OUTL( txt, ioaddr + I82544_TDT );
2279
 
2280
}
2281
 
2282
// ------------------------------------------------------------------------
2283
//
2284
//  Function : i82544_reset
2285
//
2286
// ------------------------------------------------------------------------
2287
static void
2288
i82544_reset(struct i82544* p_i82544)
2289
{
2290
    cyg_uint32 ioaddr = p_i82544->io_address;
2291
    cyg_uint32 ctrl;
2292
 
2293
#ifdef DEBUG
2294
    db_printf("i82544_reset\n");
2295
#endif
2296
 
2297
    ctrl = INL( ioaddr + I82544_CTRL );
2298
 
2299
    // reset controller
2300
    OUTL( ctrl | I82544_CTRL_RST, ioaddr + I82544_CTRL );
2301
    udelay(20);
2302
    ctrl = INL( ioaddr + I82544_CTRL );
2303
 
2304
}
2305
 
2306
// ------------------------------------------------------------------------
2307
//
2308
//                       INTERRUPT HANDLERS
2309
//
2310
// ------------------------------------------------------------------------
2311
 
2312
static cyg_uint32
2313
eth_isr(cyg_vector_t vector, cyg_addrword_t data)
2314
{
2315
    struct i82544* p_i82544 = (struct i82544 *)data;
2316
    cyg_uint16 status;
2317
    cyg_uint32 ioaddr;
2318
 
2319
#ifdef DEBUG_82544
2320
//    db_printf("eth_isr\n");
2321
#endif
2322
 
2323
    IF_BAD_82544( p_i82544 ) {
2324
#ifdef DEBUG_82544
2325
        os_printf( "i82544_isr: Bad device pointer %x\n", p_i82544 );
2326
#endif
2327
        return 0;
2328
    }
2329
 
2330
    ioaddr = p_i82544->io_address;
2331
 
2332
    status = INL( ioaddr + I82544_ICR );
2333
 
2334
#ifdef DEBUG_82544    
2335
    db_printf("eth_isr %04x\n",status);
2336
#endif    
2337
 
2338
    return (CYG_ISR_HANDLED|CYG_ISR_CALL_DSR);        // schedule DSR
2339
}
2340
 
2341
 
2342
// ------------------------------------------------------------------------
2343
 
2344
#ifndef CYGPKG_IO_ETH_DRIVERS_STAND_ALONE
2345
static void
2346
eth_dsr(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
2347
{
2348
    struct i82544* p_i82544 = (struct i82544 *)data;
2349
    struct cyg_netdevtab_entry *ndp =
2350
        (struct cyg_netdevtab_entry *)(p_i82544->ndp);
2351
    struct eth_drv_sc *sc = (struct eth_drv_sc *)(ndp->device_instance);
2352
 
2353
#ifdef DEBUG_82544
2354
    db_printf("eth_dsr\n");
2355
#endif
2356
 
2357
    eth_drv_dsr( vector, count, (cyg_addrword_t)sc );
2358
 
2359
    cyg_drv_interrupt_acknowledge(p_i82544->vector);
2360
}
2361
#endif // !CYGPKG_IO_ETH_DRIVERS_STAND_ALONE
2362
 
2363
// ------------------------------------------------------------------------
2364
// Deliver routine:
2365
 
2366
void
2367
i82544_deliver(struct eth_drv_sc *sc)
2368
{
2369
    struct i82544* p_i82544 = (struct i82544 *)(sc->driver_private);
2370
 
2371
#ifdef DEBUG
2372
    db_printf("i82544_deliver\n");
2373
#endif
2374
 
2375
    // First pass any rx data up the stack
2376
    PacketRxReady(p_i82544);
2377
 
2378
    // Then scan for completed Tx and inform the stack
2379
    TxDone(p_i82544);
2380
}
2381
 
2382
// ------------------------------------------------------------------------
2383
// Device table entry to operate the chip in a polled mode.
2384
// Only diddle the interface we were asked to!
2385
 
2386
void
2387
i82544_poll(struct eth_drv_sc *sc)
2388
{
2389
    struct i82544 *p_i82544;
2390
    p_i82544 = (struct i82544 *)sc->driver_private;
2391
 
2392
#ifdef DEBUG
2393
    db_printf("i82544_poll\n");
2394
#endif
2395
 
2396
    IF_BAD_82544( p_i82544 ) {
2397
#ifdef DEBUG
2398
        os_printf( "i82544_poll: Bad device pointer %x\n", p_i82544 );
2399
#endif
2400
        return;
2401
    }
2402
 
2403
    // As it happens, this driver always requests the DSR to be called:
2404
    (void)eth_isr( p_i82544->vector, (cyg_addrword_t)p_i82544 );
2405
 
2406
    // (no harm in calling this ints-off also, when polled)
2407
    i82544_deliver( sc );
2408
}
2409
 
2410
// ------------------------------------------------------------------------
2411
// Determine interrupt vector used by a device - for attaching GDB stubs
2412
// packet handler.
2413
 
2414
int
2415
i82544_int_vector(struct eth_drv_sc *sc)
2416
{
2417
    struct i82544 *p_i82544;
2418
    p_i82544 = (struct i82544 *)sc->driver_private;
2419
    return (p_i82544->vector);
2420
}
2421
 
2422
 
2423
// ------------------------------------------------------------------------
2424
//
2425
//  Function : pci_init_find_82544s
2426
//
2427
// This is called exactly once at the start of time to:
2428
//  o scan the PCI bus for objects
2429
//  o record them in the device table
2430
//  o acquire all the info needed for the driver to access them
2431
//  o instantiate interrupts for them
2432
//  o attach those interrupts appropriately
2433
// ------------------------------------------------------------------------
2434
 
2435
static cyg_pci_match_func find_82544s_match_func;
2436
 
2437
// Intel 82543 and 82544 are virtually identical, with different
2438
// dev codes
2439
static cyg_bool
2440
find_82544s_match_func( cyg_uint16 v, cyg_uint16 d, cyg_uint32 c, void *p )
2441
{
2442
    return ((0x8086 == v) &&
2443
            ((0x1004 == d) ||   // 82543
2444
             (0x100d == d) ||   // 82543
2445
             (0x1008 == d)      // 82544
2446
            )
2447
           );
2448
}
2449
 
2450
static int
2451
pci_init_find_82544s( void )
2452
{
2453
    cyg_pci_device_id devid;
2454
    cyg_pci_device dev_info;
2455
    cyg_uint16 cmd;
2456
    int device_index;
2457
    int found_devices = 0;
2458
 
2459
#ifdef DEBUG
2460
    db_printf("pci_init_find_82544s()\n");
2461
#endif
2462
 
2463
    // allocate memory to be used in ioctls later
2464
    if (mem_reserved_ioctl != (void*)0) {
2465
#ifdef DEBUG
2466
        db_printf("pci_init_find_82544s() called > once\n");
2467
#endif
2468
        return 0;
2469
    }
2470
 
2471
    // First initialize the heap in PCI window'd memory
2472
    i82544_heap_size = CYGHWR_INTEL_I82544_PCI_MEM_MAP_SIZE;
2473
    i82544_heap_base = (cyg_uint8 *)CYGHWR_INTEL_I82544_PCI_MEM_MAP_BASE;
2474
    i82544_heap_free = i82544_heap_base;
2475
 
2476
//    mem_reserved_ioctl = pciwindow_mem_alloc(MAX_MEM_RESERVED_IOCTL);     
2477
 
2478
    cyg_pci_init();
2479
#ifdef DEBUG
2480
    db_printf("Finished cyg_pci_init();\n");
2481
#endif
2482
 
2483
    devid = CYG_PCI_NULL_DEVID;
2484
 
2485
    for (device_index = 0;
2486
         device_index < CYGNUM_DEVS_ETH_INTEL_I82544_DEV_COUNT;
2487
         device_index++) {
2488
        struct i82544 *p_i82544 = i82544_priv_array[device_index];
2489
 
2490
        p_i82544->index = device_index;
2491
 
2492
        // See above for find_82544s_match_func
2493
        if (cyg_pci_find_matching( &find_82544s_match_func, NULL, &devid )) {
2494
#ifdef DEBUG
2495
            db_printf("eth%d = 82544\n", device_index);
2496
#endif
2497
            cyg_pci_get_device_info(devid, &dev_info);
2498
 
2499
            p_i82544->interrupt_handle = 0; // Flag not attached.
2500
            if (cyg_pci_translate_interrupt(&dev_info, &p_i82544->vector)) {
2501
#ifdef DEBUG
2502
                db_printf(" Wired to HAL vector %d\n", p_i82544->vector);
2503
#endif
2504
#ifndef CYGPKG_IO_ETH_DRIVERS_STAND_ALONE
2505
                cyg_drv_interrupt_create(
2506
                    p_i82544->vector,
2507
                    0,                  // Priority - unused
2508
                    (CYG_ADDRWORD)p_i82544, // Data item passed to ISR & DSR
2509
#ifdef CYGHWR_DEVS_ETH_INTEL_I82544_DEMUX_ALL
2510
                    eth_mux_isr,        // ISR
2511
#else
2512
                    eth_isr,            // ISR
2513
#endif
2514
                    eth_dsr,            // DSR
2515
                    &p_i82544->interrupt_handle, // handle to intr obj
2516
                    &p_i82544->interrupt_object ); // space for int obj
2517
 
2518
                cyg_drv_interrupt_attach(p_i82544->interrupt_handle);
2519
 
2520
                // Don't unmask the interrupt yet, that could get us into a
2521
                // race.
2522
#ifdef CYGNUM_DEVS_ETH_INTEL_I82544_SEPARATE_MUX_INTERRUPT
2523
                // ALSO attach it to MUX interrupt for multiplexed
2524
                // interrupts.  This is for certain boards where the
2525
                // PCI backplane is wired "straight through" instead of
2526
                // with a rotation of interrupt lines in the different
2527
                // slots.
2528
                {
2529
                    static cyg_handle_t mux_interrupt_handle = 0;
2530
                    static cyg_interrupt mux_interrupt_object;
2531
 
2532
                    if ( ! mux_interrupt_handle ) {
2533
#ifdef DEBUG
2534
                        db_printf(" Also attaching to HAL vector %d\n",
2535
                                  CYGNUM_DEVS_ETH_INTEL_I82544_SEPARATE_MUX_INTERRUPT);
2536
#endif
2537
                        cyg_drv_interrupt_create(
2538
                            CYGNUM_DEVS_ETH_INTEL_I82544_SEPARATE_MUX_INTERRUPT,
2539
                            0,              // Priority - unused
2540
                            (CYG_ADDRWORD)p_i82544,// Data item passed to ISR and DSR
2541
                            eth_mux_isr,    // ISR
2542
                            eth_dsr,        // DSR
2543
                            &mux_interrupt_handle,
2544
                            &mux_interrupt_object );
2545
 
2546
                        cyg_drv_interrupt_attach(mux_interrupt_handle);
2547
                    }
2548
                }
2549
#endif // CYGNUM_DEVS_ETH_INTEL_I82544_SEPARATE_MUX_INTERRUPT
2550
#endif // !CYGPKG_IO_ETH_DRIVERS_STAND_ALONE
2551
            }
2552
            else {
2553
                p_i82544->vector=0;
2554
#ifdef DEBUG
2555
                db_printf(" Does not generate interrupts.\n");
2556
#endif
2557
            }
2558
 
2559
            if (cyg_pci_configure_device(&dev_info)) {
2560
#ifdef DEBUG
2561
                int i;
2562
                db_printf("Found device on bus %d, devfn 0x%02x:\n",
2563
                          CYG_PCI_DEV_GET_BUS(devid),
2564
                          CYG_PCI_DEV_GET_DEVFN(devid));
2565
 
2566
                if (dev_info.command & CYG_PCI_CFG_COMMAND_ACTIVE) {
2567
                    db_printf(" Note that board is active. Probed"
2568
                              " sizes and CPU addresses invalid!\n");
2569
                }
2570
                db_printf(" Vendor    0x%04x", dev_info.vendor);
2571
                db_printf("\n Device    0x%04x", dev_info.device);
2572
                db_printf("\n Command   0x%04x, Status 0x%04x\n",
2573
                          dev_info.command, dev_info.status);
2574
 
2575
                db_printf(" Class/Rev 0x%08x", dev_info.class_rev);
2576
                db_printf("\n Header 0x%02x\n", dev_info.header_type);
2577
 
2578
                db_printf(" SubVendor 0x%04x, Sub ID 0x%04x\n",
2579
                          dev_info.header.normal.sub_vendor,
2580
                          dev_info.header.normal.sub_id);
2581
 
2582
                for(i = 0; i < CYG_PCI_MAX_BAR; i++) {
2583
                    db_printf(" BAR[%d]    0x%08x /", i, dev_info.base_address[i]);
2584
                    db_printf(" probed size 0x%08x / CPU addr 0x%08x\n",
2585
                              dev_info.base_size[i], dev_info.base_map[i]);
2586
                }
2587
                db_printf(" eth%d configured\n", device_index);
2588
#endif
2589
                found_devices++;
2590
                p_i82544->found = 1;
2591
                p_i82544->active = 0;
2592
                p_i82544->devid = devid;
2593
                p_i82544->device = dev_info.device;
2594
                p_i82544->io_address = dev_info.base_map[0];
2595
#ifdef DEBUG
2596
                db_printf(" I/O address = 0x%08x device = %04x\n", dev_info.base_map[0],
2597
                                                                   dev_info.device);
2598
#endif
2599
 
2600
                // Don't use cyg_pci_set_device_info since it clears
2601
                // some of the fields we want to print out below.
2602
                cyg_pci_read_config_uint16(dev_info.devid,
2603
                                           CYG_PCI_CFG_COMMAND, &cmd);
2604
                cmd |= (CYG_PCI_CFG_COMMAND_IO         // enable I/O space
2605
                        | CYG_PCI_CFG_COMMAND_MEMORY   // enable memory space
2606
                        | CYG_PCI_CFG_COMMAND_MASTER); // enable bus master
2607
                cyg_pci_write_config_uint16(dev_info.devid,
2608
                                            CYG_PCI_CFG_COMMAND, cmd);
2609
 
2610
                // Now the PCI part of the device is configured, reset
2611
                // it. This should make it safe to enable the
2612
                // interrupt
2613
                i82544_reset(p_i82544);
2614
 
2615
                // This is the indicator for "uses an interrupt"
2616
                if (p_i82544->interrupt_handle != 0) {
2617
                    cyg_drv_interrupt_acknowledge(p_i82544->vector);
2618
#ifndef CYGPKG_IO_ETH_DRIVERS_STAND_ALONE
2619
                    cyg_drv_interrupt_unmask(p_i82544->vector);
2620
#endif // !CYGPKG_IO_ETH_DRIVERS_STAND_ALONE
2621
                }
2622
#ifdef DEBUG
2623
                db_printf(" **** Device enabled for I/O and Memory "
2624
                          "and Bus Master\n");
2625
#endif
2626
            }
2627
            else {
2628
                p_i82544->found = 0;
2629
                p_i82544->active = 0;
2630
#ifdef DEBUG
2631
                db_printf("Failed to configure device %d\n",device_index);
2632
#endif
2633
            }
2634
        }
2635
        else {
2636
            p_i82544->found = 0;
2637
            p_i82544->active = 0;
2638
#ifdef DEBUG
2639
            db_printf("eth%d not found\n", device_index);
2640
#endif
2641
        }
2642
    }
2643
 
2644
    if (0 == found_devices)
2645
        return 0;
2646
 
2647
#ifdef CYGNUM_DEVS_ETH_INTEL_I82544_SEPARATE_MUX_INTERRUPT
2648
    // Now enable the mux shared interrupt if it is in use
2649
    if (mux_interrupt_handle) {
2650
        cyg_drv_interrupt_acknowledge(CYGNUM_DEVS_ETH_INTEL_I82544_SEPARATE_MUX_INTERRUPT);
2651
        cyg_drv_interrupt_unmask(CYGNUM_DEVS_ETH_INTEL_I82544_SEPARATE_MUX_INTERRUPT);
2652
    }
2653
#endif
2654
 
2655
    // Now a delay to ensure the hardware has "come up" before you try to
2656
    // use it.  Yes, really, the full 2 seconds.  It's only really
2657
    // necessary if DEBUG is off - otherwise all that printout wastes
2658
    // enough time.  No kidding.
2659
    udelay( 2000000 );
2660
    return 1;
2661
}
2662
 
2663
// ------------------------------------------------------------------------
2664
//
2665
//  Function : i82544_configure
2666
//
2667
//  Return : 0 = It worked.
2668
//           non0 = It failed.
2669
// ------------------------------------------------------------------------
2670
 
2671
static int i82544_configure(struct i82544* p_i82544, int promisc, int oversized)
2672
{
2673
    cyg_uint32  ioaddr;
2674
//    volatile CFG *ccs;
2675
//    volatile cyg_uint8* config_bytes;
2676
//    cyg_uint16 status;
2677
//    int count;
2678
 
2679
    IF_BAD_82544( p_i82544 ) {
2680
#ifdef DEBUG
2681
        os_printf( "eth_set_promiscuous_mode: Bad device pointer %x\n",
2682
                   p_i82544 );
2683
#endif
2684
        return -1;
2685
    }
2686
 
2687
    ioaddr = p_i82544->io_address;
2688
 
2689
    // Not currently supported
2690
 
2691
    return 0;
2692
}
2693
 
2694
// ------------------------------------------------------------------------
2695
//
2696
//  Function : eth_set_mac_address
2697
//
2698
//  Return : 0 = It worked.
2699
//           non0 = It failed.
2700
// ------------------------------------------------------------------------
2701
 
2702
static int
2703
eth_set_mac_address(struct i82544* p_i82544, cyg_uint8 *addr, int eeprom)
2704
{
2705
    cyg_uint32  ioaddr;
2706
    cyg_uint32 maclo, machi;
2707
 
2708
#ifdef DEBUG
2709
    db_printf("eth_set_mac_address\n");
2710
#endif
2711
 
2712
    IF_BAD_82544( p_i82544 ) {
2713
#ifdef DEBUG
2714
        os_printf( "eth_set_mac_address : Bad device pointer %x\n",
2715
                   p_i82544 );
2716
#endif
2717
        return -1;
2718
    }
2719
 
2720
    ioaddr = p_i82544->io_address;
2721
 
2722
    // Write MAC address to RAT[0]
2723
 
2724
    maclo = addr[0] | (addr[1]<<8) | (addr[2]<<16) | (addr[3]<<24);
2725
    machi = (1<<31) | (0<<16) | addr[4] | (addr[5]<<8);
2726
 
2727
#ifdef DEBUG
2728
    os_printf("setting: lo %08x hi %08x\n",maclo,machi);
2729
#endif
2730
 
2731
    OUTL( maclo , ioaddr + I82544_RAT );
2732
    OUTL( machi, ioaddr + I82544_RAT + 4 );
2733
 
2734
 
2735
    // record the MAC address in the device structure
2736
    p_i82544->mac_address[0] = addr[0];
2737
    p_i82544->mac_address[1] = addr[1];
2738
    p_i82544->mac_address[2] = addr[2];
2739
    p_i82544->mac_address[3] = addr[3];
2740
    p_i82544->mac_address[4] = addr[4];
2741
    p_i82544->mac_address[5] = addr[5];
2742
    p_i82544->mac_addr_ok = 1;
2743
 
2744
#ifdef DEBUG
2745
    os_printf( "Set MAC Address = %02X %02X %02X %02X %02X %02X (ok %d)\n",
2746
               p_i82544->mac_address[0],
2747
               p_i82544->mac_address[1],
2748
               p_i82544->mac_address[2],
2749
               p_i82544->mac_address[3],
2750
               p_i82544->mac_address[4],
2751
               p_i82544->mac_address[5],
2752
               p_i82544->mac_addr_ok       );
2753
#endif
2754
 
2755
    return p_i82544->mac_addr_ok ? 0 : 1;
2756
}
2757
 
2758
// ------------------------------------------------------------------------
2759
//
2760
//  Function : eth_get_mac_address
2761
//
2762
// ------------------------------------------------------------------------
2763
#ifdef ETH_DRV_GET_MAC_ADDRESS
2764
static int
2765
eth_get_mac_address(struct i82544* p_i82544, char *addr)
2766
{
2767
#ifdef DEBUG
2768
    db_printf("eth_get_mac_address\n");
2769
#endif
2770
 
2771
    IF_BAD_82544( p_i82544 ) {
2772
#ifdef DEBUG
2773
        os_printf( "eth_get_mac_address : Bad device pointer %x\n",
2774
                   p_i82544 );
2775
#endif
2776
        return -1;
2777
    }
2778
 
2779
    memcpy( addr, (char *)(&p_i82544->mac_address[0]), 6 );
2780
    return 0;
2781
}
2782
#endif
2783
// ------------------------------------------------------------------------
2784
//
2785
//  Function : i82544_ioctl
2786
//
2787
// ------------------------------------------------------------------------
2788
static int
2789
i82544_ioctl(struct eth_drv_sc *sc, unsigned long key,
2790
             void *data, int data_length)
2791
{
2792
    struct i82544 *p_i82544;
2793
 
2794
#ifdef DEBUG
2795
    db_printf("i82544_ioctl\n");
2796
#endif
2797
 
2798
    p_i82544 = (struct i82544 *)sc->driver_private;
2799
 
2800
    IF_BAD_82544( p_i82544 ) {
2801
#ifdef DEBUG
2802
        os_printf( "i82544_ioctl/control: Bad device pointer %x\n", p_i82544 );
2803
#endif
2804
        return -1;
2805
    }
2806
 
2807
#ifdef ioctlDEBUG
2808
    db_printf( "i82544_ioctl: device eth%d at %x; key is 0x%x, data at %x[%d]\n",
2809
               p_i82544->index, p_i82544, key, data, data_length );
2810
#endif
2811
 
2812
    switch ( key ) {
2813
 
2814
#ifdef ETH_DRV_SET_MAC_ADDRESS
2815
    case ETH_DRV_SET_MAC_ADDRESS:
2816
        if ( 6 != data_length )
2817
            return -2;
2818
        return eth_set_mac_address( p_i82544, data, 1 /* do write eeprom */ );
2819
#endif
2820
 
2821
#ifdef ETH_DRV_GET_MAC_ADDRESS
2822
    case ETH_DRV_GET_MAC_ADDRESS:
2823
        return eth_get_mac_address( p_i82544, data );
2824
#endif
2825
 
2826
    default:
2827
        break;
2828
    }
2829
    return -1;
2830
}
2831
 
2832
// ------------------------------------------------------------------------
2833
 
2834
// EOF if_i82544.c

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.