blob: 7b3e331dd442d0d5fed51fae388c22a1cc7ba5ea [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
3 * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
4 *
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10005 * Right now, I am very wasteful with the buffers. I allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * pages and then divide them into 2K frame buffers. This way I know I
7 * have buffers large enough to hold one frame within one buffer descriptor.
8 * Once I get this working, I will use 64 or 128 byte CPM buffers, which
9 * will be much more memory efficient and will easily handle lots of
10 * small packets.
11 *
12 * Much better multiple PHY support by Magnus Damm.
13 * Copyright (c) 2000 Ericsson Radio Systems AB.
14 *
Greg Ungerer562d2f82005-11-07 14:09:50 +100015 * Support for FEC controller of ColdFire processors.
16 * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +100017 *
18 * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
Philippe De Muyter677177c2006-06-27 13:05:33 +100019 * Copyright (c) 2004-2006 Macq Electronique SA.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/string.h>
25#include <linux/ptrace.h>
26#include <linux/errno.h>
27#include <linux/ioport.h>
28#include <linux/slab.h>
29#include <linux/interrupt.h>
30#include <linux/pci.h>
31#include <linux/init.h>
32#include <linux/delay.h>
33#include <linux/netdevice.h>
34#include <linux/etherdevice.h>
35#include <linux/skbuff.h>
36#include <linux/spinlock.h>
37#include <linux/workqueue.h>
38#include <linux/bitops.h>
Sascha Hauer6f501b12009-01-28 23:03:05 +000039#include <linux/io.h>
40#include <linux/irq.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000041#include <linux/clk.h>
Sascha Haueread73182009-01-28 23:03:11 +000042#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Greg Ungerer080853a2007-07-30 16:28:46 +100044#include <asm/cacheflush.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000045
46#ifndef CONFIG_ARCH_MXC
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/coldfire.h>
48#include <asm/mcfsim.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000049#endif
Sascha Hauer6f501b12009-01-28 23:03:05 +000050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include "fec.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Sascha Hauer196719e2009-01-28 23:03:10 +000053#ifdef CONFIG_ARCH_MXC
54#include <mach/hardware.h>
55#define FEC_ALIGNMENT 0xf
56#else
57#define FEC_ALIGNMENT 0x3
58#endif
59
Sascha Haueread73182009-01-28 23:03:11 +000060/*
61 * Define the fixed address of the FEC hardware.
62 */
Greg Ungerer87f4abb2008-06-06 15:55:36 +100063#if defined(CONFIG_M5272)
Sebastian Siewiorc1d96152008-05-01 14:04:02 +100064#define HAVE_mii_link_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66static unsigned char fec_mac_default[] = {
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68};
69
70/*
71 * Some hardware gets it MAC address out of local flash memory.
72 * if this is non-zero then assume it is the address to get MAC from.
73 */
74#if defined(CONFIG_NETtel)
75#define FEC_FLASHMAC 0xf0006006
76#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
77#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#elif defined(CONFIG_CANCam)
79#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +100080#elif defined (CONFIG_M5272C3)
81#define FEC_FLASHMAC (0xffe04000 + 4)
82#elif defined(CONFIG_MOD5272)
83#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#else
85#define FEC_FLASHMAC 0
86#endif
Greg Ungerer43be6362009-02-26 22:42:51 -080087#endif /* CONFIG_M5272 */
Sascha Haueread73182009-01-28 23:03:11 +000088
Sascha Hauer22f6b862009-04-15 01:32:18 +000089/* Forward declarations of some structures to support different PHYs */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91typedef struct {
92 uint mii_data;
93 void (*funct)(uint mii_reg, struct net_device *dev);
94} phy_cmd_t;
95
96typedef struct {
97 uint id;
98 char *name;
99
100 const phy_cmd_t *config;
101 const phy_cmd_t *startup;
102 const phy_cmd_t *ack_int;
103 const phy_cmd_t *shutdown;
104} phy_info_t;
105
106/* The number of Tx and Rx buffers. These are allocated from the page
107 * pool. The code may assume these are power of two, so it it best
108 * to keep them that size.
109 * We don't need to allocate pages for the transmitter. We just use
110 * the skbuffer directly.
111 */
112#define FEC_ENET_RX_PAGES 8
113#define FEC_ENET_RX_FRSIZE 2048
114#define FEC_ENET_RX_FRPPG (PAGE_SIZE / FEC_ENET_RX_FRSIZE)
115#define RX_RING_SIZE (FEC_ENET_RX_FRPPG * FEC_ENET_RX_PAGES)
116#define FEC_ENET_TX_FRSIZE 2048
117#define FEC_ENET_TX_FRPPG (PAGE_SIZE / FEC_ENET_TX_FRSIZE)
118#define TX_RING_SIZE 16 /* Must be power of two */
119#define TX_RING_MOD_MASK 15 /* for this to work */
120
Greg Ungerer562d2f82005-11-07 14:09:50 +1000121#if (((RX_RING_SIZE + TX_RING_SIZE) * 8) > PAGE_SIZE)
Matt Waddel6b265292006-06-27 13:10:56 +1000122#error "FEC: descriptor ring size constants too large"
Greg Ungerer562d2f82005-11-07 14:09:50 +1000123#endif
124
Sascha Hauer22f6b862009-04-15 01:32:18 +0000125/* Interrupt events/masks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
127#define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
128#define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
129#define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
130#define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
131#define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
132#define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
133#define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
134#define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
135#define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
136
137/* The FEC stores dest/src/type, data, and checksum for receive packets.
138 */
139#define PKT_MAXBUF_SIZE 1518
140#define PKT_MINBUF_SIZE 64
141#define PKT_MAXBLR_SIZE 1520
142
143
144/*
Matt Waddel6b265292006-06-27 13:10:56 +1000145 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 * size bits. Other FEC hardware does not, so we need to take that into
147 * account when setting it.
148 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000149#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Sascha Hauer196719e2009-01-28 23:03:10 +0000150 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARCH_MXC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
152#else
153#define OPT_FRAME_SIZE 0
154#endif
155
156/* The FEC buffer descriptors track the ring buffers. The rx_bd_base and
157 * tx_bd_base always point to the base of the buffer descriptors. The
158 * cur_rx and cur_tx point to the currently available buffer.
159 * The dirty_tx tracks the current buffer that is being sent by the
160 * controller. The cur_tx and dirty_tx are equal under both completely
161 * empty and completely full conditions. The empty/ready indicator in
162 * the buffer descriptor determines the actual condition.
163 */
164struct fec_enet_private {
165 /* Hardware registers of the FEC device */
Sascha Hauerf44d6302009-04-15 03:11:30 +0000166 void __iomem *hwp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Greg Ungerercb84d6e2007-07-30 16:29:09 +1000168 struct net_device *netdev;
169
Sascha Haueread73182009-01-28 23:03:11 +0000170 struct clk *clk;
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 /* The saved address of a sent-in-place packet/buffer, for skfree(). */
173 unsigned char *tx_bounce[TX_RING_SIZE];
174 struct sk_buff* tx_skbuff[TX_RING_SIZE];
175 ushort skb_cur;
176 ushort skb_dirty;
177
Sascha Hauer22f6b862009-04-15 01:32:18 +0000178 /* CPM dual port RAM relative addresses */
Sascha Hauer4661e752009-01-28 23:03:07 +0000179 dma_addr_t bd_dma;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000180 /* Address of Rx and Tx buffers */
Sascha Hauer2e285322009-04-15 01:32:16 +0000181 struct bufdesc *rx_bd_base;
182 struct bufdesc *tx_bd_base;
183 /* The next free ring entry */
184 struct bufdesc *cur_rx, *cur_tx;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000185 /* The ring entries to be free()ed */
Sascha Hauer2e285322009-04-15 01:32:16 +0000186 struct bufdesc *dirty_tx;
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 uint tx_full;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000189 /* hold while accessing the HW like ringbuffer for tx/rx but not MAC */
190 spinlock_t hw_lock;
191 /* hold while accessing the mii_list_t() elements */
192 spinlock_t mii_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 uint phy_id;
195 uint phy_id_done;
196 uint phy_status;
197 uint phy_speed;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000198 phy_info_t const *phy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 struct work_struct phy_task;
200
201 uint sequence_done;
202 uint mii_phy_task_queued;
203
204 uint phy_addr;
205
206 int index;
207 int opened;
208 int link;
209 int old_link;
210 int full_duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211};
212
213static int fec_enet_open(struct net_device *dev);
214static int fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev);
215static void fec_enet_mii(struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100216static irqreturn_t fec_enet_interrupt(int irq, void * dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217static void fec_enet_tx(struct net_device *dev);
218static void fec_enet_rx(struct net_device *dev);
219static int fec_enet_close(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220static void set_multicast_list(struct net_device *dev);
221static void fec_restart(struct net_device *dev, int duplex);
222static void fec_stop(struct net_device *dev);
223static void fec_set_mac_address(struct net_device *dev);
224
225
226/* MII processing. We keep this as simple as possible. Requests are
227 * placed on the list (if there is room). When the request is finished
228 * by the MII, an optional function may be called.
229 */
230typedef struct mii_list {
231 uint mii_regval;
232 void (*mii_func)(uint val, struct net_device *dev);
233 struct mii_list *mii_next;
234} mii_list_t;
235
236#define NMII 20
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000237static mii_list_t mii_cmds[NMII];
238static mii_list_t *mii_free;
239static mii_list_t *mii_head;
240static mii_list_t *mii_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400242static int mii_queue(struct net_device *dev, int request,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 void (*func)(uint, struct net_device *));
244
Sascha Hauer22f6b862009-04-15 01:32:18 +0000245/* Make MII read/write commands for the FEC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246#define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18))
247#define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | \
248 (VAL & 0xffff))
249#define mk_mii_end 0
250
Sascha Hauer22f6b862009-04-15 01:32:18 +0000251/* Transmitter timeout */
252#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Sascha Hauer22f6b862009-04-15 01:32:18 +0000254/* Register definitions for the PHY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256#define MII_REG_CR 0 /* Control Register */
257#define MII_REG_SR 1 /* Status Register */
258#define MII_REG_PHYIR1 2 /* PHY Identification Register 1 */
259#define MII_REG_PHYIR2 3 /* PHY Identification Register 2 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400260#define MII_REG_ANAR 4 /* A-N Advertisement Register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261#define MII_REG_ANLPAR 5 /* A-N Link Partner Ability Register */
262#define MII_REG_ANER 6 /* A-N Expansion Register */
263#define MII_REG_ANNPTR 7 /* A-N Next Page Transmit Register */
264#define MII_REG_ANLPRNPR 8 /* A-N Link Partner Received Next Page Reg. */
265
266/* values for phy_status */
267
268#define PHY_CONF_ANE 0x0001 /* 1 auto-negotiation enabled */
269#define PHY_CONF_LOOP 0x0002 /* 1 loopback mode enabled */
270#define PHY_CONF_SPMASK 0x00f0 /* mask for speed */
271#define PHY_CONF_10HDX 0x0010 /* 10 Mbit half duplex supported */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400272#define PHY_CONF_10FDX 0x0020 /* 10 Mbit full duplex supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273#define PHY_CONF_100HDX 0x0040 /* 100 Mbit half duplex supported */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400274#define PHY_CONF_100FDX 0x0080 /* 100 Mbit full duplex supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276#define PHY_STAT_LINK 0x0100 /* 1 up - 0 down */
277#define PHY_STAT_FAULT 0x0200 /* 1 remote fault */
278#define PHY_STAT_ANC 0x0400 /* 1 auto-negotiation complete */
279#define PHY_STAT_SPMASK 0xf000 /* mask for speed */
280#define PHY_STAT_10HDX 0x1000 /* 10 Mbit half duplex selected */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400281#define PHY_STAT_10FDX 0x2000 /* 10 Mbit full duplex selected */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282#define PHY_STAT_100HDX 0x4000 /* 100 Mbit half duplex selected */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400283#define PHY_STAT_100FDX 0x8000 /* 100 Mbit full duplex selected */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285
286static int
287fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
288{
Sascha Hauerf44d6302009-04-15 03:11:30 +0000289 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000290 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000291 unsigned short status;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000292 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (!fep->link) {
295 /* Link is down or autonegotiation is in progress. */
296 return 1;
297 }
298
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000299 spin_lock_irqsave(&fep->hw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 /* Fill in a Tx ring entry */
301 bdp = fep->cur_tx;
302
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000303 status = bdp->cbd_sc;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000304
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000305 if (status & BD_ENET_TX_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 /* Ooops. All transmit buffers are full. Bail out.
307 * This should not happen, since dev->tbusy should be set.
308 */
309 printk("%s: tx queue full!.\n", dev->name);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000310 spin_unlock_irqrestore(&fep->hw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return 1;
312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Sascha Hauer22f6b862009-04-15 01:32:18 +0000314 /* Clear all of the status flags */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000315 status &= ~BD_ENET_TX_STATS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Sascha Hauer22f6b862009-04-15 01:32:18 +0000317 /* Set buffer length and buffer pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 bdp->cbd_bufaddr = __pa(skb->data);
319 bdp->cbd_datlen = skb->len;
320
321 /*
Sascha Hauer22f6b862009-04-15 01:32:18 +0000322 * On some FEC implementations data must be aligned on
323 * 4-byte boundaries. Use bounce buffers to copy data
324 * and get it aligned. Ugh.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 */
Sascha Hauer196719e2009-01-28 23:03:10 +0000326 if (bdp->cbd_bufaddr & FEC_ALIGNMENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 unsigned int index;
328 index = bdp - fep->tx_bd_base;
Sascha Hauer6989f512009-01-28 23:03:06 +0000329 memcpy(fep->tx_bounce[index], (void *)skb->data, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 bdp->cbd_bufaddr = __pa(fep->tx_bounce[index]);
331 }
332
Sascha Hauer22f6b862009-04-15 01:32:18 +0000333 /* Save skb pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 fep->tx_skbuff[fep->skb_cur] = skb;
335
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700336 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 fep->skb_cur = (fep->skb_cur+1) & TX_RING_MOD_MASK;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 /* Push the data cache so the CPM does not get stale memory
340 * data.
341 */
Sascha Hauerccdc4f12009-01-28 23:03:09 +0000342 dma_sync_single(NULL, bdp->cbd_bufaddr,
343 bdp->cbd_datlen, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000345 /* Send it on its way. Tell FEC it's ready, interrupt when done,
346 * it's the last BD of the frame, and to put the CRC on the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000348 status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 | BD_ENET_TX_LAST | BD_ENET_TX_TC);
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000350 bdp->cbd_sc = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 dev->trans_start = jiffies;
353
354 /* Trigger transmission start */
Sascha Hauerf44d6302009-04-15 03:11:30 +0000355 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Sascha Hauer22f6b862009-04-15 01:32:18 +0000357 /* If this was the last BD in the ring, start at the beginning again. */
358 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 bdp = fep->tx_bd_base;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000360 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 bdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 if (bdp == fep->dirty_tx) {
364 fep->tx_full = 1;
365 netif_stop_queue(dev);
366 }
367
Sascha Hauer2e285322009-04-15 01:32:16 +0000368 fep->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000370 spin_unlock_irqrestore(&fep->hw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 return 0;
373}
374
375static void
376fec_timeout(struct net_device *dev)
377{
378 struct fec_enet_private *fep = netdev_priv(dev);
379
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700380 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000382 fec_restart(dev, fep->full_duplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 netif_wake_queue(dev);
384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100387fec_enet_interrupt(int irq, void * dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 struct net_device *dev = dev_id;
Sascha Hauerf44d6302009-04-15 03:11:30 +0000390 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 uint int_events;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000392 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000394 do {
Sascha Hauerf44d6302009-04-15 03:11:30 +0000395 int_events = readl(fep->hwp + FEC_IEVENT);
396 writel(int_events, fep->hwp + FEC_IEVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (int_events & FEC_ENET_RXF) {
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000399 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 fec_enet_rx(dev);
401 }
402
403 /* Transmit OK, or non-fatal error. Update the buffer
Sascha Hauerf44d6302009-04-15 03:11:30 +0000404 * descriptors. FEC handles all errors, we just discover
405 * them as part of the transmit process.
406 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (int_events & FEC_ENET_TXF) {
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000408 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 fec_enet_tx(dev);
410 }
411
412 if (int_events & FEC_ENET_MII) {
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000413 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 fec_enet_mii(dev);
415 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400416
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000417 } while (int_events);
418
419 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422
423static void
424fec_enet_tx(struct net_device *dev)
425{
426 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +0000427 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000428 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 struct sk_buff *skb;
430
431 fep = netdev_priv(dev);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000432 spin_lock_irq(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 bdp = fep->dirty_tx;
434
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000435 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (bdp == fep->cur_tx && fep->tx_full == 0) break;
437
438 skb = fep->tx_skbuff[fep->skb_dirty];
439 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000440 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 BD_ENET_TX_RL | BD_ENET_TX_UN |
442 BD_ENET_TX_CSL)) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700443 dev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000444 if (status & BD_ENET_TX_HB) /* No heartbeat */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700445 dev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000446 if (status & BD_ENET_TX_LC) /* Late collision */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700447 dev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000448 if (status & BD_ENET_TX_RL) /* Retrans limit */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700449 dev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000450 if (status & BD_ENET_TX_UN) /* Underrun */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700451 dev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000452 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700453 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 } else {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700455 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000458 if (status & BD_ENET_TX_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 printk("HEY! Enet xmit interrupt and TX_READY.\n");
Sascha Hauer22f6b862009-04-15 01:32:18 +0000460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 /* Deferred means some collisions occurred during transmit,
462 * but we eventually sent the packet OK.
463 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000464 if (status & BD_ENET_TX_DEF)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700465 dev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400466
Sascha Hauer22f6b862009-04-15 01:32:18 +0000467 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 dev_kfree_skb_any(skb);
469 fep->tx_skbuff[fep->skb_dirty] = NULL;
470 fep->skb_dirty = (fep->skb_dirty + 1) & TX_RING_MOD_MASK;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400471
Sascha Hauer22f6b862009-04-15 01:32:18 +0000472 /* Update pointer to next buffer descriptor to be transmitted */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000473 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 bdp = fep->tx_bd_base;
475 else
476 bdp++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400477
Sascha Hauer22f6b862009-04-15 01:32:18 +0000478 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 */
480 if (fep->tx_full) {
481 fep->tx_full = 0;
482 if (netif_queue_stopped(dev))
483 netif_wake_queue(dev);
484 }
485 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000486 fep->dirty_tx = bdp;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000487 spin_unlock_irq(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
490
491/* During a receive, the cur_rx points to the current incoming buffer.
492 * When we update through the ring, if the next incoming buffer has
493 * not been given to the system, we just set the empty indicator,
494 * effectively tossing the packet.
495 */
496static void
497fec_enet_rx(struct net_device *dev)
498{
Sascha Hauerf44d6302009-04-15 03:11:30 +0000499 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000500 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000501 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct sk_buff *skb;
503 ushort pkt_len;
504 __u8 *data;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400505
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000506#ifdef CONFIG_M532x
507 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400508#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000510 spin_lock_irq(&fep->hw_lock);
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 /* First, grab all of the stats for the incoming packet.
513 * These get messed up if we get called due to a busy condition.
514 */
515 bdp = fep->cur_rx;
516
Sascha Hauer22f6b862009-04-15 01:32:18 +0000517 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Sascha Hauer22f6b862009-04-15 01:32:18 +0000519 /* Since we have allocated space to hold a complete frame,
520 * the last indicator should be set.
521 */
522 if ((status & BD_ENET_RX_LAST) == 0)
523 printk("FEC ENET: rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Sascha Hauer22f6b862009-04-15 01:32:18 +0000525 if (!fep->opened)
526 goto rx_processing_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Sascha Hauer22f6b862009-04-15 01:32:18 +0000528 /* Check for errors. */
529 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Sascha Hauer22f6b862009-04-15 01:32:18 +0000531 dev->stats.rx_errors++;
532 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
533 /* Frame too long or too short. */
534 dev->stats.rx_length_errors++;
535 }
536 if (status & BD_ENET_RX_NO) /* Frame alignment */
537 dev->stats.rx_frame_errors++;
538 if (status & BD_ENET_RX_CR) /* CRC Error */
539 dev->stats.rx_crc_errors++;
540 if (status & BD_ENET_RX_OV) /* FIFO overrun */
541 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
Sascha Hauer22f6b862009-04-15 01:32:18 +0000543
544 /* Report late collisions as a frame error.
545 * On this error, the BD is closed, but we don't know what we
546 * have in the buffer. So, just drop this frame on the floor.
547 */
548 if (status & BD_ENET_RX_CL) {
549 dev->stats.rx_errors++;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700550 dev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000551 goto rx_processing_done;
552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Sascha Hauer22f6b862009-04-15 01:32:18 +0000554 /* Process the incoming frame. */
555 dev->stats.rx_packets++;
556 pkt_len = bdp->cbd_datlen;
557 dev->stats.rx_bytes += pkt_len;
558 data = (__u8*)__va(bdp->cbd_bufaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Sascha Hauer22f6b862009-04-15 01:32:18 +0000560 dma_sync_single(NULL, (unsigned long)__pa(data),
Sascha Hauerccdc4f12009-01-28 23:03:09 +0000561 pkt_len - 4, DMA_FROM_DEVICE);
562
Sascha Hauer22f6b862009-04-15 01:32:18 +0000563 /* This does 16 byte alignment, exactly what we need.
564 * The packet length includes FCS, but we don't want to
565 * include that when passing upstream as it messes up
566 * bridging applications.
567 */
Sascha Hauer85498892009-04-15 01:32:21 +0000568 skb = dev_alloc_skb(pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Sascha Hauer85498892009-04-15 01:32:21 +0000570 if (unlikely(!skb)) {
Sascha Hauer22f6b862009-04-15 01:32:18 +0000571 printk("%s: Memory squeeze, dropping packet.\n",
572 dev->name);
573 dev->stats.rx_dropped++;
574 } else {
Sascha Hauer85498892009-04-15 01:32:21 +0000575 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000576 skb_put(skb, pkt_len - 4); /* Make room */
577 skb_copy_to_linear_data(skb, data, pkt_len - 4);
578 skb->protocol = eth_type_trans(skb, dev);
579 netif_rx(skb);
580 }
581rx_processing_done:
582 /* Clear the status flags for this buffer */
583 status &= ~BD_ENET_RX_STATS;
584
585 /* Mark the buffer empty */
586 status |= BD_ENET_RX_EMPTY;
587 bdp->cbd_sc = status;
588
589 /* Update BD pointer to next entry */
590 if (status & BD_ENET_RX_WRAP)
591 bdp = fep->rx_bd_base;
592 else
593 bdp++;
594 /* Doing this here will keep the FEC running while we process
595 * incoming frames. On a heavily loaded network, we should be
596 * able to keep up at the expense of system resources.
597 */
598 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000600 fep->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000602 spin_unlock_irq(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000605/* called from interrupt context */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606static void
607fec_enet_mii(struct net_device *dev)
608{
609 struct fec_enet_private *fep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 mii_list_t *mip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 fep = netdev_priv(dev);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000613 spin_lock_irq(&fep->mii_lock);
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if ((mip = mii_head) == NULL) {
616 printk("MII and no head!\n");
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000617 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619
620 if (mip->mii_func != NULL)
Sascha Hauerf44d6302009-04-15 03:11:30 +0000621 (*(mip->mii_func))(readl(fep->hwp + FEC_MII_DATA), dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 mii_head = mip->mii_next;
624 mip->mii_next = mii_free;
625 mii_free = mip;
626
627 if ((mip = mii_head) != NULL)
Sascha Hauerf44d6302009-04-15 03:11:30 +0000628 writel(mip->mii_regval, fep->hwp + FEC_MII_DATA);
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000629
630unlock:
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000631 spin_unlock_irq(&fep->mii_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
634static int
635mii_queue(struct net_device *dev, int regval, void (*func)(uint, struct net_device *))
636{
637 struct fec_enet_private *fep;
638 unsigned long flags;
639 mii_list_t *mip;
640 int retval;
641
Sascha Hauer22f6b862009-04-15 01:32:18 +0000642 /* Add PHY address to register command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 fep = netdev_priv(dev);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000644 spin_lock_irqsave(&fep->mii_lock, flags);
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 regval |= fep->phy_addr << 23;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 retval = 0;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if ((mip = mii_free) != NULL) {
650 mii_free = mip->mii_next;
651 mip->mii_regval = regval;
652 mip->mii_func = func;
653 mip->mii_next = NULL;
654 if (mii_head) {
655 mii_tail->mii_next = mip;
656 mii_tail = mip;
Philippe De Muyterf909b1e2007-10-23 14:37:54 +1000657 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 mii_head = mii_tail = mip;
Sascha Hauerf44d6302009-04-15 03:11:30 +0000659 writel(regval, fep->hwp + FEC_MII_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Philippe De Muyterf909b1e2007-10-23 14:37:54 +1000661 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 retval = 1;
663 }
664
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000665 spin_unlock_irqrestore(&fep->mii_lock, flags);
666 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
669static void mii_do_cmd(struct net_device *dev, const phy_cmd_t *c)
670{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if(!c)
672 return;
673
Philippe De Muyterbe6cb662007-10-23 14:37:54 +1000674 for (; c->mii_data != mk_mii_end; c++)
675 mii_queue(dev, c->mii_data, c->funct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
678static void mii_parse_sr(uint mii_reg, struct net_device *dev)
679{
680 struct fec_enet_private *fep = netdev_priv(dev);
681 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000682 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000684 status = *s & ~(PHY_STAT_LINK | PHY_STAT_FAULT | PHY_STAT_ANC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 if (mii_reg & 0x0004)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000687 status |= PHY_STAT_LINK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (mii_reg & 0x0010)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000689 status |= PHY_STAT_FAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (mii_reg & 0x0020)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000691 status |= PHY_STAT_ANC;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000692 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
695static void mii_parse_cr(uint mii_reg, struct net_device *dev)
696{
697 struct fec_enet_private *fep = netdev_priv(dev);
698 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000699 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000701 status = *s & ~(PHY_CONF_ANE | PHY_CONF_LOOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 if (mii_reg & 0x1000)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000704 status |= PHY_CONF_ANE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (mii_reg & 0x4000)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000706 status |= PHY_CONF_LOOP;
707 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710static void mii_parse_anar(uint mii_reg, struct net_device *dev)
711{
712 struct fec_enet_private *fep = netdev_priv(dev);
713 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000714 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000716 status = *s & ~(PHY_CONF_SPMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 if (mii_reg & 0x0020)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000719 status |= PHY_CONF_10HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (mii_reg & 0x0040)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000721 status |= PHY_CONF_10FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (mii_reg & 0x0080)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000723 status |= PHY_CONF_100HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (mii_reg & 0x00100)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000725 status |= PHY_CONF_100FDX;
726 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
729/* ------------------------------------------------------------------------- */
730/* The Level one LXT970 is used by many boards */
731
732#define MII_LXT970_MIRROR 16 /* Mirror register */
733#define MII_LXT970_IER 17 /* Interrupt Enable Register */
734#define MII_LXT970_ISR 18 /* Interrupt Status Register */
735#define MII_LXT970_CONFIG 19 /* Configuration Register */
736#define MII_LXT970_CSR 20 /* Chip Status Register */
737
738static void mii_parse_lxt970_csr(uint mii_reg, struct net_device *dev)
739{
740 struct fec_enet_private *fep = netdev_priv(dev);
741 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000742 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000744 status = *s & ~(PHY_STAT_SPMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (mii_reg & 0x0800) {
746 if (mii_reg & 0x1000)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000747 status |= PHY_STAT_100FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000749 status |= PHY_STAT_100HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 } else {
751 if (mii_reg & 0x1000)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000752 status |= PHY_STAT_10FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000754 status |= PHY_STAT_10HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000756 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000759static phy_cmd_t const phy_cmd_lxt970_config[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 { mk_mii_read(MII_REG_CR), mii_parse_cr },
761 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
762 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000763 };
764static phy_cmd_t const phy_cmd_lxt970_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 { mk_mii_write(MII_LXT970_IER, 0x0002), NULL },
766 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
767 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000768 };
769static phy_cmd_t const phy_cmd_lxt970_ack_int[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 /* read SR and ISR to acknowledge */
771 { mk_mii_read(MII_REG_SR), mii_parse_sr },
772 { mk_mii_read(MII_LXT970_ISR), NULL },
773
774 /* find out the current status */
775 { mk_mii_read(MII_LXT970_CSR), mii_parse_lxt970_csr },
776 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000777 };
778static phy_cmd_t const phy_cmd_lxt970_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 { mk_mii_write(MII_LXT970_IER, 0x0000), NULL },
780 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000781 };
782static phy_info_t const phy_info_lxt970 = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400783 .id = 0x07810000,
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000784 .name = "LXT970",
785 .config = phy_cmd_lxt970_config,
786 .startup = phy_cmd_lxt970_startup,
787 .ack_int = phy_cmd_lxt970_ack_int,
788 .shutdown = phy_cmd_lxt970_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789};
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791/* ------------------------------------------------------------------------- */
792/* The Level one LXT971 is used on some of my custom boards */
793
794/* register definitions for the 971 */
795
796#define MII_LXT971_PCR 16 /* Port Control Register */
797#define MII_LXT971_SR2 17 /* Status Register 2 */
798#define MII_LXT971_IER 18 /* Interrupt Enable Register */
799#define MII_LXT971_ISR 19 /* Interrupt Status Register */
800#define MII_LXT971_LCR 20 /* LED Control Register */
801#define MII_LXT971_TCR 30 /* Transmit Control Register */
802
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400803/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 * I had some nice ideas of running the MDIO faster...
805 * The 971 should support 8MHz and I tried it, but things acted really
806 * weird, so 2.5 MHz ought to be enough for anyone...
807 */
808
809static void mii_parse_lxt971_sr2(uint mii_reg, struct net_device *dev)
810{
811 struct fec_enet_private *fep = netdev_priv(dev);
812 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000813 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000815 status = *s & ~(PHY_STAT_SPMASK | PHY_STAT_LINK | PHY_STAT_ANC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 if (mii_reg & 0x0400) {
818 fep->link = 1;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000819 status |= PHY_STAT_LINK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 } else {
821 fep->link = 0;
822 }
823 if (mii_reg & 0x0080)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000824 status |= PHY_STAT_ANC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (mii_reg & 0x4000) {
826 if (mii_reg & 0x0200)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000827 status |= PHY_STAT_100FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000829 status |= PHY_STAT_100HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 } else {
831 if (mii_reg & 0x0200)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000832 status |= PHY_STAT_10FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000834 status |= PHY_STAT_10HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836 if (mii_reg & 0x0008)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000837 status |= PHY_STAT_FAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000839 *s = status;
840}
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400841
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000842static phy_cmd_t const phy_cmd_lxt971_config[] = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400843 /* limit to 10MBit because my prototype board
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 * doesn't work with 100. */
845 { mk_mii_read(MII_REG_CR), mii_parse_cr },
846 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
847 { mk_mii_read(MII_LXT971_SR2), mii_parse_lxt971_sr2 },
848 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000849 };
850static phy_cmd_t const phy_cmd_lxt971_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 { mk_mii_write(MII_LXT971_IER, 0x00f2), NULL },
852 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
853 { mk_mii_write(MII_LXT971_LCR, 0xd422), NULL }, /* LED config */
854 /* Somehow does the 971 tell me that the link is down
855 * the first read after power-up.
856 * read here to get a valid value in ack_int */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400857 { mk_mii_read(MII_REG_SR), mii_parse_sr },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000859 };
860static phy_cmd_t const phy_cmd_lxt971_ack_int[] = {
861 /* acknowledge the int before reading status ! */
862 { mk_mii_read(MII_LXT971_ISR), NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 /* find out the current status */
864 { mk_mii_read(MII_REG_SR), mii_parse_sr },
865 { mk_mii_read(MII_LXT971_SR2), mii_parse_lxt971_sr2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000867 };
868static phy_cmd_t const phy_cmd_lxt971_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 { mk_mii_write(MII_LXT971_IER, 0x0000), NULL },
870 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000871 };
872static phy_info_t const phy_info_lxt971 = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400873 .id = 0x0001378e,
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000874 .name = "LXT971",
875 .config = phy_cmd_lxt971_config,
876 .startup = phy_cmd_lxt971_startup,
877 .ack_int = phy_cmd_lxt971_ack_int,
878 .shutdown = phy_cmd_lxt971_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879};
880
881/* ------------------------------------------------------------------------- */
882/* The Quality Semiconductor QS6612 is used on the RPX CLLF */
883
884/* register definitions */
885
886#define MII_QS6612_MCR 17 /* Mode Control Register */
887#define MII_QS6612_FTR 27 /* Factory Test Register */
888#define MII_QS6612_MCO 28 /* Misc. Control Register */
889#define MII_QS6612_ISR 29 /* Interrupt Source Register */
890#define MII_QS6612_IMR 30 /* Interrupt Mask Register */
891#define MII_QS6612_PCR 31 /* 100BaseTx PHY Control Reg. */
892
893static void mii_parse_qs6612_pcr(uint mii_reg, struct net_device *dev)
894{
895 struct fec_enet_private *fep = netdev_priv(dev);
896 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000897 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000899 status = *s & ~(PHY_STAT_SPMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 switch((mii_reg >> 2) & 7) {
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000902 case 1: status |= PHY_STAT_10HDX; break;
903 case 2: status |= PHY_STAT_100HDX; break;
904 case 5: status |= PHY_STAT_10FDX; break;
905 case 6: status |= PHY_STAT_100FDX; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000908 *s = status;
909}
910
911static phy_cmd_t const phy_cmd_qs6612_config[] = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400912 /* The PHY powers up isolated on the RPX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 * so send a command to allow operation.
914 */
915 { mk_mii_write(MII_QS6612_PCR, 0x0dc0), NULL },
916
917 /* parse cr and anar to get some info */
918 { mk_mii_read(MII_REG_CR), mii_parse_cr },
919 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
920 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000921 };
922static phy_cmd_t const phy_cmd_qs6612_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 { mk_mii_write(MII_QS6612_IMR, 0x003a), NULL },
924 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
925 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000926 };
927static phy_cmd_t const phy_cmd_qs6612_ack_int[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 /* we need to read ISR, SR and ANER to acknowledge */
929 { mk_mii_read(MII_QS6612_ISR), NULL },
930 { mk_mii_read(MII_REG_SR), mii_parse_sr },
931 { mk_mii_read(MII_REG_ANER), NULL },
932
933 /* read pcr to get info */
934 { mk_mii_read(MII_QS6612_PCR), mii_parse_qs6612_pcr },
935 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000936 };
937static phy_cmd_t const phy_cmd_qs6612_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 { mk_mii_write(MII_QS6612_IMR, 0x0000), NULL },
939 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000940 };
941static phy_info_t const phy_info_qs6612 = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400942 .id = 0x00181440,
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000943 .name = "QS6612",
944 .config = phy_cmd_qs6612_config,
945 .startup = phy_cmd_qs6612_startup,
946 .ack_int = phy_cmd_qs6612_ack_int,
947 .shutdown = phy_cmd_qs6612_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948};
949
950/* ------------------------------------------------------------------------- */
951/* AMD AM79C874 phy */
952
953/* register definitions for the 874 */
954
955#define MII_AM79C874_MFR 16 /* Miscellaneous Feature Register */
956#define MII_AM79C874_ICSR 17 /* Interrupt/Status Register */
957#define MII_AM79C874_DR 18 /* Diagnostic Register */
958#define MII_AM79C874_PMLR 19 /* Power and Loopback Register */
959#define MII_AM79C874_MCR 21 /* ModeControl Register */
960#define MII_AM79C874_DC 23 /* Disconnect Counter */
961#define MII_AM79C874_REC 24 /* Recieve Error Counter */
962
963static void mii_parse_am79c874_dr(uint mii_reg, struct net_device *dev)
964{
965 struct fec_enet_private *fep = netdev_priv(dev);
966 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000967 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000969 status = *s & ~(PHY_STAT_SPMASK | PHY_STAT_ANC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 if (mii_reg & 0x0080)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000972 status |= PHY_STAT_ANC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (mii_reg & 0x0400)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000974 status |= ((mii_reg & 0x0800) ? PHY_STAT_100FDX : PHY_STAT_100HDX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000976 status |= ((mii_reg & 0x0800) ? PHY_STAT_10FDX : PHY_STAT_10HDX);
977
978 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000981static phy_cmd_t const phy_cmd_am79c874_config[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 { mk_mii_read(MII_REG_CR), mii_parse_cr },
983 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
984 { mk_mii_read(MII_AM79C874_DR), mii_parse_am79c874_dr },
985 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000986 };
987static phy_cmd_t const phy_cmd_am79c874_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 { mk_mii_write(MII_AM79C874_ICSR, 0xff00), NULL },
989 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400990 { mk_mii_read(MII_REG_SR), mii_parse_sr },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000992 };
993static phy_cmd_t const phy_cmd_am79c874_ack_int[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /* find out the current status */
995 { mk_mii_read(MII_REG_SR), mii_parse_sr },
996 { mk_mii_read(MII_AM79C874_DR), mii_parse_am79c874_dr },
997 /* we only need to read ISR to acknowledge */
998 { mk_mii_read(MII_AM79C874_ICSR), NULL },
999 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001000 };
1001static phy_cmd_t const phy_cmd_am79c874_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 { mk_mii_write(MII_AM79C874_ICSR, 0x0000), NULL },
1003 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001004 };
1005static phy_info_t const phy_info_am79c874 = {
1006 .id = 0x00022561,
1007 .name = "AM79C874",
1008 .config = phy_cmd_am79c874_config,
1009 .startup = phy_cmd_am79c874_startup,
1010 .ack_int = phy_cmd_am79c874_ack_int,
1011 .shutdown = phy_cmd_am79c874_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012};
1013
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015/* ------------------------------------------------------------------------- */
1016/* Kendin KS8721BL phy */
1017
1018/* register definitions for the 8721 */
1019
1020#define MII_KS8721BL_RXERCR 21
Sascha Hauer43268dc2009-01-28 23:03:08 +00001021#define MII_KS8721BL_ICSR 27
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022#define MII_KS8721BL_PHYCR 31
1023
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001024static phy_cmd_t const phy_cmd_ks8721bl_config[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1026 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1027 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001028 };
1029static phy_cmd_t const phy_cmd_ks8721bl_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 { mk_mii_write(MII_KS8721BL_ICSR, 0xff00), NULL },
1031 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001032 { mk_mii_read(MII_REG_SR), mii_parse_sr },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001034 };
1035static phy_cmd_t const phy_cmd_ks8721bl_ack_int[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 /* find out the current status */
1037 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1038 /* we only need to read ISR to acknowledge */
1039 { mk_mii_read(MII_KS8721BL_ICSR), NULL },
1040 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001041 };
1042static phy_cmd_t const phy_cmd_ks8721bl_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 { mk_mii_write(MII_KS8721BL_ICSR, 0x0000), NULL },
1044 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001045 };
1046static phy_info_t const phy_info_ks8721bl = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001047 .id = 0x00022161,
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001048 .name = "KS8721BL",
1049 .config = phy_cmd_ks8721bl_config,
1050 .startup = phy_cmd_ks8721bl_startup,
1051 .ack_int = phy_cmd_ks8721bl_ack_int,
1052 .shutdown = phy_cmd_ks8721bl_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053};
1054
1055/* ------------------------------------------------------------------------- */
Greg Ungerer562d2f82005-11-07 14:09:50 +10001056/* register definitions for the DP83848 */
1057
1058#define MII_DP8384X_PHYSTST 16 /* PHY Status Register */
1059
1060static void mii_parse_dp8384x_sr2(uint mii_reg, struct net_device *dev)
1061{
Wang Chen4cf16532008-11-12 23:38:14 -08001062 struct fec_enet_private *fep = netdev_priv(dev);
Greg Ungerer562d2f82005-11-07 14:09:50 +10001063 volatile uint *s = &(fep->phy_status);
1064
1065 *s &= ~(PHY_STAT_SPMASK | PHY_STAT_LINK | PHY_STAT_ANC);
1066
1067 /* Link up */
1068 if (mii_reg & 0x0001) {
1069 fep->link = 1;
1070 *s |= PHY_STAT_LINK;
1071 } else
1072 fep->link = 0;
1073 /* Status of link */
1074 if (mii_reg & 0x0010) /* Autonegotioation complete */
1075 *s |= PHY_STAT_ANC;
1076 if (mii_reg & 0x0002) { /* 10MBps? */
1077 if (mii_reg & 0x0004) /* Full Duplex? */
1078 *s |= PHY_STAT_10FDX;
1079 else
1080 *s |= PHY_STAT_10HDX;
1081 } else { /* 100 Mbps? */
1082 if (mii_reg & 0x0004) /* Full Duplex? */
1083 *s |= PHY_STAT_100FDX;
1084 else
1085 *s |= PHY_STAT_100HDX;
1086 }
1087 if (mii_reg & 0x0008)
1088 *s |= PHY_STAT_FAULT;
1089}
1090
1091static phy_info_t phy_info_dp83848= {
1092 0x020005c9,
1093 "DP83848",
1094
1095 (const phy_cmd_t []) { /* config */
1096 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1097 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1098 { mk_mii_read(MII_DP8384X_PHYSTST), mii_parse_dp8384x_sr2 },
1099 { mk_mii_end, }
1100 },
1101 (const phy_cmd_t []) { /* startup - enable interrupts */
1102 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
1103 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1104 { mk_mii_end, }
1105 },
1106 (const phy_cmd_t []) { /* ack_int - never happens, no interrupt */
1107 { mk_mii_end, }
1108 },
1109 (const phy_cmd_t []) { /* shutdown */
1110 { mk_mii_end, }
1111 },
1112};
1113
1114/* ------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001116static phy_info_t const * const phy_info[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 &phy_info_lxt970,
1118 &phy_info_lxt971,
1119 &phy_info_qs6612,
1120 &phy_info_am79c874,
1121 &phy_info_ks8721bl,
Greg Ungerer562d2f82005-11-07 14:09:50 +10001122 &phy_info_dp83848,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 NULL
1124};
1125
1126/* ------------------------------------------------------------------------- */
Sebastian Siewiorc1d96152008-05-01 14:04:02 +10001127#ifdef HAVE_mii_link_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001129mii_link_interrupt(int irq, void * dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131/*
Greg Ungerer43be6362009-02-26 22:42:51 -08001132 * This is specific to the MII interrupt setup of the M5272EVB.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 */
Greg Ungerer43be6362009-02-26 22:42:51 -08001134static void __inline__ fec_request_mii_intr(struct net_device *dev)
1135{
1136 if (request_irq(66, mii_link_interrupt, IRQF_DISABLED, "fec(MII)", dev) != 0)
1137 printk("FEC: Could not allocate fec(MII) IRQ(66)!\n");
1138}
1139
1140static void __inline__ fec_disable_phy_intr(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
1142 volatile unsigned long *icrp;
Greg Ungerer43be6362009-02-26 22:42:51 -08001143 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR1);
1144 *icrp = 0x08000000;
1145}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Greg Ungerer43be6362009-02-26 22:42:51 -08001147static void __inline__ fec_phy_ack_intr(void)
1148{
1149 volatile unsigned long *icrp;
1150 /* Acknowledge the interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR1);
Greg Ungererf861d622007-07-30 16:29:16 +10001152 *icrp = 0x0d000000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
Greg Ungerer43be6362009-02-26 22:42:51 -08001155#ifdef CONFIG_M5272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156static void __inline__ fec_get_mac(struct net_device *dev)
1157{
1158 struct fec_enet_private *fep = netdev_priv(dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001159 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001161 if (FEC_FLASHMAC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 /*
1163 * Get MAC address from FLASH.
1164 * If it is all 1's or 0's, use the default.
1165 */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001166 iap = (unsigned char *)FEC_FLASHMAC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if ((iap[0] == 0) && (iap[1] == 0) && (iap[2] == 0) &&
1168 (iap[3] == 0) && (iap[4] == 0) && (iap[5] == 0))
1169 iap = fec_mac_default;
1170 if ((iap[0] == 0xff) && (iap[1] == 0xff) && (iap[2] == 0xff) &&
1171 (iap[3] == 0xff) && (iap[4] == 0xff) && (iap[5] == 0xff))
1172 iap = fec_mac_default;
1173 } else {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001174 *((unsigned long *) &tmpaddr[0]) = readl(fep->hwp + FEC_ADDR_LOW);
1175 *((unsigned short *) &tmpaddr[4]) = (readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 iap = &tmpaddr[0];
1177 }
1178
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001179 memcpy(dev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 /* Adjust MAC if using default MAC address */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001182 if (iap == fec_mac_default)
1183 dev->dev_addr[ETH_ALEN-1] = fec_mac_default[ETH_ALEN-1] + fep->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185#endif
1186
1187/* ------------------------------------------------------------------------- */
1188
1189static void mii_display_status(struct net_device *dev)
1190{
1191 struct fec_enet_private *fep = netdev_priv(dev);
1192 volatile uint *s = &(fep->phy_status);
1193
1194 if (!fep->link && !fep->old_link) {
1195 /* Link is still down - don't print anything */
1196 return;
1197 }
1198
1199 printk("%s: status: ", dev->name);
1200
1201 if (!fep->link) {
1202 printk("link down");
1203 } else {
1204 printk("link up");
1205
1206 switch(*s & PHY_STAT_SPMASK) {
1207 case PHY_STAT_100FDX: printk(", 100MBit Full Duplex"); break;
1208 case PHY_STAT_100HDX: printk(", 100MBit Half Duplex"); break;
1209 case PHY_STAT_10FDX: printk(", 10MBit Full Duplex"); break;
1210 case PHY_STAT_10HDX: printk(", 10MBit Half Duplex"); break;
1211 default:
1212 printk(", Unknown speed/duplex");
1213 }
1214
1215 if (*s & PHY_STAT_ANC)
1216 printk(", auto-negotiation complete");
1217 }
1218
1219 if (*s & PHY_STAT_FAULT)
1220 printk(", remote fault");
1221
1222 printk(".\n");
1223}
1224
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001225static void mii_display_config(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001227 struct fec_enet_private *fep = container_of(work, struct fec_enet_private, phy_task);
1228 struct net_device *dev = fep->netdev;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001229 uint status = fep->phy_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 /*
1232 ** When we get here, phy_task is already removed from
1233 ** the workqueue. It is thus safe to allow to reuse it.
1234 */
1235 fep->mii_phy_task_queued = 0;
1236 printk("%s: config: auto-negotiation ", dev->name);
1237
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001238 if (status & PHY_CONF_ANE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 printk("on");
1240 else
1241 printk("off");
1242
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001243 if (status & PHY_CONF_100FDX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 printk(", 100FDX");
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001245 if (status & PHY_CONF_100HDX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 printk(", 100HDX");
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001247 if (status & PHY_CONF_10FDX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 printk(", 10FDX");
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001249 if (status & PHY_CONF_10HDX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 printk(", 10HDX");
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001251 if (!(status & PHY_CONF_SPMASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 printk(", No speed/duplex selected?");
1253
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001254 if (status & PHY_CONF_LOOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 printk(", loopback enabled");
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 printk(".\n");
1258
1259 fep->sequence_done = 1;
1260}
1261
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001262static void mii_relink(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001264 struct fec_enet_private *fep = container_of(work, struct fec_enet_private, phy_task);
1265 struct net_device *dev = fep->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 int duplex;
1267
1268 /*
1269 ** When we get here, phy_task is already removed from
1270 ** the workqueue. It is thus safe to allow to reuse it.
1271 */
1272 fep->mii_phy_task_queued = 0;
1273 fep->link = (fep->phy_status & PHY_STAT_LINK) ? 1 : 0;
1274 mii_display_status(dev);
1275 fep->old_link = fep->link;
1276
1277 if (fep->link) {
1278 duplex = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001279 if (fep->phy_status
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 & (PHY_STAT_100FDX | PHY_STAT_10FDX))
1281 duplex = 1;
1282 fec_restart(dev, duplex);
Philippe De Muyterf909b1e2007-10-23 14:37:54 +10001283 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 fec_stop(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
1287/* mii_queue_relink is called in interrupt context from mii_link_interrupt */
1288static void mii_queue_relink(uint mii_reg, struct net_device *dev)
1289{
1290 struct fec_enet_private *fep = netdev_priv(dev);
1291
1292 /*
Sascha Hauer22f6b862009-04-15 01:32:18 +00001293 * We cannot queue phy_task twice in the workqueue. It
1294 * would cause an endless loop in the workqueue.
1295 * Fortunately, if the last mii_relink entry has not yet been
1296 * executed now, it will do the job for the current interrupt,
1297 * which is just what we want.
1298 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (fep->mii_phy_task_queued)
1300 return;
1301
1302 fep->mii_phy_task_queued = 1;
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001303 INIT_WORK(&fep->phy_task, mii_relink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 schedule_work(&fep->phy_task);
1305}
1306
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001307/* mii_queue_config is called in interrupt context from fec_enet_mii */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308static void mii_queue_config(uint mii_reg, struct net_device *dev)
1309{
1310 struct fec_enet_private *fep = netdev_priv(dev);
1311
1312 if (fep->mii_phy_task_queued)
1313 return;
1314
1315 fep->mii_phy_task_queued = 1;
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001316 INIT_WORK(&fep->phy_task, mii_display_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 schedule_work(&fep->phy_task);
1318}
1319
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001320phy_cmd_t const phy_cmd_relink[] = {
1321 { mk_mii_read(MII_REG_CR), mii_queue_relink },
1322 { mk_mii_end, }
1323 };
1324phy_cmd_t const phy_cmd_config[] = {
1325 { mk_mii_read(MII_REG_CR), mii_queue_config },
1326 { mk_mii_end, }
1327 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Sascha Hauer22f6b862009-04-15 01:32:18 +00001329/* Read remainder of PHY ID. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330static void
1331mii_discover_phy3(uint mii_reg, struct net_device *dev)
1332{
1333 struct fec_enet_private *fep;
1334 int i;
1335
1336 fep = netdev_priv(dev);
1337 fep->phy_id |= (mii_reg & 0xffff);
1338 printk("fec: PHY @ 0x%x, ID 0x%08x", fep->phy_addr, fep->phy_id);
1339
1340 for(i = 0; phy_info[i]; i++) {
1341 if(phy_info[i]->id == (fep->phy_id >> 4))
1342 break;
1343 }
1344
1345 if (phy_info[i])
1346 printk(" -- %s\n", phy_info[i]->name);
1347 else
1348 printk(" -- unknown PHY!\n");
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 fep->phy = phy_info[i];
1351 fep->phy_id_done = 1;
1352}
1353
1354/* Scan all of the MII PHY addresses looking for someone to respond
1355 * with a valid ID. This usually happens quickly.
1356 */
1357static void
1358mii_discover_phy(uint mii_reg, struct net_device *dev)
1359{
1360 struct fec_enet_private *fep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 uint phytype;
1362
1363 fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 if (fep->phy_addr < 32) {
1366 if ((phytype = (mii_reg & 0xffff)) != 0xffff && phytype != 0) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001367
Sascha Hauer22f6b862009-04-15 01:32:18 +00001368 /* Got first part of ID, now get remainder */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 fep->phy_id = phytype << 16;
1370 mii_queue(dev, mk_mii_read(MII_REG_PHYIR2),
1371 mii_discover_phy3);
Philippe De Muyterf909b1e2007-10-23 14:37:54 +10001372 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 fep->phy_addr++;
1374 mii_queue(dev, mk_mii_read(MII_REG_PHYIR1),
1375 mii_discover_phy);
1376 }
1377 } else {
1378 printk("FEC: No PHY device found.\n");
1379 /* Disable external MII interface */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001380 writel(0, fep->hwp + FEC_MII_SPEED);
1381 fep->phy_speed = 0;
Greg Ungerer43be6362009-02-26 22:42:51 -08001382#ifdef HAVE_mii_link_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 fec_disable_phy_intr();
Sascha Haueread73182009-01-28 23:03:11 +00001384#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
1386}
1387
Sascha Hauer22f6b862009-04-15 01:32:18 +00001388/* This interrupt occurs when the PHY detects a link change */
Sebastian Siewiorc1d96152008-05-01 14:04:02 +10001389#ifdef HAVE_mii_link_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001391mii_link_interrupt(int irq, void * dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 struct net_device *dev = dev_id;
1394 struct fec_enet_private *fep = netdev_priv(dev);
1395
1396 fec_phy_ack_intr();
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 mii_do_cmd(dev, fep->phy->ack_int);
1399 mii_do_cmd(dev, phy_cmd_relink); /* restart and display status */
1400
1401 return IRQ_HANDLED;
1402}
Sebastian Siewiorc1d96152008-05-01 14:04:02 +10001403#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405static int
1406fec_enet_open(struct net_device *dev)
1407{
1408 struct fec_enet_private *fep = netdev_priv(dev);
1409
1410 /* I should reset the ring buffers here, but I don't yet know
1411 * a simple way to do that.
1412 */
1413 fec_set_mac_address(dev);
1414
1415 fep->sequence_done = 0;
1416 fep->link = 0;
1417
1418 if (fep->phy) {
1419 mii_do_cmd(dev, fep->phy->ack_int);
1420 mii_do_cmd(dev, fep->phy->config);
1421 mii_do_cmd(dev, phy_cmd_config); /* display configuration */
1422
Matt Waddel6b265292006-06-27 13:10:56 +10001423 /* Poll until the PHY tells us its configuration
1424 * (not link state).
1425 * Request is initiated by mii_do_cmd above, but answer
1426 * comes by interrupt.
1427 * This should take about 25 usec per register at 2.5 MHz,
1428 * and we read approximately 5 registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 */
1430 while(!fep->sequence_done)
1431 schedule();
1432
1433 mii_do_cmd(dev, fep->phy->startup);
1434
1435 /* Set the initial link state to true. A lot of hardware
1436 * based on this device does not implement a PHY interrupt,
1437 * so we are never notified of link change.
1438 */
1439 fep->link = 1;
1440 } else {
1441 fep->link = 1; /* lets just try it and see */
1442 /* no phy, go full duplex, it's most likely a hub chip */
1443 fec_restart(dev, 1);
1444 }
1445
1446 netif_start_queue(dev);
1447 fep->opened = 1;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
1451static int
1452fec_enet_close(struct net_device *dev)
1453{
1454 struct fec_enet_private *fep = netdev_priv(dev);
1455
Sascha Hauer22f6b862009-04-15 01:32:18 +00001456 /* Don't know what to do yet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 fep->opened = 0;
1458 netif_stop_queue(dev);
1459 fec_stop(dev);
1460
1461 return 0;
1462}
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464/* Set or clear the multicast filter for this adaptor.
1465 * Skeleton taken from sunlance driver.
1466 * The CPM Ethernet implementation allows Multicast as well as individual
1467 * MAC address filtering. Some of the drivers check to make sure it is
1468 * a group multicast address, and discard those that are not. I guess I
1469 * will do the same for now, but just remove the test if you want
1470 * individual filtering as well (do the upper net layers want or support
1471 * this kind of feature?).
1472 */
1473
1474#define HASH_BITS 6 /* #bits in hash */
1475#define CRC32_POLY 0xEDB88320
1476
1477static void set_multicast_list(struct net_device *dev)
1478{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001479 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 struct dev_mc_list *dmi;
Sascha Hauerf44d6302009-04-15 03:11:30 +00001481 unsigned int i, j, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 unsigned char hash;
1483
Sascha Hauer22f6b862009-04-15 01:32:18 +00001484 if (dev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001485 tmp = readl(fep->hwp + FEC_R_CNTRL);
1486 tmp |= 0x8;
1487 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00001488 return;
1489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Sascha Hauer4e831832009-04-15 01:32:19 +00001491 tmp = readl(fep->hwp + FEC_R_CNTRL);
1492 tmp &= ~0x8;
1493 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001494
Sascha Hauer4e831832009-04-15 01:32:19 +00001495 if (dev->flags & IFF_ALLMULTI) {
1496 /* Catch all multicast addresses, so set the
1497 * filter to all 1's
1498 */
1499 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1500 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Sascha Hauer4e831832009-04-15 01:32:19 +00001502 return;
1503 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001504
Sascha Hauer4e831832009-04-15 01:32:19 +00001505 /* Clear filter and add the addresses in hash register
1506 */
1507 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1508 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Sascha Hauer4e831832009-04-15 01:32:19 +00001510 dmi = dev->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Sascha Hauer4e831832009-04-15 01:32:19 +00001512 for (j = 0; j < dev->mc_count; j++, dmi = dmi->next) {
1513 /* Only support group multicast for now */
1514 if (!(dmi->dmi_addr[0] & 1))
1515 continue;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001516
Sascha Hauer4e831832009-04-15 01:32:19 +00001517 /* calculate crc32 value of mac address */
1518 crc = 0xffffffff;
1519
1520 for (i = 0; i < dmi->dmi_addrlen; i++) {
1521 data = dmi->dmi_addr[i];
1522 for (bit = 0; bit < 8; bit++, data >>= 1) {
1523 crc = (crc >> 1) ^
1524 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
1526 }
Sascha Hauer4e831832009-04-15 01:32:19 +00001527
1528 /* only upper 6 bits (HASH_BITS) are used
1529 * which point to specific bit in he hash registers
1530 */
1531 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
1532
1533 if (hash > 31) {
1534 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1535 tmp |= 1 << (hash - 32);
1536 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1537 } else {
1538 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1539 tmp |= 1 << hash;
1540 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543}
1544
Sascha Hauer22f6b862009-04-15 01:32:18 +00001545/* Set a MAC change in hardware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546static void
1547fec_set_mac_address(struct net_device *dev)
1548{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001549 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 /* Set station address. */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001552 writel(dev->dev_addr[3] | (dev->dev_addr[2] << 8) |
1553 (dev->dev_addr[1] << 16) | (dev->dev_addr[0] << 24),
1554 fep->hwp + FEC_ADDR_LOW);
1555 writel((dev->dev_addr[5] << 16) | (dev->dev_addr[4] << 24),
1556 fep + FEC_ADDR_HIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 /*
1560 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00001561 *
1562 * index is only used in legacy code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 */
Sascha Haueread73182009-01-28 23:03:11 +00001564int __init fec_enet_init(struct net_device *dev, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
1566 struct fec_enet_private *fep = netdev_priv(dev);
1567 unsigned long mem_addr;
Sascha Hauer2e285322009-04-15 01:32:16 +00001568 struct bufdesc *bdp, *cbd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001571 /* Allocate memory for buffer descriptors. */
1572 cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
1573 GFP_KERNEL);
1574 if (!cbd_base) {
Greg Ungerer562d2f82005-11-07 14:09:50 +10001575 printk("FEC: allocate descriptor memory failed?\n");
1576 return -ENOMEM;
1577 }
1578
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10001579 spin_lock_init(&fep->hw_lock);
1580 spin_lock_init(&fep->mii_lock);
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 fep->index = index;
Sascha Hauerf44d6302009-04-15 03:11:30 +00001583 fep->hwp = (void __iomem *)dev->base_addr;
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001584 fep->netdev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Sascha Haueread73182009-01-28 23:03:11 +00001586 /* Set the Ethernet address */
Greg Ungerer43be6362009-02-26 22:42:51 -08001587#ifdef CONFIG_M5272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 fec_get_mac(dev);
Sascha Haueread73182009-01-28 23:03:11 +00001589#else
1590 {
1591 unsigned long l;
Sascha Hauerf44d6302009-04-15 03:11:30 +00001592 l = readl(fep->hwp + FEC_ADDR_LOW);
Sascha Haueread73182009-01-28 23:03:11 +00001593 dev->dev_addr[0] = (unsigned char)((l & 0xFF000000) >> 24);
1594 dev->dev_addr[1] = (unsigned char)((l & 0x00FF0000) >> 16);
1595 dev->dev_addr[2] = (unsigned char)((l & 0x0000FF00) >> 8);
1596 dev->dev_addr[3] = (unsigned char)((l & 0x000000FF) >> 0);
Sascha Hauerf44d6302009-04-15 03:11:30 +00001597 l = readl(fep->hwp + FEC_ADDR_HIGH);
Sascha Haueread73182009-01-28 23:03:11 +00001598 dev->dev_addr[4] = (unsigned char)((l & 0xFF000000) >> 24);
1599 dev->dev_addr[5] = (unsigned char)((l & 0x00FF0000) >> 16);
1600 }
1601#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001603 /* Set receive and transmit descriptor base. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 fep->rx_bd_base = cbd_base;
1605 fep->tx_bd_base = cbd_base + RX_RING_SIZE;
1606
Sascha Hauer22f6b862009-04-15 01:32:18 +00001607 /* Initialize the receive buffer descriptors. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 bdp = fep->rx_bd_base;
1609 for (i=0; i<FEC_ENET_RX_PAGES; i++) {
1610
Sascha Hauer22f6b862009-04-15 01:32:18 +00001611 /* Allocate a page */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 mem_addr = __get_free_page(GFP_KERNEL);
1613 /* XXX: missing check for allocation failure */
1614
Sascha Hauer22f6b862009-04-15 01:32:18 +00001615 /* Initialize the BD for every fragment in the page */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 for (j=0; j<FEC_ENET_RX_FRPPG; j++) {
1617 bdp->cbd_sc = BD_ENET_RX_EMPTY;
1618 bdp->cbd_bufaddr = __pa(mem_addr);
1619 mem_addr += FEC_ENET_RX_FRSIZE;
1620 bdp++;
1621 }
1622 }
1623
Sascha Hauer22f6b862009-04-15 01:32:18 +00001624 /* Set the last buffer to wrap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 bdp--;
1626 bdp->cbd_sc |= BD_SC_WRAP;
1627
Sascha Hauer22f6b862009-04-15 01:32:18 +00001628 /* ...and the same for transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 bdp = fep->tx_bd_base;
1630 for (i=0, j=FEC_ENET_TX_FRPPG; i<TX_RING_SIZE; i++) {
1631 if (j >= FEC_ENET_TX_FRPPG) {
1632 mem_addr = __get_free_page(GFP_KERNEL);
1633 j = 1;
1634 } else {
1635 mem_addr += FEC_ENET_TX_FRSIZE;
1636 j++;
1637 }
1638 fep->tx_bounce[i] = (unsigned char *) mem_addr;
1639
Sascha Hauer22f6b862009-04-15 01:32:18 +00001640 /* Initialize the BD for every fragment in the page */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 bdp->cbd_sc = 0;
1642 bdp->cbd_bufaddr = 0;
1643 bdp++;
1644 }
1645
Sascha Hauer22f6b862009-04-15 01:32:18 +00001646 /* Set the last buffer to wrap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 bdp--;
1648 bdp->cbd_sc |= BD_SC_WRAP;
1649
Greg Ungerer43be6362009-02-26 22:42:51 -08001650#ifdef HAVE_mii_link_interrupt
1651 fec_request_mii_intr(dev);
Sascha Haueread73182009-01-28 23:03:11 +00001652#endif
Sascha Hauer22f6b862009-04-15 01:32:18 +00001653 /* The FEC Ethernet specific entries in the device structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 dev->open = fec_enet_open;
1655 dev->hard_start_xmit = fec_enet_start_xmit;
1656 dev->tx_timeout = fec_timeout;
1657 dev->watchdog_timeo = TX_TIMEOUT;
1658 dev->stop = fec_enet_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 dev->set_multicast_list = set_multicast_list;
1660
1661 for (i=0; i<NMII-1; i++)
1662 mii_cmds[i].mii_next = &mii_cmds[i+1];
1663 mii_free = mii_cmds;
1664
Sascha Hauer22f6b862009-04-15 01:32:18 +00001665 /* Set MII speed to 2.5 MHz */
Sascha Haueread73182009-01-28 23:03:11 +00001666 fep->phy_speed = ((((clk_get_rate(fep->clk) / 2 + 4999999)
1667 / 2500000) / 2) & 0x3F) << 1;
Sascha Haueread73182009-01-28 23:03:11 +00001668 fec_restart(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 /* Queue up command to detect the PHY and initialize the
1671 * remainder of the interface.
1672 */
1673 fep->phy_id_done = 0;
1674 fep->phy_addr = 0;
1675 mii_queue(dev, mk_mii_read(MII_REG_PHYIR1), mii_discover_phy);
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return 0;
1678}
1679
1680/* This function is called to start or restart the FEC during a link
1681 * change. This only happens when switching between half and full
1682 * duplex.
1683 */
1684static void
1685fec_restart(struct net_device *dev, int duplex)
1686{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001687 struct fec_enet_private *fep = netdev_priv(dev);
Sascha Hauer2e285322009-04-15 01:32:16 +00001688 struct bufdesc *bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 int i;
1690
Sascha Hauerf44d6302009-04-15 03:11:30 +00001691 /* Whack a reset. We should wait for this. */
1692 writel(1, fep->hwp + FEC_ECNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 udelay(10);
1694
Sascha Hauerf44d6302009-04-15 03:11:30 +00001695 /* Clear any outstanding interrupt. */
1696 writel(0xffc00000, fep->hwp + FEC_IEVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Sascha Hauerf44d6302009-04-15 03:11:30 +00001698 /* Set station address. */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001699 fec_set_mac_address(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Sascha Hauerf44d6302009-04-15 03:11:30 +00001701 /* Reset all multicast. */
1702 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1703 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Sascha Hauer4f1ceb42009-04-15 01:32:20 +00001704#ifndef CONFIG_M5272
1705 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1706 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1707#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Sascha Hauerf44d6302009-04-15 03:11:30 +00001709 /* Set maximum receive buffer size. */
1710 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Sascha Hauerf44d6302009-04-15 03:11:30 +00001712 /* Set receive and transmit descriptor base. */
1713 writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
Sascha Hauer2e285322009-04-15 01:32:16 +00001714 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc) * RX_RING_SIZE,
Sascha Hauerf44d6302009-04-15 03:11:30 +00001715 fep->hwp + FEC_X_DES_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
1718 fep->cur_rx = fep->rx_bd_base;
1719
Sascha Hauerf44d6302009-04-15 03:11:30 +00001720 /* Reset SKB transmit buffers. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 fep->skb_cur = fep->skb_dirty = 0;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001722 for (i = 0; i <= TX_RING_MOD_MASK; i++) {
1723 if (fep->tx_skbuff[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 dev_kfree_skb_any(fep->tx_skbuff[i]);
1725 fep->tx_skbuff[i] = NULL;
1726 }
1727 }
1728
Sascha Hauerf44d6302009-04-15 03:11:30 +00001729 /* Initialize the receive buffer descriptors. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 bdp = fep->rx_bd_base;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001731 for (i = 0; i < RX_RING_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Sascha Hauerf44d6302009-04-15 03:11:30 +00001733 /* Initialize the BD for every fragment in the page. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 bdp->cbd_sc = BD_ENET_RX_EMPTY;
1735 bdp++;
1736 }
1737
Sascha Hauer22f6b862009-04-15 01:32:18 +00001738 /* Set the last buffer to wrap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 bdp--;
1740 bdp->cbd_sc |= BD_SC_WRAP;
1741
Sascha Hauer22f6b862009-04-15 01:32:18 +00001742 /* ...and the same for transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 bdp = fep->tx_bd_base;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001744 for (i = 0; i < TX_RING_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Sascha Hauerf44d6302009-04-15 03:11:30 +00001746 /* Initialize the BD for every fragment in the page. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 bdp->cbd_sc = 0;
1748 bdp->cbd_bufaddr = 0;
1749 bdp++;
1750 }
1751
Sascha Hauer22f6b862009-04-15 01:32:18 +00001752 /* Set the last buffer to wrap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 bdp--;
1754 bdp->cbd_sc |= BD_SC_WRAP;
1755
Sascha Hauer22f6b862009-04-15 01:32:18 +00001756 /* Enable MII mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 if (duplex) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001758 /* MII enable / FD enable */
1759 writel(OPT_FRAME_SIZE | 0x04, fep->hwp + FEC_R_CNTRL);
1760 writel(0x04, fep->hwp + FEC_X_CNTRL);
Philippe De Muyterf909b1e2007-10-23 14:37:54 +10001761 } else {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001762 /* MII enable / No Rcv on Xmit */
1763 writel(OPT_FRAME_SIZE | 0x06, fep->hwp + FEC_R_CNTRL);
1764 writel(0x0, fep->hwp + FEC_X_CNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766 fep->full_duplex = duplex;
1767
Sascha Hauer22f6b862009-04-15 01:32:18 +00001768 /* Set MII speed */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001769 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Sascha Hauer22f6b862009-04-15 01:32:18 +00001771 /* And last, enable the transmit and receive processing */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001772 writel(2, fep->hwp + FEC_ECNTRL);
1773 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Matt Waddel6b265292006-06-27 13:10:56 +10001774
Sascha Hauer22f6b862009-04-15 01:32:18 +00001775 /* Enable interrupts we wish to service */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001776 writel(FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII,
1777 fep->hwp + FEC_IMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779
1780static void
1781fec_stop(struct net_device *dev)
1782{
Sascha Hauerf44d6302009-04-15 03:11:30 +00001783 struct fec_enet_private *fep = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Sascha Hauer22f6b862009-04-15 01:32:18 +00001785 /* We cannot expect a graceful transmit stop without link !!! */
Sascha Hauerf44d6302009-04-15 03:11:30 +00001786 if (fep->link) {
1787 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
Philippe De Muyter677177c2006-06-27 13:05:33 +10001788 udelay(10);
Sascha Hauerf44d6302009-04-15 03:11:30 +00001789 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Philippe De Muyter677177c2006-06-27 13:05:33 +10001790 printk("fec_stop : Graceful transmit stop did not complete !\n");
Sascha Hauerf44d6302009-04-15 03:11:30 +00001791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Sascha Hauerf44d6302009-04-15 03:11:30 +00001793 /* Whack a reset. We should wait for this. */
1794 writel(1, fep->hwp + FEC_ECNTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 udelay(10);
1796
Sascha Hauerf44d6302009-04-15 03:11:30 +00001797 /* Clear outstanding MII command interrupts. */
1798 writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Sascha Hauerf44d6302009-04-15 03:11:30 +00001800 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1801 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
1803
Sascha Haueread73182009-01-28 23:03:11 +00001804static int __devinit
1805fec_probe(struct platform_device *pdev)
1806{
1807 struct fec_enet_private *fep;
1808 struct net_device *ndev;
1809 int i, irq, ret = 0;
1810 struct resource *r;
1811
1812 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1813 if (!r)
1814 return -ENXIO;
1815
1816 r = request_mem_region(r->start, resource_size(r), pdev->name);
1817 if (!r)
1818 return -EBUSY;
1819
1820 /* Init network device */
1821 ndev = alloc_etherdev(sizeof(struct fec_enet_private));
1822 if (!ndev)
1823 return -ENOMEM;
1824
1825 SET_NETDEV_DEV(ndev, &pdev->dev);
1826
1827 /* setup board info structure */
1828 fep = netdev_priv(ndev);
1829 memset(fep, 0, sizeof(*fep));
1830
1831 ndev->base_addr = (unsigned long)ioremap(r->start, resource_size(r));
1832
1833 if (!ndev->base_addr) {
1834 ret = -ENOMEM;
1835 goto failed_ioremap;
1836 }
1837
1838 platform_set_drvdata(pdev, ndev);
1839
1840 /* This device has up to three irqs on some platforms */
1841 for (i = 0; i < 3; i++) {
1842 irq = platform_get_irq(pdev, i);
1843 if (i && irq < 0)
1844 break;
1845 ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
1846 if (ret) {
1847 while (i >= 0) {
1848 irq = platform_get_irq(pdev, i);
1849 free_irq(irq, ndev);
1850 i--;
1851 }
1852 goto failed_irq;
1853 }
1854 }
1855
1856 fep->clk = clk_get(&pdev->dev, "fec_clk");
1857 if (IS_ERR(fep->clk)) {
1858 ret = PTR_ERR(fep->clk);
1859 goto failed_clk;
1860 }
1861 clk_enable(fep->clk);
1862
1863 ret = fec_enet_init(ndev, 0);
1864 if (ret)
1865 goto failed_init;
1866
1867 ret = register_netdev(ndev);
1868 if (ret)
1869 goto failed_register;
1870
1871 return 0;
1872
1873failed_register:
1874failed_init:
1875 clk_disable(fep->clk);
1876 clk_put(fep->clk);
1877failed_clk:
1878 for (i = 0; i < 3; i++) {
1879 irq = platform_get_irq(pdev, i);
1880 if (irq > 0)
1881 free_irq(irq, ndev);
1882 }
1883failed_irq:
1884 iounmap((void __iomem *)ndev->base_addr);
1885failed_ioremap:
1886 free_netdev(ndev);
1887
1888 return ret;
1889}
1890
1891static int __devexit
1892fec_drv_remove(struct platform_device *pdev)
1893{
1894 struct net_device *ndev = platform_get_drvdata(pdev);
1895 struct fec_enet_private *fep = netdev_priv(ndev);
1896
1897 platform_set_drvdata(pdev, NULL);
1898
1899 fec_stop(ndev);
1900 clk_disable(fep->clk);
1901 clk_put(fep->clk);
1902 iounmap((void __iomem *)ndev->base_addr);
1903 unregister_netdev(ndev);
1904 free_netdev(ndev);
1905 return 0;
1906}
1907
1908static int
1909fec_suspend(struct platform_device *dev, pm_message_t state)
1910{
1911 struct net_device *ndev = platform_get_drvdata(dev);
1912 struct fec_enet_private *fep;
1913
1914 if (ndev) {
1915 fep = netdev_priv(ndev);
1916 if (netif_running(ndev)) {
1917 netif_device_detach(ndev);
1918 fec_stop(ndev);
1919 }
1920 }
1921 return 0;
1922}
1923
1924static int
1925fec_resume(struct platform_device *dev)
1926{
1927 struct net_device *ndev = platform_get_drvdata(dev);
1928
1929 if (ndev) {
1930 if (netif_running(ndev)) {
1931 fec_enet_init(ndev, 0);
1932 netif_device_attach(ndev);
1933 }
1934 }
1935 return 0;
1936}
1937
1938static struct platform_driver fec_driver = {
1939 .driver = {
1940 .name = "fec",
1941 .owner = THIS_MODULE,
1942 },
1943 .probe = fec_probe,
1944 .remove = __devexit_p(fec_drv_remove),
1945 .suspend = fec_suspend,
1946 .resume = fec_resume,
1947};
1948
1949static int __init
1950fec_enet_module_init(void)
1951{
1952 printk(KERN_INFO "FEC Ethernet Driver\n");
1953
1954 return platform_driver_register(&fec_driver);
1955}
1956
1957static void __exit
1958fec_enet_cleanup(void)
1959{
1960 platform_driver_unregister(&fec_driver);
1961}
1962
1963module_exit(fec_enet_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964module_init(fec_enet_module_init);
1965
1966MODULE_LICENSE("GPL");