Home | History | Annotate | Line # | Download | only in pci
cmdide.c revision 1.27.40.1
      1  1.27.40.1       mjf /*	$NetBSD: cmdide.c,v 1.27.40.1 2008/04/03 12:42:49 mjf Exp $	*/
      2        1.1    bouyer 
      3        1.1    bouyer /*
      4        1.1    bouyer  * Copyright (c) 1999, 2000, 2001 Manuel Bouyer.
      5        1.1    bouyer  *
      6        1.1    bouyer  * Redistribution and use in source and binary forms, with or without
      7        1.1    bouyer  * modification, are permitted provided that the following conditions
      8        1.1    bouyer  * are met:
      9        1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     10        1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     11        1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     12        1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     13        1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     14        1.1    bouyer  * 3. All advertising materials mentioning features or use of this software
     15        1.1    bouyer  *    must display the following acknowledgement:
     16        1.1    bouyer  *	This product includes software developed by Manuel Bouyer.
     17        1.1    bouyer  * 4. The name of the author may not be used to endorse or promote products
     18        1.1    bouyer  *    derived from this software without specific prior written permission.
     19        1.1    bouyer  *
     20        1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21        1.1    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22        1.1    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23       1.18     perry  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24        1.1    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25        1.1    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26        1.1    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27        1.1    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28        1.1    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29        1.1    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30        1.1    bouyer  */
     31        1.1    bouyer 
     32       1.19     lukem #include <sys/cdefs.h>
     33  1.27.40.1       mjf __KERNEL_RCSID(0, "$NetBSD: cmdide.c,v 1.27.40.1 2008/04/03 12:42:49 mjf Exp $");
     34       1.19     lukem 
     35        1.1    bouyer #include <sys/param.h>
     36        1.1    bouyer #include <sys/systm.h>
     37        1.1    bouyer #include <sys/malloc.h>
     38        1.1    bouyer 
     39        1.1    bouyer #include <dev/pci/pcivar.h>
     40        1.1    bouyer #include <dev/pci/pcidevs.h>
     41        1.1    bouyer #include <dev/pci/pciidereg.h>
     42        1.1    bouyer #include <dev/pci/pciidevar.h>
     43        1.1    bouyer #include <dev/pci/pciide_cmd_reg.h>
     44        1.1    bouyer 
     45        1.1    bouyer 
     46  1.27.40.1       mjf static int  cmdide_match(device_t, cfdata_t, void *);
     47  1.27.40.1       mjf static void cmdide_attach(device_t, device_t, void *);
     48        1.1    bouyer 
     49  1.27.40.1       mjf CFATTACH_DECL_NEW(cmdide, sizeof(struct pciide_softc),
     50        1.1    bouyer     cmdide_match, cmdide_attach, NULL, NULL);
     51        1.1    bouyer 
     52        1.2   thorpej static void cmd_chip_map(struct pciide_softc*, struct pci_attach_args*);
     53        1.2   thorpej static void cmd0643_9_chip_map(struct pciide_softc*, struct pci_attach_args*);
     54       1.14   thorpej static void cmd0643_9_setup_channel(struct ata_channel*);
     55        1.2   thorpej static void cmd_channel_map(struct pci_attach_args *, struct pciide_softc *,
     56        1.2   thorpej 			    int);
     57        1.2   thorpej static int  cmd_pci_intr(void *);
     58       1.14   thorpej static void cmd646_9_irqack(struct ata_channel *);
     59        1.2   thorpej static void cmd680_chip_map(struct pciide_softc*, struct pci_attach_args*);
     60       1.14   thorpej static void cmd680_setup_channel(struct ata_channel*);
     61        1.2   thorpej static void cmd680_channel_map(struct pci_attach_args *, struct pciide_softc *,
     62        1.2   thorpej 			       int);
     63        1.1    bouyer 
     64        1.2   thorpej static const struct pciide_product_desc pciide_cmd_products[] =  {
     65        1.1    bouyer 	{ PCI_PRODUCT_CMDTECH_640,
     66        1.1    bouyer 	  0,
     67        1.1    bouyer 	  "CMD Technology PCI0640",
     68        1.1    bouyer 	  cmd_chip_map
     69        1.1    bouyer 	},
     70        1.1    bouyer 	{ PCI_PRODUCT_CMDTECH_643,
     71        1.1    bouyer 	  0,
     72        1.1    bouyer 	  "CMD Technology PCI0643",
     73        1.1    bouyer 	  cmd0643_9_chip_map,
     74        1.1    bouyer 	},
     75        1.1    bouyer 	{ PCI_PRODUCT_CMDTECH_646,
     76        1.1    bouyer 	  0,
     77        1.1    bouyer 	  "CMD Technology PCI0646",
     78        1.1    bouyer 	  cmd0643_9_chip_map,
     79        1.1    bouyer 	},
     80        1.1    bouyer 	{ PCI_PRODUCT_CMDTECH_648,
     81        1.3   mycroft 	  0,
     82        1.1    bouyer 	  "CMD Technology PCI0648",
     83        1.1    bouyer 	  cmd0643_9_chip_map,
     84        1.1    bouyer 	},
     85        1.1    bouyer 	{ PCI_PRODUCT_CMDTECH_649,
     86        1.3   mycroft 	  0,
     87        1.1    bouyer 	  "CMD Technology PCI0649",
     88        1.1    bouyer 	  cmd0643_9_chip_map,
     89        1.1    bouyer 	},
     90        1.1    bouyer 	{ PCI_PRODUCT_CMDTECH_680,
     91        1.3   mycroft 	  0,
     92        1.1    bouyer 	  "Silicon Image 0680",
     93        1.1    bouyer 	  cmd680_chip_map,
     94        1.1    bouyer 	},
     95        1.1    bouyer 	{ 0,
     96        1.1    bouyer 	  0,
     97        1.1    bouyer 	  NULL,
     98        1.1    bouyer 	  NULL
     99        1.1    bouyer 	}
    100        1.1    bouyer };
    101        1.1    bouyer 
    102        1.2   thorpej static int
    103  1.27.40.1       mjf cmdide_match(device_t parent, cfdata_t match, void *aux)
    104        1.1    bouyer {
    105        1.1    bouyer 	struct pci_attach_args *pa = aux;
    106        1.1    bouyer 
    107        1.1    bouyer 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_CMDTECH) {
    108        1.1    bouyer 		if (pciide_lookup_product(pa->pa_id, pciide_cmd_products))
    109        1.1    bouyer 			return (2);
    110        1.1    bouyer 	}
    111        1.1    bouyer 	return (0);
    112        1.1    bouyer }
    113        1.1    bouyer 
    114        1.2   thorpej static void
    115  1.27.40.1       mjf cmdide_attach(device_t parent, device_t self, void *aux)
    116        1.1    bouyer {
    117        1.1    bouyer 	struct pci_attach_args *pa = aux;
    118  1.27.40.1       mjf 	struct pciide_softc *sc = device_private(self);
    119  1.27.40.1       mjf 
    120  1.27.40.1       mjf 	sc->sc_wdcdev.sc_atac.atac_dev = self;
    121        1.1    bouyer 
    122        1.1    bouyer 	pciide_common_attach(sc, pa,
    123        1.1    bouyer 	    pciide_lookup_product(pa->pa_id, pciide_cmd_products));
    124        1.1    bouyer 
    125        1.1    bouyer }
    126        1.1    bouyer 
    127        1.2   thorpej static void
    128        1.2   thorpej cmd_channel_map(struct pci_attach_args *pa, struct pciide_softc *sc,
    129        1.2   thorpej     int channel)
    130        1.1    bouyer {
    131        1.1    bouyer 	struct pciide_channel *cp = &sc->pciide_channels[channel];
    132        1.1    bouyer 	bus_size_t cmdsize, ctlsize;
    133        1.1    bouyer 	u_int8_t ctrl = pciide_pci_read(sc->sc_pc, sc->sc_tag, CMD_CTRL);
    134        1.1    bouyer 	int interface, one_channel;
    135        1.1    bouyer 
    136       1.18     perry 	/*
    137        1.1    bouyer 	 * The 0648/0649 can be told to identify as a RAID controller.
    138        1.1    bouyer 	 * In this case, we have to fake interface
    139        1.1    bouyer 	 */
    140        1.1    bouyer 	if (PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_MASS_STORAGE_IDE) {
    141        1.1    bouyer 		interface = PCIIDE_INTERFACE_SETTABLE(0) |
    142        1.1    bouyer 		    PCIIDE_INTERFACE_SETTABLE(1);
    143        1.1    bouyer 		if (pciide_pci_read(pa->pa_pc, pa->pa_tag, CMD_CONF) &
    144        1.1    bouyer 		    CMD_CONF_DSA1)
    145        1.1    bouyer 			interface |= PCIIDE_INTERFACE_PCI(0) |
    146        1.1    bouyer 			    PCIIDE_INTERFACE_PCI(1);
    147        1.1    bouyer 	} else {
    148        1.1    bouyer 		interface = PCI_INTERFACE(pa->pa_class);
    149        1.1    bouyer 	}
    150        1.1    bouyer 
    151       1.14   thorpej 	sc->wdc_chanarray[channel] = &cp->ata_channel;
    152        1.1    bouyer 	cp->name = PCIIDE_CHANNEL_NAME(channel);
    153       1.14   thorpej 	cp->ata_channel.ch_channel = channel;
    154       1.16   thorpej 	cp->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
    155        1.1    bouyer 
    156        1.1    bouyer 	/*
    157       1.26       wiz 	 * Older CMD64X doesn't have independent channels
    158        1.1    bouyer 	 */
    159        1.1    bouyer 	switch (sc->sc_pp->ide_product) {
    160        1.1    bouyer 	case PCI_PRODUCT_CMDTECH_649:
    161        1.1    bouyer 		one_channel = 0;
    162        1.1    bouyer 		break;
    163        1.1    bouyer 	default:
    164        1.1    bouyer 		one_channel = 1;
    165        1.1    bouyer 		break;
    166        1.1    bouyer 	}
    167        1.1    bouyer 
    168        1.1    bouyer 	if (channel > 0 && one_channel) {
    169       1.14   thorpej 		cp->ata_channel.ch_queue =
    170       1.14   thorpej 		    sc->pciide_channels[0].ata_channel.ch_queue;
    171        1.1    bouyer 	} else {
    172       1.14   thorpej 		cp->ata_channel.ch_queue =
    173        1.8   thorpej 		    malloc(sizeof(struct ata_queue), M_DEVBUF, M_NOWAIT);
    174        1.1    bouyer 	}
    175       1.14   thorpej 	if (cp->ata_channel.ch_queue == NULL) {
    176        1.1    bouyer 		aprint_error("%s %s channel: "
    177        1.1    bouyer 		    "can't allocate memory for command queue",
    178  1.27.40.1       mjf 		    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), cp->name);
    179        1.1    bouyer 		    return;
    180        1.1    bouyer 	}
    181       1.22    bouyer 	cp->ata_channel.ch_ndrive = 2;
    182        1.1    bouyer 
    183  1.27.40.1       mjf 	aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    184  1.27.40.1       mjf 	    "%s channel %s to %s mode\n", cp->name,
    185        1.1    bouyer 	    (interface & PCIIDE_INTERFACE_SETTABLE(channel)) ?
    186        1.1    bouyer 	    "configured" : "wired",
    187        1.1    bouyer 	    (interface & PCIIDE_INTERFACE_PCI(channel)) ?
    188        1.1    bouyer 	    "native-PCI" : "compatibility");
    189        1.1    bouyer 
    190        1.1    bouyer 	/*
    191        1.1    bouyer 	 * with a CMD PCI64x, if we get here, the first channel is enabled:
    192        1.1    bouyer 	 * there's no way to disable the first channel without disabling
    193        1.1    bouyer 	 * the whole device
    194        1.1    bouyer 	 */
    195        1.1    bouyer 	if (channel != 0 && (ctrl & CMD_CTRL_2PORT) == 0) {
    196  1.27.40.1       mjf 		aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    197  1.27.40.1       mjf 		    "%s channel ignored (disabled)\n", cp->name);
    198       1.14   thorpej 		cp->ata_channel.ch_flags |= ATACH_DISABLED;
    199        1.1    bouyer 		return;
    200        1.1    bouyer 	}
    201        1.1    bouyer 
    202        1.1    bouyer 	pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize, cmd_pci_intr);
    203        1.1    bouyer }
    204        1.1    bouyer 
    205        1.2   thorpej static int
    206        1.2   thorpej cmd_pci_intr(void *arg)
    207        1.1    bouyer {
    208        1.1    bouyer 	struct pciide_softc *sc = arg;
    209        1.1    bouyer 	struct pciide_channel *cp;
    210       1.14   thorpej 	struct ata_channel *wdc_cp;
    211       1.18     perry 	int i, rv, crv;
    212        1.1    bouyer 	u_int32_t priirq, secirq;
    213        1.1    bouyer 
    214        1.1    bouyer 	rv = 0;
    215        1.1    bouyer 	priirq = pciide_pci_read(sc->sc_pc, sc->sc_tag, CMD_CONF);
    216        1.1    bouyer 	secirq = pciide_pci_read(sc->sc_pc, sc->sc_tag, CMD_ARTTIM23);
    217       1.16   thorpej 	for (i = 0; i < sc->sc_wdcdev.sc_atac.atac_nchannels; i++) {
    218        1.1    bouyer 		cp = &sc->pciide_channels[i];
    219       1.14   thorpej 		wdc_cp = &cp->ata_channel;
    220        1.1    bouyer 		/* If a compat channel skip. */
    221        1.1    bouyer 		if (cp->compat)
    222        1.1    bouyer 			continue;
    223        1.1    bouyer 		if ((i == 0 && (priirq & CMD_CONF_DRV0_INTR)) ||
    224        1.1    bouyer 		    (i == 1 && (secirq & CMD_ARTTIM23_IRQ))) {
    225        1.1    bouyer 			crv = wdcintr(wdc_cp);
    226       1.11    bouyer 			if (crv == 0) {
    227  1.27.40.1       mjf 				aprint_error("%s:%d: bogus intr\n",
    228  1.27.40.1       mjf 				    device_xname(
    229  1.27.40.1       mjf 				      sc->sc_wdcdev.sc_atac.atac_dev), i);
    230       1.11    bouyer 				sc->sc_wdcdev.irqack(wdc_cp);
    231       1.11    bouyer 			} else
    232        1.1    bouyer 				rv = 1;
    233        1.1    bouyer 		}
    234        1.1    bouyer 	}
    235        1.1    bouyer 	return rv;
    236        1.1    bouyer }
    237        1.1    bouyer 
    238        1.2   thorpej static void
    239        1.2   thorpej cmd_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
    240        1.1    bouyer {
    241        1.1    bouyer 	int channel;
    242        1.1    bouyer 
    243        1.1    bouyer 	/*
    244        1.1    bouyer 	 * For a CMD PCI064x, the use of PCI_COMMAND_IO_ENABLE
    245        1.5       wiz 	 * and base addresses registers can be disabled at
    246        1.1    bouyer 	 * hardware level. In this case, the device is wired
    247        1.1    bouyer 	 * in compat mode and its first channel is always enabled,
    248        1.1    bouyer 	 * but we can't rely on PCI_COMMAND_IO_ENABLE.
    249        1.1    bouyer 	 * In fact, it seems that the first channel of the CMD PCI0640
    250        1.1    bouyer 	 * can't be disabled.
    251        1.1    bouyer 	 */
    252        1.1    bouyer 
    253        1.1    bouyer #ifdef PCIIDE_CMD064x_DISABLE
    254        1.1    bouyer 	if (pciide_chipen(sc, pa) == 0)
    255        1.1    bouyer 		return;
    256        1.1    bouyer #endif
    257        1.1    bouyer 
    258  1.27.40.1       mjf 	aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    259  1.27.40.1       mjf 	    "hardware does not support DMA\n");
    260        1.1    bouyer 	sc->sc_dma_ok = 0;
    261        1.1    bouyer 
    262       1.16   thorpej 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
    263       1.16   thorpej 	sc->sc_wdcdev.sc_atac.atac_nchannels = PCIIDE_NUM_CHANNELS;
    264       1.16   thorpej 	sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16;
    265        1.1    bouyer 
    266       1.14   thorpej 	wdc_allocate_regs(&sc->sc_wdcdev);
    267       1.14   thorpej 
    268       1.16   thorpej 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
    269       1.16   thorpej 	     channel++) {
    270        1.1    bouyer 		cmd_channel_map(pa, sc, channel);
    271        1.1    bouyer 	}
    272        1.1    bouyer }
    273        1.1    bouyer 
    274        1.2   thorpej static void
    275        1.2   thorpej cmd0643_9_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
    276       1.18     perry {
    277        1.1    bouyer 	int channel;
    278        1.1    bouyer 	pcireg_t rev = PCI_REVISION(pa->pa_class);
    279        1.1    bouyer 
    280        1.1    bouyer 	/*
    281        1.1    bouyer 	 * For a CMD PCI064x, the use of PCI_COMMAND_IO_ENABLE
    282        1.5       wiz 	 * and base addresses registers can be disabled at
    283        1.1    bouyer 	 * hardware level. In this case, the device is wired
    284        1.1    bouyer 	 * in compat mode and its first channel is always enabled,
    285        1.1    bouyer 	 * but we can't rely on PCI_COMMAND_IO_ENABLE.
    286        1.1    bouyer 	 * In fact, it seems that the first channel of the CMD PCI0640
    287        1.1    bouyer 	 * can't be disabled.
    288        1.1    bouyer 	 */
    289        1.1    bouyer 
    290        1.1    bouyer #ifdef PCIIDE_CMD064x_DISABLE
    291        1.1    bouyer 	if (pciide_chipen(sc, pa) == 0)
    292        1.1    bouyer 		return;
    293        1.1    bouyer #endif
    294        1.1    bouyer 
    295  1.27.40.1       mjf 	aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    296  1.27.40.1       mjf 	    "bus-master DMA support present");
    297        1.1    bouyer 	pciide_mapreg_dma(sc, pa);
    298       1.27        ad 	aprint_verbose("\n");
    299       1.16   thorpej 	sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
    300        1.1    bouyer 	if (sc->sc_dma_ok) {
    301       1.16   thorpej 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA;
    302        1.1    bouyer 		switch (sc->sc_pp->ide_product) {
    303        1.1    bouyer 		case PCI_PRODUCT_CMDTECH_649:
    304       1.16   thorpej 			sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_UDMA;
    305       1.16   thorpej 			sc->sc_wdcdev.sc_atac.atac_udma_cap = 5;
    306        1.1    bouyer 			sc->sc_wdcdev.irqack = cmd646_9_irqack;
    307        1.1    bouyer 			break;
    308        1.1    bouyer 		case PCI_PRODUCT_CMDTECH_648:
    309       1.16   thorpej 			sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_UDMA;
    310       1.16   thorpej 			sc->sc_wdcdev.sc_atac.atac_udma_cap = 4;
    311        1.1    bouyer 			sc->sc_wdcdev.irqack = cmd646_9_irqack;
    312        1.1    bouyer 			break;
    313        1.1    bouyer 		case PCI_PRODUCT_CMDTECH_646:
    314        1.1    bouyer 			if (rev >= CMD0646U2_REV) {
    315       1.16   thorpej 				sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_UDMA;
    316       1.16   thorpej 				sc->sc_wdcdev.sc_atac.atac_udma_cap = 2;
    317        1.1    bouyer 			} else if (rev >= CMD0646U_REV) {
    318        1.1    bouyer 			/*
    319        1.1    bouyer 			 * Linux's driver claims that the 646U is broken
    320        1.1    bouyer 			 * with UDMA. Only enable it if we know what we're
    321        1.1    bouyer 			 * doing
    322        1.1    bouyer 			 */
    323        1.1    bouyer #ifdef PCIIDE_CMD0646U_ENABLEUDMA
    324       1.16   thorpej 				sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_UDMA;
    325       1.16   thorpej 				sc->sc_wdcdev.sc_atac.atac_udma_cap = 2;
    326        1.1    bouyer #endif
    327        1.1    bouyer 				/* explicitly disable UDMA */
    328        1.1    bouyer 				pciide_pci_write(sc->sc_pc, sc->sc_tag,
    329        1.1    bouyer 				    CMD_UDMATIM(0), 0);
    330        1.1    bouyer 				pciide_pci_write(sc->sc_pc, sc->sc_tag,
    331        1.1    bouyer 				    CMD_UDMATIM(1), 0);
    332        1.1    bouyer 			}
    333        1.1    bouyer 			sc->sc_wdcdev.irqack = cmd646_9_irqack;
    334        1.1    bouyer 			break;
    335        1.1    bouyer 		default:
    336        1.1    bouyer 			sc->sc_wdcdev.irqack = pciide_irqack;
    337        1.1    bouyer 		}
    338        1.1    bouyer 	}
    339        1.1    bouyer 
    340       1.16   thorpej 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
    341       1.16   thorpej 	sc->sc_wdcdev.sc_atac.atac_nchannels = PCIIDE_NUM_CHANNELS;
    342       1.16   thorpej 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
    343       1.16   thorpej 	sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
    344       1.16   thorpej 	sc->sc_wdcdev.sc_atac.atac_set_modes = cmd0643_9_setup_channel;
    345        1.1    bouyer 
    346       1.13   thorpej 	ATADEBUG_PRINT(("cmd0643_9_chip_map: old timings reg 0x%x 0x%x\n",
    347        1.1    bouyer 		pci_conf_read(sc->sc_pc, sc->sc_tag, 0x54),
    348        1.1    bouyer 		pci_conf_read(sc->sc_pc, sc->sc_tag, 0x58)),
    349        1.1    bouyer 		DEBUG_PROBE);
    350        1.1    bouyer 
    351       1.14   thorpej 	wdc_allocate_regs(&sc->sc_wdcdev);
    352       1.14   thorpej 
    353       1.16   thorpej 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
    354       1.16   thorpej 	     channel++)
    355        1.1    bouyer 		cmd_channel_map(pa, sc, channel);
    356        1.4    simonb 
    357        1.1    bouyer 	/*
    358        1.1    bouyer 	 * note - this also makes sure we clear the irq disable and reset
    359        1.1    bouyer 	 * bits
    360        1.1    bouyer 	 */
    361        1.1    bouyer 	pciide_pci_write(sc->sc_pc, sc->sc_tag, CMD_DMA_MODE, CMD_DMA_MULTIPLE);
    362       1.13   thorpej 	ATADEBUG_PRINT(("cmd0643_9_chip_map: timings reg now 0x%x 0x%x\n",
    363        1.1    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, 0x54),
    364        1.1    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, 0x58)),
    365        1.1    bouyer 	    DEBUG_PROBE);
    366        1.1    bouyer }
    367        1.1    bouyer 
    368        1.2   thorpej static void
    369       1.14   thorpej cmd0643_9_setup_channel(struct ata_channel *chp)
    370        1.1    bouyer {
    371        1.1    bouyer 	struct ata_drive_datas *drvp;
    372        1.1    bouyer 	u_int8_t tim;
    373        1.1    bouyer 	u_int32_t idedma_ctl, udma_reg;
    374       1.17   thorpej 	int drive, s;
    375       1.15   thorpej 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
    376       1.15   thorpej 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    377        1.1    bouyer 
    378        1.1    bouyer 	idedma_ctl = 0;
    379        1.1    bouyer 	/* setup DMA if needed */
    380        1.1    bouyer 	pciide_channel_dma_setup(cp);
    381        1.1    bouyer 
    382        1.1    bouyer 	for (drive = 0; drive < 2; drive++) {
    383        1.1    bouyer 		drvp = &chp->ch_drive[drive];
    384        1.1    bouyer 		/* If no drive, skip */
    385        1.1    bouyer 		if ((drvp->drive_flags & DRIVE) == 0)
    386        1.1    bouyer 			continue;
    387        1.1    bouyer 		/* add timing values, setup DMA if needed */
    388        1.1    bouyer 		tim = cmd0643_9_data_tim_pio[drvp->PIO_mode];
    389        1.1    bouyer 		if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) {
    390        1.1    bouyer 			if (drvp->drive_flags & DRIVE_UDMA) {
    391        1.1    bouyer 				/* UltraDMA on a 646U2, 0648 or 0649 */
    392       1.17   thorpej 				s = splbio();
    393        1.1    bouyer 				drvp->drive_flags &= ~DRIVE_DMA;
    394       1.17   thorpej 				splx(s);
    395        1.1    bouyer 				udma_reg = pciide_pci_read(sc->sc_pc,
    396       1.10   thorpej 				    sc->sc_tag, CMD_UDMATIM(chp->ch_channel));
    397        1.1    bouyer 				if (drvp->UDMA_mode > 2 &&
    398        1.1    bouyer 				    (pciide_pci_read(sc->sc_pc, sc->sc_tag,
    399        1.1    bouyer 				    CMD_BICSR) &
    400       1.10   thorpej 				    CMD_BICSR_80(chp->ch_channel)) == 0)
    401        1.1    bouyer 					drvp->UDMA_mode = 2;
    402        1.1    bouyer 				if (drvp->UDMA_mode > 2)
    403        1.1    bouyer 					udma_reg &= ~CMD_UDMATIM_UDMA33(drive);
    404       1.18     perry 				else if (sc->sc_wdcdev.sc_atac.atac_udma_cap > 2)
    405        1.1    bouyer 					udma_reg |= CMD_UDMATIM_UDMA33(drive);
    406        1.1    bouyer 				udma_reg |= CMD_UDMATIM_UDMA(drive);
    407        1.1    bouyer 				udma_reg &= ~(CMD_UDMATIM_TIM_MASK <<
    408        1.1    bouyer 				    CMD_UDMATIM_TIM_OFF(drive));
    409        1.1    bouyer 				udma_reg |=
    410        1.1    bouyer 				    (cmd0646_9_tim_udma[drvp->UDMA_mode] <<
    411        1.1    bouyer 				    CMD_UDMATIM_TIM_OFF(drive));
    412        1.1    bouyer 				pciide_pci_write(sc->sc_pc, sc->sc_tag,
    413       1.10   thorpej 				    CMD_UDMATIM(chp->ch_channel), udma_reg);
    414        1.1    bouyer 			} else {
    415        1.1    bouyer 				/*
    416        1.1    bouyer 				 * use Multiword DMA.
    417        1.1    bouyer 				 * Timings will be used for both PIO and DMA,
    418        1.1    bouyer 				 * so adjust DMA mode if needed
    419        1.1    bouyer 				 * if we have a 0646U2/8/9, turn off UDMA
    420        1.1    bouyer 				 */
    421       1.16   thorpej 				if (sc->sc_wdcdev.sc_atac.atac_cap & ATAC_CAP_UDMA) {
    422        1.1    bouyer 					udma_reg = pciide_pci_read(sc->sc_pc,
    423        1.1    bouyer 					    sc->sc_tag,
    424       1.10   thorpej 					    CMD_UDMATIM(chp->ch_channel));
    425        1.1    bouyer 					udma_reg &= ~CMD_UDMATIM_UDMA(drive);
    426        1.1    bouyer 					pciide_pci_write(sc->sc_pc, sc->sc_tag,
    427       1.10   thorpej 					    CMD_UDMATIM(chp->ch_channel),
    428        1.1    bouyer 					    udma_reg);
    429        1.1    bouyer 				}
    430        1.1    bouyer 				if (drvp->PIO_mode >= 3 &&
    431        1.1    bouyer 				    (drvp->DMA_mode + 2) > drvp->PIO_mode) {
    432        1.1    bouyer 					drvp->DMA_mode = drvp->PIO_mode - 2;
    433        1.1    bouyer 				}
    434        1.1    bouyer 				tim = cmd0643_9_data_tim_dma[drvp->DMA_mode];
    435        1.1    bouyer 			}
    436        1.1    bouyer 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    437        1.1    bouyer 		}
    438        1.1    bouyer 		pciide_pci_write(sc->sc_pc, sc->sc_tag,
    439       1.10   thorpej 		    CMD_DATA_TIM(chp->ch_channel, drive), tim);
    440        1.1    bouyer 	}
    441        1.1    bouyer 	if (idedma_ctl != 0) {
    442        1.1    bouyer 		/* Add software bits in status register */
    443        1.6      fvdl 		bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
    444        1.1    bouyer 		    idedma_ctl);
    445        1.1    bouyer 	}
    446        1.1    bouyer }
    447        1.1    bouyer 
    448        1.2   thorpej static void
    449       1.14   thorpej cmd646_9_irqack(struct ata_channel *chp)
    450        1.1    bouyer {
    451        1.1    bouyer 	u_int32_t priirq, secirq;
    452       1.15   thorpej 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    453        1.1    bouyer 
    454       1.10   thorpej 	if (chp->ch_channel == 0) {
    455        1.1    bouyer 		priirq = pciide_pci_read(sc->sc_pc, sc->sc_tag, CMD_CONF);
    456        1.1    bouyer 		pciide_pci_write(sc->sc_pc, sc->sc_tag, CMD_CONF, priirq);
    457        1.1    bouyer 	} else {
    458        1.1    bouyer 		secirq = pciide_pci_read(sc->sc_pc, sc->sc_tag, CMD_ARTTIM23);
    459        1.1    bouyer 		pciide_pci_write(sc->sc_pc, sc->sc_tag, CMD_ARTTIM23, secirq);
    460        1.1    bouyer 	}
    461        1.1    bouyer 	pciide_irqack(chp);
    462        1.1    bouyer }
    463        1.1    bouyer 
    464        1.2   thorpej static void
    465        1.2   thorpej cmd680_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
    466       1.18     perry {
    467        1.1    bouyer 	int channel;
    468        1.1    bouyer 
    469        1.1    bouyer 	if (pciide_chipen(sc, pa) == 0)
    470        1.1    bouyer 		return;
    471        1.1    bouyer 
    472  1.27.40.1       mjf 	aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    473  1.27.40.1       mjf 	    "bus-master DMA support present");
    474        1.1    bouyer 	pciide_mapreg_dma(sc, pa);
    475       1.27        ad 	aprint_verbose("\n");
    476       1.16   thorpej 	sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
    477        1.1    bouyer 	if (sc->sc_dma_ok) {
    478       1.16   thorpej 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA;
    479       1.16   thorpej 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_UDMA;
    480       1.16   thorpej 		sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    481        1.1    bouyer 		sc->sc_wdcdev.irqack = pciide_irqack;
    482        1.1    bouyer 	}
    483        1.1    bouyer 
    484       1.16   thorpej 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
    485       1.16   thorpej 	sc->sc_wdcdev.sc_atac.atac_nchannels = PCIIDE_NUM_CHANNELS;
    486       1.16   thorpej 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
    487       1.16   thorpej 	sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
    488       1.16   thorpej 	sc->sc_wdcdev.sc_atac.atac_set_modes = cmd680_setup_channel;
    489        1.1    bouyer 
    490        1.1    bouyer 	pciide_pci_write(sc->sc_pc, sc->sc_tag, 0x80, 0x00);
    491        1.1    bouyer 	pciide_pci_write(sc->sc_pc, sc->sc_tag, 0x84, 0x00);
    492        1.1    bouyer 	pciide_pci_write(sc->sc_pc, sc->sc_tag, 0x8a,
    493        1.1    bouyer 	    pciide_pci_read(sc->sc_pc, sc->sc_tag, 0x8a) | 0x01);
    494       1.14   thorpej 
    495       1.14   thorpej 	wdc_allocate_regs(&sc->sc_wdcdev);
    496       1.14   thorpej 
    497       1.16   thorpej 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
    498       1.16   thorpej 	     channel++)
    499        1.1    bouyer 		cmd680_channel_map(pa, sc, channel);
    500        1.1    bouyer }
    501        1.1    bouyer 
    502        1.2   thorpej static void
    503        1.2   thorpej cmd680_channel_map(struct pci_attach_args *pa, struct pciide_softc *sc,
    504        1.2   thorpej     int channel)
    505        1.1    bouyer {
    506        1.1    bouyer 	struct pciide_channel *cp = &sc->pciide_channels[channel];
    507        1.1    bouyer 	bus_size_t cmdsize, ctlsize;
    508        1.1    bouyer 	int interface, i, reg;
    509        1.1    bouyer 	static const u_int8_t init_val[] =
    510        1.1    bouyer 	    {             0x8a, 0x32, 0x8a, 0x32, 0x8a, 0x32,
    511        1.1    bouyer 	      0x92, 0x43, 0x92, 0x43, 0x09, 0x40, 0x09, 0x40 };
    512        1.1    bouyer 
    513        1.1    bouyer 	if (PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_MASS_STORAGE_IDE) {
    514        1.1    bouyer 		interface = PCIIDE_INTERFACE_SETTABLE(0) |
    515        1.1    bouyer 		    PCIIDE_INTERFACE_SETTABLE(1);
    516        1.1    bouyer 		interface |= PCIIDE_INTERFACE_PCI(0) |
    517        1.1    bouyer 		    PCIIDE_INTERFACE_PCI(1);
    518        1.1    bouyer 	} else {
    519        1.1    bouyer 		interface = PCI_INTERFACE(pa->pa_class);
    520        1.1    bouyer 	}
    521        1.1    bouyer 
    522       1.14   thorpej 	sc->wdc_chanarray[channel] = &cp->ata_channel;
    523        1.1    bouyer 	cp->name = PCIIDE_CHANNEL_NAME(channel);
    524       1.14   thorpej 	cp->ata_channel.ch_channel = channel;
    525       1.16   thorpej 	cp->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
    526        1.1    bouyer 
    527       1.14   thorpej 	cp->ata_channel.ch_queue =
    528        1.8   thorpej 	    malloc(sizeof(struct ata_queue), M_DEVBUF, M_NOWAIT);
    529       1.14   thorpej 	if (cp->ata_channel.ch_queue == NULL) {
    530        1.1    bouyer 		aprint_error("%s %s channel: "
    531        1.1    bouyer 		    "can't allocate memory for command queue",
    532  1.27.40.1       mjf 		    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), cp->name);
    533        1.1    bouyer 		    return;
    534        1.1    bouyer 	}
    535       1.23    bouyer 	cp->ata_channel.ch_ndrive = 2;
    536        1.1    bouyer 
    537        1.1    bouyer 	/* XXX */
    538        1.1    bouyer 	reg = 0xa2 + channel * 16;
    539        1.1    bouyer 	for (i = 0; i < sizeof(init_val); i++)
    540        1.1    bouyer 		pciide_pci_write(sc->sc_pc, sc->sc_tag, reg + i, init_val[i]);
    541        1.1    bouyer 
    542  1.27.40.1       mjf 	aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    543  1.27.40.1       mjf 	    "%s channel %s to %s mode\n", cp->name,
    544        1.1    bouyer 	    (interface & PCIIDE_INTERFACE_SETTABLE(channel)) ?
    545        1.1    bouyer 	    "configured" : "wired",
    546        1.1    bouyer 	    (interface & PCIIDE_INTERFACE_PCI(channel)) ?
    547        1.1    bouyer 	    "native-PCI" : "compatibility");
    548        1.1    bouyer 
    549        1.1    bouyer 	pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize, pciide_pci_intr);
    550        1.1    bouyer }
    551        1.1    bouyer 
    552        1.2   thorpej static void
    553       1.14   thorpej cmd680_setup_channel(struct ata_channel *chp)
    554        1.1    bouyer {
    555        1.1    bouyer 	struct ata_drive_datas *drvp;
    556        1.1    bouyer 	u_int8_t mode, off, scsc;
    557        1.1    bouyer 	u_int16_t val;
    558        1.1    bouyer 	u_int32_t idedma_ctl;
    559       1.17   thorpej 	int drive, s;
    560       1.15   thorpej 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
    561       1.15   thorpej 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    562        1.1    bouyer 	pci_chipset_tag_t pc = sc->sc_pc;
    563        1.1    bouyer 	pcitag_t pa = sc->sc_tag;
    564        1.1    bouyer 	static const u_int8_t udma2_tbl[] =
    565        1.1    bouyer 	    { 0x0f, 0x0b, 0x07, 0x06, 0x03, 0x02, 0x01 };
    566        1.1    bouyer 	static const u_int8_t udma_tbl[] =
    567        1.1    bouyer 	    { 0x0c, 0x07, 0x05, 0x04, 0x02, 0x01, 0x00 };
    568        1.1    bouyer 	static const u_int16_t dma_tbl[] =
    569        1.1    bouyer 	    { 0x2208, 0x10c2, 0x10c1 };
    570        1.1    bouyer 	static const u_int16_t pio_tbl[] =
    571        1.1    bouyer 	    { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
    572        1.1    bouyer 
    573        1.1    bouyer 	idedma_ctl = 0;
    574        1.1    bouyer 	pciide_channel_dma_setup(cp);
    575       1.10   thorpej 	mode = pciide_pci_read(pc, pa, 0x80 + chp->ch_channel * 4);
    576        1.1    bouyer 
    577        1.1    bouyer 	for (drive = 0; drive < 2; drive++) {
    578        1.1    bouyer 		drvp = &chp->ch_drive[drive];
    579        1.1    bouyer 		/* If no drive, skip */
    580        1.1    bouyer 		if ((drvp->drive_flags & DRIVE) == 0)
    581        1.1    bouyer 			continue;
    582        1.1    bouyer 		mode &= ~(0x03 << (drive * 4));
    583        1.1    bouyer 		if (drvp->drive_flags & DRIVE_UDMA) {
    584       1.17   thorpej 			s = splbio();
    585        1.1    bouyer 			drvp->drive_flags &= ~DRIVE_DMA;
    586       1.17   thorpej 			splx(s);
    587       1.10   thorpej 			off = 0xa0 + chp->ch_channel * 16;
    588        1.1    bouyer 			if (drvp->UDMA_mode > 2 &&
    589        1.1    bouyer 			    (pciide_pci_read(pc, pa, off) & 0x01) == 0)
    590        1.1    bouyer 				drvp->UDMA_mode = 2;
    591        1.1    bouyer 			scsc = pciide_pci_read(pc, pa, 0x8a);
    592        1.1    bouyer 			if (drvp->UDMA_mode == 6 && (scsc & 0x30) == 0) {
    593        1.1    bouyer 				pciide_pci_write(pc, pa, 0x8a, scsc | 0x01);
    594        1.1    bouyer 				scsc = pciide_pci_read(pc, pa, 0x8a);
    595        1.1    bouyer 				if ((scsc & 0x30) == 0)
    596        1.1    bouyer 					drvp->UDMA_mode = 5;
    597        1.1    bouyer 			}
    598        1.1    bouyer 			mode |= 0x03 << (drive * 4);
    599       1.10   thorpej 			off = 0xac + chp->ch_channel * 16 + drive * 2;
    600        1.1    bouyer 			val = pciide_pci_read(pc, pa, off) & ~0x3f;
    601        1.1    bouyer 			if (scsc & 0x30)
    602        1.1    bouyer 				val |= udma2_tbl[drvp->UDMA_mode];
    603        1.1    bouyer 			else
    604        1.1    bouyer 				val |= udma_tbl[drvp->UDMA_mode];
    605        1.1    bouyer 			pciide_pci_write(pc, pa, off, val);
    606        1.1    bouyer 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    607        1.1    bouyer 		} else if (drvp->drive_flags & DRIVE_DMA) {
    608        1.1    bouyer 			mode |= 0x02 << (drive * 4);
    609       1.10   thorpej 			off = 0xa8 + chp->ch_channel * 16 + drive * 2;
    610        1.1    bouyer 			val = dma_tbl[drvp->DMA_mode];
    611        1.1    bouyer 			pciide_pci_write(pc, pa, off, val & 0xff);
    612       1.20  christos 			pciide_pci_write(pc, pa, off+1, val >> 8);
    613        1.1    bouyer 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    614        1.1    bouyer 		} else {
    615        1.1    bouyer 			mode |= 0x01 << (drive * 4);
    616       1.10   thorpej 			off = 0xa4 + chp->ch_channel * 16 + drive * 2;
    617        1.1    bouyer 			val = pio_tbl[drvp->PIO_mode];
    618        1.1    bouyer 			pciide_pci_write(pc, pa, off, val & 0xff);
    619       1.20  christos 			pciide_pci_write(pc, pa, off+1, val >> 8);
    620        1.1    bouyer 		}
    621        1.1    bouyer 	}
    622        1.1    bouyer 
    623       1.10   thorpej 	pciide_pci_write(pc, pa, 0x80 + chp->ch_channel * 4, mode);
    624        1.1    bouyer 	if (idedma_ctl != 0) {
    625        1.1    bouyer 		/* Add software bits in status register */
    626        1.6      fvdl 		bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
    627        1.1    bouyer 		    idedma_ctl);
    628        1.1    bouyer 	}
    629        1.1    bouyer }
    630