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