blob: 967ad01b19253ac796d4e8e40ff8a656005ccbf1 [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];
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000175 struct sk_buff* rx_skbuff[RX_RING_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 ushort skb_cur;
177 ushort skb_dirty;
178
Sascha Hauer22f6b862009-04-15 01:32:18 +0000179 /* CPM dual port RAM relative addresses */
Sascha Hauer4661e752009-01-28 23:03:07 +0000180 dma_addr_t bd_dma;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000181 /* Address of Rx and Tx buffers */
Sascha Hauer2e285322009-04-15 01:32:16 +0000182 struct bufdesc *rx_bd_base;
183 struct bufdesc *tx_bd_base;
184 /* The next free ring entry */
185 struct bufdesc *cur_rx, *cur_tx;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000186 /* The ring entries to be free()ed */
Sascha Hauer2e285322009-04-15 01:32:16 +0000187 struct bufdesc *dirty_tx;
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 uint tx_full;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000190 /* hold while accessing the HW like ringbuffer for tx/rx but not MAC */
191 spinlock_t hw_lock;
192 /* hold while accessing the mii_list_t() elements */
193 spinlock_t mii_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 uint phy_id;
196 uint phy_id_done;
197 uint phy_status;
198 uint phy_speed;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000199 phy_info_t const *phy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 struct work_struct phy_task;
201
202 uint sequence_done;
203 uint mii_phy_task_queued;
204
205 uint phy_addr;
206
207 int index;
208 int opened;
209 int link;
210 int old_link;
211 int full_duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212};
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214static void fec_enet_mii(struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100215static irqreturn_t fec_enet_interrupt(int irq, void * dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216static void fec_enet_tx(struct net_device *dev);
217static void fec_enet_rx(struct net_device *dev);
218static int fec_enet_close(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219static void fec_restart(struct net_device *dev, int duplex);
220static void fec_stop(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222
223/* MII processing. We keep this as simple as possible. Requests are
224 * placed on the list (if there is room). When the request is finished
225 * by the MII, an optional function may be called.
226 */
227typedef struct mii_list {
228 uint mii_regval;
229 void (*mii_func)(uint val, struct net_device *dev);
230 struct mii_list *mii_next;
231} mii_list_t;
232
233#define NMII 20
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000234static mii_list_t mii_cmds[NMII];
235static mii_list_t *mii_free;
236static mii_list_t *mii_head;
237static mii_list_t *mii_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400239static int mii_queue(struct net_device *dev, int request,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 void (*func)(uint, struct net_device *));
241
Sascha Hauer22f6b862009-04-15 01:32:18 +0000242/* Make MII read/write commands for the FEC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243#define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18))
244#define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | \
245 (VAL & 0xffff))
246#define mk_mii_end 0
247
Sascha Hauer22f6b862009-04-15 01:32:18 +0000248/* Transmitter timeout */
249#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Sascha Hauer22f6b862009-04-15 01:32:18 +0000251/* Register definitions for the PHY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253#define MII_REG_CR 0 /* Control Register */
254#define MII_REG_SR 1 /* Status Register */
255#define MII_REG_PHYIR1 2 /* PHY Identification Register 1 */
256#define MII_REG_PHYIR2 3 /* PHY Identification Register 2 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400257#define MII_REG_ANAR 4 /* A-N Advertisement Register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258#define MII_REG_ANLPAR 5 /* A-N Link Partner Ability Register */
259#define MII_REG_ANER 6 /* A-N Expansion Register */
260#define MII_REG_ANNPTR 7 /* A-N Next Page Transmit Register */
261#define MII_REG_ANLPRNPR 8 /* A-N Link Partner Received Next Page Reg. */
262
263/* values for phy_status */
264
265#define PHY_CONF_ANE 0x0001 /* 1 auto-negotiation enabled */
266#define PHY_CONF_LOOP 0x0002 /* 1 loopback mode enabled */
267#define PHY_CONF_SPMASK 0x00f0 /* mask for speed */
268#define PHY_CONF_10HDX 0x0010 /* 10 Mbit half duplex supported */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400269#define PHY_CONF_10FDX 0x0020 /* 10 Mbit full duplex supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270#define PHY_CONF_100HDX 0x0040 /* 100 Mbit half duplex supported */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400271#define PHY_CONF_100FDX 0x0080 /* 100 Mbit full duplex supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273#define PHY_STAT_LINK 0x0100 /* 1 up - 0 down */
274#define PHY_STAT_FAULT 0x0200 /* 1 remote fault */
275#define PHY_STAT_ANC 0x0400 /* 1 auto-negotiation complete */
276#define PHY_STAT_SPMASK 0xf000 /* mask for speed */
277#define PHY_STAT_10HDX 0x1000 /* 10 Mbit half duplex selected */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400278#define PHY_STAT_10FDX 0x2000 /* 10 Mbit full duplex selected */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279#define PHY_STAT_100HDX 0x4000 /* 100 Mbit half duplex selected */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400280#define PHY_STAT_100FDX 0x8000 /* 100 Mbit full duplex selected */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282
283static int
284fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
285{
Sascha Hauerf44d6302009-04-15 03:11:30 +0000286 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000287 struct bufdesc *bdp;
Greg Ungerer9555b312009-08-06 17:58:18 +0000288 void *bufaddr;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000289 unsigned short status;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000290 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (!fep->link) {
293 /* Link is down or autonegotiation is in progress. */
Patrick McHardy5b548142009-06-12 06:22:29 +0000294 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000297 spin_lock_irqsave(&fep->hw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 /* Fill in a Tx ring entry */
299 bdp = fep->cur_tx;
300
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000301 status = bdp->cbd_sc;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000302
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000303 if (status & BD_ENET_TX_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 /* Ooops. All transmit buffers are full. Bail out.
305 * This should not happen, since dev->tbusy should be set.
306 */
307 printk("%s: tx queue full!.\n", dev->name);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000308 spin_unlock_irqrestore(&fep->hw_lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +0000309 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Sascha Hauer22f6b862009-04-15 01:32:18 +0000312 /* Clear all of the status flags */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000313 status &= ~BD_ENET_TX_STATS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Sascha Hauer22f6b862009-04-15 01:32:18 +0000315 /* Set buffer length and buffer pointer */
Greg Ungerer9555b312009-08-06 17:58:18 +0000316 bufaddr = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 bdp->cbd_datlen = skb->len;
318
319 /*
Sascha Hauer22f6b862009-04-15 01:32:18 +0000320 * On some FEC implementations data must be aligned on
321 * 4-byte boundaries. Use bounce buffers to copy data
322 * and get it aligned. Ugh.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 */
Greg Ungerer9555b312009-08-06 17:58:18 +0000324 if (((unsigned long) bufaddr) & FEC_ALIGNMENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 unsigned int index;
326 index = bdp - fep->tx_bd_base;
Sascha Hauer6989f512009-01-28 23:03:06 +0000327 memcpy(fep->tx_bounce[index], (void *)skb->data, skb->len);
Greg Ungerer9555b312009-08-06 17:58:18 +0000328 bufaddr = fep->tx_bounce[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330
Sascha Hauer22f6b862009-04-15 01:32:18 +0000331 /* Save skb pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 fep->tx_skbuff[fep->skb_cur] = skb;
333
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700334 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 fep->skb_cur = (fep->skb_cur+1) & TX_RING_MOD_MASK;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 /* Push the data cache so the CPM does not get stale memory
338 * data.
339 */
Greg Ungerer9555b312009-08-06 17:58:18 +0000340 bdp->cbd_bufaddr = dma_map_single(&dev->dev, bufaddr,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000341 FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000343 /* Send it on its way. Tell FEC it's ready, interrupt when done,
344 * it's the last BD of the frame, and to put the CRC on the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000346 status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 | BD_ENET_TX_LAST | BD_ENET_TX_TC);
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000348 bdp->cbd_sc = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 dev->trans_start = jiffies;
351
352 /* Trigger transmission start */
Sascha Hauerf44d6302009-04-15 03:11:30 +0000353 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Sascha Hauer22f6b862009-04-15 01:32:18 +0000355 /* If this was the last BD in the ring, start at the beginning again. */
356 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 bdp = fep->tx_bd_base;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000358 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 bdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 if (bdp == fep->dirty_tx) {
362 fep->tx_full = 1;
363 netif_stop_queue(dev);
364 }
365
Sascha Hauer2e285322009-04-15 01:32:16 +0000366 fep->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000368 spin_unlock_irqrestore(&fep->hw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Patrick McHardy6ed10652009-06-23 06:03:08 +0000370 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373static void
374fec_timeout(struct net_device *dev)
375{
376 struct fec_enet_private *fep = netdev_priv(dev);
377
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700378 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000380 fec_restart(dev, fep->full_duplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 netif_wake_queue(dev);
382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100385fec_enet_interrupt(int irq, void * dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 struct net_device *dev = dev_id;
Sascha Hauerf44d6302009-04-15 03:11:30 +0000388 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 uint int_events;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000390 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000392 do {
Sascha Hauerf44d6302009-04-15 03:11:30 +0000393 int_events = readl(fep->hwp + FEC_IEVENT);
394 writel(int_events, fep->hwp + FEC_IEVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (int_events & FEC_ENET_RXF) {
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000397 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 fec_enet_rx(dev);
399 }
400
401 /* Transmit OK, or non-fatal error. Update the buffer
Sascha Hauerf44d6302009-04-15 03:11:30 +0000402 * descriptors. FEC handles all errors, we just discover
403 * them as part of the transmit process.
404 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (int_events & FEC_ENET_TXF) {
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000406 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 fec_enet_tx(dev);
408 }
409
410 if (int_events & FEC_ENET_MII) {
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000411 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 fec_enet_mii(dev);
413 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400414
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000415 } while (int_events);
416
417 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420
421static void
422fec_enet_tx(struct net_device *dev)
423{
424 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +0000425 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000426 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 struct sk_buff *skb;
428
429 fep = netdev_priv(dev);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000430 spin_lock_irq(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 bdp = fep->dirty_tx;
432
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000433 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000434 if (bdp == fep->cur_tx && fep->tx_full == 0)
435 break;
436
437 dma_unmap_single(&dev->dev, bdp->cbd_bufaddr, FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
438 bdp->cbd_bufaddr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 skb = fep->tx_skbuff[fep->skb_dirty];
441 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000442 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 BD_ENET_TX_RL | BD_ENET_TX_UN |
444 BD_ENET_TX_CSL)) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700445 dev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000446 if (status & BD_ENET_TX_HB) /* No heartbeat */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700447 dev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000448 if (status & BD_ENET_TX_LC) /* Late collision */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700449 dev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000450 if (status & BD_ENET_TX_RL) /* Retrans limit */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700451 dev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000452 if (status & BD_ENET_TX_UN) /* Underrun */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700453 dev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000454 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700455 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 } else {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700457 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000460 if (status & BD_ENET_TX_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 printk("HEY! Enet xmit interrupt and TX_READY.\n");
Sascha Hauer22f6b862009-04-15 01:32:18 +0000462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 /* Deferred means some collisions occurred during transmit,
464 * but we eventually sent the packet OK.
465 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000466 if (status & BD_ENET_TX_DEF)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700467 dev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400468
Sascha Hauer22f6b862009-04-15 01:32:18 +0000469 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 dev_kfree_skb_any(skb);
471 fep->tx_skbuff[fep->skb_dirty] = NULL;
472 fep->skb_dirty = (fep->skb_dirty + 1) & TX_RING_MOD_MASK;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400473
Sascha Hauer22f6b862009-04-15 01:32:18 +0000474 /* Update pointer to next buffer descriptor to be transmitted */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000475 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 bdp = fep->tx_bd_base;
477 else
478 bdp++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400479
Sascha Hauer22f6b862009-04-15 01:32:18 +0000480 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 */
482 if (fep->tx_full) {
483 fep->tx_full = 0;
484 if (netif_queue_stopped(dev))
485 netif_wake_queue(dev);
486 }
487 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000488 fep->dirty_tx = bdp;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000489 spin_unlock_irq(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492
493/* During a receive, the cur_rx points to the current incoming buffer.
494 * When we update through the ring, if the next incoming buffer has
495 * not been given to the system, we just set the empty indicator,
496 * effectively tossing the packet.
497 */
498static void
499fec_enet_rx(struct net_device *dev)
500{
Sascha Hauerf44d6302009-04-15 03:11:30 +0000501 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000502 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000503 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 struct sk_buff *skb;
505 ushort pkt_len;
506 __u8 *data;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400507
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000508#ifdef CONFIG_M532x
509 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400510#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000512 spin_lock_irq(&fep->hw_lock);
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /* First, grab all of the stats for the incoming packet.
515 * These get messed up if we get called due to a busy condition.
516 */
517 bdp = fep->cur_rx;
518
Sascha Hauer22f6b862009-04-15 01:32:18 +0000519 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Sascha Hauer22f6b862009-04-15 01:32:18 +0000521 /* Since we have allocated space to hold a complete frame,
522 * the last indicator should be set.
523 */
524 if ((status & BD_ENET_RX_LAST) == 0)
525 printk("FEC ENET: rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Sascha Hauer22f6b862009-04-15 01:32:18 +0000527 if (!fep->opened)
528 goto rx_processing_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Sascha Hauer22f6b862009-04-15 01:32:18 +0000530 /* Check for errors. */
531 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Sascha Hauer22f6b862009-04-15 01:32:18 +0000533 dev->stats.rx_errors++;
534 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
535 /* Frame too long or too short. */
536 dev->stats.rx_length_errors++;
537 }
538 if (status & BD_ENET_RX_NO) /* Frame alignment */
539 dev->stats.rx_frame_errors++;
540 if (status & BD_ENET_RX_CR) /* CRC Error */
541 dev->stats.rx_crc_errors++;
542 if (status & BD_ENET_RX_OV) /* FIFO overrun */
543 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
Sascha Hauer22f6b862009-04-15 01:32:18 +0000545
546 /* Report late collisions as a frame error.
547 * On this error, the BD is closed, but we don't know what we
548 * have in the buffer. So, just drop this frame on the floor.
549 */
550 if (status & BD_ENET_RX_CL) {
551 dev->stats.rx_errors++;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700552 dev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000553 goto rx_processing_done;
554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Sascha Hauer22f6b862009-04-15 01:32:18 +0000556 /* Process the incoming frame. */
557 dev->stats.rx_packets++;
558 pkt_len = bdp->cbd_datlen;
559 dev->stats.rx_bytes += pkt_len;
560 data = (__u8*)__va(bdp->cbd_bufaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000562 dma_unmap_single(NULL, bdp->cbd_bufaddr, bdp->cbd_datlen,
563 DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +0000564
Sascha Hauer22f6b862009-04-15 01:32:18 +0000565 /* This does 16 byte alignment, exactly what we need.
566 * The packet length includes FCS, but we don't want to
567 * include that when passing upstream as it messes up
568 * bridging applications.
569 */
Sascha Hauer85498892009-04-15 01:32:21 +0000570 skb = dev_alloc_skb(pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Sascha Hauer85498892009-04-15 01:32:21 +0000572 if (unlikely(!skb)) {
Sascha Hauer22f6b862009-04-15 01:32:18 +0000573 printk("%s: Memory squeeze, dropping packet.\n",
574 dev->name);
575 dev->stats.rx_dropped++;
576 } else {
Sascha Hauer85498892009-04-15 01:32:21 +0000577 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000578 skb_put(skb, pkt_len - 4); /* Make room */
579 skb_copy_to_linear_data(skb, data, pkt_len - 4);
580 skb->protocol = eth_type_trans(skb, dev);
581 netif_rx(skb);
582 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000583
584 bdp->cbd_bufaddr = dma_map_single(NULL, data, bdp->cbd_datlen,
585 DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000586rx_processing_done:
587 /* Clear the status flags for this buffer */
588 status &= ~BD_ENET_RX_STATS;
589
590 /* Mark the buffer empty */
591 status |= BD_ENET_RX_EMPTY;
592 bdp->cbd_sc = status;
593
594 /* Update BD pointer to next entry */
595 if (status & BD_ENET_RX_WRAP)
596 bdp = fep->rx_bd_base;
597 else
598 bdp++;
599 /* Doing this here will keep the FEC running while we process
600 * incoming frames. On a heavily loaded network, we should be
601 * able to keep up at the expense of system resources.
602 */
603 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000605 fep->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000607 spin_unlock_irq(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000610/* called from interrupt context */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611static void
612fec_enet_mii(struct net_device *dev)
613{
614 struct fec_enet_private *fep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 mii_list_t *mip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 fep = netdev_priv(dev);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000618 spin_lock_irq(&fep->mii_lock);
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if ((mip = mii_head) == NULL) {
621 printk("MII and no head!\n");
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000622 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
625 if (mip->mii_func != NULL)
Sascha Hauerf44d6302009-04-15 03:11:30 +0000626 (*(mip->mii_func))(readl(fep->hwp + FEC_MII_DATA), dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 mii_head = mip->mii_next;
629 mip->mii_next = mii_free;
630 mii_free = mip;
631
632 if ((mip = mii_head) != NULL)
Sascha Hauerf44d6302009-04-15 03:11:30 +0000633 writel(mip->mii_regval, fep->hwp + FEC_MII_DATA);
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000634
635unlock:
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000636 spin_unlock_irq(&fep->mii_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
639static int
640mii_queue(struct net_device *dev, int regval, void (*func)(uint, struct net_device *))
641{
642 struct fec_enet_private *fep;
643 unsigned long flags;
644 mii_list_t *mip;
645 int retval;
646
Sascha Hauer22f6b862009-04-15 01:32:18 +0000647 /* Add PHY address to register command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 fep = netdev_priv(dev);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000649 spin_lock_irqsave(&fep->mii_lock, flags);
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 regval |= fep->phy_addr << 23;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 retval = 0;
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if ((mip = mii_free) != NULL) {
655 mii_free = mip->mii_next;
656 mip->mii_regval = regval;
657 mip->mii_func = func;
658 mip->mii_next = NULL;
659 if (mii_head) {
660 mii_tail->mii_next = mip;
661 mii_tail = mip;
Philippe De Muyterf909b1e2007-10-23 14:37:54 +1000662 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 mii_head = mii_tail = mip;
Sascha Hauerf44d6302009-04-15 03:11:30 +0000664 writel(regval, fep->hwp + FEC_MII_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
Philippe De Muyterf909b1e2007-10-23 14:37:54 +1000666 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 retval = 1;
668 }
669
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000670 spin_unlock_irqrestore(&fep->mii_lock, flags);
671 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674static void mii_do_cmd(struct net_device *dev, const phy_cmd_t *c)
675{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if(!c)
677 return;
678
Philippe De Muyterbe6cb662007-10-23 14:37:54 +1000679 for (; c->mii_data != mk_mii_end; c++)
680 mii_queue(dev, c->mii_data, c->funct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
683static void mii_parse_sr(uint mii_reg, struct net_device *dev)
684{
685 struct fec_enet_private *fep = netdev_priv(dev);
686 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000687 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000689 status = *s & ~(PHY_STAT_LINK | PHY_STAT_FAULT | PHY_STAT_ANC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 if (mii_reg & 0x0004)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000692 status |= PHY_STAT_LINK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (mii_reg & 0x0010)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000694 status |= PHY_STAT_FAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (mii_reg & 0x0020)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000696 status |= PHY_STAT_ANC;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000697 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
700static void mii_parse_cr(uint mii_reg, struct net_device *dev)
701{
702 struct fec_enet_private *fep = netdev_priv(dev);
703 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000704 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000706 status = *s & ~(PHY_CONF_ANE | PHY_CONF_LOOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 if (mii_reg & 0x1000)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000709 status |= PHY_CONF_ANE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (mii_reg & 0x4000)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000711 status |= PHY_CONF_LOOP;
712 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
715static void mii_parse_anar(uint mii_reg, struct net_device *dev)
716{
717 struct fec_enet_private *fep = netdev_priv(dev);
718 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000719 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000721 status = *s & ~(PHY_CONF_SPMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 if (mii_reg & 0x0020)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000724 status |= PHY_CONF_10HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (mii_reg & 0x0040)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000726 status |= PHY_CONF_10FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (mii_reg & 0x0080)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000728 status |= PHY_CONF_100HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (mii_reg & 0x00100)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000730 status |= PHY_CONF_100FDX;
731 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
734/* ------------------------------------------------------------------------- */
735/* The Level one LXT970 is used by many boards */
736
737#define MII_LXT970_MIRROR 16 /* Mirror register */
738#define MII_LXT970_IER 17 /* Interrupt Enable Register */
739#define MII_LXT970_ISR 18 /* Interrupt Status Register */
740#define MII_LXT970_CONFIG 19 /* Configuration Register */
741#define MII_LXT970_CSR 20 /* Chip Status Register */
742
743static void mii_parse_lxt970_csr(uint mii_reg, struct net_device *dev)
744{
745 struct fec_enet_private *fep = netdev_priv(dev);
746 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000747 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000749 status = *s & ~(PHY_STAT_SPMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (mii_reg & 0x0800) {
751 if (mii_reg & 0x1000)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000752 status |= PHY_STAT_100FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000754 status |= PHY_STAT_100HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 } else {
756 if (mii_reg & 0x1000)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000757 status |= PHY_STAT_10FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000759 status |= PHY_STAT_10HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000761 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000764static phy_cmd_t const phy_cmd_lxt970_config[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 { mk_mii_read(MII_REG_CR), mii_parse_cr },
766 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
767 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000768 };
769static phy_cmd_t const phy_cmd_lxt970_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 { mk_mii_write(MII_LXT970_IER, 0x0002), NULL },
771 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
772 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000773 };
774static phy_cmd_t const phy_cmd_lxt970_ack_int[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 /* read SR and ISR to acknowledge */
776 { mk_mii_read(MII_REG_SR), mii_parse_sr },
777 { mk_mii_read(MII_LXT970_ISR), NULL },
778
779 /* find out the current status */
780 { mk_mii_read(MII_LXT970_CSR), mii_parse_lxt970_csr },
781 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000782 };
783static phy_cmd_t const phy_cmd_lxt970_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 { mk_mii_write(MII_LXT970_IER, 0x0000), NULL },
785 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000786 };
787static phy_info_t const phy_info_lxt970 = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400788 .id = 0x07810000,
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000789 .name = "LXT970",
790 .config = phy_cmd_lxt970_config,
791 .startup = phy_cmd_lxt970_startup,
792 .ack_int = phy_cmd_lxt970_ack_int,
793 .shutdown = phy_cmd_lxt970_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794};
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796/* ------------------------------------------------------------------------- */
797/* The Level one LXT971 is used on some of my custom boards */
798
799/* register definitions for the 971 */
800
801#define MII_LXT971_PCR 16 /* Port Control Register */
802#define MII_LXT971_SR2 17 /* Status Register 2 */
803#define MII_LXT971_IER 18 /* Interrupt Enable Register */
804#define MII_LXT971_ISR 19 /* Interrupt Status Register */
805#define MII_LXT971_LCR 20 /* LED Control Register */
806#define MII_LXT971_TCR 30 /* Transmit Control Register */
807
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400808/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 * I had some nice ideas of running the MDIO faster...
810 * The 971 should support 8MHz and I tried it, but things acted really
811 * weird, so 2.5 MHz ought to be enough for anyone...
812 */
813
814static void mii_parse_lxt971_sr2(uint mii_reg, struct net_device *dev)
815{
816 struct fec_enet_private *fep = netdev_priv(dev);
817 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000818 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000820 status = *s & ~(PHY_STAT_SPMASK | PHY_STAT_LINK | PHY_STAT_ANC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 if (mii_reg & 0x0400) {
823 fep->link = 1;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000824 status |= PHY_STAT_LINK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 } else {
826 fep->link = 0;
827 }
828 if (mii_reg & 0x0080)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000829 status |= PHY_STAT_ANC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (mii_reg & 0x4000) {
831 if (mii_reg & 0x0200)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000832 status |= PHY_STAT_100FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000834 status |= PHY_STAT_100HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 } else {
836 if (mii_reg & 0x0200)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000837 status |= PHY_STAT_10FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000839 status |= PHY_STAT_10HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841 if (mii_reg & 0x0008)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000842 status |= PHY_STAT_FAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000844 *s = status;
845}
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400846
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000847static phy_cmd_t const phy_cmd_lxt971_config[] = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400848 /* limit to 10MBit because my prototype board
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 * doesn't work with 100. */
850 { mk_mii_read(MII_REG_CR), mii_parse_cr },
851 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
852 { mk_mii_read(MII_LXT971_SR2), mii_parse_lxt971_sr2 },
853 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000854 };
855static phy_cmd_t const phy_cmd_lxt971_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 { mk_mii_write(MII_LXT971_IER, 0x00f2), NULL },
857 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
858 { mk_mii_write(MII_LXT971_LCR, 0xd422), NULL }, /* LED config */
859 /* Somehow does the 971 tell me that the link is down
860 * the first read after power-up.
861 * read here to get a valid value in ack_int */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400862 { mk_mii_read(MII_REG_SR), mii_parse_sr },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000864 };
865static phy_cmd_t const phy_cmd_lxt971_ack_int[] = {
866 /* acknowledge the int before reading status ! */
867 { mk_mii_read(MII_LXT971_ISR), NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /* find out the current status */
869 { mk_mii_read(MII_REG_SR), mii_parse_sr },
870 { mk_mii_read(MII_LXT971_SR2), mii_parse_lxt971_sr2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000872 };
873static phy_cmd_t const phy_cmd_lxt971_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 { mk_mii_write(MII_LXT971_IER, 0x0000), NULL },
875 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000876 };
877static phy_info_t const phy_info_lxt971 = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400878 .id = 0x0001378e,
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000879 .name = "LXT971",
880 .config = phy_cmd_lxt971_config,
881 .startup = phy_cmd_lxt971_startup,
882 .ack_int = phy_cmd_lxt971_ack_int,
883 .shutdown = phy_cmd_lxt971_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884};
885
886/* ------------------------------------------------------------------------- */
887/* The Quality Semiconductor QS6612 is used on the RPX CLLF */
888
889/* register definitions */
890
891#define MII_QS6612_MCR 17 /* Mode Control Register */
892#define MII_QS6612_FTR 27 /* Factory Test Register */
893#define MII_QS6612_MCO 28 /* Misc. Control Register */
894#define MII_QS6612_ISR 29 /* Interrupt Source Register */
895#define MII_QS6612_IMR 30 /* Interrupt Mask Register */
896#define MII_QS6612_PCR 31 /* 100BaseTx PHY Control Reg. */
897
898static void mii_parse_qs6612_pcr(uint mii_reg, struct net_device *dev)
899{
900 struct fec_enet_private *fep = netdev_priv(dev);
901 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000902 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000904 status = *s & ~(PHY_STAT_SPMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 switch((mii_reg >> 2) & 7) {
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000907 case 1: status |= PHY_STAT_10HDX; break;
908 case 2: status |= PHY_STAT_100HDX; break;
909 case 5: status |= PHY_STAT_10FDX; break;
910 case 6: status |= PHY_STAT_100FDX; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000913 *s = status;
914}
915
916static phy_cmd_t const phy_cmd_qs6612_config[] = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400917 /* The PHY powers up isolated on the RPX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 * so send a command to allow operation.
919 */
920 { mk_mii_write(MII_QS6612_PCR, 0x0dc0), NULL },
921
922 /* parse cr and anar to get some info */
923 { mk_mii_read(MII_REG_CR), mii_parse_cr },
924 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
925 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000926 };
927static phy_cmd_t const phy_cmd_qs6612_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 { mk_mii_write(MII_QS6612_IMR, 0x003a), NULL },
929 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
930 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000931 };
932static phy_cmd_t const phy_cmd_qs6612_ack_int[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /* we need to read ISR, SR and ANER to acknowledge */
934 { mk_mii_read(MII_QS6612_ISR), NULL },
935 { mk_mii_read(MII_REG_SR), mii_parse_sr },
936 { mk_mii_read(MII_REG_ANER), NULL },
937
938 /* read pcr to get info */
939 { mk_mii_read(MII_QS6612_PCR), mii_parse_qs6612_pcr },
940 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000941 };
942static phy_cmd_t const phy_cmd_qs6612_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 { mk_mii_write(MII_QS6612_IMR, 0x0000), NULL },
944 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000945 };
946static phy_info_t const phy_info_qs6612 = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400947 .id = 0x00181440,
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000948 .name = "QS6612",
949 .config = phy_cmd_qs6612_config,
950 .startup = phy_cmd_qs6612_startup,
951 .ack_int = phy_cmd_qs6612_ack_int,
952 .shutdown = phy_cmd_qs6612_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953};
954
955/* ------------------------------------------------------------------------- */
956/* AMD AM79C874 phy */
957
958/* register definitions for the 874 */
959
960#define MII_AM79C874_MFR 16 /* Miscellaneous Feature Register */
961#define MII_AM79C874_ICSR 17 /* Interrupt/Status Register */
962#define MII_AM79C874_DR 18 /* Diagnostic Register */
963#define MII_AM79C874_PMLR 19 /* Power and Loopback Register */
964#define MII_AM79C874_MCR 21 /* ModeControl Register */
965#define MII_AM79C874_DC 23 /* Disconnect Counter */
966#define MII_AM79C874_REC 24 /* Recieve Error Counter */
967
968static void mii_parse_am79c874_dr(uint mii_reg, struct net_device *dev)
969{
970 struct fec_enet_private *fep = netdev_priv(dev);
971 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000972 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000974 status = *s & ~(PHY_STAT_SPMASK | PHY_STAT_ANC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 if (mii_reg & 0x0080)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000977 status |= PHY_STAT_ANC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (mii_reg & 0x0400)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000979 status |= ((mii_reg & 0x0800) ? PHY_STAT_100FDX : PHY_STAT_100HDX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000981 status |= ((mii_reg & 0x0800) ? PHY_STAT_10FDX : PHY_STAT_10HDX);
982
983 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000986static phy_cmd_t const phy_cmd_am79c874_config[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 { mk_mii_read(MII_REG_CR), mii_parse_cr },
988 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
989 { mk_mii_read(MII_AM79C874_DR), mii_parse_am79c874_dr },
990 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000991 };
992static phy_cmd_t const phy_cmd_am79c874_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 { mk_mii_write(MII_AM79C874_ICSR, 0xff00), NULL },
994 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400995 { mk_mii_read(MII_REG_SR), mii_parse_sr },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000997 };
998static phy_cmd_t const phy_cmd_am79c874_ack_int[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 /* find out the current status */
1000 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1001 { mk_mii_read(MII_AM79C874_DR), mii_parse_am79c874_dr },
1002 /* we only need to read ISR to acknowledge */
1003 { mk_mii_read(MII_AM79C874_ICSR), NULL },
1004 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001005 };
1006static phy_cmd_t const phy_cmd_am79c874_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 { mk_mii_write(MII_AM79C874_ICSR, 0x0000), NULL },
1008 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001009 };
1010static phy_info_t const phy_info_am79c874 = {
1011 .id = 0x00022561,
1012 .name = "AM79C874",
1013 .config = phy_cmd_am79c874_config,
1014 .startup = phy_cmd_am79c874_startup,
1015 .ack_int = phy_cmd_am79c874_ack_int,
1016 .shutdown = phy_cmd_am79c874_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017};
1018
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020/* ------------------------------------------------------------------------- */
1021/* Kendin KS8721BL phy */
1022
1023/* register definitions for the 8721 */
1024
1025#define MII_KS8721BL_RXERCR 21
Sascha Hauer43268dc2009-01-28 23:03:08 +00001026#define MII_KS8721BL_ICSR 27
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027#define MII_KS8721BL_PHYCR 31
1028
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001029static phy_cmd_t const phy_cmd_ks8721bl_config[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1031 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1032 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001033 };
1034static phy_cmd_t const phy_cmd_ks8721bl_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 { mk_mii_write(MII_KS8721BL_ICSR, 0xff00), NULL },
1036 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001037 { mk_mii_read(MII_REG_SR), mii_parse_sr },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001039 };
1040static phy_cmd_t const phy_cmd_ks8721bl_ack_int[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 /* find out the current status */
1042 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1043 /* we only need to read ISR to acknowledge */
1044 { mk_mii_read(MII_KS8721BL_ICSR), NULL },
1045 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001046 };
1047static phy_cmd_t const phy_cmd_ks8721bl_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 { mk_mii_write(MII_KS8721BL_ICSR, 0x0000), NULL },
1049 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001050 };
1051static phy_info_t const phy_info_ks8721bl = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001052 .id = 0x00022161,
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001053 .name = "KS8721BL",
1054 .config = phy_cmd_ks8721bl_config,
1055 .startup = phy_cmd_ks8721bl_startup,
1056 .ack_int = phy_cmd_ks8721bl_ack_int,
1057 .shutdown = phy_cmd_ks8721bl_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058};
1059
1060/* ------------------------------------------------------------------------- */
Greg Ungerer562d2f82005-11-07 14:09:50 +10001061/* register definitions for the DP83848 */
1062
1063#define MII_DP8384X_PHYSTST 16 /* PHY Status Register */
1064
1065static void mii_parse_dp8384x_sr2(uint mii_reg, struct net_device *dev)
1066{
Wang Chen4cf16532008-11-12 23:38:14 -08001067 struct fec_enet_private *fep = netdev_priv(dev);
Greg Ungerer562d2f82005-11-07 14:09:50 +10001068 volatile uint *s = &(fep->phy_status);
1069
1070 *s &= ~(PHY_STAT_SPMASK | PHY_STAT_LINK | PHY_STAT_ANC);
1071
1072 /* Link up */
1073 if (mii_reg & 0x0001) {
1074 fep->link = 1;
1075 *s |= PHY_STAT_LINK;
1076 } else
1077 fep->link = 0;
1078 /* Status of link */
1079 if (mii_reg & 0x0010) /* Autonegotioation complete */
1080 *s |= PHY_STAT_ANC;
1081 if (mii_reg & 0x0002) { /* 10MBps? */
1082 if (mii_reg & 0x0004) /* Full Duplex? */
1083 *s |= PHY_STAT_10FDX;
1084 else
1085 *s |= PHY_STAT_10HDX;
1086 } else { /* 100 Mbps? */
1087 if (mii_reg & 0x0004) /* Full Duplex? */
1088 *s |= PHY_STAT_100FDX;
1089 else
1090 *s |= PHY_STAT_100HDX;
1091 }
1092 if (mii_reg & 0x0008)
1093 *s |= PHY_STAT_FAULT;
1094}
1095
1096static phy_info_t phy_info_dp83848= {
1097 0x020005c9,
1098 "DP83848",
1099
1100 (const phy_cmd_t []) { /* config */
1101 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1102 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1103 { mk_mii_read(MII_DP8384X_PHYSTST), mii_parse_dp8384x_sr2 },
1104 { mk_mii_end, }
1105 },
1106 (const phy_cmd_t []) { /* startup - enable interrupts */
1107 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
1108 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1109 { mk_mii_end, }
1110 },
1111 (const phy_cmd_t []) { /* ack_int - never happens, no interrupt */
1112 { mk_mii_end, }
1113 },
1114 (const phy_cmd_t []) { /* shutdown */
1115 { mk_mii_end, }
1116 },
1117};
1118
1119/* ------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001121static phy_info_t const * const phy_info[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 &phy_info_lxt970,
1123 &phy_info_lxt971,
1124 &phy_info_qs6612,
1125 &phy_info_am79c874,
1126 &phy_info_ks8721bl,
Greg Ungerer562d2f82005-11-07 14:09:50 +10001127 &phy_info_dp83848,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 NULL
1129};
1130
1131/* ------------------------------------------------------------------------- */
Sebastian Siewiorc1d96152008-05-01 14:04:02 +10001132#ifdef HAVE_mii_link_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001134mii_link_interrupt(int irq, void * dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136/*
Greg Ungerer43be6362009-02-26 22:42:51 -08001137 * This is specific to the MII interrupt setup of the M5272EVB.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 */
Greg Ungerer43be6362009-02-26 22:42:51 -08001139static void __inline__ fec_request_mii_intr(struct net_device *dev)
1140{
1141 if (request_irq(66, mii_link_interrupt, IRQF_DISABLED, "fec(MII)", dev) != 0)
1142 printk("FEC: Could not allocate fec(MII) IRQ(66)!\n");
1143}
1144
1145static void __inline__ fec_disable_phy_intr(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 volatile unsigned long *icrp;
Greg Ungerer43be6362009-02-26 22:42:51 -08001148 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR1);
1149 *icrp = 0x08000000;
1150}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Greg Ungerer43be6362009-02-26 22:42:51 -08001152static void __inline__ fec_phy_ack_intr(void)
1153{
1154 volatile unsigned long *icrp;
1155 /* Acknowledge the interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR1);
Greg Ungererf861d622007-07-30 16:29:16 +10001157 *icrp = 0x0d000000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
Sascha Hauerfb922b02009-04-08 15:44:45 -07001159#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Greg Ungerer43be6362009-02-26 22:42:51 -08001161#ifdef CONFIG_M5272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162static void __inline__ fec_get_mac(struct net_device *dev)
1163{
1164 struct fec_enet_private *fep = netdev_priv(dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001165 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001167 if (FEC_FLASHMAC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 /*
1169 * Get MAC address from FLASH.
1170 * If it is all 1's or 0's, use the default.
1171 */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001172 iap = (unsigned char *)FEC_FLASHMAC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if ((iap[0] == 0) && (iap[1] == 0) && (iap[2] == 0) &&
1174 (iap[3] == 0) && (iap[4] == 0) && (iap[5] == 0))
1175 iap = fec_mac_default;
1176 if ((iap[0] == 0xff) && (iap[1] == 0xff) && (iap[2] == 0xff) &&
1177 (iap[3] == 0xff) && (iap[4] == 0xff) && (iap[5] == 0xff))
1178 iap = fec_mac_default;
1179 } else {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001180 *((unsigned long *) &tmpaddr[0]) = readl(fep->hwp + FEC_ADDR_LOW);
1181 *((unsigned short *) &tmpaddr[4]) = (readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 iap = &tmpaddr[0];
1183 }
1184
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001185 memcpy(dev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 /* Adjust MAC if using default MAC address */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001188 if (iap == fec_mac_default)
1189 dev->dev_addr[ETH_ALEN-1] = fec_mac_default[ETH_ALEN-1] + fep->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191#endif
1192
1193/* ------------------------------------------------------------------------- */
1194
1195static void mii_display_status(struct net_device *dev)
1196{
1197 struct fec_enet_private *fep = netdev_priv(dev);
1198 volatile uint *s = &(fep->phy_status);
1199
1200 if (!fep->link && !fep->old_link) {
1201 /* Link is still down - don't print anything */
1202 return;
1203 }
1204
1205 printk("%s: status: ", dev->name);
1206
1207 if (!fep->link) {
1208 printk("link down");
1209 } else {
1210 printk("link up");
1211
1212 switch(*s & PHY_STAT_SPMASK) {
1213 case PHY_STAT_100FDX: printk(", 100MBit Full Duplex"); break;
1214 case PHY_STAT_100HDX: printk(", 100MBit Half Duplex"); break;
1215 case PHY_STAT_10FDX: printk(", 10MBit Full Duplex"); break;
1216 case PHY_STAT_10HDX: printk(", 10MBit Half Duplex"); break;
1217 default:
1218 printk(", Unknown speed/duplex");
1219 }
1220
1221 if (*s & PHY_STAT_ANC)
1222 printk(", auto-negotiation complete");
1223 }
1224
1225 if (*s & PHY_STAT_FAULT)
1226 printk(", remote fault");
1227
1228 printk(".\n");
1229}
1230
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001231static void mii_display_config(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001233 struct fec_enet_private *fep = container_of(work, struct fec_enet_private, phy_task);
1234 struct net_device *dev = fep->netdev;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001235 uint status = fep->phy_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 /*
1238 ** When we get here, phy_task is already removed from
1239 ** the workqueue. It is thus safe to allow to reuse it.
1240 */
1241 fep->mii_phy_task_queued = 0;
1242 printk("%s: config: auto-negotiation ", dev->name);
1243
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001244 if (status & PHY_CONF_ANE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 printk("on");
1246 else
1247 printk("off");
1248
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001249 if (status & PHY_CONF_100FDX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 printk(", 100FDX");
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001251 if (status & PHY_CONF_100HDX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 printk(", 100HDX");
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001253 if (status & PHY_CONF_10FDX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 printk(", 10FDX");
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001255 if (status & PHY_CONF_10HDX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 printk(", 10HDX");
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001257 if (!(status & PHY_CONF_SPMASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 printk(", No speed/duplex selected?");
1259
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001260 if (status & PHY_CONF_LOOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 printk(", loopback enabled");
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 printk(".\n");
1264
1265 fep->sequence_done = 1;
1266}
1267
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001268static void mii_relink(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001270 struct fec_enet_private *fep = container_of(work, struct fec_enet_private, phy_task);
1271 struct net_device *dev = fep->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 int duplex;
1273
1274 /*
1275 ** When we get here, phy_task is already removed from
1276 ** the workqueue. It is thus safe to allow to reuse it.
1277 */
1278 fep->mii_phy_task_queued = 0;
1279 fep->link = (fep->phy_status & PHY_STAT_LINK) ? 1 : 0;
1280 mii_display_status(dev);
1281 fep->old_link = fep->link;
1282
1283 if (fep->link) {
1284 duplex = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001285 if (fep->phy_status
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 & (PHY_STAT_100FDX | PHY_STAT_10FDX))
1287 duplex = 1;
1288 fec_restart(dev, duplex);
Philippe De Muyterf909b1e2007-10-23 14:37:54 +10001289 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 fec_stop(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
1292
1293/* mii_queue_relink is called in interrupt context from mii_link_interrupt */
1294static void mii_queue_relink(uint mii_reg, struct net_device *dev)
1295{
1296 struct fec_enet_private *fep = netdev_priv(dev);
1297
1298 /*
Sascha Hauer22f6b862009-04-15 01:32:18 +00001299 * We cannot queue phy_task twice in the workqueue. It
1300 * would cause an endless loop in the workqueue.
1301 * Fortunately, if the last mii_relink entry has not yet been
1302 * executed now, it will do the job for the current interrupt,
1303 * which is just what we want.
1304 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (fep->mii_phy_task_queued)
1306 return;
1307
1308 fep->mii_phy_task_queued = 1;
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001309 INIT_WORK(&fep->phy_task, mii_relink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 schedule_work(&fep->phy_task);
1311}
1312
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001313/* mii_queue_config is called in interrupt context from fec_enet_mii */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314static void mii_queue_config(uint mii_reg, struct net_device *dev)
1315{
1316 struct fec_enet_private *fep = netdev_priv(dev);
1317
1318 if (fep->mii_phy_task_queued)
1319 return;
1320
1321 fep->mii_phy_task_queued = 1;
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001322 INIT_WORK(&fep->phy_task, mii_display_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 schedule_work(&fep->phy_task);
1324}
1325
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001326phy_cmd_t const phy_cmd_relink[] = {
1327 { mk_mii_read(MII_REG_CR), mii_queue_relink },
1328 { mk_mii_end, }
1329 };
1330phy_cmd_t const phy_cmd_config[] = {
1331 { mk_mii_read(MII_REG_CR), mii_queue_config },
1332 { mk_mii_end, }
1333 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Sascha Hauer22f6b862009-04-15 01:32:18 +00001335/* Read remainder of PHY ID. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336static void
1337mii_discover_phy3(uint mii_reg, struct net_device *dev)
1338{
1339 struct fec_enet_private *fep;
1340 int i;
1341
1342 fep = netdev_priv(dev);
1343 fep->phy_id |= (mii_reg & 0xffff);
1344 printk("fec: PHY @ 0x%x, ID 0x%08x", fep->phy_addr, fep->phy_id);
1345
1346 for(i = 0; phy_info[i]; i++) {
1347 if(phy_info[i]->id == (fep->phy_id >> 4))
1348 break;
1349 }
1350
1351 if (phy_info[i])
1352 printk(" -- %s\n", phy_info[i]->name);
1353 else
1354 printk(" -- unknown PHY!\n");
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 fep->phy = phy_info[i];
1357 fep->phy_id_done = 1;
1358}
1359
1360/* Scan all of the MII PHY addresses looking for someone to respond
1361 * with a valid ID. This usually happens quickly.
1362 */
1363static void
1364mii_discover_phy(uint mii_reg, struct net_device *dev)
1365{
1366 struct fec_enet_private *fep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 uint phytype;
1368
1369 fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 if (fep->phy_addr < 32) {
1372 if ((phytype = (mii_reg & 0xffff)) != 0xffff && phytype != 0) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001373
Sascha Hauer22f6b862009-04-15 01:32:18 +00001374 /* Got first part of ID, now get remainder */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 fep->phy_id = phytype << 16;
1376 mii_queue(dev, mk_mii_read(MII_REG_PHYIR2),
1377 mii_discover_phy3);
Philippe De Muyterf909b1e2007-10-23 14:37:54 +10001378 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 fep->phy_addr++;
1380 mii_queue(dev, mk_mii_read(MII_REG_PHYIR1),
1381 mii_discover_phy);
1382 }
1383 } else {
1384 printk("FEC: No PHY device found.\n");
1385 /* Disable external MII interface */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001386 writel(0, fep->hwp + FEC_MII_SPEED);
1387 fep->phy_speed = 0;
Greg Ungerer43be6362009-02-26 22:42:51 -08001388#ifdef HAVE_mii_link_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 fec_disable_phy_intr();
Sascha Haueread73182009-01-28 23:03:11 +00001390#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392}
1393
Sascha Hauer22f6b862009-04-15 01:32:18 +00001394/* This interrupt occurs when the PHY detects a link change */
Sebastian Siewiorc1d96152008-05-01 14:04:02 +10001395#ifdef HAVE_mii_link_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001397mii_link_interrupt(int irq, void * dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
1399 struct net_device *dev = dev_id;
1400 struct fec_enet_private *fep = netdev_priv(dev);
1401
1402 fec_phy_ack_intr();
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 mii_do_cmd(dev, fep->phy->ack_int);
1405 mii_do_cmd(dev, phy_cmd_relink); /* restart and display status */
1406
1407 return IRQ_HANDLED;
1408}
Sebastian Siewiorc1d96152008-05-01 14:04:02 +10001409#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001411static void fec_enet_free_buffers(struct net_device *dev)
1412{
1413 struct fec_enet_private *fep = netdev_priv(dev);
1414 int i;
1415 struct sk_buff *skb;
1416 struct bufdesc *bdp;
1417
1418 bdp = fep->rx_bd_base;
1419 for (i = 0; i < RX_RING_SIZE; i++) {
1420 skb = fep->rx_skbuff[i];
1421
1422 if (bdp->cbd_bufaddr)
1423 dma_unmap_single(&dev->dev, bdp->cbd_bufaddr,
1424 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1425 if (skb)
1426 dev_kfree_skb(skb);
1427 bdp++;
1428 }
1429
1430 bdp = fep->tx_bd_base;
1431 for (i = 0; i < TX_RING_SIZE; i++)
1432 kfree(fep->tx_bounce[i]);
1433}
1434
1435static int fec_enet_alloc_buffers(struct net_device *dev)
1436{
1437 struct fec_enet_private *fep = netdev_priv(dev);
1438 int i;
1439 struct sk_buff *skb;
1440 struct bufdesc *bdp;
1441
1442 bdp = fep->rx_bd_base;
1443 for (i = 0; i < RX_RING_SIZE; i++) {
1444 skb = dev_alloc_skb(FEC_ENET_RX_FRSIZE);
1445 if (!skb) {
1446 fec_enet_free_buffers(dev);
1447 return -ENOMEM;
1448 }
1449 fep->rx_skbuff[i] = skb;
1450
1451 bdp->cbd_bufaddr = dma_map_single(&dev->dev, skb->data,
1452 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1453 bdp->cbd_sc = BD_ENET_RX_EMPTY;
1454 bdp++;
1455 }
1456
1457 /* Set the last buffer to wrap. */
1458 bdp--;
1459 bdp->cbd_sc |= BD_SC_WRAP;
1460
1461 bdp = fep->tx_bd_base;
1462 for (i = 0; i < TX_RING_SIZE; i++) {
1463 fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
1464
1465 bdp->cbd_sc = 0;
1466 bdp->cbd_bufaddr = 0;
1467 bdp++;
1468 }
1469
1470 /* Set the last buffer to wrap. */
1471 bdp--;
1472 bdp->cbd_sc |= BD_SC_WRAP;
1473
1474 return 0;
1475}
1476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477static int
1478fec_enet_open(struct net_device *dev)
1479{
1480 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001481 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 /* I should reset the ring buffers here, but I don't yet know
1484 * a simple way to do that.
1485 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001487 ret = fec_enet_alloc_buffers(dev);
1488 if (ret)
1489 return ret;
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 fep->sequence_done = 0;
1492 fep->link = 0;
1493
Sascha Hauerfe957c42009-04-15 01:32:25 +00001494 fec_restart(dev, 1);
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 if (fep->phy) {
1497 mii_do_cmd(dev, fep->phy->ack_int);
1498 mii_do_cmd(dev, fep->phy->config);
1499 mii_do_cmd(dev, phy_cmd_config); /* display configuration */
1500
Matt Waddel6b265292006-06-27 13:10:56 +10001501 /* Poll until the PHY tells us its configuration
1502 * (not link state).
1503 * Request is initiated by mii_do_cmd above, but answer
1504 * comes by interrupt.
1505 * This should take about 25 usec per register at 2.5 MHz,
1506 * and we read approximately 5 registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 */
1508 while(!fep->sequence_done)
1509 schedule();
1510
1511 mii_do_cmd(dev, fep->phy->startup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
1513
Sascha Hauerfe957c42009-04-15 01:32:25 +00001514 /* Set the initial link state to true. A lot of hardware
1515 * based on this device does not implement a PHY interrupt,
1516 * so we are never notified of link change.
1517 */
1518 fep->link = 1;
1519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 netif_start_queue(dev);
1521 fep->opened = 1;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001522 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
1525static int
1526fec_enet_close(struct net_device *dev)
1527{
1528 struct fec_enet_private *fep = netdev_priv(dev);
1529
Sascha Hauer22f6b862009-04-15 01:32:18 +00001530 /* Don't know what to do yet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 fep->opened = 0;
1532 netif_stop_queue(dev);
1533 fec_stop(dev);
1534
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001535 fec_enet_free_buffers(dev);
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return 0;
1538}
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540/* Set or clear the multicast filter for this adaptor.
1541 * Skeleton taken from sunlance driver.
1542 * The CPM Ethernet implementation allows Multicast as well as individual
1543 * MAC address filtering. Some of the drivers check to make sure it is
1544 * a group multicast address, and discard those that are not. I guess I
1545 * will do the same for now, but just remove the test if you want
1546 * individual filtering as well (do the upper net layers want or support
1547 * this kind of feature?).
1548 */
1549
1550#define HASH_BITS 6 /* #bits in hash */
1551#define CRC32_POLY 0xEDB88320
1552
1553static void set_multicast_list(struct net_device *dev)
1554{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001555 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 struct dev_mc_list *dmi;
Sascha Hauerf44d6302009-04-15 03:11:30 +00001557 unsigned int i, j, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 unsigned char hash;
1559
Sascha Hauer22f6b862009-04-15 01:32:18 +00001560 if (dev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001561 tmp = readl(fep->hwp + FEC_R_CNTRL);
1562 tmp |= 0x8;
1563 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00001564 return;
1565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Sascha Hauer4e831832009-04-15 01:32:19 +00001567 tmp = readl(fep->hwp + FEC_R_CNTRL);
1568 tmp &= ~0x8;
1569 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001570
Sascha Hauer4e831832009-04-15 01:32:19 +00001571 if (dev->flags & IFF_ALLMULTI) {
1572 /* Catch all multicast addresses, so set the
1573 * filter to all 1's
1574 */
1575 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1576 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Sascha Hauer4e831832009-04-15 01:32:19 +00001578 return;
1579 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001580
Sascha Hauer4e831832009-04-15 01:32:19 +00001581 /* Clear filter and add the addresses in hash register
1582 */
1583 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1584 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Sascha Hauer4e831832009-04-15 01:32:19 +00001586 dmi = dev->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Sascha Hauer4e831832009-04-15 01:32:19 +00001588 for (j = 0; j < dev->mc_count; j++, dmi = dmi->next) {
1589 /* Only support group multicast for now */
1590 if (!(dmi->dmi_addr[0] & 1))
1591 continue;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001592
Sascha Hauer4e831832009-04-15 01:32:19 +00001593 /* calculate crc32 value of mac address */
1594 crc = 0xffffffff;
1595
1596 for (i = 0; i < dmi->dmi_addrlen; i++) {
1597 data = dmi->dmi_addr[i];
1598 for (bit = 0; bit < 8; bit++, data >>= 1) {
1599 crc = (crc >> 1) ^
1600 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
1602 }
Sascha Hauer4e831832009-04-15 01:32:19 +00001603
1604 /* only upper 6 bits (HASH_BITS) are used
1605 * which point to specific bit in he hash registers
1606 */
1607 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
1608
1609 if (hash > 31) {
1610 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1611 tmp |= 1 << (hash - 32);
1612 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1613 } else {
1614 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1615 tmp |= 1 << hash;
1616 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
1619}
1620
Sascha Hauer22f6b862009-04-15 01:32:18 +00001621/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00001622static int
1623fec_set_mac_address(struct net_device *dev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001625 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer009fda82009-04-15 01:32:23 +00001626 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Sascha Hauer009fda82009-04-15 01:32:23 +00001628 if (!is_valid_ether_addr(addr->sa_data))
1629 return -EADDRNOTAVAIL;
1630
1631 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1632
Sascha Hauerf44d6302009-04-15 03:11:30 +00001633 writel(dev->dev_addr[3] | (dev->dev_addr[2] << 8) |
1634 (dev->dev_addr[1] << 16) | (dev->dev_addr[0] << 24),
1635 fep->hwp + FEC_ADDR_LOW);
1636 writel((dev->dev_addr[5] << 16) | (dev->dev_addr[4] << 24),
1637 fep + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00001638 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
Sascha Hauer009fda82009-04-15 01:32:23 +00001641static const struct net_device_ops fec_netdev_ops = {
1642 .ndo_open = fec_enet_open,
1643 .ndo_stop = fec_enet_close,
1644 .ndo_start_xmit = fec_enet_start_xmit,
1645 .ndo_set_multicast_list = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00001646 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00001647 .ndo_validate_addr = eth_validate_addr,
1648 .ndo_tx_timeout = fec_timeout,
1649 .ndo_set_mac_address = fec_set_mac_address,
1650};
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 /*
1653 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00001654 *
1655 * index is only used in legacy code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 */
Sascha Haueread73182009-01-28 23:03:11 +00001657int __init fec_enet_init(struct net_device *dev, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
1659 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001660 struct bufdesc *cbd_base;
1661 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001663 /* Allocate memory for buffer descriptors. */
1664 cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
1665 GFP_KERNEL);
1666 if (!cbd_base) {
Greg Ungerer562d2f82005-11-07 14:09:50 +10001667 printk("FEC: allocate descriptor memory failed?\n");
1668 return -ENOMEM;
1669 }
1670
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10001671 spin_lock_init(&fep->hw_lock);
1672 spin_lock_init(&fep->mii_lock);
1673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 fep->index = index;
Sascha Hauerf44d6302009-04-15 03:11:30 +00001675 fep->hwp = (void __iomem *)dev->base_addr;
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001676 fep->netdev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Sascha Haueread73182009-01-28 23:03:11 +00001678 /* Set the Ethernet address */
Greg Ungerer43be6362009-02-26 22:42:51 -08001679#ifdef CONFIG_M5272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 fec_get_mac(dev);
Sascha Haueread73182009-01-28 23:03:11 +00001681#else
1682 {
1683 unsigned long l;
Sascha Hauerf44d6302009-04-15 03:11:30 +00001684 l = readl(fep->hwp + FEC_ADDR_LOW);
Sascha Haueread73182009-01-28 23:03:11 +00001685 dev->dev_addr[0] = (unsigned char)((l & 0xFF000000) >> 24);
1686 dev->dev_addr[1] = (unsigned char)((l & 0x00FF0000) >> 16);
1687 dev->dev_addr[2] = (unsigned char)((l & 0x0000FF00) >> 8);
1688 dev->dev_addr[3] = (unsigned char)((l & 0x000000FF) >> 0);
Sascha Hauerf44d6302009-04-15 03:11:30 +00001689 l = readl(fep->hwp + FEC_ADDR_HIGH);
Sascha Haueread73182009-01-28 23:03:11 +00001690 dev->dev_addr[4] = (unsigned char)((l & 0xFF000000) >> 24);
1691 dev->dev_addr[5] = (unsigned char)((l & 0x00FF0000) >> 16);
1692 }
1693#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001695 /* Set receive and transmit descriptor base. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 fep->rx_bd_base = cbd_base;
1697 fep->tx_bd_base = cbd_base + RX_RING_SIZE;
1698
Greg Ungerer43be6362009-02-26 22:42:51 -08001699#ifdef HAVE_mii_link_interrupt
1700 fec_request_mii_intr(dev);
Sascha Haueread73182009-01-28 23:03:11 +00001701#endif
Sascha Hauer22f6b862009-04-15 01:32:18 +00001702 /* The FEC Ethernet specific entries in the device structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 dev->watchdog_timeo = TX_TIMEOUT;
Sascha Hauer009fda82009-04-15 01:32:23 +00001704 dev->netdev_ops = &fec_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 for (i=0; i<NMII-1; i++)
1707 mii_cmds[i].mii_next = &mii_cmds[i+1];
1708 mii_free = mii_cmds;
1709
Sascha Hauer22f6b862009-04-15 01:32:18 +00001710 /* Set MII speed to 2.5 MHz */
Sascha Haueread73182009-01-28 23:03:11 +00001711 fep->phy_speed = ((((clk_get_rate(fep->clk) / 2 + 4999999)
1712 / 2500000) / 2) & 0x3F) << 1;
Sascha Haueread73182009-01-28 23:03:11 +00001713 fec_restart(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 /* Queue up command to detect the PHY and initialize the
1716 * remainder of the interface.
1717 */
1718 fep->phy_id_done = 0;
1719 fep->phy_addr = 0;
1720 mii_queue(dev, mk_mii_read(MII_REG_PHYIR1), mii_discover_phy);
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return 0;
1723}
1724
1725/* This function is called to start or restart the FEC during a link
1726 * change. This only happens when switching between half and full
1727 * duplex.
1728 */
1729static void
1730fec_restart(struct net_device *dev, int duplex)
1731{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001732 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer2e285322009-04-15 01:32:16 +00001733 struct bufdesc *bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 int i;
1735
Sascha Hauerf44d6302009-04-15 03:11:30 +00001736 /* Whack a reset. We should wait for this. */
1737 writel(1, fep->hwp + FEC_ECNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 udelay(10);
1739
Sascha Hauerf44d6302009-04-15 03:11:30 +00001740 /* Clear any outstanding interrupt. */
1741 writel(0xffc00000, fep->hwp + FEC_IEVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Sascha Hauerf44d6302009-04-15 03:11:30 +00001743 /* Reset all multicast. */
1744 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1745 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Sascha Hauer4f1ceb42009-04-15 01:32:20 +00001746#ifndef CONFIG_M5272
1747 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1748 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1749#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Sascha Hauerf44d6302009-04-15 03:11:30 +00001751 /* Set maximum receive buffer size. */
1752 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Sascha Hauerf44d6302009-04-15 03:11:30 +00001754 /* Set receive and transmit descriptor base. */
1755 writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
Sascha Hauer2e285322009-04-15 01:32:16 +00001756 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc) * RX_RING_SIZE,
Sascha Hauerf44d6302009-04-15 03:11:30 +00001757 fep->hwp + FEC_X_DES_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
1760 fep->cur_rx = fep->rx_bd_base;
1761
Sascha Hauerf44d6302009-04-15 03:11:30 +00001762 /* Reset SKB transmit buffers. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 fep->skb_cur = fep->skb_dirty = 0;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001764 for (i = 0; i <= TX_RING_MOD_MASK; i++) {
1765 if (fep->tx_skbuff[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 dev_kfree_skb_any(fep->tx_skbuff[i]);
1767 fep->tx_skbuff[i] = NULL;
1768 }
1769 }
1770
Sascha Hauerf44d6302009-04-15 03:11:30 +00001771 /* Initialize the receive buffer descriptors. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 bdp = fep->rx_bd_base;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001773 for (i = 0; i < RX_RING_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Sascha Hauerf44d6302009-04-15 03:11:30 +00001775 /* Initialize the BD for every fragment in the page. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 bdp->cbd_sc = BD_ENET_RX_EMPTY;
1777 bdp++;
1778 }
1779
Sascha Hauer22f6b862009-04-15 01:32:18 +00001780 /* Set the last buffer to wrap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 bdp--;
1782 bdp->cbd_sc |= BD_SC_WRAP;
1783
Sascha Hauer22f6b862009-04-15 01:32:18 +00001784 /* ...and the same for transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 bdp = fep->tx_bd_base;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001786 for (i = 0; i < TX_RING_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Sascha Hauerf44d6302009-04-15 03:11:30 +00001788 /* Initialize the BD for every fragment in the page. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 bdp->cbd_sc = 0;
1790 bdp->cbd_bufaddr = 0;
1791 bdp++;
1792 }
1793
Sascha Hauer22f6b862009-04-15 01:32:18 +00001794 /* Set the last buffer to wrap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 bdp--;
1796 bdp->cbd_sc |= BD_SC_WRAP;
1797
Sascha Hauer22f6b862009-04-15 01:32:18 +00001798 /* Enable MII mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (duplex) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001800 /* MII enable / FD enable */
1801 writel(OPT_FRAME_SIZE | 0x04, fep->hwp + FEC_R_CNTRL);
1802 writel(0x04, fep->hwp + FEC_X_CNTRL);
Philippe De Muyterf909b1e2007-10-23 14:37:54 +10001803 } else {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001804 /* MII enable / No Rcv on Xmit */
1805 writel(OPT_FRAME_SIZE | 0x06, fep->hwp + FEC_R_CNTRL);
1806 writel(0x0, fep->hwp + FEC_X_CNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
1808 fep->full_duplex = duplex;
1809
Sascha Hauer22f6b862009-04-15 01:32:18 +00001810 /* Set MII speed */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001811 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Sascha Hauer22f6b862009-04-15 01:32:18 +00001813 /* And last, enable the transmit and receive processing */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001814 writel(2, fep->hwp + FEC_ECNTRL);
1815 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Matt Waddel6b265292006-06-27 13:10:56 +10001816
Sascha Hauer22f6b862009-04-15 01:32:18 +00001817 /* Enable interrupts we wish to service */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001818 writel(FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII,
1819 fep->hwp + FEC_IMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
1822static void
1823fec_stop(struct net_device *dev)
1824{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001825 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Sascha Hauer22f6b862009-04-15 01:32:18 +00001827 /* We cannot expect a graceful transmit stop without link !!! */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001828 if (fep->link) {
1829 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
Philippe De Muyter677177c2006-06-27 13:05:33 +10001830 udelay(10);
Sascha Hauerf44d6302009-04-15 03:11:30 +00001831 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Philippe De Muyter677177c2006-06-27 13:05:33 +10001832 printk("fec_stop : Graceful transmit stop did not complete !\n");
Sascha Hauerf44d6302009-04-15 03:11:30 +00001833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Sascha Hauerf44d6302009-04-15 03:11:30 +00001835 /* Whack a reset. We should wait for this. */
1836 writel(1, fep->hwp + FEC_ECNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 udelay(10);
1838
Sascha Hauerf44d6302009-04-15 03:11:30 +00001839 /* Clear outstanding MII command interrupts. */
1840 writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Sascha Hauerf44d6302009-04-15 03:11:30 +00001842 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1843 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
Sascha Haueread73182009-01-28 23:03:11 +00001846static int __devinit
1847fec_probe(struct platform_device *pdev)
1848{
1849 struct fec_enet_private *fep;
1850 struct net_device *ndev;
1851 int i, irq, ret = 0;
1852 struct resource *r;
1853
1854 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1855 if (!r)
1856 return -ENXIO;
1857
1858 r = request_mem_region(r->start, resource_size(r), pdev->name);
1859 if (!r)
1860 return -EBUSY;
1861
1862 /* Init network device */
1863 ndev = alloc_etherdev(sizeof(struct fec_enet_private));
1864 if (!ndev)
1865 return -ENOMEM;
1866
1867 SET_NETDEV_DEV(ndev, &pdev->dev);
1868
1869 /* setup board info structure */
1870 fep = netdev_priv(ndev);
1871 memset(fep, 0, sizeof(*fep));
1872
1873 ndev->base_addr = (unsigned long)ioremap(r->start, resource_size(r));
1874
1875 if (!ndev->base_addr) {
1876 ret = -ENOMEM;
1877 goto failed_ioremap;
1878 }
1879
1880 platform_set_drvdata(pdev, ndev);
1881
1882 /* This device has up to three irqs on some platforms */
1883 for (i = 0; i < 3; i++) {
1884 irq = platform_get_irq(pdev, i);
1885 if (i && irq < 0)
1886 break;
1887 ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
1888 if (ret) {
1889 while (i >= 0) {
1890 irq = platform_get_irq(pdev, i);
1891 free_irq(irq, ndev);
1892 i--;
1893 }
1894 goto failed_irq;
1895 }
1896 }
1897
1898 fep->clk = clk_get(&pdev->dev, "fec_clk");
1899 if (IS_ERR(fep->clk)) {
1900 ret = PTR_ERR(fep->clk);
1901 goto failed_clk;
1902 }
1903 clk_enable(fep->clk);
1904
1905 ret = fec_enet_init(ndev, 0);
1906 if (ret)
1907 goto failed_init;
1908
1909 ret = register_netdev(ndev);
1910 if (ret)
1911 goto failed_register;
1912
1913 return 0;
1914
1915failed_register:
1916failed_init:
1917 clk_disable(fep->clk);
1918 clk_put(fep->clk);
1919failed_clk:
1920 for (i = 0; i < 3; i++) {
1921 irq = platform_get_irq(pdev, i);
1922 if (irq > 0)
1923 free_irq(irq, ndev);
1924 }
1925failed_irq:
1926 iounmap((void __iomem *)ndev->base_addr);
1927failed_ioremap:
1928 free_netdev(ndev);
1929
1930 return ret;
1931}
1932
1933static int __devexit
1934fec_drv_remove(struct platform_device *pdev)
1935{
1936 struct net_device *ndev = platform_get_drvdata(pdev);
1937 struct fec_enet_private *fep = netdev_priv(ndev);
1938
1939 platform_set_drvdata(pdev, NULL);
1940
1941 fec_stop(ndev);
1942 clk_disable(fep->clk);
1943 clk_put(fep->clk);
1944 iounmap((void __iomem *)ndev->base_addr);
1945 unregister_netdev(ndev);
1946 free_netdev(ndev);
1947 return 0;
1948}
1949
1950static int
1951fec_suspend(struct platform_device *dev, pm_message_t state)
1952{
1953 struct net_device *ndev = platform_get_drvdata(dev);
1954 struct fec_enet_private *fep;
1955
1956 if (ndev) {
1957 fep = netdev_priv(ndev);
1958 if (netif_running(ndev)) {
1959 netif_device_detach(ndev);
1960 fec_stop(ndev);
1961 }
1962 }
1963 return 0;
1964}
1965
1966static int
1967fec_resume(struct platform_device *dev)
1968{
1969 struct net_device *ndev = platform_get_drvdata(dev);
1970
1971 if (ndev) {
1972 if (netif_running(ndev)) {
1973 fec_enet_init(ndev, 0);
1974 netif_device_attach(ndev);
1975 }
1976 }
1977 return 0;
1978}
1979
1980static struct platform_driver fec_driver = {
1981 .driver = {
1982 .name = "fec",
1983 .owner = THIS_MODULE,
1984 },
1985 .probe = fec_probe,
1986 .remove = __devexit_p(fec_drv_remove),
1987 .suspend = fec_suspend,
1988 .resume = fec_resume,
1989};
1990
1991static int __init
1992fec_enet_module_init(void)
1993{
1994 printk(KERN_INFO "FEC Ethernet Driver\n");
1995
1996 return platform_driver_register(&fec_driver);
1997}
1998
1999static void __exit
2000fec_enet_cleanup(void)
2001{
2002 platform_driver_unregister(&fec_driver);
2003}
2004
2005module_exit(fec_enet_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006module_init(fec_enet_module_init);
2007
2008MODULE_LICENSE("GPL");