Skip to content
Snippets Groups Projects
ez80_emac.c 53.2 KiB
Newer Older
patacongo's avatar
patacongo committed
/****************************************************************************
 * 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)
patacongo's avatar
patacongo committed
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 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803
#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",
        rxdesc, rxdesc->np, rxdesc->pktsize, rxdesc->stat,
        inp(EZ80_EMAC_RRP_H), inp(EZ80_EMAC_RRP_L),
        inp(EZ80_EMAC_RWP_H), inp(EZ80_EMAC_RWP_L),
        inp(EZ80_EMAC_BLKSLFT_H), inp(EZ80_EMAC_BLKSLFT_L),
        istat);

  /* The RxDMA reads the data from the RxFIFO and stores it in the EMAC
   * memory Receive buffer. When the end of the packet is detected, the
   * RxDMA reads the next two bytes from the RxFIFO and writes them into
   * the Rx descriptor status LSB and MSB. The packet length counter is
   * stored into the descriptor table packet length field, the descriptor
   * table next pointer is written into the Rx descriptor table and finally 
   * the Rx_DONE_STAT bit in the EMAC Interrupt Status Register register is
   * set to 1.
   */

  /* Check if this packet was received intact */

  if ((rxdesc->stat & EMAC_RXDESC_OK) == 0)
    {
      /* No frame received in this descriptor yet */

      return OK;
    }

  /* We have a good packet. Check if the packet is a valid size
   * for the uIP buffer configuration
   */

  if (rxdesc->pktsize > CONFIG_NET_BUFSIZE)
    {
      nvdbg("receive oversize pkt\n");
      pktlen = CONFIG_NET_BUFSIZE;
    }
  else
    {
      pktlen = rxdesc->pktsize;
    }

  /* Copy the data data from the hardware to priv->dev.d_buf.  Set
   * amount of data in priv->dev.d_len
   */

  memcpy(priv->dev.d_buf, (FAR ubyte*)priv->rxnext + SIZEOF_EMACSDESC, pktlen);
  priv->dev.d_len = pktlen;

  /* Reclaim the Rx descriptor */

  priv->rxnext    = (FAR struct ez80emac_desc_s *)rxdesc->np;

  rxdesc->np      = 0;
  rxdesc->pktsize = 0;
  rxdesc->stat    = 0;

  nvdbg("rxnext=%p {%06x, %u, %04x} rrp=%02x%02x rwp=%02%02 blkslft=%02x istat=%02x\n",
        rxdesc, rxdesc->np, rxdesc->pktsize, rxdesc->stat,
        inp(EZ80_EMAC_RRP_H), inp(EZ80_EMAC_RRP_L),
        inp(EZ80_EMAC_RWP_H), inp(EZ80_EMAC_RWP_L),
        inp(EZ80_EMAC_BLKSLFT_H), inp(EZ80_EMAC_BLKSLFT_L),
        istat);

  /* We only accept IP packets of the configured type and ARP packets */

#ifdef CONFIG_NET_IPv6
  if (ETHBUF->type == HTONS(UIP_ETHTYPE_IP6))
#else
  if (ETHBUF->type == HTONS(UIP_ETHTYPE_IP))
#endif
    {
      nvdbg("IP packet received (%02x)\n", ETHBUF->type);
      EMAC_STAT(priv, rx_ip);

      uip_arp_ipin();
      uip_input(&priv->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->dev.d_len > 0)
        {
          uip_arp_out(&priv->dev);
          ez80emac_transmit(priv);
        }
    }
  else if (ETHBUF->type == htons(UIP_ETHTYPE_ARP))
    {
      nvdbg("ARP packet received (%02x)\n", ETHBUF->type);
      EMAC_STAT(priv, rx_arp);

      uip_arp_arpin(&priv->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->dev.d_len > 0)
        {
          ez80emac_transmit(priv);
        }
    }
#ifdef CONFIG_DEBUG
  else
    {
      ndbg("Unsupported packet type dropped (%02x)\n", ETHBUF->type);
      EMAC_STAT(priv, rx_dropped);
    }
#endif
  return OK;
}

