Home | History | Annotate | Line # | Download | only in obio
if_mc_obio.c revision 1.1
      1 /*-
      2  * Copyright (c) 1997 David Huang <khym (at) bga.com>
      3  * All rights reserved.
      4  *
      5  * Portions of this code are based on code by Denton Gentry <denny1 (at) home.com>
      6  * and Yanagisawa Takeshi <yanagisw (at) aa.ap.titech.ac.jp>.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. The name of the author may not be used to endorse or promote products
     14  *    derived from this software without specific prior written permission
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  *
     27  */
     28 
     29 /*
     30  * Bus attachment and DMA routines for the mc driver (Centris/Quadra
     31  * 660av and Quadra 840av onboard ethernet, based on the AMD Am79C940
     32  * MACE ethernet chip). Also uses the PSC (Peripheral Subsystem
     33  * Controller) for DMA to and from the MACE.
     34  */
     35 
     36 #include <sys/param.h>
     37 #include <sys/device.h>
     38 #include <sys/malloc.h>
     39 #include <sys/socket.h>
     40 #include <sys/systm.h>
     41 
     42 #include <net/if.h>
     43 #include <net/if_ether.h>
     44 
     45 #include <vm/vm.h>
     46 
     47 #include <machine/bus.h>
     48 #include <machine/psc.h>
     49 
     50 #include <mac68k/dev/obiovar.h>
     51 #include <mac68k/dev/if_mcreg.h>
     52 #include <mac68k/dev/if_mcvar.h>
     53 
     54 #define MACE_REG_BASE	0x50F1C000
     55 #define MACE_PROM_BASE	0x50F08000
     56 
     57 hide int	mc_obio_match __P((struct device *, struct cfdata *, void *));
     58 hide void	mc_obio_attach __P((struct device *, struct device *, void *));
     59 hide void	mc_obio_init __P((struct mc_softc *sc));
     60 hide void	mc_obio_put __P((struct mc_softc *sc, u_int len));
     61 hide int	mc_dmaintr __P((void *arg));
     62 hide void	mc_reset_rxdma __P((struct mc_softc *sc));
     63 hide void	mc_reset_rxdma_set __P((struct mc_softc *, int set));
     64 hide void	mc_reset_txdma __P((struct mc_softc *sc));
     65 hide int	mc_obio_getaddr __P((struct mc_softc *, u_int8_t *));
     66 
     67 extern int	kvtop __P((register caddr_t addr));
     68 
     69 struct cfattach mc_obio_ca = {
     70 	sizeof(struct mc_softc), mc_obio_match, mc_obio_attach
     71 };
     72 
     73 hide int
     74 mc_obio_match(parent, cf, aux)
     75 	struct device *parent;
     76 	struct cfdata *cf;
     77 	void *aux;
     78 {
     79 	struct obio_attach_args *oa = aux;
     80 	bus_space_handle_t bsh;
     81 	int found = 0;
     82 
     83 	if (PSCBase == NULL)
     84 		return 0;
     85 
     86 	if (bus_space_map(oa->oa_tag, MACE_REG_BASE, MC_REGSIZE, 0, &bsh))
     87 		return 0;
     88 
     89 	/*
     90 	 * Make sure the MACE's I/O space is readable, and if it is,
     91 	 * try to read the CHIPID register. A MACE will always have
     92 	 * 0x?940, where the ? depends on the chip version.
     93 	 */
     94 	if (bus_probe(oa->oa_tag, bsh, 0, 1)) {
     95 		if ((bus_space_read_1(
     96 			oa->oa_tag, bsh, MACE_REG(MACE_CHIPIDL)) == 0x40) &&
     97 		    ((bus_space_read_1(
     98 			oa->oa_tag, bsh, MACE_REG(MACE_CHIPIDH)) & 0xf) == 9))
     99 			found = 1;
    100 	}
    101 
    102 	bus_space_unmap(oa->oa_tag, bsh, MC_REGSIZE);
    103 
    104 	return found;
    105 }
    106 
    107 hide void
    108 mc_obio_attach(parent, self, aux)
    109 	struct device *parent, *self;
    110 	void	*aux;
    111 {
    112 	struct obio_attach_args *oa = (struct obio_attach_args *)aux;
    113 	struct mc_softc *sc = (void *)self;
    114 	u_int8_t myaddr[ETHER_ADDR_LEN];
    115 	int i, noncontig = 0;
    116 
    117 	sc->sc_regt = oa->oa_tag;
    118 	sc->sc_biucc = XMTSP_64;
    119 	sc->sc_fifocc = XMTFW_16 | RCVFW_64 | XMTFWU | RCVFWU |
    120 	    XMTBRST | RCVBRST;
    121 	sc->sc_plscc = PORTSEL_AUI;
    122 
    123 	if (bus_space_map(sc->sc_regt, MACE_REG_BASE, MC_REGSIZE, 0,
    124 	    &sc->sc_regh)) {
    125 		printf(": failed to map space for MACE regs.\n");
    126 		return;
    127 	}
    128 
    129 	if (mc_obio_getaddr(sc, myaddr)) {
    130 		printf(": failed to get MAC address.\n");
    131 		return;
    132 	}
    133 
    134 	/* allocate memory for transmit buffer and mark it non-cacheable */
    135 	sc->sc_txbuf = malloc(NBPG, M_DEVBUF, M_WAITOK);
    136 	sc->sc_txbuf_phys = kvtop(sc->sc_txbuf);
    137 	physaccess (sc->sc_txbuf, (caddr_t)sc->sc_txbuf_phys, NBPG,
    138 	    PG_V | PG_RW | PG_CI);
    139 
    140 	/*
    141 	 * allocate memory for receive buffer and mark it non-cacheable
    142 	 * XXX This should use the bus_dma interface, since the buffer
    143 	 * needs to be physically contiguous. However, it seems that
    144 	 * at least on my system, malloc() does allocate contiguous
    145 	 * memory. If it's not, suggest reducing the number of buffers
    146 	 * to 2, which will fit in one 4K page.
    147 	 */
    148 	sc->sc_rxbuf = malloc(MC_NPAGES * NBPG, M_DEVBUF, M_WAITOK);
    149 	sc->sc_rxbuf_phys = kvtop(sc->sc_rxbuf);
    150 	for (i = 0; i < MC_NPAGES; i++) {
    151 		int pa;
    152 
    153 		pa = kvtop(sc->sc_rxbuf + NBPG*i);
    154 		physaccess (sc->sc_rxbuf + NBPG*i, (caddr_t)pa, NBPG,
    155 		    PG_V | PG_RW | PG_CI);
    156 		if (pa != sc->sc_rxbuf_phys + NBPG*i)
    157 			noncontig = 1;
    158 	}
    159 
    160 	if (noncontig) {
    161 		printf("%s: receive DMA buffer not contiguous! "
    162 		    "Try compiling with \"options MC_RXDMABUFS=2\"\n",
    163 		    sc->sc_dev.dv_xname);
    164 		return;
    165 	}
    166 
    167 	sc->sc_bus_init = mc_obio_init;
    168 	sc->sc_putpacket = mc_obio_put;
    169 
    170 	/* disable receive DMA */
    171 	psc_reg2(PSC_ENETRD_CTL) = 0x8800;
    172 	psc_reg2(PSC_ENETRD_CTL) = 0x1000;
    173 	psc_reg2(PSC_ENETRD_CMD + PSC_SET0) = 0x1100;
    174 	psc_reg2(PSC_ENETRD_CMD + PSC_SET1) = 0x1100;
    175 
    176 	/* disable transmit DMA */
    177 	psc_reg2(PSC_ENETWR_CTL) = 0x8800;
    178 	psc_reg2(PSC_ENETWR_CTL) = 0x1000;
    179 	psc_reg2(PSC_ENETWR_CMD + PSC_SET0) = 0x1100;
    180 	psc_reg2(PSC_ENETWR_CMD + PSC_SET1) = 0x1100;
    181 
    182 	/* install interrupt handlers */
    183 	add_psc_lev4_intr(PSCINTR_ENET_DMA, mc_dmaintr, sc);
    184 	add_psc_lev3_intr(mcintr, sc);
    185 
    186 	/* enable MACE DMA interrupts */
    187 	psc_reg1(PSC_LEV4_IER) = 0x80 | (1 << PSCINTR_ENET_DMA);
    188 
    189 	/* don't know what this does */
    190 	psc_reg2(PSC_ENETWR_CTL) = 0x9000;
    191 	psc_reg2(PSC_ENETRD_CTL) = 0x9000;
    192 	psc_reg2(PSC_ENETWR_CTL) = 0x0400;
    193 	psc_reg2(PSC_ENETRD_CTL) = 0x0400;
    194 
    195 	/* enable MACE interrupts */
    196 	psc_reg1(PSC_LEV3_IER) = 0x80 | (1 << PSCINTR_ENET);
    197 
    198 	/* mcsetup returns 1 if something fails */
    199 	if (mcsetup(sc, myaddr)) {
    200 		/* disable interrupts */
    201 		psc_reg1(PSC_LEV4_IER) = (1 << PSCINTR_ENET_DMA);
    202 		psc_reg1(PSC_LEV3_IER) = (1 << PSCINTR_ENET);
    203 		/* remove interrupt handlers */
    204 		remove_psc_lev4_intr(PSCINTR_ENET_DMA);
    205 		remove_psc_lev3_intr();
    206 
    207 		bus_space_unmap(sc->sc_regt, sc->sc_regh, MC_REGSIZE);
    208 		return;
    209 	}
    210 }
    211 
    212 /* Bus-specific initialization */
    213 hide void
    214 mc_obio_init(sc)
    215 	struct mc_softc *sc;
    216 {
    217 	mc_reset_rxdma(sc);
    218 	mc_reset_txdma(sc);
    219 }
    220 
    221 hide void
    222 mc_obio_put(sc, len)
    223 	struct mc_softc *sc;
    224 	u_int len;
    225 {
    226 	psc_reg4(PSC_ENETWR_ADDR + sc->sc_txset) = sc->sc_txbuf_phys;
    227 	psc_reg4(PSC_ENETWR_LEN + sc->sc_txset) = len;
    228 	psc_reg2(PSC_ENETWR_CMD + sc->sc_txset) = 0x9800;
    229 
    230 	sc->sc_txset ^= 0x10;
    231 }
    232 
    233 /*
    234  * Interrupt handler for the MACE DMA completion interrupts
    235  */
    236 int
    237 mc_dmaintr(arg)
    238 	void *arg;
    239 {
    240 	struct mc_softc *sc = arg;
    241 	u_int16_t status;
    242 	u_int32_t bufsleft, which;
    243 	int head;
    244 
    245 	/*
    246 	 * Not sure what this does... figure out if this interrupt is
    247 	 * really ours?
    248 	 */
    249 	while ((which = psc_reg4(0x804)) != psc_reg4(0x804))
    250 		;
    251 	if ((which & 0x60000000) == 0)
    252 		return 0;
    253 
    254 	/* Get the read channel status */
    255 	status = psc_reg2(PSC_ENETRD_CTL);
    256 	if (status & 0x2000) {
    257 		/* I think this is an exceptional condition. Reset the DMA */
    258 		mc_reset_rxdma(sc);
    259 #ifdef MCDEBUG
    260 		printf("%s: resetting receive DMA channel (status 0x%04x)\n",
    261 		    sc->sc_dev.dv_xname, status);
    262 #endif
    263 	} else if (status & 0x100) {
    264 		/* We've received some packets from the MACE */
    265 		int offset;
    266 
    267 		/* Clear the interrupt */
    268 		psc_reg2(PSC_ENETRD_CMD + sc->sc_rxset) = 0x1100;
    269 
    270 		/* See how may receive buffers are left */
    271 		bufsleft = psc_reg4(PSC_ENETRD_LEN + sc->sc_rxset);
    272 		head = MC_RXDMABUFS - bufsleft;
    273 
    274 #if 0 /* I don't think this should ever happen */
    275 		if (head == sc->sc_tail) {
    276 #ifdef MCDEBUG
    277 			printf("%s: head == tail: suspending DMA?\n",
    278 			    sc->sc_dev.dv_xname);
    279 #endif
    280 			psc_reg2(PSC_ENETRD_CMD + sc->sc_rxset) = 0x9000;
    281 		}
    282 #endif
    283 
    284 		/* Loop through, processing each of the packets */
    285 		for (; sc->sc_tail < head; sc->sc_tail++) {
    286 			offset = sc->sc_tail * 0x800;
    287 			sc->sc_rxframe.rx_rcvcnt = sc->sc_rxbuf[offset];
    288 			sc->sc_rxframe.rx_rcvsts = sc->sc_rxbuf[offset+2];
    289 			sc->sc_rxframe.rx_rntpc = sc->sc_rxbuf[offset+4];
    290 			sc->sc_rxframe.rx_rcvcc = sc->sc_rxbuf[offset+6];
    291 			sc->sc_rxframe.rx_frame = sc->sc_rxbuf + offset + 16;
    292 
    293 			mc_rint(sc);
    294 		}
    295 
    296 		/*
    297 		 * If we're out of buffers, reset this register set
    298 		 * and switch to the other one. Otherwise, reactivate
    299 		 * this set.
    300 		 */
    301 		if (bufsleft == 0) {
    302 			mc_reset_rxdma_set(sc, sc->sc_rxset);
    303 			sc->sc_rxset ^= 0x10;
    304 		} else
    305 			psc_reg2(PSC_ENETRD_CMD + sc->sc_rxset) = 0x9800;
    306 	}
    307 
    308 	/* Get the write channel status */
    309 	status = psc_reg2(PSC_ENETWR_CTL);
    310 	if (status & 0x2000) {
    311 		/* I think this is an exceptional condition. Reset the DMA */
    312 		mc_reset_txdma(sc);
    313 #ifdef MCDEBUG
    314 		printf("%s: resetting transmit DMA channel (status 0x%04x)\n",
    315 			sc->sc_dev.dv_xname, status);
    316 #endif
    317 	} else if (status & 0x100) {
    318 		/* Clear the interrupt and switch register sets */
    319 		psc_reg2(PSC_ENETWR_CMD + sc->sc_txseti) = 0x100;
    320 		sc->sc_txseti ^= 0x10;
    321 	}
    322 
    323 	return 1;
    324 }
    325 
    326 
    327 hide void
    328 mc_reset_rxdma(sc)
    329 	struct mc_softc *sc;
    330 {
    331 	u_int8_t maccc;
    332 
    333 	/* Disable receiver, reset the DMA channels */
    334 	maccc = NIC_GET(sc, MACE_MACCC);
    335 	NIC_PUT(sc, MACE_MACCC, maccc & ~ENRCV);
    336 	psc_reg2(PSC_ENETRD_CTL) = 0x8800;
    337 	mc_reset_rxdma_set(sc, 0);
    338 	psc_reg2(PSC_ENETRD_CTL) = 0x400;
    339 
    340 	psc_reg2(PSC_ENETRD_CTL) = 0x8800;
    341 	mc_reset_rxdma_set(sc, 0x10);
    342 	psc_reg2(PSC_ENETRD_CTL) = 0x400;
    343 
    344 	/* Reenable receiver, reenable DMA */
    345 	NIC_PUT(sc, MACE_MACCC, maccc);
    346 	sc->sc_rxset = 0;
    347 
    348 	psc_reg2(PSC_ENETRD_CMD + PSC_SET0) = 0x9800;
    349 	psc_reg2(PSC_ENETRD_CMD + PSC_SET1) = 0x9800;
    350 }
    351 
    352 hide void
    353 mc_reset_rxdma_set(sc, set)
    354 	struct mc_softc *sc;
    355 	int set;
    356 {
    357 	/* disable DMA while modifying the registers, then reenable DMA */
    358 	psc_reg2(PSC_ENETRD_CMD + set) = 0x0100;
    359 	psc_reg4(PSC_ENETRD_ADDR + set) = sc->sc_rxbuf_phys;
    360 	psc_reg4(PSC_ENETRD_LEN + set) = MC_RXDMABUFS;
    361 	psc_reg2(PSC_ENETRD_CMD + set) = 0x9800;
    362 	sc->sc_tail = 0;
    363 }
    364 
    365 hide void
    366 mc_reset_txdma(sc)
    367 	struct mc_softc *sc;
    368 {
    369 	u_int8_t maccc;
    370 
    371 	psc_reg2(PSC_ENETWR_CTL) = 0x8800;
    372 	maccc = NIC_GET(sc, MACE_MACCC);
    373 	NIC_PUT(sc, MACE_MACCC, maccc & ~ENXMT);
    374 	sc->sc_txset = sc->sc_txseti = 0;
    375 	psc_reg2(PSC_ENETWR_CTL) = 0x400;
    376 	NIC_PUT(sc, MACE_MACCC, maccc);
    377 }
    378 
    379 hide int
    380 mc_obio_getaddr(sc, lladdr)
    381 	struct mc_softc *sc;
    382 	u_int8_t *lladdr;
    383 {
    384 	bus_space_handle_t bsh;
    385 	u_char csum;
    386 
    387 	if (bus_space_map(sc->sc_regt, MACE_PROM_BASE, 8*16, 0, &bsh)) {
    388 		printf(": failed to map space to read MACE address.\n%s",
    389 		    sc->sc_dev.dv_xname);
    390 		return (-1);
    391 	}
    392 
    393 	if (!bus_probe(sc->sc_regt, bsh, 0, 1)) {
    394 		bus_space_unmap(sc->sc_regt, bsh, 8*16);
    395 		return (-1);
    396 	}
    397 
    398 	csum = mc_get_enaddr(sc->sc_regt, bsh, 1, lladdr);
    399 	if (csum != 0xff)
    400 		printf(": ethernet PROM checksum failed (0x%x != 0xff)\n%s",
    401 		    (int)csum, sc->sc_dev.dv_xname);
    402 
    403 	bus_space_unmap(sc->sc_regt, bsh, 8*16);
    404 
    405 	return (csum == 0xff ? 0 : -1);
    406 }
    407