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