Home | History | Annotate | Line # | Download | only in hpc
hpcdma.c revision 1.6.2.1
      1  1.6.2.1    skrll /*	$NetBSD: hpcdma.c,v 1.6.2.1 2004/08/03 10:40:06 skrll Exp $	*/
      2      1.1      wdk 
      3      1.1      wdk /*
      4      1.1      wdk  * Copyright (c) 2001 Wayne Knowles
      5      1.1      wdk  * All rights reserved.
      6      1.1      wdk  *
      7      1.1      wdk  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1      wdk  * by Wayne Knowles
      9      1.1      wdk  *
     10      1.1      wdk  * Redistribution and use in source and binary forms, with or without
     11      1.1      wdk  * modification, are permitted provided that the following conditions
     12      1.1      wdk  * are met:
     13      1.1      wdk  * 1. Redistributions of source code must retain the above copyright
     14      1.1      wdk  *    notice, this list of conditions and the following disclaimer.
     15      1.1      wdk  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1      wdk  *    notice, this list of conditions and the following disclaimer in the
     17      1.1      wdk  *    documentation and/or other materials provided with the distribution.
     18      1.1      wdk  * 3. All advertising materials mentioning features or use of this software
     19      1.1      wdk  *    must display the following acknowledgement:
     20      1.1      wdk  *        This product includes software developed by the NetBSD
     21      1.1      wdk  *        Foundation, Inc. and its contributors.
     22      1.1      wdk  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.1      wdk  *    contributors may be used to endorse or promote products derived
     24      1.1      wdk  *    from this software without specific prior written permission.
     25      1.1      wdk  *
     26      1.1      wdk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.1      wdk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.1      wdk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.1      wdk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.1      wdk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.1      wdk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.1      wdk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.1      wdk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.1      wdk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.1      wdk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.1      wdk  * POSSIBILITY OF SUCH DAMAGE.
     37      1.1      wdk  */
     38      1.1      wdk 
     39      1.3  thorpej /*
     40      1.3  thorpej  * Support for SCSI DMA provided by the HPC.
     41      1.3  thorpej  *
     42      1.3  thorpej  * Note: We use SCSI0 offsets, etc. here.  Since the layout of SCSI0
     43      1.3  thorpej  * and SCSI1 are the same, this is no problem.
     44      1.3  thorpej  */
     45      1.3  thorpej 
     46  1.6.2.1    skrll #include <sys/cdefs.h>
     47  1.6.2.1    skrll __KERNEL_RCSID(0, "$NetBSD: hpcdma.c,v 1.6.2.1 2004/08/03 10:40:06 skrll Exp $");
     48  1.6.2.1    skrll 
     49      1.1      wdk #include <sys/param.h>
     50      1.1      wdk #include <sys/systm.h>
     51      1.1      wdk #include <sys/device.h>
     52      1.1      wdk #include <sys/buf.h>
     53      1.1      wdk 
     54      1.6  thorpej #include <uvm/uvm_extern.h>
     55      1.6  thorpej 
     56      1.1      wdk #include <machine/bus.h>
     57      1.1      wdk 
     58      1.1      wdk #include <sgimips/hpc/hpcvar.h>
     59      1.1      wdk #include <sgimips/hpc/hpcreg.h>
     60      1.1      wdk #include <sgimips/hpc/hpcdma.h>
     61      1.1      wdk 
     62      1.1      wdk /*
     63      1.1      wdk  * Allocate DMA Chain descriptor list
     64      1.1      wdk  */
     65      1.1      wdk void
     66      1.3  thorpej hpcdma_init(struct hpc_attach_args *haa, struct hpc_dma_softc *sc, int ndesc)
     67      1.1      wdk {
     68      1.1      wdk 	bus_dma_segment_t seg;
     69      1.1      wdk 	int rseg, allocsz;
     70      1.1      wdk 
     71      1.3  thorpej 	sc->sc_bst = haa->ha_st;
     72      1.1      wdk 	sc->sc_dmat = haa->ha_dmat;
     73      1.1      wdk 	sc->sc_ndesc = ndesc;
     74      1.1      wdk 	sc->sc_flags = 0;
     75      1.1      wdk 
     76      1.3  thorpej 	if (bus_space_subregion(haa->ha_st, haa->ha_sh, haa->ha_dmaoff,
     77  1.6.2.1    skrll 	    sc->hpc->scsi0_regs_size, &sc->sc_bsh) != 0) {
     78      1.3  thorpej 		printf(": can't map DMA registers\n");
     79      1.3  thorpej 		return;
     80      1.3  thorpej 	}
     81      1.3  thorpej 
     82      1.1      wdk 	/* Alloc 1 additional descriptor - needed for DMA bug fix */
     83      1.1      wdk 	allocsz = sizeof(struct hpc_dma_desc) * (ndesc + 1);
     84      1.6  thorpej 	KASSERT(allocsz <= PAGE_SIZE);
     85      1.1      wdk 
     86      1.6  thorpej 	if (bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1 /*seg*/,
     87      1.6  thorpej 			      PAGE_SIZE, 0, BUS_DMA_WAITOK,
     88      1.1      wdk 			      &sc->sc_dmamap) != 0) {
     89      1.1      wdk 		printf(": failed to create dmamap\n");
     90      1.1      wdk 		return;
     91      1.1      wdk 	}
     92      1.1      wdk 
     93      1.1      wdk 	/*
     94      1.1      wdk 	 * Allocate a block of memory for dma chaining pointers
     95      1.1      wdk 	 */
     96      1.1      wdk 	if (bus_dmamem_alloc(sc->sc_dmat, allocsz, 0, 0,
     97      1.1      wdk 			     &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
     98      1.1      wdk 		printf(": can't allocate sglist\n");
     99      1.1      wdk 		return;
    100      1.1      wdk 	}
    101      1.1      wdk 	/* Map pages into kernel memory */
    102      1.1      wdk 	if (bus_dmamem_map(sc->sc_dmat, &seg, rseg, allocsz,
    103      1.5  thorpej 			   (caddr_t *)&sc->sc_desc_kva, BUS_DMA_NOWAIT)) {
    104      1.1      wdk 		printf(": can't map sglist\n");
    105      1.1      wdk 		bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    106      1.1      wdk 		return;
    107      1.1      wdk 	}
    108      1.1      wdk 
    109      1.5  thorpej 	if (bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap, sc->sc_desc_kva,
    110      1.1      wdk 			    allocsz, NULL, BUS_DMA_NOWAIT)) {
    111      1.1      wdk 		printf(": can't load sglist\n");
    112      1.1      wdk 		return;
    113      1.1      wdk 	}
    114      1.1      wdk 
    115      1.1      wdk 	sc->sc_desc_pa = (void *) sc->sc_dmamap->dm_segs[0].ds_addr;
    116      1.1      wdk }
    117      1.1      wdk 
    118      1.1      wdk 
    119      1.1      wdk void
    120      1.3  thorpej hpcdma_sglist_create(struct hpc_dma_softc *sc, bus_dmamap_t dmamap)
    121      1.1      wdk {
    122      1.1      wdk 	struct hpc_dma_desc *hva, *hpa;
    123      1.4   simonb 	bus_dma_segment_t *segp;
    124      1.1      wdk 	int i;
    125      1.1      wdk 
    126      1.1      wdk 	KASSERT(dmamap->dm_nsegs <= sc->sc_ndesc);
    127      1.1      wdk 
    128      1.1      wdk 	hva  = sc->sc_desc_kva;
    129      1.1      wdk 	hpa  = sc->sc_desc_pa;
    130      1.1      wdk 	segp = dmamap->dm_segs;
    131      1.1      wdk 
    132      1.1      wdk #ifdef DMA_DEBUG
    133      1.1      wdk 	printf("DMA_SGLIST<");
    134      1.1      wdk #endif
    135      1.1      wdk 	for (i = dmamap->dm_nsegs; i; i--) {
    136      1.1      wdk #ifdef DMA_DEBUG
    137      1.1      wdk 		printf("%p:%ld, ", (void *)segp->ds_addr, segp->ds_len);
    138      1.1      wdk #endif
    139  1.6.2.1    skrll 		if (sc->hpc->revision == 3) {
    140  1.6.2.1    skrll 			hva->hpc3_hdd_bufptr = segp->ds_addr;
    141  1.6.2.1    skrll 			hva->hpc3_hdd_ctl    = segp->ds_len;
    142  1.6.2.1    skrll 			hva->hdd_descptr = (u_int32_t) ++hpa;
    143  1.6.2.1    skrll 		} else /* HPC 1/1.5 */ {
    144  1.6.2.1    skrll 			/* there doesn't seem to be any good way of doing this
    145  1.6.2.1    skrll 		   	   via an abstraction layer */
    146  1.6.2.1    skrll 			hva->hpc1_hdd_bufptr = segp->ds_addr;
    147  1.6.2.1    skrll 			hva->hpc1_hdd_ctl    = segp->ds_len;
    148  1.6.2.1    skrll 			hva->hdd_descptr = (u_int32_t) ++hpa;
    149  1.6.2.1    skrll 		}
    150      1.1      wdk 		++hva; ++segp;
    151      1.1      wdk 	}
    152  1.6.2.1    skrll 
    153      1.1      wdk 	/* Work around HPC3 DMA bug */
    154  1.6.2.1    skrll 	if (sc->hpc->revision == 3)
    155  1.6.2.1    skrll 	{
    156  1.6.2.1    skrll 		hva->hpc3_hdd_bufptr  = 0;
    157  1.6.2.1    skrll 		hva->hpc3_hdd_ctl     = HDD_CTL_EOCHAIN;
    158  1.6.2.1    skrll 		hva->hdd_descptr = 0;
    159  1.6.2.1    skrll 		hva++;
    160  1.6.2.1    skrll 	} else {
    161  1.6.2.1    skrll 		hva--;
    162  1.6.2.1    skrll 		hva->hpc1_hdd_bufptr |= HPC1_HDD_CTL_EOCHAIN;
    163  1.6.2.1    skrll 		hva->hdd_descptr = 0;
    164  1.6.2.1    skrll 	}
    165  1.6.2.1    skrll 
    166      1.1      wdk #ifdef DMA_DEBUG
    167      1.1      wdk 	printf(">\n");
    168      1.1      wdk #endif
    169      1.1      wdk 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
    170      1.1      wdk 	    0, sc->sc_dmamap->dm_mapsize,
    171      1.1      wdk 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    172      1.1      wdk 
    173      1.1      wdk 	/* Load DMA Descriptor list */
    174  1.6.2.1    skrll 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, sc->hpc->scsi0_ndbp,
    175      1.1      wdk 			    (u_int32_t)sc->sc_desc_pa);
    176      1.1      wdk }
    177      1.1      wdk 
    178      1.1      wdk void
    179      1.3  thorpej hpcdma_cntl(struct hpc_dma_softc *sc, uint32_t mode)
    180      1.1      wdk {
    181      1.3  thorpej 
    182  1.6.2.1    skrll 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, sc->hpc->scsi0_ctl, mode);
    183      1.2  thorpej }
    184      1.2  thorpej 
    185      1.2  thorpej void
    186      1.3  thorpej hpcdma_reset(struct hpc_dma_softc *sc)
    187      1.2  thorpej {
    188      1.2  thorpej 
    189  1.6.2.1    skrll 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, sc->hpc->scsi0_ctl,
    190  1.6.2.1    skrll 	    sc->hpc->scsi_dmactl_reset);
    191      1.2  thorpej 	delay(100);
    192  1.6.2.1    skrll 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, sc->hpc->scsi0_ctl, 0);
    193      1.2  thorpej 	delay(1000);
    194      1.1      wdk }
    195      1.1      wdk 
    196      1.1      wdk void
    197      1.3  thorpej hpcdma_flush(struct hpc_dma_softc *sc)
    198      1.1      wdk {
    199      1.1      wdk 	u_int32_t	mode;
    200      1.1      wdk 
    201  1.6.2.1    skrll 	mode = bus_space_read_4(sc->sc_bst, sc->sc_bsh, sc->hpc->scsi0_ctl);
    202  1.6.2.1    skrll 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, sc->hpc->scsi0_ctl,
    203  1.6.2.1    skrll 	    			mode | sc->hpc->scsi_dmactl_flush);
    204      1.1      wdk 
    205      1.1      wdk 	/* Wait for Active bit to drop */
    206  1.6.2.1    skrll 	while (bus_space_read_4(sc->sc_bst, sc->sc_bsh, sc->hpc->scsi0_ctl) &
    207  1.6.2.1    skrll 	    sc->hpc->scsi_dmactl_active) {
    208  1.6.2.1    skrll 		bus_space_barrier(sc->sc_bst, sc->sc_bsh, sc->hpc->scsi0_ctl, 4,
    209      1.1      wdk 		    BUS_SPACE_BARRIER_READ);
    210      1.1      wdk 	}
    211      1.1      wdk }
    212