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