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