Pull bugzilla-9429 into release branch
[pv_ops_mirror.git] / drivers / net / fec_8xx / fec_main.c
blob8d2904fa57896d33db21f274ca9181fb6f2f828a
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/module.h>
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/string.h>
17 #include <linux/ptrace.h>
18 #include <linux/errno.h>
19 #include <linux/ioport.h>
20 #include <linux/slab.h>
21 #include <linux/interrupt.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/skbuff.h>
27 #include <linux/spinlock.h>
28 #include <linux/mii.h>
29 #include <linux/ethtool.h>
30 #include <linux/bitops.h>
31 #include <linux/dma-mapping.h>
33 #include <asm/8xx_immap.h>
34 #include <asm/pgtable.h>
35 #include <asm/mpc8xx.h>
36 #include <asm/irq.h>
37 #include <asm/uaccess.h>
38 #include <asm/commproc.h>
40 #include "fec_8xx.h"
42 /*************************************************/
44 #define FEC_MAX_MULTICAST_ADDRS 64
46 /*************************************************/
48 static char version[] __devinitdata =
49 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")" "\n";
51 MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
52 MODULE_DESCRIPTION("Motorola 8xx FEC ethernet driver");
53 MODULE_LICENSE("GPL");
55 int fec_8xx_debug = -1; /* -1 == use FEC_8XX_DEF_MSG_ENABLE as value */
56 module_param(fec_8xx_debug, int, 0);
57 MODULE_PARM_DESC(fec_8xx_debug,
58 "FEC 8xx bitmapped debugging message enable value");
61 /*************************************************/
64 * Delay to wait for FEC reset command to complete (in us)
66 #define FEC_RESET_DELAY 50
68 /*****************************************************************************************/
70 static void fec_whack_reset(fec_t * fecp)
72 int i;
75 * Whack a reset. We should wait for this.
77 FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET);
78 for (i = 0;
79 (FR(fecp, ecntrl) & FEC_ECNTRL_RESET) != 0 && i < FEC_RESET_DELAY;
80 i++)
81 udelay(1);
83 if (i == FEC_RESET_DELAY)
84 printk(KERN_WARNING "FEC Reset timeout!\n");
88 /****************************************************************************/
91 * Transmitter timeout.
93 #define TX_TIMEOUT (2*HZ)
95 /****************************************************************************/
98 * Returns the CRC needed when filling in the hash table for
99 * multicast group filtering
100 * pAddr must point to a MAC address (6 bytes)
102 static __u32 fec_mulicast_calc_crc(char *pAddr)
104 u8 byte;
105 int byte_count;
106 int bit_count;
107 __u32 crc = 0xffffffff;
108 u8 msb;
110 for (byte_count = 0; byte_count < 6; byte_count++) {
111 byte = pAddr[byte_count];
112 for (bit_count = 0; bit_count < 8; bit_count++) {
113 msb = crc >> 31;
114 crc <<= 1;
115 if (msb ^ (byte & 0x1)) {
116 crc ^= FEC_CRC_POLY;
118 byte >>= 1;
121 return (crc);
125 * Set or clear the multicast filter for this adaptor.
126 * Skeleton taken from sunlance driver.
127 * The CPM Ethernet implementation allows Multicast as well as individual
128 * MAC address filtering. Some of the drivers check to make sure it is
129 * a group multicast address, and discard those that are not. I guess I
130 * will do the same for now, but just remove the test if you want
131 * individual filtering as well (do the upper net layers want or support
132 * this kind of feature?).
134 static void fec_set_multicast_list(struct net_device *dev)
136 struct fec_enet_private *fep = netdev_priv(dev);
137 fec_t *fecp = fep->fecp;
138 struct dev_mc_list *pmc;
139 __u32 crc;
140 int temp;
141 __u32 csrVal;
142 int hash_index;
143 __u32 hthi, htlo;
144 unsigned long flags;
147 if ((dev->flags & IFF_PROMISC) != 0) {
149 spin_lock_irqsave(&fep->lock, flags);
150 FS(fecp, r_cntrl, FEC_RCNTRL_PROM);
151 spin_unlock_irqrestore(&fep->lock, flags);
154 * Log any net taps.
156 printk(KERN_WARNING DRV_MODULE_NAME
157 ": %s: Promiscuous mode enabled.\n", dev->name);
158 return;
162 if ((dev->flags & IFF_ALLMULTI) != 0 ||
163 dev->mc_count > FEC_MAX_MULTICAST_ADDRS) {
165 * Catch all multicast addresses, set the filter to all 1's.
167 hthi = 0xffffffffU;
168 htlo = 0xffffffffU;
169 } else {
170 hthi = 0;
171 htlo = 0;
174 * Now populate the hash table
176 for (pmc = dev->mc_list; pmc != NULL; pmc = pmc->next) {
177 crc = fec_mulicast_calc_crc(pmc->dmi_addr);
178 temp = (crc & 0x3f) >> 1;
179 hash_index = ((temp & 0x01) << 4) |
180 ((temp & 0x02) << 2) |
181 ((temp & 0x04)) |
182 ((temp & 0x08) >> 2) |
183 ((temp & 0x10) >> 4);
184 csrVal = (1 << hash_index);
185 if (crc & 1)
186 hthi |= csrVal;
187 else
188 htlo |= csrVal;
192 spin_lock_irqsave(&fep->lock, flags);
193 FC(fecp, r_cntrl, FEC_RCNTRL_PROM);
194 FW(fecp, hash_table_high, hthi);
195 FW(fecp, hash_table_low, htlo);
196 spin_unlock_irqrestore(&fep->lock, flags);
199 static int fec_set_mac_address(struct net_device *dev, void *addr)
201 struct sockaddr *mac = addr;
202 struct fec_enet_private *fep = netdev_priv(dev);
203 struct fec *fecp = fep->fecp;
204 int i;
205 __u32 addrhi, addrlo;
206 unsigned long flags;
208 /* Get pointer to SCC area in parameter RAM. */
209 for (i = 0; i < 6; i++)
210 dev->dev_addr[i] = mac->sa_data[i];
213 * Set station address.
215 addrhi = ((__u32) dev->dev_addr[0] << 24) |
216 ((__u32) dev->dev_addr[1] << 16) |
217 ((__u32) dev->dev_addr[2] << 8) |
218 (__u32) dev->dev_addr[3];
219 addrlo = ((__u32) dev->dev_addr[4] << 24) |
220 ((__u32) dev->dev_addr[5] << 16);
222 spin_lock_irqsave(&fep->lock, flags);
223 FW(fecp, addr_low, addrhi);
224 FW(fecp, addr_high, addrlo);
225 spin_unlock_irqrestore(&fep->lock, flags);
227 return 0;
231 * This function is called to start or restart the FEC during a link
232 * change. This only happens when switching between half and full
233 * duplex.
235 void fec_restart(struct net_device *dev, int duplex, int speed)
237 #ifdef CONFIG_DUET
238 immap_t *immap = (immap_t *) IMAP_ADDR;
239 __u32 cptr;
240 #endif
241 struct fec_enet_private *fep = netdev_priv(dev);
242 struct fec *fecp = fep->fecp;
243 const struct fec_platform_info *fpi = fep->fpi;
244 cbd_t *bdp;
245 struct sk_buff *skb;
246 int i;
247 __u32 addrhi, addrlo;
249 fec_whack_reset(fep->fecp);
252 * Set station address.
254 addrhi = ((__u32) dev->dev_addr[0] << 24) |
255 ((__u32) dev->dev_addr[1] << 16) |
256 ((__u32) dev->dev_addr[2] << 8) |
257 (__u32) dev->dev_addr[3];
258 addrlo = ((__u32) dev->dev_addr[4] << 24) |
259 ((__u32) dev->dev_addr[5] << 16);
260 FW(fecp, addr_low, addrhi);
261 FW(fecp, addr_high, addrlo);
264 * Reset all multicast.
266 FW(fecp, hash_table_high, 0);
267 FW(fecp, hash_table_low, 0);
270 * Set maximum receive buffer size.
272 FW(fecp, r_buff_size, PKT_MAXBLR_SIZE);
273 FW(fecp, r_hash, PKT_MAXBUF_SIZE);
276 * Set receive and transmit descriptor base.
278 FW(fecp, r_des_start, iopa((__u32) (fep->rx_bd_base)));
279 FW(fecp, x_des_start, iopa((__u32) (fep->tx_bd_base)));
281 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
282 fep->tx_free = fep->tx_ring;
283 fep->cur_rx = fep->rx_bd_base;
286 * Reset SKB receive buffers
288 for (i = 0; i < fep->rx_ring; i++) {
289 if ((skb = fep->rx_skbuff[i]) == NULL)
290 continue;
291 fep->rx_skbuff[i] = NULL;
292 dev_kfree_skb(skb);
296 * Initialize the receive buffer descriptors.
298 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
299 skb = dev_alloc_skb(ENET_RX_FRSIZE);
300 if (skb == NULL) {
301 printk(KERN_WARNING DRV_MODULE_NAME
302 ": %s Memory squeeze, unable to allocate skb\n",
303 dev->name);
304 fep->stats.rx_dropped++;
305 break;
307 fep->rx_skbuff[i] = skb;
308 skb->dev = dev;
309 CBDW_BUFADDR(bdp, dma_map_single(NULL, skb->data,
310 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
311 DMA_FROM_DEVICE));
312 CBDW_DATLEN(bdp, 0); /* zero */
313 CBDW_SC(bdp, BD_ENET_RX_EMPTY |
314 ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
317 * if we failed, fillup remainder
319 for (; i < fep->rx_ring; i++, bdp++) {
320 fep->rx_skbuff[i] = NULL;
321 CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
325 * Reset SKB transmit buffers.
327 for (i = 0; i < fep->tx_ring; i++) {
328 if ((skb = fep->tx_skbuff[i]) == NULL)
329 continue;
330 fep->tx_skbuff[i] = NULL;
331 dev_kfree_skb(skb);
335 * ...and the same for transmit.
337 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
338 fep->tx_skbuff[i] = NULL;
339 CBDW_BUFADDR(bdp, virt_to_bus(NULL));
340 CBDW_DATLEN(bdp, 0);
341 CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
345 * Enable big endian and don't care about SDMA FC.
347 FW(fecp, fun_code, 0x78000000);
350 * Set MII speed.
352 FW(fecp, mii_speed, fep->fec_phy_speed);
355 * Clear any outstanding interrupt.
357 FW(fecp, ievent, 0xffc0);
358 FW(fecp, ivec, (fpi->fec_irq / 2) << 29);
361 * adjust to speed (only for DUET & RMII)
363 #ifdef CONFIG_DUET
364 cptr = in_be32(&immap->im_cpm.cp_cptr);
365 switch (fpi->fec_no) {
366 case 0:
368 * check if in RMII mode
370 if ((cptr & 0x100) == 0)
371 break;
373 if (speed == 10)
374 cptr |= 0x0000010;
375 else if (speed == 100)
376 cptr &= ~0x0000010;
377 break;
378 case 1:
380 * check if in RMII mode
382 if ((cptr & 0x80) == 0)
383 break;
385 if (speed == 10)
386 cptr |= 0x0000008;
387 else if (speed == 100)
388 cptr &= ~0x0000008;
389 break;
390 default:
391 break;
393 out_be32(&immap->im_cpm.cp_cptr, cptr);
394 #endif
396 FW(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
398 * adjust to duplex mode
400 if (duplex) {
401 FC(fecp, r_cntrl, FEC_RCNTRL_DRT);
402 FS(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD enable */
403 } else {
404 FS(fecp, r_cntrl, FEC_RCNTRL_DRT);
405 FC(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD disable */
409 * Enable interrupts we wish to service.
411 FW(fecp, imask, FEC_ENET_TXF | FEC_ENET_TXB |
412 FEC_ENET_RXF | FEC_ENET_RXB);
415 * And last, enable the transmit and receive processing.
417 FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
418 FW(fecp, r_des_active, 0x01000000);
421 void fec_stop(struct net_device *dev)
423 struct fec_enet_private *fep = netdev_priv(dev);
424 fec_t *fecp = fep->fecp;
425 struct sk_buff *skb;
426 int i;
428 if ((FR(fecp, ecntrl) & FEC_ECNTRL_ETHER_EN) == 0)
429 return; /* already down */
431 FW(fecp, x_cntrl, 0x01); /* Graceful transmit stop */
432 for (i = 0; ((FR(fecp, ievent) & 0x10000000) == 0) &&
433 i < FEC_RESET_DELAY; i++)
434 udelay(1);
436 if (i == FEC_RESET_DELAY)
437 printk(KERN_WARNING DRV_MODULE_NAME
438 ": %s FEC timeout on graceful transmit stop\n",
439 dev->name);
441 * Disable FEC. Let only MII interrupts.
443 FW(fecp, imask, 0);
444 FW(fecp, ecntrl, ~FEC_ECNTRL_ETHER_EN);
447 * Reset SKB transmit buffers.
449 for (i = 0; i < fep->tx_ring; i++) {
450 if ((skb = fep->tx_skbuff[i]) == NULL)
451 continue;
452 fep->tx_skbuff[i] = NULL;
453 dev_kfree_skb(skb);
457 * Reset SKB receive buffers
459 for (i = 0; i < fep->rx_ring; i++) {
460 if ((skb = fep->rx_skbuff[i]) == NULL)
461 continue;
462 fep->rx_skbuff[i] = NULL;
463 dev_kfree_skb(skb);
467 /* common receive function */
468 static int fec_enet_rx_common(struct fec_enet_private *ep,
469 struct net_device *dev, int budget)
471 fec_t *fecp = fep->fecp;
472 const struct fec_platform_info *fpi = fep->fpi;
473 cbd_t *bdp;
474 struct sk_buff *skb, *skbn, *skbt;
475 int received = 0;
476 __u16 pkt_len, sc;
477 int curidx;
479 if (fpi->use_napi) {
480 if (!netif_running(dev))
481 return 0;
485 * First, grab all of the stats for the incoming packet.
486 * These get messed up if we get called due to a busy condition.
488 bdp = fep->cur_rx;
490 /* clear RX status bits for napi*/
491 if (fpi->use_napi)
492 FW(fecp, ievent, FEC_ENET_RXF | FEC_ENET_RXB);
494 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
496 curidx = bdp - fep->rx_bd_base;
499 * Since we have allocated space to hold a complete frame,
500 * the last indicator should be set.
502 if ((sc & BD_ENET_RX_LAST) == 0)
503 printk(KERN_WARNING DRV_MODULE_NAME
504 ": %s rcv is not +last\n",
505 dev->name);
508 * Check for errors.
510 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
511 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
512 fep->stats.rx_errors++;
513 /* Frame too long or too short. */
514 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
515 fep->stats.rx_length_errors++;
516 /* Frame alignment */
517 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
518 fep->stats.rx_frame_errors++;
519 /* CRC Error */
520 if (sc & BD_ENET_RX_CR)
521 fep->stats.rx_crc_errors++;
522 /* FIFO overrun */
523 if (sc & BD_ENET_RX_OV)
524 fep->stats.rx_crc_errors++;
526 skbn = fep->rx_skbuff[curidx];
527 BUG_ON(skbn == NULL);
529 } else {
530 skb = fep->rx_skbuff[curidx];
531 BUG_ON(skb == NULL);
534 * Process the incoming frame.
536 fep->stats.rx_packets++;
537 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
538 fep->stats.rx_bytes += pkt_len + 4;
540 if (pkt_len <= fpi->rx_copybreak) {
541 /* +2 to make IP header L1 cache aligned */
542 skbn = dev_alloc_skb(pkt_len + 2);
543 if (skbn != NULL) {
544 skb_reserve(skbn, 2); /* align IP header */
545 skb_copy_from_linear_data(skb,
546 skbn->data,
547 pkt_len);
548 /* swap */
549 skbt = skb;
550 skb = skbn;
551 skbn = skbt;
553 } else
554 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
556 if (skbn != NULL) {
557 skb_put(skb, pkt_len); /* Make room */
558 skb->protocol = eth_type_trans(skb, dev);
559 received++;
560 if (!fpi->use_napi)
561 netif_rx(skb);
562 else
563 netif_receive_skb(skb);
564 } else {
565 printk(KERN_WARNING DRV_MODULE_NAME
566 ": %s Memory squeeze, dropping packet.\n",
567 dev->name);
568 fep->stats.rx_dropped++;
569 skbn = skb;
573 fep->rx_skbuff[curidx] = skbn;
574 CBDW_BUFADDR(bdp, dma_map_single(NULL, skbn->data,
575 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
576 DMA_FROM_DEVICE));
577 CBDW_DATLEN(bdp, 0);
578 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
581 * Update BD pointer to next entry.
583 if ((sc & BD_ENET_RX_WRAP) == 0)
584 bdp++;
585 else
586 bdp = fep->rx_bd_base;
589 * Doing this here will keep the FEC running while we process
590 * incoming frames. On a heavily loaded network, we should be
591 * able to keep up at the expense of system resources.
593 FW(fecp, r_des_active, 0x01000000);
595 if (received >= budget)
596 break;
600 fep->cur_rx = bdp;
602 if (fpi->use_napi) {
603 if (received < budget) {
604 netif_rx_complete(dev, &fep->napi);
606 /* enable RX interrupt bits */
607 FS(fecp, imask, FEC_ENET_RXF | FEC_ENET_RXB);
611 return received;
614 static void fec_enet_tx(struct net_device *dev)
616 struct fec_enet_private *fep = netdev_priv(dev);
617 cbd_t *bdp;
618 struct sk_buff *skb;
619 int dirtyidx, do_wake;
620 __u16 sc;
622 spin_lock(&fep->lock);
623 bdp = fep->dirty_tx;
625 do_wake = 0;
626 while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
628 dirtyidx = bdp - fep->tx_bd_base;
630 if (fep->tx_free == fep->tx_ring)
631 break;
633 skb = fep->tx_skbuff[dirtyidx];
636 * Check for errors.
638 if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
639 BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
640 fep->stats.tx_errors++;
641 if (sc & BD_ENET_TX_HB) /* No heartbeat */
642 fep->stats.tx_heartbeat_errors++;
643 if (sc & BD_ENET_TX_LC) /* Late collision */
644 fep->stats.tx_window_errors++;
645 if (sc & BD_ENET_TX_RL) /* Retrans limit */
646 fep->stats.tx_aborted_errors++;
647 if (sc & BD_ENET_TX_UN) /* Underrun */
648 fep->stats.tx_fifo_errors++;
649 if (sc & BD_ENET_TX_CSL) /* Carrier lost */
650 fep->stats.tx_carrier_errors++;
651 } else
652 fep->stats.tx_packets++;
654 if (sc & BD_ENET_TX_READY)
655 printk(KERN_WARNING DRV_MODULE_NAME
656 ": %s HEY! Enet xmit interrupt and TX_READY.\n",
657 dev->name);
660 * Deferred means some collisions occurred during transmit,
661 * but we eventually sent the packet OK.
663 if (sc & BD_ENET_TX_DEF)
664 fep->stats.collisions++;
667 * Free the sk buffer associated with this last transmit.
669 dev_kfree_skb_irq(skb);
670 fep->tx_skbuff[dirtyidx] = NULL;
673 * Update pointer to next buffer descriptor to be transmitted.
675 if ((sc & BD_ENET_TX_WRAP) == 0)
676 bdp++;
677 else
678 bdp = fep->tx_bd_base;
681 * Since we have freed up a buffer, the ring is no longer
682 * full.
684 if (!fep->tx_free++)
685 do_wake = 1;
688 fep->dirty_tx = bdp;
690 spin_unlock(&fep->lock);
692 if (do_wake && netif_queue_stopped(dev))
693 netif_wake_queue(dev);
697 * The interrupt handler.
698 * This is called from the MPC core interrupt.
700 static irqreturn_t
701 fec_enet_interrupt(int irq, void *dev_id)
703 struct net_device *dev = dev_id;
704 struct fec_enet_private *fep;
705 const struct fec_platform_info *fpi;
706 fec_t *fecp;
707 __u32 int_events;
708 __u32 int_events_napi;
710 if (unlikely(dev == NULL))
711 return IRQ_NONE;
713 fep = netdev_priv(dev);
714 fecp = fep->fecp;
715 fpi = fep->fpi;
718 * Get the interrupt events that caused us to be here.
720 while ((int_events = FR(fecp, ievent) & FR(fecp, imask)) != 0) {
722 if (!fpi->use_napi)
723 FW(fecp, ievent, int_events);
724 else {
725 int_events_napi = int_events & ~(FEC_ENET_RXF | FEC_ENET_RXB);
726 FW(fecp, ievent, int_events_napi);
729 if ((int_events & (FEC_ENET_HBERR | FEC_ENET_BABR |
730 FEC_ENET_BABT | FEC_ENET_EBERR)) != 0)
731 printk(KERN_WARNING DRV_MODULE_NAME
732 ": %s FEC ERROR(s) 0x%x\n",
733 dev->name, int_events);
735 if ((int_events & FEC_ENET_RXF) != 0) {
736 if (!fpi->use_napi)
737 fec_enet_rx_common(fep, dev, ~0);
738 else {
739 if (netif_rx_schedule_prep(dev, &fep->napi)) {
740 /* disable rx interrupts */
741 FC(fecp, imask, FEC_ENET_RXF | FEC_ENET_RXB);
742 __netif_rx_schedule(dev, &fep->napi);
743 } else {
744 printk(KERN_ERR DRV_MODULE_NAME
745 ": %s driver bug! interrupt while in poll!\n",
746 dev->name);
747 FC(fecp, imask, FEC_ENET_RXF | FEC_ENET_RXB);
752 if ((int_events & FEC_ENET_TXF) != 0)
753 fec_enet_tx(dev);
756 return IRQ_HANDLED;
759 /* This interrupt occurs when the PHY detects a link change. */
760 static irqreturn_t
761 fec_mii_link_interrupt(int irq, void *dev_id)
763 struct net_device *dev = dev_id;
764 struct fec_enet_private *fep;
765 const struct fec_platform_info *fpi;
767 if (unlikely(dev == NULL))
768 return IRQ_NONE;
770 fep = netdev_priv(dev);
771 fpi = fep->fpi;
773 if (!fpi->use_mdio)
774 return IRQ_NONE;
777 * Acknowledge the interrupt if possible. If we have not
778 * found the PHY yet we can't process or acknowledge the
779 * interrupt now. Instead we ignore this interrupt for now,
780 * which we can do since it is edge triggered. It will be
781 * acknowledged later by fec_enet_open().
783 if (!fep->phy)
784 return IRQ_NONE;
786 fec_mii_ack_int(dev);
787 fec_mii_link_status_change_check(dev, 0);
789 return IRQ_HANDLED;
793 /**********************************************************************************/
795 static int fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
797 struct fec_enet_private *fep = netdev_priv(dev);
798 fec_t *fecp = fep->fecp;
799 cbd_t *bdp;
800 int curidx;
801 unsigned long flags;
803 spin_lock_irqsave(&fep->tx_lock, flags);
806 * Fill in a Tx ring entry
808 bdp = fep->cur_tx;
810 if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
811 netif_stop_queue(dev);
812 spin_unlock_irqrestore(&fep->tx_lock, flags);
815 * Ooops. All transmit buffers are full. Bail out.
816 * This should not happen, since the tx queue should be stopped.
818 printk(KERN_WARNING DRV_MODULE_NAME
819 ": %s tx queue full!.\n", dev->name);
820 return 1;
823 curidx = bdp - fep->tx_bd_base;
825 * Clear all of the status flags.
827 CBDC_SC(bdp, BD_ENET_TX_STATS);
830 * Save skb pointer.
832 fep->tx_skbuff[curidx] = skb;
834 fep->stats.tx_bytes += skb->len;
837 * Push the data cache so the CPM does not get stale memory data.
839 CBDW_BUFADDR(bdp, dma_map_single(NULL, skb->data,
840 skb->len, DMA_TO_DEVICE));
841 CBDW_DATLEN(bdp, skb->len);
843 dev->trans_start = jiffies;
846 * If this was the last BD in the ring, start at the beginning again.
848 if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
849 fep->cur_tx++;
850 else
851 fep->cur_tx = fep->tx_bd_base;
853 if (!--fep->tx_free)
854 netif_stop_queue(dev);
857 * Trigger transmission start
859 CBDS_SC(bdp, BD_ENET_TX_READY | BD_ENET_TX_INTR |
860 BD_ENET_TX_LAST | BD_ENET_TX_TC);
861 FW(fecp, x_des_active, 0x01000000);
863 spin_unlock_irqrestore(&fep->tx_lock, flags);
865 return 0;
868 static void fec_timeout(struct net_device *dev)
870 struct fec_enet_private *fep = netdev_priv(dev);
872 fep->stats.tx_errors++;
874 if (fep->tx_free)
875 netif_wake_queue(dev);
877 /* check link status again */
878 fec_mii_link_status_change_check(dev, 0);
881 static int fec_enet_open(struct net_device *dev)
883 struct fec_enet_private *fep = netdev_priv(dev);
884 const struct fec_platform_info *fpi = fep->fpi;
885 unsigned long flags;
887 napi_enable(&fep->napi);
889 /* Install our interrupt handler. */
890 if (request_irq(fpi->fec_irq, fec_enet_interrupt, 0, "fec", dev) != 0) {
891 printk(KERN_ERR DRV_MODULE_NAME
892 ": %s Could not allocate FEC IRQ!", dev->name);
893 napi_disable(&fep->napi);
894 return -EINVAL;
897 /* Install our phy interrupt handler */
898 if (fpi->phy_irq != -1 &&
899 request_irq(fpi->phy_irq, fec_mii_link_interrupt, 0, "fec-phy",
900 dev) != 0) {
901 printk(KERN_ERR DRV_MODULE_NAME
902 ": %s Could not allocate PHY IRQ!", dev->name);
903 free_irq(fpi->fec_irq, dev);
904 napi_disable(&fep->napi);
905 return -EINVAL;
908 if (fpi->use_mdio) {
909 fec_mii_startup(dev);
910 netif_carrier_off(dev);
911 fec_mii_link_status_change_check(dev, 1);
912 } else {
913 spin_lock_irqsave(&fep->lock, flags);
914 fec_restart(dev, 1, 100); /* XXX this sucks */
915 spin_unlock_irqrestore(&fep->lock, flags);
917 netif_carrier_on(dev);
918 netif_start_queue(dev);
920 return 0;
923 static int fec_enet_close(struct net_device *dev)
925 struct fec_enet_private *fep = netdev_priv(dev);
926 const struct fec_platform_info *fpi = fep->fpi;
927 unsigned long flags;
929 netif_stop_queue(dev);
930 napi_disable(&fep->napi);
931 netif_carrier_off(dev);
933 if (fpi->use_mdio)
934 fec_mii_shutdown(dev);
936 spin_lock_irqsave(&fep->lock, flags);
937 fec_stop(dev);
938 spin_unlock_irqrestore(&fep->lock, flags);
940 /* release any irqs */
941 if (fpi->phy_irq != -1)
942 free_irq(fpi->phy_irq, dev);
943 free_irq(fpi->fec_irq, dev);
945 return 0;
948 static struct net_device_stats *fec_enet_get_stats(struct net_device *dev)
950 struct fec_enet_private *fep = netdev_priv(dev);
951 return &fep->stats;
954 static int fec_enet_poll(struct napi_struct *napi, int budget)
956 struct fec_enet_private *fep = container_of(napi, struct fec_enet_private, napi);
957 struct net_device *dev = fep->dev;
959 return fec_enet_rx_common(fep, dev, budget);
962 /*************************************************************************/
964 static void fec_get_drvinfo(struct net_device *dev,
965 struct ethtool_drvinfo *info)
967 strcpy(info->driver, DRV_MODULE_NAME);
968 strcpy(info->version, DRV_MODULE_VERSION);
971 static int fec_get_regs_len(struct net_device *dev)
973 return sizeof(fec_t);
976 static void fec_get_regs(struct net_device *dev, struct ethtool_regs *regs,
977 void *p)
979 struct fec_enet_private *fep = netdev_priv(dev);
980 unsigned long flags;
982 if (regs->len < sizeof(fec_t))
983 return;
985 regs->version = 0;
986 spin_lock_irqsave(&fep->lock, flags);
987 memcpy_fromio(p, fep->fecp, sizeof(fec_t));
988 spin_unlock_irqrestore(&fep->lock, flags);
991 static int fec_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
993 struct fec_enet_private *fep = netdev_priv(dev);
994 unsigned long flags;
995 int rc;
997 spin_lock_irqsave(&fep->lock, flags);
998 rc = mii_ethtool_gset(&fep->mii_if, cmd);
999 spin_unlock_irqrestore(&fep->lock, flags);
1001 return rc;
1004 static int fec_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1006 struct fec_enet_private *fep = netdev_priv(dev);
1007 unsigned long flags;
1008 int rc;
1010 spin_lock_irqsave(&fep->lock, flags);
1011 rc = mii_ethtool_sset(&fep->mii_if, cmd);
1012 spin_unlock_irqrestore(&fep->lock, flags);
1014 return rc;
1017 static int fec_nway_reset(struct net_device *dev)
1019 struct fec_enet_private *fep = netdev_priv(dev);
1020 return mii_nway_restart(&fep->mii_if);
1023 static __u32 fec_get_msglevel(struct net_device *dev)
1025 struct fec_enet_private *fep = netdev_priv(dev);
1026 return fep->msg_enable;
1029 static void fec_set_msglevel(struct net_device *dev, __u32 value)
1031 struct fec_enet_private *fep = netdev_priv(dev);
1032 fep->msg_enable = value;
1035 static const struct ethtool_ops fec_ethtool_ops = {
1036 .get_drvinfo = fec_get_drvinfo,
1037 .get_regs_len = fec_get_regs_len,
1038 .get_settings = fec_get_settings,
1039 .set_settings = fec_set_settings,
1040 .nway_reset = fec_nway_reset,
1041 .get_link = ethtool_op_get_link,
1042 .get_msglevel = fec_get_msglevel,
1043 .set_msglevel = fec_set_msglevel,
1044 .set_tx_csum = ethtool_op_set_tx_csum, /* local! */
1045 .set_sg = ethtool_op_set_sg,
1046 .get_regs = fec_get_regs,
1049 static int fec_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1051 struct fec_enet_private *fep = netdev_priv(dev);
1052 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data;
1053 unsigned long flags;
1054 int rc;
1056 if (!netif_running(dev))
1057 return -EINVAL;
1059 spin_lock_irqsave(&fep->lock, flags);
1060 rc = generic_mii_ioctl(&fep->mii_if, mii, cmd, NULL);
1061 spin_unlock_irqrestore(&fep->lock, flags);
1062 return rc;
1065 int fec_8xx_init_one(const struct fec_platform_info *fpi,
1066 struct net_device **devp)
1068 immap_t *immap = (immap_t *) IMAP_ADDR;
1069 static int fec_8xx_version_printed = 0;
1070 struct net_device *dev = NULL;
1071 struct fec_enet_private *fep = NULL;
1072 fec_t *fecp = NULL;
1073 int i;
1074 int err = 0;
1075 int registered = 0;
1076 __u32 siel;
1078 *devp = NULL;
1080 switch (fpi->fec_no) {
1081 case 0:
1082 fecp = &((immap_t *) IMAP_ADDR)->im_cpm.cp_fec;
1083 break;
1084 #ifdef CONFIG_DUET
1085 case 1:
1086 fecp = &((immap_t *) IMAP_ADDR)->im_cpm.cp_fec2;
1087 break;
1088 #endif
1089 default:
1090 return -EINVAL;
1093 if (fec_8xx_version_printed++ == 0)
1094 printk(KERN_INFO "%s", version);
1096 i = sizeof(*fep) + (sizeof(struct sk_buff **) *
1097 (fpi->rx_ring + fpi->tx_ring));
1099 dev = alloc_etherdev(i);
1100 if (!dev) {
1101 err = -ENOMEM;
1102 goto err;
1105 fep = netdev_priv(dev);
1106 fep->dev = dev;
1108 /* partial reset of FEC */
1109 fec_whack_reset(fecp);
1111 /* point rx_skbuff, tx_skbuff */
1112 fep->rx_skbuff = (struct sk_buff **)&fep[1];
1113 fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
1115 fep->fecp = fecp;
1116 fep->fpi = fpi;
1118 /* init locks */
1119 spin_lock_init(&fep->lock);
1120 spin_lock_init(&fep->tx_lock);
1123 * Set the Ethernet address.
1125 for (i = 0; i < 6; i++)
1126 dev->dev_addr[i] = fpi->macaddr[i];
1128 fep->ring_base = dma_alloc_coherent(NULL,
1129 (fpi->tx_ring + fpi->rx_ring) *
1130 sizeof(cbd_t), &fep->ring_mem_addr,
1131 GFP_KERNEL);
1132 if (fep->ring_base == NULL) {
1133 printk(KERN_ERR DRV_MODULE_NAME
1134 ": %s dma alloc failed.\n", dev->name);
1135 err = -ENOMEM;
1136 goto err;
1140 * Set receive and transmit descriptor base.
1142 fep->rx_bd_base = fep->ring_base;
1143 fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1145 /* initialize ring size variables */
1146 fep->tx_ring = fpi->tx_ring;
1147 fep->rx_ring = fpi->rx_ring;
1149 /* SIU interrupt */
1150 if (fpi->phy_irq != -1 &&
1151 (fpi->phy_irq >= SIU_IRQ0 && fpi->phy_irq < SIU_LEVEL7)) {
1153 siel = in_be32(&immap->im_siu_conf.sc_siel);
1154 if ((fpi->phy_irq & 1) == 0)
1155 siel |= (0x80000000 >> fpi->phy_irq);
1156 else
1157 siel &= ~(0x80000000 >> (fpi->phy_irq & ~1));
1158 out_be32(&immap->im_siu_conf.sc_siel, siel);
1162 * The FEC Ethernet specific entries in the device structure.
1164 dev->open = fec_enet_open;
1165 dev->hard_start_xmit = fec_enet_start_xmit;
1166 dev->tx_timeout = fec_timeout;
1167 dev->watchdog_timeo = TX_TIMEOUT;
1168 dev->stop = fec_enet_close;
1169 dev->get_stats = fec_enet_get_stats;
1170 dev->set_multicast_list = fec_set_multicast_list;
1171 dev->set_mac_address = fec_set_mac_address;
1172 netif_napi_add(dev, &fec->napi,
1173 fec_enet_poll, fpi->napi_weight);
1175 dev->ethtool_ops = &fec_ethtool_ops;
1176 dev->do_ioctl = fec_ioctl;
1178 fep->fec_phy_speed =
1179 ((((fpi->sys_clk + 4999999) / 2500000) / 2) & 0x3F) << 1;
1181 init_timer(&fep->phy_timer_list);
1183 /* partial reset of FEC so that only MII works */
1184 FW(fecp, mii_speed, fep->fec_phy_speed);
1185 FW(fecp, ievent, 0xffc0);
1186 FW(fecp, ivec, (fpi->fec_irq / 2) << 29);
1187 FW(fecp, imask, 0);
1188 FW(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
1189 FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
1191 netif_carrier_off(dev);
1193 err = register_netdev(dev);
1194 if (err != 0)
1195 goto err;
1196 registered = 1;
1198 if (fpi->use_mdio) {
1199 fep->mii_if.dev = dev;
1200 fep->mii_if.mdio_read = fec_mii_read;
1201 fep->mii_if.mdio_write = fec_mii_write;
1202 fep->mii_if.phy_id_mask = 0x1f;
1203 fep->mii_if.reg_num_mask = 0x1f;
1204 fep->mii_if.phy_id = fec_mii_phy_id_detect(dev);
1207 *devp = dev;
1209 return 0;
1211 err:
1212 if (dev != NULL) {
1213 if (fecp != NULL)
1214 fec_whack_reset(fecp);
1216 if (registered)
1217 unregister_netdev(dev);
1219 if (fep != NULL) {
1220 if (fep->ring_base)
1221 dma_free_coherent(NULL,
1222 (fpi->tx_ring +
1223 fpi->rx_ring) *
1224 sizeof(cbd_t), fep->ring_base,
1225 fep->ring_mem_addr);
1227 free_netdev(dev);
1229 return err;
1232 int fec_8xx_cleanup_one(struct net_device *dev)
1234 struct fec_enet_private *fep = netdev_priv(dev);
1235 fec_t *fecp = fep->fecp;
1236 const struct fec_platform_info *fpi = fep->fpi;
1238 fec_whack_reset(fecp);
1240 unregister_netdev(dev);
1242 dma_free_coherent(NULL, (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t),
1243 fep->ring_base, fep->ring_mem_addr);
1245 free_netdev(dev);
1247 return 0;
1250 /**************************************************************************************/
1251 /**************************************************************************************/
1252 /**************************************************************************************/
1254 static int __init fec_8xx_init(void)
1256 return fec_8xx_platform_init();
1259 static void __exit fec_8xx_cleanup(void)
1261 fec_8xx_platform_cleanup();
1264 /**************************************************************************************/
1265 /**************************************************************************************/
1266 /**************************************************************************************/
1268 module_init(fec_8xx_init);
1269 module_exit(fec_8xx_cleanup);