/****************************************************************************
 * Function: ez80emac_sysinterrupt
 *
 * 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 ez80emac_sysinterrupt(int irq, FAR void *context)
{
  FAR struct ez80emac_driver_s *priv = &g_emac;
  ubyte events;
  ubyte istat;

  /* 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.
   */

  EMAC_STAT(priv, sys_int);

  /* Get and clear system interrupt status bits */

  istat = inp(EZ80_EMAC_ISTAT) & EMAC_ISTAT_SYSEVENTS;
  outp(EZ80_EMAC_ISTAT, istat);

  /* Check for transmit state machine error */

  if ((istat & EMAC_ISTAT_TXFSMERR) != 0)
    {
      ndbg("Tx FSMERR 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);

      /* Increment statistics */

      EMAC_STAT(priv, tx_errors);
      EMAC_STAT(priv, tx_fsmerrors);

      /* Really need to reset the transmitter module here */
    }

  /* Check for Rx overrun error */

  if ((istat & EMAC_ISTAT_RXOVR) != 0)
    {
      ndbg("Rx OVR rxnext=%p {%06x, %u, %04x} rrp=%02x%02x rwp=%02%02 blkslft=%02x istat=%02x\n",
           priv->rxnext, priv->rxnext->np, priv->rxnext->pktsize, priv->rxnext->stat,
           inp(EZ80_EMAC_RRP_H), inp(EZ80_EMAC_RRP_L),
           inp(EZ80_EMAC_RWP_H), inp(EZ80_EMAC_RWP_L),
           inp(EZ80_EMAC_BLKSLFT_H), inp(EZ80_EMAC_BLKSLFT_L),
           istat);

      /* Increment statistics */

      EMAC_STAT(priv, rx_errors);
      EMAC_STAT(priv, rx_ovrerrors);
    }
  return OK;
}

/****************************************************************************
 * Function: ez80emac_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 ez80emac_txtimeout(int argc, uint32 arg, ...)
{
  FAR struct ez80emac_driver_s *priv = (FAR struct ez80emac_driver_s *)arg;
  irqstate_t flags;

  /* Increment statistics and dump debug info */

  EMAC_STAT(priv, tx_errors);
  EMAC_STAT(priv, tx_timeouts);

  /* Then reset the hardware */

  flags = irqsave();
  ez80emac_ifdown(&priv->dev);
  ez80emac_ifup(&priv->dev);
  irqrestore(flags);

  /* Then poll uIP for new XMIT data */

  (void)uip_poll(&priv->dev, ez80emac_uiptxpoll);
}

/****************************************************************************
 * Function: ez80emac_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 ez80emac_polltimer(int argc, uint32 arg, ...)
{
  struct ez80emac_driver_s *priv = (struct ez80emac_driver_s *)arg;

  /* Check if there is room in the send another TXr packet.  */

  /* If so, update TCP timing states and poll uIP for new XMIT data */

  (void)uip_timer(&priv->dev, ez80emac_uiptxpoll, EMAC_POLLHSEC);

  /* Setup the watchdog poll timer again */

  (void)wd_start(priv->txpoll, EMAC_WDDELAY, ez80emac_polltimer, 1, arg);
}

/****************************************************************************
 * Function: ez80emac_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 ez80emac_ifup(FAR struct uip_driver_s *dev)
{
  FAR struct ez80emac_driver_s *priv = (FAR struct ez80emac_driver_s *)dev->d_private;
  ubyte regval;

  ndbg("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 );

  /* Bring up the interface -- Must be down right now */

  DEBUGASSERT((inp(EZ80_EMAC_PTMR) == 0));
  DEBUGASSERT((inp(EZ80_EMAC_CFG4) & EMAC_CFG4_RXEN) == 0);

  /* Soft reset Rx/Tx */

  regval  = inp(EZ80_EMAC_RST);
  regval |= (EMAC_RST_HRRFN|EMAC_RST_HRTFN);
  outp(EZ80_EMAC_RST, regval);
  regval &= ~(EMAC_RST_HRRFN|EMAC_RST_HRTFN);
  outp(EZ80_EMAC_RST, regval);

  /* EMAC_AFR_BC - Accept broadcast messages
   * EMAC_AFR_MC - Accept any multicast message
   * EMAC_AFR_QMC - Accept only qualified multicast messages
   */

#ifdef CONFIG_EZ80_MCFILTER
  outp(EZ80_EMAC_AFR, EMAC_AFR_BC|EMAC_AFR_QMC|EMAC_AFR_MC);
#else
  outp(EZ80_EMAC_AFR, EMAC_AFR_BC|EMAC_AFR_MC);
