blob: 672566b89ecfe3df2331e2f0c647cb409bd1be2a [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
Sascha Hauer22f6b862009-04-15 01:32:18 +000089/* Forward declarations of some structures to support different PHYs */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91typedef struct {
92 uint mii_data;
93 void (*funct)(uint mii_reg, struct net_device *dev);
94} phy_cmd_t;
95
96typedef struct {
97 uint id;
98 char *name;
99
100 const phy_cmd_t *config;
101 const phy_cmd_t *startup;
102 const phy_cmd_t *ack_int;
103 const phy_cmd_t *shutdown;
104} phy_info_t;
105
106/* The number of Tx and Rx buffers. These are allocated from the page
107 * pool. The code may assume these are power of two, so it it best
108 * to keep them that size.
109 * We don't need to allocate pages for the transmitter. We just use
110 * the skbuffer directly.
111 */
112#define FEC_ENET_RX_PAGES 8
113#define FEC_ENET_RX_FRSIZE 2048
114#define FEC_ENET_RX_FRPPG (PAGE_SIZE / FEC_ENET_RX_FRSIZE)
115#define RX_RING_SIZE (FEC_ENET_RX_FRPPG * FEC_ENET_RX_PAGES)
116#define FEC_ENET_TX_FRSIZE 2048
117#define FEC_ENET_TX_FRPPG (PAGE_SIZE / FEC_ENET_TX_FRSIZE)
118#define TX_RING_SIZE 16 /* Must be power of two */
119#define TX_RING_MOD_MASK 15 /* for this to work */
120
Greg Ungerer562d2f82005-11-07 14:09:50 +1000121#if (((RX_RING_SIZE + TX_RING_SIZE) * 8) > PAGE_SIZE)
Matt Waddel6b265292006-06-27 13:10:56 +1000122#error "FEC: descriptor ring size constants too large"
Greg Ungerer562d2f82005-11-07 14:09:50 +1000123#endif
124
Sascha Hauer22f6b862009-04-15 01:32:18 +0000125/* Interrupt events/masks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
127#define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
128#define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
129#define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
130#define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
131#define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
132#define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
133#define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
134#define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
135#define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
136
137/* The FEC stores dest/src/type, data, and checksum for receive packets.
138 */
139#define PKT_MAXBUF_SIZE 1518
140#define PKT_MINBUF_SIZE 64
141#define PKT_MAXBLR_SIZE 1520
142
143
144/*
Matt Waddel6b265292006-06-27 13:10:56 +1000145 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 * size bits. Other FEC hardware does not, so we need to take that into
147 * account when setting it.
148 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000149#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Sascha Hauer196719e2009-01-28 23:03:10 +0000150 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARCH_MXC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
152#else
153#define OPT_FRAME_SIZE 0
154#endif
155
156/* The FEC buffer descriptors track the ring buffers. The rx_bd_base and
157 * tx_bd_base always point to the base of the buffer descriptors. The
158 * cur_rx and cur_tx point to the currently available buffer.
159 * The dirty_tx tracks the current buffer that is being sent by the
160 * controller. The cur_tx and dirty_tx are equal under both completely
161 * empty and completely full conditions. The empty/ready indicator in
162 * the buffer descriptor determines the actual condition.
163 */
164struct fec_enet_private {
165 /* Hardware registers of the FEC device */
Sascha Hauerf44d6302009-04-15 03:11:30 +0000166 void __iomem *hwp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Greg Ungerercb84d6e2007-07-30 16:29:09 +1000168 struct net_device *netdev;
169
Sascha Haueread73182009-01-28 23:03:11 +0000170 struct clk *clk;
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 /* The saved address of a sent-in-place packet/buffer, for skfree(). */
173 unsigned char *tx_bounce[TX_RING_SIZE];
174 struct sk_buff* tx_skbuff[TX_RING_SIZE];
175 ushort skb_cur;
176 ushort skb_dirty;
177
Sascha Hauer22f6b862009-04-15 01:32:18 +0000178 /* CPM dual port RAM relative addresses */
Sascha Hauer4661e752009-01-28 23:03:07 +0000179 dma_addr_t bd_dma;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000180 /* Address of Rx and Tx buffers */
Sascha Hauer2e285322009-04-15 01:32:16 +0000181 struct bufdesc *rx_bd_base;
182 struct bufdesc *tx_bd_base;
183 /* The next free ring entry */
184 struct bufdesc *cur_rx, *cur_tx;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000185 /* The ring entries to be free()ed */
Sascha Hauer2e285322009-04-15 01:32:16 +0000186 struct bufdesc *dirty_tx;
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 uint tx_full;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000189 /* hold while accessing the HW like ringbuffer for tx/rx but not MAC */
190 spinlock_t hw_lock;
191 /* hold while accessing the mii_list_t() elements */
192 spinlock_t mii_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 uint phy_id;
195 uint phy_id_done;
196 uint phy_status;
197 uint phy_speed;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000198 phy_info_t const *phy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 struct work_struct phy_task;
200
201 uint sequence_done;
202 uint mii_phy_task_queued;
203
204 uint phy_addr;
205
206 int index;
207 int opened;
208 int link;
209 int old_link;
210 int full_duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211};
212
213static int fec_enet_open(struct net_device *dev);
214static int fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev);
215static void fec_enet_mii(struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100216static irqreturn_t fec_enet_interrupt(int irq, void * dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217static void fec_enet_tx(struct net_device *dev);
218static void fec_enet_rx(struct net_device *dev);
219static int fec_enet_close(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220static void set_multicast_list(struct net_device *dev);
221static void fec_restart(struct net_device *dev, int duplex);
222static void fec_stop(struct net_device *dev);
223static void fec_set_mac_address(struct net_device *dev);
224
225
226/* MII processing. We keep this as simple as possible. Requests are
227 * placed on the list (if there is room). When the request is finished
228 * by the MII, an optional function may be called.
229 */
230typedef struct mii_list {
231 uint mii_regval;
232 void (*mii_func)(uint val, struct net_device *dev);
233 struct mii_list *mii_next;
234} mii_list_t;
235
236#define NMII 20
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000237static mii_list_t mii_cmds[NMII];
238static mii_list_t *mii_free;
239static mii_list_t *mii_head;
240static mii_list_t *mii_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400242static int mii_queue(struct net_device *dev, int request,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 void (*func)(uint, struct net_device *));
244
Sascha Hauer22f6b862009-04-15 01:32:18 +0000245/* Make MII read/write commands for the FEC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246#define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18))
247#define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | \
248 (VAL & 0xffff))
249#define mk_mii_end 0
250
Sascha Hauer22f6b862009-04-15 01:32:18 +0000251/* Transmitter timeout */
252#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Sascha Hauer22f6b862009-04-15 01:32:18 +0000254/* Register definitions for the PHY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256#define MII_REG_CR 0 /* Control Register */
257#define MII_REG_SR 1 /* Status Register */
258#define MII_REG_PHYIR1 2 /* PHY Identification Register 1 */
259#define MII_REG_PHYIR2 3 /* PHY Identification Register 2 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400260#define MII_REG_ANAR 4 /* A-N Advertisement Register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261#define MII_REG_ANLPAR 5 /* A-N Link Partner Ability Register */
262#define MII_REG_ANER 6 /* A-N Expansion Register */
263#define MII_REG_ANNPTR 7 /* A-N Next Page Transmit Register */
264#define MII_REG_ANLPRNPR 8 /* A-N Link Partner Received Next Page Reg. */
265
266/* values for phy_status */
267
268#define PHY_CONF_ANE 0x0001 /* 1 auto-negotiation enabled */
269#define PHY_CONF_LOOP 0x0002 /* 1 loopback mode enabled */
270#define PHY_CONF_SPMASK 0x00f0 /* mask for speed */
271#define PHY_CONF_10HDX 0x0010 /* 10 Mbit half duplex supported */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400272#define PHY_CONF_10FDX 0x0020 /* 10 Mbit full duplex supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273#define PHY_CONF_100HDX 0x0040 /* 100 Mbit half duplex supported */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400274#define PHY_CONF_100FDX 0x0080 /* 100 Mbit full duplex supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276#define PHY_STAT_LINK 0x0100 /* 1 up - 0 down */
277#define PHY_STAT_FAULT 0x0200 /* 1 remote fault */
278#define PHY_STAT_ANC 0x0400 /* 1 auto-negotiation complete */
279#define PHY_STAT_SPMASK 0xf000 /* mask for speed */
280#define PHY_STAT_10HDX 0x1000 /* 10 Mbit half duplex selected */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400281#define PHY_STAT_10FDX 0x2000 /* 10 Mbit full duplex selected */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282#define PHY_STAT_100HDX 0x4000 /* 100 Mbit half duplex selected */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400283#define PHY_STAT_100FDX 0x8000 /* 100 Mbit full duplex selected */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285
286static int
287fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
288{
Sascha Hauerf44d6302009-04-15 03:11:30 +0000289 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000290 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000291 unsigned short status;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000292 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (!fep->link) {
295 /* Link is down or autonegotiation is in progress. */
296 return 1;
297 }
298
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000299 spin_lock_irqsave(&fep->hw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 /* Fill in a Tx ring entry */
301 bdp = fep->cur_tx;
302
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000303 status = bdp->cbd_sc;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000304
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000305 if (status & BD_ENET_TX_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 /* Ooops. All transmit buffers are full. Bail out.
307 * This should not happen, since dev->tbusy should be set.
308 */
309 printk("%s: tx queue full!.\n", dev->name);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000310 spin_unlock_irqrestore(&fep->hw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return 1;
312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Sascha Hauer22f6b862009-04-15 01:32:18 +0000314 /* Clear all of the status flags */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000315 status &= ~BD_ENET_TX_STATS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Sascha Hauer22f6b862009-04-15 01:32:18 +0000317 /* Set buffer length and buffer pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 bdp->cbd_bufaddr = __pa(skb->data);
319 bdp->cbd_datlen = skb->len;
320
321 /*
Sascha Hauer22f6b862009-04-15 01:32:18 +0000322 * On some FEC implementations data must be aligned on
323 * 4-byte boundaries. Use bounce buffers to copy data
324 * and get it aligned. Ugh.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 */
Sascha Hauer196719e2009-01-28 23:03:10 +0000326 if (bdp->cbd_bufaddr & FEC_ALIGNMENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 unsigned int index;
328 index = bdp - fep->tx_bd_base;
Sascha Hauer6989f512009-01-28 23:03:06 +0000329 memcpy(fep->tx_bounce[index], (void *)skb->data, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 bdp->cbd_bufaddr = __pa(fep->tx_bounce[index]);
331 }
332
Sascha Hauer22f6b862009-04-15 01:32:18 +0000333 /* Save skb pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 fep->tx_skbuff[fep->skb_cur] = skb;
335
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700336 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 fep->skb_cur = (fep->skb_cur+1) & TX_RING_MOD_MASK;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 /* Push the data cache so the CPM does not get stale memory
340 * data.
341 */
Sascha Hauerccdc4f12009-01-28 23:03:09 +0000342 dma_sync_single(NULL, bdp->cbd_bufaddr,
343 bdp->cbd_datlen, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000345 /* Send it on its way. Tell FEC it's ready, interrupt when done,
346 * it's the last BD of the frame, and to put the CRC on the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000348 status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 | BD_ENET_TX_LAST | BD_ENET_TX_TC);
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000350 bdp->cbd_sc = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 dev->trans_start = jiffies;
353
354 /* Trigger transmission start */
Sascha Hauerf44d6302009-04-15 03:11:30 +0000355 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Sascha Hauer22f6b862009-04-15 01:32:18 +0000357 /* If this was the last BD in the ring, start at the beginning again. */
358 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 bdp = fep->tx_bd_base;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000360 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 bdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 if (bdp == fep->dirty_tx) {
364 fep->tx_full = 1;
365 netif_stop_queue(dev);
366 }
367
Sascha Hauer2e285322009-04-15 01:32:16 +0000368 fep->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000370 spin_unlock_irqrestore(&fep->hw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 return 0;
373}
374
375static void
376fec_timeout(struct net_device *dev)
377{
378 struct fec_enet_private *fep = netdev_priv(dev);
379
380 printk("%s: transmit timed out.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700381 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382#ifndef final_version
383 {
384 int i;
Sascha Hauer2e285322009-04-15 01:32:16 +0000385 struct bufdesc *bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 printk("Ring data dump: cur_tx %lx%s, dirty_tx %lx cur_rx: %lx\n",
388 (unsigned long)fep->cur_tx, fep->tx_full ? " (full)" : "",
389 (unsigned long)fep->dirty_tx,
390 (unsigned long)fep->cur_rx);
391
392 bdp = fep->tx_bd_base;
393 printk(" tx: %u buffers\n", TX_RING_SIZE);
394 for (i = 0 ; i < TX_RING_SIZE; i++) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400395 printk(" %08x: %04x %04x %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 (uint) bdp,
397 bdp->cbd_sc,
398 bdp->cbd_datlen,
399 (int) bdp->cbd_bufaddr);
400 bdp++;
401 }
402
403 bdp = fep->rx_bd_base;
404 printk(" rx: %lu buffers\n", (unsigned long) RX_RING_SIZE);
405 for (i = 0 ; i < RX_RING_SIZE; i++) {
406 printk(" %08x: %04x %04x %08x\n",
407 (uint) bdp,
408 bdp->cbd_sc,
409 bdp->cbd_datlen,
410 (int) bdp->cbd_bufaddr);
411 bdp++;
412 }
413 }
414#endif
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000415 fec_restart(dev, fep->full_duplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 netif_wake_queue(dev);
417}
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100420fec_enet_interrupt(int irq, void * dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 struct net_device *dev = dev_id;
Sascha Hauerf44d6302009-04-15 03:11:30 +0000423 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 uint int_events;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000425 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000427 do {
Sascha Hauerf44d6302009-04-15 03:11:30 +0000428 int_events = readl(fep->hwp + FEC_IEVENT);
429 writel(int_events, fep->hwp + FEC_IEVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (int_events & FEC_ENET_RXF) {
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000432 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 fec_enet_rx(dev);
434 }
435
436 /* Transmit OK, or non-fatal error. Update the buffer
Sascha Hauerf44d6302009-04-15 03:11:30 +0000437 * descriptors. FEC handles all errors, we just discover
438 * them as part of the transmit process.
439 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (int_events & FEC_ENET_TXF) {
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000441 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 fec_enet_tx(dev);
443 }
444
445 if (int_events & FEC_ENET_MII) {
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000446 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 fec_enet_mii(dev);
448 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400449
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000450 } while (int_events);
451
452 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
455
456static void
457fec_enet_tx(struct net_device *dev)
458{
459 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +0000460 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000461 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 struct sk_buff *skb;
463
464 fep = netdev_priv(dev);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000465 spin_lock_irq(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 bdp = fep->dirty_tx;
467
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000468 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (bdp == fep->cur_tx && fep->tx_full == 0) break;
470
471 skb = fep->tx_skbuff[fep->skb_dirty];
472 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000473 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 BD_ENET_TX_RL | BD_ENET_TX_UN |
475 BD_ENET_TX_CSL)) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700476 dev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000477 if (status & BD_ENET_TX_HB) /* No heartbeat */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700478 dev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000479 if (status & BD_ENET_TX_LC) /* Late collision */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700480 dev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000481 if (status & BD_ENET_TX_RL) /* Retrans limit */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700482 dev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000483 if (status & BD_ENET_TX_UN) /* Underrun */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700484 dev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000485 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700486 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 } else {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700488 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000491 if (status & BD_ENET_TX_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 printk("HEY! Enet xmit interrupt and TX_READY.\n");
Sascha Hauer22f6b862009-04-15 01:32:18 +0000493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 /* Deferred means some collisions occurred during transmit,
495 * but we eventually sent the packet OK.
496 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000497 if (status & BD_ENET_TX_DEF)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700498 dev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400499
Sascha Hauer22f6b862009-04-15 01:32:18 +0000500 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 dev_kfree_skb_any(skb);
502 fep->tx_skbuff[fep->skb_dirty] = NULL;
503 fep->skb_dirty = (fep->skb_dirty + 1) & TX_RING_MOD_MASK;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400504
Sascha Hauer22f6b862009-04-15 01:32:18 +0000505 /* Update pointer to next buffer descriptor to be transmitted */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000506 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 bdp = fep->tx_bd_base;
508 else
509 bdp++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400510
Sascha Hauer22f6b862009-04-15 01:32:18 +0000511 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 */
513 if (fep->tx_full) {
514 fep->tx_full = 0;
515 if (netif_queue_stopped(dev))
516 netif_wake_queue(dev);
517 }
518 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000519 fep->dirty_tx = bdp;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000520 spin_unlock_irq(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
523
524/* During a receive, the cur_rx points to the current incoming buffer.
525 * When we update through the ring, if the next incoming buffer has
526 * not been given to the system, we just set the empty indicator,
527 * effectively tossing the packet.
528 */
529static void
530fec_enet_rx(struct net_device *dev)
531{
Sascha Hauerf44d6302009-04-15 03:11:30 +0000532 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000533 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000534 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 struct sk_buff *skb;
536 ushort pkt_len;
537 __u8 *data;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400538
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000539#ifdef CONFIG_M532x
540 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400541#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000543 spin_lock_irq(&fep->hw_lock);
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 /* First, grab all of the stats for the incoming packet.
546 * These get messed up if we get called due to a busy condition.
547 */
548 bdp = fep->cur_rx;
549
Sascha Hauer22f6b862009-04-15 01:32:18 +0000550 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Sascha Hauer22f6b862009-04-15 01:32:18 +0000552 /* Since we have allocated space to hold a complete frame,
553 * the last indicator should be set.
554 */
555 if ((status & BD_ENET_RX_LAST) == 0)
556 printk("FEC ENET: rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Sascha Hauer22f6b862009-04-15 01:32:18 +0000558 if (!fep->opened)
559 goto rx_processing_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Sascha Hauer22f6b862009-04-15 01:32:18 +0000561 /* Check for errors. */
562 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Sascha Hauer22f6b862009-04-15 01:32:18 +0000564 dev->stats.rx_errors++;
565 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
566 /* Frame too long or too short. */
567 dev->stats.rx_length_errors++;
568 }
569 if (status & BD_ENET_RX_NO) /* Frame alignment */
570 dev->stats.rx_frame_errors++;
571 if (status & BD_ENET_RX_CR) /* CRC Error */
572 dev->stats.rx_crc_errors++;
573 if (status & BD_ENET_RX_OV) /* FIFO overrun */
574 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
Sascha Hauer22f6b862009-04-15 01:32:18 +0000576
577 /* Report late collisions as a frame error.
578 * On this error, the BD is closed, but we don't know what we
579 * have in the buffer. So, just drop this frame on the floor.
580 */
581 if (status & BD_ENET_RX_CL) {
582 dev->stats.rx_errors++;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700583 dev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000584 goto rx_processing_done;
585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Sascha Hauer22f6b862009-04-15 01:32:18 +0000587 /* Process the incoming frame. */
588 dev->stats.rx_packets++;
589 pkt_len = bdp->cbd_datlen;
590 dev->stats.rx_bytes += pkt_len;
591 data = (__u8*)__va(bdp->cbd_bufaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Sascha Hauer22f6b862009-04-15 01:32:18 +0000593 dma_sync_single(NULL, (unsigned long)__pa(data),
Sascha Hauerccdc4f12009-01-28 23:03:09 +0000594 pkt_len - 4, DMA_FROM_DEVICE);
595
Sascha Hauer22f6b862009-04-15 01:32:18 +0000596 /* This does 16 byte alignment, exactly what we need.
597 * The packet length includes FCS, but we don't want to
598 * include that when passing upstream as it messes up
599 * bridging applications.
600 */
601 skb = dev_alloc_skb(pkt_len - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Sascha Hauer22f6b862009-04-15 01:32:18 +0000603 if (skb == NULL) {
604 printk("%s: Memory squeeze, dropping packet.\n",
605 dev->name);
606 dev->stats.rx_dropped++;
607 } else {
608 skb_put(skb, pkt_len - 4); /* Make room */
609 skb_copy_to_linear_data(skb, data, pkt_len - 4);
610 skb->protocol = eth_type_trans(skb, dev);
611 netif_rx(skb);
612 }
613rx_processing_done:
614 /* Clear the status flags for this buffer */
615 status &= ~BD_ENET_RX_STATS;
616
617 /* Mark the buffer empty */
618 status |= BD_ENET_RX_EMPTY;
619 bdp->cbd_sc = status;
620
621 /* Update BD pointer to next entry */
622 if (status & BD_ENET_RX_WRAP)
623 bdp = fep->rx_bd_base;
624 else
625 bdp++;
626 /* Doing this here will keep the FEC running while we process
627 * incoming frames. On a heavily loaded network, we should be
628 * able to keep up at the expense of system resources.
629 */
630 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000632 fep->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000634 spin_unlock_irq(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000637/* called from interrupt context */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638static void
639fec_enet_mii(struct net_device *dev)
640{
641 struct fec_enet_private *fep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 mii_list_t *mip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 fep = netdev_priv(dev);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000645 spin_lock_irq(&fep->mii_lock);
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if ((mip = mii_head) == NULL) {
648 printk("MII and no head!\n");
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000649 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651
652 if (mip->mii_func != NULL)
Sascha Hauerf44d6302009-04-15 03:11:30 +0000653 (*(mip->mii_func))(readl(fep->hwp + FEC_MII_DATA), dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 mii_head = mip->mii_next;
656 mip->mii_next = mii_free;
657 mii_free = mip;
658
659 if ((mip = mii_head) != NULL)
Sascha Hauerf44d6302009-04-15 03:11:30 +0000660 writel(mip->mii_regval, fep->hwp + FEC_MII_DATA);
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000661
662unlock:
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000663 spin_unlock_irq(&fep->mii_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
666static int
667mii_queue(struct net_device *dev, int regval, void (*func)(uint, struct net_device *))
668{
669 struct fec_enet_private *fep;
670 unsigned long flags;
671 mii_list_t *mip;
672 int retval;
673
Sascha Hauer22f6b862009-04-15 01:32:18 +0000674 /* Add PHY address to register command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 fep = netdev_priv(dev);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000676 spin_lock_irqsave(&fep->mii_lock, flags);
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 regval |= fep->phy_addr << 23;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 retval = 0;
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if ((mip = mii_free) != NULL) {
682 mii_free = mip->mii_next;
683 mip->mii_regval = regval;
684 mip->mii_func = func;
685 mip->mii_next = NULL;
686 if (mii_head) {
687 mii_tail->mii_next = mip;
688 mii_tail = mip;
Philippe De Muyterf909b1e2007-10-23 14:37:54 +1000689 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 mii_head = mii_tail = mip;
Sascha Hauerf44d6302009-04-15 03:11:30 +0000691 writel(regval, fep->hwp + FEC_MII_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
Philippe De Muyterf909b1e2007-10-23 14:37:54 +1000693 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 retval = 1;
695 }
696
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000697 spin_unlock_irqrestore(&fep->mii_lock, flags);
698 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
701static void mii_do_cmd(struct net_device *dev, const phy_cmd_t *c)
702{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if(!c)
704 return;
705
Philippe De Muyterbe6cb662007-10-23 14:37:54 +1000706 for (; c->mii_data != mk_mii_end; c++)
707 mii_queue(dev, c->mii_data, c->funct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710static void mii_parse_sr(uint mii_reg, struct net_device *dev)
711{
712 struct fec_enet_private *fep = netdev_priv(dev);
713 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000714 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000716 status = *s & ~(PHY_STAT_LINK | PHY_STAT_FAULT | PHY_STAT_ANC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 if (mii_reg & 0x0004)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000719 status |= PHY_STAT_LINK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (mii_reg & 0x0010)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000721 status |= PHY_STAT_FAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (mii_reg & 0x0020)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000723 status |= PHY_STAT_ANC;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000724 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
727static void mii_parse_cr(uint mii_reg, struct net_device *dev)
728{
729 struct fec_enet_private *fep = netdev_priv(dev);
730 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000731 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000733 status = *s & ~(PHY_CONF_ANE | PHY_CONF_LOOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 if (mii_reg & 0x1000)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000736 status |= PHY_CONF_ANE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (mii_reg & 0x4000)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000738 status |= PHY_CONF_LOOP;
739 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
742static void mii_parse_anar(uint mii_reg, struct net_device *dev)
743{
744 struct fec_enet_private *fep = netdev_priv(dev);
745 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000746 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000748 status = *s & ~(PHY_CONF_SPMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 if (mii_reg & 0x0020)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000751 status |= PHY_CONF_10HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (mii_reg & 0x0040)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000753 status |= PHY_CONF_10FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (mii_reg & 0x0080)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000755 status |= PHY_CONF_100HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (mii_reg & 0x00100)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000757 status |= PHY_CONF_100FDX;
758 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
761/* ------------------------------------------------------------------------- */
762/* The Level one LXT970 is used by many boards */
763
764#define MII_LXT970_MIRROR 16 /* Mirror register */
765#define MII_LXT970_IER 17 /* Interrupt Enable Register */
766#define MII_LXT970_ISR 18 /* Interrupt Status Register */
767#define MII_LXT970_CONFIG 19 /* Configuration Register */
768#define MII_LXT970_CSR 20 /* Chip Status Register */
769
770static void mii_parse_lxt970_csr(uint mii_reg, struct net_device *dev)
771{
772 struct fec_enet_private *fep = netdev_priv(dev);
773 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000774 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000776 status = *s & ~(PHY_STAT_SPMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (mii_reg & 0x0800) {
778 if (mii_reg & 0x1000)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000779 status |= PHY_STAT_100FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000781 status |= PHY_STAT_100HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 } else {
783 if (mii_reg & 0x1000)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000784 status |= PHY_STAT_10FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000786 status |= PHY_STAT_10HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000788 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000791static phy_cmd_t const phy_cmd_lxt970_config[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 { mk_mii_read(MII_REG_CR), mii_parse_cr },
793 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
794 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000795 };
796static phy_cmd_t const phy_cmd_lxt970_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 { mk_mii_write(MII_LXT970_IER, 0x0002), NULL },
798 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
799 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000800 };
801static phy_cmd_t const phy_cmd_lxt970_ack_int[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 /* read SR and ISR to acknowledge */
803 { mk_mii_read(MII_REG_SR), mii_parse_sr },
804 { mk_mii_read(MII_LXT970_ISR), NULL },
805
806 /* find out the current status */
807 { mk_mii_read(MII_LXT970_CSR), mii_parse_lxt970_csr },
808 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000809 };
810static phy_cmd_t const phy_cmd_lxt970_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 { mk_mii_write(MII_LXT970_IER, 0x0000), NULL },
812 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000813 };
814static phy_info_t const phy_info_lxt970 = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400815 .id = 0x07810000,
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000816 .name = "LXT970",
817 .config = phy_cmd_lxt970_config,
818 .startup = phy_cmd_lxt970_startup,
819 .ack_int = phy_cmd_lxt970_ack_int,
820 .shutdown = phy_cmd_lxt970_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821};
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823/* ------------------------------------------------------------------------- */
824/* The Level one LXT971 is used on some of my custom boards */
825
826/* register definitions for the 971 */
827
828#define MII_LXT971_PCR 16 /* Port Control Register */
829#define MII_LXT971_SR2 17 /* Status Register 2 */
830#define MII_LXT971_IER 18 /* Interrupt Enable Register */
831#define MII_LXT971_ISR 19 /* Interrupt Status Register */
832#define MII_LXT971_LCR 20 /* LED Control Register */
833#define MII_LXT971_TCR 30 /* Transmit Control Register */
834
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400835/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 * I had some nice ideas of running the MDIO faster...
837 * The 971 should support 8MHz and I tried it, but things acted really
838 * weird, so 2.5 MHz ought to be enough for anyone...
839 */
840
841static void mii_parse_lxt971_sr2(uint mii_reg, struct net_device *dev)
842{
843 struct fec_enet_private *fep = netdev_priv(dev);
844 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000845 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000847 status = *s & ~(PHY_STAT_SPMASK | PHY_STAT_LINK | PHY_STAT_ANC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 if (mii_reg & 0x0400) {
850 fep->link = 1;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000851 status |= PHY_STAT_LINK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 } else {
853 fep->link = 0;
854 }
855 if (mii_reg & 0x0080)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000856 status |= PHY_STAT_ANC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (mii_reg & 0x4000) {
858 if (mii_reg & 0x0200)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000859 status |= PHY_STAT_100FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000861 status |= PHY_STAT_100HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 } else {
863 if (mii_reg & 0x0200)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000864 status |= PHY_STAT_10FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000866 status |= PHY_STAT_10HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868 if (mii_reg & 0x0008)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000869 status |= PHY_STAT_FAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000871 *s = status;
872}
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400873
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000874static phy_cmd_t const phy_cmd_lxt971_config[] = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400875 /* limit to 10MBit because my prototype board
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 * doesn't work with 100. */
877 { mk_mii_read(MII_REG_CR), mii_parse_cr },
878 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
879 { mk_mii_read(MII_LXT971_SR2), mii_parse_lxt971_sr2 },
880 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000881 };
882static phy_cmd_t const phy_cmd_lxt971_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 { mk_mii_write(MII_LXT971_IER, 0x00f2), NULL },
884 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
885 { mk_mii_write(MII_LXT971_LCR, 0xd422), NULL }, /* LED config */
886 /* Somehow does the 971 tell me that the link is down
887 * the first read after power-up.
888 * read here to get a valid value in ack_int */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400889 { mk_mii_read(MII_REG_SR), mii_parse_sr },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000891 };
892static phy_cmd_t const phy_cmd_lxt971_ack_int[] = {
893 /* acknowledge the int before reading status ! */
894 { mk_mii_read(MII_LXT971_ISR), NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 /* find out the current status */
896 { mk_mii_read(MII_REG_SR), mii_parse_sr },
897 { mk_mii_read(MII_LXT971_SR2), mii_parse_lxt971_sr2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000899 };
900static phy_cmd_t const phy_cmd_lxt971_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 { mk_mii_write(MII_LXT971_IER, 0x0000), NULL },
902 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000903 };
904static phy_info_t const phy_info_lxt971 = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400905 .id = 0x0001378e,
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000906 .name = "LXT971",
907 .config = phy_cmd_lxt971_config,
908 .startup = phy_cmd_lxt971_startup,
909 .ack_int = phy_cmd_lxt971_ack_int,
910 .shutdown = phy_cmd_lxt971_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911};
912
913/* ------------------------------------------------------------------------- */
914/* The Quality Semiconductor QS6612 is used on the RPX CLLF */
915
916/* register definitions */
917
918#define MII_QS6612_MCR 17 /* Mode Control Register */
919#define MII_QS6612_FTR 27 /* Factory Test Register */
920#define MII_QS6612_MCO 28 /* Misc. Control Register */
921#define MII_QS6612_ISR 29 /* Interrupt Source Register */
922#define MII_QS6612_IMR 30 /* Interrupt Mask Register */
923#define MII_QS6612_PCR 31 /* 100BaseTx PHY Control Reg. */
924
925static void mii_parse_qs6612_pcr(uint mii_reg, struct net_device *dev)
926{
927 struct fec_enet_private *fep = netdev_priv(dev);
928 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000929 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000931 status = *s & ~(PHY_STAT_SPMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 switch((mii_reg >> 2) & 7) {
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000934 case 1: status |= PHY_STAT_10HDX; break;
935 case 2: status |= PHY_STAT_100HDX; break;
936 case 5: status |= PHY_STAT_10FDX; break;
937 case 6: status |= PHY_STAT_100FDX; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000940 *s = status;
941}
942
943static phy_cmd_t const phy_cmd_qs6612_config[] = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400944 /* The PHY powers up isolated on the RPX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 * so send a command to allow operation.
946 */
947 { mk_mii_write(MII_QS6612_PCR, 0x0dc0), NULL },
948
949 /* parse cr and anar to get some info */
950 { mk_mii_read(MII_REG_CR), mii_parse_cr },
951 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
952 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000953 };
954static phy_cmd_t const phy_cmd_qs6612_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 { mk_mii_write(MII_QS6612_IMR, 0x003a), NULL },
956 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
957 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000958 };
959static phy_cmd_t const phy_cmd_qs6612_ack_int[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 /* we need to read ISR, SR and ANER to acknowledge */
961 { mk_mii_read(MII_QS6612_ISR), NULL },
962 { mk_mii_read(MII_REG_SR), mii_parse_sr },
963 { mk_mii_read(MII_REG_ANER), NULL },
964
965 /* read pcr to get info */
966 { mk_mii_read(MII_QS6612_PCR), mii_parse_qs6612_pcr },
967 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000968 };
969static phy_cmd_t const phy_cmd_qs6612_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 { mk_mii_write(MII_QS6612_IMR, 0x0000), NULL },
971 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000972 };
973static phy_info_t const phy_info_qs6612 = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400974 .id = 0x00181440,
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000975 .name = "QS6612",
976 .config = phy_cmd_qs6612_config,
977 .startup = phy_cmd_qs6612_startup,
978 .ack_int = phy_cmd_qs6612_ack_int,
979 .shutdown = phy_cmd_qs6612_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980};
981
982/* ------------------------------------------------------------------------- */
983/* AMD AM79C874 phy */
984
985/* register definitions for the 874 */
986
987#define MII_AM79C874_MFR 16 /* Miscellaneous Feature Register */
988#define MII_AM79C874_ICSR 17 /* Interrupt/Status Register */
989#define MII_AM79C874_DR 18 /* Diagnostic Register */
990#define MII_AM79C874_PMLR 19 /* Power and Loopback Register */
991#define MII_AM79C874_MCR 21 /* ModeControl Register */
992#define MII_AM79C874_DC 23 /* Disconnect Counter */
993#define MII_AM79C874_REC 24 /* Recieve Error Counter */
994
995static void mii_parse_am79c874_dr(uint mii_reg, struct net_device *dev)
996{
997 struct fec_enet_private *fep = netdev_priv(dev);
998 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000999 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001001 status = *s & ~(PHY_STAT_SPMASK | PHY_STAT_ANC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 if (mii_reg & 0x0080)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001004 status |= PHY_STAT_ANC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (mii_reg & 0x0400)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001006 status |= ((mii_reg & 0x0800) ? PHY_STAT_100FDX : PHY_STAT_100HDX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001008 status |= ((mii_reg & 0x0800) ? PHY_STAT_10FDX : PHY_STAT_10HDX);
1009
1010 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001013static phy_cmd_t const phy_cmd_am79c874_config[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1015 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1016 { mk_mii_read(MII_AM79C874_DR), mii_parse_am79c874_dr },
1017 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001018 };
1019static phy_cmd_t const phy_cmd_am79c874_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 { mk_mii_write(MII_AM79C874_ICSR, 0xff00), NULL },
1021 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001022 { mk_mii_read(MII_REG_SR), mii_parse_sr },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001024 };
1025static phy_cmd_t const phy_cmd_am79c874_ack_int[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 /* find out the current status */
1027 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1028 { mk_mii_read(MII_AM79C874_DR), mii_parse_am79c874_dr },
1029 /* we only need to read ISR to acknowledge */
1030 { mk_mii_read(MII_AM79C874_ICSR), NULL },
1031 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001032 };
1033static phy_cmd_t const phy_cmd_am79c874_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 { mk_mii_write(MII_AM79C874_ICSR, 0x0000), NULL },
1035 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001036 };
1037static phy_info_t const phy_info_am79c874 = {
1038 .id = 0x00022561,
1039 .name = "AM79C874",
1040 .config = phy_cmd_am79c874_config,
1041 .startup = phy_cmd_am79c874_startup,
1042 .ack_int = phy_cmd_am79c874_ack_int,
1043 .shutdown = phy_cmd_am79c874_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044};
1045
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047/* ------------------------------------------------------------------------- */
1048/* Kendin KS8721BL phy */
1049
1050/* register definitions for the 8721 */
1051
1052#define MII_KS8721BL_RXERCR 21
Sascha Hauer43268dc2009-01-28 23:03:08 +00001053#define MII_KS8721BL_ICSR 27
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054#define MII_KS8721BL_PHYCR 31
1055
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001056static phy_cmd_t const phy_cmd_ks8721bl_config[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1058 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1059 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001060 };
1061static phy_cmd_t const phy_cmd_ks8721bl_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 { mk_mii_write(MII_KS8721BL_ICSR, 0xff00), NULL },
1063 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001064 { mk_mii_read(MII_REG_SR), mii_parse_sr },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001066 };
1067static phy_cmd_t const phy_cmd_ks8721bl_ack_int[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 /* find out the current status */
1069 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1070 /* we only need to read ISR to acknowledge */
1071 { mk_mii_read(MII_KS8721BL_ICSR), NULL },
1072 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001073 };
1074static phy_cmd_t const phy_cmd_ks8721bl_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 { mk_mii_write(MII_KS8721BL_ICSR, 0x0000), NULL },
1076 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001077 };
1078static phy_info_t const phy_info_ks8721bl = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001079 .id = 0x00022161,
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001080 .name = "KS8721BL",
1081 .config = phy_cmd_ks8721bl_config,
1082 .startup = phy_cmd_ks8721bl_startup,
1083 .ack_int = phy_cmd_ks8721bl_ack_int,
1084 .shutdown = phy_cmd_ks8721bl_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085};
1086
1087/* ------------------------------------------------------------------------- */
Greg Ungerer562d2f82005-11-07 14:09:50 +10001088/* register definitions for the DP83848 */
1089
1090#define MII_DP8384X_PHYSTST 16 /* PHY Status Register */
1091
1092static void mii_parse_dp8384x_sr2(uint mii_reg, struct net_device *dev)
1093{
Wang Chen4cf16532008-11-12 23:38:14 -08001094 struct fec_enet_private *fep = netdev_priv(dev);
Greg Ungerer562d2f82005-11-07 14:09:50 +10001095 volatile uint *s = &(fep->phy_status);
1096
1097 *s &= ~(PHY_STAT_SPMASK | PHY_STAT_LINK | PHY_STAT_ANC);
1098
1099 /* Link up */
1100 if (mii_reg & 0x0001) {
1101 fep->link = 1;
1102 *s |= PHY_STAT_LINK;
1103 } else
1104 fep->link = 0;
1105 /* Status of link */
1106 if (mii_reg & 0x0010) /* Autonegotioation complete */
1107 *s |= PHY_STAT_ANC;
1108 if (mii_reg & 0x0002) { /* 10MBps? */
1109 if (mii_reg & 0x0004) /* Full Duplex? */
1110 *s |= PHY_STAT_10FDX;
1111 else
1112 *s |= PHY_STAT_10HDX;
1113 } else { /* 100 Mbps? */
1114 if (mii_reg & 0x0004) /* Full Duplex? */
1115 *s |= PHY_STAT_100FDX;
1116 else
1117 *s |= PHY_STAT_100HDX;
1118 }
1119 if (mii_reg & 0x0008)
1120 *s |= PHY_STAT_FAULT;
1121}
1122
1123static phy_info_t phy_info_dp83848= {
1124 0x020005c9,
1125 "DP83848",
1126
1127 (const phy_cmd_t []) { /* config */
1128 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1129 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1130 { mk_mii_read(MII_DP8384X_PHYSTST), mii_parse_dp8384x_sr2 },
1131 { mk_mii_end, }
1132 },
1133 (const phy_cmd_t []) { /* startup - enable interrupts */
1134 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
1135 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1136 { mk_mii_end, }
1137 },
1138 (const phy_cmd_t []) { /* ack_int - never happens, no interrupt */
1139 { mk_mii_end, }
1140 },
1141 (const phy_cmd_t []) { /* shutdown */
1142 { mk_mii_end, }
1143 },
1144};
1145
1146/* ------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001148static phy_info_t const * const phy_info[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 &phy_info_lxt970,
1150 &phy_info_lxt971,
1151 &phy_info_qs6612,
1152 &phy_info_am79c874,
1153 &phy_info_ks8721bl,
Greg Ungerer562d2f82005-11-07 14:09:50 +10001154 &phy_info_dp83848,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 NULL
1156};
1157
1158/* ------------------------------------------------------------------------- */
Sebastian Siewiorc1d96152008-05-01 14:04:02 +10001159#ifdef HAVE_mii_link_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001161mii_link_interrupt(int irq, void * dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163/*
Greg Ungerer43be6362009-02-26 22:42:51 -08001164 * This is specific to the MII interrupt setup of the M5272EVB.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 */
Greg Ungerer43be6362009-02-26 22:42:51 -08001166static void __inline__ fec_request_mii_intr(struct net_device *dev)
1167{
1168 if (request_irq(66, mii_link_interrupt, IRQF_DISABLED, "fec(MII)", dev) != 0)
1169 printk("FEC: Could not allocate fec(MII) IRQ(66)!\n");
1170}
1171
1172static void __inline__ fec_disable_phy_intr(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
1174 volatile unsigned long *icrp;
Greg Ungerer43be6362009-02-26 22:42:51 -08001175 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR1);
1176 *icrp = 0x08000000;
1177}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Greg Ungerer43be6362009-02-26 22:42:51 -08001179static void __inline__ fec_phy_ack_intr(void)
1180{
1181 volatile unsigned long *icrp;
1182 /* Acknowledge the interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR1);
Greg Ungererf861d622007-07-30 16:29:16 +10001184 *icrp = 0x0d000000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
Greg Ungerer43be6362009-02-26 22:42:51 -08001187#ifdef CONFIG_M5272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188static void __inline__ fec_get_mac(struct net_device *dev)
1189{
1190 struct fec_enet_private *fep = netdev_priv(dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001191 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001193 if (FEC_FLASHMAC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 /*
1195 * Get MAC address from FLASH.
1196 * If it is all 1's or 0's, use the default.
1197 */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001198 iap = (unsigned char *)FEC_FLASHMAC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if ((iap[0] == 0) && (iap[1] == 0) && (iap[2] == 0) &&
1200 (iap[3] == 0) && (iap[4] == 0) && (iap[5] == 0))
1201 iap = fec_mac_default;
1202 if ((iap[0] == 0xff) && (iap[1] == 0xff) && (iap[2] == 0xff) &&
1203 (iap[3] == 0xff) && (iap[4] == 0xff) && (iap[5] == 0xff))
1204 iap = fec_mac_default;
1205 } else {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001206 *((unsigned long *) &tmpaddr[0]) = readl(fep->hwp + FEC_ADDR_LOW);
1207 *((unsigned short *) &tmpaddr[4]) = (readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 iap = &tmpaddr[0];
1209 }
1210
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001211 memcpy(dev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 /* Adjust MAC if using default MAC address */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001214 if (iap == fec_mac_default)
1215 dev->dev_addr[ETH_ALEN-1] = fec_mac_default[ETH_ALEN-1] + fep->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217#endif
1218
1219/* ------------------------------------------------------------------------- */
1220
1221static void mii_display_status(struct net_device *dev)
1222{
1223 struct fec_enet_private *fep = netdev_priv(dev);
1224 volatile uint *s = &(fep->phy_status);
1225
1226 if (!fep->link && !fep->old_link) {
1227 /* Link is still down - don't print anything */
1228 return;
1229 }
1230
1231 printk("%s: status: ", dev->name);
1232
1233 if (!fep->link) {
1234 printk("link down");
1235 } else {
1236 printk("link up");
1237
1238 switch(*s & PHY_STAT_SPMASK) {
1239 case PHY_STAT_100FDX: printk(", 100MBit Full Duplex"); break;
1240 case PHY_STAT_100HDX: printk(", 100MBit Half Duplex"); break;
1241 case PHY_STAT_10FDX: printk(", 10MBit Full Duplex"); break;
1242 case PHY_STAT_10HDX: printk(", 10MBit Half Duplex"); break;
1243 default:
1244 printk(", Unknown speed/duplex");
1245 }
1246
1247 if (*s & PHY_STAT_ANC)
1248 printk(", auto-negotiation complete");
1249 }
1250
1251 if (*s & PHY_STAT_FAULT)
1252 printk(", remote fault");
1253
1254 printk(".\n");
1255}
1256
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001257static void mii_display_config(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001259 struct fec_enet_private *fep = container_of(work, struct fec_enet_private, phy_task);
1260 struct net_device *dev = fep->netdev;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001261 uint status = fep->phy_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 /*
1264 ** When we get here, phy_task is already removed from
1265 ** the workqueue. It is thus safe to allow to reuse it.
1266 */
1267 fep->mii_phy_task_queued = 0;
1268 printk("%s: config: auto-negotiation ", dev->name);
1269
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001270 if (status & PHY_CONF_ANE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 printk("on");
1272 else
1273 printk("off");
1274
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001275 if (status & PHY_CONF_100FDX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 printk(", 100FDX");
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001277 if (status & PHY_CONF_100HDX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 printk(", 100HDX");
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001279 if (status & PHY_CONF_10FDX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 printk(", 10FDX");
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001281 if (status & PHY_CONF_10HDX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 printk(", 10HDX");
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001283 if (!(status & PHY_CONF_SPMASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 printk(", No speed/duplex selected?");
1285
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001286 if (status & PHY_CONF_LOOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 printk(", loopback enabled");
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 printk(".\n");
1290
1291 fep->sequence_done = 1;
1292}
1293
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001294static void mii_relink(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001296 struct fec_enet_private *fep = container_of(work, struct fec_enet_private, phy_task);
1297 struct net_device *dev = fep->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 int duplex;
1299
1300 /*
1301 ** When we get here, phy_task is already removed from
1302 ** the workqueue. It is thus safe to allow to reuse it.
1303 */
1304 fep->mii_phy_task_queued = 0;
1305 fep->link = (fep->phy_status & PHY_STAT_LINK) ? 1 : 0;
1306 mii_display_status(dev);
1307 fep->old_link = fep->link;
1308
1309 if (fep->link) {
1310 duplex = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001311 if (fep->phy_status
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 & (PHY_STAT_100FDX | PHY_STAT_10FDX))
1313 duplex = 1;
1314 fec_restart(dev, duplex);
Philippe De Muyterf909b1e2007-10-23 14:37:54 +10001315 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 fec_stop(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
1318
1319/* mii_queue_relink is called in interrupt context from mii_link_interrupt */
1320static void mii_queue_relink(uint mii_reg, struct net_device *dev)
1321{
1322 struct fec_enet_private *fep = netdev_priv(dev);
1323
1324 /*
Sascha Hauer22f6b862009-04-15 01:32:18 +00001325 * We cannot queue phy_task twice in the workqueue. It
1326 * would cause an endless loop in the workqueue.
1327 * Fortunately, if the last mii_relink entry has not yet been
1328 * executed now, it will do the job for the current interrupt,
1329 * which is just what we want.
1330 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (fep->mii_phy_task_queued)
1332 return;
1333
1334 fep->mii_phy_task_queued = 1;
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001335 INIT_WORK(&fep->phy_task, mii_relink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 schedule_work(&fep->phy_task);
1337}
1338
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001339/* mii_queue_config is called in interrupt context from fec_enet_mii */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340static void mii_queue_config(uint mii_reg, struct net_device *dev)
1341{
1342 struct fec_enet_private *fep = netdev_priv(dev);
1343
1344 if (fep->mii_phy_task_queued)
1345 return;
1346
1347 fep->mii_phy_task_queued = 1;
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001348 INIT_WORK(&fep->phy_task, mii_display_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 schedule_work(&fep->phy_task);
1350}
1351
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001352phy_cmd_t const phy_cmd_relink[] = {
1353 { mk_mii_read(MII_REG_CR), mii_queue_relink },
1354 { mk_mii_end, }
1355 };
1356phy_cmd_t const phy_cmd_config[] = {
1357 { mk_mii_read(MII_REG_CR), mii_queue_config },
1358 { mk_mii_end, }
1359 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Sascha Hauer22f6b862009-04-15 01:32:18 +00001361/* Read remainder of PHY ID. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362static void
1363mii_discover_phy3(uint mii_reg, struct net_device *dev)
1364{
1365 struct fec_enet_private *fep;
1366 int i;
1367
1368 fep = netdev_priv(dev);
1369 fep->phy_id |= (mii_reg & 0xffff);
1370 printk("fec: PHY @ 0x%x, ID 0x%08x", fep->phy_addr, fep->phy_id);
1371
1372 for(i = 0; phy_info[i]; i++) {
1373 if(phy_info[i]->id == (fep->phy_id >> 4))
1374 break;
1375 }
1376
1377 if (phy_info[i])
1378 printk(" -- %s\n", phy_info[i]->name);
1379 else
1380 printk(" -- unknown PHY!\n");
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 fep->phy = phy_info[i];
1383 fep->phy_id_done = 1;
1384}
1385
1386/* Scan all of the MII PHY addresses looking for someone to respond
1387 * with a valid ID. This usually happens quickly.
1388 */
1389static void
1390mii_discover_phy(uint mii_reg, struct net_device *dev)
1391{
1392 struct fec_enet_private *fep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 uint phytype;
1394
1395 fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 if (fep->phy_addr < 32) {
1398 if ((phytype = (mii_reg & 0xffff)) != 0xffff && phytype != 0) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001399
Sascha Hauer22f6b862009-04-15 01:32:18 +00001400 /* Got first part of ID, now get remainder */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 fep->phy_id = phytype << 16;
1402 mii_queue(dev, mk_mii_read(MII_REG_PHYIR2),
1403 mii_discover_phy3);
Philippe De Muyterf909b1e2007-10-23 14:37:54 +10001404 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 fep->phy_addr++;
1406 mii_queue(dev, mk_mii_read(MII_REG_PHYIR1),
1407 mii_discover_phy);
1408 }
1409 } else {
1410 printk("FEC: No PHY device found.\n");
1411 /* Disable external MII interface */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001412 writel(0, fep->hwp + FEC_MII_SPEED);
1413 fep->phy_speed = 0;
Greg Ungerer43be6362009-02-26 22:42:51 -08001414#ifdef HAVE_mii_link_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 fec_disable_phy_intr();
Sascha Haueread73182009-01-28 23:03:11 +00001416#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
1418}
1419
Sascha Hauer22f6b862009-04-15 01:32:18 +00001420/* This interrupt occurs when the PHY detects a link change */
Sebastian Siewiorc1d96152008-05-01 14:04:02 +10001421#ifdef HAVE_mii_link_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001423mii_link_interrupt(int irq, void * dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
1425 struct net_device *dev = dev_id;
1426 struct fec_enet_private *fep = netdev_priv(dev);
1427
1428 fec_phy_ack_intr();
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 mii_do_cmd(dev, fep->phy->ack_int);
1431 mii_do_cmd(dev, phy_cmd_relink); /* restart and display status */
1432
1433 return IRQ_HANDLED;
1434}
Sebastian Siewiorc1d96152008-05-01 14:04:02 +10001435#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437static int
1438fec_enet_open(struct net_device *dev)
1439{
1440 struct fec_enet_private *fep = netdev_priv(dev);
1441
1442 /* I should reset the ring buffers here, but I don't yet know
1443 * a simple way to do that.
1444 */
1445 fec_set_mac_address(dev);
1446
1447 fep->sequence_done = 0;
1448 fep->link = 0;
1449
1450 if (fep->phy) {
1451 mii_do_cmd(dev, fep->phy->ack_int);
1452 mii_do_cmd(dev, fep->phy->config);
1453 mii_do_cmd(dev, phy_cmd_config); /* display configuration */
1454
Matt Waddel6b265292006-06-27 13:10:56 +10001455 /* Poll until the PHY tells us its configuration
1456 * (not link state).
1457 * Request is initiated by mii_do_cmd above, but answer
1458 * comes by interrupt.
1459 * This should take about 25 usec per register at 2.5 MHz,
1460 * and we read approximately 5 registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 */
1462 while(!fep->sequence_done)
1463 schedule();
1464
1465 mii_do_cmd(dev, fep->phy->startup);
1466
1467 /* Set the initial link state to true. A lot of hardware
1468 * based on this device does not implement a PHY interrupt,
1469 * so we are never notified of link change.
1470 */
1471 fep->link = 1;
1472 } else {
1473 fep->link = 1; /* lets just try it and see */
1474 /* no phy, go full duplex, it's most likely a hub chip */
1475 fec_restart(dev, 1);
1476 }
1477
1478 netif_start_queue(dev);
1479 fep->opened = 1;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001480 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
1482
1483static int
1484fec_enet_close(struct net_device *dev)
1485{
1486 struct fec_enet_private *fep = netdev_priv(dev);
1487
Sascha Hauer22f6b862009-04-15 01:32:18 +00001488 /* Don't know what to do yet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 fep->opened = 0;
1490 netif_stop_queue(dev);
1491 fec_stop(dev);
1492
1493 return 0;
1494}
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496/* Set or clear the multicast filter for this adaptor.
1497 * Skeleton taken from sunlance driver.
1498 * The CPM Ethernet implementation allows Multicast as well as individual
1499 * MAC address filtering. Some of the drivers check to make sure it is
1500 * a group multicast address, and discard those that are not. I guess I
1501 * will do the same for now, but just remove the test if you want
1502 * individual filtering as well (do the upper net layers want or support
1503 * this kind of feature?).
1504 */
1505
1506#define HASH_BITS 6 /* #bits in hash */
1507#define CRC32_POLY 0xEDB88320
1508
1509static void set_multicast_list(struct net_device *dev)
1510{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001511 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 struct dev_mc_list *dmi;
Sascha Hauerf44d6302009-04-15 03:11:30 +00001513 unsigned int i, j, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 unsigned char hash;
1515
Sascha Hauer22f6b862009-04-15 01:32:18 +00001516 if (dev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001517 tmp = readl(fep->hwp + FEC_R_CNTRL);
1518 tmp |= 0x8;
1519 writel(tmp, fep->hwp + FEC_R_CNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 } else {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001521 tmp = readl(fep->hwp + FEC_R_CNTRL);
1522 tmp &= ~0x8;
1523 writel(tmp, fep->hwp + FEC_R_CNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 if (dev->flags & IFF_ALLMULTI) {
1526 /* Catch all multicast addresses, so set the
Sascha Hauer22f6b862009-04-15 01:32:18 +00001527 * filter to all 1's
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001529 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1530 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 } else {
Sascha Hauer22f6b862009-04-15 01:32:18 +00001532 /* Clear filter and add the addresses in hash register
1533 */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001534 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1535 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 dmi = dev->mc_list;
1538
Sascha Hauer22f6b862009-04-15 01:32:18 +00001539 for (j = 0; j < dev->mc_count; j++, dmi = dmi->next) {
1540 /* Only support group multicast for now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (!(dmi->dmi_addr[0] & 1))
1542 continue;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001543
Sascha Hauer22f6b862009-04-15 01:32:18 +00001544 /* calculate crc32 value of mac address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 crc = 0xffffffff;
1546
Sascha Hauer22f6b862009-04-15 01:32:18 +00001547 for (i = 0; i < dmi->dmi_addrlen; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 data = dmi->dmi_addr[i];
Sascha Hauer22f6b862009-04-15 01:32:18 +00001549 for (bit = 0; bit < 8; bit++, data >>= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 crc = (crc >> 1) ^
1551 (((crc ^ data) & 1) ? CRC32_POLY : 0);
1552 }
1553 }
1554
1555 /* only upper 6 bits (HASH_BITS) are used
Sascha Hauer22f6b862009-04-15 01:32:18 +00001556 * which point to specific bit in he hash registers
1557 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001559
Sascha Hauerf44d6302009-04-15 03:11:30 +00001560 if (hash > 31) {
1561 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1562 tmp |= 1 << (hash - 32);
1563 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1564 } else {
1565 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1566 tmp |= 1 << hash;
1567 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
1570 }
1571 }
1572}
1573
Sascha Hauer22f6b862009-04-15 01:32:18 +00001574/* Set a MAC change in hardware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575static void
1576fec_set_mac_address(struct net_device *dev)
1577{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001578 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 /* Set station address. */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001581 writel(dev->dev_addr[3] | (dev->dev_addr[2] << 8) |
1582 (dev->dev_addr[1] << 16) | (dev->dev_addr[0] << 24),
1583 fep->hwp + FEC_ADDR_LOW);
1584 writel((dev->dev_addr[5] << 16) | (dev->dev_addr[4] << 24),
1585 fep + FEC_ADDR_HIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586}
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 /*
1589 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00001590 *
1591 * index is only used in legacy code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 */
Sascha Haueread73182009-01-28 23:03:11 +00001593int __init fec_enet_init(struct net_device *dev, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
1595 struct fec_enet_private *fep = netdev_priv(dev);
1596 unsigned long mem_addr;
Sascha Hauer2e285322009-04-15 01:32:16 +00001597 struct bufdesc *bdp, *cbd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001600 /* Allocate memory for buffer descriptors. */
1601 cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
1602 GFP_KERNEL);
1603 if (!cbd_base) {
Greg Ungerer562d2f82005-11-07 14:09:50 +10001604 printk("FEC: allocate descriptor memory failed?\n");
1605 return -ENOMEM;
1606 }
1607
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10001608 spin_lock_init(&fep->hw_lock);
1609 spin_lock_init(&fep->mii_lock);
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 fep->index = index;
Sascha Hauerf44d6302009-04-15 03:11:30 +00001612 fep->hwp = (void __iomem *)dev->base_addr;
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001613 fep->netdev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Sascha Hauer22f6b862009-04-15 01:32:18 +00001615 /* Whack a reset. We should wait for this. */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001616 writel(1, fep->hwp + FEC_ECNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 udelay(10);
1618
Sascha Haueread73182009-01-28 23:03:11 +00001619 /* Set the Ethernet address */
Greg Ungerer43be6362009-02-26 22:42:51 -08001620#ifdef CONFIG_M5272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 fec_get_mac(dev);
Sascha Haueread73182009-01-28 23:03:11 +00001622#else
1623 {
1624 unsigned long l;
Sascha Hauerf44d6302009-04-15 03:11:30 +00001625 l = readl(fep->hwp + FEC_ADDR_LOW);
Sascha Haueread73182009-01-28 23:03:11 +00001626 dev->dev_addr[0] = (unsigned char)((l & 0xFF000000) >> 24);
1627 dev->dev_addr[1] = (unsigned char)((l & 0x00FF0000) >> 16);
1628 dev->dev_addr[2] = (unsigned char)((l & 0x0000FF00) >> 8);
1629 dev->dev_addr[3] = (unsigned char)((l & 0x000000FF) >> 0);
Sascha Hauerf44d6302009-04-15 03:11:30 +00001630 l = readl(fep->hwp + FEC_ADDR_HIGH);
Sascha Haueread73182009-01-28 23:03:11 +00001631 dev->dev_addr[4] = (unsigned char)((l & 0xFF000000) >> 24);
1632 dev->dev_addr[5] = (unsigned char)((l & 0x00FF0000) >> 16);
1633 }
1634#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001636 /* Set receive and transmit descriptor base. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 fep->rx_bd_base = cbd_base;
1638 fep->tx_bd_base = cbd_base + RX_RING_SIZE;
1639
1640 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
1641 fep->cur_rx = fep->rx_bd_base;
1642
1643 fep->skb_cur = fep->skb_dirty = 0;
1644
Sascha Hauer22f6b862009-04-15 01:32:18 +00001645 /* Initialize the receive buffer descriptors. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 bdp = fep->rx_bd_base;
1647 for (i=0; i<FEC_ENET_RX_PAGES; i++) {
1648
Sascha Hauer22f6b862009-04-15 01:32:18 +00001649 /* Allocate a page */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 mem_addr = __get_free_page(GFP_KERNEL);
1651 /* XXX: missing check for allocation failure */
1652
Sascha Hauer22f6b862009-04-15 01:32:18 +00001653 /* Initialize the BD for every fragment in the page */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 for (j=0; j<FEC_ENET_RX_FRPPG; j++) {
1655 bdp->cbd_sc = BD_ENET_RX_EMPTY;
1656 bdp->cbd_bufaddr = __pa(mem_addr);
1657 mem_addr += FEC_ENET_RX_FRSIZE;
1658 bdp++;
1659 }
1660 }
1661
Sascha Hauer22f6b862009-04-15 01:32:18 +00001662 /* Set the last buffer to wrap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 bdp--;
1664 bdp->cbd_sc |= BD_SC_WRAP;
1665
Sascha Hauer22f6b862009-04-15 01:32:18 +00001666 /* ...and the same for transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 bdp = fep->tx_bd_base;
1668 for (i=0, j=FEC_ENET_TX_FRPPG; i<TX_RING_SIZE; i++) {
1669 if (j >= FEC_ENET_TX_FRPPG) {
1670 mem_addr = __get_free_page(GFP_KERNEL);
1671 j = 1;
1672 } else {
1673 mem_addr += FEC_ENET_TX_FRSIZE;
1674 j++;
1675 }
1676 fep->tx_bounce[i] = (unsigned char *) mem_addr;
1677
Sascha Hauer22f6b862009-04-15 01:32:18 +00001678 /* Initialize the BD for every fragment in the page */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 bdp->cbd_sc = 0;
1680 bdp->cbd_bufaddr = 0;
1681 bdp++;
1682 }
1683
Sascha Hauer22f6b862009-04-15 01:32:18 +00001684 /* Set the last buffer to wrap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 bdp--;
1686 bdp->cbd_sc |= BD_SC_WRAP;
1687
Sascha Hauer22f6b862009-04-15 01:32:18 +00001688 /* Set receive and transmit descriptor base */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001689 writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
Sascha Hauer2e285322009-04-15 01:32:16 +00001690 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc) * RX_RING_SIZE,
Sascha Hauerf44d6302009-04-15 03:11:30 +00001691 fep->hwp + FEC_X_DES_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Greg Ungerer43be6362009-02-26 22:42:51 -08001693#ifdef HAVE_mii_link_interrupt
1694 fec_request_mii_intr(dev);
Sascha Haueread73182009-01-28 23:03:11 +00001695#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Sascha Hauerf44d6302009-04-15 03:11:30 +00001697 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1698 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1699 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
1700 writel(2, fep->hwp + FEC_ECNTRL);
1701 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Greg Ungerercc462f72008-05-01 13:35:34 +10001702#ifndef CONFIG_M5272
Sascha Hauerf44d6302009-04-15 03:11:30 +00001703 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1704 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
Greg Ungerercc462f72008-05-01 13:35:34 +10001705#endif
Greg Ungerer562d2f82005-11-07 14:09:50 +10001706
Sascha Hauer22f6b862009-04-15 01:32:18 +00001707 /* The FEC Ethernet specific entries in the device structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 dev->open = fec_enet_open;
1709 dev->hard_start_xmit = fec_enet_start_xmit;
1710 dev->tx_timeout = fec_timeout;
1711 dev->watchdog_timeo = TX_TIMEOUT;
1712 dev->stop = fec_enet_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 dev->set_multicast_list = set_multicast_list;
1714
1715 for (i=0; i<NMII-1; i++)
1716 mii_cmds[i].mii_next = &mii_cmds[i+1];
1717 mii_free = mii_cmds;
1718
1719 /* setup MII interface */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001720 writel(OPT_FRAME_SIZE | 0x04, fep->hwp + FEC_R_CNTRL);
1721 writel(0, fep->hwp + FEC_X_CNTRL);
Sascha Haueread73182009-01-28 23:03:11 +00001722
Sascha Hauer22f6b862009-04-15 01:32:18 +00001723 /* Set MII speed to 2.5 MHz */
Sascha Haueread73182009-01-28 23:03:11 +00001724 fep->phy_speed = ((((clk_get_rate(fep->clk) / 2 + 4999999)
1725 / 2500000) / 2) & 0x3F) << 1;
Sascha Hauerf44d6302009-04-15 03:11:30 +00001726 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Sascha Haueread73182009-01-28 23:03:11 +00001727 fec_restart(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Matt Waddel6b265292006-06-27 13:10:56 +10001729 /* Clear and enable interrupts */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001730 writel(0xffc00000, fep->hwp + FEC_IEVENT);
1731 writel(FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII,
1732 fep->hwp + FEC_IMASK);
Matt Waddel6b265292006-06-27 13:10:56 +10001733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 /* Queue up command to detect the PHY and initialize the
1735 * remainder of the interface.
1736 */
1737 fep->phy_id_done = 0;
1738 fep->phy_addr = 0;
1739 mii_queue(dev, mk_mii_read(MII_REG_PHYIR1), mii_discover_phy);
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 return 0;
1742}
1743
1744/* This function is called to start or restart the FEC during a link
1745 * change. This only happens when switching between half and full
1746 * duplex.
1747 */
1748static void
1749fec_restart(struct net_device *dev, int duplex)
1750{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001751 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer2e285322009-04-15 01:32:16 +00001752 struct bufdesc *bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 int i;
1754
Sascha Hauerf44d6302009-04-15 03:11:30 +00001755 /* Whack a reset. We should wait for this. */
1756 writel(1, fep->hwp + FEC_ECNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 udelay(10);
1758
Sascha Hauerf44d6302009-04-15 03:11:30 +00001759 /* Clear any outstanding interrupt. */
1760 writel(0xffc00000, fep->hwp + FEC_IEVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Sascha Hauerf44d6302009-04-15 03:11:30 +00001762 /* Set station address. */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001763 fec_set_mac_address(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Sascha Hauerf44d6302009-04-15 03:11:30 +00001765 /* Reset all multicast. */
1766 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1767 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Sascha Hauerf44d6302009-04-15 03:11:30 +00001769 /* Set maximum receive buffer size. */
1770 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Sascha Hauerf44d6302009-04-15 03:11:30 +00001772 /* Set receive and transmit descriptor base. */
1773 writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
Sascha Hauer2e285322009-04-15 01:32:16 +00001774 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc) * RX_RING_SIZE,
Sascha Hauerf44d6302009-04-15 03:11:30 +00001775 fep->hwp + FEC_X_DES_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
1778 fep->cur_rx = fep->rx_bd_base;
1779
Sascha Hauerf44d6302009-04-15 03:11:30 +00001780 /* Reset SKB transmit buffers. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 fep->skb_cur = fep->skb_dirty = 0;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001782 for (i = 0; i <= TX_RING_MOD_MASK; i++) {
1783 if (fep->tx_skbuff[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 dev_kfree_skb_any(fep->tx_skbuff[i]);
1785 fep->tx_skbuff[i] = NULL;
1786 }
1787 }
1788
Sascha Hauerf44d6302009-04-15 03:11:30 +00001789 /* Initialize the receive buffer descriptors. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 bdp = fep->rx_bd_base;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001791 for (i = 0; i < RX_RING_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Sascha Hauerf44d6302009-04-15 03:11:30 +00001793 /* Initialize the BD for every fragment in the page. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 bdp->cbd_sc = BD_ENET_RX_EMPTY;
1795 bdp++;
1796 }
1797
Sascha Hauer22f6b862009-04-15 01:32:18 +00001798 /* Set the last buffer to wrap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 bdp--;
1800 bdp->cbd_sc |= BD_SC_WRAP;
1801
Sascha Hauer22f6b862009-04-15 01:32:18 +00001802 /* ...and the same for transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 bdp = fep->tx_bd_base;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001804 for (i = 0; i < TX_RING_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Sascha Hauerf44d6302009-04-15 03:11:30 +00001806 /* Initialize the BD for every fragment in the page. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 bdp->cbd_sc = 0;
1808 bdp->cbd_bufaddr = 0;
1809 bdp++;
1810 }
1811
Sascha Hauer22f6b862009-04-15 01:32:18 +00001812 /* Set the last buffer to wrap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 bdp--;
1814 bdp->cbd_sc |= BD_SC_WRAP;
1815
Sascha Hauer22f6b862009-04-15 01:32:18 +00001816 /* Enable MII mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 if (duplex) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001818 /* MII enable / FD enable */
1819 writel(OPT_FRAME_SIZE | 0x04, fep->hwp + FEC_R_CNTRL);
1820 writel(0x04, fep->hwp + FEC_X_CNTRL);
Philippe De Muyterf909b1e2007-10-23 14:37:54 +10001821 } else {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001822 /* MII enable / No Rcv on Xmit */
1823 writel(OPT_FRAME_SIZE | 0x06, fep->hwp + FEC_R_CNTRL);
1824 writel(0x0, fep->hwp + FEC_X_CNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 }
1826 fep->full_duplex = duplex;
1827
Sascha Hauer22f6b862009-04-15 01:32:18 +00001828 /* Set MII speed */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001829 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Sascha Hauer22f6b862009-04-15 01:32:18 +00001831 /* And last, enable the transmit and receive processing */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001832 writel(2, fep->hwp + FEC_ECNTRL);
1833 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Matt Waddel6b265292006-06-27 13:10:56 +10001834
Sascha Hauer22f6b862009-04-15 01:32:18 +00001835 /* Enable interrupts we wish to service */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001836 writel(FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII,
1837 fep->hwp + FEC_IMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
1840static void
1841fec_stop(struct net_device *dev)
1842{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001843 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Sascha Hauer22f6b862009-04-15 01:32:18 +00001845 /* We cannot expect a graceful transmit stop without link !!! */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001846 if (fep->link) {
1847 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
Philippe De Muyter677177c2006-06-27 13:05:33 +10001848 udelay(10);
Sascha Hauerf44d6302009-04-15 03:11:30 +00001849 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Philippe De Muyter677177c2006-06-27 13:05:33 +10001850 printk("fec_stop : Graceful transmit stop did not complete !\n");
Sascha Hauerf44d6302009-04-15 03:11:30 +00001851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Sascha Hauerf44d6302009-04-15 03:11:30 +00001853 /* Whack a reset. We should wait for this. */
1854 writel(1, fep->hwp + FEC_ECNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 udelay(10);
1856
Sascha Hauerf44d6302009-04-15 03:11:30 +00001857 /* Clear outstanding MII command interrupts. */
1858 writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Sascha Hauerf44d6302009-04-15 03:11:30 +00001860 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1861 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
Sascha Haueread73182009-01-28 23:03:11 +00001864static int __devinit
1865fec_probe(struct platform_device *pdev)
1866{
1867 struct fec_enet_private *fep;
1868 struct net_device *ndev;
1869 int i, irq, ret = 0;
1870 struct resource *r;
1871
1872 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1873 if (!r)
1874 return -ENXIO;
1875
1876 r = request_mem_region(r->start, resource_size(r), pdev->name);
1877 if (!r)
1878 return -EBUSY;
1879
1880 /* Init network device */
1881 ndev = alloc_etherdev(sizeof(struct fec_enet_private));
1882 if (!ndev)
1883 return -ENOMEM;
1884
1885 SET_NETDEV_DEV(ndev, &pdev->dev);
1886
1887 /* setup board info structure */
1888 fep = netdev_priv(ndev);
1889 memset(fep, 0, sizeof(*fep));
1890
1891 ndev->base_addr = (unsigned long)ioremap(r->start, resource_size(r));
1892
1893 if (!ndev->base_addr) {
1894 ret = -ENOMEM;
1895 goto failed_ioremap;
1896 }
1897
1898 platform_set_drvdata(pdev, ndev);
1899
1900 /* This device has up to three irqs on some platforms */
1901 for (i = 0; i < 3; i++) {
1902 irq = platform_get_irq(pdev, i);
1903 if (i && irq < 0)
1904 break;
1905 ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
1906 if (ret) {
1907 while (i >= 0) {
1908 irq = platform_get_irq(pdev, i);
1909 free_irq(irq, ndev);
1910 i--;
1911 }
1912 goto failed_irq;
1913 }
1914 }
1915
1916 fep->clk = clk_get(&pdev->dev, "fec_clk");
1917 if (IS_ERR(fep->clk)) {
1918 ret = PTR_ERR(fep->clk);
1919 goto failed_clk;
1920 }
1921 clk_enable(fep->clk);
1922
1923 ret = fec_enet_init(ndev, 0);
1924 if (ret)
1925 goto failed_init;
1926
1927 ret = register_netdev(ndev);
1928 if (ret)
1929 goto failed_register;
1930
1931 return 0;
1932
1933failed_register:
1934failed_init:
1935 clk_disable(fep->clk);
1936 clk_put(fep->clk);
1937failed_clk:
1938 for (i = 0; i < 3; i++) {
1939 irq = platform_get_irq(pdev, i);
1940 if (irq > 0)
1941 free_irq(irq, ndev);
1942 }
1943failed_irq:
1944 iounmap((void __iomem *)ndev->base_addr);
1945failed_ioremap:
1946 free_netdev(ndev);
1947
1948 return ret;
1949}
1950
1951static int __devexit
1952fec_drv_remove(struct platform_device *pdev)
1953{
1954 struct net_device *ndev = platform_get_drvdata(pdev);
1955 struct fec_enet_private *fep = netdev_priv(ndev);
1956
1957 platform_set_drvdata(pdev, NULL);
1958
1959 fec_stop(ndev);
1960 clk_disable(fep->clk);
1961 clk_put(fep->clk);
1962 iounmap((void __iomem *)ndev->base_addr);
1963 unregister_netdev(ndev);
1964 free_netdev(ndev);
1965 return 0;
1966}
1967
1968static int
1969fec_suspend(struct platform_device *dev, pm_message_t state)
1970{
1971 struct net_device *ndev = platform_get_drvdata(dev);
1972 struct fec_enet_private *fep;
1973
1974 if (ndev) {
1975 fep = netdev_priv(ndev);
1976 if (netif_running(ndev)) {
1977 netif_device_detach(ndev);
1978 fec_stop(ndev);
1979 }
1980 }
1981 return 0;
1982}
1983
1984static int
1985fec_resume(struct platform_device *dev)
1986{
1987 struct net_device *ndev = platform_get_drvdata(dev);
1988
1989 if (ndev) {
1990 if (netif_running(ndev)) {
1991 fec_enet_init(ndev, 0);
1992 netif_device_attach(ndev);
1993 }
1994 }
1995 return 0;
1996}
1997
1998static struct platform_driver fec_driver = {
1999 .driver = {
2000 .name = "fec",
2001 .owner = THIS_MODULE,
2002 },
2003 .probe = fec_probe,
2004 .remove = __devexit_p(fec_drv_remove),
2005 .suspend = fec_suspend,
2006 .resume = fec_resume,
2007};
2008
2009static int __init
2010fec_enet_module_init(void)
2011{
2012 printk(KERN_INFO "FEC Ethernet Driver\n");
2013
2014 return platform_driver_register(&fec_driver);
2015}
2016
2017static void __exit
2018fec_enet_cleanup(void)
2019{
2020 platform_driver_unregister(&fec_driver);
2021}
2022
2023module_exit(fec_enet_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024module_init(fec_enet_module_init);
2025
2026MODULE_LICENSE("GPL");