aceride.c revision 1.6.4.1       1 /*	$NetBSD: aceride.c,v 1.6.4.1 2005/08/07 15:51:33 riz Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1999, 2000, 2001 Manuel Bouyer.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Manuel Bouyer.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     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_acer_reg.h>
     40 
     41 static int acer_pcib_match(struct pci_attach_args *);
     42 static void acer_do_reset(struct wdc_channel *, int);
     43 static void acer_chip_map(struct pciide_softc*, struct pci_attach_args*);
     44 static void acer_setup_channel(struct wdc_channel*);
     45 static int  acer_pci_intr(void *);
     46 
     47 static int  aceride_match(struct device *, struct cfdata *, void *);
     48 static void aceride_attach(struct device *, struct device *, void *);
     49 
     50 struct aceride_softc {
     51 	struct pciide_softc pciide_sc;
     52 	struct pci_attach_args pcib_pa;
     53 };
     54 CFATTACH_DECL(aceride, sizeof(struct aceride_softc),
     55     aceride_match, aceride_attach, NULL, NULL);
     56 
     57 static const struct pciide_product_desc pciide_acer_products[] =  {
     58 	{ PCI_PRODUCT_ALI_M5229,
     59 	  0,
     60 	  "Acer Labs M5229 UDMA IDE Controller",
     61 	  acer_chip_map,
     62 	},
     63 	{ 0,
     64 	  0,
     65 	  NULL,
     66 	  NULL
     67 	}
     68 };
     69 
     70 static int
     71 aceride_match(struct device *parent, struct cfdata *match, void *aux)
     72 {
     73 	struct pci_attach_args *pa = aux;
     74 
     75 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ALI &&
     76 	    PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
     77 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
     78 		if (pciide_lookup_product(pa->pa_id, pciide_acer_products))
     79 			return (2);
     80 	}
     81 	return (0);
     82 }
     83 
     84 static void
     85 aceride_attach(struct device *parent, struct device *self, void *aux)
     86 {
     87 	struct pci_attach_args *pa = aux;
     88 	struct pciide_softc *sc = (struct pciide_softc *)self;
     89 
     90 	pciide_common_attach(sc, pa,
     91 	    pciide_lookup_product(pa->pa_id, pciide_acer_products));
     92 
     93 }
     94 
     95 static int
     96 acer_pcib_match(struct pci_attach_args *pa)
     97 {
     98 	/*
     99 	 * we need to access the PCI config space of the pcib, see
    100 	 * acer_do_reset()
    101 	 */
    102 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE &&
    103 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_ISA &&
    104 	    PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ALI &&
    105 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ALI_M1543)
    106 		return 1;
    107 	return 0;
    108 }
    109 
    110 static void
    111 acer_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
    112 {
    113 	struct pciide_channel *cp;
    114 	int channel;
    115 	pcireg_t cr, interface;
    116 	bus_size_t cmdsize, ctlsize;
    117 	pcireg_t rev = PCI_REVISION(pa->pa_class);
    118 	struct aceride_softc *acer_sc = (struct aceride_softc *)sc;
    119 
    120 	if (pciide_chipen(sc, pa) == 0)
    121 		return;
    122 
    123 	aprint_normal("%s: bus-master DMA support present",
    124 	    sc->sc_wdcdev.sc_dev.dv_xname);
    125 	pciide_mapreg_dma(sc, pa);
    126 	aprint_normal("\n");
    127 	sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
    128 	    WDC_CAPABILITY_MODE;
    129 	if (sc->sc_dma_ok) {
    130 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA;
    131 		if (rev >= 0x20) {
    132 			sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
    133 			if (rev >= 0xC4)
    134 				sc->sc_wdcdev.UDMA_cap = 5;
    135 			else if (rev >= 0xC2)
    136 				sc->sc_wdcdev.UDMA_cap = 4;
    137 			else
    138 				sc->sc_wdcdev.UDMA_cap = 2;
    139 		}
    140 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_IRQACK;
    141 		sc->sc_wdcdev.irqack = pciide_irqack;
    142 	}
    143 
    144 	sc->sc_wdcdev.PIO_cap = 4;
    145 	sc->sc_wdcdev.DMA_cap = 2;
    146 	sc->sc_wdcdev.set_modes = acer_setup_channel;
    147 	sc->sc_wdcdev.channels = sc->wdc_chanarray;
    148 	sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
    149 
    150 	pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CDRC,
    151 	    (pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CDRC) |
    152 		ACER_CDRC_DMA_EN) & ~ACER_CDRC_FIFO_DISABLE);
    153 
    154 	/* Enable "microsoft register bits" R/W. */
    155 	pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR3,
    156 	    pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR3) | ACER_CCAR3_PI);
    157 	pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR1,
    158 	    pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR1) &
    159 	    ~(ACER_CHANSTATUS_RO|PCIIDE_CHAN_RO(0)|PCIIDE_CHAN_RO(1)));
    160 	pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR2,
    161 	    pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR2) &
    162 	    ~ACER_CHANSTATUSREGS_RO);
    163 	cr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG);
    164 	cr |= (PCIIDE_CHANSTATUS_EN << PCI_INTERFACE_SHIFT);
    165 	pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG, cr);
    166 	/* Don't use cr, re-read the real register content instead */
    167 	interface = PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag,
    168 	    PCI_CLASS_REG));
    169 
    170 	/* From linux: enable "Cable Detection" */
    171 	if (rev >= 0xC2) {
    172 		pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_0x4B,
    173 		    pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_0x4B)
    174 		    | ACER_0x4B_CDETECT);
    175 	}
    176 
    177 	if (rev == 0xC3) {
    178 		/* install reset bug workaround */
    179 		if (pci_find_device(&acer_sc->pcib_pa, acer_pcib_match) == 0) {
    180 			printf("%s: WARNING: can't find pci-isa bridge\n",
    181 			    sc->sc_wdcdev.sc_dev.dv_xname);
    182 		} else
    183 			sc->sc_wdcdev.reset = acer_do_reset;
    184 	}
    185 
    186 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
    187 		cp = &sc->pciide_channels[channel];
    188 		if (pciide_chansetup(sc, channel, interface) == 0)
    189 			continue;
    190 		if ((interface & PCIIDE_CHAN_EN(channel)) == 0) {
    191 			aprint_normal("%s: %s channel ignored (disabled)\n",
    192 			    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
    193 			cp->wdc_channel.ch_flags |= WDCF_DISABLED;
    194 			continue;
    195 		}
    196 		/* newer controllers seems to lack the ACER_CHIDS. Sigh */
    197 		pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize,
    198 		     (rev >= 0xC2) ? pciide_pci_intr : acer_pci_intr);
    199 	}
    200 }
    201 
    202 static void
    203 acer_do_reset(struct wdc_channel *chp, int poll)
    204 {
    205 	struct pciide_channel *cp = (struct pciide_channel*)chp;
    206 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.ch_wdc;
    207 	struct aceride_softc *acer_sc = (struct aceride_softc *)sc;
    208 	u_int8_t reg;
    209 
    210 	/*
    211 	 * From OpenSolaris: after a reset we need to disable/enable the
    212 	 * corresponding channel, or data corruption will occur in
    213 	 * UltraDMA modes
    214 	 */
    215 
    216 	wdc_do_reset(chp, poll);
    217 	reg = pciide_pci_read(acer_sc->pcib_pa.pa_pc, acer_sc->pcib_pa.pa_tag,
    218 	    ACER_PCIB_CTRL);
    219 	pciide_pci_write(acer_sc->pcib_pa.pa_pc, acer_sc->pcib_pa.pa_tag,
    220 	    ACER_PCIB_CTRL, reg & ACER_PCIB_CTRL_ENCHAN(chp->ch_channel));
    221 	delay(1000);
    222 	pciide_pci_write(acer_sc->pcib_pa.pa_pc, acer_sc->pcib_pa.pa_tag,
    223 	    ACER_PCIB_CTRL, reg);
    224 }
    225 
    226 static void
    227 acer_setup_channel(struct wdc_channel *chp)
    228 {
    229 	struct ata_drive_datas *drvp;
    230 	int drive;
    231 	u_int32_t acer_fifo_udma;
    232 	u_int32_t idedma_ctl;
    233 	struct pciide_channel *cp = (struct pciide_channel*)chp;
    234 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.ch_wdc;
    235 
    236 	idedma_ctl = 0;
    237 	acer_fifo_udma = pci_conf_read(sc->sc_pc, sc->sc_tag, ACER_FTH_UDMA);
    238 	WDCDEBUG_PRINT(("acer_setup_channel: old fifo/udma reg 0x%x\n",
    239 	    acer_fifo_udma), DEBUG_PROBE);
    240 	/* setup DMA if needed */
    241 	pciide_channel_dma_setup(cp);
    242 
    243 	if ((chp->ch_drive[0].drive_flags | chp->ch_drive[1].drive_flags) &
    244 	    DRIVE_UDMA) { /* check 80 pins cable */
    245 		if (pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_0x4A) &
    246 		    ACER_0x4A_80PIN(chp->ch_channel)) {
    247 			if (chp->ch_drive[0].UDMA_mode > 2)
    248 				chp->ch_drive[0].UDMA_mode = 2;
    249 			if (chp->ch_drive[1].UDMA_mode > 2)
    250 				chp->ch_drive[1].UDMA_mode = 2;
    251 		}
    252 	}
    253 
    254 	for (drive = 0; drive < 2; drive++) {
    255 		drvp = &chp->ch_drive[drive];
    256 		/* If no drive, skip */
    257 		if ((drvp->drive_flags & DRIVE) == 0)
    258 			continue;
    259 		WDCDEBUG_PRINT(("acer_setup_channel: old timings reg for "
    260 		    "channel %d drive %d 0x%x\n", chp->ch_channel, drive,
    261 		    pciide_pci_read(sc->sc_pc, sc->sc_tag,
    262 		    ACER_IDETIM(chp->ch_channel, drive))), DEBUG_PROBE);
    263 		/* clear FIFO/DMA mode */
    264 		acer_fifo_udma &= ~(ACER_FTH_OPL(chp->ch_channel, drive, 0x3) |
    265 		    ACER_UDMA_EN(chp->ch_channel, drive) |
    266 		    ACER_UDMA_TIM(chp->ch_channel, drive, 0x7));
    267 
    268 		/* add timing values, setup DMA if needed */
    269 		if ((drvp->drive_flags & DRIVE_DMA) == 0 &&
    270 		    (drvp->drive_flags & DRIVE_UDMA) == 0) {
    271 			acer_fifo_udma |=
    272 			    ACER_FTH_OPL(chp->ch_channel, drive, 0x1);
    273 			goto pio;
    274 		}
    275 
    276 		acer_fifo_udma |= ACER_FTH_OPL(chp->ch_channel, drive, 0x2);
    277 		if (drvp->drive_flags & DRIVE_UDMA) {
    278 			/* use Ultra/DMA */
    279 			drvp->drive_flags &= ~DRIVE_DMA;
    280 			acer_fifo_udma |= ACER_UDMA_EN(chp->ch_channel, drive);
    281 			acer_fifo_udma |=
    282 			    ACER_UDMA_TIM(chp->ch_channel, drive,
    283 				acer_udma[drvp->UDMA_mode]);
    284 			/* XXX disable if one drive < UDMA3 ? */
    285 			if (drvp->UDMA_mode >= 3) {
    286 				pciide_pci_write(sc->sc_pc, sc->sc_tag,
    287 				    ACER_0x4B,
    288 				    pciide_pci_read(sc->sc_pc, sc->sc_tag,
    289 					ACER_0x4B) | ACER_0x4B_UDMA66);
    290 			}
    291 		} else {
    292 			/*
    293 			 * use Multiword DMA
    294 			 * Timings will be used for both PIO and DMA,
    295 			 * so adjust DMA mode if needed
    296 			 */
    297 			if (drvp->PIO_mode > (drvp->DMA_mode + 2))
    298 				drvp->PIO_mode = drvp->DMA_mode + 2;
    299 			if (drvp->DMA_mode + 2 > (drvp->PIO_mode))
    300 				drvp->DMA_mode = (drvp->PIO_mode > 2) ?
    301 				    drvp->PIO_mode - 2 : 0;
    302 			if (drvp->DMA_mode == 0)
    303 				drvp->PIO_mode = 0;
    304 		}
    305 		idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    306 pio:		pciide_pci_write(sc->sc_pc, sc->sc_tag,
    307 		    ACER_IDETIM(chp->ch_channel, drive),
    308 		    acer_pio[drvp->PIO_mode]);
    309 	}
    310 	WDCDEBUG_PRINT(("acer_setup_channel: new fifo/udma reg 0x%x\n",
    311 	    acer_fifo_udma), DEBUG_PROBE);
    312 	pci_conf_write(sc->sc_pc, sc->sc_tag, ACER_FTH_UDMA, acer_fifo_udma);
    313 	if (idedma_ctl != 0) {
    314 		/* Add software bits in status register */
    315 		bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
    316 		    idedma_ctl);
    317 	}
    318 }
    319 
    320 static int
    321 acer_pci_intr(void *arg)
    322 {
    323 	struct pciide_softc *sc = arg;
    324 	struct pciide_channel *cp;
    325 	struct wdc_channel *wdc_cp;
    326 	int i, rv, crv;
    327 	u_int32_t chids;
    328 
    329 	rv = 0;
    330 	chids = pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CHIDS);
    331 	for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
    332 		cp = &sc->pciide_channels[i];
    333 		wdc_cp = &cp->wdc_channel;
    334 		/* If a compat channel skip. */
    335 		if (cp->compat)
    336 			continue;
    337 		if (chids & ACER_CHIDS_INT(i)) {
    338 			crv = wdcintr(wdc_cp);
    339 			if (crv == 0)
    340 				printf("%s:%d: bogus intr\n",
    341 				    sc->sc_wdcdev.sc_dev.dv_xname, i);
    342 			else
    343 				rv = 1;
    344 		}
    345 	}
    346 	return rv;
    347 }
    348