blob: 46a441a9c644c885a967ee67aa1a51b3334e4fe7 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Greg Ungerer080853a2007-07-30 16:28:46 +100042#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/coldfire.h>
44#include <asm/mcfsim.h>
Sascha Hauer6f501b12009-01-28 23:03:05 +000045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "fec.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#if defined(CONFIG_FEC2)
49#define FEC_MAX_PORTS 2
50#else
51#define FEC_MAX_PORTS 1
52#endif
53
Greg Ungerer87f4abb2008-06-06 15:55:36 +100054#if defined(CONFIG_M5272)
Sebastian Siewiorc1d96152008-05-01 14:04:02 +100055#define HAVE_mii_link_interrupt
56#endif
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/*
59 * Define the fixed address of the FEC hardware.
60 */
61static unsigned int fec_hw[] = {
62#if defined(CONFIG_M5272)
63 (MCF_MBAR + 0x840),
64#elif defined(CONFIG_M527x)
65 (MCF_MBAR + 0x1000),
66 (MCF_MBAR + 0x1800),
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +100067#elif defined(CONFIG_M523x) || defined(CONFIG_M528x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 (MCF_MBAR + 0x1000),
Greg Ungerer562d2f82005-11-07 14:09:50 +100069#elif defined(CONFIG_M520x)
70 (MCF_MBAR+0x30000),
Matt Waddel6b265292006-06-27 13:10:56 +100071#elif defined(CONFIG_M532x)
72 (MCF_MBAR+0xfc030000),
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#endif
74};
75
76static unsigned char fec_mac_default[] = {
77 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
78};
79
80/*
81 * Some hardware gets it MAC address out of local flash memory.
82 * if this is non-zero then assume it is the address to get MAC from.
83 */
84#if defined(CONFIG_NETtel)
85#define FEC_FLASHMAC 0xf0006006
86#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
87#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#elif defined(CONFIG_CANCam)
89#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +100090#elif defined (CONFIG_M5272C3)
91#define FEC_FLASHMAC (0xffe04000 + 4)
92#elif defined(CONFIG_MOD5272)
93#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#else
95#define FEC_FLASHMAC 0
96#endif
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/* Forward declarations of some structures to support different PHYs
99*/
100
101typedef struct {
102 uint mii_data;
103 void (*funct)(uint mii_reg, struct net_device *dev);
104} phy_cmd_t;
105
106typedef struct {
107 uint id;
108 char *name;
109
110 const phy_cmd_t *config;
111 const phy_cmd_t *startup;
112 const phy_cmd_t *ack_int;
113 const phy_cmd_t *shutdown;
114} phy_info_t;
115
116/* The number of Tx and Rx buffers. These are allocated from the page
117 * pool. The code may assume these are power of two, so it it best
118 * to keep them that size.
119 * We don't need to allocate pages for the transmitter. We just use
120 * the skbuffer directly.
121 */
122#define FEC_ENET_RX_PAGES 8
123#define FEC_ENET_RX_FRSIZE 2048
124#define FEC_ENET_RX_FRPPG (PAGE_SIZE / FEC_ENET_RX_FRSIZE)
125#define RX_RING_SIZE (FEC_ENET_RX_FRPPG * FEC_ENET_RX_PAGES)
126#define FEC_ENET_TX_FRSIZE 2048
127#define FEC_ENET_TX_FRPPG (PAGE_SIZE / FEC_ENET_TX_FRSIZE)
128#define TX_RING_SIZE 16 /* Must be power of two */
129#define TX_RING_MOD_MASK 15 /* for this to work */
130
Greg Ungerer562d2f82005-11-07 14:09:50 +1000131#if (((RX_RING_SIZE + TX_RING_SIZE) * 8) > PAGE_SIZE)
Matt Waddel6b265292006-06-27 13:10:56 +1000132#error "FEC: descriptor ring size constants too large"
Greg Ungerer562d2f82005-11-07 14:09:50 +1000133#endif
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/* Interrupt events/masks.
136*/
137#define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
138#define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
139#define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
140#define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
141#define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
142#define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
143#define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
144#define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
145#define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
146#define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
147
148/* The FEC stores dest/src/type, data, and checksum for receive packets.
149 */
150#define PKT_MAXBUF_SIZE 1518
151#define PKT_MINBUF_SIZE 64
152#define PKT_MAXBLR_SIZE 1520
153
154
155/*
Matt Waddel6b265292006-06-27 13:10:56 +1000156 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * size bits. Other FEC hardware does not, so we need to take that into
158 * account when setting it.
159 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000160#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Matt Waddel6b265292006-06-27 13:10:56 +1000161 defined(CONFIG_M520x) || defined(CONFIG_M532x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
163#else
164#define OPT_FRAME_SIZE 0
165#endif
166
167/* The FEC buffer descriptors track the ring buffers. The rx_bd_base and
168 * tx_bd_base always point to the base of the buffer descriptors. The
169 * cur_rx and cur_tx point to the currently available buffer.
170 * The dirty_tx tracks the current buffer that is being sent by the
171 * controller. The cur_tx and dirty_tx are equal under both completely
172 * empty and completely full conditions. The empty/ready indicator in
173 * the buffer descriptor determines the actual condition.
174 */
175struct fec_enet_private {
176 /* Hardware registers of the FEC device */
177 volatile fec_t *hwp;
178
Greg Ungerercb84d6e2007-07-30 16:29:09 +1000179 struct net_device *netdev;
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /* The saved address of a sent-in-place packet/buffer, for skfree(). */
182 unsigned char *tx_bounce[TX_RING_SIZE];
183 struct sk_buff* tx_skbuff[TX_RING_SIZE];
184 ushort skb_cur;
185 ushort skb_dirty;
186
187 /* CPM dual port RAM relative addresses.
188 */
Sascha Hauer4661e752009-01-28 23:03:07 +0000189 dma_addr_t bd_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 cbd_t *rx_bd_base; /* Address of Rx and Tx buffers. */
191 cbd_t *tx_bd_base;
192 cbd_t *cur_rx, *cur_tx; /* The next free ring entry */
193 cbd_t *dirty_tx; /* The ring entries to be free()ed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 uint tx_full;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000195 /* hold while accessing the HW like ringbuffer for tx/rx but not MAC */
196 spinlock_t hw_lock;
197 /* hold while accessing the mii_list_t() elements */
198 spinlock_t mii_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 uint phy_id;
201 uint phy_id_done;
202 uint phy_status;
203 uint phy_speed;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000204 phy_info_t const *phy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 struct work_struct phy_task;
206
207 uint sequence_done;
208 uint mii_phy_task_queued;
209
210 uint phy_addr;
211
212 int index;
213 int opened;
214 int link;
215 int old_link;
216 int full_duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217};
218
219static int fec_enet_open(struct net_device *dev);
220static int fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev);
221static void fec_enet_mii(struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100222static irqreturn_t fec_enet_interrupt(int irq, void * dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223static void fec_enet_tx(struct net_device *dev);
224static void fec_enet_rx(struct net_device *dev);
225static int fec_enet_close(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static void set_multicast_list(struct net_device *dev);
227static void fec_restart(struct net_device *dev, int duplex);
228static void fec_stop(struct net_device *dev);
229static void fec_set_mac_address(struct net_device *dev);
230
231
232/* MII processing. We keep this as simple as possible. Requests are
233 * placed on the list (if there is room). When the request is finished
234 * by the MII, an optional function may be called.
235 */
236typedef struct mii_list {
237 uint mii_regval;
238 void (*mii_func)(uint val, struct net_device *dev);
239 struct mii_list *mii_next;
240} mii_list_t;
241
242#define NMII 20
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000243static mii_list_t mii_cmds[NMII];
244static mii_list_t *mii_free;
245static mii_list_t *mii_head;
246static mii_list_t *mii_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400248static int mii_queue(struct net_device *dev, int request,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 void (*func)(uint, struct net_device *));
250
251/* Make MII read/write commands for the FEC.
252*/
253#define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18))
254#define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | \
255 (VAL & 0xffff))
256#define mk_mii_end 0
257
258/* Transmitter timeout.
259*/
260#define TX_TIMEOUT (2*HZ)
261
262/* Register definitions for the PHY.
263*/
264
265#define MII_REG_CR 0 /* Control Register */
266#define MII_REG_SR 1 /* Status Register */
267#define MII_REG_PHYIR1 2 /* PHY Identification Register 1 */
268#define MII_REG_PHYIR2 3 /* PHY Identification Register 2 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400269#define MII_REG_ANAR 4 /* A-N Advertisement Register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270#define MII_REG_ANLPAR 5 /* A-N Link Partner Ability Register */
271#define MII_REG_ANER 6 /* A-N Expansion Register */
272#define MII_REG_ANNPTR 7 /* A-N Next Page Transmit Register */
273#define MII_REG_ANLPRNPR 8 /* A-N Link Partner Received Next Page Reg. */
274
275/* values for phy_status */
276
277#define PHY_CONF_ANE 0x0001 /* 1 auto-negotiation enabled */
278#define PHY_CONF_LOOP 0x0002 /* 1 loopback mode enabled */
279#define PHY_CONF_SPMASK 0x00f0 /* mask for speed */
280#define PHY_CONF_10HDX 0x0010 /* 10 Mbit half duplex supported */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400281#define PHY_CONF_10FDX 0x0020 /* 10 Mbit full duplex supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282#define PHY_CONF_100HDX 0x0040 /* 100 Mbit half duplex supported */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400283#define PHY_CONF_100FDX 0x0080 /* 100 Mbit full duplex supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285#define PHY_STAT_LINK 0x0100 /* 1 up - 0 down */
286#define PHY_STAT_FAULT 0x0200 /* 1 remote fault */
287#define PHY_STAT_ANC 0x0400 /* 1 auto-negotiation complete */
288#define PHY_STAT_SPMASK 0xf000 /* mask for speed */
289#define PHY_STAT_10HDX 0x1000 /* 10 Mbit half duplex selected */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400290#define PHY_STAT_10FDX 0x2000 /* 10 Mbit full duplex selected */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291#define PHY_STAT_100HDX 0x4000 /* 100 Mbit half duplex selected */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400292#define PHY_STAT_100FDX 0x8000 /* 100 Mbit full duplex selected */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294
295static int
296fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
297{
298 struct fec_enet_private *fep;
299 volatile fec_t *fecp;
300 volatile cbd_t *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000301 unsigned short status;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000302 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 fep = netdev_priv(dev);
305 fecp = (volatile fec_t*)dev->base_addr;
306
307 if (!fep->link) {
308 /* Link is down or autonegotiation is in progress. */
309 return 1;
310 }
311
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000312 spin_lock_irqsave(&fep->hw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /* Fill in a Tx ring entry */
314 bdp = fep->cur_tx;
315
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000316 status = bdp->cbd_sc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317#ifndef final_version
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000318 if (status & BD_ENET_TX_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* Ooops. All transmit buffers are full. Bail out.
320 * This should not happen, since dev->tbusy should be set.
321 */
322 printk("%s: tx queue full!.\n", dev->name);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000323 spin_unlock_irqrestore(&fep->hw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return 1;
325 }
326#endif
327
328 /* Clear all of the status flags.
329 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000330 status &= ~BD_ENET_TX_STATS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /* Set buffer length and buffer pointer.
333 */
334 bdp->cbd_bufaddr = __pa(skb->data);
335 bdp->cbd_datlen = skb->len;
336
337 /*
338 * On some FEC implementations data must be aligned on
339 * 4-byte boundaries. Use bounce buffers to copy data
340 * and get it aligned. Ugh.
341 */
342 if (bdp->cbd_bufaddr & 0x3) {
343 unsigned int index;
344 index = bdp - fep->tx_bd_base;
Sascha Hauer6989f512009-01-28 23:03:06 +0000345 memcpy(fep->tx_bounce[index], (void *)skb->data, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 bdp->cbd_bufaddr = __pa(fep->tx_bounce[index]);
347 }
348
349 /* Save skb pointer.
350 */
351 fep->tx_skbuff[fep->skb_cur] = skb;
352
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700353 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 fep->skb_cur = (fep->skb_cur+1) & TX_RING_MOD_MASK;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 /* Push the data cache so the CPM does not get stale memory
357 * data.
358 */
359 flush_dcache_range((unsigned long)skb->data,
360 (unsigned long)skb->data + skb->len);
361
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000362 /* Send it on its way. Tell FEC it's ready, interrupt when done,
363 * it's the last BD of the frame, and to put the CRC on the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 */
365
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000366 status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 | BD_ENET_TX_LAST | BD_ENET_TX_TC);
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000368 bdp->cbd_sc = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 dev->trans_start = jiffies;
371
372 /* Trigger transmission start */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000373 fecp->fec_x_des_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /* If this was the last BD in the ring, start at the beginning again.
376 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000377 if (status & BD_ENET_TX_WRAP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 bdp = fep->tx_bd_base;
379 } else {
380 bdp++;
381 }
382
383 if (bdp == fep->dirty_tx) {
384 fep->tx_full = 1;
385 netif_stop_queue(dev);
386 }
387
388 fep->cur_tx = (cbd_t *)bdp;
389
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000390 spin_unlock_irqrestore(&fep->hw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 return 0;
393}
394
395static void
396fec_timeout(struct net_device *dev)
397{
398 struct fec_enet_private *fep = netdev_priv(dev);
399
400 printk("%s: transmit timed out.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700401 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402#ifndef final_version
403 {
404 int i;
405 cbd_t *bdp;
406
407 printk("Ring data dump: cur_tx %lx%s, dirty_tx %lx cur_rx: %lx\n",
408 (unsigned long)fep->cur_tx, fep->tx_full ? " (full)" : "",
409 (unsigned long)fep->dirty_tx,
410 (unsigned long)fep->cur_rx);
411
412 bdp = fep->tx_bd_base;
413 printk(" tx: %u buffers\n", TX_RING_SIZE);
414 for (i = 0 ; i < TX_RING_SIZE; i++) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400415 printk(" %08x: %04x %04x %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 (uint) bdp,
417 bdp->cbd_sc,
418 bdp->cbd_datlen,
419 (int) bdp->cbd_bufaddr);
420 bdp++;
421 }
422
423 bdp = fep->rx_bd_base;
424 printk(" rx: %lu buffers\n", (unsigned long) RX_RING_SIZE);
425 for (i = 0 ; i < RX_RING_SIZE; i++) {
426 printk(" %08x: %04x %04x %08x\n",
427 (uint) bdp,
428 bdp->cbd_sc,
429 bdp->cbd_datlen,
430 (int) bdp->cbd_bufaddr);
431 bdp++;
432 }
433 }
434#endif
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000435 fec_restart(dev, fep->full_duplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 netif_wake_queue(dev);
437}
438
439/* The interrupt handler.
440 * This is called from the MPC core interrupt.
441 */
442static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100443fec_enet_interrupt(int irq, void * dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
445 struct net_device *dev = dev_id;
446 volatile fec_t *fecp;
447 uint int_events;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000448 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 fecp = (volatile fec_t*)dev->base_addr;
451
452 /* Get the interrupt events that caused us to be here.
453 */
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000454 do {
455 int_events = fecp->fec_ievent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 fecp->fec_ievent = int_events;
457
458 /* Handle receive event in its own function.
459 */
460 if (int_events & FEC_ENET_RXF) {
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000461 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 fec_enet_rx(dev);
463 }
464
465 /* Transmit OK, or non-fatal error. Update the buffer
466 descriptors. FEC handles all errors, we just discover
467 them as part of the transmit process.
468 */
469 if (int_events & FEC_ENET_TXF) {
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000470 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 fec_enet_tx(dev);
472 }
473
474 if (int_events & FEC_ENET_MII) {
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000475 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 fec_enet_mii(dev);
477 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400478
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000479 } while (int_events);
480
481 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
484
485static void
486fec_enet_tx(struct net_device *dev)
487{
488 struct fec_enet_private *fep;
489 volatile cbd_t *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000490 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 struct sk_buff *skb;
492
493 fep = netdev_priv(dev);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000494 spin_lock_irq(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 bdp = fep->dirty_tx;
496
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000497 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (bdp == fep->cur_tx && fep->tx_full == 0) break;
499
500 skb = fep->tx_skbuff[fep->skb_dirty];
501 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000502 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 BD_ENET_TX_RL | BD_ENET_TX_UN |
504 BD_ENET_TX_CSL)) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700505 dev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000506 if (status & BD_ENET_TX_HB) /* No heartbeat */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700507 dev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000508 if (status & BD_ENET_TX_LC) /* Late collision */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700509 dev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000510 if (status & BD_ENET_TX_RL) /* Retrans limit */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700511 dev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000512 if (status & BD_ENET_TX_UN) /* Underrun */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700513 dev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000514 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700515 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 } else {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700517 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519
520#ifndef final_version
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000521 if (status & BD_ENET_TX_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 printk("HEY! Enet xmit interrupt and TX_READY.\n");
523#endif
524 /* Deferred means some collisions occurred during transmit,
525 * but we eventually sent the packet OK.
526 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000527 if (status & BD_ENET_TX_DEF)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700528 dev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /* Free the sk buffer associated with this last transmit.
531 */
532 dev_kfree_skb_any(skb);
533 fep->tx_skbuff[fep->skb_dirty] = NULL;
534 fep->skb_dirty = (fep->skb_dirty + 1) & TX_RING_MOD_MASK;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /* Update pointer to next buffer descriptor to be transmitted.
537 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000538 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 bdp = fep->tx_bd_base;
540 else
541 bdp++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /* Since we have freed up a buffer, the ring is no longer
544 * full.
545 */
546 if (fep->tx_full) {
547 fep->tx_full = 0;
548 if (netif_queue_stopped(dev))
549 netif_wake_queue(dev);
550 }
551 }
552 fep->dirty_tx = (cbd_t *)bdp;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000553 spin_unlock_irq(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
556
557/* During a receive, the cur_rx points to the current incoming buffer.
558 * When we update through the ring, if the next incoming buffer has
559 * not been given to the system, we just set the empty indicator,
560 * effectively tossing the packet.
561 */
562static void
563fec_enet_rx(struct net_device *dev)
564{
565 struct fec_enet_private *fep;
566 volatile fec_t *fecp;
567 volatile cbd_t *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000568 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 struct sk_buff *skb;
570 ushort pkt_len;
571 __u8 *data;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400572
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000573#ifdef CONFIG_M532x
574 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400575#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 fep = netdev_priv(dev);
578 fecp = (volatile fec_t*)dev->base_addr;
579
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000580 spin_lock_irq(&fep->hw_lock);
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* First, grab all of the stats for the incoming packet.
583 * These get messed up if we get called due to a busy condition.
584 */
585 bdp = fep->cur_rx;
586
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000587while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589#ifndef final_version
590 /* Since we have allocated space to hold a complete frame,
591 * the last indicator should be set.
592 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000593 if ((status & BD_ENET_RX_LAST) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 printk("FEC ENET: rcv is not +last\n");
595#endif
596
597 if (!fep->opened)
598 goto rx_processing_done;
599
600 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000601 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700603 dev->stats.rx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000604 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /* Frame too long or too short. */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700606 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000608 if (status & BD_ENET_RX_NO) /* Frame alignment */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700609 dev->stats.rx_frame_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000610 if (status & BD_ENET_RX_CR) /* CRC Error */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700611 dev->stats.rx_crc_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000612 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700613 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615
616 /* Report late collisions as a frame error.
617 * On this error, the BD is closed, but we don't know what we
618 * have in the buffer. So, just drop this frame on the floor.
619 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000620 if (status & BD_ENET_RX_CL) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700621 dev->stats.rx_errors++;
622 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 goto rx_processing_done;
624 }
625
626 /* Process the incoming frame.
627 */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700628 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 pkt_len = bdp->cbd_datlen;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700630 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 data = (__u8*)__va(bdp->cbd_bufaddr);
632
633 /* This does 16 byte alignment, exactly what we need.
634 * The packet length includes FCS, but we don't want to
635 * include that when passing upstream as it messes up
636 * bridging applications.
637 */
638 skb = dev_alloc_skb(pkt_len-4);
639
640 if (skb == NULL) {
641 printk("%s: Memory squeeze, dropping packet.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700642 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 skb_put(skb,pkt_len-4); /* Make room */
David S. Miller8c7b7fa2007-07-10 22:08:12 -0700645 skb_copy_to_linear_data(skb, data, pkt_len-4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 skb->protocol=eth_type_trans(skb,dev);
647 netif_rx(skb);
648 }
649 rx_processing_done:
650
651 /* Clear the status flags for this buffer.
652 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000653 status &= ~BD_ENET_RX_STATS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 /* Mark the buffer empty.
656 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000657 status |= BD_ENET_RX_EMPTY;
658 bdp->cbd_sc = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 /* Update BD pointer to next entry.
661 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000662 if (status & BD_ENET_RX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 bdp = fep->rx_bd_base;
664 else
665 bdp++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667#if 1
668 /* Doing this here will keep the FEC running while we process
669 * incoming frames. On a heavily loaded network, we should be
670 * able to keep up at the expense of system resources.
671 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000672 fecp->fec_r_des_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673#endif
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000674 } /* while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 fep->cur_rx = (cbd_t *)bdp;
676
677#if 0
678 /* Doing this here will allow us to process all frames in the
679 * ring before the FEC is allowed to put more there. On a heavily
680 * loaded network, some frames may be lost. Unfortunately, this
681 * increases the interrupt overhead since we can potentially work
682 * our way back to the interrupt return only to come right back
683 * here.
684 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000685 fecp->fec_r_des_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686#endif
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000687
688 spin_unlock_irq(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
691
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000692/* called from interrupt context */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693static void
694fec_enet_mii(struct net_device *dev)
695{
696 struct fec_enet_private *fep;
697 volatile fec_t *ep;
698 mii_list_t *mip;
699 uint mii_reg;
700
701 fep = netdev_priv(dev);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000702 spin_lock_irq(&fep->mii_lock);
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 ep = fep->hwp;
705 mii_reg = ep->fec_mii_data;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if ((mip = mii_head) == NULL) {
708 printk("MII and no head!\n");
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000709 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711
712 if (mip->mii_func != NULL)
713 (*(mip->mii_func))(mii_reg, dev);
714
715 mii_head = mip->mii_next;
716 mip->mii_next = mii_free;
717 mii_free = mip;
718
719 if ((mip = mii_head) != NULL)
720 ep->fec_mii_data = mip->mii_regval;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000721
722unlock:
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000723 spin_unlock_irq(&fep->mii_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
726static int
727mii_queue(struct net_device *dev, int regval, void (*func)(uint, struct net_device *))
728{
729 struct fec_enet_private *fep;
730 unsigned long flags;
731 mii_list_t *mip;
732 int retval;
733
734 /* Add PHY address to register command.
735 */
736 fep = netdev_priv(dev);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000737 spin_lock_irqsave(&fep->mii_lock, flags);
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 regval |= fep->phy_addr << 23;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 retval = 0;
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if ((mip = mii_free) != NULL) {
743 mii_free = mip->mii_next;
744 mip->mii_regval = regval;
745 mip->mii_func = func;
746 mip->mii_next = NULL;
747 if (mii_head) {
748 mii_tail->mii_next = mip;
749 mii_tail = mip;
Philippe De Muyterf909b1e2007-10-23 14:37:54 +1000750 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 mii_head = mii_tail = mip;
752 fep->hwp->fec_mii_data = regval;
753 }
Philippe De Muyterf909b1e2007-10-23 14:37:54 +1000754 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 retval = 1;
756 }
757
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000758 spin_unlock_irqrestore(&fep->mii_lock, flags);
759 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
762static void mii_do_cmd(struct net_device *dev, const phy_cmd_t *c)
763{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if(!c)
765 return;
766
Philippe De Muyterbe6cb662007-10-23 14:37:54 +1000767 for (; c->mii_data != mk_mii_end; c++)
768 mii_queue(dev, c->mii_data, c->funct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
771static void mii_parse_sr(uint mii_reg, struct net_device *dev)
772{
773 struct fec_enet_private *fep = netdev_priv(dev);
774 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000775 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000777 status = *s & ~(PHY_STAT_LINK | PHY_STAT_FAULT | PHY_STAT_ANC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 if (mii_reg & 0x0004)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000780 status |= PHY_STAT_LINK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (mii_reg & 0x0010)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000782 status |= PHY_STAT_FAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (mii_reg & 0x0020)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000784 status |= PHY_STAT_ANC;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000785 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
788static void mii_parse_cr(uint mii_reg, struct net_device *dev)
789{
790 struct fec_enet_private *fep = netdev_priv(dev);
791 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000792 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000794 status = *s & ~(PHY_CONF_ANE | PHY_CONF_LOOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 if (mii_reg & 0x1000)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000797 status |= PHY_CONF_ANE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (mii_reg & 0x4000)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000799 status |= PHY_CONF_LOOP;
800 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
803static void mii_parse_anar(uint mii_reg, struct net_device *dev)
804{
805 struct fec_enet_private *fep = netdev_priv(dev);
806 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000807 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000809 status = *s & ~(PHY_CONF_SPMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 if (mii_reg & 0x0020)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000812 status |= PHY_CONF_10HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (mii_reg & 0x0040)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000814 status |= PHY_CONF_10FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (mii_reg & 0x0080)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000816 status |= PHY_CONF_100HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (mii_reg & 0x00100)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000818 status |= PHY_CONF_100FDX;
819 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
822/* ------------------------------------------------------------------------- */
823/* The Level one LXT970 is used by many boards */
824
825#define MII_LXT970_MIRROR 16 /* Mirror register */
826#define MII_LXT970_IER 17 /* Interrupt Enable Register */
827#define MII_LXT970_ISR 18 /* Interrupt Status Register */
828#define MII_LXT970_CONFIG 19 /* Configuration Register */
829#define MII_LXT970_CSR 20 /* Chip Status Register */
830
831static void mii_parse_lxt970_csr(uint mii_reg, struct net_device *dev)
832{
833 struct fec_enet_private *fep = netdev_priv(dev);
834 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000835 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000837 status = *s & ~(PHY_STAT_SPMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (mii_reg & 0x0800) {
839 if (mii_reg & 0x1000)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000840 status |= PHY_STAT_100FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000842 status |= PHY_STAT_100HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 } else {
844 if (mii_reg & 0x1000)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000845 status |= PHY_STAT_10FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000847 status |= PHY_STAT_10HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000849 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000852static phy_cmd_t const phy_cmd_lxt970_config[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 { mk_mii_read(MII_REG_CR), mii_parse_cr },
854 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
855 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000856 };
857static phy_cmd_t const phy_cmd_lxt970_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 { mk_mii_write(MII_LXT970_IER, 0x0002), NULL },
859 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
860 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000861 };
862static phy_cmd_t const phy_cmd_lxt970_ack_int[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 /* read SR and ISR to acknowledge */
864 { mk_mii_read(MII_REG_SR), mii_parse_sr },
865 { mk_mii_read(MII_LXT970_ISR), NULL },
866
867 /* find out the current status */
868 { mk_mii_read(MII_LXT970_CSR), mii_parse_lxt970_csr },
869 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000870 };
871static phy_cmd_t const phy_cmd_lxt970_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 { mk_mii_write(MII_LXT970_IER, 0x0000), NULL },
873 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000874 };
875static phy_info_t const phy_info_lxt970 = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400876 .id = 0x07810000,
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000877 .name = "LXT970",
878 .config = phy_cmd_lxt970_config,
879 .startup = phy_cmd_lxt970_startup,
880 .ack_int = phy_cmd_lxt970_ack_int,
881 .shutdown = phy_cmd_lxt970_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882};
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884/* ------------------------------------------------------------------------- */
885/* The Level one LXT971 is used on some of my custom boards */
886
887/* register definitions for the 971 */
888
889#define MII_LXT971_PCR 16 /* Port Control Register */
890#define MII_LXT971_SR2 17 /* Status Register 2 */
891#define MII_LXT971_IER 18 /* Interrupt Enable Register */
892#define MII_LXT971_ISR 19 /* Interrupt Status Register */
893#define MII_LXT971_LCR 20 /* LED Control Register */
894#define MII_LXT971_TCR 30 /* Transmit Control Register */
895
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400896/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 * I had some nice ideas of running the MDIO faster...
898 * The 971 should support 8MHz and I tried it, but things acted really
899 * weird, so 2.5 MHz ought to be enough for anyone...
900 */
901
902static void mii_parse_lxt971_sr2(uint mii_reg, struct net_device *dev)
903{
904 struct fec_enet_private *fep = netdev_priv(dev);
905 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000906 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000908 status = *s & ~(PHY_STAT_SPMASK | PHY_STAT_LINK | PHY_STAT_ANC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 if (mii_reg & 0x0400) {
911 fep->link = 1;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000912 status |= PHY_STAT_LINK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 } else {
914 fep->link = 0;
915 }
916 if (mii_reg & 0x0080)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000917 status |= PHY_STAT_ANC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (mii_reg & 0x4000) {
919 if (mii_reg & 0x0200)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000920 status |= PHY_STAT_100FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000922 status |= PHY_STAT_100HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 } else {
924 if (mii_reg & 0x0200)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000925 status |= PHY_STAT_10FDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000927 status |= PHY_STAT_10HDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929 if (mii_reg & 0x0008)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000930 status |= PHY_STAT_FAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000932 *s = status;
933}
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400934
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000935static phy_cmd_t const phy_cmd_lxt971_config[] = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400936 /* limit to 10MBit because my prototype board
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 * doesn't work with 100. */
938 { mk_mii_read(MII_REG_CR), mii_parse_cr },
939 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
940 { mk_mii_read(MII_LXT971_SR2), mii_parse_lxt971_sr2 },
941 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000942 };
943static phy_cmd_t const phy_cmd_lxt971_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 { mk_mii_write(MII_LXT971_IER, 0x00f2), NULL },
945 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
946 { mk_mii_write(MII_LXT971_LCR, 0xd422), NULL }, /* LED config */
947 /* Somehow does the 971 tell me that the link is down
948 * the first read after power-up.
949 * read here to get a valid value in ack_int */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400950 { mk_mii_read(MII_REG_SR), mii_parse_sr },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000952 };
953static phy_cmd_t const phy_cmd_lxt971_ack_int[] = {
954 /* acknowledge the int before reading status ! */
955 { mk_mii_read(MII_LXT971_ISR), NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 /* find out the current status */
957 { mk_mii_read(MII_REG_SR), mii_parse_sr },
958 { mk_mii_read(MII_LXT971_SR2), mii_parse_lxt971_sr2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000960 };
961static phy_cmd_t const phy_cmd_lxt971_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 { mk_mii_write(MII_LXT971_IER, 0x0000), NULL },
963 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000964 };
965static phy_info_t const phy_info_lxt971 = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400966 .id = 0x0001378e,
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000967 .name = "LXT971",
968 .config = phy_cmd_lxt971_config,
969 .startup = phy_cmd_lxt971_startup,
970 .ack_int = phy_cmd_lxt971_ack_int,
971 .shutdown = phy_cmd_lxt971_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972};
973
974/* ------------------------------------------------------------------------- */
975/* The Quality Semiconductor QS6612 is used on the RPX CLLF */
976
977/* register definitions */
978
979#define MII_QS6612_MCR 17 /* Mode Control Register */
980#define MII_QS6612_FTR 27 /* Factory Test Register */
981#define MII_QS6612_MCO 28 /* Misc. Control Register */
982#define MII_QS6612_ISR 29 /* Interrupt Source Register */
983#define MII_QS6612_IMR 30 /* Interrupt Mask Register */
984#define MII_QS6612_PCR 31 /* 100BaseTx PHY Control Reg. */
985
986static void mii_parse_qs6612_pcr(uint mii_reg, struct net_device *dev)
987{
988 struct fec_enet_private *fep = netdev_priv(dev);
989 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000990 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000992 status = *s & ~(PHY_STAT_SPMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 switch((mii_reg >> 2) & 7) {
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000995 case 1: status |= PHY_STAT_10HDX; break;
996 case 2: status |= PHY_STAT_100HDX; break;
997 case 5: status |= PHY_STAT_10FDX; break;
998 case 6: status |= PHY_STAT_100FDX; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001001 *s = status;
1002}
1003
1004static phy_cmd_t const phy_cmd_qs6612_config[] = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001005 /* The PHY powers up isolated on the RPX,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 * so send a command to allow operation.
1007 */
1008 { mk_mii_write(MII_QS6612_PCR, 0x0dc0), NULL },
1009
1010 /* parse cr and anar to get some info */
1011 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1012 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1013 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001014 };
1015static phy_cmd_t const phy_cmd_qs6612_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 { mk_mii_write(MII_QS6612_IMR, 0x003a), NULL },
1017 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
1018 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001019 };
1020static phy_cmd_t const phy_cmd_qs6612_ack_int[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 /* we need to read ISR, SR and ANER to acknowledge */
1022 { mk_mii_read(MII_QS6612_ISR), NULL },
1023 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1024 { mk_mii_read(MII_REG_ANER), NULL },
1025
1026 /* read pcr to get info */
1027 { mk_mii_read(MII_QS6612_PCR), mii_parse_qs6612_pcr },
1028 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001029 };
1030static phy_cmd_t const phy_cmd_qs6612_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 { mk_mii_write(MII_QS6612_IMR, 0x0000), NULL },
1032 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001033 };
1034static phy_info_t const phy_info_qs6612 = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001035 .id = 0x00181440,
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001036 .name = "QS6612",
1037 .config = phy_cmd_qs6612_config,
1038 .startup = phy_cmd_qs6612_startup,
1039 .ack_int = phy_cmd_qs6612_ack_int,
1040 .shutdown = phy_cmd_qs6612_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041};
1042
1043/* ------------------------------------------------------------------------- */
1044/* AMD AM79C874 phy */
1045
1046/* register definitions for the 874 */
1047
1048#define MII_AM79C874_MFR 16 /* Miscellaneous Feature Register */
1049#define MII_AM79C874_ICSR 17 /* Interrupt/Status Register */
1050#define MII_AM79C874_DR 18 /* Diagnostic Register */
1051#define MII_AM79C874_PMLR 19 /* Power and Loopback Register */
1052#define MII_AM79C874_MCR 21 /* ModeControl Register */
1053#define MII_AM79C874_DC 23 /* Disconnect Counter */
1054#define MII_AM79C874_REC 24 /* Recieve Error Counter */
1055
1056static void mii_parse_am79c874_dr(uint mii_reg, struct net_device *dev)
1057{
1058 struct fec_enet_private *fep = netdev_priv(dev);
1059 volatile uint *s = &(fep->phy_status);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001060 uint status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001062 status = *s & ~(PHY_STAT_SPMASK | PHY_STAT_ANC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 if (mii_reg & 0x0080)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001065 status |= PHY_STAT_ANC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (mii_reg & 0x0400)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001067 status |= ((mii_reg & 0x0800) ? PHY_STAT_100FDX : PHY_STAT_100HDX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 else
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001069 status |= ((mii_reg & 0x0800) ? PHY_STAT_10FDX : PHY_STAT_10HDX);
1070
1071 *s = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001074static phy_cmd_t const phy_cmd_am79c874_config[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1076 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1077 { mk_mii_read(MII_AM79C874_DR), mii_parse_am79c874_dr },
1078 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001079 };
1080static phy_cmd_t const phy_cmd_am79c874_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 { mk_mii_write(MII_AM79C874_ICSR, 0xff00), NULL },
1082 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001083 { mk_mii_read(MII_REG_SR), mii_parse_sr },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001085 };
1086static phy_cmd_t const phy_cmd_am79c874_ack_int[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 /* find out the current status */
1088 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1089 { mk_mii_read(MII_AM79C874_DR), mii_parse_am79c874_dr },
1090 /* we only need to read ISR to acknowledge */
1091 { mk_mii_read(MII_AM79C874_ICSR), NULL },
1092 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001093 };
1094static phy_cmd_t const phy_cmd_am79c874_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 { mk_mii_write(MII_AM79C874_ICSR, 0x0000), NULL },
1096 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001097 };
1098static phy_info_t const phy_info_am79c874 = {
1099 .id = 0x00022561,
1100 .name = "AM79C874",
1101 .config = phy_cmd_am79c874_config,
1102 .startup = phy_cmd_am79c874_startup,
1103 .ack_int = phy_cmd_am79c874_ack_int,
1104 .shutdown = phy_cmd_am79c874_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105};
1106
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108/* ------------------------------------------------------------------------- */
1109/* Kendin KS8721BL phy */
1110
1111/* register definitions for the 8721 */
1112
1113#define MII_KS8721BL_RXERCR 21
1114#define MII_KS8721BL_ICSR 22
1115#define MII_KS8721BL_PHYCR 31
1116
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001117static phy_cmd_t const phy_cmd_ks8721bl_config[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1119 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1120 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001121 };
1122static phy_cmd_t const phy_cmd_ks8721bl_startup[] = { /* enable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 { mk_mii_write(MII_KS8721BL_ICSR, 0xff00), NULL },
1124 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001125 { mk_mii_read(MII_REG_SR), mii_parse_sr },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001127 };
1128static phy_cmd_t const phy_cmd_ks8721bl_ack_int[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /* find out the current status */
1130 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1131 /* we only need to read ISR to acknowledge */
1132 { mk_mii_read(MII_KS8721BL_ICSR), NULL },
1133 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001134 };
1135static phy_cmd_t const phy_cmd_ks8721bl_shutdown[] = { /* disable interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 { mk_mii_write(MII_KS8721BL_ICSR, 0x0000), NULL },
1137 { mk_mii_end, }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001138 };
1139static phy_info_t const phy_info_ks8721bl = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001140 .id = 0x00022161,
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001141 .name = "KS8721BL",
1142 .config = phy_cmd_ks8721bl_config,
1143 .startup = phy_cmd_ks8721bl_startup,
1144 .ack_int = phy_cmd_ks8721bl_ack_int,
1145 .shutdown = phy_cmd_ks8721bl_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146};
1147
1148/* ------------------------------------------------------------------------- */
Greg Ungerer562d2f82005-11-07 14:09:50 +10001149/* register definitions for the DP83848 */
1150
1151#define MII_DP8384X_PHYSTST 16 /* PHY Status Register */
1152
1153static void mii_parse_dp8384x_sr2(uint mii_reg, struct net_device *dev)
1154{
Wang Chen4cf16532008-11-12 23:38:14 -08001155 struct fec_enet_private *fep = netdev_priv(dev);
Greg Ungerer562d2f82005-11-07 14:09:50 +10001156 volatile uint *s = &(fep->phy_status);
1157
1158 *s &= ~(PHY_STAT_SPMASK | PHY_STAT_LINK | PHY_STAT_ANC);
1159
1160 /* Link up */
1161 if (mii_reg & 0x0001) {
1162 fep->link = 1;
1163 *s |= PHY_STAT_LINK;
1164 } else
1165 fep->link = 0;
1166 /* Status of link */
1167 if (mii_reg & 0x0010) /* Autonegotioation complete */
1168 *s |= PHY_STAT_ANC;
1169 if (mii_reg & 0x0002) { /* 10MBps? */
1170 if (mii_reg & 0x0004) /* Full Duplex? */
1171 *s |= PHY_STAT_10FDX;
1172 else
1173 *s |= PHY_STAT_10HDX;
1174 } else { /* 100 Mbps? */
1175 if (mii_reg & 0x0004) /* Full Duplex? */
1176 *s |= PHY_STAT_100FDX;
1177 else
1178 *s |= PHY_STAT_100HDX;
1179 }
1180 if (mii_reg & 0x0008)
1181 *s |= PHY_STAT_FAULT;
1182}
1183
1184static phy_info_t phy_info_dp83848= {
1185 0x020005c9,
1186 "DP83848",
1187
1188 (const phy_cmd_t []) { /* config */
1189 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1190 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1191 { mk_mii_read(MII_DP8384X_PHYSTST), mii_parse_dp8384x_sr2 },
1192 { mk_mii_end, }
1193 },
1194 (const phy_cmd_t []) { /* startup - enable interrupts */
1195 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
1196 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1197 { mk_mii_end, }
1198 },
1199 (const phy_cmd_t []) { /* ack_int - never happens, no interrupt */
1200 { mk_mii_end, }
1201 },
1202 (const phy_cmd_t []) { /* shutdown */
1203 { mk_mii_end, }
1204 },
1205};
1206
1207/* ------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001209static phy_info_t const * const phy_info[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 &phy_info_lxt970,
1211 &phy_info_lxt971,
1212 &phy_info_qs6612,
1213 &phy_info_am79c874,
1214 &phy_info_ks8721bl,
Greg Ungerer562d2f82005-11-07 14:09:50 +10001215 &phy_info_dp83848,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 NULL
1217};
1218
1219/* ------------------------------------------------------------------------- */
Sebastian Siewiorc1d96152008-05-01 14:04:02 +10001220#ifdef HAVE_mii_link_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001222mii_link_interrupt(int irq, void * dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223#endif
1224
1225#if defined(CONFIG_M5272)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226/*
1227 * Code specific to Coldfire 5272 setup.
1228 */
1229static void __inline__ fec_request_intrs(struct net_device *dev)
1230{
1231 volatile unsigned long *icrp;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001232 static const struct idesc {
1233 char *name;
1234 unsigned short irq;
David Howells7d12e782006-10-05 14:55:46 +01001235 irq_handler_t handler;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001236 } *idp, id[] = {
1237 { "fec(RX)", 86, fec_enet_interrupt },
1238 { "fec(TX)", 87, fec_enet_interrupt },
1239 { "fec(OTHER)", 88, fec_enet_interrupt },
1240 { "fec(MII)", 66, mii_link_interrupt },
1241 { NULL },
1242 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 /* Setup interrupt handlers. */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001245 for (idp = id; idp->name; idp++) {
Greg Ungerer0a504772008-03-04 16:52:01 +10001246 if (request_irq(idp->irq, idp->handler, IRQF_DISABLED, idp->name, dev) != 0)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001247 printk("FEC: Could not allocate %s IRQ(%d)!\n", idp->name, idp->irq);
1248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 /* Unmask interrupt at ColdFire 5272 SIM */
1251 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR3);
1252 *icrp = 0x00000ddd;
1253 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR1);
Greg Ungererf861d622007-07-30 16:29:16 +10001254 *icrp = 0x0d000000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
1257static void __inline__ fec_set_mii(struct net_device *dev, struct fec_enet_private *fep)
1258{
1259 volatile fec_t *fecp;
1260
1261 fecp = fep->hwp;
1262 fecp->fec_r_cntrl = OPT_FRAME_SIZE | 0x04;
1263 fecp->fec_x_cntrl = 0x00;
1264
1265 /*
1266 * Set MII speed to 2.5 MHz
1267 * See 5272 manual section 11.5.8: MSCR
1268 */
1269 fep->phy_speed = ((((MCF_CLK / 4) / (2500000 / 10)) + 5) / 10) * 2;
1270 fecp->fec_mii_speed = fep->phy_speed;
1271
1272 fec_restart(dev, 0);
1273}
1274
1275static void __inline__ fec_get_mac(struct net_device *dev)
1276{
1277 struct fec_enet_private *fep = netdev_priv(dev);
1278 volatile fec_t *fecp;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001279 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 fecp = fep->hwp;
1282
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001283 if (FEC_FLASHMAC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 /*
1285 * Get MAC address from FLASH.
1286 * If it is all 1's or 0's, use the default.
1287 */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001288 iap = (unsigned char *)FEC_FLASHMAC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if ((iap[0] == 0) && (iap[1] == 0) && (iap[2] == 0) &&
1290 (iap[3] == 0) && (iap[4] == 0) && (iap[5] == 0))
1291 iap = fec_mac_default;
1292 if ((iap[0] == 0xff) && (iap[1] == 0xff) && (iap[2] == 0xff) &&
1293 (iap[3] == 0xff) && (iap[4] == 0xff) && (iap[5] == 0xff))
1294 iap = fec_mac_default;
1295 } else {
1296 *((unsigned long *) &tmpaddr[0]) = fecp->fec_addr_low;
1297 *((unsigned short *) &tmpaddr[4]) = (fecp->fec_addr_high >> 16);
1298 iap = &tmpaddr[0];
1299 }
1300
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001301 memcpy(dev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 /* Adjust MAC if using default MAC address */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001304 if (iap == fec_mac_default)
1305 dev->dev_addr[ETH_ALEN-1] = fec_mac_default[ETH_ALEN-1] + fep->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308static void __inline__ fec_disable_phy_intr(void)
1309{
1310 volatile unsigned long *icrp;
1311 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR1);
Greg Ungererf861d622007-07-30 16:29:16 +10001312 *icrp = 0x08000000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313}
1314
1315static void __inline__ fec_phy_ack_intr(void)
1316{
1317 volatile unsigned long *icrp;
1318 /* Acknowledge the interrupt */
1319 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR1);
Greg Ungererf861d622007-07-30 16:29:16 +10001320 *icrp = 0x0d000000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323/* ------------------------------------------------------------------------- */
1324
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001325#elif defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327/*
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001328 * Code specific to Coldfire 5230/5231/5232/5234/5235,
1329 * the 5270/5271/5274/5275 and 5280/5282 setups.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 */
1331static void __inline__ fec_request_intrs(struct net_device *dev)
1332{
1333 struct fec_enet_private *fep;
1334 int b;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001335 static const struct idesc {
1336 char *name;
1337 unsigned short irq;
1338 } *idp, id[] = {
1339 { "fec(TXF)", 23 },
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001340 { "fec(RXF)", 27 },
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001341 { "fec(MII)", 29 },
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001342 { NULL },
1343 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 fep = netdev_priv(dev);
1346 b = (fep->index) ? 128 : 64;
1347
1348 /* Setup interrupt handlers. */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001349 for (idp = id; idp->name; idp++) {
Greg Ungerer0a504772008-03-04 16:52:01 +10001350 if (request_irq(b+idp->irq, fec_enet_interrupt, IRQF_DISABLED, idp->name, dev) != 0)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001351 printk("FEC: Could not allocate %s IRQ(%d)!\n", idp->name, b+idp->irq);
1352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 /* Unmask interrupts at ColdFire 5280/5282 interrupt controller */
1355 {
1356 volatile unsigned char *icrp;
1357 volatile unsigned long *imrp;
Willson Callan83901fc2006-06-27 13:13:44 +10001358 int i, ilip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 b = (fep->index) ? MCFICM_INTC1 : MCFICM_INTC0;
1361 icrp = (volatile unsigned char *) (MCF_IPSBAR + b +
1362 MCFINTC_ICR0);
Willson Callan83901fc2006-06-27 13:13:44 +10001363 for (i = 23, ilip = 0x28; (i < 36); i++)
1364 icrp[i] = ilip--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 imrp = (volatile unsigned long *) (MCF_IPSBAR + b +
1367 MCFINTC_IMRH);
1368 *imrp &= ~0x0000000f;
1369 imrp = (volatile unsigned long *) (MCF_IPSBAR + b +
1370 MCFINTC_IMRL);
1371 *imrp &= ~0xff800001;
1372 }
1373
1374#if defined(CONFIG_M528x)
1375 /* Set up gpio outputs for MII lines */
1376 {
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001377 volatile u16 *gpio_paspar;
1378 volatile u8 *gpio_pehlpar;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001379
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001380 gpio_paspar = (volatile u16 *) (MCF_IPSBAR + 0x100056);
1381 gpio_pehlpar = (volatile u16 *) (MCF_IPSBAR + 0x100058);
1382 *gpio_paspar |= 0x0f00;
1383 *gpio_pehlpar = 0xc0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
1385#endif
Mike Cruseb8a94b32007-07-30 16:29:29 +10001386
1387#if defined(CONFIG_M527x)
1388 /* Set up gpio outputs for MII lines */
1389 {
1390 volatile u8 *gpio_par_fec;
1391 volatile u16 *gpio_par_feci2c;
1392
1393 gpio_par_feci2c = (volatile u16 *)(MCF_IPSBAR + 0x100082);
1394 /* Set up gpio outputs for FEC0 MII lines */
1395 gpio_par_fec = (volatile u8 *)(MCF_IPSBAR + 0x100078);
1396
1397 *gpio_par_feci2c |= 0x0f00;
1398 *gpio_par_fec |= 0xc0;
1399
1400#if defined(CONFIG_FEC2)
1401 /* Set up gpio outputs for FEC1 MII lines */
1402 gpio_par_fec = (volatile u8 *)(MCF_IPSBAR + 0x100079);
1403
1404 *gpio_par_feci2c |= 0x00a0;
1405 *gpio_par_fec |= 0xc0;
1406#endif /* CONFIG_FEC2 */
1407 }
1408#endif /* CONFIG_M527x */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
1410
1411static void __inline__ fec_set_mii(struct net_device *dev, struct fec_enet_private *fep)
1412{
1413 volatile fec_t *fecp;
1414
1415 fecp = fep->hwp;
1416 fecp->fec_r_cntrl = OPT_FRAME_SIZE | 0x04;
1417 fecp->fec_x_cntrl = 0x00;
1418
1419 /*
1420 * Set MII speed to 2.5 MHz
1421 * See 5282 manual section 17.5.4.7: MSCR
1422 */
1423 fep->phy_speed = ((((MCF_CLK / 2) / (2500000 / 10)) + 5) / 10) * 2;
1424 fecp->fec_mii_speed = fep->phy_speed;
1425
1426 fec_restart(dev, 0);
1427}
1428
1429static void __inline__ fec_get_mac(struct net_device *dev)
1430{
1431 struct fec_enet_private *fep = netdev_priv(dev);
1432 volatile fec_t *fecp;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001433 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 fecp = fep->hwp;
1436
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001437 if (FEC_FLASHMAC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 /*
1439 * Get MAC address from FLASH.
1440 * If it is all 1's or 0's, use the default.
1441 */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001442 iap = FEC_FLASHMAC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if ((iap[0] == 0) && (iap[1] == 0) && (iap[2] == 0) &&
1444 (iap[3] == 0) && (iap[4] == 0) && (iap[5] == 0))
1445 iap = fec_mac_default;
1446 if ((iap[0] == 0xff) && (iap[1] == 0xff) && (iap[2] == 0xff) &&
1447 (iap[3] == 0xff) && (iap[4] == 0xff) && (iap[5] == 0xff))
1448 iap = fec_mac_default;
1449 } else {
1450 *((unsigned long *) &tmpaddr[0]) = fecp->fec_addr_low;
1451 *((unsigned short *) &tmpaddr[4]) = (fecp->fec_addr_high >> 16);
1452 iap = &tmpaddr[0];
1453 }
1454
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001455 memcpy(dev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 /* Adjust MAC if using default MAC address */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001458 if (iap == fec_mac_default)
1459 dev->dev_addr[ETH_ALEN-1] = fec_mac_default[ETH_ALEN-1] + fep->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462static void __inline__ fec_disable_phy_intr(void)
1463{
1464}
1465
1466static void __inline__ fec_phy_ack_intr(void)
1467{
1468}
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470/* ------------------------------------------------------------------------- */
1471
Greg Ungerer562d2f82005-11-07 14:09:50 +10001472#elif defined(CONFIG_M520x)
1473
1474/*
1475 * Code specific to Coldfire 520x
1476 */
1477static void __inline__ fec_request_intrs(struct net_device *dev)
1478{
1479 struct fec_enet_private *fep;
1480 int b;
1481 static const struct idesc {
1482 char *name;
1483 unsigned short irq;
1484 } *idp, id[] = {
1485 { "fec(TXF)", 23 },
Greg Ungerer562d2f82005-11-07 14:09:50 +10001486 { "fec(RXF)", 27 },
Greg Ungerer562d2f82005-11-07 14:09:50 +10001487 { "fec(MII)", 29 },
Greg Ungerer562d2f82005-11-07 14:09:50 +10001488 { NULL },
1489 };
1490
1491 fep = netdev_priv(dev);
1492 b = 64 + 13;
1493
1494 /* Setup interrupt handlers. */
1495 for (idp = id; idp->name; idp++) {
Greg Ungerer0a504772008-03-04 16:52:01 +10001496 if (request_irq(b+idp->irq, fec_enet_interrupt, IRQF_DISABLED, idp->name,dev) != 0)
Greg Ungerer562d2f82005-11-07 14:09:50 +10001497 printk("FEC: Could not allocate %s IRQ(%d)!\n", idp->name, b+idp->irq);
1498 }
1499
1500 /* Unmask interrupts at ColdFire interrupt controller */
1501 {
1502 volatile unsigned char *icrp;
1503 volatile unsigned long *imrp;
1504
1505 icrp = (volatile unsigned char *) (MCF_IPSBAR + MCFICM_INTC0 +
1506 MCFINTC_ICR0);
1507 for (b = 36; (b < 49); b++)
1508 icrp[b] = 0x04;
1509 imrp = (volatile unsigned long *) (MCF_IPSBAR + MCFICM_INTC0 +
1510 MCFINTC_IMRH);
1511 *imrp &= ~0x0001FFF0;
1512 }
1513 *(volatile unsigned char *)(MCF_IPSBAR + MCF_GPIO_PAR_FEC) |= 0xf0;
1514 *(volatile unsigned char *)(MCF_IPSBAR + MCF_GPIO_PAR_FECI2C) |= 0x0f;
1515}
1516
1517static void __inline__ fec_set_mii(struct net_device *dev, struct fec_enet_private *fep)
1518{
1519 volatile fec_t *fecp;
1520
1521 fecp = fep->hwp;
1522 fecp->fec_r_cntrl = OPT_FRAME_SIZE | 0x04;
1523 fecp->fec_x_cntrl = 0x00;
1524
1525 /*
1526 * Set MII speed to 2.5 MHz
1527 * See 5282 manual section 17.5.4.7: MSCR
1528 */
1529 fep->phy_speed = ((((MCF_CLK / 2) / (2500000 / 10)) + 5) / 10) * 2;
1530 fecp->fec_mii_speed = fep->phy_speed;
1531
1532 fec_restart(dev, 0);
1533}
1534
1535static void __inline__ fec_get_mac(struct net_device *dev)
1536{
1537 struct fec_enet_private *fep = netdev_priv(dev);
1538 volatile fec_t *fecp;
1539 unsigned char *iap, tmpaddr[ETH_ALEN];
1540
1541 fecp = fep->hwp;
1542
1543 if (FEC_FLASHMAC) {
1544 /*
1545 * Get MAC address from FLASH.
1546 * If it is all 1's or 0's, use the default.
1547 */
1548 iap = FEC_FLASHMAC;
1549 if ((iap[0] == 0) && (iap[1] == 0) && (iap[2] == 0) &&
1550 (iap[3] == 0) && (iap[4] == 0) && (iap[5] == 0))
1551 iap = fec_mac_default;
1552 if ((iap[0] == 0xff) && (iap[1] == 0xff) && (iap[2] == 0xff) &&
1553 (iap[3] == 0xff) && (iap[4] == 0xff) && (iap[5] == 0xff))
1554 iap = fec_mac_default;
1555 } else {
1556 *((unsigned long *) &tmpaddr[0]) = fecp->fec_addr_low;
1557 *((unsigned short *) &tmpaddr[4]) = (fecp->fec_addr_high >> 16);
1558 iap = &tmpaddr[0];
1559 }
1560
1561 memcpy(dev->dev_addr, iap, ETH_ALEN);
1562
1563 /* Adjust MAC if using default MAC address */
1564 if (iap == fec_mac_default)
1565 dev->dev_addr[ETH_ALEN-1] = fec_mac_default[ETH_ALEN-1] + fep->index;
1566}
1567
Greg Ungerer562d2f82005-11-07 14:09:50 +10001568static void __inline__ fec_disable_phy_intr(void)
1569{
1570}
1571
1572static void __inline__ fec_phy_ack_intr(void)
1573{
1574}
1575
Greg Ungerer562d2f82005-11-07 14:09:50 +10001576/* ------------------------------------------------------------------------- */
1577
Matt Waddel6b265292006-06-27 13:10:56 +10001578#elif defined(CONFIG_M532x)
1579/*
1580 * Code specific for M532x
1581 */
1582static void __inline__ fec_request_intrs(struct net_device *dev)
1583{
1584 struct fec_enet_private *fep;
1585 int b;
1586 static const struct idesc {
1587 char *name;
1588 unsigned short irq;
1589 } *idp, id[] = {
1590 { "fec(TXF)", 36 },
Matt Waddel6b265292006-06-27 13:10:56 +10001591 { "fec(RXF)", 40 },
Matt Waddel6b265292006-06-27 13:10:56 +10001592 { "fec(MII)", 42 },
Matt Waddel6b265292006-06-27 13:10:56 +10001593 { NULL },
1594 };
1595
1596 fep = netdev_priv(dev);
1597 b = (fep->index) ? 128 : 64;
1598
1599 /* Setup interrupt handlers. */
1600 for (idp = id; idp->name; idp++) {
Greg Ungerer0a504772008-03-04 16:52:01 +10001601 if (request_irq(b+idp->irq, fec_enet_interrupt, IRQF_DISABLED, idp->name,dev) != 0)
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001602 printk("FEC: Could not allocate %s IRQ(%d)!\n",
Matt Waddel6b265292006-06-27 13:10:56 +10001603 idp->name, b+idp->irq);
1604 }
1605
1606 /* Unmask interrupts */
1607 MCF_INTC0_ICR36 = 0x2;
1608 MCF_INTC0_ICR37 = 0x2;
1609 MCF_INTC0_ICR38 = 0x2;
1610 MCF_INTC0_ICR39 = 0x2;
1611 MCF_INTC0_ICR40 = 0x2;
1612 MCF_INTC0_ICR41 = 0x2;
1613 MCF_INTC0_ICR42 = 0x2;
1614 MCF_INTC0_ICR43 = 0x2;
1615 MCF_INTC0_ICR44 = 0x2;
1616 MCF_INTC0_ICR45 = 0x2;
1617 MCF_INTC0_ICR46 = 0x2;
1618 MCF_INTC0_ICR47 = 0x2;
1619 MCF_INTC0_ICR48 = 0x2;
1620
1621 MCF_INTC0_IMRH &= ~(
1622 MCF_INTC_IMRH_INT_MASK36 |
1623 MCF_INTC_IMRH_INT_MASK37 |
1624 MCF_INTC_IMRH_INT_MASK38 |
1625 MCF_INTC_IMRH_INT_MASK39 |
1626 MCF_INTC_IMRH_INT_MASK40 |
1627 MCF_INTC_IMRH_INT_MASK41 |
1628 MCF_INTC_IMRH_INT_MASK42 |
1629 MCF_INTC_IMRH_INT_MASK43 |
1630 MCF_INTC_IMRH_INT_MASK44 |
1631 MCF_INTC_IMRH_INT_MASK45 |
1632 MCF_INTC_IMRH_INT_MASK46 |
1633 MCF_INTC_IMRH_INT_MASK47 |
1634 MCF_INTC_IMRH_INT_MASK48 );
1635
1636 /* Set up gpio outputs for MII lines */
1637 MCF_GPIO_PAR_FECI2C |= (0 |
1638 MCF_GPIO_PAR_FECI2C_PAR_MDC_EMDC |
1639 MCF_GPIO_PAR_FECI2C_PAR_MDIO_EMDIO);
1640 MCF_GPIO_PAR_FEC = (0 |
1641 MCF_GPIO_PAR_FEC_PAR_FEC_7W_FEC |
1642 MCF_GPIO_PAR_FEC_PAR_FEC_MII_FEC);
1643}
1644
1645static void __inline__ fec_set_mii(struct net_device *dev, struct fec_enet_private *fep)
1646{
1647 volatile fec_t *fecp;
1648
1649 fecp = fep->hwp;
1650 fecp->fec_r_cntrl = OPT_FRAME_SIZE | 0x04;
1651 fecp->fec_x_cntrl = 0x00;
1652
1653 /*
1654 * Set MII speed to 2.5 MHz
1655 */
1656 fep->phy_speed = ((((MCF_CLK / 2) / (2500000 / 10)) + 5) / 10) * 2;
1657 fecp->fec_mii_speed = fep->phy_speed;
1658
1659 fec_restart(dev, 0);
1660}
1661
1662static void __inline__ fec_get_mac(struct net_device *dev)
1663{
1664 struct fec_enet_private *fep = netdev_priv(dev);
1665 volatile fec_t *fecp;
1666 unsigned char *iap, tmpaddr[ETH_ALEN];
1667
1668 fecp = fep->hwp;
1669
1670 if (FEC_FLASHMAC) {
1671 /*
1672 * Get MAC address from FLASH.
1673 * If it is all 1's or 0's, use the default.
1674 */
1675 iap = FEC_FLASHMAC;
1676 if ((iap[0] == 0) && (iap[1] == 0) && (iap[2] == 0) &&
1677 (iap[3] == 0) && (iap[4] == 0) && (iap[5] == 0))
1678 iap = fec_mac_default;
1679 if ((iap[0] == 0xff) && (iap[1] == 0xff) && (iap[2] == 0xff) &&
1680 (iap[3] == 0xff) && (iap[4] == 0xff) && (iap[5] == 0xff))
1681 iap = fec_mac_default;
1682 } else {
1683 *((unsigned long *) &tmpaddr[0]) = fecp->fec_addr_low;
1684 *((unsigned short *) &tmpaddr[4]) = (fecp->fec_addr_high >> 16);
1685 iap = &tmpaddr[0];
1686 }
1687
1688 memcpy(dev->dev_addr, iap, ETH_ALEN);
1689
1690 /* Adjust MAC if using default MAC address */
1691 if (iap == fec_mac_default)
1692 dev->dev_addr[ETH_ALEN-1] = fec_mac_default[ETH_ALEN-1] + fep->index;
1693}
1694
Matt Waddel6b265292006-06-27 13:10:56 +10001695static void __inline__ fec_disable_phy_intr(void)
1696{
1697}
1698
1699static void __inline__ fec_phy_ack_intr(void)
1700{
1701}
1702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703#endif
1704
1705/* ------------------------------------------------------------------------- */
1706
1707static void mii_display_status(struct net_device *dev)
1708{
1709 struct fec_enet_private *fep = netdev_priv(dev);
1710 volatile uint *s = &(fep->phy_status);
1711
1712 if (!fep->link && !fep->old_link) {
1713 /* Link is still down - don't print anything */
1714 return;
1715 }
1716
1717 printk("%s: status: ", dev->name);
1718
1719 if (!fep->link) {
1720 printk("link down");
1721 } else {
1722 printk("link up");
1723
1724 switch(*s & PHY_STAT_SPMASK) {
1725 case PHY_STAT_100FDX: printk(", 100MBit Full Duplex"); break;
1726 case PHY_STAT_100HDX: printk(", 100MBit Half Duplex"); break;
1727 case PHY_STAT_10FDX: printk(", 10MBit Full Duplex"); break;
1728 case PHY_STAT_10HDX: printk(", 10MBit Half Duplex"); break;
1729 default:
1730 printk(", Unknown speed/duplex");
1731 }
1732
1733 if (*s & PHY_STAT_ANC)
1734 printk(", auto-negotiation complete");
1735 }
1736
1737 if (*s & PHY_STAT_FAULT)
1738 printk(", remote fault");
1739
1740 printk(".\n");
1741}
1742
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001743static void mii_display_config(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001745 struct fec_enet_private *fep = container_of(work, struct fec_enet_private, phy_task);
1746 struct net_device *dev = fep->netdev;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001747 uint status = fep->phy_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749 /*
1750 ** When we get here, phy_task is already removed from
1751 ** the workqueue. It is thus safe to allow to reuse it.
1752 */
1753 fep->mii_phy_task_queued = 0;
1754 printk("%s: config: auto-negotiation ", dev->name);
1755
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001756 if (status & PHY_CONF_ANE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 printk("on");
1758 else
1759 printk("off");
1760
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001761 if (status & PHY_CONF_100FDX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 printk(", 100FDX");
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001763 if (status & PHY_CONF_100HDX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 printk(", 100HDX");
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001765 if (status & PHY_CONF_10FDX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 printk(", 10FDX");
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001767 if (status & PHY_CONF_10HDX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 printk(", 10HDX");
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001769 if (!(status & PHY_CONF_SPMASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 printk(", No speed/duplex selected?");
1771
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001772 if (status & PHY_CONF_LOOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 printk(", loopback enabled");
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 printk(".\n");
1776
1777 fep->sequence_done = 1;
1778}
1779
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001780static void mii_relink(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001782 struct fec_enet_private *fep = container_of(work, struct fec_enet_private, phy_task);
1783 struct net_device *dev = fep->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 int duplex;
1785
1786 /*
1787 ** When we get here, phy_task is already removed from
1788 ** the workqueue. It is thus safe to allow to reuse it.
1789 */
1790 fep->mii_phy_task_queued = 0;
1791 fep->link = (fep->phy_status & PHY_STAT_LINK) ? 1 : 0;
1792 mii_display_status(dev);
1793 fep->old_link = fep->link;
1794
1795 if (fep->link) {
1796 duplex = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001797 if (fep->phy_status
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 & (PHY_STAT_100FDX | PHY_STAT_10FDX))
1799 duplex = 1;
1800 fec_restart(dev, duplex);
Philippe De Muyterf909b1e2007-10-23 14:37:54 +10001801 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 fec_stop(dev);
1803
1804#if 0
1805 enable_irq(fep->mii_irq);
1806#endif
1807
1808}
1809
1810/* mii_queue_relink is called in interrupt context from mii_link_interrupt */
1811static void mii_queue_relink(uint mii_reg, struct net_device *dev)
1812{
1813 struct fec_enet_private *fep = netdev_priv(dev);
1814
1815 /*
1816 ** We cannot queue phy_task twice in the workqueue. It
1817 ** would cause an endless loop in the workqueue.
1818 ** Fortunately, if the last mii_relink entry has not yet been
1819 ** executed now, it will do the job for the current interrupt,
1820 ** which is just what we want.
1821 */
1822 if (fep->mii_phy_task_queued)
1823 return;
1824
1825 fep->mii_phy_task_queued = 1;
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001826 INIT_WORK(&fep->phy_task, mii_relink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 schedule_work(&fep->phy_task);
1828}
1829
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001830/* mii_queue_config is called in interrupt context from fec_enet_mii */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831static void mii_queue_config(uint mii_reg, struct net_device *dev)
1832{
1833 struct fec_enet_private *fep = netdev_priv(dev);
1834
1835 if (fep->mii_phy_task_queued)
1836 return;
1837
1838 fep->mii_phy_task_queued = 1;
Greg Ungerercb84d6e2007-07-30 16:29:09 +10001839 INIT_WORK(&fep->phy_task, mii_display_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 schedule_work(&fep->phy_task);
1841}
1842
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001843phy_cmd_t const phy_cmd_relink[] = {
1844 { mk_mii_read(MII_REG_CR), mii_queue_relink },
1845 { mk_mii_end, }
1846 };
1847phy_cmd_t const phy_cmd_config[] = {
1848 { mk_mii_read(MII_REG_CR), mii_queue_config },
1849 { mk_mii_end, }
1850 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852/* Read remainder of PHY ID.
1853*/
1854static void
1855mii_discover_phy3(uint mii_reg, struct net_device *dev)
1856{
1857 struct fec_enet_private *fep;
1858 int i;
1859
1860 fep = netdev_priv(dev);
1861 fep->phy_id |= (mii_reg & 0xffff);
1862 printk("fec: PHY @ 0x%x, ID 0x%08x", fep->phy_addr, fep->phy_id);
1863
1864 for(i = 0; phy_info[i]; i++) {
1865 if(phy_info[i]->id == (fep->phy_id >> 4))
1866 break;
1867 }
1868
1869 if (phy_info[i])
1870 printk(" -- %s\n", phy_info[i]->name);
1871 else
1872 printk(" -- unknown PHY!\n");
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 fep->phy = phy_info[i];
1875 fep->phy_id_done = 1;
1876}
1877
1878/* Scan all of the MII PHY addresses looking for someone to respond
1879 * with a valid ID. This usually happens quickly.
1880 */
1881static void
1882mii_discover_phy(uint mii_reg, struct net_device *dev)
1883{
1884 struct fec_enet_private *fep;
1885 volatile fec_t *fecp;
1886 uint phytype;
1887
1888 fep = netdev_priv(dev);
1889 fecp = fep->hwp;
1890
1891 if (fep->phy_addr < 32) {
1892 if ((phytype = (mii_reg & 0xffff)) != 0xffff && phytype != 0) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 /* Got first part of ID, now get remainder.
1895 */
1896 fep->phy_id = phytype << 16;
1897 mii_queue(dev, mk_mii_read(MII_REG_PHYIR2),
1898 mii_discover_phy3);
Philippe De Muyterf909b1e2007-10-23 14:37:54 +10001899 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 fep->phy_addr++;
1901 mii_queue(dev, mk_mii_read(MII_REG_PHYIR1),
1902 mii_discover_phy);
1903 }
1904 } else {
1905 printk("FEC: No PHY device found.\n");
1906 /* Disable external MII interface */
1907 fecp->fec_mii_speed = fep->phy_speed = 0;
1908 fec_disable_phy_intr();
1909 }
1910}
1911
1912/* This interrupt occurs when the PHY detects a link change.
1913*/
Sebastian Siewiorc1d96152008-05-01 14:04:02 +10001914#ifdef HAVE_mii_link_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001916mii_link_interrupt(int irq, void * dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
1918 struct net_device *dev = dev_id;
1919 struct fec_enet_private *fep = netdev_priv(dev);
1920
1921 fec_phy_ack_intr();
1922
1923#if 0
1924 disable_irq(fep->mii_irq); /* disable now, enable later */
1925#endif
1926
1927 mii_do_cmd(dev, fep->phy->ack_int);
1928 mii_do_cmd(dev, phy_cmd_relink); /* restart and display status */
1929
1930 return IRQ_HANDLED;
1931}
Sebastian Siewiorc1d96152008-05-01 14:04:02 +10001932#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934static int
1935fec_enet_open(struct net_device *dev)
1936{
1937 struct fec_enet_private *fep = netdev_priv(dev);
1938
1939 /* I should reset the ring buffers here, but I don't yet know
1940 * a simple way to do that.
1941 */
1942 fec_set_mac_address(dev);
1943
1944 fep->sequence_done = 0;
1945 fep->link = 0;
1946
1947 if (fep->phy) {
1948 mii_do_cmd(dev, fep->phy->ack_int);
1949 mii_do_cmd(dev, fep->phy->config);
1950 mii_do_cmd(dev, phy_cmd_config); /* display configuration */
1951
Matt Waddel6b265292006-06-27 13:10:56 +10001952 /* Poll until the PHY tells us its configuration
1953 * (not link state).
1954 * Request is initiated by mii_do_cmd above, but answer
1955 * comes by interrupt.
1956 * This should take about 25 usec per register at 2.5 MHz,
1957 * and we read approximately 5 registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 */
1959 while(!fep->sequence_done)
1960 schedule();
1961
1962 mii_do_cmd(dev, fep->phy->startup);
1963
1964 /* Set the initial link state to true. A lot of hardware
1965 * based on this device does not implement a PHY interrupt,
1966 * so we are never notified of link change.
1967 */
1968 fep->link = 1;
1969 } else {
1970 fep->link = 1; /* lets just try it and see */
1971 /* no phy, go full duplex, it's most likely a hub chip */
1972 fec_restart(dev, 1);
1973 }
1974
1975 netif_start_queue(dev);
1976 fep->opened = 1;
1977 return 0; /* Success */
1978}
1979
1980static int
1981fec_enet_close(struct net_device *dev)
1982{
1983 struct fec_enet_private *fep = netdev_priv(dev);
1984
1985 /* Don't know what to do yet.
1986 */
1987 fep->opened = 0;
1988 netif_stop_queue(dev);
1989 fec_stop(dev);
1990
1991 return 0;
1992}
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994/* Set or clear the multicast filter for this adaptor.
1995 * Skeleton taken from sunlance driver.
1996 * The CPM Ethernet implementation allows Multicast as well as individual
1997 * MAC address filtering. Some of the drivers check to make sure it is
1998 * a group multicast address, and discard those that are not. I guess I
1999 * will do the same for now, but just remove the test if you want
2000 * individual filtering as well (do the upper net layers want or support
2001 * this kind of feature?).
2002 */
2003
2004#define HASH_BITS 6 /* #bits in hash */
2005#define CRC32_POLY 0xEDB88320
2006
2007static void set_multicast_list(struct net_device *dev)
2008{
2009 struct fec_enet_private *fep;
2010 volatile fec_t *ep;
2011 struct dev_mc_list *dmi;
2012 unsigned int i, j, bit, data, crc;
2013 unsigned char hash;
2014
2015 fep = netdev_priv(dev);
2016 ep = fep->hwp;
2017
2018 if (dev->flags&IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 ep->fec_r_cntrl |= 0x0008;
2020 } else {
2021
2022 ep->fec_r_cntrl &= ~0x0008;
2023
2024 if (dev->flags & IFF_ALLMULTI) {
2025 /* Catch all multicast addresses, so set the
2026 * filter to all 1's.
2027 */
Greg Ungerercc462f72008-05-01 13:35:34 +10002028 ep->fec_grp_hash_table_high = 0xffffffff;
2029 ep->fec_grp_hash_table_low = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 } else {
2031 /* Clear filter and add the addresses in hash register.
2032 */
Greg Ungerercc462f72008-05-01 13:35:34 +10002033 ep->fec_grp_hash_table_high = 0;
2034 ep->fec_grp_hash_table_low = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 dmi = dev->mc_list;
2037
2038 for (j = 0; j < dev->mc_count; j++, dmi = dmi->next)
2039 {
2040 /* Only support group multicast for now.
2041 */
2042 if (!(dmi->dmi_addr[0] & 1))
2043 continue;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 /* calculate crc32 value of mac address
2046 */
2047 crc = 0xffffffff;
2048
2049 for (i = 0; i < dmi->dmi_addrlen; i++)
2050 {
2051 data = dmi->dmi_addr[i];
2052 for (bit = 0; bit < 8; bit++, data >>= 1)
2053 {
2054 crc = (crc >> 1) ^
2055 (((crc ^ data) & 1) ? CRC32_POLY : 0);
2056 }
2057 }
2058
2059 /* only upper 6 bits (HASH_BITS) are used
2060 which point to specific bit in he hash registers
2061 */
2062 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 if (hash > 31)
Greg Ungerercc462f72008-05-01 13:35:34 +10002065 ep->fec_grp_hash_table_high |= 1 << (hash - 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 else
Greg Ungerercc462f72008-05-01 13:35:34 +10002067 ep->fec_grp_hash_table_low |= 1 << hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
2069 }
2070 }
2071}
2072
2073/* Set a MAC change in hardware.
2074 */
2075static void
2076fec_set_mac_address(struct net_device *dev)
2077{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 volatile fec_t *fecp;
2079
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10002080 fecp = ((struct fec_enet_private *)netdev_priv(dev))->hwp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 /* Set station address. */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10002083 fecp->fec_addr_low = dev->dev_addr[3] | (dev->dev_addr[2] << 8) |
2084 (dev->dev_addr[1] << 16) | (dev->dev_addr[0] << 24);
2085 fecp->fec_addr_high = (dev->dev_addr[5] << 16) |
2086 (dev->dev_addr[4] << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088}
2089
2090/* Initialize the FEC Ethernet on 860T (or ColdFire 5272).
2091 */
2092 /*
2093 * XXX: We need to clean up on failure exits here.
2094 */
2095int __init fec_enet_init(struct net_device *dev)
2096{
2097 struct fec_enet_private *fep = netdev_priv(dev);
2098 unsigned long mem_addr;
2099 volatile cbd_t *bdp;
2100 cbd_t *cbd_base;
2101 volatile fec_t *fecp;
2102 int i, j;
2103 static int index = 0;
2104
2105 /* Only allow us to be probed once. */
2106 if (index >= FEC_MAX_PORTS)
2107 return -ENXIO;
2108
Greg Ungerer562d2f82005-11-07 14:09:50 +10002109 /* Allocate memory for buffer descriptors.
2110 */
Sascha Hauer4661e752009-01-28 23:03:07 +00002111 mem_addr = (unsigned long)dma_alloc_coherent(NULL, PAGE_SIZE,
2112 &fep->bd_dma, GFP_KERNEL);
Greg Ungerer562d2f82005-11-07 14:09:50 +10002113 if (mem_addr == 0) {
2114 printk("FEC: allocate descriptor memory failed?\n");
2115 return -ENOMEM;
2116 }
2117
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10002118 spin_lock_init(&fep->hw_lock);
2119 spin_lock_init(&fep->mii_lock);
2120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 /* Create an Ethernet device instance.
2122 */
2123 fecp = (volatile fec_t *) fec_hw[index];
2124
2125 fep->index = index;
2126 fep->hwp = fecp;
Greg Ungerercb84d6e2007-07-30 16:29:09 +10002127 fep->netdev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 /* Whack a reset. We should wait for this.
2130 */
2131 fecp->fec_ecntrl = 1;
2132 udelay(10);
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 /* Set the Ethernet address. If using multiple Enets on the 8xx,
2135 * this needs some work to get unique addresses.
2136 *
2137 * This is our default MAC address unless the user changes
2138 * it via eth_mac_addr (our dev->set_mac_addr handler).
2139 */
2140 fec_get_mac(dev);
2141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 cbd_base = (cbd_t *)mem_addr;
2143 /* XXX: missing check for allocation failure */
2144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 /* Set receive and transmit descriptor base.
2146 */
2147 fep->rx_bd_base = cbd_base;
2148 fep->tx_bd_base = cbd_base + RX_RING_SIZE;
2149
2150 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
2151 fep->cur_rx = fep->rx_bd_base;
2152
2153 fep->skb_cur = fep->skb_dirty = 0;
2154
2155 /* Initialize the receive buffer descriptors.
2156 */
2157 bdp = fep->rx_bd_base;
2158 for (i=0; i<FEC_ENET_RX_PAGES; i++) {
2159
2160 /* Allocate a page.
2161 */
2162 mem_addr = __get_free_page(GFP_KERNEL);
2163 /* XXX: missing check for allocation failure */
2164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 /* Initialize the BD for every fragment in the page.
2166 */
2167 for (j=0; j<FEC_ENET_RX_FRPPG; j++) {
2168 bdp->cbd_sc = BD_ENET_RX_EMPTY;
2169 bdp->cbd_bufaddr = __pa(mem_addr);
2170 mem_addr += FEC_ENET_RX_FRSIZE;
2171 bdp++;
2172 }
2173 }
2174
2175 /* Set the last buffer to wrap.
2176 */
2177 bdp--;
2178 bdp->cbd_sc |= BD_SC_WRAP;
2179
2180 /* ...and the same for transmmit.
2181 */
2182 bdp = fep->tx_bd_base;
2183 for (i=0, j=FEC_ENET_TX_FRPPG; i<TX_RING_SIZE; i++) {
2184 if (j >= FEC_ENET_TX_FRPPG) {
2185 mem_addr = __get_free_page(GFP_KERNEL);
2186 j = 1;
2187 } else {
2188 mem_addr += FEC_ENET_TX_FRSIZE;
2189 j++;
2190 }
2191 fep->tx_bounce[i] = (unsigned char *) mem_addr;
2192
2193 /* Initialize the BD for every fragment in the page.
2194 */
2195 bdp->cbd_sc = 0;
2196 bdp->cbd_bufaddr = 0;
2197 bdp++;
2198 }
2199
2200 /* Set the last buffer to wrap.
2201 */
2202 bdp--;
2203 bdp->cbd_sc |= BD_SC_WRAP;
2204
2205 /* Set receive and transmit descriptor base.
2206 */
Sascha Hauer4661e752009-01-28 23:03:07 +00002207 fecp->fec_r_des_start = fep->bd_dma;
2208 fecp->fec_x_des_start = (unsigned long)fep->bd_dma + sizeof(cbd_t)
2209 * RX_RING_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
2211 /* Install our interrupt handlers. This varies depending on
2212 * the architecture.
2213 */
2214 fec_request_intrs(dev);
2215
Greg Ungerercc462f72008-05-01 13:35:34 +10002216 fecp->fec_grp_hash_table_high = 0;
2217 fecp->fec_grp_hash_table_low = 0;
Greg Ungerer562d2f82005-11-07 14:09:50 +10002218 fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;
2219 fecp->fec_ecntrl = 2;
Matt Waddel6b265292006-06-27 13:10:56 +10002220 fecp->fec_r_des_active = 0;
Greg Ungerercc462f72008-05-01 13:35:34 +10002221#ifndef CONFIG_M5272
2222 fecp->fec_hash_table_high = 0;
2223 fecp->fec_hash_table_low = 0;
2224#endif
Greg Ungerer562d2f82005-11-07 14:09:50 +10002225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 dev->base_addr = (unsigned long)fecp;
2227
2228 /* The FEC Ethernet specific entries in the device structure. */
2229 dev->open = fec_enet_open;
2230 dev->hard_start_xmit = fec_enet_start_xmit;
2231 dev->tx_timeout = fec_timeout;
2232 dev->watchdog_timeo = TX_TIMEOUT;
2233 dev->stop = fec_enet_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 dev->set_multicast_list = set_multicast_list;
2235
2236 for (i=0; i<NMII-1; i++)
2237 mii_cmds[i].mii_next = &mii_cmds[i+1];
2238 mii_free = mii_cmds;
2239
2240 /* setup MII interface */
2241 fec_set_mii(dev, fep);
2242
Matt Waddel6b265292006-06-27 13:10:56 +10002243 /* Clear and enable interrupts */
2244 fecp->fec_ievent = 0xffc00000;
Greg Ungerer398ec922008-05-01 13:47:09 +10002245 fecp->fec_imask = (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII);
Matt Waddel6b265292006-06-27 13:10:56 +10002246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 /* Queue up command to detect the PHY and initialize the
2248 * remainder of the interface.
2249 */
2250 fep->phy_id_done = 0;
2251 fep->phy_addr = 0;
2252 mii_queue(dev, mk_mii_read(MII_REG_PHYIR1), mii_discover_phy);
2253
2254 index++;
2255 return 0;
2256}
2257
2258/* This function is called to start or restart the FEC during a link
2259 * change. This only happens when switching between half and full
2260 * duplex.
2261 */
2262static void
2263fec_restart(struct net_device *dev, int duplex)
2264{
2265 struct fec_enet_private *fep;
2266 volatile cbd_t *bdp;
2267 volatile fec_t *fecp;
2268 int i;
2269
2270 fep = netdev_priv(dev);
2271 fecp = fep->hwp;
2272
2273 /* Whack a reset. We should wait for this.
2274 */
2275 fecp->fec_ecntrl = 1;
2276 udelay(10);
2277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 /* Clear any outstanding interrupt.
2279 */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10002280 fecp->fec_ievent = 0xffc00000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
2282 /* Set station address.
2283 */
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10002284 fec_set_mac_address(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286 /* Reset all multicast.
2287 */
Greg Ungerercc462f72008-05-01 13:35:34 +10002288 fecp->fec_grp_hash_table_high = 0;
2289 fecp->fec_grp_hash_table_low = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291 /* Set maximum receive buffer size.
2292 */
2293 fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;
2294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 /* Set receive and transmit descriptor base.
2296 */
Sascha Hauer4661e752009-01-28 23:03:07 +00002297 fecp->fec_r_des_start = fep->bd_dma;
2298 fecp->fec_x_des_start = (unsigned long)fep->bd_dma + sizeof(cbd_t)
2299 * RX_RING_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
2301 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
2302 fep->cur_rx = fep->rx_bd_base;
2303
2304 /* Reset SKB transmit buffers.
2305 */
2306 fep->skb_cur = fep->skb_dirty = 0;
2307 for (i=0; i<=TX_RING_MOD_MASK; i++) {
2308 if (fep->tx_skbuff[i] != NULL) {
2309 dev_kfree_skb_any(fep->tx_skbuff[i]);
2310 fep->tx_skbuff[i] = NULL;
2311 }
2312 }
2313
2314 /* Initialize the receive buffer descriptors.
2315 */
2316 bdp = fep->rx_bd_base;
2317 for (i=0; i<RX_RING_SIZE; i++) {
2318
2319 /* Initialize the BD for every fragment in the page.
2320 */
2321 bdp->cbd_sc = BD_ENET_RX_EMPTY;
2322 bdp++;
2323 }
2324
2325 /* Set the last buffer to wrap.
2326 */
2327 bdp--;
2328 bdp->cbd_sc |= BD_SC_WRAP;
2329
2330 /* ...and the same for transmmit.
2331 */
2332 bdp = fep->tx_bd_base;
2333 for (i=0; i<TX_RING_SIZE; i++) {
2334
2335 /* Initialize the BD for every fragment in the page.
2336 */
2337 bdp->cbd_sc = 0;
2338 bdp->cbd_bufaddr = 0;
2339 bdp++;
2340 }
2341
2342 /* Set the last buffer to wrap.
2343 */
2344 bdp--;
2345 bdp->cbd_sc |= BD_SC_WRAP;
2346
2347 /* Enable MII mode.
2348 */
2349 if (duplex) {
2350 fecp->fec_r_cntrl = OPT_FRAME_SIZE | 0x04;/* MII enable */
2351 fecp->fec_x_cntrl = 0x04; /* FD enable */
Philippe De Muyterf909b1e2007-10-23 14:37:54 +10002352 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 /* MII enable|No Rcv on Xmit */
2354 fecp->fec_r_cntrl = OPT_FRAME_SIZE | 0x06;
2355 fecp->fec_x_cntrl = 0x00;
2356 }
2357 fep->full_duplex = duplex;
2358
2359 /* Set MII speed.
2360 */
2361 fecp->fec_mii_speed = fep->phy_speed;
2362
2363 /* And last, enable the transmit and receive processing.
2364 */
2365 fecp->fec_ecntrl = 2;
Matt Waddel6b265292006-06-27 13:10:56 +10002366 fecp->fec_r_des_active = 0;
2367
2368 /* Enable interrupts we wish to service.
2369 */
Greg Ungerer398ec922008-05-01 13:47:09 +10002370 fecp->fec_imask = (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371}
2372
2373static void
2374fec_stop(struct net_device *dev)
2375{
2376 volatile fec_t *fecp;
2377 struct fec_enet_private *fep;
2378
2379 fep = netdev_priv(dev);
2380 fecp = fep->hwp;
2381
Philippe De Muyter677177c2006-06-27 13:05:33 +10002382 /*
2383 ** We cannot expect a graceful transmit stop without link !!!
2384 */
2385 if (fep->link)
2386 {
2387 fecp->fec_x_cntrl = 0x01; /* Graceful transmit stop */
2388 udelay(10);
2389 if (!(fecp->fec_ievent & FEC_ENET_GRA))
2390 printk("fec_stop : Graceful transmit stop did not complete !\n");
2391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393 /* Whack a reset. We should wait for this.
2394 */
2395 fecp->fec_ecntrl = 1;
2396 udelay(10);
2397
2398 /* Clear outstanding MII command interrupts.
2399 */
2400 fecp->fec_ievent = FEC_ENET_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
2402 fecp->fec_imask = FEC_ENET_MII;
2403 fecp->fec_mii_speed = fep->phy_speed;
2404}
2405
2406static int __init fec_enet_module_init(void)
2407{
2408 struct net_device *dev;
Sebastian Siewiorc1d96152008-05-01 14:04:02 +10002409 int i, err;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10002410
2411 printk("FEC ENET Version 0.2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
2413 for (i = 0; (i < FEC_MAX_PORTS); i++) {
2414 dev = alloc_etherdev(sizeof(struct fec_enet_private));
2415 if (!dev)
2416 return -ENOMEM;
2417 err = fec_enet_init(dev);
2418 if (err) {
2419 free_netdev(dev);
2420 continue;
2421 }
2422 if (register_netdev(dev) != 0) {
2423 /* XXX: missing cleanup here */
2424 free_netdev(dev);
2425 return -EIO;
2426 }
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10002427
Johannes Berge1749612008-10-27 15:59:26 -07002428 printk("%s: ethernet %pM\n", dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 }
2430 return 0;
2431}
2432
2433module_init(fec_enet_module_init);
2434
2435MODULE_LICENSE("GPL");