Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys Add a driver for the Myson Technology MTD803 3-in-1 Fast...



details:   https://anonhg.NetBSD.org/src/rev/d562ffd58019
branches:  trunk
changeset: 539088:d562ffd58019
user:      martin <martin%NetBSD.org@localhost>
date:      Thu Nov 07 21:56:56 2002 +0000

description:
Add a driver for the Myson Technology MTD803 3-in-1 Fast Ethernet Controller,
provided by Peter Bex in PR 18675.

diffstat:

 sys/arch/i386/conf/GENERIC |    5 +-
 sys/conf/files             |    6 +-
 sys/dev/DEVNAMES           |    3 +-
 sys/dev/ic/mtd803.c        |  997 +++++++++++++++++++++++++++++++++++++++++++++
 sys/dev/ic/mtd803reg.h     |  274 ++++++++++++
 sys/dev/ic/mtd803var.h     |  141 ++++++
 sys/dev/pci/files.pci      |    6 +-
 sys/dev/pci/if_mtd_pci.c   |  154 ++++++
 8 files changed, 1581 insertions(+), 5 deletions(-)

diffs (truncated from 1665 to 300 lines):

diff -r b1e17e2fe6ce -r d562ffd58019 sys/arch/i386/conf/GENERIC
--- a/sys/arch/i386/conf/GENERIC        Thu Nov 07 21:06:04 2002 +0000
+++ b/sys/arch/i386/conf/GENERIC        Thu Nov 07 21:56:56 2002 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: GENERIC,v 1.521 2002/10/27 10:28:39 tsutsui Exp $
+# $NetBSD: GENERIC,v 1.522 2002/11/07 21:56:56 martin Exp $
 #
 # GENERIC machine description file
 # 
@@ -22,7 +22,7 @@
 
 options        INCLUDE_CONFIG_FILE     # embed config file in kernel binary
 
-#ident                 "GENERIC-$Revision: 1.521 $"
+#ident                 "GENERIC-$Revision: 1.522 $"
 
 maxusers       32              # estimated number of users
 
@@ -623,6 +623,7 @@
 #hme*  at pci? dev ? function ?        # Sun Microelectronics STP2002-STQ
 le*    at pci? dev ? function ?        # PCnet-PCI Ethernet
 lmc*   at pci? dev ? function ?        # Lan Media Corp SSI/HSSI/DS3
+mtd*   at pci? dev ? function ?        # Myson MTD803 3-in-1 Ethernet
 ne*    at pci? dev ? function ?        # NE2000-compatible Ethernet
 ntwoc* at pci? dev ? function ?        # Riscom/N2 PCI Sync Serial
 pcn*   at pci? dev ? function ?        # AMD PCnet-PCI Ethernet
diff -r b1e17e2fe6ce -r d562ffd58019 sys/conf/files
--- a/sys/conf/files    Thu Nov 07 21:06:04 2002 +0000
+++ b/sys/conf/files    Thu Nov 07 21:56:56 2002 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files,v 1.578 2002/11/05 13:22:33 mrg Exp $
+#      $NetBSD: files,v 1.579 2002/11/07 21:56:57 martin Exp $
 
 #      @(#)files.newconf       7.5 (Berkeley) 5/10/93
 
@@ -823,6 +823,10 @@
 device slhci: usbus
 file   dev/ic/sl811hs.c                slhci                   needs-flag
 
+# Myson MTD803 3-in-1 Fast Ethernet Controller
+device mtd: arp, ether, ifnet, mii
+file   dev/ic/mtd803.c                 mtd
+
 # radio devices, attaches to radio hardware driver
 device radio
 attach radio at radiodev
diff -r b1e17e2fe6ce -r d562ffd58019 sys/dev/DEVNAMES
--- a/sys/dev/DEVNAMES  Thu Nov 07 21:06:04 2002 +0000
+++ b/sys/dev/DEVNAMES  Thu Nov 07 21:56:56 2002 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: DEVNAMES,v 1.123 2002/10/29 12:31:22 blymn Exp $
+#      $NetBSD: DEVNAMES,v 1.124 2002/11/07 21:56:58 martin Exp $
 #
 # This file contains all used device names and defined attributes in 
 # alphabetical order. New devices added to the system somewhere should first 
@@ -728,6 +728,7 @@
 mt                     MI              
 mt                     hp300
 mtc                    MI              
