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