fs_enet-main.c 26.9 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
#include <linux/of.h>
#include <linux/of_mdio.h>
41
#include <linux/of_platform.h>
42
#include <linux/of_gpio.h>
43
#include <linux/of_net.h>
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

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

#include "fs_enet.h"

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

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

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

V
Vitaly Bordug 已提交
64 65 66
#ifdef CONFIG_NET_POLL_CONTROLLER
static void fs_enet_netpoll(struct net_device *dev);
#endif
67 68 69 70 71 72 73 74

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 已提交
75 76 77 78 79 80 81 82
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);
}

83
/* NAPI receive function */
84
static int fs_enet_rx_napi(struct napi_struct *napi, int budget)
85
{
86
	struct fs_enet_private *fep = container_of(napi, struct fs_enet_private, napi);
S
Scott Wood 已提交
87
	struct net_device *dev = fep->ndev;
88
	const struct fs_platform_info *fpi = fep->fpi;
S
Scott Wood 已提交
89
	cbd_t __iomem *bdp;
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	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)
112
			dev_warn(fep->dev, "rcv is not +last\n");
113 114

		/*
V
Vitaly Bordug 已提交
115
		 * Check for errors.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
		 */
		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];

135
			dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
136 137 138 139 140 141 142 143
				L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
				DMA_FROM_DEVICE);

			skbn = skb;

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

144
			dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
				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 */
160 161
					skb_copy_from_linear_data(skb,
						      skbn->data, pkt_len);
162 163 164 165 166
					/* swap */
					skbt = skb;
					skb = skbn;
					skbn = skbt;
				}
S
Scott Wood 已提交
167
			} else {
168 169
				skbn = dev_alloc_skb(ENET_RX_FRSIZE);

S
Scott Wood 已提交
170 171 172 173
				if (skbn)
					skb_align(skbn, ENET_RX_ALIGN);
			}

174 175 176 177 178 179
			if (skbn != NULL) {
				skb_put(skb, pkt_len);	/* Make room */
				skb->protocol = eth_type_trans(skb, dev);
				received++;
				netif_receive_skb(skb);
			} else {
180 181
				dev_warn(fep->dev,
					 "Memory squeeze, dropping packet.\n");
182 183 184 185 186 187 188 189 190 191 192 193 194
				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 已提交
195
		 * Update BD pointer to next entry.
196 197 198 199 200 201 202
		 */
		if ((sc & BD_ENET_RX_WRAP) == 0)
			bdp++;
		else
			bdp = fep->rx_bd_base;

		(*fep->ops->rx_bd_done)(dev);
203 204 205

		if (received >= budget)
			break;
206 207 208 209
	}

	fep->cur_rx = bdp;

S
Scott Wood 已提交
210
	if (received < budget) {
211
		/* done */
212
		napi_complete(napi);
213 214 215
		(*fep->ops->napi_enable_rx)(dev);
	}
	return received;
216 217 218 219 220 221 222
}

/* 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 已提交
223
	cbd_t __iomem *bdp;
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	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)
243
			dev_warn(fep->dev, "rcv is not +last\n");
244 245

		/*
V
Vitaly Bordug 已提交
246
		 * Check for errors.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
		 */
		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];

266
			dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
267 268 269 270 271 272 273 274 275
				L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
				DMA_FROM_DEVICE);

			skbn = skb;

		} else {

			skb = fep->rx_skbuff[curidx];

276
			dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
				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 */
292 293
					skb_copy_from_linear_data(skb,
						      skbn->data, pkt_len);
294 295 296 297 298
					/* swap */
					skbt = skb;
					skb = skbn;
					skbn = skbt;
				}
S
Scott Wood 已提交
299
			} else {
300 301
				skbn = dev_alloc_skb(ENET_RX_FRSIZE);

S
Scott Wood 已提交
302 303 304 305
				if (skbn)
					skb_align(skbn, ENET_RX_ALIGN);
			}

