iPXE
sundance.c
Go to the documentation of this file.
1 /**************************************************************************
2 *
3 * sundance.c -- Etherboot device driver for the Sundance ST201 "Alta".
4 * Written 2002-2002 by Timothy Legge <tlegge@rogers.com>
5 *
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.
10 *
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.
15 *
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., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301, USA.
20 *
21 * Portions of this code based on:
22 * sundance.c: A Linux device driver for the Sundance ST201 "Alta"
23 * Written 1999-2002 by Donald Becker
24 *
25 * tulip.c: Tulip and Clone Etherboot Driver
26 * By Marty Conner
27 * Copyright (C) 2001 Entity Cyber, Inc.
28 *
29 * Linux Driver Version LK1.09a, 10-Jul-2003 (2.4.25)
30 *
31 * REVISION HISTORY:
32 * ================
33 * v1.1 01-01-2003 timlegge Initial implementation
34 * v1.7 04-10-2003 timlegge Transfers Linux Kernel (30 sec)
35 * v1.8 04-13-2003 timlegge Fix multiple transmission bug
36 * v1.9 08-19-2003 timlegge Support Multicast
37 * v1.10 01-17-2004 timlegge Initial driver output cleanup
38 * v1.11 03-21-2004 timlegge Remove unused variables
39 * v1.12 03-21-2004 timlegge Remove excess MII defines
40 * v1.13 03-24-2004 timlegge Update to Linux 2.4.25 driver
41 *
42 ****************************************************************************/
43 
44 FILE_LICENCE ( GPL2_OR_LATER );
45 
46 /* to get some global routines like printf */
47 #include "etherboot.h"
48 /* to get the interface to the body of the program */
49 #include "nic.h"
50 /* to get the PCI support functions, if this is a PCI NIC */
51 #include <ipxe/pci.h>
52 #include "mii.h"
53 
54 #define drv_version "v1.12"
55 #define drv_date "2004-03-21"
56 
57 #define HZ 100
58 
59 /* Condensed operations for readability. */
60 #define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))
61 #define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))
62 
63 /* Set the mtu */
64 static int mtu = 1514;
65 
66 /* Maximum number of multicast addresses to filter (vs. rx-all-multicast).
67  The sundance uses a 64 element hash table based on the Ethernet CRC. */
68 // static int multicast_filter_limit = 32;
69 
70 /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
71  Setting to > 1518 effectively disables this feature.
72  This chip can receive into any byte alignment buffers, so word-oriented
73  archs do not need a copy-align of the IP header. */
74 static int rx_copybreak = 0;
75 static int flowctrl = 1;
76 
77 /* Allow forcing the media type */
78 /* media[] specifies the media type the NIC operates at.
79  autosense Autosensing active media.
80  10mbps_hd 10Mbps half duplex.
81  10mbps_fd 10Mbps full duplex.
82  100mbps_hd 100Mbps half duplex.
83  100mbps_fd 100Mbps full duplex.
84 */
85 static char media[] = "autosense";
86 
87 /* Operational parameters that are set at compile time. */
88 
89 /* As Etherboot uses a Polling driver we can keep the number of rings
90 to the minimum number required. In general that is 1 transmit and 4 receive receive rings. However some cards require that
91 there be a minimum of 2 rings */
92 #define TX_RING_SIZE 2
93 #define TX_QUEUE_LEN 10 /* Limit ring entries actually used. */
94 #define RX_RING_SIZE 4
95 
96 
97 /* Operational parameters that usually are not changed. */
98 /* Time in jiffies before concluding the transmitter is hung. */
99 #define TX_TIME_OUT (4*HZ)
100 #define PKT_BUF_SZ 1536
101 
102 /* Offsets to the device registers.
103  Unlike software-only systems, device drivers interact with complex hardware.
104  It's not useful to define symbolic names for every register bit in the
105  device. The name can only partially document the semantics and make
106  the driver longer and more difficult to read.
107  In general, only the important configuration values or bits changed
108  multiple times should be defined symbolically.
109 */
111  DMACtrl = 0x00,
112  TxListPtr = 0x04,
116  RxDMAStatus = 0x0c,
117  RxListPtr = 0x10,
118  DebugCtrl0 = 0x1a,
119  DebugCtrl1 = 0x1c,
123  LEDCtrl = 0x1a,
124  ASICCtrl = 0x30,
125  EEData = 0x34,
126  EECtrl = 0x36,
129  FlashAddr = 0x40,
130  FlashData = 0x44,
131  TxStatus = 0x46,
132  TxFrameId = 0x47,
133  DownCounter = 0x18,
134  IntrClear = 0x4a,
135  IntrEnable = 0x4c,
136  IntrStatus = 0x4e,
137  MACCtrl0 = 0x50,
138  MACCtrl1 = 0x52,
139  StationAddr = 0x54,
140  MaxFrameSize = 0x5A,
141  RxMode = 0x5c,
142  MIICtrl = 0x5e,
145  RxOctetsLow = 0x68,
146  RxOctetsHigh = 0x6a,
147  TxOctetsLow = 0x6c,
148  TxOctetsHigh = 0x6e,
149  TxFramesOK = 0x70,
150  RxFramesOK = 0x72,
154  StatsOneColl = 0x77,
155  StatsTxDefer = 0x78,
156  RxMissed = 0x79,
158  StatsTxAbort = 0x7b,
159  StatsBcastTx = 0x7c,
160  StatsBcastRx = 0x7d,
161  StatsMcastTx = 0x7e,
162  StatsMcastRx = 0x7f,
163  /* Aliased and bogus values! */
164  RxStatus = 0x0c,
165 };
167  GlobalReset = 0x0001,
168  RxReset = 0x0002,
169  TxReset = 0x0004,
170  DMAReset = 0x0008,
171  FIFOReset = 0x0010,
172  NetworkReset = 0x0020,
173  HostReset = 0x0040,
174  ResetBusy = 0x0400,
175 };
176 
177 /* Bits in the interrupt status/mask registers. */
179  IntrSummary = 0x0001, IntrPCIErr = 0x0002, IntrMACCtrl = 0x0008,
180  IntrTxDone = 0x0004, IntrRxDone = 0x0010, IntrRxStart = 0x0020,
181  IntrDrvRqst = 0x0040,
182  StatsMax = 0x0080, LinkChange = 0x0100,
183  IntrTxDMADone = 0x0200, IntrRxDMADone = 0x0400,
184 };
185 
186 /* Bits in the RxMode register. */
190  0x01,
191 };
192 /* Bits in MACCtrl. */
195  EnbFlowCtrl = 0x100, EnbPassRxCRC = 0x200,
196 };
198  StatsEnable = 0x0020, StatsDisable = 0x0040, StatsEnabled = 0x0080,
199  TxEnable = 0x0100, TxDisable = 0x0200, TxEnabled = 0x0400,
200  RxEnable = 0x0800, RxDisable = 0x1000, RxEnabled = 0x2000,
201 };
202 
203 /* The Rx and Tx buffer descriptors.
204  Using only 32 bit fields simplifies software endian correction.
205  This structure must be aligned, and should avoid spanning cache lines.
206 */
207 struct netdev_desc {
212 };
213 
214 /* Bits in netdev_desc.status */
216  DescOwn = 0x8000,
217  DescEndPacket = 0x4000,
218  DescEndRing = 0x2000,
219  LastFrag = 0x80000000,
220  DescIntrOnTx = 0x8000,
221  DescIntrOnDMADone = 0x80000000,
222  DisableAlign = 0x00000001,
223 };
224 
225 /**********************************************
226 * Descriptor Ring and Buffer defination
227 ***********************************************/
228 /* Define the TX Descriptor */
230 
231 /* Define the RX Descriptor */
233 
234 /* Create a static buffer of size PKT_BUF_SZ for each RX and TX descriptor.
235  All descriptors point to a part of this buffer */
236 struct sundance_bss {
237  unsigned char txb[PKT_BUF_SZ * TX_RING_SIZE];
238  unsigned char rxb[RX_RING_SIZE * PKT_BUF_SZ];
239 };
240 #define rx_tx_buf NIC_FAKE_BSS ( struct sundance_bss )
241 #define rxb rx_tx_buf.rxb
242 #define txb rx_tx_buf.txb
243 
244 /* FIXME: Move BASE to the private structure */
245 static u32 BASE;
246 #define EEPROM_SIZE 128
247 
250  PCI_ADDR0 = 0 << 4, PCI_ADDR1 = 1 << 4, PCI_ADDR2 =
251  2 << 4, PCI_ADDR3 = 3 << 4,
252 };
253 
255 #define PCI_IOTYPE (PCI_USES_MASTER | PCI_USES_IO | PCI_ADDR0)
256 
257 #define MII_CNT 4
258 static struct sundance_private {
259  const char *nic_name;
260  /* Frequently used values */
261 
262  unsigned int cur_rx; /* Producer/consumer ring indices */
263  unsigned int mtu;
264 
265  /* These values keep track of the tranceiver/media in use */
266  unsigned int flowctrl:1;
267  unsigned int an_enable:1;
268 
269  unsigned int speed;
270 
271  /* MII tranceiver section */
274  unsigned char phys[MII_CNT];
275  unsigned char pci_rev_id;
276 } sdx;
277 
278 static struct sundance_private *sdc;
279 
280 /* Station Address location within the EEPROM */
281 #define EEPROM_SA_OFFSET 0x10
282 #define DEFAULT_INTR (IntrRxDMADone | IntrPCIErr | \
283  IntrDrvRqst | IntrTxDone | StatsMax | \
284  LinkChange)
285 
286 static int eeprom_read(long ioaddr, int location);
287 static int mdio_read(struct nic *nic, int phy_id, unsigned int location);
288 static void mdio_write(struct nic *nic, int phy_id, unsigned int location,
289  int value);
290 static void set_rx_mode(struct nic *nic);
291 
292 static void check_duplex(struct nic *nic)
293 {
294  int mii_lpa = mdio_read(nic, sdc->phys[0], MII_LPA);
295  int negotiated = mii_lpa & sdc->mii_if.advertising;
296  int duplex;
297 
298  /* Force media */
299  if (!sdc->an_enable || mii_lpa == 0xffff) {
300  if (sdc->mii_if.full_duplex)
302  BASE + MACCtrl0);
303  return;
304  }
305 
306  /* Autonegotiation */
307  duplex = (negotiated & 0x0100) || (negotiated & 0x01C0) == 0x0040;
308  if (sdc->mii_if.full_duplex != duplex) {
310  DBG ("%s: Setting %s-duplex based on MII #%d "
311  "negotiated capability %4.4x.\n", sdc->nic_name,
312  duplex ? "full" : "half", sdc->phys[0],
313  negotiated );
314  outw(inw(BASE + MACCtrl0) | duplex ? 0x20 : 0,
315  BASE + MACCtrl0);
316  }
317 }
318 
319 
320 /**************************************************************************
321  * init_ring - setup the tx and rx descriptors
322  *************************************************************************/
323 static void init_ring(struct nic *nic __unused)
324 {
325  int i;
326 
327  sdc->cur_rx = 0;
328 
329  /* Initialize all the Rx descriptors */
330  for (i = 0; i < RX_RING_SIZE; i++) {
332  rx_ring[i].status = 0;
333  rx_ring[i].length = 0;
334  rx_ring[i].addr = 0;
335  }
336 
337  /* Mark the last entry as wrapping the ring */
339 
340  for (i = 0; i < RX_RING_SIZE; i++) {
343  }
344 
345  /* We only use one transmit buffer, but two
346  * descriptors so transmit engines have somewhere
347  * to point should they feel the need */
348  tx_ring[0].status = 0x00000000;
349  tx_ring[0].addr = virt_to_bus(&txb[0]);
350  tx_ring[0].next_desc = 0; /* virt_to_bus(&tx_ring[1]); */
351 
352  /* This descriptor is never used */
353  tx_ring[1].status = 0x00000000;
354  tx_ring[1].addr = 0; /*virt_to_bus(&txb[0]); */
355  tx_ring[1].next_desc = 0;
356 
357  /* Mark the last entry as wrapping the ring,
358  * though this should never happen */
360 }
361 
362 /**************************************************************************
363  * RESET - Reset Adapter
364  * ***********************************************************************/
365 static void sundance_reset(struct nic *nic)
366 {
367  int i;
368 
369  init_ring(nic);
370 
372  /* The Tx List Pointer is written as packets are queued */
373 
374  /* Initialize other registers. */
375  /* __set_mac_addr(dev); */
376  {
377  u16 addr16;
378 
379  addr16 = (nic->node_addr[0] | (nic->node_addr[1] << 8));
380  outw(addr16, BASE + StationAddr);
381  addr16 = (nic->node_addr[2] | (nic->node_addr[3] << 8));
382  outw(addr16, BASE + StationAddr + 2);
383  addr16 = (nic->node_addr[4] | (nic->node_addr[5] << 8));
384  outw(addr16, BASE + StationAddr + 4);
385  }
386 
387  outw(sdc->mtu + 14, BASE + MaxFrameSize);
388  if (sdc->mtu > 2047) /* this will never happen with default options */
389  outl(inl(BASE + ASICCtrl) | 0x0c, BASE + ASICCtrl);
390 
391  set_rx_mode(nic);
392 
393  outw(0, BASE + DownCounter);
394  /* Set the chip to poll every N*30nsec */
395  outb(100, BASE + RxDMAPollPeriod);
396 
397  /* Fix DFE-580TX packet drop issue */
398  if (sdc->pci_rev_id >= 0x14)
399  writeb(0x01, BASE + DebugCtrl1);
400 
402 
403  /* Construct a perfect filter frame with the mac address as first match
404  * and broadcast for all others */
405  for (i = 0; i < 192; i++)
406  txb[i] = 0xFF;
407 
408  txb[0] = nic->node_addr[0];
409  txb[1] = nic->node_addr[1];
410  txb[2] = nic->node_addr[2];
411  txb[3] = nic->node_addr[3];
412  txb[4] = nic->node_addr[4];
413  txb[5] = nic->node_addr[5];
414 
415  DBG ( "%s: Done sundance_reset, status: Rx %hX Tx %hX "
416  "MAC Control %hX, %hX %hX\n",
417  sdc->nic_name, (int) inl(BASE + RxStatus),
418  (int) inw(BASE + TxStatus), (int) inl(BASE + MACCtrl0),
419  (int) inw(BASE + MACCtrl1), (int) inw(BASE + MACCtrl0) );
420 }
421 
422 /**************************************************************************
423 IRQ - Wait for a frame
424 ***************************************************************************/
425 static void sundance_irq ( struct nic *nic, irq_action_t action ) {
426  unsigned int intr_status;
427 
428  switch ( action ) {
429  case DISABLE :
430  case ENABLE :
431  intr_status = inw(nic->ioaddr + IntrStatus);
432  intr_status = intr_status & ~DEFAULT_INTR;
433  if ( action == ENABLE )
434  intr_status = intr_status | DEFAULT_INTR;
435  outw(intr_status, nic->ioaddr + IntrEnable);
436  break;
437  case FORCE :
438  outw(0x0200, BASE + ASICCtrl);
439  break;
440  }
441 }
442 /**************************************************************************
443 POLL - Wait for a frame
444 ***************************************************************************/
445 static int sundance_poll(struct nic *nic, int retrieve)
446 {
447  /* return true if there's an ethernet packet ready to read */
448  /* nic->packet should contain data on return */
449  /* nic->packetlen should contain length of data */
450  int entry = sdc->cur_rx % RX_RING_SIZE;
451  u32 frame_status = le32_to_cpu(rx_ring[entry].status);
452  int intr_status;
453  int pkt_len = 0;
454 
455  if (!(frame_status & DescOwn))
456  return 0;
457 
458  /* There is a packet ready */
459  if(!retrieve)
460  return 1;
461 
462  intr_status = inw(nic->ioaddr + IntrStatus);
463  outw(intr_status, nic->ioaddr + IntrStatus);
464 
465  pkt_len = frame_status & 0x1fff;
466 
467  if (frame_status & 0x001f4000) {
468  DBG ( "Polling frame_status error\n" ); /* Do we really care about this */
469  } else {
470  if (pkt_len < rx_copybreak) {
471  /* FIXME: What should happen Will this ever occur */
472  printf("Poll Error: pkt_len < rx_copybreak");
473  } else {
474  nic->packetlen = pkt_len;
475  memcpy(nic->packet, rxb +
477 
478  }
479  }
481  rx_ring[entry].status = 0;
482  entry++;
483  sdc->cur_rx = entry % RX_RING_SIZE;
485  nic->ioaddr + IntrStatus);
486  return 1;
487 }
488 
489 /**************************************************************************
490 TRANSMIT - Transmit a frame
491 ***************************************************************************/
492 static void sundance_transmit(struct nic *nic, const char *d, /* Destination */
493  unsigned int t, /* Type */
494  unsigned int s, /* size */
495  const char *p)
496 { /* Packet */
497  u16 nstype;
498  u32 to;
499 
500  /* Disable the Tx */
502 
503  memcpy(txb, d, ETH_ALEN);
505  nstype = htons((u16) t);
506  memcpy(txb + 2 * ETH_ALEN, (u8 *) & nstype, 2);
507  memcpy(txb + ETH_HLEN, p, s);
508 
509  s += ETH_HLEN;
510  s &= 0x0FFF;
511  while (s < ETH_ZLEN)
512  txb[s++] = '\0';
513 
514  /* Setup the transmit descriptor */
516  tx_ring[0].status = cpu_to_le32(0x00000001);
517 
518  /* Point to transmit descriptor */
520 
521  /* Enable Tx */
523  /* Trigger an immediate send */
524  outw(0, BASE + TxStatus);
525 
526  to = currticks() + TX_TIME_OUT;
527  while (!(tx_ring[0].status & 0x00010000) && (currticks() < to)); /* wait */
528 
529  if (currticks() >= to) {
530  printf("TX Time Out");
531  }
532  /* Disable Tx */
534 
535 }
536 
537 /**************************************************************************
538 DISABLE - Turn off ethernet interface
539 ***************************************************************************/
540 static void sundance_disable ( struct nic *nic __unused, void *hwdev __unused) {
541  /* put the card in its initial state */
542  /* This function serves 3 purposes.
543  * This disables DMA and interrupts so we don't receive
544  * unexpected packets or interrupts from the card after
545  * etherboot has finished.
546  * This frees resources so etherboot may use
547  * this driver on another interface
548  * This allows etherboot to reinitialize the interface
549  * if something is something goes wrong.
550  */
551  outw(0x0000, BASE + IntrEnable);
552  /* Stop the Chipchips Tx and Rx Status */
554 }
555 
558  .poll = sundance_poll,
559  .transmit = sundance_transmit,
560  .irq = sundance_irq,
561 
562 };
563 
564 /**************************************************************************
565 PROBE - Look for an adapter, this routine's visible to the outside
566 ***************************************************************************/
567 static int sundance_probe ( struct nic *nic, struct pci_device *pci ) {
568 
570  u16 mii_ctl;
571  int i;
572  int speed;
573 
574  if (pci->ioaddr == 0)
575  return 0;
576 
577  /* BASE is used throughout to address the card */
578  BASE = pci->ioaddr;
579  printf(" sundance.c: Found %s Vendor=0x%hX Device=0x%hX\n",
580  pci->id->name, pci->vendor, pci->device);
581 
582  /* Get the MAC Address by reading the EEPROM */
583  for (i = 0; i < 3; i++) {
584  ((u16 *) ee_data)[i] =
586  }
587  /* Update the nic structure with the MAC Address */
588  for (i = 0; i < ETH_ALEN; i++) {
589  nic->node_addr[i] = ee_data[i];
590  }
591 
592  /* Set the card as PCI Bus Master */
593  adjust_pci_device(pci);
594 
595 // sdc->mii_if.dev = pci;
596 // sdc->mii_if.phy_id_mask = 0x1f;
597 // sdc->mii_if.reg_num_mask = 0x1f;
598 
599  /* point to private storage */
600  sdc = &sdx;
601 
602  sdc->nic_name = pci->id->name;
603  sdc->mtu = mtu;
604 
606 
607  DBG ( "Device revision id: %hx\n", sdc->pci_rev_id );
608 
609  /* Print out some hardware info */
610  DBG ( "%s: %s at ioaddr %hX, ",
611  pci->id->name, nic->node_addr, (unsigned int) BASE);
612 
614  if (1) {
615  int phy, phy_idx = 0;
616  sdc->phys[0] = 1; /* Default Setting */
618  for (phy = 1; phy < 32 && phy_idx < MII_CNT; phy++) {
619  int mii_status = mdio_read(nic, phy, MII_BMSR);
620  if (mii_status != 0xffff && mii_status != 0x0000) {
621  sdc->phys[phy_idx++] = phy;
623  mdio_read(nic, phy, MII_ADVERTISE);
624  if ((mii_status & 0x0040) == 0)
626  DBG
627  ( "%s: MII PHY found at address %d, status " "%hX advertising %hX\n", sdc->nic_name, phy, mii_status, sdc->mii_if.advertising );
628  }
629  }
631  if (phy_idx == 0)
632  printf("%s: No MII transceiver found!\n",
633  sdc->nic_name);
634  sdc->mii_if.phy_id = sdc->phys[0];
635  }
636 
637  /* Parse override configuration */
638  sdc->an_enable = 1;
639  if (strcasecmp(media, "autosense") != 0) {
640  sdc->an_enable = 0;
641  if (strcasecmp(media, "100mbps_fd") == 0 ||
642  strcasecmp(media, "4") == 0) {
643  sdc->speed = 100;
644  sdc->mii_if.full_duplex = 1;
645  } else if (strcasecmp(media, "100mbps_hd") == 0
646  || strcasecmp(media, "3") == 0) {
647  sdc->speed = 100;
648  sdc->mii_if.full_duplex = 0;
649  } else if (strcasecmp(media, "10mbps_fd") == 0 ||
650  strcasecmp(media, "2") == 0) {
651  sdc->speed = 10;
652  sdc->mii_if.full_duplex = 1;
653  } else if (strcasecmp(media, "10mbps_hd") == 0 ||
654  strcasecmp(media, "1") == 0) {
655  sdc->speed = 10;
656  sdc->mii_if.full_duplex = 0;
657  } else {
658  sdc->an_enable = 1;
659  }
660  }
661  if (flowctrl == 1)
662  sdc->flowctrl = 1;
663 
664  /* Fibre PHY? */
665  if (inl(BASE + ASICCtrl) & 0x80) {
666  /* Default 100Mbps Full */
667  if (sdc->an_enable) {
668  sdc->speed = 100;
669  sdc->mii_if.full_duplex = 1;
670  sdc->an_enable = 0;
671  }
672  }
673 
674  /* The Linux driver uses flow control and resets the link here. This means the
675  mii section from above would need to be re done I believe. Since it serves
676  no real purpose leave it out. */
677 
678  /* Force media type */
679  if (!sdc->an_enable) {
680  mii_ctl = 0;
681  mii_ctl |= (sdc->speed == 100) ? BMCR_SPEED100 : 0;
682  mii_ctl |= (sdc->mii_if.full_duplex) ? BMCR_FULLDPLX : 0;
683  mdio_write(nic, sdc->phys[0], MII_BMCR, mii_ctl);
684  printf("Override speed=%d, %s duplex\n",
685  sdc->speed,
686  sdc->mii_if.full_duplex ? "Full" : "Half");
687  }
688 
689  /* Reset the chip to erase previous misconfiguration */
690  DBG ( "ASIC Control is %#x\n", inl(BASE + ASICCtrl) );
691  outw(0x007f, BASE + ASICCtrl + 2);
692 
693  /*
694  * wait for reset to complete
695  * this is heavily inspired by the linux sundance driver
696  * according to the linux driver it can take up to 1ms for the reset
697  * to complete
698  */
699  i = 0;
700  while(inl(BASE + ASICCtrl) & (ResetBusy << 16)) {
701  if(i++ >= 10) {
702  DBG("sundance: NIC reset did not complete.\n");
703  break;
704  }
705  udelay(100);
706  }
707 
708  DBG ( "ASIC Control is now %#x.\n", inl(BASE + ASICCtrl) );
709 
711  if (sdc->an_enable) {
712  u16 mii_advertise, mii_lpa;
713  mii_advertise =
715  mii_lpa = mdio_read(nic, sdc->phys[0], MII_LPA);
716  mii_advertise &= mii_lpa;
717  if (mii_advertise & ADVERTISE_100FULL)
718  sdc->speed = 100;
719  else if (mii_advertise & ADVERTISE_100HALF)
720  sdc->speed = 100;
721  else if (mii_advertise & ADVERTISE_10FULL)
722  sdc->speed = 10;
723  else if (mii_advertise & ADVERTISE_10HALF)
724  sdc->speed = 10;
725  } else {
726  mii_ctl = mdio_read(nic, sdc->phys[0], MII_BMCR);
727  speed = (mii_ctl & BMCR_SPEED100) ? 100 : 10;
728  sdc->speed = speed;
729  printf("%s: Link changed: %dMbps ,", sdc->nic_name, speed);
730  printf("%s duplex.\n", (mii_ctl & BMCR_FULLDPLX) ?
731  "full" : "half");
732  }
733  check_duplex(nic);
734  if (sdc->flowctrl && sdc->mii_if.full_duplex) {
735  outw(inw(BASE + MulticastFilter1 + 2) | 0x0200,
736  BASE + MulticastFilter1 + 2);
738  }
739  printf("%dMbps, %s-Duplex\n", sdc->speed,
740  sdc->mii_if.full_duplex ? "Full" : "Half");
741 
742  /* point to NIC specific routines */
744 
745  nic->irqno = pci->irq;
746  nic->ioaddr = BASE;
747 
748  return 1;
749 }
750 
751 
752 /* Read the EEPROM and MII Management Data I/O (MDIO) interfaces. */
753 static int eeprom_read(long ioaddr, int location)
754 {
755  int boguscnt = 10000; /* Typical 1900 ticks */
756  outw(0x0200 | (location & 0xff), ioaddr + EECtrl);
757  do {
758  if (!(inw(ioaddr + EECtrl) & 0x8000)) {
759  return inw(ioaddr + EEData);
760  }
761  }
762  while (--boguscnt > 0);
763  return 0;
764 }
765 
766 /* MII transceiver control section.
767  Read and write the MII registers using software-generated serial
768  MDIO protocol. See the MII specifications or DP83840A data sheet
769  for details.
770 
771  The maximum data clock rate is 2.5 Mhz.
772  The timing is decoupled from the processor clock by flushing the write
773  from the CPU write buffer with a following read, and using PCI
774  transaction time. */
775 
776 #define mdio_in(mdio_addr) inb(mdio_addr)
777 #define mdio_out(value, mdio_addr) outb(value, mdio_addr)
778 #define mdio_delay(mdio_addr) inb(mdio_addr)
779 
781  MDIO_ShiftClk = 0x0001, MDIO_Data = 0x0002, MDIO_EnbOutput =
782  0x0004,
783 };
784 #define MDIO_EnbIn (0)
785 #define MDIO_WRITE0 (MDIO_EnbOutput)
786 #define MDIO_WRITE1 (MDIO_Data | MDIO_EnbOutput)
787 
788 /* Generate the preamble required for initial synchronization and
789  a few older transceivers. */
790 static void mdio_sync(long mdio_addr)
791 {
792  int bits = 32;
793 
794  /* Establish sync by sending at least 32 logic ones. */
795  while (--bits >= 0) {
796  mdio_out(MDIO_WRITE1, mdio_addr);
797  mdio_delay(mdio_addr);
798  mdio_out(MDIO_WRITE1 | MDIO_ShiftClk, mdio_addr);
799  mdio_delay(mdio_addr);
800  }
801 }
802 
803 static int
804 mdio_read(struct nic *nic __unused, int phy_id, unsigned int location)
805 {
806  long mdio_addr = BASE + MIICtrl;
807  int mii_cmd = (0xf6 << 10) | (phy_id << 5) | location;
808  int i, retval = 0;
809 
811  mdio_sync(mdio_addr);
812 
813  /* Shift the read command bits out. */
814  for (i = 15; i >= 0; i--) {
815  int dataval =
816  (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
817 
818  mdio_out(dataval, mdio_addr);
819  mdio_delay(mdio_addr);
820  mdio_out(dataval | MDIO_ShiftClk, mdio_addr);
821  mdio_delay(mdio_addr);
822  }
823  /* Read the two transition, 16 data, and wire-idle bits. */
824  for (i = 19; i > 0; i--) {
825  mdio_out(MDIO_EnbIn, mdio_addr);
826  mdio_delay(mdio_addr);
827  retval = (retval << 1) | ((mdio_in(mdio_addr) & MDIO_Data)
828  ? 1 : 0);
829  mdio_out(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);
830  mdio_delay(mdio_addr);
831  }
832  return (retval >> 1) & 0xffff;
833 }
834 
835 static void
836 mdio_write(struct nic *nic __unused, int phy_id,
837  unsigned int location, int value)
838 {
839  long mdio_addr = BASE + MIICtrl;
840  int mii_cmd =
841  (0x5002 << 16) | (phy_id << 23) | (location << 18) | value;
842  int i;
843 
845  mdio_sync(mdio_addr);
846 
847  /* Shift the command bits out. */
848  for (i = 31; i >= 0; i--) {
849  int dataval =
850  (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
851  mdio_out(dataval, mdio_addr);
852  mdio_delay(mdio_addr);
853  mdio_out(dataval | MDIO_ShiftClk, mdio_addr);
854  mdio_delay(mdio_addr);
855  }
856  /* Clear out extra bits. */
857  for (i = 2; i > 0; i--) {
858  mdio_out(MDIO_EnbIn, mdio_addr);
859  mdio_delay(mdio_addr);
860  mdio_out(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);
861  mdio_delay(mdio_addr);
862  }
863  return;
864 }
865 
866 static void set_rx_mode(struct nic *nic __unused)
867 {
868  int i;
869  u16 mc_filter[4]; /* Multicast hash filter */
870  u32 rx_mode;
871 
872  memset(mc_filter, 0xff, sizeof(mc_filter));
874 
875  if (sdc->mii_if.full_duplex && sdc->flowctrl)
876  mc_filter[3] |= 0x0200;
877  for (i = 0; i < 4; i++)
878  outw(mc_filter[i], BASE + MulticastFilter0 + i * 2);
879  outb(rx_mode, BASE + RxMode);
880  return;
881 }
882 
883 static struct pci_device_id sundance_nics[] = {
884  PCI_ROM(0x1186, 0x1002, "dfe530txs", "D-Link DFE530TXS (Sundance ST201 Alta)", 0),
885  PCI_ROM(0x13f0, 0x0200, "ip100a", "IC+ IP100A", 0),
886  PCI_ROM(0x13f0, 0x0201, "sundance", "ST201 Sundance 'Alta' based Adaptor", 0),
887 };
888 
889 PCI_DRIVER ( sundance_driver, sundance_nics, PCI_NO_CLASS );
890 
891 DRIVER ( "SUNDANCE/PCI", nic_driver, pci_driver, sundance_driver,
893 
894 /*
895  * Local variables:
896  * c-basic-offset: 8
897  * c-indent-level: 8
898  * tab-width: 8
899  * End:
900  */
unsigned char irqno
Definition: nic.h:56
u32 next_desc
Definition: sundance.c:208
#define MII_ADVERTISE
Definition: atl1e.h:875
uint16_t u16
Definition: stdint.h:21
#define MDIO_WRITE0
Definition: sundance.c:785
int phy_id
Definition: mii.h:145
Definition: nic.h:35
#define MII_LPA
Definition: atl1e.h:876
uint8_t irq
Interrupt number.
Definition: pci.h:233
static void sundance_reset(struct nic *nic)
Definition: sundance.c:365
#define rxb
Definition: sundance.c:241
static int sundance_probe(struct nic *nic, struct pci_device *pci)
Definition: sundance.c:567
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
static void init_ring(struct nic *nic __unused)
Definition: sundance.c:323
uint16_t inw(volatile uint16_t *io_addr)
Read 16-bit word from I/O-mapped device.
#define EEPROM_SIZE
Definition: sundance.c:246
A PCI driver.
Definition: pci.h:251
unsigned int full_duplex
Definition: mii.h:150
rx_mode_bits
Definition: sundance.c:187
int mii_preamble_required
Definition: sundance.c:273
#define EEPROM_SA_OFFSET
Definition: sundance.c:281
static void sundance_irq(struct nic *nic, irq_action_t action)
Definition: sundance.c:425
#define ADVERTISE_10FULL
Definition: mii.h:76
DRIVER("SUNDANCE/PCI", nic_driver, pci_driver, sundance_driver, sundance_probe, sundance_disable, rx_tx_buf)
#define le32_to_cpu(value)
Definition: byteswap.h:113
#define outw(data, io_addr)
Definition: io.h:319
#define TX_RING_SIZE
Definition: sundance.c:92
unsigned long ioaddr
I/O address.
Definition: pci.h:225
#define mdio_out(value, mdio_addr)
Definition: sundance.c:777
const char * nic_name
Definition: sundance.c:259
static struct netdev_desc tx_ring[TX_RING_SIZE]
Definition: sundance.c:229
#define TX_TIME_OUT
Definition: sundance.c:99
static void sundance_transmit(struct nic *nic, const char *d, unsigned int t, unsigned int s, const char *p)
Definition: sundance.c:492
unsigned char pci_rev_id
Definition: sundance.c:275
static unsigned char ee_data[EEPROM_SIZE]
Definition: davicom.c:67
#define ADVERTISE_100FULL
Definition: mii.h:80
unsigned int flowctrl
Definition: sundance.c:266
static struct netdev_desc rx_ring[RX_RING_SIZE]
Definition: sundance.c:232
int strcasecmp(const char *first, const char *second)
Compare case-insensitive strings.
Definition: string.c:208
FILE_LICENCE(GPL2_OR_LATER)
static void mdio_sync(long mdio_addr)
Definition: sundance.c:790
static int flowctrl
Definition: sundance.c:75
void adjust_pci_device(struct pci_device *pci)
Enable PCI device.
Definition: pci.c:240
int dummy_connect(struct nic *nic __unused)
Definition: legacy.c:175
void writeb(uint8_t data, volatile uint8_t *io_addr)
Write byte to memory-mapped device.
static void set_rx_mode(struct nic *nic)
ASICCtrl_HiWord_bit
Definition: sundance.c:166
static u32 BASE
Definition: sundance.c:245
#define MII_CNT
Definition: sundance.c:257
static unsigned long ioaddr
Definition: davicom.c:129
void * memcpy(void *dest, const void *src, size_t len) __nonnull
unsigned int ioaddr
Definition: nic.h:55
static __always_inline unsigned long virt_to_bus(volatile const void *addr)
Convert virtual address to a bus address.
Definition: io.h:183
uint16_t device
Device ID.
Definition: pci.h:229
static struct sundance_private sdx
#define ETH_HLEN
Definition: if_ether.h:9
#define RX_RING_SIZE
Definition: sundance.c:94
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
pci_id_flags_bits
Definition: sundance.c:248
void udelay(unsigned long usecs)
Delay for a fixed number of microseconds.
Definition: timer.c:60
intr_status_bits
Definition: sundance.c:178
irq_action_t
Definition: nic.h:34
static int mtu
Definition: sundance.c:64
unsigned int mtu
Definition: sundance.c:263
static struct sundance_private * sdc
Definition: sundance.c:278
#define cpu_to_le32(value)
Definition: byteswap.h:107
chip_capability_flags
Definition: sundance.c:254
static int sundance_poll(struct nic *nic, int retrieve)
Definition: sundance.c:445
unsigned int packetlen
Definition: nic.h:54
static int rx_copybreak
Definition: sundance.c:74
mii_reg_bits
Definition: sundance.c:780
alta_offsets
Definition: sundance.c:110
unsigned int an_enable
Definition: sundance.c:267
desc_status_bits
Definition: sundance.c:215
#define outl(data, io_addr)
Definition: io.h:329
PCI bus.
A PCI device.
Definition: pci.h:210
#define PKT_BUF_SZ
Definition: sundance.c:100
#define mdio_delay(mdio_addr)
Definition: sundance.c:778
unsigned char rxb[RX_RING_SIZE *PKT_BUF_SZ]
Definition: sundance.c:238
uint16_t pkt_len
Definition: aqc1xx.h:37
#define MII_BMCR
Definition: atl1e.h:871
#define mdio_in(mdio_addr)
Definition: sundance.c:776
#define ETH_ALEN
Definition: if_ether.h:8
#define ETH_ZLEN
Definition: if_ether.h:10
A PCI device ID list entry.
Definition: pci.h:174
Definition: nic.h:37
#define le16_to_cpu(value)
Definition: byteswap.h:112
Definition: nic.h:49
const char * name
Name.
Definition: pci.h:176
static void mdio_write(struct nic *nic, int phy_id, unsigned int location, int value)
static void sundance_disable(struct nic *nic __unused, void *hwdev __unused)
Definition: sundance.c:540
uint16_t vendor
Vendor ID.
Definition: pci.h:227
static struct nic_operations sundance_operations
Definition: sundance.c:556
uint8_t status
Status.
Definition: ena.h:16
#define txb
Definition: sundance.c:242
u32 length
Definition: sundance.c:211
unsigned long retval
Definition: xen.h:45
Definition: nic.h:36
unsigned char * packet
Definition: nic.h:53
duplex
Definition: nic.h:40
#define DEFAULT_INTR
Definition: sundance.c:282
unsigned char * node_addr
Definition: nic.h:52
#define outb(data, io_addr)
Definition: io.h:309
static void check_duplex(struct nic *nic)
Definition: sundance.c:292
int advertising
Definition: mii.h:146
static volatile void * bits
Definition: bitops.h:27
Media Independent Interface constants.
static struct pci_device_id sundance_nics[]
Definition: sundance.c:883
mac_ctrl0_bits
Definition: sundance.c:193
#define ADVERTISE_10HALF
Definition: mii.h:74
mac_ctrl1_bits
Definition: sundance.c:197
uint32_t inl(volatile uint32_t *io_addr)
Read 32-bit dword from I/O-mapped device.
#define rx_tx_buf
Definition: sundance.c:240
struct pci_device_id * id
Driver device ID.
Definition: pci.h:247
#define virt_to_le32desc(addr)
Definition: sundance.c:60
struct mii_if_info mii_if
Definition: sundance.c:272
static char media[]
Definition: sundance.c:85
#define PCI_REVISION
PCI revision.
Definition: pci.h:44
static int eeprom_read(long ioaddr, int location)
Definition: sundance.c:753
PCI_DRIVER(sundance_driver, sundance_nics, PCI_NO_CLASS)
#define ADVERTISE_100HALF
Definition: mii.h:78
static int mdio_read(struct nic *nic, int phy_id, unsigned int location)
unsigned long currticks(void)
Get current system time in ticks.
Definition: timer.c:42
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
struct nic_operations * nic_op
Definition: nic.h:50
unsigned int cur_rx
Definition: sundance.c:262
#define MII_BMSR
Definition: atl1e.h:872
u32 status
Definition: sundance.c:209
unsigned int speed
Definition: sundance.c:269
#define BMCR_SPEED100
Definition: mii.h:50
unsigned char phys[MII_CNT]
Definition: sundance.c:274
int(* connect)(struct nic *)
Definition: nic.h:69
unsigned char txb[PKT_BUF_SZ *TX_RING_SIZE]
Definition: sundance.c:237
#define PCI_ROM(_vendor, _device, _name, _description, _data)
Definition: pci.h:307
#define htons(value)
Definition: byteswap.h:135
uint8_t u8
Definition: stdint.h:19
uint32_t u32
Definition: stdint.h:23
#define MDIO_WRITE1
Definition: sundance.c:786
#define BMCR_FULLDPLX
Definition: mii.h:45
#define MDIO_EnbIn
Definition: sundance.c:784
void * memset(void *dest, int character, size_t len) __nonnull
int pci_read_config_byte(struct pci_device *pci, unsigned int where, uint8_t *value)
Read byte from PCI configuration space.