Home | History | Annotate | Line # | Download | only in obio
if_mc_obio.c revision 1.14
      1 /*	$NetBSD: if_mc_obio.c,v 1.14 2005/01/15 16:01:00 chs Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 David Huang <khym (at) azeotrope.org>
      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/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: if_mc_obio.c,v 1.14 2005/01/15 16:01:00 chs Exp $");
     40 
     41 #include "opt_ddb.h"
     42 
     43 #include <sys/param.h>
     44 #include <sys/device.h>
     45 #include <sys/malloc.h>
     46 #include <sys/socket.h>
     47 #include <sys/systm.h>
     48 
     49 #include <net/if.h>
     50 #include <net/if_ether.h>
     51 
     52 #include <uvm/uvm_extern.h>
     53 
     54 #include <machine/bus.h>
     55 #include <machine/psc.h>
     56 
     57 #include <mac68k/obio/obiovar.h>
     58 #include <mac68k/dev/if_mcreg.h>
     59 #include <mac68k/dev/if_mcvar.h>
     60 
     61 #define MACE_REG_BASE	0x50F1C000
     62 #define MACE_PROM_BASE	0x50F08000
     63 
     64 hide int	mc_obio_match(struct device *, struct cfdata *, void *);
     65 hide void	mc_obio_attach(struct device *, struct device *, void *);
     66 hide void	mc_obio_init(struct mc_softc *);
     67 hide void	mc_obio_put(struct mc_softc *, u_int);
     68 hide int	mc_dmaintr(void *);
     69 hide void	mc_reset_rxdma(struct mc_softc *);
     70 hide void	mc_reset_rxdma_set(struct mc_softc *, int);
     71 hide void	mc_reset_txdma(struct mc_softc *);
     72 hide int	mc_obio_getaddr(struct mc_softc *, u_int8_t *);
     73 
     74 CFATTACH_DECL(mc_obio, sizeof(struct mc_softc),
     75     mc_obio_match, mc_obio_attach, NULL, NULL);
     76 
     77 hide int
     78 mc_obio_match(struct device *parent, struct cfdata *cf, void *aux)
     79 {
     80 	struct obio_attach_args *oa = aux;
     81 	bus_space_handle_t bsh;
     82 	int found = 0;
     83 
     84         if (current_mac_model->class != MACH_CLASSAV)
     85 		return 0;
     86 
     87 	if (bus_space_map(oa->oa_tag, MACE_REG_BASE, MC_REGSIZE, 0, &bsh))
     88 		return 0;
     89 
     90 	/*
     91 	 * Make sure the MACE's I/O space is readable, and if it is,
     92 	 * try to read the CHIPID register. A MACE will always have
     93 	 * 0x?940, where the ? depends on the chip version.
     94 	 */
     95 	if (mac68k_bus_space_probe(oa->oa_tag, bsh, 0, 1)) {
     96 		if ((bus_space_read_1(
     97 			oa->oa_tag, bsh, MACE_REG(MACE_CHIPIDL)) == 0x40) &&
     98 		    ((bus_space_read_1(
     99 			oa->oa_tag, bsh, MACE_REG(MACE_CHIPIDH)) & 0xf) == 9))
    100 			found = 1;
    101 	}
    102 
    103 	bus_space_unmap(oa->oa_tag, bsh, MC_REGSIZE);
    104 
    105 	return found;
    106 }
    107 
    108 hide void
    109 mc_obio_attach(struct device *parent, struct device *self, void *aux)
    110 {
    111 	struct obio_attach_args *oa = (struct obio_attach_args *)aux;
    112 	struct mc_softc *sc = (void *)self;
    113 	u_int8_t myaddr[ETHER_ADDR_LEN];
    114 	int i, noncontig = 0;
    115 
    116 	sc->sc_regt = oa->oa_tag;
    117 	sc->sc_biucc = XMTSP_64;
    118 	sc->sc_fifocc = XMTFW_16 | RCVFW_64 | XMTFWU | RCVFWU |
    119 	    XMTBRST | RCVBRST;
    120 	sc->sc_plscc = PORTSEL_AUI;
    121 
    122 	if (bus_space_map(sc->sc_regt, MACE_REG_BASE, MC_REGSIZE, 0,
    123 	    &sc->sc_regh)) {
    124 		printf(": failed to map space for MACE regs.\n");
    125 		return;
    126 	}
    127 
    128 	if (mc_obio_getaddr(sc, myaddr)) {
    129 		printf(": failed to get MAC address.\n");
    130 		return;
    131 	}
    132 
    133 	/* allocate memory for transmit buffer and mark it non-cacheable */
    134 	sc->sc_txbuf = malloc(PAGE_SIZE, M_DEVBUF, M_WAITOK);
    135 	sc->sc_txbuf_phys = kvtop(sc->sc_txbuf);
    136 	physaccess (sc->sc_txbuf, (caddr_t)sc->sc_txbuf_phys, PAGE_SIZE,
    137 	    PG_V | PG_RW | PG_CI);
    138 
    139 	/*
    140 	 * allocate memory for receive buffer and mark it non-cacheable
    141 	 * XXX This should use the bus_dma interface, since the buffer
    142 	 * needs to be physically contiguous. However, it seems that
    143 	 * at least on my system, malloc() does allocate contiguous
    144 	 * memory. If it's not, suggest reducing the number of buffers
    145 	 * to 2, which will fit in one 4K page.
    146 	 */
    147 	sc->sc_rxbuf = malloc(MC_NPAGES * PAGE_SIZE, M_DEVBUF, M_WAITOK);
    148 	sc->sc_rxbuf_phys = kvtop(sc->sc_rxbuf);
    149 	for (i = 0; i < MC_NPAGES; i++) {
    150 		int pa;
    151 
    152 		pa = kvtop(sc->sc_rxbuf + PAGE_SIZE*i);
    153 		physaccess (sc->sc_rxbuf + PAGE_SIZE*i, (caddr_t)pa, PAGE_SIZE,
    154 		    PG_V | PG_RW | PG_CI);
    155 		if (pa != sc->sc_rxbuf_phys + PAGE_SIZE*i)
    156 			noncontig = 1;
    157 	}
    158 
    159 	if (noncontig) {
    160 		printf("%s: receive DMA buffer not contiguous! "
    161 		    "Try compiling with \"options MC_RXDMABUFS=2\"\n",
    162 		    sc->sc_dev.dv_xname);
    163 		return;
    164 	}
    165 
    166 	sc->sc_bus_init = mc_obio_init;
    167 	sc->sc_putpacket = mc_obio_put;
    168 
    169 	/* disable receive DMA */
    170 	psc_reg2(PSC_ENETRD_CTL) = 0x8800;
    171 	psc_reg2(PSC_ENETRD_CTL) = 0x1000;
    172 	psc_reg2(PSC_ENETRD_CMD + PSC_SET0) = 0x1100;
    173 	psc_reg2(PSC_ENETRD_CMD + PSC_SET1) = 0x1100;
    174 
    175 	/* disable transmit DMA */
    176 	psc_reg2(PSC_ENETWR_CTL) = 0x8800;
    177 	psc_reg2(PSC_ENETWR_CTL) = 0x1000;
    178 	psc_reg2(PSC_ENETWR_CMD + PSC_SET0) = 0x1100;
    179 	psc_reg2(PSC_ENETWR_CMD + PSC_SET1) = 0x1100;
    180 
    181 	/* install interrupt handlers */
    182 	add_psc_lev4_intr(PSCINTR_ENET_DMA, mc_dmaintr, sc);
    183 	add_psc_lev3_intr(mcintr, sc);
    184 
    185 	/* enable MACE DMA interrupts */
    186 	psc_reg1(PSC_LEV4_IER) = 0x80 | (1 << PSCINTR_ENET_DMA);
    187 
    188 	/* don't know what this does */
    189 	psc_reg2(PSC_ENETWR_CTL) = 0x9000;
    190 	psc_reg2(PSC_ENETRD_CTL) = 0x9000;
    191 	psc_reg2(PSC_ENETWR_CTL) = 0x0400;
    192 	psc_reg2(PSC_ENETRD_CTL) = 0x0400;
    193 
    194 	/* enable MACE interrupts */
    195 	psc_reg1(PSC_LEV3_IER) = 0x80 | (1 << PSCINTR_ENET);
    196 
    197 	/* mcsetup returns 1 if something fails */
    198 	if (mcsetup(sc, myaddr)) {
    199 		/* disable interrupts */
    200 		psc_reg1(PSC_LEV4_IER) = (1 << PSCINTR_ENET_DMA);
    201 		psc_reg1(PSC_LEV3_IER) = (1 << PSCINTR_ENET);
    202 		/* remove interrupt handlers */
    203 		remove_psc_lev4_intr(PSCINTR_ENET_DMA);
    204 		remove_psc_lev3_intr();
    205 
    206 		bus_space_unmap(sc->sc_regt, sc->sc_regh, MC_REGSIZE);
    207 		return;
    208 	}
    209 }
    210 
    211 /* Bus-specific initialization */
    212 hide void
    213 mc_obio_init(struct mc_softc *sc)
    214 {
    215 	mc_reset_rxdma(sc);
    216 	mc_reset_txdma(sc);
    217 }
    218 
    219 hide void
    220 mc_obio_put(struct mc_softc *sc, u_int len)
    221 {
    222 	psc_reg4(PSC_ENETWR_ADDR + sc->sc_txset) = sc->sc_txbuf_phys;
    223 	psc_reg4(PSC_ENETWR_LEN + sc->sc_txset) = len;
    224 	psc_reg2(PSC_ENETWR_CMD + sc->sc_txset) = 0x9800;
    225 
    226 	sc->sc_txset ^= 0x10;
    227 }
    228 
    229 /*
    230  * Interrupt handler for the MACE DMA completion interrupts
    231  */
    232 int
    233 mc_dmaintr(void *arg)
    234 {
    235 	struct mc_softc *sc = arg;
    236 	u_int16_t status;
    237 	u_int32_t bufsleft, which;
    238 	int head;
    239 
    240 	/*
    241 	 * Not sure what this does... figure out if this interrupt is
    242 	 * really ours?
    243 	 */
    244 	while ((which = psc_reg4(0x804)) != psc_reg4(0x804))
    245 		;
    246 	if ((which & 0x60000000) == 0)
    247 		return 0;
    248 
    249 	/* Get the read channel status */
    250 	status = psc_reg2(PSC_ENETRD_CTL);
    251 	if (status & 0x2000) {
    252 		/* I think this is an exceptional condition. Reset the DMA */
    253 		mc_reset_rxdma(sc);
    254 #ifdef MCDEBUG
    255 		printf("%s: resetting receive DMA channel (status 0x%04x)\n",
    256 		    sc->sc_dev.dv_xname, status);
    257 #endif
    258 	} else if (status & 0x100) {
    259 		/* We've received some packets from the MACE */
    260 		int offset;
    261 
    262 		/* Clear the interrupt */
    263 		psc_reg2(PSC_ENETRD_CMD + sc->sc_rxset) = 0x1100;
    264 
    265 		/* See how may receive buffers are left */
    266 		bufsleft = psc_reg4(PSC_ENETRD_LEN + sc->sc_rxset);
    267 		head = MC_RXDMABUFS - bufsleft;
    268 
    269 #if 0 /* I don't think this should ever happen */
    270 		if (head == sc->sc_tail) {
    271 #ifdef MCDEBUG
    272 			printf("%s: head == tail: suspending DMA?\n",
    273 			    sc->sc_dev.dv_xname);
    274 #endif
    275 			psc_reg2(PSC_ENETRD_CMD + sc->sc_rxset) = 0x9000;
    276 		}
    277 #endif
    278 
    279 		/* Loop through, processing each of the packets */
    280 		for (; sc->sc_tail < head; sc->sc_tail++) {
    281 			offset = sc->sc_tail * 0x800;
    282 			sc->sc_rxframe.rx_rcvcnt = sc->sc_rxbuf[offset];
    283 			sc->sc_rxframe.rx_rcvsts = sc->sc_rxbuf[offset+2];
    284 			sc->sc_rxframe.rx_rntpc = sc->sc_rxbuf[offset+4];
    285 			sc->sc_rxframe.rx_rcvcc = sc->sc_rxbuf[offset+6];
    286 			sc->sc_rxframe.rx_frame = sc->sc_rxbuf + offset + 16;
    287 
    288 			mc_rint(sc);
    289 		}
    290 
    291 		/*
    292 		 * If we're out of buffers, reset this register set
    293 		 * and switch to the other one. Otherwise, reactivate
    294 		 * this set.
    295 		 */
    296 		if (bufsleft == 0) {
    297 			mc_reset_rxdma_set(sc, sc->sc_rxset);
    298 			sc->sc_rxset ^= 0x10;
    299 		} else
    300 			psc_reg2(PSC_ENETRD_CMD + sc->sc_rxset) = 0x9800;
    301 	}
    302 
    303 	/* Get the write channel status */
    304 	status = psc_reg2(PSC_ENETWR_CTL);
    305 	if (status & 0x2000) {
    306 		/* I think this is an exceptional condition. Reset the DMA */
    307 		mc_reset_txdma(sc);
    308 #ifdef MCDEBUG
    309 		printf("%s: resetting transmit DMA channel (status 0x%04x)\n",
    310 			sc->sc_dev.dv_xname, status);
    311 #endif
    312 	} else if (status & 0x100) {
    313 		/* Clear the interrupt and switch register sets */
    314 		psc_reg2(PSC_ENETWR_CMD + sc->sc_txseti) = 0x100;
    315 		sc->sc_txseti ^= 0x10;
    316 	}
    317 
    318 	return 1;
    319 }
    320 
    321 
    322 hide void
    323 mc_reset_rxdma(struct mc_softc *sc)
    324 {
    325 	u_int8_t maccc;
    326 
    327 	/* Disable receiver, reset the DMA channels */
    328 	maccc = NIC_GET(sc, MACE_MACCC);
    329 	NIC_PUT(sc, MACE_MACCC, maccc & ~ENRCV);
    330 	psc_reg2(PSC_ENETRD_CTL) = 0x8800;
    331 	mc_reset_rxdma_set(sc, 0);
    332 	psc_reg2(PSC_ENETRD_CTL) = 0x400;
    333 
    334 	psc_reg2(PSC_ENETRD_CTL) = 0x8800;
    335 	mc_reset_rxdma_set(sc, 0x10);
    336 	psc_reg2(PSC_ENETRD_CTL) = 0x400;
    337 
    338 	/* Reenable receiver, reenable DMA */
    339 	NIC_PUT(sc, MACE_MACCC, maccc);
    340 	sc->sc_rxset = 0;
    341 
    342 	psc_reg2(PSC_ENETRD_CMD + PSC_SET0) = 0x9800;
    343 	psc_reg2(PSC_ENETRD_CMD + PSC_SET1) = 0x9800;
    344 }
    345 
    346 hide void
    347 mc_reset_rxdma_set(struct mc_softc *sc, int set)
    348 {
    349 	/* disable DMA while modifying the registers, then reenable DMA */
    350 	psc_reg2(PSC_ENETRD_CMD + set) = 0x0100;
    351 	psc_reg4(PSC_ENETRD_ADDR + set) = sc->sc_rxbuf_phys;
    352 	psc_reg4(PSC_ENETRD_LEN + set) = MC_RXDMABUFS;
    353 	psc_reg2(PSC_ENETRD_CMD + set) = 0x9800;
    354 	sc->sc_tail = 0;
    355 }
    356 
    357 hide void
    358 mc_reset_txdma(struct mc_softc *sc)
    359 {
    360 	u_int8_t maccc;
    361 
    362 	psc_reg2(PSC_ENETWR_CTL) = 0x8800;
    363 	maccc = NIC_GET(sc, MACE_MACCC);
    364 	NIC_PUT(sc, MACE_MACCC, maccc & ~ENXMT);
    365 	sc->sc_txset = sc->sc_txseti = 0;
    366 	psc_reg2(PSC_ENETWR_CTL) = 0x400;
    367 	NIC_PUT(sc, MACE_MACCC, maccc);
    368 }
    369 
    370 hide int
    371 mc_obio_getaddr(struct mc_softc *sc, u_int8_t *lladdr)
    372 {
    373 	bus_space_handle_t bsh;
    374 	u_char csum;
    375 
    376 	if (bus_space_map(sc->sc_regt, MACE_PROM_BASE, 8*16, 0, &bsh)) {
    377 		printf(": failed to map space to read MACE address.\n%s",
    378 		    sc->sc_dev.dv_xname);
    379 		return (-1);
    380 	}
    381 
    382 	if (!mac68k_bus_space_probe(sc->sc_regt, bsh, 0, 1)) {
    383 		bus_space_unmap(sc->sc_regt, bsh, 8*16);
    384 		return (-1);
    385 	}
    386 
    387 	csum = mc_get_enaddr(sc->sc_regt, bsh, 1, lladdr);
    388 	if (csum != 0xff)
    389 		printf(": ethernet PROM checksum failed (0x%x != 0xff)\n%s",
    390 		    (int)csum, sc->sc_dev.dv_xname);
    391 
    392 	bus_space_unmap(sc->sc_regt, bsh, 8*16);
    393 
    394 	return (csum == 0xff ? 0 : -1);
    395 }
    396