blob: b4afd7a6ee29adfcddfc64ad3c2664afff79a0ef [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
121/* The FEC stores dest/src/type, data, and checksum for receive packets.
122 */
123#define PKT_MAXBUF_SIZE 1518
124#define PKT_MINBUF_SIZE 64
125#define PKT_MAXBLR_SIZE 1520
126
127
128/*
Matt Waddel6b265292006-06-27 13:10:56 +1000129 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * size bits. Other FEC hardware does not, so we need to take that into
131 * account when setting it.
132 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000133#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Sascha Hauer196719e2009-01-28 23:03:10 +0000134 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARCH_MXC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
136#else
137#define OPT_FRAME_SIZE 0
138#endif
139
140/* The FEC buffer descriptors track the ring buffers. The rx_bd_base and
141 * tx_bd_base always point to the base of the buffer descriptors. The
142 * cur_rx and cur_tx point to the currently available buffer.
143 * The dirty_tx tracks the current buffer that is being sent by the
144 * controller. The cur_tx and dirty_tx are equal under both completely
145 * empty and completely full conditions. The empty/ready indicator in
146 * the buffer descriptor determines the actual condition.
147 */
148struct fec_enet_private {
149 /* Hardware registers of the FEC device */
Sascha Hauerf44d6302009-04-15 03:11:30 +0000150 void __iomem *hwp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Greg Ungerercb84d6e2007-07-30 16:29:09 +1000152 struct net_device *netdev;
153
Sascha Haueread73182009-01-28 23:03:11 +0000154 struct clk *clk;
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 /* The saved address of a sent-in-place packet/buffer, for skfree(). */
157 unsigned char *tx_bounce[TX_RING_SIZE];
158 struct sk_buff* tx_skbuff[TX_RING_SIZE];
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000159 struct sk_buff* rx_skbuff[RX_RING_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 ushort skb_cur;
161 ushort skb_dirty;
162
Sascha Hauer22f6b862009-04-15 01:32:18 +0000163 /* CPM dual port RAM relative addresses */
Sascha Hauer4661e752009-01-28 23:03:07 +0000164 dma_addr_t bd_dma;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000165 /* Address of Rx and Tx buffers */
Sascha Hauer2e285322009-04-15 01:32:16 +0000166 struct bufdesc *rx_bd_base;
167 struct bufdesc *tx_bd_base;
168 /* The next free ring entry */
169 struct bufdesc *cur_rx, *cur_tx;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000170 /* The ring entries to be free()ed */
Sascha Hauer2e285322009-04-15 01:32:16 +0000171 struct bufdesc *dirty_tx;
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 uint tx_full;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000174 /* hold while accessing the HW like ringbuffer for tx/rx but not MAC */
175 spinlock_t hw_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Bryan Wue6b043d2010-03-31 02:10:44 +0000177 struct platform_device *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 int opened;
Bryan Wue6b043d2010-03-31 02:10:44 +0000180
181 /* Phylib and MDIO interface */
182 struct mii_bus *mii_bus;
183 struct phy_device *phy_dev;
184 int mii_timeout;
185 uint phy_speed;
Baruch Siach5eb32bd2010-05-24 00:36:13 -0700186 phy_interface_t phy_interface;
Bryan Wue6b043d2010-03-31 02:10:44 +0000187 int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 int link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 int full_duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190};
191
David Howells7d12e782006-10-05 14:55:46 +0100192static irqreturn_t fec_enet_interrupt(int irq, void * dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static void fec_enet_tx(struct net_device *dev);
194static void fec_enet_rx(struct net_device *dev);
195static int fec_enet_close(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static void fec_restart(struct net_device *dev, int duplex);
197static void fec_stop(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Bryan Wue6b043d2010-03-31 02:10:44 +0000199/* FEC MII MMFR bits definition */
200#define FEC_MMFR_ST (1 << 30)
201#define FEC_MMFR_OP_READ (2 << 28)
202#define FEC_MMFR_OP_WRITE (1 << 28)
203#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
204#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
205#define FEC_MMFR_TA (2 << 16)
206#define FEC_MMFR_DATA(v) (v & 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Bryan Wue6b043d2010-03-31 02:10:44 +0000208#define FEC_MII_TIMEOUT 10000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Sascha Hauer22f6b862009-04-15 01:32:18 +0000210/* Transmitter timeout */
211#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Denis Kirjanovc7621cb2010-06-02 09:15:47 +0000213static netdev_tx_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
215{
Sascha Hauerf44d6302009-04-15 03:11:30 +0000216 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000217 struct bufdesc *bdp;
Greg Ungerer9555b312009-08-06 17:58:18 +0000218 void *bufaddr;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000219 unsigned short status;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000220 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (!fep->link) {
223 /* Link is down or autonegotiation is in progress. */
Patrick McHardy5b548142009-06-12 06:22:29 +0000224 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000227 spin_lock_irqsave(&fep->hw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /* Fill in a Tx ring entry */
229 bdp = fep->cur_tx;
230
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000231 status = bdp->cbd_sc;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000232
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000233 if (status & BD_ENET_TX_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /* Ooops. All transmit buffers are full. Bail out.
235 * This should not happen, since dev->tbusy should be set.
236 */
237 printk("%s: tx queue full!.\n", dev->name);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000238 spin_unlock_irqrestore(&fep->hw_lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +0000239 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Sascha Hauer22f6b862009-04-15 01:32:18 +0000242 /* Clear all of the status flags */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000243 status &= ~BD_ENET_TX_STATS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Sascha Hauer22f6b862009-04-15 01:32:18 +0000245 /* Set buffer length and buffer pointer */
Greg Ungerer9555b312009-08-06 17:58:18 +0000246 bufaddr = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 bdp->cbd_datlen = skb->len;
248
249 /*
Sascha Hauer22f6b862009-04-15 01:32:18 +0000250 * On some FEC implementations data must be aligned on
251 * 4-byte boundaries. Use bounce buffers to copy data
252 * and get it aligned. Ugh.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 */
Greg Ungerer9555b312009-08-06 17:58:18 +0000254 if (((unsigned long) bufaddr) & FEC_ALIGNMENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 unsigned int index;
256 index = bdp - fep->tx_bd_base;
Sascha Hauer6989f512009-01-28 23:03:06 +0000257 memcpy(fep->tx_bounce[index], (void *)skb->data, skb->len);
Greg Ungerer9555b312009-08-06 17:58:18 +0000258 bufaddr = fep->tx_bounce[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260
Sascha Hauer22f6b862009-04-15 01:32:18 +0000261 /* Save skb pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 fep->tx_skbuff[fep->skb_cur] = skb;
263
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700264 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 fep->skb_cur = (fep->skb_cur+1) & TX_RING_MOD_MASK;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 /* Push the data cache so the CPM does not get stale memory
268 * data.
269 */
Greg Ungerer9555b312009-08-06 17:58:18 +0000270 bdp->cbd_bufaddr = dma_map_single(&dev->dev, bufaddr,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000271 FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000273 /* Send it on its way. Tell FEC it's ready, interrupt when done,
274 * it's the last BD of the frame, and to put the CRC on the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000276 status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 | BD_ENET_TX_LAST | BD_ENET_TX_TC);
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000278 bdp->cbd_sc = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 /* Trigger transmission start */
Sascha Hauerf44d6302009-04-15 03:11:30 +0000281 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Sascha Hauer22f6b862009-04-15 01:32:18 +0000283 /* If this was the last BD in the ring, start at the beginning again. */
284 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 bdp = fep->tx_bd_base;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000286 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 bdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 if (bdp == fep->dirty_tx) {
290 fep->tx_full = 1;
291 netif_stop_queue(dev);
292 }
293
Sascha Hauer2e285322009-04-15 01:32:16 +0000294 fep->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000296 spin_unlock_irqrestore(&fep->hw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Patrick McHardy6ed10652009-06-23 06:03:08 +0000298 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301static void
302fec_timeout(struct net_device *dev)
303{
304 struct fec_enet_private *fep = netdev_priv(dev);
305
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700306 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000308 fec_restart(dev, fep->full_duplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 netif_wake_queue(dev);
310}
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100313fec_enet_interrupt(int irq, void * dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 struct net_device *dev = dev_id;
Sascha Hauerf44d6302009-04-15 03:11:30 +0000316 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 uint int_events;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000318 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000320 do {
Sascha Hauerf44d6302009-04-15 03:11:30 +0000321 int_events = readl(fep->hwp + FEC_IEVENT);
322 writel(int_events, fep->hwp + FEC_IEVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (int_events & FEC_ENET_RXF) {
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000325 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 fec_enet_rx(dev);
327 }
328
329 /* Transmit OK, or non-fatal error. Update the buffer
Sascha Hauerf44d6302009-04-15 03:11:30 +0000330 * descriptors. FEC handles all errors, we just discover
331 * them as part of the transmit process.
332 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (int_events & FEC_ENET_TXF) {
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000334 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 fec_enet_tx(dev);
336 }
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000337 } while (int_events);
338
339 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
342
343static void
344fec_enet_tx(struct net_device *dev)
345{
346 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +0000347 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000348 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 struct sk_buff *skb;
350
351 fep = netdev_priv(dev);
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000352 spin_lock(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 bdp = fep->dirty_tx;
354
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000355 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000356 if (bdp == fep->cur_tx && fep->tx_full == 0)
357 break;
358
359 dma_unmap_single(&dev->dev, bdp->cbd_bufaddr, FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
360 bdp->cbd_bufaddr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 skb = fep->tx_skbuff[fep->skb_dirty];
363 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000364 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 BD_ENET_TX_RL | BD_ENET_TX_UN |
366 BD_ENET_TX_CSL)) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700367 dev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000368 if (status & BD_ENET_TX_HB) /* No heartbeat */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700369 dev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000370 if (status & BD_ENET_TX_LC) /* Late collision */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700371 dev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000372 if (status & BD_ENET_TX_RL) /* Retrans limit */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700373 dev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000374 if (status & BD_ENET_TX_UN) /* Underrun */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700375 dev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000376 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700377 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 } else {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700379 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000382 if (status & BD_ENET_TX_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 printk("HEY! Enet xmit interrupt and TX_READY.\n");
Sascha Hauer22f6b862009-04-15 01:32:18 +0000384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 /* Deferred means some collisions occurred during transmit,
386 * but we eventually sent the packet OK.
387 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000388 if (status & BD_ENET_TX_DEF)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700389 dev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400390
Sascha Hauer22f6b862009-04-15 01:32:18 +0000391 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 dev_kfree_skb_any(skb);
393 fep->tx_skbuff[fep->skb_dirty] = NULL;
394 fep->skb_dirty = (fep->skb_dirty + 1) & TX_RING_MOD_MASK;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400395
Sascha Hauer22f6b862009-04-15 01:32:18 +0000396 /* Update pointer to next buffer descriptor to be transmitted */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000397 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 bdp = fep->tx_bd_base;
399 else
400 bdp++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400401
Sascha Hauer22f6b862009-04-15 01:32:18 +0000402 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 */
404 if (fep->tx_full) {
405 fep->tx_full = 0;
406 if (netif_queue_stopped(dev))
407 netif_wake_queue(dev);
408 }
409 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000410 fep->dirty_tx = bdp;
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000411 spin_unlock(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
414
415/* During a receive, the cur_rx points to the current incoming buffer.
416 * When we update through the ring, if the next incoming buffer has
417 * not been given to the system, we just set the empty indicator,
418 * effectively tossing the packet.
419 */
420static void
421fec_enet_rx(struct net_device *dev)
422{
Sascha Hauerf44d6302009-04-15 03:11:30 +0000423 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000424 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000425 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 struct sk_buff *skb;
427 ushort pkt_len;
428 __u8 *data;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400429
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000430#ifdef CONFIG_M532x
431 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400432#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000434 spin_lock(&fep->hw_lock);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /* First, grab all of the stats for the incoming packet.
437 * These get messed up if we get called due to a busy condition.
438 */
439 bdp = fep->cur_rx;
440
Sascha Hauer22f6b862009-04-15 01:32:18 +0000441 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Sascha Hauer22f6b862009-04-15 01:32:18 +0000443 /* Since we have allocated space to hold a complete frame,
444 * the last indicator should be set.
445 */
446 if ((status & BD_ENET_RX_LAST) == 0)
447 printk("FEC ENET: rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Sascha Hauer22f6b862009-04-15 01:32:18 +0000449 if (!fep->opened)
450 goto rx_processing_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Sascha Hauer22f6b862009-04-15 01:32:18 +0000452 /* Check for errors. */
453 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Sascha Hauer22f6b862009-04-15 01:32:18 +0000455 dev->stats.rx_errors++;
456 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
457 /* Frame too long or too short. */
458 dev->stats.rx_length_errors++;
459 }
460 if (status & BD_ENET_RX_NO) /* Frame alignment */
461 dev->stats.rx_frame_errors++;
462 if (status & BD_ENET_RX_CR) /* CRC Error */
463 dev->stats.rx_crc_errors++;
464 if (status & BD_ENET_RX_OV) /* FIFO overrun */
465 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
Sascha Hauer22f6b862009-04-15 01:32:18 +0000467
468 /* Report late collisions as a frame error.
469 * On this error, the BD is closed, but we don't know what we
470 * have in the buffer. So, just drop this frame on the floor.
471 */
472 if (status & BD_ENET_RX_CL) {
473 dev->stats.rx_errors++;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700474 dev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000475 goto rx_processing_done;
476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Sascha Hauer22f6b862009-04-15 01:32:18 +0000478 /* Process the incoming frame. */
479 dev->stats.rx_packets++;
480 pkt_len = bdp->cbd_datlen;
481 dev->stats.rx_bytes += pkt_len;
482 data = (__u8*)__va(bdp->cbd_bufaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000484 dma_unmap_single(NULL, bdp->cbd_bufaddr, bdp->cbd_datlen,
485 DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +0000486
Sascha Hauer22f6b862009-04-15 01:32:18 +0000487 /* This does 16 byte alignment, exactly what we need.
488 * The packet length includes FCS, but we don't want to
489 * include that when passing upstream as it messes up
490 * bridging applications.
491 */
Sascha Hauer85498892009-04-15 01:32:21 +0000492 skb = dev_alloc_skb(pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Sascha Hauer85498892009-04-15 01:32:21 +0000494 if (unlikely(!skb)) {
Sascha Hauer22f6b862009-04-15 01:32:18 +0000495 printk("%s: Memory squeeze, dropping packet.\n",
496 dev->name);
497 dev->stats.rx_dropped++;
498 } else {
Sascha Hauer85498892009-04-15 01:32:21 +0000499 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000500 skb_put(skb, pkt_len - 4); /* Make room */
501 skb_copy_to_linear_data(skb, data, pkt_len - 4);
502 skb->protocol = eth_type_trans(skb, dev);
503 netif_rx(skb);
504 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000505
506 bdp->cbd_bufaddr = dma_map_single(NULL, data, bdp->cbd_datlen,
507 DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000508rx_processing_done:
509 /* Clear the status flags for this buffer */
510 status &= ~BD_ENET_RX_STATS;
511
512 /* Mark the buffer empty */
513 status |= BD_ENET_RX_EMPTY;
514 bdp->cbd_sc = status;
515
516 /* Update BD pointer to next entry */
517 if (status & BD_ENET_RX_WRAP)
518 bdp = fep->rx_bd_base;
519 else
520 bdp++;
521 /* Doing this here will keep the FEC running while we process
522 * incoming frames. On a heavily loaded network, we should be
523 * able to keep up at the expense of system resources.
524 */
525 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000527 fep->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000529 spin_unlock(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532/* ------------------------------------------------------------------------- */
Greg Ungerer43be6362009-02-26 22:42:51 -0800533#ifdef CONFIG_M5272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534static void __inline__ fec_get_mac(struct net_device *dev)
535{
536 struct fec_enet_private *fep = netdev_priv(dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000537 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000539 if (FEC_FLASHMAC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /*
541 * Get MAC address from FLASH.
542 * If it is all 1's or 0's, use the default.
543 */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000544 iap = (unsigned char *)FEC_FLASHMAC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if ((iap[0] == 0) && (iap[1] == 0) && (iap[2] == 0) &&
546 (iap[3] == 0) && (iap[4] == 0) && (iap[5] == 0))
547 iap = fec_mac_default;
548 if ((iap[0] == 0xff) && (iap[1] == 0xff) && (iap[2] == 0xff) &&
549 (iap[3] == 0xff) && (iap[4] == 0xff) && (iap[5] == 0xff))
550 iap = fec_mac_default;
551 } else {
Sascha Hauerf44d6302009-04-15 03:11:30 +0000552 *((unsigned long *) &tmpaddr[0]) = readl(fep->hwp + FEC_ADDR_LOW);
553 *((unsigned short *) &tmpaddr[4]) = (readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 iap = &tmpaddr[0];
555 }
556
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000557 memcpy(dev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 /* Adjust MAC if using default MAC address */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000560 if (iap == fec_mac_default)
561 dev->dev_addr[ETH_ALEN-1] = fec_mac_default[ETH_ALEN-1] + fep->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563#endif
564
565/* ------------------------------------------------------------------------- */
566
Bryan Wue6b043d2010-03-31 02:10:44 +0000567/*
568 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 */
Bryan Wue6b043d2010-03-31 02:10:44 +0000570static void fec_enet_adjust_link(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Bryan Wue6b043d2010-03-31 02:10:44 +0000572 struct fec_enet_private *fep = netdev_priv(dev);
573 struct phy_device *phy_dev = fep->phy_dev;
574 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Bryan Wue6b043d2010-03-31 02:10:44 +0000576 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Bryan Wue6b043d2010-03-31 02:10:44 +0000578 spin_lock_irqsave(&fep->hw_lock, flags);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400579
Bryan Wue6b043d2010-03-31 02:10:44 +0000580 /* Prevent a state halted on mii error */
581 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
582 phy_dev->state = PHY_RESUMING;
583 goto spin_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
Bryan Wue6b043d2010-03-31 02:10:44 +0000585
586 /* Duplex link change */
587 if (phy_dev->link) {
588 if (fep->full_duplex != phy_dev->duplex) {
589 fec_restart(dev, phy_dev->duplex);
590 status_change = 1;
591 }
592 }
593
594 /* Link on or off change */
595 if (phy_dev->link != fep->link) {
596 fep->link = phy_dev->link;
597 if (phy_dev->link)
598 fec_restart(dev, phy_dev->duplex);
599 else
600 fec_stop(dev);
601 status_change = 1;
602 }
603
604spin_unlock:
605 spin_unlock_irqrestore(&fep->hw_lock, flags);
606
607 if (status_change)
608 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
Bryan Wue6b043d2010-03-31 02:10:44 +0000611/*
612 * NOTE: a MII transaction is during around 25 us, so polling it...
613 */
614static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Bryan Wue6b043d2010-03-31 02:10:44 +0000616 struct fec_enet_private *fep = bus->priv;
617 int timeout = FEC_MII_TIMEOUT;
618
619 fep->mii_timeout = 0;
620
621 /* clear MII end of transfer bit*/
622 writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
623
624 /* start a read op */
625 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
626 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
627 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
628
629 /* wait for end of transfer */
630 while (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_MII)) {
631 cpu_relax();
632 if (timeout-- < 0) {
633 fep->mii_timeout = 1;
634 printk(KERN_ERR "FEC: MDIO read timeout\n");
635 return -ETIMEDOUT;
636 }
637 }
638
639 /* return value */
640 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
641}
642
643static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
644 u16 value)
645{
646 struct fec_enet_private *fep = bus->priv;
647 int timeout = FEC_MII_TIMEOUT;
648
649 fep->mii_timeout = 0;
650
651 /* clear MII end of transfer bit*/
652 writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
653
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 */
661 while (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_MII)) {
662 cpu_relax();
663 if (timeout-- < 0) {
664 fep->mii_timeout = 1;
665 printk(KERN_ERR "FEC: MDIO write timeout\n");
666 return -ETIMEDOUT;
667 }
668 }
669
670 return 0;
671}
672
673static int fec_enet_mdio_reset(struct mii_bus *bus)
674{
675 return 0;
676}
677
678static int fec_enet_mii_probe(struct net_device *dev)
679{
680 struct fec_enet_private *fep = netdev_priv(dev);
681 struct phy_device *phy_dev = NULL;
Denis Kirjanov1273d972010-06-02 09:17:00 +0000682 int ret;
Bryan Wue6b043d2010-03-31 02:10:44 +0000683
Bryan Wu418bd0d2010-05-28 03:40:39 -0700684 fep->phy_dev = NULL;
685
Bryan Wue6b043d2010-03-31 02:10:44 +0000686 /* find the first phy */
Denis Kirjanov1273d972010-06-02 09:17:00 +0000687 phy_dev = phy_find_first(fep->mii_bus);
Bryan Wue6b043d2010-03-31 02:10:44 +0000688 if (!phy_dev) {
689 printk(KERN_ERR "%s: no PHY found\n", dev->name);
690 return -ENODEV;
691 }
692
693 /* attach the mac to the phy */
Denis Kirjanov1273d972010-06-02 09:17:00 +0000694 ret = phy_connect_direct(dev, phy_dev,
Bryan Wue6b043d2010-03-31 02:10:44 +0000695 &fec_enet_adjust_link, 0,
696 PHY_INTERFACE_MODE_MII);
Denis Kirjanov1273d972010-06-02 09:17:00 +0000697 if (ret) {
Bryan Wue6b043d2010-03-31 02:10:44 +0000698 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
Denis Kirjanov1273d972010-06-02 09:17:00 +0000699 return ret;
Bryan Wue6b043d2010-03-31 02:10:44 +0000700 }
701
702 /* mask with MAC supported features */
703 phy_dev->supported &= PHY_BASIC_FEATURES;
704 phy_dev->advertising = phy_dev->supported;
705
706 fep->phy_dev = phy_dev;
707 fep->link = 0;
708 fep->full_duplex = 0;
709
Bryan Wu418bd0d2010-05-28 03:40:39 -0700710 printk(KERN_INFO "%s: Freescale FEC PHY driver [%s] "
711 "(mii_bus:phy_addr=%s, irq=%d)\n", dev->name,
712 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
713 fep->phy_dev->irq);
714
Bryan Wue6b043d2010-03-31 02:10:44 +0000715 return 0;
716}
717
718static int fec_enet_mii_init(struct platform_device *pdev)
719{
720 struct net_device *dev = platform_get_drvdata(pdev);
721 struct fec_enet_private *fep = netdev_priv(dev);
722 int err = -ENXIO, i;
723
724 fep->mii_timeout = 0;
725
726 /*
727 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
728 */
729 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk), 5000000) << 1;
730 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
731
732 fep->mii_bus = mdiobus_alloc();
733 if (fep->mii_bus == NULL) {
734 err = -ENOMEM;
735 goto err_out;
736 }
737
738 fep->mii_bus->name = "fec_enet_mii_bus";
739 fep->mii_bus->read = fec_enet_mdio_read;
740 fep->mii_bus->write = fec_enet_mdio_write;
741 fep->mii_bus->reset = fec_enet_mdio_reset;
742 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%x", pdev->id);
743 fep->mii_bus->priv = fep;
744 fep->mii_bus->parent = &pdev->dev;
745
746 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
747 if (!fep->mii_bus->irq) {
748 err = -ENOMEM;
749 goto err_out_free_mdiobus;
750 }
751
752 for (i = 0; i < PHY_MAX_ADDR; i++)
753 fep->mii_bus->irq[i] = PHY_POLL;
754
755 platform_set_drvdata(dev, fep->mii_bus);
756
757 if (mdiobus_register(fep->mii_bus))
758 goto err_out_free_mdio_irq;
759
Bryan Wue6b043d2010-03-31 02:10:44 +0000760 return 0;
761
Bryan Wue6b043d2010-03-31 02:10:44 +0000762err_out_free_mdio_irq:
763 kfree(fep->mii_bus->irq);
764err_out_free_mdiobus:
765 mdiobus_free(fep->mii_bus);
766err_out:
767 return err;
768}
769
770static void fec_enet_mii_remove(struct fec_enet_private *fep)
771{
772 if (fep->phy_dev)
773 phy_disconnect(fep->phy_dev);
774 mdiobus_unregister(fep->mii_bus);
775 kfree(fep->mii_bus->irq);
776 mdiobus_free(fep->mii_bus);
777}
778
779static int fec_enet_get_settings(struct net_device *dev,
780 struct ethtool_cmd *cmd)
781{
782 struct fec_enet_private *fep = netdev_priv(dev);
783 struct phy_device *phydev = fep->phy_dev;
784
785 if (!phydev)
786 return -ENODEV;
787
788 return phy_ethtool_gset(phydev, cmd);
789}
790
791static int fec_enet_set_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_sset(phydev, cmd);
801}
802
803static void fec_enet_get_drvinfo(struct net_device *dev,
804 struct ethtool_drvinfo *info)
805{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 struct fec_enet_private *fep = netdev_priv(dev);
807
Bryan Wue6b043d2010-03-31 02:10:44 +0000808 strcpy(info->driver, fep->pdev->dev.driver->name);
809 strcpy(info->version, "Revision: 1.0");
810 strcpy(info->bus_info, dev_name(&dev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
Bryan Wue6b043d2010-03-31 02:10:44 +0000812
813static struct ethtool_ops fec_enet_ethtool_ops = {
814 .get_settings = fec_enet_get_settings,
815 .set_settings = fec_enet_set_settings,
816 .get_drvinfo = fec_enet_get_drvinfo,
817 .get_link = ethtool_op_get_link,
818};
819
820static int fec_enet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
821{
822 struct fec_enet_private *fep = netdev_priv(dev);
823 struct phy_device *phydev = fep->phy_dev;
824
825 if (!netif_running(dev))
826 return -EINVAL;
827
828 if (!phydev)
829 return -ENODEV;
830
831 return phy_mii_ioctl(phydev, if_mii(rq), cmd);
832}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000834static void fec_enet_free_buffers(struct net_device *dev)
835{
836 struct fec_enet_private *fep = netdev_priv(dev);
837 int i;
838 struct sk_buff *skb;
839 struct bufdesc *bdp;
840
841 bdp = fep->rx_bd_base;
842 for (i = 0; i < RX_RING_SIZE; i++) {
843 skb = fep->rx_skbuff[i];
844
845 if (bdp->cbd_bufaddr)
846 dma_unmap_single(&dev->dev, bdp->cbd_bufaddr,
847 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
848 if (skb)
849 dev_kfree_skb(skb);
850 bdp++;
851 }
852
853 bdp = fep->tx_bd_base;
854 for (i = 0; i < TX_RING_SIZE; i++)
855 kfree(fep->tx_bounce[i]);
856}
857
858static int fec_enet_alloc_buffers(struct net_device *dev)
859{
860 struct fec_enet_private *fep = netdev_priv(dev);
861 int i;
862 struct sk_buff *skb;
863 struct bufdesc *bdp;
864
865 bdp = fep->rx_bd_base;
866 for (i = 0; i < RX_RING_SIZE; i++) {
867 skb = dev_alloc_skb(FEC_ENET_RX_FRSIZE);
868 if (!skb) {
869 fec_enet_free_buffers(dev);
870 return -ENOMEM;
871 }
872 fep->rx_skbuff[i] = skb;
873
874 bdp->cbd_bufaddr = dma_map_single(&dev->dev, skb->data,
875 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
876 bdp->cbd_sc = BD_ENET_RX_EMPTY;
877 bdp++;
878 }
879
880 /* Set the last buffer to wrap. */
881 bdp--;
882 bdp->cbd_sc |= BD_SC_WRAP;
883
884 bdp = fep->tx_bd_base;
885 for (i = 0; i < TX_RING_SIZE; i++) {
886 fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
887
888 bdp->cbd_sc = 0;
889 bdp->cbd_bufaddr = 0;
890 bdp++;
891 }
892
893 /* Set the last buffer to wrap. */
894 bdp--;
895 bdp->cbd_sc |= BD_SC_WRAP;
896
897 return 0;
898}
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900static int
901fec_enet_open(struct net_device *dev)
902{
903 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000904 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 /* I should reset the ring buffers here, but I don't yet know
907 * a simple way to do that.
908 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000910 ret = fec_enet_alloc_buffers(dev);
911 if (ret)
912 return ret;
913
Bryan Wu418bd0d2010-05-28 03:40:39 -0700914 /* Probe and connect to PHY when open the interface */
915 ret = fec_enet_mii_probe(dev);
916 if (ret) {
917 fec_enet_free_buffers(dev);
918 return ret;
919 }
Bryan Wue6b043d2010-03-31 02:10:44 +0000920 phy_start(fep->phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 netif_start_queue(dev);
922 fep->opened = 1;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000923 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
926static int
927fec_enet_close(struct net_device *dev)
928{
929 struct fec_enet_private *fep = netdev_priv(dev);
930
Sascha Hauer22f6b862009-04-15 01:32:18 +0000931 /* Don't know what to do yet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 fep->opened = 0;
933 netif_stop_queue(dev);
934 fec_stop(dev);
935
Bryan Wu418bd0d2010-05-28 03:40:39 -0700936 if (fep->phy_dev)
937 phy_disconnect(fep->phy_dev);
938
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000939 fec_enet_free_buffers(dev);
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return 0;
942}
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944/* Set or clear the multicast filter for this adaptor.
945 * Skeleton taken from sunlance driver.
946 * The CPM Ethernet implementation allows Multicast as well as individual
947 * MAC address filtering. Some of the drivers check to make sure it is
948 * a group multicast address, and discard those that are not. I guess I
949 * will do the same for now, but just remove the test if you want
950 * individual filtering as well (do the upper net layers want or support
951 * this kind of feature?).
952 */
953
954#define HASH_BITS 6 /* #bits in hash */
955#define CRC32_POLY 0xEDB88320
956
957static void set_multicast_list(struct net_device *dev)
958{
Sascha Hauerf44d6302009-04-15 03:11:30 +0000959 struct fec_enet_private *fep = netdev_priv(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000960 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +0000961 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 unsigned char hash;
963
Sascha Hauer22f6b862009-04-15 01:32:18 +0000964 if (dev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +0000965 tmp = readl(fep->hwp + FEC_R_CNTRL);
966 tmp |= 0x8;
967 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +0000968 return;
969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Sascha Hauer4e831832009-04-15 01:32:19 +0000971 tmp = readl(fep->hwp + FEC_R_CNTRL);
972 tmp &= ~0x8;
973 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400974
Sascha Hauer4e831832009-04-15 01:32:19 +0000975 if (dev->flags & IFF_ALLMULTI) {
976 /* Catch all multicast addresses, so set the
977 * filter to all 1's
978 */
979 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
980 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Sascha Hauer4e831832009-04-15 01:32:19 +0000982 return;
983 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400984
Sascha Hauer4e831832009-04-15 01:32:19 +0000985 /* Clear filter and add the addresses in hash register
986 */
987 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
988 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Jiri Pirko22bedad32010-04-01 21:22:57 +0000990 netdev_for_each_mc_addr(ha, dev) {
Sascha Hauer4e831832009-04-15 01:32:19 +0000991 /* Only support group multicast for now */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000992 if (!(ha->addr[0] & 1))
Sascha Hauer4e831832009-04-15 01:32:19 +0000993 continue;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400994
Sascha Hauer4e831832009-04-15 01:32:19 +0000995 /* calculate crc32 value of mac address */
996 crc = 0xffffffff;
997
Jiri Pirko22bedad32010-04-01 21:22:57 +0000998 for (i = 0; i < dev->addr_len; i++) {
999 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00001000 for (bit = 0; bit < 8; bit++, data >>= 1) {
1001 crc = (crc >> 1) ^
1002 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
1004 }
Sascha Hauer4e831832009-04-15 01:32:19 +00001005
1006 /* only upper 6 bits (HASH_BITS) are used
1007 * which point to specific bit in he hash registers
1008 */
1009 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
1010
1011 if (hash > 31) {
1012 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1013 tmp |= 1 << (hash - 32);
1014 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1015 } else {
1016 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1017 tmp |= 1 << hash;
1018 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021}
1022
Sascha Hauer22f6b862009-04-15 01:32:18 +00001023/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00001024static int
1025fec_set_mac_address(struct net_device *dev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001027 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer009fda82009-04-15 01:32:23 +00001028 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Sascha Hauer009fda82009-04-15 01:32:23 +00001030 if (!is_valid_ether_addr(addr->sa_data))
1031 return -EADDRNOTAVAIL;
1032
1033 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1034
Sascha Hauerf44d6302009-04-15 03:11:30 +00001035 writel(dev->dev_addr[3] | (dev->dev_addr[2] << 8) |
1036 (dev->dev_addr[1] << 16) | (dev->dev_addr[0] << 24),
1037 fep->hwp + FEC_ADDR_LOW);
1038 writel((dev->dev_addr[5] << 16) | (dev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07001039 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00001040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
Sascha Hauer009fda82009-04-15 01:32:23 +00001043static const struct net_device_ops fec_netdev_ops = {
1044 .ndo_open = fec_enet_open,
1045 .ndo_stop = fec_enet_close,
1046 .ndo_start_xmit = fec_enet_start_xmit,
1047 .ndo_set_multicast_list = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00001048 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00001049 .ndo_validate_addr = eth_validate_addr,
1050 .ndo_tx_timeout = fec_timeout,
1051 .ndo_set_mac_address = fec_set_mac_address,
Bryan Wue6b043d2010-03-31 02:10:44 +00001052 .ndo_do_ioctl = fec_enet_ioctl,
Sascha Hauer009fda82009-04-15 01:32:23 +00001053};
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /*
1056 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00001057 *
1058 * index is only used in legacy code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 */
Steven King78abcb12009-10-20 18:51:37 -07001060static int fec_enet_init(struct net_device *dev, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
1062 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001063 struct bufdesc *cbd_base;
Rob Herring633e7532010-02-05 08:56:20 +00001064 struct bufdesc *bdp;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001065 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001067 /* Allocate memory for buffer descriptors. */
1068 cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
1069 GFP_KERNEL);
1070 if (!cbd_base) {
Greg Ungerer562d2f82005-11-07 14:09:50 +10001071 printk("FEC: allocate descriptor memory failed?\n");
1072 return -ENOMEM;
1073 }
1074
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10001075 spin_lock_init(&fep->hw_lock);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 fep->index = index;
Sascha Hauerf44d6302009-04-15 03:11:30 +00001078 fep->hwp = (void __iomem *)dev->base_addr;
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001079 fep->netdev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Sascha Haueread73182009-01-28 23:03:11 +00001081 /* Set the Ethernet address */
Greg Ungerer43be6362009-02-26 22:42:51 -08001082#ifdef CONFIG_M5272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 fec_get_mac(dev);
Sascha Haueread73182009-01-28 23:03:11 +00001084#else
1085 {
1086 unsigned long l;
Sascha Hauerf44d6302009-04-15 03:11:30 +00001087 l = readl(fep->hwp + FEC_ADDR_LOW);
Sascha Haueread73182009-01-28 23:03:11 +00001088 dev->dev_addr[0] = (unsigned char)((l & 0xFF000000) >> 24);
1089 dev->dev_addr[1] = (unsigned char)((l & 0x00FF0000) >> 16);
1090 dev->dev_addr[2] = (unsigned char)((l & 0x0000FF00) >> 8);
1091 dev->dev_addr[3] = (unsigned char)((l & 0x000000FF) >> 0);
Sascha Hauerf44d6302009-04-15 03:11:30 +00001092 l = readl(fep->hwp + FEC_ADDR_HIGH);
Sascha Haueread73182009-01-28 23:03:11 +00001093 dev->dev_addr[4] = (unsigned char)((l & 0xFF000000) >> 24);
1094 dev->dev_addr[5] = (unsigned char)((l & 0x00FF0000) >> 16);
1095 }
1096#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001098 /* Set receive and transmit descriptor base. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 fep->rx_bd_base = cbd_base;
1100 fep->tx_bd_base = cbd_base + RX_RING_SIZE;
1101
Sascha Hauer22f6b862009-04-15 01:32:18 +00001102 /* The FEC Ethernet specific entries in the device structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 dev->watchdog_timeo = TX_TIMEOUT;
Sascha Hauer009fda82009-04-15 01:32:23 +00001104 dev->netdev_ops = &fec_netdev_ops;
Bryan Wue6b043d2010-03-31 02:10:44 +00001105 dev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00001106
1107 /* Initialize the receive buffer descriptors. */
1108 bdp = fep->rx_bd_base;
1109 for (i = 0; i < RX_RING_SIZE; i++) {
1110
1111 /* Initialize the BD for every fragment in the page. */
1112 bdp->cbd_sc = 0;
1113 bdp++;
1114 }
1115
1116 /* Set the last buffer to wrap */
1117 bdp--;
1118 bdp->cbd_sc |= BD_SC_WRAP;
1119
1120 /* ...and the same for transmit */
1121 bdp = fep->tx_bd_base;
1122 for (i = 0; i < TX_RING_SIZE; i++) {
1123
1124 /* Initialize the BD for every fragment in the page. */
1125 bdp->cbd_sc = 0;
1126 bdp->cbd_bufaddr = 0;
1127 bdp++;
1128 }
1129
1130 /* Set the last buffer to wrap */
1131 bdp--;
1132 bdp->cbd_sc |= BD_SC_WRAP;
1133
Sascha Haueread73182009-01-28 23:03:11 +00001134 fec_restart(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return 0;
1137}
1138
1139/* This function is called to start or restart the FEC during a link
1140 * change. This only happens when switching between half and full
1141 * duplex.
1142 */
1143static void
1144fec_restart(struct net_device *dev, int duplex)
1145{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001146 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 int i;
1148
Sascha Hauerf44d6302009-04-15 03:11:30 +00001149 /* Whack a reset. We should wait for this. */
1150 writel(1, fep->hwp + FEC_ECNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 udelay(10);
1152
Sascha Hauerf44d6302009-04-15 03:11:30 +00001153 /* Clear any outstanding interrupt. */
1154 writel(0xffc00000, fep->hwp + FEC_IEVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Sascha Hauerf44d6302009-04-15 03:11:30 +00001156 /* Reset all multicast. */
1157 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1158 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Sascha Hauer4f1ceb42009-04-15 01:32:20 +00001159#ifndef CONFIG_M5272
1160 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1161 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1162#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Sascha Hauerf44d6302009-04-15 03:11:30 +00001164 /* Set maximum receive buffer size. */
1165 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Sascha Hauerf44d6302009-04-15 03:11:30 +00001167 /* Set receive and transmit descriptor base. */
1168 writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
Sascha Hauer2e285322009-04-15 01:32:16 +00001169 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc) * RX_RING_SIZE,
Sascha Hauerf44d6302009-04-15 03:11:30 +00001170 fep->hwp + FEC_X_DES_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
1173 fep->cur_rx = fep->rx_bd_base;
1174
Sascha Hauerf44d6302009-04-15 03:11:30 +00001175 /* Reset SKB transmit buffers. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 fep->skb_cur = fep->skb_dirty = 0;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001177 for (i = 0; i <= TX_RING_MOD_MASK; i++) {
1178 if (fep->tx_skbuff[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 dev_kfree_skb_any(fep->tx_skbuff[i]);
1180 fep->tx_skbuff[i] = NULL;
1181 }
1182 }
1183
Sascha Hauer22f6b862009-04-15 01:32:18 +00001184 /* Enable MII mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (duplex) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001186 /* MII enable / FD enable */
1187 writel(OPT_FRAME_SIZE | 0x04, fep->hwp + FEC_R_CNTRL);
1188 writel(0x04, fep->hwp + FEC_X_CNTRL);
Philippe De Muyterf909b1e2007-10-23 14:37:54 +10001189 } else {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001190 /* MII enable / No Rcv on Xmit */
1191 writel(OPT_FRAME_SIZE | 0x06, fep->hwp + FEC_R_CNTRL);
1192 writel(0x0, fep->hwp + FEC_X_CNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
1194 fep->full_duplex = duplex;
1195
Sascha Hauer22f6b862009-04-15 01:32:18 +00001196 /* Set MII speed */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001197 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Baruch Siach5eb32bd2010-05-24 00:36:13 -07001199#ifdef FEC_MIIGSK_ENR
1200 if (fep->phy_interface == PHY_INTERFACE_MODE_RMII) {
1201 /* disable the gasket and wait */
1202 writel(0, fep->hwp + FEC_MIIGSK_ENR);
1203 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1204 udelay(1);
1205
1206 /* configure the gasket: RMII, 50 MHz, no loopback, no echo */
1207 writel(1, fep->hwp + FEC_MIIGSK_CFGR);
1208
1209 /* re-enable the gasket */
1210 writel(2, fep->hwp + FEC_MIIGSK_ENR);
1211 }
1212#endif
1213
Sascha Hauer22f6b862009-04-15 01:32:18 +00001214 /* And last, enable the transmit and receive processing */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001215 writel(2, fep->hwp + FEC_ECNTRL);
1216 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Matt Waddel6b265292006-06-27 13:10:56 +10001217
Sascha Hauer22f6b862009-04-15 01:32:18 +00001218 /* Enable interrupts we wish to service */
Bryan Wue6b043d2010-03-31 02:10:44 +00001219 writel(FEC_ENET_TXF | FEC_ENET_RXF, fep->hwp + FEC_IMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
1222static void
1223fec_stop(struct net_device *dev)
1224{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001225 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Sascha Hauer22f6b862009-04-15 01:32:18 +00001227 /* We cannot expect a graceful transmit stop without link !!! */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001228 if (fep->link) {
1229 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
Philippe De Muyter677177c2006-06-27 13:05:33 +10001230 udelay(10);
Sascha Hauerf44d6302009-04-15 03:11:30 +00001231 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Philippe De Muyter677177c2006-06-27 13:05:33 +10001232 printk("fec_stop : Graceful transmit stop did not complete !\n");
Sascha Hauerf44d6302009-04-15 03:11:30 +00001233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Sascha Hauerf44d6302009-04-15 03:11:30 +00001235 /* Whack a reset. We should wait for this. */
1236 writel(1, fep->hwp + FEC_ECNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 udelay(10);
1238
Sascha Hauerf44d6302009-04-15 03:11:30 +00001239 /* Clear outstanding MII command interrupts. */
1240 writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Sascha Hauerf44d6302009-04-15 03:11:30 +00001242 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
Sascha Haueread73182009-01-28 23:03:11 +00001245static int __devinit
1246fec_probe(struct platform_device *pdev)
1247{
1248 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07001249 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00001250 struct net_device *ndev;
1251 int i, irq, ret = 0;
1252 struct resource *r;
1253
1254 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1255 if (!r)
1256 return -ENXIO;
1257
1258 r = request_mem_region(r->start, resource_size(r), pdev->name);
1259 if (!r)
1260 return -EBUSY;
1261
1262 /* Init network device */
1263 ndev = alloc_etherdev(sizeof(struct fec_enet_private));
1264 if (!ndev)
1265 return -ENOMEM;
1266
1267 SET_NETDEV_DEV(ndev, &pdev->dev);
1268
1269 /* setup board info structure */
1270 fep = netdev_priv(ndev);
1271 memset(fep, 0, sizeof(*fep));
1272
1273 ndev->base_addr = (unsigned long)ioremap(r->start, resource_size(r));
Bryan Wue6b043d2010-03-31 02:10:44 +00001274 fep->pdev = pdev;
Sascha Haueread73182009-01-28 23:03:11 +00001275
1276 if (!ndev->base_addr) {
1277 ret = -ENOMEM;
1278 goto failed_ioremap;
1279 }
1280
1281 platform_set_drvdata(pdev, ndev);
1282
Baruch Siach5eb32bd2010-05-24 00:36:13 -07001283 pdata = pdev->dev.platform_data;
1284 if (pdata)
1285 fep->phy_interface = pdata->phy;
1286
Sascha Haueread73182009-01-28 23:03:11 +00001287 /* This device has up to three irqs on some platforms */
1288 for (i = 0; i < 3; i++) {
1289 irq = platform_get_irq(pdev, i);
1290 if (i && irq < 0)
1291 break;
1292 ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
1293 if (ret) {
1294 while (i >= 0) {
1295 irq = platform_get_irq(pdev, i);
1296 free_irq(irq, ndev);
1297 i--;
1298 }
1299 goto failed_irq;
1300 }
1301 }
1302
1303 fep->clk = clk_get(&pdev->dev, "fec_clk");
1304 if (IS_ERR(fep->clk)) {
1305 ret = PTR_ERR(fep->clk);
1306 goto failed_clk;
1307 }
1308 clk_enable(fep->clk);
1309
1310 ret = fec_enet_init(ndev, 0);
1311 if (ret)
1312 goto failed_init;
1313
Bryan Wue6b043d2010-03-31 02:10:44 +00001314 ret = fec_enet_mii_init(pdev);
1315 if (ret)
1316 goto failed_mii_init;
1317
Sascha Haueread73182009-01-28 23:03:11 +00001318 ret = register_netdev(ndev);
1319 if (ret)
1320 goto failed_register;
1321
1322 return 0;
1323
1324failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00001325 fec_enet_mii_remove(fep);
1326failed_mii_init:
Sascha Haueread73182009-01-28 23:03:11 +00001327failed_init:
1328 clk_disable(fep->clk);
1329 clk_put(fep->clk);
1330failed_clk:
1331 for (i = 0; i < 3; i++) {
1332 irq = platform_get_irq(pdev, i);
1333 if (irq > 0)
1334 free_irq(irq, ndev);
1335 }
1336failed_irq:
1337 iounmap((void __iomem *)ndev->base_addr);
1338failed_ioremap:
1339 free_netdev(ndev);
1340
1341 return ret;
1342}
1343
1344static int __devexit
1345fec_drv_remove(struct platform_device *pdev)
1346{
1347 struct net_device *ndev = platform_get_drvdata(pdev);
1348 struct fec_enet_private *fep = netdev_priv(ndev);
1349
1350 platform_set_drvdata(pdev, NULL);
1351
1352 fec_stop(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001353 fec_enet_mii_remove(fep);
Sascha Haueread73182009-01-28 23:03:11 +00001354 clk_disable(fep->clk);
1355 clk_put(fep->clk);
1356 iounmap((void __iomem *)ndev->base_addr);
1357 unregister_netdev(ndev);
1358 free_netdev(ndev);
1359 return 0;
1360}
1361
Denis Kirjanov59d42892010-06-02 09:27:04 +00001362#ifdef CONFIG_PM
Sascha Haueread73182009-01-28 23:03:11 +00001363static int
Eric Benard87cad5c2010-06-18 04:19:54 +00001364fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00001365{
Eric Benard87cad5c2010-06-18 04:19:54 +00001366 struct net_device *ndev = dev_get_drvdata(dev);
Sascha Haueread73182009-01-28 23:03:11 +00001367 struct fec_enet_private *fep;
1368
1369 if (ndev) {
1370 fep = netdev_priv(ndev);
Eric Bénarde3fe8552010-06-02 06:13:34 -07001371 if (netif_running(ndev))
1372 fec_enet_close(ndev);
1373 clk_disable(fep->clk);
Sascha Haueread73182009-01-28 23:03:11 +00001374 }
1375 return 0;
1376}
1377
1378static int
Eric Benard87cad5c2010-06-18 04:19:54 +00001379fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00001380{
Eric Benard87cad5c2010-06-18 04:19:54 +00001381 struct net_device *ndev = dev_get_drvdata(dev);
Eric Bénarde3fe8552010-06-02 06:13:34 -07001382 struct fec_enet_private *fep;
Sascha Haueread73182009-01-28 23:03:11 +00001383
1384 if (ndev) {
Eric Bénarde3fe8552010-06-02 06:13:34 -07001385 fep = netdev_priv(ndev);
1386 clk_enable(fep->clk);
1387 if (netif_running(ndev))
1388 fec_enet_open(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001389 }
1390 return 0;
1391}
1392
Denis Kirjanov59d42892010-06-02 09:27:04 +00001393static const struct dev_pm_ops fec_pm_ops = {
1394 .suspend = fec_suspend,
1395 .resume = fec_resume,
1396 .freeze = fec_suspend,
1397 .thaw = fec_resume,
1398 .poweroff = fec_suspend,
1399 .restore = fec_resume,
1400};
Eric Benard87cad5c2010-06-18 04:19:54 +00001401#endif
Denis Kirjanov59d42892010-06-02 09:27:04 +00001402
Sascha Haueread73182009-01-28 23:03:11 +00001403static struct platform_driver fec_driver = {
1404 .driver = {
Eric Benard87cad5c2010-06-18 04:19:54 +00001405 .name = "fec",
1406 .owner = THIS_MODULE,
1407#ifdef CONFIG_PM
1408 .pm = &fec_pm_ops,
1409#endif
Sascha Haueread73182009-01-28 23:03:11 +00001410 },
Eric Benard87cad5c2010-06-18 04:19:54 +00001411 .probe = fec_probe,
1412 .remove = __devexit_p(fec_drv_remove),
Sascha Haueread73182009-01-28 23:03:11 +00001413};
1414
1415static int __init
1416fec_enet_module_init(void)
1417{
1418 printk(KERN_INFO "FEC Ethernet Driver\n");
1419
1420 return platform_driver_register(&fec_driver);
1421}
1422
1423static void __exit
1424fec_enet_cleanup(void)
1425{
1426 platform_driver_unregister(&fec_driver);
1427}
1428
1429module_exit(fec_enet_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430module_init(fec_enet_module_init);
1431
1432MODULE_LICENSE("GPL");