blob: 47f6b3b91423f8d94a4e2a70ebe663360df4aff9 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 int link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 int full_duplex;
Baruch Siach97b72e42010-07-11 21:12:51 +0000191 struct completion mdio_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192};
193
David Howells7d12e782006-10-05 14:55:46 +0100194static irqreturn_t fec_enet_interrupt(int irq, void * dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195static void fec_enet_tx(struct net_device *dev);
196static void fec_enet_rx(struct net_device *dev);
197static int fec_enet_close(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198static void fec_restart(struct net_device *dev, int duplex);
199static void fec_stop(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Bryan Wue6b043d2010-03-31 02:10:44 +0000201/* FEC MII MMFR bits definition */
202#define FEC_MMFR_ST (1 << 30)
203#define FEC_MMFR_OP_READ (2 << 28)
204#define FEC_MMFR_OP_WRITE (1 << 28)
205#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
206#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
207#define FEC_MMFR_TA (2 << 16)
208#define FEC_MMFR_DATA(v) (v & 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Baruch Siach97b72e42010-07-11 21:12:51 +0000210#define FEC_MII_TIMEOUT 1000 /* us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Sascha Hauer22f6b862009-04-15 01:32:18 +0000212/* Transmitter timeout */
213#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Denis Kirjanovc7621cb2010-06-02 09:15:47 +0000215static netdev_tx_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
217{
Sascha Hauerf44d6302009-04-15 03:11:30 +0000218 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000219 struct bufdesc *bdp;
Greg Ungerer9555b312009-08-06 17:58:18 +0000220 void *bufaddr;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000221 unsigned short status;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000222 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (!fep->link) {
225 /* Link is down or autonegotiation is in progress. */
Patrick McHardy5b548142009-06-12 06:22:29 +0000226 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000229 spin_lock_irqsave(&fep->hw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /* Fill in a Tx ring entry */
231 bdp = fep->cur_tx;
232
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000233 status = bdp->cbd_sc;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000234
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000235 if (status & BD_ENET_TX_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 /* Ooops. All transmit buffers are full. Bail out.
237 * This should not happen, since dev->tbusy should be set.
238 */
239 printk("%s: tx queue full!.\n", dev->name);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000240 spin_unlock_irqrestore(&fep->hw_lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +0000241 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Sascha Hauer22f6b862009-04-15 01:32:18 +0000244 /* Clear all of the status flags */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000245 status &= ~BD_ENET_TX_STATS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Sascha Hauer22f6b862009-04-15 01:32:18 +0000247 /* Set buffer length and buffer pointer */
Greg Ungerer9555b312009-08-06 17:58:18 +0000248 bufaddr = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 bdp->cbd_datlen = skb->len;
250
251 /*
Sascha Hauer22f6b862009-04-15 01:32:18 +0000252 * On some FEC implementations data must be aligned on
253 * 4-byte boundaries. Use bounce buffers to copy data
254 * and get it aligned. Ugh.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 */
Greg Ungerer9555b312009-08-06 17:58:18 +0000256 if (((unsigned long) bufaddr) & FEC_ALIGNMENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 unsigned int index;
258 index = bdp - fep->tx_bd_base;
Sascha Hauer6989f512009-01-28 23:03:06 +0000259 memcpy(fep->tx_bounce[index], (void *)skb->data, skb->len);
Greg Ungerer9555b312009-08-06 17:58:18 +0000260 bufaddr = fep->tx_bounce[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262
Sascha Hauer22f6b862009-04-15 01:32:18 +0000263 /* Save skb pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 fep->tx_skbuff[fep->skb_cur] = skb;
265
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700266 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 fep->skb_cur = (fep->skb_cur+1) & TX_RING_MOD_MASK;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 /* Push the data cache so the CPM does not get stale memory
270 * data.
271 */
Greg Ungerer9555b312009-08-06 17:58:18 +0000272 bdp->cbd_bufaddr = dma_map_single(&dev->dev, bufaddr,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000273 FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000275 /* Send it on its way. Tell FEC it's ready, interrupt when done,
276 * it's the last BD of the frame, and to put the CRC on the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000278 status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 | BD_ENET_TX_LAST | BD_ENET_TX_TC);
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000280 bdp->cbd_sc = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 /* Trigger transmission start */
Sascha Hauerf44d6302009-04-15 03:11:30 +0000283 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Sascha Hauer22f6b862009-04-15 01:32:18 +0000285 /* If this was the last BD in the ring, start at the beginning again. */
286 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 bdp = fep->tx_bd_base;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000288 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 bdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 if (bdp == fep->dirty_tx) {
292 fep->tx_full = 1;
293 netif_stop_queue(dev);
294 }
295
Sascha Hauer2e285322009-04-15 01:32:16 +0000296 fep->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000298 spin_unlock_irqrestore(&fep->hw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Patrick McHardy6ed10652009-06-23 06:03:08 +0000300 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
303static void
304fec_timeout(struct net_device *dev)
305{
306 struct fec_enet_private *fep = netdev_priv(dev);
307
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700308 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000310 fec_restart(dev, fep->full_duplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 netif_wake_queue(dev);
312}
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100315fec_enet_interrupt(int irq, void * dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 struct net_device *dev = dev_id;
Sascha Hauerf44d6302009-04-15 03:11:30 +0000318 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 uint int_events;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000320 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000322 do {
Sascha Hauerf44d6302009-04-15 03:11:30 +0000323 int_events = readl(fep->hwp + FEC_IEVENT);
324 writel(int_events, fep->hwp + FEC_IEVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (int_events & FEC_ENET_RXF) {
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000327 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 fec_enet_rx(dev);
329 }
330
331 /* Transmit OK, or non-fatal error. Update the buffer
Sascha Hauerf44d6302009-04-15 03:11:30 +0000332 * descriptors. FEC handles all errors, we just discover
333 * them as part of the transmit process.
334 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (int_events & FEC_ENET_TXF) {
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000336 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 fec_enet_tx(dev);
338 }
Baruch Siach97b72e42010-07-11 21:12:51 +0000339
340 if (int_events & FEC_ENET_MII) {
341 ret = IRQ_HANDLED;
342 complete(&fep->mdio_done);
343 }
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000344 } while (int_events);
345
346 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349
350static void
351fec_enet_tx(struct net_device *dev)
352{
353 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +0000354 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000355 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 struct sk_buff *skb;
357
358 fep = netdev_priv(dev);
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000359 spin_lock(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 bdp = fep->dirty_tx;
361
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000362 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000363 if (bdp == fep->cur_tx && fep->tx_full == 0)
364 break;
365
366 dma_unmap_single(&dev->dev, bdp->cbd_bufaddr, FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
367 bdp->cbd_bufaddr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 skb = fep->tx_skbuff[fep->skb_dirty];
370 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000371 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 BD_ENET_TX_RL | BD_ENET_TX_UN |
373 BD_ENET_TX_CSL)) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700374 dev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000375 if (status & BD_ENET_TX_HB) /* No heartbeat */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700376 dev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000377 if (status & BD_ENET_TX_LC) /* Late collision */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700378 dev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000379 if (status & BD_ENET_TX_RL) /* Retrans limit */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700380 dev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000381 if (status & BD_ENET_TX_UN) /* Underrun */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700382 dev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000383 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700384 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 } else {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700386 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000389 if (status & BD_ENET_TX_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 printk("HEY! Enet xmit interrupt and TX_READY.\n");
Sascha Hauer22f6b862009-04-15 01:32:18 +0000391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /* Deferred means some collisions occurred during transmit,
393 * but we eventually sent the packet OK.
394 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000395 if (status & BD_ENET_TX_DEF)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700396 dev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400397
Sascha Hauer22f6b862009-04-15 01:32:18 +0000398 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 dev_kfree_skb_any(skb);
400 fep->tx_skbuff[fep->skb_dirty] = NULL;
401 fep->skb_dirty = (fep->skb_dirty + 1) & TX_RING_MOD_MASK;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400402
Sascha Hauer22f6b862009-04-15 01:32:18 +0000403 /* Update pointer to next buffer descriptor to be transmitted */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000404 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 bdp = fep->tx_bd_base;
406 else
407 bdp++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400408
Sascha Hauer22f6b862009-04-15 01:32:18 +0000409 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 */
411 if (fep->tx_full) {
412 fep->tx_full = 0;
413 if (netif_queue_stopped(dev))
414 netif_wake_queue(dev);
415 }
416 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000417 fep->dirty_tx = bdp;
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000418 spin_unlock(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
421
422/* During a receive, the cur_rx points to the current incoming buffer.
423 * When we update through the ring, if the next incoming buffer has
424 * not been given to the system, we just set the empty indicator,
425 * effectively tossing the packet.
426 */
427static void
428fec_enet_rx(struct net_device *dev)
429{
Sascha Hauerf44d6302009-04-15 03:11:30 +0000430 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000431 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000432 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 struct sk_buff *skb;
434 ushort pkt_len;
435 __u8 *data;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400436
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000437#ifdef CONFIG_M532x
438 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400439#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000441 spin_lock(&fep->hw_lock);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /* First, grab all of the stats for the incoming packet.
444 * These get messed up if we get called due to a busy condition.
445 */
446 bdp = fep->cur_rx;
447
Sascha Hauer22f6b862009-04-15 01:32:18 +0000448 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Sascha Hauer22f6b862009-04-15 01:32:18 +0000450 /* Since we have allocated space to hold a complete frame,
451 * the last indicator should be set.
452 */
453 if ((status & BD_ENET_RX_LAST) == 0)
454 printk("FEC ENET: rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Sascha Hauer22f6b862009-04-15 01:32:18 +0000456 if (!fep->opened)
457 goto rx_processing_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Sascha Hauer22f6b862009-04-15 01:32:18 +0000459 /* Check for errors. */
460 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Sascha Hauer22f6b862009-04-15 01:32:18 +0000462 dev->stats.rx_errors++;
463 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
464 /* Frame too long or too short. */
465 dev->stats.rx_length_errors++;
466 }
467 if (status & BD_ENET_RX_NO) /* Frame alignment */
468 dev->stats.rx_frame_errors++;
469 if (status & BD_ENET_RX_CR) /* CRC Error */
470 dev->stats.rx_crc_errors++;
471 if (status & BD_ENET_RX_OV) /* FIFO overrun */
472 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
Sascha Hauer22f6b862009-04-15 01:32:18 +0000474
475 /* Report late collisions as a frame error.
476 * On this error, the BD is closed, but we don't know what we
477 * have in the buffer. So, just drop this frame on the floor.
478 */
479 if (status & BD_ENET_RX_CL) {
480 dev->stats.rx_errors++;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700481 dev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000482 goto rx_processing_done;
483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Sascha Hauer22f6b862009-04-15 01:32:18 +0000485 /* Process the incoming frame. */
486 dev->stats.rx_packets++;
487 pkt_len = bdp->cbd_datlen;
488 dev->stats.rx_bytes += pkt_len;
489 data = (__u8*)__va(bdp->cbd_bufaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000491 dma_unmap_single(NULL, bdp->cbd_bufaddr, bdp->cbd_datlen,
492 DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +0000493
Sascha Hauer22f6b862009-04-15 01:32:18 +0000494 /* This does 16 byte alignment, exactly what we need.
495 * The packet length includes FCS, but we don't want to
496 * include that when passing upstream as it messes up
497 * bridging applications.
498 */
Sascha Hauer85498892009-04-15 01:32:21 +0000499 skb = dev_alloc_skb(pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Sascha Hauer85498892009-04-15 01:32:21 +0000501 if (unlikely(!skb)) {
Sascha Hauer22f6b862009-04-15 01:32:18 +0000502 printk("%s: Memory squeeze, dropping packet.\n",
503 dev->name);
504 dev->stats.rx_dropped++;
505 } else {
Sascha Hauer85498892009-04-15 01:32:21 +0000506 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000507 skb_put(skb, pkt_len - 4); /* Make room */
508 skb_copy_to_linear_data(skb, data, pkt_len - 4);
509 skb->protocol = eth_type_trans(skb, dev);
510 netif_rx(skb);
511 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000512
513 bdp->cbd_bufaddr = dma_map_single(NULL, data, bdp->cbd_datlen,
514 DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000515rx_processing_done:
516 /* Clear the status flags for this buffer */
517 status &= ~BD_ENET_RX_STATS;
518
519 /* Mark the buffer empty */
520 status |= BD_ENET_RX_EMPTY;
521 bdp->cbd_sc = status;
522
523 /* Update BD pointer to next entry */
524 if (status & BD_ENET_RX_WRAP)
525 bdp = fep->rx_bd_base;
526 else
527 bdp++;
528 /* Doing this here will keep the FEC running while we process
529 * incoming frames. On a heavily loaded network, we should be
530 * able to keep up at the expense of system resources.
531 */
532 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000534 fep->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000536 spin_unlock(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539/* ------------------------------------------------------------------------- */
Greg Ungerer43be6362009-02-26 22:42:51 -0800540#ifdef CONFIG_M5272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541static void __inline__ fec_get_mac(struct net_device *dev)
542{
543 struct fec_enet_private *fep = netdev_priv(dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000544 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000546 if (FEC_FLASHMAC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /*
548 * Get MAC address from FLASH.
549 * If it is all 1's or 0's, use the default.
550 */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000551 iap = (unsigned char *)FEC_FLASHMAC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if ((iap[0] == 0) && (iap[1] == 0) && (iap[2] == 0) &&
553 (iap[3] == 0) && (iap[4] == 0) && (iap[5] == 0))
554 iap = fec_mac_default;
555 if ((iap[0] == 0xff) && (iap[1] == 0xff) && (iap[2] == 0xff) &&
556 (iap[3] == 0xff) && (iap[4] == 0xff) && (iap[5] == 0xff))
557 iap = fec_mac_default;
558 } else {
Sascha Hauerf44d6302009-04-15 03:11:30 +0000559 *((unsigned long *) &tmpaddr[0]) = readl(fep->hwp + FEC_ADDR_LOW);
560 *((unsigned short *) &tmpaddr[4]) = (readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 iap = &tmpaddr[0];
562 }
563
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000564 memcpy(dev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 /* Adjust MAC if using default MAC address */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000567 if (iap == fec_mac_default)
Shawn Guo8649a232011-01-05 21:13:10 +0000568 dev->dev_addr[ETH_ALEN-1] = fec_mac_default[ETH_ALEN-1] + fep->pdev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570#endif
571
572/* ------------------------------------------------------------------------- */
573
Bryan Wue6b043d2010-03-31 02:10:44 +0000574/*
575 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 */
Bryan Wue6b043d2010-03-31 02:10:44 +0000577static void fec_enet_adjust_link(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Bryan Wue6b043d2010-03-31 02:10:44 +0000579 struct fec_enet_private *fep = netdev_priv(dev);
580 struct phy_device *phy_dev = fep->phy_dev;
581 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Bryan Wue6b043d2010-03-31 02:10:44 +0000583 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Bryan Wue6b043d2010-03-31 02:10:44 +0000585 spin_lock_irqsave(&fep->hw_lock, flags);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400586
Bryan Wue6b043d2010-03-31 02:10:44 +0000587 /* Prevent a state halted on mii error */
588 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
589 phy_dev->state = PHY_RESUMING;
590 goto spin_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
Bryan Wue6b043d2010-03-31 02:10:44 +0000592
593 /* Duplex link change */
594 if (phy_dev->link) {
595 if (fep->full_duplex != phy_dev->duplex) {
596 fec_restart(dev, phy_dev->duplex);
597 status_change = 1;
598 }
599 }
600
601 /* Link on or off change */
602 if (phy_dev->link != fep->link) {
603 fep->link = phy_dev->link;
604 if (phy_dev->link)
605 fec_restart(dev, phy_dev->duplex);
606 else
607 fec_stop(dev);
608 status_change = 1;
609 }
610
611spin_unlock:
612 spin_unlock_irqrestore(&fep->hw_lock, flags);
613
614 if (status_change)
615 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Bryan Wue6b043d2010-03-31 02:10:44 +0000618static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Bryan Wue6b043d2010-03-31 02:10:44 +0000620 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +0000621 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +0000622
623 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +0000624 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +0000625
626 /* start a read op */
627 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
628 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
629 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
630
631 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +0000632 time_left = wait_for_completion_timeout(&fep->mdio_done,
633 usecs_to_jiffies(FEC_MII_TIMEOUT));
634 if (time_left == 0) {
635 fep->mii_timeout = 1;
636 printk(KERN_ERR "FEC: MDIO read timeout\n");
637 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +0000638 }
639
640 /* return value */
641 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
642}
643
644static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
645 u16 value)
646{
647 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +0000648 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +0000649
650 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +0000651 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +0000652
Shawn Guo862f0982011-01-05 21:13:09 +0000653 /* start a write op */
654 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +0000655 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
656 FEC_MMFR_TA | FEC_MMFR_DATA(value),
657 fep->hwp + FEC_MII_DATA);
658
659 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +0000660 time_left = wait_for_completion_timeout(&fep->mdio_done,
661 usecs_to_jiffies(FEC_MII_TIMEOUT));
662 if (time_left == 0) {
663 fep->mii_timeout = 1;
664 printk(KERN_ERR "FEC: MDIO write timeout\n");
665 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +0000666 }
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;
Greg Ungerer6fcc0402010-10-11 21:03:05 +0000680 char mdio_bus_id[MII_BUS_ID_SIZE];
681 char phy_name[MII_BUS_ID_SIZE + 3];
682 int phy_id;
Bryan Wue6b043d2010-03-31 02:10:44 +0000683
Bryan Wu418bd0d2010-05-28 03:40:39 -0700684 fep->phy_dev = NULL;
685
Greg Ungerer6fcc0402010-10-11 21:03:05 +0000686 /* check for attached phy */
687 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
688 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
689 continue;
690 if (fep->mii_bus->phy_map[phy_id] == NULL)
691 continue;
692 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
693 continue;
694 strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
695 break;
Bryan Wue6b043d2010-03-31 02:10:44 +0000696 }
697
Greg Ungerer6fcc0402010-10-11 21:03:05 +0000698 if (phy_id >= PHY_MAX_ADDR) {
699 printk(KERN_INFO "%s: no PHY, assuming direct connection "
700 "to switch\n", dev->name);
701 strncpy(mdio_bus_id, "0", MII_BUS_ID_SIZE);
702 phy_id = 0;
703 }
704
705 snprintf(phy_name, MII_BUS_ID_SIZE, PHY_ID_FMT, mdio_bus_id, phy_id);
706 phy_dev = phy_connect(dev, phy_name, &fec_enet_adjust_link, 0,
707 PHY_INTERFACE_MODE_MII);
708 if (IS_ERR(phy_dev)) {
709 printk(KERN_ERR "%s: could not attach to PHY\n", dev->name);
710 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +0000711 }
712
713 /* mask with MAC supported features */
714 phy_dev->supported &= PHY_BASIC_FEATURES;
715 phy_dev->advertising = phy_dev->supported;
716
717 fep->phy_dev = phy_dev;
718 fep->link = 0;
719 fep->full_duplex = 0;
720
Bryan Wu418bd0d2010-05-28 03:40:39 -0700721 printk(KERN_INFO "%s: Freescale FEC PHY driver [%s] "
722 "(mii_bus:phy_addr=%s, irq=%d)\n", dev->name,
723 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
724 fep->phy_dev->irq);
725
Bryan Wue6b043d2010-03-31 02:10:44 +0000726 return 0;
727}
728
729static int fec_enet_mii_init(struct platform_device *pdev)
730{
731 struct net_device *dev = platform_get_drvdata(pdev);
732 struct fec_enet_private *fep = netdev_priv(dev);
733 int err = -ENXIO, i;
734
735 fep->mii_timeout = 0;
736
737 /*
738 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
739 */
740 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk), 5000000) << 1;
741 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
742
743 fep->mii_bus = mdiobus_alloc();
744 if (fep->mii_bus == NULL) {
745 err = -ENOMEM;
746 goto err_out;
747 }
748
749 fep->mii_bus->name = "fec_enet_mii_bus";
750 fep->mii_bus->read = fec_enet_mdio_read;
751 fep->mii_bus->write = fec_enet_mdio_write;
752 fep->mii_bus->reset = fec_enet_mdio_reset;
Greg Ungerer6fcc0402010-10-11 21:03:05 +0000753 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%x", pdev->id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +0000754 fep->mii_bus->priv = fep;
755 fep->mii_bus->parent = &pdev->dev;
756
757 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
758 if (!fep->mii_bus->irq) {
759 err = -ENOMEM;
760 goto err_out_free_mdiobus;
761 }
762
763 for (i = 0; i < PHY_MAX_ADDR; i++)
764 fep->mii_bus->irq[i] = PHY_POLL;
765
766 platform_set_drvdata(dev, fep->mii_bus);
767
768 if (mdiobus_register(fep->mii_bus))
769 goto err_out_free_mdio_irq;
770
Bryan Wue6b043d2010-03-31 02:10:44 +0000771 return 0;
772
Bryan Wue6b043d2010-03-31 02:10:44 +0000773err_out_free_mdio_irq:
774 kfree(fep->mii_bus->irq);
775err_out_free_mdiobus:
776 mdiobus_free(fep->mii_bus);
777err_out:
778 return err;
779}
780
781static void fec_enet_mii_remove(struct fec_enet_private *fep)
782{
783 if (fep->phy_dev)
784 phy_disconnect(fep->phy_dev);
785 mdiobus_unregister(fep->mii_bus);
786 kfree(fep->mii_bus->irq);
787 mdiobus_free(fep->mii_bus);
788}
789
790static int fec_enet_get_settings(struct net_device *dev,
791 struct ethtool_cmd *cmd)
792{
793 struct fec_enet_private *fep = netdev_priv(dev);
794 struct phy_device *phydev = fep->phy_dev;
795
796 if (!phydev)
797 return -ENODEV;
798
799 return phy_ethtool_gset(phydev, cmd);
800}
801
802static int fec_enet_set_settings(struct net_device *dev,
803 struct ethtool_cmd *cmd)
804{
805 struct fec_enet_private *fep = netdev_priv(dev);
806 struct phy_device *phydev = fep->phy_dev;
807
808 if (!phydev)
809 return -ENODEV;
810
811 return phy_ethtool_sset(phydev, cmd);
812}
813
814static void fec_enet_get_drvinfo(struct net_device *dev,
815 struct ethtool_drvinfo *info)
816{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 struct fec_enet_private *fep = netdev_priv(dev);
818
Bryan Wue6b043d2010-03-31 02:10:44 +0000819 strcpy(info->driver, fep->pdev->dev.driver->name);
820 strcpy(info->version, "Revision: 1.0");
821 strcpy(info->bus_info, dev_name(&dev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
Bryan Wue6b043d2010-03-31 02:10:44 +0000823
824static struct ethtool_ops fec_enet_ethtool_ops = {
825 .get_settings = fec_enet_get_settings,
826 .set_settings = fec_enet_set_settings,
827 .get_drvinfo = fec_enet_get_drvinfo,
828 .get_link = ethtool_op_get_link,
829};
830
831static int fec_enet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
832{
833 struct fec_enet_private *fep = netdev_priv(dev);
834 struct phy_device *phydev = fep->phy_dev;
835
836 if (!netif_running(dev))
837 return -EINVAL;
838
839 if (!phydev)
840 return -ENODEV;
841
Richard Cochran28b04112010-07-17 08:48:55 +0000842 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +0000843}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000845static void fec_enet_free_buffers(struct net_device *dev)
846{
847 struct fec_enet_private *fep = netdev_priv(dev);
848 int i;
849 struct sk_buff *skb;
850 struct bufdesc *bdp;
851
852 bdp = fep->rx_bd_base;
853 for (i = 0; i < RX_RING_SIZE; i++) {
854 skb = fep->rx_skbuff[i];
855
856 if (bdp->cbd_bufaddr)
857 dma_unmap_single(&dev->dev, bdp->cbd_bufaddr,
858 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
859 if (skb)
860 dev_kfree_skb(skb);
861 bdp++;
862 }
863
864 bdp = fep->tx_bd_base;
865 for (i = 0; i < TX_RING_SIZE; i++)
866 kfree(fep->tx_bounce[i]);
867}
868
869static int fec_enet_alloc_buffers(struct net_device *dev)
870{
871 struct fec_enet_private *fep = netdev_priv(dev);
872 int i;
873 struct sk_buff *skb;
874 struct bufdesc *bdp;
875
876 bdp = fep->rx_bd_base;
877 for (i = 0; i < RX_RING_SIZE; i++) {
878 skb = dev_alloc_skb(FEC_ENET_RX_FRSIZE);
879 if (!skb) {
880 fec_enet_free_buffers(dev);
881 return -ENOMEM;
882 }
883 fep->rx_skbuff[i] = skb;
884
885 bdp->cbd_bufaddr = dma_map_single(&dev->dev, skb->data,
886 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
887 bdp->cbd_sc = BD_ENET_RX_EMPTY;
888 bdp++;
889 }
890
891 /* Set the last buffer to wrap. */
892 bdp--;
893 bdp->cbd_sc |= BD_SC_WRAP;
894
895 bdp = fep->tx_bd_base;
896 for (i = 0; i < TX_RING_SIZE; i++) {
897 fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
898
899 bdp->cbd_sc = 0;
900 bdp->cbd_bufaddr = 0;
901 bdp++;
902 }
903
904 /* Set the last buffer to wrap. */
905 bdp--;
906 bdp->cbd_sc |= BD_SC_WRAP;
907
908 return 0;
909}
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911static int
912fec_enet_open(struct net_device *dev)
913{
914 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000915 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 /* I should reset the ring buffers here, but I don't yet know
918 * a simple way to do that.
919 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000921 ret = fec_enet_alloc_buffers(dev);
922 if (ret)
923 return ret;
924
Bryan Wu418bd0d2010-05-28 03:40:39 -0700925 /* Probe and connect to PHY when open the interface */
926 ret = fec_enet_mii_probe(dev);
927 if (ret) {
928 fec_enet_free_buffers(dev);
929 return ret;
930 }
Bryan Wue6b043d2010-03-31 02:10:44 +0000931 phy_start(fep->phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 netif_start_queue(dev);
933 fep->opened = 1;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000934 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
937static int
938fec_enet_close(struct net_device *dev)
939{
940 struct fec_enet_private *fep = netdev_priv(dev);
941
Sascha Hauer22f6b862009-04-15 01:32:18 +0000942 /* Don't know what to do yet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 fep->opened = 0;
944 netif_stop_queue(dev);
945 fec_stop(dev);
946
Bryan Wu418bd0d2010-05-28 03:40:39 -0700947 if (fep->phy_dev)
948 phy_disconnect(fep->phy_dev);
949
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000950 fec_enet_free_buffers(dev);
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return 0;
953}
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955/* Set or clear the multicast filter for this adaptor.
956 * Skeleton taken from sunlance driver.
957 * The CPM Ethernet implementation allows Multicast as well as individual
958 * MAC address filtering. Some of the drivers check to make sure it is
959 * a group multicast address, and discard those that are not. I guess I
960 * will do the same for now, but just remove the test if you want
961 * individual filtering as well (do the upper net layers want or support
962 * this kind of feature?).
963 */
964
965#define HASH_BITS 6 /* #bits in hash */
966#define CRC32_POLY 0xEDB88320
967
968static void set_multicast_list(struct net_device *dev)
969{
Sascha Hauerf44d6302009-04-15 03:11:30 +0000970 struct fec_enet_private *fep = netdev_priv(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000971 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +0000972 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 unsigned char hash;
974
Sascha Hauer22f6b862009-04-15 01:32:18 +0000975 if (dev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +0000976 tmp = readl(fep->hwp + FEC_R_CNTRL);
977 tmp |= 0x8;
978 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +0000979 return;
980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Sascha Hauer4e831832009-04-15 01:32:19 +0000982 tmp = readl(fep->hwp + FEC_R_CNTRL);
983 tmp &= ~0x8;
984 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400985
Sascha Hauer4e831832009-04-15 01:32:19 +0000986 if (dev->flags & IFF_ALLMULTI) {
987 /* Catch all multicast addresses, so set the
988 * filter to all 1's
989 */
990 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
991 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Sascha Hauer4e831832009-04-15 01:32:19 +0000993 return;
994 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400995
Sascha Hauer4e831832009-04-15 01:32:19 +0000996 /* Clear filter and add the addresses in hash register
997 */
998 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
999 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Jiri Pirko22bedad32010-04-01 21:22:57 +00001001 netdev_for_each_mc_addr(ha, dev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00001002 /* Only support group multicast for now */
Jiri Pirko22bedad32010-04-01 21:22:57 +00001003 if (!(ha->addr[0] & 1))
Sascha Hauer4e831832009-04-15 01:32:19 +00001004 continue;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001005
Sascha Hauer4e831832009-04-15 01:32:19 +00001006 /* calculate crc32 value of mac address */
1007 crc = 0xffffffff;
1008
Jiri Pirko22bedad32010-04-01 21:22:57 +00001009 for (i = 0; i < dev->addr_len; i++) {
1010 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00001011 for (bit = 0; bit < 8; bit++, data >>= 1) {
1012 crc = (crc >> 1) ^
1013 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015 }
Sascha Hauer4e831832009-04-15 01:32:19 +00001016
1017 /* only upper 6 bits (HASH_BITS) are used
1018 * which point to specific bit in he hash registers
1019 */
1020 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
1021
1022 if (hash > 31) {
1023 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1024 tmp |= 1 << (hash - 32);
1025 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1026 } else {
1027 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1028 tmp |= 1 << hash;
1029 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032}
1033
Sascha Hauer22f6b862009-04-15 01:32:18 +00001034/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00001035static int
1036fec_set_mac_address(struct net_device *dev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001038 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer009fda82009-04-15 01:32:23 +00001039 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Sascha Hauer009fda82009-04-15 01:32:23 +00001041 if (!is_valid_ether_addr(addr->sa_data))
1042 return -EADDRNOTAVAIL;
1043
1044 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1045
Sascha Hauerf44d6302009-04-15 03:11:30 +00001046 writel(dev->dev_addr[3] | (dev->dev_addr[2] << 8) |
1047 (dev->dev_addr[1] << 16) | (dev->dev_addr[0] << 24),
1048 fep->hwp + FEC_ADDR_LOW);
1049 writel((dev->dev_addr[5] << 16) | (dev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07001050 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00001051 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
Sascha Hauer009fda82009-04-15 01:32:23 +00001054static const struct net_device_ops fec_netdev_ops = {
1055 .ndo_open = fec_enet_open,
1056 .ndo_stop = fec_enet_close,
1057 .ndo_start_xmit = fec_enet_start_xmit,
1058 .ndo_set_multicast_list = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00001059 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00001060 .ndo_validate_addr = eth_validate_addr,
1061 .ndo_tx_timeout = fec_timeout,
1062 .ndo_set_mac_address = fec_set_mac_address,
Bryan Wue6b043d2010-03-31 02:10:44 +00001063 .ndo_do_ioctl = fec_enet_ioctl,
Sascha Hauer009fda82009-04-15 01:32:23 +00001064};
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 /*
1067 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00001068 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 */
Shawn Guo8649a232011-01-05 21:13:10 +00001070static int fec_enet_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
1072 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001073 struct bufdesc *cbd_base;
Rob Herring633e7532010-02-05 08:56:20 +00001074 struct bufdesc *bdp;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001075 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001077 /* Allocate memory for buffer descriptors. */
1078 cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
1079 GFP_KERNEL);
1080 if (!cbd_base) {
Greg Ungerer562d2f82005-11-07 14:09:50 +10001081 printk("FEC: allocate descriptor memory failed?\n");
1082 return -ENOMEM;
1083 }
1084
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10001085 spin_lock_init(&fep->hw_lock);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10001086
Sascha Hauerf44d6302009-04-15 03:11:30 +00001087 fep->hwp = (void __iomem *)dev->base_addr;
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001088 fep->netdev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Sascha Haueread73182009-01-28 23:03:11 +00001090 /* Set the Ethernet address */
Greg Ungerer43be6362009-02-26 22:42:51 -08001091#ifdef CONFIG_M5272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 fec_get_mac(dev);
Sascha Haueread73182009-01-28 23:03:11 +00001093#else
1094 {
1095 unsigned long l;
Sascha Hauerf44d6302009-04-15 03:11:30 +00001096 l = readl(fep->hwp + FEC_ADDR_LOW);
Sascha Haueread73182009-01-28 23:03:11 +00001097 dev->dev_addr[0] = (unsigned char)((l & 0xFF000000) >> 24);
1098 dev->dev_addr[1] = (unsigned char)((l & 0x00FF0000) >> 16);
1099 dev->dev_addr[2] = (unsigned char)((l & 0x0000FF00) >> 8);
1100 dev->dev_addr[3] = (unsigned char)((l & 0x000000FF) >> 0);
Sascha Hauerf44d6302009-04-15 03:11:30 +00001101 l = readl(fep->hwp + FEC_ADDR_HIGH);
Sascha Haueread73182009-01-28 23:03:11 +00001102 dev->dev_addr[4] = (unsigned char)((l & 0xFF000000) >> 24);
1103 dev->dev_addr[5] = (unsigned char)((l & 0x00FF0000) >> 16);
1104 }
1105#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001107 /* Set receive and transmit descriptor base. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 fep->rx_bd_base = cbd_base;
1109 fep->tx_bd_base = cbd_base + RX_RING_SIZE;
1110
Sascha Hauer22f6b862009-04-15 01:32:18 +00001111 /* The FEC Ethernet specific entries in the device structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 dev->watchdog_timeo = TX_TIMEOUT;
Sascha Hauer009fda82009-04-15 01:32:23 +00001113 dev->netdev_ops = &fec_netdev_ops;
Bryan Wue6b043d2010-03-31 02:10:44 +00001114 dev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00001115
1116 /* Initialize the receive buffer descriptors. */
1117 bdp = fep->rx_bd_base;
1118 for (i = 0; i < RX_RING_SIZE; i++) {
1119
1120 /* Initialize the BD for every fragment in the page. */
1121 bdp->cbd_sc = 0;
1122 bdp++;
1123 }
1124
1125 /* Set the last buffer to wrap */
1126 bdp--;
1127 bdp->cbd_sc |= BD_SC_WRAP;
1128
1129 /* ...and the same for transmit */
1130 bdp = fep->tx_bd_base;
1131 for (i = 0; i < TX_RING_SIZE; i++) {
1132
1133 /* Initialize the BD for every fragment in the page. */
1134 bdp->cbd_sc = 0;
1135 bdp->cbd_bufaddr = 0;
1136 bdp++;
1137 }
1138
1139 /* Set the last buffer to wrap */
1140 bdp--;
1141 bdp->cbd_sc |= BD_SC_WRAP;
1142
Sascha Haueread73182009-01-28 23:03:11 +00001143 fec_restart(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return 0;
1146}
1147
1148/* This function is called to start or restart the FEC during a link
1149 * change. This only happens when switching between half and full
1150 * duplex.
1151 */
1152static void
1153fec_restart(struct net_device *dev, int duplex)
1154{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001155 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 int i;
1157
Sascha Hauerf44d6302009-04-15 03:11:30 +00001158 /* Whack a reset. We should wait for this. */
1159 writel(1, fep->hwp + FEC_ECNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 udelay(10);
1161
Sascha Hauerf44d6302009-04-15 03:11:30 +00001162 /* Clear any outstanding interrupt. */
1163 writel(0xffc00000, fep->hwp + FEC_IEVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Sascha Hauerf44d6302009-04-15 03:11:30 +00001165 /* Reset all multicast. */
1166 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1167 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Sascha Hauer4f1ceb42009-04-15 01:32:20 +00001168#ifndef CONFIG_M5272
1169 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1170 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1171#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Sascha Hauerf44d6302009-04-15 03:11:30 +00001173 /* Set maximum receive buffer size. */
1174 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Sascha Hauerf44d6302009-04-15 03:11:30 +00001176 /* Set receive and transmit descriptor base. */
1177 writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
Sascha Hauer2e285322009-04-15 01:32:16 +00001178 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc) * RX_RING_SIZE,
Sascha Hauerf44d6302009-04-15 03:11:30 +00001179 fep->hwp + FEC_X_DES_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
1182 fep->cur_rx = fep->rx_bd_base;
1183
Sascha Hauerf44d6302009-04-15 03:11:30 +00001184 /* Reset SKB transmit buffers. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 fep->skb_cur = fep->skb_dirty = 0;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001186 for (i = 0; i <= TX_RING_MOD_MASK; i++) {
1187 if (fep->tx_skbuff[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 dev_kfree_skb_any(fep->tx_skbuff[i]);
1189 fep->tx_skbuff[i] = NULL;
1190 }
1191 }
1192
Sascha Hauer22f6b862009-04-15 01:32:18 +00001193 /* Enable MII mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (duplex) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001195 /* MII enable / FD enable */
1196 writel(OPT_FRAME_SIZE | 0x04, fep->hwp + FEC_R_CNTRL);
1197 writel(0x04, fep->hwp + FEC_X_CNTRL);
Philippe De Muyterf909b1e2007-10-23 14:37:54 +10001198 } else {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001199 /* MII enable / No Rcv on Xmit */
1200 writel(OPT_FRAME_SIZE | 0x06, fep->hwp + FEC_R_CNTRL);
1201 writel(0x0, fep->hwp + FEC_X_CNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
1203 fep->full_duplex = duplex;
1204
Sascha Hauer22f6b862009-04-15 01:32:18 +00001205 /* Set MII speed */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001206 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Baruch Siach5eb32bd2010-05-24 00:36:13 -07001208#ifdef FEC_MIIGSK_ENR
1209 if (fep->phy_interface == PHY_INTERFACE_MODE_RMII) {
1210 /* disable the gasket and wait */
1211 writel(0, fep->hwp + FEC_MIIGSK_ENR);
1212 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1213 udelay(1);
1214
1215 /* configure the gasket: RMII, 50 MHz, no loopback, no echo */
1216 writel(1, fep->hwp + FEC_MIIGSK_CFGR);
1217
1218 /* re-enable the gasket */
1219 writel(2, fep->hwp + FEC_MIIGSK_ENR);
1220 }
1221#endif
1222
Sascha Hauer22f6b862009-04-15 01:32:18 +00001223 /* And last, enable the transmit and receive processing */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001224 writel(2, fep->hwp + FEC_ECNTRL);
1225 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Matt Waddel6b265292006-06-27 13:10:56 +10001226
Sascha Hauer22f6b862009-04-15 01:32:18 +00001227 /* Enable interrupts we wish to service */
Wolfram Sang4bee1f92010-07-21 02:51:13 +00001228 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
1231static void
1232fec_stop(struct net_device *dev)
1233{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001234 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Sascha Hauer22f6b862009-04-15 01:32:18 +00001236 /* We cannot expect a graceful transmit stop without link !!! */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001237 if (fep->link) {
1238 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
Philippe De Muyter677177c2006-06-27 13:05:33 +10001239 udelay(10);
Sascha Hauerf44d6302009-04-15 03:11:30 +00001240 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Philippe De Muyter677177c2006-06-27 13:05:33 +10001241 printk("fec_stop : Graceful transmit stop did not complete !\n");
Sascha Hauerf44d6302009-04-15 03:11:30 +00001242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Sascha Hauerf44d6302009-04-15 03:11:30 +00001244 /* Whack a reset. We should wait for this. */
1245 writel(1, fep->hwp + FEC_ECNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 udelay(10);
Sascha Hauerf44d6302009-04-15 03:11:30 +00001247 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Wolfram Sang4bee1f92010-07-21 02:51:13 +00001248 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
Sascha Haueread73182009-01-28 23:03:11 +00001251static int __devinit
1252fec_probe(struct platform_device *pdev)
1253{
1254 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07001255 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00001256 struct net_device *ndev;
1257 int i, irq, ret = 0;
1258 struct resource *r;
1259
1260 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1261 if (!r)
1262 return -ENXIO;
1263
1264 r = request_mem_region(r->start, resource_size(r), pdev->name);
1265 if (!r)
1266 return -EBUSY;
1267
1268 /* Init network device */
1269 ndev = alloc_etherdev(sizeof(struct fec_enet_private));
1270 if (!ndev)
1271 return -ENOMEM;
1272
1273 SET_NETDEV_DEV(ndev, &pdev->dev);
1274
1275 /* setup board info structure */
1276 fep = netdev_priv(ndev);
1277 memset(fep, 0, sizeof(*fep));
1278
1279 ndev->base_addr = (unsigned long)ioremap(r->start, resource_size(r));
Bryan Wue6b043d2010-03-31 02:10:44 +00001280 fep->pdev = pdev;
Sascha Haueread73182009-01-28 23:03:11 +00001281
1282 if (!ndev->base_addr) {
1283 ret = -ENOMEM;
1284 goto failed_ioremap;
1285 }
1286
1287 platform_set_drvdata(pdev, ndev);
1288
Baruch Siach5eb32bd2010-05-24 00:36:13 -07001289 pdata = pdev->dev.platform_data;
1290 if (pdata)
1291 fep->phy_interface = pdata->phy;
1292
Sascha Haueread73182009-01-28 23:03:11 +00001293 /* This device has up to three irqs on some platforms */
1294 for (i = 0; i < 3; i++) {
1295 irq = platform_get_irq(pdev, i);
1296 if (i && irq < 0)
1297 break;
1298 ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
1299 if (ret) {
1300 while (i >= 0) {
1301 irq = platform_get_irq(pdev, i);
1302 free_irq(irq, ndev);
1303 i--;
1304 }
1305 goto failed_irq;
1306 }
1307 }
1308
1309 fep->clk = clk_get(&pdev->dev, "fec_clk");
1310 if (IS_ERR(fep->clk)) {
1311 ret = PTR_ERR(fep->clk);
1312 goto failed_clk;
1313 }
1314 clk_enable(fep->clk);
1315
Shawn Guo8649a232011-01-05 21:13:10 +00001316 ret = fec_enet_init(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001317 if (ret)
1318 goto failed_init;
1319
Bryan Wue6b043d2010-03-31 02:10:44 +00001320 ret = fec_enet_mii_init(pdev);
1321 if (ret)
1322 goto failed_mii_init;
1323
Oskar Schirmer03c698c2010-10-07 02:30:30 +00001324 /* Carrier starts down, phylib will bring it up */
1325 netif_carrier_off(ndev);
1326
Sascha Haueread73182009-01-28 23:03:11 +00001327 ret = register_netdev(ndev);
1328 if (ret)
1329 goto failed_register;
1330
1331 return 0;
1332
1333failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00001334 fec_enet_mii_remove(fep);
1335failed_mii_init:
Sascha Haueread73182009-01-28 23:03:11 +00001336failed_init:
1337 clk_disable(fep->clk);
1338 clk_put(fep->clk);
1339failed_clk:
1340 for (i = 0; i < 3; i++) {
1341 irq = platform_get_irq(pdev, i);
1342 if (irq > 0)
1343 free_irq(irq, ndev);
1344 }
1345failed_irq:
1346 iounmap((void __iomem *)ndev->base_addr);
1347failed_ioremap:
1348 free_netdev(ndev);
1349
1350 return ret;
1351}
1352
1353static int __devexit
1354fec_drv_remove(struct platform_device *pdev)
1355{
1356 struct net_device *ndev = platform_get_drvdata(pdev);
1357 struct fec_enet_private *fep = netdev_priv(ndev);
1358
1359 platform_set_drvdata(pdev, NULL);
1360
1361 fec_stop(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001362 fec_enet_mii_remove(fep);
Sascha Haueread73182009-01-28 23:03:11 +00001363 clk_disable(fep->clk);
1364 clk_put(fep->clk);
1365 iounmap((void __iomem *)ndev->base_addr);
1366 unregister_netdev(ndev);
1367 free_netdev(ndev);
1368 return 0;
1369}
1370
Denis Kirjanov59d42892010-06-02 09:27:04 +00001371#ifdef CONFIG_PM
Sascha Haueread73182009-01-28 23:03:11 +00001372static int
Eric Benard87cad5c2010-06-18 04:19:54 +00001373fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00001374{
Eric Benard87cad5c2010-06-18 04:19:54 +00001375 struct net_device *ndev = dev_get_drvdata(dev);
Sascha Haueread73182009-01-28 23:03:11 +00001376 struct fec_enet_private *fep;
1377
1378 if (ndev) {
1379 fep = netdev_priv(ndev);
Eric Bénarde3fe8552010-06-02 06:13:34 -07001380 if (netif_running(ndev))
1381 fec_enet_close(ndev);
1382 clk_disable(fep->clk);
Sascha Haueread73182009-01-28 23:03:11 +00001383 }
1384 return 0;
1385}
1386
1387static int
Eric Benard87cad5c2010-06-18 04:19:54 +00001388fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00001389{
Eric Benard87cad5c2010-06-18 04:19:54 +00001390 struct net_device *ndev = dev_get_drvdata(dev);
Eric Bénarde3fe8552010-06-02 06:13:34 -07001391 struct fec_enet_private *fep;
Sascha Haueread73182009-01-28 23:03:11 +00001392
1393 if (ndev) {
Eric Bénarde3fe8552010-06-02 06:13:34 -07001394 fep = netdev_priv(ndev);
1395 clk_enable(fep->clk);
1396 if (netif_running(ndev))
1397 fec_enet_open(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001398 }
1399 return 0;
1400}
1401
Denis Kirjanov59d42892010-06-02 09:27:04 +00001402static const struct dev_pm_ops fec_pm_ops = {
1403 .suspend = fec_suspend,
1404 .resume = fec_resume,
1405 .freeze = fec_suspend,
1406 .thaw = fec_resume,
1407 .poweroff = fec_suspend,
1408 .restore = fec_resume,
1409};
Eric Benard87cad5c2010-06-18 04:19:54 +00001410#endif
Denis Kirjanov59d42892010-06-02 09:27:04 +00001411
Sascha Haueread73182009-01-28 23:03:11 +00001412static struct platform_driver fec_driver = {
1413 .driver = {
Eric Benard87cad5c2010-06-18 04:19:54 +00001414 .name = "fec",
1415 .owner = THIS_MODULE,
1416#ifdef CONFIG_PM
1417 .pm = &fec_pm_ops,
1418#endif
Sascha Haueread73182009-01-28 23:03:11 +00001419 },
Eric Benard87cad5c2010-06-18 04:19:54 +00001420 .probe = fec_probe,
1421 .remove = __devexit_p(fec_drv_remove),
Sascha Haueread73182009-01-28 23:03:11 +00001422};
1423
1424static int __init
1425fec_enet_module_init(void)
1426{
1427 printk(KERN_INFO "FEC Ethernet Driver\n");
1428
1429 return platform_driver_register(&fec_driver);
1430}
1431
1432static void __exit
1433fec_enet_cleanup(void)
1434{
1435 platform_driver_unregister(&fec_driver);
1436}
1437
1438module_exit(fec_enet_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439module_init(fec_enet_module_init);
1440
1441MODULE_LICENSE("GPL");