ata-dma.c revision 94826
1/*-
2 * Copyright (c) 1998,1999,2000,2001,2002 S�ren Schmidt <sos@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer,
10 *    without modification, immediately at the beginning of the file.
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. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $FreeBSD: head/sys/dev/ata/ata-dma.c 94826 2002-04-16 08:30:51Z sos $
29 */
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/ata.h>
34#include <sys/bio.h>
35#include <sys/malloc.h>
36#include <sys/bus.h>
37#include <sys/disk.h>
38#include <sys/devicestat.h>
39#include <pci/pcivar.h>
40#include <machine/bus.h>
41#include <sys/rman.h>
42#include <dev/ata/ata-all.h>
43
44/* prototypes */
45static void ata_dmacreate(struct ata_device *, int, int);
46static void ata_dmasetupd_cb(void *, bus_dma_segment_t *, int, int);
47static void ata_dmasetupc_cb(void *, bus_dma_segment_t *, int, int);
48static void cyrix_timing(struct ata_device *, int, int);
49static void promise_timing(struct ata_device *, int, int);
50static void hpt_timing(struct ata_device *, int, int);
51static int hpt_cable80(struct ata_device *);
52
53/* misc defines */
54#define ATAPI_DEVICE(atadev) \
55	((atadev->unit == ATA_MASTER && \
56	  atadev->channel->devices & ATA_ATAPI_MASTER) || \
57	 (atadev->unit == ATA_SLAVE && \
58	  atadev->channel->devices & ATA_ATAPI_SLAVE))
59
60#define	MAXSEGSZ	PAGE_SIZE
61#define	MAXTABSZ	PAGE_SIZE
62#define	MAXCTLDMASZ	(2 * (MAXTABSZ + MAXPHYS))
63
64struct ata_dc_cb_args {
65    bus_addr_t maddr;
66    int error;
67};
68
69static void
70ata_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
71{
72    struct ata_dc_cb_args *cba = (struct ata_dc_cb_args *)xsc;
73
74    if (!(cba->error = error))
75	cba->maddr = segs[0].ds_addr;
76}
77
78int
79ata_dmaalloc(struct ata_device *atadev)
80{
81    struct ata_channel *ch;
82    struct ata_dc_cb_args ccba;
83    struct ata_dmastate *ds;
84    int error;
85
86    ch = atadev->channel;
87    ds = &atadev->dmastate;
88    if (!ds->cdmatag) {
89	if ((error = bus_dma_tag_create(ch->dmatag, 1, PAGE_SIZE,
90					BUS_SPACE_MAXADDR_32BIT,
91					BUS_SPACE_MAXADDR, NULL, NULL,
92					MAXTABSZ, 1, MAXTABSZ,
93					BUS_DMA_ALLOCNOW, &ds->cdmatag)))
94	    return error;
95    }
96    if (!ds->ddmatag) {
97	if ((error = bus_dma_tag_create(ch->dmatag, ch->alignment + 1, 0,
98					BUS_SPACE_MAXADDR_32BIT,
99					BUS_SPACE_MAXADDR, NULL, NULL,
100					MAXPHYS, ATA_DMA_ENTRIES, MAXSEGSZ,
101					BUS_DMA_ALLOCNOW, &ds->ddmatag)))
102	    return error;
103    }
104    if (!ds->mdmatab) {
105	if ((error = bus_dmamem_alloc(ds->cdmatag, (void **)&ds->dmatab, 0,
106				      &ds->cdmamap)))
107	    return error;
108
109	if ((error = bus_dmamap_load(ds->cdmatag, ds->cdmamap, ds->dmatab,
110				     MAXTABSZ, ata_dmasetupc_cb, &ccba,
111				     0)) != 0 || ccba.error != 0) {
112	    bus_dmamem_free(ds->cdmatag, ds->dmatab, ds->cdmamap);
113	    return error;
114	}
115	ds->mdmatab = ccba.maddr;
116    }
117    if (!ds->ddmamap) {
118	if ((error = bus_dmamap_create(ds->ddmatag, 0, &ds->ddmamap)) != 0)
119	    return error;
120    }
121    return 0;
122}
123
124void
125ata_dmafree(struct ata_device *atadev)
126{
127    struct ata_dmastate *ds;
128
129    ds = &atadev->dmastate;
130    if (ds->mdmatab) {
131	bus_dmamap_unload(ds->cdmatag, ds->cdmamap);
132	bus_dmamem_free(ds->cdmatag, ds->dmatab, ds->cdmamap);
133	ds->mdmatab = 0;
134	ds->cdmamap = NULL;
135	ds->dmatab = NULL;
136    }
137    if (ds->ddmamap) {
138	bus_dmamap_destroy(ds->ddmatag, ds->ddmamap);
139	ds->ddmamap = NULL;
140    }
141    if (ds->cdmatag) {
142	bus_dma_tag_destroy(ds->cdmatag);
143	ds->cdmatag = NULL;
144    }
145    if (ds->ddmatag) {
146	bus_dma_tag_destroy(ds->ddmatag);
147	ds->ddmatag = NULL;
148    }
149}
150
151void
152ata_dmafreetags(struct ata_channel *ch)
153{
154
155    if (ch->dmatag) {
156	bus_dma_tag_destroy(ch->dmatag);
157	ch->dmatag = NULL;
158    }
159}
160
161static void
162ata_dmacreate(struct ata_device *atadev, int apiomode, int mode)
163{
164
165    atadev->mode = mode;
166    if (!atadev->channel->dmatag) {
167	if (bus_dma_tag_create(NULL, 1, 0,
168			       BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
169			       NULL, NULL, MAXCTLDMASZ, ATA_DMA_ENTRIES,
170			       BUS_SPACE_MAXSIZE_32BIT, 0,
171			       &atadev->channel->dmatag)) {
172	    ata_prtdev(atadev, "DMA tag allocation failed, disabling DMA\n");
173	    ata_dmainit(atadev, apiomode, -1, -1);
174	}
175    }
176}
177
178void
179ata_dmainit(struct ata_device *atadev, int apiomode, int wdmamode, int udmamode)
180{
181    device_t parent = device_get_parent(atadev->channel->dev);
182    int chiptype = atadev->channel->chiptype;
183    int chiprev = pci_get_revid(parent);
184    int channel = atadev->channel->unit;
185    int device = ATA_DEV(atadev->unit);
186    int devno = (channel << 1) + device;
187    int error;
188
189    /* set our most pessimistic default mode */
190    atadev->mode = ATA_PIO;
191
192    if (!atadev->channel->r_bmio)
193	return;
194
195    /* if simplex controller, only allow DMA on primary channel */
196    if (channel == 1) {
197	ATA_OUTB(atadev->channel->r_bmio, ATA_BMSTAT_PORT,
198		 ATA_INB(atadev->channel->r_bmio, ATA_BMSTAT_PORT) &
199		 (ATA_BMSTAT_DMA_MASTER | ATA_BMSTAT_DMA_SLAVE));
200	if (ATA_INB(atadev->channel->r_bmio, ATA_BMSTAT_PORT) &
201	    ATA_BMSTAT_DMA_SIMPLEX) {
202	    ata_prtdev(atadev, "simplex device, DMA on primary only\n");
203	    return;
204	}
205    }
206
207    /* DMA engine address alignment is usually 1 word (2 bytes) */
208    atadev->channel->alignment = 0x1;
209
210#if 1
211    if (udmamode > 2 && !atadev->param->hwres_cblid) {
212	ata_prtdev(atadev,"DMA limited to UDMA33, non-ATA66 cable or device\n");
213	udmamode = 2;
214    }
215#endif
216    switch (chiptype) {
217
218    case 0x248a8086:	/* Intel ICH3 mobile */
219    case 0x248b8086:	/* Intel ICH3 */
220    case 0x244a8086:	/* Intel ICH2 mobile */
221    case 0x244b8086:	/* Intel ICH2 */
222	if (udmamode >= 5) {
223	    int32_t mask48, new48;
224	    int16_t word54;
225
226	    word54 = pci_read_config(parent, 0x54, 2);
227	    if (word54 & (0x10 << devno)) {
228		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
229				    ATA_UDMA5,	ATA_C_F_SETXFER,ATA_WAIT_READY);
230		if (bootverbose)
231		    ata_prtdev(atadev, "%s setting UDMA5 on Intel chip\n",
232			       (error) ? "failed" : "success");
233		if (!error) {
234		    mask48 = (1 << devno) + (3 << (16 + (devno << 2)));
235		    new48 = (1 << devno) + (1 << (16 + (devno << 2)));
236		    pci_write_config(parent, 0x48,
237				     (pci_read_config(parent, 0x48, 4) &
238				     ~mask48) | new48, 4);
239		    pci_write_config(parent, 0x54, word54 | (0x1000<<devno), 2);
240		    ata_dmacreate(atadev, apiomode, ATA_UDMA5);
241		    return;
242		}
243	    }
244	}
245	/* make sure eventual ATA100 mode from the BIOS is disabled */
246	pci_write_config(parent, 0x54,
247			 pci_read_config(parent, 0x54, 2) & ~(0x1000<<devno),2);
248	/* FALLTHROUGH */
249
250    case 0x24118086:	/* Intel ICH */
251    case 0x76018086:	/* Intel ICH */
252	if (udmamode >= 4) {
253	    int32_t mask48, new48;
254	    int16_t word54;
255
256	    word54 = pci_read_config(parent, 0x54, 2);
257	    if (word54 & (0x10 << devno)) {
258		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
259				    ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
260		if (bootverbose)
261		    ata_prtdev(atadev, "%s setting UDMA4 on Intel chip\n",
262			       (error) ? "failed" : "success");
263		if (!error) {
264		    mask48 = (1 << devno) + (3 << (16 + (devno << 2)));
265		    new48 = (1 << devno) + (2 << (16 + (devno << 2)));
266		    pci_write_config(parent, 0x48,
267				     (pci_read_config(parent, 0x48, 4) &
268				     ~mask48) | new48, 4);
269		    pci_write_config(parent, 0x54, word54 | (1 << devno), 2);
270		    ata_dmacreate(atadev, apiomode, ATA_UDMA4);
271		    return;
272		}
273	    }
274	}
275	/* make sure eventual ATA66 mode from the BIOS is disabled */
276	pci_write_config(parent, 0x54,
277			 pci_read_config(parent, 0x54, 2) & ~(1 << devno), 2);
278	/* FALLTHROUGH */
279
280    case 0x71118086:	/* Intel PIIX4 */
281    case 0x84CA8086:	/* Intel PIIX4 */
282    case 0x71998086:	/* Intel PIIX4e */
283    case 0x24218086:	/* Intel ICH0 */
284	if (udmamode >= 2) {
285	    int32_t mask48, new48;
286
287	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
288				ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
289	    if (bootverbose)
290		ata_prtdev(atadev, "%s setting UDMA2 on Intel chip\n",
291			   (error) ? "failed" : "success");
292	    if (!error) {
293		mask48 = (1 << devno) + (3 << (16 + (devno << 2)));
294		new48 = (1 << devno) + (2 << (16 + (devno << 2)));
295		pci_write_config(parent, 0x48,
296				 (pci_read_config(parent, 0x48, 4) &
297				 ~mask48) | new48, 4);
298		ata_dmacreate(atadev, apiomode, ATA_UDMA2);
299		return;
300	    }
301	}
302	/* make sure eventual ATA33 mode from the BIOS is disabled */
303	pci_write_config(parent, 0x48,
304			 pci_read_config(parent, 0x48, 4) & ~(1 << devno), 4);
305	/* FALLTHROUGH */
306
307    case 0x70108086:	/* Intel PIIX3 */
308	if (wdmamode >= 2 && apiomode >= 4) {
309	    int32_t mask40, new40, mask44, new44;
310
311	    /* if SITRE not set doit for both channels */
312	    if (!((pci_read_config(parent, 0x40, 4) >> (channel<<8)) & 0x4000)){
313		new40 = pci_read_config(parent, 0x40, 4);
314		new44 = pci_read_config(parent, 0x44, 4);
315		if (!(new40 & 0x00004000)) {
316		    new44 &= ~0x0000000f;
317		    new44 |= ((new40&0x00003000)>>10)|((new40&0x00000300)>>8);
318		}
319		if (!(new40 & 0x40000000)) {
320		    new44 &= ~0x000000f0;
321		    new44 |= ((new40&0x30000000)>>22)|((new40&0x03000000)>>20);
322		}
323		new40 |= 0x40004000;
324		pci_write_config(parent, 0x40, new40, 4);
325		pci_write_config(parent, 0x44, new44, 4);
326	    }
327	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
328				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
329	    if (bootverbose)
330		ata_prtdev(atadev, "%s setting WDMA2 on Intel chip\n",
331			   (error) ? "failed" : "success");
332	    if (!error) {
333		if (device == ATA_MASTER) {
334		    mask40 = 0x0000330f;
335		    new40 = 0x00002307;
336		    mask44 = 0;
337		    new44 = 0;
338		}
339		else {
340		    mask40 = 0x000000f0;
341		    new40 = 0x00000070;
342		    mask44 = 0x0000000f;
343		    new44 = 0x0000000b;
344		}
345		if (channel) {
346		    mask40 <<= 16;
347		    new40 <<= 16;
348		    mask44 <<= 4;
349		    new44 <<= 4;
350		}
351		pci_write_config(parent, 0x40,
352				 (pci_read_config(parent, 0x40, 4) & ~mask40)|
353				 new40, 4);
354		pci_write_config(parent, 0x44,
355				 (pci_read_config(parent, 0x44, 4) & ~mask44)|
356				 new44, 4);
357		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
358		return;
359	    }
360	}
361	/* we could set PIO mode timings, but we assume the BIOS did that */
362	break;
363
364    case 0x12308086:	/* Intel PIIX */
365	if (wdmamode >= 2 && apiomode >= 4) {
366	    int32_t word40;
367
368	    word40 = pci_read_config(parent, 0x40, 4);
369	    word40 >>= channel * 16;
370
371	    /* Check for timing config usable for DMA on controller */
372	    if (!((word40 & 0x3300) == 0x2300 &&
373		  ((word40 >> (device ? 4 : 0)) & 1) == 1))
374		break;
375
376	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
377				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
378	    if (bootverbose)
379		ata_prtdev(atadev, "%s setting WDMA2 on Intel chip\n",
380			   (error) ? "failed" : "success");
381	    if (!error) {
382		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
383		return;
384	    }
385	}
386	break;
387
388    case 0x522910b9:	/* AcerLabs Aladdin IV/V */
389	/* the older Aladdin doesn't support ATAPI DMA on both master & slave */
390	if (chiprev < 0xc2 &&
391	    atadev->channel->devices & ATA_ATAPI_MASTER &&
392	    atadev->channel->devices & ATA_ATAPI_SLAVE) {
393	    ata_prtdev(atadev, "two atapi devices on this channel, no DMA\n");
394	    break;
395	}
396	pci_write_config(parent, 0x58 + (channel << 2), 0x00310001, 4);
397	if (udmamode >= 5 && chiprev >= 0xc4) {
398	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
399				ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
400	    if (bootverbose)
401		ata_prtdev(atadev, "%s setting UDMA5 on Acer chip\n",
402			   (error) ? "failed" : "success");
403	    if (!error) {
404		int32_t word54 = pci_read_config(parent, 0x54, 4);
405
406		pci_write_config(parent, 0x4b,
407				 pci_read_config(parent, 0x4b, 1) | 0x01, 1);
408		word54 &= ~(0x000f000f << (devno << 2));
409		word54 |= (0x000f0005 << (devno << 2));
410		pci_write_config(parent, 0x54, word54, 4);
411		pci_write_config(parent, 0x53,
412				 pci_read_config(parent, 0x53, 1) | 0x03, 1);
413		ata_dmacreate(atadev, apiomode, ATA_UDMA5);
414		return;
415	    }
416	}
417	if (udmamode >= 4 && chiprev >= 0xc2) {
418	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
419				ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
420	    if (bootverbose)
421		ata_prtdev(atadev, "%s setting UDMA4 on Acer chip\n",
422			   (error) ? "failed" : "success");
423	    if (!error) {
424		int32_t word54 = pci_read_config(parent, 0x54, 4);
425
426		pci_write_config(parent, 0x4b,
427				 pci_read_config(parent, 0x4b, 1) | 0x01, 1);
428		word54 &= ~(0x000f000f << (devno << 2));
429		word54 |= (0x00080005 << (devno << 2));
430		pci_write_config(parent, 0x54, word54, 4);
431		pci_write_config(parent, 0x53,
432				 pci_read_config(parent, 0x53, 1) | 0x03, 1);
433		ata_dmacreate(atadev, apiomode, ATA_UDMA4);
434		return;
435	    }
436	}
437	if (udmamode >= 2 && chiprev >= 0x20) {
438	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
439				ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
440	    if (bootverbose)
441		ata_prtdev(atadev, "%s setting UDMA2 on Acer chip\n",
442			   (error) ? "failed" : "success");
443	    if (!error) {
444		int32_t word54 = pci_read_config(parent, 0x54, 4);
445
446		word54 &= ~(0x000f000f << (devno << 2));
447		word54 |= (0x000a0005 << (devno << 2));
448		pci_write_config(parent, 0x54, word54, 4);
449		pci_write_config(parent, 0x53,
450				 pci_read_config(parent, 0x53, 1) | 0x03, 1);
451		atadev->channel->flags |= ATA_ATAPI_DMA_RO;
452		ata_dmacreate(atadev, apiomode, ATA_UDMA2);
453		return;
454	    }
455	}
456
457	/* make sure eventual UDMA mode from the BIOS is disabled */
458	pci_write_config(parent, 0x56, pci_read_config(parent, 0x56, 2) &
459				       ~(0x0008 << (devno << 2)), 2);
460
461	if (wdmamode >= 2 && apiomode >= 4) {
462	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
463				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
464	    if (bootverbose)
465		ata_prtdev(atadev, "%s setting WDMA2 on Acer chip\n",
466			   (error) ? "failed" : "success");
467	    if (!error) {
468		pci_write_config(parent, 0x53,
469				 pci_read_config(parent, 0x53, 1) | 0x03, 1);
470		atadev->channel->flags |= ATA_ATAPI_DMA_RO;
471		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
472		return;
473	    }
474	}
475	pci_write_config(parent, 0x53,
476			 (pci_read_config(parent, 0x53, 1) & ~0x01) | 0x02, 1);
477	error = ata_command(atadev, ATA_C_SETFEATURES, 0,
478			    ATA_PIO0 + apiomode,
479			    ATA_C_F_SETXFER, ATA_WAIT_READY);
480	if (bootverbose)
481	    ata_prtdev(atadev, "%s setting PIO%d on Acer chip\n",
482		       (error) ? "failed" : "success",
483		       (apiomode >= 0) ? apiomode : 0);
484	if (!error) {
485	    int32_t word54 = pci_read_config(parent, 0x54, 4);
486	    int32_t timing;
487
488	    switch(ATA_PIO0 + apiomode) {
489	    case ATA_PIO0: timing = 0x006d0003;
490	    case ATA_PIO1: timing = 0x00580002;
491	    case ATA_PIO2: timing = 0x00440001;
492	    case ATA_PIO3: timing = 0x00330001;
493	    case ATA_PIO4: timing = 0x00310001;
494	    default:	   timing = 0x006d0003;
495	    }
496	    pci_write_config(parent, 0x58 + (channel << 2), timing, 4);
497	    word54 &= ~(0x000f000f << (devno << 2));
498	    word54 |= (0x00000004 << (devno << 2));
499	    pci_write_config(parent, 0x54, word54, 4);
500	    atadev->mode = ATA_PIO0 + apiomode;
501	    return;
502	}
503	break;
504
505    case 0x01bc10de:	/* nVIDIA nForce */
506    case 0x74411022:	/* AMD 768 */
507    case 0x74111022:	/* AMD 766 */
508    case 0x74091022:	/* AMD 756 */
509    case 0x05711106:	/* VIA 82C571, 82C586, 82C596, 82C686 , 8231, 8233 */
510	{
511	    int via_modes[5][7] = {
512		{ 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00 },	/* VIA ATA33 */
513		{ 0x00, 0x00, 0xea, 0x00, 0xe8, 0x00, 0x00 },	/* VIA ATA66 */
514		{ 0x00, 0x00, 0xf4, 0x00, 0xf1, 0xf0, 0x00 },	/* VIA ATA100 */
515		{ 0x00, 0x00, 0xf6, 0x00, 0xf2, 0xf1, 0xf0 },	/* VIA ATA133 */
516		{ 0x00, 0x00, 0xc0, 0x00, 0xc5, 0xc6, 0x00 }};	/* AMD/nVIDIA */
517	    int *reg_val = NULL;
518	    char *chip = "VIA";
519
520	    if (ata_find_dev(parent, 0x31471106, 0)) {		/* 8233a */
521		udmamode = imin(udmamode, 6);
522		reg_val = via_modes[3];
523	    }
524	    else if (ata_find_dev(parent, 0x06861106, 0x40) ||	/* 82C686b */
525		ata_find_dev(parent, 0x82311106, 0) ||		/* 8231 */
526		ata_find_dev(parent, 0x30741106, 0) ||		/* 8233 */
527		ata_find_dev(parent, 0x31091106, 0)) {		/* 8233c */
528		udmamode = imin(udmamode, 5);
529		reg_val = via_modes[2];
530	    }
531	    else if (ata_find_dev(parent, 0x06861106, 0x10) ||	/* 82C686a */
532		     ata_find_dev(parent, 0x05961106, 0x12)) {	/* 82C596b */
533		udmamode = imin(udmamode, 4);
534		reg_val = via_modes[1];
535	    }
536	    else if (ata_find_dev(parent, 0x06861106, 0)) {	/* 82C686 */
537		udmamode = imin(udmamode, 2);
538		reg_val = via_modes[1];
539	    }
540	    else if (ata_find_dev(parent, 0x05961106, 0) ||	/* 82C596a */
541		     ata_find_dev(parent, 0x05861106, 0x03)) {	/* 82C586b */
542		udmamode = imin(udmamode, 2);
543		reg_val = via_modes[0];
544	    }
545	    else if (chiptype == 0x74411022 ||			/* AMD 768 */
546		     chiptype == 0x74111022) {			/* AMD 766 */
547		udmamode = imin(udmamode, 5);
548		reg_val = via_modes[4];
549		chip = "AMD";
550	    }
551	    else if (chiptype == 0x74091022) {			/* AMD 756 */
552		udmamode = imin(udmamode, 4);
553		reg_val = via_modes[4];
554		chip = "AMD";
555	    }
556	    else if (chiptype == 0x01bc10de) {			/* nVIDIA */
557		udmamode = imin(udmamode, 5);
558		reg_val = via_modes[4];
559		chip = "nVIDIA";
560	    }
561	    else
562		udmamode = 0;
563
564	    if (udmamode >= 6) {
565		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
566				    ATA_UDMA6, ATA_C_F_SETXFER, ATA_WAIT_READY);
567		if (bootverbose)
568		    ata_prtdev(atadev, "%s setting UDMA6 on %s chip\n",
569			       (error) ? "failed" : "success", chip);
570		if (!error) {
571		    pci_write_config(parent, 0x53 - devno, reg_val[6], 1);
572		    ata_dmacreate(atadev, apiomode, ATA_UDMA6);
573		    return;
574		}
575	    }
576	    if (udmamode >= 5) {
577		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
578				    ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
579		if (bootverbose)
580		    ata_prtdev(atadev, "%s setting UDMA5 on %s chip\n",
581			       (error) ? "failed" : "success", chip);
582		if (!error) {
583		    pci_write_config(parent, 0x53 - devno, reg_val[5], 1);
584		    ata_dmacreate(atadev, apiomode, ATA_UDMA5);
585		    return;
586		}
587	    }
588	    if (udmamode >= 4) {
589		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
590				    ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
591		if (bootverbose)
592		    ata_prtdev(atadev, "%s setting UDMA4 on %s chip\n",
593			       (error) ? "failed" : "success", chip);
594		if (!error) {
595		    pci_write_config(parent, 0x53 - devno, reg_val[4], 1);
596		    ata_dmacreate(atadev, apiomode, ATA_UDMA4);
597		    return;
598		}
599	    }
600	    if (udmamode >= 2) {
601		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
602				    ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
603		if (bootverbose)
604		    ata_prtdev(atadev, "%s setting UDMA2 on %s chip\n",
605			       (error) ? "failed" : "success", chip);
606		if (!error) {
607		    pci_write_config(parent, 0x53 - devno, reg_val[2], 1);
608		    ata_dmacreate(atadev, apiomode, ATA_UDMA2);
609		    return;
610		}
611	    }
612	    if (wdmamode >= 2 && apiomode >= 4) {
613		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
614				    ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
615		if (bootverbose)
616		    ata_prtdev(atadev, "%s setting WDMA2 on %s chip\n",
617			       (error) ? "failed" : "success", chip);
618		if (!error) {
619		    pci_write_config(parent, 0x53 - devno, 0x0b, 1);
620		    pci_write_config(parent, 0x4b - devno, 0x31, 1);
621		    ata_dmacreate(atadev, apiomode, ATA_WDMA2);
622		    return;
623		}
624	    }
625	}
626	/* we could set PIO mode timings, but we assume the BIOS did that */
627	break;
628
629    case 0x55131039:	/* SiS 5591 */
630	if (ata_find_dev(parent, 0x06301039, 0x30) ||	/* SiS 630 */
631	    ata_find_dev(parent, 0x06331039, 0) ||	/* SiS 633 */
632	    ata_find_dev(parent, 0x06351039, 0) ||	/* SiS 635 */
633	    ata_find_dev(parent, 0x06401039, 0) ||	/* SiS 640 */
634	    ata_find_dev(parent, 0x06451039, 0) ||	/* SiS 645 */
635	    ata_find_dev(parent, 0x06501039, 0) ||	/* SiS 650 */
636	    ata_find_dev(parent, 0x07301039, 0) ||	/* SiS 730 */
637	    ata_find_dev(parent, 0x07331039, 0) ||	/* SiS 733 */
638	    ata_find_dev(parent, 0x07351039, 0) ||	/* SiS 735 */
639	    ata_find_dev(parent, 0x07401039, 0) ||	/* SiS 740 */
640	    ata_find_dev(parent, 0x07451039, 0) ||	/* SiS 745 */
641	    ata_find_dev(parent, 0x07501039, 0)) {	/* SiS 750 */
642	    int8_t reg = 0x40 + (devno << 1);
643	    int16_t val = pci_read_config(parent, reg, 2) & 0x0fff;
644
645	    if (udmamode >= 5) {
646		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
647				    ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
648		if (bootverbose)
649		    ata_prtdev(atadev, "%s setting UDMA5 on SiS chip\n",
650			       (error) ? "failed" : "success");
651		if (!error) {
652		    pci_write_config(parent, reg, val | 0x8000, 2);
653		    ata_dmacreate(atadev, apiomode, ATA_UDMA5);
654		    return;
655		}
656	    }
657	    if (udmamode >= 4) {
658		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
659				    ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
660		if (bootverbose)
661		    ata_prtdev(atadev, "%s setting UDMA4 on SiS chip\n",
662			       (error) ? "failed" : "success");
663		if (!error) {
664		    pci_write_config(parent, reg, val | 0x9000, 2);
665		    ata_dmacreate(atadev, apiomode, ATA_UDMA4);
666		    return;
667		}
668	    }
669	    if (udmamode >= 2) {
670		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
671				    ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
672		if (bootverbose)
673		    ata_prtdev(atadev, "%s setting UDMA2 on SiS chip\n",
674			       (error) ? "failed" : "success");
675		if (!error) {
676		    pci_write_config(parent, reg, val | 0xb000, 2);
677		    ata_dmacreate(atadev, apiomode, ATA_UDMA2);
678		    return;
679		}
680	    }
681	} else if (ata_find_dev(parent, 0x05301039, 0) || /* SiS 530 */
682		   ata_find_dev(parent, 0x05401039, 0) || /* SiS 540 */
683		   ata_find_dev(parent, 0x06201039, 0) || /* SiS 620 */
684		   ata_find_dev(parent, 0x06301039, 0)) { /* SiS 630 */
685	    int8_t reg = 0x40 + (devno << 1);
686	    int16_t val = pci_read_config(parent, reg, 2) & 0x0fff;
687
688	    if (udmamode >= 4) {
689		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
690				    ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
691		if (bootverbose)
692		    ata_prtdev(atadev, "%s setting UDMA4 on SiS chip\n",
693			       (error) ? "failed" : "success");
694		if (!error) {
695		    pci_write_config(parent, reg, val | 0x9000, 2);
696		    ata_dmacreate(atadev, apiomode, ATA_UDMA4);
697		    return;
698		}
699	    }
700	    if (udmamode >= 2) {
701		error = ata_command(atadev, ATA_C_SETFEATURES, 0,
702				    ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
703		if (bootverbose)
704		    ata_prtdev(atadev, "%s setting UDMA2 on SiS chip\n",
705			       (error) ? "failed" : "success");
706		if (!error) {
707		    pci_write_config(parent, reg, val | 0xa000, 2);
708		    ata_dmacreate(atadev, apiomode, ATA_UDMA2);
709		    return;
710		}
711	    }
712	} else if (udmamode >= 2 && chiprev > 0xc1) {
713	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
714				ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
715	    if (bootverbose)
716		ata_prtdev(atadev, "%s setting UDMA2 on SiS chip\n",
717			   (error) ? "failed" : "success");
718	    if (!error) {
719		pci_write_config(parent, 0x40 + (devno << 1), 0xa301, 2);
720		ata_dmacreate(atadev, apiomode, ATA_UDMA2);
721		return;
722	    }
723	}
724	if (wdmamode >=2 && apiomode >= 4) {
725	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
726				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
727	    if (bootverbose)
728		ata_prtdev(atadev, "%s setting WDMA2 on SiS chip\n",
729			   (error) ? "failed" : "success");
730	    if (!error) {
731		pci_write_config(parent, 0x40 + (devno << 1), 0x0301, 2);
732		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
733		return;
734	    }
735	}
736	/* we could set PIO mode timings, but we assume the BIOS did that */
737	break;
738
739    case 0x06491095:	/* CMD 649 ATA100 controller */
740	if (udmamode >= 5) {
741	    u_int8_t umode;
742
743	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
744				ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
745	    if (bootverbose)
746		ata_prtdev(atadev, "%s setting UDMA5 on CMD chip\n",
747			   (error) ? "failed" : "success");
748	    if (!error) {
749		umode = pci_read_config(parent, channel ? 0x7b : 0x73, 1);
750		umode &= ~(device ? 0xca : 0x35);
751		umode |= (device ? 0x0a : 0x05);
752		pci_write_config(parent, channel ? 0x7b : 0x73, umode, 1);
753		ata_dmacreate(atadev, apiomode, ATA_UDMA5);
754		return;
755	    }
756	}
757	/* FALLTHROUGH */
758
759    case 0x06481095:	/* CMD 648 ATA66 controller */
760	if (udmamode >= 4) {
761	    u_int8_t umode;
762
763	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
764				ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
765	    if (bootverbose)
766		ata_prtdev(atadev, "%s setting UDMA4 on CMD chip\n",
767			   (error) ? "failed" : "success");
768	    if (!error) {
769		umode = pci_read_config(parent, channel ? 0x7b : 0x73, 1);
770		umode &= ~(device ? 0xca : 0x35);
771		umode |= (device ? 0x4a : 0x15);
772		pci_write_config(parent, channel ? 0x7b : 0x73, umode, 1);
773		ata_dmacreate(atadev, apiomode, ATA_UDMA4);
774		return;
775	    }
776	}
777	if (udmamode >= 2) {
778	    u_int8_t umode;
779
780	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
781				ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
782	    if (bootverbose)
783		ata_prtdev(atadev, "%s setting UDMA2 on CMD chip\n",
784			   (error) ? "failed" : "success");
785	    if (!error) {
786		umode = pci_read_config(parent, channel ? 0x7b : 0x73, 1);
787		umode &= ~(device ? 0xca : 0x35);
788		umode |= (device ? 0x42 : 0x11);
789		pci_write_config(parent, channel ? 0x7b : 0x73, umode, 1);
790		ata_dmacreate(atadev, apiomode, ATA_UDMA2);
791		return;
792	    }
793	}
794	/* make sure eventual UDMA mode from the BIOS is disabled */
795	pci_write_config(parent, channel ? 0x7b : 0x73,
796			 pci_read_config(parent, channel ? 0x7b : 0x73, 1) &
797			 		 ~(device ? 0xca : 0x53), 1);
798	/* FALLTHROUGH */
799
800    case 0x06461095:	/* CMD 646 ATA controller */
801	if (wdmamode >= 2 && apiomode >= 4) {
802	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
803				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
804	    if (bootverbose)
805		ata_prtdev(atadev, "%s setting WDMA2 on CMD chip\n",
806			   error ? "failed" : "success");
807	    if (!error) {
808		int32_t offset = (devno < 3) ? (devno << 1) : 7;
809
810		pci_write_config(parent, 0x54 + offset, 0x3f, 1);
811		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
812		return;
813	    }
814	}
815	/* we could set PIO mode timings, but we assume the BIOS did that */
816	break;
817
818    case 0xc6931080:	/* Cypress 82c693 ATA controller */
819	if (wdmamode >= 2 && apiomode >= 4) {
820	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
821				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
822	    if (bootverbose)
823		ata_prtdev(atadev, "%s setting WDMA2 on Cypress chip\n",
824			   error ? "failed" : "success");
825	    if (!error) {
826		pci_write_config(atadev->channel->dev,
827				 channel ? 0x4e:0x4c, 0x2020, 2);
828		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
829		return;
830	    }
831	}
832	/* we could set PIO mode timings, but we assume the BIOS did that */
833	break;
834
835    case 0x01021078:	/* Cyrix 5530 ATA33 controller */
836	atadev->channel->alignment = 0xf;
837	if (udmamode >= 2) {
838	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
839				ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
840	    if (bootverbose)
841		ata_prtdev(atadev, "%s setting UDMA2 on Cyrix chip\n",
842			   (error) ? "failed" : "success");
843	    if (!error) {
844		cyrix_timing(atadev, devno, ATA_UDMA2);
845		ata_dmacreate(atadev, apiomode, ATA_UDMA2);
846		return;
847	    }
848	}
849	if (wdmamode >= 2 && apiomode >= 4) {
850	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
851				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
852	    if (bootverbose)
853		ata_prtdev(atadev, "%s setting WDMA2 on Cyrix chip\n",
854			   (error) ? "failed" : "success");
855	    if (!error) {
856		cyrix_timing(atadev, devno, ATA_WDMA2);
857		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
858		return;
859	    }
860	}
861	error = ata_command(atadev, ATA_C_SETFEATURES, 0,
862			    ATA_PIO0 + apiomode, ATA_C_F_SETXFER,
863			    ATA_WAIT_READY);
864	if (bootverbose)
865	    ata_prtdev(atadev, "%s setting %s on Cyrix chip\n",
866		       (error) ? "failed" : "success",
867		       ata_mode2str(ATA_PIO0 + apiomode));
868	cyrix_timing(atadev, devno, ATA_PIO0 + apiomode);
869	atadev->mode = ATA_PIO0 + apiomode;
870	return;
871
872    case 0x02121166:	/* ServerWorks CSB5 ATA66/100 controller */
873	if (udmamode >= 5 && chiprev >= 0x92) {
874	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
875				ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
876	    if (bootverbose)
877		ata_prtdev(atadev, "%s setting UDMA5 on ServerWorks chip\n",
878			   (error) ? "failed" : "success");
879	    if (!error) {
880		u_int16_t reg56;
881
882		pci_write_config(parent, 0x54,
883				 pci_read_config(parent, 0x54, 1) |
884				 (0x01 << devno), 1);
885		reg56 = pci_read_config(parent, 0x56, 2);
886		reg56 &= ~(0xf << (devno * 4));
887		reg56 |= (0x5 << (devno * 4));
888		pci_write_config(parent, 0x56, reg56, 2);
889		ata_dmacreate(atadev, apiomode, ATA_UDMA5);
890		return;
891	    }
892	}
893	if (udmamode >= 4) {
894	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
895				ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
896	    if (bootverbose)
897		ata_prtdev(atadev, "%s setting UDMA4 on ServerWorks chip\n",
898			   (error) ? "failed" : "success");
899	    if (!error) {
900		u_int16_t reg56;
901
902		pci_write_config(parent, 0x54,
903				 pci_read_config(parent, 0x54, 1) |
904				 (0x01 << devno), 1);
905		reg56 = pci_read_config(parent, 0x56, 2);
906		reg56 &= ~(0xf << (devno * 4));
907		reg56 |= (0x4 << (devno * 4));
908		pci_write_config(parent, 0x56, reg56, 2);
909		ata_dmacreate(atadev, apiomode, ATA_UDMA4);
910		return;
911	    }
912	}
913	/* FALLTHROUGH */
914
915    case 0x02111166:	/* ServerWorks ROSB4 ATA33 controller */
916	if (udmamode >= 2) {
917	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
918				ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
919	    if (bootverbose)
920		ata_prtdev(atadev, "%s setting UDMA2 on ServerWorks chip\n",
921			   (error) ? "failed" : "success");
922	    if (!error) {
923		u_int16_t reg56;
924
925		pci_write_config(parent, 0x54,
926				 pci_read_config(parent, 0x54, 1) |
927				 (0x01 << devno), 1);
928		reg56 = pci_read_config(parent, 0x56, 2);
929		reg56 &= ~(0xf << (devno * 4));
930		reg56 |= (0x2 << (devno * 4));
931		pci_write_config(parent, 0x56, reg56, 2);
932		ata_dmacreate(atadev, apiomode, ATA_UDMA2);
933		return;
934	    }
935	}
936	if (wdmamode >= 2 && apiomode >= 4) {
937	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
938				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
939	    if (bootverbose)
940		ata_prtdev(atadev, "%s setting WDMA2 on ServerWorks chip\n",
941			   (error) ? "failed" : "success");
942	    if (!error) {
943		int offset = devno ^ 0x01;
944		int word44 = pci_read_config(parent, 0x44, 4);
945
946		pci_write_config(parent, 0x54,
947				 pci_read_config(parent, 0x54, 1) &
948				 ~(0x01 << devno), 1);
949		word44 &= ~(0xff << (offset << 8));
950		word44 |= (0x20 << (offset << 8));
951		pci_write_config(parent, 0x44, 0x20, 4);
952		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
953		return;
954	    }
955	}
956	/* we could set PIO mode timings, but we assume the BIOS did that */
957	break;
958
959    case 0x4d69105a:	/* Promise TX2 ATA133 controllers */
960    case 0x5275105a:	/* Promise TX2 ATA133 controllers */
961    case 0x6269105a:	/* Promise TX2 ATA133 controllers */
962	ATA_OUTB(atadev->channel->r_bmio, ATA_BMDEVSPEC_0, 0x0b);
963	if (udmamode >= 6 &&
964	    !(ATA_INB(atadev->channel->r_bmio, ATA_BMDEVSPEC_1) & 0x04)) {
965	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
966				ATA_UDMA6, ATA_C_F_SETXFER, ATA_WAIT_READY);
967	    if (bootverbose)
968		ata_prtdev(atadev, "%s setting UDMA6 on Promise chip\n",
969			   (error) ? "failed" : "success");
970	    if (!error) {
971		ata_dmacreate(atadev, apiomode, ATA_UDMA6);
972		return;
973	    }
974	}
975	/* FALLTHROUGH */
976
977    case 0x4d68105a:	/* Promise TX2 ATA100 controllers */
978    case 0x6268105a:	/* Promise TX2 ATA100 controllers */
979	ATA_OUTB(atadev->channel->r_bmio, ATA_BMDEVSPEC_0, 0x0b);
980	if (udmamode >= 5 &&
981	    !(ATA_INB(atadev->channel->r_bmio, ATA_BMDEVSPEC_1) & 0x04)) {
982	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
983				ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
984	    if (bootverbose)
985		ata_prtdev(atadev, "%s setting UDMA5 on Promise chip\n",
986			   (error) ? "failed" : "success");
987	    if (!error) {
988		ata_dmacreate(atadev, apiomode, ATA_UDMA5);
989		return;
990	    }
991	}
992	ATA_OUTB(atadev->channel->r_bmio, ATA_BMDEVSPEC_0, 0x0b);
993	if (udmamode >= 4 &&
994	    !(ATA_INB(atadev->channel->r_bmio, ATA_BMDEVSPEC_1) & 0x04)) {
995	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
996				ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
997	    if (bootverbose)
998		ata_prtdev(atadev, "%s setting UDMA4 on Promise chip\n",
999			   (error) ? "failed" : "success");
1000	    if (!error) {
1001		ata_dmacreate(atadev, apiomode, ATA_UDMA4);
1002		return;
1003	    }
1004	}
1005	if (udmamode >= 2) {
1006	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1007				ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
1008	    if (bootverbose)
1009		ata_prtdev(atadev, "%s setting UDMA on Promise chip\n",
1010			   (error) ? "failed" : "success");
1011	    if (!error) {
1012		ata_dmacreate(atadev, apiomode, ATA_UDMA2);
1013		return;
1014	    }
1015	}
1016	if (wdmamode >= 2 && apiomode >= 4) {
1017	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1018				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
1019	    if (bootverbose)
1020		ata_prtdev(atadev, "%s setting WDMA2 on Promise chip\n",
1021			   (error) ? "failed" : "success");
1022	    if (!error) {
1023		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
1024		return;
1025	    }
1026	}
1027	break;
1028
1029    case 0x4d30105a:	/* Promise Ultra/FastTrak 100 controllers */
1030    case 0x0d30105a:	/* Promise OEM ATA100 controllers */
1031	if (!ATAPI_DEVICE(atadev) && udmamode >= 5 &&
1032	    !(pci_read_config(parent, 0x50, 2) & (channel ? 1<<11 : 1<<10))) {
1033	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1034				ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
1035	    if (bootverbose)
1036		ata_prtdev(atadev, "%s setting UDMA5 on Promise chip\n",
1037			   (error) ? "failed" : "success");
1038	    if (!error) {
1039		promise_timing(atadev, devno, ATA_UDMA5);
1040		ata_dmacreate(atadev, apiomode, ATA_UDMA5);
1041		return;
1042	    }
1043	}
1044	/* FALLTHROUGH */
1045
1046    case 0x4d38105a:	/* Promise Ultra/FastTrak 66 controllers */
1047	if (!ATAPI_DEVICE(atadev) && udmamode >= 4 &&
1048	    !(pci_read_config(parent, 0x50, 2) & (channel ? 1<<11 : 1<<10))) {
1049	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1050				ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
1051	    if (bootverbose)
1052		ata_prtdev(atadev, "%s setting UDMA4 on Promise chip\n",
1053			   (error) ? "failed" : "success");
1054	    if (!error) {
1055		promise_timing(atadev, devno, ATA_UDMA4);
1056		ata_dmacreate(atadev, apiomode, ATA_UDMA4);
1057		return;
1058	    }
1059	}
1060	/* FALLTHROUGH */
1061
1062    case 0x4d33105a:	/* Promise Ultra/FastTrak 33 controllers */
1063	if (!ATAPI_DEVICE(atadev) && udmamode >= 2) {
1064	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1065				ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
1066	    if (bootverbose)
1067		ata_prtdev(atadev, "%s setting UDMA2 on Promise chip\n",
1068			   (error) ? "failed" : "success");
1069	    if (!error) {
1070		promise_timing(atadev, devno, ATA_UDMA2);
1071		ata_dmacreate(atadev, apiomode, ATA_UDMA2);
1072		return;
1073	    }
1074	}
1075	if (!ATAPI_DEVICE(atadev) && wdmamode >= 2 && apiomode >= 4) {
1076	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1077				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
1078	    if (bootverbose)
1079		ata_prtdev(atadev, "%s setting WDMA2 on Promise chip\n",
1080			   (error) ? "failed" : "success");
1081	    if (!error) {
1082		promise_timing(atadev, devno, ATA_WDMA2);
1083		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
1084		return;
1085	    }
1086	}
1087	error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1088			    ATA_PIO0 + apiomode,
1089			    ATA_C_F_SETXFER, ATA_WAIT_READY);
1090	if (bootverbose)
1091	    ata_prtdev(atadev, "%s setting PIO%d on Promise chip\n",
1092		       (error) ? "failed" : "success",
1093		       (apiomode >= 0) ? apiomode : 0);
1094	promise_timing(atadev, devno, ATA_PIO0 + apiomode);
1095	atadev->mode = ATA_PIO0 + apiomode;
1096	return;
1097
1098    case 0x00041103:	/* HighPoint HPT366/368/370/372 controllers */
1099    case 0x00051103:	/* HighPoint HPT372 controllers */
1100    case 0x00081103:	/* HighPoint HPT374 controllers */
1101	if (!ATAPI_DEVICE(atadev) && udmamode >= 6 && hpt_cable80(atadev) &&
1102	    ((chiptype == 0x00041103 && chiprev >= 0x05) ||
1103	     (chiptype == 0x00051103 && chiprev >= 0x01) ||
1104	     (chiptype == 0x00081103 && chiprev >= 0x07))) {
1105	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1106				ATA_UDMA6, ATA_C_F_SETXFER, ATA_WAIT_READY);
1107	    if (bootverbose)
1108		ata_prtdev(atadev, "%s setting UDMA6 on HighPoint chip\n",
1109			   (error) ? "failed" : "success");
1110	    if (!error) {
1111		hpt_timing(atadev, devno, ATA_UDMA6);
1112		ata_dmacreate(atadev, apiomode, ATA_UDMA6);
1113		return;
1114	    }
1115	}
1116	if (!ATAPI_DEVICE(atadev) && udmamode >= 5 && hpt_cable80(atadev) &&
1117	    ((chiptype == 0x00041103 && chiprev >= 0x03) ||
1118	     (chiptype == 0x00051103 && chiprev >= 0x01) ||
1119	     (chiptype == 0x00081103 && chiprev >= 0x07))) {
1120	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1121				ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
1122	    if (bootverbose)
1123		ata_prtdev(atadev, "%s setting UDMA5 on HighPoint chip\n",
1124			   (error) ? "failed" : "success");
1125	    if (!error) {
1126		hpt_timing(atadev, devno, ATA_UDMA5);
1127		ata_dmacreate(atadev, apiomode, ATA_UDMA5);
1128		return;
1129	    }
1130	}
1131	if (!ATAPI_DEVICE(atadev) && udmamode >= 4 && hpt_cable80(atadev)) {
1132	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1133				ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
1134	    if (bootverbose)
1135		ata_prtdev(atadev, "%s setting UDMA4 on HighPoint chip\n",
1136			   (error) ? "failed" : "success");
1137	    if (!error) {
1138		hpt_timing(atadev, devno, ATA_UDMA4);
1139		ata_dmacreate(atadev, apiomode, ATA_UDMA4);
1140		return;
1141	    }
1142	}
1143	if (!ATAPI_DEVICE(atadev) && udmamode >= 2) {
1144	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1145				ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
1146	    if (bootverbose)
1147		ata_prtdev(atadev, "%s setting UDMA2 on HighPoint chip\n",
1148			   (error) ? "failed" : "success");
1149	    if (!error) {
1150		hpt_timing(atadev, devno, ATA_UDMA2);
1151		ata_dmacreate(atadev, apiomode, ATA_UDMA2);
1152		return;
1153	    }
1154	}
1155	if (!ATAPI_DEVICE(atadev) && wdmamode >= 2 && apiomode >= 4) {
1156	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1157				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
1158	    if (bootverbose)
1159		ata_prtdev(atadev, "%s setting WDMA2 on HighPoint chip\n",
1160			   (error) ? "failed" : "success");
1161	    if (!error) {
1162		hpt_timing(atadev, devno, ATA_WDMA2);
1163		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
1164		return;
1165	    }
1166	}
1167	error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1168			    ATA_PIO0 + apiomode,
1169			    ATA_C_F_SETXFER, ATA_WAIT_READY);
1170	if (bootverbose)
1171	    ata_prtdev(atadev, "%s setting PIO%d on HighPoint chip\n",
1172		       (error) ? "failed" : "success",
1173		       (apiomode >= 0) ? apiomode : 0);
1174	hpt_timing(atadev, devno, ATA_PIO0 + apiomode);
1175	atadev->mode = ATA_PIO0 + apiomode;
1176	return;
1177
1178    case 0x000116ca:	/* Cenatek Rocket Drive controller */
1179	if (wdmamode >= 0 &&
1180	    (ATA_INB(atadev->channel->r_bmio, ATA_BMSTAT_PORT) &
1181	     (device ? ATA_BMSTAT_DMA_SLAVE : ATA_BMSTAT_DMA_MASTER)))
1182	    ata_dmacreate(atadev, apiomode, ATA_DMA);
1183	else
1184	    atadev->mode = ATA_PIO;
1185	return;
1186
1187    default:		/* unknown controller chip */
1188	/* better not try generic DMA on ATAPI devices it almost never works */
1189	if (ATAPI_DEVICE(atadev))
1190	    break;
1191
1192	/* if controller says its setup for DMA take the easy way out */
1193	/* the downside is we dont know what DMA mode we are in */
1194	if ((udmamode >= 0 || wdmamode >= 2) &&
1195	    (ATA_INB(atadev->channel->r_bmio, ATA_BMSTAT_PORT) &
1196	     (device ? ATA_BMSTAT_DMA_SLAVE : ATA_BMSTAT_DMA_MASTER))) {
1197	    ata_dmacreate(atadev, apiomode, ATA_DMA);
1198	    return;
1199	}
1200
1201	/* well, we have no support for this, but try anyways */
1202	if ((wdmamode >= 2 && apiomode >= 4) && atadev->channel->r_bmio) {
1203	    error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1204				ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
1205	    if (bootverbose)
1206		ata_prtdev(atadev, "%s setting WDMA2 on generic chip\n",
1207			   (error) ? "failed" : "success");
1208	    if (!error) {
1209		ata_dmacreate(atadev, apiomode, ATA_WDMA2);
1210		return;
1211	    }
1212	}
1213    }
1214    error = ata_command(atadev, ATA_C_SETFEATURES, 0, ATA_PIO0 + apiomode,
1215			ATA_C_F_SETXFER, ATA_WAIT_READY);
1216    if (bootverbose)
1217	ata_prtdev(atadev, "%s setting PIO%d on generic chip\n",
1218		   (error) ? "failed" : "success", apiomode < 0 ? 0 : apiomode);
1219    if (!error)
1220	atadev->mode = ATA_PIO0 + apiomode;
1221    else {
1222	if (bootverbose)
1223	    ata_prtdev(atadev, "using PIO mode set by BIOS\n");
1224	atadev->mode = ATA_PIO;
1225    }
1226}
1227
1228struct ata_dmasetup_data_cb_args {
1229    struct ata_dmaentry *dmatab;
1230    int error;
1231};
1232
1233static void
1234ata_dmasetupd_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
1235{
1236    struct ata_dmasetup_data_cb_args *cba =
1237	(struct ata_dmasetup_data_cb_args *)xsc;
1238    bus_size_t cnt;
1239    u_int32_t lastcount;
1240    int i, j;
1241
1242    cba->error = error;
1243    if (error != 0)
1244	return;
1245    lastcount = j = 0;
1246    for (i = 0; i < nsegs; i++) {
1247	/*
1248	 * A maximum segment size was specified for bus_dma_tag_create, but
1249	 * some busdma code does not seem to honor this, so fix up if needed.
1250	 */
1251	for (cnt = 0; cnt < segs[i].ds_len; cnt += MAXSEGSZ, j++) {
1252	    cba->dmatab[j].base = htole32(segs[i].ds_addr + cnt);
1253	    lastcount = ulmin(segs[i].ds_len - cnt, MAXSEGSZ) & 0xffff;
1254	    cba->dmatab[j].count = htole32(lastcount);
1255	}
1256    }
1257    cba->dmatab[j - 1].count = htole32(lastcount | ATA_DMA_EOT);
1258}
1259
1260int
1261ata_dmasetup(struct ata_device *atadev, caddr_t data, int32_t count)
1262{
1263    struct ata_channel *ch = atadev->channel;
1264    struct ata_dmastate *ds = &atadev->dmastate;
1265    struct ata_dmasetup_data_cb_args cba;
1266
1267    if (ds->flags & ATA_DS_ACTIVE)
1268	    panic("ata_dmasetup: transfer active on this device!");
1269
1270    if (((uintptr_t)data & ch->alignment) || (count & ch->alignment)) {
1271	ata_prtdev(atadev, "non aligned DMA transfer attempted\n");
1272	return -1;
1273    }
1274
1275    if (!count) {
1276	ata_prtdev(atadev, "zero length DMA transfer attempted\n");
1277	return -1;
1278    }
1279
1280    cba.dmatab = ds->dmatab;
1281    if (bus_dmamap_load(ds->ddmatag, ds->ddmamap, data, count,
1282			ata_dmasetupd_cb, &cba, 0) || cba.error)
1283    ds->flags = ATA_DS_ACTIVE;
1284    bus_dmamap_sync(ds->cdmatag, ds->cdmamap, BUS_DMASYNC_PREWRITE);
1285
1286    return 0;
1287}
1288
1289void
1290ata_dmastart(struct ata_device *atadev, int dir)
1291{
1292    struct ata_channel *ch;
1293    struct ata_dmastate *ds;
1294
1295    ch = atadev->channel;
1296    ds = &atadev->dmastate;
1297    bus_dmamap_sync(ds->ddmatag, ds->ddmamap, dir ? BUS_DMASYNC_PREREAD :
1298		    BUS_DMASYNC_PREWRITE);
1299    if (dir)
1300	    ds->flags |= ATA_DS_READ;
1301    ch->flags |= ATA_DMA_ACTIVE;
1302    ATA_OUTL(ch->r_bmio, ATA_BMDTP_PORT, ds->mdmatab);
1303    ATA_OUTB(ch->r_bmio, ATA_BMCMD_PORT, dir ? ATA_BMCMD_WRITE_READ : 0);
1304    ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT,
1305	 (ATA_INB(ch->r_bmio, ATA_BMSTAT_PORT) |
1306	  (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
1307    ATA_OUTB(ch->r_bmio, ATA_BMCMD_PORT,
1308	 ATA_INB(ch->r_bmio, ATA_BMCMD_PORT) | ATA_BMCMD_START_STOP);
1309}
1310
1311int
1312ata_dmadone(struct ata_device *atadev)
1313{
1314    struct ata_channel *ch;
1315    struct ata_dmastate *ds;
1316    int error;
1317
1318    ch = atadev->channel;
1319    ds = &atadev->dmastate;
1320    bus_dmamap_sync(ds->ddmatag, ds->ddmamap, (ds->flags & ATA_DS_READ) != 0 ?
1321		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1322    bus_dmamap_unload(ds->ddmatag, ds->ddmamap);
1323    ATA_OUTB(ch->r_bmio, ATA_BMCMD_PORT,
1324		ATA_INB(ch->r_bmio, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
1325    ch->flags &= ~ATA_DMA_ACTIVE;
1326    error = ATA_INB(ch->r_bmio, ATA_BMSTAT_PORT);
1327    ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT,
1328	     error | ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
1329    ds->flags = 0;
1330    return error & ATA_BMSTAT_MASK;
1331}
1332
1333int
1334ata_dmastatus(struct ata_channel *ch)
1335{
1336    return ATA_INB(ch->r_bmio, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1337}
1338
1339static void
1340cyrix_timing(struct ata_device *atadev, int devno, int mode)
1341{
1342    u_int32_t reg20 = 0x0000e132;
1343    u_int32_t reg24 = 0x00017771;
1344
1345    switch (mode) {
1346    case ATA_PIO0:	reg20 = 0x0000e132; break;
1347    case ATA_PIO1:	reg20 = 0x00018121; break;
1348    case ATA_PIO2:	reg20 = 0x00024020; break;
1349    case ATA_PIO3:	reg20 = 0x00032010; break;
1350    case ATA_PIO4:	reg20 = 0x00040010; break;
1351    case ATA_WDMA2:	reg24 = 0x00002020; break;
1352    case ATA_UDMA2:	reg24 = 0x00911030; break;
1353    }
1354    ATA_OUTL(atadev->channel->r_bmio, (devno << 3) + 0x20, reg20);
1355    ATA_OUTL(atadev->channel->r_bmio, (devno << 3) + 0x24, reg24);
1356}
1357
1358static void
1359promise_timing(struct ata_device *atadev, int devno, int mode)
1360{
1361    u_int32_t timing = 0;
1362    /* XXX: Endianess */
1363    struct promise_timing {
1364	u_int8_t  pa:4;
1365	u_int8_t  prefetch:1;
1366	u_int8_t  iordy:1;
1367	u_int8_t  errdy:1;
1368	u_int8_t  syncin:1;
1369	u_int8_t  pb:5;
1370	u_int8_t  mb:3;
1371	u_int8_t  mc:4;
1372	u_int8_t  dmaw:1;
1373	u_int8_t  dmar:1;
1374	u_int8_t  iordyp:1;
1375	u_int8_t  dmarqp:1;
1376	u_int8_t  reserved:8;
1377    } *t = (struct promise_timing*)&timing;
1378
1379    t->iordy = 1; t->iordyp = 1;
1380    if (mode >= ATA_DMA) {
1381	t->prefetch = 1; t->errdy = 1; t->syncin = 1;
1382    }
1383
1384    switch (atadev->channel->chiptype) {
1385    case 0x4d33105a:  /* Promise Ultra/Fasttrak 33 */
1386	switch (mode) {
1387	default:
1388	case ATA_PIO0:	t->pa =	 9; t->pb = 19; t->mb = 7; t->mc = 15; break;
1389	case ATA_PIO1:	t->pa =	 5; t->pb = 12; t->mb = 7; t->mc = 15; break;
1390	case ATA_PIO2:	t->pa =	 3; t->pb =  8; t->mb = 7; t->mc = 15; break;
1391	case ATA_PIO3:	t->pa =	 2; t->pb =  6; t->mb = 7; t->mc = 15; break;
1392	case ATA_PIO4:	t->pa =	 1; t->pb =  4; t->mb = 7; t->mc = 15; break;
1393	case ATA_WDMA2: t->pa =	 3; t->pb =  7; t->mb = 3; t->mc =  3; break;
1394	case ATA_UDMA2: t->pa =	 3; t->pb =  7; t->mb = 1; t->mc =  1; break;
1395	}
1396	break;
1397
1398    case 0x4d38105a:  /* Promise Ultra/Fasttrak 66 */
1399    case 0x4d30105a:  /* Promise Ultra/Fasttrak 100 */
1400    case 0x0d30105a:  /* Promise OEM ATA 100 */
1401	switch (mode) {
1402	default:
1403	case ATA_PIO0:	t->pa = 15; t->pb = 31; t->mb = 7; t->mc = 15; break;
1404	case ATA_PIO1:	t->pa = 10; t->pb = 24; t->mb = 7; t->mc = 15; break;
1405	case ATA_PIO2:	t->pa =	 6; t->pb = 16; t->mb = 7; t->mc = 15; break;
1406	case ATA_PIO3:	t->pa =	 4; t->pb = 12; t->mb = 7; t->mc = 15; break;
1407	case ATA_PIO4:	t->pa =	 2; t->pb =  8; t->mb = 7; t->mc = 15; break;
1408	case ATA_WDMA2: t->pa =	 6; t->pb = 14; t->mb = 6; t->mc =  6; break;
1409	case ATA_UDMA2: t->pa =	 6; t->pb = 14; t->mb = 2; t->mc =  2; break;
1410	case ATA_UDMA4: t->pa =	 3; t->pb =  7; t->mb = 1; t->mc =  1; break;
1411	case ATA_UDMA5: t->pa =	 3; t->pb =  7; t->mb = 1; t->mc =  1; break;
1412	}
1413	break;
1414    }
1415    pci_write_config(device_get_parent(atadev->channel->dev),
1416		     0x60 + (devno << 2), timing, 4);
1417}
1418
1419static void
1420hpt_timing(struct ata_device *atadev, int devno, int mode)
1421{
1422    device_t parent = device_get_parent(atadev->channel->dev);
1423    u_int32_t chiptype = atadev->channel->chiptype;
1424    int chiprev = pci_get_revid(parent);
1425    u_int32_t timing;
1426
1427    if (chiptype == 0x00081103 && chiprev >= 0x07) {
1428	switch (mode) {						/* HPT374 */
1429	case ATA_PIO0:	timing = 0x0ac1f48a; break;
1430	case ATA_PIO1:	timing = 0x0ac1f465; break;
1431	case ATA_PIO2:	timing = 0x0a81f454; break;
1432	case ATA_PIO3:	timing = 0x0a81f443; break;
1433	case ATA_PIO4:	timing = 0x0a81f442; break;
1434	case ATA_WDMA2: timing = 0x22808242; break;
1435	case ATA_UDMA2: timing = 0x120c8242; break;
1436	case ATA_UDMA4: timing = 0x12ac8242; break;
1437	case ATA_UDMA5: timing = 0x12848242; break;
1438	case ATA_UDMA6: timing = 0x12808242; break;
1439	default:	timing = 0x0d029d5e;
1440	}
1441    }
1442    else if ((chiptype == 0x00041103 && chiprev >= 0x05) ||
1443	     (chiptype == 0x00051103 && chiprev >= 0x01)) {
1444	switch (mode) {						/* HPT372 */
1445	case ATA_PIO0:	timing = 0x0d029d5e; break;
1446	case ATA_PIO1:	timing = 0x0d029d26; break;
1447	case ATA_PIO2:	timing = 0x0c829ca6; break;
1448	case ATA_PIO3:	timing = 0x0c829c84; break;
1449	case ATA_PIO4:	timing = 0x0c829c62; break;
1450	case ATA_WDMA2: timing = 0x2c829262; break;
1451	case ATA_UDMA2: timing = 0x1c91dc62; break;
1452	case ATA_UDMA4: timing = 0x1c8ddc62; break;
1453	case ATA_UDMA5: timing = 0x1c6ddc62; break;
1454	case ATA_UDMA6: timing = 0x1c81dc62; break;
1455	default:	timing = 0x0d029d5e;
1456	}
1457    }
1458    else if (chiptype == 0x00041103 && chiprev >= 0x03) {
1459	switch (mode) {						/* HPT370 */
1460	case ATA_PIO0:	timing = 0x06914e57; break;
1461	case ATA_PIO1:	timing = 0x06914e43; break;
1462	case ATA_PIO2:	timing = 0x06514e33; break;
1463	case ATA_PIO3:	timing = 0x06514e22; break;
1464	case ATA_PIO4:	timing = 0x06514e21; break;
1465	case ATA_WDMA2: timing = 0x26514e21; break;
1466	case ATA_UDMA2: timing = 0x16494e31; break;
1467	case ATA_UDMA4: timing = 0x16454e31; break;
1468	case ATA_UDMA5: timing = 0x16454e31; break;
1469	default:	timing = 0x06514e57;
1470	}
1471	pci_write_config(parent, 0x40 + (devno << 2) , timing, 4);
1472    }
1473    else {							/* HPT36[68] */
1474	switch (pci_read_config(parent, 0x41 + (devno << 2), 1)) {
1475	case 0x85:	/* 25Mhz */
1476	    switch (mode) {
1477	    case ATA_PIO0:	timing = 0x40d08585; break;
1478	    case ATA_PIO1:	timing = 0x40d08572; break;
1479	    case ATA_PIO2:	timing = 0x40ca8542; break;
1480	    case ATA_PIO3:	timing = 0x40ca8532; break;
1481	    case ATA_PIO4:	timing = 0x40ca8521; break;
1482	    case ATA_WDMA2:	timing = 0x20ca8521; break;
1483	    case ATA_UDMA2:	timing = 0x10cf8521; break;
1484	    case ATA_UDMA4:	timing = 0x10c98521; break;
1485	    default:		timing = 0x01208585;
1486	    }
1487	    break;
1488	default:
1489	case 0xa7:	/* 33MHz */
1490	    switch (mode) {
1491	    case ATA_PIO0:	timing = 0x40d0a7aa; break;
1492	    case ATA_PIO1:	timing = 0x40d0a7a3; break;
1493	    case ATA_PIO2:	timing = 0x40d0a753; break;
1494	    case ATA_PIO3:	timing = 0x40c8a742; break;
1495	    case ATA_PIO4:	timing = 0x40c8a731; break;
1496	    case ATA_WDMA2:	timing = 0x20c8a731; break;
1497	    case ATA_UDMA2:	timing = 0x10caa731; break;
1498	    case ATA_UDMA4:	timing = 0x10c9a731; break;
1499	    default:		timing = 0x0120a7a7;
1500	    }
1501	    break;
1502	case 0xd9:	/* 40Mhz */
1503	    switch (mode) {
1504	    case ATA_PIO0:	timing = 0x4018d9d9; break;
1505	    case ATA_PIO1:	timing = 0x4010d9c7; break;
1506	    case ATA_PIO2:	timing = 0x4010d997; break;
1507	    case ATA_PIO3:	timing = 0x4010d974; break;
1508	    case ATA_PIO4:	timing = 0x4008d963; break;
1509	    case ATA_WDMA2:	timing = 0x2008d943; break;
1510	    case ATA_UDMA2:	timing = 0x100bd943; break;
1511	    case ATA_UDMA4:	timing = 0x100fd943; break;
1512	    default:		timing = 0x0120d9d9;
1513	    }
1514	}
1515    }
1516    pci_write_config(parent, 0x40 + (devno << 2) , timing, 4);
1517}
1518
1519static int
1520hpt_cable80(struct ata_device *atadev)
1521{
1522    device_t parent = device_get_parent(atadev->channel->dev);
1523    u_int8_t reg, val, res;
1524
1525    if (atadev->channel->chiptype==0x00081103 && pci_get_function(parent)==1) {
1526	reg = atadev->channel->unit ? 0x57 : 0x53;
1527	val = pci_read_config(parent, reg, 1);
1528	pci_write_config(parent, reg, val | 0x80, 1);
1529    }
1530    else {
1531	reg = 0x5b;
1532	val = pci_read_config(parent, reg, 1);
1533	pci_write_config(parent, reg, val & 0xfe, 1);
1534    }
1535    res = pci_read_config(parent, 0x5a, 1) & (atadev->channel->unit ? 0x1:0x2);
1536    pci_write_config(parent, reg, val, 1);
1537    return !res;
1538}
1539