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