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