Home | History | Annotate | Line # | Download | only in dev
if_mc.c revision 1.5.6.2
      1 /*	$NetBSD: if_mc.c,v 1.5.6.2 2002/10/18 02:38:35 nathanw Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 David Huang <khym (at) bga.com>
      5  * All rights reserved.
      6  *
      7  * Portions of this code are based on code by Denton Gentry <denny1 (at) home.com>
      8  * and Yanagisawa Takeshi <yanagisw (at) aa.ap.titech.ac.jp>.
      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. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  *
     29  */
     30 
     31 /*
     32  * Bus attachment and DMA routines for the mc driver (Centris/Quadra
     33  * 660av and Quadra 840av onboard ethernet, based on the AMD Am79C940
     34  * MACE ethernet chip). Also uses the PSC (Peripheral Subsystem
     35  * Controller) for DMA to and from the MACE.
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/device.h>
     40 #include <sys/malloc.h>
     41 #include <sys/socket.h>
     42 #include <sys/systm.h>
     43 
     44 #include <net/if.h>
     45 #include <net/if_ether.h>
     46 #include <net/if_media.h>
     47 
     48 #include <uvm/uvm_extern.h>
     49 
     50 #include <dev/ofw/openfirm.h>
     51 
     52 #include <machine/pio.h>
     53 #include <machine/bus.h>
     54 #include <machine/autoconf.h>
     55 
     56 #include <macppc/dev/am79c950reg.h>
     57 #include <macppc/dev/if_mcvar.h>
     58 
     59 #define MC_BUFSIZE 0x800
     60 
     61 hide int	mc_match __P((struct device *, struct cfdata *, void *));
     62 hide void	mc_attach __P((struct device *, struct device *, void *));
     63 hide void	mc_init __P((struct mc_softc *sc));
     64 hide void	mc_putpacket __P((struct mc_softc *sc, u_int len));
     65 hide int	mc_dmaintr __P((void *arg));
     66 hide void	mc_reset_rxdma __P((struct mc_softc *sc));
     67 hide void	mc_reset_txdma __P((struct mc_softc *sc));
     68 hide void	mc_select_utp __P((struct mc_softc *sc));
     69 hide void	mc_select_aui __P((struct mc_softc *sc));
     70 hide int	mc_mediachange __P((struct mc_softc *sc));
     71 hide void	mc_mediastatus __P((struct mc_softc *sc, struct ifmediareq *));
     72 
     73 int mc_supmedia[] = {
     74 	IFM_ETHER | IFM_10_T,
     75 	IFM_ETHER | IFM_10_5,
     76 	/*IFM_ETHER | IFM_AUTO,*/
     77 };
     78 
     79 #define N_SUPMEDIA (sizeof(mc_supmedia) / sizeof(int));
     80 
     81 CFATTACH_DECL(mc, sizeof(struct mc_softc),
     82     mc_match, mc_attach, NULL, NULL);
     83 
     84 hide int
     85 mc_match(parent, cf, aux)
     86 	struct device *parent;
     87 	struct cfdata *cf;
     88 	void *aux;
     89 {
     90 	struct confargs *ca = aux;
     91 
     92 	if (strcmp(ca->ca_name, "mace") != 0)
     93 		return 0;
     94 
     95 	/* requires 6 regs */
     96 	if (ca->ca_nreg / sizeof(int) != 6)
     97 		return 0;
     98 
     99 	/* requires 3 intrs */
    100 	if (ca->ca_nintr / sizeof(int) != 3)
    101 		return 0;
    102 
    103 	return 1;
    104 }
    105 
    106 hide void
    107 mc_attach(parent, self, aux)
    108 	struct device *parent, *self;
    109 	void *aux;
    110 {
    111 	struct confargs *ca = aux;
    112 	struct mc_softc *sc = (struct mc_softc *)self;
    113 	u_int8_t myaddr[ETHER_ADDR_LEN];
    114 	u_int *reg;
    115 
    116 	sc->sc_node = ca->ca_node;
    117 
    118 	reg  = ca->ca_reg;
    119 	reg[0] += ca->ca_baseaddr;
    120 	reg[2] += ca->ca_baseaddr;
    121 	reg[4] += ca->ca_baseaddr;
    122 
    123 	sc->sc_txdma = mapiodev(reg[2], reg[3]);
    124 	sc->sc_rxdma = mapiodev(reg[4], reg[5]);
    125 	bus_space_map(sc->sc_regt, reg[0], reg[1], 0, &sc->sc_regh);
    126 					/* XXX sc_regt is uninitialized */
    127 	sc->sc_tail = 0;
    128 	sc->sc_txdmacmd = dbdma_alloc(sizeof(dbdma_command_t) * 2);
    129 	sc->sc_rxdmacmd = (void *)dbdma_alloc(sizeof(dbdma_command_t) * 8);
    130 	memset(sc->sc_txdmacmd, 0, sizeof(dbdma_command_t) * 2);
    131 	memset(sc->sc_rxdmacmd, 0, sizeof(dbdma_command_t) * 8);
    132 
    133 	printf(": irq %d,%d,%d",
    134 		ca->ca_intr[0], ca->ca_intr[1], ca->ca_intr[2]);
    135 
    136 	if (OF_getprop(sc->sc_node, "local-mac-address", myaddr, 6) != 6) {
    137 		printf(": failed to get MAC address.\n");
    138 		return;
    139 	}
    140 
    141 	/* allocate memory for transmit buffer and mark it non-cacheable */
    142 	sc->sc_txbuf = malloc(NBPG, M_DEVBUF, M_WAITOK);
    143 	sc->sc_txbuf_phys = kvtop(sc->sc_txbuf);
    144 	memset(sc->sc_txbuf, 0, NBPG);
    145 
    146 	/*
    147 	 * allocate memory for receive buffer and mark it non-cacheable
    148 	 * XXX This should use the bus_dma interface, since the buffer
    149 	 * needs to be physically contiguous. However, it seems that
    150 	 * at least on my system, malloc() does allocate contiguous
    151 	 * memory. If it's not, suggest reducing the number of buffers
    152 	 * to 2, which will fit in one 4K page.
    153 	 */
    154 	sc->sc_rxbuf = malloc(MC_NPAGES * NBPG, M_DEVBUF, M_WAITOK);
    155 	sc->sc_rxbuf_phys = kvtop(sc->sc_rxbuf);
    156 	memset(sc->sc_rxbuf, 0, MC_NPAGES * NBPG);
    157 
    158 	if ((int)sc->sc_txbuf & PGOFSET)
    159 		printf("txbuf is not page-aligned\n");
    160 	if ((int)sc->sc_rxbuf & PGOFSET)
    161 		printf("rxbuf is not page-aligned\n");
    162 
    163 	sc->sc_bus_init = mc_init;
    164 	sc->sc_putpacket = mc_putpacket;
    165 
    166 
    167 	/* disable receive DMA */
    168 	dbdma_reset(sc->sc_rxdma);
    169 
    170 	/* disable transmit DMA */
    171 	dbdma_reset(sc->sc_txdma);
    172 
    173 	/* install interrupt handlers */
    174 	/*intr_establish(ca->ca_intr[1], IST_LEVEL, IPL_NET, mc_dmaintr, sc);*/
    175 	intr_establish(ca->ca_intr[2], IST_LEVEL, IPL_NET, mc_dmaintr, sc);
    176 	intr_establish(ca->ca_intr[0], IST_LEVEL, IPL_NET, mcintr, sc);
    177 
    178 	sc->sc_biucc = XMTSP_64;
    179 	sc->sc_fifocc = XMTFW_16 | RCVFW_64 | XMTFWU | RCVFWU |
    180 	    XMTBRST | RCVBRST;
    181 	/*sc->sc_plscc = PORTSEL_10BT;*/
    182 	sc->sc_plscc = PORTSEL_GPSI | ENPLSIO;
    183 
    184 	/* mcsetup returns 1 if something fails */
    185 	if (mcsetup(sc, myaddr)) {
    186 		printf("mcsetup returns non zero\n");
    187 		return;
    188 	}
    189 #ifdef NOTYET
    190 	sc->sc_mediachange = mc_mediachange;
    191 	sc->sc_mediastatus = mc_mediastatus;
    192 	sc->sc_supmedia = mc_supmedia;
    193 	sc->sc_nsupmedia = N_SUPMEDIA;
    194 	sc->sc_defaultmedia = IFM_ETHER | IFM_10_T;
    195 #endif
    196 }
    197 
    198 /* Bus-specific initialization */
    199 hide void
    200 mc_init(sc)
    201 	struct mc_softc *sc;
    202 {
    203 	mc_reset_rxdma(sc);
    204 	mc_reset_txdma(sc);
    205 }
    206 
    207 hide void
    208 mc_putpacket(sc, len)
    209 	struct mc_softc *sc;
    210 	u_int len;
    211 {
    212 	dbdma_command_t *cmd = sc->sc_txdmacmd;
    213 
    214 	DBDMA_BUILD(cmd, DBDMA_CMD_OUT_LAST, 0, len, sc->sc_txbuf_phys,
    215 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    216 
    217 	dbdma_start(sc->sc_txdma, sc->sc_txdmacmd);
    218 }
    219 
    220 /*
    221  * Interrupt handler for the MACE DMA completion interrupts
    222  */
    223 int
    224 mc_dmaintr(arg)
    225 	void *arg;
    226 {
    227 	struct mc_softc *sc = arg;
    228 	int status, offset, statoff;
    229 	int datalen, resid;
    230 	int i, n;
    231 	dbdma_command_t *cmd;
    232 
    233 	/* We've received some packets from the MACE */
    234 
    235 	/* Loop through, processing each of the packets */
    236 	i = sc->sc_tail;
    237 	for (n = 0; n < MC_RXDMABUFS; n++, i++) {
    238 		if (i == MC_RXDMABUFS)
    239 			i = 0;
    240 
    241 		cmd = &sc->sc_rxdmacmd[i];
    242 		/* flushcache(cmd, sizeof(dbdma_command_t)); */
    243 		status = dbdma_ld16(&cmd->d_status);
    244 		resid = dbdma_ld16(&cmd->d_resid);
    245 
    246 		/*if ((status & D_ACTIVE) == 0)*/
    247 		if ((status & 0x40) == 0)
    248 			continue;
    249 
    250 #if 1
    251 		if (dbdma_ld16(&cmd->d_count) != ETHERMTU + 22)
    252 			printf("bad d_count\n");
    253 #endif
    254 
    255 		datalen = dbdma_ld16(&cmd->d_count) - resid;
    256 		datalen -= 4;	/* 4 == status bytes */
    257 
    258 		if (datalen < 4 + sizeof(struct ether_header)) {
    259 			printf("short packet len=%d\n", datalen);
    260 			/* continue; */
    261 			goto next;
    262 		}
    263 
    264 		offset = i * MC_BUFSIZE;
    265 		statoff = offset + datalen;
    266 
    267 		DBDMA_BUILD_CMD(cmd, DBDMA_CMD_STOP, 0, 0, 0, 0);
    268 		__asm __volatile("eieio");
    269 
    270 		/* flushcache(sc->sc_rxbuf + offset, datalen + 4); */
    271 
    272 		sc->sc_rxframe.rx_rcvcnt = sc->sc_rxbuf[statoff + 0];
    273 		sc->sc_rxframe.rx_rcvsts = sc->sc_rxbuf[statoff + 1];
    274 		sc->sc_rxframe.rx_rntpc  = sc->sc_rxbuf[statoff + 2];
    275 		sc->sc_rxframe.rx_rcvcc  = sc->sc_rxbuf[statoff + 3];
    276 		sc->sc_rxframe.rx_frame  = sc->sc_rxbuf + offset;
    277 
    278 		mc_rint(sc);
    279 
    280 next:
    281 		DBDMA_BUILD_CMD(cmd, DBDMA_CMD_IN_LAST, 0, DBDMA_INT_ALWAYS,
    282 			DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    283 		__asm __volatile("eieio");
    284 		cmd->d_status = 0;
    285 		cmd->d_resid = 0;
    286 		sc->sc_tail = i + 1;
    287 	}
    288 
    289 	dbdma_continue(sc->sc_rxdma);
    290 
    291 	return 1;
    292 }
    293 
    294 hide void
    295 mc_reset_rxdma(sc)
    296 	struct mc_softc *sc;
    297 {
    298 	dbdma_command_t *cmd = sc->sc_rxdmacmd;
    299 	dbdma_regmap_t *dmareg = sc->sc_rxdma;
    300 	int i;
    301 	u_int8_t maccc;
    302 
    303 	/* Disable receiver, reset the DMA channels */
    304 	maccc = NIC_GET(sc, MACE_MACCC);
    305 	NIC_PUT(sc, MACE_MACCC, maccc & ~ENRCV);
    306 
    307 	dbdma_reset(dmareg);
    308 
    309 	for (i = 0; i < MC_RXDMABUFS; i++) {
    310 		DBDMA_BUILD(cmd, DBDMA_CMD_IN_LAST, 0, ETHERMTU + 22,
    311 			sc->sc_rxbuf_phys + MC_BUFSIZE * i, DBDMA_INT_ALWAYS,
    312 			DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    313 		cmd++;
    314 	}
    315 
    316 	DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0, 0,
    317 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_ALWAYS);
    318 	dbdma_st32(&cmd->d_cmddep, kvtop((caddr_t)sc->sc_rxdmacmd));
    319 	cmd++;
    320 
    321 	dbdma_start(dmareg, sc->sc_rxdmacmd);
    322 
    323 	sc->sc_tail = 0;
    324 
    325 	/* Reenable receiver, reenable DMA */
    326 	NIC_PUT(sc, MACE_MACCC, maccc);
    327 }
    328 
    329 hide void
    330 mc_reset_txdma(sc)
    331 	struct mc_softc *sc;
    332 {
    333 	dbdma_command_t *cmd = sc->sc_txdmacmd;
    334 	dbdma_regmap_t *dmareg = sc->sc_txdma;
    335 	u_int8_t maccc;
    336 
    337 	/* disable transmitter */
    338 	maccc = NIC_GET(sc, MACE_MACCC);
    339 	NIC_PUT(sc, MACE_MACCC, maccc & ~ENXMT);
    340 
    341 	dbdma_reset(dmareg);
    342 
    343 	DBDMA_BUILD(cmd, DBDMA_CMD_OUT_LAST, 0, 0, sc->sc_txbuf_phys,
    344 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    345 	cmd++;
    346 	DBDMA_BUILD(cmd, DBDMA_CMD_STOP, 0, 0, 0,
    347 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    348 
    349 	out32rb(&dmareg->d_cmdptrhi, 0);
    350 	out32rb(&dmareg->d_cmdptrlo, kvtop((caddr_t)sc->sc_txdmacmd));
    351 
    352 	/* restore old value */
    353 	NIC_PUT(sc, MACE_MACCC, maccc);
    354 }
    355 
    356 void
    357 mc_select_utp(sc)
    358 	struct mc_softc *sc;
    359 {
    360 	sc->sc_plscc = PORTSEL_GPSI | ENPLSIO;
    361 }
    362 
    363 void
    364 mc_select_aui(sc)
    365 	struct mc_softc *sc;
    366 {
    367 	sc->sc_plscc = PORTSEL_AUI;
    368 }
    369 
    370 int
    371 mc_mediachange(sc)
    372 	struct mc_softc *sc;
    373 {
    374 	struct ifmedia *ifm = &sc->sc_media;
    375 
    376 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
    377 		return EINVAL;
    378 
    379 	switch (IFM_SUBTYPE(ifm->ifm_media)) {
    380 
    381 	case IFM_10_T:
    382 		mc_select_utp(sc);
    383 		break;
    384 
    385 	case IFM_10_5:
    386 		mc_select_aui(sc);
    387 		break;
    388 
    389 	default:
    390 		return EINVAL;
    391 	}
    392 
    393 	return 0;
    394 }
    395 
    396 void
    397 mc_mediastatus(sc, ifmr)
    398 	struct mc_softc *sc;
    399 	struct ifmediareq *ifmr;
    400 {
    401 	if (sc->sc_plscc == PORTSEL_AUI)
    402 		ifmr->ifm_active = IFM_ETHER | IFM_10_5;
    403 	else
    404 		ifmr->ifm_active = IFM_ETHER | IFM_10_T;
    405 }
    406