fec.c

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  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
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 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
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 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
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 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
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 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
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 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
 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
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 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
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 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
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
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
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
/*
 * Fast Ethernet Controller (FEC) driver.
 * Copyright (c) 2006 Michael Broughton (mbobowik@telusplanet.net)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */


#include <linux/config.h>
#include <linux/errno.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/kernel.h>
#include <linux/mii.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/phy.h>
#include <linux/platform_device.h>
#include <linux/skbuff.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/string.h>

#include "fec.h"




struct fec_private {
	volatile struct fec_csr *hw;

	unsigned long bdt_mem;
	unsigned long rb_mem;

	struct bd *tx_bd_ring;
	struct sk_buff **tx_sb_ring;
	unsigned long *tx_al_ring;
	struct bd *rx_bd_ring;
	unsigned long rx_bf_ring;

	unsigned long tx_head, tx_tail, rx_tail;

	unsigned long iaur, ialr, gaur, galr, rcr, tcr;

	int link, duplex, speed, promisc;

	int irqs[FEC_INTERRUPT_COUNT];

	spinlock_t lock;

	struct net_device_stats stats;

	struct platform_device *pdev;

	struct phy_device *phy;
	unsigned int phy_id;

	struct mii_bus mdio;
	int mdio_irqs[PHY_MAX_ADDR];
};




static int fec_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
				u16 value);
static int fec_mdio_read(struct mii_bus *bus, int mii_id, int regnum);
static void fec_adjust_link(struct net_device *dev);

static irqreturn_t fec_no_action(int irq, void *dev_id, struct pt_regs *regs);
static irqreturn_t fec_tx_err(int irq, void *dev_id, struct pt_regs *regs);
static irqreturn_t fec_eberr(int irq, void *dev_id, struct pt_regs *regs);
static irqreturn_t fec_finish_xmit(int irq, void *dev_id, struct pt_regs *regs);
static irqreturn_t fec_receive(int irq, void *dev_id, struct pt_regs *regs);
static int fec_request_interrupts(struct net_device *dev);
static void fec_free_interrupts(struct net_device *dev);

static int fec_hard_start_xmit(struct sk_buff *skb, struct net_device *dev);
static void fec_tx_timeout(struct net_device *dev);
static struct net_device_stats *fec_get_stats(struct net_device *dev);
static void fec_set_multicast_list(struct net_device *dev);
static int fec_open(struct net_device *dev);
static int fec_stop(struct net_device *dev);

static void fec_reset(struct net_device *dev);
static void fec_change_mode(struct net_device *dev);
static void fec_hard_reset(struct net_device *dev);

static int fec_res_init(struct net_device *dev);
static int fec_init(struct net_device *dev);
static void fec_cleanup(struct net_device *dev);
static int fec_mdio_init(struct net_device *dev);
static void fec_mdio_cleanup(struct net_device *dev);
static int fec_probe(struct platform_device *pdev);
static int fec_remove(struct platform_device *pdev);




static const struct {
	char *name;
	irqreturn_t (*handler)(int, void *, struct pt_regs *);
} fec_interrupts[FEC_INTERRUPT_COUNT] = {
	{ "fec(TXF)",	fec_finish_xmit },
	{ "fec(TXB)",	fec_no_action },
	{ "fec(UN)",	fec_tx_err },
	{ "fec(RL)",	fec_tx_err },
	{ "fec(RXF)",	fec_receive },
	{ "fec(RXB)",	fec_no_action },
	{ "fec(MII)",	fec_no_action },
	{ "fec(LC)",	fec_tx_err },
	{ "fec(HBERR)",	fec_tx_err },
	{ "fec(GRA)",	fec_no_action },
	{ "fec(EBERR)",	fec_eberr },
	{ "fec(BABT)",	fec_no_action },
	{ "fec(BABR)",	fec_no_action },
};


static const unsigned char fec_default_mac[] = {
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};




/*
 * Debug routines
 * *****************************************************************************
 */


