fs_enet-main.c 33.4 KB
Newer Older
1 2 3
/*
 * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
 *
V
Vitaly Bordug 已提交
4
 * Copyright (c) 2003 Intracom S.A.
5
 *  by Pantelis Antoniou <panto@intracom.gr>
V
Vitaly Bordug 已提交
6 7
 *
 * 2005 (c) MontaVista Software, Inc.
8 9 10 11 12
 * Vitaly Bordug <vbordug@ru.mvista.com>
 *
 * Heavily based on original FEC driver by Dan Malek <dan@embeddededge.com>
 * and modifications by Joakim Tjernlund <joakim.tjernlund@lumentis.se>
 *
V
Vitaly Bordug 已提交
13 14
 * This file is licensed under the terms of the GNU General Public License
 * version 2. This program is licensed "as is" without any warranty of any
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 * kind, whether express or implied.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/ptrace.h>
#include <linux/errno.h>
#include <linux/ioport.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/spinlock.h>
#include <linux/mii.h>
#include <linux/ethtool.h>
#include <linux/bitops.h>
#include <linux/fs.h>
M
Marcelo Tosatti 已提交
37
#include <linux/platform_device.h>
38
#include <linux/phy.h>
39 40 41 42 43 44

#include <linux/vmalloc.h>
#include <asm/pgtable.h>
#include <asm/irq.h>
#include <asm/uaccess.h>

45
#ifdef CONFIG_PPC_CPM_NEW_BINDING
46
#include <linux/of_platform.h>
47 48
#endif

49 50 51 52
#include "fs_enet.h"

/*************************************************/

53
#ifndef CONFIG_PPC_CPM_NEW_BINDING
54 55
static char version[] __devinitdata =
    DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")" "\n";
56
#endif
57 58 59 60 61 62

MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
MODULE_DESCRIPTION("Freescale Ethernet Driver");
MODULE_LICENSE("GPL");
MODULE_VERSION(DRV_MODULE_VERSION);

S
Scott Wood 已提交
63
static int fs_enet_debug = -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
R
Rusty Russell 已提交
64
module_param(fs_enet_debug, int, 0);
65 66 67
MODULE_PARM_DESC(fs_enet_debug,
		 "Freescale bitmapped debugging message enable value");

V
Vitaly Bordug 已提交
68 69 70
#ifdef CONFIG_NET_POLL_CONTROLLER
static void fs_enet_netpoll(struct net_device *dev);
#endif
71 72 73 74 75 76 77 78

static void fs_set_multicast_list(struct net_device *dev)
{
	struct fs_enet_private *fep = netdev_priv(dev);

	(*fep->ops->set_multicast_list)(dev);
}

S
Scott Wood 已提交
79 80 81 82 83 84 85 86
static void skb_align(struct sk_buff *skb, int align)
{
	int off = ((unsigned long)skb->data) & (align - 1);

	if (off)
		skb_reserve(skb, align - off);
}

87
/* NAPI receive function */
88
static int fs_enet_rx_napi(struct napi_struct *napi, int budget)
89
{
90
	struct fs_enet_private *fep = container_of(napi, struct fs_enet_private, napi);
S
Scott Wood 已提交
91
	struct net_device *dev = fep->ndev;
92
	const struct fs_platform_info *fpi = fep->fpi;
S
Scott Wood 已提交
93
	cbd_t __iomem *bdp;
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	struct sk_buff *skb, *skbn, *skbt;
	int received = 0;
	u16 pkt_len, sc;
	int curidx;

	/*
	 * First, grab all of the stats for the incoming packet.
	 * These get messed up if we get called due to a busy condition.
	 */
	bdp = fep->cur_rx;

	/* clear RX status bits for napi*/
	(*fep->ops->napi_clear_rx_event)(dev);

	while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
		curidx = bdp - fep->rx_bd_base;

		/*
		 * Since we have allocated space to hold a complete frame,
		 * the last indicator should be set.
		 */
		if ((sc & BD_ENET_RX_LAST) == 0)
			printk(KERN_WARNING DRV_MODULE_NAME
			       ": %s rcv is not +last\n",
			       dev->name);

		/*
V
Vitaly Bordug 已提交
121
		 * Check for errors.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
		 */
		if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
			  BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
			fep->stats.rx_errors++;
			/* Frame too long or too short. */
			if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
				fep->stats.rx_length_errors++;
			/* Frame alignment */
			if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
				fep->stats.rx_frame_errors++;
			/* CRC Error */
			if (sc & BD_ENET_RX_CR)
				fep->stats.rx_crc_errors++;
			/* FIFO overrun */
			if (sc & BD_ENET_RX_OV)
				fep->stats.rx_crc_errors++;

			skb = fep->rx_skbuff[curidx];

141
			dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
142 143 144 145 146 147 148 149
				L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
				DMA_FROM_DEVICE);

			skbn = skb;

		} else {
			skb = fep->rx_skbuff[curidx];

150
			dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
				L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
				DMA_FROM_DEVICE);

			/*
			 * Process the incoming frame.
			 */
			fep->stats.rx_packets++;
			pkt_len = CBDR_DATLEN(bdp) - 4;	/* remove CRC */
			fep->stats.rx_bytes += pkt_len + 4;

			if (pkt_len <= fpi->rx_copybreak) {
				/* +2 to make IP header L1 cache aligned */
				skbn = dev_alloc_skb(pkt_len + 2);
				if (skbn != NULL) {
					skb_reserve(skbn, 2);	/* align IP header */
166 167
					skb_copy_from_linear_data(skb,
						      skbn->data, pkt_len);
168 169 170 171 172
					/* swap */
					skbt = skb;
					skb = skbn;
					skbn = skbt;
				}
S
Scott Wood 已提交
173
			} else {
174 175
				skbn = dev_alloc_skb(ENET_RX_FRSIZE);

S
Scott Wood 已提交
176 177 178 179
				if (skbn)
					skb_align(skbn, ENET_RX_ALIGN);
			}

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
			if (skbn != NULL) {
				skb_put(skb, pkt_len);	/* Make room */
				skb->protocol = eth_type_trans(skb, dev);
				received++;
				netif_receive_skb(skb);
			} else {
				printk(KERN_WARNING DRV_MODULE_NAME
				       ": %s Memory squeeze, dropping packet.\n",
				       dev->name);
				fep->stats.rx_dropped++;
				skbn = skb;
			}
		}

		fep->rx_skbuff[curidx] = skbn;
		CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
			     L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
			     DMA_FROM_DEVICE));
		CBDW_DATLEN(bdp, 0);
		CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);

		/*
V
Vitaly Bordug 已提交
202
		 * Update BD pointer to next entry.
203 204 205 206 207 208 209
		 */
		if ((sc & BD_ENET_RX_WRAP) == 0)
			bdp++;
		else
			bdp = fep->rx_bd_base;

		(*fep->ops->rx_bd_done)(dev);
