blob: f8396fb3032690bb63877734151a409053e5d5a5 [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 *
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10005 * Right now, I am very wasteful with the buffers. I allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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
10 * small packets.
11 *
12 * Much better multiple PHY support by Magnus Damm.
13 * Copyright (c) 2000 Ericsson Radio Systems AB.
14 *
Greg Ungerer562d2f82005-11-07 14:09:50 +100015 * Support for FEC controller of ColdFire processors.
16 * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +100017 *
18 * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
Philippe De Muyter677177c2006-06-27 13:05:33 +100019 * Copyright (c) 2004-2006 Macq Electronique SA.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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>
Sascha Hauer6f501b12009-01-28 23:03:05 +000039#include <linux/io.h>
40#include <linux/irq.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000041#include <linux/clk.h>
Sascha Haueread73182009-01-28 23:03:11 +000042#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Greg Ungerer080853a2007-07-30 16:28:46 +100044#include <asm/cacheflush.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000045
46#ifndef CONFIG_ARCH_MXC
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/coldfire.h>
48#include <asm/mcfsim.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000049#endif
Sascha Hauer6f501b12009-01-28 23:03:05 +000050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include "fec.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Sascha Hauer196719e2009-01-28 23:03:10 +000053#ifdef CONFIG_ARCH_MXC
54#include <mach/hardware.h>
55#define FEC_ALIGNMENT 0xf
56#else
57#define FEC_ALIGNMENT 0x3
58#endif
59
Sascha Haueread73182009-01-28 23:03:11 +000060/*
61 * Define the fixed address of the FEC hardware.
62 */
Greg Ungerer87f4abb2008-06-06 15:55:36 +100063#if defined(CONFIG_M5272)
Sebastian Siewiorc1d96152008-05-01 14:04:02 +100064#define HAVE_mii_link_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66static unsigned char fec_mac_default[] = {
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68};
69
70/*
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.
73 */
74#if defined(CONFIG_NETtel)
75#define FEC_FLASHMAC 0xf0006006
76#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
77#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#elif defined(CONFIG_CANCam)
79#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +100080#elif defined (CONFIG_M5272C3)
81#define FEC_FLASHMAC (0xffe04000 + 4)
82#elif defined(CONFIG_MOD5272)
83#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#else
85#define FEC_FLASHMAC 0
86#endif
Greg Ungerer43be6362009-02-26 22:42:51 -080087#endif /* CONFIG_M5272 */
Sascha Haueread73182009-01-28 23:03:11 +000088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* Forward declarations of some structures to support different PHYs
90*/
91
92typedef struct {
93 uint mii_data;
94 void (*funct)(uint mii_reg, struct net_device *dev);
95} phy_cmd_t;
96
97typedef struct {
98 uint id;
99 char *name;
100
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;
105} phy_info_t;
106
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.
112 */
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 */
121
Greg Ungerer562d2f82005-11-07 14:09:50 +1000122#if (((RX_RING_SIZE + TX_RING_SIZE) * 8) > PAGE_SIZE)
Matt Waddel6b265292006-06-27 13:10:56 +1000123#error "FEC: descriptor ring size constants too large"
Greg Ungerer562d2f82005-11-07 14:09:50 +1000124#endif
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126/* Interrupt events/masks.
127*/
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 */
138
139/* The FEC stores dest/src/type, data, and checksum for receive packets.
140 */
141#define PKT_MAXBUF_SIZE 1518
142#define PKT_MINBUF_SIZE 64
143#define PKT_MAXBLR_SIZE 1520
144
145
146/*
Matt Waddel6b265292006-06-27 13:10:56 +1000147 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 * size bits. Other FEC hardware does not, so we need to take that into
149 * account when setting it.
150 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000151#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Sascha Hauer196719e2009-01-28 23:03:10 +0000152 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARCH_MXC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
154#else
155#define OPT_FRAME_SIZE 0
156#endif
157
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.
165 */
166struct fec_enet_private {
167 /* Hardware registers of the FEC device */
Sascha Hauerf44d6302009-04-15 03:11:30 +0000168 void __iomem *hwp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Greg Ungerercb84d6e2007-07-30 16:29:09 +1000170 struct net_device *netdev;
171
Sascha Haueread73182009-01-28 23:03:11 +0000172 struct clk *clk;
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 /* 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];
177 ushort skb_cur;
178 ushort skb_dirty;
179
180 /* CPM dual port RAM relative addresses.
181 */
Sascha Hauer4661e752009-01-28 23:03:07 +0000182 dma_addr_t bd_dma;
Sascha Hauer2e285322009-04-15 01:32:16 +0000183 /* Address of Rx and Tx buffers. */
184 struct bufdesc *rx_bd_base;
185 struct bufdesc *tx_bd_base;
186 /* The next free ring entry */
187 struct bufdesc *cur_rx, *cur_tx;
188 /* The ring entries to be free()ed. */
189 struct bufdesc *dirty_tx;
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 uint tx_full;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000192 /* hold while accessing the HW like ringbuffer for tx/rx but not MAC */
193 spinlock_t hw_lock;
194 /* hold while accessing the mii_list_t() elements */
195 spinlock_t mii_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 uint phy_id;
198 uint phy_id_done;
199 uint phy_status;
200 uint phy_speed;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000201 phy_info_t const *phy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 struct work_struct phy_task;
203
204 uint sequence_done;
205 uint mii_phy_task_queued;
206
207 uint phy_addr;
208
209 int index;
210 int opened;
211 int link;
212 int old_link;
213 int full_duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214};
215
216static int fec_enet_open(struct net_device *dev);
217static int fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev);
218static void fec_enet_mii(struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100219static irqreturn_t fec_enet_interrupt(int irq, void * dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220static void fec_enet_tx(struct net_device *dev);
221static void fec_enet_rx(struct net_device *dev);
222static int fec_enet_close(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223static void set_multicast_list(struct net_device *dev);
224static void fec_restart(struct net_device *dev, int duplex);
225static void fec_stop(struct net_device *dev);
226static void fec_set_mac_address(struct net_device *dev);
227
228
229/* MII processing. We keep this as simple as possible. Requests are
230 * placed on the list (if there is room). When the request is finished
231 * by the MII, an optional function may be called.
232 */
233typedef struct mii_list {
234 uint mii_regval;
235 void (*mii_func)(uint val, struct net_device *dev);
236 struct mii_list *mii_next;
237} mii_list_t;
238
239#define NMII 20
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000240static mii_list_t mii_cmds[NMII];
241static mii_list_t *mii_free;
242static mii_list_t *mii_head;
243static mii_list_t *mii_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400245static int mii_queue(struct net_device *dev, int request,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 void (*func)(uint, struct net_device *));
247
248/* Make MII read/write commands for the FEC.
249*/
250#define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18))
251#define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | \
252 (VAL & 0xffff))
253#define mk_mii_end 0
254
255/* Transmitter timeout.
256*/
257#define TX_TIMEOUT (2*HZ)
258
259/* Register definitions for the PHY.
260*/
261
262#define MII_REG_CR 0 /* Control Register */
263#define MII_REG_SR 1 /* Status Register */
264#define MII_REG_PHYIR1 2 /* PHY Identification Register 1 */
265#define MII_REG_PHYIR2 3 /* PHY Identification Register 2 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400266#define MII_REG_ANAR 4 /* A-N Advertisement Register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267#define MII_REG_ANLPAR 5 /* A-N Link Partner Ability Register */
268#define MII_REG_ANER 6 /* A-N Expansion Register */
269#define MII_REG_ANNPTR 7 /* A-N Next Page Transmit Register */
270#define MII_REG_ANLPRNPR 8 /* A-N Link Partner Received Next Page Reg. */
271
272/* values for phy_status */
273
274#define PHY_CONF_ANE 0x0001 /* 1 auto-negotiation enabled */
275#define PHY_CONF_LOOP 0x0002 /* 1 loopback mode enabled */
276#define PHY_CONF_SPMASK 0x00f0 /* mask for speed */
277#define PHY_CONF_10HDX 0x0010 /* 10 Mbit half duplex supported */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400278#define PHY_CONF_10FDX 0x0020 /* 10 Mbit full duplex supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279#define PHY_CONF_100HDX 0x0040 /* 100 Mbit half duplex supported */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400280#define PHY_CONF_100FDX 0x0080 /* 100 Mbit full duplex supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282#define PHY_STAT_LINK 0x0100 /* 1 up - 0 down */
283#define PHY_STAT_FAULT 0x0200 /* 1 remote fault */
284#define PHY_STAT_ANC 0x0400 /* 1 auto-negotiation complete */
285#define PHY_STAT_SPMASK 0xf000 /* mask for speed */
286#define PHY_STAT_10HDX 0x1000 /* 10 Mbit half duplex selected */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400287#define PHY_STAT_10FDX 0x2000 /* 10 Mbit full duplex selected */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288#define PHY_STAT_100HDX 0x4000 /* 100 Mbit half duplex selected */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400289#define PHY_STAT_100FDX 0x8000 /* 100 Mbit full duplex selected */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291
292static int
293fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
294{
Sascha Hauerf44d6302009-04-15 03:11:30 +0000295 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000296 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000297 unsigned short status;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000298 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (!fep->link) {
301 /* Link is down or autonegotiation is in progress. */
302 return 1;
303 }
304
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000305 spin_lock_irqsave(&fep->hw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 /* Fill in a Tx ring entry */
307 bdp = fep->cur_tx;
308
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000309 status = bdp->cbd_sc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310#ifndef final_version
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000311 if (status & BD_ENET_TX_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 /* Ooops. All transmit buffers are full. Bail out.
313 * This should not happen, since dev->tbusy should be set.
314 */
315 printk("%s: tx queue full!.\n", dev->name);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000316 spin_unlock_irqrestore(&fep->hw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return 1;
318 }
319#endif
320
321 /* Clear all of the status flags.
322 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000323 status &= ~BD_ENET_TX_STATS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 /* Set buffer length and buffer pointer.
326 */
327 bdp->cbd_bufaddr = __pa(skb->data);
328 bdp->cbd_datlen = skb->len;
329
330 /*
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.
334 */
Sascha Hauer196719e2009-01-28 23:03:10 +0000335 if (bdp->cbd_bufaddr & FEC_ALIGNMENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 unsigned int index;
337 index = bdp - fep->tx_bd_base;
Sascha Hauer6989f512009-01-28 23:03:06 +0000338 memcpy(fep->tx_bounce[index], (void *)skb->data, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 bdp->cbd_bufaddr = __pa(fep->tx_bounce[index]);
340 }
341
342 /* Save skb pointer.
343 */
344 fep->tx_skbuff[fep->skb_cur] = skb;
345
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700346 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 fep->skb_cur = (fep->skb_cur+1) & TX_RING_MOD_MASK;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /* Push the data cache so the CPM does not get stale memory
350 * data.
351 */
Sascha Hauerccdc4f12009-01-28 23:03:09 +0000352 dma_sync_single(NULL, bdp->cbd_bufaddr,
353 bdp->cbd_datlen, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000355 /* 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 */
358
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000359 status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 | BD_ENET_TX_LAST | BD_ENET_TX_TC);
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000361 bdp->cbd_sc = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 dev->trans_start = jiffies;
364
365 /* Trigger transmission start */
Sascha Hauerf44d6302009-04-15 03:11:30 +0000366 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 /* If this was the last BD in the ring, start at the beginning again.
369 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000370 if (status & BD_ENET_TX_WRAP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 bdp = fep->tx_bd_base;
372 } else {
373 bdp++;
374 }
375
376 if (bdp == fep->dirty_tx) {
377 fep->tx_full = 1;
378 netif_stop_queue(dev);
379 }
380
Sascha Hauer2e285322009-04-15 01:32:16 +0000381 fep->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000383 spin_unlock_irqrestore(&fep->hw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 return 0;
386}
387
388static void
389fec_timeout(struct net_device *dev)
390{
391 struct fec_enet_private *fep = netdev_priv(dev);
392
393 printk("%s: transmit timed out.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700394 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395#ifndef final_version
396 {
397 int i;
Sascha Hauer2e285322009-04-15 01:32:16 +0000398 struct bufdesc *bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
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);
404
405 bdp = fep->tx_bd_base;
406 printk(" tx: %u buffers\n", TX_RING_SIZE);
407 for (i = 0 ; i < TX_RING_SIZE; i++) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400408 printk(" %08x: %04x %04x %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 (uint) bdp,
410 bdp->cbd_sc,
411 bdp->cbd_datlen,
412 (int) bdp->cbd_bufaddr);
413 bdp++;
414 }
415
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",
420 (uint) bdp,
421 bdp->cbd_sc,
422 bdp->cbd_datlen,
423 (int) bdp->cbd_bufaddr);
424 bdp++;
425 }
426 }
427#endif
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000428 fec_restart(dev, fep->full_duplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 netif_wake_queue(dev);
430}
431
432/* The interrupt handler.
433 * This is called from the MPC core interrupt.
434 */
435static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100436fec_enet_interrupt(int irq, void * dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 struct net_device *dev = dev_id;
Sascha Hauerf44d6302009-04-15 03:11:30 +0000439 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 uint int_events;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000441 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Sascha Hauerf44d6302009-04-15 03:11:30 +0000443 /* Get the interrupt events that caused us to be here. */
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000444 do {
Sascha Hauerf44d6302009-04-15 03:11:30 +0000445 int_events = readl(fep->hwp + FEC_IEVENT);
446 writel(int_events, fep->hwp + FEC_IEVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Sascha Hauerf44d6302009-04-15 03:11:30 +0000448 /* Handle receive event in its own function. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (int_events & FEC_ENET_RXF) {
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000450 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 fec_enet_rx(dev);
452 }
453
454 /* Transmit OK, or non-fatal error. Update the buffer
Sascha Hauerf44d6302009-04-15 03:11:30 +0000455 * descriptors. FEC handles all errors, we just discover
456 * them as part of the transmit process.
457 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (int_events & FEC_ENET_TXF) {
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000459 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 fec_enet_tx(dev);
461 }
462
463 if (int_events & FEC_ENET_MII) {
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000464 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 fec_enet_mii(dev);
466 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400467
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000468 } while (int_events);
469
470 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
473
474static void
475fec_enet_tx(struct net_device *dev)
476{
477 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +0000478 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000479 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 struct sk_buff *skb;
481
482 fep = netdev_priv(dev);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000483 spin_lock_irq(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 bdp = fep->dirty_tx;
485
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000486 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (bdp == fep->cur_tx && fep->tx_full == 0) break;
488
489 skb = fep->tx_skbuff[fep->skb_dirty];
490 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000491 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 BD_ENET_TX_RL | BD_ENET_TX_UN |
493 BD_ENET_TX_CSL)) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700494 dev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000495 if (status & BD_ENET_TX_HB) /* No heartbeat */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700496 dev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000497 if (status & BD_ENET_TX_LC) /* Late collision */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700498 dev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000499 if (status & BD_ENET_TX_RL) /* Retrans limit */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700500 dev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000501 if (status & BD_ENET_TX_UN) /* Underrun */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700502 dev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000503 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700504 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 } else {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700506 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508
509#ifndef final_version
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000510 if (status & BD_ENET_TX_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 printk("HEY! Enet xmit interrupt and TX_READY.\n");
512#endif
513 /* Deferred means some collisions occurred during transmit,
514 * but we eventually sent the packet OK.
515 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000516 if (status & BD_ENET_TX_DEF)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700517 dev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /* Free the sk buffer associated with this last transmit.
520 */
521 dev_kfree_skb_any(skb);
522 fep->tx_skbuff[fep->skb_dirty] = NULL;
523 fep->skb_dirty = (fep->skb_dirty + 1) & TX_RING_MOD_MASK;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /* Update pointer to next buffer descriptor to be transmitted.
526 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000527 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 bdp = fep->tx_bd_base;
529 else
530 bdp++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /* Since we have freed up a buffer, the ring is no longer
533 * full.
534 */
535 if (fep->tx_full) {
536 fep->tx_full = 0;
537 if (netif_queue_stopped(dev))
538 netif_wake_queue(dev);
539 }
540 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000541 fep->dirty_tx = bdp;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000542 spin_unlock_irq(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545
546/* During a receive, the cur_rx points to the current incoming buffer.
547 * When we update through the ring, if the next incoming buffer has
548 * not been given to the system, we just set the empty indicator,
549 * effectively tossing the packet.
550 */
551static void
552fec_enet_rx(struct net_device *dev)
553{
Sascha Hauerf44d6302009-04-15 03:11:30 +0000554 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000555 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000556 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 struct sk_buff *skb;
558 ushort pkt_len;
559 __u8 *data;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400560
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000561#ifdef CONFIG_M532x
562 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400563#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000565 spin_lock_irq(&fep->hw_lock);
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /* First, grab all of the stats for the incoming packet.
568 * These get messed up if we get called due to a busy condition.
569 */
570 bdp = fep->cur_rx;
571
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000572while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574#ifndef final_version
575 /* Since we have allocated space to hold a complete frame,
576 * the last indicator should be set.
577 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000578 if ((status & BD_ENET_RX_LAST) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 printk("FEC ENET: rcv is not +last\n");
580#endif
581
582 if (!fep->opened)
583 goto rx_processing_done;
584
585 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000586 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700588 dev->stats.rx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000589 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 /* Frame too long or too short. */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700591 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000593 if (status & BD_ENET_RX_NO) /* Frame alignment */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700594 dev->stats.rx_frame_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000595 if (status & BD_ENET_RX_CR) /* CRC Error */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700596 dev->stats.rx_crc_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000597 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700598 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600
601 /* Report late collisions as a frame error.
602 * On this error, the BD is closed, but we don't know what we
603 * have in the buffer. So, just drop this frame on the floor.
604 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000605 if (status & BD_ENET_RX_CL) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700606 dev->stats.rx_errors++;
607 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 goto rx_processing_done;
609 }
610
611 /* Process the incoming frame.
612 */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700613 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 pkt_len = bdp->cbd_datlen;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700615 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 data = (__u8*)__va(bdp->cbd_bufaddr);
617
Sascha Hauerccdc4f12009-01-28 23:03:09 +0000618 dma_sync_single(NULL, (unsigned long)__pa(data),
619 pkt_len - 4, DMA_FROM_DEVICE);
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 /* This does 16 byte alignment, exactly what we need.
622 * The packet length includes FCS, but we don't want to
623 * include that when passing upstream as it messes up
624 * bridging applications.
625 */
626 skb = dev_alloc_skb(pkt_len-4);
627
628 if (skb == NULL) {
629 printk("%s: Memory squeeze, dropping packet.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700630 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 skb_put(skb,pkt_len-4); /* Make room */
David S. Miller8c7b7fa2007-07-10 22:08:12 -0700633 skb_copy_to_linear_data(skb, data, pkt_len-4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 skb->protocol=eth_type_trans(skb,dev);
635 netif_rx(skb);
636 }
637 rx_processing_done:
638
639 /* Clear the status flags for this buffer.
640 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000641 status &= ~BD_ENET_RX_STATS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 /* Mark the buffer empty.
644 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000645 status |= BD_ENET_RX_EMPTY;
646 bdp->cbd_sc = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 /* Update BD pointer to next entry.
649 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000650 if (status & BD_ENET_RX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 bdp = fep->rx_bd_base;
652 else
653 bdp++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655#if 1
656 /* Doing this here will keep the FEC running while we process
657 * incoming frames. On a heavily loaded network, we should be
658 * able to keep up at the expense of system resources.
659 */
Sascha Hauerf44d6302009-04-15 03:11:30 +0000660 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661#endif
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000662 } /* while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) */
Sascha Hauer2e285322009-04-15 01:32:16 +0000663 fep->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665#if 0
666 /* Doing this here will allow us to process all frames in the
667 * ring before the FEC is allowed to put more there. On a heavily
668 * loaded network, some frames may be lost. Unfortunately, this
669 * increases the interrupt overhead since we can potentially work
670 * our way back to the interrupt return only to come right back
671 * here.
672 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000673 fecp->fec_r_des_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674#endif
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000675
676 spin_unlock_irq(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000680/* called from interrupt context */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681static void
682fec_enet_mii(struct net_device *dev)
683{
684 struct fec_enet_private *fep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 mii_list_t *mip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 fep = netdev_priv(dev);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000688 spin_lock_irq(&fep->mii_lock);
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if ((mip = mii_head) == NULL) {
691 printk("MII and no head!\n");
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000692 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694
695 if (mip->mii_func != NULL)
Sascha Hauerf44d6302009-04-15 03:11:30 +0000696 (*(mip->mii_func))(readl(fep->hwp + FEC_MII_DATA), dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 mii_head = mip->mii_next;
699 mip->mii_next = mii_free;
700 mii_free = mip;
701
702 if ((mip = mii_head) != NULL)
Sascha Hauerf44d6302009-04-15 03:11:30 +0000703 writel(mip->mii_regval, fep->hwp + FEC_MII_DATA);
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000704
705unlock:
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000706 spin_unlock_irq(&fep->mii_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
709static int
710mii_queue(struct net_device *dev, int regval, void (*func)(uint, struct net_device *))
711{
712 struct fec_enet_private *fep;
713 unsigned long flags;
714 mii_list_t *mip;
715 int retval;
716
717 /* Add PHY address to register command.
718 */
719 fep = netdev_priv(dev);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000720 spin_lock_irqsave(&fep->mii_lock, flags);
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 regval |= fep->phy_addr << 23;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 retval = 0;
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if ((mip = mii_free) != NULL) {
726 mii_free = mip->mii_next;
727 mip->mii_regval = regval;
728 mip->mii_func = func;
729 mip->mii_next = NULL;
730 if (mii_head) {
731 mii_tail->mii_next = mip;
732 mii_tail = mip;
Philippe De Muyterf909b1e2007-10-23 14:37:54 +1000733 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 mii_head = mii_tail = mip;
Sascha Hauerf44d6302009-04-15 03:11:30 +0000735 writel(regval, fep->hwp + FEC_MII_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
Philippe De Muyterf909b1e2007-10-23 14:37:54 +1000737 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 retval = 1;
739 }
740
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000741 spin_unlock_irqrestore(&fep->mii_lock, flags);
742 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
745static void mii_do_cmd(struct net_device *dev, const phy_cmd_t *c)
746{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if(!c)
748 return;
749
Philippe De Muyterbe6cb662007-10-23 14:37:54 +1000750 for (; c->mii_data != mk_mii_end; c++)
751 mii_queue(dev, c->mii_data, c->funct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
754static void mii_parse_sr(uint mii_reg, struct net_device *dev)
755{
756 struct fec_enet_private *fep = netdev_priv(dev);
757 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000758 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000760 status = *s & ~(PHY_STAT_LINK | PHY_STAT_FAULT | PHY_STAT_ANC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 if (mii_reg & 0x0004)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000763 status |= PHY_STAT_LINK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (mii_reg & 0x0010)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000765 status |= PHY_STAT_FAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (mii_reg & 0x0020)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000767 status |= PHY_STAT_ANC;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000768 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
771static void mii_parse_cr(uint mii_reg, struct net_device *dev)
772{
773 struct fec_enet_private *fep = netdev_priv(dev);
774 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000775 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000777 status = *s & ~(PHY_CONF_ANE | PHY_CONF_LOOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 if (mii_reg & 0x1000)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000780 status |= PHY_CONF_ANE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (mii_reg & 0x4000)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000782 status |= PHY_CONF_LOOP;
783 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
786static void mii_parse_anar(uint mii_reg, struct net_device *dev)
787{
788 struct fec_enet_private *fep = netdev_priv(dev);
789 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000790 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000792 status = *s & ~(PHY_CONF_SPMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 if (mii_reg & 0x0020)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000795 status |= PHY_CONF_10HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (mii_reg & 0x0040)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000797 status |= PHY_CONF_10FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (mii_reg & 0x0080)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000799 status |= PHY_CONF_100HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (mii_reg & 0x00100)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000801 status |= PHY_CONF_100FDX;
802 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
805/* ------------------------------------------------------------------------- */
806/* The Level one LXT970 is used by many boards */
807
808#define MII_LXT970_MIRROR 16 /* Mirror register */
809#define MII_LXT970_IER 17 /* Interrupt Enable Register */
810#define MII_LXT970_ISR 18 /* Interrupt Status Register */
811#define MII_LXT970_CONFIG 19 /* Configuration Register */
812#define MII_LXT970_CSR 20 /* Chip Status Register */
813
814static void mii_parse_lxt970_csr(uint mii_reg, struct net_device *dev)
815{
816 struct fec_enet_private *fep = netdev_priv(dev);
817 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000818 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000820 status = *s & ~(PHY_STAT_SPMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (mii_reg & 0x0800) {
822 if (mii_reg & 0x1000)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000823 status |= PHY_STAT_100FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000825 status |= PHY_STAT_100HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 } else {
827 if (mii_reg & 0x1000)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000828 status |= PHY_STAT_10FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000830 status |= PHY_STAT_10HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000832 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000835static phy_cmd_t const phy_cmd_lxt970_config[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 { mk_mii_read(MII_REG_CR), mii_parse_cr },
837 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
838 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000839 };
840static phy_cmd_t const phy_cmd_lxt970_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 { mk_mii_write(MII_LXT970_IER, 0x0002), NULL },
842 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
843 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000844 };
845static phy_cmd_t const phy_cmd_lxt970_ack_int[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 /* read SR and ISR to acknowledge */
847 { mk_mii_read(MII_REG_SR), mii_parse_sr },
848 { mk_mii_read(MII_LXT970_ISR), NULL },
849
850 /* find out the current status */
851 { mk_mii_read(MII_LXT970_CSR), mii_parse_lxt970_csr },
852 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000853 };
854static phy_cmd_t const phy_cmd_lxt970_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 { mk_mii_write(MII_LXT970_IER, 0x0000), NULL },
856 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000857 };
858static phy_info_t const phy_info_lxt970 = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400859 .id = 0x07810000,
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000860 .name = "LXT970",
861 .config = phy_cmd_lxt970_config,
862 .startup = phy_cmd_lxt970_startup,
863 .ack_int = phy_cmd_lxt970_ack_int,
864 .shutdown = phy_cmd_lxt970_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865};
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867/* ------------------------------------------------------------------------- */
868/* The Level one LXT971 is used on some of my custom boards */
869
870/* register definitions for the 971 */
871
872#define MII_LXT971_PCR 16 /* Port Control Register */
873#define MII_LXT971_SR2 17 /* Status Register 2 */
874#define MII_LXT971_IER 18 /* Interrupt Enable Register */
875#define MII_LXT971_ISR 19 /* Interrupt Status Register */
876#define MII_LXT971_LCR 20 /* LED Control Register */
877#define MII_LXT971_TCR 30 /* Transmit Control Register */
878
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400879/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 * I had some nice ideas of running the MDIO faster...
881 * The 971 should support 8MHz and I tried it, but things acted really
882 * weird, so 2.5 MHz ought to be enough for anyone...
883 */
884
885static void mii_parse_lxt971_sr2(uint mii_reg, struct net_device *dev)
886{
887 struct fec_enet_private *fep = netdev_priv(dev);
888 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000889 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000891 status = *s & ~(PHY_STAT_SPMASK | PHY_STAT_LINK | PHY_STAT_ANC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 if (mii_reg & 0x0400) {
894 fep->link = 1;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000895 status |= PHY_STAT_LINK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 } else {
897 fep->link = 0;
898 }
899 if (mii_reg & 0x0080)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000900 status |= PHY_STAT_ANC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (mii_reg & 0x4000) {
902 if (mii_reg & 0x0200)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000903 status |= PHY_STAT_100FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000905 status |= PHY_STAT_100HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 } else {
907 if (mii_reg & 0x0200)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000908 status |= PHY_STAT_10FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000910 status |= PHY_STAT_10HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912 if (mii_reg & 0x0008)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000913 status |= PHY_STAT_FAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000915 *s = status;
916}
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400917
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000918static phy_cmd_t const phy_cmd_lxt971_config[] = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400919 /* limit to 10MBit because my prototype board
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 * doesn't work with 100. */
921 { mk_mii_read(MII_REG_CR), mii_parse_cr },
922 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
923 { mk_mii_read(MII_LXT971_SR2), mii_parse_lxt971_sr2 },
924 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000925 };
926static phy_cmd_t const phy_cmd_lxt971_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 { mk_mii_write(MII_LXT971_IER, 0x00f2), NULL },
928 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
929 { mk_mii_write(MII_LXT971_LCR, 0xd422), NULL }, /* LED config */
930 /* Somehow does the 971 tell me that the link is down
931 * the first read after power-up.
932 * read here to get a valid value in ack_int */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400933 { mk_mii_read(MII_REG_SR), mii_parse_sr },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000935 };
936static phy_cmd_t const phy_cmd_lxt971_ack_int[] = {
937 /* acknowledge the int before reading status ! */
938 { mk_mii_read(MII_LXT971_ISR), NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 /* find out the current status */
940 { mk_mii_read(MII_REG_SR), mii_parse_sr },
941 { mk_mii_read(MII_LXT971_SR2), mii_parse_lxt971_sr2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000943 };
944static phy_cmd_t const phy_cmd_lxt971_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 { mk_mii_write(MII_LXT971_IER, 0x0000), NULL },
946 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000947 };
948static phy_info_t const phy_info_lxt971 = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400949 .id = 0x0001378e,
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000950 .name = "LXT971",
951 .config = phy_cmd_lxt971_config,
952 .startup = phy_cmd_lxt971_startup,
953 .ack_int = phy_cmd_lxt971_ack_int,
954 .shutdown = phy_cmd_lxt971_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955};
956
957/* ------------------------------------------------------------------------- */
958/* The Quality Semiconductor QS6612 is used on the RPX CLLF */
959
960/* register definitions */
961
962#define MII_QS6612_MCR 17 /* Mode Control Register */
963#define MII_QS6612_FTR 27 /* Factory Test Register */
964#define MII_QS6612_MCO 28 /* Misc. Control Register */
965#define MII_QS6612_ISR 29 /* Interrupt Source Register */
966#define MII_QS6612_IMR 30 /* Interrupt Mask Register */
967#define MII_QS6612_PCR 31 /* 100BaseTx PHY Control Reg. */
968
969static void mii_parse_qs6612_pcr(uint mii_reg, struct net_device *dev)
970{
971 struct fec_enet_private *fep = netdev_priv(dev);
972 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000973 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000975 status = *s & ~(PHY_STAT_SPMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 switch((mii_reg >> 2) & 7) {
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000978 case 1: status |= PHY_STAT_10HDX; break;
979 case 2: status |= PHY_STAT_100HDX; break;
980 case 5: status |= PHY_STAT_10FDX; break;
981 case 6: status |= PHY_STAT_100FDX; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000984 *s = status;
985}
986
987static phy_cmd_t const phy_cmd_qs6612_config[] = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400988 /* The PHY powers up isolated on the RPX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 * so send a command to allow operation.
990 */
991 { mk_mii_write(MII_QS6612_PCR, 0x0dc0), NULL },
992
993 /* parse cr and anar to get some info */
994 { mk_mii_read(MII_REG_CR), mii_parse_cr },
995 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
996 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000997 };
998static phy_cmd_t const phy_cmd_qs6612_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 { mk_mii_write(MII_QS6612_IMR, 0x003a), NULL },
1000 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
1001 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001002 };
1003static phy_cmd_t const phy_cmd_qs6612_ack_int[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 /* we need to read ISR, SR and ANER to acknowledge */
1005 { mk_mii_read(MII_QS6612_ISR), NULL },
1006 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1007 { mk_mii_read(MII_REG_ANER), NULL },
1008
1009 /* read pcr to get info */
1010 { mk_mii_read(MII_QS6612_PCR), mii_parse_qs6612_pcr },
1011 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001012 };
1013static phy_cmd_t const phy_cmd_qs6612_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 { mk_mii_write(MII_QS6612_IMR, 0x0000), NULL },
1015 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001016 };
1017static phy_info_t const phy_info_qs6612 = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001018 .id = 0x00181440,
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001019 .name = "QS6612",
1020 .config = phy_cmd_qs6612_config,
1021 .startup = phy_cmd_qs6612_startup,
1022 .ack_int = phy_cmd_qs6612_ack_int,
1023 .shutdown = phy_cmd_qs6612_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024};
1025
1026/* ------------------------------------------------------------------------- */
1027/* AMD AM79C874 phy */
1028
1029/* register definitions for the 874 */
1030
1031#define MII_AM79C874_MFR 16 /* Miscellaneous Feature Register */
1032#define MII_AM79C874_ICSR 17 /* Interrupt/Status Register */
1033#define MII_AM79C874_DR 18 /* Diagnostic Register */
1034#define MII_AM79C874_PMLR 19 /* Power and Loopback Register */
1035#define MII_AM79C874_MCR 21 /* ModeControl Register */
1036#define MII_AM79C874_DC 23 /* Disconnect Counter */
1037#define MII_AM79C874_REC 24 /* Recieve Error Counter */
1038
1039static void mii_parse_am79c874_dr(uint mii_reg, struct net_device *dev)
1040{
1041 struct fec_enet_private *fep = netdev_priv(dev);
1042 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001043 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001045 status = *s & ~(PHY_STAT_SPMASK | PHY_STAT_ANC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 if (mii_reg & 0x0080)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001048 status |= PHY_STAT_ANC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (mii_reg & 0x0400)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001050 status |= ((mii_reg & 0x0800) ? PHY_STAT_100FDX : PHY_STAT_100HDX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001052 status |= ((mii_reg & 0x0800) ? PHY_STAT_10FDX : PHY_STAT_10HDX);
1053
1054 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001057static phy_cmd_t const phy_cmd_am79c874_config[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1059 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1060 { mk_mii_read(MII_AM79C874_DR), mii_parse_am79c874_dr },
1061 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001062 };
1063static phy_cmd_t const phy_cmd_am79c874_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 { mk_mii_write(MII_AM79C874_ICSR, 0xff00), NULL },
1065 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001066 { mk_mii_read(MII_REG_SR), mii_parse_sr },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001068 };
1069static phy_cmd_t const phy_cmd_am79c874_ack_int[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 /* find out the current status */
1071 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1072 { mk_mii_read(MII_AM79C874_DR), mii_parse_am79c874_dr },
1073 /* we only need to read ISR to acknowledge */
1074 { mk_mii_read(MII_AM79C874_ICSR), NULL },
1075 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001076 };
1077static phy_cmd_t const phy_cmd_am79c874_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 { mk_mii_write(MII_AM79C874_ICSR, 0x0000), NULL },
1079 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001080 };
1081static phy_info_t const phy_info_am79c874 = {
1082 .id = 0x00022561,
1083 .name = "AM79C874",
1084 .config = phy_cmd_am79c874_config,
1085 .startup = phy_cmd_am79c874_startup,
1086 .ack_int = phy_cmd_am79c874_ack_int,
1087 .shutdown = phy_cmd_am79c874_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088};
1089
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091/* ------------------------------------------------------------------------- */
1092/* Kendin KS8721BL phy */
1093
1094/* register definitions for the 8721 */
1095
1096#define MII_KS8721BL_RXERCR 21
Sascha Hauer43268dc2009-01-28 23:03:08 +00001097#define MII_KS8721BL_ICSR 27
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098#define MII_KS8721BL_PHYCR 31
1099
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001100static phy_cmd_t const phy_cmd_ks8721bl_config[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1102 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1103 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001104 };
1105static phy_cmd_t const phy_cmd_ks8721bl_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 { mk_mii_write(MII_KS8721BL_ICSR, 0xff00), NULL },
1107 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001108 { mk_mii_read(MII_REG_SR), mii_parse_sr },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001110 };
1111static phy_cmd_t const phy_cmd_ks8721bl_ack_int[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 /* find out the current status */
1113 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1114 /* we only need to read ISR to acknowledge */
1115 { mk_mii_read(MII_KS8721BL_ICSR), NULL },
1116 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001117 };
1118static phy_cmd_t const phy_cmd_ks8721bl_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 { mk_mii_write(MII_KS8721BL_ICSR, 0x0000), NULL },
1120 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001121 };
1122static phy_info_t const phy_info_ks8721bl = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001123 .id = 0x00022161,
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001124 .name = "KS8721BL",
1125 .config = phy_cmd_ks8721bl_config,
1126 .startup = phy_cmd_ks8721bl_startup,
1127 .ack_int = phy_cmd_ks8721bl_ack_int,
1128 .shutdown = phy_cmd_ks8721bl_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129};
1130
1131/* ------------------------------------------------------------------------- */
Greg Ungerer562d2f82005-11-07 14:09:50 +10001132/* register definitions for the DP83848 */
1133
1134#define MII_DP8384X_PHYSTST 16 /* PHY Status Register */
1135
1136static void mii_parse_dp8384x_sr2(uint mii_reg, struct net_device *dev)
1137{
Wang Chen4cf16532008-11-12 23:38:14 -08001138 struct fec_enet_private *fep = netdev_priv(dev);
Greg Ungerer562d2f82005-11-07 14:09:50 +10001139 volatile uint *s = &(fep->phy_status);
1140
1141 *s &= ~(PHY_STAT_SPMASK | PHY_STAT_LINK | PHY_STAT_ANC);
1142
1143 /* Link up */
1144 if (mii_reg & 0x0001) {
1145 fep->link = 1;
1146 *s |= PHY_STAT_LINK;
1147 } else
1148 fep->link = 0;
1149 /* Status of link */
1150 if (mii_reg & 0x0010) /* Autonegotioation complete */
1151 *s |= PHY_STAT_ANC;
1152 if (mii_reg & 0x0002) { /* 10MBps? */
1153 if (mii_reg & 0x0004) /* Full Duplex? */
1154 *s |= PHY_STAT_10FDX;
1155 else
1156 *s |= PHY_STAT_10HDX;
1157 } else { /* 100 Mbps? */
1158 if (mii_reg & 0x0004) /* Full Duplex? */
1159 *s |= PHY_STAT_100FDX;
1160 else
1161 *s |= PHY_STAT_100HDX;
1162 }
1163 if (mii_reg & 0x0008)
1164 *s |= PHY_STAT_FAULT;
1165}
1166
1167static phy_info_t phy_info_dp83848= {
1168 0x020005c9,
1169 "DP83848",
1170
1171 (const phy_cmd_t []) { /* config */
1172 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1173 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1174 { mk_mii_read(MII_DP8384X_PHYSTST), mii_parse_dp8384x_sr2 },
1175 { mk_mii_end, }
1176 },
1177 (const phy_cmd_t []) { /* startup - enable interrupts */
1178 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
1179 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1180 { mk_mii_end, }
1181 },
1182 (const phy_cmd_t []) { /* ack_int - never happens, no interrupt */
1183 { mk_mii_end, }
1184 },
1185 (const phy_cmd_t []) { /* shutdown */
1186 { mk_mii_end, }
1187 },
1188};
1189
1190/* ------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001192static phy_info_t const * const phy_info[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 &phy_info_lxt970,
1194 &phy_info_lxt971,
1195 &phy_info_qs6612,
1196 &phy_info_am79c874,
1197 &phy_info_ks8721bl,
Greg Ungerer562d2f82005-11-07 14:09:50 +10001198 &phy_info_dp83848,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 NULL
1200};
1201
1202/* ------------------------------------------------------------------------- */
Sebastian Siewiorc1d96152008-05-01 14:04:02 +10001203#ifdef HAVE_mii_link_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001205mii_link_interrupt(int irq, void * dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207/*
Greg Ungerer43be6362009-02-26 22:42:51 -08001208 * This is specific to the MII interrupt setup of the M5272EVB.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 */
Greg Ungerer43be6362009-02-26 22:42:51 -08001210static void __inline__ fec_request_mii_intr(struct net_device *dev)
1211{
1212 if (request_irq(66, mii_link_interrupt, IRQF_DISABLED, "fec(MII)", dev) != 0)
1213 printk("FEC: Could not allocate fec(MII) IRQ(66)!\n");
1214}
1215
1216static void __inline__ fec_disable_phy_intr(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
1218 volatile unsigned long *icrp;
Greg Ungerer43be6362009-02-26 22:42:51 -08001219 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR1);
1220 *icrp = 0x08000000;
1221}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Greg Ungerer43be6362009-02-26 22:42:51 -08001223static void __inline__ fec_phy_ack_intr(void)
1224{
1225 volatile unsigned long *icrp;
1226 /* Acknowledge the interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR1);
Greg Ungererf861d622007-07-30 16:29:16 +10001228 *icrp = 0x0d000000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
Greg Ungerer43be6362009-02-26 22:42:51 -08001231#ifdef CONFIG_M5272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232static void __inline__ fec_get_mac(struct net_device *dev)
1233{
1234 struct fec_enet_private *fep = netdev_priv(dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001235 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001237 if (FEC_FLASHMAC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 /*
1239 * Get MAC address from FLASH.
1240 * If it is all 1's or 0's, use the default.
1241 */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001242 iap = (unsigned char *)FEC_FLASHMAC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if ((iap[0] == 0) && (iap[1] == 0) && (iap[2] == 0) &&
1244 (iap[3] == 0) && (iap[4] == 0) && (iap[5] == 0))
1245 iap = fec_mac_default;
1246 if ((iap[0] == 0xff) && (iap[1] == 0xff) && (iap[2] == 0xff) &&
1247 (iap[3] == 0xff) && (iap[4] == 0xff) && (iap[5] == 0xff))
1248 iap = fec_mac_default;
1249 } else {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001250 *((unsigned long *) &tmpaddr[0]) = readl(fep->hwp + FEC_ADDR_LOW);
1251 *((unsigned short *) &tmpaddr[4]) = (readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 iap = &tmpaddr[0];
1253 }
1254
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001255 memcpy(dev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 /* Adjust MAC if using default MAC address */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001258 if (iap == fec_mac_default)
1259 dev->dev_addr[ETH_ALEN-1] = fec_mac_default[ETH_ALEN-1] + fep->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261#endif
1262
1263/* ------------------------------------------------------------------------- */
1264
1265static void mii_display_status(struct net_device *dev)
1266{
1267 struct fec_enet_private *fep = netdev_priv(dev);
1268 volatile uint *s = &(fep->phy_status);
1269
1270 if (!fep->link && !fep->old_link) {
1271 /* Link is still down - don't print anything */
1272 return;
1273 }
1274
1275 printk("%s: status: ", dev->name);
1276
1277 if (!fep->link) {
1278 printk("link down");
1279 } else {
1280 printk("link up");
1281
1282 switch(*s & PHY_STAT_SPMASK) {
1283 case PHY_STAT_100FDX: printk(", 100MBit Full Duplex"); break;
1284 case PHY_STAT_100HDX: printk(", 100MBit Half Duplex"); break;
1285 case PHY_STAT_10FDX: printk(", 10MBit Full Duplex"); break;
1286 case PHY_STAT_10HDX: printk(", 10MBit Half Duplex"); break;
1287 default:
1288 printk(", Unknown speed/duplex");
1289 }
1290
1291 if (*s & PHY_STAT_ANC)
1292 printk(", auto-negotiation complete");
1293 }
1294
1295 if (*s & PHY_STAT_FAULT)
1296 printk(", remote fault");
1297
1298 printk(".\n");
1299}
1300
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001301static void mii_display_config(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001303 struct fec_enet_private *fep = container_of(work, struct fec_enet_private, phy_task);
1304 struct net_device *dev = fep->netdev;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001305 uint status = fep->phy_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 /*
1308 ** When we get here, phy_task is already removed from
1309 ** the workqueue. It is thus safe to allow to reuse it.
1310 */
1311 fep->mii_phy_task_queued = 0;
1312 printk("%s: config: auto-negotiation ", dev->name);
1313
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001314 if (status & PHY_CONF_ANE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 printk("on");
1316 else
1317 printk("off");
1318
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001319 if (status & PHY_CONF_100FDX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 printk(", 100FDX");
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001321 if (status & PHY_CONF_100HDX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 printk(", 100HDX");
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001323 if (status & PHY_CONF_10FDX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 printk(", 10FDX");
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001325 if (status & PHY_CONF_10HDX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 printk(", 10HDX");
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001327 if (!(status & PHY_CONF_SPMASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 printk(", No speed/duplex selected?");
1329
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001330 if (status & PHY_CONF_LOOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 printk(", loopback enabled");
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 printk(".\n");
1334
1335 fep->sequence_done = 1;
1336}
1337
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001338static void mii_relink(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001340 struct fec_enet_private *fep = container_of(work, struct fec_enet_private, phy_task);
1341 struct net_device *dev = fep->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 int duplex;
1343
1344 /*
1345 ** When we get here, phy_task is already removed from
1346 ** the workqueue. It is thus safe to allow to reuse it.
1347 */
1348 fep->mii_phy_task_queued = 0;
1349 fep->link = (fep->phy_status & PHY_STAT_LINK) ? 1 : 0;
1350 mii_display_status(dev);
1351 fep->old_link = fep->link;
1352
1353 if (fep->link) {
1354 duplex = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001355 if (fep->phy_status
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 & (PHY_STAT_100FDX | PHY_STAT_10FDX))
1357 duplex = 1;
1358 fec_restart(dev, duplex);
Philippe De Muyterf909b1e2007-10-23 14:37:54 +10001359 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 fec_stop(dev);
1361
1362#if 0
1363 enable_irq(fep->mii_irq);
1364#endif
1365
1366}
1367
1368/* mii_queue_relink is called in interrupt context from mii_link_interrupt */
1369static void mii_queue_relink(uint mii_reg, struct net_device *dev)
1370{
1371 struct fec_enet_private *fep = netdev_priv(dev);
1372
1373 /*
1374 ** We cannot queue phy_task twice in the workqueue. It
1375 ** would cause an endless loop in the workqueue.
1376 ** Fortunately, if the last mii_relink entry has not yet been
1377 ** executed now, it will do the job for the current interrupt,
1378 ** which is just what we want.
1379 */
1380 if (fep->mii_phy_task_queued)
1381 return;
1382
1383 fep->mii_phy_task_queued = 1;
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001384 INIT_WORK(&fep->phy_task, mii_relink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 schedule_work(&fep->phy_task);
1386}
1387
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001388/* mii_queue_config is called in interrupt context from fec_enet_mii */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389static void mii_queue_config(uint mii_reg, struct net_device *dev)
1390{
1391 struct fec_enet_private *fep = netdev_priv(dev);
1392
1393 if (fep->mii_phy_task_queued)
1394 return;
1395
1396 fep->mii_phy_task_queued = 1;
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001397 INIT_WORK(&fep->phy_task, mii_display_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 schedule_work(&fep->phy_task);
1399}
1400
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001401phy_cmd_t const phy_cmd_relink[] = {
1402 { mk_mii_read(MII_REG_CR), mii_queue_relink },
1403 { mk_mii_end, }
1404 };
1405phy_cmd_t const phy_cmd_config[] = {
1406 { mk_mii_read(MII_REG_CR), mii_queue_config },
1407 { mk_mii_end, }
1408 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410/* Read remainder of PHY ID.
1411*/
1412static void
1413mii_discover_phy3(uint mii_reg, struct net_device *dev)
1414{
1415 struct fec_enet_private *fep;
1416 int i;
1417
1418 fep = netdev_priv(dev);
1419 fep->phy_id |= (mii_reg & 0xffff);
1420 printk("fec: PHY @ 0x%x, ID 0x%08x", fep->phy_addr, fep->phy_id);
1421
1422 for(i = 0; phy_info[i]; i++) {
1423 if(phy_info[i]->id == (fep->phy_id >> 4))
1424 break;
1425 }
1426
1427 if (phy_info[i])
1428 printk(" -- %s\n", phy_info[i]->name);
1429 else
1430 printk(" -- unknown PHY!\n");
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 fep->phy = phy_info[i];
1433 fep->phy_id_done = 1;
1434}
1435
1436/* Scan all of the MII PHY addresses looking for someone to respond
1437 * with a valid ID. This usually happens quickly.
1438 */
1439static void
1440mii_discover_phy(uint mii_reg, struct net_device *dev)
1441{
1442 struct fec_enet_private *fep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 uint phytype;
1444
1445 fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 if (fep->phy_addr < 32) {
1448 if ((phytype = (mii_reg & 0xffff)) != 0xffff && phytype != 0) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 /* Got first part of ID, now get remainder.
1451 */
1452 fep->phy_id = phytype << 16;
1453 mii_queue(dev, mk_mii_read(MII_REG_PHYIR2),
1454 mii_discover_phy3);
Philippe De Muyterf909b1e2007-10-23 14:37:54 +10001455 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 fep->phy_addr++;
1457 mii_queue(dev, mk_mii_read(MII_REG_PHYIR1),
1458 mii_discover_phy);
1459 }
1460 } else {
1461 printk("FEC: No PHY device found.\n");
1462 /* Disable external MII interface */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001463 writel(0, fep->hwp + FEC_MII_SPEED);
1464 fep->phy_speed = 0;
Greg Ungerer43be6362009-02-26 22:42:51 -08001465#ifdef HAVE_mii_link_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 fec_disable_phy_intr();
Sascha Haueread73182009-01-28 23:03:11 +00001467#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
1469}
1470
1471/* This interrupt occurs when the PHY detects a link change.
1472*/
Sebastian Siewiorc1d96152008-05-01 14:04:02 +10001473#ifdef HAVE_mii_link_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001475mii_link_interrupt(int irq, void * dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
1477 struct net_device *dev = dev_id;
1478 struct fec_enet_private *fep = netdev_priv(dev);
1479
1480 fec_phy_ack_intr();
1481
1482#if 0
1483 disable_irq(fep->mii_irq); /* disable now, enable later */
1484#endif
1485
1486 mii_do_cmd(dev, fep->phy->ack_int);
1487 mii_do_cmd(dev, phy_cmd_relink); /* restart and display status */
1488
1489 return IRQ_HANDLED;
1490}
Sebastian Siewiorc1d96152008-05-01 14:04:02 +10001491#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493static int
1494fec_enet_open(struct net_device *dev)
1495{
1496 struct fec_enet_private *fep = netdev_priv(dev);
1497
1498 /* I should reset the ring buffers here, but I don't yet know
1499 * a simple way to do that.
1500 */
1501 fec_set_mac_address(dev);
1502
1503 fep->sequence_done = 0;
1504 fep->link = 0;
1505
1506 if (fep->phy) {
1507 mii_do_cmd(dev, fep->phy->ack_int);
1508 mii_do_cmd(dev, fep->phy->config);
1509 mii_do_cmd(dev, phy_cmd_config); /* display configuration */
1510
Matt Waddel6b265292006-06-27 13:10:56 +10001511 /* Poll until the PHY tells us its configuration
1512 * (not link state).
1513 * Request is initiated by mii_do_cmd above, but answer
1514 * comes by interrupt.
1515 * This should take about 25 usec per register at 2.5 MHz,
1516 * and we read approximately 5 registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 */
1518 while(!fep->sequence_done)
1519 schedule();
1520
1521 mii_do_cmd(dev, fep->phy->startup);
1522
1523 /* Set the initial link state to true. A lot of hardware
1524 * based on this device does not implement a PHY interrupt,
1525 * so we are never notified of link change.
1526 */
1527 fep->link = 1;
1528 } else {
1529 fep->link = 1; /* lets just try it and see */
1530 /* no phy, go full duplex, it's most likely a hub chip */
1531 fec_restart(dev, 1);
1532 }
1533
1534 netif_start_queue(dev);
1535 fep->opened = 1;
1536 return 0; /* Success */
1537}
1538
1539static int
1540fec_enet_close(struct net_device *dev)
1541{
1542 struct fec_enet_private *fep = netdev_priv(dev);
1543
1544 /* Don't know what to do yet.
1545 */
1546 fep->opened = 0;
1547 netif_stop_queue(dev);
1548 fec_stop(dev);
1549
1550 return 0;
1551}
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553/* Set or clear the multicast filter for this adaptor.
1554 * Skeleton taken from sunlance driver.
1555 * The CPM Ethernet implementation allows Multicast as well as individual
1556 * MAC address filtering. Some of the drivers check to make sure it is
1557 * a group multicast address, and discard those that are not. I guess I
1558 * will do the same for now, but just remove the test if you want
1559 * individual filtering as well (do the upper net layers want or support
1560 * this kind of feature?).
1561 */
1562
1563#define HASH_BITS 6 /* #bits in hash */
1564#define CRC32_POLY 0xEDB88320
1565
1566static void set_multicast_list(struct net_device *dev)
1567{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001568 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 struct dev_mc_list *dmi;
Sascha Hauerf44d6302009-04-15 03:11:30 +00001570 unsigned int i, j, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 unsigned char hash;
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (dev->flags&IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001574 tmp = readl(fep->hwp + FEC_R_CNTRL);
1575 tmp |= 0x8;
1576 writel(tmp, fep->hwp + FEC_R_CNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 } else {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001578 tmp = readl(fep->hwp + FEC_R_CNTRL);
1579 tmp &= ~0x8;
1580 writel(tmp, fep->hwp + FEC_R_CNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 if (dev->flags & IFF_ALLMULTI) {
1583 /* Catch all multicast addresses, so set the
1584 * filter to all 1's.
1585 */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001586 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1587 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 } else {
1589 /* Clear filter and add the addresses in hash register.
1590 */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001591 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1592 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 dmi = dev->mc_list;
1595
1596 for (j = 0; j < dev->mc_count; j++, dmi = dmi->next)
1597 {
1598 /* Only support group multicast for now.
1599 */
1600 if (!(dmi->dmi_addr[0] & 1))
1601 continue;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 /* calculate crc32 value of mac address
1604 */
1605 crc = 0xffffffff;
1606
1607 for (i = 0; i < dmi->dmi_addrlen; i++)
1608 {
1609 data = dmi->dmi_addr[i];
1610 for (bit = 0; bit < 8; bit++, data >>= 1)
1611 {
1612 crc = (crc >> 1) ^
1613 (((crc ^ data) & 1) ? CRC32_POLY : 0);
1614 }
1615 }
1616
1617 /* only upper 6 bits (HASH_BITS) are used
1618 which point to specific bit in he hash registers
1619 */
1620 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001621
Sascha Hauerf44d6302009-04-15 03:11:30 +00001622 if (hash > 31) {
1623 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1624 tmp |= 1 << (hash - 32);
1625 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1626 } else {
1627 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1628 tmp |= 1 << hash;
1629 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
1632 }
1633 }
1634}
1635
1636/* Set a MAC change in hardware.
1637 */
1638static void
1639fec_set_mac_address(struct net_device *dev)
1640{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001641 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 /* Set station address. */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001644 writel(dev->dev_addr[3] | (dev->dev_addr[2] << 8) |
1645 (dev->dev_addr[1] << 16) | (dev->dev_addr[0] << 24),
1646 fep->hwp + FEC_ADDR_LOW);
1647 writel((dev->dev_addr[5] << 16) | (dev->dev_addr[4] << 24),
1648 fep + FEC_ADDR_HIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
1650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 /*
1652 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00001653 *
1654 * index is only used in legacy code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 */
Sascha Haueread73182009-01-28 23:03:11 +00001656int __init fec_enet_init(struct net_device *dev, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
1658 struct fec_enet_private *fep = netdev_priv(dev);
1659 unsigned long mem_addr;
Sascha Hauer2e285322009-04-15 01:32:16 +00001660 struct bufdesc *bdp, *cbd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Greg Ungerer562d2f82005-11-07 14:09:50 +10001663 /* Allocate memory for buffer descriptors.
1664 */
Sascha Hauer4661e752009-01-28 23:03:07 +00001665 mem_addr = (unsigned long)dma_alloc_coherent(NULL, PAGE_SIZE,
1666 &fep->bd_dma, GFP_KERNEL);
Greg Ungerer562d2f82005-11-07 14:09:50 +10001667 if (mem_addr == 0) {
1668 printk("FEC: allocate descriptor memory failed?\n");
1669 return -ENOMEM;
1670 }
1671
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10001672 spin_lock_init(&fep->hw_lock);
1673 spin_lock_init(&fep->mii_lock);
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 fep->index = index;
Sascha Hauerf44d6302009-04-15 03:11:30 +00001676 fep->hwp = (void __iomem *)dev->base_addr;
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001677 fep->netdev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
1679 /* Whack a reset. We should wait for this.
1680 */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001681 writel(1, fep->hwp + FEC_ECNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 udelay(10);
1683
Sascha Haueread73182009-01-28 23:03:11 +00001684 /* Set the Ethernet address */
Greg Ungerer43be6362009-02-26 22:42:51 -08001685#ifdef CONFIG_M5272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 fec_get_mac(dev);
Sascha Haueread73182009-01-28 23:03:11 +00001687#else
1688 {
1689 unsigned long l;
Sascha Hauerf44d6302009-04-15 03:11:30 +00001690 l = readl(fep->hwp + FEC_ADDR_LOW);
Sascha Haueread73182009-01-28 23:03:11 +00001691 dev->dev_addr[0] = (unsigned char)((l & 0xFF000000) >> 24);
1692 dev->dev_addr[1] = (unsigned char)((l & 0x00FF0000) >> 16);
1693 dev->dev_addr[2] = (unsigned char)((l & 0x0000FF00) >> 8);
1694 dev->dev_addr[3] = (unsigned char)((l & 0x000000FF) >> 0);
Sascha Hauerf44d6302009-04-15 03:11:30 +00001695 l = readl(fep->hwp + FEC_ADDR_HIGH);
Sascha Haueread73182009-01-28 23:03:11 +00001696 dev->dev_addr[4] = (unsigned char)((l & 0xFF000000) >> 24);
1697 dev->dev_addr[5] = (unsigned char)((l & 0x00FF0000) >> 16);
1698 }
1699#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Sascha Hauer2e285322009-04-15 01:32:16 +00001701 cbd_base = (struct bufdesc *)mem_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 /* Set receive and transmit descriptor base.
1704 */
1705 fep->rx_bd_base = cbd_base;
1706 fep->tx_bd_base = cbd_base + RX_RING_SIZE;
1707
1708 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
1709 fep->cur_rx = fep->rx_bd_base;
1710
1711 fep->skb_cur = fep->skb_dirty = 0;
1712
1713 /* Initialize the receive buffer descriptors.
1714 */
1715 bdp = fep->rx_bd_base;
1716 for (i=0; i<FEC_ENET_RX_PAGES; i++) {
1717
1718 /* Allocate a page.
1719 */
1720 mem_addr = __get_free_page(GFP_KERNEL);
1721 /* XXX: missing check for allocation failure */
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 /* Initialize the BD for every fragment in the page.
1724 */
1725 for (j=0; j<FEC_ENET_RX_FRPPG; j++) {
1726 bdp->cbd_sc = BD_ENET_RX_EMPTY;
1727 bdp->cbd_bufaddr = __pa(mem_addr);
1728 mem_addr += FEC_ENET_RX_FRSIZE;
1729 bdp++;
1730 }
1731 }
1732
1733 /* Set the last buffer to wrap.
1734 */
1735 bdp--;
1736 bdp->cbd_sc |= BD_SC_WRAP;
1737
1738 /* ...and the same for transmmit.
1739 */
1740 bdp = fep->tx_bd_base;
1741 for (i=0, j=FEC_ENET_TX_FRPPG; i<TX_RING_SIZE; i++) {
1742 if (j >= FEC_ENET_TX_FRPPG) {
1743 mem_addr = __get_free_page(GFP_KERNEL);
1744 j = 1;
1745 } else {
1746 mem_addr += FEC_ENET_TX_FRSIZE;
1747 j++;
1748 }
1749 fep->tx_bounce[i] = (unsigned char *) mem_addr;
1750
1751 /* Initialize the BD for every fragment in the page.
1752 */
1753 bdp->cbd_sc = 0;
1754 bdp->cbd_bufaddr = 0;
1755 bdp++;
1756 }
1757
1758 /* Set the last buffer to wrap.
1759 */
1760 bdp--;
1761 bdp->cbd_sc |= BD_SC_WRAP;
1762
1763 /* Set receive and transmit descriptor base.
1764 */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001765 writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
Sascha Hauer2e285322009-04-15 01:32:16 +00001766 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc) * RX_RING_SIZE,
Sascha Hauerf44d6302009-04-15 03:11:30 +00001767 fep->hwp + FEC_X_DES_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Greg Ungerer43be6362009-02-26 22:42:51 -08001769#ifdef HAVE_mii_link_interrupt
1770 fec_request_mii_intr(dev);
Sascha Haueread73182009-01-28 23:03:11 +00001771#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Sascha Hauerf44d6302009-04-15 03:11:30 +00001773 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1774 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1775 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
1776 writel(2, fep->hwp + FEC_ECNTRL);
1777 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Greg Ungerercc462f72008-05-01 13:35:34 +10001778#ifndef CONFIG_M5272
Sascha Hauerf44d6302009-04-15 03:11:30 +00001779 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1780 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
Greg Ungerercc462f72008-05-01 13:35:34 +10001781#endif
Greg Ungerer562d2f82005-11-07 14:09:50 +10001782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 /* The FEC Ethernet specific entries in the device structure. */
1784 dev->open = fec_enet_open;
1785 dev->hard_start_xmit = fec_enet_start_xmit;
1786 dev->tx_timeout = fec_timeout;
1787 dev->watchdog_timeo = TX_TIMEOUT;
1788 dev->stop = fec_enet_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 dev->set_multicast_list = set_multicast_list;
1790
1791 for (i=0; i<NMII-1; i++)
1792 mii_cmds[i].mii_next = &mii_cmds[i+1];
1793 mii_free = mii_cmds;
1794
1795 /* setup MII interface */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001796 writel(OPT_FRAME_SIZE | 0x04, fep->hwp + FEC_R_CNTRL);
1797 writel(0, fep->hwp + FEC_X_CNTRL);
Sascha Haueread73182009-01-28 23:03:11 +00001798
1799 /*
1800 * Set MII speed to 2.5 MHz
1801 */
1802 fep->phy_speed = ((((clk_get_rate(fep->clk) / 2 + 4999999)
1803 / 2500000) / 2) & 0x3F) << 1;
Sascha Hauerf44d6302009-04-15 03:11:30 +00001804 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Sascha Haueread73182009-01-28 23:03:11 +00001805 fec_restart(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Matt Waddel6b265292006-06-27 13:10:56 +10001807 /* Clear and enable interrupts */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001808 writel(0xffc00000, fep->hwp + FEC_IEVENT);
1809 writel(FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII,
1810 fep->hwp + FEC_IMASK);
Matt Waddel6b265292006-06-27 13:10:56 +10001811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 /* Queue up command to detect the PHY and initialize the
1813 * remainder of the interface.
1814 */
1815 fep->phy_id_done = 0;
1816 fep->phy_addr = 0;
1817 mii_queue(dev, mk_mii_read(MII_REG_PHYIR1), mii_discover_phy);
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return 0;
1820}
1821
1822/* This function is called to start or restart the FEC during a link
1823 * change. This only happens when switching between half and full
1824 * duplex.
1825 */
1826static void
1827fec_restart(struct net_device *dev, int duplex)
1828{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001829 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer2e285322009-04-15 01:32:16 +00001830 struct bufdesc *bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 int i;
1832
Sascha Hauerf44d6302009-04-15 03:11:30 +00001833 /* Whack a reset. We should wait for this. */
1834 writel(1, fep->hwp + FEC_ECNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 udelay(10);
1836
Sascha Hauerf44d6302009-04-15 03:11:30 +00001837 /* Clear any outstanding interrupt. */
1838 writel(0xffc00000, fep->hwp + FEC_IEVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Sascha Hauerf44d6302009-04-15 03:11:30 +00001840 /* Set station address. */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001841 fec_set_mac_address(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Sascha Hauerf44d6302009-04-15 03:11:30 +00001843 /* Reset all multicast. */
1844 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1845 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Sascha Hauerf44d6302009-04-15 03:11:30 +00001847 /* Set maximum receive buffer size. */
1848 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Sascha Hauerf44d6302009-04-15 03:11:30 +00001850 /* Set receive and transmit descriptor base. */
1851 writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
Sascha Hauer2e285322009-04-15 01:32:16 +00001852 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc) * RX_RING_SIZE,
Sascha Hauerf44d6302009-04-15 03:11:30 +00001853 fep->hwp + FEC_X_DES_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
1856 fep->cur_rx = fep->rx_bd_base;
1857
Sascha Hauerf44d6302009-04-15 03:11:30 +00001858 /* Reset SKB transmit buffers. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 fep->skb_cur = fep->skb_dirty = 0;
1860 for (i=0; i<=TX_RING_MOD_MASK; i++) {
1861 if (fep->tx_skbuff[i] != NULL) {
1862 dev_kfree_skb_any(fep->tx_skbuff[i]);
1863 fep->tx_skbuff[i] = NULL;
1864 }
1865 }
1866
Sascha Hauerf44d6302009-04-15 03:11:30 +00001867 /* Initialize the receive buffer descriptors. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 bdp = fep->rx_bd_base;
1869 for (i=0; i<RX_RING_SIZE; i++) {
1870
Sascha Hauerf44d6302009-04-15 03:11:30 +00001871 /* Initialize the BD for every fragment in the page. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 bdp->cbd_sc = BD_ENET_RX_EMPTY;
1873 bdp++;
1874 }
1875
Sascha Hauerf44d6302009-04-15 03:11:30 +00001876 /* Set the last buffer to wrap. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 bdp--;
1878 bdp->cbd_sc |= BD_SC_WRAP;
1879
Sascha Hauerf44d6302009-04-15 03:11:30 +00001880 /* ...and the same for transmmit. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 bdp = fep->tx_bd_base;
1882 for (i=0; i<TX_RING_SIZE; i++) {
1883
Sascha Hauerf44d6302009-04-15 03:11:30 +00001884 /* Initialize the BD for every fragment in the page. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 bdp->cbd_sc = 0;
1886 bdp->cbd_bufaddr = 0;
1887 bdp++;
1888 }
1889
Sascha Hauerf44d6302009-04-15 03:11:30 +00001890 /* Set the last buffer to wrap. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 bdp--;
1892 bdp->cbd_sc |= BD_SC_WRAP;
1893
Sascha Hauerf44d6302009-04-15 03:11:30 +00001894 /* Enable MII mode. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (duplex) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001896 /* MII enable / FD enable */
1897 writel(OPT_FRAME_SIZE | 0x04, fep->hwp + FEC_R_CNTRL);
1898 writel(0x04, fep->hwp + FEC_X_CNTRL);
Philippe De Muyterf909b1e2007-10-23 14:37:54 +10001899 } else {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001900 /* MII enable / No Rcv on Xmit */
1901 writel(OPT_FRAME_SIZE | 0x06, fep->hwp + FEC_R_CNTRL);
1902 writel(0x0, fep->hwp + FEC_X_CNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
1904 fep->full_duplex = duplex;
1905
Sascha Hauerf44d6302009-04-15 03:11:30 +00001906 /* Set MII speed. */
1907 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Sascha Hauerf44d6302009-04-15 03:11:30 +00001909 /* And last, enable the transmit and receive processing. */
1910 writel(2, fep->hwp + FEC_ECNTRL);
1911 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Matt Waddel6b265292006-06-27 13:10:56 +10001912
Sascha Hauerf44d6302009-04-15 03:11:30 +00001913 /* Enable interrupts we wish to service. */
1914 writel(FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII,
1915 fep->hwp + FEC_IMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916}
1917
1918static void
1919fec_stop(struct net_device *dev)
1920{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001921 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Philippe De Muyter677177c2006-06-27 13:05:33 +10001923 /*
1924 ** We cannot expect a graceful transmit stop without link !!!
1925 */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001926 if (fep->link) {
1927 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
Philippe De Muyter677177c2006-06-27 13:05:33 +10001928 udelay(10);
Sascha Hauerf44d6302009-04-15 03:11:30 +00001929 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Philippe De Muyter677177c2006-06-27 13:05:33 +10001930 printk("fec_stop : Graceful transmit stop did not complete !\n");
Sascha Hauerf44d6302009-04-15 03:11:30 +00001931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Sascha Hauerf44d6302009-04-15 03:11:30 +00001933 /* Whack a reset. We should wait for this. */
1934 writel(1, fep->hwp + FEC_ECNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 udelay(10);
1936
Sascha Hauerf44d6302009-04-15 03:11:30 +00001937 /* Clear outstanding MII command interrupts. */
1938 writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Sascha Hauerf44d6302009-04-15 03:11:30 +00001940 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1941 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
1943
Sascha Haueread73182009-01-28 23:03:11 +00001944static int __devinit
1945fec_probe(struct platform_device *pdev)
1946{
1947 struct fec_enet_private *fep;
1948 struct net_device *ndev;
1949 int i, irq, ret = 0;
1950 struct resource *r;
1951
1952 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1953 if (!r)
1954 return -ENXIO;
1955
1956 r = request_mem_region(r->start, resource_size(r), pdev->name);
1957 if (!r)
1958 return -EBUSY;
1959
1960 /* Init network device */
1961 ndev = alloc_etherdev(sizeof(struct fec_enet_private));
1962 if (!ndev)
1963 return -ENOMEM;
1964
1965 SET_NETDEV_DEV(ndev, &pdev->dev);
1966
1967 /* setup board info structure */
1968 fep = netdev_priv(ndev);
1969 memset(fep, 0, sizeof(*fep));
1970
1971 ndev->base_addr = (unsigned long)ioremap(r->start, resource_size(r));
1972
1973 if (!ndev->base_addr) {
1974 ret = -ENOMEM;
1975 goto failed_ioremap;
1976 }
1977
1978 platform_set_drvdata(pdev, ndev);
1979
1980 /* This device has up to three irqs on some platforms */
1981 for (i = 0; i < 3; i++) {
1982 irq = platform_get_irq(pdev, i);
1983 if (i && irq < 0)
1984 break;
1985 ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
1986 if (ret) {
1987 while (i >= 0) {
1988 irq = platform_get_irq(pdev, i);
1989 free_irq(irq, ndev);
1990 i--;
1991 }
1992 goto failed_irq;
1993 }
1994 }
1995
1996 fep->clk = clk_get(&pdev->dev, "fec_clk");
1997 if (IS_ERR(fep->clk)) {
1998 ret = PTR_ERR(fep->clk);
1999 goto failed_clk;
2000 }
2001 clk_enable(fep->clk);
2002
2003 ret = fec_enet_init(ndev, 0);
2004 if (ret)
2005 goto failed_init;
2006
2007 ret = register_netdev(ndev);
2008 if (ret)
2009 goto failed_register;
2010
2011 return 0;
2012
2013failed_register:
2014failed_init:
2015 clk_disable(fep->clk);
2016 clk_put(fep->clk);
2017failed_clk:
2018 for (i = 0; i < 3; i++) {
2019 irq = platform_get_irq(pdev, i);
2020 if (irq > 0)
2021 free_irq(irq, ndev);
2022 }
2023failed_irq:
2024 iounmap((void __iomem *)ndev->base_addr);
2025failed_ioremap:
2026 free_netdev(ndev);
2027
2028 return ret;
2029}
2030
2031static int __devexit
2032fec_drv_remove(struct platform_device *pdev)
2033{
2034 struct net_device *ndev = platform_get_drvdata(pdev);
2035 struct fec_enet_private *fep = netdev_priv(ndev);
2036
2037 platform_set_drvdata(pdev, NULL);
2038
2039 fec_stop(ndev);
2040 clk_disable(fep->clk);
2041 clk_put(fep->clk);
2042 iounmap((void __iomem *)ndev->base_addr);
2043 unregister_netdev(ndev);
2044 free_netdev(ndev);
2045 return 0;
2046}
2047
2048static int
2049fec_suspend(struct platform_device *dev, pm_message_t state)
2050{
2051 struct net_device *ndev = platform_get_drvdata(dev);
2052 struct fec_enet_private *fep;
2053
2054 if (ndev) {
2055 fep = netdev_priv(ndev);
2056 if (netif_running(ndev)) {
2057 netif_device_detach(ndev);
2058 fec_stop(ndev);
2059 }
2060 }
2061 return 0;
2062}
2063
2064static int
2065fec_resume(struct platform_device *dev)
2066{
2067 struct net_device *ndev = platform_get_drvdata(dev);
2068
2069 if (ndev) {
2070 if (netif_running(ndev)) {
2071 fec_enet_init(ndev, 0);
2072 netif_device_attach(ndev);
2073 }
2074 }
2075 return 0;
2076}
2077
2078static struct platform_driver fec_driver = {
2079 .driver = {
2080 .name = "fec",
2081 .owner = THIS_MODULE,
2082 },
2083 .probe = fec_probe,
2084 .remove = __devexit_p(fec_drv_remove),
2085 .suspend = fec_suspend,
2086 .resume = fec_resume,
2087};
2088
2089static int __init
2090fec_enet_module_init(void)
2091{
2092 printk(KERN_INFO "FEC Ethernet Driver\n");
2093
2094 return platform_driver_register(&fec_driver);
2095}
2096
2097static void __exit
2098fec_enet_cleanup(void)
2099{
2100 platform_driver_unregister(&fec_driver);
2101}
2102
2103module_exit(fec_enet_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104module_init(fec_enet_module_init);
2105
2106MODULE_LICENSE("GPL");