summaryrefslogtreecommitdiffstats
path: root/drivers/net/enc28j60.c
blob: 3628a88f7dd3d3a9b8a887917f8c130efe3dcd5f (plain)
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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Microchip ENC28J60 ethernet driver (MAC + PHY)
 *
 * This code was ported from linux-3.15 kernel by Antony Pavlov.
 *
 * Copyright (C) 2007 Eurek srl
 * Author: Claudio Lanconelli <lanconelli.claudio@eptar.com>
 * based on enc28j60.c written by David Anders for 2.4 kernel version
 */

#include <common.h>
#include <net.h>
#include <malloc.h>
#include <init.h>
#include <driver.h>
#include <spi/spi.h>
#include <xfuncs.h>
#include <errno.h>

#include "enc28j60_hw.h"

#define DRV_NAME	"enc28j60"

#define SPI_OPLEN	1

#define ENC28J60_MSG_DEFAULT	\
	(NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN | NETIF_MSG_LINK)

/* Buffer size required for the largest SPI transfer (i.e., reading a
 * frame). */
#define SPI_TRANSFER_BUF_LEN	(4 + MAX_FRAMELEN)

/* Driver local data */
struct enc28j60_net {
	struct eth_device edev;
	struct spi_device *spi;
	u8 bank;		/* current register bank selected */
	u16 next_pk_ptr;	/* next packet pointer within FIFO */
	u16 max_pk_counter;	/* statistics: max packet counter */
	u16 tx_retry_count;
	bool hw_enable;
	bool full_duplex;
	int rxfilter;
	u8 spi_transfer_buf[SPI_TRANSFER_BUF_LEN];
	/* store MAC address here while hardware is in the reset state */
	u8 hwaddr[ETH_ALEN];
	struct mii_bus miibus;
};

/*
 * SPI read buffer
 * wait for the SPI transfer and copy received data to destination
 */
static int
spi_read_buf(struct enc28j60_net *priv, int len, u8 *data)
{
	u8 *rx_buf = priv->spi_transfer_buf + 4;
	u8 *tx_buf = priv->spi_transfer_buf;
	struct spi_transfer t = {
		.tx_buf = tx_buf,
		.rx_buf = rx_buf,
		.len = SPI_OPLEN + len,
	};
	struct spi_message msg;
	int ret;

	tx_buf[0] = ENC28J60_READ_BUF_MEM;
	tx_buf[1] = tx_buf[2] = tx_buf[3] = 0;	/* don't care */

	spi_message_init(&msg);
	spi_message_add_tail(&t, &msg);
	ret = spi_sync(priv->spi, &msg);
	if (ret == 0) {
		memcpy(data, &rx_buf[SPI_OPLEN], len);
		ret = msg.status;
	}
	if (ret)
		dev_err(&priv->edev.dev,
			"%s() failed: ret = %d\n", __func__, ret);

	return ret;
}

/*
 * SPI write buffer
 */
static int spi_write_buf(struct enc28j60_net *priv, int len,
			 const u8 *data)
{
	int ret;

	if (len > SPI_TRANSFER_BUF_LEN - 1 || len <= 0)
		ret = -EINVAL;
	else {
		priv->spi_transfer_buf[0] = ENC28J60_WRITE_BUF_MEM;
		memcpy(&priv->spi_transfer_buf[1], data, len);
		ret = spi_write(priv->spi, priv->spi_transfer_buf, len + 1);
		if (ret)
			dev_err(&priv->edev.dev,
				"%s() failed: ret = %d\n", __func__, ret);
	}
	return ret;
}

/*
 * basic SPI read operation
 */
static u8 spi_read_op(struct enc28j60_net *priv, u8 op,
			   u8 addr)
{
	u8 tx_buf[2];
	u8 rx_buf[4];
	u8 val = 0;
	int ret;
	int slen = SPI_OPLEN;

	/* do dummy read if needed */
	if (addr & SPRD_MASK)
		slen++;

	tx_buf[0] = op | (addr & ADDR_MASK);
	ret = spi_write_then_read(priv->spi, tx_buf, 1, rx_buf, slen);
	if (ret)
		dev_err(&priv->edev.dev,
			"%s() failed: ret = %d\n", __func__, ret);
	else
		val = rx_buf[slen - 1];

	return val;
}

/*
 * basic SPI write operation
 */
static int spi_write_op(struct enc28j60_net *priv, u8 op,
			u8 addr, u8 val)
{
	int ret;

	priv->spi_transfer_buf[0] = op | (addr & ADDR_MASK);
	priv->spi_transfer_buf[1] = val;
	ret = spi_write(priv->spi, priv->spi_transfer_buf, 2);
	if (ret)
		dev_err(&priv->edev.dev,
			"%s() failed: ret = %d\n", __func__, ret);

	return ret;
}

static void enc28j60_soft_reset(struct enc28j60_net *priv)
{
	dev_dbg(&priv->edev.dev, "%s() enter\n", __func__);

	spi_write_op(priv, ENC28J60_SOFT_RESET, 0, ENC28J60_SOFT_RESET);
	/* Errata workaround #1, CLKRDY check is unreliable,
	 * delay at least 1 mS instead */
	udelay(2000);
}

/*
 * select the current register bank if necessary
 */
static void enc28j60_set_bank(struct enc28j60_net *priv, u8 addr)
{
	u8 b = (addr & BANK_MASK) >> 5;

	/* These registers (EIE, EIR, ESTAT, ECON2, ECON1)
	 * are present in all banks, no need to switch bank
	 */
	if (addr >= EIE && addr <= ECON1)
		return;

	/* Clear or set each bank selection bit as needed */
	if ((b & ECON1_BSEL0) != (priv->bank & ECON1_BSEL0)) {
		if (b & ECON1_BSEL0)
			spi_write_op(priv, ENC28J60_BIT_FIELD_SET, ECON1,
					ECON1_BSEL0);
		else
			spi_write_op(priv, ENC28J60_BIT_FIELD_CLR, ECON1,
					ECON1_BSEL0);
	}
	if ((b & ECON1_BSEL1) != (priv->bank & ECON1_BSEL1)) {
		if (b & ECON1_BSEL1)
			spi_write_op(priv, ENC28J60_BIT_FIELD_SET, ECON1,
					ECON1_BSEL1);
		else
			spi_write_op(priv, ENC28J60_BIT_FIELD_CLR, ECON1,
					ECON1_BSEL1);
	}
	priv->bank = b;
}

/*
 * Register access routines through the SPI bus.
 * Some registers can be accessed through the bit field clear and
 * bit field set to avoid a read modify write cycle.
 */

/*
 * Register bit field Set
 */
static void enc28j60_reg_bfset(struct enc28j60_net *priv,
				      u8 addr, u8 mask)
{
	enc28j60_set_bank(priv, addr);
	spi_write_op(priv, ENC28J60_BIT_FIELD_SET, addr, mask);
}

/*
 * Register bit field Clear
 */
static void enc28j60_reg_bfclr(struct enc28j60_net *priv,
				      u8 addr, u8 mask)
{
	enc28j60_set_bank(priv, addr);
	spi_write_op(priv, ENC28J60_BIT_FIELD_CLR, addr, mask);
}

/*
 * Register byte read
 */
static int enc28j60_regb_read(struct enc28j60_net *priv,
				     u8 address)
{
	enc28j60_set_bank(priv, address);
	return spi_read_op(priv, ENC28J60_READ_CTRL_REG, address);
}

/*
 * Register word read
 */
static int enc28j60_regw_read(struct enc28j60_net *priv,
				     u8 address)
{
	int rl, rh;

	enc28j60_set_bank(priv, address);
	rl = spi_read_op(priv, ENC28J60_READ_CTRL_REG, address);
	rh = spi_read_op(priv, ENC28J60_READ_CTRL_REG, address + 1);

	return (rh << 8) | rl;
}

/*
 * Register byte write
 */
static void enc28j60_regb_write(struct enc28j60_net *priv,
				       u8 address, u8 data)
{
	enc28j60_set_bank(priv, address);
	spi_write_op(priv, ENC28J60_WRITE_CTRL_REG, address, data);
}

/*
 * Register word write
 */
static void enc28j60_regw_write(struct enc28j60_net *priv,
				       u8 address, u16 data)
{
	enc28j60_set_bank(priv, address);
	spi_write_op(priv, ENC28J60_WRITE_CTRL_REG, address, (u8) data);
	spi_write_op(priv, ENC28J60_WRITE_CTRL_REG, address + 1,
		     (u8) (data >> 8));
}

/*
 * Buffer memory read
 * Select the starting address and execute a SPI buffer read
 */
static void enc28j60_mem_read(struct enc28j60_net *priv,
				     u16 addr, int len, u8 *data)
{
	enc28j60_regw_write(priv, ERDPTL, addr);

	if (IS_ENABLED(CONFIG_ENC28J60_WRITEVERIFY)) {
		u16 reg;
		reg = enc28j60_regw_read(priv, ERDPTL);
		if (reg != addr)
			dev_err(&priv->edev.dev, "%s() error writing ERDPT "
				"(0x%04x - 0x%04x)\n", __func__, reg, addr);
	}

	spi_read_buf(priv, len, data);
}

/*
 * Write packet to enc28j60 TX buffer memory
 */
static void
enc28j60_packet_write(struct enc28j60_net *priv, int len, const u8 *data)
{
	/* Set the write pointer to start of transmit buffer area */
	enc28j60_regw_write(priv, EWRPTL, TXSTART_INIT);

	if (IS_ENABLED(CONFIG_ENC28J60_WRITEVERIFY)) {
		u16 reg;
		reg = enc28j60_regw_read(priv, EWRPTL);
		if (reg != TXSTART_INIT)
			dev_err(&priv->edev.dev,
				"%s() ERWPT:0x%04x != 0x%04x\n",
				__func__, reg, TXSTART_INIT);
	}
	/* Set the TXND pointer to correspond to the packet size given */
	enc28j60_regw_write(priv, ETXNDL, TXSTART_INIT + len);
	/* write per-packet control byte */
	spi_write_op(priv, ENC28J60_WRITE_BUF_MEM, 0, 0x00);
	dev_dbg(&priv->edev.dev,
		"%s() after control byte ERWPT:0x%04x\n",
		__func__, enc28j60_regw_read(priv, EWRPTL));
	/* copy the packet into the transmit buffer */
	spi_write_buf(priv, len, data);
	dev_dbg(&priv->edev.dev,
		"%s() after write packet ERWPT:0x%04x, len=%d\n",
		 __func__, enc28j60_regw_read(priv, EWRPTL), len);
}

static int poll_ready(struct enc28j60_net *priv, u8 reg, u8 mask, u8 val)
{
	if ((enc28j60_regb_read(priv, reg) & mask) == val) {
		return 0;
	}

	/* 20 msec timeout read */
	mdelay(20);

	if ((enc28j60_regb_read(priv, reg) & mask) != val) {
		dev_err(&priv->spi->dev, "reg %02x ready timeout!\n", reg);
		return -ETIMEDOUT;
	}

	return 0;
}

/*
 * Wait until the PHY operation is complete.
 */
static int wait_phy_ready(struct enc28j60_net *priv)
{
	return poll_ready(priv, MISTAT, MISTAT_BUSY, 0) ? 0 : 1;
}

/*
 * PHY register read
 * PHY registers are not accessed directly, but through the MII
 */
static u16 enc28j60_phy_read(struct enc28j60_net *priv, u8 address)
{
	u16 ret;

	/* set the PHY register address */
	enc28j60_regb_write(priv, MIREGADR, address);
	/* start the register read operation */
	enc28j60_regb_write(priv, MICMD, MICMD_MIIRD);
	/* wait until the PHY read completes */
	wait_phy_ready(priv);
	/* quit reading */
	enc28j60_regb_write(priv, MICMD, 0x00);
	/* return the data */
	ret = enc28j60_regw_read(priv, MIRDL);

	return ret;
}

static int enc28j60_phy_write(struct enc28j60_net *priv, u8 address, u16 data)
{
	int ret;

	/* set the PHY register address */
	enc28j60_regb_write(priv, MIREGADR, address);
	/* write the PHY data */
	enc28j60_regw_write(priv, MIWRL, data);
	/* wait until the PHY write completes and return */
	ret = wait_phy_ready(priv);

	return ret;
}

/*
 * MII-interface related functions
 */
static int enc28j60_miibus_read(struct mii_bus *bus, int phy_addr, int reg_addr)
{
	struct enc28j60_net *priv = (struct enc28j60_net *)bus->priv;

	if (phy_addr == 0) {
		return enc28j60_phy_read(priv, reg_addr);
	}

	return 0xffff;
}

static int enc28j60_miibus_write(struct mii_bus *bus, int phy_addr,
	int reg_addr, u16 data)
{
	struct enc28j60_net *priv = (struct enc28j60_net *)bus->priv;

	if (phy_addr == 0) {
		enc28j60_phy_write(priv, reg_addr, data);
	}

	return 0;
}

static int enc28j60_get_ethaddr(struct eth_device *edev, unsigned char *m)
{
	struct enc28j60_net *priv = edev->priv;

	memcpy(m, priv->hwaddr, ETH_ALEN);

	dev_dbg(&edev->dev, "getting MAC address\n");

	return 0;
}

/*
 * Program the hardware MAC address from dev->dev_addr.
 */
static int enc28j60_set_ethaddr(struct eth_device *edev,
				const unsigned char *mac_addr)
{
	int ret;
	struct enc28j60_net *priv = edev->priv;

	if (!priv->hw_enable) {
		dev_dbg(&edev->dev, "%s(): Setting MAC address\n", __func__);
		/* NOTE: MAC address in ENC28J60 is byte-backward */
		enc28j60_regb_write(priv, MAADR5, mac_addr[0]);
		enc28j60_regb_write(priv, MAADR4, mac_addr[1]);
		enc28j60_regb_write(priv, MAADR3, mac_addr[2]);
		enc28j60_regb_write(priv, MAADR2, mac_addr[3]);
		enc28j60_regb_write(priv, MAADR1, mac_addr[4]);
		enc28j60_regb_write(priv, MAADR0, mac_addr[5]);
		ret = 0;
		memcpy(priv->hwaddr, mac_addr, ETH_ALEN);
	} else {
		dev_err(&edev->dev, "%s() Hardware must be disabled to set "
			"Mac address\n", __func__);
		ret = -EBUSY;
	}

	return ret;
}

/*
 * Debug routine to dump useful register contents
 */
static inline void enc28j60_dump_regs(struct enc28j60_net *priv, const char *msg)
{
	dev_dbg(&priv->edev.dev, " %s\n"
		"HwRevID: 0x%02x\n"
		"Cntrl: ECON1 ECON2 ESTAT  EIR  EIE\n"
		"       0x%02x  0x%02x  0x%02x  0x%02x  0x%02x\n"
		"MAC  : MACON1 MACON3 MACON4\n"
		"       0x%02x   0x%02x   0x%02x\n"
		"Rx   : ERXST  ERXND  ERXWRPT ERXRDPT ERXFCON EPKTCNT MAMXFL\n"
		"       0x%04x 0x%04x 0x%04x  0x%04x  "
		"0x%02x    0x%02x    0x%04x\n"
		"Tx   : ETXST  ETXND  MACLCON1 MACLCON2 MAPHSUP\n"
		"       0x%04x 0x%04x 0x%02x     0x%02x     0x%02x\n",
		msg, enc28j60_regb_read(priv, EREVID),
		enc28j60_regb_read(priv, ECON1), enc28j60_regb_read(priv, ECON2),
		enc28j60_regb_read(priv, ESTAT), enc28j60_regb_read(priv, EIR),
		enc28j60_regb_read(priv, EIE), enc28j60_regb_read(priv, MACON1),
		enc28j60_regb_read(priv, MACON3), enc28j60_regb_read(priv, MACON4),
		enc28j60_regw_read(priv, ERXSTL), enc28j60_regw_read(priv, ERXNDL),
		enc28j60_regw_read(priv, ERXWRPTL),
		enc28j60_regw_read(priv, ERXRDPTL),
		enc28j60_regb_read(priv, ERXFCON),
		enc28j60_regb_read(priv, EPKTCNT),
		enc28j60_regw_read(priv, MAMXFLL), enc28j60_regw_read(priv, ETXSTL),
		enc28j60_regw_read(priv, ETXNDL),
		enc28j60_regb_read(priv, MACLCON1),
		enc28j60_regb_read(priv, MACLCON2),
		enc28j60_regb_read(priv, MAPHSUP));
}

/*
 * ERXRDPT need to be set always at odd addresses, refer to errata datasheet
 */
static u16 erxrdpt_workaround(u16 next_packet_ptr, u16 start, u16 end)
{
	u16 erxrdpt;

	if ((next_packet_ptr - 1 < start) || (next_packet_ptr - 1 > end))
		erxrdpt = end;
	else
		erxrdpt = next_packet_ptr - 1;

	return erxrdpt;
}

/*
 * Calculate wrap around when reading beyond the end of the RX buffer
 */
static u16 rx_packet_start(u16 ptr)
{
	if (ptr + RSV_SIZE > RXEND_INIT)
		return (ptr + RSV_SIZE) - (RXEND_INIT - RXSTART_INIT + 1);
	else
		return ptr + RSV_SIZE;
}

static void enc28j60_rxfifo_init(struct enc28j60_net *priv, u16 start, u16 end)
{
	u16 erxrdpt;

	if (start > 0x1FFF || end > 0x1FFF || start > end) {
		dev_err(&priv->edev.dev,
			"%s(%d, %d) RXFIFO bad parameters!\n",
			__func__, start, end);
		return;
	}

	/* set receive buffer start + end */
	priv->next_pk_ptr = start;
	enc28j60_regw_write(priv, ERXSTL, start);
	erxrdpt = erxrdpt_workaround(priv->next_pk_ptr, start, end);
	enc28j60_regw_write(priv, ERXRDPTL, erxrdpt);
	enc28j60_regw_write(priv, ERXNDL, end);
}