210 211 212

		if (received >= budget)
			break;
213 214 215 216
	}

	fep->cur_rx = bdp;

S
Scott Wood 已提交
217
	if (received < budget) {
218 219 220 221 222
		/* done */
		netif_rx_complete(dev, napi);
		(*fep->ops->napi_enable_rx)(dev);
	}
	return received;
223 224 225 226 227 228 229
}

/* non NAPI receive function */
static int fs_enet_rx_non_napi(struct net_device *dev)
{
	struct fs_enet_private *fep = netdev_priv(dev);
	const struct fs_platform_info *fpi = fep->fpi;
S
Scott Wood 已提交
230
	cbd_t __iomem *bdp;
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	struct sk_buff *skb, *skbn, *skbt;
	int received = 0;
	u16 pkt_len, sc;
	int curidx;
	/*
	 * First, grab all of the stats for the incoming packet.
	 * These get messed up if we get called due to a busy condition.
	 */
	bdp = fep->cur_rx;

	while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {

		curidx = bdp - fep->rx_bd_base;

		/*
		 * Since we have allocated space to hold a complete frame,
		 * the last indicator should be set.
		 */
		if ((sc & BD_ENET_RX_LAST) == 0)
			printk(KERN_WARNING DRV_MODULE_NAME
			       ": %s rcv is not +last\n",
			       dev->name);

		/*
V
Vitaly Bordug 已提交
255
		 * Check for errors.
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
		 */
		if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
			  BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
			fep->stats.rx_errors++;
			/* Frame too long or too short. */
			if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
				fep->stats.rx_length_errors++;
			/* Frame alignment */
			if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
				fep->stats.rx_frame_errors++;
			/* CRC Error */
			if (sc & BD_ENET_RX_CR)
				fep->stats.rx_crc_errors++;
			/* FIFO overrun */
			if (sc & BD_ENET_RX_OV)
				fep->stats.rx_crc_errors++;

			skb = fep->rx_skbuff[curidx];

275
			dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
276 277 278 279 280 281 282 283 284
				L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
				DMA_FROM_DEVICE);

			skbn = skb;

		} else {

			skb = fep->rx_skbuff[curidx];

285
			dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
				L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
				DMA_FROM_DEVICE);

			/*
			 * Process the incoming frame.
			 */
			fep->stats.rx_packets++;
			pkt_len = CBDR_DATLEN(bdp) - 4;	/* remove CRC */
			fep->stats.rx_bytes += pkt_len + 4;

			if (pkt_len <= fpi->rx_copybreak) {
				/* +2 to make IP header L1 cache aligned */
				skbn = dev_alloc_skb(pkt_len + 2);
				if (skbn != NULL) {
					skb_reserve(skbn, 2);	/* align IP header */
301 302
					skb_copy_from_linear_data(skb,
						      skbn->data, pkt_len);
303 304 305 306 307
					/* swap */
					skbt = skb;
					skb = skbn;
					skbn = skbt;
				}
S
Scott Wood 已提交
308
			} else {
309 310
				skbn = dev_alloc_skb(ENET_RX_FRSIZE);

S
Scott Wood 已提交
311 312 313 314
				if (skbn)
					skb_align(skbn, ENET_RX_ALIGN);
			}

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
			if (skbn != NULL) {
				skb_put(skb, pkt_len);	/* Make room */
				skb->protocol = eth_type_trans(skb, dev);
				received++;
				netif_rx(skb);
			} else {
				printk(KERN_WARNING DRV_MODULE_NAME
				       ": %s Memory squeeze, dropping packet.\n",
				       dev->name);
				fep->stats.rx_dropped++;
				skbn = skb;
			}
		}

		fep->rx_skbuff[curidx] = skbn;
		CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
			     L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
			     DMA_FROM_DEVICE));
		CBDW_DATLEN(bdp, 0);
		CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);

		/*
V
Vitaly Bordug 已提交
337
		 * Update BD pointer to next entry.
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
		 */
		if ((sc & BD_ENET_RX_WRAP) == 0)
			bdp++;
		else
			bdp = fep->rx_bd_base;

		(*fep->ops->rx_bd_done)(dev);
	}

	fep->cur_rx = bdp;

	return 0;
}