#endif

  /* Set the MAC address */

  outp(EZ80_EMAC_STAD_0, priv->dev.d_mac.ether_addr_octet[0]);
  outp(EZ80_EMAC_STAD_1, priv->dev.d_mac.ether_addr_octet[1]);
  outp(EZ80_EMAC_STAD_2, priv->dev.d_mac.ether_addr_octet[2]);
  outp(EZ80_EMAC_STAD_3, priv->dev.d_mac.ether_addr_octet[3]);
  outp(EZ80_EMAC_STAD_4, priv->dev.d_mac.ether_addr_octet[4]);
  outp(EZ80_EMAC_STAD_5, priv->dev.d_mac.ether_addr_octet[5]);

  /* Enable/disable promiscuous mode */

  regval = inp(EZ80_EMAC_AFR);
#if defined(CONFIG_EZ80_EMACPROMISC)
  regval |= EMAC_AFR_PROM;
#else
  regval &= ~EMAC_AFR_PROM;
#endif
  outp(EZ80_EMAC_AFR, regval);

  /* Enable Rx */

  regval = inp(EZ80_EMAC_CFG4);
  regval |= EMAC_CFG4_RXEN;
  outp(EZ80_EMAC_CFG4, regval);

  /* Enable the Tx poll timer */

  outp(EZ80_EMAC_PTMR, EMAC_PTMR);

  /* Turn on interrupts */

  outp(EZ80_EMAC_ISTAT, 0xff);           /* Clear all pending interrupts */
  outp(EZ80_EMAC_IEN, EMAC_EIN_HANDLED); /* Enable all interrupts */

  /* Set and activate a timer process */

  (void)wd_start(priv->txpoll, EMAC_WDDELAY, ez80emac_polltimer, 1, (uint32)priv);

  /* Enable the Ethernet interrupt */

  priv->bifup = TRUE;
  up_enable_irq(EZ80_EMACRX_IRQ);
  up_enable_irq(EZ80_EMACTX_IRQ);
  up_enable_irq(EZ80_EMACSYS_IRQ);
  return OK;
}

/****************************************************************************
 * Function: ez80emac_ifdown
 *
 * Description:
 *   NuttX Callback: Stop the interface.
 *
 * Parameters:
 *   dev  - Reference to the NuttX driver state structure
 *
 * Returned Value:
 *   None
 *
 * Assumptions:
 *
 ****************************************************************************/

static int ez80emac_ifdown(struct uip_driver_s *dev)
{
  struct ez80emac_driver_s *priv = (struct ez80emac_driver_s *)dev->d_private;
  irqstate_t flags;
  ubyte regval;

  /* Disable the Ethernet interrupt */

  flags = irqsave();
  up_disable_irq(EZ80_EMACRX_IRQ);
  up_disable_irq(EZ80_EMACTX_IRQ);
  up_disable_irq(EZ80_EMACSYS_IRQ);

  /* Cancel the TX poll timer and TX timeout timers */

  wd_cancel(priv->txpoll);
  wd_cancel(priv->txtimeout);

  /* Disable Rx */

  regval = inp(EZ80_EMAC_CFG4);
  regval &= ~EMAC_CFG4_RXEN;
  outp(EZ80_EMAC_CFG4, regval);

  /* Disable the Tx poll timer */

  outp(EZ80_EMAC_PTMR, 0);

  priv->bifup = FALSE;
  irqrestore(flags);
  return OK;
}

/****************************************************************************
 * Function: ez80emac_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 ez80emac_txavail(struct uip_driver_s *dev)
{
  struct ez80emac_driver_s *priv = (struct ez80emac_driver_s *)dev->d_private;
  irqstate_t flags;

  flags = irqsave();

  /* Ignore the notification if the interface is not yet up */

  if (priv->bifup)
    {

      /* Check if there is room in the hardware to hold another outgoing packet. */

      /* If so, then poll uIP for new XMIT data */

      (void)uip_poll(&priv->dev, ez80emac_uiptxpoll);
    }

  irqrestore(flags);
  return OK;
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Function: ez80emac_initialize
 *
 * Description:
 *   Initialize the Ethernet driver
 *
 * Parameters:
 *   None
 *
 * Returned Value:
 *   OK on success; Negated errno on failure.
 *
 ****************************************************************************/

int up_netinitialize(void)
{
  struct ez80emac_driver_s *priv = &g_emac;
  uint24 addr;
  ubyte regval;
  int ret;

  /* Initialize the hardware
   *
   * The ez80 has a fixed 8kb of EMAC SRAM memory located in the high
   * address space (we don't need to know where, we can get then from
   * the EZ80_EMAC_BP_U register). The EMAC memory is broken into
   * two parts: the Tx buffer and the Rx buffer.
   *
   * The TX buffer lies at the beginning of the EMAC memory.
   * The Transmit Lower Boundary Pointer Register, TLBP, holds the
   * least significant 12-bits of the starting address of the Tx buffer.
   */

  outp(EZ80_EMAC_TLBP_L, 0); /* Bits 0-7 set to zero */
  outp(EZ80_EMAC_TLBP_H, 0); /* Bits 8-12 set to zero */

  addr                  = (uint24)inp(EZ80_EMAC_BP_U) << 16 |
                          (uint24)inp(EZ80_EMAC_TLBP_H) << 8 |
                          (uint24)inp(EZ80_EMAC_TLBP_L);

  priv->txstart         = (FAR struct ez80emac_desc_s *)(addr);
  priv->txnext          = priv->txstart;
  priv->txhead          = NULL;
  priv->txtail          = NULL;

  priv->txnext->np      = 0;
  priv->txnext->pktsize = 0;
  priv->txnext->stat    = 0;

  nvdbg("txnext=%p {%06x, %u, %04x} trp=%02x%02x\n",
        priv->txnext, priv->txnext->np, priv->txnext->pktsize, priv->txnext->stat,
        inp(EZ80_EMAC_TRP_H), inp(EZ80_EMAC_TRP_L));

  /* The Boundary Pointer Register, EMAC_BP, points to the start of the Rx
   * buffer (end of Tx buffer + 1).  Only bits EMAC_BP[12:5] of are
   * write-able.
   */

  outp(EZ80_EMAC_BP_L, EMAC_TXBUFSIZE & 0xe0);
  outp(EZ80_EMAC_BP_H, (EMAC_TXBUFSIZE >> 8) & 0x1f);

  addr                  = (uint24)inp(EZ80_EMAC_BP_U) << 16 |
                          (uint24)inp(EZ80_EMAC_BP_H) << 8 |
                          (uint24)inp(EZ80_EMAC_BP_L);
  priv->rxstart         = (FAR struct ez80emac_desc_s *)(addr);
  priv->rxnext          = priv->rxstart;

  priv->rxnext->np      = 0;
  priv->rxnext->pktsize = 0;
  priv->rxnext->stat    = 0;

  nvdbg("rxnext=%p {%06x, %u, %04x} bp=%06x\n",
        priv->rxnext, priv->rxnext->np, priv->rxnext->pktsize,
        priv->rxnext->stat, addr);

  /* The EMAC Receive Read Pointer (RRP) register(s) should be initialized
   * to the EMAC_BEZ80_EMAC_RRP_HP value (start of the Receive buffer). The RRP register
   * points to where the next Receive packet is read from. The EMAC_BP[12:5]
   * is loaded into this register whenever the EMAC_RST [(HRRFN) is set to 1.
   * The RxDMA block uses the RRP[12:5] to compare to RWP[12:5] for determining
   * how many buffers remain. The result equates to the BLKSLFT register.
   *
   * The read-only EMAC Receive Write Pointer (RWP) egisters reports the
   * current RxDMA Receive Write pointer. This pointer gets initialized to EMAC_BP
   * whenever EMAC_RST bits SRST or HRRTN are set. Because the size of the packet
   * is limited to a minimum of 32 bytes, the last five bits are always zero.
   */

  outp(EZ80_EMAC_RRP_H, inp(EZ80_EMAC_BP_H));
  outp(EZ80_EMAC_RRP_L, inp(EZ80_EMAC_BP_L));

  nvdbg("rrp=%02x%02x rwp=%02%02\n",
        inp(EZ80_EMAC_RRP_H), inp(EZ80_EMAC_RRP_L),
        inp(EZ80_EMAC_RWP_H), inp(EZ80_EMAC_RWP_L));

  /* The Receive High Boundary Pointer Register, EMAC_RHBP, points to the end
   * of the Rx buffer + 1.  Only bits EMAC_RHBP[12:5] are write-able.
   */

  outp(EZ80_EMAC_RHBP_L, EMAC_TOTAL_BUFSIZE & 0xe0);
  outp(EZ80_EMAC_RHBP_H, (EMAC_TOTAL_BUFSIZE >> 8) & 0x1f);

  addr          = (uint24)inp(EZ80_EMAC_BP_U) << 16 |
                  (uint24)inp(EZ80_EMAC_RHBP_H) << 8 |
                  (uint24)inp(EZ80_EMAC_RHBP_L);
  priv->rxendp1 = (FAR struct ez80emac_desc_s *)addr;

  nvdbg("rxendp1=%p bp=%06x\n", priv->rxendp1, addr);

  /* The Tx and Receive buffers are divided into packet buffers of either
   * 256, 128, 64, or 32 bytes selected by BufSize register bits 7 and 6.
   */

  outp(EZ80_EMAC_BUFSZ, EMAC_BUFSZ);
  nvdbg("bufsz=%02x blksleft=%02x%02x\n",
        inp(EZ80_EMAC_BUFSZ), inp(EZ80_EMAC_BLKSLFT_H), inp(EZ80_EMAC_BLKSLFT_L));

  /* Software reset */

  outp(EZ80_EMAC_ISTAT, 0xff); /* Clear any pending interupts */
  regval = inp(EZ80_EMAC_RST);
  regval |= EMAC_RST_SRST;
  outp(EZ80_EMAC_RST, regval);
  regval &= ~EMAC_RST_SRST;
  outp(EZ80_EMAC_RST, regval);

  nvdbg("After soft reset: rwp=%02%02 trp=%02x%02x\n",
        inp(EZ80_EMAC_RWP_H), inp(EZ80_EMAC_RWP_L),
        inp(EZ80_EMAC_TRP_H), inp(EZ80_EMAC_TRP_L));

  /* PHY reset */

  up_udelay(500);
  ez80emac_miiwrite(priv, MII_MCR, MII_MCR_RESET);
  if (!ez80emac_miipoll(priv, MII_MCR, MII_MCR_RESET, FALSE))
    {
      ndbg("PHY reset error.\n");
    }

  /*  Initialize MAC */

  /* Set only the default late collision bytes in CFG2 */

  outp(EZ80_EMAC_CFG3, EMAC_LCOL);

  /* Set only the retry count in CFG3 */

  outp(EZ80_EMAC_CFG3, EMAC_RETRY);

  /* EMAC_CFG4_TXFC - Pause control frames are allowed to be transmitted
   * EMAC_CFG4_RXFC - Act on received pause control frames 
   */

  outp(EZ80_EMAC_CFG4, EMAC_CFG4_TXFC|EMAC_CFG4_RXFC);

  /* EMAC_CFG1_CRCEN + EMAC_CFG1_PADEN + EMAC_CFG1_VLPAD + EMAC_CFG1_ADPADN =
   *   if VLAN not detected, pad to 60, add CRC
   *   if VLAN detected, pad to 64, add CRC
   */

  outp(EZ80_EMAC_CFG1, EMAC_CFG1_CRCEN|EMAC_CFG1_PADEN|EMAC_CFG1_ADPADN|EMAC_CFG1_VLPAD);

  outp(EZ80_EMAC_IPGT, EMAC_IPGT);
  outp(EZ80_EMAC_IPGR1, EMAC_IPGR1);
  outp(EZ80_EMAC_IPGR2, EMAC_IPGR2);

  outp(EZ80_EMAC_MAXF_L, EMAC_MAXF & 0xff);
  outp(EZ80_EMAC_MAXF_H, EMAC_MAXF >> 8);

  /* Select the fastest MDC clock divider.  The MDC clock derives
   * from the SCLK divided by 4, 6, 8, 10, 14, 20, or 28.
   */

  outp(EZ80_EMAC_MIIMGT, CONFIG_EZ80_MDCDIV);

  /* Clear the new hash table */

  outp(EZ80_EMAC_HTBL_0, 0);
  outp(EZ80_EMAC_HTBL_1, 0);
  outp(EZ80_EMAC_HTBL_2, 0);
  outp(EZ80_EMAC_HTBL_3, 0);
  outp(EZ80_EMAC_HTBL_4, 0);
  outp(EZ80_EMAC_HTBL_5, 0);
  outp(EZ80_EMAC_HTBL_6, 0);
  outp(EZ80_EMAC_HTBL_7, 0);

  /* PHY reset */

  ez80emac_miiwrite(priv, MII_MCR, MII_MCR_RESET);
  if (!ez80emac_miipoll(priv, MII_MCR, MII_MCR_RESET, FALSE))
    {
      ndbg("PHY reset error.\n");
      ret = -EIO;
      goto errout;
    }

  /* Set auto-negotion */

  ez80emac_miiautonegotiate(priv);

  /* Initialize DMA / FIFO */

  outp(EZ80_EMAC_TPTV_L, EMAC_TPTV & 0xff);
  outp(EZ80_EMAC_TPTV_H, EMAC_TPTV >> 8);

  /* Disable all interrupts */

  outp(EZ80_EMAC_IEN, 0);

  /* Attach IRQs */

  ret = irq_attach(EZ80_EMACSYS_IRQ, ez80emac_sysinterrupt);
  if (ret < 0)
    {
      ndbg("Unable to attach IRQ %d\n", EZ80_EMACSYS_IRQ);
      ret = -EAGAIN;
      goto errout;
    }

  ret = irq_attach(EZ80_EMACRX_IRQ, ez80emac_rxinterrupt);
  if (ret < 0)
    {
      ndbg("Unable to attach IRQ %d\n", EZ80_EMACRX_IRQ);
      ret = -EAGAIN;
      goto errout;
    }

  ret = irq_attach(EZ80_EMACTX_IRQ, ez80emac_txinterrupt);
  if (ret < 0)
    {
      ndbg("Unable to attach IRQ %d\n", EZ80_EMACTX_IRQ);
      ret = -EAGAIN;
      goto errout;
    }

  /* Initialize the driver structure */

  memset(&g_emac, 0, sizeof(struct ez80emac_driver_s));
  priv->dev.d_ifup    = ez80emac_ifup;      /* I/F down callback */
  priv->dev.d_ifdown  = ez80emac_ifdown;    /* I/F up (new IP address) callback */
  priv->dev.d_txavail = ez80emac_txavail;   /* New TX data callback */
  priv->dev.d_private = (FAR void*)&g_emac; /* Used to recover private state from dev */

  /* Create a watchdog for timing polling for and timing of transmisstions */

  priv->txpoll        = wd_create();        /* Create periodic poll timer */
  priv->txtimeout     = wd_create();        /* Create TX timeout timer */

  /* Read the MAC address from the hardware into priv->dev.d_mac.ether_addr_octet */

  /* Register the device with the OS so that socket IOCTLs can be performed */

  (void)netdev_register(&priv->dev);
  return OK;

errout:
  up_netuninitialize();
  return ERROR;
}

