Home | History | Annotate | Line # | Download | only in isa
cs89x0isa.c revision 1.5
      1 /* $NetBSD: cs89x0isa.c,v 1.5 2003/02/02 10:24:40 wiz Exp $ */
      2 
      3 /*
      4  * Copyright 1997
      5  * Digital Equipment Corporation. All rights reserved.
      6  *
      7  * This software is furnished under license and may be used and
      8  * copied only in accordance with the following terms and conditions.
      9  * Subject to these conditions, you may download, copy, install,
     10  * use, modify and distribute this software in source and/or binary
     11  * form. No title or ownership is transferred hereby.
     12  *
     13  * 1) Any source code used, modified or distributed must reproduce
     14  *    and retain this copyright notice and list of conditions as
     15  *    they appear in the source file.
     16  *
     17  * 2) No right is granted to use any trade name, trademark, or logo of
     18  *    Digital Equipment Corporation. Neither the "Digital Equipment
     19  *    Corporation" name nor any trademark or logo of Digital Equipment
     20  *    Corporation may be used to endorse or promote products derived
     21  *    from this software without the prior written permission of
     22  *    Digital Equipment Corporation.
     23  *
     24  * 3) This software is provided "AS-IS" and any express or implied
     25  *    warranties, including but not limited to, any implied warranties
     26  *    of merchantability, fitness for a particular purpose, or
     27  *    non-infringement are disclaimed. In no event shall DIGITAL be
     28  *    liable for any damages whatsoever, and in particular, DIGITAL
     29  *    shall not be liable for special, indirect, consequential, or
     30  *    incidental damages or damages for lost profits, loss of
     31  *    revenue or loss of use, whether such damages arise in contract,
     32  *    negligence, tort, under statute, in equity, at law or otherwise,
     33  *    even if advised of the possibility of such damage.
     34  */
     35 
     36 /* isa dma routines for cs89x0 */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: cs89x0isa.c,v 1.5 2003/02/02 10:24:40 wiz Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/mbuf.h>
     44 #include <sys/socket.h>
     45 #include <sys/device.h>
     46 
     47 #include "rnd.h"
     48 #if NRND > 0
     49 #include <sys/rnd.h>
     50 #endif
     51 
     52 #include <net/if.h>
     53 #include <net/if_ether.h>
     54 #include <net/if_media.h>
     55 
     56 #include <machine/bus.h>
     57 
     58 #include <dev/isa/isareg.h>
     59 #include <dev/isa/isavar.h>
     60 #include <dev/isa/isadmavar.h>
     61 
     62 #include <dev/ic/cs89x0reg.h>
     63 #include <dev/ic/cs89x0var.h>
     64 #include <dev/isa/cs89x0isavar.h>
     65 
     66 #define DMA_STATUS_BITS 0x0007	/* bit masks for checking DMA status */
     67 #define DMA_STATUS_OK 0x0004
     68 
     69 void
     70 cs_isa_dma_attach(struct cs_softc *sc)
     71 {
     72 	struct cs_softc_isa *isc = (void *)sc;
     73 
     74 	if (isc->sc_drq == ISACF_DRQ_DEFAULT)
     75 		printf("%s: DMA channel unspecified, not using DMA\n",
     76 		    sc->sc_dev.dv_xname);
     77 	else if (isc->sc_drq < 5 || isc->sc_drq > 7)
     78 		printf("%s: invalid DMA channel, not using DMA\n",
     79 		    sc->sc_dev.dv_xname);
     80 	else {
     81 		bus_size_t maxsize;
     82 		bus_addr_t dma_addr;
     83 
     84 		maxsize = isa_dmamaxsize(isc->sc_ic, isc->sc_drq);
     85 		if (maxsize < CS8900_DMASIZE) {
     86 			printf("%s: max DMA size %lu is less than required %d\n",
     87 			    sc->sc_dev.dv_xname, (u_long)maxsize,
     88 			    CS8900_DMASIZE);
     89 			goto after_dma_block;
     90 		}
     91 
     92 		if (isa_dmamap_create(isc->sc_ic, isc->sc_drq,
     93 		    CS8900_DMASIZE, BUS_DMA_NOWAIT) != 0) {
     94 			printf("%s: unable to create ISA DMA map\n",
     95 			    sc->sc_dev.dv_xname);
     96 			goto after_dma_block;
     97 		}
     98 		if (isa_dmamem_alloc(isc->sc_ic, isc->sc_drq,
     99 		    CS8900_DMASIZE, &dma_addr, BUS_DMA_NOWAIT) != 0) {
    100 			printf("%s: unable to allocate DMA buffer\n",
    101 			    sc->sc_dev.dv_xname);
    102 			goto after_dma_block;
    103 		}
    104 		if (isa_dmamem_map(isc->sc_ic, isc->sc_drq, dma_addr,
    105 		    CS8900_DMASIZE, &isc->sc_dmabase,
    106 		       BUS_DMA_NOWAIT | BUS_DMA_COHERENT /* XXX */ ) != 0) {
    107 			printf("%s: unable to map DMA buffer\n",
    108 			    sc->sc_dev.dv_xname);
    109 			isa_dmamem_free(isc->sc_ic, isc->sc_drq, dma_addr,
    110 			    CS8900_DMASIZE);
    111 			goto after_dma_block;
    112 		}
    113 
    114 		isc->sc_dmasize = CS8900_DMASIZE;
    115 		sc->sc_cfgflags |= CFGFLG_DMA_MODE;
    116 		isc->sc_dmaaddr = dma_addr;
    117 after_dma_block:
    118 		;
    119 	}
    120 }
    121 
    122 void cs_isa_dma_chipinit(struct cs_softc *sc)
    123 {
    124 	struct cs_softc_isa *isc = (void *)sc;
    125 
    126 	if (sc->sc_cfgflags & CFGFLG_DMA_MODE) {
    127 		/*
    128 		 * First we program the DMA controller and ensure the memory
    129 		 * buffer is valid. If it isn't then we just go on without
    130 		 * DMA.
    131 		 */
    132 		if (isa_dmastart(isc->sc_ic, isc->sc_drq, isc->sc_dmabase,
    133 		    isc->sc_dmasize, NULL, DMAMODE_READ | DMAMODE_LOOPDEMAND,
    134 		    BUS_DMA_NOWAIT)) {
    135 			/* XXX XXX XXX */
    136 			panic("%s: unable to start DMA", sc->sc_dev.dv_xname);
    137 		}
    138 		isc->sc_dmacur = isc->sc_dmabase;
    139 
    140 		/* interrupt when a DMA'd frame is received */
    141 		CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG,
    142 		    RX_CFG_ALL_IE | RX_CFG_RX_DMA_ONLY);
    143 
    144 		/*
    145 		 * set the DMA burst bit so we don't tie up the bus for too
    146 		 * long.
    147 		 */
    148 		if (isc->sc_dmasize == 16384) {
    149 			CS_WRITE_PACKET_PAGE(sc, PKTPG_BUS_CTL,
    150 			    ((CS_READ_PACKET_PAGE(sc, PKTPG_BUS_CTL) &
    151 			     ~BUS_CTL_DMA_SIZE) | BUS_CTL_DMA_BURST));
    152 		} else { /* use 64K */
    153 			CS_WRITE_PACKET_PAGE(sc, PKTPG_BUS_CTL,
    154 			    CS_READ_PACKET_PAGE(sc, PKTPG_BUS_CTL) |
    155 			     BUS_CTL_DMA_SIZE | BUS_CTL_DMA_BURST);
    156 		}
    157 
    158 		CS_WRITE_PACKET_PAGE(sc, PKTPG_DMA_CHANNEL, isc->sc_drq - 5);
    159 	}
    160 }
    161 
    162 void cs_process_rx_dma(struct cs_softc *sc)
    163 {
    164 	struct cs_softc_isa *isc = (void *)sc;
    165 	struct ifnet *ifp;
    166 	u_int16_t num_dma_frames;
    167 	u_int16_t pkt_length;
    168 	u_int16_t status;
    169 	u_int to_copy;
    170 	char *dma_mem_ptr;
    171 	struct mbuf *m;
    172 	u_char *pBuff;
    173 	int pad;
    174 
    175 	/* initialise the pointers */
    176 	ifp = &sc->sc_ethercom.ec_if;
    177 
    178 	/* Read the number of frames DMAed. */
    179 	num_dma_frames = CS_READ_PACKET_PAGE(sc, PKTPG_DMA_FRAME_COUNT);
    180 	num_dma_frames &= (u_int16_t) (0x0fff);
    181 
    182 	/*
    183 	 * Loop till number of DMA frames ready to read is zero. After
    184 	 * reading the frame out of memory we must check if any have been
    185 	 * received while we were processing
    186 	 */
    187 	while (num_dma_frames != 0) {
    188 		dma_mem_ptr = isc->sc_dmacur;
    189 
    190 		/*
    191 		 * process all of the dma frames in memory
    192 		 *
    193 		 * This loop relies on the dma_mem_ptr variable being set to the
    194 		 * next frames start address.
    195 		 */
    196 		for (; num_dma_frames > 0; num_dma_frames--) {
    197 
    198 			/*
    199 			 * Get the length and status of the packet. Only the
    200 			 * status is guaranteed to be at dma_mem_ptr, ie need
    201 			 * to check for wraparound before reading the length
    202 			 */
    203 			status = *((unsigned short *) dma_mem_ptr)++;
    204 			if (dma_mem_ptr > (isc->sc_dmabase + isc->sc_dmasize)) {
    205 				dma_mem_ptr = isc->sc_dmabase;
    206 			}
    207 			pkt_length = *((unsigned short *) dma_mem_ptr)++;
    208 
    209 			/* Do some sanity checks on the length and status. */
    210 			if ((pkt_length > ETHER_MAX_LEN) ||
    211 			    ((status & DMA_STATUS_BITS) != DMA_STATUS_OK)) {
    212 				/*
    213 				 * the SCO driver kills the adapter in this
    214 				 * situation
    215 				 */
    216 				/*
    217 				 * should increment the error count and reset
    218 				 * the dma operation.
    219 				 */
    220 				printf("%s: cs_process_rx_dma: DMA buffer out of sync about to reset\n",
    221 				    sc->sc_dev.dv_xname);
    222 				ifp->if_ierrors++;
    223 
    224 				/* skip the rest of the DMA buffer */
    225 				isa_dmaabort(isc->sc_ic, isc->sc_drq);
    226 
    227 				/* now reset the chip and reinitialise */
    228 				cs_init(&sc->sc_ethercom.ec_if);
    229 				return;
    230 			}
    231 			/* Check the status of the received packet. */
    232 			if (status & RX_EVENT_RX_OK) {
    233 				/* get a new mbuf */
    234 				MGETHDR(m, M_DONTWAIT, MT_DATA);
    235 				if (m == 0) {
    236 					printf("%s: cs_process_rx_dma: unable to allocate mbuf\n",
    237 					    sc->sc_dev.dv_xname);
    238 					ifp->if_ierrors++;
    239 					/*
    240 					 * couldn't allocate an mbuf so
    241 					 * things are not good, may as well
    242 					 * drop all the packets I think.
    243 					 */
    244 					CS_READ_PACKET_PAGE(sc,
    245 					    PKTPG_DMA_FRAME_COUNT);
    246 
    247 					/* now reset DMA operation */
    248 					isa_dmaabort(isc->sc_ic, isc->sc_drq);
    249 
    250 					/*
    251 					 * now reset the chip and
    252 					 * reinitialise
    253 					 */
    254 					cs_init(&sc->sc_ethercom.ec_if);
    255 					return;
    256 				}
    257 				/*
    258 				 * save processing by always using a mbuf
    259 				 * cluster, guaranteed to fit packet
    260 				 */
    261 				MCLGET(m, M_DONTWAIT);
    262 				if ((m->m_flags & M_EXT) == 0) {
    263 					/* couldn't allocate an mbuf cluster */
    264 					printf("%s: cs_process_rx_dma: unable to allocate a cluster\n",
    265 					    sc->sc_dev.dv_xname);
    266 					m_freem(m);
    267 
    268 					/* skip the frame */
    269 					CS_READ_PACKET_PAGE(sc, PKTPG_DMA_FRAME_COUNT);
    270 					isa_dmaabort(isc->sc_ic, isc->sc_drq);
    271 
    272 					/*
    273 					 * now reset the chip and
    274 					 * reinitialise
    275 					 */
    276 					cs_init(&sc->sc_ethercom.ec_if);
    277 					return;
    278 				}
    279 				m->m_pkthdr.rcvif = ifp;
    280 				m->m_pkthdr.len = pkt_length;
    281 				m->m_len = pkt_length;
    282 
    283 				/*
    284 				 * align ip header on word boundary for
    285 				 * ipintr
    286 				 */
    287 				pad = ALIGN(sizeof(struct ether_header)) -
    288 				    sizeof(struct ether_header);
    289 				m->m_data += pad;
    290 
    291 				/*
    292 				 * set up the buffer pointer to point to the
    293 				 * data area
    294 				 */
    295 				pBuff = mtod(m, char *);
    296 
    297 				/*
    298 				 * Read the frame into free_pktbuf
    299 				 * The buffer is circular buffer, either
    300 				 * 16K or 64K in length.
    301 				 *
    302 				 * need to check where the end of the buffer
    303 				 * is and go back to the start.
    304 				 */
    305 				if ((dma_mem_ptr + pkt_length) <
    306 				    (isc->sc_dmabase + isc->sc_dmasize)) {
    307 					/*
    308 					 * No wrap around. Copy the frame
    309 					 * header
    310 					 */
    311 					memcpy(pBuff, dma_mem_ptr, pkt_length);
    312 					dma_mem_ptr += pkt_length;
    313 				} else {
    314 					to_copy = (u_int)
    315 					    ((isc->sc_dmabase + isc->sc_dmasize) -
    316 					    dma_mem_ptr);
    317 
    318 					/* Copy the first half of the frame. */
    319 					memcpy(pBuff, dma_mem_ptr, to_copy);
    320 					pBuff += to_copy;
    321 
    322 					/*
    323 		                         * Rest of the frame is to be read
    324 		                         * from the first byte of the DMA
    325 		                         * memory.
    326 		                         */
    327 					/*
    328 					 * Get the number of bytes leftout in
    329 					 * the frame.
    330 					 */
    331 					to_copy = pkt_length - to_copy;
    332 
    333 					dma_mem_ptr = isc->sc_dmabase;
    334 
    335 					/* Copy rest of the frame. */
    336 					memcpy(pBuff, dma_mem_ptr, to_copy);
    337 					dma_mem_ptr += to_copy;
    338 				}
    339 
    340 				cs_ether_input(sc, m);
    341 			}
    342 			/* (status & RX_OK) */
    343 			else {
    344 				/* the frame was not received OK */
    345 				/* Increment the input error count */
    346 				ifp->if_ierrors++;
    347 
    348 				/*
    349 				 * If debugging is enabled then log error
    350 				 * messages if we got any.
    351 				 */
    352 				if ((ifp->if_flags & IFF_DEBUG) &&
    353 				    status != REG_NUM_RX_EVENT)
    354 					cs_print_rx_errors(sc, status);
    355 			}
    356 			/*
    357 			 * now update the current frame pointer. the
    358 			 * dma_mem_ptr should point to the next packet to be
    359 			 * received, without the alignment considerations.
    360 			 *
    361 			 * The cs8900 pads all frames to start at the next 32bit
    362 			 * aligned addres. hence we need to pad our offset
    363 			 * pointer.
    364 			 */
    365 			dma_mem_ptr += 3;
    366 			dma_mem_ptr = (char *)
    367 			    ((long) dma_mem_ptr & 0xfffffffc);
    368 			if (dma_mem_ptr < (isc->sc_dmabase + isc->sc_dmasize)) {
    369 				isc->sc_dmacur = dma_mem_ptr;
    370 			} else {
    371 				dma_mem_ptr = isc->sc_dmacur = isc->sc_dmabase;
    372 			}
    373 		} /* for all frames */
    374 		/* Read the number of frames DMAed again. */
    375 		num_dma_frames = CS_READ_PACKET_PAGE(sc, PKTPG_DMA_FRAME_COUNT);
    376 		num_dma_frames &= (u_int16_t) (0x0fff);
    377 	} /* while there are frames left */
    378 }
    379