static void fs_enet_tx(struct net_device *dev)
{
	struct fs_enet_private *fep = netdev_priv(dev);
S
Scott Wood 已提交
355
	cbd_t __iomem *bdp;
356 357 358 359
	struct sk_buff *skb;
	int dirtyidx, do_wake, do_restart;
	u16 sc;

360
	spin_lock(&fep->tx_lock);
361 362 363 364 365 366 367 368 369 370 371 372
	bdp = fep->dirty_tx;

	do_wake = do_restart = 0;
	while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
		dirtyidx = bdp - fep->tx_bd_base;

		if (fep->tx_free == fep->tx_ring)
			break;

		skb = fep->tx_skbuff[dirtyidx];

		/*
V
Vitaly Bordug 已提交
373
		 * Check for errors.
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
		 */
		if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
			  BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {

			if (sc & BD_ENET_TX_HB)	/* No heartbeat */
				fep->stats.tx_heartbeat_errors++;
			if (sc & BD_ENET_TX_LC)	/* Late collision */
				fep->stats.tx_window_errors++;
			if (sc & BD_ENET_TX_RL)	/* Retrans limit */
				fep->stats.tx_aborted_errors++;
			if (sc & BD_ENET_TX_UN)	/* Underrun */
				fep->stats.tx_fifo_errors++;
			if (sc & BD_ENET_TX_CSL)	/* Carrier lost */
				fep->stats.tx_carrier_errors++;

			if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
				fep->stats.tx_errors++;
				do_restart = 1;
			}
		} else
			fep->stats.tx_packets++;

		if (sc & BD_ENET_TX_READY)
			printk(KERN_WARNING DRV_MODULE_NAME
			       ": %s HEY! Enet xmit interrupt and TX_READY.\n",
			       dev->name);

		/*
		 * Deferred means some collisions occurred during transmit,
		 * but we eventually sent the packet OK.
		 */
		if (sc & BD_ENET_TX_DEF)
			fep->stats.collisions++;

		/* unmap */
409 410
		dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
				skb->len, DMA_TO_DEVICE);
411 412

		/*
V
Vitaly Bordug 已提交
413
		 * Free the sk buffer associated with this last transmit.
414 415 416 417 418
		 */
		dev_kfree_skb_irq(skb);
		fep->tx_skbuff[dirtyidx] = NULL;

		/*
V
Vitaly Bordug 已提交
419
		 * Update pointer to next buffer descriptor to be transmitted.
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
		 */
		if ((sc & BD_ENET_TX_WRAP) == 0)
			bdp++;
		else
			bdp = fep->tx_bd_base;

		/*
		 * Since we have freed up a buffer, the ring is no longer
		 * full.
		 */
		if (!fep->tx_free++)
			do_wake = 1;
	}

	fep->dirty_tx = bdp;

	if (do_restart)
		(*fep->ops->tx_restart)(dev);

439
	spin_unlock(&fep->tx_lock);
440 441 442 443 444 445 446 447 448 449

	if (do_wake)
		netif_wake_queue(dev);
}

/*
 * The interrupt handler.
 * This is called from the MPC core interrupt.
 */
static irqreturn_t
450
fs_enet_interrupt(int irq, void *dev_id)
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
{
	struct net_device *dev = dev_id;
	struct fs_enet_private *fep;
	const struct fs_platform_info *fpi;
	u32 int_events;
	u32 int_clr_events;
	int nr, napi_ok;
	int handled;

	fep = netdev_priv(dev);
	fpi = fep->fpi;

	nr = 0;
	while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) {
		nr++;

		int_clr_events = int_events;
		if (fpi->use_napi)
			int_clr_events &= ~fep->ev_napi_rx;

		(*fep->ops->clear_int_events)(dev, int_clr_events);

		if (int_events & fep->ev_err)
			(*fep->ops->ev_error)(dev, int_events);

		if (int_events & fep->ev_rx) {
			if (!fpi->use_napi)
				fs_enet_rx_non_napi(dev);
			else {
480
				napi_ok = napi_schedule_prep(&fep->napi);
481 482 483 484 485 486 487

				(*fep->ops->napi_disable_rx)(dev);
				(*fep->ops->clear_int_events)(dev, fep->ev_napi_rx);

				/* NOTE: it is possible for FCCs in NAPI mode    */
				/* to submit a spurious interrupt while in poll  */
				if (napi_ok)
488
					__netif_rx_schedule(dev, &fep->napi);
489 490 491 492 493 494 495 496 497 498 499 500 501 502
			}
		}

		if (int_events & fep->ev_tx)
			fs_enet_tx(dev);
	}

	handled = nr > 0;
	return IRQ_RETVAL(handled);
}

void fs_init_bds(struct net_device *dev)
{
	struct fs_enet_private *fep = netdev_priv(dev);
S
Scott Wood 已提交
503
	cbd_t __iomem *bdp;
504 505 506 507 508 509 510 511 512 513
	struct sk_buff *skb;
	int i;

	fs_cleanup_bds(dev);

	fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
	fep->tx_free = fep->tx_ring;
	fep->cur_rx = fep->rx_bd_base;

	/*
V
Vitaly Bordug 已提交
514
	 * Initialize the receive buffer descriptors.
515 516 517 518 519 520 521 522 523
	 */
	for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
		skb = dev_alloc_skb(ENET_RX_FRSIZE);
		if (skb == NULL) {
			printk(KERN_WARNING DRV_MODULE_NAME
			       ": %s Memory squeeze, unable to allocate skb\n",
			       dev->name);
			break;
		}
S
Scott Wood 已提交
524
		skb_align(skb, ENET_RX_ALIGN);
525 526 527 528 529 530 531 532 533 534
		fep->rx_skbuff[i] = skb;
		CBDW_BUFADDR(bdp,
			dma_map_single(fep->dev, skb->data,
				L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
				DMA_FROM_DEVICE));
		CBDW_DATLEN(bdp, 0);	/* zero */
		CBDW_SC(bdp, BD_ENET_RX_EMPTY |
			((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
	}
	/*
V
Vitaly Bordug 已提交
535
	 * if we failed, fillup remainder
536 537 538 539 540 541 542
	 */
	for (; i < fep->rx_ring; i++, bdp++) {
		fep->rx_skbuff[i] = NULL;
		CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
	}

	/*
V
Vitaly Bordug 已提交
543
	 * ...and the same for transmit.
544 545 546 547 548 549 550 551 552 553 554 555 556
	 */
	for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
		fep->tx_skbuff[i] = NULL;
		CBDW_BUFADDR(bdp, 0);
		CBDW_DATLEN(bdp, 0);
		CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
	}
}

void fs_cleanup_bds(struct net_device *dev)
{
	struct fs_enet_private *fep = netdev_priv(dev);
	struct sk_buff *skb;
S
Scott Wood 已提交
557
	cbd_t __iomem *bdp;
558 559 560
	int i;

	/*
V
Vitaly Bordug 已提交
561
	 * Reset SKB transmit buffers.
562
	 */
563
	for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
564 565 566 567
		if ((skb = fep->tx_skbuff[i]) == NULL)
			continue;

		/* unmap */
568 569
		dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
				skb->len, DMA_TO_DEVICE);
570 571 572 573 574 575

		fep->tx_skbuff[i] = NULL;
		dev_kfree_skb(skb);
	}

	/*
V
Vitaly Bordug 已提交
576
	 * Reset SKB receive buffers
577
	 */