static void fec_dump_rings(struct fec_private *priv)
{
	int i;

	fec_flush_dcache();

	printk(KERN_DEBUG "FEC Buffer Descriptor Dump:\n"
		"  tx_head 0x%08lx, tx_tail 0x%08lx, rx_tail 0x%08lx\n",
		priv->tx_head,
		priv->tx_tail,
		priv->rx_tail);

	printk(KERN_DEBUG " Tx Buffers: %lu\n", TX_BD_COUNT);
	for (i = 0; i < TX_BD_COUNT; i++)
		printk(KERN_DEBUG "  %p: 0x%04hx 0x%04hx 0x%08lx %p\n",
			&priv->tx_bd_ring[i],
			priv->tx_bd_ring[i].sc,
			priv->tx_bd_ring[i].length,
			priv->tx_bd_ring[i].address,
			priv->tx_sb_ring[i]);

	printk(KERN_DEBUG " Rx Buffers: %lu\n", RX_BD_COUNT);
	for (i = 0; i < RX_BD_COUNT; i++)
		printk(KERN_DEBUG "  %p: 0x%04hx 0x%04hx 0x%08lx\n",
			&priv->rx_bd_ring[i],
			priv->rx_bd_ring[i].sc,
			priv->rx_bd_ring[i].length,
			priv->rx_bd_ring[i].address);
}




/*
 * PHY layer callback routines
 * *****************************************************************************
 */


static int fec_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
				u16 value)
{
	struct fec_private *priv = netdev_priv(bus->priv);
	unsigned long frame, result;

	frame = FEC_MMFR_WRITE(mii_id, regnum, value);

	spin_lock(&priv->lock);

	priv->hw->MMFR = frame;

	while (!(priv->hw->EIR & FEC_EIR_MII))
		cpu_relax();

	result = priv->hw->MMFR;
	priv->hw->EIR = FEC_EIR_MII;

	spin_unlock(&priv->lock);

	return 0;
}


static int fec_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
{
	struct fec_private *priv = netdev_priv(bus->priv);
	unsigned long frame, result;

	frame = FEC_MMFR_READ(mii_id, regnum);

	spin_lock(&priv->lock);

	priv->hw->MMFR = frame;

	while (!(priv->hw->EIR & FEC_EIR_MII))
		cpu_relax();

	result = priv->hw->MMFR;
	priv->hw->EIR = FEC_EIR_MII;

	spin_unlock(&priv->lock);

	return (int)(result & FEC_MMFR_READ_MASK);
}


static void fec_adjust_link(struct net_device *dev)
{
	struct fec_private *priv = netdev_priv(dev);
	unsigned long flags;

	if (priv->phy->link != priv->link ||
		priv->phy->duplex != priv->duplex ||
		priv->phy->speed != priv->speed) {
		netif_tx_disable(dev);

		spin_lock_irqsave(&priv->lock, flags);

		priv->link = priv->phy->link;
		priv->duplex = priv->phy->duplex;
		priv->speed = priv->phy->speed;

		fec_change_mode(dev);

		spin_unlock_irqrestore(&priv->lock, flags);

		if (priv->link) {
			netif_wake_queue(dev);
			pr_info("%s: Link Up %sMbit %s Duplex (phy%s)\n",
				dev->name,
				priv->speed == SPEED_100 ? "100" : "10",
				priv->duplex == DUPLEX_FULL ? "Full" : "Half",
				priv->phy->dev.bus_id);
		} else
			pr_info("%s: Link Down (phy%s)\n", 
				dev->name,
				priv->phy->dev.bus_id);
	}
}




/*
 * Interrupt setup and handlers
 * *****************************************************************************
 */


static irqreturn_t fec_no_action(int irq, void *dev_id, struct pt_regs *regs)
{
	struct net_device *dev = dev_id;
	struct fec_private *priv = netdev_priv(dev);

	if (printk_ratelimit())
		printk(KERN_CRIT
			"%s: Unexpected or spurious interrupt (0x%lx, 0x%lx)\n",
			dev->name, priv->hw->EIR, priv->hw->EIMR);

	return IRQ_NONE;
}


static irqreturn_t fec_tx_err(int irq, void *dev_id, struct pt_regs *regs)
{
	struct net_device *dev = dev_id;
	struct fec_private *priv = netdev_priv(dev);
	unsigned long eir;

	eir = priv->hw->EIR & FEC_EIR_ERR;

	do {
		priv->hw->EIR = eir;
		if (eir & FEC_EIR_HBERR)
			priv->stats.tx_heartbeat_errors++;
		if (eir & FEC_EIR_LC)
			priv->stats.tx_window_errors++;
		if (eir & FEC_EIR_RL)
			priv->stats.tx_aborted_errors++;
		if (eir & FEC_EIR_UN)
			priv->stats.tx_fifo_errors++;
	} while ((eir = priv->hw->EIR & FEC_EIR_ERR));

	return IRQ_HANDLED;
}


