if_netdock_nubus.c revision 1.20 1 /* $NetBSD: if_netdock_nubus.c,v 1.20 2010/01/19 22:06:20 pooka 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.20 2010/01/19 22:06:20 pooka 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 if (ifp->if_bpf)
455 bpf_ops->bpf_mtap(ifp->if_bpf, 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 if (ifp->if_bpf)
768 bpf_ops->bpf_mtap(ifp->if_bpf, m);
769
770 (*ifp->if_input)(ifp, m);
771
772 return (1);
773 }
774
775 static struct mbuf *
776 netdock_get(struct netdock_softc *sc, int datalen)
777 {
778 struct mbuf *m, *top, **mp;
779 u_char *data;
780 int i;
781 int len;
782 int len4;
783 u_int32_t *data4;
784
785 MGETHDR(m, M_DONTWAIT, MT_DATA);
786 if (m == NULL)
787 return (NULL);
788 m->m_pkthdr.rcvif = &sc->sc_if;
789 m->m_pkthdr.len = datalen;
790 len = MHLEN;
791 top = NULL;
792 mp = ⊤
793
794 while (datalen > 0) {
795 if (top) {
796 MGET(m, M_DONTWAIT, MT_DATA);
797 if (m == 0) {
798 m_freem(top);
799 return (NULL);
800 }
801 len = MLEN;
802 }
803 if (datalen >= MINCLSIZE) {
804 MCLGET(m, M_DONTWAIT);
805 if ((m->m_flags & M_EXT) == 0) {
806 if (top)
807 m_freem(top);
808 return (NULL);
809 }
810 len = MCLBYTES;
811 }
812
813 if (mp == &top) {
814 char *newdata = (char *)
815 ALIGN(m->m_data + sizeof(struct ether_header)) -
816 sizeof(struct ether_header);
817 len -= newdata - m->m_data;
818 m->m_data = newdata;
819 }
820
821 m->m_len = len = min(datalen, len);
822
823 data = mtod(m, u_char *);
824 len4 = len >> 2;
825 data4 = (u_int32_t *)data;
826 for (i = 0; i < len4; i++)
827 data4[i] = NIC_GET_4(sc, REG_DATA);
828 for (i = len4 << 2; i < len; i++)
829 data[i] = NIC_GET_1(sc, REG_DATA);
830
831 datalen -= len;
832 *mp = m;
833 mp = &m->m_next;
834 }
835
836 return (top);
837 }
838