578
	for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
579 580 581 582
		if ((skb = fep->rx_skbuff[i]) == NULL)
			continue;

		/* unmap */
583
		dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
584 585 586 587 588 589 590 591 592 593 594 595 596 597
			L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
			DMA_FROM_DEVICE);

		fep->rx_skbuff[i] = NULL;

		dev_kfree_skb(skb);
	}
}

/**********************************************************************************/

static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
{
	struct fs_enet_private *fep = netdev_priv(dev);
S
Scott Wood 已提交
598
	cbd_t __iomem *bdp;
599 600 601 602 603 604 605
	int curidx;
	u16 sc;
	unsigned long flags;

	spin_lock_irqsave(&fep->tx_lock, flags);

	/*
V
Vitaly Bordug 已提交
606
	 * Fill in a Tx ring entry
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
	 */
	bdp = fep->cur_tx;

	if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
		netif_stop_queue(dev);
		spin_unlock_irqrestore(&fep->tx_lock, flags);

		/*
		 * Ooops.  All transmit buffers are full.  Bail out.
		 * This should not happen, since the tx queue should be stopped.
		 */
		printk(KERN_WARNING DRV_MODULE_NAME
		       ": %s tx queue full!.\n", dev->name);
		return NETDEV_TX_BUSY;
	}

	curidx = bdp - fep->tx_bd_base;
	/*
V
Vitaly Bordug 已提交
625
	 * Clear all of the status flags.
626 627 628 629
	 */
	CBDC_SC(bdp, BD_ENET_TX_STATS);

	/*
V
Vitaly Bordug 已提交
630
	 * Save skb pointer.
631 632 633 634 635 636
	 */
	fep->tx_skbuff[curidx] = skb;

	fep->stats.tx_bytes += skb->len;

	/*
V
Vitaly Bordug 已提交
637
	 * Push the data cache so the CPM does not get stale memory data.
638 639 640 641 642 643 644 645
	 */
	CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
				skb->data, skb->len, DMA_TO_DEVICE));
	CBDW_DATLEN(bdp, skb->len);

	dev->trans_start = jiffies;

	/*
V
Vitaly Bordug 已提交
646
	 * If this was the last BD in the ring, start at the beginning again.
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
	 */
	if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
		fep->cur_tx++;
	else
		fep->cur_tx = fep->tx_bd_base;

	if (!--fep->tx_free)
		netif_stop_queue(dev);

	/* Trigger transmission start */
	sc = BD_ENET_TX_READY | BD_ENET_TX_INTR |
	     BD_ENET_TX_LAST | BD_ENET_TX_TC;

	/* note that while FEC does not have this bit
	 * it marks it as available for software use
	 * yay for hw reuse :) */
	if (skb->len <= 60)
		sc |= BD_ENET_TX_PAD;
	CBDS_SC(bdp, sc);

	(*fep->ops->tx_kickstart)(dev);

	spin_unlock_irqrestore(&fep->tx_lock, flags);

	return NETDEV_TX_OK;
}

static int fs_request_irq(struct net_device *dev, int irq, const char *name,
675
		irq_handler_t irqf)
676 677 678 679
{
	struct fs_enet_private *fep = netdev_priv(dev);

	(*fep->ops->pre_request_irq)(dev, irq);
680
	return request_irq(irq, irqf, IRQF_SHARED, name, dev);
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
}

static void fs_free_irq(struct net_device *dev, int irq)
{
	struct fs_enet_private *fep = netdev_priv(dev);

	free_irq(irq, dev);
	(*fep->ops->post_free_irq)(dev, irq);
}

static void fs_timeout(struct net_device *dev)
{
	struct fs_enet_private *fep = netdev_priv(dev);
	unsigned long flags;
	int wake = 0;

	fep->stats.tx_errors++;

	spin_lock_irqsave(&fep->lock, flags);

	if (dev->flags & IFF_UP) {
702
		phy_stop(fep->phydev);
703 704
		(*fep->ops->stop)(dev);
		(*fep->ops->restart)(dev);
705
		phy_start(fep->phydev);
706 707
	}

708
	phy_start(fep->phydev);
709 710 711 712 713 714 715
	wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
	spin_unlock_irqrestore(&fep->lock, flags);

	if (wake)
		netif_wake_queue(dev);
}

716 717 718 719 720
/*-----------------------------------------------------------------------------
 *  generic link-change handler - should be sufficient for most cases
 *-----------------------------------------------------------------------------*/
static void generic_adjust_link(struct  net_device *dev)
{
S
Scott Wood 已提交
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
	struct fs_enet_private *fep = netdev_priv(dev);
	struct phy_device *phydev = fep->phydev;
	int new_state = 0;

	if (phydev->link) {
		/* adjust to duplex mode */
		if (phydev->duplex != fep->oldduplex) {
			new_state = 1;
			fep->oldduplex = phydev->duplex;
		}

		if (phydev->speed != fep->oldspeed) {
			new_state = 1;
			fep->oldspeed = phydev->speed;
		}

		if (!fep->oldlink) {
			new_state = 1;
			fep->oldlink = 1;
			netif_schedule(dev);
			netif_carrier_on(dev);
			netif_start_queue(dev);
		}

		if (new_state)
			fep->ops->restart(dev);
	} else if (fep->oldlink) {
		new_state = 1;
		fep->oldlink = 0;
		fep->oldspeed = 0;
		fep->oldduplex = -1;
		netif_carrier_off(dev);
		netif_stop_queue(dev);
	}

	if (new_state && netif_msg_link(fep))
		phy_print_status(phydev);
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
}


