if_gm.c revision 1.6 1 /* $NetBSD: if_gm.c,v 1.6 2000/06/15 18:36:52 tsubai Exp $ */
2
3 /*-
4 * Copyright (c) 2000 Tsubai Masanari. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "opt_inet.h"
30 #include "opt_ns.h"
31 #include "bpfilter.h"
32
33 #include <sys/param.h>
34 #include <sys/device.h>
35 #include <sys/ioctl.h>
36 #include <sys/kernel.h>
37 #include <sys/mbuf.h>
38 #include <sys/socket.h>
39 #include <sys/systm.h>
40 #include <sys/callout.h>
41
42 #include <vm/vm.h>
43
44 #include <net/if.h>
45 #include <net/if_ether.h>
46 #include <net/if_media.h>
47
48 #if NBPFILTER > 0
49 #include <net/bpf.h>
50 #endif
51
52 #ifdef INET
53 #include <netinet/in.h>
54 #include <netinet/if_inarp.h>
55 #endif
56
57 #include <dev/mii/mii.h>
58 #include <dev/mii/miivar.h>
59
60 #include <dev/pci/pcivar.h>
61 #include <dev/pci/pcireg.h>
62 #include <dev/pci/pcidevs.h>
63
64 #include <dev/ofw/openfirm.h>
65 #include <macppc/dev/if_gmreg.h>
66 #include <machine/pio.h>
67
68 #define NTXBUF 4
69 #define NRXBUF 32
70
71 struct gmac_softc {
72 struct device sc_dev;
73 struct ethercom sc_ethercom;
74 vaddr_t sc_reg;
75 struct gmac_dma *sc_txlist;
76 struct gmac_dma *sc_rxlist;
77 int sc_txnext;
78 int sc_rxlast;
79 caddr_t sc_txbuf[NTXBUF];
80 caddr_t sc_rxbuf[NRXBUF];
81 struct mii_data sc_mii;
82 struct callout sc_tick_ch;
83 char sc_laddr[6];
84 };
85
86 #define sc_if sc_ethercom.ec_if
87
88 int gmac_match __P((struct device *, struct cfdata *, void *));
89 void gmac_attach __P((struct device *, struct device *, void *));
90
91 static __inline u_int gmac_read_reg __P((struct gmac_softc *, int));
92 static __inline void gmac_write_reg __P((struct gmac_softc *, int, u_int));
93
94 static __inline void gmac_start_txdma __P((struct gmac_softc *));
95 static __inline void gmac_start_rxdma __P((struct gmac_softc *));
96 static __inline void gmac_stop_txdma __P((struct gmac_softc *));
97 static __inline void gmac_stop_rxdma __P((struct gmac_softc *));
98
99 int gmac_intr __P((void *));
100 void gmac_tint __P((struct gmac_softc *));
101 void gmac_rint __P((struct gmac_softc *));
102 struct mbuf * gmac_get __P((struct gmac_softc *, caddr_t, int));
103 void gmac_start __P((struct ifnet *));
104 int gmac_put __P((struct gmac_softc *, caddr_t, struct mbuf *));
105
106 void gmac_stop __P((struct gmac_softc *));
107 void gmac_reset __P((struct gmac_softc *));
108 void gmac_init __P((struct gmac_softc *));
109 void gmac_init_mac __P((struct gmac_softc *));
110 void gmac_setladrf __P((struct gmac_softc *));
111
112 int gmac_ioctl __P((struct ifnet *, u_long, caddr_t));
113 void gmac_watchdog __P((struct ifnet *));
114
115 int gmac_mediachange __P((struct ifnet *));
116 void gmac_mediastatus __P((struct ifnet *, struct ifmediareq *));
117 int gmac_mii_readreg __P((struct device *, int, int));
118 void gmac_mii_writereg __P((struct device *, int, int, int));
119 void gmac_mii_statchg __P((struct device *));
120 void gmac_mii_tick __P((void *));
121
122 struct cfattach gm_ca = {
123 sizeof(struct gmac_softc), gmac_match, gmac_attach
124 };
125
126 int
127 gmac_match(parent, match, aux)
128 struct device *parent;
129 struct cfdata *match;
130 void *aux;
131 {
132 struct pci_attach_args *pa = aux;
133
134 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
135 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC)
136 return 1;
137
138 return 0;
139 }
140
141 void
142 gmac_attach(parent, self, aux)
143 struct device *parent, *self;
144 void *aux;
145 {
146 struct gmac_softc *sc = (void *)self;
147 struct pci_attach_args *pa = aux;
148 struct ifnet *ifp = &sc->sc_if;
149 struct mii_data *mii = &sc->sc_mii;
150 pci_intr_handle_t ih;
151 const char *intrstr = NULL;
152 int node, i;
153 char *p;
154 struct gmac_dma *dp;
155 u_int32_t reg[10];
156 u_char laddr[6];
157
158 node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
159 if (node == 0) {
160 printf(": cannot find gmac node\n");
161 return;
162 }
163
164 OF_getprop(node, "local-mac-address", laddr, sizeof laddr);
165 OF_getprop(node, "assigned-addresses", reg, sizeof reg);
166
167 bcopy(laddr, sc->sc_laddr, sizeof laddr);
168 sc->sc_reg = reg[2];
169
170 if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
171 pa->pa_intrline, &ih)) {
172 printf(": unable to map interrupt\n");
173 return;
174 }
175 intrstr = pci_intr_string(pa->pa_pc, ih);
176
177 if (pci_intr_establish(pa->pa_pc, ih, IPL_NET, gmac_intr, sc) == NULL) {
178 printf(": unable to establish interrupt");
179 if (intrstr)
180 printf(" at %s", intrstr);
181 printf("\n");
182 return;
183 }
184
185 /* Setup packet buffers and dma descriptors. */
186 p = malloc((NRXBUF + NTXBUF) * 2048 + 3 * 0x800, M_DEVBUF, M_NOWAIT);
187 if (p == NULL) {
188 printf(": cannot malloc buffers\n");
189 return;
190 }
191 p = (void *)roundup((vaddr_t)p, 0x800);
192 bzero(p, 2048 * (NRXBUF + NTXBUF) + 2 * 0x800);
193
194 sc->sc_rxlist = (void *)p;
195 p += 0x800;
196 sc->sc_txlist = (void *)p;
197 p += 0x800;
198
199 dp = sc->sc_rxlist;
200 for (i = 0; i < NRXBUF; i++) {
201 sc->sc_rxbuf[i] = p;
202 dp->address = htole32(vtophys((vaddr_t)p));
203 dp->cmd = htole32(GMAC_OWN);
204 dp++;
205 p += 2048;
206 }
207
208 dp = sc->sc_txlist;
209 for (i = 0; i < NTXBUF; i++) {
210 sc->sc_txbuf[i] = p;
211 dp->address = htole32(vtophys((vaddr_t)p));
212 dp++;
213 p += 2048;
214 }
215
216 printf(": Ethernet address %s\n", ether_sprintf(laddr));
217 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
218
219 callout_init(&sc->sc_tick_ch);
220
221 gmac_reset(sc);
222 gmac_init_mac(sc);
223
224 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
225 ifp->if_softc = sc;
226 ifp->if_ioctl = gmac_ioctl;
227 ifp->if_start = gmac_start;
228 ifp->if_watchdog = gmac_watchdog;
229 ifp->if_flags =
230 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
231
232 mii->mii_ifp = ifp;
233 mii->mii_readreg = gmac_mii_readreg;
234 mii->mii_writereg = gmac_mii_writereg;
235 mii->mii_statchg = gmac_mii_statchg;
236
237 ifmedia_init(&mii->mii_media, 0, gmac_mediachange, gmac_mediastatus);
238 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
239
240 /* Choose a default media. */
241 if (LIST_FIRST(&mii->mii_phys) == NULL) {
242 ifmedia_add(&mii->mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
243 ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_NONE);
244 } else
245 ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_AUTO);
246
247 if_attach(ifp);
248 ether_ifattach(ifp, laddr);
249
250 #if NBPFILTER > 0
251 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
252 #endif
253 }
254
255 u_int
256 gmac_read_reg(sc, reg)
257 struct gmac_softc *sc;
258 int reg;
259 {
260 return in32rb(sc->sc_reg + reg);
261 }
262
263 void
264 gmac_write_reg(sc, reg, val)
265 struct gmac_softc *sc;
266 int reg;
267 u_int val;
268 {
269 out32rb(sc->sc_reg + reg, val);
270 }
271
272 void
273 gmac_start_txdma(sc)
274 struct gmac_softc *sc;
275 {
276 u_int x;
277
278 x = gmac_read_reg(sc, GMAC_TXDMACONFIG);
279 x |= 1;
280 gmac_write_reg(sc, GMAC_TXDMACONFIG, x);
281 x = gmac_read_reg(sc, GMAC_TXMACCONFIG);
282 x |= 1;
283 gmac_write_reg(sc, GMAC_TXMACCONFIG, x);
284 }
285
286 void
287 gmac_start_rxdma(sc)
288 struct gmac_softc *sc;
289 {
290 u_int x;
291
292 x = gmac_read_reg(sc, GMAC_RXDMACONFIG);
293 x |= 1;
294 gmac_write_reg(sc, GMAC_RXDMACONFIG, x);
295 x = gmac_read_reg(sc, GMAC_RXMACCONFIG);
296 x |= 1;
297 gmac_write_reg(sc, GMAC_RXMACCONFIG, x);
298 }
299
300 void
301 gmac_stop_txdma(sc)
302 struct gmac_softc *sc;
303 {
304 u_int x;
305
306 x = gmac_read_reg(sc, GMAC_TXDMACONFIG);
307 x &= ~1;
308 gmac_write_reg(sc, GMAC_TXDMACONFIG, x);
309 x = gmac_read_reg(sc, GMAC_TXMACCONFIG);
310 x &= ~1;
311 gmac_write_reg(sc, GMAC_TXMACCONFIG, x);
312 }
313
314 void
315 gmac_stop_rxdma(sc)
316 struct gmac_softc *sc;
317 {
318 u_int x;
319
320 x = gmac_read_reg(sc, GMAC_RXDMACONFIG);
321 x &= ~1;
322 gmac_write_reg(sc, GMAC_RXDMACONFIG, x);
323 x = gmac_read_reg(sc, GMAC_RXMACCONFIG);
324 x &= ~1;
325 gmac_write_reg(sc, GMAC_RXMACCONFIG, x);
326 }
327
328 int
329 gmac_intr(v)
330 void *v;
331 {
332 struct gmac_softc *sc = v;
333 u_int status;
334
335 status = gmac_read_reg(sc, GMAC_STATUS) & 0xff;
336 if (status == 0)
337 return 0;
338
339 if (status & GMAC_INT_RXDONE)
340 gmac_rint(sc);
341
342 if (status & GMAC_INT_TXEMPTY)
343 gmac_tint(sc);
344
345 return 1;
346 }
347
348 void
349 gmac_tint(sc)
350 struct gmac_softc *sc;
351 {
352 struct ifnet *ifp = &sc->sc_if;
353
354 ifp->if_flags &= ~IFF_OACTIVE;
355 ifp->if_timer = 0;
356 gmac_start(ifp);
357 }
358
359 void
360 gmac_rint(sc)
361 struct gmac_softc *sc;
362 {
363 struct ifnet *ifp = &sc->sc_if;
364 volatile struct gmac_dma *dp;
365 struct mbuf *m;
366 int i, len;
367 u_int cmd;
368
369 for (i = sc->sc_rxlast;; i++) {
370 if (i == NRXBUF)
371 i = 0;
372
373 dp = &sc->sc_rxlist[i];
374 cmd = le32toh(dp->cmd);
375 if (cmd & GMAC_OWN)
376 break;
377 len = (cmd >> 16) & GMAC_LEN_MASK;
378 len -= 4; /* CRC */
379
380 if (le32toh(dp->cmd_hi) & 0x40000000) {
381 ifp->if_ierrors++;
382 goto next;
383 }
384
385 m = gmac_get(sc, sc->sc_rxbuf[i], len);
386 if (m == NULL) {
387 ifp->if_ierrors++;
388 goto next;
389 }
390
391 #if NBPFILTER > 0
392 /*
393 * Check if there's a BPF listener on this interface.
394 * If so, hand off the raw packet to BPF.
395 */
396 if (ifp->if_bpf)
397 bpf_tap(ifp->if_bpf, sc->sc_rxbuf[i], len);
398 #endif
399 (*ifp->if_input)(ifp, m);
400 ifp->if_ipackets++;
401
402 next:
403 dp->cmd_hi = 0;
404 __asm __volatile ("sync");
405 dp->cmd = htole32(GMAC_OWN);
406 }
407 sc->sc_rxlast = i;
408 }
409
410 struct mbuf *
411 gmac_get(sc, pkt, totlen)
412 struct gmac_softc *sc;
413 caddr_t pkt;
414 int totlen;
415 {
416 struct mbuf *m;
417 struct mbuf *top, **mp;
418 int len;
419
420 MGETHDR(m, M_DONTWAIT, MT_DATA);
421 if (m == 0)
422 return 0;
423 m->m_pkthdr.rcvif = &sc->sc_if;
424 m->m_pkthdr.len = totlen;
425 len = MHLEN;
426 top = 0;
427 mp = ⊤
428
429 while (totlen > 0) {
430 if (top) {
431 MGET(m, M_DONTWAIT, MT_DATA);
432 if (m == 0) {
433 m_freem(top);
434 return 0;
435 }
436 len = MLEN;
437 }
438 if (totlen >= MINCLSIZE) {
439 MCLGET(m, M_DONTWAIT);
440 if ((m->m_flags & M_EXT) == 0) {
441 m_free(m);
442 m_freem(top);
443 return 0;
444 }
445 len = MCLBYTES;
446 }
447 m->m_len = len = min(totlen, len);
448 bcopy(pkt, mtod(m, caddr_t), len);
449 pkt += len;
450 totlen -= len;
451 *mp = m;
452 mp = &m->m_next;
453 }
454
455 return top;
456 }
457
458 void
459 gmac_start(ifp)
460 struct ifnet *ifp;
461 {
462 struct gmac_softc *sc = ifp->if_softc;
463 struct mbuf *m;
464 caddr_t buff;
465 int i, tlen;
466 volatile struct gmac_dma *dp;
467
468 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
469 return;
470
471 for (;;) {
472 if (ifp->if_flags & IFF_OACTIVE)
473 break;
474
475 IF_DEQUEUE(&ifp->if_snd, m);
476 if (m == 0)
477 break;
478
479 /* 5 seconds to watch for failing to transmit */
480 ifp->if_timer = 5;
481 ifp->if_opackets++; /* # of pkts */
482
483 i = sc->sc_txnext;
484 buff = sc->sc_txbuf[i];
485 tlen = gmac_put(sc, buff, m);
486
487 dp = &sc->sc_txlist[i];
488 dp->cmd_hi = 0;
489 dp->address_hi = 0;
490 dp->cmd = htole32(tlen | GMAC_OWN | GMAC_SOP);
491
492 i++;
493 if (i == NTXBUF)
494 i = 0;
495 __asm __volatile ("sync");
496
497 gmac_write_reg(sc, GMAC_TXDMAKICK, i);
498 sc->sc_txnext = i;
499
500 #if NBPFILTER > 0
501 /*
502 * If BPF is listening on this interface, let it see the
503 * packet before we commit it to the wire.
504 */
505 if (ifp->if_bpf)
506 bpf_tap(ifp->if_bpf, buff, tlen);
507 #endif
508
509 i++;
510 if (i == NTXBUF)
511 i = 0;
512 if (i == gmac_read_reg(sc, GMAC_TXDMACOMPLETE)) {
513 ifp->if_flags |= IFF_OACTIVE;
514 break;
515 }
516 }
517 }
518
519 int
520 gmac_put(sc, buff, m)
521 struct gmac_softc *sc;
522 caddr_t buff;
523 struct mbuf *m;
524 {
525 struct mbuf *n;
526 int len, tlen = 0;
527
528 for (; m; m = n) {
529 len = m->m_len;
530 if (len == 0) {
531 MFREE(m, n);
532 continue;
533 }
534 bcopy(mtod(m, caddr_t), buff, len);
535 buff += len;
536 tlen += len;
537 MFREE(m, n);
538 }
539 if (tlen > 2048)
540 panic("%s: gmac_put packet overflow", sc->sc_dev.dv_xname);
541
542 return tlen;
543 }
544
545 void
546 gmac_reset(sc)
547 struct gmac_softc *sc;
548 {
549 int i, s;
550
551 s = splnet();
552
553 gmac_stop_txdma(sc);
554 gmac_stop_rxdma(sc);
555
556 gmac_write_reg(sc, GMAC_SOFTWARERESET, 3);
557 for (i = 10; i > 0; i--) {
558 delay(300000); /* XXX long delay */
559 if ((gmac_read_reg(sc, GMAC_SOFTWARERESET) & 3) == 0)
560 break;
561 }
562 if (i == 0)
563 printf("%s: reset timeout\n", sc->sc_dev.dv_xname);
564
565 sc->sc_txnext = 0;
566 sc->sc_rxlast = 0;
567 for (i = 0; i < NRXBUF; i++)
568 sc->sc_rxlist[i].cmd = htole32(GMAC_OWN);
569 __asm __volatile ("sync");
570
571 gmac_write_reg(sc, GMAC_TXDMADESCBASEHI, 0);
572 gmac_write_reg(sc, GMAC_TXDMADESCBASELO,
573 vtophys((vaddr_t)sc->sc_txlist));
574 gmac_write_reg(sc, GMAC_RXDMADESCBASEHI, 0);
575 gmac_write_reg(sc, GMAC_RXDMADESCBASELO,
576 vtophys((vaddr_t)sc->sc_rxlist));
577 gmac_write_reg(sc, GMAC_RXDMAKICK, NRXBUF);
578
579 splx(s);
580 }
581
582 void
583 gmac_stop(sc)
584 struct gmac_softc *sc;
585 {
586 struct ifnet *ifp = &sc->sc_if;
587 int s;
588
589 s = splnet();
590
591 callout_stop(&sc->sc_tick_ch);
592 mii_down(&sc->sc_mii);
593
594 gmac_stop_txdma(sc);
595 gmac_stop_rxdma(sc);
596
597 gmac_write_reg(sc, GMAC_INTMASK, 0xffffffff);
598
599 ifp->if_flags &= ~(IFF_UP | IFF_RUNNING);
600 ifp->if_timer = 0;
601
602 splx(s);
603 }
604
605 void
606 gmac_init_mac(sc)
607 struct gmac_softc *sc;
608 {
609 int i, tb;
610 char *laddr = sc->sc_laddr;
611
612 __asm ("mftb %0" : "=r"(tb));
613 gmac_write_reg(sc, GMAC_RANDOMSEED, tb);
614
615 /* init-mii */
616 gmac_write_reg(sc, GMAC_DATAPATHMODE, 4);
617 gmac_mii_writereg(&sc->sc_dev, 0, 0, 0x1000);
618
619 gmac_write_reg(sc, GMAC_TXDMACONFIG, 0xffc00);
620 gmac_write_reg(sc, GMAC_RXDMACONFIG, 0);
621 gmac_write_reg(sc, GMAC_MACPAUSE, 0x1bf0);
622 gmac_write_reg(sc, GMAC_INTERPACKETGAP0, 0);
623 gmac_write_reg(sc, GMAC_INTERPACKETGAP1, 8);
624 gmac_write_reg(sc, GMAC_INTERPACKETGAP2, 4);
625 gmac_write_reg(sc, GMAC_MINFRAMESIZE, ETHER_MIN_LEN);
626 gmac_write_reg(sc, GMAC_MAXFRAMESIZE, ETHER_MAX_LEN);
627 gmac_write_reg(sc, GMAC_PASIZE, 7);
628 gmac_write_reg(sc, GMAC_JAMSIZE, 4);
629 gmac_write_reg(sc, GMAC_ATTEMPTLIMIT,0x10);
630 gmac_write_reg(sc, GMAC_MACCNTLTYPE, 0x8808);
631
632 gmac_write_reg(sc, GMAC_MACADDRESS0, (laddr[4] << 8) | laddr[5]);
633 gmac_write_reg(sc, GMAC_MACADDRESS1, (laddr[2] << 8) | laddr[3]);
634 gmac_write_reg(sc, GMAC_MACADDRESS2, (laddr[0] << 8) | laddr[1]);
635 gmac_write_reg(sc, GMAC_MACADDRESS3, 0);
636 gmac_write_reg(sc, GMAC_MACADDRESS4, 0);
637 gmac_write_reg(sc, GMAC_MACADDRESS5, 0);
638 gmac_write_reg(sc, GMAC_MACADDRESS6, 1);
639 gmac_write_reg(sc, GMAC_MACADDRESS7, 0xc200);
640 gmac_write_reg(sc, GMAC_MACADDRESS8, 0x0180);
641 gmac_write_reg(sc, GMAC_MACADDRFILT0, 0);
642 gmac_write_reg(sc, GMAC_MACADDRFILT1, 0);
643 gmac_write_reg(sc, GMAC_MACADDRFILT2, 0);
644 gmac_write_reg(sc, GMAC_MACADDRFILT2_1MASK, 0);
645 gmac_write_reg(sc, GMAC_MACADDRFILT0MASK, 0);
646
647 for (i = 0; i < 0x6c; i += 4)
648 gmac_write_reg(sc, GMAC_HASHTABLE0 + i, 0);
649
650 gmac_write_reg(sc, GMAC_SLOTTIME, 0x40);
651
652 if (IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) {
653 gmac_write_reg(sc, GMAC_TXMACCONFIG, 6);
654 gmac_write_reg(sc, GMAC_XIFCONFIG, 1);
655 } else {
656 gmac_write_reg(sc, GMAC_TXMACCONFIG, 0);
657 gmac_write_reg(sc, GMAC_XIFCONFIG, 5);
658 }
659
660 if (0) /* g-bit? */
661 gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 3);
662 else
663 gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 0);
664 }
665
666 void
667 gmac_setladrf(sc)
668 struct gmac_softc *sc;
669 {
670 struct ifnet *ifp = &sc->sc_if;
671 struct ether_multi *enm;
672 struct ether_multistep step;
673 struct ethercom *ec = &sc->sc_ethercom;
674 u_char *cp;
675 u_int32_t crc;
676 u_int32_t hash[16];
677 u_int v;
678 int len, i;
679
680 /* Clear hash table */
681 for (i = 0; i < 16; i++)
682 hash[i] = 0;
683
684 /* Get current RX configuration */
685 v = gmac_read_reg(sc, GMAC_RXMACCONFIG);
686
687 if ((ifp->if_flags & IFF_PROMISC) != 0) {
688 /* Turn on promiscuous mode; turn off the hash filter */
689 v |= GMAC_RXMAC_PR;
690 v &= ~GMAC_RXMAC_HEN;
691 ifp->if_flags |= IFF_ALLMULTI;
692 goto chipit;
693 }
694
695 /* Turn off promiscuous mode; turn on the hash filter */
696 v &= ~GMAC_RXMAC_PR;
697 v |= GMAC_RXMAC_HEN;
698
699 /*
700 * Set up multicast address filter by passing all multicast addresses
701 * through a crc generator, and then using the high order 8 bits as an
702 * index into the 256 bit logical address filter. The high order bit
703 * selects the word, while the rest of the bits select the bit within
704 * the word.
705 */
706
707 ETHER_FIRST_MULTI(step, ec, enm);
708 while (enm != NULL) {
709 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, 6)) {
710 /*
711 * We must listen to a range of multicast addresses.
712 * For now, just accept all multicasts, rather than
713 * trying to set only those filter bits needed to match
714 * the range. (At this time, the only use of address
715 * ranges is for IP multicast routing, for which the
716 * range is big enough to require all bits set.)
717 */
718 for (i = 0; i < 16; i++)
719 hash[i] = 0xffff;
720 ifp->if_flags |= IFF_ALLMULTI;
721 goto chipit;
722 }
723
724 cp = enm->enm_addrlo;
725 crc = 0xffffffff;
726 for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
727 int octet = *cp++;
728 int i;
729
730 #define MC_POLY_LE 0xedb88320UL /* mcast crc, little endian */
731 for (i = 0; i < 8; i++) {
732 if ((crc & 1) ^ (octet & 1)) {
733 crc >>= 1;
734 crc ^= MC_POLY_LE;
735 } else {
736 crc >>= 1;
737 }
738 octet >>= 1;
739 }
740 }
741 /* Just want the 8 most significant bits. */
742 crc >>= 24;
743
744 /* Set the corresponding bit in the filter. */
745 hash[crc >> 4] |= 1 << (crc & 0xf);
746
747 ETHER_NEXT_MULTI(step, enm);
748 }
749
750 ifp->if_flags &= ~IFF_ALLMULTI;
751
752 chipit:
753 /* Now load the hash table into the chip */
754 for (i = 0; i < 16; i++)
755 gmac_write_reg(sc, GMAC_HASHTABLE0 + i * 4, hash[i]);
756
757 gmac_write_reg(sc, GMAC_RXMACCONFIG, v);
758 }
759
760 void
761 gmac_init(sc)
762 struct gmac_softc *sc;
763 {
764 struct ifnet *ifp = &sc->sc_if;
765
766 gmac_stop_txdma(sc);
767 gmac_stop_rxdma(sc);
768
769 gmac_init_mac(sc);
770 gmac_setladrf(sc);
771
772 gmac_start_txdma(sc);
773 gmac_start_rxdma(sc);
774
775 gmac_write_reg(sc, GMAC_INTMASK, ~(GMAC_INT_TXEMPTY | GMAC_INT_RXDONE));
776
777 ifp->if_flags |= IFF_RUNNING;
778 ifp->if_flags &= ~IFF_OACTIVE;
779 ifp->if_timer = 0;
780
781 callout_reset(&sc->sc_tick_ch, 1, gmac_mii_tick, sc);
782
783 gmac_start(ifp);
784 }
785
786 int
787 gmac_ioctl(ifp, cmd, data)
788 struct ifnet *ifp;
789 u_long cmd;
790 caddr_t data;
791 {
792 struct gmac_softc *sc = ifp->if_softc;
793 struct ifaddr *ifa = (struct ifaddr *)data;
794 struct ifreq *ifr = (struct ifreq *)data;
795 int s, error = 0;
796
797 s = splnet();
798
799 switch (cmd) {
800
801 case SIOCSIFADDR:
802 ifp->if_flags |= IFF_UP;
803
804 switch (ifa->ifa_addr->sa_family) {
805 #ifdef INET
806 case AF_INET:
807 gmac_init(sc);
808 arp_ifinit(ifp, ifa);
809 break;
810 #endif
811 #ifdef NS
812 case AF_NS:
813 {
814 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
815
816 if (ns_nullhost(*ina))
817 ina->x_host =
818 *(union ns_host *)LLADDR(ifp->if_sadl);
819 else {
820 bcopy(ina->x_host.c_host,
821 LLADDR(ifp->if_sadl),
822 sizeof(sc->sc_enaddr));
823 }
824 /* Set new address. */
825 gmac_init(sc);
826 break;
827 }
828 #endif
829 default:
830 gmac_init(sc);
831 break;
832 }
833 break;
834
835 case SIOCSIFFLAGS:
836 if ((ifp->if_flags & IFF_UP) == 0 &&
837 (ifp->if_flags & IFF_RUNNING) != 0) {
838 /*
839 * If interface is marked down and it is running, then
840 * stop it.
841 */
842 gmac_stop(sc);
843 ifp->if_flags &= ~IFF_RUNNING;
844 } else if ((ifp->if_flags & IFF_UP) != 0 &&
845 (ifp->if_flags & IFF_RUNNING) == 0) {
846 /*
847 * If interface is marked up and it is stopped, then
848 * start it.
849 */
850 gmac_init(sc);
851 } else {
852 /*
853 * Reset the interface to pick up changes in any other
854 * flags that affect hardware registers.
855 */
856 gmac_reset(sc);
857 gmac_init(sc);
858 }
859 #ifdef GMAC_DEBUG
860 if (ifp->if_flags & IFF_DEBUG)
861 sc->sc_flags |= GMAC_DEBUGFLAG;
862 #endif
863 break;
864
865 case SIOCADDMULTI:
866 case SIOCDELMULTI:
867 error = (cmd == SIOCADDMULTI) ?
868 ether_addmulti(ifr, &sc->sc_ethercom) :
869 ether_delmulti(ifr, &sc->sc_ethercom);
870
871 if (error == ENETRESET) {
872 /*
873 * Multicast list has changed; set the hardware filter
874 * accordingly.
875 */
876 gmac_init(sc);
877 /* gmac_setladrf(sc); */
878 error = 0;
879 }
880 break;
881
882 case SIOCGIFMEDIA:
883 case SIOCSIFMEDIA:
884 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
885 break;
886
887 default:
888 error = EINVAL;
889 }
890
891 splx(s);
892 return error;
893 }
894
895 void
896 gmac_watchdog(ifp)
897 struct ifnet *ifp;
898 {
899 struct gmac_softc *sc = ifp->if_softc;
900
901 printf("%s: device timeout\n", ifp->if_xname);
902 ifp->if_oerrors++;
903
904 gmac_reset(sc);
905 gmac_init(sc);
906 }
907
908 int
909 gmac_mediachange(ifp)
910 struct ifnet *ifp;
911 {
912 struct gmac_softc *sc = ifp->if_softc;
913
914 return mii_mediachg(&sc->sc_mii);
915 }
916
917 void
918 gmac_mediastatus(ifp, ifmr)
919 struct ifnet *ifp;
920 struct ifmediareq *ifmr;
921 {
922 struct gmac_softc *sc = ifp->if_softc;
923
924 mii_pollstat(&sc->sc_mii);
925
926 ifmr->ifm_status = sc->sc_mii.mii_media_status;
927 ifmr->ifm_active = sc->sc_mii.mii_media_active;
928 }
929
930 int
931 gmac_mii_readreg(dev, phy, reg)
932 struct device *dev;
933 int phy, reg;
934 {
935 struct gmac_softc *sc = (void *)dev;
936 int i;
937
938 gmac_write_reg(sc, GMAC_MIFFRAMEOUTPUT,
939 0x60020000 | (phy << 23) | (reg << 18));
940
941 for (i = 1000; i >= 0; i -= 10) {
942 if (gmac_read_reg(sc, GMAC_MIFFRAMEOUTPUT) & 0x10000)
943 break;
944 delay(10);
945 }
946 if (i < 0) {
947 printf("%s: gmac_mii_readreg: timeout\n", sc->sc_dev.dv_xname);
948 return 0;
949 }
950
951 return gmac_read_reg(sc, GMAC_MIFFRAMEOUTPUT) & 0xffff;
952 }
953
954 void
955 gmac_mii_writereg(dev, phy, reg, val)
956 struct device *dev;
957 int phy, reg, val;
958 {
959 struct gmac_softc *sc = (void *)dev;
960 int i;
961
962 gmac_write_reg(sc, GMAC_MIFFRAMEOUTPUT,
963 0x50020000 | (phy << 23) | (reg << 18) | (val & 0xffff));
964
965 for (i = 1000; i >= 0; i -= 10) {
966 if (gmac_read_reg(sc, GMAC_MIFFRAMEOUTPUT) & 0x10000)
967 break;
968 delay(10);
969 }
970 if (i < 0)
971 printf("%s: gmac_mii_writereg: timeout\n", sc->sc_dev.dv_xname);
972 }
973
974 void
975 gmac_mii_statchg(dev)
976 struct device *dev;
977 {
978 struct gmac_softc *sc = (void *)dev;
979
980 gmac_stop_txdma(sc);
981 gmac_stop_rxdma(sc);
982
983 if (IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) {
984 gmac_write_reg(sc, GMAC_TXMACCONFIG, 6);
985 gmac_write_reg(sc, GMAC_XIFCONFIG, 1);
986 } else {
987 gmac_write_reg(sc, GMAC_TXMACCONFIG, 0);
988 gmac_write_reg(sc, GMAC_XIFCONFIG, 5);
989 }
990
991 if (0) /* g-bit? */
992 gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 3);
993 else
994 gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 0);
995
996 gmac_start_txdma(sc);
997 gmac_start_rxdma(sc);
998 }
999
1000 void
1001 gmac_mii_tick(v)
1002 void *v;
1003 {
1004 struct gmac_softc *sc = v;
1005 int s;
1006
1007 s = splnet();
1008 mii_tick(&sc->sc_mii);
1009 splx(s);
1010
1011 callout_reset(&sc->sc_tick_ch, hz, gmac_mii_tick, sc);
1012 }
1013