Home | History | Annotate | Line # | Download | only in pci
rdcide.c revision 1.2
      1  1.2  dyoung /*	$NetBSD: rdcide.c,v 1.2 2011/04/04 22:13:58 dyoung Exp $	*/
      2  1.1  bouyer 
      3  1.1  bouyer /*
      4  1.1  bouyer  * Copyright (c) 2011 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.1  bouyer  * 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.1  bouyer #include <sys/cdefs.h>
     28  1.2  dyoung __KERNEL_RCSID(0, "$NetBSD: rdcide.c,v 1.2 2011/04/04 22:13:58 dyoung Exp $");
     29  1.1  bouyer 
     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/rdcide_reg.h>
     38  1.1  bouyer 
     39  1.2  dyoung static void rdcide_chip_map(struct pciide_softc *,
     40  1.2  dyoung     const struct pci_attach_args *);
     41  1.1  bouyer static void rdcide_setup_channel(struct ata_channel *);
     42  1.1  bouyer 
     43  1.1  bouyer static bool rdcide_resume(device_t, const pmf_qual_t *);
     44  1.1  bouyer static bool rdcide_suspend(device_t, const pmf_qual_t *);
     45  1.1  bouyer static int  rdcide_match(device_t, cfdata_t, void *);
     46  1.1  bouyer static void rdcide_attach(device_t, device_t, void *);
     47  1.1  bouyer 
     48  1.1  bouyer static const struct pciide_product_desc pciide_intel_products[] =  {
     49  1.1  bouyer 	{ PCI_PRODUCT_RDC_IDE,
     50  1.1  bouyer 	  0,
     51  1.1  bouyer 	  "RDC IDE controller",
     52  1.1  bouyer 	  rdcide_chip_map,
     53  1.1  bouyer 	},
     54  1.1  bouyer };
     55  1.1  bouyer 
     56  1.1  bouyer CFATTACH_DECL_NEW(rdcide, sizeof(struct pciide_softc),
     57  1.1  bouyer     rdcide_match, rdcide_attach, NULL, NULL);
     58  1.1  bouyer 
     59  1.1  bouyer static int
     60  1.1  bouyer rdcide_match(device_t parent, cfdata_t match, void *aux)
     61  1.1  bouyer {
     62  1.1  bouyer 	struct pci_attach_args *pa = aux;
     63  1.1  bouyer 
     64  1.1  bouyer 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_RDC) {
     65  1.1  bouyer 		if (pciide_lookup_product(pa->pa_id, pciide_intel_products))
     66  1.1  bouyer 			return (2);
     67  1.1  bouyer 	}
     68  1.1  bouyer 	return (0);
     69  1.1  bouyer }
     70  1.1  bouyer 
     71  1.1  bouyer static void
     72  1.1  bouyer rdcide_attach(device_t parent, device_t self, void *aux)
     73  1.1  bouyer {
     74  1.1  bouyer 	struct pci_attach_args *pa = aux;
     75  1.1  bouyer 	struct pciide_softc *sc = device_private(self);
     76  1.1  bouyer 
     77  1.1  bouyer 	sc->sc_wdcdev.sc_atac.atac_dev = self;
     78  1.1  bouyer 
     79  1.1  bouyer 	pciide_common_attach(sc, pa,
     80  1.1  bouyer 	    pciide_lookup_product(pa->pa_id, pciide_intel_products));
     81  1.1  bouyer 
     82  1.1  bouyer 	if (!pmf_device_register(self, rdcide_suspend, rdcide_resume))
     83  1.1  bouyer 		aprint_error_dev(self, "couldn't establish power handler\n");
     84  1.1  bouyer }
     85  1.1  bouyer 
     86  1.1  bouyer static bool
     87  1.1  bouyer rdcide_resume(device_t dv, const pmf_qual_t *qual)
     88  1.1  bouyer {
     89  1.1  bouyer 	struct pciide_softc *sc = device_private(dv);
     90  1.1  bouyer 
     91  1.1  bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, RDCIDE_PATR,
     92  1.1  bouyer 	    sc->sc_pm_reg[0]);
     93  1.1  bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, RDCIDE_PSD1ATR,
     94  1.1  bouyer 	    sc->sc_pm_reg[1]);
     95  1.1  bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, RDCIDE_UDCCR,
     96  1.1  bouyer 	    sc->sc_pm_reg[2]);
     97  1.1  bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, RDCIDE_IIOCR,
     98  1.1  bouyer 	    sc->sc_pm_reg[3]);
     99  1.1  bouyer 
    100  1.1  bouyer 	return true;
    101  1.1  bouyer }
    102  1.1  bouyer 
    103  1.1  bouyer static bool
    104  1.1  bouyer rdcide_suspend(device_t dv, const pmf_qual_t *qual)
    105  1.1  bouyer {
    106  1.1  bouyer 	struct pciide_softc *sc = device_private(dv);
    107  1.1  bouyer 
    108  1.1  bouyer 	sc->sc_pm_reg[0] = pci_conf_read(sc->sc_pc, sc->sc_tag,
    109  1.1  bouyer 	    RDCIDE_PATR);
    110  1.1  bouyer 	sc->sc_pm_reg[1] = pci_conf_read(sc->sc_pc, sc->sc_tag,
    111  1.1  bouyer 	    RDCIDE_PSD1ATR);
    112  1.1  bouyer 	sc->sc_pm_reg[2] = pci_conf_read(sc->sc_pc, sc->sc_tag,
    113  1.1  bouyer 	    RDCIDE_UDCCR);
    114  1.1  bouyer 	sc->sc_pm_reg[3] = pci_conf_read(sc->sc_pc, sc->sc_tag,
    115  1.1  bouyer 	    RDCIDE_IIOCR);
    116  1.1  bouyer 
    117  1.1  bouyer 	return true;
    118  1.1  bouyer }
    119  1.1  bouyer 
    120  1.1  bouyer static void
    121  1.2  dyoung rdcide_chip_map(struct pciide_softc *sc, const struct pci_attach_args *pa)
    122  1.1  bouyer {
    123  1.1  bouyer 	struct pciide_channel *cp;
    124  1.1  bouyer 	int channel;
    125  1.1  bouyer 	u_int32_t patr;
    126  1.1  bouyer 	pcireg_t interface = PCI_INTERFACE(pa->pa_class);
    127  1.1  bouyer 
    128  1.1  bouyer 	if (pciide_chipen(sc, pa) == 0)
    129  1.1  bouyer 		return;
    130  1.1  bouyer 
    131  1.1  bouyer 	aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    132  1.1  bouyer 	    "bus-master DMA support present");
    133  1.1  bouyer 	pciide_mapreg_dma(sc, pa);
    134  1.1  bouyer 	aprint_verbose("\n");
    135  1.1  bouyer 	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
    136  1.1  bouyer 	if (sc->sc_dma_ok) {
    137  1.1  bouyer 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
    138  1.1  bouyer 		sc->sc_wdcdev.irqack = pciide_irqack;
    139  1.1  bouyer 		sc->sc_wdcdev.dma_init = pciide_dma_init;
    140  1.1  bouyer 	}
    141  1.1  bouyer 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
    142  1.1  bouyer 	sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
    143  1.1  bouyer 	sc->sc_wdcdev.sc_atac.atac_udma_cap = 5;
    144  1.1  bouyer 	sc->sc_wdcdev.sc_atac.atac_set_modes = rdcide_setup_channel;
    145  1.1  bouyer 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
    146  1.1  bouyer 	sc->sc_wdcdev.sc_atac.atac_nchannels = PCIIDE_NUM_CHANNELS;
    147  1.1  bouyer 
    148  1.1  bouyer 	ATADEBUG_PRINT(("rdcide_setup_chip: old PATR=0x%x",
    149  1.1  bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, RDCIDE_PATR)),
    150  1.1  bouyer 	    DEBUG_PROBE);
    151  1.1  bouyer 	ATADEBUG_PRINT((", PSD1ATR=0x%x",
    152  1.1  bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, RDCIDE_PSD1ATR)),
    153  1.1  bouyer 	    DEBUG_PROBE);
    154  1.1  bouyer 	ATADEBUG_PRINT((", UDCCR 0x%x",
    155  1.1  bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, RDCIDE_UDCCR)),
    156  1.1  bouyer 	    DEBUG_PROBE);
    157  1.1  bouyer 	ATADEBUG_PRINT((", IIOCR 0x%x",
    158  1.1  bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, RDCIDE_IIOCR)),
    159  1.1  bouyer 	    DEBUG_PROBE);
    160  1.1  bouyer 	ATADEBUG_PRINT(("\n"), DEBUG_PROBE);
    161  1.1  bouyer 
    162  1.1  bouyer 	wdc_allocate_regs(&sc->sc_wdcdev);
    163  1.1  bouyer 
    164  1.1  bouyer 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
    165  1.1  bouyer 	     channel++) {
    166  1.1  bouyer 		cp = &sc->pciide_channels[channel];
    167  1.1  bouyer 		if (pciide_chansetup(sc, channel, interface) == 0)
    168  1.1  bouyer 			continue;
    169  1.1  bouyer 		patr = pci_conf_read(sc->sc_pc, sc->sc_tag, RDCIDE_PATR);
    170  1.1  bouyer 		if ((patr & RDCIDE_PATR_EN(channel)) == 0) {
    171  1.1  bouyer 			aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    172  1.1  bouyer 			    "%s channel ignored (disabled)\n", cp->name);
    173  1.1  bouyer 			cp->ata_channel.ch_flags |= ATACH_DISABLED;
    174  1.1  bouyer 			continue;
    175  1.1  bouyer 		}
    176  1.1  bouyer 		pciide_mapchan(pa, cp, interface, pciide_pci_intr);
    177  1.1  bouyer 	}
    178  1.1  bouyer 	ATADEBUG_PRINT(("rdcide_setup_chip: PATR=0x%x",
    179  1.1  bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, RDCIDE_PATR)),
    180  1.1  bouyer 	    DEBUG_PROBE);
    181  1.1  bouyer 	ATADEBUG_PRINT((", PSD1ATR=0x%x",
    182  1.1  bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, RDCIDE_PSD1ATR)),
    183  1.1  bouyer 	    DEBUG_PROBE);
    184  1.1  bouyer 	ATADEBUG_PRINT((", UDCCR 0x%x",
    185  1.1  bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, RDCIDE_UDCCR)),
    186  1.1  bouyer 	    DEBUG_PROBE);
    187  1.1  bouyer 	ATADEBUG_PRINT((", IIOCR 0x%x",
    188  1.1  bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, RDCIDE_IIOCR)),
    189  1.1  bouyer 	    DEBUG_PROBE);
    190  1.1  bouyer 	ATADEBUG_PRINT(("\n"), DEBUG_PROBE);
    191  1.1  bouyer 
    192  1.1  bouyer }
    193  1.1  bouyer 
    194  1.1  bouyer static void
    195  1.1  bouyer rdcide_setup_channel(struct ata_channel *chp)
    196  1.1  bouyer {
    197  1.1  bouyer 	u_int8_t drive;
    198  1.1  bouyer 	u_int32_t patr, psd1atr, udccr, iiocr;
    199  1.1  bouyer 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
    200  1.1  bouyer 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    201  1.1  bouyer 	struct ata_drive_datas *drvp = cp->ata_channel.ch_drive;
    202  1.1  bouyer 
    203  1.1  bouyer 	patr = pci_conf_read(sc->sc_pc, sc->sc_tag, RDCIDE_PATR);
    204  1.1  bouyer 	psd1atr = pci_conf_read(sc->sc_pc, sc->sc_tag, RDCIDE_PSD1ATR);
    205  1.1  bouyer 	udccr = pci_conf_read(sc->sc_pc, sc->sc_tag, RDCIDE_UDCCR);
    206  1.1  bouyer 	iiocr = pci_conf_read(sc->sc_pc, sc->sc_tag, RDCIDE_IIOCR);
    207  1.1  bouyer 
    208  1.1  bouyer 	/* setup DMA */
    209  1.1  bouyer 	pciide_channel_dma_setup(cp);
    210  1.1  bouyer 
    211  1.1  bouyer 	/* clear modes */
    212  1.1  bouyer 	patr = patr & (RDCIDE_PATR_EN(0) | RDCIDE_PATR_EN(1));
    213  1.1  bouyer 	psd1atr &= ~RDCIDE_PSD1ATR_SETUP_MASK(chp->ch_channel);
    214  1.1  bouyer 	psd1atr &= ~RDCIDE_PSD1ATR_HOLD_MASK(chp->ch_channel);
    215  1.1  bouyer 	for (drive = 0; drive < 2; drive++) {
    216  1.1  bouyer 		udccr &= ~RDCIDE_UDCCR_EN(chp->ch_channel, drive);
    217  1.1  bouyer 		udccr &= ~RDCIDE_UDCCR_TIM_MASK(chp->ch_channel, drive);
    218  1.1  bouyer 		iiocr &= ~RDCIDE_IIOCR_CLK_MASK(chp->ch_channel, drive);
    219  1.1  bouyer 	}
    220  1.1  bouyer 	/* now setup modes */
    221  1.1  bouyer 	for (drive = 0; drive < 2; drive++) {
    222  1.1  bouyer 		if ((drvp[drive].drive_flags & DRIVE) == 0)
    223  1.1  bouyer 			continue;
    224  1.1  bouyer 		if ((drvp[drive].drive_flags & DRIVE_ATAPI) == 0)
    225  1.1  bouyer 			patr |= RDCIDE_PATR_ATA(chp->ch_channel, drive);
    226  1.1  bouyer 		if (drive == 0) {
    227  1.1  bouyer 			patr |= RDCIDE_PATR_SETUP(
    228  1.1  bouyer 			    rdcide_setup[drvp[drive].PIO_mode],
    229  1.1  bouyer 			    chp->ch_channel);
    230  1.1  bouyer 			patr |= RDCIDE_PATR_HOLD(
    231  1.1  bouyer 			    rdcide_hold[drvp[drive].PIO_mode],
    232  1.1  bouyer 			    chp->ch_channel);
    233  1.1  bouyer 		} else {
    234  1.1  bouyer 			patr |= RDCIDE_PATR_DEV1_TEN(chp->ch_channel);
    235  1.1  bouyer 			psd1atr |= RDCIDE_PSD1ATR_SETUP(
    236  1.1  bouyer 			    rdcide_setup[drvp[drive].PIO_mode],
    237  1.1  bouyer 			    chp->ch_channel);
    238  1.1  bouyer 			psd1atr |= RDCIDE_PSD1ATR_HOLD(
    239  1.1  bouyer 			    rdcide_hold[drvp[drive].PIO_mode],
    240  1.1  bouyer 			    chp->ch_channel);
    241  1.1  bouyer 		}
    242  1.1  bouyer 		if (drvp[drive].PIO_mode > 0) {
    243  1.1  bouyer 			patr |= RDCIDE_PATR_FTIM(chp->ch_channel, drive);
    244  1.1  bouyer 			patr |= RDCIDE_PATR_IORDY(chp->ch_channel, drive);
    245  1.1  bouyer 		}
    246  1.1  bouyer 		if (drvp[drive].drive_flags & DRIVE_DMA) {
    247  1.1  bouyer 			patr |= RDCIDE_PATR_DMAEN(chp->ch_channel, drive);
    248  1.1  bouyer 		}
    249  1.1  bouyer 		if ((drvp[drive].drive_flags & DRIVE_UDMA) == 0)
    250  1.1  bouyer 			continue;
    251  1.1  bouyer 
    252  1.1  bouyer 		if ((iiocr & RDCIDE_IIOCR_CABLE(chp->ch_channel, drive)) == 0
    253  1.1  bouyer 		    && drvp[drive].UDMA_mode > 2)
    254  1.1  bouyer 			drvp[drive].UDMA_mode = 2;
    255  1.1  bouyer 		udccr |= RDCIDE_UDCCR_EN(chp->ch_channel, drive);
    256  1.1  bouyer 		udccr |= RDCIDE_UDCCR_TIM(
    257  1.1  bouyer 		    rdcide_udmatim[drvp[drive].UDMA_mode],
    258  1.1  bouyer 		    chp->ch_channel, drive);
    259  1.1  bouyer 		iiocr |= RDCIDE_IIOCR_CLK(
    260  1.1  bouyer 		    rdcide_udmaclk[drvp[drive].UDMA_mode],
    261  1.1  bouyer 		    chp->ch_channel, drive);
    262  1.1  bouyer 	}
    263  1.1  bouyer 
    264  1.1  bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, RDCIDE_PATR, patr);
    265  1.1  bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, RDCIDE_PSD1ATR, psd1atr);
    266  1.1  bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, RDCIDE_UDCCR, udccr);
    267  1.1  bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, RDCIDE_IIOCR, iiocr);
    268  1.1  bouyer }
    269