static void fs_adjust_link(struct net_device *dev)
{
	struct fs_enet_private *fep = netdev_priv(dev);
	unsigned long flags;

	spin_lock_irqsave(&fep->lock, flags);

	if(fep->ops->adjust_link)
		fep->ops->adjust_link(dev);
	else
		generic_adjust_link(dev);

	spin_unlock_irqrestore(&fep->lock, flags);
}

static int fs_init_phy(struct net_device *dev)
{
	struct fs_enet_private *fep = netdev_priv(dev);
	struct phy_device *phydev;

	fep->oldlink = 0;
	fep->oldspeed = 0;
	fep->oldduplex = -1;
	if(fep->fpi->bus_id)
785 786
		phydev = phy_connect(dev, fep->fpi->bus_id, &fs_adjust_link, 0,
				PHY_INTERFACE_MODE_MII);
787 788 789 790 791 792 793 794 795 796 797 798 799 800
	else {
		printk("No phy bus ID specified in BSP code\n");
		return -EINVAL;
	}
	if (IS_ERR(phydev)) {
		printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
		return PTR_ERR(phydev);
	}

	fep->phydev = phydev;

	return 0;
}

801 802 803 804
static int fs_enet_open(struct net_device *dev)
{
	struct fs_enet_private *fep = netdev_priv(dev);
	int r;
805
	int err;
806

S
Scott Wood 已提交
807 808
	if (fep->fpi->use_napi)
		napi_enable(&fep->napi);
809

810 811 812 813
	/* Install our interrupt handler. */
	r = fs_request_irq(dev, fep->interrupt, "fs_enet-mac", fs_enet_interrupt);
	if (r != 0) {
		printk(KERN_ERR DRV_MODULE_NAME
814
		       ": %s Could not allocate FS_ENET IRQ!", dev->name);
S
Scott Wood 已提交
815 816
		if (fep->fpi->use_napi)
			napi_disable(&fep->napi);
817 818 819
		return -EINVAL;
	}

820
	err = fs_init_phy(dev);
S
Scott Wood 已提交
821 822 823
	if (err) {
		if (fep->fpi->use_napi)
			napi_disable(&fep->napi);
824
		return err;
825
	}
826
	phy_start(fep->phydev);
827 828 829 830 831 832 833 834 835 836 837

	return 0;
}

static int fs_enet_close(struct net_device *dev)
{
	struct fs_enet_private *fep = netdev_priv(dev);
	unsigned long flags;

	netif_stop_queue(dev);
	netif_carrier_off(dev);
838 839
	if (fep->fpi->use_napi)
		napi_disable(&fep->napi);
840
	phy_stop(fep->phydev);
841 842

	spin_lock_irqsave(&fep->lock, flags);
843
	spin_lock(&fep->tx_lock);
844
	(*fep->ops->stop)(dev);
845
	spin_unlock(&fep->tx_lock);
846 847 848
	spin_unlock_irqrestore(&fep->lock, flags);

	/* release any irqs */
849 850
	phy_disconnect(fep->phydev);
	fep->phydev = NULL;
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
	fs_free_irq(dev, fep->interrupt);

	return 0;
}

static struct net_device_stats *fs_enet_get_stats(struct net_device *dev)
{
	struct fs_enet_private *fep = netdev_priv(dev);
	return &fep->stats;
}

/*************************************************************************/

static void fs_get_drvinfo(struct net_device *dev,
			    struct ethtool_drvinfo *info)
{
	strcpy(info->driver, DRV_MODULE_NAME);
	strcpy(info->version, DRV_MODULE_VERSION);
}

static int fs_get_regs_len(struct net_device *dev)
{
	struct fs_enet_private *fep = netdev_priv(dev);

	return (*fep->ops->get_regs_len)(dev);
}

static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
			 void *p)
{
	struct fs_enet_private *fep = netdev_priv(dev);
	unsigned long flags;
	int r, len;

	len = regs->len;

	spin_lock_irqsave(&fep->lock, flags);
	r = (*fep->ops->get_regs)(dev, p, &len);
	spin_unlock_irqrestore(&fep->lock, flags);

	if (r == 0)
		regs->version = 0;
}

static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
{
	struct fs_enet_private *fep = netdev_priv(dev);
898 899 900 901

	if (!fep->phydev)
		return -ENODEV;

902
	return phy_ethtool_gset(fep->phydev, cmd);
903 904 905 906 907
}

static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
{
	struct fs_enet_private *fep = netdev_priv(dev);
908 909 910 911 912

	if (!fep->phydev)
		return -ENODEV;

	return phy_ethtool_sset(fep->phydev, cmd);
913 914 915 916
}

static int fs_nway_reset(struct net_device *dev)
{
917
	return 0;
918 919 920 921 922 923 924 925 926 927 928 929 930 931
}

static u32 fs_get_msglevel(struct net_device *dev)
{
	struct fs_enet_private *fep = netdev_priv(dev);
	return fep->msg_enable;
}

static void fs_set_msglevel(struct net_device *dev, u32 value)
{
	struct fs_enet_private *fep = netdev_priv(dev);
	fep->msg_enable = value;
}

932
static const struct ethtool_ops fs_ethtool_ops = {
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
	.get_drvinfo = fs_get_drvinfo,
	.get_regs_len = fs_get_regs_len,
	.get_settings = fs_get_settings,
	.set_settings = fs_set_settings,
	.nway_reset = fs_nway_reset,
	.get_link = ethtool_op_get_link,
	.get_msglevel = fs_get_msglevel,
	.set_msglevel = fs_set_msglevel,
	.set_tx_csum = ethtool_op_set_tx_csum,	/* local! */
	.set_sg = ethtool_op_set_sg,
	.get_regs = fs_get_regs,
};

static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
{
	struct fs_enet_private *fep = netdev_priv(dev);
	struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data;

	if (!netif_running(dev))
		return -EINVAL;

954
	return phy_mii_ioctl(fep->phydev, mii, cmd);
955 956 957 958 959
}

