blob: cb3958704a8727f65351bd87b46d1501f2897d92 [file] [log] [blame]
Pantelis Antoniou48257c42005-10-28 16:25:58 -04001/*
2 * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
3 *
4 * Copyright (c) 2003 Intracom S.A.
5 * by Pantelis Antoniou <panto@intracom.gr>
6 *
7 * 2005 (c) MontaVista Software, Inc.
8 * Vitaly Bordug <vbordug@ru.mvista.com>
9 *
10 * Heavily based on original FEC driver by Dan Malek <dan@embeddededge.com>
11 * and modifications by Joakim Tjernlund <joakim.tjernlund@lumentis.se>
12 *
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
16 */
17
Pantelis Antoniou48257c42005-10-28 16:25:58 -040018#include <linux/module.h>
19#include <linux/kernel.h>
20#include <linux/types.h>
21#include <linux/sched.h>
22#include <linux/string.h>
23#include <linux/ptrace.h>
24#include <linux/errno.h>
25#include <linux/ioport.h>
26#include <linux/slab.h>
27#include <linux/interrupt.h>
28#include <linux/pci.h>
29#include <linux/init.h>
30#include <linux/delay.h>
31#include <linux/netdevice.h>
32#include <linux/etherdevice.h>
33#include <linux/skbuff.h>
34#include <linux/spinlock.h>
35#include <linux/mii.h>
36#include <linux/ethtool.h>
37#include <linux/bitops.h>
38#include <linux/fs.h>
Marcelo Tosattif7b99962005-11-09 11:00:16 -020039#include <linux/platform_device.h>
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070040#include <linux/phy.h>
Pantelis Antoniou48257c42005-10-28 16:25:58 -040041
42#include <linux/vmalloc.h>
43#include <asm/pgtable.h>
44
45#include <asm/pgtable.h>
46#include <asm/irq.h>
47#include <asm/uaccess.h>
48
49#include "fs_enet.h"
50
51/*************************************************/
52
53static char version[] __devinitdata =
54 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")" "\n";
55
56MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
57MODULE_DESCRIPTION("Freescale Ethernet Driver");
58MODULE_LICENSE("GPL");
59MODULE_VERSION(DRV_MODULE_VERSION);
60
Rusty Russell8d3b33f2006-03-25 03:07:05 -080061int fs_enet_debug = -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
62module_param(fs_enet_debug, int, 0);
Pantelis Antoniou48257c42005-10-28 16:25:58 -040063MODULE_PARM_DESC(fs_enet_debug,
64 "Freescale bitmapped debugging message enable value");
65
Pantelis Antoniou48257c42005-10-28 16:25:58 -040066
67static void fs_set_multicast_list(struct net_device *dev)
68{
69 struct fs_enet_private *fep = netdev_priv(dev);
70
71 (*fep->ops->set_multicast_list)(dev);
72}
73
74/* NAPI receive function */
75static int fs_enet_rx_napi(struct net_device *dev, int *budget)
76{
77 struct fs_enet_private *fep = netdev_priv(dev);
78 const struct fs_platform_info *fpi = fep->fpi;
79 cbd_t *bdp;
80 struct sk_buff *skb, *skbn, *skbt;
81 int received = 0;
82 u16 pkt_len, sc;
83 int curidx;
84 int rx_work_limit = 0; /* pacify gcc */
85
86 rx_work_limit = min(dev->quota, *budget);
87
88 if (!netif_running(dev))
89 return 0;
90
91 /*
92 * First, grab all of the stats for the incoming packet.
93 * These get messed up if we get called due to a busy condition.
94 */
95 bdp = fep->cur_rx;
96
97 /* clear RX status bits for napi*/
98 (*fep->ops->napi_clear_rx_event)(dev);
99
100 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
101
102 curidx = bdp - fep->rx_bd_base;
103
104 /*
105 * Since we have allocated space to hold a complete frame,
106 * the last indicator should be set.
107 */
108 if ((sc & BD_ENET_RX_LAST) == 0)
109 printk(KERN_WARNING DRV_MODULE_NAME
110 ": %s rcv is not +last\n",
111 dev->name);
112
113 /*
114 * Check for errors.
115 */
116 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
117 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
118 fep->stats.rx_errors++;
119 /* Frame too long or too short. */
120 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
121 fep->stats.rx_length_errors++;
122 /* Frame alignment */
123 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
124 fep->stats.rx_frame_errors++;
125 /* CRC Error */
126 if (sc & BD_ENET_RX_CR)
127 fep->stats.rx_crc_errors++;
128 /* FIFO overrun */
129 if (sc & BD_ENET_RX_OV)
130 fep->stats.rx_crc_errors++;
131
132 skb = fep->rx_skbuff[curidx];
133
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300134 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400135 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
136 DMA_FROM_DEVICE);
137
138 skbn = skb;
139
140 } else {
141
142 /* napi, got packet but no quota */
143 if (--rx_work_limit < 0)
144 break;
145
146 skb = fep->rx_skbuff[curidx];
147
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300148 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400149 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
150 DMA_FROM_DEVICE);
151
152 /*
153 * Process the incoming frame.
154 */
155 fep->stats.rx_packets++;
156 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
157 fep->stats.rx_bytes += pkt_len + 4;
158
159 if (pkt_len <= fpi->rx_copybreak) {
160 /* +2 to make IP header L1 cache aligned */
161 skbn = dev_alloc_skb(pkt_len + 2);
162 if (skbn != NULL) {
163 skb_reserve(skbn, 2); /* align IP header */
164 memcpy(skbn->data, skb->data, pkt_len);
165 /* swap */
166 skbt = skb;
167 skb = skbn;
168 skbn = skbt;
169 }
170 } else
171 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
172
173 if (skbn != NULL) {
174 skb->dev = dev;
175 skb_put(skb, pkt_len); /* Make room */
176 skb->protocol = eth_type_trans(skb, dev);
177 received++;
178 netif_receive_skb(skb);
179 } else {
180 printk(KERN_WARNING DRV_MODULE_NAME
181 ": %s Memory squeeze, dropping packet.\n",
182 dev->name);
183 fep->stats.rx_dropped++;
184 skbn = skb;
185 }
186 }
187
188 fep->rx_skbuff[curidx] = skbn;
189 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
190 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
191 DMA_FROM_DEVICE));
192 CBDW_DATLEN(bdp, 0);
193 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
194
195 /*
196 * Update BD pointer to next entry.
197 */
198 if ((sc & BD_ENET_RX_WRAP) == 0)
199 bdp++;
200 else
201 bdp = fep->rx_bd_base;
202
203 (*fep->ops->rx_bd_done)(dev);
204 }
205
206 fep->cur_rx = bdp;
207
208 dev->quota -= received;
209 *budget -= received;
210
211 if (rx_work_limit < 0)
212 return 1; /* not done */
213
214 /* done */
215 netif_rx_complete(dev);
216
217 (*fep->ops->napi_enable_rx)(dev);
218
219 return 0;
220}
221
222/* non NAPI receive function */
223static int fs_enet_rx_non_napi(struct net_device *dev)
224{
225 struct fs_enet_private *fep = netdev_priv(dev);
226 const struct fs_platform_info *fpi = fep->fpi;
227 cbd_t *bdp;
228 struct sk_buff *skb, *skbn, *skbt;
229 int received = 0;
230 u16 pkt_len, sc;
231 int curidx;
232 /*
233 * First, grab all of the stats for the incoming packet.
234 * These get messed up if we get called due to a busy condition.
235 */
236 bdp = fep->cur_rx;
237
238 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
239
240 curidx = bdp - fep->rx_bd_base;
241
242 /*
243 * Since we have allocated space to hold a complete frame,
244 * the last indicator should be set.
245 */
246 if ((sc & BD_ENET_RX_LAST) == 0)
247 printk(KERN_WARNING DRV_MODULE_NAME
248 ": %s rcv is not +last\n",
249 dev->name);
250
251 /*
252 * Check for errors.
253 */
254 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
255 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
256 fep->stats.rx_errors++;
257 /* Frame too long or too short. */
258 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
259 fep->stats.rx_length_errors++;
260 /* Frame alignment */
261 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
262 fep->stats.rx_frame_errors++;
263 /* CRC Error */
264 if (sc & BD_ENET_RX_CR)
265 fep->stats.rx_crc_errors++;
266 /* FIFO overrun */
267 if (sc & BD_ENET_RX_OV)
268 fep->stats.rx_crc_errors++;
269
270 skb = fep->rx_skbuff[curidx];
271
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300272 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400273 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
274 DMA_FROM_DEVICE);
275
276 skbn = skb;
277
278 } else {
279
280 skb = fep->rx_skbuff[curidx];
281
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300282 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400283 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
284 DMA_FROM_DEVICE);
285
286 /*
287 * Process the incoming frame.
288 */
289 fep->stats.rx_packets++;
290 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
291 fep->stats.rx_bytes += pkt_len + 4;
292
293 if (pkt_len <= fpi->rx_copybreak) {
294 /* +2 to make IP header L1 cache aligned */
295 skbn = dev_alloc_skb(pkt_len + 2);
296 if (skbn != NULL) {
297 skb_reserve(skbn, 2); /* align IP header */
298 memcpy(skbn->data, skb->data, pkt_len);
299 /* swap */
300 skbt = skb;
301 skb = skbn;
302 skbn = skbt;
303 }
304 } else
305 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
306
307 if (skbn != NULL) {
308 skb->dev = dev;
309 skb_put(skb, pkt_len); /* Make room */
310 skb->protocol = eth_type_trans(skb, dev);
311 received++;
312 netif_rx(skb);
313 } else {
314 printk(KERN_WARNING DRV_MODULE_NAME
315 ": %s Memory squeeze, dropping packet.\n",
316 dev->name);
317 fep->stats.rx_dropped++;
318 skbn = skb;
319 }
320 }
321
322 fep->rx_skbuff[curidx] = skbn;
323 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
324 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
325 DMA_FROM_DEVICE));
326 CBDW_DATLEN(bdp, 0);
327 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
328
329 /*
330 * Update BD pointer to next entry.
331 */
332 if ((sc & BD_ENET_RX_WRAP) == 0)
333 bdp++;
334 else
335 bdp = fep->rx_bd_base;
336
337 (*fep->ops->rx_bd_done)(dev);
338 }
339
340 fep->cur_rx = bdp;
341
342 return 0;
343}
344
345static void fs_enet_tx(struct net_device *dev)
346{
347 struct fs_enet_private *fep = netdev_priv(dev);
348 cbd_t *bdp;
349 struct sk_buff *skb;
350 int dirtyidx, do_wake, do_restart;
351 u16 sc;
352
353 spin_lock(&fep->lock);
354 bdp = fep->dirty_tx;
355
356 do_wake = do_restart = 0;
357 while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
358
359 dirtyidx = bdp - fep->tx_bd_base;
360
361 if (fep->tx_free == fep->tx_ring)
362 break;
363
364 skb = fep->tx_skbuff[dirtyidx];
365
366 /*
367 * Check for errors.
368 */
369 if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
370 BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
371
372 if (sc & BD_ENET_TX_HB) /* No heartbeat */
373 fep->stats.tx_heartbeat_errors++;
374 if (sc & BD_ENET_TX_LC) /* Late collision */
375 fep->stats.tx_window_errors++;
376 if (sc & BD_ENET_TX_RL) /* Retrans limit */
377 fep->stats.tx_aborted_errors++;
378 if (sc & BD_ENET_TX_UN) /* Underrun */
379 fep->stats.tx_fifo_errors++;
380 if (sc & BD_ENET_TX_CSL) /* Carrier lost */
381 fep->stats.tx_carrier_errors++;
382
383 if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
384 fep->stats.tx_errors++;
385 do_restart = 1;
386 }
387 } else
388 fep->stats.tx_packets++;
389
390 if (sc & BD_ENET_TX_READY)
391 printk(KERN_WARNING DRV_MODULE_NAME
392 ": %s HEY! Enet xmit interrupt and TX_READY.\n",
393 dev->name);
394
395 /*
396 * Deferred means some collisions occurred during transmit,
397 * but we eventually sent the packet OK.
398 */
399 if (sc & BD_ENET_TX_DEF)
400 fep->stats.collisions++;
401
402 /* unmap */
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300403 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
404 skb->len, DMA_TO_DEVICE);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400405
406 /*
407 * Free the sk buffer associated with this last transmit.
408 */
409 dev_kfree_skb_irq(skb);
410 fep->tx_skbuff[dirtyidx] = NULL;
411
412 /*
413 * Update pointer to next buffer descriptor to be transmitted.
414 */
415 if ((sc & BD_ENET_TX_WRAP) == 0)
416 bdp++;
417 else
418 bdp = fep->tx_bd_base;
419
420 /*
421 * Since we have freed up a buffer, the ring is no longer
422 * full.
423 */
424 if (!fep->tx_free++)
425 do_wake = 1;
426 }
427
428 fep->dirty_tx = bdp;
429
430 if (do_restart)
431 (*fep->ops->tx_restart)(dev);
432
433 spin_unlock(&fep->lock);
434
435 if (do_wake)
436 netif_wake_queue(dev);
437}
438
439/*
440 * The interrupt handler.
441 * This is called from the MPC core interrupt.
442 */
443static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100444fs_enet_interrupt(int irq, void *dev_id)
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400445{
446 struct net_device *dev = dev_id;
447 struct fs_enet_private *fep;
448 const struct fs_platform_info *fpi;
449 u32 int_events;
450 u32 int_clr_events;
451 int nr, napi_ok;
452 int handled;
453
454 fep = netdev_priv(dev);
455 fpi = fep->fpi;
456
457 nr = 0;
458 while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) {
459
460 nr++;
461
462 int_clr_events = int_events;
463 if (fpi->use_napi)
464 int_clr_events &= ~fep->ev_napi_rx;
465
466 (*fep->ops->clear_int_events)(dev, int_clr_events);
467
468 if (int_events & fep->ev_err)
469 (*fep->ops->ev_error)(dev, int_events);
470
471 if (int_events & fep->ev_rx) {
472 if (!fpi->use_napi)
473 fs_enet_rx_non_napi(dev);
474 else {
475 napi_ok = netif_rx_schedule_prep(dev);
476
477 (*fep->ops->napi_disable_rx)(dev);
478 (*fep->ops->clear_int_events)(dev, fep->ev_napi_rx);
479
480 /* NOTE: it is possible for FCCs in NAPI mode */
481 /* to submit a spurious interrupt while in poll */
482 if (napi_ok)
483 __netif_rx_schedule(dev);
484 }
485 }
486
487 if (int_events & fep->ev_tx)
488 fs_enet_tx(dev);
489 }
490
491 handled = nr > 0;
492 return IRQ_RETVAL(handled);
493}
494
495void fs_init_bds(struct net_device *dev)
496{
497 struct fs_enet_private *fep = netdev_priv(dev);
498 cbd_t *bdp;
499 struct sk_buff *skb;
500 int i;
501
502 fs_cleanup_bds(dev);
503
504 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
505 fep->tx_free = fep->tx_ring;
506 fep->cur_rx = fep->rx_bd_base;
507
508 /*
509 * Initialize the receive buffer descriptors.
510 */
511 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
512 skb = dev_alloc_skb(ENET_RX_FRSIZE);
513 if (skb == NULL) {
514 printk(KERN_WARNING DRV_MODULE_NAME
515 ": %s Memory squeeze, unable to allocate skb\n",
516 dev->name);
517 break;
518 }
519 fep->rx_skbuff[i] = skb;
520 skb->dev = dev;
521 CBDW_BUFADDR(bdp,
522 dma_map_single(fep->dev, skb->data,
523 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
524 DMA_FROM_DEVICE));
525 CBDW_DATLEN(bdp, 0); /* zero */
526 CBDW_SC(bdp, BD_ENET_RX_EMPTY |
527 ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
528 }
529 /*
530 * if we failed, fillup remainder
531 */
532 for (; i < fep->rx_ring; i++, bdp++) {
533 fep->rx_skbuff[i] = NULL;
534 CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
535 }
536
537 /*
538 * ...and the same for transmit.
539 */
540 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
541 fep->tx_skbuff[i] = NULL;
542 CBDW_BUFADDR(bdp, 0);
543 CBDW_DATLEN(bdp, 0);
544 CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
545 }
546}
547
548void fs_cleanup_bds(struct net_device *dev)
549{
550 struct fs_enet_private *fep = netdev_priv(dev);
551 struct sk_buff *skb;
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300552 cbd_t *bdp;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400553 int i;
554
555 /*
556 * Reset SKB transmit buffers.
557 */
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300558 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400559 if ((skb = fep->tx_skbuff[i]) == NULL)
560 continue;
561
562 /* unmap */
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300563 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
564 skb->len, DMA_TO_DEVICE);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400565
566 fep->tx_skbuff[i] = NULL;
567 dev_kfree_skb(skb);
568 }
569
570 /*
571 * Reset SKB receive buffers
572 */
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300573 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400574 if ((skb = fep->rx_skbuff[i]) == NULL)
575 continue;
576
577 /* unmap */
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300578 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400579 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
580 DMA_FROM_DEVICE);
581
582 fep->rx_skbuff[i] = NULL;
583
584 dev_kfree_skb(skb);
585 }
586}
587
588/**********************************************************************************/
589
590static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
591{
592 struct fs_enet_private *fep = netdev_priv(dev);
593 cbd_t *bdp;
594 int curidx;
595 u16 sc;
596 unsigned long flags;
597
598 spin_lock_irqsave(&fep->tx_lock, flags);
599
600 /*
601 * Fill in a Tx ring entry
602 */
603 bdp = fep->cur_tx;
604
605 if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
606 netif_stop_queue(dev);
607 spin_unlock_irqrestore(&fep->tx_lock, flags);
608
609 /*
610 * Ooops. All transmit buffers are full. Bail out.
611 * This should not happen, since the tx queue should be stopped.
612 */
613 printk(KERN_WARNING DRV_MODULE_NAME
614 ": %s tx queue full!.\n", dev->name);
615 return NETDEV_TX_BUSY;
616 }
617
618 curidx = bdp - fep->tx_bd_base;
619 /*
620 * Clear all of the status flags.
621 */
622 CBDC_SC(bdp, BD_ENET_TX_STATS);
623
624 /*
625 * Save skb pointer.
626 */
627 fep->tx_skbuff[curidx] = skb;
628
629 fep->stats.tx_bytes += skb->len;
630
631 /*
632 * Push the data cache so the CPM does not get stale memory data.
633 */
634 CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
635 skb->data, skb->len, DMA_TO_DEVICE));
636 CBDW_DATLEN(bdp, skb->len);
637
638 dev->trans_start = jiffies;
639
640 /*
641 * If this was the last BD in the ring, start at the beginning again.
642 */
643 if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
644 fep->cur_tx++;
645 else
646 fep->cur_tx = fep->tx_bd_base;
647
648 if (!--fep->tx_free)
649 netif_stop_queue(dev);
650
651 /* Trigger transmission start */
652 sc = BD_ENET_TX_READY | BD_ENET_TX_INTR |
653 BD_ENET_TX_LAST | BD_ENET_TX_TC;
654
655 /* note that while FEC does not have this bit
656 * it marks it as available for software use
657 * yay for hw reuse :) */
658 if (skb->len <= 60)
659 sc |= BD_ENET_TX_PAD;
660 CBDS_SC(bdp, sc);
661
662 (*fep->ops->tx_kickstart)(dev);
663
664 spin_unlock_irqrestore(&fep->tx_lock, flags);
665
666 return NETDEV_TX_OK;
667}
668
669static int fs_request_irq(struct net_device *dev, int irq, const char *name,
David Howells7d12e782006-10-05 14:55:46 +0100670 irq_handler_t irqf)
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400671{
672 struct fs_enet_private *fep = netdev_priv(dev);
673
674 (*fep->ops->pre_request_irq)(dev, irq);
Thomas Gleixner1fb9df52006-07-01 19:29:39 -0700675 return request_irq(irq, irqf, IRQF_SHARED, name, dev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400676}
677
678static void fs_free_irq(struct net_device *dev, int irq)
679{
680 struct fs_enet_private *fep = netdev_priv(dev);
681
682 free_irq(irq, dev);
683 (*fep->ops->post_free_irq)(dev, irq);
684}
685
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400686static void fs_timeout(struct net_device *dev)
687{
688 struct fs_enet_private *fep = netdev_priv(dev);
689 unsigned long flags;
690 int wake = 0;
691
692 fep->stats.tx_errors++;
693
694 spin_lock_irqsave(&fep->lock, flags);
695
696 if (dev->flags & IFF_UP) {
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700697 phy_stop(fep->phydev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400698 (*fep->ops->stop)(dev);
699 (*fep->ops->restart)(dev);
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700700 phy_start(fep->phydev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400701 }
702
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700703 phy_start(fep->phydev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400704 wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
705 spin_unlock_irqrestore(&fep->lock, flags);
706
707 if (wake)
708 netif_wake_queue(dev);
709}
710
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700711/*-----------------------------------------------------------------------------
712 * generic link-change handler - should be sufficient for most cases
713 *-----------------------------------------------------------------------------*/
714static void generic_adjust_link(struct net_device *dev)
715{
716 struct fs_enet_private *fep = netdev_priv(dev);
717 struct phy_device *phydev = fep->phydev;
718 int new_state = 0;
719
720 if (phydev->link) {
721
722 /* adjust to duplex mode */
723 if (phydev->duplex != fep->oldduplex){
724 new_state = 1;
725 fep->oldduplex = phydev->duplex;
726 }
727
728 if (phydev->speed != fep->oldspeed) {
729 new_state = 1;
730 fep->oldspeed = phydev->speed;
731 }
732
733 if (!fep->oldlink) {
734 new_state = 1;
735 fep->oldlink = 1;
736 netif_schedule(dev);
737 netif_carrier_on(dev);
738 netif_start_queue(dev);
739 }
740
741 if (new_state)
742 fep->ops->restart(dev);
743
744 } else if (fep->oldlink) {
745 new_state = 1;
746 fep->oldlink = 0;
747 fep->oldspeed = 0;
748 fep->oldduplex = -1;
749 netif_carrier_off(dev);
750 netif_stop_queue(dev);
751 }
752
753 if (new_state && netif_msg_link(fep))
754 phy_print_status(phydev);
755}
756
757
758static void fs_adjust_link(struct net_device *dev)
759{
760 struct fs_enet_private *fep = netdev_priv(dev);
761 unsigned long flags;
762
763 spin_lock_irqsave(&fep->lock, flags);
764
765 if(fep->ops->adjust_link)
766 fep->ops->adjust_link(dev);
767 else
768 generic_adjust_link(dev);
769
770 spin_unlock_irqrestore(&fep->lock, flags);
771}
772
773static int fs_init_phy(struct net_device *dev)
774{
775 struct fs_enet_private *fep = netdev_priv(dev);
776 struct phy_device *phydev;
777
778 fep->oldlink = 0;
779 fep->oldspeed = 0;
780 fep->oldduplex = -1;
781 if(fep->fpi->bus_id)
782 phydev = phy_connect(dev, fep->fpi->bus_id, &fs_adjust_link, 0);
783 else {
784 printk("No phy bus ID specified in BSP code\n");
785 return -EINVAL;
786 }
787 if (IS_ERR(phydev)) {
788 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
789 return PTR_ERR(phydev);
790 }
791
792 fep->phydev = phydev;
793
794 return 0;
795}
796
797
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400798static int fs_enet_open(struct net_device *dev)
799{
800 struct fs_enet_private *fep = netdev_priv(dev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400801 int r;
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700802 int err;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400803
804 /* Install our interrupt handler. */
805 r = fs_request_irq(dev, fep->interrupt, "fs_enet-mac", fs_enet_interrupt);
806 if (r != 0) {
807 printk(KERN_ERR DRV_MODULE_NAME
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700808 ": %s Could not allocate FS_ENET IRQ!", dev->name);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400809 return -EINVAL;
810 }
811
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700812 err = fs_init_phy(dev);
813 if(err)
814 return err;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400815
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700816 phy_start(fep->phydev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400817
818 return 0;
819}
820
821static int fs_enet_close(struct net_device *dev)
822{
823 struct fs_enet_private *fep = netdev_priv(dev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400824 unsigned long flags;
825
826 netif_stop_queue(dev);
827 netif_carrier_off(dev);
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700828 phy_stop(fep->phydev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400829
830 spin_lock_irqsave(&fep->lock, flags);
831 (*fep->ops->stop)(dev);
832 spin_unlock_irqrestore(&fep->lock, flags);
833
834 /* release any irqs */
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700835 phy_disconnect(fep->phydev);
836 fep->phydev = NULL;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400837 fs_free_irq(dev, fep->interrupt);
838
839 return 0;
840}
841
842static struct net_device_stats *fs_enet_get_stats(struct net_device *dev)
843{
844 struct fs_enet_private *fep = netdev_priv(dev);
845 return &fep->stats;
846}
847
848/*************************************************************************/
849
850static void fs_get_drvinfo(struct net_device *dev,
851 struct ethtool_drvinfo *info)
852{
853 strcpy(info->driver, DRV_MODULE_NAME);
854 strcpy(info->version, DRV_MODULE_VERSION);
855}
856
857static int fs_get_regs_len(struct net_device *dev)
858{
859 struct fs_enet_private *fep = netdev_priv(dev);
860
861 return (*fep->ops->get_regs_len)(dev);
862}
863
864static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
865 void *p)
866{
867 struct fs_enet_private *fep = netdev_priv(dev);
868 unsigned long flags;
869 int r, len;
870
871 len = regs->len;
872
873 spin_lock_irqsave(&fep->lock, flags);
874 r = (*fep->ops->get_regs)(dev, p, &len);
875 spin_unlock_irqrestore(&fep->lock, flags);
876
877 if (r == 0)
878 regs->version = 0;
879}
880
881static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
882{
883 struct fs_enet_private *fep = netdev_priv(dev);
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700884 return phy_ethtool_gset(fep->phydev, cmd);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400885}
886
887static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
888{
889 struct fs_enet_private *fep = netdev_priv(dev);
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700890 phy_ethtool_sset(fep->phydev, cmd);
891 return 0;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400892}
893
894static int fs_nway_reset(struct net_device *dev)
895{
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700896 return 0;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400897}
898
899static u32 fs_get_msglevel(struct net_device *dev)
900{
901 struct fs_enet_private *fep = netdev_priv(dev);
902 return fep->msg_enable;
903}
904
905static void fs_set_msglevel(struct net_device *dev, u32 value)
906{
907 struct fs_enet_private *fep = netdev_priv(dev);
908 fep->msg_enable = value;
909}
910
Jeff Garzik7282d492006-09-13 14:30:00 -0400911static const struct ethtool_ops fs_ethtool_ops = {
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400912 .get_drvinfo = fs_get_drvinfo,
913 .get_regs_len = fs_get_regs_len,
914 .get_settings = fs_get_settings,
915 .set_settings = fs_set_settings,
916 .nway_reset = fs_nway_reset,
917 .get_link = ethtool_op_get_link,
918 .get_msglevel = fs_get_msglevel,
919 .set_msglevel = fs_set_msglevel,
920 .get_tx_csum = ethtool_op_get_tx_csum,
921 .set_tx_csum = ethtool_op_set_tx_csum, /* local! */
922 .get_sg = ethtool_op_get_sg,
923 .set_sg = ethtool_op_set_sg,
924 .get_regs = fs_get_regs,
925};
926
927static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
928{
929 struct fs_enet_private *fep = netdev_priv(dev);
930 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data;
931 unsigned long flags;
932 int rc;
933
934 if (!netif_running(dev))
935 return -EINVAL;
936
937 spin_lock_irqsave(&fep->lock, flags);
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700938 rc = phy_mii_ioctl(fep->phydev, mii, cmd);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400939 spin_unlock_irqrestore(&fep->lock, flags);
940 return rc;
941}
942
943extern int fs_mii_connect(struct net_device *dev);
944extern void fs_mii_disconnect(struct net_device *dev);
945
946static struct net_device *fs_init_instance(struct device *dev,
Vitaly Bordug611a15a2006-09-21 22:38:05 +0400947 struct fs_platform_info *fpi)
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400948{
949 struct net_device *ndev = NULL;
950 struct fs_enet_private *fep = NULL;
951 int privsize, i, r, err = 0, registered = 0;
952
Vitaly Bordug611a15a2006-09-21 22:38:05 +0400953 fpi->fs_no = fs_get_id(fpi);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400954 /* guard */
955 if ((unsigned int)fpi->fs_no >= FS_MAX_INDEX)
956 return ERR_PTR(-EINVAL);
957
958 privsize = sizeof(*fep) + (sizeof(struct sk_buff **) *
959 (fpi->rx_ring + fpi->tx_ring));
960
961 ndev = alloc_etherdev(privsize);
962 if (!ndev) {
963 err = -ENOMEM;
964 goto err;
965 }
966 SET_MODULE_OWNER(ndev);
967
968 fep = netdev_priv(ndev);
969 memset(fep, 0, privsize); /* clear everything */
970
971 fep->dev = dev;
972 dev_set_drvdata(dev, ndev);
973 fep->fpi = fpi;
974 if (fpi->init_ioports)
Vitaly Bordugd3465c92006-09-21 22:38:05 +0400975 fpi->init_ioports((struct fs_platform_info *)fpi);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400976
977#ifdef CONFIG_FS_ENET_HAS_FEC
978 if (fs_get_fec_index(fpi->fs_no) >= 0)
979 fep->ops = &fs_fec_ops;
980#endif
981
982#ifdef CONFIG_FS_ENET_HAS_SCC
983 if (fs_get_scc_index(fpi->fs_no) >=0 )
984 fep->ops = &fs_scc_ops;
985#endif
986
987#ifdef CONFIG_FS_ENET_HAS_FCC
988 if (fs_get_fcc_index(fpi->fs_no) >= 0)
989 fep->ops = &fs_fcc_ops;
990#endif
991
992 if (fep->ops == NULL) {
993 printk(KERN_ERR DRV_MODULE_NAME
994 ": %s No matching ops found (%d).\n",
995 ndev->name, fpi->fs_no);
996 err = -EINVAL;
997 goto err;
998 }
999
1000 r = (*fep->ops->setup_data)(ndev);
1001 if (r != 0) {
1002 printk(KERN_ERR DRV_MODULE_NAME
1003 ": %s setup_data failed\n",
1004 ndev->name);
1005 err = r;
1006 goto err;
1007 }
1008
1009 /* point rx_skbuff, tx_skbuff */
1010 fep->rx_skbuff = (struct sk_buff **)&fep[1];
1011 fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
1012
1013 /* init locks */
1014 spin_lock_init(&fep->lock);
1015 spin_lock_init(&fep->tx_lock);
1016
1017 /*
1018 * Set the Ethernet address.
1019 */
1020 for (i = 0; i < 6; i++)
1021 ndev->dev_addr[i] = fpi->macaddr[i];
1022
1023 r = (*fep->ops->allocate_bd)(ndev);
1024
1025 if (fep->ring_base == NULL) {
1026 printk(KERN_ERR DRV_MODULE_NAME
1027 ": %s buffer descriptor alloc failed (%d).\n", ndev->name, r);
1028 err = r;
1029 goto err;
1030 }
1031
1032 /*
1033 * Set receive and transmit descriptor base.
1034 */
1035 fep->rx_bd_base = fep->ring_base;
1036 fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1037
1038 /* initialize ring size variables */
1039 fep->tx_ring = fpi->tx_ring;
1040 fep->rx_ring = fpi->rx_ring;
1041
1042 /*
1043 * The FEC Ethernet specific entries in the device structure.
1044 */
1045 ndev->open = fs_enet_open;
1046 ndev->hard_start_xmit = fs_enet_start_xmit;
1047 ndev->tx_timeout = fs_timeout;
1048 ndev->watchdog_timeo = 2 * HZ;
1049 ndev->stop = fs_enet_close;
1050 ndev->get_stats = fs_enet_get_stats;
1051 ndev->set_multicast_list = fs_set_multicast_list;
1052 if (fpi->use_napi) {
1053 ndev->poll = fs_enet_rx_napi;
1054 ndev->weight = fpi->napi_weight;
1055 }
1056 ndev->ethtool_ops = &fs_ethtool_ops;
1057 ndev->do_ioctl = fs_ioctl;
1058
1059 init_timer(&fep->phy_timer_list);
1060
1061 netif_carrier_off(ndev);
1062
1063 err = register_netdev(ndev);
1064 if (err != 0) {
1065 printk(KERN_ERR DRV_MODULE_NAME
1066 ": %s register_netdev failed.\n", ndev->name);
1067 goto err;
1068 }
1069 registered = 1;
1070
Pantelis Antoniou48257c42005-10-28 16:25:58 -04001071
1072 return ndev;
1073
1074 err:
1075 if (ndev != NULL) {
1076
1077 if (registered)
1078 unregister_netdev(ndev);
1079
1080 if (fep != NULL) {
1081 (*fep->ops->free_bd)(ndev);
1082 (*fep->ops->cleanup_data)(ndev);
1083 }
1084
1085 free_netdev(ndev);
1086 }
1087
1088 dev_set_drvdata(dev, NULL);
1089
1090 return ERR_PTR(err);
1091}
1092
1093static int fs_cleanup_instance(struct net_device *ndev)
1094{
1095 struct fs_enet_private *fep;
1096 const struct fs_platform_info *fpi;
1097 struct device *dev;
1098
1099 if (ndev == NULL)
1100 return -EINVAL;
1101
1102 fep = netdev_priv(ndev);
1103 if (fep == NULL)
1104 return -EINVAL;
1105
1106 fpi = fep->fpi;
1107
Pantelis Antoniou48257c42005-10-28 16:25:58 -04001108 unregister_netdev(ndev);
1109
1110 dma_free_coherent(fep->dev, (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t),
1111 fep->ring_base, fep->ring_mem_addr);
1112
1113 /* reset it */
1114 (*fep->ops->cleanup_data)(ndev);
1115
1116 dev = fep->dev;
1117 if (dev != NULL) {
1118 dev_set_drvdata(dev, NULL);
1119 fep->dev = NULL;
1120 }
1121
1122 free_netdev(ndev);
1123
1124 return 0;
1125}
1126
1127/**************************************************************************************/
1128
1129/* handy pointer to the immap */
1130void *fs_enet_immap = NULL;
1131
1132static int setup_immap(void)
1133{
1134 phys_addr_t paddr = 0;
1135 unsigned long size = 0;
1136
1137#ifdef CONFIG_CPM1
1138 paddr = IMAP_ADDR;
1139 size = 0x10000; /* map 64K */
1140#endif
1141
1142#ifdef CONFIG_CPM2
1143 paddr = CPM_MAP_ADDR;
1144 size = 0x40000; /* map 256 K */
1145#endif
1146 fs_enet_immap = ioremap(paddr, size);
1147 if (fs_enet_immap == NULL)
1148 return -EBADF; /* XXX ahem; maybe just BUG_ON? */
1149
1150 return 0;
1151}
1152
1153static void cleanup_immap(void)
1154{
1155 if (fs_enet_immap != NULL) {
1156 iounmap(fs_enet_immap);
1157 fs_enet_immap = NULL;
1158 }
1159}
1160
1161/**************************************************************************************/
1162
1163static int __devinit fs_enet_probe(struct device *dev)
1164{
1165 struct net_device *ndev;
1166
1167 /* no fixup - no device */
1168 if (dev->platform_data == NULL) {
1169 printk(KERN_INFO "fs_enet: "
1170 "probe called with no platform data; "
1171 "remove unused devices\n");
1172 return -ENODEV;
1173 }
1174
1175 ndev = fs_init_instance(dev, dev->platform_data);
1176 if (IS_ERR(ndev))
1177 return PTR_ERR(ndev);
1178 return 0;
1179}
1180
1181static int fs_enet_remove(struct device *dev)
1182{
1183 return fs_cleanup_instance(dev_get_drvdata(dev));
1184}
1185
1186static struct device_driver fs_enet_fec_driver = {
1187 .name = "fsl-cpm-fec",
1188 .bus = &platform_bus_type,
1189 .probe = fs_enet_probe,
1190 .remove = fs_enet_remove,
1191#ifdef CONFIG_PM
1192/* .suspend = fs_enet_suspend, TODO */
1193/* .resume = fs_enet_resume, TODO */
1194#endif
1195};
1196
1197static struct device_driver fs_enet_scc_driver = {
1198 .name = "fsl-cpm-scc",
1199 .bus = &platform_bus_type,
1200 .probe = fs_enet_probe,
1201 .remove = fs_enet_remove,
1202#ifdef CONFIG_PM
1203/* .suspend = fs_enet_suspend, TODO */
1204/* .resume = fs_enet_resume, TODO */
1205#endif
1206};
1207
1208static struct device_driver fs_enet_fcc_driver = {
1209 .name = "fsl-cpm-fcc",
1210 .bus = &platform_bus_type,
1211 .probe = fs_enet_probe,
1212 .remove = fs_enet_remove,
1213#ifdef CONFIG_PM
1214/* .suspend = fs_enet_suspend, TODO */
1215/* .resume = fs_enet_resume, TODO */
1216#endif
1217};
1218
1219static int __init fs_init(void)
1220{
1221 int r;
1222
1223 printk(KERN_INFO
1224 "%s", version);
1225
1226 r = setup_immap();
1227 if (r != 0)
1228 return r;
Pantelis Antoniou48257c42005-10-28 16:25:58 -04001229
Vitaly Bordug5b4b8452006-08-14 23:00:30 -07001230#ifdef CONFIG_FS_ENET_HAS_FCC
1231 /* let's insert mii stuff */
1232 r = fs_enet_mdio_bb_init();
1233
1234 if (r != 0) {
1235 printk(KERN_ERR DRV_MODULE_NAME
1236 "BB PHY init failed.\n");
1237 return r;
1238 }
Pantelis Antoniou48257c42005-10-28 16:25:58 -04001239 r = driver_register(&fs_enet_fcc_driver);
1240 if (r != 0)
1241 goto err;
Vitaly Bordug5b4b8452006-08-14 23:00:30 -07001242#endif
Pantelis Antoniou48257c42005-10-28 16:25:58 -04001243
Vitaly Bordug5b4b8452006-08-14 23:00:30 -07001244#ifdef CONFIG_FS_ENET_HAS_FEC
1245 r = fs_enet_mdio_fec_init();
1246 if (r != 0) {
1247 printk(KERN_ERR DRV_MODULE_NAME
1248 "FEC PHY init failed.\n");
1249 return r;
1250 }
1251
1252 r = driver_register(&fs_enet_fec_driver);
1253 if (r != 0)
1254 goto err;
1255#endif
1256
1257#ifdef CONFIG_FS_ENET_HAS_SCC
Pantelis Antoniou48257c42005-10-28 16:25:58 -04001258 r = driver_register(&fs_enet_scc_driver);
1259 if (r != 0)
1260 goto err;
Vitaly Bordug5b4b8452006-08-14 23:00:30 -07001261#endif
Pantelis Antoniou48257c42005-10-28 16:25:58 -04001262
1263 return 0;
1264err:
1265 cleanup_immap();
1266 return r;
1267
1268}
1269
1270static void __exit fs_cleanup(void)
1271{
1272 driver_unregister(&fs_enet_fec_driver);
1273 driver_unregister(&fs_enet_fcc_driver);
1274 driver_unregister(&fs_enet_scc_driver);
1275 cleanup_immap();
1276}
1277
1278/**************************************************************************************/
1279
1280module_init(fs_init);
1281module_exit(fs_cleanup);