blob: cce32d43175f5c3ed5f6e7ef2ebd56eede8e9139 [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>
Baruch Siach5eb32bd2010-05-24 00:36:13 -070044#include <linux/fec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Greg Ungerer080853a2007-07-30 16:28:46 +100046#include <asm/cacheflush.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000047
48#ifndef CONFIG_ARCH_MXC
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/coldfire.h>
50#include <asm/mcfsim.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000051#endif
Sascha Hauer6f501b12009-01-28 23:03:05 +000052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include "fec.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Sascha Hauer196719e2009-01-28 23:03:10 +000055#ifdef CONFIG_ARCH_MXC
56#include <mach/hardware.h>
57#define FEC_ALIGNMENT 0xf
58#else
59#define FEC_ALIGNMENT 0x3
60#endif
61
Sascha Haueread73182009-01-28 23:03:11 +000062/*
63 * Define the fixed address of the FEC hardware.
64 */
Greg Ungerer87f4abb2008-06-06 15:55:36 +100065#if defined(CONFIG_M5272)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67static unsigned char fec_mac_default[] = {
68 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69};
70
71/*
72 * Some hardware gets it MAC address out of local flash memory.
73 * if this is non-zero then assume it is the address to get MAC from.
74 */
75#if defined(CONFIG_NETtel)
76#define FEC_FLASHMAC 0xf0006006
77#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
78#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#elif defined(CONFIG_CANCam)
80#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +100081#elif defined (CONFIG_M5272C3)
82#define FEC_FLASHMAC (0xffe04000 + 4)
83#elif defined(CONFIG_MOD5272)
84#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#else
86#define FEC_FLASHMAC 0
87#endif
Greg Ungerer43be6362009-02-26 22:42:51 -080088#endif /* CONFIG_M5272 */
Sascha Haueread73182009-01-28 23:03:11 +000089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* The number of Tx and Rx buffers. These are allocated from the page
91 * pool. The code may assume these are power of two, so it it best
92 * to keep them that size.
93 * We don't need to allocate pages for the transmitter. We just use
94 * the skbuffer directly.
95 */
96#define FEC_ENET_RX_PAGES 8
97#define FEC_ENET_RX_FRSIZE 2048
98#define FEC_ENET_RX_FRPPG (PAGE_SIZE / FEC_ENET_RX_FRSIZE)
99#define RX_RING_SIZE (FEC_ENET_RX_FRPPG * FEC_ENET_RX_PAGES)
100#define FEC_ENET_TX_FRSIZE 2048
101#define FEC_ENET_TX_FRPPG (PAGE_SIZE / FEC_ENET_TX_FRSIZE)
102#define TX_RING_SIZE 16 /* Must be power of two */
103#define TX_RING_MOD_MASK 15 /* for this to work */
104
Greg Ungerer562d2f82005-11-07 14:09:50 +1000105#if (((RX_RING_SIZE + TX_RING_SIZE) * 8) > PAGE_SIZE)
Matt Waddel6b265292006-06-27 13:10:56 +1000106#error "FEC: descriptor ring size constants too large"
Greg Ungerer562d2f82005-11-07 14:09:50 +1000107#endif
108
Sascha Hauer22f6b862009-04-15 01:32:18 +0000109/* Interrupt events/masks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
111#define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
112#define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
113#define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
114#define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
115#define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
116#define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
117#define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
118#define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
119#define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
120
Wolfram Sang4bee1f92010-07-21 02:51:13 +0000121#define FEC_DEFAULT_IMASK (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII)
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/* The FEC stores dest/src/type, data, and checksum for receive packets.
124 */
125#define PKT_MAXBUF_SIZE 1518
126#define PKT_MINBUF_SIZE 64
127#define PKT_MAXBLR_SIZE 1520
128
129
130/*
Matt Waddel6b265292006-06-27 13:10:56 +1000131 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 * size bits. Other FEC hardware does not, so we need to take that into
133 * account when setting it.
134 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000135#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Sascha Hauer196719e2009-01-28 23:03:10 +0000136 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARCH_MXC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
138#else
139#define OPT_FRAME_SIZE 0
140#endif
141
142/* The FEC buffer descriptors track the ring buffers. The rx_bd_base and
143 * tx_bd_base always point to the base of the buffer descriptors. The
144 * cur_rx and cur_tx point to the currently available buffer.
145 * The dirty_tx tracks the current buffer that is being sent by the
146 * controller. The cur_tx and dirty_tx are equal under both completely
147 * empty and completely full conditions. The empty/ready indicator in
148 * the buffer descriptor determines the actual condition.
149 */
150struct fec_enet_private {
151 /* Hardware registers of the FEC device */
Sascha Hauerf44d6302009-04-15 03:11:30 +0000152 void __iomem *hwp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Greg Ungerercb84d6e2007-07-30 16:29:09 +1000154 struct net_device *netdev;
155
Sascha Haueread73182009-01-28 23:03:11 +0000156 struct clk *clk;
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /* The saved address of a sent-in-place packet/buffer, for skfree(). */
159 unsigned char *tx_bounce[TX_RING_SIZE];
160 struct sk_buff* tx_skbuff[TX_RING_SIZE];
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000161 struct sk_buff* rx_skbuff[RX_RING_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 ushort skb_cur;
163 ushort skb_dirty;
164
Sascha Hauer22f6b862009-04-15 01:32:18 +0000165 /* CPM dual port RAM relative addresses */
Sascha Hauer4661e752009-01-28 23:03:07 +0000166 dma_addr_t bd_dma;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000167 /* Address of Rx and Tx buffers */
Sascha Hauer2e285322009-04-15 01:32:16 +0000168 struct bufdesc *rx_bd_base;
169 struct bufdesc *tx_bd_base;
170 /* The next free ring entry */
171 struct bufdesc *cur_rx, *cur_tx;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000172 /* The ring entries to be free()ed */
Sascha Hauer2e285322009-04-15 01:32:16 +0000173 struct bufdesc *dirty_tx;
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 uint tx_full;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000176 /* hold while accessing the HW like ringbuffer for tx/rx but not MAC */
177 spinlock_t hw_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Bryan Wue6b043d2010-03-31 02:10:44 +0000179 struct platform_device *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 int opened;
Bryan Wue6b043d2010-03-31 02:10:44 +0000182
183 /* Phylib and MDIO interface */
184 struct mii_bus *mii_bus;
185 struct phy_device *phy_dev;
186 int mii_timeout;
187 uint phy_speed;
Baruch Siach5eb32bd2010-05-24 00:36:13 -0700188 phy_interface_t phy_interface;
Bryan Wue6b043d2010-03-31 02:10:44 +0000189 int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 int link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 int full_duplex;
Baruch Siach97b72e42010-07-11 21:12:51 +0000192 struct completion mdio_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193};
194
David Howells7d12e782006-10-05 14:55:46 +0100195static irqreturn_t fec_enet_interrupt(int irq, void * dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static void fec_enet_tx(struct net_device *dev);
197static void fec_enet_rx(struct net_device *dev);
198static int fec_enet_close(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static void fec_restart(struct net_device *dev, int duplex);
200static void fec_stop(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Bryan Wue6b043d2010-03-31 02:10:44 +0000202/* FEC MII MMFR bits definition */
203#define FEC_MMFR_ST (1 << 30)
204#define FEC_MMFR_OP_READ (2 << 28)
205#define FEC_MMFR_OP_WRITE (1 << 28)
206#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
207#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
208#define FEC_MMFR_TA (2 << 16)
209#define FEC_MMFR_DATA(v) (v & 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Baruch Siach97b72e42010-07-11 21:12:51 +0000211#define FEC_MII_TIMEOUT 1000 /* us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Sascha Hauer22f6b862009-04-15 01:32:18 +0000213/* Transmitter timeout */
214#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Denis Kirjanovc7621cb2010-06-02 09:15:47 +0000216static netdev_tx_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
218{
Sascha Hauerf44d6302009-04-15 03:11:30 +0000219 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000220 struct bufdesc *bdp;
Greg Ungerer9555b312009-08-06 17:58:18 +0000221 void *bufaddr;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000222 unsigned short status;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000223 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (!fep->link) {
226 /* Link is down or autonegotiation is in progress. */
Patrick McHardy5b548142009-06-12 06:22:29 +0000227 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000230 spin_lock_irqsave(&fep->hw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /* Fill in a Tx ring entry */
232 bdp = fep->cur_tx;
233
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000234 status = bdp->cbd_sc;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000235
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000236 if (status & BD_ENET_TX_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 /* Ooops. All transmit buffers are full. Bail out.
238 * This should not happen, since dev->tbusy should be set.
239 */
240 printk("%s: tx queue full!.\n", dev->name);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000241 spin_unlock_irqrestore(&fep->hw_lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +0000242 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Sascha Hauer22f6b862009-04-15 01:32:18 +0000245 /* Clear all of the status flags */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000246 status &= ~BD_ENET_TX_STATS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Sascha Hauer22f6b862009-04-15 01:32:18 +0000248 /* Set buffer length and buffer pointer */
Greg Ungerer9555b312009-08-06 17:58:18 +0000249 bufaddr = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 bdp->cbd_datlen = skb->len;
251
252 /*
Sascha Hauer22f6b862009-04-15 01:32:18 +0000253 * On some FEC implementations data must be aligned on
254 * 4-byte boundaries. Use bounce buffers to copy data
255 * and get it aligned. Ugh.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 */
Greg Ungerer9555b312009-08-06 17:58:18 +0000257 if (((unsigned long) bufaddr) & FEC_ALIGNMENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 unsigned int index;
259 index = bdp - fep->tx_bd_base;
Sascha Hauer6989f512009-01-28 23:03:06 +0000260 memcpy(fep->tx_bounce[index], (void *)skb->data, skb->len);
Greg Ungerer9555b312009-08-06 17:58:18 +0000261 bufaddr = fep->tx_bounce[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263
Sascha Hauer22f6b862009-04-15 01:32:18 +0000264 /* Save skb pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 fep->tx_skbuff[fep->skb_cur] = skb;
266
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700267 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 fep->skb_cur = (fep->skb_cur+1) & TX_RING_MOD_MASK;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 /* Push the data cache so the CPM does not get stale memory
271 * data.
272 */
Greg Ungerer9555b312009-08-06 17:58:18 +0000273 bdp->cbd_bufaddr = dma_map_single(&dev->dev, bufaddr,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000274 FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000276 /* Send it on its way. Tell FEC it's ready, interrupt when done,
277 * it's the last BD of the frame, and to put the CRC on the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000279 status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 | BD_ENET_TX_LAST | BD_ENET_TX_TC);
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000281 bdp->cbd_sc = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /* Trigger transmission start */
Sascha Hauerf44d6302009-04-15 03:11:30 +0000284 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Sascha Hauer22f6b862009-04-15 01:32:18 +0000286 /* If this was the last BD in the ring, start at the beginning again. */
287 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 bdp = fep->tx_bd_base;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000289 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 bdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 if (bdp == fep->dirty_tx) {
293 fep->tx_full = 1;
294 netif_stop_queue(dev);
295 }
296
Sascha Hauer2e285322009-04-15 01:32:16 +0000297 fep->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000299 spin_unlock_irqrestore(&fep->hw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Patrick McHardy6ed10652009-06-23 06:03:08 +0000301 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304static void
305fec_timeout(struct net_device *dev)
306{
307 struct fec_enet_private *fep = netdev_priv(dev);
308
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700309 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000311 fec_restart(dev, fep->full_duplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 netif_wake_queue(dev);
313}
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100316fec_enet_interrupt(int irq, void * dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 struct net_device *dev = dev_id;
Sascha Hauerf44d6302009-04-15 03:11:30 +0000319 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 uint int_events;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000321 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000323 do {
Sascha Hauerf44d6302009-04-15 03:11:30 +0000324 int_events = readl(fep->hwp + FEC_IEVENT);
325 writel(int_events, fep->hwp + FEC_IEVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (int_events & FEC_ENET_RXF) {
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000328 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 fec_enet_rx(dev);
330 }
331
332 /* Transmit OK, or non-fatal error. Update the buffer
Sascha Hauerf44d6302009-04-15 03:11:30 +0000333 * descriptors. FEC handles all errors, we just discover
334 * them as part of the transmit process.
335 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (int_events & FEC_ENET_TXF) {
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000337 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 fec_enet_tx(dev);
339 }
Baruch Siach97b72e42010-07-11 21:12:51 +0000340
341 if (int_events & FEC_ENET_MII) {
342 ret = IRQ_HANDLED;
343 complete(&fep->mdio_done);
344 }
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000345 } while (int_events);
346
347 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
350
351static void
352fec_enet_tx(struct net_device *dev)
353{
354 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +0000355 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000356 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 struct sk_buff *skb;
358
359 fep = netdev_priv(dev);
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000360 spin_lock(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 bdp = fep->dirty_tx;
362
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000363 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000364 if (bdp == fep->cur_tx && fep->tx_full == 0)
365 break;
366
367 dma_unmap_single(&dev->dev, bdp->cbd_bufaddr, FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
368 bdp->cbd_bufaddr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 skb = fep->tx_skbuff[fep->skb_dirty];
371 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000372 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 BD_ENET_TX_RL | BD_ENET_TX_UN |
374 BD_ENET_TX_CSL)) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700375 dev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000376 if (status & BD_ENET_TX_HB) /* No heartbeat */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700377 dev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000378 if (status & BD_ENET_TX_LC) /* Late collision */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700379 dev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000380 if (status & BD_ENET_TX_RL) /* Retrans limit */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700381 dev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000382 if (status & BD_ENET_TX_UN) /* Underrun */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700383 dev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000384 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700385 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 } else {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700387 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000390 if (status & BD_ENET_TX_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 printk("HEY! Enet xmit interrupt and TX_READY.\n");
Sascha Hauer22f6b862009-04-15 01:32:18 +0000392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /* Deferred means some collisions occurred during transmit,
394 * but we eventually sent the packet OK.
395 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000396 if (status & BD_ENET_TX_DEF)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700397 dev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400398
Sascha Hauer22f6b862009-04-15 01:32:18 +0000399 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 dev_kfree_skb_any(skb);
401 fep->tx_skbuff[fep->skb_dirty] = NULL;
402 fep->skb_dirty = (fep->skb_dirty + 1) & TX_RING_MOD_MASK;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400403
Sascha Hauer22f6b862009-04-15 01:32:18 +0000404 /* Update pointer to next buffer descriptor to be transmitted */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000405 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 bdp = fep->tx_bd_base;
407 else
408 bdp++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400409
Sascha Hauer22f6b862009-04-15 01:32:18 +0000410 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 */
412 if (fep->tx_full) {
413 fep->tx_full = 0;
414 if (netif_queue_stopped(dev))
415 netif_wake_queue(dev);
416 }
417 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000418 fep->dirty_tx = bdp;
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000419 spin_unlock(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422
423/* During a receive, the cur_rx points to the current incoming buffer.
424 * When we update through the ring, if the next incoming buffer has
425 * not been given to the system, we just set the empty indicator,
426 * effectively tossing the packet.
427 */
428static void
429fec_enet_rx(struct net_device *dev)
430{
Sascha Hauerf44d6302009-04-15 03:11:30 +0000431 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000432 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000433 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 struct sk_buff *skb;
435 ushort pkt_len;
436 __u8 *data;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400437
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000438#ifdef CONFIG_M532x
439 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400440#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000442 spin_lock(&fep->hw_lock);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /* First, grab all of the stats for the incoming packet.
445 * These get messed up if we get called due to a busy condition.
446 */
447 bdp = fep->cur_rx;
448
Sascha Hauer22f6b862009-04-15 01:32:18 +0000449 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Sascha Hauer22f6b862009-04-15 01:32:18 +0000451 /* Since we have allocated space to hold a complete frame,
452 * the last indicator should be set.
453 */
454 if ((status & BD_ENET_RX_LAST) == 0)
455 printk("FEC ENET: rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Sascha Hauer22f6b862009-04-15 01:32:18 +0000457 if (!fep->opened)
458 goto rx_processing_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Sascha Hauer22f6b862009-04-15 01:32:18 +0000460 /* Check for errors. */
461 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Sascha Hauer22f6b862009-04-15 01:32:18 +0000463 dev->stats.rx_errors++;
464 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
465 /* Frame too long or too short. */
466 dev->stats.rx_length_errors++;
467 }
468 if (status & BD_ENET_RX_NO) /* Frame alignment */
469 dev->stats.rx_frame_errors++;
470 if (status & BD_ENET_RX_CR) /* CRC Error */
471 dev->stats.rx_crc_errors++;
472 if (status & BD_ENET_RX_OV) /* FIFO overrun */
473 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
Sascha Hauer22f6b862009-04-15 01:32:18 +0000475
476 /* Report late collisions as a frame error.
477 * On this error, the BD is closed, but we don't know what we
478 * have in the buffer. So, just drop this frame on the floor.
479 */
480 if (status & BD_ENET_RX_CL) {
481 dev->stats.rx_errors++;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700482 dev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000483 goto rx_processing_done;
484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Sascha Hauer22f6b862009-04-15 01:32:18 +0000486 /* Process the incoming frame. */
487 dev->stats.rx_packets++;
488 pkt_len = bdp->cbd_datlen;
489 dev->stats.rx_bytes += pkt_len;
490 data = (__u8*)__va(bdp->cbd_bufaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000492 dma_unmap_single(NULL, bdp->cbd_bufaddr, bdp->cbd_datlen,
493 DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +0000494
Sascha Hauer22f6b862009-04-15 01:32:18 +0000495 /* This does 16 byte alignment, exactly what we need.
496 * The packet length includes FCS, but we don't want to
497 * include that when passing upstream as it messes up
498 * bridging applications.
499 */
Sascha Hauer85498892009-04-15 01:32:21 +0000500 skb = dev_alloc_skb(pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Sascha Hauer85498892009-04-15 01:32:21 +0000502 if (unlikely(!skb)) {
Sascha Hauer22f6b862009-04-15 01:32:18 +0000503 printk("%s: Memory squeeze, dropping packet.\n",
504 dev->name);
505 dev->stats.rx_dropped++;
506 } else {
Sascha Hauer85498892009-04-15 01:32:21 +0000507 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000508 skb_put(skb, pkt_len - 4); /* Make room */
509 skb_copy_to_linear_data(skb, data, pkt_len - 4);
510 skb->protocol = eth_type_trans(skb, dev);
511 netif_rx(skb);
512 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000513
514 bdp->cbd_bufaddr = dma_map_single(NULL, data, bdp->cbd_datlen,
515 DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000516rx_processing_done:
517 /* Clear the status flags for this buffer */
518 status &= ~BD_ENET_RX_STATS;
519
520 /* Mark the buffer empty */
521 status |= BD_ENET_RX_EMPTY;
522 bdp->cbd_sc = status;
523
524 /* Update BD pointer to next entry */
525 if (status & BD_ENET_RX_WRAP)
526 bdp = fep->rx_bd_base;
527 else
528 bdp++;
529 /* Doing this here will keep the FEC running while we process
530 * incoming frames. On a heavily loaded network, we should be
531 * able to keep up at the expense of system resources.
532 */
533 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000535 fep->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000537 spin_unlock(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540/* ------------------------------------------------------------------------- */
Greg Ungerer43be6362009-02-26 22:42:51 -0800541#ifdef CONFIG_M5272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542static void __inline__ fec_get_mac(struct net_device *dev)
543{
544 struct fec_enet_private *fep = netdev_priv(dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000545 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000547 if (FEC_FLASHMAC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /*
549 * Get MAC address from FLASH.
550 * If it is all 1's or 0's, use the default.
551 */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000552 iap = (unsigned char *)FEC_FLASHMAC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if ((iap[0] == 0) && (iap[1] == 0) && (iap[2] == 0) &&
554 (iap[3] == 0) && (iap[4] == 0) && (iap[5] == 0))
555 iap = fec_mac_default;
556 if ((iap[0] == 0xff) && (iap[1] == 0xff) && (iap[2] == 0xff) &&
557 (iap[3] == 0xff) && (iap[4] == 0xff) && (iap[5] == 0xff))
558 iap = fec_mac_default;
559 } else {
Sascha Hauerf44d6302009-04-15 03:11:30 +0000560 *((unsigned long *) &tmpaddr[0]) = readl(fep->hwp + FEC_ADDR_LOW);
561 *((unsigned short *) &tmpaddr[4]) = (readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 iap = &tmpaddr[0];
563 }
564
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000565 memcpy(dev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 /* Adjust MAC if using default MAC address */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000568 if (iap == fec_mac_default)
569 dev->dev_addr[ETH_ALEN-1] = fec_mac_default[ETH_ALEN-1] + fep->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571#endif
572
573/* ------------------------------------------------------------------------- */
574
Bryan Wue6b043d2010-03-31 02:10:44 +0000575/*
576 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
Bryan Wue6b043d2010-03-31 02:10:44 +0000578static void fec_enet_adjust_link(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Bryan Wue6b043d2010-03-31 02:10:44 +0000580 struct fec_enet_private *fep = netdev_priv(dev);
581 struct phy_device *phy_dev = fep->phy_dev;
582 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Bryan Wue6b043d2010-03-31 02:10:44 +0000584 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Bryan Wue6b043d2010-03-31 02:10:44 +0000586 spin_lock_irqsave(&fep->hw_lock, flags);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400587
Bryan Wue6b043d2010-03-31 02:10:44 +0000588 /* Prevent a state halted on mii error */
589 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
590 phy_dev->state = PHY_RESUMING;
591 goto spin_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
Bryan Wue6b043d2010-03-31 02:10:44 +0000593
594 /* Duplex link change */
595 if (phy_dev->link) {
596 if (fep->full_duplex != phy_dev->duplex) {
597 fec_restart(dev, phy_dev->duplex);
598 status_change = 1;
599 }
600 }
601
602 /* Link on or off change */
603 if (phy_dev->link != fep->link) {
604 fep->link = phy_dev->link;
605 if (phy_dev->link)
606 fec_restart(dev, phy_dev->duplex);
607 else
608 fec_stop(dev);
609 status_change = 1;
610 }
611
612spin_unlock:
613 spin_unlock_irqrestore(&fep->hw_lock, flags);
614
615 if (status_change)
616 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
Bryan Wue6b043d2010-03-31 02:10:44 +0000619static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Bryan Wue6b043d2010-03-31 02:10:44 +0000621 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +0000622 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +0000623
624 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +0000625 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +0000626
627 /* start a read op */
628 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
629 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
630 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
631
632 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +0000633 time_left = wait_for_completion_timeout(&fep->mdio_done,
634 usecs_to_jiffies(FEC_MII_TIMEOUT));
635 if (time_left == 0) {
636 fep->mii_timeout = 1;
637 printk(KERN_ERR "FEC: MDIO read timeout\n");
638 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +0000639 }
640
641 /* return value */
642 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
643}
644
645static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
646 u16 value)
647{
648 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +0000649 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +0000650
651 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +0000652 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +0000653
654 /* start a read op */
655 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
656 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
657 FEC_MMFR_TA | FEC_MMFR_DATA(value),
658 fep->hwp + FEC_MII_DATA);
659
660 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +0000661 time_left = wait_for_completion_timeout(&fep->mdio_done,
662 usecs_to_jiffies(FEC_MII_TIMEOUT));
663 if (time_left == 0) {
664 fep->mii_timeout = 1;
665 printk(KERN_ERR "FEC: MDIO write timeout\n");
666 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +0000667 }
668
669 return 0;
670}
671
672static int fec_enet_mdio_reset(struct mii_bus *bus)
673{
674 return 0;
675}
676
677static int fec_enet_mii_probe(struct net_device *dev)
678{
679 struct fec_enet_private *fep = netdev_priv(dev);
680 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +0000681 char mdio_bus_id[MII_BUS_ID_SIZE];
682 char phy_name[MII_BUS_ID_SIZE + 3];
683 int phy_id;
Bryan Wue6b043d2010-03-31 02:10:44 +0000684
Bryan Wu418bd0d2010-05-28 03:40:39 -0700685 fep->phy_dev = NULL;
686
Greg Ungerer6fcc0402010-10-11 21:03:05 +0000687 /* check for attached phy */
688 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
689 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
690 continue;
691 if (fep->mii_bus->phy_map[phy_id] == NULL)
692 continue;
693 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
694 continue;
695 strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
696 break;
Bryan Wue6b043d2010-03-31 02:10:44 +0000697 }
698
Greg Ungerer6fcc0402010-10-11 21:03:05 +0000699 if (phy_id >= PHY_MAX_ADDR) {
700 printk(KERN_INFO "%s: no PHY, assuming direct connection "
701 "to switch\n", dev->name);
702 strncpy(mdio_bus_id, "0", MII_BUS_ID_SIZE);
703 phy_id = 0;
704 }
705
706 snprintf(phy_name, MII_BUS_ID_SIZE, PHY_ID_FMT, mdio_bus_id, phy_id);
707 phy_dev = phy_connect(dev, phy_name, &fec_enet_adjust_link, 0,
708 PHY_INTERFACE_MODE_MII);
709 if (IS_ERR(phy_dev)) {
710 printk(KERN_ERR "%s: could not attach to PHY\n", dev->name);
711 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +0000712 }
713
714 /* mask with MAC supported features */
715 phy_dev->supported &= PHY_BASIC_FEATURES;
716 phy_dev->advertising = phy_dev->supported;
717
718 fep->phy_dev = phy_dev;
719 fep->link = 0;
720 fep->full_duplex = 0;
721
Bryan Wu418bd0d2010-05-28 03:40:39 -0700722 printk(KERN_INFO "%s: Freescale FEC PHY driver [%s] "
723 "(mii_bus:phy_addr=%s, irq=%d)\n", dev->name,
724 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
725 fep->phy_dev->irq);
726
Bryan Wue6b043d2010-03-31 02:10:44 +0000727 return 0;
728}
729
730static int fec_enet_mii_init(struct platform_device *pdev)
731{
732 struct net_device *dev = platform_get_drvdata(pdev);
733 struct fec_enet_private *fep = netdev_priv(dev);
734 int err = -ENXIO, i;
735
736 fep->mii_timeout = 0;
737
738 /*
739 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
740 */
741 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk), 5000000) << 1;
742 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
743
744 fep->mii_bus = mdiobus_alloc();
745 if (fep->mii_bus == NULL) {
746 err = -ENOMEM;
747 goto err_out;
748 }
749
750 fep->mii_bus->name = "fec_enet_mii_bus";
751 fep->mii_bus->read = fec_enet_mdio_read;
752 fep->mii_bus->write = fec_enet_mdio_write;
753 fep->mii_bus->reset = fec_enet_mdio_reset;
Greg Ungerer6fcc0402010-10-11 21:03:05 +0000754 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%x", pdev->id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +0000755 fep->mii_bus->priv = fep;
756 fep->mii_bus->parent = &pdev->dev;
757
758 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
759 if (!fep->mii_bus->irq) {
760 err = -ENOMEM;
761 goto err_out_free_mdiobus;
762 }
763
764 for (i = 0; i < PHY_MAX_ADDR; i++)
765 fep->mii_bus->irq[i] = PHY_POLL;
766
767 platform_set_drvdata(dev, fep->mii_bus);
768
769 if (mdiobus_register(fep->mii_bus))
770 goto err_out_free_mdio_irq;
771
Bryan Wue6b043d2010-03-31 02:10:44 +0000772 return 0;
773
Bryan Wue6b043d2010-03-31 02:10:44 +0000774err_out_free_mdio_irq:
775 kfree(fep->mii_bus->irq);
776err_out_free_mdiobus:
777 mdiobus_free(fep->mii_bus);
778err_out:
779 return err;
780}
781
782static void fec_enet_mii_remove(struct fec_enet_private *fep)
783{
784 if (fep->phy_dev)
785 phy_disconnect(fep->phy_dev);
786 mdiobus_unregister(fep->mii_bus);
787 kfree(fep->mii_bus->irq);
788 mdiobus_free(fep->mii_bus);
789}
790
791static int fec_enet_get_settings(struct net_device *dev,
792 struct ethtool_cmd *cmd)
793{
794 struct fec_enet_private *fep = netdev_priv(dev);
795 struct phy_device *phydev = fep->phy_dev;
796
797 if (!phydev)
798 return -ENODEV;
799
800 return phy_ethtool_gset(phydev, cmd);
801}
802
803static int fec_enet_set_settings(struct net_device *dev,
804 struct ethtool_cmd *cmd)
805{
806 struct fec_enet_private *fep = netdev_priv(dev);
807 struct phy_device *phydev = fep->phy_dev;
808
809 if (!phydev)
810 return -ENODEV;
811
812 return phy_ethtool_sset(phydev, cmd);
813}
814
815static void fec_enet_get_drvinfo(struct net_device *dev,
816 struct ethtool_drvinfo *info)
817{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 struct fec_enet_private *fep = netdev_priv(dev);
819
Bryan Wue6b043d2010-03-31 02:10:44 +0000820 strcpy(info->driver, fep->pdev->dev.driver->name);
821 strcpy(info->version, "Revision: 1.0");
822 strcpy(info->bus_info, dev_name(&dev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
Bryan Wue6b043d2010-03-31 02:10:44 +0000824
825static struct ethtool_ops fec_enet_ethtool_ops = {
826 .get_settings = fec_enet_get_settings,
827 .set_settings = fec_enet_set_settings,
828 .get_drvinfo = fec_enet_get_drvinfo,
829 .get_link = ethtool_op_get_link,
830};
831
832static int fec_enet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
833{
834 struct fec_enet_private *fep = netdev_priv(dev);
835 struct phy_device *phydev = fep->phy_dev;
836
837 if (!netif_running(dev))
838 return -EINVAL;
839
840 if (!phydev)
841 return -ENODEV;
842
Richard Cochran28b04112010-07-17 08:48:55 +0000843 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +0000844}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000846static void fec_enet_free_buffers(struct net_device *dev)
847{
848 struct fec_enet_private *fep = netdev_priv(dev);
849 int i;
850 struct sk_buff *skb;
851 struct bufdesc *bdp;
852
853 bdp = fep->rx_bd_base;
854 for (i = 0; i < RX_RING_SIZE; i++) {
855 skb = fep->rx_skbuff[i];
856
857 if (bdp->cbd_bufaddr)
858 dma_unmap_single(&dev->dev, bdp->cbd_bufaddr,
859 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
860 if (skb)
861 dev_kfree_skb(skb);
862 bdp++;
863 }
864
865 bdp = fep->tx_bd_base;
866 for (i = 0; i < TX_RING_SIZE; i++)
867 kfree(fep->tx_bounce[i]);
868}
869
870static int fec_enet_alloc_buffers(struct net_device *dev)
871{
872 struct fec_enet_private *fep = netdev_priv(dev);
873 int i;
874 struct sk_buff *skb;
875 struct bufdesc *bdp;
876
877 bdp = fep->rx_bd_base;
878 for (i = 0; i < RX_RING_SIZE; i++) {
879 skb = dev_alloc_skb(FEC_ENET_RX_FRSIZE);
880 if (!skb) {
881 fec_enet_free_buffers(dev);
882 return -ENOMEM;
883 }
884 fep->rx_skbuff[i] = skb;
885
886 bdp->cbd_bufaddr = dma_map_single(&dev->dev, skb->data,
887 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
888 bdp->cbd_sc = BD_ENET_RX_EMPTY;
889 bdp++;
890 }
891
892 /* Set the last buffer to wrap. */
893 bdp--;
894 bdp->cbd_sc |= BD_SC_WRAP;
895
896 bdp = fep->tx_bd_base;
897 for (i = 0; i < TX_RING_SIZE; i++) {
898 fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
899
900 bdp->cbd_sc = 0;
901 bdp->cbd_bufaddr = 0;
902 bdp++;
903 }
904
905 /* Set the last buffer to wrap. */
906 bdp--;
907 bdp->cbd_sc |= BD_SC_WRAP;
908
909 return 0;
910}
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912static int
913fec_enet_open(struct net_device *dev)
914{
915 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000916 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 /* I should reset the ring buffers here, but I don't yet know
919 * a simple way to do that.
920 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000922 ret = fec_enet_alloc_buffers(dev);
923 if (ret)
924 return ret;
925
Bryan Wu418bd0d2010-05-28 03:40:39 -0700926 /* Probe and connect to PHY when open the interface */
927 ret = fec_enet_mii_probe(dev);
928 if (ret) {
929 fec_enet_free_buffers(dev);
930 return ret;
931 }
Bryan Wue6b043d2010-03-31 02:10:44 +0000932 phy_start(fep->phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 netif_start_queue(dev);
934 fep->opened = 1;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000935 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
938static int
939fec_enet_close(struct net_device *dev)
940{
941 struct fec_enet_private *fep = netdev_priv(dev);
942
Sascha Hauer22f6b862009-04-15 01:32:18 +0000943 /* Don't know what to do yet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 fep->opened = 0;
945 netif_stop_queue(dev);
946 fec_stop(dev);
947
Bryan Wu418bd0d2010-05-28 03:40:39 -0700948 if (fep->phy_dev)
949 phy_disconnect(fep->phy_dev);
950
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000951 fec_enet_free_buffers(dev);
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return 0;
954}
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956/* Set or clear the multicast filter for this adaptor.
957 * Skeleton taken from sunlance driver.
958 * The CPM Ethernet implementation allows Multicast as well as individual
959 * MAC address filtering. Some of the drivers check to make sure it is
960 * a group multicast address, and discard those that are not. I guess I
961 * will do the same for now, but just remove the test if you want
962 * individual filtering as well (do the upper net layers want or support
963 * this kind of feature?).
964 */
965
966#define HASH_BITS 6 /* #bits in hash */
967#define CRC32_POLY 0xEDB88320
968
969static void set_multicast_list(struct net_device *dev)
970{
Sascha Hauerf44d6302009-04-15 03:11:30 +0000971 struct fec_enet_private *fep = netdev_priv(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000972 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +0000973 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 unsigned char hash;
975
Sascha Hauer22f6b862009-04-15 01:32:18 +0000976 if (dev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +0000977 tmp = readl(fep->hwp + FEC_R_CNTRL);
978 tmp |= 0x8;
979 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +0000980 return;
981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Sascha Hauer4e831832009-04-15 01:32:19 +0000983 tmp = readl(fep->hwp + FEC_R_CNTRL);
984 tmp &= ~0x8;
985 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400986
Sascha Hauer4e831832009-04-15 01:32:19 +0000987 if (dev->flags & IFF_ALLMULTI) {
988 /* Catch all multicast addresses, so set the
989 * filter to all 1's
990 */
991 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
992 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Sascha Hauer4e831832009-04-15 01:32:19 +0000994 return;
995 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400996
Sascha Hauer4e831832009-04-15 01:32:19 +0000997 /* Clear filter and add the addresses in hash register
998 */
999 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1000 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Jiri Pirko22bedad32010-04-01 21:22:57 +00001002 netdev_for_each_mc_addr(ha, dev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00001003 /* Only support group multicast for now */
Jiri Pirko22bedad32010-04-01 21:22:57 +00001004 if (!(ha->addr[0] & 1))
Sascha Hauer4e831832009-04-15 01:32:19 +00001005 continue;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001006
Sascha Hauer4e831832009-04-15 01:32:19 +00001007 /* calculate crc32 value of mac address */
1008 crc = 0xffffffff;
1009
Jiri Pirko22bedad32010-04-01 21:22:57 +00001010 for (i = 0; i < dev->addr_len; i++) {
1011 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00001012 for (bit = 0; bit < 8; bit++, data >>= 1) {
1013 crc = (crc >> 1) ^
1014 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016 }
Sascha Hauer4e831832009-04-15 01:32:19 +00001017
1018 /* only upper 6 bits (HASH_BITS) are used
1019 * which point to specific bit in he hash registers
1020 */
1021 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
1022
1023 if (hash > 31) {
1024 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1025 tmp |= 1 << (hash - 32);
1026 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1027 } else {
1028 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1029 tmp |= 1 << hash;
1030 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033}
1034
Sascha Hauer22f6b862009-04-15 01:32:18 +00001035/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00001036static int
1037fec_set_mac_address(struct net_device *dev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001039 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer009fda82009-04-15 01:32:23 +00001040 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Sascha Hauer009fda82009-04-15 01:32:23 +00001042 if (!is_valid_ether_addr(addr->sa_data))
1043 return -EADDRNOTAVAIL;
1044
1045 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1046
Sascha Hauerf44d6302009-04-15 03:11:30 +00001047 writel(dev->dev_addr[3] | (dev->dev_addr[2] << 8) |
1048 (dev->dev_addr[1] << 16) | (dev->dev_addr[0] << 24),
1049 fep->hwp + FEC_ADDR_LOW);
1050 writel((dev->dev_addr[5] << 16) | (dev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07001051 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00001052 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054
Sascha Hauer009fda82009-04-15 01:32:23 +00001055static const struct net_device_ops fec_netdev_ops = {
1056 .ndo_open = fec_enet_open,
1057 .ndo_stop = fec_enet_close,
1058 .ndo_start_xmit = fec_enet_start_xmit,
1059 .ndo_set_multicast_list = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00001060 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00001061 .ndo_validate_addr = eth_validate_addr,
1062 .ndo_tx_timeout = fec_timeout,
1063 .ndo_set_mac_address = fec_set_mac_address,
Bryan Wue6b043d2010-03-31 02:10:44 +00001064 .ndo_do_ioctl = fec_enet_ioctl,
Sascha Hauer009fda82009-04-15 01:32:23 +00001065};
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 /*
1068 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00001069 *
1070 * index is only used in legacy code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 */
Steven King78abcb12009-10-20 18:51:37 -07001072static int fec_enet_init(struct net_device *dev, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
1074 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001075 struct bufdesc *cbd_base;
Rob Herring633e7532010-02-05 08:56:20 +00001076 struct bufdesc *bdp;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001077 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001079 /* Allocate memory for buffer descriptors. */
1080 cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
1081 GFP_KERNEL);
1082 if (!cbd_base) {
Greg Ungerer562d2f82005-11-07 14:09:50 +10001083 printk("FEC: allocate descriptor memory failed?\n");
1084 return -ENOMEM;
1085 }
1086
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10001087 spin_lock_init(&fep->hw_lock);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 fep->index = index;
Sascha Hauerf44d6302009-04-15 03:11:30 +00001090 fep->hwp = (void __iomem *)dev->base_addr;
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001091 fep->netdev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Sascha Haueread73182009-01-28 23:03:11 +00001093 /* Set the Ethernet address */
Greg Ungerer43be6362009-02-26 22:42:51 -08001094#ifdef CONFIG_M5272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 fec_get_mac(dev);
Sascha Haueread73182009-01-28 23:03:11 +00001096#else
1097 {
1098 unsigned long l;
Sascha Hauerf44d6302009-04-15 03:11:30 +00001099 l = readl(fep->hwp + FEC_ADDR_LOW);
Sascha Haueread73182009-01-28 23:03:11 +00001100 dev->dev_addr[0] = (unsigned char)((l & 0xFF000000) >> 24);
1101 dev->dev_addr[1] = (unsigned char)((l & 0x00FF0000) >> 16);
1102 dev->dev_addr[2] = (unsigned char)((l & 0x0000FF00) >> 8);
1103 dev->dev_addr[3] = (unsigned char)((l & 0x000000FF) >> 0);
Sascha Hauerf44d6302009-04-15 03:11:30 +00001104 l = readl(fep->hwp + FEC_ADDR_HIGH);
Sascha Haueread73182009-01-28 23:03:11 +00001105 dev->dev_addr[4] = (unsigned char)((l & 0xFF000000) >> 24);
1106 dev->dev_addr[5] = (unsigned char)((l & 0x00FF0000) >> 16);
1107 }
1108#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001110 /* Set receive and transmit descriptor base. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 fep->rx_bd_base = cbd_base;
1112 fep->tx_bd_base = cbd_base + RX_RING_SIZE;
1113
Sascha Hauer22f6b862009-04-15 01:32:18 +00001114 /* The FEC Ethernet specific entries in the device structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 dev->watchdog_timeo = TX_TIMEOUT;
Sascha Hauer009fda82009-04-15 01:32:23 +00001116 dev->netdev_ops = &fec_netdev_ops;
Bryan Wue6b043d2010-03-31 02:10:44 +00001117 dev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00001118
1119 /* Initialize the receive buffer descriptors. */
1120 bdp = fep->rx_bd_base;
1121 for (i = 0; i < RX_RING_SIZE; i++) {
1122
1123 /* Initialize the BD for every fragment in the page. */
1124 bdp->cbd_sc = 0;
1125 bdp++;
1126 }
1127
1128 /* Set the last buffer to wrap */
1129 bdp--;
1130 bdp->cbd_sc |= BD_SC_WRAP;
1131
1132 /* ...and the same for transmit */
1133 bdp = fep->tx_bd_base;
1134 for (i = 0; i < TX_RING_SIZE; i++) {
1135
1136 /* Initialize the BD for every fragment in the page. */
1137 bdp->cbd_sc = 0;
1138 bdp->cbd_bufaddr = 0;
1139 bdp++;
1140 }
1141
1142 /* Set the last buffer to wrap */
1143 bdp--;
1144 bdp->cbd_sc |= BD_SC_WRAP;
1145
Sascha Haueread73182009-01-28 23:03:11 +00001146 fec_restart(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return 0;
1149}
1150
1151/* This function is called to start or restart the FEC during a link
1152 * change. This only happens when switching between half and full
1153 * duplex.
1154 */
1155static void
1156fec_restart(struct net_device *dev, int duplex)
1157{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001158 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 int i;
1160
Sascha Hauerf44d6302009-04-15 03:11:30 +00001161 /* Whack a reset. We should wait for this. */
1162 writel(1, fep->hwp + FEC_ECNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 udelay(10);
1164
Sascha Hauerf44d6302009-04-15 03:11:30 +00001165 /* Clear any outstanding interrupt. */
1166 writel(0xffc00000, fep->hwp + FEC_IEVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Sascha Hauerf44d6302009-04-15 03:11:30 +00001168 /* Reset all multicast. */
1169 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1170 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Sascha Hauer4f1ceb42009-04-15 01:32:20 +00001171#ifndef CONFIG_M5272
1172 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1173 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1174#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Sascha Hauerf44d6302009-04-15 03:11:30 +00001176 /* Set maximum receive buffer size. */
1177 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Sascha Hauerf44d6302009-04-15 03:11:30 +00001179 /* Set receive and transmit descriptor base. */
1180 writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
Sascha Hauer2e285322009-04-15 01:32:16 +00001181 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc) * RX_RING_SIZE,
Sascha Hauerf44d6302009-04-15 03:11:30 +00001182 fep->hwp + FEC_X_DES_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
1185 fep->cur_rx = fep->rx_bd_base;
1186
Sascha Hauerf44d6302009-04-15 03:11:30 +00001187 /* Reset SKB transmit buffers. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 fep->skb_cur = fep->skb_dirty = 0;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001189 for (i = 0; i <= TX_RING_MOD_MASK; i++) {
1190 if (fep->tx_skbuff[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 dev_kfree_skb_any(fep->tx_skbuff[i]);
1192 fep->tx_skbuff[i] = NULL;
1193 }
1194 }
1195
Sascha Hauer22f6b862009-04-15 01:32:18 +00001196 /* Enable MII mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (duplex) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001198 /* MII enable / FD enable */
1199 writel(OPT_FRAME_SIZE | 0x04, fep->hwp + FEC_R_CNTRL);
1200 writel(0x04, fep->hwp + FEC_X_CNTRL);
Philippe De Muyterf909b1e2007-10-23 14:37:54 +10001201 } else {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001202 /* MII enable / No Rcv on Xmit */
1203 writel(OPT_FRAME_SIZE | 0x06, fep->hwp + FEC_R_CNTRL);
1204 writel(0x0, fep->hwp + FEC_X_CNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206 fep->full_duplex = duplex;
1207
Sascha Hauer22f6b862009-04-15 01:32:18 +00001208 /* Set MII speed */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001209 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Baruch Siach5eb32bd2010-05-24 00:36:13 -07001211#ifdef FEC_MIIGSK_ENR
1212 if (fep->phy_interface == PHY_INTERFACE_MODE_RMII) {
1213 /* disable the gasket and wait */
1214 writel(0, fep->hwp + FEC_MIIGSK_ENR);
1215 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1216 udelay(1);
1217
1218 /* configure the gasket: RMII, 50 MHz, no loopback, no echo */
1219 writel(1, fep->hwp + FEC_MIIGSK_CFGR);
1220
1221 /* re-enable the gasket */
1222 writel(2, fep->hwp + FEC_MIIGSK_ENR);
1223 }
1224#endif
1225
Sascha Hauer22f6b862009-04-15 01:32:18 +00001226 /* And last, enable the transmit and receive processing */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001227 writel(2, fep->hwp + FEC_ECNTRL);
1228 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Matt Waddel6b265292006-06-27 13:10:56 +10001229
Sascha Hauer22f6b862009-04-15 01:32:18 +00001230 /* Enable interrupts we wish to service */
Wolfram Sang4bee1f92010-07-21 02:51:13 +00001231 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
1234static void
1235fec_stop(struct net_device *dev)
1236{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001237 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Sascha Hauer22f6b862009-04-15 01:32:18 +00001239 /* We cannot expect a graceful transmit stop without link !!! */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001240 if (fep->link) {
1241 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
Philippe De Muyter677177c2006-06-27 13:05:33 +10001242 udelay(10);
Sascha Hauerf44d6302009-04-15 03:11:30 +00001243 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Philippe De Muyter677177c2006-06-27 13:05:33 +10001244 printk("fec_stop : Graceful transmit stop did not complete !\n");
Sascha Hauerf44d6302009-04-15 03:11:30 +00001245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Sascha Hauerf44d6302009-04-15 03:11:30 +00001247 /* Whack a reset. We should wait for this. */
1248 writel(1, fep->hwp + FEC_ECNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 udelay(10);
Sascha Hauerf44d6302009-04-15 03:11:30 +00001250 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Wolfram Sang4bee1f92010-07-21 02:51:13 +00001251 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}
1253
Sascha Haueread73182009-01-28 23:03:11 +00001254static int __devinit
1255fec_probe(struct platform_device *pdev)
1256{
1257 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07001258 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00001259 struct net_device *ndev;
1260 int i, irq, ret = 0;
1261 struct resource *r;
1262
1263 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1264 if (!r)
1265 return -ENXIO;
1266
1267 r = request_mem_region(r->start, resource_size(r), pdev->name);
1268 if (!r)
1269 return -EBUSY;
1270
1271 /* Init network device */
1272 ndev = alloc_etherdev(sizeof(struct fec_enet_private));
1273 if (!ndev)
1274 return -ENOMEM;
1275
1276 SET_NETDEV_DEV(ndev, &pdev->dev);
1277
1278 /* setup board info structure */
1279 fep = netdev_priv(ndev);
1280 memset(fep, 0, sizeof(*fep));
1281
1282 ndev->base_addr = (unsigned long)ioremap(r->start, resource_size(r));
Bryan Wue6b043d2010-03-31 02:10:44 +00001283 fep->pdev = pdev;
Sascha Haueread73182009-01-28 23:03:11 +00001284
1285 if (!ndev->base_addr) {
1286 ret = -ENOMEM;
1287 goto failed_ioremap;
1288 }
1289
1290 platform_set_drvdata(pdev, ndev);
1291
Baruch Siach5eb32bd2010-05-24 00:36:13 -07001292 pdata = pdev->dev.platform_data;
1293 if (pdata)
1294 fep->phy_interface = pdata->phy;
1295
Sascha Haueread73182009-01-28 23:03:11 +00001296 /* This device has up to three irqs on some platforms */
1297 for (i = 0; i < 3; i++) {
1298 irq = platform_get_irq(pdev, i);
1299 if (i && irq < 0)
1300 break;
1301 ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
1302 if (ret) {
1303 while (i >= 0) {
1304 irq = platform_get_irq(pdev, i);
1305 free_irq(irq, ndev);
1306 i--;
1307 }
1308 goto failed_irq;
1309 }
1310 }
1311
1312 fep->clk = clk_get(&pdev->dev, "fec_clk");
1313 if (IS_ERR(fep->clk)) {
1314 ret = PTR_ERR(fep->clk);
1315 goto failed_clk;
1316 }
1317 clk_enable(fep->clk);
1318
1319 ret = fec_enet_init(ndev, 0);
1320 if (ret)
1321 goto failed_init;
1322
Bryan Wue6b043d2010-03-31 02:10:44 +00001323 ret = fec_enet_mii_init(pdev);
1324 if (ret)
1325 goto failed_mii_init;
1326
Oskar Schirmer03c698c2010-10-07 02:30:30 +00001327 /* Carrier starts down, phylib will bring it up */
1328 netif_carrier_off(ndev);
1329
Sascha Haueread73182009-01-28 23:03:11 +00001330 ret = register_netdev(ndev);
1331 if (ret)
1332 goto failed_register;
1333
1334 return 0;
1335
1336failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00001337 fec_enet_mii_remove(fep);
1338failed_mii_init:
Sascha Haueread73182009-01-28 23:03:11 +00001339failed_init:
1340 clk_disable(fep->clk);
1341 clk_put(fep->clk);
1342failed_clk:
1343 for (i = 0; i < 3; i++) {
1344 irq = platform_get_irq(pdev, i);
1345 if (irq > 0)
1346 free_irq(irq, ndev);
1347 }
1348failed_irq:
1349 iounmap((void __iomem *)ndev->base_addr);
1350failed_ioremap:
1351 free_netdev(ndev);
1352
1353 return ret;
1354}
1355
1356static int __devexit
1357fec_drv_remove(struct platform_device *pdev)
1358{
1359 struct net_device *ndev = platform_get_drvdata(pdev);
1360 struct fec_enet_private *fep = netdev_priv(ndev);
1361
1362 platform_set_drvdata(pdev, NULL);
1363
1364 fec_stop(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001365 fec_enet_mii_remove(fep);
Sascha Haueread73182009-01-28 23:03:11 +00001366 clk_disable(fep->clk);
1367 clk_put(fep->clk);
1368 iounmap((void __iomem *)ndev->base_addr);
1369 unregister_netdev(ndev);
1370 free_netdev(ndev);
1371 return 0;
1372}
1373
Denis Kirjanov59d42892010-06-02 09:27:04 +00001374#ifdef CONFIG_PM
Sascha Haueread73182009-01-28 23:03:11 +00001375static int
Eric Benard87cad5c2010-06-18 04:19:54 +00001376fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00001377{
Eric Benard87cad5c2010-06-18 04:19:54 +00001378 struct net_device *ndev = dev_get_drvdata(dev);
Sascha Haueread73182009-01-28 23:03:11 +00001379 struct fec_enet_private *fep;
1380
1381 if (ndev) {
1382 fep = netdev_priv(ndev);
Eric Bénarde3fe8552010-06-02 06:13:34 -07001383 if (netif_running(ndev))
1384 fec_enet_close(ndev);
1385 clk_disable(fep->clk);
Sascha Haueread73182009-01-28 23:03:11 +00001386 }
1387 return 0;
1388}
1389
1390static int
Eric Benard87cad5c2010-06-18 04:19:54 +00001391fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00001392{
Eric Benard87cad5c2010-06-18 04:19:54 +00001393 struct net_device *ndev = dev_get_drvdata(dev);
Eric Bénarde3fe8552010-06-02 06:13:34 -07001394 struct fec_enet_private *fep;
Sascha Haueread73182009-01-28 23:03:11 +00001395
1396 if (ndev) {
Eric Bénarde3fe8552010-06-02 06:13:34 -07001397 fep = netdev_priv(ndev);
1398 clk_enable(fep->clk);
1399 if (netif_running(ndev))
1400 fec_enet_open(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001401 }
1402 return 0;
1403}
1404
Denis Kirjanov59d42892010-06-02 09:27:04 +00001405static const struct dev_pm_ops fec_pm_ops = {
1406 .suspend = fec_suspend,
1407 .resume = fec_resume,
1408 .freeze = fec_suspend,
1409 .thaw = fec_resume,
1410 .poweroff = fec_suspend,
1411 .restore = fec_resume,
1412};
Eric Benard87cad5c2010-06-18 04:19:54 +00001413#endif
Denis Kirjanov59d42892010-06-02 09:27:04 +00001414
Sascha Haueread73182009-01-28 23:03:11 +00001415static struct platform_driver fec_driver = {
1416 .driver = {
Eric Benard87cad5c2010-06-18 04:19:54 +00001417 .name = "fec",
1418 .owner = THIS_MODULE,
1419#ifdef CONFIG_PM
1420 .pm = &fec_pm_ops,
1421#endif
Sascha Haueread73182009-01-28 23:03:11 +00001422 },
Eric Benard87cad5c2010-06-18 04:19:54 +00001423 .probe = fec_probe,
1424 .remove = __devexit_p(fec_drv_remove),
Sascha Haueread73182009-01-28 23:03:11 +00001425};
1426
1427static int __init
1428fec_enet_module_init(void)
1429{
1430 printk(KERN_INFO "FEC Ethernet Driver\n");
1431
1432 return platform_driver_register(&fec_driver);
1433}
1434
1435static void __exit
1436fec_enet_cleanup(void)
1437{
1438 platform_driver_unregister(&fec_driver);
1439}
1440
1441module_exit(fec_enet_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442module_init(fec_enet_module_init);
1443
1444MODULE_LICENSE("GPL");