blob: 698dba8f2aa1bb22189c171a90c9a972cbc249bf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#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/pci.h>
23#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/netdevice.h>
26#include <linux/etherdevice.h>
27#include <linux/skbuff.h>
28#include <linux/spinlock.h>
29#include <linux/mii.h>
30#include <linux/ethtool.h>
31#include <linux/bitops.h>
Rolf Eike Beerd6bd3a32006-09-29 01:59:48 -070032#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <asm/8xx_immap.h>
35#include <asm/pgtable.h>
36#include <asm/mpc8xx.h>
37#include <asm/irq.h>
38#include <asm/uaccess.h>
39#include <asm/commproc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include "fec_8xx.h"
42
43/*************************************************/
44
45#define FEC_MAX_MULTICAST_ADDRS 64
46
47/*************************************************/
48
49static char version[] __devinitdata =
50 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")" "\n";
51
52MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
53MODULE_DESCRIPTION("Motorola 8xx FEC ethernet driver");
54MODULE_LICENSE("GPL");
55
Rusty Russell8d3b33f2006-03-25 03:07:05 -080056int fec_8xx_debug = -1; /* -1 == use FEC_8XX_DEF_MSG_ENABLE as value */
57module_param(fec_8xx_debug, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058MODULE_PARM_DESC(fec_8xx_debug,
59 "FEC 8xx bitmapped debugging message enable value");
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/*************************************************/
63
64/*
65 * Delay to wait for FEC reset command to complete (in us)
66 */
67#define FEC_RESET_DELAY 50
68
69/*****************************************************************************************/
70
71static void fec_whack_reset(fec_t * fecp)
72{
73 int i;
74
75 /*
76 * Whack a reset. We should wait for this.
77 */
78 FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET);
79 for (i = 0;
80 (FR(fecp, ecntrl) & FEC_ECNTRL_RESET) != 0 && i < FEC_RESET_DELAY;
81 i++)
82 udelay(1);
83
84 if (i == FEC_RESET_DELAY)
85 printk(KERN_WARNING "FEC Reset timeout!\n");
86
87}
88
89/****************************************************************************/
90
91/*
92 * Transmitter timeout.
93 */
94#define TX_TIMEOUT (2*HZ)
95
96/****************************************************************************/
97
98/*
99 * Returns the CRC needed when filling in the hash table for
100 * multicast group filtering
101 * pAddr must point to a MAC address (6 bytes)
102 */
103static __u32 fec_mulicast_calc_crc(char *pAddr)
104{
105 u8 byte;
106 int byte_count;
107 int bit_count;
108 __u32 crc = 0xffffffff;
109 u8 msb;
110
111 for (byte_count = 0; byte_count < 6; byte_count++) {
112 byte = pAddr[byte_count];
113 for (bit_count = 0; bit_count < 8; bit_count++) {
114 msb = crc >> 31;
115 crc <<= 1;
116 if (msb ^ (byte & 0x1)) {
117 crc ^= FEC_CRC_POLY;
118 }
119 byte >>= 1;
120 }
121 }
122 return (crc);
123}
124
125/*
126 * Set or clear the multicast filter for this adaptor.
127 * Skeleton taken from sunlance driver.
128 * The CPM Ethernet implementation allows Multicast as well as individual
129 * MAC address filtering. Some of the drivers check to make sure it is
130 * a group multicast address, and discard those that are not. I guess I
131 * will do the same for now, but just remove the test if you want
132 * individual filtering as well (do the upper net layers want or support
133 * this kind of feature?).
134 */
135static void fec_set_multicast_list(struct net_device *dev)
136{
137 struct fec_enet_private *fep = netdev_priv(dev);
138 fec_t *fecp = fep->fecp;
139 struct dev_mc_list *pmc;
140 __u32 crc;
141 int temp;
142 __u32 csrVal;
143 int hash_index;
144 __u32 hthi, htlo;
145 unsigned long flags;
146
147
148 if ((dev->flags & IFF_PROMISC) != 0) {
149
150 spin_lock_irqsave(&fep->lock, flags);
151 FS(fecp, r_cntrl, FEC_RCNTRL_PROM);
152 spin_unlock_irqrestore(&fep->lock, flags);
153
154 /*
155 * Log any net taps.
156 */
157 printk(KERN_WARNING DRV_MODULE_NAME
158 ": %s: Promiscuous mode enabled.\n", dev->name);
159 return;
160
161 }
162
163 if ((dev->flags & IFF_ALLMULTI) != 0 ||
164 dev->mc_count > FEC_MAX_MULTICAST_ADDRS) {
165 /*
166 * Catch all multicast addresses, set the filter to all 1's.
167 */
168 hthi = 0xffffffffU;
169 htlo = 0xffffffffU;
170 } else {
171 hthi = 0;
172 htlo = 0;
173
174 /*
175 * Now populate the hash table
176 */
177 for (pmc = dev->mc_list; pmc != NULL; pmc = pmc->next) {
178 crc = fec_mulicast_calc_crc(pmc->dmi_addr);
179 temp = (crc & 0x3f) >> 1;
180 hash_index = ((temp & 0x01) << 4) |
181 ((temp & 0x02) << 2) |
182 ((temp & 0x04)) |
183 ((temp & 0x08) >> 2) |
184 ((temp & 0x10) >> 4);
185 csrVal = (1 << hash_index);
186 if (crc & 1)
187 hthi |= csrVal;
188 else
189 htlo |= csrVal;
190 }
191 }
192
193 spin_lock_irqsave(&fep->lock, flags);
194 FC(fecp, r_cntrl, FEC_RCNTRL_PROM);
195 FW(fecp, hash_table_high, hthi);
196 FW(fecp, hash_table_low, htlo);
197 spin_unlock_irqrestore(&fep->lock, flags);
198}
199
200static int fec_set_mac_address(struct net_device *dev, void *addr)
201{
202 struct sockaddr *mac = addr;
203 struct fec_enet_private *fep = netdev_priv(dev);
204 struct fec *fecp = fep->fecp;
205 int i;
206 __u32 addrhi, addrlo;
207 unsigned long flags;
208
209 /* Get pointer to SCC area in parameter RAM. */
210 for (i = 0; i < 6; i++)
211 dev->dev_addr[i] = mac->sa_data[i];
212
213 /*
214 * Set station address.
215 */
216 addrhi = ((__u32) dev->dev_addr[0] << 24) |
217 ((__u32) dev->dev_addr[1] << 16) |
218 ((__u32) dev->dev_addr[2] << 8) |
219 (__u32) dev->dev_addr[3];
220 addrlo = ((__u32) dev->dev_addr[4] << 24) |
221 ((__u32) dev->dev_addr[5] << 16);
222
223 spin_lock_irqsave(&fep->lock, flags);
224 FW(fecp, addr_low, addrhi);
225 FW(fecp, addr_high, addrlo);
226 spin_unlock_irqrestore(&fep->lock, flags);
227
228 return 0;
229}
230
231/*
232 * This function is called to start or restart the FEC during a link
233 * change. This only happens when switching between half and full
234 * duplex.
235 */
236void fec_restart(struct net_device *dev, int duplex, int speed)
237{
238#ifdef CONFIG_DUET
239 immap_t *immap = (immap_t *) IMAP_ADDR;
240 __u32 cptr;
241#endif
242 struct fec_enet_private *fep = netdev_priv(dev);
243 struct fec *fecp = fep->fecp;
244 const struct fec_platform_info *fpi = fep->fpi;
245 cbd_t *bdp;
246 struct sk_buff *skb;
247 int i;
248 __u32 addrhi, addrlo;
249
250 fec_whack_reset(fep->fecp);
251
252 /*
253 * Set station address.
254 */
255 addrhi = ((__u32) dev->dev_addr[0] << 24) |
256 ((__u32) dev->dev_addr[1] << 16) |
257 ((__u32) dev->dev_addr[2] << 8) |
258 (__u32) dev->dev_addr[3];
259 addrlo = ((__u32) dev->dev_addr[4] << 24) |
260 ((__u32) dev->dev_addr[5] << 16);
261 FW(fecp, addr_low, addrhi);
262 FW(fecp, addr_high, addrlo);
263
264 /*
265 * Reset all multicast.
266 */
267 FW(fecp, hash_table_high, 0);
268 FW(fecp, hash_table_low, 0);
269
270 /*
271 * Set maximum receive buffer size.
272 */
273 FW(fecp, r_buff_size, PKT_MAXBLR_SIZE);
274 FW(fecp, r_hash, PKT_MAXBUF_SIZE);
275
276 /*
277 * Set receive and transmit descriptor base.
278 */
279 FW(fecp, r_des_start, iopa((__u32) (fep->rx_bd_base)));
280 FW(fecp, x_des_start, iopa((__u32) (fep->tx_bd_base)));
281
282 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
283 fep->tx_free = fep->tx_ring;
284 fep->cur_rx = fep->rx_bd_base;
285
286 /*
287 * Reset SKB receive buffers
288 */
289 for (i = 0; i < fep->rx_ring; i++) {
290 if ((skb = fep->rx_skbuff[i]) == NULL)
291 continue;
292 fep->rx_skbuff[i] = NULL;
293 dev_kfree_skb(skb);
294 }
295
296 /*
297 * Initialize the receive buffer descriptors.
298 */
299 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
300 skb = dev_alloc_skb(ENET_RX_FRSIZE);
301 if (skb == NULL) {
302 printk(KERN_WARNING DRV_MODULE_NAME
303 ": %s Memory squeeze, unable to allocate skb\n",
304 dev->name);
305 fep->stats.rx_dropped++;
306 break;
307 }
308 fep->rx_skbuff[i] = skb;
309 skb->dev = dev;
310 CBDW_BUFADDR(bdp, dma_map_single(NULL, skb->data,
311 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
312 DMA_FROM_DEVICE));
313 CBDW_DATLEN(bdp, 0); /* zero */
314 CBDW_SC(bdp, BD_ENET_RX_EMPTY |
315 ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
316 }
317 /*
318 * if we failed, fillup remainder
319 */
320 for (; i < fep->rx_ring; i++, bdp++) {
321 fep->rx_skbuff[i] = NULL;
322 CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
323 }
324
325 /*
326 * Reset SKB transmit buffers.
327 */
328 for (i = 0; i < fep->tx_ring; i++) {
329 if ((skb = fep->tx_skbuff[i]) == NULL)
330 continue;
331 fep->tx_skbuff[i] = NULL;
332 dev_kfree_skb(skb);
333 }
334
335 /*
336 * ...and the same for transmit.
337 */
338 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
339 fep->tx_skbuff[i] = NULL;
340 CBDW_BUFADDR(bdp, virt_to_bus(NULL));
341 CBDW_DATLEN(bdp, 0);
342 CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
343 }
344
345 /*
346 * Enable big endian and don't care about SDMA FC.
347 */
348 FW(fecp, fun_code, 0x78000000);
349
350 /*
351 * Set MII speed.
352 */
353 FW(fecp, mii_speed, fep->fec_phy_speed);
354
355 /*
356 * Clear any outstanding interrupt.
357 */
358 FW(fecp, ievent, 0xffc0);
359 FW(fecp, ivec, (fpi->fec_irq / 2) << 29);
360
361 /*
362 * adjust to speed (only for DUET & RMII)
363 */
364#ifdef CONFIG_DUET
365 cptr = in_be32(&immap->im_cpm.cp_cptr);
366 switch (fpi->fec_no) {
367 case 0:
368 /*
369 * check if in RMII mode
370 */
371 if ((cptr & 0x100) == 0)
372 break;
373
374 if (speed == 10)
375 cptr |= 0x0000010;
376 else if (speed == 100)
377 cptr &= ~0x0000010;
378 break;
379 case 1:
380 /*
381 * check if in RMII mode
382 */
383 if ((cptr & 0x80) == 0)
384 break;
385
386 if (speed == 10)
387 cptr |= 0x0000008;
388 else if (speed == 100)
389 cptr &= ~0x0000008;
390 break;
391 default:
392 break;
393 }
394 out_be32(&immap->im_cpm.cp_cptr, cptr);
395#endif
396
397 FW(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
398 /*
399 * adjust to duplex mode
400 */
401 if (duplex) {
402 FC(fecp, r_cntrl, FEC_RCNTRL_DRT);
403 FS(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD enable */
404 } else {
405 FS(fecp, r_cntrl, FEC_RCNTRL_DRT);
406 FC(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD disable */
407 }
408
409 /*
410 * Enable interrupts we wish to service.
411 */
412 FW(fecp, imask, FEC_ENET_TXF | FEC_ENET_TXB |
413 FEC_ENET_RXF | FEC_ENET_RXB);
414
415 /*
416 * And last, enable the transmit and receive processing.
417 */
418 FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
419 FW(fecp, r_des_active, 0x01000000);
420}
421
422void fec_stop(struct net_device *dev)
423{
424 struct fec_enet_private *fep = netdev_priv(dev);
425 fec_t *fecp = fep->fecp;
426 struct sk_buff *skb;
427 int i;
428
429 if ((FR(fecp, ecntrl) & FEC_ECNTRL_ETHER_EN) == 0)
430 return; /* already down */
431
432 FW(fecp, x_cntrl, 0x01); /* Graceful transmit stop */
433 for (i = 0; ((FR(fecp, ievent) & 0x10000000) == 0) &&
434 i < FEC_RESET_DELAY; i++)
435 udelay(1);
436
437 if (i == FEC_RESET_DELAY)
438 printk(KERN_WARNING DRV_MODULE_NAME
439 ": %s FEC timeout on graceful transmit stop\n",
440 dev->name);
441 /*
442 * Disable FEC. Let only MII interrupts.
443 */
444 FW(fecp, imask, 0);
445 FW(fecp, ecntrl, ~FEC_ECNTRL_ETHER_EN);
446
447 /*
448 * Reset SKB transmit buffers.
449 */
450 for (i = 0; i < fep->tx_ring; i++) {
451 if ((skb = fep->tx_skbuff[i]) == NULL)
452 continue;
453 fep->tx_skbuff[i] = NULL;
454 dev_kfree_skb(skb);
455 }
456
457 /*
458 * Reset SKB receive buffers
459 */
460 for (i = 0; i < fep->rx_ring; i++) {
461 if ((skb = fep->rx_skbuff[i]) == NULL)
462 continue;
463 fep->rx_skbuff[i] = NULL;
464 dev_kfree_skb(skb);
465 }
466}
467
468/* common receive function */
469static int fec_enet_rx_common(struct net_device *dev, int *budget)
470{
471 struct fec_enet_private *fep = netdev_priv(dev);
472 fec_t *fecp = fep->fecp;
473 const struct fec_platform_info *fpi = fep->fpi;
474 cbd_t *bdp;
475 struct sk_buff *skb, *skbn, *skbt;
476 int received = 0;
477 __u16 pkt_len, sc;
478 int curidx;
479 int rx_work_limit;
480
481 if (fpi->use_napi) {
482 rx_work_limit = min(dev->quota, *budget);
483
484 if (!netif_running(dev))
485 return 0;
486 }
487
488 /*
489 * First, grab all of the stats for the incoming packet.
490 * These get messed up if we get called due to a busy condition.
491 */
492 bdp = fep->cur_rx;
493
494 /* clear RX status bits for napi*/
495 if (fpi->use_napi)
496 FW(fecp, ievent, FEC_ENET_RXF | FEC_ENET_RXB);
497
498 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
499
500 curidx = bdp - fep->rx_bd_base;
501
502 /*
503 * Since we have allocated space to hold a complete frame,
504 * the last indicator should be set.
505 */
506 if ((sc & BD_ENET_RX_LAST) == 0)
507 printk(KERN_WARNING DRV_MODULE_NAME
508 ": %s rcv is not +last\n",
509 dev->name);
510
511 /*
512 * Check for errors.
513 */
514 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
515 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
516 fep->stats.rx_errors++;
517 /* Frame too long or too short. */
518 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
519 fep->stats.rx_length_errors++;
520 /* Frame alignment */
521 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
522 fep->stats.rx_frame_errors++;
523 /* CRC Error */
524 if (sc & BD_ENET_RX_CR)
525 fep->stats.rx_crc_errors++;
526 /* FIFO overrun */
527 if (sc & BD_ENET_RX_OV)
528 fep->stats.rx_crc_errors++;
529
530 skbn = fep->rx_skbuff[curidx];
531 BUG_ON(skbn == NULL);
532
533 } else {
534
535 /* napi, got packet but no quota */
536 if (fpi->use_napi && --rx_work_limit < 0)
537 break;
538
539 skb = fep->rx_skbuff[curidx];
540 BUG_ON(skb == NULL);
541
542 /*
543 * Process the incoming frame.
544 */
545 fep->stats.rx_packets++;
546 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
547 fep->stats.rx_bytes += pkt_len + 4;
548
549 if (pkt_len <= fpi->rx_copybreak) {
550 /* +2 to make IP header L1 cache aligned */
551 skbn = dev_alloc_skb(pkt_len + 2);
552 if (skbn != NULL) {
553 skb_reserve(skbn, 2); /* align IP header */
554 memcpy(skbn->data, skb->data, pkt_len);
555 /* swap */
556 skbt = skb;
557 skb = skbn;
558 skbn = skbt;
559 }
560 } else
561 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
562
563 if (skbn != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 skb_put(skb, pkt_len); /* Make room */
565 skb->protocol = eth_type_trans(skb, dev);
566 received++;
567 if (!fpi->use_napi)
568 netif_rx(skb);
569 else
570 netif_receive_skb(skb);
571 } else {
572 printk(KERN_WARNING DRV_MODULE_NAME
573 ": %s Memory squeeze, dropping packet.\n",
574 dev->name);
575 fep->stats.rx_dropped++;
576 skbn = skb;
577 }
578 }
579
580 fep->rx_skbuff[curidx] = skbn;
581 CBDW_BUFADDR(bdp, dma_map_single(NULL, skbn->data,
582 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
583 DMA_FROM_DEVICE));
584 CBDW_DATLEN(bdp, 0);
585 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
586
587 /*
588 * Update BD pointer to next entry.
589 */
590 if ((sc & BD_ENET_RX_WRAP) == 0)
591 bdp++;
592 else
593 bdp = fep->rx_bd_base;
594
595 /*
596 * Doing this here will keep the FEC running while we process
597 * incoming frames. On a heavily loaded network, we should be
598 * able to keep up at the expense of system resources.
599 */
600 FW(fecp, r_des_active, 0x01000000);
601 }
602
603 fep->cur_rx = bdp;
604
605 if (fpi->use_napi) {
606 dev->quota -= received;
607 *budget -= received;
608
609 if (rx_work_limit < 0)
610 return 1; /* not done */
611
612 /* done */
613 netif_rx_complete(dev);
614
615 /* enable RX interrupt bits */
616 FS(fecp, imask, FEC_ENET_RXF | FEC_ENET_RXB);
617 }
618
619 return 0;
620}
621
622static void fec_enet_tx(struct net_device *dev)
623{
624 struct fec_enet_private *fep = netdev_priv(dev);
625 cbd_t *bdp;
626 struct sk_buff *skb;
627 int dirtyidx, do_wake;
628 __u16 sc;
629
630 spin_lock(&fep->lock);
631 bdp = fep->dirty_tx;
632
633 do_wake = 0;
634 while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
635
636 dirtyidx = bdp - fep->tx_bd_base;
637
638 if (fep->tx_free == fep->tx_ring)
639 break;
640
641 skb = fep->tx_skbuff[dirtyidx];
642
643 /*
644 * Check for errors.
645 */
646 if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
647 BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
648 fep->stats.tx_errors++;
649 if (sc & BD_ENET_TX_HB) /* No heartbeat */
650 fep->stats.tx_heartbeat_errors++;
651 if (sc & BD_ENET_TX_LC) /* Late collision */
652 fep->stats.tx_window_errors++;
653 if (sc & BD_ENET_TX_RL) /* Retrans limit */
654 fep->stats.tx_aborted_errors++;
655 if (sc & BD_ENET_TX_UN) /* Underrun */
656 fep->stats.tx_fifo_errors++;
657 if (sc & BD_ENET_TX_CSL) /* Carrier lost */
658 fep->stats.tx_carrier_errors++;
659 } else
660 fep->stats.tx_packets++;
661
662 if (sc & BD_ENET_TX_READY)
663 printk(KERN_WARNING DRV_MODULE_NAME
664 ": %s HEY! Enet xmit interrupt and TX_READY.\n",
665 dev->name);
666
667 /*
668 * Deferred means some collisions occurred during transmit,
669 * but we eventually sent the packet OK.
670 */
671 if (sc & BD_ENET_TX_DEF)
672 fep->stats.collisions++;
673
674 /*
675 * Free the sk buffer associated with this last transmit.
676 */
677 dev_kfree_skb_irq(skb);
678 fep->tx_skbuff[dirtyidx] = NULL;
679
680 /*
681 * Update pointer to next buffer descriptor to be transmitted.
682 */
683 if ((sc & BD_ENET_TX_WRAP) == 0)
684 bdp++;
685 else
686 bdp = fep->tx_bd_base;
687
688 /*
689 * Since we have freed up a buffer, the ring is no longer
690 * full.
691 */
692 if (!fep->tx_free++)
693 do_wake = 1;
694 }
695
696 fep->dirty_tx = bdp;
697
698 spin_unlock(&fep->lock);
699
700 if (do_wake && netif_queue_stopped(dev))
701 netif_wake_queue(dev);
702}
703
704/*
705 * The interrupt handler.
706 * This is called from the MPC core interrupt.
707 */
708static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100709fec_enet_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 struct net_device *dev = dev_id;
712 struct fec_enet_private *fep;
713 const struct fec_platform_info *fpi;
714 fec_t *fecp;
715 __u32 int_events;
716 __u32 int_events_napi;
717
718 if (unlikely(dev == NULL))
719 return IRQ_NONE;
720
721 fep = netdev_priv(dev);
722 fecp = fep->fecp;
723 fpi = fep->fpi;
724
725 /*
726 * Get the interrupt events that caused us to be here.
727 */
728 while ((int_events = FR(fecp, ievent) & FR(fecp, imask)) != 0) {
729
730 if (!fpi->use_napi)
731 FW(fecp, ievent, int_events);
732 else {
733 int_events_napi = int_events & ~(FEC_ENET_RXF | FEC_ENET_RXB);
734 FW(fecp, ievent, int_events_napi);
735 }
736
737 if ((int_events & (FEC_ENET_HBERR | FEC_ENET_BABR |
738 FEC_ENET_BABT | FEC_ENET_EBERR)) != 0)
739 printk(KERN_WARNING DRV_MODULE_NAME
740 ": %s FEC ERROR(s) 0x%x\n",
741 dev->name, int_events);
742
743 if ((int_events & FEC_ENET_RXF) != 0) {
744 if (!fpi->use_napi)
745 fec_enet_rx_common(dev, NULL);
746 else {
747 if (netif_rx_schedule_prep(dev)) {
748 /* disable rx interrupts */
749 FC(fecp, imask, FEC_ENET_RXF | FEC_ENET_RXB);
750 __netif_rx_schedule(dev);
751 } else {
752 printk(KERN_ERR DRV_MODULE_NAME
753 ": %s driver bug! interrupt while in poll!\n",
754 dev->name);
755 FC(fecp, imask, FEC_ENET_RXF | FEC_ENET_RXB);
756 }
757 }
758 }
759
760 if ((int_events & FEC_ENET_TXF) != 0)
761 fec_enet_tx(dev);
762 }
763
764 return IRQ_HANDLED;
765}
766
767/* This interrupt occurs when the PHY detects a link change. */
768static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100769fec_mii_link_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 struct net_device *dev = dev_id;
772 struct fec_enet_private *fep;
773 const struct fec_platform_info *fpi;
774
775 if (unlikely(dev == NULL))
776 return IRQ_NONE;
777
778 fep = netdev_priv(dev);
779 fpi = fep->fpi;
780
781 if (!fpi->use_mdio)
782 return IRQ_NONE;
783
784 /*
785 * Acknowledge the interrupt if possible. If we have not
786 * found the PHY yet we can't process or acknowledge the
787 * interrupt now. Instead we ignore this interrupt for now,
788 * which we can do since it is edge triggered. It will be
789 * acknowledged later by fec_enet_open().
790 */
791 if (!fep->phy)
792 return IRQ_NONE;
793
794 fec_mii_ack_int(dev);
795 fec_mii_link_status_change_check(dev, 0);
796
797 return IRQ_HANDLED;
798}
799
800
801/**********************************************************************************/
802
803static int fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
804{
805 struct fec_enet_private *fep = netdev_priv(dev);
806 fec_t *fecp = fep->fecp;
807 cbd_t *bdp;
808 int curidx;
809 unsigned long flags;
810
811 spin_lock_irqsave(&fep->tx_lock, flags);
812
813 /*
814 * Fill in a Tx ring entry
815 */
816 bdp = fep->cur_tx;
817
818 if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
819 netif_stop_queue(dev);
820 spin_unlock_irqrestore(&fep->tx_lock, flags);
821
822 /*
823 * Ooops. All transmit buffers are full. Bail out.
824 * This should not happen, since the tx queue should be stopped.
825 */
826 printk(KERN_WARNING DRV_MODULE_NAME
827 ": %s tx queue full!.\n", dev->name);
828 return 1;
829 }
830
831 curidx = bdp - fep->tx_bd_base;
832 /*
833 * Clear all of the status flags.
834 */
835 CBDC_SC(bdp, BD_ENET_TX_STATS);
836
837 /*
838 * Save skb pointer.
839 */
840 fep->tx_skbuff[curidx] = skb;
841
842 fep->stats.tx_bytes += skb->len;
843
844 /*
845 * Push the data cache so the CPM does not get stale memory data.
846 */
847 CBDW_BUFADDR(bdp, dma_map_single(NULL, skb->data,
848 skb->len, DMA_TO_DEVICE));
849 CBDW_DATLEN(bdp, skb->len);
850
851 dev->trans_start = jiffies;
852
853 /*
854 * If this was the last BD in the ring, start at the beginning again.
855 */
856 if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
857 fep->cur_tx++;
858 else
859 fep->cur_tx = fep->tx_bd_base;
860
861 if (!--fep->tx_free)
862 netif_stop_queue(dev);
863
864 /*
865 * Trigger transmission start
866 */
867 CBDS_SC(bdp, BD_ENET_TX_READY | BD_ENET_TX_INTR |
868 BD_ENET_TX_LAST | BD_ENET_TX_TC);
869 FW(fecp, x_des_active, 0x01000000);
870
871 spin_unlock_irqrestore(&fep->tx_lock, flags);
872
873 return 0;
874}
875
876static void fec_timeout(struct net_device *dev)
877{
878 struct fec_enet_private *fep = netdev_priv(dev);
879
880 fep->stats.tx_errors++;
881
882 if (fep->tx_free)
883 netif_wake_queue(dev);
884
885 /* check link status again */
886 fec_mii_link_status_change_check(dev, 0);
887}
888
889static int fec_enet_open(struct net_device *dev)
890{
891 struct fec_enet_private *fep = netdev_priv(dev);
892 const struct fec_platform_info *fpi = fep->fpi;
893 unsigned long flags;
894
895 /* Install our interrupt handler. */
896 if (request_irq(fpi->fec_irq, fec_enet_interrupt, 0, "fec", dev) != 0) {
897 printk(KERN_ERR DRV_MODULE_NAME
898 ": %s Could not allocate FEC IRQ!", dev->name);
899 return -EINVAL;
900 }
901
902 /* Install our phy interrupt handler */
903 if (fpi->phy_irq != -1 &&
904 request_irq(fpi->phy_irq, fec_mii_link_interrupt, 0, "fec-phy",
905 dev) != 0) {
906 printk(KERN_ERR DRV_MODULE_NAME
907 ": %s Could not allocate PHY IRQ!", dev->name);
908 free_irq(fpi->fec_irq, dev);
909 return -EINVAL;
910 }
911
912 if (fpi->use_mdio) {
913 fec_mii_startup(dev);
914 netif_carrier_off(dev);
915 fec_mii_link_status_change_check(dev, 1);
916 } else {
917 spin_lock_irqsave(&fep->lock, flags);
918 fec_restart(dev, 1, 100); /* XXX this sucks */
919 spin_unlock_irqrestore(&fep->lock, flags);
920
921 netif_carrier_on(dev);
922 netif_start_queue(dev);
923 }
924 return 0;
925}
926
927static int fec_enet_close(struct net_device *dev)
928{
929 struct fec_enet_private *fep = netdev_priv(dev);
930 const struct fec_platform_info *fpi = fep->fpi;
931 unsigned long flags;
932
933 netif_stop_queue(dev);
934 netif_carrier_off(dev);
935
936 if (fpi->use_mdio)
937 fec_mii_shutdown(dev);
938
939 spin_lock_irqsave(&fep->lock, flags);
940 fec_stop(dev);
941 spin_unlock_irqrestore(&fep->lock, flags);
942
943 /* release any irqs */
944 if (fpi->phy_irq != -1)
945 free_irq(fpi->phy_irq, dev);
946 free_irq(fpi->fec_irq, dev);
947
948 return 0;
949}
950
951static struct net_device_stats *fec_enet_get_stats(struct net_device *dev)
952{
953 struct fec_enet_private *fep = netdev_priv(dev);
954 return &fep->stats;
955}
956
957static int fec_enet_poll(struct net_device *dev, int *budget)
958{
959 return fec_enet_rx_common(dev, budget);
960}
961
962/*************************************************************************/
963
964static void fec_get_drvinfo(struct net_device *dev,
965 struct ethtool_drvinfo *info)
966{
967 strcpy(info->driver, DRV_MODULE_NAME);
968 strcpy(info->version, DRV_MODULE_VERSION);
969}
970
971static int fec_get_regs_len(struct net_device *dev)
972{
973 return sizeof(fec_t);
974}
975
976static void fec_get_regs(struct net_device *dev, struct ethtool_regs *regs,
977 void *p)
978{
979 struct fec_enet_private *fep = netdev_priv(dev);
980 unsigned long flags;
981
982 if (regs->len < sizeof(fec_t))
983 return;
984
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);
989}
990
991static int fec_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
992{
993 struct fec_enet_private *fep = netdev_priv(dev);
994 unsigned long flags;
995 int rc;
996
997 spin_lock_irqsave(&fep->lock, flags);
998 rc = mii_ethtool_gset(&fep->mii_if, cmd);
999 spin_unlock_irqrestore(&fep->lock, flags);
1000
1001 return rc;
1002}
1003
1004static int fec_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1005{
1006 struct fec_enet_private *fep = netdev_priv(dev);
1007 unsigned long flags;
1008 int rc;
1009
1010 spin_lock_irqsave(&fep->lock, flags);
1011 rc = mii_ethtool_sset(&fep->mii_if, cmd);
1012 spin_unlock_irqrestore(&fep->lock, flags);
1013
1014 return rc;
1015}
1016
1017static int fec_nway_reset(struct net_device *dev)
1018{
1019 struct fec_enet_private *fep = netdev_priv(dev);
1020 return mii_nway_restart(&fep->mii_if);
1021}
1022
1023static __u32 fec_get_msglevel(struct net_device *dev)
1024{
1025 struct fec_enet_private *fep = netdev_priv(dev);
1026 return fep->msg_enable;
1027}
1028
1029static void fec_set_msglevel(struct net_device *dev, __u32 value)
1030{
1031 struct fec_enet_private *fep = netdev_priv(dev);
1032 fep->msg_enable = value;
1033}
1034
Jeff Garzik7282d492006-09-13 14:30:00 -04001035static 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 .get_tx_csum = ethtool_op_get_tx_csum,
1045 .set_tx_csum = ethtool_op_set_tx_csum, /* local! */
1046 .get_sg = ethtool_op_get_sg,
1047 .set_sg = ethtool_op_set_sg,
1048 .get_regs = fec_get_regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049};
1050
1051static int fec_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1052{
1053 struct fec_enet_private *fep = netdev_priv(dev);
1054 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data;
1055 unsigned long flags;
1056 int rc;
1057
1058 if (!netif_running(dev))
1059 return -EINVAL;
1060
1061 spin_lock_irqsave(&fep->lock, flags);
1062 rc = generic_mii_ioctl(&fep->mii_if, mii, cmd, NULL);
1063 spin_unlock_irqrestore(&fep->lock, flags);
1064 return rc;
1065}
1066
1067int fec_8xx_init_one(const struct fec_platform_info *fpi,
1068 struct net_device **devp)
1069{
1070 immap_t *immap = (immap_t *) IMAP_ADDR;
1071 static int fec_8xx_version_printed = 0;
1072 struct net_device *dev = NULL;
1073 struct fec_enet_private *fep = NULL;
1074 fec_t *fecp = NULL;
1075 int i;
1076 int err = 0;
1077 int registered = 0;
1078 __u32 siel;
1079
1080 *devp = NULL;
1081
1082 switch (fpi->fec_no) {
1083 case 0:
1084 fecp = &((immap_t *) IMAP_ADDR)->im_cpm.cp_fec;
1085 break;
1086#ifdef CONFIG_DUET
1087 case 1:
1088 fecp = &((immap_t *) IMAP_ADDR)->im_cpm.cp_fec2;
1089 break;
1090#endif
1091 default:
1092 return -EINVAL;
1093 }
1094
1095 if (fec_8xx_version_printed++ == 0)
1096 printk(KERN_INFO "%s", version);
1097
1098 i = sizeof(*fep) + (sizeof(struct sk_buff **) *
1099 (fpi->rx_ring + fpi->tx_ring));
1100
1101 dev = alloc_etherdev(i);
1102 if (!dev) {
1103 err = -ENOMEM;
1104 goto err;
1105 }
1106 SET_MODULE_OWNER(dev);
1107
1108 fep = netdev_priv(dev);
1109
1110 /* partial reset of FEC */
1111 fec_whack_reset(fecp);
1112
1113 /* point rx_skbuff, tx_skbuff */
1114 fep->rx_skbuff = (struct sk_buff **)&fep[1];
1115 fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
1116
1117 fep->fecp = fecp;
1118 fep->fpi = fpi;
1119
1120 /* init locks */
1121 spin_lock_init(&fep->lock);
1122 spin_lock_init(&fep->tx_lock);
1123
1124 /*
1125 * Set the Ethernet address.
1126 */
1127 for (i = 0; i < 6; i++)
1128 dev->dev_addr[i] = fpi->macaddr[i];
1129
1130 fep->ring_base = dma_alloc_coherent(NULL,
1131 (fpi->tx_ring + fpi->rx_ring) *
1132 sizeof(cbd_t), &fep->ring_mem_addr,
1133 GFP_KERNEL);
1134 if (fep->ring_base == NULL) {
1135 printk(KERN_ERR DRV_MODULE_NAME
1136 ": %s dma alloc failed.\n", dev->name);
1137 err = -ENOMEM;
1138 goto err;
1139 }
1140
1141 /*
1142 * Set receive and transmit descriptor base.
1143 */
1144 fep->rx_bd_base = fep->ring_base;
1145 fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1146
1147 /* initialize ring size variables */
1148 fep->tx_ring = fpi->tx_ring;
1149 fep->rx_ring = fpi->rx_ring;
1150
1151 /* SIU interrupt */
1152 if (fpi->phy_irq != -1 &&
1153 (fpi->phy_irq >= SIU_IRQ0 && fpi->phy_irq < SIU_LEVEL7)) {
1154
1155 siel = in_be32(&immap->im_siu_conf.sc_siel);
1156 if ((fpi->phy_irq & 1) == 0)
1157 siel |= (0x80000000 >> fpi->phy_irq);
1158 else
1159 siel &= ~(0x80000000 >> (fpi->phy_irq & ~1));
1160 out_be32(&immap->im_siu_conf.sc_siel, siel);
1161 }
1162
1163 /*
1164 * The FEC Ethernet specific entries in the device structure.
1165 */
1166 dev->open = fec_enet_open;
1167 dev->hard_start_xmit = fec_enet_start_xmit;
1168 dev->tx_timeout = fec_timeout;
1169 dev->watchdog_timeo = TX_TIMEOUT;
1170 dev->stop = fec_enet_close;
1171 dev->get_stats = fec_enet_get_stats;
1172 dev->set_multicast_list = fec_set_multicast_list;
1173 dev->set_mac_address = fec_set_mac_address;
1174 if (fpi->use_napi) {
1175 dev->poll = fec_enet_poll;
1176 dev->weight = fpi->napi_weight;
1177 }
1178 dev->ethtool_ops = &fec_ethtool_ops;
1179 dev->do_ioctl = fec_ioctl;
1180
1181 fep->fec_phy_speed =
1182 ((((fpi->sys_clk + 4999999) / 2500000) / 2) & 0x3F) << 1;
1183
1184 init_timer(&fep->phy_timer_list);
1185
1186 /* partial reset of FEC so that only MII works */
1187 FW(fecp, mii_speed, fep->fec_phy_speed);
1188 FW(fecp, ievent, 0xffc0);
1189 FW(fecp, ivec, (fpi->fec_irq / 2) << 29);
1190 FW(fecp, imask, 0);
1191 FW(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
1192 FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
1193
1194 netif_carrier_off(dev);
1195
1196 err = register_netdev(dev);
1197 if (err != 0)
1198 goto err;
1199 registered = 1;
1200
1201 if (fpi->use_mdio) {
1202 fep->mii_if.dev = dev;
1203 fep->mii_if.mdio_read = fec_mii_read;
1204 fep->mii_if.mdio_write = fec_mii_write;
1205 fep->mii_if.phy_id_mask = 0x1f;
1206 fep->mii_if.reg_num_mask = 0x1f;
1207 fep->mii_if.phy_id = fec_mii_phy_id_detect(dev);
1208 }
1209
1210 *devp = dev;
1211
1212 return 0;
1213
1214 err:
1215 if (dev != NULL) {
1216 if (fecp != NULL)
1217 fec_whack_reset(fecp);
1218
1219 if (registered)
1220 unregister_netdev(dev);
1221
1222 if (fep != NULL) {
1223 if (fep->ring_base)
1224 dma_free_coherent(NULL,
1225 (fpi->tx_ring +
1226 fpi->rx_ring) *
1227 sizeof(cbd_t), fep->ring_base,
1228 fep->ring_mem_addr);
1229 }
1230 free_netdev(dev);
1231 }
1232 return err;
1233}
1234
1235int fec_8xx_cleanup_one(struct net_device *dev)
1236{
1237 struct fec_enet_private *fep = netdev_priv(dev);
1238 fec_t *fecp = fep->fecp;
1239 const struct fec_platform_info *fpi = fep->fpi;
1240
1241 fec_whack_reset(fecp);
1242
1243 unregister_netdev(dev);
1244
1245 dma_free_coherent(NULL, (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t),
1246 fep->ring_base, fep->ring_mem_addr);
1247
1248 free_netdev(dev);
1249
1250 return 0;
1251}
1252
1253/**************************************************************************************/
1254/**************************************************************************************/
1255/**************************************************************************************/
1256
1257static int __init fec_8xx_init(void)
1258{
1259 return fec_8xx_platform_init();
1260}
1261
1262static void __exit fec_8xx_cleanup(void)
1263{
1264 fec_8xx_platform_cleanup();
1265}
1266
1267/**************************************************************************************/
1268/**************************************************************************************/
1269/**************************************************************************************/
1270
1271module_init(fec_8xx_init);
1272module_exit(fec_8xx_cleanup);