static irqreturn_t fec_eberr(int irq, void *dev_id, struct pt_regs *regs)
{
	struct net_device *dev = dev_id;

	printk(KERN_CRIT "%s: Ethernet Bus Error\n", dev->name);
	BUG();

	return IRQ_HANDLED;
}


static irqreturn_t fec_finish_xmit(int irq, void *dev_id, struct pt_regs *regs)
{
	struct net_device *dev = dev_id;
	struct fec_private *priv = netdev_priv(dev);
	unsigned long free_bds = 0;
	struct bd *bd;

	while (1) {
		bd = &priv->tx_bd_ring[priv->tx_tail];

		fec_flush_dcache_line(bd);

		if (bd->sc & FEC_TX_BD_SC_R)
			break;

		if (!(bd->sc & FEC_TX_BD_SC_TO1))
			break;

		if (bd->sc & FEC_TX_BD_SC_L) {
			if (!priv->tx_sb_ring[priv->tx_tail]) {
				printk(KERN_CRIT "%s: Tx Ring error!\n",
					dev->name);
				fec_dump_rings(priv);
				BUG();
			}

			dev_kfree_skb_any(priv->tx_sb_ring[priv->tx_tail]);
			priv->tx_sb_ring[priv->tx_tail] = NULL;

			priv->stats.tx_packets++;
		}

		priv->stats.tx_bytes += bd->length;

		bd->sc = (bd->sc & FEC_TX_BD_SC_W) ? FEC_TX_BD_SC_W : 0;

		priv->tx_tail = TX_BD_IDX_INC(priv->tx_tail);

		free_bds++;
	}

	priv->hw->EIR = FEC_EIR_TXF;

	if (free_bds > 1 && netif_queue_stopped(dev))
		netif_wake_queue(dev);

	return IRQ_HANDLED;
}


static irqreturn_t fec_receive(int irq, void *dev_id, struct pt_regs *regs)
{
	struct net_device *dev = dev_id;
	struct fec_private *priv = netdev_priv(dev);
	struct bd *bd;
	struct sk_buff *skb;
	unsigned long start, end;
	unsigned short len, err;

	/* Consume as many complete frames as possible. */
	while (1) {

		start = end = priv->rx_tail;

		/* Get the index range for an entire frame, or we are done */
		while (1) {
			bd = &priv->rx_bd_ring[end];

			if (bd->sc & FEC_RX_BD_SC_E) {
				fec_flush_dcache_line(bd);
				if (bd->sc & FEC_RX_BD_SC_E)
					goto receive_done;
			}

			if (bd->sc & FEC_RX_BD_SC_L)
				break;

			end = RX_BD_IDX_INC(end);

			/* A single frame cannot use up all of the buffers. */
			if (start == end) {
				printk(KERN_CRIT "%s: Rx Ring error!\n",
					dev->name);
				fec_dump_rings(priv);
				BUG();
			}
		}

		priv->rx_tail = RX_BD_IDX_INC(end);

		if (bd->sc & FEC_RX_BD_SC_ERR) {
			priv->stats.rx_errors++;

			if (bd->sc & FEC_RX_BD_SC_NO)
				priv->stats.rx_frame_errors++;
			if (bd->sc & FEC_RX_BD_SC_CR)
				priv->stats.rx_crc_errors++;
			if (bd->sc & FEC_RX_BD_SC_OV)
				priv->stats.rx_fifo_errors++;
			if (bd->sc & FEC_RX_BD_SC_TR)
				priv->stats.rx_frame_errors++;

			err = 1;
		} else {
			len = bd->length - 4;
			skb = dev_alloc_skb(len + 2);
			if (skb) {
				skb_reserve(skb, 2);
				err = 0;
			} else {
				if (printk_ratelimit())
					printk(KERN_NOTICE
						"%s: Low on memory, "
						"dropping packet\n",
						dev->name);
				priv->stats.rx_dropped++;
				err = 1;
			}
		}

		if (err) {
			/* The frame has an error, so drop it. */
			while (start != end) {
				bd = &priv->rx_bd_ring[start];
				if (bd->sc & FEC_RX_BD_SC_W)
					bd->sc = FEC_RX_BD_SC_E |
							FEC_RX_BD_SC_W;
				else
					bd->sc = FEC_RX_BD_SC_E;
				start = RX_BD_IDX_INC(start);
			}
			bd = &priv->rx_bd_ring[end];
			if (bd->sc & FEC_RX_BD_SC_W)
				bd->sc = FEC_RX_BD_SC_E | FEC_RX_BD_SC_W;
			else
				bd->sc = FEC_RX_BD_SC_E;
		} else {
			/*
			 * Make sure the receive buffers holding the frame are
			 * flushed from the data cache.
			 */
			fec_flush_dcache();

			while (start != end) {
				bd = &priv->rx_bd_ring[start];
				memcpy(skb_put(skb, RX_BUF_LEN),
						(void *)bd->address,
						RX_BUF_LEN);
				len -= RX_BUF_LEN;
				if (bd->sc & FEC_RX_BD_SC_W)
					bd->sc = FEC_RX_BD_SC_E |
							FEC_RX_BD_SC_W;
				else
					bd->sc = FEC_RX_BD_SC_E;
				start = RX_BD_IDX_INC(start);
			}
			bd = &priv->rx_bd_ring[end];
			memcpy(skb_put(skb, len), (void *)bd->address, len);
			if (bd->sc & FEC_RX_BD_SC_W)
				bd->sc = FEC_RX_BD_SC_E | FEC_RX_BD_SC_W;
			else
				bd->sc = FEC_RX_BD_SC_E;

			skb->dev = dev;
			skb->protocol = eth_type_trans(skb, dev);

			switch (netif_rx(skb)) {
			case NET_RX_CN_LOW:
			case NET_RX_CN_MOD:
			case NET_RX_CN_HIGH:
				/*
				 * What can I do here?
				 * If flow control is enabled, send a pause
				 * frame?
				 */
			case NET_RX_SUCCESS:
				priv->stats.rx_packets++;
				priv->stats.rx_bytes += bd->length;
				if (bd->sc & FEC_RX_BD_SC_MC)
					priv->stats.multicast++;
				if (bd->sc & FEC_RX_BD_SC_LG)
					priv->stats.rx_length_errors++;
				break;
			case NET_RX_DROP:
			case NET_RX_BAD:
				priv->stats.rx_dropped++;
				break;
			}
		}

		priv->hw->RDAR = FEC_RDAR_ACTIVE;
	}

receive_done:
	priv->hw->EIR = FEC_EIR_RXF;

	return IRQ_HANDLED;
}


