xref: /netbsd-src/sys/arch/playstation2/dev/if_smap.c (revision da5f4674a3fc214be3572d358b66af40ab9401e7)
1 /*	$NetBSD: if_smap.c,v 1.5 2003/07/15 02:54:36 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by UCHIYAMA Yasushi.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: if_smap.c,v 1.5 2003/07/15 02:54:36 lukem Exp $");
41 
42 #include "debug_playstation2.h"
43 
44 #include "bpfilter.h"
45 #include "rnd.h"
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 
50 #include <sys/syslog.h>
51 #include <sys/mbuf.h>
52 #include <sys/ioctl.h>
53 #include <sys/socket.h>
54 
55 #include <playstation2/ee/eevar.h>
56 
57 #if NRND > 0
58 #include <sys/rnd.h>
59 #endif
60 
61 #include <net/if.h>
62 #include <net/if_dl.h>
63 #include <net/if_types.h>
64 
65 #include <net/if_ether.h>
66 #include <net/if_media.h>
67 
68 #include <dev/mii/mii.h>
69 #include <dev/mii/miivar.h>
70 
71 #include <netinet/in.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/in_var.h>
74 #include <netinet/ip.h>
75 #include <netinet/if_inarp.h>
76 
77 #if NBPFILTER > 0
78 #include <net/bpf.h>
79 #include <net/bpfdesc.h>
80 #endif
81 
82 #include <playstation2/dev/spdvar.h>
83 #include <playstation2/dev/spdreg.h>
84 #include <playstation2/dev/emac3var.h>
85 #include <playstation2/dev/if_smapreg.h>
86 
87 #ifdef SMAP_DEBUG
88 #include <playstation2/ee/gsvar.h>
89 int	smap_debug = 0;
90 #define	DPRINTF(fmt, args...)						\
91 	if (smap_debug)							\
92 		printf("%s: " fmt, __FUNCTION__ , ##args)
93 #define	DPRINTFN(n, arg)						\
94 	if (smap_debug > (n))						\
95 		printf("%s: " fmt, __FUNCTION__ , ##args)
96 #define STATIC
97 struct smap_softc *__sc;
98 void __smap_status(int);
99 void __smap_lock_check(const char *, int);
100 #define FUNC_ENTER()	__smap_lock_check(__FUNCTION__, 1)
101 #define FUNC_EXIT()	__smap_lock_check(__FUNCTION__, 0)
102 #else
103 #define	DPRINTF(arg...)		((void)0)
104 #define DPRINTFN(n, arg...)	((void)0)
105 #define STATIC			static
106 #define FUNC_ENTER()		((void)0)
107 #define FUNC_EXIT()		((void)0)
108 #endif
109 
110 struct smap_softc {
111 	struct emac3_softc emac3;
112 	struct ethercom ethercom;
113 
114 	u_int32_t *tx_buf;
115 	u_int32_t *rx_buf;
116 	struct smap_desc *tx_desc;
117 	struct smap_desc *rx_desc;
118 
119 #define	SMAP_FIFO_ALIGN		4
120 	int tx_buf_freesize;	/* buffer usage */
121 	int tx_desc_cnt;	/* descriptor usage */
122 	u_int16_t tx_fifo_ptr;
123 	int tx_done_index, tx_start_index;
124 	int rx_done_index;
125 
126 #if NRND > 0
127 	rndsource_element_t rnd_source;
128 #endif
129 };
130 
131 #define DEVNAME		(sc->emac3.dev.dv_xname)
132 #define ROUND4(x)	(((x) + 3) & ~3)
133 #define ROUND16(x)	(((x) + 15) & ~15)
134 
135 STATIC int smap_match(struct device *, struct cfdata *, void *);
136 STATIC void smap_attach(struct device *, struct device *, void *);
137 
138 CFATTACH_DECL(smap, sizeof (struct smap_softc),
139     smap_match, smap_attach, NULL, NULL);
140 
141 STATIC int smap_intr(void *);
142 STATIC void smap_rxeof(void *);
143 STATIC void smap_txeof(void *);
144 STATIC void smap_start(struct ifnet *);
145 STATIC void smap_watchdog(struct ifnet *);
146 STATIC int smap_ioctl(struct ifnet *, u_long, caddr_t);
147 STATIC int smap_init(struct ifnet *);
148 STATIC void smap_stop(struct ifnet *, int);
149 
150 STATIC int smap_get_eaddr(struct smap_softc *, u_int8_t *);
151 STATIC int smap_fifo_init(struct smap_softc *);
152 STATIC int smap_fifo_reset(bus_addr_t);
153 STATIC void smap_desc_init(struct smap_softc *);
154 
155 int
156 smap_match(struct device *parent, struct cfdata *cf, void *aux)
157 {
158 	struct spd_attach_args *spa = aux;
159 
160 	if (spa->spa_slot != SPD_NIC)
161 		return (0);
162 
163 	return (1);
164 }
165 
166 void
167 smap_attach(struct device *parent, struct device *self, void *aux)
168 {
169 	struct spd_attach_args *spa = aux;
170 	struct smap_softc *sc = (void *)self;
171 	struct emac3_softc *emac3 = &sc->emac3;
172 	struct ifnet *ifp = &sc->ethercom.ec_if;
173 	struct mii_data *mii = &emac3->mii;
174 	void *txbuf, *rxbuf;
175 	u_int16_t r;
176 
177 #ifdef SMAP_DEBUG
178 	__sc = sc;
179 #endif
180 
181 	printf(": %s\n", spa->spa_product_name);
182 
183 	/* SPD EEPROM */
184 	if (smap_get_eaddr(sc, emac3->eaddr) != 0)
185 		return;
186 
187 	printf("%s: Ethernet address %s\n", DEVNAME,
188 	    ether_sprintf(emac3->eaddr));
189 
190 	/* disable interrupts */
191 	r = _reg_read_2(SPD_INTR_ENABLE_REG16);
192 	r &= ~(SPD_INTR_RXEND | SPD_INTR_TXEND | SPD_INTR_RXDNV |
193 	    SPD_INTR_EMAC3);
194 	_reg_write_2(SPD_INTR_ENABLE_REG16, r);
195 	emac3_intr_disable();
196 
197 	/* clear pending interrupts */
198 	_reg_write_2(SPD_INTR_CLEAR_REG16, SPD_INTR_RXEND | SPD_INTR_TXEND |
199 	    SPD_INTR_RXDNV);
200 	emac3_intr_clear();
201 
202 	/* buffer descriptor mode */
203 	_reg_write_1(SMAP_DESC_MODE_REG8, 0);
204 
205 	if (smap_fifo_init(sc) != 0)
206 		return;
207 
208 	if (emac3_init(&sc->emac3) != 0)
209 		return;
210 	emac3_intr_disable();
211 	emac3_disable();
212 
213 	smap_desc_init(sc);
214 
215 	/* allocate temporary buffer */
216 	txbuf = malloc(ETHER_MAX_LEN - ETHER_CRC_LEN + SMAP_FIFO_ALIGN + 16,
217 	    M_DEVBUF, M_NOWAIT);
218 	if (txbuf == NULL) {
219 		printf("%s: no memory.\n", DEVNAME);
220 		return;
221 	}
222 
223 	rxbuf = malloc(ETHER_MAX_LEN + SMAP_FIFO_ALIGN + 16,
224 	    M_DEVBUF, M_NOWAIT);
225 	if (rxbuf == NULL) {
226 		printf("%s: no memory.\n", DEVNAME);
227 		free(txbuf, M_DEVBUF);
228 		return;
229 	}
230 
231 	sc->tx_buf = (u_int32_t *)ROUND16((vaddr_t)txbuf);
232 	sc->rx_buf = (u_int32_t *)ROUND16((vaddr_t)rxbuf);
233 
234 	/*
235 	 * setup MI layer
236 	 */
237 	strcpy(ifp->if_xname, DEVNAME);
238 	ifp->if_softc	= sc;
239 	ifp->if_start	= smap_start;
240 	ifp->if_ioctl	= smap_ioctl;
241 	ifp->if_init	= smap_init;
242 	ifp->if_stop	= smap_stop;
243 	ifp->if_watchdog= smap_watchdog;
244 	ifp->if_flags	= IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS |
245 	    IFF_MULTICAST;
246 	IFQ_SET_READY(&ifp->if_snd);
247 
248 	/* ifmedia setup. */
249 	mii->mii_ifp		= ifp;
250 	mii->mii_readreg	= emac3_phy_readreg;
251 	mii->mii_writereg	= emac3_phy_writereg;
252 	mii->mii_statchg	= emac3_phy_statchg;
253 	ifmedia_init(&mii->mii_media, 0, emac3_ifmedia_upd, emac3_ifmedia_sts);
254 	mii_attach(&emac3->dev, mii, 0xffffffff, MII_PHY_ANY,
255 	    MII_OFFSET_ANY, 0);
256 
257 	/* Choose a default media. */
258 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
259 		ifmedia_add(&mii->mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
260 		ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_NONE);
261 	} else {
262 		ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_AUTO);
263 	}
264 
265 	if_attach(ifp);
266 	ether_ifattach(ifp, emac3->eaddr);
267 
268 	spd_intr_establish(SPD_NIC, smap_intr, sc);
269 
270 #if NRND > 0
271 	rnd_attach_source(&sc->rnd_source, DEVNAME,
272 	    RND_TYPE_NET, 0);
273 #endif
274 }
275 
276 int
277 smap_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
278 {
279 	struct smap_softc *sc = ifp->if_softc;
280 	struct ifreq *ifr = (struct ifreq *) data;
281 	int error, s;
282 
283 	s = splnet();
284 
285 	switch (command) {
286 	case SIOCGIFMEDIA:
287 	case SIOCSIFMEDIA:
288 		error = ifmedia_ioctl(ifp, ifr, &sc->emac3.mii.mii_media,
289 		    command);
290 		break;
291 
292 	default:
293 		error = ether_ioctl(ifp, command, data);
294 
295 		if (error == ENETRESET) {
296 			emac3_setmulti(&sc->emac3, &sc->ethercom);
297 			error = 0;
298 		}
299 		break;
300 	}
301 
302 	splx(s);
303 
304 	return (error);
305 }
306 
307 int
308 smap_intr(void *arg)
309 {
310 	struct smap_softc *sc = arg;
311 	struct ifnet *ifp;
312 	u_int16_t cause, disable, r;
313 
314 	cause = _reg_read_2(SPD_INTR_STATUS_REG16) &
315 	    _reg_read_2(SPD_INTR_ENABLE_REG16);
316 
317 	disable = cause & (SPD_INTR_RXDNV | SPD_INTR_TXDNV);
318 	if (disable) {
319 		r = _reg_read_2(SPD_INTR_ENABLE_REG16);
320 		r &= ~disable;
321 		_reg_write_2(SPD_INTR_ENABLE_REG16, r);
322 
323 		printf("%s: invalid descriptor. (%c%c)\n", DEVNAME,
324 		    disable & SPD_INTR_RXDNV ? 'R' : '_',
325 		    disable & SPD_INTR_TXDNV ? 'T' : '_');
326 
327 		if (disable & SPD_INTR_RXDNV)
328 			smap_rxeof(arg);
329 
330 		_reg_write_2(SPD_INTR_CLEAR_REG16, disable);
331 	}
332 
333 	if (cause & SPD_INTR_TXEND) {
334 		_reg_write_2(SPD_INTR_CLEAR_REG16, SPD_INTR_TXEND);
335 		if (_reg_read_1(SMAP_RXFIFO_FRAME_REG8) > 0)
336 			cause |= SPD_INTR_RXEND;
337 		smap_txeof(arg);
338 	}
339 
340 	if (cause & SPD_INTR_RXEND) {
341 		_reg_write_2(SPD_INTR_CLEAR_REG16, SPD_INTR_RXEND);
342 		smap_rxeof(arg);
343 		if (sc->tx_desc_cnt > 0 &&
344 		    sc->tx_desc_cnt > _reg_read_1(SMAP_TXFIFO_FRAME_REG8))
345 			smap_txeof(arg);
346 	}
347 
348 	if (cause & SPD_INTR_EMAC3)
349 		emac3_intr(arg);
350 
351 	/* if transmission is pending, start here */
352 	ifp = &sc->ethercom.ec_if;
353 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
354 		smap_start(ifp);
355 #if NRND > 0
356 	rnd_add_uint32(&sc->rnd_source, cause | sc->tx_fifo_ptr << 16);
357 #endif
358 
359 	return (1);
360 }
361 
362 void
363 smap_rxeof(void *arg)
364 {
365 	struct smap_softc *sc = arg;
366 	struct smap_desc *d;
367 	struct ifnet *ifp = &sc->ethercom.ec_if;
368 	struct mbuf *m;
369 	u_int16_t r16, stat;
370 	u_int32_t *p;
371 	int i, j, sz, rxsz, cnt;
372 
373 	FUNC_ENTER();
374 
375 	i = sc->rx_done_index;
376 
377 	for (cnt = 0;; cnt++, i = (i + 1) & 0x3f) {
378 		m = NULL;
379 		d = &sc->rx_desc[i];
380 		stat = d->stat;
381 
382 		if ((stat & SMAP_RXDESC_EMPTY) != 0) {
383 			break;
384 		} else if (stat & 0x7fff) {
385 			ifp->if_ierrors++;
386 			goto next_packet;
387 		}
388 
389 		sz = d->sz;
390 		rxsz = ROUND4(sz);
391 
392 		KDASSERT(sz >= ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN);
393 		KDASSERT(sz <= ETHER_MAX_LEN);
394 
395 		/* load data from FIFO */
396 		_reg_write_2(SMAP_RXFIFO_PTR_REG16, d->ptr & 0x3ffc);
397 		p = sc->rx_buf;
398 		for (j = 0; j < rxsz; j += sizeof(u_int32_t)) {
399 			*p++ = _reg_read_4(SMAP_RXFIFO_DATA_REG);
400 		}
401 
402 		/* put to mbuf */
403 		MGETHDR(m, M_DONTWAIT, MT_DATA);
404 		if (m == NULL) {
405 			printf("%s: unable to allocate Rx mbuf\n", DEVNAME);
406 			ifp->if_ierrors++;
407 			goto next_packet;
408 		}
409 
410 		if (sz > (MHLEN - 2)) {
411 			MCLGET(m, M_DONTWAIT);
412 			if ((m->m_flags & M_EXT) == 0) {
413 				printf("%s: unable to allocate Rx cluster\n",
414 				    DEVNAME);
415 				m_freem(m);
416 				m = NULL;
417 				ifp->if_ierrors++;
418 				goto next_packet;
419 			}
420 		}
421 
422 		m->m_data += 2; /* for alignment */
423 		m->m_pkthdr.rcvif = ifp;
424 		m->m_pkthdr.len = m->m_len = sz;
425 		memcpy(mtod(m, caddr_t), (caddr_t)sc->rx_buf, sz);
426 
427 	next_packet:
428 		ifp->if_ipackets++;
429 
430 		_reg_write_1(SMAP_RXFIFO_FRAME_DEC_REG8, 1);
431 
432 		/* free descriptor */
433 		d->sz	= 0;
434 		d->ptr	= 0;
435 		d->stat	= SMAP_RXDESC_EMPTY;
436 		_wbflush();
437 
438 		if (m != NULL) {
439 #if NBPFILTER > 0
440 			if (ifp->if_bpf)
441 				bpf_mtap(ifp->if_bpf, m);
442 #endif
443 			(*ifp->if_input)(ifp, m);
444 		}
445 	}
446 	sc->rx_done_index = i;
447 
448 	r16 = _reg_read_2(SPD_INTR_ENABLE_REG16);
449 	if (((r16 & SPD_INTR_RXDNV) == 0) && cnt > 0) {
450 		r16  |= SPD_INTR_RXDNV;
451 		_reg_write_2(SPD_INTR_ENABLE_REG16, r16);
452 	}
453 
454 	FUNC_EXIT();
455 }
456 
457 void
458 smap_txeof(void *arg)
459 {
460 	struct smap_softc *sc = arg;
461 	struct ifnet *ifp = &sc->ethercom.ec_if;
462 	struct smap_desc *d;
463 	int i;
464 
465 	FUNC_ENTER();
466 
467 	/* clear the timeout timer. */
468 	ifp->if_timer = 0;
469 
470 	/* garbage collect */
471 	for (i = sc->tx_done_index;; i = (i + 1) & 0x3f) {
472 		u_int16_t stat;
473 
474 		d = &sc->tx_desc[i];
475 		stat = d->stat;
476 		if (stat & SMAP_TXDESC_READY) {
477 			/* all descriptor processed. */
478 			break;
479 		} else if (stat & 0x7fff) {
480 			if (stat & (SMAP_TXDESC_ECOLL | SMAP_TXDESC_LCOLL |
481 			    SMAP_TXDESC_MCOLL | SMAP_TXDESC_SCOLL))
482 				ifp->if_collisions++;
483 			else
484 				ifp->if_oerrors++;
485 		} else {
486 			ifp->if_opackets++;
487 		}
488 
489 		if (sc->tx_desc_cnt == 0)
490 			break;
491 
492 		sc->tx_buf_freesize += ROUND4(d->sz);
493 		sc->tx_desc_cnt--;
494 
495 		d->sz = 0;
496 		d->ptr = 0;
497 		d->stat = 0;
498 		_wbflush();
499 	}
500 	sc->tx_done_index = i;
501 
502 	/* OK to start transmit */
503 	ifp->if_flags &= ~IFF_OACTIVE;
504 
505 	FUNC_EXIT();
506 }
507 
508 void
509 smap_start(struct ifnet *ifp)
510 {
511 	struct smap_softc *sc = ifp->if_softc;
512 	struct smap_desc *d;
513 	struct mbuf *m0, *m;
514 	u_int8_t *p, *q;
515 	u_int32_t *r;
516 	int i, sz, pktsz;
517 	u_int16_t fifop;
518 	u_int16_t r16;
519 
520 	KDASSERT(ifp->if_flags & IFF_RUNNING);
521 	FUNC_ENTER();
522 
523 	while (1) {
524 		IFQ_POLL(&ifp->if_snd, m0);
525 		if (m0 == NULL)
526 			goto end;
527 
528 		pktsz = m0->m_pkthdr.len;
529 		KDASSERT(pktsz <= ETHER_MAX_LEN - ETHER_CRC_LEN);
530 		sz = ROUND4(pktsz);
531 
532 		if (sz > sc->tx_buf_freesize ||
533 		    sc->tx_desc_cnt >= SMAP_DESC_MAX ||
534 		    emac3_tx_done() != 0) {
535 			ifp->if_flags |= IFF_OACTIVE;
536 			goto end;
537 		}
538 
539 		IFQ_DEQUEUE(&ifp->if_snd, m0);
540 		KDASSERT(m0 != NULL);
541 #if NBPFILTER > 0
542 		if (ifp->if_bpf)
543 			bpf_mtap(ifp->if_bpf, m0);
544 #endif
545 
546 		p = (u_int8_t *)sc->tx_buf;
547 		q = p + sz;
548 		/* copy to temporary buffer area */
549 		for (m = m0; m != 0; m = m->m_next) {
550 			memcpy(p, mtod(m, caddr_t), m->m_len);
551 			p += m->m_len;
552 		}
553 		m_freem(m0);
554 
555 		/* zero padding area */
556 		for (; p < q; p++)
557 			*p = 0;
558 
559 		/* put to FIFO */
560 		fifop = sc->tx_fifo_ptr;
561 		KDASSERT((fifop & 3) == 0);
562 		_reg_write_2(SMAP_TXFIFO_PTR_REG16, fifop);
563 		sc->tx_fifo_ptr = (fifop + sz) & 0xfff;
564 
565 		r = sc->tx_buf;
566 		for (i = 0; i < sz; i += sizeof(u_int32_t))
567 			*(__volatile__ u_int32_t *)SMAP_TXFIFO_DATA_REG = *r++;
568 		_wbflush();
569 
570 		/* put FIFO to EMAC3 */
571 		d = &sc->tx_desc[sc->tx_start_index];
572 		KDASSERT((d->stat & SMAP_TXDESC_READY) == 0);
573 
574 		d->sz = pktsz;
575 		d->ptr = fifop + SMAP_TXBUF_BASE;
576 		d->stat = SMAP_TXDESC_READY | SMAP_TXDESC_GENFCS |
577 		    SMAP_TXDESC_GENPAD;
578 		_wbflush();
579 
580 		sc->tx_buf_freesize -= sz;
581 		sc->tx_desc_cnt++;
582 		sc->tx_start_index = (sc->tx_start_index + 1) & 0x3f;
583 		_reg_write_1(SMAP_TXFIFO_FRAME_INC_REG8, 1);
584 
585 		emac3_tx_kick();
586 		r16 = _reg_read_2(SPD_INTR_ENABLE_REG16);
587 		if ((r16 & SPD_INTR_TXDNV) == 0) {
588 			r16 |= SPD_INTR_TXDNV;
589 			_reg_write_2(SPD_INTR_ENABLE_REG16, r16);
590 		}
591 	}
592  end:
593 	/* set watchdog timer */
594 	ifp->if_timer = 5;
595 
596 	FUNC_EXIT();
597 }
598 
599 void
600 smap_watchdog(struct ifnet *ifp)
601 {
602 	struct smap_softc *sc = ifp->if_softc;
603 
604 	printf("%s: watchdog timeout\n",DEVNAME);
605 	sc->ethercom.ec_if.if_oerrors++;
606 
607 	smap_fifo_init(sc);
608 	smap_desc_init(sc);
609 	emac3_reset(&sc->emac3);
610 }
611 
612 int
613 smap_init(struct ifnet *ifp)
614 {
615 	struct smap_softc *sc = ifp->if_softc;
616 	u_int16_t r16;
617 
618 	smap_fifo_init(sc);
619 	emac3_reset(&sc->emac3);
620 	smap_desc_init(sc);
621 
622 	_reg_write_2(SPD_INTR_CLEAR_REG16, SPD_INTR_RXEND | SPD_INTR_TXEND |
623 	    SPD_INTR_RXDNV);
624 	emac3_intr_clear();
625 
626 	r16 = _reg_read_2(SPD_INTR_ENABLE_REG16);
627 	r16 |=  SPD_INTR_EMAC3 | SPD_INTR_RXEND | SPD_INTR_TXEND |
628 	    SPD_INTR_RXDNV;
629 	_reg_write_2(SPD_INTR_ENABLE_REG16, r16);
630 	emac3_intr_enable();
631 
632 	emac3_enable();
633 
634 	/* Program the multicast filter, if necessary. */
635 	emac3_setmulti(&sc->emac3, &sc->ethercom);
636 
637 	/* Set current media. */
638 	mii_mediachg(&sc->emac3.mii);
639 
640 	ifp->if_flags |= IFF_RUNNING;
641 
642 	return (0);
643 }
644 
645 void
646 smap_stop(struct ifnet *ifp, int disable)
647 {
648 	struct smap_softc *sc = ifp->if_softc;
649 
650 	mii_down(&sc->emac3.mii);
651 
652 	if (disable)
653 		emac3_disable();
654 
655 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
656 }
657 
658 /*
659  * FIFO
660  */
661 int
662 smap_fifo_init(struct smap_softc *sc)
663 {
664 
665 	if (smap_fifo_reset(SMAP_TXFIFO_CTRL_REG8) != 0)
666 		goto error;
667 
668 	if (smap_fifo_reset(SMAP_RXFIFO_CTRL_REG8) != 0)
669 		goto error;
670 
671 	return (0);
672 error:
673 	printf("%s: FIFO reset not complete.\n", DEVNAME);
674 
675 	return (1);
676 }
677 
678 int
679 smap_fifo_reset(bus_addr_t a)
680 {
681 	int retry = 10000;
682 
683 	_reg_write_1(a, SMAP_FIFO_RESET);
684 
685 	while ((_reg_read_1(a) & SMAP_FIFO_RESET) && --retry > 0)
686 		;
687 
688 	return (retry == 0);
689 }
690 
691 /*
692  * Buffer descriptor
693  */
694 void
695 smap_desc_init(struct smap_softc *sc)
696 {
697 	struct smap_desc *d;
698 	int i;
699 
700 	sc->tx_desc = (void *)SMAP_TXDESC_BASE;
701 	sc->rx_desc = (void *)SMAP_RXDESC_BASE;
702 
703 	sc->tx_buf_freesize = SMAP_TXBUF_SIZE;
704 	sc->tx_fifo_ptr = 0;
705 	sc->tx_start_index = 0;
706 	sc->tx_done_index = 0;
707 	sc->rx_done_index = 0;
708 
709 	/* intialize entry */
710 	d = sc->tx_desc;
711 	for (i = 0; i < SMAP_DESC_MAX; i++, d++) {
712 		d->stat = 0;
713 		d->__reserved = 0;
714 		d->sz = 0;
715 		d->ptr = 0;
716 	}
717 
718 	d = sc->rx_desc;
719 	for (i = 0; i < SMAP_DESC_MAX; i++, d++) {
720 		d->stat = SMAP_RXDESC_EMPTY;
721 		d->__reserved = 0;
722 		d->sz = 0;
723 		d->ptr = 0;
724 	}
725 	_wbflush();
726 }
727 
728 
729 /*
730  * EEPROM
731  */
732 int
733 smap_get_eaddr(struct smap_softc *sc, u_int8_t *eaddr)
734 {
735 	u_int16_t checksum, *p = (u_int16_t *)eaddr;
736 
737 	spd_eeprom_read(0, p, 3);
738 	spd_eeprom_read(3, &checksum, 1);
739 
740 	if (checksum != (u_int16_t)(p[0] + p[1] + p[2])) {
741 		printf("%s: Ethernet address checksum error.(%s)\n",
742 		    DEVNAME, ether_sprintf(eaddr));
743 		return (1);
744 	}
745 
746 	return (0);
747 }
748 
749 #ifdef SMAP_DEBUG
750 #include <mips/locore.h>
751 void
752 __smap_lock_check(const char *func, int enter)
753 {
754 	static int cnt;
755 	static const char *last;
756 
757 	cnt += enter ? 1 : -1;
758 
759 	if (cnt < 0 || cnt > 1)
760 		panic("%s cnt=%d last=%s", func, cnt, last);
761 
762 	last = func;
763 }
764 
765 void
766 __smap_status(int msg)
767 {
768 	static int cnt;
769 	__gsfb_print(1, "%d: tx=%d rx=%d txcnt=%d free=%d cnt=%d\n", msg,
770 	    _reg_read_1(SMAP_TXFIFO_FRAME_REG8),
771 	    _reg_read_1(SMAP_RXFIFO_FRAME_REG8), __sc->tx_desc_cnt,
772 	    __sc->tx_buf_freesize, cnt++);
773 }
774 #endif /* SMAP_DEBUG */
775