Home | History | Annotate | Line # | Download | only in pci
cypide.c revision 1.3
      1  1.3  mycroft /*	$NetBSD: cypide.c,v 1.3 2003/10/24 00:24:15 mycroft 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.1   bouyer  * 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.1   bouyer 
     33  1.1   bouyer #include <sys/param.h>
     34  1.1   bouyer #include <sys/systm.h>
     35  1.1   bouyer #include <sys/malloc.h>
     36  1.1   bouyer 
     37  1.1   bouyer #include <dev/pci/pcivar.h>
     38  1.1   bouyer #include <dev/pci/pcidevs.h>
     39  1.1   bouyer #include <dev/pci/pciidereg.h>
     40  1.1   bouyer #include <dev/pci/pciidevar.h>
     41  1.1   bouyer #include <dev/pci/pciide_cy693_reg.h>
     42  1.1   bouyer #include <dev/pci/cy82c693var.h>
     43  1.1   bouyer 
     44  1.2  thorpej static void cy693_chip_map(struct pciide_softc*, struct pci_attach_args*);
     45  1.2  thorpej static void cy693_setup_channel(struct channel_softc*);
     46  1.1   bouyer 
     47  1.2  thorpej static int  cypide_match(struct device *, struct cfdata *, void *);
     48  1.2  thorpej static void cypide_attach(struct device *, struct device *, void *);
     49  1.1   bouyer 
     50  1.1   bouyer CFATTACH_DECL(cypide, sizeof(struct pciide_softc),
     51  1.1   bouyer     cypide_match, cypide_attach, NULL, NULL);
     52  1.1   bouyer 
     53  1.2  thorpej static const struct pciide_product_desc pciide_cypress_products[] =  {
     54  1.1   bouyer 	{ PCI_PRODUCT_CONTAQ_82C693,
     55  1.1   bouyer 	  IDE_16BIT_IOSPACE,
     56  1.1   bouyer 	  "Cypress 82C693 IDE Controller",
     57  1.1   bouyer 	  cy693_chip_map,
     58  1.1   bouyer 	},
     59  1.1   bouyer 	{ 0,
     60  1.1   bouyer 	  0,
     61  1.1   bouyer 	  NULL,
     62  1.1   bouyer 	  NULL
     63  1.1   bouyer 	}
     64  1.1   bouyer };
     65  1.1   bouyer 
     66  1.2  thorpej static int
     67  1.2  thorpej cypide_match(struct device *parent, struct cfdata *match, void *aux)
     68  1.1   bouyer {
     69  1.1   bouyer 	struct pci_attach_args *pa = aux;
     70  1.1   bouyer 
     71  1.3  mycroft 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_CONTAQ &&
     72  1.3  mycroft 	    PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
     73  1.3  mycroft 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
     74  1.1   bouyer 		if (pciide_lookup_product(pa->pa_id, pciide_cypress_products))
     75  1.1   bouyer 			return (2);
     76  1.1   bouyer 	}
     77  1.1   bouyer 	return (0);
     78  1.1   bouyer }
     79  1.1   bouyer 
     80  1.2  thorpej static void
     81  1.2  thorpej cypide_attach(struct device *parent, struct device *self, void *aux)
     82  1.1   bouyer {
     83  1.1   bouyer 	struct pci_attach_args *pa = aux;
     84  1.1   bouyer 	struct pciide_softc *sc = (struct pciide_softc *)self;
     85  1.1   bouyer 
     86  1.1   bouyer 	pciide_common_attach(sc, pa,
     87  1.1   bouyer 	    pciide_lookup_product(pa->pa_id, pciide_cypress_products));
     88  1.1   bouyer 
     89  1.1   bouyer }
     90  1.1   bouyer 
     91  1.2  thorpej static void
     92  1.2  thorpej cy693_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
     93  1.1   bouyer {
     94  1.1   bouyer 	struct pciide_channel *cp;
     95  1.1   bouyer 	pcireg_t interface = PCI_INTERFACE(pa->pa_class);
     96  1.1   bouyer 	bus_size_t cmdsize, ctlsize;
     97  1.1   bouyer 
     98  1.1   bouyer 	if (pciide_chipen(sc, pa) == 0)
     99  1.1   bouyer 		return;
    100  1.1   bouyer 
    101  1.1   bouyer 	/*
    102  1.1   bouyer 	 * this chip has 2 PCI IDE functions, one for primary and one for
    103  1.1   bouyer 	 * secondary. So we need to call pciide_mapregs_compat() with
    104  1.1   bouyer 	 * the real channel
    105  1.1   bouyer 	 */
    106  1.1   bouyer 	if (pa->pa_function == 1) {
    107  1.1   bouyer 		sc->sc_cy_compatchan = 0;
    108  1.1   bouyer 	} else if (pa->pa_function == 2) {
    109  1.1   bouyer 		sc->sc_cy_compatchan = 1;
    110  1.1   bouyer 	} else {
    111  1.1   bouyer 		aprint_error("%s: unexpected PCI function %d\n",
    112  1.1   bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname, pa->pa_function);
    113  1.1   bouyer 		return;
    114  1.1   bouyer 	}
    115  1.1   bouyer 	if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) {
    116  1.1   bouyer 		aprint_normal("%s: bus-master DMA support present",
    117  1.1   bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname);
    118  1.1   bouyer 		pciide_mapreg_dma(sc, pa);
    119  1.1   bouyer 	} else {
    120  1.1   bouyer 		aprint_normal("%s: hardware does not support DMA",
    121  1.1   bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname);
    122  1.1   bouyer 		sc->sc_dma_ok = 0;
    123  1.1   bouyer 	}
    124  1.1   bouyer 	aprint_normal("\n");
    125  1.1   bouyer 
    126  1.1   bouyer 	sc->sc_cy_handle = cy82c693_init(pa->pa_iot);
    127  1.1   bouyer 	if (sc->sc_cy_handle == NULL) {
    128  1.1   bouyer 		aprint_error("%s: unable to map hyperCache control registers\n",
    129  1.1   bouyer 		    sc->sc_wdcdev.sc_dev.dv_xname);
    130  1.1   bouyer 		sc->sc_dma_ok = 0;
    131  1.1   bouyer 	}
    132  1.1   bouyer 
    133  1.1   bouyer 	sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
    134  1.1   bouyer 	    WDC_CAPABILITY_MODE;
    135  1.1   bouyer 	if (sc->sc_dma_ok) {
    136  1.1   bouyer 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_IRQACK;
    137  1.1   bouyer 		sc->sc_wdcdev.irqack = pciide_irqack;
    138  1.1   bouyer 	}
    139  1.1   bouyer 	sc->sc_wdcdev.PIO_cap = 4;
    140  1.1   bouyer 	sc->sc_wdcdev.DMA_cap = 2;
    141  1.1   bouyer 	sc->sc_wdcdev.set_modes = cy693_setup_channel;
    142  1.1   bouyer 
    143  1.1   bouyer 	sc->sc_wdcdev.channels = sc->wdc_chanarray;
    144  1.1   bouyer 	sc->sc_wdcdev.nchannels = 1;
    145  1.1   bouyer 
    146  1.1   bouyer 	/* Only one channel for this chip; if we are here it's enabled */
    147  1.1   bouyer 	cp = &sc->pciide_channels[0];
    148  1.1   bouyer 	sc->wdc_chanarray[0] = &cp->wdc_channel;
    149  1.1   bouyer 	cp->name = PCIIDE_CHANNEL_NAME(0);
    150  1.1   bouyer 	cp->wdc_channel.channel = 0;
    151  1.1   bouyer 	cp->wdc_channel.wdc = &sc->sc_wdcdev;
    152  1.1   bouyer 	cp->wdc_channel.ch_queue =
    153  1.1   bouyer 	    malloc(sizeof(struct channel_queue), M_DEVBUF, M_NOWAIT);
    154  1.1   bouyer 	if (cp->wdc_channel.ch_queue == NULL) {
    155  1.1   bouyer 		aprint_error("%s primary channel: "
    156  1.1   bouyer 		    "can't allocate memory for command queue",
    157  1.1   bouyer 		sc->sc_wdcdev.sc_dev.dv_xname);
    158  1.1   bouyer 		return;
    159  1.1   bouyer 	}
    160  1.1   bouyer 	aprint_normal("%s: primary channel %s to ",
    161  1.1   bouyer 	    sc->sc_wdcdev.sc_dev.dv_xname,
    162  1.1   bouyer 	    (interface & PCIIDE_INTERFACE_SETTABLE(0)) ?
    163  1.1   bouyer 	    "configured" : "wired");
    164  1.1   bouyer 	if (interface & PCIIDE_INTERFACE_PCI(0)) {
    165  1.1   bouyer 		aprint_normal("native-PCI mode\n");
    166  1.1   bouyer 		pciide_mapregs_native(pa, cp, &cmdsize, &ctlsize,
    167  1.1   bouyer 		    pciide_pci_intr);
    168  1.1   bouyer 	} else {
    169  1.1   bouyer 		aprint_normal("compatibility mode\n");
    170  1.1   bouyer 		pciide_mapregs_compat(pa, cp, sc->sc_cy_compatchan, &cmdsize,
    171  1.1   bouyer 		    &ctlsize);
    172  1.1   bouyer 	}
    173  1.1   bouyer 	wdcattach(&cp->wdc_channel);
    174  1.1   bouyer }
    175  1.1   bouyer 
    176  1.2  thorpej static void
    177  1.2  thorpej cy693_setup_channel(struct channel_softc *chp)
    178  1.1   bouyer {
    179  1.1   bouyer 	struct ata_drive_datas *drvp;
    180  1.1   bouyer 	int drive;
    181  1.1   bouyer 	u_int32_t cy_cmd_ctrl;
    182  1.1   bouyer 	u_int32_t idedma_ctl;
    183  1.1   bouyer 	struct pciide_channel *cp = (struct pciide_channel*)chp;
    184  1.1   bouyer 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
    185  1.1   bouyer 	int dma_mode = -1;
    186  1.1   bouyer 
    187  1.1   bouyer 	WDCDEBUG_PRINT(("cy693_chip_map: old timings reg 0x%x\n",
    188  1.1   bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL)),DEBUG_PROBE);
    189  1.1   bouyer 
    190  1.1   bouyer 	cy_cmd_ctrl = idedma_ctl = 0;
    191  1.1   bouyer 
    192  1.1   bouyer 	/* setup DMA if needed */
    193  1.1   bouyer 	pciide_channel_dma_setup(cp);
    194  1.1   bouyer 
    195  1.1   bouyer 	for (drive = 0; drive < 2; drive++) {
    196  1.1   bouyer 		drvp = &chp->ch_drive[drive];
    197  1.1   bouyer 		/* If no drive, skip */
    198  1.1   bouyer 		if ((drvp->drive_flags & DRIVE) == 0)
    199  1.1   bouyer 			continue;
    200  1.1   bouyer 		/* add timing values, setup DMA if needed */
    201  1.1   bouyer 		if (drvp->drive_flags & DRIVE_DMA) {
    202  1.1   bouyer 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    203  1.1   bouyer 			/* use Multiword DMA */
    204  1.1   bouyer 			if (dma_mode == -1 || dma_mode > drvp->DMA_mode)
    205  1.1   bouyer 				dma_mode = drvp->DMA_mode;
    206  1.1   bouyer 		}
    207  1.1   bouyer 		cy_cmd_ctrl |= (cy_pio_pulse[drvp->PIO_mode] <<
    208  1.1   bouyer 		    CY_CMD_CTRL_IOW_PULSE_OFF(drive));
    209  1.1   bouyer 		cy_cmd_ctrl |= (cy_pio_rec[drvp->PIO_mode] <<
    210  1.1   bouyer 		    CY_CMD_CTRL_IOW_REC_OFF(drive));
    211  1.1   bouyer 		cy_cmd_ctrl |= (cy_pio_pulse[drvp->PIO_mode] <<
    212  1.1   bouyer 		    CY_CMD_CTRL_IOR_PULSE_OFF(drive));
    213  1.1   bouyer 		cy_cmd_ctrl |= (cy_pio_rec[drvp->PIO_mode] <<
    214  1.1   bouyer 		    CY_CMD_CTRL_IOR_REC_OFF(drive));
    215  1.1   bouyer 	}
    216  1.1   bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL, cy_cmd_ctrl);
    217  1.1   bouyer 	chp->ch_drive[0].DMA_mode = dma_mode;
    218  1.1   bouyer 	chp->ch_drive[1].DMA_mode = dma_mode;
    219  1.1   bouyer 
    220  1.1   bouyer 	if (dma_mode == -1)
    221  1.1   bouyer 		dma_mode = 0;
    222  1.1   bouyer 
    223  1.1   bouyer 	if (sc->sc_cy_handle != NULL) {
    224  1.1   bouyer 		/* Note: `multiple' is implied. */
    225  1.1   bouyer 		cy82c693_write(sc->sc_cy_handle,
    226  1.1   bouyer 		    (sc->sc_cy_compatchan == 0) ?
    227  1.1   bouyer 		    CY_DMA_IDX_PRIMARY : CY_DMA_IDX_SECONDARY, dma_mode);
    228  1.1   bouyer 	}
    229  1.1   bouyer 
    230  1.1   bouyer 	if (idedma_ctl != 0) {
    231  1.1   bouyer 		/* Add software bits in status register */
    232  1.1   bouyer 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    233  1.1   bouyer 		    IDEDMA_CTL, idedma_ctl);
    234  1.1   bouyer 	}
    235  1.1   bouyer 	WDCDEBUG_PRINT(("cy693_chip_map: new timings reg 0x%x\n",
    236  1.1   bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL)), DEBUG_PROBE);
    237  1.1   bouyer }
    238