2 * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
3 * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
5 * Right now, I am very wasteful with the buffers. I allocate memory
6 * pages and then divide them into 2K frame buffers. This way I know I
7 * have buffers large enough to hold one frame within one buffer descriptor.
8 * Once I get this working, I will use 64 or 128 byte CPM buffers, which
9 * will be much more memory efficient and will easily handle lots of
12 * Much better multiple PHY support by Magnus Damm.
13 * Copyright (c) 2000 Ericsson Radio Systems AB.
15 * Support for FEC controller of ColdFire processors.
16 * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
18 * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
19 * Copyright (c) 2004-2006 Macq Electronique SA.
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/string.h>
25 #include <linux/ptrace.h>
26 #include <linux/errno.h>
27 #include <linux/ioport.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h>
30 #include <linux/pci.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/skbuff.h>
36 #include <linux/spinlock.h>
37 #include <linux/workqueue.h>
38 #include <linux/bitops.h>
40 #include <linux/irq.h>
41 #include <linux/clk.h>
42 #include <linux/platform_device.h>
44 #include <asm/cacheflush.h>
46 #ifndef CONFIG_ARCH_MXC
47 #include <asm/coldfire.h>
48 #include <asm/mcfsim.h>
53 #ifdef CONFIG_ARCH_MXC
54 #include <mach/hardware.h>
55 #define FEC_ALIGNMENT 0xf
57 #define FEC_ALIGNMENT 0x3
61 * Define the fixed address of the FEC hardware.
63 #if defined(CONFIG_M5272)
64 #define HAVE_mii_link_interrupt
66 static unsigned char fec_mac_default
[] = {
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71 * Some hardware gets it MAC address out of local flash memory.
72 * if this is non-zero then assume it is the address to get MAC from.
74 #if defined(CONFIG_NETtel)
75 #define FEC_FLASHMAC 0xf0006006
76 #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
77 #define FEC_FLASHMAC 0xf0006000
78 #elif defined(CONFIG_CANCam)
79 #define FEC_FLASHMAC 0xf0020000
80 #elif defined (CONFIG_M5272C3)
81 #define FEC_FLASHMAC (0xffe04000 + 4)
82 #elif defined(CONFIG_MOD5272)
83 #define FEC_FLASHMAC 0xffc0406b
85 #define FEC_FLASHMAC 0
87 #endif /* CONFIG_M5272 */
89 /* Forward declarations of some structures to support different PHYs
94 void (*funct
)(uint mii_reg
, struct net_device
*dev
);
101 const phy_cmd_t
*config
;
102 const phy_cmd_t
*startup
;
103 const phy_cmd_t
*ack_int
;
104 const phy_cmd_t
*shutdown
;
107 /* The number of Tx and Rx buffers. These are allocated from the page
108 * pool. The code may assume these are power of two, so it it best
109 * to keep them that size.
110 * We don't need to allocate pages for the transmitter. We just use
111 * the skbuffer directly.
113 #define FEC_ENET_RX_PAGES 8
114 #define FEC_ENET_RX_FRSIZE 2048
115 #define FEC_ENET_RX_FRPPG (PAGE_SIZE / FEC_ENET_RX_FRSIZE)
116 #define RX_RING_SIZE (FEC_ENET_RX_FRPPG * FEC_ENET_RX_PAGES)
117 #define FEC_ENET_TX_FRSIZE 2048
118 #define FEC_ENET_TX_FRPPG (PAGE_SIZE / FEC_ENET_TX_FRSIZE)
119 #define TX_RING_SIZE 16 /* Must be power of two */
120 #define TX_RING_MOD_MASK 15 /* for this to work */
122 #if (((RX_RING_SIZE + TX_RING_SIZE) * 8) > PAGE_SIZE)
123 #error "FEC: descriptor ring size constants too large"
126 /* Interrupt events/masks.
128 #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
129 #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
130 #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
131 #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
132 #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
133 #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
134 #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
135 #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
136 #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
137 #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
139 /* The FEC stores dest/src/type, data, and checksum for receive packets.
141 #define PKT_MAXBUF_SIZE 1518
142 #define PKT_MINBUF_SIZE 64
143 #define PKT_MAXBLR_SIZE 1520
147 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
148 * size bits. Other FEC hardware does not, so we need to take that into
149 * account when setting it.
151 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
152 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARCH_MXC)
153 #define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
155 #define OPT_FRAME_SIZE 0
158 /* The FEC buffer descriptors track the ring buffers. The rx_bd_base and
159 * tx_bd_base always point to the base of the buffer descriptors. The
160 * cur_rx and cur_tx point to the currently available buffer.
161 * The dirty_tx tracks the current buffer that is being sent by the
162 * controller. The cur_tx and dirty_tx are equal under both completely
163 * empty and completely full conditions. The empty/ready indicator in
164 * the buffer descriptor determines the actual condition.
166 struct fec_enet_private
{
167 /* Hardware registers of the FEC device */
170 struct net_device
*netdev
;
174 /* The saved address of a sent-in-place packet/buffer, for skfree(). */
175 unsigned char *tx_bounce
[TX_RING_SIZE
];
176 struct sk_buff
* tx_skbuff
[TX_RING_SIZE
];
180 /* CPM dual port RAM relative addresses.
183 cbd_t
*rx_bd_base
; /* Address of Rx and Tx buffers. */
185 cbd_t
*cur_rx
, *cur_tx
; /* The next free ring entry */
186 cbd_t
*dirty_tx
; /* The ring entries to be free()ed. */
188 /* hold while accessing the HW like ringbuffer for tx/rx but not MAC */
190 /* hold while accessing the mii_list_t() elements */
197 phy_info_t
const *phy
;
198 struct work_struct phy_task
;
201 uint mii_phy_task_queued
;
212 static int fec_enet_open(struct net_device
*dev
);
213 static int fec_enet_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
);
214 static void fec_enet_mii(struct net_device
*dev
);
215 static irqreturn_t
fec_enet_interrupt(int irq
, void * dev_id
);
216 static void fec_enet_tx(struct net_device
*dev
);
217 static void fec_enet_rx(struct net_device
*dev
);
218 static int fec_enet_close(struct net_device
*dev
);
219 static void set_multicast_list(struct net_device
*dev
);
220 static void fec_restart(struct net_device
*dev
, int duplex
);
221 static void fec_stop(struct net_device
*dev
);
222 static void fec_set_mac_address(struct net_device
*dev
);
225 /* MII processing. We keep this as simple as possible. Requests are
226 * placed on the list (if there is room). When the request is finished
227 * by the MII, an optional function may be called.
229 typedef struct mii_list
{
231 void (*mii_func
)(uint val
, struct net_device
*dev
);
232 struct mii_list
*mii_next
;
236 static mii_list_t mii_cmds
[NMII
];
237 static mii_list_t
*mii_free
;
238 static mii_list_t
*mii_head
;
239 static mii_list_t
*mii_tail
;
241 static int mii_queue(struct net_device
*dev
, int request
,
242 void (*func
)(uint
, struct net_device
*));
244 /* Make MII read/write commands for the FEC.
246 #define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18))
247 #define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | \
251 /* Transmitter timeout.
253 #define TX_TIMEOUT (2*HZ)
255 /* Register definitions for the PHY.
258 #define MII_REG_CR 0 /* Control Register */
259 #define MII_REG_SR 1 /* Status Register */
260 #define MII_REG_PHYIR1 2 /* PHY Identification Register 1 */
261 #define MII_REG_PHYIR2 3 /* PHY Identification Register 2 */
262 #define MII_REG_ANAR 4 /* A-N Advertisement Register */
263 #define MII_REG_ANLPAR 5 /* A-N Link Partner Ability Register */
264 #define MII_REG_ANER 6 /* A-N Expansion Register */
265 #define MII_REG_ANNPTR 7 /* A-N Next Page Transmit Register */
266 #define MII_REG_ANLPRNPR 8 /* A-N Link Partner Received Next Page Reg. */
268 /* values for phy_status */
270 #define PHY_CONF_ANE 0x0001 /* 1 auto-negotiation enabled */
271 #define PHY_CONF_LOOP 0x0002 /* 1 loopback mode enabled */
272 #define PHY_CONF_SPMASK 0x00f0 /* mask for speed */
273 #define PHY_CONF_10HDX 0x0010 /* 10 Mbit half duplex supported */
274 #define PHY_CONF_10FDX 0x0020 /* 10 Mbit full duplex supported */
275 #define PHY_CONF_100HDX 0x0040 /* 100 Mbit half duplex supported */
276 #define PHY_CONF_100FDX 0x0080 /* 100 Mbit full duplex supported */
278 #define PHY_STAT_LINK 0x0100 /* 1 up - 0 down */
279 #define PHY_STAT_FAULT 0x0200 /* 1 remote fault */
280 #define PHY_STAT_ANC 0x0400 /* 1 auto-negotiation complete */
281 #define PHY_STAT_SPMASK 0xf000 /* mask for speed */
282 #define PHY_STAT_10HDX 0x1000 /* 10 Mbit half duplex selected */
283 #define PHY_STAT_10FDX 0x2000 /* 10 Mbit full duplex selected */
284 #define PHY_STAT_100HDX 0x4000 /* 100 Mbit half duplex selected */
285 #define PHY_STAT_100FDX 0x8000 /* 100 Mbit full duplex selected */
289 fec_enet_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
291 struct fec_enet_private
*fep
;
292 volatile fec_t
*fecp
;
294 unsigned short status
;
297 fep
= netdev_priv(dev
);
298 fecp
= (volatile fec_t
*)dev
->base_addr
;
301 /* Link is down or autonegotiation is in progress. */
305 spin_lock_irqsave(&fep
->hw_lock
, flags
);
306 /* Fill in a Tx ring entry */
309 status
= bdp
->cbd_sc
;
310 #ifndef final_version
311 if (status
& BD_ENET_TX_READY
) {
312 /* Ooops. All transmit buffers are full. Bail out.
313 * This should not happen, since dev->tbusy should be set.
315 printk("%s: tx queue full!.\n", dev
->name
);
316 spin_unlock_irqrestore(&fep
->hw_lock
, flags
);
321 /* Clear all of the status flags.
323 status
&= ~BD_ENET_TX_STATS
;
325 /* Set buffer length and buffer pointer.
327 bdp
->cbd_bufaddr
= __pa(skb
->data
);
328 bdp
->cbd_datlen
= skb
->len
;
331 * On some FEC implementations data must be aligned on
332 * 4-byte boundaries. Use bounce buffers to copy data
333 * and get it aligned. Ugh.
335 if (bdp
->cbd_bufaddr
& FEC_ALIGNMENT
) {
337 index
= bdp
- fep
->tx_bd_base
;
338 memcpy(fep
->tx_bounce
[index
], (void *)skb
->data
, skb
->len
);
339 bdp
->cbd_bufaddr
= __pa(fep
->tx_bounce
[index
]);
344 fep
->tx_skbuff
[fep
->skb_cur
] = skb
;
346 dev
->stats
.tx_bytes
+= skb
->len
;
347 fep
->skb_cur
= (fep
->skb_cur
+1) & TX_RING_MOD_MASK
;
349 /* Push the data cache so the CPM does not get stale memory
352 dma_sync_single(NULL
, bdp
->cbd_bufaddr
,
353 bdp
->cbd_datlen
, DMA_TO_DEVICE
);
355 /* Send it on its way. Tell FEC it's ready, interrupt when done,
356 * it's the last BD of the frame, and to put the CRC on the end.
359 status
|= (BD_ENET_TX_READY
| BD_ENET_TX_INTR
360 | BD_ENET_TX_LAST
| BD_ENET_TX_TC
);
361 bdp
->cbd_sc
= status
;
363 dev
->trans_start
= jiffies
;
365 /* Trigger transmission start */
366 fecp
->fec_x_des_active
= 0;
368 /* If this was the last BD in the ring, start at the beginning again.
370 if (status
& BD_ENET_TX_WRAP
) {
371 bdp
= fep
->tx_bd_base
;
376 if (bdp
== fep
->dirty_tx
) {
378 netif_stop_queue(dev
);
381 fep
->cur_tx
= (cbd_t
*)bdp
;
383 spin_unlock_irqrestore(&fep
->hw_lock
, flags
);
389 fec_timeout(struct net_device
*dev
)
391 struct fec_enet_private
*fep
= netdev_priv(dev
);
393 printk("%s: transmit timed out.\n", dev
->name
);
394 dev
->stats
.tx_errors
++;
395 #ifndef final_version
400 printk("Ring data dump: cur_tx %lx%s, dirty_tx %lx cur_rx: %lx\n",
401 (unsigned long)fep
->cur_tx
, fep
->tx_full
? " (full)" : "",
402 (unsigned long)fep
->dirty_tx
,
403 (unsigned long)fep
->cur_rx
);
405 bdp
= fep
->tx_bd_base
;
406 printk(" tx: %u buffers\n", TX_RING_SIZE
);
407 for (i
= 0 ; i
< TX_RING_SIZE
; i
++) {
408 printk(" %08x: %04x %04x %08x\n",
412 (int) bdp
->cbd_bufaddr
);
416 bdp
= fep
->rx_bd_base
;
417 printk(" rx: %lu buffers\n", (unsigned long) RX_RING_SIZE
);
418 for (i
= 0 ; i
< RX_RING_SIZE
; i
++) {
419 printk(" %08x: %04x %04x %08x\n",
423 (int) bdp
->cbd_bufaddr
);
428 fec_restart(dev
, fep
->full_duplex
);
429 netif_wake_queue(dev
);
432 /* The interrupt handler.
433 * This is called from the MPC core interrupt.
436 fec_enet_interrupt(int irq
, void * dev_id
)
438 struct net_device
*dev
= dev_id
;
439 volatile fec_t
*fecp
;
441 irqreturn_t ret
= IRQ_NONE
;
443 fecp
= (volatile fec_t
*)dev
->base_addr
;
445 /* Get the interrupt events that caused us to be here.
448 int_events
= fecp
->fec_ievent
;
449 fecp
->fec_ievent
= int_events
;
451 /* Handle receive event in its own function.
453 if (int_events
& FEC_ENET_RXF
) {
458 /* Transmit OK, or non-fatal error. Update the buffer
459 descriptors. FEC handles all errors, we just discover
460 them as part of the transmit process.
462 if (int_events
& FEC_ENET_TXF
) {
467 if (int_events
& FEC_ENET_MII
) {
472 } while (int_events
);
479 fec_enet_tx(struct net_device
*dev
)
481 struct fec_enet_private
*fep
;
483 unsigned short status
;
486 fep
= netdev_priv(dev
);
487 spin_lock_irq(&fep
->hw_lock
);
490 while (((status
= bdp
->cbd_sc
) & BD_ENET_TX_READY
) == 0) {
491 if (bdp
== fep
->cur_tx
&& fep
->tx_full
== 0) break;
493 skb
= fep
->tx_skbuff
[fep
->skb_dirty
];
494 /* Check for errors. */
495 if (status
& (BD_ENET_TX_HB
| BD_ENET_TX_LC
|
496 BD_ENET_TX_RL
| BD_ENET_TX_UN
|
498 dev
->stats
.tx_errors
++;
499 if (status
& BD_ENET_TX_HB
) /* No heartbeat */
500 dev
->stats
.tx_heartbeat_errors
++;
501 if (status
& BD_ENET_TX_LC
) /* Late collision */
502 dev
->stats
.tx_window_errors
++;
503 if (status
& BD_ENET_TX_RL
) /* Retrans limit */
504 dev
->stats
.tx_aborted_errors
++;
505 if (status
& BD_ENET_TX_UN
) /* Underrun */
506 dev
->stats
.tx_fifo_errors
++;
507 if (status
& BD_ENET_TX_CSL
) /* Carrier lost */
508 dev
->stats
.tx_carrier_errors
++;
510 dev
->stats
.tx_packets
++;
513 #ifndef final_version
514 if (status
& BD_ENET_TX_READY
)
515 printk("HEY! Enet xmit interrupt and TX_READY.\n");
517 /* Deferred means some collisions occurred during transmit,
518 * but we eventually sent the packet OK.
520 if (status
& BD_ENET_TX_DEF
)
521 dev
->stats
.collisions
++;
523 /* Free the sk buffer associated with this last transmit.
525 dev_kfree_skb_any(skb
);
526 fep
->tx_skbuff
[fep
->skb_dirty
] = NULL
;
527 fep
->skb_dirty
= (fep
->skb_dirty
+ 1) & TX_RING_MOD_MASK
;
529 /* Update pointer to next buffer descriptor to be transmitted.
531 if (status
& BD_ENET_TX_WRAP
)
532 bdp
= fep
->tx_bd_base
;
536 /* Since we have freed up a buffer, the ring is no longer
541 if (netif_queue_stopped(dev
))
542 netif_wake_queue(dev
);
545 fep
->dirty_tx
= (cbd_t
*)bdp
;
546 spin_unlock_irq(&fep
->hw_lock
);
550 /* During a receive, the cur_rx points to the current incoming buffer.
551 * When we update through the ring, if the next incoming buffer has
552 * not been given to the system, we just set the empty indicator,
553 * effectively tossing the packet.
556 fec_enet_rx(struct net_device
*dev
)
558 struct fec_enet_private
*fep
;
559 volatile fec_t
*fecp
;
561 unsigned short status
;
570 fep
= netdev_priv(dev
);
571 fecp
= (volatile fec_t
*)dev
->base_addr
;
573 spin_lock_irq(&fep
->hw_lock
);
575 /* First, grab all of the stats for the incoming packet.
576 * These get messed up if we get called due to a busy condition.
580 while (!((status
= bdp
->cbd_sc
) & BD_ENET_RX_EMPTY
)) {
582 #ifndef final_version
583 /* Since we have allocated space to hold a complete frame,
584 * the last indicator should be set.
586 if ((status
& BD_ENET_RX_LAST
) == 0)
587 printk("FEC ENET: rcv is not +last\n");
591 goto rx_processing_done
;
593 /* Check for errors. */
594 if (status
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
| BD_ENET_RX_NO
|
595 BD_ENET_RX_CR
| BD_ENET_RX_OV
)) {
596 dev
->stats
.rx_errors
++;
597 if (status
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
)) {
598 /* Frame too long or too short. */
599 dev
->stats
.rx_length_errors
++;
601 if (status
& BD_ENET_RX_NO
) /* Frame alignment */
602 dev
->stats
.rx_frame_errors
++;
603 if (status
& BD_ENET_RX_CR
) /* CRC Error */
604 dev
->stats
.rx_crc_errors
++;
605 if (status
& BD_ENET_RX_OV
) /* FIFO overrun */
606 dev
->stats
.rx_fifo_errors
++;
609 /* Report late collisions as a frame error.
610 * On this error, the BD is closed, but we don't know what we
611 * have in the buffer. So, just drop this frame on the floor.
613 if (status
& BD_ENET_RX_CL
) {
614 dev
->stats
.rx_errors
++;
615 dev
->stats
.rx_frame_errors
++;
616 goto rx_processing_done
;
619 /* Process the incoming frame.
621 dev
->stats
.rx_packets
++;
622 pkt_len
= bdp
->cbd_datlen
;
623 dev
->stats
.rx_bytes
+= pkt_len
;
624 data
= (__u8
*)__va(bdp
->cbd_bufaddr
);
626 dma_sync_single(NULL
, (unsigned long)__pa(data
),
627 pkt_len
- 4, DMA_FROM_DEVICE
);
629 /* This does 16 byte alignment, exactly what we need.
630 * The packet length includes FCS, but we don't want to
631 * include that when passing upstream as it messes up
632 * bridging applications.
634 skb
= dev_alloc_skb(pkt_len
-4);
637 printk("%s: Memory squeeze, dropping packet.\n", dev
->name
);
638 dev
->stats
.rx_dropped
++;
640 skb_put(skb
,pkt_len
-4); /* Make room */
641 skb_copy_to_linear_data(skb
, data
, pkt_len
-4);
642 skb
->protocol
=eth_type_trans(skb
,dev
);
647 /* Clear the status flags for this buffer.
649 status
&= ~BD_ENET_RX_STATS
;
651 /* Mark the buffer empty.
653 status
|= BD_ENET_RX_EMPTY
;
654 bdp
->cbd_sc
= status
;
656 /* Update BD pointer to next entry.
658 if (status
& BD_ENET_RX_WRAP
)
659 bdp
= fep
->rx_bd_base
;
664 /* Doing this here will keep the FEC running while we process
665 * incoming frames. On a heavily loaded network, we should be
666 * able to keep up at the expense of system resources.
668 fecp
->fec_r_des_active
= 0;
670 } /* while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) */
671 fep
->cur_rx
= (cbd_t
*)bdp
;
674 /* Doing this here will allow us to process all frames in the
675 * ring before the FEC is allowed to put more there. On a heavily
676 * loaded network, some frames may be lost. Unfortunately, this
677 * increases the interrupt overhead since we can potentially work
678 * our way back to the interrupt return only to come right back
681 fecp
->fec_r_des_active
= 0;
684 spin_unlock_irq(&fep
->hw_lock
);
688 /* called from interrupt context */
690 fec_enet_mii(struct net_device
*dev
)
692 struct fec_enet_private
*fep
;
697 fep
= netdev_priv(dev
);
698 spin_lock_irq(&fep
->mii_lock
);
701 mii_reg
= ep
->fec_mii_data
;
703 if ((mip
= mii_head
) == NULL
) {
704 printk("MII and no head!\n");
708 if (mip
->mii_func
!= NULL
)
709 (*(mip
->mii_func
))(mii_reg
, dev
);
711 mii_head
= mip
->mii_next
;
712 mip
->mii_next
= mii_free
;
715 if ((mip
= mii_head
) != NULL
)
716 ep
->fec_mii_data
= mip
->mii_regval
;
719 spin_unlock_irq(&fep
->mii_lock
);
723 mii_queue(struct net_device
*dev
, int regval
, void (*func
)(uint
, struct net_device
*))
725 struct fec_enet_private
*fep
;
730 /* Add PHY address to register command.
732 fep
= netdev_priv(dev
);
733 spin_lock_irqsave(&fep
->mii_lock
, flags
);
735 regval
|= fep
->phy_addr
<< 23;
738 if ((mip
= mii_free
) != NULL
) {
739 mii_free
= mip
->mii_next
;
740 mip
->mii_regval
= regval
;
741 mip
->mii_func
= func
;
742 mip
->mii_next
= NULL
;
744 mii_tail
->mii_next
= mip
;
747 mii_head
= mii_tail
= mip
;
748 fep
->hwp
->fec_mii_data
= regval
;
754 spin_unlock_irqrestore(&fep
->mii_lock
, flags
);
758 static void mii_do_cmd(struct net_device
*dev
, const phy_cmd_t
*c
)
763 for (; c
->mii_data
!= mk_mii_end
; c
++)
764 mii_queue(dev
, c
->mii_data
, c
->funct
);
767 static void mii_parse_sr(uint mii_reg
, struct net_device
*dev
)
769 struct fec_enet_private
*fep
= netdev_priv(dev
);
770 volatile uint
*s
= &(fep
->phy_status
);
773 status
= *s
& ~(PHY_STAT_LINK
| PHY_STAT_FAULT
| PHY_STAT_ANC
);
775 if (mii_reg
& 0x0004)
776 status
|= PHY_STAT_LINK
;
777 if (mii_reg
& 0x0010)
778 status
|= PHY_STAT_FAULT
;
779 if (mii_reg
& 0x0020)
780 status
|= PHY_STAT_ANC
;
784 static void mii_parse_cr(uint mii_reg
, struct net_device
*dev
)
786 struct fec_enet_private
*fep
= netdev_priv(dev
);
787 volatile uint
*s
= &(fep
->phy_status
);
790 status
= *s
& ~(PHY_CONF_ANE
| PHY_CONF_LOOP
);
792 if (mii_reg
& 0x1000)
793 status
|= PHY_CONF_ANE
;
794 if (mii_reg
& 0x4000)
795 status
|= PHY_CONF_LOOP
;
799 static void mii_parse_anar(uint mii_reg
, struct net_device
*dev
)
801 struct fec_enet_private
*fep
= netdev_priv(dev
);
802 volatile uint
*s
= &(fep
->phy_status
);
805 status
= *s
& ~(PHY_CONF_SPMASK
);
807 if (mii_reg
& 0x0020)
808 status
|= PHY_CONF_10HDX
;
809 if (mii_reg
& 0x0040)
810 status
|= PHY_CONF_10FDX
;
811 if (mii_reg
& 0x0080)
812 status
|= PHY_CONF_100HDX
;
813 if (mii_reg
& 0x00100)
814 status
|= PHY_CONF_100FDX
;
818 /* ------------------------------------------------------------------------- */
819 /* The Level one LXT970 is used by many boards */
821 #define MII_LXT970_MIRROR 16 /* Mirror register */
822 #define MII_LXT970_IER 17 /* Interrupt Enable Register */
823 #define MII_LXT970_ISR 18 /* Interrupt Status Register */
824 #define MII_LXT970_CONFIG 19 /* Configuration Register */
825 #define MII_LXT970_CSR 20 /* Chip Status Register */
827 static void mii_parse_lxt970_csr(uint mii_reg
, struct net_device
*dev
)
829 struct fec_enet_private
*fep
= netdev_priv(dev
);
830 volatile uint
*s
= &(fep
->phy_status
);
833 status
= *s
& ~(PHY_STAT_SPMASK
);
834 if (mii_reg
& 0x0800) {
835 if (mii_reg
& 0x1000)
836 status
|= PHY_STAT_100FDX
;
838 status
|= PHY_STAT_100HDX
;
840 if (mii_reg
& 0x1000)
841 status
|= PHY_STAT_10FDX
;
843 status
|= PHY_STAT_10HDX
;
848 static phy_cmd_t
const phy_cmd_lxt970_config
[] = {
849 { mk_mii_read(MII_REG_CR
), mii_parse_cr
},
850 { mk_mii_read(MII_REG_ANAR
), mii_parse_anar
},
853 static phy_cmd_t
const phy_cmd_lxt970_startup
[] = { /* enable interrupts */
854 { mk_mii_write(MII_LXT970_IER
, 0x0002), NULL
},
855 { mk_mii_write(MII_REG_CR
, 0x1200), NULL
}, /* autonegotiate */
858 static phy_cmd_t
const phy_cmd_lxt970_ack_int
[] = {
859 /* read SR and ISR to acknowledge */
860 { mk_mii_read(MII_REG_SR
), mii_parse_sr
},
861 { mk_mii_read(MII_LXT970_ISR
), NULL
},
863 /* find out the current status */
864 { mk_mii_read(MII_LXT970_CSR
), mii_parse_lxt970_csr
},
867 static phy_cmd_t
const phy_cmd_lxt970_shutdown
[] = { /* disable interrupts */
868 { mk_mii_write(MII_LXT970_IER
, 0x0000), NULL
},
871 static phy_info_t
const phy_info_lxt970
= {
874 .config
= phy_cmd_lxt970_config
,
875 .startup
= phy_cmd_lxt970_startup
,
876 .ack_int
= phy_cmd_lxt970_ack_int
,
877 .shutdown
= phy_cmd_lxt970_shutdown
880 /* ------------------------------------------------------------------------- */
881 /* The Level one LXT971 is used on some of my custom boards */
883 /* register definitions for the 971 */
885 #define MII_LXT971_PCR 16 /* Port Control Register */
886 #define MII_LXT971_SR2 17 /* Status Register 2 */
887 #define MII_LXT971_IER 18 /* Interrupt Enable Register */
888 #define MII_LXT971_ISR 19 /* Interrupt Status Register */
889 #define MII_LXT971_LCR 20 /* LED Control Register */
890 #define MII_LXT971_TCR 30 /* Transmit Control Register */
893 * I had some nice ideas of running the MDIO faster...
894 * The 971 should support 8MHz and I tried it, but things acted really
895 * weird, so 2.5 MHz ought to be enough for anyone...
898 static void mii_parse_lxt971_sr2(uint mii_reg
, struct net_device
*dev
)
900 struct fec_enet_private
*fep
= netdev_priv(dev
);
901 volatile uint
*s
= &(fep
->phy_status
);
904 status
= *s
& ~(PHY_STAT_SPMASK
| PHY_STAT_LINK
| PHY_STAT_ANC
);
906 if (mii_reg
& 0x0400) {
908 status
|= PHY_STAT_LINK
;
912 if (mii_reg
& 0x0080)
913 status
|= PHY_STAT_ANC
;
914 if (mii_reg
& 0x4000) {
915 if (mii_reg
& 0x0200)
916 status
|= PHY_STAT_100FDX
;
918 status
|= PHY_STAT_100HDX
;
920 if (mii_reg
& 0x0200)
921 status
|= PHY_STAT_10FDX
;
923 status
|= PHY_STAT_10HDX
;
925 if (mii_reg
& 0x0008)
926 status
|= PHY_STAT_FAULT
;
931 static phy_cmd_t
const phy_cmd_lxt971_config
[] = {
932 /* limit to 10MBit because my prototype board
933 * doesn't work with 100. */
934 { mk_mii_read(MII_REG_CR
), mii_parse_cr
},
935 { mk_mii_read(MII_REG_ANAR
), mii_parse_anar
},
936 { mk_mii_read(MII_LXT971_SR2
), mii_parse_lxt971_sr2
},
939 static phy_cmd_t
const phy_cmd_lxt971_startup
[] = { /* enable interrupts */
940 { mk_mii_write(MII_LXT971_IER
, 0x00f2), NULL
},
941 { mk_mii_write(MII_REG_CR
, 0x1200), NULL
}, /* autonegotiate */
942 { mk_mii_write(MII_LXT971_LCR
, 0xd422), NULL
}, /* LED config */
943 /* Somehow does the 971 tell me that the link is down
944 * the first read after power-up.
945 * read here to get a valid value in ack_int */
946 { mk_mii_read(MII_REG_SR
), mii_parse_sr
},
949 static phy_cmd_t
const phy_cmd_lxt971_ack_int
[] = {
950 /* acknowledge the int before reading status ! */
951 { mk_mii_read(MII_LXT971_ISR
), NULL
},
952 /* find out the current status */
953 { mk_mii_read(MII_REG_SR
), mii_parse_sr
},
954 { mk_mii_read(MII_LXT971_SR2
), mii_parse_lxt971_sr2
},
957 static phy_cmd_t
const phy_cmd_lxt971_shutdown
[] = { /* disable interrupts */
958 { mk_mii_write(MII_LXT971_IER
, 0x0000), NULL
},
961 static phy_info_t
const phy_info_lxt971
= {
964 .config
= phy_cmd_lxt971_config
,
965 .startup
= phy_cmd_lxt971_startup
,
966 .ack_int
= phy_cmd_lxt971_ack_int
,
967 .shutdown
= phy_cmd_lxt971_shutdown
970 /* ------------------------------------------------------------------------- */
971 /* The Quality Semiconductor QS6612 is used on the RPX CLLF */
973 /* register definitions */
975 #define MII_QS6612_MCR 17 /* Mode Control Register */
976 #define MII_QS6612_FTR 27 /* Factory Test Register */
977 #define MII_QS6612_MCO 28 /* Misc. Control Register */
978 #define MII_QS6612_ISR 29 /* Interrupt Source Register */
979 #define MII_QS6612_IMR 30 /* Interrupt Mask Register */
980 #define MII_QS6612_PCR 31 /* 100BaseTx PHY Control Reg. */
982 static void mii_parse_qs6612_pcr(uint mii_reg
, struct net_device
*dev
)
984 struct fec_enet_private
*fep
= netdev_priv(dev
);
985 volatile uint
*s
= &(fep
->phy_status
);
988 status
= *s
& ~(PHY_STAT_SPMASK
);
990 switch((mii_reg
>> 2) & 7) {
991 case 1: status
|= PHY_STAT_10HDX
; break;
992 case 2: status
|= PHY_STAT_100HDX
; break;
993 case 5: status
|= PHY_STAT_10FDX
; break;
994 case 6: status
|= PHY_STAT_100FDX
; break;
1000 static phy_cmd_t
const phy_cmd_qs6612_config
[] = {
1001 /* The PHY powers up isolated on the RPX,
1002 * so send a command to allow operation.
1004 { mk_mii_write(MII_QS6612_PCR
, 0x0dc0), NULL
},
1006 /* parse cr and anar to get some info */
1007 { mk_mii_read(MII_REG_CR
), mii_parse_cr
},
1008 { mk_mii_read(MII_REG_ANAR
), mii_parse_anar
},
1011 static phy_cmd_t
const phy_cmd_qs6612_startup
[] = { /* enable interrupts */
1012 { mk_mii_write(MII_QS6612_IMR
, 0x003a), NULL
},
1013 { mk_mii_write(MII_REG_CR
, 0x1200), NULL
}, /* autonegotiate */
1016 static phy_cmd_t
const phy_cmd_qs6612_ack_int
[] = {
1017 /* we need to read ISR, SR and ANER to acknowledge */
1018 { mk_mii_read(MII_QS6612_ISR
), NULL
},
1019 { mk_mii_read(MII_REG_SR
), mii_parse_sr
},
1020 { mk_mii_read(MII_REG_ANER
), NULL
},
1022 /* read pcr to get info */
1023 { mk_mii_read(MII_QS6612_PCR
), mii_parse_qs6612_pcr
},
1026 static phy_cmd_t
const phy_cmd_qs6612_shutdown
[] = { /* disable interrupts */
1027 { mk_mii_write(MII_QS6612_IMR
, 0x0000), NULL
},
1030 static phy_info_t
const phy_info_qs6612
= {
1033 .config
= phy_cmd_qs6612_config
,
1034 .startup
= phy_cmd_qs6612_startup
,
1035 .ack_int
= phy_cmd_qs6612_ack_int
,
1036 .shutdown
= phy_cmd_qs6612_shutdown
1039 /* ------------------------------------------------------------------------- */
1040 /* AMD AM79C874 phy */
1042 /* register definitions for the 874 */
1044 #define MII_AM79C874_MFR 16 /* Miscellaneous Feature Register */
1045 #define MII_AM79C874_ICSR 17 /* Interrupt/Status Register */
1046 #define MII_AM79C874_DR 18 /* Diagnostic Register */
1047 #define MII_AM79C874_PMLR 19 /* Power and Loopback Register */
1048 #define MII_AM79C874_MCR 21 /* ModeControl Register */
1049 #define MII_AM79C874_DC 23 /* Disconnect Counter */
1050 #define MII_AM79C874_REC 24 /* Recieve Error Counter */
1052 static void mii_parse_am79c874_dr(uint mii_reg
, struct net_device
*dev
)
1054 struct fec_enet_private
*fep
= netdev_priv(dev
);
1055 volatile uint
*s
= &(fep
->phy_status
);
1058 status
= *s
& ~(PHY_STAT_SPMASK
| PHY_STAT_ANC
);
1060 if (mii_reg
& 0x0080)
1061 status
|= PHY_STAT_ANC
;
1062 if (mii_reg
& 0x0400)
1063 status
|= ((mii_reg
& 0x0800) ? PHY_STAT_100FDX
: PHY_STAT_100HDX
);
1065 status
|= ((mii_reg
& 0x0800) ? PHY_STAT_10FDX
: PHY_STAT_10HDX
);
1070 static phy_cmd_t
const phy_cmd_am79c874_config
[] = {
1071 { mk_mii_read(MII_REG_CR
), mii_parse_cr
},
1072 { mk_mii_read(MII_REG_ANAR
), mii_parse_anar
},
1073 { mk_mii_read(MII_AM79C874_DR
), mii_parse_am79c874_dr
},
1076 static phy_cmd_t
const phy_cmd_am79c874_startup
[] = { /* enable interrupts */
1077 { mk_mii_write(MII_AM79C874_ICSR
, 0xff00), NULL
},
1078 { mk_mii_write(MII_REG_CR
, 0x1200), NULL
}, /* autonegotiate */
1079 { mk_mii_read(MII_REG_SR
), mii_parse_sr
},
1082 static phy_cmd_t
const phy_cmd_am79c874_ack_int
[] = {
1083 /* find out the current status */
1084 { mk_mii_read(MII_REG_SR
), mii_parse_sr
},
1085 { mk_mii_read(MII_AM79C874_DR
), mii_parse_am79c874_dr
},
1086 /* we only need to read ISR to acknowledge */
1087 { mk_mii_read(MII_AM79C874_ICSR
), NULL
},
1090 static phy_cmd_t
const phy_cmd_am79c874_shutdown
[] = { /* disable interrupts */
1091 { mk_mii_write(MII_AM79C874_ICSR
, 0x0000), NULL
},
1094 static phy_info_t
const phy_info_am79c874
= {
1097 .config
= phy_cmd_am79c874_config
,
1098 .startup
= phy_cmd_am79c874_startup
,
1099 .ack_int
= phy_cmd_am79c874_ack_int
,
1100 .shutdown
= phy_cmd_am79c874_shutdown
1104 /* ------------------------------------------------------------------------- */
1105 /* Kendin KS8721BL phy */
1107 /* register definitions for the 8721 */
1109 #define MII_KS8721BL_RXERCR 21
1110 #define MII_KS8721BL_ICSR 27
1111 #define MII_KS8721BL_PHYCR 31
1113 static phy_cmd_t
const phy_cmd_ks8721bl_config
[] = {
1114 { mk_mii_read(MII_REG_CR
), mii_parse_cr
},
1115 { mk_mii_read(MII_REG_ANAR
), mii_parse_anar
},
1118 static phy_cmd_t
const phy_cmd_ks8721bl_startup
[] = { /* enable interrupts */
1119 { mk_mii_write(MII_KS8721BL_ICSR
, 0xff00), NULL
},
1120 { mk_mii_write(MII_REG_CR
, 0x1200), NULL
}, /* autonegotiate */
1121 { mk_mii_read(MII_REG_SR
), mii_parse_sr
},
1124 static phy_cmd_t
const phy_cmd_ks8721bl_ack_int
[] = {
1125 /* find out the current status */
1126 { mk_mii_read(MII_REG_SR
), mii_parse_sr
},
1127 /* we only need to read ISR to acknowledge */
1128 { mk_mii_read(MII_KS8721BL_ICSR
), NULL
},
1131 static phy_cmd_t
const phy_cmd_ks8721bl_shutdown
[] = { /* disable interrupts */
1132 { mk_mii_write(MII_KS8721BL_ICSR
, 0x0000), NULL
},
1135 static phy_info_t
const phy_info_ks8721bl
= {
1138 .config
= phy_cmd_ks8721bl_config
,
1139 .startup
= phy_cmd_ks8721bl_startup
,
1140 .ack_int
= phy_cmd_ks8721bl_ack_int
,
1141 .shutdown
= phy_cmd_ks8721bl_shutdown
1144 /* ------------------------------------------------------------------------- */
1145 /* register definitions for the DP83848 */
1147 #define MII_DP8384X_PHYSTST 16 /* PHY Status Register */
1149 static void mii_parse_dp8384x_sr2(uint mii_reg
, struct net_device
*dev
)
1151 struct fec_enet_private
*fep
= netdev_priv(dev
);
1152 volatile uint
*s
= &(fep
->phy_status
);
1154 *s
&= ~(PHY_STAT_SPMASK
| PHY_STAT_LINK
| PHY_STAT_ANC
);
1157 if (mii_reg
& 0x0001) {
1159 *s
|= PHY_STAT_LINK
;
1162 /* Status of link */
1163 if (mii_reg
& 0x0010) /* Autonegotioation complete */
1165 if (mii_reg
& 0x0002) { /* 10MBps? */
1166 if (mii_reg
& 0x0004) /* Full Duplex? */
1167 *s
|= PHY_STAT_10FDX
;
1169 *s
|= PHY_STAT_10HDX
;
1170 } else { /* 100 Mbps? */
1171 if (mii_reg
& 0x0004) /* Full Duplex? */
1172 *s
|= PHY_STAT_100FDX
;
1174 *s
|= PHY_STAT_100HDX
;
1176 if (mii_reg
& 0x0008)
1177 *s
|= PHY_STAT_FAULT
;
1180 static phy_info_t phy_info_dp83848
= {
1184 (const phy_cmd_t
[]) { /* config */
1185 { mk_mii_read(MII_REG_CR
), mii_parse_cr
},
1186 { mk_mii_read(MII_REG_ANAR
), mii_parse_anar
},
1187 { mk_mii_read(MII_DP8384X_PHYSTST
), mii_parse_dp8384x_sr2
},
1190 (const phy_cmd_t
[]) { /* startup - enable interrupts */
1191 { mk_mii_write(MII_REG_CR
, 0x1200), NULL
}, /* autonegotiate */
1192 { mk_mii_read(MII_REG_SR
), mii_parse_sr
},
1195 (const phy_cmd_t
[]) { /* ack_int - never happens, no interrupt */
1198 (const phy_cmd_t
[]) { /* shutdown */
1203 /* ------------------------------------------------------------------------- */
1205 static phy_info_t
const * const phy_info
[] = {
1215 /* ------------------------------------------------------------------------- */
1216 #ifdef HAVE_mii_link_interrupt
1218 mii_link_interrupt(int irq
, void * dev_id
);
1221 * This is specific to the MII interrupt setup of the M5272EVB.
1223 static void __inline__
fec_request_mii_intr(struct net_device
*dev
)
1225 if (request_irq(66, mii_link_interrupt
, IRQF_DISABLED
, "fec(MII)", dev
) != 0)
1226 printk("FEC: Could not allocate fec(MII) IRQ(66)!\n");
1229 static void __inline__
fec_disable_phy_intr(void)
1231 volatile unsigned long *icrp
;
1232 icrp
= (volatile unsigned long *) (MCF_MBAR
+ MCFSIM_ICR1
);
1236 static void __inline__
fec_phy_ack_intr(void)
1238 volatile unsigned long *icrp
;
1239 /* Acknowledge the interrupt */
1240 icrp
= (volatile unsigned long *) (MCF_MBAR
+ MCFSIM_ICR1
);
1246 static void __inline__
fec_get_mac(struct net_device
*dev
)
1248 struct fec_enet_private
*fep
= netdev_priv(dev
);
1249 volatile fec_t
*fecp
;
1250 unsigned char *iap
, tmpaddr
[ETH_ALEN
];
1256 * Get MAC address from FLASH.
1257 * If it is all 1's or 0's, use the default.
1259 iap
= (unsigned char *)FEC_FLASHMAC
;
1260 if ((iap
[0] == 0) && (iap
[1] == 0) && (iap
[2] == 0) &&
1261 (iap
[3] == 0) && (iap
[4] == 0) && (iap
[5] == 0))
1262 iap
= fec_mac_default
;
1263 if ((iap
[0] == 0xff) && (iap
[1] == 0xff) && (iap
[2] == 0xff) &&
1264 (iap
[3] == 0xff) && (iap
[4] == 0xff) && (iap
[5] == 0xff))
1265 iap
= fec_mac_default
;
1267 *((unsigned long *) &tmpaddr
[0]) = fecp
->fec_addr_low
;
1268 *((unsigned short *) &tmpaddr
[4]) = (fecp
->fec_addr_high
>> 16);
1272 memcpy(dev
->dev_addr
, iap
, ETH_ALEN
);
1274 /* Adjust MAC if using default MAC address */
1275 if (iap
== fec_mac_default
)
1276 dev
->dev_addr
[ETH_ALEN
-1] = fec_mac_default
[ETH_ALEN
-1] + fep
->index
;
1280 /* ------------------------------------------------------------------------- */
1282 static void mii_display_status(struct net_device
*dev
)
1284 struct fec_enet_private
*fep
= netdev_priv(dev
);
1285 volatile uint
*s
= &(fep
->phy_status
);
1287 if (!fep
->link
&& !fep
->old_link
) {
1288 /* Link is still down - don't print anything */
1292 printk("%s: status: ", dev
->name
);
1295 printk("link down");
1299 switch(*s
& PHY_STAT_SPMASK
) {
1300 case PHY_STAT_100FDX
: printk(", 100MBit Full Duplex"); break;
1301 case PHY_STAT_100HDX
: printk(", 100MBit Half Duplex"); break;
1302 case PHY_STAT_10FDX
: printk(", 10MBit Full Duplex"); break;
1303 case PHY_STAT_10HDX
: printk(", 10MBit Half Duplex"); break;
1305 printk(", Unknown speed/duplex");
1308 if (*s
& PHY_STAT_ANC
)
1309 printk(", auto-negotiation complete");
1312 if (*s
& PHY_STAT_FAULT
)
1313 printk(", remote fault");
1318 static void mii_display_config(struct work_struct
*work
)
1320 struct fec_enet_private
*fep
= container_of(work
, struct fec_enet_private
, phy_task
);
1321 struct net_device
*dev
= fep
->netdev
;
1322 uint status
= fep
->phy_status
;
1325 ** When we get here, phy_task is already removed from
1326 ** the workqueue. It is thus safe to allow to reuse it.
1328 fep
->mii_phy_task_queued
= 0;
1329 printk("%s: config: auto-negotiation ", dev
->name
);
1331 if (status
& PHY_CONF_ANE
)
1336 if (status
& PHY_CONF_100FDX
)
1338 if (status
& PHY_CONF_100HDX
)
1340 if (status
& PHY_CONF_10FDX
)
1342 if (status
& PHY_CONF_10HDX
)
1344 if (!(status
& PHY_CONF_SPMASK
))
1345 printk(", No speed/duplex selected?");
1347 if (status
& PHY_CONF_LOOP
)
1348 printk(", loopback enabled");
1352 fep
->sequence_done
= 1;
1355 static void mii_relink(struct work_struct
*work
)
1357 struct fec_enet_private
*fep
= container_of(work
, struct fec_enet_private
, phy_task
);
1358 struct net_device
*dev
= fep
->netdev
;
1362 ** When we get here, phy_task is already removed from
1363 ** the workqueue. It is thus safe to allow to reuse it.
1365 fep
->mii_phy_task_queued
= 0;
1366 fep
->link
= (fep
->phy_status
& PHY_STAT_LINK
) ? 1 : 0;
1367 mii_display_status(dev
);
1368 fep
->old_link
= fep
->link
;
1373 & (PHY_STAT_100FDX
| PHY_STAT_10FDX
))
1375 fec_restart(dev
, duplex
);
1380 enable_irq(fep
->mii_irq
);
1385 /* mii_queue_relink is called in interrupt context from mii_link_interrupt */
1386 static void mii_queue_relink(uint mii_reg
, struct net_device
*dev
)
1388 struct fec_enet_private
*fep
= netdev_priv(dev
);
1391 ** We cannot queue phy_task twice in the workqueue. It
1392 ** would cause an endless loop in the workqueue.
1393 ** Fortunately, if the last mii_relink entry has not yet been
1394 ** executed now, it will do the job for the current interrupt,
1395 ** which is just what we want.
1397 if (fep
->mii_phy_task_queued
)
1400 fep
->mii_phy_task_queued
= 1;
1401 INIT_WORK(&fep
->phy_task
, mii_relink
);
1402 schedule_work(&fep
->phy_task
);
1405 /* mii_queue_config is called in interrupt context from fec_enet_mii */
1406 static void mii_queue_config(uint mii_reg
, struct net_device
*dev
)
1408 struct fec_enet_private
*fep
= netdev_priv(dev
);
1410 if (fep
->mii_phy_task_queued
)
1413 fep
->mii_phy_task_queued
= 1;
1414 INIT_WORK(&fep
->phy_task
, mii_display_config
);
1415 schedule_work(&fep
->phy_task
);
1418 phy_cmd_t
const phy_cmd_relink
[] = {
1419 { mk_mii_read(MII_REG_CR
), mii_queue_relink
},
1422 phy_cmd_t
const phy_cmd_config
[] = {
1423 { mk_mii_read(MII_REG_CR
), mii_queue_config
},
1427 /* Read remainder of PHY ID.
1430 mii_discover_phy3(uint mii_reg
, struct net_device
*dev
)
1432 struct fec_enet_private
*fep
;
1435 fep
= netdev_priv(dev
);
1436 fep
->phy_id
|= (mii_reg
& 0xffff);
1437 printk("fec: PHY @ 0x%x, ID 0x%08x", fep
->phy_addr
, fep
->phy_id
);
1439 for(i
= 0; phy_info
[i
]; i
++) {
1440 if(phy_info
[i
]->id
== (fep
->phy_id
>> 4))
1445 printk(" -- %s\n", phy_info
[i
]->name
);
1447 printk(" -- unknown PHY!\n");
1449 fep
->phy
= phy_info
[i
];
1450 fep
->phy_id_done
= 1;
1453 /* Scan all of the MII PHY addresses looking for someone to respond
1454 * with a valid ID. This usually happens quickly.
1457 mii_discover_phy(uint mii_reg
, struct net_device
*dev
)
1459 struct fec_enet_private
*fep
;
1460 volatile fec_t
*fecp
;
1463 fep
= netdev_priv(dev
);
1466 if (fep
->phy_addr
< 32) {
1467 if ((phytype
= (mii_reg
& 0xffff)) != 0xffff && phytype
!= 0) {
1469 /* Got first part of ID, now get remainder.
1471 fep
->phy_id
= phytype
<< 16;
1472 mii_queue(dev
, mk_mii_read(MII_REG_PHYIR2
),
1476 mii_queue(dev
, mk_mii_read(MII_REG_PHYIR1
),
1480 printk("FEC: No PHY device found.\n");
1481 /* Disable external MII interface */
1482 fecp
->fec_mii_speed
= fep
->phy_speed
= 0;
1483 #ifdef HAVE_mii_link_interrupt
1484 fec_disable_phy_intr();
1489 /* This interrupt occurs when the PHY detects a link change.
1491 #ifdef HAVE_mii_link_interrupt
1493 mii_link_interrupt(int irq
, void * dev_id
)
1495 struct net_device
*dev
= dev_id
;
1496 struct fec_enet_private
*fep
= netdev_priv(dev
);
1501 disable_irq(fep
->mii_irq
); /* disable now, enable later */
1504 mii_do_cmd(dev
, fep
->phy
->ack_int
);
1505 mii_do_cmd(dev
, phy_cmd_relink
); /* restart and display status */
1512 fec_enet_open(struct net_device
*dev
)
1514 struct fec_enet_private
*fep
= netdev_priv(dev
);
1516 /* I should reset the ring buffers here, but I don't yet know
1517 * a simple way to do that.
1519 fec_set_mac_address(dev
);
1521 fep
->sequence_done
= 0;
1525 mii_do_cmd(dev
, fep
->phy
->ack_int
);
1526 mii_do_cmd(dev
, fep
->phy
->config
);
1527 mii_do_cmd(dev
, phy_cmd_config
); /* display configuration */
1529 /* Poll until the PHY tells us its configuration
1531 * Request is initiated by mii_do_cmd above, but answer
1532 * comes by interrupt.
1533 * This should take about 25 usec per register at 2.5 MHz,
1534 * and we read approximately 5 registers.
1536 while(!fep
->sequence_done
)
1539 mii_do_cmd(dev
, fep
->phy
->startup
);
1541 /* Set the initial link state to true. A lot of hardware
1542 * based on this device does not implement a PHY interrupt,
1543 * so we are never notified of link change.
1547 fep
->link
= 1; /* lets just try it and see */
1548 /* no phy, go full duplex, it's most likely a hub chip */
1549 fec_restart(dev
, 1);
1552 netif_start_queue(dev
);
1554 return 0; /* Success */
1558 fec_enet_close(struct net_device
*dev
)
1560 struct fec_enet_private
*fep
= netdev_priv(dev
);
1562 /* Don't know what to do yet.
1565 netif_stop_queue(dev
);
1571 /* Set or clear the multicast filter for this adaptor.
1572 * Skeleton taken from sunlance driver.
1573 * The CPM Ethernet implementation allows Multicast as well as individual
1574 * MAC address filtering. Some of the drivers check to make sure it is
1575 * a group multicast address, and discard those that are not. I guess I
1576 * will do the same for now, but just remove the test if you want
1577 * individual filtering as well (do the upper net layers want or support
1578 * this kind of feature?).
1581 #define HASH_BITS 6 /* #bits in hash */
1582 #define CRC32_POLY 0xEDB88320
1584 static void set_multicast_list(struct net_device
*dev
)
1586 struct fec_enet_private
*fep
;
1588 struct dev_mc_list
*dmi
;
1589 unsigned int i
, j
, bit
, data
, crc
;
1592 fep
= netdev_priv(dev
);
1595 if (dev
->flags
&IFF_PROMISC
) {
1596 ep
->fec_r_cntrl
|= 0x0008;
1599 ep
->fec_r_cntrl
&= ~0x0008;
1601 if (dev
->flags
& IFF_ALLMULTI
) {
1602 /* Catch all multicast addresses, so set the
1603 * filter to all 1's.
1605 ep
->fec_grp_hash_table_high
= 0xffffffff;
1606 ep
->fec_grp_hash_table_low
= 0xffffffff;
1608 /* Clear filter and add the addresses in hash register.
1610 ep
->fec_grp_hash_table_high
= 0;
1611 ep
->fec_grp_hash_table_low
= 0;
1615 for (j
= 0; j
< dev
->mc_count
; j
++, dmi
= dmi
->next
)
1617 /* Only support group multicast for now.
1619 if (!(dmi
->dmi_addr
[0] & 1))
1622 /* calculate crc32 value of mac address
1626 for (i
= 0; i
< dmi
->dmi_addrlen
; i
++)
1628 data
= dmi
->dmi_addr
[i
];
1629 for (bit
= 0; bit
< 8; bit
++, data
>>= 1)
1632 (((crc
^ data
) & 1) ? CRC32_POLY
: 0);
1636 /* only upper 6 bits (HASH_BITS) are used
1637 which point to specific bit in he hash registers
1639 hash
= (crc
>> (32 - HASH_BITS
)) & 0x3f;
1642 ep
->fec_grp_hash_table_high
|= 1 << (hash
- 32);
1644 ep
->fec_grp_hash_table_low
|= 1 << hash
;
1650 /* Set a MAC change in hardware.
1653 fec_set_mac_address(struct net_device
*dev
)
1655 volatile fec_t
*fecp
;
1657 fecp
= ((struct fec_enet_private
*)netdev_priv(dev
))->hwp
;
1659 /* Set station address. */
1660 fecp
->fec_addr_low
= dev
->dev_addr
[3] | (dev
->dev_addr
[2] << 8) |
1661 (dev
->dev_addr
[1] << 16) | (dev
->dev_addr
[0] << 24);
1662 fecp
->fec_addr_high
= (dev
->dev_addr
[5] << 16) |
1663 (dev
->dev_addr
[4] << 24);
1668 * XXX: We need to clean up on failure exits here.
1670 * index is only used in legacy code
1672 int __init
fec_enet_init(struct net_device
*dev
, int index
)
1674 struct fec_enet_private
*fep
= netdev_priv(dev
);
1675 unsigned long mem_addr
;
1676 volatile cbd_t
*bdp
;
1678 volatile fec_t
*fecp
;
1681 /* Allocate memory for buffer descriptors.
1683 mem_addr
= (unsigned long)dma_alloc_coherent(NULL
, PAGE_SIZE
,
1684 &fep
->bd_dma
, GFP_KERNEL
);
1685 if (mem_addr
== 0) {
1686 printk("FEC: allocate descriptor memory failed?\n");
1690 spin_lock_init(&fep
->hw_lock
);
1691 spin_lock_init(&fep
->mii_lock
);
1693 /* Create an Ethernet device instance.
1695 fecp
= (volatile fec_t
*)dev
->base_addr
;
1701 /* Whack a reset. We should wait for this.
1703 fecp
->fec_ecntrl
= 1;
1706 /* Set the Ethernet address */
1712 l
= fecp
->fec_addr_low
;
1713 dev
->dev_addr
[0] = (unsigned char)((l
& 0xFF000000) >> 24);
1714 dev
->dev_addr
[1] = (unsigned char)((l
& 0x00FF0000) >> 16);
1715 dev
->dev_addr
[2] = (unsigned char)((l
& 0x0000FF00) >> 8);
1716 dev
->dev_addr
[3] = (unsigned char)((l
& 0x000000FF) >> 0);
1717 l
= fecp
->fec_addr_high
;
1718 dev
->dev_addr
[4] = (unsigned char)((l
& 0xFF000000) >> 24);
1719 dev
->dev_addr
[5] = (unsigned char)((l
& 0x00FF0000) >> 16);
1723 cbd_base
= (cbd_t
*)mem_addr
;
1725 /* Set receive and transmit descriptor base.
1727 fep
->rx_bd_base
= cbd_base
;
1728 fep
->tx_bd_base
= cbd_base
+ RX_RING_SIZE
;
1730 fep
->dirty_tx
= fep
->cur_tx
= fep
->tx_bd_base
;
1731 fep
->cur_rx
= fep
->rx_bd_base
;
1733 fep
->skb_cur
= fep
->skb_dirty
= 0;
1735 /* Initialize the receive buffer descriptors.
1737 bdp
= fep
->rx_bd_base
;
1738 for (i
=0; i
<FEC_ENET_RX_PAGES
; i
++) {
1742 mem_addr
= __get_free_page(GFP_KERNEL
);
1743 /* XXX: missing check for allocation failure */
1745 /* Initialize the BD for every fragment in the page.
1747 for (j
=0; j
<FEC_ENET_RX_FRPPG
; j
++) {
1748 bdp
->cbd_sc
= BD_ENET_RX_EMPTY
;
1749 bdp
->cbd_bufaddr
= __pa(mem_addr
);
1750 mem_addr
+= FEC_ENET_RX_FRSIZE
;
1755 /* Set the last buffer to wrap.
1758 bdp
->cbd_sc
|= BD_SC_WRAP
;
1760 /* ...and the same for transmmit.
1762 bdp
= fep
->tx_bd_base
;
1763 for (i
=0, j
=FEC_ENET_TX_FRPPG
; i
<TX_RING_SIZE
; i
++) {
1764 if (j
>= FEC_ENET_TX_FRPPG
) {
1765 mem_addr
= __get_free_page(GFP_KERNEL
);
1768 mem_addr
+= FEC_ENET_TX_FRSIZE
;
1771 fep
->tx_bounce
[i
] = (unsigned char *) mem_addr
;
1773 /* Initialize the BD for every fragment in the page.
1776 bdp
->cbd_bufaddr
= 0;
1780 /* Set the last buffer to wrap.
1783 bdp
->cbd_sc
|= BD_SC_WRAP
;
1785 /* Set receive and transmit descriptor base.
1787 fecp
->fec_r_des_start
= fep
->bd_dma
;
1788 fecp
->fec_x_des_start
= (unsigned long)fep
->bd_dma
+ sizeof(cbd_t
)
1791 #ifdef HAVE_mii_link_interrupt
1792 fec_request_mii_intr(dev
);
1795 fecp
->fec_grp_hash_table_high
= 0;
1796 fecp
->fec_grp_hash_table_low
= 0;
1797 fecp
->fec_r_buff_size
= PKT_MAXBLR_SIZE
;
1798 fecp
->fec_ecntrl
= 2;
1799 fecp
->fec_r_des_active
= 0;
1800 #ifndef CONFIG_M5272
1801 fecp
->fec_hash_table_high
= 0;
1802 fecp
->fec_hash_table_low
= 0;
1805 /* The FEC Ethernet specific entries in the device structure. */
1806 dev
->open
= fec_enet_open
;
1807 dev
->hard_start_xmit
= fec_enet_start_xmit
;
1808 dev
->tx_timeout
= fec_timeout
;
1809 dev
->watchdog_timeo
= TX_TIMEOUT
;
1810 dev
->stop
= fec_enet_close
;
1811 dev
->set_multicast_list
= set_multicast_list
;
1813 for (i
=0; i
<NMII
-1; i
++)
1814 mii_cmds
[i
].mii_next
= &mii_cmds
[i
+1];
1815 mii_free
= mii_cmds
;
1817 /* setup MII interface */
1818 fecp
->fec_r_cntrl
= OPT_FRAME_SIZE
| 0x04;
1819 fecp
->fec_x_cntrl
= 0x00;
1822 * Set MII speed to 2.5 MHz
1824 fep
->phy_speed
= ((((clk_get_rate(fep
->clk
) / 2 + 4999999)
1825 / 2500000) / 2) & 0x3F) << 1;
1826 fecp
->fec_mii_speed
= fep
->phy_speed
;
1827 fec_restart(dev
, 0);
1829 /* Clear and enable interrupts */
1830 fecp
->fec_ievent
= 0xffc00000;
1831 fecp
->fec_imask
= (FEC_ENET_TXF
| FEC_ENET_RXF
| FEC_ENET_MII
);
1833 /* Queue up command to detect the PHY and initialize the
1834 * remainder of the interface.
1836 fep
->phy_id_done
= 0;
1838 mii_queue(dev
, mk_mii_read(MII_REG_PHYIR1
), mii_discover_phy
);
1843 /* This function is called to start or restart the FEC during a link
1844 * change. This only happens when switching between half and full
1848 fec_restart(struct net_device
*dev
, int duplex
)
1850 struct fec_enet_private
*fep
;
1851 volatile cbd_t
*bdp
;
1852 volatile fec_t
*fecp
;
1855 fep
= netdev_priv(dev
);
1858 /* Whack a reset. We should wait for this.
1860 fecp
->fec_ecntrl
= 1;
1863 /* Clear any outstanding interrupt.
1865 fecp
->fec_ievent
= 0xffc00000;
1867 /* Set station address.
1869 fec_set_mac_address(dev
);
1871 /* Reset all multicast.
1873 fecp
->fec_grp_hash_table_high
= 0;
1874 fecp
->fec_grp_hash_table_low
= 0;
1876 /* Set maximum receive buffer size.
1878 fecp
->fec_r_buff_size
= PKT_MAXBLR_SIZE
;
1880 /* Set receive and transmit descriptor base.
1882 fecp
->fec_r_des_start
= fep
->bd_dma
;
1883 fecp
->fec_x_des_start
= (unsigned long)fep
->bd_dma
+ sizeof(cbd_t
)
1886 fep
->dirty_tx
= fep
->cur_tx
= fep
->tx_bd_base
;
1887 fep
->cur_rx
= fep
->rx_bd_base
;
1889 /* Reset SKB transmit buffers.
1891 fep
->skb_cur
= fep
->skb_dirty
= 0;
1892 for (i
=0; i
<=TX_RING_MOD_MASK
; i
++) {
1893 if (fep
->tx_skbuff
[i
] != NULL
) {
1894 dev_kfree_skb_any(fep
->tx_skbuff
[i
]);
1895 fep
->tx_skbuff
[i
] = NULL
;
1899 /* Initialize the receive buffer descriptors.
1901 bdp
= fep
->rx_bd_base
;
1902 for (i
=0; i
<RX_RING_SIZE
; i
++) {
1904 /* Initialize the BD for every fragment in the page.
1906 bdp
->cbd_sc
= BD_ENET_RX_EMPTY
;
1910 /* Set the last buffer to wrap.
1913 bdp
->cbd_sc
|= BD_SC_WRAP
;
1915 /* ...and the same for transmmit.
1917 bdp
= fep
->tx_bd_base
;
1918 for (i
=0; i
<TX_RING_SIZE
; i
++) {
1920 /* Initialize the BD for every fragment in the page.
1923 bdp
->cbd_bufaddr
= 0;
1927 /* Set the last buffer to wrap.
1930 bdp
->cbd_sc
|= BD_SC_WRAP
;
1935 fecp
->fec_r_cntrl
= OPT_FRAME_SIZE
| 0x04;/* MII enable */
1936 fecp
->fec_x_cntrl
= 0x04; /* FD enable */
1938 /* MII enable|No Rcv on Xmit */
1939 fecp
->fec_r_cntrl
= OPT_FRAME_SIZE
| 0x06;
1940 fecp
->fec_x_cntrl
= 0x00;
1942 fep
->full_duplex
= duplex
;
1946 fecp
->fec_mii_speed
= fep
->phy_speed
;
1948 /* And last, enable the transmit and receive processing.
1950 fecp
->fec_ecntrl
= 2;
1951 fecp
->fec_r_des_active
= 0;
1953 /* Enable interrupts we wish to service.
1955 fecp
->fec_imask
= (FEC_ENET_TXF
| FEC_ENET_RXF
| FEC_ENET_MII
);
1959 fec_stop(struct net_device
*dev
)
1961 volatile fec_t
*fecp
;
1962 struct fec_enet_private
*fep
;
1964 fep
= netdev_priv(dev
);
1968 ** We cannot expect a graceful transmit stop without link !!!
1972 fecp
->fec_x_cntrl
= 0x01; /* Graceful transmit stop */
1974 if (!(fecp
->fec_ievent
& FEC_ENET_GRA
))
1975 printk("fec_stop : Graceful transmit stop did not complete !\n");
1978 /* Whack a reset. We should wait for this.
1980 fecp
->fec_ecntrl
= 1;
1983 /* Clear outstanding MII command interrupts.
1985 fecp
->fec_ievent
= FEC_ENET_MII
;
1987 fecp
->fec_imask
= FEC_ENET_MII
;
1988 fecp
->fec_mii_speed
= fep
->phy_speed
;
1991 static int __devinit
1992 fec_probe(struct platform_device
*pdev
)
1994 struct fec_enet_private
*fep
;
1995 struct net_device
*ndev
;
1996 int i
, irq
, ret
= 0;
1999 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2003 r
= request_mem_region(r
->start
, resource_size(r
), pdev
->name
);
2007 /* Init network device */
2008 ndev
= alloc_etherdev(sizeof(struct fec_enet_private
));
2012 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
2014 /* setup board info structure */
2015 fep
= netdev_priv(ndev
);
2016 memset(fep
, 0, sizeof(*fep
));
2018 ndev
->base_addr
= (unsigned long)ioremap(r
->start
, resource_size(r
));
2020 if (!ndev
->base_addr
) {
2022 goto failed_ioremap
;
2025 platform_set_drvdata(pdev
, ndev
);
2027 /* This device has up to three irqs on some platforms */
2028 for (i
= 0; i
< 3; i
++) {
2029 irq
= platform_get_irq(pdev
, i
);
2032 ret
= request_irq(irq
, fec_enet_interrupt
, IRQF_DISABLED
, pdev
->name
, ndev
);
2035 irq
= platform_get_irq(pdev
, i
);
2036 free_irq(irq
, ndev
);
2043 fep
->clk
= clk_get(&pdev
->dev
, "fec_clk");
2044 if (IS_ERR(fep
->clk
)) {
2045 ret
= PTR_ERR(fep
->clk
);
2048 clk_enable(fep
->clk
);
2050 ret
= fec_enet_init(ndev
, 0);
2054 ret
= register_netdev(ndev
);
2056 goto failed_register
;
2062 clk_disable(fep
->clk
);
2065 for (i
= 0; i
< 3; i
++) {
2066 irq
= platform_get_irq(pdev
, i
);
2068 free_irq(irq
, ndev
);
2071 iounmap((void __iomem
*)ndev
->base_addr
);
2078 static int __devexit
2079 fec_drv_remove(struct platform_device
*pdev
)
2081 struct net_device
*ndev
= platform_get_drvdata(pdev
);
2082 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2084 platform_set_drvdata(pdev
, NULL
);
2087 clk_disable(fep
->clk
);
2089 iounmap((void __iomem
*)ndev
->base_addr
);
2090 unregister_netdev(ndev
);
2096 fec_suspend(struct platform_device
*dev
, pm_message_t state
)
2098 struct net_device
*ndev
= platform_get_drvdata(dev
);
2099 struct fec_enet_private
*fep
;
2102 fep
= netdev_priv(ndev
);
2103 if (netif_running(ndev
)) {
2104 netif_device_detach(ndev
);
2112 fec_resume(struct platform_device
*dev
)
2114 struct net_device
*ndev
= platform_get_drvdata(dev
);
2117 if (netif_running(ndev
)) {
2118 fec_enet_init(ndev
, 0);
2119 netif_device_attach(ndev
);
2125 static struct platform_driver fec_driver
= {
2128 .owner
= THIS_MODULE
,
2131 .remove
= __devexit_p(fec_drv_remove
),
2132 .suspend
= fec_suspend
,
2133 .resume
= fec_resume
,
2137 fec_enet_module_init(void)
2139 printk(KERN_INFO
"FEC Ethernet Driver\n");
2141 return platform_driver_register(&fec_driver
);
2145 fec_enet_cleanup(void)
2147 platform_driver_unregister(&fec_driver
);
2150 module_exit(fec_enet_cleanup
);
2151 module_init(fec_enet_module_init
);
2153 MODULE_LICENSE("GPL");