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