Warnings purge of drivers (continued)
[gpxe.git] / src / drivers / net / sundance.c
blob32c9453eb568b305545a6ba707c2885ab2f56d21
1 /**************************************************************************
3 * sundance.c -- Etherboot device driver for the Sundance ST201 "Alta".
4 * Written 2002-2002 by Timothy Legge <tlegge@rogers.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * Portions of this code based on:
21 * sundance.c: A Linux device driver for the Sundance ST201 "Alta"
22 * Written 1999-2002 by Donald Becker
24 * tulip.c: Tulip and Clone Etherboot Driver
25 * By Marty Conner
26 * Copyright (C) 2001 Entity Cyber, Inc.
28 * Linux Driver Version LK1.09a, 10-Jul-2003 (2.4.25)
30 * REVISION HISTORY:
31 * ================
32 * v1.1 01-01-2003 timlegge Initial implementation
33 * v1.7 04-10-2003 timlegge Transfers Linux Kernel (30 sec)
34 * v1.8 04-13-2003 timlegge Fix multiple transmission bug
35 * v1.9 08-19-2003 timlegge Support Multicast
36 * v1.10 01-17-2004 timlegge Initial driver output cleanup
37 * v1.11 03-21-2004 timlegge Remove unused variables
38 * v1.12 03-21-2004 timlegge Remove excess MII defines
39 * v1.13 03-24-2004 timlegge Update to Linux 2.4.25 driver
41 ****************************************************************************/
43 /* to get some global routines like printf */
44 #include "etherboot.h"
45 /* to get the interface to the body of the program */
46 #include "nic.h"
47 /* to get the PCI support functions, if this is a PCI NIC */
48 #include <gpxe/pci.h>
49 #include "timer.h"
50 #include "mii.h"
52 #define drv_version "v1.12"
53 #define drv_date "2004-03-21"
55 #define HZ 100
57 /* Condensed operations for readability. */
58 #define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))
59 #define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))
61 /* May need to be moved to mii.h */
62 struct mii_if_info {
63 int phy_id;
64 int advertising;
65 unsigned int full_duplex:1; /* is full duplex? */
68 /* Set the mtu */
69 static int mtu = 1514;
71 /* Maximum number of multicast addresses to filter (vs. rx-all-multicast).
72 The sundance uses a 64 element hash table based on the Ethernet CRC. */
73 // static int multicast_filter_limit = 32;
75 /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
76 Setting to > 1518 effectively disables this feature.
77 This chip can receive into any byte alignment buffers, so word-oriented
78 archs do not need a copy-align of the IP header. */
79 static int rx_copybreak = 0;
80 static int flowctrl = 1;
82 /* Allow forcing the media type */
83 /* media[] specifies the media type the NIC operates at.
84 autosense Autosensing active media.
85 10mbps_hd 10Mbps half duplex.
86 10mbps_fd 10Mbps full duplex.
87 100mbps_hd 100Mbps half duplex.
88 100mbps_fd 100Mbps full duplex.
90 static char media[] = "autosense";
92 /* Operational parameters that are set at compile time. */
94 /* As Etherboot uses a Polling driver we can keep the number of rings
95 to the minimum number required. In general that is 1 transmit and 4 receive receive rings. However some cards require that
96 there be a minimum of 2 rings */
97 #define TX_RING_SIZE 2
98 #define TX_QUEUE_LEN 10 /* Limit ring entries actually used. */
99 #define RX_RING_SIZE 4
102 /* Operational parameters that usually are not changed. */
103 /* Time in jiffies before concluding the transmitter is hung. */
104 #define TX_TIME_OUT (4*HZ)
105 #define PKT_BUF_SZ 1536
107 /* Offsets to the device registers.
108 Unlike software-only systems, device drivers interact with complex hardware.
109 It's not useful to define symbolic names for every register bit in the
110 device. The name can only partially document the semantics and make
111 the driver longer and more difficult to read.
112 In general, only the important configuration values or bits changed
113 multiple times should be defined symbolically.
115 enum alta_offsets {
116 DMACtrl = 0x00,
117 TxListPtr = 0x04,
118 TxDMABurstThresh = 0x08,
119 TxDMAUrgentThresh = 0x09,
120 TxDMAPollPeriod = 0x0a,
121 RxDMAStatus = 0x0c,
122 RxListPtr = 0x10,
123 DebugCtrl0 = 0x1a,
124 DebugCtrl1 = 0x1c,
125 RxDMABurstThresh = 0x14,
126 RxDMAUrgentThresh = 0x15,
127 RxDMAPollPeriod = 0x16,
128 LEDCtrl = 0x1a,
129 ASICCtrl = 0x30,
130 EEData = 0x34,
131 EECtrl = 0x36,
132 TxStartThresh = 0x3c,
133 RxEarlyThresh = 0x3e,
134 FlashAddr = 0x40,
135 FlashData = 0x44,
136 TxStatus = 0x46,
137 TxFrameId = 0x47,
138 DownCounter = 0x18,
139 IntrClear = 0x4a,
140 IntrEnable = 0x4c,
141 IntrStatus = 0x4e,
142 MACCtrl0 = 0x50,
143 MACCtrl1 = 0x52,
144 StationAddr = 0x54,
145 MaxFrameSize = 0x5A,
146 RxMode = 0x5c,
147 MIICtrl = 0x5e,
148 MulticastFilter0 = 0x60,
149 MulticastFilter1 = 0x64,
150 RxOctetsLow = 0x68,
151 RxOctetsHigh = 0x6a,
152 TxOctetsLow = 0x6c,
153 TxOctetsHigh = 0x6e,
154 TxFramesOK = 0x70,
155 RxFramesOK = 0x72,
156 StatsCarrierError = 0x74,
157 StatsLateColl = 0x75,
158 StatsMultiColl = 0x76,
159 StatsOneColl = 0x77,
160 StatsTxDefer = 0x78,
161 RxMissed = 0x79,
162 StatsTxXSDefer = 0x7a,
163 StatsTxAbort = 0x7b,
164 StatsBcastTx = 0x7c,
165 StatsBcastRx = 0x7d,
166 StatsMcastTx = 0x7e,
167 StatsMcastRx = 0x7f,
168 /* Aliased and bogus values! */
169 RxStatus = 0x0c,
171 enum ASICCtrl_HiWord_bit {
172 GlobalReset = 0x0001,
173 RxReset = 0x0002,
174 TxReset = 0x0004,
175 DMAReset = 0x0008,
176 FIFOReset = 0x0010,
177 NetworkReset = 0x0020,
178 HostReset = 0x0040,
179 ResetBusy = 0x0400,
182 /* Bits in the interrupt status/mask registers. */
183 enum intr_status_bits {
184 IntrSummary = 0x0001, IntrPCIErr = 0x0002, IntrMACCtrl = 0x0008,
185 IntrTxDone = 0x0004, IntrRxDone = 0x0010, IntrRxStart = 0x0020,
186 IntrDrvRqst = 0x0040,
187 StatsMax = 0x0080, LinkChange = 0x0100,
188 IntrTxDMADone = 0x0200, IntrRxDMADone = 0x0400,
191 /* Bits in the RxMode register. */
192 enum rx_mode_bits {
193 AcceptAllIPMulti = 0x20, AcceptMultiHash = 0x10, AcceptAll = 0x08,
194 AcceptBroadcast = 0x04, AcceptMulticast = 0x02, AcceptMyPhys =
195 0x01,
197 /* Bits in MACCtrl. */
198 enum mac_ctrl0_bits {
199 EnbFullDuplex = 0x20, EnbRcvLargeFrame = 0x40,
200 EnbFlowCtrl = 0x100, EnbPassRxCRC = 0x200,
202 enum mac_ctrl1_bits {
203 StatsEnable = 0x0020, StatsDisable = 0x0040, StatsEnabled = 0x0080,
204 TxEnable = 0x0100, TxDisable = 0x0200, TxEnabled = 0x0400,
205 RxEnable = 0x0800, RxDisable = 0x1000, RxEnabled = 0x2000,
208 /* The Rx and Tx buffer descriptors.
209 Using only 32 bit fields simplifies software endian correction.
210 This structure must be aligned, and should avoid spanning cache lines.
212 struct netdev_desc {
213 u32 next_desc;
214 u32 status;
215 u32 addr;
216 u32 length;
219 /* Bits in netdev_desc.status */
220 enum desc_status_bits {
221 DescOwn = 0x8000,
222 DescEndPacket = 0x4000,
223 DescEndRing = 0x2000,
224 LastFrag = 0x80000000,
225 DescIntrOnTx = 0x8000,
226 DescIntrOnDMADone = 0x80000000,
227 DisableAlign = 0x00000001,
230 /**********************************************
231 * Descriptor Ring and Buffer defination
232 ***********************************************/
233 /* Define the TX Descriptor */
234 static struct netdev_desc tx_ring[TX_RING_SIZE];
236 /* Define the RX Descriptor */
237 static struct netdev_desc rx_ring[RX_RING_SIZE];
239 /* Create a static buffer of size PKT_BUF_SZ for each RX and TX descriptor.
240 All descriptors point to a part of this buffer */
241 struct {
242 unsigned char txb[PKT_BUF_SZ * TX_RING_SIZE];
243 unsigned char rxb[RX_RING_SIZE * PKT_BUF_SZ];
244 } rx_tx_buf __shared;
245 #define rxb rx_tx_buf.rxb
246 #define txb rx_tx_buf.txb
248 /* FIXME: Move BASE to the private structure */
249 static u32 BASE;
250 #define EEPROM_SIZE 128
252 enum pci_id_flags_bits {
253 PCI_USES_IO = 1, PCI_USES_MEM = 2, PCI_USES_MASTER = 4,
254 PCI_ADDR0 = 0 << 4, PCI_ADDR1 = 1 << 4, PCI_ADDR2 =
255 2 << 4, PCI_ADDR3 = 3 << 4,
258 enum chip_capability_flags { CanHaveMII = 1, KendinPktDropBug = 2, };
259 #define PCI_IOTYPE (PCI_USES_MASTER | PCI_USES_IO | PCI_ADDR0)
261 #define MII_CNT 4
262 static struct sundance_private {
263 const char *nic_name;
264 /* Frequently used values */
266 unsigned int cur_rx; /* Producer/consumer ring indicies */
267 unsigned int mtu;
269 /* These values keep track of the tranceiver/media in use */
270 unsigned int flowctrl:1;
271 unsigned int an_enable:1;
273 unsigned int speed;
275 /* MII tranceiver section */
276 struct mii_if_info mii_if;
277 int mii_preamble_required;
278 unsigned char phys[MII_CNT];
279 unsigned char pci_rev_id;
280 } sdx;
282 static struct sundance_private *sdc;
284 /* Station Address location within the EEPROM */
285 #define EEPROM_SA_OFFSET 0x10
286 #define DEFAULT_INTR (IntrRxDMADone | IntrPCIErr | \
287 IntrDrvRqst | IntrTxDone | StatsMax | \
288 LinkChange)
290 static int eeprom_read(long ioaddr, int location);
291 static int mdio_read(struct nic *nic, int phy_id, unsigned int location);
292 static void mdio_write(struct nic *nic, int phy_id, unsigned int location,
293 int value);
294 static void set_rx_mode(struct nic *nic);
296 static void check_duplex(struct nic *nic)
298 int mii_lpa = mdio_read(nic, sdc->phys[0], MII_LPA);
299 int negotiated = mii_lpa & sdc->mii_if.advertising;
300 int duplex;
302 /* Force media */
303 if (!sdc->an_enable || mii_lpa == 0xffff) {
304 if (sdc->mii_if.full_duplex)
305 outw(inw(BASE + MACCtrl0) | EnbFullDuplex,
306 BASE + MACCtrl0);
307 return;
310 /* Autonegotiation */
311 duplex = (negotiated & 0x0100) || (negotiated & 0x01C0) == 0x0040;
312 if (sdc->mii_if.full_duplex != duplex) {
313 sdc->mii_if.full_duplex = duplex;
314 DBG ("%s: Setting %s-duplex based on MII #%d "
315 "negotiated capability %4.4x.\n", sdc->nic_name,
316 duplex ? "full" : "half", sdc->phys[0],
317 negotiated );
318 outw(inw(BASE + MACCtrl0) | duplex ? 0x20 : 0,
319 BASE + MACCtrl0);
324 /**************************************************************************
325 * init_ring - setup the tx and rx descriptors
326 *************************************************************************/
327 static void init_ring(struct nic *nic __unused)
329 int i;
331 sdc->cur_rx = 0;
333 /* Initialize all the Rx descriptors */
334 for (i = 0; i < RX_RING_SIZE; i++) {
335 rx_ring[i].next_desc = virt_to_le32desc(&rx_ring[i + 1]);
336 rx_ring[i].status = 0;
337 rx_ring[i].length = 0;
338 rx_ring[i].addr = 0;
341 /* Mark the last entry as wrapping the ring */
342 rx_ring[i - 1].next_desc = virt_to_le32desc(&rx_ring[0]);
344 for (i = 0; i < RX_RING_SIZE; i++) {
345 rx_ring[i].addr = virt_to_le32desc(&rxb[i * PKT_BUF_SZ]);
346 rx_ring[i].length = cpu_to_le32(PKT_BUF_SZ | LastFrag);
349 /* We only use one transmit buffer, but two
350 * descriptors so transmit engines have somewhere
351 * to point should they feel the need */
352 tx_ring[0].status = 0x00000000;
353 tx_ring[0].addr = virt_to_bus(&txb[0]);
354 tx_ring[0].next_desc = 0; /* virt_to_bus(&tx_ring[1]); */
356 /* This descriptor is never used */
357 tx_ring[1].status = 0x00000000;
358 tx_ring[1].addr = 0; /*virt_to_bus(&txb[0]); */
359 tx_ring[1].next_desc = 0;
361 /* Mark the last entry as wrapping the ring,
362 * though this should never happen */
363 tx_ring[1].length = cpu_to_le32(LastFrag | PKT_BUF_SZ);
366 /**************************************************************************
367 * RESET - Reset Adapter
368 * ***********************************************************************/
369 static void sundance_reset(struct nic *nic)
371 int i;
373 init_ring(nic);
375 outl(virt_to_le32desc(&rx_ring[0]), BASE + RxListPtr);
376 /* The Tx List Pointer is written as packets are queued */
378 /* Initialize other registers. */
379 /* __set_mac_addr(dev); */
381 u16 addr16;
383 addr16 = (nic->node_addr[0] | (nic->node_addr[1] << 8));
384 outw(addr16, BASE + StationAddr);
385 addr16 = (nic->node_addr[2] | (nic->node_addr[3] << 8));
386 outw(addr16, BASE + StationAddr + 2);
387 addr16 = (nic->node_addr[4] | (nic->node_addr[5] << 8));
388 outw(addr16, BASE + StationAddr + 4);
391 outw(sdc->mtu + 14, BASE + MaxFrameSize);
392 if (sdc->mtu > 2047) /* this will never happen with default options */
393 outl(inl(BASE + ASICCtrl) | 0x0c, BASE + ASICCtrl);
395 set_rx_mode(nic);
397 outw(0, BASE + DownCounter);
398 /* Set the chip to poll every N*30nsec */
399 outb(100, BASE + RxDMAPollPeriod);
401 /* Fix DFE-580TX packet drop issue */
402 if (sdc->pci_rev_id >= 0x14)
403 writeb(0x01, BASE + DebugCtrl1);
405 outw(RxEnable | TxEnable, BASE + MACCtrl1);
407 /* Construct a perfect filter frame with the mac address as first match
408 * and broadcast for all others */
409 for (i = 0; i < 192; i++)
410 txb[i] = 0xFF;
412 txb[0] = nic->node_addr[0];
413 txb[1] = nic->node_addr[1];
414 txb[2] = nic->node_addr[2];
415 txb[3] = nic->node_addr[3];
416 txb[4] = nic->node_addr[4];
417 txb[5] = nic->node_addr[5];
419 DBG ( "%s: Done sundance_reset, status: Rx %hX Tx %hX "
420 "MAC Control %hX, %hX %hX\n",
421 sdc->nic_name, (int) inl(BASE + RxStatus),
422 (int) inw(BASE + TxStatus), (int) inl(BASE + MACCtrl0),
423 (int) inw(BASE + MACCtrl1), (int) inw(BASE + MACCtrl0) );
426 /**************************************************************************
427 IRQ - Wait for a frame
428 ***************************************************************************/
429 static void sundance_irq ( struct nic *nic, irq_action_t action ) {
430 unsigned int intr_status;
432 switch ( action ) {
433 case DISABLE :
434 case ENABLE :
435 intr_status = inw(nic->ioaddr + IntrStatus);
436 intr_status = intr_status & ~DEFAULT_INTR;
437 if ( action == ENABLE )
438 intr_status = intr_status | DEFAULT_INTR;
439 outw(intr_status, nic->ioaddr + IntrEnable);
440 break;
441 case FORCE :
442 outw(0x0200, BASE + ASICCtrl);
443 break;
446 /**************************************************************************
447 POLL - Wait for a frame
448 ***************************************************************************/
449 static int sundance_poll(struct nic *nic, int retreive)
451 /* return true if there's an ethernet packet ready to read */
452 /* nic->packet should contain data on return */
453 /* nic->packetlen should contain length of data */
454 int entry = sdc->cur_rx % RX_RING_SIZE;
455 u32 frame_status = le32_to_cpu(rx_ring[entry].status);
456 int intr_status;
457 int pkt_len = 0;
459 if (!(frame_status & DescOwn))
460 return 0;
462 /* There is a packet ready */
463 if(!retreive)
464 return 1;
466 intr_status = inw(nic->ioaddr + IntrStatus);
467 outw(intr_status, nic->ioaddr + IntrStatus);
469 pkt_len = frame_status & 0x1fff;
471 if (frame_status & 0x001f4000) {
472 DBG ( "Polling frame_status error\n" ); /* Do we really care about this */
473 } else {
474 if (pkt_len < rx_copybreak) {
475 /* FIXME: What should happen Will this ever occur */
476 printf("Poll Error: pkt_len < rx_copybreak");
477 } else {
478 nic->packetlen = pkt_len;
479 memcpy(nic->packet, rxb +
480 (sdc->cur_rx * PKT_BUF_SZ), nic->packetlen);
484 rx_ring[entry].length = cpu_to_le32(PKT_BUF_SZ | LastFrag);
485 rx_ring[entry].status = 0;
486 entry++;
487 sdc->cur_rx = entry % RX_RING_SIZE;
488 outw(DEFAULT_INTR & ~(IntrRxDone|IntrRxDMADone),
489 nic->ioaddr + IntrStatus);
490 return 1;
493 /**************************************************************************
494 TRANSMIT - Transmit a frame
495 ***************************************************************************/
496 static void sundance_transmit(struct nic *nic, const char *d, /* Destination */
497 unsigned int t, /* Type */
498 unsigned int s, /* size */
499 const char *p)
500 { /* Packet */
501 u16 nstype;
502 u32 to;
504 /* Disable the Tx */
505 outw(TxDisable, BASE + MACCtrl1);
507 memcpy(txb, d, ETH_ALEN);
508 memcpy(txb + ETH_ALEN, nic->node_addr, ETH_ALEN);
509 nstype = htons((u16) t);
510 memcpy(txb + 2 * ETH_ALEN, (u8 *) & nstype, 2);
511 memcpy(txb + ETH_HLEN, p, s);
513 s += ETH_HLEN;
514 s &= 0x0FFF;
515 while (s < ETH_ZLEN)
516 txb[s++] = '\0';
518 /* Setup the transmit descriptor */
519 tx_ring[0].length = cpu_to_le32(s | LastFrag);
520 tx_ring[0].status = cpu_to_le32(0x00000001);
522 /* Point to transmit descriptor */
523 outl(virt_to_le32desc(&tx_ring[0]), BASE + TxListPtr);
525 /* Enable Tx */
526 outw(TxEnable, BASE + MACCtrl1);
527 /* Trigger an immediate send */
528 outw(0, BASE + TxStatus);
530 to = currticks() + TX_TIME_OUT;
531 while (!(tx_ring[0].status & 0x00010000) && (currticks() < to)); /* wait */
533 if (currticks() >= to) {
534 printf("TX Time Out");
536 /* Disable Tx */
537 outw(TxDisable, BASE + MACCtrl1);
541 /**************************************************************************
542 DISABLE - Turn off ethernet interface
543 ***************************************************************************/
544 static void sundance_disable ( struct nic *nic __unused ) {
545 /* put the card in its initial state */
546 /* This function serves 3 purposes.
547 * This disables DMA and interrupts so we don't receive
548 * unexpected packets or interrupts from the card after
549 * etherboot has finished.
550 * This frees resources so etherboot may use
551 * this driver on another interface
552 * This allows etherboot to reinitialize the interface
553 * if something is something goes wrong.
555 outw(0x0000, BASE + IntrEnable);
556 /* Stop the Chipchips Tx and Rx Status */
557 outw(TxDisable | RxDisable | StatsDisable, BASE + MACCtrl1);
560 static struct nic_operations sundance_operations = {
561 .connect = dummy_connect,
562 .poll = sundance_poll,
563 .transmit = sundance_transmit,
564 .irq = sundance_irq,
568 /**************************************************************************
569 PROBE - Look for an adapter, this routine's visible to the outside
570 ***************************************************************************/
571 static int sundance_probe ( struct nic *nic, struct pci_device *pci ) {
573 u8 ee_data[EEPROM_SIZE];
574 u16 mii_ctl;
575 int i;
576 int speed;
578 if (pci->ioaddr == 0)
579 return 0;
581 /* BASE is used throughout to address the card */
582 BASE = pci->ioaddr;
583 printf(" sundance.c: Found %s Vendor=0x%hX Device=0x%hX\n",
584 pci->driver_name, pci->vendor, pci->device);
586 /* Get the MAC Address by reading the EEPROM */
587 for (i = 0; i < 3; i++) {
588 ((u16 *) ee_data)[i] =
589 le16_to_cpu(eeprom_read(BASE, i + EEPROM_SA_OFFSET));
591 /* Update the nic structure with the MAC Address */
592 for (i = 0; i < ETH_ALEN; i++) {
593 nic->node_addr[i] = ee_data[i];
596 /* Set the card as PCI Bus Master */
597 adjust_pci_device(pci);
599 // sdc->mii_if.dev = pci;
600 // sdc->mii_if.phy_id_mask = 0x1f;
601 // sdc->mii_if.reg_num_mask = 0x1f;
603 /* point to private storage */
604 sdc = &sdx;
606 sdc->nic_name = pci->driver_name;
607 sdc->mtu = mtu;
609 pci_read_config_byte(pci, PCI_REVISION_ID, &sdc->pci_rev_id);
611 DBG ( "Device revision id: %hx\n", sdc->pci_rev_id );
613 /* Print out some hardware info */
614 DBG ( "%s: %s at ioaddr %hX, ", pci->driver_name, nic->node_addr, (unsigned int) BASE);
616 sdc->mii_preamble_required = 0;
617 if (1) {
618 int phy, phy_idx = 0;
619 sdc->phys[0] = 1; /* Default Setting */
620 sdc->mii_preamble_required++;
621 for (phy = 1; phy < 32 && phy_idx < MII_CNT; phy++) {
622 int mii_status = mdio_read(nic, phy, MII_BMSR);
623 if (mii_status != 0xffff && mii_status != 0x0000) {
624 sdc->phys[phy_idx++] = phy;
625 sdc->mii_if.advertising =
626 mdio_read(nic, phy, MII_ADVERTISE);
627 if ((mii_status & 0x0040) == 0)
628 sdc->mii_preamble_required++;
629 DBG
630 ( "%s: MII PHY found at address %d, status " "%hX advertising %hX\n", sdc->nic_name, phy, mii_status, sdc->mii_if.advertising );
633 sdc->mii_preamble_required--;
634 if (phy_idx == 0)
635 printf("%s: No MII transceiver found!\n",
636 sdc->nic_name);
637 sdc->mii_if.phy_id = sdc->phys[0];
640 /* Parse override configuration */
641 sdc->an_enable = 1;
642 if (strcasecmp(media, "autosense") != 0) {
643 sdc->an_enable = 0;
644 if (strcasecmp(media, "100mbps_fd") == 0 ||
645 strcasecmp(media, "4") == 0) {
646 sdc->speed = 100;
647 sdc->mii_if.full_duplex = 1;
648 } else if (strcasecmp(media, "100mbps_hd") == 0
649 || strcasecmp(media, "3") == 0) {
650 sdc->speed = 100;
651 sdc->mii_if.full_duplex = 0;
652 } else if (strcasecmp(media, "10mbps_fd") == 0 ||
653 strcasecmp(media, "2") == 0) {
654 sdc->speed = 10;
655 sdc->mii_if.full_duplex = 1;
656 } else if (strcasecmp(media, "10mbps_hd") == 0 ||
657 strcasecmp(media, "1") == 0) {
658 sdc->speed = 10;
659 sdc->mii_if.full_duplex = 0;
660 } else {
661 sdc->an_enable = 1;
664 if (flowctrl == 1)
665 sdc->flowctrl = 1;
667 /* Fibre PHY? */
668 if (inl(BASE + ASICCtrl) & 0x80) {
669 /* Default 100Mbps Full */
670 if (sdc->an_enable) {
671 sdc->speed = 100;
672 sdc->mii_if.full_duplex = 1;
673 sdc->an_enable = 0;
677 /* The Linux driver uses flow control and resets the link here. This means the
678 mii section from above would need to be re done I believe. Since it serves
679 no real purpose leave it out. */
681 /* Force media type */
682 if (!sdc->an_enable) {
683 mii_ctl = 0;
684 mii_ctl |= (sdc->speed == 100) ? BMCR_SPEED100 : 0;
685 mii_ctl |= (sdc->mii_if.full_duplex) ? BMCR_FULLDPLX : 0;
686 mdio_write(nic, sdc->phys[0], MII_BMCR, mii_ctl);
687 printf("Override speed=%d, %s duplex\n",
688 sdc->speed,
689 sdc->mii_if.full_duplex ? "Full" : "Half");
692 /* Reset the chip to erase previous misconfiguration */
693 DBG ( "ASIC Control is %#lx\n", inl(BASE + ASICCtrl) );
694 outw(0x007f, BASE + ASICCtrl + 2);
695 DBG ( "ASIC Control is now %#lx.\n", inl(BASE + ASICCtrl) );
697 sundance_reset(nic);
698 if (sdc->an_enable) {
699 u16 mii_advertise, mii_lpa;
700 mii_advertise =
701 mdio_read(nic, sdc->phys[0], MII_ADVERTISE);
702 mii_lpa = mdio_read(nic, sdc->phys[0], MII_LPA);
703 mii_advertise &= mii_lpa;
704 if (mii_advertise & ADVERTISE_100FULL)
705 sdc->speed = 100;
706 else if (mii_advertise & ADVERTISE_100HALF)
707 sdc->speed = 100;
708 else if (mii_advertise & ADVERTISE_10FULL)
709 sdc->speed = 10;
710 else if (mii_advertise & ADVERTISE_10HALF)
711 sdc->speed = 10;
712 } else {
713 mii_ctl = mdio_read(nic, sdc->phys[0], MII_BMCR);
714 speed = (mii_ctl & BMCR_SPEED100) ? 100 : 10;
715 sdc->speed = speed;
716 printf("%s: Link changed: %dMbps ,", sdc->nic_name, speed);
717 printf("%s duplex.\n", (mii_ctl & BMCR_FULLDPLX) ?
718 "full" : "half");
720 check_duplex(nic);
721 if (sdc->flowctrl && sdc->mii_if.full_duplex) {
722 outw(inw(BASE + MulticastFilter1 + 2) | 0x0200,
723 BASE + MulticastFilter1 + 2);
724 outw(inw(BASE + MACCtrl0) | EnbFlowCtrl, BASE + MACCtrl0);
726 printf("%dMbps, %s-Duplex\n", sdc->speed,
727 sdc->mii_if.full_duplex ? "Full" : "Half");
729 /* point to NIC specific routines */
730 nic->nic_op = &sundance_operations;
731 pci_fill_nic ( nic, pci );
732 nic->irqno = pci->irq;
733 nic->ioaddr = BASE;
735 return 1;
739 /* Read the EEPROM and MII Management Data I/O (MDIO) interfaces. */
740 static int eeprom_read(long ioaddr, int location)
742 int boguscnt = 10000; /* Typical 1900 ticks */
743 outw(0x0200 | (location & 0xff), ioaddr + EECtrl);
744 do {
745 if (!(inw(ioaddr + EECtrl) & 0x8000)) {
746 return inw(ioaddr + EEData);
749 while (--boguscnt > 0);
750 return 0;
753 /* MII transceiver control section.
754 Read and write the MII registers using software-generated serial
755 MDIO protocol. See the MII specifications or DP83840A data sheet
756 for details.
758 The maximum data clock rate is 2.5 Mhz.
759 The timing is decoupled from the processor clock by flushing the write
760 from the CPU write buffer with a following read, and using PCI
761 transaction time. */
763 #define mdio_in(mdio_addr) inb(mdio_addr)
764 #define mdio_out(value, mdio_addr) outb(value, mdio_addr)
765 #define mdio_delay(mdio_addr) inb(mdio_addr)
767 enum mii_reg_bits {
768 MDIO_ShiftClk = 0x0001, MDIO_Data = 0x0002, MDIO_EnbOutput =
769 0x0004,
771 #define MDIO_EnbIn (0)
772 #define MDIO_WRITE0 (MDIO_EnbOutput)
773 #define MDIO_WRITE1 (MDIO_Data | MDIO_EnbOutput)
775 /* Generate the preamble required for initial synchronization and
776 a few older transceivers. */
777 static void mdio_sync(long mdio_addr)
779 int bits = 32;
781 /* Establish sync by sending at least 32 logic ones. */
782 while (--bits >= 0) {
783 mdio_out(MDIO_WRITE1, mdio_addr);
784 mdio_delay(mdio_addr);
785 mdio_out(MDIO_WRITE1 | MDIO_ShiftClk, mdio_addr);
786 mdio_delay(mdio_addr);
790 static int
791 mdio_read(struct nic *nic __unused, int phy_id, unsigned int location)
793 long mdio_addr = BASE + MIICtrl;
794 int mii_cmd = (0xf6 << 10) | (phy_id << 5) | location;
795 int i, retval = 0;
797 if (sdc->mii_preamble_required)
798 mdio_sync(mdio_addr);
800 /* Shift the read command bits out. */
801 for (i = 15; i >= 0; i--) {
802 int dataval =
803 (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
805 mdio_out(dataval, mdio_addr);
806 mdio_delay(mdio_addr);
807 mdio_out(dataval | MDIO_ShiftClk, mdio_addr);
808 mdio_delay(mdio_addr);
810 /* Read the two transition, 16 data, and wire-idle bits. */
811 for (i = 19; i > 0; i--) {
812 mdio_out(MDIO_EnbIn, mdio_addr);
813 mdio_delay(mdio_addr);
814 retval = (retval << 1) | ((mdio_in(mdio_addr) & MDIO_Data)
815 ? 1 : 0);
816 mdio_out(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);
817 mdio_delay(mdio_addr);
819 return (retval >> 1) & 0xffff;
822 static void
823 mdio_write(struct nic *nic __unused, int phy_id,
824 unsigned int location, int value)
826 long mdio_addr = BASE + MIICtrl;
827 int mii_cmd =
828 (0x5002 << 16) | (phy_id << 23) | (location << 18) | value;
829 int i;
831 if (sdc->mii_preamble_required)
832 mdio_sync(mdio_addr);
834 /* Shift the command bits out. */
835 for (i = 31; i >= 0; i--) {
836 int dataval =
837 (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
838 mdio_out(dataval, mdio_addr);
839 mdio_delay(mdio_addr);
840 mdio_out(dataval | MDIO_ShiftClk, mdio_addr);
841 mdio_delay(mdio_addr);
843 /* Clear out extra bits. */
844 for (i = 2; i > 0; i--) {
845 mdio_out(MDIO_EnbIn, mdio_addr);
846 mdio_delay(mdio_addr);
847 mdio_out(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);
848 mdio_delay(mdio_addr);
850 return;
853 static void set_rx_mode(struct nic *nic __unused)
855 int i;
856 u16 mc_filter[4]; /* Multicast hash filter */
857 u32 rx_mode;
859 memset(mc_filter, 0xff, sizeof(mc_filter));
860 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
862 if (sdc->mii_if.full_duplex && sdc->flowctrl)
863 mc_filter[3] |= 0x0200;
864 for (i = 0; i < 4; i++)
865 outw(mc_filter[i], BASE + MulticastFilter0 + i * 2);
866 outb(rx_mode, BASE + RxMode);
867 return;
870 static struct pci_device_id sundance_nics[] = {
871 PCI_ROM(0x13f0, 0x0201, "sundance", "ST201 Sundance 'Alta' based Adaptor"),
872 PCI_ROM(0x1186, 0x1002, "dfe530txs", "D-Link DFE530TXS (Sundance ST201 Alta)"),
875 PCI_DRIVER ( sundance_driver, sundance_nics, PCI_NO_CLASS );
877 DRIVER ( "SUNDANCE/PCI", nic_driver, pci_driver, sundance_driver,
878 sundance_probe, sundance_disable );