+mtd                    MI
 mtty                   MI              
 mu                     vax
 mulaw                  MI              Attribute
diff -r b1e17e2fe6ce -r d562ffd58019 sys/dev/ic/mtd803.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/ic/mtd803.c       Thu Nov 07 21:56:56 2002 +0000
@@ -0,0 +1,997 @@
+/* $NetBSD: mtd803.c,v 1.1 2002/11/07 21:56:59 martin Exp $ */
+
+/*-
+ *
+ * Copyright (c) 2002 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Peter Bex <Peter.Bex%student.kun.nl@localhost>.
+ *
+ * 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. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *      This product includes software developed by the NetBSD
+ *      Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+ */
+
+/*
+ * TODO:
+ * - Most importantly, get some bus_dmamap_syncs in the correct places.
+ *    I don't have access to a computer with PCI other than i386, and i386
+ *    is just such a machine where dmamap_syncs don't do anything.
+ * - Powerhook for when resuming after standby.
+ * - Watchdog stuff doesn't work yet, the system crashes.(lockmgr: no context)
+ * - There seems to be a CardBus version of the card. (see datasheet)
+ *    Perhaps a detach function is necessary then? (free buffs, stop rx/tx etc)
+ * - When you enable the TXBUN (Tx buffer unavailable) interrupt, it gets
+ *    raised every time a packet is sent. Strange, since everything works anyway
+ */
+
+#include "bpfilter.h"
+
+#include <sys/param.h>
+#include <sys/mbuf.h>
+#include <sys/systm.h>
+#include <sys/device.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <sys/syslog.h>
+
+#include <net/if.h>
+#include <net/if_ether.h>
+#include <net/if_media.h>
+
+#ifdef INET
+#include <netinet/in.h>
+#include <netinet/if_inarp.h>
+#include <netinet/in_systm.h>
+#include <netinet/in_var.h>
+#include <netinet/ip.h>
+#endif
+
+#if NBPFILTER > 0
+#include <net/bpf.h>
+#include <net/bpfdesc.h>
+#endif
+
+#include <machine/bus.h>
+
+#include <dev/ic/mtd803reg.h>
+#include <dev/ic/mtd803var.h>
+#include <dev/mii/mii.h>
+#include <dev/mii/miivar.h>
+
+/*
+ * Device driver for the MTD803 3-in-1 Fast Ethernet Controller
+ * Written by Peter Bex (peter.bex%student.kun.nl@localhost)
+ *
+ * Datasheet at:   http://www.myson.com.tw   or   http://www.century-semi.com
+ */
+
+#define MTD_READ_1(sc, reg) \
+       bus_space_read_1((sc)->bus_tag, (sc)->bus_handle, (reg))
+#define MTD_WRITE_1(sc, reg, data) \
+       bus_space_write_1((sc)->bus_tag, (sc)->bus_handle, (reg), (data))
+
+#define MTD_READ_2(sc, reg) \
+       bus_space_read_2((sc)->bus_tag, (sc)->bus_handle, (reg))
+#define MTD_WRITE_2(sc, reg, data) \
+       bus_space_write_2((sc)->bus_tag, (sc)->bus_handle, (reg), (data))
+
+#define MTD_READ_4(sc, reg) \
+       bus_space_read_4((sc)->bus_tag, (sc)->bus_handle, (reg))
+#define MTD_WRITE_4(sc, reg, data) \
+       bus_space_write_4((sc)->bus_tag, (sc)->bus_handle, (reg), (data))
+
+#define MTD_SETBIT(sc, reg, x) \
+       MTD_WRITE_4((sc), (reg), MTD_READ_4((sc), (reg)) | (x))
+#define MTD_CLRBIT(sc, reg, x) \
+       MTD_WRITE_4((sc), (reg), MTD_READ_4((sc), (reg)) & ~(x))
+
+#define ETHER_CRC32(buf, len)  (ether_crc32_be((buf), (len)))
+
+int mtd_mii_readreg __P((struct device *, int, int));
+void mtd_mii_writereg __P((struct device *, int, int, int));
+void mtd_mii_statchg __P((struct device *));
+
+void mtd_start __P((struct ifnet *));
+void mtd_stop __P((struct ifnet *, int));
+int mtd_ioctl __P((struct ifnet *, u_long, caddr_t));
+void mtd_setmulti __P((struct mtd_softc *));
+void mtd_watchdog __P((struct ifnet *));
+int mtd_mediachange __P((struct ifnet *));
+void mtd_mediastatus __P((struct ifnet *, struct ifmediareq *));
+
+int mtd_init __P((struct ifnet *));
+void mtd_reset __P((struct mtd_softc *));
+void mtd_shutdown __P((void *));
+int mtd_init_desc __P((struct mtd_softc *));
+int mtd_put __P((struct mtd_softc *, int, struct mbuf *));
+struct mbuf *mtd_get __P((struct mtd_softc *, int, int));
+
+int mtd_rxirq __P((struct mtd_softc *));
+int mtd_txirq __P((struct mtd_softc *));
+int mtd_bufirq __P((struct mtd_softc *));
+
+
+int
+mtd_config(sc)
+       struct mtd_softc *sc;
+{
+       struct ifnet *ifp = &sc->ethercom.ec_if;
+       int i;
+
+       /* Read station address */
+       for (i = 0; i < ETHER_ADDR_LEN; ++i)
+               sc->eaddr[i] = MTD_READ_1(sc, MTD_PAR0 + i);
+
+       /* Initialize ifnet structure */
+       memcpy(ifp->if_xname, sc->dev.dv_xname, IFNAMSIZ);
+       ifp->if_softc = sc;
+       ifp->if_init = mtd_init;
+       ifp->if_start = mtd_start;
+       ifp->if_stop = mtd_stop;
+       ifp->if_ioctl = mtd_ioctl;
+       ifp->if_watchdog = mtd_watchdog;
+       ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
+       IFQ_SET_READY(&ifp->if_snd);
+
+       /* Setup MII interface */
+       sc->mii.mii_ifp = ifp;
+       sc->mii.mii_readreg = mtd_mii_readreg;
+       sc->mii.mii_writereg = mtd_mii_writereg;
+       sc->mii.mii_statchg = mtd_mii_statchg;
+
+       ifmedia_init(&sc->mii.mii_media, 0, mtd_mediachange, mtd_mediastatus);
+
+       mii_attach(&sc->dev, &sc->mii, 0xffffffff, MII_PHY_ANY, 0, 0);
+
+       if (LIST_FIRST(&sc->mii.mii_phys) == NULL) {
+               printf("%s: Unable to configure MII\n", sc->dev.dv_xname);
+               return 1;
+       } else {
+               ifmedia_set(&sc->mii.mii_media, IFM_ETHER | IFM_AUTO);
+       }
+
+       if (mtd_init_desc(sc))
+               return 1;
+
+       /* Attach interface */
+       if_attach(ifp);
+       ether_ifattach(ifp, sc->eaddr);
+
+#if NRND > 0
+       /* Initialise random source */
+       rnd_attach_source(&sc->rnd_src, sc->dev.dv_xname, RND_TYPE_NET, 0);
+#endif
+
+       /* Add shutdown hook to reset card when we reboot */
+       sc->sd_hook = shutdownhook_establish(mtd_shutdown, sc);
+
+       return 0;
+}
+
+
+/*
+ * mtd_init
+ * Must be called at splnet()
+ */
+int
+mtd_init(ifp)
+       struct ifnet *ifp;
+{
+       struct mtd_softc *sc = ifp->if_softc;
+
+       mtd_reset(sc);
+
+       /*
+        * Set cache alignment and burst length. Don't really know what these
+        * mean, so their values are probably suboptimal.
+        */
+       MTD_WRITE_4(sc, MTD_BCR, MTD_BCR_BLEN16);
+
+       MTD_WRITE_4(sc, MTD_RXTXR, MTD_TX_STFWD | MTD_RX_BLEN | MTD_RX_512
+                       | MTD_TX_FDPLX);
+
+       /* Promiscuous mode? */
+       if (ifp->if_flags & IFF_PROMISC)
+               MTD_SETBIT(sc, MTD_RXTXR, MTD_RX_PROM);
+       else
+               MTD_CLRBIT(sc, MTD_RXTXR, MTD_RX_PROM);
+
+       /* Broadcast mode? */
+       if (ifp->if_flags & IFF_BROADCAST)
+               MTD_SETBIT(sc, MTD_RXTXR, MTD_RX_ABROAD);
+       else
+               MTD_CLRBIT(sc, MTD_RXTXR, MTD_RX_ABROAD);
+
+       mtd_setmulti(sc);
+
+       /* Enable interrupts */



Home | Main Index | Thread Index | Old Index