Merge branch 'master' of /pub/scm/gpxe
[gpxe.git] / src / drivers / net / 3c595.c
blob3553fa611b4e03976df709e32ce4703c88aa7420
1 /*
2 * 3c595.c -- 3COM 3C595 Fast Etherlink III PCI driver for etherboot
4 * Copyright (C) 2000 Shusuke Nisiyama <shu@athena.qe.eng.hokudai.ac.jp>
5 * All rights reserved.
6 * Mar. 14, 2000
8 * This software may be used, modified, copied, distributed, and sold, in
9 * both source and binary form provided that the above copyright and these
10 * terms are retained. Under no circumstances are the authors responsible for
11 * the proper functioning of this software, nor do the authors assume any
12 * responsibility for damages incurred with its use.
14 * This code is based on Martin Renters' etherboot-4.4.3 3c509.c and
15 * Herb Peyerl's FreeBSD 3.4-RELEASE if_vx.c driver.
17 * Copyright (C) 1993-1994, David Greenman, Martin Renters.
18 * Copyright (C) 1993-1995, Andres Vega Garcia.
19 * Copyright (C) 1995, Serge Babkin.
21 * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
23 * timlegge 08-24-2003 Add Multicast Support
26 /* #define EDEBUG */
28 #include "etherboot.h"
29 #include "nic.h"
30 #include <gpxe/pci.h>
31 #include <gpxe/ethernet.h>
32 #include "3c595.h"
33 #include "timer.h"
35 static struct nic_operations t595_operations;
37 static unsigned short eth_nic_base;
38 static unsigned short vx_connector, vx_connectors;
40 static struct connector_entry {
41 int bit;
42 char *name;
43 } conn_tab[VX_CONNECTORS] = {
44 #define CONNECTOR_UTP 0
45 { 0x08, "utp"},
46 #define CONNECTOR_AUI 1
47 { 0x20, "aui"},
48 /* dummy */
49 { 0, "???"},
50 #define CONNECTOR_BNC 3
51 { 0x10, "bnc"},
52 #define CONNECTOR_TX 4
53 { 0x02, "tx"},
54 #define CONNECTOR_FX 5
55 { 0x04, "fx"},
56 #define CONNECTOR_MII 6
57 { 0x40, "mii"},
58 { 0, "???"}
61 static void vxgetlink(void);
62 static void vxsetlink(void);
64 /**************************************************************************
65 ETH_RESET - Reset adapter
66 ***************************************************************************/
67 static void t595_reset(struct nic *nic)
69 int i;
71 /***********************************************************
72 Reset 3Com 595 card
73 *************************************************************/
75 /* stop card */
76 outw(RX_DISABLE, BASE + VX_COMMAND);
77 outw(RX_DISCARD_TOP_PACK, BASE + VX_COMMAND);
78 VX_BUSY_WAIT;
79 outw(TX_DISABLE, BASE + VX_COMMAND);
80 outw(STOP_TRANSCEIVER, BASE + VX_COMMAND);
81 udelay(8000);
82 outw(RX_RESET, BASE + VX_COMMAND);
83 VX_BUSY_WAIT;
84 outw(TX_RESET, BASE + VX_COMMAND);
85 VX_BUSY_WAIT;
86 outw(C_INTR_LATCH, BASE + VX_COMMAND);
87 outw(SET_RD_0_MASK, BASE + VX_COMMAND);
88 outw(SET_INTR_MASK, BASE + VX_COMMAND);
89 outw(SET_RX_FILTER, BASE + VX_COMMAND);
92 * initialize card
94 VX_BUSY_WAIT;
96 GO_WINDOW(0);
98 /* Disable the card */
99 /* outw(0, BASE + VX_W0_CONFIG_CTRL); */
101 /* Configure IRQ to none */
102 /* outw(SET_IRQ(0), BASE + VX_W0_RESOURCE_CFG); */
104 /* Enable the card */
105 /* outw(ENABLE_DRQ_IRQ, BASE + VX_W0_CONFIG_CTRL); */
107 GO_WINDOW(2);
109 /* Reload the ether_addr. */
110 for (i = 0; i < ETH_ALEN; i++)
111 outb(nic->node_addr[i], BASE + VX_W2_ADDR_0 + i);
113 outw(RX_RESET, BASE + VX_COMMAND);
114 VX_BUSY_WAIT;
115 outw(TX_RESET, BASE + VX_COMMAND);
116 VX_BUSY_WAIT;
118 /* Window 1 is operating window */
119 GO_WINDOW(1);
120 for (i = 0; i < 31; i++)
121 inb(BASE + VX_W1_TX_STATUS);
123 outw(SET_RD_0_MASK | S_CARD_FAILURE | S_RX_COMPLETE |
124 S_TX_COMPLETE | S_TX_AVAIL, BASE + VX_COMMAND);
125 outw(SET_INTR_MASK | S_CARD_FAILURE | S_RX_COMPLETE |
126 S_TX_COMPLETE | S_TX_AVAIL, BASE + VX_COMMAND);
129 * Attempt to get rid of any stray interrupts that occured during
130 * configuration. On the i386 this isn't possible because one may
131 * already be queued. However, a single stray interrupt is
132 * unimportant.
135 outw(ACK_INTR | 0xff, BASE + VX_COMMAND);
137 outw(SET_RX_FILTER | FIL_INDIVIDUAL |
138 FIL_BRDCST|FIL_MULTICAST, BASE + VX_COMMAND);
140 vxsetlink();
142 int i,j;
143 i = CONNECTOR_TX;
144 GO_WINDOW(3);
145 j = inl(BASE + VX_W3_INTERNAL_CFG) & ~INTERNAL_CONNECTOR_MASK;
146 outl(BASE + VX_W3_INTERNAL_CFG, j | (i <<INTERNAL_CONNECTOR_BITS));
147 GO_WINDOW(4);
148 outw(LINKBEAT_ENABLE, BASE + VX_W4_MEDIA_TYPE);
149 GO_WINDOW(1);
152 /* start tranciever and receiver */
153 outw(RX_ENABLE, BASE + VX_COMMAND);
154 outw(TX_ENABLE, BASE + VX_COMMAND);
158 /**************************************************************************
159 ETH_TRANSMIT - Transmit a frame
160 ***************************************************************************/
161 static char padmap[] = {
162 0, 3, 2, 1};
164 static void t595_transmit(
165 struct nic *nic,
166 const char *d, /* Destination */
167 unsigned int t, /* Type */
168 unsigned int s, /* size */
169 const char *p) /* Packet */
171 register int len;
172 int pad;
173 int status;
175 #ifdef EDEBUG
176 printf("{l=%d,t=%hX}",s+ETH_HLEN,t);
177 #endif
179 /* swap bytes of type */
180 t= htons(t);
182 len=s+ETH_HLEN; /* actual length of packet */
183 pad = padmap[len & 3];
186 * The 3c595 automatically pads short packets to minimum ethernet length,
187 * but we drop packets that are too large. Perhaps we should truncate
188 * them instead?
190 if (len + pad > ETH_FRAME_LEN) {
191 return;
194 /* drop acknowledgements */
195 while(( status=inb(BASE + VX_W1_TX_STATUS) )& TXS_COMPLETE ) {
196 if(status & (TXS_UNDERRUN|TXS_MAX_COLLISION|TXS_STATUS_OVERFLOW)) {
197 outw(TX_RESET, BASE + VX_COMMAND);
198 outw(TX_ENABLE, BASE + VX_COMMAND);
201 outb(0x0, BASE + VX_W1_TX_STATUS);
204 while (inw(BASE + VX_W1_FREE_TX) < len + pad + 4) {
205 /* no room in FIFO */
208 outw(len, BASE + VX_W1_TX_PIO_WR_1);
209 outw(0x0, BASE + VX_W1_TX_PIO_WR_1); /* Second dword meaningless */
211 /* write packet */
212 outsw(BASE + VX_W1_TX_PIO_WR_1, d, ETH_ALEN/2);
213 outsw(BASE + VX_W1_TX_PIO_WR_1, nic->node_addr, ETH_ALEN/2);
214 outw(t, BASE + VX_W1_TX_PIO_WR_1);
215 outsw(BASE + VX_W1_TX_PIO_WR_1, p, s / 2);
216 if (s & 1)
217 outb(*(p+s - 1), BASE + VX_W1_TX_PIO_WR_1);
219 while (pad--)
220 outb(0, BASE + VX_W1_TX_PIO_WR_1); /* Padding */
222 /* wait for Tx complete */
223 while((inw(BASE + VX_STATUS) & S_COMMAND_IN_PROGRESS) != 0)
227 /**************************************************************************
228 ETH_POLL - Wait for a frame
229 ***************************************************************************/
230 static int t595_poll(struct nic *nic, int retrieve)
232 /* common variables */
233 /* variables for 3C595 */
234 short status, cst;
235 register short rx_fifo;
237 cst=inw(BASE + VX_STATUS);
239 #ifdef EDEBUG
240 if(cst & 0x1FFF)
241 printf("-%hX-",cst);
242 #endif
244 if( (cst & S_RX_COMPLETE)==0 ) {
245 /* acknowledge everything */
246 outw(ACK_INTR | cst, BASE + VX_COMMAND);
247 outw(C_INTR_LATCH, BASE + VX_COMMAND);
249 return 0;
252 status = inw(BASE + VX_W1_RX_STATUS);
253 #ifdef EDEBUG
254 printf("*%hX*",status);
255 #endif
257 if (status & ERR_RX) {
258 outw(RX_DISCARD_TOP_PACK, BASE + VX_COMMAND);
259 return 0;
262 rx_fifo = status & RX_BYTES_MASK;
263 if (rx_fifo==0)
264 return 0;
266 if ( ! retrieve ) return 1;
268 /* read packet */
269 #ifdef EDEBUG
270 printf("[l=%d",rx_fifo);
271 #endif
272 insw(BASE + VX_W1_RX_PIO_RD_1, nic->packet, rx_fifo / 2);
273 if(rx_fifo & 1)
274 nic->packet[rx_fifo-1]=inb(BASE + VX_W1_RX_PIO_RD_1);
275 nic->packetlen=rx_fifo;
277 while(1) {
278 status = inw(BASE + VX_W1_RX_STATUS);
279 #ifdef EDEBUG
280 printf("*%hX*",status);
281 #endif
282 rx_fifo = status & RX_BYTES_MASK;
284 if(rx_fifo>0) {
285 insw(BASE + VX_W1_RX_PIO_RD_1, nic->packet+nic->packetlen, rx_fifo / 2);
286 if(rx_fifo & 1)
287 nic->packet[nic->packetlen+rx_fifo-1]=inb(BASE + VX_W1_RX_PIO_RD_1);
288 nic->packetlen+=rx_fifo;
289 #ifdef EDEBUG
290 printf("+%d",rx_fifo);
291 #endif
293 if(( status & RX_INCOMPLETE )==0) {
294 #ifdef EDEBUG
295 printf("=%d",nic->packetlen);
296 #endif
297 break;
299 udelay(1000);
302 /* acknowledge reception of packet */
303 outw(RX_DISCARD_TOP_PACK, BASE + VX_COMMAND);
304 while (inw(BASE + VX_STATUS) & S_COMMAND_IN_PROGRESS);
305 #ifdef EDEBUG
307 unsigned short type = 0; /* used by EDEBUG */
308 type = (nic->packet[12]<<8) | nic->packet[13];
309 if(nic->packet[0]+nic->packet[1]+nic->packet[2]+nic->packet[3]+nic->packet[4]+
310 nic->packet[5] == 0xFF*ETH_ALEN)
311 printf(",t=%hX,b]",type);
312 else
313 printf(",t=%hX]",type);
315 #endif
316 return 1;
320 /*************************************************************************
321 3Com 595 - specific routines
322 **************************************************************************/
324 static int
325 eeprom_rdy()
327 int i;
329 for (i = 0; is_eeprom_busy(BASE) && i < MAX_EEPROMBUSY; i++)
330 udelay(1000);
331 if (i >= MAX_EEPROMBUSY) {
332 /* printf("3c595: eeprom failed to come ready.\n"); */
333 printf("3c595: eeprom is busy.\n"); /* memory in EPROM is tight */
334 return (0);
336 return (1);
340 * get_e: gets a 16 bits word from the EEPROM. we must have set the window
341 * before
343 static int
344 get_e(offset)
345 int offset;
347 if (!eeprom_rdy())
348 return (0xffff);
349 outw(EEPROM_CMD_RD | offset, BASE + VX_W0_EEPROM_COMMAND);
350 if (!eeprom_rdy())
351 return (0xffff);
352 return (inw(BASE + VX_W0_EEPROM_DATA));
355 static void
356 vxgetlink(void)
358 int n, k;
360 GO_WINDOW(3);
361 vx_connectors = inw(BASE + VX_W3_RESET_OPT) & 0x7f;
362 for (n = 0, k = 0; k < VX_CONNECTORS; k++) {
363 if (vx_connectors & conn_tab[k].bit) {
364 if (n > 0) {
365 printf("/");
367 printf(conn_tab[k].name);
368 n++;
371 if (vx_connectors == 0) {
372 printf("no connectors!");
373 return;
375 GO_WINDOW(3);
376 vx_connector = (inl(BASE + VX_W3_INTERNAL_CFG)
377 & INTERNAL_CONNECTOR_MASK)
378 >> INTERNAL_CONNECTOR_BITS;
379 if (vx_connector & 0x10) {
380 vx_connector &= 0x0f;
381 printf("[*%s*]", conn_tab[vx_connector].name);
382 printf(": disable 'auto select' with DOS util!");
383 } else {
384 printf("[*%s*]", conn_tab[vx_connector].name);
388 static void
389 vxsetlink(void)
391 int i, j;
392 char *reason, *warning;
393 static char prev_conn = -1;
395 if (prev_conn == -1) {
396 prev_conn = vx_connector;
399 i = vx_connector; /* default in EEPROM */
400 reason = "default";
401 warning = 0;
403 if ((vx_connectors & conn_tab[vx_connector].bit) == 0) {
404 warning = "strange connector type in EEPROM.";
405 reason = "forced";
406 i = CONNECTOR_UTP;
409 if (warning != 0) {
410 printf("warning: %s\n", warning);
412 printf("selected %s. (%s)\n", conn_tab[i].name, reason);
414 /* Set the selected connector. */
415 GO_WINDOW(3);
416 j = inl(BASE + VX_W3_INTERNAL_CFG) & ~INTERNAL_CONNECTOR_MASK;
417 outl(j | (i <<INTERNAL_CONNECTOR_BITS), BASE + VX_W3_INTERNAL_CFG);
419 /* First, disable all. */
420 outw(STOP_TRANSCEIVER, BASE + VX_COMMAND);
421 udelay(8000);
422 GO_WINDOW(4);
423 outw(0, BASE + VX_W4_MEDIA_TYPE);
425 /* Second, enable the selected one. */
426 switch(i) {
427 case CONNECTOR_UTP:
428 GO_WINDOW(4);
429 outw(ENABLE_UTP, BASE + VX_W4_MEDIA_TYPE);
430 break;
431 case CONNECTOR_BNC:
432 outw(START_TRANSCEIVER,BASE + VX_COMMAND);
433 udelay(8000);
434 break;
435 case CONNECTOR_TX:
436 case CONNECTOR_FX:
437 GO_WINDOW(4);
438 outw(LINKBEAT_ENABLE, BASE + VX_W4_MEDIA_TYPE);
439 break;
440 default: /* AUI and MII fall here */
441 break;
443 GO_WINDOW(1);
446 static void t595_disable ( struct nic *nic ) {
448 t595_reset(nic);
450 outw(STOP_TRANSCEIVER, BASE + VX_COMMAND);
451 udelay(8000);
452 GO_WINDOW(4);
453 outw(0, BASE + VX_W4_MEDIA_TYPE);
454 GO_WINDOW(1);
457 static void t595_irq(struct nic *nic __unused, irq_action_t action __unused)
459 switch ( action ) {
460 case DISABLE :
461 break;
462 case ENABLE :
463 break;
464 case FORCE :
465 break;
469 /**************************************************************************
470 ETH_PROBE - Look for an adapter
471 ***************************************************************************/
472 static int t595_probe ( struct nic *nic, struct pci_device *pci ) {
474 int i;
475 unsigned short *p;
477 if (pci->ioaddr == 0)
478 return 0;
479 eth_nic_base = pci->ioaddr;
481 nic->irqno = 0;
482 pci_fill_nic ( nic, pci );
483 nic->ioaddr = pci->ioaddr;
485 GO_WINDOW(0);
486 outw(GLOBAL_RESET, BASE + VX_COMMAND);
487 VX_BUSY_WAIT;
489 vxgetlink();
492 printf("\nEEPROM:");
493 for (i = 0; i < (EEPROMSIZE/2); i++) {
494 printf("%hX:", get_e(i));
496 printf("\n");
499 * Read the station address from the eeprom
501 p = (unsigned short *) nic->node_addr;
502 for (i = 0; i < 3; i++) {
503 GO_WINDOW(0);
504 p[i] = htons(get_e(EEPROM_OEM_ADDR_0 + i));
505 GO_WINDOW(2);
506 outw(ntohs(p[i]), BASE + VX_W2_ADDR_0 + (i * 2));
509 DBG ( "Ethernet address: %s\n", eth_ntoa (nic->node_addr) );
511 t595_reset(nic);
512 nic->nic_op = &t595_operations;
513 return 1;
517 static struct nic_operations t595_operations = {
518 .connect = dummy_connect,
519 .poll = t595_poll,
520 .transmit = t595_transmit,
521 .irq = t595_irq,
525 static struct pci_device_id t595_nics[] = {
526 PCI_ROM(0x10b7, 0x5900, "3c590", "3Com590"), /* Vortex 10Mbps */
527 PCI_ROM(0x10b7, 0x5950, "3c595", "3Com595"), /* Vortex 100baseTx */
528 PCI_ROM(0x10b7, 0x5951, "3c595-1", "3Com595"), /* Vortex 100baseT4 */
529 PCI_ROM(0x10b7, 0x5952, "3c595-2", "3Com595"), /* Vortex 100base-MII */
530 PCI_ROM(0x10b7, 0x9000, "3c900-tpo", "3Com900-TPO"), /* 10 Base TPO */
531 PCI_ROM(0x10b7, 0x9001, "3c900-t4", "3Com900-Combo"), /* 10/100 T4 */
532 PCI_ROM(0x10b7, 0x9004, "3c900b-tpo", "3Com900B-TPO"), /* 10 Base TPO */
533 PCI_ROM(0x10b7, 0x9005, "3c900b-combo", "3Com900B-Combo"), /* 10 Base Combo */
534 PCI_ROM(0x10b7, 0x9006, "3c900b-tpb2", "3Com900B-2/T"), /* 10 Base TP and Base2 */
535 PCI_ROM(0x10b7, 0x900a, "3c900b-fl", "3Com900B-FL"), /* 10 Base F */
536 PCI_ROM(0x10b7, 0x9800, "3c980-cyclone-1", "3Com980-Cyclone"), /* Cyclone */
537 PCI_ROM(0x10b7, 0x9805, "3c9805-1", "3Com9805"), /* Dual Port Server Cyclone */
538 PCI_ROM(0x10b7, 0x7646, "3csoho100-tx-1", "3CSOHO100-TX"), /* Hurricane */
539 PCI_ROM(0x10b7, 0x4500, "3c450-1", "3Com450 HomePNA Tornado"),
542 PCI_DRIVER ( t595_driver, t595_nics, PCI_NO_CLASS );
544 DRIVER ( "3C595", nic_driver, pci_driver, t595_driver,
545 t595_probe, t595_disable );
548 * Local variables:
549 * c-basic-offset: 8
550 * End: