Newer
Older
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
/****************************************************************************
* drivers/net/ez80_emac.c
*
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* References:
* eZ80F91 MCU Product Specification, PS019214-0808, Zilig, Inc., 2008.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#if defined(CONFIG_NET) && defined(CONFIG_EZ80_EMAC)
#include <time.h>
#include <string.h>
#include <debug.h>
#include <wdog.h>
#include <errno.h>
#include <nuttx/irq.h>
#include <nuttx/arch.h>
#include <nuttx/mii.h>
#include <arch/io.h>
#include <net/uip/uip.h>
#include <net/uip/uip-arp.h>
#include <net/uip/uip-arch.h>
#include "chip.h"
#include "up_internal.h"
/****************************************************************************
* Definitions
****************************************************************************/
#if CONFIG_NET_BUFSIZE > 1518
# error "MAXF size too big for this device"
#endif
/* The memory region shared between this driver and the EMAC hardware
* is subdivided into a Transmit buffer and the Receive buffer.
* The Transmit and Receive buffers are subdivided into packet buffers
* of 32, 64, 128, or 256 bytes in size.
*/
#ifndef CONFIG_EZ80_PKTBUFSIZE
# define CONFIG_EZ80_PKTBUFSIZE 64
#endif
#ifndef CONFIG_EZ80_NTXPKTBUFS
# define CONFIG_EZ80_NTXPKTBUFS 64
#endif
#ifndef CONFIG_EZ80_NRXPKTBUFS
# define CONFIG_EZ80_NRXPKTBUFS 64
#endif
#define EMAC_TXBUFSIZE (CONFIG_EZ80_PKTBUFSIZE * CONFIG_EZ80_NTXPKTBUFS)
#if EMAC_TXBUFSIZE > 8192
# error "Too many TX buffers"
#endif
#define EMAC_RXBUFSIZE (CONFIG_EZ80_PKTBUFSIZE * CONFIG_EZ80_NRXPKTBUFS)
#if EMAC_RXBUFSIZE > 8192
# error "Too many TX buffers"
#endif
#define EMAC_TOTAL_BUFSIZE (EMAC_TXBUFSIZE + EMAC_RXBUFSIZE)
#if EMAC_TOTAL_BUFSIZE > 8192
# error "Too many TX+RX buffers"
#elif EMAC_TOTAL_BUFSIZE < 8192
# error "Unused buffers!"
#endif
#if CONFIG_EZ80_PKTBUFSIZE == 256
# define EMAC_BUFSZ EMAC_BUFSZ_256b
#elif CONFIG_EZ80_PKTBUFSIZE == 128
# define EMAC_BUFSZ EMAC_BUFSZ_128b
#elif CONFIG_EZ80_PKTBUFSIZE == 64
# define EMAC_BUFSZ EMAC_BUFSZ_64b
#elif CONFIG_EZ80_PKTBUFSIZE == 32
# define EMAC_BUFSZ EMAC_BUFSZ_32b
#else
# error "Unsupported CONFIG_EZ80_PKTBUFSIZE value"
#endif
/* The system clock frequency is defined in the linkcmd file */
extern unsigned long SYS_CLK_FREQ;
#define _DEFCLK ((unsigned long)&SYS_CLK_FREQ)
/* Select the fastest MDC clock that does not exceed 25MHz. The MDC
* clock derives from the SCLK divided by 4, 6, 8, 10, 14, 20, or 28.
*/
#ifndef CONFIG_EZ80_MDCDIV
# define CONFIG_EZ80_MDCDIV EMAC_MDC_DIV4
#endif
/* Select the Tx poll timer. If not specified, a 10MS timer is set */
#ifndef CONFIG_EZ80_TXPOLLTIMERMS
# define CONFIG_EZ80_TXPOLLTIMERMS 10
#endif
/* Misc. timing values and settings */
#define EMAC_MXPOLLLOOPS 100000
#define EMAC_CRCPOLY2 0xedb88320;
/* Default register settings */
#define EMAC_TPTV 0x1000 /* TPTV: Default 0x1000 slot time (1slot:512bit) */
#define EMAC_IPGT 0x12 /* IPGT: Back-to-back IPG default value */
#define EMAC_IPGR1 0x0c /* IPGR1: Non-back-to-back IPG default value */
#define EMAC_IPGR2 0x12 /* IPGR2: Non-back-to-back IPG default value */
#define EMAC_MAXF CONFIG_NET_BUFSIZE /* Maximum packet length value */
#define EMAC_LCOL 0x37 /* CFG2: Late collision window default value */
#define EMAC_RETRY 0x0f /* CFG3: Maximum number of retry default value */
/* Poll timer setting. The transmit poll timer is set in increments of
* SYSCLCK / 256
*/
#define EMAC_PTMR ((CONFIG_EZ80_TXPOLLTIMERMS * (_DEFCLK / 1000) >> 8))
/* EMAC system interrupts :
*
* EMAC_ISTAT_TXFSMERR - Bit 7: 1=Transmit state machine error interrupt
* A Transmit State Machine Error should never occur. However, if this
* bit is set, the entire transmitter module must be reset.
* EMAC_ISTAT_MGTDONE - Bit 6: 1=MII Mgmt done interrupt
* This bit is set when communicating to the PHY over the MII during
* a Read or Write operation.
* EMAC_ISTAT_RXOVR - Bit 2: 1=Receive overrun interrupt
* If this bit is set, all incoming packets are ignored until
* this bit is cleared by software.
*/
#define EMAC_ISTAT_SYSEVENTS (EMAC_ISTAT_TXFSMERR|EMAC_ISTAT_MGTDONE|EMAC_ISTAT_RXOVR)
/* EMAC Tx interrupts:
*
* EMAC_ISTAT_TXDONE - Bit 0: 1=Transmit done interrupt
* Denotes when packet transmission is complete.
* EMAC_ISTAT_TXCF - Bit 1: 1=Transmit control frame interrupt
* Denotes when control frame transmission is complete.
*/
#define EMAC_ISTAT_TXEVENTS (EMAC_ISTAT_TXDONE|EMAC_ISTAT_TXCF)
/* EMAC Rx interrupts:
*
* EMAC_ISTAT_RXDONE - Bit 3: 1=Receive done interrupt
* Denotes when packet reception is complete.
* EMAC_ISTAT_RXPCF - Bit 4: 1=Receive pause control frame interrupt
* Denotes when pause control frame reception is complete.
* EMAC_ISTAT_RXCF - Bit 5: 1=Receive control frame interrupt
* Denotes when control frame reception is complete.
*/
#define EMAC_ISTAT_RXEVENTS (EMAC_ISTAT_RXDONE|EMAC_ISTAT_RXPCF|EMAC_ISTAT_RXCF)
#define EMAC_EIN_HANDLED (EMAC_ISTAT_RXEVENTS|EMAC_ISTAT_TXEVENTS|EMAC_ISTAT_SYSEVENTS)
/* TX poll deley = 1 seconds. CLK_TCK is the number of clock ticks per second */
#define EMAC_WDDELAY (1*CLK_TCK)
#define EMAC_POLLHSEC (1*2)
/* TX timeout = 1 minute */
#define EMAC_TXTIMEOUT (60*CLK_TCK)
/* This is a helper pointer for accessing the contents of the Ethernet header */
#define ETHBUF ((struct uip_eth_hdr *)priv->dev.d_buf)
/****************************************************************************
* Private Types
****************************************************************************/
/* EMAC statistics (debug only) */
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
struct ez80mac_statistics_s
{
uint32 rx_int; /* Number of Rx interrupts received */
uint32 rx_ip; /* Number of Rx IP packets received */
uint32 rx_arp; /* Number of Rx ARP packets received */
uint32 rx_dropped; /* Number of dropped, unsupported Rx packets */
uint32 rx_errors; /* Number of Rx errors (all types) */
uint32 rx_ovrerrors; /* Number of FIFO overrun errors */
uint32 tx_queued; /* Number of Tx descriptors queued */
uint32 tx_int; /* Number of Tx interrupts received */
uint32 tx_errors; /* Number of Tx errors (all types) */
uint32 tx_abterrors; /* Number of aborted Tx descriptors */
uint32 tx_fsmerrors; /* Number of Tx state machine errors */
uint32 tx_timeouts; /* Number of Tx timeout errors */
uint32 sys_int; /* Number of system interrupts received */
};
# define EMAC_STAT(priv,name) (((priv)->stat.(name))++)
#else
# define EMAC_STAT(priv,name)
#endif
/* Private driver data. The ez80emac_driver_s encapsulates all state information
* for a single hardware interface
*/
struct ez80emac_driver_s
{
/* Tx buffer management
*
* txstart: The beginning of the Rx descriptor list (and also the beginning of
* Tx/Rx memory).
* txhead: Points to the oldest Tx descriptor queued for output (but for
* which output has not yet completed. Initialized to NULL; set
* by ez80emac_transmit() when Tx is started and by ez80emac_reclaimtxdesc()
* when Tx processing completes. txhead == NULL is also a sure
* indication that there is no Tx in progress.
* txtail: Points to the last Tx descriptor queued for output. Initialized
* to NULL. Set by ez80emac_transmit() when a new Tx descriptor is added.
* txnext: Points to the next free Tx descriptor. Initialized to txstart; set
* when ez80emac_transmit() adds the descriptor; reset to txstart when the
* last Tx packet is sent.
*/
FAR struct ez80emac_desc_s *txstart;
FAR struct ez80emac_desc_s *txhead;
FAR struct ez80emac_desc_s *txtail;
FAR struct ez80emac_desc_s *txnext;
/* Rx buffer management
*
* rxstart: The beginning of the Rx descriptor list (and also the end of
* Tx buffer + 1).
* rxnext: The next of Rx descriptor available for receipt of a packet.
* Initialized to rxstart; rxnext is incremented by rmac_rxinterrupt()
* as part of Rx interrupt processing. rxnext wraps back to rxstart
* when rxnext exceeds rxendp1.
* rxendp1: The end of the Rx descriptor list + 1.
*/
FAR struct ez80emac_desc_s *rxstart;
FAR struct ez80emac_desc_s *rxnext;
FAR struct ez80emac_desc_s *rxendp1;
boolean bifup; /* TRUE:ifup FALSE:ifdown */
boolean blinkok; /* TRUE:successful MII autonegotiation */
boolean bfullduplex; /* TRUE:full duplex */
boolean b100mbs; /* TRUE:100Mbp */
WDOG_ID txpoll; /* TX poll timer */
WDOG_ID txtimeout; /* TX timeout timer */
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
struct ez80mac_statistics_s stat;
#endif
/* This holds the information visible to uIP/NuttX */
struct uip_driver_s dev; /* Interface understood by uIP */
};
/****************************************************************************
* Private Data
****************************************************************************/
/* There is only a single instance of driver private data (because there is
* only one EMAC interface.
*/
static struct ez80emac_driver_s g_emac;
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* MII logic */
static void ez80emac_miiwrite(FAR struct ez80emac_driver_s *priv, ubyte offset,
uint16 value);
static uint16 ez80emac_miiread(FAR struct ez80emac_driver_s *priv, uint32 offset);
static boolean ez80emac_miipoll(FAR struct ez80emac_driver_s *priv, uint32 offset,
uint16 bits, boolean bclear);
static void ez80emac_miiautonegotiate(FAR struct ez80emac_driver_s *priv);
/* Multi-cast filtering */
#ifdef CONFIG_EZ80_MCFILTER
static void ez80emac_machash(FAR ubyte *mac, int *ndx, int *bitno)
#endif
/* Common TX logic */
static int ez80emac_transmit(struct ez80emac_driver_s *priv);
static int ez80emac_uiptxpoll(struct uip_driver_s *dev);
/* Interrupt handling */
static int ez80emac_txinterrupt(int irq, FAR void *context);
static int ez80emac_rxinterrupt(int irq, FAR void *context);
static int ez80emac_sysinterrupt(int irq, FAR void *context);
/* Watchdog timer expirations */
static void ez80emac_polltimer(int argc, uint32 arg, ...);
static void ez80emac_txtimeout(int argc, uint32 arg, ...);
/* NuttX callback functions */
static int ez80emac_ifup(struct uip_driver_s *dev);
static int ez80emac_ifdown(struct uip_driver_s *dev);
static int ez80emac_txavail(struct uip_driver_s *dev);
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Function: ez80emac_miiwrite
*
* Description:
* Write a signel MII register
*
* Parameters:
* priv - Reference to the driver state structure
* offset - Register offset in PMD
* value - Value to write
*
* Returned Value:
* None
*
****************************************************************************/
static void ez80emac_miiwrite(FAR struct ez80emac_driver_s *priv, ubyte offset, uint16 value)
{
ubyte regval;
/* Wait for MII management operation to complete */
while (inp(EZ80_EMAC_MIISTAT) & EMAC_MIISTAT_BUSY);
/* Set up PHY addressing */
outp(EZ80_EMAC_FIAD, CONFIG_EZ80_FIAD & EMAC_FIAD_MASK);
outp(EZ80_EMAC_RGAD, offset & EMAC_RGAD_MASK);
/* Write the control data */
outp(EZ80_EMAC_CTLD_H, value >> 8);
outp(EZ80_EMAC_CTLD_L, value & 0xff);
/* Send control data to PHY */
regval = inp(EZ80_EMAC_MIIMGT);
regval |= EMAC_MIIMGMT_LCTLD;
outp(EZ80_EMAC_MIIMGT, regval);
}
/****************************************************************************
* Function: ez80emac_miiread
*
* Description:
* Read a single MII register
*
* Parameters:
* priv - Reference to the driver state structure
* offset - Register offset in PMD
*
* Returned Value:
* Value read from the register
*
****************************************************************************/
static uint16 ez80emac_miiread(FAR struct ez80emac_driver_s *priv, uint32 offset)
{
ubyte regval;
/* Wait for MII management operation to complete */
while (inp(EZ80_EMAC_MIISTAT) & EMAC_MIISTAT_BUSY);
/* Set up PHY addressing */
outp(EZ80_EMAC_FIAD, CONFIG_EZ80_FIAD & EMAC_FIAD_MASK);
outp(EZ80_EMAC_RGAD, offset & EMAC_RGAD_MASK);
/* Read status from PHY */
regval = inp(EZ80_EMAC_MIIMGT);
regval |= EMAC_MIIMGMT_RSTAT;
outp(EZ80_EMAC_MIIMGT, regval);
/* Wait for MII management operation to complete */
while (inp(EZ80_EMAC_MIISTAT) & EMAC_MIISTAT_BUSY);
return ((uint16)inp(EZ80_EMAC_PRSD_H) << 8 | inp(EZ80_EMAC_PRSD_L));
}
/****************************************************************************
* Function: ez80emac_miipoll
*
* Description:
* Read an MII register until the bit has the specified polarity (or until
* the maximum number of retries occurs
*
* Parameters:
* priv - Reference to the driver state structure
* offset - Register offset in PMD
* bits - Selects set of bits to wait for
* bclear - TRUE:Return true when all bits in 'bits' are 0
* FALSE:Return true when one or more bits in 'bits' are 1
*
* Returned Value:
* TRUE:Bit has requested polarity; FALSE: EMAC_MXPOLLLOOPS exceeded
*
****************************************************************************/
static boolean ez80emac_miipoll(FAR struct ez80emac_driver_s *priv, uint32 offset,
uint16 bits, boolean bclear)
{
uint16 value;
int i;
for (i = 0; i < EMAC_MXPOLLLOOPS; i++)
{
value = ez80emac_miiread(priv, offset);
if (bclear)
{
if ((value & bits) == 0)
{
return TRUE;
}
}
else
{
if ((value & bits) != 0)
{
return TRUE;
}
}
}
return FALSE;
}
/****************************************************************************
* Function: ez80emac_miiautonegotiate
*
* Description:
* Dump all MII registers
*
* Parameters:
* priv - Reference to the driver state structure
* offset - Register offset in PMD
* bits - Selects set of bits to wait for
* bclear - TRUE:Return true when all bits in 'bits' are 0
* FALSE:Return true when one or more bits in 'bits' are 1
*
* Returned Value:
* TRUE:Bit has requested polarity; FALSE: EMAC_MXPOLLLOOPS exceeded
*
****************************************************************************/
static void ez80emac_miiautonegotiate(FAR struct ez80emac_driver_s *priv)
{
uint16 advertise;
uint16 lpa;
uint16 mcr;
ubyte regval;
/* Start auto-negotiation */
ez80emac_miiwrite(priv, MII_MCR, 0);
ez80emac_miiwrite(priv,
MII_MCR,
MII_MCR_ANENABLE | MII_MCR_ANRESTART | MII_MCR_FULLDPLX | MII_MCR_SPEED100);
/* Wait for auto-negotiation to start */
if (!ez80emac_miipoll(priv, MII_MCR, MII_MCR_ANRESTART, FALSE))
{
ndbg("Autonegotiation didn't start.\n");
}
/* Wait for auto-negotiation to complete */
if (!ez80emac_miipoll(priv, MII_MSR, MII_MSR_ANEGCOMPLETE, TRUE))
{
ndbg("Autonegotiation didn't complete.\n");
}
/* Wait link */
if (!ez80emac_miipoll(priv, MII_MSR, MII_MSR_LINKSTATUS, TRUE))
{
ndbg("Link is down!\n");
priv->blinkok = FALSE;
}
else
{
priv->blinkok = TRUE;
}
/* Read capable media type */
up_udelay(500);
advertise = ez80emac_miiread(priv, MII_ADVERTISE);
lpa = ez80emac_miiread(priv, MII_LPA);
/* Check for 100BASETX full duplex */
if ((advertise & MII_ADVERTISE_100BASETXFULL) && (lpa & MII_LPA_100BASETXFULL))
{
ndbg("100BASETX full duplex\n");
regval = inp(EZ80_EMAC_CFG1);
regval |= EMAC_CFG1_FULLHD; /* Enable full duplex mode */
outp(EZ80_EMAC_CFG1, regval);
priv->b100mbs = TRUE;
priv->bfullduplex = TRUE;
}
/* Check for 100BASETX half duplex */
else if ((advertise & MII_ADVERTISE_100BASETXHALF) && (lpa & MII_LPA_100BASETXHALF))
{
ndbg("100BASETX half duplex\n");
regval = inp(EZ80_EMAC_CFG1);
regval &= ~EMAC_CFG1_FULLHD; /* Disable full duplex mode */
outp(EZ80_EMAC_CFG1, regval);
priv->b100mbs = TRUE;
priv->bfullduplex = FALSE;
}
/* Check for 10BASETX full duplex */
else if ((advertise & MII_ADVERTISE_10BASETXFULL) && (lpa & MII_LPA_10BASETXFULL))
{
ndbg("10BASETX full duplex\n");
regval = inp(EZ80_EMAC_CFG1);
regval |= EMAC_CFG1_FULLHD; /* Enable full duplex mode */
outp(EZ80_EMAC_CFG1, regval);
priv->b100mbs = FALSE;
priv->bfullduplex = TRUE;
}
/* Check for 10BASETX half duplex */
else if ((advertise & MII_ADVERTISE_10BASETXHALF) && (lpa & MII_LPA_10BASETXHALF))
{
ndbg("10BASETX half duplex\n");
regval = inp(EZ80_EMAC_CFG1);
regval &= ~EMAC_CFG1_FULLHD; /* Disable full duplex mode */
outp(EZ80_EMAC_CFG1, regval);
priv->b100mbs = FALSE;
priv->bfullduplex = FALSE;
}
else
{
ndbg("No valid connection; force 10Mbps half-duplex.\n");
regval = inp(EZ80_EMAC_CFG1);
regval &= ~EMAC_CFG1_FULLHD; /* Disable full duplex mode */
outp(EZ80_EMAC_CFG1, regval);
priv->b100mbs = FALSE;
priv->bfullduplex = FALSE;
}
/* set MII control */
mcr = ez80emac_miiread(priv, MII_MCR);
if (priv->bfullduplex)
{
mcr |= MII_MCR_FULLDPLX;
}
else
{
mcr &= ~MII_MCR_FULLDPLX;
}
if (priv->b100mbs)
{
mcr |= MII_MCR_SPEED100;
}
else
{
mcr &= ~MII_MCR_SPEED100;
}
mcr |= MII_MCR_ANENABLE;
ez80emac_miiwrite(priv, MII_MCR, mcr);
nvdbg("MII registers (FIAD=%lx)\n", CONFIG_EZ80_FIAD);
nvdbg(" %-10s: %04x\n", "MII_MCR", ez80emac_miiread(priv, MII_MCR));
nvdbg(" %-10s: %04x\n", "MII_MSR", ez80emac_miiread(priv, MII_MSR));
nvdbg(" %-10s: %04x\n", "MII_PHYID1", ez80emac_miiread(priv, MII_PHYID1));
nvdbg(" %-10s: %04x\n", "MII_PHYID2", ez80emac_miiread(priv, MII_PHYID2));
nvdbg(" %-10s: %04x\n", "MII_ADVERTISE", ez80emac_miiread(priv, MII_ADVERTISE));
nvdbg(" %-10s: %04x\n", "MII_LPA", ez80emac_miiread(priv, MII_LPA));
nvdbg(" %-10s: %04x\n", "MII_EXPANSION", ez80emac_miiread(priv, MII_EXPANSION));
nvdbg(" %-10s: %04x\n", "MII_LBRERROR", ez80emac_miiread(priv, MII_LBRERROR));
nvdbg(" %-10s: %04x\n", "MII_PHYADDR", ez80emac_miiread(priv, MII_PHYADDR));
}
/****************************************************************************
* Function: ez80emac_machash
*
* Description:
* Given a MAC address, perform the CRC32 calculation and return the
* index and bit number for the multi-cast hash table.
*
* Parameters:
* priv - Reference to the driver state structure
* mac - The MAC address to add
* enable - TRUE: Enable filtering on this address; FALSE: disable
*
* Returned Value:
* None
*
****************************************************************************/
#ifdef CONFIG_EZ80_MCFILTER
static void ez80emac_machash(FAR ubyte *mac, int *ndx, int *bitno)
{
uint32 hash;
uint32 crc32;
int i;
int j;
/* Calculate the CRC32 value on the MAC address */
crc32 = 0xffffffff;
for (i = 0; i < 6; i++)
{
crc32 ^= (uint32)mac[i] & 0x0f;
for (j = 0; j < 4; j++)
{
if (crc32 & 1)
{
crc32 = (crc32 >> 1) ^ EMAC_CRCPOLY2;
}
else
{
crc32 >>= 1;
}
}
crc32 ^= (uint32)mac[i] >> 4;
for (j = 0; j < 4; j++)
{
if (crc32 & 1)
{
crc32 = (crc32 >> 1) ^ EMAC_CRCPOLY2;
}
else
{
crc32 >>= 1;
}
}
}
/* The normal CRC result would be the complement of crc32,
* the following calculates the EMAC hash value
*
* This loop changes the bit ordering the for bits [23:28] of
* the CRC32 value to -> [0:5]
*/
crc32 &= 0x000001f8;
hash = 0;
for (j = 31; j >= 23; j--)
{
hash = (hash << 1) + (crc32 & 1);
crc32 >>= 1;
}
*ndx = (hash >> 3) & 7;
*bitno = hash & 7;
}
#endif
/****************************************************************************
* Function: ez80emac_transmit
*
* Description:
* Start hardware transmission. Called either from the txdone interrupt
* handling or from watchdog based polling.
*
* Parameters:
* priv - Reference to the driver state structure
*
* Returned Value:
* OK on success; a negated errno on failure
*
* Assumptions:
*
****************************************************************************/
static int ez80emac_transmit(struct ez80emac_driver_s *priv)
{
FAR struct ez80emac_desc_s *txdesc;
irqstate_t flags;
ubyte regval;
/* Increment statistics */
EMAC_STAT(priv, tx_queued);
/* Copy the data to the next packet in the Tx buffer */
flags = irqsave();
txdesc = priv->txnext;
txdesc->np = 0;
txdesc->pktsize = priv->dev.d_len;
txdesc->stat = 0;
memcpy((ubyte*)txdesc + SIZEOF_EMACSDESC, priv->dev.d_buf, priv->dev.d_len);
/* Add the new Tx descriptor to the tail of the chain. Is there a
* descriptor queued before this one?
*/
if (priv->txtail)
{
/* Yes, link it to this one */
priv->txtail->np = (uint24)txdesc;
}
else
{
/* No.. then there are no pending Tx actions. This
* descriptor is both the new head and tail.
*/
priv->txhead = txdesc;
}
/* In any event, this descriptor is the new tail */
priv->txtail = txdesc;
/* Caculate the address of the next, available Tx descriptor.
* Hmmm... need some way to determine if the next descriptor
* is in use or not.
*/
priv->txnext = (FAR struct ez80emac_desc_s *)
((FAR ubyte*)txdesc + txdesc->pktsize + SIZEOF_EMACSDESC);
if (priv->txnext >= priv->rxstart)
{
priv->txnext = priv->txstart;
}
/* Setup the new 'next' Tx descriptor (very dangerous with
* no check for overrun!)
*/
priv->txnext->np = 0;
priv->txnext->pktsize = 0;
priv->txnext->stat = 0;
/* Then, give ownership of the descriptor to the hardware. It should
* perform the transmission on its next polling cycle.
*/
txdesc->np = (uint24)priv->txnext;
txdesc->stat = EMAC_TXDESC_OWNER;
irqrestore(flags);
/* Setup the TX timeout watchdog (perhaps restarting the timer) */
(void)wd_start(priv->txtimeout, EMAC_TXTIMEOUT, ez80emac_txtimeout, 1, (uint32)priv);
return OK;
}
/****************************************************************************
* Function: ez80emac_uiptxpoll
*
* Description:
* The transmitter is available, check if uIP has any outgoing packets ready
* to send. This is a callback from uip_poll(). uip_poll() may be called:
*
* 1. When the preceding TX packet send is complete,
* 2. When the preceding TX packet send timesout and the interface is reset
* 3. During normal TX polling
*
* Parameters:
* dev - Reference to the NuttX driver state structure
*
* Returned Value:
* OK on success; a negated errno on failure
*
* Assumptions:
*
****************************************************************************/
static int ez80emac_uiptxpoll(struct uip_driver_s *dev)
{
struct ez80emac_driver_s *priv = (struct ez80emac_driver_s *)dev->d_private;
/* If the polling resulted in data that should be sent out on the network,
* the field d_len is set to a value > 0.
*/
if (priv->dev.d_len > 0)
{
uip_arp_out(&priv->dev);
ez80emac_transmit(priv);
/* Check if there is room in the DM90x0 to hold another packet. If not,
* return a non-zero value to terminate the poll.
*/
}
/* If zero is returned, the polling will continue until all connections have
* been examined.
*/
return 0;
}
/****************************************************************************
* Function: ez80emac_txinterrupt
*
* Description:
* Process Rx-related interrupt events
*
* Parameters:
* priv - Driver data instance
* istat - Snapshot of ISTAT register containing Rx events to provess
*
* Returned Value:
* None
*
****************************************************************************/
static int ez80emac_txinterrupt(int irq, FAR void *context)
{
FAR struct ez80emac_driver_s *priv = &g_emac;
FAR struct ez80emac_desc_s *txdesc = priv->txhead;
ubyte regval;
ubyte istat;
/* EMAC Tx interrupts:
*
* EMAC_ISTAT_TXDONE - Bit 0: 1=Transmit done interrupt
* Denotes when packet transmission is complete.
* EMAC_ISTAT_TXCF - Bit 1: 1=Transmit control frame interrupt
* Denotes when control frame transmission is complete.
*/
/* Get and clear Tx interrupt status bits */
istat = inp(EZ80_EMAC_ISTAT) & EMAC_ISTAT_TXEVENTS;
outp(EZ80_EMAC_ISTAT, istat);
/* All events are packet/control frame transmit complete events */
nvdbg("txhead=%p {%06x, %u, %04x} trp=%02x%02x istat=%02x\n",
txdesc, txdesc->np, txdesc->pktsize, txdesc->stat,
inp(EZ80_EMAC_TRP_H), inp(EZ80_EMAC_TRP_L), istat);
EMAC_STAT(priv, tx_int);
/* Check if the packet is still owned by the hardware */
if ((txdesc->stat & EMAC_TXDESC_OWNER) != 0)
{
ndbg("Descriptor %p still owned by H/W {%06x, %u, %04x} trp=%02x%02x istat=%02x\n",
txdesc, txdesc->np, txdesc->pktsize, txdesc->stat,
inp(EZ80_EMAC_TRP_H), inp(EZ80_EMAC_TRP_L), istat);
return OK;
}
/* Handle errors */
else if ((txdesc->stat & EMAC_TXDESC_ABORT) != 0)
{
ndbg("Descriptor %p aborted {%06x, %u, %04x} trp=%02x%02x istat=%02x\n",
txdesc, txdesc->np, txdesc->pktsize, txdesc->stat,
inp(EZ80_EMAC_TRP_H), inp(EZ80_EMAC_TRP_L), istat);
EMAC_STAT(priv, tx_errors);
EMAC_STAT(priv, tx_abterrors);
}
/* Get the address of the next Tx descriptor in the list (if any) */
priv->txhead = (FAR struct ez80emac_desc_s *)txdesc->np;
if (!priv->txhead)
{
/* If there is no new head, then there is no tail either */
priv->txnext = priv->txstart;
priv->txtail = NULL;
/* Reset the transmit function. That should force the TRP to be
* the same as TDLP which is the set to txstart.
*/
regval = inp(EZ80_EMAC_RST);
regval |= EMAC_RST_HRTFN;
outp(EZ80_EMAC_RST, regval);
regval &= ~EMAC_RST_HRTFN;
outp(EZ80_EMAC_RST, regval);
/* If no further xmits are pending, then cancel the TX timeout */
wd_cancel(priv->txtimeout);
}
/* Clean up the old Tx descriptor -- not really necessary */
txdesc->np = 0;
txdesc->pktsize = 0;
txdesc->stat = 0;
nvdbg("New txhead=%p {%06x, %u, %04x} trp=%02x%02x istat=%02x\n",
priv->txhead, priv->txhead->np, priv->txhead->pktsize, priv->txhead->stat,
inp(EZ80_EMAC_TRP_H), inp(EZ80_EMAC_TRP_L), istat);
return OK;
}
/****************************************************************************
* Function: ez80emac_rxinterrupt
*
* Description:
* Process Rx-related interrupt events
*
* Parameters:
* priv - Driver data instance
* istat - Snapshot of ISTAT register containing Rx events to provess
*
* Returned Value:
* None
*
****************************************************************************/
static int ez80emac_rxinterrupt(int irq, FAR void *context)
{
FAR struct ez80emac_driver_s *priv = &g_emac;
FAR struct ez80emac_desc_s *rxdesc = priv->rxnext;
ubyte istat;
int pktlen;
/* EMAC Rx interrupts:
*
* EMAC_ISTAT_RXDONE - Bit 3: 1=Receive done interrupt
* Denotes when packet reception is complete.
* EMAC_ISTAT_RXPCF - Bit 4: 1=Receive pause control frame interrupt
* Denotes when pause control frame reception is complete.
* EMAC_ISTAT_RXCF - Bit 5: 1=Receive control frame interrupt
* Denotes when control frame reception is complete.
*/
/* Get and clear Rx interrupt status bits */
istat = inp(EZ80_EMAC_ISTAT) & EMAC_ISTAT_RXEVENTS;
outp(EZ80_EMAC_ISTAT, istat);
EMAC_STAT(priv, rx_int);
/* The RRP register points to where the next Receive packet is read from.
* The read-only EMAC Receive Write Pointer (RWP) egisters reports the
* current RxDMA Receive Write pointer. The RxDMA block uses the RRP[12:5]
* to compare to RWP[12:5] for determining how many buffers remain. The
* result is the BLKSLFT register.
*/
nvdbg("rxnext=%p {%06x, %u, %04x} rrp=%02x%02x rwp=%02%02 blkslft=%02x istat=%02x\n",