ata-dma.c revision 108931
145095Ssos/*-
289917Ssos * Copyright (c) 1998,1999,2000,2001,2002 S�ren Schmidt <sos@FreeBSD.org>
345095Ssos * All rights reserved.
445095Ssos *
545095Ssos * Redistribution and use in source and binary forms, with or without
645095Ssos * modification, are permitted provided that the following conditions
745095Ssos * are met:
845095Ssos * 1. Redistributions of source code must retain the above copyright
945095Ssos *    notice, this list of conditions and the following disclaimer,
1045095Ssos *    without modification, immediately at the beginning of the file.
1145095Ssos * 2. Redistributions in binary form must reproduce the above copyright
1245095Ssos *    notice, this list of conditions and the following disclaimer in the
1345095Ssos *    documentation and/or other materials provided with the distribution.
1445095Ssos * 3. The name of the author may not be used to endorse or promote products
1545095Ssos *    derived from this software without specific prior written permission.
1645095Ssos *
1745095Ssos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1845095Ssos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1945095Ssos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2045095Ssos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2145095Ssos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2245095Ssos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2345095Ssos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2445095Ssos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2545095Ssos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2645095Ssos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2745095Ssos *
2850477Speter * $FreeBSD: head/sys/dev/ata/ata-dma.c 108931 2003-01-08 10:03:31Z sos $
2945095Ssos */
3045095Ssos
3145095Ssos#include <sys/param.h>
3245095Ssos#include <sys/systm.h>
3374302Ssos#include <sys/ata.h>
3495533Smike#include <sys/endian.h>
3545095Ssos#include <sys/malloc.h>
3645798Ssos#include <sys/bus.h>
3745095Ssos#include <pci/pcivar.h>
3866106Ssos#include <machine/bus.h>
3972106Ssos#include <sys/rman.h>
4045095Ssos#include <dev/ata/ata-all.h>
4145095Ssos
4252067Ssos/* prototypes */
4393882Ssosstatic void ata_dmacreate(struct ata_device *, int, int);
4493882Ssosstatic void ata_dmasetupd_cb(void *, bus_dma_segment_t *, int, int);
4593882Ssosstatic void ata_dmasetupc_cb(void *, bus_dma_segment_t *, int, int);
4693882Ssosstatic void cyrix_timing(struct ata_device *, int, int);
4793882Ssosstatic void promise_timing(struct ata_device *, int, int);
4893882Ssosstatic void hpt_timing(struct ata_device *, int, int);
4993882Ssosstatic int hpt_cable80(struct ata_device *);
5052067Ssos
5152067Ssos/* misc defines */
5293882Ssos#define ATAPI_DEVICE(atadev) \
5393882Ssos	((atadev->unit == ATA_MASTER && \
5493882Ssos	  atadev->channel->devices & ATA_ATAPI_MASTER) || \
5593882Ssos	 (atadev->unit == ATA_SLAVE && \
5693882Ssos	  atadev->channel->devices & ATA_ATAPI_SLAVE))
5745720Speter
5893882Ssos#define	MAXSEGSZ	PAGE_SIZE
5993882Ssos#define	MAXTABSZ	PAGE_SIZE
6093882Ssos#define	MAXCTLDMASZ	(2 * (MAXTABSZ + MAXPHYS))
6193882Ssos
6293882Ssosstruct ata_dc_cb_args {
6393882Ssos    bus_addr_t maddr;
6493882Ssos    int error;
6593882Ssos};
6693882Ssos
6793882Ssosstatic void
6893882Ssosata_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
6966070Ssos{
7093882Ssos    struct ata_dc_cb_args *cba = (struct ata_dc_cb_args *)xsc;
7166070Ssos
7293882Ssos    if (!(cba->error = error))
7393882Ssos	cba->maddr = segs[0].ds_addr;
7493882Ssos}
7593882Ssos
7693882Ssosint
7793882Ssosata_dmaalloc(struct ata_device *atadev)
7893882Ssos{
7993882Ssos    struct ata_channel *ch;
8093882Ssos    struct ata_dc_cb_args ccba;
8193882Ssos    struct ata_dmastate *ds;
8293882Ssos    int error;
8393882Ssos
8493882Ssos    ch = atadev->channel;
8593882Ssos    ds = &atadev->dmastate;
8693882Ssos    if (!ds->cdmatag) {
8793882Ssos	if ((error = bus_dma_tag_create(ch->dmatag, 1, PAGE_SIZE,
8893882Ssos					BUS_SPACE_MAXADDR_32BIT,
8993882Ssos					BUS_SPACE_MAXADDR, NULL, NULL,
9093882Ssos					MAXTABSZ, 1, MAXTABSZ,
9193882Ssos					BUS_DMA_ALLOCNOW, &ds->cdmatag)))
9293882Ssos	    return error;
9393882Ssos    }
9493882Ssos    if (!ds->ddmatag) {
9593882Ssos	if ((error = bus_dma_tag_create(ch->dmatag, ch->alignment + 1, 0,
9693882Ssos					BUS_SPACE_MAXADDR_32BIT,
9793882Ssos					BUS_SPACE_MAXADDR, NULL, NULL,
9893882Ssos					MAXPHYS, ATA_DMA_ENTRIES, MAXSEGSZ,
9993882Ssos					BUS_DMA_ALLOCNOW, &ds->ddmatag)))
10093882Ssos	    return error;
10193882Ssos    }
10293882Ssos    if (!ds->mdmatab) {
10393882Ssos	if ((error = bus_dmamem_alloc(ds->cdmatag, (void **)&ds->dmatab, 0,
10493882Ssos				      &ds->cdmamap)))
10593882Ssos	    return error;
10693882Ssos
10793882Ssos	if ((error = bus_dmamap_load(ds->cdmatag, ds->cdmamap, ds->dmatab,
10893882Ssos				     MAXTABSZ, ata_dmasetupc_cb, &ccba,
10993882Ssos				     0)) != 0 || ccba.error != 0) {
11093882Ssos	    bus_dmamem_free(ds->cdmatag, ds->dmatab, ds->cdmamap);
11193882Ssos	    return error;
11266070Ssos	}
11393882Ssos	ds->mdmatab = ccba.maddr;
11466070Ssos    }
11593882Ssos    if (!ds->ddmamap) {
11693882Ssos	if ((error = bus_dmamap_create(ds->ddmatag, 0, &ds->ddmamap)) != 0)
11793882Ssos	    return error;
11893882Ssos    }
11993882Ssos    return 0;
12066070Ssos}
12166070Ssos
12256744Ssosvoid
12393882Ssosata_dmafree(struct ata_device *atadev)
12445095Ssos{
12593882Ssos    struct ata_dmastate *ds;
12693882Ssos
12793882Ssos    ds = &atadev->dmastate;
12893882Ssos    if (ds->mdmatab) {
12993882Ssos	bus_dmamap_unload(ds->cdmatag, ds->cdmamap);
13093882Ssos	bus_dmamem_free(ds->cdmatag, ds->dmatab, ds->cdmamap);
13193882Ssos	ds->mdmatab = 0;
13293882Ssos	ds->cdmamap = NULL;
13393882Ssos	ds->dmatab = NULL;
13493882Ssos    }
13593882Ssos    if (ds->ddmamap) {
13693882Ssos	bus_dmamap_destroy(ds->ddmatag, ds->ddmamap);
13793882Ssos	ds->ddmamap = NULL;
13893882Ssos    }
13993882Ssos    if (ds->cdmatag) {
14093882Ssos	bus_dma_tag_destroy(ds->cdmatag);
14193882Ssos	ds->cdmatag = NULL;
14293882Ssos    }
14393882Ssos    if (ds->ddmatag) {
14493882Ssos	bus_dma_tag_destroy(ds->ddmatag);
14593882Ssos	ds->ddmatag = NULL;
14693882Ssos    }
14793882Ssos}
14893882Ssos
14993882Ssosvoid
15093882Ssosata_dmafreetags(struct ata_channel *ch)
15193882Ssos{
15293882Ssos
15393882Ssos    if (ch->dmatag) {
15493882Ssos	bus_dma_tag_destroy(ch->dmatag);
15593882Ssos	ch->dmatag = NULL;
15693882Ssos    }
15793882Ssos}
15893882Ssos
15993882Ssosstatic void
16093882Ssosata_dmacreate(struct ata_device *atadev, int apiomode, int mode)
16193882Ssos{
16293882Ssos
16393882Ssos    atadev->mode = mode;
16493882Ssos    if (!atadev->channel->dmatag) {
16593882Ssos	if (bus_dma_tag_create(NULL, 1, 0,
16693882Ssos			       BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
16793882Ssos			       NULL, NULL, MAXCTLDMASZ, ATA_DMA_ENTRIES,
16893882Ssos			       BUS_SPACE_MAXSIZE_32BIT, 0,
16993882Ssos			       &atadev->channel->dmatag)) {
17093882Ssos	    ata_prtdev(atadev, "DMA tag allocation failed, disabling DMA\n");
17193882Ssos	    ata_dmainit(atadev, apiomode, -1, -1);
17293882Ssos	}
17393882Ssos    }
17493882Ssos}
17593882Ssos
17693882Ssosvoid
17793882Ssosata_dmainit(struct ata_device *atadev, int apiomode, int wdmamode, int udmamode)
17893882Ssos{
17993882Ssos    device_t parent = device_get_parent(atadev->channel->dev);
18093882Ssos    int chiptype = atadev->channel->chiptype;
18193882Ssos    int chiprev = pci_get_revid(parent);
18293882Ssos    int channel = atadev->channel->unit;
18393882Ssos    int device = ATA_DEV(atadev->unit);
18493882Ssos    int devno = (channel << 1) + device;
18564307Ssos    int error;
18645095Ssos
18756744Ssos    /* set our most pessimistic default mode */
18890215Ssos    atadev->mode = ATA_PIO;
18956744Ssos
19093882Ssos    if (!atadev->channel->r_bmio)
19156744Ssos	return;
19245095Ssos
19352067Ssos    /* if simplex controller, only allow DMA on primary channel */
19493882Ssos    if (channel == 1) {
19593882Ssos	ATA_OUTB(atadev->channel->r_bmio, ATA_BMSTAT_PORT,
19693882Ssos		 ATA_INB(atadev->channel->r_bmio, ATA_BMSTAT_PORT) &
19772106Ssos		 (ATA_BMSTAT_DMA_MASTER | ATA_BMSTAT_DMA_SLAVE));
19893882Ssos	if (ATA_INB(atadev->channel->r_bmio, ATA_BMSTAT_PORT) &
19993882Ssos	    ATA_BMSTAT_DMA_SIMPLEX) {
20090215Ssos	    ata_prtdev(atadev, "simplex device, DMA on primary only\n");
20156744Ssos	    return;
20252067Ssos	}
20352067Ssos    }
20452067Ssos
20566070Ssos    /* DMA engine address alignment is usually 1 word (2 bytes) */
20693882Ssos    atadev->channel->alignment = 0x1;
20745095Ssos
20885345Ssos#if 1
20993882Ssos    if (udmamode > 2 && !atadev->param->hwres_cblid) {
21090215Ssos	ata_prtdev(atadev,"DMA limited to UDMA33, non-ATA66 cable or device\n");
21160829Ssos	udmamode = 2;
21260829Ssos    }
21385345Ssos#endif
21493882Ssos    switch (chiptype) {
21545095Ssos
216100380Sjhb    case 0x24cb8086:	/* Intel ICH4 */
21785352Ssos    case 0x248a8086:	/* Intel ICH3 mobile */
21885352Ssos    case 0x248b8086:	/* Intel ICH3 */
21975553Ssos    case 0x244a8086:	/* Intel ICH2 mobile */
22064307Ssos    case 0x244b8086:	/* Intel ICH2 */
22164307Ssos	if (udmamode >= 5) {
22264307Ssos	    int32_t mask48, new48;
22364307Ssos	    int16_t word54;
22464307Ssos
22564307Ssos	    word54 = pci_read_config(parent, 0x54, 2);
22664307Ssos	    if (word54 & (0x10 << devno)) {
22790215Ssos		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
22890215Ssos				    ATA_UDMA5,	ATA_C_F_SETXFER,ATA_WAIT_READY);
22990215Ssos		if (bootverbose)
23090215Ssos		    ata_prtdev(atadev, "%s setting UDMA5 on Intel chip\n",
23164307Ssos			       (error) ? "failed" : "success");
23264307Ssos		if (!error) {
23364307Ssos		    mask48 = (1 << devno) + (3 << (16 + (devno << 2)));
23464307Ssos		    new48 = (1 << devno) + (1 << (16 + (devno << 2)));
23564307Ssos		    pci_write_config(parent, 0x48,
23664307Ssos				     (pci_read_config(parent, 0x48, 4) &
23764307Ssos				     ~mask48) | new48, 4);
23890215Ssos		    pci_write_config(parent, 0x54, word54 | (0x1000<<devno), 2);
23993882Ssos		    ata_dmacreate(atadev, apiomode, ATA_UDMA5);
24064307Ssos		    return;
24164307Ssos		}
24264307Ssos	    }
24364307Ssos	}
24464479Ssos	/* make sure eventual ATA100 mode from the BIOS is disabled */
24564479Ssos	pci_write_config(parent, 0x54,
24664479Ssos			 pci_read_config(parent, 0x54, 2) & ~(0x1000<<devno),2);
24764307Ssos	/* FALLTHROUGH */
24864307Ssos
24990215Ssos    case 0x24118086:	/* Intel ICH */
25090215Ssos    case 0x76018086:	/* Intel ICH */
25157391Ssos	if (udmamode >= 4) {
25257391Ssos	    int32_t mask48, new48;
25357391Ssos	    int16_t word54;
25457391Ssos
25557391Ssos	    word54 = pci_read_config(parent, 0x54, 2);
25657391Ssos	    if (word54 & (0x10 << devno)) {
25790215Ssos		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
25890215Ssos				    ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
25990215Ssos		if (bootverbose)
26090215Ssos		    ata_prtdev(atadev, "%s setting UDMA4 on Intel chip\n",
26175553Ssos			       (error) ? "failed" : "success");
26257391Ssos		if (!error) {
26357391Ssos		    mask48 = (1 << devno) + (3 << (16 + (devno << 2)));
26457391Ssos		    new48 = (1 << devno) + (2 << (16 + (devno << 2)));
26557391Ssos		    pci_write_config(parent, 0x48,
26657391Ssos				     (pci_read_config(parent, 0x48, 4) &
26757391Ssos				     ~mask48) | new48, 4);
26857391Ssos		    pci_write_config(parent, 0x54, word54 | (1 << devno), 2);
26993882Ssos		    ata_dmacreate(atadev, apiomode, ATA_UDMA4);
27057391Ssos		    return;
27157391Ssos		}
27257391Ssos	    }
27390215Ssos	}
27464479Ssos	/* make sure eventual ATA66 mode from the BIOS is disabled */
27564479Ssos	pci_write_config(parent, 0x54,
27664479Ssos			 pci_read_config(parent, 0x54, 2) & ~(1 << devno), 2);
27757391Ssos	/* FALLTHROUGH */
27857391Ssos
27945095Ssos    case 0x71118086:	/* Intel PIIX4 */
28085352Ssos    case 0x84CA8086:	/* Intel PIIX4 */
28156138Ssos    case 0x71998086:	/* Intel PIIX4e */
28256138Ssos    case 0x24218086:	/* Intel ICH0 */
28345095Ssos	if (udmamode >= 2) {
28451520Ssos	    int32_t mask48, new48;
28545095Ssos
28690215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
28753029Ssos				ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
28851520Ssos	    if (bootverbose)
28990215Ssos		ata_prtdev(atadev, "%s setting UDMA2 on Intel chip\n",
29075553Ssos			   (error) ? "failed" : "success");
29153681Ssos	    if (!error) {
29253681Ssos		mask48 = (1 << devno) + (3 << (16 + (devno << 2)));
29353681Ssos		new48 = (1 << devno) + (2 << (16 + (devno << 2)));
29457325Ssos		pci_write_config(parent, 0x48,
29557325Ssos				 (pci_read_config(parent, 0x48, 4) &
29653681Ssos				 ~mask48) | new48, 4);
29793882Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA2);
29856744Ssos		return;
29953681Ssos	    }
30045095Ssos	}
30164479Ssos	/* make sure eventual ATA33 mode from the BIOS is disabled */
30264479Ssos	pci_write_config(parent, 0x48,
30364479Ssos			 pci_read_config(parent, 0x48, 4) & ~(1 << devno), 4);
30445095Ssos	/* FALLTHROUGH */
30545095Ssos
30645095Ssos    case 0x70108086:	/* Intel PIIX3 */
30745095Ssos	if (wdmamode >= 2 && apiomode >= 4) {
30845095Ssos	    int32_t mask40, new40, mask44, new44;
30945095Ssos
31045095Ssos	    /* if SITRE not set doit for both channels */
31193882Ssos	    if (!((pci_read_config(parent, 0x40, 4) >> (channel<<8)) & 0x4000)){
31257325Ssos		new40 = pci_read_config(parent, 0x40, 4);
31357325Ssos		new44 = pci_read_config(parent, 0x44, 4);
31451520Ssos		if (!(new40 & 0x00004000)) {
31551520Ssos		    new44 &= ~0x0000000f;
31651520Ssos		    new44 |= ((new40&0x00003000)>>10)|((new40&0x00000300)>>8);
31751520Ssos		}
31851520Ssos		if (!(new40 & 0x40000000)) {
31951520Ssos		    new44 &= ~0x000000f0;
32051520Ssos		    new44 |= ((new40&0x30000000)>>22)|((new40&0x03000000)>>20);
32151520Ssos		}
32251520Ssos		new40 |= 0x40004000;
32357325Ssos		pci_write_config(parent, 0x40, new40, 4);
32457325Ssos		pci_write_config(parent, 0x44, new44, 4);
32545095Ssos	    }
32690215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
32753029Ssos				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
32851520Ssos	    if (bootverbose)
32990215Ssos		ata_prtdev(atadev, "%s setting WDMA2 on Intel chip\n",
33075553Ssos			   (error) ? "failed" : "success");
33153681Ssos	    if (!error) {
33253681Ssos		if (device == ATA_MASTER) {
33353681Ssos		    mask40 = 0x0000330f;
33453681Ssos		    new40 = 0x00002307;
33553681Ssos		    mask44 = 0;
33653681Ssos		    new44 = 0;
33753681Ssos		}
33853681Ssos		else {
33953681Ssos		    mask40 = 0x000000f0;
34053681Ssos		    new40 = 0x00000070;
34153681Ssos		    mask44 = 0x0000000f;
34253681Ssos		    new44 = 0x0000000b;
34353681Ssos		}
34493882Ssos		if (channel) {
34553681Ssos		    mask40 <<= 16;
34653681Ssos		    new40 <<= 16;
34753681Ssos		    mask44 <<= 4;
34853681Ssos		    new44 <<= 4;
34953681Ssos		}
35057325Ssos		pci_write_config(parent, 0x40,
35157325Ssos				 (pci_read_config(parent, 0x40, 4) & ~mask40)|
35290215Ssos				 new40, 4);
35357325Ssos		pci_write_config(parent, 0x44,
35457325Ssos				 (pci_read_config(parent, 0x44, 4) & ~mask44)|
35590215Ssos				 new44, 4);
35693882Ssos		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
35756744Ssos		return;
35845095Ssos	    }
35951520Ssos	}
36053681Ssos	/* we could set PIO mode timings, but we assume the BIOS did that */
36145095Ssos	break;
36245095Ssos
36345095Ssos    case 0x12308086:	/* Intel PIIX */
36454544Ssos	if (wdmamode >= 2 && apiomode >= 4) {
36554544Ssos	    int32_t word40;
36654544Ssos
36757325Ssos	    word40 = pci_read_config(parent, 0x40, 4);
36893882Ssos	    word40 >>= channel * 16;
36954544Ssos
37054544Ssos	    /* Check for timing config usable for DMA on controller */
37154544Ssos	    if (!((word40 & 0x3300) == 0x2300 &&
37293882Ssos		  ((word40 >> (device ? 4 : 0)) & 1) == 1))
37354544Ssos		break;
37454544Ssos
37590215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
37654544Ssos				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
37754544Ssos	    if (bootverbose)
37890215Ssos		ata_prtdev(atadev, "%s setting WDMA2 on Intel chip\n",
37956558Ssos			   (error) ? "failed" : "success");
38054544Ssos	    if (!error) {
38193882Ssos		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
38256744Ssos		return;
38354544Ssos	    }
38454544Ssos	}
38545095Ssos	break;
38645095Ssos
38752067Ssos    case 0x522910b9:	/* AcerLabs Aladdin IV/V */
38875271Ssos	/* the older Aladdin doesn't support ATAPI DMA on both master & slave */
38993882Ssos	if (chiprev < 0xc2 &&
39093882Ssos	    atadev->channel->devices & ATA_ATAPI_MASTER &&
39193882Ssos	    atadev->channel->devices & ATA_ATAPI_SLAVE) {
39290215Ssos	    ata_prtdev(atadev, "two atapi devices on this channel, no DMA\n");
39353029Ssos	    break;
39452067Ssos	}
39593882Ssos	pci_write_config(parent, 0x58 + (channel << 2), 0x00310001, 4);
39693882Ssos	if (udmamode >= 5 && chiprev >= 0xc4) {
39790215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
39875271Ssos				ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
39975271Ssos	    if (bootverbose)
40090215Ssos		ata_prtdev(atadev, "%s setting UDMA5 on Acer chip\n",
40175271Ssos			   (error) ? "failed" : "success");
40275271Ssos	    if (!error) {
40375271Ssos		int32_t word54 = pci_read_config(parent, 0x54, 4);
40452067Ssos
40575271Ssos		pci_write_config(parent, 0x4b,
40675271Ssos				 pci_read_config(parent, 0x4b, 1) | 0x01, 1);
40775271Ssos		word54 &= ~(0x000f000f << (devno << 2));
40875271Ssos		word54 |= (0x000f0005 << (devno << 2));
40975271Ssos		pci_write_config(parent, 0x54, word54, 4);
41075271Ssos		pci_write_config(parent, 0x53,
41175271Ssos				 pci_read_config(parent, 0x53, 1) | 0x03, 1);
41293882Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA5);
41375271Ssos		return;
41475271Ssos	    }
41575271Ssos	}
41693882Ssos	if (udmamode >= 4 && chiprev >= 0xc2) {
41790215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
41875271Ssos				ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
41975271Ssos	    if (bootverbose)
42090215Ssos		ata_prtdev(atadev, "%s setting UDMA4 on Acer chip\n",
42175271Ssos			   (error) ? "failed" : "success");
42275271Ssos	    if (!error) {
42375271Ssos		int32_t word54 = pci_read_config(parent, 0x54, 4);
42475271Ssos
42575271Ssos		pci_write_config(parent, 0x4b,
42675271Ssos				 pci_read_config(parent, 0x4b, 1) | 0x01, 1);
42775271Ssos		word54 &= ~(0x000f000f << (devno << 2));
42875271Ssos		word54 |= (0x00080005 << (devno << 2));
42975271Ssos		pci_write_config(parent, 0x54, word54, 4);
43075271Ssos		pci_write_config(parent, 0x53,
43175271Ssos				 pci_read_config(parent, 0x53, 1) | 0x03, 1);
43293882Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA4);
43375271Ssos		return;
43475271Ssos	    }
43575271Ssos	}
43693882Ssos	if (udmamode >= 2 && chiprev >= 0x20) {
43790215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
43853029Ssos				ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
43951520Ssos	    if (bootverbose)
44090215Ssos		ata_prtdev(atadev, "%s setting UDMA2 on Acer chip\n",
44156558Ssos			   (error) ? "failed" : "success");
44253681Ssos	    if (!error) {
44375271Ssos		int32_t word54 = pci_read_config(parent, 0x54, 4);
44475271Ssos
44560829Ssos		word54 &= ~(0x000f000f << (devno << 2));
44660829Ssos		word54 |= (0x000a0005 << (devno << 2));
44757325Ssos		pci_write_config(parent, 0x54, word54, 4);
44857325Ssos		pci_write_config(parent, 0x53,
44957325Ssos				 pci_read_config(parent, 0x53, 1) | 0x03, 1);
45093882Ssos		atadev->channel->flags |= ATA_ATAPI_DMA_RO;
45193882Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA2);
45256744Ssos		return;
45353681Ssos	    }
45451520Ssos	}
45570186Ssos
45670186Ssos	/* make sure eventual UDMA mode from the BIOS is disabled */
45770752Ssos	pci_write_config(parent, 0x56, pci_read_config(parent, 0x56, 2) &
45870752Ssos				       ~(0x0008 << (devno << 2)), 2);
45970186Ssos
46053681Ssos	if (wdmamode >= 2 && apiomode >= 4) {
46190215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
46253029Ssos				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
46352067Ssos	    if (bootverbose)
46490215Ssos		ata_prtdev(atadev, "%s setting WDMA2 on Acer chip\n",
46556558Ssos			   (error) ? "failed" : "success");
46653681Ssos	    if (!error) {
46757325Ssos		pci_write_config(parent, 0x53,
46857325Ssos				 pci_read_config(parent, 0x53, 1) | 0x03, 1);
46993882Ssos		atadev->channel->flags |= ATA_ATAPI_DMA_RO;
47093882Ssos		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
47156744Ssos		return;
47253681Ssos	    }
47352067Ssos	}
47457771Ssos	pci_write_config(parent, 0x53,
47557771Ssos			 (pci_read_config(parent, 0x53, 1) & ~0x01) | 0x02, 1);
47693882Ssos	error = ata_command(atadev, ATA_C_SETFEATURES, 0,
47793882Ssos			    ATA_PIO0 + apiomode,
47893882Ssos			    ATA_C_F_SETXFER, ATA_WAIT_READY);
47993882Ssos	if (bootverbose)
48093882Ssos	    ata_prtdev(atadev, "%s setting PIO%d on Acer chip\n",
48193882Ssos		       (error) ? "failed" : "success",
48293882Ssos		       (apiomode >= 0) ? apiomode : 0);
48393882Ssos	if (!error) {
48493882Ssos	    int32_t word54 = pci_read_config(parent, 0x54, 4);
48593882Ssos	    int32_t timing;
48693882Ssos
48793882Ssos	    switch(ATA_PIO0 + apiomode) {
48893882Ssos	    case ATA_PIO0: timing = 0x006d0003;
48993882Ssos	    case ATA_PIO1: timing = 0x00580002;
49093882Ssos	    case ATA_PIO2: timing = 0x00440001;
49193882Ssos	    case ATA_PIO3: timing = 0x00330001;
49293882Ssos	    case ATA_PIO4: timing = 0x00310001;
49393882Ssos	    default:	   timing = 0x006d0003;
49493882Ssos	    }
49593882Ssos	    pci_write_config(parent, 0x58 + (channel << 2), timing, 4);
49693882Ssos	    word54 &= ~(0x000f000f << (devno << 2));
49793882Ssos	    word54 |= (0x00000004 << (devno << 2));
49893882Ssos	    pci_write_config(parent, 0x54, word54, 4);
49993882Ssos	    atadev->mode = ATA_PIO0 + apiomode;
50093882Ssos	    return;
50193882Ssos	}
50252067Ssos	break;
50352067Ssos
50494826Ssos    case 0x01bc10de:	/* nVIDIA nForce */
50593094Ssos    case 0x74411022:	/* AMD 768 */
50676584Ssos    case 0x74111022:	/* AMD 766 */
50759103Ssos    case 0x74091022:	/* AMD 756 */
508103535Ssos    case 0x05711106:	/* VIA 82C571, 82C586, 82C596, 82C686, 8231,8233,8235 */
50989917Ssos	{
51094826Ssos	    int via_modes[5][7] = {
51194826Ssos		{ 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00 },	/* VIA ATA33 */
51294826Ssos		{ 0x00, 0x00, 0xea, 0x00, 0xe8, 0x00, 0x00 },	/* VIA ATA66 */
51394826Ssos		{ 0x00, 0x00, 0xf4, 0x00, 0xf1, 0xf0, 0x00 },	/* VIA ATA100 */
51494826Ssos		{ 0x00, 0x00, 0xf6, 0x00, 0xf2, 0xf1, 0xf0 },	/* VIA ATA133 */
51594826Ssos		{ 0x00, 0x00, 0xc0, 0x00, 0xc5, 0xc6, 0x00 }};	/* AMD/nVIDIA */
51689917Ssos	    int *reg_val = NULL;
51794826Ssos	    char *chip = "VIA";
51889917Ssos
519103535Ssos	    if (ata_find_dev(parent, 0x31471106, 0) ||		/* 8233a */
520103535Ssos		ata_find_dev(parent, 0x31771106, 0)) {		/* 8235 */
52189917Ssos		udmamode = imin(udmamode, 6);
52289917Ssos		reg_val = via_modes[3];
52371156Ssos	    }
52489917Ssos	    else if (ata_find_dev(parent, 0x06861106, 0x40) ||	/* 82C686b */
52589917Ssos		ata_find_dev(parent, 0x82311106, 0) ||		/* 8231 */
52689917Ssos		ata_find_dev(parent, 0x30741106, 0) ||		/* 8233 */
52789917Ssos		ata_find_dev(parent, 0x31091106, 0)) {		/* 8233c */
52889917Ssos		udmamode = imin(udmamode, 5);
52989917Ssos		reg_val = via_modes[2];
53089917Ssos	    }
53189917Ssos	    else if (ata_find_dev(parent, 0x06861106, 0x10) ||	/* 82C686a */
53289917Ssos		     ata_find_dev(parent, 0x05961106, 0x12)) {	/* 82C596b */
53389917Ssos		udmamode = imin(udmamode, 4);
53489917Ssos		reg_val = via_modes[1];
53589917Ssos	    }
53693674Ssos	    else if (ata_find_dev(parent, 0x06861106, 0)) {	/* 82C686 */
53789917Ssos		udmamode = imin(udmamode, 2);
53889917Ssos		reg_val = via_modes[1];
53989917Ssos	    }
54089917Ssos	    else if (ata_find_dev(parent, 0x05961106, 0) ||	/* 82C596a */
54189917Ssos		     ata_find_dev(parent, 0x05861106, 0x03)) {	/* 82C586b */
54289917Ssos		udmamode = imin(udmamode, 2);
54389917Ssos		reg_val = via_modes[0];
54489917Ssos	    }
54594826Ssos	    else if (chiptype == 0x74411022 ||			/* AMD 768 */
54694826Ssos		     chiptype == 0x74111022) {			/* AMD 766 */
54794826Ssos		udmamode = imin(udmamode, 5);
54894826Ssos		reg_val = via_modes[4];
54994826Ssos		chip = "AMD";
55094826Ssos	    }
55194826Ssos	    else if (chiptype == 0x74091022) {			/* AMD 756 */
55294826Ssos		udmamode = imin(udmamode, 4);
55394826Ssos		reg_val = via_modes[4];
55494826Ssos		chip = "AMD";
55594826Ssos	    }
55694826Ssos	    else if (chiptype == 0x01bc10de) {			/* nVIDIA */
55794826Ssos		udmamode = imin(udmamode, 5);
55894826Ssos		reg_val = via_modes[4];
55994826Ssos		chip = "nVIDIA";
56094826Ssos	    }
56194826Ssos	    else
56289917Ssos		udmamode = 0;
56394826Ssos
56489917Ssos	    if (udmamode >= 6) {
56590215Ssos		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
56689917Ssos				    ATA_UDMA6, ATA_C_F_SETXFER, ATA_WAIT_READY);
56764695Ssos		if (bootverbose)
56894826Ssos		    ata_prtdev(atadev, "%s setting UDMA6 on %s chip\n",
56994826Ssos			       (error) ? "failed" : "success", chip);
57064695Ssos		if (!error) {
57189917Ssos		    pci_write_config(parent, 0x53 - devno, reg_val[6], 1);
57293882Ssos		    ata_dmacreate(atadev, apiomode, ATA_UDMA6);
57371156Ssos		    return;
57471156Ssos		}
57571156Ssos	    }
57689917Ssos	    if (udmamode >= 5) {
57790215Ssos		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
57889917Ssos				    ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
57971156Ssos		if (bootverbose)
58094826Ssos		    ata_prtdev(atadev, "%s setting UDMA5 on %s chip\n",
58194826Ssos			       (error) ? "failed" : "success", chip);
58271156Ssos		if (!error) {
58389917Ssos		    pci_write_config(parent, 0x53 - devno, reg_val[5], 1);
58493882Ssos		    ata_dmacreate(atadev, apiomode, ATA_UDMA5);
58571156Ssos		    return;
58671156Ssos		}
58771156Ssos	    }
58871156Ssos	    if (udmamode >= 4) {
58990215Ssos		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
59071156Ssos				    ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
59171156Ssos		if (bootverbose)
59294826Ssos		    ata_prtdev(atadev, "%s setting UDMA4 on %s chip\n",
59394826Ssos			       (error) ? "failed" : "success", chip);
59471156Ssos		if (!error) {
59589917Ssos		    pci_write_config(parent, 0x53 - devno, reg_val[4], 1);
59693882Ssos		    ata_dmacreate(atadev, apiomode, ATA_UDMA4);
59764695Ssos		    return;
59864695Ssos		}
59956138Ssos	    }
60064695Ssos	    if (udmamode >= 2) {
60190215Ssos		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
60264695Ssos				    ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
60364695Ssos		if (bootverbose)
60494826Ssos		    ata_prtdev(atadev, "%s setting UDMA2 on %s chip\n",
60594826Ssos			       (error) ? "failed" : "success", chip);
60664695Ssos		if (!error) {
60789917Ssos		    pci_write_config(parent, 0x53 - devno, reg_val[2], 1);
60893882Ssos		    ata_dmacreate(atadev, apiomode, ATA_UDMA2);
60964695Ssos		    return;
61064695Ssos		}
61156138Ssos	    }
61294826Ssos	    if (wdmamode >= 2 && apiomode >= 4) {
61394826Ssos		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
61494826Ssos				    ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
61594826Ssos		if (bootverbose)
61694826Ssos		    ata_prtdev(atadev, "%s setting WDMA2 on %s chip\n",
61794826Ssos			       (error) ? "failed" : "success", chip);
61894826Ssos		if (!error) {
61994826Ssos		    pci_write_config(parent, 0x53 - devno, 0x0b, 1);
62094826Ssos		    pci_write_config(parent, 0x4b - devno, 0x31, 1);
62194826Ssos		    ata_dmacreate(atadev, apiomode, ATA_WDMA2);
62294826Ssos		    return;
62394826Ssos		}
62453681Ssos	    }
62553681Ssos	}
62653681Ssos	/* we could set PIO mode timings, but we assume the BIOS did that */
62753681Ssos	break;
62853681Ssos
62954544Ssos    case 0x55131039:	/* SiS 5591 */
63090215Ssos	if (ata_find_dev(parent, 0x06301039, 0x30) ||	/* SiS 630 */
63193674Ssos	    ata_find_dev(parent, 0x06331039, 0) ||	/* SiS 633 */
63293674Ssos	    ata_find_dev(parent, 0x06351039, 0) ||	/* SiS 635 */
63393674Ssos	    ata_find_dev(parent, 0x06401039, 0) ||	/* SiS 640 */
63493674Ssos	    ata_find_dev(parent, 0x06451039, 0) ||	/* SiS 645 */
63593674Ssos	    ata_find_dev(parent, 0x06501039, 0) ||	/* SiS 650 */
63693674Ssos	    ata_find_dev(parent, 0x07301039, 0) ||	/* SiS 730 */
63793674Ssos	    ata_find_dev(parent, 0x07331039, 0) ||	/* SiS 733 */
63893674Ssos	    ata_find_dev(parent, 0x07351039, 0) ||	/* SiS 735 */
63993674Ssos	    ata_find_dev(parent, 0x07401039, 0) ||	/* SiS 740 */
64093674Ssos	    ata_find_dev(parent, 0x07451039, 0) ||	/* SiS 745 */
64193674Ssos	    ata_find_dev(parent, 0x07501039, 0)) {	/* SiS 750 */
64287198Ssos	    int8_t reg = 0x40 + (devno << 1);
64387951Ssos	    int16_t val = pci_read_config(parent, reg, 2) & 0x0fff;
64487198Ssos
64587198Ssos	    if (udmamode >= 5) {
64690215Ssos		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
64787198Ssos				    ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
64887198Ssos		if (bootverbose)
64990215Ssos		    ata_prtdev(atadev, "%s setting UDMA5 on SiS chip\n",
65087198Ssos			       (error) ? "failed" : "success");
65187198Ssos		if (!error) {
65287951Ssos		    pci_write_config(parent, reg, val | 0x8000, 2);
65393882Ssos		    ata_dmacreate(atadev, apiomode, ATA_UDMA5);
65487198Ssos		    return;
65587198Ssos		}
65654544Ssos	    }
65787198Ssos	    if (udmamode >= 4) {
65890215Ssos		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
65987198Ssos				    ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
66087198Ssos		if (bootverbose)
66190215Ssos		    ata_prtdev(atadev, "%s setting UDMA4 on SiS chip\n",
66287198Ssos			       (error) ? "failed" : "success");
66387198Ssos		if (!error) {
66487951Ssos		    pci_write_config(parent, reg, val | 0x9000, 2);
66593882Ssos		    ata_dmacreate(atadev, apiomode, ATA_UDMA4);
66687198Ssos		    return;
66787198Ssos		}
66887198Ssos	    }
66987198Ssos	    if (udmamode >= 2) {
67090215Ssos		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
67187198Ssos				    ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
67287198Ssos		if (bootverbose)
67390215Ssos		    ata_prtdev(atadev, "%s setting UDMA2 on SiS chip\n",
67487198Ssos			       (error) ? "failed" : "success");
67587198Ssos		if (!error) {
67687951Ssos		    pci_write_config(parent, reg, val | 0xb000, 2);
67793882Ssos		    ata_dmacreate(atadev, apiomode, ATA_UDMA2);
67887198Ssos		    return;
67987198Ssos		}
68087198Ssos	    }
68187198Ssos	} else if (ata_find_dev(parent, 0x05301039, 0) || /* SiS 530 */
68287198Ssos		   ata_find_dev(parent, 0x05401039, 0) || /* SiS 540 */
68387198Ssos		   ata_find_dev(parent, 0x06201039, 0) || /* SiS 620 */
68487198Ssos		   ata_find_dev(parent, 0x06301039, 0)) { /* SiS 630 */
68587198Ssos	    int8_t reg = 0x40 + (devno << 1);
68687198Ssos	    int16_t val = pci_read_config(parent, reg, 2) & 0x0fff;
68787198Ssos
68887198Ssos	    if (udmamode >= 4) {
68990215Ssos		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
69087198Ssos				    ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
69187198Ssos		if (bootverbose)
69290215Ssos		    ata_prtdev(atadev, "%s setting UDMA4 on SiS chip\n",
69387198Ssos			       (error) ? "failed" : "success");
69487198Ssos		if (!error) {
69587198Ssos		    pci_write_config(parent, reg, val | 0x9000, 2);
69693882Ssos		    ata_dmacreate(atadev, apiomode, ATA_UDMA4);
69787198Ssos		    return;
69887198Ssos		}
69987198Ssos	    }
70087198Ssos	    if (udmamode >= 2) {
70190215Ssos		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
70287198Ssos				    ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
70387198Ssos		if (bootverbose)
70490215Ssos		    ata_prtdev(atadev, "%s setting UDMA2 on SiS chip\n",
70587198Ssos			       (error) ? "failed" : "success");
70687198Ssos		if (!error) {
70787951Ssos		    pci_write_config(parent, reg, val | 0xa000, 2);
70893882Ssos		    ata_dmacreate(atadev, apiomode, ATA_UDMA2);
70987198Ssos		    return;
71087198Ssos		}
71187198Ssos	    }
71293882Ssos	} else if (udmamode >= 2 && chiprev > 0xc1) {
71390215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
71488478Ssos				ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
71588478Ssos	    if (bootverbose)
71690215Ssos		ata_prtdev(atadev, "%s setting UDMA2 on SiS chip\n",
71788478Ssos			   (error) ? "failed" : "success");
71888478Ssos	    if (!error) {
71988478Ssos		pci_write_config(parent, 0x40 + (devno << 1), 0xa301, 2);
72093882Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA2);
72188478Ssos		return;
72287198Ssos	    }
72354544Ssos	}
72454544Ssos	if (wdmamode >=2 && apiomode >= 4) {
72590215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
72654544Ssos				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
72754544Ssos	    if (bootverbose)
72890215Ssos		ata_prtdev(atadev, "%s setting WDMA2 on SiS chip\n",
72956558Ssos			   (error) ? "failed" : "success");
73054544Ssos	    if (!error) {
73157325Ssos		pci_write_config(parent, 0x40 + (devno << 1), 0x0301, 2);
73293882Ssos		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
73356744Ssos		return;
73454544Ssos	    }
73554544Ssos	}
73654544Ssos	/* we could set PIO mode timings, but we assume the BIOS did that */
73754544Ssos	break;
73854544Ssos
739103255Ssos    case 0x06801095:	/* Sil 0680 ATA133 controller */
740103255Ssos	{
741103255Ssos	    u_int8_t ureg = 0xac + (device * 0x02) + (channel * 0x10);
742103255Ssos	    u_int8_t uval = pci_read_config(parent, ureg, 1);
743103255Ssos	    u_int8_t mreg = channel ? 0x84 : 0x80;
744103255Ssos	    u_int8_t mask = device ? 0x30 : 0x03;
745103255Ssos	    u_int8_t mode = pci_read_config(parent, mreg, 1);
746103255Ssos
747103255Ssos	    /* enable UDMA mode */
748103255Ssos	    pci_write_config(parent, mreg,
749103255Ssos			     (mode & ~mask) | (device ? 0x30 : 0x03), 1);
750103255Ssos    	    if (udmamode >= 6) {
751103255Ssos		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
752103255Ssos				    ATA_UDMA6, ATA_C_F_SETXFER, ATA_WAIT_READY);
753103255Ssos		if (bootverbose)
754103255Ssos		    ata_prtdev(atadev, "%s setting UDMA6 on Sil chip\n",
755103255Ssos			       (error) ? "failed" : "success");
756103255Ssos		if (!error) {
757103255Ssos		    pci_write_config(parent, ureg, (uval & 0x3f) | 0x01, 1);
758103255Ssos		    ata_dmacreate(atadev, apiomode, ATA_UDMA6);
759103255Ssos		    return;
760103255Ssos		}
761103255Ssos	    }
762103255Ssos    	    if (udmamode >= 5) {
763103255Ssos		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
764103255Ssos				    ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
765103255Ssos		if (bootverbose)
766103255Ssos		    ata_prtdev(atadev, "%s setting UDMA5 on Sil chip\n",
767103255Ssos			       (error) ? "failed" : "success");
768103255Ssos		if (!error) {
769103255Ssos		    pci_write_config(parent, ureg, (uval & 0x3f) | 0x02, 1);
770103255Ssos		    ata_dmacreate(atadev, apiomode, ATA_UDMA5);
771103255Ssos		    return;
772103255Ssos		}
773103255Ssos	    }
774103255Ssos    	    if (udmamode >= 4) {
775103255Ssos		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
776103255Ssos				    ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
777103255Ssos		if (bootverbose)
778103255Ssos		    ata_prtdev(atadev, "%s setting UDMA4 on Sil chip\n",
779103255Ssos			       (error) ? "failed" : "success");
780103255Ssos		if (!error) {
781103255Ssos		    pci_write_config(parent, ureg, (uval & 0x3f) | 0x03, 1);
782103255Ssos		    ata_dmacreate(atadev, apiomode, ATA_UDMA4);
783103255Ssos		    return;
784103255Ssos		}
785103255Ssos	    }
786103255Ssos    	    if (udmamode >= 2) {
787103255Ssos		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
788103255Ssos				    ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
789103255Ssos		if (bootverbose)
790103255Ssos		    ata_prtdev(atadev, "%s setting UDMA2 on Sil chip\n",
791103255Ssos			       (error) ? "failed" : "success");
792103255Ssos		if (!error) {
793103255Ssos		    pci_write_config(parent, ureg, (uval & 0x3f) | 0x07, 1);
794103255Ssos		    ata_dmacreate(atadev, apiomode, ATA_UDMA2);
795103255Ssos		    return;
796103255Ssos		}
797103255Ssos	    }
798103255Ssos
799103255Ssos	    /* disable UDMA mode and enable WDMA mode */
800103255Ssos	    pci_write_config(parent, mreg,
801103255Ssos			     (mode & ~mask) | (device ? 0x20 : 0x02), 1);
802103255Ssos	    if (wdmamode >= 2 && apiomode >= 4) {
803103255Ssos		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
804103255Ssos				    ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
805103255Ssos		if (bootverbose)
806103255Ssos		    ata_prtdev(atadev, "%s setting WDMA2 on Sil chip\n",
807103255Ssos			       (error) ? "failed" : "success");
808103255Ssos		if (!error) {
809103255Ssos		    pci_write_config(parent, ureg - 0x4, 0x10c1, 2);
810103255Ssos		    ata_dmacreate(atadev, apiomode, ATA_WDMA2);
811103255Ssos		    return;
812103255Ssos		}
813103255Ssos	    }
814103255Ssos
815103255Ssos	    /* restore PIO mode */
816103255Ssos	    pci_write_config(parent, mreg, mode, 1);
817103255Ssos	}
818103255Ssos	/* we could set PIO mode timings, but we assume the BIOS did that */
819103255Ssos	break;
820103255Ssos
82166583Ssos    case 0x06491095:	/* CMD 649 ATA100 controller */
82266583Ssos	if (udmamode >= 5) {
82366583Ssos	    u_int8_t umode;
82466583Ssos
82590215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
82666583Ssos				ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
82766583Ssos	    if (bootverbose)
82890215Ssos		ata_prtdev(atadev, "%s setting UDMA5 on CMD chip\n",
82966583Ssos			   (error) ? "failed" : "success");
83066583Ssos	    if (!error) {
83193882Ssos		umode = pci_read_config(parent, channel ? 0x7b : 0x73, 1);
83293882Ssos		umode &= ~(device ? 0xca : 0x35);
83393882Ssos		umode |= (device ? 0x0a : 0x05);
83493882Ssos		pci_write_config(parent, channel ? 0x7b : 0x73, umode, 1);
83593882Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA5);
83666583Ssos		return;
83766583Ssos	    }
83866583Ssos	}
83966583Ssos	/* FALLTHROUGH */
84066583Ssos
84166583Ssos    case 0x06481095:	/* CMD 648 ATA66 controller */
84266583Ssos	if (udmamode >= 4) {
84366583Ssos	    u_int8_t umode;
84466583Ssos
84590215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
84666583Ssos				ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
84766583Ssos	    if (bootverbose)
84890215Ssos		ata_prtdev(atadev, "%s setting UDMA4 on CMD chip\n",
84966583Ssos			   (error) ? "failed" : "success");
85066583Ssos	    if (!error) {
85193882Ssos		umode = pci_read_config(parent, channel ? 0x7b : 0x73, 1);
85293882Ssos		umode &= ~(device ? 0xca : 0x35);
85393882Ssos		umode |= (device ? 0x4a : 0x15);
85493882Ssos		pci_write_config(parent, channel ? 0x7b : 0x73, umode, 1);
85593882Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA4);
85666583Ssos		return;
85766583Ssos	    }
85866583Ssos	}
85966583Ssos	if (udmamode >= 2) {
86066583Ssos	    u_int8_t umode;
86166583Ssos
86290215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
86366583Ssos				ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
86466583Ssos	    if (bootverbose)
86590215Ssos		ata_prtdev(atadev, "%s setting UDMA2 on CMD chip\n",
86666583Ssos			   (error) ? "failed" : "success");
86766583Ssos	    if (!error) {
86893882Ssos		umode = pci_read_config(parent, channel ? 0x7b : 0x73, 1);
86993882Ssos		umode &= ~(device ? 0xca : 0x35);
87093882Ssos		umode |= (device ? 0x42 : 0x11);
87193882Ssos		pci_write_config(parent, channel ? 0x7b : 0x73, umode, 1);
87293882Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA2);
87366583Ssos		return;
87466583Ssos	    }
87566583Ssos	}
87666583Ssos	/* make sure eventual UDMA mode from the BIOS is disabled */
87793882Ssos	pci_write_config(parent, channel ? 0x7b : 0x73,
87893882Ssos			 pci_read_config(parent, channel ? 0x7b : 0x73, 1) &
87995010Ssos					 ~(device ? 0xca : 0x53), 1);
88066583Ssos	/* FALLTHROUGH */
88166583Ssos
88257325Ssos    case 0x06461095:	/* CMD 646 ATA controller */
88357325Ssos	if (wdmamode >= 2 && apiomode >= 4) {
88490215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
88557325Ssos				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
88657325Ssos	    if (bootverbose)
88790215Ssos		ata_prtdev(atadev, "%s setting WDMA2 on CMD chip\n",
88857325Ssos			   error ? "failed" : "success");
88957325Ssos	    if (!error) {
89057325Ssos		int32_t offset = (devno < 3) ? (devno << 1) : 7;
89157325Ssos
89257325Ssos		pci_write_config(parent, 0x54 + offset, 0x3f, 1);
89393882Ssos		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
89457325Ssos		return;
89557325Ssos	    }
89657325Ssos	}
89757325Ssos	/* we could set PIO mode timings, but we assume the BIOS did that */
89857325Ssos	break;
89957325Ssos
90057477Ssos    case 0xc6931080:	/* Cypress 82c693 ATA controller */
90157477Ssos	if (wdmamode >= 2 && apiomode >= 4) {
90290215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
90357477Ssos				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
90457477Ssos	    if (bootverbose)
90590215Ssos		ata_prtdev(atadev, "%s setting WDMA2 on Cypress chip\n",
90657477Ssos			   error ? "failed" : "success");
90757477Ssos	    if (!error) {
90893882Ssos		pci_write_config(atadev->channel->dev,
90993882Ssos				 channel ? 0x4e:0x4c, 0x2020, 2);
91093882Ssos		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
91157477Ssos		return;
91257477Ssos	    }
91357477Ssos	}
91457477Ssos	/* we could set PIO mode timings, but we assume the BIOS did that */
91557477Ssos	break;
91657477Ssos
91766583Ssos    case 0x01021078:	/* Cyrix 5530 ATA33 controller */
91893882Ssos	atadev->channel->alignment = 0xf;
91966070Ssos	if (udmamode >= 2) {
92090215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
92166070Ssos				ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
92266070Ssos	    if (bootverbose)
92390215Ssos		ata_prtdev(atadev, "%s setting UDMA2 on Cyrix chip\n",
92466070Ssos			   (error) ? "failed" : "success");
92566070Ssos	    if (!error) {
92693882Ssos		cyrix_timing(atadev, devno, ATA_UDMA2);
92793882Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA2);
92866070Ssos		return;
92966070Ssos	    }
93066070Ssos	}
93166070Ssos	if (wdmamode >= 2 && apiomode >= 4) {
93290215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
93366070Ssos				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
93466070Ssos	    if (bootverbose)
93590215Ssos		ata_prtdev(atadev, "%s setting WDMA2 on Cyrix chip\n",
93666070Ssos			   (error) ? "failed" : "success");
93766070Ssos	    if (!error) {
93893882Ssos		cyrix_timing(atadev, devno, ATA_WDMA2);
93993882Ssos		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
94066070Ssos		return;
94166070Ssos	    }
94266070Ssos	}
94390215Ssos	error = ata_command(atadev, ATA_C_SETFEATURES, 0,
94484584Ssos			    ATA_PIO0 + apiomode, ATA_C_F_SETXFER,
94566070Ssos			    ATA_WAIT_READY);
94666070Ssos	if (bootverbose)
94790215Ssos	    ata_prtdev(atadev, "%s setting %s on Cyrix chip\n",
94866070Ssos		       (error) ? "failed" : "success",
94984584Ssos		       ata_mode2str(ATA_PIO0 + apiomode));
95093882Ssos	cyrix_timing(atadev, devno, ATA_PIO0 + apiomode);
95190215Ssos	atadev->mode = ATA_PIO0 + apiomode;
95266070Ssos	return;
95366070Ssos
95492573Ssos    case 0x02121166:	/* ServerWorks CSB5 ATA66/100 controller */
95593882Ssos	if (udmamode >= 5 && chiprev >= 0x92) {
95692573Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
95792573Ssos				ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
95892573Ssos	    if (bootverbose)
95992573Ssos		ata_prtdev(atadev, "%s setting UDMA5 on ServerWorks chip\n",
96092573Ssos			   (error) ? "failed" : "success");
96192573Ssos	    if (!error) {
96292573Ssos		u_int16_t reg56;
96392573Ssos
96492573Ssos		pci_write_config(parent, 0x54,
96592573Ssos				 pci_read_config(parent, 0x54, 1) |
96692573Ssos				 (0x01 << devno), 1);
96792573Ssos		reg56 = pci_read_config(parent, 0x56, 2);
96892573Ssos		reg56 &= ~(0xf << (devno * 4));
96992573Ssos		reg56 |= (0x5 << (devno * 4));
97092573Ssos		pci_write_config(parent, 0x56, reg56, 2);
97193882Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA5);
97292573Ssos		return;
97392573Ssos	    }
97492573Ssos	}
97592573Ssos	if (udmamode >= 4) {
97692573Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
97792573Ssos				ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
97892573Ssos	    if (bootverbose)
97992573Ssos		ata_prtdev(atadev, "%s setting UDMA4 on ServerWorks chip\n",
98092573Ssos			   (error) ? "failed" : "success");
98192573Ssos	    if (!error) {
98292573Ssos		u_int16_t reg56;
98392573Ssos
98492573Ssos		pci_write_config(parent, 0x54,
98592573Ssos				 pci_read_config(parent, 0x54, 1) |
98692573Ssos				 (0x01 << devno), 1);
98792573Ssos		reg56 = pci_read_config(parent, 0x56, 2);
98892573Ssos		reg56 &= ~(0xf << (devno * 4));
98992573Ssos		reg56 |= (0x4 << (devno * 4));
99092573Ssos		pci_write_config(parent, 0x56, reg56, 2);
99193882Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA4);
99292573Ssos		return;
99392573Ssos	    }
99492573Ssos	}
99592573Ssos	/* FALLTHROUGH */
99692573Ssos
99766583Ssos    case 0x02111166:	/* ServerWorks ROSB4 ATA33 controller */
99866583Ssos	if (udmamode >= 2) {
99990215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
100066583Ssos				ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
100166583Ssos	    if (bootverbose)
100290215Ssos		ata_prtdev(atadev, "%s setting UDMA2 on ServerWorks chip\n",
100366583Ssos			   (error) ? "failed" : "success");
100466583Ssos	    if (!error) {
100570186Ssos		u_int16_t reg56;
100670186Ssos
100766583Ssos		pci_write_config(parent, 0x54,
100866583Ssos				 pci_read_config(parent, 0x54, 1) |
100966583Ssos				 (0x01 << devno), 1);
101066583Ssos		reg56 = pci_read_config(parent, 0x56, 2);
101166583Ssos		reg56 &= ~(0xf << (devno * 4));
101266583Ssos		reg56 |= (0x2 << (devno * 4));
101366583Ssos		pci_write_config(parent, 0x56, reg56, 2);
101493882Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA2);
101566583Ssos		return;
101666583Ssos	    }
101766583Ssos	}
101866583Ssos	if (wdmamode >= 2 && apiomode >= 4) {
101990215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
102066583Ssos				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
102166583Ssos	    if (bootverbose)
102290215Ssos		ata_prtdev(atadev, "%s setting WDMA2 on ServerWorks chip\n",
102366583Ssos			   (error) ? "failed" : "success");
102466583Ssos	    if (!error) {
102593882Ssos		int offset = devno ^ 0x01;
102666583Ssos		int word44 = pci_read_config(parent, 0x44, 4);
102766583Ssos
102866583Ssos		pci_write_config(parent, 0x54,
102966583Ssos				 pci_read_config(parent, 0x54, 1) &
103066583Ssos				 ~(0x01 << devno), 1);
103166583Ssos		word44 &= ~(0xff << (offset << 8));
103266583Ssos		word44 |= (0x20 << (offset << 8));
103366583Ssos		pci_write_config(parent, 0x44, 0x20, 4);
103493882Ssos		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
103566583Ssos		return;
103666583Ssos	    }
103766583Ssos	}
103866583Ssos	/* we could set PIO mode timings, but we assume the BIOS did that */
103966583Ssos	break;
104066583Ssos
104189917Ssos    case 0x4d69105a:	/* Promise TX2 ATA133 controllers */
104294037Ssos    case 0x5275105a:	/* Promise TX2 ATA133 controllers */
104394426Ssos    case 0x6269105a:	/* Promise TX2 ATA133 controllers */
1044104298Ssos    case 0x7275105a:	/* Promise TX2 ATA133 controllers */
104593882Ssos	ATA_OUTB(atadev->channel->r_bmio, ATA_BMDEVSPEC_0, 0x0b);
104693882Ssos	if (udmamode >= 6 &&
104793882Ssos	    !(ATA_INB(atadev->channel->r_bmio, ATA_BMDEVSPEC_1) & 0x04)) {
104890215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
104989917Ssos				ATA_UDMA6, ATA_C_F_SETXFER, ATA_WAIT_READY);
105089917Ssos	    if (bootverbose)
105190215Ssos		ata_prtdev(atadev, "%s setting UDMA6 on Promise chip\n",
105289917Ssos			   (error) ? "failed" : "success");
105389917Ssos	    if (!error) {
105493882Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA6);
105589917Ssos		return;
105689917Ssos	    }
105789917Ssos	}
105889917Ssos	/* FALLTHROUGH */
105989917Ssos
106084413Ssos    case 0x4d68105a:	/* Promise TX2 ATA100 controllers */
106188478Ssos    case 0x6268105a:	/* Promise TX2 ATA100 controllers */
106293882Ssos	ATA_OUTB(atadev->channel->r_bmio, ATA_BMDEVSPEC_0, 0x0b);
106393882Ssos	if (udmamode >= 5 &&
106493882Ssos	    !(ATA_INB(atadev->channel->r_bmio, ATA_BMDEVSPEC_1) & 0x04)) {
106590215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
106689917Ssos				ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
106789917Ssos	    if (bootverbose)
106890215Ssos		ata_prtdev(atadev, "%s setting UDMA5 on Promise chip\n",
106989917Ssos			   (error) ? "failed" : "success");
107089917Ssos	    if (!error) {
107193882Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA5);
107289917Ssos		return;
107389917Ssos	    }
107489917Ssos	}
107593882Ssos	ATA_OUTB(atadev->channel->r_bmio, ATA_BMDEVSPEC_0, 0x0b);
107693882Ssos	if (udmamode >= 4 &&
107793882Ssos	    !(ATA_INB(atadev->channel->r_bmio, ATA_BMDEVSPEC_1) & 0x04)) {
107890215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
107989917Ssos				ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
108084413Ssos	    if (bootverbose)
108190215Ssos		ata_prtdev(atadev, "%s setting UDMA4 on Promise chip\n",
108289917Ssos			   (error) ? "failed" : "success");
108384413Ssos	    if (!error) {
108493882Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA4);
108584413Ssos		return;
108684413Ssos	    }
108784413Ssos	}
108884413Ssos	if (udmamode >= 2) {
108990215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
109084413Ssos				ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
109184413Ssos	    if (bootverbose)
109290215Ssos		ata_prtdev(atadev, "%s setting UDMA on Promise chip\n",
109390215Ssos			   (error) ? "failed" : "success");
109484413Ssos	    if (!error) {
109593882Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA2);
109684413Ssos		return;
109784413Ssos	    }
109884413Ssos	}
109984413Ssos	if (wdmamode >= 2 && apiomode >= 4) {
110090215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
110184413Ssos				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
110284413Ssos	    if (bootverbose)
110390215Ssos		ata_prtdev(atadev, "%s setting WDMA2 on Promise chip\n",
110490215Ssos			   (error) ? "failed" : "success");
110584413Ssos	    if (!error) {
110693882Ssos		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
110784413Ssos		return;
110884413Ssos	    }
110984413Ssos	}
111084413Ssos	break;
111184413Ssos
111298428Ssos    case 0x0d30105a:	/* Promise OEM ATA100 controllers */
111366070Ssos    case 0x4d30105a:	/* Promise Ultra/FastTrak 100 controllers */
111493882Ssos	if (!ATAPI_DEVICE(atadev) && udmamode >= 5 &&
111593882Ssos	    !(pci_read_config(parent, 0x50, 2) & (channel ? 1<<11 : 1<<10))) {
111690215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
111764307Ssos				ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
111864307Ssos	    if (bootverbose)
111990215Ssos		ata_prtdev(atadev, "%s setting UDMA5 on Promise chip\n",
112064307Ssos			   (error) ? "failed" : "success");
112164307Ssos	    if (!error) {
112293882Ssos		promise_timing(atadev, devno, ATA_UDMA5);
112393882Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA5);
112464307Ssos		return;
112564307Ssos	    }
112664307Ssos	}
112782053Ssos	/* FALLTHROUGH */
112882053Ssos
112998428Ssos    case 0x0d38105a:	/* Promise FastTrak 66 controllers */
113082053Ssos    case 0x4d38105a:	/* Promise Ultra/FastTrak 66 controllers */
113193882Ssos	if (!ATAPI_DEVICE(atadev) && udmamode >= 4 &&
113293882Ssos	    !(pci_read_config(parent, 0x50, 2) & (channel ? 1<<11 : 1<<10))) {
113390215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
113453029Ssos				ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
113552918Ssos	    if (bootverbose)
113690215Ssos		ata_prtdev(atadev, "%s setting UDMA4 on Promise chip\n",
113756558Ssos			   (error) ? "failed" : "success");
113853681Ssos	    if (!error) {
113993882Ssos		promise_timing(atadev, devno, ATA_UDMA4);
114093882Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA4);
114156744Ssos		return;
114253681Ssos	    }
114352918Ssos	}
114482053Ssos	/* FALLTHROUGH */
114582053Ssos
114682053Ssos    case 0x4d33105a:	/* Promise Ultra/FastTrak 33 controllers */
114793882Ssos	if (!ATAPI_DEVICE(atadev) && udmamode >= 2) {
114890215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
114953029Ssos				ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
115051520Ssos	    if (bootverbose)
115190215Ssos		ata_prtdev(atadev, "%s setting UDMA2 on Promise chip\n",
115256558Ssos			   (error) ? "failed" : "success");
115353681Ssos	    if (!error) {
115493882Ssos		promise_timing(atadev, devno, ATA_UDMA2);
115593882Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA2);
115656744Ssos		return;
115753681Ssos	    }
115845095Ssos	}
115993882Ssos	if (!ATAPI_DEVICE(atadev) && wdmamode >= 2 && apiomode >= 4) {
116090215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
116153029Ssos				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
116251520Ssos	    if (bootverbose)
116390215Ssos		ata_prtdev(atadev, "%s setting WDMA2 on Promise chip\n",
116456558Ssos			   (error) ? "failed" : "success");
116553681Ssos	    if (!error) {
116693882Ssos		promise_timing(atadev, devno, ATA_WDMA2);
116793882Ssos		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
116856744Ssos		return;
116953681Ssos	    }
117051520Ssos	}
117190215Ssos	error = ata_command(atadev, ATA_C_SETFEATURES, 0,
117284584Ssos			    ATA_PIO0 + apiomode,
117355333Ssos			    ATA_C_F_SETXFER, ATA_WAIT_READY);
117453681Ssos	if (bootverbose)
117590215Ssos	    ata_prtdev(atadev, "%s setting PIO%d on Promise chip\n",
117656558Ssos		       (error) ? "failed" : "success",
117756558Ssos		       (apiomode >= 0) ? apiomode : 0);
117893882Ssos	promise_timing(atadev, devno, ATA_PIO0 + apiomode);
117990215Ssos	atadev->mode = ATA_PIO0 + apiomode;
118056744Ssos	return;
118145095Ssos
118285350Ssos    case 0x00041103:	/* HighPoint HPT366/368/370/372 controllers */
118390844Ssos    case 0x00051103:	/* HighPoint HPT372 controllers */
118490533Ssos    case 0x00081103:	/* HighPoint HPT374 controllers */
118593882Ssos	if (!ATAPI_DEVICE(atadev) && udmamode >= 6 && hpt_cable80(atadev) &&
118693882Ssos	    ((chiptype == 0x00041103 && chiprev >= 0x05) ||
118793882Ssos	     (chiptype == 0x00051103 && chiprev >= 0x01) ||
118893882Ssos	     (chiptype == 0x00081103 && chiprev >= 0x07))) {
118990215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
119085350Ssos				ATA_UDMA6, ATA_C_F_SETXFER, ATA_WAIT_READY);
119185350Ssos	    if (bootverbose)
119290215Ssos		ata_prtdev(atadev, "%s setting UDMA6 on HighPoint chip\n",
119385350Ssos			   (error) ? "failed" : "success");
119485350Ssos	    if (!error) {
119593882Ssos		hpt_timing(atadev, devno, ATA_UDMA6);
119693882Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA6);
119785350Ssos		return;
119885350Ssos	    }
119985350Ssos	}
120093882Ssos	if (!ATAPI_DEVICE(atadev) && udmamode >= 5 && hpt_cable80(atadev) &&
120193882Ssos	    ((chiptype == 0x00041103 && chiprev >= 0x03) ||
120293882Ssos	     (chiptype == 0x00051103 && chiprev >= 0x01) ||
120393882Ssos	     (chiptype == 0x00081103 && chiprev >= 0x07))) {
120490215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
120564307Ssos				ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
120664307Ssos	    if (bootverbose)
120790215Ssos		ata_prtdev(atadev, "%s setting UDMA5 on HighPoint chip\n",
120864307Ssos			   (error) ? "failed" : "success");
120964307Ssos	    if (!error) {
121093882Ssos		hpt_timing(atadev, devno, ATA_UDMA5);
121193882Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA5);
121264307Ssos		return;
121364307Ssos	    }
121464307Ssos	}
121593882Ssos	if (!ATAPI_DEVICE(atadev) && udmamode >= 4 && hpt_cable80(atadev)) {
121690215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
121753029Ssos				ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
121852067Ssos	    if (bootverbose)
121990215Ssos		ata_prtdev(atadev, "%s setting UDMA4 on HighPoint chip\n",
122056558Ssos			   (error) ? "failed" : "success");
122153681Ssos	    if (!error) {
122293882Ssos		hpt_timing(atadev, devno, ATA_UDMA4);
122393882Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA4);
122456744Ssos		return;
122553681Ssos	    }
122651520Ssos	}
122793882Ssos	if (!ATAPI_DEVICE(atadev) && udmamode >= 2) {
122890215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
122953029Ssos				ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
123051520Ssos	    if (bootverbose)
123190215Ssos		ata_prtdev(atadev, "%s setting UDMA2 on HighPoint chip\n",
123256558Ssos			   (error) ? "failed" : "success");
123353681Ssos	    if (!error) {
123493882Ssos		hpt_timing(atadev, devno, ATA_UDMA2);
123593882Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA2);
123656744Ssos		return;
123753681Ssos	    }
123845095Ssos	}
123993882Ssos	if (!ATAPI_DEVICE(atadev) && wdmamode >= 2 && apiomode >= 4) {
124090215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
124153029Ssos				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
124251520Ssos	    if (bootverbose)
124390215Ssos		ata_prtdev(atadev, "%s setting WDMA2 on HighPoint chip\n",
124456558Ssos			   (error) ? "failed" : "success");
124553681Ssos	    if (!error) {
124693882Ssos		hpt_timing(atadev, devno, ATA_WDMA2);
124793882Ssos		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
124856744Ssos		return;
124953681Ssos	    }
125045095Ssos	}
125190215Ssos	error = ata_command(atadev, ATA_C_SETFEATURES, 0,
125284584Ssos			    ATA_PIO0 + apiomode,
125355333Ssos			    ATA_C_F_SETXFER, ATA_WAIT_READY);
125453681Ssos	if (bootverbose)
125590215Ssos	    ata_prtdev(atadev, "%s setting PIO%d on HighPoint chip\n",
125656558Ssos		       (error) ? "failed" : "success",
125756558Ssos		       (apiomode >= 0) ? apiomode : 0);
125893882Ssos	hpt_timing(atadev, devno, ATA_PIO0 + apiomode);
125990215Ssos	atadev->mode = ATA_PIO0 + apiomode;
126056744Ssos	return;
126145095Ssos
1262107562Ssos    case 0x00091191:	/* Acard ATP865R controller */
1263107562Ssos    case 0x00081191:	/* Acard ATP865 controller */
1264107562Ssos	if (ATAPI_DEVICE(atadev))
1265107562Ssos	    break;
1266107562Ssos	if (udmamode >= 6) {
1267107562Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1268107562Ssos				ATA_UDMA6, ATA_C_F_SETXFER, ATA_WAIT_READY);
1269107562Ssos	    if (bootverbose)
1270107562Ssos		ata_prtdev(atadev, "%s setting up UDMA6 mode on Acard chip\n",
1271107562Ssos			   (error) ? "failed" : "success");
1272107562Ssos	    if (!error) {
1273107562Ssos		u_int16_t reg44 = pci_read_config(parent, 0x44, 2);
1274107562Ssos
1275107562Ssos		reg44 &= ~(0x000f << (devno << 2));
1276107562Ssos		reg44 |= (0x0007 << (devno << 2));
1277107562Ssos		pci_write_config(parent, 0x44, reg44, 2);
1278107562Ssos		pci_write_config(parent, 0x4a, 0xa6, 1);
1279107562Ssos		pci_write_config(parent, 0x40 + devno, 0x031, 1);
1280107562Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA6);
1281107562Ssos		return;
1282107562Ssos	    }
1283107562Ssos	}
1284107562Ssos	if (udmamode >= 5) {
1285107562Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1286107562Ssos				ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
1287107562Ssos	    if (bootverbose)
1288107562Ssos		ata_prtdev(atadev, "%s setting up UDMA5 mode on Acard chip\n",
1289107562Ssos			   (error) ? "failed" : "success");
1290107562Ssos	    if (!error) {
1291107562Ssos		u_int16_t reg44 = pci_read_config(parent, 0x44, 2);
1292107562Ssos
1293107562Ssos		reg44 &= ~(0x000f << (devno << 2));
1294107562Ssos		reg44 |= (0x0006 << (devno << 2));
1295107562Ssos		pci_write_config(parent, 0x44, reg44, 2);
1296107562Ssos		pci_write_config(parent, 0x4a, 0xa6, 1);
1297107562Ssos		pci_write_config(parent, 0x40 + devno, 0x031, 1);
1298107562Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA5);
1299107562Ssos		return;
1300107562Ssos	    }
1301107562Ssos	}
1302107562Ssos	/* FALLTHROUGH */
1303107562Ssos
1304107562Ssos    case 0x00071191:	/* Acard ATP860R controller */
1305107562Ssos    case 0x00061191:	/* Acard ATP860 controller */
1306107562Ssos	if (ATAPI_DEVICE(atadev))
1307107562Ssos	    break;
1308107562Ssos	if (udmamode >= 4) {
1309107562Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1310107562Ssos				ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
1311107562Ssos	    if (bootverbose)
1312107562Ssos		ata_prtdev(atadev, "%s setting up UDMA4 mode on Acard chip\n",
1313107562Ssos			   (error) ? "failed" : "success");
1314107562Ssos	    if (!error) {
1315107562Ssos		u_int16_t reg44 = pci_read_config(parent, 0x44, 2);
1316107562Ssos
1317107562Ssos		reg44 &= ~(0x000f << (devno << 2));
1318107562Ssos		reg44 |= (0x0005 << (devno << 2));
1319107562Ssos		pci_write_config(parent, 0x44, reg44, 2);
1320107562Ssos		pci_write_config(parent, 0x4a, 0xa6, 1);
1321107562Ssos		pci_write_config(parent, 0x40 + devno, 0x031, 1);
1322107562Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA4);
1323107562Ssos		return;
1324107562Ssos	    }
1325107562Ssos	}
1326107562Ssos	if (udmamode >= 2) {
1327107562Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1328107562Ssos				ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
1329107562Ssos	    if (bootverbose)
1330107562Ssos		ata_prtdev(atadev, "%s setting up UDMA2 mode on Acard chip\n",
1331107562Ssos			   (error) ? "failed" : "success");
1332107562Ssos	    if (!error) {
1333107562Ssos		u_int16_t reg44 = pci_read_config(parent, 0x44, 2);
1334107562Ssos
1335107562Ssos		reg44 &= ~(0x000f << (devno << 2));
1336107562Ssos		reg44 |= (0x0003 << (devno << 2));
1337107562Ssos		pci_write_config(parent, 0x44, reg44, 2);
1338107562Ssos		pci_write_config(parent, 0x4a, 0xa6, 1);
1339107562Ssos		pci_write_config(parent, 0x40 + devno, 0x031, 1);
1340107562Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA2);
1341107562Ssos		return;
1342107562Ssos	    }
1343107562Ssos	}
1344107562Ssos	if (wdmamode >= 2 && apiomode >= 4) {
1345107562Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1346107562Ssos				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
1347107562Ssos	    if (bootverbose)
1348107562Ssos		ata_prtdev(atadev, "%s setting up WDMA2 mode on Acard chip\n",
1349107562Ssos			   (error) ? "failed" : "success");
1350107562Ssos	    if (!error) {
1351107562Ssos		u_int16_t reg44 = pci_read_config(parent, 0x44, 2);
1352107562Ssos
1353107562Ssos		reg44 &= ~(0x000f << (devno << 2));
1354107562Ssos		pci_write_config(parent, 0x44, reg44, 2);
1355107562Ssos		pci_write_config(parent, 0x4a, 0xa6, 1);
1356107562Ssos		pci_write_config(parent, 0x40 + devno, 0x031, 1);
1357107562Ssos		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
1358107562Ssos		return;
1359107562Ssos	    }
1360107562Ssos	}
1361107562Ssos	/* we could set PIO mode timings, but we assume the BIOS did that */
1362107562Ssos	break;
1363107562Ssos
1364107562Ssos    case 0x00051191:	/* Acard ATP850UF controller */
1365107562Ssos	if (ATAPI_DEVICE(atadev))
1366107562Ssos	    break;
1367107562Ssos	if (udmamode >= 2) {
1368107562Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1369107562Ssos				ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
1370107562Ssos	    if (bootverbose)
1371107562Ssos		ata_prtdev(atadev, "%s setting up UDMA2 mode on Acard chip\n",
1372107562Ssos			   (error) ? "failed" : "success");
1373107562Ssos	    if (!error) {
1374107562Ssos		u_int8_t reg54 = pci_read_config(parent, 0x54, 1);
1375107562Ssos
1376107562Ssos		reg54 |= (0x03 << (devno << 1));
1377107562Ssos		pci_write_config(parent, 0x54, reg54, 1);
1378107562Ssos		pci_write_config(parent, 0x4a, 0xa6, 1);
1379107562Ssos		pci_write_config(parent, 0x40 + (devno << 1), 0x0301, 2);
1380107562Ssos		ata_dmacreate(atadev, apiomode, ATA_UDMA2);
1381107562Ssos		return;
1382107562Ssos	    }
1383107562Ssos	}
1384107562Ssos	if (wdmamode >= 2 && apiomode >= 4) {
1385107562Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1386107562Ssos				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
1387107562Ssos	    if (bootverbose)
1388107562Ssos		ata_prtdev(atadev, "%s setting up WDMA2 mode on Acard chip\n",
1389107562Ssos			   (error) ? "failed" : "success");
1390107562Ssos	    if (!error) {
1391107562Ssos		u_int8_t reg54 = pci_read_config(parent, 0x54, 1);
1392107562Ssos
1393107562Ssos		reg54 &= ~(0x03 << (devno << 1));
1394107562Ssos		pci_write_config(parent, 0x54, reg54, 1);
1395107562Ssos		pci_write_config(parent, 0x4a, 0xa6, 1);
1396107562Ssos		pci_write_config(parent, 0x40 + (devno << 1), 0x0301, 2);
1397107562Ssos		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
1398107562Ssos		return;
1399107562Ssos	    }
1400107562Ssos	}
1401107562Ssos	/* we could set PIO mode timings, but we assume the BIOS did that */
1402107562Ssos	break;
1403107562Ssos
140490572Ssos    case 0x000116ca:	/* Cenatek Rocket Drive controller */
140590572Ssos	if (wdmamode >= 0 &&
140693882Ssos	    (ATA_INB(atadev->channel->r_bmio, ATA_BMSTAT_PORT) &
140793882Ssos	     (device ? ATA_BMSTAT_DMA_SLAVE : ATA_BMSTAT_DMA_MASTER)))
140893882Ssos	    ata_dmacreate(atadev, apiomode, ATA_DMA);
140990572Ssos	else
141090572Ssos	    atadev->mode = ATA_PIO;
141190572Ssos	return;
141290572Ssos
141351548Ssos    default:		/* unknown controller chip */
141451548Ssos	/* better not try generic DMA on ATAPI devices it almost never works */
141593882Ssos	if (ATAPI_DEVICE(atadev))
141651548Ssos	    break;
141751548Ssos
141856744Ssos	/* if controller says its setup for DMA take the easy way out */
141956744Ssos	/* the downside is we dont know what DMA mode we are in */
142090572Ssos	if ((udmamode >= 0 || wdmamode >= 2) &&
142193882Ssos	    (ATA_INB(atadev->channel->r_bmio, ATA_BMSTAT_PORT) &
142293882Ssos	     (device ? ATA_BMSTAT_DMA_SLAVE : ATA_BMSTAT_DMA_MASTER))) {
142393882Ssos	    ata_dmacreate(atadev, apiomode, ATA_DMA);
142456744Ssos	    return;
142556744Ssos	}
142656744Ssos
142751548Ssos	/* well, we have no support for this, but try anyways */
142893882Ssos	if ((wdmamode >= 2 && apiomode >= 4) && atadev->channel->r_bmio) {
142990215Ssos	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
143053029Ssos				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
143151520Ssos	    if (bootverbose)
143290215Ssos		ata_prtdev(atadev, "%s setting WDMA2 on generic chip\n",
143356558Ssos			   (error) ? "failed" : "success");
143453681Ssos	    if (!error) {
143593882Ssos		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
143656744Ssos		return;
143753681Ssos	    }
143845095Ssos	}
143945095Ssos    }
144090215Ssos    error = ata_command(atadev, ATA_C_SETFEATURES, 0, ATA_PIO0 + apiomode,
144190572Ssos			ATA_C_F_SETXFER, ATA_WAIT_READY);
144255333Ssos    if (bootverbose)
144390215Ssos	ata_prtdev(atadev, "%s setting PIO%d on generic chip\n",
144456988Ssos		   (error) ? "failed" : "success", apiomode < 0 ? 0 : apiomode);
144556988Ssos    if (!error)
144690215Ssos	atadev->mode = ATA_PIO0 + apiomode;
144757771Ssos    else {
144856988Ssos	if (bootverbose)
144990215Ssos	    ata_prtdev(atadev, "using PIO mode set by BIOS\n");
145090215Ssos	atadev->mode = ATA_PIO;
145157771Ssos    }
145245095Ssos}
145345095Ssos
145493882Ssosstruct ata_dmasetup_data_cb_args {
145593882Ssos    struct ata_dmaentry *dmatab;
145693882Ssos    int error;
145793882Ssos};
145893882Ssos
145993882Ssosstatic void
146093882Ssosata_dmasetupd_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
146193882Ssos{
146293882Ssos    struct ata_dmasetup_data_cb_args *cba =
146393882Ssos	(struct ata_dmasetup_data_cb_args *)xsc;
146493882Ssos    bus_size_t cnt;
146593882Ssos    u_int32_t lastcount;
146693882Ssos    int i, j;
146793882Ssos
146893882Ssos    cba->error = error;
146993882Ssos    if (error != 0)
147093882Ssos	return;
147193882Ssos    lastcount = j = 0;
147293882Ssos    for (i = 0; i < nsegs; i++) {
147393882Ssos	/*
147493882Ssos	 * A maximum segment size was specified for bus_dma_tag_create, but
147593882Ssos	 * some busdma code does not seem to honor this, so fix up if needed.
147693882Ssos	 */
147793882Ssos	for (cnt = 0; cnt < segs[i].ds_len; cnt += MAXSEGSZ, j++) {
147893882Ssos	    cba->dmatab[j].base = htole32(segs[i].ds_addr + cnt);
147993882Ssos	    lastcount = ulmin(segs[i].ds_len - cnt, MAXSEGSZ) & 0xffff;
148093882Ssos	    cba->dmatab[j].count = htole32(lastcount);
148193882Ssos	}
148293882Ssos    }
148393882Ssos    cba->dmatab[j - 1].count = htole32(lastcount | ATA_DMA_EOT);
148493882Ssos}
148593882Ssos
148666070Ssosint
148793882Ssosata_dmasetup(struct ata_device *atadev, caddr_t data, int32_t count)
148845095Ssos{
148993882Ssos    struct ata_channel *ch = atadev->channel;
149045095Ssos
149190215Ssos    if (((uintptr_t)data & ch->alignment) || (count & ch->alignment)) {
149293882Ssos	ata_prtdev(atadev, "non aligned DMA transfer attempted\n");
149345095Ssos	return -1;
149466070Ssos    }
149545095Ssos
149645095Ssos    if (!count) {
149793882Ssos	ata_prtdev(atadev, "zero length DMA transfer attempted\n");
149845095Ssos	return -1;
149945095Ssos    }
150095010Ssos    return 0;
150195010Ssos}
150245095Ssos
150395010Ssosint
150495010Ssosata_dmastart(struct ata_device *atadev, caddr_t data, int32_t count, int dir)
150595010Ssos{
150695010Ssos    struct ata_channel *ch = atadev->channel;
150795010Ssos    struct ata_dmastate *ds = &atadev->dmastate;
150895010Ssos    struct ata_dmasetup_data_cb_args cba;
150995010Ssos
151095010Ssos    if (ds->flags & ATA_DS_ACTIVE)
151195010Ssos	    panic("ata_dmasetup: transfer active on this device!");
151295010Ssos
1513108931Ssos    switch(ch->chiptype) {
1514108931Ssos    case 0x0d38105a:  /* Promise Fasttrak 66 */
1515108931Ssos    case 0x4d38105a:  /* Promise Ultra/Fasttrak 66 */
1516108931Ssos    case 0x0d30105a:  /* Promise OEM ATA 100 */
1517108931Ssos    case 0x4d30105a:  /* Promise Ultra/Fasttrak 100 */
1518108931Ssos	ATA_OUTB(ch->r_bmio, 0x11,
1519108931Ssos		 ATA_INB(ch->r_bmio, 0x11) | (atadev->unit ? 0x08 : 0x02));
1520108931Ssos
1521108931Ssos	ATA_OUTL(ch->r_bmio, (atadev->unit ? 0x24 : 0x20),
1522108931Ssos                 (dir ? 0x05000000 : 0x06000000) | (count >> 1));
1523108931Ssos        break;
1524108931Ssos    }
1525108931Ssos
152693882Ssos    cba.dmatab = ds->dmatab;
152795010Ssos    bus_dmamap_sync(ds->cdmatag, ds->cdmamap, BUS_DMASYNC_PREWRITE);
152893882Ssos    if (bus_dmamap_load(ds->ddmatag, ds->ddmamap, data, count,
152993882Ssos			ata_dmasetupd_cb, &cba, 0) || cba.error)
153095010Ssos	return -1;
153193882Ssos
153295010Ssos    bus_dmamap_sync(ds->cdmatag, ds->cdmamap, BUS_DMASYNC_POSTWRITE);
153393882Ssos    bus_dmamap_sync(ds->ddmatag, ds->ddmamap, dir ? BUS_DMASYNC_PREREAD :
153493882Ssos		    BUS_DMASYNC_PREWRITE);
153595010Ssos
153695010Ssos    ch->flags |= ATA_DMA_ACTIVE;
1537108931Ssos    ds->flags = dir ? (ATA_DS_ACTIVE | ATA_DS_READ) : ATA_DS_ACTIVE;
153895010Ssos
153993882Ssos    ATA_OUTL(ch->r_bmio, ATA_BMDTP_PORT, ds->mdmatab);
154090215Ssos    ATA_OUTB(ch->r_bmio, ATA_BMCMD_PORT, dir ? ATA_BMCMD_WRITE_READ : 0);
154190215Ssos    ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT,
1542108931Ssos	     (ATA_INB(ch->r_bmio, ATA_BMSTAT_PORT) |
1543108931Ssos	     (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
154490215Ssos    ATA_OUTB(ch->r_bmio, ATA_BMCMD_PORT,
1545108931Ssos	     ATA_INB(ch->r_bmio, ATA_BMCMD_PORT) | ATA_BMCMD_START_STOP);
154695010Ssos    return 0;
154745095Ssos}
154845095Ssos
154966070Ssosint
155093882Ssosata_dmadone(struct ata_device *atadev)
155145095Ssos{
1552108931Ssos    struct ata_channel *ch = atadev->channel;
1553108931Ssos    struct ata_dmastate *ds = &atadev->dmastate;
155472106Ssos    int error;
155572106Ssos
1556108931Ssos    switch(ch->chiptype) {
1557108931Ssos    case 0x0d38105a:  /* Promise Fasttrak 66 */
1558108931Ssos    case 0x4d38105a:  /* Promise Ultra/Fasttrak 66 */
1559108931Ssos    case 0x0d30105a:  /* Promise OEM ATA 100 */
1560108931Ssos    case 0x4d30105a:  /* Promise Ultra/Fasttrak 100 */
1561108931Ssos	ATA_OUTL(ch->r_bmio, (atadev->unit ? 0x24 : 0x20), 0);
1562108931Ssos	ATA_OUTB(ch->r_bmio, 0x11,
1563108931Ssos		 ATA_INB(ch->r_bmio, 0x11) & ~(atadev->unit ? 0x08 : 0x02));
1564108931Ssos        break;
1565108931Ssos    }
1566108931Ssos
156793882Ssos    bus_dmamap_sync(ds->ddmatag, ds->ddmamap, (ds->flags & ATA_DS_READ) != 0 ?
156893882Ssos		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
156993882Ssos    bus_dmamap_unload(ds->ddmatag, ds->ddmamap);
1570108931Ssos
157190215Ssos    ATA_OUTB(ch->r_bmio, ATA_BMCMD_PORT,
157290215Ssos		ATA_INB(ch->r_bmio, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
157390215Ssos    error = ATA_INB(ch->r_bmio, ATA_BMSTAT_PORT);
157490215Ssos    ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT,
157572106Ssos	     error | ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
157695010Ssos    ch->flags &= ~ATA_DMA_ACTIVE;
157793882Ssos    ds->flags = 0;
157895010Ssos    return (error & ATA_BMSTAT_MASK);
157945095Ssos}
158045095Ssos
158166070Ssosint
158290215Ssosata_dmastatus(struct ata_channel *ch)
158345095Ssos{
158490215Ssos    return ATA_INB(ch->r_bmio, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
158545095Ssos}
158645095Ssos
158752067Ssosstatic void
158893882Ssoscyrix_timing(struct ata_device *atadev, int devno, int mode)
158952067Ssos{
159066070Ssos    u_int32_t reg20 = 0x0000e132;
159166070Ssos    u_int32_t reg24 = 0x00017771;
159266070Ssos
159366070Ssos    switch (mode) {
159466070Ssos    case ATA_PIO0:	reg20 = 0x0000e132; break;
159566070Ssos    case ATA_PIO1:	reg20 = 0x00018121; break;
159666070Ssos    case ATA_PIO2:	reg20 = 0x00024020; break;
159766070Ssos    case ATA_PIO3:	reg20 = 0x00032010; break;
159866070Ssos    case ATA_PIO4:	reg20 = 0x00040010; break;
159966070Ssos    case ATA_WDMA2:	reg24 = 0x00002020; break;
160066070Ssos    case ATA_UDMA2:	reg24 = 0x00911030; break;
160166070Ssos    }
160293882Ssos    ATA_OUTL(atadev->channel->r_bmio, (devno << 3) + 0x20, reg20);
160393882Ssos    ATA_OUTL(atadev->channel->r_bmio, (devno << 3) + 0x24, reg24);
160466070Ssos}
160589917Ssos
160666070Ssosstatic void
160793882Ssospromise_timing(struct ata_device *atadev, int devno, int mode)
160866070Ssos{
160956988Ssos    u_int32_t timing = 0;
161093882Ssos    /* XXX: Endianess */
161156988Ssos    struct promise_timing {
161256988Ssos	u_int8_t  pa:4;
161356988Ssos	u_int8_t  prefetch:1;
161456988Ssos	u_int8_t  iordy:1;
161556988Ssos	u_int8_t  errdy:1;
161656988Ssos	u_int8_t  syncin:1;
161756988Ssos	u_int8_t  pb:5;
161856988Ssos	u_int8_t  mb:3;
161956988Ssos	u_int8_t  mc:4;
162056988Ssos	u_int8_t  dmaw:1;
162156988Ssos	u_int8_t  dmar:1;
162256988Ssos	u_int8_t  iordyp:1;
162356988Ssos	u_int8_t  dmarqp:1;
162456988Ssos	u_int8_t  reserved:8;
162556988Ssos    } *t = (struct promise_timing*)&timing;
162656988Ssos
162756988Ssos    t->iordy = 1; t->iordyp = 1;
162856988Ssos    if (mode >= ATA_DMA) {
162956988Ssos	t->prefetch = 1; t->errdy = 1; t->syncin = 1;
163055333Ssos    }
163156988Ssos
163293882Ssos    switch (atadev->channel->chiptype) {
163366070Ssos    case 0x4d33105a:  /* Promise Ultra/Fasttrak 33 */
163456988Ssos	switch (mode) {
163556988Ssos	default:
163690215Ssos	case ATA_PIO0:	t->pa =	 9; t->pb = 19; t->mb = 7; t->mc = 15; break;
163790215Ssos	case ATA_PIO1:	t->pa =	 5; t->pb = 12; t->mb = 7; t->mc = 15; break;
163890215Ssos	case ATA_PIO2:	t->pa =	 3; t->pb =  8; t->mb = 7; t->mc = 15; break;
163990215Ssos	case ATA_PIO3:	t->pa =	 2; t->pb =  6; t->mb = 7; t->mc = 15; break;
164090215Ssos	case ATA_PIO4:	t->pa =	 1; t->pb =  4; t->mb = 7; t->mc = 15; break;
164190215Ssos	case ATA_WDMA2: t->pa =	 3; t->pb =  7; t->mb = 3; t->mc =  3; break;
164290215Ssos	case ATA_UDMA2: t->pa =	 3; t->pb =  7; t->mb = 1; t->mc =  1; break;
164356988Ssos	}
164456988Ssos	break;
164556988Ssos
164698428Ssos    case 0x0d38105a:  /* Promise Fasttrak 66 */
164766070Ssos    case 0x4d38105a:  /* Promise Ultra/Fasttrak 66 */
164898428Ssos    case 0x0d30105a:  /* Promise OEM ATA 100 */
164966070Ssos    case 0x4d30105a:  /* Promise Ultra/Fasttrak 100 */
165056988Ssos	switch (mode) {
165156988Ssos	default:
165290215Ssos	case ATA_PIO0:	t->pa = 15; t->pb = 31; t->mb = 7; t->mc = 15; break;
165390215Ssos	case ATA_PIO1:	t->pa = 10; t->pb = 24; t->mb = 7; t->mc = 15; break;
165490215Ssos	case ATA_PIO2:	t->pa =	 6; t->pb = 16; t->mb = 7; t->mc = 15; break;
165590215Ssos	case ATA_PIO3:	t->pa =	 4; t->pb = 12; t->mb = 7; t->mc = 15; break;
165690215Ssos	case ATA_PIO4:	t->pa =	 2; t->pb =  8; t->mb = 7; t->mc = 15; break;
165790215Ssos	case ATA_WDMA2: t->pa =	 6; t->pb = 14; t->mb = 6; t->mc =  6; break;
165890215Ssos	case ATA_UDMA2: t->pa =	 6; t->pb = 14; t->mb = 2; t->mc =  2; break;
165990215Ssos	case ATA_UDMA4: t->pa =	 3; t->pb =  7; t->mb = 1; t->mc =  1; break;
166090215Ssos	case ATA_UDMA5: t->pa =	 3; t->pb =  7; t->mb = 1; t->mc =  1; break;
166156988Ssos	}
166256988Ssos	break;
166356988Ssos    }
166493882Ssos    pci_write_config(device_get_parent(atadev->channel->dev),
166593882Ssos		     0x60 + (devno << 2), timing, 4);
166655333Ssos}
166752067Ssos
166855333Ssosstatic void
166993882Ssoshpt_timing(struct ata_device *atadev, int devno, int mode)
167055333Ssos{
167193882Ssos    device_t parent = device_get_parent(atadev->channel->dev);
167293882Ssos    u_int32_t chiptype = atadev->channel->chiptype;
167393882Ssos    int chiprev = pci_get_revid(parent);
167455333Ssos    u_int32_t timing;
167591914Ssos
167693882Ssos    if (chiptype == 0x00081103 && chiprev >= 0x07) {
167790844Ssos	switch (mode) {						/* HPT374 */
167890533Ssos	case ATA_PIO0:	timing = 0x0ac1f48a; break;
167990533Ssos	case ATA_PIO1:	timing = 0x0ac1f465; break;
168090533Ssos	case ATA_PIO2:	timing = 0x0a81f454; break;
168190533Ssos	case ATA_PIO3:	timing = 0x0a81f443; break;
168290533Ssos	case ATA_PIO4:	timing = 0x0a81f442; break;
168391593Ssos	case ATA_WDMA2: timing = 0x22808242; break;
168491593Ssos	case ATA_UDMA2: timing = 0x120c8242; break;
168591593Ssos	case ATA_UDMA4: timing = 0x12ac8242; break;
168691593Ssos	case ATA_UDMA5: timing = 0x12848242; break;
168791593Ssos	case ATA_UDMA6: timing = 0x12808242; break;
168890533Ssos	default:	timing = 0x0d029d5e;
168990533Ssos	}
169090533Ssos    }
169193882Ssos    else if ((chiptype == 0x00041103 && chiprev >= 0x05) ||
169293882Ssos	     (chiptype == 0x00051103 && chiprev >= 0x01)) {
169390844Ssos	switch (mode) {						/* HPT372 */
169490215Ssos	case ATA_PIO0:	timing = 0x0d029d5e; break;
169590215Ssos	case ATA_PIO1:	timing = 0x0d029d26; break;
169690215Ssos	case ATA_PIO2:	timing = 0x0c829ca6; break;
169790215Ssos	case ATA_PIO3:	timing = 0x0c829c84; break;
169890215Ssos	case ATA_PIO4:	timing = 0x0c829c62; break;
169985350Ssos	case ATA_WDMA2: timing = 0x2c829262; break;
170090215Ssos	case ATA_UDMA2: timing = 0x1c91dc62; break;
170190215Ssos	case ATA_UDMA4: timing = 0x1c8ddc62; break;
170290215Ssos	case ATA_UDMA5: timing = 0x1c6ddc62; break;
170390215Ssos	case ATA_UDMA6: timing = 0x1c81dc62; break;
170485350Ssos	default:	timing = 0x0d029d5e;
170585350Ssos	}
170685350Ssos    }
170793882Ssos    else if (chiptype == 0x00041103 && chiprev >= 0x03) {
170890844Ssos	switch (mode) {						/* HPT370 */
170964307Ssos	case ATA_PIO0:	timing = 0x06914e57; break;
171064307Ssos	case ATA_PIO1:	timing = 0x06914e43; break;
171164307Ssos	case ATA_PIO2:	timing = 0x06514e33; break;
171264307Ssos	case ATA_PIO3:	timing = 0x06514e22; break;
171364307Ssos	case ATA_PIO4:	timing = 0x06514e21; break;
171490215Ssos	case ATA_WDMA2: timing = 0x26514e21; break;
171590215Ssos	case ATA_UDMA2: timing = 0x16494e31; break;
171690215Ssos	case ATA_UDMA4: timing = 0x16454e31; break;
171790215Ssos	case ATA_UDMA5: timing = 0x16454e31; break;
171864307Ssos	default:	timing = 0x06514e57;
171952067Ssos	}
172064307Ssos	pci_write_config(parent, 0x40 + (devno << 2) , timing, 4);
172164307Ssos    }
172290844Ssos    else {							/* HPT36[68] */
172364307Ssos	switch (pci_read_config(parent, 0x41 + (devno << 2), 1)) {
172464307Ssos	case 0x85:	/* 25Mhz */
172564307Ssos	    switch (mode) {
172691914Ssos	    case ATA_PIO0:	timing = 0x40d08585; break;
172791914Ssos	    case ATA_PIO1:	timing = 0x40d08572; break;
172891914Ssos	    case ATA_PIO2:	timing = 0x40ca8542; break;
172991914Ssos	    case ATA_PIO3:	timing = 0x40ca8532; break;
173091914Ssos	    case ATA_PIO4:	timing = 0x40ca8521; break;
173191914Ssos	    case ATA_WDMA2:	timing = 0x20ca8521; break;
173291914Ssos	    case ATA_UDMA2:	timing = 0x10cf8521; break;
173391914Ssos	    case ATA_UDMA4:	timing = 0x10c98521; break;
173464307Ssos	    default:		timing = 0x01208585;
173564307Ssos	    }
173664307Ssos	    break;
173764307Ssos	default:
173864307Ssos	case 0xa7:	/* 33MHz */
173964307Ssos	    switch (mode) {
174091914Ssos	    case ATA_PIO0:	timing = 0x40d0a7aa; break;
174191914Ssos	    case ATA_PIO1:	timing = 0x40d0a7a3; break;
174291914Ssos	    case ATA_PIO2:	timing = 0x40d0a753; break;
174391914Ssos	    case ATA_PIO3:	timing = 0x40c8a742; break;
174491914Ssos	    case ATA_PIO4:	timing = 0x40c8a731; break;
174591914Ssos	    case ATA_WDMA2:	timing = 0x20c8a731; break;
174691914Ssos	    case ATA_UDMA2:	timing = 0x10caa731; break;
174791914Ssos	    case ATA_UDMA4:	timing = 0x10c9a731; break;
174864307Ssos	    default:		timing = 0x0120a7a7;
174964307Ssos	    }
175064307Ssos	    break;
175164307Ssos	case 0xd9:	/* 40Mhz */
175264307Ssos	    switch (mode) {
175391914Ssos	    case ATA_PIO0:	timing = 0x4018d9d9; break;
175491914Ssos	    case ATA_PIO1:	timing = 0x4010d9c7; break;
175591914Ssos	    case ATA_PIO2:	timing = 0x4010d997; break;
175691914Ssos	    case ATA_PIO3:	timing = 0x4010d974; break;
175791914Ssos	    case ATA_PIO4:	timing = 0x4008d963; break;
175891914Ssos	    case ATA_WDMA2:	timing = 0x2008d943; break;
175991914Ssos	    case ATA_UDMA2:	timing = 0x100bd943; break;
176091914Ssos	    case ATA_UDMA4:	timing = 0x100fd943; break;
176164307Ssos	    default:		timing = 0x0120d9d9;
176264307Ssos	    }
176352067Ssos	}
176452067Ssos    }
176591914Ssos    pci_write_config(parent, 0x40 + (devno << 2) , timing, 4);
176652067Ssos}
176791914Ssos
176891914Ssosstatic int
176993882Ssoshpt_cable80(struct ata_device *atadev)
177091914Ssos{
177193882Ssos    device_t parent = device_get_parent(atadev->channel->dev);
177291914Ssos    u_int8_t reg, val, res;
177391914Ssos
177493882Ssos    if (atadev->channel->chiptype==0x00081103 && pci_get_function(parent)==1) {
177593882Ssos	reg = atadev->channel->unit ? 0x57 : 0x53;
177691914Ssos	val = pci_read_config(parent, reg, 1);
177791914Ssos	pci_write_config(parent, reg, val | 0x80, 1);
177891914Ssos    }
177991914Ssos    else {
178091914Ssos	reg = 0x5b;
178191914Ssos	val = pci_read_config(parent, reg, 1);
178291914Ssos	pci_write_config(parent, reg, val & 0xfe, 1);
178391914Ssos    }
178493882Ssos    res = pci_read_config(parent, 0x5a, 1) & (atadev->channel->unit ? 0x1:0x2);
178591914Ssos    pci_write_config(parent, reg, val, 1);
178691914Ssos    return !res;
178791914Ssos}
1788