Home | History | Annotate | Line # | Download | only in dev
wdc_obio.c revision 1.2
      1 /*	$NetBSD: wdc_obio.c,v 1.2 1999/01/22 10:44:38 tsubai Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum and by Onno van der Linden.
      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. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/device.h>
     42 #include <sys/malloc.h>
     43 
     44 #include <vm/vm.h>
     45 
     46 #include <machine/bus.h>
     47 #include <machine/autoconf.h>
     48 
     49 #include <dev/ata/atavar.h>
     50 #include <dev/ic/wdcvar.h>
     51 
     52 #include <macppc/dev/dbdma.h>
     53 
     54 #define WDC_REG_NPORTS		8
     55 #define WDC_AUXREG_OFFSET	0x16
     56 #define WDC_DEFAULT_PIO_IRQ	13	/* XXX */
     57 #define WDC_DEFAULT_DMA_IRQ	2	/* XXX */
     58 
     59 #define WDC_OPTIONS_DMA 0x01
     60 
     61 /*
     62  * XXX This code currently doesn't even try to allow 32-bit data port use.
     63  */
     64 
     65 struct wdc_obio_softc {
     66 	struct wdc_softc sc_wdcdev;
     67 	struct channel_softc *wdc_chanptr;
     68 	struct channel_softc wdc_channel;
     69 	dbdma_regmap_t *sc_dmareg;
     70 	dbdma_command_t	*sc_dmacmd;
     71 };
     72 
     73 int	wdc_obio_probe	__P((struct device *, struct cfdata *, void *));
     74 void	wdc_obio_attach	__P((struct device *, struct device *, void *));
     75 
     76 struct cfattach wdc_obio_ca = {
     77 	sizeof(struct wdc_obio_softc), wdc_obio_probe, wdc_obio_attach
     78 };
     79 
     80 static int	wdc_obio_dma_init __P((void *, int, int, void *, size_t, int));
     81 static void 	wdc_obio_dma_start __P((void *, int, int, int));
     82 static int	wdc_obio_dma_finish __P((void *, int, int, int));
     83 
     84 int
     85 wdc_obio_probe(parent, match, aux)
     86 	struct device *parent;
     87 	struct cfdata *match;
     88 	void *aux;
     89 {
     90 	struct confargs *ca = aux;
     91 
     92 	if (strcmp(ca->ca_name, "ATA") == 0 ||
     93 	    strcmp(ca->ca_name, "ata") == 0 ||
     94 	    strcmp(ca->ca_name, "ata0") == 0 ||
     95 	    strcmp(ca->ca_name, "ide") == 0)
     96 		return 1;
     97 
     98 	return 0;
     99 }
    100 
    101 void
    102 wdc_obio_attach(parent, self, aux)
    103 	struct device *parent, *self;
    104 	void *aux;
    105 {
    106 	struct wdc_obio_softc *sc = (void *)self;
    107 	struct confargs *ca = aux;
    108 	struct channel_softc *chp = &sc->wdc_channel;
    109 	int piointr, dmaintr;
    110 	int use_dma = 0;
    111 
    112 	piointr = dmaintr = -1;
    113 
    114 	if (sc->sc_wdcdev.sc_dev.dv_cfdata->cf_flags & WDC_OPTIONS_DMA) {
    115 		if (ca->ca_nreg >= 16 || ca->ca_nintr == -1)
    116 			use_dma = 1;	/* XXX Don't work yet. */
    117 	}
    118 
    119 	if (ca->ca_nintr == -1) {
    120 		piointr = WDC_DEFAULT_PIO_IRQ;
    121 		dmaintr = WDC_DEFAULT_DMA_IRQ;
    122 		printf(" irq property not found; using %d,%d",
    123 			piointr, dmaintr);
    124 	}
    125 
    126 	if (ca->ca_nintr >= 4 && ca->ca_nreg >= 8) {
    127 		piointr = ca->ca_intr[0];
    128 		printf(" irq %d", piointr);
    129 	}
    130 	if (ca->ca_nintr >= 8 && use_dma) {
    131 		dmaintr = ca->ca_intr[1];
    132 		printf(",%d", dmaintr);
    133 	}
    134 
    135 	if (piointr == -1) {
    136 		printf(": couldn't get irq property\n");
    137 		return;
    138 	}
    139 
    140 	if (use_dma)
    141 		printf(": DMA transfer");
    142 
    143 	printf("\n");
    144 
    145 	chp->cmd_iot = chp->ctl_iot =
    146 		macppc_make_bus_space_tag(ca->ca_baseaddr + ca->ca_reg[0], 4);
    147 
    148 	if (bus_space_map(chp->cmd_iot, 0, WDC_REG_NPORTS, 0, &chp->cmd_ioh) ||
    149 	    bus_space_subregion(chp->cmd_iot, chp->cmd_ioh,
    150 			WDC_AUXREG_OFFSET, 1, &chp->ctl_ioh)) {
    151 		printf("%s: couldn't map registers\n",
    152 			sc->sc_wdcdev.sc_dev.dv_xname);
    153 		return;
    154 	}
    155 #if 0
    156 	chp->data32iot = chp->cmd_iot;
    157 	chp->data32ioh = chp->cmd_ioh;
    158 #endif
    159 
    160 	intr_establish(piointr, IST_LEVEL, IPL_BIO, wdcintr, chp);
    161 
    162 	if (use_dma) {
    163 		if (dmaintr != -1)
    164 			intr_establish(dmaintr, IST_LEVEL, IPL_BIO,
    165 				       wdcintr, chp);
    166 
    167 		sc->sc_dmacmd = dbdma_alloc(sizeof(dbdma_command_t) * 20);
    168 		sc->sc_dmareg = mapiodev(ca->ca_baseaddr + ca->ca_reg[2],
    169 					 ca->ca_reg[3]);
    170 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA;
    171 	}
    172 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
    173 	sc->sc_wdcdev.PIO_cap = 0;
    174 	sc->wdc_chanptr = chp;
    175 	sc->sc_wdcdev.channels = &sc->wdc_chanptr;
    176 	sc->sc_wdcdev.nchannels = 1;
    177 	sc->sc_wdcdev.dma_arg = sc;
    178 	sc->sc_wdcdev.dma_init = wdc_obio_dma_init;
    179 	sc->sc_wdcdev.dma_start = wdc_obio_dma_start;
    180 	sc->sc_wdcdev.dma_finish = wdc_obio_dma_finish;
    181 	chp->channel = 0;
    182 	chp->wdc = &sc->sc_wdcdev;
    183 	chp->ch_queue = malloc(sizeof(struct channel_queue),
    184 		M_DEVBUF, M_NOWAIT);
    185 	if (chp->ch_queue == NULL) {
    186 		printf("%s: can't allocate memory for command queue",
    187 		sc->sc_wdcdev.sc_dev.dv_xname);
    188 		return;
    189 	}
    190 
    191 	wdcattach(chp);
    192 }
    193 
    194 static int
    195 wdc_obio_dma_init(v, channel, drive, databuf, datalen, read)
    196 	void *v;
    197 	void *databuf;
    198 	size_t datalen;
    199 	int read;
    200 {
    201 	struct wdc_obio_softc *sc = v;
    202 	vaddr_t va = (vaddr_t)databuf;
    203 	dbdma_command_t *cmdp;
    204 	u_int cmd;
    205 
    206 #ifdef DIAGNOSTIC
    207 	if (va & PGOFSET)
    208 		panic("wdc_obio: databuf not page aligned");
    209 	if (datalen > 65536)
    210 		panic("wdc_obio: datalen too large");
    211 #endif
    212 
    213 	cmdp = sc->sc_dmacmd;
    214 	cmd = read ? DBDMA_CMD_IN_MORE : DBDMA_CMD_OUT_MORE;
    215 	while (datalen > NBPG) {
    216 		DBDMA_BUILD(cmdp, cmd, 0, NBPG, vtophys(va),
    217 			DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    218 		datalen -= NBPG;
    219 		va += NBPG;
    220 		cmdp++;
    221 	}
    222 
    223 	/* the last page (datalen <= NBPG here) */
    224 	cmd = read ? DBDMA_CMD_IN_LAST : DBDMA_CMD_OUT_LAST;
    225 	DBDMA_BUILD(cmdp, cmd, 0, datalen, vtophys(va),
    226 		DBDMA_INT_ALWAYS, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    227 	cmdp++;
    228 
    229 	DBDMA_BUILD(cmdp, DBDMA_CMD_STOP, 0, 0, 0,
    230 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    231 
    232 	return 0;
    233 }
    234 
    235 static void
    236 wdc_obio_dma_start(v, channel, drive, read)
    237 	void *v;
    238 	int channel, drive;
    239 {
    240 	struct wdc_obio_softc *sc = v;
    241 
    242 	dbdma_start(sc->sc_dmareg, sc->sc_dmacmd);
    243 }
    244 
    245 static int
    246 wdc_obio_dma_finish(v, channel, drive, read)
    247 	void *v;
    248 	int channel, drive;
    249 	int read;
    250 {
    251 	/* nothing to do */
    252 	return 0;
    253 }
    254