]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/net/fec_8xx/fec_main.c
Linux-2.6.12-rc2
[mirror_ubuntu-artful-kernel.git] / drivers / net / fec_8xx / fec_main.c
1 /*
2 * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
3 *
4 * Copyright (c) 2003 Intracom S.A.
5 * by Pantelis Antoniou <panto@intracom.gr>
6 *
7 * Heavily based on original FEC driver by Dan Malek <dan@embeddededge.com>
8 * and modifications by Joakim Tjernlund <joakim.tjernlund@lumentis.se>
9 *
10 * Released under the GPL
11 */
12
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>
34
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>
42
43 #include "fec_8xx.h"
44
45 /*************************************************/
46
47 #define FEC_MAX_MULTICAST_ADDRS 64
48
49 /*************************************************/
50
51 static char version[] __devinitdata =
52 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")" "\n";
53
54 MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
55 MODULE_DESCRIPTION("Motorola 8xx FEC ethernet driver");
56 MODULE_LICENSE("GPL");
57
58 MODULE_PARM(fec_8xx_debug, "i");
59 MODULE_PARM_DESC(fec_8xx_debug,
60 "FEC 8xx bitmapped debugging message enable value");
61
62 int fec_8xx_debug = -1; /* -1 == use FEC_8XX_DEF_MSG_ENABLE as value */
63
64 /*************************************************/
65
66 /*
67 * Delay to wait for FEC reset command to complete (in us)
68 */
69 #define FEC_RESET_DELAY 50
70
71 /*****************************************************************************************/
72
73 static void fec_whack_reset(fec_t * fecp)
74 {
75 int i;
76
77 /*
78 * Whack a reset. We should wait for this.
79 */
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);
85
86 if (i == FEC_RESET_DELAY)
87 printk(KERN_WARNING "FEC Reset timeout!\n");
88
89 }
90
91 /****************************************************************************/
92
93 /*
94 * Transmitter timeout.
95 */
96 #define TX_TIMEOUT (2*HZ)
97
98 /****************************************************************************/
99
100 /*
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)
104 */
105 static __u32 fec_mulicast_calc_crc(char *pAddr)
106 {
107 u8 byte;
108 int byte_count;
109 int bit_count;
110 __u32 crc = 0xffffffff;
111 u8 msb;
112
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;
120 }
121 byte >>= 1;
122 }
123 }
124 return (crc);
125 }
126
127 /*
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?).
136 */
137 static void fec_set_multicast_list(struct net_device *dev)
138 {
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;
148
149
150 if ((dev->flags & IFF_PROMISC) != 0) {
151
152 spin_lock_irqsave(&fep->lock, flags);
153 FS(fecp, r_cntrl, FEC_RCNTRL_PROM);
154 spin_unlock_irqrestore(&fep->lock, flags);
155
156 /*
157 * Log any net taps.
158 */
159 printk(KERN_WARNING DRV_MODULE_NAME
160 ": %s: Promiscuous mode enabled.\n", dev->name);
161 return;
162
163 }
164
165 if ((dev->flags & IFF_ALLMULTI) != 0 ||
166 dev->mc_count > FEC_MAX_MULTICAST_ADDRS) {
167 /*
168 * Catch all multicast addresses, set the filter to all 1's.
169 */
170 hthi = 0xffffffffU;
171 htlo = 0xffffffffU;
172 } else {
173 hthi = 0;
174 htlo = 0;
175
176 /*
177 * Now populate the hash table
178 */
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;
192 }
193 }
194
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);
200 }
201
202 static int fec_set_mac_address(struct net_device *dev, void *addr)
203 {
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;
210
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];
214
215 /*
216 * Set station address.
217 */
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);
224
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);
229
230 return 0;
231 }
232
233 /*
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.
237 */
238 void fec_restart(struct net_device *dev, int duplex, int speed)
239 {
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;
251
252 fec_whack_reset(fep->fecp);
253
254 /*
255 * Set station address.
256 */
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);
265
266 /*
267 * Reset all multicast.
268 */
269 FW(fecp, hash_table_high, 0);
270 FW(fecp, hash_table_low, 0);
271
272 /*
273 * Set maximum receive buffer size.
274 */
275 FW(fecp, r_buff_size, PKT_MAXBLR_SIZE);
276 FW(fecp, r_hash, PKT_MAXBUF_SIZE);
277
278 /*
279 * Set receive and transmit descriptor base.
280 */
281 FW(fecp, r_des_start, iopa((__u32) (fep->rx_bd_base)));
282 FW(fecp, x_des_start, iopa((__u32) (fep->tx_bd_base)));
283
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;
287
288 /*
289 * Reset SKB receive buffers
290 */
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);
296 }
297
298 /*
299 * Initialize the receive buffer descriptors.
300 */
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;
309 }
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));
318 }
319 /*
320 * if we failed, fillup remainder
321 */
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);
325 }
326
327 /*
328 * Reset SKB transmit buffers.
329 */
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);
335 }
336
337 /*
338 * ...and the same for transmit.
339 */
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);
345 }
346
347 /*
348 * Enable big endian and don't care about SDMA FC.
349 */
350 FW(fecp, fun_code, 0x78000000);
351
352 /*
353 * Set MII speed.
354 */
355 FW(fecp, mii_speed, fep->fec_phy_speed);
356
357 /*
358 * Clear any outstanding interrupt.
359 */
360 FW(fecp, ievent, 0xffc0);
361 FW(fecp, ivec, (fpi->fec_irq / 2) << 29);
362
363 /*
364 * adjust to speed (only for DUET & RMII)
365 */
366 #ifdef CONFIG_DUET
367 cptr = in_be32(&immap->im_cpm.cp_cptr);
368 switch (fpi->fec_no) {
369 case 0:
370 /*
371 * check if in RMII mode
372 */
373 if ((cptr & 0x100) == 0)
374 break;
375
376 if (speed == 10)
377 cptr |= 0x0000010;
378 else if (speed == 100)
379 cptr &= ~0x0000010;
380 break;
381 case 1:
382 /*
383 * check if in RMII mode
384 */
385 if ((cptr & 0x80) == 0)
386 break;
387
388 if (speed == 10)
389 cptr |= 0x0000008;
390 else if (speed == 100)
391 cptr &= ~0x0000008;
392 break;
393 default:
394 break;
395 }
396 out_be32(&immap->im_cpm.cp_cptr, cptr);
397 #endif
398
399 FW(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
400 /*
401 * adjust to duplex mode
402 */
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 */
409 }
410
411 /*
412 * Enable interrupts we wish to service.
413 */
414 FW(fecp, imask, FEC_ENET_TXF | FEC_ENET_TXB |
415 FEC_ENET_RXF | FEC_ENET_RXB);
416
417 /*
418 * And last, enable the transmit and receive processing.
419 */
420 FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
421 FW(fecp, r_des_active, 0x01000000);
422 }
423
424 void fec_stop(struct net_device *dev)
425 {
426 struct fec_enet_private *fep = netdev_priv(dev);
427 fec_t *fecp = fep->fecp;
428 struct sk_buff *skb;
429 int i;
430
431 if ((FR(fecp, ecntrl) & FEC_ECNTRL_ETHER_EN) == 0)
432 return; /* already down */
433
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);
438
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);
443 /*
444 * Disable FEC. Let only MII interrupts.
445 */
446 FW(fecp, imask, 0);
447 FW(fecp, ecntrl, ~FEC_ECNTRL_ETHER_EN);
448
449 /*
450 * Reset SKB transmit buffers.
451 */
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);
457 }
458
459 /*
460 * Reset SKB receive buffers
461 */
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);
467 }
468 }
469
470 /* common receive function */
471 static int fec_enet_rx_common(struct net_device *dev, int *budget)
472 {
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;
482
483 if (fpi->use_napi) {
484 rx_work_limit = min(dev->quota, *budget);
485
486 if (!netif_running(dev))
487 return 0;
488 }
489
490 /*
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.
493 */
494 bdp = fep->cur_rx;
495
496 /* clear RX status bits for napi*/
497 if (fpi->use_napi)
498 FW(fecp, ievent, FEC_ENET_RXF | FEC_ENET_RXB);
499
500 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
501
502 curidx = bdp - fep->rx_bd_base;
503
504 /*
505 * Since we have allocated space to hold a complete frame,
506 * the last indicator should be set.
507 */
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);
512
513 /*
514 * Check for errors.
515 */
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++;
531
532 skbn = fep->rx_skbuff[curidx];
533 BUG_ON(skbn == NULL);
534
535 } else {
536
537 /* napi, got packet but no quota */
538 if (fpi->use_napi && --rx_work_limit < 0)
539 break;
540
541 skb = fep->rx_skbuff[curidx];
542 BUG_ON(skb == NULL);
543
544 /*
545 * Process the incoming frame.
546 */
547 fep->stats.rx_packets++;
548 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
549 fep->stats.rx_bytes += pkt_len + 4;
550
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;
561 }
562 } else
563 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
564
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;
580 }
581 }
582
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);
589
590 /*
591 * Update BD pointer to next entry.
592 */
593 if ((sc & BD_ENET_RX_WRAP) == 0)
594 bdp++;
595 else
596 bdp = fep->rx_bd_base;
597
598 /*
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.
602 */
603 FW(fecp, r_des_active, 0x01000000);
604 }
605
606 fep->cur_rx = bdp;
607
608 if (fpi->use_napi) {
609 dev->quota -= received;
610 *budget -= received;
611
612 if (rx_work_limit < 0)
613 return 1; /* not done */
614
615 /* done */
616 netif_rx_complete(dev);
617
618 /* enable RX interrupt bits */
619 FS(fecp, imask, FEC_ENET_RXF | FEC_ENET_RXB);
620 }
621
622 return 0;
623 }
624
625 static void fec_enet_tx(struct net_device *dev)
626 {
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;
632
633 spin_lock(&fep->lock);
634 bdp = fep->dirty_tx;
635
636 do_wake = 0;
637 while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
638
639 dirtyidx = bdp - fep->tx_bd_base;
640
641 if (fep->tx_free == fep->tx_ring)
642 break;
643
644 skb = fep->tx_skbuff[dirtyidx];
645
646 /*
647 * Check for errors.
648 */
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++;
664
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);
669
670 /*
671 * Deferred means some collisions occurred during transmit,
672 * but we eventually sent the packet OK.
673 */
674 if (sc & BD_ENET_TX_DEF)
675 fep->stats.collisions++;
676
677 /*
678 * Free the sk buffer associated with this last transmit.
679 */
680 dev_kfree_skb_irq(skb);
681 fep->tx_skbuff[dirtyidx] = NULL;
682
683 /*
684 * Update pointer to next buffer descriptor to be transmitted.
685 */
686 if ((sc & BD_ENET_TX_WRAP) == 0)
687 bdp++;
688 else
689 bdp = fep->tx_bd_base;
690
691 /*
692 * Since we have freed up a buffer, the ring is no longer
693 * full.
694 */
695 if (!fep->tx_free++)
696 do_wake = 1;
697 }
698
699 fep->dirty_tx = bdp;
700
701 spin_unlock(&fep->lock);
702
703 if (do_wake && netif_queue_stopped(dev))
704 netif_wake_queue(dev);
705 }
706
707 /*
708 * The interrupt handler.
709 * This is called from the MPC core interrupt.
710 */
711 static irqreturn_t
712 fec_enet_interrupt(int irq, void *dev_id, struct pt_regs *regs)
713 {
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;
720
721 if (unlikely(dev == NULL))
722 return IRQ_NONE;
723
724 fep = netdev_priv(dev);
725 fecp = fep->fecp;
726 fpi = fep->fpi;
727
728 /*
729 * Get the interrupt events that caused us to be here.
730 */
731 while ((int_events = FR(fecp, ievent) & FR(fecp, imask)) != 0) {
732
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);
738 }
739
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);
745
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);
759 }
760 }
761 }
762
763 if ((int_events & FEC_ENET_TXF) != 0)
764 fec_enet_tx(dev);
765 }
766
767 return IRQ_HANDLED;
768 }
769
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)
773 {
774 struct net_device *dev = dev_id;
775 struct fec_enet_private *fep;
776 const struct fec_platform_info *fpi;
777
778 if (unlikely(dev == NULL))
779 return IRQ_NONE;
780
781 fep = netdev_priv(dev);
782 fpi = fep->fpi;
783
784 if (!fpi->use_mdio)
785 return IRQ_NONE;
786
787 /*
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().
793 */
794 if (!fep->phy)
795 return IRQ_NONE;
796
797 fec_mii_ack_int(dev);
798 fec_mii_link_status_change_check(dev, 0);
799
800 return IRQ_HANDLED;
801 }
802
803
804 /**********************************************************************************/
805
806 static int fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
807 {
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;
813
814 spin_lock_irqsave(&fep->tx_lock, flags);
815
816 /*
817 * Fill in a Tx ring entry
818 */
819 bdp = fep->cur_tx;
820
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);
824
825 /*
826 * Ooops. All transmit buffers are full. Bail out.
827 * This should not happen, since the tx queue should be stopped.
828 */
829 printk(KERN_WARNING DRV_MODULE_NAME
830 ": %s tx queue full!.\n", dev->name);
831 return 1;
832 }
833
834 curidx = bdp - fep->tx_bd_base;
835 /*
836 * Clear all of the status flags.
837 */
838 CBDC_SC(bdp, BD_ENET_TX_STATS);
839
840 /*
841 * Save skb pointer.
842 */
843 fep->tx_skbuff[curidx] = skb;
844
845 fep->stats.tx_bytes += skb->len;
846
847 /*
848 * Push the data cache so the CPM does not get stale memory data.
849 */
850 CBDW_BUFADDR(bdp, dma_map_single(NULL, skb->data,
851 skb->len, DMA_TO_DEVICE));
852 CBDW_DATLEN(bdp, skb->len);
853
854 dev->trans_start = jiffies;
855
856 /*
857 * If this was the last BD in the ring, start at the beginning again.
858 */
859 if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
860 fep->cur_tx++;
861 else
862 fep->cur_tx = fep->tx_bd_base;
863
864 if (!--fep->tx_free)
865 netif_stop_queue(dev);
866
867 /*
868 * Trigger transmission start
869 */
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);
873
874 spin_unlock_irqrestore(&fep->tx_lock, flags);
875
876 return 0;
877 }
878
879 static void fec_timeout(struct net_device *dev)
880 {
881 struct fec_enet_private *fep = netdev_priv(dev);
882
883 fep->stats.tx_errors++;
884
885 if (fep->tx_free)
886 netif_wake_queue(dev);
887
888 /* check link status again */
889 fec_mii_link_status_change_check(dev, 0);
890 }
891
892 static int fec_enet_open(struct net_device *dev)
893 {
894 struct fec_enet_private *fep = netdev_priv(dev);
895 const struct fec_platform_info *fpi = fep->fpi;
896 unsigned long flags;
897
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;
903 }
904
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;
913 }
914
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);
923
924 netif_carrier_on(dev);
925 netif_start_queue(dev);
926 }
927 return 0;
928 }
929
930 static int fec_enet_close(struct net_device *dev)
931 {
932 struct fec_enet_private *fep = netdev_priv(dev);
933 const struct fec_platform_info *fpi = fep->fpi;
934 unsigned long flags;
935
936 netif_stop_queue(dev);
937 netif_carrier_off(dev);
938
939 if (fpi->use_mdio)
940 fec_mii_shutdown(dev);
941
942 spin_lock_irqsave(&fep->lock, flags);
943 fec_stop(dev);
944 spin_unlock_irqrestore(&fep->lock, flags);
945
946 /* release any irqs */
947 if (fpi->phy_irq != -1)
948 free_irq(fpi->phy_irq, dev);
949 free_irq(fpi->fec_irq, dev);
950
951 return 0;
952 }
953
954 static struct net_device_stats *fec_enet_get_stats(struct net_device *dev)
955 {
956 struct fec_enet_private *fep = netdev_priv(dev);
957 return &fep->stats;
958 }
959
960 static int fec_enet_poll(struct net_device *dev, int *budget)
961 {
962 return fec_enet_rx_common(dev, budget);
963 }
964
965 /*************************************************************************/
966
967 static void fec_get_drvinfo(struct net_device *dev,
968 struct ethtool_drvinfo *info)
969 {
970 strcpy(info->driver, DRV_MODULE_NAME);
971 strcpy(info->version, DRV_MODULE_VERSION);
972 }
973
974 static int fec_get_regs_len(struct net_device *dev)
975 {
976 return sizeof(fec_t);
977 }
978
979 static void fec_get_regs(struct net_device *dev, struct ethtool_regs *regs,
980 void *p)
981 {
982 struct fec_enet_private *fep = netdev_priv(dev);
983 unsigned long flags;
984
985 if (regs->len < sizeof(fec_t))
986 return;
987
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);
992 }
993
994 static int fec_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
995 {
996 struct fec_enet_private *fep = netdev_priv(dev);
997 unsigned long flags;
998 int rc;
999
1000 spin_lock_irqsave(&fep->lock, flags);
1001 rc = mii_ethtool_gset(&fep->mii_if, cmd);
1002 spin_unlock_irqrestore(&fep->lock, flags);
1003
1004 return rc;
1005 }
1006
1007 static int fec_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1008 {
1009 struct fec_enet_private *fep = netdev_priv(dev);
1010 unsigned long flags;
1011 int rc;
1012
1013 spin_lock_irqsave(&fep->lock, flags);
1014 rc = mii_ethtool_sset(&fep->mii_if, cmd);
1015 spin_unlock_irqrestore(&fep->lock, flags);
1016
1017 return rc;
1018 }
1019
1020 static int fec_nway_reset(struct net_device *dev)
1021 {
1022 struct fec_enet_private *fep = netdev_priv(dev);
1023 return mii_nway_restart(&fep->mii_if);
1024 }
1025
1026 static __u32 fec_get_msglevel(struct net_device *dev)
1027 {
1028 struct fec_enet_private *fep = netdev_priv(dev);
1029 return fep->msg_enable;
1030 }
1031
1032 static void fec_set_msglevel(struct net_device *dev, __u32 value)
1033 {
1034 struct fec_enet_private *fep = netdev_priv(dev);
1035 fep->msg_enable = value;
1036 }
1037
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,
1052 };
1053
1054 static int fec_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1055 {
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;
1060
1061 if (!netif_running(dev))
1062 return -EINVAL;
1063
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;
1068 }
1069
1070 int fec_8xx_init_one(const struct fec_platform_info *fpi,
1071 struct net_device **devp)
1072 {
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;
1082
1083 *devp = NULL;
1084
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;
1096 }
1097
1098 if (fec_8xx_version_printed++ == 0)
1099 printk(KERN_INFO "%s", version);
1100
1101 i = sizeof(*fep) + (sizeof(struct sk_buff **) *
1102 (fpi->rx_ring + fpi->tx_ring));
1103
1104 dev = alloc_etherdev(i);
1105 if (!dev) {
1106 err = -ENOMEM;
1107 goto err;
1108 }
1109 SET_MODULE_OWNER(dev);
1110
1111 fep = netdev_priv(dev);
1112
1113 /* partial reset of FEC */
1114 fec_whack_reset(fecp);
1115
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;
1119
1120 fep->fecp = fecp;
1121 fep->fpi = fpi;
1122
1123 /* init locks */
1124 spin_lock_init(&fep->lock);
1125 spin_lock_init(&fep->tx_lock);
1126
1127 /*
1128 * Set the Ethernet address.
1129 */
1130 for (i = 0; i < 6; i++)
1131 dev->dev_addr[i] = fpi->macaddr[i];
1132
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;
1142 }
1143
1144 /*
1145 * Set receive and transmit descriptor base.
1146 */
1147 fep->rx_bd_base = fep->ring_base;
1148 fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1149
1150 /* initialize ring size variables */
1151 fep->tx_ring = fpi->tx_ring;
1152 fep->rx_ring = fpi->rx_ring;
1153
1154 /* SIU interrupt */
1155 if (fpi->phy_irq != -1 &&
1156 (fpi->phy_irq >= SIU_IRQ0 && fpi->phy_irq < SIU_LEVEL7)) {
1157
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);
1164 }
1165
1166 /*
1167 * The FEC Ethernet specific entries in the device structure.
1168 */
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;
1180 }
1181 dev->ethtool_ops = &fec_ethtool_ops;
1182 dev->do_ioctl = fec_ioctl;
1183
1184 fep->fec_phy_speed =
1185 ((((fpi->sys_clk + 4999999) / 2500000) / 2) & 0x3F) << 1;
1186
1187 init_timer(&fep->phy_timer_list);
1188
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);
1196
1197 netif_carrier_off(dev);
1198
1199 err = register_netdev(dev);
1200 if (err != 0)
1201 goto err;
1202 registered = 1;
1203
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);
1211 }
1212
1213 *devp = dev;
1214
1215 return 0;
1216
1217 err:
1218 if (dev != NULL) {
1219 if (fecp != NULL)
1220 fec_whack_reset(fecp);
1221
1222 if (registered)
1223 unregister_netdev(dev);
1224
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);
1232 }
1233 free_netdev(dev);
1234 }
1235 return err;
1236 }
1237
1238 int fec_8xx_cleanup_one(struct net_device *dev)
1239 {
1240 struct fec_enet_private *fep = netdev_priv(dev);
1241 fec_t *fecp = fep->fecp;
1242 const struct fec_platform_info *fpi = fep->fpi;
1243
1244 fec_whack_reset(fecp);
1245
1246 unregister_netdev(dev);
1247
1248 dma_free_coherent(NULL, (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t),
1249 fep->ring_base, fep->ring_mem_addr);
1250
1251 free_netdev(dev);
1252
1253 return 0;
1254 }
1255
1256 /**************************************************************************************/
1257 /**************************************************************************************/
1258 /**************************************************************************************/
1259
1260 static int __init fec_8xx_init(void)
1261 {
1262 return fec_8xx_platform_init();
1263 }
1264
1265 static void __exit fec_8xx_cleanup(void)
1266 {
1267 fec_8xx_platform_cleanup();
1268 }
1269
1270 /**************************************************************************************/
1271 /**************************************************************************************/
1272 /**************************************************************************************/
1273
1274 module_init(fec_8xx_init);
1275 module_exit(fec_8xx_cleanup);