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