/****************************************************************************
 * Function: up_multicastfilter
 *
 * Description:
 *   Add one MAC address to the multi-cast hash table
 *
 * Parameters:
 *   dev    - Reference to the uIP driver state structure
 *   mac    - The MAC address to add
 *   enable - TRUE: Enable filtering on this address; FALSE: disable
 *
 * Returned Value:
 *   OK on success; Negated errno on failure.
 *
 ****************************************************************************/

#ifdef CONFIG_ARCH_MCFILTER
int up_multicastfilter(FAR struct uip_driver_s *dev, FAR ubyte *mac, boolean enable)
{
  FAR struct ez80emac_driver_s *priv = (FAR struct ez80emac_driver_s *)dev->priv;
  ubyte regval;
  int ndx;
  int bit;
  int i;

  /* The EMAC Hash Table Registers represent an 8x8 hash table matrix.
   * This table is used as an option to select between different multi-cast
   * addresses.  If a multicast address is received, the first 6 bits of the CRC
   * decoded and to a table that points to a single bit in the hash table matrix.
   * if the selected bit is '1', then multicast packet is accepted.  If the bit
   * is '0', the multicast packet is rejected.
   */

  /* Apply the hash algorithm to the the hash table index and bit number
   * corresponding to this MAC
   */

  ez80emac_machash(mclist->dmi_addr, &ndx, &bit);

  /* And set/clear that bit in that array element */

  regval = inp(EZ80_EMAC_HTBL_0 + ndx);
  if (enable)
    {
       regval |= (1 << bit);
    }
  else
    {
       regval &= ~(1 << bit);
    }
  outp(EZ80_EMAC_HTBL_0 + ndx, regval);
  return OK;
}
#endif

/****************************************************************************
 * Function: up_netuninitialize
 *
 * Description:
 *   Un-initialize the Ethernet driver
 *
 * Parameters:
 *   None
 *
 * Returned Value:
 *   None
 *
 * Assumptions:
 *
 ****************************************************************************/

void up_netuninitialize(void)
{
  FAR struct ez80emac_driver_s *priv = &g_emac;
  int i;

  ez80emac_ifdown(&priv->dev);
  /* netdev_unregister(priv->dev); No such API yet */

  priv->txnext = priv->txstart;
  priv->txhead = NULL;
  priv->txtail = NULL;

  irq_detach(EZ80_EMACRX_IRQ);
  irq_detach(EZ80_EMACTX_IRQ);
  irq_detach(EZ80_EMACSYS_IRQ);
}

#endif /* CONFIG_NET && CONFIG_EZ80_EMAC */