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