1 /* epic100.c: A SMC 83c170 EPIC/100 fast ethernet driver for Etherboot */
3 #define LINUX_OUT_MACROS
12 #define virt_to_bus(x) ((unsigned long)x)
14 #define TX_RING_SIZE 2 /* use at least 2 buffers for TX */
15 #define RX_RING_SIZE 2
17 #define PKT_BUF_SZ 1536 /* Size of each temporary Tx/Rx buffer.*/
25 #define EPIC_DEBUG 0 /* debug level */
27 /* The EPIC100 Rx and Tx buffer descriptors. */
29 unsigned short status
;
30 unsigned short rxlength
;
31 unsigned long bufaddr
;
32 unsigned short buflength
;
33 unsigned short control
;
37 /* description of the tx descriptors control bits commonly used */
38 #define TD_STDFLAGS TD_LASTDESC
41 unsigned short status
;
42 unsigned short txlength
;
43 unsigned long bufaddr
;
44 unsigned short buflength
;
45 unsigned short control
;
49 #define delay(nanosec) do { int _i = 3; while (--_i > 0) \
50 { __SLOW_DOWN_IO; }} while (0)
52 static void epic100_open(void);
53 static void epic100_init_ring(void);
54 static void epic100_disable(struct nic
*nic
);
55 static int epic100_poll(struct nic
*nic
);
56 static void epic100_transmit(struct nic
*nic
, const char *destaddr
,
57 unsigned int type
, unsigned int len
, const char *data
);
58 static int read_eeprom(int location
);
59 static int mii_read(int phy_id
, int location
);
78 static unsigned int cur_rx
, cur_tx
; /* The next free ring entry */
80 static unsigned short eeprom
[64];
82 static signed char phys
[4]; /* MII device addresses. */
83 static struct epic_rx_desc rx_ring
[RX_RING_SIZE
];
84 static struct epic_tx_desc tx_ring
[TX_RING_SIZE
];
85 #ifdef USE_LOWMEM_BUFFER
86 #define rx_packet ((char *)0x10000 - PKT_BUF_SZ * RX_RING_SIZE)
87 #define tx_packet ((char *)0x10000 - PKT_BUF_SZ * RX_RING_SIZE - PKT_BUF_SZ * TX_RING_SIZE)
89 static char rx_packet
[PKT_BUF_SZ
* RX_RING_SIZE
];
90 static char tx_packet
[PKT_BUF_SZ
* TX_RING_SIZE
];
93 /***********************************************************************/
94 /* Externally visible functions */
95 /***********************************************************************/
98 epic100_reset(struct nic
*nic
)
100 /* Soft reset the chip. */
101 outl(GC_SOFT_RESET
, genctl
);
105 epic100_probe(struct nic
*nic
, unsigned short *probeaddrs
)
107 unsigned short sum
= 0;
108 unsigned short value
;
111 unsigned int phy
, phy_idx
;
113 if (probeaddrs
== 0 || probeaddrs
[0] == 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
= probeaddrs
[0] & ~3; /* Mask the bit that says "this is an io addr" */
123 /* compute all used static epic100 registers address */
124 command
= ioaddr
+ COMMAND
; /* Control Register */
125 intstat
= ioaddr
+ INTSTAT
; /* Interrupt Status */
126 intmask
= ioaddr
+ INTMASK
; /* Interrupt Mask */
127 genctl
= ioaddr
+ GENCTL
; /* General Control */
128 eectl
= ioaddr
+ EECTL
; /* EEPROM Control */
129 test
= ioaddr
+ TEST
; /* Test register (clocks) */
130 mmctl
= ioaddr
+ MMCTL
; /* MII Management Interface Control */
131 mmdata
= ioaddr
+ MMDATA
; /* MII Management Interface Data */
132 lan0
= ioaddr
+ LAN0
; /* MAC address. (0x40-0x48) */
133 rxcon
= ioaddr
+ RXCON
; /* Receive Control */
134 txcon
= ioaddr
+ TXCON
; /* Transmit Control */
135 prcdar
= ioaddr
+ PRCDAR
; /* PCI Receive Current Descr Address */
136 ptcdar
= ioaddr
+ PTCDAR
; /* PCI Transmit Current Descr Address */
137 eththr
= ioaddr
+ ETHTHR
; /* Early Transmit Threshold */
139 /* Reset the chip & bring it out of low-power mode. */
140 outl(GC_SOFT_RESET
, genctl
);
142 /* Disable ALL interrupts by setting the interrupt mask. */
143 outl(INTR_DISABLE
, intmask
);
146 * set the internal clocks:
147 * Application Note 7.15 says:
148 * In order to set the CLOCK TEST bit in the TEST register,
149 * perform the following:
151 * Write 0x0008 to the test register at least sixteen
154 * The CLOCK TEST bit is Write-Only. Writing it several times
155 * consecutively insures a successful write to the bit...
158 for (i
= 0; i
< 16; i
++) {
159 outl(0x00000008, test
);
163 for (i
= 0; i
< 64; i
++) {
164 value
= read_eeprom(i
);
170 printf("EEPROM contents\n");
171 for (i
= 0; i
< 64; i
++) {
172 printf(" %hhX%s", eeprom
[i
], i
% 16 == 15 ? "\n" : "");
177 /* This could also be read from the EEPROM. */
178 ap
= (unsigned short*)nic
->node_addr
;
179 for (i
= 0; i
< 3; i
++)
180 *ap
++ = inw(lan0
+ i
*4);
182 printf(" I/O %#hX %! ", ioaddr
, nic
->node_addr
);
184 /* Find the connected MII xcvrs. */
185 for (phy
= 0, phy_idx
= 0; phy
< 32 && phy_idx
< sizeof(phys
); phy
++) {
186 int mii_status
= mii_read(phy
, 0);
188 if (mii_status
!= 0xffff && mii_status
!= 0x0000) {
189 phys
[phy_idx
++] = phy
;
191 printf("MII transceiver found at address %d.\n", phy
);
197 printf("***WARNING***: No MII transceiver found!\n");
199 /* Use the known PHY address of the EPII. */
205 nic
->reset
= epic100_reset
;
206 nic
->poll
= epic100_poll
;
207 nic
->transmit
= epic100_transmit
;
208 nic
->disable
= epic100_disable
;
222 /* Pull the chip out of low-power mode, and set for PCI read multiple. */
223 outl(GC_RX_FIFO_THR_64
| GC_MRC_READ_MULT
| GC_ONE_COPY
, genctl
);
225 outl(TX_FIFO_THRESH
, eththr
);
227 tmp
= TC_EARLY_TX_ENABLE
| TX_SLOT_TIME
;
229 mii_reg5
= mii_read(phys
[0], 5);
230 if (mii_reg5
!= 0xffff && (mii_reg5
& 0x0100)) {
232 printf(" full-duplex mode");
233 tmp
|= TC_LM_FULL_DPX
;
239 /* Give adress of RX and TX ring to the chip */
240 outl(virt_to_bus(&rx_ring
), prcdar
);
241 outl(virt_to_bus(&tx_ring
), ptcdar
);
243 /* Start the chip's Rx process: receive unicast and broadcast */
245 outl(CR_START_RX
| CR_QUEUE_RX
, command
);
250 /* Initialize the Rx and Tx rings. */
252 epic100_init_ring(void)
260 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
261 rx_ring
[i
].status
= RRING_OWN
; /* Owned by Epic chip */
262 rx_ring
[i
].buflength
= PKT_BUF_SZ
;
263 rx_ring
[i
].bufaddr
= virt_to_bus(p
+ (PKT_BUF_SZ
* i
));
264 rx_ring
[i
].control
= 0;
265 rx_ring
[i
].next
= virt_to_bus(&(rx_ring
[i
+ 1]) );
267 /* Mark the last entry as wrapping the ring. */
268 rx_ring
[i
-1].next
= virt_to_bus(&rx_ring
[0]);
271 *The Tx buffer descriptor is filled in as needed,
272 * but we do need to clear the ownership bit.
276 for (i
= 0; i
< TX_RING_SIZE
; i
++) {
277 tx_ring
[i
].status
= 0; /* Owned by CPU */
278 tx_ring
[i
].bufaddr
= virt_to_bus(p
+ (PKT_BUF_SZ
* i
));
279 tx_ring
[i
].control
= TD_STDFLAGS
;
280 tx_ring
[i
].next
= virt_to_bus(&(tx_ring
[i
+ 1]) );
282 tx_ring
[i
-1].next
= virt_to_bus(&tx_ring
[0]);
285 /* function: epic100_transmit
286 * This transmits a packet.
288 * Arguments: char d[6]: destination ethernet address.
289 * unsigned short t: ethernet protocol type.
290 * unsigned short s: size of the data-part of the packet.
291 * char *p: the data for the packet.
295 epic100_transmit(struct nic
*nic
, const char *destaddr
, unsigned int type
,
296 unsigned int len
, const char *data
)
298 unsigned short nstype
;
302 /* Calculate the next Tx descriptor entry. */
303 entry
= cur_tx
% TX_RING_SIZE
;
305 if ((tx_ring
[entry
].status
& TRING_OWN
) == TRING_OWN
) {
306 printf("eth_transmit: Unable to transmit. status=%hX. Resetting...\n",
307 tx_ring
[entry
].status
);
313 txp
= (char*)tx_ring
[entry
].bufaddr
;
315 memcpy(txp
, destaddr
, ETH_ALEN
);
316 memcpy(txp
+ ETH_ALEN
, nic
->node_addr
, ETH_ALEN
);
317 nstype
= htons(type
);
318 memcpy(txp
+ 12, (char*)&nstype
, 2);
319 memcpy(txp
+ ETH_HLEN
, data
, len
);
324 * Caution: the write order is important here,
325 * set the base address with the "ownership"
328 tx_ring
[entry
].txlength
= (len
>= 60 ? len
: 60);
329 tx_ring
[entry
].buflength
= len
;
330 tx_ring
[entry
].status
= TRING_OWN
; /* Pass ownership to the chip. */
334 /* Trigger an immediate transmit demand. */
335 outl(CR_QUEUE_TX
, command
);
337 load_timer2(10*TICKS_PER_MS
); /* timeout 10 ms for transmit */
338 while ((tx_ring
[entry
].status
& TRING_OWN
) && timer2_running())
341 if ((tx_ring
[entry
].status
& TRING_OWN
) != 0)
342 printf("Oops, transmitter timeout, status=%hX\n",
343 tx_ring
[entry
].status
);
346 /* function: epic100_poll / eth_poll
347 * This receives a packet from the network.
351 * returns: 1 if a packet was received.
352 * 0 if no pacet was received.
354 * returns the packet in the array nic->packet.
355 * returns the length of the packet in nic->packetlen.
359 epic100_poll(struct nic
*nic
)
365 entry
= cur_rx
% RX_RING_SIZE
;
367 if ((status
= rx_ring
[entry
].status
& RRING_OWN
) == RRING_OWN
)
370 /* We own the next entry, it's a new packet. Send it up. */
373 printf("epic_poll: entry %d status %hX\n", entry
, status
);
377 if (status
& 0x2000) {
378 printf("epic_poll: Giant packet\n");
380 } else if (status
& 0x0006) {
381 /* Rx Frame errors are counted in hardware. */
382 printf("epic_poll: Frame received with errors\n");
385 /* Omit the four octet CRC from the length. */
386 nic
->packetlen
= rx_ring
[entry
].rxlength
- 4;
387 memcpy(nic
->packet
, (char*)rx_ring
[entry
].bufaddr
, nic
->packetlen
);
391 /* Clear all error sources. */
392 outl(status
& INTR_CLEARERRS
, intstat
);
394 /* Give the descriptor back to the chip */
395 rx_ring
[entry
].status
= RRING_OWN
;
397 /* Restart Receiver */
398 outl(CR_START_RX
| CR_QUEUE_RX
, command
);
405 epic100_disable(struct nic
*nic
)
411 /* Serial EEPROM section. */
413 /* EEPROM_Ctrl bits. */
414 #define EE_SHIFT_CLK 0x04 /* EEPROM shift clock. */
415 #define EE_CS 0x02 /* EEPROM chip select. */
416 #define EE_DATA_WRITE 0x08 /* EEPROM chip data in. */
417 #define EE_WRITE_0 0x01
418 #define EE_WRITE_1 0x09
419 #define EE_DATA_READ 0x10 /* EEPROM chip data out. */
420 #define EE_ENB (0x0001 | EE_CS)
422 /* The EEPROM commands include the alway-set leading bit. */
423 #define EE_WRITE_CMD (5 << 6)
424 #define EE_READ_CMD (6 << 6)
425 #define EE_ERASE_CMD (7 << 6)
427 #define eeprom_delay(n) delay(n)
430 read_eeprom(int location
)
434 int read_cmd
= location
| EE_READ_CMD
;
436 outl(EE_ENB
& ~EE_CS
, eectl
);
439 /* Shift the read command bits out. */
440 for (i
= 10; i
>= 0; i
--) {
441 short dataval
= (read_cmd
& (1 << i
)) ? EE_DATA_WRITE
: 0;
442 outl(EE_ENB
| dataval
, eectl
);
444 outl(EE_ENB
| dataval
| EE_SHIFT_CLK
, eectl
);
446 outl(EE_ENB
| dataval
, eectl
); /* Finish EEPROM a clock tick. */
451 for (i
= 16; i
> 0; i
--) {
452 outl(EE_ENB
| EE_SHIFT_CLK
, eectl
);
454 retval
= (retval
<< 1) | ((inl(eectl
) & EE_DATA_READ
) ? 1 : 0);
459 /* Terminate the EEPROM access. */
460 outl(EE_ENB
& ~EE_CS
, eectl
);
467 #define MII_WRITEOP 2
470 mii_read(int phy_id
, int location
)
474 outl((phy_id
<< 9) | (location
<< 4) | MII_READOP
, mmctl
);
475 /* Typical operation takes < 50 ticks. */
477 for (i
= 4000; i
> 0; i
--)
478 if ((inl(mmctl
) & MII_READOP
) == 0)