Home | History | Annotate | Line # | Download | only in pci
pciide_common.c revision 1.2
      1  1.2  bouyer /*	$NetBSD: pciide_common.c,v 1.2 2003/10/23 19:29:35 bouyer 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  * 3. All advertising materials mentioning features or use of this software
     16  1.1  bouyer  *    must display the following acknowledgement:
     17  1.1  bouyer  *	This product includes software developed by Manuel Bouyer.
     18  1.1  bouyer  * 4. Neither the name of the University nor the names of its contributors
     19  1.1  bouyer  *    may be used to endorse or promote products derived from this software
     20  1.1  bouyer  *    without specific prior written permission.
     21  1.1  bouyer  *
     22  1.1  bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  1.1  bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  1.1  bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  1.1  bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  1.1  bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  1.1  bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  1.1  bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  1.1  bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  1.1  bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  1.1  bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  1.1  bouyer  *
     33  1.1  bouyer  */
     34  1.1  bouyer 
     35  1.1  bouyer 
     36  1.1  bouyer /*
     37  1.1  bouyer  * Copyright (c) 1996, 1998 Christopher G. Demetriou.  All rights reserved.
     38  1.1  bouyer  *
     39  1.1  bouyer  * Redistribution and use in source and binary forms, with or without
     40  1.1  bouyer  * modification, are permitted provided that the following conditions
     41  1.1  bouyer  * are met:
     42  1.1  bouyer  * 1. Redistributions of source code must retain the above copyright
     43  1.1  bouyer  *    notice, this list of conditions and the following disclaimer.
     44  1.1  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     45  1.1  bouyer  *    notice, this list of conditions and the following disclaimer in the
     46  1.1  bouyer  *    documentation and/or other materials provided with the distribution.
     47  1.1  bouyer  * 3. All advertising materials mentioning features or use of this software
     48  1.1  bouyer  *    must display the following acknowledgement:
     49  1.1  bouyer  *      This product includes software developed by Christopher G. Demetriou
     50  1.1  bouyer  *	for the NetBSD Project.
     51  1.1  bouyer  * 4. The name of the author may not be used to endorse or promote products
     52  1.1  bouyer  *    derived from this software without specific prior written permission
     53  1.1  bouyer  *
     54  1.1  bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     55  1.1  bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     56  1.1  bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     57  1.1  bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     58  1.1  bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     59  1.1  bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     60  1.1  bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     61  1.1  bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     62  1.1  bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     63  1.1  bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     64  1.1  bouyer  */
     65  1.1  bouyer 
     66  1.1  bouyer /*
     67  1.1  bouyer  * PCI IDE controller driver.
     68  1.1  bouyer  *
     69  1.1  bouyer  * Author: Christopher G. Demetriou, March 2, 1998 (derived from NetBSD
     70  1.1  bouyer  * sys/dev/pci/ppb.c, revision 1.16).
     71  1.1  bouyer  *
     72  1.1  bouyer  * See "PCI IDE Controller Specification, Revision 1.0 3/4/94" and
     73  1.1  bouyer  * "Programming Interface for Bus Master IDE Controller, Revision 1.0
     74  1.1  bouyer  * 5/16/94" from the PCI SIG.
     75  1.1  bouyer  *
     76  1.1  bouyer  */
     77  1.1  bouyer 
     78  1.1  bouyer #include <sys/cdefs.h>
     79  1.2  bouyer __KERNEL_RCSID(0, "$NetBSD: pciide_common.c,v 1.2 2003/10/23 19:29:35 bouyer Exp $");
     80  1.1  bouyer 
     81  1.1  bouyer #include <sys/param.h>
     82  1.1  bouyer #include <sys/malloc.h>
     83  1.1  bouyer 
     84  1.1  bouyer #include <uvm/uvm_extern.h>
     85  1.1  bouyer 
     86  1.1  bouyer #include <dev/pci/pcireg.h>
     87  1.1  bouyer #include <dev/pci/pcivar.h>
     88  1.1  bouyer #include <dev/pci/pcidevs.h>
     89  1.1  bouyer #include <dev/pci/pciidereg.h>
     90  1.1  bouyer #include <dev/pci/pciidevar.h>
     91  1.1  bouyer 
     92  1.1  bouyer #ifdef WDCDEBUG
     93  1.1  bouyer int wdcdebug_pciide_mask = 0;
     94  1.1  bouyer #endif
     95  1.1  bouyer 
     96  1.1  bouyer static const char dmaerrfmt[] =
     97  1.1  bouyer     "%s:%d: unable to %s table DMA map for drive %d, error=%d\n";
     98  1.1  bouyer 
     99  1.1  bouyer 
    100  1.1  bouyer 
    101  1.1  bouyer /* options passed via the 'flags' config keyword */
    102  1.1  bouyer #define	PCIIDE_OPTIONS_DMA	0x01
    103  1.1  bouyer #define	PCIIDE_OPTIONS_NODMA	0x02
    104  1.1  bouyer 
    105  1.1  bouyer /* Default product description for devices not known from this controller */
    106  1.1  bouyer const struct pciide_product_desc default_product_desc = {
    107  1.1  bouyer 	0,
    108  1.1  bouyer 	0,
    109  1.1  bouyer 	"Generic PCI IDE controller",
    110  1.1  bouyer 	default_chip_map,
    111  1.1  bouyer };
    112  1.1  bouyer 
    113  1.1  bouyer const struct pciide_product_desc *
    114  1.1  bouyer pciide_lookup_product(id, pp)
    115  1.1  bouyer 	pcireg_t id;
    116  1.1  bouyer 	const struct pciide_product_desc *pp;
    117  1.1  bouyer {
    118  1.1  bouyer 	for (; pp->chip_map != NULL; pp++)
    119  1.1  bouyer 		if (PCI_PRODUCT(id) == pp->ide_product)
    120  1.1  bouyer 			break;
    121  1.1  bouyer 
    122  1.1  bouyer 	if (pp->chip_map == NULL)
    123  1.1  bouyer 		return NULL;
    124  1.1  bouyer 	return pp;
    125  1.1  bouyer }
    126  1.1  bouyer 
    127  1.1  bouyer void
    128  1.1  bouyer pciide_common_attach(sc, pa, pp)
    129  1.1  bouyer 	struct pciide_softc *sc;
    130  1.1  bouyer 	struct pci_attach_args *pa;
    131  1.1  bouyer 	const struct pciide_product_desc *pp;
    132  1.1  bouyer {
    133  1.1  bouyer 	pci_chipset_tag_t pc = pa->pa_pc;
    134  1.1  bouyer 	pcitag_t tag = pa->pa_tag;
    135  1.1  bouyer 	pcireg_t csr;
    136  1.1  bouyer 	char devinfo[256];
    137  1.1  bouyer 	const char *displaydev;
    138  1.1  bouyer 
    139  1.1  bouyer 	aprint_naive(": disk controller\n");
    140  1.1  bouyer 	aprint_normal("\n");
    141  1.1  bouyer 
    142  1.1  bouyer 	sc->sc_pci_id = pa->pa_id;
    143  1.1  bouyer 	if (pp == NULL) {
    144  1.1  bouyer 		/* should only happen for generic pciide devices */
    145  1.1  bouyer 		sc->sc_pp = &default_product_desc;
    146  1.1  bouyer 		pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    147  1.1  bouyer 		displaydev = devinfo;
    148  1.1  bouyer 	} else {
    149  1.1  bouyer 		sc->sc_pp = pp;
    150  1.1  bouyer 		displaydev = sc->sc_pp->ide_name;
    151  1.1  bouyer 	}
    152  1.1  bouyer 
    153  1.1  bouyer 	/* if displaydev == NULL, printf is done in chip-specific map */
    154  1.1  bouyer 	if (displaydev)
    155  1.1  bouyer 		aprint_normal("%s: %s (rev. 0x%02x)\n",
    156  1.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname, displaydev,
    157  1.1  bouyer 		    PCI_REVISION(pa->pa_class));
    158  1.1  bouyer 
    159  1.1  bouyer 	sc->sc_pc = pa->pa_pc;
    160  1.1  bouyer 	sc->sc_tag = pa->pa_tag;
    161  1.1  bouyer 
    162  1.1  bouyer 	/* Set up DMA defaults; these might be adjusted by chip_map. */
    163  1.1  bouyer 	sc->sc_dma_maxsegsz = IDEDMA_BYTE_COUNT_MAX;
    164  1.1  bouyer 	sc->sc_dma_boundary = IDEDMA_BYTE_COUNT_ALIGN;
    165  1.1  bouyer 
    166  1.1  bouyer #ifdef WDCDEBUG
    167  1.1  bouyer 	if (wdcdebug_pciide_mask & DEBUG_PROBE)
    168  1.1  bouyer 		pci_conf_print(sc->sc_pc, sc->sc_tag, NULL);
    169  1.1  bouyer #endif
    170  1.1  bouyer 	sc->sc_pp->chip_map(sc, pa);
    171  1.1  bouyer 
    172  1.1  bouyer 	if (sc->sc_dma_ok) {
    173  1.1  bouyer 		csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    174  1.1  bouyer 		csr |= PCI_COMMAND_MASTER_ENABLE;
    175  1.1  bouyer 		pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
    176  1.1  bouyer 	}
    177  1.1  bouyer 	WDCDEBUG_PRINT(("pciide: command/status register=%x\n",
    178  1.1  bouyer 	    pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG)), DEBUG_PROBE);
    179  1.1  bouyer }
    180  1.1  bouyer 
    181  1.1  bouyer /* tell whether the chip is enabled or not */
    182  1.1  bouyer int
    183  1.1  bouyer pciide_chipen(sc, pa)
    184  1.1  bouyer 	struct pciide_softc *sc;
    185  1.1  bouyer 	struct pci_attach_args *pa;
    186  1.1  bouyer {
    187  1.1  bouyer 	pcireg_t csr;
    188  1.1  bouyer 
    189  1.1  bouyer 	if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0) {
    190  1.1  bouyer 		csr = pci_conf_read(sc->sc_pc, sc->sc_tag,
    191  1.1  bouyer 		    PCI_COMMAND_STATUS_REG);
    192  1.1  bouyer 		aprint_normal("%s: device disabled (at %s)\n",
    193  1.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname,
    194  1.1  bouyer 		   (csr & PCI_COMMAND_IO_ENABLE) == 0 ?
    195  1.1  bouyer 		   "device" : "bridge");
    196  1.1  bouyer 		return 0;
    197  1.1  bouyer 	}
    198  1.1  bouyer 	return 1;
    199  1.1  bouyer }
    200  1.1  bouyer 
    201  1.1  bouyer void
    202  1.1  bouyer pciide_mapregs_compat(pa, cp, compatchan, cmdsizep, ctlsizep)
    203  1.1  bouyer 	struct pci_attach_args *pa;
    204  1.1  bouyer 	struct pciide_channel *cp;
    205  1.1  bouyer 	int compatchan;
    206  1.1  bouyer 	bus_size_t *cmdsizep, *ctlsizep;
    207  1.1  bouyer {
    208  1.1  bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
    209  1.1  bouyer 	struct channel_softc *wdc_cp = &cp->wdc_channel;
    210  1.1  bouyer 
    211  1.1  bouyer 	cp->compat = 1;
    212  1.1  bouyer 	*cmdsizep = PCIIDE_COMPAT_CMD_SIZE;
    213  1.1  bouyer 	*ctlsizep = PCIIDE_COMPAT_CTL_SIZE;
    214  1.1  bouyer 
    215  1.1  bouyer 	wdc_cp->cmd_iot = pa->pa_iot;
    216  1.1  bouyer 	if (bus_space_map(wdc_cp->cmd_iot, PCIIDE_COMPAT_CMD_BASE(compatchan),
    217  1.1  bouyer 	    PCIIDE_COMPAT_CMD_SIZE, 0, &wdc_cp->cmd_ioh) != 0) {
    218  1.1  bouyer 		aprint_error("%s: couldn't map %s channel cmd regs\n",
    219  1.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
    220  1.1  bouyer 		goto bad;
    221  1.1  bouyer 	}
    222  1.1  bouyer 
    223  1.1  bouyer 	wdc_cp->ctl_iot = pa->pa_iot;
    224  1.1  bouyer 	if (bus_space_map(wdc_cp->ctl_iot, PCIIDE_COMPAT_CTL_BASE(compatchan),
    225  1.1  bouyer 	    PCIIDE_COMPAT_CTL_SIZE, 0, &wdc_cp->ctl_ioh) != 0) {
    226  1.1  bouyer 		aprint_error("%s: couldn't map %s channel ctl regs\n",
    227  1.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
    228  1.1  bouyer 		bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh,
    229  1.1  bouyer 		    PCIIDE_COMPAT_CMD_SIZE);
    230  1.1  bouyer 		goto bad;
    231  1.1  bouyer 	}
    232  1.1  bouyer 
    233  1.1  bouyer 	wdc_cp->data32iot = wdc_cp->cmd_iot;
    234  1.1  bouyer 	wdc_cp->data32ioh = wdc_cp->cmd_ioh;
    235  1.1  bouyer 	pciide_map_compat_intr(pa, cp, compatchan);
    236  1.1  bouyer 	return;
    237  1.1  bouyer 
    238  1.1  bouyer bad:
    239  1.1  bouyer 	cp->wdc_channel.ch_flags |= WDCF_DISABLED;
    240  1.1  bouyer 	return;
    241  1.1  bouyer }
    242  1.1  bouyer 
    243  1.1  bouyer void
    244  1.1  bouyer pciide_mapregs_native(pa, cp, cmdsizep, ctlsizep, pci_intr)
    245  1.1  bouyer 	struct pci_attach_args * pa;
    246  1.1  bouyer 	struct pciide_channel *cp;
    247  1.1  bouyer 	bus_size_t *cmdsizep, *ctlsizep;
    248  1.1  bouyer 	int (*pci_intr) __P((void *));
    249  1.1  bouyer {
    250  1.1  bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
    251  1.1  bouyer 	struct channel_softc *wdc_cp = &cp->wdc_channel;
    252  1.1  bouyer 	const char *intrstr;
    253  1.1  bouyer 	pci_intr_handle_t intrhandle;
    254  1.1  bouyer 
    255  1.1  bouyer 	cp->compat = 0;
    256  1.1  bouyer 
    257  1.1  bouyer 	if (sc->sc_pci_ih == NULL) {
    258  1.1  bouyer 		if (pci_intr_map(pa, &intrhandle) != 0) {
    259  1.1  bouyer 			aprint_error("%s: couldn't map native-PCI interrupt\n",
    260  1.1  bouyer 			    sc->sc_wdcdev.sc_dev.dv_xname);
    261  1.1  bouyer 			goto bad;
    262  1.1  bouyer 		}
    263  1.1  bouyer 		intrstr = pci_intr_string(pa->pa_pc, intrhandle);
    264  1.1  bouyer 		sc->sc_pci_ih = pci_intr_establish(pa->pa_pc,
    265  1.1  bouyer 		    intrhandle, IPL_BIO, pci_intr, sc);
    266  1.1  bouyer 		if (sc->sc_pci_ih != NULL) {
    267  1.1  bouyer 			aprint_normal("%s: using %s for native-PCI interrupt\n",
    268  1.1  bouyer 			    sc->sc_wdcdev.sc_dev.dv_xname,
    269  1.1  bouyer 			    intrstr ? intrstr : "unknown interrupt");
    270  1.1  bouyer 		} else {
    271  1.1  bouyer 			aprint_error(
    272  1.1  bouyer 			    "%s: couldn't establish native-PCI interrupt",
    273  1.1  bouyer 			    sc->sc_wdcdev.sc_dev.dv_xname);
    274  1.1  bouyer 			if (intrstr != NULL)
    275  1.1  bouyer 				aprint_normal(" at %s", intrstr);
    276  1.1  bouyer 			aprint_normal("\n");
    277  1.1  bouyer 			goto bad;
    278  1.1  bouyer 		}
    279  1.1  bouyer 	}
    280  1.1  bouyer 	cp->ih = sc->sc_pci_ih;
    281  1.1  bouyer 	if (pci_mapreg_map(pa, PCIIDE_REG_CMD_BASE(wdc_cp->channel),
    282  1.1  bouyer 	    PCI_MAPREG_TYPE_IO, 0,
    283  1.1  bouyer 	    &wdc_cp->cmd_iot, &wdc_cp->cmd_ioh, NULL, cmdsizep) != 0) {
    284  1.1  bouyer 		aprint_error("%s: couldn't map %s channel cmd regs\n",
    285  1.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
    286  1.1  bouyer 		goto bad;
    287  1.1  bouyer 	}
    288  1.1  bouyer 
    289  1.1  bouyer 	if (pci_mapreg_map(pa, PCIIDE_REG_CTL_BASE(wdc_cp->channel),
    290  1.1  bouyer 	    PCI_MAPREG_TYPE_IO, 0,
    291  1.1  bouyer 	    &wdc_cp->ctl_iot, &cp->ctl_baseioh, NULL, ctlsizep) != 0) {
    292  1.1  bouyer 		aprint_error("%s: couldn't map %s channel ctl regs\n",
    293  1.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
    294  1.1  bouyer 		bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh, *cmdsizep);
    295  1.1  bouyer 		goto bad;
    296  1.1  bouyer 	}
    297  1.1  bouyer 	/*
    298  1.1  bouyer 	 * In native mode, 4 bytes of I/O space are mapped for the control
    299  1.1  bouyer 	 * register, the control register is at offset 2. Pass the generic
    300  1.1  bouyer 	 * code a handle for only one byte at the right offset.
    301  1.1  bouyer 	 */
    302  1.1  bouyer 	if (bus_space_subregion(wdc_cp->ctl_iot, cp->ctl_baseioh, 2, 1,
    303  1.1  bouyer 	    &wdc_cp->ctl_ioh) != 0) {
    304  1.1  bouyer 		aprint_error("%s: unable to subregion %s channel ctl regs\n",
    305  1.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
    306  1.1  bouyer 		bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh, *cmdsizep);
    307  1.1  bouyer 		bus_space_unmap(wdc_cp->cmd_iot, cp->ctl_baseioh, *ctlsizep);
    308  1.1  bouyer 		goto bad;
    309  1.1  bouyer 	}
    310  1.1  bouyer 
    311  1.1  bouyer 	wdc_cp->data32iot = wdc_cp->cmd_iot;
    312  1.1  bouyer 	wdc_cp->data32ioh = wdc_cp->cmd_ioh;
    313  1.1  bouyer 	return;
    314  1.1  bouyer 
    315  1.1  bouyer bad:
    316  1.1  bouyer 	cp->wdc_channel.ch_flags |= WDCF_DISABLED;
    317  1.1  bouyer 	return;
    318  1.1  bouyer }
    319  1.1  bouyer 
    320  1.1  bouyer void
    321  1.1  bouyer pciide_mapreg_dma(sc, pa)
    322  1.1  bouyer 	struct pciide_softc *sc;
    323  1.1  bouyer 	struct pci_attach_args *pa;
    324  1.1  bouyer {
    325  1.1  bouyer 	pcireg_t maptype;
    326  1.1  bouyer 	bus_addr_t addr;
    327  1.1  bouyer 
    328  1.1  bouyer 	/*
    329  1.1  bouyer 	 * Map DMA registers
    330  1.1  bouyer 	 *
    331  1.1  bouyer 	 * Note that sc_dma_ok is the right variable to test to see if
    332  1.1  bouyer 	 * DMA can be done.  If the interface doesn't support DMA,
    333  1.1  bouyer 	 * sc_dma_ok will never be non-zero.  If the DMA regs couldn't
    334  1.1  bouyer 	 * be mapped, it'll be zero.  I.e., sc_dma_ok will only be
    335  1.1  bouyer 	 * non-zero if the interface supports DMA and the registers
    336  1.1  bouyer 	 * could be mapped.
    337  1.1  bouyer 	 *
    338  1.1  bouyer 	 * XXX Note that despite the fact that the Bus Master IDE specs
    339  1.1  bouyer 	 * XXX say that "The bus master IDE function uses 16 bytes of IO
    340  1.1  bouyer 	 * XXX space," some controllers (at least the United
    341  1.1  bouyer 	 * XXX Microelectronics UM8886BF) place it in memory space.
    342  1.1  bouyer 	 */
    343  1.1  bouyer 	maptype = pci_mapreg_type(pa->pa_pc, pa->pa_tag,
    344  1.1  bouyer 	    PCIIDE_REG_BUS_MASTER_DMA);
    345  1.1  bouyer 
    346  1.1  bouyer 	switch (maptype) {
    347  1.1  bouyer 	case PCI_MAPREG_TYPE_IO:
    348  1.1  bouyer 		sc->sc_dma_ok = (pci_mapreg_info(pa->pa_pc, pa->pa_tag,
    349  1.1  bouyer 		    PCIIDE_REG_BUS_MASTER_DMA, PCI_MAPREG_TYPE_IO,
    350  1.1  bouyer 		    &addr, NULL, NULL) == 0);
    351  1.1  bouyer 		if (sc->sc_dma_ok == 0) {
    352  1.1  bouyer 			aprint_normal(
    353  1.1  bouyer 			    ", but unused (couldn't query registers)");
    354  1.1  bouyer 			break;
    355  1.1  bouyer 		}
    356  1.1  bouyer 		if ((sc->sc_pp->ide_flags & IDE_16BIT_IOSPACE)
    357  1.1  bouyer 		    && addr >= 0x10000) {
    358  1.1  bouyer 			sc->sc_dma_ok = 0;
    359  1.1  bouyer 			aprint_normal(
    360  1.1  bouyer 			    ", but unused (registers at unsafe address "
    361  1.1  bouyer 			    "%#lx)", (unsigned long)addr);
    362  1.1  bouyer 			break;
    363  1.1  bouyer 		}
    364  1.1  bouyer 		/* FALLTHROUGH */
    365  1.1  bouyer 
    366  1.1  bouyer 	case PCI_MAPREG_MEM_TYPE_32BIT:
    367  1.1  bouyer 		sc->sc_dma_ok = (pci_mapreg_map(pa,
    368  1.1  bouyer 		    PCIIDE_REG_BUS_MASTER_DMA, maptype, 0,
    369  1.1  bouyer 		    &sc->sc_dma_iot, &sc->sc_dma_ioh, NULL, NULL) == 0);
    370  1.1  bouyer 		sc->sc_dmat = pa->pa_dmat;
    371  1.1  bouyer 		if (sc->sc_dma_ok == 0) {
    372  1.1  bouyer 			aprint_normal(", but unused (couldn't map registers)");
    373  1.1  bouyer 		} else {
    374  1.1  bouyer 			sc->sc_wdcdev.dma_arg = sc;
    375  1.1  bouyer 			sc->sc_wdcdev.dma_init = pciide_dma_init;
    376  1.1  bouyer 			sc->sc_wdcdev.dma_start = pciide_dma_start;
    377  1.1  bouyer 			sc->sc_wdcdev.dma_finish = pciide_dma_finish;
    378  1.1  bouyer 		}
    379  1.1  bouyer 
    380  1.1  bouyer 		if (sc->sc_wdcdev.sc_dev.dv_cfdata->cf_flags &
    381  1.1  bouyer 		    PCIIDE_OPTIONS_NODMA) {
    382  1.1  bouyer 			aprint_normal(
    383  1.1  bouyer 			    ", but unused (forced off by config file)");
    384  1.1  bouyer 			sc->sc_dma_ok = 0;
    385  1.1  bouyer 		}
    386  1.1  bouyer 		break;
    387  1.1  bouyer 
    388  1.1  bouyer 	default:
    389  1.1  bouyer 		sc->sc_dma_ok = 0;
    390  1.1  bouyer 		aprint_normal(
    391  1.1  bouyer 		    ", but unsupported register maptype (0x%x)", maptype);
    392  1.1  bouyer 	}
    393  1.1  bouyer }
    394  1.1  bouyer 
    395  1.1  bouyer int
    396  1.1  bouyer pciide_compat_intr(arg)
    397  1.1  bouyer 	void *arg;
    398  1.1  bouyer {
    399  1.1  bouyer 	struct pciide_channel *cp = arg;
    400  1.1  bouyer 
    401  1.1  bouyer #ifdef DIAGNOSTIC
    402  1.1  bouyer 	/* should only be called for a compat channel */
    403  1.1  bouyer 	if (cp->compat == 0)
    404  1.1  bouyer 		panic("pciide compat intr called for non-compat chan %p", cp);
    405  1.1  bouyer #endif
    406  1.1  bouyer 	return (wdcintr(&cp->wdc_channel));
    407  1.1  bouyer }
    408  1.1  bouyer 
    409  1.1  bouyer int
    410  1.1  bouyer pciide_pci_intr(arg)
    411  1.1  bouyer 	void *arg;
    412  1.1  bouyer {
    413  1.1  bouyer 	struct pciide_softc *sc = arg;
    414  1.1  bouyer 	struct pciide_channel *cp;
    415  1.1  bouyer 	struct channel_softc *wdc_cp;
    416  1.1  bouyer 	int i, rv, crv;
    417  1.1  bouyer 
    418  1.1  bouyer 	rv = 0;
    419  1.1  bouyer 	for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
    420  1.1  bouyer 		cp = &sc->pciide_channels[i];
    421  1.1  bouyer 		wdc_cp = &cp->wdc_channel;
    422  1.1  bouyer 
    423  1.1  bouyer 		/* If a compat channel skip. */
    424  1.1  bouyer 		if (cp->compat)
    425  1.1  bouyer 			continue;
    426  1.1  bouyer 		/* if this channel not waiting for intr, skip */
    427  1.1  bouyer 		if ((wdc_cp->ch_flags & WDCF_IRQ_WAIT) == 0)
    428  1.1  bouyer 			continue;
    429  1.1  bouyer 
    430  1.1  bouyer 		crv = wdcintr(wdc_cp);
    431  1.1  bouyer 		if (crv == 0)
    432  1.1  bouyer 			;		/* leave rv alone */
    433  1.1  bouyer 		else if (crv == 1)
    434  1.1  bouyer 			rv = 1;		/* claim the intr */
    435  1.1  bouyer 		else if (rv == 0)	/* crv should be -1 in this case */
    436  1.1  bouyer 			rv = crv;	/* if we've done no better, take it */
    437  1.1  bouyer 	}
    438  1.1  bouyer 	return (rv);
    439  1.1  bouyer }
    440  1.1  bouyer 
    441  1.1  bouyer void
    442  1.1  bouyer pciide_channel_dma_setup(cp)
    443  1.1  bouyer 	struct pciide_channel *cp;
    444  1.1  bouyer {
    445  1.1  bouyer 	int drive;
    446  1.1  bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
    447  1.1  bouyer 	struct ata_drive_datas *drvp;
    448  1.1  bouyer 
    449  1.1  bouyer 	for (drive = 0; drive < 2; drive++) {
    450  1.1  bouyer 		drvp = &cp->wdc_channel.ch_drive[drive];
    451  1.1  bouyer 		/* If no drive, skip */
    452  1.1  bouyer 		if ((drvp->drive_flags & DRIVE) == 0)
    453  1.1  bouyer 			continue;
    454  1.1  bouyer 		/* setup DMA if needed */
    455  1.1  bouyer 		if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
    456  1.1  bouyer 		    (drvp->drive_flags & DRIVE_UDMA) == 0) ||
    457  1.1  bouyer 		    sc->sc_dma_ok == 0) {
    458  1.1  bouyer 			drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
    459  1.1  bouyer 			continue;
    460  1.1  bouyer 		}
    461  1.1  bouyer 		if (pciide_dma_table_setup(sc, cp->wdc_channel.channel, drive)
    462  1.1  bouyer 		    != 0) {
    463  1.1  bouyer 			/* Abort DMA setup */
    464  1.1  bouyer 			drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
    465  1.1  bouyer 			continue;
    466  1.1  bouyer 		}
    467  1.1  bouyer 	}
    468  1.1  bouyer }
    469  1.1  bouyer 
    470  1.1  bouyer int
    471  1.1  bouyer pciide_dma_table_setup(sc, channel, drive)
    472  1.1  bouyer 	struct pciide_softc *sc;
    473  1.1  bouyer 	int channel, drive;
    474  1.1  bouyer {
    475  1.1  bouyer 	bus_dma_segment_t seg;
    476  1.1  bouyer 	int error, rseg;
    477  1.1  bouyer 	const bus_size_t dma_table_size =
    478  1.1  bouyer 	    sizeof(struct idedma_table) * NIDEDMA_TABLES;
    479  1.1  bouyer 	struct pciide_dma_maps *dma_maps =
    480  1.1  bouyer 	    &sc->pciide_channels[channel].dma_maps[drive];
    481  1.1  bouyer 
    482  1.1  bouyer 	/* If table was already allocated, just return */
    483  1.1  bouyer 	if (dma_maps->dma_table)
    484  1.1  bouyer 		return 0;
    485  1.1  bouyer 
    486  1.1  bouyer 	/* Allocate memory for the DMA tables and map it */
    487  1.1  bouyer 	if ((error = bus_dmamem_alloc(sc->sc_dmat, dma_table_size,
    488  1.1  bouyer 	    IDEDMA_TBL_ALIGN, IDEDMA_TBL_ALIGN, &seg, 1, &rseg,
    489  1.1  bouyer 	    BUS_DMA_NOWAIT)) != 0) {
    490  1.1  bouyer 		aprint_error(dmaerrfmt, sc->sc_wdcdev.sc_dev.dv_xname, channel,
    491  1.1  bouyer 		    "allocate", drive, error);
    492  1.1  bouyer 		return error;
    493  1.1  bouyer 	}
    494  1.1  bouyer 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    495  1.1  bouyer 	    dma_table_size,
    496  1.1  bouyer 	    (caddr_t *)&dma_maps->dma_table,
    497  1.1  bouyer 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    498  1.1  bouyer 		aprint_error(dmaerrfmt, sc->sc_wdcdev.sc_dev.dv_xname, channel,
    499  1.1  bouyer 		    "map", drive, error);
    500  1.1  bouyer 		return error;
    501  1.1  bouyer 	}
    502  1.1  bouyer 	WDCDEBUG_PRINT(("pciide_dma_table_setup: table at %p len %lu, "
    503  1.1  bouyer 	    "phy 0x%lx\n", dma_maps->dma_table, (u_long)dma_table_size,
    504  1.1  bouyer 	    (unsigned long)seg.ds_addr), DEBUG_PROBE);
    505  1.1  bouyer 	/* Create and load table DMA map for this disk */
    506  1.1  bouyer 	if ((error = bus_dmamap_create(sc->sc_dmat, dma_table_size,
    507  1.1  bouyer 	    1, dma_table_size, IDEDMA_TBL_ALIGN, BUS_DMA_NOWAIT,
    508  1.1  bouyer 	    &dma_maps->dmamap_table)) != 0) {
    509  1.1  bouyer 		aprint_error(dmaerrfmt, sc->sc_wdcdev.sc_dev.dv_xname, channel,
    510  1.1  bouyer 		    "create", drive, error);
    511  1.1  bouyer 		return error;
    512  1.1  bouyer 	}
    513  1.1  bouyer 	if ((error = bus_dmamap_load(sc->sc_dmat,
    514  1.1  bouyer 	    dma_maps->dmamap_table,
    515  1.1  bouyer 	    dma_maps->dma_table,
    516  1.1  bouyer 	    dma_table_size, NULL, BUS_DMA_NOWAIT)) != 0) {
    517  1.1  bouyer 		aprint_error(dmaerrfmt, sc->sc_wdcdev.sc_dev.dv_xname, channel,
    518  1.1  bouyer 		    "load", drive, error);
    519  1.1  bouyer 		return error;
    520  1.1  bouyer 	}
    521  1.1  bouyer 	WDCDEBUG_PRINT(("pciide_dma_table_setup: phy addr of table 0x%lx\n",
    522  1.1  bouyer 	    (unsigned long)dma_maps->dmamap_table->dm_segs[0].ds_addr),
    523  1.1  bouyer 	    DEBUG_PROBE);
    524  1.1  bouyer 	/* Create a xfer DMA map for this drive */
    525  1.1  bouyer 	if ((error = bus_dmamap_create(sc->sc_dmat, IDEDMA_BYTE_COUNT_MAX,
    526  1.1  bouyer 	    NIDEDMA_TABLES, sc->sc_dma_maxsegsz, sc->sc_dma_boundary,
    527  1.1  bouyer 	    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    528  1.1  bouyer 	    &dma_maps->dmamap_xfer)) != 0) {
    529  1.1  bouyer 		aprint_error(dmaerrfmt, sc->sc_wdcdev.sc_dev.dv_xname, channel,
    530  1.1  bouyer 		    "create xfer", drive, error);
    531  1.1  bouyer 		return error;
    532  1.1  bouyer 	}
    533  1.1  bouyer 	return 0;
    534  1.1  bouyer }
    535  1.1  bouyer 
    536  1.1  bouyer int
    537  1.1  bouyer pciide_dma_init(v, channel, drive, databuf, datalen, flags)
    538  1.1  bouyer 	void *v;
    539  1.1  bouyer 	int channel, drive;
    540  1.1  bouyer 	void *databuf;
    541  1.1  bouyer 	size_t datalen;
    542  1.1  bouyer 	int flags;
    543  1.1  bouyer {
    544  1.1  bouyer 	struct pciide_softc *sc = v;
    545  1.1  bouyer 	int error, seg;
    546  1.1  bouyer 	struct pciide_dma_maps *dma_maps =
    547  1.1  bouyer 	    &sc->pciide_channels[channel].dma_maps[drive];
    548  1.1  bouyer 
    549  1.1  bouyer 	error = bus_dmamap_load(sc->sc_dmat,
    550  1.1  bouyer 	    dma_maps->dmamap_xfer,
    551  1.1  bouyer 	    databuf, datalen, NULL, BUS_DMA_NOWAIT | BUS_DMA_STREAMING |
    552  1.1  bouyer 	    ((flags & WDC_DMA_READ) ? BUS_DMA_READ : BUS_DMA_WRITE));
    553  1.1  bouyer 	if (error) {
    554  1.1  bouyer 		printf(dmaerrfmt, sc->sc_wdcdev.sc_dev.dv_xname, channel,
    555  1.1  bouyer 		    "load xfer", drive, error);
    556  1.1  bouyer 		return error;
    557  1.1  bouyer 	}
    558  1.1  bouyer 
    559  1.1  bouyer 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
    560  1.1  bouyer 	    dma_maps->dmamap_xfer->dm_mapsize,
    561  1.1  bouyer 	    (flags & WDC_DMA_READ) ?
    562  1.1  bouyer 	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    563  1.1  bouyer 
    564  1.1  bouyer 	for (seg = 0; seg < dma_maps->dmamap_xfer->dm_nsegs; seg++) {
    565  1.1  bouyer #ifdef DIAGNOSTIC
    566  1.1  bouyer 		/* A segment must not cross a 64k boundary */
    567  1.1  bouyer 		{
    568  1.1  bouyer 		u_long phys = dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
    569  1.1  bouyer 		u_long len = dma_maps->dmamap_xfer->dm_segs[seg].ds_len;
    570  1.1  bouyer 		if ((phys & ~IDEDMA_BYTE_COUNT_MASK) !=
    571  1.1  bouyer 		    ((phys + len - 1) & ~IDEDMA_BYTE_COUNT_MASK)) {
    572  1.1  bouyer 			printf("pciide_dma: segment %d physical addr 0x%lx"
    573  1.1  bouyer 			    " len 0x%lx not properly aligned\n",
    574  1.1  bouyer 			    seg, phys, len);
    575  1.1  bouyer 			panic("pciide_dma: buf align");
    576  1.1  bouyer 		}
    577  1.1  bouyer 		}
    578  1.1  bouyer #endif
    579  1.1  bouyer 		dma_maps->dma_table[seg].base_addr =
    580  1.1  bouyer 		    htole32(dma_maps->dmamap_xfer->dm_segs[seg].ds_addr);
    581  1.1  bouyer 		dma_maps->dma_table[seg].byte_count =
    582  1.1  bouyer 		    htole32(dma_maps->dmamap_xfer->dm_segs[seg].ds_len &
    583  1.1  bouyer 		    IDEDMA_BYTE_COUNT_MASK);
    584  1.1  bouyer 		WDCDEBUG_PRINT(("\t seg %d len %d addr 0x%x\n",
    585  1.1  bouyer 		   seg, le32toh(dma_maps->dma_table[seg].byte_count),
    586  1.1  bouyer 		   le32toh(dma_maps->dma_table[seg].base_addr)), DEBUG_DMA);
    587  1.1  bouyer 
    588  1.1  bouyer 	}
    589  1.1  bouyer 	dma_maps->dma_table[dma_maps->dmamap_xfer->dm_nsegs -1].byte_count |=
    590  1.1  bouyer 	    htole32(IDEDMA_BYTE_COUNT_EOT);
    591  1.1  bouyer 
    592  1.1  bouyer 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_table, 0,
    593  1.1  bouyer 	    dma_maps->dmamap_table->dm_mapsize,
    594  1.1  bouyer 	    BUS_DMASYNC_PREWRITE);
    595  1.1  bouyer 
    596  1.1  bouyer 	/* Maps are ready. Start DMA function */
    597  1.1  bouyer #ifdef DIAGNOSTIC
    598  1.1  bouyer 	if (dma_maps->dmamap_table->dm_segs[0].ds_addr & ~IDEDMA_TBL_MASK) {
    599  1.1  bouyer 		printf("pciide_dma_init: addr 0x%lx not properly aligned\n",
    600  1.1  bouyer 		    (u_long)dma_maps->dmamap_table->dm_segs[0].ds_addr);
    601  1.1  bouyer 		panic("pciide_dma_init: table align");
    602  1.1  bouyer 	}
    603  1.1  bouyer #endif
    604  1.1  bouyer 
    605  1.1  bouyer 	/* Clear status bits */
    606  1.1  bouyer 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    607  1.1  bouyer 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel,
    608  1.1  bouyer 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    609  1.1  bouyer 		IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel));
    610  1.1  bouyer 	/* Write table addr */
    611  1.1  bouyer 	bus_space_write_4(sc->sc_dma_iot, sc->sc_dma_ioh,
    612  1.1  bouyer 	    IDEDMA_TBL + IDEDMA_SCH_OFFSET * channel,
    613  1.1  bouyer 	    dma_maps->dmamap_table->dm_segs[0].ds_addr);
    614  1.1  bouyer 	/* set read/write */
    615  1.1  bouyer 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    616  1.1  bouyer 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
    617  1.1  bouyer 	    (flags & WDC_DMA_READ) ? IDEDMA_CMD_WRITE: 0);
    618  1.1  bouyer 	/* remember flags */
    619  1.1  bouyer 	dma_maps->dma_flags = flags;
    620  1.1  bouyer 	return 0;
    621  1.1  bouyer }
    622  1.1  bouyer 
    623  1.1  bouyer void
    624  1.1  bouyer pciide_dma_start(v, channel, drive)
    625  1.1  bouyer 	void *v;
    626  1.1  bouyer 	int channel, drive;
    627  1.1  bouyer {
    628  1.1  bouyer 	struct pciide_softc *sc = v;
    629  1.1  bouyer 
    630  1.1  bouyer 	WDCDEBUG_PRINT(("pciide_dma_start\n"),DEBUG_XFERS);
    631  1.1  bouyer 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    632  1.1  bouyer 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
    633  1.1  bouyer 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    634  1.1  bouyer 		IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) | IDEDMA_CMD_START);
    635  1.1  bouyer }
    636  1.1  bouyer 
    637  1.1  bouyer int
    638  1.1  bouyer pciide_dma_finish(v, channel, drive, force)
    639  1.1  bouyer 	void *v;
    640  1.1  bouyer 	int channel, drive;
    641  1.1  bouyer 	int force;
    642  1.1  bouyer {
    643  1.1  bouyer 	struct pciide_softc *sc = v;
    644  1.1  bouyer 	u_int8_t status;
    645  1.1  bouyer 	int error = 0;
    646  1.1  bouyer 	struct pciide_dma_maps *dma_maps =
    647  1.1  bouyer 	    &sc->pciide_channels[channel].dma_maps[drive];
    648  1.1  bouyer 
    649  1.1  bouyer 	status = bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    650  1.1  bouyer 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel);
    651  1.1  bouyer 	WDCDEBUG_PRINT(("pciide_dma_finish: status 0x%x\n", status),
    652  1.1  bouyer 	    DEBUG_XFERS);
    653  1.1  bouyer 
    654  1.1  bouyer 	if (force == 0 && (status & IDEDMA_CTL_INTR) == 0)
    655  1.1  bouyer 		return WDC_DMAST_NOIRQ;
    656  1.1  bouyer 
    657  1.1  bouyer 	/* stop DMA channel */
    658  1.1  bouyer 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    659  1.1  bouyer 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
    660  1.1  bouyer 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    661  1.1  bouyer 		IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) & ~IDEDMA_CMD_START);
    662  1.1  bouyer 
    663  1.1  bouyer 	/* Unload the map of the data buffer */
    664  1.1  bouyer 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
    665  1.1  bouyer 	    dma_maps->dmamap_xfer->dm_mapsize,
    666  1.1  bouyer 	    (dma_maps->dma_flags & WDC_DMA_READ) ?
    667  1.1  bouyer 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    668  1.1  bouyer 	bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_xfer);
    669  1.1  bouyer 
    670  1.1  bouyer 	if ((status & IDEDMA_CTL_ERR) != 0) {
    671  1.1  bouyer 		printf("%s:%d:%d: bus-master DMA error: status=0x%x\n",
    672  1.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname, channel, drive, status);
    673  1.1  bouyer 		error |= WDC_DMAST_ERR;
    674  1.1  bouyer 	}
    675  1.1  bouyer 
    676  1.1  bouyer 	if ((status & IDEDMA_CTL_INTR) == 0) {
    677  1.1  bouyer 		printf("%s:%d:%d: bus-master DMA error: missing interrupt, "
    678  1.1  bouyer 		    "status=0x%x\n", sc->sc_wdcdev.sc_dev.dv_xname, channel,
    679  1.1  bouyer 		    drive, status);
    680  1.1  bouyer 		error |= WDC_DMAST_NOIRQ;
    681  1.1  bouyer 	}
    682  1.1  bouyer 
    683  1.1  bouyer 	if ((status & IDEDMA_CTL_ACT) != 0) {
    684  1.1  bouyer 		/* data underrun, may be a valid condition for ATAPI */
    685  1.1  bouyer 		error |= WDC_DMAST_UNDER;
    686  1.1  bouyer 	}
    687  1.1  bouyer 	return error;
    688  1.1  bouyer }
    689  1.1  bouyer 
    690  1.1  bouyer void
    691  1.1  bouyer pciide_irqack(chp)
    692  1.1  bouyer 	struct channel_softc *chp;
    693  1.1  bouyer {
    694  1.1  bouyer 	struct pciide_channel *cp = (struct pciide_channel*)chp;
    695  1.1  bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
    696  1.1  bouyer 
    697  1.1  bouyer 	/* clear status bits in IDE DMA registers */
    698  1.1  bouyer 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    699  1.1  bouyer 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * chp->channel,
    700  1.1  bouyer 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    701  1.1  bouyer 		IDEDMA_CTL + IDEDMA_SCH_OFFSET * chp->channel));
    702  1.1  bouyer }
    703  1.1  bouyer 
    704  1.1  bouyer /* some common code used by several chip_map */
    705  1.1  bouyer int
    706  1.1  bouyer pciide_chansetup(sc, channel, interface)
    707  1.1  bouyer 	struct pciide_softc *sc;
    708  1.1  bouyer 	int channel;
    709  1.1  bouyer 	pcireg_t interface;
    710  1.1  bouyer {
    711  1.1  bouyer 	struct pciide_channel *cp = &sc->pciide_channels[channel];
    712  1.1  bouyer 	sc->wdc_chanarray[channel] = &cp->wdc_channel;
    713  1.1  bouyer 	cp->name = PCIIDE_CHANNEL_NAME(channel);
    714  1.1  bouyer 	cp->wdc_channel.channel = channel;
    715  1.1  bouyer 	cp->wdc_channel.wdc = &sc->sc_wdcdev;
    716  1.1  bouyer 	cp->wdc_channel.ch_queue =
    717  1.1  bouyer 	    malloc(sizeof(struct channel_queue), M_DEVBUF, M_NOWAIT);
    718  1.1  bouyer 	if (cp->wdc_channel.ch_queue == NULL) {
    719  1.1  bouyer 		aprint_error("%s %s channel: "
    720  1.1  bouyer 		    "can't allocate memory for command queue",
    721  1.1  bouyer 		sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
    722  1.1  bouyer 		return 0;
    723  1.1  bouyer 	}
    724  1.1  bouyer 	aprint_normal("%s: %s channel %s to %s mode\n",
    725  1.1  bouyer 	    sc->sc_wdcdev.sc_dev.dv_xname, cp->name,
    726  1.1  bouyer 	    (interface & PCIIDE_INTERFACE_SETTABLE(channel)) ?
    727  1.1  bouyer 	    "configured" : "wired",
    728  1.1  bouyer 	    (interface & PCIIDE_INTERFACE_PCI(channel)) ?
    729  1.1  bouyer 	    "native-PCI" : "compatibility");
    730  1.1  bouyer 	return 1;
    731  1.1  bouyer }
    732  1.1  bouyer 
    733  1.1  bouyer /* some common code used by several chip channel_map */
    734  1.1  bouyer void
    735  1.1  bouyer pciide_mapchan(pa, cp, interface, cmdsizep, ctlsizep, pci_intr)
    736  1.1  bouyer 	struct pci_attach_args *pa;
    737  1.1  bouyer 	struct pciide_channel *cp;
    738  1.1  bouyer 	pcireg_t interface;
    739  1.1  bouyer 	bus_size_t *cmdsizep, *ctlsizep;
    740  1.1  bouyer 	int (*pci_intr) __P((void *));
    741  1.1  bouyer {
    742  1.1  bouyer 	struct channel_softc *wdc_cp = &cp->wdc_channel;
    743  1.1  bouyer 
    744  1.1  bouyer 	if (interface & PCIIDE_INTERFACE_PCI(wdc_cp->channel))
    745  1.1  bouyer 		pciide_mapregs_native(pa, cp, cmdsizep, ctlsizep, pci_intr);
    746  1.1  bouyer 	else
    747  1.1  bouyer 		pciide_mapregs_compat(pa, cp, wdc_cp->channel, cmdsizep,
    748  1.1  bouyer 		    ctlsizep);
    749  1.1  bouyer 	wdcattach(wdc_cp);
    750  1.1  bouyer }
    751  1.1  bouyer 
    752  1.1  bouyer /*
    753  1.1  bouyer  * generic code to map the compat intr.
    754  1.1  bouyer  */
    755  1.1  bouyer void
    756  1.1  bouyer pciide_map_compat_intr(pa, cp, compatchan)
    757  1.1  bouyer 	struct pci_attach_args *pa;
    758  1.1  bouyer 	struct pciide_channel *cp;
    759  1.1  bouyer 	int compatchan;
    760  1.1  bouyer {
    761  1.1  bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
    762  1.1  bouyer 
    763  1.1  bouyer #ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH
    764  1.1  bouyer 	cp->ih = pciide_machdep_compat_intr_establish(&sc->sc_wdcdev.sc_dev,
    765  1.1  bouyer 	    pa, compatchan, pciide_compat_intr, cp);
    766  1.1  bouyer 	if (cp->ih == NULL) {
    767  1.1  bouyer #endif
    768  1.1  bouyer 		aprint_error("%s: no compatibility interrupt for use by %s "
    769  1.1  bouyer 		    "channel\n", sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
    770  1.1  bouyer 		cp->wdc_channel.ch_flags |= WDCF_DISABLED;
    771  1.1  bouyer #ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH
    772  1.1  bouyer 	}
    773  1.1  bouyer #endif
    774  1.1  bouyer }
    775  1.1  bouyer 
    776  1.1  bouyer void
    777  1.1  bouyer default_chip_map(sc, pa)
    778  1.1  bouyer 	struct pciide_softc *sc;
    779  1.1  bouyer 	struct pci_attach_args *pa;
    780  1.1  bouyer {
    781  1.1  bouyer 	struct pciide_channel *cp;
    782  1.1  bouyer 	pcireg_t interface = PCI_INTERFACE(pa->pa_class);
    783  1.1  bouyer 	pcireg_t csr;
    784  1.1  bouyer 	int channel, drive;
    785  1.1  bouyer 	struct ata_drive_datas *drvp;
    786  1.1  bouyer 	u_int8_t idedma_ctl;
    787  1.1  bouyer 	bus_size_t cmdsize, ctlsize;
    788  1.1  bouyer 	char *failreason;
    789  1.1  bouyer 
    790  1.1  bouyer 	if (pciide_chipen(sc, pa) == 0)
    791  1.1  bouyer 		return;
    792  1.1  bouyer 
    793  1.1  bouyer 	if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) {
    794  1.1  bouyer 		aprint_normal("%s: bus-master DMA support present",
    795  1.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname);
    796  1.1  bouyer 		if (sc->sc_pp == &default_product_desc &&
    797  1.1  bouyer 		    (sc->sc_wdcdev.sc_dev.dv_cfdata->cf_flags &
    798  1.1  bouyer 		    PCIIDE_OPTIONS_DMA) == 0) {
    799  1.1  bouyer 			aprint_normal(", but unused (no driver support)");
    800  1.1  bouyer 			sc->sc_dma_ok = 0;
    801  1.1  bouyer 		} else {
    802  1.1  bouyer 			pciide_mapreg_dma(sc, pa);
    803  1.1  bouyer 			if (sc->sc_dma_ok != 0)
    804  1.1  bouyer 				aprint_normal(", used without full driver "
    805  1.1  bouyer 				    "support");
    806  1.1  bouyer 		}
    807  1.1  bouyer 	} else {
    808  1.1  bouyer 		aprint_normal("%s: hardware does not support DMA",
    809  1.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname);
    810  1.1  bouyer 		sc->sc_dma_ok = 0;
    811  1.1  bouyer 	}
    812  1.1  bouyer 	aprint_normal("\n");
    813  1.1  bouyer 	if (sc->sc_dma_ok) {
    814  1.1  bouyer 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_IRQACK;
    815  1.1  bouyer 		sc->sc_wdcdev.irqack = pciide_irqack;
    816  1.1  bouyer 	}
    817  1.1  bouyer 	sc->sc_wdcdev.PIO_cap = 0;
    818  1.1  bouyer 	sc->sc_wdcdev.DMA_cap = 0;
    819  1.1  bouyer 
    820  1.1  bouyer 	sc->sc_wdcdev.channels = sc->wdc_chanarray;
    821  1.1  bouyer 	sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
    822  1.1  bouyer 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
    823  1.1  bouyer 
    824  1.1  bouyer 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
    825  1.1  bouyer 		cp = &sc->pciide_channels[channel];
    826  1.1  bouyer 		if (pciide_chansetup(sc, channel, interface) == 0)
    827  1.1  bouyer 			continue;
    828  1.1  bouyer 		pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize,
    829  1.1  bouyer 		    pciide_pci_intr);
    830  1.1  bouyer 		if (cp->wdc_channel.ch_flags & WDCF_DISABLED)
    831  1.1  bouyer 			continue;
    832  1.1  bouyer 		/*
    833  1.1  bouyer 		 * Check to see if something appears to be there.
    834  1.1  bouyer 		 */
    835  1.1  bouyer 		failreason = NULL;
    836  1.1  bouyer 		/*
    837  1.1  bouyer 		 * In native mode, always enable the controller. It's
    838  1.1  bouyer 		 * not possible to have an ISA board using the same address
    839  1.1  bouyer 		 * anyway.
    840  1.1  bouyer 		 */
    841  1.1  bouyer 		if (interface & PCIIDE_INTERFACE_PCI(channel))
    842  1.1  bouyer 			goto next;
    843  1.1  bouyer 		if (!wdcprobe(&cp->wdc_channel)) {
    844  1.1  bouyer 			failreason = "not responding; disabled or no drives?";
    845  1.1  bouyer 			goto next;
    846  1.1  bouyer 		}
    847  1.1  bouyer 		/*
    848  1.1  bouyer 		 * Now, make sure it's actually attributable to this PCI IDE
    849  1.1  bouyer 		 * channel by trying to access the channel again while the
    850  1.1  bouyer 		 * PCI IDE controller's I/O space is disabled.  (If the
    851  1.1  bouyer 		 * channel no longer appears to be there, it belongs to
    852  1.1  bouyer 		 * this controller.)  YUCK!
    853  1.1  bouyer 		 */
    854  1.1  bouyer 		csr = pci_conf_read(sc->sc_pc, sc->sc_tag,
    855  1.1  bouyer 		    PCI_COMMAND_STATUS_REG);
    856  1.1  bouyer 		pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG,
    857  1.1  bouyer 		    csr & ~PCI_COMMAND_IO_ENABLE);
    858  1.1  bouyer 		if (wdcprobe(&cp->wdc_channel))
    859  1.1  bouyer 			failreason = "other hardware responding at addresses";
    860  1.1  bouyer 		pci_conf_write(sc->sc_pc, sc->sc_tag,
    861  1.1  bouyer 		    PCI_COMMAND_STATUS_REG, csr);
    862  1.1  bouyer next:
    863  1.1  bouyer 		if (failreason) {
    864  1.1  bouyer 			aprint_error("%s: %s channel ignored (%s)\n",
    865  1.1  bouyer 			    sc->sc_wdcdev.sc_dev.dv_xname, cp->name,
    866  1.1  bouyer 			    failreason);
    867  1.1  bouyer 			cp->wdc_channel.ch_flags |= WDCF_DISABLED;
    868  1.2  bouyer 			bus_space_unmap(cp->wdc_channel.cmd_iot,
    869  1.2  bouyer 			    cp->wdc_channel.cmd_ioh, cmdsize);
    870  1.2  bouyer 			bus_space_unmap(cp->wdc_channel.ctl_iot,
    871  1.2  bouyer 			    cp->wdc_channel.ctl_ioh, ctlsize);
    872  1.2  bouyer 
    873  1.1  bouyer 		}
    874  1.1  bouyer 	}
    875  1.1  bouyer 
    876  1.1  bouyer 	if (sc->sc_dma_ok == 0)
    877  1.1  bouyer 		return;
    878  1.1  bouyer 
    879  1.1  bouyer 	/* Allocate DMA maps */
    880  1.1  bouyer 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
    881  1.1  bouyer 		idedma_ctl = 0;
    882  1.1  bouyer 		cp = &sc->pciide_channels[channel];
    883  1.1  bouyer 		for (drive = 0; drive < 2; drive++) {
    884  1.1  bouyer 			drvp = &cp->wdc_channel.ch_drive[drive];
    885  1.1  bouyer 			/* If no drive, skip */
    886  1.1  bouyer 			if ((drvp->drive_flags & DRIVE) == 0)
    887  1.1  bouyer 				continue;
    888  1.1  bouyer 			if ((drvp->drive_flags & DRIVE_DMA) == 0)
    889  1.1  bouyer 				continue;
    890  1.1  bouyer 			if (pciide_dma_table_setup(sc, channel, drive) != 0) {
    891  1.1  bouyer 				/* Abort DMA setup */
    892  1.1  bouyer 				aprint_error(
    893  1.1  bouyer 				    "%s:%d:%d: can't allocate DMA maps, "
    894  1.1  bouyer 				    "using PIO transfers\n",
    895  1.1  bouyer 				    sc->sc_wdcdev.sc_dev.dv_xname,
    896  1.1  bouyer 				    channel, drive);
    897  1.1  bouyer 				drvp->drive_flags &= ~DRIVE_DMA;
    898  1.1  bouyer 			}
    899  1.1  bouyer 			aprint_normal("%s:%d:%d: using DMA data transfers\n",
    900  1.1  bouyer 			    sc->sc_wdcdev.sc_dev.dv_xname,
    901  1.1  bouyer 			    channel, drive);
    902  1.1  bouyer 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    903  1.1  bouyer 		}
    904  1.1  bouyer 		if (idedma_ctl != 0) {
    905  1.1  bouyer 			/* Add software bits in status register */
    906  1.1  bouyer 			bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    907  1.1  bouyer 			    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
    908  1.1  bouyer 			    idedma_ctl);
    909  1.1  bouyer 		}
    910  1.1  bouyer 	}
    911  1.1  bouyer }
    912  1.1  bouyer 
    913  1.1  bouyer void
    914  1.1  bouyer sata_setup_channel(chp)
    915  1.1  bouyer 	struct channel_softc *chp;
    916  1.1  bouyer {
    917  1.1  bouyer 	struct ata_drive_datas *drvp;
    918  1.1  bouyer 	int drive;
    919  1.1  bouyer 	u_int32_t idedma_ctl;
    920  1.1  bouyer 	struct pciide_channel *cp = (struct pciide_channel*)chp;
    921  1.1  bouyer 	struct pciide_softc *sc = (struct pciide_softc*)cp->wdc_channel.wdc;
    922  1.1  bouyer 
    923  1.1  bouyer 	/* setup DMA if needed */
    924  1.1  bouyer 	pciide_channel_dma_setup(cp);
    925  1.1  bouyer 
    926  1.1  bouyer 	idedma_ctl = 0;
    927  1.1  bouyer 
    928  1.1  bouyer 	for (drive = 0; drive < 2; drive++) {
    929  1.1  bouyer 		drvp = &chp->ch_drive[drive];
    930  1.1  bouyer 		/* If no drive, skip */
    931  1.1  bouyer 		if ((drvp->drive_flags & DRIVE) == 0)
    932  1.1  bouyer 			continue;
    933  1.1  bouyer 		if (drvp->drive_flags & DRIVE_UDMA) {
    934  1.1  bouyer 			/* use Ultra/DMA */
    935  1.1  bouyer 			drvp->drive_flags &= ~DRIVE_DMA;
    936  1.1  bouyer 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    937  1.1  bouyer 		} else if (drvp->drive_flags & DRIVE_DMA) {
    938  1.1  bouyer 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    939  1.1  bouyer 		}
    940  1.1  bouyer 	}
    941  1.1  bouyer 
    942  1.1  bouyer 	/*
    943  1.1  bouyer 	 * Nothing to do to setup modes; it is meaningless in S-ATA
    944  1.1  bouyer 	 * (but many S-ATA drives still want to get the SET_FEATURE
    945  1.1  bouyer 	 * command).
    946  1.1  bouyer 	 */
    947  1.1  bouyer 	if (idedma_ctl != 0) {
    948  1.1  bouyer 		/* Add software bits in status register */
    949  1.1  bouyer 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    950  1.1  bouyer 		    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
    951  1.1  bouyer 		    idedma_ctl);
    952  1.1  bouyer 	}
    953  1.1  bouyer }
    954