static int fec_request_interrupts(struct net_device *dev)
{
	struct fec_private *priv = netdev_priv(dev);
	int i, j;

	for (i = 0; i < FEC_INTERRUPT_COUNT; i++) {
		if (request_irq(priv->irqs[i], fec_interrupts[i].handler, 0,
				fec_interrupts[i].name, dev)) {
			printk(KERN_ERR "%s: Could not request irq %d - %s!\n",
				dev->name, priv->irqs[i],
				fec_interrupts[i].name);
			for (j = 0; j < i; j++)
				free_irq(priv->irqs[j], dev);
			return -EBUSY;
		}
	}

	return 0;
}


static void fec_free_interrupts(struct net_device *dev)
{
	struct fec_private *priv = netdev_priv(dev);
	int i;

	for (i = 0; i < FEC_INTERRUPT_COUNT; i++)
		free_irq(priv->irqs[i], dev);
}




/*
 * Netdev routines
 * *****************************************************************************
 */


static int fec_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
{
	struct fec_private *priv = netdev_priv(dev);
	unsigned long free_bds, data;
	unsigned short len;
	struct bd *bd, *al_bd = NULL;

	bd = &priv->tx_bd_ring[priv->tx_head];

	free_bds = TX_BD_IDX_DIST(priv->tx_head, priv->tx_tail);
	if (!free_bds) {
		fec_flush_dcache_line(bd);
		if (!(bd->sc & FEC_TX_BD_SC_TO1))
			free_bds = TX_BD_COUNT;
	}

	if (free_bds <= 1)
	{
		printk(KERN_CRIT "%s: Tx Ring full when queue awake!\n",
			dev->name);
		fec_dump_rings(priv);
		BUG();
	}

	/* Make sure the data buffer is aligned on a four byte boundary. */
	if (TX_SKB_UNALIGNED(skb->data)) {
		/*
		 * The data buffer is out of alignment. We need to copy the
		 * first one, two, or three bytes to an alignment buffer.
		 * 
		 * It seems that the normal case is two bytes out of alignment.
		 */
		unsigned long *al = &priv->tx_al_ring[priv->tx_head];

		al_bd = bd;
		len = 4 - TX_SKB_UNALIGNED(skb->data);

		memcpy(al, skb->data, len);

		al_bd->address = (unsigned long)al;
		al_bd->length = len;

		if (al_bd->sc & FEC_TX_BD_SC_W)
			al_bd->sc = FEC_TX_BD_SC_TO1 | FEC_TX_BD_SC_W;
		else
			al_bd->sc = FEC_TX_BD_SC_TO1;

		priv->tx_head = TX_BD_IDX_INC(priv->tx_head);

		free_bds--;

		bd = &priv->tx_bd_ring[priv->tx_head];

		/*
		 * The wrap bit in the bd sc field, is the only bit in this bd
		 * that needs to be checked. Since that bit is never modified
		 * by the fec hardware, it is not nessessary to flush the bd
		 * from the dcache.
		 *
		fec_flush_dcache_line(bd);
		 */

		data = (unsigned long)skb->data + len;
		len = (unsigned short)skb->len - len;
	} else {
		data = (unsigned long)skb->data;
		len = (unsigned short)skb->len;
	}

	priv->tx_sb_ring[priv->tx_head] = skb;

	bd->address = data;
	bd->length = len;

	if (bd->sc & FEC_TX_BD_SC_W)
		bd->sc = FEC_TX_BD_SC_R | FEC_TX_BD_SC_TO1 | FEC_TX_BD_SC_W |
				FEC_TX_BD_SC_L | FEC_TX_BD_SC_TC;
	else
		bd->sc = FEC_TX_BD_SC_R | FEC_TX_BD_SC_TO1 | FEC_TX_BD_SC_L |
				FEC_TX_BD_SC_TC;

	priv->tx_head = TX_BD_IDX_INC(priv->tx_head);

	free_bds--;

	if (al_bd)
		al_bd->sc |= FEC_TX_BD_SC_R;

	dev->trans_start = jiffies;

	priv->hw->TDAR = FEC_TDAR_ACTIVE;

	if (free_bds <= 1)
		netif_stop_queue(dev);

	return NETDEV_TX_OK;
}