306 307 308 309 310 311
			if (skbn != NULL) {
				skb_put(skb, pkt_len);	/* Make room */
				skb->protocol = eth_type_trans(skb, dev);
				received++;
				netif_rx(skb);
			} else {
312 313
				dev_warn(fep->dev,
					 "Memory squeeze, dropping packet.\n");
314 315 316 317 318 319 320 321 322 323 324 325 326
				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 已提交
327
		 * Update BD pointer to next entry.
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
		 */
		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 已提交
345
	cbd_t __iomem *bdp;
346 347 348 349
	struct sk_buff *skb;
	int dirtyidx, do_wake, do_restart;
	u16 sc;

350
	spin_lock(&fep->tx_lock);
351 352 353 354 355 356 357 358 359 360 361 362
	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 已提交
363
		 * Check for errors.
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
		 */
		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++;

386 387 388 389
		if (sc & BD_ENET_TX_READY) {
			dev_warn(fep->dev,
				 "HEY! Enet xmit interrupt and TX_READY.\n");
		}
390 391 392 393 394 395 396 397 398

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

		/* unmap */
399 400
		dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
				skb->len, DMA_TO_DEVICE);
401 402

		/*
V
Vitaly Bordug 已提交
403
		 * Free the sk buffer associated with this last transmit.
404 405 406 407 408
		 */
		dev_kfree_skb_irq(skb);
		fep->tx_skbuff[dirtyidx] = NULL;

		/*
V
Vitaly Bordug 已提交
409
		 * Update pointer to next buffer descriptor to be transmitted.
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
		 */
		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);

429
	spin_unlock(&fep->tx_lock);
430 431 432 433 434 435 436 437 438 439

	if (do_wake)
		netif_wake_queue(dev);
}

/*
 * The interrupt handler.
 * This is called from the MPC core interrupt.
 */
static irqreturn_t
440
fs_enet_interrupt(int irq, void *dev_id)
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
{
	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 {
470
				napi_ok = napi_schedule_prep(&fep->napi);
471 472 473 474 475 476 477

				(*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)
478
					__napi_schedule(&fep->napi);
479 480 481 482 483 484 485 486 487 488 489 490 491 492
			}
		}

		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 已提交
493
	cbd_t __iomem *bdp;
494 495 496 497 498 499 500 501 502 503
	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 已提交
504
	 * Initialize the receive buffer descriptors.
505 506 507 508
	 */
	for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
		skb = dev_alloc_skb(ENET_RX_FRSIZE);
		if (skb == NULL) {
509 510
			dev_warn(fep->dev,
				 "Memory squeeze, unable to allocate skb\n");
511 512
			break;
		}
S
Scott Wood 已提交
513
		skb_align(skb, ENET_RX_ALIGN);
514 515 516 517 518 519 520 521 522 523
		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 已提交
524
	 * if we failed, fillup remainder
525 526 527 528 529 530 531
	 */
	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 已提交
532
	 * ...and the same for transmit.
533 534 535 536 537 538 539 540 541 542 543 544 545
	 */
	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 已提交
546
	cbd_t __iomem *bdp;
547 548 549
	int i;

	/*
V
Vitaly Bordug 已提交
550
	 * Reset SKB transmit buffers.
551
	 */
552
	for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
553 554 555 556
		if ((skb = fep->tx_skbuff[i]) == NULL)
			continue;

		/* unmap */
557 558
		dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
				skb->len, DMA_TO_DEVICE);
559 560 561 562 563 564

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

	/*
V
Vitaly Bordug 已提交
565
	 * Reset SKB receive buffers
566
	 */
567
	for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
568 569 570 571
		if ((skb = fep->rx_skbuff[i]) == NULL)
			continue;

		/* unmap */
572
		dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
573 574 575 576 577 578 579 580 581 582 583
			L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
			DMA_FROM_DEVICE);

		fep->rx_skbuff[i] = NULL;

		dev_kfree_skb(skb);
	}
}

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

584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
#ifdef CONFIG_FS_ENET_MPC5121_FEC
/*
 * MPC5121 FEC requeries 4-byte alignment for TX data buffer!
 */
static struct sk_buff *tx_skb_align_workaround(struct net_device *dev,
					       struct sk_buff *skb)
{
	struct sk_buff *new_skb;
	struct fs_enet_private *fep = netdev_priv(dev);

	/* Alloc new skb */
	new_skb = dev_alloc_skb(skb->len + 4);
	if (!new_skb) {
		if (net_ratelimit()) {
			dev_warn(fep->dev,
				 "Memory squeeze, dropping tx packet.\n");
		}
		return NULL;
	}

