blob: 42d9ac9ba3958aa3b4b6a7210bb96cb51aa3dd52 [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>
Bryan Wue6b043d2010-03-31 02:10:44 +000043#include <linux/phy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Greg Ungerer080853a2007-07-30 16:28:46 +100045#include <asm/cacheflush.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000046
47#ifndef CONFIG_ARCH_MXC
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/coldfire.h>
49#include <asm/mcfsim.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000050#endif
Sascha Hauer6f501b12009-01-28 23:03:05 +000051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include "fec.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Sascha Hauer196719e2009-01-28 23:03:10 +000054#ifdef CONFIG_ARCH_MXC
55#include <mach/hardware.h>
56#define FEC_ALIGNMENT 0xf
57#else
58#define FEC_ALIGNMENT 0x3
59#endif
60
Sascha Haueread73182009-01-28 23:03:11 +000061/*
62 * Define the fixed address of the FEC hardware.
63 */
Greg Ungerer87f4abb2008-06-06 15:55:36 +100064#if defined(CONFIG_M5272)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66static unsigned char fec_mac_default[] = {
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68};
69
70/*
71 * Some hardware gets it MAC address out of local flash memory.
72 * if this is non-zero then assume it is the address to get MAC from.
73 */
74#if defined(CONFIG_NETtel)
75#define FEC_FLASHMAC 0xf0006006
76#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
77#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#elif defined(CONFIG_CANCam)
79#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +100080#elif defined (CONFIG_M5272C3)
81#define FEC_FLASHMAC (0xffe04000 + 4)
82#elif defined(CONFIG_MOD5272)
83#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#else
85#define FEC_FLASHMAC 0
86#endif
Greg Ungerer43be6362009-02-26 22:42:51 -080087#endif /* CONFIG_M5272 */
Sascha Haueread73182009-01-28 23:03:11 +000088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* The number of Tx and Rx buffers. These are allocated from the page
90 * pool. The code may assume these are power of two, so it it best
91 * to keep them that size.
92 * We don't need to allocate pages for the transmitter. We just use
93 * the skbuffer directly.
94 */
95#define FEC_ENET_RX_PAGES 8
96#define FEC_ENET_RX_FRSIZE 2048
97#define FEC_ENET_RX_FRPPG (PAGE_SIZE / FEC_ENET_RX_FRSIZE)
98#define RX_RING_SIZE (FEC_ENET_RX_FRPPG * FEC_ENET_RX_PAGES)
99#define FEC_ENET_TX_FRSIZE 2048
100#define FEC_ENET_TX_FRPPG (PAGE_SIZE / FEC_ENET_TX_FRSIZE)
101#define TX_RING_SIZE 16 /* Must be power of two */
102#define TX_RING_MOD_MASK 15 /* for this to work */
103
Greg Ungerer562d2f82005-11-07 14:09:50 +1000104#if (((RX_RING_SIZE + TX_RING_SIZE) * 8) > PAGE_SIZE)
Matt Waddel6b265292006-06-27 13:10:56 +1000105#error "FEC: descriptor ring size constants too large"
Greg Ungerer562d2f82005-11-07 14:09:50 +1000106#endif
107
Sascha Hauer22f6b862009-04-15 01:32:18 +0000108/* Interrupt events/masks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
110#define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
111#define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
112#define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
113#define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
114#define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
115#define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
116#define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
117#define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
118#define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
119
120/* The FEC stores dest/src/type, data, and checksum for receive packets.
121 */
122#define PKT_MAXBUF_SIZE 1518
123#define PKT_MINBUF_SIZE 64
124#define PKT_MAXBLR_SIZE 1520
125
126
127/*
Matt Waddel6b265292006-06-27 13:10:56 +1000128 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * size bits. Other FEC hardware does not, so we need to take that into
130 * account when setting it.
131 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000132#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Sascha Hauer196719e2009-01-28 23:03:10 +0000133 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARCH_MXC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
135#else
136#define OPT_FRAME_SIZE 0
137#endif
138
139/* The FEC buffer descriptors track the ring buffers. The rx_bd_base and
140 * tx_bd_base always point to the base of the buffer descriptors. The
141 * cur_rx and cur_tx point to the currently available buffer.
142 * The dirty_tx tracks the current buffer that is being sent by the
143 * controller. The cur_tx and dirty_tx are equal under both completely
144 * empty and completely full conditions. The empty/ready indicator in
145 * the buffer descriptor determines the actual condition.
146 */
147struct fec_enet_private {
148 /* Hardware registers of the FEC device */
Sascha Hauerf44d6302009-04-15 03:11:30 +0000149 void __iomem *hwp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Greg Ungerercb84d6e2007-07-30 16:29:09 +1000151 struct net_device *netdev;
152
Sascha Haueread73182009-01-28 23:03:11 +0000153 struct clk *clk;
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 /* The saved address of a sent-in-place packet/buffer, for skfree(). */
156 unsigned char *tx_bounce[TX_RING_SIZE];
157 struct sk_buff* tx_skbuff[TX_RING_SIZE];
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000158 struct sk_buff* rx_skbuff[RX_RING_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 ushort skb_cur;
160 ushort skb_dirty;
161
Sascha Hauer22f6b862009-04-15 01:32:18 +0000162 /* CPM dual port RAM relative addresses */
Sascha Hauer4661e752009-01-28 23:03:07 +0000163 dma_addr_t bd_dma;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000164 /* Address of Rx and Tx buffers */
Sascha Hauer2e285322009-04-15 01:32:16 +0000165 struct bufdesc *rx_bd_base;
166 struct bufdesc *tx_bd_base;
167 /* The next free ring entry */
168 struct bufdesc *cur_rx, *cur_tx;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000169 /* The ring entries to be free()ed */
Sascha Hauer2e285322009-04-15 01:32:16 +0000170 struct bufdesc *dirty_tx;
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 uint tx_full;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000173 /* hold while accessing the HW like ringbuffer for tx/rx but not MAC */
174 spinlock_t hw_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Bryan Wue6b043d2010-03-31 02:10:44 +0000176 struct platform_device *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 int opened;
Bryan Wue6b043d2010-03-31 02:10:44 +0000179
180 /* Phylib and MDIO interface */
181 struct mii_bus *mii_bus;
182 struct phy_device *phy_dev;
183 int mii_timeout;
184 uint phy_speed;
185 int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 int link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 int full_duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
David Howells7d12e782006-10-05 14:55:46 +0100190static irqreturn_t fec_enet_interrupt(int irq, void * dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191static void fec_enet_tx(struct net_device *dev);
192static void fec_enet_rx(struct net_device *dev);
193static int fec_enet_close(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194static void fec_restart(struct net_device *dev, int duplex);
195static void fec_stop(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Bryan Wue6b043d2010-03-31 02:10:44 +0000197/* FEC MII MMFR bits definition */
198#define FEC_MMFR_ST (1 << 30)
199#define FEC_MMFR_OP_READ (2 << 28)
200#define FEC_MMFR_OP_WRITE (1 << 28)
201#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
202#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
203#define FEC_MMFR_TA (2 << 16)
204#define FEC_MMFR_DATA(v) (v & 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Bryan Wue6b043d2010-03-31 02:10:44 +0000206#define FEC_MII_TIMEOUT 10000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Sascha Hauer22f6b862009-04-15 01:32:18 +0000208/* Transmitter timeout */
209#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static int
212fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
213{
Sascha Hauerf44d6302009-04-15 03:11:30 +0000214 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000215 struct bufdesc *bdp;
Greg Ungerer9555b312009-08-06 17:58:18 +0000216 void *bufaddr;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000217 unsigned short status;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000218 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (!fep->link) {
221 /* Link is down or autonegotiation is in progress. */
Patrick McHardy5b548142009-06-12 06:22:29 +0000222 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000225 spin_lock_irqsave(&fep->hw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 /* Fill in a Tx ring entry */
227 bdp = fep->cur_tx;
228
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000229 status = bdp->cbd_sc;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000230
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000231 if (status & BD_ENET_TX_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /* Ooops. All transmit buffers are full. Bail out.
233 * This should not happen, since dev->tbusy should be set.
234 */
235 printk("%s: tx queue full!.\n", dev->name);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000236 spin_unlock_irqrestore(&fep->hw_lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +0000237 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Sascha Hauer22f6b862009-04-15 01:32:18 +0000240 /* Clear all of the status flags */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000241 status &= ~BD_ENET_TX_STATS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Sascha Hauer22f6b862009-04-15 01:32:18 +0000243 /* Set buffer length and buffer pointer */
Greg Ungerer9555b312009-08-06 17:58:18 +0000244 bufaddr = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 bdp->cbd_datlen = skb->len;
246
247 /*
Sascha Hauer22f6b862009-04-15 01:32:18 +0000248 * On some FEC implementations data must be aligned on
249 * 4-byte boundaries. Use bounce buffers to copy data
250 * and get it aligned. Ugh.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 */
Greg Ungerer9555b312009-08-06 17:58:18 +0000252 if (((unsigned long) bufaddr) & FEC_ALIGNMENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 unsigned int index;
254 index = bdp - fep->tx_bd_base;
Sascha Hauer6989f512009-01-28 23:03:06 +0000255 memcpy(fep->tx_bounce[index], (void *)skb->data, skb->len);
Greg Ungerer9555b312009-08-06 17:58:18 +0000256 bufaddr = fep->tx_bounce[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258
Sascha Hauer22f6b862009-04-15 01:32:18 +0000259 /* Save skb pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 fep->tx_skbuff[fep->skb_cur] = skb;
261
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700262 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 fep->skb_cur = (fep->skb_cur+1) & TX_RING_MOD_MASK;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 /* Push the data cache so the CPM does not get stale memory
266 * data.
267 */
Greg Ungerer9555b312009-08-06 17:58:18 +0000268 bdp->cbd_bufaddr = dma_map_single(&dev->dev, bufaddr,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000269 FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000271 /* Send it on its way. Tell FEC it's ready, interrupt when done,
272 * it's the last BD of the frame, and to put the CRC on the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000274 status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 | BD_ENET_TX_LAST | BD_ENET_TX_TC);
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000276 bdp->cbd_sc = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 /* Trigger transmission start */
Sascha Hauerf44d6302009-04-15 03:11:30 +0000279 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Sascha Hauer22f6b862009-04-15 01:32:18 +0000281 /* If this was the last BD in the ring, start at the beginning again. */
282 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 bdp = fep->tx_bd_base;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000284 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 bdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 if (bdp == fep->dirty_tx) {
288 fep->tx_full = 1;
289 netif_stop_queue(dev);
290 }
291
Sascha Hauer2e285322009-04-15 01:32:16 +0000292 fep->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000294 spin_unlock_irqrestore(&fep->hw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Patrick McHardy6ed10652009-06-23 06:03:08 +0000296 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299static void
300fec_timeout(struct net_device *dev)
301{
302 struct fec_enet_private *fep = netdev_priv(dev);
303
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700304 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000306 fec_restart(dev, fep->full_duplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 netif_wake_queue(dev);
308}
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100311fec_enet_interrupt(int irq, void * dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 struct net_device *dev = dev_id;
Sascha Hauerf44d6302009-04-15 03:11:30 +0000314 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 uint int_events;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000316 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000318 do {
Sascha Hauerf44d6302009-04-15 03:11:30 +0000319 int_events = readl(fep->hwp + FEC_IEVENT);
320 writel(int_events, fep->hwp + FEC_IEVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (int_events & FEC_ENET_RXF) {
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000323 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 fec_enet_rx(dev);
325 }
326
327 /* Transmit OK, or non-fatal error. Update the buffer
Sascha Hauerf44d6302009-04-15 03:11:30 +0000328 * descriptors. FEC handles all errors, we just discover
329 * them as part of the transmit process.
330 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (int_events & FEC_ENET_TXF) {
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000332 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 fec_enet_tx(dev);
334 }
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000335 } while (int_events);
336
337 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340
341static void
342fec_enet_tx(struct net_device *dev)
343{
344 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +0000345 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000346 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct sk_buff *skb;
348
349 fep = netdev_priv(dev);
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000350 spin_lock(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 bdp = fep->dirty_tx;
352
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000353 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000354 if (bdp == fep->cur_tx && fep->tx_full == 0)
355 break;
356
357 dma_unmap_single(&dev->dev, bdp->cbd_bufaddr, FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
358 bdp->cbd_bufaddr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 skb = fep->tx_skbuff[fep->skb_dirty];
361 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000362 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 BD_ENET_TX_RL | BD_ENET_TX_UN |
364 BD_ENET_TX_CSL)) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700365 dev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000366 if (status & BD_ENET_TX_HB) /* No heartbeat */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700367 dev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000368 if (status & BD_ENET_TX_LC) /* Late collision */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700369 dev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000370 if (status & BD_ENET_TX_RL) /* Retrans limit */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700371 dev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000372 if (status & BD_ENET_TX_UN) /* Underrun */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700373 dev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000374 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700375 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 } else {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700377 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000380 if (status & BD_ENET_TX_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 printk("HEY! Enet xmit interrupt and TX_READY.\n");
Sascha Hauer22f6b862009-04-15 01:32:18 +0000382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* Deferred means some collisions occurred during transmit,
384 * but we eventually sent the packet OK.
385 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000386 if (status & BD_ENET_TX_DEF)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700387 dev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400388
Sascha Hauer22f6b862009-04-15 01:32:18 +0000389 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 dev_kfree_skb_any(skb);
391 fep->tx_skbuff[fep->skb_dirty] = NULL;
392 fep->skb_dirty = (fep->skb_dirty + 1) & TX_RING_MOD_MASK;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400393
Sascha Hauer22f6b862009-04-15 01:32:18 +0000394 /* Update pointer to next buffer descriptor to be transmitted */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000395 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 bdp = fep->tx_bd_base;
397 else
398 bdp++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400399
Sascha Hauer22f6b862009-04-15 01:32:18 +0000400 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 */
402 if (fep->tx_full) {
403 fep->tx_full = 0;
404 if (netif_queue_stopped(dev))
405 netif_wake_queue(dev);
406 }
407 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000408 fep->dirty_tx = bdp;
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000409 spin_unlock(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
412
413/* During a receive, the cur_rx points to the current incoming buffer.
414 * When we update through the ring, if the next incoming buffer has
415 * not been given to the system, we just set the empty indicator,
416 * effectively tossing the packet.
417 */
418static void
419fec_enet_rx(struct net_device *dev)
420{
Sascha Hauerf44d6302009-04-15 03:11:30 +0000421 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000422 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000423 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 struct sk_buff *skb;
425 ushort pkt_len;
426 __u8 *data;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400427
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000428#ifdef CONFIG_M532x
429 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400430#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000432 spin_lock(&fep->hw_lock);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /* First, grab all of the stats for the incoming packet.
435 * These get messed up if we get called due to a busy condition.
436 */
437 bdp = fep->cur_rx;
438
Sascha Hauer22f6b862009-04-15 01:32:18 +0000439 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Sascha Hauer22f6b862009-04-15 01:32:18 +0000441 /* Since we have allocated space to hold a complete frame,
442 * the last indicator should be set.
443 */
444 if ((status & BD_ENET_RX_LAST) == 0)
445 printk("FEC ENET: rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Sascha Hauer22f6b862009-04-15 01:32:18 +0000447 if (!fep->opened)
448 goto rx_processing_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Sascha Hauer22f6b862009-04-15 01:32:18 +0000450 /* Check for errors. */
451 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Sascha Hauer22f6b862009-04-15 01:32:18 +0000453 dev->stats.rx_errors++;
454 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
455 /* Frame too long or too short. */
456 dev->stats.rx_length_errors++;
457 }
458 if (status & BD_ENET_RX_NO) /* Frame alignment */
459 dev->stats.rx_frame_errors++;
460 if (status & BD_ENET_RX_CR) /* CRC Error */
461 dev->stats.rx_crc_errors++;
462 if (status & BD_ENET_RX_OV) /* FIFO overrun */
463 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
Sascha Hauer22f6b862009-04-15 01:32:18 +0000465
466 /* Report late collisions as a frame error.
467 * On this error, the BD is closed, but we don't know what we
468 * have in the buffer. So, just drop this frame on the floor.
469 */
470 if (status & BD_ENET_RX_CL) {
471 dev->stats.rx_errors++;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700472 dev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000473 goto rx_processing_done;
474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Sascha Hauer22f6b862009-04-15 01:32:18 +0000476 /* Process the incoming frame. */
477 dev->stats.rx_packets++;
478 pkt_len = bdp->cbd_datlen;
479 dev->stats.rx_bytes += pkt_len;
480 data = (__u8*)__va(bdp->cbd_bufaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000482 dma_unmap_single(NULL, bdp->cbd_bufaddr, bdp->cbd_datlen,
483 DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +0000484
Sascha Hauer22f6b862009-04-15 01:32:18 +0000485 /* This does 16 byte alignment, exactly what we need.
486 * The packet length includes FCS, but we don't want to
487 * include that when passing upstream as it messes up
488 * bridging applications.
489 */
Sascha Hauer85498892009-04-15 01:32:21 +0000490 skb = dev_alloc_skb(pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Sascha Hauer85498892009-04-15 01:32:21 +0000492 if (unlikely(!skb)) {
Sascha Hauer22f6b862009-04-15 01:32:18 +0000493 printk("%s: Memory squeeze, dropping packet.\n",
494 dev->name);
495 dev->stats.rx_dropped++;
496 } else {
Sascha Hauer85498892009-04-15 01:32:21 +0000497 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000498 skb_put(skb, pkt_len - 4); /* Make room */
499 skb_copy_to_linear_data(skb, data, pkt_len - 4);
500 skb->protocol = eth_type_trans(skb, dev);
501 netif_rx(skb);
502 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000503
504 bdp->cbd_bufaddr = dma_map_single(NULL, data, bdp->cbd_datlen,
505 DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000506rx_processing_done:
507 /* Clear the status flags for this buffer */
508 status &= ~BD_ENET_RX_STATS;
509
510 /* Mark the buffer empty */
511 status |= BD_ENET_RX_EMPTY;
512 bdp->cbd_sc = status;
513
514 /* Update BD pointer to next entry */
515 if (status & BD_ENET_RX_WRAP)
516 bdp = fep->rx_bd_base;
517 else
518 bdp++;
519 /* Doing this here will keep the FEC running while we process
520 * incoming frames. On a heavily loaded network, we should be
521 * able to keep up at the expense of system resources.
522 */
523 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000525 fep->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000527 spin_unlock(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530/* ------------------------------------------------------------------------- */
Greg Ungerer43be6362009-02-26 22:42:51 -0800531#ifdef CONFIG_M5272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532static void __inline__ fec_get_mac(struct net_device *dev)
533{
534 struct fec_enet_private *fep = netdev_priv(dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000535 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000537 if (FEC_FLASHMAC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /*
539 * Get MAC address from FLASH.
540 * If it is all 1's or 0's, use the default.
541 */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000542 iap = (unsigned char *)FEC_FLASHMAC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if ((iap[0] == 0) && (iap[1] == 0) && (iap[2] == 0) &&
544 (iap[3] == 0) && (iap[4] == 0) && (iap[5] == 0))
545 iap = fec_mac_default;
546 if ((iap[0] == 0xff) && (iap[1] == 0xff) && (iap[2] == 0xff) &&
547 (iap[3] == 0xff) && (iap[4] == 0xff) && (iap[5] == 0xff))
548 iap = fec_mac_default;
549 } else {
Sascha Hauerf44d6302009-04-15 03:11:30 +0000550 *((unsigned long *) &tmpaddr[0]) = readl(fep->hwp + FEC_ADDR_LOW);
551 *((unsigned short *) &tmpaddr[4]) = (readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 iap = &tmpaddr[0];
553 }
554
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000555 memcpy(dev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 /* Adjust MAC if using default MAC address */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000558 if (iap == fec_mac_default)
559 dev->dev_addr[ETH_ALEN-1] = fec_mac_default[ETH_ALEN-1] + fep->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561#endif
562
563/* ------------------------------------------------------------------------- */
564
Bryan Wue6b043d2010-03-31 02:10:44 +0000565/*
566 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 */
Bryan Wue6b043d2010-03-31 02:10:44 +0000568static void fec_enet_adjust_link(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Bryan Wue6b043d2010-03-31 02:10:44 +0000570 struct fec_enet_private *fep = netdev_priv(dev);
571 struct phy_device *phy_dev = fep->phy_dev;
572 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Bryan Wue6b043d2010-03-31 02:10:44 +0000574 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Bryan Wue6b043d2010-03-31 02:10:44 +0000576 spin_lock_irqsave(&fep->hw_lock, flags);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400577
Bryan Wue6b043d2010-03-31 02:10:44 +0000578 /* Prevent a state halted on mii error */
579 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
580 phy_dev->state = PHY_RESUMING;
581 goto spin_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
Bryan Wue6b043d2010-03-31 02:10:44 +0000583
584 /* Duplex link change */
585 if (phy_dev->link) {
586 if (fep->full_duplex != phy_dev->duplex) {
587 fec_restart(dev, phy_dev->duplex);
588 status_change = 1;
589 }
590 }
591
592 /* Link on or off change */
593 if (phy_dev->link != fep->link) {
594 fep->link = phy_dev->link;
595 if (phy_dev->link)
596 fec_restart(dev, phy_dev->duplex);
597 else
598 fec_stop(dev);
599 status_change = 1;
600 }
601
602spin_unlock:
603 spin_unlock_irqrestore(&fep->hw_lock, flags);
604
605 if (status_change)
606 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Bryan Wue6b043d2010-03-31 02:10:44 +0000609/*
610 * NOTE: a MII transaction is during around 25 us, so polling it...
611 */
612static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Bryan Wue6b043d2010-03-31 02:10:44 +0000614 struct fec_enet_private *fep = bus->priv;
615 int timeout = FEC_MII_TIMEOUT;
616
617 fep->mii_timeout = 0;
618
619 /* clear MII end of transfer bit*/
620 writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
621
622 /* start a read op */
623 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
624 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
625 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
626
627 /* wait for end of transfer */
628 while (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_MII)) {
629 cpu_relax();
630 if (timeout-- < 0) {
631 fep->mii_timeout = 1;
632 printk(KERN_ERR "FEC: MDIO read timeout\n");
633 return -ETIMEDOUT;
634 }
635 }
636
637 /* return value */
638 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
639}
640
641static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
642 u16 value)
643{
644 struct fec_enet_private *fep = bus->priv;
645 int timeout = FEC_MII_TIMEOUT;
646
647 fep->mii_timeout = 0;
648
649 /* clear MII end of transfer bit*/
650 writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
651
652 /* start a read op */
653 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
654 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
655 FEC_MMFR_TA | FEC_MMFR_DATA(value),
656 fep->hwp + FEC_MII_DATA);
657
658 /* wait for end of transfer */
659 while (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_MII)) {
660 cpu_relax();
661 if (timeout-- < 0) {
662 fep->mii_timeout = 1;
663 printk(KERN_ERR "FEC: MDIO write timeout\n");
664 return -ETIMEDOUT;
665 }
666 }
667
668 return 0;
669}
670
671static int fec_enet_mdio_reset(struct mii_bus *bus)
672{
673 return 0;
674}
675
676static int fec_enet_mii_probe(struct net_device *dev)
677{
678 struct fec_enet_private *fep = netdev_priv(dev);
679 struct phy_device *phy_dev = NULL;
680 int phy_addr;
681
682 /* find the first phy */
683 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
684 if (fep->mii_bus->phy_map[phy_addr]) {
685 phy_dev = fep->mii_bus->phy_map[phy_addr];
686 break;
687 }
688 }
689
690 if (!phy_dev) {
691 printk(KERN_ERR "%s: no PHY found\n", dev->name);
692 return -ENODEV;
693 }
694
695 /* attach the mac to the phy */
696 phy_dev = phy_connect(dev, dev_name(&phy_dev->dev),
697 &fec_enet_adjust_link, 0,
698 PHY_INTERFACE_MODE_MII);
699 if (IS_ERR(phy_dev)) {
700 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
701 return PTR_ERR(phy_dev);
702 }
703
704 /* mask with MAC supported features */
705 phy_dev->supported &= PHY_BASIC_FEATURES;
706 phy_dev->advertising = phy_dev->supported;
707
708 fep->phy_dev = phy_dev;
709 fep->link = 0;
710 fep->full_duplex = 0;
711
712 return 0;
713}
714
715static int fec_enet_mii_init(struct platform_device *pdev)
716{
717 struct net_device *dev = platform_get_drvdata(pdev);
718 struct fec_enet_private *fep = netdev_priv(dev);
719 int err = -ENXIO, i;
720
721 fep->mii_timeout = 0;
722
723 /*
724 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
725 */
726 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk), 5000000) << 1;
727 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
728
729 fep->mii_bus = mdiobus_alloc();
730 if (fep->mii_bus == NULL) {
731 err = -ENOMEM;
732 goto err_out;
733 }
734
735 fep->mii_bus->name = "fec_enet_mii_bus";
736 fep->mii_bus->read = fec_enet_mdio_read;
737 fep->mii_bus->write = fec_enet_mdio_write;
738 fep->mii_bus->reset = fec_enet_mdio_reset;
739 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%x", pdev->id);
740 fep->mii_bus->priv = fep;
741 fep->mii_bus->parent = &pdev->dev;
742
743 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
744 if (!fep->mii_bus->irq) {
745 err = -ENOMEM;
746 goto err_out_free_mdiobus;
747 }
748
749 for (i = 0; i < PHY_MAX_ADDR; i++)
750 fep->mii_bus->irq[i] = PHY_POLL;
751
752 platform_set_drvdata(dev, fep->mii_bus);
753
754 if (mdiobus_register(fep->mii_bus))
755 goto err_out_free_mdio_irq;
756
757 if (fec_enet_mii_probe(dev) != 0)
758 goto err_out_unregister_bus;
759
760 return 0;
761
762err_out_unregister_bus:
763 mdiobus_unregister(fep->mii_bus);
764err_out_free_mdio_irq:
765 kfree(fep->mii_bus->irq);
766err_out_free_mdiobus:
767 mdiobus_free(fep->mii_bus);
768err_out:
769 return err;
770}
771
772static void fec_enet_mii_remove(struct fec_enet_private *fep)
773{
774 if (fep->phy_dev)
775 phy_disconnect(fep->phy_dev);
776 mdiobus_unregister(fep->mii_bus);
777 kfree(fep->mii_bus->irq);
778 mdiobus_free(fep->mii_bus);
779}
780
781static int fec_enet_get_settings(struct net_device *dev,
782 struct ethtool_cmd *cmd)
783{
784 struct fec_enet_private *fep = netdev_priv(dev);
785 struct phy_device *phydev = fep->phy_dev;
786
787 if (!phydev)
788 return -ENODEV;
789
790 return phy_ethtool_gset(phydev, cmd);
791}
792
793static int fec_enet_set_settings(struct net_device *dev,
794 struct ethtool_cmd *cmd)
795{
796 struct fec_enet_private *fep = netdev_priv(dev);
797 struct phy_device *phydev = fep->phy_dev;
798
799 if (!phydev)
800 return -ENODEV;
801
802 return phy_ethtool_sset(phydev, cmd);
803}
804
805static void fec_enet_get_drvinfo(struct net_device *dev,
806 struct ethtool_drvinfo *info)
807{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 struct fec_enet_private *fep = netdev_priv(dev);
809
Bryan Wue6b043d2010-03-31 02:10:44 +0000810 strcpy(info->driver, fep->pdev->dev.driver->name);
811 strcpy(info->version, "Revision: 1.0");
812 strcpy(info->bus_info, dev_name(&dev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
Bryan Wue6b043d2010-03-31 02:10:44 +0000814
815static struct ethtool_ops fec_enet_ethtool_ops = {
816 .get_settings = fec_enet_get_settings,
817 .set_settings = fec_enet_set_settings,
818 .get_drvinfo = fec_enet_get_drvinfo,
819 .get_link = ethtool_op_get_link,
820};
821
822static int fec_enet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
823{
824 struct fec_enet_private *fep = netdev_priv(dev);
825 struct phy_device *phydev = fep->phy_dev;
826
827 if (!netif_running(dev))
828 return -EINVAL;
829
830 if (!phydev)
831 return -ENODEV;
832
833 return phy_mii_ioctl(phydev, if_mii(rq), cmd);
834}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000836static void fec_enet_free_buffers(struct net_device *dev)
837{
838 struct fec_enet_private *fep = netdev_priv(dev);
839 int i;
840 struct sk_buff *skb;
841 struct bufdesc *bdp;
842
843 bdp = fep->rx_bd_base;
844 for (i = 0; i < RX_RING_SIZE; i++) {
845 skb = fep->rx_skbuff[i];
846
847 if (bdp->cbd_bufaddr)
848 dma_unmap_single(&dev->dev, bdp->cbd_bufaddr,
849 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
850 if (skb)
851 dev_kfree_skb(skb);
852 bdp++;
853 }
854
855 bdp = fep->tx_bd_base;
856 for (i = 0; i < TX_RING_SIZE; i++)
857 kfree(fep->tx_bounce[i]);
858}
859
860static int fec_enet_alloc_buffers(struct net_device *dev)
861{
862 struct fec_enet_private *fep = netdev_priv(dev);
863 int i;
864 struct sk_buff *skb;
865 struct bufdesc *bdp;
866
867 bdp = fep->rx_bd_base;
868 for (i = 0; i < RX_RING_SIZE; i++) {
869 skb = dev_alloc_skb(FEC_ENET_RX_FRSIZE);
870 if (!skb) {
871 fec_enet_free_buffers(dev);
872 return -ENOMEM;
873 }
874 fep->rx_skbuff[i] = skb;
875
876 bdp->cbd_bufaddr = dma_map_single(&dev->dev, skb->data,
877 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
878 bdp->cbd_sc = BD_ENET_RX_EMPTY;
879 bdp++;
880 }
881
882 /* Set the last buffer to wrap. */
883 bdp--;
884 bdp->cbd_sc |= BD_SC_WRAP;
885
886 bdp = fep->tx_bd_base;
887 for (i = 0; i < TX_RING_SIZE; i++) {
888 fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
889
890 bdp->cbd_sc = 0;
891 bdp->cbd_bufaddr = 0;
892 bdp++;
893 }
894
895 /* Set the last buffer to wrap. */
896 bdp--;
897 bdp->cbd_sc |= BD_SC_WRAP;
898
899 return 0;
900}
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902static int
903fec_enet_open(struct net_device *dev)
904{
905 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000906 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 /* I should reset the ring buffers here, but I don't yet know
909 * a simple way to do that.
910 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000912 ret = fec_enet_alloc_buffers(dev);
913 if (ret)
914 return ret;
915
Bryan Wue6b043d2010-03-31 02:10:44 +0000916 /* schedule a link state check */
917 phy_start(fep->phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 netif_start_queue(dev);
919 fep->opened = 1;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000920 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
923static int
924fec_enet_close(struct net_device *dev)
925{
926 struct fec_enet_private *fep = netdev_priv(dev);
927
Sascha Hauer22f6b862009-04-15 01:32:18 +0000928 /* Don't know what to do yet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 fep->opened = 0;
Bryan Wue6b043d2010-03-31 02:10:44 +0000930 phy_stop(fep->phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 netif_stop_queue(dev);
932 fec_stop(dev);
933
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000934 fec_enet_free_buffers(dev);
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return 0;
937}
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939/* Set or clear the multicast filter for this adaptor.
940 * Skeleton taken from sunlance driver.
941 * The CPM Ethernet implementation allows Multicast as well as individual
942 * MAC address filtering. Some of the drivers check to make sure it is
943 * a group multicast address, and discard those that are not. I guess I
944 * will do the same for now, but just remove the test if you want
945 * individual filtering as well (do the upper net layers want or support
946 * this kind of feature?).
947 */
948
949#define HASH_BITS 6 /* #bits in hash */
950#define CRC32_POLY 0xEDB88320
951
952static void set_multicast_list(struct net_device *dev)
953{
Sascha Hauerf44d6302009-04-15 03:11:30 +0000954 struct fec_enet_private *fep = netdev_priv(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000955 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +0000956 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 unsigned char hash;
958
Sascha Hauer22f6b862009-04-15 01:32:18 +0000959 if (dev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +0000960 tmp = readl(fep->hwp + FEC_R_CNTRL);
961 tmp |= 0x8;
962 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +0000963 return;
964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Sascha Hauer4e831832009-04-15 01:32:19 +0000966 tmp = readl(fep->hwp + FEC_R_CNTRL);
967 tmp &= ~0x8;
968 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400969
Sascha Hauer4e831832009-04-15 01:32:19 +0000970 if (dev->flags & IFF_ALLMULTI) {
971 /* Catch all multicast addresses, so set the
972 * filter to all 1's
973 */
974 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
975 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Sascha Hauer4e831832009-04-15 01:32:19 +0000977 return;
978 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400979
Sascha Hauer4e831832009-04-15 01:32:19 +0000980 /* Clear filter and add the addresses in hash register
981 */
982 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
983 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Jiri Pirko22bedad32010-04-01 21:22:57 +0000985 netdev_for_each_mc_addr(ha, dev) {
Sascha Hauer4e831832009-04-15 01:32:19 +0000986 /* Only support group multicast for now */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000987 if (!(ha->addr[0] & 1))
Sascha Hauer4e831832009-04-15 01:32:19 +0000988 continue;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400989
Sascha Hauer4e831832009-04-15 01:32:19 +0000990 /* calculate crc32 value of mac address */
991 crc = 0xffffffff;
992
Jiri Pirko22bedad32010-04-01 21:22:57 +0000993 for (i = 0; i < dev->addr_len; i++) {
994 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +0000995 for (bit = 0; bit < 8; bit++, data >>= 1) {
996 crc = (crc >> 1) ^
997 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999 }
Sascha Hauer4e831832009-04-15 01:32:19 +00001000
1001 /* only upper 6 bits (HASH_BITS) are used
1002 * which point to specific bit in he hash registers
1003 */
1004 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
1005
1006 if (hash > 31) {
1007 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1008 tmp |= 1 << (hash - 32);
1009 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1010 } else {
1011 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1012 tmp |= 1 << hash;
1013 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016}
1017
Sascha Hauer22f6b862009-04-15 01:32:18 +00001018/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00001019static int
1020fec_set_mac_address(struct net_device *dev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001022 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer009fda82009-04-15 01:32:23 +00001023 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Sascha Hauer009fda82009-04-15 01:32:23 +00001025 if (!is_valid_ether_addr(addr->sa_data))
1026 return -EADDRNOTAVAIL;
1027
1028 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1029
Sascha Hauerf44d6302009-04-15 03:11:30 +00001030 writel(dev->dev_addr[3] | (dev->dev_addr[2] << 8) |
1031 (dev->dev_addr[1] << 16) | (dev->dev_addr[0] << 24),
1032 fep->hwp + FEC_ADDR_LOW);
1033 writel((dev->dev_addr[5] << 16) | (dev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07001034 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00001035 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
Sascha Hauer009fda82009-04-15 01:32:23 +00001038static const struct net_device_ops fec_netdev_ops = {
1039 .ndo_open = fec_enet_open,
1040 .ndo_stop = fec_enet_close,
1041 .ndo_start_xmit = fec_enet_start_xmit,
1042 .ndo_set_multicast_list = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00001043 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00001044 .ndo_validate_addr = eth_validate_addr,
1045 .ndo_tx_timeout = fec_timeout,
1046 .ndo_set_mac_address = fec_set_mac_address,
Bryan Wue6b043d2010-03-31 02:10:44 +00001047 .ndo_do_ioctl = fec_enet_ioctl,
Sascha Hauer009fda82009-04-15 01:32:23 +00001048};
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /*
1051 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00001052 *
1053 * index is only used in legacy code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 */
Steven King78abcb12009-10-20 18:51:37 -07001055static int fec_enet_init(struct net_device *dev, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
1057 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001058 struct bufdesc *cbd_base;
Rob Herring633e7532010-02-05 08:56:20 +00001059 struct bufdesc *bdp;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001060 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001062 /* Allocate memory for buffer descriptors. */
1063 cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
1064 GFP_KERNEL);
1065 if (!cbd_base) {
Greg Ungerer562d2f82005-11-07 14:09:50 +10001066 printk("FEC: allocate descriptor memory failed?\n");
1067 return -ENOMEM;
1068 }
1069
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10001070 spin_lock_init(&fep->hw_lock);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 fep->index = index;
Sascha Hauerf44d6302009-04-15 03:11:30 +00001073 fep->hwp = (void __iomem *)dev->base_addr;
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001074 fep->netdev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Sascha Haueread73182009-01-28 23:03:11 +00001076 /* Set the Ethernet address */
Greg Ungerer43be6362009-02-26 22:42:51 -08001077#ifdef CONFIG_M5272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 fec_get_mac(dev);
Sascha Haueread73182009-01-28 23:03:11 +00001079#else
1080 {
1081 unsigned long l;
Sascha Hauerf44d6302009-04-15 03:11:30 +00001082 l = readl(fep->hwp + FEC_ADDR_LOW);
Sascha Haueread73182009-01-28 23:03:11 +00001083 dev->dev_addr[0] = (unsigned char)((l & 0xFF000000) >> 24);
1084 dev->dev_addr[1] = (unsigned char)((l & 0x00FF0000) >> 16);
1085 dev->dev_addr[2] = (unsigned char)((l & 0x0000FF00) >> 8);
1086 dev->dev_addr[3] = (unsigned char)((l & 0x000000FF) >> 0);
Sascha Hauerf44d6302009-04-15 03:11:30 +00001087 l = readl(fep->hwp + FEC_ADDR_HIGH);
Sascha Haueread73182009-01-28 23:03:11 +00001088 dev->dev_addr[4] = (unsigned char)((l & 0xFF000000) >> 24);
1089 dev->dev_addr[5] = (unsigned char)((l & 0x00FF0000) >> 16);
1090 }
1091#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001093 /* Set receive and transmit descriptor base. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 fep->rx_bd_base = cbd_base;
1095 fep->tx_bd_base = cbd_base + RX_RING_SIZE;
1096
Sascha Hauer22f6b862009-04-15 01:32:18 +00001097 /* The FEC Ethernet specific entries in the device structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 dev->watchdog_timeo = TX_TIMEOUT;
Sascha Hauer009fda82009-04-15 01:32:23 +00001099 dev->netdev_ops = &fec_netdev_ops;
Bryan Wue6b043d2010-03-31 02:10:44 +00001100 dev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00001101
1102 /* Initialize the receive buffer descriptors. */
1103 bdp = fep->rx_bd_base;
1104 for (i = 0; i < RX_RING_SIZE; i++) {
1105
1106 /* Initialize the BD for every fragment in the page. */
1107 bdp->cbd_sc = 0;
1108 bdp++;
1109 }
1110
1111 /* Set the last buffer to wrap */
1112 bdp--;
1113 bdp->cbd_sc |= BD_SC_WRAP;
1114
1115 /* ...and the same for transmit */
1116 bdp = fep->tx_bd_base;
1117 for (i = 0; i < TX_RING_SIZE; i++) {
1118
1119 /* Initialize the BD for every fragment in the page. */
1120 bdp->cbd_sc = 0;
1121 bdp->cbd_bufaddr = 0;
1122 bdp++;
1123 }
1124
1125 /* Set the last buffer to wrap */
1126 bdp--;
1127 bdp->cbd_sc |= BD_SC_WRAP;
1128
Sascha Haueread73182009-01-28 23:03:11 +00001129 fec_restart(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return 0;
1132}
1133
1134/* This function is called to start or restart the FEC during a link
1135 * change. This only happens when switching between half and full
1136 * duplex.
1137 */
1138static void
1139fec_restart(struct net_device *dev, int duplex)
1140{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001141 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 int i;
1143
Sascha Hauerf44d6302009-04-15 03:11:30 +00001144 /* Whack a reset. We should wait for this. */
1145 writel(1, fep->hwp + FEC_ECNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 udelay(10);
1147
Sascha Hauerf44d6302009-04-15 03:11:30 +00001148 /* Clear any outstanding interrupt. */
1149 writel(0xffc00000, fep->hwp + FEC_IEVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Sascha Hauerf44d6302009-04-15 03:11:30 +00001151 /* Reset all multicast. */
1152 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1153 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Sascha Hauer4f1ceb42009-04-15 01:32:20 +00001154#ifndef CONFIG_M5272
1155 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1156 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1157#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Sascha Hauerf44d6302009-04-15 03:11:30 +00001159 /* Set maximum receive buffer size. */
1160 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Sascha Hauerf44d6302009-04-15 03:11:30 +00001162 /* Set receive and transmit descriptor base. */
1163 writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
Sascha Hauer2e285322009-04-15 01:32:16 +00001164 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc) * RX_RING_SIZE,
Sascha Hauerf44d6302009-04-15 03:11:30 +00001165 fep->hwp + FEC_X_DES_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
1168 fep->cur_rx = fep->rx_bd_base;
1169
Sascha Hauerf44d6302009-04-15 03:11:30 +00001170 /* Reset SKB transmit buffers. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 fep->skb_cur = fep->skb_dirty = 0;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001172 for (i = 0; i <= TX_RING_MOD_MASK; i++) {
1173 if (fep->tx_skbuff[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 dev_kfree_skb_any(fep->tx_skbuff[i]);
1175 fep->tx_skbuff[i] = NULL;
1176 }
1177 }
1178
Sascha Hauer22f6b862009-04-15 01:32:18 +00001179 /* Enable MII mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (duplex) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001181 /* MII enable / FD enable */
1182 writel(OPT_FRAME_SIZE | 0x04, fep->hwp + FEC_R_CNTRL);
1183 writel(0x04, fep->hwp + FEC_X_CNTRL);
Philippe De Muyterf909b1e2007-10-23 14:37:54 +10001184 } else {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001185 /* MII enable / No Rcv on Xmit */
1186 writel(OPT_FRAME_SIZE | 0x06, fep->hwp + FEC_R_CNTRL);
1187 writel(0x0, fep->hwp + FEC_X_CNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189 fep->full_duplex = duplex;
1190
Sascha Hauer22f6b862009-04-15 01:32:18 +00001191 /* Set MII speed */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001192 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Sascha Hauer22f6b862009-04-15 01:32:18 +00001194 /* And last, enable the transmit and receive processing */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001195 writel(2, fep->hwp + FEC_ECNTRL);
1196 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Matt Waddel6b265292006-06-27 13:10:56 +10001197
Sascha Hauer22f6b862009-04-15 01:32:18 +00001198 /* Enable interrupts we wish to service */
Bryan Wue6b043d2010-03-31 02:10:44 +00001199 writel(FEC_ENET_TXF | FEC_ENET_RXF, fep->hwp + FEC_IMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
1202static void
1203fec_stop(struct net_device *dev)
1204{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001205 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Sascha Hauer22f6b862009-04-15 01:32:18 +00001207 /* We cannot expect a graceful transmit stop without link !!! */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001208 if (fep->link) {
1209 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
Philippe De Muyter677177c2006-06-27 13:05:33 +10001210 udelay(10);
Sascha Hauerf44d6302009-04-15 03:11:30 +00001211 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Philippe De Muyter677177c2006-06-27 13:05:33 +10001212 printk("fec_stop : Graceful transmit stop did not complete !\n");
Sascha Hauerf44d6302009-04-15 03:11:30 +00001213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Sascha Hauerf44d6302009-04-15 03:11:30 +00001215 /* Whack a reset. We should wait for this. */
1216 writel(1, fep->hwp + FEC_ECNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 udelay(10);
1218
Sascha Hauerf44d6302009-04-15 03:11:30 +00001219 /* Clear outstanding MII command interrupts. */
1220 writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Sascha Hauerf44d6302009-04-15 03:11:30 +00001222 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
Sascha Haueread73182009-01-28 23:03:11 +00001225static int __devinit
1226fec_probe(struct platform_device *pdev)
1227{
1228 struct fec_enet_private *fep;
1229 struct net_device *ndev;
1230 int i, irq, ret = 0;
1231 struct resource *r;
1232
1233 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1234 if (!r)
1235 return -ENXIO;
1236
1237 r = request_mem_region(r->start, resource_size(r), pdev->name);
1238 if (!r)
1239 return -EBUSY;
1240
1241 /* Init network device */
1242 ndev = alloc_etherdev(sizeof(struct fec_enet_private));
1243 if (!ndev)
1244 return -ENOMEM;
1245
1246 SET_NETDEV_DEV(ndev, &pdev->dev);
1247
1248 /* setup board info structure */
1249 fep = netdev_priv(ndev);
1250 memset(fep, 0, sizeof(*fep));
1251
1252 ndev->base_addr = (unsigned long)ioremap(r->start, resource_size(r));
Bryan Wue6b043d2010-03-31 02:10:44 +00001253 fep->pdev = pdev;
Sascha Haueread73182009-01-28 23:03:11 +00001254
1255 if (!ndev->base_addr) {
1256 ret = -ENOMEM;
1257 goto failed_ioremap;
1258 }
1259
1260 platform_set_drvdata(pdev, ndev);
1261
1262 /* This device has up to three irqs on some platforms */
1263 for (i = 0; i < 3; i++) {
1264 irq = platform_get_irq(pdev, i);
1265 if (i && irq < 0)
1266 break;
1267 ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
1268 if (ret) {
1269 while (i >= 0) {
1270 irq = platform_get_irq(pdev, i);
1271 free_irq(irq, ndev);
1272 i--;
1273 }
1274 goto failed_irq;
1275 }
1276 }
1277
1278 fep->clk = clk_get(&pdev->dev, "fec_clk");
1279 if (IS_ERR(fep->clk)) {
1280 ret = PTR_ERR(fep->clk);
1281 goto failed_clk;
1282 }
1283 clk_enable(fep->clk);
1284
1285 ret = fec_enet_init(ndev, 0);
1286 if (ret)
1287 goto failed_init;
1288
Bryan Wue6b043d2010-03-31 02:10:44 +00001289 ret = fec_enet_mii_init(pdev);
1290 if (ret)
1291 goto failed_mii_init;
1292
Sascha Haueread73182009-01-28 23:03:11 +00001293 ret = register_netdev(ndev);
1294 if (ret)
1295 goto failed_register;
1296
Bryan Wue6b043d2010-03-31 02:10:44 +00001297 printk(KERN_INFO "%s: Freescale FEC PHY driver [%s] "
1298 "(mii_bus:phy_addr=%s, irq=%d)\n", ndev->name,
1299 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1300 fep->phy_dev->irq);
1301
Sascha Haueread73182009-01-28 23:03:11 +00001302 return 0;
1303
1304failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00001305 fec_enet_mii_remove(fep);
1306failed_mii_init:
Sascha Haueread73182009-01-28 23:03:11 +00001307failed_init:
1308 clk_disable(fep->clk);
1309 clk_put(fep->clk);
1310failed_clk:
1311 for (i = 0; i < 3; i++) {
1312 irq = platform_get_irq(pdev, i);
1313 if (irq > 0)
1314 free_irq(irq, ndev);
1315 }
1316failed_irq:
1317 iounmap((void __iomem *)ndev->base_addr);
1318failed_ioremap:
1319 free_netdev(ndev);
1320
1321 return ret;
1322}
1323
1324static int __devexit
1325fec_drv_remove(struct platform_device *pdev)
1326{
1327 struct net_device *ndev = platform_get_drvdata(pdev);
1328 struct fec_enet_private *fep = netdev_priv(ndev);
1329
1330 platform_set_drvdata(pdev, NULL);
1331
1332 fec_stop(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001333 fec_enet_mii_remove(fep);
Sascha Haueread73182009-01-28 23:03:11 +00001334 clk_disable(fep->clk);
1335 clk_put(fep->clk);
1336 iounmap((void __iomem *)ndev->base_addr);
1337 unregister_netdev(ndev);
1338 free_netdev(ndev);
1339 return 0;
1340}
1341
1342static int
1343fec_suspend(struct platform_device *dev, pm_message_t state)
1344{
1345 struct net_device *ndev = platform_get_drvdata(dev);
1346 struct fec_enet_private *fep;
1347
1348 if (ndev) {
1349 fep = netdev_priv(ndev);
1350 if (netif_running(ndev)) {
1351 netif_device_detach(ndev);
1352 fec_stop(ndev);
1353 }
1354 }
1355 return 0;
1356}
1357
1358static int
1359fec_resume(struct platform_device *dev)
1360{
1361 struct net_device *ndev = platform_get_drvdata(dev);
1362
1363 if (ndev) {
1364 if (netif_running(ndev)) {
1365 fec_enet_init(ndev, 0);
1366 netif_device_attach(ndev);
1367 }
1368 }
1369 return 0;
1370}
1371
1372static struct platform_driver fec_driver = {
1373 .driver = {
1374 .name = "fec",
1375 .owner = THIS_MODULE,
1376 },
1377 .probe = fec_probe,
1378 .remove = __devexit_p(fec_drv_remove),
1379 .suspend = fec_suspend,
1380 .resume = fec_resume,
1381};
1382
1383static int __init
1384fec_enet_module_init(void)
1385{
1386 printk(KERN_INFO "FEC Ethernet Driver\n");
1387
1388 return platform_driver_register(&fec_driver);
1389}
1390
1391static void __exit
1392fec_enet_cleanup(void)
1393{
1394 platform_driver_unregister(&fec_driver);
1395}
1396
1397module_exit(fec_enet_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398module_init(fec_enet_module_init);
1399
1400MODULE_LICENSE("GPL");