static void enc28j60_txfifo_init(struct enc28j60_net *priv, u16 start, u16 end)
{
	if (start > 0x1FFF || end > 0x1FFF || start > end) {
		dev_err(&priv->edev.dev,
			"%s(%d, %d) TXFIFO bad parameters!\n",
			__func__, start, end);
		return;
	}
	/* set transmit buffer start + end */
	enc28j60_regw_write(priv, ETXSTL, start);
	enc28j60_regw_write(priv, ETXNDL, end);
}

/*
 * Low power mode shrinks power consumption about 100x, so we'd like
 * the chip to be in that mode whenever it's inactive.  (However, we
 * can't stay in lowpower mode during suspend with WOL active.)
 */
static void enc28j60_lowpower(struct enc28j60_net *priv, bool is_low)
{
	dev_dbg(&priv->spi->dev, "%s power...\n", is_low ? "low" : "high");

	if (is_low) {
		enc28j60_reg_bfclr(priv, ECON1, ECON1_RXEN);
		poll_ready(priv, ESTAT, ESTAT_RXBUSY, 0);
		poll_ready(priv, ECON1, ECON1_TXRTS, 0);
		/* ECON2_VRPS was set during initialization */
		enc28j60_reg_bfset(priv, ECON2, ECON2_PWRSV);
	} else {
		enc28j60_reg_bfclr(priv, ECON2, ECON2_PWRSV);
		poll_ready(priv, ESTAT, ESTAT_CLKRDY, ESTAT_CLKRDY);
		/* caller sets ECON1_RXEN */
	}
}

static int enc28j60_hw_init(struct enc28j60_net *priv)
{
	u8 reg;

	dev_dbg(&priv->edev.dev, "%s() - %s\n", __func__,
		priv->full_duplex ? "FullDuplex" : "HalfDuplex");

	/* first reset the chip */
	enc28j60_soft_reset(priv);
	/* Clear ECON1 */
	spi_write_op(priv, ENC28J60_WRITE_CTRL_REG, ECON1, 0x00);
	priv->bank = 0;
	priv->hw_enable = false;
	priv->tx_retry_count = 0;
	priv->max_pk_counter = 0;

	/* enable address auto increment and voltage regulator powersave */
	enc28j60_regb_write(priv, ECON2, ECON2_AUTOINC | ECON2_VRPS);

	enc28j60_rxfifo_init(priv, RXSTART_INIT, RXEND_INIT);
	enc28j60_txfifo_init(priv, TXSTART_INIT, TXEND_INIT);

	/*
	 * Check the RevID.
	 * If it's 0x00 or 0xFF probably the enc28j60 is not mounted or
	 * damaged
	 */
	reg = enc28j60_regb_read(priv, EREVID);

	dev_dbg(&priv->edev.dev, "chip RevID: 0x%02x\n", reg);

	if (reg == 0x00 || reg == 0xff) {
		dev_err(&priv->edev.dev,
			"%s() Invalid RevId %d\n", __func__, reg);
		return 0;
	}

	/* default filter mode: (unicast OR broadcast) AND crc valid */
	enc28j60_regb_write(priv, ERXFCON,
			    ERXFCON_UCEN | ERXFCON_CRCEN | ERXFCON_BCEN);

	/* enable MAC receive */
	enc28j60_regb_write(priv, MACON1,
			    MACON1_MARXEN | MACON1_TXPAUS | MACON1_RXPAUS);

	/* enable automatic padding and CRC operations */
	if (priv->full_duplex) {
		enc28j60_regb_write(priv, MACON3,
				    MACON3_PADCFG0 | MACON3_TXCRCEN |
				    MACON3_FRMLNEN | MACON3_FULDPX);
		/* set inter-frame gap (non-back-to-back) */
		enc28j60_regb_write(priv, MAIPGL, 0x12);
		/* set inter-frame gap (back-to-back) */
		enc28j60_regb_write(priv, MABBIPG, 0x15);
	} else {
		enc28j60_regb_write(priv, MACON3,
				    MACON3_PADCFG0 | MACON3_TXCRCEN |
				    MACON3_FRMLNEN);
		enc28j60_regb_write(priv, MACON4, 1 << 6);	/* DEFER bit */
		/* set inter-frame gap (non-back-to-back) */
		enc28j60_regw_write(priv, MAIPGL, 0x0C12);
		/* set inter-frame gap (back-to-back) */
		enc28j60_regb_write(priv, MABBIPG, 0x12);
	}
	/*
	 * MACLCON1 (default)
	 * MACLCON2 (default)
	 * Set the maximum packet size which the controller will accept
	 */
	enc28j60_regw_write(priv, MAMXFLL, MAX_FRAMELEN);

	/* Configure LEDs */
	if (!enc28j60_phy_write(priv, PHLCON, ENC28J60_LAMPS_MODE))
		return 0;

	if (priv->full_duplex) {
		if (!enc28j60_phy_write(priv, PHCON1, PHCON1_PDPXMD))
			return 0;
		if (!enc28j60_phy_write(priv, PHCON2, 0x00))
			return 0;
	} else {
		if (!enc28j60_phy_write(priv, PHCON1, 0x00))
			return 0;
		if (!enc28j60_phy_write(priv, PHCON2, PHCON2_HDLDIS))
			return 0;
	}

	enc28j60_dump_regs(priv, "Hw initialized.");

	return 1;
}

