Home | History | Annotate | Line # | Download | only in pci
pciide.c revision 1.33.2.1
      1  1.33.2.1    bouyer /*	$NetBSD: pciide.c,v 1.33.2.1 1999/04/06 17:48:05 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.26    bouyer #define WDCDEBUG
     46      1.26    bouyer 
     47       1.9    bouyer #define DEBUG_DMA   0x01
     48       1.9    bouyer #define DEBUG_XFERS  0x02
     49       1.9    bouyer #define DEBUG_FUNCS  0x08
     50       1.9    bouyer #define DEBUG_PROBE  0x10
     51       1.9    bouyer #ifdef WDCDEBUG
     52      1.26    bouyer int wdcdebug_pciide_mask = 0;
     53       1.9    bouyer #define WDCDEBUG_PRINT(args, level) \
     54       1.9    bouyer 	if (wdcdebug_pciide_mask & (level)) printf args
     55       1.9    bouyer #else
     56       1.9    bouyer #define WDCDEBUG_PRINT(args, level)
     57       1.9    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.9    bouyer #include <sys/malloc.h>
     62       1.9    bouyer 
     63       1.9    bouyer #include <vm/vm.h>
     64       1.9    bouyer #include <vm/vm_param.h>
     65       1.9    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.9    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.9    bouyer #include <dev/pci/pciide_piix_reg.h>
     73       1.9    bouyer #include <dev/pci/pciide_apollo_reg.h>
     74       1.9    bouyer #include <dev/pci/pciide_cmd_reg.h>
     75      1.18  drochner #include <dev/pci/pciide_cy693_reg.h>
     76      1.18  drochner #include <dev/pci/pciide_sis_reg.h>
     77      1.30    bouyer #include <dev/pci/pciide_acer_reg.h>
     78       1.9    bouyer #include <dev/ata/atavar.h>
     79       1.6       cgd #include <dev/ic/wdcreg.h>
     80       1.9    bouyer #include <dev/ic/wdcvar.h>
     81       1.1       cgd 
     82      1.14    bouyer /* inlines for reading/writing 8-bit PCI registers */
     83      1.14    bouyer static __inline u_int8_t pciide_pci_read __P((pci_chipset_tag_t, pcitag_t,
     84      1.14    bouyer 		int));
     85      1.14    bouyer static __inline u_int8_t
     86      1.14    bouyer pciide_pci_read(pc, pa, reg)
     87      1.14    bouyer 	pci_chipset_tag_t pc;
     88      1.14    bouyer 	pcitag_t pa;
     89      1.14    bouyer 	int reg;
     90      1.14    bouyer {
     91      1.21    bouyer 	return (
     92      1.21    bouyer 	    pci_conf_read(pc, pa, (reg & ~0x03)) >> ((reg & 0x03) * 8) & 0xff);
     93      1.14    bouyer }
     94      1.14    bouyer 
     95      1.14    bouyer 
     96      1.14    bouyer static __inline void pciide_pci_write __P((pci_chipset_tag_t, pcitag_t,
     97      1.14    bouyer 		int, u_int8_t));
     98      1.14    bouyer static __inline void
     99      1.14    bouyer pciide_pci_write(pc, pa, reg, val)
    100      1.14    bouyer 	pci_chipset_tag_t pc;
    101      1.14    bouyer 	pcitag_t pa;
    102      1.14    bouyer 	int reg;
    103      1.14    bouyer 	u_int8_t val;
    104      1.14    bouyer {
    105      1.14    bouyer 	pcireg_t pcival;
    106      1.14    bouyer 
    107      1.14    bouyer 	pcival = pci_conf_read(pc, pa, (reg & ~0x03));
    108      1.21    bouyer 	pcival &= ~(0xff << ((reg & 0x03) * 8));
    109      1.21    bouyer 	pcival |= (val << ((reg & 0x03) * 8));
    110      1.14    bouyer 	pci_conf_write(pc, pa, (reg & ~0x03), pcival);
    111      1.14    bouyer }
    112      1.14    bouyer 
    113       1.1       cgd struct pciide_softc {
    114       1.9    bouyer 	struct wdc_softc	sc_wdcdev;	/* common wdc definitions */
    115      1.28    bouyer 	pci_chipset_tag_t	sc_pc;		/* PCI registers info */
    116      1.28    bouyer 	pcitag_t		sc_tag;
    117       1.1       cgd 	void			*sc_pci_ih;	/* PCI interrupt handle */
    118       1.5       cgd 	int			sc_dma_ok;	/* bus-master DMA info */
    119       1.2       cgd 	bus_space_tag_t		sc_dma_iot;
    120       1.2       cgd 	bus_space_handle_t	sc_dma_ioh;
    121       1.9    bouyer 	bus_dma_tag_t		sc_dmat;
    122       1.9    bouyer 	/* Chip description */
    123       1.9    bouyer 	const struct pciide_product_desc *sc_pp;
    124       1.9    bouyer 	/* common definitions */
    125      1.18  drochner 	struct channel_softc *wdc_chanarray[PCIIDE_NUM_CHANNELS];
    126       1.9    bouyer 	/* internal bookkeeping */
    127       1.1       cgd 	struct pciide_channel {			/* per-channel data */
    128      1.18  drochner 		struct channel_softc wdc_channel; /* generic part */
    129      1.18  drochner 		char		*name;
    130       1.5       cgd 		int		hw_ok;		/* hardware mapped & OK? */
    131       1.1       cgd 		int		compat;		/* is it compat? */
    132       1.1       cgd 		void		*ih;		/* compat or pci handle */
    133       1.9    bouyer 		/* DMA tables and DMA map for xfer, for each drive */
    134       1.9    bouyer 		struct pciide_dma_maps {
    135       1.9    bouyer 			bus_dmamap_t    dmamap_table;
    136       1.9    bouyer 			struct idedma_table *dma_table;
    137       1.9    bouyer 			bus_dmamap_t    dmamap_xfer;
    138       1.9    bouyer 		} dma_maps[2];
    139       1.9    bouyer 	} pciide_channels[PCIIDE_NUM_CHANNELS];
    140       1.9    bouyer };
    141       1.9    bouyer 
    142       1.9    bouyer void default_setup_cap __P((struct pciide_softc*));
    143      1.28    bouyer void default_setup_chip __P((struct pciide_softc*));
    144      1.28    bouyer void default_channel_map __P((struct pci_attach_args *,
    145      1.28    bouyer 		struct pciide_channel *));
    146       1.9    bouyer 
    147       1.9    bouyer void piix_setup_cap __P((struct pciide_softc*));
    148      1.28    bouyer void piix_setup_chip __P((struct pciide_softc*));
    149      1.28    bouyer void piix_setup_channel __P((struct channel_softc*));
    150      1.28    bouyer void piix3_4_setup_chip __P((struct pciide_softc*));
    151      1.28    bouyer void piix3_4_setup_channel __P((struct channel_softc*));
    152      1.28    bouyer void piix_channel_map __P((struct pci_attach_args *, struct pciide_channel *));
    153       1.9    bouyer static u_int32_t piix_setup_idetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
    154       1.9    bouyer static u_int32_t piix_setup_idetim_drvs __P((struct ata_drive_datas*));
    155       1.9    bouyer static u_int32_t piix_setup_sidetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
    156       1.9    bouyer 
    157       1.9    bouyer void apollo_setup_cap __P((struct pciide_softc*));
    158      1.28    bouyer void apollo_setup_chip __P((struct pciide_softc*));
    159      1.28    bouyer void apollo_setup_channel __P((struct channel_softc*));
    160      1.28    bouyer void apollo_channel_map __P((struct pci_attach_args *,
    161      1.28    bouyer 		struct pciide_channel *));
    162       1.9    bouyer 
    163      1.14    bouyer void cmd0643_6_setup_cap __P((struct pciide_softc*));
    164      1.28    bouyer void cmd0643_6_setup_chip __P((struct pciide_softc*));
    165      1.28    bouyer void cmd0643_6_setup_channel __P((struct channel_softc*));
    166      1.28    bouyer void cmd_channel_map __P((struct pci_attach_args *, struct pciide_channel *));
    167      1.18  drochner 
    168      1.18  drochner void cy693_setup_cap __P((struct pciide_softc*));
    169      1.28    bouyer void cy693_setup_chip __P((struct pciide_softc*));
    170      1.28    bouyer void cy693_setup_channel __P((struct channel_softc*));
    171      1.28    bouyer void cy693_channel_map __P((struct pci_attach_args *, struct pciide_channel *));
    172      1.18  drochner 
    173      1.18  drochner void sis_setup_cap __P((struct pciide_softc*));
    174      1.28    bouyer void sis_setup_chip __P((struct pciide_softc*));
    175      1.28    bouyer void sis_setup_channel __P((struct channel_softc*));
    176      1.28    bouyer void sis_channel_map __P((struct pci_attach_args *, struct pciide_channel *));
    177       1.9    bouyer 
    178      1.30    bouyer void acer_setup_cap __P((struct pciide_softc*));
    179      1.30    bouyer void acer_setup_chip __P((struct pciide_softc*));
    180      1.30    bouyer void acer_setup_channel __P((struct channel_softc*));
    181      1.30    bouyer void acer_channel_map __P((struct pci_attach_args *, struct pciide_channel *));
    182      1.30    bouyer 
    183      1.28    bouyer void pciide_channel_dma_setup __P((struct pciide_channel *));
    184       1.9    bouyer int  pciide_dma_table_setup __P((struct pciide_softc*, int, int));
    185       1.9    bouyer int  pciide_dma_init __P((void*, int, int, void *, size_t, int));
    186       1.9    bouyer void pciide_dma_start __P((void*, int, int, int));
    187       1.9    bouyer int  pciide_dma_finish __P((void*, int, int, int));
    188      1.28    bouyer void pciide_print_modes __P((struct pciide_channel *));
    189       1.9    bouyer 
    190       1.9    bouyer struct pciide_product_desc {
    191       1.9    bouyer     u_int32_t ide_product;
    192       1.9    bouyer     int ide_flags;
    193      1.18  drochner     int ide_num_channels;
    194       1.9    bouyer     const char *ide_name;
    195       1.9    bouyer     /* init controller's capabilities for drives probe */
    196       1.9    bouyer     void (*setup_cap) __P((struct pciide_softc*));
    197       1.9    bouyer     /* init controller after drives probe */
    198      1.28    bouyer     void (*setup_chip) __P((struct pciide_softc*));
    199      1.18  drochner     /* map channel if possible/necessary */
    200      1.28    bouyer     void (*channel_map) __P((struct pci_attach_args *,
    201      1.28    bouyer 		struct pciide_channel *));
    202       1.9    bouyer };
    203       1.9    bouyer 
    204       1.9    bouyer /* Flags for ide_flags */
    205       1.9    bouyer #define CMD_PCI064x_IOEN 0x01 /* CMD-style PCI_COMMAND_IO_ENABLE */
    206       1.9    bouyer #define ONE_QUEUE         0x02 /* device need serialised access */
    207       1.9    bouyer 
    208       1.9    bouyer /* Default product description for devices not known from this controller */
    209       1.9    bouyer const struct pciide_product_desc default_product_desc = {
    210       1.9    bouyer     0,
    211       1.9    bouyer     0,
    212      1.18  drochner     PCIIDE_NUM_CHANNELS,
    213       1.9    bouyer     "Generic PCI IDE controller",
    214       1.9    bouyer     default_setup_cap,
    215       1.9    bouyer     default_setup_chip,
    216      1.18  drochner     default_channel_map
    217       1.9    bouyer };
    218       1.1       cgd 
    219       1.9    bouyer 
    220       1.9    bouyer const struct pciide_product_desc pciide_intel_products[] =  {
    221       1.9    bouyer     { PCI_PRODUCT_INTEL_82092AA,
    222       1.9    bouyer       0,
    223      1.18  drochner       PCIIDE_NUM_CHANNELS,
    224       1.9    bouyer       "Intel 82092AA IDE controller",
    225       1.9    bouyer       default_setup_cap,
    226       1.9    bouyer       default_setup_chip,
    227      1.18  drochner       default_channel_map
    228       1.9    bouyer     },
    229       1.9    bouyer     { PCI_PRODUCT_INTEL_82371FB_IDE,
    230       1.9    bouyer       0,
    231      1.18  drochner       PCIIDE_NUM_CHANNELS,
    232       1.9    bouyer       "Intel 82371FB IDE controller (PIIX)",
    233       1.9    bouyer       piix_setup_cap,
    234       1.9    bouyer       piix_setup_chip,
    235      1.18  drochner       piix_channel_map
    236       1.9    bouyer     },
    237       1.9    bouyer     { PCI_PRODUCT_INTEL_82371SB_IDE,
    238       1.9    bouyer       0,
    239      1.18  drochner       PCIIDE_NUM_CHANNELS,
    240       1.9    bouyer       "Intel 82371SB IDE Interface (PIIX3)",
    241       1.9    bouyer       piix_setup_cap,
    242       1.9    bouyer       piix3_4_setup_chip,
    243      1.18  drochner       piix_channel_map
    244       1.9    bouyer     },
    245       1.9    bouyer     { PCI_PRODUCT_INTEL_82371AB_IDE,
    246       1.9    bouyer       0,
    247      1.18  drochner       PCIIDE_NUM_CHANNELS,
    248       1.9    bouyer       "Intel 82371AB IDE controller (PIIX4)",
    249       1.9    bouyer       piix_setup_cap,
    250       1.9    bouyer       piix3_4_setup_chip,
    251      1.18  drochner       piix_channel_map
    252       1.9    bouyer     },
    253       1.9    bouyer     { 0,
    254       1.9    bouyer       0,
    255      1.18  drochner       0,
    256       1.9    bouyer       NULL,
    257       1.9    bouyer     }
    258       1.9    bouyer };
    259       1.9    bouyer const struct pciide_product_desc pciide_cmd_products[] =  {
    260       1.9    bouyer     { PCI_PRODUCT_CMDTECH_640,
    261       1.9    bouyer       ONE_QUEUE | CMD_PCI064x_IOEN,
    262      1.18  drochner       PCIIDE_NUM_CHANNELS,
    263       1.9    bouyer       "CMD Technology PCI0640",
    264       1.9    bouyer       default_setup_cap,
    265       1.9    bouyer       default_setup_chip,
    266      1.18  drochner       cmd_channel_map
    267       1.9    bouyer     },
    268      1.14    bouyer     { PCI_PRODUCT_CMDTECH_643,
    269      1.14    bouyer       ONE_QUEUE | CMD_PCI064x_IOEN,
    270      1.18  drochner       PCIIDE_NUM_CHANNELS,
    271      1.14    bouyer       "CMD Technology PCI0643",
    272      1.14    bouyer       cmd0643_6_setup_cap,
    273      1.14    bouyer       cmd0643_6_setup_chip,
    274      1.18  drochner       cmd_channel_map
    275      1.14    bouyer     },
    276      1.14    bouyer     { PCI_PRODUCT_CMDTECH_646,
    277      1.14    bouyer       ONE_QUEUE | CMD_PCI064x_IOEN,
    278      1.18  drochner       PCIIDE_NUM_CHANNELS,
    279      1.14    bouyer       "CMD Technology PCI0646",
    280      1.14    bouyer       cmd0643_6_setup_cap,
    281      1.14    bouyer       cmd0643_6_setup_chip,
    282      1.18  drochner       cmd_channel_map
    283      1.14    bouyer     },
    284       1.9    bouyer     { 0,
    285       1.9    bouyer       0,
    286      1.18  drochner       0,
    287       1.9    bouyer       NULL,
    288       1.9    bouyer     }
    289       1.9    bouyer };
    290       1.9    bouyer 
    291       1.9    bouyer const struct pciide_product_desc pciide_via_products[] =  {
    292       1.9    bouyer     { PCI_PRODUCT_VIATECH_VT82C586_IDE,
    293       1.9    bouyer       0,
    294      1.18  drochner       PCIIDE_NUM_CHANNELS,
    295      1.11    bouyer       "VIA Technologies VT82C586 (Apollo VP) IDE Controller",
    296      1.11    bouyer       apollo_setup_cap,
    297      1.11    bouyer       apollo_setup_chip,
    298      1.18  drochner       apollo_channel_map
    299      1.11    bouyer      },
    300      1.11    bouyer     { PCI_PRODUCT_VIATECH_VT82C586A_IDE,
    301      1.11    bouyer       0,
    302      1.18  drochner       PCIIDE_NUM_CHANNELS,
    303      1.11    bouyer       "VIA Technologies VT82C586A IDE Controller",
    304       1.9    bouyer       apollo_setup_cap,
    305       1.9    bouyer       apollo_setup_chip,
    306      1.18  drochner       apollo_channel_map
    307      1.18  drochner     },
    308      1.18  drochner     { 0,
    309      1.18  drochner       0,
    310      1.18  drochner       0,
    311      1.18  drochner       NULL,
    312      1.18  drochner     }
    313      1.18  drochner };
    314      1.18  drochner 
    315      1.18  drochner const struct pciide_product_desc pciide_cypress_products[] =  {
    316      1.18  drochner     { PCI_PRODUCT_CONTAQ_82C693,
    317      1.18  drochner       0,
    318      1.18  drochner       1,
    319      1.18  drochner       "Contaq Microsystems CY82C693 IDE Controller",
    320      1.18  drochner       cy693_setup_cap,
    321      1.18  drochner       cy693_setup_chip,
    322      1.18  drochner       cy693_channel_map
    323      1.18  drochner     },
    324      1.18  drochner     { 0,
    325      1.18  drochner       0,
    326      1.18  drochner       0,
    327      1.18  drochner       NULL,
    328      1.18  drochner     }
    329      1.18  drochner };
    330      1.18  drochner 
    331      1.18  drochner const struct pciide_product_desc pciide_sis_products[] =  {
    332      1.18  drochner     { PCI_PRODUCT_SIS_5597_IDE,
    333      1.18  drochner       0,
    334      1.18  drochner       PCIIDE_NUM_CHANNELS,
    335      1.18  drochner       "Silicon Integrated System 5597/5598 IDE controller",
    336      1.18  drochner       sis_setup_cap,
    337      1.18  drochner       sis_setup_chip,
    338      1.18  drochner       sis_channel_map
    339      1.18  drochner     },
    340      1.18  drochner     { 0,
    341      1.18  drochner       0,
    342      1.18  drochner       0,
    343      1.18  drochner       NULL,
    344      1.18  drochner     }
    345       1.9    bouyer };
    346       1.9    bouyer 
    347      1.30    bouyer const struct pciide_product_desc pciide_acer_products[] =  {
    348      1.30    bouyer     { PCI_PRODUCT_ALI_M5229,
    349      1.30    bouyer       0,
    350      1.30    bouyer       PCIIDE_NUM_CHANNELS,
    351      1.30    bouyer       "Acer Labs M5229 UDMA IDE Controller",
    352      1.30    bouyer       acer_setup_cap,
    353      1.30    bouyer       acer_setup_chip,
    354      1.30    bouyer       acer_channel_map
    355      1.30    bouyer     },
    356      1.30    bouyer     { 0,
    357      1.30    bouyer       0,
    358      1.30    bouyer       0,
    359      1.30    bouyer       NULL,
    360      1.30    bouyer     }
    361      1.30    bouyer };
    362      1.30    bouyer 
    363       1.9    bouyer struct pciide_vendor_desc {
    364       1.9    bouyer     u_int32_t ide_vendor;
    365       1.9    bouyer     const struct pciide_product_desc *ide_products;
    366       1.9    bouyer };
    367       1.9    bouyer 
    368       1.9    bouyer const struct pciide_vendor_desc pciide_vendors[] = {
    369       1.9    bouyer     { PCI_VENDOR_INTEL, pciide_intel_products },
    370       1.9    bouyer     { PCI_VENDOR_CMDTECH, pciide_cmd_products },
    371       1.9    bouyer     { PCI_VENDOR_VIATECH, pciide_via_products },
    372      1.18  drochner     { PCI_VENDOR_CONTAQ, pciide_cypress_products },
    373      1.18  drochner     { PCI_VENDOR_SIS, pciide_sis_products },
    374      1.30    bouyer     { PCI_VENDOR_ALI, pciide_acer_products },
    375       1.9    bouyer     { 0, NULL }
    376       1.1       cgd };
    377       1.1       cgd 
    378       1.9    bouyer 
    379       1.1       cgd #define	PCIIDE_CHANNEL_NAME(chan)	((chan) == 0 ? "primary" : "secondary")
    380       1.1       cgd 
    381      1.13    bouyer /* options passed via the 'flags' config keyword */
    382      1.13    bouyer #define PCIIDE_OPTIONS_DMA	0x01
    383      1.13    bouyer 
    384       1.1       cgd int	pciide_match __P((struct device *, struct cfdata *, void *));
    385       1.1       cgd void	pciide_attach __P((struct device *, struct device *, void *));
    386       1.1       cgd 
    387       1.1       cgd struct cfattach pciide_ca = {
    388       1.1       cgd 	sizeof(struct pciide_softc), pciide_match, pciide_attach
    389       1.1       cgd };
    390       1.1       cgd 
    391      1.28    bouyer int	pciide_mapregs_compat __P(( struct pci_attach_args *,
    392      1.28    bouyer 	    struct pciide_channel *, int, bus_size_t *, bus_size_t*));
    393      1.28    bouyer int	pciide_mapregs_native __P((struct pci_attach_args *,
    394      1.28    bouyer 	    struct pciide_channel *, bus_size_t *, bus_size_t *));
    395      1.28    bouyer void	pciide_mapchan __P((struct pci_attach_args *,
    396      1.28    bouyer 	    struct pciide_channel *, int, bus_size_t *, bus_size_t *));
    397      1.28    bouyer int	pciiide_chan_candisable __P((struct pciide_channel *));
    398      1.28    bouyer void	pciide_map_compat_intr __P(( struct pci_attach_args *,
    399      1.28    bouyer 	    struct pciide_channel *, int, int));
    400       1.5       cgd int	pciide_print __P((void *, const char *pnp));
    401       1.1       cgd int	pciide_compat_intr __P((void *));
    402       1.1       cgd int	pciide_pci_intr __P((void *));
    403       1.9    bouyer const struct pciide_product_desc* pciide_lookup_product __P((u_int32_t));
    404       1.1       cgd 
    405       1.9    bouyer const struct pciide_product_desc*
    406       1.9    bouyer pciide_lookup_product(id)
    407       1.9    bouyer     u_int32_t id;
    408       1.9    bouyer {
    409       1.9    bouyer     const struct pciide_product_desc *pp;
    410       1.9    bouyer     const struct pciide_vendor_desc *vp;
    411       1.9    bouyer 
    412       1.9    bouyer     for (vp = pciide_vendors; vp->ide_products != NULL; vp++)
    413       1.9    bouyer 	if (PCI_VENDOR(id) == vp->ide_vendor)
    414       1.9    bouyer 	    break;
    415       1.9    bouyer 
    416       1.9    bouyer     if ((pp = vp->ide_products) == NULL)
    417       1.9    bouyer 	return NULL;
    418       1.9    bouyer 
    419       1.9    bouyer     for (; pp->ide_name != NULL; pp++)
    420       1.9    bouyer 	if (PCI_PRODUCT(id) == pp->ide_product)
    421       1.9    bouyer 	    break;
    422       1.9    bouyer 
    423       1.9    bouyer     if (pp->ide_name == NULL)
    424       1.9    bouyer 	return NULL;
    425       1.9    bouyer     return pp;
    426       1.9    bouyer }
    427       1.6       cgd 
    428       1.1       cgd int
    429       1.1       cgd pciide_match(parent, match, aux)
    430       1.1       cgd 	struct device *parent;
    431       1.1       cgd 	struct cfdata *match;
    432       1.1       cgd 	void *aux;
    433       1.1       cgd {
    434       1.1       cgd 	struct pci_attach_args *pa = aux;
    435       1.1       cgd 
    436       1.1       cgd 	/*
    437       1.1       cgd 	 * Check the ID register to see that it's a PCI IDE controller.
    438       1.1       cgd 	 * If it is, we assume that we can deal with it; it _should_
    439       1.1       cgd 	 * work in a standardized way...
    440       1.1       cgd 	 */
    441       1.1       cgd 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
    442       1.1       cgd 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
    443       1.1       cgd 		return (1);
    444       1.1       cgd 	}
    445       1.1       cgd 
    446       1.1       cgd 	return (0);
    447       1.1       cgd }
    448       1.1       cgd 
    449       1.1       cgd void
    450       1.1       cgd pciide_attach(parent, self, aux)
    451       1.1       cgd 	struct device *parent, *self;
    452       1.1       cgd 	void *aux;
    453       1.1       cgd {
    454       1.1       cgd 	struct pci_attach_args *pa = aux;
    455       1.1       cgd 	pci_chipset_tag_t pc = pa->pa_pc;
    456       1.9    bouyer 	pcitag_t tag = pa->pa_tag;
    457       1.1       cgd 	struct pciide_softc *sc = (struct pciide_softc *)self;
    458       1.1       cgd 	struct pciide_channel *cp;
    459       1.1       cgd 	pcireg_t class, interface, csr;
    460       1.1       cgd 	char devinfo[256];
    461       1.1       cgd 	int i;
    462       1.1       cgd 
    463       1.9    bouyer         sc->sc_pp = pciide_lookup_product(pa->pa_id);
    464       1.9    bouyer 	if (sc->sc_pp == NULL) {
    465       1.9    bouyer 		sc->sc_pp = &default_product_desc;
    466       1.9    bouyer 		pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    467       1.9    bouyer 		printf(": %s (rev. 0x%02x)\n", devinfo,
    468       1.9    bouyer 		    PCI_REVISION(pa->pa_class));
    469       1.9    bouyer 	} else {
    470       1.9    bouyer 		printf(": %s\n", sc->sc_pp->ide_name);
    471       1.9    bouyer 	}
    472       1.1       cgd 
    473       1.1       cgd 	if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0) {
    474       1.9    bouyer 		csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    475       1.9    bouyer 		/*
    476       1.9    bouyer 		 * For a CMD PCI064x, the use of PCI_COMMAND_IO_ENABLE
    477       1.9    bouyer 		 * and base adresses registers can be disabled at
    478       1.9    bouyer 		 * hardware level. In this case, the device is wired
    479       1.9    bouyer 		 * in compat mode and its first channel is always enabled,
    480       1.9    bouyer 		 * but we can't rely on PCI_COMMAND_IO_ENABLE.
    481       1.9    bouyer 		 * In fact, it seems that the first channel of the CMD PCI0640
    482       1.9    bouyer 		 * can't be disabled.
    483       1.9    bouyer 		 */
    484      1.11    bouyer #ifndef PCIIDE_CMD064x_DISABLE
    485       1.9    bouyer 		if ((sc->sc_pp->ide_flags & CMD_PCI064x_IOEN) == 0) {
    486      1.11    bouyer #else
    487      1.11    bouyer 		if (1) {
    488      1.11    bouyer #endif
    489       1.9    bouyer 			printf("%s: device disabled (at %s)\n",
    490       1.9    bouyer 		 	   sc->sc_wdcdev.sc_dev.dv_xname,
    491       1.9    bouyer 		  	  (csr & PCI_COMMAND_IO_ENABLE) == 0 ?
    492       1.9    bouyer 			  "device" : "bridge");
    493       1.9    bouyer 			return;
    494       1.9    bouyer 		}
    495       1.1       cgd 	}
    496       1.1       cgd 
    497      1.28    bouyer 	sc->sc_pc = pa->pa_pc;
    498      1.28    bouyer 	sc->sc_tag = pa->pa_tag;
    499      1.28    bouyer 
    500       1.9    bouyer 	class = pci_conf_read(pc, tag, PCI_CLASS_REG);
    501       1.1       cgd 	interface = PCI_INTERFACE(class);
    502       1.1       cgd 
    503       1.1       cgd 	/*
    504       1.2       cgd 	 * Map DMA registers, if DMA is supported.
    505       1.2       cgd 	 *
    506       1.5       cgd 	 * Note that sc_dma_ok is the right variable to test to see if
    507       1.9    bouyer 	 * DMA can be done.  If the interface doesn't support DMA,
    508       1.9    bouyer 	 * sc_dma_ok will never be non-zero.  If the DMA regs couldn't
    509       1.5       cgd 	 * be mapped, it'll be zero.  I.e., sc_dma_ok will only be
    510       1.5       cgd 	 * non-zero if the interface supports DMA and the registers
    511       1.5       cgd 	 * could be mapped.
    512       1.4       cgd 	 *
    513       1.4       cgd 	 * XXX Note that despite the fact that the Bus Master IDE specs
    514       1.4       cgd 	 * XXX say that "The bus master IDE functoin uses 16 bytes of IO
    515       1.4       cgd 	 * XXX space," some controllers (at least the United
    516       1.4       cgd 	 * XXX Microelectronics UM8886BF) place it in memory space.
    517       1.4       cgd 	 * XXX eventually, we should probably read the register and check
    518       1.4       cgd 	 * XXX which type it is.  Either that or 'quirk' certain devices.
    519       1.2       cgd 	 */
    520       1.2       cgd 	if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) {
    521       1.9    bouyer 		printf("%s: bus-master DMA support present",
    522       1.9    bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname);
    523      1.13    bouyer 		if (sc->sc_pp == &default_product_desc &&
    524      1.13    bouyer 		    (sc->sc_wdcdev.sc_dev.dv_cfdata->cf_flags &
    525      1.13    bouyer 		    PCIIDE_OPTIONS_DMA) == 0) {
    526      1.11    bouyer 			printf(", but unused (no driver support)");
    527      1.11    bouyer 			sc->sc_dma_ok = 0;
    528       1.9    bouyer 		} else {
    529      1.11    bouyer 			sc->sc_dma_ok = (pci_mapreg_map(pa,
    530      1.11    bouyer 			    PCIIDE_REG_BUS_MASTER_DMA, PCI_MAPREG_TYPE_IO, 0,
    531      1.11    bouyer 			    &sc->sc_dma_iot, &sc->sc_dma_ioh, NULL, NULL) == 0);
    532      1.11    bouyer 			sc->sc_dmat = pa->pa_dmat;
    533      1.11    bouyer 			if (sc->sc_dma_ok == 0) {
    534      1.11    bouyer 				printf(", but unused (couldn't map registers)");
    535      1.11    bouyer 			} else {
    536      1.13    bouyer 				if (sc->sc_pp == &default_product_desc)
    537      1.13    bouyer 					printf(", used without full driver "
    538      1.13    bouyer 					    "support");
    539      1.11    bouyer 				sc->sc_wdcdev.dma_arg = sc;
    540      1.11    bouyer 				sc->sc_wdcdev.dma_init = pciide_dma_init;
    541      1.11    bouyer 				sc->sc_wdcdev.dma_start = pciide_dma_start;
    542      1.11    bouyer 				sc->sc_wdcdev.dma_finish = pciide_dma_finish;
    543      1.11    bouyer 			}
    544       1.9    bouyer 		}
    545      1.15    bouyer 	} else {
    546  1.33.2.1    bouyer 		printf("%s: hardware does not support DMA",
    547      1.15    bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname);
    548       1.1       cgd 	}
    549      1.15    bouyer 	printf("\n");
    550       1.9    bouyer 	sc->sc_pp->setup_cap(sc);
    551      1.18  drochner 	sc->sc_wdcdev.channels = sc->wdc_chanarray;
    552      1.18  drochner 	sc->sc_wdcdev.nchannels = sc->sc_pp->ide_num_channels;;
    553       1.9    bouyer 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
    554       1.1       cgd 
    555      1.18  drochner 	for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
    556       1.9    bouyer 		cp = &sc->pciide_channels[i];
    557      1.18  drochner 		sc->wdc_chanarray[i] = &cp->wdc_channel;
    558       1.2       cgd 
    559      1.18  drochner 		cp->name = PCIIDE_CHANNEL_NAME(i);
    560      1.18  drochner 
    561      1.18  drochner 		cp->wdc_channel.channel = i;
    562      1.18  drochner 		cp->wdc_channel.wdc = &sc->sc_wdcdev;
    563       1.9    bouyer 		if (i > 0 && (sc->sc_pp->ide_flags & ONE_QUEUE)) {
    564      1.18  drochner 		    cp->wdc_channel.ch_queue =
    565      1.18  drochner 			sc->pciide_channels[0].wdc_channel.ch_queue;
    566       1.9    bouyer 		} else {
    567      1.18  drochner 		    cp->wdc_channel.ch_queue =
    568       1.9    bouyer 		        malloc(sizeof(struct channel_queue), M_DEVBUF,
    569       1.9    bouyer 			M_NOWAIT);
    570       1.9    bouyer 		}
    571      1.18  drochner 		if (cp->wdc_channel.ch_queue == NULL) {
    572       1.9    bouyer 		    printf("%s %s channel: "
    573       1.9    bouyer 			"can't allocate memory for command queue",
    574      1.18  drochner 			sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
    575       1.9    bouyer 			continue;
    576       1.9    bouyer 		}
    577       1.2       cgd 		printf("%s: %s channel %s to %s mode\n",
    578      1.18  drochner 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name,
    579       1.2       cgd 		    (interface & PCIIDE_INTERFACE_SETTABLE(i)) ?
    580       1.2       cgd 		      "configured" : "wired",
    581       1.2       cgd 		    (interface & PCIIDE_INTERFACE_PCI(i)) ? "native-PCI" :
    582       1.2       cgd 		      "compatibility");
    583       1.1       cgd 
    584       1.9    bouyer 		/*
    585      1.18  drochner 		 * sc->sc_pp->channel_map() will also call wdcattach.
    586      1.18  drochner 		 * Eventually the channel will be  disabled if there's no
    587      1.18  drochner 		 * drive present. sc->hw_ok will be updated accordingly.
    588       1.9    bouyer 		 */
    589      1.28    bouyer 		sc->sc_pp->channel_map(pa, cp);
    590       1.2       cgd 
    591       1.5       cgd 	}
    592      1.18  drochner 	/* Now that all drives are know, setup DMA, etc ...*/
    593      1.28    bouyer 	sc->sc_pp->setup_chip(sc);
    594      1.16    bouyer 	if (sc->sc_dma_ok) {
    595      1.16    bouyer 		csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    596      1.16    bouyer 		csr |= PCI_COMMAND_MASTER_ENABLE;
    597      1.16    bouyer 		pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
    598      1.16    bouyer 	}
    599       1.9    bouyer 	WDCDEBUG_PRINT(("pciide: command/status register=%x\n",
    600       1.9    bouyer 	    pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG)), DEBUG_PROBE);
    601       1.5       cgd }
    602       1.5       cgd 
    603       1.5       cgd int
    604      1.28    bouyer pciide_mapregs_compat(pa, cp, compatchan, cmdsizep, ctlsizep)
    605       1.5       cgd 	struct pci_attach_args *pa;
    606      1.18  drochner 	struct pciide_channel *cp;
    607      1.18  drochner 	int compatchan;
    608      1.18  drochner 	bus_size_t *cmdsizep, *ctlsizep;
    609       1.5       cgd {
    610      1.28    bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
    611      1.18  drochner 	struct channel_softc *wdc_cp = &cp->wdc_channel;
    612       1.5       cgd 	int rv = 1;
    613       1.5       cgd 
    614       1.5       cgd 	cp->compat = 1;
    615      1.18  drochner 	*cmdsizep = PCIIDE_COMPAT_CMD_SIZE;
    616      1.18  drochner 	*ctlsizep = PCIIDE_COMPAT_CTL_SIZE;
    617       1.5       cgd 
    618       1.9    bouyer 	wdc_cp->cmd_iot = pa->pa_iot;
    619      1.18  drochner 	if (bus_space_map(wdc_cp->cmd_iot, PCIIDE_COMPAT_CMD_BASE(compatchan),
    620       1.9    bouyer 	    PCIIDE_COMPAT_CMD_SIZE, 0, &wdc_cp->cmd_ioh) != 0) {
    621       1.5       cgd 		printf("%s: couldn't map %s channel cmd regs\n",
    622      1.18  drochner 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
    623       1.5       cgd 		rv = 0;
    624       1.5       cgd 	}
    625       1.5       cgd 
    626       1.9    bouyer 	wdc_cp->ctl_iot = pa->pa_iot;
    627      1.18  drochner 	if (bus_space_map(wdc_cp->ctl_iot, PCIIDE_COMPAT_CTL_BASE(compatchan),
    628       1.9    bouyer 	    PCIIDE_COMPAT_CTL_SIZE, 0, &wdc_cp->ctl_ioh) != 0) {
    629       1.5       cgd 		printf("%s: couldn't map %s channel ctl regs\n",
    630      1.18  drochner 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
    631       1.9    bouyer 		bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh,
    632       1.5       cgd 		    PCIIDE_COMPAT_CMD_SIZE);
    633       1.5       cgd 		rv = 0;
    634       1.5       cgd 	}
    635       1.5       cgd 
    636       1.5       cgd 	return (rv);
    637       1.5       cgd }
    638       1.5       cgd 
    639       1.9    bouyer int
    640      1.28    bouyer pciide_mapregs_native(pa, cp, cmdsizep, ctlsizep)
    641      1.28    bouyer 	struct pci_attach_args * pa;
    642      1.18  drochner 	struct pciide_channel *cp;
    643      1.18  drochner 	bus_size_t *cmdsizep, *ctlsizep;
    644       1.9    bouyer {
    645      1.28    bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
    646      1.18  drochner 	struct channel_softc *wdc_cp = &cp->wdc_channel;
    647      1.29    bouyer 	const char *intrstr;
    648      1.29    bouyer 	pci_intr_handle_t intrhandle;
    649       1.9    bouyer 
    650       1.9    bouyer 	cp->compat = 0;
    651       1.9    bouyer 
    652      1.29    bouyer 	if (sc->sc_pci_ih == NULL) {
    653      1.29    bouyer 		if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
    654      1.29    bouyer 		    pa->pa_intrline, &intrhandle) != 0) {
    655      1.29    bouyer 			printf("%s: couldn't map native-PCI interrupt\n",
    656      1.29    bouyer 			    sc->sc_wdcdev.sc_dev.dv_xname);
    657      1.29    bouyer 			return 0;
    658      1.29    bouyer 		}
    659      1.29    bouyer 		intrstr = pci_intr_string(pa->pa_pc, intrhandle);
    660      1.29    bouyer 		sc->sc_pci_ih = pci_intr_establish(pa->pa_pc,
    661      1.29    bouyer 		    intrhandle, IPL_BIO, pciide_pci_intr, sc);
    662      1.29    bouyer 		if (sc->sc_pci_ih != NULL) {
    663      1.29    bouyer 			printf("%s: using %s for native-PCI interrupt\n",
    664      1.29    bouyer 			    sc->sc_wdcdev.sc_dev.dv_xname,
    665      1.29    bouyer 			    intrstr ? intrstr : "unknown interrupt");
    666      1.29    bouyer 		} else {
    667      1.29    bouyer 			printf("%s: couldn't establish native-PCI interrupt",
    668      1.29    bouyer 			    sc->sc_wdcdev.sc_dev.dv_xname);
    669      1.29    bouyer 			if (intrstr != NULL)
    670      1.29    bouyer 				printf(" at %s", intrstr);
    671      1.29    bouyer 			printf("\n");
    672      1.29    bouyer 			return 0;
    673      1.29    bouyer 		}
    674      1.18  drochner 	}
    675      1.29    bouyer 	cp->ih = sc->sc_pci_ih;
    676      1.18  drochner 	if (pci_mapreg_map(pa, PCIIDE_REG_CMD_BASE(wdc_cp->channel),
    677      1.18  drochner 	    PCI_MAPREG_TYPE_IO, 0,
    678      1.18  drochner 	    &wdc_cp->cmd_iot, &wdc_cp->cmd_ioh, NULL, cmdsizep) != 0) {
    679       1.9    bouyer 		printf("%s: couldn't map %s channel cmd regs\n",
    680      1.18  drochner 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
    681      1.18  drochner 		return 0;
    682       1.9    bouyer 	}
    683       1.9    bouyer 
    684      1.18  drochner 	if (pci_mapreg_map(pa, PCIIDE_REG_CTL_BASE(wdc_cp->channel),
    685      1.18  drochner 	    PCI_MAPREG_TYPE_IO, 0,
    686      1.18  drochner 	    &wdc_cp->ctl_iot, &wdc_cp->ctl_ioh, NULL, ctlsizep) != 0) {
    687       1.9    bouyer 		printf("%s: couldn't map %s channel ctl regs\n",
    688      1.18  drochner 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
    689      1.18  drochner 		bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh, *cmdsizep);
    690      1.18  drochner 		return 0;
    691       1.9    bouyer 	}
    692      1.18  drochner 	return (1);
    693       1.9    bouyer }
    694       1.9    bouyer 
    695       1.9    bouyer int
    696       1.9    bouyer pciide_compat_intr(arg)
    697       1.9    bouyer 	void *arg;
    698       1.9    bouyer {
    699      1.19  drochner 	struct pciide_channel *cp = arg;
    700       1.9    bouyer 
    701       1.9    bouyer #ifdef DIAGNOSTIC
    702       1.9    bouyer 	/* should only be called for a compat channel */
    703       1.9    bouyer 	if (cp->compat == 0)
    704       1.9    bouyer 		panic("pciide compat intr called for non-compat chan %p\n", cp);
    705       1.9    bouyer #endif
    706      1.19  drochner 	return (wdcintr(&cp->wdc_channel));
    707       1.9    bouyer }
    708       1.9    bouyer 
    709       1.9    bouyer int
    710       1.9    bouyer pciide_pci_intr(arg)
    711       1.9    bouyer 	void *arg;
    712       1.9    bouyer {
    713       1.9    bouyer 	struct pciide_softc *sc = arg;
    714       1.9    bouyer 	struct pciide_channel *cp;
    715       1.9    bouyer 	struct channel_softc *wdc_cp;
    716       1.9    bouyer 	int i, rv, crv;
    717       1.9    bouyer 
    718       1.9    bouyer 	rv = 0;
    719      1.18  drochner 	for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
    720       1.9    bouyer 		cp = &sc->pciide_channels[i];
    721      1.18  drochner 		wdc_cp = &cp->wdc_channel;
    722       1.9    bouyer 
    723       1.9    bouyer 		/* If a compat channel skip. */
    724       1.9    bouyer 		if (cp->compat)
    725       1.9    bouyer 			continue;
    726       1.9    bouyer 		/* if this channel not waiting for intr, skip */
    727       1.9    bouyer 		if ((wdc_cp->ch_flags & WDCF_IRQ_WAIT) == 0)
    728       1.9    bouyer 			continue;
    729       1.9    bouyer 
    730       1.9    bouyer 		crv = wdcintr(wdc_cp);
    731       1.9    bouyer 		if (crv == 0)
    732       1.9    bouyer 			;		/* leave rv alone */
    733       1.9    bouyer 		else if (crv == 1)
    734       1.9    bouyer 			rv = 1;		/* claim the intr */
    735       1.9    bouyer 		else if (rv == 0)	/* crv should be -1 in this case */
    736       1.9    bouyer 			rv = crv;	/* if we've done no better, take it */
    737       1.9    bouyer 	}
    738       1.9    bouyer 	return (rv);
    739       1.9    bouyer }
    740       1.9    bouyer 
    741      1.28    bouyer void
    742      1.28    bouyer pciide_channel_dma_setup(cp)
    743      1.28    bouyer 	struct pciide_channel *cp;
    744      1.28    bouyer {
    745      1.28    bouyer 	int drive;
    746      1.28    bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
    747      1.28    bouyer 	struct ata_drive_datas *drvp;
    748      1.28    bouyer 
    749      1.28    bouyer 	for (drive = 0; drive < 2; drive++) {
    750      1.28    bouyer 		drvp = &cp->wdc_channel.ch_drive[drive];
    751      1.28    bouyer 		/* If no drive, skip */
    752      1.28    bouyer 		if ((drvp->drive_flags & DRIVE) == 0)
    753      1.28    bouyer 			continue;
    754      1.28    bouyer 		/* setup DMA if needed */
    755      1.28    bouyer 		if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
    756      1.28    bouyer 		    (drvp->drive_flags & DRIVE_UDMA) == 0) ||
    757      1.28    bouyer 		    sc->sc_dma_ok == 0) {
    758      1.28    bouyer 			drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
    759      1.28    bouyer 			continue;
    760      1.28    bouyer 		}
    761      1.28    bouyer 		if (pciide_dma_table_setup(sc, cp->wdc_channel.channel, drive)
    762      1.28    bouyer 		    != 0) {
    763      1.28    bouyer 			/* Abort DMA setup */
    764      1.28    bouyer 			drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
    765      1.28    bouyer 			continue;
    766      1.28    bouyer 		}
    767      1.28    bouyer 	}
    768      1.28    bouyer }
    769      1.28    bouyer 
    770      1.18  drochner int
    771      1.18  drochner pciide_dma_table_setup(sc, channel, drive)
    772       1.9    bouyer 	struct pciide_softc *sc;
    773      1.18  drochner 	int channel, drive;
    774       1.9    bouyer {
    775      1.18  drochner 	bus_dma_segment_t seg;
    776      1.18  drochner 	int error, rseg;
    777      1.18  drochner 	const bus_size_t dma_table_size =
    778      1.18  drochner 	    sizeof(struct idedma_table) * NIDEDMA_TABLES;
    779      1.18  drochner 	struct pciide_dma_maps *dma_maps =
    780      1.18  drochner 	    &sc->pciide_channels[channel].dma_maps[drive];
    781      1.18  drochner 
    782      1.28    bouyer 	/* If table was already allocated, just return */
    783      1.28    bouyer 	if (dma_maps->dma_table)
    784      1.28    bouyer 		return 0;
    785      1.28    bouyer 
    786      1.18  drochner 	/* Allocate memory for the DMA tables and map it */
    787      1.18  drochner 	if ((error = bus_dmamem_alloc(sc->sc_dmat, dma_table_size,
    788      1.18  drochner 	    IDEDMA_TBL_ALIGN, IDEDMA_TBL_ALIGN, &seg, 1, &rseg,
    789      1.18  drochner 	    BUS_DMA_NOWAIT)) != 0) {
    790      1.18  drochner 		printf("%s:%d: unable to allocate table DMA for "
    791      1.18  drochner 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
    792      1.18  drochner 		    channel, drive, error);
    793      1.18  drochner 		return error;
    794      1.18  drochner 	}
    795      1.18  drochner 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    796      1.18  drochner 	    dma_table_size,
    797      1.18  drochner 	    (caddr_t *)&dma_maps->dma_table,
    798      1.18  drochner 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    799      1.18  drochner 		printf("%s:%d: unable to map table DMA for"
    800      1.18  drochner 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
    801      1.18  drochner 		    channel, drive, error);
    802      1.18  drochner 		return error;
    803      1.18  drochner 	}
    804      1.18  drochner 	WDCDEBUG_PRINT(("pciide_dma_table_setup: table at %p len %ld, "
    805      1.18  drochner 	    "phy 0x%lx\n", dma_maps->dma_table, dma_table_size,
    806      1.18  drochner 	    seg.ds_addr), DEBUG_PROBE);
    807      1.18  drochner 
    808      1.18  drochner 	/* Create and load table DMA map for this disk */
    809      1.18  drochner 	if ((error = bus_dmamap_create(sc->sc_dmat, dma_table_size,
    810      1.18  drochner 	    1, dma_table_size, IDEDMA_TBL_ALIGN, BUS_DMA_NOWAIT,
    811      1.18  drochner 	    &dma_maps->dmamap_table)) != 0) {
    812      1.18  drochner 		printf("%s:%d: unable to create table DMA map for "
    813      1.18  drochner 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
    814      1.18  drochner 		    channel, drive, error);
    815      1.18  drochner 		return error;
    816      1.18  drochner 	}
    817      1.18  drochner 	if ((error = bus_dmamap_load(sc->sc_dmat,
    818      1.18  drochner 	    dma_maps->dmamap_table,
    819      1.18  drochner 	    dma_maps->dma_table,
    820      1.18  drochner 	    dma_table_size, NULL, BUS_DMA_NOWAIT)) != 0) {
    821      1.18  drochner 		printf("%s:%d: unable to load table DMA map for "
    822      1.18  drochner 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
    823      1.18  drochner 		    channel, drive, error);
    824      1.18  drochner 		return error;
    825      1.18  drochner 	}
    826      1.18  drochner 	WDCDEBUG_PRINT(("pciide_dma_table_setup: phy addr of table 0x%lx\n",
    827      1.18  drochner 	    dma_maps->dmamap_table->dm_segs[0].ds_addr), DEBUG_PROBE);
    828      1.18  drochner 	/* Create a xfer DMA map for this drive */
    829      1.18  drochner 	if ((error = bus_dmamap_create(sc->sc_dmat, IDEDMA_BYTE_COUNT_MAX,
    830      1.18  drochner 	    NIDEDMA_TABLES, IDEDMA_BYTE_COUNT_MAX, IDEDMA_BYTE_COUNT_ALIGN,
    831      1.18  drochner 	    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    832      1.18  drochner 	    &dma_maps->dmamap_xfer)) != 0) {
    833      1.18  drochner 		printf("%s:%d: unable to create xfer DMA map for "
    834      1.18  drochner 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
    835      1.18  drochner 		    channel, drive, error);
    836      1.18  drochner 		return error;
    837      1.18  drochner 	}
    838      1.18  drochner 	return 0;
    839       1.9    bouyer }
    840       1.9    bouyer 
    841      1.18  drochner int
    842      1.18  drochner pciide_dma_init(v, channel, drive, databuf, datalen, flags)
    843      1.18  drochner 	void *v;
    844      1.18  drochner 	int channel, drive;
    845      1.18  drochner 	void *databuf;
    846      1.18  drochner 	size_t datalen;
    847      1.18  drochner 	int flags;
    848       1.9    bouyer {
    849      1.18  drochner 	struct pciide_softc *sc = v;
    850      1.18  drochner 	int error, seg;
    851      1.18  drochner 	struct pciide_dma_maps *dma_maps =
    852      1.18  drochner 	    &sc->pciide_channels[channel].dma_maps[drive];
    853      1.18  drochner 
    854      1.18  drochner 	error = bus_dmamap_load(sc->sc_dmat,
    855      1.18  drochner 	    dma_maps->dmamap_xfer,
    856      1.18  drochner 	    databuf, datalen, NULL, BUS_DMA_NOWAIT);
    857      1.18  drochner 	if (error) {
    858      1.18  drochner 		printf("%s:%d: unable to load xfer DMA map for"
    859      1.18  drochner 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
    860      1.18  drochner 		    channel, drive, error);
    861      1.18  drochner 		return error;
    862      1.18  drochner 	}
    863       1.9    bouyer 
    864      1.18  drochner 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
    865      1.18  drochner 	    dma_maps->dmamap_xfer->dm_mapsize,
    866      1.18  drochner 	    (flags & WDC_DMA_READ) ?
    867      1.18  drochner 	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    868       1.9    bouyer 
    869      1.18  drochner 	for (seg = 0; seg < dma_maps->dmamap_xfer->dm_nsegs; seg++) {
    870      1.18  drochner #ifdef DIAGNOSTIC
    871      1.18  drochner 		/* A segment must not cross a 64k boundary */
    872      1.18  drochner 		{
    873      1.18  drochner 		u_long phys = dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
    874      1.18  drochner 		u_long len = dma_maps->dmamap_xfer->dm_segs[seg].ds_len;
    875      1.18  drochner 		if ((phys & ~IDEDMA_BYTE_COUNT_MASK) !=
    876      1.18  drochner 		    ((phys + len - 1) & ~IDEDMA_BYTE_COUNT_MASK)) {
    877      1.18  drochner 			printf("pciide_dma: segment %d physical addr 0x%lx"
    878      1.18  drochner 			    " len 0x%lx not properly aligned\n",
    879      1.18  drochner 			    seg, phys, len);
    880      1.18  drochner 			panic("pciide_dma: buf align");
    881       1.9    bouyer 		}
    882       1.9    bouyer 		}
    883      1.18  drochner #endif
    884      1.18  drochner 		dma_maps->dma_table[seg].base_addr =
    885      1.18  drochner 		    dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
    886      1.18  drochner 		dma_maps->dma_table[seg].byte_count =
    887      1.18  drochner 		    dma_maps->dmamap_xfer->dm_segs[seg].ds_len &
    888      1.18  drochner 		    IDEDMA_BYTE_COUNT_MASK;
    889      1.18  drochner 		WDCDEBUG_PRINT(("\t seg %d len %d addr 0x%x\n",
    890      1.18  drochner 		   seg, dma_maps->dma_table[seg].byte_count,
    891      1.18  drochner 		   dma_maps->dma_table[seg].base_addr), DEBUG_DMA);
    892      1.18  drochner 
    893       1.9    bouyer 	}
    894      1.18  drochner 	dma_maps->dma_table[dma_maps->dmamap_xfer->dm_nsegs -1].byte_count |=
    895      1.18  drochner 		IDEDMA_BYTE_COUNT_EOT;
    896       1.9    bouyer 
    897      1.18  drochner 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_table, 0,
    898      1.18  drochner 	    dma_maps->dmamap_table->dm_mapsize,
    899      1.18  drochner 	    BUS_DMASYNC_PREWRITE);
    900       1.9    bouyer 
    901      1.18  drochner 	/* Maps are ready. Start DMA function */
    902      1.18  drochner #ifdef DIAGNOSTIC
    903      1.18  drochner 	if (dma_maps->dmamap_table->dm_segs[0].ds_addr & ~IDEDMA_TBL_MASK) {
    904      1.18  drochner 		printf("pciide_dma_init: addr 0x%lx not properly aligned\n",
    905      1.18  drochner 		    dma_maps->dmamap_table->dm_segs[0].ds_addr);
    906      1.18  drochner 		panic("pciide_dma_init: table align");
    907      1.18  drochner 	}
    908      1.18  drochner #endif
    909      1.18  drochner 
    910      1.18  drochner 	/* Clear status bits */
    911      1.18  drochner 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    912      1.18  drochner 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel,
    913      1.18  drochner 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    914      1.18  drochner 		IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel));
    915      1.18  drochner 	/* Write table addr */
    916      1.18  drochner 	bus_space_write_4(sc->sc_dma_iot, sc->sc_dma_ioh,
    917      1.18  drochner 	    IDEDMA_TBL + IDEDMA_SCH_OFFSET * channel,
    918      1.18  drochner 	    dma_maps->dmamap_table->dm_segs[0].ds_addr);
    919      1.18  drochner 	/* set read/write */
    920      1.18  drochner 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    921      1.18  drochner 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
    922      1.18  drochner 	    (flags & WDC_DMA_READ) ? IDEDMA_CMD_WRITE: 0);
    923      1.18  drochner 	return 0;
    924      1.18  drochner }
    925      1.18  drochner 
    926      1.18  drochner void
    927      1.18  drochner pciide_dma_start(v, channel, drive, flags)
    928      1.18  drochner 	void *v;
    929      1.18  drochner 	int channel, drive, flags;
    930      1.18  drochner {
    931      1.18  drochner 	struct pciide_softc *sc = v;
    932      1.18  drochner 
    933      1.18  drochner 	WDCDEBUG_PRINT(("pciide_dma_start\n"),DEBUG_XFERS);
    934      1.18  drochner 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    935      1.18  drochner 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
    936      1.18  drochner 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    937      1.18  drochner 		IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) | IDEDMA_CMD_START);
    938      1.18  drochner }
    939      1.18  drochner 
    940      1.18  drochner int
    941      1.18  drochner pciide_dma_finish(v, channel, drive, flags)
    942      1.18  drochner 	void *v;
    943      1.18  drochner 	int channel, drive;
    944      1.18  drochner 	int flags;
    945      1.18  drochner {
    946      1.18  drochner 	struct pciide_softc *sc = v;
    947      1.18  drochner 	u_int8_t status;
    948      1.18  drochner 	struct pciide_dma_maps *dma_maps =
    949      1.18  drochner 	    &sc->pciide_channels[channel].dma_maps[drive];
    950      1.18  drochner 
    951      1.18  drochner 	/* Unload the map of the data buffer */
    952      1.18  drochner 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
    953      1.18  drochner 	    dma_maps->dmamap_xfer->dm_mapsize,
    954      1.18  drochner 	    (flags & WDC_DMA_READ) ?
    955      1.18  drochner 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    956      1.18  drochner 	bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_xfer);
    957      1.18  drochner 
    958      1.18  drochner 	status = bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    959      1.18  drochner 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel);
    960      1.18  drochner 	WDCDEBUG_PRINT(("pciide_dma_finish: status 0x%x\n", status),
    961      1.18  drochner 	    DEBUG_XFERS);
    962      1.18  drochner 
    963      1.18  drochner 	/* stop DMA channel */
    964      1.18  drochner 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    965      1.18  drochner 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
    966      1.18  drochner 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    967      1.18  drochner 		IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) & ~IDEDMA_CMD_START);
    968      1.18  drochner 
    969      1.18  drochner 	/* Clear status bits */
    970      1.18  drochner 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    971      1.18  drochner 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel,
    972      1.18  drochner 	    status);
    973      1.18  drochner 
    974      1.18  drochner 	if ((status & IDEDMA_CTL_ERR) != 0) {
    975      1.18  drochner 		printf("%s:%d:%d: Bus-Master DMA error: status=0x%x\n",
    976      1.18  drochner 		    sc->sc_wdcdev.sc_dev.dv_xname, channel, drive, status);
    977      1.18  drochner 		return -1;
    978      1.18  drochner 	}
    979      1.18  drochner 
    980      1.18  drochner 	if ((flags & WDC_DMA_POLL) == 0 && (status & IDEDMA_CTL_INTR) == 0) {
    981      1.18  drochner 		printf("%s:%d:%d: Bus-Master DMA error: missing interrupt, "
    982      1.18  drochner 		    "status=0x%x\n", sc->sc_wdcdev.sc_dev.dv_xname, channel,
    983      1.18  drochner 		    drive, status);
    984      1.18  drochner 		return -1;
    985      1.18  drochner 	}
    986      1.18  drochner 
    987      1.18  drochner 	if ((status & IDEDMA_CTL_ACT) != 0) {
    988      1.18  drochner 		/* data underrun, may be a valid condition for ATAPI */
    989      1.18  drochner 		return 1;
    990      1.18  drochner 	}
    991      1.18  drochner 	return 0;
    992      1.18  drochner }
    993      1.18  drochner 
    994      1.18  drochner /* some common code used by several chip channel_map */
    995      1.18  drochner void
    996      1.28    bouyer pciide_mapchan(pa, cp, interface, cmdsizep, ctlsizep)
    997      1.18  drochner 	struct pci_attach_args *pa;
    998      1.18  drochner 	int interface;
    999      1.18  drochner 	struct pciide_channel *cp;
   1000      1.18  drochner 	bus_size_t *cmdsizep, *ctlsizep;
   1001      1.18  drochner {
   1002      1.18  drochner 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   1003      1.18  drochner 
   1004      1.18  drochner 	if (interface & PCIIDE_INTERFACE_PCI(wdc_cp->channel))
   1005      1.28    bouyer 		cp->hw_ok = pciide_mapregs_native(pa, cp, cmdsizep, ctlsizep);
   1006      1.18  drochner 	else
   1007      1.28    bouyer 		cp->hw_ok = pciide_mapregs_compat(pa, cp,
   1008      1.28    bouyer 		    wdc_cp->channel, cmdsizep, ctlsizep);
   1009      1.18  drochner 	if (cp->hw_ok == 0)
   1010      1.18  drochner 		return;
   1011      1.18  drochner 	wdc_cp->data32iot = wdc_cp->cmd_iot;
   1012      1.18  drochner 	wdc_cp->data32ioh = wdc_cp->cmd_ioh;
   1013      1.18  drochner 	wdcattach(wdc_cp);
   1014      1.18  drochner }
   1015      1.18  drochner 
   1016      1.18  drochner /*
   1017      1.18  drochner  * Generic code to call to know if a channel can be disabled. Return 1
   1018      1.18  drochner  * if channel can be disabled, 0 if not
   1019      1.18  drochner  */
   1020      1.18  drochner int
   1021      1.28    bouyer pciiide_chan_candisable(cp)
   1022      1.18  drochner 	struct pciide_channel *cp;
   1023      1.18  drochner {
   1024      1.28    bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1025      1.18  drochner 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   1026      1.18  drochner 
   1027      1.18  drochner 	if ((wdc_cp->ch_drive[0].drive_flags & DRIVE) == 0 &&
   1028      1.18  drochner 	    (wdc_cp->ch_drive[1].drive_flags & DRIVE) == 0) {
   1029      1.18  drochner 		printf("%s: disabling %s channel (no drives)\n",
   1030      1.18  drochner 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   1031      1.18  drochner 		cp->hw_ok = 0;
   1032      1.18  drochner 		return 1;
   1033      1.18  drochner 	}
   1034      1.18  drochner 	return 0;
   1035      1.18  drochner }
   1036      1.18  drochner 
   1037      1.18  drochner /*
   1038      1.18  drochner  * generic code to map the compat intr if hw_ok=1 and it is a compat channel.
   1039      1.18  drochner  * Set hw_ok=0 on failure
   1040      1.18  drochner  */
   1041      1.18  drochner void
   1042      1.28    bouyer pciide_map_compat_intr(pa, cp, compatchan, interface)
   1043       1.5       cgd 	struct pci_attach_args *pa;
   1044      1.18  drochner 	struct pciide_channel *cp;
   1045      1.18  drochner 	int compatchan, interface;
   1046      1.18  drochner {
   1047      1.28    bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1048      1.18  drochner 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   1049      1.18  drochner 
   1050      1.18  drochner 	if (cp->hw_ok == 0)
   1051      1.18  drochner 		return;
   1052      1.18  drochner 	if ((interface & PCIIDE_INTERFACE_PCI(wdc_cp->channel)) != 0)
   1053      1.18  drochner 		return;
   1054      1.18  drochner 
   1055      1.18  drochner 	cp->ih = pciide_machdep_compat_intr_establish(&sc->sc_wdcdev.sc_dev,
   1056      1.19  drochner 	    pa, compatchan, pciide_compat_intr, cp);
   1057      1.18  drochner 	if (cp->ih == NULL) {
   1058      1.18  drochner 		printf("%s: no compatibility interrupt for use by %s "
   1059      1.18  drochner 		    "channel\n", sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   1060      1.18  drochner 		cp->hw_ok = 0;
   1061      1.18  drochner 	}
   1062      1.18  drochner }
   1063      1.18  drochner 
   1064      1.18  drochner void
   1065      1.28    bouyer pciide_print_modes(cp)
   1066      1.28    bouyer 	struct pciide_channel *cp;
   1067      1.18  drochner {
   1068      1.28    bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1069      1.28    bouyer 	int drive;
   1070      1.18  drochner 	struct channel_softc *chp;
   1071      1.18  drochner 	struct ata_drive_datas *drvp;
   1072      1.18  drochner 
   1073      1.28    bouyer 	chp = &cp->wdc_channel;
   1074      1.28    bouyer 	for (drive = 0; drive < 2; drive++) {
   1075      1.28    bouyer 		drvp = &chp->ch_drive[drive];
   1076      1.28    bouyer 		if ((drvp->drive_flags & DRIVE) == 0)
   1077      1.28    bouyer 			continue;
   1078      1.28    bouyer 		printf("%s(%s:%d:%d): using PIO mode %d",
   1079      1.28    bouyer 		    drvp->drv_softc->dv_xname,
   1080      1.28    bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname,
   1081      1.28    bouyer 		    chp->channel, drive, drvp->PIO_mode);
   1082      1.28    bouyer 		if (drvp->drive_flags & DRIVE_DMA)
   1083      1.28    bouyer 			printf(", DMA mode %d", drvp->DMA_mode);
   1084      1.28    bouyer 		if (drvp->drive_flags & DRIVE_UDMA)
   1085      1.28    bouyer 			printf(", Ultra-DMA mode %d", drvp->UDMA_mode);
   1086      1.28    bouyer 		if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA))
   1087      1.28    bouyer 			printf(" (using DMA data transfers)");
   1088      1.28    bouyer 		printf("\n");
   1089      1.18  drochner 	}
   1090      1.18  drochner }
   1091      1.18  drochner 
   1092      1.18  drochner void
   1093      1.18  drochner default_setup_cap(sc)
   1094      1.18  drochner 	struct pciide_softc *sc;
   1095      1.18  drochner {
   1096      1.18  drochner 	if (sc->sc_dma_ok)
   1097      1.18  drochner 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA;
   1098      1.27    bouyer 	sc->sc_wdcdev.PIO_cap = 0;
   1099      1.27    bouyer 	sc->sc_wdcdev.DMA_cap = 0;
   1100      1.18  drochner }
   1101      1.18  drochner 
   1102      1.18  drochner void
   1103      1.28    bouyer default_setup_chip(sc)
   1104      1.18  drochner 	struct pciide_softc *sc;
   1105       1.5       cgd {
   1106      1.18  drochner 	int channel, drive, idedma_ctl;
   1107      1.18  drochner 	struct channel_softc *chp;
   1108      1.18  drochner 	struct ata_drive_datas *drvp;
   1109      1.18  drochner 
   1110      1.18  drochner 	if (sc->sc_dma_ok == 0)
   1111      1.18  drochner 		return; /* nothing to do */
   1112      1.18  drochner 
   1113      1.18  drochner 	/* Allocate DMA maps */
   1114      1.18  drochner 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   1115      1.18  drochner 		idedma_ctl = 0;
   1116      1.18  drochner 		chp = &sc->pciide_channels[channel].wdc_channel;
   1117      1.18  drochner 		for (drive = 0; drive < 2; drive++) {
   1118      1.18  drochner 			drvp = &chp->ch_drive[drive];
   1119      1.18  drochner 			/* If no drive, skip */
   1120      1.18  drochner 			if ((drvp->drive_flags & DRIVE) == 0)
   1121      1.18  drochner 				continue;
   1122      1.18  drochner 			if ((drvp->drive_flags & DRIVE_DMA) == 0)
   1123      1.18  drochner 				continue;
   1124      1.18  drochner 			if (pciide_dma_table_setup(sc, channel, drive) != 0) {
   1125      1.18  drochner 				/* Abort DMA setup */
   1126      1.18  drochner 				printf("%s:%d:%d: can't allocate DMA maps, "
   1127      1.18  drochner 				    "using PIO transfers\n",
   1128      1.18  drochner 				    sc->sc_wdcdev.sc_dev.dv_xname,
   1129      1.18  drochner 				    channel, drive);
   1130      1.18  drochner 				drvp->drive_flags &= ~DRIVE_DMA;
   1131      1.18  drochner 			}
   1132      1.18  drochner 			printf("%s:%d:%d: using DMA data tranferts\n",
   1133      1.18  drochner 			    sc->sc_wdcdev.sc_dev.dv_xname,
   1134      1.18  drochner 			    channel, drive);
   1135      1.18  drochner 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1136      1.18  drochner 		}
   1137      1.18  drochner 		if (idedma_ctl != 0) {
   1138      1.18  drochner 			/* Add software bits in status register */
   1139      1.18  drochner 			bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1140      1.18  drochner 			    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
   1141      1.18  drochner 			    idedma_ctl);
   1142      1.18  drochner 		}
   1143      1.18  drochner 	}
   1144      1.18  drochner 
   1145      1.18  drochner }
   1146      1.18  drochner 
   1147      1.18  drochner void
   1148      1.28    bouyer default_channel_map(pa, cp)
   1149      1.18  drochner 	struct pci_attach_args *pa;
   1150      1.18  drochner 	struct pciide_channel *cp;
   1151      1.18  drochner {
   1152      1.28    bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1153      1.18  drochner 	bus_size_t cmdsize, ctlsize;
   1154       1.6       cgd 	pcireg_t csr;
   1155       1.6       cgd 	const char *failreason = NULL;
   1156      1.18  drochner 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   1157      1.18  drochner 	int interface =
   1158      1.28    bouyer 	    PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
   1159      1.18  drochner 
   1160      1.18  drochner 	if (interface & PCIIDE_INTERFACE_PCI(wdc_cp->channel))
   1161      1.28    bouyer 		cp->hw_ok = pciide_mapregs_native(pa, cp, &cmdsize, &ctlsize);
   1162      1.18  drochner 	else
   1163      1.28    bouyer 		cp->hw_ok = pciide_mapregs_compat(pa, cp, wdc_cp->channel,
   1164      1.18  drochner 		    &cmdsize, &ctlsize);
   1165      1.18  drochner 	if (cp->hw_ok == 0)
   1166      1.18  drochner 		return;
   1167       1.6       cgd 
   1168       1.6       cgd 	/*
   1169       1.6       cgd 	 * Check to see if something appears to be there.
   1170       1.6       cgd 	 */
   1171      1.18  drochner 	if (!wdcprobe(wdc_cp)) {
   1172       1.6       cgd 		failreason = "not responding; disabled or no drives?";
   1173       1.6       cgd 		goto out;
   1174       1.6       cgd 	}
   1175       1.5       cgd 
   1176       1.5       cgd 	/*
   1177       1.6       cgd 	 * Now, make sure it's actually attributable to this PCI IDE
   1178       1.6       cgd 	 * channel by trying to access the channel again while the
   1179       1.6       cgd 	 * PCI IDE controller's I/O space is disabled.  (If the
   1180       1.6       cgd 	 * channel no longer appears to be there, it belongs to
   1181       1.6       cgd 	 * this controller.)  YUCK!
   1182       1.5       cgd 	 */
   1183      1.28    bouyer 	csr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG);
   1184      1.28    bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG,
   1185       1.6       cgd 	    csr & ~PCI_COMMAND_IO_ENABLE);
   1186      1.18  drochner 	if (wdcprobe(wdc_cp))
   1187       1.6       cgd 		failreason = "other hardware responding at addresses";
   1188      1.28    bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG, csr);
   1189       1.6       cgd 
   1190       1.6       cgd out:
   1191      1.18  drochner 	if (failreason) {
   1192      1.18  drochner 		printf("%s: %s channel ignored (%s)\n",
   1193      1.18  drochner 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name,
   1194      1.18  drochner 		    failreason);
   1195      1.18  drochner 		cp->hw_ok = 0;
   1196      1.18  drochner 		bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh, cmdsize);
   1197      1.18  drochner 		bus_space_unmap(wdc_cp->ctl_iot, wdc_cp->ctl_ioh, ctlsize);
   1198      1.18  drochner 	}
   1199      1.28    bouyer 	pciide_map_compat_intr(pa, cp, wdc_cp->channel, interface);
   1200      1.18  drochner 	if (cp->hw_ok) {
   1201      1.18  drochner 		wdc_cp->data32iot = wdc_cp->cmd_iot;
   1202      1.18  drochner 		wdc_cp->data32ioh = wdc_cp->cmd_ioh;
   1203      1.18  drochner 		wdcattach(wdc_cp);
   1204      1.18  drochner 	}
   1205       1.9    bouyer }
   1206       1.9    bouyer 
   1207       1.9    bouyer void
   1208       1.9    bouyer piix_setup_cap(sc)
   1209       1.9    bouyer 	struct pciide_softc *sc;
   1210       1.9    bouyer {
   1211       1.9    bouyer 	if (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82371AB_IDE)
   1212       1.9    bouyer 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
   1213       1.9    bouyer 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
   1214       1.9    bouyer 	    WDC_CAPABILITY_DMA;
   1215      1.27    bouyer 	sc->sc_wdcdev.PIO_cap = 4;
   1216      1.27    bouyer 	sc->sc_wdcdev.DMA_cap = 2;
   1217      1.27    bouyer 	sc->sc_wdcdev.UDMA_cap = 2;
   1218      1.28    bouyer 	if (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82371SB_IDE ||
   1219      1.28    bouyer 	    sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82371AB_IDE)
   1220      1.28    bouyer 		sc->sc_wdcdev.set_modes = piix3_4_setup_channel;
   1221      1.28    bouyer 	else
   1222      1.28    bouyer 		sc->sc_wdcdev.set_modes = piix_setup_channel;
   1223       1.9    bouyer }
   1224       1.9    bouyer 
   1225       1.9    bouyer void
   1226      1.28    bouyer piix_setup_chip(sc)
   1227       1.9    bouyer 	struct pciide_softc *sc;
   1228       1.9    bouyer {
   1229      1.28    bouyer 	u_int8_t channel;
   1230       1.9    bouyer 
   1231       1.9    bouyer 
   1232      1.28    bouyer 	WDCDEBUG_PRINT(("piix_setup_chip: old idetim=0x%x\n",
   1233      1.28    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM)), DEBUG_PROBE);
   1234       1.9    bouyer 
   1235      1.18  drochner 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   1236      1.28    bouyer 		piix_setup_channel(&sc->pciide_channels[channel].wdc_channel);
   1237      1.28    bouyer 	}
   1238      1.28    bouyer 	WDCDEBUG_PRINT(("piix_setup_chip: idetim=0x%x\n",
   1239      1.28    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM)), DEBUG_PROBE);
   1240      1.28    bouyer }
   1241      1.28    bouyer 
   1242      1.28    bouyer void
   1243      1.28    bouyer piix_setup_channel(chp)
   1244      1.28    bouyer 	struct channel_softc *chp;
   1245      1.28    bouyer {
   1246      1.28    bouyer 	u_int8_t mode[2], drive;
   1247      1.28    bouyer 	u_int32_t oidetim, idetim, idedma_ctl;
   1248      1.28    bouyer 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   1249      1.28    bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1250      1.28    bouyer 	struct ata_drive_datas *drvp = cp->wdc_channel.ch_drive;
   1251      1.28    bouyer 
   1252      1.28    bouyer 	oidetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM);
   1253      1.28    bouyer 	idetim = PIIX_IDETIM_CLEAR(oidetim, 0xffff, chp->channel);
   1254      1.28    bouyer 	idedma_ctl = 0;
   1255      1.28    bouyer 
   1256      1.28    bouyer 	/* set up new idetim: Enable IDE registers decode */
   1257      1.28    bouyer 	idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE,
   1258      1.28    bouyer 	    chp->channel);
   1259       1.9    bouyer 
   1260      1.28    bouyer 	/* setup DMA */
   1261      1.28    bouyer 	pciide_channel_dma_setup(cp);
   1262       1.9    bouyer 
   1263      1.28    bouyer 	/*
   1264      1.28    bouyer 	 * Here we have to mess up with drives mode: PIIX can't have
   1265      1.28    bouyer 	 * different timings for master and slave drives.
   1266      1.28    bouyer 	 * We need to find the best combination.
   1267      1.28    bouyer 	 */
   1268       1.9    bouyer 
   1269      1.28    bouyer 	/* If both drives supports DMA, take the lower mode */
   1270      1.28    bouyer 	if ((drvp[0].drive_flags & DRIVE_DMA) &&
   1271      1.28    bouyer 	    (drvp[1].drive_flags & DRIVE_DMA)) {
   1272      1.28    bouyer 		mode[0] = mode[1] =
   1273      1.28    bouyer 		    min(drvp[0].DMA_mode, drvp[1].DMA_mode);
   1274      1.28    bouyer 		    drvp[0].DMA_mode = mode[0];
   1275      1.28    bouyer 		goto ok;
   1276      1.28    bouyer 	}
   1277      1.28    bouyer 	/*
   1278      1.28    bouyer 	 * If only one drive supports DMA, use its mode, and
   1279      1.28    bouyer 	 * put the other one in PIO mode 0 if mode not compatible
   1280      1.28    bouyer 	 */
   1281      1.28    bouyer 	if (drvp[0].drive_flags & DRIVE_DMA) {
   1282      1.28    bouyer 		mode[0] = drvp[0].DMA_mode;
   1283      1.28    bouyer 		mode[1] = drvp[1].PIO_mode;
   1284      1.28    bouyer 		if (piix_isp_pio[mode[1]] != piix_isp_dma[mode[0]] ||
   1285      1.28    bouyer 		    piix_rtc_pio[mode[1]] != piix_rtc_dma[mode[0]])
   1286      1.28    bouyer 			mode[1] = 0;
   1287      1.28    bouyer 		goto ok;
   1288      1.28    bouyer 	}
   1289      1.28    bouyer 	if (drvp[1].drive_flags & DRIVE_DMA) {
   1290      1.28    bouyer 		mode[1] = drvp[1].DMA_mode;
   1291      1.28    bouyer 		mode[0] = drvp[0].PIO_mode;
   1292      1.28    bouyer 		if (piix_isp_pio[mode[0]] != piix_isp_dma[mode[1]] ||
   1293      1.28    bouyer 		    piix_rtc_pio[mode[0]] != piix_rtc_dma[mode[1]])
   1294       1.9    bouyer 			mode[0] = 0;
   1295      1.28    bouyer 		goto ok;
   1296      1.28    bouyer 	}
   1297      1.28    bouyer 	/*
   1298      1.28    bouyer 	 * If both drives are not DMA, takes the lower mode, unless
   1299      1.28    bouyer 	 * one of them is PIO mode < 2
   1300      1.28    bouyer 	 */
   1301      1.28    bouyer 	if (drvp[0].PIO_mode < 2) {
   1302      1.28    bouyer 		mode[0] = 0;
   1303      1.28    bouyer 		mode[1] = drvp[1].PIO_mode;
   1304      1.28    bouyer 	} else if (drvp[1].PIO_mode < 2) {
   1305      1.28    bouyer 		mode[1] = 0;
   1306      1.28    bouyer 		mode[0] = drvp[0].PIO_mode;
   1307      1.28    bouyer 	} else {
   1308      1.28    bouyer 		mode[0] = mode[1] =
   1309      1.28    bouyer 		    min(drvp[1].PIO_mode, drvp[0].PIO_mode);
   1310      1.28    bouyer 	}
   1311      1.28    bouyer ok:	/* The modes are setup */
   1312      1.28    bouyer 	for (drive = 0; drive < 2; drive++) {
   1313      1.28    bouyer 		if (drvp[drive].drive_flags & DRIVE_DMA) {
   1314      1.28    bouyer 			drvp[drive].DMA_mode = mode[drive];
   1315       1.9    bouyer 			idetim |= piix_setup_idetim_timings(
   1316      1.28    bouyer 			    mode[drive], 1, chp->channel);
   1317      1.28    bouyer 			goto end;
   1318      1.28    bouyer 		} else
   1319      1.28    bouyer 			drvp[drive].PIO_mode = mode[drive];
   1320      1.28    bouyer 	}
   1321      1.28    bouyer 	/* If we are there, none of the drives are DMA */
   1322      1.28    bouyer 	if (mode[0] >= 2)
   1323      1.28    bouyer 		idetim |= piix_setup_idetim_timings(
   1324      1.28    bouyer 		    mode[0], 0, chp->channel);
   1325      1.28    bouyer 	else
   1326      1.28    bouyer 		idetim |= piix_setup_idetim_timings(
   1327      1.28    bouyer 		    mode[1], 0, chp->channel);
   1328      1.28    bouyer end:	/*
   1329      1.28    bouyer 	 * timing mode is now set up in the controller. Enable
   1330      1.28    bouyer 	 * it per-drive
   1331      1.28    bouyer 	 */
   1332      1.28    bouyer 	for (drive = 0; drive < 2; drive++) {
   1333      1.28    bouyer 		/* If no drive, skip */
   1334      1.28    bouyer 		if ((drvp[drive].drive_flags & DRIVE) == 0)
   1335      1.28    bouyer 			continue;
   1336      1.28    bouyer 		idetim |= piix_setup_idetim_drvs(&drvp[drive]);
   1337      1.28    bouyer 		if (drvp[drive].drive_flags & DRIVE_DMA)
   1338      1.28    bouyer 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1339      1.28    bouyer 	}
   1340      1.28    bouyer 	if (idedma_ctl != 0) {
   1341      1.28    bouyer 		/* Add software bits in status register */
   1342      1.28    bouyer 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1343      1.28    bouyer 		    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
   1344      1.28    bouyer 		    idedma_ctl);
   1345       1.9    bouyer 	}
   1346      1.28    bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_IDETIM, idetim);
   1347      1.28    bouyer 	pciide_print_modes(cp);
   1348       1.9    bouyer }
   1349       1.9    bouyer 
   1350       1.9    bouyer void
   1351      1.28    bouyer piix3_4_setup_chip(sc)
   1352       1.9    bouyer 	struct pciide_softc *sc;
   1353       1.8  drochner {
   1354      1.28    bouyer 	int channel;
   1355       1.9    bouyer 
   1356       1.9    bouyer 	WDCDEBUG_PRINT(("piix3_4_setup_chip: old idetim=0x%x, sidetim=0x%x",
   1357      1.28    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM),
   1358      1.28    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM)), DEBUG_PROBE);
   1359       1.9    bouyer 	if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
   1360       1.9    bouyer 		WDCDEBUG_PRINT((", udamreg 0x%x",
   1361      1.28    bouyer 		    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG)),
   1362       1.9    bouyer 		    DEBUG_PROBE);
   1363       1.9    bouyer 	}
   1364       1.9    bouyer 	WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
   1365       1.9    bouyer 
   1366      1.18  drochner 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   1367      1.28    bouyer 		piix3_4_setup_channel(
   1368      1.28    bouyer 		    &sc->pciide_channels[channel].wdc_channel);
   1369      1.28    bouyer 	}
   1370      1.28    bouyer 
   1371      1.28    bouyer 	WDCDEBUG_PRINT(("piix3_4_setup_chip: idetim=0x%x, sidetim=0x%x",
   1372      1.28    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM),
   1373      1.28    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM)), DEBUG_PROBE);
   1374      1.28    bouyer 	if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
   1375      1.28    bouyer 		WDCDEBUG_PRINT((", udmareg=0x%x",
   1376      1.28    bouyer 		pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG)),
   1377      1.28    bouyer 		DEBUG_PROBE);
   1378      1.28    bouyer 	}
   1379      1.28    bouyer 	WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
   1380      1.28    bouyer }
   1381      1.28    bouyer 
   1382      1.28    bouyer void
   1383      1.28    bouyer piix3_4_setup_channel(chp)
   1384      1.28    bouyer 	struct channel_softc *chp;
   1385      1.28    bouyer {
   1386      1.28    bouyer 	struct ata_drive_datas *drvp;
   1387      1.28    bouyer 	u_int32_t oidetim, idetim, sidetim, udmareg, idedma_ctl;
   1388      1.28    bouyer 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   1389      1.28    bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1390      1.28    bouyer 	int drive;
   1391      1.28    bouyer 
   1392      1.28    bouyer 	oidetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM);
   1393      1.28    bouyer 	sidetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM);
   1394      1.28    bouyer 	udmareg = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG);
   1395      1.28    bouyer 	idetim = PIIX_IDETIM_CLEAR(oidetim, 0xffff, chp->channel);
   1396      1.28    bouyer 	sidetim &= ~(PIIX_SIDETIM_ISP_MASK(chp->channel) |
   1397      1.28    bouyer 	    PIIX_SIDETIM_RTC_MASK(chp->channel));
   1398      1.28    bouyer 
   1399      1.28    bouyer 	idedma_ctl = 0;
   1400      1.28    bouyer 	/* If channel disabled, no need to go further */
   1401      1.28    bouyer 	if ((PIIX_IDETIM_READ(oidetim, chp->channel) & PIIX_IDETIM_IDE) == 0)
   1402      1.28    bouyer 		return;
   1403      1.28    bouyer 	/* set up new idetim: Enable IDE registers decode */
   1404      1.28    bouyer 	idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE, chp->channel);
   1405      1.28    bouyer 
   1406      1.28    bouyer 	/* setup DMA if needed */
   1407      1.28    bouyer 	pciide_channel_dma_setup(cp);
   1408      1.28    bouyer 
   1409      1.28    bouyer 	for (drive = 0; drive < 2; drive++) {
   1410      1.28    bouyer 		udmareg &= ~(PIIX_UDMACTL_DRV_EN(chp->channel, drive) |
   1411      1.28    bouyer 		    PIIX_UDMATIM_SET(0x3, chp->channel, drive));
   1412      1.28    bouyer 		drvp = &chp->ch_drive[drive];
   1413      1.28    bouyer 		/* If no drive, skip */
   1414      1.28    bouyer 		if ((drvp->drive_flags & DRIVE) == 0)
   1415       1.9    bouyer 			continue;
   1416      1.28    bouyer 		if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
   1417      1.28    bouyer 		    (drvp->drive_flags & DRIVE_UDMA) == 0))
   1418      1.28    bouyer 			goto pio;
   1419      1.28    bouyer 
   1420      1.28    bouyer 		if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
   1421      1.28    bouyer 		    (drvp->drive_flags & DRIVE_UDMA)) {
   1422      1.28    bouyer 			/* use Ultra/DMA */
   1423      1.28    bouyer 			drvp->drive_flags &= ~DRIVE_DMA;
   1424      1.28    bouyer 			udmareg |= PIIX_UDMACTL_DRV_EN(
   1425      1.28    bouyer 			    chp->channel, drive);
   1426      1.28    bouyer 			udmareg |= PIIX_UDMATIM_SET(
   1427      1.28    bouyer 			    piix4_sct_udma[drvp->UDMA_mode],
   1428      1.28    bouyer 			    chp->channel, drive);
   1429      1.28    bouyer 		} else {
   1430      1.28    bouyer 			/* use Multiword DMA */
   1431      1.28    bouyer 			drvp->drive_flags &= ~DRIVE_UDMA;
   1432       1.9    bouyer 			if (drive == 0) {
   1433       1.9    bouyer 				idetim |= piix_setup_idetim_timings(
   1434      1.28    bouyer 				    drvp->DMA_mode, 1, chp->channel);
   1435       1.9    bouyer 			} else {
   1436       1.9    bouyer 				sidetim |= piix_setup_sidetim_timings(
   1437      1.28    bouyer 					drvp->DMA_mode, 1, chp->channel);
   1438       1.9    bouyer 				idetim =PIIX_IDETIM_SET(idetim,
   1439      1.28    bouyer 				    PIIX_IDETIM_SITRE, chp->channel);
   1440       1.9    bouyer 			}
   1441       1.9    bouyer 		}
   1442      1.28    bouyer 		idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1443      1.28    bouyer 
   1444      1.28    bouyer pio:		/* use PIO mode */
   1445      1.28    bouyer 		idetim |= piix_setup_idetim_drvs(drvp);
   1446      1.28    bouyer 		if (drive == 0) {
   1447      1.28    bouyer 			idetim |= piix_setup_idetim_timings(
   1448      1.28    bouyer 			    drvp->PIO_mode, 0, chp->channel);
   1449      1.28    bouyer 		} else {
   1450      1.28    bouyer 			sidetim |= piix_setup_sidetim_timings(
   1451      1.28    bouyer 				drvp->PIO_mode, 0, chp->channel);
   1452      1.28    bouyer 			idetim =PIIX_IDETIM_SET(idetim,
   1453      1.28    bouyer 			    PIIX_IDETIM_SITRE, chp->channel);
   1454       1.9    bouyer 		}
   1455       1.9    bouyer 	}
   1456      1.28    bouyer 	if (idedma_ctl != 0) {
   1457      1.28    bouyer 		/* Add software bits in status register */
   1458      1.28    bouyer 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1459      1.28    bouyer 		    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
   1460      1.28    bouyer 		    idedma_ctl);
   1461       1.9    bouyer 	}
   1462      1.28    bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_IDETIM, idetim);
   1463      1.28    bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM, sidetim);
   1464      1.28    bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG, udmareg);
   1465      1.28    bouyer 	pciide_print_modes(cp);
   1466       1.9    bouyer }
   1467       1.8  drochner 
   1468      1.28    bouyer 
   1469       1.9    bouyer /* setup ISP and RTC fields, based on mode */
   1470       1.9    bouyer static u_int32_t
   1471       1.9    bouyer piix_setup_idetim_timings(mode, dma, channel)
   1472       1.9    bouyer 	u_int8_t mode;
   1473       1.9    bouyer 	u_int8_t dma;
   1474       1.9    bouyer 	u_int8_t channel;
   1475       1.9    bouyer {
   1476       1.9    bouyer 
   1477       1.9    bouyer 	if (dma)
   1478       1.9    bouyer 		return PIIX_IDETIM_SET(0,
   1479       1.9    bouyer 		    PIIX_IDETIM_ISP_SET(piix_isp_dma[mode]) |
   1480       1.9    bouyer 		    PIIX_IDETIM_RTC_SET(piix_rtc_dma[mode]),
   1481       1.9    bouyer 		    channel);
   1482       1.9    bouyer 	else
   1483       1.9    bouyer 		return PIIX_IDETIM_SET(0,
   1484       1.9    bouyer 		    PIIX_IDETIM_ISP_SET(piix_isp_pio[mode]) |
   1485       1.9    bouyer 		    PIIX_IDETIM_RTC_SET(piix_rtc_pio[mode]),
   1486       1.9    bouyer 		    channel);
   1487       1.8  drochner }
   1488       1.8  drochner 
   1489       1.9    bouyer /* setup DTE, PPE, IE and TIME field based on PIO mode */
   1490       1.9    bouyer static u_int32_t
   1491       1.9    bouyer piix_setup_idetim_drvs(drvp)
   1492       1.9    bouyer 	struct ata_drive_datas *drvp;
   1493       1.6       cgd {
   1494       1.9    bouyer 	u_int32_t ret = 0;
   1495       1.9    bouyer 	struct channel_softc *chp = drvp->chnl_softc;
   1496       1.9    bouyer 	u_int8_t channel = chp->channel;
   1497       1.9    bouyer 	u_int8_t drive = drvp->drive;
   1498       1.9    bouyer 
   1499       1.9    bouyer 	/*
   1500       1.9    bouyer 	 * If drive is using UDMA, timings setups are independant
   1501       1.9    bouyer 	 * So just check DMA and PIO here.
   1502       1.9    bouyer 	 */
   1503       1.9    bouyer 	if (drvp->drive_flags & DRIVE_DMA) {
   1504       1.9    bouyer 		/* if mode = DMA mode 0, use compatible timings */
   1505       1.9    bouyer 		if ((drvp->drive_flags & DRIVE_DMA) &&
   1506       1.9    bouyer 		    drvp->DMA_mode == 0) {
   1507       1.9    bouyer 			drvp->PIO_mode = 0;
   1508       1.9    bouyer 			return ret;
   1509       1.9    bouyer 		}
   1510       1.9    bouyer 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
   1511       1.9    bouyer 		/*
   1512       1.9    bouyer 		 * PIO and DMA timings are the same, use fast timings for PIO
   1513       1.9    bouyer 		 * too, else use compat timings.
   1514       1.9    bouyer 		 */
   1515       1.9    bouyer 		if ((piix_isp_pio[drvp->PIO_mode] !=
   1516       1.9    bouyer 		    piix_isp_dma[drvp->DMA_mode]) ||
   1517       1.9    bouyer 		    (piix_rtc_pio[drvp->PIO_mode] !=
   1518       1.9    bouyer 		    piix_rtc_dma[drvp->DMA_mode]))
   1519       1.9    bouyer 			drvp->PIO_mode = 0;
   1520       1.9    bouyer 		/* if PIO mode <= 2, use compat timings for PIO */
   1521       1.9    bouyer 		if (drvp->PIO_mode <= 2) {
   1522       1.9    bouyer 			ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_DTE(drive),
   1523       1.9    bouyer 			    channel);
   1524       1.9    bouyer 			return ret;
   1525       1.9    bouyer 		}
   1526       1.9    bouyer 	}
   1527       1.6       cgd 
   1528       1.6       cgd 	/*
   1529       1.9    bouyer 	 * Now setup PIO modes. If mode < 2, use compat timings.
   1530       1.9    bouyer 	 * Else enable fast timings. Enable IORDY and prefetch/post
   1531       1.9    bouyer 	 * if PIO mode >= 3.
   1532       1.6       cgd 	 */
   1533       1.6       cgd 
   1534       1.9    bouyer 	if (drvp->PIO_mode < 2)
   1535       1.9    bouyer 		return ret;
   1536       1.9    bouyer 
   1537       1.9    bouyer 	ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
   1538       1.9    bouyer 	if (drvp->PIO_mode >= 3) {
   1539       1.9    bouyer 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_IE(drive), channel);
   1540       1.9    bouyer 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_PPE(drive), channel);
   1541       1.9    bouyer 	}
   1542       1.9    bouyer 	return ret;
   1543       1.9    bouyer }
   1544       1.9    bouyer 
   1545       1.9    bouyer /* setup values in SIDETIM registers, based on mode */
   1546       1.9    bouyer static u_int32_t
   1547       1.9    bouyer piix_setup_sidetim_timings(mode, dma, channel)
   1548       1.9    bouyer 	u_int8_t mode;
   1549       1.9    bouyer 	u_int8_t dma;
   1550       1.9    bouyer 	u_int8_t channel;
   1551       1.9    bouyer {
   1552       1.9    bouyer 	if (dma)
   1553       1.9    bouyer 		return PIIX_SIDETIM_ISP_SET(piix_isp_dma[mode], channel) |
   1554       1.9    bouyer 		    PIIX_SIDETIM_RTC_SET(piix_rtc_dma[mode], channel);
   1555       1.9    bouyer 	else
   1556       1.9    bouyer 		return PIIX_SIDETIM_ISP_SET(piix_isp_pio[mode], channel) |
   1557       1.9    bouyer 		    PIIX_SIDETIM_RTC_SET(piix_rtc_pio[mode], channel);
   1558       1.9    bouyer }
   1559       1.9    bouyer 
   1560      1.18  drochner void
   1561      1.28    bouyer piix_channel_map(pa, cp)
   1562       1.9    bouyer 	struct pci_attach_args *pa;
   1563      1.18  drochner 	struct pciide_channel *cp;
   1564       1.9    bouyer {
   1565      1.28    bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1566      1.18  drochner 	bus_size_t cmdsize, ctlsize;
   1567      1.18  drochner 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   1568      1.28    bouyer 	u_int32_t idetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM);
   1569       1.9    bouyer 
   1570      1.28    bouyer 	if ((PIIX_IDETIM_READ(idetim, wdc_cp->channel) &
   1571      1.28    bouyer 	    PIIX_IDETIM_IDE) == 0) {
   1572      1.18  drochner 		printf("%s: %s channel ignored (disabled)\n",
   1573      1.18  drochner 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   1574      1.18  drochner 		return;
   1575      1.18  drochner 	}
   1576      1.18  drochner 
   1577      1.18  drochner 	/* PIIX are compat-only pciide devices */
   1578      1.28    bouyer 	pciide_mapchan(pa, cp, 0, &cmdsize, &ctlsize);
   1579      1.18  drochner 	if (cp->hw_ok == 0)
   1580      1.18  drochner 		return;
   1581      1.28    bouyer 	if (pciiide_chan_candisable(cp)) {
   1582      1.18  drochner 		idetim = PIIX_IDETIM_CLEAR(idetim, PIIX_IDETIM_IDE,
   1583      1.18  drochner 					   wdc_cp->channel);
   1584      1.28    bouyer 		pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_IDETIM, idetim);
   1585      1.18  drochner 	}
   1586      1.28    bouyer 	pciide_map_compat_intr(pa, cp, wdc_cp->channel, 0);
   1587       1.9    bouyer }
   1588       1.9    bouyer 
   1589       1.9    bouyer void
   1590       1.9    bouyer apollo_setup_cap(sc)
   1591       1.9    bouyer 	struct pciide_softc *sc;
   1592       1.9    bouyer {
   1593      1.11    bouyer 	if (sc->sc_pp->ide_product == PCI_PRODUCT_VIATECH_VT82C586A_IDE)
   1594       1.9    bouyer 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
   1595       1.9    bouyer 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
   1596       1.9    bouyer 	    WDC_CAPABILITY_DMA;
   1597      1.27    bouyer 	sc->sc_wdcdev.PIO_cap = 4;
   1598      1.27    bouyer 	sc->sc_wdcdev.DMA_cap = 2;
   1599      1.27    bouyer 	sc->sc_wdcdev.UDMA_cap = 2;
   1600      1.28    bouyer 	sc->sc_wdcdev.set_modes = apollo_setup_channel;
   1601       1.9    bouyer 
   1602       1.9    bouyer }
   1603      1.28    bouyer 
   1604       1.9    bouyer void
   1605      1.28    bouyer apollo_setup_chip(sc)
   1606       1.9    bouyer 	struct pciide_softc *sc;
   1607       1.9    bouyer {
   1608      1.28    bouyer 	int channel;
   1609       1.9    bouyer 
   1610       1.9    bouyer 	WDCDEBUG_PRINT(("apollo_setup_chip: old APO_IDECONF=0x%x, "
   1611       1.9    bouyer 	    "APO_CTLMISC=0x%x, APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
   1612      1.28    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_IDECONF),
   1613      1.28    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_CTLMISC),
   1614      1.28    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM),
   1615      1.28    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA)),
   1616       1.9    bouyer 	    DEBUG_PROBE);
   1617       1.9    bouyer 
   1618      1.18  drochner 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   1619      1.28    bouyer 		apollo_setup_channel(&sc->pciide_channels[channel].wdc_channel);
   1620      1.28    bouyer 	}
   1621      1.28    bouyer 	WDCDEBUG_PRINT(("apollo_setup_chip: APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
   1622      1.28    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM),
   1623      1.28    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA)), DEBUG_PROBE);
   1624      1.28    bouyer }
   1625      1.28    bouyer 
   1626      1.28    bouyer void
   1627      1.28    bouyer apollo_setup_channel(chp)
   1628      1.28    bouyer 	struct channel_softc *chp;
   1629      1.28    bouyer {
   1630      1.28    bouyer 	u_int32_t udmatim_reg, datatim_reg;
   1631      1.28    bouyer 	u_int8_t idedma_ctl;
   1632      1.28    bouyer 	int mode, drive;
   1633      1.28    bouyer 	struct ata_drive_datas *drvp;
   1634      1.28    bouyer 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   1635      1.28    bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1636      1.28    bouyer 
   1637      1.28    bouyer 	idedma_ctl = 0;
   1638      1.28    bouyer 	datatim_reg = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM);
   1639      1.28    bouyer 	udmatim_reg = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA);
   1640      1.28    bouyer 	datatim_reg &= ~APO_DATATIM_MASK(chp->channel);
   1641      1.28    bouyer 	udmatim_reg &= ~AP0_UDMA_MASK(chp->channel);
   1642      1.28    bouyer 
   1643      1.28    bouyer 	/* setup DMA if needed */
   1644      1.28    bouyer 	pciide_channel_dma_setup(cp);
   1645       1.9    bouyer 
   1646      1.28    bouyer 	for (drive = 0; drive < 2; drive++) {
   1647      1.28    bouyer 		drvp = &chp->ch_drive[drive];
   1648      1.28    bouyer 		/* If no drive, skip */
   1649      1.28    bouyer 		if ((drvp->drive_flags & DRIVE) == 0)
   1650      1.28    bouyer 			continue;
   1651      1.28    bouyer 		/* add timing values, setup DMA if needed */
   1652      1.28    bouyer 		if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
   1653      1.28    bouyer 		    (drvp->drive_flags & DRIVE_UDMA) == 0)) {
   1654      1.28    bouyer 			mode = drvp->PIO_mode;
   1655      1.28    bouyer 			goto pio;
   1656       1.8  drochner 		}
   1657      1.28    bouyer 		if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
   1658      1.28    bouyer 		    (drvp->drive_flags & DRIVE_UDMA)) {
   1659      1.28    bouyer 			/* use Ultra/DMA */
   1660      1.28    bouyer 			drvp->drive_flags &= ~DRIVE_DMA;
   1661      1.28    bouyer 			udmatim_reg |= APO_UDMA_EN(chp->channel, drive) |
   1662      1.28    bouyer 			    APO_UDMA_EN_MTH(chp->channel, drive) |
   1663      1.28    bouyer 			    APO_UDMA_TIME(chp->channel, drive,
   1664      1.28    bouyer 				apollo_udma_tim[drvp->UDMA_mode]);
   1665      1.28    bouyer 			/* can use PIO timings, MW DMA unused */
   1666      1.28    bouyer 			mode = drvp->PIO_mode;
   1667      1.28    bouyer 		} else {
   1668      1.28    bouyer 			/* use Multiword DMA */
   1669      1.28    bouyer 			drvp->drive_flags &= ~DRIVE_UDMA;
   1670      1.28    bouyer 			/* mode = min(pio, dma+2) */
   1671      1.28    bouyer 			if (drvp->PIO_mode <= (drvp->DMA_mode +2))
   1672      1.28    bouyer 				mode = drvp->PIO_mode;
   1673      1.28    bouyer 			else
   1674      1.28    bouyer 				mode = drvp->DMA_mode;
   1675       1.8  drochner 		}
   1676      1.28    bouyer 		idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1677      1.28    bouyer 
   1678      1.28    bouyer pio:		/* setup PIO mode */
   1679      1.28    bouyer 		datatim_reg |=
   1680      1.28    bouyer 		    APO_DATATIM_PULSE(chp->channel, drive,
   1681      1.28    bouyer 			apollo_pio_set[mode]) |
   1682      1.28    bouyer 		    APO_DATATIM_RECOV(chp->channel, drive,
   1683      1.28    bouyer 			apollo_pio_rec[mode]);
   1684      1.28    bouyer 		drvp->PIO_mode = mode;
   1685      1.28    bouyer 		drvp->DMA_mode = mode - 2;
   1686      1.28    bouyer 	}
   1687      1.28    bouyer 	if (idedma_ctl != 0) {
   1688      1.28    bouyer 		/* Add software bits in status register */
   1689      1.28    bouyer 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1690      1.28    bouyer 		    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
   1691      1.28    bouyer 		    idedma_ctl);
   1692       1.9    bouyer 	}
   1693      1.28    bouyer 	pciide_print_modes(cp);
   1694      1.28    bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, APO_DATATIM, datatim_reg);
   1695      1.28    bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, APO_UDMA, udmatim_reg);
   1696       1.9    bouyer }
   1697       1.6       cgd 
   1698      1.18  drochner void
   1699      1.28    bouyer apollo_channel_map(pa, cp)
   1700       1.9    bouyer 	struct pci_attach_args *pa;
   1701      1.18  drochner 	struct pciide_channel *cp;
   1702       1.9    bouyer {
   1703      1.28    bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1704      1.18  drochner 	bus_size_t cmdsize, ctlsize;
   1705      1.18  drochner 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   1706      1.28    bouyer 	u_int32_t ideconf = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_IDECONF);
   1707      1.18  drochner 	int interface =
   1708      1.28    bouyer 	    PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
   1709       1.6       cgd 
   1710      1.18  drochner 	if ((ideconf & APO_IDECONF_EN(wdc_cp->channel)) == 0) {
   1711      1.18  drochner 		printf("%s: %s channel ignored (disabled)\n",
   1712      1.18  drochner 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   1713      1.18  drochner 		return;
   1714      1.18  drochner 	}
   1715      1.18  drochner 
   1716      1.28    bouyer 	pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize);
   1717      1.18  drochner 	if (cp->hw_ok == 0)
   1718      1.18  drochner 		return;
   1719      1.28    bouyer 	if (pciiide_chan_candisable(cp)) {
   1720      1.18  drochner 		ideconf &= ~APO_IDECONF_EN(wdc_cp->channel);
   1721      1.28    bouyer 		pci_conf_write(sc->sc_pc, sc->sc_tag, APO_IDECONF, ideconf);
   1722      1.18  drochner 	}
   1723      1.28    bouyer 	pciide_map_compat_intr(pa, cp, wdc_cp->channel, interface);
   1724       1.5       cgd }
   1725       1.5       cgd 
   1726      1.18  drochner void
   1727      1.28    bouyer cmd_channel_map(pa, cp)
   1728       1.9    bouyer 	struct pci_attach_args *pa;
   1729      1.18  drochner 	struct pciide_channel *cp;
   1730       1.9    bouyer {
   1731      1.28    bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1732      1.18  drochner 	bus_size_t cmdsize, ctlsize;
   1733      1.18  drochner 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   1734      1.28    bouyer 	u_int8_t ctrl = pciide_pci_read(sc->sc_pc, sc->sc_tag, CMD_CTRL);
   1735      1.18  drochner 	int interface =
   1736      1.28    bouyer 	    PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
   1737       1.5       cgd 
   1738       1.9    bouyer 	/*
   1739       1.9    bouyer 	 * with a CMD PCI64x, if we get here, the first channel is enabled:
   1740       1.9    bouyer 	 * there's no way to disable the first channel without disabling
   1741       1.9    bouyer 	 * the whole device
   1742       1.9    bouyer 	 */
   1743      1.18  drochner 	if (wdc_cp->channel != 0 && (ctrl & CMD_CTRL_2PORT) == 0) {
   1744      1.18  drochner 		printf("%s: %s channel ignored (disabled)\n",
   1745      1.18  drochner 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   1746      1.18  drochner 		return;
   1747      1.18  drochner 	}
   1748      1.18  drochner 
   1749      1.28    bouyer 	pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize);
   1750      1.18  drochner 	if (cp->hw_ok == 0)
   1751      1.18  drochner 		return;
   1752      1.18  drochner 	if (wdc_cp->channel == 1) {
   1753      1.28    bouyer 		if (pciiide_chan_candisable(cp)) {
   1754      1.18  drochner 			ctrl &= ~CMD_CTRL_2PORT;
   1755      1.18  drochner 			pciide_pci_write(pa->pa_pc, pa->pa_tag,
   1756      1.24    bouyer 			    CMD_CTRL, ctrl);
   1757      1.18  drochner 		}
   1758      1.18  drochner 	}
   1759      1.28    bouyer 	pciide_map_compat_intr(pa, cp, wdc_cp->channel, interface);
   1760      1.14    bouyer }
   1761      1.14    bouyer 
   1762      1.14    bouyer void
   1763      1.14    bouyer cmd0643_6_setup_cap(sc)
   1764      1.14    bouyer 	struct pciide_softc *sc;
   1765      1.14    bouyer {
   1766      1.14    bouyer 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
   1767      1.14    bouyer 	    WDC_CAPABILITY_DMA;
   1768      1.27    bouyer 	sc->sc_wdcdev.PIO_cap = 4;
   1769      1.27    bouyer 	sc->sc_wdcdev.DMA_cap = 2;
   1770      1.28    bouyer 	sc->sc_wdcdev.set_modes = cmd0643_6_setup_channel;
   1771      1.14    bouyer }
   1772      1.14    bouyer 
   1773      1.14    bouyer void
   1774      1.28    bouyer cmd0643_6_setup_chip(sc)
   1775      1.14    bouyer 	struct pciide_softc *sc;
   1776      1.14    bouyer {
   1777      1.28    bouyer 	int channel;
   1778      1.28    bouyer 
   1779      1.28    bouyer 	WDCDEBUG_PRINT(("cmd0643_6_setup_chip: old timings reg 0x%x 0x%x\n",
   1780      1.28    bouyer 		pci_conf_read(sc->sc_pc, sc->sc_tag, 0x54),
   1781      1.28    bouyer 		pci_conf_read(sc->sc_pc, sc->sc_tag, 0x58)),
   1782      1.28    bouyer 		DEBUG_PROBE);
   1783      1.28    bouyer 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   1784      1.28    bouyer 		cmd0643_6_setup_channel(
   1785      1.28    bouyer 		    &sc->pciide_channels[channel].wdc_channel);
   1786      1.28    bouyer 	}
   1787      1.28    bouyer 	/* configure for DMA read multiple */
   1788      1.28    bouyer 	pciide_pci_write(sc->sc_pc, sc->sc_tag, CMD_DMA_MODE, CMD_DMA_MULTIPLE);
   1789      1.28    bouyer 	WDCDEBUG_PRINT(("cmd0643_6_setup_chip: timings reg now 0x%x 0x%x\n",
   1790      1.28    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, 0x54),
   1791      1.28    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, 0x58)),
   1792      1.28    bouyer 	    DEBUG_PROBE);
   1793      1.28    bouyer }
   1794      1.28    bouyer 
   1795      1.28    bouyer void
   1796      1.28    bouyer cmd0643_6_setup_channel(chp)
   1797      1.14    bouyer 	struct channel_softc *chp;
   1798      1.28    bouyer {
   1799      1.14    bouyer 	struct ata_drive_datas *drvp;
   1800      1.14    bouyer 	u_int8_t tim;
   1801      1.14    bouyer 	u_int32_t idedma_ctl;
   1802      1.28    bouyer 	int drive;
   1803      1.28    bouyer 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   1804      1.28    bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1805      1.28    bouyer 
   1806      1.28    bouyer 	idedma_ctl = 0;
   1807      1.28    bouyer 	/* setup DMA if needed */
   1808      1.28    bouyer 	pciide_channel_dma_setup(cp);
   1809      1.14    bouyer 
   1810      1.28    bouyer 	for (drive = 0; drive < 2; drive++) {
   1811      1.28    bouyer 		drvp = &chp->ch_drive[drive];
   1812      1.28    bouyer 		/* If no drive, skip */
   1813      1.28    bouyer 		if ((drvp->drive_flags & DRIVE) == 0)
   1814      1.28    bouyer 			continue;
   1815      1.28    bouyer 		/* add timing values, setup DMA if needed */
   1816      1.28    bouyer 		tim = cmd0643_6_data_tim_pio[drvp->PIO_mode];
   1817      1.28    bouyer 		if (drvp->drive_flags & DRIVE_DMA) {
   1818      1.14    bouyer 			/*
   1819      1.14    bouyer 			 * use Multiword DMA.
   1820      1.14    bouyer 			 * Timings will be used for both PIO and DMA, so adjust
   1821      1.14    bouyer 			 * DMA mode if needed
   1822      1.14    bouyer 			 */
   1823      1.14    bouyer 			if (drvp->PIO_mode >= 3 &&
   1824      1.14    bouyer 			    (drvp->DMA_mode + 2) > drvp->PIO_mode) {
   1825      1.14    bouyer 				drvp->DMA_mode = drvp->PIO_mode - 2;
   1826      1.14    bouyer 			}
   1827      1.14    bouyer 			tim = cmd0643_6_data_tim_dma[drvp->DMA_mode];
   1828      1.14    bouyer 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1829      1.14    bouyer 		}
   1830      1.28    bouyer 		pciide_pci_write(sc->sc_pc, sc->sc_tag,
   1831      1.28    bouyer 		    CMD_DATA_TIM(chp->channel, drive), tim);
   1832      1.28    bouyer 	}
   1833      1.28    bouyer 	if (idedma_ctl != 0) {
   1834      1.28    bouyer 		/* Add software bits in status register */
   1835      1.28    bouyer 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1836      1.28    bouyer 		    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
   1837      1.28    bouyer 		    idedma_ctl);
   1838      1.14    bouyer 	}
   1839      1.28    bouyer 	pciide_print_modes(cp);
   1840       1.1       cgd }
   1841       1.1       cgd 
   1842      1.18  drochner void
   1843      1.18  drochner cy693_setup_cap(sc)
   1844      1.18  drochner 	struct pciide_softc *sc;
   1845      1.18  drochner {
   1846      1.18  drochner 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
   1847      1.18  drochner 	    WDC_CAPABILITY_DMA;
   1848      1.27    bouyer 	sc->sc_wdcdev.PIO_cap = 4;
   1849      1.27    bouyer 	sc->sc_wdcdev.DMA_cap = 2;
   1850      1.28    bouyer 	sc->sc_wdcdev.set_modes = cy693_setup_channel;
   1851      1.18  drochner }
   1852      1.18  drochner 
   1853      1.18  drochner void
   1854      1.28    bouyer cy693_setup_chip(sc)
   1855       1.9    bouyer 	struct pciide_softc *sc;
   1856       1.1       cgd {
   1857      1.28    bouyer 	WDCDEBUG_PRINT(("cy693_setup_chip: old timings reg 0x%x\n",
   1858      1.28    bouyer 		pci_conf_read(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL)),
   1859      1.28    bouyer 		DEBUG_PROBE);
   1860      1.28    bouyer 	cy693_setup_channel(&sc->pciide_channels[0].wdc_channel);
   1861      1.28    bouyer 	WDCDEBUG_PRINT(("cy693_setup_chip: new timings reg 0x%x\n",
   1862      1.28    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL)), DEBUG_PROBE);
   1863      1.28    bouyer }
   1864      1.28    bouyer 
   1865      1.28    bouyer void
   1866      1.28    bouyer cy693_setup_channel(chp)
   1867      1.18  drochner 	struct channel_softc *chp;
   1868      1.28    bouyer {
   1869      1.18  drochner 	struct ata_drive_datas *drvp;
   1870      1.18  drochner 	int drive;
   1871      1.18  drochner 	u_int32_t cy_cmd_ctrl;
   1872      1.18  drochner 	u_int32_t idedma_ctl;
   1873      1.28    bouyer 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   1874      1.28    bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1875       1.9    bouyer 
   1876      1.18  drochner 	cy_cmd_ctrl = idedma_ctl = 0;
   1877      1.28    bouyer 
   1878      1.28    bouyer 	/* setup DMA if needed */
   1879      1.28    bouyer 	pciide_channel_dma_setup(cp);
   1880      1.28    bouyer 
   1881      1.18  drochner 	for (drive = 0; drive < 2; drive++) {
   1882      1.18  drochner 		drvp = &chp->ch_drive[drive];
   1883      1.18  drochner 		/* If no drive, skip */
   1884      1.18  drochner 		if ((drvp->drive_flags & DRIVE) == 0)
   1885      1.18  drochner 			continue;
   1886      1.18  drochner 		/* add timing values, setup DMA if needed */
   1887      1.28    bouyer 		if (drvp->drive_flags & DRIVE_DMA) {
   1888      1.28    bouyer 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1889      1.28    bouyer 			/*
   1890      1.28    bouyer 			 * use Multiword DMA
   1891      1.28    bouyer 			 * Timings will be used for both PIO and DMA, so adjust
   1892      1.28    bouyer 			 * DMA mode if needed
   1893      1.28    bouyer 			 */
   1894      1.28    bouyer 			if (drvp->PIO_mode > (drvp->DMA_mode + 2))
   1895      1.28    bouyer 				drvp->PIO_mode = drvp->DMA_mode + 2;
   1896      1.28    bouyer 			if (drvp->DMA_mode == 0)
   1897      1.28    bouyer 				drvp->PIO_mode = 0;
   1898      1.18  drochner 		}
   1899      1.28    bouyer 		cy_cmd_ctrl |= (cy_pio_pulse[drvp->PIO_mode] <<
   1900      1.18  drochner 		    CY_CMD_CTRL_IOW_PULSE_OFF(drive));
   1901      1.18  drochner 		cy_cmd_ctrl |= (cy_pio_rec[drvp->PIO_mode] <<
   1902      1.18  drochner 		    CY_CMD_CTRL_IOW_REC_OFF(drive));
   1903      1.33    bouyer 		cy_cmd_ctrl |= (cy_pio_pulse[drvp->PIO_mode] <<
   1904      1.33    bouyer 		    CY_CMD_CTRL_IOR_PULSE_OFF(drive));
   1905      1.33    bouyer 		cy_cmd_ctrl |= (cy_pio_rec[drvp->PIO_mode] <<
   1906      1.33    bouyer 		    CY_CMD_CTRL_IOR_REC_OFF(drive));
   1907      1.18  drochner 	}
   1908      1.28    bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL, cy_cmd_ctrl);
   1909      1.28    bouyer 	pciide_print_modes(cp);
   1910      1.18  drochner 	if (idedma_ctl != 0) {
   1911      1.18  drochner 		/* Add software bits in status register */
   1912      1.18  drochner 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1913      1.18  drochner 		    IDEDMA_CTL, idedma_ctl);
   1914       1.9    bouyer 	}
   1915       1.1       cgd }
   1916       1.1       cgd 
   1917      1.18  drochner void
   1918      1.28    bouyer cy693_channel_map(pa, cp)
   1919      1.18  drochner 	struct pci_attach_args *pa;
   1920      1.18  drochner 	struct pciide_channel *cp;
   1921       1.1       cgd {
   1922      1.28    bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   1923      1.18  drochner 	bus_size_t cmdsize, ctlsize;
   1924      1.18  drochner 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   1925      1.18  drochner 	int interface =
   1926      1.28    bouyer 	    PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
   1927      1.18  drochner 	int compatchan;
   1928       1.9    bouyer 
   1929       1.9    bouyer #ifdef DIAGNOSTIC
   1930      1.18  drochner 	if (wdc_cp->channel != 0)
   1931      1.18  drochner 		panic("cy693_channel_map: channel %d", wdc_cp->channel);
   1932       1.9    bouyer #endif
   1933       1.9    bouyer 
   1934      1.18  drochner 	/*
   1935      1.18  drochner 	 * this chip has 2 PCI IDE functions, one for primary and one for
   1936      1.18  drochner 	 * secondary. So we need to call pciide_mapregs_compat() with
   1937      1.18  drochner 	 * the real channel
   1938      1.18  drochner 	 */
   1939      1.18  drochner 	if (pa->pa_function == 1) {
   1940      1.18  drochner 		compatchan = 0;
   1941      1.18  drochner 	} else if (pa->pa_function == 2) {
   1942      1.18  drochner 		compatchan = 1;
   1943      1.18  drochner 	} else {
   1944      1.18  drochner 		printf("%s: unexpected PCI function %d\n",
   1945      1.18  drochner 		    sc->sc_wdcdev.sc_dev.dv_xname, pa->pa_function);
   1946      1.18  drochner 		cp->hw_ok = 0;
   1947      1.18  drochner 		return;
   1948       1.9    bouyer 	}
   1949      1.18  drochner 
   1950      1.18  drochner 	/* Only one channel for this chip; if we are here it's enabled */
   1951      1.18  drochner 	if (interface & PCIIDE_INTERFACE_PCI(0))
   1952      1.28    bouyer 		cp->hw_ok = pciide_mapregs_native(pa, cp, &cmdsize, &ctlsize);
   1953      1.18  drochner 	else
   1954      1.28    bouyer 		cp->hw_ok = pciide_mapregs_compat(pa, cp, compatchan,
   1955      1.18  drochner 		    &cmdsize, &ctlsize);
   1956      1.18  drochner 	if (cp->hw_ok == 0)
   1957      1.18  drochner 		return;
   1958      1.18  drochner 	wdc_cp->data32iot = wdc_cp->cmd_iot;
   1959      1.18  drochner 	wdc_cp->data32ioh = wdc_cp->cmd_ioh;
   1960      1.18  drochner 	wdcattach(wdc_cp);
   1961      1.28    bouyer 	if (pciiide_chan_candisable(cp)) {
   1962      1.28    bouyer 		pci_conf_write(sc->sc_pc, sc->sc_tag,
   1963      1.18  drochner 		    PCI_COMMAND_STATUS_REG, 0);
   1964       1.9    bouyer 	}
   1965      1.28    bouyer 	pciide_map_compat_intr(pa, cp, compatchan, interface);
   1966       1.9    bouyer }
   1967       1.9    bouyer 
   1968       1.9    bouyer void
   1969      1.18  drochner sis_setup_cap(sc)
   1970      1.18  drochner 	struct pciide_softc *sc;
   1971       1.9    bouyer {
   1972      1.18  drochner 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
   1973      1.18  drochner 	    WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA;
   1974      1.27    bouyer 	sc->sc_wdcdev.PIO_cap = 4;
   1975      1.27    bouyer 	sc->sc_wdcdev.DMA_cap = 2;
   1976      1.27    bouyer 	sc->sc_wdcdev.UDMA_cap = 2;
   1977      1.28    bouyer 	sc->sc_wdcdev.set_modes = sis_setup_channel;
   1978      1.15    bouyer }
   1979      1.15    bouyer 
   1980      1.15    bouyer void
   1981      1.28    bouyer sis_setup_chip(sc)
   1982      1.15    bouyer 	struct pciide_softc *sc;
   1983      1.15    bouyer {
   1984      1.28    bouyer 	int channel;
   1985      1.28    bouyer 
   1986      1.28    bouyer 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   1987      1.28    bouyer 		sis_setup_channel(&sc->pciide_channels[channel].wdc_channel);
   1988      1.28    bouyer 	}
   1989      1.28    bouyer 	pciide_pci_write(sc->sc_pc, sc->sc_tag, SIS_MISC,
   1990      1.28    bouyer 	    pciide_pci_read(sc->sc_pc, sc->sc_tag, SIS_MISC) |
   1991      1.28    bouyer 	    SIS_MISC_TIM_SEL | SIS_MISC_FIFO_SIZE);
   1992      1.28    bouyer }
   1993      1.28    bouyer 
   1994      1.28    bouyer void
   1995      1.28    bouyer sis_setup_channel(chp)
   1996      1.15    bouyer 	struct channel_softc *chp;
   1997      1.28    bouyer {
   1998      1.15    bouyer 	struct ata_drive_datas *drvp;
   1999      1.28    bouyer 	int drive;
   2000      1.18  drochner 	u_int32_t sis_tim;
   2001      1.18  drochner 	u_int32_t idedma_ctl;
   2002      1.28    bouyer 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   2003      1.28    bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   2004      1.15    bouyer 
   2005      1.28    bouyer 	WDCDEBUG_PRINT(("sis_setup_chip: old timings reg for "
   2006      1.28    bouyer 	    "channel %d 0x%x\n", chp->channel,
   2007      1.28    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, SIS_TIM(chp->channel))),
   2008      1.28    bouyer 	    DEBUG_PROBE);
   2009      1.28    bouyer 	sis_tim = 0;
   2010      1.18  drochner 	idedma_ctl = 0;
   2011      1.28    bouyer 	/* setup DMA if needed */
   2012      1.28    bouyer 	pciide_channel_dma_setup(cp);
   2013      1.28    bouyer 
   2014      1.28    bouyer 	for (drive = 0; drive < 2; drive++) {
   2015      1.28    bouyer 		drvp = &chp->ch_drive[drive];
   2016      1.28    bouyer 		/* If no drive, skip */
   2017      1.28    bouyer 		if ((drvp->drive_flags & DRIVE) == 0)
   2018      1.28    bouyer 			continue;
   2019      1.28    bouyer 		/* add timing values, setup DMA if needed */
   2020      1.28    bouyer 		if ((drvp->drive_flags & DRIVE_DMA) == 0 &&
   2021      1.28    bouyer 		    (drvp->drive_flags & DRIVE_UDMA) == 0)
   2022      1.28    bouyer 			goto pio;
   2023      1.28    bouyer 
   2024      1.28    bouyer 		if (drvp->drive_flags & DRIVE_UDMA) {
   2025      1.28    bouyer 			/* use Ultra/DMA */
   2026      1.28    bouyer 			drvp->drive_flags &= ~DRIVE_DMA;
   2027      1.28    bouyer 			sis_tim |= sis_udma_tim[drvp->UDMA_mode] <<
   2028      1.28    bouyer 			    SIS_TIM_UDMA_TIME_OFF(drive);
   2029      1.28    bouyer 			sis_tim |= SIS_TIM_UDMA_EN(drive);
   2030      1.28    bouyer 		} else {
   2031      1.28    bouyer 			/*
   2032      1.28    bouyer 			 * use Multiword DMA
   2033      1.28    bouyer 			 * Timings will be used for both PIO and DMA,
   2034      1.28    bouyer 			 * so adjust DMA mode if needed
   2035      1.28    bouyer 			 */
   2036      1.28    bouyer 			if (drvp->PIO_mode > (drvp->DMA_mode + 2))
   2037      1.28    bouyer 				drvp->PIO_mode = drvp->DMA_mode + 2;
   2038      1.32    bouyer 			if (drvp->DMA_mode + 2 > (drvp->PIO_mode))
   2039      1.32    bouyer 				drvp->DMA_mode = (drvp->PIO_mode > 2) ?
   2040      1.32    bouyer 				    drvp->PIO_mode - 2 : 0;
   2041      1.28    bouyer 			if (drvp->DMA_mode == 0)
   2042      1.28    bouyer 				drvp->PIO_mode = 0;
   2043      1.28    bouyer 		}
   2044      1.28    bouyer 		idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   2045      1.28    bouyer pio:		sis_tim |= sis_pio_act[drvp->PIO_mode] <<
   2046      1.28    bouyer 		    SIS_TIM_ACT_OFF(drive);
   2047      1.28    bouyer 		sis_tim |= sis_pio_rec[drvp->PIO_mode] <<
   2048      1.28    bouyer 		    SIS_TIM_REC_OFF(drive);
   2049      1.28    bouyer 	}
   2050      1.28    bouyer 	WDCDEBUG_PRINT(("sis_setup_chip: new timings reg for "
   2051      1.28    bouyer 	    "channel %d 0x%x\n", chp->channel, sis_tim), DEBUG_PROBE);
   2052      1.28    bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, SIS_TIM(chp->channel), sis_tim);
   2053      1.18  drochner 	if (idedma_ctl != 0) {
   2054      1.18  drochner 		/* Add software bits in status register */
   2055      1.18  drochner 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   2056      1.18  drochner 		    IDEDMA_CTL, idedma_ctl);
   2057      1.18  drochner 	}
   2058      1.28    bouyer 	pciide_print_modes(cp);
   2059      1.18  drochner }
   2060      1.18  drochner 
   2061      1.18  drochner void
   2062      1.28    bouyer sis_channel_map(pa, cp)
   2063      1.18  drochner 	struct pci_attach_args *pa;
   2064      1.18  drochner 	struct pciide_channel *cp;
   2065      1.18  drochner {
   2066      1.28    bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   2067      1.18  drochner 	bus_size_t cmdsize, ctlsize;
   2068      1.18  drochner 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   2069      1.28    bouyer 	u_int8_t sis_ctr0 = pciide_pci_read(sc->sc_pc, sc->sc_tag, SIS_CTRL0);
   2070      1.18  drochner 	int interface =
   2071      1.28    bouyer 	    PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
   2072      1.18  drochner 
   2073      1.18  drochner 	if ((wdc_cp->channel == 0 && (sis_ctr0 & SIS_CTRL0_CHAN0_EN) == 0) ||
   2074      1.18  drochner 	    (wdc_cp->channel == 1 && (sis_ctr0 & SIS_CTRL0_CHAN1_EN) == 0)) {
   2075      1.18  drochner 		printf("%s: %s channel ignored (disabled)\n",
   2076      1.18  drochner 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   2077      1.18  drochner 		return;
   2078      1.18  drochner 	}
   2079      1.18  drochner 
   2080      1.28    bouyer 	pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize);
   2081      1.18  drochner 	if (cp->hw_ok == 0)
   2082      1.18  drochner 		return;
   2083      1.28    bouyer 	if (pciiide_chan_candisable(cp)) {
   2084      1.18  drochner 		if (wdc_cp->channel == 0)
   2085      1.18  drochner 			sis_ctr0 &= ~SIS_CTRL0_CHAN0_EN;
   2086      1.18  drochner 		else
   2087      1.18  drochner 			sis_ctr0 &= ~SIS_CTRL0_CHAN1_EN;
   2088      1.28    bouyer 		pciide_pci_write(sc->sc_pc, sc->sc_tag, SIS_CTRL0, sis_ctr0);
   2089      1.30    bouyer 	}
   2090      1.30    bouyer 	pciide_map_compat_intr(pa, cp, wdc_cp->channel, interface);
   2091      1.30    bouyer }
   2092      1.30    bouyer 
   2093      1.30    bouyer void
   2094      1.30    bouyer acer_setup_cap(sc)
   2095      1.30    bouyer 	struct pciide_softc *sc;
   2096      1.30    bouyer {
   2097      1.30    bouyer 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
   2098      1.30    bouyer 	    WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA;
   2099      1.30    bouyer 	sc->sc_wdcdev.PIO_cap = 4;
   2100      1.30    bouyer 	sc->sc_wdcdev.DMA_cap = 2;
   2101      1.30    bouyer 	sc->sc_wdcdev.UDMA_cap = 2;
   2102      1.30    bouyer 	sc->sc_wdcdev.set_modes = acer_setup_channel;
   2103      1.30    bouyer }
   2104      1.30    bouyer 
   2105      1.30    bouyer void
   2106      1.30    bouyer acer_setup_chip(sc)
   2107      1.30    bouyer 	struct pciide_softc *sc;
   2108      1.30    bouyer {
   2109      1.30    bouyer 	int channel;
   2110      1.30    bouyer 
   2111      1.30    bouyer 	pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CDRC,
   2112      1.30    bouyer 	    (pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CDRC) |
   2113      1.30    bouyer 		ACER_CDRC_DMA_EN) & ~ACER_CDRC_FIFO_DISABLE);
   2114      1.30    bouyer 
   2115      1.30    bouyer 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
   2116      1.30    bouyer 		acer_setup_channel(&sc->pciide_channels[channel].wdc_channel);
   2117      1.30    bouyer 	}
   2118      1.30    bouyer }
   2119      1.30    bouyer 
   2120      1.30    bouyer void
   2121      1.30    bouyer acer_setup_channel(chp)
   2122      1.30    bouyer 	struct channel_softc *chp;
   2123      1.30    bouyer {
   2124      1.30    bouyer 	struct ata_drive_datas *drvp;
   2125      1.30    bouyer 	int drive;
   2126      1.30    bouyer 	u_int32_t acer_fifo_udma;
   2127      1.30    bouyer 	u_int32_t idedma_ctl;
   2128      1.30    bouyer 	struct pciide_channel *cp = (struct pciide_channel*)chp;
   2129      1.30    bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   2130      1.30    bouyer 
   2131      1.30    bouyer 	idedma_ctl = 0;
   2132      1.30    bouyer 	acer_fifo_udma = pci_conf_read(sc->sc_pc, sc->sc_tag, ACER_FTH_UDMA);
   2133      1.30    bouyer 	WDCDEBUG_PRINT(("acer_setup_chip: old fifo/udma reg 0x%x\n",
   2134      1.30    bouyer 	    acer_fifo_udma), DEBUG_PROBE);
   2135      1.30    bouyer 	/* setup DMA if needed */
   2136      1.30    bouyer 	pciide_channel_dma_setup(cp);
   2137      1.30    bouyer 
   2138      1.30    bouyer 	for (drive = 0; drive < 2; drive++) {
   2139      1.30    bouyer 		drvp = &chp->ch_drive[drive];
   2140      1.30    bouyer 		/* If no drive, skip */
   2141      1.30    bouyer 		if ((drvp->drive_flags & DRIVE) == 0)
   2142      1.30    bouyer 			continue;
   2143      1.30    bouyer 		WDCDEBUG_PRINT(("acer_setup_chip: old timings reg for "
   2144      1.30    bouyer 		    "channel %d drive %d 0x%x\n", chp->channel, drive,
   2145      1.30    bouyer 		    pciide_pci_read(sc->sc_pc, sc->sc_tag,
   2146      1.30    bouyer 		    ACER_IDETIM(chp->channel, drive))), DEBUG_PROBE);
   2147      1.30    bouyer 		/* clear FIFO/DMA mode */
   2148      1.30    bouyer 		acer_fifo_udma &= ~(ACER_FTH_OPL(chp->channel, drive, 0x3) |
   2149      1.30    bouyer 		    ACER_UDMA_EN(chp->channel, drive) |
   2150      1.30    bouyer 		    ACER_UDMA_TIM(chp->channel, drive, 0x7));
   2151      1.30    bouyer 
   2152      1.30    bouyer 		/* add timing values, setup DMA if needed */
   2153      1.30    bouyer 		if ((drvp->drive_flags & DRIVE_DMA) == 0 &&
   2154      1.30    bouyer 		    (drvp->drive_flags & DRIVE_UDMA) == 0) {
   2155      1.30    bouyer 			acer_fifo_udma |=
   2156      1.30    bouyer 			    ACER_FTH_OPL(chp->channel, drive, 0x1);
   2157      1.30    bouyer 			goto pio;
   2158      1.30    bouyer 		}
   2159      1.30    bouyer 
   2160      1.30    bouyer 		acer_fifo_udma |= ACER_FTH_OPL(chp->channel, drive, 0x2);
   2161      1.30    bouyer 		if (drvp->drive_flags & DRIVE_UDMA) {
   2162      1.30    bouyer 			/* use Ultra/DMA */
   2163      1.30    bouyer 			drvp->drive_flags &= ~DRIVE_DMA;
   2164      1.30    bouyer 			acer_fifo_udma |= ACER_UDMA_EN(chp->channel, drive);
   2165      1.30    bouyer 			acer_fifo_udma |=
   2166      1.30    bouyer 			    ACER_UDMA_TIM(chp->channel, drive,
   2167      1.30    bouyer 				acer_udma[drvp->UDMA_mode]);
   2168      1.30    bouyer 		} else {
   2169      1.30    bouyer 			/*
   2170      1.30    bouyer 			 * use Multiword DMA
   2171      1.30    bouyer 			 * Timings will be used for both PIO and DMA,
   2172      1.30    bouyer 			 * so adjust DMA mode if needed
   2173      1.30    bouyer 			 */
   2174      1.30    bouyer 			if (drvp->PIO_mode > (drvp->DMA_mode + 2))
   2175      1.30    bouyer 				drvp->PIO_mode = drvp->DMA_mode + 2;
   2176      1.32    bouyer 			if (drvp->DMA_mode + 2 > (drvp->PIO_mode))
   2177      1.32    bouyer 				drvp->DMA_mode = (drvp->PIO_mode > 2) ?
   2178      1.32    bouyer 				    drvp->PIO_mode - 2 : 0;
   2179      1.30    bouyer 			if (drvp->DMA_mode == 0)
   2180      1.30    bouyer 				drvp->PIO_mode = 0;
   2181      1.30    bouyer 		}
   2182      1.30    bouyer 		idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   2183      1.30    bouyer pio:		pciide_pci_write(sc->sc_pc, sc->sc_tag,
   2184      1.30    bouyer 		    ACER_IDETIM(chp->channel, drive),
   2185      1.30    bouyer 		    acer_pio[drvp->PIO_mode]);
   2186      1.30    bouyer 	}
   2187      1.32    bouyer 	WDCDEBUG_PRINT(("acer_setup_chip: new fifo/udma reg 0x%x\n",
   2188      1.30    bouyer 	    acer_fifo_udma), DEBUG_PROBE);
   2189      1.30    bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, ACER_FTH_UDMA, acer_fifo_udma);
   2190      1.30    bouyer 	if (idedma_ctl != 0) {
   2191      1.30    bouyer 		/* Add software bits in status register */
   2192      1.30    bouyer 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   2193      1.30    bouyer 		    IDEDMA_CTL, idedma_ctl);
   2194      1.30    bouyer 	}
   2195      1.30    bouyer 	pciide_print_modes(cp);
   2196      1.30    bouyer }
   2197      1.30    bouyer 
   2198      1.30    bouyer void
   2199      1.30    bouyer acer_channel_map(pa, cp)
   2200      1.30    bouyer 	struct pci_attach_args *pa;
   2201      1.30    bouyer 	struct pciide_channel *cp;
   2202      1.30    bouyer {
   2203      1.30    bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
   2204      1.30    bouyer 	bus_size_t cmdsize, ctlsize;
   2205      1.30    bouyer 	struct channel_softc *wdc_cp = &cp->wdc_channel;
   2206      1.31    bouyer 	u_int32_t cr;
   2207      1.31    bouyer 	int interface;
   2208      1.31    bouyer 
   2209      1.31    bouyer 	/*
   2210      1.31    bouyer 	 * Enable "microsoft register bits" R/W. Will be done 2 times
   2211      1.31    bouyer 	 * (one for each channel) but should'nt be a problem. There's no
   2212      1.31    bouyer 	 * better place where to put this.
   2213      1.31    bouyer 	 */
   2214      1.31    bouyer 	pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR3,
   2215      1.31    bouyer 	    pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR3) | ACER_CCAR3_PI);
   2216      1.31    bouyer 	pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR1,
   2217      1.31    bouyer 	    pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR1) &
   2218      1.31    bouyer 	    ~(ACER_CHANSTATUS_RO|PCIIDE_CHAN_RO(0)|PCIIDE_CHAN_RO(1)));
   2219      1.31    bouyer 	pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR2,
   2220      1.31    bouyer 	    pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR2) &
   2221      1.31    bouyer 	    ~ACER_CHANSTATUSREGS_RO);
   2222      1.31    bouyer 	cr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG);
   2223      1.31    bouyer 	cr |= (PCIIDE_CHANSTATUS_EN << PCI_INTERFACE_SHIFT);
   2224      1.31    bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG, cr);
   2225      1.31    bouyer 	/* Don't use cr, re-read the real register content instead */
   2226      1.31    bouyer 	interface = PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag,
   2227      1.31    bouyer 	    PCI_CLASS_REG));
   2228      1.30    bouyer 
   2229      1.30    bouyer 	if ((interface & PCIIDE_CHAN_EN(wdc_cp->channel)) == 0) {
   2230      1.30    bouyer 		printf("%s: %s channel ignored (disabled)\n",
   2231      1.30    bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
   2232      1.30    bouyer 		return;
   2233      1.30    bouyer 	}
   2234      1.30    bouyer 
   2235      1.30    bouyer 	pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize);
   2236      1.30    bouyer 	if (cp->hw_ok == 0)
   2237      1.30    bouyer 		return;
   2238      1.30    bouyer 	if (pciiide_chan_candisable(cp)) {
   2239      1.30    bouyer 		cr &= ~(PCIIDE_CHAN_EN(wdc_cp->channel) << PCI_INTERFACE_SHIFT);
   2240      1.30    bouyer 		pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG, cr);
   2241      1.15    bouyer 	}
   2242      1.28    bouyer 	pciide_map_compat_intr(pa, cp, wdc_cp->channel, interface);
   2243       1.1       cgd }
   2244