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