static void fec_tx_timeout(struct net_device *dev)
{
	struct fec_private *priv = netdev_priv(dev);
	unsigned long flags;

	spin_lock_irqsave(&priv->lock, flags);

	fec_finish_xmit(0, dev, NULL);

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


static struct net_device_stats *fec_get_stats(struct net_device *dev)
{
	struct fec_private *priv = netdev_priv(dev);

	return &priv->stats;
}


static void fec_set_multicast_list(struct net_device *dev)
{
	struct fec_private *priv = netdev_priv(dev);
	unsigned long flags, iaur, ialr, gaur, galr;
	struct dev_mc_list *dmi;
	unsigned int i, j, bit, data, crc, promisc;
	unsigned char hash;

	iaur = ialr = 0;
	gaur = galr = (dev->flags & IFF_ALLMULTI) ? 0xffffffff : 0;

	for (j = 0, dmi = dev->mc_list; j < dev->mc_count; j++, dmi = dmi->next)
	{
		if (is_multicast_ether_addr(dmi->dmi_addr) &&
			dev->flags & IFF_ALLMULTI)
			continue;

		crc = 0xffffffff;

		/*
		 * This is going to be slow. Anyone who needs to change the
		 * multicast list often or has a large mc_count will want to
		 * consider optimizing this.
		 */
		for (i = 0; i < dmi->dmi_addrlen; i++) {
			data = dmi->dmi_addr[i];
			for (bit = 0; bit < 8; bit++, data >>= 1)
				crc = (crc >> 1) ^ (((crc ^ data) & 1) ?
					CRC32_POLY : 0);
		}

		hash = CRC2HASH(crc);

		if (dmi->dmi_addr[0] & 1) {
			if (hash > 31)
				gaur |= 1 << (hash - 32);
			else
				galr |= 1 << hash;
		} else {
			if (hash > 31)
				iaur |= 1 << (hash - 32);
			else
				ialr |= 1 << hash;
		}
	}

	promisc = (dev->flags & IFF_PROMISC) ? 1 : 0;

	/* No need to stop the tx queue, the xmit_lock spin lock is locked */
	spin_lock_irqsave(&priv->lock, flags);

	if (priv->promisc == promisc) {
		priv->hw->IAUR = priv->iaur = iaur;
		priv->hw->IALR = priv->ialr = ialr;
		priv->hw->GAUR = priv->gaur = gaur;
		priv->hw->GALR = priv->galr = galr;
	} else {
		priv->promisc = promisc;

		priv->iaur = iaur;
		priv->ialr = ialr;
		priv->gaur = gaur;
		priv->galr = galr;

		fec_change_mode(dev);

		pr_info("%s: Promiscuous mode %s\n", dev->name,
				priv->promisc ? "enabled" : "disabled");
	}

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


static int fec_open(struct net_device *dev)
{
	struct fec_private *priv = netdev_priv(dev);
	char phy_id[BUS_ID_SIZE];
	int err;

	err = fec_request_interrupts(dev);
	if (err)
		return err;

	snprintf(phy_id, BUS_ID_SIZE, PHY_ID_FMT, priv->pdev->id, priv->phy_id);

	priv->phy = phy_connect(dev, phy_id, &fec_adjust_link, 0);
	if (IS_ERR(priv->phy)) {
		printk(KERN_ERR "%s: %s: PHY connect failed\n",
			dev->name, phy_id);
		err = PTR_ERR(priv->phy);
		priv->phy = NULL;
		fec_free_interrupts(dev);
		return err;
	}

	priv->phy->supported &= PHY_BASIC_FEATURES;
	priv->phy->advertising = priv->phy->supported;

	priv->hw->ECR = FEC_ECR_ETHER_EN;

	phy_start(priv->phy);

	return 0;
}


static int fec_stop(struct net_device *dev)
{
	struct fec_private *priv = netdev_priv(dev);

	if (priv->phy) {
		phy_disconnect(priv->phy);
		priv->phy = NULL;
	}

	netif_stop_queue(dev);

	priv->hw->EIMR = FEC_EIMR_MASK_ALL;
	priv->hw->ECR = FEC_ECR_OFF;

	fec_free_interrupts(dev);

	fec_hard_reset(dev);

	return 0;
}




/*
 * Reset routines
 * *****************************************************************************
 */


static void fec_reset(struct net_device *dev)
{
	struct fec_private *priv = netdev_priv(dev);
	int i;

	priv->hw->ECR = FEC_ECR_RESET;

	memset(priv->tx_bd_ring, 0, TX_BD_TABLE_LEN);
	priv->tx_bd_ring[TX_BD_COUNT-1].sc |= FEC_TX_BD_SC_W;

	for (i = 0; i < TX_SB_COUNT; i++) {
		if (priv->tx_sb_ring[i]) {
			dev_kfree_skb_any(priv->tx_sb_ring[i]);
			priv->tx_sb_ring[i] = NULL;
		}
	}

	memset(priv->tx_al_ring, 0, TX_AL_TABLE_LEN);

	for (i = 0; i < RX_BD_COUNT; i++)
		priv->rx_bd_ring[i].sc = FEC_RX_BD_SC_E;
	priv->rx_bd_ring[RX_BD_COUNT-1].sc |= FEC_RX_BD_SC_W;

	priv->tx_head = priv->tx_tail = priv->rx_tail = 0;

	priv->hw->EIMR = FEC_EIMR_TXF | FEC_EIMR_RXF | FEC_EIMR_EBERR |
				FEC_EIMR_ERR;
	priv->hw->EIR = FEC_EIR_CLEAR_ALL;

	priv->hw->IAUR = priv->iaur;
	priv->hw->IALR = priv->ialr;

	priv->hw->GAUR = priv->gaur;
	priv->hw->GALR = priv->galr;

	priv->hw->PALR = (0
		| (dev->dev_addr[0] << 24)
		| (dev->dev_addr[1] << 16)
		| (dev->dev_addr[2] << 8)
		| (dev->dev_addr[3] << 0));
	priv->hw->PAUR = (0
		| (dev->dev_addr[4] << 24)
		| (dev->dev_addr[5] << 16));

	priv->hw->RCR = priv->rcr;
	priv->hw->TCR = priv->tcr;

	priv->hw->MSCR = FEC_MSCR_MII_SPEED;

	priv->hw->EMRBR = FEC_EMRBR_R_BUF_SIZE(RX_BUF_LEN);
	priv->hw->ERDSR = (unsigned long)priv->rx_bd_ring;
	priv->hw->ETDSR = (unsigned long)priv->tx_bd_ring;
}


static void fec_change_mode(struct net_device *dev)
{
	struct fec_private *priv = netdev_priv(dev);

	if (priv->duplex) {
		priv->rcr |= FEC_RCR_FCE;
		priv->rcr &= ~FEC_RCR_DRT;
		priv->tcr |= FEC_TCR_FDEN;
	} else {
		priv->rcr &= ~FEC_RCR_FCE;
		priv->rcr |= FEC_RCR_DRT;
		priv->tcr &= ~FEC_TCR_FDEN;
	}

	if (priv->promisc)
		priv->rcr |= FEC_RCR_PROM;
	else
		priv->rcr &= ~FEC_RCR_PROM;

	fec_reset(dev);

	if (priv->link) {
		priv->hw->ECR = FEC_ECR_ETHER_EN;
		priv->hw->RDAR = FEC_RDAR_ACTIVE;
	}
}


static void fec_hard_reset(struct net_device *dev)
{
	struct fec_private *priv = netdev_priv(dev);

	priv->iaur = 0;
	priv->ialr = 0;
	priv->gaur = 0;
	priv->galr = 0;
	priv->rcr = FEC_RCR_MAX_FL(MAX_FL) | FEC_RCR_FCE | FEC_RCR_MII_MODE;
	priv->tcr = FEC_TCR_FDEN;

	priv->link = 0;
	priv->duplex = DUPLEX_FULL;
	priv->speed = SPEED_100;
	priv->promisc = 0;

	fec_reset(dev);
}




/*
 * Init and cleanup routines
 * *****************************************************************************
 */


static int fec_res_init(struct net_device *dev)
{
	struct fec_private *priv = netdev_priv(dev);
	struct resource *r;
	int i;

	r = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "CSR");
	if (!r) {
		printk(KERN_ERR "FEC: CSR resource not found\n");
		return -ENODEV;
	}
	priv->hw = (volatile struct fec_csr *)r->start;

	r = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "MAC");
	if (r && is_valid_ether_addr((void *)r->start))
		memcpy(dev->dev_addr, (void *)r->start, ETH_ALEN);
	else {
		pr_info("FEC: MAC resource not found or not valid, "
			"using default\n");
		memcpy(dev->dev_addr, fec_default_mac, ETH_ALEN);
	}

	r = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "PHY_ID");
	if (!r) {
		printk(KERN_ERR "FEC: PHY_ID resource not found\n");
		return -ENODEV;
	}
	priv->phy_id = *(unsigned int *)r->start;

	for (i = 0; i < FEC_INTERRUPT_COUNT; i++) {
		r = platform_get_resource_byname(priv->pdev, IORESOURCE_IRQ,
							fec_interrupts[i].name);
		if (!r) {
			printk(KERN_ERR "FEC: %s resource irq not found\n",
					fec_interrupts[i].name);
			return -ENODEV;
		}
		priv->irqs[i] = (int)r->start;
	}

	return 0;
}


