Home | History | Annotate | Line # | Download | only in dev
if_smap.c revision 1.11
      1 /*	$NetBSD: if_smap.c,v 1.11 2008/01/19 22:10:15 dyoung 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.11 2008/01/19 22:10:15 dyoung 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, __func__ , ##args)
     93 #define	DPRINTFN(n, arg)						\
     94 	if (smap_debug > (n))						\
     95 		printf("%s: " fmt, __func__ , ##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(__func__, 1)
    101 #define FUNC_EXIT()	__smap_lock_check(__func__, 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, void *);
    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 	sc->ethercom.ec_mii = mii;
    254 	ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
    255 	mii_attach(&emac3->dev, mii, 0xffffffff, MII_PHY_ANY,
    256 	    MII_OFFSET_ANY, 0);
    257 
    258 	/* Choose a default media. */
    259 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
    260 		ifmedia_add(&mii->mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
    261 		ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_NONE);
    262 	} else {
    263 		ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_AUTO);
    264 	}
    265 
    266 	if_attach(ifp);
    267 	ether_ifattach(ifp, emac3->eaddr);
    268 
    269 	spd_intr_establish(SPD_NIC, smap_intr, sc);
    270 
    271 #if NRND > 0
    272 	rnd_attach_source(&sc->rnd_source, DEVNAME,
    273 	    RND_TYPE_NET, 0);
    274 #endif
    275 }
    276 
    277 int
    278 smap_ioctl(struct ifnet *ifp, u_long command, void *data)
    279 {
    280 	struct smap_softc *sc = ifp->if_softc;
    281 	struct ifreq *ifr = (struct ifreq *) data;
    282 	int error, s;
    283 
    284 	s = splnet();
    285 
    286 	error = ether_ioctl(ifp, command, data);
    287 
    288 	if (error == ENETRESET) {
    289 		if (ifp->if_flags & IFF_RUNNING)
    290 			emac3_setmulti(&sc->emac3, &sc->ethercom);
    291 		error = 0;
    292 	}
    293 
    294 	splx(s);
    295 
    296 	return (error);
    297 }
    298 
    299 int
    300 smap_intr(void *arg)
    301 {
    302 	struct smap_softc *sc = arg;
    303 	struct ifnet *ifp;
    304 	u_int16_t cause, disable, r;
    305 
    306 	cause = _reg_read_2(SPD_INTR_STATUS_REG16) &
    307 	    _reg_read_2(SPD_INTR_ENABLE_REG16);
    308 
    309 	disable = cause & (SPD_INTR_RXDNV | SPD_INTR_TXDNV);
    310 	if (disable) {
    311 		r = _reg_read_2(SPD_INTR_ENABLE_REG16);
    312 		r &= ~disable;
    313 		_reg_write_2(SPD_INTR_ENABLE_REG16, r);
    314 
    315 		printf("%s: invalid descriptor. (%c%c)\n", DEVNAME,
    316 		    disable & SPD_INTR_RXDNV ? 'R' : '_',
    317 		    disable & SPD_INTR_TXDNV ? 'T' : '_');
    318 
    319 		if (disable & SPD_INTR_RXDNV)
    320 			smap_rxeof(arg);
    321 
    322 		_reg_write_2(SPD_INTR_CLEAR_REG16, disable);
    323 	}
    324 
    325 	if (cause & SPD_INTR_TXEND) {
    326 		_reg_write_2(SPD_INTR_CLEAR_REG16, SPD_INTR_TXEND);
    327 		if (_reg_read_1(SMAP_RXFIFO_FRAME_REG8) > 0)
    328 			cause |= SPD_INTR_RXEND;
    329 		smap_txeof(arg);
    330 	}
    331 
    332 	if (cause & SPD_INTR_RXEND) {
    333 		_reg_write_2(SPD_INTR_CLEAR_REG16, SPD_INTR_RXEND);
    334 		smap_rxeof(arg);
    335 		if (sc->tx_desc_cnt > 0 &&
    336 		    sc->tx_desc_cnt > _reg_read_1(SMAP_TXFIFO_FRAME_REG8))
    337 			smap_txeof(arg);
    338 	}
    339 
    340 	if (cause & SPD_INTR_EMAC3)
    341 		emac3_intr(arg);
    342 
    343 	/* if transmission is pending, start here */
    344 	ifp = &sc->ethercom.ec_if;
    345 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    346 		smap_start(ifp);
    347 #if NRND > 0
    348 	rnd_add_uint32(&sc->rnd_source, cause | sc->tx_fifo_ptr << 16);
    349 #endif
    350 
    351 	return (1);
    352 }
    353 
    354 void
    355 smap_rxeof(void *arg)
    356 {
    357 	struct smap_softc *sc = arg;
    358 	struct smap_desc *d;
    359 	struct ifnet *ifp = &sc->ethercom.ec_if;
    360 	struct mbuf *m;
    361 	u_int16_t r16, stat;
    362 	u_int32_t *p;
    363 	int i, j, sz, rxsz, cnt;
    364 
    365 	FUNC_ENTER();
    366 
    367 	i = sc->rx_done_index;
    368 
    369 	for (cnt = 0;; cnt++, i = (i + 1) & 0x3f) {
    370 		m = NULL;
    371 		d = &sc->rx_desc[i];
    372 		stat = d->stat;
    373 
    374 		if ((stat & SMAP_RXDESC_EMPTY) != 0) {
    375 			break;
    376 		} else if (stat & 0x7fff) {
    377 			ifp->if_ierrors++;
    378 			goto next_packet;
    379 		}
    380 
    381 		sz = d->sz;
    382 		rxsz = ROUND4(sz);
    383 
    384 		KDASSERT(sz >= ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN);
    385 		KDASSERT(sz <= ETHER_MAX_LEN);
    386 
    387 		/* load data from FIFO */
    388 		_reg_write_2(SMAP_RXFIFO_PTR_REG16, d->ptr & 0x3ffc);
    389 		p = sc->rx_buf;
    390 		for (j = 0; j < rxsz; j += sizeof(u_int32_t)) {
    391 			*p++ = _reg_read_4(SMAP_RXFIFO_DATA_REG);
    392 		}
    393 
    394 		/* put to mbuf */
    395 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    396 		if (m == NULL) {
    397 			printf("%s: unable to allocate Rx mbuf\n", DEVNAME);
    398 			ifp->if_ierrors++;
    399 			goto next_packet;
    400 		}
    401 
    402 		if (sz > (MHLEN - 2)) {
    403 			MCLGET(m, M_DONTWAIT);
    404 			if ((m->m_flags & M_EXT) == 0) {
    405 				printf("%s: unable to allocate Rx cluster\n",
    406 				    DEVNAME);
    407 				m_freem(m);
    408 				m = NULL;
    409 				ifp->if_ierrors++;
    410 				goto next_packet;
    411 			}
    412 		}
    413 
    414 		m->m_data += 2; /* for alignment */
    415 		m->m_pkthdr.rcvif = ifp;
    416 		m->m_pkthdr.len = m->m_len = sz;
    417 		memcpy(mtod(m, void *), (void *)sc->rx_buf, sz);
    418 
    419 	next_packet:
    420 		ifp->if_ipackets++;
    421 
    422 		_reg_write_1(SMAP_RXFIFO_FRAME_DEC_REG8, 1);
    423 
    424 		/* free descriptor */
    425 		d->sz	= 0;
    426 		d->ptr	= 0;
    427 		d->stat	= SMAP_RXDESC_EMPTY;
    428 		_wbflush();
    429 
    430 		if (m != NULL) {
    431 #if NBPFILTER > 0
    432 			if (ifp->if_bpf)
    433 				bpf_mtap(ifp->if_bpf, m);
    434 #endif
    435 			(*ifp->if_input)(ifp, m);
    436 		}
    437 	}
    438 	sc->rx_done_index = i;
    439 
    440 	r16 = _reg_read_2(SPD_INTR_ENABLE_REG16);
    441 	if (((r16 & SPD_INTR_RXDNV) == 0) && cnt > 0) {
    442 		r16  |= SPD_INTR_RXDNV;
    443 		_reg_write_2(SPD_INTR_ENABLE_REG16, r16);
    444 	}
    445 
    446 	FUNC_EXIT();
    447 }
    448 
    449 void
    450 smap_txeof(void *arg)
    451 {
    452 	struct smap_softc *sc = arg;
    453 	struct ifnet *ifp = &sc->ethercom.ec_if;
    454 	struct smap_desc *d;
    455 	int i;
    456 
    457 	FUNC_ENTER();
    458 
    459 	/* clear the timeout timer. */
    460 	ifp->if_timer = 0;
    461 
    462 	/* garbage collect */
    463 	for (i = sc->tx_done_index;; i = (i + 1) & 0x3f) {
    464 		u_int16_t stat;
    465 
    466 		d = &sc->tx_desc[i];
    467 		stat = d->stat;
    468 		if (stat & SMAP_TXDESC_READY) {
    469 			/* all descriptor processed. */
    470 			break;
    471 		} else if (stat & 0x7fff) {
    472 			if (stat & (SMAP_TXDESC_ECOLL | SMAP_TXDESC_LCOLL |
    473 			    SMAP_TXDESC_MCOLL | SMAP_TXDESC_SCOLL))
    474 				ifp->if_collisions++;
    475 			else
    476 				ifp->if_oerrors++;
    477 		} else {
    478 			ifp->if_opackets++;
    479 		}
    480 
    481 		if (sc->tx_desc_cnt == 0)
    482 			break;
    483 
    484 		sc->tx_buf_freesize += ROUND4(d->sz);
    485 		sc->tx_desc_cnt--;
    486 
    487 		d->sz = 0;
    488 		d->ptr = 0;
    489 		d->stat = 0;
    490 		_wbflush();
    491 	}
    492 	sc->tx_done_index = i;
    493 
    494 	/* OK to start transmit */
    495 	ifp->if_flags &= ~IFF_OACTIVE;
    496 
    497 	FUNC_EXIT();
    498 }
    499 
    500 void
    501 smap_start(struct ifnet *ifp)
    502 {
    503 	struct smap_softc *sc = ifp->if_softc;
    504 	struct smap_desc *d;
    505 	struct mbuf *m0, *m;
    506 	u_int8_t *p, *q;
    507 	u_int32_t *r;
    508 	int i, sz, pktsz;
    509 	u_int16_t fifop;
    510 	u_int16_t r16;
    511 
    512 	KDASSERT(ifp->if_flags & IFF_RUNNING);
    513 	FUNC_ENTER();
    514 
    515 	while (1) {
    516 		IFQ_POLL(&ifp->if_snd, m0);
    517 		if (m0 == NULL)
    518 			goto end;
    519 
    520 		pktsz = m0->m_pkthdr.len;
    521 		KDASSERT(pktsz <= ETHER_MAX_LEN - ETHER_CRC_LEN);
    522 		sz = ROUND4(pktsz);
    523 
    524 		if (sz > sc->tx_buf_freesize ||
    525 		    sc->tx_desc_cnt >= SMAP_DESC_MAX ||
    526 		    emac3_tx_done() != 0) {
    527 			ifp->if_flags |= IFF_OACTIVE;
    528 			goto end;
    529 		}
    530 
    531 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    532 		KDASSERT(m0 != NULL);
    533 #if NBPFILTER > 0
    534 		if (ifp->if_bpf)
    535 			bpf_mtap(ifp->if_bpf, m0);
    536 #endif
    537 
    538 		p = (u_int8_t *)sc->tx_buf;
    539 		q = p + sz;
    540 		/* copy to temporary buffer area */
    541 		for (m = m0; m != 0; m = m->m_next) {
    542 			memcpy(p, mtod(m, void *), m->m_len);
    543 			p += m->m_len;
    544 		}
    545 		m_freem(m0);
    546 
    547 		/* zero padding area */
    548 		for (; p < q; p++)
    549 			*p = 0;
    550 
    551 		/* put to FIFO */
    552 		fifop = sc->tx_fifo_ptr;
    553 		KDASSERT((fifop & 3) == 0);
    554 		_reg_write_2(SMAP_TXFIFO_PTR_REG16, fifop);
    555 		sc->tx_fifo_ptr = (fifop + sz) & 0xfff;
    556 
    557 		r = sc->tx_buf;
    558 		for (i = 0; i < sz; i += sizeof(u_int32_t))
    559 			*(volatile u_int32_t *)SMAP_TXFIFO_DATA_REG = *r++;
    560 		_wbflush();
    561 
    562 		/* put FIFO to EMAC3 */
    563 		d = &sc->tx_desc[sc->tx_start_index];
    564 		KDASSERT((d->stat & SMAP_TXDESC_READY) == 0);
    565 
    566 		d->sz = pktsz;
    567 		d->ptr = fifop + SMAP_TXBUF_BASE;
    568 		d->stat = SMAP_TXDESC_READY | SMAP_TXDESC_GENFCS |
    569 		    SMAP_TXDESC_GENPAD;
    570 		_wbflush();
    571 
    572 		sc->tx_buf_freesize -= sz;
    573 		sc->tx_desc_cnt++;
    574 		sc->tx_start_index = (sc->tx_start_index + 1) & 0x3f;
    575 		_reg_write_1(SMAP_TXFIFO_FRAME_INC_REG8, 1);
    576 
    577 		emac3_tx_kick();
    578 		r16 = _reg_read_2(SPD_INTR_ENABLE_REG16);
    579 		if ((r16 & SPD_INTR_TXDNV) == 0) {
    580 			r16 |= SPD_INTR_TXDNV;
    581 			_reg_write_2(SPD_INTR_ENABLE_REG16, r16);
    582 		}
    583 	}
    584  end:
    585 	/* set watchdog timer */
    586 	ifp->if_timer = 5;
    587 
    588 	FUNC_EXIT();
    589 }
    590 
    591 void
    592 smap_watchdog(struct ifnet *ifp)
    593 {
    594 	struct smap_softc *sc = ifp->if_softc;
    595 
    596 	printf("%s: watchdog timeout\n",DEVNAME);
    597 	sc->ethercom.ec_if.if_oerrors++;
    598 
    599 	smap_fifo_init(sc);
    600 	smap_desc_init(sc);
    601 	emac3_reset(&sc->emac3);
    602 }
    603 
    604 int
    605 smap_init(struct ifnet *ifp)
    606 {
    607 	struct smap_softc *sc = ifp->if_softc;
    608 	u_int16_t r16;
    609 	int rc;
    610 
    611 	smap_fifo_init(sc);
    612 	emac3_reset(&sc->emac3);
    613 	smap_desc_init(sc);
    614 
    615 	_reg_write_2(SPD_INTR_CLEAR_REG16, SPD_INTR_RXEND | SPD_INTR_TXEND |
    616 	    SPD_INTR_RXDNV);
    617 	emac3_intr_clear();
    618 
    619 	r16 = _reg_read_2(SPD_INTR_ENABLE_REG16);
    620 	r16 |=  SPD_INTR_EMAC3 | SPD_INTR_RXEND | SPD_INTR_TXEND |
    621 	    SPD_INTR_RXDNV;
    622 	_reg_write_2(SPD_INTR_ENABLE_REG16, r16);
    623 	emac3_intr_enable();
    624 
    625 	emac3_enable();
    626 
    627 	/* Program the multicast filter, if necessary. */
    628 	emac3_setmulti(&sc->emac3, &sc->ethercom);
    629 
    630 	/* Set current media. */
    631 	if ((rc = mii_mediachg(&sc->emac3.mii)) == ENXIO)
    632 		rc = 0;
    633 	else if (rc != 0)
    634 		return rc;
    635 
    636 	ifp->if_flags |= IFF_RUNNING;
    637 
    638 	return (0);
    639 }
    640 
    641 void
    642 smap_stop(struct ifnet *ifp, int disable)
    643 {
    644 	struct smap_softc *sc = ifp->if_softc;
    645 
    646 	mii_down(&sc->emac3.mii);
    647 
    648 	if (disable)
    649 		emac3_disable();
    650 
    651 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    652 }
    653 
    654 /*
    655  * FIFO
    656  */
    657 int
    658 smap_fifo_init(struct smap_softc *sc)
    659 {
    660 
    661 	if (smap_fifo_reset(SMAP_TXFIFO_CTRL_REG8) != 0)
    662 		goto error;
    663 
    664 	if (smap_fifo_reset(SMAP_RXFIFO_CTRL_REG8) != 0)
    665 		goto error;
    666 
    667 	return (0);
    668 error:
    669 	printf("%s: FIFO reset not complete.\n", DEVNAME);
    670 
    671 	return (1);
    672 }
    673 
    674 int
    675 smap_fifo_reset(bus_addr_t a)
    676 {
    677 	int retry = 10000;
    678 
    679 	_reg_write_1(a, SMAP_FIFO_RESET);
    680 
    681 	while ((_reg_read_1(a) & SMAP_FIFO_RESET) && --retry > 0)
    682 		;
    683 
    684 	return (retry == 0);
    685 }
    686 
    687 /*
    688  * Buffer descriptor
    689  */
    690 void
    691 smap_desc_init(struct smap_softc *sc)
    692 {
    693 	struct smap_desc *d;
    694 	int i;
    695 
    696 	sc->tx_desc = (void *)SMAP_TXDESC_BASE;
    697 	sc->rx_desc = (void *)SMAP_RXDESC_BASE;
    698 
    699 	sc->tx_buf_freesize = SMAP_TXBUF_SIZE;
    700 	sc->tx_fifo_ptr = 0;
    701 	sc->tx_start_index = 0;
    702 	sc->tx_done_index = 0;
    703 	sc->rx_done_index = 0;
    704 
    705 	/* intialize entry */
    706 	d = sc->tx_desc;
    707 	for (i = 0; i < SMAP_DESC_MAX; i++, d++) {
    708 		d->stat = 0;
    709 		d->__reserved = 0;
    710 		d->sz = 0;
    711 		d->ptr = 0;
    712 	}
    713 
    714 	d = sc->rx_desc;
    715 	for (i = 0; i < SMAP_DESC_MAX; i++, d++) {
    716 		d->stat = SMAP_RXDESC_EMPTY;
    717 		d->__reserved = 0;
    718 		d->sz = 0;
    719 		d->ptr = 0;
    720 	}
    721 	_wbflush();
    722 }
    723 
    724 
    725 /*
    726  * EEPROM
    727  */
    728 int
    729 smap_get_eaddr(struct smap_softc *sc, u_int8_t *eaddr)
    730 {
    731 	u_int16_t checksum, *p = (u_int16_t *)eaddr;
    732 
    733 	spd_eeprom_read(0, p, 3);
    734 	spd_eeprom_read(3, &checksum, 1);
    735 
    736 	if (checksum != (u_int16_t)(p[0] + p[1] + p[2])) {
    737 		printf("%s: Ethernet address checksum error.(%s)\n",
    738 		    DEVNAME, ether_sprintf(eaddr));
    739 		return (1);
    740 	}
    741 
    742 	return (0);
    743 }
    744 
    745 #ifdef SMAP_DEBUG
    746 #include <mips/locore.h>
    747 void
    748 __smap_lock_check(const char *func, int enter)
    749 {
    750 	static int cnt;
    751 	static const char *last;
    752 
    753 	cnt += enter ? 1 : -1;
    754 
    755 	if (cnt < 0 || cnt > 1)
    756 		panic("%s cnt=%d last=%s", func, cnt, last);
    757 
    758 	last = func;
    759 }
    760 
    761 void
    762 __smap_status(int msg)
    763 {
    764 	static int cnt;
    765 	__gsfb_print(1, "%d: tx=%d rx=%d txcnt=%d free=%d cnt=%d\n", msg,
    766 	    _reg_read_1(SMAP_TXFIFO_FRAME_REG8),
    767 	    _reg_read_1(SMAP_RXFIFO_FRAME_REG8), __sc->tx_desc_cnt,
    768 	    __sc->tx_buf_freesize, cnt++);
    769 }
    770 #endif /* SMAP_DEBUG */
    771