extern int fs_mii_connect(struct net_device *dev);
extern void fs_mii_disconnect(struct net_device *dev);

960
#ifndef CONFIG_PPC_CPM_NEW_BINDING
961
static struct net_device *fs_init_instance(struct device *dev,
962
		struct fs_platform_info *fpi)
963 964 965 966 967
{
	struct net_device *ndev = NULL;
	struct fs_enet_private *fep = NULL;
	int privsize, i, r, err = 0, registered = 0;

968
	fpi->fs_no = fs_get_id(fpi);
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
	/* guard */
	if ((unsigned int)fpi->fs_no >= FS_MAX_INDEX)
		return ERR_PTR(-EINVAL);

	privsize = sizeof(*fep) + (sizeof(struct sk_buff **) *
			    (fpi->rx_ring + fpi->tx_ring));

	ndev = alloc_etherdev(privsize);
	if (!ndev) {
		err = -ENOMEM;
		goto err;
	}

	fep = netdev_priv(ndev);

	fep->dev = dev;
	dev_set_drvdata(dev, ndev);
	fep->fpi = fpi;
	if (fpi->init_ioports)
988
		fpi->init_ioports((struct fs_platform_info *)fpi);
989 990 991 992 993 994 995

#ifdef CONFIG_FS_ENET_HAS_FEC
	if (fs_get_fec_index(fpi->fs_no) >= 0)
		fep->ops = &fs_fec_ops;
#endif

#ifdef CONFIG_FS_ENET_HAS_SCC
S
Scott Wood 已提交
996
	if (fs_get_scc_index(fpi->fs_no) >=0)
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
		fep->ops = &fs_scc_ops;
#endif

#ifdef CONFIG_FS_ENET_HAS_FCC
	if (fs_get_fcc_index(fpi->fs_no) >= 0)
		fep->ops = &fs_fcc_ops;
#endif

	if (fep->ops == NULL) {
		printk(KERN_ERR DRV_MODULE_NAME
		       ": %s No matching ops found (%d).\n",
		       ndev->name, fpi->fs_no);
		err = -EINVAL;
		goto err;
	}

	r = (*fep->ops->setup_data)(ndev);
	if (r != 0) {
		printk(KERN_ERR DRV_MODULE_NAME
		       ": %s setup_data failed\n",
			ndev->name);
		err = r;
		goto err;
	}

	/* point rx_skbuff, tx_skbuff */
	fep->rx_skbuff = (struct sk_buff **)&fep[1];
	fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;

	/* init locks */
	spin_lock_init(&fep->lock);
	spin_lock_init(&fep->tx_lock);

	/*
V
Vitaly Bordug 已提交
1031
	 * Set the Ethernet address.
1032 1033 1034
	 */
	for (i = 0; i < 6; i++)
		ndev->dev_addr[i] = fpi->macaddr[i];
V
Vitaly Bordug 已提交
1035

1036
	r = (*fep->ops->allocate_bd)(ndev);
V
Vitaly Bordug 已提交
1037

1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
	if (fep->ring_base == NULL) {
		printk(KERN_ERR DRV_MODULE_NAME
		       ": %s buffer descriptor alloc failed (%d).\n", ndev->name, r);
		err = r;
		goto err;
	}

	/*
	 * Set receive and transmit descriptor base.
	 */
	fep->rx_bd_base = fep->ring_base;
	fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;

	/* initialize ring size variables */
	fep->tx_ring = fpi->tx_ring;
	fep->rx_ring = fpi->rx_ring;

	/*
V
Vitaly Bordug 已提交
1056
	 * The FEC Ethernet specific entries in the device structure.
1057 1058 1059 1060 1061 1062 1063 1064
	 */
	ndev->open = fs_enet_open;
	ndev->hard_start_xmit = fs_enet_start_xmit;
	ndev->tx_timeout = fs_timeout;
	ndev->watchdog_timeo = 2 * HZ;
	ndev->stop = fs_enet_close;
	ndev->get_stats = fs_enet_get_stats;
	ndev->set_multicast_list = fs_set_multicast_list;
V
Vitaly Bordug 已提交
1065 1066 1067 1068 1069

#ifdef CONFIG_NET_POLL_CONTROLLER
	ndev->poll_controller = fs_enet_netpoll;
#endif

1070 1071 1072
	netif_napi_add(ndev, &fep->napi,
		       fs_enet_rx_napi, fpi->napi_weight);

1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
	ndev->ethtool_ops = &fs_ethtool_ops;
	ndev->do_ioctl = fs_ioctl;

	init_timer(&fep->phy_timer_list);

	netif_carrier_off(ndev);

	err = register_netdev(ndev);
	if (err != 0) {
		printk(KERN_ERR DRV_MODULE_NAME
		       ": %s register_netdev failed.\n", ndev->name);
		goto err;
	}
	registered = 1;


	return ndev;

S
Scott Wood 已提交
1091
err:
1092 1093 1094 1095
	if (ndev != NULL) {
		if (registered)
			unregister_netdev(ndev);

1096
		if (fep && fep->ops) {
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
			(*fep->ops->free_bd)(ndev);
			(*fep->ops->cleanup_data)(ndev);
		}

		free_netdev(ndev);
	}

	dev_set_drvdata(dev, NULL);

	return ERR_PTR(err);
}

static int fs_cleanup_instance(struct net_device *ndev)
{
	struct fs_enet_private *fep;
	const struct fs_platform_info *fpi;
	struct device *dev;

	if (ndev == NULL)
		return -EINVAL;

	fep = netdev_priv(ndev);
	if (fep == NULL)
		return -EINVAL;

	fpi = fep->fpi;

	unregister_netdev(ndev);

	dma_free_coherent(fep->dev, (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t),
S
Scott Wood 已提交
1127
			  (void __force *)fep->ring_base, fep->ring_mem_addr);
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141

	/* reset it */
	(*fep->ops->cleanup_data)(ndev);

	dev = fep->dev;
	if (dev != NULL) {
		dev_set_drvdata(dev, NULL);
		fep->dev = NULL;
	}

	free_netdev(ndev);

	return 0;
}
1142
#endif
1143 1144 1145 1146