static void enc28j60_hw_enable(struct enc28j60_net *priv)
{
	dev_dbg(&priv->edev.dev, "%s()\n", __func__);

	/* enable receive logic */
	enc28j60_reg_bfset(priv, ECON1, ECON1_RXEN);
	priv->hw_enable = true;
}

static void enc28j60_hw_disable(struct enc28j60_net *priv)
{
	/* disable interrutps and packet reception */
	enc28j60_regb_write(priv, EIE, 0x00);
	enc28j60_reg_bfclr(priv, ECON1, ECON1_RXEN);
	priv->hw_enable = false;
}

/*
 * Receive Status vector
 */
static inline void enc28j60_dump_rsv(struct enc28j60_net *priv, const char *msg,
			      u16 pk_ptr, int len, u16 sts)
{
	struct device_d *dev = &priv->edev.dev;

	dev_dbg(dev, "%s - NextPk: 0x%04x - RSV:\n",
		msg, pk_ptr);
	dev_dbg(dev, "ByteCount: %d, DribbleNibble: %d\n", len,
		 RSV_GETBIT(sts, RSV_DRIBBLENIBBLE));
	dev_dbg(dev, "RxOK: %d, CRCErr:%d, LenChkErr: %d,"
		 " LenOutOfRange: %d\n", RSV_GETBIT(sts, RSV_RXOK),
		 RSV_GETBIT(sts, RSV_CRCERROR),
		 RSV_GETBIT(sts, RSV_LENCHECKERR),
		 RSV_GETBIT(sts, RSV_LENOUTOFRANGE));
	dev_dbg(dev, "Multicast: %d, Broadcast: %d, "
		 "LongDropEvent: %d, CarrierEvent: %d\n",
		 RSV_GETBIT(sts, RSV_RXMULTICAST),
		 RSV_GETBIT(sts, RSV_RXBROADCAST),
		 RSV_GETBIT(sts, RSV_RXLONGEVDROPEV),
		 RSV_GETBIT(sts, RSV_CARRIEREV));
	dev_dbg(dev, "ControlFrame: %d, PauseFrame: %d,"
		 " UnknownOp: %d, VLanTagFrame: %d\n",
		 RSV_GETBIT(sts, RSV_RXCONTROLFRAME),
		 RSV_GETBIT(sts, RSV_RXPAUSEFRAME),
		 RSV_GETBIT(sts, RSV_RXUNKNOWNOPCODE),
		 RSV_GETBIT(sts, RSV_RXTYPEVLAN));
}

/*
 * Hardware transmit function.
 * Fill the buffer memory and send the contents of the transmit buffer
 * onto the network
 */
static int enc28j60_eth_send(struct eth_device *edev, void *packet,
				int packet_length)
{
	struct enc28j60_net *priv = edev->priv;

	dev_dbg(&edev->dev, "Tx Packet Len:%d\n", packet_length);

	enc28j60_packet_write(priv, packet_length, packet);

	/* readback and verify written data */
	if (IS_ENABLED(CONFIG_ENC28J60_WRITEVERIFY)) {
		int test_len, k;
		u8 test_buf[64]; /* limit the test to the first 64 bytes */
		int okflag;

		test_len = packet_length;
		if (test_len > sizeof(test_buf))
			test_len = sizeof(test_buf);

		/* + 1 to skip control byte */
		enc28j60_mem_read(priv, TXSTART_INIT + 1, test_len, test_buf);
		okflag = 1;
		for (k = 0; k < test_len; k++) {
			if (((u8 *)packet)[k] != test_buf[k]) {
				dev_dbg(&edev->dev,
					 "Error, %d location differ: "
					 "0x%02x-0x%02x\n", k,
					 ((u8 *)packet)[k], test_buf[k]);
				okflag = 0;
			}
		}
		if (!okflag)
			dev_dbg(&edev->dev, "Tx write buffer verify ERROR!\n");
	}

	/* set TX request flag */
	enc28j60_reg_bfset(priv, ECON1, ECON1_TXRTS);

	return 0;
}

