Home | History | Annotate | Line # | Download | only in pci
pciide_common.c revision 1.71
      1  1.71   thorpej /*	$NetBSD: pciide_common.c,v 1.71 2024/03/31 18:59:52 thorpej Exp $	*/
      2   1.1    bouyer 
      3   1.1    bouyer 
      4   1.1    bouyer /*
      5   1.1    bouyer  * Copyright (c) 1999, 2000, 2001, 2003 Manuel Bouyer.
      6   1.1    bouyer  *
      7   1.1    bouyer  * Redistribution and use in source and binary forms, with or without
      8   1.1    bouyer  * modification, are permitted provided that the following conditions
      9   1.1    bouyer  * are met:
     10   1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     11   1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     12   1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     14   1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     15   1.1    bouyer  *
     16   1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.26     perry  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21   1.1    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22   1.1    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23   1.1    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24   1.1    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25   1.1    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26   1.1    bouyer  *
     27   1.1    bouyer  */
     28   1.1    bouyer 
     29   1.1    bouyer 
     30   1.1    bouyer /*
     31   1.1    bouyer  * Copyright (c) 1996, 1998 Christopher G. Demetriou.  All rights reserved.
     32   1.1    bouyer  *
     33   1.1    bouyer  * Redistribution and use in source and binary forms, with or without
     34   1.1    bouyer  * modification, are permitted provided that the following conditions
     35   1.1    bouyer  * are met:
     36   1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     37   1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     38   1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     39   1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     40   1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     41   1.1    bouyer  * 3. All advertising materials mentioning features or use of this software
     42   1.1    bouyer  *    must display the following acknowledgement:
     43   1.1    bouyer  *      This product includes software developed by Christopher G. Demetriou
     44   1.1    bouyer  *	for the NetBSD Project.
     45   1.1    bouyer  * 4. The name of the author may not be used to endorse or promote products
     46   1.1    bouyer  *    derived from this software without specific prior written permission
     47   1.1    bouyer  *
     48   1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     49   1.1    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     50   1.1    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     51   1.1    bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     52   1.1    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     53   1.1    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     54   1.1    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     55   1.1    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     56   1.1    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     57   1.1    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     58   1.1    bouyer  */
     59   1.1    bouyer 
     60   1.1    bouyer /*
     61   1.1    bouyer  * PCI IDE controller driver.
     62   1.1    bouyer  *
     63   1.1    bouyer  * Author: Christopher G. Demetriou, March 2, 1998 (derived from NetBSD
     64   1.1    bouyer  * sys/dev/pci/ppb.c, revision 1.16).
     65   1.1    bouyer  *
     66   1.1    bouyer  * See "PCI IDE Controller Specification, Revision 1.0 3/4/94" and
     67   1.1    bouyer  * "Programming Interface for Bus Master IDE Controller, Revision 1.0
     68   1.1    bouyer  * 5/16/94" from the PCI SIG.
     69   1.1    bouyer  *
     70   1.1    bouyer  */
     71   1.1    bouyer 
     72   1.1    bouyer #include <sys/cdefs.h>
     73  1.71   thorpej __KERNEL_RCSID(0, "$NetBSD: pciide_common.c,v 1.71 2024/03/31 18:59:52 thorpej Exp $");
     74   1.1    bouyer 
     75   1.1    bouyer #include <sys/param.h>
     76   1.1    bouyer 
     77   1.1    bouyer #include <dev/pci/pcireg.h>
     78   1.1    bouyer #include <dev/pci/pcivar.h>
     79   1.1    bouyer #include <dev/pci/pcidevs.h>
     80   1.1    bouyer #include <dev/pci/pciidereg.h>
     81   1.1    bouyer #include <dev/pci/pciidevar.h>
     82   1.1    bouyer 
     83   1.3      fvdl #include <dev/ic/wdcreg.h>
     84   1.3      fvdl 
     85  1.16   thorpej #ifdef ATADEBUG
     86  1.16   thorpej int atadebug_pciide_mask = 0;
     87   1.1    bouyer #endif
     88   1.1    bouyer 
     89  1.33     itohy #if NATA_DMA
     90  1.26     perry static const char dmaerrfmt[] =
     91   1.1    bouyer     "%s:%d: unable to %s table DMA map for drive %d, error=%d\n";
     92  1.33     itohy #endif
     93   1.1    bouyer 
     94   1.1    bouyer /* Default product description for devices not known from this controller */
     95   1.1    bouyer const struct pciide_product_desc default_product_desc = {
     96   1.1    bouyer 	0,
     97   1.1    bouyer 	0,
     98   1.1    bouyer 	"Generic PCI IDE controller",
     99   1.1    bouyer 	default_chip_map,
    100  1.26     perry };
    101   1.1    bouyer 
    102   1.1    bouyer const struct pciide_product_desc *
    103  1.39       dsl pciide_lookup_product(pcireg_t id, const struct pciide_product_desc *pp)
    104   1.1    bouyer {
    105   1.1    bouyer 	for (; pp->chip_map != NULL; pp++)
    106   1.1    bouyer 		if (PCI_PRODUCT(id) == pp->ide_product)
    107   1.1    bouyer 			break;
    108  1.26     perry 
    109   1.1    bouyer 	if (pp->chip_map == NULL)
    110   1.1    bouyer 		return NULL;
    111   1.1    bouyer 	return pp;
    112   1.1    bouyer }
    113   1.1    bouyer 
    114   1.1    bouyer void
    115  1.61   msaitoh pciide_common_attach(struct pciide_softc *sc, const struct pci_attach_args *pa,
    116  1.61   msaitoh     const struct pciide_product_desc *pp)
    117   1.1    bouyer {
    118   1.1    bouyer 	pci_chipset_tag_t pc = pa->pa_pc;
    119   1.1    bouyer 	pcitag_t tag = pa->pa_tag;
    120  1.33     itohy #if NATA_DMA
    121   1.1    bouyer 	pcireg_t csr;
    122  1.33     itohy #endif
    123  1.52  drochner 	const char *displaydev = NULL;
    124  1.52  drochner 	int dontprint = 0;
    125   1.1    bouyer 
    126   1.1    bouyer 	sc->sc_pci_id = pa->pa_id;
    127   1.1    bouyer 	if (pp == NULL) {
    128   1.1    bouyer 		/* should only happen for generic pciide devices */
    129   1.1    bouyer 		sc->sc_pp = &default_product_desc;
    130   1.1    bouyer 	} else {
    131   1.1    bouyer 		sc->sc_pp = pp;
    132  1.52  drochner 		/* if ide_name == NULL, printf is done in chip-specific map */
    133  1.52  drochner 		if (pp->ide_name)
    134  1.52  drochner 			displaydev = pp->ide_name;
    135  1.52  drochner 		else
    136  1.52  drochner 			dontprint = 1;
    137   1.1    bouyer 	}
    138   1.1    bouyer 
    139  1.52  drochner 	if (dontprint) {
    140  1.52  drochner 		aprint_naive("disk controller\n");
    141  1.52  drochner 		aprint_normal("\n"); /* ??? */
    142  1.52  drochner 	} else
    143  1.52  drochner 		pci_aprint_devinfo_fancy(pa, "disk controller", displaydev, 1);
    144   1.1    bouyer 
    145   1.1    bouyer 	sc->sc_pc = pa->pa_pc;
    146   1.1    bouyer 	sc->sc_tag = pa->pa_tag;
    147   1.1    bouyer 
    148  1.33     itohy #if NATA_DMA
    149   1.1    bouyer 	/* Set up DMA defaults; these might be adjusted by chip_map. */
    150   1.1    bouyer 	sc->sc_dma_maxsegsz = IDEDMA_BYTE_COUNT_MAX;
    151   1.1    bouyer 	sc->sc_dma_boundary = IDEDMA_BYTE_COUNT_ALIGN;
    152  1.33     itohy #endif
    153   1.1    bouyer 
    154  1.16   thorpej #ifdef ATADEBUG
    155  1.16   thorpej 	if (atadebug_pciide_mask & DEBUG_PROBE)
    156   1.1    bouyer 		pci_conf_print(sc->sc_pc, sc->sc_tag, NULL);
    157   1.1    bouyer #endif
    158   1.1    bouyer 	sc->sc_pp->chip_map(sc, pa);
    159   1.1    bouyer 
    160  1.33     itohy #if NATA_DMA
    161   1.1    bouyer 	if (sc->sc_dma_ok) {
    162   1.1    bouyer 		csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    163   1.1    bouyer 		csr |= PCI_COMMAND_MASTER_ENABLE;
    164   1.1    bouyer 		pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
    165   1.1    bouyer 	}
    166  1.33     itohy #endif
    167  1.16   thorpej 	ATADEBUG_PRINT(("pciide: command/status register=%x\n",
    168   1.1    bouyer 	    pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG)), DEBUG_PROBE);
    169   1.1    bouyer }
    170   1.1    bouyer 
    171  1.44  jakllsch int
    172  1.44  jakllsch pciide_common_detach(struct pciide_softc *sc, int flags)
    173  1.44  jakllsch {
    174  1.44  jakllsch 	struct pciide_channel *cp;
    175  1.44  jakllsch 	struct ata_channel *wdc_cp;
    176  1.44  jakllsch 	struct wdc_regs *wdr;
    177  1.44  jakllsch 	int channel, drive;
    178  1.44  jakllsch 	int rv;
    179  1.44  jakllsch 
    180  1.44  jakllsch 	rv = wdcdetach(sc->sc_wdcdev.sc_atac.atac_dev, flags);
    181  1.44  jakllsch 	if (rv)
    182  1.44  jakllsch 		return rv;
    183  1.44  jakllsch 
    184  1.44  jakllsch 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
    185  1.44  jakllsch 	     channel++) {
    186  1.44  jakllsch 		cp = &sc->pciide_channels[channel];
    187  1.44  jakllsch 		wdc_cp = &cp->ata_channel;
    188  1.44  jakllsch 		wdr = CHAN_TO_WDC_REGS(wdc_cp);
    189  1.44  jakllsch 
    190  1.44  jakllsch 		if (wdc_cp->ch_flags & ATACH_DISABLED)
    191  1.44  jakllsch 			continue;
    192  1.44  jakllsch 
    193  1.44  jakllsch 		if (wdr->cmd_ios != 0)
    194  1.44  jakllsch 			bus_space_unmap(wdr->cmd_iot,
    195  1.44  jakllsch 			    wdr->cmd_baseioh, wdr->cmd_ios);
    196  1.44  jakllsch 		if (cp->compat != 0) {
    197  1.44  jakllsch 			if (wdr->ctl_ios != 0)
    198  1.44  jakllsch 				bus_space_unmap(wdr->ctl_iot,
    199  1.44  jakllsch 				    wdr->ctl_ioh, wdr->ctl_ios);
    200  1.44  jakllsch 		} else {
    201  1.44  jakllsch 			if (cp->ctl_ios != 0)
    202  1.44  jakllsch 				bus_space_unmap(wdr->ctl_iot,
    203  1.44  jakllsch 				    cp->ctl_baseioh, cp->ctl_ios);
    204  1.44  jakllsch 		}
    205  1.44  jakllsch 
    206  1.57    bouyer 		for (drive = 0; drive < sc->sc_wdcdev.wdc_maxdrives; drive++) {
    207  1.48  dholland #if NATA_DMA
    208  1.44  jakllsch 			pciide_dma_table_teardown(sc, channel, drive);
    209  1.48  dholland #endif
    210  1.44  jakllsch 		}
    211  1.44  jakllsch 	}
    212  1.44  jakllsch 
    213  1.48  dholland #if NATA_DMA
    214  1.44  jakllsch 	if (sc->sc_dma_ios != 0)
    215  1.44  jakllsch 		bus_space_unmap(sc->sc_dma_iot, sc->sc_dma_ioh, sc->sc_dma_ios);
    216  1.44  jakllsch 	if (sc->sc_ba5_ss != 0)
    217  1.45  jakllsch 		bus_space_unmap(sc->sc_ba5_st, sc->sc_ba5_sh, sc->sc_ba5_ss);
    218  1.48  dholland #endif
    219  1.44  jakllsch 
    220  1.44  jakllsch 	return 0;
    221  1.44  jakllsch }
    222  1.44  jakllsch 
    223  1.46  jakllsch int
    224  1.46  jakllsch pciide_detach(device_t self, int flags)
    225  1.46  jakllsch {
    226  1.46  jakllsch 	struct pciide_softc *sc = device_private(self);
    227  1.46  jakllsch 	struct pciide_channel *cp;
    228  1.46  jakllsch 	int channel;
    229  1.46  jakllsch #ifndef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_DISESTABLISH
    230  1.46  jakllsch 	bool has_compat_chan;
    231  1.46  jakllsch 
    232  1.46  jakllsch 	has_compat_chan = false;
    233  1.46  jakllsch 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
    234  1.46  jakllsch 	     channel++) {
    235  1.46  jakllsch 		cp = &sc->pciide_channels[channel];
    236  1.46  jakllsch 		if (cp->compat != 0) {
    237  1.46  jakllsch 			has_compat_chan = true;
    238  1.46  jakllsch 		}
    239  1.46  jakllsch 	}
    240  1.46  jakllsch 
    241  1.46  jakllsch 	if (has_compat_chan != false)
    242  1.46  jakllsch 		return EBUSY;
    243  1.46  jakllsch #endif
    244  1.46  jakllsch 
    245  1.46  jakllsch 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
    246  1.46  jakllsch 	     channel++) {
    247  1.46  jakllsch 		cp = &sc->pciide_channels[channel];
    248  1.46  jakllsch 		if (cp->compat != 0)
    249  1.58  jakllsch 			if (cp->ih != NULL) {
    250  1.46  jakllsch 			       pciide_unmap_compat_intr(sc->sc_pc, cp, channel);
    251  1.58  jakllsch 			       cp->ih = NULL;
    252  1.58  jakllsch 			}
    253  1.46  jakllsch 	}
    254  1.46  jakllsch 
    255  1.58  jakllsch 	if (sc->sc_pci_ih != NULL) {
    256  1.46  jakllsch 		pci_intr_disestablish(sc->sc_pc, sc->sc_pci_ih);
    257  1.58  jakllsch 		sc->sc_pci_ih = NULL;
    258  1.58  jakllsch 	}
    259  1.46  jakllsch 
    260  1.46  jakllsch 	return pciide_common_detach(sc, flags);
    261  1.46  jakllsch }
    262  1.46  jakllsch 
    263   1.1    bouyer /* tell whether the chip is enabled or not */
    264   1.1    bouyer int
    265  1.49    dyoung pciide_chipen(struct pciide_softc *sc, const struct pci_attach_args *pa)
    266   1.1    bouyer {
    267   1.1    bouyer 	pcireg_t csr;
    268   1.1    bouyer 
    269  1.51    dyoung 	if ((pa->pa_flags & PCI_FLAGS_IO_OKAY) == 0) {
    270  1.38      cube 		aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    271  1.50    dyoung 		    "I/O access disabled at bridge\n");
    272  1.50    dyoung 		return 0;
    273  1.50    dyoung 	}
    274  1.50    dyoung 	csr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG);
    275  1.50    dyoung 	if ((csr & PCI_COMMAND_IO_ENABLE) == 0) {
    276  1.50    dyoung 		aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    277  1.50    dyoung 		    "I/O access disabled at device\n");
    278   1.1    bouyer 		return 0;
    279   1.1    bouyer 	}
    280   1.1    bouyer 	return 1;
    281   1.1    bouyer }
    282   1.1    bouyer 
    283   1.1    bouyer void
    284  1.61   msaitoh pciide_mapregs_compat(const struct pci_attach_args *pa,
    285  1.61   msaitoh     struct pciide_channel *cp, int compatchan)
    286   1.1    bouyer {
    287  1.19   thorpej 	struct pciide_softc *sc = CHAN_TO_PCIIDE(&cp->ata_channel);
    288  1.17   thorpej 	struct ata_channel *wdc_cp = &cp->ata_channel;
    289  1.19   thorpej 	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(wdc_cp);
    290   1.3      fvdl 	int i;
    291   1.1    bouyer 
    292   1.1    bouyer 	cp->compat = 1;
    293   1.1    bouyer 
    294  1.17   thorpej 	wdr->cmd_iot = pa->pa_iot;
    295  1.17   thorpej 	if (bus_space_map(wdr->cmd_iot, PCIIDE_COMPAT_CMD_BASE(compatchan),
    296  1.17   thorpej 	    PCIIDE_COMPAT_CMD_SIZE, 0, &wdr->cmd_baseioh) != 0) {
    297  1.38      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    298  1.38      cube 		    "couldn't map %s channel cmd regs\n", cp->name);
    299   1.1    bouyer 		goto bad;
    300   1.1    bouyer 	}
    301  1.44  jakllsch 	wdr->cmd_ios = PCIIDE_COMPAT_CMD_SIZE;
    302   1.1    bouyer 
    303  1.17   thorpej 	wdr->ctl_iot = pa->pa_iot;
    304  1.17   thorpej 	if (bus_space_map(wdr->ctl_iot, PCIIDE_COMPAT_CTL_BASE(compatchan),
    305  1.17   thorpej 	    PCIIDE_COMPAT_CTL_SIZE, 0, &wdr->ctl_ioh) != 0) {
    306  1.38      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    307  1.38      cube 		    "couldn't map %s channel ctl regs\n", cp->name);
    308  1.44  jakllsch 		bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, wdr->cmd_ios);
    309   1.1    bouyer 		goto bad;
    310   1.1    bouyer 	}
    311  1.44  jakllsch 	wdr->ctl_ios = PCIIDE_COMPAT_CTL_SIZE;
    312   1.1    bouyer 
    313   1.3      fvdl 	for (i = 0; i < WDC_NREG; i++) {
    314  1.17   thorpej 		if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh, i,
    315  1.17   thorpej 		    i == 0 ? 4 : 1, &wdr->cmd_iohs[i]) != 0) {
    316  1.38      cube 			aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    317  1.38      cube 			    "couldn't subregion %s channel cmd regs\n",
    318  1.38      cube 			    cp->name);
    319   1.3      fvdl 			goto bad;
    320   1.3      fvdl 		}
    321   1.3      fvdl 	}
    322  1.63  jdolecek 	wdc_init_shadow_regs(wdr);
    323  1.17   thorpej 	wdr->data32iot = wdr->cmd_iot;
    324  1.17   thorpej 	wdr->data32ioh = wdr->cmd_iohs[0];
    325   1.1    bouyer 	return;
    326   1.1    bouyer 
    327   1.1    bouyer bad:
    328  1.17   thorpej 	cp->ata_channel.ch_flags |= ATACH_DISABLED;
    329   1.1    bouyer 	return;
    330   1.1    bouyer }
    331   1.1    bouyer 
    332   1.1    bouyer void
    333  1.49    dyoung pciide_mapregs_native(const struct pci_attach_args *pa,
    334  1.44  jakllsch 	struct pciide_channel *cp, int (*pci_intr)(void *))
    335   1.1    bouyer {
    336  1.19   thorpej 	struct pciide_softc *sc = CHAN_TO_PCIIDE(&cp->ata_channel);
    337  1.17   thorpej 	struct ata_channel *wdc_cp = &cp->ata_channel;
    338  1.19   thorpej 	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(wdc_cp);
    339   1.1    bouyer 	const char *intrstr;
    340   1.1    bouyer 	pci_intr_handle_t intrhandle;
    341   1.3      fvdl 	int i;
    342  1.60  christos 	char intrbuf[PCI_INTRSTR_LEN];
    343   1.1    bouyer 
    344   1.1    bouyer 	cp->compat = 0;
    345   1.1    bouyer 
    346   1.1    bouyer 	if (sc->sc_pci_ih == NULL) {
    347   1.1    bouyer 		if (pci_intr_map(pa, &intrhandle) != 0) {
    348  1.38      cube 			aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    349  1.38      cube 			    "couldn't map native-PCI interrupt\n");
    350   1.1    bouyer 			goto bad;
    351  1.26     perry 		}
    352  1.60  christos 		intrstr = pci_intr_string(pa->pa_pc, intrhandle, intrbuf, sizeof(intrbuf));
    353  1.62  jdolecek 		sc->sc_pci_ih = pci_intr_establish_xname(pa->pa_pc,
    354  1.62  jdolecek 		    intrhandle, IPL_BIO, pci_intr, sc,
    355  1.62  jdolecek 		    device_xname(sc->sc_wdcdev.sc_atac.atac_dev));
    356   1.1    bouyer 		if (sc->sc_pci_ih != NULL) {
    357  1.38      cube 			aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    358  1.38      cube 			    "using %s for native-PCI interrupt\n",
    359   1.1    bouyer 			    intrstr ? intrstr : "unknown interrupt");
    360   1.1    bouyer 		} else {
    361  1.38      cube 			aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    362  1.38      cube 			    "couldn't establish native-PCI interrupt");
    363   1.1    bouyer 			if (intrstr != NULL)
    364  1.38      cube 				aprint_error(" at %s", intrstr);
    365  1.38      cube 			aprint_error("\n");
    366   1.1    bouyer 			goto bad;
    367   1.1    bouyer 		}
    368   1.1    bouyer 	}
    369   1.1    bouyer 	cp->ih = sc->sc_pci_ih;
    370   1.8   thorpej 	if (pci_mapreg_map(pa, PCIIDE_REG_CMD_BASE(wdc_cp->ch_channel),
    371   1.1    bouyer 	    PCI_MAPREG_TYPE_IO, 0,
    372  1.44  jakllsch 	    &wdr->cmd_iot, &wdr->cmd_baseioh, NULL, &wdr->cmd_ios) != 0) {
    373  1.38      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    374  1.38      cube 		    "couldn't map %s channel cmd regs\n", cp->name);
    375   1.1    bouyer 		goto bad;
    376   1.1    bouyer 	}
    377   1.1    bouyer 
    378   1.8   thorpej 	if (pci_mapreg_map(pa, PCIIDE_REG_CTL_BASE(wdc_cp->ch_channel),
    379   1.1    bouyer 	    PCI_MAPREG_TYPE_IO, 0,
    380  1.44  jakllsch 	    &wdr->ctl_iot, &cp->ctl_baseioh, NULL, &cp->ctl_ios) != 0) {
    381  1.38      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    382  1.38      cube 		    "couldn't map %s channel ctl regs\n", cp->name);
    383  1.44  jakllsch 		bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, wdr->cmd_ios);
    384   1.1    bouyer 		goto bad;
    385   1.1    bouyer 	}
    386   1.1    bouyer 	/*
    387   1.1    bouyer 	 * In native mode, 4 bytes of I/O space are mapped for the control
    388   1.1    bouyer 	 * register, the control register is at offset 2. Pass the generic
    389   1.1    bouyer 	 * code a handle for only one byte at the right offset.
    390   1.1    bouyer 	 */
    391  1.17   thorpej 	if (bus_space_subregion(wdr->ctl_iot, cp->ctl_baseioh, 2, 1,
    392  1.17   thorpej 	    &wdr->ctl_ioh) != 0) {
    393  1.38      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    394  1.38      cube 		    "unable to subregion %s channel ctl regs\n", cp->name);
    395  1.44  jakllsch 		bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, wdr->cmd_ios);
    396  1.44  jakllsch 		bus_space_unmap(wdr->cmd_iot, cp->ctl_baseioh, cp->ctl_ios);
    397   1.1    bouyer 		goto bad;
    398   1.1    bouyer 	}
    399   1.1    bouyer 
    400   1.3      fvdl 	for (i = 0; i < WDC_NREG; i++) {
    401  1.17   thorpej 		if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh, i,
    402  1.17   thorpej 		    i == 0 ? 4 : 1, &wdr->cmd_iohs[i]) != 0) {
    403  1.38      cube 			aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    404  1.38      cube 			    "couldn't subregion %s channel cmd regs\n",
    405  1.38      cube 			    cp->name);
    406   1.3      fvdl 			goto bad;
    407   1.3      fvdl 		}
    408   1.3      fvdl 	}
    409  1.63  jdolecek 	wdc_init_shadow_regs(wdr);
    410  1.17   thorpej 	wdr->data32iot = wdr->cmd_iot;
    411  1.17   thorpej 	wdr->data32ioh = wdr->cmd_iohs[0];
    412   1.1    bouyer 	return;
    413   1.1    bouyer 
    414   1.1    bouyer bad:
    415  1.17   thorpej 	cp->ata_channel.ch_flags |= ATACH_DISABLED;
    416   1.1    bouyer 	return;
    417   1.1    bouyer }
    418   1.1    bouyer 
    419  1.33     itohy #if NATA_DMA
    420   1.1    bouyer void
    421  1.49    dyoung pciide_mapreg_dma(struct pciide_softc *sc, const struct pci_attach_args *pa)
    422   1.1    bouyer {
    423   1.1    bouyer 	pcireg_t maptype;
    424   1.1    bouyer 	bus_addr_t addr;
    425   1.3      fvdl 	struct pciide_channel *pc;
    426   1.3      fvdl 	int reg, chan;
    427   1.3      fvdl 	bus_size_t size;
    428   1.1    bouyer 
    429   1.1    bouyer 	/*
    430   1.1    bouyer 	 * Map DMA registers
    431   1.1    bouyer 	 *
    432   1.1    bouyer 	 * Note that sc_dma_ok is the right variable to test to see if
    433   1.1    bouyer 	 * DMA can be done.  If the interface doesn't support DMA,
    434   1.1    bouyer 	 * sc_dma_ok will never be non-zero.  If the DMA regs couldn't
    435   1.1    bouyer 	 * be mapped, it'll be zero.  I.e., sc_dma_ok will only be
    436   1.1    bouyer 	 * non-zero if the interface supports DMA and the registers
    437   1.1    bouyer 	 * could be mapped.
    438   1.1    bouyer 	 *
    439   1.1    bouyer 	 * XXX Note that despite the fact that the Bus Master IDE specs
    440   1.1    bouyer 	 * XXX say that "The bus master IDE function uses 16 bytes of IO
    441   1.1    bouyer 	 * XXX space," some controllers (at least the United
    442   1.1    bouyer 	 * XXX Microelectronics UM8886BF) place it in memory space.
    443   1.1    bouyer 	 */
    444   1.1    bouyer 	maptype = pci_mapreg_type(pa->pa_pc, pa->pa_tag,
    445   1.1    bouyer 	    PCIIDE_REG_BUS_MASTER_DMA);
    446   1.1    bouyer 
    447   1.1    bouyer 	switch (maptype) {
    448   1.1    bouyer 	case PCI_MAPREG_TYPE_IO:
    449   1.1    bouyer 		sc->sc_dma_ok = (pci_mapreg_info(pa->pa_pc, pa->pa_tag,
    450   1.1    bouyer 		    PCIIDE_REG_BUS_MASTER_DMA, PCI_MAPREG_TYPE_IO,
    451   1.1    bouyer 		    &addr, NULL, NULL) == 0);
    452   1.1    bouyer 		if (sc->sc_dma_ok == 0) {
    453  1.36        ad 			aprint_verbose(
    454   1.1    bouyer 			    ", but unused (couldn't query registers)");
    455   1.1    bouyer 			break;
    456   1.1    bouyer 		}
    457   1.1    bouyer 		if ((sc->sc_pp->ide_flags & IDE_16BIT_IOSPACE)
    458   1.1    bouyer 		    && addr >= 0x10000) {
    459   1.1    bouyer 			sc->sc_dma_ok = 0;
    460  1.36        ad 			aprint_verbose(
    461   1.1    bouyer 			    ", but unused (registers at unsafe address "
    462   1.1    bouyer 			    "%#lx)", (unsigned long)addr);
    463   1.1    bouyer 			break;
    464   1.1    bouyer 		}
    465   1.1    bouyer 		/* FALLTHROUGH */
    466  1.26     perry 
    467   1.1    bouyer 	case PCI_MAPREG_MEM_TYPE_32BIT:
    468   1.1    bouyer 		sc->sc_dma_ok = (pci_mapreg_map(pa,
    469   1.1    bouyer 		    PCIIDE_REG_BUS_MASTER_DMA, maptype, 0,
    470  1.44  jakllsch 		    &sc->sc_dma_iot, &sc->sc_dma_ioh, NULL, &sc->sc_dma_ios)
    471  1.44  jakllsch 		    == 0);
    472   1.1    bouyer 		sc->sc_dmat = pa->pa_dmat;
    473   1.1    bouyer 		if (sc->sc_dma_ok == 0) {
    474  1.36        ad 			aprint_verbose(", but unused (couldn't map registers)");
    475   1.1    bouyer 		} else {
    476   1.1    bouyer 			sc->sc_wdcdev.dma_arg = sc;
    477   1.1    bouyer 			sc->sc_wdcdev.dma_init = pciide_dma_init;
    478   1.1    bouyer 			sc->sc_wdcdev.dma_start = pciide_dma_start;
    479   1.1    bouyer 			sc->sc_wdcdev.dma_finish = pciide_dma_finish;
    480   1.1    bouyer 		}
    481   1.1    bouyer 
    482  1.38      cube 		if (device_cfdata(sc->sc_wdcdev.sc_atac.atac_dev)->cf_flags &
    483   1.1    bouyer 		    PCIIDE_OPTIONS_NODMA) {
    484  1.36        ad 			aprint_verbose(
    485   1.1    bouyer 			    ", but unused (forced off by config file)");
    486   1.1    bouyer 			sc->sc_dma_ok = 0;
    487  1.71   thorpej 		} else {
    488  1.71   thorpej 			bool disable;
    489  1.71   thorpej 
    490  1.71   thorpej 			if (prop_dictionary_get_bool(
    491  1.71   thorpej 			    device_properties(sc->sc_wdcdev.sc_atac.atac_dev),
    492  1.71   thorpej 			    "pciide-disable-dma", &disable) && disable) {
    493  1.71   thorpej 				aprint_verbose(
    494  1.71   thorpej 				    ", but unused (disabled by platform)");
    495  1.71   thorpej 				sc->sc_dma_ok = 0;
    496  1.71   thorpej 			}
    497   1.1    bouyer 		}
    498   1.1    bouyer 		break;
    499   1.1    bouyer 
    500   1.1    bouyer 	default:
    501   1.1    bouyer 		sc->sc_dma_ok = 0;
    502  1.36        ad 		aprint_verbose(
    503   1.1    bouyer 		    ", but unsupported register maptype (0x%x)", maptype);
    504   1.1    bouyer 	}
    505   1.3      fvdl 
    506  1.12    bouyer 	if (sc->sc_dma_ok == 0)
    507  1.12    bouyer 		return;
    508  1.12    bouyer 
    509   1.3      fvdl 	/*
    510   1.3      fvdl 	 * Set up the default handles for the DMA registers.
    511   1.3      fvdl 	 * Just reserve 32 bits for each handle, unless space
    512   1.3      fvdl 	 * doesn't permit it.
    513   1.3      fvdl 	 */
    514   1.3      fvdl 	for (chan = 0; chan < PCIIDE_NUM_CHANNELS; chan++) {
    515   1.3      fvdl 		pc = &sc->pciide_channels[chan];
    516   1.3      fvdl 		for (reg = 0; reg < IDEDMA_NREGS; reg++) {
    517   1.3      fvdl 			size = 4;
    518   1.3      fvdl 			if (size > (IDEDMA_SCH_OFFSET - reg))
    519   1.3      fvdl 				size = IDEDMA_SCH_OFFSET - reg;
    520   1.3      fvdl 			if (bus_space_subregion(sc->sc_dma_iot, sc->sc_dma_ioh,
    521   1.3      fvdl 			    IDEDMA_SCH_OFFSET * chan + reg, size,
    522   1.3      fvdl 			    &pc->dma_iohs[reg]) != 0) {
    523   1.3      fvdl 				sc->sc_dma_ok = 0;
    524  1.36        ad 				aprint_verbose(", but can't subregion offset %d "
    525   1.3      fvdl 					      "size %lu", reg, (u_long)size);
    526   1.3      fvdl 				return;
    527   1.3      fvdl 			}
    528   1.3      fvdl 		}
    529   1.3      fvdl 	}
    530   1.1    bouyer }
    531  1.33     itohy #endif	/* NATA_DMA */
    532   1.1    bouyer 
    533   1.1    bouyer int
    534  1.39       dsl pciide_compat_intr(void *arg)
    535   1.1    bouyer {
    536   1.1    bouyer 	struct pciide_channel *cp = arg;
    537   1.1    bouyer 
    538   1.1    bouyer #ifdef DIAGNOSTIC
    539   1.1    bouyer 	/* should only be called for a compat channel */
    540   1.1    bouyer 	if (cp->compat == 0)
    541   1.1    bouyer 		panic("pciide compat intr called for non-compat chan %p", cp);
    542   1.1    bouyer #endif
    543  1.17   thorpej 	return (wdcintr(&cp->ata_channel));
    544   1.1    bouyer }
    545   1.1    bouyer 
    546   1.1    bouyer int
    547  1.39       dsl pciide_pci_intr(void *arg)
    548   1.1    bouyer {
    549   1.1    bouyer 	struct pciide_softc *sc = arg;
    550   1.1    bouyer 	struct pciide_channel *cp;
    551  1.17   thorpej 	struct ata_channel *wdc_cp;
    552   1.1    bouyer 	int i, rv, crv;
    553   1.1    bouyer 
    554   1.1    bouyer 	rv = 0;
    555  1.20   thorpej 	for (i = 0; i < sc->sc_wdcdev.sc_atac.atac_nchannels; i++) {
    556   1.1    bouyer 		cp = &sc->pciide_channels[i];
    557  1.17   thorpej 		wdc_cp = &cp->ata_channel;
    558   1.1    bouyer 
    559   1.1    bouyer 		/* If a compat channel skip. */
    560   1.1    bouyer 		if (cp->compat)
    561   1.1    bouyer 			continue;
    562   1.1    bouyer 
    563  1.64  jdolecek 		/* if this channel not waiting for intr, skip */
    564  1.64  jdolecek 		if ((wdc_cp->ch_flags & ATACH_IRQ_WAIT) == 0)
    565  1.64  jdolecek 			continue;
    566  1.64  jdolecek 
    567   1.1    bouyer 		crv = wdcintr(wdc_cp);
    568   1.1    bouyer 		if (crv == 0)
    569   1.1    bouyer 			;		/* leave rv alone */
    570   1.1    bouyer 		else if (crv == 1)
    571   1.1    bouyer 			rv = 1;		/* claim the intr */
    572   1.1    bouyer 		else if (rv == 0)	/* crv should be -1 in this case */
    573   1.1    bouyer 			rv = crv;	/* if we've done no better, take it */
    574   1.1    bouyer 	}
    575   1.1    bouyer 	return (rv);
    576   1.1    bouyer }
    577   1.1    bouyer 
    578  1.33     itohy #if NATA_DMA
    579   1.1    bouyer void
    580  1.39       dsl pciide_channel_dma_setup(struct pciide_channel *cp)
    581   1.1    bouyer {
    582  1.21   thorpej 	int drive, s;
    583  1.19   thorpej 	struct pciide_softc *sc = CHAN_TO_PCIIDE(&cp->ata_channel);
    584   1.1    bouyer 	struct ata_drive_datas *drvp;
    585   1.1    bouyer 
    586  1.57    bouyer 	KASSERT(cp->ata_channel.ch_ndrives != 0);
    587  1.17   thorpej 
    588  1.57    bouyer 	for (drive = 0; drive < cp->ata_channel.ch_ndrives; drive++) {
    589  1.17   thorpej 		drvp = &cp->ata_channel.ch_drive[drive];
    590   1.1    bouyer 		/* If no drive, skip */
    591  1.57    bouyer 		if (drvp->drive_type == ATA_DRIVET_NONE)
    592   1.1    bouyer 			continue;
    593   1.1    bouyer 		/* setup DMA if needed */
    594  1.57    bouyer 		if (((drvp->drive_flags & ATA_DRIVE_DMA) == 0 &&
    595  1.57    bouyer 		    (drvp->drive_flags & ATA_DRIVE_UDMA) == 0) ||
    596   1.1    bouyer 		    sc->sc_dma_ok == 0) {
    597  1.21   thorpej 			s = splbio();
    598  1.57    bouyer 			drvp->drive_flags &= ~(ATA_DRIVE_DMA | ATA_DRIVE_UDMA);
    599  1.21   thorpej 			splx(s);
    600   1.1    bouyer 			continue;
    601   1.1    bouyer 		}
    602  1.17   thorpej 		if (pciide_dma_table_setup(sc, cp->ata_channel.ch_channel,
    603   1.8   thorpej 					   drive) != 0) {
    604   1.1    bouyer 			/* Abort DMA setup */
    605  1.21   thorpej 			s = splbio();
    606  1.57    bouyer 			drvp->drive_flags &= ~(ATA_DRIVE_DMA | ATA_DRIVE_UDMA);
    607  1.21   thorpej 			splx(s);
    608   1.1    bouyer 			continue;
    609   1.1    bouyer 		}
    610   1.1    bouyer 	}
    611   1.1    bouyer }
    612   1.1    bouyer 
    613  1.24    briggs #define NIDEDMA_TABLES(sc)	\
    614  1.66  riastrad 	(MAXPHYS/(uimin((sc)->sc_dma_maxsegsz, PAGE_SIZE)) + 1)
    615  1.24    briggs 
    616   1.1    bouyer int
    617  1.40       dsl pciide_dma_table_setup(struct pciide_softc *sc, int channel, int drive)
    618   1.1    bouyer {
    619  1.44  jakllsch 	int error;
    620   1.1    bouyer 	const bus_size_t dma_table_size =
    621  1.24    briggs 	    sizeof(struct idedma_table) * NIDEDMA_TABLES(sc);
    622   1.1    bouyer 	struct pciide_dma_maps *dma_maps =
    623   1.1    bouyer 	    &sc->pciide_channels[channel].dma_maps[drive];
    624   1.1    bouyer 
    625   1.1    bouyer 	/* If table was already allocated, just return */
    626   1.1    bouyer 	if (dma_maps->dma_table)
    627   1.1    bouyer 		return 0;
    628   1.1    bouyer 
    629   1.1    bouyer 	/* Allocate memory for the DMA tables and map it */
    630   1.1    bouyer 	if ((error = bus_dmamem_alloc(sc->sc_dmat, dma_table_size,
    631  1.44  jakllsch 	    IDEDMA_TBL_ALIGN, IDEDMA_TBL_ALIGN, &dma_maps->dmamap_table_seg,
    632  1.44  jakllsch 	    1, &dma_maps->dmamap_table_nseg, BUS_DMA_NOWAIT)) != 0) {
    633  1.38      cube 		aprint_error(dmaerrfmt,
    634  1.38      cube 		    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel,
    635  1.38      cube 		    "allocate", drive, error);
    636   1.1    bouyer 		return error;
    637   1.1    bouyer 	}
    638  1.44  jakllsch 	if ((error = bus_dmamem_map(sc->sc_dmat, &dma_maps->dmamap_table_seg,
    639  1.44  jakllsch 	    dma_maps->dmamap_table_nseg, dma_table_size,
    640  1.37  christos 	    (void **)&dma_maps->dma_table,
    641   1.1    bouyer 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    642  1.38      cube 		aprint_error(dmaerrfmt,
    643  1.38      cube 		    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel,
    644  1.38      cube 		    "map", drive, error);
    645   1.1    bouyer 		return error;
    646   1.1    bouyer 	}
    647  1.16   thorpej 	ATADEBUG_PRINT(("pciide_dma_table_setup: table at %p len %lu, "
    648   1.1    bouyer 	    "phy 0x%lx\n", dma_maps->dma_table, (u_long)dma_table_size,
    649  1.44  jakllsch 	    (unsigned long)dma_maps->dmamap_table_seg.ds_addr), DEBUG_PROBE);
    650   1.1    bouyer 	/* Create and load table DMA map for this disk */
    651   1.1    bouyer 	if ((error = bus_dmamap_create(sc->sc_dmat, dma_table_size,
    652   1.1    bouyer 	    1, dma_table_size, IDEDMA_TBL_ALIGN, BUS_DMA_NOWAIT,
    653   1.1    bouyer 	    &dma_maps->dmamap_table)) != 0) {
    654  1.38      cube 		aprint_error(dmaerrfmt,
    655  1.38      cube 		    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel,
    656  1.38      cube 		    "create", drive, error);
    657   1.1    bouyer 		return error;
    658   1.1    bouyer 	}
    659   1.1    bouyer 	if ((error = bus_dmamap_load(sc->sc_dmat,
    660   1.1    bouyer 	    dma_maps->dmamap_table,
    661   1.1    bouyer 	    dma_maps->dma_table,
    662   1.1    bouyer 	    dma_table_size, NULL, BUS_DMA_NOWAIT)) != 0) {
    663  1.38      cube 		aprint_error(dmaerrfmt,
    664  1.38      cube 		    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel,
    665  1.38      cube 		    "load", drive, error);
    666   1.1    bouyer 		return error;
    667   1.1    bouyer 	}
    668  1.16   thorpej 	ATADEBUG_PRINT(("pciide_dma_table_setup: phy addr of table 0x%lx\n",
    669   1.1    bouyer 	    (unsigned long)dma_maps->dmamap_table->dm_segs[0].ds_addr),
    670   1.1    bouyer 	    DEBUG_PROBE);
    671   1.1    bouyer 	/* Create a xfer DMA map for this drive */
    672  1.25    briggs 	if ((error = bus_dmamap_create(sc->sc_dmat, MAXPHYS,
    673  1.24    briggs 	    NIDEDMA_TABLES(sc), sc->sc_dma_maxsegsz, sc->sc_dma_boundary,
    674   1.1    bouyer 	    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    675   1.1    bouyer 	    &dma_maps->dmamap_xfer)) != 0) {
    676  1.38      cube 		aprint_error(dmaerrfmt,
    677  1.38      cube 		    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel,
    678  1.38      cube 		    "create xfer", drive, error);
    679   1.1    bouyer 		return error;
    680   1.1    bouyer 	}
    681   1.1    bouyer 	return 0;
    682   1.1    bouyer }
    683   1.1    bouyer 
    684  1.44  jakllsch void
    685  1.44  jakllsch pciide_dma_table_teardown(struct pciide_softc *sc, int channel, int drive)
    686  1.44  jakllsch {
    687  1.44  jakllsch 	struct pciide_channel *cp;
    688  1.44  jakllsch 	struct pciide_dma_maps *dma_maps;
    689  1.44  jakllsch 
    690  1.44  jakllsch 	cp = &sc->pciide_channels[channel];
    691  1.44  jakllsch 	dma_maps = &cp->dma_maps[drive];
    692  1.44  jakllsch 
    693  1.44  jakllsch 	if (dma_maps->dma_table == NULL)
    694  1.44  jakllsch 		return;
    695  1.44  jakllsch 
    696  1.44  jakllsch 	bus_dmamap_destroy(sc->sc_dmat, dma_maps->dmamap_xfer);
    697  1.44  jakllsch 	bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_table);
    698  1.44  jakllsch 	bus_dmamap_destroy(sc->sc_dmat, dma_maps->dmamap_table);
    699  1.44  jakllsch 	bus_dmamem_unmap(sc->sc_dmat, dma_maps->dma_table,
    700  1.44  jakllsch 	    sizeof(struct idedma_table) * NIDEDMA_TABLES(sc));
    701  1.44  jakllsch 	bus_dmamem_free(sc->sc_dmat, &dma_maps->dmamap_table_seg,
    702  1.44  jakllsch 	    dma_maps->dmamap_table_nseg);
    703  1.44  jakllsch 
    704  1.44  jakllsch 	dma_maps->dma_table = NULL;
    705  1.44  jakllsch 
    706  1.44  jakllsch 	return;
    707  1.44  jakllsch }
    708  1.44  jakllsch 
    709   1.1    bouyer int
    710  1.61   msaitoh pciide_dma_dmamap_setup(struct pciide_softc *sc, int channel, int drive,
    711  1.61   msaitoh     void *databuf, size_t datalen, int flags)
    712   1.1    bouyer {
    713   1.1    bouyer 	int error, seg;
    714   1.3      fvdl 	struct pciide_channel *cp = &sc->pciide_channels[channel];
    715   1.3      fvdl 	struct pciide_dma_maps *dma_maps = &cp->dma_maps[drive];
    716   1.1    bouyer 
    717   1.1    bouyer 	error = bus_dmamap_load(sc->sc_dmat,
    718   1.1    bouyer 	    dma_maps->dmamap_xfer,
    719   1.1    bouyer 	    databuf, datalen, NULL, BUS_DMA_NOWAIT | BUS_DMA_STREAMING |
    720   1.1    bouyer 	    ((flags & WDC_DMA_READ) ? BUS_DMA_READ : BUS_DMA_WRITE));
    721   1.1    bouyer 	if (error) {
    722  1.38      cube 		aprint_error(dmaerrfmt,
    723  1.38      cube 		    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel,
    724  1.38      cube 		    "load xfer", drive, error);
    725   1.1    bouyer 		return error;
    726   1.1    bouyer 	}
    727   1.1    bouyer 
    728   1.1    bouyer 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
    729   1.1    bouyer 	    dma_maps->dmamap_xfer->dm_mapsize,
    730   1.1    bouyer 	    (flags & WDC_DMA_READ) ?
    731   1.1    bouyer 	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    732   1.1    bouyer 
    733   1.1    bouyer 	for (seg = 0; seg < dma_maps->dmamap_xfer->dm_nsegs; seg++) {
    734  1.70   thorpej 		bus_addr_t phys = dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
    735  1.70   thorpej 		bus_size_t len = dma_maps->dmamap_xfer->dm_segs[seg].ds_len;
    736  1.70   thorpej 
    737   1.1    bouyer #ifdef DIAGNOSTIC
    738   1.1    bouyer 		/* A segment must not cross a 64k boundary */
    739   1.1    bouyer 		{
    740   1.1    bouyer 		if ((phys & ~IDEDMA_BYTE_COUNT_MASK) !=
    741   1.1    bouyer 		    ((phys + len - 1) & ~IDEDMA_BYTE_COUNT_MASK)) {
    742  1.70   thorpej 			printf("pciide_dma: seg %d addr 0x%" PRIx64
    743  1.70   thorpej 			    " len 0x%" PRIx64 " not properly aligned\n",
    744  1.70   thorpej 			    seg, (uint64_t)phys, (uint64_t)len);
    745   1.1    bouyer 			panic("pciide_dma: buf align");
    746   1.1    bouyer 		}
    747   1.1    bouyer 		}
    748   1.1    bouyer #endif
    749  1.70   thorpej 		/*
    750  1.70   thorpej 		 * Some controllers get really upset if the length
    751  1.70   thorpej 		 * of any DMA segment is odd.  This isn't something
    752  1.70   thorpej 		 * that's going to happen in normal steady-state
    753  1.70   thorpej 		 * operation (reading VM pages, etc.), but physio users
    754  1.70   thorpej 		 * don't have as many guard rails.
    755  1.70   thorpej 		 *
    756  1.70   thorpej 		 * Consider an 8K read request that starts at an odd
    757  1.70   thorpej 		 * offset within a page.  At first blush, all of the
    758  1.70   thorpej 		 * checks pass because it's a sector-rounded size, but
    759  1.70   thorpej 		 * unless the buffer spans 2 physically contiguous pages,
    760  1.70   thorpej 		 * it's going to result in 2 odd-length DMA segments.
    761  1.70   thorpej 		 *
    762  1.70   thorpej 		 * Odd start addresses are also frowned upon, so we
    763  1.70   thorpej 		 * catch those here, too.
    764  1.70   thorpej 		 *
    765  1.70   thorpej 		 * Returning EINVAL here will cause the upper layers to
    766  1.70   thorpej 		 * fall back onto PIO.
    767  1.70   thorpej 		 */
    768  1.70   thorpej 		if ((phys & 1) != 0 || (len & 1) != 0) {
    769  1.70   thorpej 			aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    770  1.70   thorpej 			    "Invalid DMA segment: "
    771  1.70   thorpej 			    "seg %d addr 0x%" PRIx64 " len 0x%" PRIx64 "\n",
    772  1.70   thorpej 			    seg, (uint64_t)phys, (uint64_t)len);
    773  1.70   thorpej 			bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_xfer);
    774  1.70   thorpej 			return EINVAL;
    775  1.70   thorpej 		}
    776  1.70   thorpej 		dma_maps->dma_table[seg].base_addr = htole32(phys);
    777   1.1    bouyer 		dma_maps->dma_table[seg].byte_count =
    778  1.70   thorpej 		    htole32(len & IDEDMA_BYTE_COUNT_MASK);
    779  1.16   thorpej 		ATADEBUG_PRINT(("\t seg %d len %d addr 0x%x\n",
    780   1.1    bouyer 		   seg, le32toh(dma_maps->dma_table[seg].byte_count),
    781   1.1    bouyer 		   le32toh(dma_maps->dma_table[seg].base_addr)), DEBUG_DMA);
    782   1.1    bouyer 
    783   1.1    bouyer 	}
    784   1.1    bouyer 	dma_maps->dma_table[dma_maps->dmamap_xfer->dm_nsegs -1].byte_count |=
    785   1.1    bouyer 	    htole32(IDEDMA_BYTE_COUNT_EOT);
    786   1.1    bouyer 
    787   1.1    bouyer 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_table, 0,
    788   1.1    bouyer 	    dma_maps->dmamap_table->dm_mapsize,
    789   1.1    bouyer 	    BUS_DMASYNC_PREWRITE);
    790   1.1    bouyer 
    791   1.1    bouyer #ifdef DIAGNOSTIC
    792   1.1    bouyer 	if (dma_maps->dmamap_table->dm_segs[0].ds_addr & ~IDEDMA_TBL_MASK) {
    793  1.22    bouyer 		printf("pciide_dma_dmamap_setup: addr 0x%lx "
    794  1.22    bouyer 		    "not properly aligned\n",
    795   1.1    bouyer 		    (u_long)dma_maps->dmamap_table->dm_segs[0].ds_addr);
    796   1.1    bouyer 		panic("pciide_dma_init: table align");
    797   1.1    bouyer 	}
    798   1.1    bouyer #endif
    799  1.22    bouyer 	/* remember flags */
    800  1.22    bouyer 	dma_maps->dma_flags = flags;
    801  1.22    bouyer 
    802  1.22    bouyer 	return 0;
    803  1.22    bouyer }
    804  1.22    bouyer 
    805  1.22    bouyer int
    806  1.61   msaitoh pciide_dma_init(void *v, int channel, int drive, void *databuf, size_t datalen,
    807  1.61   msaitoh     int flags)
    808  1.22    bouyer {
    809  1.22    bouyer 	struct pciide_softc *sc = v;
    810  1.22    bouyer 	int error;
    811  1.22    bouyer 	struct pciide_channel *cp = &sc->pciide_channels[channel];
    812  1.22    bouyer 	struct pciide_dma_maps *dma_maps = &cp->dma_maps[drive];
    813   1.1    bouyer 
    814  1.22    bouyer 	if ((error = pciide_dma_dmamap_setup(sc, channel, drive,
    815  1.22    bouyer 	    databuf, datalen, flags)) != 0)
    816  1.22    bouyer 		return error;
    817  1.22    bouyer 	/* Maps are ready. Start DMA function */
    818   1.1    bouyer 	/* Clear status bits */
    819   1.3      fvdl 	bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
    820   1.3      fvdl 	    bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0));
    821   1.1    bouyer 	/* Write table addr */
    822   1.3      fvdl 	bus_space_write_4(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_TBL], 0,
    823   1.1    bouyer 	    dma_maps->dmamap_table->dm_segs[0].ds_addr);
    824   1.1    bouyer 	/* set read/write */
    825   1.3      fvdl 	bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0,
    826   1.5   thorpej 	    ((flags & WDC_DMA_READ) ? IDEDMA_CMD_WRITE : 0) | cp->idedma_cmd);
    827   1.1    bouyer 	return 0;
    828   1.1    bouyer }
    829   1.1    bouyer 
    830   1.1    bouyer void
    831  1.35  christos pciide_dma_start(void *v, int channel, int drive)
    832   1.1    bouyer {
    833   1.1    bouyer 	struct pciide_softc *sc = v;
    834   1.3      fvdl 	struct pciide_channel *cp = &sc->pciide_channels[channel];
    835   1.1    bouyer 
    836  1.16   thorpej 	ATADEBUG_PRINT(("pciide_dma_start\n"),DEBUG_XFERS);
    837   1.3      fvdl 	bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0,
    838   1.3      fvdl 	    bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0)
    839   1.3      fvdl 		| IDEDMA_CMD_START);
    840   1.1    bouyer }
    841   1.1    bouyer 
    842   1.1    bouyer int
    843  1.40       dsl pciide_dma_finish(void *v, int channel, int drive, int force)
    844   1.1    bouyer {
    845   1.1    bouyer 	struct pciide_softc *sc = v;
    846   1.1    bouyer 	u_int8_t status;
    847   1.1    bouyer 	int error = 0;
    848   1.3      fvdl 	struct pciide_channel *cp = &sc->pciide_channels[channel];
    849   1.3      fvdl 	struct pciide_dma_maps *dma_maps = &cp->dma_maps[drive];
    850   1.1    bouyer 
    851   1.3      fvdl 	status = bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0);
    852  1.16   thorpej 	ATADEBUG_PRINT(("pciide_dma_finish: status 0x%x\n", status),
    853   1.1    bouyer 	    DEBUG_XFERS);
    854   1.1    bouyer 
    855  1.14    bouyer 	if (force == WDC_DMAEND_END && (status & IDEDMA_CTL_INTR) == 0)
    856   1.1    bouyer 		return WDC_DMAST_NOIRQ;
    857   1.1    bouyer 
    858   1.1    bouyer 	/* stop DMA channel */
    859   1.3      fvdl 	bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0,
    860   1.3      fvdl 	    bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0)
    861   1.3      fvdl 		& ~IDEDMA_CMD_START);
    862   1.1    bouyer 
    863   1.1    bouyer 	/* Unload the map of the data buffer */
    864   1.1    bouyer 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
    865   1.1    bouyer 	    dma_maps->dmamap_xfer->dm_mapsize,
    866   1.1    bouyer 	    (dma_maps->dma_flags & WDC_DMA_READ) ?
    867   1.1    bouyer 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    868   1.1    bouyer 	bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_xfer);
    869   1.1    bouyer 
    870  1.14    bouyer 	if ((status & IDEDMA_CTL_ERR) != 0 && force != WDC_DMAEND_ABRT_QUIET) {
    871  1.38      cube 		aprint_error("%s:%d:%d: bus-master DMA error: status=0x%x\n",
    872  1.38      cube 		    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel,
    873  1.38      cube 		    drive, status);
    874   1.1    bouyer 		error |= WDC_DMAST_ERR;
    875   1.1    bouyer 	}
    876   1.1    bouyer 
    877  1.14    bouyer 	if ((status & IDEDMA_CTL_INTR) == 0 && force != WDC_DMAEND_ABRT_QUIET) {
    878  1.38      cube 		aprint_error("%s:%d:%d: bus-master DMA error: missing "
    879  1.38      cube 		    "interrupt, status=0x%x\n",
    880  1.38      cube 		    device_xname(sc->sc_wdcdev.sc_atac.atac_dev),
    881  1.20   thorpej 		    channel, drive, status);
    882   1.1    bouyer 		error |= WDC_DMAST_NOIRQ;
    883   1.1    bouyer 	}
    884   1.1    bouyer 
    885  1.14    bouyer 	if ((status & IDEDMA_CTL_ACT) != 0 && force != WDC_DMAEND_ABRT_QUIET) {
    886   1.1    bouyer 		/* data underrun, may be a valid condition for ATAPI */
    887   1.1    bouyer 		error |= WDC_DMAST_UNDER;
    888   1.1    bouyer 	}
    889   1.1    bouyer 	return error;
    890   1.1    bouyer }
    891   1.1    bouyer 
    892   1.1    bouyer void
    893  1.39       dsl pciide_irqack(struct ata_channel *chp)
    894   1.1    bouyer {
    895  1.19   thorpej 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
    896  1.19   thorpej 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    897   1.1    bouyer 
    898   1.1    bouyer 	/* clear status bits in IDE DMA registers */
    899   1.3      fvdl 	bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
    900   1.3      fvdl 	    bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0));
    901   1.1    bouyer }
    902  1.33     itohy #endif	/* NATA_DMA */
    903   1.1    bouyer 
    904   1.1    bouyer /* some common code used by several chip_map */
    905   1.1    bouyer int
    906  1.39       dsl pciide_chansetup(struct pciide_softc *sc, int channel, pcireg_t interface)
    907   1.1    bouyer {
    908   1.1    bouyer 	struct pciide_channel *cp = &sc->pciide_channels[channel];
    909  1.17   thorpej 	sc->wdc_chanarray[channel] = &cp->ata_channel;
    910   1.1    bouyer 	cp->name = PCIIDE_CHANNEL_NAME(channel);
    911  1.17   thorpej 	cp->ata_channel.ch_channel = channel;
    912  1.20   thorpej 	cp->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
    913  1.65  jdolecek 
    914  1.38      cube 	aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    915  1.38      cube 	    "%s channel %s to %s mode\n", cp->name,
    916   1.1    bouyer 	    (interface & PCIIDE_INTERFACE_SETTABLE(channel)) ?
    917   1.1    bouyer 	    "configured" : "wired",
    918   1.1    bouyer 	    (interface & PCIIDE_INTERFACE_PCI(channel)) ?
    919   1.1    bouyer 	    "native-PCI" : "compatibility");
    920   1.1    bouyer 	return 1;
    921   1.1    bouyer }
    922   1.1    bouyer 
    923   1.1    bouyer /* some common code used by several chip channel_map */
    924   1.1    bouyer void
    925  1.61   msaitoh pciide_mapchan(const struct pci_attach_args *pa, struct pciide_channel *cp,
    926  1.44  jakllsch 	pcireg_t interface, int (*pci_intr)(void *))
    927   1.1    bouyer {
    928  1.17   thorpej 	struct ata_channel *wdc_cp = &cp->ata_channel;
    929   1.1    bouyer 
    930   1.8   thorpej 	if (interface & PCIIDE_INTERFACE_PCI(wdc_cp->ch_channel))
    931  1.44  jakllsch 		pciide_mapregs_native(pa, cp, pci_intr);
    932  1.13    bouyer 	else {
    933  1.44  jakllsch 		pciide_mapregs_compat(pa, cp, wdc_cp->ch_channel);
    934  1.17   thorpej 		if ((cp->ata_channel.ch_flags & ATACH_DISABLED) == 0)
    935  1.13    bouyer 			pciide_map_compat_intr(pa, cp, wdc_cp->ch_channel);
    936  1.13    bouyer 	}
    937   1.1    bouyer 	wdcattach(wdc_cp);
    938   1.1    bouyer }
    939   1.1    bouyer 
    940   1.1    bouyer /*
    941   1.1    bouyer  * generic code to map the compat intr.
    942   1.1    bouyer  */
    943   1.1    bouyer void
    944  1.61   msaitoh pciide_map_compat_intr(const struct pci_attach_args *pa,
    945  1.61   msaitoh     struct pciide_channel *cp, int compatchan)
    946   1.1    bouyer {
    947  1.19   thorpej 	struct pciide_softc *sc = CHAN_TO_PCIIDE(&cp->ata_channel);
    948   1.1    bouyer 
    949   1.1    bouyer #ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH
    950  1.20   thorpej 	cp->ih =
    951  1.38      cube 	   pciide_machdep_compat_intr_establish(sc->sc_wdcdev.sc_atac.atac_dev,
    952  1.20   thorpej 	   pa, compatchan, pciide_compat_intr, cp);
    953   1.1    bouyer 	if (cp->ih == NULL) {
    954   1.1    bouyer #endif
    955  1.38      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    956  1.38      cube 		    "no compatibility interrupt for use by %s "
    957  1.38      cube 		    "channel\n", cp->name);
    958  1.17   thorpej 		cp->ata_channel.ch_flags |= ATACH_DISABLED;
    959   1.1    bouyer #ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH
    960   1.1    bouyer 	}
    961   1.1    bouyer #endif
    962   1.1    bouyer }
    963   1.1    bouyer 
    964   1.1    bouyer void
    965  1.61   msaitoh pciide_unmap_compat_intr(pci_chipset_tag_t pc, struct pciide_channel *cp,
    966  1.61   msaitoh     int compatchan)
    967  1.46  jakllsch {
    968  1.46  jakllsch #ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_DISESTABLISH
    969  1.46  jakllsch 	struct pciide_softc *sc = CHAN_TO_PCIIDE(&cp->ata_channel);
    970  1.46  jakllsch 
    971  1.46  jakllsch 	pciide_machdep_compat_intr_disestablish(sc->sc_wdcdev.sc_atac.atac_dev,
    972  1.46  jakllsch 	    sc->sc_pc, compatchan, cp->ih);
    973  1.46  jakllsch #endif
    974  1.46  jakllsch }
    975  1.46  jakllsch 
    976  1.46  jakllsch void
    977  1.49    dyoung default_chip_map(struct pciide_softc *sc, const struct pci_attach_args *pa)
    978   1.1    bouyer {
    979   1.1    bouyer 	struct pciide_channel *cp;
    980   1.1    bouyer 	pcireg_t interface = PCI_INTERFACE(pa->pa_class);
    981   1.1    bouyer 	pcireg_t csr;
    982  1.33     itohy 	int channel;
    983  1.33     itohy #if NATA_DMA
    984  1.33     itohy 	int drive;
    985   1.1    bouyer 	u_int8_t idedma_ctl;
    986  1.33     itohy #endif
    987  1.27  christos 	const char *failreason;
    988  1.17   thorpej 	struct wdc_regs *wdr;
    989   1.1    bouyer 
    990   1.1    bouyer 	if (pciide_chipen(sc, pa) == 0)
    991   1.1    bouyer 		return;
    992   1.1    bouyer 
    993   1.1    bouyer 	if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) {
    994  1.33     itohy #if NATA_DMA
    995  1.38      cube 		aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    996  1.38      cube 		    "bus-master DMA support present");
    997   1.1    bouyer 		if (sc->sc_pp == &default_product_desc &&
    998  1.38      cube 		    (device_cfdata(sc->sc_wdcdev.sc_atac.atac_dev)->cf_flags &
    999   1.1    bouyer 		    PCIIDE_OPTIONS_DMA) == 0) {
   1000  1.36        ad 			aprint_verbose(", but unused (no driver support)");
   1001   1.1    bouyer 			sc->sc_dma_ok = 0;
   1002   1.1    bouyer 		} else {
   1003   1.1    bouyer 			pciide_mapreg_dma(sc, pa);
   1004   1.1    bouyer 			if (sc->sc_dma_ok != 0)
   1005  1.36        ad 				aprint_verbose(", used without full driver "
   1006   1.1    bouyer 				    "support");
   1007   1.1    bouyer 		}
   1008  1.33     itohy #else
   1009  1.38      cube 		aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
   1010  1.38      cube 		    "bus-master DMA support present, but unused (no driver "
   1011  1.38      cube 		    "support)");
   1012  1.33     itohy #endif	/* NATA_DMA */
   1013   1.1    bouyer 	} else {
   1014  1.38      cube 		aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
   1015  1.38      cube 		    "hardware does not support DMA");
   1016  1.33     itohy #if NATA_DMA
   1017   1.1    bouyer 		sc->sc_dma_ok = 0;
   1018  1.33     itohy #endif
   1019   1.1    bouyer 	}
   1020  1.36        ad 	aprint_verbose("\n");
   1021  1.33     itohy #if NATA_DMA
   1022   1.1    bouyer 	if (sc->sc_dma_ok) {
   1023  1.20   thorpej 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA;
   1024   1.1    bouyer 		sc->sc_wdcdev.irqack = pciide_irqack;
   1025   1.1    bouyer 	}
   1026  1.33     itohy #endif
   1027  1.20   thorpej 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
   1028  1.33     itohy #if NATA_DMA
   1029  1.20   thorpej 	sc->sc_wdcdev.sc_atac.atac_dma_cap = 0;
   1030  1.33     itohy #endif
   1031   1.1    bouyer 
   1032  1.20   thorpej 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
   1033  1.20   thorpej 	sc->sc_wdcdev.sc_atac.atac_nchannels = PCIIDE_NUM_CHANNELS;
   1034  1.20   thorpej 	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
   1035  1.57    bouyer 	sc->sc_wdcdev.wdc_maxdrives = 2;
   1036   1.1    bouyer 
   1037  1.17   thorpej 	wdc_allocate_regs(&sc->sc_wdcdev);
   1038  1.17   thorpej 
   1039  1.20   thorpej 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
   1040  1.20   thorpej 	     channel++) {
   1041   1.1    bouyer 		cp = &sc->pciide_channels[channel];
   1042   1.1    bouyer 		if (pciide_chansetup(sc, channel, interface) == 0)
   1043   1.1    bouyer 			continue;
   1044  1.19   thorpej 		wdr = CHAN_TO_WDC_REGS(&cp->ata_channel);
   1045  1.10    bouyer 		if (interface & PCIIDE_INTERFACE_PCI(channel))
   1046  1.44  jakllsch 			pciide_mapregs_native(pa, cp, pciide_pci_intr);
   1047  1.10    bouyer 		else
   1048  1.10    bouyer 			pciide_mapregs_compat(pa, cp,
   1049  1.44  jakllsch 			    cp->ata_channel.ch_channel);
   1050  1.17   thorpej 		if (cp->ata_channel.ch_flags & ATACH_DISABLED)
   1051   1.1    bouyer 			continue;
   1052   1.1    bouyer 		/*
   1053   1.1    bouyer 		 * Check to see if something appears to be there.
   1054   1.1    bouyer 		 */
   1055   1.1    bouyer 		failreason = NULL;
   1056   1.1    bouyer 		/*
   1057   1.1    bouyer 		 * In native mode, always enable the controller. It's
   1058   1.1    bouyer 		 * not possible to have an ISA board using the same address
   1059   1.1    bouyer 		 * anyway.
   1060   1.1    bouyer 		 */
   1061  1.13    bouyer 		if (interface & PCIIDE_INTERFACE_PCI(channel)) {
   1062  1.17   thorpej 			wdcattach(&cp->ata_channel);
   1063  1.13    bouyer 			continue;
   1064  1.13    bouyer 		}
   1065  1.63  jdolecek 		if (!wdcprobe(CHAN_TO_WDC_REGS(&cp->ata_channel))) {
   1066   1.1    bouyer 			failreason = "not responding; disabled or no drives?";
   1067   1.1    bouyer 			goto next;
   1068   1.1    bouyer 		}
   1069   1.1    bouyer 		/*
   1070   1.1    bouyer 		 * Now, make sure it's actually attributable to this PCI IDE
   1071   1.1    bouyer 		 * channel by trying to access the channel again while the
   1072   1.1    bouyer 		 * PCI IDE controller's I/O space is disabled.  (If the
   1073   1.1    bouyer 		 * channel no longer appears to be there, it belongs to
   1074   1.1    bouyer 		 * this controller.)  YUCK!
   1075   1.1    bouyer 		 */
   1076   1.1    bouyer 		csr = pci_conf_read(sc->sc_pc, sc->sc_tag,
   1077   1.1    bouyer 		    PCI_COMMAND_STATUS_REG);
   1078   1.1    bouyer 		pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG,
   1079   1.1    bouyer 		    csr & ~PCI_COMMAND_IO_ENABLE);
   1080  1.63  jdolecek 		if (wdcprobe(CHAN_TO_WDC_REGS(&cp->ata_channel)))
   1081   1.1    bouyer 			failreason = "other hardware responding at addresses";
   1082   1.1    bouyer 		pci_conf_write(sc->sc_pc, sc->sc_tag,
   1083   1.1    bouyer 		    PCI_COMMAND_STATUS_REG, csr);
   1084   1.1    bouyer next:
   1085   1.1    bouyer 		if (failreason) {
   1086  1.38      cube 			aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
   1087  1.38      cube 			    "%s channel ignored (%s)\n", cp->name, failreason);
   1088  1.17   thorpej 			cp->ata_channel.ch_flags |= ATACH_DISABLED;
   1089  1.17   thorpej 			bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh,
   1090  1.44  jakllsch 			    wdr->cmd_ios);
   1091  1.44  jakllsch 			bus_space_unmap(wdr->ctl_iot, wdr->ctl_ioh,
   1092  1.44  jakllsch 			    wdr->ctl_ios);
   1093  1.10    bouyer 		} else {
   1094  1.13    bouyer 			pciide_map_compat_intr(pa, cp,
   1095  1.17   thorpej 			    cp->ata_channel.ch_channel);
   1096  1.17   thorpej 			wdcattach(&cp->ata_channel);
   1097   1.1    bouyer 		}
   1098   1.1    bouyer 	}
   1099   1.1    bouyer 
   1100  1.33     itohy #if NATA_DMA
   1101   1.1    bouyer 	if (sc->sc_dma_ok == 0)
   1102   1.1    bouyer 		return;
   1103   1.1    bouyer 
   1104   1.1    bouyer 	/* Allocate DMA maps */
   1105  1.20   thorpej 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
   1106  1.20   thorpej 	     channel++) {
   1107   1.1    bouyer 		idedma_ctl = 0;
   1108   1.1    bouyer 		cp = &sc->pciide_channels[channel];
   1109  1.57    bouyer 		for (drive = 0; drive < sc->sc_wdcdev.wdc_maxdrives; drive++) {
   1110  1.29    bouyer 			/*
   1111  1.29    bouyer 			 * we have not probed the drives yet, allocate
   1112  1.67   msaitoh 			 * resources for all of them.
   1113  1.29    bouyer 			 */
   1114   1.1    bouyer 			if (pciide_dma_table_setup(sc, channel, drive) != 0) {
   1115   1.1    bouyer 				/* Abort DMA setup */
   1116   1.1    bouyer 				aprint_error(
   1117   1.1    bouyer 				    "%s:%d:%d: can't allocate DMA maps, "
   1118   1.1    bouyer 				    "using PIO transfers\n",
   1119  1.38      cube 				    device_xname(
   1120  1.38      cube 				      sc->sc_wdcdev.sc_atac.atac_dev),
   1121   1.1    bouyer 				    channel, drive);
   1122  1.29    bouyer 				sc->sc_dma_ok = 0;
   1123  1.29    bouyer 				sc->sc_wdcdev.sc_atac.atac_cap &= ~ATAC_CAP_DMA;
   1124  1.29    bouyer 				sc->sc_wdcdev.irqack = NULL;
   1125  1.29    bouyer 				break;
   1126   1.1    bouyer 			}
   1127   1.1    bouyer 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1128   1.1    bouyer 		}
   1129   1.1    bouyer 		if (idedma_ctl != 0) {
   1130   1.1    bouyer 			/* Add software bits in status register */
   1131   1.3      fvdl 			bus_space_write_1(sc->sc_dma_iot,
   1132   1.3      fvdl 			    cp->dma_iohs[IDEDMA_CTL], 0, idedma_ctl);
   1133   1.1    bouyer 		}
   1134   1.1    bouyer 	}
   1135  1.33     itohy #endif	/* NATA_DMA */
   1136   1.1    bouyer }
   1137   1.1    bouyer 
   1138   1.1    bouyer void
   1139  1.39       dsl sata_setup_channel(struct ata_channel *chp)
   1140   1.1    bouyer {
   1141  1.33     itohy #if NATA_DMA
   1142   1.1    bouyer 	struct ata_drive_datas *drvp;
   1143  1.34     itohy 	int drive;
   1144  1.34     itohy #if NATA_UDMA
   1145  1.34     itohy 	int s;
   1146  1.34     itohy #endif
   1147   1.1    bouyer 	u_int32_t idedma_ctl;
   1148  1.19   thorpej 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
   1149  1.19   thorpej 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
   1150   1.1    bouyer 
   1151   1.1    bouyer 	/* setup DMA if needed */
   1152   1.1    bouyer 	pciide_channel_dma_setup(cp);
   1153   1.1    bouyer 
   1154   1.1    bouyer 	idedma_ctl = 0;
   1155   1.1    bouyer 
   1156  1.57    bouyer 	KASSERT(cp->ata_channel.ch_ndrives != 0);
   1157  1.57    bouyer 	for (drive = 0; drive < cp->ata_channel.ch_ndrives; drive++) {
   1158   1.1    bouyer 		drvp = &chp->ch_drive[drive];
   1159   1.1    bouyer 		/* If no drive, skip */
   1160  1.57    bouyer 		if (drvp->drive_type == ATA_DRIVET_NONE)
   1161   1.1    bouyer 			continue;
   1162  1.33     itohy #if NATA_UDMA
   1163  1.57    bouyer 		if (drvp->drive_flags & ATA_DRIVE_UDMA) {
   1164   1.1    bouyer 			/* use Ultra/DMA */
   1165  1.21   thorpej 			s = splbio();
   1166  1.57    bouyer 			drvp->drive_flags &= ~ATA_DRIVE_DMA;
   1167  1.21   thorpej 			splx(s);
   1168   1.1    bouyer 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1169  1.33     itohy 		} else
   1170  1.33     itohy #endif	/* NATA_UDMA */
   1171  1.57    bouyer 		if (drvp->drive_flags & ATA_DRIVE_DMA) {
   1172   1.1    bouyer 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1173   1.1    bouyer 		}
   1174   1.1    bouyer 	}
   1175   1.1    bouyer 
   1176   1.1    bouyer 	/*
   1177   1.1    bouyer 	 * Nothing to do to setup modes; it is meaningless in S-ATA
   1178   1.1    bouyer 	 * (but many S-ATA drives still want to get the SET_FEATURE
   1179   1.1    bouyer 	 * command).
   1180   1.1    bouyer 	 */
   1181   1.1    bouyer 	if (idedma_ctl != 0) {
   1182   1.1    bouyer 		/* Add software bits in status register */
   1183   1.3      fvdl 		bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
   1184   1.1    bouyer 		    idedma_ctl);
   1185   1.1    bouyer 	}
   1186  1.33     itohy #endif	/* NATA_DMA */
   1187   1.1    bouyer }
   1188