/**************************************************************************************/

/* handy pointer to the immap */
S
Scott Wood 已提交
1147
void __iomem *fs_enet_immap = NULL;
1148 1149 1150 1151

static int setup_immap(void)
{
#ifdef CONFIG_CPM1
1152 1153 1154 1155
	fs_enet_immap = ioremap(IMAP_ADDR, 0x4000);
	WARN_ON(!fs_enet_immap);
#elif defined(CONFIG_CPM2)
	fs_enet_immap = cpm2_immr;
1156 1157 1158 1159 1160 1161 1162
#endif

	return 0;
}

static void cleanup_immap(void)
{
1163 1164 1165
#if defined(CONFIG_CPM1)
	iounmap(fs_enet_immap);
#endif
1166 1167 1168 1169
}

/**************************************************************************************/

1170 1171 1172 1173 1174 1175 1176
#ifdef CONFIG_PPC_CPM_NEW_BINDING
static int __devinit find_phy(struct device_node *np,
                              struct fs_platform_info *fpi)
{
	struct device_node *phynode, *mdionode;
	struct resource res;
	int ret = 0, len;
1177 1178 1179 1180
	const u32 *data;

	data  = of_get_property(np, "fixed-link", NULL);
	if (data) {
1181
		snprintf(fpi->bus_id, 16, "%x:%02x", 0, *data);
1182 1183
		return 0;
	}
1184

1185
	data = of_get_property(np, "phy-handle", &len);
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
	if (!data || len != 4)
		return -EINVAL;

	phynode = of_find_node_by_phandle(*data);
	if (!phynode)
		return -EINVAL;

	mdionode = of_get_parent(phynode);
	if (!mdionode)
		goto out_put_phy;

	ret = of_address_to_resource(mdionode, 0, &res);
	if (ret)
		goto out_put_mdio;

	data = of_get_property(phynode, "reg", &len);
	if (!data || len != 4)
		goto out_put_mdio;

1205
	snprintf(fpi->bus_id, 16, "%x:%02x", res.start, *data);
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244

out_put_mdio:
	of_node_put(mdionode);
out_put_phy:
	of_node_put(phynode);
	return ret;
}

#ifdef CONFIG_FS_ENET_HAS_FEC
#define IS_FEC(match) ((match)->data == &fs_fec_ops)
#else
#define IS_FEC(match) 0
#endif

static int __devinit fs_enet_probe(struct of_device *ofdev,
                                   const struct of_device_id *match)
{
	struct net_device *ndev;
	struct fs_enet_private *fep;
	struct fs_platform_info *fpi;
	const u32 *data;
	const u8 *mac_addr;
	int privsize, len, ret = -ENODEV;

	fpi = kzalloc(sizeof(*fpi), GFP_KERNEL);
	if (!fpi)
		return -ENOMEM;

	if (!IS_FEC(match)) {
		data = of_get_property(ofdev->node, "fsl,cpm-command", &len);
		if (!data || len != 4)
			goto out_free_fpi;

		fpi->cp_command = *data;
	}

	fpi->rx_ring = 32;
	fpi->tx_ring = 32;
	fpi->rx_copybreak = 240;
S
Scott Wood 已提交
1245
	fpi->use_napi = 1;
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
	fpi->napi_weight = 17;

	ret = find_phy(ofdev->node, fpi);
	if (ret)
		goto out_free_fpi;

	privsize = sizeof(*fep) +
	           sizeof(struct sk_buff **) *
	           (fpi->rx_ring + fpi->tx_ring);

	ndev = alloc_etherdev(privsize);
	if (!ndev) {
		ret = -ENOMEM;
		goto out_free_fpi;
	}

	dev_set_drvdata(&ofdev->dev, ndev);

	fep = netdev_priv(ndev);
	fep->dev = &ofdev->dev;
S
Scott Wood 已提交
1266
	fep->ndev = ndev;
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
	fep->fpi = fpi;
	fep->ops = match->data;

	ret = fep->ops->setup_data(ndev);
	if (ret)
		goto out_free_dev;

	fep->rx_skbuff = (struct sk_buff **)&fep[1];
	fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;

	spin_lock_init(&fep->lock);
	spin_lock_init(&fep->tx_lock);

	mac_addr = of_get_mac_address(ofdev->node);
	if (mac_addr)
		memcpy(ndev->dev_addr, mac_addr, 6);

	ret = fep->ops->allocate_bd(ndev);
	if (ret)
		goto out_cleanup_data;

	fep->rx_bd_base = fep->ring_base;
	fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;

	fep->tx_ring = fpi->tx_ring;
	fep->rx_ring = fpi->rx_ring;

	ndev->open = fs_enet_open;
	ndev->hard_start_xmit = fs_enet_start_xmit;
	ndev->tx_timeout = fs_timeout;
	ndev->watchdog_timeo = 2 * HZ;
	ndev->stop = fs_enet_close;
	ndev->get_stats = fs_enet_get_stats;
	ndev->set_multicast_list = fs_set_multicast_list;
S
Scott Wood 已提交
1301 1302 1303 1304 1305

	if (fpi->use_napi)
		netif_napi_add(ndev, &fep->napi, fs_enet_rx_napi,
		               fpi->napi_weight);

1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
	ndev->ethtool_ops = &fs_ethtool_ops;
	ndev->do_ioctl = fs_ioctl;

	init_timer(&fep->phy_timer_list);

	netif_carrier_off(ndev);

	ret = register_netdev(ndev);
	if (ret)
		goto out_free_bd;

	printk(KERN_INFO "%s: fs_enet: %02x:%02x:%02x:%02x:%02x:%02x\n",
	       ndev->name,
	       ndev->dev_addr[0], ndev->dev_addr[1], ndev->dev_addr[2],
	       ndev->dev_addr[3], ndev->dev_addr[4], ndev->dev_addr[5]);

	return 0;

out_free_bd:
	fep->ops->free_bd(ndev);
out_cleanup_data:
	fep->ops->cleanup_data(ndev);
out_free_dev:
	free_netdev(ndev);
	dev_set_drvdata(&ofdev->dev, NULL);
out_free_fpi:
	kfree(fpi);
	return ret;
}

