2 /* epic100.c: A SMC 83c170 EPIC/100 fast ethernet driver for Etherboot */
4 /* 05/06/2003 timlegge Fixed relocation and implemented Multicast */
5 #define LINUX_OUT_MACROS
9 #include <gpxe/ethernet.h>
14 /* Condensed operations for readability */
15 #define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))
16 #define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))
18 #define TX_RING_SIZE 2 /* use at least 2 buffers for TX */
19 #define RX_RING_SIZE 2
21 #define PKT_BUF_SZ 1536 /* Size of each temporary Tx/Rx buffer.*/
29 #define EPIC_DEBUG 0 /* debug level */
31 /* The EPIC100 Rx and Tx buffer descriptors. */
34 unsigned long bufaddr;
35 unsigned long buflength;
38 /* description of the tx descriptors control bits commonly used */
39 #define TD_STDFLAGS TD_LASTDESC
43 unsigned long bufaddr;
44 unsigned long buflength;
48 #define delay(nanosec) do { int _i = 3; while (--_i > 0) \
49 { __SLOW_DOWN_IO; }} while (0)
51 static void epic100_open(void);
52 static void epic100_init_ring(void);
53 static void epic100_disable(struct nic *nic);
54 static int epic100_poll(struct nic *nic, int retrieve);
55 static void epic100_transmit(struct nic *nic, const char *destaddr,
56 unsigned int type, unsigned int len, const char *data);
58 static int read_eeprom(int location);
60 static int mii_read(int phy_id, int location);
61 static void epic100_irq(struct nic *nic, irq_action_t action);
63 static struct nic_operations epic100_operations;
83 static unsigned int cur_rx, cur_tx; /* The next free ring entry */
85 static unsigned short eeprom[64];
87 static signed char phys[4]; /* MII device addresses. */
89 struct epic_rx_desc rx_ring[RX_RING_SIZE]
90 __attribute__ ((aligned(4)));
91 struct epic_tx_desc tx_ring[TX_RING_SIZE]
92 __attribute__ ((aligned(4)));
93 unsigned char rx_packet[PKT_BUF_SZ * RX_RING_SIZE];
94 unsigned char tx_packet[PKT_BUF_SZ * TX_RING_SIZE];
95 } epic100_bufs __shared;
96 #define rx_ring epic100_bufs.rx_ring
97 #define tx_ring epic100_bufs.tx_ring
98 #define rx_packet epic100_bufs.rx_packet
99 #define tx_packet epic100_bufs.tx_packet
101 /***********************************************************************/
102 /* Externally visible functions */
103 /***********************************************************************/
107 epic100_probe ( struct nic *nic, struct pci_device *pci ) {
111 unsigned int phy, phy_idx;
113 if (pci->ioaddr == 0)
116 /* Ideally we would detect all network cards in slot order. That would
117 be best done a central PCI probe dispatch, which wouldn't work
118 well with the current structure. So instead we detect just the
119 Epic cards in slot order. */
121 ioaddr = pci->ioaddr;
124 nic->ioaddr = pci->ioaddr & ~3;
126 /* compute all used static epic100 registers address */
127 command = ioaddr + COMMAND; /* Control Register */
128 intstat = ioaddr + INTSTAT; /* Interrupt Status */
129 intmask = ioaddr + INTMASK; /* Interrupt Mask */
130 genctl = ioaddr + GENCTL; /* General Control */
131 eectl = ioaddr + EECTL; /* EEPROM Control */
132 test = ioaddr + TEST; /* Test register (clocks) */
133 mmctl = ioaddr + MMCTL; /* MII Management Interface Control */
134 mmdata = ioaddr + MMDATA; /* MII Management Interface Data */
135 lan0 = ioaddr + LAN0; /* MAC address. (0x40-0x48) */
136 mc0 = ioaddr + MC0; /* Multicast Control */
137 rxcon = ioaddr + RXCON; /* Receive Control */
138 txcon = ioaddr + TXCON; /* Transmit Control */
139 prcdar = ioaddr + PRCDAR; /* PCI Receive Current Descr Address */
140 ptcdar = ioaddr + PTCDAR; /* PCI Transmit Current Descr Address */
141 eththr = ioaddr + ETHTHR; /* Early Transmit Threshold */
143 /* Reset the chip & bring it out of low-power mode. */
144 outl(GC_SOFT_RESET, genctl);
146 /* Disable ALL interrupts by setting the interrupt mask. */
147 outl(INTR_DISABLE, intmask);
150 * set the internal clocks:
151 * Application Note 7.15 says:
152 * In order to set the CLOCK TEST bit in the TEST register,
153 * perform the following:
155 * Write 0x0008 to the test register at least sixteen
158 * The CLOCK TEST bit is Write-Only. Writing it several times
159 * consecutively insures a successful write to the bit...
162 for (i = 0; i < 16; i++) {
163 outl(0x00000008, test);
168 unsigned short sum = 0;
169 unsigned short value;
170 for (i = 0; i < 64; i++) {
171 value = read_eeprom(i);
178 printf("EEPROM contents\n");
179 for (i = 0; i < 64; i++) {
180 printf(" %hhX%s", eeprom[i], i % 16 == 15 ? "\n" : "");
185 /* This could also be read from the EEPROM. */
186 ap = (unsigned short*)nic->node_addr;
187 for (i = 0; i < 3; i++)
188 *ap++ = inw(lan0 + i*4);
190 DBG ( " I/O %4.4x %s ", ioaddr, eth_ntoa ( nic->node_addr ) );
192 /* Find the connected MII xcvrs. */
193 for (phy = 0, phy_idx = 0; phy < 32 && phy_idx < sizeof(phys); phy++) {
194 int mii_status = mii_read(phy, 0);
196 if (mii_status != 0xffff && mii_status != 0x0000) {
197 phys[phy_idx++] = phy;
199 printf("MII transceiver found at address %d.\n", phy);
205 printf("***WARNING***: No MII transceiver found!\n");
207 /* Use the known PHY address of the EPII. */
212 nic->nic_op = &epic100_operations;
217 static void set_rx_mode(void)
219 unsigned char mc_filter[8];
221 memset(mc_filter, 0xff, sizeof(mc_filter));
223 for(i = 0; i < 4; i++)
224 outw(((unsigned short *)mc_filter)[i], mc0 + i*4);
237 /* Pull the chip out of low-power mode, and set for PCI read multiple. */
238 outl(GC_RX_FIFO_THR_64 | GC_MRC_READ_MULT | GC_ONE_COPY, genctl);
240 outl(TX_FIFO_THRESH, eththr);
242 tmp = TC_EARLY_TX_ENABLE | TX_SLOT_TIME;
244 mii_reg5 = mii_read(phys[0], 5);
245 if (mii_reg5 != 0xffff && (mii_reg5 & 0x0100)) {
247 printf(" full-duplex mode");
248 tmp |= TC_LM_FULL_DPX;
254 /* Give adress of RX and TX ring to the chip */
255 outl(virt_to_le32desc(&rx_ring), prcdar);
256 outl(virt_to_le32desc(&tx_ring), ptcdar);
258 /* Start the chip's Rx process: receive unicast and broadcast */
260 outl(CR_START_RX | CR_QUEUE_RX, command);
265 /* Initialize the Rx and Tx rings. */
267 epic100_init_ring(void)
273 for (i = 0; i < RX_RING_SIZE; i++) {
274 rx_ring[i].status = cpu_to_le32(RRING_OWN); /* Owned by Epic chip */
275 rx_ring[i].buflength = cpu_to_le32(PKT_BUF_SZ);
276 rx_ring[i].bufaddr = virt_to_bus(&rx_packet[i * PKT_BUF_SZ]);
277 rx_ring[i].next = virt_to_le32desc(&rx_ring[i + 1]) ;
279 /* Mark the last entry as wrapping the ring. */
280 rx_ring[i-1].next = virt_to_le32desc(&rx_ring[0]);
283 *The Tx buffer descriptor is filled in as needed,
284 * but we do need to clear the ownership bit.
287 for (i = 0; i < TX_RING_SIZE; i++) {
288 tx_ring[i].status = 0x0000; /* Owned by CPU */
289 tx_ring[i].buflength = 0x0000 | cpu_to_le32(TD_STDFLAGS << 16);
290 tx_ring[i].bufaddr = virt_to_bus(&tx_packet[i * PKT_BUF_SZ]);
291 tx_ring[i].next = virt_to_le32desc(&tx_ring[i + 1]);
293 tx_ring[i-1].next = virt_to_le32desc(&tx_ring[0]);
296 /* function: epic100_transmit
297 * This transmits a packet.
299 * Arguments: char d[6]: destination ethernet address.
300 * unsigned short t: ethernet protocol type.
301 * unsigned short s: size of the data-part of the packet.
302 * char *p: the data for the packet.
306 epic100_transmit(struct nic *nic, const char *destaddr, unsigned int type,
307 unsigned int len, const char *data)
309 unsigned short nstype;
314 /* Calculate the next Tx descriptor entry. */
315 entry = cur_tx % TX_RING_SIZE;
317 if ((tx_ring[entry].status & TRING_OWN) == TRING_OWN) {
318 printf("eth_transmit: Unable to transmit. status=%4.4lx. Resetting...\n",
319 tx_ring[entry].status);
325 txp = tx_packet + (entry * PKT_BUF_SZ);
327 memcpy(txp, destaddr, ETH_ALEN);
328 memcpy(txp + ETH_ALEN, nic->node_addr, ETH_ALEN);
329 nstype = htons(type);
330 memcpy(txp + 12, (char*)&nstype, 2);
331 memcpy(txp + ETH_HLEN, data, len);
335 while(len < ETH_ZLEN)
338 * Caution: the write order is important here,
339 * set the base address with the "ownership"
343 tx_ring[entry].buflength |= cpu_to_le32(len);
344 tx_ring[entry].status = cpu_to_le32(len << 16) |
345 cpu_to_le32(TRING_OWN); /* Pass ownership to the chip. */
349 /* Trigger an immediate transmit demand. */
350 outl(CR_QUEUE_TX, command);
353 /* timeout 10 ms for transmit */
354 while ((le32_to_cpu(tx_ring[entry].status) & (TRING_OWN)) &&
355 ct + 10*1000 < currticks())
358 if ((le32_to_cpu(tx_ring[entry].status) & TRING_OWN) != 0)
359 printf("Oops, transmitter timeout, status=%4.4lX\n",
360 tx_ring[entry].status);
363 /* function: epic100_poll / eth_poll
364 * This receives a packet from the network.
368 * returns: 1 if a packet was received.
369 * 0 if no pacet was received.
371 * returns the packet in the array nic->packet.
372 * returns the length of the packet in nic->packetlen.
376 epic100_poll(struct nic *nic, int retrieve)
381 entry = cur_rx % RX_RING_SIZE;
383 if ((rx_ring[entry].status & cpu_to_le32(RRING_OWN)) == RRING_OWN)
386 if ( ! retrieve ) return 1;
388 status = le32_to_cpu(rx_ring[entry].status);
389 /* We own the next entry, it's a new packet. Send it up. */
392 printf("epic_poll: entry %d status %hX\n", entry, status);
396 if (status & 0x2000) {
397 printf("epic_poll: Giant packet\n");
399 } else if (status & 0x0006) {
400 /* Rx Frame errors are counted in hardware. */
401 printf("epic_poll: Frame received with errors\n");
404 /* Omit the four octet CRC from the length. */
405 nic->packetlen = le32_to_cpu((rx_ring[entry].buflength))- 4;
406 memcpy(nic->packet, &rx_packet[entry * PKT_BUF_SZ], nic->packetlen);
410 /* Clear all error sources. */
411 outl(status & INTR_CLEARERRS, intstat);
413 /* Give the descriptor back to the chip */
414 rx_ring[entry].status = RRING_OWN;
416 /* Restart Receiver */
417 outl(CR_START_RX | CR_QUEUE_RX, command);
423 static void epic100_disable ( struct nic *nic __unused ) {
424 /* Soft reset the chip. */
425 outl(GC_SOFT_RESET, genctl);
428 static void epic100_irq(struct nic *nic __unused, irq_action_t action __unused)
441 /* Serial EEPROM section. */
443 /* EEPROM_Ctrl bits. */
444 #define EE_SHIFT_CLK 0x04 /* EEPROM shift clock. */
445 #define EE_CS 0x02 /* EEPROM chip select. */
446 #define EE_DATA_WRITE 0x08 /* EEPROM chip data in. */
447 #define EE_WRITE_0 0x01
448 #define EE_WRITE_1 0x09
449 #define EE_DATA_READ 0x10 /* EEPROM chip data out. */
450 #define EE_ENB (0x0001 | EE_CS)
452 /* The EEPROM commands include the alway-set leading bit. */
453 #define EE_WRITE_CMD (5 << 6)
454 #define EE_READ_CMD (6 << 6)
455 #define EE_ERASE_CMD (7 << 6)
457 #define eeprom_delay(n) delay(n)
460 read_eeprom(int location)
464 int read_cmd = location | EE_READ_CMD;
466 outl(EE_ENB & ~EE_CS, eectl);
469 /* Shift the read command bits out. */
470 for (i = 10; i >= 0; i--) {
471 short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
472 outl(EE_ENB | dataval, eectl);
474 outl(EE_ENB | dataval | EE_SHIFT_CLK, eectl);
476 outl(EE_ENB | dataval, eectl); /* Finish EEPROM a clock tick. */
481 for (i = 16; i > 0; i--) {
482 outl(EE_ENB | EE_SHIFT_CLK, eectl);
484 retval = (retval << 1) | ((inl(eectl) & EE_DATA_READ) ? 1 : 0);
489 /* Terminate the EEPROM access. */
490 outl(EE_ENB & ~EE_CS, eectl);
497 #define MII_WRITEOP 2
500 mii_read(int phy_id, int location)
504 outl((phy_id << 9) | (location << 4) | MII_READOP, mmctl);
505 /* Typical operation takes < 50 ticks. */
507 for (i = 4000; i > 0; i--)
508 if ((inl(mmctl) & MII_READOP) == 0)
513 static struct nic_operations epic100_operations = {
514 .connect = dummy_connect,
515 .poll = epic100_poll,
516 .transmit = epic100_transmit,
521 static struct pci_device_id epic100_nics[] = {
522 PCI_ROM(0x10b8, 0x0005, "epic100", "SMC EtherPowerII"), /* SMC 83c170 EPIC/100 */
523 PCI_ROM(0x10b8, 0x0006, "smc-83c175", "SMC EPIC/C 83c175"),
526 PCI_DRIVER ( epic100_driver, epic100_nics, PCI_NO_CLASS );
528 DRIVER ( "EPIC100", nic_driver, pci_driver, epic100_driver,
529 epic100_probe, epic100_disable );