Home | History | Annotate | Line # | Download | only in dev
wdc_obio.c revision 1.9.4.1
      1  1.9.4.1      he /*	$NetBSD: wdc_obio.c,v 1.9.4.1 2002/01/16 10:15:57 he 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.9  tsubai #include <dev/ata/atareg.h>
     50      1.1  tsubai #include <dev/ata/atavar.h>
     51      1.1  tsubai #include <dev/ic/wdcvar.h>
     52      1.1  tsubai 
     53      1.1  tsubai #include <macppc/dev/dbdma.h>
     54      1.1  tsubai 
     55      1.1  tsubai #define WDC_REG_NPORTS		8
     56      1.1  tsubai #define WDC_AUXREG_OFFSET	0x16
     57      1.1  tsubai #define WDC_DEFAULT_PIO_IRQ	13	/* XXX */
     58      1.1  tsubai #define WDC_DEFAULT_DMA_IRQ	2	/* XXX */
     59      1.1  tsubai 
     60      1.1  tsubai #define WDC_OPTIONS_DMA 0x01
     61      1.1  tsubai 
     62      1.1  tsubai /*
     63      1.1  tsubai  * XXX This code currently doesn't even try to allow 32-bit data port use.
     64      1.1  tsubai  */
     65      1.1  tsubai 
     66      1.1  tsubai struct wdc_obio_softc {
     67      1.1  tsubai 	struct wdc_softc sc_wdcdev;
     68      1.1  tsubai 	struct channel_softc *wdc_chanptr;
     69      1.1  tsubai 	struct channel_softc wdc_channel;
     70      1.1  tsubai 	dbdma_regmap_t *sc_dmareg;
     71      1.1  tsubai 	dbdma_command_t	*sc_dmacmd;
     72  1.9.4.1      he 	u_int sc_dmaconf[2];	/* per target value of CONFIG_REG */
     73      1.5  tsubai 	void *sc_ih;
     74      1.1  tsubai };
     75      1.1  tsubai 
     76      1.9  tsubai int wdc_obio_probe __P((struct device *, struct cfdata *, void *));
     77      1.9  tsubai void wdc_obio_attach __P((struct device *, struct device *, void *));
     78      1.9  tsubai int wdc_obio_detach __P((struct device *, int));
     79      1.9  tsubai int wdc_obio_dma_init __P((void *, int, int, void *, size_t, int));
     80      1.9  tsubai void wdc_obio_dma_start __P((void *, int, int));
     81      1.9  tsubai int wdc_obio_dma_finish __P((void *, int, int, int));
     82  1.9.4.1      he 
     83  1.9.4.1      he static void wdc_obio_select __P((struct channel_softc *, int));
     84      1.9  tsubai static void adjust_timing __P((struct channel_softc *));
     85  1.9.4.1      he static void ata4_adjust_timing __P((struct channel_softc *));
     86      1.1  tsubai 
     87      1.1  tsubai struct cfattach wdc_obio_ca = {
     88      1.5  tsubai 	sizeof(struct wdc_obio_softc), wdc_obio_probe, wdc_obio_attach,
     89      1.5  tsubai 	wdc_obio_detach, wdcactivate
     90      1.1  tsubai };
     91      1.1  tsubai 
     92      1.1  tsubai 
     93      1.1  tsubai int
     94      1.1  tsubai wdc_obio_probe(parent, match, aux)
     95      1.1  tsubai 	struct device *parent;
     96      1.1  tsubai 	struct cfdata *match;
     97      1.1  tsubai 	void *aux;
     98      1.1  tsubai {
     99      1.1  tsubai 	struct confargs *ca = aux;
    100      1.3  tsubai 	char compat[32];
    101      1.1  tsubai 
    102      1.3  tsubai 	/* XXX should not use name */
    103      1.1  tsubai 	if (strcmp(ca->ca_name, "ATA") == 0 ||
    104      1.1  tsubai 	    strcmp(ca->ca_name, "ata") == 0 ||
    105      1.2  tsubai 	    strcmp(ca->ca_name, "ata0") == 0 ||
    106      1.1  tsubai 	    strcmp(ca->ca_name, "ide") == 0)
    107      1.3  tsubai 		return 1;
    108      1.3  tsubai 
    109      1.3  tsubai 	bzero(compat, sizeof(compat));
    110      1.3  tsubai 	OF_getprop(ca->ca_node, "compatible", compat, sizeof(compat));
    111      1.6  tsubai 	if (strcmp(compat, "heathrow-ata") == 0 ||
    112      1.6  tsubai 	    strcmp(compat, "keylargo-ata") == 0)
    113      1.1  tsubai 		return 1;
    114      1.1  tsubai 
    115      1.1  tsubai 	return 0;
    116      1.1  tsubai }
    117      1.1  tsubai 
    118      1.1  tsubai void
    119      1.1  tsubai wdc_obio_attach(parent, self, aux)
    120      1.1  tsubai 	struct device *parent, *self;
    121      1.1  tsubai 	void *aux;
    122      1.1  tsubai {
    123      1.1  tsubai 	struct wdc_obio_softc *sc = (void *)self;
    124      1.1  tsubai 	struct confargs *ca = aux;
    125      1.1  tsubai 	struct channel_softc *chp = &sc->wdc_channel;
    126      1.4  tsubai 	int intr;
    127      1.1  tsubai 	int use_dma = 0;
    128      1.7  tsubai 	char path[80];
    129      1.1  tsubai 
    130      1.1  tsubai 	if (sc->sc_wdcdev.sc_dev.dv_cfdata->cf_flags & WDC_OPTIONS_DMA) {
    131      1.1  tsubai 		if (ca->ca_nreg >= 16 || ca->ca_nintr == -1)
    132      1.1  tsubai 			use_dma = 1;	/* XXX Don't work yet. */
    133      1.1  tsubai 	}
    134      1.1  tsubai 
    135      1.1  tsubai 	if (ca->ca_nintr >= 4 && ca->ca_nreg >= 8) {
    136      1.4  tsubai 		intr = ca->ca_intr[0];
    137      1.4  tsubai 		printf(" irq %d", intr);
    138      1.4  tsubai 	} else if (ca->ca_nintr == -1) {
    139      1.4  tsubai 		intr = WDC_DEFAULT_PIO_IRQ;
    140      1.4  tsubai 		printf(" irq property not found; using %d", intr);
    141      1.4  tsubai 	} else {
    142      1.1  tsubai 		printf(": couldn't get irq property\n");
    143      1.1  tsubai 		return;
    144      1.1  tsubai 	}
    145      1.1  tsubai 
    146      1.1  tsubai 	if (use_dma)
    147      1.1  tsubai 		printf(": DMA transfer");
    148      1.1  tsubai 
    149      1.1  tsubai 	printf("\n");
    150      1.1  tsubai 
    151      1.1  tsubai 	chp->cmd_iot = chp->ctl_iot =
    152      1.1  tsubai 		macppc_make_bus_space_tag(ca->ca_baseaddr + ca->ca_reg[0], 4);
    153      1.1  tsubai 
    154      1.1  tsubai 	if (bus_space_map(chp->cmd_iot, 0, WDC_REG_NPORTS, 0, &chp->cmd_ioh) ||
    155      1.1  tsubai 	    bus_space_subregion(chp->cmd_iot, chp->cmd_ioh,
    156      1.1  tsubai 			WDC_AUXREG_OFFSET, 1, &chp->ctl_ioh)) {
    157      1.1  tsubai 		printf("%s: couldn't map registers\n",
    158      1.1  tsubai 			sc->sc_wdcdev.sc_dev.dv_xname);
    159      1.1  tsubai 		return;
    160      1.1  tsubai 	}
    161      1.1  tsubai #if 0
    162      1.1  tsubai 	chp->data32iot = chp->cmd_iot;
    163      1.1  tsubai 	chp->data32ioh = chp->cmd_ioh;
    164      1.1  tsubai #endif
    165      1.1  tsubai 
    166      1.5  tsubai 	sc->sc_ih = intr_establish(intr, IST_LEVEL, IPL_BIO, wdcintr, chp);
    167      1.1  tsubai 
    168      1.1  tsubai 	if (use_dma) {
    169      1.1  tsubai 		sc->sc_dmacmd = dbdma_alloc(sizeof(dbdma_command_t) * 20);
    170      1.1  tsubai 		sc->sc_dmareg = mapiodev(ca->ca_baseaddr + ca->ca_reg[2],
    171      1.1  tsubai 					 ca->ca_reg[3]);
    172      1.1  tsubai 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA;
    173  1.9.4.1      he 		sc->sc_wdcdev.DMA_cap = 2;
    174  1.9.4.1      he 		if (strcmp(ca->ca_name, "ata-4") == 0) {
    175  1.9.4.1      he 			sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
    176  1.9.4.1      he 			sc->sc_wdcdev.UDMA_cap = 4;
    177  1.9.4.1      he 			sc->sc_wdcdev.set_modes = ata4_adjust_timing;
    178  1.9.4.1      he 		} else {
    179  1.9.4.1      he 			sc->sc_wdcdev.set_modes = adjust_timing;
    180  1.9.4.1      he 		}
    181  1.9.4.1      he #ifdef notyet
    182  1.9.4.1      he 		/* Minimum cycle time is 150ns (DMA MODE 1) on ohare. */
    183  1.9.4.1      he 		if (ohare) {
    184  1.9.4.1      he 			sc->sc_wdcdev.PIO_cap = 3;
    185  1.9.4.1      he 			sc->sc_wdcdev.DMA_cap = 1;
    186  1.9.4.1      he 		}
    187  1.9.4.1      he #endif
    188  1.9.4.1      he 	} else {
    189  1.9.4.1      he 		/* all non-dma controllers can use adjust_timing */
    190  1.9.4.1      he 		sc->sc_wdcdev.set_modes = adjust_timing;
    191      1.1  tsubai 	}
    192  1.9.4.1      he 
    193  1.9.4.1      he 	sc->sc_wdcdev.PIO_cap = 4;
    194  1.9.4.1      he 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_MODE;
    195      1.1  tsubai 	sc->wdc_chanptr = chp;
    196      1.1  tsubai 	sc->sc_wdcdev.channels = &sc->wdc_chanptr;
    197      1.1  tsubai 	sc->sc_wdcdev.nchannels = 1;
    198      1.1  tsubai 	sc->sc_wdcdev.dma_arg = sc;
    199      1.1  tsubai 	sc->sc_wdcdev.dma_init = wdc_obio_dma_init;
    200      1.1  tsubai 	sc->sc_wdcdev.dma_start = wdc_obio_dma_start;
    201      1.1  tsubai 	sc->sc_wdcdev.dma_finish = wdc_obio_dma_finish;
    202      1.1  tsubai 	chp->channel = 0;
    203      1.1  tsubai 	chp->wdc = &sc->sc_wdcdev;
    204      1.1  tsubai 	chp->ch_queue = malloc(sizeof(struct channel_queue),
    205      1.1  tsubai 		M_DEVBUF, M_NOWAIT);
    206      1.1  tsubai 	if (chp->ch_queue == NULL) {
    207      1.1  tsubai 		printf("%s: can't allocate memory for command queue",
    208      1.1  tsubai 		sc->sc_wdcdev.sc_dev.dv_xname);
    209      1.1  tsubai 		return;
    210      1.7  tsubai 	}
    211      1.7  tsubai 
    212      1.7  tsubai #define OHARE_FEATURE_REG	0xf3000038
    213      1.7  tsubai 
    214      1.7  tsubai 	/* XXX Enable wdc1 by feature reg. */
    215      1.7  tsubai 	bzero(path, sizeof(path));
    216      1.7  tsubai 	OF_package_to_path(ca->ca_node, path, sizeof(path));
    217      1.7  tsubai 	if (strcmp(path, "/bandit@F2000000/ohare@10/ata@21000") == 0) {
    218      1.7  tsubai 		u_int x;
    219      1.7  tsubai 
    220      1.7  tsubai 		x = in32rb(OHARE_FEATURE_REG);
    221      1.7  tsubai 		x |= 8;
    222      1.7  tsubai 		out32rb(OHARE_FEATURE_REG, x);
    223      1.1  tsubai 	}
    224      1.1  tsubai 
    225      1.1  tsubai 	wdcattach(chp);
    226  1.9.4.1      he 	sc->sc_wdcdev.set_modes(chp);
    227      1.9  tsubai 
    228      1.9  tsubai }
    229      1.9  tsubai 
    230      1.9  tsubai /* Multiword DMA transfer timings */
    231  1.9.4.1      he struct ide_timings {
    232      1.9  tsubai 	int cycle;	/* minimum cycle time [ns] */
    233      1.9  tsubai 	int active;	/* minimum command active time [ns] */
    234  1.9.4.1      he };
    235  1.9.4.1      he static struct ide_timings pio_timing[5] = {
    236  1.9.4.1      he 	{ 600, 180 },    /* Mode 0 */
    237  1.9.4.1      he 	{ 390, 150 },    /*      1 */
    238  1.9.4.1      he 	{ 240, 105 },    /*      2 */
    239  1.9.4.1      he 	{ 180,  90 },    /*      3 */
    240  1.9.4.1      he 	{ 120,  75 }     /*      4 */
    241  1.9.4.1      he };
    242  1.9.4.1      he static struct ide_timings dma_timing[3] = {
    243  1.9.4.1      he 	{ 480, 240 },	/* Mode 0 */
    244  1.9.4.1      he 	{ 165,  90 },	/* Mode 1 */
    245  1.9.4.1      he 	{ 120,  75 }	/* Mode 2 */
    246  1.9.4.1      he };
    247  1.9.4.1      he 
    248  1.9.4.1      he static struct ide_timings udma_timing[5] = {
    249  1.9.4.1      he 	{120, 180},	/* Mode 0 */
    250  1.9.4.1      he 	{ 90, 150},	/* Mode 1 */
    251  1.9.4.1      he 	{ 60, 120},	/* Mode 2 */
    252  1.9.4.1      he 	{ 45, 90},	/* Mode 3 */
    253  1.9.4.1      he 	{ 30, 90}	/* Mode 4 */
    254      1.9  tsubai };
    255      1.9  tsubai 
    256      1.9  tsubai #define TIME_TO_TICK(time) howmany((time), 30)
    257  1.9.4.1      he #define PIO_REC_OFFSET 4
    258  1.9.4.1      he #define PIO_REC_MIN 1
    259  1.9.4.1      he #define PIO_ACT_MIN 1
    260  1.9.4.1      he #define DMA_REC_OFFSET 1
    261  1.9.4.1      he #define DMA_REC_MIN 1
    262  1.9.4.1      he #define DMA_ACT_MIN 1
    263  1.9.4.1      he 
    264  1.9.4.1      he #define ATA4_TIME_TO_TICK(time)  howmany((time), 15) /* 15 ns clock */
    265      1.9  tsubai 
    266      1.9  tsubai #define CONFIG_REG (0x200 >> 4)		/* IDE access timing register */
    267      1.9  tsubai 
    268      1.9  tsubai void
    269  1.9.4.1      he wdc_obio_select(chp, drive)
    270  1.9.4.1      he 	struct channel_softc *chp;
    271  1.9.4.1      he 	int drive;
    272  1.9.4.1      he {
    273  1.9.4.1      he 	struct wdc_obio_softc *sc = (struct wdc_obio_softc *)chp->wdc;
    274  1.9.4.1      he 	bus_space_write_4(chp->cmd_iot, chp->cmd_ioh,
    275  1.9.4.1      he 			CONFIG_REG, sc->sc_dmaconf[drive]);
    276  1.9.4.1      he }
    277  1.9.4.1      he 
    278  1.9.4.1      he void
    279      1.9  tsubai adjust_timing(chp)
    280      1.9  tsubai 	struct channel_softc *chp;
    281      1.9  tsubai {
    282  1.9.4.1      he 	struct wdc_obio_softc *sc = (struct wdc_obio_softc *)chp->wdc;
    283  1.9.4.1      he 	int drive;
    284  1.9.4.1      he 	int min_cycle, min_active;
    285      1.9  tsubai 	int cycle_tick, act_tick, inact_tick, half_tick;
    286      1.9  tsubai 
    287  1.9.4.1      he 	for (drive = 0; drive < 2; drive++) {
    288  1.9.4.1      he 		u_int conf = 0;
    289  1.9.4.1      he 		struct ata_drive_datas *drvp;
    290  1.9.4.1      he 
    291  1.9.4.1      he 		drvp = &chp->ch_drive[drive];
    292  1.9.4.1      he 		/* set up pio mode timings */
    293  1.9.4.1      he 		if (drvp->drive_flags & DRIVE) {
    294  1.9.4.1      he 			int piomode = drvp->PIO_mode;
    295  1.9.4.1      he 			min_cycle = pio_timing[piomode].cycle;
    296  1.9.4.1      he 			min_active = pio_timing[piomode].active;
    297  1.9.4.1      he 
    298  1.9.4.1      he 			cycle_tick = TIME_TO_TICK(min_cycle);
    299  1.9.4.1      he 			act_tick = TIME_TO_TICK(min_active);
    300  1.9.4.1      he 			if (act_tick < PIO_ACT_MIN)
    301  1.9.4.1      he 				act_tick = PIO_ACT_MIN;
    302  1.9.4.1      he 			inact_tick = cycle_tick - act_tick - PIO_REC_OFFSET;
    303  1.9.4.1      he 			if (inact_tick < PIO_REC_MIN)
    304  1.9.4.1      he 				inact_tick = PIO_REC_MIN;
    305  1.9.4.1      he 			/* mask: 0x000007ff */
    306  1.9.4.1      he 			conf |= (inact_tick << 5) | act_tick;
    307  1.9.4.1      he 		}
    308  1.9.4.1      he 		/* Set up dma mode timings */
    309  1.9.4.1      he 		if (drvp->drive_flags & DRIVE_DMA) {
    310  1.9.4.1      he 			int dmamode = drvp->DMA_mode;
    311  1.9.4.1      he 			min_cycle = dma_timing[dmamode].cycle;
    312  1.9.4.1      he 			min_active = dma_timing[dmamode].active;
    313  1.9.4.1      he 			cycle_tick = TIME_TO_TICK(min_cycle);
    314  1.9.4.1      he 			act_tick = TIME_TO_TICK(min_active);
    315  1.9.4.1      he 			inact_tick = cycle_tick - act_tick - DMA_REC_OFFSET;
    316  1.9.4.1      he 			if (inact_tick < DMA_REC_MIN)
    317  1.9.4.1      he 				inact_tick = DMA_REC_MIN;
    318  1.9.4.1      he 			half_tick = 0;	/* XXX */
    319  1.9.4.1      he 			/* mask: 0xfffff800 */
    320  1.9.4.1      he 			conf |=
    321  1.9.4.1      he 					(half_tick << 21) |
    322  1.9.4.1      he 					(inact_tick << 16) | (act_tick << 11);
    323  1.9.4.1      he 		}
    324  1.9.4.1      he 		if (conf) {
    325  1.9.4.1      he 			printf("conf[%d] = 0x%x, cyc = %d (%d ns), act = %d (%d ns), inact = %d\n",
    326  1.9.4.1      he 					drive, conf, cycle_tick, min_cycle, act_tick, min_active, inact_tick);
    327  1.9.4.1      he 		}
    328  1.9.4.1      he 		sc->sc_dmaconf[drive] = conf;
    329  1.9.4.1      he 	}
    330  1.9.4.1      he 	sc->sc_wdcdev.cap &= ~WDC_CAPABILITY_SELECT;
    331  1.9.4.1      he 	sc->sc_wdcdev.select = 0;
    332  1.9.4.1      he 	if (sc->sc_dmaconf[0]) {
    333  1.9.4.1      he 		wdc_obio_select(chp,0);
    334  1.9.4.1      he 		if (sc->sc_dmaconf[1] && (sc->sc_dmaconf[0] != sc->sc_dmaconf[1])) {
    335  1.9.4.1      he 			sc->sc_wdcdev.select = wdc_obio_select;
    336  1.9.4.1      he 			sc->sc_wdcdev.cap |= WDC_CAPABILITY_SELECT;
    337  1.9.4.1      he 		}
    338  1.9.4.1      he 	} else if (sc->sc_dmaconf[1]) {
    339  1.9.4.1      he 		wdc_obio_select(chp,1);
    340  1.9.4.1      he 	}
    341  1.9.4.1      he }
    342      1.9  tsubai 
    343  1.9.4.1      he void
    344  1.9.4.1      he ata4_adjust_timing(chp)
    345  1.9.4.1      he 	struct channel_softc *chp;
    346  1.9.4.1      he {
    347  1.9.4.1      he 	struct wdc_obio_softc *sc = (struct wdc_obio_softc *)chp->wdc;
    348  1.9.4.1      he 	int drive;
    349  1.9.4.1      he 	int min_cycle, min_active;
    350  1.9.4.1      he 	int cycle_tick, act_tick, inact_tick;
    351  1.9.4.1      he 
    352  1.9.4.1      he 	for (drive = 0; drive < 2; drive++) {
    353  1.9.4.1      he 		u_int conf = 0;
    354  1.9.4.1      he 		struct ata_drive_datas *drvp;
    355  1.9.4.1      he 
    356  1.9.4.1      he 		drvp = &chp->ch_drive[drive];
    357  1.9.4.1      he 		/* set up pio mode timings */
    358  1.9.4.1      he 
    359  1.9.4.1      he 		if (drvp->drive_flags & DRIVE) {
    360  1.9.4.1      he 			int piomode = drvp->PIO_mode;
    361  1.9.4.1      he 			min_cycle = pio_timing[piomode].cycle;
    362  1.9.4.1      he 			min_active = pio_timing[piomode].active;
    363  1.9.4.1      he 
    364  1.9.4.1      he 			cycle_tick = ATA4_TIME_TO_TICK(min_cycle);
    365  1.9.4.1      he 			act_tick = ATA4_TIME_TO_TICK(min_active);
    366  1.9.4.1      he 			inact_tick = cycle_tick - act_tick;
    367  1.9.4.1      he 			/* mask: 0x000003ff */
    368  1.9.4.1      he 			conf |= (inact_tick << 5) | act_tick;
    369  1.9.4.1      he 		}
    370  1.9.4.1      he 		/* set up dma mode timings */
    371  1.9.4.1      he 		if (drvp->drive_flags & DRIVE_DMA) {
    372  1.9.4.1      he 			int dmamode = drvp->DMA_mode;
    373  1.9.4.1      he 			min_cycle = dma_timing[dmamode].cycle;
    374  1.9.4.1      he 			min_active = dma_timing[dmamode].active;
    375  1.9.4.1      he 			cycle_tick = ATA4_TIME_TO_TICK(min_cycle);
    376  1.9.4.1      he 			act_tick = ATA4_TIME_TO_TICK(min_active);
    377  1.9.4.1      he 			inact_tick = cycle_tick - act_tick;
    378  1.9.4.1      he 			/* mask: 0x001ffc00 */
    379  1.9.4.1      he 			conf |= (act_tick << 10) | (inact_tick << 15);
    380  1.9.4.1      he 		}
    381  1.9.4.1      he 		/* set up udma mode timings */
    382  1.9.4.1      he 		if (drvp->drive_flags & DRIVE_UDMA) {
    383  1.9.4.1      he 			int udmamode = drvp->UDMA_mode;
    384  1.9.4.1      he 			min_cycle = udma_timing[udmamode].cycle;
    385  1.9.4.1      he 			min_active = udma_timing[udmamode].active;
    386  1.9.4.1      he 			act_tick = ATA4_TIME_TO_TICK(min_active);
    387  1.9.4.1      he 			cycle_tick = ATA4_TIME_TO_TICK(min_cycle);
    388  1.9.4.1      he 			/* mask: 0x1ff00000 */
    389  1.9.4.1      he 			conf |= (cycle_tick << 21) | (act_tick << 25) | 0x100000;
    390  1.9.4.1      he 		}
    391  1.9.4.1      he 		if (conf) {
    392  1.9.4.1      he 			printf("ata4 conf[%d] = 0x%x, cyc = %d (%d ns), act = %d (%d ns), inact = %d\n",
    393  1.9.4.1      he 					drive, conf, cycle_tick, min_cycle, act_tick, min_active, inact_tick);
    394  1.9.4.1      he 		}
    395  1.9.4.1      he 		sc->sc_dmaconf[drive] = conf;
    396  1.9.4.1      he 	}
    397  1.9.4.1      he 	sc->sc_wdcdev.cap &= ~WDC_CAPABILITY_SELECT;
    398  1.9.4.1      he 	sc->sc_wdcdev.select = 0;
    399  1.9.4.1      he 	if (sc->sc_dmaconf[0]) {
    400  1.9.4.1      he 		wdc_obio_select(chp,0);
    401  1.9.4.1      he 		if (sc->sc_dmaconf[1] && (sc->sc_dmaconf[0] != sc->sc_dmaconf[1])) {
    402  1.9.4.1      he 			sc->sc_wdcdev.select = wdc_obio_select;
    403  1.9.4.1      he 			sc->sc_wdcdev.cap |= WDC_CAPABILITY_SELECT;
    404  1.9.4.1      he 		}
    405  1.9.4.1      he 	} else if (sc->sc_dmaconf[1]) {
    406  1.9.4.1      he 		wdc_obio_select(chp,1);
    407  1.9.4.1      he 	}
    408      1.5  tsubai }
    409      1.5  tsubai 
    410      1.5  tsubai int
    411      1.5  tsubai wdc_obio_detach(self, flags)
    412      1.5  tsubai 	struct device *self;
    413      1.5  tsubai 	int flags;
    414      1.5  tsubai {
    415      1.5  tsubai 	struct wdc_obio_softc *sc = (void *)self;
    416      1.5  tsubai 	struct channel_softc *chp = &sc->wdc_channel;
    417      1.5  tsubai 	int error;
    418      1.5  tsubai 
    419      1.5  tsubai 	if ((error = wdcdetach(self, flags)) != 0)
    420      1.5  tsubai 		return error;
    421      1.5  tsubai 
    422      1.5  tsubai 	intr_disestablish(sc->sc_ih);
    423      1.5  tsubai 
    424      1.5  tsubai 	free(sc->wdc_channel.ch_queue, M_DEVBUF);
    425      1.5  tsubai 
    426      1.5  tsubai 	/* Unmap our i/o space. */
    427      1.5  tsubai 	bus_space_unmap(chp->cmd_iot, chp->cmd_ioh, WDC_REG_NPORTS);
    428      1.5  tsubai 
    429      1.5  tsubai 	/* Unmap DMA registers. */
    430      1.5  tsubai 	/* XXX unmapiodev(sc->sc_dmareg); */
    431      1.5  tsubai 	/* XXX free(sc->sc_dmacmd); */
    432      1.5  tsubai 
    433      1.5  tsubai 	return 0;
    434      1.1  tsubai }
    435      1.1  tsubai 
    436      1.9  tsubai int
    437      1.1  tsubai wdc_obio_dma_init(v, channel, drive, databuf, datalen, read)
    438      1.1  tsubai 	void *v;
    439      1.1  tsubai 	void *databuf;
    440      1.1  tsubai 	size_t datalen;
    441      1.1  tsubai 	int read;
    442      1.1  tsubai {
    443      1.1  tsubai 	struct wdc_obio_softc *sc = v;
    444      1.1  tsubai 	vaddr_t va = (vaddr_t)databuf;
    445      1.1  tsubai 	dbdma_command_t *cmdp;
    446      1.4  tsubai 	u_int cmd, offset;
    447      1.1  tsubai 
    448      1.1  tsubai 	cmdp = sc->sc_dmacmd;
    449      1.1  tsubai 	cmd = read ? DBDMA_CMD_IN_MORE : DBDMA_CMD_OUT_MORE;
    450      1.4  tsubai 
    451      1.4  tsubai 	offset = va & PGOFSET;
    452      1.4  tsubai 
    453      1.4  tsubai 	/* if va is not page-aligned, setup the first page */
    454      1.4  tsubai 	if (offset != 0) {
    455      1.4  tsubai 		int rest = NBPG - offset;	/* the rest of the page */
    456      1.4  tsubai 
    457      1.4  tsubai 		if (datalen > rest) {		/* if continues to next page */
    458      1.4  tsubai 			DBDMA_BUILD(cmdp, cmd, 0, rest, vtophys(va),
    459      1.4  tsubai 				DBDMA_INT_NEVER, DBDMA_WAIT_NEVER,
    460      1.4  tsubai 				DBDMA_BRANCH_NEVER);
    461      1.4  tsubai 			datalen -= rest;
    462      1.4  tsubai 			va += rest;
    463      1.4  tsubai 			cmdp++;
    464      1.4  tsubai 		}
    465      1.4  tsubai 	}
    466      1.4  tsubai 
    467      1.4  tsubai 	/* now va is page-aligned */
    468      1.1  tsubai 	while (datalen > NBPG) {
    469      1.1  tsubai 		DBDMA_BUILD(cmdp, cmd, 0, NBPG, vtophys(va),
    470      1.1  tsubai 			DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    471      1.1  tsubai 		datalen -= NBPG;
    472      1.1  tsubai 		va += NBPG;
    473      1.1  tsubai 		cmdp++;
    474      1.1  tsubai 	}
    475      1.1  tsubai 
    476      1.1  tsubai 	/* the last page (datalen <= NBPG here) */
    477      1.1  tsubai 	cmd = read ? DBDMA_CMD_IN_LAST : DBDMA_CMD_OUT_LAST;
    478      1.1  tsubai 	DBDMA_BUILD(cmdp, cmd, 0, datalen, vtophys(va),
    479      1.4  tsubai 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    480      1.1  tsubai 	cmdp++;
    481      1.1  tsubai 
    482      1.1  tsubai 	DBDMA_BUILD(cmdp, DBDMA_CMD_STOP, 0, 0, 0,
    483      1.1  tsubai 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    484      1.1  tsubai 
    485      1.1  tsubai 	return 0;
    486      1.1  tsubai }
    487      1.1  tsubai 
    488      1.9  tsubai void
    489      1.8  tsubai wdc_obio_dma_start(v, channel, drive)
    490      1.1  tsubai 	void *v;
    491      1.1  tsubai 	int channel, drive;
    492      1.1  tsubai {
    493      1.1  tsubai 	struct wdc_obio_softc *sc = v;
    494      1.1  tsubai 
    495      1.1  tsubai 	dbdma_start(sc->sc_dmareg, sc->sc_dmacmd);
    496      1.1  tsubai }
    497      1.1  tsubai 
    498      1.9  tsubai int
    499      1.1  tsubai wdc_obio_dma_finish(v, channel, drive, read)
    500      1.1  tsubai 	void *v;
    501      1.1  tsubai 	int channel, drive;
    502      1.1  tsubai 	int read;
    503      1.1  tsubai {
    504      1.4  tsubai 	struct wdc_obio_softc *sc = v;
    505      1.4  tsubai 
    506      1.4  tsubai 	dbdma_stop(sc->sc_dmareg);
    507      1.1  tsubai 	return 0;
    508      1.1  tsubai }
    509