Home | History | Annotate | Line # | Download | only in pci
pciide.c revision 1.6.2.15
      1  1.6.2.15  bouyer /*	$NetBSD: pciide.c,v 1.6.2.15 1998/10/02 19:37:21 bouyer Exp $	*/
      2       1.1     cgd 
      3       1.1     cgd /*
      4       1.1     cgd  * Copyright (c) 1996, 1998 Christopher G. Demetriou.  All rights reserved.
      5       1.1     cgd  *
      6       1.1     cgd  * Redistribution and use in source and binary forms, with or without
      7       1.1     cgd  * modification, are permitted provided that the following conditions
      8       1.1     cgd  * are met:
      9       1.1     cgd  * 1. Redistributions of source code must retain the above copyright
     10       1.1     cgd  *    notice, this list of conditions and the following disclaimer.
     11       1.1     cgd  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.1     cgd  *    notice, this list of conditions and the following disclaimer in the
     13       1.1     cgd  *    documentation and/or other materials provided with the distribution.
     14       1.1     cgd  * 3. All advertising materials mentioning features or use of this software
     15       1.1     cgd  *    must display the following acknowledgement:
     16       1.1     cgd  *      This product includes software developed by Christopher G. Demetriou
     17       1.1     cgd  *	for the NetBSD Project.
     18       1.1     cgd  * 4. The name of the author may not be used to endorse or promote products
     19       1.1     cgd  *    derived from this software without specific prior written permission
     20       1.1     cgd  *
     21       1.1     cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22       1.1     cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23       1.1     cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24       1.1     cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25       1.1     cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26       1.1     cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27       1.1     cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28       1.1     cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29       1.1     cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30       1.1     cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31       1.1     cgd  */
     32       1.1     cgd 
     33       1.1     cgd /*
     34       1.1     cgd  * PCI IDE controller driver.
     35       1.1     cgd  *
     36       1.1     cgd  * Author: Christopher G. Demetriou, March 2, 1998 (derived from NetBSD
     37       1.1     cgd  * sys/dev/pci/ppb.c, revision 1.16).
     38       1.1     cgd  *
     39       1.2     cgd  * See "PCI IDE Controller Specification, Revision 1.0 3/4/94" and
     40       1.2     cgd  * "Programming Interface for Bus Master IDE Controller, Revision 1.0
     41       1.2     cgd  * 5/16/94" from the PCI SIG.
     42       1.1     cgd  *
     43       1.1     cgd  */
     44       1.1     cgd 
     45   1.6.2.1  bouyer #define WDCDEBUG
     46   1.6.2.1  bouyer 
     47   1.6.2.1  bouyer #define DEBUG_DMA   0x01
     48   1.6.2.1  bouyer #define DEBUG_XFERS  0x02
     49   1.6.2.1  bouyer #define DEBUG_FUNCS  0x08
     50   1.6.2.1  bouyer #define DEBUG_PROBE  0x10
     51   1.6.2.1  bouyer #ifdef WDCDEBUG
     52   1.6.2.1  bouyer int wdcdebug_pciide_mask = DEBUG_PROBE;
     53   1.6.2.1  bouyer #define WDCDEBUG_PRINT(args, level) \
     54   1.6.2.1  bouyer 	if (wdcdebug_pciide_mask & (level)) printf args
     55   1.6.2.1  bouyer #else
     56   1.6.2.1  bouyer #define WDCDEBUG_PRINT(args, level)
     57   1.6.2.1  bouyer #endif
     58       1.1     cgd #include <sys/param.h>
     59       1.1     cgd #include <sys/systm.h>
     60       1.1     cgd #include <sys/device.h>
     61   1.6.2.1  bouyer #include <sys/malloc.h>
     62   1.6.2.1  bouyer 
     63   1.6.2.1  bouyer #include <vm/vm.h>
     64   1.6.2.1  bouyer #include <vm/vm_param.h>
     65   1.6.2.1  bouyer #include <vm/vm_kern.h>
     66       1.1     cgd 
     67       1.1     cgd #include <dev/pci/pcireg.h>
     68       1.1     cgd #include <dev/pci/pcivar.h>
     69   1.6.2.1  bouyer #include <dev/pci/pcidevs.h>
     70       1.1     cgd #include <dev/pci/pciidereg.h>
     71       1.1     cgd #include <dev/pci/pciidevar.h>
     72   1.6.2.4  bouyer #include <dev/pci/pciide_piix_reg.h>
     73  1.6.2.11  bouyer #include <dev/pci/pciide_apollo_reg.h>
     74  1.6.2.12  bouyer #include <dev/pci/pciide_cmd_reg.h>
     75   1.6.2.1  bouyer #include <dev/ata/atavar.h>
     76       1.6     cgd #include <dev/ic/wdcreg.h>
     77   1.6.2.1  bouyer #include <dev/ic/wdcvar.h>
     78       1.1     cgd 
     79       1.1     cgd struct pciide_softc {
     80   1.6.2.1  bouyer 	struct wdc_softc	sc_wdcdev;	/* common wdc definitions */
     81       1.1     cgd 
     82       1.1     cgd 	void			*sc_pci_ih;	/* PCI interrupt handle */
     83       1.5     cgd 	int			sc_dma_ok;	/* bus-master DMA info */
     84       1.2     cgd 	bus_space_tag_t		sc_dma_iot;
     85       1.2     cgd 	bus_space_handle_t	sc_dma_ioh;
     86   1.6.2.1  bouyer 	bus_dma_tag_t		sc_dmat;
     87   1.6.2.1  bouyer 	/* Chip description */
     88   1.6.2.1  bouyer 	const struct pciide_product_desc *sc_pp;
     89   1.6.2.1  bouyer 	/* common definitions */
     90   1.6.2.1  bouyer 	struct channel_softc wdc_channels[PCIIDE_NUM_CHANNELS];
     91   1.6.2.1  bouyer 	/* internal bookkeeping */
     92       1.1     cgd 	struct pciide_channel {			/* per-channel data */
     93       1.5     cgd 		int		hw_ok;		/* hardware mapped & OK? */
     94       1.1     cgd 		int		compat;		/* is it compat? */
     95       1.1     cgd 		void		*ih;		/* compat or pci handle */
     96   1.6.2.1  bouyer 		/* DMA tables and DMA map for xfer, for each drive */
     97   1.6.2.1  bouyer 		struct pciide_dma_maps {
     98   1.6.2.1  bouyer 			bus_dmamap_t    dmamap_table;
     99   1.6.2.1  bouyer 			struct idedma_table *dma_table;
    100   1.6.2.1  bouyer 			bus_dmamap_t    dmamap_xfer;
    101   1.6.2.1  bouyer 		} dma_maps[2];
    102   1.6.2.1  bouyer 	} pciide_channels[PCIIDE_NUM_CHANNELS];
    103   1.6.2.1  bouyer };
    104   1.6.2.1  bouyer 
    105   1.6.2.1  bouyer void default_setup_cap __P((struct pciide_softc*));
    106   1.6.2.1  bouyer void default_setup_chip __P((struct pciide_softc*,
    107   1.6.2.1  bouyer 				pci_chipset_tag_t, pcitag_t));
    108  1.6.2.12  bouyer const char *default_compat_channel_probe __P((struct pciide_softc *,
    109  1.6.2.12  bouyer 	    struct pci_attach_args *, int));
    110  1.6.2.14  bouyer 
    111   1.6.2.1  bouyer void piix_setup_cap __P((struct pciide_softc*));
    112   1.6.2.1  bouyer void piix_setup_chip __P((struct pciide_softc*,
    113   1.6.2.1  bouyer 				pci_chipset_tag_t, pcitag_t));
    114   1.6.2.1  bouyer void piix3_4_setup_chip __P((struct pciide_softc*,
    115   1.6.2.1  bouyer 				pci_chipset_tag_t, pcitag_t));
    116  1.6.2.14  bouyer const char *piix_compat_channel_probe __P((struct pciide_softc *,
    117  1.6.2.14  bouyer 	    struct pci_attach_args *, int));
    118   1.6.2.1  bouyer static u_int32_t piix_setup_idetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
    119   1.6.2.7  bouyer static u_int32_t piix_setup_idetim_drvs __P((struct ata_drive_datas*));
    120   1.6.2.1  bouyer static u_int32_t piix_setup_sidetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
    121   1.6.2.1  bouyer 
    122  1.6.2.11  bouyer void apollo_setup_cap __P((struct pciide_softc*));
    123  1.6.2.11  bouyer void apollo_setup_chip __P((struct pciide_softc*,
    124  1.6.2.11  bouyer 				pci_chipset_tag_t, pcitag_t));
    125  1.6.2.14  bouyer const char *apollo_compat_channel_probe __P((struct pciide_softc *,
    126  1.6.2.14  bouyer 	    struct pci_attach_args *, int));
    127  1.6.2.11  bouyer 
    128  1.6.2.12  bouyer const char *cmd_compat_channel_probe __P((struct pciide_softc *,
    129  1.6.2.12  bouyer             struct pci_attach_args *, int));
    130  1.6.2.12  bouyer 
    131   1.6.2.1  bouyer int  pciide_dma_table_setup __P((struct pciide_softc*, int, int));
    132   1.6.2.1  bouyer int  pciide_dma_init __P((void*, int, int, void *, size_t, int));
    133   1.6.2.1  bouyer void pciide_dma_start __P((void*, int, int, int));
    134   1.6.2.1  bouyer int  pciide_dma_finish __P((void*, int, int, int));
    135   1.6.2.1  bouyer 
    136   1.6.2.1  bouyer struct pciide_product_desc {
    137   1.6.2.1  bouyer     u_int32_t ide_product;
    138   1.6.2.1  bouyer     int ide_flags;
    139   1.6.2.1  bouyer     const char *ide_name;
    140   1.6.2.1  bouyer     /* init controller's capabilities for drives probe */
    141   1.6.2.1  bouyer     void (*setup_cap) __P((struct pciide_softc*));
    142   1.6.2.1  bouyer     /* init controller after drives probe */
    143   1.6.2.1  bouyer     void (*setup_chip) __P((struct pciide_softc*, pci_chipset_tag_t, pcitag_t));
    144  1.6.2.12  bouyer     /* Probe for compat channel enabled/disabled */
    145  1.6.2.12  bouyer     const char * (*compat_channel_probe) __P((struct pciide_softc *,
    146  1.6.2.12  bouyer 		struct pci_attach_args *, int));
    147   1.6.2.1  bouyer };
    148   1.6.2.1  bouyer 
    149   1.6.2.1  bouyer /* Flags for ide_flags */
    150  1.6.2.12  bouyer #define CMD_PCI064x_IOEN 0x01 /* CMD-style PCI_COMMAND_IO_ENABLE */
    151   1.6.2.1  bouyer #define ONE_QUEUE         0x02 /* device need serialised access */
    152   1.6.2.1  bouyer 
    153   1.6.2.1  bouyer /* Default product description for devices not known from this controller */
    154   1.6.2.1  bouyer const struct pciide_product_desc default_product_desc = {
    155   1.6.2.1  bouyer     0,
    156   1.6.2.1  bouyer     0,
    157   1.6.2.1  bouyer     "Generic PCI IDE controller",
    158   1.6.2.1  bouyer     default_setup_cap,
    159  1.6.2.12  bouyer     default_setup_chip,
    160  1.6.2.12  bouyer     default_compat_channel_probe
    161   1.6.2.1  bouyer };
    162       1.1     cgd 
    163   1.6.2.1  bouyer 
    164   1.6.2.1  bouyer const struct pciide_product_desc pciide_intel_products[] =  {
    165   1.6.2.1  bouyer     { PCI_PRODUCT_INTEL_82092AA,
    166   1.6.2.1  bouyer       0,
    167   1.6.2.1  bouyer       "Intel 82092AA IDE controller",
    168   1.6.2.1  bouyer       default_setup_cap,
    169  1.6.2.12  bouyer       default_setup_chip,
    170  1.6.2.12  bouyer       default_compat_channel_probe
    171   1.6.2.1  bouyer     },
    172   1.6.2.1  bouyer     { PCI_PRODUCT_INTEL_82371FB_IDE,
    173   1.6.2.1  bouyer       0,
    174   1.6.2.1  bouyer       "Intel 82371FB IDE controller (PIIX)",
    175   1.6.2.1  bouyer       piix_setup_cap,
    176  1.6.2.12  bouyer       piix_setup_chip,
    177  1.6.2.14  bouyer       piix_compat_channel_probe
    178   1.6.2.1  bouyer     },
    179   1.6.2.1  bouyer     { PCI_PRODUCT_INTEL_82371SB_IDE,
    180   1.6.2.1  bouyer       0,
    181   1.6.2.1  bouyer       "Intel 82371SB IDE Interface (PIIX3)",
    182   1.6.2.1  bouyer       piix_setup_cap,
    183  1.6.2.12  bouyer       piix3_4_setup_chip,
    184  1.6.2.14  bouyer       piix_compat_channel_probe
    185   1.6.2.1  bouyer     },
    186   1.6.2.1  bouyer     { PCI_PRODUCT_INTEL_82371AB_IDE,
    187   1.6.2.1  bouyer       0,
    188   1.6.2.1  bouyer       "Intel 82371AB IDE controller (PIIX4)",
    189   1.6.2.1  bouyer       piix_setup_cap,
    190  1.6.2.12  bouyer       piix3_4_setup_chip,
    191  1.6.2.14  bouyer       piix_compat_channel_probe
    192   1.6.2.1  bouyer     },
    193   1.6.2.1  bouyer     { 0,
    194   1.6.2.1  bouyer       0,
    195   1.6.2.1  bouyer       NULL,
    196   1.6.2.1  bouyer     }
    197   1.6.2.1  bouyer };
    198   1.6.2.1  bouyer const struct pciide_product_desc pciide_cmd_products[] =  {
    199   1.6.2.1  bouyer     { PCI_PRODUCT_CMDTECH_640,
    200  1.6.2.12  bouyer       ONE_QUEUE | CMD_PCI064x_IOEN,
    201   1.6.2.1  bouyer       "CMD Technology PCI0640",
    202   1.6.2.1  bouyer       default_setup_cap,
    203  1.6.2.12  bouyer       default_setup_chip,
    204  1.6.2.12  bouyer       cmd_compat_channel_probe
    205   1.6.2.1  bouyer     },
    206   1.6.2.1  bouyer     { 0,
    207   1.6.2.1  bouyer       0,
    208   1.6.2.1  bouyer       NULL,
    209   1.6.2.1  bouyer     }
    210   1.6.2.1  bouyer };
    211   1.6.2.1  bouyer 
    212  1.6.2.11  bouyer const struct pciide_product_desc pciide_via_products[] =  {
    213  1.6.2.11  bouyer     { PCI_PRODUCT_VIATECH_VT82C586_IDE,
    214  1.6.2.11  bouyer       0,
    215  1.6.2.11  bouyer       "VT82C586 (Apollo VP) IDE Controller",
    216  1.6.2.11  bouyer       apollo_setup_cap,
    217  1.6.2.11  bouyer       apollo_setup_chip,
    218  1.6.2.14  bouyer       apollo_compat_channel_probe
    219  1.6.2.11  bouyer      },
    220  1.6.2.11  bouyer      { 0,
    221  1.6.2.11  bouyer        0,
    222  1.6.2.11  bouyer        NULL,
    223  1.6.2.11  bouyer      }
    224  1.6.2.11  bouyer };
    225  1.6.2.11  bouyer 
    226   1.6.2.1  bouyer struct pciide_vendor_desc {
    227   1.6.2.1  bouyer     u_int32_t ide_vendor;
    228   1.6.2.1  bouyer     const struct pciide_product_desc *ide_products;
    229   1.6.2.1  bouyer };
    230   1.6.2.1  bouyer 
    231   1.6.2.1  bouyer const struct pciide_vendor_desc pciide_vendors[] = {
    232   1.6.2.1  bouyer     { PCI_VENDOR_INTEL, pciide_intel_products },
    233   1.6.2.1  bouyer     { PCI_VENDOR_CMDTECH, pciide_cmd_products },
    234  1.6.2.11  bouyer     { PCI_VENDOR_VIATECH, pciide_via_products },
    235   1.6.2.1  bouyer     { 0, NULL }
    236       1.1     cgd };
    237       1.1     cgd 
    238   1.6.2.1  bouyer 
    239       1.1     cgd #define	PCIIDE_CHANNEL_NAME(chan)	((chan) == 0 ? "primary" : "secondary")
    240       1.1     cgd 
    241       1.1     cgd int	pciide_match __P((struct device *, struct cfdata *, void *));
    242       1.1     cgd void	pciide_attach __P((struct device *, struct device *, void *));
    243       1.1     cgd 
    244       1.1     cgd struct cfattach pciide_ca = {
    245       1.1     cgd 	sizeof(struct pciide_softc), pciide_match, pciide_attach
    246       1.1     cgd };
    247       1.1     cgd 
    248       1.5     cgd int	pciide_map_channel_compat __P((struct pciide_softc *,
    249       1.5     cgd 	    struct pci_attach_args *, int));
    250       1.5     cgd int	pciide_map_channel_native __P((struct pciide_softc *,
    251       1.5     cgd 	    struct pci_attach_args *, int));
    252       1.5     cgd int	pciide_print __P((void *, const char *pnp));
    253       1.1     cgd int	pciide_compat_intr __P((void *));
    254       1.1     cgd int	pciide_pci_intr __P((void *));
    255   1.6.2.1  bouyer const struct pciide_product_desc* pciide_lookup_product __P((u_int32_t));
    256       1.1     cgd 
    257   1.6.2.1  bouyer const struct pciide_product_desc*
    258   1.6.2.1  bouyer pciide_lookup_product(id)
    259   1.6.2.1  bouyer     u_int32_t id;
    260   1.6.2.1  bouyer {
    261   1.6.2.1  bouyer     const struct pciide_product_desc *pp;
    262   1.6.2.1  bouyer     const struct pciide_vendor_desc *vp;
    263   1.6.2.1  bouyer 
    264   1.6.2.1  bouyer     for (vp = pciide_vendors; vp->ide_products != NULL; vp++)
    265   1.6.2.1  bouyer 	if (PCI_VENDOR(id) == vp->ide_vendor)
    266   1.6.2.1  bouyer 	    break;
    267   1.6.2.1  bouyer 
    268   1.6.2.1  bouyer     if ((pp = vp->ide_products) == NULL)
    269   1.6.2.1  bouyer 	return NULL;
    270   1.6.2.1  bouyer 
    271   1.6.2.1  bouyer     for (; pp->ide_name != NULL; pp++)
    272   1.6.2.1  bouyer 	if (PCI_PRODUCT(id) == pp->ide_product)
    273   1.6.2.1  bouyer 	    break;
    274   1.6.2.1  bouyer 
    275   1.6.2.1  bouyer     if (pp->ide_name == NULL)
    276   1.6.2.1  bouyer 	return NULL;
    277   1.6.2.1  bouyer     return pp;
    278   1.6.2.1  bouyer }
    279       1.6     cgd 
    280       1.1     cgd int
    281       1.1     cgd pciide_match(parent, match, aux)
    282       1.1     cgd 	struct device *parent;
    283       1.1     cgd 	struct cfdata *match;
    284       1.1     cgd 	void *aux;
    285       1.1     cgd {
    286       1.1     cgd 	struct pci_attach_args *pa = aux;
    287       1.1     cgd 
    288       1.1     cgd 	/*
    289       1.1     cgd 	 * Check the ID register to see that it's a PCI IDE controller.
    290       1.1     cgd 	 * If it is, we assume that we can deal with it; it _should_
    291       1.1     cgd 	 * work in a standardized way...
    292       1.1     cgd 	 */
    293       1.1     cgd 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
    294       1.1     cgd 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
    295       1.1     cgd 		return (1);
    296       1.1     cgd 	}
    297       1.1     cgd 
    298       1.1     cgd 	return (0);
    299       1.1     cgd }
    300       1.1     cgd 
    301       1.1     cgd void
    302       1.1     cgd pciide_attach(parent, self, aux)
    303       1.1     cgd 	struct device *parent, *self;
    304       1.1     cgd 	void *aux;
    305       1.1     cgd {
    306       1.1     cgd 	struct pci_attach_args *pa = aux;
    307       1.1     cgd 	pci_chipset_tag_t pc = pa->pa_pc;
    308   1.6.2.1  bouyer 	pcitag_t tag = pa->pa_tag;
    309       1.1     cgd 	struct pciide_softc *sc = (struct pciide_softc *)self;
    310       1.1     cgd 	struct pciide_channel *cp;
    311       1.1     cgd 	pcireg_t class, interface, csr;
    312       1.1     cgd 	pci_intr_handle_t intrhandle;
    313       1.1     cgd 	const char *intrstr;
    314       1.1     cgd 	char devinfo[256];
    315       1.1     cgd 	int i;
    316       1.1     cgd 
    317   1.6.2.1  bouyer         sc->sc_pp = pciide_lookup_product(pa->pa_id);
    318   1.6.2.1  bouyer 	if (sc->sc_pp == NULL) {
    319   1.6.2.1  bouyer 		sc->sc_pp = &default_product_desc;
    320   1.6.2.1  bouyer 		pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    321   1.6.2.1  bouyer 		printf(": %s (rev. 0x%02x)\n", devinfo,
    322   1.6.2.1  bouyer 		    PCI_REVISION(pa->pa_class));
    323   1.6.2.1  bouyer 	} else {
    324   1.6.2.1  bouyer 		printf(": %s\n", sc->sc_pp->ide_name);
    325   1.6.2.1  bouyer 	}
    326       1.1     cgd 
    327       1.1     cgd 	if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0) {
    328   1.6.2.1  bouyer 		csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    329  1.6.2.12  bouyer 		/*
    330  1.6.2.12  bouyer 		 * For a CMD PCI064x, the use of PCI_COMMAND_IO_ENABLE
    331  1.6.2.12  bouyer 		 * and base adresses registers can be disabled at
    332  1.6.2.12  bouyer 		 * hardware level. In this case, the device is wired
    333  1.6.2.12  bouyer 		 * in compat mode and its first channel is always enabled,
    334  1.6.2.12  bouyer 		 * but we can't rely on PCI_COMMAND_IO_ENABLE.
    335  1.6.2.12  bouyer 		 * In fact, it seems that the first channel of the CMD PCI0640
    336  1.6.2.12  bouyer 		 * can't be disabled.
    337  1.6.2.12  bouyer 		 */
    338  1.6.2.12  bouyer 		if ((sc->sc_pp->ide_flags & CMD_PCI064x_IOEN) == 0) {
    339  1.6.2.12  bouyer 			printf("%s: device disabled (at %s)\n",
    340  1.6.2.12  bouyer 		 	   sc->sc_wdcdev.sc_dev.dv_xname,
    341  1.6.2.12  bouyer 		  	  (csr & PCI_COMMAND_IO_ENABLE) == 0 ?
    342  1.6.2.12  bouyer 			  "device" : "bridge");
    343  1.6.2.12  bouyer 			return;
    344  1.6.2.12  bouyer 		}
    345       1.1     cgd 	}
    346       1.1     cgd 
    347   1.6.2.1  bouyer 	class = pci_conf_read(pc, tag, PCI_CLASS_REG);
    348       1.1     cgd 	interface = PCI_INTERFACE(class);
    349       1.1     cgd 
    350       1.1     cgd 	/*
    351  1.6.2.12  bouyer 	 * Set up PCI interrupt only if at last one channel is in native mode.
    352  1.6.2.12  bouyer 	 * At last one device (CMD PCI0640) has a default value of 14, which
    353  1.6.2.12  bouyer 	 * will be mapped even if both channels are in compat-only mode.
    354       1.1     cgd 	 */
    355  1.6.2.12  bouyer 	if (interface & (PCIIDE_INTERFACE_PCI(0) | PCIIDE_INTERFACE_PCI(1))) {
    356  1.6.2.12  bouyer 		if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
    357  1.6.2.12  bouyer 		    pa->pa_intrline, &intrhandle) != 0) {
    358  1.6.2.12  bouyer 			printf("%s: couldn't map native-PCI interrupt\n",
    359   1.6.2.1  bouyer 			    sc->sc_wdcdev.sc_dev.dv_xname);
    360  1.6.2.12  bouyer 		} else {
    361  1.6.2.12  bouyer 			intrstr = pci_intr_string(pa->pa_pc, intrhandle);
    362  1.6.2.12  bouyer 			sc->sc_pci_ih = pci_intr_establish(pa->pa_pc,
    363  1.6.2.12  bouyer 			    intrhandle, IPL_BIO, pciide_pci_intr, sc);
    364  1.6.2.12  bouyer 			if (sc->sc_pci_ih != NULL) {
    365  1.6.2.12  bouyer 				printf("%s: using %s for native-PCI "
    366  1.6.2.12  bouyer 				    "interrupt\n",
    367  1.6.2.12  bouyer 				    sc->sc_wdcdev.sc_dev.dv_xname,
    368  1.6.2.12  bouyer 				    intrstr ? intrstr : "unknown interrupt");
    369  1.6.2.12  bouyer 			} else {
    370  1.6.2.12  bouyer 				printf("%s: couldn't establish native-PCI "
    371  1.6.2.12  bouyer 				    "interrupt",
    372  1.6.2.12  bouyer 				    sc->sc_wdcdev.sc_dev.dv_xname);
    373  1.6.2.12  bouyer 				if (intrstr != NULL)
    374  1.6.2.12  bouyer 					printf(" at %s", intrstr);
    375  1.6.2.12  bouyer 				printf("\n");
    376  1.6.2.12  bouyer 			}
    377  1.6.2.12  bouyer 		}
    378       1.1     cgd 	}
    379       1.1     cgd 
    380       1.2     cgd 	/*
    381       1.2     cgd 	 * Map DMA registers, if DMA is supported.
    382       1.2     cgd 	 *
    383       1.5     cgd 	 * Note that sc_dma_ok is the right variable to test to see if
    384  1.6.2.12  bouyer 	 * DMA can be done.  If the interface doesn't support DMA,
    385  1.6.2.12  bouyer 	 * sc_dma_ok will never be non-zero.  If the DMA regs couldn't
    386       1.5     cgd 	 * be mapped, it'll be zero.  I.e., sc_dma_ok will only be
    387       1.5     cgd 	 * non-zero if the interface supports DMA and the registers
    388       1.5     cgd 	 * could be mapped.
    389       1.4     cgd 	 *
    390       1.4     cgd 	 * XXX Note that despite the fact that the Bus Master IDE specs
    391       1.4     cgd 	 * XXX say that "The bus master IDE functoin uses 16 bytes of IO
    392       1.4     cgd 	 * XXX space," some controllers (at least the United
    393       1.4     cgd 	 * XXX Microelectronics UM8886BF) place it in memory space.
    394       1.4     cgd 	 * XXX eventually, we should probably read the register and check
    395       1.4     cgd 	 * XXX which type it is.  Either that or 'quirk' certain devices.
    396       1.2     cgd 	 */
    397       1.2     cgd 	if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) {
    398       1.5     cgd 		sc->sc_dma_ok = (pci_mapreg_map(pa,
    399       1.2     cgd 		    PCIIDE_REG_BUS_MASTER_DMA, PCI_MAPREG_TYPE_IO, 0,
    400       1.2     cgd 		    &sc->sc_dma_iot, &sc->sc_dma_ioh, NULL, NULL) == 0);
    401   1.6.2.1  bouyer 		sc->sc_dmat = pa->pa_dmat;
    402   1.6.2.1  bouyer 		printf("%s: bus-master DMA support present",
    403   1.6.2.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname);
    404   1.6.2.1  bouyer 		if (sc->sc_dma_ok == 0) {
    405   1.6.2.1  bouyer 			printf(", but unused (couldn't map registers)");
    406   1.6.2.1  bouyer 		} else if (sc->sc_pp == 0) {
    407   1.6.2.1  bouyer 			printf(", but unused (no driver support)");
    408   1.6.2.1  bouyer 		} else {
    409   1.6.2.1  bouyer 			sc->sc_wdcdev.dma_arg = sc;
    410   1.6.2.1  bouyer 			sc->sc_wdcdev.dma_init = pciide_dma_init;
    411   1.6.2.1  bouyer 			sc->sc_wdcdev.dma_start = pciide_dma_start;
    412   1.6.2.1  bouyer 			sc->sc_wdcdev.dma_finish = pciide_dma_finish;
    413   1.6.2.1  bouyer 		}
    414   1.6.2.1  bouyer 		printf("\n");
    415       1.1     cgd 	}
    416   1.6.2.1  bouyer 	if (sc->sc_pp == NULL)
    417   1.6.2.1  bouyer 		default_setup_cap(sc);
    418   1.6.2.1  bouyer 	else
    419   1.6.2.1  bouyer 		sc->sc_pp->setup_cap(sc);
    420   1.6.2.1  bouyer 	sc->sc_wdcdev.channels = sc->wdc_channels;
    421   1.6.2.1  bouyer 	sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
    422  1.6.2.15  bouyer 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
    423       1.1     cgd 
    424       1.1     cgd 	for (i = 0; i < PCIIDE_NUM_CHANNELS; i++) {
    425   1.6.2.1  bouyer 		cp = &sc->pciide_channels[i];
    426       1.2     cgd 
    427   1.6.2.1  bouyer 		sc->wdc_channels[i].channel = i;
    428   1.6.2.1  bouyer 		sc->wdc_channels[i].wdc = &sc->sc_wdcdev;
    429   1.6.2.1  bouyer 		if (i > 0 && (sc->sc_pp->ide_flags & ONE_QUEUE)) {
    430   1.6.2.1  bouyer 		    sc->wdc_channels[i].ch_queue =
    431   1.6.2.1  bouyer 			sc->wdc_channels[0].ch_queue;
    432   1.6.2.1  bouyer 		} else {
    433   1.6.2.1  bouyer 		    sc->wdc_channels[i].ch_queue =
    434   1.6.2.1  bouyer 		        malloc(sizeof(struct channel_queue), M_DEVBUF,
    435   1.6.2.1  bouyer 			M_NOWAIT);
    436   1.6.2.1  bouyer 		}
    437   1.6.2.1  bouyer 		if (sc->wdc_channels[i].ch_queue == NULL) {
    438   1.6.2.1  bouyer 		    printf("%s %s channel: "
    439   1.6.2.1  bouyer 			"can't allocate memory for command queue",
    440   1.6.2.1  bouyer 			sc->sc_wdcdev.sc_dev.dv_xname,
    441   1.6.2.1  bouyer 			PCIIDE_CHANNEL_NAME(i));
    442   1.6.2.1  bouyer 			continue;
    443   1.6.2.1  bouyer 		}
    444       1.2     cgd 		printf("%s: %s channel %s to %s mode\n",
    445   1.6.2.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname,
    446   1.6.2.1  bouyer 		    PCIIDE_CHANNEL_NAME(i),
    447       1.2     cgd 		    (interface & PCIIDE_INTERFACE_SETTABLE(i)) ?
    448       1.2     cgd 		      "configured" : "wired",
    449       1.2     cgd 		    (interface & PCIIDE_INTERFACE_PCI(i)) ? "native-PCI" :
    450       1.2     cgd 		      "compatibility");
    451       1.1     cgd 
    452       1.5     cgd 		if (interface & PCIIDE_INTERFACE_PCI(i))
    453       1.5     cgd 			cp->hw_ok = pciide_map_channel_native(sc, pa, i);
    454       1.5     cgd 		else
    455       1.5     cgd 			cp->hw_ok = pciide_map_channel_compat(sc, pa, i);
    456       1.5     cgd 		if (!cp->hw_ok)
    457       1.5     cgd 			continue;
    458  1.6.2.15  bouyer 		sc->wdc_channels[i].data32iot = sc->wdc_channels[i].cmd_iot;
    459  1.6.2.15  bouyer 		sc->wdc_channels[i].data32ioh = sc->wdc_channels[i].cmd_ioh;
    460   1.6.2.1  bouyer 		/* Now call common attach routine */
    461   1.6.2.1  bouyer 		wdcattach(&sc->wdc_channels[i]);
    462       1.5     cgd 	}
    463   1.6.2.1  bouyer 	if (sc->sc_pp == NULL)
    464   1.6.2.1  bouyer 		default_setup_chip(sc, pc, tag);
    465   1.6.2.1  bouyer 	else
    466   1.6.2.1  bouyer 		sc->sc_pp->setup_chip(sc, pc, tag);
    467   1.6.2.1  bouyer 	WDCDEBUG_PRINT(("pciide: command/status register=%x\n",
    468   1.6.2.1  bouyer 	    pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG)), DEBUG_PROBE);
    469       1.5     cgd }
    470       1.5     cgd 
    471       1.5     cgd int
    472       1.5     cgd pciide_map_channel_compat(sc, pa, chan)
    473       1.5     cgd 	struct pciide_softc *sc;
    474       1.5     cgd 	struct pci_attach_args *pa;
    475       1.5     cgd 	int chan;
    476       1.5     cgd {
    477   1.6.2.1  bouyer 	struct pciide_channel *cp = &sc->pciide_channels[chan];
    478   1.6.2.1  bouyer 	struct channel_softc *wdc_cp = &sc->wdc_channels[chan];
    479       1.6     cgd 	const char *probe_fail_reason;
    480       1.5     cgd 	int rv = 1;
    481       1.5     cgd 
    482       1.5     cgd 	cp->compat = 1;
    483       1.5     cgd 
    484   1.6.2.1  bouyer 	wdc_cp->cmd_iot = pa->pa_iot;
    485   1.6.2.1  bouyer 	if (bus_space_map(wdc_cp->cmd_iot, PCIIDE_COMPAT_CMD_BASE(chan),
    486   1.6.2.1  bouyer 	    PCIIDE_COMPAT_CMD_SIZE, 0, &wdc_cp->cmd_ioh) != 0) {
    487       1.5     cgd 		printf("%s: couldn't map %s channel cmd regs\n",
    488   1.6.2.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname,
    489   1.6.2.1  bouyer 		    PCIIDE_CHANNEL_NAME(chan));
    490       1.5     cgd 		rv = 0;
    491       1.5     cgd 	}
    492       1.5     cgd 
    493   1.6.2.1  bouyer 	wdc_cp->ctl_iot = pa->pa_iot;
    494   1.6.2.1  bouyer 	if (bus_space_map(wdc_cp->ctl_iot, PCIIDE_COMPAT_CTL_BASE(chan),
    495   1.6.2.1  bouyer 	    PCIIDE_COMPAT_CTL_SIZE, 0, &wdc_cp->ctl_ioh) != 0) {
    496       1.5     cgd 		printf("%s: couldn't map %s channel ctl regs\n",
    497   1.6.2.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname,
    498   1.6.2.1  bouyer 		    PCIIDE_CHANNEL_NAME(chan));
    499       1.5     cgd 		rv = 0;
    500       1.5     cgd 	}
    501       1.5     cgd 
    502       1.5     cgd 	/*
    503       1.5     cgd 	 * If we weren't able to map the device successfully,
    504       1.5     cgd 	 * we just give up now.  Something else has already
    505       1.5     cgd 	 * occupied those ports, indicating that the device has
    506       1.5     cgd 	 * (probably) been completely disabled (by some nonstandard
    507       1.5     cgd 	 * mechanism).
    508       1.5     cgd 	 *
    509       1.5     cgd 	 * XXX If we successfully map some ports, but not others,
    510       1.5     cgd 	 * XXX it might make sense to unmap the ones that we mapped.
    511       1.5     cgd 	 */
    512       1.5     cgd 	if (rv == 0)
    513       1.5     cgd 		goto out;
    514       1.5     cgd 
    515       1.5     cgd 	/*
    516  1.6.2.14  bouyer 	 * If we were able to map the device successfully, check if
    517  1.6.2.14  bouyer 	 * the channel is enabled. For "known" device, a chip-specific
    518  1.6.2.14  bouyer 	 * routine will be used (which read the rigth PCI register).
    519  1.6.2.14  bouyer 	 * For unknow device, a generic routine using "standart" wdc probe
    520  1.6.2.14  bouyer 	 * will try to guess it.
    521       1.5     cgd 	 *
    522  1.6.2.14  bouyer 	 * If the channel has been disabled, other devices are free to use
    523       1.5     cgd 	 * its ports.
    524       1.5     cgd 	 */
    525  1.6.2.12  bouyer 	probe_fail_reason = sc->sc_pp->compat_channel_probe(sc, pa, chan);
    526       1.6     cgd 	if (probe_fail_reason != NULL) {
    527   1.6.2.1  bouyer 		printf("%s: %s channel ignored (%s)\n",
    528   1.6.2.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname,
    529       1.6     cgd 		    PCIIDE_CHANNEL_NAME(chan), probe_fail_reason);
    530       1.5     cgd 		rv = 0;
    531       1.5     cgd 
    532   1.6.2.1  bouyer 		bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh,
    533       1.5     cgd 		    PCIIDE_COMPAT_CMD_SIZE);
    534   1.6.2.1  bouyer 		bus_space_unmap(wdc_cp->ctl_iot, wdc_cp->ctl_ioh,
    535       1.5     cgd 		    PCIIDE_COMPAT_CTL_SIZE);
    536       1.5     cgd 
    537       1.5     cgd 		goto out;
    538       1.5     cgd 	}
    539       1.5     cgd 
    540       1.5     cgd 	/*
    541       1.5     cgd 	 * If we're here, we were able to map the device successfully
    542       1.5     cgd 	 * and it really looks like there's a controller there.
    543       1.5     cgd 	 *
    544       1.5     cgd 	 * Unless those conditions are true, we don't map the
    545       1.5     cgd 	 * compatibility interrupt.  The spec indicates that if a
    546       1.5     cgd 	 * channel is configured for compatibility mode and the PCI
    547       1.5     cgd 	 * device's I/O space is enabled, the channel will be enabled.
    548       1.5     cgd 	 * Hoewver, some devices seem to be able to disable invididual
    549       1.5     cgd 	 * compatibility channels (via non-standard mechanisms).  If
    550       1.5     cgd 	 * the channel is disabled, the interrupt line can (probably)
    551       1.5     cgd 	 * be used by other devices (and may be assigned to other
    552       1.5     cgd 	 * devices by the BIOS).  If we mapped the interrupt we might
    553       1.5     cgd 	 * conflict with another interrupt assignment.
    554       1.5     cgd 	 */
    555   1.6.2.1  bouyer 	cp->ih = pciide_machdep_compat_intr_establish(&sc->sc_wdcdev.sc_dev,
    556   1.6.2.1  bouyer 	    pa, chan, pciide_compat_intr, wdc_cp);
    557       1.5     cgd 	if (cp->ih == NULL) {
    558       1.5     cgd 		printf("%s: no compatibility interrupt for use by %s channel\n",
    559   1.6.2.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname,
    560   1.6.2.1  bouyer 		    PCIIDE_CHANNEL_NAME(chan));
    561       1.5     cgd 		rv = 0;
    562       1.5     cgd 	}
    563       1.5     cgd 
    564       1.5     cgd out:
    565       1.5     cgd 	return (rv);
    566       1.5     cgd }
    567       1.5     cgd 
    568       1.6     cgd int
    569       1.5     cgd pciide_map_channel_native(sc, pa, chan)
    570       1.5     cgd 	struct pciide_softc *sc;
    571       1.5     cgd 	struct pci_attach_args *pa;
    572       1.5     cgd 	int chan;
    573       1.5     cgd {
    574   1.6.2.1  bouyer 	struct pciide_channel *cp = &sc->pciide_channels[chan];
    575   1.6.2.1  bouyer 	struct channel_softc *wdc_cp = &sc->wdc_channels[chan];
    576       1.5     cgd 	int rv = 1;
    577       1.5     cgd 
    578       1.5     cgd 	cp->compat = 0;
    579       1.5     cgd 
    580       1.5     cgd 	if (pci_mapreg_map(pa, PCIIDE_REG_CMD_BASE(chan), PCI_MAPREG_TYPE_IO,
    581   1.6.2.1  bouyer 	    0, &wdc_cp->cmd_iot, &wdc_cp->cmd_ioh, NULL, NULL) != 0) {
    582       1.5     cgd 		printf("%s: couldn't map %s channel cmd regs\n",
    583   1.6.2.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname,
    584   1.6.2.1  bouyer 		    PCIIDE_CHANNEL_NAME(chan));
    585       1.5     cgd 		rv = 0;
    586       1.5     cgd 	}
    587       1.5     cgd 
    588       1.5     cgd 	if (pci_mapreg_map(pa, PCIIDE_REG_CTL_BASE(chan), PCI_MAPREG_TYPE_IO,
    589   1.6.2.1  bouyer 	    0, &wdc_cp->ctl_iot, &wdc_cp->ctl_ioh, NULL, NULL) != 0) {
    590       1.5     cgd 		printf("%s: couldn't map %s channel ctl regs\n",
    591   1.6.2.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname,
    592   1.6.2.1  bouyer 		    PCIIDE_CHANNEL_NAME(chan));
    593       1.5     cgd 		rv = 0;
    594       1.5     cgd 	}
    595       1.5     cgd 
    596       1.5     cgd 	if ((cp->ih = sc->sc_pci_ih) == NULL) {
    597       1.5     cgd 		printf("%s: no native-PCI interrupt for use by %s channel\n",
    598   1.6.2.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname,
    599   1.6.2.1  bouyer 		    PCIIDE_CHANNEL_NAME(chan));
    600       1.5     cgd 		rv = 0;
    601       1.1     cgd 	}
    602       1.5     cgd 
    603       1.5     cgd 	return (rv);
    604       1.1     cgd }
    605       1.1     cgd 
    606       1.1     cgd int
    607       1.1     cgd pciide_compat_intr(arg)
    608       1.1     cgd 	void *arg;
    609       1.1     cgd {
    610   1.6.2.1  bouyer 	struct channel_softc *wdc_cp = arg;
    611       1.1     cgd 
    612       1.1     cgd #ifdef DIAGNOSTIC
    613   1.6.2.1  bouyer 	struct pciide_softc *sc = (struct pciide_softc*)wdc_cp->wdc;
    614   1.6.2.1  bouyer 	struct pciide_channel *cp = &sc->pciide_channels[wdc_cp->channel];
    615       1.1     cgd 	/* should only be called for a compat channel */
    616       1.1     cgd 	if (cp->compat == 0)
    617       1.1     cgd 		panic("pciide compat intr called for non-compat chan %p\n", cp);
    618       1.1     cgd #endif
    619   1.6.2.1  bouyer 	return (wdcintr(wdc_cp));
    620       1.1     cgd }
    621       1.1     cgd 
    622       1.1     cgd int
    623       1.1     cgd pciide_pci_intr(arg)
    624       1.1     cgd 	void *arg;
    625       1.1     cgd {
    626       1.1     cgd 	struct pciide_softc *sc = arg;
    627       1.1     cgd 	struct pciide_channel *cp;
    628   1.6.2.1  bouyer 	struct channel_softc *wdc_cp;
    629       1.1     cgd 	int i, rv, crv;
    630       1.1     cgd 
    631       1.1     cgd 	rv = 0;
    632       1.1     cgd 	for (i = 0; i < PCIIDE_NUM_CHANNELS; i++) {
    633   1.6.2.1  bouyer 		cp = &sc->pciide_channels[i];
    634   1.6.2.1  bouyer 		wdc_cp = &sc->wdc_channels[i];
    635       1.1     cgd 
    636   1.6.2.1  bouyer 		/* If a compat channel skip. */
    637   1.6.2.1  bouyer 		if (cp->compat)
    638   1.6.2.1  bouyer 			continue;
    639   1.6.2.1  bouyer 		/* if this channel not waiting for intr, skip */
    640   1.6.2.1  bouyer 		if ((wdc_cp->ch_flags & WDCF_IRQ_WAIT) == 0)
    641       1.1     cgd 			continue;
    642       1.1     cgd 
    643   1.6.2.1  bouyer 		crv = wdcintr(wdc_cp);
    644       1.1     cgd 		if (crv == 0)
    645       1.1     cgd 			;		/* leave rv alone */
    646       1.1     cgd 		else if (crv == 1)
    647       1.1     cgd 			rv = 1;		/* claim the intr */
    648       1.1     cgd 		else if (rv == 0)	/* crv should be -1 in this case */
    649       1.1     cgd 			rv = crv;	/* if we've done no better, take it */
    650       1.1     cgd 	}
    651       1.1     cgd 	return (rv);
    652   1.6.2.1  bouyer }
    653   1.6.2.1  bouyer 
    654   1.6.2.1  bouyer void
    655   1.6.2.1  bouyer default_setup_cap(sc)
    656   1.6.2.1  bouyer 	struct pciide_softc *sc;
    657   1.6.2.1  bouyer {
    658   1.6.2.1  bouyer 	if (sc->sc_dma_ok)
    659   1.6.2.1  bouyer 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA;
    660   1.6.2.2  bouyer 	sc->sc_wdcdev.pio_mode = 0;
    661   1.6.2.1  bouyer 	sc->sc_wdcdev.dma_mode = 0;
    662   1.6.2.1  bouyer }
    663   1.6.2.1  bouyer 
    664   1.6.2.1  bouyer void
    665   1.6.2.1  bouyer default_setup_chip(sc, pc, tag)
    666   1.6.2.1  bouyer 	struct pciide_softc *sc;
    667   1.6.2.1  bouyer 	pci_chipset_tag_t pc;
    668   1.6.2.1  bouyer 	pcitag_t tag;
    669   1.6.2.1  bouyer {
    670   1.6.2.2  bouyer 	int channel, drive, idedma_ctl;
    671   1.6.2.2  bouyer 	struct channel_softc *chp;
    672   1.6.2.2  bouyer 	struct ata_drive_datas *drvp;
    673   1.6.2.2  bouyer 
    674   1.6.2.2  bouyer 	if (sc->sc_dma_ok == 0)
    675   1.6.2.2  bouyer 		return; /* nothing to do */
    676   1.6.2.2  bouyer 
    677   1.6.2.2  bouyer 	/* Allocate DMA maps */
    678   1.6.2.2  bouyer 	for (channel = 0; channel < PCIIDE_NUM_CHANNELS; channel++) {
    679   1.6.2.2  bouyer 		idedma_ctl = 0;
    680   1.6.2.2  bouyer 		chp = &sc->wdc_channels[channel];
    681   1.6.2.2  bouyer 		for (drive = 0; drive < 2; drive++) {
    682   1.6.2.2  bouyer 			drvp = &chp->ch_drive[drive];
    683   1.6.2.2  bouyer 			/* If no drive, skip */
    684   1.6.2.2  bouyer 			if ((drvp->drive_flags & DRIVE) == 0)
    685   1.6.2.2  bouyer 				continue;
    686   1.6.2.2  bouyer 			if (pciide_dma_table_setup(sc, channel, drive) != 0) {
    687   1.6.2.2  bouyer 				/* Abort DMA setup */
    688   1.6.2.2  bouyer 				printf("%s:%d:%d: can't allocate DMA maps, "
    689   1.6.2.2  bouyer 				    "using PIO transferts\n",
    690   1.6.2.2  bouyer 				    sc->sc_wdcdev.sc_dev.dv_xname,
    691   1.6.2.2  bouyer 				    channel, drive);
    692   1.6.2.2  bouyer 				drvp->drive_flags &= ~DRIVE_DMA;
    693   1.6.2.2  bouyer 			}
    694   1.6.2.2  bouyer 			printf("%s:%d:%d: using DMA mode %d\n",
    695   1.6.2.2  bouyer 			    sc->sc_wdcdev.sc_dev.dv_xname,
    696   1.6.2.2  bouyer 			    channel, drive,
    697   1.6.2.2  bouyer 			    drvp->DMA_mode);
    698   1.6.2.2  bouyer 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    699   1.6.2.2  bouyer 		}
    700   1.6.2.2  bouyer 		if (idedma_ctl != 0) {
    701   1.6.2.2  bouyer 			/* Add software bits in status register */
    702   1.6.2.2  bouyer 			bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    703   1.6.2.2  bouyer 			    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
    704   1.6.2.2  bouyer 			    idedma_ctl);
    705   1.6.2.2  bouyer 		}
    706   1.6.2.2  bouyer 	}
    707   1.6.2.2  bouyer 
    708   1.6.2.1  bouyer }
    709   1.6.2.1  bouyer 
    710  1.6.2.12  bouyer const char *
    711  1.6.2.12  bouyer default_compat_channel_probe(sc, pa, chan)
    712  1.6.2.12  bouyer 	struct pciide_softc *sc;
    713  1.6.2.12  bouyer 	struct pci_attach_args *pa;
    714  1.6.2.12  bouyer {
    715  1.6.2.12  bouyer 	pcireg_t csr;
    716  1.6.2.12  bouyer 	const char *failreason = NULL;
    717  1.6.2.12  bouyer 
    718  1.6.2.12  bouyer 	/*
    719  1.6.2.12  bouyer 	 * Check to see if something appears to be there.
    720  1.6.2.12  bouyer 	 */
    721  1.6.2.12  bouyer 	if (!wdcprobe(&sc->wdc_channels[chan])) {
    722  1.6.2.12  bouyer 		failreason = "not responding; disabled or no drives?";
    723  1.6.2.12  bouyer 		goto out;
    724  1.6.2.12  bouyer 	}
    725  1.6.2.12  bouyer 
    726  1.6.2.12  bouyer 	/*
    727  1.6.2.12  bouyer 	 * Now, make sure it's actually attributable to this PCI IDE
    728  1.6.2.12  bouyer 	 * channel by trying to access the channel again while the
    729  1.6.2.12  bouyer 	 * PCI IDE controller's I/O space is disabled.  (If the
    730  1.6.2.12  bouyer 	 * channel no longer appears to be there, it belongs to
    731  1.6.2.12  bouyer 	 * this controller.)  YUCK!
    732  1.6.2.12  bouyer 	 */
    733  1.6.2.12  bouyer 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    734  1.6.2.12  bouyer 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    735  1.6.2.12  bouyer 	    csr & ~PCI_COMMAND_IO_ENABLE);
    736  1.6.2.12  bouyer 	if (wdcprobe(&sc->wdc_channels[chan]))
    737  1.6.2.12  bouyer 		failreason = "other hardware responding at addresses";
    738  1.6.2.12  bouyer 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);
    739  1.6.2.12  bouyer 
    740  1.6.2.12  bouyer out:
    741  1.6.2.12  bouyer 	return (failreason);
    742  1.6.2.12  bouyer }
    743  1.6.2.12  bouyer 
    744   1.6.2.1  bouyer void
    745   1.6.2.1  bouyer piix_setup_cap(sc)
    746   1.6.2.1  bouyer 	struct pciide_softc *sc;
    747   1.6.2.1  bouyer {
    748   1.6.2.1  bouyer 	if (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82371AB_IDE)
    749   1.6.2.1  bouyer 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
    750   1.6.2.2  bouyer 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_PIO |
    751   1.6.2.2  bouyer 	    WDC_CAPABILITY_DMA;
    752   1.6.2.1  bouyer 	sc->sc_wdcdev.pio_mode = 4;
    753   1.6.2.1  bouyer 	sc->sc_wdcdev.dma_mode = 2;
    754   1.6.2.1  bouyer }
    755   1.6.2.1  bouyer 
    756   1.6.2.1  bouyer void
    757   1.6.2.1  bouyer piix_setup_chip(sc, pc, tag)
    758   1.6.2.1  bouyer 	struct pciide_softc *sc;
    759   1.6.2.1  bouyer 	pci_chipset_tag_t pc;
    760   1.6.2.1  bouyer 	pcitag_t tag;
    761   1.6.2.1  bouyer {
    762   1.6.2.1  bouyer 	struct channel_softc *chp;
    763   1.6.2.1  bouyer 	u_int8_t mode[2];
    764   1.6.2.1  bouyer 	u_int8_t channel, drive;
    765   1.6.2.1  bouyer 	u_int32_t idetim, sidetim, idedma_ctl;
    766   1.6.2.1  bouyer 	struct ata_drive_datas *drvp;
    767   1.6.2.1  bouyer 
    768   1.6.2.1  bouyer 	idetim = sidetim = 0;
    769   1.6.2.1  bouyer 
    770   1.6.2.1  bouyer 	WDCDEBUG_PRINT(("piix_setup_chip: old idetim=0x%x, sidetim=0x%x\n",
    771   1.6.2.1  bouyer 	    pci_conf_read(pc, tag, PIIX_IDETIM),
    772   1.6.2.1  bouyer 	    pci_conf_read(pc, tag, PIIX_SIDETIM)), DEBUG_PROBE);
    773   1.6.2.1  bouyer 
    774   1.6.2.1  bouyer 	for (channel = 0; channel < PCIIDE_NUM_CHANNELS; channel++) {
    775   1.6.2.1  bouyer 		chp = &sc->wdc_channels[channel];
    776   1.6.2.1  bouyer 		drvp = chp->ch_drive;
    777   1.6.2.1  bouyer 		idedma_ctl = 0;
    778   1.6.2.1  bouyer 		/* Enable IDE registers decode */
    779   1.6.2.1  bouyer 		idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE,
    780   1.6.2.1  bouyer 		    channel);
    781   1.6.2.1  bouyer 
    782   1.6.2.1  bouyer 		/* setup DMA if needed */
    783   1.6.2.1  bouyer 		for (drive = 0; drive < 2; drive++) {
    784   1.6.2.1  bouyer 			if (drvp[drive].drive_flags & DRIVE_DMA &&
    785   1.6.2.1  bouyer 			    pciide_dma_table_setup(sc, channel, drive) != 0) {
    786   1.6.2.1  bouyer 				drvp[drive].drive_flags &= ~DRIVE_DMA;
    787   1.6.2.1  bouyer 			}
    788   1.6.2.1  bouyer 		}
    789   1.6.2.1  bouyer 
    790   1.6.2.1  bouyer 		/*
    791   1.6.2.1  bouyer 		 * Here we have to mess up with drives mode: PIIX can't have
    792   1.6.2.1  bouyer 		 * different timings for master and slave drives.
    793   1.6.2.1  bouyer 		 * We need to find the best combination.
    794   1.6.2.1  bouyer 		 */
    795   1.6.2.1  bouyer 
    796   1.6.2.1  bouyer 		/* If both drives supports DMA, takes the lower mode */
    797   1.6.2.1  bouyer 		if ((drvp[0].drive_flags & DRIVE_DMA) &&
    798   1.6.2.1  bouyer 		    (drvp[1].drive_flags & DRIVE_DMA)) {
    799   1.6.2.1  bouyer 			mode[0] = mode[1] =
    800   1.6.2.1  bouyer 			    min(drvp[0].DMA_mode, drvp[1].DMA_mode);
    801   1.6.2.7  bouyer 			    drvp[0].DMA_mode = mode[0];
    802   1.6.2.1  bouyer 			goto ok;
    803   1.6.2.1  bouyer 		}
    804   1.6.2.1  bouyer 		/*
    805   1.6.2.1  bouyer 		 * If only one drive supports DMA, use its mode, and
    806   1.6.2.1  bouyer 		 * put the other one in PIO mode 0 if mode not compatible
    807   1.6.2.1  bouyer 		 */
    808   1.6.2.1  bouyer 		if (drvp[0].drive_flags & DRIVE_DMA) {
    809   1.6.2.1  bouyer 			mode[0] = drvp[0].DMA_mode;
    810   1.6.2.1  bouyer 			mode[1] = drvp[1].PIO_mode;
    811   1.6.2.7  bouyer 			if (piix_isp_pio[mode[1]] != piix_isp_dma[mode[0]] ||
    812   1.6.2.7  bouyer 			    piix_rtc_pio[mode[1]] != piix_rtc_dma[mode[0]])
    813   1.6.2.1  bouyer 				mode[1] = 0;
    814   1.6.2.1  bouyer 			goto ok;
    815   1.6.2.1  bouyer 		}
    816   1.6.2.1  bouyer 		if (drvp[1].drive_flags & DRIVE_DMA) {
    817   1.6.2.1  bouyer 			mode[1] = drvp[1].DMA_mode;
    818   1.6.2.1  bouyer 			mode[0] = drvp[0].PIO_mode;
    819   1.6.2.7  bouyer 			if (piix_isp_pio[mode[0]] != piix_isp_dma[mode[1]] ||
    820   1.6.2.7  bouyer 			    piix_rtc_pio[mode[0]] != piix_rtc_dma[mode[1]])
    821   1.6.2.1  bouyer 				mode[0] = 0;
    822   1.6.2.1  bouyer 			goto ok;
    823   1.6.2.1  bouyer 		}
    824   1.6.2.1  bouyer 		/*
    825   1.6.2.1  bouyer 		 * If both drives are not DMA, takes the lower mode, unless
    826   1.6.2.7  bouyer 		 * one of them is PIO mode < 2
    827   1.6.2.1  bouyer 		 */
    828   1.6.2.7  bouyer 		if (drvp[0].PIO_mode < 2) {
    829   1.6.2.1  bouyer 			mode[0] = 0;
    830   1.6.2.1  bouyer 			mode[1] = drvp[1].PIO_mode;
    831   1.6.2.7  bouyer 		} else if (drvp[1].PIO_mode < 2) {
    832   1.6.2.1  bouyer 			mode[1] = 0;
    833   1.6.2.1  bouyer 			mode[0] = drvp[0].PIO_mode;
    834   1.6.2.1  bouyer 		} else {
    835   1.6.2.1  bouyer 			mode[0] = mode[1] =
    836   1.6.2.1  bouyer 			    min(drvp[1].PIO_mode, drvp[0].PIO_mode);
    837   1.6.2.1  bouyer 		}
    838   1.6.2.1  bouyer ok:		/* The modes are setup */
    839   1.6.2.1  bouyer 		for (drive = 0; drive < 2; drive++) {
    840   1.6.2.1  bouyer 			if (drvp[drive].drive_flags & DRIVE_DMA) {
    841   1.6.2.7  bouyer 				drvp[drive].DMA_mode = mode[drive];
    842   1.6.2.1  bouyer 				idetim |= piix_setup_idetim_timings(
    843   1.6.2.1  bouyer 				    mode[drive], 1, channel);
    844   1.6.2.1  bouyer 				goto end;
    845   1.6.2.7  bouyer 			} else
    846   1.6.2.7  bouyer 				drvp[drive].PIO_mode = mode[drive];
    847   1.6.2.1  bouyer 		}
    848   1.6.2.1  bouyer 		/* If we are there, none of the drives are DMA */
    849   1.6.2.7  bouyer 		if (mode[0] >= 2)
    850   1.6.2.1  bouyer 			idetim |= piix_setup_idetim_timings(
    851   1.6.2.1  bouyer 			    mode[0], 0, channel);
    852   1.6.2.1  bouyer 		else
    853   1.6.2.1  bouyer 			idetim |= piix_setup_idetim_timings(
    854   1.6.2.1  bouyer 			    mode[1], 0, channel);
    855   1.6.2.1  bouyer end:		/*
    856   1.6.2.1  bouyer 		 * timing mode is now set up in the controller. Enable
    857   1.6.2.1  bouyer 		 * it per-drive
    858   1.6.2.1  bouyer 		 */
    859   1.6.2.1  bouyer 		for (drive = 0; drive < 2; drive++) {
    860   1.6.2.5  bouyer 			/* If no drive, skip */
    861   1.6.2.5  bouyer 			if ((drvp[drive].drive_flags & DRIVE) == 0)
    862   1.6.2.5  bouyer 				continue;
    863   1.6.2.7  bouyer 			idetim |= piix_setup_idetim_drvs(&drvp[drive]);
    864   1.6.2.7  bouyer 			printf("%s:%d:%d: using PIO mode %d",
    865   1.6.2.7  bouyer 			    sc->sc_wdcdev.sc_dev.dv_xname,
    866   1.6.2.7  bouyer 			    channel, drive, drvp[drive].PIO_mode);
    867   1.6.2.1  bouyer 			if (drvp[drive].drive_flags & DRIVE_DMA) {
    868   1.6.2.1  bouyer 				idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    869   1.6.2.7  bouyer 				printf(", DMA mode %d", drvp[drive].DMA_mode);
    870   1.6.2.1  bouyer 			}
    871   1.6.2.7  bouyer 			printf("\n");
    872   1.6.2.1  bouyer 		}
    873   1.6.2.1  bouyer 		if (idedma_ctl != 0) {
    874   1.6.2.1  bouyer 			/* Add software bits in status register */
    875   1.6.2.1  bouyer 			bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    876   1.6.2.1  bouyer 			    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
    877   1.6.2.1  bouyer 			    idedma_ctl);
    878   1.6.2.1  bouyer 		}
    879   1.6.2.1  bouyer 	}
    880   1.6.2.1  bouyer 	WDCDEBUG_PRINT(("piix_setup_chip: idetim=0x%x, sidetim=0x%x\n",
    881   1.6.2.1  bouyer 	    idetim, sidetim), DEBUG_PROBE);
    882   1.6.2.1  bouyer 	pci_conf_write(pc, tag, PIIX_IDETIM, idetim);
    883   1.6.2.1  bouyer 	pci_conf_write(pc, tag, PIIX_SIDETIM, sidetim);
    884   1.6.2.1  bouyer }
    885   1.6.2.1  bouyer 
    886   1.6.2.1  bouyer void
    887   1.6.2.1  bouyer piix3_4_setup_chip(sc, pc, tag)
    888   1.6.2.1  bouyer 	struct pciide_softc *sc;
    889   1.6.2.1  bouyer 	pci_chipset_tag_t pc;
    890   1.6.2.1  bouyer 	pcitag_t tag;
    891   1.6.2.1  bouyer {
    892   1.6.2.1  bouyer 	int channel, drive;
    893   1.6.2.1  bouyer 	struct channel_softc *chp;
    894   1.6.2.1  bouyer 	struct ata_drive_datas *drvp;
    895   1.6.2.6  bouyer 	u_int32_t idetim, sidetim, udmareg, idedma_ctl;
    896   1.6.2.1  bouyer 
    897   1.6.2.6  bouyer 	idetim = sidetim = udmareg = 0;
    898   1.6.2.1  bouyer 
    899   1.6.2.6  bouyer 	WDCDEBUG_PRINT(("piix3_4_setup_chip: old idetim=0x%x, sidetim=0x%x",
    900   1.6.2.1  bouyer 	    pci_conf_read(pc, tag, PIIX_IDETIM),
    901   1.6.2.1  bouyer 	    pci_conf_read(pc, tag, PIIX_SIDETIM)), DEBUG_PROBE);
    902   1.6.2.6  bouyer 	if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
    903   1.6.2.6  bouyer 		WDCDEBUG_PRINT((", udamreg 0x%x",
    904   1.6.2.6  bouyer 		    pci_conf_read(pc, tag, PIIX_UDMAREG)),
    905   1.6.2.6  bouyer 		    DEBUG_PROBE);
    906   1.6.2.6  bouyer 	}
    907   1.6.2.6  bouyer 	WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
    908   1.6.2.6  bouyer 
    909   1.6.2.1  bouyer 	for (channel = 0; channel < PCIIDE_NUM_CHANNELS; channel++) {
    910   1.6.2.1  bouyer 		chp = &sc->wdc_channels[channel];
    911   1.6.2.1  bouyer 		idedma_ctl = 0;
    912   1.6.2.1  bouyer 		/* Enable IDE registers decode */
    913   1.6.2.1  bouyer 		idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE,
    914   1.6.2.1  bouyer 		    channel);
    915   1.6.2.1  bouyer 		for (drive = 0; drive < 2; drive++) {
    916   1.6.2.1  bouyer 			drvp = &chp->ch_drive[drive];
    917   1.6.2.1  bouyer 			/* If no drive, skip */
    918   1.6.2.1  bouyer 			if ((drvp->drive_flags & DRIVE) == 0)
    919   1.6.2.1  bouyer 				continue;
    920   1.6.2.1  bouyer 			/* add timing values, setup DMA if needed */
    921   1.6.2.1  bouyer 			if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
    922   1.6.2.1  bouyer 			    (drvp->drive_flags & DRIVE_UDMA) == 0) ||
    923   1.6.2.8  bouyer 			    sc->sc_dma_ok == 0) {
    924   1.6.2.8  bouyer 				drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
    925   1.6.2.1  bouyer 				goto pio;
    926   1.6.2.8  bouyer 			}
    927   1.6.2.8  bouyer 			if (pciide_dma_table_setup(sc, channel, drive) != 0) {
    928   1.6.2.9  bouyer 				/* Abort DMA setup */
    929   1.6.2.8  bouyer 				drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
    930   1.6.2.9  bouyer 				goto pio;
    931   1.6.2.8  bouyer 			}
    932   1.6.2.1  bouyer 			if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
    933   1.6.2.1  bouyer 			    (drvp->drive_flags & DRIVE_UDMA)) {
    934   1.6.2.1  bouyer 				/* use Ultra/DMA */
    935   1.6.2.1  bouyer 				drvp->drive_flags &= ~DRIVE_DMA;
    936   1.6.2.6  bouyer 				udmareg |= PIIX_UDMACTL_DRV_EN(
    937   1.6.2.1  bouyer 				    channel, drive);
    938   1.6.2.6  bouyer 				udmareg |= PIIX_UDMATIM_SET(
    939   1.6.2.1  bouyer 				    piix4_sct_udma[drvp->UDMA_mode],
    940   1.6.2.1  bouyer 				    channel, drive);
    941   1.6.2.1  bouyer 			} else {
    942   1.6.2.1  bouyer 				/* use Multiword DMA */
    943   1.6.2.1  bouyer 				drvp->drive_flags &= ~DRIVE_UDMA;
    944   1.6.2.1  bouyer 				if (drive == 0) {
    945   1.6.2.1  bouyer 					idetim |= piix_setup_idetim_timings(
    946   1.6.2.1  bouyer 					    drvp->DMA_mode, 1, channel);
    947   1.6.2.1  bouyer 				} else {
    948   1.6.2.1  bouyer 					sidetim |= piix_setup_sidetim_timings(
    949   1.6.2.1  bouyer 						drvp->DMA_mode, 1, channel);
    950   1.6.2.1  bouyer 					idetim =PIIX_IDETIM_SET(idetim,
    951   1.6.2.1  bouyer 					    PIIX_IDETIM_SITRE, channel);
    952   1.6.2.1  bouyer 				}
    953   1.6.2.1  bouyer 			}
    954   1.6.2.1  bouyer 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    955   1.6.2.1  bouyer 
    956   1.6.2.1  bouyer pio:			/* use PIO mode */
    957  1.6.2.10  bouyer 			idetim |= piix_setup_idetim_drvs(drvp);
    958   1.6.2.1  bouyer 			if (drive == 0) {
    959   1.6.2.1  bouyer 				idetim |= piix_setup_idetim_timings(
    960   1.6.2.1  bouyer 				    drvp->PIO_mode, 0, channel);
    961   1.6.2.1  bouyer 			} else {
    962   1.6.2.1  bouyer 				sidetim |= piix_setup_sidetim_timings(
    963   1.6.2.1  bouyer 					drvp->PIO_mode, 0, channel);
    964   1.6.2.1  bouyer 				idetim =PIIX_IDETIM_SET(idetim,
    965   1.6.2.1  bouyer 				    PIIX_IDETIM_SITRE, channel);
    966   1.6.2.1  bouyer 			}
    967   1.6.2.7  bouyer 			printf("%s:%d:%d: using PIO mode %d",
    968   1.6.2.7  bouyer 			    sc->sc_wdcdev.sc_dev.dv_xname,
    969  1.6.2.10  bouyer 			    channel, drive, drvp->PIO_mode);
    970   1.6.2.7  bouyer 			if (drvp[drive].drive_flags & DRIVE_DMA)
    971  1.6.2.10  bouyer 			    printf(", DMA mode %d", drvp->DMA_mode);
    972  1.6.2.10  bouyer 			if (drvp->drive_flags & DRIVE_UDMA)
    973   1.6.2.7  bouyer 			    printf(", UDMA mode %d", drvp->UDMA_mode);
    974   1.6.2.7  bouyer 			printf("\n");
    975   1.6.2.1  bouyer 		}
    976   1.6.2.1  bouyer 		if (idedma_ctl != 0) {
    977   1.6.2.1  bouyer 			/* Add software bits in status register */
    978   1.6.2.1  bouyer 			bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    979   1.6.2.1  bouyer 			    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
    980   1.6.2.1  bouyer 			    idedma_ctl);
    981   1.6.2.1  bouyer 		}
    982   1.6.2.1  bouyer 	}
    983   1.6.2.1  bouyer 
    984   1.6.2.1  bouyer 	WDCDEBUG_PRINT(("piix3_4_setup_chip: idetim=0x%x, sidetim=0x%x",
    985   1.6.2.1  bouyer 	    idetim, sidetim), DEBUG_PROBE);
    986   1.6.2.6  bouyer 	if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
    987   1.6.2.6  bouyer 		WDCDEBUG_PRINT((", udmareg=0x%x", udmareg), DEBUG_PROBE);
    988   1.6.2.6  bouyer 		pci_conf_write(pc, tag, PIIX_UDMAREG, udmareg);
    989   1.6.2.1  bouyer 	}
    990   1.6.2.1  bouyer 	WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
    991   1.6.2.1  bouyer 	pci_conf_write(pc, tag, PIIX_IDETIM, idetim);
    992   1.6.2.1  bouyer 	pci_conf_write(pc, tag, PIIX_SIDETIM, sidetim);
    993   1.6.2.1  bouyer }
    994   1.6.2.1  bouyer 
    995   1.6.2.1  bouyer /* setup ISP and RTC fields, based on mode */
    996   1.6.2.1  bouyer static u_int32_t
    997   1.6.2.1  bouyer piix_setup_idetim_timings(mode, dma, channel)
    998   1.6.2.1  bouyer 	u_int8_t mode;
    999   1.6.2.1  bouyer 	u_int8_t dma;
   1000   1.6.2.1  bouyer 	u_int8_t channel;
   1001   1.6.2.1  bouyer {
   1002   1.6.2.1  bouyer 
   1003   1.6.2.1  bouyer 	if (dma)
   1004   1.6.2.1  bouyer 		return PIIX_IDETIM_SET(0,
   1005   1.6.2.1  bouyer 		    PIIX_IDETIM_ISP_SET(piix_isp_dma[mode]) |
   1006   1.6.2.1  bouyer 		    PIIX_IDETIM_RTC_SET(piix_rtc_dma[mode]),
   1007   1.6.2.1  bouyer 		    channel);
   1008   1.6.2.1  bouyer 	else
   1009   1.6.2.1  bouyer 		return PIIX_IDETIM_SET(0,
   1010   1.6.2.1  bouyer 		    PIIX_IDETIM_ISP_SET(piix_isp_pio[mode]) |
   1011   1.6.2.1  bouyer 		    PIIX_IDETIM_RTC_SET(piix_rtc_pio[mode]),
   1012   1.6.2.1  bouyer 		    channel);
   1013   1.6.2.1  bouyer }
   1014   1.6.2.1  bouyer 
   1015   1.6.2.7  bouyer /* setup DTE, PPE, IE and TIME field based on PIO mode */
   1016   1.6.2.1  bouyer static u_int32_t
   1017   1.6.2.7  bouyer piix_setup_idetim_drvs(drvp)
   1018   1.6.2.7  bouyer 	struct ata_drive_datas *drvp;
   1019   1.6.2.1  bouyer {
   1020   1.6.2.1  bouyer 	u_int32_t ret = 0;
   1021   1.6.2.7  bouyer 	struct channel_softc *chp = drvp->chnl_softc;
   1022   1.6.2.7  bouyer 	u_int8_t channel = chp->channel;
   1023   1.6.2.7  bouyer 	u_int8_t drive = drvp->drive;
   1024   1.6.2.7  bouyer 
   1025   1.6.2.7  bouyer 	/*
   1026   1.6.2.7  bouyer 	 * If drive is using UDMA, timings setups are independant
   1027   1.6.2.7  bouyer 	 * So just check DMA and PIO here.
   1028   1.6.2.7  bouyer 	 */
   1029   1.6.2.7  bouyer 	if (drvp->drive_flags & DRIVE_DMA) {
   1030   1.6.2.7  bouyer 		/* if mode = DMA mode 0, use compatible timings */
   1031   1.6.2.7  bouyer 		if ((drvp->drive_flags & DRIVE_DMA) &&
   1032   1.6.2.7  bouyer 		    drvp->DMA_mode == 0) {
   1033   1.6.2.7  bouyer 			drvp->PIO_mode = 0;
   1034   1.6.2.7  bouyer 			return ret;
   1035   1.6.2.7  bouyer 		}
   1036   1.6.2.7  bouyer 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
   1037   1.6.2.7  bouyer 		/*
   1038   1.6.2.7  bouyer 		 * PIO and DMA timings are the same, use fast timings for PIO
   1039   1.6.2.7  bouyer 		 * too, else use compat timings.
   1040   1.6.2.7  bouyer 		 */
   1041   1.6.2.7  bouyer 		if ((piix_isp_pio[drvp->PIO_mode] !=
   1042   1.6.2.7  bouyer 		    piix_isp_dma[drvp->DMA_mode]) ||
   1043   1.6.2.7  bouyer 		    (piix_rtc_pio[drvp->PIO_mode] !=
   1044   1.6.2.7  bouyer 		    piix_rtc_dma[drvp->DMA_mode]))
   1045   1.6.2.7  bouyer 			drvp->PIO_mode = 0;
   1046   1.6.2.7  bouyer 		/* if PIO mode <= 2, use compat timings for PIO */
   1047   1.6.2.7  bouyer 		if (drvp->PIO_mode <= 2) {
   1048   1.6.2.7  bouyer 			ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_DTE(drive),
   1049   1.6.2.7  bouyer 			    channel);
   1050   1.6.2.7  bouyer 			return ret;
   1051   1.6.2.7  bouyer 		}
   1052   1.6.2.7  bouyer 	}
   1053   1.6.2.1  bouyer 
   1054   1.6.2.7  bouyer 	/*
   1055   1.6.2.7  bouyer 	 * Now setup PIO modes. If mode < 2, use compat timings.
   1056   1.6.2.7  bouyer 	 * Else enable fast timings. Enable IORDY and prefetch/post
   1057   1.6.2.7  bouyer 	 * if PIO mode >= 3.
   1058   1.6.2.7  bouyer 	 */
   1059   1.6.2.7  bouyer 
   1060   1.6.2.7  bouyer 	if (drvp->PIO_mode < 2)
   1061   1.6.2.7  bouyer 		return ret;
   1062   1.6.2.6  bouyer 
   1063   1.6.2.1  bouyer 	ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
   1064   1.6.2.7  bouyer 	if (drvp->PIO_mode >= 3) {
   1065   1.6.2.1  bouyer 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_IE(drive), channel);
   1066   1.6.2.1  bouyer 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_PPE(drive), channel);
   1067   1.6.2.7  bouyer 	}
   1068   1.6.2.1  bouyer 	return ret;
   1069   1.6.2.1  bouyer }
   1070   1.6.2.1  bouyer 
   1071   1.6.2.1  bouyer /* setup values in SIDETIM registers, based on mode */
   1072   1.6.2.1  bouyer static u_int32_t
   1073   1.6.2.1  bouyer piix_setup_sidetim_timings(mode, dma, channel)
   1074   1.6.2.1  bouyer 	u_int8_t mode;
   1075   1.6.2.1  bouyer 	u_int8_t dma;
   1076   1.6.2.1  bouyer 	u_int8_t channel;
   1077   1.6.2.1  bouyer {
   1078   1.6.2.1  bouyer 	if (dma)
   1079   1.6.2.1  bouyer 		return PIIX_SIDETIM_ISP_SET(piix_isp_dma[mode], channel) |
   1080   1.6.2.1  bouyer 		    PIIX_SIDETIM_RTC_SET(piix_rtc_dma[mode], channel);
   1081   1.6.2.1  bouyer 	else
   1082   1.6.2.1  bouyer 		return PIIX_SIDETIM_ISP_SET(piix_isp_pio[mode], channel) |
   1083   1.6.2.1  bouyer 		    PIIX_SIDETIM_RTC_SET(piix_rtc_pio[mode], channel);
   1084   1.6.2.1  bouyer }
   1085   1.6.2.1  bouyer 
   1086  1.6.2.14  bouyer const char*
   1087  1.6.2.14  bouyer piix_compat_channel_probe(sc, pa, chan)
   1088  1.6.2.14  bouyer 	struct pciide_softc *sc;
   1089  1.6.2.14  bouyer 	struct pci_attach_args *pa;
   1090  1.6.2.14  bouyer 	int chan;
   1091  1.6.2.14  bouyer {
   1092  1.6.2.14  bouyer 	u_int32_t idetim = pci_conf_read(pa->pa_pc, pa->pa_tag, PIIX_IDETIM);
   1093  1.6.2.14  bouyer 
   1094  1.6.2.14  bouyer 	if (PIIX_IDETIM_READ(idetim, chan) & PIIX_IDETIM_IDE)
   1095  1.6.2.14  bouyer 		return NULL;
   1096  1.6.2.14  bouyer 	else
   1097  1.6.2.14  bouyer 		return "disabled";
   1098  1.6.2.14  bouyer }
   1099  1.6.2.14  bouyer 
   1100  1.6.2.11  bouyer void
   1101  1.6.2.11  bouyer apollo_setup_cap(sc)
   1102  1.6.2.11  bouyer 	struct pciide_softc *sc;
   1103  1.6.2.11  bouyer {
   1104  1.6.2.11  bouyer 	if (sc->sc_pp->ide_product == PCI_PRODUCT_VIATECH_VT82C586_IDE)
   1105  1.6.2.11  bouyer 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
   1106  1.6.2.11  bouyer 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_PIO |
   1107  1.6.2.11  bouyer 	    WDC_CAPABILITY_DMA;
   1108  1.6.2.11  bouyer 	sc->sc_wdcdev.pio_mode = 4;
   1109  1.6.2.11  bouyer 	sc->sc_wdcdev.dma_mode = 2;
   1110  1.6.2.11  bouyer 
   1111  1.6.2.11  bouyer }
   1112  1.6.2.11  bouyer void
   1113  1.6.2.11  bouyer apollo_setup_chip(sc, pc, tag)
   1114  1.6.2.11  bouyer 	struct pciide_softc *sc;
   1115  1.6.2.11  bouyer 	pci_chipset_tag_t pc;
   1116  1.6.2.11  bouyer 	pcitag_t tag;
   1117  1.6.2.11  bouyer {
   1118  1.6.2.11  bouyer 	u_int32_t udmatim_reg, ideconf_reg, ctlmisc_reg, datatim_reg;
   1119  1.6.2.11  bouyer 	u_int8_t idedma_ctl;
   1120  1.6.2.11  bouyer 	int mode;
   1121  1.6.2.11  bouyer 	int channel, drive;
   1122  1.6.2.11  bouyer 	struct channel_softc *chp;
   1123  1.6.2.11  bouyer 	struct ata_drive_datas *drvp;
   1124  1.6.2.11  bouyer 
   1125  1.6.2.11  bouyer 	ideconf_reg = pci_conf_read(pc, tag, APO_IDECONF);
   1126  1.6.2.11  bouyer 	ctlmisc_reg = pci_conf_read(pc, tag, APO_CTLMISC);
   1127  1.6.2.11  bouyer 
   1128  1.6.2.11  bouyer 	WDCDEBUG_PRINT(("apollo_setup_chip: old APO_IDECONF=0x%x, "
   1129  1.6.2.11  bouyer 	    "APO_CTLMISC=0x%x, APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
   1130  1.6.2.11  bouyer 	    ideconf_reg, ctlmisc_reg,
   1131  1.6.2.11  bouyer 	    pci_conf_read(pc, tag, APO_DATATIM),
   1132  1.6.2.11  bouyer 	    pci_conf_read(pc, tag, APO_UDMA)),
   1133  1.6.2.11  bouyer 	    DEBUG_PROBE);
   1134  1.6.2.11  bouyer 
   1135  1.6.2.11  bouyer 	datatim_reg = 0;
   1136  1.6.2.11  bouyer 	udmatim_reg = 0;
   1137  1.6.2.11  bouyer 	for (channel = 0; channel < PCIIDE_NUM_CHANNELS; channel++) {
   1138  1.6.2.11  bouyer 		chp = &sc->wdc_channels[channel];
   1139  1.6.2.11  bouyer 		idedma_ctl = 0;
   1140  1.6.2.11  bouyer 		for (drive = 0; drive < 2; drive++) {
   1141  1.6.2.11  bouyer 			drvp = &chp->ch_drive[drive];
   1142  1.6.2.11  bouyer 			/* If no drive, skip */
   1143  1.6.2.11  bouyer 			if ((drvp->drive_flags & DRIVE) == 0)
   1144  1.6.2.11  bouyer 				continue;
   1145  1.6.2.11  bouyer 			/* add timing values, setup DMA if needed */
   1146  1.6.2.11  bouyer 			if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
   1147  1.6.2.11  bouyer 			    (drvp->drive_flags & DRIVE_UDMA) == 0) ||
   1148  1.6.2.11  bouyer 			    sc->sc_dma_ok == 0) {
   1149  1.6.2.11  bouyer 				drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
   1150  1.6.2.11  bouyer 				mode = drvp->PIO_mode;
   1151  1.6.2.11  bouyer 				goto pio;
   1152  1.6.2.11  bouyer 			}
   1153  1.6.2.11  bouyer 			if (pciide_dma_table_setup(sc, channel, drive) != 0) {
   1154  1.6.2.11  bouyer 				/* Abort DMA setup */
   1155  1.6.2.11  bouyer 				drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
   1156  1.6.2.11  bouyer 				mode = drvp->PIO_mode;
   1157  1.6.2.11  bouyer 				goto pio;
   1158  1.6.2.11  bouyer 			}
   1159  1.6.2.11  bouyer 			if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
   1160  1.6.2.11  bouyer 			    (drvp->drive_flags & DRIVE_UDMA)) {
   1161  1.6.2.11  bouyer 				/* use Ultra/DMA */
   1162  1.6.2.11  bouyer 				drvp->drive_flags &= ~DRIVE_DMA;
   1163  1.6.2.11  bouyer 				udmatim_reg |= APO_UDMA_EN(channel, drive) |
   1164  1.6.2.11  bouyer 				    APO_UDMA_EN_MTH(channel, drive) |
   1165  1.6.2.11  bouyer 				    APO_UDMA_TIME(channel, drive,
   1166  1.6.2.11  bouyer 					apollo_udma_tim[drvp->UDMA_mode]);
   1167  1.6.2.11  bouyer 				/* can use PIO timings, MW DMA unused */
   1168  1.6.2.11  bouyer 				mode = drvp->PIO_mode;
   1169  1.6.2.11  bouyer 			} else {
   1170  1.6.2.11  bouyer 				/* use Multiword DMA */
   1171  1.6.2.11  bouyer 				drvp->drive_flags &= ~DRIVE_UDMA;
   1172  1.6.2.11  bouyer 				/* mode = min(pio, dma+2) */
   1173  1.6.2.11  bouyer 				if (drvp->PIO_mode <= (drvp->DMA_mode +2))
   1174  1.6.2.11  bouyer 					mode = drvp->PIO_mode;
   1175  1.6.2.11  bouyer 				else
   1176  1.6.2.11  bouyer 					mode = drvp->DMA_mode;
   1177  1.6.2.11  bouyer 			}
   1178  1.6.2.11  bouyer 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1179  1.6.2.11  bouyer 
   1180  1.6.2.11  bouyer pio:			/* setup PIO mode */
   1181  1.6.2.11  bouyer 			datatim_reg |=
   1182  1.6.2.11  bouyer 			    APO_DATATIM_PULSE(channel, drive,
   1183  1.6.2.11  bouyer 				apollo_pio_set[mode]) |
   1184  1.6.2.11  bouyer 			    APO_DATATIM_RECOV(channel, drive,
   1185  1.6.2.11  bouyer 				apollo_pio_rec[mode]);
   1186  1.6.2.11  bouyer 			drvp->PIO_mode = mode;
   1187  1.6.2.11  bouyer 			drvp->DMA_mode = mode + 2;
   1188  1.6.2.11  bouyer 			printf("%s:%d:%d: using PIO mode %d",
   1189  1.6.2.11  bouyer 			    sc->sc_wdcdev.sc_dev.dv_xname,
   1190  1.6.2.11  bouyer 			    channel, drive, drvp->PIO_mode);
   1191  1.6.2.11  bouyer 			if (drvp[drive].drive_flags & DRIVE_DMA)
   1192  1.6.2.11  bouyer 			    printf(", DMA mode %d", drvp->DMA_mode);
   1193  1.6.2.11  bouyer 			if (drvp->drive_flags & DRIVE_UDMA)
   1194  1.6.2.11  bouyer 			    printf(", UDMA mode %d", drvp->UDMA_mode);
   1195  1.6.2.11  bouyer 			printf("\n");
   1196  1.6.2.11  bouyer 		}
   1197  1.6.2.11  bouyer 		if (idedma_ctl != 0) {
   1198  1.6.2.11  bouyer 			/* Add software bits in status register */
   1199  1.6.2.11  bouyer 			bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1200  1.6.2.11  bouyer 			    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
   1201  1.6.2.11  bouyer 			    idedma_ctl);
   1202  1.6.2.11  bouyer 		}
   1203  1.6.2.11  bouyer 	}
   1204  1.6.2.11  bouyer 	WDCDEBUG_PRINT(("apollo_setup_chip: APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
   1205  1.6.2.11  bouyer 	    datatim_reg, udmatim_reg), DEBUG_PROBE);
   1206  1.6.2.11  bouyer 	pci_conf_write(pc, tag, APO_DATATIM, datatim_reg);
   1207  1.6.2.11  bouyer 	pci_conf_write(pc, tag, APO_UDMA, udmatim_reg);
   1208  1.6.2.11  bouyer }
   1209   1.6.2.1  bouyer 
   1210  1.6.2.14  bouyer const char*
   1211  1.6.2.14  bouyer apollo_compat_channel_probe(sc, pa, chan)
   1212  1.6.2.14  bouyer 	struct pciide_softc *sc;
   1213  1.6.2.14  bouyer 	struct pci_attach_args *pa;
   1214  1.6.2.14  bouyer 	int chan;
   1215  1.6.2.14  bouyer {
   1216  1.6.2.14  bouyer 
   1217  1.6.2.14  bouyer 	u_int32_t ideconf = pci_conf_read(pa->pa_pc, pa->pa_tag, APO_IDECONF);
   1218  1.6.2.14  bouyer 
   1219  1.6.2.14  bouyer 	if (ideconf & APO_IDECONF_EN(chan))
   1220  1.6.2.14  bouyer 		return NULL;
   1221  1.6.2.14  bouyer 	else
   1222  1.6.2.14  bouyer 		return "disabled";
   1223  1.6.2.14  bouyer 
   1224  1.6.2.14  bouyer }
   1225  1.6.2.14  bouyer 
   1226  1.6.2.12  bouyer const char*
   1227  1.6.2.12  bouyer cmd_compat_channel_probe(sc, pa, chan)
   1228  1.6.2.12  bouyer 	struct pciide_softc *sc;
   1229  1.6.2.12  bouyer 	struct pci_attach_args *pa;
   1230  1.6.2.12  bouyer 	int chan;
   1231  1.6.2.12  bouyer {
   1232  1.6.2.12  bouyer 
   1233  1.6.2.12  bouyer 	/*
   1234  1.6.2.12  bouyer 	 * with a CMD PCI64x, if we get here, the first channel is enabled:
   1235  1.6.2.12  bouyer 	 * there's no way to disable the first channel without disabling
   1236  1.6.2.12  bouyer 	 * the whole device
   1237  1.6.2.12  bouyer 	 */
   1238  1.6.2.12  bouyer 	if (chan == 0)
   1239  1.6.2.12  bouyer 		return NULL;
   1240  1.6.2.12  bouyer 
   1241  1.6.2.12  bouyer 	/* Second channel is enabled if CMD_CONF_2PORT is set */
   1242  1.6.2.12  bouyer 	if ((pci_conf_read(pa->pa_pc, pa->pa_tag, CMD_CONF_CTRL0) &
   1243  1.6.2.12  bouyer 	    CMD_CONF_2PORT) == 0)
   1244  1.6.2.12  bouyer 		return "disabled";
   1245   1.6.2.1  bouyer 
   1246  1.6.2.12  bouyer 	return NULL;
   1247  1.6.2.12  bouyer }
   1248  1.6.2.12  bouyer 
   1249   1.6.2.1  bouyer int
   1250   1.6.2.1  bouyer pciide_dma_table_setup(sc, channel, drive)
   1251   1.6.2.1  bouyer 	struct pciide_softc *sc;
   1252   1.6.2.1  bouyer 	int channel, drive;
   1253   1.6.2.1  bouyer {
   1254   1.6.2.1  bouyer 	bus_dma_segment_t seg;
   1255   1.6.2.1  bouyer 	int error, rseg;
   1256   1.6.2.1  bouyer 	const bus_size_t dma_table_size =
   1257   1.6.2.1  bouyer 	    sizeof(struct idedma_table) * NIDEDMA_TABLES;
   1258   1.6.2.1  bouyer 	struct pciide_dma_maps *dma_maps =
   1259   1.6.2.1  bouyer 	    &sc->pciide_channels[channel].dma_maps[drive];
   1260   1.6.2.1  bouyer 
   1261   1.6.2.1  bouyer 	/* Allocate memory for the DMA tables and map it */
   1262   1.6.2.1  bouyer 	if ((error = bus_dmamem_alloc(sc->sc_dmat, dma_table_size,
   1263   1.6.2.1  bouyer 	    IDEDMA_TBL_ALIGN, IDEDMA_TBL_ALIGN, &seg, 1, &rseg,
   1264   1.6.2.1  bouyer 	    BUS_DMA_NOWAIT)) != 0) {
   1265  1.6.2.14  bouyer 		printf("%s:%d: unable to allocate table DMA for "
   1266   1.6.2.1  bouyer 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1267   1.6.2.1  bouyer 		    channel, drive, error);
   1268   1.6.2.1  bouyer 		return error;
   1269   1.6.2.1  bouyer 	}
   1270   1.6.2.1  bouyer 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
   1271   1.6.2.1  bouyer 	    dma_table_size,
   1272   1.6.2.1  bouyer 	    (caddr_t *)&dma_maps->dma_table,
   1273   1.6.2.1  bouyer 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
   1274   1.6.2.1  bouyer 		printf("%s:%d: unable to map table DMA for"
   1275   1.6.2.1  bouyer 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1276   1.6.2.1  bouyer 		    channel, drive, error);
   1277   1.6.2.1  bouyer 		return error;
   1278   1.6.2.1  bouyer 	}
   1279   1.6.2.1  bouyer 	WDCDEBUG_PRINT(("pciide_dma_table_setup: table at %p len %ld, "
   1280   1.6.2.1  bouyer 	    "phy 0x%lx\n", dma_maps->dma_table, dma_table_size,
   1281   1.6.2.1  bouyer 	    seg.ds_addr), DEBUG_PROBE);
   1282   1.6.2.1  bouyer 
   1283   1.6.2.1  bouyer 	/* Create and load table DMA map for this disk */
   1284   1.6.2.1  bouyer 	if ((error = bus_dmamap_create(sc->sc_dmat, dma_table_size,
   1285   1.6.2.1  bouyer 	    1, dma_table_size, IDEDMA_TBL_ALIGN, BUS_DMA_NOWAIT,
   1286   1.6.2.1  bouyer 	    &dma_maps->dmamap_table)) != 0) {
   1287  1.6.2.14  bouyer 		printf("%s:%d: unable to create table DMA map for "
   1288   1.6.2.1  bouyer 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1289   1.6.2.1  bouyer 		    channel, drive, error);
   1290   1.6.2.1  bouyer 		return error;
   1291   1.6.2.1  bouyer 	}
   1292   1.6.2.1  bouyer 	if ((error = bus_dmamap_load(sc->sc_dmat,
   1293   1.6.2.1  bouyer 	    dma_maps->dmamap_table,
   1294   1.6.2.1  bouyer 	    dma_maps->dma_table,
   1295   1.6.2.1  bouyer 	    dma_table_size, NULL, BUS_DMA_NOWAIT)) != 0) {
   1296  1.6.2.14  bouyer 		printf("%s:%d: unable to load table DMA map for "
   1297   1.6.2.1  bouyer 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1298   1.6.2.1  bouyer 		    channel, drive, error);
   1299   1.6.2.1  bouyer 		return error;
   1300   1.6.2.1  bouyer 	}
   1301   1.6.2.1  bouyer 	WDCDEBUG_PRINT(("pciide_dma_table_setup: phy addr of table 0x%lx\n",
   1302   1.6.2.1  bouyer 	    dma_maps->dmamap_table->dm_segs[0].ds_addr), DEBUG_PROBE);
   1303   1.6.2.1  bouyer 	/* Create a xfer DMA map for this drive */
   1304   1.6.2.1  bouyer 	if ((error = bus_dmamap_create(sc->sc_dmat, IDEDMA_BYTE_COUNT_MAX,
   1305   1.6.2.1  bouyer 	    NIDEDMA_TABLES, IDEDMA_BYTE_COUNT_MAX, IDEDMA_BYTE_COUNT_ALIGN,
   1306   1.6.2.1  bouyer 	    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
   1307   1.6.2.1  bouyer 	    &dma_maps->dmamap_xfer)) != 0) {
   1308  1.6.2.14  bouyer 		printf("%s:%d: unable to create xfer DMA map for "
   1309   1.6.2.1  bouyer 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1310   1.6.2.1  bouyer 		    channel, drive, error);
   1311   1.6.2.1  bouyer 		return error;
   1312   1.6.2.1  bouyer 	}
   1313   1.6.2.1  bouyer 	return 0;
   1314   1.6.2.1  bouyer }
   1315   1.6.2.1  bouyer 
   1316   1.6.2.1  bouyer int
   1317  1.6.2.14  bouyer pciide_dma_init(v, channel, drive, databuf, datalen, flags)
   1318   1.6.2.1  bouyer 	void *v;
   1319   1.6.2.1  bouyer 	int channel, drive;
   1320   1.6.2.1  bouyer 	void *databuf;
   1321   1.6.2.1  bouyer 	size_t datalen;
   1322  1.6.2.14  bouyer 	int flags;
   1323   1.6.2.1  bouyer {
   1324   1.6.2.1  bouyer 	struct pciide_softc *sc = v;
   1325   1.6.2.1  bouyer 	int error, seg;
   1326   1.6.2.1  bouyer 	struct pciide_dma_maps *dma_maps =
   1327   1.6.2.1  bouyer 	    &sc->pciide_channels[channel].dma_maps[drive];
   1328   1.6.2.1  bouyer 
   1329   1.6.2.1  bouyer 	error = bus_dmamap_load(sc->sc_dmat,
   1330   1.6.2.1  bouyer 	    dma_maps->dmamap_xfer,
   1331   1.6.2.1  bouyer 	    databuf, datalen, NULL, BUS_DMA_NOWAIT);
   1332   1.6.2.1  bouyer 	if (error) {
   1333   1.6.2.1  bouyer 		printf("%s:%d: unable to load xfer DMA map for"
   1334   1.6.2.1  bouyer 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1335   1.6.2.1  bouyer 		    channel, drive, error);
   1336   1.6.2.1  bouyer 		return error;
   1337   1.6.2.1  bouyer 	}
   1338   1.6.2.1  bouyer 
   1339   1.6.2.1  bouyer 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
   1340   1.6.2.1  bouyer 	    dma_maps->dmamap_xfer->dm_mapsize,
   1341  1.6.2.14  bouyer 	    (flags & WDC_DMA_READ) ?
   1342  1.6.2.14  bouyer 	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
   1343   1.6.2.1  bouyer 
   1344   1.6.2.1  bouyer 	WDCDEBUG_PRINT(("pciide_dma_init: %d segs for %p len %d (phy 0x%x)\n",
   1345   1.6.2.1  bouyer 	    dma_maps->dmamap_xfer->dm_nsegs, databuf, datalen,
   1346   1.6.2.1  bouyer 	    vtophys(databuf)), DEBUG_DMA|DEBUG_XFERS);
   1347   1.6.2.1  bouyer 	for (seg = 0; seg < dma_maps->dmamap_xfer->dm_nsegs; seg++) {
   1348   1.6.2.1  bouyer #ifdef DIAGNOSTIC
   1349   1.6.2.1  bouyer 		/* A segment must not cross a 64k boundary */
   1350   1.6.2.1  bouyer 		{
   1351   1.6.2.1  bouyer 		u_long phys = dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
   1352   1.6.2.1  bouyer 		u_long len = dma_maps->dmamap_xfer->dm_segs[seg].ds_len;
   1353   1.6.2.1  bouyer 		if ((phys & ~IDEDMA_BYTE_COUNT_MASK) !=
   1354   1.6.2.1  bouyer 		    ((phys + len - 1) & ~IDEDMA_BYTE_COUNT_MASK)) {
   1355   1.6.2.1  bouyer 			printf("pciide_dma: segment %d physical addr 0x%lx"
   1356   1.6.2.1  bouyer 			    " len 0x%lx not properly aligned\n",
   1357   1.6.2.1  bouyer 			    seg, phys, len);
   1358   1.6.2.1  bouyer 			panic("pciide_dma: buf align");
   1359   1.6.2.1  bouyer 		}
   1360   1.6.2.1  bouyer 		}
   1361   1.6.2.1  bouyer #endif
   1362   1.6.2.1  bouyer 		dma_maps->dma_table[seg].base_addr =
   1363   1.6.2.1  bouyer 		    dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
   1364   1.6.2.1  bouyer 		dma_maps->dma_table[seg].byte_count =
   1365   1.6.2.1  bouyer 		    dma_maps->dmamap_xfer->dm_segs[seg].ds_len &
   1366   1.6.2.1  bouyer 		    IDEDMA_BYTE_COUNT_MASK;
   1367   1.6.2.1  bouyer 		WDCDEBUG_PRINT(("\t seg %d len %d addr 0x%x\n",
   1368   1.6.2.1  bouyer 		   seg, dma_maps->dma_table[seg].byte_count,
   1369   1.6.2.1  bouyer 		   dma_maps->dma_table[seg].base_addr), DEBUG_DMA);
   1370   1.6.2.1  bouyer 
   1371   1.6.2.1  bouyer 	}
   1372   1.6.2.1  bouyer 	dma_maps->dma_table[dma_maps->dmamap_xfer->dm_nsegs -1].byte_count |=
   1373   1.6.2.1  bouyer 		IDEDMA_BYTE_COUNT_EOT;
   1374   1.6.2.1  bouyer 
   1375   1.6.2.1  bouyer 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_table, 0,
   1376   1.6.2.1  bouyer 	    dma_maps->dmamap_table->dm_mapsize,
   1377   1.6.2.1  bouyer 	    BUS_DMASYNC_PREWRITE);
   1378   1.6.2.1  bouyer 
   1379   1.6.2.1  bouyer 	/* Maps are ready. Start DMA function */
   1380   1.6.2.1  bouyer #ifdef DIAGNOSTIC
   1381   1.6.2.1  bouyer 	if (dma_maps->dmamap_table->dm_segs[0].ds_addr & ~IDEDMA_TBL_MASK) {
   1382   1.6.2.1  bouyer 		printf("pciide_dma_init: addr 0x%lx not properly aligned\n",
   1383   1.6.2.1  bouyer 		    dma_maps->dmamap_table->dm_segs[0].ds_addr);
   1384   1.6.2.1  bouyer 		panic("pciide_dma_init: table align");
   1385   1.6.2.1  bouyer 	}
   1386   1.6.2.1  bouyer #endif
   1387   1.6.2.1  bouyer 
   1388   1.6.2.1  bouyer 	WDCDEBUG_PRINT(("phy addr of table at %p = 0x%lx len %ld (%d segs, "
   1389   1.6.2.1  bouyer 	    "phys 0x%x)\n",
   1390   1.6.2.1  bouyer 	    dma_maps->dma_table,
   1391   1.6.2.1  bouyer 	    dma_maps->dmamap_table->dm_segs[0].ds_addr,
   1392   1.6.2.1  bouyer 	    dma_maps->dmamap_table->dm_segs[0].ds_len,
   1393   1.6.2.1  bouyer 	    dma_maps->dmamap_table->dm_nsegs,
   1394   1.6.2.1  bouyer 	    vtophys(dma_maps->dma_table)), DEBUG_DMA);
   1395   1.6.2.1  bouyer 	/* Clear status bits */
   1396   1.6.2.1  bouyer 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1397   1.6.2.1  bouyer 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel,
   1398   1.6.2.1  bouyer 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1399   1.6.2.1  bouyer 		IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel));
   1400   1.6.2.1  bouyer 	/* Write table addr */
   1401   1.6.2.1  bouyer 	bus_space_write_4(sc->sc_dma_iot, sc->sc_dma_ioh,
   1402   1.6.2.1  bouyer 	    IDEDMA_TBL + IDEDMA_SCH_OFFSET * channel,
   1403   1.6.2.1  bouyer 	    dma_maps->dmamap_table->dm_segs[0].ds_addr);
   1404   1.6.2.1  bouyer 	/* set read/write */
   1405   1.6.2.1  bouyer 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1406   1.6.2.1  bouyer 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
   1407  1.6.2.14  bouyer 	    (flags & WDC_DMA_READ) ? IDEDMA_CMD_WRITE: 0);
   1408   1.6.2.1  bouyer 	return 0;
   1409   1.6.2.1  bouyer }
   1410   1.6.2.1  bouyer 
   1411   1.6.2.1  bouyer void
   1412  1.6.2.14  bouyer pciide_dma_start(v, channel, drive, flags)
   1413   1.6.2.1  bouyer 	void *v;
   1414  1.6.2.14  bouyer 	int channel, drive, flags;
   1415   1.6.2.1  bouyer {
   1416   1.6.2.1  bouyer 	struct pciide_softc *sc = v;
   1417   1.6.2.1  bouyer 
   1418   1.6.2.1  bouyer 	WDCDEBUG_PRINT(("pciide_dma_start\n"),DEBUG_XFERS);
   1419   1.6.2.1  bouyer 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1420   1.6.2.1  bouyer 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
   1421   1.6.2.1  bouyer 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1422   1.6.2.1  bouyer 		IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) | IDEDMA_CMD_START);
   1423   1.6.2.1  bouyer }
   1424   1.6.2.1  bouyer 
   1425   1.6.2.1  bouyer int
   1426  1.6.2.14  bouyer pciide_dma_finish(v, channel, drive, flags)
   1427   1.6.2.1  bouyer 	void *v;
   1428   1.6.2.1  bouyer 	int channel, drive;
   1429  1.6.2.14  bouyer 	int flags;
   1430   1.6.2.1  bouyer {
   1431   1.6.2.1  bouyer 	struct pciide_softc *sc = v;
   1432   1.6.2.1  bouyer 	u_int8_t status;
   1433   1.6.2.1  bouyer 	struct pciide_dma_maps *dma_maps =
   1434   1.6.2.1  bouyer 	    &sc->pciide_channels[channel].dma_maps[drive];
   1435   1.6.2.1  bouyer 
   1436   1.6.2.1  bouyer 	/* Unload the map of the data buffer */
   1437   1.6.2.1  bouyer 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
   1438   1.6.2.1  bouyer 	    dma_maps->dmamap_xfer->dm_mapsize,
   1439  1.6.2.14  bouyer 	    (flags & WDC_DMA_READ) ?
   1440  1.6.2.14  bouyer 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   1441   1.6.2.1  bouyer 	bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_xfer);
   1442   1.6.2.1  bouyer 
   1443   1.6.2.1  bouyer 	status = bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1444   1.6.2.1  bouyer 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel);
   1445   1.6.2.1  bouyer 	WDCDEBUG_PRINT(("pciide_dma_finish: status 0x%x\n", status),
   1446   1.6.2.1  bouyer 	    DEBUG_XFERS);
   1447   1.6.2.1  bouyer 
   1448   1.6.2.1  bouyer 	/* stop DMA channel */
   1449   1.6.2.1  bouyer 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1450   1.6.2.1  bouyer 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
   1451   1.6.2.1  bouyer 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1452   1.6.2.1  bouyer 		IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) & ~IDEDMA_CMD_START);
   1453   1.6.2.1  bouyer 
   1454   1.6.2.1  bouyer 	/* Clear status bits */
   1455   1.6.2.1  bouyer 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1456   1.6.2.1  bouyer 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel,
   1457   1.6.2.1  bouyer 	    status);
   1458   1.6.2.1  bouyer 
   1459  1.6.2.14  bouyer 	if ((status & IDEDMA_CTL_ERR) != 0) {
   1460   1.6.2.1  bouyer 		printf("%s:%d:%d: Bus-Master DMA error: status=0x%x\n",
   1461   1.6.2.1  bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname, channel, drive, status);
   1462  1.6.2.14  bouyer 		return -1;
   1463  1.6.2.14  bouyer 	}
   1464  1.6.2.14  bouyer 
   1465  1.6.2.14  bouyer 	if ((flags & WDC_DMA_POLL) == 0 && (status & IDEDMA_CTL_INTR) == 0) {
   1466  1.6.2.14  bouyer 		printf("%s:%d:%d: Bus-Master DMA error: missing interrupt, "
   1467  1.6.2.14  bouyer 		    "status=0x%x\n", sc->sc_wdcdev.sc_dev.dv_xname, channel,
   1468  1.6.2.14  bouyer 		    drive, status);
   1469  1.6.2.14  bouyer 		return -1;
   1470  1.6.2.14  bouyer 	}
   1471  1.6.2.14  bouyer 
   1472  1.6.2.14  bouyer 	if ((status & IDEDMA_CTL_ACT) != 0) {
   1473  1.6.2.14  bouyer 		/* data underrun, may be a valid condition for ATAPI */
   1474   1.6.2.1  bouyer 		return 1;
   1475   1.6.2.1  bouyer 	}
   1476  1.6.2.14  bouyer 
   1477   1.6.2.1  bouyer 	return 0;
   1478       1.1     cgd }
   1479