if_netdock_nubus.c revision 1.15 1 /* $NetBSD: if_netdock_nubus.c,v 1.15 2007/09/01 14:27:56 jmmv 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.15 2007/09/01 14:27:56 jmmv Exp $");
47
48 #include <sys/param.h>
49 #include <sys/device.h>
50 #include <sys/socket.h>
51 #include <sys/systm.h>
52 #include <sys/mbuf.h>
53 #include <sys/ioctl.h>
54
55 #include <net/if.h>
56 #include <net/if_dl.h>
57 #include <net/if_ether.h>
58
59 #include "opt_inet.h"
60 #ifdef INET
61 #include <netinet/in.h>
62 #include <netinet/if_inarp.h>
63 #endif
64
65 #include "bpfilter.h"
66 #if NBPFILTER > 0
67 #include <net/bpf.h>
68 #endif
69
70 #include <machine/bus.h>
71 #include <machine/viareg.h>
72 #include <mac68k/nubus/nubus.h>
73
74 /***********************/
75
76 #define NETDOCK_DEBUG
77
78 #define NETDOCK_NUBUS_CATEGORY 0x0020
79 #define NETDOCK_NUBUS_TYPE 0x0003
80 #define NETDOCK_NUBUS_DRSW 0x0103
81 #define NETDOCK_NUBUS_DRHW 0x0100
82
83 #define ETHERMICRODOCK_NUBUS_CATEGORY 0x0020
84 #define ETHERMICRODOCK_NUBUS_TYPE 0x0003
85 #define ETHERMICRODOCK_NUBUS_DRSW 0x0102
86 #define ETHERMICRODOCK_NUBUS_DRHW 0x0100
87
88 #define REG_ISR 0x000c
89 #define REG_000E 0x000e
90 #define REG_0000 0x0000
91 #define REG_0002 0x0002
92 #define REG_0004 0x0004
93 #define REG_0006 0x0006
94 #define REG_DATA 0x0008
95 #define REG_EFD00 0xefd00
96
97 #define ISR_ALL 0x3300
98 #define ISR_TX 0x0200
99 #define ISR_RX 0x0100
100 #define ISR_READY 0x0800
101 #define ISR_BIT_0C 0x1000
102 #define ISR_BIT_0D 0x2000
103 #define ISR_MASK 0x0033
104 #define ISR_BIT_03 0x0008
105
106 #define REG_0002_BIT_04 0x0010
107 #define REG_0000_BIT_08 0x0100
108 #define REG_0004_BIT_0F 0x8000
109 #define REG_0004_BIT_07 0x0080
110 #define REG_DATA_BIT_02 0x0004
111 #define REG_DATA_BIT_03 0x0008
112 #define REG_DATA_BIT_04 0x0010
113 #define REG_DATA_BIT_05 0x0020
114 #define REG_DATA_BIT_08 0x0100
115 #define REG_DATA_BIT_07 0x0080
116 #define REG_DATA_BIT_0F 0x8000
117
118 /***********************/
119
120 typedef struct netdock_softc {
121 struct device sc_dev;
122 struct ethercom sc_ethercom;
123 #define sc_if sc_ethercom.ec_if
124
125 bus_space_tag_t sc_regt;
126 bus_space_handle_t sc_regh;
127
128 u_int8_t sc_enaddr[ETHER_ADDR_LEN];
129
130 } netdock_softc_t;
131
132 /***********************/
133
134 static int netdock_nubus_match(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, sc->sc_dev.dv_xname, 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 SIOCSIFADDR:
387 ifa = (struct ifaddr *)data;
388 ifp->if_flags |= IFF_UP;
389 switch (ifa->ifa_addr->sa_family) {
390 #ifdef INET
391 case AF_INET:
392 (void)netdock_init(sc);
393 arp_ifinit(ifp, ifa);
394 break;
395 #endif
396 default:
397 (void)netdock_init(sc);
398 break;
399 }
400 break;
401
402 case SIOCSIFFLAGS:
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 = EINVAL;
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 sc->sc_dev.dv_xname);
454
455 #if NBPFILTER > 0
456 if (ifp->if_bpf)
457 bpf_mtap(ifp->if_bpf, m);
458 #endif
459
460 if ((netdock_put(sc, m)) == 0) {
461 IF_PREPEND(&ifp->if_snd, m);
462 return;
463 }
464
465 ifp->if_opackets++;
466 }
467
468 }
469
470 static void
471 netdock_reset(struct netdock_softc *sc)
472 {
473
474 netdock_stop(sc);
475 netdock_init(sc);
476 }
477
478 static int
479 netdock_init(struct netdock_softc *sc)
480 {
481 int s;
482 int saveisr;
483 int savetmp;
484
485 if (sc->sc_if.if_flags & IFF_RUNNING)
486 return (0);
487
488 s = splnet();
489
490 /* 0606 */
491 NIC_PUT_2(sc, REG_000E, 0x0200);
492 NIC_PUT_2(sc, REG_ISR , 0);
493 NIC_PUT_2(sc, REG_000E, 0);
494
495 NIC_PUT_2(sc, REG_0000, 0x8104);
496 NIC_PUT_2(sc, REG_0004, 0x0043);
497 NIC_PUT_2(sc, REG_000E, 0x0100);
498 NIC_PUT_2(sc, REG_0002, 0x6618);
499 NIC_PUT_2(sc, REG_0000, 0x8010);
500
501 NIC_PUT_1(sc, REG_0004 + 0, sc->sc_enaddr[0]);
502 NIC_PUT_1(sc, REG_0004 + 1, sc->sc_enaddr[1]);
503 NIC_PUT_1(sc, REG_0004 + 2, sc->sc_enaddr[2]);
504 NIC_PUT_1(sc, REG_0004 + 3, sc->sc_enaddr[3]);
505 NIC_PUT_1(sc, REG_0004 + 4, sc->sc_enaddr[4]);
506 NIC_PUT_1(sc, REG_0004 + 5, sc->sc_enaddr[5]);
507
508 NIC_PUT_2(sc, REG_ISR , 0x2008);
509 NIC_PUT_2(sc, REG_000E, 0x0200);
510 NIC_PUT_2(sc, REG_0000, 0x4000);
511 NIC_PUT_2(sc, REG_ISR, ISR_MASK);
512
513
514 /* 1320 */
515 saveisr = NIC_GET_2(sc, REG_ISR);
516 NIC_PUT_2(sc, REG_ISR , 0);
517 savetmp = NIC_GET_2(sc, REG_000E);
518 NIC_PUT_2(sc, REG_000E, 0x0100);
519 NIC_ANDW(sc, REG_ISR, ~ISR_BIT_03);
520 NIC_PUT_2(sc, REG_000E, savetmp);
521
522 /* 1382 */
523 savetmp = NIC_GET_2(sc, REG_000E);
524 NIC_PUT_2(sc, REG_000E, 0x0100);
525 NIC_ORW(sc, REG_ISR, ISR_BIT_03);
526 NIC_PUT_2(sc, REG_000E, savetmp);
527 NIC_PUT_2(sc, REG_ISR , saveisr);
528
529
530 sc->sc_if.if_flags |= IFF_RUNNING;
531 sc->sc_if.if_flags &= ~IFF_OACTIVE;
532
533 splx(s);
534 return (0);
535 }
536
537 static int
538 netdock_stop(struct netdock_softc *sc)
539 {
540 int s = splnet();
541
542 sc->sc_if.if_timer = 0;
543 sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_UP);
544
545 splx(s);
546 return (0);
547 }
548
549 static void
550 netdock_watchdog(struct ifnet *ifp)
551 {
552 struct netdock_softc *sc = ifp->if_softc;
553 int tmp;
554
555 printf("netdock_watchdog: resetting chip\n");
556 tmp = ifp->if_flags & IFF_UP;
557 netdock_reset(sc);
558 ifp->if_flags |= tmp;
559 }
560
561 static u_int
562 netdock_put(struct netdock_softc *sc, struct mbuf *m0)
563 {
564 struct mbuf *m;
565 u_int totlen = 0;
566 u_int tmplen;
567 int isr;
568 int timeout;
569 int tmp;
570 u_int i;
571
572 for (m = m0; m; m = m->m_next)
573 totlen += m->m_len;
574
575 if (totlen >= ETHER_MAX_LEN)
576 panic("%s: netdock_put: packet overflow", sc->sc_dev.dv_xname);
577
578 totlen += 6;
579 tmplen = totlen;
580 tmplen &= 0xff00;
581 tmplen |= 0x2000;
582 NIC_PUT_2(sc, REG_0000, tmplen);
583
584 timeout = 0x3000;
585 while ((((isr = NIC_GET_2(sc, REG_ISR)) & ISR_READY) == 0) &&
586 timeout--) {
587 if (isr & ISR_TX)
588 netdock_txint(sc);
589 }
590 if (timeout == 0)
591 return (0);
592
593 tmp = NIC_GET_2(sc, REG_0002);
594 tmp <<= 8;
595 NIC_PUT_2(sc, REG_0002, tmp);
596 NIC_PUT_2(sc, REG_0006, 0x240);
597 NIC_GET_2(sc, REG_ISR);
598 tmplen = ((totlen << 8) & 0xfe00) | ((totlen >> 8) & 0x00ff);
599 NIC_PUT_2(sc, REG_DATA, tmplen);
600
601 for (m = m0; m; m = m->m_next) {
602 u_char *data = mtod(m, u_char *);
603 int len = m->m_len;
604 int len4 = len >> 2;
605 u_int32_t *data4 = (u_int32_t *)data;
606 for (i = 0; i < len4; i++)
607 NIC_PUT_4(sc, REG_DATA, data4[i]);
608 for (i = len4 << 2; i < len; i++)
609 NIC_PUT_1(sc, REG_DATA, data[i]);
610 }
611
612 if (totlen & 0x01)
613 NIC_PUT_2(sc, REG_DATA, 0x2020);
614 else
615 NIC_PUT_2(sc, REG_DATA, 0);
616
617 NIC_PUT_2(sc, REG_0000, 0xc000);
618
619 m_freem(m0);
620 /* sc->sc_if.if_timer = 5; */
621 return (totlen);
622 }
623
624 void
625 netdock_intr(void *arg)
626 {
627 struct netdock_softc *sc = (struct netdock_softc *)arg;
628 int isr;
629 int tmp;
630
631 NIC_PUT_2(sc, REG_ISR, 0);
632 while ((isr = (NIC_GET_2(sc, REG_ISR) & ISR_ALL)) != 0) {
633 if (isr & ISR_TX)
634 netdock_txint(sc);
635
636 if (isr & ISR_RX)
637 netdock_rxint(sc);
638
639 if (isr & (ISR_BIT_0C | ISR_BIT_0D)) {
640 if (isr & ISR_BIT_0C) {
641 NIC_PUT_2(sc, REG_000E, 0);
642 NIC_BSET(sc, REG_0004, 0x08);
643 NIC_PUT_2(sc, REG_000E, 0x0200);
644 }
645 if (isr & ISR_BIT_0D) {
646 NIC_PUT_2(sc, REG_000E, 0);
647 tmp = NIC_GET_2(sc, REG_0002);
648 if (tmp & REG_0002_BIT_04)
649 NIC_GET_2(sc, REG_0006);
650 tmp = NIC_GET_2(sc, REG_0000);
651 if (tmp & REG_0000_BIT_08)
652 NIC_BSET(sc, REG_0000, 0x08);
653 NIC_PUT_2(sc, REG_000E, 0x0200);
654 }
655 NIC_PUT_2(sc, REG_ISR, isr);
656 }
657 }
658 NIC_PUT_2(sc, REG_ISR, ISR_MASK);
659 }
660
661 static void
662 netdock_txint(struct netdock_softc *sc)
663 {
664 struct ifnet *ifp = &sc->sc_if;
665 int savereg0002;
666 int reg0004;
667 int regdata;
668
669 ifp->if_flags &= ~IFF_OACTIVE;
670 ifp->if_timer = 0;
671
672 savereg0002 = NIC_GET_2(sc, REG_0002);
673
674 while (((reg0004 = NIC_GET_2(sc, REG_0004)) & REG_0004_BIT_0F) == 0) {
675 NIC_PUT_2(sc, REG_0002, reg0004);
676 NIC_PUT_2(sc, REG_0006, 0x0060);
677 NIC_GET_2(sc, REG_ISR);
678 regdata = NIC_GET_2(sc, REG_DATA);
679 if ((regdata & REG_DATA_BIT_08) == 0) {
680 /* ifp->if_collisions++; */
681 if (regdata & REG_DATA_BIT_07)
682 /* ifp->if_oerrors++; */
683 NIC_PUT_2(sc, REG_000E, 0);
684 NIC_ORW(sc, REG_0000, 0x0100);
685 NIC_PUT_2(sc, REG_000E, 0x0200);
686 }
687 NIC_GET_2(sc ,REG_DATA);
688
689 if (regdata & REG_DATA_BIT_0F)
690 NIC_GET_4(sc, REG_EFD00);
691 NIC_PUT_2(sc, REG_0000, 0xa000);
692 NIC_PUT_2(sc, REG_ISR, ISR_TX);
693 }
694
695 NIC_PUT_2(sc, REG_0002, savereg0002);
696 NIC_PUT_2(sc, REG_000E, 0);
697 NIC_GET_2(sc, REG_0006);
698 NIC_PUT_2(sc, REG_000E, 0x0200);
699 NIC_PUT_2(sc, REG_0006, 0);
700 }
701
702 static void
703 netdock_rxint(struct netdock_softc *sc)
704 {
705 struct ifnet *ifp = &sc->sc_if;
706 int regdata1;
707 int regdata2;
708 u_int len;
709 int timeout;
710
711 while ((NIC_GET_2(sc, REG_0004) & REG_0004_BIT_07) == 0) {
712 NIC_GET_2(sc, REG_ISR);
713 NIC_PUT_2(sc, REG_0006, 0x00e0);
714 NIC_GET_2(sc, REG_ISR);
715 regdata1 = NIC_GET_2(sc, REG_DATA);
716 regdata2 = NIC_GET_2(sc, REG_DATA);
717 len = ((regdata2 << 8) & 0x0700) | ((regdata2 >> 8) & 0x00ff);
718
719 #if 0
720 printf("netdock_rxint: r1=0x%04x, r2=0x%04x, len=%d\n",
721 regdata1, regdata2, len);
722 #endif
723
724 if ((regdata1 & REG_DATA_BIT_04) == 0)
725 len -= 2;
726
727 /* CRC is included with the packet; trim it off. */
728 len -= ETHER_CRC_LEN;
729
730 if ((regdata1 & 0x00ac) == 0) {
731 if (netdock_read(sc, len))
732 ifp->if_ipackets++;
733 else
734 ifp->if_ierrors++;
735 } else {
736 ifp->if_ierrors++;
737
738 if (regdata1 & REG_DATA_BIT_02)
739 NIC_GET_4(sc, REG_EFD00);
740 if (regdata1 & REG_DATA_BIT_03)
741 ;
742 if (regdata1 & REG_DATA_BIT_05)
743 NIC_GET_4(sc, REG_EFD00);
744 if (regdata1 & REG_DATA_BIT_07)
745 ;
746 }
747
748 timeout = 0x14;
749 while ((NIC_GET_2(sc, REG_0000) & REG_0000_BIT_08) && timeout--)
750 ;
751 if (timeout == 0)
752 ;
753
754 NIC_PUT_2(sc, REG_0000, 0x8000);
755 }
756 NIC_PUT_2(sc, REG_0006, 0);
757 }
758
759 static int
760 netdock_read(struct netdock_softc *sc, int len)
761 {
762 struct ifnet *ifp = &sc->sc_if;
763 struct mbuf *m;
764
765 m = netdock_get(sc, len);
766 if (m == 0)
767 return (0);
768
769 #if NBPFILTER > 0
770 if (ifp->if_bpf)
771 bpf_mtap(ifp->if_bpf, m);
772 #endif
773
774 (*ifp->if_input)(ifp, m);
775
776 return (1);
777 }
778
779 static struct mbuf *
780 netdock_get(struct netdock_softc *sc, int datalen)
781 {
782 struct mbuf *m, *top, **mp;
783 u_char *data;
784 int i;
785 int len;
786 int len4;
787 u_int32_t *data4;
788
789 MGETHDR(m, M_DONTWAIT, MT_DATA);
790 if (m == NULL)
791 return (NULL);
792 m->m_pkthdr.rcvif = &sc->sc_if;
793 m->m_pkthdr.len = datalen;
794 len = MHLEN;
795 top = NULL;
796 mp = ⊤
797
798 while (datalen > 0) {
799 if (top) {
800 MGET(m, M_DONTWAIT, MT_DATA);
801 if (m == 0) {
802 m_freem(top);
803 return (NULL);
804 }
805 len = MLEN;
806 }
807 if (datalen >= MINCLSIZE) {
808 MCLGET(m, M_DONTWAIT);
809 if ((m->m_flags & M_EXT) == 0) {
810 if (top)
811 m_freem(top);
812 return (NULL);
813 }
814 len = MCLBYTES;
815 }
816
817 if (mp == &top) {
818 char *newdata = (char *)
819 ALIGN(m->m_data + sizeof(struct ether_header)) -
820 sizeof(struct ether_header);
821 len -= newdata - m->m_data;
822 m->m_data = newdata;
823 }
824
825 m->m_len = len = min(datalen, len);
826
827 data = mtod(m, u_char *);
828 len4 = len >> 2;
829 data4 = (u_int32_t *)data;
830 for (i = 0; i < len4; i++)
831 data4[i] = NIC_GET_4(sc, REG_DATA);
832 for (i = len4 << 2; i < len; i++)
833 data[i] = NIC_GET_1(sc, REG_DATA);
834
835 datalen -= len;
836 *mp = m;
837 mp = &m->m_next;
838 }
839
840 return (top);
841 }
842