static int fec_init(struct net_device *dev)
{
	struct fec_private *priv = netdev_priv(dev);
	struct {
		struct bd	tx_bd_ring[TX_BD_COUNT];
		struct sk_buff *tx_sb_ring[TX_SB_COUNT];
		unsigned long	tx_al_ring[TX_AL_COUNT];
		struct bd	rx_bd_ring[RX_BD_COUNT];
	} *bdt;
	unsigned long i, b;
	int err;

	err = fec_res_init(dev);
	if (err)
		return err;

	if (RX_BD_TABLE_LEN > (BD_TABLE_LEN/2)) {
		printk(KERN_ERR
			"FEC: Not enought space for "
			"the Rx buffer descriptors\n");
		return -ENOMEM;
	}

	priv->bdt_mem = __get_free_pages(GFP_KERNEL, BD_TABLE_PAGE_ORDER);
	if (!priv->bdt_mem) {
		printk(KERN_ERR "FEC: Allocation of BD table mem failed\n");
		return -ENOMEM;
	}

	priv->rb_mem = __get_free_pages(GFP_KERNEL, RX_BUF_PAGE_ORDER);
	if (!priv->rb_mem) {
		free_page(priv->bdt_mem);
		printk(KERN_ERR "FEC: Allocation of Rx buffer mem failed\n");
		return -ENOMEM;
	}

	memset((void *)priv->bdt_mem, 0, BD_TABLE_LEN);
	memset((void *)priv->rb_mem, 0, RX_BF_TABLE_LEN);

	bdt = (void *)priv->bdt_mem;

	priv->tx_bd_ring = bdt->tx_bd_ring;
	priv->tx_sb_ring = bdt->tx_sb_ring;
	priv->tx_al_ring = bdt->tx_al_ring;
	priv->rx_bd_ring = bdt->rx_bd_ring;

	for (i = 0, b = priv->rb_mem; i < RX_BD_COUNT; i++, b += RX_BUF_SIZE)
		priv->rx_bd_ring[i].address = b;

	priv->lock = SPIN_LOCK_UNLOCKED;

	dev->base_addr = (unsigned long)priv->hw;
	dev->open = fec_open;
	dev->stop = fec_stop;
	dev->hard_start_xmit = fec_hard_start_xmit;
	dev->tx_timeout = fec_tx_timeout;
	dev->get_stats = fec_get_stats;
	dev->set_multicast_list = fec_set_multicast_list;
	dev->watchdog_timeo = TX_TIMEOUT;

	fec_hard_reset(dev);

	return 0;
}


