1 /**************************************************************************
2 ETHERBOOT - BOOTP/TFTP Bootstrap Program
4 Author: Martin Renters.
7 This code is based heavily on David Greenman's if_ed.c driver and
8 Andres Vega Garcia's if_ep.c driver.
10 Copyright (C) 1993-1994, David Greenman, Martin Renters.
11 Copyright (C) 1993-1995, Andres Vega Garcia.
12 Copyright (C) 1995, Serge Babkin.
13 This software may be used, modified, copied, distributed, and sold, in
14 both source and binary form provided that the above copyright and these
15 terms are retained. Under no circumstances are the authors responsible for
16 the proper functioning of this software, nor do the authors assume any
17 responsibility for damages incurred with its use.
19 3c509 support added by Serge Babkin (babkin@hq.icb.chel.su)
23 ***************************************************************************/
25 FILE_LICENCE ( BSD2
);
29 #include <gpxe/ethernet.h>
30 #include "etherboot.h"
35 static enum { none
, bnc
, utp
} connector
= none
; /* for 3C509 */
37 /**************************************************************************
38 ETH_RESET - Reset adapter
39 ***************************************************************************/
40 void t5x9_disable ( struct nic
*nic
) {
42 outw(RX_DISABLE
, nic
->ioaddr
+ EP_COMMAND
);
43 outw(RX_DISCARD_TOP_PACK
, nic
->ioaddr
+ EP_COMMAND
);
44 while (inw(nic
->ioaddr
+ EP_STATUS
) & S_COMMAND_IN_PROGRESS
)
46 outw(TX_DISABLE
, nic
->ioaddr
+ EP_COMMAND
);
47 outw(STOP_TRANSCEIVER
, nic
->ioaddr
+ EP_COMMAND
);
49 outw(RX_RESET
, nic
->ioaddr
+ EP_COMMAND
);
50 outw(TX_RESET
, nic
->ioaddr
+ EP_COMMAND
);
51 outw(C_INTR_LATCH
, nic
->ioaddr
+ EP_COMMAND
);
52 outw(SET_RD_0_MASK
, nic
->ioaddr
+ EP_COMMAND
);
53 outw(SET_INTR_MASK
, nic
->ioaddr
+ EP_COMMAND
);
54 outw(SET_RX_FILTER
, nic
->ioaddr
+ EP_COMMAND
);
57 * wait for reset to complete
59 while (inw(nic
->ioaddr
+ EP_STATUS
) & S_COMMAND_IN_PROGRESS
)
62 GO_WINDOW(nic
->ioaddr
,0);
64 /* Disable the card */
65 outw(0, nic
->ioaddr
+ EP_W0_CONFIG_CTRL
);
67 /* Configure IRQ to none */
68 outw(SET_IRQ(0), nic
->ioaddr
+ EP_W0_RESOURCE_CFG
);
71 static void t509_enable ( struct nic
*nic
) {
75 GO_WINDOW(nic
->ioaddr
,0);
76 outw(ENABLE_DRQ_IRQ
, nic
->ioaddr
+ EP_W0_CONFIG_CTRL
);
78 GO_WINDOW(nic
->ioaddr
,2);
80 /* Reload the ether_addr. */
81 for (i
= 0; i
< ETH_ALEN
; i
++)
82 outb(nic
->node_addr
[i
], nic
->ioaddr
+ EP_W2_ADDR_0
+ i
);
84 outw(RX_RESET
, nic
->ioaddr
+ EP_COMMAND
);
85 outw(TX_RESET
, nic
->ioaddr
+ EP_COMMAND
);
87 /* Window 1 is operating window */
88 GO_WINDOW(nic
->ioaddr
,1);
89 for (i
= 0; i
< 31; i
++)
90 inb(nic
->ioaddr
+ EP_W1_TX_STATUS
);
92 /* get rid of stray intr's */
93 outw(ACK_INTR
| 0xff, nic
->ioaddr
+ EP_COMMAND
);
95 outw(SET_RD_0_MASK
| S_5_INTS
, nic
->ioaddr
+ EP_COMMAND
);
97 outw(SET_INTR_MASK
, nic
->ioaddr
+ EP_COMMAND
);
99 outw(SET_RX_FILTER
| FIL_GROUP
| FIL_INDIVIDUAL
| FIL_BRDCST
,
100 nic
->ioaddr
+ EP_COMMAND
);
103 if (connector
== bnc
) {
104 outw(START_TRANSCEIVER
, nic
->ioaddr
+ EP_COMMAND
);
108 else if (connector
== utp
) {
109 GO_WINDOW(nic
->ioaddr
,4);
110 outw(ENABLE_UTP
, nic
->ioaddr
+ EP_W4_MEDIA_TYPE
);
111 sleep(2); /* Give time for media to negotiate */
112 GO_WINDOW(nic
->ioaddr
,1);
115 /* start transceiver and receiver */
116 outw(RX_ENABLE
, nic
->ioaddr
+ EP_COMMAND
);
117 outw(TX_ENABLE
, nic
->ioaddr
+ EP_COMMAND
);
119 /* set early threshold for minimal packet length */
120 outw(SET_RX_EARLY_THRESH
| ETH_ZLEN
, nic
->ioaddr
+ EP_COMMAND
);
121 outw(SET_TX_START_THRESH
| 16, nic
->ioaddr
+ EP_COMMAND
);
124 static void t509_reset ( struct nic
*nic
) {
125 t5x9_disable ( nic
);
129 /**************************************************************************
130 ETH_TRANSMIT - Transmit a frame
131 ***************************************************************************/
132 static char padmap
[] = {
135 static void t509_transmit(
137 const char *d
, /* Destination */
138 unsigned int t
, /* Type */
139 unsigned int s
, /* size */
140 const char *p
) /* Packet */
142 register unsigned int len
;
147 printf("{l=%d,t=%hX}",s
+ETH_HLEN
,t
);
150 /* swap bytes of type */
153 len
=s
+ETH_HLEN
; /* actual length of packet */
154 pad
= padmap
[len
& 3];
157 * The 3c509 automatically pads short packets to minimum ethernet length,
158 * but we drop packets that are too large. Perhaps we should truncate
161 if (len
+ pad
> ETH_FRAME_LEN
) {
165 /* drop acknowledgements */
166 while ((status
=inb(nic
->ioaddr
+ EP_W1_TX_STATUS
)) & TXS_COMPLETE
) {
167 if (status
& (TXS_UNDERRUN
|TXS_MAX_COLLISION
|TXS_STATUS_OVERFLOW
)) {
168 outw(TX_RESET
, nic
->ioaddr
+ EP_COMMAND
);
169 outw(TX_ENABLE
, nic
->ioaddr
+ EP_COMMAND
);
171 outb(0x0, nic
->ioaddr
+ EP_W1_TX_STATUS
);
174 while (inw(nic
->ioaddr
+ EP_W1_FREE_TX
) < (unsigned short)len
+ pad
+ 4)
175 ; /* no room in FIFO */
177 outw(len
, nic
->ioaddr
+ EP_W1_TX_PIO_WR_1
);
178 outw(0x0, nic
->ioaddr
+ EP_W1_TX_PIO_WR_1
); /* Second dword meaningless */
181 outsw(nic
->ioaddr
+ EP_W1_TX_PIO_WR_1
, d
, ETH_ALEN
/2);
182 outsw(nic
->ioaddr
+ EP_W1_TX_PIO_WR_1
, nic
->node_addr
, ETH_ALEN
/2);
183 outw(t
, nic
->ioaddr
+ EP_W1_TX_PIO_WR_1
);
184 outsw(nic
->ioaddr
+ EP_W1_TX_PIO_WR_1
, p
, s
/ 2);
186 outb(*(p
+s
- 1), nic
->ioaddr
+ EP_W1_TX_PIO_WR_1
);
189 outb(0, nic
->ioaddr
+ EP_W1_TX_PIO_WR_1
); /* Padding */
191 /* wait for Tx complete */
192 while((inw(nic
->ioaddr
+ EP_STATUS
) & S_COMMAND_IN_PROGRESS
) != 0)
196 /**************************************************************************
197 ETH_POLL - Wait for a frame
198 ***************************************************************************/
199 static int t509_poll(struct nic
*nic
, int retrieve
)
201 /* common variables */
202 /* variables for 3C509 */
204 register short rx_fifo
;
206 cst
=inw(nic
->ioaddr
+ EP_STATUS
);
213 if( (cst
& S_RX_COMPLETE
)==0 ) {
214 /* acknowledge everything */
215 outw(ACK_INTR
| (cst
& S_5_INTS
), nic
->ioaddr
+ EP_COMMAND
);
216 outw(C_INTR_LATCH
, nic
->ioaddr
+ EP_COMMAND
);
221 status
= inw(nic
->ioaddr
+ EP_W1_RX_STATUS
);
223 printf("*%hX*",status
);
226 if (status
& ERR_RX
) {
227 outw(RX_DISCARD_TOP_PACK
, nic
->ioaddr
+ EP_COMMAND
);
231 rx_fifo
= status
& RX_BYTES_MASK
;
235 if ( ! retrieve
) return 1;
239 printf("[l=%d",rx_fifo
);
241 insw(nic
->ioaddr
+ EP_W1_RX_PIO_RD_1
, nic
->packet
, rx_fifo
/ 2);
243 nic
->packet
[rx_fifo
-1]=inb(nic
->ioaddr
+ EP_W1_RX_PIO_RD_1
);
244 nic
->packetlen
=rx_fifo
;
247 status
= inw(nic
->ioaddr
+ EP_W1_RX_STATUS
);
249 printf("*%hX*",status
);
251 rx_fifo
= status
& RX_BYTES_MASK
;
253 insw(nic
->ioaddr
+ EP_W1_RX_PIO_RD_1
, nic
->packet
+nic
->packetlen
, rx_fifo
/ 2);
255 nic
->packet
[nic
->packetlen
+rx_fifo
-1]=inb(nic
->ioaddr
+ EP_W1_RX_PIO_RD_1
);
256 nic
->packetlen
+=rx_fifo
;
258 printf("+%d",rx_fifo
);
261 if(( status
& RX_INCOMPLETE
)==0) {
263 printf("=%d",nic
->packetlen
);
267 udelay(1000); /* if incomplete wait 1 ms */
269 /* acknowledge reception of packet */
270 outw(RX_DISCARD_TOP_PACK
, nic
->ioaddr
+ EP_COMMAND
);
271 while (inw(nic
->ioaddr
+ EP_STATUS
) & S_COMMAND_IN_PROGRESS
)
275 unsigned short type
= 0; /* used by EDEBUG */
276 type
= (nic
->packet
[12]<<8) | nic
->packet
[13];
277 if(nic
->packet
[0]+nic
->packet
[1]+nic
->packet
[2]+nic
->packet
[3]+nic
->packet
[4]+
278 nic
->packet
[5] == 0xFF*ETH_ALEN
)
279 printf(",t=%hX,b]",type
);
281 printf(",t=%hX]",type
);
287 /**************************************************************************
288 ETH_IRQ - interrupt handling
289 ***************************************************************************/
290 static void t509_irq(struct nic
*nic __unused
, irq_action_t action __unused
)
302 /*************************************************************************
303 3Com 509 - specific routines
304 **************************************************************************/
306 static int eeprom_rdy ( uint16_t ioaddr
) {
309 for (i
= 0; is_eeprom_busy(ioaddr
) && i
< MAX_EEPROMBUSY
; i
++);
310 if (i
>= MAX_EEPROMBUSY
) {
311 /* printf("3c509: eeprom failed to come ready.\n"); */
312 /* memory in EPROM is tight */
313 /* printf("3c509: eeprom busy.\n"); */
320 * get_e: gets a 16 bits word from the EEPROM.
322 static int get_e ( uint16_t ioaddr
, int offset
) {
324 if (!eeprom_rdy(ioaddr
))
326 outw(EEPROM_CMD_RD
| offset
, ioaddr
+ EP_W0_EEPROM_COMMAND
);
327 if (!eeprom_rdy(ioaddr
))
329 return (inw(ioaddr
+ EP_W0_EEPROM_DATA
));
332 static struct nic_operations t509_operations
= {
333 .connect
= dummy_connect
,
335 .transmit
= t509_transmit
,
339 /**************************************************************************
340 ETH_PROBE - Look for an adapter
341 ***************************************************************************/
342 int t5x9_probe ( struct nic
*nic
,
343 uint16_t prod_id_check
, uint16_t prod_id_mask
) {
348 /* Check product ID */
349 prod_id
= get_e ( nic
->ioaddr
, EEPROM_PROD_ID
);
350 if ( ( prod_id
& prod_id_mask
) != prod_id_check
) {
351 printf ( "EEPROM Product ID is incorrect (%hx & %hx != %hx)\n",
352 prod_id
, prod_id_mask
, prod_id_check
);
356 /* test for presence of connectors */
357 GO_WINDOW(nic
->ioaddr
,0);
358 i
= inw(nic
->ioaddr
+ EP_W0_CONFIG_CTRL
);
359 j
= (inw(nic
->ioaddr
+ EP_W0_ADDRESS_CFG
) >> 14) & 0x3;
367 printf("10baseT not present\n");
375 printf("10base5 not present\n");
384 printf("10base2 not present\n");
389 printf("unknown connector\n");
394 * Read the station address from the eeprom
396 p
= (unsigned short *) nic
->node_addr
;
397 for (i
= 0; i
< ETH_ALEN
/ 2; i
++) {
398 p
[i
] = htons(get_e(nic
->ioaddr
,i
));
399 GO_WINDOW(nic
->ioaddr
,2);
400 outw(ntohs(p
[i
]), nic
->ioaddr
+ EP_W2_ADDR_0
+ (i
* 2));
403 DBG ( "Ethernet Address: %s\n", eth_ntoa ( nic
->node_addr
) );
407 nic
->nic_op
= &t509_operations
;