Home | History | Annotate | Line # | Download | only in pci
ixpide.c revision 1.17
      1 /*	$NetBSD: ixpide.c,v 1.17 2010/11/05 18:07:24 jakllsch Exp $	*/
      2 
      3 /*
      4  *  Copyright (c) 2004 The NetBSD Foundation.
      5  *  All rights reserved.
      6  *
      7  *  Redistribution and use in source and binary forms, with or without
      8  *  modification, are permitted provided that the following conditions
      9  *  are met:
     10  *  1. Redistributions of source code must retain the above copyright
     11  *     notice, this list of conditions and the following disclaimer.
     12  *  2. Redistributions in binary form must reproduce the above copyright
     13  *     notice, this list of conditions and the following disclaimer in the
     14  *     documentation and/or other materials provided with the distribution.
     15  *
     16  *  THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  *  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  *  POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: ixpide.c,v 1.17 2010/11/05 18:07:24 jakllsch Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 
     35 #include <dev/pci/pcivar.h>
     36 #include <dev/pci/pcidevs.h>
     37 #include <dev/pci/pciidereg.h>
     38 #include <dev/pci/pciidevar.h>
     39 #include <dev/pci/pciide_ixp_reg.h>
     40 
     41 static bool	ixpide_resume(device_t, const pmf_qual_t *);
     42 static bool	ixpide_suspend(device_t, const pmf_qual_t *);
     43 static int	ixpide_match(device_t, cfdata_t, void *);
     44 static void	ixpide_attach(device_t, device_t, void *);
     45 
     46 static void	ixp_chip_map(struct pciide_softc *, struct pci_attach_args *);
     47 static void	ixp_setup_channel(struct ata_channel *);
     48 
     49 CFATTACH_DECL_NEW(ixpide, sizeof(struct pciide_softc),
     50     ixpide_match, ixpide_attach, NULL, NULL);
     51 
     52 static const char ixpdesc[] = "ATI Technologies IXP IDE Controller";
     53 
     54 static const struct pciide_product_desc pciide_ixpide_products[] = {
     55 	{ PCI_PRODUCT_ATI_IXP_IDE_200, 0, ixpdesc, ixp_chip_map },
     56 	{ PCI_PRODUCT_ATI_IXP_IDE_300, 0, ixpdesc, ixp_chip_map },
     57 	{ PCI_PRODUCT_ATI_IXP_IDE_400, 0, ixpdesc, ixp_chip_map },
     58 	{ PCI_PRODUCT_ATI_IXP_IDE_600, 0, ixpdesc, ixp_chip_map },
     59 	{ PCI_PRODUCT_ATI_SB400_SATA_1, 0, ixpdesc, ixp_chip_map },
     60 	{ PCI_PRODUCT_ATI_SB400_SATA_2, 0, ixpdesc, ixp_chip_map },
     61 	{ PCI_PRODUCT_ATI_SB600_SATA_1, 0, ixpdesc, ixp_chip_map },
     62 	{ PCI_PRODUCT_ATI_SB600_SATA_2, 0, ixpdesc, ixp_chip_map },
     63 	{ PCI_PRODUCT_ATI_SB700_SATA_IDE, 0, ixpdesc, ixp_chip_map },
     64 	{ PCI_PRODUCT_ATI_SB700_IDE, 0, ixpdesc, ixp_chip_map },
     65 	{ 0, 			       0, NULL,	   NULL }
     66 };
     67 
     68 static int
     69 ixpide_match(device_t parent, cfdata_t cfdata, void *aux)
     70 {
     71 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
     72 
     73 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ATI &&
     74 	    PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
     75 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE &&
     76 	    pciide_lookup_product(pa->pa_id, pciide_ixpide_products))
     77 		return (2);
     78 	return (0);
     79 }
     80 
     81 static void
     82 ixpide_attach(device_t parent, device_t self, void *aux)
     83 {
     84 	struct pci_attach_args *pa = aux;
     85 	struct pciide_softc *sc = device_private(self);
     86 
     87 	sc->sc_wdcdev.sc_atac.atac_dev = self;
     88 
     89 	pciide_common_attach(sc, pa,
     90 	    pciide_lookup_product(pa->pa_id, pciide_ixpide_products));
     91 
     92 	if (!pmf_device_register(self, ixpide_suspend, ixpide_resume))
     93 		aprint_error_dev(self, "couldn't establish power handler\n");
     94 }
     95 
     96 static void
     97 ixp_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
     98 {
     99 	struct pciide_channel *cp;
    100 	int channel;
    101 	pcireg_t interface;
    102 
    103 	if (pciide_chipen(sc, pa) == 0)
    104 		return;
    105 
    106 	aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    107 	    "bus-master DMA support present");
    108 	pciide_mapreg_dma(sc, pa);
    109 	aprint_verbose("\n");
    110 
    111 	sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
    112 	if (sc->sc_dma_ok) {
    113 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
    114 		sc->sc_wdcdev.irqack = pciide_irqack;
    115 	}
    116 
    117 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
    118 	sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
    119 	sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    120 	sc->sc_wdcdev.sc_atac.atac_set_modes = ixp_setup_channel;
    121 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
    122 	sc->sc_wdcdev.sc_atac.atac_nchannels = PCIIDE_NUM_CHANNELS;
    123 
    124 	interface = PCI_INTERFACE(pa->pa_class);
    125 
    126 	wdc_allocate_regs(&sc->sc_wdcdev);
    127 
    128 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
    129 	    channel++) {
    130 		cp = &sc->pciide_channels[channel];
    131 		if (pciide_chansetup(sc, channel, interface) == 0)
    132 			continue;
    133 		pciide_mapchan(pa, cp, interface, pciide_pci_intr);
    134 	}
    135 }
    136 
    137 /* Values from linux driver */
    138 static const uint8_t ixp_pio_timings[] = {
    139 	0x5d, 0x47, 0x34, 0x22, 0x20
    140 };
    141 
    142 static const uint8_t ixp_mdma_timings[] = {
    143 	0x77, 0x21, 0x20
    144 };
    145 
    146 static bool
    147 ixpide_resume(device_t dv, const pmf_qual_t *qual)
    148 {
    149 	struct pciide_softc *sc = device_private(dv);
    150 
    151 	pci_conf_write(sc->sc_pc, sc->sc_tag, IXP_MDMA_TIMING,
    152 	    sc->sc_pm_reg[0]);
    153 	pci_conf_write(sc->sc_pc, sc->sc_tag, IXP_PIO_TIMING,
    154 	    sc->sc_pm_reg[1]);
    155 
    156 	return true;
    157 }
    158 
    159 static bool
    160 ixpide_suspend(device_t dv, const pmf_qual_t *qual)
    161 {
    162 	struct pciide_softc *sc = device_private(dv);
    163 
    164 	sc->sc_pm_reg[0] = pci_conf_read(sc->sc_pc, sc->sc_tag,
    165 	    IXP_MDMA_TIMING);
    166 	sc->sc_pm_reg[1] = pci_conf_read(sc->sc_pc, sc->sc_tag,
    167 	    IXP_PIO_TIMING);
    168 
    169 	return true;
    170 }
    171 
    172 static void
    173 ixp_setup_channel(struct ata_channel *chp)
    174 {
    175 	int drive, s;
    176 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
    177 	pcireg_t udma, mdma_timing, pio, pio_timing;
    178 	struct ata_drive_datas *drvp;
    179 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    180 
    181 	pio_timing = pci_conf_read(sc->sc_pc, sc->sc_tag, IXP_PIO_TIMING);
    182 	pio = pci_conf_read(sc->sc_pc, sc->sc_tag, IXP_PIO_CTL);
    183 	mdma_timing = pci_conf_read(sc->sc_pc, sc->sc_tag, IXP_MDMA_TIMING);
    184 	udma = pci_conf_read(sc->sc_pc, sc->sc_tag, IXP_UDMA_CTL);
    185 
    186 	pciide_channel_dma_setup(cp);
    187 
    188 	for (drive = 0; drive < 2; drive++) {
    189 		drvp = &chp->ch_drive[drive];
    190 		if ((drvp->drive_flags & DRIVE) == 0)
    191 			continue;
    192 		if (drvp->drive_flags & DRIVE_UDMA) {
    193 			s = splbio();
    194 			drvp->drive_flags &= ~DRIVE_DMA;
    195 			splx(s);
    196 			IXP_UDMA_ENABLE(udma, chp->ch_channel, drive);
    197 			IXP_SET_MODE(udma, chp->ch_channel, drive, drvp->UDMA_mode);
    198 		} else if (drvp->drive_flags & DRIVE_DMA) {
    199 			IXP_UDMA_DISABLE(udma, chp->ch_channel, drive);
    200 			IXP_SET_TIMING(mdma_timing, chp->ch_channel, drive,
    201 			    ixp_mdma_timings[drvp->DMA_mode]);
    202 		} else
    203 			IXP_UDMA_DISABLE(udma, chp->ch_channel, drive);
    204 
    205 		/*
    206 		 * Set PIO mode and timings
    207 		 * Linux driver avoids PIO mode 1, let's do it too.
    208 		 */
    209 		if (drvp->PIO_mode == 1)
    210 			drvp->PIO_mode = 0;
    211 
    212 		IXP_SET_MODE(pio, chp->ch_channel, drive, drvp->PIO_mode);
    213 		IXP_SET_TIMING(pio_timing, chp->ch_channel, drive,
    214 		    ixp_pio_timings[drvp->PIO_mode]);
    215 	}
    216 	pci_conf_write(sc->sc_pc, sc->sc_tag, IXP_UDMA_CTL, udma);
    217 	pci_conf_write(sc->sc_pc, sc->sc_tag, IXP_MDMA_TIMING, mdma_timing);
    218 	pci_conf_write(sc->sc_pc, sc->sc_tag, IXP_PIO_CTL, pio);
    219 	pci_conf_write(sc->sc_pc, sc->sc_tag, IXP_PIO_TIMING, pio_timing);
    220 	ATADEBUG_PRINT(("ixp_setup_channel: udma = %08x, mdma_timing = %08x, pio_mode = %08x,"
    221 	    " pio_timing = %08x\n", udma, mdma_timing, pio, pio_timing), DEBUG_PROBE);
    222 }
    223