blob: 77f747a5afa71aaf5d964c26a89d792339e46faa [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) {
564 skb->dev = dev;
565 skb_put(skb, pkt_len); /* Make room */
566 skb->protocol = eth_type_trans(skb, dev);
567 received++;
568 if (!fpi->use_napi)
569 netif_rx(skb);
570 else
571 netif_receive_skb(skb);
572 } else {
573 printk(KERN_WARNING DRV_MODULE_NAME
574 ": %s Memory squeeze, dropping packet.\n",
575 dev->name);
576 fep->stats.rx_dropped++;
577 skbn = skb;
578 }
579 }
580
581 fep->rx_skbuff[curidx] = skbn;
582 CBDW_BUFADDR(bdp, dma_map_single(NULL, skbn->data,
583 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
584 DMA_FROM_DEVICE));
585 CBDW_DATLEN(bdp, 0);
586 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
587
588 /*
589 * Update BD pointer to next entry.
590 */
591 if ((sc & BD_ENET_RX_WRAP) == 0)
592 bdp++;
593 else
594 bdp = fep->rx_bd_base;
595
596 /*
597 * Doing this here will keep the FEC running while we process
598 * incoming frames. On a heavily loaded network, we should be
599 * able to keep up at the expense of system resources.
600 */
601 FW(fecp, r_des_active, 0x01000000);
602 }
603
604 fep->cur_rx = bdp;
605
606 if (fpi->use_napi) {
607 dev->quota -= received;
608 *budget -= received;
609
610 if (rx_work_limit < 0)
611 return 1; /* not done */
612
613 /* done */
614 netif_rx_complete(dev);
615
616 /* enable RX interrupt bits */
617 FS(fecp, imask, FEC_ENET_RXF | FEC_ENET_RXB);
618 }
619
620 return 0;
621}
622
623static void fec_enet_tx(struct net_device *dev)
624{
625 struct fec_enet_private *fep = netdev_priv(dev);
626 cbd_t *bdp;
627 struct sk_buff *skb;
628 int dirtyidx, do_wake;
629 __u16 sc;
630
631 spin_lock(&fep->lock);
632 bdp = fep->dirty_tx;
633
634 do_wake = 0;
635 while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
636
637 dirtyidx = bdp - fep->tx_bd_base;
638
639 if (fep->tx_free == fep->tx_ring)
640 break;
641
642 skb = fep->tx_skbuff[dirtyidx];
643
644 /*
645 * Check for errors.
646 */
647 if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
648 BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
649 fep->stats.tx_errors++;
650 if (sc & BD_ENET_TX_HB) /* No heartbeat */
651 fep->stats.tx_heartbeat_errors++;
652 if (sc & BD_ENET_TX_LC) /* Late collision */
653 fep->stats.tx_window_errors++;
654 if (sc & BD_ENET_TX_RL) /* Retrans limit */
655 fep->stats.tx_aborted_errors++;
656 if (sc & BD_ENET_TX_UN) /* Underrun */
657 fep->stats.tx_fifo_errors++;
658 if (sc & BD_ENET_TX_CSL) /* Carrier lost */
659 fep->stats.tx_carrier_errors++;
660 } else
661 fep->stats.tx_packets++;
662
663 if (sc & BD_ENET_TX_READY)
664 printk(KERN_WARNING DRV_MODULE_NAME
665 ": %s HEY! Enet xmit interrupt and TX_READY.\n",
666 dev->name);
667
668 /*
669 * Deferred means some collisions occurred during transmit,
670 * but we eventually sent the packet OK.
671 */
672 if (sc & BD_ENET_TX_DEF)
673 fep->stats.collisions++;
674
675 /*
676 * Free the sk buffer associated with this last transmit.
677 */
678 dev_kfree_skb_irq(skb);
679 fep->tx_skbuff[dirtyidx] = NULL;
680
681 /*
682 * Update pointer to next buffer descriptor to be transmitted.
683 */
684 if ((sc & BD_ENET_TX_WRAP) == 0)
685 bdp++;
686 else
687 bdp = fep->tx_bd_base;
688
689 /*
690 * Since we have freed up a buffer, the ring is no longer
691 * full.
692 */
693 if (!fep->tx_free++)
694 do_wake = 1;
695 }
696
697 fep->dirty_tx = bdp;
698
699 spin_unlock(&fep->lock);
700
701 if (do_wake && netif_queue_stopped(dev))
702 netif_wake_queue(dev);
703}
704
705/*
706 * The interrupt handler.
707 * This is called from the MPC core interrupt.
708 */
709static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100710fec_enet_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 struct net_device *dev = dev_id;
713 struct fec_enet_private *fep;
714 const struct fec_platform_info *fpi;
715 fec_t *fecp;
716 __u32 int_events;
717 __u32 int_events_napi;
718
719 if (unlikely(dev == NULL))
720 return IRQ_NONE;
721
722 fep = netdev_priv(dev);
723 fecp = fep->fecp;
724 fpi = fep->fpi;
725
726 /*
727 * Get the interrupt events that caused us to be here.
728 */
729 while ((int_events = FR(fecp, ievent) & FR(fecp, imask)) != 0) {
730
731 if (!fpi->use_napi)
732 FW(fecp, ievent, int_events);
733 else {
734 int_events_napi = int_events & ~(FEC_ENET_RXF | FEC_ENET_RXB);
735 FW(fecp, ievent, int_events_napi);
736 }
737
738 if ((int_events & (FEC_ENET_HBERR | FEC_ENET_BABR |
739 FEC_ENET_BABT | FEC_ENET_EBERR)) != 0)
740 printk(KERN_WARNING DRV_MODULE_NAME
741 ": %s FEC ERROR(s) 0x%x\n",
742 dev->name, int_events);
743
744 if ((int_events & FEC_ENET_RXF) != 0) {
745 if (!fpi->use_napi)
746 fec_enet_rx_common(dev, NULL);
747 else {
748 if (netif_rx_schedule_prep(dev)) {
749 /* disable rx interrupts */
750 FC(fecp, imask, FEC_ENET_RXF | FEC_ENET_RXB);
751 __netif_rx_schedule(dev);
752 } else {
753 printk(KERN_ERR DRV_MODULE_NAME
754 ": %s driver bug! interrupt while in poll!\n",
755 dev->name);
756 FC(fecp, imask, FEC_ENET_RXF | FEC_ENET_RXB);
757 }
758 }
759 }
760
761 if ((int_events & FEC_ENET_TXF) != 0)
762 fec_enet_tx(dev);
763 }
764
765 return IRQ_HANDLED;
766}
767
768/* This interrupt occurs when the PHY detects a link change. */
769static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100770fec_mii_link_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 struct net_device *dev = dev_id;
773 struct fec_enet_private *fep;
774 const struct fec_platform_info *fpi;
775
776 if (unlikely(dev == NULL))
777 return IRQ_NONE;
778
779 fep = netdev_priv(dev);
780 fpi = fep->fpi;
781
782 if (!fpi->use_mdio)
783 return IRQ_NONE;
784
785 /*
786 * Acknowledge the interrupt if possible. If we have not
787 * found the PHY yet we can't process or acknowledge the
788 * interrupt now. Instead we ignore this interrupt for now,
789 * which we can do since it is edge triggered. It will be
790 * acknowledged later by fec_enet_open().
791 */
792 if (!fep->phy)
793 return IRQ_NONE;
794
795 fec_mii_ack_int(dev);
796 fec_mii_link_status_change_check(dev, 0);
797
798 return IRQ_HANDLED;
799}
800
801
802/**********************************************************************************/
803
804static int fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
805{
806 struct fec_enet_private *fep = netdev_priv(dev);
807 fec_t *fecp = fep->fecp;
808 cbd_t *bdp;
809 int curidx;
810 unsigned long flags;
811
812 spin_lock_irqsave(&fep->tx_lock, flags);
813
814 /*
815 * Fill in a Tx ring entry
816 */
817 bdp = fep->cur_tx;
818
819 if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
820 netif_stop_queue(dev);
821 spin_unlock_irqrestore(&fep->tx_lock, flags);
822
823 /*
824 * Ooops. All transmit buffers are full. Bail out.
825 * This should not happen, since the tx queue should be stopped.
826 */
827 printk(KERN_WARNING DRV_MODULE_NAME
828 ": %s tx queue full!.\n", dev->name);
829 return 1;
830 }
831
832 curidx = bdp - fep->tx_bd_base;
833 /*
834 * Clear all of the status flags.
835 */
836 CBDC_SC(bdp, BD_ENET_TX_STATS);
837
838 /*
839 * Save skb pointer.
840 */
841 fep->tx_skbuff[curidx] = skb;
842
843 fep->stats.tx_bytes += skb->len;
844
845 /*
846 * Push the data cache so the CPM does not get stale memory data.
847 */
848 CBDW_BUFADDR(bdp, dma_map_single(NULL, skb->data,
849 skb->len, DMA_TO_DEVICE));
850 CBDW_DATLEN(bdp, skb->len);
851
852 dev->trans_start = jiffies;
853
854 /*
855 * If this was the last BD in the ring, start at the beginning again.
856 */
857 if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
858 fep->cur_tx++;
859 else
860 fep->cur_tx = fep->tx_bd_base;
861
862 if (!--fep->tx_free)
863 netif_stop_queue(dev);
864
865 /*
866 * Trigger transmission start
867 */
868 CBDS_SC(bdp, BD_ENET_TX_READY | BD_ENET_TX_INTR |
869 BD_ENET_TX_LAST | BD_ENET_TX_TC);
870 FW(fecp, x_des_active, 0x01000000);
871
872 spin_unlock_irqrestore(&fep->tx_lock, flags);
873
874 return 0;
875}
876
877static void fec_timeout(struct net_device *dev)
878{
879 struct fec_enet_private *fep = netdev_priv(dev);
880
881 fep->stats.tx_errors++;
882
883 if (fep->tx_free)
884 netif_wake_queue(dev);
885
886 /* check link status again */
887 fec_mii_link_status_change_check(dev, 0);
888}
889
890static int fec_enet_open(struct net_device *dev)
891{
892 struct fec_enet_private *fep = netdev_priv(dev);
893 const struct fec_platform_info *fpi = fep->fpi;
894 unsigned long flags;
895
896 /* Install our interrupt handler. */
897 if (request_irq(fpi->fec_irq, fec_enet_interrupt, 0, "fec", dev) != 0) {
898 printk(KERN_ERR DRV_MODULE_NAME
899 ": %s Could not allocate FEC IRQ!", dev->name);
900 return -EINVAL;
901 }
902
903 /* Install our phy interrupt handler */
904 if (fpi->phy_irq != -1 &&
905 request_irq(fpi->phy_irq, fec_mii_link_interrupt, 0, "fec-phy",
906 dev) != 0) {
907 printk(KERN_ERR DRV_MODULE_NAME
908 ": %s Could not allocate PHY IRQ!", dev->name);
909 free_irq(fpi->fec_irq, dev);
910 return -EINVAL;
911 }
912
913 if (fpi->use_mdio) {
914 fec_mii_startup(dev);
915 netif_carrier_off(dev);
916 fec_mii_link_status_change_check(dev, 1);
917 } else {
918 spin_lock_irqsave(&fep->lock, flags);
919 fec_restart(dev, 1, 100); /* XXX this sucks */
920 spin_unlock_irqrestore(&fep->lock, flags);
921
922 netif_carrier_on(dev);
923 netif_start_queue(dev);
924 }
925 return 0;
926}
927
928static int fec_enet_close(struct net_device *dev)
929{
930 struct fec_enet_private *fep = netdev_priv(dev);
931 const struct fec_platform_info *fpi = fep->fpi;
932 unsigned long flags;
933
934 netif_stop_queue(dev);
935 netif_carrier_off(dev);
936
937 if (fpi->use_mdio)
938 fec_mii_shutdown(dev);
939
940 spin_lock_irqsave(&fep->lock, flags);
941 fec_stop(dev);
942 spin_unlock_irqrestore(&fep->lock, flags);
943
944 /* release any irqs */
945 if (fpi->phy_irq != -1)
946 free_irq(fpi->phy_irq, dev);
947 free_irq(fpi->fec_irq, dev);
948
949 return 0;
950}
951
952static struct net_device_stats *fec_enet_get_stats(struct net_device *dev)
953{
954 struct fec_enet_private *fep = netdev_priv(dev);
955 return &fep->stats;
956}
957
958static int fec_enet_poll(struct net_device *dev, int *budget)
959{
960 return fec_enet_rx_common(dev, budget);
961}
962
963/*************************************************************************/
964
965static void fec_get_drvinfo(struct net_device *dev,
966 struct ethtool_drvinfo *info)
967{
968 strcpy(info->driver, DRV_MODULE_NAME);
969 strcpy(info->version, DRV_MODULE_VERSION);
970}
971
972static int fec_get_regs_len(struct net_device *dev)
973{
974 return sizeof(fec_t);
975}
976
977static void fec_get_regs(struct net_device *dev, struct ethtool_regs *regs,
978 void *p)
979{
980 struct fec_enet_private *fep = netdev_priv(dev);
981 unsigned long flags;
982
983 if (regs->len < sizeof(fec_t))
984 return;
985
986 regs->version = 0;
987 spin_lock_irqsave(&fep->lock, flags);
988 memcpy_fromio(p, fep->fecp, sizeof(fec_t));
989 spin_unlock_irqrestore(&fep->lock, flags);
990}
991
992static int fec_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
993{
994 struct fec_enet_private *fep = netdev_priv(dev);
995 unsigned long flags;
996 int rc;
997
998 spin_lock_irqsave(&fep->lock, flags);
999 rc = mii_ethtool_gset(&fep->mii_if, cmd);
1000 spin_unlock_irqrestore(&fep->lock, flags);
1001
1002 return rc;
1003}
1004
1005static int fec_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1006{
1007 struct fec_enet_private *fep = netdev_priv(dev);
1008 unsigned long flags;
1009 int rc;
1010
1011 spin_lock_irqsave(&fep->lock, flags);
1012 rc = mii_ethtool_sset(&fep->mii_if, cmd);
1013 spin_unlock_irqrestore(&fep->lock, flags);
1014
1015 return rc;
1016}
1017
1018static int fec_nway_reset(struct net_device *dev)
1019{
1020 struct fec_enet_private *fep = netdev_priv(dev);
1021 return mii_nway_restart(&fep->mii_if);
1022}
1023
1024static __u32 fec_get_msglevel(struct net_device *dev)
1025{
1026 struct fec_enet_private *fep = netdev_priv(dev);
1027 return fep->msg_enable;
1028}
1029
1030static void fec_set_msglevel(struct net_device *dev, __u32 value)
1031{
1032 struct fec_enet_private *fep = netdev_priv(dev);
1033 fep->msg_enable = value;
1034}
1035
Jeff Garzik7282d492006-09-13 14:30:00 -04001036static const struct ethtool_ops fec_ethtool_ops = {
1037 .get_drvinfo = fec_get_drvinfo,
1038 .get_regs_len = fec_get_regs_len,
1039 .get_settings = fec_get_settings,
1040 .set_settings = fec_set_settings,
1041 .nway_reset = fec_nway_reset,
1042 .get_link = ethtool_op_get_link,
1043 .get_msglevel = fec_get_msglevel,
1044 .set_msglevel = fec_set_msglevel,
1045 .get_tx_csum = ethtool_op_get_tx_csum,
1046 .set_tx_csum = ethtool_op_set_tx_csum, /* local! */
1047 .get_sg = ethtool_op_get_sg,
1048 .set_sg = ethtool_op_set_sg,
1049 .get_regs = fec_get_regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050};
1051
1052static int fec_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1053{
1054 struct fec_enet_private *fep = netdev_priv(dev);
1055 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data;
1056 unsigned long flags;
1057 int rc;
1058
1059 if (!netif_running(dev))
1060 return -EINVAL;
1061
1062 spin_lock_irqsave(&fep->lock, flags);
1063 rc = generic_mii_ioctl(&fep->mii_if, mii, cmd, NULL);
1064 spin_unlock_irqrestore(&fep->lock, flags);
1065 return rc;
1066}
1067
1068int fec_8xx_init_one(const struct fec_platform_info *fpi,
1069 struct net_device **devp)
1070{
1071 immap_t *immap = (immap_t *) IMAP_ADDR;
1072 static int fec_8xx_version_printed = 0;
1073 struct net_device *dev = NULL;
1074 struct fec_enet_private *fep = NULL;
1075 fec_t *fecp = NULL;
1076 int i;
1077 int err = 0;
1078 int registered = 0;
1079 __u32 siel;
1080
1081 *devp = NULL;
1082
1083 switch (fpi->fec_no) {
1084 case 0:
1085 fecp = &((immap_t *) IMAP_ADDR)->im_cpm.cp_fec;
1086 break;
1087#ifdef CONFIG_DUET
1088 case 1:
1089 fecp = &((immap_t *) IMAP_ADDR)->im_cpm.cp_fec2;
1090 break;
1091#endif
1092 default:
1093 return -EINVAL;
1094 }
1095
1096 if (fec_8xx_version_printed++ == 0)
1097 printk(KERN_INFO "%s", version);
1098
1099 i = sizeof(*fep) + (sizeof(struct sk_buff **) *
1100 (fpi->rx_ring + fpi->tx_ring));
1101
1102 dev = alloc_etherdev(i);
1103 if (!dev) {
1104 err = -ENOMEM;
1105 goto err;
1106 }
1107 SET_MODULE_OWNER(dev);
1108
1109 fep = netdev_priv(dev);
1110
1111 /* partial reset of FEC */
1112 fec_whack_reset(fecp);
1113
1114 /* point rx_skbuff, tx_skbuff */
1115 fep->rx_skbuff = (struct sk_buff **)&fep[1];
1116 fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
1117
1118 fep->fecp = fecp;
1119 fep->fpi = fpi;
1120
1121 /* init locks */
1122 spin_lock_init(&fep->lock);
1123 spin_lock_init(&fep->tx_lock);
1124
1125 /*
1126 * Set the Ethernet address.
1127 */
1128 for (i = 0; i < 6; i++)
1129 dev->dev_addr[i] = fpi->macaddr[i];
1130
1131 fep->ring_base = dma_alloc_coherent(NULL,
1132 (fpi->tx_ring + fpi->rx_ring) *
1133 sizeof(cbd_t), &fep->ring_mem_addr,
1134 GFP_KERNEL);
1135 if (fep->ring_base == NULL) {
1136 printk(KERN_ERR DRV_MODULE_NAME
1137 ": %s dma alloc failed.\n", dev->name);
1138 err = -ENOMEM;
1139 goto err;
1140 }
1141
1142 /*
1143 * Set receive and transmit descriptor base.
1144 */
1145 fep->rx_bd_base = fep->ring_base;
1146 fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1147
1148 /* initialize ring size variables */
1149 fep->tx_ring = fpi->tx_ring;
1150 fep->rx_ring = fpi->rx_ring;
1151
1152 /* SIU interrupt */
1153 if (fpi->phy_irq != -1 &&
1154 (fpi->phy_irq >= SIU_IRQ0 && fpi->phy_irq < SIU_LEVEL7)) {
1155
1156 siel = in_be32(&immap->im_siu_conf.sc_siel);
1157 if ((fpi->phy_irq & 1) == 0)
1158 siel |= (0x80000000 >> fpi->phy_irq);
1159 else
1160 siel &= ~(0x80000000 >> (fpi->phy_irq & ~1));
1161 out_be32(&immap->im_siu_conf.sc_siel, siel);
1162 }
1163
1164 /*
1165 * The FEC Ethernet specific entries in the device structure.
1166 */
1167 dev->open = fec_enet_open;
1168 dev->hard_start_xmit = fec_enet_start_xmit;
1169 dev->tx_timeout = fec_timeout;
1170 dev->watchdog_timeo = TX_TIMEOUT;
1171 dev->stop = fec_enet_close;
1172 dev->get_stats = fec_enet_get_stats;
1173 dev->set_multicast_list = fec_set_multicast_list;
1174 dev->set_mac_address = fec_set_mac_address;
1175 if (fpi->use_napi) {
1176 dev->poll = fec_enet_poll;
1177 dev->weight = fpi->napi_weight;
1178 }
1179 dev->ethtool_ops = &fec_ethtool_ops;
1180 dev->do_ioctl = fec_ioctl;
1181
1182 fep->fec_phy_speed =
1183 ((((fpi->sys_clk + 4999999) / 2500000) / 2) & 0x3F) << 1;
1184
1185 init_timer(&fep->phy_timer_list);
1186
1187 /* partial reset of FEC so that only MII works */
1188 FW(fecp, mii_speed, fep->fec_phy_speed);
1189 FW(fecp, ievent, 0xffc0);
1190 FW(fecp, ivec, (fpi->fec_irq / 2) << 29);
1191 FW(fecp, imask, 0);
1192 FW(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
1193 FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
1194
1195 netif_carrier_off(dev);
1196
1197 err = register_netdev(dev);
1198 if (err != 0)
1199 goto err;
1200 registered = 1;
1201
1202 if (fpi->use_mdio) {
1203 fep->mii_if.dev = dev;
1204 fep->mii_if.mdio_read = fec_mii_read;
1205 fep->mii_if.mdio_write = fec_mii_write;
1206 fep->mii_if.phy_id_mask = 0x1f;
1207 fep->mii_if.reg_num_mask = 0x1f;
1208 fep->mii_if.phy_id = fec_mii_phy_id_detect(dev);
1209 }
1210
1211 *devp = dev;
1212
1213 return 0;
1214
1215 err:
1216 if (dev != NULL) {
1217 if (fecp != NULL)
1218 fec_whack_reset(fecp);
1219
1220 if (registered)
1221 unregister_netdev(dev);
1222
1223 if (fep != NULL) {
1224 if (fep->ring_base)
1225 dma_free_coherent(NULL,
1226 (fpi->tx_ring +
1227 fpi->rx_ring) *
1228 sizeof(cbd_t), fep->ring_base,
1229 fep->ring_mem_addr);
1230 }
1231 free_netdev(dev);
1232 }
1233 return err;
1234}
1235
1236int fec_8xx_cleanup_one(struct net_device *dev)
1237{
1238 struct fec_enet_private *fep = netdev_priv(dev);
1239 fec_t *fecp = fep->fecp;
1240 const struct fec_platform_info *fpi = fep->fpi;
1241
1242 fec_whack_reset(fecp);
1243
1244 unregister_netdev(dev);
1245
1246 dma_free_coherent(NULL, (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t),
1247 fep->ring_base, fep->ring_mem_addr);
1248
1249 free_netdev(dev);
1250
1251 return 0;
1252}
1253
1254/**************************************************************************************/
1255/**************************************************************************************/
1256/**************************************************************************************/
1257
1258static int __init fec_8xx_init(void)
1259{
1260 return fec_8xx_platform_init();
1261}
1262
1263static void __exit fec_8xx_cleanup(void)
1264{
1265 fec_8xx_platform_cleanup();
1266}
1267
1268/**************************************************************************************/
1269/**************************************************************************************/
1270/**************************************************************************************/
1271
1272module_init(fec_8xx_init);
1273module_exit(fec_8xx_cleanup);