/*
 * Hardware receive function.
 * Read the buffer memory, update the FIFO pointer to free the buffer,
 * check the status vector and decrement the packet counter.
 */
static void enc28j60_hw_rx(struct eth_device *edev)
{
	struct enc28j60_net *priv = edev->priv;
	u16 erxrdpt, next_packet, rxstat;
	u8 rsv[RSV_SIZE];
	int len;

	dev_dbg(&edev->dev, "RX pk_addr:0x%04x\n", priv->next_pk_ptr);

	if (unlikely(priv->next_pk_ptr > RXEND_INIT)) {
		dev_err(&edev->dev, "%s() Invalid packet address!! 0x%04x\n",
			__func__, priv->next_pk_ptr);

		/* packet address corrupted: reset RX logic */
		enc28j60_reg_bfclr(priv, ECON1, ECON1_RXEN);
		enc28j60_reg_bfset(priv, ECON1, ECON1_RXRST);
		enc28j60_reg_bfclr(priv, ECON1, ECON1_RXRST);
		enc28j60_rxfifo_init(priv, RXSTART_INIT, RXEND_INIT);
		enc28j60_reg_bfclr(priv, EIR, EIR_RXERIF);
		enc28j60_reg_bfset(priv, ECON1, ECON1_RXEN);

		return;
	}

	/* Read next packet pointer and rx status vector */
	enc28j60_mem_read(priv, priv->next_pk_ptr, sizeof(rsv), rsv);

	next_packet = rsv[1];
	next_packet <<= 8;
	next_packet |= rsv[0];

	len = rsv[3];
	len <<= 8;
	len |= rsv[2];

	rxstat = rsv[5];
	rxstat <<= 8;
	rxstat |= rsv[4];

	enc28j60_dump_rsv(priv, __func__, next_packet, len, rxstat);

	if (!RSV_GETBIT(rxstat, RSV_RXOK) || len > MAX_FRAMELEN) {
		dev_err(&edev->dev, "Rx Error (%04x)\n", rxstat);
	} else {
		/* copy the packet from the receive buffer */
		enc28j60_mem_read(priv,
			rx_packet_start(priv->next_pk_ptr),
			len, NetRxPackets[0]);

		net_receive(edev, NetRxPackets[0], len);
	}

	/*
	 * Move the RX read pointer to the start of the next
	 * received packet.
	 * This frees the memory we just read out
	 */
	erxrdpt = erxrdpt_workaround(next_packet, RXSTART_INIT, RXEND_INIT);
	dev_dbg(&edev->dev, ": %s() ERXRDPT:0x%04x\n", __func__, erxrdpt);

	enc28j60_regw_write(priv, ERXRDPTL, erxrdpt);

	if (IS_ENABLED(CONFIG_ENC28J60_WRITEVERIFY)) {
		u16 reg;
		reg = enc28j60_regw_read(priv, ERXRDPTL);
		if (reg != erxrdpt)
			dev_dbg(&edev->dev, "%s() ERXRDPT verify "
				"error (0x%04x - 0x%04x)\n", __func__,
				reg, erxrdpt);
	}

	priv->next_pk_ptr = next_packet;

	/* we are done with this packet, decrement the packet counter */
	enc28j60_reg_bfset(priv, ECON2, ECON2_PKTDEC);
}

/*
 * Access the PHY to determine link status
 */
static void enc28j60_check_link_status(struct eth_device *edev)
{
	struct enc28j60_net *priv = edev->priv;
	u16 reg;
	int duplex;

	reg = enc28j60_phy_read(priv, PHSTAT2);
	dev_dbg(&edev->dev, "%s() PHSTAT1: %04x, PHSTAT2: %04x\n", __func__,
			enc28j60_phy_read(priv, PHSTAT1), reg);
	duplex = reg & PHSTAT2_DPXSTAT;

	if (reg & PHSTAT2_LSTAT) {
		dev_dbg(&edev->dev, "link up - %s\n",
			duplex ? "Full duplex" : "Half duplex");
	} else {
		dev_dbg(&edev->dev, "link down\n");
	}
}

