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