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