Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / net / fec_8xx / fec_main.c
blob7e433809713968b4f719ff280df017f436e073c7
1 /*
2 * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
4 * Copyright (c) 2003 Intracom S.A.
5 * by Pantelis Antoniou <panto@intracom.gr>
7 * Heavily based on original FEC driver by Dan Malek <dan@embeddededge.com>
8 * and modifications by Joakim Tjernlund <joakim.tjernlund@lumentis.se>
10 * Released under the GPL
13 #include <linux/config.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/sched.h>
18 #include <linux/string.h>
19 #include <linux/ptrace.h>
20 #include <linux/errno.h>
21 #include <linux/ioport.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/pci.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/spinlock.h>
31 #include <linux/mii.h>
32 #include <linux/ethtool.h>
33 #include <linux/bitops.h>
35 #include <asm/8xx_immap.h>
36 #include <asm/pgtable.h>
37 #include <asm/mpc8xx.h>
38 #include <asm/irq.h>
39 #include <asm/uaccess.h>
40 #include <asm/commproc.h>
41 #include <asm/dma-mapping.h>
43 #include "fec_8xx.h"
45 /*************************************************/
47 #define FEC_MAX_MULTICAST_ADDRS 64
49 /*************************************************/
51 static char version[] __devinitdata =
52 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")" "\n";
54 MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
55 MODULE_DESCRIPTION("Motorola 8xx FEC ethernet driver");
56 MODULE_LICENSE("GPL");
58 int fec_8xx_debug = -1; /* -1 == use FEC_8XX_DEF_MSG_ENABLE as value */
59 module_param(fec_8xx_debug, int, 0);
60 MODULE_PARM_DESC(fec_8xx_debug,
61 "FEC 8xx bitmapped debugging message enable value");
64 /*************************************************/
67 * Delay to wait for FEC reset command to complete (in us)
69 #define FEC_RESET_DELAY 50
71 /*****************************************************************************************/
73 static void fec_whack_reset(fec_t * fecp)
75 int i;
78 * Whack a reset. We should wait for this.
80 FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET);
81 for (i = 0;
82 (FR(fecp, ecntrl) & FEC_ECNTRL_RESET) != 0 && i < FEC_RESET_DELAY;
83 i++)
84 udelay(1);
86 if (i == FEC_RESET_DELAY)
87 printk(KERN_WARNING "FEC Reset timeout!\n");
91 /****************************************************************************/
94 * Transmitter timeout.
96 #define TX_TIMEOUT (2*HZ)
98 /****************************************************************************/
101 * Returns the CRC needed when filling in the hash table for
102 * multicast group filtering
103 * pAddr must point to a MAC address (6 bytes)
105 static __u32 fec_mulicast_calc_crc(char *pAddr)
107 u8 byte;
108 int byte_count;
109 int bit_count;
110 __u32 crc = 0xffffffff;
111 u8 msb;
113 for (byte_count = 0; byte_count < 6; byte_count++) {
114 byte = pAddr[byte_count];
115 for (bit_count = 0; bit_count < 8; bit_count++) {
116 msb = crc >> 31;
117 crc <<= 1;
118 if (msb ^ (byte & 0x1)) {
119 crc ^= FEC_CRC_POLY;
121 byte >>= 1;
124 return (crc);
128 * Set or clear the multicast filter for this adaptor.
129 * Skeleton taken from sunlance driver.
130 * The CPM Ethernet implementation allows Multicast as well as individual
131 * MAC address filtering. Some of the drivers check to make sure it is
132 * a group multicast address, and discard those that are not. I guess I
133 * will do the same for now, but just remove the test if you want
134 * individual filtering as well (do the upper net layers want or support
135 * this kind of feature?).
137 static void fec_set_multicast_list(struct net_device *dev)
139 struct fec_enet_private *fep = netdev_priv(dev);
140 fec_t *fecp = fep->fecp;
141 struct dev_mc_list *pmc;
142 __u32 crc;
143 int temp;
144 __u32 csrVal;
145 int hash_index;
146 __u32 hthi, htlo;
147 unsigned long flags;
150 if ((dev->flags & IFF_PROMISC) != 0) {
152 spin_lock_irqsave(&fep->lock, flags);
153 FS(fecp, r_cntrl, FEC_RCNTRL_PROM);
154 spin_unlock_irqrestore(&fep->lock, flags);
157 * Log any net taps.
159 printk(KERN_WARNING DRV_MODULE_NAME
160 ": %s: Promiscuous mode enabled.\n", dev->name);
161 return;
165 if ((dev->flags & IFF_ALLMULTI) != 0 ||
166 dev->mc_count > FEC_MAX_MULTICAST_ADDRS) {
168 * Catch all multicast addresses, set the filter to all 1's.
170 hthi = 0xffffffffU;
171 htlo = 0xffffffffU;
172 } else {
173 hthi = 0;
174 htlo = 0;
177 * Now populate the hash table
179 for (pmc = dev->mc_list; pmc != NULL; pmc = pmc->next) {
180 crc = fec_mulicast_calc_crc(pmc->dmi_addr);
181 temp = (crc & 0x3f) >> 1;
182 hash_index = ((temp & 0x01) << 4) |
183 ((temp & 0x02) << 2) |
184 ((temp & 0x04)) |
185 ((temp & 0x08) >> 2) |
186 ((temp & 0x10) >> 4);
187 csrVal = (1 << hash_index);
188 if (crc & 1)
189 hthi |= csrVal;
190 else
191 htlo |= csrVal;
195 spin_lock_irqsave(&fep->lock, flags);
196 FC(fecp, r_cntrl, FEC_RCNTRL_PROM);
197 FW(fecp, hash_table_high, hthi);
198 FW(fecp, hash_table_low, htlo);
199 spin_unlock_irqrestore(&fep->lock, flags);
202 static int fec_set_mac_address(struct net_device *dev, void *addr)
204 struct sockaddr *mac = addr;
205 struct fec_enet_private *fep = netdev_priv(dev);
206 struct fec *fecp = fep->fecp;
207 int i;
208 __u32 addrhi, addrlo;
209 unsigned long flags;
211 /* Get pointer to SCC area in parameter RAM. */
212 for (i = 0; i < 6; i++)
213 dev->dev_addr[i] = mac->sa_data[i];
216 * Set station address.
218 addrhi = ((__u32) dev->dev_addr[0] << 24) |
219 ((__u32) dev->dev_addr[1] << 16) |
220 ((__u32) dev->dev_addr[2] << 8) |
221 (__u32) dev->dev_addr[3];
222 addrlo = ((__u32) dev->dev_addr[4] << 24) |
223 ((__u32) dev->dev_addr[5] << 16);
225 spin_lock_irqsave(&fep->lock, flags);
226 FW(fecp, addr_low, addrhi);
227 FW(fecp, addr_high, addrlo);
228 spin_unlock_irqrestore(&fep->lock, flags);
230 return 0;
234 * This function is called to start or restart the FEC during a link
235 * change. This only happens when switching between half and full
236 * duplex.
238 void fec_restart(struct net_device *dev, int duplex, int speed)
240 #ifdef CONFIG_DUET
241 immap_t *immap = (immap_t *) IMAP_ADDR;
242 __u32 cptr;
243 #endif
244 struct fec_enet_private *fep = netdev_priv(dev);
245 struct fec *fecp = fep->fecp;
246 const struct fec_platform_info *fpi = fep->fpi;
247 cbd_t *bdp;
248 struct sk_buff *skb;
249 int i;
250 __u32 addrhi, addrlo;
252 fec_whack_reset(fep->fecp);
255 * Set station address.
257 addrhi = ((__u32) dev->dev_addr[0] << 24) |
258 ((__u32) dev->dev_addr[1] << 16) |
259 ((__u32) dev->dev_addr[2] << 8) |
260 (__u32) dev->dev_addr[3];
261 addrlo = ((__u32) dev->dev_addr[4] << 24) |
262 ((__u32) dev->dev_addr[5] << 16);
263 FW(fecp, addr_low, addrhi);
264 FW(fecp, addr_high, addrlo);
267 * Reset all multicast.
269 FW(fecp, hash_table_high, 0);
270 FW(fecp, hash_table_low, 0);
273 * Set maximum receive buffer size.
275 FW(fecp, r_buff_size, PKT_MAXBLR_SIZE);
276 FW(fecp, r_hash, PKT_MAXBUF_SIZE);
279 * Set receive and transmit descriptor base.
281 FW(fecp, r_des_start, iopa((__u32) (fep->rx_bd_base)));
282 FW(fecp, x_des_start, iopa((__u32) (fep->tx_bd_base)));
284 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
285 fep->tx_free = fep->tx_ring;
286 fep->cur_rx = fep->rx_bd_base;
289 * Reset SKB receive buffers
291 for (i = 0; i < fep->rx_ring; i++) {
292 if ((skb = fep->rx_skbuff[i]) == NULL)
293 continue;
294 fep->rx_skbuff[i] = NULL;
295 dev_kfree_skb(skb);
299 * Initialize the receive buffer descriptors.
301 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
302 skb = dev_alloc_skb(ENET_RX_FRSIZE);
303 if (skb == NULL) {
304 printk(KERN_WARNING DRV_MODULE_NAME
305 ": %s Memory squeeze, unable to allocate skb\n",
306 dev->name);
307 fep->stats.rx_dropped++;
308 break;
310 fep->rx_skbuff[i] = skb;
311 skb->dev = dev;
312 CBDW_BUFADDR(bdp, dma_map_single(NULL, skb->data,
313 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
314 DMA_FROM_DEVICE));
315 CBDW_DATLEN(bdp, 0); /* zero */
316 CBDW_SC(bdp, BD_ENET_RX_EMPTY |
317 ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
320 * if we failed, fillup remainder
322 for (; i < fep->rx_ring; i++, bdp++) {
323 fep->rx_skbuff[i] = NULL;
324 CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
328 * Reset SKB transmit buffers.
330 for (i = 0; i < fep->tx_ring; i++) {
331 if ((skb = fep->tx_skbuff[i]) == NULL)
332 continue;
333 fep->tx_skbuff[i] = NULL;
334 dev_kfree_skb(skb);
338 * ...and the same for transmit.
340 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
341 fep->tx_skbuff[i] = NULL;
342 CBDW_BUFADDR(bdp, virt_to_bus(NULL));
343 CBDW_DATLEN(bdp, 0);
344 CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
348 * Enable big endian and don't care about SDMA FC.
350 FW(fecp, fun_code, 0x78000000);
353 * Set MII speed.
355 FW(fecp, mii_speed, fep->fec_phy_speed);
358 * Clear any outstanding interrupt.
360 FW(fecp, ievent, 0xffc0);
361 FW(fecp, ivec, (fpi->fec_irq / 2) << 29);
364 * adjust to speed (only for DUET & RMII)
366 #ifdef CONFIG_DUET
367 cptr = in_be32(&immap->im_cpm.cp_cptr);
368 switch (fpi->fec_no) {
369 case 0:
371 * check if in RMII mode
373 if ((cptr & 0x100) == 0)
374 break;
376 if (speed == 10)
377 cptr |= 0x0000010;
378 else if (speed == 100)
379 cptr &= ~0x0000010;
380 break;
381 case 1:
383 * check if in RMII mode
385 if ((cptr & 0x80) == 0)
386 break;
388 if (speed == 10)
389 cptr |= 0x0000008;
390 else if (speed == 100)
391 cptr &= ~0x0000008;
392 break;
393 default:
394 break;
396 out_be32(&immap->im_cpm.cp_cptr, cptr);
397 #endif
399 FW(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
401 * adjust to duplex mode
403 if (duplex) {
404 FC(fecp, r_cntrl, FEC_RCNTRL_DRT);
405 FS(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD enable */
406 } else {
407 FS(fecp, r_cntrl, FEC_RCNTRL_DRT);
408 FC(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD disable */
412 * Enable interrupts we wish to service.
414 FW(fecp, imask, FEC_ENET_TXF | FEC_ENET_TXB |
415 FEC_ENET_RXF | FEC_ENET_RXB);
418 * And last, enable the transmit and receive processing.
420 FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
421 FW(fecp, r_des_active, 0x01000000);
424 void fec_stop(struct net_device *dev)
426 struct fec_enet_private *fep = netdev_priv(dev);
427 fec_t *fecp = fep->fecp;
428 struct sk_buff *skb;
429 int i;
431 if ((FR(fecp, ecntrl) & FEC_ECNTRL_ETHER_EN) == 0)
432 return; /* already down */
434 FW(fecp, x_cntrl, 0x01); /* Graceful transmit stop */
435 for (i = 0; ((FR(fecp, ievent) & 0x10000000) == 0) &&
436 i < FEC_RESET_DELAY; i++)
437 udelay(1);
439 if (i == FEC_RESET_DELAY)
440 printk(KERN_WARNING DRV_MODULE_NAME
441 ": %s FEC timeout on graceful transmit stop\n",
442 dev->name);
444 * Disable FEC. Let only MII interrupts.
446 FW(fecp, imask, 0);
447 FW(fecp, ecntrl, ~FEC_ECNTRL_ETHER_EN);
450 * Reset SKB transmit buffers.
452 for (i = 0; i < fep->tx_ring; i++) {
453 if ((skb = fep->tx_skbuff[i]) == NULL)
454 continue;
455 fep->tx_skbuff[i] = NULL;
456 dev_kfree_skb(skb);
460 * Reset SKB receive buffers
462 for (i = 0; i < fep->rx_ring; i++) {
463 if ((skb = fep->rx_skbuff[i]) == NULL)
464 continue;
465 fep->rx_skbuff[i] = NULL;
466 dev_kfree_skb(skb);
470 /* common receive function */
471 static int fec_enet_rx_common(struct net_device *dev, int *budget)
473 struct fec_enet_private *fep = netdev_priv(dev);
474 fec_t *fecp = fep->fecp;
475 const struct fec_platform_info *fpi = fep->fpi;
476 cbd_t *bdp;
477 struct sk_buff *skb, *skbn, *skbt;
478 int received = 0;
479 __u16 pkt_len, sc;
480 int curidx;
481 int rx_work_limit;
483 if (fpi->use_napi) {
484 rx_work_limit = min(dev->quota, *budget);
486 if (!netif_running(dev))
487 return 0;
491 * First, grab all of the stats for the incoming packet.
492 * These get messed up if we get called due to a busy condition.
494 bdp = fep->cur_rx;
496 /* clear RX status bits for napi*/
497 if (fpi->use_napi)
498 FW(fecp, ievent, FEC_ENET_RXF | FEC_ENET_RXB);
500 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
502 curidx = bdp - fep->rx_bd_base;
505 * Since we have allocated space to hold a complete frame,
506 * the last indicator should be set.
508 if ((sc & BD_ENET_RX_LAST) == 0)
509 printk(KERN_WARNING DRV_MODULE_NAME
510 ": %s rcv is not +last\n",
511 dev->name);
514 * Check for errors.
516 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
517 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
518 fep->stats.rx_errors++;
519 /* Frame too long or too short. */
520 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
521 fep->stats.rx_length_errors++;
522 /* Frame alignment */
523 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
524 fep->stats.rx_frame_errors++;
525 /* CRC Error */
526 if (sc & BD_ENET_RX_CR)
527 fep->stats.rx_crc_errors++;
528 /* FIFO overrun */
529 if (sc & BD_ENET_RX_OV)
530 fep->stats.rx_crc_errors++;
532 skbn = fep->rx_skbuff[curidx];
533 BUG_ON(skbn == NULL);
535 } else {
537 /* napi, got packet but no quota */
538 if (fpi->use_napi && --rx_work_limit < 0)
539 break;
541 skb = fep->rx_skbuff[curidx];
542 BUG_ON(skb == NULL);
545 * Process the incoming frame.
547 fep->stats.rx_packets++;
548 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
549 fep->stats.rx_bytes += pkt_len + 4;
551 if (pkt_len <= fpi->rx_copybreak) {
552 /* +2 to make IP header L1 cache aligned */
553 skbn = dev_alloc_skb(pkt_len + 2);
554 if (skbn != NULL) {
555 skb_reserve(skbn, 2); /* align IP header */
556 memcpy(skbn->data, skb->data, pkt_len);
557 /* swap */
558 skbt = skb;
559 skb = skbn;
560 skbn = skbt;
562 } else
563 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
565 if (skbn != NULL) {
566 skb->dev = dev;
567 skb_put(skb, pkt_len); /* Make room */
568 skb->protocol = eth_type_trans(skb, dev);
569 received++;
570 if (!fpi->use_napi)
571 netif_rx(skb);
572 else
573 netif_receive_skb(skb);
574 } else {
575 printk(KERN_WARNING DRV_MODULE_NAME
576 ": %s Memory squeeze, dropping packet.\n",
577 dev->name);
578 fep->stats.rx_dropped++;
579 skbn = skb;
583 fep->rx_skbuff[curidx] = skbn;
584 CBDW_BUFADDR(bdp, dma_map_single(NULL, skbn->data,
585 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
586 DMA_FROM_DEVICE));
587 CBDW_DATLEN(bdp, 0);
588 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
591 * Update BD pointer to next entry.
593 if ((sc & BD_ENET_RX_WRAP) == 0)
594 bdp++;
595 else
596 bdp = fep->rx_bd_base;
599 * Doing this here will keep the FEC running while we process
600 * incoming frames. On a heavily loaded network, we should be
601 * able to keep up at the expense of system resources.
603 FW(fecp, r_des_active, 0x01000000);
606 fep->cur_rx = bdp;
608 if (fpi->use_napi) {
609 dev->quota -= received;
610 *budget -= received;
612 if (rx_work_limit < 0)
613 return 1; /* not done */
615 /* done */
616 netif_rx_complete(dev);
618 /* enable RX interrupt bits */
619 FS(fecp, imask, FEC_ENET_RXF | FEC_ENET_RXB);
622 return 0;
625 static void fec_enet_tx(struct net_device *dev)
627 struct fec_enet_private *fep = netdev_priv(dev);
628 cbd_t *bdp;
629 struct sk_buff *skb;
630 int dirtyidx, do_wake;
631 __u16 sc;
633 spin_lock(&fep->lock);
634 bdp = fep->dirty_tx;
636 do_wake = 0;
637 while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
639 dirtyidx = bdp - fep->tx_bd_base;
641 if (fep->tx_free == fep->tx_ring)
642 break;
644 skb = fep->tx_skbuff[dirtyidx];
647 * Check for errors.
649 if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
650 BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
651 fep->stats.tx_errors++;
652 if (sc & BD_ENET_TX_HB) /* No heartbeat */
653 fep->stats.tx_heartbeat_errors++;
654 if (sc & BD_ENET_TX_LC) /* Late collision */
655 fep->stats.tx_window_errors++;
656 if (sc & BD_ENET_TX_RL) /* Retrans limit */
657 fep->stats.tx_aborted_errors++;
658 if (sc & BD_ENET_TX_UN) /* Underrun */
659 fep->stats.tx_fifo_errors++;
660 if (sc & BD_ENET_TX_CSL) /* Carrier lost */
661 fep->stats.tx_carrier_errors++;
662 } else
663 fep->stats.tx_packets++;
665 if (sc & BD_ENET_TX_READY)
666 printk(KERN_WARNING DRV_MODULE_NAME
667 ": %s HEY! Enet xmit interrupt and TX_READY.\n",
668 dev->name);
671 * Deferred means some collisions occurred during transmit,
672 * but we eventually sent the packet OK.
674 if (sc & BD_ENET_TX_DEF)
675 fep->stats.collisions++;
678 * Free the sk buffer associated with this last transmit.
680 dev_kfree_skb_irq(skb);
681 fep->tx_skbuff[dirtyidx] = NULL;
684 * Update pointer to next buffer descriptor to be transmitted.
686 if ((sc & BD_ENET_TX_WRAP) == 0)
687 bdp++;
688 else
689 bdp = fep->tx_bd_base;
692 * Since we have freed up a buffer, the ring is no longer
693 * full.
695 if (!fep->tx_free++)
696 do_wake = 1;
699 fep->dirty_tx = bdp;
701 spin_unlock(&fep->lock);
703 if (do_wake && netif_queue_stopped(dev))
704 netif_wake_queue(dev);
708 * The interrupt handler.
709 * This is called from the MPC core interrupt.
711 static irqreturn_t
712 fec_enet_interrupt(int irq, void *dev_id, struct pt_regs *regs)
714 struct net_device *dev = dev_id;
715 struct fec_enet_private *fep;
716 const struct fec_platform_info *fpi;
717 fec_t *fecp;
718 __u32 int_events;
719 __u32 int_events_napi;
721 if (unlikely(dev == NULL))
722 return IRQ_NONE;
724 fep = netdev_priv(dev);
725 fecp = fep->fecp;
726 fpi = fep->fpi;
729 * Get the interrupt events that caused us to be here.
731 while ((int_events = FR(fecp, ievent) & FR(fecp, imask)) != 0) {
733 if (!fpi->use_napi)
734 FW(fecp, ievent, int_events);
735 else {
736 int_events_napi = int_events & ~(FEC_ENET_RXF | FEC_ENET_RXB);
737 FW(fecp, ievent, int_events_napi);
740 if ((int_events & (FEC_ENET_HBERR | FEC_ENET_BABR |
741 FEC_ENET_BABT | FEC_ENET_EBERR)) != 0)
742 printk(KERN_WARNING DRV_MODULE_NAME
743 ": %s FEC ERROR(s) 0x%x\n",
744 dev->name, int_events);
746 if ((int_events & FEC_ENET_RXF) != 0) {
747 if (!fpi->use_napi)
748 fec_enet_rx_common(dev, NULL);
749 else {
750 if (netif_rx_schedule_prep(dev)) {
751 /* disable rx interrupts */
752 FC(fecp, imask, FEC_ENET_RXF | FEC_ENET_RXB);
753 __netif_rx_schedule(dev);
754 } else {
755 printk(KERN_ERR DRV_MODULE_NAME
756 ": %s driver bug! interrupt while in poll!\n",
757 dev->name);
758 FC(fecp, imask, FEC_ENET_RXF | FEC_ENET_RXB);
763 if ((int_events & FEC_ENET_TXF) != 0)
764 fec_enet_tx(dev);
767 return IRQ_HANDLED;
770 /* This interrupt occurs when the PHY detects a link change. */
771 static irqreturn_t
772 fec_mii_link_interrupt(int irq, void *dev_id, struct pt_regs *regs)
774 struct net_device *dev = dev_id;
775 struct fec_enet_private *fep;
776 const struct fec_platform_info *fpi;
778 if (unlikely(dev == NULL))
779 return IRQ_NONE;
781 fep = netdev_priv(dev);
782 fpi = fep->fpi;
784 if (!fpi->use_mdio)
785 return IRQ_NONE;
788 * Acknowledge the interrupt if possible. If we have not
789 * found the PHY yet we can't process or acknowledge the
790 * interrupt now. Instead we ignore this interrupt for now,
791 * which we can do since it is edge triggered. It will be
792 * acknowledged later by fec_enet_open().
794 if (!fep->phy)
795 return IRQ_NONE;
797 fec_mii_ack_int(dev);
798 fec_mii_link_status_change_check(dev, 0);
800 return IRQ_HANDLED;
804 /**********************************************************************************/
806 static int fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
808 struct fec_enet_private *fep = netdev_priv(dev);
809 fec_t *fecp = fep->fecp;
810 cbd_t *bdp;
811 int curidx;
812 unsigned long flags;
814 spin_lock_irqsave(&fep->tx_lock, flags);
817 * Fill in a Tx ring entry
819 bdp = fep->cur_tx;
821 if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
822 netif_stop_queue(dev);
823 spin_unlock_irqrestore(&fep->tx_lock, flags);
826 * Ooops. All transmit buffers are full. Bail out.
827 * This should not happen, since the tx queue should be stopped.
829 printk(KERN_WARNING DRV_MODULE_NAME
830 ": %s tx queue full!.\n", dev->name);
831 return 1;
834 curidx = bdp - fep->tx_bd_base;
836 * Clear all of the status flags.
838 CBDC_SC(bdp, BD_ENET_TX_STATS);
841 * Save skb pointer.
843 fep->tx_skbuff[curidx] = skb;
845 fep->stats.tx_bytes += skb->len;
848 * Push the data cache so the CPM does not get stale memory data.
850 CBDW_BUFADDR(bdp, dma_map_single(NULL, skb->data,
851 skb->len, DMA_TO_DEVICE));
852 CBDW_DATLEN(bdp, skb->len);
854 dev->trans_start = jiffies;
857 * If this was the last BD in the ring, start at the beginning again.
859 if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
860 fep->cur_tx++;
861 else
862 fep->cur_tx = fep->tx_bd_base;
864 if (!--fep->tx_free)
865 netif_stop_queue(dev);
868 * Trigger transmission start
870 CBDS_SC(bdp, BD_ENET_TX_READY | BD_ENET_TX_INTR |
871 BD_ENET_TX_LAST | BD_ENET_TX_TC);
872 FW(fecp, x_des_active, 0x01000000);
874 spin_unlock_irqrestore(&fep->tx_lock, flags);
876 return 0;
879 static void fec_timeout(struct net_device *dev)
881 struct fec_enet_private *fep = netdev_priv(dev);
883 fep->stats.tx_errors++;
885 if (fep->tx_free)
886 netif_wake_queue(dev);
888 /* check link status again */
889 fec_mii_link_status_change_check(dev, 0);
892 static int fec_enet_open(struct net_device *dev)
894 struct fec_enet_private *fep = netdev_priv(dev);
895 const struct fec_platform_info *fpi = fep->fpi;
896 unsigned long flags;
898 /* Install our interrupt handler. */
899 if (request_irq(fpi->fec_irq, fec_enet_interrupt, 0, "fec", dev) != 0) {
900 printk(KERN_ERR DRV_MODULE_NAME
901 ": %s Could not allocate FEC IRQ!", dev->name);
902 return -EINVAL;
905 /* Install our phy interrupt handler */
906 if (fpi->phy_irq != -1 &&
907 request_irq(fpi->phy_irq, fec_mii_link_interrupt, 0, "fec-phy",
908 dev) != 0) {
909 printk(KERN_ERR DRV_MODULE_NAME
910 ": %s Could not allocate PHY IRQ!", dev->name);
911 free_irq(fpi->fec_irq, dev);
912 return -EINVAL;
915 if (fpi->use_mdio) {
916 fec_mii_startup(dev);
917 netif_carrier_off(dev);
918 fec_mii_link_status_change_check(dev, 1);
919 } else {
920 spin_lock_irqsave(&fep->lock, flags);
921 fec_restart(dev, 1, 100); /* XXX this sucks */
922 spin_unlock_irqrestore(&fep->lock, flags);
924 netif_carrier_on(dev);
925 netif_start_queue(dev);
927 return 0;
930 static int fec_enet_close(struct net_device *dev)
932 struct fec_enet_private *fep = netdev_priv(dev);
933 const struct fec_platform_info *fpi = fep->fpi;
934 unsigned long flags;
936 netif_stop_queue(dev);
937 netif_carrier_off(dev);
939 if (fpi->use_mdio)
940 fec_mii_shutdown(dev);
942 spin_lock_irqsave(&fep->lock, flags);
943 fec_stop(dev);
944 spin_unlock_irqrestore(&fep->lock, flags);
946 /* release any irqs */
947 if (fpi->phy_irq != -1)
948 free_irq(fpi->phy_irq, dev);
949 free_irq(fpi->fec_irq, dev);
951 return 0;
954 static struct net_device_stats *fec_enet_get_stats(struct net_device *dev)
956 struct fec_enet_private *fep = netdev_priv(dev);
957 return &fep->stats;
960 static int fec_enet_poll(struct net_device *dev, int *budget)
962 return fec_enet_rx_common(dev, budget);
965 /*************************************************************************/
967 static void fec_get_drvinfo(struct net_device *dev,
968 struct ethtool_drvinfo *info)
970 strcpy(info->driver, DRV_MODULE_NAME);
971 strcpy(info->version, DRV_MODULE_VERSION);
974 static int fec_get_regs_len(struct net_device *dev)
976 return sizeof(fec_t);
979 static void fec_get_regs(struct net_device *dev, struct ethtool_regs *regs,
980 void *p)
982 struct fec_enet_private *fep = netdev_priv(dev);
983 unsigned long flags;
985 if (regs->len < sizeof(fec_t))
986 return;
988 regs->version = 0;
989 spin_lock_irqsave(&fep->lock, flags);
990 memcpy_fromio(p, fep->fecp, sizeof(fec_t));
991 spin_unlock_irqrestore(&fep->lock, flags);
994 static int fec_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
996 struct fec_enet_private *fep = netdev_priv(dev);
997 unsigned long flags;
998 int rc;
1000 spin_lock_irqsave(&fep->lock, flags);
1001 rc = mii_ethtool_gset(&fep->mii_if, cmd);
1002 spin_unlock_irqrestore(&fep->lock, flags);
1004 return rc;
1007 static int fec_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1009 struct fec_enet_private *fep = netdev_priv(dev);
1010 unsigned long flags;
1011 int rc;
1013 spin_lock_irqsave(&fep->lock, flags);
1014 rc = mii_ethtool_sset(&fep->mii_if, cmd);
1015 spin_unlock_irqrestore(&fep->lock, flags);
1017 return rc;
1020 static int fec_nway_reset(struct net_device *dev)
1022 struct fec_enet_private *fep = netdev_priv(dev);
1023 return mii_nway_restart(&fep->mii_if);
1026 static __u32 fec_get_msglevel(struct net_device *dev)
1028 struct fec_enet_private *fep = netdev_priv(dev);
1029 return fep->msg_enable;
1032 static void fec_set_msglevel(struct net_device *dev, __u32 value)
1034 struct fec_enet_private *fep = netdev_priv(dev);
1035 fep->msg_enable = value;
1038 static struct ethtool_ops fec_ethtool_ops = {
1039 .get_drvinfo = fec_get_drvinfo,
1040 .get_regs_len = fec_get_regs_len,
1041 .get_settings = fec_get_settings,
1042 .set_settings = fec_set_settings,
1043 .nway_reset = fec_nway_reset,
1044 .get_link = ethtool_op_get_link,
1045 .get_msglevel = fec_get_msglevel,
1046 .set_msglevel = fec_set_msglevel,
1047 .get_tx_csum = ethtool_op_get_tx_csum,
1048 .set_tx_csum = ethtool_op_set_tx_csum, /* local! */
1049 .get_sg = ethtool_op_get_sg,
1050 .set_sg = ethtool_op_set_sg,
1051 .get_regs = fec_get_regs,
1054 static int fec_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1056 struct fec_enet_private *fep = netdev_priv(dev);
1057 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data;
1058 unsigned long flags;
1059 int rc;
1061 if (!netif_running(dev))
1062 return -EINVAL;
1064 spin_lock_irqsave(&fep->lock, flags);
1065 rc = generic_mii_ioctl(&fep->mii_if, mii, cmd, NULL);
1066 spin_unlock_irqrestore(&fep->lock, flags);
1067 return rc;
1070 int fec_8xx_init_one(const struct fec_platform_info *fpi,
1071 struct net_device **devp)
1073 immap_t *immap = (immap_t *) IMAP_ADDR;
1074 static int fec_8xx_version_printed = 0;
1075 struct net_device *dev = NULL;
1076 struct fec_enet_private *fep = NULL;
1077 fec_t *fecp = NULL;
1078 int i;
1079 int err = 0;
1080 int registered = 0;
1081 __u32 siel;
1083 *devp = NULL;
1085 switch (fpi->fec_no) {
1086 case 0:
1087 fecp = &((immap_t *) IMAP_ADDR)->im_cpm.cp_fec;
1088 break;
1089 #ifdef CONFIG_DUET
1090 case 1:
1091 fecp = &((immap_t *) IMAP_ADDR)->im_cpm.cp_fec2;
1092 break;
1093 #endif
1094 default:
1095 return -EINVAL;
1098 if (fec_8xx_version_printed++ == 0)
1099 printk(KERN_INFO "%s", version);
1101 i = sizeof(*fep) + (sizeof(struct sk_buff **) *
1102 (fpi->rx_ring + fpi->tx_ring));
1104 dev = alloc_etherdev(i);
1105 if (!dev) {
1106 err = -ENOMEM;
1107 goto err;
1109 SET_MODULE_OWNER(dev);
1111 fep = netdev_priv(dev);
1113 /* partial reset of FEC */
1114 fec_whack_reset(fecp);
1116 /* point rx_skbuff, tx_skbuff */
1117 fep->rx_skbuff = (struct sk_buff **)&fep[1];
1118 fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
1120 fep->fecp = fecp;
1121 fep->fpi = fpi;
1123 /* init locks */
1124 spin_lock_init(&fep->lock);
1125 spin_lock_init(&fep->tx_lock);
1128 * Set the Ethernet address.
1130 for (i = 0; i < 6; i++)
1131 dev->dev_addr[i] = fpi->macaddr[i];
1133 fep->ring_base = dma_alloc_coherent(NULL,
1134 (fpi->tx_ring + fpi->rx_ring) *
1135 sizeof(cbd_t), &fep->ring_mem_addr,
1136 GFP_KERNEL);
1137 if (fep->ring_base == NULL) {
1138 printk(KERN_ERR DRV_MODULE_NAME
1139 ": %s dma alloc failed.\n", dev->name);
1140 err = -ENOMEM;
1141 goto err;
1145 * Set receive and transmit descriptor base.
1147 fep->rx_bd_base = fep->ring_base;
1148 fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1150 /* initialize ring size variables */
1151 fep->tx_ring = fpi->tx_ring;
1152 fep->rx_ring = fpi->rx_ring;
1154 /* SIU interrupt */
1155 if (fpi->phy_irq != -1 &&
1156 (fpi->phy_irq >= SIU_IRQ0 && fpi->phy_irq < SIU_LEVEL7)) {
1158 siel = in_be32(&immap->im_siu_conf.sc_siel);
1159 if ((fpi->phy_irq & 1) == 0)
1160 siel |= (0x80000000 >> fpi->phy_irq);
1161 else
1162 siel &= ~(0x80000000 >> (fpi->phy_irq & ~1));
1163 out_be32(&immap->im_siu_conf.sc_siel, siel);
1167 * The FEC Ethernet specific entries in the device structure.
1169 dev->open = fec_enet_open;
1170 dev->hard_start_xmit = fec_enet_start_xmit;
1171 dev->tx_timeout = fec_timeout;
1172 dev->watchdog_timeo = TX_TIMEOUT;
1173 dev->stop = fec_enet_close;
1174 dev->get_stats = fec_enet_get_stats;
1175 dev->set_multicast_list = fec_set_multicast_list;
1176 dev->set_mac_address = fec_set_mac_address;
1177 if (fpi->use_napi) {
1178 dev->poll = fec_enet_poll;
1179 dev->weight = fpi->napi_weight;
1181 dev->ethtool_ops = &fec_ethtool_ops;
1182 dev->do_ioctl = fec_ioctl;
1184 fep->fec_phy_speed =
1185 ((((fpi->sys_clk + 4999999) / 2500000) / 2) & 0x3F) << 1;
1187 init_timer(&fep->phy_timer_list);
1189 /* partial reset of FEC so that only MII works */
1190 FW(fecp, mii_speed, fep->fec_phy_speed);
1191 FW(fecp, ievent, 0xffc0);
1192 FW(fecp, ivec, (fpi->fec_irq / 2) << 29);
1193 FW(fecp, imask, 0);
1194 FW(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
1195 FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
1197 netif_carrier_off(dev);
1199 err = register_netdev(dev);
1200 if (err != 0)
1201 goto err;
1202 registered = 1;
1204 if (fpi->use_mdio) {
1205 fep->mii_if.dev = dev;
1206 fep->mii_if.mdio_read = fec_mii_read;
1207 fep->mii_if.mdio_write = fec_mii_write;
1208 fep->mii_if.phy_id_mask = 0x1f;
1209 fep->mii_if.reg_num_mask = 0x1f;
1210 fep->mii_if.phy_id = fec_mii_phy_id_detect(dev);
1213 *devp = dev;
1215 return 0;
1217 err:
1218 if (dev != NULL) {
1219 if (fecp != NULL)
1220 fec_whack_reset(fecp);
1222 if (registered)
1223 unregister_netdev(dev);
1225 if (fep != NULL) {
1226 if (fep->ring_base)
1227 dma_free_coherent(NULL,
1228 (fpi->tx_ring +
1229 fpi->rx_ring) *
1230 sizeof(cbd_t), fep->ring_base,
1231 fep->ring_mem_addr);
1233 free_netdev(dev);
1235 return err;
1238 int fec_8xx_cleanup_one(struct net_device *dev)
1240 struct fec_enet_private *fep = netdev_priv(dev);
1241 fec_t *fecp = fep->fecp;
1242 const struct fec_platform_info *fpi = fep->fpi;
1244 fec_whack_reset(fecp);
1246 unregister_netdev(dev);
1248 dma_free_coherent(NULL, (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t),
1249 fep->ring_base, fep->ring_mem_addr);
1251 free_netdev(dev);
1253 return 0;
1256 /**************************************************************************************/
1257 /**************************************************************************************/
1258 /**************************************************************************************/
1260 static int __init fec_8xx_init(void)
1262 return fec_8xx_platform_init();
1265 static void __exit fec_8xx_cleanup(void)
1267 fec_8xx_platform_cleanup();
1270 /**************************************************************************************/
1271 /**************************************************************************************/
1272 /**************************************************************************************/
1274 module_init(fec_8xx_init);
1275 module_exit(fec_8xx_cleanup);