Home | History | Annotate | Line # | Download | only in dev
wdc_obio.c revision 1.46.16.8
      1  1.46.16.8  macallan /*	$NetBSD: wdc_obio.c,v 1.46.16.8 2007/09/12 00:41:13 macallan Exp $	*/
      2        1.1    tsubai 
      3        1.1    tsubai /*-
      4       1.27   mycroft  * Copyright (c) 1998, 2003 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.26     lukem 
     39       1.26     lukem #include <sys/cdefs.h>
     40  1.46.16.8  macallan __KERNEL_RCSID(0, "$NetBSD: wdc_obio.c,v 1.46.16.8 2007/09/12 00:41:13 macallan Exp $");
     41        1.1    tsubai 
     42        1.1    tsubai #include <sys/param.h>
     43        1.1    tsubai #include <sys/systm.h>
     44        1.1    tsubai #include <sys/device.h>
     45        1.1    tsubai #include <sys/malloc.h>
     46        1.1    tsubai 
     47       1.10       mrg #include <uvm/uvm_extern.h>
     48        1.1    tsubai 
     49        1.1    tsubai #include <machine/bus.h>
     50        1.1    tsubai #include <machine/autoconf.h>
     51  1.46.16.2   garbled #include <machine/pio.h>
     52        1.1    tsubai 
     53        1.9    tsubai #include <dev/ata/atareg.h>
     54        1.1    tsubai #include <dev/ata/atavar.h>
     55        1.1    tsubai #include <dev/ic/wdcvar.h>
     56        1.1    tsubai 
     57       1.12      matt #include <dev/ofw/openfirm.h>
     58       1.12      matt 
     59        1.1    tsubai #include <macppc/dev/dbdma.h>
     60        1.1    tsubai 
     61        1.1    tsubai #define WDC_REG_NPORTS		8
     62        1.1    tsubai #define WDC_AUXREG_OFFSET	0x16
     63        1.1    tsubai #define WDC_DEFAULT_PIO_IRQ	13	/* XXX */
     64        1.1    tsubai #define WDC_DEFAULT_DMA_IRQ	2	/* XXX */
     65        1.1    tsubai 
     66        1.1    tsubai #define WDC_OPTIONS_DMA 0x01
     67        1.1    tsubai 
     68        1.1    tsubai /*
     69        1.1    tsubai  * XXX This code currently doesn't even try to allow 32-bit data port use.
     70        1.1    tsubai  */
     71        1.1    tsubai 
     72        1.1    tsubai struct wdc_obio_softc {
     73        1.1    tsubai 	struct wdc_softc sc_wdcdev;
     74       1.39   thorpej 	struct ata_channel *sc_chanptr;
     75       1.39   thorpej 	struct ata_channel sc_channel;
     76       1.39   thorpej 	struct ata_queue sc_chqueue;
     77       1.39   thorpej 	struct wdc_regs sc_wdc_regs;
     78  1.46.16.8  macallan 	bus_space_handle_t sc_dmaregh;
     79        1.1    tsubai 	dbdma_regmap_t *sc_dmareg;
     80        1.1    tsubai 	dbdma_command_t	*sc_dmacmd;
     81       1.18       dbj 	u_int sc_dmaconf[2];	/* per target value of CONFIG_REG */
     82        1.5    tsubai 	void *sc_ih;
     83        1.1    tsubai };
     84        1.1    tsubai 
     85        1.9    tsubai int wdc_obio_probe __P((struct device *, struct cfdata *, void *));
     86        1.9    tsubai void wdc_obio_attach __P((struct device *, struct device *, void *));
     87        1.9    tsubai int wdc_obio_detach __P((struct device *, int));
     88        1.9    tsubai int wdc_obio_dma_init __P((void *, int, int, void *, size_t, int));
     89        1.9    tsubai void wdc_obio_dma_start __P((void *, int, int));
     90        1.9    tsubai int wdc_obio_dma_finish __P((void *, int, int, int));
     91       1.18       dbj 
     92       1.39   thorpej static void wdc_obio_select __P((struct ata_channel *, int));
     93       1.39   thorpej static void adjust_timing __P((struct ata_channel *));
     94       1.39   thorpej static void ata4_adjust_timing __P((struct ata_channel *));
     95        1.1    tsubai 
     96       1.22   thorpej CFATTACH_DECL(wdc_obio, sizeof(struct wdc_obio_softc),
     97       1.22   thorpej     wdc_obio_probe, wdc_obio_attach, wdc_obio_detach, wdcactivate);
     98        1.1    tsubai 
     99  1.46.16.8  macallan static const char *ata_names[] = {
    100  1.46.16.8  macallan     "heathrow-ata",
    101  1.46.16.8  macallan     "keylargo-ata",
    102  1.46.16.8  macallan     "ohare-ata",
    103  1.46.16.8  macallan     NULL
    104  1.46.16.8  macallan };
    105  1.46.16.8  macallan 
    106        1.1    tsubai int
    107        1.1    tsubai wdc_obio_probe(parent, match, aux)
    108        1.1    tsubai 	struct device *parent;
    109        1.1    tsubai 	struct cfdata *match;
    110        1.1    tsubai 	void *aux;
    111        1.1    tsubai {
    112        1.1    tsubai 	struct confargs *ca = aux;
    113        1.1    tsubai 
    114        1.3    tsubai 	/* XXX should not use name */
    115        1.1    tsubai 	if (strcmp(ca->ca_name, "ATA") == 0 ||
    116        1.1    tsubai 	    strcmp(ca->ca_name, "ata") == 0 ||
    117        1.2    tsubai 	    strcmp(ca->ca_name, "ata0") == 0 ||
    118        1.1    tsubai 	    strcmp(ca->ca_name, "ide") == 0)
    119        1.3    tsubai 		return 1;
    120        1.3    tsubai 
    121  1.46.16.8  macallan 	if (of_compatible(ca->ca_node, ata_names) >= 0)
    122        1.1    tsubai 		return 1;
    123        1.1    tsubai 
    124        1.1    tsubai 	return 0;
    125        1.1    tsubai }
    126        1.1    tsubai 
    127        1.1    tsubai void
    128        1.1    tsubai wdc_obio_attach(parent, self, aux)
    129        1.1    tsubai 	struct device *parent, *self;
    130        1.1    tsubai 	void *aux;
    131        1.1    tsubai {
    132        1.1    tsubai 	struct wdc_obio_softc *sc = (void *)self;
    133       1.39   thorpej 	struct wdc_regs *wdr;
    134        1.1    tsubai 	struct confargs *ca = aux;
    135       1.39   thorpej 	struct ata_channel *chp = &sc->sc_channel;
    136  1.46.16.7  macallan 	int intr, i, type = IST_EDGE;
    137        1.1    tsubai 	int use_dma = 0;
    138  1.46.16.8  macallan 	char path[80];
    139        1.1    tsubai 
    140       1.44   thorpej 	if (device_cfdata(&sc->sc_wdcdev.sc_atac.atac_dev)->cf_flags &
    141       1.44   thorpej 	    WDC_OPTIONS_DMA) {
    142        1.1    tsubai 		if (ca->ca_nreg >= 16 || ca->ca_nintr == -1)
    143        1.1    tsubai 			use_dma = 1;	/* XXX Don't work yet. */
    144        1.1    tsubai 	}
    145        1.1    tsubai 
    146        1.1    tsubai 	if (ca->ca_nintr >= 4 && ca->ca_nreg >= 8) {
    147        1.4    tsubai 		intr = ca->ca_intr[0];
    148        1.4    tsubai 		printf(" irq %d", intr);
    149  1.46.16.7  macallan 		if (ca->ca_nintr > 8) {
    150  1.46.16.7  macallan 			type = ca->ca_intr[1] ? IST_LEVEL : IST_EDGE;
    151  1.46.16.7  macallan 		}
    152  1.46.16.7  macallan 		printf(", %s triggered", (type == IST_EDGE) ? "edge" : "level");
    153        1.4    tsubai 	} else if (ca->ca_nintr == -1) {
    154        1.4    tsubai 		intr = WDC_DEFAULT_PIO_IRQ;
    155        1.4    tsubai 		printf(" irq property not found; using %d", intr);
    156        1.4    tsubai 	} else {
    157        1.1    tsubai 		printf(": couldn't get irq property\n");
    158        1.1    tsubai 		return;
    159        1.1    tsubai 	}
    160        1.1    tsubai 
    161        1.1    tsubai 	if (use_dma)
    162        1.1    tsubai 		printf(": DMA transfer");
    163        1.1    tsubai 
    164        1.1    tsubai 	printf("\n");
    165        1.1    tsubai 
    166       1.39   thorpej 	sc->sc_wdcdev.regs = wdr = &sc->sc_wdc_regs;
    167       1.39   thorpej 
    168  1.46.16.6  macallan 	wdr->cmd_iot = wdr->ctl_iot = ca->ca_tag;
    169  1.46.16.4  macallan 
    170  1.46.16.6  macallan 	if (bus_space_map(wdr->cmd_iot, ca->ca_baseaddr + ca->ca_reg[0],
    171  1.46.16.6  macallan 	    WDC_REG_NPORTS << 4, 0, &wdr->cmd_baseioh) ||
    172  1.46.16.4  macallan 	    bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
    173  1.46.16.6  macallan 			WDC_AUXREG_OFFSET << 4, 1, &wdr->ctl_ioh)) {
    174        1.1    tsubai 		printf("%s: couldn't map registers\n",
    175       1.40   thorpej 			sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
    176        1.1    tsubai 		return;
    177        1.1    tsubai 	}
    178  1.46.16.4  macallan 
    179       1.30    bouyer 	for (i = 0; i < WDC_NREG; i++) {
    180  1.46.16.6  macallan 		if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh, i << 4,
    181       1.39   thorpej 		    i == 0 ? 4 : 1, &wdr->cmd_iohs[i]) != 0) {
    182       1.39   thorpej 			bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh,
    183  1.46.16.6  macallan 			    WDC_REG_NPORTS << 4);
    184       1.30    bouyer 			printf("%s: couldn't subregion registers\n",
    185       1.40   thorpej 			    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
    186       1.30    bouyer 			return;
    187       1.30    bouyer 		}
    188       1.30    bouyer 	}
    189        1.1    tsubai #if 0
    190       1.39   thorpej 	wdr->data32iot = wdr->cmd_iot;
    191       1.39   thorpej 	wdr->data32ioh = wdr->cmd_ioh;
    192        1.1    tsubai #endif
    193        1.1    tsubai 
    194  1.46.16.7  macallan 	sc->sc_ih = intr_establish(intr, type, IPL_BIO, wdcintr, chp);
    195        1.1    tsubai 
    196        1.1    tsubai 	if (use_dma) {
    197        1.1    tsubai 		sc->sc_dmacmd = dbdma_alloc(sizeof(dbdma_command_t) * 20);
    198  1.46.16.8  macallan 		/*
    199  1.46.16.8  macallan 		 * XXX
    200  1.46.16.8  macallan 		 * we don't use ca->ca_reg[3] for size here because at least
    201  1.46.16.8  macallan 		 * on the PB3400c it says 0x200 for both IDE channels ( the
    202  1.46.16.8  macallan 		 * one on the mainboard and the other on the mediabay ) but
    203  1.46.16.8  macallan 		 * their start addresses are only 0x100 apart. Since those
    204  1.46.16.8  macallan 		 * DMA registers are always 0x100 or less we don't really
    205  1.46.16.8  macallan 		 * have to care though
    206  1.46.16.8  macallan 		 */
    207  1.46.16.8  macallan 		if (bus_space_map(wdr->cmd_iot, ca->ca_baseaddr + ca->ca_reg[2],
    208  1.46.16.8  macallan 		    0x100, BUS_SPACE_MAP_LINEAR, &sc->sc_dmaregh)) {
    209  1.46.16.8  macallan 
    210  1.46.16.8  macallan 			aprint_error("%s: unable to map DMA registers (%08x)\n",
    211  1.46.16.8  macallan 			    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
    212  1.46.16.8  macallan 			    ca->ca_reg[2]);
    213  1.46.16.8  macallan 			/* should unmap stuff here */
    214  1.46.16.8  macallan 			return;
    215  1.46.16.8  macallan 		}
    216  1.46.16.8  macallan 		sc->sc_dmareg = bus_space_vaddr(wdr->cmd_iot, sc->sc_dmaregh);
    217  1.46.16.8  macallan 
    218       1.40   thorpej 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA;
    219       1.40   thorpej 		sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
    220       1.16    bouyer 		if (strcmp(ca->ca_name, "ata-4") == 0) {
    221       1.40   thorpej 			sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_UDMA;
    222       1.40   thorpej 			sc->sc_wdcdev.sc_atac.atac_udma_cap = 4;
    223       1.45  macallan 			sc->sc_wdcdev.sc_atac.atac_set_modes =
    224       1.45  macallan 			    ata4_adjust_timing;
    225       1.16    bouyer 		} else {
    226       1.40   thorpej 			sc->sc_wdcdev.sc_atac.atac_set_modes = adjust_timing;
    227       1.16    bouyer 		}
    228       1.13    bouyer #ifdef notyet
    229       1.13    bouyer 		/* Minimum cycle time is 150ns (DMA MODE 1) on ohare. */
    230       1.13    bouyer 		if (ohare) {
    231       1.40   thorpej 			sc->sc_wdcdev.sc_atac.atac_pio_cap = 3;
    232       1.40   thorpej 			sc->sc_wdcdev.sc_atac.atac_dma_cap = 1;
    233       1.13    bouyer 		}
    234       1.13    bouyer #endif
    235       1.17    bouyer 	} else {
    236       1.24       wiz 		/* all non-DMA controllers can use adjust_timing */
    237       1.40   thorpej 		sc->sc_wdcdev.sc_atac.atac_set_modes = adjust_timing;
    238        1.1    tsubai 	}
    239       1.17    bouyer 
    240       1.40   thorpej 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
    241       1.40   thorpej 	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
    242       1.39   thorpej 	sc->sc_chanptr = chp;
    243       1.40   thorpej 	sc->sc_wdcdev.sc_atac.atac_channels = &sc->sc_chanptr;
    244       1.40   thorpej 	sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
    245        1.1    tsubai 	sc->sc_wdcdev.dma_arg = sc;
    246        1.1    tsubai 	sc->sc_wdcdev.dma_init = wdc_obio_dma_init;
    247        1.1    tsubai 	sc->sc_wdcdev.dma_start = wdc_obio_dma_start;
    248        1.1    tsubai 	sc->sc_wdcdev.dma_finish = wdc_obio_dma_finish;
    249       1.35   thorpej 	chp->ch_channel = 0;
    250       1.40   thorpej 	chp->ch_atac = &sc->sc_wdcdev.sc_atac;
    251       1.39   thorpej 	chp->ch_queue = &sc->sc_chqueue;
    252       1.43    bouyer 	chp->ch_ndrive = 2;
    253        1.7    tsubai 
    254       1.41   aymeric 	wdc_init_shadow_regs(chp);
    255       1.41   aymeric 
    256        1.7    tsubai #define OHARE_FEATURE_REG	0xf3000038
    257        1.7    tsubai 
    258        1.7    tsubai 	/* XXX Enable wdc1 by feature reg. */
    259       1.14       wiz 	memset(path, 0, sizeof(path));
    260        1.7    tsubai 	OF_package_to_path(ca->ca_node, path, sizeof(path));
    261        1.7    tsubai 	if (strcmp(path, "/bandit@F2000000/ohare@10/ata@21000") == 0) {
    262  1.46.16.4  macallan 		u_int x;
    263        1.7    tsubai 
    264  1.46.16.4  macallan 		x = in32rb(OHARE_FEATURE_REG);
    265        1.7    tsubai 		x |= 8;
    266  1.46.16.4  macallan 		out32rb(OHARE_FEATURE_REG, x);
    267        1.1    tsubai 	}
    268        1.1    tsubai 
    269       1.29    bouyer 	wdcattach(chp);
    270        1.9    tsubai }
    271        1.9    tsubai 
    272        1.9    tsubai /* Multiword DMA transfer timings */
    273       1.13    bouyer struct ide_timings {
    274        1.9    tsubai 	int cycle;	/* minimum cycle time [ns] */
    275        1.9    tsubai 	int active;	/* minimum command active time [ns] */
    276       1.13    bouyer };
    277  1.46.16.4  macallan static struct ide_timings pio_timing[5] = {
    278       1.19       dbj 	{ 600, 180 },    /* Mode 0 */
    279       1.19       dbj 	{ 390, 150 },    /*      1 */
    280       1.19       dbj 	{ 240, 105 },    /*      2 */
    281       1.19       dbj 	{ 180,  90 },    /*      3 */
    282       1.19       dbj 	{ 120,  75 }     /*      4 */
    283       1.13    bouyer };
    284  1.46.16.4  macallan static struct ide_timings dma_timing[3] = {
    285       1.19       dbj 	{ 480, 240 },	/* Mode 0 */
    286       1.19       dbj 	{ 165,  90 },	/* Mode 1 */
    287       1.19       dbj 	{ 120,  75 }	/* Mode 2 */
    288        1.9    tsubai };
    289        1.9    tsubai 
    290  1.46.16.4  macallan static struct ide_timings udma_timing[5] = {
    291       1.19       dbj 	{120, 180},	/* Mode 0 */
    292       1.19       dbj 	{ 90, 150},	/* Mode 1 */
    293       1.19       dbj 	{ 60, 120},	/* Mode 2 */
    294       1.19       dbj 	{ 45, 90},	/* Mode 3 */
    295       1.19       dbj 	{ 30, 90}	/* Mode 4 */
    296       1.16    bouyer };
    297       1.16    bouyer 
    298        1.9    tsubai #define TIME_TO_TICK(time) howmany((time), 30)
    299       1.16    bouyer #define PIO_REC_OFFSET 4
    300       1.16    bouyer #define PIO_REC_MIN 1
    301       1.16    bouyer #define PIO_ACT_MIN 1
    302       1.16    bouyer #define DMA_REC_OFFSET 1
    303       1.16    bouyer #define DMA_REC_MIN 1
    304       1.16    bouyer #define DMA_ACT_MIN 1
    305       1.16    bouyer 
    306       1.18       dbj #define ATA4_TIME_TO_TICK(time)  howmany((time), 15) /* 15 ns clock */
    307       1.16    bouyer 
    308  1.46.16.8  macallan #define CONFIG_REG (0x200)		/* IDE access timing register */
    309        1.9    tsubai 
    310       1.18       dbj void
    311       1.18       dbj wdc_obio_select(chp, drive)
    312       1.39   thorpej 	struct ata_channel *chp;
    313       1.18       dbj 	int drive;
    314       1.18       dbj {
    315       1.40   thorpej 	struct wdc_obio_softc *sc = (struct wdc_obio_softc *)chp->ch_atac;
    316       1.40   thorpej 	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
    317       1.39   thorpej 
    318       1.39   thorpej 	bus_space_write_4(wdr->cmd_iot, wdr->cmd_baseioh,
    319       1.18       dbj 			CONFIG_REG, sc->sc_dmaconf[drive]);
    320       1.18       dbj }
    321        1.9    tsubai 
    322        1.9    tsubai void
    323        1.9    tsubai adjust_timing(chp)
    324       1.39   thorpej 	struct ata_channel *chp;
    325        1.9    tsubai {
    326       1.40   thorpej 	struct wdc_obio_softc *sc = (struct wdc_obio_softc *)chp->ch_atac;
    327       1.13    bouyer 	int drive;
    328       1.31       mjl 	int min_cycle = 0, min_active = 0;
    329       1.31       mjl 	int cycle_tick = 0, act_tick = 0, inact_tick = 0, half_tick;
    330        1.9    tsubai 
    331       1.13    bouyer 	for (drive = 0; drive < 2; drive++) {
    332       1.18       dbj 		u_int conf = 0;
    333       1.18       dbj 		struct ata_drive_datas *drvp;
    334       1.18       dbj 
    335       1.13    bouyer 		drvp = &chp->ch_drive[drive];
    336       1.18       dbj 		/* set up pio mode timings */
    337       1.18       dbj 		if (drvp->drive_flags & DRIVE) {
    338       1.18       dbj 			int piomode = drvp->PIO_mode;
    339       1.18       dbj 			min_cycle = pio_timing[piomode].cycle;
    340       1.18       dbj 			min_active = pio_timing[piomode].active;
    341       1.18       dbj 
    342       1.18       dbj 			cycle_tick = TIME_TO_TICK(min_cycle);
    343       1.18       dbj 			act_tick = TIME_TO_TICK(min_active);
    344       1.18       dbj 			if (act_tick < PIO_ACT_MIN)
    345       1.18       dbj 				act_tick = PIO_ACT_MIN;
    346       1.18       dbj 			inact_tick = cycle_tick - act_tick - PIO_REC_OFFSET;
    347       1.18       dbj 			if (inact_tick < PIO_REC_MIN)
    348       1.18       dbj 				inact_tick = PIO_REC_MIN;
    349       1.18       dbj 			/* mask: 0x000007ff */
    350       1.18       dbj 			conf |= (inact_tick << 5) | act_tick;
    351       1.18       dbj 		}
    352       1.24       wiz 		/* Set up DMA mode timings */
    353       1.13    bouyer 		if (drvp->drive_flags & DRIVE_DMA) {
    354       1.18       dbj 			int dmamode = drvp->DMA_mode;
    355       1.18       dbj 			min_cycle = dma_timing[dmamode].cycle;
    356       1.18       dbj 			min_active = dma_timing[dmamode].active;
    357       1.18       dbj 			cycle_tick = TIME_TO_TICK(min_cycle);
    358       1.18       dbj 			act_tick = TIME_TO_TICK(min_active);
    359       1.18       dbj 			inact_tick = cycle_tick - act_tick - DMA_REC_OFFSET;
    360       1.18       dbj 			if (inact_tick < DMA_REC_MIN)
    361       1.18       dbj 				inact_tick = DMA_REC_MIN;
    362       1.18       dbj 			half_tick = 0;	/* XXX */
    363       1.18       dbj 			/* mask: 0xfffff800 */
    364       1.18       dbj 			conf |=
    365       1.18       dbj 					(half_tick << 21) |
    366       1.18       dbj 					(inact_tick << 16) | (act_tick << 11);
    367       1.13    bouyer 		}
    368       1.20    bouyer #ifdef DEBUG
    369       1.18       dbj 		if (conf) {
    370       1.18       dbj 			printf("conf[%d] = 0x%x, cyc = %d (%d ns), act = %d (%d ns), inact = %d\n",
    371       1.18       dbj 					drive, conf, cycle_tick, min_cycle, act_tick, min_active, inact_tick);
    372       1.18       dbj 		}
    373       1.20    bouyer #endif
    374       1.18       dbj 		sc->sc_dmaconf[drive] = conf;
    375       1.13    bouyer 	}
    376       1.18       dbj 	sc->sc_wdcdev.select = 0;
    377       1.18       dbj 	if (sc->sc_dmaconf[0]) {
    378       1.18       dbj 		wdc_obio_select(chp,0);
    379       1.38   thorpej 		if (sc->sc_dmaconf[1] &&
    380       1.38   thorpej 		    (sc->sc_dmaconf[0] != sc->sc_dmaconf[1])) {
    381       1.18       dbj 			sc->sc_wdcdev.select = wdc_obio_select;
    382       1.13    bouyer 		}
    383       1.18       dbj 	} else if (sc->sc_dmaconf[1]) {
    384       1.18       dbj 		wdc_obio_select(chp,1);
    385       1.13    bouyer 	}
    386       1.16    bouyer }
    387       1.16    bouyer 
    388       1.16    bouyer void
    389       1.16    bouyer ata4_adjust_timing(chp)
    390       1.39   thorpej 	struct ata_channel *chp;
    391       1.16    bouyer {
    392       1.40   thorpej 	struct wdc_obio_softc *sc = (struct wdc_obio_softc *)chp->ch_atac;
    393       1.16    bouyer 	int drive;
    394       1.31       mjl 	int min_cycle = 0, min_active = 0;
    395       1.31       mjl 	int cycle_tick = 0, act_tick = 0, inact_tick = 0;
    396       1.16    bouyer 
    397       1.18       dbj 	for (drive = 0; drive < 2; drive++) {
    398       1.18       dbj 		u_int conf = 0;
    399       1.18       dbj 		struct ata_drive_datas *drvp;
    400       1.16    bouyer 
    401       1.16    bouyer 		drvp = &chp->ch_drive[drive];
    402       1.18       dbj 		/* set up pio mode timings */
    403       1.18       dbj 
    404       1.18       dbj 		if (drvp->drive_flags & DRIVE) {
    405       1.18       dbj 			int piomode = drvp->PIO_mode;
    406       1.18       dbj 			min_cycle = pio_timing[piomode].cycle;
    407       1.18       dbj 			min_active = pio_timing[piomode].active;
    408       1.18       dbj 
    409       1.18       dbj 			cycle_tick = ATA4_TIME_TO_TICK(min_cycle);
    410       1.18       dbj 			act_tick = ATA4_TIME_TO_TICK(min_active);
    411       1.18       dbj 			inact_tick = cycle_tick - act_tick;
    412       1.18       dbj 			/* mask: 0x000003ff */
    413       1.18       dbj 			conf |= (inact_tick << 5) | act_tick;
    414       1.18       dbj 		}
    415       1.18       dbj 		/* set up dma mode timings */
    416       1.16    bouyer 		if (drvp->drive_flags & DRIVE_DMA) {
    417       1.18       dbj 			int dmamode = drvp->DMA_mode;
    418       1.18       dbj 			min_cycle = dma_timing[dmamode].cycle;
    419       1.18       dbj 			min_active = dma_timing[dmamode].active;
    420       1.18       dbj 			cycle_tick = ATA4_TIME_TO_TICK(min_cycle);
    421       1.18       dbj 			act_tick = ATA4_TIME_TO_TICK(min_active);
    422       1.18       dbj 			inact_tick = cycle_tick - act_tick;
    423       1.18       dbj 			/* mask: 0x001ffc00 */
    424       1.18       dbj 			conf |= (act_tick << 10) | (inact_tick << 15);
    425       1.16    bouyer 		}
    426       1.18       dbj 		/* set up udma mode timings */
    427       1.16    bouyer 		if (drvp->drive_flags & DRIVE_UDMA) {
    428       1.18       dbj 			int udmamode = drvp->UDMA_mode;
    429       1.18       dbj 			min_cycle = udma_timing[udmamode].cycle;
    430       1.18       dbj 			min_active = udma_timing[udmamode].active;
    431       1.18       dbj 			act_tick = ATA4_TIME_TO_TICK(min_active);
    432       1.18       dbj 			cycle_tick = ATA4_TIME_TO_TICK(min_cycle);
    433       1.18       dbj 			/* mask: 0x1ff00000 */
    434       1.18       dbj 			conf |= (cycle_tick << 21) | (act_tick << 25) | 0x100000;
    435       1.18       dbj 		}
    436       1.20    bouyer #ifdef DEBUG
    437       1.18       dbj 		if (conf) {
    438       1.18       dbj 			printf("ata4 conf[%d] = 0x%x, cyc = %d (%d ns), act = %d (%d ns), inact = %d\n",
    439       1.18       dbj 					drive, conf, cycle_tick, min_cycle, act_tick, min_active, inact_tick);
    440       1.16    bouyer 		}
    441       1.20    bouyer #endif
    442       1.18       dbj 		sc->sc_dmaconf[drive] = conf;
    443       1.16    bouyer 	}
    444       1.18       dbj 	sc->sc_wdcdev.select = 0;
    445       1.18       dbj 	if (sc->sc_dmaconf[0]) {
    446       1.18       dbj 		wdc_obio_select(chp,0);
    447       1.38   thorpej 		if (sc->sc_dmaconf[1] &&
    448       1.38   thorpej 		    (sc->sc_dmaconf[0] != sc->sc_dmaconf[1])) {
    449       1.18       dbj 			sc->sc_wdcdev.select = wdc_obio_select;
    450       1.16    bouyer 		}
    451       1.18       dbj 	} else if (sc->sc_dmaconf[1]) {
    452       1.18       dbj 		wdc_obio_select(chp,1);
    453       1.16    bouyer 	}
    454        1.5    tsubai }
    455        1.5    tsubai 
    456        1.5    tsubai int
    457        1.5    tsubai wdc_obio_detach(self, flags)
    458        1.5    tsubai 	struct device *self;
    459        1.5    tsubai 	int flags;
    460        1.5    tsubai {
    461        1.5    tsubai 	struct wdc_obio_softc *sc = (void *)self;
    462        1.5    tsubai 	int error;
    463        1.5    tsubai 
    464        1.5    tsubai 	if ((error = wdcdetach(self, flags)) != 0)
    465        1.5    tsubai 		return error;
    466        1.5    tsubai 
    467        1.5    tsubai 	intr_disestablish(sc->sc_ih);
    468        1.5    tsubai 
    469        1.5    tsubai 	/* Unmap our i/o space. */
    470       1.39   thorpej 	bus_space_unmap(sc->sc_wdcdev.regs->cmd_iot,
    471  1.46.16.6  macallan 			sc->sc_wdcdev.regs->cmd_baseioh, WDC_REG_NPORTS << 4);
    472        1.5    tsubai 
    473        1.5    tsubai 	/* Unmap DMA registers. */
    474        1.5    tsubai 	/* XXX unmapiodev(sc->sc_dmareg); */
    475        1.5    tsubai 	/* XXX free(sc->sc_dmacmd); */
    476        1.5    tsubai 
    477        1.5    tsubai 	return 0;
    478        1.1    tsubai }
    479        1.1    tsubai 
    480        1.9    tsubai int
    481       1.25  hamajima wdc_obio_dma_init(v, channel, drive, databuf, datalen, flags)
    482        1.1    tsubai 	void *v;
    483        1.1    tsubai 	void *databuf;
    484        1.1    tsubai 	size_t datalen;
    485       1.25  hamajima 	int flags;
    486        1.1    tsubai {
    487        1.1    tsubai 	struct wdc_obio_softc *sc = v;
    488        1.1    tsubai 	vaddr_t va = (vaddr_t)databuf;
    489        1.1    tsubai 	dbdma_command_t *cmdp;
    490        1.4    tsubai 	u_int cmd, offset;
    491       1.25  hamajima 	int read = flags & WDC_DMA_READ;
    492        1.1    tsubai 
    493        1.1    tsubai 	cmdp = sc->sc_dmacmd;
    494        1.1    tsubai 	cmd = read ? DBDMA_CMD_IN_MORE : DBDMA_CMD_OUT_MORE;
    495        1.4    tsubai 
    496        1.4    tsubai 	offset = va & PGOFSET;
    497        1.4    tsubai 
    498        1.4    tsubai 	/* if va is not page-aligned, setup the first page */
    499        1.4    tsubai 	if (offset != 0) {
    500       1.23   thorpej 		int rest = PAGE_SIZE - offset;	/* the rest of the page */
    501        1.4    tsubai 
    502        1.4    tsubai 		if (datalen > rest) {		/* if continues to next page */
    503        1.4    tsubai 			DBDMA_BUILD(cmdp, cmd, 0, rest, vtophys(va),
    504        1.4    tsubai 				DBDMA_INT_NEVER, DBDMA_WAIT_NEVER,
    505        1.4    tsubai 				DBDMA_BRANCH_NEVER);
    506        1.4    tsubai 			datalen -= rest;
    507        1.4    tsubai 			va += rest;
    508        1.4    tsubai 			cmdp++;
    509        1.4    tsubai 		}
    510        1.4    tsubai 	}
    511        1.4    tsubai 
    512        1.4    tsubai 	/* now va is page-aligned */
    513       1.23   thorpej 	while (datalen > PAGE_SIZE) {
    514       1.23   thorpej 		DBDMA_BUILD(cmdp, cmd, 0, PAGE_SIZE, vtophys(va),
    515        1.1    tsubai 			DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    516       1.23   thorpej 		datalen -= PAGE_SIZE;
    517       1.23   thorpej 		va += PAGE_SIZE;
    518        1.1    tsubai 		cmdp++;
    519        1.1    tsubai 	}
    520        1.1    tsubai 
    521       1.23   thorpej 	/* the last page (datalen <= PAGE_SIZE here) */
    522        1.1    tsubai 	cmd = read ? DBDMA_CMD_IN_LAST : DBDMA_CMD_OUT_LAST;
    523        1.1    tsubai 	DBDMA_BUILD(cmdp, cmd, 0, datalen, vtophys(va),
    524        1.4    tsubai 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    525        1.1    tsubai 	cmdp++;
    526        1.1    tsubai 
    527        1.1    tsubai 	DBDMA_BUILD(cmdp, DBDMA_CMD_STOP, 0, 0, 0,
    528        1.1    tsubai 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    529        1.1    tsubai 
    530        1.1    tsubai 	return 0;
    531        1.1    tsubai }
    532        1.1    tsubai 
    533        1.9    tsubai void
    534        1.8    tsubai wdc_obio_dma_start(v, channel, drive)
    535        1.1    tsubai 	void *v;
    536        1.1    tsubai 	int channel, drive;
    537        1.1    tsubai {
    538        1.1    tsubai 	struct wdc_obio_softc *sc = v;
    539        1.1    tsubai 
    540        1.1    tsubai 	dbdma_start(sc->sc_dmareg, sc->sc_dmacmd);
    541        1.1    tsubai }
    542        1.1    tsubai 
    543        1.9    tsubai int
    544        1.1    tsubai wdc_obio_dma_finish(v, channel, drive, read)
    545        1.1    tsubai 	void *v;
    546        1.1    tsubai 	int channel, drive;
    547        1.1    tsubai 	int read;
    548        1.1    tsubai {
    549        1.4    tsubai 	struct wdc_obio_softc *sc = v;
    550        1.4    tsubai 
    551        1.4    tsubai 	dbdma_stop(sc->sc_dmareg);
    552        1.1    tsubai 	return 0;
    553        1.1    tsubai }
    554