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