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