	/* Make sure new skb is properly aligned */
	skb_align(new_skb, 4);

	/* Copy data to new skb ... */
	skb_copy_from_linear_data(skb, new_skb->data, skb->len);
	skb_put(new_skb, skb->len);

	/* ... and free an old one */
	dev_kfree_skb_any(skb);

	return new_skb;
}
#endif

618 619 620
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 已提交
621
	cbd_t __iomem *bdp;
622 623 624 625
	int curidx;
	u16 sc;
	unsigned long flags;

626 627 628 629 630 631 632 633 634 635 636 637 638
#ifdef CONFIG_FS_ENET_MPC5121_FEC
	if (((unsigned long)skb->data) & 0x3) {
		skb = tx_skb_align_workaround(dev, skb);
		if (!skb) {
			/*
			 * We have lost packet due to memory allocation error
			 * in tx_skb_align_workaround(). Hopefully original
			 * skb is still valid, so try transmit it later.
			 */
			return NETDEV_TX_BUSY;
		}
	}
#endif
639 640 641
	spin_lock_irqsave(&fep->tx_lock, flags);

	/*
V
Vitaly Bordug 已提交
642
	 * Fill in a Tx ring entry
643 644 645 646 647 648 649 650 651 652 653
	 */
	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.
		 */
654
		dev_warn(fep->dev, "tx queue full!.\n");
655 656 657 658 659
		return NETDEV_TX_BUSY;
	}

	curidx = bdp - fep->tx_bd_base;
	/*
V
Vitaly Bordug 已提交
660
	 * Clear all of the status flags.
661 662 663 664
	 */
	CBDC_SC(bdp, BD_ENET_TX_STATS);

	/*
V
Vitaly Bordug 已提交
665
	 * Save skb pointer.
666 667 668 669 670 671
	 */
	fep->tx_skbuff[curidx] = skb;

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

	/*
V
Vitaly Bordug 已提交
672
	 * Push the data cache so the CPM does not get stale memory data.
673 674 675 676 677 678
	 */
	CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
				skb->data, skb->len, DMA_TO_DEVICE));
	CBDW_DATLEN(bdp, skb->len);

	/*
V
Vitaly Bordug 已提交
679
	 * If this was the last BD in the ring, start at the beginning again.
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
	 */
	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);

700 701
	skb_tx_timestamp(skb);

702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
	(*fep->ops->tx_kickstart)(dev);

	spin_unlock_irqrestore(&fep->tx_lock, flags);

	return NETDEV_TX_OK;
}

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) {
720
		phy_stop(fep->phydev);
721 722
		(*fep->ops->stop)(dev);
		(*fep->ops->restart)(dev);
723
		phy_start(fep->phydev);
724 725
	}

726
	phy_start(fep->phydev);
727 728 729 730 731 732 733
	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);
}

734 735 736 737 738
/*-----------------------------------------------------------------------------
 *  generic link-change handler - should be sufficient for most cases
 *-----------------------------------------------------------------------------*/
static void generic_adjust_link(struct  net_device *dev)
{
S
Scott Wood 已提交
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
	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;
		}

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

	if (new_state && netif_msg_link(fep))
		phy_print_status(phydev);
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
}


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;
797 798 799 800 801 802

	phydev = of_phy_connect(dev, fep->fpi->phy_node, &fs_adjust_link, 0,
				PHY_INTERFACE_MODE_MII);
	if (!phydev) {
		phydev = of_phy_connect_fixed_link(dev, &fs_adjust_link,
						   PHY_INTERFACE_MODE_MII);
803
	}
804 805 806
	if (!phydev) {
		dev_err(&dev->dev, "Could not attach to PHY\n");
		return -ENODEV;
807 808 809 810 811 812 813
	}

	fep->phydev = phydev;

	return 0;
}

