2 ** File: 3c509.c Jun. 01, 2000
4 ** Author: Giovanni Falzoni <gfalzoni@inwind.it>
6 ** This file contains specific implementation of the ethernet
7 ** device driver for 3Com Etherlink III (3c509) boards.
8 ** NOTE: The board has to be setup to disable PnP and to assign
9 ** I/O base and IRQ. The driver is for ISA bus only
14 #include <minix/drivers.h>
15 #include <minix/com.h>
16 #include <net/gen/ether.h>
17 #include <net/gen/eth_io.h>
21 #if (ENABLE_3C509 == 1)
25 static const char *const IfNamesMsg
[] = {
26 "10BaseT", "AUI", "unknown", "BNC",
30 ** Name: void el3_update_stats(dpeth_t *dep)
31 ** Function: Reads statistic counters from board
32 ** and updates local counters.
34 static void el3_update_stats(dpeth_t
* dep
)
37 /* Disables statistics while reading and switches to the correct window */
38 outw_el3(dep
, REG_CmdStatus
, CMD_StatsDisable
);
39 SetWindow(WNO_Statistics
);
41 /* Reads everything, adding values to the local counters */
42 dep
->de_stat
.ets_sendErr
+= inb_el3(dep
, REG_TxCarrierLost
); /* Reg. 00 */
43 dep
->de_stat
.ets_sendErr
+= inb_el3(dep
, REG_TxNoCD
); /* Reg. 01 */
44 dep
->de_stat
.ets_collision
+= inb_el3(dep
, REG_TxMultColl
); /* Reg. 02 */
45 dep
->de_stat
.ets_collision
+= inb_el3(dep
, REG_TxSingleColl
); /* Reg. 03 */
46 dep
->de_stat
.ets_collision
+= inb_el3(dep
, REG_TxLate
); /* Reg. 04 */
47 dep
->de_stat
.ets_recvErr
+= inb_el3(dep
, REG_RxDiscarded
); /* Reg. 05 */
48 dep
->de_stat
.ets_packetT
+= inb_el3(dep
, REG_TxFrames
); /* Reg. 06 */
49 dep
->de_stat
.ets_packetR
+= inb_el3(dep
, REG_RxFrames
); /* Reg. 07 */
50 dep
->de_stat
.ets_transDef
+= inb_el3(dep
, REG_TxDefer
); /* Reg. 08 */
51 dep
->bytes_Rx
+= (unsigned) inw_el3(dep
, REG_RxBytes
); /* Reg. 10 */
52 dep
->bytes_Tx
+= (unsigned) inw_el3(dep
, REG_TxBytes
); /* Reg. 12 */
54 /* Goes back to operating window and enables statistics */
55 SetWindow(WNO_Operating
);
56 outw_el3(dep
, REG_CmdStatus
, CMD_StatsEnable
);
62 ** Name: void el3_getstats(dpeth_t *dep)
63 ** Function: Reads statistics counters from board.
65 static void el3_getstats(dpeth_t
* dep
)
69 el3_update_stats(dep
);
75 ** Name: void el3_dodump(dpeth_t *dep)
76 ** Function: Dumps counter on screen (support for console display).
78 static void el3_dodump(dpeth_t
* dep
)
86 ** Name: void el3_rx_mode(dpeth_t *dep)
87 ** Function: Initializes receiver mode
89 static void el3_rx_mode(dpeth_t
* dep
)
92 dep
->de_recv_mode
= FilterIndividual
;
93 if (dep
->de_flags
& DEF_BROAD
) dep
->de_recv_mode
|= FilterBroadcast
;
94 if (dep
->de_flags
& DEF_MULTI
) dep
->de_recv_mode
|= FilterMulticast
;
95 if (dep
->de_flags
& DEF_PROMISC
) dep
->de_recv_mode
|= FilterPromiscuous
;
97 outw_el3(dep
, REG_CmdStatus
, CMD_RxReset
);
98 outw_el3(dep
, REG_CmdStatus
, CMD_SetRxFilter
| dep
->de_recv_mode
);
99 outw_el3(dep
, REG_CmdStatus
, CMD_RxEnable
);
105 ** Name: void el3_reset(dpeth_t *dep)
106 ** Function: Reset function specific for Etherlink hardware.
108 static void el3_reset(dpeth_t
* UNUSED(dep
))
115 ** Name: void el3_write_fifo(dpeth_t * dep, int pktsize);
116 ** Function: Writes a packet from user area to board.
117 ** Remark: Writing a word/dword at a time may result faster
118 ** but is a lot more complicated. Let's go simpler way.
120 static void el3_write_fifo(dpeth_t
* dep
, int pktsize
)
123 iovec_dat_s_t
*iovp
= &dep
->de_write_iovec
;
124 int r
, padding
= pktsize
;
126 do { /* Writes chuncks of packet from user buffers */
128 bytes
= iovp
->iod_iovec
[ix
].iov_size
; /* Size of buffer */
129 if (bytes
> pktsize
) bytes
= pktsize
;
130 /* Writes from user buffer to Tx FIFO */
131 r
= sys_safe_outsb(dep
->de_data_port
, iovp
->iod_proc_nr
,
132 iovp
->iod_iovec
[ix
].iov_grant
, 0, bytes
);
134 panic("el3_write_fifo: sys_safe_outsb failed: %d", r
);
136 if (++ix
>= IOVEC_NR
) { /* Next buffer of IO vector */
140 /* Till packet done */
141 } while ((pktsize
-= bytes
) > 0);
142 while ((padding
++ % sizeof(long)) != 0) outb(dep
->de_data_port
, 0x00);
147 ** Name: void el3_recv(dpeth_t *dep, int fromint, int size)
148 ** Function: Receive function. Called from interrupt handler or
149 ** from main to unload recv. buffer (packet to client)
151 static void el3_recv(dpeth_t
*dep
, int fromint
, int size
)
155 while ((dep
->de_flags
& DEF_READING
) && (rxptr
= dep
->de_recvq_head
)) {
157 lock(); /* Remove buffer from queue */
158 if (dep
->de_recvq_tail
== dep
->de_recvq_head
)
159 dep
->de_recvq_head
= dep
->de_recvq_tail
= NULL
;
161 dep
->de_recvq_head
= rxptr
->next
;
164 /* Copy buffer to user area and free it */
165 mem2user(dep
, rxptr
);
167 dep
->de_read_s
= rxptr
->size
;
168 dep
->de_flags
|= DEF_ACK_RECV
;
169 dep
->de_flags
&= NOT(DEF_READING
);
171 /* Return buffer to the idle pool */
172 free_buff(dep
, rxptr
);
178 ** Name: void el3_rx_complete(dpeth_t * dep);
179 ** Function: Upon receiving a packet, provides status checks
180 ** and if packet is OK copies it to local buffer.
182 static void el3_rx_complete(dpeth_t
* dep
)
188 RxStatus
= inw_el3(dep
, REG_RxStatus
);
189 pktsize
= RxStatus
& RXS_Length
; /* Mask off packet length */
191 if (RxStatus
& RXS_Error
) {
193 /* First checks for receiving errors */
194 RxStatus
&= RXS_ErrType
;
195 switch (RxStatus
) { /* Bad packet (see error type) */
198 case RXS_Runt
: dep
->de_stat
.ets_recvErr
+= 1; break;
199 case RXS_Overrun
: dep
->de_stat
.ets_OVW
+= 1; break;
200 case RXS_Framing
: dep
->de_stat
.ets_frameAll
+= 1; break;
201 case RXS_CRC
: dep
->de_stat
.ets_CRCerr
+= 1; break;
204 } else if ((rxptr
= alloc_buff(dep
, pktsize
+ sizeof(buff_t
))) == NULL
) {
205 /* Memory not available. Drop packet */
206 dep
->de_stat
.ets_fifoOver
+= 1;
209 /* Good packet. Read it from FIFO */
210 insb(dep
->de_data_port
, SELF
, rxptr
->buffer
, pktsize
);
212 rxptr
->size
= pktsize
;
214 lock(); /* Queue packet to receive queue */
215 if (dep
->de_recvq_head
== NULL
)
216 dep
->de_recvq_head
= rxptr
;
218 dep
->de_recvq_tail
->next
= rxptr
;
219 dep
->de_recvq_tail
= rxptr
;
222 /* Reply to pending Receive requests, if any */
223 el3_recv(dep
, TRUE
, pktsize
);
226 /* Discard top packet from queue */
227 outw_el3(dep
, REG_CmdStatus
, CMD_RxDiscard
);
233 ** Name: void el3_send(dpeth_t *dep, int count)
234 ** Function: Send function. Called from main to transit a packet or
235 ** from interrupt handler when Tx FIFO gets available.
237 static void el3_send(dpeth_t
* dep
, int from_int
, int count
)
244 if ((dep
->de_flags
& DEF_XMIT_BUSY
) &&
245 (now
- dep
->de_xmit_start
) > 4) {
247 DEBUG(printf("3c509: Transmitter timed out. Resetting ....\n");)
248 dep
->de_stat
.ets_sendErr
+= 1;
249 /* Resets and restars the transmitter */
250 outw_el3(dep
, REG_CmdStatus
, CMD_TxReset
);
251 outw_el3(dep
, REG_CmdStatus
, CMD_TxEnable
);
252 dep
->de_flags
&= NOT(DEF_XMIT_BUSY
);
254 if (!(dep
->de_flags
& DEF_XMIT_BUSY
)) {
256 /* Writes Transmitter preamble 1st Word (packet len, no ints) */
257 outw_el3(dep
, REG_TxFIFO
, count
);
258 /* Writes Transmitter preamble 2nd Word (all zero) */
259 outw_el3(dep
, REG_TxFIFO
, 0);
261 el3_write_fifo(dep
, count
);
263 getuptime(&dep
->de_xmit_start
);
264 dep
->de_flags
|= (DEF_XMIT_BUSY
| DEF_ACK_SEND
);
265 if (inw_el3(dep
, REG_TxFree
) > ETH_MAX_PACK_SIZE
) {
266 /* Tx has enough room for a packet of maximum size */
267 dep
->de_flags
&= NOT(DEF_XMIT_BUSY
| DEF_SENDING
);
269 /* Interrupt driver when enough room is available */
270 outw_el3(dep
, REG_CmdStatus
, CMD_SetTxAvailable
| ETH_MAX_PACK_SIZE
);
271 dep
->de_flags
&= NOT(DEF_SENDING
);
274 /* Pops Tx status stack */
275 for (ix
= 4; --ix
&& (TxStatus
= inb_el3(dep
, REG_TxStatus
)) > 0;) {
276 if (TxStatus
& 0x38) dep
->de_stat
.ets_sendErr
+= 1;
278 outw_el3(dep
, REG_CmdStatus
, CMD_TxReset
);
280 outw_el3(dep
, REG_CmdStatus
, CMD_TxEnable
);
281 outb_el3(dep
, REG_TxStatus
, 0);
288 ** Name: void el3_close(dpeth_t *dep)
289 ** Function: Stops board and makes it ready to shut down.
291 static void el3_close(dpeth_t
* dep
)
294 /* Disables statistics, Receiver and Transmitter */
295 outw_el3(dep
, REG_CmdStatus
, CMD_StatsDisable
);
296 outw_el3(dep
, REG_CmdStatus
, CMD_RxDisable
);
297 outw_el3(dep
, REG_CmdStatus
, CMD_TxDisable
);
299 if (dep
->de_if_port
== BNC_XCVR
) {
300 outw_el3(dep
, REG_CmdStatus
, CMD_StopIntXcvr
);
301 /* milli_delay(5); */
303 } else if (dep
->de_if_port
== TP_XCVR
) {
304 SetWindow(WNO_Diagnostics
);
305 outw_el3(dep
, REG_MediaStatus
, inw_el3(dep
, REG_MediaStatus
) &
306 NOT((MediaLBeatEnable
| MediaJabberEnable
)));
307 /* milli_delay(5); */
309 DEBUG(printf("%s: stopping Etherlink ... \n", dep
->de_name
));
310 /* Issues a global reset
311 outw_el3(dep, REG_CmdStatus, CMD_GlobalReset); */
312 sys_irqdisable(&dep
->de_hook
); /* Disable interrupt */
318 ** Name: void el3_interrupt(dpeth_t *dep)
319 ** Function: Interrupt handler. Acknwledges transmit interrupts
320 ** or unloads receive buffer to memory queue.
322 static void el3_interrupt(dpeth_t
* dep
)
327 for (loop
= 5; loop
> 0 && ((isr
= inw_el3(dep
, REG_CmdStatus
)) &
328 (INT_Latch
| INT_RxComplete
| INT_UpdateStats
)); loop
-= 1) {
330 if (isr
& INT_RxComplete
) /* Got a new packet */
331 el3_rx_complete(dep
);
333 if (isr
& INT_TxAvailable
) { /* Tx has room for big packets */
334 DEBUG(printf("3c509: got Tx interrupt, Status=0x%04x\n", isr
);)
335 dep
->de_flags
&= NOT(DEF_XMIT_BUSY
);
336 outw_el3(dep
, REG_CmdStatus
, CMD_Acknowledge
| INT_TxAvailable
);
337 if (dep
->de_flags
& DEF_SENDING
) /* Send pending */
338 el3_send(dep
, TRUE
, dep
->de_send_s
);
340 if (isr
& (INT_AdapterFail
| INT_RxEarly
| INT_UpdateStats
)) {
342 if (isr
& INT_UpdateStats
) /* Empties statistics */
345 if (isr
& INT_RxEarly
) /* Not really used. Do nothing */
346 outw_el3(dep
, REG_CmdStatus
, CMD_Acknowledge
| (INT_RxEarly
));
348 if (isr
& INT_AdapterFail
) {
349 /* Adapter error. Reset and re-enable receiver */
350 DEBUG(printf("3c509: got Rx fail interrupt, Status=0x%04x\n", isr
);)
352 outw_el3(dep
, REG_CmdStatus
, CMD_Acknowledge
| INT_AdapterFail
);
356 /* Acknowledge interrupt */
357 outw_el3(dep
, REG_CmdStatus
, CMD_Acknowledge
| (INT_Latch
| INT_Requested
));
363 ** Name: unsigned el3_read_eeprom(port_t port, unsigned address);
364 ** Function: Reads the EEPROM at specified address
366 static unsigned el3_read_eeprom(port_t port
, unsigned address
)
371 address
|= EL3_READ_EEPROM
;
373 milli_delay(5); /* Allows EEPROM reads */
374 for (result
= 0, bit
= 16; bit
> 0; bit
-= 1) {
375 result
= (result
<< 1) | (inb(port
) & 0x0001);
381 ** Name: void el3_read_StationAddress(dpeth_t *dep)
382 ** Function: Reads station address from board
384 static void el3_read_StationAddress(dpeth_t
* dep
)
388 for (ix
= EE_3COM_NODE_ADDR
; ix
< SA_ADDR_LEN
+EE_3COM_NODE_ADDR
;) {
389 /* Accesses with word No. */
390 rc
= el3_read_eeprom(dep
->de_id_port
, ix
/ 2);
391 /* Swaps bytes of word */
392 dep
->de_address
.ea_addr
[ix
++] = (rc
>> 8) & 0xFF;
393 dep
->de_address
.ea_addr
[ix
++] = rc
& 0xFF;
399 ** Name: void el3_open(dpeth_t *dep)
400 ** Function: Initalizes board hardware and driver data structures.
402 static void el3_open(dpeth_t
* dep
)
404 unsigned int AddrCfgReg
, ResCfgReg
;
407 el3_read_StationAddress(dep
); /* Get ethernet address */
409 /* Get address and resource configurations */
410 AddrCfgReg
= el3_read_eeprom(dep
->de_id_port
, EE_ADDR_CFG
);
411 ResCfgReg
= el3_read_eeprom(dep
->de_id_port
, EE_RESOURCE_CFG
);
412 outb(dep
->de_id_port
, EL3_ACTIVATE
); /* Activate the board */
414 /* Gets xcvr configuration */
415 dep
->de_if_port
= AddrCfgReg
& EL3_CONFIG_XCVR_MASK
;
417 AddrCfgReg
= ((AddrCfgReg
& EL3_CONFIG_IOBASE_MASK
) << 4) + EL3_IO_BASE_ADDR
;
418 if (AddrCfgReg
!= dep
->de_base_port
)
419 panic("Bad I/O port for Etherlink board");
422 dep
->de_irq
&= NOT(DEI_DEFAULT
); /* Strips the default flag */
423 if (ResCfgReg
!= dep
->de_irq
) panic("Bad IRQ for Etherlink board");
425 SetWindow(WNO_Setup
);
427 /* Reset transmitter and receiver */
428 outw_el3(dep
, REG_CmdStatus
, CMD_TxReset
);
429 outw_el3(dep
, REG_CmdStatus
, CMD_RxReset
);
431 /* Enable the adapter */
432 outb_el3(dep
, REG_CfgControl
, EL3_EnableAdapter
);
433 /* Disable Status bits */
434 outw_el3(dep
, REG_CmdStatus
, CMD_SetStatusEnab
+ 0x00);
436 /* Set "my own" address */
437 SetWindow(WNO_StationAddress
);
438 for (ix
= 0; ix
< 6; ix
+= 1)
439 outb_el3(dep
, REG_SA0_1
+ ix
, dep
->de_address
.ea_addr
[ix
]);
441 /* Start Transceivers as required */
442 if (dep
->de_if_port
== BNC_XCVR
) {
443 /* Start internal transceiver for Coaxial cable */
444 outw_el3(dep
, REG_CmdStatus
, CMD_StartIntXcvr
);
447 } else if (dep
->de_if_port
== TP_XCVR
) {
448 /* Start internal transceiver for Twisted pair cable */
449 SetWindow(WNO_Diagnostics
);
450 outw_el3(dep
, REG_MediaStatus
,
451 inw_el3(dep
, REG_MediaStatus
) | (MediaLBeatEnable
| MediaJabberEnable
));
454 /* Switch to the statistic window, and clear counts (by reading) */
455 SetWindow(WNO_Statistics
);
456 for (ix
= REG_TxCarrierLost
; ix
<= REG_TxDefer
; ix
+= 1) inb_el3(dep
, ix
);
457 inw_el3(dep
, REG_RxBytes
);
458 inw_el3(dep
, REG_TxBytes
);
460 /* Switch to operating window for normal use */
461 SetWindow(WNO_Operating
);
463 /* Receive individual address & broadcast. (Mofified later by rx_mode) */
464 outw_el3(dep
, REG_CmdStatus
, CMD_SetRxFilter
|
465 (FilterIndividual
| FilterBroadcast
));
467 /* Turn on statistics */
468 outw_el3(dep
, REG_CmdStatus
, CMD_StatsEnable
);
470 /* Enable transmitter and receiver */
471 outw_el3(dep
, REG_CmdStatus
, CMD_TxEnable
);
472 outw_el3(dep
, REG_CmdStatus
, CMD_RxEnable
);
474 /* Enable all the status bits */
475 outw_el3(dep
, REG_CmdStatus
, CMD_SetStatusEnab
| 0xFF);
477 /* Acknowledge all interrupts to clear adapter. Enable interrupts */
478 outw_el3(dep
, REG_CmdStatus
, CMD_Acknowledge
| 0xFF);
479 outw_el3(dep
, REG_CmdStatus
, CMD_SetIntMask
|
480 (INT_Latch
| INT_TxAvailable
| INT_RxComplete
| INT_UpdateStats
));
482 /* Ready to operate, sets the environment for eth_task */
483 dep
->de_data_port
= dep
->de_base_port
;
484 /* Allocates Rx/Tx buffers */
485 init_buff(dep
, NULL
);
487 /* Device specific functions */
488 dep
->de_recvf
= el3_recv
;
489 dep
->de_sendf
= el3_send
;
490 dep
->de_flagsf
= el3_rx_mode
;
491 dep
->de_resetf
= el3_reset
;
492 dep
->de_getstatsf
= el3_getstats
;
493 dep
->de_dumpstatsf
= el3_dodump
;
494 dep
->de_interruptf
= el3_interrupt
;
496 printf("%s: Etherlink III (%s) at %X:%d, %s port - ",
497 dep
->de_name
, "3c509", dep
->de_base_port
, dep
->de_irq
,
498 IfNamesMsg
[dep
->de_if_port
>> 14]);
499 for (ix
= 0; ix
< SA_ADDR_LEN
; ix
+= 1)
500 printf("%02X%c", dep
->de_address
.ea_addr
[ix
],
501 ix
< SA_ADDR_LEN
- 1 ? ':' : '\n');
507 ** Name: unsigned int el3_checksum(port_t port);
508 ** Function: Reads EEPROM and computes checksum.
510 static unsigned short el3_checksum(port_t port
)
512 unsigned short rc
, checksum
, address
;
513 unsigned char lo
, hi
;
515 for (checksum
= address
= 0; address
< 15; address
+= 1) {
516 rc
= el3_read_eeprom(port
, address
);
518 hi
= (rc
>> 8) & 0xFF;
519 if ((address
== EE_PROD_ID
&& (rc
& EE_PROD_ID_MASK
) != EL3_PRODUCT_ID
) ||
520 (address
== EE_3COM_CODE
&& rc
!= EL3_3COM_CODE
))
522 if (address
== EE_ADDR_CFG
||
523 address
== EE_RESOURCE_CFG
||
524 address
== EE_SW_CONFIG_INFO
) {
531 rc
= ((unsigned) hi
<< 8) + lo
;
534 rc
= el3_read_eeprom(port
, address
);
535 return(checksum
^= rc
); /* If OK checksum is 0 */
539 ** Name: void el3_write_id(port_t port);
540 ** Function: Writes the ID sequence to the board.
542 static void el3_write_id(port_t port
)
546 outb(port
, 0); /* Selects the ID port */
547 outb(port
, 0); /* Resets hardware pattern generator */
548 for (pattern
= ix
= 0x00FF; ix
> 0; ix
-= 1) {
551 pattern
= (pattern
& 0x0100) ? pattern
^ 0xCF : pattern
;
557 ** Name: int el3_probe(dpeth_t *dep)
558 ** Function: Checks for presence of the board.
560 PUBLIC
int el3_probe(dpeth_t
* dep
)
564 /* Don't ask me what is this for !! */
565 outb(0x0279, 0x02); /* Select PnP config control register. */
566 outb(0x0A79, 0x02); /* Return to WaitForKey state. */
567 /* Tests I/O ports in the 0x1xF range for a valid ID port */
568 for (id_port
= 0x110; id_port
< 0x200; id_port
+= 0x10) {
571 if (inb(id_port
) & 0x01) break;
573 if (id_port
== 0x200) return 0; /* No board responding */
575 el3_write_id(id_port
);
576 outb(id_port
, EL3_ID_GLOBAL_RESET
); /* Reset the board */
577 milli_delay(5); /* Technical reference says 162 micro sec. */
578 el3_write_id(id_port
);
579 outb(id_port
, EL3_SET_TAG_REGISTER
);
582 dep
->de_id_port
= id_port
; /* Stores ID port No. */
583 dep
->de_ramsize
= /* RAM size is meaningless */
584 dep
->de_offset_page
= 0;
585 dep
->de_linmem
= 0L; /* Access is via I/O port */
587 /* Device specific functions */
588 dep
->de_initf
= el3_open
;
589 dep
->de_stopf
= el3_close
;
591 return(el3_checksum(id_port
) == 0); /* Etherlink board found/not found */
594 #endif /* ENABLE_3C509 */