blob: 2f9fa9e3d331fcdc903164068262156de075bb2c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
3 * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
4 *
5 * This version of the driver is specific to the FADS implementation,
6 * since the board contains control registers external to the processor
7 * for the control of the LevelOne LXT970 transceiver. The MPC860T manual
8 * describes connections using the internal parallel port I/O, which
9 * is basically all of Port D.
10 *
11 * Includes support for the following PHYs: QS6612, LXT970, LXT971/2.
12 *
13 * Right now, I am very wasteful with the buffers. I allocate memory
14 * pages and then divide them into 2K frame buffers. This way I know I
15 * have buffers large enough to hold one frame within one buffer descriptor.
16 * Once I get this working, I will use 64 or 128 byte CPM buffers, which
17 * will be much more memory efficient and will easily handle lots of
18 * small packets.
19 *
20 * Much better multiple PHY support by Magnus Damm.
21 * Copyright (c) 2000 Ericsson Radio Systems AB.
22 *
23 * Make use of MII for PHY control configurable.
24 * Some fixes.
25 * Copyright (c) 2000-2002 Wolfgang Denk, DENX Software Engineering.
26 *
27 * Support for AMD AM79C874 added.
28 * Thomas Lange, thomas@corelatus.com
29 */
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/kernel.h>
32#include <linux/sched.h>
33#include <linux/string.h>
34#include <linux/ptrace.h>
35#include <linux/errno.h>
36#include <linux/ioport.h>
37#include <linux/slab.h>
38#include <linux/interrupt.h>
39#include <linux/pci.h>
40#include <linux/init.h>
41#include <linux/delay.h>
42#include <linux/netdevice.h>
43#include <linux/etherdevice.h>
44#include <linux/skbuff.h>
45#include <linux/spinlock.h>
46#include <linux/bitops.h>
47#ifdef CONFIG_FEC_PACKETHOOK
48#include <linux/pkthook.h>
49#endif
50
51#include <asm/8xx_immap.h>
52#include <asm/pgtable.h>
53#include <asm/mpc8xx.h>
54#include <asm/irq.h>
55#include <asm/uaccess.h>
56#include <asm/commproc.h>
57
58#ifdef CONFIG_USE_MDIO
59/* Forward declarations of some structures to support different PHYs
60*/
61
62typedef struct {
63 uint mii_data;
64 void (*funct)(uint mii_reg, struct net_device *dev);
65} phy_cmd_t;
66
67typedef struct {
68 uint id;
69 char *name;
70
71 const phy_cmd_t *config;
72 const phy_cmd_t *startup;
73 const phy_cmd_t *ack_int;
74 const phy_cmd_t *shutdown;
75} phy_info_t;
76#endif /* CONFIG_USE_MDIO */
77
78/* The number of Tx and Rx buffers. These are allocated from the page
79 * pool. The code may assume these are power of two, so it is best
80 * to keep them that size.
81 * We don't need to allocate pages for the transmitter. We just use
82 * the skbuffer directly.
83 */
84#ifdef CONFIG_ENET_BIG_BUFFERS
85#define FEC_ENET_RX_PAGES 16
86#define FEC_ENET_RX_FRSIZE 2048
87#define FEC_ENET_RX_FRPPG (PAGE_SIZE / FEC_ENET_RX_FRSIZE)
88#define RX_RING_SIZE (FEC_ENET_RX_FRPPG * FEC_ENET_RX_PAGES)
89#define TX_RING_SIZE 16 /* Must be power of two */
90#define TX_RING_MOD_MASK 15 /* for this to work */
91#else
92#define FEC_ENET_RX_PAGES 4
93#define FEC_ENET_RX_FRSIZE 2048
94#define FEC_ENET_RX_FRPPG (PAGE_SIZE / FEC_ENET_RX_FRSIZE)
95#define RX_RING_SIZE (FEC_ENET_RX_FRPPG * FEC_ENET_RX_PAGES)
96#define TX_RING_SIZE 8 /* Must be power of two */
97#define TX_RING_MOD_MASK 7 /* for this to work */
98#endif
99
100/* Interrupt events/masks.
101*/
102#define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
103#define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
104#define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
105#define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
106#define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
107#define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
108#define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
109#define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
110#define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
111#define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
112
113/*
114*/
115#define FEC_ECNTRL_PINMUX 0x00000004
116#define FEC_ECNTRL_ETHER_EN 0x00000002
117#define FEC_ECNTRL_RESET 0x00000001
118
119#define FEC_RCNTRL_BC_REJ 0x00000010
120#define FEC_RCNTRL_PROM 0x00000008
121#define FEC_RCNTRL_MII_MODE 0x00000004
122#define FEC_RCNTRL_DRT 0x00000002
123#define FEC_RCNTRL_LOOP 0x00000001
124
125#define FEC_TCNTRL_FDEN 0x00000004
126#define FEC_TCNTRL_HBC 0x00000002
127#define FEC_TCNTRL_GTS 0x00000001
128
129/* Delay to wait for FEC reset command to complete (in us)
130*/
131#define FEC_RESET_DELAY 50
132
133/* The FEC stores dest/src/type, data, and checksum for receive packets.
134 */
135#define PKT_MAXBUF_SIZE 1518
136#define PKT_MINBUF_SIZE 64
137#define PKT_MAXBLR_SIZE 1520
138
139/* The FEC buffer descriptors track the ring buffers. The rx_bd_base and
140 * tx_bd_base always point to the base of the buffer descriptors. The
141 * cur_rx and cur_tx point to the currently available buffer.
142 * The dirty_tx tracks the current buffer that is being sent by the
143 * controller. The cur_tx and dirty_tx are equal under both completely
144 * empty and completely full conditions. The empty/ready indicator in
145 * the buffer descriptor determines the actual condition.
146 */
147struct fec_enet_private {
148 /* The saved address of a sent-in-place packet/buffer, for skfree(). */
149 struct sk_buff* tx_skbuff[TX_RING_SIZE];
150 ushort skb_cur;
151 ushort skb_dirty;
152
153 /* CPM dual port RAM relative addresses.
154 */
155 cbd_t *rx_bd_base; /* Address of Rx and Tx buffers. */
156 cbd_t *tx_bd_base;
157 cbd_t *cur_rx, *cur_tx; /* The next free ring entry */
158 cbd_t *dirty_tx; /* The ring entries to be free()ed. */
159
160 /* Virtual addresses for the receive buffers because we can't
161 * do a __va() on them anymore.
162 */
163 unsigned char *rx_vaddr[RX_RING_SIZE];
164
165 struct net_device_stats stats;
166 uint tx_full;
167 spinlock_t lock;
168
169#ifdef CONFIG_USE_MDIO
170 uint phy_id;
171 uint phy_id_done;
172 uint phy_status;
173 uint phy_speed;
174 phy_info_t *phy;
Aristeu Sergio Rozanski Filho8b0ed2f2005-08-07 09:42:28 -0700175 struct work_struct phy_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 uint sequence_done;
178
179 uint phy_addr;
180#endif /* CONFIG_USE_MDIO */
181
182 int link;
183 int old_link;
184 int full_duplex;
185
186#ifdef CONFIG_FEC_PACKETHOOK
187 unsigned long ph_lock;
188 fec_ph_func *ph_rxhandler;
189 fec_ph_func *ph_txhandler;
190 __u16 ph_proto;
191 volatile __u32 *ph_regaddr;
192 void *ph_priv;
193#endif
194};
195
196static int fec_enet_open(struct net_device *dev);
197static int fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev);
198#ifdef CONFIG_USE_MDIO
199static void fec_enet_mii(struct net_device *dev);
200#endif /* CONFIG_USE_MDIO */
Al Viro39e3eb72006-10-09 12:48:42 +0100201static irqreturn_t fec_enet_interrupt(int irq, void * dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202#ifdef CONFIG_FEC_PACKETHOOK
203static void fec_enet_tx(struct net_device *dev, __u32 regval);
204static void fec_enet_rx(struct net_device *dev, __u32 regval);
205#else
206static void fec_enet_tx(struct net_device *dev);
207static void fec_enet_rx(struct net_device *dev);
208#endif
209static int fec_enet_close(struct net_device *dev);
210static struct net_device_stats *fec_enet_get_stats(struct net_device *dev);
211static void set_multicast_list(struct net_device *dev);
212static void fec_restart(struct net_device *dev, int duplex);
213static void fec_stop(struct net_device *dev);
214static ushort my_enet_addr[3];
215
216#ifdef CONFIG_USE_MDIO
217/* MII processing. We keep this as simple as possible. Requests are
218 * placed on the list (if there is room). When the request is finished
219 * by the MII, an optional function may be called.
220 */
221typedef struct mii_list {
222 uint mii_regval;
223 void (*mii_func)(uint val, struct net_device *dev);
224 struct mii_list *mii_next;
225} mii_list_t;
226
227#define NMII 20
228mii_list_t mii_cmds[NMII];
229mii_list_t *mii_free;
230mii_list_t *mii_head;
231mii_list_t *mii_tail;
232
233static int mii_queue(struct net_device *dev, int request,
234 void (*func)(uint, struct net_device *));
235
236/* Make MII read/write commands for the FEC.
237*/
238#define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18))
239#define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | \
240 (VAL & 0xffff))
241#define mk_mii_end 0
242#endif /* CONFIG_USE_MDIO */
243
244/* Transmitter timeout.
245*/
246#define TX_TIMEOUT (2*HZ)
247
248#ifdef CONFIG_USE_MDIO
249/* Register definitions for the PHY.
250*/
251
252#define MII_REG_CR 0 /* Control Register */
253#define MII_REG_SR 1 /* Status Register */
254#define MII_REG_PHYIR1 2 /* PHY Identification Register 1 */
255#define MII_REG_PHYIR2 3 /* PHY Identification Register 2 */
256#define MII_REG_ANAR 4 /* A-N Advertisement Register */
257#define MII_REG_ANLPAR 5 /* A-N Link Partner Ability Register */
258#define MII_REG_ANER 6 /* A-N Expansion Register */
259#define MII_REG_ANNPTR 7 /* A-N Next Page Transmit Register */
260#define MII_REG_ANLPRNPR 8 /* A-N Link Partner Received Next Page Reg. */
261
262/* values for phy_status */
263
264#define PHY_CONF_ANE 0x0001 /* 1 auto-negotiation enabled */
265#define PHY_CONF_LOOP 0x0002 /* 1 loopback mode enabled */
266#define PHY_CONF_SPMASK 0x00f0 /* mask for speed */
267#define PHY_CONF_10HDX 0x0010 /* 10 Mbit half duplex supported */
268#define PHY_CONF_10FDX 0x0020 /* 10 Mbit full duplex supported */
269#define PHY_CONF_100HDX 0x0040 /* 100 Mbit half duplex supported */
270#define PHY_CONF_100FDX 0x0080 /* 100 Mbit full duplex supported */
271
272#define PHY_STAT_LINK 0x0100 /* 1 up - 0 down */
273#define PHY_STAT_FAULT 0x0200 /* 1 remote fault */
274#define PHY_STAT_ANC 0x0400 /* 1 auto-negotiation complete */
275#define PHY_STAT_SPMASK 0xf000 /* mask for speed */
276#define PHY_STAT_10HDX 0x1000 /* 10 Mbit half duplex selected */
277#define PHY_STAT_10FDX 0x2000 /* 10 Mbit full duplex selected */
278#define PHY_STAT_100HDX 0x4000 /* 100 Mbit half duplex selected */
279#define PHY_STAT_100FDX 0x8000 /* 100 Mbit full duplex selected */
280#endif /* CONFIG_USE_MDIO */
281
282#ifdef CONFIG_FEC_PACKETHOOK
283int
284fec_register_ph(struct net_device *dev, fec_ph_func *rxfun, fec_ph_func *txfun,
285 __u16 proto, volatile __u32 *regaddr, void *priv)
286{
287 struct fec_enet_private *fep;
288 int retval = 0;
289
290 fep = dev->priv;
291
292 if (test_and_set_bit(0, (void*)&fep->ph_lock) != 0) {
293 /* Someone is messing with the packet hook */
294 return -EAGAIN;
295 }
296 if (fep->ph_rxhandler != NULL || fep->ph_txhandler != NULL) {
297 retval = -EBUSY;
298 goto out;
299 }
300 fep->ph_rxhandler = rxfun;
301 fep->ph_txhandler = txfun;
302 fep->ph_proto = proto;
303 fep->ph_regaddr = regaddr;
304 fep->ph_priv = priv;
305
306 out:
307 fep->ph_lock = 0;
308
309 return retval;
310}
311
312
313int
314fec_unregister_ph(struct net_device *dev)
315{
316 struct fec_enet_private *fep;
317 int retval = 0;
318
319 fep = dev->priv;
320
321 if (test_and_set_bit(0, (void*)&fep->ph_lock) != 0) {
322 /* Someone is messing with the packet hook */
323 return -EAGAIN;
324 }
325
326 fep->ph_rxhandler = fep->ph_txhandler = NULL;
327 fep->ph_proto = 0;
328 fep->ph_regaddr = NULL;
329 fep->ph_priv = NULL;
330
331 fep->ph_lock = 0;
332
333 return retval;
334}
335
336EXPORT_SYMBOL(fec_register_ph);
337EXPORT_SYMBOL(fec_unregister_ph);
338
339#endif /* CONFIG_FEC_PACKETHOOK */
340
341static int
342fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
343{
344 struct fec_enet_private *fep;
345 volatile fec_t *fecp;
346 volatile cbd_t *bdp;
347
348 fep = dev->priv;
349 fecp = (volatile fec_t*)dev->base_addr;
350
351 if (!fep->link) {
352 /* Link is down or autonegotiation is in progress. */
353 return 1;
354 }
355
356 /* Fill in a Tx ring entry */
357 bdp = fep->cur_tx;
358
359#ifndef final_version
360 if (bdp->cbd_sc & BD_ENET_TX_READY) {
361 /* Ooops. All transmit buffers are full. Bail out.
362 * This should not happen, since dev->tbusy should be set.
363 */
364 printk("%s: tx queue full!.\n", dev->name);
365 return 1;
366 }
367#endif
368
369 /* Clear all of the status flags.
370 */
371 bdp->cbd_sc &= ~BD_ENET_TX_STATS;
372
373 /* Set buffer length and buffer pointer.
374 */
375 bdp->cbd_bufaddr = __pa(skb->data);
376 bdp->cbd_datlen = skb->len;
377
378 /* Save skb pointer.
379 */
380 fep->tx_skbuff[fep->skb_cur] = skb;
381
382 fep->stats.tx_bytes += skb->len;
383 fep->skb_cur = (fep->skb_cur+1) & TX_RING_MOD_MASK;
384
385 /* Push the data cache so the CPM does not get stale memory
386 * data.
387 */
388 flush_dcache_range((unsigned long)skb->data,
389 (unsigned long)skb->data + skb->len);
390
391 /* disable interrupts while triggering transmit */
392 spin_lock_irq(&fep->lock);
393
394 /* Send it on its way. Tell FEC its ready, interrupt when done,
395 * its the last BD of the frame, and to put the CRC on the end.
396 */
397
398 bdp->cbd_sc |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
399 | BD_ENET_TX_LAST | BD_ENET_TX_TC);
400
401 dev->trans_start = jiffies;
402
403 /* Trigger transmission start */
404 fecp->fec_x_des_active = 0x01000000;
405
406 /* If this was the last BD in the ring, start at the beginning again.
407 */
408 if (bdp->cbd_sc & BD_ENET_TX_WRAP) {
409 bdp = fep->tx_bd_base;
410 } else {
411 bdp++;
412 }
413
414 if (bdp->cbd_sc & BD_ENET_TX_READY) {
415 netif_stop_queue(dev);
416 fep->tx_full = 1;
417 }
418
419 fep->cur_tx = (cbd_t *)bdp;
420
421 spin_unlock_irq(&fep->lock);
422
423 return 0;
424}
425
426static void
427fec_timeout(struct net_device *dev)
428{
429 struct fec_enet_private *fep = dev->priv;
430
431 printk("%s: transmit timed out.\n", dev->name);
432 fep->stats.tx_errors++;
433#ifndef final_version
434 {
435 int i;
436 cbd_t *bdp;
437
438 printk("Ring data dump: cur_tx %lx%s, dirty_tx %lx cur_rx: %lx\n",
439 (unsigned long)fep->cur_tx, fep->tx_full ? " (full)" : "",
440 (unsigned long)fep->dirty_tx,
441 (unsigned long)fep->cur_rx);
442
443 bdp = fep->tx_bd_base;
444 printk(" tx: %u buffers\n", TX_RING_SIZE);
445 for (i = 0 ; i < TX_RING_SIZE; i++) {
446 printk(" %08x: %04x %04x %08x\n",
447 (uint) bdp,
448 bdp->cbd_sc,
449 bdp->cbd_datlen,
450 bdp->cbd_bufaddr);
451 bdp++;
452 }
453
454 bdp = fep->rx_bd_base;
455 printk(" rx: %lu buffers\n", RX_RING_SIZE);
456 for (i = 0 ; i < RX_RING_SIZE; i++) {
457 printk(" %08x: %04x %04x %08x\n",
458 (uint) bdp,
459 bdp->cbd_sc,
460 bdp->cbd_datlen,
461 bdp->cbd_bufaddr);
462 bdp++;
463 }
464 }
465#endif
466 if (!fep->tx_full)
467 netif_wake_queue(dev);
468}
469
470/* The interrupt handler.
471 * This is called from the MPC core interrupt.
472 */
Aristeu Sergio Rozanski Filhofbccb3d2005-08-07 09:42:36 -0700473static irqreturn_t
Al Viro39e3eb72006-10-09 12:48:42 +0100474fec_enet_interrupt(int irq, void * dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 struct net_device *dev = dev_id;
477 volatile fec_t *fecp;
478 uint int_events;
479#ifdef CONFIG_FEC_PACKETHOOK
480 struct fec_enet_private *fep = dev->priv;
481 __u32 regval;
482
483 if (fep->ph_regaddr) regval = *fep->ph_regaddr;
484#endif
485 fecp = (volatile fec_t*)dev->base_addr;
486
487 /* Get the interrupt events that caused us to be here.
488 */
489 while ((int_events = fecp->fec_ievent) != 0) {
490 fecp->fec_ievent = int_events;
491 if ((int_events & (FEC_ENET_HBERR | FEC_ENET_BABR |
492 FEC_ENET_BABT | FEC_ENET_EBERR)) != 0) {
493 printk("FEC ERROR %x\n", int_events);
494 }
495
496 /* Handle receive event in its own function.
497 */
498 if (int_events & FEC_ENET_RXF) {
499#ifdef CONFIG_FEC_PACKETHOOK
500 fec_enet_rx(dev, regval);
501#else
502 fec_enet_rx(dev);
503#endif
504 }
505
506 /* Transmit OK, or non-fatal error. Update the buffer
507 descriptors. FEC handles all errors, we just discover
508 them as part of the transmit process.
509 */
510 if (int_events & FEC_ENET_TXF) {
511#ifdef CONFIG_FEC_PACKETHOOK
512 fec_enet_tx(dev, regval);
513#else
514 fec_enet_tx(dev);
515#endif
516 }
517
518 if (int_events & FEC_ENET_MII) {
519#ifdef CONFIG_USE_MDIO
520 fec_enet_mii(dev);
521#else
522printk("%s[%d] %s: unexpected FEC_ENET_MII event\n", __FILE__,__LINE__,__FUNCTION__);
523#endif /* CONFIG_USE_MDIO */
524 }
525
526 }
Aristeu Sergio Rozanski Filhofbccb3d2005-08-07 09:42:36 -0700527 return IRQ_RETVAL(IRQ_HANDLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
530
531static void
532#ifdef CONFIG_FEC_PACKETHOOK
533fec_enet_tx(struct net_device *dev, __u32 regval)
534#else
535fec_enet_tx(struct net_device *dev)
536#endif
537{
538 struct fec_enet_private *fep;
539 volatile cbd_t *bdp;
540 struct sk_buff *skb;
541
542 fep = dev->priv;
543 /* lock while transmitting */
544 spin_lock(&fep->lock);
545 bdp = fep->dirty_tx;
546
547 while ((bdp->cbd_sc&BD_ENET_TX_READY) == 0) {
548 if (bdp == fep->cur_tx && fep->tx_full == 0) break;
549
550 skb = fep->tx_skbuff[fep->skb_dirty];
551 /* Check for errors. */
552 if (bdp->cbd_sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
553 BD_ENET_TX_RL | BD_ENET_TX_UN |
554 BD_ENET_TX_CSL)) {
555 fep->stats.tx_errors++;
556 if (bdp->cbd_sc & BD_ENET_TX_HB) /* No heartbeat */
557 fep->stats.tx_heartbeat_errors++;
558 if (bdp->cbd_sc & BD_ENET_TX_LC) /* Late collision */
559 fep->stats.tx_window_errors++;
560 if (bdp->cbd_sc & BD_ENET_TX_RL) /* Retrans limit */
561 fep->stats.tx_aborted_errors++;
562 if (bdp->cbd_sc & BD_ENET_TX_UN) /* Underrun */
563 fep->stats.tx_fifo_errors++;
564 if (bdp->cbd_sc & BD_ENET_TX_CSL) /* Carrier lost */
565 fep->stats.tx_carrier_errors++;
566 } else {
567#ifdef CONFIG_FEC_PACKETHOOK
568 /* Packet hook ... */
569 if (fep->ph_txhandler &&
570 ((struct ethhdr *)skb->data)->h_proto
571 == fep->ph_proto) {
572 fep->ph_txhandler((__u8*)skb->data, skb->len,
573 regval, fep->ph_priv);
574 }
575#endif
576 fep->stats.tx_packets++;
577 }
578
579#ifndef final_version
580 if (bdp->cbd_sc & BD_ENET_TX_READY)
581 printk("HEY! Enet xmit interrupt and TX_READY.\n");
582#endif
583 /* Deferred means some collisions occurred during transmit,
584 * but we eventually sent the packet OK.
585 */
586 if (bdp->cbd_sc & BD_ENET_TX_DEF)
587 fep->stats.collisions++;
588
589 /* Free the sk buffer associated with this last transmit.
590 */
591#if 0
592printk("TXI: %x %x %x\n", bdp, skb, fep->skb_dirty);
593#endif
594 dev_kfree_skb_irq (skb/*, FREE_WRITE*/);
595 fep->tx_skbuff[fep->skb_dirty] = NULL;
596 fep->skb_dirty = (fep->skb_dirty + 1) & TX_RING_MOD_MASK;
597
598 /* Update pointer to next buffer descriptor to be transmitted.
599 */
600 if (bdp->cbd_sc & BD_ENET_TX_WRAP)
601 bdp = fep->tx_bd_base;
602 else
603 bdp++;
604
605 /* Since we have freed up a buffer, the ring is no longer
606 * full.
607 */
608 if (fep->tx_full) {
609 fep->tx_full = 0;
610 if (netif_queue_stopped(dev))
611 netif_wake_queue(dev);
612 }
613#ifdef CONFIG_FEC_PACKETHOOK
614 /* Re-read register. Not exactly guaranteed to be correct,
615 but... */
616 if (fep->ph_regaddr) regval = *fep->ph_regaddr;
617#endif
618 }
619 fep->dirty_tx = (cbd_t *)bdp;
620 spin_unlock(&fep->lock);
621}
622
623
624/* During a receive, the cur_rx points to the current incoming buffer.
625 * When we update through the ring, if the next incoming buffer has
626 * not been given to the system, we just set the empty indicator,
627 * effectively tossing the packet.
628 */
629static void
630#ifdef CONFIG_FEC_PACKETHOOK
631fec_enet_rx(struct net_device *dev, __u32 regval)
632#else
633fec_enet_rx(struct net_device *dev)
634#endif
635{
636 struct fec_enet_private *fep;
637 volatile fec_t *fecp;
638 volatile cbd_t *bdp;
639 struct sk_buff *skb;
640 ushort pkt_len;
641 __u8 *data;
642
643 fep = dev->priv;
644 fecp = (volatile fec_t*)dev->base_addr;
645
646 /* First, grab all of the stats for the incoming packet.
647 * These get messed up if we get called due to a busy condition.
648 */
649 bdp = fep->cur_rx;
650
651while (!(bdp->cbd_sc & BD_ENET_RX_EMPTY)) {
652
653#ifndef final_version
654 /* Since we have allocated space to hold a complete frame,
655 * the last indicator should be set.
656 */
657 if ((bdp->cbd_sc & BD_ENET_RX_LAST) == 0)
658 printk("FEC ENET: rcv is not +last\n");
659#endif
660
661 /* Check for errors. */
662 if (bdp->cbd_sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
663 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
664 fep->stats.rx_errors++;
665 if (bdp->cbd_sc & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
666 /* Frame too long or too short. */
667 fep->stats.rx_length_errors++;
668 }
669 if (bdp->cbd_sc & BD_ENET_RX_NO) /* Frame alignment */
670 fep->stats.rx_frame_errors++;
671 if (bdp->cbd_sc & BD_ENET_RX_CR) /* CRC Error */
672 fep->stats.rx_crc_errors++;
673 if (bdp->cbd_sc & BD_ENET_RX_OV) /* FIFO overrun */
674 fep->stats.rx_crc_errors++;
675 }
676
677 /* Report late collisions as a frame error.
678 * On this error, the BD is closed, but we don't know what we
679 * have in the buffer. So, just drop this frame on the floor.
680 */
681 if (bdp->cbd_sc & BD_ENET_RX_CL) {
682 fep->stats.rx_errors++;
683 fep->stats.rx_frame_errors++;
684 goto rx_processing_done;
685 }
686
687 /* Process the incoming frame.
688 */
689 fep->stats.rx_packets++;
690 pkt_len = bdp->cbd_datlen;
691 fep->stats.rx_bytes += pkt_len;
692 data = fep->rx_vaddr[bdp - fep->rx_bd_base];
693
694#ifdef CONFIG_FEC_PACKETHOOK
695 /* Packet hook ... */
696 if (fep->ph_rxhandler) {
697 if (((struct ethhdr *)data)->h_proto == fep->ph_proto) {
698 switch (fep->ph_rxhandler(data, pkt_len, regval,
699 fep->ph_priv)) {
700 case 1:
701 goto rx_processing_done;
702 break;
703 case 0:
704 break;
705 default:
706 fep->stats.rx_errors++;
707 goto rx_processing_done;
708 }
709 }
710 }
711
712 /* If it wasn't filtered - copy it to an sk buffer. */
713#endif
714
715 /* This does 16 byte alignment, exactly what we need.
716 * The packet length includes FCS, but we don't want to
717 * include that when passing upstream as it messes up
718 * bridging applications.
719 */
720 skb = dev_alloc_skb(pkt_len-4);
721
722 if (skb == NULL) {
723 printk("%s: Memory squeeze, dropping packet.\n", dev->name);
724 fep->stats.rx_dropped++;
725 } else {
726 skb->dev = dev;
727 skb_put(skb,pkt_len-4); /* Make room */
728 eth_copy_and_sum(skb, data, pkt_len-4, 0);
729 skb->protocol=eth_type_trans(skb,dev);
730 netif_rx(skb);
731 }
732 rx_processing_done:
733
734 /* Clear the status flags for this buffer.
735 */
736 bdp->cbd_sc &= ~BD_ENET_RX_STATS;
737
738 /* Mark the buffer empty.
739 */
740 bdp->cbd_sc |= BD_ENET_RX_EMPTY;
741
742 /* Update BD pointer to next entry.
743 */
744 if (bdp->cbd_sc & BD_ENET_RX_WRAP)
745 bdp = fep->rx_bd_base;
746 else
747 bdp++;
748
749#if 1
750 /* Doing this here will keep the FEC running while we process
751 * incoming frames. On a heavily loaded network, we should be
752 * able to keep up at the expense of system resources.
753 */
754 fecp->fec_r_des_active = 0x01000000;
755#endif
756#ifdef CONFIG_FEC_PACKETHOOK
757 /* Re-read register. Not exactly guaranteed to be correct,
758 but... */
759 if (fep->ph_regaddr) regval = *fep->ph_regaddr;
760#endif
761 } /* while (!(bdp->cbd_sc & BD_ENET_RX_EMPTY)) */
762 fep->cur_rx = (cbd_t *)bdp;
763
764#if 0
765 /* Doing this here will allow us to process all frames in the
766 * ring before the FEC is allowed to put more there. On a heavily
767 * loaded network, some frames may be lost. Unfortunately, this
768 * increases the interrupt overhead since we can potentially work
769 * our way back to the interrupt return only to come right back
770 * here.
771 */
772 fecp->fec_r_des_active = 0x01000000;
773#endif
774}
775
776
777#ifdef CONFIG_USE_MDIO
778static void
779fec_enet_mii(struct net_device *dev)
780{
781 struct fec_enet_private *fep;
782 volatile fec_t *ep;
783 mii_list_t *mip;
784 uint mii_reg;
785
786 fep = (struct fec_enet_private *)dev->priv;
787 ep = &(((immap_t *)IMAP_ADDR)->im_cpm.cp_fec);
788 mii_reg = ep->fec_mii_data;
789
790 if ((mip = mii_head) == NULL) {
791 printk("MII and no head!\n");
792 return;
793 }
794
795 if (mip->mii_func != NULL)
796 (*(mip->mii_func))(mii_reg, dev);
797
798 mii_head = mip->mii_next;
799 mip->mii_next = mii_free;
800 mii_free = mip;
801
802 if ((mip = mii_head) != NULL) {
803 ep->fec_mii_data = mip->mii_regval;
804
805 }
806}
807
808static int
809mii_queue(struct net_device *dev, int regval, void (*func)(uint, struct net_device *))
810{
811 struct fec_enet_private *fep;
812 unsigned long flags;
813 mii_list_t *mip;
814 int retval;
815
816 /* Add PHY address to register command.
817 */
818 fep = dev->priv;
819 regval |= fep->phy_addr << 23;
820
821 retval = 0;
822
823 /* lock while modifying mii_list */
824 spin_lock_irqsave(&fep->lock, flags);
825
826 if ((mip = mii_free) != NULL) {
827 mii_free = mip->mii_next;
828 mip->mii_regval = regval;
829 mip->mii_func = func;
830 mip->mii_next = NULL;
831 if (mii_head) {
832 mii_tail->mii_next = mip;
833 mii_tail = mip;
834 } else {
835 mii_head = mii_tail = mip;
836 (&(((immap_t *)IMAP_ADDR)->im_cpm.cp_fec))->fec_mii_data = regval;
837 }
838 } else {
839 retval = 1;
840 }
841
842 spin_unlock_irqrestore(&fep->lock, flags);
843
844 return(retval);
845}
846
847static void mii_do_cmd(struct net_device *dev, const phy_cmd_t *c)
848{
849 int k;
850
851 if(!c)
852 return;
853
854 for(k = 0; (c+k)->mii_data != mk_mii_end; k++)
855 mii_queue(dev, (c+k)->mii_data, (c+k)->funct);
856}
857
858static void mii_parse_sr(uint mii_reg, struct net_device *dev)
859{
860 struct fec_enet_private *fep = dev->priv;
861 volatile uint *s = &(fep->phy_status);
862
863 *s &= ~(PHY_STAT_LINK | PHY_STAT_FAULT | PHY_STAT_ANC);
864
865 if (mii_reg & 0x0004)
866 *s |= PHY_STAT_LINK;
867 if (mii_reg & 0x0010)
868 *s |= PHY_STAT_FAULT;
869 if (mii_reg & 0x0020)
870 *s |= PHY_STAT_ANC;
871
872 fep->link = (*s & PHY_STAT_LINK) ? 1 : 0;
873}
874
875static void mii_parse_cr(uint mii_reg, struct net_device *dev)
876{
877 struct fec_enet_private *fep = dev->priv;
878 volatile uint *s = &(fep->phy_status);
879
880 *s &= ~(PHY_CONF_ANE | PHY_CONF_LOOP);
881
882 if (mii_reg & 0x1000)
883 *s |= PHY_CONF_ANE;
884 if (mii_reg & 0x4000)
885 *s |= PHY_CONF_LOOP;
886}
887
888static void mii_parse_anar(uint mii_reg, struct net_device *dev)
889{
890 struct fec_enet_private *fep = dev->priv;
891 volatile uint *s = &(fep->phy_status);
892
893 *s &= ~(PHY_CONF_SPMASK);
894
895 if (mii_reg & 0x0020)
896 *s |= PHY_CONF_10HDX;
897 if (mii_reg & 0x0040)
898 *s |= PHY_CONF_10FDX;
899 if (mii_reg & 0x0080)
900 *s |= PHY_CONF_100HDX;
901 if (mii_reg & 0x00100)
902 *s |= PHY_CONF_100FDX;
903}
904#if 0
905static void mii_disp_reg(uint mii_reg, struct net_device *dev)
906{
907 printk("reg %u = 0x%04x\n", (mii_reg >> 18) & 0x1f, mii_reg & 0xffff);
908}
909#endif
910
911/* ------------------------------------------------------------------------- */
912/* The Level one LXT970 is used by many boards */
913
914#ifdef CONFIG_FEC_LXT970
915
916#define MII_LXT970_MIRROR 16 /* Mirror register */
917#define MII_LXT970_IER 17 /* Interrupt Enable Register */
918#define MII_LXT970_ISR 18 /* Interrupt Status Register */
919#define MII_LXT970_CONFIG 19 /* Configuration Register */
920#define MII_LXT970_CSR 20 /* Chip Status Register */
921
922static void mii_parse_lxt970_csr(uint mii_reg, struct net_device *dev)
923{
924 struct fec_enet_private *fep = dev->priv;
925 volatile uint *s = &(fep->phy_status);
926
927 *s &= ~(PHY_STAT_SPMASK);
928
929 if (mii_reg & 0x0800) {
930 if (mii_reg & 0x1000)
931 *s |= PHY_STAT_100FDX;
932 else
933 *s |= PHY_STAT_100HDX;
934 }
935 else {
936 if (mii_reg & 0x1000)
937 *s |= PHY_STAT_10FDX;
938 else
939 *s |= PHY_STAT_10HDX;
940 }
941}
942
943static phy_info_t phy_info_lxt970 = {
944 0x07810000,
945 "LXT970",
946
947 (const phy_cmd_t []) { /* config */
948#if 0
949// { mk_mii_write(MII_REG_ANAR, 0x0021), NULL },
950
951 /* Set default operation of 100-TX....for some reason
952 * some of these bits are set on power up, which is wrong.
953 */
954 { mk_mii_write(MII_LXT970_CONFIG, 0), NULL },
955#endif
956 { mk_mii_read(MII_REG_CR), mii_parse_cr },
957 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
958 { mk_mii_end, }
959 },
960 (const phy_cmd_t []) { /* startup - enable interrupts */
961 { mk_mii_write(MII_LXT970_IER, 0x0002), NULL },
962 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
963 { mk_mii_end, }
964 },
965 (const phy_cmd_t []) { /* ack_int */
966 /* read SR and ISR to acknowledge */
967
968 { mk_mii_read(MII_REG_SR), mii_parse_sr },
969 { mk_mii_read(MII_LXT970_ISR), NULL },
970
971 /* find out the current status */
972
973 { mk_mii_read(MII_LXT970_CSR), mii_parse_lxt970_csr },
974 { mk_mii_end, }
975 },
976 (const phy_cmd_t []) { /* shutdown - disable interrupts */
977 { mk_mii_write(MII_LXT970_IER, 0x0000), NULL },
978 { mk_mii_end, }
979 },
980};
981
982#endif /* CONFIG_FEC_LXT970 */
983
984/* ------------------------------------------------------------------------- */
985/* The Level one LXT971 is used on some of my custom boards */
986
987#ifdef CONFIG_FEC_LXT971
988
989/* register definitions for the 971 */
990
991#define MII_LXT971_PCR 16 /* Port Control Register */
992#define MII_LXT971_SR2 17 /* Status Register 2 */
993#define MII_LXT971_IER 18 /* Interrupt Enable Register */
994#define MII_LXT971_ISR 19 /* Interrupt Status Register */
995#define MII_LXT971_LCR 20 /* LED Control Register */
996#define MII_LXT971_TCR 30 /* Transmit Control Register */
997
998/*
999 * I had some nice ideas of running the MDIO faster...
1000 * The 971 should support 8MHz and I tried it, but things acted really
1001 * weird, so 2.5 MHz ought to be enough for anyone...
1002 */
1003
1004static void mii_parse_lxt971_sr2(uint mii_reg, struct net_device *dev)
1005{
1006 struct fec_enet_private *fep = dev->priv;
1007 volatile uint *s = &(fep->phy_status);
1008
1009 *s &= ~(PHY_STAT_SPMASK);
1010
1011 if (mii_reg & 0x4000) {
1012 if (mii_reg & 0x0200)
1013 *s |= PHY_STAT_100FDX;
1014 else
1015 *s |= PHY_STAT_100HDX;
1016 }
1017 else {
1018 if (mii_reg & 0x0200)
1019 *s |= PHY_STAT_10FDX;
1020 else
1021 *s |= PHY_STAT_10HDX;
1022 }
1023 if (mii_reg & 0x0008)
1024 *s |= PHY_STAT_FAULT;
1025}
1026
1027static phy_info_t phy_info_lxt971 = {
1028 0x0001378e,
1029 "LXT971",
1030
1031 (const phy_cmd_t []) { /* config */
1032// { mk_mii_write(MII_REG_ANAR, 0x021), NULL }, /* 10 Mbps, HD */
1033 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1034 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1035 { mk_mii_end, }
1036 },
1037 (const phy_cmd_t []) { /* startup - enable interrupts */
1038 { mk_mii_write(MII_LXT971_IER, 0x00f2), NULL },
1039 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
1040
1041 /* Somehow does the 971 tell me that the link is down
1042 * the first read after power-up.
1043 * read here to get a valid value in ack_int */
1044
1045 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1046 { mk_mii_end, }
1047 },
1048 (const phy_cmd_t []) { /* ack_int */
1049 /* find out the current status */
1050
1051 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1052 { mk_mii_read(MII_LXT971_SR2), mii_parse_lxt971_sr2 },
1053
1054 /* we only need to read ISR to acknowledge */
1055
1056 { mk_mii_read(MII_LXT971_ISR), NULL },
1057 { mk_mii_end, }
1058 },
1059 (const phy_cmd_t []) { /* shutdown - disable interrupts */
1060 { mk_mii_write(MII_LXT971_IER, 0x0000), NULL },
1061 { mk_mii_end, }
1062 },
1063};
1064
1065#endif /* CONFIG_FEC_LXT970 */
1066
1067
1068/* ------------------------------------------------------------------------- */
1069/* The Quality Semiconductor QS6612 is used on the RPX CLLF */
1070
1071#ifdef CONFIG_FEC_QS6612
1072
1073/* register definitions */
1074
1075#define MII_QS6612_MCR 17 /* Mode Control Register */
1076#define MII_QS6612_FTR 27 /* Factory Test Register */
1077#define MII_QS6612_MCO 28 /* Misc. Control Register */
1078#define MII_QS6612_ISR 29 /* Interrupt Source Register */
1079#define MII_QS6612_IMR 30 /* Interrupt Mask Register */
1080#define MII_QS6612_PCR 31 /* 100BaseTx PHY Control Reg. */
1081
1082static void mii_parse_qs6612_pcr(uint mii_reg, struct net_device *dev)
1083{
1084 struct fec_enet_private *fep = dev->priv;
1085 volatile uint *s = &(fep->phy_status);
1086
1087 *s &= ~(PHY_STAT_SPMASK);
1088
1089 switch((mii_reg >> 2) & 7) {
1090 case 1: *s |= PHY_STAT_10HDX; break;
1091 case 2: *s |= PHY_STAT_100HDX; break;
1092 case 5: *s |= PHY_STAT_10FDX; break;
1093 case 6: *s |= PHY_STAT_100FDX; break;
1094 }
1095}
1096
1097static phy_info_t phy_info_qs6612 = {
1098 0x00181440,
1099 "QS6612",
1100
1101 (const phy_cmd_t []) { /* config */
1102// { mk_mii_write(MII_REG_ANAR, 0x061), NULL }, /* 10 Mbps */
1103
1104 /* The PHY powers up isolated on the RPX,
1105 * so send a command to allow operation.
1106 */
1107
1108 { mk_mii_write(MII_QS6612_PCR, 0x0dc0), NULL },
1109
1110 /* parse cr and anar to get some info */
1111
1112 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1113 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1114 { mk_mii_end, }
1115 },
1116 (const phy_cmd_t []) { /* startup - enable interrupts */
1117 { mk_mii_write(MII_QS6612_IMR, 0x003a), NULL },
1118 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
1119 { mk_mii_end, }
1120 },
1121 (const phy_cmd_t []) { /* ack_int */
1122
1123 /* we need to read ISR, SR and ANER to acknowledge */
1124
1125 { mk_mii_read(MII_QS6612_ISR), NULL },
1126 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1127 { mk_mii_read(MII_REG_ANER), NULL },
1128
1129 /* read pcr to get info */
1130
1131 { mk_mii_read(MII_QS6612_PCR), mii_parse_qs6612_pcr },
1132 { mk_mii_end, }
1133 },
1134 (const phy_cmd_t []) { /* shutdown - disable interrupts */
1135 { mk_mii_write(MII_QS6612_IMR, 0x0000), NULL },
1136 { mk_mii_end, }
1137 },
1138};
1139
1140#endif /* CONFIG_FEC_QS6612 */
1141
1142/* ------------------------------------------------------------------------- */
1143/* The Advanced Micro Devices AM79C874 is used on the ICU862 */
1144
1145#ifdef CONFIG_FEC_AM79C874
1146
1147/* register definitions for the 79C874 */
1148
1149#define MII_AM79C874_MFR 16 /* Miscellaneous Features Register */
1150#define MII_AM79C874_ICSR 17 /* Interrupt Control/Status Register */
1151#define MII_AM79C874_DR 18 /* Diagnostic Register */
1152#define MII_AM79C874_PMLR 19 /* Power Management & Loopback Register */
1153#define MII_AM79C874_MCR 21 /* Mode Control Register */
1154#define MII_AM79C874_DC 23 /* Disconnect Counter */
1155#define MII_AM79C874_REC 24 /* Receiver Error Counter */
1156
1157static void mii_parse_amd79c874_dr(uint mii_reg, struct net_device *dev, uint data)
1158{
1159 volatile struct fec_enet_private *fep = dev->priv;
1160 uint s = fep->phy_status;
1161
1162 s &= ~(PHY_STAT_SPMASK);
1163
1164 /* Register 18: Bit 10 is data rate, 11 is Duplex */
1165 switch ((mii_reg >> 10) & 3) {
1166 case 0: s |= PHY_STAT_10HDX; break;
1167 case 1: s |= PHY_STAT_100HDX; break;
1168 case 2: s |= PHY_STAT_10FDX; break;
1169 case 3: s |= PHY_STAT_100FDX; break;
1170 }
1171
1172 fep->phy_status = s;
1173}
1174
1175static phy_info_t phy_info_amd79c874 = {
1176 0x00022561,
1177 "AM79C874",
1178
1179 (const phy_cmd_t []) { /* config */
1180// { mk_mii_write(MII_REG_ANAR, 0x021), NULL }, /* 10 Mbps, HD */
1181 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1182 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1183 { mk_mii_end, }
1184 },
1185 (const phy_cmd_t []) { /* startup - enable interrupts */
1186 { mk_mii_write(MII_AM79C874_ICSR, 0xff00), NULL },
1187 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
1188 { mk_mii_end, }
1189 },
1190 (const phy_cmd_t []) { /* ack_int */
1191 /* find out the current status */
1192
1193 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1194 { mk_mii_read(MII_AM79C874_DR), mii_parse_amd79c874_dr },
1195
1196 /* we only need to read ICSR to acknowledge */
1197
1198 { mk_mii_read(MII_AM79C874_ICSR), NULL },
1199 { mk_mii_end, }
1200 },
1201 (const phy_cmd_t []) { /* shutdown - disable interrupts */
1202 { mk_mii_write(MII_AM79C874_ICSR, 0x0000), NULL },
1203 { mk_mii_end, }
1204 },
1205};
1206
1207#endif /* CONFIG_FEC_AM79C874 */
1208
1209static phy_info_t *phy_info[] = {
1210
1211#ifdef CONFIG_FEC_LXT970
1212 &phy_info_lxt970,
1213#endif /* CONFIG_FEC_LXT970 */
1214
1215#ifdef CONFIG_FEC_LXT971
1216 &phy_info_lxt971,
1217#endif /* CONFIG_FEC_LXT971 */
1218
1219#ifdef CONFIG_FEC_QS6612
1220 &phy_info_qs6612,
1221#endif /* CONFIG_FEC_QS6612 */
1222
1223#ifdef CONFIG_FEC_AM79C874
1224 &phy_info_amd79c874,
1225#endif /* CONFIG_FEC_AM79C874 */
1226
1227 NULL
1228};
1229
1230static void mii_display_status(struct net_device *dev)
1231{
1232 struct fec_enet_private *fep = dev->priv;
1233 volatile uint *s = &(fep->phy_status);
1234
1235 if (!fep->link && !fep->old_link) {
1236 /* Link is still down - don't print anything */
1237 return;
1238 }
1239
1240 printk("%s: status: ", dev->name);
1241
1242 if (!fep->link) {
1243 printk("link down");
1244 } else {
1245 printk("link up");
1246
1247 switch(*s & PHY_STAT_SPMASK) {
1248 case PHY_STAT_100FDX: printk(", 100 Mbps Full Duplex"); break;
1249 case PHY_STAT_100HDX: printk(", 100 Mbps Half Duplex"); break;
1250 case PHY_STAT_10FDX: printk(", 10 Mbps Full Duplex"); break;
1251 case PHY_STAT_10HDX: printk(", 10 Mbps Half Duplex"); break;
1252 default:
1253 printk(", Unknown speed/duplex");
1254 }
1255
1256 if (*s & PHY_STAT_ANC)
1257 printk(", auto-negotiation complete");
1258 }
1259
1260 if (*s & PHY_STAT_FAULT)
1261 printk(", remote fault");
1262
1263 printk(".\n");
1264}
1265
Aristeu Sergio Rozanski Filho8b0ed2f2005-08-07 09:42:28 -07001266static void mii_display_config(void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Aristeu Sergio Rozanski Filho8b0ed2f2005-08-07 09:42:28 -07001268 struct net_device *dev = (struct net_device *)priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 struct fec_enet_private *fep = dev->priv;
1270 volatile uint *s = &(fep->phy_status);
1271
1272 printk("%s: config: auto-negotiation ", dev->name);
1273
1274 if (*s & PHY_CONF_ANE)
1275 printk("on");
1276 else
1277 printk("off");
1278
1279 if (*s & PHY_CONF_100FDX)
1280 printk(", 100FDX");
1281 if (*s & PHY_CONF_100HDX)
1282 printk(", 100HDX");
1283 if (*s & PHY_CONF_10FDX)
1284 printk(", 10FDX");
1285 if (*s & PHY_CONF_10HDX)
1286 printk(", 10HDX");
1287 if (!(*s & PHY_CONF_SPMASK))
1288 printk(", No speed/duplex selected?");
1289
1290 if (*s & PHY_CONF_LOOP)
1291 printk(", loopback enabled");
1292
1293 printk(".\n");
1294
1295 fep->sequence_done = 1;
1296}
1297
Aristeu Sergio Rozanski Filho8b0ed2f2005-08-07 09:42:28 -07001298static void mii_relink(void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
Aristeu Sergio Rozanski Filho8b0ed2f2005-08-07 09:42:28 -07001300 struct net_device *dev = (struct net_device *)priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 struct fec_enet_private *fep = dev->priv;
1302 int duplex;
1303
1304 fep->link = (fep->phy_status & PHY_STAT_LINK) ? 1 : 0;
1305 mii_display_status(dev);
1306 fep->old_link = fep->link;
1307
1308 if (fep->link) {
1309 duplex = 0;
1310 if (fep->phy_status
1311 & (PHY_STAT_100FDX | PHY_STAT_10FDX))
1312 duplex = 1;
1313 fec_restart(dev, duplex);
1314 }
1315 else
1316 fec_stop(dev);
1317
1318#if 0
1319 enable_irq(fep->mii_irq);
1320#endif
1321
1322}
1323
1324static void mii_queue_relink(uint mii_reg, struct net_device *dev)
1325{
1326 struct fec_enet_private *fep = dev->priv;
1327
Aristeu Sergio Rozanski Filho8b0ed2f2005-08-07 09:42:28 -07001328 INIT_WORK(&fep->phy_task, mii_relink, (void *)dev);
1329 schedule_work(&fep->phy_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
1331
1332static void mii_queue_config(uint mii_reg, struct net_device *dev)
1333{
1334 struct fec_enet_private *fep = dev->priv;
1335
Aristeu Sergio Rozanski Filho8b0ed2f2005-08-07 09:42:28 -07001336 INIT_WORK(&fep->phy_task, mii_display_config, (void *)dev);
1337 schedule_work(&fep->phy_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
1340
1341
1342phy_cmd_t phy_cmd_relink[] = { { mk_mii_read(MII_REG_CR), mii_queue_relink },
1343 { mk_mii_end, } };
1344phy_cmd_t phy_cmd_config[] = { { mk_mii_read(MII_REG_CR), mii_queue_config },
1345 { mk_mii_end, } };
1346
1347
1348
1349/* Read remainder of PHY ID.
1350*/
1351static void
1352mii_discover_phy3(uint mii_reg, struct net_device *dev)
1353{
1354 struct fec_enet_private *fep;
1355 int i;
1356
1357 fep = dev->priv;
1358 fep->phy_id |= (mii_reg & 0xffff);
1359
1360 for(i = 0; phy_info[i]; i++)
1361 if(phy_info[i]->id == (fep->phy_id >> 4))
1362 break;
1363
1364 if(!phy_info[i])
1365 panic("%s: PHY id 0x%08x is not supported!\n",
1366 dev->name, fep->phy_id);
1367
1368 fep->phy = phy_info[i];
1369 fep->phy_id_done = 1;
1370
1371 printk("%s: Phy @ 0x%x, type %s (0x%08x)\n",
1372 dev->name, fep->phy_addr, fep->phy->name, fep->phy_id);
1373}
1374
1375/* Scan all of the MII PHY addresses looking for someone to respond
1376 * with a valid ID. This usually happens quickly.
1377 */
1378static void
1379mii_discover_phy(uint mii_reg, struct net_device *dev)
1380{
1381 struct fec_enet_private *fep;
1382 uint phytype;
1383
1384 fep = dev->priv;
1385
1386 if ((phytype = (mii_reg & 0xffff)) != 0xffff) {
1387
1388 /* Got first part of ID, now get remainder.
1389 */
1390 fep->phy_id = phytype << 16;
1391 mii_queue(dev, mk_mii_read(MII_REG_PHYIR2), mii_discover_phy3);
1392 } else {
1393 fep->phy_addr++;
1394 if (fep->phy_addr < 32) {
1395 mii_queue(dev, mk_mii_read(MII_REG_PHYIR1),
1396 mii_discover_phy);
1397 } else {
1398 printk("fec: No PHY device found.\n");
1399 }
1400 }
1401}
1402#endif /* CONFIG_USE_MDIO */
1403
1404/* This interrupt occurs when the PHY detects a link change.
1405*/
Aristeu Sergio Rozanski Filhofbccb3d2005-08-07 09:42:36 -07001406static
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407#ifdef CONFIG_RPXCLASSIC
Aristeu Sergio Rozanski Filhofbccb3d2005-08-07 09:42:36 -07001408void mii_link_interrupt(void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409#else
Al Viro39e3eb72006-10-09 12:48:42 +01001410irqreturn_t mii_link_interrupt(int irq, void * dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411#endif
1412{
1413#ifdef CONFIG_USE_MDIO
1414 struct net_device *dev = dev_id;
1415 struct fec_enet_private *fep = dev->priv;
1416 volatile immap_t *immap = (immap_t *)IMAP_ADDR;
1417 volatile fec_t *fecp = &(immap->im_cpm.cp_fec);
1418 unsigned int ecntrl = fecp->fec_ecntrl;
1419
1420 /* We need the FEC enabled to access the MII
1421 */
1422 if ((ecntrl & FEC_ECNTRL_ETHER_EN) == 0) {
1423 fecp->fec_ecntrl |= FEC_ECNTRL_ETHER_EN;
1424 }
1425#endif /* CONFIG_USE_MDIO */
1426
1427#if 0
1428 disable_irq(fep->mii_irq); /* disable now, enable later */
1429#endif
1430
1431
1432#ifdef CONFIG_USE_MDIO
1433 mii_do_cmd(dev, fep->phy->ack_int);
1434 mii_do_cmd(dev, phy_cmd_relink); /* restart and display status */
1435
1436 if ((ecntrl & FEC_ECNTRL_ETHER_EN) == 0) {
1437 fecp->fec_ecntrl = ecntrl; /* restore old settings */
1438 }
1439#else
1440printk("%s[%d] %s: unexpected Link interrupt\n", __FILE__,__LINE__,__FUNCTION__);
1441#endif /* CONFIG_USE_MDIO */
1442
Aristeu Sergio Rozanski Filhofbccb3d2005-08-07 09:42:36 -07001443#ifndef CONFIG_RPXCLASSIC
1444 return IRQ_RETVAL(IRQ_HANDLED);
1445#endif /* CONFIG_RPXCLASSIC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
1448static int
1449fec_enet_open(struct net_device *dev)
1450{
1451 struct fec_enet_private *fep = dev->priv;
1452
1453 /* I should reset the ring buffers here, but I don't yet know
1454 * a simple way to do that.
1455 */
1456
1457#ifdef CONFIG_USE_MDIO
1458 fep->sequence_done = 0;
1459 fep->link = 0;
1460
1461 if (fep->phy) {
1462 mii_do_cmd(dev, fep->phy->ack_int);
1463 mii_do_cmd(dev, fep->phy->config);
1464 mii_do_cmd(dev, phy_cmd_config); /* display configuration */
1465 while(!fep->sequence_done)
1466 schedule();
1467
1468 mii_do_cmd(dev, fep->phy->startup);
1469 netif_start_queue(dev);
1470 return 0; /* Success */
1471 }
1472 return -ENODEV; /* No PHY we understand */
1473#else
1474 fep->link = 1;
1475 netif_start_queue(dev);
1476 return 0; /* Success */
1477#endif /* CONFIG_USE_MDIO */
1478
1479}
1480
1481static int
1482fec_enet_close(struct net_device *dev)
1483{
1484 /* Don't know what to do yet.
1485 */
1486 netif_stop_queue(dev);
1487 fec_stop(dev);
1488
1489 return 0;
1490}
1491
1492static struct net_device_stats *fec_enet_get_stats(struct net_device *dev)
1493{
1494 struct fec_enet_private *fep = (struct fec_enet_private *)dev->priv;
1495
1496 return &fep->stats;
1497}
1498
1499/* Set or clear the multicast filter for this adaptor.
1500 * Skeleton taken from sunlance driver.
1501 * The CPM Ethernet implementation allows Multicast as well as individual
1502 * MAC address filtering. Some of the drivers check to make sure it is
1503 * a group multicast address, and discard those that are not. I guess I
1504 * will do the same for now, but just remove the test if you want
1505 * individual filtering as well (do the upper net layers want or support
1506 * this kind of feature?).
1507 */
1508
1509static void set_multicast_list(struct net_device *dev)
1510{
1511 struct fec_enet_private *fep;
1512 volatile fec_t *ep;
1513
1514 fep = (struct fec_enet_private *)dev->priv;
1515 ep = &(((immap_t *)IMAP_ADDR)->im_cpm.cp_fec);
1516
1517 if (dev->flags&IFF_PROMISC) {
1518
1519 /* Log any net taps. */
1520 printk("%s: Promiscuous mode enabled.\n", dev->name);
1521 ep->fec_r_cntrl |= FEC_RCNTRL_PROM;
1522 } else {
1523
1524 ep->fec_r_cntrl &= ~FEC_RCNTRL_PROM;
1525
1526 if (dev->flags & IFF_ALLMULTI) {
1527 /* Catch all multicast addresses, so set the
1528 * filter to all 1's.
1529 */
1530 ep->fec_hash_table_high = 0xffffffff;
1531 ep->fec_hash_table_low = 0xffffffff;
1532 }
1533#if 0
1534 else {
1535 /* Clear filter and add the addresses in the list.
1536 */
1537 ep->sen_gaddr1 = 0;
1538 ep->sen_gaddr2 = 0;
1539 ep->sen_gaddr3 = 0;
1540 ep->sen_gaddr4 = 0;
1541
1542 dmi = dev->mc_list;
1543
1544 for (i=0; i<dev->mc_count; i++) {
1545
1546 /* Only support group multicast for now.
1547 */
1548 if (!(dmi->dmi_addr[0] & 1))
1549 continue;
1550
1551 /* The address in dmi_addr is LSB first,
1552 * and taddr is MSB first. We have to
1553 * copy bytes MSB first from dmi_addr.
1554 */
1555 mcptr = (u_char *)dmi->dmi_addr + 5;
1556 tdptr = (u_char *)&ep->sen_taddrh;
1557 for (j=0; j<6; j++)
1558 *tdptr++ = *mcptr--;
1559
1560 /* Ask CPM to run CRC and set bit in
1561 * filter mask.
1562 */
1563 cpmp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SCC1, CPM_CR_SET_GADDR) | CPM_CR_FLG;
1564 /* this delay is necessary here -- Cort */
1565 udelay(10);
1566 while (cpmp->cp_cpcr & CPM_CR_FLG);
1567 }
1568 }
1569#endif
1570 }
1571}
1572
1573/* Initialize the FEC Ethernet on 860T.
1574 */
1575static int __init fec_enet_init(void)
1576{
1577 struct net_device *dev;
1578 struct fec_enet_private *fep;
1579 int i, j, k, err;
1580 unsigned char *eap, *iap, *ba;
Aristeu Sergio Rozanski Filhofc007dd2005-08-07 09:42:33 -07001581 dma_addr_t mem_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 volatile cbd_t *bdp;
1583 cbd_t *cbd_base;
1584 volatile immap_t *immap;
1585 volatile fec_t *fecp;
1586 bd_t *bd;
1587#ifdef CONFIG_SCC_ENET
1588 unsigned char tmpaddr[6];
1589#endif
1590
1591 immap = (immap_t *)IMAP_ADDR; /* pointer to internal registers */
1592
1593 bd = (bd_t *)__res;
1594
1595 dev = alloc_etherdev(sizeof(*fep));
1596 if (!dev)
1597 return -ENOMEM;
1598
1599 fep = dev->priv;
1600
1601 fecp = &(immap->im_cpm.cp_fec);
1602
1603 /* Whack a reset. We should wait for this.
1604 */
1605 fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
1606 for (i = 0;
1607 (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
1608 ++i) {
1609 udelay(1);
1610 }
1611 if (i == FEC_RESET_DELAY) {
1612 printk ("FEC Reset timeout!\n");
1613 }
1614
1615 /* Set the Ethernet address. If using multiple Enets on the 8xx,
1616 * this needs some work to get unique addresses.
1617 */
1618 eap = (unsigned char *)my_enet_addr;
1619 iap = bd->bi_enetaddr;
1620
1621#ifdef CONFIG_SCC_ENET
1622 /*
1623 * If a board has Ethernet configured both on a SCC and the
1624 * FEC, it needs (at least) 2 MAC addresses (we know that Sun
1625 * disagrees, but anyway). For the FEC port, we create
1626 * another address by setting one of the address bits above
1627 * something that would have (up to now) been allocated.
1628 */
1629 for (i=0; i<6; i++)
1630 tmpaddr[i] = *iap++;
1631 tmpaddr[3] |= 0x80;
1632 iap = tmpaddr;
1633#endif
1634
1635 for (i=0; i<6; i++) {
1636 dev->dev_addr[i] = *eap++ = *iap++;
1637 }
1638
1639 /* Allocate memory for buffer descriptors.
1640 */
1641 if (((RX_RING_SIZE + TX_RING_SIZE) * sizeof(cbd_t)) > PAGE_SIZE) {
1642 printk("FEC init error. Need more space.\n");
1643 printk("FEC initialization failed.\n");
1644 return 1;
1645 }
Aristeu Sergio Rozanski Filhofc007dd2005-08-07 09:42:33 -07001646 cbd_base = (cbd_t *)dma_alloc_coherent(dev->class_dev.dev, PAGE_SIZE,
1647 &mem_addr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 /* Set receive and transmit descriptor base.
1650 */
1651 fep->rx_bd_base = cbd_base;
1652 fep->tx_bd_base = cbd_base + RX_RING_SIZE;
1653
1654 fep->skb_cur = fep->skb_dirty = 0;
1655
1656 /* Initialize the receive buffer descriptors.
1657 */
1658 bdp = fep->rx_bd_base;
1659 k = 0;
1660 for (i=0; i<FEC_ENET_RX_PAGES; i++) {
1661
1662 /* Allocate a page.
1663 */
Aristeu Sergio Rozanski Filhofc007dd2005-08-07 09:42:33 -07001664 ba = (unsigned char *)dma_alloc_coherent(dev->class_dev.dev,
1665 PAGE_SIZE,
1666 &mem_addr,
1667 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 /* BUG: no check for failure */
1669
1670 /* Initialize the BD for every fragment in the page.
1671 */
1672 for (j=0; j<FEC_ENET_RX_FRPPG; j++) {
1673 bdp->cbd_sc = BD_ENET_RX_EMPTY;
1674 bdp->cbd_bufaddr = mem_addr;
1675 fep->rx_vaddr[k++] = ba;
1676 mem_addr += FEC_ENET_RX_FRSIZE;
1677 ba += FEC_ENET_RX_FRSIZE;
1678 bdp++;
1679 }
1680 }
1681
1682 /* Set the last buffer to wrap.
1683 */
1684 bdp--;
1685 bdp->cbd_sc |= BD_SC_WRAP;
1686
1687#ifdef CONFIG_FEC_PACKETHOOK
1688 fep->ph_lock = 0;
1689 fep->ph_rxhandler = fep->ph_txhandler = NULL;
1690 fep->ph_proto = 0;
1691 fep->ph_regaddr = NULL;
1692 fep->ph_priv = NULL;
1693#endif
1694
1695 /* Install our interrupt handler.
1696 */
1697 if (request_irq(FEC_INTERRUPT, fec_enet_interrupt, 0, "fec", dev) != 0)
1698 panic("Could not allocate FEC IRQ!");
1699
1700#ifdef CONFIG_RPXCLASSIC
1701 /* Make Port C, bit 15 an input that causes interrupts.
1702 */
1703 immap->im_ioport.iop_pcpar &= ~0x0001;
1704 immap->im_ioport.iop_pcdir &= ~0x0001;
1705 immap->im_ioport.iop_pcso &= ~0x0001;
1706 immap->im_ioport.iop_pcint |= 0x0001;
1707 cpm_install_handler(CPMVEC_PIO_PC15, mii_link_interrupt, dev);
1708
1709 /* Make LEDS reflect Link status.
1710 */
1711 *((uint *) RPX_CSR_ADDR) &= ~BCSR2_FETHLEDMODE;
1712#endif
1713
1714#ifdef PHY_INTERRUPT
1715 ((immap_t *)IMAP_ADDR)->im_siu_conf.sc_siel |=
1716 (0x80000000 >> PHY_INTERRUPT);
1717
1718 if (request_irq(PHY_INTERRUPT, mii_link_interrupt, 0, "mii", dev) != 0)
1719 panic("Could not allocate MII IRQ!");
1720#endif
1721
1722 dev->base_addr = (unsigned long)fecp;
1723
1724 /* The FEC Ethernet specific entries in the device structure. */
1725 dev->open = fec_enet_open;
1726 dev->hard_start_xmit = fec_enet_start_xmit;
1727 dev->tx_timeout = fec_timeout;
1728 dev->watchdog_timeo = TX_TIMEOUT;
1729 dev->stop = fec_enet_close;
1730 dev->get_stats = fec_enet_get_stats;
1731 dev->set_multicast_list = set_multicast_list;
1732
1733#ifdef CONFIG_USE_MDIO
1734 for (i=0; i<NMII-1; i++)
1735 mii_cmds[i].mii_next = &mii_cmds[i+1];
1736 mii_free = mii_cmds;
1737#endif /* CONFIG_USE_MDIO */
1738
1739 /* Configure all of port D for MII.
1740 */
1741 immap->im_ioport.iop_pdpar = 0x1fff;
1742
1743 /* Bits moved from Rev. D onward.
1744 */
1745 if ((mfspr(SPRN_IMMR) & 0xffff) < 0x0501)
1746 immap->im_ioport.iop_pddir = 0x1c58; /* Pre rev. D */
1747 else
1748 immap->im_ioport.iop_pddir = 0x1fff; /* Rev. D and later */
1749
1750#ifdef CONFIG_USE_MDIO
1751 /* Set MII speed to 2.5 MHz
1752 */
1753 fecp->fec_mii_speed = fep->phy_speed =
1754 (( (bd->bi_intfreq + 500000) / 2500000 / 2 ) & 0x3F ) << 1;
1755#else
1756 fecp->fec_mii_speed = 0; /* turn off MDIO */
1757#endif /* CONFIG_USE_MDIO */
1758
1759 err = register_netdev(dev);
1760 if (err) {
1761 free_netdev(dev);
1762 return err;
1763 }
1764
1765 printk ("%s: FEC ENET Version 0.2, FEC irq %d"
1766#ifdef PHY_INTERRUPT
1767 ", MII irq %d"
1768#endif
1769 ", addr ",
1770 dev->name, FEC_INTERRUPT
1771#ifdef PHY_INTERRUPT
1772 , PHY_INTERRUPT
1773#endif
1774 );
1775 for (i=0; i<6; i++)
1776 printk("%02x%c", dev->dev_addr[i], (i==5) ? '\n' : ':');
1777
1778#ifdef CONFIG_USE_MDIO /* start in full duplex mode, and negotiate speed */
1779 fec_restart (dev, 1);
1780#else /* always use half duplex mode only */
1781 fec_restart (dev, 0);
1782#endif
1783
1784#ifdef CONFIG_USE_MDIO
1785 /* Queue up command to detect the PHY and initialize the
1786 * remainder of the interface.
1787 */
1788 fep->phy_id_done = 0;
1789 fep->phy_addr = 0;
1790 mii_queue(dev, mk_mii_read(MII_REG_PHYIR1), mii_discover_phy);
1791#endif /* CONFIG_USE_MDIO */
1792
1793 return 0;
1794}
1795module_init(fec_enet_init);
1796
1797/* This function is called to start or restart the FEC during a link
1798 * change. This only happens when switching between half and full
1799 * duplex.
1800 */
1801static void
1802fec_restart(struct net_device *dev, int duplex)
1803{
1804 struct fec_enet_private *fep;
1805 int i;
1806 volatile cbd_t *bdp;
1807 volatile immap_t *immap;
1808 volatile fec_t *fecp;
1809
1810 immap = (immap_t *)IMAP_ADDR; /* pointer to internal registers */
1811
1812 fecp = &(immap->im_cpm.cp_fec);
1813
1814 fep = dev->priv;
1815
1816 /* Whack a reset. We should wait for this.
1817 */
1818 fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
1819 for (i = 0;
1820 (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
1821 ++i) {
1822 udelay(1);
1823 }
1824 if (i == FEC_RESET_DELAY) {
1825 printk ("FEC Reset timeout!\n");
1826 }
1827
1828 /* Set station address.
1829 */
1830 fecp->fec_addr_low = (my_enet_addr[0] << 16) | my_enet_addr[1];
1831 fecp->fec_addr_high = my_enet_addr[2];
1832
1833 /* Reset all multicast.
1834 */
1835 fecp->fec_hash_table_high = 0;
1836 fecp->fec_hash_table_low = 0;
1837
1838 /* Set maximum receive buffer size.
1839 */
1840 fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;
1841 fecp->fec_r_hash = PKT_MAXBUF_SIZE;
1842
1843 /* Set receive and transmit descriptor base.
1844 */
1845 fecp->fec_r_des_start = iopa((uint)(fep->rx_bd_base));
1846 fecp->fec_x_des_start = iopa((uint)(fep->tx_bd_base));
1847
1848 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
1849 fep->cur_rx = fep->rx_bd_base;
1850
1851 /* Reset SKB transmit buffers.
1852 */
1853 fep->skb_cur = fep->skb_dirty = 0;
1854 for (i=0; i<=TX_RING_MOD_MASK; i++) {
1855 if (fep->tx_skbuff[i] != NULL) {
1856 dev_kfree_skb(fep->tx_skbuff[i]);
1857 fep->tx_skbuff[i] = NULL;
1858 }
1859 }
1860
1861 /* Initialize the receive buffer descriptors.
1862 */
1863 bdp = fep->rx_bd_base;
1864 for (i=0; i<RX_RING_SIZE; i++) {
1865
1866 /* Initialize the BD for every fragment in the page.
1867 */
1868 bdp->cbd_sc = BD_ENET_RX_EMPTY;
1869 bdp++;
1870 }
1871
1872 /* Set the last buffer to wrap.
1873 */
1874 bdp--;
1875 bdp->cbd_sc |= BD_SC_WRAP;
1876
1877 /* ...and the same for transmmit.
1878 */
1879 bdp = fep->tx_bd_base;
1880 for (i=0; i<TX_RING_SIZE; i++) {
1881
1882 /* Initialize the BD for every fragment in the page.
1883 */
1884 bdp->cbd_sc = 0;
1885 bdp->cbd_bufaddr = 0;
1886 bdp++;
1887 }
1888
1889 /* Set the last buffer to wrap.
1890 */
1891 bdp--;
1892 bdp->cbd_sc |= BD_SC_WRAP;
1893
1894 /* Enable MII mode.
1895 */
1896 if (duplex) {
1897 fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE; /* MII enable */
1898 fecp->fec_x_cntrl = FEC_TCNTRL_FDEN; /* FD enable */
1899 }
1900 else {
1901 fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT;
1902 fecp->fec_x_cntrl = 0;
1903 }
1904 fep->full_duplex = duplex;
1905
1906 /* Enable big endian and don't care about SDMA FC.
1907 */
1908 fecp->fec_fun_code = 0x78000000;
1909
1910#ifdef CONFIG_USE_MDIO
1911 /* Set MII speed.
1912 */
1913 fecp->fec_mii_speed = fep->phy_speed;
1914#endif /* CONFIG_USE_MDIO */
1915
1916 /* Clear any outstanding interrupt.
1917 */
1918 fecp->fec_ievent = 0xffc0;
1919
1920 fecp->fec_ivec = (FEC_INTERRUPT/2) << 29;
1921
1922 /* Enable interrupts we wish to service.
1923 */
1924 fecp->fec_imask = ( FEC_ENET_TXF | FEC_ENET_TXB |
1925 FEC_ENET_RXF | FEC_ENET_RXB | FEC_ENET_MII );
1926
1927 /* And last, enable the transmit and receive processing.
1928 */
1929 fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
1930 fecp->fec_r_des_active = 0x01000000;
1931}
1932
1933static void
1934fec_stop(struct net_device *dev)
1935{
1936 volatile immap_t *immap;
1937 volatile fec_t *fecp;
1938 struct fec_enet_private *fep;
1939 int i;
1940
1941 immap = (immap_t *)IMAP_ADDR; /* pointer to internal registers */
1942
1943 fecp = &(immap->im_cpm.cp_fec);
1944
1945 if ((fecp->fec_ecntrl & FEC_ECNTRL_ETHER_EN) == 0)
1946 return; /* already down */
1947
1948 fep = dev->priv;
1949
1950
1951 fecp->fec_x_cntrl = 0x01; /* Graceful transmit stop */
1952
1953 for (i = 0;
1954 ((fecp->fec_ievent & 0x10000000) == 0) && (i < FEC_RESET_DELAY);
1955 ++i) {
1956 udelay(1);
1957 }
1958 if (i == FEC_RESET_DELAY) {
1959 printk ("FEC timeout on graceful transmit stop\n");
1960 }
1961
1962 /* Clear outstanding MII command interrupts.
1963 */
1964 fecp->fec_ievent = FEC_ENET_MII;
1965
1966 /* Enable MII command finished interrupt
1967 */
1968 fecp->fec_ivec = (FEC_INTERRUPT/2) << 29;
1969 fecp->fec_imask = FEC_ENET_MII;
1970
1971#ifdef CONFIG_USE_MDIO
1972 /* Set MII speed.
1973 */
1974 fecp->fec_mii_speed = fep->phy_speed;
1975#endif /* CONFIG_USE_MDIO */
1976
1977 /* Disable FEC
1978 */
1979 fecp->fec_ecntrl &= ~(FEC_ECNTRL_ETHER_EN);
1980}