static void fec_cleanup(struct net_device *dev)
{
	struct fec_private *priv = netdev_priv(dev);

	free_pages(priv->bdt_mem, BD_TABLE_PAGE_ORDER);
	free_pages(priv->rb_mem, RX_BUF_PAGE_ORDER);
}


static int fec_mdio_init(struct net_device *dev)
{
	struct fec_private *priv = netdev_priv(dev);
	int i, err;

	priv->mdio.name = "FEC MII Bus";
	priv->mdio.read = &fec_mdio_read;
	priv->mdio.write = &fec_mdio_write;
	priv->mdio.id = priv->pdev->id;
	priv->mdio.priv = dev;
	priv->mdio.dev = &priv->pdev->dev;

	for (i = 0; i < PHY_MAX_ADDR; i++)
		priv->mdio_irqs[i] = PHY_POLL;
	priv->mdio.irq = priv->mdio_irqs;

	if ((err = mdiobus_register(&priv->mdio))) {
		printk(KERN_ERR "%s: Cannot register as MDIO bus\n",
			priv->mdio.name);
		return err;
	}

	return 0;
}


static void fec_mdio_cleanup(struct net_device *dev)
{
	struct fec_private *priv = netdev_priv(dev);

	mdiobus_unregister(&priv->mdio);
}


