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