static int fs_enet_remove(struct of_device *ofdev)
{
	struct net_device *ndev = dev_get_drvdata(&ofdev->dev);
	struct fs_enet_private *fep = netdev_priv(ndev);

	unregister_netdev(ndev);

	fep->ops->free_bd(ndev);
	fep->ops->cleanup_data(ndev);
	dev_set_drvdata(fep->dev, NULL);

	free_netdev(ndev);
	return 0;
}

static struct of_device_id fs_enet_match[] = {
#ifdef CONFIG_FS_ENET_HAS_SCC
	{
		.compatible = "fsl,cpm1-scc-enet",
		.data = (void *)&fs_scc_ops,
	},
#endif
#ifdef CONFIG_FS_ENET_HAS_FCC
	{
		.compatible = "fsl,cpm2-fcc-enet",
		.data = (void *)&fs_fcc_ops,
	},
#endif
#ifdef CONFIG_FS_ENET_HAS_FEC
	{
		.compatible = "fsl,pq1-fec-enet",
		.data = (void *)&fs_fec_ops,
	},
#endif
	{}
};

static struct of_platform_driver fs_enet_driver = {
	.name	= "fs_enet",
	.match_table = fs_enet_match,
	.probe = fs_enet_probe,
	.remove = fs_enet_remove,
};

static int __init fs_init(void)
{
	int r = setup_immap();
	if (r != 0)
		return r;

	r = of_register_platform_driver(&fs_enet_driver);
	if (r != 0)
		goto out;

	return 0;

out:
	cleanup_immap();
	return r;
}

static void __exit fs_cleanup(void)
{
	of_unregister_platform_driver(&fs_enet_driver);
	cleanup_immap();
}
#else
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
static int __devinit fs_enet_probe(struct device *dev)
{
	struct net_device *ndev;

	/* no fixup - no device */
	if (dev->platform_data == NULL) {
		printk(KERN_INFO "fs_enet: "
				"probe called with no platform data; "
				"remove unused devices\n");
		return -ENODEV;
	}

	ndev = fs_init_instance(dev, dev->platform_data);
	if (IS_ERR(ndev))
		return PTR_ERR(ndev);
	return 0;
}

static int fs_enet_remove(struct device *dev)
{
	return fs_cleanup_instance(dev_get_drvdata(dev));
}

static struct device_driver fs_enet_fec_driver = {
	.name	  	= "fsl-cpm-fec",
	.bus		= &platform_bus_type,
	.probe		= fs_enet_probe,
	.remove		= fs_enet_remove,
#ifdef CONFIG_PM
/*	.suspend	= fs_enet_suspend,	TODO */
/*	.resume		= fs_enet_resume,	TODO */
#endif
};

static struct device_driver fs_enet_scc_driver = {
	.name	  	= "fsl-cpm-scc",
	.bus		= &platform_bus_type,
	.probe		= fs_enet_probe,
	.remove		= fs_enet_remove,
#ifdef CONFIG_PM
/*	.suspend	= fs_enet_suspend,	TODO */
/*	.resume		= fs_enet_resume,	TODO */
#endif
};

static struct device_driver fs_enet_fcc_driver = {
	.name	  	= "fsl-cpm-fcc",
	.bus		= &platform_bus_type,
	.probe		= fs_enet_probe,
	.remove		= fs_enet_remove,
#ifdef CONFIG_PM
/*	.suspend	= fs_enet_suspend,	TODO */
/*	.resume		= fs_enet_resume,	TODO */
#endif
};

static int __init fs_init(void)
{
	int r;

	printk(KERN_INFO
			"%s", version);

	r = setup_immap();
	if (r != 0)
		return r;
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479

#ifdef CONFIG_FS_ENET_HAS_FCC
	/* let's insert mii stuff */
	r = fs_enet_mdio_bb_init();

	if (r != 0) {
		printk(KERN_ERR DRV_MODULE_NAME
			"BB PHY init failed.\n");
		return r;
	}
	r = driver_register(&fs_enet_fcc_driver);
1480 1481
	if (r != 0)
		goto err;
1482
#endif
1483

1484 1485 1486 1487 1488 1489 1490 1491 1492
#ifdef CONFIG_FS_ENET_HAS_FEC
	r =  fs_enet_mdio_fec_init();
	if (r != 0) {
		printk(KERN_ERR DRV_MODULE_NAME
			"FEC PHY init failed.\n");
		return r;
	}

	r = driver_register(&fs_enet_fec_driver);
1493 1494
	if (r != 0)
		goto err;
1495
#endif
1496

1497
#ifdef CONFIG_FS_ENET_HAS_SCC
1498 1499 1500
	r = driver_register(&fs_enet_scc_driver);
	if (r != 0)
		goto err;
1501
#endif
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515

	return 0;
err:
	cleanup_immap();
	return r;
}

static void __exit fs_cleanup(void)
{
	driver_unregister(&fs_enet_fec_driver);
	driver_unregister(&fs_enet_fcc_driver);
	driver_unregister(&fs_enet_scc_driver);
	cleanup_immap();
}
1516
#endif
1517

V
Vitaly Bordug 已提交
1518 1519 1520 1521 1522 1523 1524 1525 1526
#ifdef CONFIG_NET_POLL_CONTROLLER
static void fs_enet_netpoll(struct net_device *dev)
{
       disable_irq(dev->irq);
       fs_enet_interrupt(dev->irq, dev, NULL);
       enable_irq(dev->irq);
}
#endif

1527 1528 1529 1530
/**************************************************************************************/

module_init(fs_init);
module_exit(fs_cleanup);