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
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
|
/****************************************************************************
* arch/arm/src/tiva/tiva_ethernet.c
*
* Copyright (C) 2009-2010, 2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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_TIVA_ETHERNET)
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include <string.h>
#include <debug.h>
#include <wdog.h>
#include <errno.h>
#include <nuttx/irq.h>
#include <nuttx/arch.h>
#include <arch/board/board.h>
#include <nuttx/net/uip.h>
#include <nuttx/net/arp.h>
#include <nuttx/net/netdev.h>
#include "chip.h"
#include "up_arch.h"
#include "tiva_gpio.h"
#include "tiva_ethernet.h"
#include "chip/tiva_pinmap.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Half duplex can be forced if CONFIG_TIVA_ETHHDUPLEX is defined. */
#ifdef CONFIG_TIVA_ETHHDUPLEX
# define TIVA_DUPLEX_SETBITS 0
# define TIVA_DUPLEX_CLRBITS MAC_TCTL_DUPLEX
#else
# define TIVA_DUPLEX_SETBITS MAC_TCTL_DUPLEX
# define TIVA_DUPLEX_CLRBITS 0
#endif
/* Auto CRC generation can be suppressed if CONFIG_TIVA_ETHNOAUTOCRC is definde */
#ifdef CONFIG_TIVA_ETHNOAUTOCRC
# define TIVA_CRC_SETBITS 0
# define TIVA_CRC_CLRBITS MAC_TCTL_CRC
#else
# define TIVA_CRC_SETBITS MAC_TCTL_CRC
# define TIVA_CRC_CLRBITS 0
#endif
/* Tx padding can be suppressed if CONFIG_TIVA_ETHNOPAD is defined */
#ifdef CONFIG_TIVA_ETHNOPAD
# define TIVA_PADEN_SETBITS 0
# define TIVA_PADEN_CLRBITS MAC_TCTL_PADEN
#else
# define TIVA_PADEN_SETBITS MAC_TCTL_PADEN
# define TIVA_PADEN_CLRBITS 0
#endif
#define TIVA_TCTCL_SETBITS (TIVA_DUPLEX_SETBITS|TIVA_CRC_SETBITS|TIVA_PADEN_SETBITS)
#define TIVA_TCTCL_CLRBITS (TIVA_DUPLEX_CLRBITS|TIVA_CRC_CLRBITS|TIVA_PADEN_CLRBITS)
/* Multicast frames can be enabled by defining CONFIG_TIVA_MULTICAST */
#ifdef CONFIG_TIVA_MULTICAST
# define TIVA_AMUL_SETBITS MAC_RCTL_AMUL
# define TIVA_AMUL_CLRBITS 0
#else
# define TIVA_AMUL_SETBITS 0
# define TIVA_AMUL_CLRBITS MAC_RCTL_AMUL
#endif
/* Promiscuous mode can be enabled by defining CONFIG_TIVA_PROMISCUOUS */
#ifdef CONFIG_TIVA_PROMISCUOUS
# define TIVA_PRMS_SETBITS MAC_RCTL_PRMS
# define TIVA_PRMS_CLRBITS 0
#else
# define TIVA_PRMS_SETBITS 0
# define TIVA_PRMS_CLRBITS MAC_RCTL_PRMS
#endif
/* Bad CRC rejection can be enabled by define CONFIG_TIVA_BADCRC */
#ifdef CONFIG_TIVA_BADCRC
# define TIVA_BADCRC_SETBITS MAC_RCTL_BADCRC
# define TIVA_BADCRC_CLRBITS 0
#else
# define TIVA_BADCRC_SETBITS 0
# define TIVA_BADCRC_CLRBITS MAC_RCTL_BADCRC
#endif
#define TIVA_RCTCL_SETBITS (TIVA_AMUL_SETBITS|TIVA_PRMS_SETBITS|TIVA_BADCRC_SETBITS)
#define TIVA_RCTCL_CLRBITS (TIVA_AMUL_CLRBITS|TIVA_PRMS_CLRBITS|TIVA_BADCRC_CLRBITS)
/* CONFIG_TIVA_DUMPPACKET will dump the contents of each packet to the console. */
#ifdef CONFIG_TIVA_DUMPPACKET
# define tiva_dumppacket(m,a,n) lib_dumpbuffer(m,a,n)
#else
# define tiva_dumppacket(m,a,n)
#endif
/* TX poll deley = 1 seconds. CLK_TCK is the number of clock ticks per second */
#define TIVA_WDDELAY (1*CLK_TCK)
#define TIVA_POLLHSEC (1*2)
/* TX timeout = 1 minute */
#define TIVA_TXTIMEOUT (60*CLK_TCK)
/* This is a helper pointer for accessing the contents of the Ethernet header */
#define ETHBUF ((struct eth_hdr_s *)priv->ld_dev.d_buf)
#define TIVA_MAX_MDCCLK 2500000
/****************************************************************************
* Private Types
****************************************************************************/
/* EMAC statistics (debug only) */
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
struct tiva_statistics_s
{
uint32_t rx_int; /* Number of Rx interrupts received */
uint32_t rx_packets; /* Number of packets received (sum of the following): */
uint32_t rx_ip; /* Number of Rx IP packets received */
uint32_t rx_arp; /* Number of Rx ARP packets received */
uint32_t rx_dropped; /* Number of dropped, unsupported Rx packets */
uint32_t rx_pktsize; /* Number of dropped, too small or too big */
uint32_t rx_errors; /* Number of Rx errors (reception error) */
uint32_t rx_ovrerrors; /* Number of Rx FIFO overrun errors */
uint32_t tx_int; /* Number of Tx interrupts received */
uint32_t tx_packets; /* Number of Tx packets queued */
uint32_t tx_errors; /* Number of Tx errors (transmission error)*/
uint32_t tx_timeouts; /* Number of Tx timeout errors */
};
# define EMAC_STAT(priv,name) priv->ld_stat.name++
#else
# define EMAC_STAT(priv,name)
#endif
/* The tiva_driver_s encapsulates all state information for a single hardware
* interface
*/
struct tiva_driver_s
{
/* The following fields would only be necessary on chips that support
* multiple Ethernet controllers.
*/
#if TIVA_NETHCONTROLLERS > 1
uint32_t ld_base; /* Ethernet controller base address */
int ld_irq; /* Ethernet controller IRQ */
#endif
bool ld_bifup; /* true:ifup false:ifdown */
WDOG_ID ld_txpoll; /* TX poll timer */
WDOG_ID ld_txtimeout; /* TX timeout timer */
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
struct tiva_statistics_s ld_stat;
#endif
/* This holds the information visible to uIP/NuttX */
struct net_driver_s ld_dev; /* Interface understood by uIP */
};
/****************************************************************************
* Private Data
****************************************************************************/
static struct tiva_driver_s g_lm3sdev[TIVA_NETHCONTROLLERS];
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* Miscellaneous low level helpers */
#if TIVA_NETHCONTROLLERS > 1
static uint32_t tiva_ethin(struct tiva_driver_s *priv, int offset);
static void tiva_ethout(struct tiva_driver_s *priv, int offset, uint32_t value);
#else
static inline uint32_t tiva_ethin(struct tiva_driver_s *priv, int offset);
static inline void tiva_ethout(struct tiva_driver_s *priv, int offset, uint32_t value);
#endif
static void tiva_ethreset(struct tiva_driver_s *priv);
#if 0 /* Not used */
static void tiva_phywrite(struct tiva_driver_s *priv, int regaddr, uint16_t value);
#endif
static uint16_t tiva_phyread(struct tiva_driver_s *priv, int regaddr);
/* Common TX logic */
static int tiva_transmit(struct tiva_driver_s *priv);
static int tiva_txpoll(struct net_driver_s *dev);
/* Interrupt handling */
static void tiva_receive(struct tiva_driver_s *priv);
static void tiva_txdone(struct tiva_driver_s *priv);
static int tiva_interrupt(int irq, FAR void *context);
/* Watchdog timer expirations */
static void tiva_polltimer(int argc, uint32_t arg, ...);
static void tiva_txtimeout(int argc, uint32_t arg, ...);
/* NuttX callback functions */
static int tiva_ifup(struct net_driver_s *dev);
static int tiva_ifdown(struct net_driver_s *dev);
static int tiva_txavail(struct net_driver_s *dev);
#ifdef CONFIG_NET_IGMP
static int tiva_addmac(struct net_driver_s *dev, FAR const uint8_t *mac);
static int tiva_rmmac(struct net_driver_s *dev, FAR const uint8_t *mac);
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Function: tiva_ethin
*
* Description:
* Read a register from the Ethernet module
*
* Parameters:
* priv - Reference to the driver state structure
* offset - Byte offset of the register from the ethernet base address
*
* Returned Value:
* Register value
*
****************************************************************************/
#if TIVA_NETHCONTROLLERS > 1
static uint32_t tiva_ethin(struct tiva_driver_s *priv, int offset)
{
return getreg32(priv->ld_base + offset);
}
#else
static inline uint32_t tiva_ethin(struct tiva_driver_s *priv, int offset)
{
return getreg32(TIVA_ETHCON_BASE + offset);
}
#endif
/****************************************************************************
* Function: tiva_ethout
*
* Description:
* Write a register to the Ethernet module
*
* Parameters:
* priv - Reference to the driver state structure
* offset - Byte offset of the register from the ethernet base address
* value - The value to write the Ethernet register
*
* Returned Value:
* None
*
****************************************************************************/
#if TIVA_NETHCONTROLLERS > 1
static void tiva_ethout(struct tiva_driver_s *priv, int offset, uint32_t value)
{
putreg32(value, priv->ld_base + offset);
}
#else
static inline void tiva_ethout(struct tiva_driver_s *priv, int offset, uint32_t value)
{
putreg32(value, TIVA_ETHCON_BASE + offset);
}
#endif
/****************************************************************************
* Function: tiva_ethreset
*
* Description:
* Configure and reset the Ethernet module, leaving it in a disabled state.
*
* Parameters:
* priv - Reference to the driver state structure
*
* Returned Value:
* OK on success; a negated errno on failure
*
* Assumptions:
*
****************************************************************************/
static void tiva_ethreset(struct tiva_driver_s *priv)
{
irqstate_t flags;
uint32_t regval;
#if TIVA_NETHCONTROLLERS > 1
# error "If multiple interfaces are supported, this function would have to be redesigned"
#endif
/* Make sure that clocking is enabled for the Ethernet (and PHY) peripherals */
flags = irqsave();
regval = getreg32(TIVA_SYSCON_RCGC2);
regval |= (SYSCON_RCGC2_EMAC0|SYSCON_RCGC2_EPHY0);
putreg32(regval, TIVA_SYSCON_RCGC2);
nllvdbg("RCGC2: %08x\n", regval);
/* Put the Ethernet controller into the reset state */
regval = getreg32(TIVA_SYSCON_SRCR2);
regval |= (SYSCON_SRCR2_EMAC0|SYSCON_SRCR2_EPHY0);
putreg32(regval, TIVA_SYSCON_SRCR2);
/* Wait just a bit. This is a much longer delay than necessary */
up_mdelay(2);
/* Then take the Ethernet controller out of the reset state */
regval &= ~(SYSCON_SRCR2_EMAC0|SYSCON_SRCR2_EPHY0);
putreg32(regval, TIVA_SYSCON_SRCR2);
nllvdbg("SRCR2: %08x\n", regval);
/* Wait just a bit, again. If we touch the ethernet too soon, we may busfault. */
up_mdelay(2);
/* Enable Port F for Ethernet LEDs: LED0=Bit 3; LED1=Bit 2 */
#ifdef CONFIG_TIVA_ETHLEDS
/* Configure the pins for the peripheral function */
tiva_configgpio(GPIO_ETHPHY_LED0 | GPIO_STRENGTH_2MA | GPIO_PADTYPE_STD);
tiva_configgpio(GPIO_ETHPHY_LED1 | GPIO_STRENGTH_2MA | GPIO_PADTYPE_STD);
#endif
/* Disable all Ethernet controller interrupts */
regval = tiva_ethin(priv, TIVA_MAC_IM_OFFSET);
regval &= ~MAC_IM_ALLINTS;
tiva_ethout(priv, TIVA_MAC_IM_OFFSET, regval);
/* Clear any pending interrupts (shouldn't be any) */
regval = tiva_ethin(priv, TIVA_MAC_RIS_OFFSET);
tiva_ethout(priv, TIVA_MAC_IACK_OFFSET, regval);
irqrestore(flags);
}
/****************************************************************************
* Function: tiva_phywrite
*
* Description:
* Write a 16-bit word to a PHY register
*
* Parameters:
* priv - Reference to the driver state structure
* regaddr - Address of the PHY register to write
* value - The value to write to the register
*
* Returned Value:
* None
*
****************************************************************************/
#if 0 /* Not used */
static void tiva_phywrite(struct tiva_driver_s *priv, int regaddr, uint16_t value)
{
/* Wait for any MII transactions in progress to complete */
while ((tiva_ethin(priv, TIVA_MAC_MCTL_OFFSET) & MAC_MCTL_START) != 0);
/* Set up the data to be written */
DEBUGASSERT(value < MAC_MTXD_MASK);
tiva_ethout(priv, TIVA_MAC_MTXD_OFFSET, value);
/* Set up the PHY register address and start the write operation */
regaddr <<= MAC_MCTL_REGADR_SHIFT;
DEBUGASSERT((regaddr & MAC_MTXD_MASK) == regaddr);
tiva_ethout(priv, TIVA_MAC_MCTL_OFFSET, regaddr | MAC_MCTL_WRITE | MAC_MCTL_START);
/* Wait for the write transaction to complete */
while ((tiva_ethin(priv, TIVA_MAC_MCTL_OFFSET) & MAC_MCTL_START) != 0);
}
#endif
/****************************************************************************
* Function: tiva_phyread
*
* Description:
* Write a 16-bit word to a PHY register
*
* Parameters:
* priv - Reference to the driver state structure
* regaddr - Address of the PHY register to write
* value - The value to write to the register
*
* Returned Value:
* None
*
****************************************************************************/
static uint16_t tiva_phyread(struct tiva_driver_s *priv, int regaddr)
{
/* Wait for any MII transactions in progress to complete */
while ((tiva_ethin(priv, TIVA_MAC_MCTL_OFFSET) & MAC_MCTL_START) != 0);
/* Set up the PHY register address and start the read operation */
regaddr <<= MAC_MCTL_REGADR_SHIFT;
DEBUGASSERT((regaddr & MAC_MTXD_MASK) == regaddr);
tiva_ethout(priv, TIVA_MAC_MCTL_OFFSET, regaddr | MAC_MCTL_START);
/* Wait for the write transaction to complete */
while ((tiva_ethin(priv, TIVA_MAC_MCTL_OFFSET) & MAC_MCTL_START) != 0);
/* Read and return the PHY data */
return (uint16_t)(tiva_ethin(priv, TIVA_MAC_MRXD_OFFSET) & MAC_MTRD_MASK);
}
/****************************************************************************
* Function: tiva_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
*
****************************************************************************/
static int tiva_transmit(struct tiva_driver_s *priv)
{
irqstate_t flags;
uint32_t regval;
uint8_t *dbuf;
int pktlen;
int bytesleft;
int ret = -EBUSY;
/* Verify that the hardware is ready to send another packet */
flags = irqsave();
if ((tiva_ethin(priv, TIVA_MAC_TR_OFFSET) & MAC_TR_NEWTX) == 0)
{
/* Increment statistics */
EMAC_STAT(priv, tx_packets);
tiva_dumppacket("Transmit packet", priv->ld_dev.d_buf, priv->ld_dev.d_len);
/* Transfer the packet into the Tx FIFO. The LS 16-bits of the first
* 32-bit word written to the Tx FIFO contains the Ethernet payload
* data length. That is the full length of the message (d_len) minus
* the size of the Ethernet header (14).
*/
pktlen = priv->ld_dev.d_len;
nllvdbg("Sending packet, pktlen: %d\n", pktlen);
DEBUGASSERT(pktlen > NET_LLH_LEN);
dbuf = priv->ld_dev.d_buf;
regval = (uint32_t)(pktlen - 14);
regval |= ((uint32_t)(*dbuf++) << 16);
regval |= ((uint32_t)(*dbuf++) << 24);
tiva_ethout(priv, TIVA_MAC_DATA_OFFSET, regval);
/* Write all of the whole, 32-bit values in the middle of the packet */
for (bytesleft = pktlen - 2; bytesleft > 3; bytesleft -= 4, dbuf += 4)
{
/* Transfer a whole word from the user buffer. Note, the user
* buffer may be un-aligned.
*/
tiva_ethout(priv, TIVA_MAC_DATA_OFFSET, *(uint32_t*)dbuf);
}
/* Write the last, partial word in the FIFO */
if (bytesleft > 0)
{
/* Write the last word */
regval = 0;
switch (bytesleft)
{
case 0:
default:
break;
case 3:
regval |= ((uint32_t)dbuf[2] << 16);
case 2:
regval |= ((uint32_t)dbuf[1] << 8);
case 1:
regval |= (uint32_t)dbuf[0];
break;
}
tiva_ethout(priv, TIVA_MAC_DATA_OFFSET, regval);
}
/* Activate the transmitter */
tiva_ethout(priv, TIVA_MAC_TR_OFFSET, MAC_TR_NEWTX);
/* Setup the TX timeout watchdog (perhaps restarting the timer) */
(void)wd_start(priv->ld_txtimeout, TIVA_TXTIMEOUT, tiva_txtimeout, 1, (uint32_t)priv);
ret = OK;
}
irqrestore(flags);
return ret;
}
/****************************************************************************
* Function: tiva_txpoll
*
* Description:
* The transmitter is available, check if uIP has any outgoing packets ready
* to send. This is a callback from devif_poll(). devif_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 tiva_txpoll(struct net_driver_s *dev)
{
struct tiva_driver_s *priv = (struct tiva_driver_s *)dev->d_private;
int ret = OK;
/* If the polling resulted in data that should be sent out on the network,
* the field d_len is set to a value > 0.
*/
nllvdbg("Poll result: d_len=%d\n", priv->ld_dev.d_len);
if (priv->ld_dev.d_len > 0)
{
/* Send the packet. tiva_transmit() will return zero if the
* packet was successfully handled.
*/
DEBUGASSERT((tiva_ethin(priv, TIVA_MAC_TR_OFFSET) & MAC_TR_NEWTX) == 0)
arp_out(&priv->ld_dev);
ret = tiva_transmit(priv);
}
/* If zero is returned, the polling will continue until all connections have
* been examined.
*/
return ret;
}
/****************************************************************************
* Function: tiva_receive
*
* Description:
* An interrupt was received indicating the availability of a new RX packet
*
* Parameters:
* priv - Reference to the driver state structure
*
* Returned Value:
* None
*
* Assumptions:
*
****************************************************************************/
static void tiva_receive(struct tiva_driver_s *priv)
{
uint32_t regval;
uint8_t *dbuf;
int pktlen;
int bytesleft;
/* Loop while there are incoming packets to be processed */
while ((tiva_ethin(priv, TIVA_MAC_NP_OFFSET) & MAC_NP_MASK) != 0)
{
/* Update statistics */
EMAC_STAT(priv, rx_packets);
/* Copy the data data from the hardware to priv->ld_dev.d_buf. Set
* amount of data in priv->ld_dev.d_len
*/
dbuf = priv->ld_dev.d_buf;
/* The packet frame length begins in the LS 16-bits of the first
* word from the FIFO followed by the Ethernet header beginning
* in the MS 16-bits of the first word.
*
* Pick off the packet length from the first word. This packet length
* includes the len/type field (size 2) and the FCS (size 4).
*/
regval = tiva_ethin(priv, TIVA_MAC_DATA_OFFSET);
pktlen = (int)(regval & 0x0000ffff);
nllvdbg("Receiving packet, pktlen: %d\n", pktlen);
/* Check if the pktlen is valid. It should be large enough to hold
* an Ethernet header and small enough to fit entirely in the I/O
* buffer. Six is subtracted to acount for the 2-byte length/type
* and 4 byte FCS that are not copied into the uIP packet.
*/
if (pktlen > (CONFIG_NET_BUFSIZE + 6) || pktlen <= (NET_LLH_LEN + 6))
{
int wordlen;
/* We will have to drop this packet */
nlldbg("Bad packet size dropped (%d)\n", pktlen);
EMAC_STAT(priv, rx_pktsize);
/* The number of bytes and words left to read is pktlen - 4 (including,
* the final, possibly partial word) because we've already read 4 bytes.
*/
wordlen = (pktlen - 1) >> 2;
/* Read and discard the remaining words in the FIFO */
while (wordlen--)
{
(void)tiva_ethin(priv, TIVA_MAC_DATA_OFFSET);
}
/* Check for another packet */
continue;
}
/* Save the first two bytes from the first word */
*dbuf++ = (uint8_t)((regval >> 16) & 0xff);
*dbuf++ = (uint8_t)((regval >> 24) & 0xff);
/* Read all of the whole, 32-bit values in the middle of the packet.
* We've already read the length (2 bytes) plus the first two bytes
* of data.
*/
for (bytesleft = pktlen - 4; bytesleft > 7; bytesleft -= 4, dbuf += 4)
{
/* Transfer a whole word to the user buffer. Note, the user
* buffer may be un-aligned.
*/
*(uint32_t*)dbuf = tiva_ethin(priv, TIVA_MAC_DATA_OFFSET);
}
/* Handle the last, partial word in the FIFO (0-3 bytes) and discard
* the 4-byte FCS.
*/
for (; bytesleft > 0; bytesleft -= 4)
{
/* Read the last word. And transfer all but the last four
* bytes of the FCS into the user buffer.
*/
regval = tiva_ethin(priv, TIVA_MAC_DATA_OFFSET);
switch (bytesleft)
{
default:
break;
case 7:
dbuf[2] = (regval >> 16) & 0xff;
case 6:
dbuf[1] = (regval >> 8) & 0xff;
case 5:
dbuf[0] = regval & 0xff;
break;
}
}
/* Pass the packet length to uIP MINUS 2 bytes for the length and
* 4 bytes for the FCS.
*/
priv->ld_dev.d_len = pktlen - 6;
tiva_dumppacket("Received packet", priv->ld_dev.d_buf, priv->ld_dev.d_len);
/* We only accept IP packets of the configured type and ARP packets */
#ifdef CONFIG_NET_IPv6
if (ETHBUF->type == HTONS(ETHTYPE_IP6))
#else
if (ETHBUF->type == HTONS(ETHTYPE_IP))
#endif
{
nllvdbg("IP packet received (%02x)\n", ETHBUF->type);
EMAC_STAT(priv, rx_ip);
arp_ipin(&priv->ld_dev);
devif_input(&priv->ld_dev);
/* If the above function invocation resulted in data that should be
* sent out on the network, the field d_len will set to a value > 0.
*/
if (priv->ld_dev.d_len > 0)
{
arp_out(&priv->ld_dev);
tiva_transmit(priv);
}
}
else if (ETHBUF->type == htons(ETHTYPE_ARP))
{
nllvdbg("ARP packet received (%02x)\n", ETHBUF->type);
EMAC_STAT(priv, rx_arp);
arp_arpin(&priv->ld_dev);
/* If the above function invocation resulted in data that should be
* sent out on the network, the field d_len will set to a value > 0.
*/
if (priv->ld_dev.d_len > 0)
{
tiva_transmit(priv);
}
}
#ifdef CONFIG_DEBUG
else
{
nlldbg("Unsupported packet type dropped (%02x)\n", htons(ETHBUF->type));
EMAC_STAT(priv, rx_dropped);
}
#endif
}
}
/****************************************************************************
* Function: tiva_txdone
*
* Description:
* An interrupt was received indicating that the last TX packet(s) is done
*
* Parameters:
* priv - Reference to the driver state structure
*
* Returned Value:
* None
*
* Assumptions:
*
****************************************************************************/
static void tiva_txdone(struct tiva_driver_s *priv)
{
/* Cancel the TX timeout */
wd_cancel(priv->ld_txtimeout);
/* Verify that the Tx FIFO is not in use. The NEWTX bit initiates an
* Ethernet transmission once the packet has been placed in the TX FIFO.
* This bit is cleared once the transmission has been completed. Since
* we get here because of of TXEMP which indicates that the packet was
* transmitted and that the TX FIFO is empty, NEWTX should always be zero
* at this point.
*/
DEBUGASSERT((tiva_ethin(priv, TIVA_MAC_TR_OFFSET) & MAC_TR_NEWTX) == 0)
/* Then poll uIP for new XMIT data */
(void)devif_poll(&priv->ld_dev, tiva_txpoll);
}
/****************************************************************************
* Function: tiva_interrupt
*
* Description:
* Hardware interrupt handler
*
* Parameters:
* irq - Number of the IRQ that generated the interrupt
* context - Interrupt register state save info (architecture-specific)
*
* Returned Value:
* OK on success
*
* Assumptions:
*
****************************************************************************/
static int tiva_interrupt(int irq, FAR void *context)
{
register struct tiva_driver_s *priv;
uint32_t ris;
#if TIVA_NETHCONTROLLERS > 1
# error "A mechanism to associate and interface with an IRQ is needed"
#else
priv = &g_lm3sdev[0];
#endif
/* Read the raw interrupt status register */
ris = tiva_ethin(priv, TIVA_MAC_RIS_OFFSET);
/* Clear all pending interrupts */
tiva_ethout(priv, TIVA_MAC_IACK_OFFSET, ris);
/* Check for errors */
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
if ((ris & MAC_RIS_TXER) != 0)
{
EMAC_STAT(priv, tx_errors); /* Number of Tx errors */
}
if ((ris & MAC_RIS_FOV) != 0)
{
EMAC_STAT(priv, rx_ovrerrors); /* Number of Rx FIFO overrun errors */
}
if ((ris & MAC_RIS_RXER) != 0)
{
EMAC_STAT(priv, rx_errors); /* Number of Rx errors */
}
#endif
/* Handle (unmasked) interrupts according to status bit settings */
ris &= tiva_ethin(priv, TIVA_MAC_IM_OFFSET);
/* Is this an Rx interrupt (meaning that a packet has been received)? */
if ((ris & MAC_RIS_RXINT) != 0)
{
/* Handle the incoming packet */
EMAC_STAT(priv, rx_int);
tiva_receive(priv);
}
/* Is this an Tx interrupt (meaning that the Tx FIFO is empty)? */
if ((ris & MAC_RIS_TXEMP) != 0)
{
/* Handle the complete of the transmission */
EMAC_STAT(priv, tx_int);
tiva_txdone(priv);
}
/* Enable Ethernet interrupts (perhaps excluding the TX done interrupt if
* there are no pending transmissions).
*/
return OK;
}
/****************************************************************************
* Function: tiva_txtimeout
*
* Description:
* Our TX watchdog timed out. Called from the timer interrupt handler.
* The last TX never completed. Reset the hardware and start again.
*
* Parameters:
* argc - The number of available arguments
* arg - The first argument
*
* Returned Value:
* None
*
* Assumptions:
*
****************************************************************************/
static void tiva_txtimeout(int argc, uint32_t arg, ...)
{
struct tiva_driver_s *priv = (struct tiva_driver_s *)arg;
/* Increment statistics */
nlldbg("Tx timeout\n");
EMAC_STAT(priv, tx_timeouts);
/* Then reset the hardware */
DEBUGASSERT(priv->ld_bifup);
tiva_ifdown(&priv->ld_dev);
tiva_ifup(&priv->ld_dev);
/* Then poll uIP for new XMIT data */
(void)devif_poll(&priv->ld_dev, tiva_txpoll);
}
/****************************************************************************
* Function: tiva_polltimer
*
* Description:
* Periodic timer handler. Called from the timer interrupt handler.
*
* Parameters:
* argc - The number of available arguments
* arg - The first argument
*
* Returned Value:
* None
*
* Assumptions:
*
****************************************************************************/
static void tiva_polltimer(int argc, uint32_t arg, ...)
{
struct tiva_driver_s *priv = (struct tiva_driver_s *)arg;
/* Check if we can send another Tx packet now. The NEWTX bit initiates an
* Ethernet transmission once the packet has been placed in the TX FIFO.
* This bit is cleared once the transmission has been completed.
*
* NOTE: This can cause missing poll cycles and, hence, some timing
* inaccuracies.
*/
if ((tiva_ethin(priv, TIVA_MAC_TR_OFFSET) & MAC_TR_NEWTX) == 0)
{
/* If so, update TCP timing states and poll uIP for new XMIT data */
(void)devif_timer(&priv->ld_dev, tiva_txpoll, TIVA_POLLHSEC);
/* Setup the watchdog poll timer again */
(void)wd_start(priv->ld_txpoll, TIVA_WDDELAY, tiva_polltimer, 1, arg);
}
}
/****************************************************************************
* Function: tiva_ifup
*
* Description:
* NuttX Callback: Bring up the Ethernet interface when an IP address is
* provided
*
* Parameters:
* dev - Reference to the NuttX driver state structure
*
* Returned Value:
* None
*
* Assumptions:
*
****************************************************************************/
static int tiva_ifup(struct net_driver_s *dev)
{
struct tiva_driver_s *priv = (struct tiva_driver_s *)dev->d_private;
irqstate_t flags;
uint32_t regval;
uint32_t div;
uint16_t phyreg;
nlldbg("Bringing up: %d.%d.%d.%d\n",
dev->d_ipaddr & 0xff, (dev->d_ipaddr >> 8) & 0xff,
(dev->d_ipaddr >> 16) & 0xff, dev->d_ipaddr >> 24 );
/* Enable and reset the Ethernet controller */
flags = irqsave();
tiva_ethreset(priv);
/* Set the management clock divider register for access to the PHY
* register set. The MDC clock is divided down from the system clock per:
*
* MDCCLK_FREQUENCY = SYSCLK_FREQUENCY / (2 * (div + 1))
* div = (SYSCLK_FREQUENCY / 2 / MDCCLK_FREQUENCY) - 1
*
* Where the maximum value for MDCCLK_FREQUENCY is 2,500,000. We will
* add 1 to assure the max TIVA_MAX_MDCCLK is not exceeded.
*/
div = SYSCLK_FREQUENCY / 2 / TIVA_MAX_MDCCLK;
tiva_ethout(priv, TIVA_MAC_MDV_OFFSET, div);
nllvdbg("MDV: %08x\n", div);
/* Then configure the Ethernet Controller for normal operation
*
* Setup the transmit control register (Full duplex, TX CRC Auto Generation,
* TX Padding Enabled).
*/
regval = tiva_ethin(priv, TIVA_MAC_TCTL_OFFSET);
regval &= ~TIVA_TCTCL_CLRBITS;
regval |= TIVA_TCTCL_SETBITS;
tiva_ethout(priv, TIVA_MAC_TCTL_OFFSET, regval);
nllvdbg("TCTL: %08x\n", regval);
/* Setup the receive control register (Disable multicast frames, disable
* promiscuous mode, disable bad CRC rejection).
*/
regval = tiva_ethin(priv, TIVA_MAC_RCTL_OFFSET);
regval &= ~TIVA_RCTCL_CLRBITS;
regval |= TIVA_RCTCL_SETBITS;
tiva_ethout(priv, TIVA_MAC_RCTL_OFFSET, regval);
nllvdbg("RCTL: %08x\n", regval);
/* Setup the time stamp configuration register */
#ifdef TIVA_ETHTS
regval = tiva_ethin(priv, TIVA_MAC_TS_OFFSET);
#ifdef CONFIG_TIVA_TIMESTAMP
regval |= MAC_TS_EN;
#else
regval &= ~(MAC_TS_EN);
#endif
tiva_ethout(priv, TIVA_MAC_TS_OFFSET, regval);
nllvdbg("TS: %08x\n", regval);
#endif
/* Wait for the link to come up. This following is not very conservative
* of system resources -- it really should wait gracefully on a semaphore
* and the interrupt handler should post the semaphore when LINKSTATUS is
* set
*/
nlldbg("Waiting for link\n");
do
{
phyreg = tiva_phyread(priv, MII_MSR);
}
while ((phyreg & MII_MSR_LINKSTATUS) == 0);
nlldbg("Link established\n");
/* Reset the receive FIFO */
regval = tiva_ethin(priv, TIVA_MAC_RCTL_OFFSET);
regval |= MAC_RCTL_RSTFIFO;
tiva_ethout(priv, TIVA_MAC_RCTL_OFFSET, regval);
/* Enable the Ethernet receiver */
regval = tiva_ethin(priv, TIVA_MAC_RCTL_OFFSET);
regval |= MAC_RCTL_RXEN;
tiva_ethout(priv, TIVA_MAC_RCTL_OFFSET, regval);
/* Enable the Ethernet transmitter */
regval = tiva_ethin(priv, TIVA_MAC_TCTL_OFFSET);
regval |= MAC_TCTL_TXEN;
tiva_ethout(priv, TIVA_MAC_TCTL_OFFSET, regval);
/* Reset the receive FIFO (again) */
regval = tiva_ethin(priv, TIVA_MAC_RCTL_OFFSET);
regval |= MAC_RCTL_RSTFIFO;
tiva_ethout(priv, TIVA_MAC_RCTL_OFFSET, regval);
/* Enable the Ethernet interrupt */
#if TIVA_NETHCONTROLLERS > 1
up_enable_irq(priv->irq);
#else
up_enable_irq(TIVA_IRQ_ETHCON);
#endif
/* Enable the Ethernet RX packet receipt interrupt */
regval = tiva_ethin(priv, TIVA_MAC_IM_OFFSET);
regval |= MAC_IM_RXINTM;
tiva_ethout(priv, TIVA_MAC_IM_OFFSET, regval);
/* Program the hardware with it's MAC address (for filtering) */
regval = (uint32_t)priv->ld_dev.d_mac.ether_addr_octet[3] << 24 |
(uint32_t)priv->ld_dev.d_mac.ether_addr_octet[2] << 16 |
(uint32_t)priv->ld_dev.d_mac.ether_addr_octet[1] << 8 |
(uint32_t)priv->ld_dev.d_mac.ether_addr_octet[0];
tiva_ethout(priv, TIVA_MAC_IA0_OFFSET, regval);
regval = (uint32_t)priv->ld_dev.d_mac.ether_addr_octet[5] << 8 |
(uint32_t)priv->ld_dev.d_mac.ether_addr_octet[4];
tiva_ethout(priv, TIVA_MAC_IA1_OFFSET, regval);
/* Set and activate a timer process */
(void)wd_start(priv->ld_txpoll, TIVA_WDDELAY, tiva_polltimer, 1, (uint32_t)priv);
priv->ld_bifup = true;
irqrestore(flags);
return OK;
}
/****************************************************************************
* Function: tiva_ifdown
*
* Description:
* NuttX Callback: Stop the interface. The only way to restore normal
* behavior is to call tiva_ifup().
*
* Parameters:
* dev - Reference to the NuttX driver state structure
*
* Returned Value:
* None
*
* Assumptions:
*
****************************************************************************/
static int tiva_ifdown(struct net_driver_s *dev)
{
struct tiva_driver_s *priv = (struct tiva_driver_s *)dev->d_private;
irqstate_t flags;
uint32_t regval;
nlldbg("Taking down: %d.%d.%d.%d\n",
dev->d_ipaddr & 0xff, (dev->d_ipaddr >> 8) & 0xff,
(dev->d_ipaddr >> 16) & 0xff, dev->d_ipaddr >> 24 );
/* Cancel the TX poll timer and TX timeout timers */
flags = irqsave();
wd_cancel(priv->ld_txpoll);
wd_cancel(priv->ld_txtimeout);
/* Disable the Ethernet interrupt */
#if TIVA_NETHCONTROLLERS > 1
up_disable_irq(priv->irq);
#else
up_disable_irq(TIVA_IRQ_ETHCON);
#endif
/* Disable all Ethernet controller interrupt sources */
regval = tiva_ethin(priv, TIVA_MAC_IM_OFFSET);
regval &= ~MAC_IM_ALLINTS;
tiva_ethout(priv, TIVA_MAC_IM_OFFSET, regval);
/* Reset the receive FIFO */
regval = tiva_ethin(priv, TIVA_MAC_RCTL_OFFSET);
regval |= MAC_RCTL_RSTFIFO;
tiva_ethout(priv, TIVA_MAC_RCTL_OFFSET, regval);
/* Disable the Ethernet receiver */
regval = tiva_ethin(priv, TIVA_MAC_RCTL_OFFSET);
regval &= ~MAC_RCTL_RXEN;
tiva_ethout(priv, TIVA_MAC_RCTL_OFFSET, regval);
/* Disable the Ethernet transmitter */
regval = tiva_ethin(priv, TIVA_MAC_RCTL_OFFSET);
regval &= ~MAC_TCTL_TXEN;
tiva_ethout(priv, TIVA_MAC_TCTL_OFFSET, regval);
/* Reset the receive FIFO (again) */
regval = tiva_ethin(priv, TIVA_MAC_RCTL_OFFSET);
regval |= MAC_RCTL_RSTFIFO;
tiva_ethout(priv, TIVA_MAC_RCTL_OFFSET, regval);
/* Clear any pending interrupts */
regval = tiva_ethin(priv, TIVA_MAC_RIS_OFFSET);
tiva_ethout(priv, TIVA_MAC_IACK_OFFSET, regval);
/* The interface is now DOWN */
priv->ld_bifup = false;
irqrestore(flags);
return OK;
}
/****************************************************************************
* Function: tiva_txavail
*
* Description:
* Driver callback invoked when new TX data is available. This is a
* stimulus perform an out-of-cycle poll and, thereby, reduce the TX
* latency.
*
* Parameters:
* dev - Reference to the NuttX driver state structure
*
* Returned Value:
* None
*
* Assumptions:
* Called in normal user mode
*
****************************************************************************/
static int tiva_txavail(struct net_driver_s *dev)
{
struct tiva_driver_s *priv = (struct tiva_driver_s *)dev->d_private;
irqstate_t flags;
/* Ignore the notification if the interface is not yet up or if the Tx FIFO
* hardware is not available at this time. The NEWTX bit initiates an
* Ethernet transmission once the packet has been placed in the TX FIFO.
* This bit is cleared once the transmission has been completed. When the
* transmission completes, tiva_txdone() will be called and the Tx polling
* will occur at that time.
*/
flags = irqsave();
if (priv->ld_bifup && (tiva_ethin(priv, TIVA_MAC_TR_OFFSET) & MAC_TR_NEWTX) == 0)
{
/* If the interface is up and we can use the Tx FIFO, then poll uIP
* for new Tx data
*/
(void)devif_poll(&priv->ld_dev, tiva_txpoll);
}
irqrestore(flags);
return OK;
}
/****************************************************************************
* Function: tiva_addmac
*
* Description:
* NuttX Callback: Add the specified MAC address to the hardware multicast
* address filtering
*
* Parameters:
* dev - Reference to the NuttX driver state structure
* mac - The MAC address to be added
*
* Returned Value:
* None
*
* Assumptions:
*
****************************************************************************/
#ifdef CONFIG_NET_IGMP
static int tiva_addmac(struct net_driver_s *dev, FAR const uint8_t *mac)
{
FAR struct tiva_driver_s *priv = (FAR struct tiva_driver_s *)dev->d_private;
/* Add the MAC address to the hardware multicast routing table */
#warning "Multicast MAC support not implemented"
return OK;
}
#endif
/****************************************************************************
* Function: tiva_rmmac
*
* Description:
* NuttX Callback: Remove the specified MAC address from the hardware multicast
* address filtering
*
* Parameters:
* dev - Reference to the NuttX driver state structure
* mac - The MAC address to be removed
*
* Returned Value:
* None
*
* Assumptions:
*
****************************************************************************/
#ifdef CONFIG_NET_IGMP
static int tiva_rmmac(struct net_driver_s *dev, FAR const uint8_t *mac)
{
FAR struct tiva_driver_s *priv = (FAR struct tiva_driver_s *)dev->d_private;
/* Add the MAC address to the hardware multicast routing table */
#warning "Multicast MAC support not implemented"
return OK;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Function: tiva_ethinitialize
*
* Description:
* Initialize the Ethernet driver for one interface
*
* Parameters:
* None
*
* Returned Value:
* OK on success; Negated errno on failure.
*
* Assumptions:
*
****************************************************************************/
#if TIVA_NETHCONTROLLERS > 1
int tiva_ethinitialize(int intf)
#else
static inline int tiva_ethinitialize(int intf)
#endif
{
struct tiva_driver_s *priv = &g_lm3sdev[intf];
int ret;
/* Check if the Ethernet module is present */
ndbg("Setting up eth%d\n", intf);
#if TIVA_NETHCONTROLLERS > 1
# error "This debug check only works with one interface"
#else
DEBUGASSERT((getreg32(TIVA_SYSCON_DC4) & (SYSCON_DC4_EMAC0|SYSCON_DC4_EPHY0)) == (SYSCON_DC4_EMAC0|SYSCON_DC4_EPHY0));
#endif
DEBUGASSERT((unsigned)intf < TIVA_NETHCONTROLLERS);
/* Initialize the driver structure */
memset(priv, 0, sizeof(struct tiva_driver_s));
priv->ld_dev.d_ifup = tiva_ifup; /* I/F down callback */
priv->ld_dev.d_ifdown = tiva_ifdown; /* I/F up (new IP address) callback */
priv->ld_dev.d_txavail = tiva_txavail; /* New TX data callback */
#ifdef CONFIG_NET_IGMP
priv->ld_dev.d_addmac = tiva_addmac; /* Add multicast MAC address */
priv->ld_dev.d_rmmac = tiva_rmmac; /* Remove multicast MAC address */
#endif
priv->ld_dev.d_private = (void*)priv; /* Used to recover private state from dev */
/* Create a watchdog for timing polling for and timing of transmisstions */
#if TIVA_NETHCONTROLLERS > 1
# error "A mechanism to associate base address an IRQ with an interface is needed"
priv->ld_base = ??; /* Ethernet controller base address */
priv->ld_irq = ??; /* Ethernet controller IRQ number */
#endif
priv->ld_txpoll = wd_create(); /* Create periodic poll timer */
priv->ld_txtimeout = wd_create(); /* Create TX timeout timer */
/* If the board can provide us with a MAC address, get the address
* from the board now. The MAC will not be applied until tiva_ifup()
* is caleld (and the MAC can be overwritten with a netdev ioctl call).
*/
#ifdef CONFIG_TIVA_BOARDMAC
tiva_ethernetmac(&priv->ld_dev.d_mac);
#endif
/* Perform minimal, one-time initialization -- just reset the controller and
* leave it disabled. The Ethernet controller will be reset and properly
* re-initialized each time tiva_ifup() is called.
*/
tiva_ethreset(priv);
tiva_ifdown(&priv->ld_dev);
/* Attach the IRQ to the driver */
#if TIVA_NETHCONTROLLERS > 1
ret = irq_attach(priv->irq, tiva_interrupt);
#else
ret = irq_attach(TIVA_IRQ_ETHCON, tiva_interrupt);
#endif
if (ret != 0)
{
/* We could not attach the ISR to the IRQ */
return -EAGAIN;
}
/* Register the device with the OS so that socket IOCTLs can be performed */
(void)netdev_register(&priv->ld_dev);
return OK;
}
/************************************************************************************
* Name: up_netinitialize
*
* Description:
* Initialize the first network interface. If there are more than one interface
* in the chip, then board-specific logic will have to provide this function to
* determine which, if any, Ethernet controllers should be initialized.
*
************************************************************************************/
#if TIVA_NETHCONTROLLERS == 1
void up_netinitialize(void)
{
(void)tiva_ethinitialize(0);
}
#endif
#endif /* CONFIG_NET && CONFIG_TIVA_ETHERNET */
|