static int enc28j60_eth_rx(struct eth_device *edev)
{
	struct enc28j60_net *priv = edev->priv;
	int pk_counter;

	pk_counter = enc28j60_regb_read(priv, EPKTCNT);
	if (pk_counter)
		dev_dbg(&edev->dev, "intRX, pk_cnt: %d\n", pk_counter);

	if (pk_counter > priv->max_pk_counter) {
		priv->max_pk_counter = pk_counter;
		if (priv->max_pk_counter > 1)
			dev_dbg(&edev->dev,
				"RX max_pk_cnt: %d\n", priv->max_pk_counter);
	}

	while (pk_counter-- > 0)
		enc28j60_hw_rx(edev);

	return 0;
}

static int enc28j60_init_dev(struct eth_device *edev)
{
	/* empty */

	return 0;
}

/*
 * Open/initialize the board. This is called (in the current kernel)
 * sometime after booting when the 'ifconfig' program is run.
 *
 * This routine should set everything up anew at each open, even
 * registers that "should" only need to be set once at boot, so that
 * there is non-reboot way to recover if something goes wrong.
 */
static int enc28j60_eth_open(struct eth_device *edev)
{
	struct enc28j60_net *priv = edev->priv;
	int ret;

	dev_dbg(&edev->dev, "%s() enter\n", __func__);

	/* Reset the hardware here (and take it out of low power mode) */
	enc28j60_lowpower(priv, false);
	enc28j60_hw_disable(priv);
	if (!enc28j60_hw_init(priv)) {
		dev_err(&edev->dev, "hw_init() failed\n");
		return -EINVAL;
	}

	/* Update the MAC address after reset */
	enc28j60_set_ethaddr(edev, priv->hwaddr);

	enc28j60_hw_enable(priv);

	/* Wait for link up after hardware reset */
	mdelay(2000);

	ret = phy_device_connect(edev, &priv->miibus, 0,
				&enc28j60_check_link_status, 0,
				PHY_INTERFACE_MODE_MII);

	return ret;
}

static void enc28j60_eth_halt(struct eth_device *edev)
{
	struct enc28j60_net *priv = edev->priv;

	dev_dbg(&edev->dev, "%s() enter\n", __func__);

	enc28j60_hw_disable(priv);
	enc28j60_lowpower(priv, true);
}

static int enc28j60_probe(struct device_d *dev)
{
	struct eth_device *edev;
	struct enc28j60_net *priv;
	int ret = 0;

	priv = xzalloc(sizeof(*priv));

	priv->spi = (struct spi_device *)dev->type_data;

	edev = &priv->edev;
	edev->priv = priv;
	edev->init = enc28j60_init_dev;
	edev->open = enc28j60_eth_open;
	edev->send = enc28j60_eth_send;
	edev->recv = enc28j60_eth_rx;
	edev->get_ethaddr = enc28j60_get_ethaddr;
	edev->set_ethaddr = enc28j60_set_ethaddr;
	edev->halt = enc28j60_eth_halt;
	edev->parent = dev;

	/*
	 * Here is a quote from ENC28J60 Data Sheet:
	 *
	 * The ENC28J60 does not support automatic duplex
	 * negotiation. If it is connected to an automatic duplex
	 * negotiation enabled network switch or Ethernet controller,
	 * the ENC28J60 will be detected as a half-duplex device.
	 *
	 * So most people prefer to set up half-duplex mode.
	 */
	priv->full_duplex = 0;

	ret = eth_register(edev);
	if (ret)
		goto eth_error_register;

	if (!enc28j60_hw_init(priv)) {
		dev_err(dev, DRV_NAME " chip not found\n");
		ret = -EIO;
		goto hw_error_register;
	}

	enc28j60_lowpower(priv, true);

	priv->miibus.read = enc28j60_miibus_read;
	priv->miibus.write = enc28j60_miibus_write;

	priv->miibus.priv = priv;
	priv->miibus.parent = dev;

	ret = mdiobus_register(&priv->miibus);
	if (ret)
		goto mdio_error_register;

	dev_info(dev, DRV_NAME " driver registered\n");

	return 0;

mdio_error_register:
hw_error_register:
	eth_unregister(edev);

eth_error_register:
	free(priv);

	return ret;
}

static __maybe_unused struct of_device_id enc28j60_dt_ids[] = {
	{
		.compatible = "microchip,enc28j60",
	}, {
		/* sentinel */
	}
};

static struct driver_d enc28j60_driver = {
	.name = DRV_NAME,
	.probe = enc28j60_probe,
	.of_compatible = DRV_OF_COMPAT(enc28j60_dt_ids),
};
device_spi_driver(enc28j60_driver);