source: rtems/c/src/libchip/network/elnk.c @ 641fb28

Last change on this file since 641fb28 was 641fb28, checked in by cvs2git <rtems-devel@…>, on 06/13/03 at 17:43:12

This commit was manufactured by cvs2svn to create branch 'rtems-4-6-branch'.

Cherrypick from master 2003-06-13 17:43:11 UTC Joel Sherrill <joel.sherrill@…> '2003-06-13 Greg Menke <gregory.menke@…>':

c/src/libchip/network/elnk.c
c/src/libchip/network/if_media.h
c/src/libchip/network/mii.h

  • Property mode set to 100644
File size: 89.3 KB
Line 
1/*
2 *  RTEMS driver for Etherlink based Ethernet Controllers
3 *
4 *  Copyright (C) 2003, Gregory Menke, NASA/GSFC
5 *
6 *  The license and distribution terms for this file may be
7 *  found in found in the file LICENSE in this distribution or at
8 *  http://www.OARcorp.com/rtems/license.html.
9 *
10 * elnk.c
11 *
12 *
13 */
14
15
16/*
17 * Portions of this driver are taken from the Freebsd if_xl.c driver,
18 * version "1.133 2003/03/19 01:48:14" and are covered by the license
19 * text included below that was taken verbatim from the original file.
20 * More particularly, all structures, variables, and #defines prefixed
21 * with XL_ or xl_, along with their associated comments were taken
22 * directly from the Freebsd driver and modified as required to suit the
23 * purposes of this one.  Additionally, much of the device setup &
24 * manipulation logic was also copied and modified to suit.  All types
25 * and functions beginning with elnk are either my own creations or were
26 * adapted from other RTEMS components, and regardless, are subject to
27 * the standard OAR licensing terms given in the comments at the top of
28 * this file.
29 *
30 * Greg Menke
31 */
32
33 /*
34 * Copyright (c) 1997, 1998, 1999
35 *      Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 *    notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 *    notice, this list of conditions and the following disclaimer in the
44 *    documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 *    must display the following acknowledgement:
47 *      This product includes software developed by Bill Paul.
48 * 4. Neither the name of the author nor the names of any co-contributors
49 *    may be used to endorse or promote products derived from this software
50 *    without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
56 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
59 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
60 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
61 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
62 * THE POSSIBILITY OF SUCH DAMAGE.
63 */
64
65#include <rtems.h>
66
67/*
68 *  This driver only supports architectures with the new style
69 *  exception processing.  The following checks try to keep this
70 *  from being compiled on systems which can't support this driver.
71 */
72
73#if defined(__i386__)
74#define ELNK_SUPPORTED
75#endif
76
77#if defined(__PPC__) && (defined(mpc604) || defined(mpc750) || defined(mpc603e))
78#define ELNK_SUPPORTED
79#endif
80
81/* #undef ELNK_SUPPORTED */
82
83
84#if defined(ELNK_SUPPORTED)
85#include <bsp.h>
86#if defined(__i386__)
87#include <pcibios.h>
88#endif
89#if defined(__PPC__)
90#include <bsp/pci.h>
91#include <libcpu/byteorder.h>
92#include <libcpu/io.h>
93#endif
94
95#include <stdlib.h>
96#include <stdio.h>
97#include <stdarg.h>
98#include <string.h>
99#include <rtems/error.h>
100#include <rtems/bspIo.h>
101#include <rtems/rtems_bsdnet.h>
102
103#include <sys/param.h>
104#include <sys/mbuf.h>
105
106#include <sys/socket.h>
107#include <sys/sockio.h>
108#include <net/if.h>
109#include <netinet/in.h>
110#include <netinet/if_ether.h>
111
112#include "if_media.h"
113#include "mii.h"
114
115#if defined(__i386__)
116#include <irq.h>
117#endif
118#if defined(__PPC)
119#include <bsp/irq.h>
120#endif
121
122#ifdef malloc
123#undef malloc
124#endif
125#ifdef free
126#undef free
127#endif
128
129#define ELNK_DEBUG
130
131
132#define DRIVER_PREFIX   "elnk"
133
134
135
136
137
138/*
139* These buffers allocated for each unit, so ensure
140*
141*   rtems_bsdnet_config.mbuf_bytecount
142*   rtems_bsdnet_config.mbuf_cluster_bytecount
143*
144* are adequately sized to provide enough clusters and mbufs for all the
145* units.  The default bsdnet configuration is sufficient for one unit,
146* but will be nearing exhaustion with 2 or more.  Although a little
147* expensive in memory, the following configuration should eliminate all
148* mbuf/cluster issues;
149*
150*   rtems_bsdnet_config.mbuf_bytecount           = 128*1024;
151*   rtems_bsdnet_config.mbuf_cluster_bytecount   = 256*1024;
152*
153* The default size in buffers of the rx & tx rings are given below.
154* This driver honors the rtems_bsdnet_ifconfig fields 'rbuf_count' and
155* 'xbuf_count', allowing the user to specify something else.
156*/
157
158#define RX_RING_SIZE 16 /* default number of receive buffers */
159#define TX_RING_SIZE 16 /* default number of transmit buffers */
160
161
162/*
163 * Number of boards supported by this driver
164 */
165#define NUM_UNITS       8
166
167/*
168 * Receive buffer size -- Allow for a full ethernet packet including CRC
169 */
170#define XL_PACKET_SIZE  1540
171
172
173/*
174** Events, one per unit.  The event is sent to the rx task from the isr
175** or from the stack to the tx task whenever a unit needs service.  The
176** rx/tx tasks identify the requesting unit(s) by their particular
177** events so only requesting units are serviced.
178*/
179
180static rtems_event_set unit_signals[NUM_UNITS]= { RTEMS_EVENT_1, 
181                                                  RTEMS_EVENT_2,
182                                                  RTEMS_EVENT_3, 
183                                                  RTEMS_EVENT_4,
184                                                  RTEMS_EVENT_5, 
185                                                  RTEMS_EVENT_6,
186                                                  RTEMS_EVENT_7, 
187                                                  RTEMS_EVENT_8 };
188
189
190#if defined(__PPC)
191#define phys_to_bus(address) ((unsigned int)((address)) + PCI_DRAM_OFFSET)
192#define bus_to_phys(address) ((unsigned int)((address)) - PCI_DRAM_OFFSET)
193#define CPU_CACHE_ALIGNMENT_FOR_BUFFER PPC_CACHE_ALIGNMENT
194#else
195extern void Wait_X_ms( unsigned int timeToWait );
196#define phys_to_bus(address) ((unsigned int) ((address)))
197#define bus_to_phys(address) ((unsigned int) ((address)))
198#define rtems_bsp_delay_in_bus_cycles(cycle) Wait_X_ms( cycle/100 )
199#define CPU_CACHE_ALIGNMENT_FOR_BUFFER PG_SIZE
200
201inline void st_le32(volatile unsigned32 *addr, unsigned32 value)
202{
203  *(addr)=value ;
204}
205
206inline unsigned32 ld_le32(volatile unsigned32 *addr)
207{
208  return(*addr);
209}
210#endif
211
212/* the actual duration waited in DELAY is not especially predictable,
213 * though it will be consistent.  It should not be relied upon for
214 * specific timing given the vague per-bsp, per-architecture
215 * implementation of the actual delay function.  It would probably be
216 * helpful to make this more accurate at some point...
217 */
218#define DELAY(n)        rtems_bsp_delay_in_bus_cycles( n*20  )
219
220
221
222
223/*
224 * Register layouts.
225 */
226#define XL_COMMAND              0x0E
227#define XL_STATUS               0x0E
228
229#define XL_TX_STATUS            0x1B
230#define XL_TX_FREE              0x1C
231#define XL_DMACTL               0x20
232#define XL_DOWNLIST_PTR         0x24
233#define XL_DOWN_POLL            0x2D /* 3c90xB only */
234#define XL_TX_FREETHRESH        0x2F
235#define XL_UPLIST_PTR           0x38
236#define XL_UPLIST_STATUS        0x30
237#define XL_UP_POLL              0x3D /* 3c90xB only */
238
239#define XL_PKTSTAT_UP_STALLED           0x00002000
240#define XL_PKTSTAT_UP_ERROR             0x00004000
241#define XL_PKTSTAT_UP_CMPLT             0x00008000
242
243#define XL_DMACTL_DN_CMPLT_REQ          0x00000002
244#define XL_DMACTL_DOWN_STALLED          0x00000004
245#define XL_DMACTL_UP_CMPLT              0x00000008
246#define XL_DMACTL_DOWN_CMPLT            0x00000010
247#define XL_DMACTL_UP_RX_EARLY           0x00000020
248#define XL_DMACTL_ARM_COUNTDOWN         0x00000040
249#define XL_DMACTL_DOWN_INPROG           0x00000080
250#define XL_DMACTL_COUNTER_SPEED         0x00000100
251#define XL_DMACTL_DOWNDOWN_MODE         0x00000200
252#define XL_DMACTL_TARGET_ABORT          0x40000000
253#define XL_DMACTL_MASTER_ABORT          0x80000000
254
255/*
256 * Command codes. Some command codes require that we wait for
257 * the CMD_BUSY flag to clear. Those codes are marked as 'mustwait.'
258 */
259#define XL_CMD_RESET            0x0000  /* mustwait */
260#define XL_CMD_WINSEL           0x0800
261#define XL_CMD_COAX_START       0x1000
262#define XL_CMD_RX_DISABLE       0x1800
263#define XL_CMD_RX_ENABLE        0x2000
264#define XL_CMD_RX_RESET         0x2800  /* mustwait */
265#define XL_CMD_UP_STALL         0x3000  /* mustwait */
266#define XL_CMD_UP_UNSTALL       0x3001
267#define XL_CMD_DOWN_STALL       0x3002  /* mustwait */
268#define XL_CMD_DOWN_UNSTALL     0x3003
269#define XL_CMD_RX_DISCARD       0x4000
270#define XL_CMD_TX_ENABLE        0x4800
271#define XL_CMD_TX_DISABLE       0x5000
272#define XL_CMD_TX_RESET         0x5800  /* mustwait */
273#define XL_CMD_INTR_FAKE        0x6000
274#define XL_CMD_INTR_ACK         0x6800
275#define XL_CMD_INTR_ENB         0x7000
276#define XL_CMD_STAT_ENB         0x7800
277#define XL_CMD_RX_SET_FILT      0x8000
278#define XL_CMD_RX_SET_THRESH    0x8800
279#define XL_CMD_TX_SET_THRESH    0x9000
280#define XL_CMD_TX_SET_START     0x9800
281#define XL_CMD_DMA_UP           0xA000
282#define XL_CMD_DMA_STOP         0xA001
283#define XL_CMD_STATS_ENABLE     0xA800
284#define XL_CMD_STATS_DISABLE    0xB000
285#define XL_CMD_COAX_STOP        0xB800
286
287#define XL_CMD_SET_TX_RECLAIM   0xC000 /* 3c905B only */
288#define XL_CMD_RX_SET_HASH      0xC800 /* 3c905B only */
289
290#define XL_HASH_SET             0x0400
291#define XL_HASHFILT_SIZE        256
292
293/*
294 * status codes
295 * Note that bits 15 to 13 indicate the currently visible register window
296 * which may be anything from 0 to 7.
297 */
298#define XL_STAT_INTLATCH        0x0001  /* 0 */
299#define XL_STAT_ADFAIL          0x0002  /* 1 */
300#define XL_STAT_TX_COMPLETE     0x0004  /* 2 */
301#define XL_STAT_TX_AVAIL        0x0008  /* 3 first generation */
302#define XL_STAT_RX_COMPLETE     0x0010  /* 4 */
303#define XL_STAT_RX_EARLY        0x0020  /* 5 */
304#define XL_STAT_INTREQ          0x0040  /* 6 */
305#define XL_STAT_STATSOFLOW      0x0080  /* 7 */
306#define XL_STAT_DMADONE         0x0100  /* 8 first generation */
307#define XL_STAT_LINKSTAT        0x0100  /* 8 3c509B */
308#define XL_STAT_DOWN_COMPLETE   0x0200  /* 9 */
309#define XL_STAT_UP_COMPLETE     0x0400  /* 10 */
310#define XL_STAT_DMABUSY         0x0800  /* 11 first generation */
311#define XL_STAT_CMDBUSY         0x1000  /* 12 */
312
313/*
314 * Interrupts we normally want enabled.
315 */
316#define XL_INTRS        \
317        (XL_STAT_UP_COMPLETE|XL_STAT_STATSOFLOW|XL_STAT_ADFAIL| \
318         XL_STAT_DOWN_COMPLETE|XL_STAT_TX_COMPLETE|XL_STAT_INTLATCH)
319
320
321
322/*
323 * General constants that are fun to know.
324 *
325 * 3Com PCI vendor ID
326 */
327#define TC_VENDORID             0x10B7
328
329/*
330 * 3Com chip device IDs.
331 */
332#define TC_DEVICEID_BOOMERANG_10BT              0x9000
333#define TC_DEVICEID_BOOMERANG_10BT_COMBO        0x9001
334#define TC_DEVICEID_BOOMERANG_10_100BT          0x9050
335#define TC_DEVICEID_BOOMERANG_100BT4            0x9051
336#define TC_DEVICEID_KRAKATOA_10BT               0x9004
337#define TC_DEVICEID_KRAKATOA_10BT_COMBO         0x9005
338#define TC_DEVICEID_KRAKATOA_10BT_TPC           0x9006
339#define TC_DEVICEID_CYCLONE_10FL                0x900A
340#define TC_DEVICEID_HURRICANE_10_100BT          0x9055
341#define TC_DEVICEID_CYCLONE_10_100BT4           0x9056
342#define TC_DEVICEID_CYCLONE_10_100_COMBO        0x9058
343#define TC_DEVICEID_CYCLONE_10_100FX            0x905A
344#define TC_DEVICEID_TORNADO_10_100BT            0x9200
345#define TC_DEVICEID_TORNADO_10_100BT_920B       0x9201
346#define TC_DEVICEID_HURRICANE_10_100BT_SERV     0x9800
347#define TC_DEVICEID_TORNADO_10_100BT_SERV       0x9805
348#define TC_DEVICEID_HURRICANE_SOHO100TX         0x7646
349#define TC_DEVICEID_TORNADO_HOMECONNECT         0x4500
350#define TC_DEVICEID_HURRICANE_555               0x5055
351#define TC_DEVICEID_HURRICANE_556               0x6055
352#define TC_DEVICEID_HURRICANE_556B              0x6056
353#define TC_DEVICEID_HURRICANE_575A              0x5057
354#define TC_DEVICEID_HURRICANE_575B              0x5157
355#define TC_DEVICEID_HURRICANE_575C              0x5257
356#define TC_DEVICEID_HURRICANE_656               0x6560
357#define TC_DEVICEID_HURRICANE_656B              0x6562
358#define TC_DEVICEID_TORNADO_656C                0x6564
359
360
361
362#define XL_RXSTAT_LENMASK       0x00001FFF
363#define XL_RXSTAT_UP_ERROR      0x00004000
364#define XL_RXSTAT_UP_CMPLT      0x00008000
365#define XL_RXSTAT_UP_OVERRUN    0x00010000
366#define XL_RXSTAT_RUNT          0x00020000
367#define XL_RXSTAT_ALIGN         0x00040000
368#define XL_RXSTAT_CRC           0x00080000
369#define XL_RXSTAT_OVERSIZE      0x00100000
370#define XL_RXSTAT_DRIBBLE       0x00800000
371#define XL_RXSTAT_UP_OFLOW      0x01000000
372#define XL_RXSTAT_IPCKERR       0x02000000      /* 3c905B only */
373#define XL_RXSTAT_TCPCKERR      0x04000000      /* 3c905B only */
374#define XL_RXSTAT_UDPCKERR      0x08000000      /* 3c905B only */
375#define XL_RXSTAT_BUFEN         0x10000000      /* 3c905B only */
376#define XL_RXSTAT_IPCKOK        0x20000000      /* 3c905B only */
377#define XL_RXSTAT_TCPCOK        0x40000000      /* 3c905B only */
378#define XL_RXSTAT_UDPCKOK       0x80000000      /* 3c905B only */
379
380#define XL_TXSTAT_LENMASK       0x00001FFF
381#define XL_TXSTAT_CRCDIS        0x00002000
382#define XL_TXSTAT_TX_INTR       0x00008000
383#define XL_TXSTAT_DL_COMPLETE   0x00010000
384#define XL_TXSTAT_IPCKSUM       0x02000000      /* 3c905B only */
385#define XL_TXSTAT_TCPCKSUM      0x04000000      /* 3c905B only */
386#define XL_TXSTAT_UDPCKSUM      0x08000000      /* 3c905B only */
387#define XL_TXSTAT_RND_DEFEAT    0x10000000      /* 3c905B only */
388#define XL_TXSTAT_EMPTY         0x20000000      /* 3c905B only */
389#define XL_TXSTAT_DL_INTR       0x80000000
390
391
392#define XL_FLAG_FUNCREG                 0x0001
393#define XL_FLAG_PHYOK                   0x0002
394#define XL_FLAG_EEPROM_OFFSET_30        0x0004
395#define XL_FLAG_WEIRDRESET              0x0008
396#define XL_FLAG_8BITROM                 0x0010
397#define XL_FLAG_INVERT_LED_PWR          0x0020
398#define XL_FLAG_INVERT_MII_PWR          0x0040
399#define XL_FLAG_NO_XCVR_PWR             0x0080
400#define XL_FLAG_USE_MMIO                0x0100
401
402
403
404#define XL_EE_READ      0x0080  /* read, 5 bit address */
405#define XL_EE_WRITE     0x0040  /* write, 5 bit address */
406#define XL_EE_ERASE     0x00c0  /* erase, 5 bit address */
407#define XL_EE_EWEN      0x0030  /* erase, no data needed */
408#define XL_EE_8BIT_READ 0x0200  /* read, 8 bit address */
409#define XL_EE_BUSY      0x8000
410
411#define XL_EE_EADDR0    0x00    /* station address, first word */
412#define XL_EE_EADDR1    0x01    /* station address, next word, */
413#define XL_EE_EADDR2    0x02    /* station address, last word */
414#define XL_EE_PRODID    0x03    /* product ID code */
415#define XL_EE_MDATA_DATE        0x04    /* manufacturing data, date */
416#define XL_EE_MDATA_DIV         0x05    /* manufacturing data, division */
417#define XL_EE_MDATA_PCODE       0x06    /* manufacturing data, product code */
418#define XL_EE_MFG_ID    0x07
419#define XL_EE_PCI_PARM  0x08
420#define XL_EE_ROM_ONFO  0x09
421#define XL_EE_OEM_ADR0  0x0A
422#define XL_EE_OEM_ADR1  0x0B
423#define XL_EE_OEM_ADR2  0x0C
424#define XL_EE_SOFTINFO1 0x0D
425#define XL_EE_COMPAT    0x0E
426#define XL_EE_SOFTINFO2 0x0F
427#define XL_EE_CAPS      0x10    /* capabilities word */
428#define XL_EE_RSVD0     0x11
429#define XL_EE_ICFG_0    0x12
430#define XL_EE_ICFG_1    0x13
431#define XL_EE_RSVD1     0x14
432#define XL_EE_SOFTINFO3 0x15
433#define XL_EE_RSVD_2    0x16
434
435/*
436 * Bits in the capabilities word
437 */
438#define XL_CAPS_PNP             0x0001
439#define XL_CAPS_FULL_DUPLEX     0x0002
440#define XL_CAPS_LARGE_PKTS      0x0004
441#define XL_CAPS_SLAVE_DMA       0x0008
442#define XL_CAPS_SECOND_DMA      0x0010
443#define XL_CAPS_FULL_BM         0x0020
444#define XL_CAPS_FRAG_BM         0x0040
445#define XL_CAPS_CRC_PASSTHRU    0x0080
446#define XL_CAPS_TXDONE          0x0100
447#define XL_CAPS_NO_TXLENGTH     0x0200
448#define XL_CAPS_RX_REPEAT       0x0400
449#define XL_CAPS_SNOOPING        0x0800
450#define XL_CAPS_100MBPS         0x1000
451#define XL_CAPS_PWRMGMT         0x2000
452
453
454
455/*
456 * Window 0 registers
457 */
458#define XL_W0_EE_DATA           0x0C
459#define XL_W0_EE_CMD            0x0A
460#define XL_W0_RSRC_CFG          0x08
461#define XL_W0_ADDR_CFG          0x06
462#define XL_W0_CFG_CTRL          0x04
463
464#define XL_W0_PROD_ID           0x02
465#define XL_W0_MFG_ID            0x00
466
467/*
468 * Window 1
469 */
470
471#define XL_W1_TX_FIFO           0x10
472
473#define XL_W1_FREE_TX           0x0C
474#define XL_W1_TX_STATUS         0x0B
475#define XL_W1_TX_TIMER          0x0A
476#define XL_W1_RX_STATUS         0x08
477#define XL_W1_RX_FIFO           0x00
478
479/*
480 * RX status codes
481 */
482#define XL_RXSTATUS_OVERRUN     0x01
483#define XL_RXSTATUS_RUNT        0x02
484#define XL_RXSTATUS_ALIGN       0x04
485#define XL_RXSTATUS_CRC         0x08
486#define XL_RXSTATUS_OVERSIZE    0x10
487#define XL_RXSTATUS_DRIBBLE     0x20
488
489/*
490 * TX status codes
491 */
492#define XL_TXSTATUS_RECLAIM     0x02 /* 3c905B only */
493#define XL_TXSTATUS_OVERFLOW    0x04
494#define XL_TXSTATUS_MAXCOLS     0x08
495#define XL_TXSTATUS_UNDERRUN    0x10
496#define XL_TXSTATUS_JABBER      0x20
497#define XL_TXSTATUS_INTREQ      0x40
498#define XL_TXSTATUS_COMPLETE    0x80
499
500/*
501 * Window 2
502 */
503#define XL_W2_RESET_OPTIONS     0x0C    /* 3c905B only */
504#define XL_W2_STATION_MASK_HI   0x0A
505#define XL_W2_STATION_MASK_MID  0x08
506#define XL_W2_STATION_MASK_LO   0x06
507#define XL_W2_STATION_ADDR_HI   0x04
508#define XL_W2_STATION_ADDR_MID  0x02
509#define XL_W2_STATION_ADDR_LO   0x00
510
511#define XL_RESETOPT_FEATUREMASK 0x0001|0x0002|0x004
512#define XL_RESETOPT_D3RESETDIS  0x0008
513#define XL_RESETOPT_DISADVFD    0x0010
514#define XL_RESETOPT_DISADV100   0x0020
515#define XL_RESETOPT_DISAUTONEG  0x0040
516#define XL_RESETOPT_DEBUGMODE   0x0080
517#define XL_RESETOPT_FASTAUTO    0x0100
518#define XL_RESETOPT_FASTEE      0x0200
519#define XL_RESETOPT_FORCEDCONF  0x0400
520#define XL_RESETOPT_TESTPDTPDR  0x0800
521#define XL_RESETOPT_TEST100TX   0x1000
522#define XL_RESETOPT_TEST100RX   0x2000
523
524#define XL_RESETOPT_INVERT_LED  0x0010
525#define XL_RESETOPT_INVERT_MII  0x4000
526
527/*
528 * Window 3 (fifo management)
529 */
530#define XL_W3_INTERNAL_CFG      0x00
531#define XL_W3_MAXPKTSIZE        0x04    /* 3c905B only */
532#define XL_W3_RESET_OPT         0x08
533#define XL_W3_FREE_TX           0x0C
534#define XL_W3_FREE_RX           0x0A
535#define XL_W3_MAC_CTRL          0x06
536
537#define XL_ICFG_CONNECTOR_MASK  0x00F00000
538#define XL_ICFG_CONNECTOR_BITS  20
539
540#define XL_ICFG_RAMSIZE_MASK    0x00000007
541#define XL_ICFG_RAMWIDTH        0x00000008
542#define XL_ICFG_ROMSIZE_MASK    (0x00000040|0x00000080)
543#define XL_ICFG_DISABLE_BASSD   0x00000100
544#define XL_ICFG_RAMLOC          0x00000200
545#define XL_ICFG_RAMPART         (0x00010000|0x00020000)
546#define XL_ICFG_XCVRSEL         (0x00100000|0x00200000|0x00400000)
547#define XL_ICFG_AUTOSEL         0x01000000
548
549#define XL_XCVR_10BT            0x00
550#define XL_XCVR_AUI             0x01
551#define XL_XCVR_RSVD_0          0x02
552#define XL_XCVR_COAX            0x03
553#define XL_XCVR_100BTX          0x04
554#define XL_XCVR_100BFX          0x05
555#define XL_XCVR_MII             0x06
556#define XL_XCVR_RSVD_1          0x07
557#define XL_XCVR_AUTO            0x08    /* 3c905B only */
558
559#define XL_MACCTRL_DEFER_EXT_END        0x0001
560#define XL_MACCTRL_DEFER_0              0x0002
561#define XL_MACCTRL_DEFER_1              0x0004
562#define XL_MACCTRL_DEFER_2              0x0008
563#define XL_MACCTRL_DEFER_3              0x0010
564#define XL_MACCTRL_DUPLEX               0x0020
565#define XL_MACCTRL_ALLOW_LARGE_PACK     0x0040
566#define XL_MACCTRL_EXTEND_AFTER_COL     0x0080 (3c905B only)
567#define XL_MACCTRL_FLOW_CONTROL_ENB     0x0100 (3c905B only)
568#define XL_MACCTRL_VLT_END              0x0200 (3c905B only)
569
570/*
571 * The 'reset options' register contains power-on reset values
572 * loaded from the EEPROM. This includes the supported media
573 * types on the card. It is also known as the media options register.
574 */
575#define XL_W3_MEDIA_OPT         0x08
576
577#define XL_MEDIAOPT_BT4         0x0001  /* MII */
578#define XL_MEDIAOPT_BTX         0x0002  /* on-chip */
579#define XL_MEDIAOPT_BFX         0x0004  /* on-chip */
580#define XL_MEDIAOPT_BT          0x0008  /* on-chip */
581#define XL_MEDIAOPT_BNC         0x0010  /* on-chip */
582#define XL_MEDIAOPT_AUI         0x0020  /* on-chip */
583#define XL_MEDIAOPT_MII         0x0040  /* MII */
584#define XL_MEDIAOPT_VCO         0x0100  /* 1st gen chip only */
585
586#define XL_MEDIAOPT_10FL        0x0100  /* 3x905B only, on-chip */
587#define XL_MEDIAOPT_MASK        0x01FF
588
589/*
590 * Window 4 (diagnostics)
591 */
592#define XL_W4_UPPERBYTESOK      0x0D
593#define XL_W4_BADSSD            0x0C
594#define XL_W4_MEDIA_STATUS      0x0A
595#define XL_W4_PHY_MGMT          0x08
596#define XL_W4_NET_DIAG          0x06
597#define XL_W4_FIFO_DIAG         0x04
598#define XL_W4_VCO_DIAG          0x02
599
600#define XL_W4_CTRLR_STAT        0x08
601#define XL_W4_TX_DIAG           0x00
602
603#define XL_MII_CLK              0x01
604#define XL_MII_DATA             0x02
605#define XL_MII_DIR              0x04
606
607#define XL_MEDIA_SQE            0x0008
608#define XL_MEDIA_10TP           0x00C0
609#define XL_MEDIA_LNK            0x0080
610#define XL_MEDIA_LNKBEAT        0x0800
611
612#define XL_MEDIASTAT_CRCSTRIP   0x0004
613#define XL_MEDIASTAT_SQEENB     0x0008
614#define XL_MEDIASTAT_COLDET     0x0010
615#define XL_MEDIASTAT_CARRIER    0x0020
616#define XL_MEDIASTAT_JABGUARD   0x0040
617#define XL_MEDIASTAT_LINKBEAT   0x0080
618#define XL_MEDIASTAT_JABDETECT  0x0200
619#define XL_MEDIASTAT_POLREVERS  0x0400
620#define XL_MEDIASTAT_LINKDETECT 0x0800
621#define XL_MEDIASTAT_TXINPROG   0x1000
622#define XL_MEDIASTAT_DCENB      0x4000
623#define XL_MEDIASTAT_AUIDIS     0x8000
624
625#define XL_NETDIAG_TEST_LOWVOLT         0x0001
626#define XL_NETDIAG_ASIC_REVMASK         (0x0002|0x0004|0x0008|0x0010|0x0020)
627#define XL_NETDIAG_UPPER_BYTES_ENABLE   0x0040
628#define XL_NETDIAG_STATS_ENABLED        0x0080
629#define XL_NETDIAG_TX_FATALERR          0x0100
630#define XL_NETDIAG_TRANSMITTING         0x0200
631#define XL_NETDIAG_RX_ENABLED           0x0400
632#define XL_NETDIAG_TX_ENABLED           0x0800
633#define XL_NETDIAG_FIFO_LOOPBACK        0x1000
634#define XL_NETDIAG_MAC_LOOPBACK         0x2000
635#define XL_NETDIAG_ENDEC_LOOPBACK       0x4000
636#define XL_NETDIAG_EXTERNAL_LOOP        0x8000
637
638/*
639 * Window 5
640 */
641#define XL_W5_STAT_ENB          0x0C
642#define XL_W5_INTR_ENB          0x0A
643#define XL_W5_RECLAIM_THRESH    0x09    /* 3c905B only */
644#define XL_W5_RX_FILTER         0x08
645#define XL_W5_RX_EARLYTHRESH    0x06
646#define XL_W5_TX_AVAILTHRESH    0x02
647#define XL_W5_TX_STARTTHRESH    0x00
648
649/*
650 * RX filter bits
651 */
652#define XL_RXFILTER_INDIVIDUAL  0x01
653#define XL_RXFILTER_ALLMULTI    0x02
654#define XL_RXFILTER_BROADCAST   0x04
655#define XL_RXFILTER_ALLFRAMES   0x08
656#define XL_RXFILTER_MULTIHASH   0x10 /* 3c905B only */
657
658/*
659 * Window 6 (stats)
660 */
661#define XL_W6_TX_BYTES_OK       0x0C
662#define XL_W6_RX_BYTES_OK       0x0A
663#define XL_W6_UPPER_FRAMES_OK   0x09
664#define XL_W6_DEFERRED          0x08
665#define XL_W6_RX_OK             0x07
666#define XL_W6_TX_OK             0x06
667#define XL_W6_RX_OVERRUN        0x05
668#define XL_W6_COL_LATE          0x04
669#define XL_W6_COL_SINGLE        0x03
670#define XL_W6_COL_MULTIPLE      0x02
671#define XL_W6_SQE_ERRORS        0x01
672#define XL_W6_CARRIER_LOST      0x00
673
674/*
675 * Window 7 (bus master control)
676 */
677#define XL_W7_BM_ADDR           0x00
678#define XL_W7_BM_LEN            0x06
679#define XL_W7_BM_STATUS         0x0B
680#define XL_W7_BM_TIMEr          0x0A
681
682/*
683 * bus master control registers
684 */
685#define XL_BM_PKTSTAT           0x20
686#define XL_BM_DOWNLISTPTR       0x24
687#define XL_BM_FRAGADDR          0x28
688#define XL_BM_FRAGLEN           0x2C
689#define XL_BM_TXFREETHRESH      0x2F
690#define XL_BM_UPPKTSTAT         0x30
691#define XL_BM_UPLISTPTR         0x38
692
693
694
695
696
697
698
699
700struct xl_mii_frame {
701        u_int8_t                mii_stdelim;
702        u_int8_t                mii_opcode;
703        u_int8_t                mii_phyaddr;
704        u_int8_t                mii_regaddr;
705        u_int8_t                mii_turnaround;
706        u_int16_t               mii_data;
707};
708
709/*
710 * MII constants
711 */
712#define XL_MII_STARTDELIM       0x01
713#define XL_MII_READOP           0x02
714#define XL_MII_WRITEOP          0x01
715#define XL_MII_TURNAROUND       0x02
716
717
718
719/*
720 * The 3C905B adapters implement a few features that we want to
721 * take advantage of, namely the multicast hash filter. With older
722 * chips, you only have the option of turning on reception of all
723 * multicast frames, which is kind of lame.
724 *
725 * We also use this to decide on a transmit strategy. For the 3c90xB
726 * cards, we can use polled descriptor mode, which reduces CPU overhead.
727 */
728#define XL_TYPE_905B    1
729#define XL_TYPE_90X     2
730
731#define XL_FLAG_FUNCREG                 0x0001
732#define XL_FLAG_PHYOK                   0x0002
733#define XL_FLAG_EEPROM_OFFSET_30        0x0004
734#define XL_FLAG_WEIRDRESET              0x0008
735#define XL_FLAG_8BITROM                 0x0010
736#define XL_FLAG_INVERT_LED_PWR          0x0020
737#define XL_FLAG_INVERT_MII_PWR          0x0040
738#define XL_FLAG_NO_XCVR_PWR             0x0080
739#define XL_FLAG_USE_MMIO                0x0100
740
741#define XL_NO_XCVR_PWR_MAGICBITS        0x0900
742
743
744#define XL_MIN_FRAMELEN         60
745
746#define XL_LAST_FRAG            0x80000000
747
748
749
750
751
752
753
754struct xl_stats
755{
756      u_int8_t          xl_carrier_lost;
757      u_int8_t          xl_sqe_errs;
758      u_int8_t          xl_tx_multi_collision;
759      u_int8_t          xl_tx_single_collision;
760      u_int8_t          xl_tx_late_collision;
761      u_int8_t          xl_rx_overrun;
762      u_int8_t          xl_tx_frames_ok;
763      u_int8_t          xl_rx_frames_ok;
764      u_int8_t          xl_tx_deferred;
765      u_int8_t          xl_upper_frames_ok;
766      u_int16_t         xl_rx_bytes_ok;
767      u_int16_t         xl_tx_bytes_ok;
768
769      u_int8_t          xl_upper_bytes_ok;
770      u_int8_t          xl_badssd;
771
772      u_int16_t         intstatus;
773      u_int16_t         rxstatus;
774      u_int8_t          txstatus;
775      u_int16_t         mediastatus;
776
777      u_int16_t         miianr, miipar, miistatus, miicmd;
778
779      u_int32_t         device_interrupts;
780
781      u_int16_t         smbstatus;
782};
783
784
785
786struct xl_type
787{
788      u_int16_t         xl_vid;
789      u_int16_t         xl_did;
790      char              *xl_name;
791};
792
793
794
795/*
796 * Various supported device vendors/types and their names.
797 */
798static struct xl_type xl_devs[] = {
799        { TC_VENDORID, TC_DEVICEID_BOOMERANG_10BT,
800                "3Com 3c900-TPO Etherlink XL" },
801        { TC_VENDORID, TC_DEVICEID_BOOMERANG_10BT_COMBO,
802                "3Com 3c900-COMBO Etherlink XL" },
803        { TC_VENDORID, TC_DEVICEID_BOOMERANG_10_100BT,
804                "3Com 3c905-TX Fast Etherlink XL" },
805        { TC_VENDORID, TC_DEVICEID_BOOMERANG_100BT4,
806                "3Com 3c905-T4 Fast Etherlink XL" },
807        { TC_VENDORID, TC_DEVICEID_KRAKATOA_10BT,
808                "3Com 3c900B-TPO Etherlink XL" },
809        { TC_VENDORID, TC_DEVICEID_KRAKATOA_10BT_COMBO,
810                "3Com 3c900B-COMBO Etherlink XL" },
811        { TC_VENDORID, TC_DEVICEID_KRAKATOA_10BT_TPC,
812                "3Com 3c900B-TPC Etherlink XL" },
813        { TC_VENDORID, TC_DEVICEID_CYCLONE_10FL,
814                "3Com 3c900B-FL Etherlink XL" },
815        { TC_VENDORID, TC_DEVICEID_HURRICANE_10_100BT,
816                "3Com 3c905B-TX Fast Etherlink XL" },
817        { TC_VENDORID, TC_DEVICEID_CYCLONE_10_100BT4,
818                "3Com 3c905B-T4 Fast Etherlink XL" },
819        { TC_VENDORID, TC_DEVICEID_CYCLONE_10_100FX,
820                "3Com 3c905B-FX/SC Fast Etherlink XL" },
821        { TC_VENDORID, TC_DEVICEID_CYCLONE_10_100_COMBO,
822                "3Com 3c905B-COMBO Fast Etherlink XL" },
823        { TC_VENDORID, TC_DEVICEID_TORNADO_10_100BT,
824                "3Com 3c905C-TX Fast Etherlink XL" },
825        { TC_VENDORID, TC_DEVICEID_TORNADO_10_100BT_920B,
826                "3Com 3c920B-EMB Integrated Fast Etherlink XL" },
827        { TC_VENDORID, TC_DEVICEID_HURRICANE_10_100BT_SERV,
828                "3Com 3c980 Fast Etherlink XL" },
829        { TC_VENDORID, TC_DEVICEID_TORNADO_10_100BT_SERV,
830                "3Com 3c980C Fast Etherlink XL" },
831        { TC_VENDORID, TC_DEVICEID_HURRICANE_SOHO100TX,
832                "3Com 3cSOHO100-TX OfficeConnect" },
833        { TC_VENDORID, TC_DEVICEID_TORNADO_HOMECONNECT,
834                "3Com 3c450-TX HomeConnect" },
835        { TC_VENDORID, TC_DEVICEID_HURRICANE_555,
836                "3Com 3c555 Fast Etherlink XL" },
837        { TC_VENDORID, TC_DEVICEID_HURRICANE_556,
838                "3Com 3c556 Fast Etherlink XL" },
839        { TC_VENDORID, TC_DEVICEID_HURRICANE_556B,
840                "3Com 3c556B Fast Etherlink XL" },
841        { TC_VENDORID, TC_DEVICEID_HURRICANE_575A,
842                "3Com 3c575TX Fast Etherlink XL" },
843        { TC_VENDORID, TC_DEVICEID_HURRICANE_575B,
844                "3Com 3c575B Fast Etherlink XL" },
845        { TC_VENDORID, TC_DEVICEID_HURRICANE_575C,
846                "3Com 3c575C Fast Etherlink XL" },
847        { TC_VENDORID, TC_DEVICEID_HURRICANE_656,
848                "3Com 3c656 Fast Etherlink XL" },
849        { TC_VENDORID, TC_DEVICEID_HURRICANE_656B,
850                "3Com 3c656B Fast Etherlink XL" },
851        { TC_VENDORID, TC_DEVICEID_TORNADO_656C,
852                "3Com 3c656C Fast Etherlink XL" },
853        { 0, 0, NULL }
854};
855
856
857
858
859
860
861#define CSR_WRITE_4(sc, reg, val)       outl( val, sc->ioaddr + reg)
862#define CSR_WRITE_2(sc, reg, val)       outw( val, sc->ioaddr + reg)
863#define CSR_WRITE_1(sc, reg, val)       outb( val, sc->ioaddr + reg)
864
865#define CSR_READ_4(sc, reg)     inl(sc->ioaddr + reg)
866#define CSR_READ_2(sc, reg)     inw(sc->ioaddr + reg)
867#define CSR_READ_1(sc, reg)     inb(sc->ioaddr + reg)
868
869#define XL_SEL_WIN(x)           CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_WINSEL | x)
870
871#define XL_TIMEOUT              1000
872
873
874
875
876
877/* message descriptor entry, ensure this struct is aligned to 8 bytes */
878struct MD
879{
880      /* used by hardware */
881      volatile unsigned32 next;
882      volatile unsigned32 status;
883      volatile unsigned32 addr;
884      volatile unsigned32 length;
885      /* used by software */
886      struct mbuf       *mbuf;        /* scratch variable used in the tx ring */
887      struct MD         *next_md;
888} __attribute__ ((packed));
889
890
891
892
893
894
895
896/*
897 * Per-device data
898 */
899struct elnk_softc
900{
901      struct arpcom             arpcom;
902
903      rtems_irq_connect_data    irqInfo;
904      rtems_event_set           ioevent;
905      unsigned int              ioaddr;
906
907      volatile unsigned char    *bufferBase, *ringBase;
908
909      struct MD                 *curr_rx_md, *last_tx_md, *last_txchain_head;
910
911      rtems_id                  stat_timer_id;
912      unsigned32                stats_update_ticks;
913
914      struct xl_stats           xl_stats;
915
916      u_int8_t                  xl_unit;        /* interface number */
917      u_int8_t                  xl_type;
918      int                       xl_flags;
919      u_int16_t                 xl_media;
920      u_int16_t                 xl_caps;
921      u_int32_t                 xl_xcvr;
922      u_int8_t                  xl_stats_no_timeout;
923      u_int16_t                 xl_tx_thresh;
924
925      int                       tx_idle;
926
927      unsigned short            vendorID, deviceID;
928      int                       acceptBroadcast;
929      int                       numTxbuffers, numRxbuffers;
930};
931
932static struct elnk_softc elnk_softc[NUM_UNITS];
933static rtems_id rxDaemonTid;
934static rtems_id txDaemonTid;
935
936
937
938
939
940
941
942
943
944
945
946
947/*
948 * Murphy's law says that it's possible the chip can wedge and
949 * the 'command in progress' bit may never clear. Hence, we wait
950 * only a finite amount of time to avoid getting caught in an
951 * infinite loop. Normally this delay routine would be a macro,
952 * but it isn't called during normal operation so we can afford
953 * to make it a function.
954 */
955static void
956xl_wait(sc)
957   struct elnk_softc            *sc;
958{
959   register int         i;
960
961   for (i = 0; i < XL_TIMEOUT; i++) {
962      if (!(CSR_READ_2(sc, XL_STATUS) & XL_STAT_CMDBUSY))
963         break;
964   }
965
966   if (i == XL_TIMEOUT)
967      printk("ek%d: command never completed!\n", sc->xl_unit);
968
969   return;
970}
971
972
973
974
975
976
977/*
978 * MII access routines are provided for adapters with external
979 * PHYs (3c905-TX, 3c905-T4, 3c905B-T4) and those with built-in
980 * autoneg logic that's faked up to look like a PHY (3c905B-TX).
981 * Note: if you don't perform the MDIO operations just right,
982 * it's possible to end up with code that works correctly with
983 * some chips/CPUs/processor speeds/bus speeds/etc but not
984 * with others.
985 */
986#define MII_SET(x)                                      \
987        CSR_WRITE_2(sc, XL_W4_PHY_MGMT,                 \
988                CSR_READ_2(sc, XL_W4_PHY_MGMT) | (x))
989
990#define MII_CLR(x)                                      \
991        CSR_WRITE_2(sc, XL_W4_PHY_MGMT,                 \
992                CSR_READ_2(sc, XL_W4_PHY_MGMT) & ~(x))
993
994/*
995 * Sync the PHYs by setting data bit and strobing the clock 32 times.
996 */
997static void
998xl_mii_sync(sc)
999   struct elnk_softc            *sc;
1000{
1001   register int         i;
1002
1003   XL_SEL_WIN(4);
1004   MII_SET(XL_MII_DIR|XL_MII_DATA);
1005
1006   for (i = 0; i < 32; i++) {
1007      MII_SET(XL_MII_CLK);
1008      MII_SET(XL_MII_DATA);
1009      MII_CLR(XL_MII_CLK);
1010      MII_SET(XL_MII_DATA);
1011   }
1012
1013   return;
1014}
1015
1016/*
1017 * Clock a series of bits through the MII.
1018 */
1019static void
1020xl_mii_send(sc, bits, cnt)
1021   struct elnk_softc            *sc;
1022   u_int32_t            bits;
1023   int                  cnt;
1024{
1025   int                  i;
1026
1027   XL_SEL_WIN(4);
1028   MII_CLR(XL_MII_CLK);
1029
1030   for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
1031      if (bits & i) {
1032         MII_SET(XL_MII_DATA);
1033      } else {
1034         MII_CLR(XL_MII_DATA);
1035      }
1036      MII_CLR(XL_MII_CLK);
1037      MII_SET(XL_MII_CLK);
1038   }
1039}
1040
1041/*
1042 * Read an PHY register through the MII.
1043 */
1044static int
1045xl_mii_readreg(sc, frame)
1046   struct elnk_softc            *sc;
1047   struct xl_mii_frame  *frame;
1048       
1049{
1050   int                  i, ack;
1051
1052   /*
1053    * Set up frame for RX.
1054    */
1055   frame->mii_stdelim = XL_MII_STARTDELIM;
1056   frame->mii_opcode = XL_MII_READOP;
1057   frame->mii_turnaround = 0;
1058   frame->mii_data = 0;
1059       
1060   /*
1061    * Select register window 4.
1062    */
1063
1064   XL_SEL_WIN(4);
1065
1066   CSR_WRITE_2(sc, XL_W4_PHY_MGMT, 0);
1067   /*
1068    * Turn on data xmit.
1069    */
1070   MII_SET(XL_MII_DIR);
1071
1072   xl_mii_sync(sc);
1073
1074   /*
1075    * Send command/address info.
1076    */
1077   xl_mii_send(sc, frame->mii_stdelim, 2);
1078   xl_mii_send(sc, frame->mii_opcode, 2);
1079   xl_mii_send(sc, frame->mii_phyaddr, 5);
1080   xl_mii_send(sc, frame->mii_regaddr, 5);
1081
1082   /* Idle bit */
1083   MII_CLR((XL_MII_CLK|XL_MII_DATA));
1084   MII_SET(XL_MII_CLK);
1085
1086   /* Turn off xmit. */
1087   MII_CLR(XL_MII_DIR);
1088
1089   /* Check for ack */
1090   MII_CLR(XL_MII_CLK);
1091   ack = CSR_READ_2(sc, XL_W4_PHY_MGMT) & XL_MII_DATA;
1092   MII_SET(XL_MII_CLK);
1093
1094   /*
1095    * Now try reading data bits. If the ack failed, we still
1096    * need to clock through 16 cycles to keep the PHY(s) in sync.
1097    */
1098   if (ack) {
1099      for(i = 0; i < 16; i++) {
1100         MII_CLR(XL_MII_CLK);
1101         MII_SET(XL_MII_CLK);
1102      }
1103      goto fail;
1104   }
1105
1106   for (i = 0x8000; i; i >>= 1) {
1107      MII_CLR(XL_MII_CLK);
1108      if (!ack) {
1109         if (CSR_READ_2(sc, XL_W4_PHY_MGMT) & XL_MII_DATA)
1110            frame->mii_data |= i;
1111      }
1112      MII_SET(XL_MII_CLK);
1113   }
1114
1115  fail:
1116
1117   MII_CLR(XL_MII_CLK);
1118   MII_SET(XL_MII_CLK);
1119
1120   if (ack)
1121      return(1);
1122   return(0);
1123}
1124
1125/*
1126 * Write to a PHY register through the MII.
1127 */
1128static int
1129xl_mii_writereg(sc, frame)
1130   struct elnk_softc            *sc;
1131   struct xl_mii_frame  *frame;
1132       
1133{
1134   /*
1135    * Set up frame for TX.
1136    */
1137
1138   frame->mii_stdelim = XL_MII_STARTDELIM;
1139   frame->mii_opcode = XL_MII_WRITEOP;
1140   frame->mii_turnaround = XL_MII_TURNAROUND;
1141       
1142   /*
1143    * Select the window 4.
1144    */
1145   XL_SEL_WIN(4);
1146
1147   /*
1148    * Turn on data output.
1149    */
1150   MII_SET(XL_MII_DIR);
1151
1152   xl_mii_sync(sc);
1153
1154   xl_mii_send(sc, frame->mii_stdelim, 2);
1155   xl_mii_send(sc, frame->mii_opcode, 2);
1156   xl_mii_send(sc, frame->mii_phyaddr, 5);
1157   xl_mii_send(sc, frame->mii_regaddr, 5);
1158   xl_mii_send(sc, frame->mii_turnaround, 2);
1159   xl_mii_send(sc, frame->mii_data, 16);
1160
1161   /* Idle bit. */
1162   MII_SET(XL_MII_CLK);
1163   MII_CLR(XL_MII_CLK);
1164
1165   /*
1166    * Turn off xmit.
1167    */
1168   MII_CLR(XL_MII_DIR);
1169
1170   return(0);
1171}
1172
1173static int
1174xl_miibus_readreg(sc, phy, reg)
1175   struct elnk_softc            *sc;
1176   int                  phy, reg;
1177{
1178   struct xl_mii_frame  frame;
1179
1180   /*
1181    * Pretend that PHYs are only available at MII address 24.
1182    * This is to guard against problems with certain 3Com ASIC
1183    * revisions that incorrectly map the internal transceiver
1184    * control registers at all MII addresses. This can cause
1185    * the miibus code to attach the same PHY several times over.
1186    */
1187   if ((!(sc->xl_flags & XL_FLAG_PHYOK)) && phy != 24)
1188   {
1189      printk("etherlink : unit elnk%d xl_miibus_readreg returned\n", sc->xl_unit);
1190      return(0);
1191   }
1192
1193   bzero((char *)&frame, sizeof(frame));
1194
1195   frame.mii_phyaddr = phy;
1196   frame.mii_regaddr = reg;
1197   xl_mii_readreg(sc, &frame);
1198
1199   return(frame.mii_data);
1200}
1201
1202static int
1203xl_miibus_writereg(sc, phy, reg, data)
1204   struct elnk_softc            *sc;
1205   int                  phy, reg, data;
1206{
1207   struct xl_mii_frame  frame;
1208
1209   if ((!(sc->xl_flags & XL_FLAG_PHYOK)) && phy != 24)
1210   {
1211      printk("etherlink : unit elnk%d xl_miibus_writereg returned\n", sc->xl_unit);
1212      return(0);
1213   }
1214
1215   bzero((char *)&frame, sizeof(frame));
1216
1217   frame.mii_phyaddr = phy;
1218   frame.mii_regaddr = reg;
1219   frame.mii_data = data;
1220
1221   xl_mii_writereg(sc, &frame);
1222
1223   return(0);
1224}
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234/*
1235 * The EEPROM is slow: give it time to come ready after issuing
1236 * it a command.
1237 */
1238static int
1239xl_eeprom_wait(sc)
1240   struct elnk_softc            *sc;
1241{
1242   int                  i;
1243
1244   for (i = 0; i < 100; i++) {
1245      if (CSR_READ_2(sc, XL_W0_EE_CMD) & XL_EE_BUSY)
1246         DELAY(162);
1247      else
1248         break;
1249   }
1250
1251   if (i == 100) {
1252      printk("etherlink : unit elnk%d eeprom failed to come ready\n", sc->xl_unit);
1253      return(1);
1254   }
1255
1256   return(0);
1257}
1258
1259/*
1260 * Read a sequence of words from the EEPROM. Note that ethernet address
1261 * data is stored in the EEPROM in network byte order.
1262 */
1263static int
1264xl_read_eeprom(sc, dest, off, cnt, swap)
1265   struct elnk_softc            *sc;
1266   caddr_t                      dest;
1267   int                  off;
1268   int                  cnt;
1269   int                  swap;
1270{
1271   int                  err = 0, i;
1272   u_int16_t            word = 0, *ptr;
1273#define EEPROM_5BIT_OFFSET(A) ((((A) << 2) & 0x7F00) | ((A) & 0x003F))
1274#define EEPROM_8BIT_OFFSET(A) ((A) & 0x003F)
1275   /* WARNING! DANGER!
1276    * It's easy to accidentally overwrite the rom content!
1277    * Note: the 3c575 uses 8bit EEPROM offsets.
1278    */
1279   XL_SEL_WIN(0);
1280
1281   if (xl_eeprom_wait(sc))
1282      return(1);
1283
1284   if (sc->xl_flags & XL_FLAG_EEPROM_OFFSET_30)
1285      off += 0x30;
1286
1287   for (i = 0; i < cnt; i++) {
1288      if (sc->xl_flags & XL_FLAG_8BITROM)
1289         CSR_WRITE_2(sc, XL_W0_EE_CMD,
1290                     XL_EE_8BIT_READ | EEPROM_8BIT_OFFSET(off + i));
1291      else
1292         CSR_WRITE_2(sc, XL_W0_EE_CMD,
1293                     XL_EE_READ | EEPROM_5BIT_OFFSET(off + i));
1294      err = xl_eeprom_wait(sc);
1295      if (err)
1296         break;
1297      word = CSR_READ_2(sc, XL_W0_EE_DATA);
1298      ptr = (u_int16_t *)(dest + (i * 2));
1299      if (swap)
1300         *ptr = ntohs(word);
1301      else
1302         *ptr = word;   
1303   }
1304
1305   return(err ? 1 : 0);
1306}
1307
1308
1309
1310
1311static void
1312xl_stats_update(timerid,xsc)
1313   rtems_id timerid;
1314   void *xsc;
1315{
1316   volatile struct elnk_softc   *sc = (struct elnk_softc *)xsc;
1317   volatile struct ifnet        *ifp = &sc->arpcom.ac_if;
1318
1319   int  i;
1320
1321   sc->xl_stats.intstatus = CSR_READ_2(sc, XL_STATUS);
1322
1323   sc->xl_stats.miianr    = xl_miibus_readreg(sc, 0x18, MII_ANAR );
1324   sc->xl_stats.miipar    = xl_miibus_readreg(sc, 0x18, MII_ANLPAR );
1325   sc->xl_stats.miistatus = xl_miibus_readreg(sc, 0x18, MII_BMSR );
1326   sc->xl_stats.miicmd    = xl_miibus_readreg(sc, 0x18, MII_BMCR );
1327     
1328   XL_SEL_WIN(1);
1329   sc->xl_stats.rxstatus = CSR_READ_2(sc, XL_W1_RX_STATUS );
1330   sc->xl_stats.txstatus = CSR_READ_1(sc, XL_W1_TX_STATUS );
1331   sc->xl_stats.smbstatus = CSR_READ_2(sc, 2 );
1332
1333   /* Read all the stats registers. */
1334   XL_SEL_WIN(6);
1335
1336   sc->xl_stats.xl_carrier_lost              += CSR_READ_1(sc, XL_W6_CARRIER_LOST);
1337   sc->xl_stats.xl_sqe_errs                  += CSR_READ_1(sc, XL_W6_SQE_ERRORS);
1338   sc->xl_stats.xl_tx_multi_collision        += CSR_READ_1(sc, XL_W6_COL_MULTIPLE);
1339   sc->xl_stats.xl_tx_single_collision       += CSR_READ_1(sc, XL_W6_COL_SINGLE);
1340   sc->xl_stats.xl_tx_late_collision         += CSR_READ_1(sc, XL_W6_COL_LATE);
1341   sc->xl_stats.xl_rx_overrun                += CSR_READ_1(sc, XL_W6_RX_OVERRUN);
1342   sc->xl_stats.xl_tx_frames_ok              += CSR_READ_1(sc, XL_W6_TX_OK);
1343   sc->xl_stats.xl_rx_frames_ok              += CSR_READ_1(sc, XL_W6_RX_OK);
1344   sc->xl_stats.xl_tx_deferred               += CSR_READ_1(sc, XL_W6_DEFERRED);
1345   sc->xl_stats.xl_upper_frames_ok           += CSR_READ_1(sc, XL_W6_UPPER_FRAMES_OK);
1346
1347   sc->xl_stats.xl_rx_bytes_ok               += CSR_READ_2(sc, XL_W6_TX_BYTES_OK );
1348   sc->xl_stats.xl_tx_bytes_ok               += CSR_READ_2(sc, XL_W6_RX_BYTES_OK );
1349
1350/*
1351   for (i = 0; i < 16; i++)
1352   *p++ = CSR_READ_1(sc, XL_W6_CARRIER_LOST + i);
1353*/
1354
1355   ifp->if_ierrors += sc->xl_stats.xl_rx_overrun;
1356
1357   ifp->if_collisions += sc->xl_stats.xl_tx_multi_collision +
1358      sc->xl_stats.xl_tx_single_collision +
1359      sc->xl_stats.xl_tx_late_collision;
1360
1361   /*
1362    * Boomerang and cyclone chips have an extra stats counter
1363    * in window 4 (BadSSD). We have to read this too in order
1364    * to clear out all the stats registers and avoid a statsoflow
1365    * interrupt.
1366    */
1367   XL_SEL_WIN(4);
1368   sc->xl_stats.xl_upper_bytes_ok       += CSR_READ_1(sc, XL_W4_UPPERBYTESOK);
1369   sc->xl_stats.xl_badssd               += CSR_READ_1(sc, XL_W4_BADSSD);
1370   sc->xl_stats.mediastatus              = CSR_READ_2(sc, XL_W4_MEDIA_STATUS );
1371
1372   XL_SEL_WIN(7);
1373
1374   if (!sc->xl_stats_no_timeout)
1375      rtems_timer_fire_after( sc->stat_timer_id, sc->stats_update_ticks, xl_stats_update, (void *)sc );
1376   return;
1377}
1378
1379
1380
1381
1382
1383
1384
1385static void
1386xl_reset(sc)
1387   struct elnk_softc            *sc;
1388{
1389   register int         i;
1390
1391   XL_SEL_WIN(0);
1392   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RESET |
1393               ((sc->xl_flags & XL_FLAG_WEIRDRESET) ?
1394                XL_RESETOPT_DISADVFD:0));
1395
1396   for (i = 0; i < XL_TIMEOUT; i++) {
1397      DELAY(10);
1398      if (!(CSR_READ_2(sc, XL_STATUS) & XL_STAT_CMDBUSY))
1399         break;
1400   }
1401
1402   if (i == XL_TIMEOUT)
1403      printk("etherlink : unit elnk%d reset didn't complete\n", sc->xl_unit);
1404
1405   /* Reset TX and RX. */
1406   /* Note: the RX reset takes an absurd amount of time
1407    * on newer versions of the Tornado chips such as those
1408    * on the 3c905CX and newer 3c908C cards. We wait an
1409    * extra amount of time so that xl_wait() doesn't complain
1410    * and annoy the users.
1411    */
1412   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_RESET);
1413   DELAY(100000);
1414   xl_wait(sc);
1415   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_TX_RESET);
1416   xl_wait(sc);
1417
1418   if (sc->xl_flags & XL_FLAG_INVERT_LED_PWR ||
1419       sc->xl_flags & XL_FLAG_INVERT_MII_PWR) {
1420      XL_SEL_WIN(2);
1421      CSR_WRITE_2(sc, XL_W2_RESET_OPTIONS, CSR_READ_2(sc,
1422                                                      XL_W2_RESET_OPTIONS)
1423                  | ((sc->xl_flags & XL_FLAG_INVERT_LED_PWR)?XL_RESETOPT_INVERT_LED:0)
1424                  | ((sc->xl_flags & XL_FLAG_INVERT_MII_PWR)?XL_RESETOPT_INVERT_MII:0)
1425         );
1426   }
1427
1428   /* Wait a little while for the chip to get its brains in order. */
1429   DELAY(100000);
1430   return;
1431}
1432
1433
1434
1435
1436static void
1437xl_stop(sc)
1438   struct elnk_softc            *sc;
1439{
1440   register int         i;
1441   struct ifnet         *ifp;
1442
1443   ifp = &sc->arpcom.ac_if;
1444   ifp->if_timer = 0;
1445
1446   rtems_timer_cancel( sc->stat_timer_id );
1447
1448   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_DISABLE);
1449   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_STATS_DISABLE);
1450   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_INTR_ENB);
1451   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_DISCARD);
1452   xl_wait(sc);
1453   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_TX_DISABLE);
1454   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_COAX_STOP);
1455   DELAY(800);
1456
1457   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_INTR_ACK|XL_STAT_INTLATCH);
1458   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_STAT_ENB|0);
1459   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_INTR_ENB|0);
1460
1461   return;
1462}
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478static void
1479xl_setcfg(sc)
1480   struct elnk_softc            *sc;
1481{
1482   u_int32_t            icfg;
1483
1484   XL_SEL_WIN(3);
1485   icfg = CSR_READ_4(sc, XL_W3_INTERNAL_CFG);
1486   icfg &= ~XL_ICFG_CONNECTOR_MASK;
1487   if (sc->xl_media & XL_MEDIAOPT_MII ||
1488       sc->xl_media & XL_MEDIAOPT_BT4)
1489      icfg |= (XL_XCVR_MII << XL_ICFG_CONNECTOR_BITS);
1490   if (sc->xl_media & XL_MEDIAOPT_BTX)
1491      icfg |= (XL_XCVR_AUTO << XL_ICFG_CONNECTOR_BITS);
1492
1493   CSR_WRITE_4(sc, XL_W3_INTERNAL_CFG, icfg);
1494   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_COAX_STOP);
1495
1496   XL_SEL_WIN(7);
1497   return;
1498}
1499
1500static void
1501xl_setmode(sc, media)
1502   struct elnk_softc            *sc;
1503   int                  media;
1504{
1505   u_int32_t            icfg;
1506   u_int16_t            mediastat;
1507
1508   printk("etherlink : unit elnk%d selecting ", sc->xl_unit);
1509
1510   XL_SEL_WIN(4);
1511   mediastat = CSR_READ_2(sc, XL_W4_MEDIA_STATUS);
1512   XL_SEL_WIN(3);
1513   icfg = CSR_READ_4(sc, XL_W3_INTERNAL_CFG);
1514
1515   if (sc->xl_media & XL_MEDIAOPT_BT) {
1516      if (IFM_SUBTYPE(media) == IFM_10_T) {
1517         printk("10baseT transceiver, ");
1518         sc->xl_xcvr = XL_XCVR_10BT;
1519         icfg &= ~XL_ICFG_CONNECTOR_MASK;
1520         icfg |= (XL_XCVR_10BT << XL_ICFG_CONNECTOR_BITS);
1521         mediastat |= XL_MEDIASTAT_LINKBEAT|
1522            XL_MEDIASTAT_JABGUARD;
1523         mediastat &= ~XL_MEDIASTAT_SQEENB;
1524      }
1525   }
1526
1527   if (sc->xl_media & XL_MEDIAOPT_BFX) {
1528      if (IFM_SUBTYPE(media) == IFM_100_FX) {
1529         printk("100baseFX port, ");
1530         sc->xl_xcvr = XL_XCVR_100BFX;
1531         icfg &= ~XL_ICFG_CONNECTOR_MASK;
1532         icfg |= (XL_XCVR_100BFX << XL_ICFG_CONNECTOR_BITS);
1533         mediastat |= XL_MEDIASTAT_LINKBEAT;
1534         mediastat &= ~XL_MEDIASTAT_SQEENB;
1535      }
1536   }
1537
1538   if (sc->xl_media & (XL_MEDIAOPT_AUI|XL_MEDIAOPT_10FL)) {
1539      if (IFM_SUBTYPE(media) == IFM_10_5) {
1540         printk("AUI port, ");
1541         sc->xl_xcvr = XL_XCVR_AUI;
1542         icfg &= ~XL_ICFG_CONNECTOR_MASK;
1543         icfg |= (XL_XCVR_AUI << XL_ICFG_CONNECTOR_BITS);
1544         mediastat &= ~(XL_MEDIASTAT_LINKBEAT|
1545                        XL_MEDIASTAT_JABGUARD);
1546         mediastat |= ~XL_MEDIASTAT_SQEENB;
1547      }
1548      if (IFM_SUBTYPE(media) == IFM_10_FL) {
1549         printk("10baseFL transceiver, ");
1550         sc->xl_xcvr = XL_XCVR_AUI;
1551         icfg &= ~XL_ICFG_CONNECTOR_MASK;
1552         icfg |= (XL_XCVR_AUI << XL_ICFG_CONNECTOR_BITS);
1553         mediastat &= ~(XL_MEDIASTAT_LINKBEAT|
1554                        XL_MEDIASTAT_JABGUARD);
1555         mediastat |= ~XL_MEDIASTAT_SQEENB;
1556      }
1557   }
1558
1559   if (sc->xl_media & XL_MEDIAOPT_BNC) {
1560      if (IFM_SUBTYPE(media) == IFM_10_2) {
1561         printk("BNC port, ");
1562         sc->xl_xcvr = XL_XCVR_COAX;
1563         icfg &= ~XL_ICFG_CONNECTOR_MASK;
1564         icfg |= (XL_XCVR_COAX << XL_ICFG_CONNECTOR_BITS);
1565         mediastat &= ~(XL_MEDIASTAT_LINKBEAT|
1566                        XL_MEDIASTAT_JABGUARD|
1567                        XL_MEDIASTAT_SQEENB);
1568      }
1569   }
1570
1571   if ((media & IFM_GMASK) == IFM_FDX ||
1572       IFM_SUBTYPE(media) == IFM_100_FX) {
1573      printk("full duplex\n");
1574      XL_SEL_WIN(3);
1575      CSR_WRITE_1(sc, XL_W3_MAC_CTRL, XL_MACCTRL_DUPLEX);
1576   } else {
1577      printk("half duplex\n");
1578      XL_SEL_WIN(3);
1579      CSR_WRITE_1(sc, XL_W3_MAC_CTRL,
1580                  (CSR_READ_1(sc, XL_W3_MAC_CTRL) & ~XL_MACCTRL_DUPLEX));
1581   }
1582
1583   if (IFM_SUBTYPE(media) == IFM_10_2)
1584      CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_COAX_START);
1585   else
1586      CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_COAX_STOP);
1587   CSR_WRITE_4(sc, XL_W3_INTERNAL_CFG, icfg);
1588   XL_SEL_WIN(4);
1589   CSR_WRITE_2(sc, XL_W4_MEDIA_STATUS, mediastat);
1590   DELAY(800);
1591   XL_SEL_WIN(7);
1592
1593   return;
1594}
1595
1596
1597
1598
1599
1600
1601
1602static void
1603xl_choose_xcvr(sc, verbose)
1604   struct elnk_softc    *sc;
1605   int                  verbose;
1606{
1607   u_int16_t            devid;
1608
1609   /*
1610    * Read the device ID from the EEPROM.
1611    * This is what's loaded into the PCI device ID register, so it has
1612    * to be correct otherwise we wouldn't have gotten this far.
1613    */
1614   xl_read_eeprom(sc, (caddr_t)&devid, XL_EE_PRODID, 1, 0);
1615
1616   switch(devid) {
1617      case TC_DEVICEID_BOOMERANG_10BT:  /* 3c900-TPO */
1618      case TC_DEVICEID_KRAKATOA_10BT:           /* 3c900B-TPO */
1619         sc->xl_media = XL_MEDIAOPT_BT;
1620         sc->xl_xcvr = XL_XCVR_10BT;
1621         if (verbose)
1622            printk("etherlink : unit elnk%d guessing 10BaseT "
1623                   "transceiver\n", sc->xl_unit);
1624         break;
1625      case TC_DEVICEID_BOOMERANG_10BT_COMBO:    /* 3c900-COMBO */
1626      case TC_DEVICEID_KRAKATOA_10BT_COMBO:     /* 3c900B-COMBO */
1627         sc->xl_media = XL_MEDIAOPT_BT|XL_MEDIAOPT_BNC|XL_MEDIAOPT_AUI;
1628         sc->xl_xcvr = XL_XCVR_10BT;
1629         if (verbose)
1630            printk("etherlink : unit elnk%d guessing COMBO "
1631                   "(AUI/BNC/TP)\n", sc->xl_unit);
1632         break;
1633      case TC_DEVICEID_KRAKATOA_10BT_TPC:       /* 3c900B-TPC */
1634         sc->xl_media = XL_MEDIAOPT_BT|XL_MEDIAOPT_BNC;
1635         sc->xl_xcvr = XL_XCVR_10BT;
1636         if (verbose)
1637            printk("etherlink : unit elnk%d guessing TPC (BNC/TP)\n", sc->xl_unit);
1638         break;
1639      case TC_DEVICEID_CYCLONE_10FL:            /* 3c900B-FL */
1640         sc->xl_media = XL_MEDIAOPT_10FL;
1641         sc->xl_xcvr = XL_XCVR_AUI;
1642         if (verbose)
1643            printk("etherlink : unit elnk%d guessing 10baseFL\n", sc->xl_unit);
1644         break;
1645      case TC_DEVICEID_BOOMERANG_10_100BT:      /* 3c905-TX */
1646      case TC_DEVICEID_HURRICANE_555:           /* 3c555 */
1647      case TC_DEVICEID_HURRICANE_556:           /* 3c556 */
1648      case TC_DEVICEID_HURRICANE_556B:  /* 3c556B */
1649      case TC_DEVICEID_HURRICANE_575A:  /* 3c575TX */
1650      case TC_DEVICEID_HURRICANE_575B:  /* 3c575B */
1651      case TC_DEVICEID_HURRICANE_575C:  /* 3c575C */
1652      case TC_DEVICEID_HURRICANE_656:           /* 3c656 */
1653      case TC_DEVICEID_HURRICANE_656B:  /* 3c656B */
1654      case TC_DEVICEID_TORNADO_656C:            /* 3c656C */
1655      case TC_DEVICEID_TORNADO_10_100BT_920B:   /* 3c920B-EMB */
1656         sc->xl_media = XL_MEDIAOPT_MII;
1657         sc->xl_xcvr = XL_XCVR_MII;
1658         if (verbose)
1659            printk("etherlink : unit elnk%d guessing MII\n", sc->xl_unit);
1660         break;
1661      case TC_DEVICEID_BOOMERANG_100BT4:        /* 3c905-T4 */
1662      case TC_DEVICEID_CYCLONE_10_100BT4:       /* 3c905B-T4 */
1663         sc->xl_media = XL_MEDIAOPT_BT4;
1664         sc->xl_xcvr = XL_XCVR_MII;
1665         if (verbose)
1666            printk("etherlink : unit elnk%d guessing 100BaseT4/MII\n", sc->xl_unit);
1667         break;
1668      case TC_DEVICEID_HURRICANE_10_100BT:      /* 3c905B-TX */
1669      case TC_DEVICEID_HURRICANE_10_100BT_SERV:/*3c980-TX */
1670      case TC_DEVICEID_TORNADO_10_100BT_SERV:   /* 3c980C-TX */
1671      case TC_DEVICEID_HURRICANE_SOHO100TX:     /* 3cSOHO100-TX */
1672      case TC_DEVICEID_TORNADO_10_100BT:        /* 3c905C-TX */
1673      case TC_DEVICEID_TORNADO_HOMECONNECT:     /* 3c450-TX */
1674         sc->xl_media = XL_MEDIAOPT_BTX;
1675         sc->xl_xcvr = XL_XCVR_AUTO;
1676         if (verbose)
1677            printk("etherlink : unit elnk%d guessing 10/100 internal\n", sc->xl_unit);
1678         break;
1679      case TC_DEVICEID_CYCLONE_10_100_COMBO:    /* 3c905B-COMBO */
1680         sc->xl_media = XL_MEDIAOPT_BTX|XL_MEDIAOPT_BNC|XL_MEDIAOPT_AUI;
1681         sc->xl_xcvr = XL_XCVR_AUTO;
1682         if (verbose)
1683            printk("etherlink : unit elnk%d guessing 10/100 "
1684                   "plus BNC/AUI\n", sc->xl_unit);
1685         break;
1686      default:
1687         printk("etherlink : unit elnk%d unknown device ID: %x -- "
1688                "defaulting to 10baseT\n", sc->xl_unit, devid);
1689         sc->xl_media = XL_MEDIAOPT_BT;
1690         break;
1691   }
1692
1693   return;
1694}
1695
1696
1697
1698
1699
1700
1701
1702/*
1703 * This routine is a kludge to work around possible hardware faults
1704 * or manufacturing defects that can cause the media options register
1705 * (or reset options register, as it's called for the first generation
1706 * 3c90x adapters) to return an incorrect result. I have encountered
1707 * one Dell Latitude laptop docking station with an integrated 3c905-TX
1708 * which doesn't have any of the 'mediaopt' bits set. This screws up
1709 * the attach routine pretty badly because it doesn't know what media
1710 * to look for. If we find ourselves in this predicament, this routine
1711 * will try to guess the media options values and warn the user of a
1712 * possible manufacturing defect with his adapter/system/whatever.
1713 */
1714static void
1715xl_mediacheck(sc)
1716   struct elnk_softc            *sc;
1717{
1718
1719   /*
1720    * If some of the media options bits are set, assume they are
1721    * correct. If not, try to figure it out down below.
1722    * XXX I should check for 10baseFL, but I don't have an adapter
1723    * to test with.
1724    */
1725   if (sc->xl_media & (XL_MEDIAOPT_MASK & ~XL_MEDIAOPT_VCO)) {
1726      /*
1727       * Check the XCVR value. If it's not in the normal range
1728       * of values, we need to fake it up here.
1729       */
1730      if (sc->xl_xcvr <= XL_XCVR_AUTO)
1731         return;
1732      else {
1733         printk("etherlink : unit elnk%d bogus xcvr value "
1734                "in EEPROM (%x)\n", sc->xl_unit, sc->xl_xcvr);
1735         printk("etherlink : unit elnk%d choosing new default based "
1736                "on card type\n", sc->xl_unit);
1737      }
1738   } else {
1739      if (sc->xl_type == XL_TYPE_905B &&
1740          sc->xl_media & XL_MEDIAOPT_10FL)
1741         return;
1742      printk("etherlink : unit elnk%d WARNING: no media options bits set in "
1743             "the media options register!!\n", sc->xl_unit);
1744      printk("etherlink : unit elnk%d this could be a manufacturing defect in "
1745             "your adapter or system\n", sc->xl_unit);
1746      printk("etherlink : unit elnk%d attempting to guess media type; you "
1747             "should probably consult your vendor\n", sc->xl_unit);
1748   }
1749
1750   xl_choose_xcvr(sc, 1);
1751   return;
1752}
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778static void no_op(const rtems_irq_connect_data* irq)
1779{
1780   return;
1781}
1782
1783
1784
1785
1786static int elnkIsOn(const rtems_irq_connect_data* irq)
1787{
1788  return BSP_irq_enabled_at_i8259s (irq->name);
1789}
1790
1791
1792
1793
1794
1795
1796static void
1797elnk_start_txchain( struct elnk_softc *sc, struct MD *chaintop )
1798{
1799   printk("unit elnk%d tx start\n", sc->xl_unit );
1800
1801   sc->tx_idle = 0;
1802
1803   /* save the address of the TX list */
1804   sc->last_txchain_head = chaintop;
1805
1806   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_DOWN_STALL);
1807   xl_wait(sc);
1808   CSR_WRITE_4(sc, XL_DOWNLIST_PTR, phys_to_bus( sc->last_txchain_head ));
1809   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_DOWN_UNSTALL);
1810}
1811
1812
1813
1814
1815
1816/*
1817 * ELNK interrupt handler
1818 */
1819static rtems_isr
1820elnk_interrupt_handler ( struct elnk_softc *sc )
1821{
1822   struct ifnet         *ifp = &sc->arpcom.ac_if;
1823   u_int16_t            status;
1824
1825   while( (status = CSR_READ_2(sc, XL_STATUS)) & XL_INTRS && status != 0xFFFF)
1826   {
1827      sc->xl_stats.device_interrupts++;
1828
1829      CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_INTR_ACK|(status & XL_INTRS));
1830
1831#if 0
1832      printk("etherlink : unit elnk%d intstatus %04x\n", sc->xl_unit, status  );
1833#endif
1834
1835      if (status & XL_STAT_UP_COMPLETE)
1836      {
1837#if 0
1838         printk("etherlink : unit elnk%d rx\n", sc->xl_unit );
1839#endif
1840         /* received packets */
1841         rtems_event_send(rxDaemonTid, sc->ioevent);
1842      }
1843
1844      if (status & XL_STAT_DOWN_COMPLETE)
1845      {
1846         struct MD *chaintailmd = NULL;
1847
1848         /* all packets uploaded to the device */
1849
1850         if( sc->last_txchain_head->mbuf )
1851         {
1852            /* either this is a chain of 2 or more packets, or its a
1853             * single packet chain and another chain is also ready to send
1854             */
1855            if( (int)sc->last_txchain_head->mbuf == -1 )
1856            {
1857               /* single packet was sent, no indirection to the last entry
1858                  in the chain.  since mbuf is != 0, then another chain
1859                  is ready */
1860               chaintailmd = sc->last_txchain_head;
1861            }
1862            else
1863            {
1864               /* otherwise, this is a pointer to the last packet in the
1865                  chain */
1866               chaintailmd = (struct MD *)sc->last_txchain_head->mbuf;
1867               if( !chaintailmd->mbuf ) chaintailmd = NULL;
1868            }
1869         }
1870
1871         if( chaintailmd )
1872         {
1873            /* the next MD is the start of another chain */
1874            elnk_start_txchain(sc, chaintailmd->next_md );
1875         }
1876         else
1877         {
1878            /* otherwise, we have nothing else to send */
1879            CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_DOWN_STALL);
1880            sc->tx_idle = -1;
1881            printk("unit elnk%d tx done\n", sc->xl_unit );
1882         }
1883      }
1884
1885      if (status & XL_STAT_TX_COMPLETE)
1886      {
1887         ifp->if_oerrors++;
1888         printk("etherlink : unit elnk%d transmission error\n", sc->xl_unit );
1889      }
1890      if (status & XL_STAT_ADFAIL)
1891      {
1892         printk("etherlink : unit elnk%d Catastrophic bus failure\n", sc->xl_unit );
1893      }
1894      if (status & XL_STAT_STATSOFLOW)
1895      {
1896         sc->xl_stats_no_timeout = 1;
1897         xl_stats_update(sc->stat_timer_id,sc);
1898         sc->xl_stats_no_timeout = 0;
1899      }
1900   }
1901   
1902
1903#if 0
1904   {
1905      unsigned16 intstatus, intenable, indenable;
1906
1907      intstatus = CSR_READ_2(sc, XL_STATUS );
1908
1909      XL_SEL_WIN(5);
1910      intenable = CSR_READ_2(sc, XL_W5_INTR_ENB );
1911      indenable = CSR_READ_2(sc, XL_W5_STAT_ENB );
1912      XL_SEL_WIN(7);
1913      printk("etherlink : unit elnk%d istat %04x, ien %04x, ind %04x\n", sc->xl_unit, intstatus, intenable, indenable  );
1914   }
1915#endif
1916}
1917
1918
1919
1920
1921
1922static rtems_isr
1923elnk_interrupt_handler_entry()
1924{
1925   int i;
1926
1927   /*
1928   ** Check all the initialized units for interrupt service
1929   */
1930
1931   for(i=0; i< NUM_UNITS; i++ )
1932   {
1933      if( elnk_softc[i].ioaddr )
1934         elnk_interrupt_handler( &elnk_softc[i] );
1935   }
1936}
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948/*
1949 * Initialize the ethernet hardware
1950 */
1951static void
1952elnk_initialize_hardware (struct elnk_softc *sc)
1953{
1954   volatile unsigned char *cp;
1955   int nb, i, rxsize, txsize, ringsize;
1956
1957   /*
1958    * Init RX ring
1959    */
1960   cp = (volatile unsigned char *)malloc( (nb =
1961                                           (ringsize = ((rxsize = (sc->numRxbuffers * sizeof(struct MD))) +
1962                                                        (txsize = (sc->numTxbuffers * sizeof(struct MD))))) +
1963                                             (sc->numTxbuffers * XL_PACKET_SIZE)) +
1964                                          + CPU_CACHE_ALIGNMENT_FOR_BUFFER);
1965   sc->bufferBase = cp;
1966   cp += (CPU_CACHE_ALIGNMENT_FOR_BUFFER - (int)cp) & (CPU_CACHE_ALIGNMENT_FOR_BUFFER - 1);
1967#if defined(__i386__)
1968#ifdef PCI_BRIDGE_DOES_NOT_ENSURE_CACHE_COHERENCY_FOR_DMA
1969   if (_CPU_is_paging_enabled())
1970      _CPU_change_memory_mapping_attribute
1971         (NULL, cp, nb, PTE_CACHE_DISABLE | PTE_WRITABLE);
1972#endif
1973#endif
1974   sc->ringBase = cp;
1975
1976   {
1977      volatile unsigned char *txpackets;
1978      struct MD            *rx_ring, *tx_ring, *nxtmd, *thismd;
1979      struct mbuf          *m;
1980
1981      /* rebuild tx and rx rings */
1982
1983      rx_ring = (struct MD *)sc->ringBase;
1984      tx_ring = (struct MD *)&sc->ringBase[ rxsize ];
1985      txpackets = &sc->ringBase[ringsize];
1986
1987      /*
1988       * The rx ring is easy as its just an array of MD structs, the mbuf
1989       * data is dynamically requested from the ip stack.
1990       */
1991      for(i=0 ; i<sc->numRxbuffers; i++)
1992      {
1993         /* allocate an mbuf for each receive descriptor */
1994         MGETHDR (m, M_WAIT, MT_DATA);
1995         MCLGET (m, M_WAIT);
1996         m->m_pkthdr.rcvif = &sc->arpcom.ac_if;
1997     
1998         if( i == sc->numRxbuffers-1 )
1999            nxtmd = &rx_ring[0];
2000         else
2001            nxtmd = &rx_ring[i+1];
2002
2003         rx_ring[i].next_md = nxtmd;
2004         rx_ring[i].mbuf = m;
2005
2006         rx_ring[i].status = 0;
2007         st_le32( &rx_ring[i].addr, (unsigned32)phys_to_bus( mtod(m, void *) ));
2008         st_le32( &rx_ring[i].length, XL_LAST_FRAG | XL_PACKET_SIZE );
2009         st_le32( &rx_ring[i].next, (unsigned32)phys_to_bus( nxtmd ));
2010      }
2011      sc->curr_rx_md = &rx_ring[0];
2012
2013
2014      /*
2015       * The tx ring is more complex. Each MD has a packet buffer
2016       * permanently assigned to it.  Although the next_md fields form a
2017       * ring, the DPD next is filled only when packets are added to the
2018       * tx chain.  mbuf is a scratch register used to form chains of tx
2019       * packets.
2020       */
2021
2022      for(i=0 ; i<sc->numTxbuffers; i++)
2023      {
2024         if( i == sc->numTxbuffers-1 )
2025            nxtmd = &tx_ring[0];
2026         else
2027            nxtmd = &tx_ring[i+1];
2028
2029         thismd = &tx_ring[i];
2030
2031         thismd->next_md = nxtmd;
2032         thismd->mbuf = NULL;
2033
2034         st_le32( &thismd->status, XL_TXSTAT_DL_COMPLETE );
2035         st_le32( &thismd->addr, (unsigned32)phys_to_bus( &txpackets[ i * XL_PACKET_SIZE ] ));
2036         st_le32( &thismd->length, XL_LAST_FRAG );
2037         thismd->next = 0;
2038      }
2039      sc->last_tx_md = &tx_ring[0];
2040
2041   }
2042
2043
2044
2045
2046#ifdef ELNK_DEBUG
2047   printk("etherlink : %02x:%02x:%02x:%02x:%02x:%02x   name 'elnk%d', io %x, int %d\n",
2048          sc->arpcom.ac_enaddr[0], sc->arpcom.ac_enaddr[1],
2049          sc->arpcom.ac_enaddr[2], sc->arpcom.ac_enaddr[3],
2050          sc->arpcom.ac_enaddr[4], sc->arpcom.ac_enaddr[5],
2051          sc->xl_unit,
2052          (unsigned)sc->ioaddr, sc->irqInfo.name );
2053#endif
2054
2055
2056   sc->irqInfo.hdl  = (rtems_irq_hdl)elnk_interrupt_handler_entry;
2057   sc->irqInfo.on   = no_op;
2058   sc->irqInfo.off  = no_op;
2059   sc->irqInfo.isOn = elnkIsOn;
2060
2061   if( sc->irqInfo.name != 255 )
2062   {
2063      int st;
2064
2065#ifdef BSP_SHARED_HANDLER_SUPPORT
2066      st = BSP_install_rtems_shared_irq_handler( &sc->irqInfo );
2067#else
2068      st = BSP_install_rtems_irq_handler( &sc->irqInfo );
2069#endif
2070      if (!st)
2071         rtems_panic ("etherlink : unit elnk%d Interrupt name %d already in use\n", sc->xl_unit, sc->irqInfo.name );
2072   }
2073   else
2074   {
2075      printk("etherlink : unit elnk%d Interrupt not specified by device\n", sc->xl_unit );
2076   }
2077}
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089static void
2090elnk_rxDaemon (void *arg)
2091{
2092   struct elnk_softc     *sc;
2093   volatile struct ifnet *ifp;
2094   struct ether_header   *eh;
2095   struct mbuf           *m;
2096   volatile struct MD    *rmd;
2097   unsigned int          i,len, rxstat;
2098   rtems_event_set       events;
2099
2100   for (;;)
2101   {
2102
2103      rtems_bsdnet_event_receive( RTEMS_ALL_EVENTS,
2104                                  RTEMS_WAIT|RTEMS_EVENT_ANY,
2105                                  RTEMS_NO_TIMEOUT,
2106                                  &events);
2107
2108      for(;;)
2109      {
2110         for(i=0; i< NUM_UNITS; i++ )
2111         {
2112            sc = &elnk_softc[i];
2113            if( sc->ioaddr )
2114            {
2115               if( events & sc->ioevent )
2116               {
2117                  struct ifnet *ifp = &sc->arpcom.ac_if;
2118
2119                  rmd   = sc->curr_rx_md;
2120
2121                  /*
2122                  ** Read off all the packets we've received on this unit
2123                  */
2124                  while( (rxstat = ld_le32(&rmd->status)) )
2125                  {
2126                     if (rxstat & XL_RXSTAT_UP_ERROR)
2127                     {
2128                        printk("unit %i up error\n", sc->xl_unit );
2129                        ifp->if_ierrors++;
2130                     }
2131
2132                     if( (rxstat & XL_RXSTAT_UP_CMPLT) )
2133                     {
2134
2135#if 0
2136                        {
2137                           char *pkt, *delim;
2138                           int  i;
2139                           pkt = mtod(rmd->mbuf, char *);
2140                           printk("unit %i rx  pkt (%08x) ", sc->xl_unit, pkt );
2141                           for(delim="", i=0; i < sizeof(struct ether_header)+8; i++, delim=":")
2142                              printk("%s%02x", delim, (char) pkt[i] );
2143                           printk("\n");
2144                        }
2145#endif
2146
2147                        /* pass on the packet in the mbuf */
2148                        len = ( ld_le32(&rmd->status) & XL_RXSTAT_LENMASK);
2149                        m = rmd->mbuf;
2150                        m->m_len = m->m_pkthdr.len = len - sizeof(struct ether_header);
2151                        eh = mtod(m, struct ether_header *);
2152                        m->m_data += sizeof(struct ether_header);
2153
2154                        ether_input(ifp, eh, m);
2155
2156                        /* get a new mbuf */
2157                        MGETHDR (m, M_WAIT, MT_DATA);
2158                        MCLGET (m, M_WAIT);
2159                        m->m_pkthdr.rcvif = ifp;
2160                        rmd->mbuf   = m;
2161                        rmd->status = 0;
2162                        st_le32( &rmd->addr, (unsigned32)phys_to_bus(mtod(m, void *)) );
2163                     }
2164                     else
2165                     {
2166                        /* some kind of packet failure */
2167                        printk("etherlink : unit elnk%d bad receive status -- packet dropped\n", sc->xl_unit);
2168                        ifp->if_ierrors++;
2169                     }
2170                     /* clear descriptor status */
2171                     rmd->status = 0;
2172     
2173                     rmd = rmd->next_md;
2174                  }
2175
2176                  sc->curr_rx_md = rmd;
2177               }
2178            }
2179         }
2180
2181         /*
2182         ** If more events are pending, service them before we go back to sleep
2183         */
2184         if( rtems_event_receive( RTEMS_ALL_EVENTS,
2185                                  RTEMS_NO_WAIT | RTEMS_EVENT_ANY,
2186                                  0,
2187                                  &events ) == RTEMS_UNSATISFIED ) break;
2188      }
2189   }
2190}
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203/*
2204 * Driver transmit daemon
2205 */
2206void
2207elnk_txDaemon (void *arg)
2208{
2209   struct elnk_softc     *sc;
2210   struct ifnet          *ifp;
2211   struct mbuf           *m;
2212   struct mbuf           *hold;
2213   volatile struct MD    *lastmd, *nextmd, *firstmd;
2214   unsigned char        *pktbuff, *buffer;
2215   unsigned int         len;
2216   int                  chainCount,i;
2217   rtems_event_set       events;
2218
2219   for (;;)
2220   {
2221      /*
2222       * Wait for packets bound for any of the units
2223       */
2224      rtems_bsdnet_event_receive( RTEMS_ALL_EVENTS,
2225                                  RTEMS_EVENT_ANY | RTEMS_WAIT,
2226                                  RTEMS_NO_TIMEOUT, &events);
2227
2228      for(i=0; i< NUM_UNITS; i++ )
2229      {
2230         sc  = &elnk_softc[i];
2231         if( sc->ioaddr )
2232         {
2233            if( events & sc->ioevent )
2234            {
2235               ifp = &sc->arpcom.ac_if;
2236
2237               /*
2238                * Send packets till queue is empty or tx ring is full
2239                */
2240               chainCount = 0;
2241               firstmd = NULL;
2242               
2243               lastmd = sc->last_tx_md;
2244
2245               for(;;)
2246               {
2247                  nextmd = lastmd->next_md;
2248
2249                  /* stop when ring is full */
2250                  if( ! (ld_le32(&nextmd->status) & XL_TXSTAT_DL_COMPLETE) )
2251                  {
2252                     printk("etherlink : unit elnk%d tx ring full!\n", sc->xl_unit);
2253                     break;
2254                  }
2255
2256                  IF_DEQUEUE(&ifp->if_snd, m);
2257                  if( !m ) break;
2258
2259
2260                 
2261                  hold = m;
2262                  len = 0;
2263                 
2264                  buffer = pktbuff = (char *)bus_to_phys( ld_le32(&nextmd->addr) );
2265                  for(;;)
2266                  {
2267                     len += m->m_len;
2268                     memcpy((void*) buffer, (char *)m->m_data, m->m_len);
2269                     buffer += m->m_len ;
2270                     if ((m = m->m_next) == NULL)
2271                        break;
2272                  }
2273                  m_freem( hold );
2274
2275
2276                  {
2277                     char *pkt = pktbuff, *delim;
2278                     int  i;
2279                     printk("unit %i queued  pkt (%08x) ", sc->xl_unit, (unsigned32)pkt );
2280                     for(delim="", i=0; i < sizeof(struct ether_header)+8; i++, delim=":")
2281                        printk("%s%02x", delim, (char) pkt[i] );
2282                     printk("\n");
2283                  }
2284
2285     
2286                  if (len < XL_MIN_FRAMELEN ) len = XL_MIN_FRAMELEN;
2287                  st_le32( &nextmd->length, XL_LAST_FRAG | (len & XL_TXSTAT_LENMASK) );
2288
2289                  /* this packet will be the new end of the list */
2290                  nextmd->next = 0;
2291                  nextmd->status = 0;
2292
2293                  if( !firstmd )
2294                  {                     
2295                     /* keep track of the first packet we add to the chain */
2296                     firstmd = nextmd;
2297
2298                     /* use the mbuf pointer of the last packet of the
2299                      * previous chain as a flag so when a dnComplete
2300                      * interrupt indicates the card is finished, the
2301                      * isr can immediately start the next chain.  Note
2302                      * several chains of packets may be assembled this
2303                      * way- but this assignment won't have any effect
2304                      * if a chain isn't already being transmitted.
2305                      */
2306                     lastmd->mbuf = (struct mbuf *)-1;
2307                  }
2308                  else
2309                  {
2310                     /* hook this packet to the previous one */
2311                     st_le32( &lastmd->next, (unsigned32)phys_to_bus( nextmd ));
2312                  }
2313
2314                  ++chainCount;
2315                  lastmd = nextmd;
2316               }
2317
2318
2319
2320               if( firstmd )
2321               {
2322                  /* save the last descriptor we set up in the chain */
2323                  sc->last_tx_md = lastmd;
2324
2325                  /*
2326                   * We've added one or more packets to a chain, flag
2327                   * the last packet so we get an dnComplete interrupt
2328                   * when the card finishes accepting the chain
2329                   */
2330                  st_le32(&lastmd->status, XL_TXSTAT_DL_INTR );
2331
2332                  /*
2333                   * point the chain head's mbuf to the tail so we can
2334                   * locate the next chain to send inside the isr.  If
2335                   * we're only sending one packet, then don't bother
2336                   * with the link, as the mbuf value will either be 0
2337                   * if theres no next chain or -1 if there is.
2338                   */
2339                  if( chainCount > 1 )
2340                     firstmd->mbuf = (struct mbuf *)lastmd;
2341
2342                  /* clear our "next chain present" flag.  If another
2343                   * chain is added later, this flag will be set to -1
2344                   */
2345                  lastmd->mbuf = NULL;
2346
2347
2348                  if( sc->tx_idle == 0 && CSR_READ_4(sc, XL_DOWNLIST_PTR) == 0 )
2349                  {
2350                     printk("etherlink : unit elnk%d tx forced!\n", sc->xl_unit);
2351                     sc->tx_idle = -1;
2352                  }
2353
2354                  /* start sending this chain of packets if tx isn't busy */
2355                  if( sc->tx_idle )
2356                  {
2357                     elnk_start_txchain(sc, firstmd);
2358                  }
2359               }
2360
2361               ifp->if_flags &= ~IFF_OACTIVE;
2362            }
2363         }
2364      }
2365   }
2366}
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378static void
2379elnk_start (struct ifnet *ifp)
2380{
2381   struct elnk_softc *sc = ifp->if_softc;
2382#if 0
2383   printk("unit %i tx signaled\n", sc->xl_unit );
2384#endif
2385   ifp->if_flags |= IFF_OACTIVE;
2386   rtems_event_send( txDaemonTid, sc->ioevent );
2387}
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403/*
2404 * Initialize and start the device
2405 */
2406static void
2407elnk_init (void *arg)
2408{
2409   int i;
2410   struct elnk_softc *sc = arg;
2411   struct ifnet *ifp = &sc->arpcom.ac_if;
2412
2413   xl_stop(sc);
2414
2415   XL_SEL_WIN(3);
2416   CSR_WRITE_1(sc, XL_W3_MAC_CTRL, XL_MACCTRL_DUPLEX);
2417
2418   {
2419      unsigned32 cr,sr;
2420
2421      xl_miibus_writereg(sc, 0x18, MII_BMCR, BMCR_RESET );
2422   
2423      while( (cr = xl_miibus_readreg(sc, 0x18, MII_BMCR )) & BMCR_RESET )
2424      {
2425         DELAY(100000);
2426      }
2427
2428      xl_miibus_writereg(sc, 0x18, MII_ANAR, ANAR_10 | ANAR_10_FD | ANAR_TX | ANAR_TX_FD | ANAR_T4);
2429      xl_miibus_writereg(sc, 0x18, MII_BMCR, BMCR_AUTOEN );
2430
2431      /* while( ((sr = xl_miibus_readreg(sc, 0x18, MII_BMSR)) & BMSR_ACOMP) == 0 ); */
2432   }
2433
2434   
2435   XL_SEL_WIN(7);
2436   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_RESET);
2437   xl_wait(sc);
2438   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_TX_RESET);
2439   xl_wait(sc);
2440   DELAY(10000);
2441
2442   sc->tx_idle = -1;
2443
2444   /*
2445    * Set up hardware if its not already been done
2446    */
2447   if( !sc->irqInfo.hdl )
2448   {
2449      elnk_initialize_hardware(sc);
2450   }
2451
2452   /*
2453    * Enable the card
2454    */
2455   {
2456      u_int8_t          rxfilt;
2457
2458      /*
2459       * Cancel pending I/O
2460       */
2461
2462      /* Init our MAC address */
2463      XL_SEL_WIN(2);
2464      for (i = 0; i < ETHER_ADDR_LEN; i++)
2465      {
2466         CSR_WRITE_1(sc, XL_W2_STATION_ADDR_LO + i, sc->arpcom.ac_enaddr[i]);
2467      }
2468
2469      /* Clear the station mask. */
2470      for (i = 0; i < 3; i++)
2471         CSR_WRITE_2(sc, XL_W2_STATION_MASK_LO + (i * 2), 0);
2472
2473      /*
2474       * Set the TX freethresh value.
2475       * Note that this has no effect on 3c905B "cyclone"
2476       * cards but is required for 3c900/3c905 "boomerang"
2477       * cards in order to enable the download engine.
2478       */
2479      CSR_WRITE_1(sc, XL_TX_FREETHRESH, XL_PACKET_SIZE >> 8);
2480
2481      /* Set the TX start threshold for best performance. */
2482      sc->xl_tx_thresh = XL_MIN_FRAMELEN;
2483      CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_TX_SET_START|sc->xl_tx_thresh);
2484
2485      /*
2486       * If this is a 3c905B, also set the tx reclaim threshold.
2487       * This helps cut down on the number of tx reclaim errors
2488       * that could happen on a busy network. The chip multiplies
2489       * the register value by 16 to obtain the actual threshold
2490       * in bytes, so we divide by 16 when setting the value here.
2491       * The existing threshold value can be examined by reading
2492       * the register at offset 9 in window 5.
2493       */
2494      if (sc->xl_type == XL_TYPE_905B) {
2495         CSR_WRITE_2(sc, XL_COMMAND,
2496                     XL_CMD_SET_TX_RECLAIM|(XL_PACKET_SIZE >> 4));
2497      }
2498
2499      /* Set RX filter bits. */
2500      XL_SEL_WIN(5);
2501      rxfilt = CSR_READ_1(sc, XL_W5_RX_FILTER);
2502
2503      /* Set the individual bit to receive frames for this host only. */
2504      rxfilt |= XL_RXFILTER_INDIVIDUAL;
2505
2506      /* If we want promiscuous mode, set the allframes bit. */
2507      if (ifp->if_flags & IFF_PROMISC) {
2508         rxfilt |= XL_RXFILTER_ALLFRAMES;
2509         CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_SET_FILT|rxfilt);
2510      } else {
2511         rxfilt &= ~XL_RXFILTER_ALLFRAMES;
2512         CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_SET_FILT|rxfilt);
2513      }
2514
2515      /*
2516       * Set capture broadcast bit to capture broadcast frames.
2517       */
2518      if (ifp->if_flags & IFF_BROADCAST) {
2519         rxfilt |= XL_RXFILTER_BROADCAST;
2520         CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_SET_FILT|rxfilt);
2521      } else {
2522         rxfilt &= ~XL_RXFILTER_BROADCAST;
2523         CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_SET_FILT|rxfilt);
2524      }
2525
2526#if 0
2527      /*
2528       * Program the multicast filter, if necessary.
2529       */
2530      if (sc->xl_type == XL_TYPE_905B)
2531         xl_setmulti_hash(sc);
2532      else
2533         xl_setmulti(sc);
2534#endif
2535      /*
2536       * Load the address of the RX list. We have to
2537       * stall the upload engine before we can manipulate
2538       * the uplist pointer register, then unstall it when
2539       * we're finished. We also have to wait for the
2540       * stall command to complete before proceeding.
2541       * Note that we have to do this after any RX resets
2542       * have completed since the uplist register is cleared
2543       * by a reset.
2544       */
2545      CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_UP_STALL);
2546      xl_wait(sc);
2547      CSR_WRITE_4(sc, XL_UPLIST_PTR, phys_to_bus( sc->curr_rx_md ));
2548      CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_UP_UNSTALL);
2549      xl_wait(sc);
2550
2551
2552      if (sc->xl_type == XL_TYPE_905B) {
2553         /* Set polling interval */
2554         CSR_WRITE_1(sc, XL_DOWN_POLL, 64);
2555         xl_wait(sc);
2556      }
2557
2558      /*
2559       * If the coax transceiver is on, make sure to enable
2560       * the DC-DC converter.
2561       */
2562      XL_SEL_WIN(3);
2563      if (sc->xl_xcvr == XL_XCVR_COAX)
2564         CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_COAX_START);
2565      else
2566         CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_COAX_STOP);
2567
2568      /* increase packet size to allow reception of 802.1q or ISL packets */
2569      if (sc->xl_type == XL_TYPE_905B)
2570         CSR_WRITE_2(sc, XL_W3_MAXPKTSIZE, XL_PACKET_SIZE);
2571      /* Clear out the stats counters. */
2572
2573      memset( &sc->xl_stats, 0, sizeof(struct xl_stats));
2574
2575      CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_STATS_DISABLE);
2576      sc->xl_stats_no_timeout = 1;
2577      xl_stats_update(sc->stat_timer_id,sc);
2578      sc->xl_stats_no_timeout = 0;
2579      XL_SEL_WIN(4);
2580      CSR_WRITE_2(sc, XL_W4_NET_DIAG, XL_NETDIAG_UPPER_BYTES_ENABLE);
2581      CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_STATS_ENABLE);
2582
2583
2584      /*
2585       * Enable interrupts.
2586       */
2587      CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_INTR_ACK|0xFF);
2588      CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_STAT_ENB|XL_INTRS);
2589      CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_INTR_ENB|XL_INTRS);
2590
2591      /* Set the RX early threshold */
2592      CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_SET_THRESH|(XL_PACKET_SIZE >>2));
2593      CSR_WRITE_2(sc, XL_DMACTL, XL_DMACTL_UP_RX_EARLY );
2594
2595#if 0
2596      {
2597         unsigned32 icfg;
2598         unsigned16 mctl;
2599
2600         XL_SEL_WIN(3);
2601         icfg = CSR_READ_4( sc, XL_W3_INTERNAL_CFG );
2602         mctl = CSR_READ_2( sc, XL_W3_MAC_CTRL );
2603
2604         printk("etherlink : unit elnk%d intcfg %08x, macctl %04x\n", sc->xl_unit, icfg, mctl);
2605      }
2606#endif
2607
2608      /* Enable receiver and transmitter. */
2609      CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_TX_ENABLE);
2610      xl_wait(sc);
2611      CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_ENABLE);
2612      xl_wait(sc);
2613
2614      /* Select window 7 for normal operations. */
2615      XL_SEL_WIN(7);
2616
2617      /* schedule the stats update timer */
2618      rtems_timer_fire_after( sc->stat_timer_id, sc->stats_update_ticks, xl_stats_update, (void *)sc );
2619   }
2620
2621   /*
2622    * Tell the world that we're running.
2623    */
2624   ifp->if_flags |= IFF_RUNNING;
2625}
2626
2627
2628
2629
2630
2631
2632
2633/*
2634 * Stop the device
2635 */
2636static void
2637elnk_stop (struct elnk_softc *sc)
2638{
2639   struct ifnet *ifp = &sc->arpcom.ac_if;
2640
2641   /*
2642    * Stop the transmitter
2643    */
2644   xl_stop(sc);
2645
2646   ifp->if_flags &= ~IFF_RUNNING;
2647}
2648
2649
2650
2651
2652/*
2653 * Show interface statistics
2654 */
2655static void
2656elnk_stats (struct elnk_softc *sc)
2657{
2658   printf("    MII PHY data { anr:%04x  lpar:%04x  stat:%04x  ctl:%04x }\n",
2659          sc->xl_unit,
2660          sc->xl_stats.miianr,
2661          sc->xl_stats.miipar,
2662          sc->xl_stats.miistatus,
2663          sc->xl_stats.miicmd);
2664
2665   printf("          interrupts:%-5d            intstatus:%04x   mediastat:%04x\n",
2666          sc->xl_stats.device_interrupts,
2667          sc->xl_stats.intstatus,
2668          sc->xl_stats.mediastatus);
2669
2670   printf("            rxstatus:%04x              txstatus:%02x       smbstat:%04x\n",
2671          sc->xl_stats.rxstatus,
2672          sc->xl_stats.txstatus,
2673          sc->xl_stats.smbstatus);
2674
2675   printf("        carrier_lost:%-5d             sqe_errs:%-5d\n",   
2676          sc->xl_stats.xl_carrier_lost,
2677          sc->xl_stats.xl_sqe_errs);
2678
2679   printf("  tx_multi_collision:%-5d  tx_single_collision:%-5d\n",   
2680          sc->xl_stats.xl_tx_multi_collision,
2681          sc->xl_stats.xl_tx_single_collision);
2682
2683   printf("   tx_late_collision:%-5d           rx_overrun:%-5d\n",   
2684          sc->xl_stats.xl_tx_late_collision,
2685          sc->xl_stats.xl_rx_overrun);
2686
2687   printf("         tx_deferred:%-5d               badssd:%-5d\n",   
2688          sc->xl_stats.xl_tx_deferred,
2689          sc->xl_stats.xl_badssd);
2690
2691   printf("        rx_frames_ok:%-5d         tx_frames_ok:%-5d\n",   
2692          sc->xl_stats.xl_rx_frames_ok + ((sc->xl_stats.xl_upper_frames_ok & 3) << 16),
2693          sc->xl_stats.xl_tx_frames_ok + (((sc->xl_stats.xl_upper_frames_ok >> 4) & 3) << 16) );
2694
2695   printf("         rx_bytes_ok:%-5d          tx_bytes_ok:%-5d\n",   
2696          sc->xl_stats.xl_rx_bytes_ok + ((sc->xl_stats.xl_upper_bytes_ok & 7) << 16),
2697          sc->xl_stats.xl_tx_bytes_ok + (((sc->xl_stats.xl_upper_bytes_ok >> 4) & 7) << 16) );
2698}
2699
2700
2701
2702
2703
2704
2705
2706/*
2707 * Driver ioctl handler
2708 */
2709static int
2710elnk_ioctl (struct ifnet *ifp, int command, caddr_t data)
2711{
2712   struct elnk_softc *sc = ifp->if_softc;
2713   int error = 0;
2714
2715   switch (command) {
2716      case SIOCGIFADDR:
2717      case SIOCSIFADDR:
2718         ether_ioctl (ifp, command, data);
2719         break;
2720
2721      case SIOCSIFFLAGS:
2722         switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
2723            case IFF_RUNNING:
2724               elnk_stop (sc);
2725               break;
2726
2727            case IFF_UP:
2728               elnk_init (sc);
2729               break;
2730
2731            case IFF_UP | IFF_RUNNING:
2732               elnk_stop (sc);
2733               elnk_init (sc);
2734               break;
2735
2736            default:
2737               break;
2738         }
2739         break;
2740
2741      case SIO_RTEMS_SHOW_STATS:
2742         elnk_stats (sc);
2743         break;
2744               
2745         /*
2746          * FIXME: All sorts of multicast commands need to be added here!
2747          */
2748      default:
2749         error = EINVAL;
2750         break;
2751   }
2752
2753   return error;
2754}
2755
2756
2757
2758
2759
2760
2761
2762
2763#if 0
2764static int iftap(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m )
2765{
2766   int i;
2767   char *delim, *pkt;
2768
2769   printk("unit %i, src ", ifp->if_unit );
2770   for(delim= "", i=0; i< ETHER_ADDR_LEN; i++, delim=":")
2771      printk("%s%02x", delim, (char) eh->ether_shost[i] );
2772
2773   printk(" dest ");
2774
2775   for(delim= "", i=0; i< ETHER_ADDR_LEN; i++, delim=":")
2776      printk("%s%02x", delim, (char) eh->ether_dhost[i] );
2777   printk("  pkt ");
2778
2779   pkt = (char *)eh;
2780   for(delim="", i=0; i < sizeof(struct ether_header); i++, delim=":")
2781      printk("%s%02x", delim, (char) pkt[i] );
2782
2783   printk("\n");
2784   return 0;
2785}
2786#endif
2787
2788
2789
2790struct el_boards
2791{
2792      int pbus,pdev,pfun, vid, did, tindex;
2793};
2794
2795
2796
2797/*
2798 * Attach an ELNK driver to the system
2799 */
2800int
2801rtems_elnk_driver_attach (struct rtems_bsdnet_ifconfig *config, int attach)
2802{
2803   struct elnk_softc *sc;
2804   struct ifnet *ifp;
2805   char         *unitName;
2806   int          unitNumber;
2807   int          mtu, i;
2808   unsigned char cvalue;
2809   int          pbus, pdev, pfun;
2810   unsigned int lvalue;
2811   struct el_boards         sysboards[NUM_UNITS];
2812   int                      numFound = 0;
2813
2814
2815   /*
2816    * Get the instance number for the board we're going to configure
2817    * from the user.
2818    */
2819   if( (unitNumber = rtems_bsdnet_parse_driver_name( config, &unitName)) == -1 )
2820   {
2821      return 0;
2822   }
2823
2824   if( strcmp(unitName, DRIVER_PREFIX) )
2825   {
2826      printk("etherlink : invalid unit name '%s'\n", unitName );
2827      return 0;
2828   }
2829
2830   if ((unitNumber < 1) || (unitNumber > NUM_UNITS))
2831   {
2832      printk("etherlink : unit %i is invalid, must be (1 <= n <= %d)\n", unitNumber, NUM_UNITS);
2833      return 0;
2834   }
2835
2836
2837   {
2838      int  done= 0, unum;
2839
2840      /*
2841       * Run thru the list of boards, finding all that are present in
2842       * the system.  Sort by slot,dev - and then use the unitNumber-1
2843       * to index the list and select the device.  Yucky.
2844       */
2845      for( i=0; !done && xl_devs[i].xl_vid; i++)
2846      {
2847         for(unum= 1;
2848             !done && BSP_pciFindDevice( xl_devs[i].xl_vid, xl_devs[i].xl_did, unum-1,
2849                                         &sysboards[numFound].pbus,
2850                                         &sysboards[numFound].pdev,
2851                                         &sysboards[numFound].pfun)==0;
2852             unum++)
2853         {
2854            if( numFound == NUM_UNITS )
2855            {
2856               printk("etherlink : Maximum of %d units found, extra devices ignored.\n", NUM_UNITS );
2857               done=-1;
2858            }
2859            else
2860            {
2861               sysboards[numFound].vid = xl_devs[i].xl_vid;
2862               sysboards[numFound].did = xl_devs[i].xl_did;
2863               sysboards[numFound].tindex = i;
2864               ++numFound;
2865            }
2866         }
2867      }
2868
2869      if( ! numFound )
2870      {
2871         printk("etherlink : No Etherlink devices found\n");
2872         return 0;
2873      }
2874
2875      if( unitNumber-1 >= numFound )
2876      {
2877         printk("etherlink : device '%s' not found\n", config->name );
2878         return 0;
2879      }
2880
2881      /*
2882      * Got the list of etherlink boards in the system, now sort by
2883      * slot,device.  bubble sorts aren't all that wonderful, but this
2884      * is a short & infrequently sorted list.
2885      */
2886      if( numFound > 1 )
2887      {
2888         struct el_boards       tboard;
2889         int                    didsort;
2890
2891         do
2892         {
2893            didsort = 0;
2894
2895            for(i=1; i<numFound; i++)
2896            {
2897               if( sysboards[i-1].pbus > sysboards[i].pbus ||
2898                   (sysboards[i-1].pbus == sysboards[i].pbus && sysboards[i-1].pdev > sysboards[i].pdev) )
2899               {
2900                  memcpy(&tboard, &sysboards[i-1], sizeof(struct el_boards));
2901                  memcpy(&sysboards[i-1], &sysboards[i], sizeof(struct el_boards));
2902                  memcpy(&sysboards[i], &tboard, sizeof(struct el_boards));
2903                  didsort++;
2904               }
2905            }
2906         }
2907         while( didsort );
2908      }
2909
2910      /*
2911      ** board list is sorted, now select the unit
2912      */
2913
2914      pbus = sysboards[unitNumber-1].pbus;
2915      pdev = sysboards[unitNumber-1].pdev;
2916      pfun = sysboards[unitNumber-1].pfun;
2917   }
2918
2919   sc = &elnk_softc[unitNumber - 1];
2920   ifp = &sc->arpcom.ac_if;
2921   if (ifp->if_softc != NULL)
2922   {
2923      printk("etherlink : unit %i already in use.\n", unitNumber );
2924      return 0;
2925   }
2926
2927   /*
2928   ** Save various things
2929   */
2930   sc->xl_unit = unitNumber;
2931   sc->xl_type = sysboards[ unitNumber-1 ].tindex;
2932
2933   sc->vendorID = sysboards[numFound].vid;
2934   sc->deviceID = sysboards[numFound].did;
2935
2936   sc->numRxbuffers = (config->rbuf_count) ? config->rbuf_count : RX_RING_SIZE;
2937   sc->numTxbuffers = (config->xbuf_count) ? config->xbuf_count : TX_RING_SIZE;
2938
2939   if (config->mtu)
2940      mtu = config->mtu;
2941   else
2942      mtu = ETHERMTU;
2943
2944   sc->acceptBroadcast = !config->ignore_broadcast;
2945
2946
2947
2948#ifdef ELNK_DEBUG
2949   printk("etherlink : device '%s', name 'elnk%d', pci %02x:%02x.%02x, %d rx/%d tx buffers\n",
2950          xl_devs[sc->xl_type].xl_name, sc->xl_unit,
2951          pbus, pdev, pfun,
2952          sc->numRxbuffers, sc->numTxbuffers);
2953#endif
2954
2955
2956   /*
2957   ** Create this unit's stats timer
2958   */
2959   if( rtems_timer_create( rtems_build_name( 'X', 'L', 't', (char)(sc->xl_unit & 255)),
2960                           &sc->stat_timer_id ) != RTEMS_SUCCESSFUL )
2961   {
2962      printk("etherlink : unit elnk%d unable to create stats timer\n", sc->xl_unit );
2963      return 0;
2964   }
2965
2966   /* update stats 1 times/second if things aren't incrementing fast
2967    * enough to trigger stats interrupts
2968    */
2969   rtems_clock_get( RTEMS_CLOCK_GET_TICKS_PER_SECOND, &sc->stats_update_ticks );
2970
2971
2972   /*
2973   ** Get this unit's rx/tx event
2974   */
2975   sc->ioevent = unit_signals[unitNumber-1];
2976
2977
2978   /*
2979   ** Prep the board
2980   */
2981   pci_write_config_word(pbus, pdev, pfun,
2982                         PCI_COMMAND,
2983                         (unsigned16)( PCI_COMMAND_IO |
2984                                       PCI_COMMAND_MASTER |
2985                                       PCI_COMMAND_INVALIDATE |
2986                                       PCI_COMMAND_WAIT |
2987                                       PCI_COMMAND_FAST_BACK ) );
2988
2989   /*
2990    * Get the devices base address
2991    */
2992   pci_read_config_dword(pbus, pdev, pfun,
2993                         PCI_BASE_ADDRESS_0,
2994                         &lvalue);
2995
2996   sc->ioaddr = (unsigned32)lvalue & PCI_BASE_ADDRESS_IO_MASK;
2997       
2998
2999   /*
3000   ** Store the interrupt name, we'll use it later when we initialize
3001   ** the board.
3002   */
3003   pci_read_config_byte(pbus, pdev, pfun,
3004                        PCI_INTERRUPT_LINE,
3005                        &cvalue);
3006
3007   memset(&sc->irqInfo,0,sizeof(rtems_irq_connect_data));
3008   sc->irqInfo.name = cvalue;
3009
3010
3011   /*
3012   ** Establish basic board config, set node address from config or
3013   ** board eeprom, do stuff with additional device properties
3014   */
3015
3016#if 0
3017   {
3018      unsigned8 pci_latency;
3019      unsigned8 new_latency = 248;
3020
3021      /* Check the PCI latency value.  On the 3c590 series the latency timer
3022         must be set to the maximum value to avoid data corruption that occurs
3023         when the timer expires during a transfer.  This bug exists the Vortex
3024         chip only. */
3025      pci_read_config_byte(pbus,pdev,pfun, PCI_LATENCY_TIMER, &pci_latency);
3026      if (pci_latency < new_latency)
3027      {
3028         printk("etherlink : unit elnk%d Overriding PCI latency, timer (CFLT) setting of %d, new value is %d.\n", sc->xl_unit, pci_latency, new_latency );
3029         pci_write_config_byte(pbus,pdev,pfun, PCI_LATENCY_TIMER, new_latency);
3030      }
3031   }
3032#endif
3033
3034
3035   /* Reset the adapter. */
3036   xl_reset(sc);
3037
3038
3039   {
3040      u_int16_t         xcvr[2];
3041      u_char            eaddr[ETHER_ADDR_LEN];
3042      int               media = IFM_ETHER|IFM_100_TX|IFM_FDX;
3043
3044      sc->xl_flags = 0;
3045      if (sc->deviceID == TC_DEVICEID_HURRICANE_555)
3046         sc->xl_flags |= XL_FLAG_EEPROM_OFFSET_30 | XL_FLAG_PHYOK;
3047      if (sc->deviceID == TC_DEVICEID_HURRICANE_556 ||
3048          sc->deviceID == TC_DEVICEID_HURRICANE_556B)
3049         sc->xl_flags |= XL_FLAG_FUNCREG | XL_FLAG_PHYOK |
3050            XL_FLAG_EEPROM_OFFSET_30 | XL_FLAG_WEIRDRESET |
3051            XL_FLAG_INVERT_LED_PWR | XL_FLAG_INVERT_MII_PWR;
3052      if (sc->deviceID == TC_DEVICEID_HURRICANE_555 ||
3053          sc->deviceID == TC_DEVICEID_HURRICANE_556)
3054         sc->xl_flags |= XL_FLAG_8BITROM;
3055      if (sc->deviceID == TC_DEVICEID_HURRICANE_556B)
3056         sc->xl_flags |= XL_FLAG_NO_XCVR_PWR;
3057
3058      if (sc->deviceID == TC_DEVICEID_HURRICANE_575A ||
3059          sc->deviceID == TC_DEVICEID_HURRICANE_575B ||
3060          sc->deviceID == TC_DEVICEID_HURRICANE_575C ||
3061          sc->deviceID == TC_DEVICEID_HURRICANE_656B ||
3062          sc->deviceID == TC_DEVICEID_TORNADO_656C)
3063         sc->xl_flags |= XL_FLAG_FUNCREG | XL_FLAG_PHYOK |
3064            XL_FLAG_EEPROM_OFFSET_30 | XL_FLAG_8BITROM;
3065      if (sc->deviceID == TC_DEVICEID_HURRICANE_656)
3066         sc->xl_flags |= XL_FLAG_FUNCREG | XL_FLAG_PHYOK;
3067      if (sc->deviceID == TC_DEVICEID_HURRICANE_575B)
3068         sc->xl_flags |= XL_FLAG_INVERT_LED_PWR;
3069      if (sc->deviceID == TC_DEVICEID_HURRICANE_575C)
3070         sc->xl_flags |= XL_FLAG_INVERT_MII_PWR;
3071      if (sc->deviceID == TC_DEVICEID_TORNADO_656C)
3072         sc->xl_flags |= XL_FLAG_INVERT_MII_PWR;
3073      if (sc->deviceID == TC_DEVICEID_HURRICANE_656 ||
3074          sc->deviceID == TC_DEVICEID_HURRICANE_656B)
3075         sc->xl_flags |= XL_FLAG_INVERT_MII_PWR |
3076            XL_FLAG_INVERT_LED_PWR;
3077      if (sc->deviceID == TC_DEVICEID_TORNADO_10_100BT_920B)
3078         sc->xl_flags |= XL_FLAG_PHYOK;
3079
3080     
3081      if (config->hardware_address)
3082      {
3083         memcpy(sc->arpcom.ac_enaddr, config->hardware_address, ETHER_ADDR_LEN);
3084      }
3085      else
3086      {
3087         if (xl_read_eeprom(sc, (caddr_t)&eaddr, XL_EE_OEM_ADR0, 3, 1))
3088         {
3089            printk("etherlink : unit elnk%d Failed to read station address\n", sc->xl_unit );
3090            return 0;
3091         }
3092         memcpy((char *)&sc->arpcom.ac_enaddr, eaddr, ETHER_ADDR_LEN);
3093      }
3094
3095      /*
3096       * Figure out the card type. 3c905B adapters have the
3097       * 'supportsNoTxLength' bit set in the capabilities
3098       * word in the EEPROM.
3099       */
3100      xl_read_eeprom(sc, (caddr_t)&sc->xl_caps, XL_EE_CAPS, 1, 0);
3101      if (sc->xl_caps & XL_CAPS_NO_TXLENGTH)
3102         sc->xl_type = XL_TYPE_905B;
3103      else
3104         sc->xl_type = XL_TYPE_90X;
3105
3106
3107      /*
3108       * Now we have to see what sort of media we have.
3109       * This includes probing for an MII interace and a
3110       * possible PHY.
3111       */
3112      XL_SEL_WIN(3);
3113      sc->xl_media = CSR_READ_2(sc, XL_W3_MEDIA_OPT);
3114
3115      xl_read_eeprom(sc, (char *)&xcvr, XL_EE_ICFG_0, 2, 0);
3116      sc->xl_xcvr = xcvr[0] | xcvr[1] << 16;
3117      sc->xl_xcvr &= XL_ICFG_CONNECTOR_MASK;
3118      sc->xl_xcvr >>= XL_ICFG_CONNECTOR_BITS;
3119
3120#if 0
3121      printk("etherlink : unit elnk%d EEPROM set xcvr to 0x%x\n", sc->xl_unit, sc->xl_xcvr);
3122#endif
3123
3124      {
3125         char   msg[255];
3126         int    i;
3127
3128         struct _availmedia
3129         {
3130               int bit;
3131               char *name;
3132         } _am[]= {{ XL_MEDIAOPT_BT4, "100BaseT4" },
3133                   { XL_MEDIAOPT_BTX, "100BaseTX" },
3134                   { XL_MEDIAOPT_BFX, "100BaseFX" },
3135                   { XL_MEDIAOPT_BT,  "10BaseT" },
3136                   { XL_MEDIAOPT_BNC, "10Base2" },
3137                   { XL_MEDIAOPT_AUI, "10mbps AUI"},
3138                   { XL_MEDIAOPT_MII, "MII"},
3139                   { 0, NULL }};
3140
3141         msg[0]= 0;
3142         for( i=0; _am[i].bit; i++)
3143         {
3144            if( sc->xl_media & _am[i].bit )
3145               sprintf( &msg[strlen(msg)], ",%s", _am[i].name );
3146         }
3147         if( !strlen(msg) ) strcpy( &msg[1], "<no media bits>");
3148
3149         printk("etherlink : unit elnk%d available media : %s\n", sc->xl_unit, &msg[1]);
3150      }
3151                   
3152      xl_mediacheck(sc);
3153
3154#if 0
3155/* this has already been called by xl_mediacheck()- hopefully
3156   I've not messed something up here */
3157      /*
3158       * Sanity check. If the user has selected "auto" and this isn't
3159       * a 10/100 card of some kind, we need to force the transceiver
3160       * type to something sane.
3161       */
3162      if (sc->xl_xcvr == XL_XCVR_AUTO)
3163         xl_choose_xcvr(sc, 1);
3164#endif
3165
3166
3167      /* Choose a default media. */
3168      switch(sc->xl_xcvr) {
3169         case XL_XCVR_10BT:
3170            media = IFM_ETHER|IFM_10_T;
3171            xl_setmode(sc, media);
3172            break;
3173         case XL_XCVR_AUI:
3174            if (sc->xl_type == XL_TYPE_905B &&
3175                sc->xl_media == XL_MEDIAOPT_10FL) {
3176               media = IFM_ETHER|IFM_10_FL;
3177               xl_setmode(sc, media);
3178            } else {
3179               media = IFM_ETHER|IFM_10_5;
3180               xl_setmode(sc, media);
3181            }
3182            break;
3183         case XL_XCVR_COAX:
3184            media = IFM_ETHER|IFM_10_2;
3185            xl_setmode(sc, media);
3186            break;
3187         case XL_XCVR_AUTO:
3188         case XL_XCVR_100BTX:
3189            xl_setcfg(sc);
3190            break;
3191         case XL_XCVR_MII:
3192            printk("etherlink : unit elnk%d MII media not supported!\n", sc->xl_unit);
3193            break;
3194         case XL_XCVR_100BFX:
3195            media = IFM_ETHER|IFM_100_FX;
3196            break;
3197         default:
3198            printk("etherlink : unit elnk%d unknown XCVR type: %d\n", sc->xl_unit, sc->xl_xcvr);
3199            /*
3200             * This will probably be wrong, but it prevents
3201             * the ifmedia code from panicking.
3202             */
3203            media = IFM_ETHER|IFM_10_T;
3204            break;
3205      }
3206
3207
3208      if (sc->xl_flags & XL_FLAG_NO_XCVR_PWR) {
3209         XL_SEL_WIN(0);
3210         CSR_WRITE_2(sc, XL_W0_MFG_ID, XL_NO_XCVR_PWR_MAGICBITS);
3211      }
3212
3213      XL_SEL_WIN(7);
3214   }
3215
3216
3217
3218   /*
3219    * Set up network interface
3220    */
3221   ifp->if_softc = sc;
3222   ifp->if_name = unitName;
3223   ifp->if_unit = sc->xl_unit;
3224   ifp->if_mtu = mtu;
3225   ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
3226   if (ifp->if_snd.ifq_maxlen == 0)
3227      ifp->if_snd.ifq_maxlen = ifqmaxlen;
3228   ifp->if_init = elnk_init;
3229   ifp->if_start = elnk_start;
3230   ifp->if_ioctl = elnk_ioctl;
3231   ifp->if_output = ether_output;
3232
3233#if 0
3234   ifp->if_tap = iftap;
3235#endif
3236
3237   /*
3238    * Attach the interface
3239    */
3240   if_attach (ifp);
3241   ether_ifattach (ifp);
3242
3243#ifdef ELNK_DEBUG
3244   printk( "etherlink : unit elnk%d driver attached\n", sc->xl_unit );
3245#endif
3246
3247   /*
3248    * Start driver tasks if this is the first unit initialized
3249    */
3250   if (txDaemonTid == 0)
3251   {
3252      rxDaemonTid = rtems_bsdnet_newproc( "XLrx", 4096,
3253                                          elnk_rxDaemon, NULL);
3254     
3255      txDaemonTid = rtems_bsdnet_newproc( "XLtx", 4096,
3256                                          elnk_txDaemon, NULL);
3257#ifdef ELNK_DEBUG
3258      printk( "etherlink : driver tasks created\n" );
3259#endif
3260   }
3261
3262   return 1;
3263};
3264
3265#endif /* ELNK_SUPPORTED */
3266
3267/* eof */
Note: See TracBrowser for help on using the repository browser.