Home | History | Annotate | Line # | Download | only in nubus
if_netdock_nubus.c revision 1.5
      1 /*	$NetBSD: if_netdock_nubus.c,v 1.5 2004/10/30 18:08:34 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 2000,2002 Daishi Kato <daishi (at) axlight.com>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Daishi Kato
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Asante NetDock (for Duo series) driver
     35  * the chip inside is not known
     36  */
     37 
     38 /*
     39  * The author would like to thank Takeo Kuwata <tkuwata (at) mac.com> for
     40  * his help in stabilizing this driver.
     41  */
     42 
     43 /***********************/
     44 
     45 #include <sys/cdefs.h>
     46 __KERNEL_RCSID(0, "$NetBSD: if_netdock_nubus.c,v 1.5 2004/10/30 18:08:34 thorpej Exp $");
     47 
     48 #include <sys/param.h>
     49 #include <sys/device.h>
     50 #include <sys/socket.h>
     51 #include <sys/systm.h>
     52 #include <sys/mbuf.h>
     53 #include <sys/ioctl.h>
     54 
     55 #include <net/if.h>
     56 #include <net/if_dl.h>
     57 #include <net/if_ether.h>
     58 
     59 #include "opt_inet.h"
     60 #ifdef INET
     61 #include <netinet/in.h>
     62 #include <netinet/if_inarp.h>
     63 #endif
     64 
     65 #include "bpfilter.h"
     66 #if NBPFILTER > 0
     67 #include <net/bpf.h>
     68 #endif
     69 
     70 #include <machine/bus.h>
     71 #include <machine/viareg.h>
     72 #include <mac68k/nubus/nubus.h>
     73 
     74 /***********************/
     75 
     76 #define NETDOCK_DEBUG
     77 
     78 #define NETDOCK_NUBUS_CATEGORY	0x0020
     79 #define NETDOCK_NUBUS_TYPE	0x0003
     80 #define NETDOCK_NUBUS_DRSW	0x0103
     81 #define NETDOCK_NUBUS_DRHW	0x0100
     82 
     83 #define ETHERMICRODOCK_NUBUS_CATEGORY	0x0020
     84 #define ETHERMICRODOCK_NUBUS_TYPE	0x0003
     85 #define ETHERMICRODOCK_NUBUS_DRSW	0x0102
     86 #define ETHERMICRODOCK_NUBUS_DRHW	0x0100
     87 
     88 #define REG_ISR		0x000c
     89 #define REG_000E	0x000e
     90 #define REG_0000	0x0000
     91 #define REG_0002	0x0002
     92 #define REG_0004	0x0004
     93 #define REG_0006	0x0006
     94 #define REG_DATA	0x0008
     95 #define REG_EFD00	0xefd00
     96 
     97 #define ISR_ALL		0x3300
     98 #define ISR_TX		0x0200
     99 #define ISR_RX		0x0100
    100 #define ISR_READY	0x0800
    101 #define ISR_BIT_0C	0x1000
    102 #define ISR_BIT_0D	0x2000
    103 #define ISR_MASK	0x0033
    104 #define ISR_BIT_03	0x0008
    105 
    106 #define REG_0002_BIT_04	0x0010
    107 #define REG_0000_BIT_08	0x0100
    108 #define REG_0004_BIT_0F	0x8000
    109 #define REG_0004_BIT_07 0x0080
    110 #define REG_DATA_BIT_02 0x0004
    111 #define REG_DATA_BIT_03 0x0008
    112 #define REG_DATA_BIT_04 0x0010
    113 #define REG_DATA_BIT_05 0x0020
    114 #define REG_DATA_BIT_08	0x0100
    115 #define REG_DATA_BIT_07	0x0080
    116 #define REG_DATA_BIT_0F	0x8000
    117 
    118 /***********************/
    119 
    120 typedef struct netdock_softc {
    121 	struct device	sc_dev;
    122 	struct ethercom	sc_ethercom;
    123 #define sc_if		sc_ethercom.ec_if
    124 
    125 	bus_space_tag_t		sc_regt;
    126 	bus_space_handle_t	sc_regh;
    127 
    128 	u_int8_t	sc_enaddr[ETHER_ADDR_LEN];
    129 
    130 } netdock_softc_t;
    131 
    132 /***********************/
    133 
    134 static int	netdock_nubus_match __P((struct device *, struct cfdata *,
    135 			void*));
    136 static void	netdock_nubus_attach __P((struct device *, struct device *,
    137 			void *));
    138 static int	netdock_nb_get_enaddr __P((bus_space_tag_t, bus_space_handle_t,
    139 			struct nubus_attach_args *, u_int8_t  *));
    140 #ifdef NETDOCK_DEBUG_DRIVER
    141 static void	netdock_print_driver __P((bus_space_tag_t, bus_space_handle_t,
    142 			struct nubus_attach_args *));
    143 #endif
    144 
    145 int	netdock_setup __P((struct netdock_softc *, u_int8_t *));
    146 void	netdock_intr __P((void *));
    147 
    148 static void	netdock_watchdog __P((struct ifnet *));
    149 static int	netdock_init __P((struct netdock_softc *));
    150 static int	netdock_stop __P((struct netdock_softc *));
    151 static int	netdock_ioctl __P((struct ifnet *, u_long, caddr_t));
    152 static void	netdock_start __P((struct ifnet *));
    153 static void	netdock_reset __P((struct netdock_softc *));
    154 static void	netdock_txint __P((struct netdock_softc *));
    155 static void	netdock_rxint __P((struct netdock_softc *));
    156 
    157 static u_int	netdock_put __P((struct netdock_softc *, struct mbuf *));
    158 static int	netdock_read __P((struct netdock_softc *, int));
    159 static struct mbuf *netdock_get __P((struct netdock_softc *, int));
    160 
    161 /***********************/
    162 
    163 #define NIC_GET_1(sc, o)	(bus_space_read_1((sc)->sc_regt,	\
    164 					(sc)->sc_regh, (o)))
    165 #define NIC_PUT_1(sc, o, val)	(bus_space_write_1((sc)->sc_regt,	\
    166 					(sc)->sc_regh, (o), (val)))
    167 #define NIC_GET_2(sc, o)	(bus_space_read_2((sc)->sc_regt,	\
    168 					(sc)->sc_regh, (o)))
    169 #define NIC_PUT_2(sc, o, val)	(bus_space_write_2((sc)->sc_regt,	\
    170 					(sc)->sc_regh, (o), (val)))
    171 #define NIC_GET_4(sc, o)	(bus_space_read_4((sc)->sc_regt,	\
    172 					(sc)->sc_regh, (o)))
    173 #define NIC_PUT_4(sc, o, val)	(bus_space_write_4((sc)->sc_regt,	\
    174 					(sc)->sc_regh, (o), (val)))
    175 
    176 #define NIC_BSET(sc, o, b)						\
    177 	__asm__ __volatile("bset %0,%1" : : "di" ((u_short)(b)),	\
    178 		"g" (*(u_int8_t *)((sc)->sc_regh.base + (o))))
    179 #define NIC_BCLR(sc, o, b)						\
    180 	__asm__ __volatile("bclr %0,%1" : : "di" ((u_short)(b)),	\
    181 		"g" (*(u_int8_t *)((sc)->sc_regh.base + (o))))
    182 #define NIC_ANDW(sc, o, b)						\
    183 	__asm__ __volatile("andw %0,%1" : : "di" ((u_short)(b)),	\
    184 		"g" (*(u_int8_t *)((sc)->sc_regh.base + (o))))
    185 #define NIC_ORW(sc, o, b)						\
    186 	__asm__ __volatile("orw %0,%1" : : "di" ((u_short)(b)),	\
    187 		"g" (*(u_int8_t *)((sc)->sc_regh.base + (o))))
    188 
    189 
    190 /***********************/
    191 
    192 CFATTACH_DECL(netdock_nubus, sizeof(struct netdock_softc),
    193     netdock_nubus_match, netdock_nubus_attach, NULL, NULL);
    194 
    195 /***********************/
    196 
    197 static int
    198 netdock_nubus_match(parent, cf, aux)
    199 	struct device *parent;
    200 	struct cfdata *cf;
    201 	void *aux;
    202 {
    203 	struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
    204 	bus_space_handle_t bsh;
    205 	int rv;
    206 
    207 	if (bus_space_map(na->na_tag, NUBUS_SLOT2PA(na->slot),
    208 	    NBMEMSIZE, 0, &bsh))
    209 		return (0);
    210 
    211 	rv = 0;
    212 
    213 	if (na->category == NETDOCK_NUBUS_CATEGORY &&
    214 	    na->type == NETDOCK_NUBUS_TYPE &&
    215 	    na->drsw == NETDOCK_NUBUS_DRSW) {
    216 		/* assuming this IS Asante NetDock */
    217 		rv = 1;
    218 	}
    219 
    220 	if (na->category == ETHERMICRODOCK_NUBUS_CATEGORY &&
    221 	    na->type == ETHERMICRODOCK_NUBUS_TYPE &&
    222 	    na->drsw == ETHERMICRODOCK_NUBUS_DRSW) {
    223 		/* assuming this IS Newer EtherMicroDock */
    224 		rv = 1;
    225 	}
    226 
    227 	bus_space_unmap(na->na_tag, bsh, NBMEMSIZE);
    228 
    229 	return rv;
    230 }
    231 
    232 static void
    233 netdock_nubus_attach(parent, self, aux)
    234 	struct device *parent, *self;
    235 	void *aux;
    236 {
    237 	struct netdock_softc *sc = (struct netdock_softc *)self;
    238 	struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
    239 	bus_space_tag_t bst;
    240 	bus_space_handle_t bsh;
    241 	u_int8_t enaddr[ETHER_ADDR_LEN];
    242 	char *cardtype;
    243 
    244 	bst = na->na_tag;
    245 	if (bus_space_map(bst, NUBUS_SLOT2PA(na->slot), NBMEMSIZE, 0, &bsh)) {
    246 		printf(": failed to map memory space.\n");
    247 		return;
    248 	}
    249 
    250 	sc->sc_regt = bst;
    251 	cardtype = nubus_get_card_name(bst, bsh, na->fmt);
    252 
    253 #ifdef NETDOCK_DEBUG_DRIVER
    254 	netdock_print_driver(bst, bsh, na);
    255 #endif
    256 
    257 	if (netdock_nb_get_enaddr(bst, bsh, na, enaddr)) {
    258 		printf(": can't find MAC address.\n");
    259 		bus_space_unmap(bst, bsh, NBMEMSIZE);
    260 		return;
    261 	}
    262 
    263 	if (bus_space_subregion(bst, bsh, 0xe00300, 0xf0000, &sc->sc_regh)) {
    264 		printf(": failed to map register space.\n");
    265 		bus_space_unmap(bst, bsh, NBMEMSIZE);
    266 		return;
    267 	}
    268 
    269 	printf(": %s\n", cardtype);
    270 
    271 	if (netdock_setup(sc, enaddr)) {
    272 		bus_space_unmap(bst, bsh, NBMEMSIZE);
    273 		return;
    274 	}
    275 
    276 	add_nubus_intr(na->slot, netdock_intr, (void *)sc);
    277 
    278 	return;
    279 }
    280 
    281 static int
    282 netdock_nb_get_enaddr(bst, bsh, na, ep)
    283 	bus_space_tag_t bst;
    284 	bus_space_handle_t bsh;
    285 	struct nubus_attach_args *na;
    286 	u_int8_t *ep;
    287 {
    288 	nubus_dir dir;
    289 	nubus_dirent dirent;
    290 
    291 	/*
    292 	 * these hardwired resource IDs are only for NetDock
    293 	 */
    294 	nubus_get_main_dir(na->fmt, &dir);
    295 	if (nubus_find_rsrc(bst, bsh, na->fmt, &dir, 0x81, &dirent) <= 0)
    296 		return 1;
    297 	nubus_get_dir_from_rsrc(na->fmt, &dirent, &dir);
    298 	if (nubus_find_rsrc(bst, bsh, na->fmt, &dir, 0x80, &dirent) <= 0)
    299 		return 1;
    300 	if (nubus_get_ind_data(bst, bsh, na->fmt, &dirent,
    301 		ep, ETHER_ADDR_LEN) <= 0)
    302 		return 1;
    303 
    304 	return 0;
    305 }
    306 
    307 #ifdef NETDOCK_DEBUG_DRIVER
    308 static void
    309 netdock_print_driver(bst, bsh, na)
    310 	bus_space_tag_t bst;
    311 	bus_space_handle_t bsh;
    312 	struct nubus_attach_args *na;
    313 {
    314 #define HEADSIZE	(8+4)
    315 #define CODESIZE	(6759-4)
    316 	unsigned char mydata[HEADSIZE + CODESIZE];
    317 	nubus_dir dir;
    318 	nubus_dirent dirent;
    319 	int i, rv;
    320 
    321 	nubus_get_main_dir(na->fmt, &dir);
    322 	rv = nubus_find_rsrc(bst, bsh, na->fmt, &dir, 0x81, &dirent);
    323 	if (rv <= 0) {
    324 		printf(": can't find sResource.\n");
    325 		return;
    326 	}
    327 	nubus_get_dir_from_rsrc(na->fmt, &dirent, &dir);
    328 	if (nubus_find_rsrc(bst, bsh, na->fmt, &dir, NUBUS_RSRC_DRVRDIR,
    329 	    &dirent) <= 0) {
    330 		printf(": can't find sResource.\n");
    331 		return;
    332 	}
    333 	if (nubus_get_ind_data(bst, bsh, na->fmt,
    334 		&dirent, mydata, HEADSIZE+CODESIZE) <= 0) {
    335 		printf(": can't find indirect data.\n");
    336 		return;
    337 	}
    338 	printf("\n########## begin driver dir");
    339 	for (i = 0; i < HEADSIZE; i++) {
    340 		if (i % 16 == 0)
    341 			printf("\n%02x:",i);
    342 		printf(" %02x", mydata[i]);
    343 	}
    344 	printf("\n########## begin driver code");
    345 	for (i = 0; i < CODESIZE; i++) {
    346 		if (i % 16 == 0)
    347 			printf("\n%02x:",i);
    348 		printf(" %02x", mydata[i + HEADSIZE]);
    349 	}
    350 #if 0
    351 	printf("\n########## begin driver code (partial)\n");
    352 #define PARTSIZE 256
    353 #define OFFSET 0x1568
    354 	for (i = OFFSET; i < OFFSET + PARTSIZE; i++) {
    355 		if ((i - OFFSET) % 16 == 0)
    356 			printf("\n%02x:",i);
    357 		printf(" %02x", mydata[i + HEADSIZE]);
    358 	}
    359 #endif
    360 	printf("\n########## end\n");
    361 }
    362 #endif
    363 
    364 
    365 int
    366 netdock_setup(sc, lladdr)
    367 	struct netdock_softc *sc;
    368 	u_int8_t *lladdr;
    369 {
    370 	struct ifnet *ifp = &sc->sc_if;
    371 
    372 	bcopy(lladdr, sc->sc_enaddr, ETHER_ADDR_LEN);
    373 	printf("%s: Ethernet address %s\n",
    374 	    sc->sc_dev.dv_xname, ether_sprintf(lladdr));
    375 
    376 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    377 	ifp->if_softc = sc;
    378 	ifp->if_ioctl = netdock_ioctl;
    379 	ifp->if_start = netdock_start;
    380 	ifp->if_flags =
    381 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    382 	ifp->if_watchdog = netdock_watchdog;
    383 
    384 	if_attach(ifp);
    385 	ether_ifattach(ifp, lladdr);
    386 
    387 	return (0);
    388 }
    389 
    390 static int
    391 netdock_ioctl(ifp, cmd, data)
    392 	struct ifnet *ifp;
    393 	u_long cmd;
    394 	caddr_t data;
    395 {
    396 	struct ifaddr *ifa;
    397 	struct ifreq *ifr;
    398 	struct netdock_softc *sc = ifp->if_softc;
    399 	int s = splnet();
    400 	int err = 0;
    401 	int temp;
    402 
    403 	switch (cmd) {
    404 	case SIOCSIFADDR:
    405 		ifa = (struct ifaddr *)data;
    406 		ifp->if_flags |= IFF_UP;
    407 		switch (ifa->ifa_addr->sa_family) {
    408 #ifdef INET
    409 		case AF_INET:
    410 			(void)netdock_init(sc);
    411 			arp_ifinit(ifp, ifa);
    412 			break;
    413 #endif
    414 		default:
    415 			(void)netdock_init(sc);
    416 			break;
    417 		}
    418 		break;
    419 
    420 	case SIOCSIFFLAGS:
    421 		if ((ifp->if_flags & IFF_UP) == 0 &&
    422 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    423 			netdock_stop(sc);
    424 			ifp->if_flags &= ~IFF_RUNNING;
    425 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    426 		    (ifp->if_flags & IFF_RUNNING) == 0) {
    427 			(void)netdock_init(sc);
    428 		} else {
    429 			temp = ifp->if_flags & IFF_UP;
    430 			netdock_reset(sc);
    431 			ifp->if_flags |= temp;
    432 			netdock_start(ifp);
    433 		}
    434 		break;
    435 
    436 	case SIOCADDMULTI:
    437 	case SIOCDELMULTI:
    438 		ifr = (struct ifreq *)data;
    439 		if (cmd == SIOCADDMULTI)
    440 			err = ether_addmulti(ifr, &sc->sc_ethercom);
    441 		else
    442 			err = ether_delmulti(ifr, &sc->sc_ethercom);
    443 
    444 		if (err == ENETRESET) {
    445 			if (ifp->if_flags & IFF_RUNNING) {
    446 				temp = ifp->if_flags & IFF_UP;
    447 				netdock_reset(sc);
    448 				ifp->if_flags |= temp;
    449 			}
    450 			err = 0;
    451 		}
    452 		break;
    453 	default:
    454 		err = EINVAL;
    455 		break;
    456 	}
    457 	splx(s);
    458 	return (err);
    459 }
    460 
    461 static void
    462 netdock_start(ifp)
    463 	struct ifnet *ifp;
    464 {
    465 	struct netdock_softc *sc = ifp->if_softc;
    466 	struct mbuf *m;
    467 
    468 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    469 		return;
    470 
    471 	while (1) {
    472 		IF_DEQUEUE(&ifp->if_snd, m);
    473 		if (m == 0)
    474 			return;
    475 
    476 		if ((m->m_flags & M_PKTHDR) == 0)
    477 			panic("%s: netdock_start: no header mbuf",
    478 				sc->sc_dev.dv_xname);
    479 
    480 #if NBPFILTER > 0
    481 		if (ifp->if_bpf)
    482 			bpf_mtap(ifp->if_bpf, m);
    483 #endif
    484 
    485 		if ((netdock_put(sc, m)) == 0) {
    486 			IF_PREPEND(&ifp->if_snd, m);
    487 			return;
    488 		}
    489 
    490 		ifp->if_opackets++;
    491 	}
    492 
    493 }
    494 
    495 static void
    496 netdock_reset(sc)
    497 	struct netdock_softc *sc;
    498 {
    499 
    500 	netdock_stop(sc);
    501 	netdock_init(sc);
    502 }
    503 
    504 static int
    505 netdock_init(sc)
    506 	struct netdock_softc *sc;
    507 {
    508 	int s;
    509 	int saveisr;
    510 	int savetmp;
    511 
    512 	if (sc->sc_if.if_flags & IFF_RUNNING)
    513 		return (0);
    514 
    515 	s = splnet();
    516 
    517 	/* 0606 */
    518 	NIC_PUT_2(sc, REG_000E, 0x0200);
    519 	NIC_PUT_2(sc, REG_ISR , 0);
    520 	NIC_PUT_2(sc, REG_000E, 0);
    521 
    522 	NIC_PUT_2(sc, REG_0000, 0x8104);
    523 	NIC_PUT_2(sc, REG_0004, 0x0043);
    524 	NIC_PUT_2(sc, REG_000E, 0x0100);
    525 	NIC_PUT_2(sc, REG_0002, 0x6618);
    526 	NIC_PUT_2(sc, REG_0000, 0x8010);
    527 
    528 	NIC_PUT_1(sc, REG_0004 + 0, sc->sc_enaddr[0]);
    529 	NIC_PUT_1(sc, REG_0004 + 1, sc->sc_enaddr[1]);
    530 	NIC_PUT_1(sc, REG_0004 + 2, sc->sc_enaddr[2]);
    531 	NIC_PUT_1(sc, REG_0004 + 3, sc->sc_enaddr[3]);
    532 	NIC_PUT_1(sc, REG_0004 + 4, sc->sc_enaddr[4]);
    533 	NIC_PUT_1(sc, REG_0004 + 5, sc->sc_enaddr[5]);
    534 
    535 	NIC_PUT_2(sc, REG_ISR , 0x2008);
    536 	NIC_PUT_2(sc, REG_000E, 0x0200);
    537 	NIC_PUT_2(sc, REG_0000, 0x4000);
    538 	NIC_PUT_2(sc, REG_ISR, ISR_MASK);
    539 
    540 
    541 	/* 1320 */
    542 	saveisr = NIC_GET_2(sc, REG_ISR);
    543 	NIC_PUT_2(sc, REG_ISR , 0);
    544 	savetmp = NIC_GET_2(sc, REG_000E);
    545 	NIC_PUT_2(sc, REG_000E, 0x0100);
    546 	NIC_ANDW(sc, REG_ISR, ~ISR_BIT_03);
    547 	NIC_PUT_2(sc, REG_000E, savetmp);
    548 
    549 	/* 1382 */
    550 	savetmp = NIC_GET_2(sc, REG_000E);
    551 	NIC_PUT_2(sc, REG_000E, 0x0100);
    552 	NIC_ORW(sc, REG_ISR, ISR_BIT_03);
    553 	NIC_PUT_2(sc, REG_000E, savetmp);
    554 	NIC_PUT_2(sc, REG_ISR , saveisr);
    555 
    556 
    557 	sc->sc_if.if_flags |= IFF_RUNNING;
    558 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    559 
    560 	splx(s);
    561 	return (0);
    562 }
    563 
    564 static int
    565 netdock_stop(sc)
    566 	struct netdock_softc *sc;
    567 {
    568 	int s = splnet();
    569 
    570 	sc->sc_if.if_timer = 0;
    571 	sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_UP);
    572 
    573 	splx(s);
    574 	return (0);
    575 }
    576 
    577 static void
    578 netdock_watchdog(ifp)
    579 	struct ifnet *ifp;
    580 {
    581 	struct netdock_softc *sc = ifp->if_softc;
    582 	int tmp;
    583 
    584 	printf("netdock_watchdog: resetting chip\n");
    585 	tmp = ifp->if_flags & IFF_UP;
    586 	netdock_reset(sc);
    587 	ifp->if_flags |= tmp;
    588 }
    589 
    590 static u_int
    591 netdock_put(sc, m0)
    592 	struct netdock_softc *sc;
    593 	struct mbuf *m0;
    594 {
    595 	struct mbuf *m;
    596 	u_int totlen = 0;
    597 	u_int tmplen;
    598 	int isr;
    599 	int timeout;
    600 	int tmp;
    601 	u_int i;
    602 
    603 	for (m = m0; m; m = m->m_next)
    604 		totlen += m->m_len;
    605 
    606 	if (totlen >= ETHER_MAX_LEN)
    607 		panic("%s: netdock_put: packet overflow", sc->sc_dev.dv_xname);
    608 
    609 	totlen += 6;
    610 	tmplen = totlen;
    611 	tmplen &= 0xff00;
    612 	tmplen |= 0x2000;
    613 	NIC_PUT_2(sc, REG_0000, tmplen);
    614 
    615 	timeout = 0x3000;
    616 	while ((((isr = NIC_GET_2(sc, REG_ISR)) & ISR_READY) == 0) &&
    617 	    timeout--) {
    618 		if (isr & ISR_TX)
    619 			netdock_txint(sc);
    620 	}
    621 	if (timeout == 0)
    622 		return (0);
    623 
    624 	tmp = NIC_GET_2(sc, REG_0002);
    625 	tmp <<= 8;
    626 	NIC_PUT_2(sc, REG_0002, tmp);
    627 	NIC_PUT_2(sc, REG_0006, 0x240);
    628 	NIC_GET_2(sc, REG_ISR);
    629 	tmplen = ((totlen << 8) & 0xfe00) | ((totlen >> 8) & 0x00ff);
    630 	NIC_PUT_2(sc, REG_DATA, tmplen);
    631 
    632 	for (m = m0; m; m = m->m_next) {
    633 		u_char *data = mtod(m, u_char *);
    634 		int len = m->m_len;
    635 		int len4 = len >> 2;
    636 		u_int32_t *data4 = (u_int32_t *)data;
    637 		for (i = 0; i < len4; i++)
    638 			NIC_PUT_4(sc, REG_DATA, data4[i]);
    639 		for (i = len4 << 2; i < len; i++)
    640 			NIC_PUT_1(sc, REG_DATA, data[i]);
    641 	}
    642 
    643 	if (totlen & 0x01)
    644 		NIC_PUT_2(sc, REG_DATA, 0x2020);
    645 	else
    646 		NIC_PUT_2(sc, REG_DATA, 0);
    647 
    648 	NIC_PUT_2(sc, REG_0000, 0xc000);
    649 
    650 	m_freem(m0);
    651 	/* sc->sc_if.if_timer = 5; */
    652 	return (totlen);
    653 }
    654 
    655 void
    656 netdock_intr(arg)
    657 	void *arg;
    658 {
    659 	struct netdock_softc *sc = (struct netdock_softc *)arg;
    660 	int isr;
    661 	int tmp;
    662 
    663 	NIC_PUT_2(sc, REG_ISR, 0);
    664 	while ((isr = (NIC_GET_2(sc, REG_ISR) & ISR_ALL)) != 0) {
    665 		if (isr & ISR_TX)
    666 			netdock_txint(sc);
    667 
    668 		if (isr & ISR_RX)
    669 			netdock_rxint(sc);
    670 
    671 		if (isr & (ISR_BIT_0C | ISR_BIT_0D)) {
    672 			if (isr & ISR_BIT_0C) {
    673 				NIC_PUT_2(sc, REG_000E, 0);
    674 				NIC_BSET(sc, REG_0004, 0x08);
    675 				NIC_PUT_2(sc, REG_000E, 0x0200);
    676 			}
    677 			if (isr & ISR_BIT_0D) {
    678 				NIC_PUT_2(sc, REG_000E, 0);
    679 				tmp = NIC_GET_2(sc, REG_0002);
    680 				if (tmp & REG_0002_BIT_04)
    681 					NIC_GET_2(sc, REG_0006);
    682 				tmp = NIC_GET_2(sc, REG_0000);
    683 				if (tmp & REG_0000_BIT_08)
    684 					NIC_BSET(sc, REG_0000, 0x08);
    685 				NIC_PUT_2(sc, REG_000E, 0x0200);
    686 			}
    687 			NIC_PUT_2(sc, REG_ISR, isr);
    688 		}
    689 	}
    690 	NIC_PUT_2(sc, REG_ISR, ISR_MASK);
    691 }
    692 
    693 static void
    694 netdock_txint(sc)
    695 	struct netdock_softc *sc;
    696 {
    697 	struct ifnet *ifp = &sc->sc_if;
    698 	int savereg0002;
    699 	int reg0004;
    700 	int regdata;
    701 
    702 	ifp->if_flags &= ~IFF_OACTIVE;
    703 	ifp->if_timer = 0;
    704 
    705 	savereg0002 = NIC_GET_2(sc, REG_0002);
    706 
    707 	while (((reg0004 = NIC_GET_2(sc, REG_0004)) & REG_0004_BIT_0F) == 0) {
    708 		NIC_PUT_2(sc, REG_0002, reg0004);
    709 		NIC_PUT_2(sc, REG_0006, 0x0060);
    710 		NIC_GET_2(sc, REG_ISR);
    711 		regdata = NIC_GET_2(sc, REG_DATA);
    712 		if ((regdata & REG_DATA_BIT_08) == 0) {
    713 			/* ifp->if_collisions++; */
    714 			if (regdata & REG_DATA_BIT_07)
    715 			/* ifp->if_oerrors++; */
    716 			NIC_PUT_2(sc, REG_000E, 0);
    717 			NIC_ORW(sc, REG_0000, 0x0100);
    718 			NIC_PUT_2(sc, REG_000E, 0x0200);
    719 		}
    720 		NIC_GET_2(sc ,REG_DATA);
    721 
    722 		if (regdata & REG_DATA_BIT_0F)
    723 			NIC_GET_4(sc, REG_EFD00);
    724 		NIC_PUT_2(sc, REG_0000, 0xa000);
    725 		NIC_PUT_2(sc, REG_ISR, ISR_TX);
    726 	}
    727 
    728 	NIC_PUT_2(sc, REG_0002, savereg0002);
    729 	NIC_PUT_2(sc, REG_000E, 0);
    730 	NIC_GET_2(sc, REG_0006);
    731 	NIC_PUT_2(sc, REG_000E, 0x0200);
    732 	NIC_PUT_2(sc, REG_0006, 0);
    733 }
    734 
    735 static void
    736 netdock_rxint(sc)
    737 	struct netdock_softc *sc;
    738 {
    739 	struct ifnet *ifp = &sc->sc_if;
    740 	int regdata1;
    741 	int regdata2;
    742 	u_int len;
    743 	int timeout;
    744 
    745 	while ((NIC_GET_2(sc, REG_0004) & REG_0004_BIT_07) == 0) {
    746 		NIC_GET_2(sc, REG_ISR);
    747 		NIC_PUT_2(sc, REG_0006, 0x00e0);
    748 		NIC_GET_2(sc, REG_ISR);
    749 		regdata1 = NIC_GET_2(sc, REG_DATA);
    750 		regdata2 = NIC_GET_2(sc, REG_DATA);
    751 		len = ((regdata2 << 8) & 0x0700) | ((regdata2 >> 8) & 0x00ff);
    752 
    753 #if 0
    754 		printf("netdock_rxint: r1=0x%04x, r2=0x%04x, len=%d\n",
    755 		       regdata1, regdata2, len);
    756 #endif
    757 
    758 		if ((regdata1 & REG_DATA_BIT_04) == 0)
    759 			len -= 2;
    760 
    761 		if ((regdata1 & 0x00ac) == 0) {
    762 			if (netdock_read(sc, len))
    763 				ifp->if_ipackets++;
    764 			else
    765 				ifp->if_ierrors++;
    766 		} else {
    767 			ifp->if_ierrors++;
    768 
    769 			if (regdata1 & REG_DATA_BIT_02)
    770 				NIC_GET_4(sc, REG_EFD00);
    771 			if (regdata1 & REG_DATA_BIT_03)
    772 				;
    773 			if (regdata1 & REG_DATA_BIT_05)
    774 				NIC_GET_4(sc, REG_EFD00);
    775 			if (regdata1 & REG_DATA_BIT_07)
    776 				;
    777 		}
    778 
    779 		timeout = 0x14;
    780 		while ((NIC_GET_2(sc, REG_0000) & REG_0000_BIT_08) && timeout--)
    781 			;
    782 		if (timeout == 0)
    783 			;
    784 
    785 		NIC_PUT_2(sc, REG_0000, 0x8000);
    786 	}
    787 	NIC_PUT_2(sc, REG_0006, 0);
    788 }
    789 
    790 static int
    791 netdock_read(sc, len)
    792 	struct netdock_softc *sc;
    793 	int len;
    794 {
    795 	struct ifnet *ifp = &sc->sc_if;
    796 	struct mbuf *m;
    797 
    798 	m = netdock_get(sc, len);
    799 	if (m == 0)
    800 		return (0);
    801 
    802 	/* the data comes with frame checksum and ethernet header */
    803 	m->m_flags |= M_HASFCS;
    804 
    805 #if NBPFILTER > 0
    806 	if (ifp->if_bpf)
    807 		bpf_mtap(ifp->if_bpf, m);
    808 #endif
    809 
    810 	(*ifp->if_input)(ifp, m);
    811 
    812 	return (1);
    813 }
    814 
    815 static struct mbuf *
    816 netdock_get(sc, datalen)
    817 	struct netdock_softc *sc;
    818 	int datalen;
    819 {
    820 	struct mbuf *m, *top, **mp;
    821 	u_char *data;
    822 	int i;
    823 	int len;
    824 	int len4;
    825 	u_int32_t *data4;
    826 
    827 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    828 	if (m == NULL)
    829 		return (NULL);
    830 	m->m_pkthdr.rcvif = &sc->sc_if;
    831 	m->m_pkthdr.len = datalen;
    832 	len = MHLEN;
    833 	top = NULL;
    834 	mp = &top;
    835 
    836 	while (datalen > 0) {
    837 		if (top) {
    838 			MGET(m, M_DONTWAIT, MT_DATA);
    839 			if (m == 0) {
    840 				m_freem(top);
    841 				return (NULL);
    842 			}
    843 			len = MLEN;
    844 		}
    845 		if (datalen >= MINCLSIZE) {
    846 			MCLGET(m, M_DONTWAIT);
    847 			if ((m->m_flags & M_EXT) == 0) {
    848 				if (top)
    849 					m_freem(top);
    850 				return (NULL);
    851 			}
    852 			len = MCLBYTES;
    853 		}
    854 
    855 		if (mp == &top) {
    856 			caddr_t newdata = (caddr_t)
    857 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
    858 			    sizeof(struct ether_header);
    859 			len -= newdata - m->m_data;
    860 			m->m_data = newdata;
    861 		}
    862 
    863 		m->m_len = len = min(datalen, len);
    864 
    865 		data = mtod(m, u_char *);
    866 		len4 = len >> 2;
    867 		data4 = (u_int32_t *)data;
    868 		for (i = 0; i < len4; i++)
    869 			data4[i] = NIC_GET_4(sc, REG_DATA);
    870 		for (i = len4 << 2; i < len; i++)
    871 			data[i] = NIC_GET_1(sc, REG_DATA);
    872 
    873 		datalen -= len;
    874 		*mp = m;
    875 		mp = &m->m_next;
    876 	}
    877 
    878 	return (top);
    879 }
    880