static int fec_probe(struct platform_device *pdev)
{
	struct net_device *dev;
	struct fec_private *priv;
	int err;

	dev = alloc_etherdev(sizeof(*priv));
	if (!dev)
		return -ENOMEM;

	priv = netdev_priv(dev);
	priv->pdev = pdev;
	platform_set_drvdata(pdev, dev);

	err = fec_init(dev);
	if (err)
		goto fec_probe_init_fail;

	err = fec_mdio_init(dev);
	if (err)
		goto fec_probe_mdio_init_fail;

	err = register_netdev(dev);
	if (err)
		goto fec_probe_reg_netdev_fail;

	pr_info("FEC: probed (%s)\n", dev->name);

	return 0;

fec_probe_reg_netdev_fail:
	fec_mdio_cleanup(dev);

fec_probe_mdio_init_fail:
	fec_cleanup(dev);

fec_probe_init_fail:
	platform_set_drvdata(pdev, NULL);

	free_netdev(dev);

	return err;
}


static int fec_remove(struct platform_device *pdev)
{
	struct net_device *dev = platform_get_drvdata(pdev);

	unregister_netdev(dev);

	fec_mdio_cleanup(dev);

	fec_cleanup(dev);

	platform_set_drvdata(pdev, NULL);

	free_netdev(dev);

	return 0;
}




/*
 * Module stuff
 * *****************************************************************************
 */


static struct platform_driver fec_driver = {
	.probe = fec_probe,
	.remove = fec_remove,
	.driver	= {
		.name = "FEC",
	},
};


static int __init fec_module_init(void)
{
	int err;

	err = fec_platform_init();
	if (err)
		return err;

	err = platform_driver_register(&fec_driver);
	if (err)
		fec_platform_cleanup();

	return err;
}


static void __exit fec_module_cleanup(void)
{
	platform_driver_unregister(&fec_driver);
	fec_platform_cleanup();
}


module_init(fec_module_init);
module_exit(fec_module_cleanup);


MODULE_AUTHOR("Michael Broughton <mbobowik@telusplanet.net>");
MODULE_DESCRIPTION("ColdFire FEC driver");
MODULE_LICENSE("GPL");