814 815 816 817
static int fs_enet_open(struct net_device *dev)
{
	struct fs_enet_private *fep = netdev_priv(dev);
	int r;
818
	int err;
819

820 821 822 823
	/* to initialize the fep->cur_rx,... */
	/* not doing this, will cause a crash in fs_enet_rx_napi */
	fs_init_bds(fep->ndev);

S
Scott Wood 已提交
824 825
	if (fep->fpi->use_napi)
		napi_enable(&fep->napi);
826

827
	/* Install our interrupt handler. */
828 829
	r = request_irq(fep->interrupt, fs_enet_interrupt, IRQF_SHARED,
			"fs_enet-mac", dev);
830
	if (r != 0) {
831
		dev_err(fep->dev, "Could not allocate FS_ENET IRQ!");
S
Scott Wood 已提交
832 833
		if (fep->fpi->use_napi)
			napi_disable(&fep->napi);
834 835 836
		return -EINVAL;
	}

837
	err = fs_init_phy(dev);
S
Scott Wood 已提交
838
	if (err) {
839
		free_irq(fep->interrupt, dev);
S
Scott Wood 已提交
840 841
		if (fep->fpi->use_napi)
			napi_disable(&fep->napi);
842
		return err;
843
	}
844
	phy_start(fep->phydev);
845

846 847
	netif_start_queue(dev);

848 849 850 851 852 853 854 855 856 857
	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);
858 859
	if (fep->fpi->use_napi)
		napi_disable(&fep->napi);
860
	phy_stop(fep->phydev);
861 862

	spin_lock_irqsave(&fep->lock, flags);
863
	spin_lock(&fep->tx_lock);
864
	(*fep->ops->stop)(dev);
865
	spin_unlock(&fep->tx_lock);
866 867 868
	spin_unlock_irqrestore(&fep->lock, flags);

	/* release any irqs */
869 870
	phy_disconnect(fep->phydev);
	fep->phydev = NULL;
871
	free_irq(fep->interrupt, dev);
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 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917

	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);
918 919 920 921

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

922
	return phy_ethtool_gset(fep->phydev, cmd);
923 924 925 926 927
}

static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
{
	struct fs_enet_private *fep = netdev_priv(dev);
928 929 930 931 932

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

	return phy_ethtool_sset(fep->phydev, cmd);
933 934 935 936
}

static int fs_nway_reset(struct net_device *dev)
{
937
	return 0;
938 939 940 941 942 943 944 945 946 947 948 949 950 951
}

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;
}

952
static const struct ethtool_ops fs_ethtool_ops = {
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
	.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,
	.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);

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

971
	return phy_mii_ioctl(fep->phydev, rq, cmd);
972 973 974 975 976 977 978
}

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

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

979 980 981 982 983 984
#ifdef CONFIG_FS_ENET_HAS_FEC
#define IS_FEC(match) ((match)->data == &fs_fec_ops)
#else
#define IS_FEC(match) 0
#endif

985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
static const struct net_device_ops fs_enet_netdev_ops = {
	.ndo_open		= fs_enet_open,
	.ndo_stop		= fs_enet_close,
	.ndo_get_stats		= fs_enet_get_stats,
	.ndo_start_xmit		= fs_enet_start_xmit,
	.ndo_tx_timeout		= fs_timeout,
	.ndo_set_multicast_list	= fs_set_multicast_list,
	.ndo_do_ioctl		= fs_ioctl,
	.ndo_validate_addr	= eth_validate_addr,
	.ndo_set_mac_address	= eth_mac_addr,
	.ndo_change_mtu		= eth_change_mtu,
#ifdef CONFIG_NET_POLL_CONTROLLER
	.ndo_poll_controller	= fs_enet_netpoll,
#endif
};

1001
static struct of_device_id fs_enet_match[];
1002
static int __devinit fs_enet_probe(struct platform_device *ofdev)
1003
{
1004
	const struct of_device_id *match;
1005 1006 1007 1008 1009 1010 1011
	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;

1012 1013
	match = of_match_device(fs_enet_match, &ofdev->dev);
	if (!match)
1014 1015
		return -EINVAL;

1016 1017 1018 1019
	fpi = kzalloc(sizeof(*fpi), GFP_KERNEL);
	if (!fpi)
		return -ENOMEM;

1020
	if (!IS_FEC(match)) {
1021
		data = of_get_property(ofdev->dev.of_node, "fsl,cpm-command", &len);
1022 1023 1024 1025 1026 1027 1028 1029 1030
		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 已提交
1031
	fpi->use_napi = 1;
1032
	fpi->napi_weight = 17;
1033 1034
	fpi->phy_node = of_parse_phandle(ofdev->dev.of_node, "phy-handle", 0);
	if ((!fpi->phy_node) && (!of_get_property(ofdev->dev.of_node, "fixed-link",
1035
						  NULL)))
1036 1037 1038 1039 1040 1041 1042 1043 1044
		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;
1045
		goto out_put;
1046 1047
	}

1048
	SET_NETDEV_DEV(ndev, &ofdev->dev);
1049 1050 1051 1052
	dev_set_drvdata(&ofdev->dev, ndev);

	fep = netdev_priv(ndev);
	fep->dev = &ofdev->dev;
S
Scott Wood 已提交
1053
	fep->ndev = ndev;
1054
	fep->fpi = fpi;
1055
	fep->ops = match->data;
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066

	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);

1067
	mac_addr = of_get_mac_address(ofdev->dev.of_node);
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
	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;

1081
	ndev->netdev_ops = &fs_enet_netdev_ops;
1082
	ndev->watchdog_timeo = 2 * HZ;
S
Scott Wood 已提交
1083 1084 1085 1086
	if (fpi->use_napi)
		netif_napi_add(ndev, &fep->napi, fs_enet_rx_napi,
		               fpi->napi_weight);

1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
	ndev->ethtool_ops = &fs_ethtool_ops;

	init_timer(&fep->phy_timer_list);

	netif_carrier_off(ndev);

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

1097
	pr_info("%s: fs_enet: %pM\n", ndev->name, ndev->dev_addr);
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107

	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);
1108
out_put:
1109
	of_node_put(fpi->phy_node);
1110 1111 1112 1113 1114
out_free_fpi:
	kfree(fpi);
	return ret;
}

1115
static int fs_enet_remove(struct platform_device *ofdev)
1116 1117 1118 1119 1120 1121 1122 1123 1124
{
	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);
1125
	of_node_put(fep->fpi->phy_node);
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
	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,
	},
1136 1137 1138 1139
	{
		.compatible = "fsl,cpm2-scc-enet",
		.data = (void *)&fs_scc_ops,
	},
1140 1141 1142 1143 1144 1145 1146 1147
#endif
#ifdef CONFIG_FS_ENET_HAS_FCC
	{
		.compatible = "fsl,cpm2-fcc-enet",
		.data = (void *)&fs_fcc_ops,
	},
#endif
#ifdef CONFIG_FS_ENET_HAS_FEC
1148 1149 1150 1151 1152 1153
#ifdef CONFIG_FS_ENET_MPC5121_FEC
	{
		.compatible = "fsl,mpc5121-fec",
		.data = (void *)&fs_fec_ops,
	},
#else
1154 1155 1156 1157
	{
		.compatible = "fsl,pq1-fec-enet",
		.data = (void *)&fs_fec_ops,
	},
1158
#endif
1159 1160 1161
#endif
	{}
};
1162
MODULE_DEVICE_TABLE(of, fs_enet_match);
1163

1164
static struct platform_driver fs_enet_driver = {
1165 1166 1167 1168 1169
	.driver = {
		.owner = THIS_MODULE,
		.name = "fs_enet",
		.of_match_table = fs_enet_match,
	},
1170 1171 1172 1173 1174 1175
	.probe = fs_enet_probe,
	.remove = fs_enet_remove,
};

static int __init fs_init(void)
{
1176
	return platform_driver_register(&fs_enet_driver);
1177 1178 1179 1180
}

static void __exit fs_cleanup(void)
{
1181
	platform_driver_unregister(&fs_enet_driver);
1182
}
1183

V
Vitaly Bordug 已提交
1184 1185 1186 1187
#ifdef CONFIG_NET_POLL_CONTROLLER
static void fs_enet_netpoll(struct net_device *dev)
{
       disable_irq(dev->irq);
A
Alexey Dobriyan 已提交
1188
       fs_enet_interrupt(dev->irq, dev);
V
Vitaly Bordug 已提交
1189 1190 1191 1192
       enable_irq(dev->irq);
}
#endif

1193 1194 1195 1196
/**************************************************************************************/

module_init(fs_init);
module_exit(fs_cleanup);